Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
From: pbonzini@redhat.com (Paolo Bonzini)
Subject: [ltt-dev] [PATCH 2/2] introduce uatomic_and and uatomic_or
Date: Fri, 12 Nov 2010 15:39:17 +0100	[thread overview]
Message-ID: <4CDD5195.4010308@redhat.com> (raw)
In-Reply-To: <20101112122939.GA30826@Krystal>

On 11/12/2010 01:29 PM, Mathieu Desnoyers wrote:
> * Paolo Bonzini (pbonzini at redhat.com) wrote:
>> On 11/12/2010 01:19 PM, Mathieu Desnoyers wrote:
>>> * Paolo Bonzini (pbonzini at redhat.com) wrote:
>>>> Return nothing can be useful because it can be optimized on x86
>>>> as "lock orl (mem), reg/imm".  However, there are no other
>>>> return-nothing atomic ops in uatomic_*.h so I decided not to
>>>> provide this.
>>>>
>>>
>>> How about we start by implementing uatomic_and/uatomic_or that
>>> return void, and if we ever need
>>> uatomic_return_and/uatomic_return_or (see my other mail), we add
>>> them ?
>>
>> I am using uatomic_or's return value in my call_rcu-with-futex, but
>> I don't need atomic-or-and-exchange really, I can even change it to
>> a load followed by an atomic or (it introduces a race but it is
>> benign).
>
> If the race does not matter, why do we need the new atomic op in the
> first place ? ;)

Say the "correct" code would be

    atomic x = *p, *p |= FLAG1;   # uatomic_xchg_or
    if (x & ~FLAG2)
        futex_wake (...)

I may try to rewrite it like this:

    x = *p;                       # 1
    atomic *p |= FLAG1;           # 2, uatomic_or
    if (x & ~FLAG2)
        futex_wake (...)

This is racy because something can set or reset FLAG2 between 1 and 2. 
Setting it may cause spurious wakeups, which are okay.  If I know that 
nothing can reset FLAG2 between 1 and 2, correctness is preserved.

Instead if I remove atomicity, I get races on read-modify-write 
operations, and that is much more obviously incorrect.

But I really do not like being tricky (futexes are tricky enough on 
their own), and rather than spending a lot of time proving this correct 
I'd probably just use cmpxchg here.

Paolo



  reply	other threads:[~2010-11-12 14:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-12  8:57 [ltt-dev] [PATCH 0/2] Add uatomic_and and uatomic_or to uatomic library Paolo Bonzini
2010-11-12  8:57 ` [ltt-dev] [PATCH 1/2] use generic-size operations for common implementation of atomic ops Paolo Bonzini
2010-11-12 12:04   ` Mathieu Desnoyers
2010-11-12  8:57 ` [ltt-dev] [PATCH 2/2] introduce uatomic_and and uatomic_or Paolo Bonzini
2010-11-12 12:08   ` Mathieu Desnoyers
2010-11-12 12:13     ` Paolo Bonzini
2010-11-12 12:19       ` Mathieu Desnoyers
2010-11-12 12:24         ` Paolo Bonzini
2010-11-12 12:27           ` Paolo Bonzini
2010-11-12 12:30             ` Mathieu Desnoyers
2010-11-12 12:38               ` Mathieu Desnoyers
2010-11-12 14:39                 ` Paolo Bonzini
2010-11-12 12:29           ` Mathieu Desnoyers
2010-11-12 14:39             ` Paolo Bonzini [this message]
2010-11-12 12:15     ` Mathieu Desnoyers

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=4CDD5195.4010308@redhat.com \
    --to=pbonzini@redhat.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