mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Arnd Bergmann <arnd@arndb.de>, Ingo Molnar <mingo@elte.hu>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Tony Breeds <tonyb@au1.ibm.com>
Subject: Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL
Date: Tue, 11 May 2010 11:06:42 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.2.00.1005111051330.3711@i5.linux-foundation.org> (raw)
In-Reply-To: <1273478159.5605.3324.camel@twins>



On Mon, 10 May 2010, Peter Zijlstra wrote:
> 
> As to the 2 jiffy spin timeout, I guess we should add a lockdep warning
> for that, because anybody holding a mutex for longer than 2 jiffies and
> not sleeping does need fixing anyway.

I really hate the jiffies thing, but looking at the optimistic spinning, I 
do wonder about two things..

First - we check "need_resched()" only if owner is NULL. That sounds 
wrong. If we need to reschedule, we need to stop spinning _regardless_ of 
whether the owner may have been preempted before setting the owner field.

Second: we allow "owner" to change, and we'll continue spinning. This is 
how you can end up spinning for a long time - not because anybody holds 
the mutex for longer than 2 jiffies, but because a lot of other threads 
_together_ hold the mutex for longer than 2 jiffies.

Now, I think we do want some limited "continue spinning even if somebody 
else ended up getting it instead", but I think we should at least limit 
it. Otherwise we end up being potentially rather unfair, since we don't 
have any fair queueing logic for the optimistic spinning phase.

Now, we could just count the number of times "owner" has changed, and I 
suspect that would be sufficient. Now, that trivial counting sceme would 
fail if "owner" stays the same (ie the same process re-takes the lock over 
and over again, possibly due to hot cacheline things being very unfair 
to the person who already owns it), but quite frankly, I don't think we 
can get into that kind of situation. 

Why? Mutexes may end up being very heavily contended, but they can't be 
contended by just _one_ thread. So if we're really in a starvation issue, 
the thread that is waiting _will_ see multiple different owners.

So once you have seen X number of other owners, you just say "screw it, 
this spinning thing isn't working for me, I'll go to the sleeping case".

Of course, that is _really_ unfair. It will make it all that easier for 
the other spinners to just get the lock by spinning. We used to have some 
logic to say "we won't spin if there are proper waiters", but that was 
horrible for performance. See commit ac6e60ee405.

Of course, it's quite possible that as long as "need_resched()" isn't set, 
spinning really _is_ the right thing to do. Maybe it causes horrible CPU 
load on some odd "everybody synchronize" loads, but maybe that really is 
the best we can do.

			Linus

  reply	other threads:[~2010-05-11 18:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-28  4:38 Benjamin Herrenschmidt
2010-04-28  4:39 ` Benjamin Herrenschmidt
2010-04-28 12:06 ` Arnd Bergmann
2010-04-28 22:35   ` Benjamin Herrenschmidt
2010-05-07  4:20     ` Tony Breeds
2010-05-07  5:30       ` Frederic Weisbecker
2010-05-07  6:01         ` Benjamin Herrenschmidt
2010-05-07 21:29           ` Frederic Weisbecker
2010-05-07 22:27             ` Benjamin Herrenschmidt
2010-05-10  7:55               ` Peter Zijlstra
2010-05-11 18:06                 ` Linus Torvalds [this message]
2010-05-11 18:19                   ` Peter Zijlstra
2010-05-11 21:13                   ` Benjamin Herrenschmidt
2010-05-07  6:16         ` Mike Galbraith
2010-05-11 15:43       ` [tip:core/locking] " tip-bot for Tony Breeds
2010-05-11 23:05         ` Tony Breeds
2010-05-18 16:08         ` Ingo Molnar
2010-05-18 16:26           ` Linus Torvalds
2010-05-19  5:46           ` Tony Breeds
2010-05-19  7:56             ` [tip:core/urgent] " tip-bot for Tony Breeds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.00.1005111051330.3711@i5.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=benh@kernel.crashing.org \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=peterz@infradead.org \
    --cc=tonyb@au1.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome