From: Paolo Bonzini <pbonzini@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>,
Daniel Berrange <berrange@redhat.com>,
Eduardo Habkost <ehabkost@redhat.com>,
qemu-block@nongnu.org, qemu-devel@nongnu.org,
Max Reitz <mreitz@redhat.com>
Subject: Re: [RFC] Move to C11 Atomics
Date: Mon, 21 Sep 2020 16:16:07 +0200 [thread overview]
Message-ID: <439bbea5-60d7-aa9c-e693-3a3b1143154c@redhat.com> (raw)
In-Reply-To: <20200921134423.GA156064@stefanha-x1.localdomain>
[-- Attachment #1.1: Type: text/plain, Size: 2470 bytes --]
On 21/09/20 15:44, Stefan Hajnoczi wrote:
> On Mon, Sep 21, 2020 at 01:15:30PM +0200, Paolo Bonzini wrote:
>> On 21/09/20 12:41, Stefan Hajnoczi wrote:
>>> The upshot is that all atomic variables in QEMU need to use C11 Atomic
>>> atomic_* types. This is a big change!
>>
>> The main issue with this is that C11 atomic types do too much magic,
>> including defaulting to seq-cst operations for loads and stores. As
>> documented in docs/devel/atomics.rst, seq-cst loads and stores are
>> almost never what you want (they're only a little below volatile :)):
>
> I can't find where atomics.rst says seq-cst operations are almost never
> what you want?
Note that I'm talking only about loads and stores. They are so much
never what you want that we don't even provide a wrapper for them in
qemu/atomic.h.
``qemu/atomic.h`` also provides loads and stores that cannot be
reordered with each other::
typeof(*ptr) atomic_mb_read(ptr)
void atomic_mb_set(ptr, val)
However these do not provide sequential consistency and, in
particular, they do not participate in the total ordering enforced by
sequentially-consistent operations. For this reason they are
deprecated. They should instead be replaced with any of the following
(ordered from easiest to hardest):
- accesses inside a mutex or spinlock
- lightweight synchronization primitives such as ``QemuEvent``
- RCU operations (``atomic_rcu_read``, ``atomic_rcu_set``) when
publishing or accessing a new version of a data structure
- other atomic accesses: ``atomic_read`` and ``atomic_load_acquire``
for loads, ``atomic_set`` and ``atomic_store_release`` for stores,
``smp_mb`` to forbid reordering subsequent loads before a store.
where seq-cst loads and stores are again completely missing. smp_mb is
there to replace them, as we did in commit 5710a3e0 ("async: use
explicit memory barriers").
>> we can use store-release/load-acquire
>
> They don't provide atomic arithmetic/logic operations. The only
> non-seq-cst ALU operation I see in atomic.h is
> atomic_fetch_inc_nonzero(), and it's a cmpxchg() loop (ugly compared to
> an atomic ALU instruction).
Seq-cst is fine for RMW operations (arithmetic/logic, xchg, cmpxchg),
also because they're usually less performance critical than loads and
stores. It's only loads and stores that give a false sense of
correctness as in the above commit.
Paolo
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2020-09-21 14:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-21 10:41 [RFC] Move to C11 Atomics Stefan Hajnoczi
2020-09-21 11:15 ` Paolo Bonzini
2020-09-21 13:44 ` Stefan Hajnoczi
2020-09-21 14:16 ` Paolo Bonzini [this message]
2020-09-21 16:49 ` Stefan Hajnoczi
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=439bbea5-60d7-aa9c-e693-3a3b1143154c@redhat.com \
--to=pbonzini@redhat.com \
--cc=berrange@redhat.com \
--cc=ehabkost@redhat.com \
--cc=fam@euphon.net \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@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;
as well as URLs for NNTP newsgroup(s).