Skip to contentSkip to conversation

PCI: pnv_php: Use common error handling code in pnv_php_alloc_slot()

PATCH In review Markus Elfring 3 participants 3 messages Jun 11 – Jun 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: pnv_php: Use common error handling code in pnv_php_alloc_slot()

From: Markus Elfring <[email protected]>
Date: Thu, 11 Jun 2026 11:16:49 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of an if branch.

This issue was detected by using the Coccinelle software.

---
 drivers/pci/hotplug/pnv_php.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pnv_php.c +3 -4
index ff92a5c301b8..e448f1802002 100644
@@ -791,16 +791,15 @@ static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
 		return NULL;
 
 	php_slot->name = kstrdup(label, GFP_KERNEL);
-	if (!php_slot->name) {
-		kfree(php_slot);
-		return NULL;
-	}
+	if (!php_slot->name)
+		goto free_php_slot;
 
 	/* Allocate workqueue for this slot's interrupt handling */
 	php_slot->wq = alloc_workqueue("pciehp-%s", WQ_PERCPU, 0, php_slot->name);
 	if (!php_slot->wq) {
 		SLOT_WARN(php_slot, "Cannot alloc workqueue\n");
 		kfree(php_slot->name);
+free_php_slot:
 		kfree(php_slot);
 		return NULL;
 	}
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

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

New issues:
- [Low] The patch introduces spaghetti code by placing an error cleanup label inside the body of an intermediate 'if' block.

Pre-existing issues:
- [High] Calling SLOT_WARN() when alloc_workqueue() fails causes a NULL pointer dereference because php_slot->bus is uninitialized. --

commit fc5acb6d5ef23950f12a496bbb0ba6bddebe9f87
Author: Markus Elfring <[email protected]>

PCI: pnv_php: Use common error handling code in pnv_php_alloc_slot()

This commit introduces a new goto label to reuse error handling when allocating a PCI hotplug slot in pnv_php.

Quoted 4 lines
drivers/pci/hotplug/pnv_php.c +0 -0
index ff92a5c301b81..e448f18020026 100644

[ ... ]

Quoted 1 line
drivers/pci/hotplug/pnv_php.c +0 -0
@@ -791,16 +791,15 @@ static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)

[ ... ]

drivers/pci/hotplug/pnv_php.c static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) · line 800

Quoted 4 lines
drivers/pci/hotplug/pnv_php.c +0 -0
 	/* Allocate workqueue for this slot's interrupt handling */
 	php_slot->wq = alloc_workqueue("pciehp-%s", WQ_PERCPU, 0, php_slot->name);
 	if (!php_slot->wq) {
 		SLOT_WARN(php_slot, "Cannot alloc workqueue\n");

[Severity: High]
This is a pre-existing issue, but can this call to SLOT_WARN() cause a NULL pointer dereference?

Looking at pnv_php_alloc_slot(), php_slot->pdev and php_slot->bus are not initialized until much later in the function. Since the structure is kzalloc'd, SLOT_WARN() will evaluate with a NULL php_slot->pdev and fall back to dev_warn(&(sl)->bus->dev, ...).

Because php_slot->bus is also NULL here, this calculates an offset from a NULL pointer and passes it to dev_warn(), which will crash when it attempts to read the device name.

drivers/pci/hotplug/pnv_php.c static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) · line 802

Quoted 2 lines
drivers/pci/hotplug/pnv_php.c +1 -0
 		kfree(php_slot->name);
+free_php_slot:

[Severity: Low]
Does placing a cleanup label inside an intermediate if block obscure the control flow?

Standard kernel coding style typically places cleanup labels sequentially at the end of the function rather than embedding them inside failure paths.

Quoted 3 lines
drivers/pci/hotplug/pnv_php.c +0 -0
 		kfree(php_slot);
 		return NULL;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Markus Elfring
Quoted 24 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 11 Jun 2026 11:16:49 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of an if branch.

This issue was detected by using the Coccinelle software.

---
 drivers/pci/hotplug/pnv_php.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pci/hotplug/pnv_php.c +0 -3
index ff92a5c301b8..e448f1802002 100644
@@ -791,16 +791,15 @@ static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
 		return NULL;
 
 	php_slot->name = kstrdup(label, GFP_KERNEL);
-	if (!php_slot->name) {
-		kfree(php_slot);
-		return NULL;

drivers/pci/hotplug/pnv_php.c static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) · line 802

Quoted 10 lines
drivers/pci/hotplug/pnv_php.c +3 -1
-	}
+	if (!php_slot->name)
+		goto free_php_slot;
 
 	/* Allocate workqueue for this slot's interrupt handling */
 	php_slot->wq = alloc_workqueue("pciehp-%s", WQ_PERCPU, 0, php_slot->name);
 	if (!php_slot->wq) {
 		SLOT_WARN(php_slot, "Cannot alloc workqueue\n");
 		kfree(php_slot->name);
+free_php_slot:

Really? What kind of coding practice is this?

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்