[PATCH] Input: cyapa - serialize firmware update and input unregister

From: Ziheng Wei

Date: Thu Dec 03 2026 - 22:13:50 EST


cyapa_update_fw_store() checks, unregisters, and clears cyapa->input
before acquiring state_sync_lock. Because kernfs only serializes writes
per open file handle, two concurrent writes to update_fw on separate file
descriptors can both observe a non-NULL cyapa->input and call
input_unregister_device() twice on the same input_dev.

Calling input_unregister_device() while holding state_sync_lock would
deadlock against cyapa_close() and invert lock ordering with
input->mutex. Add a dedicated update_fw_lock mutex to serialize
cyapa_update_fw_store() around unregistering cyapa->input and the
subsequent state_sync_lock-protected firmware update and reinitialization.

Found by LLM-assisted static analysis. Compile-tested on x86_64.

Fixes: c806b0b84d20 ("Input: cyapa - add sysfs interfaces support in the cyapa driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
drivers/input/mouse/cyapa.h | 2 ++
drivers/input/mouse/cyapa.c | 7 +++++++
2 files changed, 9 insertions(+)

diff --git a/drivers/input/mouse/cyapa.h b/drivers/input/mouse/cyapa.h
index ce951fe4516a..7e2b0712bb98 100644
--- a/drivers/input/mouse/cyapa.h
+++ b/drivers/input/mouse/cyapa.h
@@ -377,6 +377,8 @@ struct cyapa {
* to sync and wait other interface and detecting are done or ready.
*/
struct mutex state_sync_lock;
+ /* Serializes firmware update and input_unregister_device() */
+ struct mutex update_fw_lock;

const struct cyapa_dev_ops *ops;

diff --git a/drivers/input/mouse/cyapa.c b/drivers/input/mouse/cyapa.c
index c086d28f1412..24b4bcd33837 100644
--- a/drivers/input/mouse/cyapa.c
+++ b/drivers/input/mouse/cyapa.c
@@ -592,6 +592,7 @@ static int cyapa_initialize(struct cyapa *cyapa)
cyapa->state = CYAPA_STATE_NO_DEVICE;
cyapa->gen = CYAPA_GEN_UNKNOWN;
mutex_init(&cyapa->state_sync_lock);
+ mutex_init(&cyapa->update_fw_lock);

/*
* Set to hard code default, they will be updated with trackpad set
@@ -1087,6 +1088,10 @@ static ssize_t cyapa_update_fw_store(struct device *dev,
else
fw_name[count] = '\0';

+ error = mutex_lock_interruptible(&cyapa->update_fw_lock);
+ if (error)
+ return error;
+
if (cyapa->input) {
/*
* Force the input device to be registered after the firmware
@@ -1104,6 +1109,7 @@ static ssize_t cyapa_update_fw_store(struct device *dev,
* previous state just as it entered fw update entrance.
*/
cyapa_reinitialize(cyapa);
+ mutex_unlock(&cyapa->update_fw_lock);
return error;
}

@@ -1124,6 +1130,7 @@ static ssize_t cyapa_update_fw_store(struct device *dev,
}

mutex_unlock(&cyapa->state_sync_lock);
+ mutex_unlock(&cyapa->update_fw_lock);

return error ? error : count;
}

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa