Re: [PATCH 1/1] sched: prevent divide by zero error incpu_avg_load_per_task

From: Ingo Molnar
Date: Sat Nov 29 2008 - 14:51:53 EST



* Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Wed, 26 Nov 2008, Steven Rostedt wrote:
> > {
> > struct rq *rq = cpu_rq(cpu);
> > + unsigned long nr_running = rq->nr_running;
> >
> > - if (rq->nr_running)
> > - rq->avg_load_per_task = rq->load.weight / rq->nr_running;
> > + if (nr_running)
> > + rq->avg_load_per_task = rq->load.weight / nr_running;
> > else
> > rq->avg_load_per_task = 0;
>
> I don't think this necessarily fixes it.
>
> 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.

Okay - i've queued up the fix below, to be on the safe side.

Ingo

---------------->