From 84bca3a7cbb67e019234fe7a78fb8e1df98bad35 Mon Sep 17 00:00:00 2001 From: Xie XiuQi Date: Fri, 3 May 2019 16:18:53 +0800 Subject: [PATCH] sched/numa: Fix a possible divide-by-zero commit a860fa7b96e1a1c974556327aa1aee852d434c21 upstream. sched_clock_cpu() may not be consistent between CPUs. If a task migrates to another CPU, then se.exec_start is set to that CPU's rq_clock_task() by update_stats_curr_start(). Specifically, the new value might be before the old value due to clock skew. So then if in numa_get_avg_runtime() the expression: 'now - p->last_task_numa_placement' ends up as -1, then the divider '*period + 1' in task_numa_placement() is 0 and things go bang. Similar to update_curr(), check if time goes backwards to avoid this. [ peterz: Wrote new changelog. ] [ mingo: Tweaked the code comment. ] Signed-off-by: Xie XiuQi Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: cj.chengjian@huawei.com Cc: Link: http://lkml.kernel.org/r/20190425080016.GX11158@hirez.programming.kicks-ass.net Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman Signed-off-by: Yang Yingliang Conflicts: kernel/sched/fair.c [yyl: adjust context] Signed-off-by: Yang Yingliang --- kernel/sched/fair.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 2decb431071e..87dc36f7800d 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2046,8 +2046,8 @@ static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) delta = runtime - p->last_sum_exec_runtime; *period = now - p->last_task_numa_placement; - /* Avoid backward, and prevent potential divide error */ - if ((s64)*period < 0) + /* Avoid time going backwards, prevent potential divide error: */ + if (unlikely((s64)*period < 0)) *period = 0; } else { delta = p->se.avg.load_sum; -- GitLab