Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BA6A606A696 for ; Wed, 2 Dec 2026 08:51:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=IJHRl6DS; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=yb9ne/m4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Unsubscribe-Post:List-Owner; bh=wVGlho/cbj9wAXD2u8RKh757jb45C+JTd5rJRhDBLS0=; b=IJHRl6DSdC17KdgHaBli6AgMfP aOmd4kFNZUyUR/IHQ4Ak9A3ISP+xgviLHMjr63gGjEMuVnr7JdTMwzZFID3FWMBhqyXxOByvC3SL+ +7J9SONcwpIv4nYq2SDk3uR2Bm3Ahk3lQ98zmcqWYpqrvCe0n0qqztTKQMAmjzFoVMXpEYQRM1JZF CsuIPKj1bgUukQU1N6tMbYHl0NwlqrRGHuROCP++LWahEqHQSNUqDEbMoXmwJgMy7DE3vVyWAym56 oKOW5CcG7jsKnUXf88ZB7QVqSUnDB1pny9gtevgQTc5kMagv8Hy8cPgO6XSHxhuOsjc+OwUqgBngZ ugaTCFsg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYg3v-0000000AiVF-0l9v; Wed, 02 Dec 2026 08:51:39 +0000 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYg3r-0000000AiPq-21Xm for linux-nvme@lists.infradead.org; Wed, 02 Dec 2026 08:51:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796201489; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=wVGlho/cbj9wAXD2u8RKh757jb45C+JTd5rJRhDBLS0=; b=yb9ne/m4LU8YVsBd3Wb7lT8C7BqAkpxxrbOqc7N4pL+BHJfRTVkoz+h6Ett/sN4MRT2dBd54yNkm6OFrkL0QpkRz3VkrdHdKl8vNNTpsHyQHp+wasgslxTHJoeOL7iRaqepASXxdKXUS2f0sTwPHYGQUo/y/EZEe3viMWuAhHjY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R441e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037009110;MF=kanie@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XG8XKfT_1796201488; Received: from localhost(mailfrom:kanie@linux.alibaba.com fp:SMTPD_---0XG8XKfT_1796201488 cluster:ay36) by smtp.aliyun-inc.com; Wed, 02 Dec 2026 16:51:29 +0800 From: Guixin Liu To: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Nilay Shroff , Daniel Wagner , Hannes Reinecke Cc: linux-nvme@lists.infradead.org Subject: [PATCH v4] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock Date: Wed, 2 Dec 2026 16:51:24 +0800 Message-ID: <20261202085124.1252825-1-kanie@linux.alibaba.com> X-Mailer: git-send-email 2.43.7 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261202_005135_737817_C380DBE9 X-CRM114-Status: GOOD ( 23.86 ) X-Spam-Score: -17.6 (-----------------) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 nvme_parse_ana_log() reads ctrl->ana_log_buf and ctrl->ana_log_size under ctrl->ana_lock, but the buffer lifecycle is not fully serialized against it: - nvme_mpath_uninit() frees the buffer without ana_lock, so a concurrent parser in nvme_mpath_add_disk() can use freed memory. - nvme_mpath_init_identify() reallocates the buffer and updates ana_log_size outside ana_lock. nvme_mpath_add_disk() tests nvme_ctrl_use_ana() before taking the lock, so it can pass the check and then dereference a NULL buffer after nvme_mpath_uninit() cleared it; and the freshly allocated buffer can be parsed before nvme_read_ana_log() populates it, reading uninitialized memory. - The shrink path leaves the old log in place while shrinking ana_log_size, and anagrpmax is updated outside ana_lock, so a concurrent parser validates stale descriptors against the new limits and trips WARN_ON_ONCE(). - After nvme_mpath_uninit() clears the buffer, nvme_read_ana_log() calls nvme_get_log() with a NULL buffer and size 0, and nvme_bytes_to_numd(0) underflows to 0xffffffff. - nvme_mpath_add_disk() tests nvme_ctrl_use_ana() outside ana_lock. If ANA is disabled while it waits for the lock, the NULL-buffer bail in nvme_parse_ana_log() leaves desc.state == 0, so the namespace is marked NVME_NS_ANA_PENDING and ana_work is queued instead of taking the non-ANA path, stuck once ANA is gone. Take ana_lock around the buffer release in nvme_mpath_uninit() and around the reallocation in nvme_mpath_init_identify(), updating ana_log_buf, ana_log_size and anagrpmax together so a parser never observes a freed or NULL buffer or stale limits. Allocate the new buffer outside the lock and zero it, and clear ngrps on the shrink path, so a parser running before nvme_read_ana_log() populates the log sees ngrps == 0. nvme_parse_ana_log() and nvme_read_ana_log() bail out when the buffer is NULL. Test nvme_ctrl_use_ana() under ana_lock in nvme_mpath_add_disk() so it falls back to the non-ANA path if ANA was disabled while it waited for the lock. Keep the ana_work drain outside the lock: ana_work itself takes ana_lock, so cancelling it under the lock would deadlock. Fixes: 0d0b660f214d ("nvme: add ANA support") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- Reported by Sashiko AI review as a pre-existing issue. Confirmed by source analysis and reproduced in a KASAN guest with nvmet-tcp loopback and ANA enabled (test-only delays + forced ANA-log size changes). The buffer lifecycle is not serialized against nvme_parse_ana_log(), which reads ana_log_buf/ana_log_size under ana_lock: the free in nvme_mpath_uninit() and the reallocation in nvme_mpath_init_identify() run outside the lock, so a concurrent nvme_mpath_add_disk() parser can use freed memory, dereference a NULL buffer, or read uninitialized or stale descriptors. The FROZEN guard does not close the window: a tcp reset runs nvme_init_identify() before nvme_start_freeze() and does not flush scan_work. With the fix the buffer pointer, ana_log_size and anagrpmax are updated atomically under ana_lock, the new buffer is zeroed (and ngrps cleared on the shrink path) so a parser before nvme_read_ana_log() populates the log sees ngrps == 0, nvme_parse_ana_log() and nvme_read_ana_log() bail out on a NULL buffer, and nvme_mpath_add_disk() tests nvme_ctrl_use_ana() under ana_lock so it falls back to the non-ANA path if ANA was disabled while it waited for the lock. ANA change/optimized transitions and multipath I/O remain unaffected. The descriptor publication into ns->ana_grpid/ana_state deliberately stays outside ana_lock. nvme_update_ns_ana_state() calls nvme_mpath_set_live(), which for a zoned namespace runs synchronous report-zones I/O via nvme_mpath_revalidate_zones(). Holding ana_lock across that I/O would deadlock scan_work against reset_work: reset does not flush scan_work before nvme_mpath_init_identify() takes ana_lock, while a request inserted after nvme_dev_disable() quiesced the queues never completes. ana_work can publish under ana_lock because reset cancels it in nvme_stop_ctrl() first; scan_work has no such drain. The resulting race where a stale add_disk snapshot overwrites a fresher ana_work state is pre-existing and recoverable (the failing I/O queues ana_work via nvme_failover_req()), so it is left as is rather than serialized at the cost of the deadlock. Changes since v3: - test nvme_ctrl_use_ana() under ana_lock in nvme_mpath_add_disk(): the unlocked check raced with nvme_mpath_uninit() clearing the buffer, and the NULL-buffer bail in nvme_parse_ana_log() left desc.state == 0, marking the namespace NVME_NS_ANA_PENDING and queuing ana_work instead of taking the non-ANA path, stuck once ANA was disabled (Sashiko AI review) Changes since v2: - bail out of nvme_read_ana_log() when ana_log_buf is NULL, avoiding nvme_get_log() with a NULL buffer and size 0 where nvme_bytes_to_numd(0) underflows to 0xffffffff (Sashiko AI review) - update anagrpmax under ana_lock and clear ngrps on the shrink path, so a concurrent parser does not validate stale pre-reset descriptors against the shrunk ana_log_size/anagrpmax and trip WARN_ON_ONCE() (Sashiko AI review) Changes since v1: - serialize the reallocation swap (buffer pointer and ana_log_size) under ana_lock, allocating the new buffer outside the lock; bail out of nvme_parse_ana_log() when ana_log_buf is NULL and zero the reallocated buffer. v1 only serialized the free, leaving a NULL dereference window and an uninitialized-memory read (Sashiko AI review) v3: https://lore.kernel.org/all/20261201074919.1051448-1-kanie@linux.alibaba.com/ v2: https://lore.kernel.org/all/20261125122446.1122253-1-kanie@linux.alibaba.com/ v1: https://lore.kernel.org/all/20261125060153.206699-1-kanie@linux.alibaba.com/ drivers/nvme/host/multipath.c | 66 ++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c index bd27a5355bab..9fb88e32e5d4 100644 --- a/drivers/nvme/host/multipath.c +++ b/drivers/nvme/host/multipath.c @@ -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 0; + 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) { @@ -1505,27 +1512,30 @@ void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns) void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid) { - if (nvme_ctrl_use_ana(ns->ctrl)) { - struct nvme_ana_group_desc desc = { - .grpid = anagrpid, - .state = 0, - }; + struct nvme_ana_group_desc desc = { + .grpid = anagrpid, + .state = 0, + }; + bool use_ana; - mutex_lock(&ns->ctrl->ana_lock); + mutex_lock(&ns->ctrl->ana_lock); + use_ana = nvme_ctrl_use_ana(ns->ctrl); + if (use_ana) { ns->ana_grpid = le32_to_cpu(anagrpid); nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); - mutex_unlock(&ns->ctrl->ana_lock); - if (desc.state) { - /* found the group desc: update */ - nvme_update_ns_ana_state(&desc, ns); - } else { - /* group desc not found: trigger a re-read */ - set_bit(NVME_NS_ANA_PENDING, &ns->flags); - queue_work(nvme_wq, &ns->ctrl->ana_work); - } - } else { + } + mutex_unlock(&ns->ctrl->ana_lock); + + if (!use_ana) { 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 { + /* group desc not found: trigger a re-read */ + set_bit(NVME_NS_ANA_PENDING, &ns->flags); + queue_work(nvme_wq, &ns->ctrl->ana_work); } } @@ -1609,7 +1619,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 +1631,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 +1671,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); } -- 2.43.7