[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 12/20] block-gen: assert that bdrv_co_pwrite is always called wit
From: |
Emanuele Giuseppe Esposito |
Subject: |
[PATCH 12/20] block-gen: assert that bdrv_co_pwrite is always called with graph rdlock taken |
Date: |
Wed, 16 Nov 2022 08:48:42 -0500 |
This function, in addition to be called by a generated_co_wrapper,
is also called elsewhere else.
The strategy is to always take the lock at the function called
when the coroutine is created, to avoid recursive locking.
By protecting brdv_co_pwrite, we also automatically protect
the following other generated_co_wrappers:
blk_co_pwrite
blk_co_pwritev
blk_co_pwritev_part
blk_co_pwrite_compressed
blk_co_pwrite_zeroes
Protecting bdrv_driver_pwritev_compressed() and bdrv_driver_pwritev_compressed()
implies that the following BlockDriver callbacks always called with graph rdlock
taken:
- bdrv_aio_pwritev
- bdrv_co_writev
- bdrv_co_pwritev
- bdrv_co_pwritev_part
- bdrv_co_pwritev_compressed
- bdrv_co_pwritev_compressed_part
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
block/block-backend.c | 1 +
block/block-copy.c | 8 ++++++--
block/io.c | 2 ++
include/block/block_int-common.h | 6 ++++++
include/block/block_int-io.h | 1 +
5 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/block/block-backend.c b/block/block-backend.c
index 0686cd6942..d48ec3a2ac 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -1363,6 +1363,7 @@ blk_co_do_pwritev_part(BlockBackend *blk, int64_t offset,
int64_t bytes,
IO_CODE();
blk_wait_while_drained(blk);
+ GRAPH_RDLOCK_GUARD();
/* Call blk_bs() only after waiting, the graph may have changed */
bs = blk_bs(blk);
diff --git a/block/block-copy.c b/block/block-copy.c
index f33ab1d0b6..dabf461112 100644
--- a/block/block-copy.c
+++ b/block/block-copy.c
@@ -464,6 +464,8 @@ static coroutine_fn int block_copy_task_run(AioTaskPool
*pool,
* a full-size buffer or disabled if the copy_range attempt fails. The output
* value of @method should be used for subsequent tasks.
* Returns 0 on success.
+ *
+ * Called with graph rdlock taken.
*/
static int coroutine_fn block_copy_do_copy(BlockCopyState *s,
int64_t offset, int64_t bytes,
@@ -554,8 +556,10 @@ static coroutine_fn int block_copy_task_entry(AioTask
*task)
BlockCopyMethod method = t->method;
int ret;
- ret = block_copy_do_copy(s, t->req.offset, t->req.bytes, &method,
- &error_is_read);
+ WITH_GRAPH_RDLOCK_GUARD() {
+ ret = block_copy_do_copy(s, t->req.offset, t->req.bytes, &method,
+ &error_is_read);
+ }
WITH_QEMU_LOCK_GUARD(&s->lock) {
if (s->method == t->method) {
diff --git a/block/io.c b/block/io.c
index ac12725fb2..9280fb9f38 100644
--- a/block/io.c
+++ b/block/io.c
@@ -1012,6 +1012,7 @@ static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState *bs,
unsigned int nb_sectors;
QEMUIOVector local_qiov;
int ret;
+ assert_bdrv_graph_readable();
bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
@@ -1090,6 +1091,7 @@ bdrv_driver_pwritev_compressed(BlockDriverState *bs,
int64_t offset,
BlockDriver *drv = bs->drv;
QEMUIOVector local_qiov;
int ret;
+ assert_bdrv_graph_readable();
bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h
index f285a6b8f7..d44f825d95 100644
--- a/include/block/block_int-common.h
+++ b/include/block/block_int-common.h
@@ -479,6 +479,7 @@ struct BlockDriver {
BlockAIOCB *(*bdrv_aio_preadv)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov,
BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
+ /* Called with graph rdlock taken. */
BlockAIOCB *(*bdrv_aio_pwritev)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov,
BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
@@ -515,6 +516,7 @@ struct BlockDriver {
QEMUIOVector *qiov, size_t qiov_offset,
BdrvRequestFlags flags);
+ /* Called with graph rdlock taken. */
int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
int flags);
@@ -532,10 +534,12 @@ struct BlockDriver {
* no larger than 'max_transfer'.
*
* The buffer in @qiov may point directly to guest memory.
+ * Called with graph rdlock taken.
*/
int coroutine_fn (*bdrv_co_pwritev)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov,
BdrvRequestFlags flags);
+ /* Called with graph rdlock taken. */
int coroutine_fn (*bdrv_co_pwritev_part)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset,
BdrvRequestFlags flags);
@@ -693,8 +697,10 @@ struct BlockDriver {
BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
Error **errp);
+ /* Called with graph rdlock held. */
int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov);
+ /* Called with graph rdlock held. */
int coroutine_fn (*bdrv_co_pwritev_compressed_part)(BlockDriverState *bs,
int64_t offset, int64_t bytes, QEMUIOVector *qiov,
size_t qiov_offset);
diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h
index 8bc061ebb8..ae88507d6a 100644
--- a/include/block/block_int-io.h
+++ b/include/block/block_int-io.h
@@ -69,6 +69,7 @@ static inline int coroutine_fn bdrv_co_pwrite(BdrvChild
*child,
{
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
IO_CODE();
+ assert_bdrv_graph_readable();
return bdrv_co_pwritev(child, offset, bytes, &qiov, flags);
}
--
2.31.1
- [PATCH 00/20] Protect the block layer with a rwlock: part 1, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 00/20] Protect the block layer with a rwlock: part 1, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 00/20] Protect the block layer with a rwlock: part 1, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 02/20] graph-lock: introduce BdrvGraphRWlock structure, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 19/20] block-gen: assert that bdrv_co_ioctl is always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 01/20] block: introduce a lock to protect graph operations, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 12/20] block-gen: assert that bdrv_co_pwrite is always called with graph rdlock taken,
Emanuele Giuseppe Esposito <=
- [PATCH 06/20] block: assert that graph read and writes are performed correctly, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 13/20] block-gen: assert that bdrv_co_pwrite_{zeros/sync} is always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 11/20] block-gen: assert that bdrv_co_{check/invalidate_cache} are always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 18/20] block-gen: assert that bdrv_co_common_block_status_above is always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 08/20] block-coroutine-wrapper.py: take the graph rdlock in bdrv_* functions, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 16/20] block-gen: assert that bdrv_co_{read/write}v_vmstate are always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 04/20] block.c: wrlock in bdrv_replace_child_noperm, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 20/20] block-gen: assert that nbd_co_do_establish_connection is always called with graph rdlock taken, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 09/20] block-backend: introduce new generated_co_wrapper_blk annotation, Emanuele Giuseppe Esposito, 2022/11/16
- [PATCH 07/20] graph-lock: implement WITH_GRAPH_RDLOCK_GUARD and GRAPH_RDLOCK_GUARD macros, Emanuele Giuseppe Esposito, 2022/11/16