From: "H. Peter Anvin" <hpa@zytor.com>
To: George Spelvin <linux@horizon.com>
Cc: linux-kernel@vger.kernel.org, schwab@linux-m68k.org,
torvalds@linux-foundation.org
Subject: Re: x86-32: clean up rwsem inline asm statements
Date: Wed, 13 Jan 2010 16:39:36 -0800 [thread overview]
Message-ID: <4B4E67C8.501@zytor.com> (raw)
In-Reply-To: <20100114002708.4154.qmail@science.horizon.com>
On 01/13/2010 04:27 PM, George Spelvin wrote:
>> There are a number of things that can be done better... for one thing,
>> "+m" (sem->count) and "a" (sem) is just bloody wrong. The right thing
>> would be "a" (&sem->count) for proper robustness.
>
> Actually, no. The "+m" (sem->count) is telling GCC that sem->count is
> updated; "a" (&sem->count) does *not* tell it to invalidate cached
> copies of sem->count that it may have lying around.
>
> However, we can't just use "+m" (sem->count) because GCC has a poor
> grasp on the concept of atomic operations; as far as it is concerned,
> it is exactly equivalent to copying the value into a stack slot, do the
> operation there, and copy it back.
You completely missed the point.
The reason we need both "+m" (sem->count) and "a" (sem) is because we
need the address of the semaphore in %eax in case we hit the slow path.
They're actually orthogonal requirements, and we could implement them as:
LOCK_PREFIX xadd %1,%0
jz 1f
call call_rwsem_wake
1:
: "+m" (sem->count), "=d" (tmp)
: "a" (sem)
... just fine, and arguably that would be more correct in the sense that
if sem->count isn't the first member of *sem, then we still pass the
address of sem in %eax to call_rwsem_wake. In practice, with the
zero-offset sem->count, gcc will typically generate (%eax) as the
argument since it has it available anyway, but it wouldn't have to.
The code as it currently is:
LOCK_PREFIX xadd %1,(%2)
jz 1f
call call_rwsem_wake
1:
: "+m" (sem->count), "=d" (tmp)
: "a" (sem)
... implicitly assumes the offset of "count" is zero but doesn't enforce
it in any way. My comment was that since the assumption is clearly that
arguments 0 and 2 point to the same memory object, it should be
(&sem->count) in the second case, but that's actually not really the
"proper" fix because call_rwsem_wake presumably expects the value of sem.
-hpa
next prev parent reply other threads:[~2010-01-14 0:39 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-13 19:58 x86-32: clean up rwsem inline asm statements George Spelvin
2010-01-13 21:04 ` H. Peter Anvin
2010-01-14 0:27 ` George Spelvin
2010-01-14 0:39 ` H. Peter Anvin [this message]
2010-01-14 1:05 ` Linus Torvalds
-- strict thread matches above, loose matches on Subject: below --
2010-01-13 0:21 Linus Torvalds
2010-01-13 0:45 ` Andreas Schwab
2010-01-13 1:26 ` Linus Torvalds
2010-01-13 0:48 ` H. Peter Anvin
2010-01-13 0:59 ` Linus Torvalds
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=4B4E67C8.501@zytor.com \
--to=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@horizon.com \
--cc=schwab@linux-m68k.org \
--cc=torvalds@linux-foundation.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