From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: "Henrique de Moraes Holschuh" <hmh@hmh.eng.br>,
"Mark Pearson" <mpearson-lenovo@squebb.ca>,
"Derek J. Clark" <derekjohn.clark@gmail.com>,
"Rong Zhang" <i@rong.moe>, "Hans de Goede" <hansg@kernel.org>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Cc: platform-driver-x86@vger.kernel.org,
ibm-acpi-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org
Subject: [PATCH 5/8] platform/x86: thinkpad_acpi: use scoped_guard() for hotkey config locking
Date: Wed, 25 Nov 2026 15:11:18 -0800 [thread overview]
Message-ID: <20261125-thinkpad-acpi-hotkey-poll-v1-5-6dee57519c2e@gmail.com> (raw)
In-Reply-To: <20261125-thinkpad-acpi-hotkey-poll-v1-0-6dee57519c2e@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-25 23:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 23:11 [PATCH 0/8] platform/x86: thinkpad_acpi: clean up hotkey polling and locking Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 1/8] platform/x86: thinkpad_acpi: remove early exit in hotkey_kthread() Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 2/8] platform/x86: thinkpad_acpi: drop dead -ENXIO check in hotkey_init() Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 3/8] platform/x86: thinkpad_acpi: simplify hotkey open/close and poll setup Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 4/8] platform/x86: thinkpad_acpi: update polling state in hotkey_user_mask_set() Dmitry Torokhov
2026-11-25 23:11 ` Dmitry Torokhov [this message]
2026-11-25 23:11 ` [PATCH 6/8] platform/x86: thinkpad_acpi: convert hotkey_thread_data_mutex to a spinlock Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 7/8] platform/x86: thinkpad_acpi: remove unused hotkey_poll_active flag Dmitry Torokhov
2026-11-25 23:11 ` [PATCH 8/8] platform/x86: thinkpad_acpi: drop redundant input device parent assignment Dmitry Torokhov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125-thinkpad-acpi-hotkey-poll-v1-5-6dee57519c2e@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=derekjohn.clark@gmail.com \
--cc=hansg@kernel.org \
--cc=hmh@hmh.eng.br \
--cc=i@rong.moe \
--cc=ibm-acpi-devel@lists.sourceforge.net \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mpearson-lenovo@squebb.ca \
--cc=platform-driver-x86@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®