All of lore.kernel.org
 help / color / mirror / Atom feed
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: Tue, 02 Sep 2008 17:00:14 +0200	[thread overview]
Message-ID: <48BD54FE.8010406@domain.hid> (raw)
In-Reply-To: <48BD5180.4080509@domain.hid>

Gilles Chanteperdrix wrote:
> Jan Kiszka wrote:
>> Gilles Chanteperdrix wrote:
>>> Jan Kiszka wrote:
>>>> Patch 2 of my fast lock series actually also contained an attempt to fix
>>>> a race I spotted in the code that atomically sets the claimed bit. I
>>>> forgot about this fact and even, worse, I only replace the original race
>>>> with another one.
>>>>
>>>> So here comes a new attempt to fix the issue that the lock owner and/or
>>>> the claimed bit can change between trylock and the cmpxchg under nklock.
>>>> Please have a look and cross-check the logic.
>>>>
>>>> The patch applies on top of vanilla SVN, so my series has to be rebased
>>>> and the fix has to be ported to native as well - where we just found it
>>>> in the field.
>>> Ok. Got it.
>>>
>>>> Jan
>>>>
>>>> ---
>>>>  ksrc/skins/posix/mutex.h |   11 ++++++-----
>>>>  1 file changed, 6 insertions(+), 5 deletions(-)
>>>>
>>>> Index: b/ksrc/skins/posix/mutex.h
>>>> ===================================================================
>>>> --- a/ksrc/skins/posix/mutex.h
>>>> +++ b/ksrc/skins/posix/mutex.h
>>>> @@ -134,17 +134,18 @@ static inline int pse51_mutex_timedlock_
>>>>  	/* Set bit 0, so that mutex_unlock will know that the mutex is claimed.
>>>>  	   Hold the nklock, for mutual exclusion with slow mutex_unlock. */
>>>>  	xnlock_get_irqsave(&nklock, s);
>>>> +	owner = xnarch_atomic_intptr_get(mutex->owner);
>>> Bad, this makes two atomic ops. So, I would rather propose:
>> Err, how many archs perform special dances for atomic_read?
> 
> powerpc, arm >= 6

None of both. All atomic_read for arm use plain ((v)->counter), powerpc
simply uses assembly to avoid volatile atomic_t types. Do you happen to
confuse atomic_read and set here?

> 
>>>>  	while(!test_claimed(owner)) {
>>> - while(!test_claimed(owner)) {
>>> + do {
>>>> -		old = xnarch_atomic_intptr_cmpxchg(mutex->owner,
>>>> -						   owner, set_claimed(owner, 1));
>>>> -		if (likely(old == owner))
>>>> -			break;
>>>> -		if (old == NULL) {
>>>> +		if (owner == NULL) {
>>>>  			/* Owner called fast mutex_unlock
>>>>  			   (on another cpu) */
>>>>  			xnlock_put_irqrestore(&nklock, s);
>>>>  			goto retry_lock;
>>>>  		}
>>>> +		old = xnarch_atomic_intptr_cmpxchg(mutex->owner,
>>>> +						   owner, set_claimed(owner, 1));
>>>> +		if (likely(old == owner))
>>>> +			break;
>>>>  		owner = old;
>>> -  	}
>>> +       } while (!test_claimed(owner))
>> Your version slows down the contended case by performing redundant
>> atomic cmpxchgs. My version puts minimal additional overhead in the
>> !test_claimed case, but introduces no further atomic ops and avoids to
>> much traffic on mutex->owner's cacheline when there are >1 waiters.
> 
> But your version slows down the common case where the owner will not
> have changed... I do not care for making the uncommon case slower as
> long as the common case is fast.

The tiny slowdown (a simple, typically cached read and test) is
negligible compared to the full slow path, even if there is only one
waiter. Atomic ops are heavier, sleeping is much heavier.

Jan

-- 
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux


  parent reply	other threads:[~2008-09-02 15:00 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 [this message]
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
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=48BD54FE.8010406@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.