Skip to contentSkip to conversation

ACPI: scan: Address power management initialization and PCI devices handling

PATCH Superseded Rafael J. Wysocki 4 participants 24 messages Aug 31 – Sep 2

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

cover ACPI: scan: Address power management initialization and PCI devices handling

Hi All,

This series aims at addressing a few issues present in the core ACPI device enumeration code, mostly related to the acpi_bus_attach() function (that gets renamed in one of the patches).

The first one is that, on some systems, ACPI power management is initialized twice for devices that have missing dependencies to start with. An attempt to address this issue had been made during the 7.3 merge window, but it had to be reverted [1]. The approach used here is roughly the same as in the reverted commit, but it takes PCI devices (which are a special case) into account.

The other issues are that flags are unnecessarily cleared by acpi_bus_attach() for devices with missing dependencies, the "initialized" ACPI device object flag is not particularly useful, the "visited" ACPI device object flag is used for two different things which is confusing at best, and that PCI devices are handled by acpi_bus_attach() like any other devices which is a mistake.

Please see the changelogs of individual patches for details.

I have tested this series on a couple of machines, but let's see what Sashiko has to say about it.

Thanks!

author maintainer
#1 lore raw

PATCH 1/7 ACPI: scan: Stop calling acpi_bus_init_power() early

From: "Rafael J. Wysocki" <[email protected]>

There is a problem, introduced by commit 9d9bcae47fd5 ("ACPI: delay enumeration of devices with a _DEP pointing to an INT3472 device") inadvertently, that causes devices with missing dependencies to be put into power state D0 prematurely on some systems [1].

Namely, acpi_bus_init_power() called by acpi_bus_get_power_flags() during the early initialization of ACPI device objects, may discover that all of the power resources needed by the given device to be in power state D0 are initially on, so it will reference count those power resources and transition the device into D0. Later, if acpi_bus_attach() running for that device notices that it has missing dependencies, the enumeration of it will be deferred and its power_manageable flag will be cleared, even though it is still in D0 at that point.

After the dependencies in question have been met, acpi_bus_attach() will run again for the device and now it will call acpi_bus_init_power() that will take additional references to the power resources used by the device in D0. These additional references prevent the power resources from being turned off when the device goes into D3hot/D3cold.

To address this, stop calling acpi_bus_init_power() from acpi_bus_get_power_flags(), but also take the initialization of PCI devices into account, which needs to be done because they are initialized and bound to their ACPI companions before calling acpi_bus_attach() for the latter.

To that end, notice that each PCI device discovered on the bus is put into power state D0 via pci_power_up() which involves invoking acpi_device_set_power(). The initial ACPI power state of the device needs to be known at that point to carry out the power transition of it properly, so modify acpi_device_set_power() to call acpi_bus_init_power() upfront if the device's ACPI power state is still unknown.

Also use the ACPI power state tracking to decide whether or not acpi_bus_init_power() needs to be called by acpi_bus_attach() instead of using the "initialized" flag of the ACPI device object for this purpose, which is fragile and inconvenient, and stop clearing the power_manageable flag for devices with unmet dependencies, which is not necessary.

While at it, add a debug message pringing statement to acpi_bus_init_power() to facilitate diagnostics.

Fixes 9d9bcae47fd5 ("ACPI: delay enumeration of devices with a _DEP pointing to an INT3472 device")
---
 drivers/acpi/device_pm.c | 19 ++++++++++---------
 drivers/acpi/scan.c      | 18 ++++++------------
 2 files changed, 16 insertions(+), 21 deletions(-)
drivers/acpi/device_pm.c +10 -9
index aa55ecfc2923..a680e6972a8c 100644
@@ -168,6 +168,12 @@ int acpi_device_set_power(struct acpi_device *device, int state)
 	    || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
 		return -EINVAL;
 
+	if (device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device)) {
+		device->flags.power_manageable = 0;
+		return -ENODEV;
+	}
+
 	acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
 			  acpi_power_state_string(device->power.state),
 			  acpi_power_state_string(state));
@@ -309,15 +315,6 @@ int acpi_bus_init_power(struct acpi_device *device)
 	int state;
 	int result;
 
-	if (!device)
-		return -EINVAL;
-
-	device->power.state = ACPI_STATE_UNKNOWN;
-	if (!acpi_device_is_present(device)) {
-		device->flags.initialized = false;
-		return -ENXIO;
-	}
-
 	result = acpi_device_get_power(device, &state);
 	if (result)
 		return result;
@@ -351,6 +348,10 @@ int acpi_bus_init_power(struct acpi_device *device)
 		state = ACPI_STATE_D0;
 	}
 	device->power.state = state;
+
+	acpi_handle_debug(device->handle, "Initial power state: %s\n",
+			  acpi_power_state_string(state));
+
 	return 0;
 }
 
drivers/acpi/scan.c +6 -12
index f48715ed827c..1ad8dffc2daf 100644
@@ -1145,8 +1145,7 @@ static void acpi_bus_get_power_flags(struct acpi_device *device)
 			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
 	}
 
-	if (acpi_bus_init_power(device))
-		device->flags.power_manageable = 0;
+	device->power.state = ACPI_STATE_UNKNOWN;
 }
 
 static void acpi_bus_get_flags(struct acpi_device *device)
@@ -2354,9 +2353,7 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 	acpi_bus_get_status(device);
 	/* Skip devices that are not ready for enumeration (e.g. not present) */
 	if (!acpi_dev_ready_for_enumeration(device)) {
-		device->flags.initialized = false;
 		acpi_device_clear_enumerated(device);
-		device->flags.power_manageable = 0;
 		return 0;
 	}
 	if (device->handler)
@@ -2364,16 +2361,13 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 
 	acpi_ec_register_opregions(device);
 
-	if (!device->flags.initialized) {
-		device->flags.power_manageable =
-			device->power.states[ACPI_STATE_D0].flags.valid;
-		if (acpi_bus_init_power(device))
-			device->flags.power_manageable = 0;
+	if (device->flags.power_manageable &&
+	    device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device))
+		device->flags.power_manageable = 0;
 
-		device->flags.initialized = true;
-	} else if (device->flags.visited) {
+	if (device->flags.visited)
 		goto ok;
-	}
 
 	ret = acpi_scan_attach_handler(device);
 	if (ret < 0)
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH v1 1/7] ACPI: scan: Stop calling acpi_bus_init_power() early

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Transient failures in early PCI enumeration due to unmet ACPI dependencies permanently disable…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Transient failures in early PCI enumeration due to unmet ACPI dependencies permanently disable ACPI power management for the device.
- [High] Concurrent execution of acpi_device_set_power and acpi_bus_attach leads to power resource reference count leaks.
- [High] Bitfield data race on struct acpi_device_flags leading to corrupted device flags.
- [High] Unprivileged sysfs access to a child's power state bypasses the parent's power resource initialization, leading to reference count underflow and permanent power leaks. --

