[PATCH 1/2] nvmet: defer setting ns->enabled to false in nvmet_ns_disable()
From: Nilay Shroff
Date: Wed Sep 16 2026 - 11:17:18 EST
nvmet_ns_disable() currently clears ns->enabled before draining
in-flight I/O references. This allows namespace configuration to be
changed while existing I/O requests can still hold a reference to the
namespace.
This can race with configuration of namespace attributes such as the
device path, UUID, NGUID etc. These attributes can be accessed by I/O
requests without holding subsys->lock and must not be modified while
such requests are still using the namespace.
In nvmet_ns_disable(), keep ns->enabled set while existing namespace
references are being drained, so namespace configuration remains blocked
until all in-flight I/O has completed. Set ns->enabled to false only
after the namespace references have been drained and the namespace
device has been disabled.
Since setting ns->enabled to false is deferred in nvmet_ns_disable(),
use the NVMET_NS_ENABLED XArray mark in nvmet_req_find_ns() to
determine whether a namespace can accept new I/O.
Similarly, use the NVMET_NS_ENABLED XArray mark in nvmet_ns_disable()
to prevent concurrent callers from starting namespace disable.
Signed-off-by: Nilay Shroff <nilay@xxxxxxxxxxxxx>
---
drivers/nvme/target/core.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 43871a8f56ca..77d113fceced 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -448,7 +448,8 @@ u16 nvmet_req_find_ns(struct nvmet_req *req)
struct nvmet_subsys *subsys = nvmet_req_subsys(req);
req->ns = xa_load(&subsys->namespaces, nsid);
- if (unlikely(!req->ns || !req->ns->enabled)) {
+ if (unlikely(!req->ns) ||
+ !xa_get_mark(&subsys->namespaces, nsid, NVMET_NS_ENABLED)) {
req->error_loc = offsetof(struct nvme_common_command, nsid);
if (!req->ns) /* ns doesn't exist! */
return NVME_SC_INVALID_NS | NVME_STATUS_DNR;
@@ -644,10 +645,9 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
struct nvmet_ctrl *ctrl;
mutex_lock(&subsys->lock);
- if (!ns->enabled)
+ if (!xa_get_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED))
goto out_unlock;
- ns->enabled = false;
xa_clear_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED);
nvmet_debugfs_ns_free(ns);
@@ -675,6 +675,7 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
mutex_lock(&subsys->lock);
nvmet_ns_changed(subsys, ns->nsid);
nvmet_ns_dev_disable(ns);
+ ns->enabled = false;
out_unlock:
mutex_unlock(&subsys->lock);
}
--
2.53.0