Received: from canpmsgout02.his.huawei.com (canpmsgout02.his.huawei.com [113.46.200.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5D99230264 for ; Mon, 22 Jun 2026 01:21:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.217 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782091323; cv=none; b=U+i6AFI2XYJo9MchfugHKWWmdqdn/+f9i+t35qJiI0PIHXvTM0z58WUUSUCojNW/OcSJvbM3vDUWW6fUne9H7m9urBxMol55hjALUNW66KxPIDuycASxwPlXmGfv8aIlVqCzLKHkyIqdUz0c4vMmVmMh3G1tNyJ+/jaKQGziB00= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782091323; c=relaxed/simple; bh=koCiqGXc8hKzAMas+mvVVZlfS+b/ro3HNXOZ19MkRxQ=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=g0hYlSXQxi2P/+Oygd25s5unbADpXwAahyvDR80ZpUbP26n0LpEi4uGZcIDKrWpV+LHp9gUPwE/j9OLoYmtWmhTtDYrvbGdzjJ4FxrBfeskJ+c6fp+/28jFMK7KYO1Jg9X9Fo9+RAfAiqS5Ah/2N0wViUKxFC1UHbPq9BDRg8G0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=eWFQcjj7; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=eWFQcjj7; arc=none smtp.client-ip=113.46.200.217 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="eWFQcjj7"; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="eWFQcjj7" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=VQDLcNX7H0RfY+nHyvxQRES1bhoVcOlkEWngyUigYTk=; b=eWFQcjj7EpevrocgXqU/46jI2z2e61Tr99vDk7CqI5CRIkEpqM9I7MHwS/CTB+Z0Q/Y4Nib4x czjkrFR7flkAc0cvV+wnh5fdKqfoPll+GuCHI0/gBhZC0LVYQJV8Sn9AyaPQy6Z1N6O65sdnZwq 77RwO4qJgg98cx+8Rh4AjBc= Received: from canpmsgout01.his.huawei.com (unknown [172.19.92.178]) by canpmsgout02.his.huawei.com (SkyGuard) with ESMTPS id 4gk9Ds0xdtzcbTs; Mon, 22 Jun 2026 09:13:17 +0800 (CST) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=VQDLcNX7H0RfY+nHyvxQRES1bhoVcOlkEWngyUigYTk=; b=eWFQcjj7EpevrocgXqU/46jI2z2e61Tr99vDk7CqI5CRIkEpqM9I7MHwS/CTB+Z0Q/Y4Nib4x czjkrFR7flkAc0cvV+wnh5fdKqfoPll+GuCHI0/gBhZC0LVYQJV8Sn9AyaPQy6Z1N6O65sdnZwq 77RwO4qJgg98cx+8Rh4AjBc= Received: from mail.maildlp.com (unknown [172.19.162.223]) by canpmsgout01.his.huawei.com (SkyGuard) with ESMTPS id 4gk9Dm50ZPz1T4HL; Mon, 22 Jun 2026 09:13:12 +0800 (CST) Received: from dggpemf100007.china.huawei.com (unknown [7.185.36.214]) by mail.maildlp.com (Postfix) with ESMTPS id 3AA0940561; Mon, 22 Jun 2026 09:21:50 +0800 (CST) Received: from huawei.com (10.67.121.11) by dggpemf100007.china.huawei.com (7.185.36.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 22 Jun 2026 09:21:49 +0800 From: kangfenglong To: CC: , , , , Subject: [PATCH v2] nvme-pci: fix CMB mapping when CMBSZ Size field is zero Date: Mon, 22 Jun 2026 09:21:47 +0800 Message-ID: <20260622012147.40600-1-kangfenglong@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To dggpemf100007.china.huawei.com (7.185.36.214) The controller memory buffer size is defined by the SZ field in the CMBSZ register (bits 31:12). According to the NVMe specification, a value of zero in the SZ field indicates that no CMB is present. Commit f65efd6dfe4e ("nvme-pci: clean up CMB initialization") replaced the check for a zero SZ field with a check for a zero CMBSZ register value, under the assumption that a zero register implies no CMB. However, a CMBSZ register can be non-zero while the SZ field is zero, for example when Size Units (SZU) is set to a non-zero value but the actual size is zero (e.g. CMBSZ = 0x100: SZU = 1, SZ = 0). When this happens, nvme_map_cmb() proceeds to compute a size of zero, passes the alignment checks (zero is always aligned), and calls pci_p2pdma_add_resource() with size=0. The P2PDMA subsystem then defaults size to the entire remaining BAR, which may not be properly aligned for memory hotplug, triggering: WARNING: CPU: 0 PID: 10 at mm/memory_hotplug.c:396 __add_pages Misaligned __add_pages start: 0x494a0000 end: 0x494a000f Fix this by: 1. Restoring the check for a zero SZ field, which explicitly verifies that the controller actually has a CMB before proceeding. 2. Adding a boundary check in nvme_cmb_size_unit() to reject SZU values greater than 6. The SZU field is 4 bits, but values 7-15 are reserved by the NVMe specification and produce a shift count >= 40 in the left-shift operation (1ULL << (12 + 4 * szu) when szu >= 7), which exceeds the maximum 64 GB unit size defined by the spec. 3. Using check_mul_overflow() for the size calculation to detect integer overflow that could wrap to a non-zero value and bypass the SZ check. Fixes: f65efd6dfe4e ("nvme-pci: clean up CMB initialization") Signed-off-by: kangfenglong --- drivers/nvme/host/pci.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index 8438c904ec49..5c7b1e757e4d 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -2448,6 +2448,9 @@ static u64 nvme_cmb_size_unit(struct nvme_dev *dev) { u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK; + if (szu > 6) + return 0; + return 1ULL << (12 + 4 * szu); } @@ -2472,9 +2475,12 @@ static void nvme_map_cmb(struct nvme_dev *dev) dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ); if (!dev->cmbsz) return; + if (!nvme_cmb_size(dev)) + return; dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC); - size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev); + if (check_mul_overflow(nvme_cmb_size_unit(dev), nvme_cmb_size(dev), &size)) + return; offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc); bar = NVME_CMB_BIR(dev->cmbloc); bar_size = pci_resource_len(pdev, bar); -- 2.12.0.windows.1