]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
sched: prevent divide by zero error in cpu_avg_load_per_task, update
authorIngo Molnar <mingo@elte.hu>
Sat, 29 Nov 2008 19:45:15 +0000 (20:45 +0100)
committerIngo Molnar <mingo@elte.hu>
Sat, 29 Nov 2008 19:45:15 +0000 (20:45 +0100)
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>
kernel/sched.c

index 700aa9a1413fc783028170bcfebc014b17a38120..b7480fb5c3dc21a7bf6513a978cf0ed2e8c19a8f 100644 (file)
@@ -1453,7 +1453,7 @@ static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
 static unsigned long cpu_avg_load_per_task(int cpu)
 {
        struct rq *rq = cpu_rq(cpu);
-       unsigned long nr_running = rq->nr_running;
+       unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
 
        if (nr_running)
                rq->avg_load_per_task = rq->load.weight / nr_running;