Skip to contentSkip to conversation

nvmet-fc: skip port entry rebinding if already bound to a target port

PATCH Superseded Nguyen Duy Nhat Anh 3 participants 4 messages Oct 16

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvmet-fc: skip port entry rebinding if already bound to a target port

In nvmet_fc_portentry_rebind_tgt(), nvmet_fc_tgtlock serializes access to nvmet_fc_portentry_list, but does not prevent multiple concurrent or duplicate target port registrations from attempting to rebind to an already-assigned port entry.

When pe->tgtport is non-NULL, WARN_ON() is triggered and pe->tgtport is overwritten, leading to kernel warnings and leaking the reference count acquired by nvmet_fc_tgtport_get().

Fix this by checking if pe->tgtport is already set before acquiring a reference count and rebinding. If it is bound, skip the entry to prevent clobbering existing bindings.

---
v1 -> v2:
- Move pe->tgtport check before nvmet_fc_tgtport_get() to avoid refcount leak.

---
 drivers/nvme/target/fc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/target/fc.c +3 -1
index 1b557775e033..9b7fd8f5bd47 100644
@@ -1324,7 +1324,9 @@ nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
 			if (!nvmet_fc_tgtport_get(tgtport))
 				continue;
 
-			WARN_ON(pe->tgtport);
+			if (pe->tgtport)
+				continue;
+
 			tgtport->pe = pe;
 			pe->tgtport = tgtport;
 			break;
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] nvme-fc: refcount leak of tgtport in nvmet_fc_portentry_rebind_tgt() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] nvme-fc: refcount leak of tgtport in nvmet_fc_portentry_rebind_tgt() --

commit 3bf5702b60af10d1adf6131dcac2e5211f8bdb47
Author: Nguyen Duy Nhat Anh <[email protected]>

nvmet-fc: skip port entry rebinding if already bound to a target port

In nvmet_fc_portentry_rebind_tgt(), nvmet_fc_tgtlock serializes access to nvmet_fc_portentry_list, but does not prevent multiple concurrent or duplicate target port registrations from attempting to rebind to an already-assigned port entry. This patch skips the entry if it is already bound to prevent clobbering existing bindings.

drivers/nvme/target/fc.c nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport) · line 1329

Quoted 12 lines
drivers/nvme/target/fc.c +3 -1
index eecdffefa24fa..ac3194497a1e0 100644
@@ -1324,7 +1324,9 @@ nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
 			if (!nvmet_fc_tgtport_get(tgtport))
 				continue;
 
-			WARN_ON(pe->tgtport);
+			if (pe->tgtport)
+				continue;
+

[Severity: Medium]
Does this code leak the target port reference?

The commit message states "Fix this by checking if pe->tgtport is already set before acquiring a reference count", but the check is placed immediately after nvmet_fc_tgtport_get() rather than before it.

If pe->tgtport is already bound, the continue statement will skip the rest of the loop, leaving the reference count acquired by nvmet_fc_tgtport_get() permanently elevated.

Could the check be moved before nvmet_fc_tgtport_get() as intended?

Quoted 3 lines
drivers/nvme/target/fc.c +0 -0
 			tgtport->pe = pe;
 			pe->tgtport = tgtport;
 			break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Nguyen Duy Nhat Anh
Quoted 2 lines · Nguyen Duy Nhat Anh

v1 -> v2:
- Move pe->tgtport check before nvmet_fc_tgtport_get() to avoid refcount leak.

You may want to double check before posting new versions because this v2 is identical to v1.

#3 lore raw

Sorry about the noise in the recent patches—I botched staging the diff before amending. v3 has the actual diff moving the check above nvmet_fc_tgtport_get().

Regards,
Nhat Anh

Quoted 7 lines · Keith Busch
Quoted 2 lines · Nguyen Duy Nhat Anh

v1 -> v2:
- Move pe->tgtport check before nvmet_fc_tgtport_get() to avoid refcount leak.

You may want to double check before posting new versions because this v2 is identical to v1.