[PATCH 5/8] platform/x86: thinkpad_acpi: use scoped_guard() for hotkey config locking

From: Dmitry Torokhov

Date: Wed Nov 25 2026 - 18:11:49 EST


Replace the HOTKEY_CONFIG_CRITICAL_START and HOTKEY_CONFIG_CRITICAL_END
macros and manual hotkey_thread_data_mutex lock/unlock calls with
scoped_guard(mutex, &hotkey_thread_data_mutex).

Keep the hotkey_driver_mask assignment inside the guard in
tpacpi_hotkey_driver_mask_set() when polling is enabled so updates to
hotkey_driver_mask, hotkey_source_mask, and hotkey_config_change remain
atomic with respect to hotkey_kthread().

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/platform/x86/lenovo/thinkpad_acpi.c | 74 ++++++++++++-----------------
1 file changed, 31 insertions(+), 43 deletions(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 7a82a9014a3e..58500b72d9d2 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -1819,8 +1819,6 @@ static struct task_struct *tpacpi_hotkey_task;
*
* Increment hotkey_config_change when changing them if you
* want the kthread to forget old state.
- *
- * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
*/
static struct mutex hotkey_thread_data_mutex;
static unsigned int hotkey_config_change;
@@ -1828,29 +1826,14 @@ static unsigned int hotkey_config_change;
/*
* hotkey poller control variables
*
- * Must be atomic or readers will also need to acquire mutex
- *
- * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
- * should be used only when the changes need to be taken as
- * a block, OR when one needs to force the kthread to forget
- * old state.
+ * Must be atomic or readers will also need to acquire mutex.
*/
static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
static unsigned int hotkey_poll_freq = 10; /* Hz */

-#define HOTKEY_CONFIG_CRITICAL_START \
- do { \
- mutex_lock(&hotkey_thread_data_mutex); \
- hotkey_config_change++; \
- } while (0);
-#define HOTKEY_CONFIG_CRITICAL_END \
- mutex_unlock(&hotkey_thread_data_mutex);
-
#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */

#define hotkey_source_mask 0U
-#define HOTKEY_CONFIG_CRITICAL_START
-#define HOTKEY_CONFIG_CRITICAL_END

#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */

@@ -2159,12 +2142,15 @@ static int tpacpi_hotkey_driver_mask_set(const u32 mask)

guard(mutex)(&hotkey_mutex);

- HOTKEY_CONFIG_CRITICAL_START
- hotkey_driver_mask = mask;
#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
- hotkey_source_mask |= (mask & ~hotkey_all_mask);
+ scoped_guard(mutex, &hotkey_thread_data_mutex) {
+ hotkey_driver_mask = mask;
+ hotkey_source_mask |= (mask & ~hotkey_all_mask);
+ hotkey_config_change++;
+ }
+#else
+ hotkey_driver_mask = mask;
#endif
- HOTKEY_CONFIG_CRITICAL_END

rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
~hotkey_source_mask);
@@ -2460,13 +2446,13 @@ static int hotkey_kthread(void *data)
t = 0;

/* Initial state for compares */
- mutex_lock(&hotkey_thread_data_mutex);
- change_detector = hotkey_config_change;
- poll_mask = hotkey_source_mask;
- event_mask = hotkey_source_mask &
- (hotkey_driver_mask | hotkey_user_mask);
- poll_freq = hotkey_poll_freq;
- mutex_unlock(&hotkey_thread_data_mutex);
+ scoped_guard(mutex, &hotkey_thread_data_mutex) {
+ change_detector = hotkey_config_change;
+ poll_mask = hotkey_source_mask;
+ event_mask = hotkey_source_mask &
+ (hotkey_driver_mask | hotkey_user_mask);
+ poll_freq = hotkey_poll_freq;
+ }
hotkey_read_nvram(&s[so], poll_mask);

while (!kthread_should_stop()) {
@@ -2483,18 +2469,19 @@ static int hotkey_kthread(void *data)
if (t > 0 && !was_frozen)
continue;

- mutex_lock(&hotkey_thread_data_mutex);
- if (was_frozen || hotkey_config_change != change_detector) {
- /* forget old state on thaw or config change */
- si = so;
- t = 0;
- change_detector = hotkey_config_change;
+ scoped_guard(mutex, &hotkey_thread_data_mutex) {
+ if (was_frozen ||
+ hotkey_config_change != change_detector) {
+ /* forget old state on thaw or config change */
+ si = so;
+ t = 0;
+ change_detector = hotkey_config_change;
+ }
+ poll_mask = hotkey_source_mask;
+ event_mask = hotkey_source_mask &
+ (hotkey_driver_mask | hotkey_user_mask);
+ poll_freq = hotkey_poll_freq;
}
- poll_mask = hotkey_source_mask;
- event_mask = hotkey_source_mask &
- (hotkey_driver_mask | hotkey_user_mask);
- poll_freq = hotkey_poll_freq;
- mutex_unlock(&hotkey_thread_data_mutex);

if (likely(poll_mask)) {
hotkey_read_nvram(&s[si], poll_mask);
@@ -2739,9 +2726,10 @@ static ssize_t hotkey_source_mask_store(struct device *dev,
if (rc)
return rc;

- HOTKEY_CONFIG_CRITICAL_START
- hotkey_source_mask = t;
- HOTKEY_CONFIG_CRITICAL_END
+ scoped_guard(mutex, &hotkey_thread_data_mutex) {
+ hotkey_source_mask = t;
+ hotkey_config_change++;
+ }

rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
~hotkey_source_mask);

--
2.56.1.839.g22a8290db9-goog