Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()

From: Fabian Frederick
Date: Sat Feb 21 2015 - 02:43:00 EST




> On 20 February 2015 at 19:58 Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
>
> On Fri, Feb 20, 2015 at 09:34:28PM +0300, Sergei Shtylyov wrote:
> > Hello.
> >
> > On 02/20/2015 09:12 PM, Fabian Frederick wrote:
> >
> > >Use helper functions to access current->state.
> > >Direct assignments are prone to races and therefore buggy.
> >
> > >current->state = TASK_RUNNING is replaced by __set_current_state()
> >
> >Â Â You sometimes use __set_current_state() and sometimes
> >set_current_state().
>
> It depends on which state; setting yourself TASK_RUNNING is free of
> wakeup races -- you're already running after all, so it can safely use
> __set_current_state().
>
> Setting a blocking state otoh needs set_current_state() which issues a
> full memory barriers with the store (critically in this case,
> effectively after the store) such that it orders the state store with a
> subsequent load in the condition check if it really needs to go to
> sleep.
>
>
> In full:
>
>Â Â Â Âcurrent->state = TASK_UNINTERRUPTIBLE;Â Â Â Â Â wait = false;
>Â Â Â Âsmp_mb();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âsmp_wmb();
>Â Â Â Âif (wait)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âp->state = TASK_RUNNING;
>Â Â Â Â Â Â Â Âschedule();
>
> Without that smp_mb(); the following order is possible:
>
>Â Â Â Âif (wait)
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âwait = false;
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âsmp_wmb();
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âp->state = TASK_RUNNING;
>Â Â Â Âcurrent->state = TASK_UNINTERRUPTIBLE;
>Â Â Â Â Â Â Â Âschedule();
>
> And we'll wait forever more..
Do I have to add more comments in changelogs or is it OK for you ?
Maybe something like:
"
current->state = TASK_RUNNING can safely be converted to __set_current_state()
as we're already in that state. Other assignments are converted to
set_current_state() (which uses set_mb()).

"
Regards,
Fabian
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/