Skip to contentSkip to conversation

nvme: switch to libmultipath

PATCH In review John Garry 2 participants 20 messages Jul 3

Subsystems (3)
lore
Share & tools
lore

John Garry

author
#0 lore raw

v3 cover nvme: switch to libmultipath

This switches the NVMe host driver to use libmultipath. That library is very heavily based on the NVMe multipath code, so the change over should hopefully be straightforward. There is often a direct replacement for functions.

The multipath functionality in nvme_ns_head and nvme_ns structures are replaced with the mpath_head and mpath_device structures.

It's hard to switch to libmulipath in a step-by-step fashion without breaking builds or functionality. To make the series reviewable, I took the approach of adding libmultipath-based code, which would initially be unused, and then finally making the full switch.

I think that more testing is required here and any help on that would be appreciated.

The series is based on v7.2-rc1 and libmultipath v3.

Full series also available at
https://github.com/johnpgarry/linux/tree/scsi-multipath-v7.2-v3

Differences to v2 (apart from porting changes for v3 libmultipath):
- rebase

Differences to v1 (apart from porting changes for v2 libmultipath):
- always depend on LIBMULTIAPTH and drop nvme_ns_head.ns_count
- add nvme_add_ns() and nvme_delete_ns()
- init .drv_module (Nilay)
- condense code

John Garry (10):
nvme-multipath: add initial support for using libmultipath
nvme-multipath: add nvme_mpath_available_path()
nvme-multipath: add nvme_mpath_{add, remove}_cdev()
nvme-multipath: add nvme_mpath_is_{disabled, optimised}
nvme-multipath: add nvme_mpath_cdev_ioctl()
nvme-multipath: add uring_cmd support
nvme-multipath: add nvme_mpath_synchronize()
nvme-multipath: add nvme_{add,delete}_ns()
nvme-multipath: add nvme_mpath_head_queue_if_no_path()
nvme-multipath: switch to use libmultipath

 drivers/nvme/host/Kconfig     |   1 +
 drivers/nvme/host/core.c      |  89 ++--
 drivers/nvme/host/ioctl.c     | 110 ++--
 drivers/nvme/host/multipath.c | 928 +++++++---------------------------
 drivers/nvme/host/nvme.h      | 135 +++--
 drivers/nvme/host/pr.c        |  18 -
 drivers/nvme/host/sysfs.c     |  89 +---
 7 files changed, 339 insertions(+), 1031 deletions(-)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v3 1/10 nvme-multipath: add initial support for using libmultipath

Add initial support, as follows:
- Add mpath_head_template
- Add mpath_device in nvme_ns
- Add mpath_disk pointer to head structure

Initially all the functionality which mpath_head_template points to will be unused, until the driver fully switches to libmultipath. Otherwise it's hard to do so in a step-wise fashion without breaking functionality.

Many of the libmultipath-based function added will reference the ns mpath_device, so add that now. Also add the NS head disk pointer for the same reason.

---
 drivers/nvme/host/Kconfig     | 1 +
 drivers/nvme/host/multipath.c | 4 ++++
 drivers/nvme/host/nvme.h      | 8 ++++++++
 3 files changed, 13 insertions(+)
drivers/nvme/host/Kconfig +1 -0
index 31974c7dd20c9..1b3f76e781bad 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config NVME_CORE
 	tristate
+	select LIBMULTIPATH
 
 config BLK_DEV_NVME
 	tristate "NVM Express block device"
drivers/nvme/host/multipath.c +4 -0
index 9b9a657fa330f..14947736744a5 100644
@@ -1496,3 +1496,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
 }
+
+__maybe_unused
+static const struct mpath_head_template mpdt = {
+};
drivers/nvme/host/nvme.h +8 -0
index 824651cc898db..3e023948015ac 100644
@@ -13,6 +13,7 @@
 #include <linux/blk-mq.h>
 #include <linux/sed-opal.h>
 #include <linux/fault-inject.h>
+#include <linux/multipath.h>
 #include <linux/rcupdate.h>
 #include <linux/wait.h>
 #include <linux/t10-pi.h>
@@ -560,6 +561,8 @@ struct nvme_ns_head {
 
 	u16			nr_plids;
 	u16			*plids;
+
+	struct mpath_head	mpath_head;
 #ifdef CONFIG_NVME_MULTIPATH
 	struct bio_list		requeue_list;
 	spinlock_t		requeue_lock;
@@ -590,6 +593,7 @@ enum nvme_ns_features {
 };
 
 struct nvme_ns {
+	struct mpath_device mpath_device;
 	struct list_head list;
 
 	struct nvme_ctrl *ctrl;
@@ -620,6 +624,10 @@ struct nvme_ns {
 	struct nvme_fault_inject fault_inject;
 };
 
+#define nvme_mpath_to_ns(d) container_of(d, struct nvme_ns, mpath_device)
+#define nvme_mpath_to_ns_head(h) \
+		container_of(h, struct nvme_ns_head, mpath_head)
+
 /* NVMe ns supports metadata actions by the controller (generate/strip) */
 static inline bool nvme_ns_has_pi(struct nvme_ns_head *head)
 {
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH v3 01/10] nvme-multipath: add initial support for using libmultipath

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The commit message claims to add an `mpath_disk` pointer to the head structure, but it is…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The commit message claims to add an mpath_disk pointer to the head structure, but it is entirely missing from the implementation.
- [Low] The patch selects a non-existent Kconfig symbol LIBMULTIPATH.
- [Low] The patch references a non-existent header file <linux/multipath.h> and instantiates undefined, incomplete structure types. --

commit 733f55d7e9c3919c9123eedece5d53fa4f13cc46
Author: John Garry <[email protected]>

nvme-multipath: add initial support for using libmultipath

This patch introduces initial structural changes to support libmultipath in the nvme subsystem. It modifies nvme Kconfig, nvme_ns, and nvme_ns_head structures to include multipath components.

Quoted 4 lines

Add initial support, as follows:
- Add mpath_head_template
- Add mpath_device in nvme_ns
- Add mpath_disk pointer to head structure

[Severity: Medium]
The commit message states that an mpath_disk pointer is being added to the head structure, but the actual implementation below adds a struct mpath_head by value instead. Is the commit message stale, or was this pointer omitted by mistake?

drivers/nvme/host/Kconfig line 4

Quoted 9 lines
drivers/nvme/host/Kconfig +1 -0
index 31974c7dd20c9..1b3f76e781bad 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config NVME_CORE
 	tristate
+	select LIBMULTIPATH

[Severity: Low]
Does the LIBMULTIPATH Kconfig symbol exist in the tree? Selecting a non-existent symbol here will result in Kconfig unmet dependency warnings.

Quoted 4 lines
drivers/nvme/host/multipath.c +0 -0
index 9b9a657fa330f..14947736744a5 100644

[ ... ]

drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1502

Quoted 8 lines
drivers/nvme/host/multipath.c +4 -0
@@ -1496,3 +1496,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
 }
+
+__maybe_unused
+static const struct mpath_head_template mpdt = {
+};

[Severity: Low]
Does this compile? The struct mpath_head_template type does not appear to be defined anywhere.

drivers/nvme/host/nvme.h line 16

Quoted 9 lines
drivers/nvme/host/nvme.h +1 -0
index 824651cc898db..3e023948015ac 100644
@@ -13,6 +13,7 @@
 #include <linux/blk-mq.h>
 #include <linux/sed-opal.h>
 #include <linux/fault-inject.h>
+#include <linux/multipath.h>

[Severity: Low]
Is linux/multipath.h present in the tree? Including a missing header will cause build failures.

[ ... ]

Quoted 10 lines
drivers/nvme/host/nvme.h +2 -0
@@ -560,6 +561,8 @@ struct nvme_ns_head {
 
 	u16			nr_plids;
 	u16			*plids;
+
+	struct mpath_head	mpath_head;
 #ifdef CONFIG_NVME_MULTIPATH
 	struct bio_list		requeue_list;
 	spinlock_t		requeue_lock;

drivers/nvme/host/nvme.h enum nvme_ns_features { · line 597

Quoted 5 lines
drivers/nvme/host/nvme.h +1 -0
@@ -590,6 +593,7 @@ enum nvme_ns_features {
 };
 
 struct nvme_ns {
+	struct mpath_device mpath_device;
 	struct list_head list;

[Severity: Low]
Since linux/multipath.h does not seem to exist, these embed undefined structures (struct mpath_head and struct mpath_device) directly by value, which will result in incomplete type errors during compilation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v3 2/10 nvme-multipath: add nvme_mpath_available_path()

In reply to John Garry

This is for mpath_head_template.available_path callback.

Currently the same functionality is in nvme_available_path().

---
 drivers/nvme/host/multipath.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/nvme/host/multipath.c +20 -0
index 14947736744a5..14c4370f7303f 100644
@@ -505,6 +505,25 @@ static bool nvme_available_path(struct nvme_ns_head *head)
 	return nvme_mpath_queue_if_no_path(head);
 }
 
+static bool nvme_mpath_available_path(struct mpath_device *mpath_device)
+{
+	struct nvme_ns *ns = nvme_mpath_to_ns(mpath_device);
+
+	if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
+		return false;
+
+	switch (nvme_ctrl_state(ns->ctrl)) {
+	case NVME_CTRL_LIVE:
+	case NVME_CTRL_RESETTING:
+	case NVME_CTRL_CONNECTING:
+		return true;
+	default:
+		break;
+	}
+
+	return false;
+}
+
 static void nvme_ns_head_submit_bio(struct bio *bio)
 {
 	struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
@@ -1499,4 +1518,5 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 
 __maybe_unused
 static const struct mpath_head_template mpdt = {
+	.available_path = nvme_mpath_available_path,
 };
Signature · 1 line
2.43.7
author
#4 lore raw

PATCH v3 3/10 nvme-multipath: add nvme_mpath_{add, remove}_cdev()

In reply to John Garry

These are for mpath_head_template.add_cdev+del_cdev callbacks.

Currently the same functionality is in nvme_add_ns_head_cdev() and nvme_cdev_del().

---
 drivers/nvme/host/multipath.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/nvme/host/multipath.c +20 -0
index 14c4370f7303f..e0573ca71ec60 100644
@@ -630,6 +630,24 @@ const struct block_device_operations nvme_ns_head_ops = {
 	.pr_ops		= &nvme_pr_ops,
 };
 
+static int nvme_mpath_add_cdev(struct mpath_head *mpath_head)
+{
+	struct nvme_ns_head *head = nvme_mpath_to_ns_head(mpath_head);
+	char name[32];
+
+	head->cdev_device.parent = &head->subsys->dev;
+	snprintf(name, sizeof(name), "ng%dn%d", head->subsys->instance,
+		 head->instance);
+
+	return nvme_cdev_add(name, &mpath_head->cdev, &mpath_head->cdev_device,
+			&mpath_chr_fops, THIS_MODULE);
+}
+
+static void nvme_mpath_del_cdev(struct mpath_head *mpath_head)
+{
+	nvme_cdev_del(&mpath_head->cdev, &mpath_head->cdev_device);
+}
+
 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
 {
 	return container_of(cdev, struct nvme_ns_head, cdev);
@@ -1519,4 +1537,6 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 __maybe_unused
 static const struct mpath_head_template mpdt = {
 	.available_path = nvme_mpath_available_path,
+	.add_cdev = nvme_mpath_add_cdev,
+	.del_cdev = nvme_mpath_del_cdev,
 };
Signature · 1 line
2.43.7
author
#5 lore raw

PATCH v3 4/10 nvme-multipath: add nvme_mpath_is_{disabled, optimised}

In reply to John Garry

These are for mpath_head_template.is_{disabled, optimized} callbacks, and just call into nvme_path_is_disabled() and nvme_path_is_optimized(), respectively.

---
 drivers/nvme/host/multipath.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/nvme/host/multipath.c +12 -0
index e0573ca71ec60..6d3df1775dbeb 100644
@@ -305,6 +305,11 @@ static bool nvme_path_is_disabled(struct nvme_ns *ns)
 	return false;
 }
 
+static bool nvme_mpath_is_disabled(struct mpath_device *mpath_device)
+{
+	return nvme_path_is_disabled(nvme_mpath_to_ns(mpath_device));
+}
+
 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
 {
 	int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
@@ -447,6 +452,11 @@ static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
 		ns->ana_state == NVME_ANA_OPTIMIZED;
 }
 
+static bool nvme_mpath_is_optimized(struct mpath_device *mpath_device)
+{
+	return nvme_path_is_optimized(nvme_mpath_to_ns(mpath_device));
+}
+
 static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head)
 {
 	int node = numa_node_id();
@@ -1539,4 +1549,6 @@ static const struct mpath_head_template mpdt = {
 	.available_path = nvme_mpath_available_path,
 	.add_cdev = nvme_mpath_add_cdev,
 	.del_cdev = nvme_mpath_del_cdev,
+	.is_disabled = nvme_mpath_is_disabled,
+	.is_optimized = nvme_mpath_is_optimized,
 };
Signature · 1 line
2.43.7
#6 lore raw

Re: [PATCH v3 04/10] nvme-multipath: add nvme_mpath_is_{disabled, optimised}

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch relies on missing structs, headers, and Kconfig symbols, causing a fatal build failure. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch relies on missing structs, headers, and Kconfig symbols, causing a fatal build failure. --

commit b4542aff5d48fe0783ce1a5c4b163d732171214c
Author: John Garry <[email protected]>

nvme-multipath: add nvme_mpath_is_{disabled, optimised}

This commit adds nvme_mpath_is_disabled and nvme_mpath_is_optimized callbacks for multipath devices. It wires these up to the mpath_head_template structure.

drivers/nvme/host/multipath.c static bool nvme_path_is_disabled(struct nvme_ns *ns) · line 309

Quoted 10 lines
drivers/nvme/host/multipath.c +2 -0
index e0573ca71ec60..6d3df1775dbeb 100644
@@ -305,6 +305,11 @@ static bool nvme_path_is_disabled(struct nvme_ns *ns)
 	return false;
 }
 
+static bool nvme_mpath_is_disabled(struct mpath_device *mpath_device)
+{

[Severity: Low]
Does this patch depend on other missing patches in a series?

Compiling this change fails because struct mpath_device is an incomplete type. It appears other dependencies like <linux/multipath.h> or LIBMULTIPATH are missing from the tree.

Quoted 3 lines
drivers/nvme/host/multipath.c +3 -0
+	return nvme_path_is_disabled(nvme_mpath_to_ns(mpath_device));
+}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#7 lore raw

PATCH v3 5/10 nvme-multipath: add nvme_mpath_cdev_ioctl()

In reply to John Garry

Add nvme_mpath_cdev_ioctl(), which does the same as nvme_ns_head_chr_ioctl()

Also add nvme_mpath_ioctl_begin() and nvme_mpath_ioctl_finish() - they are special handling for how the SRCU read lock needs to be dropped for the controller command IOCTL handling. In this, nvme_mpath_ioctl_begin() takes a reference to the controller, and then mpath_chr_ioctl() will drop the SRCU read lock before calling nvme_mpath_cdev_ioctl() and finally the controller reference is dropped in nvme_mpath_ioctl_finish().

---
 drivers/nvme/host/ioctl.c     | 37 +++++++++++++++++++++++++++++++----
 drivers/nvme/host/multipath.c |  3 +++
 drivers/nvme/host/nvme.h      |  6 ++++++
 3 files changed, 42 insertions(+), 4 deletions(-)
drivers/nvme/host/ioctl.c +33 -4
index 664216eece4a6..14ac86b8b1a8a 100644
@@ -623,11 +623,9 @@ int nvme_ioctl(struct block_device *bdev, blk_mode_t mode,
 	return nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
 }
 
-long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+static long _nvme_ns_chr_ioctl(struct nvme_ns *ns, unsigned int cmd,
+				unsigned long arg, bool open_for_write)
 {
-	struct nvme_ns *ns =
-		container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
-	bool open_for_write = file->f_mode & FMODE_WRITE;
 	void __user *argp = (void __user *)arg;
 
 	if (is_ctrl_ioctl(cmd))
@@ -635,6 +633,14 @@ long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	return nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
 }
 
+long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct nvme_ns *ns =
+		container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
+
+	return _nvme_ns_chr_ioctl(ns, cmd, arg, file->f_mode & FMODE_WRITE);
+}
+
 static int nvme_uring_cmd_checks(unsigned int issue_flags)
 {
 
@@ -689,6 +695,29 @@ int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd,
 	return 0;
 }
 #ifdef CONFIG_NVME_MULTIPATH
+long nvme_mpath_cdev_ioctl(struct mpath_device *mpath_device, unsigned int cmd,
+					unsigned long arg, bool open_for_write)
+{
+	return _nvme_ns_chr_ioctl(nvme_mpath_to_ns(mpath_device), cmd,
+			arg, open_for_write);
+}
+
+void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
+		unsigned int cmd, void **data)
+{
+	struct nvme_ctrl *ctrl = nvme_mpath_to_ns(mpath_device)->ctrl;
+
+	if (is_ctrl_ioctl(cmd)) {
+		nvme_get_ctrl(ctrl);
+		*data = ctrl;
+	}
+}
+
+void nvme_mpath_ioctl_finish(void *opaque)
+{
+	nvme_put_ctrl(opaque);
+}
+
 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
 		void __user *argp, struct nvme_ns_head *head, int srcu_idx,
 		bool open_for_write)
