Skip to contentSkip to conversation

Native SCSI Multipath support

PATCH Superseded John Garry 2 participants 64 messages Jul 29

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

John Garry

author
#0 lore raw

v6 cover Native SCSI Multipath support

NATIVE SCSI MULTIPATH:

This series introduces native SCSI multipath support. It is intended as an alternative to dm-mpath.

This support aims to provide a multipath-enabled SCSI block device/ gendisk.

For a SCSI device to support native multipath, either of the following conditions must be satisfied:
a. unique ID in VPD page 83 and ALUA support and scsi_multipath modparam
enabled
b. unique ID in VPD page 83 and scsi_multipath_always modparam enabled

This series relies on reading sdev->access_state to get path information. This path information would be provided by ALUA. ALUA support which does not rely on device handlers has already been discussed at
https://lore.kernel.org/linux-scsi/[email protected]/
and support will be added in the next phase.

New classes of devices are added:
- scsi_mpath_device
- scsi_mpath_disk

These are required since a multipath scsi_device has no common scsi host. An example of the sysfs files and directories for these new classes is as follows:

$ ls -l /sys/class/scsi_mpath_device/scsi_mpath_device0/
total 0
-rw-r--r--    1 root     root          4096 Feb 25 11:59 iopolicy
drwxr-xr-x    2 root     root             0 Feb 25 11:59 multipath
drwxr-xr-x    2 root     root             0 Feb 25 11:59 power
lrwxrwxrwx    1 root     root             0 Feb 25 11:59 subsystem ->
../../../../class/scsi_mpath_device
-rw-r--r--    1 root     root          4096 Feb 25 11:58 uevent
-r--r--r--    1 root     root          4096 Feb 25 11:59 vpd_id
$ ls -l /sys/class/scsi_mpath_device/scsi_mpath_device0/multipath/
total 0
lrwxrwxrwx    1 root     root             0 Feb 25 11:59 8:0:0:0 ->
../../../../platform/host8/session1/target8:0:0/8:0:0:0
lrwxrwxrwx    1 root     root             0 Feb 25 11:59 9:0:0:0 ->
../../../../platform/host9/session2/target9:0:0/9:0:0:0
$ cat /sys/class/scsi_mpath_device/scsi_mpath_device0/vpd_id
naa.600140505200a986f0043c9afa1fd077
$ cat /sys/class/scsi_mpath_device/scsi_mpath_device0/iopolicy
numa
$
$ ls -l /sys/class/scsi_mpath_disk/scsi_mpath_disk0/
total 0
drwxr-xr-x    2 root     root             0 Feb 25 12:00 power
drwxr-xr-x   11 root     root             0 Feb 25 11:58 sdc
lrwxrwxrwx    1 root     root             0 Feb 25 11:58 subsystem ->
../../../../class/scsi_mpath_disk
-rw-r--r--    1 root     root          4096 Feb 25 11:58 uevent
$ ls -l /sys/class/scsi_mpath_disk/scsi_mpath_disk0/sdc/multipath/
total 0
lrwxrwxrwx    1 root     root             0 Feb 25 12:00 sdc:0 ->
../../../../../platform/host8/session1/target8:0:0/8:0:0:0/block/sdc:0
lrwxrwxrwx    1 root     root             0 Feb 25 12:00 sdc:1 ->
../../../../../platform/host9/session2/target9:0:0/9:0:0:0/block/sdc:1
$ ls -l /dev/sdc
brw-rw----    1 root     disk        8,  32 Feb 25 11:58 /dev/sdc

The scsi_device and scsi_disk classes otherwise remain unmodified. However, the per-path block device is hidden in /dev/. Furthermore, multipathed block devices have a new naming scheme, sdX:Y, where X is the scsi multipath device index and Y is the path index.

No multipath sg support is added. We still have a per-path sg device. Since the SCSI block device is multipath enabled, we can access multipathed scsi_ioctl() through that block device.

For failover, we take the approach of cloning bio's and re-submitting them in full (for failover errors).

LIBMULTIPATH:

This series introduces also libmultipath. It is essentially a refactoring of NVME multipath support, so we can have a common library to also support native SCSI multipath.

Much of the code is taken directly from the NVMe multipath code. However, NVMe specifics are removed. A template structure is provided so the driver -may provide callbacks for driver specifics, like ANA support for NVMe.

Important new structures introduced include:

- mpath_head
These contain much of the multipath-specific functionality from nvme_ns_head, including a pointer to the gendisk structure and a path SRCU-based array.

- mpath_device
This is the per-path structure, and contains much the same multipath-specific functionality in nvme_ns

libmultipath provides functionality for path management, path selection, data path, and failover handling.

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

Differences to v5:
- deal with issues sashiko found in libmultipath:
- include wait_bit.h
- don't store error in mpath_head->disk
- use check_mul_overflow() in mpath_can_remove_head()
- use -ENOTTY in mpath_bdev_ioctl()
- deal with issues sashiko found in scsi mpath support:
- check 0 returned from scsi_vpd_lun_id()
- call flush_work() in scsi_mpath_device_iopolicy_store()
- clear SCMD_MPATH_IO_STATS for !bio in scsi_mpath_start_request()
- add checks for calling scsi_mpath_{start,end}_request()
- tweak sd_mpath_dev_show()

Differences to v4:
- deal with issues sashiko/codex found in libmultipath:
- SPDX spelling mistake
- make driver set mpath_device->mpath_head
- change delayed support to use unsigned int for delay
- check for partition in bdev ioctl
- drop zoned support for now
- deal with issues sashiko/codex found in scsi mpath support:
- use ida destory in 2x places
- set vpd_id earlier
- iter through all possible mpath heads when searching
- remove scsi mpath device later in __scsi_remove_device
- call scsi_mpath_clear_path() outside state mutex
- add revalidate paths caller to sd revalidate
- fix sdev put in failover handling
- use READ_ONCE() fo reading sdev state
- fix scsi mpath start/end request
- take scsi mpath head ref in scsi_mpath_remove_head_work()
- fix sd_mpath_probe error path
- fix sd_mpath_dev_show() for deleted multipath disk
- Add RB tag from Hannes (thanks!)

John Garry (27):
libmultipath: Add initial framework
libmultipath: Add basic gendisk support
libmultipath: Add path selection support
libmultipath: Add bio handling
libmultipath: Add support for mpath_device management
libmultipath: Add delayed removal support
libmultipath: Add sysfs helpers
libmultipath: Add support for block device IOCTL
libmultipath: Add mpath_bdev_getgeo()
libmultipath: Add mpath_bdev_get_unique_id()
scsi-multipath: introduce basic SCSI device support
scsi-multipath: introduce scsi_device head structure
scsi-multipath: provide sysfs link from to scsi_device
scsi-multipath: support iopolicy
scsi-multipath: clone each bio
scsi-multipath: clear path when device is blocked
scsi-multipath: revalidate paths upon device unblock
scsi-multipath: failover handling
scsi-multipath: provide callbacks for path state
scsi-multipath: add scsi_mpath_{start,end}_request()
scsi-multipath: add delayed disk removal support
scsi: sd: add multipath disk class
scsi: sd: add multipath disk attr groups
scsi: sd: support multipath disk
scsi: sd: add mpath_dev file
scsi: sd: add mpath_numa_nodes dev attribute
scsi: sd: add mpath_queue_depth dev attribute

 drivers/scsi/Kconfig          |  10 +
 drivers/scsi/Makefile         |   1 +
 drivers/scsi/scsi.c           |   8 +-
 drivers/scsi/scsi_lib.c       |  13 +-
 drivers/scsi/scsi_multipath.c | 673 ++++++++++++++++++++++++
 drivers/scsi/scsi_scan.c      |   4 +
 drivers/scsi/scsi_sysfs.c     |  10 +
 drivers/scsi/sd.c             | 610 +++++++++++++++++++++-
 drivers/scsi/sd.h             |   3 +
 include/linux/multipath.h     | 156 ++++++
 include/scsi/scsi_cmnd.h      |   9 +-
 include/scsi/scsi_device.h    |   2 +
 include/scsi/scsi_driver.h    |   4 +
 include/scsi/scsi_host.h      |   4 +
 include/scsi/scsi_multipath.h | 119 +++++
 lib/Kconfig                   |   6 +
 lib/Makefile                  |   2 +
 lib/multipath.c               | 948 ++++++++++++++++++++++++++++++++++
 18 files changed, 2558 insertions(+), 24 deletions(-)
 create mode 100644 drivers/scsi/scsi_multipath.c
 create mode 100644 include/linux/multipath.h
 create mode 100644 include/scsi/scsi_multipath.h
 create mode 100644 lib/multipath.c
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v6 1/27 libmultipath: Add initial framework

Add initial framework for libmultipath. libmultipath is a library for multipath-capable block drivers, such as NVMe. The main function is to support path management, path selection, and failover handling.

Basic support to add and remove the head structure - mpath_head - is included.

This main purpose of this structure is to manage available paths and path selection. It is quite similar to the multipath functionality in nvme_ns_head. It also manages the multipath gendisk.

Each path is represented by the mpath_device structure. It should hold a pointer to the per-path gendisk and also a list element for all siblings of paths. For NVMe, there would be a mpath_device per nvme_ns.

All the libmultipath code is more or less taken from drivers/nvme/host/multipath.c, which was originally authored by Christoph Hellwig <[email protected]>.

---
 include/linux/multipath.h | 28 ++++++++++++++++
 lib/Kconfig               |  6 ++++
 lib/Makefile              |  2 ++
 lib/multipath.c           | 67 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+)
 create mode 100644 include/linux/multipath.h
 create mode 100644 lib/multipath.c
include/linux/multipath.h new file +28 -0
new file mode 100644
index 0000000000000..407d985cd31f5
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#ifndef _LIBMULTIPATH_H
+#define _LIBMULTIPATH_H
+
+#include <linux/blkdev.h>
+#include <linux/srcu.h>
+
+struct mpath_device {
+	struct list_head	siblings;
+	struct gendisk		*disk;
+};
+
+struct mpath_head {
+	struct srcu_struct	srcu;
+	struct list_head	dev_list;	/* list of all mpath_devs */
+	struct mutex		lock;
+
+	refcount_t		refcount;
+
+	struct mpath_device __rcu 		*current_path[MAX_NUMNODES];
+};
+
+int mpath_get_head(struct mpath_head *mpath_head);
+void mpath_put_head(struct mpath_head *mpath_head);
+int mpath_head_init(struct mpath_head *mpath_head);
+void mpath_head_uninit(struct mpath_head *mpath_head);
+
+#endif // _LIBMULTIPATH_H
lib/Kconfig +6 -0
index 55748b68714e0..d0258bef374a1 100644
@@ -636,3 +636,9 @@ config UNION_FIND
 
 config MIN_HEAP
 	bool
+
+config LIBMULTIPATH
+	bool "MULTIPATH BLOCK DRIVER LIBRARY"
+	depends on BLOCK
+	help
+	  If you say yes here then you get a multipath lib for block drivers
lib/Makefile +2 -0
index 7f75cc6edf94a..7ba5e13be4171 100644
@@ -334,3 +334,5 @@ CONTEXT_ANALYSIS_test_context-analysis.o := y
 obj-$(CONFIG_CONTEXT_ANALYSIS_TEST) += test_context-analysis.o
 
 subdir-$(CONFIG_FORTIFY_SOURCE) += test_fortify
+
+obj-$(CONFIG_LIBMULTIPATH)	+= multipath.o
lib/multipath.c new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.43.7
author
#2 lore raw

PATCH v6 2/27 libmultipath: Add basic gendisk support

In reply to John Garry

Add support to allocate and free a multipath gendisk.

NVMe has almost like-for-like equivalents here:
- mpath_alloc_head_disk() -> nvme_mpath_alloc_disk()
- multipath_partition_scan_work() -> nvme_partition_scan_work()
- mpath_remove_disk() -> nvme_remove_head()
- mpath_device_set_live() -> nvme_mpath_set_live()

struct mpath_head_template is introduced as a method for drivers to provide custom multipath functionality.

---
 include/linux/multipath.h |  38 ++++++++++++++
 lib/multipath.c           | 104 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)
include/linux/multipath.h +38 -0
index 407d985cd31f5..ca6e02505be43 100644
@@ -5,11 +5,19 @@
 #include <linux/blkdev.h>
 #include <linux/srcu.h>
 
+extern const struct block_device_operations mpath_ops;
+
 struct mpath_device {
+	struct mpath_head	*mpath_head;
 	struct list_head	siblings;
 	struct gendisk		*disk;
 };
 
