nvme-multipath: requeue work fixes
v4 cover nvme-multipath: requeue work fixes
Patch 1 applies a new delayed_removal_secs to a head removal that is already pending, so that clearing it fails the I/O queued without a path right away instead of when the old delay expires.
Patch 2 stops nvme_mpath_set_live() from waiting for an SRCU grace period per namespace, which makes an ANA change with many namespaces take tens to hundreds of milliseconds.
---
V3 -> V4:
- Don't requeue the head removal in patch 1 if a path came back before nvme_init_ns_head() cancelled it, drop the module reference it held instead, as Nilay asked (race reported by Sashiko)
- Rebase onto v7.4-rc3-178-g20eef29b7c84 and update the numbers of patch 1 from a new run
V2 -> V3:
- Requeue a pending head removal with the new delayed_removal_secs instead of only kicking the requeue work when it is cleared, as Nilay suggested
- Rebase onto v7.4-rc2-470-g7e1392a42ea2 and update the numbers from a new run
- Don't carry Hannes's Reviewed-by on the v2 patch 1, which is rewritten
V1 -> V2:
- Add patch 1
- Take the SRCU cookie under head->lock in patch 2 (Sashiko)
- Rerun the tests
v3: https://lore.kernel.org/r/20261114-bug-nvme-mpath-set-live-srcu-v3-0-d84651de6334@linux.dev
v2: https://lore.kernel.org/r/20261111-bug-nvme-mpath-set-live-srcu-v2-0-fc772914d293@linux.dev
v1: https://lore.kernel.org/r/20261110-bug-nvme-mpath-set-live-srcu-v1-1-1f58b405b0f8@linux.dev
---
Qiliang Yuan (2):
nvme-multipath: apply delayed_removal_secs changes to a pending removal
nvme-multipath: don't wait for a grace period in nvme_mpath_set_live()
drivers/nvme/host/multipath.c | 26 +++++++++++++++++++++++++- drivers/nvme/host/nvme.h | 1 + 2 files changed, 26 insertions(+), 1 deletion(-)
---
base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
change-id: 20261110-bug-nvme-mpath-set-live-srcu-cbd8965f1457
Best regards,
Signature · 1 line
Qiliang Yuan <[email protected]>
PATCH v4 1/2 nvme-multipath: apply delayed_removal_secs changes to a pending removal
With delayed_removal_secs set, removing the last path of a namespace head doesn't delete the head right away. nvme_mpath_remove_disk() queues remove_work with the configured delay, and bios submitted in the meantime wait on head->requeue_list in case a path comes back.
A write to delayed_removal_secs only takes effect for removals that start later. A removal that is already pending keeps the old delay, so clearing the attribute leaves the queued bios waiting until that delay expires instead of failing them, and lowering or raising it doesn't move the pending removal either.
Requeue a pending remove_work with the new delay when the attribute is written. Cancel it first so that the module reference taken for it carries over, as nvme_init_ns_head() does when a path comes back. If a path is already back and nvme_init_ns_head() hasn't cancelled the work yet, drop that reference instead of requeueing the work.
With delayed_removal_secs set and no path left, the attribute is changed two seconds later and the head is removed after (mean of two runs, min-max in parentheses):
delay (s) before (ms) after (ms) change 30 -> 0 26877 (26327-27426) 9 (8-10) -99.97% 30 -> 5 27987 (27464-28509) 5248 (5193-5302) -81% 10 -> 30 8152 (8095-8209) 30106 (30080-30132) +269%
A direct read queued before the 30 -> 0 write fails after:
before (ms) after (ms) change 26801 (26243-27358) 6 (4-8) -99.98%
--- drivers/nvme/host/multipath.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/nvme/host/multipath.c +11 -0
@@ -1242,6 +1242,17 @@ static ssize_t delayed_removal_secs_store(struct device *dev,mutex_lock (&head->subsys->lock); head->delayed_removal_secs = sec;assign_bit (NVME_NSHEAD_QUEUE_IF_NO_PATH , &head->flags, sec); +/* +* Apply the new delay to a removal that is still pending. If a path +* came back and nvme_init_ns_head() hasn't cancelled the removal yet, +* cancel it here and drop the module reference it held. +*/ +if (cancel_delayed_work (&head->remove_work)) { +if (list_empty (&head->list)) +queue_delayed_work (nvme_wq, &head->remove_work, sec *HZ ); +else +module_put (THIS_MODULE ); + }mutex_unlock (&head->subsys->lock);/* * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen
Signature · 1 line
2.43.0
Re: [PATCH v4 1/2] nvme-multipath: apply delayed_removal_secs changes to a pending removal
Quoted 51 lines · Qiliang Yuan
With
delayed_removal_secsset, removing the last path of a namespace head doesn't delete the head right away.nvme_mpath_remove_disk()queuesremove_workwith the configured delay, and bios submitted in the meantime wait onhead->requeue_listin case a path comes back.A write to
delayed_removal_secsonly takes effect for removals that start later. A removal that is already pending keeps the old delay, so clearing the attribute leaves the queued bios waiting until that delay expires instead of failing them, and lowering or raising it doesn't move the pending removal either.Requeue a pending
remove_workwith the new delay when the attribute is written. Cancel it first so that the module reference taken for it carries over, asnvme_init_ns_head()does when a path comes back. If a path is already back andnvme_init_ns_head()hasn't cancelled the work yet, drop that reference instead of requeueing the work.With
delayed_removal_secsset and no path left, the attribute is changed two seconds later and the head is removed after (mean of two runs, min-max in parentheses):delay (s) before (ms) after (ms) change 30 -> 0 26877 (26327-27426) 9 (8-10) -99.97% 30 -> 5 27987 (27464-28509) 5248 (5193-5302) -81% 10 -> 30 8152 (8095-8209) 30106 (30080-30132) +269%A direct read queued before the 30 -> 0 write fails after:
before (ms) after (ms) change 26801 (26243-27358) 6 (4-8) -99.98%Fixes 62188639ec160 ("nvme-multipath: introduce delayed removal of the multipath head node")Suggested-by Nilay Shroff <[email protected]>Signed-off-by Qiliang Yuan <[email protected]>--- drivers/nvme/host/multipath.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/nvme/host/multipath.c +4
-0@@ -1242,6 +1242,17 @@ static ssize_t delayed_removal_secs_store(struct device *dev,mutex_lock (&head->subsys->lock); head->delayed_removal_secs = sec;assign_bit (NVME_NSHEAD_QUEUE_IF_NO_PATH , &head->flags, sec); +/* +* Apply the new delay to a removal that is still pending. If a path +* came back and nvme_init_ns_head() hasn't cancelled the removal yet, +* cancel it here and drop the module reference it held.
drivers/nvme/host/multipath.c static ssize_t delayed_removal_secs_store(struct device *dev, · line 1258
Quoted 10 lines
drivers/nvme/host/multipath.c +7
-0+*/ +if (cancel_delayed_work (&head->remove_work)) { +if (list_empty (&head->list)) +queue_delayed_work (nvme_wq, &head->remove_work, sec *HZ ); +else +module_put (THIS_MODULE ); + }mutex_unlock (&head->subsys->lock);/* * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen
The above change works and fixes the race which sashiko reported. However, I think moving the cancel_delayed_work() check in nvme_init_ns_head() under head->subsys->lock is more intuitive.
With that change, delayed_removal_secs_store() can always requeue successfully cancelled work:
if (cancel_delayed_work(&head->remove_work))
queue_delayed_work(nvme_wq, &head->remove_work, sec * HZ);
And nvme_init_ns_head() would then become:
mutex_lock(&ctrl->subsys->lock);
...
...list_add_tail_rcu(&ns->siblings, &head->list);ns->head = head;
#ifdef CONFIG_NVME_MULTIPATH
if (cancel_delayed_work(&head->remove_work))
module_put(THIS_MODULE);
#endifmutex_unlock(&ctrl->subsys->lock);
This way, path addition and delayed-work cancellation are serialized with updates to delayed_removal_secs_store() through head->subsys->lock. Consequently, we can eliminate the list_empty() check and the module_put() from delayed_removal_secs_store().
Thanks,
--Nilay
PATCH v4 2/2 nvme-multipath: don't wait for a grace period in nvme_mpath_set_live()
In reply to Qiliang YuanWhen a path goes live, nvme_mpath_set_live() waits for an SRCU grace period of the namespace head before kicking the requeue work, so that bios which found no usable path are on the requeue list by the time it runs.
The ANA work calls nvme_mpath_set_live() for every namespace that becomes optimized or non-optimized, one after another, so an ANA change waits for one grace period per namespace.
Start the grace period with start_poll_synchronize_srcu() instead and have nvme_requeue_work() wait for it only if it has not completed yet. Take the cookie under head->lock, so that concurrent callers store their cookies in order and the requeue work never sees an older one. The grace periods of all namespaces then run in parallel, and requeue work kicked for other reasons finds the cookie completed and does not wait.
Flipping an ANA group from inaccessible to optimized ten times on each of two boots over nvme-tcp loopback, nvme_read_ana_log() takes (median, min-max in parentheses):
namespaces before (ms) after (ms) change
256 52.0 (35.4-74.7) 8.7 (2.5-19.6) -83%
1024 154.3 (123.9-193.8) 43.1 (18.2-58.3) -72%--- drivers/nvme/host/multipath.c | 15 ++++++++++++++- drivers/nvme/host/nvme.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/nvme/host/multipath.c +14 -1
@@ -712,6 +712,11 @@ static void nvme_requeue_work(struct work_struct *work) container_of(work, struct nvme_ns_head, requeue_work);struct bio *bio, *next; +/* Let bios that found no usable path reach the requeue list first. */ +if (!poll_state_synchronize_srcu (&head->srcu, +READ_ONCE (head->requeue_srcu_cookie))) +synchronize_srcu (&head->srcu); +spin_lock_irq (&head->requeue_lock); next =bio_list_get (&head->requeue_list);spin_unlock_irq (&head->requeue_lock);
@@ -767,6 +772,7 @@ int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)mutex_init (&head->lock);spin_lock_init (&head->requeue_lock);INIT_WORK (&head->requeue_work, nvme_requeue_work); + head->requeue_srcu_cookie =get_completed_synchronize_srcu ();INIT_WORK (&head->partition_scan_work, nvme_partition_scan_work);INIT_DELAYED_WORK (&head->remove_work, nvme_remove_head_work);
@@ -849,9 +855,16 @@ static void nvme_mpath_set_live(struct nvme_ns *ns)__nvme_find_path (head, node);srcu_read_unlock (&head->srcu, srcu_idx); } +/* +* Have nvme_requeue_work() wait for the grace period instead of +* blocking here, so that namespaces going live together, e.g. on +* an ANA change, do not wait for one grace period each. Start it +* under head->lock so that the newest cookie is stored last. +*/ +WRITE_ONCE (head->requeue_srcu_cookie, +start_poll_synchronize_srcu (&head->srcu));mutex_unlock (&head->lock); -synchronize_srcu (&head->srcu);nvme_mpath_revalidate_zones (head);kblockd_schedule_work (&head->requeue_work); }
drivers/nvme/host/nvme.h +1 -0
@@ -587,6 +587,7 @@ struct nvme_ns_head {__guarded_by (&requeue_lock);spinlock_t requeue_lock;struct work_struct requeue_work; +unsigned long requeue_srcu_cookie;struct work_struct partition_scan_work;struct mutex lock;unsigned long flags;
Signature · 1 line
2.43.0