Skip to contentSkip to conversation

cxl/acpi: Add Null check for adev

PATCH In review Chenyuan Yang 4 participants 4 messages Mar 13, 2025 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/acpi: Add Null check for adev

Not all devices have an ACPI companion fwnode, so adev might be NULL. This is similar to the commit cd2fd6eab480
("platform/x86: int3472: Check for adev == NULL").

Add a check for adev not being set and return -ENODEV in that case to avoid a possible NULL pointer deref in cxl_acpi_probe().

---
 drivers/cxl/acpi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/acpi.c +3 -0
index cb14829bb9be..9195001db3c1 100644
@@ -823,6 +823,9 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	struct acpi_device *adev = ACPI_COMPANION(host);
 	struct cxl_cfmws_context ctx;
 
+	if (!adev)
+		return -ENODEV;
+
 	device_lock_set_class(&pdev->dev, &cxl_root_key);
 	rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class,
 				      &pdev->dev);
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 7 lines · Chenyuan Yang

Not all devices have an ACPI companion fwnode, so adev might be NULL. This is similar to the commit cd2fd6eab480
("platform/x86: int3472: Check for adev == NULL").

Add a check for adev not being set and return -ENODEV in that case to avoid a possible NULL pointer deref in cxl_acpi_probe().

Avoiding the NULL ptr deref seems obvious as ACPI_COMPANION() return is routinely checked throughout the kernel. Why the reference to the other commit? Do these devices have something in common?

I'm curious as to when *this* specific adev can be NULL.

Looks good to check it like you do here, or if someone chimes in that it can never be NULL, just add a code comment saying so.

Perhaps emit a message on NULL too.

Quoted 22 lines
---
 drivers/cxl/acpi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/acpi.c +3 -0
index cb14829bb9be..9195001db3c1 100644
@@ -823,6 +823,9 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	struct acpi_device *adev = ACPI_COMPANION(host);
 	struct cxl_cfmws_context ctx;
 
+	if (!adev)
+		return -ENODEV;
+
 	device_lock_set_class(&pdev->dev, &cxl_root_key);
 	rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class,
 				      &pdev->dev);
Signature · 1 line
2.34.1
maintainer
#2 lore raw
Quoted 9 lines · Alison Schofield
Quoted 7 lines · Chenyuan Yang

Not all devices have an ACPI companion fwnode, so adev might be NULL. This is similar to the commit cd2fd6eab480
("platform/x86: int3472: Check for adev == NULL").

Add a check for adev not being set and return -ENODEV in that case to avoid a possible NULL pointer deref in cxl_acpi_probe().

Quoted 5 lines

Avoiding the NULL ptr deref seems obvious as ACPI_COMPANION() return is routinely checked throughout the kernel. Why the reference to the other commit? Do these devices have something in common?

I'm curious as to when *this* specific adev can be NULL.

You get a NULL by either the the platform device being probed is not setup correctly and the fwnode end up failing is_acpi_device_node() or CONFIG_ACPI is not set. The second is impossible as cxl_acpi depends on CONFIG_CXL_ACPI which depends on CONFIG_ACPI. The likelihood of the first I don't think is high unless the platform device for ACPI0017 is corrupted. So in this instance in the probe function, the checking while is fine for code correctness, may not be absolutely necessary perhaps. Just my 2 cents.

DJ

Quoted 30 lines

Looks good to check it like you do here, or if someone chimes in that it can never be NULL, just add a code comment saying so.

Perhaps emit a message on NULL too.

Quoted 22 lines
---
 drivers/cxl/acpi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/acpi.c +3 -0
index cb14829bb9be..9195001db3c1 100644
@@ -823,6 +823,9 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	struct acpi_device *adev = ACPI_COMPANION(host);
 	struct cxl_cfmws_context ctx;
 
+	if (!adev)
+		return -ENODEV;
+
 	device_lock_set_class(&pdev->dev, &cxl_root_key);
 	rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class,
 				      &pdev->dev);
Signature · 1 line
2.34.1
#3 lore raw
In reply to Chenyuan Yang
Quoted 3 lines · Chenyuan Yang

Not all devices have an ACPI companion fwnode, so adev might be NULL. This is similar to the commit cd2fd6eab480 ("platform/x86: int3472:
Check for adev == NULL").

No, please do not consume review bandwidth with patches like this.

Read the full commit you referenced it explicitly states the failing condition:

"This can e.g. (theoretically) happen when a user manually binds one of the int3472 drivers to another i2c/platform device through sysfs."

Then think through whether that theoretical condition applies to the cxl_acpi driver and the ACPI0017 device.