Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-multipath: flush every path that completed writes
@ 2026-11-30 21:14 Keith Busch
  2026-11-30 21:38 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Keith Busch @ 2026-11-30 21:14 UTC (permalink / raw)
  To: linux-nvme, hch; +Cc: erwanaliasr1, Keith Busch

From: Keith Busch <kbusch@kernel.org>

The nvme specification says a flush commits data only for commands
completed by the controller, but the multipath head sends the flush down
a single path. Writes completed through another controller path are not
made durable.

Track which paths completed writes, and send a flush to each of them
along with the original.

Fixes: 32acab3181c7 ("nvme: implement multipath access to nvme subsystems")
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
 drivers/nvme/host/core.c      |  7 ++++++
 drivers/nvme/host/multipath.c | 40 +++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h      | 10 +++++++++
 3 files changed, 57 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index d602425e05295..25061a164f026 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -5628,8 +5628,14 @@ static int __init nvme_core_init(void)
 	result = nvme_init_auth();
 	if (result)
 		goto destroy_ns_chr;
+
+	result = nvme_mpath_init();
+	if (result)
+		goto exit_auth;
 	return 0;
 
+exit_auth:
+	nvme_exit_auth();
 destroy_ns_chr:
 	class_unregister(&nvme_ns_chr_class);
 unregister_generic_ns:
@@ -5652,6 +5658,7 @@ static int __init nvme_core_init(void)
 
 static void __exit nvme_core_exit(void)
 {
+	nvme_mpath_exit();
 	nvme_exit_auth();
 	class_unregister(&nvme_ns_chr_class);
 	class_unregister(&nvme_subsys_class);
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index bd27a5355bab3..39531d8422293 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -203,6 +203,10 @@ void nvme_mpath_end_request(struct request *rq)
 {
 	struct nvme_ns *ns = rq->q->queuedata;
 
+	/* a Flush only covers writes completed by the controller it's sent to */
+	if (op_is_write(req_op(rq)) && !test_bit(NVME_NS_WRITTEN, &ns->flags))
+		set_bit(NVME_NS_WRITTEN, &ns->flags);
+
 	if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)
 		atomic_dec_if_positive(&ns->ctrl->nr_active);
 
@@ -563,6 +567,30 @@ static bool nvme_available_path(struct nvme_ns_head *head)
 	return nvme_mpath_queue_if_no_path(head);
 }
 
+static struct bio_set nvme_mpath_flush_bs;
+
+static void nvme_mpath_flush_paths(struct nvme_ns_head *head,
+		struct nvme_ns *ns, struct bio *bio)
+	__must_hold_shared(&head->srcu)
+{
+	struct nvme_ns *sib;
+
+	list_for_each_entry_srcu(sib, &head->list, siblings,
+				 srcu_read_lock_held(&head->srcu)) {
+		struct bio *flush;
+
+		if (sib == ns || !test_bit(NVME_NS_WRITTEN, &sib->flags) ||
+		    !test_and_clear_bit(NVME_NS_WRITTEN, &sib->flags))
+			continue;
+
+		flush = bio_alloc_bioset(sib->disk->part0, 0,
+					 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
+					 GFP_NOIO, &nvme_mpath_flush_bs);
+		bio_chain(flush, bio);
+		submit_bio_noacct(flush);
+	}
+}
+
 static void nvme_ns_head_submit_bio(struct bio *bio)
 {
 	struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
@@ -590,6 +618,8 @@ static void nvme_ns_head_submit_bio(struct bio *bio)
 		 */
 		bio_set_flag(bio, BIO_REMAPPED);
 		bio->bi_opf |= REQ_NVME_MPATH;
+		if (bio->bi_opf & REQ_PREFLUSH)
+			nvme_mpath_flush_paths(head, ns, bio);
 		trace_block_bio_remap(bio, disk_devt(ns->head->disk),
 				      bio->bi_iter.bi_sector);
 		submit_bio_noacct(bio);
@@ -1578,6 +1608,16 @@ void nvme_mpath_put_disk(struct nvme_ns_head *head)
 	put_disk(head->disk);
 }
 
+int nvme_mpath_init(void)
+{
+	return bioset_init(&nvme_mpath_flush_bs, BIO_POOL_SIZE, 0, 0);
+}
+
+void nvme_mpath_exit(void)
+{
+	bioset_exit(&nvme_mpath_flush_bs);
+}
+
 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
 {
 	mutex_init(&ctrl->ana_lock);
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index e069b6d661201..788d987b5e289 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -655,6 +655,7 @@ struct nvme_ns {
 #define NVME_NS_READY			4
 #define NVME_NS_SYSFS_ATTR_LINK	5
 #define NVME_NS_CDEV_LIVE		6
+#define NVME_NS_WRITTEN			7
 
 	struct cdev		cdev;
 	struct device		cdev_device;
@@ -1101,6 +1102,8 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid);
 void nvme_mpath_put_disk(struct nvme_ns_head *head);
 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl);
