Skip to contentSkip to conversation

nvmet-fc: extend critical section in nvmet_fc_remove_port()

PATCH In review [email protected] 2 participants 3 messages Oct 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-fc: extend critical section in nvmet_fc_remove_port()

From: Paritosh Potukuchi <[email protected]>

Currently in nvmet_fc_remove_port the critical section starts from the call to spin_lock_irqsave(&nvmet_fc_tgtlock, flags) and ends before the call to nvmet_fc_portentry_unbind(pe).

nvmet_fc_portentry_unbind takes a spin_lock_irqsave on nvmet_fc_tgtlock again, to execute its critical section. The problem here is that, releasing the lock before calling nvmet_fc_portentry_unbind, and then retaking the lock inside it, creates room for other threads to possibly enter the critical section.

The solution proposed is to extend the critical section inside nvmet_fc_remove_port, till the point where tgtport is being used.

---
 drivers/nvme/target/fc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/nvme/target/fc.c +2 -5
index 1b557775e033..96d8aafb9f56 100644
@@ -1271,15 +1271,11 @@ nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
 static void
 nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
 {
-	unsigned long flags;
-
-	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
 	if (pe->tgtport) {
 		nvmet_fc_tgtport_put(pe->tgtport);
 		pe->tgtport->pe = NULL;
 	}
 	list_del(&pe->pe_list);
-	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
 }
 
 /*
@@ -2922,7 +2918,6 @@ nvmet_fc_remove_port(struct nvmet_port *port)
 	spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
 	if (pe->tgtport && nvmet_fc_tgtport_get(pe->tgtport))
 		tgtport = pe->tgtport;
-	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
 
 	nvmet_fc_portentry_unbind(pe);
 
@@ -2932,6 +2927,8 @@ nvmet_fc_remove_port(struct nvmet_port *port)
 		nvmet_fc_tgtport_put(tgtport);
 	}
 
+	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
 	kfree(pe);
 }
 
Signature · 1 line
2.55.0
#1 lore raw

drivers/nvme/target/fc.c nvmet_fc_remove_port(struct nvmet_port *port) · line 2934

Quoted 9 lines · paritoshpotukuchi
drivers/nvme/target/fc.c +2 -0
@@ -2932,6 +2927,8 @@ nvmet_fc_remove_port(struct nvmet_port *port)
 		nvmet_fc_tgtport_put(tgtport);
 	}
 
+	spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
+
 	kfree(pe);
 }
 

I don't think this safe.
nvmet_fc_tgtport_put() can end up calling fcloop_targetport_delete() which calls flush_work(), therefore you call a blocking function while in atomic context

Maurizio

#2 lore raw
Quoted 4 lines

I don't think this safe.
nvmet_fc_tgtport_put() can end up calling fcloop_targetport_delete() which calls flush_work(), therefore you call a blocking function while in atomic context

Thanks for pointing that out.

The current implementation of nvmet_fc_portentry_unbind calls nvmet_fc_tgtport_put() while holding a spinlock.

From what we understand, this seems to be a design flaw, since flush_work() is still being called while holding a spinlock.

Paritosh