Skip to contentSkip to conversation

nvme-multipath: serialize ana_log_buf lifecycle with ana_lock

PATCH In review Guixin Liu 4 participants 4 messages Dec 4 NVMe

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH v5 1/1 nvme-multipath: serialize ana_log_buf lifecycle with ana_lock

nvme_parse_ana_log() reads ana_log_buf and ana_log_size under ana_lock, but the buffer is freed in nvme_mpath_uninit() and reallocated in nvme_mpath_init_identify() outside the lock. A concurrent parser in nvme_mpath_add_disk() can then use freed memory, dereference a NULL buffer, read a freshly allocated buffer before nvme_read_ana_log() populates it, or validate stale descriptors against shrunk limits (WARN_ON_ONCE). Once the buffer is NULL, nvme_read_ana_log() calls nvme_get_log() with size 0, where nvme_bytes_to_numd(0) underflows to 0xffffffff, and the NULL-buffer bail in nvme_parse_ana_log() leaves desc.state == 0 so the namespace is stuck NVME_NS_ANA_PENDING and never goes live.

Take ana_lock around the buffer release and reallocation, updating ana_log_buf, ana_log_size and anagrpmax together. Allocate the new buffer outside the lock and zero it, and clear ngrps on the shrink path, so an early parser sees ngrps == 0. nvme_read_ana_log() bails on a NULL buffer and nvme_parse_ana_log() returns -ENOENT in that case. nvme_mpath_add_disk() keeps the lockless nvme_ctrl_use_ana() feature test and, on -ENOENT, clears any pending bit and falls back to the non-ANA path so the namespace goes live even if ANA was disabled while it parsed. The ana_work drain stays outside the lock since ana_work itself takes ana_lock.

Fixes 0d0b660f214d ("nvme: add ANA support")

---
Originally reported by Sashiko AI review as pre-existing issues, confirmed by source analysis and reproduced in a KASAN guest.

Tested on 7.4.0-rc1 (KASAN, nvmet-tcp loopback, ANA enabled). A test-only parameter clears ana_log_buf before nvme_mpath_add_disk() takes ana_lock:
without the fix the namespace stays NVME_NS_ANA_PENDING and /dev/nvme0n2 never appears; with the fix it goes optimized and live and 4K reads pass. Normal ANA path, multipath fio (err=0) and controller reset are clean.

The ns->ana_grpid/ana_state publication stays outside ana_lock on
purpose: nvme_mpath_set_live() runs synchronous report-zones I/O for zoned namespaces, and holding ana_lock across it deadlocks scan_work against reset_work (reset takes ana_lock in nvme_mpath_init_identify() without flushing scan_work). The stale-snapshot race this leaves is pre-existing and recoverable via failover. An already-pending namespace stranded when ANA is disabled (nvme_mpath_update() returns early on a NULL buffer) is a separate pre-existing gap left for a follow-up.

Changes since v4:
- revert the under-lock nvme_ctrl_use_ana() test, keep it lockless; nvme_parse_ana_log() returns -ENOENT for a NULL buffer and add_disk falls back to the non-ANA path (clearing pending) on that error (Nilay Shroff)

Changes since v3:
- move the nvme_ctrl_use_ana() test under ana_lock so add_disk takes the non-ANA path if ANA was disabled while it waited (Sashiko)

Changes since v2:
- bail out of nvme_read_ana_log() on a NULL buffer (numd underflow); update anagrpmax under ana_lock and clear ngrps on shrink (Sashiko)

Changes since v1:
- serialize the realloc swap under ana_lock, allocate/zero outside the lock, bail out of nvme_parse_ana_log() on a NULL buffer (Sashiko)

v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 drivers/nvme/host/multipath.c | 43 ++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 8 deletions(-)
drivers/nvme/host/multipath.c +35 -8
index bd27a5355bab..e2ded689323e 100644
@@ -889,6 +889,9 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 
 	lockdep_assert_held(&ctrl->ana_lock);
 
+	if (!ctrl->ana_log_buf)
+		return -ENOENT;
+
 	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
 		struct nvme_ana_group_desc *desc = base + offset;
 		u32 nr_nsids;
