ACPI: button: Report wakeup key only for power button wakeups
Subsystems (2)
PATCH v9 1/1 ACPI: button: Report wakeup key only for power button wakeups
The ACPI button driver reports KEY_WAKEUP from the Power Button input device to let userspace know that the system was resumed by a power button wakeup.
However, reporting KEY_WAKEUP from generic system resume paths can make userspace observe a Power Button wakeup even when the system was resumed by a different wake source.
This is reproducible on an AMD Android 15 Xen guest. With a kernel without this fix, a non-power-button S3 resume:
echo mem > /sys/power/state
xl trigger android s3resume
makes the Power Button input device report KEY_WAKEUP. The same test on a kernel with this fix no longer reports KEY_WAKEUP from the Power Button input device.
Track whether a power button event/notify is observed while the ACPI button device is suspended, and report KEY_WAKEUP on resume only in that case.
When the fixed power-button status bit is set during early S3 resume, clear it to avoid a deferred KEY_POWER delivery to userspace, and set a sleep.c-owned flag so button resume can still report KEY_WAKEUP. Do not call into the button driver from that IRQ-disabled path. Only the fixed power button (ACPI_BUS_TYPE_POWER_BUTTON) consumes that flag. Sample the button driver's wakeup_pending separately and call the helper even when wakeup_pending is already set, so a short-circuit does not leave the flag set.
Order the suspended and wakeup_pending updates with SMP barriers. Clear wakeup_pending before setting suspended, and publish suspended == false with a release store before dropping a wakeup_pending that arrived in between. Sample suspended once in the notify path so the handler cannot emit both KEY_WAKEUP and KEY_POWER for one event. Drain ACPI notify work before sampling on resume.
---
Changes since v8:
- Consume acpi_sleep_pwr_btn_wakeup_pending() before combining it with wakeup_pending, and only from the fixed power button, so the sleep.c flag cannot stay set or be taken by a control-method power button.
- Pair suspended and wakeup_pending with smp_load_acquire, smp_store_release, smp_wmb, and smp_mb. Sample suspended once in acpi_button_notify.
- Add Fixes: 16f70feaabe9 ("ACPI: button: trigger wakeup key events") and keep the version changelog below ---.
Changes since v7:
- Guard acpi_sleep_pwr_btn_wakeup_pending() with CONFIG_ACPI_SLEEP, matching its definition in sleep.c, so button resume does not link against a missing symbol when CONFIG_PM_SLEEP=y and CONFIG_ACPI_SLEEP is unset (0day/LKP ERROR on x86_64 randconfig).
Changes since v6:
- Include <linux/types.h> in include/acpi/button.h so the bool declaration/stub of acpi_sleep_pwr_btn_wakeup_pending() builds when the header is included without prior bool typedefs (0day/LKP ERROR via nouveau_connector.c on parisc64-defconfig and arm64-randconfig).
--- drivers/acpi/button.c | 83 ++++++++++++++++++++++++++++++++++++++----- drivers/acpi/sleep.c | 47 ++++++++++++++++-------- include/acpi/button.h | 19 ++++++++++ 3 files changed, 126 insertions(+), 23 deletions(-)
drivers/acpi/button.c +75 -8
@@ -17,6 +17,7 @@#include <linux/seq_file.h> #include <linux/input.h> #include <linux/slab.h> +#include <linux/spinlock.h> #include <linux/acpi.h> #include <linux/dmi.h> #include <linux/platform_device.h>
@@ -203,6 +204,7 @@ struct acpi_button {bool last_state;ktime_t last_time;bool suspended; +bool wakeup_pending;bool lid_state_initialized;bool gpe_enabled; };
@@ -474,6 +476,7 @@ static void acpi_button_notify(acpi_handle handle, u32 event, void *data)struct acpi_device *device = button->adev;struct input_dev *input;int keycode; +bool suspended;switch (event) {case ACPI_BUTTON_NOTIFY_STATUS :
@@ -488,7 +491,16 @@ static void acpi_button_notify(acpi_handle handle, u32 event, void *data)acpi_pm_wakeup_event (button->dev); -if (button->suspended || event ==ACPI_BUTTON_NOTIFY_WAKE ) +/* +* Sample suspended once. Two separate READ_ONCE() calls can observe +* different values if resume runs on another CPU, and then this path +* either drops the event or emits both KEY_WAKEUP and KEY_POWER. +*/ + suspended =smp_load_acquire (&button->suspended); +if (button->type ==ACPI_BUTTON_TYPE_POWER && suspended) +smp_store_release (&button->wakeup_pending, true); + +if (suspended || event ==ACPI_BUTTON_NOTIFY_WAKE )return ; input = button->input;
@@ -510,6 +522,12 @@ static void acpi_button_notify_run(void *data)static u32 acpi_button_event (void *data) { +struct acpi_button *button = data; + +if (button->type ==ACPI_BUTTON_TYPE_POWER && +smp_load_acquire (&button->suspended)) +smp_store_release (&button->wakeup_pending, true); +acpi_os_execute (OSL_NOTIFY_HANDLER , acpi_button_notify_run, data);return ACPI_INTERRUPT_HANDLED ; }
@@ -519,7 +537,15 @@ static int acpi_button_suspend(struct device *dev) {struct acpi_button *button =dev_get_drvdata (dev); - button->suspended = true; +/* +* Clear wakeup_pending before marking suspended. Otherwise a power +* button SCI between the two stores could set wakeup_pending and then +* be overwritten here, losing a real wakeup. smp_wmb() makes that +* order visible to the SCI and notify handlers on other CPUs. +*/ +WRITE_ONCE (button->wakeup_pending, false); +smp_wmb (); +WRITE_ONCE (button->suspended, true);return 0 ; }
@@ -528,20 +554,61 @@ static int acpi_button_resume(struct device *dev)struct acpi_button *button =dev_get_drvdata (dev);struct input_dev *input; - button->suspended = false; -if (button->type ==ACPI_BUTTON_TYPE_LID ) { - button->last_state = !!acpi_lid_evaluate_state (ACPI_HANDLE (dev)); - button->last_time =ktime_get (); -acpi_lid_initialize_state (button); - } +/* +* Drain notify work queued while suspended so a late power-button +* notify still observes suspended == true, sets wakeup_pending, and +* does not synthesize KEY_POWER for userspace. +*/ +acpi_os_wait_events_complete (); +/* +* Consume the fixed-button flag before combining it with +* wakeup_pending. A short-circuit || would skip +* acpi_sleep_pwr_btn_wakeup_pending() when wakeup_pending is already +* true and leave the flag set for a later non-button resume. Only the +* fixed power button owns that flag. +* +* Publish suspended == false with a release store, then sample +* wakeup_pending again. A press that arrived after the first sample +* still observed suspended == true and must be reported as KEY_WAKEUP +* before the flag is cleared. +*/ if (button->type ==ACPI_BUTTON_TYPE_POWER ) { +bool from_button =READ_ONCE (button->wakeup_pending); +bool from_fixed = false; + +if (button->adev->device_type ==ACPI_BUS_TYPE_POWER_BUTTON ) + from_fixed =acpi_sleep_pwr_btn_wakeup_pending (); + +if (from_button || from_fixed) { + input = button->input; +input_report_key (input,KEY_WAKEUP ,1 ); +input_sync (input); +input_report_key (input,KEY_WAKEUP ,0 ); +input_sync (input); + } +/* Drop the sample already reported, before publishing resume. */ +WRITE_ONCE (button->wakeup_pending, false); + } + +smp_store_release (&button->suspended, false); +smp_mb (); +if (button->type ==ACPI_BUTTON_TYPE_POWER && +smp_load_acquire (&button->wakeup_pending)) { input = button->input;input_report_key (input,KEY_WAKEUP ,1 );input_sync (input);input_report_key (input,KEY_WAKEUP ,0 );input_sync (input); +WRITE_ONCE (button->wakeup_pending, false); } + +if (button->type ==ACPI_BUTTON_TYPE_LID ) { + button->last_state = !!acpi_lid_evaluate_state (ACPI_HANDLE (dev)); + button->last_time =ktime_get (); +acpi_lid_initialize_state (button); + } + return 0; }#endif
drivers/acpi/sleep.c +32 -15
78 lines not on this page · raw message
include/acpi/button.h +19 -0
@@ -2,6 +2,8 @@#ifndef ACPI_BUTTON_H #define ACPI_BUTTON_H +#include <linux/types.h> /* for bool */ +#define ACPI_BUTTON_HID_POWER "PNP0C0C"#define ACPI_BUTTON_HID_LID "PNP0C0D"#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
@@ -15,4 +17,21 @@ static inline int acpi_lid_open(void) }#endif /* IS_ENABLED(CONFIG_ACPI_BUTTON) */ +/* +* Set in acpi_suspend_enter() when the fixed power-button status bit is +* observed (IRQ-disabled context). Consumed by the button driver on resume +* so KEY_WAKEUP can be reported without calling into the button driver from +* that early path. Defined in sleep.c under CONFIG_ACPI_SLEEP. CONFIG_PM_SLEEP +* can be enabled without CONFIG_ACPI_SLEEP, so the stub keeps button.o +* linkable in that case. Safe when CONFIG_ACPI_BUTTON=m. +*/ +#if IS_ENABLED (CONFIG_ACPI_SLEEP ) +bool acpi_sleep_pwr_btn_wakeup_pending (void ); +#else +static inline bool acpi_sleep_pwr_btn_wakeup_pending (void ) +{ +return false; +} +#endif +#endif /* ACPI_BUTTON_H */
base-commit: 0889fa4ceb57abcd36535bd17cfcadce893e6e13
Signature · 1 line
2.34.1