commit 00db710bd9860a36a16f28aa782d074b11d43aab
Author: Rafael J. Wysocki <[email protected]>

ACPI: scan: Stop calling acpi_bus_init_power() early

This commit prevents premature transition into the D0 power state for devices with missing dependencies by changing when acpi_bus_init_power() is called. It updates acpi_device_set_power() to initialize power upfront and adjusts acpi_bus_attach() to use ACPI power state tracking instead of the initialized flag.

Quoted 4 lines
drivers/acpi/device_pm.c +0 -0
index aa55ecfc2923c..a680e6972a8c2 100644

[ ... ]

drivers/acpi/device_pm.c int acpi_device_set_power(struct acpi_device *device, int state) · line 173

Quoted 7 lines
drivers/acpi/device_pm.c +3 -0
@@ -168,6 +168,12 @@ int acpi_device_set_power(struct acpi_device *device, int state)
 	    || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
 		return -EINVAL;
 
+	if (device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device)) {
+		device->flags.power_manageable = 0;

[Severity: High]
If acpi_bus_init_power() fails transiently here because dependencies are unmet (for instance, _PSC or _ON evaluations fail during early PCI discovery), does this permanently disable ACPI power management for the device? Since the prior restoration logic was removed from acpi_bus_attach(), it appears there is no mechanism to recover from this transient failure when dependencies are met later.

[Severity: High]
Because struct acpi_device_flags is a bitfield, does modifying power_manageable here without mutual exclusion risk a data race?

If acpi_device_set_power() runs concurrently with async enumeration (such as acpi_bus_attach() calling acpi_device_set_enumerated()), they could perform concurrent read-modify-write operations on the bitfield. This could cause one of the flag updates (like the visited or enumerated flag) to be silently lost.

Quoted 6 lines
drivers/acpi/device_pm.c +3 -0
+		return -ENODEV;
+	}
+
 	acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
 			  acpi_power_state_string(device->power.state),
 			  acpi_power_state_string(state));

[ ... ]

Quoted 4 lines
drivers/acpi/scan.c +0 -0
index f48715ed827c7..1ad8dffc2dafe 100644

[ ... ]

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2366

Quoted 12 lines
drivers/acpi/scan.c +3 -5
@@ -2364,16 +2361,13 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 
 	acpi_ec_register_opregions(device);
 
-	if (!device->flags.initialized) {
-		device->flags.power_manageable =
-			device->power.states[ACPI_STATE_D0].flags.valid;
-		if (acpi_bus_init_power(device))
-			device->flags.power_manageable = 0;
+	if (device->flags.power_manageable &&
+	    device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device))

[Severity: High]
Is it possible for acpi_bus_attach() (running asynchronously via acpi_scan_clear_dep_fn()) and acpi_device_set_power() (reachable via sysfs power/control) to execute concurrently?

Both paths now perform a lockless check of device->power.state == ACPI_STATE_UNKNOWN and subsequently call acpi_bus_init_power(). Could this lead to both threads acquiring power resource references via acpi_power_on_resources(), keeping the hardware powered on indefinitely when the second set of references is leaked?

[Severity: High]
Could an unprivileged user reading the real_power_state sysfs attribute of a child device bypass this parent initialization?

If a sysfs read calls acpi_device_get_power() on the child, it can locklessly mutate the parent's state to ACPI_STATE_D0:

acpi_device_get_power() {
    ...
    if (!device->power.flags.ignore_parent && parent &&
        parent->power.state == ACPI_STATE_UNKNOWN &&
        result == ACPI_STATE_D0)
            parent->power.state = ACPI_STATE_D0;
    ...
}

If this occurs before acpi_bus_attach() runs for the parent, the lockless check here in acpi_bus_attach() will see a state other than ACPI_STATE_UNKNOWN and skip calling acpi_bus_init_power().

Does this result in the parent's power resources never being initialized, leading to a reference count underflow when the device later transitions to D3?

Quoted 7 lines
drivers/acpi/scan.c +2 -3
+		device->flags.power_manageable = 0;
 
-		device->flags.initialized = true;
-	} else if (device->flags.visited) {
+	if (device->flags.visited)
 		goto ok;
-	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCH v1 1/7] ACPI: scan: Stop calling acpi_bus_init_power() early

Quoted 32 lines · On Mon, Aug 31, 2026 at 10:14 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Transient failures in early PCI enumeration due to unmet ACPI dependencies permanently disable ACPI power management for the device.
- [High] Concurrent execution of acpi_device_set_power and acpi_bus_attach leads to power resource reference count leaks.
- [High] Bitfield data race on struct acpi_device_flags leading to corrupted device flags.
- [High] Unprivileged sysfs access to a child's power state bypasses the parent's power resource initialization, leading to reference count underflow and permanent power leaks. --

commit 00db710bd9860a36a16f28aa782d074b11d43aab
Author: Rafael J. Wysocki <[email protected]>

ACPI: scan: Stop calling acpi_bus_init_power() early

This commit prevents premature transition into the D0 power state for devices with missing dependencies by changing when acpi_bus_init_power() is called. It updates acpi_device_set_power() to initialize power upfront and adjusts acpi_bus_attach() to use ACPI power state tracking instead of the initialized flag.

Quoted 4 lines
drivers/acpi/device_pm.c +0 -0
index aa55ecfc2923c..a680e6972a8c2 100644

[ ... ]

Quoted 4 lines
drivers/acpi/device_pm.c +0 -0
@@ -168,6 +168,12 @@ int acpi_device_set_power(struct acpi_device *device, int state)
          || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
              return -EINVAL;
 
Quoted 11 lines

drivers/acpi/device_pm.c int acpi_device_set_power(struct acpi_device *device, int state) · line 173

Quoted 3 lines
drivers/acpi/device_pm.c +3 -0
+     if (device->power.state == ACPI_STATE_UNKNOWN &&
+         acpi_bus_init_power(device)) {
+             device->flags.power_manageable = 0;

[Severity: High]
If acpi_bus_init_power() fails transiently here because dependencies are unmet (for instance, _PSC or _ON evaluations fail during early PCI discovery), does this permanently disable ACPI power management for the device? Since the prior restoration logic was removed from acpi_bus_attach(), it appears there is no mechanism to recover from this transient failure when dependencies are met later.

Good point.

Quoted 9 lines

[Severity: High]
Because struct acpi_device_flags is a bitfield, does modifying power_manageable here without mutual exclusion risk a data race?

If acpi_device_set_power() runs concurrently with async enumeration (such as acpi_bus_attach() calling acpi_device_set_enumerated()), they could perform concurrent read-modify-write operations on the bitfield. This could cause one of the flag updates (like the visited or enumerated flag) to be silently lost.

This will not happen AFAICS, but in fact it is not necessary to clear flags.power_manageable in acpi_device_set_power() which will also address the previous comment.

The power state will remain ACPI_STATE_UNKNOWN until it gets properly initialized either during acpi_device_set_power() execution or by acpi_bus_attach() which both call acpi_bus_init_power() if the power state is unknown.

Quoted 21 lines
Quoted 7 lines
drivers/acpi/device_pm.c +3 -0
+             return -ENODEV;
+     }
+
      acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
                        acpi_power_state_string(device->power.state),
                        acpi_power_state_string(state));

