From mboxrd@z Thu Jan 1 00:00:00 1970 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 C19CF606A322 for ; Fri, 4 Dec 2026 07:01:12 +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=DrXvavd4; dkim=pass (1024-bit key, unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=Z17HZdFq 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=MiwkqhVJsWZjtnVGawUvXpgZdo0AsBkIMatWZZyOHww=; b=DrXvavd4i+o+UH1Irr7a5NT7fj A0gaCQkVRMiZ2IKUrZ98T/kJ7gSORi+H67Wdup6g83IiUCjJhLBqpJn009/nqnrc68GFE5TjS3UZA hvEQb9fo4XC2RutN8t+OyNHZ3f72UJgwwgRxjMn4IKprTPNqxixbfoOjBLXWWglnWZV2t2iNkifOO lwPQ+ZAw2w3RnIlMjg1AW04bjEaOLSy2+6c/xlHf9ojqycOPj46kBLeV4IrFptbtOBn0hixfx9fQH oWriH2ccafyQqpavIi6tNc3a5V5zafP3TWgNqt5i6prAKHcIdOBSgSwDiqh6cWYSSpGARkgjTiPf4 t91qjGAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZNI7-0000000Gs03-3530; Fri, 04 Dec 2026 07:01:11 +0000 Received: from out30-112.freemail.mail.aliyun.com ([115.124.30.112]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZNHk-0000000GrxE-3dyV for linux-nvme@lists.infradead.org; Fri, 04 Dec 2026 07:01:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796367646; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=MiwkqhVJsWZjtnVGawUvXpgZdo0AsBkIMatWZZyOHww=; b=Z17HZdFq8BU1BHSRAZMp+V2KxwQ1rk7ZlAw/msE3q7F2pax2vf0RGoT/RvK1FwaoJ6+NeSCR+b2AYOSll5EySSSlI3KuiHMQA4zrRgkI/Qn9zIyuH9GSW21ou9pqE1fcuJu55RW2haQ0bmRZSM8PBz/tZWXSFfE7wGfFDqELuUc= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R951e4;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_---0XGGL.ib_1796367645; Received: from localhost(mailfrom:kanie@linux.alibaba.com fp:SMTPD_---0XGGL.ib_1796367645 cluster:ay36) by smtp.aliyun-inc.com; Fri, 04 Dec 2026 15:00:45 +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 v5] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock Date: Fri, 4 Dec 2026 15:00:41 +0800 Message-ID: <20261204070041.3602839-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-20261203_230059_255917_6C58EE1E X-CRM114-Status: GOOD ( 19.70 ) 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.8 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 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") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- 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/20261202085124.1252825-1-kanie@linux.alibaba.com/ 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 | 43 ++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c index bd27a5355bab..e2ded689323e 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 -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); } -- 2.43.7