Skip to contentSkip to conversation

nvme-multipath: requeue work fixes

PATCH Applied Qiliang Yuan 3 participants 6 messages Nov 20 NVMe

lore
Share & tools
lore
#0 lore raw

v6 cover nvme-multipath: requeue work fixes

Patch 1 keeps a large delayed_removal_secs from wrapping around into a short delay when it is converted to jiffies.

Patch 2 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 usable path right away instead of when the old delay expires.

Patch 3 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.

---
V5 -> V6:
- Add patch 1: compute the delayed head removal in 64 bits and cap it at MAX_JIFFY_OFFSET, so that a large delayed_removal_secs doesn't wrap around (Sashiko)
- Patch 2: kick requeue_work when delayed_removal_secs is cleared, so that queued bios also fail when paths remain but none is usable, and use the capped delay of patch 1 (Sashiko)
- Patch 2: keep Nilay's Reviewed-by, as only these two changes are new
- Update the numbers of patch 2 from a new run

V4 -> V5:
- Cancel the pending head removal under subsys->lock in nvme_init_ns_head() and drop the list_empty() check and module_put() from delayed_removal_secs_store() (Nilay Shroff)
- Rebase onto v7.4-rc3-230-g4405c18c95d2 and update the numbers of patch 1 from a new run

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

v5: https://lore.kernel.org/r/20261119-bug-nvme-mpath-set-live-srcu-v5-0-e1483e2e447f@linux.dev
v4: https://lore.kernel.org/r/20261118-bug-nvme-mpath-set-live-srcu-v4-0-21f368376c8c@linux.dev
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 (3):
nvme-multipath: keep a large delayed_removal_secs from wrapping around
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/core.c      |  7 +++++--
 drivers/nvme/host/multipath.c | 37 +++++++++++++++++++++++++++++++++++--
 drivers/nvme/host/nvme.h      |  1 +
 3 files changed, 41 insertions(+), 4 deletions(-)

---
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
change-id: 20261110-bug-nvme-mpath-set-live-srcu-cbd8965f1457

Best regards,

Signature · 1 line
Qiliang Yuan <[email protected]>
#1 lore raw

PATCH v6 1/3 nvme-multipath: keep a large delayed_removal_secs from wrapping around

nvme_mpath_remove_disk() delays the removal of a namespace head that lost its last path by delayed_removal_secs, which it converts to jiffies as delayed_removal_secs * HZ.

delayed_removal_secs is an unsigned int, so the product is computed in 32 bits and wraps around for values above UINT_MAX / HZ. With HZ=1000, 4294968 seconds turns into a delay of 704 jiffies, and the head goes away in under a second instead of after about 50 days.

Compute the delay in 64 bits and cap it at MAX_JIFFY_OFFSET.

With delayed_removal_secs set to 4294968 at HZ=1000, removing the last path deletes the head after (mean of two runs, min-max in parentheses; "> 10000" means it was still there 10 s later):

before (ms) after (ms)
858 (856-860) > 10000

Fixes 62188639ec160 ("nvme-multipath: introduce delayed removal of the multipath head node")
---
 drivers/nvme/host/multipath.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/nvme/host/multipath.c +10 -1
index ae8059366b57d..9958d08cd3829 100644
@@ -724,6 +724,15 @@ static void nvme_requeue_work(struct work_struct *work)
 	}
 }
 