[ ... ]

Quoted 4 lines
drivers/acpi/scan.c +0 -0
index f48715ed827c7..1ad8dffc2dafe 100644

[ ... ]

Quoted 4 lines
drivers/acpi/scan.c +0 -0
@@ -2364,16 +2361,13 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 
      acpi_ec_register_opregions(device);
 
Quoted 13 lines

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2366

Quoted 8 lines
drivers/acpi/scan.c +3 -5
-     if (!device->flags.initialized) {
-             device->flags.power_manageable =
-                     device->power.states[ACPI_STATE_D0].flags.valid;
-             if (acpi_bus_init_power(device))
-                     device->flags.power_manageable = 0;
+     if (device->flags.power_manageable &&
+         device->power.state == ACPI_STATE_UNKNOWN &&
+         acpi_bus_init_power(device))

[Severity: High]
Is it possible for acpi_bus_attach() (running asynchronously via acpi_scan_clear_dep_fn()) and acpi_device_set_power() (reachable via sysfs power/control) to execute concurrently?

This can only happen for PCI devices and only when their initial power-up fails to initialize the ACPI power state.

Quoted 5 lines

Both paths now perform a lockless check of device->power.state == ACPI_STATE_UNKNOWN and subsequently call acpi_bus_init_power(). Could this lead to both threads acquiring power resource references via acpi_power_on_resources(), keeping the hardware powered on indefinitely when the second set of references is leaked?

This can be addressed by pulling the PCI device checks from the last patch into the first one.

Quoted 16 lines

[Severity: High]
Could an unprivileged user reading the real_power_state sysfs attribute of a child device bypass this parent initialization?

If a sysfs read calls acpi_device_get_power() on the child, it can locklessly mutate the parent's state to ACPI_STATE_D0:

acpi_device_get_power() {
    ...
    if (!device->power.flags.ignore_parent && parent &&
        parent->power.state == ACPI_STATE_UNKNOWN &&
        result == ACPI_STATE_D0)
            parent->power.state = ACPI_STATE_D0;
    ...
}
Quoted 7 lines

If this occurs before acpi_bus_attach() runs for the parent, the lockless check here in acpi_bus_attach() will see a state other than ACPI_STATE_UNKNOWN and skip calling acpi_bus_init_power().

Does this result in the parent's power resources never being initialized, leading to a reference count underflow when the device later transitions to D3?

Good catch. This parent power state update is fishy and shouldn't be done anyway.

Quoted 9 lines

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2376

Quoted 7 lines
drivers/acpi/scan.c +2 -3
+             device->flags.power_manageable = 0;
 
-             device->flags.initialized = true;
-     } else if (device->flags.visited) {
+     if (device->flags.visited)
              goto ok;
-     }

--

#4 lore raw

Re: [PATCH v1 1/7] ACPI: scan: Stop calling acpi_bus_init_power() early

In reply to Rafael J. Wysocki
Quoted 47 lines · Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

There is a problem, introduced by commit 9d9bcae47fd5 ("ACPI: delay enumeration of devices with a _DEP pointing to an INT3472 device") inadvertently, that causes devices with missing dependencies to be put into power state D0 prematurely on some systems [1].

Namely, acpi_bus_init_power() called by acpi_bus_get_power_flags() during the early initialization of ACPI device objects, may discover that all of the power resources needed by the given device to be in power state D0 are initially on, so it will reference count those power resources and transition the device into D0. Later, if acpi_bus_attach() running for that device notices that it has missing dependencies, the enumeration of it will be deferred and its power_manageable flag will be cleared, even though it is still in D0 at that point.

After the dependencies in question have been met, acpi_bus_attach() will run again for the device and now it will call acpi_bus_init_power() that will take additional references to the power resources used by the device in D0. These additional references prevent the power resources from being turned off when the device goes into D3hot/D3cold.

To address this, stop calling acpi_bus_init_power() from acpi_bus_get_power_flags(), but also take the initialization of PCI devices into account, which needs to be done because they are initialized and bound to their ACPI companions before calling acpi_bus_attach() for the latter.

To that end, notice that each PCI device discovered on the bus is put into power state D0 via pci_power_up() which involves invoking acpi_device_set_power(). The initial ACPI power state of the device needs to be known at that point to carry out the power transition of it properly, so modify acpi_device_set_power() to call acpi_bus_init_power() upfront if the device's ACPI power state is still unknown.

Also use the ACPI power state tracking to decide whether or not acpi_bus_init_power() needs to be called by acpi_bus_attach() instead of using the "initialized" flag of the ACPI device object for this purpose, which is fragile and inconvenient, and stop clearing the power_manageable flag for devices with unmet dependencies, which is not necessary.

While at it, add a debug message pringing statement to acpi_bus_init_power() to facilitate diagnostics.

...

drivers/acpi/device_pm.c int acpi_device_set_power(struct acpi_device *device, int state) · line 175

Quoted 5 lines
drivers/acpi/device_pm.c +5 -0
+	if (device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device)) {
+		device->flags.power_manageable = 0;
+		return -ENODEV;
+	}

In this form it might be harder to catch the side effect of the conditional. I would split it into two:

	if (device->power.state == ACPI_STATE_UNKNOWN) {
		int result;

		result = acpi_bus_init_power(device);
		if (result) {
			device->flags.power_manageable = 0;
			return result; // shouldn't we instead of return -ENODEV?
		}
	}

...

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2367

Quoted 4 lines
drivers/acpi/scan.c +4 -0
+	if (device->flags.power_manageable &&
+	    device->power.state == ACPI_STATE_UNKNOWN &&
+	    acpi_bus_init_power(device))
+		device->flags.power_manageable = 0;

In the similar way. And it might be even worth to have a helper to deduplicate this check and setting?

static inline int ...()

{
	int result;

	if (device->power.state != ACPI_STATE_UNKNOWN)
		return 0;

	result = acpi_bus_init_power(device);
	if (result)
		device->flags.power_manageable = 0;

	return result;
}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#5 lore raw

PATCH 2/7 ACPI: PM: Introduce acpi_device_init_power()

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

Two out of three callers of acpi_bus_init_power() need to clear flags.power_manageable for the target device on errors, which is somewhat cumbersome, so rename the function to __acpi_device_init_power(), add a wrapper called acpi_device_init_power() around it that will take care of the flags.power_manageable clearing, and make the two callers of acpi_bus_init_power() in question invoke that wrapper.

While at it, clean up the declaration of local variables in __acpi_device_init_power().

No intentional functional impact.

