Skip to contentSkip to conversation

nvmet: fix ns enable/disable possible hang

PATCH Applied Sagi Grimberg 4 participants 4 messages May 21, 2024 – May 22, 2024

Subsystems (2)
lore
Share & tools
lore

Sagi Grimberg

author maintainer
#0 lore raw

PATCH v2 1/1 nvmet: fix ns enable/disable possible hang

When disabling an nvmet namespace, there is a period where the subsys->lock is released, as the ns disable waits for backend IO to complete, and the ns percpu ref to be properly killed. The original intent was to avoid taking the subsystem lock for a prolong period as other processes may need to acquire it (for example new incoming connections).

However, it opens up a window where another process may come in and enable the ns, (re)intiailizing the ns percpu_ref, causing the disable sequence to hang.

Solve this by taking the global nvmet_config_sem over the entire configfs enable/disable sequence.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

---
Changes from v1:
- Added Fixes tag for stable
- edited changelog to 73 chars
- added code comment explaining the reasoning of taking the global sem

 drivers/nvme/target/configfs.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/nvme/target/configfs.c +8 -0
index 6a736d28d767..c90d5b37bb4b 100644
@@ -676,10 +676,18 @@ static ssize_t nvmet_ns_enable_store(struct config_item *item,
 	if (kstrtobool(page, &enable))
 		return -EINVAL;
 
+	/*
+	 * take a global nvmet_config_sem because the disable routine has a
+	 * window where it releases the subsys-lock, giving a chance to
+	 * a parallel enable to concurrently execute causing the disable to
+	 * have a misaccounting of the ns percpu_ref.
+	 */
+	down_write(&nvmet_config_sem);
 	if (enable)
 		ret = nvmet_ns_enable(ns);
 	else
 		nvmet_ns_disable(ns);
+	up_write(&nvmet_config_sem);
 
 	return ret ? ret : count;
 }
Signature · 1 line
2.40.1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Sagi Grimberg
Quoted 11 lines · Sagi Grimberg

When disabling an nvmet namespace, there is a period where the subsys->lock is released, as the ns disable waits for backend IO to complete, and the ns percpu ref to be properly killed. The original intent was to avoid taking the subsystem lock for a prolong period as other processes may need to acquire it (for example new incoming connections).

However, it opens up a window where another process may come in and enable the ns, (re)intiailizing the ns percpu_ref, causing the disable sequence to hang.

Quoted 5 lines

Solve this by taking the global nvmet_config_sem over the entire configfs enable/disable sequence.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

-ck

maintainer Applied
#3 lore raw
In reply to Sagi Grimberg
Quoted 7 lines · Sagi Grimberg

When disabling an nvmet namespace, there is a period where the subsys->lock is released, as the ns disable waits for backend IO to complete, and the ns percpu ref to be properly killed. The original intent was to avoid taking the subsystem lock for a prolong period as other processes may need to acquire it (for example new incoming connections).

Quoted 6 lines

However, it opens up a window where another process may come in and enable the ns, (re)intiailizing the ns percpu_ref, causing the disable sequence to hang.

Solve this by taking the global nvmet_config_sem over the entire configfs enable/disable sequence.

Thanks, applied to nvme-6.10.