Skip to contentSkip to conversation

Input: serio - don't sleep on serio_mutex from drvctl_store()

PATCH New Nguyen Ngoc Thang 1 participant 1 message Sep 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Input: serio - don't sleep on serio_mutex from drvctl_store()

Writing to the drvctl attribute takes serio_mutex while holding the attribute's kernfs active reference. serio_unregister_port() does the
opposite: it holds serio_mutex and device_del() then waits for active references to drain in kernfs_drain(). If a drvctl write is in flight when the port is unregistered, the writer waits for serio_mutex and the unregistering task waits for the writer, and neither makes progress. Interruptibility of the lock does not help, as nothing signals the writer.

lockdep reports it as:

WARNING: possible circular locking dependency detected repro/4908 is trying to acquire lock:
(kn->active){++++}-{0:0}, at: __kernfs_remove+0x34c/0xb90
but task is already holding lock:
(serio_mutex){+.+.}-{4:4}, at: serio_unregister_port+0x1b/0x40 drvctl_store
sysfs_kf_write
...
kernfs_drain
device_del
serio_destroy_port
serio_unregister_port
userio_char_release

Use mutex_trylock() and restart the syscall when the mutex is busy, so the active reference is dropped before waiting. Once the port is being removed the retried write fails with -ENODEV.

---
 drivers/input/serio/serio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/input/serio/serio.c +3 -1
index 54dd26249b02..ebee89adc8a6 100644
@@ -15,6 +15,7 @@
 #include <linux/serio.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
+#include <linux/sched/signal.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 #include <linux/mutex.h>
@@ -357,7 +358,8 @@ static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, c
 	struct device_driver *drv;
 	int error;
 
-	scoped_cond_guard(mutex_intr, return -EINTR, &serio_mutex) {
+	/* Removal holds serio_mutex and waits for us: retry, don't sleep. */
+	scoped_cond_guard(mutex_try, return restart_syscall(), &serio_mutex) {
 		if (!strncmp(buf, "none", count)) {
 			serio_disconnect_port(serio);
 		} else if (!strncmp(buf, "reconnect", count)) {

base-commit: daae2ab46e0cb612f50ca1d86cf50e5962461ae5

Signature · 1 line
2.43.0