nvme-multipath: introduce adaptive I/O policy
Subsystems (2)
v4 cover nvme-multipath: introduce adaptive I/O policy
Hi,
This series introduces a new adaptive I/O policy for NVMe native multipath. Existing policies such as numa, round-robin, and queue-depth are static and do not adapt to real-time transport performance. The numa selects the path closest to the NUMA node of the current CPU, optimizing memory and path locality, but ignores actual path performance. The round-robin distributes I/O evenly across all paths, providing fairness but not performance awareness. The queue-depth reacts to instantaneous queue occupancy, avoiding heavily loaded paths, but does not account for actual latency, throughput, or link speed.
The new adaptive policy addresses these gaps selecting paths dynamically based on measured I/O latency for both PCIe and fabrics. Latency is derived by passively sampling I/O completions. Each path is assigned a weight proportional to its latency score, and I/Os are then forwarded accordingly. As condition changes (e.g. latency spikes, bandwidth differences), path weights are updated, automatically steering traffic toward better-performing paths.
Early results show reduced tail latency under mixed workloads and improved throughput by exploiting higher-speed links more effectively. For example, with NVMf/TCP using two paths (one throttled with ~30 ms delay), fio results with random read/write/rw workloads (direct I/O) showed:
numa round-robin queue-depth adaptive
----------- ----------- ----------- ---------
READ: 50.0 MiB/s 105 MiB/s 230 MiB/s 350 MiB/s
WRITE: 65.9 MiB/s 125 MiB/s 385 MiB/s 446 MiB/s
RW: R:30.6 MiB/s R:56.5 MiB/s R:122 MiB/s R:175 MiB/s
W:30.7 MiB/s W:56.5 MiB/s W:122 MiB/s W:175 MiB/sThis pathcset includes totla 6 patches:
[PATCH 1/6] block: expose blk_stat_{enable,disable}_accounting()
- Make blk_stat APIs available to block drivers.
- Needed for per-path latency measurement in adaptive policy.
[PATCH 2/6] nvme-multipath: add adaptive I/O policy
- Implement path scoring based on latency (EWMA).
- Distribute I/O proportionally to per-path weights.
[PATCH 3/6] nvme: add generic debugfs support
- Introduce generic debugfs support for NVMe module
[PATCH 4/6] nvme-multipath: add debugfs attribute adaptive_ewma_shift
- Adds a debugfs attribute to control ewma shift
[PATCH 5/6] nvme-multipath: add debugfs attribute adaptive_weight_timeout
- Adds a debugfs attribute to control path weight calculation timeout
[PATCH 6/6] nvme-multipath: add debugfs attribute adaptive_stat
- Add “adaptive_stat” under per-path and head debugfs directories to expose adaptive policy state and statistics.
As ususal, feedback and suggestions are most welcome!
Thanks!
Changes from v3:
- Update the adaptive APIs name (which actually enable/disable adaptive policy) to reflect the actual work it does. Also removed the misleading use of "current_path" from the adaptive policy code (Hannes Reinecke)
- Move adaptive_ewma_shift and adaptive_weight_timeout attributes from sysfs to debugfs (Hannes Reinecke)
Link to v3: https://lore.kernel.org/all/[email protected]/
Changes from v2:
- Addede a new patch to allow user to configure EWMA shift through sysfs (Hannes Reinecke)
- Added a new patch to allow user to configure path weight calculation timeout (Hannes Reinecke)
- Distinguish between read/write and other commands (e.g. admin comamnd) and calculate path weight for other commands which is separate from read/write weight. (Hannes Reinecke)
- Normalize per-path weight in the range from 0-128 instead
of 0-100 (Hannes Reinecke)
- Restructure and optimize adaptive I/O forwarding code to use
one loop instead of two (Hannes Reinecke)
Link to v2: https://lore.kernel.org/all/[email protected]/
Changes from v1:
- Ensure that the completion of I/O occurs on the same CPU as the submitting I/O CPU (Hannes Reinecke)
- Remove adapter link speed from the path weight calculation
(Hannes Reinecke)
- Add adaptive I/O stat under debugfs instead of current sysfs (Hannes Reinecke)
- Move path weight calculation to a workqueue from IO completion code path
Link to v1: https://lore.kernel.org/all/[email protected]/
Nilay Shroff (6):
block: expose blk_stat_{enable,disable}_accounting() to drivers
nvme-multipath: add support for adaptive I/O policy
nvme: add generic debugfs support
nvme-multipath: add debugfs attribute adaptive_ewma_shift
nvme-multipath: add debugfs attribute adaptive_weight_timeout
nvme-multipath: add debugfs attribute adaptive_stat
block/blk-stat.h | 4 - drivers/nvme/host/Makefile | 2 +- drivers/nvme/host/core.c | 22 +- drivers/nvme/host/debugfs.c | 335 ++++++++++++++++++++++++++ drivers/nvme/host/ioctl.c | 31 ++- drivers/nvme/host/multipath.c | 430 +++++++++++++++++++++++++++++++++- drivers/nvme/host/nvme.h | 87 ++++++- drivers/nvme/host/pr.c | 6 +- drivers/nvme/host/sysfs.c | 2 +- include/linux/blk-mq.h | 4 + 10 files changed, 895 insertions(+), 28 deletions(-) create mode 100644 drivers/nvme/host/debugfs.c
Signature · 1 line
2.51.0
RFC PATCH v4 1/6 block: expose blk_stat_{enable,disable}_accounting() to drivers
The functions blk_stat_enable_accounting() and blk_stat_disable_accounting() are currently exported, but their prototypes are only defined in a private header. Move these prototypes into a common header so that block drivers can directly use these APIs.
--- block/blk-stat.h | 4 ---- include/linux/blk-mq.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-)
block/blk-stat.h +0 -4
@@ -67,10 +67,6 @@ void blk_free_queue_stats(struct blk_queue_stats *);void blk_stat_add (struct request *rq,u64 now); -/* record time/size info in request but not add a callback */ -void blk_stat_enable_accounting (struct request_queue *q); -void blk_stat_disable_accounting (struct request_queue *q); -/** * blk_stat_alloc_callback() - Allocate a block statistics callback. * @timer_fn: Timer callback function.
include/linux/blk-mq.h +4 -0
@@ -735,6 +735,10 @@ int blk_rq_poll(struct request *rq, struct io_comp_batch *iob,bool blk_mq_queue_inflight (struct request_queue *q); +/* record time/size info in request but not add a callback */ +void blk_stat_enable_accounting (struct request_queue *q); +void blk_stat_disable_accounting (struct request_queue *q); +enum {/* return when out of requests */ BLK_MQ_REQ_NOWAIT = (__force blk_mq_req_flags_t)(1 <<0 ),
Signature · 1 line
2.51.0
RFC PATCH v4 2/6 nvme-multipath: add support for adaptive I/O policy
In reply to Nilay ShroffThis commit introduces a new I/O policy named "adaptive". Users can configure it by writing "adaptive" to "/sys/class/nvme-subsystem/nvme- subsystemX/iopolicy"
The adaptive policy dynamically distributes I/O based on measured completion latency. The main idea is to calculate latency for each path, derive a weight, and then proportionally forward I/O according to those weights.
To ensure scalability, path latency is measured per-CPU. Each CPU maintains its own statistics, and I/O forwarding uses these per-CPU values. Every ~15 seconds, a simple average latency of per-CPU batched samples are computed and fed into an Exponentially Weighted Moving Average (EWMA):
avg_latency = div_u64(batch, batch_count);new_ewma_latency = (prev_ewma_latency * (WEIGHT-1) + avg_latency)/WEIGHT
With WEIGHT = 8, this assigns 7/8 (~87.5%) weight to the previous latency value and 1/8 (~12.5%) to the most recent latency. This smoothing reduces jitter, adapts quickly to changing conditions, avoids storing historical samples, and works well for both low and high I/O rates. Path weights are then derived from the smoothed (EWMA) latency as follows (example with two paths A and B):
path_A_score = NSEC_PER_SEC / path_A_ewma_latency path_B_score = NSEC_PER_SEC / path_B_ewma_latency total_score = path_A_score + path_B_score
path_A_weight = (path_A_score * 100) / total_score path_B_weight = (path_B_score * 100) / total_score
where:
- path_X_ewma_latency is the smoothed latency of a path in nanoseconds
- NSEC_PER_SEC is used as a scaling factor since valid latencies are < 1 second
- weights are normalized to a 0–64 scale across all paths.
Path credits are refilled based on this weight, with one credit consumed per I/O. When all credits are consumed, the credits are refilled again based on the current weight. This ensures that I/O is distributed across paths proportionally to their calculated weight.
--- drivers/nvme/host/core.c | 15 +- drivers/nvme/host/ioctl.c | 31 ++- drivers/nvme/host/multipath.c | 425 ++++++++++++++++++++++++++++++++-- drivers/nvme/host/nvme.h | 74 +++++- drivers/nvme/host/pr.c | 6 +- drivers/nvme/host/sysfs.c | 2 +- 6 files changed, 530 insertions(+), 23 deletions(-)
drivers/nvme/host/core.c +13 -2
@@ -672,6 +672,9 @@ static void nvme_free_ns_head(struct kref *ref)cleanup_srcu_struct (&head->srcu);nvme_put_subsystem (head->subsys);kfree (head->plids); +#ifdef CONFIG_NVME_MULTIPATH +free_percpu (head->adp_path); +#endif kfree (head); }
@@ -689,6 +692,7 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +nvme_free_ns_stat (ns);put_disk (ns->disk);nvme_put_ns_head (ns->head);nvme_put_ctrl (ns->ctrl);
@@ -4137,6 +4141,9 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)if (nvme_init_ns_head (ns, info)) goto out_cleanup_disk; +if (nvme_alloc_ns_stat (ns)) + goto out_unlink_ns; +/* * If multipathing is enabled, the device name for all disks and not * just those that represent shared namespaces needs to be based on the
@@ -4161,7 +4168,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info) }if (nvme_update_ns_info (ns, info)) - goto out_unlink_ns; + goto out_free_ns_stat;mutex_lock (&ctrl->namespaces_lock);/*
@@ -4170,7 +4177,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)*/ if (test_bit (NVME_CTRL_FROZEN , &ctrl->flags)) {mutex_unlock (&ctrl->namespaces_lock); - goto out_unlink_ns; + goto out_free_ns_stat; }nvme_ns_add_to_ctrl_list (ns);mutex_unlock (&ctrl->namespaces_lock);
@@ -4201,6 +4208,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)list_del_rcu (&ns->list);mutex_unlock (&ctrl->namespaces_lock);synchronize_srcu (&ctrl->srcu); +out_free_ns_stat: +nvme_free_ns_stat (ns); out_unlink_ns:mutex_lock (&ctrl->subsys->lock);list_del_rcu (&ns->siblings);
@@ -4244,6 +4253,8 @@ static void nvme_ns_remove(struct nvme_ns *ns)*/ synchronize_srcu (&ns->head->srcu); +nvme_mpath_cancel_adaptive_path_weight_work (ns); +/* wait for concurrent submissions */ if (nvme_mpath_clear_current_path (ns))synchronize_srcu (&ns->head->srcu);
drivers/nvme/host/ioctl.c +28 -3
@@ -700,18 +700,29 @@ static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,int nvme_ns_head_ioctl (struct block_device *bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg) { +u8 opcode;struct nvme_ns_head *head = bdev->bd_disk->private_data;bool open_for_write = mode &BLK_OPEN_WRITE ;void __user *argp = (void __user *)arg;struct nvme_ns *ns;int srcu_idx, ret = -EWOULDBLOCK ;unsigned int flags =0 ; +unsigned int op_type =NVME_STAT_OTHER ;if (bdev_is_partition (bdev)) flags |=NVME_IOCTL_PARTITION ; +if (cmd ==NVME_IOCTL_SUBMIT_IO ) { +if (get_user (opcode, (u8 *)argp)) +return -EFAULT ; +if (opcode == nvme_cmd_write) + op_type =NVME_STAT_WRITE ; +else if (opcode == nvme_cmd_read) + op_type =NVME_STAT_READ ; + } + srcu_idx =srcu_read_lock (&head->srcu); - ns =nvme_find_path (head); + ns =nvme_find_path (head, op_type);if (!ns) goto out_unlock;
@@ -733,6 +744,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, blk_mode_t mode,long nvme_ns_head_chr_ioctl (struct file *file,unsigned int cmd,unsigned long arg) { +u8 opcode;bool open_for_write = file->f_mode &FMODE_WRITE ;struct cdev *cdev =file_inode (file)->i_cdev; struct nvme_ns_head *head =
@@ -740,9 +752,19 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,void __user *argp = (void __user *)arg;struct nvme_ns *ns;int srcu_idx, ret = -EWOULDBLOCK ; +unsigned int op_type =NVME_STAT_OTHER ; + +if (cmd ==NVME_IOCTL_SUBMIT_IO ) { +if (get_user (opcode, (u8 *)argp)) +return -EFAULT ; +if (opcode == nvme_cmd_write) + op_type =NVME_STAT_WRITE ; +else if (opcode == nvme_cmd_read) + op_type =NVME_STAT_READ ; + } srcu_idx =srcu_read_lock (&head->srcu); - ns =nvme_find_path (head); + ns =nvme_find_path (head, op_type);if (!ns) goto out_unlock;
@@ -762,7 +784,10 @@ int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,struct cdev *cdev =file_inode (ioucmd->file)->i_cdev; struct nvme_ns_head *head = container_of(cdev, struct nvme_ns_head, cdev);int srcu_idx =srcu_read_lock (&head->srcu); -struct nvme_ns *ns =nvme_find_path (head); +const struct nvme_uring_cmd *cmd =io_uring_sqe_cmd (ioucmd->sqe); +struct nvme_ns *ns =nvme_find_path (head, +READ_ONCE (cmd->opcode) &1 ? +NVME_STAT_WRITE :NVME_STAT_READ );int ret = -EINVAL ;if (ns)
drivers/nvme/host/multipath.c +412 -13
538 lines not on this page · raw message
drivers/nvme/host/nvme.h +72 -2
140 lines not on this page · raw message
drivers/nvme/host/pr.c +4 -2
@@ -53,10 +53,12 @@ static int nvme_send_ns_head_pr_command(struct block_device *bdev, struct nvme_command *c, void *data, unsigned int data_len) { struct nvme_ns_head *head = bdev->bd_disk->private_data; -int srcu_idx =srcu_read_lock (&head->srcu); -struct nvme_ns *ns =nvme_find_path (head); +int srcu_idx; +struct nvme_ns *ns;int ret = -EWOULDBLOCK ; + srcu_idx =srcu_read_lock (&head->srcu); + ns =nvme_find_path (head,NVME_STAT_OTHER );if (ns) { c->common.nsid =cpu_to_le32 (ns->head->ns_id); ret =nvme_submit_sync_cmd (ns->queue, c, data, data_len);
drivers/nvme/host/sysfs.c +1 -1
@@ -194,7 +194,7 @@ static int ns_head_update_nuse(struct nvme_ns_head *head)return 0 ; srcu_idx =srcu_read_lock (&head->srcu); - ns =nvme_find_path (head); + ns =nvme_find_path (head,NVME_STAT_OTHER );if (!ns) goto out_unlock;
Signature · 1 line
2.51.0
Re: [RFC PATCHv4 2/6] nvme-multipath: add support for adaptive I/O policy
Quoted 44 lines · Nilay Shroff
This commit introduces a new I/O policy named "adaptive". Users can configure it by writing "adaptive" to "
/sys/class/nvme-subsystem/nvme-subsystemX/iopolicy"The adaptive policy dynamically distributes I/O based on measured completion latency. The main idea is to calculate latency for each path, derive a weight, and then proportionally forward I/O according to those weights.
To ensure scalability, path latency is measured per-CPU. Each CPU maintains its own statistics, and I/O forwarding uses these per-CPU values. Every ~15 seconds, a simple average latency of per-CPU batched samples are computed and fed into an Exponentially Weighted Moving Average (EWMA):
avg_latency=div_u64(batch, batch_count);new_ewma_latency= (prev_ewma_latency* (WEIGHT-1) + avg_latency)/WEIGHTWith WEIGHT = 8, this assigns 7/8 (~87.5%) weight to the previous latency value and 1/8 (~12.5%) to the most recent latency. This smoothing reduces jitter, adapts quickly to changing conditions, avoids storing historical samples, and works well for both low and high I/O rates. Path weights are then derived from the smoothed (EWMA) latency as follows (example with two paths A and B):
path_A_score=NSEC_PER_SEC/path_A_ewma_latencypath_B_score=NSEC_PER_SEC/path_B_ewma_latencytotal_score=path_A_score+path_B_score
path_A_weight= (path_A_score* 100) /total_scorepath_B_weight= (path_B_score* 100) /total_scorewhere:
-path_X_ewma_latencyis the smoothed latency of a path in nanoseconds
-NSEC_PER_SECis used as a scaling factor since valid latencies are < 1 second
- weights are normalized to a 0–64 scale across all paths.Path credits are refilled based on this weight, with one credit consumed per I/O. When all credits are consumed, the credits are refilled again based on the current weight. This ensures that I/O is distributed across paths proportionally to their calculated weight.
Signed-off-by Nilay Shroff <[email protected]>
Quoted 9 lines
--- drivers/nvme/host/core.c | 15 +- drivers/nvme/host/ioctl.c | 31 ++- drivers/nvme/host/multipath.c | 425 ++++++++++++++++++++++++++++++++-- drivers/nvme/host/nvme.h | 74 +++++- drivers/nvme/host/pr.c | 6 +- drivers/nvme/host/sysfs.c | 2 +- 6 files changed, 530 insertions(+), 23 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
RFC PATCH v4 3/6 nvme: add generic debugfs support
In reply to Nilay ShroffAdd generic infrastructure for creating and managing debugfs files in the NVMe module. This introduces helper APIs that allow NVMe drivers to register and unregister debugfs entries, along with a reusable attribute structure for defining new debugfs files.
The implementation uses seq_file interfaces to safely expose per-NS and per-NS-head statistics, while supporting both simple show callbacks and full seq_operations.
--- drivers/nvme/host/Makefile | 2 +- drivers/nvme/host/core.c | 3 + drivers/nvme/host/debugfs.c | 138 ++++++++++++++++++++++++++++++++++ drivers/nvme/host/multipath.c | 2 + drivers/nvme/host/nvme.h | 10 +++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 drivers/nvme/host/debugfs.c
drivers/nvme/host/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_NVME_FC) += nvme-fc.o obj-$(CONFIG_NVME_TCP) += nvme-tcp.o obj-$(CONFIG_NVME_APPLE) += nvme-apple.o -nvme-core-y += core.o ioctl.o sysfs.o pr.o +nvme-core-y += core.o ioctl.o sysfs.o pr.o debugfs.o nvme-core-$(CONFIG_NVME_VERBOSE_ERRORS) += constants.o nvme-core-$(CONFIG_TRACING) += trace.o nvme-core-$(CONFIG_NVME_MULTIPATH) += multipath.o
drivers/nvme/host/core.c +3 -0
@@ -4187,6 +4187,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)if (device_add_disk (ctrl->device, ns->disk, nvme_ns_attr_groups)) goto out_cleanup_ns_from_list; +nvme_debugfs_register (ns->disk); +if (!nvme_ns_head_multipath (ns->head))nvme_add_ns_cdev (ns);
@@ -4276,6 +4278,7 @@ static void nvme_ns_remove(struct nvme_ns *ns)nvme_mpath_remove_sysfs_link (ns); +nvme_debugfs_unregister (ns->disk);del_gendisk (ns->disk);mutex_lock (&ns->ctrl->namespaces_lock);
drivers/nvme/host/debugfs.c new file +138 -0
138 lines not on this page · raw message
drivers/nvme/host/multipath.c +2 -0
@@ -1086,6 +1086,7 @@ static void nvme_remove_head(struct nvme_ns_head *head)nvme_cdev_del (&head->cdev, &head->cdev_device);synchronize_srcu (&head->srcu); +nvme_debugfs_unregister (head->disk);del_gendisk (head->disk); }nvme_put_ns_head (head);
@@ -1192,6 +1193,7 @@ static void nvme_mpath_set_live(struct nvme_ns *ns) }nvme_add_ns_head_cdev (head);kblockd_schedule_work (&head->partition_scan_work); +nvme_debugfs_register (head->disk); }nvme_mpath_add_sysfs_link (ns->head);
drivers/nvme/host/nvme.h +10 -0
@@ -1000,6 +1000,16 @@ static inline int nvme_data_dir(const enum req_op op)return NVME_STAT_OTHER ; } +void nvme_debugfs_register (struct gendisk *disk); +static inline void nvme_debugfs_unregister (struct gendisk *disk) +{ +/* +* Nothing to do for now. When the request queue is unregistered, +* all files under q->debugfs_dir are recursively deleted. +* This is just a placeholder; the compiler will optimize it out. +*/ +} +#ifdef CONFIG_NVME_MULTIPATH static inline bool nvme_ctrl_use_ana (struct nvme_ctrl *ctrl) {
Signature · 1 line
2.51.0
RFC PATCH v4 4/6 nvme-multipath: add debugfs attribute adaptive_ewma_shift
In reply to Nilay ShroffBy default, the EWMA (Exponentially Weighted Moving Average) shift value, used for storing latency samples for adaptive iopolicy, is set to 3. The EWMA is calculated using the following formula:
ewma = (old * ((1 << ewma_shift) - 1) + new) >> ewma_shift;
The default value of 3 assigns ~87.5% weight to the existing EWMA value and ~12.5% weight to the new latency sample. This provides a stable average that smooths out short-term variations.
However, different workloads may require faster or slower adaptation to changing conditions. This commit introduces a new debugfs attribute, adaptive_ewma_shift, allowing users to tune the weighting factor.
For example:
- adaptive_ewma_shift = 2 => 75% old, 25% new
- adaptive_ewma_shift = 1 => 50% old, 50% new
- adaptive_ewma_shift = 0 => 0% old, 100% new
--- drivers/nvme/host/core.c | 3 +++ drivers/nvme/host/debugfs.c | 46 +++++++++++++++++++++++++++++++++++ drivers/nvme/host/multipath.c | 8 +++--- drivers/nvme/host/nvme.h | 1 + 4 files changed, 54 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +3 -0
@@ -3913,6 +3913,9 @@ static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, head->ids = info->ids; head->shared = info->is_shared; head->rotational = info->is_rotational; +#ifdef CONFIG_NVME_MULTIPATH + head->adp_ewma_shift =NVME_DEFAULT_ADP_EWMA_SHIFT ; +#endif ratelimit_state_init (&head->rs_nuse,5 *HZ ,1 );ratelimit_set_flags (&head->rs_nuse,RATELIMIT_MSG_ON_RELEASE );kref_init (&head->ref);
drivers/nvme/host/debugfs.c +46 -0
54 lines not on this page · raw message
drivers/nvme/host/multipath.c +4 -4
@@ -294,10 +294,9 @@ static void nvme_mpath_weight_work(struct work_struct *weight_work)* For instance, with EWMA_SHIFT = 3, this assigns 7/8 (~87.5 %) weight to * the existing/old ewma and 1/8 (~12.5%) weight to the new sample. */ -static inline u64 ewma_update (u64 old,u64 new) +static inline u64 ewma_update (u64 old,u64 new,u32 ewma_shift) { -return (old * ((1 <<NVME_DEFAULT_ADP_EWMA_SHIFT ) -1 ) - + new) >>NVME_DEFAULT_ADP_EWMA_SHIFT ; +return (old * ((1 << ewma_shift) -1 ) + new) >> ewma_shift; }static void nvme_mpath_add_sample (struct request *rq,struct nvme_ns *ns)
@@ -389,7 +388,8 @@ static void nvme_mpath_add_sample(struct request *rq, struct nvme_ns *ns)if (unlikely (!stat->slat_ns))WRITE_ONCE (stat->slat_ns, avg_lat_ns);else { - slat_ns =ewma_update (stat->slat_ns, avg_lat_ns); + slat_ns =ewma_update (stat->slat_ns, avg_lat_ns, +READ_ONCE (head->adp_ewma_shift));WRITE_ONCE (stat->slat_ns, slat_ns); }
drivers/nvme/host/nvme.h +1 -0
@@ -545,6 +545,7 @@ struct nvme_ns_head {unsigned int delayed_removal_secs;struct nvme_ns *__percpu *adp_path; +u32 adp_ewma_shift;#define NVME_NSHEAD_DISK_LIVE 0#define NVME_NSHEAD_QUEUE_IF_NO_PATH 1
Signature · 1 line
2.51.0
Re: [RFC PATCHv4 4/6] nvme-multipath: add debugfs attribute adaptive_ewma_shift
Quoted 19 lines · Nilay Shroff
By default, the EWMA (Exponentially Weighted Moving Average) shift value, used for storing latency samples for adaptive iopolicy, is set to 3. The EWMA is calculated using the following formula:
ewma = (old * ((1 << ewma_shift) - 1) + new) >> ewma_shift;The default value of 3 assigns ~87.5% weight to the existing EWMA value and ~12.5% weight to the new latency sample. This provides a stable average that smooths out short-term variations.
However, different workloads may require faster or slower adaptation to changing conditions. This commit introduces a new debugfs attribute,
adaptive_ewma_shift, allowing users to tune the weighting factor.For example:
-adaptive_ewma_shift= 2 => 75% old, 25% new
-adaptive_ewma_shift= 1 => 50% old, 50% new
-adaptive_ewma_shift= 0 => 0% old, 100% new
Quoted 8 lines
Signed-off-by Nilay Shroff <[email protected]>--- drivers/nvme/host/core.c | 3 +++ drivers/nvme/host/debugfs.c | 46 +++++++++++++++++++++++++++++++++++ drivers/nvme/host/multipath.c | 8 +++--- drivers/nvme/host/nvme.h | 1 + 4 files changed, 54 insertions(+), 4 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
RFC PATCH v4 5/6 nvme-multipath: add debugfs attribute adaptive_weight_timeout
In reply to Nilay ShroffBy default, the adaptive I/O policy accumulates latency samples over a 15-second window. When this window expires, the driver computes the average latency and updates the smoothed (EWMA) latency value. The path weight is then recalculated based on this data.
A 15-second window provides a good balance for most workloads, as it helps smooth out transient latency spikes and produces a more stable path weight profile. However, some workloads may benefit from faster or slower adaptation to changing latency conditions.
This commit introduces a new debugfs attribute, adaptive_weight_timeout, which allows users to configure the path weight calculation interval based on their workload requirements.
--- drivers/nvme/host/core.c | 1 + drivers/nvme/host/debugfs.c | 40 ++++++++++++++++++++++++++++++++++- drivers/nvme/host/multipath.c | 7 ++++-- drivers/nvme/host/nvme.h | 1 + 4 files changed, 46 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +1 -0
@@ -3915,6 +3915,7 @@ static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, head->rotational = info->is_rotational;#ifdef CONFIG_NVME_MULTIPATH head->adp_ewma_shift =NVME_DEFAULT_ADP_EWMA_SHIFT ; + head->adp_weight_timeout =NVME_DEFAULT_ADP_WEIGHT_TIMEOUT ;#endif ratelimit_state_init (&head->rs_nuse,5 *HZ ,1 );ratelimit_set_flags (&head->rs_nuse,RATELIMIT_MSG_ON_RELEASE );
drivers/nvme/host/debugfs.c +39 -1
@@ -146,12 +146,50 @@ static ssize_t nvme_adp_ewma_shift_store(void *data, const char __user *ubuf,WRITE_ONCE (head->adp_ewma_shift, res);return count; } + +static int nvme_adp_weight_timeout_show (void *data,struct seq_file *m) +{ +struct nvme_ns_head *head = data; + +seq_printf (m,"%llu\n" , +div_u64 (READ_ONCE (head->adp_weight_timeout),NSEC_PER_SEC )); +return 0 ; +} + +static ssize_t nvme_adp_weight_timeout_store (void *data, +const char __user *ubuf, +size_t count,loff_t *ppos) +{ +struct nvme_ns_head *head = data; +char kbuf[8 ]; +u32 res; +int ret; +size_t len; +char *arg; + + len =min (sizeof (kbuf) -1 , count); + +if (copy_from_user (kbuf, ubuf, len)) +return -EFAULT ; + + kbuf[len] ='\0' ; + arg =strstrip (kbuf); + + ret =kstrtou32 (arg,0 , &res); +if (ret) +return ret; + +WRITE_ONCE (head->adp_weight_timeout, res *NSEC_PER_SEC ); +return count; +}#endif static const struct nvme_debugfs_attr nvme_mpath_debugfs_attrs[] = { #ifdef CONFIG_NVME_MULTIPATH - {"adaptive_ewma_shift" ,0600 , nvme_adp_ewma_shift_show, + {"adaptive_ewma_shift" ,0600 , nvme_adp_ewma_shift_show, nvme_adp_ewma_shift_store}, + {"adaptive_weight_timeout" ,0600 , nvme_adp_weight_timeout_show, + nvme_adp_weight_timeout_store}, #endif {}, };
drivers/nvme/host/multipath.c +5 -2
@@ -362,8 +362,11 @@ static void nvme_mpath_add_sample(struct request *rq, struct nvme_ns *ns) stat->batch_count++; stat->nr_samples++; -if (now > stat->last_weight_ts && - (now - stat->last_weight_ts) >=NVME_DEFAULT_ADP_WEIGHT_TIMEOUT ) { +if (now > stat->last_weight_ts) { +u64 timeout =READ_ONCE (head->adp_weight_timeout); + +if ((now - stat->last_weight_ts) < timeout) +return ; stat->last_weight_ts = now;
drivers/nvme/host/nvme.h +1 -0
@@ -546,6 +546,7 @@ struct nvme_ns_head {struct nvme_ns *__percpu *adp_path;u32 adp_ewma_shift; +u64 adp_weight_timeout;#define NVME_NSHEAD_DISK_LIVE 0#define NVME_NSHEAD_QUEUE_IF_NO_PATH 1
Signature · 1 line
2.51.0
Re: [RFC PATCHv4 5/6] nvme-multipath: add debugfs attribute adaptive_weight_timeout
Quoted 14 lines · Nilay Shroff
By default, the adaptive I/O policy accumulates latency samples over a 15-second window. When this window expires, the driver computes the average latency and updates the smoothed (EWMA) latency value. The path weight is then recalculated based on this data.
A 15-second window provides a good balance for most workloads, as it helps smooth out transient latency spikes and produces a more stable path weight profile. However, some workloads may benefit from faster or slower adaptation to changing latency conditions.
This commit introduces a new debugfs attribute,
adaptive_weight_timeout, which allows users to configure the path weight calculation interval based on their workload requirements.
Quoted 8 lines
Signed-off-by Nilay Shroff <[email protected]>--- drivers/nvme/host/core.c | 1 + drivers/nvme/host/debugfs.c | 40 ++++++++++++++++++++++++++++++++++- drivers/nvme/host/multipath.c | 7 ++++-- drivers/nvme/host/nvme.h | 1 + 4 files changed, 46 insertions(+), 3 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
RFC PATCH v4 6/6 nvme-multipath: add debugfs attribute adaptive_stat
In reply to Nilay ShroffThis commit introduces a new debugfs attribute, "adaptive_stat", under both per-path and head debugfs directories (defined under /sys/kernel/ debug/block/). This attribute provides visibility into the internal state of the adaptive I/O policy to aid in debugging and performance analysis.
For per-path entries, "adaptive_stat" reports the corresponding path statistics such as I/O weight, selection count, processed samples, and ignored samples.
For head entries, it reports per-CPU statistics for each reachable path, including I/O weight, path score, smoothed (EWMA) latency, selection count, processed samples, and ignored samples.
These additions enhance observability of the adaptive I/O path selection behavior and help diagnose imbalance or instability in multipath performance.
--- drivers/nvme/host/debugfs.c | 113 ++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+)
drivers/nvme/host/debugfs.c +113 -0
@@ -182,6 +182,115 @@ static ssize_t nvme_adp_weight_timeout_store(void *data,WRITE_ONCE (head->adp_weight_timeout, res *NSEC_PER_SEC );return count; } + +static void *nvme_mpath_adp_stat_start (struct seq_file *m,loff_t *pos) +{ +struct nvme_ns *ns; +struct nvme_debugfs_ctx *ctx = m->private; +struct nvme_ns_head *head = ctx->data; + +/* Remember srcu index, so we can unlock later. */ + ctx->srcu_idx =srcu_read_lock (&head->srcu); + ns = list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); + +while (*pos && ns) { + ns =list_next_or_null_rcu (&head->list, &ns->siblings, + struct nvme_ns, siblings); + (*pos)--; + } + +return ns; +} + +static void *nvme_mpath_adp_stat_next (struct seq_file *m,void *v,loff_t *pos) +{ +struct nvme_ns *ns = v; +struct nvme_debugfs_ctx *ctx = m->private; +struct nvme_ns_head *head = ctx->data; + + (*pos)++; + +return list_next_or_null_rcu (&head->list, &ns->siblings, + struct nvme_ns, siblings); +} + +static void nvme_mpath_adp_stat_stop (struct seq_file *m,void *v) +{ +struct nvme_debugfs_ctx *ctx = m->private; +struct nvme_ns_head *head = ctx->data; +int srcu_idx = ctx->srcu_idx; + +srcu_read_unlock (&head->srcu, srcu_idx); +} + +static int nvme_mpath_adp_stat_show (struct seq_file *m,void *v) +{ +int i, cpu; +struct nvme_path_stat *stat; +struct nvme_ns *ns = v; + +seq_printf (m,"%s:\n" , ns->disk->disk_name); +for_each_online_cpu (cpu) { +seq_printf (m,"cpu %d : " , cpu); +for (i =0 ; i <NVME_NUM_STAT_GROUPS ; i++) { + stat = &per_cpu_ptr (ns->info, cpu)[i].stat; +seq_printf (m,"%u %u %llu %llu %llu %llu %llu " , + stat->weight, stat->credit, stat->score, + stat->slat_ns, stat->sel, + stat->nr_samples, stat->nr_ignored); + } +seq_putc (m,'\n' ); + } +return 0 ; +} + +static const struct seq_operations nvme_mpath_adp_stat_seq_ops = { + .start = nvme_mpath_adp_stat_start, + .next = nvme_mpath_adp_stat_next, + .stop = nvme_mpath_adp_stat_stop, + .show = nvme_mpath_adp_stat_show +}; + +static void adp_stat_read_all (struct nvme_ns *ns,struct nvme_path_stat *batch) +{ +int i, cpu; +u32 ncpu[NVME_NUM_STAT_GROUPS ] = {0 }; +struct nvme_path_stat *stat; + +for_each_online_cpu (cpu) { +for (i =0 ; i <NVME_NUM_STAT_GROUPS ; i++) { + stat = &per_cpu_ptr (ns->info, cpu)[i].stat; + batch[i].sel += stat->sel; + batch[i].nr_samples += stat->nr_samples; + batch[i].nr_ignored += stat->nr_ignored; + batch[i].weight += stat->weight; +if (stat->weight) + ncpu[i]++; + } + } + +for (i =0 ; i <NVME_NUM_STAT_GROUPS ; i++) { +if (!ncpu[i]) +continue ; + batch[i].weight =DIV_U64_ROUND_CLOSEST (batch[i].weight, + ncpu[i]); + } +} + +static int nvme_ns_adp_stat_show (void *data,struct seq_file *m) +{ +int i; +struct nvme_path_stat stat[NVME_NUM_STAT_GROUPS ] = {0 }; +struct nvme_ns *ns = (struct nvme_ns *)data; + +adp_stat_read_all (ns, stat); +for (i =0 ; i <NVME_NUM_STAT_GROUPS ; i++) { +seq_printf (m,"%u %llu %llu %llu " , + stat[i].weight, stat[i].sel, + stat[i].nr_samples, stat[i].nr_ignored); + } +return 0 ; +}#endif static const struct nvme_debugfs_attr nvme_mpath_debugfs_attrs[] = {
@@ -190,11 +299,15 @@ static const struct nvme_debugfs_attr nvme_mpath_debugfs_attrs[] = { nvme_adp_ewma_shift_store}, {"adaptive_weight_timeout", 0600, nvme_adp_weight_timeout_show, nvme_adp_weight_timeout_store}, + {"adaptive_stat", 0400, .seq_ops = &nvme_mpath_adp_stat_seq_ops}, #endif {}, };static const struct nvme_debugfs_attr nvme_ns_debugfs_attrs[] = { +#ifdef CONFIG_NVME_MULTIPATH + {"adaptive_stat" ,0400 , nvme_ns_adp_stat_show}, +#endif {}, };
Signature · 1 line
2.51.0
Hi Nilay:
Could you plz update Documentation/admin-guide/nvme-multipath.rst too?
Best Regards,
Guixin Liu
Quoted 114 lines · Nilay Shroff
Hi,
This series introduces a new adaptive I/O policy for NVMe native multipath. Existing policies such as numa, round-robin, and queue-depth are static and do not adapt to real-time transport performance. The numa selects the path closest to the NUMA node of the current CPU, optimizing memory and path locality, but ignores actual path performance. The round-robin distributes I/O evenly across all paths, providing fairness but not performance awareness. The queue-depth reacts to instantaneous queue occupancy, avoiding heavily loaded paths, but does not account for actual latency, throughput, or link speed.
The new adaptive policy addresses these gaps selecting paths dynamically based on measured I/O latency for both PCIe and fabrics. Latency is derived by passively sampling I/O completions. Each path is assigned a weight proportional to its latency score, and I/Os are then forwarded accordingly. As condition changes (e.g. latency spikes, bandwidth differences), path weights are updated, automatically steering traffic toward better-performing paths.
Early results show reduced tail latency under mixed workloads and improved throughput by exploiting higher-speed links more effectively. For example, with NVMf/TCP using two paths (one throttled with ~30 ms delay), fio results with random read/write/rw workloads (direct I/O) showed:
numa round-robin queue-depth adaptive ----------- ----------- ----------- --------- READ: 50.0 MiB/s 105 MiB/s 230 MiB/s 350 MiB/s WRITE: 65.9 MiB/s 125 MiB/s 385 MiB/s 446 MiB/s RW: R:30.6 MiB/s R:56.5 MiB/s R:122 MiB/s R:175 MiB/s W:30.7 MiB/s W:56.5 MiB/s W:122 MiB/s W:175 MiB/sThis pathcset includes totla 6 patches:
[PATCH 1/6] block: expose blk_stat_{enable,disable}_accounting()
- Makeblk_statAPIs available to block drivers.
- Needed for per-path latency measurement in adaptive policy.[PATCH 2/6] nvme-multipath: add adaptive I/O policy
- Implement path scoring based on latency (EWMA).
- Distribute I/O proportionally to per-path weights.[PATCH 3/6] nvme: add generic debugfs support
- Introduce generic debugfs support for NVMe module[PATCH 4/6] nvme-multipath: add debugfs attribute
adaptive_ewma_shift
- Adds a debugfs attribute to control ewma shift[PATCH 5/6] nvme-multipath: add debugfs attribute
adaptive_weight_timeout
- Adds a debugfs attribute to control path weight calculation timeout[PATCH 6/6] nvme-multipath: add debugfs attribute
adaptive_stat
- Add “adaptive_stat” under per-path and head debugfs directories to expose adaptive policy state and statistics.As ususal, feedback and suggestions are most welcome!
Thanks!
Changes from v3:
- Update the adaptive APIs name (which actually enable/disable adaptive policy) to reflect the actual work it does. Also removed the misleading use of "current_path" from the adaptive policy code (Hannes Reinecke)
- Moveadaptive_ewma_shiftandadaptive_weight_timeoutattributes from sysfs to debugfs (Hannes Reinecke)
Link to v3: https://lore.kernel.org/all/[email protected]/Changes from v2:
- Addede a new patch to allow user to configure EWMA shift through sysfs (Hannes Reinecke)
- Added a new patch to allow user to configure path weight calculation timeout (Hannes Reinecke)
- Distinguish between read/write and other commands (e.g. admin comamnd) and calculate path weight for other commands which is separate from read/write weight. (Hannes Reinecke)
- Normalize per-path weight in the range from 0-128 insteadof 0-100 (Hannes Reinecke)- Restructure and optimize adaptive I/O forwarding code to use
one loop instead of two (Hannes Reinecke)
Link to v2: https://lore.kernel.org/all/[email protected]/Changes from v1:
- Ensure that the completion of I/O occurs on the same CPU as the submitting I/O CPU (Hannes Reinecke)
- Remove adapter link speed from the path weight calculation (Hannes Reinecke)
- Add adaptive I/O stat under debugfs instead of current sysfs (Hannes Reinecke)
- Move path weight calculation to a workqueue from IO completion code path
Link to v1: https://lore.kernel.org/all/[email protected]/Nilay Shroff (6):
block: expose blk_stat_{enable,disable}_accounting() to drivers
nvme-multipath: add support for adaptive I/O policy
nvme: add generic debugfs support
nvme-multipath: add debugfs attributeadaptive_ewma_shift
nvme-multipath: add debugfs attributeadaptive_weight_timeout
nvme-multipath: add debugfs attributeadaptive_statblock/blk-stat.h | 4 - drivers/nvme/host/Makefile | 2 +- drivers/nvme/host/core.c | 22 +- drivers/nvme/host/debugfs.c | 335 ++++++++++++++++++++++++++ drivers/nvme/host/ioctl.c | 31 ++- drivers/nvme/host/multipath.c | 430 +++++++++++++++++++++++++++++++++- drivers/nvme/host/nvme.h | 87 ++++++- drivers/nvme/host/pr.c | 6 +- drivers/nvme/host/sysfs.c | 2 +- include/linux/blk-mq.h | 4 + 10 files changed, 895 insertions(+), 28 deletions(-)create mode 100644
drivers/nvme/host/debugfs.c
Quoted 4 lines · Guixin Liu
Hi Nilay:
Could you plz update
Documentation/admin-guide/nvme-multipath.rsttoo?
Thanks for the pointer! I'll update the documentation and add one more patch in the existing patchset.
--Nilay