From: Matthew Wilcox <matthew@wil.cx>
To: linux-ia64@vger.kernel.org
Subject: Re: test_and_set_bit implementation
Date: Tue, 12 Dec 2006 15:47:46 +0000 [thread overview]
Message-ID: <20061212154746.GI21070@parisc-linux.org> (raw)
In-Reply-To: <457EC42C.90002@bull.net>
On Tue, Dec 12, 2006 at 04:01:00PM +0100, Zoltan Menyhart wrote:
> Let's assume the bit test & set is already set, why is then the
> cmpxchg_acq() executed? Cannot we just return, e.g. like this?
>
> do {
> old = *m;
> if (old & bit)
> return 1;
> new = old | bit;
> } while (cmpxchg_acq(m, old, new) != old);
The original code and your rewrite both access memory twice in the loop.
Why don't we do it with one memory reference per loop instead?
{
CMPXCHG_BUGCHECK_DECL
u32 *m = (u32 *)addr + (nr >> 5);
u32 bit = 1 << (nr & 31);
u32 old = *m;
while (!(old & bit)) {
u32 new = old | bit;
u32 prev = cmpxchg_acq(m, old, new);
CMPXCHG_BUGCHECK(m);
if (prev = old)
return 1;
old = prev;
}
return 0;
}
Looking at the disassembly of grab_block() in fs/ext2/balloc.c, I don't
see much difference. The ld4.acq turns into a regular ld4 (because
'm' is no longer tagged as volatile), and is hoisted out of the loop.
Interestingly, gcc chooses to reorder the tests, and make the loop four
bundles long instead of three, but will 'goto repeat' in two bundles
instead of four. Using likely()/unlikely() doesn't persuade gcc to
change the order of the two branches, so I assume it actually is better
to do it this way.
next prev parent reply other threads:[~2006-12-12 15:47 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-12-12 15:01 test_and_set_bit implementation Zoltan Menyhart
2006-12-12 15:47 ` Matthew Wilcox [this message]
2006-12-12 17:20 ` Christoph Lameter
2006-12-12 17:22 ` Christoph Lameter
2006-12-12 18:07 ` Matthew Wilcox
2006-12-13 10:02 ` Zoltan Menyhart
2006-12-13 10:20 ` Zoltan Menyhart
2006-12-13 12:29 ` Matthew Wilcox
2006-12-13 18:28 ` Christoph Lameter
2006-12-14 9:24 ` Zoltan Menyhart
2006-12-14 9:37 ` Zoltan Menyhart
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=20061212154746.GI21070@parisc-linux.org \
--to=matthew@wil.cx \
--cc=linux-ia64@vger.kernel.org \
/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