+struct mpath_head_template {
+};
+
+#define MPATH_HEAD_DISK_LIVE 			0
+
 struct mpath_head {
 	struct srcu_struct	srcu;
 	struct list_head	dev_list;	/* list of all mpath_devs */
@@ -17,12 +25,42 @@ struct mpath_head {
 
 	refcount_t		refcount;
 
+	unsigned long		flags;
+	struct gendisk		*disk;
+	struct work_struct	partition_scan_work;
+	struct device		*parent;
+	const struct attribute_group 		**disk_groups;
+	const struct mpath_head_template	*mpdt;
 	struct mpath_device __rcu 		*current_path[MAX_NUMNODES];
 };
 
+static inline struct mpath_head *mpath_bd_device_to_head(struct device *dev)
+{
+	return dev_get_drvdata(dev);
+}
+
+static inline struct mpath_head *mpath_gendisk_to_head(struct gendisk *disk)
+{
+	return mpath_bd_device_to_head(disk_to_dev(disk));
+}
+
 int mpath_get_head(struct mpath_head *mpath_head);
 void mpath_put_head(struct mpath_head *mpath_head);
 int mpath_head_init(struct mpath_head *mpath_head);
 void mpath_head_uninit(struct mpath_head *mpath_head);
 
+void mpath_put_disk(struct mpath_head *mpath_head);
+void mpath_remove_disk(struct mpath_head *mpath_head);
+int mpath_alloc_head_disk(struct mpath_head *mpath_head,
+			struct queue_limits *lim, int numa_node);
+void mpath_device_set_live(struct mpath_device *mpath_device);
+
+static inline bool is_mpath_disk(struct gendisk *disk)
+{
+	#if IS_ENABLED(CONFIG_LIBMULTIPATH)
+	return disk->fops == &mpath_ops;
+	#else
+	return false;
+	#endif
+}
 #endif // _LIBMULTIPATH_H
lib/multipath.c +104 -0

116 lines not on this page · raw message

Signature · 1 line
2.43.7
#3 lore raw

Re: [PATCH v6 02/27] libmultipath: Add basic gendisk support

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The block_device_operations struct `mpath_ops` lacks the mandatory `submit_bio` callback, causing…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The block_device_operations struct mpath_ops lacks the mandatory submit_bio callback, causing device_add_disk() to unconditionally fail and preventing the multipath disk from functioning.
- [High] Re-using gendisk after device_add_disk() failure violates the driver model and causes kobject corruption.
- [High] mpath_head_uninit() synchronously blocks on open file descriptors, causing device removal to hang indefinitely if userspace holds the device open.
--

--- Patch [2]: [PATCH v6 02/27] libmultipath: Add basic gendisk support --- commit 0995e161443bbd8d5024681ae6aeaebc37bafd47
Author: John Garry <[email protected]>

libmultipath: Add basic gendisk support

This commit adds support to allocate and free a multipath gendisk. It introduces struct mpath_head_template as a method for drivers to provide custom multipath functionality.

Quoted 3 lines
lib/multipath.c +0 -0

[ ... ]

Quoted 1 line
lib/multipath.c +0 -0
@@ -36,6 +36,107 @@ void mpath_head_uninit(struct mpath_head *mpath_head)

[ ... ]

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 54

Quoted 2 lines
lib/multipath.c +2 -0
+const struct block_device_operations mpath_ops = {
+	.owner          = THIS_MODULE,

[Severity: High]
Does setting the owner to THIS_MODULE (libmultipath) rather than the caller module (such as nvme_core) introduce a regression where the caller module can be unloaded while the disk is held open by userspace? If the caller's module exit calls mpath_head_uninit(), could it hang indefinitely in an uninterruptible wait on the refcount, permanently deadlocking module_mutex?

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 57

Quoted 3 lines
lib/multipath.c +3 -0
+	.open		= mpath_bdev_open,
+	.release	= mpath_bdev_release,
+};

[Severity: High]
Is this regression missing a submit_bio callback in mpath_ops? For bio-based queues, the block layer's __add_disk() mandates that disk->fops->submit_bio is present. If it is absent, device_add_disk() unconditionally aborts and returns -EINVAL, which prevents the multipath block device from registering.

[ ... ]

Quoted 2 lines
lib/multipath.c +2 -0
+void mpath_device_set_live(struct mpath_device *mpath_device)
+{

[ ... ]

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 134

Quoted 8 lines
lib/multipath.c +8 -0
+	if (!test_and_set_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) {
+		dev_set_drvdata(disk_to_dev(mpath_head->disk), mpath_head);
+		ret = device_add_disk(mpath_head->parent, mpath_head->disk,
+				mpath_head->disk_groups);
+		if (ret) {
+			clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags);
+			return;
+		}

[Severity: High]
If device_add_disk() fails, clearing the live flag without freeing the disk leaves the object intact. When this function is called again for another path, won't it retry device_add_disk() on the exact same gendisk object? Because the driver model forbids calling device_add() more than once on the same device object and requires dropping the device upon failure, could this regression result in kobject BUGs or memory corruption?

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

Re: [PATCH v6 02/27] libmultipath: Add basic gendisk support

Quoted 22 lines · On 29/07/2026 12:20, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The block_device_operations struct mpath_ops lacks the mandatory submit_bio callback, causing device_add_disk() to unconditionally fail and preventing the multipath disk from functioning.
- [High] Re-using gendisk after device_add_disk() failure violates the driver model and causes kobject corruption.
- [High] mpath_head_uninit() synchronously blocks on open file descriptors, causing device removal to hang indefinitely if userspace holds the device open.
--

--- Patch [2]: [PATCH v6 02/27] libmultipath: Add basic gendisk support --- commit 0995e161443bbd8d5024681ae6aeaebc37bafd47
Author: John Garry <[email protected]>

libmultipath: Add basic gendisk support

This commit adds support to allocate and free a multipath gendisk. It introduces struct mpath_head_template as a method for drivers to provide custom multipath functionality.

Quoted 3 lines
lib/multipath.c +0 -0

[ ... ]

Quoted 1 line
lib/multipath.c +0 -0
@@ -36,6 +36,107 @@ void mpath_head_uninit(struct mpath_head *mpath_head)

[ ... ]

Quoted 9 lines

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 54

Quoted 2 lines
lib/multipath.c +2 -0
+const struct block_device_operations mpath_ops = {
+	.owner          = THIS_MODULE,

[Severity: High]
Does setting the owner to THIS_MODULE (libmultipath) rather than the caller module (such as nvme_core) introduce a regression where the caller module can be unloaded while the disk is held open by userspace? If the caller's module exit calls mpath_head_uninit(), could it hang indefinitely in an uninterruptible wait on the refcount, permanently deadlocking module_mutex?

the caller module should call mpath_head_uninit(), which ensures all references are dropped

Quoted 10 lines

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 57

Quoted 4 lines
lib/multipath.c +3 -0
+	.open		= mpath_bdev_open,
+	.release	= mpath_bdev_release,
+};

[Severity: High]
Is this regression missing a submit_bio callback in mpath_ops? For bio-based queues, the block layer's __add_disk() mandates that disk->fops->submit_bio is present. If it is absent, device_add_disk() unconditionally aborts and returns -EINVAL, which prevents the multipath block device from registering.

it's added later

Quoted 5 lines

[ ... ]

Quoted 2 lines
lib/multipath.c +2 -0
+void mpath_device_set_live(struct mpath_device *mpath_device)
+{

[ ... ]

Quoted 16 lines

lib/multipath.c void mpath_head_uninit(struct mpath_head *mpath_head) · line 134

Quoted 8 lines
lib/multipath.c +8 -0
+	if (!test_and_set_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) {
+		dev_set_drvdata(disk_to_dev(mpath_head->disk), mpath_head);
+		ret = device_add_disk(mpath_head->parent, mpath_head->disk,
+				mpath_head->disk_groups);
+		if (ret) {
+			clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags);
+			return;
+		}

[Severity: High]
If device_add_disk() fails, clearing the live flag without freeing the disk leaves the object intact. When this function is called again for another path, won't it retry device_add_disk() on the exact same gendisk object? Because the driver model forbids calling device_add() more than once on the same device object and requires dropping the device upon failure, could this regression result in kobject BUGs or memory corruption?

This is same as NVMe, so I won't change it now

Quoted 1 line
author
#5 lore raw

PATCH v6 3/27 libmultipath: Add path selection support

In reply to John Garry

From: John Garry <[email protected]>

Add code for path selection.

NVMe ANA is abstracted into enum mpath_access_state. The motivation here is so that SCSI ALUA can be used. Callbacks .is_disabled and .is_optimized are added to get the path access state.

Path selection modes round-robin, NUMA, and queue-depth are added, same as NVMe supports.

NVMe has almost like-for-like equivalents here:
- __mpath_find_path() -> __nvme_find_path()
- mpath_find_path() -> nvme_find_path()

and similar for all introduced callee functions.

Functions mpath_set_iopolicy() and mpath_get_iopolicy() are added for setting default iopolicy.

---
 include/linux/multipath.h |  33 +++++
 lib/multipath.c           | 253 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 285 insertions(+), 1 deletion(-)
include/linux/multipath.h +33 -0
index ca6e02505be43..9bbab0aa8961a 100644
@@ -7,13 +7,31 @@
 
 extern const struct block_device_operations mpath_ops;
 
+enum mpath_iopolicy_e {
+	MPATH_IOPOLICY_NUMA,
+	MPATH_IOPOLICY_RR,
+	MPATH_IOPOLICY_QD,
+};
+
+enum mpath_access_state {
+	MPATH_STATE_OPTIMIZED,
+	MPATH_STATE_NONOPTIMIZED,
+	MPATH_STATE_OTHER
+};
+
 struct mpath_device {
 	struct mpath_head	*mpath_head;
 	struct list_head	siblings;
 	struct gendisk		*disk;
+	int			numa_node;
+	atomic_t		*nr_active;
+	enum mpath_access_state access_state;
 };
 
 struct mpath_head_template {
+	bool (*is_disabled)(struct mpath_device *);
+	bool (*is_optimized)(struct mpath_device *);
+	const struct attribute_group **device_groups;
 };
 
 #define MPATH_HEAD_DISK_LIVE 			0
@@ -25,6 +43,7 @@ struct mpath_head {
 
 	refcount_t		refcount;
 
+	enum mpath_iopolicy_e	*iopolicy;
 	unsigned long		flags;
 	struct gendisk		*disk;
 	struct work_struct	partition_scan_work;
@@ -44,6 +63,14 @@ static inline struct mpath_head *mpath_gendisk_to_head(struct gendisk *disk)
 	return mpath_bd_device_to_head(disk_to_dev(disk));
 }
 
+static inline enum mpath_iopolicy_e mpath_read_iopolicy(
+			struct mpath_head *mpath_head)
+{
+	return READ_ONCE(*mpath_head->iopolicy);
+}
+void mpath_synchronize(struct mpath_head *mpath_head);
+int mpath_set_iopolicy(const char *str, enum mpath_iopolicy_e *iopolicy);
+int mpath_get_iopolicy(char *buf, int iopolicy);
 int mpath_get_head(struct mpath_head *mpath_head);
 void mpath_put_head(struct mpath_head *mpath_head);
 int mpath_head_init(struct mpath_head *mpath_head);
@@ -63,4 +90,10 @@ static inline bool is_mpath_disk(struct gendisk *disk)
 	return false;
 	#endif
 }
+
+static inline bool mpath_qd_iopolicy(enum mpath_iopolicy_e *iopolicy)
+{
+	return READ_ONCE(*iopolicy) == MPATH_IOPOLICY_QD;
+}
+
 #endif // _LIBMULTIPATH_H
lib/multipath.c +252 -1

279 lines not on this page · raw message

Signature · 1 line
2.43.7
#6 lore raw

Re: [PATCH v6 03/27] libmultipath: Add path selection support

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

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

New issues:
- [High] Round-robin path selection behaves as reverse round-robin for non-optimized paths due to a missing loop break.
- [High] Unsafe and logically flawed use of list_is_singular on an RCU-protected list routes I/O to a removed device.

Pre-existing issues:
- [High] mpath_head_uninit() blocks indefinitely waiting for user-space to close the block device. --

--- Patch [3]: [PATCH v6 03/27] libmultipath: Add path selection support --- commit 70a41be1b0a598af8cbd34732b889f79118d17ac
Author: John Garry <[email protected]>

libmultipath: Add path selection support

This commit adds code for path selection support in libmultipath. It abstracts NVMe ANA into mpath_access_state to allow SCSI ALUA usage and adds round-robin, NUMA, and queue-depth selection modes. It also introduces functions to set and get the default I/O policy.

Quoted 4 lines
lib/multipath.c +0 -0
index 1cf20b0f62d07..26714116023a3 100644

[ ... ]

Quoted 5 lines
lib/multipath.c +4 -0
@@ -7,8 +7,245 @@
+static struct mpath_device *mpath_round_robin_path(
+				struct mpath_head *mpath_head)
+{
+	struct mpath_device *mpath_device, *found = NULL;

lib/multipath.c line 20

Quoted 10 lines
lib/multipath.c +10 -0
+	int node = numa_node_id();
+	enum mpath_access_state access_state_old;
+	struct mpath_device *old =
+			srcu_dereference(mpath_head->current_path[node],
+				&mpath_head->srcu);
+
+	if (unlikely(!old))
+		return __mpath_find_path(mpath_head, node);
+
+	if (list_is_singular(&mpath_head->dev_list)) {

[Severity: High]
Does using list_is_singular() here on an SRCU-protected list introduce a data race on head->prev?

Additionally, if old is concurrently removed and exactly one other active device remains in the list, list_is_singular() evaluates to true. Will this cause the function to return the unlinked old path instead of the valid active path?

Quoted 7 lines
lib/multipath.c +7 -0
+		if (mpath_path_is_disabled(mpath_head, old))
+			return NULL;
+		return old;
+	}
+
+	for (mpath_device = mpath_next_dev(mpath_head, old);
+	    mpath_device && mpath_device != old;

lib/multipath.c line 149

Quoted 10 lines
lib/multipath.c +10 -0
+	    mpath_device = mpath_next_dev(mpath_head, mpath_device)) {
+
+		if (mpath_path_is_disabled(mpath_head, mpath_device))
+			continue;
+		if (mpath_device->access_state == MPATH_STATE_OPTIMIZED) {
+			found = mpath_device;
+			goto out;
+		}
+		if (mpath_device->access_state == MPATH_STATE_NONOPTIMIZED)
+			found = mpath_device;

[Severity: High]
For the MPATH_STATE_NONOPTIMIZED case, found is overwritten unconditionally. Because there is no loop break or check like if (!found), will this evaluate multiple non-optimized paths and incorrectly select the last available one rather than the next available one?

lib/multipath.c line 38

Quoted 1 line
lib/multipath.c +1 -0
+	}

[ ... ]

[Severity: High]
This is a pre-existing issue, but I noticed a potential problem during teardown in mpath_head_uninit():

lib/multipath.c:mpath_head_uninit() {
        ...
        if (!refcount_dec_and_test(refcount))
                wait_var_event(refcount, !refcount_read(refcount));
}

Since a reference is taken for every user-space open, can wait_var_event() block indefinitely if a user process holds the device open while the driver is unbound or the device is hot-unplugged?

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

Re: [PATCH v6 03/27] libmultipath: Add path selection support

Quoted 39 lines · On 29/07/2026 12:33, [email protected] wrote:

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

New issues:
- [High] Round-robin path selection behaves as reverse round-robin for non-optimized paths due to a missing loop break.
- [High] Unsafe and logically flawed use of list_is_singular on an RCU-protected list routes I/O to a removed device.

Pre-existing issues:
- [High] mpath_head_uninit() blocks indefinitely waiting for user-space to close the block device. --

--- Patch [3]: [PATCH v6 03/27] libmultipath: Add path selection support --- commit 70a41be1b0a598af8cbd34732b889f79118d17ac
Author: John Garry <[email protected]>

libmultipath: Add path selection support

This commit adds code for path selection support in libmultipath. It abstracts NVMe ANA into mpath_access_state to allow SCSI ALUA usage and adds round-robin, NUMA, and queue-depth selection modes. It also introduces functions to set and get the default I/O policy.

Quoted 4 lines
lib/multipath.c +0 -0
index 1cf20b0f62d07..26714116023a3 100644

[ ... ]

Quoted 11 lines
lib/multipath.c +10 -0
@@ -7,8 +7,245 @@
+static struct mpath_device *mpath_round_robin_path(
+				struct mpath_head *mpath_head)
+{
+	struct mpath_device *mpath_device, *found = NULL;
+	int node = numa_node_id();
+	enum mpath_access_state access_state_old;
+	struct mpath_device *old =
+			srcu_dereference(mpath_head->current_path[node],
+				&mpath_head->srcu);
+
Quoted 8 lines

lib/multipath.c line 132

Quoted 4 lines
lib/multipath.c +4 -0
+	if (unlikely(!old))
+		return __mpath_find_path(mpath_head, node);
+
+	if (list_is_singular(&mpath_head->dev_list)) {

[Severity: High]
Does using list_is_singular() here on an SRCU-protected list introduce a data race on head->prev?

locking is added later

Quoted 5 lines

Additionally, if old is concurrently removed and exactly one other active device remains in the list, list_is_singular() evaluates to true. Will this cause the function to return the unlinked old path instead of the valid active path?

as above

Quoted 16 lines
Quoted 16 lines
lib/multipath.c +15 -0
+		if (mpath_path_is_disabled(mpath_head, old))
+			return NULL;
+		return old;
+	}
+
+	for (mpath_device = mpath_next_dev(mpath_head, old);
+	    mpath_device && mpath_device != old;
+	    mpath_device = mpath_next_dev(mpath_head, mpath_device)) {
+
+		if (mpath_path_is_disabled(mpath_head, mpath_device))
+			continue;
+		if (mpath_device->access_state == MPATH_STATE_OPTIMIZED) {
+			found = mpath_device;
+			goto out;
+		}
Quoted 8 lines

lib/multipath.c line 149

Quoted 2 lines
lib/multipath.c +2 -0
+		if (mpath_device->access_state == MPATH_STATE_NONOPTIMIZED)
+			found = mpath_device;

[Severity: High]
For the MPATH_STATE_NONOPTIMIZED case, found is overwritten unconditionally. Because there is no loop break or check like if (!found), will this evaluate multiple non-optimized paths and incorrectly select the last available one rather than the next available one?

I think so, but no one cares about non-optimized. Again, this is same as NVMe code.

Quoted 15 lines

lib/multipath.c line 38

Quoted 2 lines
lib/multipath.c +1 -0
+	}

[ ... ]

[Severity: High]
This is a pre-existing issue, but I noticed a potential problem during teardown in mpath_head_uninit():

lib/multipath.c:mpath_head_uninit() {
         ...
         if (!refcount_dec_and_test(refcount))
                 wait_var_event(refcount, !refcount_read(refcount));
}
Quoted 3 lines

Since a reference is taken for every user-space open, can wait_var_event() block indefinitely if a user process holds the device open while the driver is unbound or the device is hot-unplugged?

yes, but that is just the way it is - userspace needs to drop the ref

Quoted 1 line
author
#8 lore raw

PATCH v6 4/27 libmultipath: Add bio handling

In reply to John Garry

From: John Garry <[email protected]>

Add support to submit a bio per-path. In addition, for failover, add support to requeue a failed bio.

NVMe has almost like-for-like equivalents here:
- nvme_available_path() -> mpath_available_path()
- nvme_requeue_work() -> mpath_requeue_work()
- nvme_ns_head_submit_bio() -> mpath_bdev_submit_bio()

For failover, a driver may want to re-submit a bio, so add support to clone a bio prior to submission.

A bio which is submitted to a per-path device has flag REQ_MPATH set, same as what is done for NVMe with REQ_NVME_MPATH.

---
 include/linux/multipath.h |  22 ++++++++
 lib/multipath.c           | 108 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 128 insertions(+), 2 deletions(-)
include/linux/multipath.h +22 -0
index 9bbab0aa8961a..374f0531f7e07 100644
@@ -3,6 +3,7 @@
 #define _LIBMULTIPATH_H
 
 #include <linux/blkdev.h>
+#include <linux/blk-mq.h>
 #include <linux/srcu.h>
 
 extern const struct block_device_operations mpath_ops;
@@ -29,8 +30,10 @@ struct mpath_device {
 };
 
 struct mpath_head_template {
+	bool (*available_path)(struct mpath_device *);
 	bool (*is_disabled)(struct mpath_device *);
 	bool (*is_optimized)(struct mpath_device *);
+	struct bio *(*clone_bio)(struct bio *);
 	const struct attribute_group **device_groups;
 };
 
@@ -44,6 +47,14 @@ struct mpath_head {
 	refcount_t		refcount;
 
 	enum mpath_iopolicy_e	*iopolicy;
+
+	struct bio_list		requeue_list; /* list for requeing bio */
+	spinlock_t		requeue_lock;
+	struct work_struct	requeue_work; /* work struct for requeue */
+
+	atomic_long_t		requeue_no_usable_path_cnt;
+	atomic_long_t		fail_no_avail_path_cnt;
+
 	unsigned long		flags;
 	struct gendisk		*disk;
 	struct work_struct	partition_scan_work;
@@ -53,6 +64,13 @@ struct mpath_head {
 	struct mpath_device __rcu 		*current_path[MAX_NUMNODES];
 };
 
+#define REQ_MPATH		REQ_DRV
+
+static inline bool is_mpath_request(struct request *req)
+{
+	return req->cmd_flags & REQ_MPATH;
+}
+
 static inline struct mpath_head *mpath_bd_device_to_head(struct device *dev)
 {
 	return dev_get_drvdata(dev);
@@ -96,4 +114,8 @@ static inline bool mpath_qd_iopolicy(enum mpath_iopolicy_e *iopolicy)
 	return READ_ONCE(*iopolicy) == MPATH_IOPOLICY_QD;
 }
 
+static inline void mpath_schedule_requeue_work(struct mpath_head *mpath_head)
+{
+	kblockd_schedule_work(&mpath_head->requeue_work);
+}
 #endif // _LIBMULTIPATH_H
lib/multipath.c +106 -2

168 lines not on this page · raw message

Signature · 1 line
2.43.7
author
#9 lore raw

PATCH v6 5/27 libmultipath: Add support for mpath_device management

In reply to John Garry

Add support to add or remove a mpath_device as a path.

NVMe has almost like-for-like equivalents here:
- nvme_mpath_clear_current_path() -> mpath_clear_current_path()
- nvme_mpath_add_sysfs_link() -> mpath_add_sysfs_link()
- nvme_mpath_remove_sysfs_link() -> mpath_remove_sysfs_link()
- nvme_mpath_revalidate_paths() -> mpath_revalidate_paths()

The functionality in mpath_clear_paths() and mpath_synchronize() have the same pattern which is frequently used in the NVMe code.

---
 include/linux/multipath.h |  12 +++
 lib/multipath.c           | 154 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+)
include/linux/multipath.h +12 -0
index 374f0531f7e07..033ee8d54bbe9 100644
@@ -20,10 +20,13 @@ enum mpath_access_state {
 	MPATH_STATE_OTHER
 };
 
+#define MPATH_DEVICE_SYSFS_ATTR_LINK      0
+
 struct mpath_device {
 	struct mpath_head	*mpath_head;
 	struct list_head	siblings;
 	struct gendisk		*disk;
+	unsigned long		flags;
 	int			numa_node;
 	atomic_t		*nr_active;
 	enum mpath_access_state access_state;
@@ -89,6 +92,15 @@ static inline enum mpath_iopolicy_e mpath_read_iopolicy(
 void mpath_synchronize(struct mpath_head *mpath_head);
 int mpath_set_iopolicy(const char *str, enum mpath_iopolicy_e *iopolicy);
 int mpath_get_iopolicy(char *buf, int iopolicy);
+bool mpath_clear_current_path(struct mpath_device *mpath_device);
+int mpath_add_device(struct mpath_device *mpath_device, struct gendisk *disk,
+		int numa_node, atomic_t *nr_active);
+bool mpath_delete_device(struct mpath_device *mpath_device);
+bool mpath_head_devices_empty(struct mpath_head *mpath_head);
+void mpath_clear_paths(struct mpath_head *mpath_head);
+void mpath_revalidate_paths(struct mpath_head *mpath_head);
+void mpath_add_sysfs_link(struct mpath_head *mpath_head);
+void mpath_remove_sysfs_link(struct mpath_device *mpath_device);
 int mpath_get_head(struct mpath_head *mpath_head);
 void mpath_put_head(struct mpath_head *mpath_head);
 int mpath_head_init(struct mpath_head *mpath_head);
lib/multipath.c +154 -0

172 lines not on this page · raw message

Signature · 1 line
2.43.7
#10 lore raw

Re: [PATCH v6 05/27] libmultipath: Add support for mpath_device management

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] RCU Use-After-Free Vulnerability in mpath_delete_device() - [High] NULL Pointer Dereference in…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] RCU Use-After-Free Vulnerability in mpath_delete_device()
- [High] NULL Pointer Dereference in sysfs link management
- [High] Race condition causing sysfs link leak and dangling kobject references
- [Medium] TOCTOU race in mpath_remove_sysfs_link
--

--- Patch [5]: [PATCH v6 05/27] libmultipath: Add support for mpath_device management --- commit a32651ca51a97ca7e06ffd62321d0190b9e9cfa0
Author: John Garry <[email protected]>

libmultipath: Add support for mpath_device management

Add support to add or remove a mpath_device as a path.

NVMe has almost like-for-like equivalents here:
- nvme_mpath_clear_current_path() -> mpath_clear_current_path()
- nvme_mpath_add_sysfs_link() -> mpath_add_sysfs_link()
- nvme_mpath_remove_sysfs_link() -> mpath_remove_sysfs_link()
- nvme_mpath_revalidate_paths() -> mpath_revalidate_paths()

The functionality in mpath_clear_paths() and mpath_synchronize() have the same pattern which is frequently used in the NVMe code.

Quoted 4 lines
lib/multipath.c +0 -0
index 93b82eea7d38b..6b8f6e846987a 100644

[ ... ]

Quoted 4 lines
lib/multipath.c +0 -0
@@ -51,6 +51,87 @@ void mpath_synchronize(struct mpath_head *mpath_head)
 }
 EXPORT_SYMBOL_GPL(mpath_synchronize);
 

[ ... ]

lib/multipath.c void mpath_synchronize(struct mpath_head *mpath_head) · line 82

Quoted 10 lines
lib/multipath.c +10 -0
+bool mpath_delete_device(struct mpath_device *mpath_device)
+{
+	bool empty;
+
+	mutex_lock(&mpath_device->mpath_head->lock);
+	list_del_rcu(&mpath_device->siblings);
+	empty = list_empty(&mpath_device->mpath_head->dev_list);
+	mutex_unlock(&mpath_device->mpath_head->lock);
+
+	mpath_synchronize(mpath_device->mpath_head);

[Severity: High]
Does this sequence safely handle RCU readers accessing current_path?

Because mpath_delete_device() removes the device from dev_list and immediately calls mpath_synchronize() without clearing it from mpath_head->current_path, the RCU-protected current_path array may still hold a pointer to the device. Lockless readers can then dereference the freed device.

If callers attempt to clear current_path before calling mpath_delete_device(), couldn't the pointer be repopulated from dev_list by concurrent readers during the race window? If cleared after, the synchronization has already passed, providing no protection against use-after-free.

Quoted 4 lines
lib/multipath.c +4 -0
+
+	return empty;
+}
+EXPORT_SYMBOL_GPL(mpath_delete_device);

[ ... ]

Quoted 8 lines
lib/multipath.c +4 -0
@@ -489,6 +572,77 @@ void mpath_device_set_live(struct mpath_device *mpath_device)
 }
 EXPORT_SYMBOL_GPL(mpath_device_set_live);
 
+void mpath_add_sysfs_link(struct mpath_head *mpath_head)
+{
+	struct device *target;
+	struct device *source;

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 588

Quoted 10 lines
lib/multipath.c +10 -0
+	int rc, srcu_idx;
+	struct kobject *mpath_gd_kobj;
+	struct mpath_device *mpath_device;
+
+	/*
+	 * Ensure head disk node is already added otherwise we may get invalid
+	 * kobj for head disk node
+	 */
+	if (!test_bit(GD_ADDED, &mpath_head->disk->state))
+		return;

[Severity: High]
Is it possible for mpath_head->disk to be NULL here?

When a multi-path head disk is removed, mpath_put_disk() is called, which sets mpath_head->disk = NULL. If a path subsequently goes live, mpath_add_sysfs_link() appears to dereference it without checking for NULL.

Could this cause a deterministic oops when path teardown outlives the head, such as during an abrupt controller removal?

Quoted 9 lines
lib/multipath.c +9 -0
+
+	mpath_gd_kobj = &disk_to_dev(mpath_head->disk)->kobj;
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+
+	list_for_each_entry_srcu(mpath_device, &mpath_head->dev_list, siblings,
+				 srcu_read_lock_held(&mpath_head->srcu)) {
+		if (!test_bit(GD_ADDED, &mpath_device->disk->state))
+			continue;
+

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 607

Quoted 10 lines
lib/multipath.c +10 -0
+		if (test_and_set_bit(MPATH_DEVICE_SYSFS_ATTR_LINK,
+					&mpath_device->flags))
+			continue;
+
+		target = disk_to_dev(mpath_device->disk);
+		source = disk_to_dev(mpath_head->disk);
+		/*
+		 * Create sysfs link from head gendisk kobject @kobj to the
+		 * ns path gendisk kobject @target->kobj.
+		 */

[Severity: High]
Could this re-create a sysfs link for a device that is in the middle of being deleted?

During path removal, mpath_remove_sysfs_link() clears the MPATH_DEVICE_SYSFS_ATTR_LINK bit before mpath_delete_device() removes the device from the RCU dev_list.

If a different path concurrently goes live, mpath_add_sysfs_link() iterates over dev_list, observes the cleared bit on the dying device, and might mistakenly re-create the sysfs link. When the dying device is finally removed and freed, the re-created sysfs link is permanently leaked and points to a freed block device kobject.

Quoted 2 lines
lib/multipath.c +2 -0
+		rc = sysfs_add_link_to_group(mpath_gd_kobj, "multipath",
+				&target->kobj, dev_name(target));

[ ... ]

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 637

Quoted 11 lines
lib/multipath.c +11 -0
+void mpath_remove_sysfs_link(struct mpath_device *mpath_device)
+{
+	struct device *target;
+	struct kobject *mpath_gd_kobj;
+	struct mpath_head *mpath_head = mpath_device->mpath_head;
+
+	if (!test_bit(MPATH_DEVICE_SYSFS_ATTR_LINK, &mpath_device->flags))
+		return;
+
+	target = disk_to_dev(mpath_device->disk);
+	mpath_gd_kobj = &disk_to_dev(mpath_head->disk)->kobj;

[Severity: High]
Does mpath_head->disk need to be checked for NULL before dereferencing it here?

Similar to mpath_add_sysfs_link(), if underlying paths are detached after mpath_put_disk() sets mpath_head->disk = NULL, this will cause a NULL pointer dereference during teardown.

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 642

Quoted 5 lines
lib/multipath.c +5 -0
+
+	sysfs_remove_link_from_group(mpath_gd_kobj, "multipath",
+			dev_name(target));
+
+	clear_bit(MPATH_DEVICE_SYSFS_ATTR_LINK, &mpath_device->flags);

[Severity: Medium]
Is there a race condition between test_bit() at the start of this function and clear_bit() here?

A concurrent execution for the same device could pass the test_bit() check and issue duplicate removals to the sysfs core. Should this diverging non-atomic pattern be replaced with test_and_clear_bit() to match the safe pattern used in mpath_add_sysfs_link()?

Quoted 1 line

+}

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

Re: [PATCH v6 05/27] libmultipath: Add support for mpath_device management

Quoted 52 lines · On 29/07/2026 12:57, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] RCU Use-After-Free Vulnerability in mpath_delete_device()
- [High] NULL Pointer Dereference in sysfs link management
- [High] Race condition causing sysfs link leak and dangling kobject references
- [Medium] TOCTOU race in mpath_remove_sysfs_link
--

--- Patch [5]: [PATCH v6 05/27] libmultipath: Add support for mpath_device management --- commit a32651ca51a97ca7e06ffd62321d0190b9e9cfa0
Author: John Garry <[email protected]>

libmultipath: Add support for mpath_device management

Add support to add or remove a mpath_device as a path.

NVMe has almost like-for-like equivalents here:
- nvme_mpath_clear_current_path() -> mpath_clear_current_path()
- nvme_mpath_add_sysfs_link() -> mpath_add_sysfs_link()
- nvme_mpath_remove_sysfs_link() -> mpath_remove_sysfs_link()
- nvme_mpath_revalidate_paths() -> mpath_revalidate_paths()

The functionality in mpath_clear_paths() and mpath_synchronize() have the same pattern which is frequently used in the NVMe code.

Quoted 4 lines
lib/multipath.c +0 -0
index 93b82eea7d38b..6b8f6e846987a 100644

[ ... ]

Quoted 4 lines
lib/multipath.c +0 -0
@@ -51,6 +51,87 @@ void mpath_synchronize(struct mpath_head *mpath_head)
  }
  EXPORT_SYMBOL_GPL(mpath_synchronize);
 

[ ... ]

lib/multipath.c void mpath_synchronize(struct mpath_head *mpath_head) · line 82

Quoted 10 lines
lib/multipath.c +10 -0
+bool mpath_delete_device(struct mpath_device *mpath_device)
+{
+	bool empty;
+
+	mutex_lock(&mpath_device->mpath_head->lock);
+	list_del_rcu(&mpath_device->siblings);
+	empty = list_empty(&mpath_device->mpath_head->dev_list);
+	mutex_unlock(&mpath_device->mpath_head->lock);
+
+	mpath_synchronize(mpath_device->mpath_head);

[Severity: High]
Does this sequence safely handle RCU readers accessing current_path?

Quoted 5 lines

Because mpath_delete_device() removes the device from dev_list and immediately calls mpath_synchronize() without clearing it from mpath_head->current_path, the RCU-protected current_path array may still hold a pointer to the device. Lockless readers can then dereference the freed device.

it is removed in sd_mpath_remove() -> mpath_clear_current_path()

Quoted 36 lines

If callers attempt to clear current_path before calling mpath_delete_device(), couldn't the pointer be repopulated from dev_list by concurrent readers during the race window? If cleared after, the synchronization has already passed, providing no protection against use-after-free.

Quoted 4 lines
lib/multipath.c +4 -0
+
+	return empty;
+}
+EXPORT_SYMBOL_GPL(mpath_delete_device);

[ ... ]

Quoted 8 lines
lib/multipath.c +4 -0
@@ -489,6 +572,77 @@ void mpath_device_set_live(struct mpath_device *mpath_device)
  }
  EXPORT_SYMBOL_GPL(mpath_device_set_live);
 
+void mpath_add_sysfs_link(struct mpath_head *mpath_head)
+{
+	struct device *target;
+	struct device *source;

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 588

Quoted 10 lines
lib/multipath.c +10 -0
+	int rc, srcu_idx;
+	struct kobject *mpath_gd_kobj;
+	struct mpath_device *mpath_device;
+
+	/*
+	 * Ensure head disk node is already added otherwise we may get invalid
+	 * kobj for head disk node
+	 */
+	if (!test_bit(GD_ADDED, &mpath_head->disk->state))
+		return;

[Severity: High]
Is it possible for mpath_head->disk to be NULL here?

Quoted 6 lines

When a multi-path head disk is removed, mpath_put_disk() is called, which sets mpath_head->disk = NULL. If a path subsequently goes live, mpath_add_sysfs_link() appears to dereference it without checking for NULL.

Could this cause a deterministic oops when path teardown outlives the head, such as during an abrupt controller removal?

if a new device is going live, then mpath_head->disk has to be present as it has a reference to it

Quoted 29 lines
Quoted 10 lines
lib/multipath.c +9 -0
+
+	mpath_gd_kobj = &disk_to_dev(mpath_head->disk)->kobj;
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+
+	list_for_each_entry_srcu(mpath_device, &mpath_head->dev_list, siblings,
+				 srcu_read_lock_held(&mpath_head->srcu)) {
+		if (!test_bit(GD_ADDED, &mpath_device->disk->state))
+			continue;
+

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 607

Quoted 10 lines
lib/multipath.c +10 -0
+		if (test_and_set_bit(MPATH_DEVICE_SYSFS_ATTR_LINK,
+					&mpath_device->flags))
+			continue;
+
+		target = disk_to_dev(mpath_device->disk);
+		source = disk_to_dev(mpath_head->disk);
+		/*
+		 * Create sysfs link from head gendisk kobject @kobj to the
+		 * ns path gendisk kobject @target->kobj.
+		 */

[Severity: High]
Could this re-create a sysfs link for a device that is in the middle of being deleted?

During path removal, mpath_remove_sysfs_link() clears the MPATH_DEVICE_SYSFS_ATTR_LINK bit before mpath_delete_device() removes the device from the RCU dev_list.

Quoted 5 lines

If a different path concurrently goes live, mpath_add_sysfs_link() iterates over dev_list, observes the cleared bit on the dying device, and might mistakenly re-create the sysfs link. When the dying device is finally removed and freed, the re-created sysfs link is permanently leaked and points to a freed block device kobject.

we call mpath_delete_device() before mpath_remove_sysfs_link() in sd_mpath_remove(), so this should not happen

Quoted 22 lines
Quoted 3 lines
lib/multipath.c +2 -0
+		rc = sysfs_add_link_to_group(mpath_gd_kobj, "multipath",
+				&target->kobj, dev_name(target));

[ ... ]

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 637

Quoted 11 lines
lib/multipath.c +11 -0
+void mpath_remove_sysfs_link(struct mpath_device *mpath_device)
+{
+	struct device *target;
+	struct kobject *mpath_gd_kobj;
+	struct mpath_head *mpath_head = mpath_device->mpath_head;
+
+	if (!test_bit(MPATH_DEVICE_SYSFS_ATTR_LINK, &mpath_device->flags))
+		return;
+
+	target = disk_to_dev(mpath_device->disk);
+	mpath_gd_kobj = &disk_to_dev(mpath_head->disk)->kobj;

[Severity: High]
Does mpath_head->disk need to be checked for NULL before dereferencing it here?

Quoted 3 lines

Similar to mpath_add_sysfs_link(), if underlying paths are detached after mpath_put_disk() sets mpath_head->disk = NULL, this will cause a NULL pointer dereference during teardown.

the paths cannot be detached after mpath_put_disk()

Quoted 11 lines

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 642

Quoted 6 lines
lib/multipath.c +5 -0
+
+	sysfs_remove_link_from_group(mpath_gd_kobj, "multipath",
+			dev_name(target));
+
+	clear_bit(MPATH_DEVICE_SYSFS_ATTR_LINK, &mpath_device->flags);

[Severity: Medium]
Is there a race condition between test_bit() at the start of this function and clear_bit() here?

Quoted 2 lines

A concurrent execution for the same device could pass the test_bit() check and issue duplicate removals to the sysfs core.

We don't call for the same device concurrently

Quoted 6 lines

Should this diverging
non-atomic pattern be replaced with test_and_clear_bit() to match the safe pattern used in mpath_add_sysfs_link()?

Quoted 1 line

+}

author
#12 lore raw

PATCH v6 6/27 libmultipath: Add delayed removal support

In reply to John Garry

From: John Garry <[email protected]>

Add support for delayed removal, same as exists for NVMe.

The purpose of this feature is to keep the multipath disk and cdev present for intermittent periods of no available path.

Helpers mpath_delayed_removal_secs_show() and mpath_delayed_removal_secs_store() may be used in the driver sysfs code.

The driver is responsible for supplying the removal work callback for the delayed work.

---
 include/linux/multipath.h | 18 ++++++++
 lib/multipath.c           | 91 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 108 insertions(+), 1 deletion(-)
include/linux/multipath.h +18 -0
index 033ee8d54bbe9..66b6ad9c2219a 100644
@@ -34,6 +34,7 @@ struct mpath_device {
 
 struct mpath_head_template {
 	bool (*available_path)(struct mpath_device *);
+	void (*remove_head)(struct mpath_head *);
 	bool (*is_disabled)(struct mpath_device *);
 	bool (*is_optimized)(struct mpath_device *);
 	struct bio *(*clone_bio)(struct bio *);
@@ -41,6 +42,7 @@ struct mpath_head_template {
 };
 
 #define MPATH_HEAD_DISK_LIVE 			0
+#define MPATH_HEAD_QUEUE_IF_NO_PATH		1
 
 struct mpath_head {
 	struct srcu_struct	srcu;
@@ -58,6 +60,10 @@ struct mpath_head {
 	atomic_long_t		requeue_no_usable_path_cnt;
 	atomic_long_t		fail_no_avail_path_cnt;
 
+	struct delayed_work	remove_work;
+	unsigned int		delayed_removal_secs;
+	struct module		*drv_module;
+
 	unsigned long		flags;
 	struct gendisk		*disk;
 	struct work_struct	partition_scan_work;
@@ -111,6 +117,11 @@ void mpath_remove_disk(struct mpath_head *mpath_head);
 int mpath_alloc_head_disk(struct mpath_head *mpath_head,
 			struct queue_limits *lim, int numa_node);
 void mpath_device_set_live(struct mpath_device *mpath_device);
+bool mpath_can_remove_head(struct mpath_head *mpath_head);
+ssize_t mpath_delayed_removal_secs_show(struct mpath_head *mpath_head,
+			char *buf);
+ssize_t mpath_delayed_removal_secs_store(struct mpath_head *mpath_head,
+			const char *buf, size_t count);
 
 static inline bool is_mpath_disk(struct gendisk *disk)
 {
@@ -126,6 +137,13 @@ static inline bool mpath_qd_iopolicy(enum mpath_iopolicy_e *iopolicy)
 	return READ_ONCE(*iopolicy) == MPATH_IOPOLICY_QD;
 }
 
+static inline bool mpath_head_queue_if_no_path(struct mpath_head *mpath_head)
+{
+	if (test_bit(MPATH_HEAD_QUEUE_IF_NO_PATH, &mpath_head->flags))
+		return true;
+	return false;
+}
+
 static inline void mpath_schedule_requeue_work(struct mpath_head *mpath_head)
 {
 	kblockd_schedule_work(&mpath_head->requeue_work);
lib/multipath.c +90 -1

121 lines not on this page · raw message

Signature · 1 line
2.43.7
#13 lore raw

Re: [PATCH v6 06/27] libmultipath: Add delayed removal support

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] mpath_remove_head_work() unconditionally removes the multipath head without verifying if paths…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] mpath_remove_head_work() unconditionally removes the multipath head without verifying if paths have been concurrently added, leading to destruction of an active device.
- [High] Missing cancellation of remove_work during multipath head teardown leads to a use-after-free when the delayed work expires.
- [High] mpath_head->drv_module is never initialized, leading to silent failures or crashes on module unload.
- [High] Disabling delayed removal via sysfs leaves queued I/Os stuck indefinitely.
- [Medium] Module reference count leak when mpath_can_remove_head() is called multiple times.
- [Medium] Large delayed removal values cause immediate removal instead of a long delay.
--

--- Patch [6]: [PATCH v6 06/27] libmultipath: Add delayed removal support --- commit 87d6b13fc148098f06b11013c5f12523eb0d477f
Author: John Garry <[email protected]>

libmultipath: Add delayed removal support

Add support for delayed removal, same as exists for NVMe, to keep the multipath disk and cdev present for intermittent periods of no available path.

lib/multipath.c int mpath_add_device(struct mpath_device *mpath_device, struct gendisk *disk, · line 73

Quoted 13 lines
lib/multipath.c +3 -0
index 6b8f6e846987a..fada92bf9754c 100644
@@ -66,6 +66,9 @@ int mpath_add_device(struct mpath_device *mpath_device, struct gendisk *disk,
 	list_add_tail_rcu(&mpath_device->siblings, &mpath_head->dev_list);
 	mutex_unlock(&mpath_head->lock);
 
+	if (cancel_delayed_work(&mpath_head->remove_work))
+		module_put(mpath_head->drv_module);
+
 	return 0;
 }

[Severity: High]
Can this race with the removal work?

If the delayed work is already executing, cancel_delayed_work() returns false, but mpath_add_device() still returns success. Meanwhile, could mpath_remove_head_work() execute without taking mpath_head->lock or verifying if the device list is empty, unintentionally destroying the multipath head while the driver believes a new path was successfully added?

[ ... ]

Quoted 6 lines
lib/multipath.c +2 -0
@@ -481,6 +493,39 @@ static void mpath_requeue_work(struct work_struct *work)
 	}
 }
 
+bool mpath_can_remove_head(struct mpath_head *mpath_head)
+{

lib/multipath.c static void mpath_requeue_work(struct work_struct *work) · line 507

Quoted 10 lines
lib/multipath.c +10 -0
+	unsigned long delay;
+	bool remove = false;
+
+	mutex_lock(&mpath_head->lock);
+	/*
+	 * Ensure that no one could remove this module while the head
+	 * remove work is pending.
+	 */
+	if (mpath_head_queue_if_no_path(mpath_head) &&
+	    !check_mul_overflow(mpath_head->delayed_removal_secs, HZ, &delay) &&

[Severity: Medium]
What happens if a user configures a very large delayed removal value?

If check_mul_overflow() detects an overflow (e.g., on 32-bit systems where a large sysfs input overflows unsigned long), it fails the condition and falls through to the else block, setting remove = true. Does this cause an immediate teardown instead of clamping the delay to a maximum value?

lib/multipath.c static void mpath_requeue_work(struct work_struct *work) · line 509

Quoted 2 lines
lib/multipath.c +2 -0
+	    try_module_get(mpath_head->drv_module)) {
+		mod_delayed_work(mpath_wq, &mpath_head->remove_work, delay);

[Severity: Medium]
Could this lead to a module reference leak?

If mod_delayed_work() returns true (meaning the work was already pending and its timer was just modified), it doesn't queue a new work item. Since try_module_get() was called unconditionally beforehand, and the work callback only drops one module reference, do subsequent calls to mpath_can_remove_head() leak a module reference permanently?

Quoted 7 lines
lib/multipath.c +7 -0
+	} else {
+		remove = true;
+	}
+
+	mutex_unlock(&mpath_head->lock);
+	return remove;
+}

[ ... ]

lib/multipath.c int mpath_alloc_head_disk(struct mpath_head *mpath_head, · line 579

Quoted 7 lines
lib/multipath.c +3 -0
@@ -529,6 +574,9 @@ int mpath_alloc_head_disk(struct mpath_head *mpath_head,
 	mpath_head->disk->private_data = mpath_head;
 	mpath_head->disk->fops = &mpath_ops;
 
+	INIT_DELAYED_WORK(&mpath_head->remove_work, mpath_remove_head_work);
+	mpath_head->delayed_removal_secs = 0;
+

[Severity: High]
Is drv_module properly initialized anywhere?

It seems drv_module is added to struct mpath_head but never assigned in mpath_alloc_head_disk() or elsewhere in this patch. If it defaults to NULL, try_module_get(NULL) succeeds but pins no module. Could this allow the driver to unload while the delayed work is still pending, leading to a kernel panic when the timer fires?

[Severity: High]
Is the remove_work guaranteed to be cancelled during object teardown?

The standard teardown sequence calls mpath_put_disk() and mpath_head_uninit(). If cancel_delayed_work_sync(&mpath_head->remove_work) is omitted during teardown, could the timer remain active on the workqueue and execute on the freed mpath_head structure later?

Quoted 3 lines
lib/multipath.c +0 -0
 	set_bit(GD_SUPPRESS_PART_SCAN, &mpath_head->disk->state);
 
 	return 0;

[ ... ]

Quoted 33 lines
lib/multipath.c +29 -0
@@ -572,6 +620,47 @@ void mpath_device_set_live(struct mpath_device *mpath_device)
 }
 EXPORT_SYMBOL_GPL(mpath_device_set_live);
 
+ssize_t mpath_delayed_removal_secs_show(struct mpath_head *mpath_head,
+					char *buf)
+{
+	int ret;
+
+	mutex_lock(&mpath_head->lock);
+	ret = sysfs_emit(buf, "%u\n", mpath_head->delayed_removal_secs);
+	mutex_unlock(&mpath_head->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mpath_delayed_removal_secs_show);
+
+ssize_t mpath_delayed_removal_secs_store(struct mpath_head *mpath_head,
+			const char *buf, size_t count)
+{
+	unsigned int sec;
+	ssize_t ret;
+
+	ret = kstrtouint(buf, 0, &sec);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&mpath_head->lock);
+	mpath_head->delayed_removal_secs = sec;
+	if (sec)
+		set_bit(MPATH_HEAD_QUEUE_IF_NO_PATH, &mpath_head->flags);
+	else
+		clear_bit(MPATH_HEAD_QUEUE_IF_NO_PATH, &mpath_head->flags);

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 661

Quoted 10 lines
lib/multipath.c +10 -0
+	mutex_unlock(&mpath_head->lock);
+
+	/*
+	 * Ensure that update to MPATH_HEAD_QUEUE_IF_NO_PATH is seen
+	 * by its reader.
+	 */
+	mpath_synchronize(mpath_head);
+
+	return count;
+}

[Severity: High]
If a user disables delayed removal by setting delayed_removal_secs to 0 via sysfs while paths are missing, how do pending I/Os get handled?

We clear the flag and call mpath_synchronize(), but shouldn't we also call mpath_schedule_requeue_work() here to flush and fail the I/Os that are already parked in mpath_head->requeue_list?

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

Re: [PATCH v6 06/27] libmultipath: Add delayed removal support

Quoted 36 lines · On 29/07/2026 13:08, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] mpath_remove_head_work() unconditionally removes the multipath head without verifying if paths have been concurrently added, leading to destruction of an active device.
- [High] Missing cancellation of remove_work during multipath head teardown leads to a use-after-free when the delayed work expires.
- [High] mpath_head->drv_module is never initialized, leading to silent failures or crashes on module unload.
- [High] Disabling delayed removal via sysfs leaves queued I/Os stuck indefinitely.
- [Medium] Module reference count leak when mpath_can_remove_head() is called multiple times.
- [Medium] Large delayed removal values cause immediate removal instead of a long delay.
--

--- Patch [6]: [PATCH v6 06/27] libmultipath: Add delayed removal support --- commit 87d6b13fc148098f06b11013c5f12523eb0d477f
Author: John Garry <[email protected]>

libmultipath: Add delayed removal support

Add support for delayed removal, same as exists for NVMe, to keep the multipath disk and cdev present for intermittent periods of no available path.

lib/multipath.c int mpath_add_device(struct mpath_device *mpath_device, struct gendisk *disk, · line 73

Quoted 13 lines
lib/multipath.c +3 -0
index 6b8f6e846987a..fada92bf9754c 100644
@@ -66,6 +66,9 @@ int mpath_add_device(struct mpath_device *mpath_device, struct gendisk *disk,
  	list_add_tail_rcu(&mpath_device->siblings, &mpath_head->dev_list);
  	mutex_unlock(&mpath_head->lock);
 
+	if (cancel_delayed_work(&mpath_head->remove_work))
+		module_put(mpath_head->drv_module);
+
  	return 0;
  }

[Severity: High]
Can this race with the removal work?

Quoted 7 lines

If the delayed work is already executing, cancel_delayed_work() returns false, but mpath_add_device() still returns success. Meanwhile, could mpath_remove_head_work() execute without taking mpath_head->lock or verifying if the device list is empty, unintentionally destroying the multipath head while the driver believes a new path was successfully added?

the removal callback should take care of this

Quoted 22 lines

[ ... ]

Quoted 6 lines
lib/multipath.c +2 -0
@@ -481,6 +493,39 @@ static void mpath_requeue_work(struct work_struct *work)
  	}
  }
 
+bool mpath_can_remove_head(struct mpath_head *mpath_head)
+{

lib/multipath.c static void mpath_requeue_work(struct work_struct *work) · line 507

Quoted 10 lines
lib/multipath.c +10 -0
+	unsigned long delay;
+	bool remove = false;
+
+	mutex_lock(&mpath_head->lock);
+	/*
+	 * Ensure that no one could remove this module while the head
+	 * remove work is pending.
+	 */
+	if (mpath_head_queue_if_no_path(mpath_head) &&
+	    !check_mul_overflow(mpath_head->delayed_removal_secs, HZ, &delay) &&

[Severity: Medium]
What happens if a user configures a very large delayed removal value?

Quoted 4 lines

If check_mul_overflow() detects an overflow (e.g., on 32-bit systems where a large sysfs input overflows unsigned long), it fails the condition and falls through to the else block, setting remove = true. Does this cause an immediate teardown instead of clamping the delay to a maximum value?

yes, so don't use a silly big value

Quoted 7 lines

lib/multipath.c static void mpath_requeue_work(struct work_struct *work) · line 509

Quoted 3 lines
lib/multipath.c +2 -0
+	    try_module_get(mpath_head->drv_module)) {
+		mod_delayed_work(mpath_wq, &mpath_head->remove_work, delay);

[Severity: Medium]
Could this lead to a module reference leak?

Quoted 5 lines

If mod_delayed_work() returns true (meaning the work was already pending and its timer was just modified), it doesn't queue a new work item. Since try_module_get() was called unconditionally beforehand, and the work callback only drops one module reference, do subsequent calls to mpath_can_remove_head() leak a module reference permanently?

it should not happen - that is the job of the caller to ensure

Quoted 15 lines
Quoted 8 lines
lib/multipath.c +7 -0
+	} else {
+		remove = true;
+	}
+
+	mutex_unlock(&mpath_head->lock);
+	return remove;
+}

[ ... ]

Quoted 4 lines
lib/multipath.c +0 -0
@@ -529,6 +574,9 @@ int mpath_alloc_head_disk(struct mpath_head *mpath_head,
  	mpath_head->disk->private_data = mpath_head;
  	mpath_head->disk->fops = &mpath_ops;
 
Quoted 6 lines

lib/multipath.c int mpath_alloc_head_disk(struct mpath_head *mpath_head, · line 579

Quoted 3 lines
lib/multipath.c +3 -0
+	INIT_DELAYED_WORK(&mpath_head->remove_work, mpath_remove_head_work);
+	mpath_head->delayed_removal_secs = 0;
+

[Severity: High]
Is drv_module properly initialized anywhere?

yes

Quoted 9 lines

It seems drv_module is added to struct mpath_head but never assigned in mpath_alloc_head_disk() or elsewhere in this patch. If it defaults to NULL, try_module_get(NULL) succeeds but pins no module. Could this allow the driver to unload while the delayed work is still pending, leading to a kernel panic when the timer fires?

[Severity: High]
Is the remove_work guaranteed to be cancelled during object teardown?

it must be assigned

Quoted 60 lines

The standard teardown sequence calls mpath_put_disk() and mpath_head_uninit(). If cancel_delayed_work_sync(&mpath_head->remove_work) is omitted during teardown, could the timer remain active on the workqueue and execute on the freed mpath_head structure later?

Quoted 3 lines
lib/multipath.c +0 -0
  	set_bit(GD_SUPPRESS_PART_SCAN, &mpath_head->disk->state);
 
  	return 0;

[ ... ]

Quoted 33 lines
lib/multipath.c +29 -0
@@ -572,6 +620,47 @@ void mpath_device_set_live(struct mpath_device *mpath_device)
  }
  EXPORT_SYMBOL_GPL(mpath_device_set_live);
 
+ssize_t mpath_delayed_removal_secs_show(struct mpath_head *mpath_head,
+					char *buf)
+{
+	int ret;
+
+	mutex_lock(&mpath_head->lock);
+	ret = sysfs_emit(buf, "%u\n", mpath_head->delayed_removal_secs);
+	mutex_unlock(&mpath_head->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mpath_delayed_removal_secs_show);
+
+ssize_t mpath_delayed_removal_secs_store(struct mpath_head *mpath_head,
+			const char *buf, size_t count)
+{
+	unsigned int sec;
+	ssize_t ret;
+
+	ret = kstrtouint(buf, 0, &sec);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&mpath_head->lock);
+	mpath_head->delayed_removal_secs = sec;
+	if (sec)
+		set_bit(MPATH_HEAD_QUEUE_IF_NO_PATH, &mpath_head->flags);
+	else
+		clear_bit(MPATH_HEAD_QUEUE_IF_NO_PATH, &mpath_head->flags);

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 661

Quoted 10 lines
lib/multipath.c +10 -0
+	mutex_unlock(&mpath_head->lock);
+
+	/*
+	 * Ensure that update to MPATH_HEAD_QUEUE_IF_NO_PATH is seen
+	 * by its reader.
+	 */
+	mpath_synchronize(mpath_head);
+
+	return count;
+}

[Severity: High]
If a user disables delayed removal by setting delayed_removal_secs to 0 via sysfs while paths are missing, how do pending I/Os get handled?

Quoted 3 lines

We clear the flag and call mpath_synchronize(), but shouldn't we also call mpath_schedule_requeue_work() here to flush and fail the I/Os that are already parked in mpath_head->requeue_list?

I don't see how this is related to requeue work

Quoted 1 line
author
#15 lore raw

PATCH v6 7/27 libmultipath: Add sysfs helpers

In reply to John Garry

Add helpers for driver sysfs code for the following functionality:
- get/set iopolicy with mpath_iopolicy_store() and mpath_iopolicy_show()
- show device path per NUMA node
- "multipath" attribute group, equivalent to nvme_ns_mpath_attr_group
- device groups attribute array, similar to nvme_ns_attr_groups but not containing NVMe members.

---
 include/linux/multipath.h |  5 +++
 lib/multipath.c           | 94 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/linux/multipath.h +5 -0
index 66b6ad9c2219a..7a3708ff37254 100644
@@ -7,6 +7,8 @@
 #include <linux/srcu.h>
 
 extern const struct block_device_operations mpath_ops;
+extern const struct attribute_group mpath_attr_group;
+extern const struct attribute_group *mpath_device_groups[];
 
 enum mpath_iopolicy_e {
 	MPATH_IOPOLICY_NUMA,
@@ -118,6 +120,9 @@ int mpath_alloc_head_disk(struct mpath_head *mpath_head,
 			struct queue_limits *lim, int numa_node);
 void mpath_device_set_live(struct mpath_device *mpath_device);
 bool mpath_can_remove_head(struct mpath_head *mpath_head);
+ssize_t mpath_numa_nodes_show(struct mpath_device *mpath_device, char *buf);
+ssize_t mpath_iopolicy_show(enum mpath_iopolicy_e *iopolicy, char *buf);
+bool mpath_iopolicy_store(enum mpath_iopolicy_e *iopolicy, const char *buf);
 ssize_t mpath_delayed_removal_secs_show(struct mpath_head *mpath_head,
 			char *buf);
 ssize_t mpath_delayed_removal_secs_store(struct mpath_head *mpath_head,
lib/multipath.c +94 -0

100 lines not on this page · raw message

Signature · 1 line
2.43.7
#16 lore raw

Re: [PATCH v6 07/27] libmultipath: Add sysfs helpers

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The sysfs store helper `mpath_iopolicy_store` updates the `iopolicy` locklessly and without…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The sysfs store helper mpath_iopolicy_store updates the iopolicy locklessly and without invalidating cached paths, creating a TOCTOU race on concurrent writes and leaving the system operating on stale paths.
- [Medium] Inconsistent evaluation of iopolicy in __mpath_find_path due to reading the state inside the device loop.
--

--- Patch [7]: [PATCH v6 07/27] libmultipath: Add sysfs helpers --- commit ceaf280cc3938326ee64a5bab20fde5b9a603a83
Author: John Garry <[email protected]>

libmultipath: Add sysfs helpers

This commit adds helper functions for driver sysfs code to get/set the iopolicy, show device paths per NUMA node, and manage multipath attribute groups.

Quoted 4 lines
lib/multipath.c +0 -0
index fada92bf9754c..58eb9be5bebbe 100644

[ ... ]

Quoted 1 line
lib/multipath.c +0 -0
@@ -620,6 +620,100 @@ void mpath_device_set_live(struct mpath_device *mpath_device)

[ ... ]

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 669

Quoted 9 lines
lib/multipath.c +9 -0
+static void mpath_iopolicy_update(enum mpath_iopolicy_e *iopolicy,
+		int new)
+{
+	int old = READ_ONCE(*iopolicy);
+
+	if (old == new)
+		return;
+
+	WRITE_ONCE(*iopolicy, new);

[Severity: High]
Since kernfs does not serialize store callbacks, can this lockless read-compare-update sequence lead to TOCTOU races on concurrent writes?

If multiple threads execute this simultaneously, could they both read the same old state, overwrite it, and emit inconsistent logs?

Additionally, does this policy update need a way to invalidate the cached paths? Because this helper only updates the policy and has no reference to mpath_head, it doesn't invalidate mpath_head->current_path.

Will the new policy continue using the stale path cached by the old policy?

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 674

Quoted 5 lines
lib/multipath.c +5 -0
+
+	pr_info("iopolicy changed from %s to %s\n",
+		mpath_iopolicy_names[old],
+		mpath_iopolicy_names[new]);
+}

[Severity: Medium]
Does introducing this concurrent sysfs update expose a TOCTOU race in __mpath_find_path()?

If __mpath_find_path() iterates over devices and reads the IO policy on each iteration via mpath_read_iopolicy(mpath_head):

	if (mpath_device->numa_node != NUMA_NO_NODE &&
	    (mpath_read_iopolicy(mpath_head) ==
		MPATH_IOPOLICY_NUMA))
		distance = node_distance(node,
				mpath_device->numa_node);
	else
		distance = LOCAL_DISTANCE;

Could a concurrent change here transition the policy mid-loop, causing the function to mix incompatible node_distance metrics with LOCAL_DISTANCE and potentially corrupting the minimum-finding logic?

Should the policy be read exactly once before the loop?

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

Re: [PATCH v6 07/27] libmultipath: Add sysfs helpers

Quoted 40 lines · On 29/07/2026 13:28, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The sysfs store helper mpath_iopolicy_store updates the iopolicy locklessly and without invalidating cached paths, creating a TOCTOU race on concurrent writes and leaving the system operating on stale paths.
- [Medium] Inconsistent evaluation of iopolicy in __mpath_find_path due to reading the state inside the device loop.
--

--- Patch [7]: [PATCH v6 07/27] libmultipath: Add sysfs helpers --- commit ceaf280cc3938326ee64a5bab20fde5b9a603a83
Author: John Garry <[email protected]>

libmultipath: Add sysfs helpers

This commit adds helper functions for driver sysfs code to get/set the iopolicy, show device paths per NUMA node, and manage multipath attribute groups.

Quoted 4 lines
lib/multipath.c +0 -0
index fada92bf9754c..58eb9be5bebbe 100644

[ ... ]

Quoted 1 line
lib/multipath.c +0 -0
@@ -620,6 +620,100 @@ void mpath_device_set_live(struct mpath_device *mpath_device)

[ ... ]

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 669

Quoted 9 lines
lib/multipath.c +9 -0
+static void mpath_iopolicy_update(enum mpath_iopolicy_e *iopolicy,
+		int new)
+{
+	int old = READ_ONCE(*iopolicy);
+
+	if (old == new)
+		return;
+
+	WRITE_ONCE(*iopolicy, new);

[Severity: High]
Since kernfs does not serialize store callbacks, can this lockless read-compare-update sequence lead to TOCTOU races on concurrent writes?

Quoted 2 lines

If multiple threads execute this simultaneously, could they both read the same old state, overwrite it, and emit inconsistent logs?

Don't call it concurrently then

Quoted 28 lines

Additionally, does this policy update need a way to invalidate the cached paths? Because this helper only updates the policy and has no reference to mpath_head, it doesn't invalidate mpath_head->current_path.

Will the new policy continue using the stale path cached by the old policy?

lib/multipath.c void mpath_device_set_live(struct mpath_device *mpath_device) · line 674

Quoted 5 lines
lib/multipath.c +5 -0
+
+	pr_info("iopolicy changed from %s to %s\n",
+		mpath_iopolicy_names[old],
+		mpath_iopolicy_names[new]);
+}

[Severity: Medium]
Does introducing this concurrent sysfs update expose a TOCTOU race in __mpath_find_path()?

If __mpath_find_path() iterates over devices and reads the IO policy on each iteration via mpath_read_iopolicy(mpath_head):

	if (mpath_device->numa_node != NUMA_NO_NODE &&
	    (mpath_read_iopolicy(mpath_head) ==
		MPATH_IOPOLICY_NUMA))
		distance = node_distance(node,
				mpath_device->numa_node);
	else
		distance = LOCAL_DISTANCE;
Quoted 6 lines

Could a concurrent change here transition the policy mid-loop, causing the function to mix incompatible node_distance metrics with LOCAL_DISTANCE and potentially corrupting the minimum-finding logic?

Should the policy be read exactly once before the loop?

So we choose not the best/appropiate path once - it won't make a difference

author
#18 lore raw

PATCH v6 8/27 libmultipath: Add support for block device IOCTL

In reply to John Garry

From: John Garry <[email protected]>

Add mpath_bdev_ioctl() as a multipath block device IOCTL handler. This handler calls into the mpath_device bdev fops handler.

The .compat_ioctl handler is given the standard handler.

---
 lib/multipath.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
lib/multipath.c +42 -0
index 58eb9be5bebbe..f1758d1e60a60 100644
@@ -454,11 +454,53 @@ static void mpath_bdev_release(struct gendisk *disk)
 	mpath_put_head(mpath_head);
 }
 
+static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
+		    unsigned int cmd, unsigned long arg)
+{
+	struct gendisk *disk = bdev->bd_disk;
+	struct mpath_head *mpath_head = mpath_gendisk_to_head(disk);
+	struct mpath_device *mpath_device;
+	int srcu_idx, err;
+
+	/*
+	 * This check is duplicated from sd_ioctl() as we don't pass the
+	 * partition bdev to fops->ioctl. That is not yet possible as the
+	 * per-path disk is hidden and, as such, does not have partitions
+	 * scanned.
+	 */
+	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
+		return -ENOIOCTLCMD;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+	if (!mpath_device) {
+		err = -EWOULDBLOCK;
+		goto out_unlock;
+	}
+
+	if (!mpath_device->disk->fops->ioctl) {
+		err = -ENOTTY;
+		goto out_unlock;
+	}
+
+	err = mpath_device->disk->fops->ioctl(
+			mpath_device->disk->part0, mode, cmd, arg);
+out_unlock:
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+	return err;
+}
+
 const struct block_device_operations mpath_ops = {
 	.owner          = THIS_MODULE,
 	.open		= mpath_bdev_open,
 	.release	= mpath_bdev_release,
 	.submit_bio	= mpath_bdev_submit_bio,
+	.ioctl		= mpath_bdev_ioctl,
+	/*
+	 * Both NVMe and SCSI use generic blkdev_compat_ptr_ioctl, so would
+	 * avoid their custom compat_ioctl implementation.
+	 */
+	.compat_ioctl	= blkdev_compat_ptr_ioctl,
 };
 EXPORT_SYMBOL_GPL(mpath_ops);
 
Signature · 1 line
2.43.7
#19 lore raw

Re: [PATCH v6 08/27] libmultipath: Add support for block device IOCTL

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Deadlock in `mpath_bdev_ioctl` due to holding `srcu_read_lock` while forwarding ioctls that can…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Deadlock in mpath_bdev_ioctl due to holding srcu_read_lock while forwarding ioctls that can trigger synchronous path removal.
- [High] Bypass of underlying partition-specific IOCTL security policies because mpath_bdev_ioctl unconditionally passes part0. --

--- Patch [8]: [PATCH v6 08/27] libmultipath: Add support for block device IOCTL --- commit d2e45114ccfb93fccdccc60ed75e05084f9b9173
Author: John Garry <[email protected]>

libmultipath: Add support for block device IOCTL

Add mpath_bdev_ioctl() as a multipath block device IOCTL handler. This handler calls into the mpath_device bdev fops handler.

Quoted 29 lines
lib/multipath.c +21 -0
index 58eb9be5bebbe..f1758d1e60a60 100644
@@ -454,11 +454,53 @@ static void mpath_bdev_release(struct gendisk *disk)
 	mpath_put_head(mpath_head);
 }
 
+static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
+		    unsigned int cmd, unsigned long arg)
+{
+	struct gendisk *disk = bdev->bd_disk;
+	struct mpath_head *mpath_head = mpath_gendisk_to_head(disk);
+	struct mpath_device *mpath_device;
+	int srcu_idx, err;
+
+	/*
+	 * This check is duplicated from sd_ioctl() as we don't pass the
+	 * partition bdev to fops->ioctl. That is not yet possible as the
+	 * per-path disk is hidden and, as such, does not have partitions
+	 * scanned.
+	 */
+	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
+		return -ENOIOCTLCMD;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+	if (!mpath_device) {
+		err = -EWOULDBLOCK;

lib/multipath.c static void mpath_bdev_release(struct gendisk *disk) · line 487

Quoted 10 lines
lib/multipath.c +10 -0
+		goto out_unlock;
+	}
+
+	if (!mpath_device->disk->fops->ioctl) {
+		err = -ENOTTY;
+		goto out_unlock;
+	}
+
+	err = mpath_device->disk->fops->ioctl(
+			mpath_device->disk->part0, mode, cmd, arg);

[Severity: High]
Does unconditionally passing part0 bypass partition-specific IOCTL security policies in the underlying driver?

By passing the whole-disk block device (mpath_device->disk->part0), bdev_is_partition() will evaluate to false in the underlying driver. For drivers like NVMe, this strips the partition context and could bypass requirements like CAP_SYS_ADMIN for passthrough commands on partitions, allowing a user with only CAP_SYS_RAWIO to execute whole-disk commands.

[Severity: High]
Can holding the SRCU read lock across the underlying ioctl cause a deadlock?

If the underlying ioctl triggers a controller reset or device removal, the teardown path could eventually wait on synchronize_srcu() for this exact srcu instance. Since the ioctl is waiting for the operation to complete while holding srcu_read_lock(), synchronize_srcu() would block forever.

Quoted 4 lines
lib/multipath.c +4 -0
+out_unlock:
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+	return err;
+}

[ ... ]

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

Re: [PATCH v6 08/27] libmultipath: Add support for block device IOCTL

Quoted 49 lines · On 29/07/2026 13:39, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Deadlock in mpath_bdev_ioctl due to holding srcu_read_lock while forwarding ioctls that can trigger synchronous path removal.
- [High] Bypass of underlying partition-specific IOCTL security policies because mpath_bdev_ioctl unconditionally passes part0. --

--- Patch [8]: [PATCH v6 08/27] libmultipath: Add support for block device IOCTL --- commit d2e45114ccfb93fccdccc60ed75e05084f9b9173
Author: John Garry <[email protected]>

libmultipath: Add support for block device IOCTL

Add mpath_bdev_ioctl() as a multipath block device IOCTL handler. This handler calls into the mpath_device bdev fops handler.

Quoted 35 lines
lib/multipath.c +27 -0
index 58eb9be5bebbe..f1758d1e60a60 100644
@@ -454,11 +454,53 @@ static void mpath_bdev_release(struct gendisk *disk)
  	mpath_put_head(mpath_head);
  }
 
+static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
+		    unsigned int cmd, unsigned long arg)
+{
+	struct gendisk *disk = bdev->bd_disk;
+	struct mpath_head *mpath_head = mpath_gendisk_to_head(disk);
+	struct mpath_device *mpath_device;
+	int srcu_idx, err;
+
+	/*
+	 * This check is duplicated from sd_ioctl() as we don't pass the
+	 * partition bdev to fops->ioctl. That is not yet possible as the
+	 * per-path disk is hidden and, as such, does not have partitions
+	 * scanned.
+	 */
+	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
+		return -ENOIOCTLCMD;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+	if (!mpath_device) {
+		err = -EWOULDBLOCK;
+		goto out_unlock;
+	}
+
+	if (!mpath_device->disk->fops->ioctl) {
+		err = -ENOTTY;
+		goto out_unlock;
Quoted 8 lines

lib/multipath.c static void mpath_bdev_release(struct gendisk *disk) · line 487

Quoted 4 lines
lib/multipath.c +4 -0
+	}
+
+	err = mpath_device->disk->fops->ioctl(
+			mpath_device->disk->part0, mode, cmd, arg);

[Severity: High]
Does unconditionally passing part0 bypass partition-specific IOCTL security policies in the underlying driver?

Check the comment above regarding sd_ioctl()

Quoted 7 lines

By passing the whole-disk block device (mpath_device->disk->part0), bdev_is_partition() will evaluate to false in the underlying driver. For drivers like NVMe, this strips the partition context and could bypass requirements like CAP_SYS_ADMIN for passthrough commands on partitions, allowing a user with only CAP_SYS_RAWIO to execute whole-disk commands.

Quoted 8 lines

[Severity: High]
Can holding the SRCU read lock across the underlying ioctl cause a deadlock?

If the underlying ioctl triggers a controller reset or device removal, the teardown path could eventually wait on synchronize_srcu() for this exact srcu instance. Since the ioctl is waiting for the operation to complete while holding srcu_read_lock(), synchronize_srcu() would block forever.

Currently the only ioctls supported cannot support doing controller reset or device removal, etc.

Quoted 7 lines
Quoted 4 lines
lib/multipath.c +4 -0
+out_unlock:
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+	return err;
+}

[ ... ]

author
#21 lore raw

PATCH v6 9/27 libmultipath: Add mpath_bdev_getgeo()

In reply to John Garry

From: John Garry <[email protected]>

Add mpath_bdev_getgeo() as a multipath block device .getgeo handler.

Here we just redirect into the selected mpath_device disk fops->getgeo handler.

---
 lib/multipath.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/multipath.c +21 -0
index f1758d1e60a60..2017c4d0a210c 100644
@@ -490,6 +490,26 @@ static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
 	return err;
 }
 