@@ -1008,6 +1011,10 @@ static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
 	int error;
 
 	mutex_lock(&ctrl->ana_lock);
+	if (!ctrl->ana_log_buf) {
+		error = 0;
+		goto out_unlock;
+	}
 	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
 			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
 	if (error) {
@@ -1510,12 +1517,19 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
 			.grpid = anagrpid,
 			.state = 0,
 		};
+		int ret;
 
 		mutex_lock(&ns->ctrl->ana_lock);
 		ns->ana_grpid = le32_to_cpu(anagrpid);
-		nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
+		ret = nvme_parse_ana_log(ns->ctrl, &desc,
+					 nvme_lookup_ana_group_desc);
 		mutex_unlock(&ns->ctrl->ana_lock);
-		if (desc.state) {
+		if (ret == -ENOENT) {
+			/* ANA log buffer gone: fall back to non-ANA */
+			clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
+			ns->ana_state = NVME_ANA_OPTIMIZED;
+			nvme_mpath_set_live(ns);
+		} else if (desc.state) {
 			/* found the group desc: update */
 			nvme_update_ns_ana_state(&desc, ns);
 		} else {
@@ -1527,7 +1541,6 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
 		ns->ana_state = NVME_ANA_OPTIMIZED;
 		nvme_mpath_set_live(ns);
 	}
-
 }
 
 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
@@ -1609,7 +1622,6 @@ int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 	ctrl->anacap = id->anacap;
 	ctrl->anatt = id->anatt;
 	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
-	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
 
 	ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
 		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
@@ -1622,13 +1634,26 @@ int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 		goto out_uninit;
 	}
 	if (ana_log_size > ctrl->ana_log_size) {
+		void *buf;
+
 		nvme_mpath_stop(ctrl);
-		nvme_mpath_uninit(ctrl);
-		ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
-		if (!ctrl->ana_log_buf)
+		buf = kvzalloc(ana_log_size, GFP_KERNEL);
+		if (!buf)
 			return -ENOMEM;
+		mutex_lock(&ctrl->ana_lock);
+		kvfree(ctrl->ana_log_buf);
+		ctrl->ana_log_buf = buf;
+		ctrl->ana_log_size = ana_log_size;
+		ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
+		mutex_unlock(&ctrl->ana_lock);
+	} else {
+		mutex_lock(&ctrl->ana_lock);
+		ctrl->ana_log_size = ana_log_size;
+		ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
+		if (ctrl->ana_log_buf)
+			ctrl->ana_log_buf->ngrps = 0;
+		mutex_unlock(&ctrl->ana_lock);
 	}
-	ctrl->ana_log_size = ana_log_size;
 	error = nvme_read_ana_log(ctrl);
 	if (error)
 		goto out_uninit;
@@ -1649,7 +1674,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	cancel_work_sync(&ctrl->ana_work);
 	timer_delete_sync(&ctrl->anatt_timer);
 
+	mutex_lock(&ctrl->ana_lock);
 	kvfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
+	mutex_unlock(&ctrl->ana_lock);
 }
Signature · 1 line
2.43.7
Reviewed-by on v5 1/1
#2 lore raw
In reply to Guixin Liu
Quoted 23 lines · Guixin Liu

nvme_parse_ana_log() reads ana_log_buf and ana_log_size under ana_lock, but the buffer is freed in nvme_mpath_uninit() and reallocated in nvme_mpath_init_identify() outside the lock. A concurrent parser in nvme_mpath_add_disk() can then use freed memory, dereference a NULL buffer, read a freshly allocated buffer before nvme_read_ana_log() populates it, or validate stale descriptors against shrunk limits (WARN_ON_ONCE). Once the buffer is NULL, nvme_read_ana_log() calls nvme_get_log() with size 0, where nvme_bytes_to_numd(0) underflows to 0xffffffff, and the NULL-buffer bail in nvme_parse_ana_log() leaves desc.state == 0 so the namespace is stuck NVME_NS_ANA_PENDING and never goes live.

