Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bin Luo <luob87709@gmail.com>
To: linux-nvme@lists.infradead.org
Cc: nareshgottumukkala83@gmail.com, paul.ely@broadcom.com,
	hch@lst.de, sagi@grimberg.me, kch@nvidia.com,
	gregkh@linuxfoundation.org
Subject: [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup
Date: Thu, 26 Nov 2026 13:16:24 +0800	[thread overview]
Message-ID: <20261126051624.601-1-luob87709@gmail.com> (raw)
In-Reply-To: <20261125115444.783-1-luob87709@gmail.com>

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@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



  parent reply	other threads:[~2026-11-26  5:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 11:54 [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD Bin Luo
2026-11-25 12:15 ` sashiko-bot
2026-11-25 13:48 ` Greg KH
2026-11-25 13:50 ` Greg KH
2026-11-26  5:16 ` Bin Luo [this message]
2026-11-26  8:42   ` [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126051624.601-1-luob87709@gmail.com \
    --to=luob87709@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=kch@nvidia.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=nareshgottumukkala83@gmail.com \
    --cc=paul.ely@broadcom.com \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox