Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Keith Busch <kbusch@meta.com>
To: <linux-nvme@lists.infradead.org>, <hch@lst.de>
Cc: <erwanaliasr1@gmail.com>, Keith Busch <kbusch@kernel.org>
Subject: [PATCH] nvme-multipath: flush every path that completed writes
Date: Mon, 30 Nov 2026 13:14:38 -0800	[thread overview]
Message-ID: <20261130211438.556153-1-kbusch@meta.com> (raw)

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



                 reply	other threads:[~2026-11-30 21:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130211438.556153-1-kbusch@meta.com \
    --to=kbusch@meta.com \
    --cc=erwanaliasr1@gmail.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox