Re: [PATCH] Input: cyapa - publish PIP command state to the IRQ handler

From: Dmitry Torokhov

Date: Mon Nov 30 2026 - 18:45:41 EST


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@xxxxxxxxx>

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