Re: Active waiting with yield()

From: Peter Zijlstra
Date: Fri Nov 14 2008 - 15:57:21 EST


On Fri, 2008-11-14 at 14:34 -0500, Mikulas Patocka wrote:
>
> On Fri, 14 Nov 2008, Alan Cox wrote:
>
> > > * driver unload --- check the count of outstanding requests and call
> > > yield() repeatedly until it goes to zero, then unload.
> >
> > Use a wakeup when the request count hits zero
> >
> > > * reduced size of data structures (and reduced cache footprint for the hot
> > > path that actually processes requests)
> >
> > The CPU will predict the non-wakeup path if that is normal. You can even
> > make the wakeup use something like
> >
> > if (exiting & count == 0)
> >
> > to get the prediction righ
> >
> > > The downside of yield is slower unloading of the driver by few tens of
> > > miliseconds, but the user doesn't really care about fractions of a second
> > > when unloading drivers.
> >
> > And more power usage, plus extremely rude behaviour when virtualising.
>
> How these unlikely cases can be rude?
>
> If I have a race condition that gets triggered just for one user in the
> world when repeatedly loading & unloading a driver for an hour, and I use
> yield() to solve it, what's wrong with it? A wait queue increases cache
> footprint for every user. (even if I use preallocated hashed wait queue,
> it still eats a cacheline to access it and find out that it's empty)

Depending on the situation, yield() might be a NOP and therefore not
wait at all and possibly lock up the machine.

Consider the task in question to be the highest priority RT task on the
system, you doing: while (!condition) yield(); will lock up the system,
because whatever is to make condition true will never get a chance to
run (not considering SMP).

Clearly you don't understand it, please refrain from using it. Use
regular condition variables (waitqueues).

The rules about yield are:

- You're likely wrong, don't use it.
- Seriously, you don't need it.
- If you still think you do, goto 1.

In all of the kernel there is 1 valid use (and it might only be in the
-rt kernel - didn't check mainline recently).

The _ONLY_ valid use case of yield(), is if you have two equal priority
FIFO threads that co-depend. And that situation is almost always
avoidable.

--
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/