From 07f14695afa65d55f08f61d86683e9647fd3f04a Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sat, 31 Oct 2026 21:34:26 -0600 Subject: [PATCH 1/6] hid: ipts: Don't retry sending on -EINTR ipts_mei_send() calls mei_cldev_send() again for as long as it returns -EINTR. mei_cldev_send() queues the message and then waits interruptibly for the transfer to complete, returning -EINTR if a signal is pending. Since the signal stays pending until the task returns to userspace, every retry fails immediately, and the loop never terminates. The task spins in the kernel, cannot be killed, and blocks the freezer. Every retry also queues the message again. On EDS v1 devices, iptsd switches the device to multitouch mode on startup, which restarts the device from iptsd's process context. If iptsd is stopped during that restart (e.g. by a system-sleep hook at the intermediate wakeup of suspend-then-hibernate), it gets stuck, and the system fails to hibernate: Freezing user space processes failed after 20.006 seconds (1 tasks refusing to freeze, wq_busy=0): task:iptsd state:R running task ... __mei_cl_send_timeout+0x197/0x360 [mei] ipts_mei_send+0x46/0x90 [ipts] ipts_cmd_send+0x8c/0xf0 [ipts] ipts_control_get_device_info+0x49/0xec [ipts] ipts_control_start.cold+0x24/0x271 [ipts] ipts_control_restart+0x2f/0x40 [ipts] Return the error instead of retrying. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/mei.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 1e0395ceae4a4..96a28856e26e4 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -158,10 +158,13 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length) if (!data) return -EFAULT; - do { - ret = mei_cldev_send(mei->cldev, (u8 *)data, length); - } while (ret == -EINTR); - + /* + * Don't retry on -EINTR: mei_cldev_send() queues the message before it waits for the + * transfer to complete, and returns -EINTR if a signal is pending. The signal stays + * pending until we return to userspace, so retrying would loop forever (and queue + * the message again every time). + */ + ret = mei_cldev_send(mei->cldev, (u8 *)data, length); if (ret < 0) return ret; From 827a3fba09f0317e6794ede322dd79f2b023fa8d Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 1 Nov 2026 21:07:21 -0700 Subject: [PATCH 2/6] hid: ipts: Keep a reference to the thread until it is stopped ipts_thread_stop() waits for the completion that the thread signals right before returning, and only then calls kthread_stop(). By that time the thread may have fully exited, and since nothing holds a reference to its task_struct, kthread_stop() operates on freed memory: refcount_t: addition on 0; use-after-free. WARNING: lib/refcount.c:25 at refcount_warn_saturate+0x6a/0x90 Call Trace: kthread_stop+0x16c/0x180 ipts_thread_stop+0x32/0x60 [ipts] ipts_receiver_stop+0x22/0x40 [ipts] _ipts_control_stop.part.0+0x29/0x50 [ipts] ipts_control_restart+0x21/0x40 [ipts] Take a reference when starting the thread, and drop it with kthread_stop_put(). Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/thread.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c index 355e92bea26f8..be6e96879e01f 100644 --- a/drivers/hid/ipts/thread.c +++ b/drivers/hid/ipts/thread.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "thread.h" @@ -40,6 +41,8 @@ static int ipts_thread_runner(void *data) int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread), void *data, const char *name) { + struct task_struct *task = NULL; + if (!thread) return -EFAULT; @@ -52,8 +55,21 @@ int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_th thread->should_stop = false; thread->threadfn = threadfn; - thread->thread = kthread_run(ipts_thread_runner, thread, name); - return PTR_ERR_OR_ZERO(thread->thread); + task = kthread_create(ipts_thread_runner, thread, name); + if (IS_ERR(task)) { + thread->thread = NULL; + return PTR_ERR(task); + } + + /* + * The runner can return before ipts_thread_stop() calls kthread_stop(), and the task + * goes away once it has exited. Keep a reference until the thread has been stopped. + */ + get_task_struct(task); + thread->thread = task; + wake_up_process(task); + + return 0; } int ipts_thread_stop(struct ipts_thread *thread) @@ -74,7 +90,7 @@ int ipts_thread_stop(struct ipts_thread *thread) wmb(); wait_for_completion(&thread->done); - ret = kthread_stop(thread->thread); + ret = kthread_stop_put(thread->thread); thread->thread = NULL; thread->data = NULL; From 854ed22e8b8fc52a2896641a0c581aee3ebd3a02 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 1 Nov 2026 21:07:21 -0700 Subject: [PATCH 3/6] hid: ipts: Finish tearing down the device if stopping the receiver failed The receiver thread returns an error if the sensor does not shut down cleanly (e.g. "READY_FOR_DATA: cmd failed: 5"). The thread has exited at that point, but _ipts_control_stop() returns early and leaves the buffers allocated, and ipts_control_stop() skips freeing the HID device. MEI removes and probes the device again on every suspend and hibernation. Since the old HID device is never destroyed, it keeps its hidraw node, the new one gets a different node, and iptsd is left talking to the wrong device, breaking touch until the next reboot: READY_FOR_DATA: cmd failed: 5 Failed to wait for data: -53 Failed to stop receiver loop: -53 Failed to stop receiver: -53 Failed to stop IPTS: -53 hid-generic 0000:1B96:005E.0163: input,hidraw3: ... Log the error and finish tearing down the device. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/control.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 5360842d260ba..e6570fa4f6922 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -433,11 +433,14 @@ static int _ipts_control_stop(struct ipts_context *ipts) ipts_hid_disable(ipts); dev_info(ipts->dev, "Stopping IPTS\n"); + /* + * The receiver thread is gone even if this fails: the error only says that shutting + * it down did not go cleanly. Bailing out here would leave the resources and the HID + * device behind. + */ ret = ipts_receiver_stop(ipts); - if (ret) { + if (ret) dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret); - return ret; - } ret = ipts_resources_free(&ipts->resources); if (ret) { @@ -451,18 +454,19 @@ static int _ipts_control_stop(struct ipts_context *ipts) int ipts_control_stop(struct ipts_context *ipts) { int ret = 0; + int hid_ret = 0; + /* + * Always free the HID device. This is called when removing the device, so nothing + * would free it later, and it would stay around when the device is probed again. + */ ret = _ipts_control_stop(ipts); - if (ret) - return ret; - ret = ipts_hid_free(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to free HID device: %d\n", ret); - return ret; - } + hid_ret = ipts_hid_free(ipts); + if (hid_ret) + dev_err(ipts->dev, "Failed to free HID device: %d\n", hid_ret); - return 0; + return ret ?: hid_ret; } int ipts_control_restart(struct ipts_context *ipts) From eb6d52deadc47b8888351d2c2e822f1a03db63af Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 1 Nov 2026 21:07:21 -0700 Subject: [PATCH 4/6] hid: ipts: Drop stale responses when starting the device When sending a command is interrupted, or waiting for its response times out, the ME may still answer later. That response stays in the list of received messages until the device is removed, where it can be mistaken for the response to a later command with the same code. This is not just a leak. After a restart, a stale READY_FOR_DATA response makes the event mode receiver process a buffer that has no data, and if its status is an error, the next shutdown of the receiver fails: IPTS running in event mode Failed to process buffer: -19 FEEDBACK: recv failed: -11 Failed to send feedback: -11 Stopping IPTS READY_FOR_DATA: cmd failed: 5 Failed to wait for data: -53 Drop all unconsumed responses when starting the device, before sending the first command. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/control.c | 10 ++++++++++ drivers/hid/ipts/mei.c | 22 ++++++++++++++++++++++ drivers/hid/ipts/mei.h | 8 ++++++++ 3 files changed, 40 insertions(+) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index e6570fa4f6922..42e322433499e 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -345,6 +345,7 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c int ipts_control_start(struct ipts_context *ipts) { int ret = 0; + size_t stale = 0; struct ipts_device_info info = { 0 }; if (!ipts) @@ -352,6 +353,15 @@ int ipts_control_start(struct ipts_context *ipts) dev_info(ipts->dev, "Starting IPTS\n"); + /* + * Responses to commands that were interrupted while sending (or that timed out) can + * still arrive later. Drop them, so that they are not mistaken for responses to the + * commands sent below. + */ + stale = ipts_mei_flush(&ipts->mei); + if (stale) + dev_dbg(ipts->dev, "Dropped %zu stale responses\n", stale); + ret = ipts_control_get_device_info(ipts, &info); if (ret) { dev_err(ipts->dev, "Failed to get device info: %d\n", ret); diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 96a28856e26e4..0cf5cd9682ea0 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -171,6 +171,28 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length) return 0; } +size_t ipts_mei_flush(struct ipts_mei *mei) +{ + size_t count = 0; + struct ipts_mei_message *entry = NULL; + struct ipts_mei_message *tmp = NULL; + + if (!mei) + return 0; + + down_write(&mei->message_lock); + + list_for_each_entry_safe(entry, tmp, &mei->messages, list) { + list_del(&entry->list); + devm_kfree(&mei->cldev->dev, entry); + count++; + } + + up_write(&mei->message_lock); + + return count; +} + int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev) { if (!mei) diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index 973bade6b0fdd..a4bf68ee98ef8 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -63,4 +63,12 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length); */ int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev); +/** + * ipts_mei_flush() - Drops all received messages that have not been consumed. + * @mei: The IPTS MEI device context. + * + * Returns: The number of dropped messages. + */ +size_t ipts_mei_flush(struct ipts_mei *mei); + #endif /* IPTS_MEI_H */ From 5120454eca313633c8bb3cdd9539d141c8215d4a Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sat, 31 Oct 2026 21:35:30 -0600 Subject: [PATCH 5/6] hid: ipts: Restart the device if switching modes failed On EDS v1 devices, switching modes restarts the device. If the restart fails (e.g. because the calling task has a signal pending), the device stays stopped and the HID device inactive. Since ipts_hid_raw_request() rejects requests while the HID device is inactive, userspace cannot retry, and touch input stays broken until the module is reloaded or the MEI client device is probed again. When the restart fails, restore the previous mode and restart the device from a work item instead, retrying a few times. Restoring the previous mode means that the device comes back in the mode that works without iptsd, and that iptsd can switch modes again once it is restarted. Serialize restarts with a mutex, and make sure that no restart is queued or running when the device is removed. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/context.h | 9 +++++++ drivers/hid/ipts/control.c | 49 ++++++++++++++++++++++++++++++++++++++ drivers/hid/ipts/control.h | 18 ++++++++++++++ drivers/hid/ipts/eds1.c | 20 ++++++++++++++-- drivers/hid/ipts/main.c | 2 ++ 5 files changed, 96 insertions(+), 2 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index ba33259f1f7c5..8dd4ff9ea52f7 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -15,6 +15,7 @@ #include #include #include +#include #include "mei.h" #include "resources.h" @@ -47,6 +48,14 @@ struct ipts_context { struct ipts_resources resources; struct ipts_thread receiver_loop; + + /* + * Serializes restarting the device. + */ + struct mutex control_lock; + + struct delayed_work restart_work; + unsigned int restart_attempts; }; #endif /* IPTS_CONTEXT_H */ diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 42e322433499e..ccd4e7ee522dd 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -22,6 +22,9 @@ #include "spec-data.h" #include "spec-device.h" +#define IPTS_RESTART_DELAY_MS 2000 +#define IPTS_RESTART_MAX_ATTEMPTS 5 + static int ipts_control_get_device_info(struct ipts_context *ipts, struct ipts_device_info *info) { int ret = 0; @@ -498,3 +501,49 @@ int ipts_control_restart(struct ipts_context *ipts) return 0; } + +static void ipts_control_restart_work(struct work_struct *work) +{ + int ret = 0; + struct ipts_context *ipts = + container_of(to_delayed_work(work), struct ipts_context, restart_work); + + mutex_lock(&ipts->control_lock); + + dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); + ret = ipts_control_restart(ipts); + + mutex_unlock(&ipts->control_lock); + + if (!ret) { + ipts->restart_attempts = 0; + return; + } + + if (++ipts->restart_attempts >= IPTS_RESTART_MAX_ATTEMPTS) { + dev_err(ipts->dev, "Giving up restarting IPTS: %d\n", ret); + ipts->restart_attempts = 0; + return; + } + + queue_delayed_work(system_freezable_wq, &ipts->restart_work, + msecs_to_jiffies(IPTS_RESTART_DELAY_MS)); +} + +void ipts_control_init_restart(struct ipts_context *ipts) +{ + mutex_init(&ipts->control_lock); + INIT_DELAYED_WORK(&ipts->restart_work, ipts_control_restart_work); + ipts->restart_attempts = 0; +} + +void ipts_control_schedule_restart(struct ipts_context *ipts) +{ + queue_delayed_work(system_freezable_wq, &ipts->restart_work, + msecs_to_jiffies(IPTS_RESTART_DELAY_MS)); +} + +void ipts_control_disable_restart(struct ipts_context *ipts) +{ + disable_delayed_work_sync(&ipts->restart_work); +} diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h index 26629c5144edb..41e97f365c805 100644 --- a/drivers/hid/ipts/control.h +++ b/drivers/hid/ipts/control.h @@ -123,4 +123,22 @@ int ipts_control_stop(struct ipts_context *ipts); */ int ipts_control_restart(struct ipts_context *ipts); +/** + * ipts_control_init_restart() - Initializes restarting the device in the background. + * @ipts: The IPTS driver context. + */ +void ipts_control_init_restart(struct ipts_context *ipts); + +/** + * ipts_control_schedule_restart() - Restarts the device in the background. + * @ipts: The IPTS driver context. + */ +void ipts_control_schedule_restart(struct ipts_context *ipts); + +/** + * ipts_control_disable_restart() - Cancels background restarts and prevents new ones. + * @ipts: The IPTS driver context. + */ +void ipts_control_disable_restart(struct ipts_context *ipts); + #endif /* IPTS_CONTROL_H */ diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index 7b9f54388a9f6..29a1a5fcfc37e 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -50,19 +50,35 @@ int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) { int ret = 0; + enum ipts_mode old_mode; if (!ipts) return -EFAULT; + mutex_lock(&ipts->control_lock); + if (ipts->mode == mode) - return 0; + goto out; + old_mode = ipts->mode; ipts->mode = mode; ret = ipts_control_restart(ipts); - if (ret) + if (ret) { dev_err(ipts->dev, "Failed to switch modes: %d\n", ret); + /* + * The device is stopped, and the HID device is inactive, so userspace cannot + * retry. Restart the device in the previous mode. Do it in the background, + * since the current task may not be able to talk to the device anymore + * (e.g. because it has a signal pending). + */ + ipts->mode = old_mode; + ipts_control_schedule_restart(ipts); + } + +out: + mutex_unlock(&ipts->control_lock); return ret; } diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index fb5b5c13ee3ea..a058f5b3835d5 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -76,6 +76,7 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id mutex_init(&ipts->feature_lock); init_completion(&ipts->feature_event); + ipts_control_init_restart(ipts); mei_cldev_set_drvdata(cldev, ipts); @@ -99,6 +100,7 @@ static void ipts_remove(struct mei_cl_device *cldev) } ipts = mei_cldev_get_drvdata(cldev); + ipts_control_disable_restart(ipts); ret = ipts_control_stop(ipts); if (ret) From 02852547dc734f1b024c59ec9ec85e37b7f380a8 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Mon, 2 Nov 2026 09:39:20 -0700 Subject: [PATCH 6/6] hid: ipts: Reconnect to the ME when restarting after a failed mode switch After a command times out, the ME client can get stuck one response behind: each response is delivered only when the next command is sent. Every later command then times out as well. The restart attempts cycle between failing on GET_DEVICE_INFO and failing on SET_MODE until they give up, and the device stays stopped. Waiting does not clear this, but reloading the module does. Disconnect from the ME client and connect again before each restart attempt, like a re-probe does. The cause of the stuck response is not known. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 --- drivers/hid/ipts/control.c | 20 +++++++++++++++++++- drivers/hid/ipts/mei.c | 23 +++++++++++++++++++++++ drivers/hid/ipts/mei.h | 8 ++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index ccd4e7ee522dd..642e5f564e2f6 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -511,8 +511,26 @@ static void ipts_control_restart_work(struct work_struct *work) mutex_lock(&ipts->control_lock); dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); - ret = ipts_control_restart(ipts); + ret = _ipts_control_stop(ipts); + if (ret) + goto unlock; + + /* + * After a timeout, every response from the ME can arrive only when the next command is + * sent, which makes all further commands time out as well. Waiting doesn't help, but + * reconnecting does. + */ + ret = ipts_mei_reconnect(&ipts->mei); + if (ret) { + dev_err(ipts->dev, "Failed to reconnect to the ME: %d\n", ret); + goto unlock; + } + + msleep(1000); + + ret = ipts_control_start(ipts); +unlock: mutex_unlock(&ipts->control_lock); if (!ret) { diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 0cf5cd9682ea0..706da2b97340b 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -193,6 +193,29 @@ size_t ipts_mei_flush(struct ipts_mei *mei) return count; } +int ipts_mei_reconnect(struct ipts_mei *mei) +{ + int ret = 0; + + if (!mei) + return -EFAULT; + + if (!mei->cldev) + return -EFAULT; + + /* + * This also unregisters the receive callback, and fails only if the client was already + * gone. Try to connect anyway. + */ + mei_cldev_disable(mei->cldev); + + ret = mei_cldev_enable(mei->cldev); + if (ret) + return ret; + + return mei_cldev_register_rx_cb(mei->cldev, ipts_mei_incoming); +} + int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev) { if (!mei) diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index a4bf68ee98ef8..612e59e9f3cd3 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -54,6 +54,14 @@ int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts */ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length); +/** + * ipts_mei_reconnect() - Disconnects from the ME client and connects again. + * @mei: The MEI device context. + * + * Return: 0 on success, <0 on error. + */ +int ipts_mei_reconnect(struct ipts_mei *mei); + /** * ipts_mei_init() - Initialize the MEI device context. * @mei: The MEI device context to initialize.