[PATCH] nvmet: fix ns enable/disable possible hang

Sagi Grimberg sagi at grimberg.me
Tue May 21 01:56:23 PDT 2024


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.

Signed-off-by: Sagi Grimberg <sagi at grimberg.me>
---
 drivers/nvme/target/configfs.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 6a736d28d767..b38097acc605 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -676,10 +676,12 @@ static ssize_t nvmet_ns_enable_store(struct config_item *item,
 	if (kstrtobool(page, &enable))
 		return -EINVAL;
 
+	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;
 }
-- 
2.40.1




More information about the Linux-nvme mailing list