From: Ye Bin When processing nvme_timeout(): [ 206.734601][ T8184] nvme nvme0: I/O tag 512 (1200) opcode 0x0 (I/O Cmd) QID 3 timeout, aborting req_op:FLUSH(2) size:0 [ 206.736112][ C0] nvme nvme0: Abort status: 0x0 [ 208.094637][ T8184] nvme nvme0: I/O tag 512 (1200) opcode 0x0 (I/O Cmd) QID 3 timeout, reset controller [root@localhost ~]# cat /proc/8184/stack [<0>] msleep+0x37/0x50 [<0>] blk_mq_tagset_wait_completed_request+0x6f/0xe0 [<0>] nvme_cancel_tagset+0x79/0xa0 [<0>] nvme_dev_disable+0x55c/0x7e0 [<0>] nvme_timeout+0x25b/0x1530 [<0>] blk_mq_handle_expired+0x210/0x2c0 [<0>] bt_iter+0x2bb/0x360 [<0>] blk_mq_queue_tag_busy_iter+0x9f8/0x1f30 [<0>] blk_mq_timeout_work+0x5dc/0x7d0 [<0>] process_one_work+0xa08/0x1d00 [<0>] worker_thread+0x698/0xeb0 [<0>] kthread+0x408/0x540 [<0>] ret_from_fork+0xa4d/0xdd0 [<0>] ret_from_fork_asm+0x1a/0x30 The above issue may happen as follows: nvme_timeout // tag 512 request's flush request the first timeout iod->aborted = 1; abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY); // Abort tag 512 flush request blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio); // Abort request completion, will no wait .... ****'abort_req' not complete*** .... nvme_timeout // tag 512 request's flush request the second timeout if (!nvmeq->qid || (iod->flags & IOD_ABORTED)) nvme_req(req)->flags |= NVME_REQ_CANCELLED; goto disable; ... **** tag 512 request's flush request end **** nvme_try_complete_req // PATH[1] blk_mq_complete_request_remote(req); WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); ... nvme_end_req(req); blk_mq_end_request(req, status); __blk_mq_end_request(rq, error); if (rq->end_io) rq->end_io(rq, error); flush_end_io(rq, error); // The timeout process holds the reference count. // so request keep MQ_RQ_COMPLETE state if (!refcount_dec_and_test(&flush_rq->ref)) fq->rq_status = error; return; **** tag 512 flush request is MQ_RQ_COMPLETE state **** disable: nvme_dev_disable(dev, false); nvme_cancel_tagset(&dev->ctrl); blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl); nvme_cancel_request if (blk_mq_request_completed(req)) // PATH[2] return true; blk_mq_tagset_wait_completed_request(&dev->tagset); while (true) blk_mq_tagset_busy_iter(tagset, blk_mq_tagset_count_completed_rqs, &count); blk_mq_tagset_count_completed_rqs(); // request is MQ_RQ_COMPLETE state if (blk_mq_request_completed(rq)) // return true (*count)++; if (!count) // So the value of 'count' is never 0, loop endless break; msleep(5); The root cause is that a flush request is moved to MQ_RQ_IDLE only when its reference count drops to zero, which was introduced by commit 9f16a66733c9 ("block: mark flush request as IDLE when it is really finished"). When the last reference is held by the timeout work itself (PATH[1] and PATH[2]), flush_end_io() returns early and leaves the request in MQ_RQ_COMPLETE, so blk_mq_tagset_wait_completed_request() called from the same timeout path never observes count == 0 and loops forever. Mark the flush request MQ_RQ_IDLE as soon as its completion is processed, before waiting for the remaining references. Reference counting still prevents the request from being freed or reused until the last reference is dropped, so its tag cannot be reallocated underneath a holder. flush_end_io() is also invoked from blk_mq_put_rq_ref() when a tag iterator drops its reference, including for flush requests that are still in flight. That path must not change the request state, otherwise the timeout handler would no longer see an in-flight flush request (blk_mq_req_expired() requires MQ_RQ_IN_FLIGHT) and blk_mq_queue_inflight()/nvme_cancel_tagset() would skip it. Use a separate helper for that path that only drops the reference. Fixes: 9f16a66733c9 ("block: mark flush request as IDLE when it is really finished") Signed-off-by: Ye Bin --- v2 -> v3: - Don't mark the flush request IDLE unconditionally in flush_end_io(): that path is also entered from blk_mq_put_rq_ref() for a request that may still be in flight, which would hide it from the timeout, cancel and inflight paths and reintroduce the bug fixed by 9f16a66733c9. - Split out flush_end_io_put_ref(); only the completion path now marks the request IDLE, so blk_mq_tagset_wait_completed_request() no longer spins when an iterator holds the last reference. - Add blk_flush_put_rq_ref() for the iterator path, which only drops the reference and leaves the request state untouched. - Correct the commit message. - Fix grammar, drop leading spaces, trim the call trace and shorten the source comment. block/blk-flush.c | 63 ++++++++++++++++++++++++++++++++++++----------- block/blk-mq.c | 2 +- block/blk.h | 1 + 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/block/blk-flush.c b/block/blk-flush.c index 38bd4ae5d9e7..ba6f3119b389 100644 --- a/block/blk-flush.c +++ b/block/blk-flush.c @@ -198,19 +198,18 @@ static void blk_flush_complete_seq(struct request *rq, blk_kick_flush(q, fq, cmd_flags); } -static enum rq_end_io_ret flush_end_io(struct request *flush_rq, - blk_status_t error, - const struct io_comp_batch *iob) +/* + * Drop a reference to a flush request and, if it was the last one, run the + * final cleanup. The caller must hold fq->mq_flush_lock. + */ +static enum rq_end_io_ret flush_end_io_put_ref(struct request *flush_rq, + blk_status_t error) { struct request_queue *q = flush_rq->q; struct list_head *running; struct request *rq, *n; - unsigned long flags = 0; struct blk_flush_queue *fq = blk_get_flush_queue(flush_rq->mq_ctx); - /* release the tag's ownership to the req cloned from */ - spin_lock_irqsave(&fq->mq_flush_lock, flags); - if (!req_ref_put_and_test(flush_rq)) { /* * Remember the status of the flush for whoever drops the last @@ -219,17 +218,10 @@ static enum rq_end_io_ret flush_end_io(struct request *flush_rq, */ if (error != BLK_STS_OK) fq->rq_status = error; - spin_unlock_irqrestore(&fq->mq_flush_lock, flags); return RQ_END_IO_NONE; } blk_account_io_flush(flush_rq); - /* - * Flush request has to be marked as IDLE when it is really ended - * because its .end_io() is called from timeout code path too for - * avoiding use-after-free. - */ - WRITE_ONCE(flush_rq->state, MQ_RQ_IDLE); if (fq->rq_status != BLK_STS_OK) { error = fq->rq_status; fq->rq_status = BLK_STS_OK; @@ -257,10 +249,51 @@ static enum rq_end_io_ret flush_end_io(struct request *flush_rq, blk_flush_complete_seq(rq, fq, seq, error); } - spin_unlock_irqrestore(&fq->mq_flush_lock, flags); return RQ_END_IO_NONE; } +static enum rq_end_io_ret flush_end_io(struct request *flush_rq, + blk_status_t error, + const struct io_comp_batch *iob) +{ + struct blk_flush_queue *fq = blk_get_flush_queue(flush_rq->mq_ctx); + enum rq_end_io_ret ret; + unsigned long flags; + + /* release the tag's ownership to the req cloned from */ + spin_lock_irqsave(&fq->mq_flush_lock, flags); + + /* + * Mark the request IDLE as soon as its completion is processed. If an + * iterator holds the last reference (e.g. the timeout handler that is + * running nvme_timeout()), leaving it in MQ_RQ_COMPLETE would make + * blk_mq_tagset_wait_completed_request() loop forever. Reference + * counting still prevents the request from being reused. + */ + WRITE_ONCE(flush_rq->state, MQ_RQ_IDLE); + + ret = flush_end_io_put_ref(flush_rq, error); + spin_unlock_irqrestore(&fq->mq_flush_lock, flags); + return ret; +} + +/* + * Called when a tag iterator drops its reference to a flush request. It must + * not change the request state: the iterator may be looking at a request that + * is still in flight. + */ +enum rq_end_io_ret blk_flush_put_rq_ref(struct request *flush_rq) +{ + struct blk_flush_queue *fq = blk_get_flush_queue(flush_rq->mq_ctx); + unsigned long flags; + enum rq_end_io_ret ret; + + spin_lock_irqsave(&fq->mq_flush_lock, flags); + ret = flush_end_io_put_ref(flush_rq, BLK_STS_OK); + spin_unlock_irqrestore(&fq->mq_flush_lock, flags); + return ret; +} + bool is_flush_rq(struct request *rq) { return rq->end_io == flush_end_io; diff --git a/block/blk-mq.c b/block/blk-mq.c index 5ae22d556540..1bb75de288c1 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -1688,7 +1688,7 @@ static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expi void blk_mq_put_rq_ref(struct request *rq) { if (is_flush_rq(rq)) { - if (rq->end_io(rq, 0, NULL) == RQ_END_IO_FREE) + if (blk_flush_put_rq_ref(rq) == RQ_END_IO_FREE) blk_mq_free_request(rq); } else if (req_ref_put_and_test(rq)) { __blk_mq_free_request(rq); diff --git a/block/blk.h b/block/blk.h index 0e38e4b88cf5..c190ecd9bd66 100644 --- a/block/blk.h +++ b/block/blk.h @@ -45,6 +45,7 @@ struct blk_flush_queue { }; bool is_flush_rq(struct request *req); +enum rq_end_io_ret blk_flush_put_rq_ref(struct request *req); struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size, gfp_t flags); -- 2.34.1