+static int mpath_bdev_getgeo(struct gendisk *disk, struct hd_geometry *geo)
+{
+	struct mpath_head *mpath_head = mpath_gendisk_to_head(disk);
+	int srcu_idx, ret = -EWOULDBLOCK;
+	struct mpath_device *mpath_device;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+	if (mpath_device) {
+		if (mpath_device->disk->fops->getgeo)
+			ret = mpath_device->disk->fops->getgeo(
+					mpath_device->disk, geo);
+		else
+			ret = -ENOTTY; /* See blkdev_getgeo */
+	}
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+
+	return ret;
+}
+
 const struct block_device_operations mpath_ops = {
 	.owner          = THIS_MODULE,
 	.open		= mpath_bdev_open,
@@ -501,6 +521,7 @@ const struct block_device_operations mpath_ops = {
 	 * avoid their custom compat_ioctl implementation.
 	 */
 	.compat_ioctl	= blkdev_compat_ptr_ioctl,
+	.getgeo		= mpath_bdev_getgeo,
 };
 EXPORT_SYMBOL_GPL(mpath_ops);
 
Signature · 1 line
2.43.7
author
#22 lore raw

PATCH v6 10/27 libmultipath: Add mpath_bdev_get_unique_id()

In reply to John Garry

From: John Garry <[email protected]>

Add mpath_bdev_get_unique_id() as a multipath block device .get_unique_id handler.

---
 lib/multipath.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
lib/multipath.c +22 -0
index 2017c4d0a210c..66b3338cf8389 100644
@@ -454,6 +454,27 @@ static void mpath_bdev_release(struct gendisk *disk)
 	mpath_put_head(mpath_head);
 }
 
+static int mpath_bdev_get_unique_id(struct gendisk *disk, u8 id[16],
+    enum blk_unique_id type)
+{
+	struct mpath_head *mpath_head = mpath_gendisk_to_head(disk);
+	int srcu_idx, ret = -EWOULDBLOCK;
+	struct mpath_device *mpath_device;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+	if (mpath_device) {
+		if (mpath_device->disk->fops->get_unique_id)
+			ret = mpath_device->disk->fops->get_unique_id(
+					mpath_device->disk, id, type);
+		else
+			ret = 0; /* referencing __dm_get_unique_id() */
+	}
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+
+	return ret;
+}
+
 static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
 		    unsigned int cmd, unsigned long arg)
 {
@@ -521,6 +542,7 @@ const struct block_device_operations mpath_ops = {
 	 * avoid their custom compat_ioctl implementation.
 	 */
 	.compat_ioctl	= blkdev_compat_ptr_ioctl,
+	.get_unique_id	= mpath_bdev_get_unique_id,
 	.getgeo		= mpath_bdev_getgeo,
 };
 EXPORT_SYMBOL_GPL(mpath_ops);
Signature · 1 line
2.43.7
author
#23 lore raw

PATCH v6 11/27 scsi-multipath: introduce basic SCSI device support

In reply to John Garry

For a scsi_device to support multipath, introduce structure scsi_mpath_device to hold multipath-specific details.

Like NS structure for NVME, scsi_mpath_device holds the mpath_device structure to device management and path selection.

A module param are introduced to enable multipath - the following modes are available:
- on
- off
- always

SCSI multipath will only be available until the following conditions:
- scsi_multipath enabled and implicit ALUA supported and unique ID available in VPD page 83.
- scsi_multipath always mode and unique ID available in VPD page 83

The scsi_device structure contains a pointer to scsi_mpath_device; having this pointer set or unset indicates whether multipath is enabled or disabled for the scsi_device.

---
 drivers/scsi/Kconfig          |  10 +++
 drivers/scsi/Makefile         |   1 +
 drivers/scsi/scsi.c           |   8 +-
 drivers/scsi/scsi_multipath.c | 148 ++++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_scan.c      |   4 +
 drivers/scsi/scsi_sysfs.c     |   2 +
 include/scsi/scsi_device.h    |   2 +
 include/scsi/scsi_multipath.h |  55 +++++++++++++
 8 files changed, 229 insertions(+), 1 deletion(-)
 create mode 100644 drivers/scsi/scsi_multipath.c
 create mode 100644 include/scsi/scsi_multipath.h
drivers/scsi/Kconfig +10 -0
index c3042393af234..d6c31df454825 100644
@@ -76,6 +76,16 @@ config SCSI_LIB_KUNIT_TEST
 
 	  If unsure say N.
 
+config SCSI_MULTIPATH
+	bool "SCSI multipath support (EXPERIMENTAL)"
+	depends on SCSI_MOD
+	select LIBMULTIPATH
+	help
+	  This option enables support for native SCSI multipath support for
+	  SCSI host.
+
+	  If unsure say N.
+
 comment "SCSI support type (disk, tape, CD-ROM)"
 	depends on SCSI
 
drivers/scsi/Makefile +1 -0
index 842c254bb2269..04f9c2e51fcbd 100644
@@ -168,6 +168,7 @@ scsi_mod-y			+= scsi_trace.o scsi_logging.o
 scsi_mod-$(CONFIG_PM)		+= scsi_pm.o
 scsi_mod-$(CONFIG_SCSI_DH)	+= scsi_dh.o
 scsi_mod-$(CONFIG_BLK_DEV_BSG)	+= scsi_bsg.o
+scsi_mod-$(CONFIG_SCSI_MULTIPATH)	+= scsi_multipath.o
 
 hv_storvsc-y			:= storvsc_drv.o
 
drivers/scsi/scsi.c +7 -1
index 76cdad063f7bc..70aa1dbeacebf 100644
@@ -64,6 +64,7 @@
 #include <scsi/scsi_driver.h>
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_host.h>
+#include <scsi/scsi_multipath.h>
 #include <scsi/scsi_tcq.h>
 
 #include "scsi_priv.h"
@@ -1042,12 +1043,16 @@ static int __init init_scsi(void)
 	error = scsi_sysfs_register();
 	if (error)
 		goto cleanup_sysctl;
+	error =  scsi_multipath_init();
+	if (error)
+		goto cleanup_sysfs;
 
 	scsi_netlink_init();
 
 	printk(KERN_NOTICE "SCSI subsystem initialized\n");
 	return 0;
-
+cleanup_sysfs:
+	scsi_sysfs_unregister();
 cleanup_sysctl:
 	scsi_exit_sysctl();
 cleanup_hosts:
@@ -1066,6 +1071,7 @@ static int __init init_scsi(void)
 static void __exit exit_scsi(void)
 {
 	scsi_netlink_exit();
+	scsi_multipath_exit();
 	scsi_sysfs_unregister();
 	scsi_exit_sysctl();
 	scsi_exit_hosts();
drivers/scsi/scsi_multipath.c new file +148 -0

148 lines not on this page · raw message

drivers/scsi/scsi_scan.c +4 -0
index e27da038603a2..3f1ac302719c2 100644
@@ -46,6 +46,7 @@
 #include <scsi/scsi_transport.h>
 #include <scsi/scsi_dh.h>
 #include <scsi/scsi_eh.h>
+#include <scsi/scsi_multipath.h>
 
 #include "scsi_priv.h"
 #include "scsi_logging.h"
@@ -1130,6 +1131,9 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
 	sdev->max_queue_depth = sdev->queue_depth;
 	WARN_ON_ONCE(sdev->max_queue_depth > sdev->budget_map.depth);
 
+	if (scsi_mpath_dev_alloc(sdev))
+		return SCSI_SCAN_NO_RESPONSE;
+
 	/*
 	 * Ok, the device is now all set up, we can
 	 * register it and tell the rest of the kernel
drivers/scsi/scsi_sysfs.c +2 -0
index dfc3559e7e04f..2f80d703ce640 100644
@@ -23,6 +23,7 @@
 #include <scsi/scsi_transport.h>
 #include <scsi/scsi_driver.h>
 #include <scsi/scsi_devinfo.h>
+#include <scsi/scsi_multipath.h>
 
 #include "scsi_priv.h"
 #include "scsi_logging.h"
@@ -455,6 +456,7 @@ static void scsi_device_dev_release(struct device *dev)
 	might_sleep();
 
 	scsi_dh_release_device(sdev);
+	scsi_mpath_dev_release(sdev);
 
 	parent = sdev->sdev_gendev.parent;
 
include/scsi/scsi_device.h +2 -0
index 029f5115b2ea0..40a435e66b34e 100644
@@ -280,6 +280,8 @@ struct scsi_device {
 	struct device		sdev_gendev,
 				sdev_dev;
 
+	struct scsi_mpath_device *scsi_mpath_dev;
+
 	struct work_struct	requeue_work;
 
 	struct scsi_device_handler *handler;
include/scsi/scsi_multipath.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.43.7
author
#24 lore raw

PATCH v6 12/27 scsi-multipath: introduce scsi_device head structure

In reply to John Garry

From: John Garry <[email protected]>

Introduce a scsi_device head structure - scsi_mpath_head - to manage multipathing for a scsi_device. This is similar to nvme_ns_head structure.

There is no reference in scsi_mpath_head to any disk, as this would be managed by the scsi_disk driver.

A list of scsi_mpath_head structures is managed to lookup for matching multipathed scsi_device's. Matching is done through the scsi_device unique id.

A new class for multipathed devices is added, scsi_mpath_device_class.

The purpose of this class is for managing the scsi_mpath_head.dev member.

The naming for the scsi_device structure is in form H:C:I:L, where H is host, C is channel, I is ID, and L is lun.

However, for a multipathed scsi_device, all the naming members may be different between member scsi_device's. As such, just use a simple single-number naming index for each scsi_mpath_head.

The sysfs device folder will have links to the scsi_device's so, it will be possible to lookup the member scsi_device's.

An example sysfs entry is as follows:

# ls -l /sys/class/scsi_mpath_device/scsi_mpath_device0/
total 0
drwxr-xr-x    2 root     root             0 Apr 13 15:48 power
lrwxrwxrwx    1 root     root             0 Apr 13 15:48 subsystem -> ../../../../class/scsi_mpath_device
-rw-r--r--    1 root     root          4096 Apr 13 15:48 uevent
-r--r--r--    1 root     root          4096 Apr 13 15:48 vpd_id
---
 drivers/scsi/scsi_multipath.c | 199 +++++++++++++++++++++++++++++++++-
 drivers/scsi/scsi_sysfs.c     |   3 +
 include/scsi/scsi_multipath.h |  31 ++++++
 3 files changed, 231 insertions(+), 2 deletions(-)
drivers/scsi/scsi_multipath.c +197 -2
index 5559e2e8d64a8..30a2d01be8aaf 100644
@@ -29,6 +29,10 @@ static const char *scsi_multipath_modes[] = {
 
 static int scsi_multipath = SCSI_MULTIPATH_OFF;
 
+static LIST_HEAD(scsi_mpath_heads_list);
+static DEFINE_MUTEX(scsi_mpath_heads_lock);
+static DEFINE_IDA(scsi_multipath_dev_ida);
+
 static int scsi_multipath_param_set(const char *val, const struct kernel_param *kp)
 {
 	int mode;
@@ -72,6 +76,55 @@ static int scsi_mpath_unique_lun_id(struct scsi_device *sdev)
 	return 0;
 }
 
+static void scsi_mpath_head_release(struct device *dev)
+{
+	struct scsi_mpath_head *scsi_mpath_head =
+		container_of(dev, struct scsi_mpath_head, dev);
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
+	ida_destroy(&scsi_mpath_head->ida);
+	mpath_head_uninit(mpath_head);
+	kfree(scsi_mpath_head);
+}
+
+static ssize_t scsi_mpath_device_vpd_id_show(struct device *dev,
+			struct device_attribute *attr,
+			char *buf)
+{
+	struct scsi_mpath_head *scsi_mpath_head =
+		container_of(dev, struct scsi_mpath_head, dev);
+
+	return sysfs_emit(buf, "%s\n", scsi_mpath_head->vpd_id);
+}
+static DEVICE_ATTR(vpd_id, S_IRUGO, scsi_mpath_device_vpd_id_show, NULL);
+
+static struct attribute *scsi_mpath_device_attrs[] = {
+	&dev_attr_vpd_id.attr,
+	NULL
+};
+
+static const struct attribute_group scsi_mpath_device_attrs_group = {
+	.attrs = scsi_mpath_device_attrs,
+};
+
+static bool scsi_multipath_sysfs_group_visible(struct kobject *kobj)
+{
+	return true;
+}
+DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs)
+
+static const struct attribute_group *scsi_mpath_device_groups[] = {
+	&scsi_mpath_device_attrs_group,
+	NULL
+};
+
+static const struct class scsi_mpath_device_class = {
+	.name = "scsi_mpath_device",
+	.dev_groups = scsi_mpath_device_groups,
+	.dev_release = scsi_mpath_head_release,
+};
+
 static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 {
 	struct Scsi_Host *shost = sdev->host;
@@ -91,6 +144,74 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+static struct mpath_head_template smpdt = {
+};
+
+static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
+{
+	struct scsi_mpath_head *scsi_mpath_head;
+	int ret;
+
+	scsi_mpath_head = kzalloc(sizeof(*scsi_mpath_head), GFP_KERNEL);
+	if (!scsi_mpath_head)
+		return NULL;
+
+	ida_init(&scsi_mpath_head->ida);
+
+	if (mpath_head_init(&scsi_mpath_head->mpath_head))
+		goto out_free;
+	scsi_mpath_head->mpath_head.mpdt = &smpdt;
+
+	strscpy(scsi_mpath_head->vpd_id, vpd_id,
+		SCSI_MPATH_DEVICE_ID_LEN);
+
+	scsi_mpath_head->index = ida_alloc(&scsi_multipath_dev_ida, GFP_KERNEL);
+	if (scsi_mpath_head->index < 0)
+		goto out_uninit_head;
+	kref_init(&scsi_mpath_head->ref);
+
+	device_initialize(&scsi_mpath_head->dev);
+	scsi_mpath_head->dev.class = &scsi_mpath_device_class;
+	ret = dev_set_name(&scsi_mpath_head->dev, "scsi_mpath_device%d",
+				scsi_mpath_head->index);
+	if (ret) {
+		put_device(&scsi_mpath_head->dev);
+		return NULL;
+	}
+
+	ret = device_add(&scsi_mpath_head->dev);
+	if (ret) {
+		put_device(&scsi_mpath_head->dev);
+		return NULL;
+	}
+
+	return scsi_mpath_head;
+
+out_uninit_head:
+	mpath_head_uninit(&scsi_mpath_head->mpath_head);
+out_free:
+	kfree(scsi_mpath_head);
+	return NULL;
+}
+
+static struct scsi_mpath_head *scsi_mpath_find_head(
+			struct scsi_mpath_device *scsi_mpath_dev)
+{
+	struct scsi_mpath_head *scsi_mpath_head;
+
+	list_for_each_entry(scsi_mpath_head, &scsi_mpath_heads_list, entry) {
+		if (strncmp(scsi_mpath_head->vpd_id,
+			scsi_mpath_dev->device_id_str,
+			SCSI_MPATH_DEVICE_ID_LEN) == 0) {
+			if (scsi_mpath_get_head(scsi_mpath_head))
+				continue;
+			return scsi_mpath_head;
+		}
+	}
+
+	return NULL;
+}
+
 static void scsi_multipath_sdev_uninit(struct scsi_device *sdev)
 {
 	kfree(sdev->scsi_mpath_dev);
@@ -99,6 +220,7 @@ static void scsi_multipath_sdev_uninit(struct scsi_device *sdev)
 
 int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 {
+	struct scsi_mpath_head *scsi_mpath_head;
 	int ret;
 
 	if (scsi_multipath == SCSI_MULTIPATH_OFF)
@@ -118,13 +240,51 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 	if (ret < 0)
 		goto out_uninit;
 
-	return 0;
+	mutex_lock(&scsi_mpath_heads_lock);
+	scsi_mpath_head = scsi_mpath_find_head(sdev->scsi_mpath_dev);
+	if (scsi_mpath_head)
+		goto found;
+	scsi_mpath_head =
+		scsi_mpath_alloc_head(sdev->scsi_mpath_dev->device_id_str);
+	if (!scsi_mpath_head) {
+		sdev_printk(KERN_NOTICE, sdev, "could not allocate multipath head, device multipathing disabled\n");
+		mutex_unlock(&scsi_mpath_heads_lock);
+		goto out_uninit;
+	}
 
+	list_add_tail(&scsi_mpath_head->entry, &scsi_mpath_heads_list);
+found:
+	mutex_unlock(&scsi_mpath_heads_lock);
+	ret = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL);
+	if (ret < 0)
+		goto out_put_head;
+	sdev->scsi_mpath_dev->index = ret;
+	sdev->scsi_mpath_dev->scsi_mpath_head = scsi_mpath_head;
+	sdev->scsi_mpath_dev->mpath_device.mpath_head =
+				&scsi_mpath_head->mpath_head;
+	return 0;
+out_put_head:
+	scsi_mpath_put_head(scsi_mpath_head);
 out_uninit:
 	scsi_multipath_sdev_uninit(sdev);
 	return 0;
 }
 
+static void scsi_mpath_remove_head(struct scsi_mpath_device *scsi_mpath_dev)
+{
+	scsi_mpath_put_head(scsi_mpath_dev->scsi_mpath_head);
+	scsi_mpath_dev->scsi_mpath_head = NULL;
+}
+
+void scsi_mpath_remove_device(struct scsi_mpath_device *scsi_mpath_dev)
+{
+	struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+
+	ida_free(&scsi_mpath_head->ida, scsi_mpath_dev->index);
+
+	scsi_mpath_remove_head(scsi_mpath_dev);
+}
+
 void scsi_mpath_dev_release(struct scsi_device *sdev)
 {
 	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
@@ -135,13 +295,48 @@ void scsi_mpath_dev_release(struct scsi_device *sdev)
 	scsi_multipath_sdev_uninit(sdev);
 }
 
-int __init scsi_multipath_init(void)
+int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head)
 {
+	if (!kref_get_unless_zero(&scsi_mpath_head->ref))
+		return -ENXIO;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(scsi_mpath_get_head);
+
+static void scsi_mpath_free_head(struct kref *ref)
+{
+	struct scsi_mpath_head *scsi_mpath_head =
+		container_of(ref, struct scsi_mpath_head, ref);
+
+	/*
+	 * If we race with scsi_mpath_find_head(), then that function may
+	 * find this scsi_mpath_head in the heads list; however we would fail
+	 * to take a reference to this scsi_mpath_head and continue the search.
+	 * As such, it is safe to call device_unregister (and free
+	 * scsi_mpath_head) after we delete this head from the list.
+	 */
+	mutex_lock(&scsi_mpath_heads_lock);
+	list_del_init(&scsi_mpath_head->entry);
+	mutex_unlock(&scsi_mpath_heads_lock);
+
+	device_unregister(&scsi_mpath_head->dev);
+}
+
+void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head)
+{
+	kref_put(&scsi_mpath_head->ref, scsi_mpath_free_head);
+}
+EXPORT_SYMBOL_GPL(scsi_mpath_put_head);
+
+int __init scsi_multipath_init(void)
+{
+	return class_register(&scsi_mpath_device_class);
+}
 
 void __exit scsi_multipath_exit(void)
 {
+	ida_destroy(&scsi_multipath_dev_ida);
+	class_unregister(&scsi_mpath_device_class);
 }
 
 MODULE_LICENSE("GPL");