drivers/nvme/host/multipath.c +3 -0
index 6d3df1775dbeb..9a1703f01ef60 100644
@@ -1551,4 +1551,7 @@ static const struct mpath_head_template mpdt = {
 	.del_cdev = nvme_mpath_del_cdev,
 	.is_disabled = nvme_mpath_is_disabled,
 	.is_optimized = nvme_mpath_is_optimized,
+	.cdev_ioctl = nvme_mpath_cdev_ioctl,
+	.ioctl_begin = nvme_mpath_ioctl_begin,
+	.ioctl_finish = nvme_mpath_ioctl_finish,
 };
drivers/nvme/host/nvme.h +6 -0
index 3e023948015ac..4405e47bfe1d9 100644
@@ -1070,6 +1070,12 @@ void nvme_mpath_remove_disk(struct nvme_ns_head *head);
 void nvme_mpath_start_request(struct request *rq);
 void nvme_mpath_end_request(struct request *rq);
 
+long nvme_mpath_cdev_ioctl(struct mpath_device *mpath_device, unsigned int cmd,
+			unsigned long arg, bool open_for_write);
+void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
+			unsigned int cmd, void **opaque);
+void nvme_mpath_ioctl_finish(void *opaque);
+
 static inline void nvme_trace_bio_complete(struct request *req)
 {
 	struct nvme_ns *ns = req->q->queuedata;
Signature · 1 line
2.43.7
#8 lore raw

Re: [PATCH v3 05/10] nvme-multipath: add nvme_mpath_cdev_ioctl()

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Accessing `ns->ctrl` in `_nvme_ns_chr_ioctl()` leads to a Use-After-Free because the SRCU read…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Accessing ns->ctrl in _nvme_ns_chr_ioctl() leads to a Use-After-Free because the SRCU read lock protecting the namespace is dropped by the caller.
- [Critical] nvme_mpath_ioctl_begin() fails to initialize *data for non-controller ioctls, causing a NULL pointer dereference or crash in nvme_mpath_ioctl_finish(). --

commit 7c14abcc0f25bf0d710bcb67c0dbd7f7f3dc3774
Author: John Garry <[email protected]>

nvme-multipath: add nvme_mpath_cdev_ioctl()

This commit adds nvme_mpath_cdev_ioctl() and related ioctl lifecycle callbacks to handle SRCU read lock dropping during controller command IOCTL handling for NVMe multipath character devices.

Quoted 4 lines
drivers/nvme/host/ioctl.c +0 -0
index 664216eece4a6..14ac86b8b1a8a 100644

[ ... ]

drivers/nvme/host/ioctl.c int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd, · line 703

Quoted 10 lines
drivers/nvme/host/ioctl.c +6 -0
@@ -689,6 +695,29 @@ int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd,
 	return 0;
 }
 #ifdef CONFIG_NVME_MULTIPATH
+long nvme_mpath_cdev_ioctl(struct mpath_device *mpath_device, unsigned int cmd,
+					unsigned long arg, bool open_for_write)
+{
+	return _nvme_ns_chr_ioctl(nvme_mpath_to_ns(mpath_device), cmd,
+			arg, open_for_write);
+}

[Severity: Critical]
Could this lead to a use-after-free on the namespace object?

When a userspace program issues a controller ioctl on a multipath char device, the generic mpath_chr_ioctl() drops the SRCU read lock before calling nvme_mpath_cdev_ioctl().

While nvme_mpath_ioctl_begin() takes a reference to the controller, the namespace object itself is left unprotected during this window. If the namespace is concurrently removed, passing the unprotected ns pointer down to _nvme_ns_chr_ioctl() and dereferencing ns->ctrl could access freed memory.

drivers/nvme/host/ioctl.c int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd, · line 714

Quoted 11 lines
drivers/nvme/host/ioctl.c +11 -0
+
+void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
+		unsigned int cmd, void **data)
+{
+	struct nvme_ctrl *ctrl = nvme_mpath_to_ns(mpath_device)->ctrl;
+
+	if (is_ctrl_ioctl(cmd)) {
+		nvme_get_ctrl(ctrl);
+		*data = ctrl;
+	}
+}

[Severity: Critical]
Does this leave *data uninitialized or unmodified for non-controller ioctls?

If a user issues a standard ioctl (like NVME_IOCTL_ID), is_ctrl_ioctl(cmd) will evaluate to false, meaning *data is not assigned here.

drivers/nvme/host/ioctl.c int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd, · line 719

Quoted 5 lines
drivers/nvme/host/ioctl.c +5 -0
+
+void nvme_mpath_ioctl_finish(void *opaque)
+{
+	nvme_put_ctrl(opaque);
+}

[Severity: Critical]
Is it safe to unconditionally call nvme_put_ctrl() here?

If nvme_mpath_ioctl_begin() did not set *data (because it was not a controller ioctl), opaque might be NULL or uninitialized garbage.

Calling nvme_put_ctrl() unconditionally:

static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)

{
        put_device(ctrl->device);
}

will dereference ctrl->device, which could result in a panic if opaque is NULL or an invalid pointer. Should there be a check before calling nvme_put_ctrl()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#9 lore raw

PATCH v3 6/10 nvme-multipath: add uring_cmd support

In reply to John Garry

Add callback nvme_mpath_chr_uring_cmd, which is equivalent to nvme_ns_head_chr_uring_cmd().

Also fill in chr_uring_cmd_iopoll with same function as currently used, chr_uring_cmd_iopoll().

---
 drivers/nvme/host/ioctl.c     | 8 ++++++++
 drivers/nvme/host/multipath.c | 2 ++
 drivers/nvme/host/nvme.h      | 2 ++
 3 files changed, 12 insertions(+)
drivers/nvme/host/ioctl.c +8 -0
index 14ac86b8b1a8a..d9eee6e685f96 100644
@@ -718,6 +718,14 @@ void nvme_mpath_ioctl_finish(void *opaque)
 	nvme_put_ctrl(opaque);
 }
 
+int nvme_mpath_chr_uring_cmd(struct mpath_device *mpath_device,
+		struct io_uring_cmd *ioucmd,
+		unsigned int issue_flags)
+{
+	return nvme_ns_uring_cmd(nvme_mpath_to_ns(mpath_device), ioucmd,
+					issue_flags);
+}
+
 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
 		void __user *argp, struct nvme_ns_head *head, int srcu_idx,
 		bool open_for_write)
drivers/nvme/host/multipath.c +2 -0
index 9a1703f01ef60..b27b45c59c883 100644
@@ -1554,4 +1554,6 @@ static const struct mpath_head_template mpdt = {
 	.cdev_ioctl = nvme_mpath_cdev_ioctl,
 	.ioctl_begin = nvme_mpath_ioctl_begin,
 	.ioctl_finish = nvme_mpath_ioctl_finish,
+	.chr_uring_cmd = nvme_mpath_chr_uring_cmd,
+	.chr_uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll,
 };
drivers/nvme/host/nvme.h +2 -0
index 4405e47bfe1d9..35618285caf89 100644
@@ -1069,6 +1069,8 @@ void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
 void nvme_mpath_remove_disk(struct nvme_ns_head *head);
 void nvme_mpath_start_request(struct request *rq);
 void nvme_mpath_end_request(struct request *rq);
+int nvme_mpath_chr_uring_cmd(struct mpath_device *mpath_device,
+		struct io_uring_cmd *ioucmd, unsigned int issue_flags);
 
 long nvme_mpath_cdev_ioctl(struct mpath_device *mpath_device, unsigned int cmd,
 			unsigned long arg, bool open_for_write);
Signature · 1 line
2.43.7
#10 lore raw

Re: [PATCH v3 06/10] nvme-multipath: add uring_cmd support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Polling thread accesses a freed request if it completes asynchronously via a timeout or abortion. --

commit 26f589fad741b01889fb3c28f4f60f8b1eb60783
Author: John Garry <[email protected]>

nvme-multipath: add uring_cmd support

This commit adds uring_cmd support to NVMe multipath devices by introducing the nvme_mpath_chr_uring_cmd callback. It also wires up the existing nvme_ns_chr_uring_cmd_iopoll for io_uring polling.

Quoted 4 lines
drivers/nvme/host/multipath.c +0 -0
index 9a1703f01ef60..b27b45c59c883 100644

[ ... ]

drivers/nvme/host/multipath.c static const struct mpath_head_template mpdt = { · line 1558

Quoted 6 lines
drivers/nvme/host/multipath.c +2 -0
@@ -1554,4 +1554,6 @@ static const struct mpath_head_template mpdt = {
 	.cdev_ioctl = nvme_mpath_cdev_ioctl,
 	.ioctl_begin = nvme_mpath_ioctl_begin,
 	.ioctl_finish = nvme_mpath_ioctl_finish,
+	.chr_uring_cmd = nvme_mpath_chr_uring_cmd,
+	.chr_uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll,

[Severity: High]
This is a pre-existing issue, but does assigning nvme_ns_chr_uring_cmd_iopoll() here expose a use-after-free for multipath devices if a command times out?

Looking at nvme_uring_cmd_end_io(), if a command completes asynchronously or times out, the handler frees the request by returning RQ_END_IO_FREE, but it appears to leave pdu->req set:

nvme_uring_cmd_end_io() {
    ...
    } else {
        io_uring_cmd_do_in_task_lazy(ioucmd, nvme_uring_task_cb);
    }
    return RQ_END_IO_FREE;
}

Since pdu->req isn't cleared, if the polling thread continues to spin in nvme_ns_chr_uring_cmd_iopoll(), won't it dereference the freed request?

nvme_ns_chr_uring_cmd_iopoll() {
    ...
    struct request *req = pdu->req;

    if (req && blk_rq_is_poll(req))
        return blk_rq_poll(req, iob, poll_flags);
    ...
}
Quoted 1 line
drivers/nvme/host/multipath.c +0 -0
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#11 lore raw

PATCH v3 7/10 nvme-multipath: add nvme_mpath_synchronize()

In reply to John Garry

Add a wrapper which calls into mpath_synchronize.

---
 drivers/nvme/host/nvme.h | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/nvme/host/nvme.h +8 -0
index 35618285caf89..292526c9dda29 100644
@@ -1078,6 +1078,11 @@ void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
 			unsigned int cmd, void **opaque);
 void nvme_mpath_ioctl_finish(void *opaque);
 
+static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
+{
+	mpath_synchronize(&head->mpath_head);
+}
+
 static inline void nvme_trace_bio_complete(struct request *req)
 {
 	struct nvme_ns *ns = req->q->queuedata;
@@ -1113,6 +1118,9 @@ static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
 {
 	return false;
 }
+static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
+{
+}
 static inline void nvme_failover_req(struct request *req)
 {
 }
Signature · 1 line
2.43.7
#12 lore raw

Re: [PATCH v3 07/10] nvme-multipath: add nvme_mpath_synchronize()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch introduces a wrapper `nvme_mpath_synchronize()` that calls an undefined function…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a wrapper nvme_mpath_synchronize() that calls an undefined function mpath_synchronize(), relying on a missing dependency (libmultipath), which leads to a build failure. --

commit f655883eea782b4086749f2985502e444177db21
Author: John Garry <[email protected]>

nvme-multipath: add nvme_mpath_synchronize()

This commit adds a wrapper function which calls into mpath_synchronize for the NVME multipath subsystem.

drivers/nvme/host/nvme.h void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device, · line 1083

Quoted 11 lines
drivers/nvme/host/nvme.h +3 -0
index 35618285caf89..292526c9dda29 100644
@@ -1078,6 +1078,11 @@ void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
 			unsigned int cmd, void **opaque);
 void nvme_mpath_ioctl_finish(void *opaque);
 
