All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com
Subject: [PULL 02/25] block: Mark bdrv_has_zero_init() and callers GRAPH_RDLOCK
Date: Tue,  7 Nov 2023 19:45:42 +0100	[thread overview]
Message-ID: <20231107184605.236540-3-kwolf@redhat.com> (raw)
In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com>

This adds GRAPH_RDLOCK annotations to declare that callers of
bdrv_has_zero_init() need to hold a reader lock for the graph because
it calls bdrv_filter_bs(), which accesses bs->file/backing.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-ID: <20231027155333.420094-3-kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/block/block-global-state.h |  2 +-
 include/block/block_int-common.h   |  2 +-
 block.c                            |  2 +-
 block/qcow2.c                      |  3 ++-
 block/raw-format.c                 |  2 +-
 block/vdi.c                        |  2 +-
 block/vhdx.c                       | 13 +++++++++----
 block/vmdk.c                       |  2 +-
 block/vpc.c                        |  2 +-
 blockdev.c                         |  2 ++
 qemu-img.c                         |  2 ++
 11 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/include/block/block-global-state.h b/include/block/block-global-state.h
index fca0a40dbd..3ae468ea15 100644
--- a/include/block/block-global-state.h
+++ b/include/block/block-global-state.h
@@ -189,7 +189,7 @@ void bdrv_drain_all(void);
 void bdrv_aio_cancel(BlockAIOCB *acb);
 
 int bdrv_has_zero_init_1(BlockDriverState *bs);
-int bdrv_has_zero_init(BlockDriverState *bs);
+int coroutine_mixed_fn GRAPH_RDLOCK bdrv_has_zero_init(BlockDriverState *bs);
 BlockDriverState *bdrv_find_node(const char *node_name);
 BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, Error **errp);
 XDbgBlockGraph * GRAPH_RDLOCK bdrv_get_xdbg_block_graph(Error **errp);
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h
index 8abdd2724b..c0db862de7 100644
--- a/include/block/block_int-common.h
+++ b/include/block/block_int-common.h
@@ -349,7 +349,7 @@ struct BlockDriver {
      * Returns 1 if newly created images are guaranteed to contain only
      * zeros, 0 otherwise.
      */
-    int (*bdrv_has_zero_init)(BlockDriverState *bs);
+    int GRAPH_RDLOCK_PTR (*bdrv_has_zero_init)(BlockDriverState *bs);
 
     /*
      * Remove fd handlers, timers, and other event loop callbacks so the event
diff --git a/block.c b/block.c
index a527aa1a4c..1171f47570 100644
--- a/block.c
+++ b/block.c
@@ -6587,7 +6587,7 @@ int bdrv_has_zero_init_1(BlockDriverState *bs)
     return 1;
 }
 
-int bdrv_has_zero_init(BlockDriverState *bs)
+int coroutine_mixed_fn bdrv_has_zero_init(BlockDriverState *bs)
 {
     BlockDriverState *filtered;
     GLOBAL_STATE_CODE();
diff --git a/block/qcow2.c b/block/qcow2.c
index aa01d9e7b5..a1443a31aa 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -5302,7 +5302,8 @@ static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs,
     return spec_info;
 }
 
-static int coroutine_mixed_fn qcow2_has_zero_init(BlockDriverState *bs)
+static int coroutine_mixed_fn GRAPH_RDLOCK
+qcow2_has_zero_init(BlockDriverState *bs)
 {
     BDRVQcow2State *s = bs->opaque;
     bool preallocated;
diff --git a/block/raw-format.c b/block/raw-format.c
index 3fb77b0097..8ca74c1cf3 100644
--- a/block/raw-format.c
+++ b/block/raw-format.c
@@ -452,7 +452,7 @@ raw_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
     return bdrv_co_ioctl(bs->file->bs, req, buf);
 }
 
-static int raw_has_zero_init(BlockDriverState *bs)
+static int GRAPH_RDLOCK raw_has_zero_init(BlockDriverState *bs)
 {
     return bdrv_has_zero_init(bs->file->bs);
 }
diff --git a/block/vdi.c b/block/vdi.c
index 7cfd12b50d..8e144ce523 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -990,7 +990,7 @@ static void vdi_close(BlockDriverState *bs)
     migrate_del_blocker(&s->migration_blocker);
 }
 
-static int vdi_has_zero_init(BlockDriverState *bs)
+static int GRAPH_RDLOCK vdi_has_zero_init(BlockDriverState *bs)
 {
     BDRVVdiState *s = bs->opaque;
 
diff --git a/block/vhdx.c b/block/vhdx.c
index a9d08742f9..e136ba1ae1 100644
--- a/block/vhdx.c
+++ b/block/vhdx.c
@@ -1695,7 +1695,7 @@ exit:
  *  Fixed images: default state of the BAT is fully populated, with
  *                file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT.
  */
-static int coroutine_fn
+static int coroutine_fn GRAPH_UNLOCKED
 vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
                 uint64_t image_size, VHDXImageType type,
                 bool use_zero_blocks, uint64_t file_offset,
@@ -1708,6 +1708,7 @@ vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
     uint64_t unused;
     int block_state;
     VHDXSectorInfo sinfo;
+    bool has_zero_init;
 
     assert(s->bat == NULL);
 
@@ -1737,9 +1738,13 @@ vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
         goto exit;
     }
 
