Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD
@ 2026-11-25 11:54 Bin Luo
  2026-11-25 12:15 ` sashiko-bot
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Bin Luo @ 2026-11-25 11:54 UTC (permalink / raw)
  To: linux-nvme; +Cc: nareshgottumukkala83, paul.ely, hch, sagi, kch, gregkh

nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks
queue->qlock and accesses the queue lists. Association teardown may already
have dropped the owner reference, so this put can free the queue before
completion accesses it.

Take a temporary queue reference before dropping the FOD reference. Hold it
through queue/list accesses and deferred-work submission.
Release it on both FOD-return and FOD-reuse paths. The deferred request's
lookup reference stays with the recycled FOD.

Fixes: 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")

Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference
injection. Unfixed baseline reports UAF; both patched runs have no KASAN.
Signed-off-by: Bin Luo <luob87709@gmail.com>
---
 drivers/nvme/target/fc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index 5d6c1c7..0b3a448 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 
 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
 
+	/* Hold queue across the FOD reference put and queue/list accesses. */
+	kref_get(&queue->ref);
+
 	/* release the queue lookup reference on the completed IO */
 	nvmet_fc_tgt_q_put(queue);
 
@@ -742,6 +745,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	if (!deferfcp) {
 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
 		spin_unlock_irqrestore(&queue->qlock, flags);
+		nvmet_fc_tgt_q_put(queue);
 		return;
 	}
 
@@ -774,6 +778,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	 */
 
 	queue_work(queue->work_q, &fod->defer_work);
+	nvmet_fc_tgt_q_put(queue);
 }
 
 static struct nvmet_fc_tgt_queue *
-- 
2.51.0.windows.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-25 12:15 UTC (permalink / raw)
  To: Bin Luo, linux-nvme

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvmet-fc: redundant back-to-back kref_get() and put in
  nvmet_fc_free_fcp_iod()
--

commit 6c7c03981acb44ae799c2343512f506b50503766
Author: Bin Luo <luob87709@gmail.com>

nvmet-fc: hold queue reference while freeing FCP IOD

This commit holds a reference on the target queue in
nvmet_fc_free_fcp_iod() until after queue list operations and deferred
work submission complete.

> diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
> index 5d6c1c79508af..0b3a44886c3df 100644
> --- a/drivers/nvme/target/fc.c
> +++ b/drivers/nvme/target/fc.c
> @@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
>  
>  	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
>  
> +	/* Hold queue across the FOD reference put and queue/list accesses. */
> +	kref_get(&queue->ref);
> +
>  	/* release the queue lookup reference on the completed IO */
>  	nvmet_fc_tgt_q_put(queue);
>  
> @@ -742,6 +745,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
>  	if (!deferfcp) {
>  		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
>  		spin_unlock_irqrestore(&queue->qlock, flags);
> +		nvmet_fc_tgt_q_put(queue);
>  		return;
>  	}
>  
> @@ -774,6 +778,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
>  	 */
>  
>  	queue_work(queue->work_q, &fod->defer_work);
> +	nvmet_fc_tgt_q_put(queue);
>  }

[Severity: Low]
Can nvmet_fc_free_fcp_iod() drop the early nvmet_fc_tgt_q_put(queue)
instead of pairing it with a kref_get(&queue->ref) right above it?

Since nvmet_fc_tgt_q_put() calls kref_put(&queue->ref, ...), taking
queue->ref and immediately putting it performs two back-to-back atomic
operations on queue->ref that cancel each other out on every FCP I/O
completion.  Removing the early nvmet_fc_tgt_q_put() and letting the two
new puts at the exit paths release the completing FOD's lookup reference
directly would avoid the extra atomic operations.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125115444.783-1-luob87709@gmail.com?part=1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD
  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 ` [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup Bin Luo
  3 siblings, 0 replies; 6+ messages in thread
From: Greg KH @ 2026-11-25 13:48 UTC (permalink / raw)
  To: Bin Luo; +Cc: linux-nvme, nareshgottumukkala83, paul.ely, hch, sagi, kch

On Wed, Nov 25, 2026 at 07:54:44PM +0800, Bin Luo wrote:
> nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks
> queue->qlock and accesses the queue lists. Association teardown may already
> have dropped the owner reference, so this put can free the queue before
> completion accesses it.
> 
> Take a temporary queue reference before dropping the FOD reference. Hold it
> through queue/list accesses and deferred-work submission.
> Release it on both FOD-return and FOD-reuse paths. The deferred request's
> lookup reference stays with the recycled FOD.
> 
> Fixes: 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")
> 
> Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference
> injection. Unfixed baseline reports UAF; both patched runs have no KASAN.

That's not a valid tag.

And no empty line between Fixes: and your signed-off-by.

thanks,

greg k-h


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD
  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 ` [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup Bin Luo
  3 siblings, 0 replies; 6+ messages in thread
From: Greg KH @ 2026-11-25 13:50 UTC (permalink / raw)
  To: Bin Luo; +Cc: linux-nvme, nareshgottumukkala83, paul.ely, hch, sagi, kch

On Wed, Nov 25, 2026 at 07:54:44PM +0800, Bin Luo wrote:
> nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks
> queue->qlock and accesses the queue lists. Association teardown may already
> have dropped the owner reference, so this put can free the queue before
> completion accesses it.
> 
> Take a temporary queue reference before dropping the FOD reference. Hold it
> through queue/list accesses and deferred-work submission.
> Release it on both FOD-return and FOD-reuse paths. The deferred request's
> lookup reference stays with the recycled FOD.
> 
> Fixes: 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")
> 
> Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference
> injection. Unfixed baseline reports UAF; both patched runs have no KASAN.
> Signed-off-by: Bin Luo <luob87709@gmail.com>
> ---
>  drivers/nvme/target/fc.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
> index 5d6c1c7..0b3a448 100644
> --- a/drivers/nvme/target/fc.c
> +++ b/drivers/nvme/target/fc.c
> @@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
>  
>  	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
>  
> +	/* Hold queue across the FOD reference put and queue/list accesses. */
> +	kref_get(&queue->ref);

This feels wrong, what is going to drop this "early"?  Why do you have 2
references now?  And there's no real function to call for this to match
up with nvmet_fc_tgt_q_put()?

Shouldn't you just fix the logic to NOT touch the queue after it could
have been dropped instead?

thanks,

greg k-h


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup
  2026-11-25 11:54 [PATCH] nvmet-fc: hold queue reference while freeing FCP IOD Bin Luo
                   ` (2 preceding siblings ...)
  2026-11-25 13:50 ` Greg KH
@ 2026-11-26  5:16 ` Bin Luo
  2026-11-26  8:42   ` Christoph Hellwig
  3 siblings, 1 reply; 6+ messages in thread
From: Bin Luo @ 2026-11-26  5:16 UTC (permalink / raw)
  To: linux-nvme; +Cc: nareshgottumukkala83, paul.ely, hch, sagi, kch, gregkh

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



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup
  2026-11-26  5:16 ` [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup Bin Luo
@ 2026-11-26  8:42   ` Christoph Hellwig
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2026-11-26  8:42 UTC (permalink / raw)
  To: Bin Luo; +Cc: linux-nvme, nareshgottumukkala83, paul.ely, hch, sagi, kch,
	gregkh

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

In the future, please send new or revised patches separated and not
as a reply in a thread, though.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-26  8:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2] nvmet-fc: put queue ref after FCP IOD cleanup Bin Luo
2026-11-26  8:42   ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox