Re: [PATCH]: kthread: Fix memory ordering in __kthread_parkme

From: Peter Zijlstra
Date: Fri Nov 07 2014 - 16:27:52 EST


On Fri, Nov 07, 2014 at 07:41:03PM +0100, Oleg Nesterov wrote:
> On 11/07, Peter Zijlstra wrote:
> >
> > static void __kthread_parkme(struct kthread *self)
> > {
> > - __set_current_state(TASK_PARKED);
> > + set_current_state(TASK_PARKED);
> > while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
> > if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
> > complete(&self->parked);
> > schedule();
> > - __set_current_state(TASK_PARKED);
> > + set_current_state(TASK_PARKED);
> > }
>
> Perhaps it makses sense to do set_current_state(PARKED) once at the start
> of "for (;;)" loop, but this is cosmetic.

Yeah, we should probably clean that up, it looks a bit odd. But I didn't
want to do too many changes.

> What if kthread_unpark() is called right after test_bit(KTHREAD_SHOULD_PARK)
> and KTHREAD_IS_PARKED is not set? It seems that __kthread_unpark() should
> call wake_up_state() unconditionally ?

set_current_state(TASK_PARKED)
while (test_bit(KTHREAD_SHOULD_PARK, ..)) {
clear_bit(KTHREAD_SHOULD_PARK, ..);
if (test_and_clear_bit(KTHREAD_IS_PARKED, ..) {
...
wake_up_state(, TASK_PARKED);
}
if (!test_and_set_bit(KTHREAD_IS_PARKED, ..))
complete(..);
schedule();


Then yes we'll miss the wakeup, but we also miss the __kthread_bind().

Now I don't think this'll actually happen because kthread_park() waits
for the completion under the hotplug and smpboot_threads_lock lock, and
we do the unpark under the hotplug lock as well, so its fully serialized

But yes, we should probably clean this up as well.
--
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/