Skip to contentSkip to conversation

cxl/pci: Check dport->regs.rcd_pcie_cap availability before accessing

PATCH In review Li Ming 3 participants 3 messages Nov 29, 2024 – Dec 9, 2024 CXL

lore
Share & tools
lore

Li Ming

author reviewer
#0 lore raw

PATCH 1/1 cxl/pci: Check dport->regs.rcd_pcie_cap availability before accessing

RCD Upstream Port's PCI Express Capability is a component registers block stored in RCD Upstream Port RCRB. CXL PCI driver helps to map it during the RCD probing, but mapping failure is allowed for component registers blocks in CXL PCI driver.

dport->regs.rcd_pcie_cap is used to store the virtual address of the RCD Upstream Port's PCI Express Capability, add a dport->regs.rcd_pcie_cap checking in rcd_pcie_cap_emit() just in case user accesses a invalid address via RCD sysfs.

Fixes c5eaec79fa43 ("cxl/pci: Add sysfs attribute for CXL 1.1 device link status")
---
 drivers/cxl/pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/pci.c +3 -0
index b2cb81f6d9e7..e53b1c95a248 100644
@@ -836,6 +836,9 @@ static ssize_t rcd_pcie_cap_emit(struct device *dev, u16 offset, char *buf, size
 	if (!root_dev)
 		return -ENXIO;
 
+	if (!dport->regs.rcd_pcie_cap)
+		return -ENXIO;
+
 	guard(device)(root_dev);
 	if (!root_dev->driver)
 		return -ENXIO;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Li Ming

RCD Upstream Port's PCI Express Capability is a component registers block stored in RCD Upstream Port RCRB. CXL PCI driver helps to map it during the RCD probing, but mapping failure is allowed for component registers blocks in CXL PCI driver.

dport->regs.rcd_pcie_cap is used to store the virtual address of the RCD Upstream Port's PCI Express Capability, add a dport->regs.rcd_pcie_cap checking in rcd_pcie_cap_emit() just in case user accesses a invalid address via RCD sysfs.

I traced this enough to convince myself that dport cannot be NULL if cxl_mem_find_port() succeeds. So, LGTM -

Quoted 24 lines
Fixes c5eaec79fa43 ("cxl/pci: Add sysfs attribute for CXL 1.1 device link status")
---
 drivers/cxl/pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/pci.c +3 -0
index b2cb81f6d9e7..e53b1c95a248 100644
@@ -836,6 +836,9 @@ static ssize_t rcd_pcie_cap_emit(struct device *dev, u16 offset, char *buf, size
 	if (!root_dev)
 		return -ENXIO;
 
+	if (!dport->regs.rcd_pcie_cap)
+		return -ENXIO;
+
 	guard(device)(root_dev);
 	if (!root_dev->driver)
 		return -ENXIO;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Li Ming
Quoted 5 lines · Li Ming

RCD Upstream Port's PCI Express Capability is a component registers block stored in RCD Upstream Port RCRB. CXL PCI driver helps to map it during the RCD probing, but mapping failure is allowed for component registers blocks in CXL PCI driver.

Quoted 7 lines

dport->regs.rcd_pcie_cap is used to store the virtual address of the RCD Upstream Port's PCI Express Capability, add a dport->regs.rcd_pcie_cap checking in rcd_pcie_cap_emit() just in case user accesses a invalid address via RCD sysfs.

Fixes c5eaec79fa43 ("cxl/pci: Add sysfs attribute for CXL 1.1 device link status")

Hi Ming,

This patch looks ok.

...but it bothers me that the sysfs attributes are visible while the attributes are in this -ENXIO return state. I will throw together a follow-on patch to hide the attributes altogether when these preconditions are not met.