From: Linus Torvalds <torvalds@linux-foundation.org>
To: Jarek Poplawski <jarkao2@o2.pl>
Cc: Ingo Molnar <mingo@elte.hu>, Miklos Szeredi <miklos@szeredi.hu>,
cebbert@redhat.com, chris@atlee.ca, linux-kernel@vger.kernel.org,
tglx@linutronix.de, akpm@linux-foundation.org
Subject: Re: [BUG] long freezes on thinkpad t60
Date: Wed, 20 Jun 2007 10:34:15 -0700 (PDT) [thread overview]
Message-ID: <alpine.LFD.0.98.0706201018290.3593@woody.linux-foundation.org> (raw)
In-Reply-To: <20070620093612.GA1626@ff.dom.local>
On Wed, 20 Jun 2007, Jarek Poplawski wrote:
>
> I don't agree with this (+ I know it doesn't matter).
>
> The real bug is what Chuck Ebbert wrote: "Spinlocks aren't fair".
> And here they are simply lawlessly not fair.
Well, that's certainly a valid standpoint. I wouldn't claim you're
_wrong_.
At least in theory.
In *practice*, fair spinlocks are simply not possible. Not as in "it's
hard to do", but as in "you simply cannot do it".
The thing is, most spinlocks get held for a rather short time (if that
wasn't the case, you'd use something like a mutex), and it's acually a
short time not on a "human scale", but on a "CPU core scale".
IOW, the cost of doing the cacheline bouncing is often equal to, or bigger
than, the actual cost of the operation that happens inside the spinlock!
What does this result in? It automatically means that software simply
*cannot* do fair spinlocks, because all the timing costs are in parts that
software doesn't even have any visibility into, or control over!
Yeah, you could add artificial delays, by doing things like cycle counting
around the spinlock operation to see if you got the lock when it was
_contended_, or whether you got a lock that wasn't, and then adding some
statistics etc, but at that point, you don't have a spinlock any more, you
have something else. I don't know what to call it.
And you could add flags like "this spinlock is getting a lot of
contention", try some other algorithm etc. But it's all complicated and
against the whole *point* of a spinlock, which is to get in and out as
fast as humanly possible.
So in practice, spinlock fairness is inherently tied to the hardware
behaviour of the cache coherency algorithm.
Which gets us to the next level: we can consider hardware that isn't
"fair" in its cache coherency to be buggy hardware.
That's also a perfectly fine standpoint, and it's actually one that I have
a lot of sympathy for. I think it's much easier to some degree to do
fairness in the cache coherency than at any other level, because it's
something where the hardware really *could* do things like counting
bouncing etc.
However, while it's a perfectly fine standpoint, it's also totally
unrealistic. First off, the hardware doesn't even know whether the
spinlock "failed" or not on any architecture platform I'm aware of. On
x86, the spinlock operation under Linux is actually just an atomic
decrement, and it so happens that the rules for failure was that it didn't
go negative. But that's just a Linux internal rule - the hardware doesn't
know.
So you'd have to actualyl have some specific sequence with specific
fairness rules (maybe you could make the rule be that a failed atomic
"cmpxchg" counts as a real failure, and if you give higher priority to
cores with lots of failures etc).
IOW, it's certainly possible *in*theory* to try to make hardware that has
fairness guarantees. However, any hardware designer will tell you that
(a) they have enough problems as it is
(b) it's simply not worth their time, since there are other (simpler)
things that are much much more important.
So the end result:
- hardware won't do it, and they'd be crazy to really even try (apart
from maybe some really simplistic stuff that doesn't _guarantee_
anything, but maybe helps fairness a bit)
- software cannot do it, without turning spinlocks into something really
slow and complicated, at which point they've lost all meaning, and you
should just use a higher-level construct like a mutex instead.
In other words, spinlocks are optimized for *lack* of contention. If a
spinlock has contention, you don't try to make the spinlock "fair". No,
you try to fix the contention instead!
That way, you fix many things. You probably speed things up, _and_ you
make it fairer. It might sometimes take some brains and effort, but it's
worth it.
The patch I sent out was an example of that. You *can* fix contention
problems. Does it take clever approaches? Yes. It's why we have hashed
spinlocks, RCU, and code sequences that are entirely lockless and use
optimistic approaches. And suddenly you get fairness *and* performance!
It's a win-win situation. It does require a bit of effort, but hey, we're
good at effort.
Linus
next prev parent reply other threads:[~2007-06-20 17:36 UTC|newest]
Thread overview: 88+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-24 12:04 Miklos Szeredi
2007-05-24 12:54 ` Ingo Molnar
2007-05-24 14:03 ` Miklos Szeredi
2007-05-24 14:10 ` Ingo Molnar
2007-05-24 14:28 ` Miklos Szeredi
2007-05-24 14:42 ` Ingo Molnar
2007-05-24 14:44 ` Ingo Molnar
2007-05-24 17:09 ` Miklos Szeredi
2007-05-24 21:01 ` Ingo Molnar
2007-05-25 9:51 ` Miklos Szeredi
2007-06-14 16:04 ` Miklos Szeredi
2007-06-15 21:25 ` Chuck Ebbert
2007-06-16 10:37 ` Ingo Molnar
2007-06-17 21:46 ` Miklos Szeredi
2007-06-18 6:43 ` Ingo Molnar
2007-06-18 7:24 ` Miklos Szeredi
2007-06-18 8:12 ` Ingo Molnar
2007-06-18 8:20 ` Andrew Morton
2007-06-19 4:22 ` Ravikiran G Thirumalai
2007-06-18 8:25 ` Miklos Szeredi
2007-06-18 8:31 ` Ingo Molnar
2007-06-18 8:34 ` Miklos Szeredi
2007-06-18 9:18 ` Ingo Molnar
2007-06-18 9:38 ` Ingo Molnar
2007-06-18 9:44 ` Ingo Molnar
2007-06-18 10:18 ` Miklos Szeredi
2007-06-18 12:36 ` Ingo Molnar
2007-06-18 13:10 ` Miklos Szeredi
2007-06-18 16:34 ` Linus Torvalds
2007-06-18 17:41 ` Miklos Szeredi
2007-06-18 17:48 ` Linus Torvalds
2007-06-18 18:02 ` Ingo Molnar
2007-06-18 18:00 ` Ingo Molnar
2007-06-18 18:25 ` Linus Torvalds
2007-06-20 9:36 ` Jarek Poplawski
2007-06-20 17:34 ` Linus Torvalds [this message]
2007-06-21 7:30 ` Ingo Molnar
2007-06-21 15:50 ` Linus Torvalds
2007-06-21 16:08 ` Ingo Molnar
2007-06-21 16:32 ` Linus Torvalds
2007-06-21 16:44 ` Chuck Ebbert
2007-06-21 17:31 ` Linus Torvalds
2007-06-21 18:29 ` Eric Dumazet
2007-06-21 18:44 ` Linus Torvalds
2007-06-21 19:35 ` Linus Torvalds
2007-06-21 20:09 ` Ingo Molnar
2007-06-21 20:14 ` Linus Torvalds
2007-06-21 20:30 ` Ingo Molnar
2007-06-21 20:48 ` Linus Torvalds
2007-06-21 21:06 ` Ingo Molnar
2007-06-21 20:42 ` [patch] spinlock debug: make looping nicer Ingo Molnar
2007-06-21 20:58 ` Linus Torvalds
2007-06-21 21:15 ` Ingo Molnar
2007-06-22 7:00 ` Jarek Poplawski
2007-06-21 20:36 ` [BUG] long freezes on thinkpad t60 Eric Dumazet
2007-06-21 19:56 ` Ingo Molnar
2007-06-21 20:10 ` Linus Torvalds
2007-06-21 20:23 ` Ingo Molnar
2007-06-21 20:12 ` Ingo Molnar
2007-06-26 8:42 ` Nick Piggin
2007-06-26 10:56 ` Jarek Poplawski
2007-06-26 17:23 ` Linus Torvalds
2007-06-27 5:23 ` Nick Piggin
2007-06-27 6:04 ` Linus Torvalds
2007-06-27 6:20 ` Nick Piggin
2007-06-27 19:47 ` Linus Torvalds
2007-06-27 20:10 ` Ingo Molnar
2007-06-27 20:17 ` Davide Libenzi
2007-06-27 22:11 ` Linus Torvalds
2007-06-27 23:30 ` Davide Libenzi
2007-06-28 0:46 ` Linus Torvalds
2007-06-28 3:03 ` Davide Libenzi
2007-07-02 7:06 ` Nick Piggin
2007-06-21 20:16 ` Ingo Molnar
2007-06-22 8:17 ` Ingo Molnar
2007-06-23 10:36 ` Miklos Szeredi
2007-06-23 16:39 ` Linus Torvalds
2007-06-25 6:45 ` Jarek Poplawski
2007-06-21 20:18 ` Ingo Molnar
2007-06-21 20:36 ` Linus Torvalds
2007-06-21 7:38 ` Jarek Poplawski
2007-06-21 8:39 ` Ingo Molnar
2007-06-21 11:09 ` Jarek Poplawski
2007-06-21 16:01 ` Linus Torvalds
2007-06-22 10:38 ` Jarek Poplawski
2007-05-24 22:08 ` Henrique de Moraes Holschuh
2007-05-24 22:13 ` Kok, Auke
2007-05-25 6:58 ` Ingo Molnar
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.0.98.0706201018290.3593@woody.linux-foundation.org \
--to=torvalds@linux-foundation.org \
--cc=akpm@linux-foundation.org \
--cc=cebbert@redhat.com \
--cc=chris@atlee.ca \
--cc=jarkao2@o2.pl \
--cc=linux-kernel@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=mingo@elte.hu \
--cc=tglx@linutronix.de \
/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
all inboxes | Powered by JetHome®