Skip to contentSkip to conversation

nvme-pci: fix CMB mapping when CMBSZ Size field is zero

PATCH Superseded kangfenglong 2 participants 2 messages Jun 18

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-pci: fix CMB mapping when CMBSZ Size field is zero

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 restoring the check for a zero SZ field, which explicitly verifies that the controller actually has a CMB before proceeding with the mapping.

Fixes f65efd6dfe4e ("nvme-pci: clean up CMB initialization")
---
 drivers/nvme/host/pci.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/host/pci.c +2 -0
index 8438c904ec49..530751b11019 100644
@@ -2472,6 +2472,8 @@ 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);

--
2.47.0

#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A 64-bit shift overflow in nvme_cmb_size_unit() causes undefined behavior and integer overflow, allowing a malicious device or surprise removal to bypass the patch's zero-size check. --

commit cff061918a7d037b79313b9dd26c4235a518f58f
Author: kangfenglong <[email protected]>

nvme-pci: fix CMB mapping when CMBSZ Size field is zero

This commit restores a check for a zero SZ field to verify the controller actually has a CMB before proceeding with mapping. This prevents size from defaulting to the entire remaining BAR and triggering memory hotplug alignment warnings.

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1845

Quoted 13 lines
drivers/nvme/host/pci.c +2 -0
index e6267a6aa3801..14f0f141df5bb 100644
@@ -1838,6 +1838,8 @@ 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);

[Severity: High]
This is a pre-existing issue, but could this multiplication still silently overflow to zero and bypass the new zero-size check?

If a device advertises a Size Units (SZU) of 12 and a Size (SZ) of 16, nvme_cmb_size_unit() calculates 1ULL << 60. The multiplication here then becomes (1ULL << 60) * 16, which mathematically equals 1ULL << 64. This wraps around to exactly 0 in a 64-bit unsigned integer. Since SZ is 16, the new nvme_cmb_size(dev) check is passed, but size still becomes 0, potentially triggering the same pci_p2pdma_add_resource() issue this patch aims to fix.

Additionally, if the PCIe device is surprise-removed, readl() returns 0xFFFFFFFF, resulting in SZU = 15. The shift calculation in nvme_cmb_size_unit():

    return 1ULL << (12 + 4 * szu);

evaluates to 1ULL << 72. This is undefined behavior and can result in an unaligned size being passed down.

Does this calculation need further bounds checking or validation to handle integer overflows and surprise removals?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1