diff options
author | Ingo Molnar <mingo@elte.hu> | 2008-11-29 14:45:15 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2008-11-29 14:45:15 -0500 |
commit | af6d596fd603219b054c1c90fb16672a9fd441bd (patch) | |
tree | e3e5a85532079aedc9b0b194a319ec084b8a4449 | |
parent | 1583715ddb61f822041807a0f18b3b4845e88c76 (diff) |
sched: prevent divide by zero error in cpu_avg_load_per_task, update
Regarding the bug addressed in:
4cd4262: sched: prevent divide by zero error in cpu_avg_load_per_task
Linus points out that the fix is not complete:
> There's nothing that keeps gcc from deciding not to reload
> rq->nr_running.
>
> Of course, in _practice_, I don't think gcc ever will (if it decides
> that it will spill, gcc is likely going to decide that it will
> literally spill the local variable to the stack rather than decide to
> reload off the pointer), but it's a valid compiler optimization, and
> it even has a name (rematerialization).
>
> So I suspect that your patch does fix the bug, but it still leaves the
> fairly unlikely _potential_ for it to re-appear at some point.
>
> We have ACCESS_ONCE() as a macro to guarantee that the compiler
> doesn't rematerialize a pointer access. That also would clarify
> the fact that we access something unsafe outside a lock.
So make sure our nr_running value is immutable and cannot change
after we check it for nonzero.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
-rw-r--r-- | kernel/sched.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/kernel/sched.c b/kernel/sched.c index 700aa9a1413f..b7480fb5c3dc 100644 --- a/kernel/sched.c +++ b/kernel/sched.c | |||
@@ -1453,7 +1453,7 @@ static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); | |||
1453 | static unsigned long cpu_avg_load_per_task(int cpu) | 1453 | static unsigned long cpu_avg_load_per_task(int cpu) |
1454 | { | 1454 | { |
1455 | struct rq *rq = cpu_rq(cpu); | 1455 | struct rq *rq = cpu_rq(cpu); |
1456 | unsigned long nr_running = rq->nr_running; | 1456 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); |
1457 | 1457 | ||
1458 | if (nr_running) | 1458 | if (nr_running) |
1459 | rq->avg_load_per_task = rq->load.weight / nr_running; | 1459 | rq->avg_load_per_task = rq->load.weight / nr_running; |