[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 02/41] qcow2: avoid reentrant bdrv_read() in copy_se
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PATCH 02/41] qcow2: avoid reentrant bdrv_read() in copy_sectors() |
Date: |
Mon, 5 Dec 2011 15:20:39 +0100 |
From: Stefan Hajnoczi <address@hidden>
A BlockDriverState should not issue requests on itself through the
public block layer interface. Nested, or reentrant, requests are
problematic because they do I/O throttling and request tracking twice.
Features like block layer copy-on-read use request tracking to avoid
race conditions between concurrent requests. The reentrant request will
have to "wait" for its parent request to complete. But the parent is
waiting for the reentrant request to make progress so we have reached
deadlock.
The solution is for block drivers to avoid the public block layer
interfaces for reentrant requests. Instead they should call their own
internal functions if they wish to perform reentrant requests.
This is also a good opportunity to make copy_sectors() a true
coroutine_fn. That means calling bdrv_co_writev() instead of
bdrv_write(). Behavior is unchanged but we're being explicit that this
executes in coroutine context.
Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/qcow2-cluster.c | 27 +++++++++++++++++++--------
1 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 0e33707..07a2e93 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -289,12 +289,15 @@ void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t
sector_num,
}
}
-static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
- uint64_t cluster_offset, int n_start, int n_end)
+static int coroutine_fn copy_sectors(BlockDriverState *bs,
+ uint64_t start_sect,
+ uint64_t cluster_offset,
+ int n_start, int n_end)
{
BDRVQcowState *s = bs->opaque;
+ QEMUIOVector qiov;
+ struct iovec iov;
int n, ret;
- void *buf;
/*
* If this is the last cluster and it is only partially used, we must only
@@ -310,29 +313,37 @@ static int copy_sectors(BlockDriverState *bs, uint64_t
start_sect,
return 0;
}
- buf = qemu_blockalign(bs, n * BDRV_SECTOR_SIZE);
+ iov.iov_len = n * BDRV_SECTOR_SIZE;
+ iov.iov_base = qemu_blockalign(bs, iov.iov_len);
+
+ qemu_iovec_init_external(&qiov, &iov, 1);
BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
- ret = bdrv_read(bs, start_sect + n_start, buf, n);
+
+ /* Call .bdrv_co_readv() directly instead of using the public block-layer
+ * interface. This avoids double I/O throttling and request tracking,
+ * which can lead to deadlock when block layer copy-on-read is enabled.
+ */
+ ret = bs->drv->bdrv_co_readv(bs, start_sect + n_start, n, &qiov);
if (ret < 0) {
goto out;
}
if (s->crypt_method) {
qcow2_encrypt_sectors(s, start_sect + n_start,
- buf, buf, n, 1,
+ iov.iov_base, iov.iov_base, n, 1,
&s->aes_encrypt_key);
}
BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
- ret = bdrv_write(bs->file, (cluster_offset >> 9) + n_start, buf, n);
+ ret = bdrv_co_writev(bs->file, (cluster_offset >> 9) + n_start, n, &qiov);
if (ret < 0) {
goto out;
}
ret = 0;
out:
- qemu_vfree(buf);
+ qemu_vfree(iov.iov_base);
return ret;
}
--
1.7.6.4
- [Qemu-devel] [PULL 00/41] Block patches, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 01/41] qcow2: Unlock during COW, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 08/41] block: add I/O throttling algorithm, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 07/41] CoQueue: introduce qemu_co_queue_wait_insert_head, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 05/41] block: Use bdrv functions to replace file operation in cow.c, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 04/41] xen_disk: remove dead code, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 06/41] block: add the blockio limits command line support, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 02/41] qcow2: avoid reentrant bdrv_read() in copy_sectors(),
Kevin Wolf <=
- [Qemu-devel] [PATCH 03/41] qed: adjust the way to get nb_sectors, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 13/41] qcow2: Update snapshot table information at once, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 15/41] qcow2: Rework qcow2_snapshot_create error handling, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 10/41] block: Add coroutine_fn marker to coroutine functions, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 09/41] hmp/qmp: add block_set_io_throttle, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 12/41] qcow2: Return real error code in qcow2_write_snapshots, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 11/41] qcow2: Return real error code in qcow2_read_snapshots, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 14/41] qcow2: Cleanups and memleak fix in qcow2_snapshot_create, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 16/41] qcow2: Return real error in qcow2_snapshot_goto, Kevin Wolf, 2011/12/05
- [Qemu-devel] [PATCH 19/41] qcow2: Fix error path in qcow2_snapshot_load_tmp, Kevin Wolf, 2011/12/05