drivers/scsi/scsi_sysfs.c +3 -0
index 2f80d703ce640..799569b74b535 100644
@@ -1525,6 +1525,9 @@ void __scsi_remove_device(struct scsi_device *sdev)
 	 */
 	scsi_target_reap(scsi_target(sdev));
 
+	if (sdev->scsi_mpath_dev)
+		scsi_mpath_remove_device(sdev->scsi_mpath_dev);
+
 	put_device(dev);
 }
 
include/scsi/scsi_multipath.h +31 -0
index d3d410dafd17a..50298056181f9 100644
@@ -19,12 +19,25 @@
 #ifdef CONFIG_SCSI_MULTIPATH
 #define SCSI_MPATH_DEVICE_ID_LEN 256
 
+struct scsi_mpath_head {
+	struct mpath_head	mpath_head;
+	char			vpd_id[SCSI_MPATH_DEVICE_ID_LEN];
+	struct list_head	entry;
+	struct ida		ida;
+	struct kref		ref;
+	struct device		dev;
+	int			index;
+};
+
 struct scsi_mpath_device {
 	struct mpath_device	mpath_device;
 	struct scsi_device 	*sdev;
+	int			index;
+	struct scsi_mpath_head	*scsi_mpath_head;
 
 	char			device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
 };
+
 #define to_scsi_mpath_device(d) \
 	container_of(d, struct scsi_mpath_device, mpath_device)
 
@@ -32,8 +45,13 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev);
 void scsi_mpath_dev_release(struct scsi_device *sdev);
 int scsi_multipath_init(void);
 void scsi_multipath_exit(void);
+void scsi_mpath_remove_device(struct scsi_mpath_device *scsi_mpath_dev);
+int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head);
+void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
 #else /* CONFIG_SCSI_MULTIPATH */
 
+struct scsi_mpath_head {
+};
 struct scsi_mpath_device {
 };
 
@@ -51,5 +69,18 @@ static inline int scsi_multipath_init(void)
 static inline void scsi_multipath_exit(void)
 {
 }
+static inline
+void scsi_mpath_remove_device(struct scsi_mpath_device *scsi_mpath_dev)
+{
+}
+static inline
+int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head)
+{
+	return 0;
+}
+static inline
+void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head)
+{
+}
 #endif /* CONFIG_SCSI_MULTIPATH */
 #endif /* _SCSI_SCSI_MULTIPATH_H */
Signature · 1 line
2.43.7
#25 lore raw

Re: [PATCH v6 12/27] scsi-multipath: introduce scsi_device head structure

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The struct `mpath_head_template smpdt` is defined completely empty, lacking logically required…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The struct mpath_head_template smpdt is defined completely empty, lacking logically required callbacks, leading to NULL pointer dereferences.
- [Medium] The patch omits the logic to create sysfs links to member scsi_device's, contradicting the stated commit message.
- [Low] The function scsi_multipath_sysfs_group_visible and the macro DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs) generate unused static code, causing a -Wunused-function warning. --

--- Patch [12]: [PATCH v6 12/27] scsi-multipath: introduce scsi_device head structure --- commit 41b0248171de9edac7171ac10282e0c5b3f1056c
Author: John Garry <[email protected]>

scsi-multipath: introduce scsi_device head structure

Introduce a scsi_mpath_head structure to manage multipathing for a scsi_device. A list of scsi_mpath_head structures is managed to lookup for matching multipathed scsi_device's.

Quoted 2 lines

The sysfs device folder will have links to the scsi_device's so, it will be possible to lookup the member scsi_device's.

[Severity: Medium]
I notice the patch doesn't seem to contain the code to create these sysfs links to the member scsi_device structures. Is this implementation missing, or is it planned for a future patch?

drivers/scsi/scsi_multipath.c +0 -0
index 5559e2e8d64a8..30a2d01be8aaf 100644

[ ... ]

Review

Quoted 9 lines
(quoted diff) +9 -0
+static const struct attribute_group scsi_mpath_device_attrs_group = {
+	.attrs = scsi_mpath_device_attrs,
+};
+
+static bool scsi_multipath_sysfs_group_visible(struct kobject *kobj)
+{
+	return true;
+}
+DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs)

[Severity: Low]
Is this function and macro used anywhere in this patch?

It looks like DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE generates a static function that isn't assigned to any attribute_group.is_visible callback, which might cause a unused-function compiler warning.

Quoted 5 lines
(quoted diff) +5 -0
+
+static const struct attribute_group *scsi_mpath_device_groups[] = {
+	&scsi_mpath_device_attrs_group,
+	NULL
+};

[ ... ]

Review static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 148

Quoted 9 lines

static int scsi_multipath_sdev_init(struct scsi_device *sdev) {

(quoted diff) +2 -0
 	struct Scsi_Host *shost = sdev->host;
@@ -91,6 +144,74 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+static struct mpath_head_template smpdt = {
+};

[Severity: High]
Will this cause a null pointer dereference when the generic multipath core attempts to evaluate I/O paths?

Since smpdt is defined completely empty without any callback assignments, function pointers like mpath_head->mpdt->is_disabled will be null when dereferenced by the framework.

Quoted 4 lines
(quoted diff) +4 -0
+
+static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
+{
+	struct scsi_mpath_head *scsi_mpath_head;

[ ... ]

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

Re: [PATCH v6 12/27] scsi-multipath: introduce scsi_device head structure

Quoted 16 lines · On 29/07/2026 14:46, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The struct mpath_head_template smpdt is defined completely empty, lacking logically required callbacks, leading to NULL pointer dereferences.
- [Medium] The patch omits the logic to create sysfs links to member scsi_device's, contradicting the stated commit message.
- [Low] The function scsi_multipath_sysfs_group_visible and the macro DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs) generate unused static code, causing a -Wunused-function warning. --

--- Patch [12]: [PATCH v6 12/27] scsi-multipath: introduce scsi_device head structure --- commit 41b0248171de9edac7171ac10282e0c5b3f1056c
Author: John Garry <[email protected]>

scsi-multipath: introduce scsi_device head structure

Introduce a scsi_mpath_head structure to manage multipathing for a scsi_device. A list of scsi_mpath_head structures is managed to lookup for matching multipathed scsi_device's.

Quoted 7 lines
Quoted 2 lines

The sysfs device folder will have links to the scsi_device's so, it will be possible to lookup the member scsi_device's.

[Severity: Medium]
I notice the patch doesn't seem to contain the code to create these sysfs links to the member scsi_device structures. Is this implementation missing, or is it planned for a future patch?

future patch

Quoted 21 lines
drivers/scsi/scsi_multipath.c +0 -0
index 5559e2e8d64a8..30a2d01be8aaf 100644
 
[ ... ]

Review

Quoted 10 lines
(quoted diff) +9 -0
+static const struct attribute_group scsi_mpath_device_attrs_group = {
+	.attrs = scsi_mpath_device_attrs,
+};
+
+static bool scsi_multipath_sysfs_group_visible(struct kobject *kobj)
+{
+	return true;
+}
+DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs)

[Severity: Low]
Is this function and macro used anywhere in this patch?

Quoted 3 lines

It looks like DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE generates a static function that isn't assigned to any attribute_group.is_visible callback, which might cause a unused-function compiler warning.

yes, it is used later. For me, the compiler does not complain as DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE uses inline in the function declaration

Quoted 23 lines
Quoted 6 lines
(quoted diff) +5 -0
+
+static const struct attribute_group *scsi_mpath_device_groups[] = {
+	&scsi_mpath_device_attrs_group,
+	NULL
+};

[ ... ]

Review static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 148

Quoted 9 lines
  static int scsi_multipath_sdev_init(struct scsi_device *sdev)
  {
  	struct Scsi_Host *shost = sdev->host;
(quoted diff) +2 -0
@@ -91,6 +144,74 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
  	return 0;
  }
 
+static struct mpath_head_template smpdt = {
+};

[Severity: High]
Will this cause a null pointer dereference when the generic multipath core attempts to evaluate I/O paths?

Quoted 3 lines

Since smpdt is defined completely empty without any callback assignments, function pointers like mpath_head->mpdt->is_disabled will be null when dereferenced by the framework.

it is filled in later

Quoted 8 lines
Quoted 5 lines
(quoted diff) +4 -0
+
+static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
+{
+	struct scsi_mpath_head *scsi_mpath_head;

[ ... ]

author
#27 lore raw

PATCH v6 13/27 scsi-multipath: provide sysfs link from to scsi_device

In reply to John Garry

From: John Garry <[email protected]>

Provide a link in sysfs from a scsi_mpath_device to member scsi_device's.

An example is as follows:

# ls -l /sys/class/scsi_mpath_device/scsi_mpath_device0/multipath/
total 0
lrwxrwxrwx    1 root     root             0 Feb 24 12:01 8:0:0:0 -> ../../../../platform/host8/session1/target8:0:0/8:0:0:0
lrwxrwxrwx    1 root     root             0 Feb 24 12:01 9:0:0:0 -> ../../../../platform/host9/session2/target9:0:0/9:0:0:0
---
 drivers/scsi/scsi_multipath.c | 43 +++++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_sysfs.c     |  5 ++++
 include/scsi/scsi_multipath.h |  8 +++++++
 3 files changed, 56 insertions(+)
drivers/scsi/scsi_multipath.c +43 -0
index 30a2d01be8aaf..1ec7bf1835de0 100644
@@ -114,11 +114,54 @@ static bool scsi_multipath_sysfs_group_visible(struct kobject *kobj)
 }
 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs)
 
+static struct attribute dummy_attr = {
+	.name = "dummy",
+};
+
+static struct attribute *scsi_mpath_attrs[] = {
+	&dummy_attr,
+	NULL
+};
+
+static const struct attribute_group scsi_mpath_attr_group = {
+	.name           = "multipath",
+	.attrs		= scsi_mpath_attrs,
+	.is_visible     = SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs),
+};
+
 static const struct attribute_group *scsi_mpath_device_groups[] = {
 	&scsi_mpath_device_attrs_group,
+	&scsi_mpath_attr_group,
 	NULL
 };
 
+void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
+{
+	struct device *target = &sdev->sdev_gendev;
+	struct scsi_mpath_head *scsi_mpath_head =
+		sdev->scsi_mpath_dev->scsi_mpath_head;
+	struct device *source = &scsi_mpath_head->dev;
+	int error;
+
+	error = sysfs_add_link_to_group(&source->kobj, "multipath",
+			&target->kobj, dev_name(target));
+	if (error) {
+		sdev_printk(KERN_INFO, sdev, "Failed to create mpath sysfs link, error=%d\n",
+				    error);
+	}
+}
+
+void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev)
+{
+	struct device *target = &sdev->sdev_gendev;
+	struct scsi_mpath_head *scsi_mpath_head =
+		sdev->scsi_mpath_dev->scsi_mpath_head;
+	struct device *source = &scsi_mpath_head->dev;
+
+	sysfs_remove_link_from_group(&source->kobj, "multipath",
+		dev_name(target));
+}
+
 static const struct class scsi_mpath_device_class = {
 	.name = "scsi_mpath_device",
 	.dev_groups = scsi_mpath_device_groups,
drivers/scsi/scsi_sysfs.c +5 -0
index 799569b74b535..ce51fcc002bbf 100644
@@ -1443,6 +1443,9 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
 	transport_add_device(&sdev->sdev_gendev);
 	sdev->is_visible = 1;
 
+	if (sdev->scsi_mpath_dev)
+		scsi_mpath_add_sysfs_link(sdev);
+
 	if (IS_ENABLED(CONFIG_BLK_DEV_BSG)) {
 		sdev->bsg_dev = scsi_bsg_register_queue(sdev);
 		if (IS_ERR(sdev->bsg_dev)) {
@@ -1495,6 +1498,8 @@ void __scsi_remove_device(struct scsi_device *sdev)
 
 		if (IS_ENABLED(CONFIG_BLK_DEV_BSG) && sdev->bsg_dev)
 			bsg_unregister_queue(sdev->bsg_dev);
+		if (sdev->scsi_mpath_dev)
+			scsi_mpath_remove_sysfs_link(sdev);
 		device_unregister(&sdev->sdev_dev);
 		transport_remove_device(dev);
 		device_del(dev);
include/scsi/scsi_multipath.h +8 -0
index 50298056181f9..a9fd02bc42371 100644
@@ -46,6 +46,8 @@ void scsi_mpath_dev_release(struct scsi_device *sdev);
 int scsi_multipath_init(void);
 void scsi_multipath_exit(void);
 void scsi_mpath_remove_device(struct scsi_mpath_device *scsi_mpath_dev);
+void scsi_mpath_add_sysfs_link(struct scsi_device *sdev);
+void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev);
 int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
 #else /* CONFIG_SCSI_MULTIPATH */
@@ -82,5 +84,11 @@ static inline
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head)
 {
 }
+static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
+{
+}
+static inline void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev)
+{
+}
 #endif /* CONFIG_SCSI_MULTIPATH */
 #endif /* _SCSI_SCSI_MULTIPATH_H */
Signature · 1 line
2.43.7
author
#28 lore raw

PATCH v6 14/27 scsi-multipath: support iopolicy

In reply to John Garry

Add support to set the multipath iopolicy.

The iopolicy member is per scsi_mpath_head structure.

A module param is added so that the default iopolicy may be set.

---
 drivers/scsi/scsi_multipath.c | 52 +++++++++++++++++++++++++++++++++++
 include/scsi/scsi_multipath.h |  3 ++
 2 files changed, 55 insertions(+)
drivers/scsi/scsi_multipath.c +52 -0
index 1ec7bf1835de0..593f94aac70e9 100644
@@ -61,6 +61,23 @@ static const struct kernel_param_ops multipath_param_ops = {
 module_param_cb(multipath, &multipath_param_ops, &scsi_multipath, 0444);
 MODULE_PARM_DESC(multipath, "turn on native multipath support, options: on, off, always");
 
+static enum mpath_iopolicy_e iopolicy = MPATH_IOPOLICY_NUMA;
+
+static int scsi_mpath_set_iopolicy_param(const char *val, const struct kernel_param *kp)
+{
+	return mpath_set_iopolicy(val, &iopolicy);
+}
+
+static int scsi_mpath_get_iopolicy_param(char *buf, const struct kernel_param *kp)
+{
+	return mpath_get_iopolicy(buf, iopolicy);
+}
+
+module_param_call(multipath_iopolicy, scsi_mpath_set_iopolicy_param,
+		scsi_mpath_get_iopolicy_param, &iopolicy, 0644);
+MODULE_PARM_DESC(multipath_iopolicy,
+	"Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'");
+
 static int scsi_mpath_unique_lun_id(struct scsi_device *sdev)
 {
 	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
@@ -99,8 +116,41 @@ static ssize_t scsi_mpath_device_vpd_id_show(struct device *dev,
 }
 static DEVICE_ATTR(vpd_id, S_IRUGO, scsi_mpath_device_vpd_id_show, NULL);
 
+static ssize_t scsi_mpath_device_iopolicy_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct scsi_mpath_head *scsi_mpath_head =
+		container_of(dev, struct scsi_mpath_head, dev);
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+	if (!mpath_iopolicy_store(&scsi_mpath_head->iopolicy, buf))
+		return -EINVAL;
+
+	mpath_clear_paths(mpath_head);
+	mpath_schedule_requeue_work(mpath_head);
+	/*
+	 * Ensure requeue work completes, as this work could run later when
+	 * the mpath_head is gone.
+	 */
+	flush_work(&mpath_head->requeue_work);
+	return count;
+}
+
+static ssize_t scsi_mpath_device_iopolicy_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct scsi_mpath_head *scsi_mpath_head =
+		container_of(dev, struct scsi_mpath_head, dev);
+
+	return mpath_iopolicy_show(&scsi_mpath_head->iopolicy, buf);
+}
+
+static DEVICE_ATTR(iopolicy, S_IRUGO | S_IWUSR,
+		scsi_mpath_device_iopolicy_show, scsi_mpath_device_iopolicy_store);
+
 static struct attribute *scsi_mpath_device_attrs[] = {
 	&dev_attr_vpd_id.attr,
+	&dev_attr_iopolicy.attr,
 	NULL
 };
 
@@ -204,6 +254,8 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
 	if (mpath_head_init(&scsi_mpath_head->mpath_head))
 		goto out_free;
 	scsi_mpath_head->mpath_head.mpdt = &smpdt;
+	scsi_mpath_head->iopolicy = iopolicy;
+	scsi_mpath_head->mpath_head.iopolicy = &scsi_mpath_head->iopolicy;
 
 	strscpy(scsi_mpath_head->vpd_id, vpd_id,
 		SCSI_MPATH_DEVICE_ID_LEN);
include/scsi/scsi_multipath.h +3 -0
index a9fd02bc42371..2dc02313b0496 100644
@@ -25,6 +25,7 @@ struct scsi_mpath_head {
 	struct list_head	entry;
 	struct ida		ida;
 	struct kref		ref;
+	enum mpath_iopolicy_e	iopolicy;
 	struct device		dev;
 	int			index;
 };
@@ -40,6 +41,8 @@ struct scsi_mpath_device {
 
 #define to_scsi_mpath_device(d) \
 	container_of(d, struct scsi_mpath_device, mpath_device)
+#define to_scsi_mpath_head(d) \
+	container_of(d, struct scsi_mpath_head, mpath_head)
 
 int scsi_mpath_dev_alloc(struct scsi_device *sdev);
 void scsi_mpath_dev_release(struct scsi_device *sdev);
Signature · 1 line
2.43.7
#29 lore raw

Re: [PATCH v6 14/27] scsi-multipath: support iopolicy

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] The struct mpath_head_template 'smpdt' is entirely empty, lacking implementations for…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] The struct mpath_head_template 'smpdt' is entirely empty, lacking implementations for logically required callbacks, leading to NULL pointer dereferences.
- [Low] Missing READ_ONCE() when reading the global iopolicy variable, leading to a data race.
--

--- Patch [14]: [PATCH v6 14/27] scsi-multipath: support iopolicy --- commit f1795dc3e5eb8bb57ed7783aab4a6563ae5c3b62
Author: John Garry <[email protected]>

scsi-multipath: support iopolicy

Add support to set the multipath iopolicy. The iopolicy member is per scsi_mpath_head structure, and a module param is added so that the default iopolicy may be set.

Quoted 8 lines
drivers/scsi/scsi_multipath.c +0 -0
index 1ec7bf1835de0..593f94aac70e9 100644
@@ -61,6 +61,23 @@ static const struct kernel_param_ops multipath_param_ops = {
 module_param_cb(multipath, &multipath_param_ops, &scsi_multipath, 0444);
 MODULE_PARM_DESC(multipath, "turn on native multipath support, options: on, off, always");
 

drivers/scsi/scsi_multipath.c static const struct kernel_param_ops multipath_param_ops = { · line 73

Quoted 10 lines
drivers/scsi/scsi_multipath.c +10 -0
+static enum mpath_iopolicy_e iopolicy = MPATH_IOPOLICY_NUMA;
+
+static int scsi_mpath_set_iopolicy_param(const char *val, const struct kernel_param *kp)
+{
+	return mpath_set_iopolicy(val, &iopolicy);
+}
+
+static int scsi_mpath_get_iopolicy_param(char *buf, const struct kernel_param *kp)
+{
+	return mpath_get_iopolicy(buf, iopolicy);

[Severity: Low]
Is a READ_ONCE() missing here when reading the global iopolicy variable?

mpath_set_iopolicy() updates the variable using WRITE_ONCE(). Concurrent reads here could lead to a data race and trigger KCSAN warnings.

Quoted 7 lines
drivers/scsi/scsi_multipath.c +7 -0
+}
+
+module_param_call(multipath_iopolicy, scsi_mpath_set_iopolicy_param,
+		scsi_mpath_get_iopolicy_param, &iopolicy, 0644);
+MODULE_PARM_DESC(multipath_iopolicy,
+	"Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'");
+

[ ... ]

drivers/scsi/scsi_multipath.c static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id) · line 256

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
@@ -204,6 +254,8 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
 	if (mpath_head_init(&scsi_mpath_head->mpath_head))
 		goto out_free;
 	scsi_mpath_head->mpath_head.mpdt = &smpdt;

[Severity: Critical]
Since the scsi_mpath_head is initialized with smpdt here, and smpdt appears to be an empty template, could this lead to NULL pointer dereferences in the core multipath library?

drivers/scsi/scsi_multipath.c:
static struct mpath_head_template smpdt = {
};

The core code unconditionally dereferences these callbacks. For example, during path validation:

lib/multipath.c:mpath_path_is_disabled() {
    ...
    return mpath_head->mpdt->is_disabled(mpath_device);
}

drivers/scsi/scsi_multipath.c static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id) · line 257

Quoted 1 line
drivers/scsi/scsi_multipath.c +1 -0
+	scsi_mpath_head->iopolicy = iopolicy;

[Severity: Low]
Would it be better to also use READ_ONCE() here when reading the global iopolicy variable during initialization?

Quoted 4 lines
drivers/scsi/scsi_multipath.c +1 -0
+	scsi_mpath_head->mpath_head.iopolicy = &scsi_mpath_head->iopolicy;
 
 	strscpy(scsi_mpath_head->vpd_id, vpd_id,
 		SCSI_MPATH_DEVICE_ID_LEN);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author
#30 lore raw

Re: [PATCH v6 14/27] scsi-multipath: support iopolicy

Quoted 50 lines · On 29/07/2026 15:07, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] The struct mpath_head_template 'smpdt' is entirely empty, lacking implementations for logically required callbacks, leading to NULL pointer dereferences.
- [Low] Missing READ_ONCE() when reading the global iopolicy variable, leading to a data race.
--

--- Patch [14]: [PATCH v6 14/27] scsi-multipath: support iopolicy --- commit f1795dc3e5eb8bb57ed7783aab4a6563ae5c3b62
Author: John Garry <[email protected]>

scsi-multipath: support iopolicy

Add support to set the multipath iopolicy. The iopolicy member is per scsi_mpath_head structure, and a module param is added so that the default iopolicy may be set.

Quoted 8 lines
drivers/scsi/scsi_multipath.c +0 -0
index 1ec7bf1835de0..593f94aac70e9 100644
@@ -61,6 +61,23 @@ static const struct kernel_param_ops multipath_param_ops = {
  module_param_cb(multipath, &multipath_param_ops, &scsi_multipath, 0444);
  MODULE_PARM_DESC(multipath, "turn on native multipath support, options: on, off, always");
 

drivers/scsi/scsi_multipath.c static const struct kernel_param_ops multipath_param_ops = { · line 73

Quoted 10 lines
drivers/scsi/scsi_multipath.c +10 -0
+static enum mpath_iopolicy_e iopolicy = MPATH_IOPOLICY_NUMA;
+
+static int scsi_mpath_set_iopolicy_param(const char *val, const struct kernel_param *kp)
+{
+	return mpath_set_iopolicy(val, &iopolicy);
+}
+
+static int scsi_mpath_get_iopolicy_param(char *buf, const struct kernel_param *kp)
+{
+	return mpath_get_iopolicy(buf, iopolicy);

[Severity: Low]
Is a READ_ONCE() missing here when reading the global iopolicy variable?

mpath_set_iopolicy() updates the variable using WRITE_ONCE(). Concurrent reads here could lead to a data race and trigger KCSAN warnings.

Quoted 7 lines
drivers/scsi/scsi_multipath.c +7 -0
+}
+
+module_param_call(multipath_iopolicy, scsi_mpath_set_iopolicy_param,
+		scsi_mpath_get_iopolicy_param, &iopolicy, 0644);
+MODULE_PARM_DESC(multipath_iopolicy,
+	"Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'");
+

[ ... ]

Quoted 10 lines

drivers/scsi/scsi_multipath.c static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id) · line 256

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
@@ -204,6 +254,8 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
  	if (mpath_head_init(&scsi_mpath_head->mpath_head))
  		goto out_free;
  	scsi_mpath_head->mpath_head.mpdt = &smpdt;

[Severity: Critical]
Since the scsi_mpath_head is initialized with smpdt here, and smpdt appears to be an empty template, could this lead to NULL pointer dereferences in the core multipath library?

It is filled in later. And that is not even related to this patch.

Quoted 12 lines

drivers/scsi/scsi_multipath.c:
static struct mpath_head_template smpdt = {
};

The core code unconditionally dereferences these callbacks. For example, during path validation:

lib/multipath.c:mpath_path_is_disabled() {
     ...
     return mpath_head->mpdt->is_disabled(mpath_device);
}
Quoted 5 lines

drivers/scsi/scsi_multipath.c static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id) · line 257