+    bdrv_graph_co_rdlock();
+    has_zero_init = bdrv_has_zero_init(blk_bs(blk));
+    bdrv_graph_co_rdunlock();
+
     if (type == VHDX_TYPE_FIXED ||
                 use_zero_blocks ||
-                bdrv_has_zero_init(blk_bs(blk)) == 0) {
+                has_zero_init == 0) {
         /* for a fixed file, the default BAT entry is not zero */
         s->bat = g_try_malloc0(length);
         if (length && s->bat == NULL) {
@@ -1782,7 +1787,7 @@ exit:
  * to create the BAT itself, we will also cause the BAT to be
  * created.
  */
-static int coroutine_fn
+static int coroutine_fn GRAPH_UNLOCKED
 vhdx_create_new_region_table(BlockBackend *blk, uint64_t image_size,
                              uint32_t block_size, uint32_t sector_size,
                              uint32_t log_size, bool use_zero_blocks,
@@ -2173,7 +2178,7 @@ static int coroutine_fn vhdx_co_check(BlockDriverState *bs,
     return 0;
 }
 
-static int vhdx_has_zero_init(BlockDriverState *bs)
+static int GRAPH_RDLOCK vhdx_has_zero_init(BlockDriverState *bs)
 {
     BDRVVHDXState *s = bs->opaque;
     int state;
diff --git a/block/vmdk.c b/block/vmdk.c
index 85864b8045..91ed7a8d93 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -2894,7 +2894,7 @@ vmdk_co_get_allocated_file_size(BlockDriverState *bs)
     return ret;
 }
 
-static int vmdk_has_zero_init(BlockDriverState *bs)
+static int GRAPH_RDLOCK vmdk_has_zero_init(BlockDriverState *bs)
 {
     int i;
     BDRVVmdkState *s = bs->opaque;
diff --git a/block/vpc.c b/block/vpc.c
index aa1a48ae0e..483775103c 100644
--- a/block/vpc.c
+++ b/block/vpc.c
@@ -1170,7 +1170,7 @@ fail:
 }
 
 
-static int vpc_has_zero_init(BlockDriverState *bs)
+static int GRAPH_RDLOCK vpc_has_zero_init(BlockDriverState *bs)
 {
     BDRVVPCState *s = bs->opaque;
 
diff --git a/blockdev.c b/blockdev.c
index e9b7e38dc4..148df99e00 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -3156,9 +3156,11 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp)
         return;
     }
 
+    bdrv_graph_rdlock_main_loop();
     zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
                    (arg->mode == NEW_IMAGE_MODE_EXISTING ||
                     !bdrv_has_zero_init(target_bs)));
+    bdrv_graph_rdunlock_main_loop();
 
 
     /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
diff --git a/qemu-img.c b/qemu-img.c
index 369c2e8ddf..c061fd0634 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2099,7 +2099,9 @@ static int convert_do_copy(ImgConvertState *s)
     /* Check whether we have zero initialisation or can get it efficiently */
     if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
         !s->target_has_backing) {
+        bdrv_graph_rdlock_main_loop();
         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
+        bdrv_graph_rdunlock_main_loop();
     }
 
     /* Allocate buffer for copied data. For compressed images, only one cluster
-- 
2.41.0



  parent reply	other threads:[~2023-11-07 18:49 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07 18:45 [PULL 00/25] Block layer patches Kevin Wolf
2023-11-07 18:45 ` [PULL 01/25] block: Mark bdrv_probe_blocksizes() and callers GRAPH_RDLOCK Kevin Wolf
2023-11-07 18:45 ` Kevin Wolf [this message]
2023-11-07 18:45 ` [PULL 03/25] block: Mark bdrv_filter_bs() " Kevin Wolf
2023-11-07 18:45 ` [PULL 04/25] block: Mark bdrv_root_attach_child() GRAPH_WRLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 05/25] block: Mark block_job_add_bdrv() GRAPH_WRLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 06/25] block: Mark bdrv_filter_or_cow_bs() and callers GRAPH_RDLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 07/25] block: Mark bdrv_skip_implicit_filters() " Kevin Wolf
2023-11-07 18:45 ` [PULL 08/25] block: Mark bdrv_skip_filters() " Kevin Wolf
2023-11-07 18:45 ` [PULL 09/25] block: Mark bdrv_(un)freeze_backing_chain() " Kevin Wolf
2023-11-07 18:45 ` [PULL 10/25] block: Mark bdrv_chain_contains() " Kevin Wolf
2023-11-07 18:45 ` [PULL 11/25] block: Mark bdrv_filter_child() " Kevin Wolf
2023-11-07 18:45 ` [PULL 12/25] block: Mark bdrv_cow_child() " Kevin Wolf
2023-11-07 18:45 ` [PULL 13/25] block: Mark bdrv_set_backing_hd_drained() GRAPH_WRLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 14/25] block: Inline bdrv_set_backing_noperm() Kevin Wolf
2023-11-07 18:45 ` [PULL 15/25] block: Mark bdrv_replace_node_common() GRAPH_WRLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 16/25] block: Mark bdrv_replace_node() GRAPH_WRLOCK Kevin Wolf
2023-11-07 18:45 ` [PULL 17/25] block: Protect bs->backing with graph_lock Kevin Wolf
2023-11-07 18:45 ` [PULL 18/25] blkverify: Add locking for request_fn Kevin Wolf
2023-11-07 18:45 ` [PULL 19/25] block: Introduce bdrv_co_change_backing_file() Kevin Wolf
2023-11-07 18:46 ` [PULL 20/25] block: Add missing GRAPH_RDLOCK annotations Kevin Wolf
2023-11-07 18:46 ` [PULL 21/25] qcow2: Take locks for accessing bs->file Kevin Wolf
2023-11-07 18:46 ` [PULL 22/25] vhdx: " Kevin Wolf
2023-11-07 18:46 ` [PULL 23/25] block: Take graph lock for most of .bdrv_open Kevin Wolf
2023-11-07 18:46 ` [PULL 24/25] block: Protect bs->file with graph_lock Kevin Wolf
2023-11-07 18:46 ` [PULL 25/25] hw/ide/ahci: trigger either error IRQ or regular IRQ, not both Kevin Wolf
2023-11-08  1:44 ` [PULL 00/25] Block layer patches 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=20231107184605.236540-3-kwolf@redhat.com \
    --to=kwolf@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.