+/*
+ * Convert delayed_removal_secs to jiffies, capped at MAX_JIFFY_OFFSET so
+ * that a large value doesn't wrap around into a short delay.
+ */
+static unsigned long nvme_delayed_removal_jiffies(unsigned int secs)
+{
+	return min_t(u64, (u64)secs * HZ, MAX_JIFFY_OFFSET);
+}
+
 static void nvme_remove_head(struct nvme_ns_head *head)
 {
 	if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
@@ -1523,7 +1532,7 @@ void nvme_mpath_remove_disk(struct nvme_ns_head *head)
 	 */
 	if (head->delayed_removal_secs && try_module_get(THIS_MODULE)) {
 		mod_delayed_work(nvme_wq, &head->remove_work,
-				head->delayed_removal_secs * HZ);
+			nvme_delayed_removal_jiffies(head->delayed_removal_secs));
 	} else {
 		list_del_init(&head->entry);
 		remove = true;
Signature · 1 line
2.43.0
Reviewed-by on v6 1/3
#2 lore raw

Re: [PATCH v6 1/3] nvme-multipath: keep a large delayed_removal_secs from wrapping around

Quoted 15 lines · Qiliang Yuan

nvme_mpath_remove_disk() delays the removal of a namespace head that lost its last path by delayed_removal_secs, which it converts to jiffies as delayed_removal_secs * HZ.

delayed_removal_secs is an unsigned int, so the product is computed in 32 bits and wraps around for values above UINT_MAX / HZ. With HZ=1000, 4294968 seconds turns into a delay of 704 jiffies, and the head goes away in under a second instead of after about 50 days.

Compute the delay in 64 bits and cap it at MAX_JIFFY_OFFSET.

With delayed_removal_secs set to 4294968 at HZ=1000, removing the last path deletes the head after (mean of two runs, min-max in parentheses; "> 10000" means it was still there 10 s later):

Quoted 5 lines

before (ms) after (ms)
858 (856-860) > 10000

Fixes 62188639ec160 ("nvme-multipath: introduce delayed removal of the multipath head node")

Looks good to me:

#3 lore raw

PATCH v6 2/3 nvme-multipath: apply delayed_removal_secs changes to a pending removal

In reply to Qiliang Yuan

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. When paths remain but none of them is usable, for example because they are ANA inaccessible, nothing resubmits the queued bios after the attribute is cleared at all.

Requeue a pending remove_work with the new delay when the attribute is written, cancelling it first so that the module reference taken for it carries over. Have nvme_init_ns_head() cancel the pending removal before it drops subsys->lock rather than after, so that a write to the attribute cannot requeue the removal once a path is back. When the attribute is cleared, kick requeue_work, so that the queued bios fail right away if there is still no usable path.

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    26311 (26103-26518)      8.5 (8-9)            -99.97%
  30 -> 5    28281 (28037-28525)     5293 (5198-5388)      -81%
  10 -> 30    8110 (8097-8122)      30094 (30057-30130)    +271%

A direct read queued before the 30 -> 0 write fails this long after the write, with no path left and with the only path ANA inaccessible ("> 15000" means it was still queued 15 s later):

  paths         before (ms)            after (ms)   change
  none          26261 (26071-26451)    4.5 (4-5)    -99.98%
  inaccessible  > 15000                4.5 (4-5)    < -99.97%
Fixes 62188639ec160 ("nvme-multipath: introduce delayed removal of the multipath head node")
---
 drivers/nvme/host/core.c      |  7 +++++--
 drivers/nvme/host/multipath.c | 11 +++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +5 -2
index f4aa02869b790..48d432c5d7aeb 100644
@@ -4231,12 +4231,15 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 
 	list_add_tail_rcu(&ns->siblings, &head->list);
 	ns->head = head;
-	mutex_unlock(&ctrl->subsys->lock);
-
 #ifdef CONFIG_NVME_MULTIPATH
+	/*
+	 * Cancel a pending head removal under subsys->lock, so that
+	 * delayed_removal_secs_store() never requeues it once a path is back.
+	 */
 	if (cancel_delayed_work(&head->remove_work))
 		module_put(THIS_MODULE);
 #endif
+	mutex_unlock(&ctrl->subsys->lock);
 	return 0;
 
 out_put_ns_head:
drivers/nvme/host/multipath.c +11 -0
index 9958d08cd3829..df96933f49459 100644
@@ -1251,6 +1251,13 @@ 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. The module
+	 * reference taken for it carries over to the requeued work.
+	 */
+	if (cancel_delayed_work(&head->remove_work))
+		queue_delayed_work(nvme_wq, &head->remove_work,
+				   nvme_delayed_removal_jiffies(sec));
 	mutex_unlock(&head->subsys->lock);
 	/*
 	 * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen
@@ -1258,6 +1265,10 @@ static ssize_t delayed_removal_secs_store(struct device *dev,
 	 */
 	synchronize_srcu(&head->srcu);
 
+	/* Resubmit queued bios, so that they fail without a usable path */
+	if (!sec)
+		kblockd_schedule_work(&head->requeue_work);
+
 	return count;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v6 3/3 nvme-multipath: don't wait for a grace period in nvme_mpath_set_live()

In reply to Qiliang Yuan

When 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
index df96933f49459..ad9b3746ddba1 100644
@@ -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);
@@ -776,6 +781,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);
 
@@ -858,9 +864,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
index 8833a463f1c84..3fff9b394b1cd 100644
@@ -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
maintainer Applied
#5 lore raw
In reply to Qiliang Yuan
Quoted 8 lines · Qiliang Yuan

Patch 1 keeps a large delayed_removal_secs from wrapping around into a short delay when it is converted to jiffies.

Patch 2 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 usable path right away instead of when the old delay expires.

[...]

Thanks, applied to nvme-7.4.

[1/3] nvme-multipath: keep a large delayed_removal_secs from wrapping around

      commit: a280772ee0a8a1f2ac769be4d88a5c52ef5ed743
[2/3] nvme-multipath: apply delayed_removal_secs changes to a pending removal
      commit: 536a72ea54f81ebdf56a2a0b31d7dc0fb2837c1d
[3/3] nvme-multipath: don't wait for a grace period in nvme_mpath_set_live()
      commit: 3ca1ccf527aae210ecee2905062dd6da0934416a

--
Keith Busch <[email protected]>