[PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup

Bin Luo luob87709 at gmail.com
Wed Nov 25 21:16:24 PST 2026


In nvmet_fc_free_fcp_iod(), the completing IO's queue lookup reference
is dropped before locking queue->qlock, checking queue->pending_cmd_list,
returning or reusing the embedded fod, and queuing defer_work on
queue->work_q.

Because nvmet_req_complete() releases the nvme_sq percpu reference as
soon as queue_response() submits the asynchronous LLDD fcp_op() for
READDATA or RSP, association teardown in nvmet_fc_delete_target_queue()
can drop the queue owner reference before the LLDD completion reaches
nvmet_fc_free_fcp_iod(). In that case, dropping the completing IO's
reference early frees the queue (and its embedded fod array) before the
subsequent queue->qlock and list accesses, causing a use-after-free.

Move nvmet_fc_tgt_q_put(queue) to the end of nvmet_fc_free_fcp_iod() so
both the fod-return and deferred-IO reuse paths finish all queue and fod
accesses before dropping the completing IO's queue reference. On the
deferred-IO reuse path, the deferred request's own lookup reference
taken in nvmet_fc_rcv_fcp_req() remains with the recycled fod.

Fixes: 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")
Signed-off-by: Bin Luo <luob87709 at gmail.com>
---
Changes in v2:
- Drop the redundant kref_get()/nvmet_fc_tgt_q_put() pair and instead
  move the existing nvmet_fc_tgt_q_put(queue) to the exit path after all
  queue and fod accesses complete (Greg KH, Sashiko).
- Update the stale comment above queue_work() on the deferred-IO reuse
  path to clarify which queue reference remains with the recycled fod.
- Remove the invalid Tested: trailer and fix the Fixes:/Signed-off-by:
  trailer block formatting (Greg KH).
- Re-tested on v7.4-rc1 x86_64 KASAN_GENERIC QEMU with the owner-drop
  regression harness: both injected and control runs completed cleanly
  with no KASAN reports.

 drivers/nvme/target/fc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index 5d6c1c7..15f66ae 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -733,16 +733,13 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 
 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
 
-	/* release the queue lookup reference on the completed IO */
-	nvmet_fc_tgt_q_put(queue);
-
 	spin_lock_irqsave(&queue->qlock, flags);
 	deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
 				struct nvmet_fc_defer_fcp_req, req_list);
 	if (!deferfcp) {
 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
 		spin_unlock_irqrestore(&queue->qlock, flags);
-		return;
+		goto out_put_queue;
 	}
 
 	/* Re-use the fod for the next pending cmd that was deferred */
@@ -769,11 +766,14 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	tgtport->ops->defer_rcv(&tgtport->fc_target_port, fcpreq);
 
 	/*
-	 * Leave the queue lookup get reference taken when
-	 * fod was originally allocated.
+	 * Leave the queue lookup reference taken when the deferred IO was
+	 * received for the recycled fod.
 	 */
-
 	queue_work(queue->work_q, &fod->defer_work);
+
+out_put_queue:
+	/* release the queue lookup reference on the completed IO */
+	nvmet_fc_tgt_q_put(queue);
 }
 
 static struct nvmet_fc_tgt_queue *
-- 
2.51.0.windows.1




More information about the Linux-nvme mailing list