Quoted 1 line
drivers/scsi/scsi_multipath.c +1 -0
+	scsi_mpath_head->iopolicy = iopolicy;

[Severity: Low]
Would it be better to also use READ_ONCE() here when reading the global iopolicy variable during initialization?

This won't make a difference, as iopolicy is only written once at module init time.

Quoted 6 lines
Quoted 5 lines
drivers/scsi/scsi_multipath.c +1 -0
+	scsi_mpath_head->mpath_head.iopolicy = &scsi_mpath_head->iopolicy;
 
  	strscpy(scsi_mpath_head->vpd_id, vpd_id,
  		SCSI_MPATH_DEVICE_ID_LEN);
author
#31 lore raw

PATCH v6 15/27 scsi-multipath: clone each bio

In reply to John Garry

From: John Garry <[email protected]>

For failover handling, we will take the approach to resubmit each bio.

However, unlike NVMe, for SCSI there is no guarantee that any bio submitted is either all or none completed.

As such, for SCSI, for failover handling we will take the approach to just re-submit the original bio. For this, clone and submit each bio.

---
 drivers/scsi/scsi_multipath.c | 33 +++++++++++++++++++++++++++++++++
 include/scsi/scsi_multipath.h |  1 +
 2 files changed, 34 insertions(+)
drivers/scsi/scsi_multipath.c +33 -0
index 593f94aac70e9..0d439cac6447a 100644
@@ -99,6 +99,7 @@ static void scsi_mpath_head_release(struct device *dev)
 		container_of(dev, struct scsi_mpath_head, dev);
 	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
 
+	bioset_exit(&scsi_mpath_head->bio_pool);
 	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
 	ida_destroy(&scsi_mpath_head->ida);
 	mpath_head_uninit(mpath_head);
@@ -237,7 +238,34 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+static void scsi_mpath_clone_end_io(struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	master_bio->bi_status = clone->bi_status;
+	bio_put(clone);
+	bio_endio(master_bio);
+}
+
+static struct bio *scsi_mpath_clone_bio(struct bio *bio)
+{
+	struct mpath_head *mpath_head = bio->bi_bdev->bd_disk->private_data;
+	struct scsi_mpath_head *scsi_mpath_head = to_scsi_mpath_head(mpath_head);
+	struct bio *clone;
+
+	clone = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
+				&scsi_mpath_head->bio_pool);
+	if (!clone)
+		return NULL;
+
+	clone->bi_end_io = scsi_mpath_clone_end_io;
+	clone->bi_private = bio;
+
+	return clone;
+}
+
 static struct mpath_head_template smpdt = {
+	.clone_bio = scsi_mpath_clone_bio,
 };
 
 static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
@@ -263,6 +291,9 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
 	scsi_mpath_head->index = ida_alloc(&scsi_multipath_dev_ida, GFP_KERNEL);
 	if (scsi_mpath_head->index < 0)
 		goto out_uninit_head;
+	if (bioset_init(&scsi_mpath_head->bio_pool, BIO_POOL_SIZE,
+			0, BIOSET_PERCPU_CACHE))
+		goto out_ida_free;
 	kref_init(&scsi_mpath_head->ref);
 
 	device_initialize(&scsi_mpath_head->dev);
@@ -282,6 +313,8 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(char *vpd_id)
 
 	return scsi_mpath_head;
 
+out_ida_free:
+	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
 out_uninit_head:
 	mpath_head_uninit(&scsi_mpath_head->mpath_head);
 out_free:
include/scsi/scsi_multipath.h +1 -0
index 2dc02313b0496..5cc37acaa664f 100644
@@ -26,6 +26,7 @@ struct scsi_mpath_head {
 	struct ida		ida;
 	struct kref		ref;
 	enum mpath_iopolicy_e	iopolicy;
+	struct bio_set		bio_pool;
 	struct device		dev;
 	int			index;
 };
Signature · 1 line
2.43.7
#32 lore raw

Re: [PATCH v6 15/27] scsi-multipath: clone each bio

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The `smpdt` structure fails to implement mandatory `mpath_head_template` callbacks, leading to a…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The smpdt structure fails to implement mandatory mpath_head_template callbacks, leading to a NULL pointer dereference.
- [High] The clone completion callback entirely lacks the failover resubmission logic claimed in the commit message, breaking failover handling.
- [High] The bio_pool mempool is destroyed before waiting for active users to drain, leading to a use-after-free when in-flight cloned bios complete. --

--- Patch [15]: [PATCH v6 15/27] scsi-multipath: clone each bio --- commit ea53159f3e5b8959582afd4b035267bb7a201a6d
Author: John Garry <[email protected]>

scsi-multipath: clone each bio

For failover handling, we will take the approach to resubmit each bio.

However, unlike NVMe, for SCSI there is no guarantee that any bio submitted is either all or none completed.

As such, for SCSI, for failover handling we will take the approach to just re-submit the original bio. For this, clone and submit each bio.

drivers/scsi/scsi_multipath.c static void scsi_mpath_head_release(struct device *dev) · line 105

Quoted 12 lines
drivers/scsi/scsi_multipath.c +1 -0
index 593f94aac70e9..0d439cac6447a 100644
@@ -99,6 +99,7 @@ static void scsi_mpath_head_release(struct device *dev)
 		container_of(dev, struct scsi_mpath_head, dev);
 	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
 
+	bioset_exit(&scsi_mpath_head->bio_pool);
 	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
 	ida_destroy(&scsi_mpath_head->ida);
 	mpath_head_uninit(mpath_head);

[Severity: High]
Does this code destroy the mempool before all users have drained?

When the last SCSI path is removed, scsi_mpath_head_release() calls bioset_exit() immediately. It then calls mpath_head_uninit(), which synchronously waits for all users to release their references.

If there are in-flight cloned bios completing, or if the block device is still open and receives new I/O while waiting, they could access the already-freed bio_pool via bio_put() or bio_alloc_clone().

Can this result in a use-after-free on the destroyed mempool?

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 248

Quoted 12 lines
drivers/scsi/scsi_multipath.c +8 -0
@@ -237,7 +238,34 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+static void scsi_mpath_clone_end_io(struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	master_bio->bi_status = clone->bi_status;
+	bio_put(clone);
+	bio_endio(master_bio);
+}

[Severity: High]
Does this callback omit the failover resubmission logic mentioned in the commit message?

When an underlying SCSI path fails an I/O, this function unconditionally copies the error to master_bio and calls bio_endio().

Since the commit message states the approach is to re-submit the original bio for failover handling, shouldn't there be code here to intercept the error and requeue it, rather than immediately returning the error to the issuer?

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 269

Quoted 3 lines
drivers/scsi/scsi_multipath.c +1 -0
 static struct mpath_head_template smpdt = {
+	.clone_bio = scsi_mpath_clone_bio,
 };

[Severity: High]
Is this struct missing mandatory mpath_head_template callbacks?

Any I/O submission to the multipath block device invokes mpath_find_path(), which unconditionally calls mpath_head->mpdt->is_disabled(mpath_device).

Because smpdt only initializes .clone_bio, functions like is_disabled, is_optimized, and available_path will be NULL.

Will this cause an immediate kernel panic due to a NULL pointer dereference when an I/O is issued?

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

Re: [PATCH v6 15/27] scsi-multipath: clone each bio

Quoted 29 lines · On 29/07/2026 15:23, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The smpdt structure fails to implement mandatory mpath_head_template callbacks, leading to a NULL pointer dereference.
- [High] The clone completion callback entirely lacks the failover resubmission logic claimed in the commit message, breaking failover handling.
- [High] The bio_pool mempool is destroyed before waiting for active users to drain, leading to a use-after-free when in-flight cloned bios complete. --

--- Patch [15]: [PATCH v6 15/27] scsi-multipath: clone each bio --- commit ea53159f3e5b8959582afd4b035267bb7a201a6d
Author: John Garry <[email protected]>

scsi-multipath: clone each bio

For failover handling, we will take the approach to resubmit each bio.

However, unlike NVMe, for SCSI there is no guarantee that any bio submitted is either all or none completed.

As such, for SCSI, for failover handling we will take the approach to just re-submit the original bio. For this, clone and submit each bio.

Quoted 8 lines
drivers/scsi/scsi_multipath.c +0 -0
index 593f94aac70e9..0d439cac6447a 100644
@@ -99,6 +99,7 @@ static void scsi_mpath_head_release(struct device *dev)
  		container_of(dev, struct scsi_mpath_head, dev);
  	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
 
Quoted 7 lines

drivers/scsi/scsi_multipath.c static void scsi_mpath_head_release(struct device *dev) · line 105

Quoted 4 lines
drivers/scsi/scsi_multipath.c +1 -0
+	bioset_exit(&scsi_mpath_head->bio_pool);
  	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
  	ida_destroy(&scsi_mpath_head->ida);
  	mpath_head_uninit(mpath_head);

[Severity: High]
Does this code destroy the mempool before all users have drained?

Yes, all bios should be finished at this stage

Quoted 30 lines

When the last SCSI path is removed, scsi_mpath_head_release() calls bioset_exit() immediately. It then calls mpath_head_uninit(), which synchronously waits for all users to release their references.

If there are in-flight cloned bios completing, or if the block device is still open and receives new I/O while waiting, they could access the already-freed bio_pool via bio_put() or bio_alloc_clone().

Can this result in a use-after-free on the destroyed mempool?

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 248

Quoted 12 lines
drivers/scsi/scsi_multipath.c +8 -0
@@ -237,7 +238,34 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
  	return 0;
  }
 
+static void scsi_mpath_clone_end_io(struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	master_bio->bi_status = clone->bi_status;
+	bio_put(clone);
+	bio_endio(master_bio);
+}

[Severity: High]
Does this callback omit the failover resubmission logic mentioned in the commit message?

Quoted 8 lines

When an underlying SCSI path fails an I/O, this function unconditionally copies the error to master_bio and calls bio_endio().

Since the commit message states the approach is to re-submit the original bio for failover handling, shouldn't there be code here to intercept the error and requeue it, rather than immediately returning the error to the issuer?

this is done in a later patch

Quoted 9 lines

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 269

Quoted 3 lines
  static struct mpath_head_template smpdt = {
drivers/scsi/scsi_multipath.c +1 -0
+	.clone_bio = scsi_mpath_clone_bio,
  };

[Severity: High]
Is this struct missing mandatory mpath_head_template callbacks?

Quoted 9 lines

Any I/O submission to the multipath block device invokes mpath_find_path(), which unconditionally calls mpath_head->mpdt->is_disabled(mpath_device).

Because smpdt only initializes .clone_bio, functions like is_disabled, is_optimized, and available_path will be NULL.

Will this cause an immediate kernel panic due to a NULL pointer dereference when an I/O is issued?

they are added later

author
#34 lore raw

PATCH v6 16/27 scsi-multipath: clear path when device is blocked

In reply to John Garry

From: John Garry <[email protected]>

Add scsi_mpath_dev_clear_path() to clear a device path when it becomes blocked.

---
 drivers/scsi/scsi_lib.c       |  3 +++
 drivers/scsi/scsi_multipath.c | 10 ++++++++++
 include/scsi/scsi_multipath.h |  5 +++++
 3 files changed, 18 insertions(+)
drivers/scsi/scsi_lib.c +3 -0
index 22e2e3223440d..e2452392671f5 100644
@@ -35,6 +35,7 @@
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_transport.h> /* scsi_init_limits() */
 #include <scsi/scsi_dh.h>
+#include <scsi/scsi_multipath.h>
 
 #include <trace/events/scsi.h>
 
@@ -3016,6 +3017,8 @@ static void scsi_device_block(struct scsi_device *sdev, void *data)
 		scsi_stop_queue(sdev);
 
 	mutex_unlock(&sdev->state_mutex);
+	if ((err == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_dev_clear_path(sdev->scsi_mpath_dev);
 
 	WARN_ONCE(err, "%s: failed to block %s in state %d\n",
 		  __func__, dev_name(&sdev->sdev_gendev), state);
drivers/scsi/scsi_multipath.c +10 -0
index 0d439cac6447a..b23cd6f12c602 100644
@@ -238,6 +238,16 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+       if (mpath_clear_current_path(mpath_device))
+               mpath_synchronize(mpath_head);
+}
+
 static void scsi_mpath_clone_end_io(struct bio *clone)
 {
 	struct bio *master_bio = clone->bi_private;
include/scsi/scsi_multipath.h +5 -0
index 5cc37acaa664f..8f58c44d96a77 100644
@@ -54,6 +54,7 @@ void scsi_mpath_add_sysfs_link(struct scsi_device *sdev);
 void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev);
 int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
+void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev);
 #else /* CONFIG_SCSI_MULTIPATH */
 
 struct scsi_mpath_head {
@@ -88,6 +89,10 @@ static inline
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head)
 {
 }
+static inline
+void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
+{
+}
 static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
 {
 }
Signature · 1 line
2.43.7
#35 lore raw

Re: [PATCH v6 16/27] scsi-multipath: clear path when device is blocked

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unprotected, lockless access to `sdev->scsi_mpath_dev` in `scsi_device_block()` causes NULL…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unprotected, lockless access to sdev->scsi_mpath_dev in scsi_device_block() causes NULL pointer dereferences and Use-After-Free (UAF) bugs when racing with device initialization or teardown. --

--- Patch [16]: [PATCH v6 16/27] scsi-multipath: clear path when device is blocked --- commit 8f07d9f873129f9ecc9b8d3d02fee5bce667c745
Author: John Garry <[email protected]>

scsi-multipath: clear path when device is blocked

Add scsi_mpath_dev_clear_path() to clear a device path when it becomes blocked.

Quoted 4 lines
drivers/scsi/scsi_lib.c +0 -0
index daeb3693fe559..a1ec11050090c 100644

[ ... ]

drivers/scsi/scsi_lib.c static void scsi_device_block(struct scsi_device *sdev, void *data) · line 3013

Quoted 6 lines
drivers/scsi/scsi_lib.c +2 -0
@@ -3008,6 +3009,8 @@ static void scsi_device_block(struct scsi_device *sdev, void *data)
 		scsi_stop_queue(sdev);
 
 	mutex_unlock(&sdev->state_mutex);
+	if ((err == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_dev_clear_path(sdev->scsi_mpath_dev);

[Severity: High]
Is it safe to access sdev->scsi_mpath_dev without holding state_mutex?

When scsi_alloc_sdev() adds a new device to shost->__devices, it becomes globally visible. Then scsi_mpath_dev_alloc() sets sdev->scsi_mpath_dev but leaves scsi_mpath_head as NULL while issuing SCSI VPD queries.

If a concurrent error on the bus triggers scsi_device_block(), it could read the partially initialized sdev->scsi_mpath_dev here outside of any locks.

Additionally, if scsi_mpath_dev_alloc() encounters an error and frees sdev->scsi_mpath_dev, could reading it here cause a use-after-free?

During teardown, if __scsi_remove_device() is called concurrently with scsi_device_block(), the teardown thread might set scsi_mpath_head to NULL. Can this lead to a NULL pointer dereference when evaluating scsi_mpath_dev_clear_path()?

Quoted 3 lines
drivers/scsi/scsi_lib.c +0 -0
 	WARN_ONCE(err, "%s: failed to block %s in state %d\n",
 		  __func__, dev_name(&sdev->sdev_gendev), state);

[ ... ]

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
index 0d439cac6447a..b23cd6f12c602 100644

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 245

Quoted 9 lines
drivers/scsi/scsi_multipath.c +5 -0
@@ -238,6 +238,16 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;

[Severity: High]
Does this code dereference a NULL pointer if it races with initialization or teardown?

If scsi_mpath_head is NULL due to the races mentioned above, deriving mpath_head from it will calculate an invalid pointer.

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 247

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+
+       if (mpath_clear_current_path(mpath_device))

[Severity: High]
Could calling mpath_clear_current_path() with a partially initialized mpath_device lead to a NULL pointer dereference?

If mpath_device->mpath_head is NULL, it will trigger an immediate dereference in the multipath code:

lib/multipath.c:mpath_clear_current_path() {
    ...
	struct mpath_head *mpath_head = mpath_device->mpath_head;
    ...
		if (mpath_device ==
			rcu_access_pointer(mpath_head->current_path[node])) {
    ...
}
Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+               mpath_synchronize(mpath_head);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author
#36 lore raw

Re: [PATCH v6 16/27] scsi-multipath: clear path when device is blocked

Quoted 4 lines · On 29/07/2026 15:42, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unprotected, lockless access to sdev->scsi_mpath_dev in scsi_device_block() causes NULL pointer dereferences and Use-After-Free (UAF) bugs when racing with device initialization or teardown. --

Same as v5, there should be not races

Quoted 93 lines

--- Patch [16]: [PATCH v6 16/27] scsi-multipath: clear path when device is blocked --- commit 8f07d9f873129f9ecc9b8d3d02fee5bce667c745
Author: John Garry <[email protected]>

scsi-multipath: clear path when device is blocked

Add scsi_mpath_dev_clear_path() to clear a device path when it becomes blocked.

Quoted 4 lines
drivers/scsi/scsi_lib.c +0 -0
index daeb3693fe559..a1ec11050090c 100644

[ ... ]

drivers/scsi/scsi_lib.c static void scsi_device_block(struct scsi_device *sdev, void *data) · line 3013

Quoted 6 lines
drivers/scsi/scsi_lib.c +2 -0
@@ -3008,6 +3009,8 @@ static void scsi_device_block(struct scsi_device *sdev, void *data)
  		scsi_stop_queue(sdev);
 
  	mutex_unlock(&sdev->state_mutex);
+	if ((err == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_dev_clear_path(sdev->scsi_mpath_dev);

[Severity: High]
Is it safe to access sdev->scsi_mpath_dev without holding state_mutex?

When scsi_alloc_sdev() adds a new device to shost->__devices, it becomes globally visible. Then scsi_mpath_dev_alloc() sets sdev->scsi_mpath_dev but leaves scsi_mpath_head as NULL while issuing SCSI VPD queries.

If a concurrent error on the bus triggers scsi_device_block(), it could read the partially initialized sdev->scsi_mpath_dev here outside of any locks.

Additionally, if scsi_mpath_dev_alloc() encounters an error and frees sdev->scsi_mpath_dev, could reading it here cause a use-after-free?

During teardown, if __scsi_remove_device() is called concurrently with scsi_device_block(), the teardown thread might set scsi_mpath_head to NULL. Can this lead to a NULL pointer dereference when evaluating scsi_mpath_dev_clear_path()?

Quoted 3 lines
drivers/scsi/scsi_lib.c +0 -0
  	WARN_ONCE(err, "%s: failed to block %s in state %d\n",
  		  __func__, dev_name(&sdev->sdev_gendev), state);

[ ... ]

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
index 0d439cac6447a..b23cd6f12c602 100644

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 245

Quoted 9 lines
drivers/scsi/scsi_multipath.c +5 -0
@@ -238,6 +238,16 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
  	return 0;
  }
 
+void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;

[Severity: High]
Does this code dereference a NULL pointer if it races with initialization or teardown?

If scsi_mpath_head is NULL due to the races mentioned above, deriving mpath_head from it will calculate an invalid pointer.

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 247

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+
+       if (mpath_clear_current_path(mpath_device))

[Severity: High]
Could calling mpath_clear_current_path() with a partially initialized mpath_device lead to a NULL pointer dereference?

If mpath_device->mpath_head is NULL, it will trigger an immediate dereference in the multipath code:

lib/multipath.c:mpath_clear_current_path() {
     ...
	struct mpath_head *mpath_head = mpath_device->mpath_head;
     ...
		if (mpath_device ==
			rcu_access_pointer(mpath_head->current_path[node])) {
     ...
}
Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+               mpath_synchronize(mpath_head);
+}
author
#37 lore raw

PATCH v6 17/27 scsi-multipath: revalidate paths upon device unblock

In reply to John Garry

From: John Garry <[email protected]>

When a scsi_device is unblocked, we need revalidate paths which means that we clear paths and requeue any queued bios.

---
 drivers/scsi/scsi_lib.c       | 2 ++
 drivers/scsi/scsi_multipath.c | 8 ++++++++
 include/scsi/scsi_multipath.h | 5 +++++
 3 files changed, 15 insertions(+)
drivers/scsi/scsi_lib.c +2 -0
index e2452392671f5..41d201b7e7def 100644
@@ -3100,6 +3100,8 @@ static int scsi_internal_device_unblock(struct scsi_device *sdev,
 	mutex_lock(&sdev->state_mutex);
 	ret = scsi_internal_device_unblock_nowait(sdev, new_state);
 	mutex_unlock(&sdev->state_mutex);
+	if ((ret == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_revalidate_paths(sdev->scsi_mpath_dev);
 
 	return ret;
 }
drivers/scsi/scsi_multipath.c +8 -0
index b23cd6f12c602..12750621a7fc0 100644
@@ -238,6 +238,14 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+       mpath_revalidate_paths(mpath_head);
+}
+
 void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
 {
        struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
include/scsi/scsi_multipath.h +5 -0
index 8f58c44d96a77..cc77e63c43da3 100644
@@ -55,6 +55,7 @@ void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev);
 int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev);
+void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev);
 #else /* CONFIG_SCSI_MULTIPATH */
 
 struct scsi_mpath_head {
@@ -93,6 +94,10 @@ static inline
 void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
 {
 }
+static inline
+void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
+{
+}
 static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
 {
 }
Signature · 1 line
2.43.7
#38 lore raw

Re: [PATCH v6 17/27] scsi-multipath: revalidate paths upon device unblock

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

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

New issues:
- [High] Calling scsi_mpath_revalidate_paths() outside sdev->state_mutex without a NULL check leads to TOCTOU races with device removal or allocation, causing a NULL pointer dereference.
- [High] Direct callers of scsi_internal_device_unblock_nowait bypass path revalidation, which can leave queued bios indefinitely stalled.

Pre-existing issues:
- [High] The patch calls mpath_revalidate_paths(), which schedules requeue_work on a background workqueue, but the teardown path fails to cancel this work, leading to a Use-After-Free. --

--- Patch [17]: [PATCH v6 17/27] scsi-multipath: revalidate paths upon device unblock --- commit 6253cb1fc352f4ec6d0d42ac99c1827b35592ff4
Author: John Garry <[email protected]>

scsi-multipath: revalidate paths upon device unblock

When a scsi_device is unblocked, we need revalidate paths which means that we clear paths and requeue any queued bios.

drivers/scsi/scsi_lib.c static int scsi_internal_device_unblock(struct scsi_device *sdev, · line 3096

Quoted 10 lines
drivers/scsi/scsi_lib.c +2 -0
index a1ec11050090c..6a2d7dbfbe8c3 100644
@@ -3092,6 +3092,8 @@ static int scsi_internal_device_unblock(struct scsi_device *sdev,
 	mutex_lock(&sdev->state_mutex);
 	ret = scsi_internal_device_unblock_nowait(sdev, new_state);
 	mutex_unlock(&sdev->state_mutex);
+	if ((ret == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_revalidate_paths(sdev->scsi_mpath_dev);

[Severity: High]
Is it safe to access sdev->scsi_mpath_dev and call scsi_mpath_revalidate_paths() outside of sdev->state_mutex?

It seems that concurrently, __scsi_remove_device() could nullify scsi_mpath_dev->scsi_mpath_head. A similar race might exist during device initialization before the head is populated.

When scsi_mpath_revalidate_paths() is then called, it blindly computes the offset to &scsi_mpath_head->mpath_head, which could lead to a NULL pointer dereference.

[Severity: High]
Does placing this hook in the wrapper bypass path revalidation for some drivers?

Low-level drivers like mpt3sas and scsi_transport_srp call the core API scsi_internal_device_unblock_nowait() directly. Since the revalidation is only added to the scsi_internal_device_unblock() wrapper, won't devices managed by those drivers miss path revalidation when unblocked, potentially leaving queued bios permanently stalled?

Quoted 7 lines
drivers/scsi/scsi_lib.c +0 -0
 	return ret;
 }
drivers/scsi/scsi_multipath.c +0 -0
index b23cd6f12c602..12750621a7fc0 100644

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 246

Quoted 10 lines
drivers/scsi/scsi_multipath.c +6 -0
@@ -238,6 +238,14 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
 	return 0;
 }
 
+void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+       mpath_revalidate_paths(mpath_head);

[Severity: High]
This is a pre-existing issue, but by introducing this new asynchronous trigger, could we run into a use-after-free during device removal?

Calling mpath_revalidate_paths() queues mpath_head->requeue_work. If the device is subsequently removed, the teardown path in scsi_mpath_head_release() frees scsi_mpath_head via kfree():

drivers/scsi/scsi_multipath.c:scsi_mpath_head_release() {
    ...
    mpath_head_uninit(mpath_head);
    kfree(scsi_mpath_head);
}

Because cancel_work_sync(&mpath_head->requeue_work) is missing from the teardown logic, could the pending work execute on a freed mpath_head structure?

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author
#39 lore raw

Re: [PATCH v6 17/27] scsi-multipath: revalidate paths upon device unblock

Quoted 4 lines · On 29/07/2026 15:54, [email protected] wrote:

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

New issues:
- [High] Calling scsi_mpath_revalidate_paths() outside sdev->state_mutex without a NULL check leads to TOCTOU races with device removal or allocation, causing a NULL pointer dereference.

Same as v5 response, this should not happen

Quoted 1 line

- [High] Direct callers of scsi_internal_device_unblock_nowait bypass path revalidation, which can leave queued bios indefinitely stalled.

can you name relevant situations?

Quoted 3 lines

Pre-existing issues:
- [High] The patch calls mpath_revalidate_paths(), which schedules requeue_work on a background workqueue, but the teardown path fails to cancel this work, leading to a Use-After-Free.

this is done by (disk) sd driver through calling mpath_put_disk()

Quoted 82 lines

--

--- Patch [17]: [PATCH v6 17/27] scsi-multipath: revalidate paths upon device unblock --- commit 6253cb1fc352f4ec6d0d42ac99c1827b35592ff4
Author: John Garry <[email protected]>

scsi-multipath: revalidate paths upon device unblock

When a scsi_device is unblocked, we need revalidate paths which means that we clear paths and requeue any queued bios.

drivers/scsi/scsi_lib.c static int scsi_internal_device_unblock(struct scsi_device *sdev, · line 3096

Quoted 10 lines
drivers/scsi/scsi_lib.c +2 -0
index a1ec11050090c..6a2d7dbfbe8c3 100644
@@ -3092,6 +3092,8 @@ static int scsi_internal_device_unblock(struct scsi_device *sdev,
  	mutex_lock(&sdev->state_mutex);
  	ret = scsi_internal_device_unblock_nowait(sdev, new_state);
  	mutex_unlock(&sdev->state_mutex);
+	if ((ret == 0) && sdev->scsi_mpath_dev)
+		scsi_mpath_revalidate_paths(sdev->scsi_mpath_dev);

[Severity: High]
Is it safe to access sdev->scsi_mpath_dev and call scsi_mpath_revalidate_paths() outside of sdev->state_mutex?

It seems that concurrently, __scsi_remove_device() could nullify scsi_mpath_dev->scsi_mpath_head. A similar race might exist during device initialization before the head is populated.

When scsi_mpath_revalidate_paths() is then called, it blindly computes the offset to &scsi_mpath_head->mpath_head, which could lead to a NULL pointer dereference.

[Severity: High]
Does placing this hook in the wrapper bypass path revalidation for some drivers?

Low-level drivers like mpt3sas and scsi_transport_srp call the core API scsi_internal_device_unblock_nowait() directly. Since the revalidation is only added to the scsi_internal_device_unblock() wrapper, won't devices managed by those drivers miss path revalidation when unblocked, potentially leaving queued bios permanently stalled?

Quoted 7 lines
drivers/scsi/scsi_lib.c +0 -0
  	return ret;
  }
drivers/scsi/scsi_multipath.c +0 -0
index b23cd6f12c602..12750621a7fc0 100644

[ ... ]

drivers/scsi/scsi_multipath.c static int scsi_multipath_sdev_init(struct scsi_device *sdev) · line 246

Quoted 10 lines
drivers/scsi/scsi_multipath.c +6 -0
@@ -238,6 +238,14 @@ static int scsi_multipath_sdev_init(struct scsi_device *sdev)
  	return 0;
  }
 
+void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
+{
+       struct scsi_mpath_head *scsi_mpath_head = scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+       mpath_revalidate_paths(mpath_head);

[Severity: High]
This is a pre-existing issue, but by introducing this new asynchronous trigger, could we run into a use-after-free during device removal?

Calling mpath_revalidate_paths() queues mpath_head->requeue_work. If the device is subsequently removed, the teardown path in scsi_mpath_head_release() frees scsi_mpath_head via kfree():

drivers/scsi/scsi_multipath.c:scsi_mpath_head_release() {
     ...
     mpath_head_uninit(mpath_head);
     kfree(scsi_mpath_head);
}

Because cancel_work_sync(&mpath_head->requeue_work) is missing from the teardown logic, could the pending work execute on a freed mpath_head structure?

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+}
+
author
#40 lore raw

PATCH v6 18/27 scsi-multipath: failover handling

In reply to John Garry

From: John Garry <[email protected]>

Failover occurs when the scsi_cmnd has failed and it is discovered that the target scsi_device has transport down.

For a scsi command which suffers failover, requeue the master bio of each bio attached to its request.

A bio which for which failover occurs is handled in scsi_mpath_clone_end_io(). Failover is detected for blk_path_error() occuring, same as how dm-mpath detects this.

---
 drivers/scsi/scsi_multipath.c | 39 +++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/scsi/scsi_multipath.c +39 -0
index 12750621a7fc0..6ced89576368c 100644
@@ -256,10 +256,49 @@ void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)
                mpath_synchronize(mpath_head);
 }
 
+static inline void bio_list_add_clone(struct bio_list *bl,
+				struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	if (bl->tail)
+		bl->tail->bi_next = master_bio;
+	else
+		bl->head = master_bio;
+	bl->tail = master_bio;
+	bio_put(clone);
+}
+
 static void scsi_mpath_clone_end_io(struct bio *clone)
 {
 	struct bio *master_bio = clone->bi_private;
 
+	if (clone->bi_status && blk_path_error(clone->bi_status)) {
+		struct block_device *bi_bdev = clone->bi_bdev;
+		struct request_queue *q = bi_bdev->bd_queue;
+		struct scsi_device *sdev = scsi_device_from_queue(q);
+		struct scsi_mpath_device *scsi_mpath_dev;
+		struct mpath_device *mpath_device;
+		struct mpath_head *mpath_head;
+		unsigned long flags;
+
+		if (!sdev)
+			goto end_bio;
+
+		scsi_mpath_dev = sdev->scsi_mpath_dev;
+		mpath_device = &scsi_mpath_dev->mpath_device;
+		mpath_head = mpath_device->mpath_head;
+
+		spin_lock_irqsave(&mpath_head->requeue_lock, flags);
+		bio_list_add_clone(&mpath_head->requeue_list, clone);
+		spin_unlock_irqrestore(&mpath_head->requeue_lock, flags);
+
+		mpath_schedule_requeue_work(mpath_head);
+		put_device(&sdev->sdev_gendev);
+		return;
+	}
+
+end_bio:
 	master_bio->bi_status = clone->bi_status;
 	bio_put(clone);
 	bio_endio(master_bio);
Signature · 1 line
2.43.7
#41 lore raw

Re: [PATCH v6 18/27] scsi-multipath: failover handling

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] List corruption and use-after-free occur due to `bio_list_add_clone()` failing to nullify…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] List corruption and use-after-free occur due to bio_list_add_clone() failing to nullify master_bio->bi_next.
- [High] A use-after-free crash occurs because cancel_work_sync() is missing during mpath_head teardown. --

--- Patch [18]: [PATCH v6 18/27] scsi-multipath: failover handling --- commit d3d46b0330ddb0dc22577a7546f9765b9e1a4651
Author: John Garry <[email protected]>

scsi-multipath: failover handling

For a scsi command which suffers failover, requeue the master bio of each bio attached to its request.

Quoted 5 lines
drivers/scsi/scsi_multipath.c +0 -0
index 12750621a7fc0..6ced89576368c 100644
@@ -256,10 +256,49 @@ void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)

[ ... ]

drivers/scsi/scsi_multipath.c void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev) · line 270

Quoted 12 lines
drivers/scsi/scsi_multipath.c +12 -0
+static inline void bio_list_add_clone(struct bio_list *bl,
+				struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	if (bl->tail)
+		bl->tail->bi_next = master_bio;
+	else
+		bl->head = master_bio;
+	bl->tail = master_bio;
+	bio_put(clone);
+}

[Severity: High]
Will this leave the newly appended list element improperly terminated?

This custom insertion into requeue_list appears to omit the standard master_bio->bi_next = NULL assignment for the new list tail.

If master_bio->bi_next happens to contain a stale pointer when it is added here, could iterating the list later result in memory corruption or memory being accessed after it has been freed?

[ ... ]

Quoted 5 lines
drivers/scsi/scsi_multipath.c +5 -0
+static void scsi_mpath_clone_end_io(struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	if (clone->bi_status && blk_path_error(clone->bi_status)) {

[ ... ]

drivers/scsi/scsi_multipath.c void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev) · line 296

Quoted 5 lines
drivers/scsi/scsi_multipath.c +5 -0
+		spin_lock_irqsave(&mpath_head->requeue_lock, flags);
+		bio_list_add_clone(&mpath_head->requeue_list, clone);
+		spin_unlock_irqrestore(&mpath_head->requeue_lock, flags);
+
+		mpath_schedule_requeue_work(mpath_head);

[Severity: High]
Is it possible for the scheduled requeue work item to run after the mpath_head structure has been torn down?

If a device hot-unplug overlaps with I/O path errors, requeue_work is scheduled here.

When teardown proceeds to remove the device and drops the last reference, it appears scsi_mpath_head_release() frees the scsi_mpath_head structure without calling cancel_work_sync() first.

Could this sequence lead to a delayed kworker executing on deallocated memory?

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

Re: [PATCH v6 18/27] scsi-multipath: failover handling

Quoted 2 lines · On 29/07/2026 16:14, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] List corruption and use-after-free occur due to bio_list_add_clone() failing to nullify master_bio->bi_next.

master_bio->bi_next as we never create a request for the master_bio

Quoted 48 lines

- [High] A use-after-free crash occurs because cancel_work_sync() is missing during mpath_head teardown. --

--- Patch [18]: [PATCH v6 18/27] scsi-multipath: failover handling --- commit d3d46b0330ddb0dc22577a7546f9765b9e1a4651
Author: John Garry <[email protected]>

scsi-multipath: failover handling

For a scsi command which suffers failover, requeue the master bio of each bio attached to its request.

Quoted 5 lines
drivers/scsi/scsi_multipath.c +0 -0
index 12750621a7fc0..6ced89576368c 100644
@@ -256,10 +256,49 @@ void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev)

