[PATCH 6/7] Input: gscps2 - serialize concurrent interrupt handlers

From: Dmitry Torokhov

Date: Sun Aug 30 2026 - 16:55:41 EST


gscps2_interrupt() may be invoked concurrently from a hardware interrupt
on one CPU and from process context via gscps2_writeb_output() on another
CPU. In gscps2_report_data(), ps2port->lock is released before calling
serio_interrupt() to avoid recursive deadlocks. However, if two execution
contexts run gscps2_report_data() concurrently for the same port, they
could race to acquire serio->lock inside serio_interrupt(), potentially
delivering multi-byte scancodes out of order.

Serialize execution of gscps2_interrupt() using gscps2_interrupt_lock
with ACQUIRE(spinlock_irqsave_try). Using spin_trylock prevents
overlapping executions and guarantees in-order packet delivery without
risking recursive deadlocks if an input driver synchronously sends a
command back via serio_write(). If the lock cannot be acquired, return
IRQ_NONE to preserve spurious interrupt detection on shared IRQ lines.

Reported-by: sashiko-bot@xxxxxxxxxx
Assisted-by: LLM
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/input/serio/gscps2.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/drivers/input/serio/gscps2.c b/drivers/input/serio/gscps2.c
index 907fb1537595..2afd53a163ff 100644
--- a/drivers/input/serio/gscps2.c
+++ b/drivers/input/serio/gscps2.c
@@ -288,6 +288,8 @@ static bool gscps2_report_data(struct gscps2port *ps2port)
}
}

+static DEFINE_SPINLOCK(gscps2_interrupt_lock);
+
/**
* gscps2_interrupt() - Interruption service routine
* @irq: interrupt number which triggered (unused)
@@ -305,21 +307,31 @@ static irqreturn_t gscps2_interrupt(int irq, void *dev)
{
struct gscps2port *ps2port;
bool handled = false;
+ bool more_data;
+
+ ACQUIRE(spinlock_irqsave_try, lock)(&gscps2_interrupt_lock);
+ if (ACQUIRE_ERR(spinlock_irqsave_try, &lock))
+ return IRQ_NONE;

guard(rcu)();

- list_for_each_entry_rcu(ps2port, &ps2port_list, node) {
- if (gscps2_read_data(ps2port))
- handled = true;
- }
+ do {
+ more_data = false;

- /* all data was read from the ports - now report the data to upper layer */
- list_for_each_entry_rcu(ps2port, &ps2port_list, node) {
- if (gscps2_report_data(ps2port)) {
- /* More data ready - break early to restart interrupt */
- break;
+ list_for_each_entry_rcu(ps2port, &ps2port_list, node) {
+ if (gscps2_read_data(ps2port))
+ handled = true;
}
- }
+
+ /* all data was read from the ports - now report the data to upper layer */
+ list_for_each_entry_rcu(ps2port, &ps2port_list, node) {
+ if (gscps2_report_data(ps2port)) {
+ /* More data ready - restart loop to read new data */
+ more_data = true;
+ break;
+ }
+ }
+ } while (more_data);

return IRQ_RETVAL(handled);
}

--
2.55.0.897.gb25b4bd76c-goog