Skip to contentSkip to conversation

ACPI: scan: Use unique number for instance_no

PATCH Superseded Andy Shevchenko 2 participants 5 messages Mar 12, 2021 – Mar 19, 2021 ACPI

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI: scan: Use unique number for instance_no

Current mechanism of incrementing and decrementing plain integer to get a next free instance_no when creating an ACPI device is fragile.

In case of hot plug event or namespace removal of the device instances with the low numbers the plain integer counter can't cover the gaps and become desynchronized with real state of affairs. If during next hot plug event or namespace injection the new instances of the devices need to be instantiated, the counter may mistakenly point to the existing instance_no and kernel will complain:
"sysfs: cannot create duplicate filename '/bus/acpi/devices/XXXX1234:02'"

Replace plain integer approach by using IDA framework.

Fixes e49bd2dd5a50 ("ACPI: use PNPID:instance_no as bus_id of ACPI device")
Fixes ca9dc8d42b30 ("ACPI / scan: Fix acpi_bus_id_list bookkeeping")
---
 drivers/acpi/internal.h |  4 ++-
 drivers/acpi/scan.c     | 55 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 53 insertions(+), 6 deletions(-)
drivers/acpi/internal.h +3 -1
index e6a5d997241c..6fee4f71ba1c 100644
@@ -9,6 +9,8 @@
 #ifndef _ACPI_INTERNAL_H_
 #define _ACPI_INTERNAL_H_
 
+#include <linux/idr.h>
+
 #define PREFIX "ACPI: "
 
 int early_acpi_osi_init(void);
@@ -98,7 +100,7 @@ extern struct list_head acpi_bus_id_list;
 
 struct acpi_device_bus_id {
 	const char *bus_id;
-	unsigned int instance_no;
+	struct ida no;
 	struct list_head node;
 };
 
drivers/acpi/scan.c +50 -5

90 lines not on this page · raw message

Signature · 1 line
2.30.1
#1 lore raw
Quoted 11 lines · Andy Shevchenko

Current mechanism of incrementing and decrementing plain integer to get a next free instance_no when creating an ACPI device is fragile.

In case of hot plug event or namespace removal of the device instances with the low numbers the plain integer counter can't cover the gaps and become desynchronized with real state of affairs. If during next hot plug event or namespace injection the new instances of the devices need to be instantiated, the counter may mistakenly point to the existing instance_no and kernel will complain:
"sysfs: cannot create duplicate filename '/bus/acpi/devices/XXXX1234:02'"

This is a slightly convoluted way of stating that there is a bug in acpi_device_del().

Yes, there is one, the instance_no decrementation is clearly incorrect.

Quoted 1 line

Replace plain integer approach by using IDA framework.

Also the general idea of using IDA for the instance numbering is a good one IMO.

Quoted 22 lines
Fixes e49bd2dd5a50 ("ACPI: use PNPID:instance_no as bus_id of ACPI device")
Fixes ca9dc8d42b30 ("ACPI / scan: Fix acpi_bus_id_list bookkeeping")
---
 drivers/acpi/internal.h |  4 ++-
 drivers/acpi/scan.c     | 55 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 53 insertions(+), 6 deletions(-)
drivers/acpi/internal.h +2 -0
index e6a5d997241c..6fee4f71ba1c 100644
@@ -9,6 +9,8 @@
 #ifndef _ACPI_INTERNAL_H_
 #define _ACPI_INTERNAL_H_
 
+#include <linux/idr.h>
+
 #define PREFIX "ACPI: "
 
 int early_acpi_osi_init(void);

drivers/acpi/internal.h extern struct list_head acpi_bus_id_list; · line 103

Quoted 5 lines
drivers/acpi/internal.h +1 -1
@@ -98,7 +100,7 @@ extern struct list_head acpi_bus_id_list;
 
 struct acpi_device_bus_id {
        const char *bus_id;
-       unsigned int instance_no;
+       struct ida no;

struct ida instance_ida; ?

Quoted 17 lines
drivers/acpi/internal.h +0 -0
        struct list_head node;
 };
 
drivers/acpi/scan.c +6 -0
index a184529d8fa4..a118a58f7dad 100644
@@ -468,9 +468,27 @@ static void acpi_device_release(struct device *dev)
        kfree(acpi_dev);
 }
 
+static int acpi_device_get_instance_no(struct acpi_device *device)
+{
+       const char *p;
+       int result;
+       int error;
+

drivers/acpi/scan.c static void acpi_device_release(struct device *dev) · line 486

Quoted 10 lines
drivers/acpi/scan.c +10 -0
+       p = strrchr(dev_name(&device->dev), ':');
+       if (!p)
+               return -ENODATA;
+
+       error = kstrtoint(p + 1, 16, &result);
+       if (error)
+               return error;
+
+       return result;
+}

I don't like the above at all.

I would just store the instance number in struct acpi_device_pnp (say).

#2 lore raw
Quoted 14 lines · Rafael J. Wysocki
Quoted 11 lines · Andy Shevchenko

Current mechanism of incrementing and decrementing plain integer to get a next free instance_no when creating an ACPI device is fragile.

In case of hot plug event or namespace removal of the device instances with the low numbers the plain integer counter can't cover the gaps and become desynchronized with real state of affairs. If during next hot plug event or namespace injection the new instances of the devices need to be instantiated, the counter may mistakenly point to the existing instance_no and kernel will complain:
"sysfs: cannot create duplicate filename '/bus/acpi/devices/XXXX1234:02'"

Quoted 2 lines

This is a slightly convoluted way of stating that there is a bug in acpi_device_del().

Any suggestion how to massage the above?
But in the dry end, yes, decrementing is a bug.

Quoted 5 lines

Yes, there is one, the instance_no decrementation is clearly incorrect.

Quoted 1 line

Replace plain integer approach by using IDA framework.

Also the general idea of using IDA for the instance numbering is a good one IMO.

...

Quoted 4 lines

drivers/acpi/internal.h extern struct list_head acpi_bus_id_list; · line 103

Quoted 2 lines
drivers/acpi/internal.h +1 -1
-       unsigned int instance_no;
+       struct ida no;

struct ida instance_ida; ?

Will rename.

...

Quoted 12 lines

drivers/acpi/scan.c static void acpi_device_release(struct device *dev) · line 485

Quoted 9 lines
drivers/acpi/scan.c +9 -0
+       p = strrchr(dev_name(&device->dev), ':');
+       if (!p)
+               return -ENODATA;
+
+       error = kstrtoint(p + 1, 16, &result);
+       if (error)
+               return error;
+
+       return result;

I don't like the above at all.

Quoted 1 line

I would just store the instance number in struct acpi_device_pnp (say).

TBH, I simply didn't know which struct to touch and left this one and I also don't like it. Lemme see if acpi_device_pnp is good enough for that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 5 lines · Rafael J. Wysocki

On Fri, Mar 12, 2021 at 5:02 PM Andy Shevchenko
<[email protected]> wrote:

This is a slightly convoluted way of stating that there is a bug in acpi_device_del().

Any suggestion how to massage the above?
But in the dry end, yes, decrementing is a bug.

Okay, v2 has been sent. I tried to simplify the message.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Andy Shevchenko
Quoted 16 lines · Andy Shevchenko
Quoted 14 lines · Rafael J. Wysocki
Quoted 11 lines · Andy Shevchenko

Current mechanism of incrementing and decrementing plain integer to get a next free instance_no when creating an ACPI device is fragile.

In case of hot plug event or namespace removal of the device instances with the low numbers the plain integer counter can't cover the gaps and become desynchronized with real state of affairs. If during next hot plug event or namespace injection the new instances of the devices need to be instantiated, the counter may mistakenly point to the existing instance_no and kernel will complain:
"sysfs: cannot create duplicate filename '/bus/acpi/devices/XXXX1234:02'"

Quoted 4 lines
Quoted 2 lines

This is a slightly convoluted way of stating that there is a bug in acpi_device_del().

Any suggestion how to massage the above?

Why don't you simply say something like "The decrementation of acpi_device_bus_id->instance_no in acpi_device_del() is incorrect, because it may cause a duplicate instance number to be allocated next time a device with the same acpi_device_bus_id is added."