block/io: serialising request clean up and locking fix
v2 cover block/io: serialising request clean up and locking fix
Peter Lieven noticed that reqs->overlap_offset and reqs->overlap_bytes are written outside bs->reqs_lock. Patch 3 fixes it, while patches 1 and 2 are preparatory cleanups.
v1->v2: fix comment in patch 2, commit message in patch 3 [Kevin]
Paolo Bonzini (3):
block: eliminate BDRV_REQ_NO_SERIALISING
block/io: wait for serialising requests when a request becomes
serialising
block/io: take bs->reqs_lock in bdrv_mark_request_serialising
block/file-posix.c | 1 - block/io.c | 162 +++++++++++++++++++++++----------------------- include/block/block.h | 12 ---- include/block/block_int.h | 3 +- 4 files changed, 81 insertions(+), 97 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/3 block: eliminate BDRV_REQ_NO_SERIALISING
It is unused since commit 00e30f0 ("block/backup: use backup-top instead of write notifiers", 2019-10-01), drop it to simplify the code.
While at it, drop redundant assertions on flags.
--- block/io.c | 18 ++++-------------- include/block/block.h | 12 ------------ 2 files changed, 4 insertions(+), 26 deletions(-)
block/io.c +4 -14
@@ -1445,8 +1445,7 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child,* potential fallback support, if we ever implement any read flags * to pass through to drivers. For now, there aren't any * passthrough flags. */ -assert (!(flags & ~(BDRV_REQ_NO_SERIALISING |BDRV_REQ_COPY_ON_READ | -BDRV_REQ_PREFETCH ))); +assert (!(flags & ~(BDRV_REQ_COPY_ON_READ |BDRV_REQ_PREFETCH )));/* Handle Copy on Read and associated serialisation */ if (flags &BDRV_REQ_COPY_ON_READ ) {
@@ -1458,12 +1457,7 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child,bdrv_mark_request_serialising (req,bdrv_get_cluster_size (bs)); } -/* BDRV_REQ_SERIALISING is only for write operation */ -assert (!(flags &BDRV_REQ_SERIALISING )); - -if (!(flags &BDRV_REQ_NO_SERIALISING )) { -bdrv_wait_serialising_requests (req); - } +bdrv_wait_serialising_requests (req);if (flags &BDRV_REQ_COPY_ON_READ ) {int64_t pnum;
@@ -1711,7 +1705,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,bdrv_inc_in_flight (bs);/* Don't do copy-on-read if we read data before write operation */ -if (atomic_read (&bs->copy_on_read) && !(flags &BDRV_REQ_NO_SERIALISING )) { +if (atomic_read (&bs->copy_on_read)) { flags |=BDRV_REQ_COPY_ON_READ ; }
@@ -1852,8 +1846,6 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes,return -EPERM ; } -/* BDRV_REQ_NO_SERIALISING is only for read operation */ -assert (!(flags &BDRV_REQ_NO_SERIALISING ));assert (!(bs->open_flags &BDRV_O_INACTIVE ));assert ((bs->open_flags &BDRV_O_NO_IO ) ==0 );assert (!(flags & ~BDRV_REQ_MASK ));
@@ -3222,9 +3214,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(/* BDRV_REQ_SERIALISING is only for write operation */ assert (!(read_flags &BDRV_REQ_SERIALISING )); -if (!(read_flags &BDRV_REQ_NO_SERIALISING )) { -bdrv_wait_serialising_requests (&req); - } +bdrv_wait_serialising_requests (&req); ret = src->bs->drv->bdrv_co_copy_range_from(src->bs, src, src_offset,
include/block/block.h +0 -12
@@ -51,18 +51,6 @@ typedef enum {*/ BDRV_REQ_MAY_UNMAP =0x4 , -/* -* The BDRV_REQ_NO_SERIALISING flag is only valid for reads and means that -* we don't want wait_serialising_requests() during the read operation. -* -* This flag is used for backup copy-on-write operations, when we need to -* read old data before write (write notifier triggered). It is okay since -* we already waited for other serializing requests in the initiating write -* (see bdrv_aligned_pwritev), and it is necessary if the initiating write -* is already serializing (without the flag, the read would deadlock -* waiting for the serialising write to complete). -*/ -BDRV_REQ_NO_SERIALISING =0x8 ,BDRV_REQ_FUA =0x10 , BDRV_REQ_WRITE_COMPRESSED = 0x20,
Signature · 1 line
1.8.3.1
PATCH v2 2/3 block/io: wait for serialising requests when a request becomes serialising
In reply to Paolo BonziniMarking without waiting would not result in actual serialising behavior. Thus, make a call bdrv_mark_request_serialising sufficient for serialisation to happen.
--- block/file-posix.c | 1 - block/io.c | 40 +++++++++++++++++----------------------- include/block/block_int.h | 3 +-- 3 files changed, 18 insertions(+), 26 deletions(-)
block/file-posix.c +0 -1
@@ -2753,7 +2753,6 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, req->overlap_bytes = req->bytes; bdrv_mark_request_serialising(req, bs->bl.request_alignment); - bdrv_wait_serialising_requests(req); }#endif
block/io.c +17 -23
@@ -41,6 +41,7 @@#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)static void bdrv_parent_cb_resize (BlockDriverState *bs); +static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self); static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,int64_t offset,int bytes,BdrvRequestFlags flags);
@@ -715,7 +716,7 @@ static void tracked_request_begin(BdrvTrackedRequest *req,qemu_co_mutex_unlock (&bs->reqs_lock); } -void bdrv_mark_request_serialising (BdrvTrackedRequest *req,uint64_t align) +bool bdrv_mark_request_serialising (BdrvTrackedRequest *req,uint64_t align) {int64_t overlap_offset = req->offset & ~(align -1 );uint64_t overlap_bytes =ROUND_UP (req->offset + req->bytes, align)
@@ -728,18 +729,7 @@ void bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) req->overlap_offset =MIN (req->overlap_offset, overlap_offset); req->overlap_bytes =MAX (req->overlap_bytes, overlap_bytes); -} - -static bool is_request_serialising_and_aligned (BdrvTrackedRequest *req) -{ -/* -* If the request is serialising, overlap_offset and overlap_bytes are set, -* so we can check if the request is aligned. Otherwise, don't care and -* return false. -*/ - -return req->serialising && (req->offset == req->overlap_offset) && - (req->bytes == req->overlap_bytes); +return bdrv_wait_serialising_requests (req); }/**
@@ -823,7 +813,7 @@ void bdrv_dec_in_flight(BlockDriverState *bs)bdrv_wakeup (bs); } -bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self) +static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self) {BlockDriverState *bs = self->bs;BdrvTrackedRequest *req;
@@ -1455,10 +1445,10 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child,* it ensures that the CoR read and write operations are atomic and * guest writes cannot interleave between them. */ bdrv_mark_request_serialising (req,bdrv_get_cluster_size (bs)); + }else { +bdrv_wait_serialising_requests (req); } -bdrv_wait_serialising_requests (req); -if (flags &BDRV_REQ_COPY_ON_READ ) {int64_t pnum;
@@ -1851,13 +1841,19 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes,assert (!(flags & ~BDRV_REQ_MASK ));if (flags &BDRV_REQ_SERIALISING ) { -bdrv_mark_request_serialising (req,bdrv_get_cluster_size (bs)); + waited =bdrv_mark_request_serialising (req,bdrv_get_cluster_size (bs)); +/* +* For a misaligned request we should have already waited earlier, +* because we come after bdrv_padding_rmw_read which must be called +* with the request already marked as serialising. +*/ +assert (!waited || + (req->offset == req->overlap_offset && + req->bytes == req->overlap_bytes)); + }else { +bdrv_wait_serialising_requests (req); } - waited =bdrv_wait_serialising_requests (req); - -assert (!waited || !req->serialising || -is_request_serialising_and_aligned (req));assert (req->overlap_offset <= offset);assert (offset + bytes <= req->overlap_offset + req->overlap_bytes);assert (end_sector <= bs->total_sectors || child->perm &BLK_PERM_RESIZE );
@@ -2019,7 +2015,6 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child, padding =bdrv_init_padding (bs, offset, bytes, &pad);if (padding) {bdrv_mark_request_serialising (req, align); -bdrv_wait_serialising_requests (req);bdrv_padding_rmw_read (child, req, &pad, true);
@@ -2122,7 +2117,6 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,if (bdrv_pad_request (bs, &qiov, &qiov_offset, &offset, &bytes, &pad)) {bdrv_mark_request_serialising (&req, align); -bdrv_wait_serialising_requests (&req);bdrv_padding_rmw_read (child, &req, &pad, false); }
include/block/block_int.h +1 -2
@@ -999,8 +999,7 @@ extern unsigned int bdrv_drain_all_count;void bdrv_apply_subtree_drain (BdrvChild *child,BlockDriverState *new_parent);void bdrv_unapply_subtree_drain (BdrvChild *child,BlockDriverState *old_parent); -bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self); -void bdrv_mark_request_serialising (BdrvTrackedRequest *req,uint64_t align); +bool coroutine_fn bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align); BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs);int get_tmp_filename (char *filename,int size);
Signature · 1 line
1.8.3.1
PATCH v2 3/3 block/io: take bs->reqs_lock in bdrv_mark_request_serialising
In reply to Paolo Bonzinibdrv_mark_request_serialising is writing the overlap_offset and overlap_bytes fields of BdrvTrackedRequest. Take bs->reqs_lock for the whole duration of it, and not just when waiting for serialising requests, so that tracked_request_overlaps does not look at a half-updated request.
The new code does not unlock/relock around retries. This is unnecessary because a retry is always preceded by a CoQueue wait, which already releases and reacquires bs->reqs_lock.
--- block/io.c | 112 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 49 deletions(-)
block/io.c +63 -49
@@ -41,7 +41,6 @@#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)static void bdrv_parent_cb_resize (BlockDriverState *bs); -static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self); static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,int64_t offset,int bytes,BdrvRequestFlags flags);
@@ -716,12 +715,69 @@ static void tracked_request_begin(BdrvTrackedRequest *req,qemu_co_mutex_unlock (&bs->reqs_lock); } +static bool tracked_request_overlaps (BdrvTrackedRequest *req, +int64_t offset,uint64_t bytes) +{ +/* aaaa bbbb */ +if (offset >= req->overlap_offset + req->overlap_bytes) { +return false; + } +/* bbbb aaaa */ +if (req->overlap_offset >= offset + bytes) { +return false; + } +return true; +} + +static bool coroutine_fn +bdrv_wait_serialising_requests_locked (BlockDriverState *bs, +BdrvTrackedRequest *self) +{ +BdrvTrackedRequest *req; +bool retry; +bool waited = false; + +do { + retry = false; + QLIST_FOREACH(req, &bs->tracked_requests, list) { +if (req == self || (!req->serialising && !self->serialising)) { +continue ; + } +if (tracked_request_overlaps (req, self->overlap_offset, + self->overlap_bytes)) + { +/* Hitting this means there was a reentrant request, for +* example, a block driver issuing nested requests. This must +* never happen since it means deadlock. +*/ +assert (qemu_coroutine_self () != req->co); + +/* If the request is already (indirectly) waiting for us, or +* will wait for us as soon as it wakes up, then just go on +* (instead of producing a deadlock in the former case). */ +if (!req->waiting_for) { + self->waiting_for = req; +qemu_co_queue_wait (&req->wait_queue, &bs->reqs_lock); + self->waiting_for =NULL ; + retry = true; + waited = true; +break ; + } + } + } + }while (retry); +return waited; +} +bool bdrv_mark_request_serialising (BdrvTrackedRequest *req,uint64_t align) { +BlockDriverState *bs = req->bs;int64_t overlap_offset = req->offset & ~(align -1 );uint64_t overlap_bytes =ROUND_UP (req->offset + req->bytes, align) - overlap_offset; +bool waited; +qemu_co_mutex_lock (&bs->reqs_lock);if (!req->serialising) {atomic_inc (&req->bs->serialising_in_flight); req->serialising = true;
@@ -729,7 +785,9 @@ bool bdrv_mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) req->overlap_offset =MIN (req->overlap_offset, overlap_offset); req->overlap_bytes =MAX (req->overlap_bytes, overlap_bytes); -return bdrv_wait_serialising_requests (req); + waited =bdrv_wait_serialising_requests_locked (bs, req); +qemu_co_mutex_unlock (&bs->reqs_lock); +return waited; }/**
@@ -783,20 +841,6 @@ static int bdrv_get_cluster_size(BlockDriverState *bs) } } -static bool tracked_request_overlaps (BdrvTrackedRequest *req, -int64_t offset,uint64_t bytes) -{ -/* aaaa bbbb */ -if (offset >= req->overlap_offset + req->overlap_bytes) { -return false; - } -/* bbbb aaaa */ -if (req->overlap_offset >= offset + bytes) { -return false; - } -return true; -} -void bdrv_inc_in_flight (BlockDriverState *bs) {atomic_inc (&bs->in_flight);
@@ -816,45 +860,15 @@ void bdrv_dec_in_flight(BlockDriverState *bs) static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self) {BlockDriverState *bs = self->bs; -BdrvTrackedRequest *req; -bool retry;bool waited = false;if (!atomic_read (&bs->serialising_in_flight)) {return false; } -do { - retry = false; -qemu_co_mutex_lock (&bs->reqs_lock); - QLIST_FOREACH(req, &bs->tracked_requests, list) { -if (req == self || (!req->serialising && !self->serialising)) { -continue ; - } -if (tracked_request_overlaps (req, self->overlap_offset, - self->overlap_bytes)) - { -/* Hitting this means there was a reentrant request, for -* example, a block driver issuing nested requests. This must -* never happen since it means deadlock. -*/ -assert (qemu_coroutine_self () != req->co); - -/* If the request is already (indirectly) waiting for us, or -* will wait for us as soon as it wakes up, then just go on -* (instead of producing a deadlock in the former case). */ -if (!req->waiting_for) { - self->waiting_for = req; -qemu_co_queue_wait (&req->wait_queue, &bs->reqs_lock); - self->waiting_for =NULL ; - retry = true; - waited = true; -break ; - } - } - } -qemu_co_mutex_unlock (&bs->reqs_lock); - }while (retry); +qemu_co_mutex_lock (&bs->reqs_lock); + waited =bdrv_wait_serialising_requests_locked (bs, self); +qemu_co_mutex_unlock (&bs->reqs_lock);return waited; }
Signature · 1 line
1.8.3.1
Stefan Hajnoczi
AppliedQuoted 12 lines · Paolo Bonzini
Peter Lieven noticed that
reqs->overlap_offsetandreqs->overlap_bytesare written outsidebs->reqs_lock. Patch 3 fixes it, while patches 1 and 2 are preparatory cleanups.
v1->v2: fix comment in patch 2, commit message in patch 3 [Kevin]Paolo Bonzini (3):
block: eliminateBDRV_REQ_NO_SERIALISING
block/io: wait for serialising requests when a request becomes
serialising
block/io: takebs->reqs_lockinbdrv_mark_request_serialising
Quoted 5 lines
block/file-posix.c | 1 - block/io.c | 162 +++++++++++++++++++++++----------------------- include/block/block.h | 12 ---- include/block/block_int.h | 3 +- 4 files changed, 81 insertions(+), 97 deletions(-)
Is it possible to trigger the bug somehow?
Can you implement a test case?
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Paolo Bonzini
Peter Lieven noticed that
reqs->overlap_offsetandreqs->overlap_bytesare written outsidebs->reqs_lock. Patch 3 fixes it, while patches 1 and 2 are preparatory cleanups.
v1->v2: fix comment in patch 2, commit message in patch 3 [Kevin]Paolo Bonzini (3):
block: eliminateBDRV_REQ_NO_SERIALISING
block/io: wait for serialising requests when a request becomes
serialising
block/io: takebs->reqs_lockinbdrv_mark_request_serialising
Quoted 8 lines
Quoted 5 lines
block/file-posix.c | 1 - block/io.c | 162 +++++++++++++++++++++++----------------------- include/block/block.h | 12 ---- include/block/block_int.h | 3 +- 4 files changed, 81 insertions(+), 97 deletions(-)Is it possible to trigger the bug somehow?
Can you implement a test case?
No, it was found by inspection only, and it's actually protected by the AioContext lock for now.
Paolo