[PATCH] nvme: Allow sending of admin commands to inaccessible paths

Hannes Reinecke hare at kernel.org
Fri May 30 05:01:42 PDT 2025


The NVMe Base Specification (Section 8.1.1.10) specifies that admin
commands should not be affected by ANA states. So implement a function
nvme_find_admin_path() to return the first non-disabled path irrespective
of the ANA state.

Signed-off-by: Hannes Reinecke <hare at kernel.org>
---
 drivers/nvme/host/ioctl.c     | 33 +++++++++++++++++++------------
 drivers/nvme/host/multipath.c | 37 +++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h      |  1 +
 3 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/drivers/nvme/host/ioctl.c b/drivers/nvme/host/ioctl.c
index 5d5f8b07cdec..1cf66d8b2d7f 100644
--- a/drivers/nvme/host/ioctl.c
+++ b/drivers/nvme/host/ioctl.c
@@ -723,19 +723,22 @@ int nvme_ns_head_ioctl(struct block_device *bdev, blk_mode_t mode,
 		flags |= NVME_IOCTL_PARTITION;
 
 	srcu_idx = srcu_read_lock(&head->srcu);
+	if (is_ctrl_ioctl(cmd)) {
+		ns = nvme_find_admin_path(head);
+		if (!ns)
+			goto out_unlock;
+		/*
+		 * Handle ioctls that apply to the controller instead of the namespace
+		 * seperately and drop the ns SRCU reference early.  This avoids a
+		 * deadlock when deleting namespaces using the passthrough interface.
+		 */
+		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
+					       open_for_write);
+	}
 	ns = nvme_find_path(head);
 	if (!ns)
 		goto out_unlock;
 
-	/*
-	 * Handle ioctls that apply to the controller instead of the namespace
-	 * seperately and drop the ns SRCU reference early.  This avoids a
-	 * deadlock when deleting namespaces using the passthrough interface.
-	 */
-	if (is_ctrl_ioctl(cmd))
-		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
-					       open_for_write);
-
 	ret = nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
 out_unlock:
 	srcu_read_unlock(&head->srcu, srcu_idx);
@@ -754,13 +757,17 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
 	int srcu_idx, ret = -EWOULDBLOCK;
 
 	srcu_idx = srcu_read_lock(&head->srcu);
-	ns = nvme_find_path(head);
-	if (!ns)
-		goto out_unlock;
+	if (is_ctrl_ioctl(cmd)) {
+		ns = nvme_find_admin_path(head);
+		if (!ns)
+			goto out_unlock;
 
-	if (is_ctrl_ioctl(cmd))
 		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
 				open_for_write);
+	}
+	ns = nvme_find_path(head);
+	if (!ns)
+		goto out_unlock;
 
 	ret = nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
 out_unlock:
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 3fdbbe1fdbc4..d225c518a32c 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -473,6 +473,43 @@ inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
 	}
 }
 
+inline struct nvme_ns *nvme_find_admin_path(struct nvme_ns_head *head)
+{
+	struct nvme_ns *found = NULL, *fallback = NULL, *ns;
+
+	ns = srcu_dereference(head->current_path[numa_node_id()], &head->srcu);
+	if (ns && !nvme_path_is_disabled(ns))
+		return ns;
+
+	/*
+	 * Return the first optimized path or, failing that, the first non-
+	 * disabled path. Processing rules for admin commands are different
+	 * than those for I/O commands (NVMe Base Spec v2.2 section 8.1.1.10
+	 * "Asymmetric Namespace Access States Command Processing Effects")
+	 * so do not set the current path.
+	 */
+	list_for_each_entry_srcu(ns, &head->list, siblings,
+				 srcu_read_lock_held(&head->srcu)) {
+		if (nvme_path_is_disabled(ns))
+			continue;
+
+		switch (ns->ana_state) {
+		case NVME_ANA_OPTIMIZED:
+			return ns;
+		case NVME_ANA_NONOPTIMIZED:
+			found = ns;
+			break;
+		default:
+			fallback = ns;
+			break;
+		}
+	}
+
+	if (!found)
+		found = fallback;
+	return found;
+}
+
 static bool nvme_available_path(struct nvme_ns_head *head)
 {
 	struct nvme_ns *ns;
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 45beb701011b..1d236a7ba757 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -952,6 +952,7 @@ extern const struct block_device_operations nvme_bdev_ops;
 
 void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl);
 struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
+struct nvme_ns *nvme_find_admin_path(struct nvme_ns_head *head);
 #ifdef CONFIG_NVME_MULTIPATH
 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
 {
-- 
2.35.3




More information about the Linux-nvme mailing list