[ ... ]

drivers/scsi/scsi_multipath.c void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev) · line 270

Quoted 12 lines
drivers/scsi/scsi_multipath.c +12 -0
+static inline void bio_list_add_clone(struct bio_list *bl,
+				struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	if (bl->tail)
+		bl->tail->bi_next = master_bio;
+	else
+		bl->head = master_bio;
+	bl->tail = master_bio;
+	bio_put(clone);
+}

[Severity: High]
Will this leave the newly appended list element improperly terminated?

This custom insertion into requeue_list appears to omit the standard master_bio->bi_next = NULL assignment for the new list tail.

If master_bio->bi_next happens to contain a stale pointer when it is added here, could iterating the list later result in memory corruption or memory being accessed after it has been freed?

[ ... ]

Quoted 5 lines
drivers/scsi/scsi_multipath.c +5 -0
+static void scsi_mpath_clone_end_io(struct bio *clone)
+{
+	struct bio *master_bio = clone->bi_private;
+
+	if (clone->bi_status && blk_path_error(clone->bi_status)) {

[ ... ]

Quoted 9 lines

drivers/scsi/scsi_multipath.c void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev) · line 296

Quoted 5 lines
drivers/scsi/scsi_multipath.c +5 -0
+		spin_lock_irqsave(&mpath_head->requeue_lock, flags);
+		bio_list_add_clone(&mpath_head->requeue_list, clone);
+		spin_unlock_irqrestore(&mpath_head->requeue_lock, flags);
+
+		mpath_schedule_requeue_work(mpath_head);

[Severity: High]
Is it possible for the scheduled requeue work item to run after the mpath_head structure has been torn down?

this flush is done at the disk teardown

Quoted 11 lines

If a device hot-unplug overlaps with I/O path errors, requeue_work is scheduled here.

When teardown proceeds to remove the device and drops the last reference, it appears scsi_mpath_head_release() frees the scsi_mpath_head structure without calling cancel_work_sync() first.

Could this sequence lead to a delayed kworker executing on deallocated memory?

author
#43 lore raw

PATCH v6 19/27 scsi-multipath: provide callbacks for path state

In reply to John Garry

Provide callbacks for .is_disabled, .is_optimized, and .available_path.

These all use scsi_device.sdev_state and scsi_device.access_state.

Member scsi_device.access_state will be driven by ALUA. Currently only device handlers support this, and in future we will have core SCSI support for implicit ALUA (not relying on device handlers).

---
 drivers/scsi/scsi_multipath.c | 49 +++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/scsi/scsi_multipath.c +49 -0
index 6ced89576368c..2a05f177a1dcd 100644
@@ -321,7 +321,56 @@ static struct bio *scsi_mpath_clone_bio(struct bio *bio)
 	return clone;
 }
 
+static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
+{
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+	struct scsi_device *sdev = scsi_mpath_dev->sdev;
+	unsigned char access_state = READ_ONCE(sdev->access_state) &
+					SCSI_ACCESS_STATE_MASK;
+
+	if (READ_ONCE(sdev->sdev_state) != SDEV_RUNNING)
+		return true;
+
+	if (access_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    access_state == SCSI_ACCESS_STATE_ACTIVE)
+		return false;
+
+	return true;
+}
+
+static bool scsi_mpath_is_optimized(struct mpath_device *mpath_device)
+{
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+	struct scsi_device *sdev = scsi_mpath_dev->sdev;
+	unsigned char access_state = READ_ONCE(sdev->access_state) &
+					SCSI_ACCESS_STATE_MASK;
+
+	if (READ_ONCE(sdev->sdev_state) != SDEV_RUNNING)
+		return false;
+
+	return access_state == SCSI_ACCESS_STATE_OPTIMAL;
+}
+
+static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
+{
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+	struct scsi_device *sdev = scsi_mpath_dev->sdev;
+	enum scsi_device_state sdev_state = READ_ONCE(sdev->sdev_state);
+
+	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_QUIESCE ||
+	    sdev_state == SDEV_BLOCK || sdev_state == SDEV_CREATED_BLOCK)
+		return true;
+
+	return false;
+}
+
 static struct mpath_head_template smpdt = {
+	.is_disabled = scsi_mpath_is_disabled,
+	.is_optimized = scsi_mpath_is_optimized,
+	.available_path = scsi_mpath_available_path,
 	.clone_bio = scsi_mpath_clone_bio,
 };
 
Signature · 1 line
2.43.7
#44 lore raw

Re: [PATCH v6 19/27] scsi-multipath: provide callbacks for path state

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

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

New issues:
- [High] scsi_mpath_is_disabled() returns true for STANDBY paths, preventing I/O and deadlocking implicit ALUA failovers.
- [High] Bios placed on the requeue_list during an ALUA state change are permanently stalled due to missing notification hooks.
- [High] ALUA path prioritization is broken because routing policies check a static struct field instead of the new .is_optimized callback.
- [High] scsi_mpath_available_path ignores ALUA offline state, causing indefinite I/O hangs instead of failing.
- [High] LBA-dependent paths are incorrectly flagged as disabled, preventing I/O.
- [High] SDEV_CREATED state is omitted from available paths, causing premature I/O failures.
- [High] I/O submitted during device quiesce (e.g., system suspend) will hang indefinitely because the requeue list is never flushed upon resume.

Pre-existing issues:
- [High] Missing cancellation of background requeue_work task before tearing down and freeing the scsi_mpath_head object. --

--- Patch [19]: [PATCH v6 19/27] scsi-multipath: provide callbacks for path state --- commit a79ca9463c83fa5a5480f800bdc6d5f3fef27829
Author: John Garry <[email protected]>

scsi-multipath: provide callbacks for path state

This commit provides callbacks for .is_disabled, .is_optimized, and .available_path. These callbacks use the scsi_device's sdev_state and access_state, which will be driven by ALUA.

Quoted 3 lines
drivers/scsi/scsi_multipath.c +0 -0

[ ... ]

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
+{

[ ... ]

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 340

Quoted 6 lines
drivers/scsi/scsi_multipath.c +6 -0
+	if (access_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    access_state == SCSI_ACCESS_STATE_ACTIVE)
+		return false;
+
+	return true;
+}

[Severity: High]
Will this return true for STANDBY paths?

If implicit ALUA relies on sending I/O to a STANDBY path to transition it to ACTIVE via a Unit Attention, marking it disabled might prevent the core from routing any I/O to it, potentially deadlocking the failover.

[Severity: High]
Does this also need to allow SCSI_ACCESS_STATE_LBA?

SPC-4 defines LBA-dependent states as active and capable of processing I/O commands. Should this state also be permitted here to avoid stalling I/O on storage arrays that rely on it?

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
+{

[ ... ]

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 368

Quoted 6 lines
drivers/scsi/scsi_multipath.c +6 -0
+	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_QUIESCE ||
+	    sdev_state == SDEV_BLOCK || sdev_state == SDEV_CREATED_BLOCK)
+		return true;
+
+	return false;
+}

[Severity: High]
Should SDEV_CREATED be included here?

During SCSI device probing, the device state is SDEV_CREATED and the core SCSI layer permits I/O operations like INQUIRY. If SDEV_CREATED is omitted here, could valid I/O be incorrectly failed as unavailable during device scanning?

[Severity: High]
By allowing SDEV_QUIESCE here, bios will be placed on the requeue list.

When the device is later unquiesced during a system resume, scsi_device_resume() sets the state back to SDEV_RUNNING, but it does not seem to trigger scsi_mpath_revalidate_paths() or schedule requeue_work.

Will bios queued during suspend remain stuck on the list indefinitely?

[Severity: High]
This function only checks sdev_state.

If an ALUA path transitions to SCSI_ACCESS_STATE_OFFLINE, sdev_state remains SDEV_RUNNING, which causes this callback to return true.

Could this cause the block layer to endlessly requeue the I/O, believing the path is temporarily unavailable, rather than failing it immediately for being permanently offline?

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 375

Quoted 6 lines

static struct mpath_head_template smpdt = {

drivers/scsi/scsi_multipath.c +3 -0
+	.is_disabled = scsi_mpath_is_disabled,
+	.is_optimized = scsi_mpath_is_optimized,
+	.available_path = scsi_mpath_available_path,
 	.clone_bio = scsi_mpath_clone_bio,
 };

[Severity: High]
How does this integration handle ALUA state changes for requeued I/O?

When no active paths are found, bios are placed on the requeue list:

lib/multipath.c:mpath_bdev_submit_bio()

    spin_lock_irq(&mpath_head->requeue_lock);
    bio_list_add(&mpath_head->requeue_list, bio);
    spin_unlock_irq(&mpath_head->requeue_lock);

When scsi_dh_alua.c handles an ALUA state change, does it notify scsi-multipath to schedule the requeue work? If not, could these bios remain permanently stalled?

[Severity: High]
Are the routing algorithms in lib/multipath.c actually using this new .is_optimized callback?

lib/multipath.c:__mpath_find_path()

    switch(mpath_device->access_state) {
    case MPATH_STATE_OPTIMIZED:

It appears the path finder checks the statically initialized access_state field instead of invoking the new callback. Will this cause all active paths to be incorrectly treated as optimized, bypassing path prioritization?

[Severity: High]
This is a pre-existing issue, but I noticed a potential use-after-free during device removal in scsi_mpath_head_release():

drivers/scsi/scsi_multipath.c:scsi_mpath_head_release() static void scsi_mpath_head_release(struct device *dev)

{
    ...
    mpath_head_uninit(mpath_head);
    kfree(scsi_mpath_head);
}

Since scsi_mpath_revalidate_paths() asynchronously schedules the requeue_work task, if the device is removed and the refcount drops to zero, the structure is freed without calling cancel_work_sync().

Could this allow pending work to execute on the freed scsi_mpath_head pointer?

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

Re: [PATCH v6 19/27] scsi-multipath: provide callbacks for path state

Quoted 46 lines · On 29/07/2026 16:43, [email protected] wrote:

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

New issues:
- [High] scsi_mpath_is_disabled() returns true for STANDBY paths, preventing I/O and deadlocking implicit ALUA failovers.
- [High] Bios placed on the requeue_list during an ALUA state change are permanently stalled due to missing notification hooks.
- [High] ALUA path prioritization is broken because routing policies check a static struct field instead of the new .is_optimized callback.
- [High] scsi_mpath_available_path ignores ALUA offline state, causing indefinite I/O hangs instead of failing.
- [High] LBA-dependent paths are incorrectly flagged as disabled, preventing I/O.
- [High] SDEV_CREATED state is omitted from available paths, causing premature I/O failures.
- [High] I/O submitted during device quiesce (e.g., system suspend) will hang indefinitely because the requeue list is never flushed upon resume.

Pre-existing issues:
- [High] Missing cancellation of background requeue_work task before tearing down and freeing the scsi_mpath_head object. --

--- Patch [19]: [PATCH v6 19/27] scsi-multipath: provide callbacks for path state --- commit a79ca9463c83fa5a5480f800bdc6d5f3fef27829
Author: John Garry <[email protected]>

scsi-multipath: provide callbacks for path state

This commit provides callbacks for .is_disabled, .is_optimized, and .available_path. These callbacks use the scsi_device's sdev_state and access_state, which will be driven by ALUA.

Quoted 3 lines
drivers/scsi/scsi_multipath.c +0 -0

[ ... ]

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
+{

[ ... ]

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 340

Quoted 6 lines
drivers/scsi/scsi_multipath.c +6 -0
+	if (access_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    access_state == SCSI_ACCESS_STATE_ACTIVE)
+		return false;
+
+	return true;
+}

[Severity: High]
Will this return true for STANDBY paths?

Quoted 3 lines

If implicit ALUA relies on sending I/O to a STANDBY path to transition it to ACTIVE via a Unit Attention, marking it disabled might prevent the core from routing any I/O to it, potentially deadlocking the failover.

ALUA support to be improved

Quoted 3 lines

[Severity: High]
Does this also need to allow SCSI_ACCESS_STATE_LBA?

As above

Quoted 20 lines

SPC-4 defines LBA-dependent states as active and capable of processing I/O commands. Should this state also be permitted here to avoid stalling I/O on storage arrays that rely on it?

Quoted 2 lines
drivers/scsi/scsi_multipath.c +2 -0
+static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
+{

[ ... ]

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 368

Quoted 6 lines
drivers/scsi/scsi_multipath.c +6 -0
+	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_QUIESCE ||
+	    sdev_state == SDEV_BLOCK || sdev_state == SDEV_CREATED_BLOCK)
+		return true;
+
+	return false;
+}

[Severity: High]
Should SDEV_CREATED be included here?

Quoted 4 lines

During SCSI device probing, the device state is SDEV_CREATED and the core SCSI layer permits I/O operations like INQUIRY. If SDEV_CREATED is omitted here, could valid I/O be incorrectly failed as unavailable during device scanning?

Not relevant to multipath

Quoted 7 lines

[Severity: High]
By allowing SDEV_QUIESCE here, bios will be placed on the requeue list.

When the device is later unquiesced during a system resume, scsi_device_resume() sets the state back to SDEV_RUNNING, but it does not seem to trigger scsi_mpath_revalidate_paths() or schedule requeue_work.

No bios should be queued when quiesed

Quoted 8 lines

Will bios queued during suspend remain stuck on the list indefinitely?

[Severity: High]
This function only checks sdev_state.

If an ALUA path transitions to SCSI_ACCESS_STATE_OFFLINE, sdev_state remains SDEV_RUNNING, which causes this callback to return true.

ALUA support incomplete

Quoted 17 lines

Could this cause the block layer to endlessly requeue the I/O, believing the path is temporarily unavailable, rather than failing it immediately for being permanently offline?

drivers/scsi/scsi_multipath.c static struct bio *scsi_mpath_clone_bio(struct bio *bio) · line 375

Quoted 6 lines
  static struct mpath_head_template smpdt = {
drivers/scsi/scsi_multipath.c +3 -0
+	.is_disabled = scsi_mpath_is_disabled,
+	.is_optimized = scsi_mpath_is_optimized,
+	.available_path = scsi_mpath_available_path,
  	.clone_bio = scsi_mpath_clone_bio,
  };

[Severity: High]
How does this integration handle ALUA state changes for requeued I/O?

When no active paths are found, bios are placed on the requeue list:

Quoted 8 lines

lib/multipath.c:mpath_bdev_submit_bio()

     spin_lock_irq(&mpath_head->requeue_lock);
     bio_list_add(&mpath_head->requeue_list, bio);
     spin_unlock_irq(&mpath_head->requeue_lock);

When scsi_dh_alua.c handles an ALUA state change, does it notify scsi-multipath to schedule the requeue work? If not, could these bios remain permanently stalled?

ALUA support incomplete

Quoted 5 lines

[Severity: High]
Are the routing algorithms in lib/multipath.c actually using this new .is_optimized callback?

Quoted 7 lines

