From: "Mark Pearson" <mpearson-lenovo@squebb.ca>
To: "Dmitry Torokhov" <dmitry.torokhov@gmail.com>,
"Henrique de Moraes Holschuh" <hmh@hmh.eng.br>,
"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"
<platform-driver-x86@vger.kernel.org>,
ibm-acpi-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/8] platform/x86: thinkpad_acpi: use scoped_guard() for hotkey config locking
Date: Thu, 26 Nov 2026 22:01:24 -0500 [thread overview]
Message-ID: <2c7da819-fb12-4504-8e15-42aa830db18f@app.fastmail.com> (raw)
In-Reply-To: <20261125-thinkpad-acpi-hotkey-poll-v1-5-6dee57519c2e@gmail.com>
On Wed, Nov 25, 2026, at 6:11 PM, Dmitry Torokhov wrote:
> 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
Looks good to me.
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Mark
next prev parent reply other threads:[~2026-11-27 3:01 UTC|newest]
Thread overview: 18+ 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-27 2:43 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 2/8] platform/x86: thinkpad_acpi: drop dead -ENXIO check in hotkey_init() Dmitry Torokhov
2026-11-27 2:46 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 3/8] platform/x86: thinkpad_acpi: simplify hotkey open/close and poll setup Dmitry Torokhov
2026-11-27 3:28 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 4/8] platform/x86: thinkpad_acpi: update polling state in hotkey_user_mask_set() Dmitry Torokhov
2026-11-27 2:57 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 5/8] platform/x86: thinkpad_acpi: use scoped_guard() for hotkey config locking Dmitry Torokhov
2026-11-27 3:01 ` Mark Pearson [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-27 3:35 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 7/8] platform/x86: thinkpad_acpi: remove unused hotkey_poll_active flag Dmitry Torokhov
2026-11-27 3:02 ` Mark Pearson
2026-11-25 23:11 ` [PATCH 8/8] platform/x86: thinkpad_acpi: drop redundant input device parent assignment Dmitry Torokhov
2026-11-27 3:05 ` Mark Pearson
2026-11-27 13:25 ` [PATCH 0/8] platform/x86: thinkpad_acpi: clean up hotkey polling and locking Rong Zhang
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=2c7da819-fb12-4504-8e15-42aa830db18f@app.fastmail.com \
--to=mpearson-lenovo@squebb.ca \
--cc=derekjohn.clark@gmail.com \
--cc=dmitry.torokhov@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=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®