From: Jan Kiszka <jan.kiszka@domain.hid>
To: Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>
Cc: xenomai-core <xenomai@xenomai.org>
Subject: Re: [Xenomai-core] [PATCH] POSIX: Fix race when setting claimed bit
Date: Wed, 03 Sep 2008 09:17:27 +0200 [thread overview]
Message-ID: <48BE3A07.7070305@domain.hid> (raw)
In-Reply-To: <48BD8540.1050504@domain.hid>
[-- Attachment #1: Type: text/plain, Size: 3729 bytes --]
Gilles Chanteperdrix wrote:
> Jan Kiszka wrote:
>> Gilles Chanteperdrix wrote:
>>> Jan Kiszka wrote:
>>>> Gilles Chanteperdrix wrote:
>>>>> Jan Kiszka wrote:
>>>>>> OTOH, we can safe some text size which is precious as well. So I'm
>>>>>> convinced to go your way (with a modification):
>>>>> My approach sucks: we get a silly atomic_cmpxchg if the mutex is already
>>>>> claimed, which is as least as much a common case as a currently
>>>>> unclaimed mutex. Need to think a bit. But I think a good solution is to
>>>>> re-read only if the mutex has been seen as already claimed.
>>>> That makes no difference as then we will go through the cmpxchg path anyway.
>>>>
>>>> There is _no_ way around re-reading under nklock, all we can avoid is
>>>> atomic cmpxchg in the case of >1 waiters. But that would come at the
>>>> price of more complexity for all waiter.
>>>>
>>>> However, let's find some solution. I bet things will look different
>>>> again when we start fiddling with a generic lock + the additional bit to
>>>> replace XNWAKEN.
>>> What I meant is that if the claimed bit is already set, we can avoid the
>>> cmpxchg altogether, which was the intent of the original code. So I propose
>>> the following version:
>>>
>>> if(test_claimed(owner))
>>> owner = xnarch_atomic_intptr_get(mutex->owner);
>> This version lacks a test for owner == 0 here, otherwise you re-create
>> my old bug that bite me today.
>
> Ok, so jumping in the middle of the loop is the best thing to do then.
>
>>> while(!test_claimed(owner)) {
>>> old = xnarch_atomic_intptr_cmpxchg(mutex->owner,
>>> owner, set_claimed(owner, 1));
>>> if (likely(old == owner))
>>> break;
>>> if (old == NULL) {
>>> /* Owner called fast mutex_unlock
>>> (on another cpu) */
>>> xnlock_put_irqrestore(&nklock, s);
>>> goto retry_lock;
>>> }
>>> owner = old;
>>> }
>>>
>>> The compiler rearranges things correctly (at least on ARM), and avoids the
>>> redundant test.
>>>
>> My latest concern remains: Is all this additional complexity, are all
>> these conditional branches and the text size increase worth the effort?
>> How much cycles or micoseconds would be gain on the most suffering
>> architecture?
>
> Since the else and the while are folded together and the loop becomes
> post-tested, and with the help of conditional instructions, I do not
> think there is much more conditional branch (we need to to jump over
> the while loop anyway). However avoiding the cmpxchg is a real gain,
> since it is implemented as irq save/irq restore on an old arm.
>
> if(test_claimed(owner)) {
> old = xnarch_atomic_intptr_get(mutex->owner);
> goto test_old;
> }
> while(!test_claimed(owner)) {
> old = xnarch_atomic_intptr_cmpxchg(mutex->owner,
> owner, set_claimed(owner, 1));
> if (likely(old == owner))
> break;
> test_old:
> if (old == NULL) {
> /* Owner called fast mutex_unlock
> (on another cpu) */
> xnlock_put_irqrestore(&nklock, s);
> goto retry_lock;
> }
> owner = old;
> }
Should work now, but the beautifulness of the code suffers a bit...
What about making the expected compiler optimizations explicit?
if (test_claimed(owner)) {
old = xnarch_atomic_intptr_get(mutex->owner);
goto test_no_owner;
}
do {
old = xnarch_atomic_intptr_cmpxchg(mutex->owner, owner,
set_claimed(owner, 1));
if (likely(old == owner))
break;
test_no_owner:
if (old == NULL) {
/* Owner called fast mutex_unlock
(on another cpu) */
xnlock_put_irqrestore(&nklock, s);
goto retry_lock;
}
owner = old;
} while (!test_claimed(owner));
Jan
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 257 bytes --]
next prev parent reply other threads:[~2008-09-03 7:17 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-02 13:33 [Xenomai-core] [PATCH] POSIX: Fix race when setting claimed bit Jan Kiszka
2008-09-02 13:36 ` Gilles Chanteperdrix
2008-09-02 13:52 ` Jan Kiszka
2008-09-02 13:53 ` Gilles Chanteperdrix
2008-09-02 14:25 ` Jan Kiszka
2008-09-02 14:45 ` Gilles Chanteperdrix
2008-09-02 14:53 ` Gilles Chanteperdrix
2008-09-02 15:00 ` Jan Kiszka
2008-09-02 15:29 ` Jan Kiszka
2008-09-02 15:35 ` Gilles Chanteperdrix
2008-09-02 15:43 ` Jan Kiszka
2008-09-02 17:59 ` Gilles Chanteperdrix
2008-09-02 18:14 ` Jan Kiszka
2008-09-02 18:26 ` Gilles Chanteperdrix
2008-09-03 7:17 ` Jan Kiszka [this message]
2008-09-03 8:59 ` Gilles Chanteperdrix
2008-09-05 8:31 ` [Xenomai-core] [PATCH] POSIX: Fix race when setting claimed bit - v2 Jan Kiszka
2008-09-06 17:46 ` Gilles Chanteperdrix
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=48BE3A07.7070305@domain.hid \
--to=jan.kiszka@domain.hid \
--cc=gilles.chanteperdrix@xenomai.org \
--cc=xenomai@xenomai.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.