ublk: fix dead loop when canceling io command
Subsystems (2)
PATCH 1/1 ublk: fix dead loop when canceling io command
Commit f40139fde527 ("ublk: fix race between io_uring_cmd_complete_in_task and ublk_cancel_cmd") adds request state check in ublk_cancel_cmd(), and if the request is
started, skip canceling this uring_cmd.
However, the current uring_cmd may be in ACTIVE state, without block request coming to the uring command. Meantime, the cached request in tag_set.tags[tag] is recycled and has been delivered to ublk server, then this uring_cmd can't be canceled.
ublk requests are aborted in ublk char device release handler, which depends on canceling all ACTIVE uring_cmd. So cause dead loop.
Fix this issue by not taking stale request into account when canceling uring_cmd in ublk_cancel_cmd().
--- drivers/block/ublk_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/ublk_drv.c +1 -1
@@ -1708,7 +1708,7 @@ static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag,* that ublk_dispatch_req() is always called */ req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); -if (req &&blk_mq_request_started (req)) +if (req &&blk_mq_request_started (req) && req->tag == tag)return ;spin_lock (&ubq->cancel_lock);
Signature · 1 line
2.47.1
Quoted 10 lines · Ming Lei
Commit
f40139fde527("ublk: fix race betweenio_uring_cmd_complete_in_taskandublk_cancel_cmd") adds request state check inublk_cancel_cmd(), and if the request is
started, skip canceling thisuring_cmd.However, the current
uring_cmdmay be in ACTIVE state, without block request coming to the uring command. Meantime, the cached request in tag_set.tags[tag] is recycled and has been delivered to ublk server, then thisuring_cmdcan't be canceled.[...]
Applied, thanks!
[1/1] ublk: fix dead loop when canceling io command
commit: dd24f87f65c957f30e605e44961d2fd53a44c780
Best regards,
Signature · 1 line
Jens Axboe
Quoted 9 lines · Ming Lei
Commit
f40139fde527("ublk: fix race betweenio_uring_cmd_complete_in_taskandublk_cancel_cmd") adds request state check inublk_cancel_cmd(), and if the request is
started, skip canceling thisuring_cmd.However, the current
uring_cmdmay be in ACTIVE state, without block request coming to the uring command. Meantime, the cached request in tag_set.tags[tag] is recycled and has been delivered to ublk server, then thisuring_cmdcan't be canceled.
To check my understanding, the scenario you're describing is that the request has started but also has already been completed by the ublk server? And this can happen because tag_set.tags[q_id]->rqs[tag] still points to the old request even after it has completed? Reading through blk-mq.c, that does seem possible since rqs[tag] is set in blk_mq_start_request() but not cleared in __blk_mq_end_request().
Quoted 3 lines
ublk requests are aborted in ublk char device release handler, which depends on canceling all ACTIVE
uring_cmd. So cause dead loop.
Do you mean "deadlock"?
Quoted 17 lines
Fix this issue by not taking stale request into account when canceling
uring_cmdinublk_cancel_cmd().Reported-by Shinichiro Kawasaki <[email protected]>Closes https://lore.kernel.org/linux-block/mruqwpf4tqenkbtgezv5oxwq7ngyq24jzeyqy4ixzvivatbbxv@4oh2wzz4e6qn/ open hereFixes f40139fde527 ("ublk: fix race between io_uring_cmd_complete_in_task and ublk_cancel_cmd")Signed-off-by Ming Lei <[email protected]>--- drivers/block/ublk_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/ublk_drv.c +0
-0
drivers/block/ublk_drv.c static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag, · line 1711
Quoted 5 lines
drivers/block/ublk_drv.c +1
-1@@ -1708,7 +1708,7 @@ static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag,* that ublk_dispatch_req() is always called */ req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); -if (req &&blk_mq_request_started (req)) +if (req &&blk_mq_request_started (req) && req->tag == tag)
Is it possible that req now belongs to a different hctx (q_id)? From a quick reading of blk-mq.c, it looks like the hctx's requests are always allocated from the hctx's static_rqs, so I don't think that should be a problem. Reading req->tag here is probably racy though, as it's written by blk_mq_rq_ctx_init(), called from *blk_mq_alloc_request*() on the submitting task. How about checking blk_mq_rq_state(req) == MQ_RQ_IN_FLIGHT instead of blk_mq_request_started(req) && req->tag == tag?
Best,
Caleb
Quoted 6 lines
drivers/block/ublk_drv.c +0
-1return ;spin_lock (&ubq->cancel_lock); --2.47.1
Quoted 48 lines · Caleb Sander Mateos
Quoted 9 lines · Ming Lei
Commit
f40139fde527("ublk: fix race betweenio_uring_cmd_complete_in_taskandublk_cancel_cmd") adds request state check inublk_cancel_cmd(), and if the request is
started, skip canceling thisuring_cmd.However, the current
uring_cmdmay be in ACTIVE state, without block request coming to the uring command. Meantime, the cached request in tag_set.tags[tag] is recycled and has been delivered to ublk server, then thisuring_cmdcan't be canceled.To check my understanding, the scenario you're describing is that the request has started but also has already been completed by the ublk server? And this can happen because tag_set.tags[q_id]->rqs[tag] still points to the old request even after it has completed? Reading through
blk-mq.c, that does seem possible since rqs[tag] is set inblk_mq_start_request()but not cleared in__blk_mq_end_request().Quoted 3 lines
ublk requests are aborted in ublk char device release handler, which depends on canceling all ACTIVE
uring_cmd. So cause dead loop.Do you mean "deadlock"?
Quoted 17 lines
Fix this issue by not taking stale request into account when canceling
uring_cmdinublk_cancel_cmd().Reported-by Shinichiro Kawasaki <[email protected]>Closes https://lore.kernel.org/linux-block/mruqwpf4tqenkbtgezv5oxwq7ngyq24jzeyqy4ixzvivatbbxv@4oh2wzz4e6qn/ open hereFixes f40139fde527 ("ublk: fix race between io_uring_cmd_complete_in_task and ublk_cancel_cmd")Signed-off-by Ming Lei <[email protected]>--- drivers/block/ublk_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/ublk_drv.c +0
-0drivers/block/ublk_drv.c static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag, · line 1711
Quoted 5 lines
drivers/block/ublk_drv.c +1
-1@@ -1708,7 +1708,7 @@ static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag,* that ublk_dispatch_req() is always called */ req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); -if (req &&blk_mq_request_started (req)) +if (req &&blk_mq_request_started (req) && req->tag == tag)
Quoted 8 lines
Is it possible that req now belongs to a different hctx (
q_id)? From a quick reading ofblk-mq.c, it looks like the hctx's requests are always allocated from the hctx'sstatic_rqs, so I don't think that should be a problem. Readingreq->taghere is probably racy though, as it's written byblk_mq_rq_ctx_init(), called from *blk_mq_alloc_request*() on the submitting task. How about checkingblk_mq_rq_state(req)==MQ_RQ_IN_FLIGHTinstead ofblk_mq_request_started(req)&&req->tag== tag?
Ah, never mind, I see what you mean. The struct request could have already been reused for a new I/O to the ublk queue and associated with a different tag. And that new request could be inflight even though the old one has completed. Really this condition intends to ask "is this tag active?" I guess you could use sbitmap_test_bit(&tag_set.tags[q_id].bitmap_tags, tag) (exposed through a blk-mq helper) to determine that more directly. But the quick fix probably makes sense. It's technically racy, as the load and store of req->state need to use acquire and release ordering to synchronize the accesses to req->tag, but it's probably unlikely to observe that reordering in practice.
Best,
Caleb
Quoted 12 lines · Ming Lei
Commit
f40139fde527("ublk: fix race betweenio_uring_cmd_complete_in_taskandublk_cancel_cmd") adds request state check inublk_cancel_cmd(), and if the request is
started, skip canceling thisuring_cmd.However, the current
uring_cmdmay be in ACTIVE state, without block request coming to the uring command. Meantime, the cached request in tag_set.tags[tag] is recycled and has been delivered to ublk server, then thisuring_cmdcan't be canceled.ublk requests are aborted in ublk char device release handler, which depends on canceling all ACTIVE
uring_cmd. So cause dead loop.
Quoted 7 lines
Fix this issue by not taking stale request into account when canceling
uring_cmdinublk_cancel_cmd().Reported-by Shinichiro Kawasaki <[email protected]>Closes https://lore.kernel.org/linux-block/mruqwpf4tqenkbtgezv5oxwq7ngyq24jzeyqy4ixzvivatbbxv@4oh2wzz4e6qn/ open hereFixes f40139fde527 ("ublk: fix race between io_uring_cmd_complete_in_task and ublk_cancel_cmd")Signed-off-by Ming Lei <[email protected]>
FYI, I confirmed that this patch avoids the hang that I reported in the link of the Closed tag. Thanks!