Re: [PATCH] It may not be assumed that wake_up(), finish_wait() and co. imply a memory barrier

David Howells
2009-04-23T20:38:23Z

Linus Torvalds <torvalds@linux-foundation.org> wrote:

> I think we _have_ to imply a smp_wmb() in the wakup semantics, because
> otherwise sleepers can't do anything sane (no amount of barriers on the
> sleeping side will help). IOW, there basically has to be an implied write
> barrier between the thing that causes an event to become true, and the
> thing that turns 'task->state' back to RUNNING.

Well, Ingo's point is it could be left up to the caller of wake_up() to supply
the barrier:

*my_variable = 1234;
smp_wmb();
wake_up(&my_queue);

or:

spin_lock(&my_lock);
*my_variable = 1234;
wake_up(&my_queue);
spin_unlock(&my_lock);

on the condition that the sleeper also gets the lock.

Also, he points out that wake_up() and co. may not insert useful memory barrier
at all, for three reasons:

(1) If there's no-one to wake up, then certain wake functions will return
immediately.

(2) If there's no-one to wake up, then other wake functions will only impose
LOCK and UNLOCK barriers.

(3) If someone supplies a special awakener instead of default_wake_function(),
then they can bypass try_to_wake_up() and whatever barrier that implies.

Though as far as I can see, if you want to wake someone up, you *have* to
go through try_to_wake_up().

What I'd like to say is that wake_up() and friends _will_ interpose at least a
write barrier _if_ they wake anything up (which is more or less what you said
above). If they don't wake anything up, then there's no need for a memory
barrier between the assignment to my_variable and the non-existent alterations
to the state of the task not being awoken.

David