---
 drivers/acpi/device_pm.c | 22 +++++++++++++++-------
 drivers/acpi/scan.c      |  5 ++---
 include/acpi/acpi_bus.h  |  2 +-
 3 files changed, 18 insertions(+), 11 deletions(-)
drivers/acpi/device_pm.c +15 -7
index a680e6972a8c..7fd780125177 100644
@@ -169,10 +169,8 @@ int acpi_device_set_power(struct acpi_device *device, int state)
 		return -EINVAL;
 
 	if (device->power.state == ACPI_STATE_UNKNOWN &&
-	    acpi_bus_init_power(device)) {
-		device->flags.power_manageable = 0;
+	    acpi_device_init_power(device))
 		return -ENODEV;
-	}
 
 	acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
 			  acpi_power_state_string(device->power.state),
@@ -310,10 +308,9 @@ int acpi_bus_set_power(acpi_handle handle, int state)
 }
 EXPORT_SYMBOL(acpi_bus_set_power);
 
-int acpi_bus_init_power(struct acpi_device *device)
+static int __acpi_device_init_power(struct acpi_device *device)
 {
-	int state;
-	int result;
+	int result, state;
 
 	result = acpi_device_get_power(device, &state);
 	if (result)
@@ -355,6 +352,17 @@ int acpi_bus_init_power(struct acpi_device *device)
 	return 0;
 }
 
+int acpi_device_init_power(struct acpi_device *device)
+{
+	int ret;
+
+	ret = __acpi_device_init_power(device);
+	if (ret)
+		device->flags.power_manageable = 0;
+
+	return ret;
+}
+
 /**
  * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  * @device: Device object whose power state is to be fixed up.
@@ -417,7 +425,7 @@ int acpi_device_update_power(struct acpi_device *device, int *state_p)
 	int result;
 
 	if (device->power.state == ACPI_STATE_UNKNOWN) {
-		result = acpi_bus_init_power(device);
+		result = __acpi_device_init_power(device);
 		if (!result && state_p)
 			*state_p = device->power.state;
 
drivers/acpi/scan.c +2 -3
index 1ad8dffc2daf..5aa3ecb000e1 100644
@@ -2362,9 +2362,8 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 	acpi_ec_register_opregions(device);
 
 	if (device->flags.power_manageable &&
-	    device->power.state == ACPI_STATE_UNKNOWN &&
-	    acpi_bus_init_power(device))
-		device->flags.power_manageable = 0;
+	    device->power.state == ACPI_STATE_UNKNOWN)
+		acpi_device_init_power(device);
 
 	if (device->flags.visited)
 		goto ok;
include/acpi/acpi_bus.h +1 -1
index 1a45e0d521d8..b0d9057ccb8a 100644
@@ -618,7 +618,7 @@ int acpi_bus_get_status(struct acpi_device *device);
 int acpi_bus_set_power(acpi_handle handle, int state);
 const char *acpi_power_state_string(int state);
 int acpi_device_set_power(struct acpi_device *device, int state);
-int acpi_bus_init_power(struct acpi_device *device);
+int acpi_device_init_power(struct acpi_device *device);
 int acpi_device_fix_up_power(struct acpi_device *device);
 void acpi_device_fix_up_power_extended(struct acpi_device *adev);
 void acpi_device_fix_up_power_children(struct acpi_device *adev);
Signature · 1 line
2.51.0
#6 lore raw

Re: [PATCH v1 2/7] ACPI: PM: Introduce acpi_device_init_power()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

From: "Rafael J. Wysocki" <[email protected]>

Two out of three callers of acpi_bus_init_power() need to clear flags.power_manageable for the target device on errors, which is somewhat cumbersome, so rename the function to __acpi_device_init_power(), add a wrapper called acpi_device_init_power() around it that will take care of the flags.power_manageable clearing, and make the two callers of acpi_bus_init_power() in question invoke that wrapper.

Quoted 6 lines

While at it, clean up the declaration of local variables in __acpi_device_init_power().

No intentional functional impact.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v1 2/7] ACPI: PM: Introduce acpi_device_init_power()

In reply to Rafael J. Wysocki
Quoted 11 lines · Rafael J. Wysocki

Two out of three callers of acpi_bus_init_power() need to clear flags.power_manageable for the target device on errors, which is somewhat cumbersome, so rename the function to __acpi_device_init_power(), add a wrapper called acpi_device_init_power() around it that will take care of the flags.power_manageable clearing, and make the two callers of acpi_bus_init_power() in question invoke that wrapper.

While at it, clean up the declaration of local variables in __acpi_device_init_power().

...

drivers/acpi/device_pm.c int acpi_bus_set_power(acpi_handle handle, int state) · line 313

Quoted 4 lines

{

drivers/acpi/device_pm.c +1 -2
-	int state;
-	int result;
+	int result, state;

Not sure if this change is required.

Quoted 2 lines
drivers/acpi/device_pm.c +0 -0
 	result = acpi_device_get_power(device, &state);
 	if (result)

...

Quoted 1 line

+int acpi_device_init_power(struct acpi_device *device)

Ah, here is a helper!

drivers/acpi/device_pm.c int acpi_bus_init_power(struct acpi_device *device) · line 357

Quoted 2 lines
drivers/acpi/device_pm.c +2 -0
+{
+	int ret;

Elsewhere in the file it's called 'result'.

Quoted 7 lines
drivers/acpi/device_pm.c +7 -0
+
+	ret = __acpi_device_init_power(device);
+	if (ret)
+		device->flags.power_manageable = 0;
+
+	return ret;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#8 lore raw

Re: [PATCH v1 2/7] ACPI: PM: Introduce acpi_device_init_power()

Quoted 15 lines · Andy Shevchenko
Quoted 11 lines · Rafael J. Wysocki

Two out of three callers of acpi_bus_init_power() need to clear flags.power_manageable for the target device on errors, which is somewhat cumbersome, so rename the function to __acpi_device_init_power(), add a wrapper called acpi_device_init_power() around it that will take care of the flags.power_manageable clearing, and make the two callers of acpi_bus_init_power() in question invoke that wrapper.

While at it, clean up the declaration of local variables in __acpi_device_init_power().

Quoted 8 lines

...

drivers/acpi/device_pm.c int acpi_bus_set_power(acpi_handle handle, int state) · line 313

Quoted 4 lines

{

drivers/acpi/device_pm.c +1 -2
-     int state;
-     int result;
+     int result, state;

Not sure if this change is required.

Nope, but I think that it's useful.

Quoted 8 lines
Quoted 2 lines
      result = acpi_device_get_power(device, &state);
      if (result)

...

Quoted 1 line

+int acpi_device_init_power(struct acpi_device *device)

Ah, here is a helper!

Yeah, I guess it's better to fold it into the first patch.

Quoted 4 lines

drivers/acpi/device_pm.c int acpi_bus_init_power(struct acpi_device *device) · line 357

Quoted 2 lines
drivers/acpi/device_pm.c +2 -0
+{
+     int ret;

Elsewhere in the file it's called 'result'.

Yes, it is.

So I guess you're suggesting to also use that name here, which is fair enough.

Quoted 7 lines
Quoted 7 lines
drivers/acpi/device_pm.c +7 -0
+
+     ret = __acpi_device_init_power(device);
+     if (ret)
+             device->flags.power_manageable = 0;
+
+     return ret;
+}
author maintainer
#9 lore raw

PATCH 3/7 ACPI: bus: Drop initialized flag from struct acpi_device_flags

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

After previous changes, the ACPI device object "initialized" flag is not particularly useful. It is set almost all the time and it is only checked along with the "visited" flag, but checking the latter is sufficient.

Drop it accordingly.

---
 drivers/acpi/scan.c     | 2 --
 include/acpi/acpi_bus.h | 5 ++---
 2 files changed, 2 insertions(+), 5 deletions(-)
drivers/acpi/scan.c +0 -2
index 5aa3ecb000e1..019a43e3b5d7 100644
@@ -281,7 +281,6 @@ static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p)
 	 * that.
 	 */
 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
