* [PATCH] Input: cyapa - publish PIP command state to the IRQ handler
@ 2026-11-30 3:19 Gui-Dong Han
2026-11-30 23:45 ` Dmitry Torokhov
0 siblings, 1 reply; 2+ messages in thread
From: Gui-Dong Han @ 2026-11-30 3:19 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Dudley Du, Sakari Ailus, linux-input, linux-kernel, Gui-Dong Han,
baijiaju1990
cyapa_i2c_pip_cmd_irq_sync() sets is_irq_mode and the response parameters
before cyapa_do_i2c_pip_cmd_irq_sync() or cyapa_do_i2c_pip_cmd_polling()
increments cmd_issued. cyapa_pip_irq_cmd_handler() reads cmd_issued
without holding cmd_lock to decide whether to inspect that state.
However, atomic_inc() and atomic_read() are unordered. On weakly ordered
architectures, cyapa_pip_irq_cmd_handler() can observe a nonzero
cmd_issued before the updates to is_irq_mode or the response parameters
become visible, causing it to misread the command mode or use stale
response state.
Add smp_wmb() before atomic_inc(&pip->cmd_issued) in both command paths,
and use atomic_read_acquire(&pip->cmd_issued) in
cyapa_pip_irq_cmd_handler() to ensure the setup stores are visible once a
nonzero count is observed.
Fixes: 6972a859601a ("Input: cyapa - add gen5 trackpad device basic functions support")
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
---
Found by auditing atomic operations used for synchronization.
A similar fix can be found in 6df8e84aa6b5.
---
drivers/input/mouse/cyapa_gen5.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/input/mouse/cyapa_gen5.c b/drivers/input/mouse/cyapa_gen5.c
index 59f6e97d5482..8f5cec948878 100644
--- a/drivers/input/mouse/cyapa_gen5.c
+++ b/drivers/input/mouse/cyapa_gen5.c
@@ -544,6 +544,8 @@ static int cyapa_do_i2c_pip_cmd_irq_sync(
/* Wait for interrupt to set ready completion */
init_completion(&pip->cmd_ready);
+ /* Publish command state to cyapa_pip_irq_cmd_handler(). */
+ smp_wmb();
atomic_inc(&pip->cmd_issued);
error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
if (error) {
@@ -574,6 +576,8 @@ static int cyapa_do_i2c_pip_cmd_polling(
int length;
int error;
+ /* Publish command state to cyapa_pip_irq_cmd_handler(). */
+ smp_wmb();
atomic_inc(&pip->cmd_issued);
error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
if (error) {
@@ -2608,7 +2612,7 @@ bool cyapa_pip_irq_cmd_handler(struct cyapa *cyapa)
struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
int length;
- if (atomic_read(&pip->cmd_issued)) {
+ if (atomic_read_acquire(&pip->cmd_issued)) {
/* Polling command response data. */
if (pip->is_irq_mode == false)
return false;
base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] Input: cyapa - publish PIP command state to the IRQ handler
2026-11-30 3:19 [PATCH] Input: cyapa - publish PIP command state to the IRQ handler Gui-Dong Han
@ 2026-11-30 23:45 ` Dmitry Torokhov
0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Torokhov @ 2026-11-30 23:45 UTC (permalink / raw)
To: Gui-Dong Han
Cc: Dudley Du, Sakari Ailus, linux-input, linux-kernel, baijiaju1990
Hi Gui-Dong,
On Mon, Nov 30, 2026 at 11:19:27AM +0800, Gui-Dong Han wrote:
> Add smp_wmb() before atomic_inc(&pip->cmd_issued) in both command paths,
> and use atomic_read_acquire(&pip->cmd_issued) in
> cyapa_pip_irq_cmd_handler() to ensure the setup stores are visible once a
> nonzero count is observed.
>
> Fixes: 6972a859601a ("Input: cyapa - add gen5 trackpad device basic functions support")
> Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
Pairing a raw smp_wmb() plus atomic_inc() with atomic_read_acquire() is
pretty awkward and asymmetric, and since commands are already serialized
by cmd_lock, cmd_issued is really just a boolean flag rather than a
counter.
More importantly, adding barriers only papers over a bigger race on the
error and timeout paths. cyapa_irq() is a threaded IRQ that sleeps in
I2C reads inside cyapa_pip_irq_cmd_handler(). If a command write fails
or times out while the handler is still running, the sender decrements
cmd_issued, clears the response pointers, and returns (with the caller's
stack response buffers going out of scope) without synchronizing with
the IRQ thread. The handler can then dereference cleared/freed response
pointers and run a second atomic_dec(), driving cmd_issued to -1 and
permanently breaking touch reporting.
Since cyapa_irq() already runs in thread context, I would prefer to drop
the lockless atomic_t and manual barriers altogether and replace
cmd_issued with a bool protected by a dedicated mutex separate from
cmd_lock. Take it with guard()/scoped_guard() during setup, teardown,
and timeout cancellation (without holding it across I2C writes, polling,
or wait_for_completion_timeout(), and checking cmd_issued on timeout so
a racing completion is not discarded), and hold it across
cyapa_pip_irq_cmd_handler() so it is released before returning to
cyapa_irq(). That handles memory ordering, the teardown races, and the
-1 underflow together.
Could you please rework and resubmit the patch along those lines?
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 23:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 3:19 [PATCH] Input: cyapa - publish PIP command state to the IRQ handler Gui-Dong Han
2026-11-30 23:45 ` Dmitry Torokhov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox