Skip to contentSkip to conversation

PNP: Release protocol device on registration failure

PATCH In review Yuho Choi 2 participants 2 messages Aug 7

Subsystems (2)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 PNP: Release protocol device on registration failure

pnp_register_protocol() adds a protocol to the PNP list before calling device_register(). If device registration fails, pnp_remove_protocol() only removes the list entry and leaves the device-core reference acquired by device_initialize() held.

Give static protocol devices a release callback and drop the reference on registration failure. Also stop PNP ACPI device enumeration when protocol registration fails, instead of using an unregistered protocol device as a parent.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/pnp/core.c         | 9 ++++++++-
 drivers/pnp/pnpacpi/core.c | 7 ++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
drivers/pnp/core.c +8 -1
index 81603327079c..fc3bff43fdd8 100644
@@ -23,6 +23,10 @@ static LIST_HEAD(pnp_protocols);
 LIST_HEAD(pnp_global);
 DEFINE_MUTEX(pnp_lock);
 
+static void pnp_protocol_release(struct device *dev)
+{
+}
+
 /*
  * ACPI or PNPBIOS should tell us about all platform devices, so we can
  * skip some blind probes.  ISAPNP typically enumerates only plug-in ISA
@@ -66,14 +70,17 @@ int pnp_register_protocol(struct pnp_protocol *protocol)
 
 	protocol->number = nodenum;
 	dev_set_name(&protocol->dev, "pnp%d", nodenum);
+	protocol->dev.release = pnp_protocol_release;
 
 	list_add_tail(&protocol->protocol_list, &pnp_protocols);
 
 	mutex_unlock(&pnp_lock);
 
 	ret = device_register(&protocol->dev);
-	if (ret)
+	if (ret) {
 		pnp_remove_protocol(protocol);
+		put_device(&protocol->dev);
+	}
 
 	return ret;
 }
drivers/pnp/pnpacpi/core.c +6 -1
index fbf03ff007eb..da0ebc378696 100644
@@ -298,12 +298,17 @@ static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
 int pnpacpi_disabled __initdata;
 static int __init pnpacpi_init(void)
 {
+	int ret;
+
 	if (acpi_disabled || pnpacpi_disabled) {
 		printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
 		return 0;
 	}
 	printk(KERN_INFO "pnp: PnP ACPI init\n");
-	pnp_register_protocol(&pnpacpi_protocol);
+	ret = pnp_register_protocol(&pnpacpi_protocol);
+	if (ret)
+		return ret;
+
 	acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
 	printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
 	pnp_platform_devices = 1;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 34 lines · Yuho Choi

pnp_register_protocol() adds a protocol to the PNP list before calling device_register(). If device registration fails, pnp_remove_protocol() only removes the list entry and leaves the device-core reference acquired by device_initialize() held.

Give static protocol devices a release callback and drop the reference on registration failure. Also stop PNP ACPI device enumeration when protocol registration fails, instead of using an unregistered protocol device as a parent.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/pnp/core.c         | 9 ++++++++-
 drivers/pnp/pnpacpi/core.c | 7 ++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
drivers/pnp/core.c +4 -0
index 81603327079c..fc3bff43fdd8 100644
@@ -23,6 +23,10 @@ static LIST_HEAD(pnp_protocols);
 LIST_HEAD(pnp_global);
 DEFINE_MUTEX(pnp_lock);
 
+static void pnp_protocol_release(struct device *dev)
+{
+}
+
 /*
  * ACPI or PNPBIOS should tell us about all platform devices, so we can
  * skip some blind probes.  ISAPNP typically enumerates only plug-in ISA

drivers/pnp/core.c int pnp_register_protocol(struct pnp_protocol *protocol) · line 73

Quoted 4 lines
drivers/pnp/core.c +1 -0
@@ -66,14 +70,17 @@ int pnp_register_protocol(struct pnp_protocol *protocol)
 
        protocol->number = nodenum;
        dev_set_name(&protocol->dev, "pnp%d", nodenum);
+       protocol->dev.release = pnp_protocol_release;

Sashiko complains about adding an empty release callback here and IMV it has a point:

https://sashiko.dev/#/patchset/20260807035453.948129-1-dbgh9129%40gmail.com

Quoted 40 lines
drivers/pnp/core.c +3 -1
        list_add_tail(&protocol->protocol_list, &pnp_protocols);
 
        mutex_unlock(&pnp_lock);
 
        ret = device_register(&protocol->dev);
-       if (ret)
+       if (ret) {
                pnp_remove_protocol(protocol);
+               put_device(&protocol->dev);
+       }
 
        return ret;
 }
drivers/pnp/pnpacpi/core.c +6 -2
index fbf03ff007eb..da0ebc378696 100644
@@ -298,12 +298,17 @@ static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
 int pnpacpi_disabled __initdata;
 static int __init pnpacpi_init(void)
 {
+       int ret;
+
        if (acpi_disabled || pnpacpi_disabled) {
                printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
                return 0;
        }
        printk(KERN_INFO "pnp: PnP ACPI init\n");
-       pnp_register_protocol(&pnpacpi_protocol);
+       ret = pnp_register_protocol(&pnpacpi_protocol);
+       if (ret)
+               return ret;
+
        acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
        printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
        pnp_platform_devices = 1;
--

2.43.0