-	adev->flags.initialized = false;
 
 	/* For eject this is deferred to acpi_bus_post_eject() */
 	if (!(flags & ACPI_SCAN_CHECK_FLAG_EJECT)) {
@@ -1827,7 +1826,6 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
 	acpi_set_pnp_ids(handle, &device->pnp, type);
 	acpi_init_properties(device);
 	acpi_bus_get_flags(device);
-	device->flags.initialized = true;
 	device->flags.enumeration_by_parent =
 		acpi_device_enumeration_by_parent(device);
 	acpi_device_clear_enumerated(device);
include/acpi/acpi_bus.h +2 -3
index b0d9057ccb8a..596fbd748e88 100644
@@ -180,7 +180,6 @@ struct acpi_device_flags {
 	u32 removable:1;
 	u32 ejectable:1;
 	u32 power_manageable:1;
-	u32 initialized:1;
 	u32 visited:1;
 	u32 hotplug_notify:1;
 	u32 is_dock_station:1;
@@ -189,7 +188,7 @@ struct acpi_device_flags {
 	u32 cca_seen:1;
 	u32 enumeration_by_parent:1;
 	u32 honor_deps:1;
-	u32 reserved:19;
+	u32 reserved:20;
 };
 
 /* File System */
@@ -654,7 +653,7 @@ void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
 
 static inline bool acpi_device_enumerated(struct acpi_device *adev)
 {
-	return adev && adev->flags.initialized && adev->flags.visited;
+	return adev && adev->flags.visited;
 }
 
 /*
Signature · 1 line
2.51.0
#10 lore raw

Re: [PATCH v1 3/7] ACPI: bus: Drop initialized flag from struct acpi_device_flags

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing the `initialized` flag causes the device enumeration check to remain true after a failed…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the initialized flag causes the device enumeration check to remain true after a failed hot-remove eject, leading to a double-detach and a NULL pointer dereference (e.g., in acpi_pci_root_remove()) upon subsequent status checks. --

commit 8f4c1e701ec6dd8150885a28ad18d087926defd7
Author: Rafael J. Wysocki <[email protected]>

ACPI: bus: Drop initialized flag from struct acpi_device_flags

After previous changes, the ACPI device object "initialized" flag is not particularly useful. It is set almost all the time and it is only checked along with the "visited" flag, but checking the latter is sufficient. Drop it accordingly.

drivers/acpi/scan.c static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p) · line 286

Quoted 12 lines
drivers/acpi/scan.c +0 -1
index 5aa3ecb000e14..019a43e3b5d7f 100644
@@ -281,7 +281,6 @@ static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p)
 	 * that.
 	 */
 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
-	adev->flags.initialized = false;
 
 	/* For eject this is deferred to acpi_bus_post_eject() */
 	if (!(flags & ACPI_SCAN_CHECK_FLAG_EJECT)) {

[Severity: High]
Could removing this state barrier cause a double-detach and a NULL pointer dereference if a hot-remove eject fails?

When a hot-remove eject operation is initiated (ACPI_SCAN_CHECK_FLAG_EJECT), acpi_scan_check_and_detach() executes handler->detach(adev).

For a PCI root device, acpi_pci_root_remove() frees the acpi_pci_root structure and sets device->driver_data = NULL. At this stage, adev->handler and adev->flags.visited are intentionally left uncleared because they are deferred to acpi_bus_post_eject().

If the firmware eject (_EJ0) fails, acpi_scan_hot_remove() aborts and acpi_bus_post_eject() is skipped. This leaves the device in the tree with adev->handler attached and visited = 1.

Before this patch, adev->flags.initialized = false was unconditionally set during the eject phase, so a subsequent status check would evaluate acpi_device_enumerated() to false and safely abort.

By removing the initialized flag, the status check relies solely on visited. This bypasses the early return and executes handler->detach(adev) a second time.

Handlers like acpi_pci_root_remove() do not check if acpi_driver_data() is NULL on a second pass, and will crash when dereferencing root->bus:

drivers/acpi/pci_root.c:acpi_pci_root_remove() {
    ...
    struct acpi_pci_root *root = acpi_driver_data(device);
    
    pci_lock_rescan_remove();
    pci_stop_root_bus(root->bus);
    ...
}

[ ... ]

include/acpi/acpi_bus.h void acpi_set_modalias(struct acpi_device *adev, const char *default_id, · line 657

Quoted 11 lines
include/acpi/acpi_bus.h +1 -1
index b0d9057ccb8ab..596fbd748e88d 100644
@@ -654,7 +653,7 @@ void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
 
 static inline bool acpi_device_enumerated(struct acpi_device *adev)
 {
-	return adev && adev->flags.initialized && adev->flags.visited;
+	return adev && adev->flags.visited;
 }

[Severity: High]
Does this change bypass the protection that initialized provided during failed hot-remove operations as described above?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#11 lore raw

PATCH 4/7 ACPI: scan: Combine two conditionals in acpi_bus_attach()

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

There are two conditionals in acpi_bus_attach() that can be combined, which slightly reduces the overhead and makes the code a bit easier to follow, so do that.

No intentional functional impact.

---
 drivers/acpi/scan.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/acpi/scan.c +3 -8
index 019a43e3b5d7..8c5a2fcef582 100644
@@ -2370,16 +2370,11 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 	if (ret < 0)
 		return 0;
 
-	if (ret > 0 && !device->flags.enumeration_by_parent) {
+	if (!device->flags.enumeration_by_parent && (ret > 0 ||
+	    (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
 		acpi_device_set_enumerated(device);
-		goto ok;
-	}
-
-	if (device->pnp.type.platform_id || device->pnp.type.backlight ||
-	    device->flags.enumeration_by_parent)
-		acpi_default_enumeration(device);
 	else
-		acpi_device_set_enumerated(device);
+		acpi_default_enumeration(device);
 
 ok:
 	acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
Signature · 1 line
2.51.0
#12 lore raw

Re: [PATCH v1 4/7] ACPI: scan: Combine two conditionals in acpi_bus_attach()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

From: "Rafael J. Wysocki" <[email protected]>

There are two conditionals in acpi_bus_attach() that can be combined, which slightly reduces the overhead and makes the code a bit easier to follow, so do that.

No intentional functional impact.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#13 lore raw

Re: [PATCH v1 4/7] ACPI: scan: Combine two conditionals in acpi_bus_attach()

In reply to Rafael J. Wysocki
Quoted 5 lines · Rafael J. Wysocki

There are two conditionals in acpi_bus_attach() that can be combined, which slightly reduces the overhead and makes the code a bit easier to follow, so do that.

No intentional functional impact.

...

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2377

Quoted 13 lines
drivers/acpi/scan.c +3 -8
-	if (ret > 0 && !device->flags.enumeration_by_parent) {
+	if (!device->flags.enumeration_by_parent && (ret > 0 ||
+	    (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
 		acpi_device_set_enumerated(device);
-		goto ok;
-	}
-
-	if (device->pnp.type.platform_id || device->pnp.type.backlight ||
-	    device->flags.enumeration_by_parent)
-		acpi_default_enumeration(device);
 	else
-		acpi_device_set_enumerated(device);
+		acpi_default_enumeration(device);

I would leave a longer line (having logical split)

	if (!device->flags.enumeration_by_parent &&
	    (ret > 0 || (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
		acpi_device_set_enumerated(device);
	else
		acpi_default_enumeration(device);

Or even going further and cleaning too many negations (if I'm not mistaken in the logic)

	if (device->flags.enumeration_by_parent ||
// not sure what the expected ret values here, maybe < 0 or == 0 part is not needed
	    (ret <= 0 && (device->pnp.type.platform_id || device->pnp.type.backlight)))
		acpi_default_enumeration(device);
	else
		acpi_device_set_enumerated(device);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#14 lore raw

Re: [PATCH v1 4/7] ACPI: scan: Combine two conditionals in acpi_bus_attach()

Quoted 33 lines · Andy Shevchenko
Quoted 5 lines · Rafael J. Wysocki

There are two conditionals in acpi_bus_attach() that can be combined, which slightly reduces the overhead and makes the code a bit easier to follow, so do that.

No intentional functional impact.

...

drivers/acpi/scan.c static int acpi_bus_attach(struct acpi_device *device, void *first_pass) · line 2377

Quoted 13 lines
drivers/acpi/scan.c +3 -8
-     if (ret > 0 && !device->flags.enumeration_by_parent) {
+     if (!device->flags.enumeration_by_parent && (ret > 0 ||
+         (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
              acpi_device_set_enumerated(device);
-             goto ok;
-     }
-
-     if (device->pnp.type.platform_id || device->pnp.type.backlight ||
-         device->flags.enumeration_by_parent)
-             acpi_default_enumeration(device);
      else
-             acpi_device_set_enumerated(device);
+             acpi_default_enumeration(device);

I would leave a longer line (having logical split)

        if (!device->flags.enumeration_by_parent &&
            (ret > 0 || (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
                acpi_device_set_enumerated(device);
        else
                acpi_default_enumeration(device);
Quoted 5 lines

Or even going further and cleaning too many negations (if I'm not mistaken in the logic)

        if (device->flags.enumeration_by_parent ||

// not sure what the expected ret values here, maybe < 0 or == 0 part is not needed

!ret should be fine.

Quoted 4 lines
            (ret <= 0 && (device->pnp.type.platform_id || device->pnp.type.backlight)))
                acpi_default_enumeration(device);
        else
                acpi_device_set_enumerated(device);

Yes, it looks better this way.

author maintainer
#15 lore raw

PATCH 5/7 ACPI: scan: Add ACPI device "enumerated" marker

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

Currently, the "visited" flag of ACPI device objects has two roles. One of them is to indicate that the given ACPI device object has been enumerated, which basically means that either it has been associated with a "physical" device representation, or there is no matching "physical" device representation for it. The other one is to indicate that acpi_bus_attach() has processed the device. That dual purpose leads to some confusion regarding when that flag should be set and cleared and by whom.

To address that confusion, add a new bool field called "enumerated" to struct acpi_device for the purpose of indicating whether or not the given ACPI device object has been enumerated. Accordingly, switch over acpi_device_enumerated() and acpi_device_set/clear_enumerated() to operate on that field and the "visited" flag will now be only used internally by the core ACPI device enumeration code.

Namely, acpi_bus_attach() will set flags.visited for a given ACPI device object after it has been completely processed by that function and regardless of its current status it needs to be "unattached", for example by acpi_scan_check_and_detach(), so that acpi_bus_attach() can process it again.

The rule regarding flags.visited and the "enumerated" field is that the latter cannot be set until the former has been set and they need to be cleared in reverse order.

This allows a couple of checks to be dropped from acpi_bus_attach() and its first_pass argument is not needed any more.

---
 drivers/acpi/scan.c     | 28 +++++++++++++---------------
 include/acpi/acpi_bus.h |  3 ++-
 include/linux/acpi.h    |  4 ++--
 3 files changed, 17 insertions(+), 18 deletions(-)
drivers/acpi/scan.c +13 -15
index 8c5a2fcef582..153c66ca9217 100644
@@ -266,8 +266,8 @@ static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p)
 		if (acpi_device_is_enabled(adev))
 			return 0;
 
-		/* Skip device that have not been enumerated. */
-		if (!acpi_device_enumerated(adev)) {
+		/* Skip device that have not been prepared for enumeration. */
+		if (!adev->flags.visited) {
 			dev_dbg(&adev->dev, "Still not enumerated\n");
 			return 0;
 		}
@@ -286,6 +286,7 @@ static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p)
 	if (!(flags & ACPI_SCAN_CHECK_FLAG_EJECT)) {
 		adev->handler = NULL;
 		acpi_device_clear_enumerated(adev);
+		adev->flags.visited = 0;
 	}
 	return 0;
 }
@@ -304,6 +305,7 @@ static int acpi_bus_post_eject(struct acpi_device *adev, void *not_used)
 	}
 
 	acpi_device_clear_enumerated(adev);
+	adev->flags.visited = 0;
 
 	return 0;
 }
@@ -2336,26 +2338,23 @@ static int acpi_scan_attach_handler(struct acpi_device *device)
 	return ret;
 }
 
-static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
+static int acpi_bus_attach(struct acpi_device *device, void *not_used)
 {
-	bool skip = !first_pass && device->flags.visited;
 	acpi_handle ejd;
+	bool skip;
 	int ret;
 
+	skip = !!device->flags.visited;
 	if (skip)
-		goto ok;
+		goto children;
 
 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
 		register_dock_dependent_device(device, ejd);
 
 	acpi_bus_get_status(device);
 	/* Skip devices that are not ready for enumeration (e.g. not present) */
-	if (!acpi_dev_ready_for_enumeration(device)) {
-		acpi_device_clear_enumerated(device);
+	if (!acpi_dev_ready_for_enumeration(device))
 		return 0;
-	}
-	if (device->handler)
-		goto ok;
 
 	acpi_ec_register_opregions(device);
 
@@ -2363,21 +2362,20 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
 	    device->power.state == ACPI_STATE_UNKNOWN)
 		acpi_device_init_power(device);
 
-	if (device->flags.visited)
-		goto ok;
-
 	ret = acpi_scan_attach_handler(device);
 	if (ret < 0)
 		return 0;
 
+	device->flags.visited = 1;
+
 	if (!device->flags.enumeration_by_parent && (ret > 0 ||
 	    (!device->pnp.type.platform_id && !device->pnp.type.backlight)))
 		acpi_device_set_enumerated(device);
 	else
 		acpi_default_enumeration(device);
 
-ok:
-	acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
+children:
+	acpi_dev_for_each_child(device, acpi_bus_attach, NULL);
 
 	if (!skip && device->handler && device->handler->hotplug.notify_online)
 		device->handler->hotplug.notify_online(device);
include/acpi/acpi_bus.h +2 -1
index 596fbd748e88..4392e3d6cd7a 100644
@@ -467,6 +467,7 @@ struct acpi_device {
 	struct list_head physical_node_list;
 	struct mutex physical_node_lock;
 	void (*remove)(struct acpi_device *);
+	bool enumerated;
 };
 
 /* Non-device subnode */
@@ -653,7 +654,7 @@ void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
 
 static inline bool acpi_device_enumerated(struct acpi_device *adev)
 {
-	return adev && adev->flags.visited;
+	return adev && adev->enumerated;
 }
 
 /*
include/linux/acpi.h +2 -2
index ddacac812094..2e9e49e2e665 100644
@@ -782,12 +782,12 @@ struct platform_device *acpi_create_platform_device(struct acpi_device *,
 
 static inline void acpi_device_set_enumerated(struct acpi_device *adev)
 {
-	adev->flags.visited = true;
+	adev->enumerated = true;
 }
 
 static inline void acpi_device_clear_enumerated(struct acpi_device *adev)
 {
-	adev->flags.visited = false;
+	adev->enumerated = false;
 }
 
 enum acpi_reconfig_event  {
Signature · 1 line
2.51.0
#16 lore raw

Re: [PATCH v1 5/7] ACPI: scan: Add ACPI device "enumerated" marker

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

From: "Rafael J. Wysocki" <[email protected]>

Currently, the "visited" flag of ACPI device objects has two roles. One of them is to indicate that the given ACPI device object has been enumerated, which basically means that either it has been associated with a "physical" device representation, or there is no matching "physical" device representation for it. The other one is to indicate that acpi_bus_attach() has processed the device. That dual purpose leads to some confusion regarding when that flag should be set and cleared and by whom.

To address that confusion, add a new bool field called "enumerated" to struct acpi_device for the purpose of indicating whether or not the given ACPI device object has been enumerated. Accordingly, switch over acpi_device_enumerated() and acpi_device_set/clear_enumerated() to [ ... ]

Quoted 4 lines

This allows a couple of checks to be dropped from acpi_bus_attach() and its first_pass argument is not needed any more.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#17 lore raw

PATCH 6/7 ACPI: scan: Adjust and rename acpi_bus_attach()

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

After previous changes, the second argument of acpi_bus_attach() is ignored and none of its callers checks its return value, so rename it to attach_subtree(), add a void wrapper around it called acpi_scan_attach(), and adjust its callers to invoke that wrapper.

No intentional functional impact.

---
 drivers/acpi/scan.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/acpi/scan.c +13 -6
index 153c66ca9217..517126fa2d50 100644
@@ -2338,7 +2338,7 @@ static int acpi_scan_attach_handler(struct acpi_device *device)
 	return ret;
 }
 
-static int acpi_bus_attach(struct acpi_device *device, void *not_used)
+static int attach_subtree(struct acpi_device *device, void *not_used)
 {
 	acpi_handle ejd;
 	bool skip;
@@ -2374,8 +2374,10 @@ static int acpi_bus_attach(struct acpi_device *device, void *not_used)
 	else
 		acpi_default_enumeration(device);
 
+	acpi_handle_debug(device->handle, "Scanning complete\n");
+
 children:
-	acpi_dev_for_each_child(device, acpi_bus_attach, NULL);
+	acpi_dev_for_each_child(device, attach_subtree, NULL);
 
 	if (!skip && device->handler && device->handler->hotplug.notify_online)
 		device->handler->hotplug.notify_online(device);
@@ -2383,6 +2385,11 @@ static int acpi_bus_attach(struct acpi_device *device, void *not_used)
 	return 0;
 }
 
+static void acpi_scan_attach(struct acpi_device *adev)
+{
+	attach_subtree(adev, NULL);
+}
+
 static int acpi_dev_get_next_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
 {
 	struct acpi_device **adev_p = data;
@@ -2414,7 +2421,7 @@ static void acpi_scan_clear_dep_fn(void *dev, async_cookie_t cookie)
 	struct acpi_device *adev = to_acpi_device(dev);
 
 	acpi_scan_lock_acquire();
-	acpi_bus_attach(adev, (void *)true);
+	acpi_scan_attach(adev);
 	acpi_scan_lock_release();
 
 	acpi_dev_put(adev);
@@ -2427,7 +2434,7 @@ static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
 
 	/*
 	 * Async schedule the deferred acpi_scan_clear_dep_fn() since:
-	 * - acpi_bus_attach() needs to hold acpi_scan_lock which cannot
+	 * - acpi_scan_attach() needs to run under acpi_scan_lock which cannot
 	 *   be acquired under acpi_dep_list_lock (held here)
 	 * - the deferred work at boot stage is ensured to be finished
 	 *   before userspace init task by the async_synchronize_full()
@@ -2568,7 +2575,7 @@ static void acpi_scan_postponed_branch(acpi_handle handle)
 	 */
 	acpi_mipi_init_crs_csi2_swnodes();
 
-	acpi_bus_attach(adev, NULL);
+	acpi_scan_attach(adev);
 }
 
 static void acpi_scan_postponed(void)
@@ -2725,7 +2732,7 @@ int acpi_bus_scan(acpi_handle handle)
 	acpi_mipi_scan_crs_csi2();
 	acpi_mipi_init_crs_csi2_swnodes();
 
-	acpi_bus_attach(device, (void *)true);
+	acpi_scan_attach(device);
 
 	/* Pass 2: Enumerate all of the remaining devices. */
 
Signature · 1 line
2.51.0
#18 lore raw

Re: [PATCH v1 6/7] ACPI: scan: Adjust and rename acpi_bus_attach()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: "Rafael J. Wysocki" <[email protected]>

After previous changes, the second argument of acpi_bus_attach() is ignored and none of its callers checks its return value, so rename it to attach_subtree(), add a void wrapper around it called acpi_scan_attach(), and adjust its callers to invoke that wrapper.

No intentional functional impact.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#19 lore raw

PATCH 7/7 ACPI: scan: Take PCI device enumeration into account directly

In reply to Rafael J. Wysocki

From: "Rafael J. Wysocki" <[email protected]>

The ACPI companions of PCI devices are associated with the corresponding PCI devices before being processed by acpi_scan_attach() and by the time they are passed to attach_subtree(), the PCI devices associated with them have been already enumerated and initialized (including power management initialization).

Accordingly, it is not necessary or even useful to check their status in attach_subtree(), so do not do that.

Fixes 2c22e6520ac8 ("ACPI / scan: Use direct recurrence for device hierarchy walks")
---
 drivers/acpi/scan.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
drivers/acpi/scan.c +22 -4
index 517126fa2d50..53e11cbb79ce 100644
@@ -20,6 +20,7 @@
 #include <linux/kthread.h>
 #include <linux/dmi.h>
 #include <linux/dma-map-ops.h>
+#include <linux/pci.h>
 #include <linux/platform_data/x86/apple.h>
 #include <linux/pgtable.h>
 #include <linux/crc32.h>
@@ -2340,6 +2341,7 @@ static int acpi_scan_attach_handler(struct acpi_device *device)
 
 static int attach_subtree(struct acpi_device *device, void *not_used)
 {
+	struct pci_dev *pci;
 	acpi_handle ejd;
 	bool skip;
 	int ret;
@@ -2351,10 +2353,26 @@ static int attach_subtree(struct acpi_device *device, void *not_used)
 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
 		register_dock_dependent_device(device, ejd);
 
-	acpi_bus_get_status(device);
-	/* Skip devices that are not ready for enumeration (e.g. not present) */
-	if (!acpi_dev_ready_for_enumeration(device))
-		return 0;
+	/*
+	 * If the given ACPI device object has been already associated with a
+	 * PCI device found on the bus, the device can be regarded as present
+	 * and functional.
+	 */
+	pci = acpi_dev_get_pci_dev(device);
+	if (pci) {
+		acpi_handle_debug(device->handle, "PCI companion %s found\n",
+				  dev_name(&pci->dev));
+
+		pci_dev_put(pci);
+	} else {
+		acpi_bus_get_status(device);
+		/*
+		 * Skip devices that are not ready for enumeration (e.g. not
+		 * present).
+		 */
+		if (!acpi_dev_ready_for_enumeration(device))
+			return 0;
+	}
 
 	acpi_ec_register_opregions(device);
 
Signature · 1 line
2.51.0
#20 lore raw

Re: [PATCH v1 7/7] ACPI: scan: Take PCI device enumeration into account directly

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

From: "Rafael J. Wysocki" <[email protected]>

The ACPI companions of PCI devices are associated with the corresponding PCI devices before being processed by acpi_scan_attach() and by the time they are passed to attach_subtree(), the PCI devices associated with them have been already enumerated and initialized (including power management initialization).

Quoted 5 lines

Accordingly, it is not necessary or even useful to check their status in attach_subtree(), so do not do that.

Fixes 2c22e6520ac8 ("ACPI / scan: Use direct recurrence for device hierarchy walks")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#21 lore raw

Re: [PATCH v1 7/7] ACPI: scan: Take PCI device enumeration into account directly

In reply to Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki

The ACPI companions of PCI devices are associated with the corresponding PCI devices before being processed by acpi_scan_attach() and by the time they are passed to attach_subtree(), the PCI devices associated with them have been already enumerated and initialized (including power management initialization).

Accordingly, it is not necessary or even useful to check their status in attach_subtree(), so do not do that.

...

drivers/acpi/scan.c static int attach_subtree(struct acpi_device *device, void *not_used) · line 2364

Quoted 3 lines
drivers/acpi/scan.c +3 -0
+	if (pci) {
+		acpi_handle_debug(device->handle, "PCI companion %s found\n",
+				  dev_name(&pci->dev));

We have pci_name().

Quoted 11 lines
drivers/acpi/scan.c +11 -0
+
+		pci_dev_put(pci);
+	} else {
+		acpi_bus_get_status(device);
+		/*
+		 * Skip devices that are not ready for enumeration (e.g. not
+		 * present).
+		 */
+		if (!acpi_dev_ready_for_enumeration(device))
+			return 0;
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#22 lore raw

Re: [PATCH v1 7/7] ACPI: scan: Take PCI device enumeration into account directly

Quoted 12 lines · Andy Shevchenko
Quoted 8 lines · Rafael J. Wysocki

The ACPI companions of PCI devices are associated with the corresponding PCI devices before being processed by acpi_scan_attach() and by the time they are passed to attach_subtree(), the PCI devices associated with them have been already enumerated and initialized (including power management initialization).

Accordingly, it is not necessary or even useful to check their status in attach_subtree(), so do not do that.

Quoted 7 lines

...

drivers/acpi/scan.c static int attach_subtree(struct acpi_device *device, void *not_used) · line 2364

Quoted 3 lines
drivers/acpi/scan.c +3 -0
+     if (pci) {
+             acpi_handle_debug(device->handle, "PCI companion %s found\n",
+                               dev_name(&pci->dev));

We have pci_name().

Ah, OK

Tested-by on the v1 series
#23 lore raw
In reply to Rafael J. Wysocki

Hi Rafael,

Quoted 20 lines · Rafael J. Wysocki

Hi All,

This series aims at addressing a few issues present in the core ACPI device enumeration code, mostly related to the acpi_bus_attach() function (that gets renamed in one of the patches).

The first one is that, on some systems, ACPI power management is initialized twice for devices that have missing dependencies to start with. An attempt to address this issue had been made during the 7.3 merge window, but it had to be reverted [1]. The approach used here is roughly the same as in the reverted commit, but it takes PCI devices (which are a special case) into account.

The other issues are that flags are unnecessarily cleared by acpi_bus_attach() for devices with missing dependencies, the "initialized" ACPI device object flag is not particularly useful, the "visited" ACPI device object flag is used for two different things which is confusing at best, and that PCI devices are handled by acpi_bus_attach() like any other devices which is a mistake.

Quoted 6 lines

Please see the changelogs of individual patches for details.

I have tested this series on a couple of machines, but let's see what Sashiko has to say about it.

Thanks!

I tested the complete series on the SpacemiT K3 RISC-V Pico-ITX platform where the original duplicate ACPI PowerResource reference issue was observed.

The power resource left on by firmware is now turned off as unused after the namespace scan. After the device dependency is satisfied, the power resource is turned on once for the device and is turned off normally during runtime suspend. Runtime resume also works correctly.

Signature · 2 lines
Best Regards,
Peixin Xie