Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme: Add module reference counting for multipath nvme device
@ 2026-08-10 23:50 wenxiong
  2026-08-11 13:18 ` John Garry
  2026-08-11 19:12 ` Keith Busch
  0 siblings, 2 replies; 9+ messages in thread
From: wenxiong @ 2026-08-10 23:50 UTC (permalink / raw)
  To: linux-nvme, kbusch; +Cc: gjoyce, wenxiong, Wen Xiong

From: Wen Xiong <wenxiong@linux.ibm.com>

Ensure proper module reference counting for NVMe multipath head devices
on open, preventing the controller module from being unloaded while
the multipath head device is still open or in use.

This patch acquires a module reference in nvme_ns_head_open() and
releases it in nvme_ns_head_release()

Signed-off-by: Wen Xiong <wenxiong@linux.ibm.com>
---
 drivers/nvme/host/multipath.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 9b9a657fa330..74c3710bf5f7 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -554,14 +554,39 @@ static void nvme_ns_head_submit_bio(struct bio *bio)
 
 static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode)
 {
-	if (!nvme_tryget_ns_head(disk->private_data))
+	struct nvme_ns_head *head = disk->private_data;
+	struct nvme_ns *ns;
+	int srcu_idx;
+
+	if (!nvme_tryget_ns_head(head))
 		return -ENXIO;
+
+	/* Get module reference from any available path */
+	srcu_idx = srcu_read_lock(&head->srcu);
+	ns = nvme_find_path(head);
+	if (ns && !try_module_get(ns->ctrl->ops->module)) {
+		srcu_read_unlock(&head->srcu, srcu_idx);
+		nvme_put_ns_head(head);
+		return -ENXIO;
+	}
+	srcu_read_unlock(&head->srcu, srcu_idx);
+
 	return 0;
 }
 
 static void nvme_ns_head_release(struct gendisk *disk)
 {
-	nvme_put_ns_head(disk->private_data);
+	struct nvme_ns_head *head = disk->private_data;
+	struct nvme_ns *ns;
+	int srcu_idx;
+
+	srcu_idx = srcu_read_lock(&head->srcu);
+	ns = nvme_find_path(head);
+	if (ns)
+		module_put(ns->ctrl->ops->module);
+	srcu_read_unlock(&head->srcu, srcu_idx);
+
+	nvme_put_ns_head(head);
 }
 
 static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16],
-- 
2.52.0



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

end of thread, other threads:[~2026-08-12 14:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-10 23:50 [PATCH] nvme: Add module reference counting for multipath nvme device wenxiong
2026-08-11 13:18 ` John Garry
2026-08-11 18:54   ` Wen Xiong
2026-08-12  8:43     ` John Garry
2026-08-12 14:32       ` Wen Xiong
2026-08-11 19:12 ` Keith Busch
2026-08-11 20:33   ` Wen Xiong
2026-08-11 20:49     ` Keith Busch
2026-08-12  3:59       ` Wen Xiong

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