All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Ying Fang <fangying1@huawei.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH 4/4] async: use explicit memory barriers and relaxed accesses
Date: Tue, 7 Apr 2020 10:09:54 +0100	[thread overview]
Message-ID: <20200407090954.GC247777@stefanha-x1.localdomain> (raw)
In-Reply-To: <20200406191320.13371-5-pbonzini@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2063 bytes --]

On Mon, Apr 06, 2020 at 03:13:20PM -0400, Paolo Bonzini wrote:
> When using C11 atomics, non-seqcst reads and writes do not participate
> in the total order of seqcst operations.  In util/async.c and util/aio-posix.c,
> in particular, the pattern that we use
> 
>           write ctx->notify_me                 write bh->scheduled
>           read bh->scheduled                   read ctx->notify_me
>           if !bh->scheduled, sleep             if ctx->notify_me, notify
> 
> needs to use seqcst operations for both the write and the read.  In
> general this is something that we do not want, because there can be
> many sources that are polled in addition to bottom halves.  The
> alternative is to place a seqcst memory barrier between the write
> and the read.  This also comes with a disadvantage, in that the
> memory barrier is implicit on strongly-ordered architectures and
> it wastes a few dozen clock cycles.
> 
> Fortunately, ctx->notify_me is never written concurrently by two
> threads, so we can instead relax the writes to ctx->notify_me.
> [This part of the commit message is still to be written more
> in detail and is what I am not sure about.]
> 
> Analyzed-by: Ying Fang <fangying1@huawei.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  util/aio-posix.c |  9 ++++++++-
>  util/aio-win32.c |  8 +++++++-
>  util/async.c     | 12 ++++++++++--
>  3 files changed, 25 insertions(+), 4 deletions(-)
> 
> diff --git a/util/aio-posix.c b/util/aio-posix.c
> index cd6cf0a4a9..37afec726f 100644
> --- a/util/aio-posix.c
> +++ b/util/aio-posix.c
> @@ -569,7 +569,13 @@ bool aio_poll(AioContext *ctx, bool blocking)
>       * so disable the optimization now.
>       */
>      if (blocking) {
> -        atomic_add(&ctx->notify_me, 2);
> +        atomic_set(&ctx->notify_me, atomic_read(&ctx->notify_me) + 2);

Non-atomic "atomic" code looks suspicious and warrants a comment
mentioning that this is only executed from one thread.  This applies to
the other instances in this patch too.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2020-04-07  9:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-06 19:13 [RFC PATCH 0/4] async: fix hangs on weakly-ordered architectures Paolo Bonzini
2020-04-06 19:13 ` [PATCH 1/4] atomics: convert to reStructuredText Paolo Bonzini
2020-04-06 19:58   ` Eric Blake
2020-04-07 10:29     ` Paolo Bonzini
2020-04-06 19:13 ` [PATCH 2/4] atomics: update documentation for C11 Paolo Bonzini
2020-04-06 20:03   ` Eric Blake
2020-04-07  9:06   ` Stefan Hajnoczi
2020-04-07  9:13     ` Paolo Bonzini
2020-04-06 19:13 ` [PATCH 3/4] rcu: do not mention atomic_mb_read/set in documentation Paolo Bonzini
2020-04-06 19:13 ` [PATCH 4/4] async: use explicit memory barriers and relaxed accesses Paolo Bonzini
2020-04-07  9:09   ` Stefan Hajnoczi [this message]
2020-04-07  9:18     ` Paolo Bonzini
2020-04-07  9:10 ` [RFC PATCH 0/4] async: fix hangs on weakly-ordered architectures 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=20200407090954.GC247777@stefanha-x1.localdomain \
    --to=stefanha@redhat.com \
    --cc=fangying1@huawei.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.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.