+static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
+{
+	mpath_synchronize(&head->mpath_head);

[Severity: Low]
Does this code build cleanly? It appears that mpath_synchronize() is not declared or defined in the current tree. This seems like it would cause a compiler error due to an implicit function declaration when building with NVME multipath enabled.

Is this patch relying on a missing dependency or missing header inclusion?

Quoted 3 lines
drivers/nvme/host/nvme.h +2 -0
+}
+
 static inline void nvme_trace_bio_complete(struct request *req)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#13 lore raw

Re: [PATCH v3 07/10] nvme-multipath: add nvme_mpath_synchronize()

Quoted 30 lines · On 03/07/2026 12:24, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a wrapper nvme_mpath_synchronize() that calls an undefined function mpath_synchronize(), relying on a missing dependency (libmultipath), which leads to a build failure. --

commit f655883eea782b4086749f2985502e444177db21
Author: John Garry <[email protected]>

nvme-multipath: add nvme_mpath_synchronize()

This commit adds a wrapper function which calls into mpath_synchronize for the NVME multipath subsystem.

drivers/nvme/host/nvme.h void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device, · line 1083

Quoted 11 lines
drivers/nvme/host/nvme.h +3 -0
index 35618285caf89..292526c9dda29 100644
@@ -1078,6 +1078,11 @@ void nvme_mpath_ioctl_begin(struct mpath_device *mpath_device,
  			unsigned int cmd, void **opaque);
  void nvme_mpath_ioctl_finish(void *opaque);
 
+static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
+{
+	mpath_synchronize(&head->mpath_head);

[Severity: Low]
Does this code build cleanly? It appears that mpath_synchronize() is not declared or defined in the current tree. This seems like it would cause a compiler error due to an implicit function declaration when building with NVME multipath enabled.

Quoted 1 line

Is this patch relying on a missing dependency or missing header inclusion?

I think that next time I'll just send a mega series with everything..

Quoted 5 lines
Quoted 4 lines
drivers/nvme/host/nvme.h +2 -0
+}
+
  static inline void nvme_trace_bio_complete(struct request *req)
author
#14 lore raw

PATCH v3 8/10 nvme-multipath: add nvme_{add,delete}_ns()

In reply to John Garry

Add functions to call into the mpath_add_device() and mpath_delete_device() functions.

The per-NS gendisk pointer is used as the mpath_device disk pointer, which is used in libmultipath for references the per-path block device.

---
 drivers/nvme/host/nvme.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/nvme/host/nvme.h +12 -0
index 292526c9dda29..9fd958a426f1f 100644
@@ -1041,6 +1041,18 @@ 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);
+
+static inline void nvme_add_ns(struct nvme_ns *ns)
+{
+	mpath_add_device(&ns->mpath_device, &ns->head->mpath_head,
+		ns->disk, ns->ctrl->numa_node, &ns->ctrl->nr_active);
+}
+
+static inline bool nvme_delete_ns(struct nvme_ns *ns)
+{
+	return mpath_delete_device(&ns->mpath_device);
+}
+
 #ifdef CONFIG_NVME_MULTIPATH
 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
 {
Signature · 1 line
2.43.7
#15 lore raw

Re: [PATCH v3 08/10] nvme-multipath: add nvme_{add,delete}_ns()

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] The function `nvme_add_ns()` unconditionally accesses `ns->ctrl->nr_active`, which is conditionally…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The function nvme_add_ns() unconditionally accesses ns->ctrl->nr_active, which is conditionally compiled, causing a build failure when CONFIG_NVME_MULTIPATH is disabled.
- [Low] The patch introduces calls to undeclared functions (mpath_add_device, mpath_delete_device), causing a hard compilation failure.
--

commit 6769aa1dce3bd34edd5c71bc4aa5c9950e9c148a
Author: John Garry <[email protected]>

nvme-multipath: add nvme_{add,delete}_ns()

This commit adds wrapper functions to call into the mpath_add_device and mpath_delete_device APIs. The per-NS gendisk pointer is utilized as the mpath_device disk pointer for libmultipath references.

Quoted 3 lines
drivers/nvme/host/nvme.h +0 -0

[ ... ]

drivers/nvme/host/nvme.h extern const struct block_device_operations nvme_bdev_ops; · line 1048

Quoted 9 lines
drivers/nvme/host/nvme.h +5 -0
@@ -1041,6 +1041,18 @@ 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);
+
+static inline void nvme_add_ns(struct nvme_ns *ns)
+{
+	mpath_add_device(&ns->mpath_device, &ns->head->mpath_head,
+		ns->disk, ns->ctrl->numa_node, &ns->ctrl->nr_active);

[Severity: Low]
Will this cause a build failure when CONFIG_NVME_MULTIPATH is disabled? Looking at struct nvme_ctrl, the nr_active field is conditionally compiled, but nvme_add_ns accesses it unconditionally.

Also, are mpath_add_device and mpath_delete_device declared in a header that is included here? It appears these functions might be undeclared, which would result in implicit declaration errors during the build.

Quoted 6 lines
drivers/nvme/host/nvme.h +6 -0
+}
+
+static inline bool nvme_delete_ns(struct nvme_ns *ns)
+{
+	return mpath_delete_device(&ns->mpath_device);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author
#16 lore raw

PATCH v3 9/10 nvme-multipath: add nvme_mpath_head_queue_if_no_path()

In reply to John Garry

Add a wrapper to call into mpath_head_queue_if_no_path().

---
 drivers/nvme/host/nvme.h | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/nvme/host/nvme.h +9 -0
index 9fd958a426f1f..354f77dea4870 100644
@@ -1095,6 +1095,11 @@ static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
 	mpath_synchronize(&head->mpath_head);
 }
 
