block: Protect bs->children with graph_lock
authorKevin Wolf <kwolf@redhat.com>
Fri, 29 Sep 2023 14:51:56 +0000 (16:51 +0200)
committerKevin Wolf <kwolf@redhat.com>
Thu, 12 Oct 2023 14:31:33 +0000 (16:31 +0200)
Almost all functions that access the child links already take the graph
lock now. Add locking to the remaining users and finally annotate the
struct field itself as protected by the graph lock.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-ID: <20230929145157.45443-22-kwolf@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
block.c
block/replication.c
include/block/block_int-common.h
tests/unit/test-bdrv-drain.c

diff --git a/block.c b/block.c
index 2ac7406c779ba42465c6c84b761c5f6b015c28e2..f9cf05ddcfc0ecc5dbbd3e0d54ec596b9272442c 100644 (file)
--- a/block.c
+++ b/block.c
@@ -2973,6 +2973,8 @@ static void bdrv_child_free(BdrvChild *child)
 {
     assert(!child->bs);
     GLOBAL_STATE_CODE();
+    GRAPH_RDLOCK_GUARD_MAINLOOP();
+
     assert(!child->next.le_prev); /* not in children list */
 
     g_free(child->name);
index 3459f506690df179c758bc49a3da562a82fb9791..d522c7396f221f12dc51fd00c5b0a9728c832c40 100644 (file)
@@ -430,7 +430,8 @@ static void backup_job_completed(void *opaque, int ret)
     backup_job_cleanup(bs);
 }
 
-static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
+static bool GRAPH_RDLOCK
+check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
 {
     BdrvChild *child;
 
index 0e37acd976ceae78efe9937a35a58567bdc67143..b8d9d24f3973cdc6e629e26aa0ce68d1fecde572 100644 (file)
@@ -1042,7 +1042,7 @@ struct BdrvChild {
      */
     bool quiesced_parent;
 
-    QLIST_ENTRY(BdrvChild) next;
+    QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next;
     QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next_parent;
 };
 
@@ -1176,7 +1176,7 @@ struct BlockDriverState {
      * See also comment in include/block/block.h, to learn how backing and file
      * are connected with BdrvChildRole.
      */
-    QLIST_HEAD(, BdrvChild) children;
+    QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children;
     BdrvChild *backing;
     BdrvChild *file;
 
index d734829778b00b902bab260ad32efacfa82cf0e8..f67e9df01cdba1c8ccaec04c362d33ca82539721 100644 (file)
@@ -1034,9 +1034,13 @@ static void coroutine_fn test_co_delete_by_drain(void *opaque)
         blk_co_unref(blk);
     } else {
         BdrvChild *c, *next_c;
+        bdrv_graph_co_rdlock();
         QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
+            bdrv_graph_co_rdunlock();
             bdrv_co_unref_child(bs, c);
+            bdrv_graph_co_rdlock();
         }
+        bdrv_graph_co_rdunlock();
     }
 
     dbdd->done = true;