+int nvme_mpath_init(void);
+void nvme_mpath_exit(void);
 void nvme_mpath_update(struct nvme_ctrl *ctrl);
 void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
 void nvme_mpath_stop(struct nvme_ctrl *ctrl);
@@ -1188,6 +1191,13 @@ static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
 static inline void nvme_trace_bio_complete(struct request *req)
 {
 }
+static inline int nvme_mpath_init(void)
+{
+	return 0;
+}
+static inline void nvme_mpath_exit(void)
+{
+}
 static inline void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
 {
 }
-- 
2.52.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] nvme-multipath: flush every path that completed writes
  2026-11-30 21:14 [PATCH] nvme-multipath: flush every path that completed writes Keith Busch
@ 2026-11-30 21:38 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 21:38 UTC (permalink / raw)
  To: Keith Busch, linux-nvme

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] nvme-multipath: flushing unhealthy sibling paths in
  nvme_mpath_flush_paths()
- [High] nvme-multipath: mempool deadlock without rescuer in
  nvme_mpath_init()
- [High] nvme-multipath: data write races sibling preflush in
  nvme_ns_head_submit_bio()
- [Medium] nvme-multipath: premature NVME_NS_WRITTEN clear in
  nvme_mpath_flush_paths()
- [Medium] nvme-multipath: NVME_NS_WRITTEN not cleared on ns in
  nvme_mpath_flush_paths()
- [Medium] nvme-multipath: REQ_NOWAIT ignored for flushes in
  nvme_mpath_flush_paths()
--

commit ac6211f78b685d8e139acbc7bde856f29907dd4e
Author: Keith Busch <kbusch@kernel.org>

nvme-multipath: flush every path that completed writes

This commit tracks which namespace paths have completed writes with
NVME_NS_WRITTEN and submits chained REQ_PREFLUSH bios to each written
sibling path when a REQ_PREFLUSH bio is submitted to the multipath head.

> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
> index 27facdbb3d4c0..cb12f05d9b532 100644
> --- a/drivers/nvme/host/multipath.c
> +++ b/drivers/nvme/host/multipath.c
> @@ -203,6 +203,10 @@ void nvme_mpath_end_request(struct request *rq)
>  {
>  	struct nvme_ns *ns = rq->q->queuedata;
>  
> +	/* a Flush only covers writes completed by the controller it's sent to */
> +	if (op_is_write(req_op(rq)) && !test_bit(NVME_NS_WRITTEN, &ns->flags))
> +		set_bit(NVME_NS_WRITTEN, &ns->flags);
> +
>  	if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)
>  		atomic_dec_if_positive(&ns->ctrl->nr_active);
>  
> @@ -563,6 +567,30 @@ static bool nvme_available_path(struct nvme_ns_head *head)
>  	return nvme_mpath_queue_if_no_path(head);
>  }
>  
> +static struct bio_set nvme_mpath_flush_bs;
> +
> +static void nvme_mpath_flush_paths(struct nvme_ns_head *head,
> +		struct nvme_ns *ns, struct bio *bio)
> +	__must_hold_shared(&head->srcu)
> +{
> +	struct nvme_ns *sib;
> +
> +	list_for_each_entry_srcu(sib, &head->list, siblings,
> +				 srcu_read_lock_held(&head->srcu)) {
> +		struct bio *flush;
> +
> +		if (sib == ns || !test_bit(NVME_NS_WRITTEN, &sib->flags) ||
> +		    !test_and_clear_bit(NVME_NS_WRITTEN, &sib->flags))
> +			continue;

[Severity: Medium]
Does clearing NVME_NS_WRITTEN on sib before the sibling flush bio completes
let a concurrent flush finish before sib's volatile write cache is actually
flushed?

CPU0                                CPU1
nvme_ns_head_submit_bio(bioA)
  nvme_mpath_flush_paths()
    test_and_clear_bit(..., &sib->flags)
                                    nvme_ns_head_submit_bio(bioB)
                                      nvme_mpath_flush_paths()
                                        test_bit(..., &sib->flags) == 0
                                      submit_bio_noacct(bioB) -> ns
                                      /* bioB completes on ns before
                                         flush on sib finishes */
    submit_bio_noacct(flush) -> sib

Similarly, if a write completes on sib in nvme_mpath_end_request() while
NVME_NS_WRITTEN is still set, just before nvme_mpath_flush_paths() clears it
for an already-issued flush, that write is left without NVME_NS_WRITTEN set.

[Severity: Medium]
Because sib == ns short-circuits before test_and_clear_bit(NVME_NS_WRITTEN,
&sib->flags), doesn't NVME_NS_WRITTEN remain set on the primary path ns
that is receiving the REQ_PREFLUSH bio?  If a later REQ_PREFLUSH bio
selects a different path, nvme_mpath_flush_paths() will still see
NVME_NS_WRITTEN set on ns and issue a redundant flush to it.

> +
> +		flush = bio_alloc_bioset(sib->disk->part0, 0,
> +					 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
> +					 GFP_NOIO, &nvme_mpath_flush_bs);
> +		bio_chain(flush, bio);
> +		submit_bio_noacct(flush);
> +	}
> +}

