Skip to content

Commit a2d7b2e

Browse files
committed
ACPI: PM: Fix sharing of wakeup power resources
If an ACPI wakeup power resource is shared between multiple devices, it may not be managed correctly. Suppose, for example, that two devices, A and B, share a wakeup power resource P whose wakeup_enabled flag is 0 initially. Next, suppose that wakeup power is enabled for A and B, in this order, and disabled for B. When wakeup power is enabled for A, P will be turned on and its wakeup_enabled flag will be set. Next, when wakeup power is enabled for B, P will not be touched, because its wakeup_enabled flag is set. Now, when wakeup power is disabled for B, P will be turned off which is incorrect, because A will still need P in order to signal wakeup. Moreover, if wakeup power is enabled for A and then disabled for B, the latter will cause P to be turned off incorrectly (it will be still needed by A), because acpi_disable_wakeup_device_power() is allowed to manipulate power resources when the wakeup.prepare_count counter of the given device is 0. While the first issue could be addressed by changing the wakeup_enabled power resource flag into a counter, addressing the second one requires modifying acpi_disable_wakeup_device_power() to do nothing when the target device's wakeup.prepare_count reference counter is zero and that would cause the new counter to be redundant. Namely, if acpi_disable_wakeup_device_power() is modified as per the above, every change of the new counter following a wakeup.prepare_count change would be reflected by the analogous change of the main reference counter of the given power resource. Accordingly, modify acpi_disable_wakeup_device_power() to do nothing when the target device's wakeup.prepare_count reference counter is zero and drop the power resource wakeup_enabled flag altogether. While at it, ensure that all of the power resources that can be turned off will be turned off when disabling device wakeup due to a power resource manipulation error, to prevent energy from being wasted. Fixes: b5d667e ("ACPI / PM: Take unusual configurations of power resources into account") Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 7a63296 commit a2d7b2e

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed

drivers/acpi/power.c

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ struct acpi_power_resource {
5252
u32 order;
5353
unsigned int ref_count;
5454
u8 state;
55-
bool wakeup_enabled;
5655
struct mutex resource_lock;
5756
struct list_head dependents;
5857
};
@@ -710,7 +709,6 @@ int acpi_device_sleep_wake(struct acpi_device *dev,
710709
*/
711710
int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
712711
{
713-
struct acpi_power_resource_entry *entry;
714712
int err = 0;
715713

716714
if (!dev || !dev->wakeup.flags.valid)
@@ -721,26 +719,13 @@ int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
721719
if (dev->wakeup.prepare_count++)
722720
goto out;
723721

724-
list_for_each_entry(entry, &dev->wakeup.resources, node) {
725-
struct acpi_power_resource *resource = entry->resource;
726-
727-
mutex_lock(&resource->resource_lock);
728-
729-
if (!resource->wakeup_enabled) {
730-
err = acpi_power_on_unlocked(resource);
731-
if (!err)
732-
resource->wakeup_enabled = true;
733-
}
734-
735-
mutex_unlock(&resource->resource_lock);
736-
737-
if (err) {
738-
dev_err(&dev->dev,
739-
"Cannot turn wakeup power resources on\n");
740-
dev->wakeup.flags.valid = 0;
741-
goto out;
742-
}
722+
err = acpi_power_on_list(&dev->wakeup.resources);
723+
if (err) {
724+
dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
725+
dev->wakeup.flags.valid = 0;
726+
goto out;
743727
}
728+
744729
/*
745730
* Passing 3 as the third argument below means the device may be
746731
* put into arbitrary power state afterward.
@@ -770,39 +755,33 @@ int acpi_disable_wakeup_device_power(struct acpi_device *dev)
770755

771756
mutex_lock(&acpi_device_lock);
772757

773-
if (--dev->wakeup.prepare_count > 0)
758+
if (dev->wakeup.prepare_count > 1) {
759+
dev->wakeup.prepare_count--;
774760
goto out;
761+
}
775762

776-
/*
777-
* Executing the code below even if prepare_count is already zero when
778-
* the function is called may be useful, for example for initialisation.
779-
*/
780-
if (dev->wakeup.prepare_count < 0)
781-
dev->wakeup.prepare_count = 0;
763+
/* Do nothing if wakeup power has not been enabled for this device. */
764+
if (!dev->wakeup.prepare_count)
765+
goto out;
782766

783767
err = acpi_device_sleep_wake(dev, 0, 0, 0);
784768
if (err)
785769
goto out;
786770

771+
/*
772+
* All of the power resources in the list need to be turned off even if
773+
* there are errors.
774+
*/
787775
list_for_each_entry(entry, &dev->wakeup.resources, node) {
788-
struct acpi_power_resource *resource = entry->resource;
789-
790-
mutex_lock(&resource->resource_lock);
791-
792-
if (resource->wakeup_enabled) {
793-
err = acpi_power_off_unlocked(resource);
794-
if (!err)
795-
resource->wakeup_enabled = false;
796-
}
797-
798-
mutex_unlock(&resource->resource_lock);
776+
int ret;
799777

800-
if (err) {
801-
dev_err(&dev->dev,
802-
"Cannot turn wakeup power resources off\n");
803-
dev->wakeup.flags.valid = 0;
804-
break;
805-
}
778+
ret = acpi_power_off(entry->resource);
779+
if (ret && !err)
780+
err = ret;
781+
}
782+
if (err) {
783+
dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
784+
dev->wakeup.flags.valid = 0;
806785
}
807786

808787
out:

0 commit comments

Comments
 (0)