ide: Fix deadlock between TRIM and drain
cover ide: Fix deadlock between TRIM and drain
Patches 2 and 4 are the core of the fix, see their commit message for details.
Kevin Wolf (7):
blkdebug: Add 'delay-ns' option
block: Add blk_co_start/end_request() and BDRV_REQ_NO_QUEUE
block: Add flags parameter to blk_*_pdiscard()
ide: Minimal fix for deadlock between TRIM and drain
ide: Clean up ide_trim_co_entry() to be idiomatic coroutine code
ide-test: Factor out wait_dma_completion()
ide-test: Test reset during TRIM
qapi/block-core.json | 4 + include/block/block-common.h | 11 ++- include/system/block-backend-io.h | 6 +- block/blkdebug.c | 15 +++- block/block-backend.c | 47 +++++++--- block/export/virtio-blk-handler.c | 2 +- block/mirror.c | 4 +- hw/ide/core.c | 110 +++++++++++------------- nbd/server.c | 2 +- qemu-io-cmds.c | 2 +- tests/qtest/ide-test.c | 137 ++++++++++++++++++++++++------ tests/unit/test-block-iothread.c | 4 +- 12 files changed, 236 insertions(+), 108 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/7 blkdebug: Add 'delay-ns' option
Sometimes reproducing a problem for debugging involves slow I/O, so let's add something to blkdebug to make I/O slow when we need it. This can be used either together with an error so that the request fails after the delay, or with errno=0, which allows the request to succeed after the delay.
--- qapi/block-core.json | 4 ++++ block/blkdebug.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
qapi/block-core.json +4 -0
@@ -3919,6 +3919,9 @@ # # @errno: error identifier (errno) to be returned; defaults to EIO # +# @delay-ns: request delay before completion in nanoseconds +# (default: 0, since: 11.1) +# # @sector: specifies the sector index which has to be affected in # order to actually trigger the event; defaults to "any sector" #
@@ -3934,6 +3937,7 @@ '*state': 'int', '*iotype': 'BlkdebugIOType', '*errno': 'int', + '*delay-ns': 'int', '*sector': 'int', '*once': 'bool', '*immediately': 'bool' } }
block/blkdebug.c +14 -1
@@ -95,6 +95,7 @@ typedef struct BlkdebugRule {int immediately;int once;int64_t offset; +int64_t delay_ns; } inject;struct {int new_state;
@@ -144,6 +145,10 @@ static QemuOptsList inject_error_opts = { .name = "immediately", .type = QEMU_OPT_BOOL, }, + { + .name = "delay-ns", + .type = QEMU_OPT_NUMBER, + }, {/* end of list */ } }, };
@@ -216,6 +221,8 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp) rule->options.inject.once =qemu_opt_get_bool (opts,"once" ,0 ); rule->options.inject.immediately =qemu_opt_get_bool (opts,"immediately" ,0 ); + rule->options.inject.delay_ns = +qemu_opt_get_number (opts,"delay-ns" ,0 ); sector =qemu_opt_get_number (opts,"sector" ,-1 ); rule->options.inject.offset = sector ==-1 ?-1 : sector *BDRV_SECTOR_SIZE ;
@@ -594,6 +601,7 @@ static int coroutine_fn rule_check(BlockDriverState *bs, uint64_t offset,BlkdebugRule *rule =NULL ;int error;bool immediately; +int64_t delay_ns;qemu_mutex_lock (&s->lock); QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
@@ -608,13 +616,14 @@ static int coroutine_fn rule_check(BlockDriverState *bs, uint64_t offset, } } -if (!rule || !rule->options.inject.error) { +if (!rule) {qemu_mutex_unlock (&s->lock);return 0 ; } immediately = rule->options.inject.immediately; error = rule->options.inject.error; + delay_ns = rule->options.inject.delay_ns;if (rule->options.inject.once) {QSIMPLEQ_REMOVE (&s->active_rules, rule, BlkdebugRule, active_next);
@@ -622,6 +631,10 @@ static int coroutine_fn rule_check(BlockDriverState *bs, uint64_t offset, }qemu_mutex_unlock (&s->lock); + +if (delay_ns) { +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME , delay_ns); + }if (!immediately) {aio_co_schedule (qemu_get_current_aio_context (),qemu_coroutine_self ());qemu_coroutine_yield ();
Signature · 1 line
2.53.0
PATCH 2/7 block: Add blk_co_start/end_request() and BDRV_REQ_NO_QUEUE
In reply to Kevin WolfIf a device uses blk_inc/dec_in_flight() in order to build macro operations that involve multiple requests for the block layer and that need to be completed as a unit before the BlockBackend can be considered drained, it sets the stage for a deadlock: When a drain is requested, the inner request at the BlockBackend level will be queued in blk_wait_while_drained() and wait until the drained section ends, but at the same time, drain_begin can only return if the whole macro operation at the device level has completed.
Introduce a new interface to allow implementing the logic correctly:
Instead of queueing individual requests, blk_co_start_request() calls blk_wait_while_drained() once at the beginning. The individual requests must then set BDRV_REQ_NO_QUEUE to avoid being queued and running into the deadlock; being wrapped in blk_co_start/end_request() makes sure that drain_begin waits for them and they don't sneak in when the BlockBackend is supposed to already be quiescent.
--- include/block/block-common.h | 11 ++++++++- include/system/block-backend-io.h | 2 ++ block/block-backend.c | 38 +++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 10 deletions(-)
include/block/block-common.h +10 -1
@@ -215,8 +215,17 @@ typedef enum {*/ BDRV_REQ_NO_WAIT =0x400 , +/* +* Used between blk_co_start_request() and blk_end_request() to avoid +* that the request waits in a drained BlockBackend until the drained +* section ends. Waiting would cause a deadlock because drain waits for +* blk_end_request() to be called, but the request never completes +* because it waits for the drain to end. +*/ +BDRV_REQ_NO_QUEUE =0x800 , +/* Mask of valid flags */ -BDRV_REQ_MASK =0x7ff , +BDRV_REQ_MASK =0xfff , } BdrvRequestFlags;#define BDRV_O_NO_SHARE 0x0001/* don't share permissions */
include/system/block-backend-io.h +2 -0
@@ -71,6 +71,8 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,void blk_inc_in_flight (BlockBackend *blk);void blk_dec_in_flight (BlockBackend *blk); +void coroutine_fn blk_co_start_request(BlockBackend *blk); +void blk_end_request (BlockBackend *blk); bool coroutine_fn GRAPH_RDLOCK blk_co_is_inserted(BlockBackend *blk); bool co_wrapper_mixed_bdrv_rdlock blk_is_inserted(BlockBackend *blk);
block/block-backend.c +29 -9
@@ -82,6 +82,7 @@ struct BlockBackend {QemuMutex queued_requests_lock;/* protects queued_requests */ CoQueue queued_requests;bool disable_request_queuing;/* atomic */ +int start_request_count;/* atomic */ VMChangeStateEntry *vmsh;bool force_allow_inactivate;
@@ -1306,10 +1307,16 @@ bool blk_in_drain(BlockBackend *blk) }/* To be called between exactly one pair of blk_inc/dec_in_flight() */ -static void coroutine_fn blk_wait_while_drained(BlockBackend *blk) +static void coroutine_fn blk_wait_while_drained(BlockBackend *blk, +BdrvRequestFlags flags) {assert (blk->in_flight >0 ); +if (flags &BDRV_REQ_NO_QUEUE ) { +assert (qatomic_read (&blk->start_request_count)); +return ; + } +if (qatomic_read (&blk->quiesce_counter) && !qatomic_read (&blk->disable_request_queuing)) {/*
@@ -1335,7 +1342,7 @@ blk_co_do_preadv_part(BlockBackend *blk, int64_t offset, int64_t bytes,BlockDriverState *bs;IO_CODE (); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk, flags);GRAPH_RDLOCK_GUARD ();/* Call blk_bs() only after waiting, the graph may have changed */
@@ -1410,7 +1417,7 @@ blk_co_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,BlockDriverState *bs;IO_CODE (); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk, flags);GRAPH_RDLOCK_GUARD ();/* Call blk_bs() only after waiting, the graph may have changed */
@@ -1523,6 +1530,19 @@ void blk_dec_in_flight(BlockBackend *blk)aio_wait_kick (); } +void coroutine_fn blk_co_start_request(BlockBackend *blk) +{ +blk_inc_in_flight (blk); +blk_wait_while_drained (blk,0 ); +qatomic_inc (&blk->start_request_count); +} + +void blk_end_request (BlockBackend *blk) +{ +qatomic_dec (&blk->start_request_count); +blk_dec_in_flight (blk); +} +static void error_callback_bh (void *opaque) {struct BlockBackendAIOCB *acb = opaque;
@@ -1741,7 +1761,7 @@ blk_co_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf) {IO_CODE (); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk,0 );GRAPH_RDLOCK_GUARD ();if (!blk_co_is_available (blk)) {
@@ -1788,7 +1808,7 @@ blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes)int ret;IO_CODE (); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk,0 );GRAPH_RDLOCK_GUARD (); ret =blk_check_byte_request (blk, offset, bytes);
@@ -1834,7 +1854,7 @@ int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, static int coroutine_fn blk_co_do_flush(BlockBackend *blk) {IO_CODE (); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk,0 );GRAPH_RDLOCK_GUARD ();if (!blk_co_is_available (blk)) {
@@ -2009,7 +2029,7 @@ int coroutine_fn blk_co_zone_report(BlockBackend *blk, int64_t offset,IO_CODE ();blk_inc_in_flight (blk);/* increase before waiting */ -blk_wait_while_drained (blk); +blk_wait_while_drained (blk,0 );GRAPH_RDLOCK_GUARD ();if (!blk_is_available (blk)) {blk_dec_in_flight (blk);
@@ -2034,7 +2054,7 @@ int coroutine_fn blk_co_zone_mgmt(BlockBackend *blk, BlockZoneOp op,IO_CODE ();blk_inc_in_flight (blk); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk,0 );GRAPH_RDLOCK_GUARD (); ret =blk_check_byte_request (blk, offset, len);
@@ -2058,7 +2078,7 @@ int coroutine_fn blk_co_zone_append(BlockBackend *blk, int64_t *offset,IO_CODE ();blk_inc_in_flight (blk); -blk_wait_while_drained (blk); +blk_wait_while_drained (blk, flags);GRAPH_RDLOCK_GUARD ();if (!blk_is_available (blk)) {blk_dec_in_flight (blk);
Signature · 1 line
2.53.0
PATCH 3/7 block: Add flags parameter to blk_*_pdiscard()
In reply to Kevin WolfAll existing callers pass 0, but we need a way to pass BDRV_REQ_NO_QUEUE for discard requests.
--- include/system/block-backend-io.h | 4 ++-- block/block-backend.c | 11 ++++++----- block/export/virtio-blk-handler.c | 2 +- block/mirror.c | 4 ++-- nbd/server.c | 2 +- qemu-io-cmds.c | 2 +- tests/unit/test-block-iothread.c | 4 ++-- 7 files changed, 15 insertions(+), 14 deletions(-)
include/system/block-backend-io.h +2 -2
@@ -218,9 +218,9 @@ int co_wrapper_mixed blk_zone_append(BlockBackend *blk, int64_t *offset, BdrvRequestFlags flags); int co_wrapper_mixed blk_pdiscard(BlockBackend *blk, int64_t offset, -int64_t bytes); +int64_t bytes,BdrvRequestFlags flags); int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, -int64_t bytes); +int64_t bytes,BdrvRequestFlags flags); int co_wrapper_mixed blk_flush(BlockBackend *blk); int coroutine_fn blk_co_flush(BlockBackend *blk);
block/block-backend.c +6 -5
@@ -1803,12 +1803,13 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,/* To be called between exactly one pair of blk_inc/dec_in_flight() */ static int coroutine_fn -blk_co_do_pdiscard (BlockBackend *blk,int64_t offset,int64_t bytes) +blk_co_do_pdiscard (BlockBackend *blk,int64_t offset,int64_t bytes, +BdrvRequestFlags flags) {int ret;IO_CODE (); -blk_wait_while_drained (blk,0 ); +blk_wait_while_drained (blk, flags);GRAPH_RDLOCK_GUARD (); ret =blk_check_byte_request (blk, offset, bytes);
@@ -1824,7 +1825,7 @@ static void coroutine_fn blk_aio_pdiscard_entry(void *opaque)BlkAioEmAIOCB *acb = opaque;BlkRwCo *rwco = &acb->rwco; - rwco->ret =blk_co_do_pdiscard (rwco->blk, rwco->offset, acb->bytes); + rwco->ret =blk_co_do_pdiscard (rwco->blk, rwco->offset, acb->bytes,0 );blk_aio_complete (acb); }
@@ -1838,13 +1839,13 @@ BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, } int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, -int64_t bytes) +int64_t bytes,BdrvRequestFlags flags) {int ret;IO_OR_GS_CODE ();blk_inc_in_flight (blk); - ret =blk_co_do_pdiscard (blk, offset, bytes); + ret =blk_co_do_pdiscard (blk, offset, bytes, flags);blk_dec_in_flight (blk);return ret;
block/export/virtio-blk-handler.c +1 -1
@@ -122,7 +122,7 @@ virtio_blk_discard_write_zeroes(VirtioBlkHandler *handler, struct iovec *iov, }if (blk_co_pdiscard (blk, sector <<VIRTIO_BLK_SECTOR_BITS , - bytes) ==0 ) { + bytes,0 ) ==0 ) {return VIRTIO_BLK_S_OK ; } }
block/mirror.c +2 -2
@@ -454,7 +454,7 @@ static void coroutine_fn mirror_co_discard(void *opaque) *op->bytes_handled = op->bytes; op->is_in_flight = true; - ret =blk_co_pdiscard (op->s->target, op->offset, op->bytes); + ret =blk_co_pdiscard (op->s->target, op->offset, op->bytes,0 );mirror_write_complete (op, ret); }
@@ -1532,7 +1532,7 @@ do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, zero_bitmap_end - zero_bitmap_offset); }assert (!qiov); - ret =blk_co_pdiscard (job->target, offset, bytes); + ret =blk_co_pdiscard (job->target, offset, bytes,0 );break ;default :
nbd/server.c +1 -1
@@ -2990,7 +2990,7 @@ static coroutine_fn int nbd_handle_request(NBDClient *client, "flush failed", errp);case NBD_CMD_TRIM : - ret =blk_co_pdiscard (exp->common.blk, request->from, request->len); + ret =blk_co_pdiscard (exp->common.blk, request->from, request->len,0 );if (ret >=0 && request->flags &NBD_CMD_FLAG_FUA ) { ret =blk_co_flush (exp->common.blk); }
qemu-io-cmds.c +1 -1
@@ -2201,7 +2201,7 @@ static int discard_f(BlockBackend *blk, int argc, char **argv) }clock_gettime (CLOCK_MONOTONIC , &t1); - ret =blk_pdiscard (blk, offset, bytes); + ret =blk_pdiscard (blk, offset, bytes,0 );clock_gettime (CLOCK_MONOTONIC , &t2);if (ret <0 ) {
tests/unit/test-block-iothread.c +2 -2
@@ -270,11 +270,11 @@ static void test_sync_op_blk_pdiscard(BlockBackend *blk)int ret;/* Early success: UNMAP not supported */ - ret =blk_pdiscard (blk,0 ,512 ); + ret =blk_pdiscard (blk,0 ,512 ,0 ); g_assert_cmpint(ret, ==, 0);/* Early error: Negative offset */ - ret =blk_pdiscard (blk,-2 ,512 ); + ret =blk_pdiscard (blk,-2 ,512 ,0 ); g_assert_cmpint(ret, ==, -EIO); }
Signature · 1 line
2.53.0
PATCH 4/7 ide: Minimal fix for deadlock between TRIM and drain
In reply to Kevin WolfThe implementation of TRIM in IDE can chain multiple discard requests and uses blk_inc/dec_in_flight() to make sure that the whole TRIM operation has completed when the device needs to be quiescent (e.g. for the drain when performing an IDE reset, it would be bad if an IDE request like TRIM were still in flight).
The problem is that each drain request calls blk_wait_while_drained() and when draining, it waits until the drained section ends. At the same time, drain_begin can only return if the whole TRIM operation has completed. This is a classic deadlock.
Use blk_co_start/end_request() and BDRV_REQ_NO_QUEUE to avoid the problem. This requires moving the TRIM state machine to a coroutine. This commit does the minimal conversion so that we do have a coroutine that works for the fix, but it still looks much like a callback-based implementation. This will be cleaned up in the next patch.
--- hw/ide/core.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-)
hw/ide/core.c +18 -19
@@ -420,7 +420,6 @@ typedef struct TrimAIOCB {QEMUBH *bh;int ret;QEMUIOVector *qiov; -BlockAIOCB *aiocb;int i, j; } TrimAIOCB;
@@ -433,11 +432,6 @@ static void trim_aio_cancel(BlockAIOCB *acb) iocb->i = (iocb->qiov->iov[iocb->j].iov_len /8 ) -1 ; iocb->ret = -ECANCELED ; - -if (iocb->aiocb) { -blk_aio_cancel_async (iocb->aiocb); - iocb->aiocb =NULL ; - } }static const AIOCBInfo trim_aiocb_info = {
@@ -456,15 +450,20 @@ static void ide_trim_bh_cb(void *opaque) iocb->bh =NULL ;qemu_aio_unref (iocb); -/* Paired with an increment in ide_issue_trim() */ -blk_dec_in_flight (blk); +/* Paired with blk_co_start_request in ide_trim_co_entry() */ +blk_end_request (blk); } -static void ide_issue_trim_cb (void *opaque,int ret) +static void coroutine_fn ide_trim_co_entry(void *opaque) {TrimAIOCB *iocb = opaque;IDEState *s = iocb->s; +int ret =0 ; + +/* Paired with blk_end_request in ide_trim_bh_cb() */ +blk_co_start_request (s->blk); +loop:if (iocb->i >=0 ) {if (ret >=0 ) {block_acct_done (blk_get_stats (s->blk), &s->acct);
@@ -499,11 +498,11 @@ static void ide_issue_trim_cb(void *opaque, int ret) count << BDRV_SECTOR_BITS, BLOCK_ACCT_UNMAP);/* Got an entry! Submit and exit. */ - iocb->aiocb =blk_aio_pdiscard (s->blk, - sector <<BDRV_SECTOR_BITS , - count <<BDRV_SECTOR_BITS , - ide_issue_trim_cb, opaque); -return ; + ret =blk_co_pdiscard (s->blk, + sector <<BDRV_SECTOR_BITS , + count <<BDRV_SECTOR_BITS , +BDRV_REQ_NO_QUEUE ); + goto loop; } iocb->j++;
@@ -514,7 +513,6 @@ static void ide_issue_trim_cb(void *opaque, int ret) } done: - iocb->aiocb =NULL ;if (iocb->bh) {replay_bh_schedule_event (iocb->bh); }
@@ -527,9 +525,7 @@ BlockAIOCB *ide_issue_trim(IDEState *s = opaque;IDEDevice *dev = s->unit ? s->bus->slave : s->bus->master;TrimAIOCB *iocb; - -/* Paired with a decrement in ide_trim_bh_cb() */ -blk_inc_in_flight (s->blk); +Coroutine *co; iocb =blk_aio_get (&trim_aiocb_info, s->blk, cb, cb_opaque); iocb->s = s;
@@ -539,7 +535,10 @@ BlockAIOCB *ide_issue_trim( iocb->qiov = qiov; iocb->i =-1 ; iocb->j =0 ; -ide_issue_trim_cb (iocb,0 ); + + co =qemu_coroutine_create (ide_trim_co_entry, iocb); +aio_co_enter (qemu_get_current_aio_context (), co); +return &iocb->common; }
Signature · 1 line
2.53.0
PATCH 5/7 ide: Clean up ide_trim_co_entry() to be idiomatic coroutine code
In reply to Kevin WolfThe previous commit did a minimal conversion of the callback based state machine for TRIM to a coroutine in order to fix a bug. Refactor it to actually look like normal coroutine based code, which improves its readability.
--- hw/ide/core.c | 87 +++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 48 deletions(-)
hw/ide/core.c +39 -48
@@ -420,18 +420,15 @@ typedef struct TrimAIOCB {QEMUBH *bh;int ret;QEMUIOVector *qiov; -int i, j; +bool canceled; } TrimAIOCB;static void trim_aio_cancel (BlockAIOCB *acb) {TrimAIOCB *iocb =container_of (acb, TrimAIOCB, common); -/* Exit the loop so ide_issue_trim_cb will not continue */ - iocb->j = iocb->qiov->niov -1 ; - iocb->i = (iocb->qiov->iov[iocb->j].iov_len /8 ) -1 ; - - iocb->ret = -ECANCELED ; +/* Exit the loop so ide_trim_co_entry will not continue */ + iocb->canceled = true; }static const AIOCBInfo trim_aiocb_info = {
@@ -458,60 +455,55 @@ static void coroutine_fn ide_trim_co_entry(void *opaque) {TrimAIOCB *iocb = opaque;IDEState *s = iocb->s; -int ret =0 ; +int i, j; +int ret;/* Paired with blk_end_request in ide_trim_bh_cb() */ blk_co_start_request (s->blk); -loop: -if (iocb->i >=0 ) { -if (ret >=0 ) { -block_acct_done (blk_get_stats (s->blk), &s->acct); - }else { -block_acct_failed (blk_get_stats (s->blk), &s->acct); - } - } +for (j =0 ; j < iocb->qiov->niov; j++) { +for (i =0 ; i < iocb->qiov->iov[j].iov_len /8 ; i++) { +uint64_t *buffer = iocb->qiov->iov[j].iov_base; -if (ret >=0 ) { -while (iocb->j < iocb->qiov->niov) { -int j = iocb->j; -while (++iocb->i < iocb->qiov->iov[j].iov_len /8 ) { -int i = iocb->i; -uint64_t *buffer = iocb->qiov->iov[j].iov_base; +/* 6-byte LBA + 2-byte range per entry */ +uint64_t entry =le64_to_cpu (buffer[i]); +uint64_t sector = entry &0x0000ffffffffffffULL ; +uint16_t count = entry >>48 ; -/* 6-byte LBA + 2-byte range per entry */ -uint64_t entry =le64_to_cpu (buffer[i]); -uint64_t sector = entry &0x0000ffffffffffffULL ; -uint16_t count = entry >>48 ; +if (count ==0 ) { +continue ; + } -if (count ==0 ) { -continue ; - } +if (iocb->canceled) { + iocb->ret = -ECANCELED ; + goto done; + } -if (!ide_sect_range_ok (s, sector, count)) { -block_acct_invalid (blk_get_stats (s->blk),BLOCK_ACCT_UNMAP ); - iocb->ret = -EINVAL ; - goto done; - } +if (!ide_sect_range_ok (s, sector, count)) { +block_acct_invalid (blk_get_stats (s->blk),BLOCK_ACCT_UNMAP ); + iocb->ret = -EINVAL ; + goto done; + } -block_acct_start (blk_get_stats (s->blk), &s->acct, - count <<BDRV_SECTOR_BITS ,BLOCK_ACCT_UNMAP ); +block_acct_start (blk_get_stats (s->blk), &s->acct, + count <<BDRV_SECTOR_BITS ,BLOCK_ACCT_UNMAP ); -/* Got an entry! Submit and exit. */ - ret =blk_co_pdiscard (s->blk, - sector <<BDRV_SECTOR_BITS , - count <<BDRV_SECTOR_BITS , -BDRV_REQ_NO_QUEUE ); - goto loop; +/* Got an entry! Submit and exit. */ + ret =blk_co_pdiscard (s->blk, + sector <<BDRV_SECTOR_BITS , + count <<BDRV_SECTOR_BITS , +BDRV_REQ_NO_QUEUE ); +if (ret >=0 ) { +block_acct_done (blk_get_stats (s->blk), &s->acct); + }else { + iocb->ret = ret; +block_acct_failed (blk_get_stats (s->blk), &s->acct); + goto done; } - - iocb->j++; - iocb->i =-1 ; } - }else { - iocb->ret = ret; } + iocb->ret =0 ; done:if (iocb->bh) {replay_bh_schedule_event (iocb->bh);
@@ -533,8 +525,7 @@ BlockAIOCB *ide_issue_trim( &DEVICE(dev)->mem_reentrancy_guard); iocb->ret =0 ; iocb->qiov = qiov; - iocb->i =-1 ; - iocb->j =0 ; + iocb->canceled = false; co =qemu_coroutine_create (ide_trim_co_entry, iocb);aio_co_enter (qemu_get_current_aio_context (), co);
Signature · 1 line
2.53.0
PATCH 6/7 ide-test: Factor out wait_dma_completion()
In reply to Kevin Wolf--- tests/qtest/ide-test.c | 48 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-)
tests/qtest/ide-test.c +29 -19
@@ -200,6 +200,34 @@ static uint64_t trim_range_le(uint64_t sector, uint16_t count)return cpu_to_le64 (((uint64_t )count <<48 ) + sector); } +static uint8_t wait_dma_completion (QTestState *qts,QPCIDevice *dev, +QPCIBar bmdma_bar,QPCIBar ide_bar) +{ +uint8_t status; + +/* Wait for the DMA transfer to complete */ +do { + status =qpci_io_readb (dev, bmdma_bar, bmreg_status); + }while ((status & (BM_STS_ACTIVE |BM_STS_INTR )) ==BM_STS_ACTIVE ); + + g_assert_cmpint(qtest_get_irq(qts, IDE_PRIMARY_IRQ), ==, + !!(status &BM_STS_INTR )); + +/* Check IDE status code */ +assert_bit_set (qpci_io_readb (dev, ide_bar, reg_status),DRDY ); +assert_bit_clear (qpci_io_readb (dev, ide_bar, reg_status),BSY |DRQ ); + +/* Reading the status register clears the IRQ */ +g_assert (!qtest_get_irq (qts,IDE_PRIMARY_IRQ )); + +/* Stop DMA transfer if still active */ +if (status &BM_STS_ACTIVE ) { +qpci_io_writeb (dev, bmdma_bar, bmreg_cmd,0 ); + } + +return status; +} + static int send_dma_request(QTestState *qts, int cmd, uint64_t sector, int nb_sectors, PrdtEntry *prdt, int prdt_entries, void(*post_exec)(QPCIDevice *dev, QPCIBar ide_bar,
@@ -280,25 +308,7 @@ static int send_dma_request(QTestState *qts, int cmd, uint64_t sector,qpci_io_writeb (dev, bmdma_bar, bmreg_cmd,0 ); } -/* Wait for the DMA transfer to complete */ -do { - status =qpci_io_readb (dev, bmdma_bar, bmreg_status); - }while ((status & (BM_STS_ACTIVE |BM_STS_INTR )) ==BM_STS_ACTIVE ); - - g_assert_cmpint(qtest_get_irq(qts, IDE_PRIMARY_IRQ), ==, - !!(status &BM_STS_INTR )); - -/* Check IDE status code */ -assert_bit_set (qpci_io_readb (dev, ide_bar, reg_status),DRDY ); -assert_bit_clear (qpci_io_readb (dev, ide_bar, reg_status),BSY |DRQ ); - -/* Reading the status register clears the IRQ */ -g_assert (!qtest_get_irq (qts,IDE_PRIMARY_IRQ )); - -/* Stop DMA transfer if still active */ -if (status &BM_STS_ACTIVE ) { -qpci_io_writeb (dev, bmdma_bar, bmreg_cmd,0 ); - } + status =wait_dma_completion (qts, dev, bmdma_bar, ide_bar);free_pci_device (dev);
Signature · 1 line
2.53.0
PATCH 7/7 ide-test: Test reset during TRIM
In reply to Kevin WolfThis is a regression test for the bug fixed in the previous commits, a deadlock between the drain issued by an IDE reset and the TRIM state machine.
--- tests/qtest/ide-test.c | 95 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 8 deletions(-)
tests/qtest/ide-test.c +87 -8
@@ -41,8 +41,11 @@#define IDE_PCI_FUNC 1#define IDE_BASE 0x1f0 +#define IDE_BASE2 0x3f6#define IDE_PRIMARY_IRQ 14 +#define IDE_CTRL_RESET 0x04 +#define ATAPI_BLOCK_SIZE 2048/* How many bytes to receive via ATAPI PIO at one time.
@@ -99,6 +102,7 @@ enum {CMDF_ABORT =0x100 ,CMDF_NO_BM =0x200 , + CMDF_NO_WAIT = 0x400, }; enum {
@@ -228,21 +232,21 @@ static uint8_t wait_dma_completion(QTestState *qts, QPCIDevice *dev,return status; } -static int send_dma_request (QTestState *qts,int cmd,uint64_t sector, -int nb_sectors,PrdtEntry *prdt,int prdt_entries, -void (*post_exec)(QPCIDevice *dev,QPCIBar ide_bar, -uint64_t sector,int nb_sectors)) +static int send_dma_request_dev (QTestState *qts,QPCIDevice *dev, +QPCIBar bmdma_bar,QPCIBar ide_bar,int cmd, +uint64_t sector,int nb_sectors, +PrdtEntry *prdt,int prdt_entries, +void (*post_exec)(QPCIDevice *dev, +QPCIBar ide_bar, +uint64_t sector, +int nb_sectors)) { -QPCIDevice *dev; -QPCIBar bmdma_bar, ide_bar;uintptr_t guest_prdt;size_t len;bool from_dev;uint8_t status;int flags; - dev =get_pci_device (qts, &bmdma_bar, &ide_bar); - flags = cmd & ~0xff ; cmd &=0xff ;
@@ -308,8 +312,28 @@ static int send_dma_request(QTestState *qts, int cmd, uint64_t sector,qpci_io_writeb (dev, bmdma_bar, bmreg_cmd,0 ); } +if (flags &CMDF_NO_WAIT ) { +return 0 ; + } + status =wait_dma_completion (qts, dev, bmdma_bar, ide_bar); +return status; +} + +static int send_dma_request (QTestState *qts,int cmd,uint64_t sector, +int nb_sectors,PrdtEntry *prdt,int prdt_entries, +void (*post_exec)(QPCIDevice *dev,QPCIBar ide_bar, +uint64_t sector,int nb_sectors)) +{ +QPCIDevice *dev; +QPCIBar bmdma_bar, ide_bar; +uint8_t status; + + dev =get_pci_device (qts, &bmdma_bar, &ide_bar); + status =send_dma_request_dev (qts, dev, bmdma_bar, ide_bar, + cmd, sector, nb_sectors, prdt, prdt_entries, + post_exec);free_pci_device (dev);return status;
@@ -457,6 +481,60 @@ static void test_bmdma_trim(void)test_bmdma_teardown (qts); } +static void test_bmdma_trim_reset (void ) +{ +QTestState *qts; +QPCIDevice *dev; +QPCIBar bmdma_bar, ide_bar, ide_bar2; +uint8_t status; +const uint64_t trim_range[] = { +trim_range_le (0 ,2 ), +trim_range_le (6 ,8 ), + }; +size_t len =512 ; +uint8_t *buf; +uintptr_t guest_buf; +PrdtEntry prdt[1 ]; + + qts =ide_test_start ( +"-blockdev file,filename=%s,node-name=img " +"-blockdev blkdebug,image=img,node-name=dbg,discard=unmap," +"inject-error.0.event=none,inject-error.0.iotype=discard," +"inject-error.0.errno=0,inject-error.0.delay-ns=1000000 " +"-device ide-hd,drive=dbg,bus=ide.0" , + tmp_path[0 ]); +qtest_irq_intercept_in (qts,"ioapic" ); + + guest_buf =guest_alloc (&guest_malloc, len); + prdt[0 ].addr =cpu_to_le32 (guest_buf), + prdt[0 ].size =cpu_to_le32 (len |PRDT_EOT ), + + dev =get_pci_device (qts, &bmdma_bar, &ide_bar); + ide_bar2 =qpci_legacy_iomap (dev,IDE_BASE2 ); + + buf =g_malloc (len); + +/* TRIM request with two segments */ + *((uint64_t *)buf) = trim_range[0 ]; + *((uint64_t *)buf +1 ) = trim_range[1 ]; + +qtest_memwrite (qts, guest_buf, buf,2 *sizeof (uint64_t )); + +send_dma_request_dev (qts, dev, bmdma_bar, ide_bar,CMD_DSM |CMDF_NO_WAIT ,0 ,1 , prdt, +ARRAY_SIZE (prdt),NULL ); + +/* Reset the device while the first segment is in flight */ +qpci_io_writeb (dev, ide_bar2,0 ,IDE_CTRL_RESET ); + + status =wait_dma_completion (qts, dev, bmdma_bar, ide_bar); + g_assert_cmphex(status, ==, BM_STS_INTR); +assert_bit_clear (qpci_io_readb (dev, ide_bar, reg_status),DF |ERR ); + +free_pci_device (dev); +g_free (buf); +test_bmdma_teardown (qts); +} +/* * This test is developed according to the Programming Interface for * Bus Master IDE Controller (Revision 1.0 5/16/94)
@@ -1138,6 +1216,7 @@ int main(int argc, char **argv)qtest_add_func ("/ide/bmdma/simple_rw" , test_bmdma_simple_rw);qtest_add_func ("/ide/bmdma/trim" , test_bmdma_trim); +qtest_add_func ("/ide/bmdma/trim_reset" , test_bmdma_trim_reset);qtest_add_func ("/ide/bmdma/various_prdts" , test_bmdma_various_prdts);qtest_add_func ("/ide/bmdma/no_busmaster" , test_bmdma_no_busmaster);
Signature · 1 line
2.53.0
Kevin Wolf
AppliedAm 21.04.2026 um 18:11 hat Kevin Wolf geschrieben:
Quoted 11 lines
Patches 2 and 4 are the core of the fix, see their commit message for details.
Kevin Wolf (7):
blkdebug: Add 'delay-ns' option
block: Add blk_co_start/end_request() andBDRV_REQ_NO_QUEUE
block: Add flags parameter to blk_*_pdiscard()
ide: Minimal fix for deadlock between TRIM and drain
ide: Clean upide_trim_co_entry()to be idiomatic coroutine code
ide-test: Factor outwait_dma_completion()
ide-test: Test reset during TRIM
Applied to the block branch.
Kevin
Quoted 11 lines · Kevin Wolf
Patches 2 and 4 are the core of the fix, see their commit message for details.
Kevin Wolf (7):
blkdebug: Add 'delay-ns' option
block: Add blk_co_start/end_request() andBDRV_REQ_NO_QUEUE
block: Add flags parameter to blk_*_pdiscard()
ide: Minimal fix for deadlock between TRIM and drain
ide: Clean upide_trim_co_entry()to be idiomatic coroutine code
ide-test: Factor outwait_dma_completion()
ide-test: Test reset during TRIM
Hi!
Is this whole patchset supposed to go to qemu-stable, to be applied to stable qemu releases? I'm a bit confused because only one of the patches has Cc: qemu-stable tag, but whole patchset has been Cc'd to qemu-stable, and at least patch 2 is also needed.
I'd gladly add testing patches too, but I'd rather understand the inital intention.
The actual fix - "ide: Minimal fix for deadlock between TRIM and drain" (commit 095c08a7ba in the master branch now) does not work in any of the qemu stable branches as-is, because even 11.0.x lacks blk_co_start_request() and BDRV_REQ_NO_QUEUE definition - so at least 34a67637767d "block: Add blk_co_start/end_request() and BDRV_REQ_NO_QUEUE" is needed.
Next, blk_co_pdiscard() lacks "flags" parameter, so another commit, 53074ba0330a "block: Add flags parameter to blk_*_pdiscard()", is also needed.
But this area received quite a lot of changes, so I don't know how far this can be applied to older stable qemu branches, and how well it all will work within older branches. I'm especially interested in 10.0.x series, which has become an LTS.
What do you think?
I'm testing 11.0.x with whole this series and the above-mentioned 2 changes, for now, and I'll let you know how it goes for 10.0 and 10.2, hopefully later today.
Thanks,
/mjt
Quoted 14 lines
qapi/block-core.json | 4 + include/block/block-common.h | 11 ++- include/system/block-backend-io.h | 6 +- block/blkdebug.c | 15 +++- block/block-backend.c | 47 +++++++--- block/export/virtio-blk-handler.c | 2 +- block/mirror.c | 4 +- hw/ide/core.c | 110 +++++++++++------------- nbd/server.c | 2 +- qemu-io-cmds.c | 2 +- tests/qtest/ide-test.c | 137 ++++++++++++++++++++++++------ tests/unit/test-block-iothread.c | 4 +- 12 files changed, 236 insertions(+), 108 deletions(-)
Quoted 13 lines · Michael Tokarev
Quoted 11 lines · Kevin Wolf
Patches 2 and 4 are the core of the fix, see their commit message for details.
Kevin Wolf (7):
blkdebug: Add 'delay-ns' option
block: Add blk_co_start/end_request() andBDRV_REQ_NO_QUEUE
block: Add flags parameter to blk_*_pdiscard()
ide: Minimal fix for deadlock between TRIM and drain
ide: Clean upide_trim_co_entry()to be idiomatic coroutine code
ide-test: Factor outwait_dma_completion()
ide-test: Test reset during TRIM
Quoted 6 lines
Hi!
Is this whole patchset supposed to go to qemu-stable, to be applied to stable qemu releases? I'm a bit confused because only one of the patches has Cc: qemu-stable tag, but whole patchset has been Cc'd to qemu-stable, and at least patch 2 is also needed.
Umm. Please scratch that. Ofcourse whole patchset is needed, and all "other" patches are parts of the same patcheset! This is what happens when I do things in ENOCOFFEE mode :)
However, it'd be still nice to understand is this stuff ok to go for 10.0.x branch, as this area is rather fragile...
Thank you!
/mjt