thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From 29bcf843d796ffc2a0906dea947e4cdfe9f7ec60 Mon Sep 17 00:00:00 2001
ed5979
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
Date: Thu, 9 Mar 2023 08:24:36 -0500
ed5979
Subject: [PATCH 11/12] async: update documentation of the memory barriers
ed5979
ed5979
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
RH-MergeRequest: 158: qatomic: add smp_mb__before/after_rmw()
ed5979
RH-Bugzilla: 2175660
ed5979
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ed5979
RH-Acked-by: David Hildenbrand <david@redhat.com>
ed5979
RH-Acked-by: Eric Auger <eric.auger@redhat.com>
ed5979
RH-Commit: [8/9] 5ca20e4c8983e0bc1ecee66bead3472777abe4d1 (eesposit/qemu-kvm)
ed5979
ed5979
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2175660
ed5979
ed5979
commit 8dd48650b43dfde4ebea34191ac267e474bcc29e
ed5979
Author: Paolo Bonzini <pbonzini@redhat.com>
ed5979
Date:   Mon Mar 6 10:15:06 2023 +0100
ed5979
ed5979
    async: update documentation of the memory barriers
ed5979
ed5979
    Ever since commit 8c6b0356b539 ("util/async: make bh_aio_poll() O(1)",
ed5979
    2020-02-22), synchronization between qemu_bh_schedule() and aio_bh_poll()
ed5979
    is happening when the bottom half is enqueued in the bh_list; not
ed5979
    when the flags are set.  Update the documentation to match.
ed5979
ed5979
    Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
ed5979
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
ed5979
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
---
ed5979
 util/async.c | 33 +++++++++++++++++++--------------
ed5979
 1 file changed, 19 insertions(+), 14 deletions(-)
ed5979
ed5979
diff --git a/util/async.c b/util/async.c
ed5979
index 63434ddae4..37d3e6036d 100644
ed5979
--- a/util/async.c
ed5979
+++ b/util/async.c
ed5979
@@ -73,14 +73,21 @@ static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
ed5979
     unsigned old_flags;
ed5979
 
ed5979
     /*
ed5979
-     * The memory barrier implicit in qatomic_fetch_or makes sure that:
ed5979
-     * 1. idle & any writes needed by the callback are done before the
ed5979
-     *    locations are read in the aio_bh_poll.
ed5979
-     * 2. ctx is loaded before the callback has a chance to execute and bh
ed5979
-     *    could be freed.
ed5979
+     * Synchronizes with atomic_fetch_and() in aio_bh_dequeue(), ensuring that
ed5979
+     * insertion starts after BH_PENDING is set.
ed5979
      */
ed5979
     old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
ed5979
+
ed5979
     if (!(old_flags & BH_PENDING)) {
ed5979
+        /*
ed5979
+         * At this point the bottom half becomes visible to aio_bh_poll().
ed5979
+         * This insertion thus synchronizes with QSLIST_MOVE_ATOMIC in
ed5979
+         * aio_bh_poll(), ensuring that:
ed5979
+         * 1. any writes needed by the callback are visible from the callback
ed5979
+         *    after aio_bh_dequeue() returns bh.
ed5979
+         * 2. ctx is loaded before the callback has a chance to execute and bh
ed5979
+         *    could be freed.
ed5979
+         */
ed5979
         QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
ed5979
     }
ed5979
 
ed5979
@@ -106,11 +113,8 @@ static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
ed5979
     QSLIST_REMOVE_HEAD(head, next);
ed5979
 
ed5979
     /*
ed5979
-     * The qatomic_and is paired with aio_bh_enqueue().  The implicit memory
ed5979
-     * barrier ensures that the callback sees all writes done by the scheduling
ed5979
-     * thread.  It also ensures that the scheduling thread sees the cleared
ed5979
-     * flag before bh->cb has run, and thus will call aio_notify again if
ed5979
-     * necessary.
ed5979
+     * Synchronizes with qatomic_fetch_or() in aio_bh_enqueue(), ensuring that
ed5979
+     * the removal finishes before BH_PENDING is reset.
ed5979
      */
ed5979
     *flags = qatomic_fetch_and(&bh->flags,
ed5979
                               ~(BH_PENDING | BH_SCHEDULED | BH_IDLE));
ed5979
@@ -157,6 +161,7 @@ int aio_bh_poll(AioContext *ctx)
ed5979
     BHListSlice *s;
ed5979
     int ret = 0;
ed5979
 
ed5979
+    /* Synchronizes with QSLIST_INSERT_HEAD_ATOMIC in aio_bh_enqueue().  */
ed5979
     QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list);
ed5979
     QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
ed5979
 
ed5979
@@ -446,15 +451,15 @@ LuringState *aio_get_linux_io_uring(AioContext *ctx)
ed5979
 void aio_notify(AioContext *ctx)
ed5979
 {
ed5979
     /*
ed5979
-     * Write e.g. bh->flags before writing ctx->notified.  Pairs with smp_mb in
ed5979
-     * aio_notify_accept.
ed5979
+     * Write e.g. ctx->bh_list before writing ctx->notified.  Pairs with
ed5979
+     * smp_mb() in aio_notify_accept().
ed5979
      */
ed5979
     smp_wmb();
ed5979
     qatomic_set(&ctx->notified, true);
ed5979
 
ed5979
     /*
ed5979
-     * Write ctx->notified before reading ctx->notify_me.  Pairs
ed5979
-     * with smp_mb in aio_ctx_prepare or aio_poll.
ed5979
+     * Write ctx->notified (and also ctx->bh_list) before reading ctx->notify_me.
ed5979
+     * Pairs with smp_mb() in aio_ctx_prepare or aio_poll.
ed5979
      */
ed5979
     smp_mb();
ed5979
     if (qatomic_read(&ctx->notify_me)) {
ed5979
-- 
ed5979
2.39.1
ed5979