qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] graph-lock: make sure reader_count access is atomic


From: Wolfgang Bumiller
Subject: [PATCH 1/2] graph-lock: make sure reader_count access is atomic
Date: Mon, 1 Jul 2024 13:42:29 +0200

There's one case where `reader_count` is accessed non-atomically. This
was likely seen as being "guarded by a mutex" held in that block, but
other access to this does not actually depend on the mutex and already
uses atomics.

Additionally this replaces the pattern of atomic_set(atomic_read() +
1) with qatomic_inc() (and -1 with _dec)

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
 block/graph-lock.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/block/graph-lock.c b/block/graph-lock.c
index c81162b147..32fb29b841 100644
--- a/block/graph-lock.c
+++ b/block/graph-lock.c
@@ -176,8 +176,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void)
     bdrv_graph = qemu_get_current_aio_context()->bdrv_graph;
 
     for (;;) {
-        qatomic_set(&bdrv_graph->reader_count,
-                    bdrv_graph->reader_count + 1);
+        qatomic_inc(&bdrv_graph->reader_count);
         /* make sure writer sees reader_count before we check has_writer */
         smp_mb();
 
@@ -226,7 +225,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void)
             }
 
             /* slow path where reader sleeps */
-            bdrv_graph->reader_count--;
+            qatomic_dec(&bdrv_graph->reader_count);
             aio_wait_kick();
             qemu_co_queue_wait(&reader_queue, &aio_context_list_lock);
         }
@@ -238,8 +237,7 @@ void coroutine_fn bdrv_graph_co_rdunlock(void)
     BdrvGraphRWlock *bdrv_graph;
     bdrv_graph = qemu_get_current_aio_context()->bdrv_graph;
 
-    qatomic_store_release(&bdrv_graph->reader_count,
-                          bdrv_graph->reader_count - 1);
+    qatomic_dec(&bdrv_graph->reader_count);
     /* make sure writer sees reader_count before we check has_writer */
     smp_mb();
 
-- 
2.39.2





reply via email to

[Prev in Thread] Current Thread [Next in Thread]