From: Matthew Wilcox <matthew@wil.cx>
To: Nick Piggin <nickpiggin@yahoo.com.au>
Cc: "Luck, Tony" <tony.luck@intel.com>,
Stephen Rothwell <sfr@canb.auug.org.au>,
linux-arch@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: down_spin() implementation
Date: Fri, 28 Mar 2008 06:45:17 -0600 [thread overview]
Message-ID: <20080328124517.GQ16721@parisc-linux.org> (raw)
In-Reply-To: <200803281101.25037.nickpiggin@yahoo.com.au>
On Fri, Mar 28, 2008 at 11:01:24AM +1100, Nick Piggin wrote:
> Uhm, how do you use this exactly? All other holders of this
> semaphore must have preempt disabled and not sleep, right? (and
> so you need a new down() that disables preempt too)
Ah, I see what you're saying. The deadlock would be (on a single CPU
machine), task A holding the semaphore, being preempted, task B taking
a spinlock (thus non-preemptable), then calling down_spin() which will
never succeed.
That hadn't occurred to me -- I'm not used to thinking about preemption.
I considered interrupt context and saw how that would deadlock, so just
put a note in the documentation that it wasn't usable from interrupts.
So it makes little sense to add this to semaphores. Better to introduce
a spinaphore, as you say.
> struct {
> atomic_t cur;
> int max;
> } ss_t;
>
> void spin_init(ss_t *ss, int max)
> {
> &ss->cur = ATOMIC_INIT(0);
> &ss->max = max;
> }
>
> void spin_take(ss_t *ss)
> {
> preempt_disable();
> while (unlikely(!atomic_add_unless(&ss->cur, 1, &ss->max))) {
> while (atomic_read(&ss->cur) == ss->max)
> cpu_relax();
> }
> }
I think we can do better here with:
atomic_set(max);
and
while (unlikely(!atomic_add_unless(&ss->cur, -1, 0)))
while (atomic_read(&ss->cur) == 0)
cpu_relax();
It still spins on the spinaphore itself rather than on a local cacheline,
so there's room for improvement. But it's not clear whether it'd be
worth it.
> About the same number as down_spin(). And it is much harder to
> misuse. So LOC isn't such a great argument for this kind of thing.
LOC wasn't really my argument -- I didn't want to introduce a new data
structure unnecessarily. But the pitfalls (that I hadn't seen) of
mixing down_spin() into semaphores are just too awful.
I'll pop this patch off the stack of semaphore patches. Thanks.
--
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours. We can't possibly take such
a retrograde step."
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2008-03-28 12:45 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-25 20:49 What if a TLB flush needed to sleep? Luck, Tony
2008-03-25 21:47 ` Alan Cox
2008-03-26 12:32 ` Matthew Wilcox
2008-03-26 20:29 ` Luck, Tony
2008-03-27 8:09 ` Jens Axboe
2008-03-27 14:15 ` down_spin() implementation Matthew Wilcox
2008-03-28 0:01 ` Nick Piggin
2008-03-28 12:45 ` Matthew Wilcox [this message]
2008-03-28 21:16 ` Luck, Tony
2008-03-28 23:48 ` Arnd Bergmann
[not found] ` <20080328124517.GQ16721-6jwH94ZQLHl74goWV3ctuw@public.gmane.org>
2008-03-29 1:04 ` Nick Piggin
2008-03-28 4:51 ` Stephen Rothwell
2008-03-28 5:03 ` Nick Piggin
2008-03-28 12:46 ` Matthew Wilcox
2008-03-28 12:51 ` Jens Axboe
2008-03-28 13:17 ` Matthew Wilcox
2008-03-28 13:24 ` Jens Axboe
2008-03-26 19:25 ` What if a TLB flush needed to sleep? Christoph Lameter
2008-03-26 20:29 ` Thomas Gleixner
2008-03-27 1:19 ` Christoph Lameter
2008-03-27 13:20 ` Peter Zijlstra
2008-03-27 18:44 ` Christoph Lameter
2008-03-28 9:59 ` Peter Zijlstra
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=20080328124517.GQ16721@parisc-linux.org \
--to=matthew@wil.cx \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nickpiggin@yahoo.com.au \
--cc=sfr@canb.auug.org.au \
--cc=tony.luck@intel.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