Re: [PATCH -v7][RFC]: mutex: implement adaptive spinning

From: Linus Torvalds
Date: Fri Jan 09 2009 - 11:36:18 EST




On Fri, 9 Jan 2009, Steven Rostedt wrote:
>
> I was going to say a while ago...
> In PREEMPT=y the need_resched() is not needed at all. If you have
> preemption enabled, you will get preempted in that loop. No need for the
> need_resched() in the outer loop. Although I'm not sure how it would even
> hit the "need_resched". If it was set, then it is most likely going to be
> cleared when coming back from being preempted.

No, no, you miss the point entirely.

It's not about correctness.

Remember: the whole (and only) point of spinning is about performance.

And the thing is, we should only spin if it makes sense. So the

if (need_resched())
break;

is not there because of any "ok, I need to sleep now", it's there because
of something TOTALLY DIFFERENT, namely "ok, it makes no sense to spin now,
since I should be sleeping".

See? WE DO NOT WANT TO BE PREEMPTED in this region, because that totally
destroys the whole point of the spinning. If we go through the scheduler,
then we should go through the scheduler AND GO TO SLEEP, so that we don't
go through the scheduler any more than absolutely necessary.

So this code - by design - is always only going to get worse if you have
involuntary preemption. The preemption is going to do _two_ bad things:

- it's going to call the scheduler at the wrong point, meaning that we
now scheduler _more_ (or at least not less) than if we didn't have that
spin-loop in the first place.

- .. and to make things worse, since it scheduled "for us", it is going
to clear that "need_resched()" flag, so we'll _stay_ in the bad
spinning loop too long!

So quite frankly, if you have CONFIG_PREEMPT, then the spinning really is
the wrong thing to do, or the whole mutex slow-path thing should be done
with preemption disabled so that we only schedule where we _should_ be
scheduling.

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