+static inline bool nvme_mpath_head_queue_if_no_path(struct nvme_ns_head *head)
+{
+	return mpath_head_queue_if_no_path(&head->mpath_head);
+}
+
 static inline void nvme_trace_bio_complete(struct request *req)
 {
 	struct nvme_ns *ns = req->q->queuedata;
@@ -1218,6 +1223,10 @@ static inline bool nvme_mpath_queue_if_no_path(struct nvme_ns_head *head)
 {
 	return false;
 }
+static inline bool nvme_mpath_head_queue_if_no_path(struct nvme_ns_head *head)
+{
+	return false;
+}
 #endif /* CONFIG_NVME_MULTIPATH */
 
 int nvme_ns_get_unique_id(struct nvme_ns *ns, u8 id[16],
Signature · 1 line
2.43.7
#17 lore raw

Re: [PATCH v3 09/10] nvme-multipath: add nvme_mpath_head_queue_if_no_path()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The newly added wrapper nvme_mpath_head_queue_if_no_path() calls an undefined function…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The newly added wrapper nvme_mpath_head_queue_if_no_path() calls an undefined function mpath_head_queue_if_no_path(). The required header <linux/multipath.h> and the underlying libmultipath implementation are missing from the kernel tree and this patch series. Compiling the kernel with CONFIG_NVME_MULTIPATH=y will result in a build failure. --

commit cff4d8c98386c9683c8dc00a277d4f52cdd8c6a0
Author: John Garry <[email protected]>

nvme-multipath: add nvme_mpath_head_queue_if_no_path()

This commit adds a wrapper function to check if the multipath head should queue when there are no paths. It provides both an active implementation and a stub for when CONFIG_NVME_MULTIPATH is disabled.

drivers/nvme/host/nvme.h static inline void nvme_mpath_synchronize(struct nvme_ns_head *head) · line 1100

Quoted 11 lines
drivers/nvme/host/nvme.h +3 -0
index 9fd958a426f1f..354f77dea4870 100644
@@ -1095,6 +1095,11 @@ static inline void nvme_mpath_synchronize(struct nvme_ns_head *head)
 	mpath_synchronize(&head->mpath_head);
 }
 
+static inline bool nvme_mpath_head_queue_if_no_path(struct nvme_ns_head *head)
+{
+	return mpath_head_queue_if_no_path(&head->mpath_head);

[Severity: Low]
Does this cause a build failure when CONFIG_NVME_MULTIPATH=y?

It appears that mpath_head_queue_if_no_path() is currently undefined. I couldn't find a declaration or the underlying implementation for it in the kernel tree. Compiling with this configuration might result in an undefined reference or missing header error.

Quoted 3 lines
drivers/nvme/host/nvme.h +2 -0
+}
+
 static inline void nvme_trace_bio_complete(struct request *req)

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author
#18 lore raw

PATCH v3 10/10 nvme-multipath: switch to use libmultipath

In reply to John Garry

Now that much unused libmulipath-based code has been added, do the full switch over.

The major change is that the multipath management is moved out of the nvme_ns_head structure and into the mpath_head structure.

The check for ns->head->disk is now replaced with a ns->mpath_head.disk check, it decide whether we are really in multipath mode.

---
 drivers/nvme/host/core.c      |  89 ++--
 drivers/nvme/host/ioctl.c     |  91 +---
 drivers/nvme/host/multipath.c | 921 +++++-----------------------------
 drivers/nvme/host/nvme.h      | 104 +---
 drivers/nvme/host/pr.c        |  18 -
 drivers/nvme/host/sysfs.c     |  89 +---
 6 files changed, 238 insertions(+), 1074 deletions(-)
drivers/nvme/host/core.c +45 -44
index 453c1f0b2dd09..476a0cd2452ce 100644
@@ -413,7 +413,7 @@ static inline enum nvme_disposition nvme_decide_disposition(struct request *req)
 	if ((nvme_req(req)->status & NVME_SCT_SC_MASK) == NVME_SC_AUTH_REQUIRED)
 		return AUTHENTICATE;
 
-	if (req->cmd_flags & REQ_NVME_MPATH) {
+	if (is_mpath_request(req)) {
 		if (nvme_is_path_error(nvme_req(req)->status) ||
 		    blk_queue_dying(req->q))
 			return FAILOVER;
@@ -454,7 +454,7 @@ static inline void __nvme_end_req(struct request *req)
 	}
 	nvme_end_req_zoned(req);
 	nvme_trace_bio_complete(req);
-	if (req->cmd_flags & REQ_NVME_MPATH)
+	if (is_mpath_request(req))
 		nvme_mpath_end_request(req);
 }
 
@@ -687,7 +687,7 @@ static void nvme_free_ns_head(struct kref *ref)
 
 	nvme_mpath_put_disk(head);
 	ida_free(&head->subsys->ns_ida, head->instance);
-	cleanup_srcu_struct(&head->srcu);
+	mpath_head_uninit(&head->mpath_head);
 	nvme_put_subsystem(head->subsys);
 	kfree(head->plids);
 	kfree(head);
@@ -778,7 +778,7 @@ blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
 	    state != NVME_CTRL_DELETING &&
 	    state != NVME_CTRL_DEAD &&
 	    !test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags) &&
-	    !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
+	    !blk_noretry_request(rq) && !is_mpath_request(rq))
 		return BLK_STS_RESOURCE;
 
 	if (!(rq->rq_flags & RQF_DONTPREP))
@@ -2557,11 +2557,12 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
 
 	if (!ret && nvme_ns_head_multipath(ns->head)) {
 		struct queue_limits *ns_lim = &ns->disk->queue->limits;
+		struct gendisk *disk = ns->head->mpath_head.disk;
 		struct queue_limits lim;
 		unsigned int memflags;
 
-		lim = queue_limits_start_update(ns->head->disk->queue);
-		memflags = blk_mq_freeze_queue(ns->head->disk->queue);
+		lim = queue_limits_start_update(disk->queue);
+		memflags = blk_mq_freeze_queue(disk->queue);
 		/*
 		 * queue_limits mixes values that are the hardware limitations
 		 * for bio splitting with what is the device configuration.
@@ -2582,22 +2583,22 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
 		lim.io_min = ns_lim->io_min;
 		lim.io_opt = ns_lim->io_opt;
 		queue_limits_stack_bdev(&lim, ns->disk->part0, 0,
-					ns->head->disk->disk_name);
+				disk->disk_name);
 		if (lim.features & BLK_FEAT_ZONED)
 			nvme_stack_zone_resources(&lim, ns_lim);
 		if (unsupported)
-			ns->head->disk->flags |= GENHD_FL_HIDDEN;
+			disk->flags |= GENHD_FL_HIDDEN;
 		else
 			nvme_init_integrity(ns->head, &lim, info);
 		lim.max_write_streams = ns_lim->max_write_streams;
 		lim.write_stream_granularity = ns_lim->write_stream_granularity;
-		ret = queue_limits_commit_update(ns->head->disk->queue, &lim);
+		ret = queue_limits_commit_update(disk->queue, &lim);
 
-		set_capacity_and_notify(ns->head->disk, get_capacity(ns->disk));
-		set_disk_ro(ns->head->disk, nvme_ns_is_readonly(ns, info));
+		set_capacity_and_notify(disk, get_capacity(ns->disk));
+		set_disk_ro(disk, nvme_ns_is_readonly(ns, info));
 		nvme_mpath_revalidate_paths(ns->head);
 
-		blk_mq_unfreeze_queue(ns->head->disk->queue, memflags);
+		blk_mq_unfreeze_queue(disk->queue, memflags);
 	}
 
 	return ret;
@@ -3967,25 +3968,18 @@ static void nvme_add_ns_cdev(struct nvme_ns *ns)
 static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
 		struct nvme_ns_info *info)
 {
+	struct nvme_subsystem *subsys = ctrl->subsys;
 	struct nvme_ns_head *head;
-	size_t size = sizeof(*head);
 	int ret = -ENOMEM;
 
-#ifdef CONFIG_NVME_MULTIPATH
-	size += nr_node_ids * sizeof(struct nvme_ns *);
-#endif
-
-	head = kzalloc(size, GFP_KERNEL);
+	head = kzalloc(sizeof(*head), GFP_KERNEL);
 	if (!head)
 		goto out;
 	ret = ida_alloc_min(&ctrl->subsys->ns_ida, 1, GFP_KERNEL);
 	if (ret < 0)
 		goto out_free_head;
 	head->instance = ret;
-	INIT_LIST_HEAD(&head->list);
-	ret = init_srcu_struct(&head->srcu);
-	if (ret)
-		goto out_ida_remove;
+
 	head->subsys = ctrl->subsys;
 	head->ns_id = info->nsid;
 	head->ids = info->ids;
@@ -3998,22 +3992,32 @@ static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
 	if (head->ids.csi) {
 		ret = nvme_get_effects_log(ctrl, head->ids.csi, &head->effects);
 		if (ret)
-			goto out_cleanup_srcu;
+			goto out_ida_free;
 	} else
 		head->effects = ctrl->effects;
 
+	ret = mpath_head_init(&head->mpath_head);
+	if (ret)
+		goto out_ida_free;
+
+	head->mpath_head.drv_module = THIS_MODULE;
+	head->mpath_head.disk_groups = nvme_ns_attr_groups;
+	head->mpath_head.parent = &subsys->dev;
+	head->mpath_head.iopolicy = &head->subsys->mpath_iopolicy;
+
 	ret = nvme_mpath_alloc_disk(ctrl, head);
 	if (ret)
-		goto out_cleanup_srcu;
+		goto out_mpath_head_free;
 
 	list_add_tail(&head->entry, &ctrl->subsys->nsheads);
 
 	kref_get(&ctrl->subsys->ref);
 
 	return head;
-out_cleanup_srcu:
-	cleanup_srcu_struct(&head->srcu);
-out_ida_remove:
+
+out_mpath_head_free:
+	mpath_put_head(&head->mpath_head);
+out_ida_free:
 	ida_free(&ctrl->subsys->ns_ida, head->instance);
 out_free_head:
 	kfree(head);
@@ -4052,7 +4056,7 @@ static int nvme_global_check_duplicate_ids(struct nvme_subsystem *this,
 static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 {
 	struct nvme_ctrl *ctrl = ns->ctrl;
-	struct nvme_ns_head *head = NULL;
+	struct nvme_ns_head *head;
 	int ret;
 
 	ret = nvme_global_check_duplicate_ids(ctrl->subsys, &info->ids);
@@ -4111,7 +4115,7 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 	} else {
 		ret = -EINVAL;
 		if ((!info->is_shared || !head->shared) &&
-		    !list_empty(&head->list)) {
+		    !mpath_head_devices_empty(&head->mpath_head)) {
 			dev_err(ctrl->device,
 				"Duplicate unshared namespace %d\n",
 				info->nsid);
@@ -4133,14 +4137,10 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 		}
 	}
 
-	list_add_tail_rcu(&ns->siblings, &head->list);
 	ns->head = head;
+	nvme_add_ns(ns);
 	mutex_unlock(&ctrl->subsys->lock);
 
-#ifdef CONFIG_NVME_MULTIPATH
-	if (cancel_delayed_work(&head->remove_work))
-		module_put(THIS_MODULE);
-#endif
 	return 0;
 
 out_put_ns_head:
@@ -4280,18 +4280,18 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)
 	synchronize_srcu(&ctrl->srcu);
  out_unlink_ns:
 	mutex_lock(&ctrl->subsys->lock);
-	list_del_rcu(&ns->siblings);
-	if (list_empty(&ns->head->list)) {
+
+	if (nvme_delete_ns(ns)) {
 		list_del_init(&ns->head->entry);
 		/*
 		 * If multipath is not configured, we still create a namespace
-		 * head (nshead), but head->disk is not initialized in that
+		 * head (nshead), but mpath_head->disk is not initialized in that
 		 * case.  As a result, only a single reference to nshead is held
 		 * (via kref_init()) when it is created. Therefore, ensure that
 		 * we do not release the reference to nshead twice if head->disk
 		 * is not present.
 		 */
-		if (ns->head->disk)
+		if (ns->head->mpath_head.disk)
 			last_path = true;
 	}
 	mutex_unlock(&ctrl->subsys->lock);
