[PATCH OLK-6.6 0/1] [PCIe]: Fix nvme_map_cmb triggering WARN_ON when CMBSZ.SZ is zero
From: zengchao <zc18179036325@163.com> driver inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/10377 ----------------------------------------------------------- kangfenglong (1): [PCIe]: Fix nvme_map_cmb triggering WARN_ON when CMBSZ.SZ is zero drivers/nvme/host/pci.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) -- 2.33.0
From: kangfenglong <kangfenglong@huawei.com> driver inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/10377 ----------------------------------------------------------- When an NVMe controller reports a non-zero CMBSZ register but a zero SZ field (e.g., CMBSZ=0x00000100: SZU=1/SZ=0), nvme_map_cmb() computes a size of zero and passes it to pci_p2pdma_add_resource(). The alignment check IS_ALIGNED(size, memremap_compat_align()) at the end of nvme_map_cmb() fails to catch this because IS_ALIGNED(0, X) always returns true — zero is a multiple of any alignment value. Inside pci_p2pdma_add_resource(), a size of zero is treated as "use the full BAR" and replaced with the BAR length (64KB in this case). This 64KB range eventually reaches __add_pages(), where check_pfn_span() requires at least subsection alignment (2MB on ARM64 with CONFIG_SPARSEMEM_VMEMMAP), triggering: Misaligned __add_pages start: 0x494a0000 end: 0x494a000f WARNING: CPU: 0 PID: 10 at mm/memory_hotplug.c:396 Root cause: Commit f65efd6dfe4e ("nvme-pci: clean up CMB initialization") replaced the original zero-SZ check with a check for a zero CMBSZ register value, under the assumption that a zero register implies no CMB. However a controller (e.g., Shenzhen Unionmemory 1cc4:0123) can set CMBSZ.SZU=1 with CMBSZ.SZ=0 and all capability bits cleared, indicating it does not actually support CMB. Per the NVMe spec such controllers should return CMBSZ=0. Fix: 1. Restore the explicit zero-size check in nvme_map_cmb(): if (!nvme_cmb_size(dev)) return; 2. Add a boundary check in nvme_cmb_size_unit() to reject reserved SZU values greater than 6 (SZU is 4 bits but 7-15 are reserved), which would otherwise produce a shift count >= 40 in (1ULL << (12 + 4 * szu)) and exceed the max 64GB unit size per spec. 3. Check the return value of nvme_cmb_size_unit() for zero, and use check_mul_overflow() for the size calculation to detect integer overflow that could wrap to a non-zero value and bypass the SZ check. This ensures a zero-size CMB is treated as no CMB, preventing the invalid memory range from reaching the memory hotplug path. This is a third-party hardware compatibility issue. The fix has been submitted to the upstream Linux NVMe mailing list as [PATCH v3] and merged accordingly. Fixes: 88de4598bca8 ("nvme-pci: clean up SMBSZ bit definitions") Signed-off-by: kangfenglong <kangfenglong@huawei.com> Signed-off-by: zengchao <zc18179036325@163.com> --- drivers/nvme/host/pci.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index a10d2d515b29..3031b42a537a 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -23,6 +23,7 @@ #include <linux/module.h> #include <linux/mutex.h> #include <linux/once.h> +#include <linux/overflow.h> #include <linux/pci.h> #include <linux/suspend.h> #include <linux/t10-pi.h> @@ -1840,6 +1841,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); } @@ -1850,7 +1854,7 @@ static u32 nvme_cmb_size(struct nvme_dev *dev) static void nvme_map_cmb(struct nvme_dev *dev) { - u64 size, offset; + u64 size, unit_size, offset; resource_size_t bar_size; struct pci_dev *pdev = to_pci_dev(dev->dev); int bar; @@ -1864,10 +1868,16 @@ 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); - offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc); + unit_size = nvme_cmb_size_unit(dev); + if (!unit_size) + return; + if (check_mul_overflow(unit_size, nvme_cmb_size(dev), &size)) + return; + offset = unit_size * NVME_CMB_OFST(dev->cmbloc); bar = NVME_CMB_BIR(dev->cmbloc); bar_size = pci_resource_len(pdev, bar); -- 2.33.0
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://atomgit.com/openeuler/kernel/merge_requests/31079 邮件列表地址:https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/UTZ... FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://atomgit.com/openeuler/kernel/merge_requests/31079 Mailing list address: https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/UTZ...
participants (2)
-
patchwork bot -
Yushan Wang