[PATCH v2 3/5] nvmet: complete the passthru request after applying command effects

Guixin Liu kanie at linux.alibaba.com
Wed Dec 2 03:58:54 PST 2026


nvmet_passthru_execute_cmd_work() completes the target request before
applying the command effects. With the loop transport the nvmet_req is
embedded in the host request's pdu, and completing it lets the submitter
free that request, so the subsequent nvme_passthru_end() read of
req->cmd is a use-after-free. Completing early also drops the passthru
reference that keeps the backend controller alive across
nvme_passthru_end().

Apply the effects before completing the target request. The backend
request is still freed first, so it is idle before nvme_passthru_end()
flushes scan_work and the reset deadlock does not return.

Fixes: bc8fb906b0ff9 ("nvme: handle effects after freeing the request")
Cc: stable at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at linux.alibaba.com>
---

Reproduced in a QEMU/KVM guest with nvme-loop and nvmet passthru to a
QEMU nvme backend. Identify Controller takes the passthru work path
(use_workqueue) with non-zero command effects, so nvme_passthru_end()
runs after nvmet_req_complete(). A TEST-ONLY msleep after completion
(not part of this patch) widens the window.

Address tracking shows the host loop request, whose pdu embeds the
nvmet_req, is freed by the submitter ~0.6ms after the completion wakes
it, while the work reads req->cmd ~100ms later (stale opcode returned).
This is a use-after-free. KASAN does not print a splat for this access
in the test kernel (a known module-side detection gap, also seen
elsewhere); the free/read ordering of the same object is the evidence.

Patched: the effects are applied and req->cmd is read before
nvmet_req_complete(), so the request is still alive; the backend
request is freed first to keep the reset deadlock fix intact.

Regression: identify and I/O over loop passthru work, and eight rounds
of backend controller reset concurrent with passthru identify complete
without deadlock or use-after-free.

Changes since v1:
- Apply the effects before completing the target request instead of
  snapshotting req->cmd onto the stack. This also keeps the passthru
  reference alive across nvme_passthru_end(), avoiding a use-after-free
  on the backend controller (Sashiko).
 drivers/nvme/target/passthru.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
index 2d1aeb84637c..26d16f4e0df2 100644
--- a/drivers/nvme/target/passthru.c
+++ b/drivers/nvme/target/passthru.c
@@ -253,11 +253,19 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
 		status = NVME_SC_INTERNAL;
 
 	req->cqe->result = nvme_req(rq)->result;
-	nvmet_req_complete(req, status);
 	blk_mq_free_request(rq);
 
+	/*
+	 * Apply the command effects before completing the target request:
+	 * nvmet_req_complete() may free req (the loop transport embeds it in
+	 * the host request pdu) and drops the passthru reference keeping ctrl
+	 * alive. Free the backend request first so it is idle before
+	 * nvme_passthru_end() flushes scan_work, or a concurrent reset of ctrl
+	 * deadlocks.
+	 */
 	if (effects)
 		nvme_passthru_end(ctrl, ns, effects, req->cmd, status);
+	nvmet_req_complete(req, status);
 }
 
 static enum rq_end_io_ret nvmet_passthru_req_done(struct request *rq,
-- 
2.43.7




More information about the Linux-nvme mailing list