@@ -4306,6 +4306,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)
 
 static void nvme_ns_remove(struct nvme_ns *ns)
 {
+	struct nvme_ns_head *head = ns->head;
 	bool last_path = false;
 
 	if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
@@ -4319,23 +4320,23 @@ static void nvme_ns_remove(struct nvme_ns *ns)
 	 * Ensure that !NVME_NS_READY is seen by other threads to prevent
 	 * this ns going back into current_path.
 	 */
-	synchronize_srcu(&ns->head->srcu);
+	nvme_mpath_synchronize(head);
 
 	/* wait for concurrent submissions */
 	if (nvme_mpath_clear_current_path(ns))
-		synchronize_srcu(&ns->head->srcu);
+		nvme_mpath_synchronize(head);
 
 	mutex_lock(&ns->ctrl->subsys->lock);
-	list_del_rcu(&ns->siblings);
-	if (list_empty(&ns->head->list)) {
-		if (!nvme_mpath_queue_if_no_path(ns->head))
+	if (nvme_delete_ns(ns)) {
+
+		if (!nvme_mpath_head_queue_if_no_path(ns->head))
 			list_del_init(&ns->head->entry);
 		last_path = true;
 	}
 	mutex_unlock(&ns->ctrl->subsys->lock);
 
 	/* guarantee not available in head->list */
-	synchronize_srcu(&ns->head->srcu);
+	nvme_mpath_synchronize(head);
 
 	if (!nvme_ns_head_multipath(ns->head)) {
 		if (test_and_clear_bit(NVME_NS_CDEV_LIVE, &ns->flags))
drivers/nvme/host/ioctl.c +1 -90

103 lines not on this page · raw message

drivers/nvme/host/multipath.c +139 -782

1,206 lines not on this page · raw message

drivers/nvme/host/nvme.h +26 -78

236 lines not on this page · raw message

drivers/nvme/host/pr.c +0 -18
index fe7dbe2648158..65e4dc74833e9 100644
@@ -49,22 +49,6 @@ static enum pr_type block_pr_type_from_nvme(enum nvme_pr_type type)
 	return 0;
 }
 
-static int nvme_send_ns_head_pr_command(struct block_device *bdev,
-		struct nvme_command *c, void *data, unsigned int data_len)
-{
-	struct nvme_ns_head *head = bdev->bd_disk->private_data;
-	int srcu_idx = srcu_read_lock(&head->srcu);
-	struct nvme_ns *ns = nvme_find_path(head);
-	int ret = -EWOULDBLOCK;
-
-	if (ns) {
-		c->common.nsid = cpu_to_le32(ns->head->ns_id);
-		ret = nvme_submit_sync_cmd(ns->queue, c, data, data_len);
-	}
-	srcu_read_unlock(&head->srcu, srcu_idx);
-	return ret;
-}
-
 static int nvme_send_ns_pr_command(struct nvme_ns *ns, struct nvme_command *c,
 		void *data, unsigned int data_len)
 {
@@ -101,8 +85,6 @@ static int __nvme_send_pr_command(struct block_device *bdev, u32 cdw10,
 	c.common.cdw10 = cpu_to_le32(cdw10);
 	c.common.cdw11 = cpu_to_le32(cdw11);
 
-	if (nvme_disk_is_ns_head(bdev->bd_disk))
-		return nvme_send_ns_head_pr_command(bdev, &c, data, data_len);
 	return nvme_send_ns_pr_command(bdev->bd_disk->private_data, &c,
 				data, data_len);
 }
drivers/nvme/host/sysfs.c +27 -62

163 lines not on this page · raw message

Signature · 1 line
2.43.7
#19 lore raw

Re: [PATCH v3 10/10] nvme-multipath: switch to use libmultipath

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Diagnostic sysfs attribute functions incorrectly cast the head disk's `private_data` to…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Diagnostic sysfs attribute functions incorrectly cast the head disk's private_data to struct nvme_ns_head *, leading to out-of-bounds reads and writes.
- [High] Unconditional clearing of NVME_NS_READY for all paths during revalidation.
- [High] Silent removal of BLK_FEAT_PCI_P2PDMA disables P2P DMA on multipath devices.
- [High] Incorrect teardown function mpath_put_head used in nvme_alloc_ns_head error path instead of mpath_head_uninit.
--

commit 7fcfa1e0451b9e00996c66fccb6e7ea67da8b9ea
Author: John Garry <[email protected]>

nvme-multipath: switch to use libmultipath

Now that much unused libmulipath-based code has been added, do the full switch over. The major change is that the multipath management is moved out of the nvme_ns_head structure and into the mpath_head structure.

Quoted 4 lines
drivers/nvme/host/core.c +0 -0
index 453c1f0b2dd09..476a0cd2452ce 100644

[ ... ]

Quoted 28 lines
drivers/nvme/host/core.c +10 -2
@@ -3998,22 +3992,32 @@ static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
 	if (head->ids.csi) {
 		ret = nvme_get_effects_log(ctrl, head->ids.csi, &head->effects);
 		if (ret)
-			goto out_cleanup_srcu;
+			goto out_ida_free;
 	} else
 		head->effects = ctrl->effects;
 
+	ret = mpath_head_init(&head->mpath_head);
+	if (ret)
+		goto out_ida_free;
+
+	head->mpath_head.drv_module = THIS_MODULE;
+	head->mpath_head.disk_groups = nvme_ns_attr_groups;
+	head->mpath_head.parent = &subsys->dev;
+	head->mpath_head.iopolicy = &head->subsys->mpath_iopolicy;
 
 	ret = nvme_mpath_alloc_disk(ctrl, head);
 	if (ret)
-		goto out_cleanup_srcu;
+		goto out_mpath_head_free;
 
 	list_add_tail(&head->entry, &ctrl->subsys->nsheads);
 
 	kref_get(&ctrl->subsys->ref);
 
 	return head;

drivers/nvme/host/core.c static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, · line 4023

Quoted 10 lines
drivers/nvme/host/core.c +4 -3
-out_cleanup_srcu:
-	cleanup_srcu_struct(&head->srcu);
-out_ida_remove:
+
+out_mpath_head_free:
+	mpath_put_head(&head->mpath_head);
+out_ida_free:
 	ida_free(&ctrl->subsys->ns_ida, head->instance);
 out_free_head:
 	kfree(head);

[Severity: High]
Can this error path lead to a NULL pointer dereference or double-free?

If mpath_put_head() triggers mpdt->remove_head(), it might call list_del_init(&head->entry) on a list that hasn't been added yet (as list_add_tail() is called later). Should this use mpath_head_uninit() instead to properly clean up the SRCU struct without prematurely freeing resources?

Quoted 4 lines
drivers/nvme/host/multipath.c +0 -0
index b27b45c59c883..f4cf2dc2c1e43 100644

[ ... ]

Quoted 7 lines
drivers/nvme/host/multipath.c +1 -5
-void nvme_mpath_revalidate_paths(struct nvme_ns_head *head)
+static void nvme_mpath_revalidate_paths_cb(struct mpath_device *mpath_device)
 {
-	sector_t capacity = get_capacity(head->disk);
-	struct nvme_ns *ns;
-	int node;
-	int srcu_idx;

drivers/nvme/host/multipath.c void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) · line 235

Quoted 10 lines
drivers/nvme/host/multipath.c +2 -8
-
-	srcu_idx = srcu_read_lock(&head->srcu);
-	list_for_each_entry_srcu(ns, &head->list, siblings,
-				 srcu_read_lock_held(&head->srcu)) {
-		if (capacity != get_capacity(ns->disk))
-			clear_bit(NVME_NS_READY, &ns->flags);
-	}
-	srcu_read_unlock(&head->srcu, srcu_idx);
+	clear_bit(NVME_NS_READY, &nvme_mpath_to_ns(mpath_device)->flags);
+}

[Severity: High]
Is it correct to unconditionally clear NVME_NS_READY here?

In the previous implementation, nvme_mpath_revalidate_paths() checked if the capacity of each path matched the head (capacity != get_capacity(ns->disk)) before clearing the flag. Does this unconditionally disable healthy paths during capacity revalidation?

[ ... ]

drivers/nvme/host/multipath.c static ssize_t io_requeue_no_usable_path_count_show(struct device *dev, · line 669

Quoted 12 lines
drivers/nvme/host/multipath.c +4 -2
@@ -1253,24 +661,26 @@ static ssize_t io_requeue_no_usable_path_count_show(struct device *dev,
 {
 	struct gendisk *disk = dev_to_disk(dev);
 	struct nvme_ns_head *head = disk->private_data;
+	struct mpath_head *mpath_head = &head->mpath_head;
+	unsigned long val =
+		atomic_long_read(&mpath_head->requeue_no_usable_path_cnt);
 
-	return sysfs_emit(buf, "%lu\n",
-		    atomic_long_read(&head->io_requeue_no_usable_path_count));
+	return sysfs_emit(buf, "%lu\n", val);
 }

[Severity: Critical]
For multipath disks, does disk->private_data now point to struct mpath_head instead of struct nvme_ns_head?

If so, casting it to struct nvme_ns_head * and evaluating &head->mpath_head will compute an address past the end of the allocated object. Could this lead to out-of-bounds reads and writes when reading or writing these sysfs attributes?

This also appears to affect io_requeue_no_usable_path_count_store(), io_fail_no_available_path_count_show(), and io_fail_no_available_path_count_store() below.

[ ... ]

Quoted 19 lines
drivers/nvme/host/multipath.c +19 -0
+int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
+{
+	struct queue_limits lim;
+	int ret;
+
+	head->mpath_head.mpdt = &mpdt;
+
+	/*
+	 * If "multipath_always_on" is enabled, a multipath node is added
+	 * regardless of whether the disk is single/multi ported, and whether
+	 * the namespace is shared or private. If "multipath_always_on" is not
+	 * enabled, a multipath node is added only if the subsystem supports
+	 * multiple controllers and the "multipath" option is configured. In
+	 * either case, for private namespaces, we ensure that the NSID is
+	 * unique.
+	 */
+	if (!multipath_always_on) {
+		if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) ||
+				!multipath)

drivers/nvme/host/multipath.c static const struct mpath_head_template mpdt = { · line 904

Quoted 10 lines
drivers/nvme/host/multipath.c +10 -0
+			return 0;
+	}
+
+	if (!nvme_is_unique_nsid(ctrl, head))
+		return 0;
+
+	blk_set_stacking_limits(&lim);
+	lim.dma_alignment = 3;
+	lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT |
+		BLK_FEAT_POLL | BLK_FEAT_ATOMIC_WRITES;

[Severity: High]
Should BLK_FEAT_PCI_P2PDMA be included in the features bitmask here?

Previously, it was added unconditionally to the multipath head's queue limits. Will its removal cause the block layer to reject PCI Peer-to-Peer DMA bios submitted to the multipath node?

Quoted 12 lines
drivers/nvme/host/multipath.c +12 -0
+	if (head->ids.csi == NVME_CSI_ZNS)
+		lim.features |= BLK_FEAT_ZONED;
+
+	ret = mpath_alloc_head_disk(&head->mpath_head, &lim, ctrl->numa_node);
+	if (ret)
+		return ret;
+
+	sprintf(head->mpath_head.disk->disk_name, "nvme%dn%d",
+			ctrl->subsys->instance, head->instance);
+	nvme_tryget_ns_head(head);
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10