All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hannes Reinecke <hare@kernel.org>
To: Christoph Hellwig <hch@lst.de>
Cc: Keith Busch <kbusch@kernel.org>, Sagi Grimberg <sagi@grimberg.me>,
	linux-nvme@lists.infradead.org, Hannes Reinecke <hare@kernel.org>
Subject: [PATCH] nvme: Allow sending of admin commands to inaccessible paths
Date: Fri, 30 May 2025 14:01:42 +0200	[thread overview]
Message-ID: <20250530120142.130469-1-hare@kernel.org> (raw)

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@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



             reply	other threads:[~2025-05-30 12:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-30 12:01 Hannes Reinecke [this message]
2025-06-04  8:07 ` [PATCH] nvme: Allow sending of admin commands to inaccessible paths Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250530120142.130469-1-hare@kernel.org \
    --to=hare@kernel.org \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.