[PATCH v2] nvme: fix double blk_mq_complete_request for timeout request with low probability
From: Lei Yin
Date: Thu Apr 06 2023 - 11:39:11 EST
When nvme_cancel_tagset traverses all tagsets and executes
nvme_cancel_request, this request may be executing blk_mq_free_request
that is called by nvme_rdma_complete_timed_out/nvme_tcp_complete_timed_out.
When blk_mq_free_request executes to WRITE_ONCE(rq->state, MQ_RQ_IDLE) and
__blk_mq_free_request(rq), it will cause double blk_mq_complete_request for
this request, and it will cause a null pointer error in the second
execution of this function because rq->mq_hctx has set to NULL in first
execution.
With multipath, by injecting a large number of requests timed out, I have
reproduced the issue that caused kernel crashes in three versions of the
kernel(include 5.10.167, 6.2.10 and upstream version which compiled by
myself). The error stack is as follows:
[ 2777.253091] <TASK>
[ 2777.253102] nvme_failover_req+0x10a/0x120 [nvme_core]
[ 2777.255302] blk_complete_reqs+0x3e/0x60
[ 2777.255726] __do_softirq+0xb6/0x2ad
[ 2777.256139] ? __pfx_smpboot_thread_fn+0x10/0x10
[ 2777.256556] run_ksoftirqd+0x28/0x40
[ 2777.256978] smpboot_thread_fn+0xdb/0x1d0
[ 2777.257399] kthread+0xd7/0x100
[ 2777.257827] ? __pfx_kthread+0x10/0x10
[ 2777.258253] ret_from_fork+0x29/0x50
[ 2777.258695] </TASK>
Signed-off-by: Lei Yin <yinlei2@xxxxxxxxxx>
---
drivers/nvme/host/core.c | 4 ++--
drivers/nvme/host/fabrics.h | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 53ef028596c6..c1cc384f4f3e 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -450,8 +450,8 @@ bool nvme_cancel_request(struct request *req, void *dat=
a)
dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
"Cancelling I/O %d", req->tag);
=20
- /* don't abort one completed request */
- if (blk_mq_request_completed(req))
+ /* don't abort one completed or idle request */
+ if (blk_mq_rq_state(req) !=3D MQ_RQ_IN_FLIGHT)
return true;
=20
nvme_req(req)->status =3D NVME_SC_HOST_ABORTED_CMD;
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index dcac3df8a5f7..a25a0118b722 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -197,7 +197,7 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctr=
l *ctrl)
=20
static inline void nvmf_complete_timed_out_request(struct request *rq)
{
- if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
+ if (blk_mq_rq_state(rq) =3D=3D MQ_RQ_IN_FLIGHT) {
nvme_req(rq)->status =3D NVME_SC_HOST_ABORTED_CMD;
blk_mq_complete_request(rq);
}
--=20
2.39.1