lib/multipath.c:__mpath_find_path()

     switch(mpath_device->access_state) {
     case MPATH_STATE_OPTIMIZED:

It appears the path finder checks the statically initialized access_state field instead of invoking the new callback. Will this cause all active paths to be incorrectly treated as optimized, bypassing path prioritization?

used elsewhere

author
#46 lore raw

PATCH v6 20/27 scsi-multipath: add scsi_mpath_{start,end}_request()

In reply to John Garry

From: John Garry <[email protected]>

Add scsi_mpath_{start,end}_request() to handle updating private multipath request data, like nvme_mpath_{start,end}_request().

New member Scsi_Host.mpath_nr_active is added. This is required for queue depth multipath iopolicy.

For NVMe, this count is per controller. The reason is that many NSes may be connected to a controller, so congestion should be judged at controller level.

SCSI has no definition of a controller, but SCSI host is a comparable concept.

Indeed, many SCSI disks may be connected to the same SCSI host, so it makes sense to count number of active requests at this point. However, for a transport like iSCSI Initiator over TCP/IP, we have a separate SCSI host per SCSI device (so there the count would be same at SCSI device level).

---
 drivers/scsi/scsi_lib.c       |  8 +++-
 drivers/scsi/scsi_multipath.c | 69 +++++++++++++++++++++++++++++++++++
 include/scsi/scsi_cmnd.h      |  9 ++++-
 include/scsi/scsi_host.h      |  4 ++
 include/scsi/scsi_multipath.h | 11 ++++++
 5 files changed, 99 insertions(+), 2 deletions(-)
drivers/scsi/scsi_lib.c +7 -1
index 41d201b7e7def..baafca4eea866 100644
@@ -669,7 +669,10 @@ static bool scsi_end_request(struct request *req, blk_status_t error,
 	struct scsi_device *sdev = cmd->device;
 	struct request_queue *q = sdev->request_queue;
 
-	if (blk_update_request(req, error, bytes))
+	if (sdev->scsi_mpath_dev && is_mpath_request(req)) {
+		if (scsi_mpath_end_request(req, error, bytes))
+			return true;
+	} else if (blk_update_request(req, error, bytes))
 		return true;
 
 	if (q->limits.features & BLK_FEAT_ADD_RANDOM)
@@ -1917,6 +1920,9 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
 	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
 	cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
 
+	if (sdev->scsi_mpath_dev && is_mpath_request(req))
+		scsi_mpath_start_request(req);
+
 	blk_mq_start_request(req);
 	if (blk_mq_is_reserved_rq(req)) {
 		reason = shost->hostt->queue_reserved_command(shost, cmd);
drivers/scsi/scsi_multipath.c +69 -0

75 lines not on this page · raw message

include/scsi/scsi_cmnd.h +8 -1
index 8ecfb94049db5..52227e2cd9749 100644
@@ -58,8 +58,11 @@ struct scsi_pointer {
  */
 #define SCMD_FORCE_EH_SUCCESS	(1 << 3)
 #define SCMD_FAIL_IF_RECOVERING	(1 << 4)
+#define SCMD_MPATH_IO_STATS	(1 << 5)
+#define SCMD_MPATH_CNT_ACTIVE	(1 << 6)
 /* flags preserved across unprep / reprep */
-#define SCMD_PRESERVED_FLAGS	(SCMD_INITIALIZED | SCMD_FAIL_IF_RECOVERING)
+#define SCMD_PRESERVED_FLAGS	(SCMD_INITIALIZED | SCMD_FAIL_IF_RECOVERING | \
+				SCMD_MPATH_IO_STATS | SCMD_MPATH_CNT_ACTIVE)
 
 /* for scmd->state */
 #define SCMD_STATE_COMPLETE	0
@@ -139,6 +142,10 @@ struct scsi_cmnd {
 					 * to release this memory.  (The memory
 					 * obtained by scsi_malloc is guaranteed
 					 * to be at an address < 16Mb). */
+	#ifdef CONFIG_SCSI_MULTIPATH
+	unsigned long		start_time;
+	unsigned int		start_bytes;
+	#endif
 
 	int result;		/* Status code from lower level driver */
 };
include/scsi/scsi_host.h +4 -0
index 7e2011830ba4b..cf4fff3ea1a8e 100644
@@ -753,6 +753,10 @@ struct Scsi_Host {
 	/* Delay for runtime autosuspend */
 	int rpm_autosuspend_delay;
 
+	#ifdef CONFIG_SCSI_MULTIPATH
+	atomic_t mpath_nr_active;
+	#endif
+
 	/*
 	 * We should ensure that this is aligned, both for better performance
 	 * and also because some compilers (m68k) don't automatically force
include/scsi/scsi_multipath.h +11 -0
index cc77e63c43da3..c3398e9f03f16 100644
@@ -56,6 +56,9 @@ int scsi_mpath_get_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev);
 void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev);
+void scsi_mpath_start_request(struct request *req);
+bool scsi_mpath_end_request(struct request *req, blk_status_t error,
+			       unsigned int nr_bytes);
 #else /* CONFIG_SCSI_MULTIPATH */
 
 struct scsi_mpath_head {
@@ -98,6 +101,14 @@ static inline
 void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
 {
 }
+static inline void scsi_mpath_start_request(struct request *req)
+{
+}
+static inline bool scsi_mpath_end_request(struct request *req, blk_status_t error,
+			       unsigned int nr_bytes)
+{
+	return false;
+}
 static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
 {
 }
Signature · 1 line
2.43.7
#47 lore raw

Re: [PATCH v6 20/27] scsi-multipath: add scsi_mpath_{start,end}_request()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Permanent leak of mpath_nr_active counter and master block device in-flight IO stats when a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Permanent leak of mpath_nr_active counter and master block device in-flight IO stats when a requeued multipath request fails with a hard error. --

--- Patch [20]: [PATCH v6 20/27] scsi-multipath: add scsi_mpath_{start,end}_request() --- commit 1a27bdf4453d197f11533d04e7997e7873652eb4
Author: John Garry <[email protected]>

scsi-multipath: add scsi_mpath_{start,end}_request()

Add scsi_mpath_{start,end}_request() to handle updating private multipath request data, like nvme_mpath_{start,end}_request().

New member Scsi_Host.mpath_nr_active is added. This is required for queue depth multipath iopolicy.

Quoted 4 lines
drivers/scsi/scsi_lib.c +0 -0
index 6a2d7dbfbe8c3..ce9a2c7fbe707 100644

[ ... ]

drivers/scsi/scsi_lib.c static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1928

Quoted 10 lines
drivers/scsi/scsi_lib.c +3 -0
@@ -1917,6 +1920,9 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
 	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
 	cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
 
+	if (sdev->scsi_mpath_dev && is_mpath_request(req))
+		scsi_mpath_start_request(req);
+
 	blk_mq_start_request(req);
 	if (blk_mq_is_reserved_rq(req)) {
 		reason = shost->hostt->queue_reserved_command(shost, cmd);

[Severity: High]
Can this permanently leak the mpath_nr_active counter and master block device in-flight IO stats when a requeued multipath request fails with a hard error?

When a multipath request is dispatched, scsi_mpath_start_request() increments mpath_nr_active and sets SCMD_MPATH_CNT_ACTIVE. If the dispatch then fails with BLK_STS_RESOURCE (e.g. due to a full queue), the request is requeued and the multipath flags are preserved by SCMD_PRESERVED_FLAGS.

On a subsequent retry, if the device has transitioned to an offline state (which is common during path failure), the early state check in scsi_queue_rq() returns an error before scsi_mpath_start_request() is reached:

	if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
		ret = scsi_device_state_check(sdev, req);
		if (ret != BLK_STS_OK)
			goto out_put_budget;
	}

Returning an error here causes the block layer to directly complete the request, bypassing the driver's normal scsi_end_request() completion path. The block layer will eventually call the scsi_cleanup_rq() hook, which merely zeroes cmd->flags:

	cmd->flags = 0;

but fails to decrement mpath_nr_active or call bdev_end_io_acct().

Would this leave the queue-depth multipath I/O policy perceiving an artificially inflated load on the controller, permanently degrading load balancing?

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

Re: [PATCH v6 20/27] scsi-multipath: add scsi_mpath_{start,end}_request()

Quoted 57 lines · On 29/07/2026 17:12, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Permanent leak of mpath_nr_active counter and master block device in-flight IO stats when a requeued multipath request fails with a hard error. --

--- Patch [20]: [PATCH v6 20/27] scsi-multipath: add scsi_mpath_{start,end}_request() --- commit 1a27bdf4453d197f11533d04e7997e7873652eb4
Author: John Garry <[email protected]>

scsi-multipath: add scsi_mpath_{start,end}_request()

Add scsi_mpath_{start,end}_request() to handle updating private multipath request data, like nvme_mpath_{start,end}_request().

New member Scsi_Host.mpath_nr_active is added. This is required for queue depth multipath iopolicy.

Quoted 4 lines
drivers/scsi/scsi_lib.c +0 -0
index 6a2d7dbfbe8c3..ce9a2c7fbe707 100644

[ ... ]

drivers/scsi/scsi_lib.c static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1928

Quoted 10 lines
drivers/scsi/scsi_lib.c +3 -0
@@ -1917,6 +1920,9 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  	cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
 
+	if (sdev->scsi_mpath_dev && is_mpath_request(req))
+		scsi_mpath_start_request(req);
+
  	blk_mq_start_request(req);
  	if (blk_mq_is_reserved_rq(req)) {
  		reason = shost->hostt->queue_reserved_command(shost, cmd);

[Severity: High]
Can this permanently leak the mpath_nr_active counter and master block device in-flight IO stats when a requeued multipath request fails with a hard error?

When a multipath request is dispatched, scsi_mpath_start_request() increments mpath_nr_active and sets SCMD_MPATH_CNT_ACTIVE. If the dispatch then fails with BLK_STS_RESOURCE (e.g. due to a full queue), the request is requeued and the multipath flags are preserved by SCMD_PRESERVED_FLAGS.

On a subsequent retry, if the device has transitioned to an offline state (which is common during path failure), the early state check in scsi_queue_rq() returns an error before scsi_mpath_start_request() is reached:

	if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
		ret = scsi_device_state_check(sdev, req);
		if (ret != BLK_STS_OK)
			goto out_put_budget;
	}

Returning an error here causes the block layer to directly complete the request, bypassing the driver's normal scsi_end_request() completion path. The block layer will eventually call the scsi_cleanup_rq() hook, which merely zeroes cmd->flags:

Quoted 7 lines
	cmd->flags = 0;

but fails to decrement mpath_nr_active or call bdev_end_io_acct().

Would this leave the queue-depth multipath I/O policy perceiving an artificially inflated load on the controller, permanently degrading load balancing?

Can be improved later

Quoted 1 line
author
#49 lore raw

PATCH v6 21/27 scsi-multipath: add delayed disk removal support

In reply to John Garry

Add support in core code for delayed disk removal support. In this, the callback calls into the scsi_driver to do the necessary removal work.

The scsi_disk driver (sd) must ensure that the scsi_mpath_device does not go away while the delayed removal work is active, i.e. it must keep a reference.

No reference to the scsi_disk multipath structures are kept outside that driver, so that driver needs to provide a scsi_driver.mpath_remove_head callback to do the necessary work.

---
 drivers/scsi/scsi_multipath.c | 27 +++++++++++++++++++++++++++
 include/scsi/scsi_driver.h    |  4 ++++
 2 files changed, 31 insertions(+)
drivers/scsi/scsi_multipath.c +27 -0
index 0adc5adb4b234..a42e20d33e422 100644
@@ -367,7 +367,34 @@ static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
 	return false;
 }
 
+static int scsi_mpath_remove_head_drv(struct device_driver *drv, void *data)
+{
+	struct scsi_mpath_head *scsi_mpath_head = data;
+	struct scsi_driver *scsi_driver = to_scsi_driver(drv);
+
+	if (scsi_driver->mpath_remove_head)
+		scsi_driver->mpath_remove_head(scsi_mpath_head);
+
+	return 0;
+}
+
+static void scsi_mpath_remove_head_work(struct mpath_head *mpath_head)
+{
+	struct scsi_mpath_head *scsi_mpath_head = to_scsi_mpath_head(mpath_head);
+
+	/*
+	 * scsi_mpath_remove_head_drv() could drop the final reference to
+	 * scsi_mpath_head, meaning that we pass a free'd pointer further
+	 * drivers - take a reference so that won't happen.
+	 */
+	scsi_mpath_get_head(scsi_mpath_head);
+	bus_for_each_drv(&scsi_bus_type, NULL, scsi_mpath_head,
+		scsi_mpath_remove_head_drv);
+	scsi_mpath_put_head(scsi_mpath_head);
+}
+
 static struct mpath_head_template smpdt = {
+	.remove_head = scsi_mpath_remove_head_work,
 	.is_disabled = scsi_mpath_is_disabled,
 	.is_optimized = scsi_mpath_is_optimized,
 	.available_path = scsi_mpath_available_path,
include/scsi/scsi_driver.h +4 -0
index 249cea724abd1..d92b63d357f2a 100644
@@ -8,6 +8,7 @@
 
 struct module;
 struct request;
+struct scsi_mpath_head;
 
 struct scsi_driver {
 	struct device_driver	gendrv;
@@ -22,6 +23,9 @@ struct scsi_driver {
 	int (*done)(struct scsi_cmnd *);
 	int (*eh_action)(struct scsi_cmnd *, int);
 	void (*eh_reset)(struct scsi_cmnd *);
+	#ifdef CONFIG_SCSI_MULTIPATH
+	void (*mpath_remove_head)(struct scsi_mpath_head *);
+	#endif
 };
 #define to_scsi_driver(drv) \
 	container_of((drv), struct scsi_driver, gendrv)
Signature · 1 line
2.43.7
#50 lore raw

Re: [PATCH v6 21/27] scsi-multipath: add delayed disk removal support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The unchecked return value of `scsi_mpath_get_head()` in `scsi_mpath_remove_head_work()` can lead…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The unchecked return value of scsi_mpath_get_head() in scsi_mpath_remove_head_work() can lead to a Use-After-Free (UAF) and double-free. --

--- Patch [21]: [PATCH v6 21/27] scsi-multipath: add delayed disk removal support --- commit 10f5e133107a91a4b4df48e21559fe51a6b82a5e
Author: John Garry <[email protected]>

scsi-multipath: add delayed disk removal support

This commit adds core code support for delayed disk removal, introducing a worker function that iterates over the scsi_bus_type to invoke a newly added mpath_remove_head callback. It also attempts to manage reference counting for scsi_mpath_head during this removal process.

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
index 0adc5adb4b234..a42e20d33e422 100644

[ ... ]

Quoted 5 lines
drivers/scsi/scsi_multipath.c +4 -0
@@ -367,7 +367,34 @@ static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
+static void scsi_mpath_remove_head_work(struct mpath_head *mpath_head)
+{
+	struct scsi_mpath_head *scsi_mpath_head = to_scsi_mpath_head(mpath_head);
+

drivers/scsi/scsi_multipath.c static bool scsi_mpath_available_path(struct mpath_device *mpath_device) · line 380

Quoted 10 lines
drivers/scsi/scsi_multipath.c +10 -0
+	/*
+	 * scsi_mpath_remove_head_drv() could drop the final reference to
+	 * scsi_mpath_head, meaning that we pass a free'd pointer further
+	 * drivers - take a reference so that won't happen.
+	 */
+	scsi_mpath_get_head(scsi_mpath_head);
+	bus_for_each_drv(&scsi_bus_type, NULL, scsi_mpath_head,
+		scsi_mpath_remove_head_drv);
+	scsi_mpath_put_head(scsi_mpath_head);
+}

[Severity: High]
Can ignoring the return value of scsi_mpath_get_head() cause a use-after-free regression?

Looking at scsi_mpath_get_head(), it uses kref_get_unless_zero() which can fail and return -ENXIO. If the reference count drops to zero concurrently just before scsi_mpath_get_head() is called, the acquisition will fail.

By proceeding unconditionally when it fails, bus_for_each_drv() could operate on an already freed scsi_mpath_head pointer.

Furthermore, scsi_mpath_put_head() would then invoke kref_put() on an object that had a zero reference count, causing a double-free regression.

Should this code check the return value of scsi_mpath_get_head() and exit early if the reference could not be acquired?

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

Re: [PATCH v6 21/27] scsi-multipath: add delayed disk removal support

Quoted 31 lines · On 29/07/2026 17:26, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The unchecked return value of scsi_mpath_get_head() in scsi_mpath_remove_head_work() can lead to a Use-After-Free (UAF) and double-free. --

--- Patch [21]: [PATCH v6 21/27] scsi-multipath: add delayed disk removal support --- commit 10f5e133107a91a4b4df48e21559fe51a6b82a5e
Author: John Garry <[email protected]>

scsi-multipath: add delayed disk removal support

This commit adds core code support for delayed disk removal, introducing a worker function that iterates over the scsi_bus_type to invoke a newly added mpath_remove_head callback. It also attempts to manage reference counting for scsi_mpath_head during this removal process.

Quoted 4 lines
drivers/scsi/scsi_multipath.c +0 -0
index 0adc5adb4b234..a42e20d33e422 100644

[ ... ]

Quoted 11 lines
drivers/scsi/scsi_multipath.c +10 -0
@@ -367,7 +367,34 @@ static bool scsi_mpath_available_path(struct mpath_device *mpath_device)
+static void scsi_mpath_remove_head_work(struct mpath_head *mpath_head)
+{
+	struct scsi_mpath_head *scsi_mpath_head = to_scsi_mpath_head(mpath_head);
+
+	/*
+	 * scsi_mpath_remove_head_drv() could drop the final reference to
+	 * scsi_mpath_head, meaning that we pass a free'd pointer further
+	 * drivers - take a reference so that won't happen.
+	 */
+	scsi_mpath_get_head(scsi_mpath_head);
Quoted 8 lines

drivers/scsi/scsi_multipath.c static bool scsi_mpath_available_path(struct mpath_device *mpath_device) · line 394

Quoted 4 lines
drivers/scsi/scsi_multipath.c +4 -0
+	bus_for_each_drv(&scsi_bus_type, NULL, scsi_mpath_head,
+		scsi_mpath_remove_head_drv);
+	scsi_mpath_put_head(scsi_mpath_head);
+}

[Severity: High]
Can ignoring the return value of scsi_mpath_get_head() cause a use-after-free regression?

It should not fail as we already must have a reference

Quoted 14 lines

Looking at scsi_mpath_get_head(), it uses kref_get_unless_zero() which can fail and return -ENXIO. If the reference count drops to zero concurrently just before scsi_mpath_get_head() is called, the acquisition will fail.

By proceeding unconditionally when it fails, bus_for_each_drv() could operate on an already freed scsi_mpath_head pointer.

Furthermore, scsi_mpath_put_head() would then invoke kref_put() on an object that had a zero reference count, causing a double-free regression.

Should this code check the return value of scsi_mpath_get_head() and exit early if the reference could not be acquired?

author
#52 lore raw

PATCH v6 22/27 scsi: sd: add multipath disk class

In reply to John Garry

Add a new class, sd_mpath_disk_class, which is the multipath version of the scsi_disk class.

Structure sd_mpath_disk is introduced to manage the multipath gendisk.

---
 drivers/scsi/sd.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/sd.h |  3 +++
 2 files changed, 45 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +42 -1
index 599e75f333343..242a15bc2c5bb 100644
@@ -70,6 +70,7 @@
 #include <scsi/scsi_ioctl.h>
 #include <scsi/scsicam.h>
 #include <scsi/scsi_common.h>
+#include <scsi/scsi_multipath.h>
 
 #include "sd.h"
 #include "scsi_priv.h"
@@ -113,6 +114,39 @@ static mempool_t *sd_page_pool;
 static mempool_t *sd_large_page_pool;
 static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
+#ifdef CONFIG_SCSI_MULTIPATH
+struct sd_mpath_disk {
+	struct scsi_mpath_head		*scsi_mpath_head;
+};
+
+static void sd_mpath_disk_release(struct device *dev)
+{
+}
+
+static const struct class sd_mpath_disk_class = {
+	.name = "scsi_mpath_disk",
+	.dev_release = sd_mpath_disk_release,
+};
+
+static int sd_mpath_class_register(void)
+{
+	return class_register(&sd_mpath_disk_class);
+}
+
+static void sd_mpath_class_unregister(void)
+{
+	class_unregister(&sd_mpath_disk_class);
+}
+#else /* CONFIG_SCSI_MULTIPATH */
+static int sd_mpath_class_register(void)
+{
+	return 0;
+}
+
+static void sd_mpath_class_unregister(void)
+{
+}
+#endif
 
 static const char *sd_cache_types[] = {
 	"write through", "none", "write back",
@@ -4453,11 +4487,15 @@ static int __init init_sd(void)
 	if (err)
 		goto err_out;
 
+	err = sd_mpath_class_register();
+	if (err)
+		goto err_out_class;
+
 	sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0);
 	if (!sd_page_pool) {
 		printk(KERN_ERR "sd: can't init discard page pool\n");
 		err = -ENOMEM;
-		goto err_out_class;
+		goto err_out_mpath_class;
 	}
 
 	err = scsi_register_driver(&sd_template);
@@ -4468,6 +4506,8 @@ static int __init init_sd(void)
 
 err_out_driver:
 	mempool_destroy(sd_page_pool);
+err_out_mpath_class:
+	sd_mpath_class_unregister();
 err_out_class:
 	class_unregister(&sd_disk_class);
 err_out:
@@ -4493,6 +4533,7 @@ static void __exit exit_sd(void)
 		mempool_destroy(sd_large_page_pool);
 
 	class_unregister(&sd_disk_class);
+	sd_mpath_class_unregister();
 
 	for (i = 0; i < SD_MAJORS; i++)
 		unregister_blkdev(sd_major(i), "sd");
drivers/scsi/sd.h +3 -0
index 574af82430169..22438173ea2f8 100644
@@ -83,6 +83,9 @@ struct zoned_disk_info {
 
 struct scsi_disk {
 	struct scsi_device *device;
+#ifdef CONFIG_SCSI_MULTIPATH
+	struct sd_mpath_disk *sd_mpath_disk;
+#endif
 
 	/*
 	 * disk_dev is used to show attributes in /sys/class/scsi_disk/,
Signature · 1 line
2.43.7
#53 lore raw

Re: [PATCH v6 22/27] scsi: sd: add multipath disk class

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Empty `dev_release` callback `sd_mpath_disk_release` bypasses device/kobject memory management.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Empty dev_release callback sd_mpath_disk_release bypasses device/kobject memory management.
- [Medium] Struct sd_mpath_disk is incomplete and fails to manage the multipath gendisk as claimed. --

--- Patch [22]: [PATCH v6 22/27] scsi: sd: add multipath disk class --- commit f604e0ae8b45c7be3d202656fafcb45e6c81e7cf
Author: John Garry <[email protected]>

scsi: sd: add multipath disk class

Add a new class, sd_mpath_disk_class, which is the multipath version of the scsi_disk class. Structure sd_mpath_disk is introduced to manage the multipath gendisk.

Quoted 4 lines
drivers/scsi/sd.c +0 -0
index 599e75f333343..242a15bc2c5bb 100644

[ ... ]

drivers/scsi/sd.c static mempool_t *sd_page_pool; · line 120

Quoted 8 lines
drivers/scsi/sd.c +4 -0
@@ -113,6 +114,39 @@ static mempool_t *sd_page_pool;
 static mempool_t *sd_large_page_pool;
 static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
+#ifdef CONFIG_SCSI_MULTIPATH
+struct sd_mpath_disk {
+	struct scsi_mpath_head		*scsi_mpath_head;
+};

[Severity: Medium]
The commit message states that sd_mpath_disk is introduced to manage the multipath gendisk. However, does this struct lack a struct gendisk pointer and an embedded struct device? Without these, it doesn't appear capable of fulfilling its documented purpose of managing the multipath disk.

drivers/scsi/sd.c static mempool_t *sd_page_pool; · line 124

Quoted 4 lines
drivers/scsi/sd.c +4 -0
+
+static void sd_mpath_disk_release(struct device *dev)
+{
+}

[Severity: High]
Is it safe to provide an empty dev_release callback here?

When a device is assigned to sd_mpath_disk_class and its reference count reaches zero, this callback is invoked. Providing an empty release callback circumvents proper resource cleanup, silencing kobject warnings while guaranteeing that the enclosing structure is never actually freed.

Could this cause a memory leak of the device structure during teardown?

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+
+static const struct class sd_mpath_disk_class = {
+	.name = "scsi_mpath_disk",
+	.dev_release = sd_mpath_disk_release,
+};
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
author
#54 lore raw

Re: [PATCH v6 22/27] scsi: sd: add multipath disk class

Quoted 20 lines · On 29/07/2026 17:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Empty dev_release callback sd_mpath_disk_release bypasses device/kobject memory management.
- [Medium] Struct sd_mpath_disk is incomplete and fails to manage the multipath gendisk as claimed. --

--- Patch [22]: [PATCH v6 22/27] scsi: sd: add multipath disk class --- commit f604e0ae8b45c7be3d202656fafcb45e6c81e7cf
Author: John Garry <[email protected]>

scsi: sd: add multipath disk class

Add a new class, sd_mpath_disk_class, which is the multipath version of the scsi_disk class. Structure sd_mpath_disk is introduced to manage the multipath gendisk.

Quoted 4 lines
drivers/scsi/sd.c +0 -0
index 599e75f333343..242a15bc2c5bb 100644

[ ... ]

Quoted 14 lines

drivers/scsi/sd.c static mempool_t *sd_page_pool; · line 120

Quoted 8 lines
drivers/scsi/sd.c +4 -0
@@ -113,6 +114,39 @@ static mempool_t *sd_page_pool;
  static mempool_t *sd_large_page_pool;
  static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
  static struct lock_class_key sd_bio_compl_lkclass;
+#ifdef CONFIG_SCSI_MULTIPATH
+struct sd_mpath_disk {
+	struct scsi_mpath_head		*scsi_mpath_head;
+};

[Severity: Medium]
The commit message states that sd_mpath_disk is introduced to manage the multipath gendisk. However, does this struct lack a struct gendisk pointer and an embedded struct device? Without these, it doesn't appear capable of fulfilling its documented purpose of managing the multipath disk.

This is added later

Quoted 8 lines

drivers/scsi/sd.c static mempool_t *sd_page_pool; · line 124

Quoted 5 lines
drivers/scsi/sd.c +4 -0
+
+static void sd_mpath_disk_release(struct device *dev)
+{
+}

[Severity: High]
Is it safe to provide an empty dev_release callback here?

This is added later

Quoted 14 lines

When a device is assigned to sd_mpath_disk_class and its reference count reaches zero, this callback is invoked. Providing an empty release callback circumvents proper resource cleanup, silencing kobject warnings while guaranteeing that the enclosing structure is never actually freed.

Could this cause a memory leak of the device structure during teardown?

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+
+static const struct class sd_mpath_disk_class = {
+	.name = "scsi_mpath_disk",
+	.dev_release = sd_mpath_disk_release,
+};
author
#55 lore raw

PATCH v6 23/27 scsi: sd: add multipath disk attr groups

In reply to John Garry

From: John Garry <[email protected]>

Set multipath disk attr groups, which includes delayed disk removal and everything from mpath_attr_group.

---
 drivers/scsi/sd.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/scsi/sd.c +36 -0
index 242a15bc2c5bb..36c745cbb2619 100644
@@ -4006,6 +4006,42 @@ static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
 	return 0;
 }
 
+static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);
+
+	return mpath_delayed_removal_secs_store(mpath_head, buf, count);
+}
+
+static ssize_t sd_mpath_device_delayed_removal_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);
+
+	return mpath_delayed_removal_secs_show(mpath_head, buf);
+}
+
+static DEVICE_ATTR(delayed_removal_secs, S_IRUGO | S_IWUSR,
+		sd_mpath_device_delayed_removal_show,
+		sd_mpath_device_delayed_removal_store);
+
+static struct attribute *sd_mpath_disk_attrs[] = {
+	&dev_attr_delayed_removal_secs.attr,
+	NULL
+};
+
+static const struct attribute_group sd_mpath_disk_attr_group = {
+	.attrs		= sd_mpath_disk_attrs,
+};
+
+__maybe_unused
+static const struct attribute_group *sd_mpath_disk_attr_groups[] = {
+	&sd_mpath_disk_attr_group,
+	&mpath_attr_group,
+	NULL
+};
+
 /**
  *	sd_probe - called during driver initialization and whenever a
  *	new scsi device is attached to the system. It is called once
Signature · 1 line
2.43.7
#56 lore raw

Re: [PATCH v6 23/27] scsi: sd: add multipath disk attr groups

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Result: Dead Code. The newly defined sysfs attribute groups for multipath disks are never…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Result: Dead Code. The newly defined sysfs attribute groups for multipath disks are never assigned or registered, rendering the patch's functionality completely dead.
- [Medium] Result: Latent Type Confusion. The sd_mpath_disk_attr_group lacks an is_visible callback, exposing it to a fatal type-confusion or NULL dereference crash if mistakenly wired up to standard SCSI disks.
- [Medium] Result: Speculative Use-After-Free. Uncanceled delayed work caused by the delayed_removal_secs sysfs configuration risks a UAF on mpath_head teardown.
--

--- Patch [23]: [PATCH v6 23/27] scsi: sd: add multipath disk attr groups --- commit 681fea1e5bfb430f8ec567b5c630bf2528c332b3
Author: John Garry <[email protected]>

scsi: sd: add multipath disk attr groups

Set multipath disk attr groups, which includes delayed disk removal and everything from mpath_attr_group.

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4012

Quoted 12 lines
drivers/scsi/sd.c +4 -0
index 242a15bc2c5bb..36c745cbb2619 100644
@@ -4006,6 +4006,42 @@ static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
 	return 0;
 }
 
+static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);

[Severity: Medium]
Does the teardown path in mpath_head_uninit() contain an explicit cancel_delayed_work_sync(&mpath_head->remove_work)? If a delayed work is scheduled via this sysfs store function, could it race with mpath_head teardown and trigger a use-after-free if the work isn't canceled?

Quoted 12 lines
drivers/scsi/sd.c +12 -0
+
+	return mpath_delayed_removal_secs_store(mpath_head, buf, count);
+}
+
+static ssize_t sd_mpath_device_delayed_removal_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);
+
+	return mpath_delayed_removal_secs_show(mpath_head, buf);
+}
+

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4034

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+static DEVICE_ATTR(delayed_removal_secs, S_IRUGO | S_IWUSR,
+		sd_mpath_device_delayed_removal_show,
+		sd_mpath_device_delayed_removal_store);
+
+static struct attribute *sd_mpath_disk_attrs[] = {
+	&dev_attr_delayed_removal_secs.attr,
+	NULL
+};
+
+static const struct attribute_group sd_mpath_disk_attr_group = {

[Severity: Medium]
Should this group include an is_visible() callback? Without one, if this attribute group is ever mistakenly attached to generic, non-multipath SCSI disks, accessing the attribute would trigger mpath_bd_device_to_head() on an invalid device pointer, potentially causing a type confusion or NULL dereference crash.

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4039

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+	.attrs		= sd_mpath_disk_attrs,
+};
+
+__maybe_unused
+static const struct attribute_group *sd_mpath_disk_attr_groups[] = {

[Severity: Medium]
Are these attribute groups actually registered anywhere? The array is defined as __maybe_unused, but it doesn't appear to be hooked up to any device class (e.g., sd_mpath_disk_class.dev_groups) or passed to device_add_disk() in this patch. Will these attributes ever be visible to userspace?

Quoted 4 lines
drivers/scsi/sd.c +4 -0
+	&sd_mpath_disk_attr_group,
+	&mpath_attr_group,
+	NULL
+};
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
author
#57 lore raw

Re: [PATCH v6 23/27] scsi: sd: add multipath disk attr groups

Quoted 23 lines · On 29/07/2026 17:48, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Result: Dead Code. The newly defined sysfs attribute groups for multipath disks are never assigned or registered, rendering the patch's functionality completely dead.
- [Medium] Result: Latent Type Confusion. The sd_mpath_disk_attr_group lacks an is_visible callback, exposing it to a fatal type-confusion or NULL dereference crash if mistakenly wired up to standard SCSI disks.
- [Medium] Result: Speculative Use-After-Free. Uncanceled delayed work caused by the delayed_removal_secs sysfs configuration risks a UAF on mpath_head teardown.
--

--- Patch [23]: [PATCH v6 23/27] scsi: sd: add multipath disk attr groups --- commit 681fea1e5bfb430f8ec567b5c630bf2528c332b3
Author: John Garry <[email protected]>

scsi: sd: add multipath disk attr groups

Set multipath disk attr groups, which includes delayed disk removal and everything from mpath_attr_group.

Quoted 8 lines
drivers/scsi/sd.c +0 -0
index 242a15bc2c5bb..36c745cbb2619 100644
@@ -4006,6 +4006,42 @@ static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  	return 0;
  }
 
Quoted 10 lines

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4012

Quoted 4 lines
drivers/scsi/sd.c +4 -0
+static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);

[Severity: Medium]
Does the teardown path in mpath_head_uninit() contain an explicit cancel_delayed_work_sync(&mpath_head->remove_work)? If a delayed work is scheduled via this sysfs store function, could it race with mpath_head teardown and trigger a use-after-free if the work isn't canceled?

delayed work is not scheduled via this sysfs store function

Quoted 19 lines
Quoted 19 lines
drivers/scsi/sd.c +18 -0
+
+	return mpath_delayed_removal_secs_store(mpath_head, buf, count);
+}
+
+static ssize_t sd_mpath_device_delayed_removal_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mpath_head *mpath_head = mpath_bd_device_to_head(dev);
+
+	return mpath_delayed_removal_secs_show(mpath_head, buf);
+}
+
+static DEVICE_ATTR(delayed_removal_secs, S_IRUGO | S_IWUSR,
+		sd_mpath_device_delayed_removal_show,
+		sd_mpath_device_delayed_removal_store);
+
+static struct attribute *sd_mpath_disk_attrs[] = {
+	&dev_attr_delayed_removal_secs.attr,
Quoted 8 lines

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4034

Quoted 4 lines
drivers/scsi/sd.c +4 -0
+	NULL
+};
+
+static const struct attribute_group sd_mpath_disk_attr_group = {

[Severity: Medium]
Should this group include an is_visible() callback? Without one, if this attribute group is ever mistakenly attached to generic,

it's not used elsewhere, so I won't add a pointless is_visible callback

Quoted 5 lines

non-multipath SCSI
disks, accessing the attribute would trigger mpath_bd_device_to_head() on an invalid device pointer, potentially causing a type confusion or NULL dereference crash.

Quoted 8 lines

drivers/scsi/sd.c static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) · line 4039

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+	.attrs		= sd_mpath_disk_attrs,
+};
+
+__maybe_unused
+static const struct attribute_group *sd_mpath_disk_attr_groups[] = {

[Severity: Medium]
Are these attribute groups actually registered anywhere?

They are used later

Quoted 10 lines

The array is defined
as __maybe_unused, but it doesn't appear to be hooked up to any device class (e.g., sd_mpath_disk_class.dev_groups) or passed to device_add_disk() in this patch. Will these attributes ever be visible to userspace?

Quoted 4 lines
drivers/scsi/sd.c +4 -0
+	&sd_mpath_disk_attr_group,
+	&mpath_attr_group,
+	NULL
+};
author
#58 lore raw

PATCH v6 24/27 scsi: sd: support multipath disk

In reply to John Garry

From: John Garry <[email protected]>

Add support to attach a multipath disk.

We still allocate the gendisk per path, and this is required for the per-path submission. However, those gendisks are marked as hidden. Those disks are named sdX:Y, where X is the multipath disk index and Y is the per-path index.

A global list of sd_mpath_disks is kept for matching scsi_device's.

The multipath gendisk has the name and disk->major/minor set to minic a scsi_disk.

The following is an example of relevant scsi_disk and block sysfs directories:

$ ls -l /sys/block/ | grep sdc
lrwxrwxrwx    1 root     root             0 Feb 24 16:01 sdc -> ../devices/virtual/scsi_mpath_disk/0/sdc
lrwxrwxrwx    1 root     root             0 Feb 24 16:01 sdc:0 -> ../devices/platform/host8/session1/target8:0:0/8:0:0:0/block/sdc:0
lrwxrwxrwx    1 root     root             0 Feb 24 16:02 sdc:1 -> ../devices/platform/host9/session2/target9:0:0/9:0:0:0/block/sdc:1
$ ls -l /sys/class/scsi_mpath_disk/scsi_mpath_disk0/
total 0
drwxr-xr-x    2 root     root             0 Feb 24 16:03 power
drwxr-xr-x   11 root     root             0 Feb 24 16:01 sdc
lrwxrwxrwx    1 root     root             0 Feb 24 16:01 subsystem -> ../../../../class/scsi_mpath_disk
-rw-r--r--    1 root     root          4096 Feb 24 16:01 uevent
$ ls -l /sys/class/scsi_mpath_disk/scsi_mpath_disk0/sdc/multipath/
total 0
lrwxrwxrwx    1 root     root             0 Feb 24 16:20 sdc:0 -> ../../../../../platform/host8/session1/target8:0:0/8:0:0:0/block/sdc:0
lrwxrwxrwx    1 root     root             0 Feb 24 16:20 sdc:1 -> ../../../../../platform/host9/session2/target9:0:0/9:0:0:0/block/sdc:1
$ ls -l /dev/sdc*
brw-rw----    1 root     disk        8,  32 Feb 24 16:01 /dev/sdc
brw-rw----    1 root     disk        8,  33 Feb 24 16:01 /dev/sdc1
brw-rw----    1 root     disk        8,  34 Feb 24 16:01 /dev/sdc2
$ lsblk /dev/sdc
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sdc               8:32   0  600M  0 disk
|-sdc1            8:33   0    9M  0 part
`-sdc2            8:34   0  568M  0 part
---
 drivers/scsi/sd.c | 425 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 405 insertions(+), 20 deletions(-)
drivers/scsi/sd.c +405 -20
index 36c745cbb2619..f044b2dfaa9fd 100644
@@ -115,12 +115,30 @@ static mempool_t *sd_large_page_pool;
 static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
 #ifdef CONFIG_SCSI_MULTIPATH
+static LIST_HEAD(sd_mpath_disks_list);
+static DEFINE_MUTEX(sd_mpath_disks_lock);
+
 struct sd_mpath_disk {
+	struct device			dev;
+	int				disk_index;
+	int				disk_count;
+	struct list_head		entry;
 	struct scsi_mpath_head		*scsi_mpath_head;
 };
 
 static void sd_mpath_disk_release(struct device *dev)
 {
+	struct sd_mpath_disk *sd_mpath_disk =
+		container_of(dev, struct sd_mpath_disk, dev);
+	struct scsi_mpath_head *scsi_mpath_head =
+		sd_mpath_disk->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+	mpath_put_disk(mpath_head);
+	ida_free(&sd_index_ida, sd_mpath_disk->disk_index);
+	scsi_mpath_put_head(scsi_mpath_head);
+
+	kfree(sd_mpath_disk);
 }
 
 static const struct class sd_mpath_disk_class = {
@@ -817,7 +835,8 @@ static void scsi_disk_release(struct device *dev)
 {
 	struct scsi_disk *sdkp = to_scsi_disk(dev);
 
-	ida_free(&sd_index_ida, sdkp->index);
+	if (sdkp->index >= 0)
+		ida_free(&sd_index_ida, sdkp->index);
 	put_device(&sdkp->device->sdev_gendev);
 	free_opal_dev(sdkp->opal_dev);
 
@@ -4006,6 +4025,90 @@ static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
 	return 0;
 }
 
+#ifdef CONFIG_SCSI_MULTIPATH
+static int sd_mpath_revalidate_head(struct scsi_disk *sdkp)
+{
+	struct sd_mpath_disk *sd_mpath_disk = sdkp->sd_mpath_disk;
+	struct scsi_mpath_head *scsi_mpath_head = sd_mpath_disk->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+	struct gendisk *disk = mpath_head->disk;
+	struct queue_limits *sdkp_lim = &sdkp->disk->queue->limits;
+	struct queue_limits lim;
+	unsigned int memflags;
+	int ret;
+
+	lim = queue_limits_start_update(disk->queue);
+	memflags = blk_mq_freeze_queue(disk->queue);
+
+	lim.logical_block_size = sdkp_lim->logical_block_size;
+	lim.physical_block_size = sdkp_lim->physical_block_size;
+	lim.io_min = sdkp_lim->io_min;
+	lim.io_opt = sdkp_lim->io_opt;
+
+	queue_limits_stack_bdev(&lim, sdkp->disk->part0, 0,
+					disk->disk_name);
+
+	/* TODO: setup integrity and zoned limits */
+	lim.max_write_streams = sdkp_lim->max_write_streams;
+	lim.write_stream_granularity = sdkp_lim->write_stream_granularity;
+	ret = queue_limits_commit_update(disk->queue, &lim);
+
+	set_capacity_and_notify(disk, get_capacity(sdkp->disk));
+
+	mpath_revalidate_paths(mpath_head);
+
+	blk_mq_unfreeze_queue(disk->queue, memflags);
+
+	return ret;
+}
+
+static int sd_mpath_get_disk(struct sd_mpath_disk *sd_mpath_disk)
+{
+	if (!get_device(&sd_mpath_disk->dev))
+		return -ENXIO;
+	return 0;
+}
+
+static void sd_mpath_put_disk(struct sd_mpath_disk *sd_mpath_disk)
+{
+	put_device(&sd_mpath_disk->dev);
+}
+
+static struct sd_mpath_disk *sd_mpath_find_disk(
+			struct scsi_mpath_head *scsi_mpath_head)
+{
+	struct sd_mpath_disk *sd_mpath_disk;
+	int ret;
+
+	list_for_each_entry(sd_mpath_disk, &sd_mpath_disks_list, entry) {
+		ret = sd_mpath_get_disk(sd_mpath_disk);
+		if (ret)
+			continue;
+
+		if (sd_mpath_disk->scsi_mpath_head == scsi_mpath_head)
+			return sd_mpath_disk;
+
+		sd_mpath_put_disk(sd_mpath_disk);
+	}
+
+	return NULL;
+}
+
+static int sd_mpath_add_disk(struct scsi_disk *sdkp)
+{
+	struct scsi_device *sdp = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdp->scsi_mpath_dev;
+	struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+	int ret;
+
+	ret = mpath_add_device(mpath_device, sdkp->disk,
+		dev_to_node(sdp->host->dma_dev), &sdp->host->mpath_nr_active);
+	if (ret)
+		return ret;
+	mpath_device_set_live(mpath_device);
+	return 0;
+}
+
 static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -4035,13 +4138,259 @@ static const struct attribute_group sd_mpath_disk_attr_group = {
 	.attrs		= sd_mpath_disk_attrs,
 };
 
-__maybe_unused
 static const struct attribute_group *sd_mpath_disk_attr_groups[] = {
 	&sd_mpath_disk_attr_group,
 	&mpath_attr_group,
 	NULL
 };
 
+static int sd_mpath_probe(struct scsi_disk *sdkp)
+{
+	struct scsi_device *sdp = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdp->scsi_mpath_dev;
+	struct device *dma_dev = sdp->host->dma_dev;
+	struct scsi_mpath_head *scsi_mpath_head =
+				scsi_mpath_dev->scsi_mpath_head;
+	struct sd_mpath_disk *sd_mpath_disk;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+	char disk_name[DISK_NAME_LEN - 2];
+	struct queue_limits lim;
+	struct gendisk *disk;
+	int error;
+
+	/*
+	 * sd_mpath_disks_list is kept locked if no disk found.
+	 * Otherwise an extra reference is taken.
+	 */
+	mutex_lock(&sd_mpath_disks_lock);
+	sd_mpath_disk = sd_mpath_find_disk(scsi_mpath_head);
+	if (sd_mpath_disk) {
+		error = sized_strscpy(disk_name, mpath_head->disk->disk_name,
+				sizeof(disk_name));
+		if (error < 0) {
+			/*
+			 * Should not happen as would fail for the same when
+			 * allocating the sd_mpath_disk
+			 */
+			sd_mpath_put_disk(sd_mpath_disk);
+			mutex_unlock(&sd_mpath_disks_lock);
+			return error;
+		}
+		sd_mpath_disk->disk_count++;
+		mutex_unlock(&sd_mpath_disks_lock);
+
+		goto found;
+	}
+
+	sd_mpath_disk = kzalloc(sizeof(*sd_mpath_disk), GFP_KERNEL);
+	if (!sd_mpath_disk) {
+		error = -ENOMEM;
+		goto out_unlock;
+	}
+
+	sd_mpath_disk->scsi_mpath_head = scsi_mpath_head;
+
+	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;
+
+	mpath_head->parent = &sd_mpath_disk->dev;
+	mpath_head->drv_module = THIS_MODULE;
+	mpath_head->disk_groups = sd_mpath_disk_attr_groups;
+	error = mpath_alloc_head_disk(mpath_head, &lim,
+				dev_to_node(dma_dev));
+	if (error)
+		goto out_free_disk;
+	disk = mpath_head->disk;
+
+	error = ida_alloc(&sd_index_ida, GFP_KERNEL);
+	if (error < 0) {
+		sdev_printk(KERN_WARNING, sdp, "sd_mpath_probe: memory exhausted.\n");
+		goto out_put_disk;
+	}
+	sd_mpath_disk->disk_index = error;
+	error = sd_format_disk_name("sd", sd_mpath_disk->disk_index,
+				disk->disk_name, DISK_NAME_LEN);
+	if (error)
+		goto out_free_index;
+
+	error = sized_strscpy(disk_name, mpath_head->disk->disk_name,
+				sizeof(disk_name));
+	if (error < 0)
+		goto out_free_index;
+
+	device_initialize(&sd_mpath_disk->dev);
+	sd_mpath_disk->dev.class = &sd_mpath_disk_class;
+
+	/* undone in sd_mpath_disk_release() */
+	scsi_mpath_get_head(scsi_mpath_head);
+
+	error = dev_set_name(&sd_mpath_disk->dev, "scsi_mpath_disk%d",
+				scsi_mpath_head->index);
+	if (error) {
+		put_device(&sd_mpath_disk->dev);
+		goto out_unlock;
+	}
+
+	error = device_add(&sd_mpath_disk->dev);
+	if (error) {
+		put_device(&sd_mpath_disk->dev);
+		goto out_unlock;
+	}
+
+	list_add_tail(&sd_mpath_disk->entry, &sd_mpath_disks_list);
+	disk->major = sd_major((sd_mpath_disk->disk_index & 0xf0) >> 4);
+	disk->first_minor = ((sd_mpath_disk->disk_index & 0xf) << 4) |
+				(sd_mpath_disk->disk_index & 0xfff00);
+	disk->minors = SD_MINORS;
+
+	sd_mpath_disk->disk_count = 1;
+	mutex_unlock(&sd_mpath_disks_lock);
+found:
+	sdkp->sd_mpath_disk = sd_mpath_disk;
+	sdkp->disk->flags |= GENHD_FL_HIDDEN;
+	snprintf(sdkp->disk->disk_name, DISK_NAME_LEN, "%s:%d",
+		disk_name, scsi_mpath_dev->index);
+
+	sdkp->index = -1;
+	return 0;
+
+out_free_index:
+	ida_free(&sd_index_ida, sd_mpath_disk->disk_index);
+out_put_disk:
+	mpath_put_disk(mpath_head);
+out_free_disk:
+	kfree(sd_mpath_disk);
+out_unlock:
+	mutex_unlock(&sd_mpath_disks_lock);
+	return error;
+}
+
+static void sd_mpath_remove(struct scsi_disk *sdkp)
+{
+	struct sd_mpath_disk *sd_mpath_disk = sdkp->sd_mpath_disk;
+	struct scsi_device *sdp = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdp->scsi_mpath_dev;
+	struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+	struct scsi_mpath_head *scsi_mpath_head = sd_mpath_disk->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+	bool remove = false;
+
+	mpath_synchronize(mpath_head);
+
+	if (mpath_clear_current_path(mpath_device))
+		mpath_synchronize(mpath_head);
+
+	mpath_delete_device(mpath_device);
+
+	mutex_lock(&sd_mpath_disks_lock);
+	sd_mpath_disk->disk_count--;
+	if (!sd_mpath_disk->disk_count && mpath_can_remove_head(mpath_head)) {
+		list_del_init(&sd_mpath_disk->entry);
+		remove = true;
+	}
+	mutex_unlock(&sd_mpath_disks_lock);
+	mpath_remove_sysfs_link(mpath_device);
+	mpath_device->disk = NULL;
+
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}
+
+static void sd_mpath_remove_head(struct scsi_mpath_head *scsi_mpath_head)
+{
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+	struct sd_mpath_disk *sd_mpath_disk;
+	struct device *dev = &scsi_mpath_head->dev;
+
+	mutex_lock(&sd_mpath_disks_lock);
+	sd_mpath_disk = sd_mpath_find_disk(scsi_mpath_head);
+	if (!sd_mpath_disk) {
+		dev_warn(dev, "could not find mpath disk\n");
+		mutex_unlock(&sd_mpath_disks_lock);
+		return;
+	}
+
+	if (sd_mpath_disk->disk_count) {
+		dev_dbg(dev, "non-zero multipath disk count in removal\n");
+		sd_mpath_put_disk(sd_mpath_disk);
+		mutex_unlock(&sd_mpath_disks_lock);
+		return;
+	}
+
+	list_del_init(&sd_mpath_disk->entry);
+	mutex_unlock(&sd_mpath_disks_lock);
+
+	device_del(&sd_mpath_disk->dev);
+	mpath_remove_disk(mpath_head);
+	sd_mpath_put_disk(sd_mpath_disk);
+}
+
+/*
+ * Always calls for a failed probe, so we need to handle that some structures
+ * have not been setup.
+ */
+static void sd_mpath_fail_probe(struct scsi_disk *sdkp)
+{
+	struct sd_mpath_disk *sd_mpath_disk = sdkp->sd_mpath_disk;
+	struct scsi_mpath_device *scsi_mpath_dev;
+	struct mpath_device *mpath_device;
+	struct scsi_device *sdp = sdkp->device;
+	struct scsi_mpath_head *scsi_mpath_head;
+	struct mpath_head *mpath_head;
+	bool remove = false;
+
+	if (!sd_mpath_disk)
+		return;
+
+	scsi_mpath_dev = sdp->scsi_mpath_dev;
+	mpath_device = &scsi_mpath_dev->mpath_device;
+	scsi_mpath_head = sd_mpath_disk->scsi_mpath_head;
+	mpath_head = &scsi_mpath_head->mpath_head;
+
+	mutex_lock(&sd_mpath_disks_lock);
+	sd_mpath_disk->disk_count--;
+	if (!sd_mpath_disk->disk_count) {
+		list_del_init(&sd_mpath_disk->entry);
+		remove = true;
+	}
+	mutex_unlock(&sd_mpath_disks_lock);
+	mpath_device->disk = NULL;
+
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}
+
+#else /* CONFIG_SCSI_MULTIPATH */
+static int sd_mpath_probe(struct scsi_disk *sdkp)
+{
+	return 0;
+}
+static void sd_mpath_remove(struct scsi_disk *sdkp)
+{
+	return;
+}
+static void sd_mpath_fail_probe(struct scsi_disk *sdkp)
+{
+
+}
+static int sd_mpath_revalidate_head(struct scsi_disk *sdkp)
+{
+	return 0;
+}
+static int sd_mpath_add_disk(struct scsi_disk *sdkp)
+{
+	return 0;
+}
+#endif
+
 /**
  *	sd_probe - called during driver initialization and whenever a
  *	new scsi device is attached to the system. It is called once
@@ -4065,7 +4414,7 @@ static int sd_probe(struct scsi_device *sdp)
 	struct device *dev = &sdp->sdev_gendev;
 	struct scsi_disk *sdkp;
 	struct gendisk *gd;
-	int index;
+	int index = -1;
 	int error;
 
 	scsi_autopm_get_device(sdp);
@@ -4094,22 +4443,33 @@ static int sd_probe(struct scsi_device *sdp)
 					 &sd_bio_compl_lkclass);
 	if (!gd)
 		goto out_free;
+	sdkp->disk = gd;
+	sdkp->device = sdp;
 
-	index = ida_alloc(&sd_index_ida, GFP_KERNEL);
-	if (index < 0) {
-		sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
-		goto out_put;
-	}
+	if (sdp->scsi_mpath_dev) {
+		error = sd_mpath_probe(sdkp);
+		if (error)
+			goto out_put;
+	} else {
+		index = ida_alloc(&sd_index_ida, GFP_KERNEL);
+		if (index < 0) {
+			sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
+			goto out_put;
+		}
 
-	error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
-	if (error) {
-		sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
-		goto out_free_index;
+		error = sd_format_disk_name("sd", index, gd->disk_name,
+					DISK_NAME_LEN);
+		if (error) {
+			sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
+			goto out_free_index;
+		}
+		sdkp->index = index;
+
+		gd->major = sd_major((index & 0xf0) >> 4);
+		gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
+		gd->minors = SD_MINORS;
 	}
 
-	sdkp->device = sdp;
-	sdkp->disk = gd;
-	sdkp->index = index;
 	sdkp->max_retries = SD_MAX_RETRIES;
 	atomic_set(&sdkp->openers, 0);
 	atomic_set(&sdkp->device->ioerr_cnt, 0);
@@ -4129,6 +4489,7 @@ static int sd_probe(struct scsi_device *sdp)
 
 	error = device_add(&sdkp->disk_dev);
 	if (error) {
+		sd_mpath_fail_probe(sdkp);
 		put_device(&sdkp->disk_dev);
 		put_disk(gd);
 		goto out;
@@ -4136,10 +4497,6 @@ static int sd_probe(struct scsi_device *sdp)
 
 	dev_set_drvdata(dev, sdkp);
 
-	gd->major = sd_major((index & 0xf0) >> 4);
-	gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
-	gd->minors = SD_MINORS;
-
 	gd->fops = &sd_fops;
 	gd->private_data = sdkp;
 
@@ -4158,11 +4515,18 @@ static int sd_probe(struct scsi_device *sdp)
 	sd_revalidate_disk(gd);
 	if (sdp->sector_size > PAGE_SIZE) {
 		if (sd_large_pool_create()) {
+			sd_mpath_fail_probe(sdkp);
 			error = -ENOMEM;
 			goto out_free_index;
 		}
 	}
 
+	if (sdp->scsi_mpath_dev) {
+		error = sd_mpath_revalidate_head(sdkp);
+		if (error)
+			sdev_printk(KERN_WARNING, sdp, "could not revalidate multipath limits\n");
+	}
+
 	if (sdp->removable) {
 		gd->flags |= GENHD_FL_REMOVABLE;
 		gd->events |= DISK_EVENT_MEDIA_CHANGE;
@@ -4177,6 +4541,7 @@ static int sd_probe(struct scsi_device *sdp)
 
 	error = device_add_disk(dev, gd, NULL);
 	if (error) {
+		sd_mpath_fail_probe(sdkp);
 		device_unregister(&sdkp->disk_dev);
 		put_disk(gd);
 		if (sdp->sector_size > PAGE_SIZE)
@@ -4184,6 +4549,19 @@ static int sd_probe(struct scsi_device *sdp)
 		goto out;
 	}
 
+	if (sdp->scsi_mpath_dev) {
+		error = sd_mpath_add_disk(sdkp);
+		if (error) {
+			sd_mpath_fail_probe(sdkp);
+			device_del(&sdkp->disk_dev);
+			del_gendisk(sdkp->disk);
+
+			put_disk(sdkp->disk);
+			if (sdp->sector_size > PAGE_SIZE)
+				sd_large_pool_destroy();
+			goto out;
+		}
+	}
 	if (sdkp->security) {
 		sdkp->opal_dev = init_opal_dev(sdkp, &sd_sec_submit);
 		if (sdkp->opal_dev)
@@ -4197,7 +4575,8 @@ static int sd_probe(struct scsi_device *sdp)
 	return 0;
 
  out_free_index:
-	ida_free(&sd_index_ida, index);
+	if (index >= 0)
+		ida_free(&sd_index_ida, index);
  out_put:
 	put_disk(gd);
  out_free:
@@ -4325,6 +4704,9 @@ static void sd_remove(struct scsi_device *sdp)
 	struct device *dev = &sdp->sdev_gendev;
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
 
+	if (sdp->scsi_mpath_dev)
+		sd_mpath_remove(sdkp);
+
 	scsi_autopm_get_device(sdkp->device);
 
 	device_del(&sdkp->disk_dev);
@@ -4493,6 +4875,9 @@ static struct scsi_driver sd_template = {
 	.resume			= sd_resume,
 	.init_command		= sd_init_command,
 	.uninit_command		= sd_uninit_command,
+#ifdef CONFIG_SCSI_MULTIPATH
+	.mpath_remove_head	= sd_mpath_remove_head,
+#endif
 	.done			= sd_done,
 	.eh_action		= sd_eh_action,
 	.eh_reset		= sd_eh_reset,
Signature · 1 line
2.43.7
#59 lore raw

Re: [PATCH v6 24/27] scsi: sd: support multipath disk

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

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

New issues:
- [High] Missing put_device() in the sd_mpath_add_disk() error path permanently leaks the sdkp structure.
- [High] Inverted device teardown order causes double device_del() and sysfs warnings.
- [High] Cyclic kobject reference between sd_mpath_disk and its child gendisk guarantees both are permanently leaked on removal.
- [Medium] Ignored return value of scsi_mpath_get_head() leads to potential kref underflow and use-after-free.

Pre-existing issues:
- [High] kfree() is called on an active kobject when sd_large_pool_create() fails, leading to UAF / Sysfs Corruption. --

--- Patch [24]: [PATCH v6 24/27] scsi: sd: support multipath disk --- commit 1896d58c043437e813525b463ccbb2249c843f33
Author: John Garry <[email protected]>
scsi: sd: support multipath disk

This commit adds support for attaching a multipath disk in the SCSI sd subsystem. It allocates gendisks per-path but marks them as hidden, and maintains a global list of sd_mpath_disks to match with scsi_devices.

Quoted 16 lines
drivers/scsi/sd.c +7 -0
@@ -115,12 +115,30 @@ static mempool_t *sd_large_page_pool;
 static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
 #ifdef CONFIG_SCSI_MULTIPATH
+static LIST_HEAD(sd_mpath_disks_list);
+static DEFINE_MUTEX(sd_mpath_disks_lock);
+
 struct sd_mpath_disk {
+	struct device			dev;
+	int				disk_index;
+	int				disk_count;
+	struct list_head		entry;
 	struct scsi_mpath_head		*scsi_mpath_head;
 };

drivers/scsi/sd.c static mempool_t *sd_large_page_pool; · line 137

Quoted 10 lines
drivers/scsi/sd.c +7 -0
 
 static void sd_mpath_disk_release(struct device *dev)
 {
+	struct sd_mpath_disk *sd_mpath_disk =
+		container_of(dev, struct sd_mpath_disk, dev);
+	struct scsi_mpath_head *scsi_mpath_head =
+		sd_mpath_disk->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+	mpath_put_disk(mpath_head);

[Severity: High]
Does this create an unbreakable cyclic kobject reference? The child disk is created as a child of sd_mpath_disk->dev, meaning it holds a reference to the parent. If the child's reference is only dropped via mpath_put_disk() inside the parent's release function (sd_mpath_disk_release), the parent's release function will never execute because the child still holds a reference to it. Will this permanently leak both the sd_mpath_disk and the multipath gendisk on removal?

[ ... ]

Quoted 6 lines
drivers/scsi/sd.c +2 -0
@@ -4035,13 +4138,259 @@ static const struct attribute_group sd_mpath_disk_attr_group = {
 	.attrs		= sd_mpath_disk_attrs,
 };
 
+static int sd_mpath_probe(struct scsi_disk *sdkp)
+{

[ ... ]

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4227

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+	device_initialize(&sd_mpath_disk->dev);
+	sd_mpath_disk->dev.class = &sd_mpath_disk_class;
+
+	/* undone in sd_mpath_disk_release() */
+	scsi_mpath_get_head(scsi_mpath_head);

[Severity: Medium]
Is it safe to ignore the return value here? In sd_mpath_probe(), scsi_mpath_get_head() is called but its return value is discarded. Since scsi_mpath_get_head() explicitly uses kref_get_unless_zero() and can return -ENXIO, could a failure lead to a refcount underflow and a use-after-free during the release path?

[ ... ]

Quoted 2 lines
drivers/scsi/sd.c +2 -0
+static void sd_mpath_remove(struct scsi_disk *sdkp)
+{

[ ... ]

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4302

Quoted 6 lines
drivers/scsi/sd.c +6 -0
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}

[Severity: High]
Is this device teardown order inverted? In sd_mpath_remove() and the related sd_mpath_remove_head(), the parent device (sd_mpath_disk->dev) is deleted via device_del() before the child gendisk is removed via mpath_remove_disk(). Does this cause double device_del() operations or sysfs hierarchy corruption, since the device model requires children to be unregistered before their parents?

[ ... ]

Quoted 2 lines
drivers/scsi/sd.c +2 -0
+static void sd_mpath_fail_probe(struct scsi_disk *sdkp)
+{

[ ... ]

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4302

Quoted 6 lines
drivers/scsi/sd.c +6 -0
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}

[Severity: High]
Does the same inverted teardown order apply here in sd_mpath_fail_probe()? Deleting the parent device before the child gendisk violates the device model requirements and can trigger sysfs warnings.

[ ... ]

drivers/scsi/sd.c static int sd_probe(struct scsi_device *sdp) · line 4522

Quoted 9 lines
drivers/scsi/sd.c +1 -0
@@ -4158,11 +4515,18 @@ static int sd_probe(struct scsi_device *sdp)
 	sd_revalidate_disk(gd);
 	if (sdp->sector_size > PAGE_SIZE) {
 		if (sd_large_pool_create()) {
+			sd_mpath_fail_probe(sdkp);
 			error = -ENOMEM;
 			goto out_free_index;
 		}
 	}

[Severity: High]
This is a pre-existing issue, but does jumping to out_free_index here leak the active kobject? In sd_probe(), device_add(&sdkp->disk_dev) has already succeeded earlier in the function. When sd_large_pool_create() fails, it jumps to out_free_index and eventually calls kfree(sdkp). Should this error path use device_del() and put_device() instead to prevent sysfs corruption and a use-after-free on the active kobject?

[ ... ]

Quoted 1 line
drivers/scsi/sd.c +0 -0
@@ -4177,6 +4541,7 @@ static int sd_probe(struct scsi_device *sdp)

[ ... ]

Quoted 13 lines
drivers/scsi/sd.c +4 -0
 	error = device_add_disk(dev, gd, NULL);
 	if (error) {
+		sd_mpath_fail_probe(sdkp);
 		device_unregister(&sdkp->disk_dev);
 		put_disk(gd);
 		if (sdp->sector_size > PAGE_SIZE)
@@ -4184,6 +4549,19 @@ static int sd_probe(struct scsi_device *sdp)
 		goto out;
 	}
 
+	if (sdp->scsi_mpath_dev) {
+		error = sd_mpath_add_disk(sdkp);
+		if (error) {

drivers/scsi/sd.c static int sd_probe(struct scsi_device *sdp) · line 4564

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+			sd_mpath_fail_probe(sdkp);
+			device_del(&sdkp->disk_dev);
+			del_gendisk(sdkp->disk);
+
+			put_disk(sdkp->disk);
+			if (sdp->sector_size > PAGE_SIZE)
+				sd_large_pool_destroy();
+			goto out;
+		}
+	}

[Severity: High]
Does this error path leak the sdkp structure? In sd_probe(), if sd_mpath_add_disk() fails, the code manually calls device_del() and del_gendisk() but entirely omits put_device(&sdkp->disk_dev). Because the initial reference count set by device_initialize() is never decremented to zero, will this permanently leak the scsi_disk structure and its associated resources?

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

Re: [PATCH v6 24/27] scsi: sd: support multipath disk

Quoted 10 lines · On 29/07/2026 18:01, [email protected] wrote:

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

New issues:
- [High] Missing put_device() in the sd_mpath_add_disk() error path permanently leaks the sdkp structure.
- [High] Inverted device teardown order causes double device_del() and sysfs warnings.
- [High] Cyclic kobject reference between sd_mpath_disk and its child gendisk guarantees both are permanently leaked on removal.
- [Medium] Ignored return value of scsi_mpath_get_head() leads to potential kref underflow and use-after-free.

Pre-existing issues:
- [High] kfree() is called on an active kobject when sd_large_pool_create() fails, leading to UAF / Sysfs Corruption.

That needs to be fixed in mkp-scsi tree

Quoted 38 lines

--

--- Patch [24]: [PATCH v6 24/27] scsi: sd: support multipath disk --- commit 1896d58c043437e813525b463ccbb2249c843f33
Author: John Garry <[email protected]>
scsi: sd: support multipath disk

This commit adds support for attaching a multipath disk in the SCSI sd subsystem. It allocates gendisks per-path but marks them as hidden, and maintains a global list of sd_mpath_disks to match with scsi_devices.

Quoted 16 lines
drivers/scsi/sd.c +7 -0
@@ -115,12 +115,30 @@ static mempool_t *sd_large_page_pool;
  static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
  static struct lock_class_key sd_bio_compl_lkclass;
  #ifdef CONFIG_SCSI_MULTIPATH
+static LIST_HEAD(sd_mpath_disks_list);
+static DEFINE_MUTEX(sd_mpath_disks_lock);
+
  struct sd_mpath_disk {
+	struct device			dev;
+	int				disk_index;
+	int				disk_count;
+	struct list_head		entry;
  	struct scsi_mpath_head		*scsi_mpath_head;
  };

drivers/scsi/sd.c static mempool_t *sd_large_page_pool; · line 137

Quoted 10 lines
drivers/scsi/sd.c +7 -0
 
  static void sd_mpath_disk_release(struct device *dev)
  {
+	struct sd_mpath_disk *sd_mpath_disk =
+		container_of(dev, struct sd_mpath_disk, dev);
+	struct scsi_mpath_head *scsi_mpath_head =
+		sd_mpath_disk->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+
+	mpath_put_disk(mpath_head);
Quoted 8 lines

[Severity: High]
Does this create an unbreakable cyclic kobject reference? The child disk is created as a child of sd_mpath_disk->dev, meaning it holds a reference to the parent. If the child's reference is only dropped via mpath_put_disk() inside the parent's release function (sd_mpath_disk_release), the parent's release function will never execute because the child still holds a reference to it. Will this permanently leak both the sd_mpath_disk and the multipath gendisk on removal?

I think that the earlier del_gendisk() -> device_del(disk_to_dev(disk)) drops that reference

Quoted 10 lines

[ ... ]

Quoted 6 lines
drivers/scsi/sd.c +2 -0
@@ -4035,13 +4138,259 @@ static const struct attribute_group sd_mpath_disk_attr_group = {
  	.attrs		= sd_mpath_disk_attrs,
  };
 
+static int sd_mpath_probe(struct scsi_disk *sdkp)
+{

[ ... ]

Quoted 8 lines

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4227

Quoted 5 lines
drivers/scsi/sd.c +5 -0
+	device_initialize(&sd_mpath_disk->dev);
+	sd_mpath_disk->dev.class = &sd_mpath_disk_class;
+
+	/* undone in sd_mpath_disk_release() */
+	scsi_mpath_get_head(scsi_mpath_head);

[Severity: Medium]
Is it safe to ignore the return value here? In sd_mpath_probe(),

the count must be non-zero here

later I can add try get variant to make this clear

Quoted 10 lines

scsi_mpath_get_head() is called but its return value is discarded. Since scsi_mpath_get_head() explicitly uses kref_get_unless_zero() and can return -ENXIO, could a failure lead to a refcount underflow and a use-after-free during the release path?

[ ... ]

Quoted 2 lines
drivers/scsi/sd.c +2 -0
+static void sd_mpath_remove(struct scsi_disk *sdkp)
+{

[ ... ]

Quoted 14 lines

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4302

Quoted 6 lines
drivers/scsi/sd.c +6 -0
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}

[Severity: High]
Is this device teardown order inverted? In sd_mpath_remove() and the related sd_mpath_remove_head(), the parent device (sd_mpath_disk->dev) is deleted via device_del() before the child gendisk is removed via mpath_remove_disk(). Does this cause double device_del() operations or sysfs hierarchy corruption, since the device model requires children to be unregistered before their parents?

maybe it's a problem, but I did not experience an issue. There is a later put on the &sd_mpath_disk->dev in the sd_mpath_put_disk() call, so the parent is not gone before disk

Quoted 6 lines

[ ... ]

Quoted 2 lines
drivers/scsi/sd.c +2 -0
+static void sd_mpath_fail_probe(struct scsi_disk *sdkp)
+{

[ ... ]

Quoted 11 lines

drivers/scsi/sd.c static const struct attribute_group sd_mpath_disk_attr_group = { · line 4302

Quoted 6 lines
drivers/scsi/sd.c +6 -0
+	if (remove) {
+		device_del(&sd_mpath_disk->dev);
+		mpath_remove_disk(mpath_head);
+	}
+	sd_mpath_put_disk(sd_mpath_disk);
+}

[Severity: High]
Does the same inverted teardown order apply here in sd_mpath_fail_probe()? Deleting the parent device before the child gendisk violates the device model requirements and can trigger sysfs warnings.

Same answer as above

Quoted 15 lines

[ ... ]

drivers/scsi/sd.c static int sd_probe(struct scsi_device *sdp) · line 4522

Quoted 9 lines
drivers/scsi/sd.c +1 -0
@@ -4158,11 +4515,18 @@ static int sd_probe(struct scsi_device *sdp)
  	sd_revalidate_disk(gd);
  	if (sdp->sector_size > PAGE_SIZE) {
  		if (sd_large_pool_create()) {
+			sd_mpath_fail_probe(sdkp);
  			error = -ENOMEM;
  			goto out_free_index;
  		}
  	}

[Severity: High]
This is a pre-existing issue,

Yes, mkp-scsi tree needs fixing

Quoted 31 lines

but does jumping to out_free_index here leak
the active kobject? In sd_probe(), device_add(&sdkp->disk_dev) has already succeeded earlier in the function. When sd_large_pool_create() fails, it jumps to out_free_index and eventually calls kfree(sdkp). Should this error path use device_del() and put_device() instead to prevent sysfs corruption and a use-after-free on the active kobject?

[ ... ]

Quoted 1 line
drivers/scsi/sd.c +0 -0
@@ -4177,6 +4541,7 @@ static int sd_probe(struct scsi_device *sdp)

[ ... ]

Quoted 20 lines
  	error = device_add_disk(dev, gd, NULL);
  	if (error) {
drivers/scsi/sd.c +11 -0
+		sd_mpath_fail_probe(sdkp);
  		device_unregister(&sdkp->disk_dev);
  		put_disk(gd);
  		if (sdp->sector_size > PAGE_SIZE)
@@ -4184,6 +4549,19 @@ static int sd_probe(struct scsi_device *sdp)
  		goto out;
  	}
 
+	if (sdp->scsi_mpath_dev) {
+		error = sd_mpath_add_disk(sdkp);
+		if (error) {
+			sd_mpath_fail_probe(sdkp);
+			device_del(&sdkp->disk_dev);
+			del_gendisk(sdkp->disk);
+
+			put_disk(sdkp->disk);
+			if (sdp->sector_size > PAGE_SIZE)
+				sd_large_pool_destroy();
Quoted 8 lines

drivers/scsi/sd.c static int sd_probe(struct scsi_device *sdp) · line 4564

Quoted 3 lines
drivers/scsi/sd.c +3 -0
+			goto out;
+		}
+	}

[Severity: High]
Does this error path leak the sdkp structure? In sd_probe(), if sd_mpath_add_disk() fails, the code manually calls device_del() and del_gendisk() but entirely omits put_device(&sdkp->disk_dev).

let's fix mkp-scsi tree first

Quoted 5 lines

Because the
initial reference count set by device_initialize() is never decremented to zero, will this permanently leak the scsi_disk structure and its associated resources?

author
#61 lore raw

PATCH v6 25/27 scsi: sd: add mpath_dev file

In reply to John Garry

Add a mpath_dev file so that the multipath disk can be looked up from per-path scsi_disk directory.

The following is an example of this usage:

$ ls -l /dev/sdc
brw-rw----    1 root     disk        8,  32 Feb 24 16:08 /dev/sdc
$ cat /sys/class/scsi_mpath_disk/scsi_mpath_disk0/sdc/multipath/sdc:0/mpath_dev
8:32

This can be used by a util like lsscsi, which would find that the gendisk for the per-path scsi_disk is missing.

---
 drivers/scsi/sd.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +74 -1
index f044b2dfaa9fd..cb1bf4174248b 100644
@@ -4109,6 +4109,77 @@ static int sd_mpath_add_disk(struct scsi_disk *sdkp)
 	return 0;
 }
 
+static ssize_t sd_mpath_dev_show(struct device *dev,
+			struct device_attribute *attr, char *page)
+{
+	struct gendisk *gd = dev_to_disk(dev);
+	struct scsi_disk *sdkp = gd->private_data;
+	struct scsi_device *sdev = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct scsi_mpath_head *scsi_mpath_head =
+				scsi_mpath_dev->scsi_mpath_head;
+	struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+	struct sd_mpath_disk *sd_mpath_disk;
+	struct gendisk *disk;
+	char devt_str[13];
+	ssize_t ret = -EINVAL;
+
+	/*
+	 * This attr is for the per-path scsi_disk block device. We need to
+	 * handle the scenario that the multipath disk has been deleted and
+	 * the per-path scsi_disk (and its attr files) is still present.
+	 */
+	mutex_lock(&sd_mpath_disks_lock);
+	sd_mpath_disk = sd_mpath_find_disk(scsi_mpath_head);
+	if (!sd_mpath_disk)
+		goto out_unlock;
+	if (!sd_mpath_disk->disk_count)
+		goto out_put_disk;
+
+	disk = mpath_head->disk;
+	/* A reference to this disk is held through sd_mpath_disk */
+	if (!disk || !test_bit(GD_ADDED, &disk->state))
+		goto out_put_disk;
+	format_dev_t(devt_str, disk_to_dev(disk)->devt);
+	ret = sysfs_emit(page, "%s\n", devt_str);
+out_put_disk:
+	sd_mpath_put_disk(sd_mpath_disk);
+out_unlock:
+	mutex_unlock(&sd_mpath_disks_lock);
+	return ret;
+}
+static DEVICE_ATTR(mpath_dev, 0444, sd_mpath_dev_show, NULL);
+
+static struct attribute *sd_mpath_dev_attrs[] = {
+	&dev_attr_mpath_dev.attr,
+	NULL
+};
+
+static umode_t sd_mpath_dev_attr_is_visible(struct kobject *kobj,
+				struct attribute *attr, int i)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct gendisk *gd = dev_to_disk(dev);
+	struct scsi_disk *sdkp = gd->private_data;
+	struct scsi_device *sdev = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_device = sdev->scsi_mpath_dev;
+
+	if (!scsi_mpath_device)
+		return 0;
+
+	return attr->mode;
+}
+
+static const struct attribute_group sd_mpath_dev_attr_group = {
+	.is_visible = sd_mpath_dev_attr_is_visible,
+	.attrs = sd_mpath_dev_attrs,
+};
+
+static const struct attribute_group *sd_mpath_dev_groups[] = {
+	&sd_mpath_dev_attr_group,
+	NULL
+};
+
 static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -4389,6 +4460,8 @@ static int sd_mpath_add_disk(struct scsi_disk *sdkp)
 {
 	return 0;
 }
+
+#define sd_mpath_dev_groups NULL
 #endif
 
 /**
@@ -4539,7 +4612,7 @@ static int sd_probe(struct scsi_device *sdp)
 			sdp->host->rpm_autosuspend_delay);
 	}
 
-	error = device_add_disk(dev, gd, NULL);
+	error = device_add_disk(dev, gd, sd_mpath_dev_groups);
 	if (error) {
 		sd_mpath_fail_probe(sdkp);
 		device_unregister(&sdkp->disk_dev);
Signature · 1 line
2.43.7
author
#62 lore raw

PATCH v6 26/27 scsi: sd: add mpath_numa_nodes dev attribute

In reply to John Garry

From: John Garry <[email protected]>

Add an attribute to show multipath NUMA node per-path (scsi_disk).

The following is an example of reading the file:

$ cat /sys/devices/platform/host8/session1/target8:0:0/8:0:0:0/block/sdc:0/numa_
mpath_numa_nodes
0-3
$ cat /sys/devices/platform/host9/session2/target9:0:0/9:0:0:0/block/sdc:1/numa_
mpath_numa_nodes
$
---
 drivers/scsi/sd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/scsi/sd.c +14 -0
index cb1bf4174248b..e63a42bc3d134 100644
@@ -4150,8 +4150,22 @@ static ssize_t sd_mpath_dev_show(struct device *dev,
 }
 static DEVICE_ATTR(mpath_dev, 0444, sd_mpath_dev_show, NULL);
 
+static ssize_t sd_mpath_numa_nodes_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct gendisk *gd = dev_to_disk(dev);
+	struct scsi_disk *sdkp = gd->private_data;
+	struct scsi_device *sdev = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct mpath_device *mpath_device = &scsi_mpath_dev->mpath_device;
+
+	return mpath_numa_nodes_show(mpath_device, buf);
+}
+static DEVICE_ATTR(mpath_numa_nodes, 0444, sd_mpath_numa_nodes_show, NULL);
+
 static struct attribute *sd_mpath_dev_attrs[] = {
 	&dev_attr_mpath_dev.attr,
+	&dev_attr_mpath_numa_nodes.attr,
 	NULL
 };
 
Signature · 1 line
2.43.7
author
#63 lore raw

PATCH v6 27/27 scsi: sd: add mpath_queue_depth dev attribute

In reply to John Garry

From: John Garry <[email protected]>

Add a queue_depth file so that the multipath dynamic queue depth can be looked up from per-path gendisk (scsi_disk) directory.

---
 drivers/scsi/sd.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/scsi/sd.c +19 -0
index e63a42bc3d134..6713af46f61a6 100644
@@ -4163,9 +4163,28 @@ static ssize_t sd_mpath_numa_nodes_show(struct device *dev,
 }
 static DEVICE_ATTR(mpath_numa_nodes, 0444, sd_mpath_numa_nodes_show, NULL);
 
+static ssize_t sd_mpath_queue_depth_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct gendisk *gd = dev_to_disk(dev);
+	struct scsi_disk *sdkp = gd->private_data;
+	struct scsi_device *sdev = sdkp->device;
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct scsi_mpath_head *scsi_mpath_head =
+				scsi_mpath_dev->scsi_mpath_head;
+	struct Scsi_Host *shost = sdev->host;
+
+	if (!mpath_qd_iopolicy(&scsi_mpath_head->iopolicy))
+		return 0;
+
+	return sysfs_emit(buf, "%d\n", atomic_read(&shost->mpath_nr_active));
+}
+static DEVICE_ATTR(mpath_queue_depth, 0444, sd_mpath_queue_depth_show, NULL);
+
 static struct attribute *sd_mpath_dev_attrs[] = {
 	&dev_attr_mpath_dev.attr,
 	&dev_attr_mpath_numa_nodes.attr,
+	&dev_attr_mpath_queue_depth.attr,
 	NULL
 };
 
Signature · 1 line
2.43.7