[Severity: High]
Can nvme_mpath_flush_paths() submit a chained flush bio to a sibling path
that is resetting, connecting, dead, or in an inaccessible ANA state?

Sibling namespaces remain on head->list while unavailable, and
nvme_mpath_end_request() also sets NVME_NS_WRITTEN on failed writes (including
when nvme_failover_req() zeroes nvme_req(req)->status and calls
nvme_end_req()).  Because flush is submitted without REQ_NVME_MPATH or a check
on sib's state, nvme_fail_nonready_command() returns BLK_STS_RESOURCE when
sib->ctrl is resetting or connecting (stalling the parent bio on the healthy
path ns), or nvme_decide_disposition() completes flush with an error instead
of failing over, propagating -EIO to the parent bio via bio_chain().

[Severity: Medium]
Should nvme_mpath_flush_paths() propagate REQ_NOWAIT from the parent bio and
avoid GFP_NOIO when REQ_NOWAIT is set?  The multipath head disk advertises
BLK_FEAT_NOWAIT, so allocating with GFP_NOIO or submitting flush without
REQ_NOWAIT can sleep in reclaim, mempool allocation, or sibling queue
submission for a non-blocking caller.

> @@ -590,6 +618,8 @@ static void nvme_ns_head_submit_bio(struct bio *bio)
>  		 */
>  		bio_set_flag(bio, BIO_REMAPPED);
>  		bio->bi_opf |= REQ_NVME_MPATH;
> +		if (bio->bi_opf & REQ_PREFLUSH)
> +			nvme_mpath_flush_paths(head, ns, bio);
>  		trace_block_bio_remap(bio, disk_devt(ns->head->disk),
>  				      bio->bi_iter.bi_sector);
>  		submit_bio_noacct(bio);

[Severity: High]
When bio carries data along with REQ_PREFLUSH (such as a filesystem journal
commit write with REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA), doesn't submitting
bio to ns concurrently with the sibling flushes allow bio's data write to reach
media on ns before sibling controllers finish flushing earlier writes?
bio_chain(flush, bio) only delays bio_endio(bio); blk_insert_flush() on ns does
not wait for the flush bios in flight on sibling queues before issuing bio's
data write.

[ ... ]

> @@ -1545,6 +1575,16 @@ void nvme_mpath_put_disk(struct nvme_ns_head *head)
>  	put_disk(head->disk);
>  }
>  
> +int nvme_mpath_init(void)
> +{
> +	return bioset_init(&nvme_mpath_flush_bs, BIO_POOL_SIZE, 0, 0);
> +}

[Severity: High]
Can bio_alloc_bioset() in nvme_mpath_flush_paths() deadlock when
nvme_mpath_flush_bs is initialized without BIOSET_NEED_RESCUER?

nvme_ns_head_submit_bio() runs inside __submit_bio_noacct() with
current->bio_list active, so each submit_bio_noacct(flush) in
nvme_mpath_flush_paths() queues flush onto current->bio_list[0] without
dispatching it until nvme_ns_head_submit_bio() returns.  If a namespace has
more than BIO_POOL_SIZE (2) written sibling paths (or concurrent flushes share
the global pool) under memory pressure, punt_bios_to_rescuer() does nothing
because bs->rescue_workqueue is NULL, and mempool_alloc() sleeps forever
waiting for the flush bios still sitting on current->bio_list[0] while holding
srcu_read_lock(&head->srcu).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130211438.556153-1-kbusch@meta.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 21:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 21:14 [PATCH] nvme-multipath: flush every path that completed writes Keith Busch
2026-11-30 21:38 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox