loop: nowait aio bug fixes
v3 cover loop: nowait aio bug fixes
Hi Jens,
The 1st two patches fix error handling code paths for nowait aio.
The 3rd patch fixes IO hang issue because of rqos throttle.
The last patch improves blk_mq_flush_plug_list() to support new request added to plug list from ->queue_rq().
Tests:
- not observe regression on xfstests
- not observe performance regression on nvme polling/irq test
V3:
- don't disable rqos
- add BD_LOWLEVEL_BIO_FIRST flag for loop to submit lowlevel bios first
- improves blk_mq_flush_plug_list() to accept new request added from ->queue_rq()
V2:
- use QUEUE_FLAG_DISABLE_WBT_DEF to disable wbt, so that check of QUEUE_FLAG_QOS_ENABLED can work as expected
Ming Lei (4):
loop: move kiocb_start_write to aio prep phase
loop: fix error handling in aio functions
loop: fix NOWAIT io hang by introducing BD_LOWLEVEL_BIO_FIRST
blk-mq: fix plug list corruption when queue_rq adds new requests
block/blk-core.c | 3 +- block/blk-mq.c | 64 +++++++++++++++++++++++++++++---------- drivers/block/loop.c | 27 ++++++++++++----- include/linux/blk_types.h | 1 + 4 files changed, 70 insertions(+), 25 deletions(-)
Signature · 1 line
2.47.0
PATCH v3 1/4 loop: move kiocb_start_write to aio prep phase
Move the kiocb_start_write() call from lo_submit_rw_aio() to lo_rw_aio_prep() to initialize write accounting earlier in the I/O preparation phase rather than during submission.
Make sure both kiocb_start_write() and kiocb_end_write() are called just once.
--- drivers/block/loop.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/block/loop.c +5 -4
@@ -402,6 +402,9 @@ static int lo_rw_aio_prep(struct loop_device *lo, struct loop_cmd *cmd, cmd->iocb.ki_complete =NULL ; cmd->iocb.ki_flags =0 ; } + +if (req_op (rq) ==REQ_OP_WRITE ) +kiocb_start_write (&cmd->iocb);return 0 ; }
@@ -431,11 +434,9 @@ static int lo_submit_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, }atomic_set (&cmd->ref,2 ); - -if (rw ==ITER_SOURCE ) { -kiocb_start_write (&cmd->iocb); +if (rw ==ITER_SOURCE ) ret = file->f_op->write_iter (&cmd->iocb, &iter); - }else +else ret = file->f_op->read_iter (&cmd->iocb, &iter);lo_rw_aio_do_completion (cmd);
Signature · 1 line
2.47.0
PATCH v3 2/4 loop: fix error handling in aio functions
In reply to Ming LeiReplace goto fail with direct return in lo_rw_aio() and lo_rw_aio_nowait() error paths.
We can't call lo_rw_aio_complete() for early completion when lo request reference isn't initialized yet.
--- drivers/block/loop.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/block/loop.c +2 -4
@@ -458,12 +458,11 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,if (!cmd->use_aio || !lo_backfile_support_nowait (lo)) { ret =lo_rw_aio_prep (lo, cmd, nr_bvec, pos);if (unlikely (ret)) - goto fail; +return ret; } cmd->iocb.ki_flags &= ~IOCB_NOWAIT ; ret =lo_submit_rw_aio (lo, cmd, nr_bvec, rw); -fail:if (ret != -EIOCBQUEUED )lo_rw_aio_complete (&cmd->iocb, ret);return -EIOCBQUEUED ;
@@ -505,14 +504,13 @@ static int lo_rw_aio_nowait(struct loop_device *lo, struct loop_cmd *cmd,int ret =lo_rw_aio_prep (lo, cmd, nr_bvec, pos);if (unlikely (ret)) - goto fail; +return ret;if (!lo_aio_try_nowait (lo, cmd))return -EAGAIN ; cmd->iocb.ki_flags |=IOCB_NOWAIT ; ret =lo_submit_rw_aio (lo, cmd, nr_bvec, rw); -fail:if (ret != -EIOCBQUEUED && ret != -EAGAIN )lo_rw_aio_complete (&cmd->iocb, ret);return ret;
Signature · 1 line
2.47.0
PATCH v3 3/4 loop: fix NOWAIT io hang by introducing BD_LOWLEVEL_BIO_FIRST
In reply to Ming LeiWhen loop device is configured with DIRECT_IO, NOWAIT IOs can hang because of a deadlock scenario:
- loop IO can be throttled by rqos
- backing file IOs are always added to tail of per-task ->bio_list
When loop IO is throttled, the dependent backing file IO cannot be handled, resulting in deadlock.
Introduce BD_LOWLEVEL_BIO_FIRST flag and apply it for loop DIRECT_IO mode, so block layer goes through __submit_bio_noacct() in which the low level IOs are handled first.
This ensures proper bio submission order and prevents NOWAIT IO hangs in loop direct I/O mode.
--- block/blk-core.c | 3 ++- drivers/block/loop.c | 12 ++++++++++++ include/linux/blk_types.h | 1 + 3 files changed, 15 insertions(+), 1 deletion(-)
block/blk-core.c +2 -1
@@ -751,7 +751,8 @@ void submit_bio_noacct_nocheck(struct bio *bio, bool split)bio_list_add_head (¤t->bio_list[0 ], bio); else bio_list_add(¤t->bio_list[0], bio); - }else if (!bdev_test_flag (bio->bi_bdev,BD_HAS_SUBMIT_BIO )) { + }else if (!bdev_test_flag (bio->bi_bdev,BD_HAS_SUBMIT_BIO | +BD_LOWLEVEL_BIO_FIRST )) {__submit_bio_noacct_mq (bio); }else {__submit_bio_noacct (bio);
drivers/block/loop.c +12 -0
@@ -208,6 +208,18 @@ static inline void loop_update_dio(struct loop_device *lo)if ((lo->lo_flags &LO_FLAGS_DIRECT_IO ) && !lo_can_use_dio (lo)) lo->lo_flags &= ~LO_FLAGS_DIRECT_IO ; + +/* +* NOWAIT is applied for direct IO mode, so backing file IOs are +* submitted in loop IO context. BD_LOWLEVEL_BIO_FIRST has to be +* set for avoiding IO deadlock which is triggered when loop IO +* and backing file IO are reordered, meanwhile loop IO is throttled +* by block layer RQOS. +*/ +if (lo->lo_flags &LO_FLAGS_DIRECT_IO ) +bdev_set_flag (lo->lo_disk->part0,BD_LOWLEVEL_BIO_FIRST ); +else +bdev_clear_flag (lo->lo_disk->part0,BD_LOWLEVEL_BIO_FIRST ); }/**
include/linux/blk_types.h +1 -0
@@ -54,6 +54,7 @@ struct block_device {#ifdef CONFIG_FAIL_MAKE_REQUEST #define BD_MAKE_IT_FAIL (1u<<12)#endif +#define BD_LOWLEVEL_BIO_FIRST (1u<<13)dev_t bd_dev;struct address_space *bd_mapping;/* page cache */
Signature · 1 line
2.47.0
PATCH v3 4/4 blk-mq: fix plug list corruption when queue_rq adds new requests
In reply to Ming LeiWhen blk_mq_flush_plug_list() dispatches requests, stacking drivers (like loop with NOWAIT support) may submit bios to backing devices from within their queue_rq() callback. These backing device IOs can add new requests back to the same plug->mq_list that's currently being iterated, causing list corruption.
The corruption occurs in this call path:
submit_bio() with active plug
blk_mq_submit_bio()
blk_add_rq_to_plug() - adds loop request
if plug is full:
blk_mq_flush_plug_list(plug, false)
blk_mq_dispatch_list(&plug->mq_list, ...)
rq_list_pop(&plug->mq_list) # Iterating
loop_queue_rq()
lo_rw_aio_nowait()
file->f_op->write_iter() # Backing file IO
submit_bio() # New bio!
blk_mq_submit_bio()
blk_add_rq_to_plug()
rq_list_add_tail(&plug->mq_list, rq) # CORRUPTION!Fix this by extracting the dispatch logic into __blk_mq_flush_plug_list() and making blk_mq_flush_plug_list() loop:
1. Save plug state (rq_count, has_elevator, multiple_queues)
2. Reset plug state to allow new requests
3. Swap plug->mq_list to a local list
4. Dispatch the local list via __blk_mq_flush_plug_list()
5. Repeat if new requests were added during dispatch
This ensures:
- No list corruption: each iteration works on a detached copy
- Complete flush: keeps flushing until no new requests are added
- Handles nesting: recursive calls see rq_count=0 and return early
--- block/blk-mq.c | 64 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 16 deletions(-)
block/blk-mq.c +48 -16
@@ -2955,10 +2955,30 @@ static void blk_mq_dispatch_multiple_queue_requests(struct rq_list *rqs) }while (!rq_list_empty (rqs)); } +static void __blk_mq_flush_plug_list (struct rq_list *list, +unsigned int depth, +bool has_elevator, +bool multiple_queues, +bool from_schedule) +{ +if (!has_elevator && !from_schedule) { +if (multiple_queues) { +blk_mq_dispatch_multiple_queue_requests (list); +return ; + } + +blk_mq_dispatch_queue_requests (list, depth); +if (rq_list_empty (list)) +return ; + } + +do { +blk_mq_dispatch_list (list, from_schedule); + }while (!rq_list_empty (list)); +} +void blk_mq_flush_plug_list (struct blk_plug *plug,bool from_schedule) { -unsigned int depth; -/* * We may have been called recursively midway through handling * plug->mq_list via a schedule() in the driver's queue_rq() callback.
@@ -2968,23 +2988,35 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)*/ if (plug->rq_count ==0 )return ; - depth = plug->rq_count; - plug->rq_count =0 ; -if (!plug->has_elevator && !from_schedule) { -if (plug->multiple_queues) { -blk_mq_dispatch_multiple_queue_requests (&plug->mq_list); -return ; - } +/* +* Flush requests in a loop to handle cases where queue_rq() callback +* adds new requests back to the plug (e.g., stacking drivers like loop +* submitting bios to backing devices). Keep flushing until no new +* requests are added. +*/ +do { +struct rq_list reqs; +unsigned int depth; +bool has_elevator, multiple_queues; -blk_mq_dispatch_queue_requests (&plug->mq_list, depth); -if (rq_list_empty (&plug->mq_list)) -return ; - } + depth = plug->rq_count; + plug->rq_count =0 ; + has_elevator = plug->has_elevator; + plug->has_elevator = false; + multiple_queues = plug->multiple_queues; + plug->multiple_queues = false; -do { -blk_mq_dispatch_list (&plug->mq_list, from_schedule); - }while (!rq_list_empty (&plug->mq_list)); +/* +* Swap plug->mq_list to a local list to allow new requests +* being added to plug->mq_list during dispatching. +*/ + reqs = plug->mq_list; +rq_list_init (&plug->mq_list); + +__blk_mq_flush_plug_list (&reqs, depth, has_elevator, + multiple_queues, from_schedule); + }while (plug->rq_count >0 ); } static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
Signature · 1 line
2.47.0