Take ana_lock around the buffer release and reallocation, updating ana_log_buf, ana_log_size and anagrpmax together. Allocate the new buffer outside the lock and zero it, and clear ngrps on the shrink path, so an early parser sees ngrps == 0. nvme_read_ana_log() bails on a NULL buffer and nvme_parse_ana_log() returns -ENOENT in that case. nvme_mpath_add_disk() keeps the lockless nvme_ctrl_use_ana() feature test and, on -ENOENT, clears any pending bit and falls back to the non-ANA path so the namespace goes live even if ANA was disabled while it parsed. The ana_work drain stays outside the lock since ana_work itself takes ana_lock.

Quoted 3 lines
Fixes 0d0b660f214d ("nvme: add ANA support")

Looks good to me:

Reviewed-by on v5 1/1
#3 lore raw
In reply to Guixin Liu
Quoted 59 lines · Guixin Liu

nvme_parse_ana_log() reads ana_log_buf and ana_log_size under ana_lock, but the buffer is freed in nvme_mpath_uninit() and reallocated in nvme_mpath_init_identify() outside the lock. A concurrent parser in nvme_mpath_add_disk() can then use freed memory, dereference a NULL buffer, read a freshly allocated buffer before nvme_read_ana_log() populates it, or validate stale descriptors against shrunk limits (WARN_ON_ONCE). Once the buffer is NULL, nvme_read_ana_log() calls nvme_get_log() with size 0, where nvme_bytes_to_numd(0) underflows to 0xffffffff, and the NULL-buffer bail in nvme_parse_ana_log() leaves desc.state == 0 so the namespace is stuck NVME_NS_ANA_PENDING and never goes live.

Take ana_lock around the buffer release and reallocation, updating ana_log_buf, ana_log_size and anagrpmax together. Allocate the new buffer outside the lock and zero it, and clear ngrps on the shrink path, so an early parser sees ngrps == 0. nvme_read_ana_log() bails on a NULL buffer and nvme_parse_ana_log() returns -ENOENT in that case. nvme_mpath_add_disk() keeps the lockless nvme_ctrl_use_ana() feature test and, on -ENOENT, clears any pending bit and falls back to the non-ANA path so the namespace goes live even if ANA was disabled while it parsed. The ana_work drain stays outside the lock since ana_work itself takes ana_lock.

Fixes 0d0b660f214d ("nvme: add ANA support")

---
Originally reported by Sashiko AI review as pre-existing issues, confirmed by source analysis and reproduced in a KASAN guest.

Tested on 7.4.0-rc1 (KASAN, nvmet-tcp loopback, ANA enabled). A test-only parameter clears ana_log_buf before nvme_mpath_add_disk() takes ana_lock:
without the fix the namespace stays NVME_NS_ANA_PENDING and /dev/nvme0n2 never appears; with the fix it goes optimized and live and 4K reads pass. Normal ANA path, multipath fio (err=0) and controller reset are clean.

The ns->ana_grpid/ana_state publication stays outside ana_lock on
purpose: nvme_mpath_set_live() runs synchronous report-zones I/O for zoned namespaces, and holding ana_lock across it deadlocks scan_work against reset_work (reset takes ana_lock in nvme_mpath_init_identify() without flushing scan_work). The stale-snapshot race this leaves is pre-existing and recoverable via failover. An already-pending namespace stranded when ANA is disabled (nvme_mpath_update() returns early on a NULL buffer) is a separate pre-existing gap left for a follow-up.

Changes since v4:
- revert the under-lock nvme_ctrl_use_ana() test, keep it lockless; nvme_parse_ana_log() returns -ENOENT for a NULL buffer and add_disk falls back to the non-ANA path (clearing pending) on that error (Nilay Shroff)

Changes since v3:
- move the nvme_ctrl_use_ana() test under ana_lock so add_disk takes the non-ANA path if ANA was disabled while it waited (Sashiko)

Changes since v2:
- bail out of nvme_read_ana_log() on a NULL buffer (numd underflow); update anagrpmax under ana_lock and clear ngrps on shrink (Sashiko)

Quoted 9 lines

Changes since v1:
- serialize the realloc swap under ana_lock, allocate/zero outside the lock, bail out of nvme_parse_ana_log() on a NULL buffer (Sashiko)

v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich