From e001361edfc20cc4d45c35d72ddbf53d9d64ccf5 Mon Sep 17 00:00:00 2001 From: Josh Spaulding Date: Wed, 2 Sep 2026 21:30:00 -0500 Subject: [PATCH 01/19] hid: ipts: Fix double list_del race in ipts_mei_search() ipts_mei_search() looks up a matching response under the read side of message_lock, drops the lock, and then re-acquires it for writing to unlink the entry. Two concurrent callers waiting for the same command code (typically the receiver poll thread and a userspace hidraw raw request, both inside ipts_control_send_feedback()) can each find the same entry during their read-locked walk and then both unlink and free it. The second list_del() hits LIST_POISON and, with CONFIG_DEBUG_LIST, oopses the poll kthread: list_del corruption, ...->next is LIST_POISON1 (dead000000000100) kernel BUG at lib/list_debug.c:56! RIP: __list_del_entry_valid_or_report.cold Call Trace: ipts_mei_search+0x83/0x150 [ipts] ipts_mei_recv+0x61/0x190 [ipts] ipts_cmd_recv_timeout+0x2f/0x90 [ipts] ipts_control_send_feedback+0x81/0xc0 [ipts] ipts_receiver_poll_loop.cold+0x89/0x11c [ipts] ipts_thread_runner+0x21/0x40 [ipts] Without CONFIG_DEBUG_LIST the same race shows up as a general protection fault on 0xdead000000000108 in ipts_mei_search(). The other caller is then left blocked in D state inside the driver, which makes every later suspend fail with "tasks refusing to freeze". Seen on resume from s2idle on a Surface Pro 7 while iptsd was (re)started. Hold the write lock across the search and the unlink so that a message can only be claimed by one caller. Signed-off-by: Josh Spaulding --- drivers/hid/ipts/mei.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 1e0395ceae4a4..60c2ec51b28f9 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -26,13 +26,6 @@ static void locked_list_add(struct list_head *new, struct list_head *head, up_write(lock); } -static void locked_list_del(struct list_head *entry, struct rw_semaphore *lock) -{ - down_write(lock); - list_del(entry); - up_write(lock); -} - static void ipts_mei_incoming(struct mei_cl_device *cldev) { ssize_t ret = 0; @@ -85,7 +78,7 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, if (!rsp) return -EFAULT; - down_read(&mei->message_lock); + down_write(&mei->message_lock); /* * Iterate over the list of received messages, and check if there is one @@ -96,14 +89,14 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, break; } - up_read(&mei->message_lock); - /* * If entry is not the list head, this means that the loop above has been stopped early, - * and that we found a matching element. We drop the message from the list and return it. + * and that we found a matching element. We drop the message from the list while still + * holding the lock, so that no other waiter can claim the same entry, and return it. */ if (!list_entry_is_head(entry, &mei->messages, list)) { - locked_list_del(&entry->list, &mei->message_lock); + list_del(&entry->list); + up_write(&mei->message_lock); *rsp = entry->rsp; devm_kfree(&mei->cldev->dev, entry); @@ -111,6 +104,8 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, return 0; } + up_write(&mei->message_lock); + return -EAGAIN; } From 410814a94ecd9feade3a54206b865fe9d7ef578b Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:06:37 +0000 Subject: [PATCH 02/19] hid: ipts: Reference-count the context and free it after its last user The context is allocated with devm_kzalloc(), so devres frees it when the device is released, whoever still uses it. The following changes bound the stop path, and a stop that gives up must keep everything that a receiver thread or a HID device which is still there can touch. With a devm context, devres frees it anyway, and the next touch is a use-after-free. Reference-count the context instead, and free it only when the last reference is dropped: - Probe holds one reference, dropped by a devres action. Remove and every later probe failure disable the MEI device before that, which cancels its receive callback, so no message arrives while the context is freed. If adding the devres action fails, the context is freed at once, before the MEI device is disabled; the receive callback finds no context then, because drvdata is not set yet. - The receiver thread and the HID device hold one each, for their whole life. - The context holds a reference to the MEI device until it is freed: a user that is still there after remove keeps sending through it and logging against it. ipts_remove() then waits, bounded, for the remaining users to return. That narrows the window in which the module code is freed under one that is about to return, but does not close it: the last user wakes remove from inside its own put, before it has left the module code. After a failed stop the receiver thread or the HID device is still there on purpose, and remove does not wait for it. A failed probe used to return without disabling the MEI device, so its receive callback stayed registered after the context was gone. Every probe failure after mei_cldev_enable() now disables it. The messages are now plain kzalloc() instead of devm: they are freed on the error paths of the receive callback, and any left over is freed with the context. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 43 +++++++++++++++++ drivers/hid/ipts/hid.c | 11 +++++ drivers/hid/ipts/main.c | 94 +++++++++++++++++++++++++++++++++++-- drivers/hid/ipts/mei.c | 30 +++++++++++- drivers/hid/ipts/mei.h | 8 ++++ drivers/hid/ipts/receiver.c | 17 +++++++ 6 files changed, 196 insertions(+), 7 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index ba33259f1f7c5..40fa5731e597c 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -8,13 +8,16 @@ #ifndef IPTS_CONTEXT_H #define IPTS_CONTEXT_H +#include #include #include #include +#include #include #include #include #include +#include #include "mei.h" #include "resources.h" @@ -43,10 +46,50 @@ struct ipts_context { bool hid_active; struct hid_device *hid; + /* + * Freed when the last reference is dropped. Probe holds one (dropped by devres), and the + * receiver thread and the HID device hold one each. + */ + struct kref refcount; + + /* + * Users other than probe. ipts_remove() waits, bounded, for this to reach zero. That + * narrows the window in which the module code is freed under a user that is about to + * return, but does not close it: the last user wakes remove from inside its own put. + */ + atomic_t users; + wait_queue_head_t users_wq; + struct ipts_device_info info; struct ipts_resources resources; struct ipts_thread receiver_loop; }; +void ipts_context_release(struct kref *refcount); + +/* + * Every user of the context (the receiver thread, the HID device) takes a reference, so the + * context is freed only after the last user is gone. + */ +static inline void ipts_context_get(struct ipts_context *ipts) +{ + if (!ipts) + return; + + kref_get(&ipts->refcount); + atomic_inc(&ipts->users); +} + +static inline void ipts_context_put(struct ipts_context *ipts) +{ + if (!ipts) + return; + + if (atomic_dec_and_test(&ipts->users)) + wake_up_all(&ipts->users_wq); + + kref_put(&ipts->refcount, ipts_context_release); +} + #endif /* IPTS_CONTEXT_H */ diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c index e34a1a4f9fa77..4e8e85f745a84 100644 --- a/drivers/hid/ipts/hid.c +++ b/drivers/hid/ipts/hid.c @@ -189,6 +189,12 @@ int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info) return err; } + /* + * The HID device uses the context in its callbacks until it is destroyed: hold a + * reference for its whole life. + */ + ipts_context_get(ipts); + ipts->hid->driver_data = ipts; ipts->hid->dev.parent = ipts->dev; ipts->hid->ll_driver = &ipts_hid_driver; @@ -221,5 +227,10 @@ int ipts_hid_free(struct ipts_context *ipts) hid_destroy_device(ipts->hid); ipts->hid = NULL; + /* + * Drop the reference taken by ipts_hid_init(). + */ + ipts_context_put(ipts); + return 0; } diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index fb5b5c13ee3ea..6558a23c7c8ca 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -16,6 +19,7 @@ #include #include #include +#include #include "context.h" #include "control.h" @@ -39,6 +43,27 @@ static int ipts_set_dma_mask(struct mei_cl_device *cldev) return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32)); } +void ipts_context_release(struct kref *refcount) +{ + struct ipts_context *ipts = container_of(refcount, struct ipts_context, refcount); + struct mei_cl_device *cldev = ipts->mei.cldev; + + ipts_mei_free_messages(&ipts->mei); + kfree(ipts); + + /* + * Drop the reference to the MEI device that probe took for the context. + */ + put_device(&cldev->dev); +} + +static void ipts_context_drop_probe(void *data) +{ + struct ipts_context *ipts = data; + + kref_put(&ipts->refcount, ipts_context_release); +} + static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id *id) { int ret = 0; @@ -59,18 +84,49 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id return ret; } - ipts = devm_kzalloc(&cldev->dev, sizeof(*ipts), GFP_KERNEL); + /* + * The context is freed by its kref once every user dropped its reference. It must not be + * tied to the device lifetime: a receiver thread or HID device that is still there after + * a failed stop keeps using it after the device is gone. + */ + ipts = kzalloc(sizeof(*ipts), GFP_KERNEL); if (!ipts) { - mei_cldev_disable(cldev); - return -ENOMEM; + ret = -ENOMEM; + goto err_disable; } + /* + * Such a user also keeps sending through the MEI device and logging against it after + * remove returned, so the context holds a reference to it until it is freed. + */ + get_device(&cldev->dev); + + kref_init(&ipts->refcount); + atomic_set(&ipts->users, 0); + init_waitqueue_head(&ipts->users_wq); + + /* + * Before the release function can run: it frees the message list. + */ ret = ipts_mei_init(&ipts->mei, cldev); if (ret) { dev_err(&cldev->dev, "Failed to init MEI bus logic: %d\n", ret); - return ret; + put_device(&cldev->dev); + kfree(ipts); + goto err_disable; } + /* + * Drop the probe reference when the device is released. Remove and every later probe + * failure disable the MEI device before that, which cancels its receive callback, so no + * message arrives while the context is being freed. If adding the action fails, the + * context is freed at once, before the MEI device is disabled: the receive callback finds + * no context then, because drvdata is not set yet. + */ + ret = devm_add_action_or_reset(&cldev->dev, ipts_context_drop_probe, ipts); + if (ret) + goto err_disable; + ipts->dev = &cldev->dev; ipts->mode = IPTS_MODE_EVENT; @@ -82,12 +138,22 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id ret = ipts_control_start(ipts); if (ret) { dev_err(&cldev->dev, "Failed to start IPTS: %d\n", ret); - return ret; + goto err_disable; } return 0; + +err_disable: + mei_cldev_disable(cldev); + return ret; } +/* + * How long ipts_remove() waits for the last user of the context to return. One that does not + * return keeps the context alive, so it is never freed under it. + */ +#define IPTS_REMOVE_TIMEOUT_MS 12000 + static void ipts_remove(struct mei_cl_device *cldev) { int ret = 0; @@ -105,6 +171,24 @@ static void ipts_remove(struct mei_cl_device *cldev) dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret); mei_cldev_disable(cldev); + + /* + * A failed stop keeps the receiver thread or the HID device, and their references, on + * purpose, so there is nothing to wait for. + */ + if (ret) + return; + + /* + * Wait, bounded, for the remaining users of the context to return, so the module code is + * not freed while they still run it. That narrows the window, but does not close it: the + * last user wakes this from inside its own put, before it has left the module code. The + * probe reference is dropped by devres after this, and the context is freed after its + * last user. + */ + if (!wait_event_timeout(ipts->users_wq, atomic_read(&ipts->users) == 0, + msecs_to_jiffies(IPTS_REMOVE_TIMEOUT_MS))) + dev_err(&cldev->dev, "A user of the IPTS context did not return\n"); } static struct mei_cl_device_id ipts_device_id_table[] = { diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 60c2ec51b28f9..4a2ce1b895ca4 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,7 @@ static void ipts_mei_incoming(struct mei_cl_device *cldev) return; } - entry = devm_kzalloc(ipts->dev, sizeof(*entry), GFP_KERNEL); + entry = kzalloc(sizeof(*entry), GFP_KERNEL); if (!entry) return; @@ -55,11 +56,13 @@ static void ipts_mei_incoming(struct mei_cl_device *cldev) if (ret < 0) { dev_err(ipts->dev, "Error while reading response: %ld\n", ret); + kfree(entry); return; } if (ret == 0) { dev_err(ipts->dev, "Received empty response\n"); + kfree(entry); return; } @@ -99,7 +102,7 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, up_write(&mei->message_lock); *rsp = entry->rsp; - devm_kfree(&mei->cldev->dev, entry); + kfree(entry); return 0; } @@ -163,6 +166,29 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length) return 0; } +void ipts_mei_free_messages(struct ipts_mei *mei) +{ + struct ipts_mei_message *entry = NULL; + struct ipts_mei_message *tmp = NULL; + + if (!mei) + return; + + /* + * Only called when the context is released. If the lock is still taken, its holder died + * with it, and the messages are leaked rather than freed under it. + */ + if (!down_write_trylock(&mei->message_lock)) + return; + + list_for_each_entry_safe(entry, tmp, &mei->messages, list) { + list_del(&entry->list); + kfree(entry); + } + + up_write(&mei->message_lock); +} + 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..7114d9a10dcb7 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_free_messages() - Free every message still queued. + * @mei: The MEI device context. + * + * Called from the context release function, once no user of the context is left. + */ +void ipts_mei_free_messages(struct ipts_mei *mei); + #endif /* IPTS_MEI_H */ diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c index 977724c728c3e..20b8298ab4e68 100644 --- a/drivers/hid/ipts/receiver.c +++ b/drivers/hid/ipts/receiver.c @@ -231,6 +231,11 @@ int ipts_receiver_start(struct ipts_context *ipts) return ret; } + /* + * The receiver thread uses the context until it stops: hold a reference for its whole run. + */ + ipts_context_get(ipts); + return 0; } @@ -241,7 +246,19 @@ int ipts_receiver_stop(struct ipts_context *ipts) if (!ipts) return -EFAULT; + /* + * A start that failed early never started the thread, so there is no reference to drop. + */ + if (!ipts->receiver_loop.thread) + return 0; + ret = ipts_thread_stop(&ipts->receiver_loop); + + /* + * The thread is gone, so drop its reference. + */ + ipts_context_put(ipts); + if (ret) { dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret); return ret; From 224f2615abcdc4f7c151fcbd739d55a1d657cdbd Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:07:19 +0000 Subject: [PATCH 03/19] hid: ipts: Bound the wait for the receiver thread on stop ipts_remove() runs on rmmod, and also on every reboot, power-off and suspend: mei_me_shutdown() and mei_me_pci_suspend() both call mei_stop(), which removes every ME client device. ipts_thread_stop() waited for the receiver thread with wait_for_completion() and no timeout. If the thread died in an oops, or never got out of a wait, the stop never returned, and reboot or suspend hung with it. Wait for the thread with a timeout instead, which the caller passes in. While waiting, check whether the thread is exiting: a thread that died runs no driver code anymore and counts as stopped, without calling kthread_stop() on it, which could wait for its exit forever. A thread that is still running after the timeout is reported to the caller, which then frees none of the driver's memory that the thread can still touch: the receiver keeps the thread's context reference, so the context stays, and the context keeps the MEI device. Only a later stop drops that reference. After remove there is none, so the context then stays for good. Until later changes in this series remove the HID device on those paths, a stop that gives up here, or finds the thread dead, still returns before the HID device is removed: rmmod with a stuck or dead receiver then leaves the HID device registered, and opening its hidraw node runs freed module code. Before this change, that rmmod hung for good instead. Keep a reference on the task_struct from creation until the stop is done, so that a thread which exits on its own early can still be looked at, and never store a kthread_create() error as the thread. The return value of the thread function moves to thread->exit_code; the receiver reports it as before. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/receiver.c | 19 ++++++++++-- drivers/hid/ipts/thread.c | 58 +++++++++++++++++++++++++++++++++---- drivers/hid/ipts/thread.h | 21 ++++++++++++-- 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c index 20b8298ab4e68..211315371a7f6 100644 --- a/drivers/hid/ipts/receiver.c +++ b/drivers/hid/ipts/receiver.c @@ -239,6 +239,11 @@ int ipts_receiver_start(struct ipts_context *ipts) return 0; } +/* + * How long a stop waits for the receiver thread to return. + */ +#define IPTS_RECEIVER_STOP_TIMEOUT_MS 10000 + int ipts_receiver_stop(struct ipts_context *ipts) { int ret = 0; @@ -252,13 +257,23 @@ int ipts_receiver_stop(struct ipts_context *ipts) if (!ipts->receiver_loop.thread) return 0; - ret = ipts_thread_stop(&ipts->receiver_loop); + ret = ipts_thread_stop(&ipts->receiver_loop, IPTS_RECEIVER_STOP_TIMEOUT_MS); + if (ret == -ETIMEDOUT) { + /* + * Still running: the thread keeps its reference, and everything it uses. + */ + dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret); + return ret; + } /* - * The thread is gone, so drop its reference. + * The thread is gone (it returned or died), so drop its reference. */ ipts_context_put(ipts); + if (!ret) + ret = ipts->receiver_loop.exit_code; + if (ret) { dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret); return ret; diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c index 355e92bea26f8..81d13a8944d17 100644 --- a/drivers/hid/ipts/thread.c +++ b/drivers/hid/ipts/thread.c @@ -8,7 +8,11 @@ #include #include #include +#include #include +#include +#include +#include #include "thread.h" @@ -40,6 +44,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; @@ -49,21 +55,40 @@ int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_th init_completion(&thread->done); thread->data = data; + thread->exit_code = 0; 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, "%s", name); + if (IS_ERR(task)) { + thread->thread = NULL; + thread->threadfn = NULL; + thread->data = NULL; + return PTR_ERR(task); + } + + /* + * Keep the task_struct until ipts_thread_stop() is done with it, even if the + * thread exits on its own (or dies) before that. + */ + get_task_struct(task); + thread->thread = task; + wake_up_process(task); + + return 0; } -int ipts_thread_stop(struct ipts_thread *thread) +int ipts_thread_stop(struct ipts_thread *thread, unsigned int timeout_ms) { int ret = 0; + unsigned long end = 0; + struct task_struct *task = NULL; if (!thread) return -EFAULT; - if (!thread->thread) + task = thread->thread; + if (!task) return 0; WRITE_ONCE(thread->should_stop, true); @@ -73,8 +98,29 @@ int ipts_thread_stop(struct ipts_thread *thread) */ wmb(); - wait_for_completion(&thread->done); - ret = kthread_stop(thread->thread); + end = jiffies + msecs_to_jiffies(timeout_ms); + + while (!wait_for_completion_timeout(&thread->done, + msecs_to_jiffies(IPTS_THREAD_STOP_POLL_MS))) { + /* + * A thread that died in an oops is unwinding through do_exit() and will + * never complete. It runs no driver code anymore, so it counts as stopped. + * kthread_stop() could wait for its exit forever, so it is not called. + */ + if ((task->flags & PF_EXITING) && !completion_done(&thread->done)) { + pr_err("ipts: thread %s died without finishing\n", task->comm); + ret = -EOWNERDEAD; + goto out; + } + + if (time_after(jiffies, end)) + return -ETIMEDOUT; + } + + thread->exit_code = kthread_stop(task); + +out: + put_task_struct(task); thread->thread = NULL; thread->data = NULL; diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h index 1f966b8b32c45..1e4f8389db7d9 100644 --- a/drivers/hid/ipts/thread.h +++ b/drivers/hid/ipts/thread.h @@ -24,6 +24,11 @@ struct ipts_thread { bool should_stop; struct completion done; + /* + * Return value of the thread function, valid after ipts_thread_stop() returned 0. + */ + int exit_code; + void *data; int (*threadfn)(struct ipts_thread *thread); }; @@ -48,12 +53,24 @@ bool ipts_thread_should_stop(struct ipts_thread *thread); int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread), void *data, const char name[]); +/* + * How often ipts_thread_stop() checks whether the thread died while it waits. + */ +#define IPTS_THREAD_STOP_POLL_MS 100 + /** * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished. * @thread: The thread that should stop. + * @timeout_ms: How long to wait for the thread function to return. + * + * The wait is bounded. A thread that died (oops) without finishing runs no driver code + * anymore and is treated as stopped. A thread that is still running after the timeout is + * left alone, and nothing it uses may be freed. * - * Returns: The return value of the thread function. + * Returns: 0 if the thread function returned (its return value is in thread->exit_code), + * -EOWNERDEAD if the thread died without returning, -ETIMEDOUT if it is still running, + * <0 on other errors. */ -int ipts_thread_stop(struct ipts_thread *thread); +int ipts_thread_stop(struct ipts_thread *thread, unsigned int timeout_ms); #endif /* IPTS_THREAD_H */ From 9df2c857567f760d59104ccf926661ce08965a53 Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:08:01 +0000 Subject: [PATCH 04/19] hid: ipts: Never wait for the message lock, a reply or a send forever Every command waits for its reply in ipts_mei_recv(), which slept in wait_event() with ipts_mei_search() as the wait condition. The search takes message_lock, so a waiter blocked on the lock inside the wait condition, and a task that held the lock without releasing it (one that died while holding it) blocked every later command for good: the receiver thread, the HID requests, and the shutdown handshake on the stop path. The lock only ever covers a list add or delete, so nobody has a reason to hold it for long. Never block on it: - ipts_mei_search() only tries the lock, and reports a busy lock to the caller. - ipts_mei_recv() searches with the task running and sleeps until a new message is queued (a sequence counter bumped under the lock) or the time is up. While the lock is busy, it tries again after a short sleep. The time limit covers both. - ipts_mei_incoming() gives up after a bounded wait for the lock and drops the response. - Sends use mei_cldev_send_timeout(). An interrupted send is not retried, because the message is already queued and would go out twice. The timeout bounds the wait for the write to complete; the MEI core can still wait without a bound for its bus lock, and for room in a full transmit queue, which the driver cannot change. Holding the lock also holds a context reference, taken and dropped by the new lock helpers, so the context is never freed under a holder that never lets go. The lock is released before the reference, because dropping the last reference frees the context, and the release function takes the lock itself to free the messages. The "negative timeout waits forever" case could never be reached with a u64 timeout and is gone; a zero timeout still checks once. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/cmd.h | 2 +- drivers/hid/ipts/context.h | 6 +- drivers/hid/ipts/mei.c | 127 +++++++++++++++++++++++++++++-------- drivers/hid/ipts/mei.h | 16 ++++- 4 files changed, 119 insertions(+), 32 deletions(-) diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h index 2b4079075b642..84cf8278287b5 100644 --- a/drivers/hid/ipts/cmd.h +++ b/drivers/hid/ipts/cmd.h @@ -25,7 +25,7 @@ * @rsp: The address that the received response will be copied to. * @timeout: How many milliseconds the function will wait at most. * - * A negative timeout means to wait forever. + * A timeout of 0 means to check once and return. * * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received. */ diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index 40fa5731e597c..db0f4c2793961 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -48,7 +48,7 @@ struct ipts_context { /* * Freed when the last reference is dropped. Probe holds one (dropped by devres), and the - * receiver thread and the HID device hold one each. + * receiver thread, the HID device and every holder of message_lock hold one each. */ struct kref refcount; @@ -69,8 +69,8 @@ struct ipts_context { void ipts_context_release(struct kref *refcount); /* - * Every user of the context (the receiver thread, the HID device) takes a reference, so the - * context is freed only after the last user is gone. + * Every user of the context (the receiver thread, the HID device, a holder of message_lock) + * takes a reference, so the context is freed only after the last user is gone. */ static inline void ipts_context_get(struct ipts_context *ipts) { diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 4a2ce1b895ca4..bcca4c76f0c93 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -5,9 +5,11 @@ * Linux driver for Intel Precise Touch & Stylus */ +#include #include #include #include +#include #include #include #include @@ -19,12 +21,51 @@ #include "context.h" #include "mei.h" -static void locked_list_add(struct list_head *new, struct list_head *head, - struct rw_semaphore *lock) +/* + * The message lock is only ever held around a list add or delete. A holder that keeps it + * for longer died or is stuck, and waiting for it must never block forever: ipts_remove() + * runs on reboot and suspend, too. + */ +static bool ipts_mei_trylock(struct ipts_mei *mei) +{ + struct ipts_context *ipts = container_of(mei, struct ipts_context, mei); + + if (!down_write_trylock(&mei->message_lock)) + return false; + + /* + * Hold the context while the lock is held, so a holder that never releases the lock (a + * dead or stuck task) keeps the context alive instead of freeing it under itself. + */ + ipts_context_get(ipts); + + return true; +} + +static bool ipts_mei_lock_timeout(struct ipts_mei *mei, unsigned int timeout_ms) { - down_write(lock); - list_add(new, head); - up_write(lock); + unsigned long end = jiffies + msecs_to_jiffies(timeout_ms); + + while (!ipts_mei_trylock(mei)) { + if (time_after(jiffies, end)) + return false; + + usleep_range(IPTS_MEI_LOCK_POLL_US, 2 * IPTS_MEI_LOCK_POLL_US); + } + + return true; +} + +static void ipts_mei_unlock(struct ipts_mei *mei) +{ + struct ipts_context *ipts = container_of(mei, struct ipts_context, mei); + + /* + * Release the lock before the reference: if this is the last one, the release function + * frees the context, and takes the lock itself to free the messages. + */ + up_write(&mei->message_lock); + ipts_context_put(ipts); } static void ipts_mei_incoming(struct mei_cl_device *cldev) @@ -66,7 +107,16 @@ static void ipts_mei_incoming(struct mei_cl_device *cldev) return; } - locked_list_add(&entry->list, &ipts->mei.messages, &ipts->mei.message_lock); + if (!ipts_mei_lock_timeout(&ipts->mei, IPTS_MEI_LOCK_TIMEOUT_MS)) { + dev_err_ratelimited(ipts->dev, "Message lock is busy, dropping a response\n"); + kfree(entry); + return; + } + + list_add(&entry->list, &ipts->mei.messages); + WRITE_ONCE(ipts->mei.seq, ipts->mei.seq + 1); + ipts_mei_unlock(&ipts->mei); + wake_up_all(&ipts->mei.message_queue); } @@ -81,7 +131,8 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, if (!rsp) return -EFAULT; - down_write(&mei->message_lock); + if (!ipts_mei_trylock(mei)) + return -EBUSY; /* * Iterate over the list of received messages, and check if there is one @@ -99,7 +150,7 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, */ if (!list_entry_is_head(entry, &mei->messages, list)) { list_del(&entry->list); - up_write(&mei->message_lock); + ipts_mei_unlock(mei); *rsp = entry->rsp; kfree(entry); @@ -107,7 +158,7 @@ static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, return 0; } - up_write(&mei->message_lock); + ipts_mei_unlock(mei); return -EAGAIN; } @@ -116,36 +167,53 @@ int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts u64 timeout) { int ret = 0; + long left = 0; + unsigned long seq = 0; + unsigned long end = 0; if (!mei) return -EFAULT; /* - * A timeout of 0 means check and return immideately. + * A timeout of 0 means check and return immediately. */ - if (timeout == 0) - return ipts_mei_search(mei, code, rsp); + if (timeout == 0) { + ret = ipts_mei_search(mei, code, rsp); + return ret == -EBUSY ? -EAGAIN : ret; + } + + end = jiffies + msecs_to_jiffies(timeout); /* - * A timeout of less than 0 means to wait forever. + * Search with the task running, then sleep until a new message is queued or the time + * is up. seq is read before the search, so a message queued in between ends the sleep + * at once. While another task holds the lock, try again after a short sleep. */ - if (timeout < 0) { - wait_event(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0); - return 0; - } + do { + seq = READ_ONCE(mei->seq); + /* Read the sequence before searching for a message. */ + smp_rmb(); - ret = wait_event_timeout(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0, - msecs_to_jiffies(timeout)); + ret = ipts_mei_search(mei, code, rsp); + if (ret != -EAGAIN && ret != -EBUSY) + return ret; - if (ret > 0) - return 0; + left = (long)(end - jiffies); + if (left <= 0) + break; + + if (ret == -EBUSY) + usleep_range(IPTS_MEI_LOCK_POLL_US, 2 * IPTS_MEI_LOCK_POLL_US); + else + wait_event_timeout(mei->message_queue, READ_ONCE(mei->seq) != seq, left); + } while (left > 0); return -EAGAIN; } int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length) { - int ret = 0; + ssize_t ret = 0; if (!mei) return -EFAULT; @@ -156,9 +224,11 @@ 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); + /* + * Bounded: a send that is not acknowledged in time fails. An interrupted send is not + * retried, because the message is already queued and would go out twice. + */ + ret = mei_cldev_send_timeout(mei->cldev, (u8 *)data, length, IPTS_MEI_SEND_TIMEOUT_MS); if (ret < 0) return ret; @@ -175,8 +245,10 @@ void ipts_mei_free_messages(struct ipts_mei *mei) return; /* - * Only called when the context is released. If the lock is still taken, its holder died - * with it, and the messages are leaked rather than freed under it. + * Only called when the context is released, so every holder of the lock dropped its + * reference and the lock is free, unless its holder died with it: then the messages are + * leaked rather than freed under it. The raw lock is taken, so no reference is taken on + * the context being released. */ if (!down_write_trylock(&mei->message_lock)) return; @@ -198,6 +270,7 @@ int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev) return -EFAULT; mei->cldev = cldev; + mei->seq = 0; INIT_LIST_HEAD(&mei->messages); init_waitqueue_head(&mei->message_queue); diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index 7114d9a10dcb7..269cba241692a 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -28,8 +28,22 @@ struct ipts_mei { wait_queue_head_t message_queue; struct rw_semaphore message_lock; + + /* + * Bumped under message_lock whenever a message is queued, so that waiters can sleep + * without taking the lock inside the wait condition. + */ + unsigned long seq; }; +/* + * Upper bounds for taking the message lock to queue a response, and for one send. While + * the lock is busy, it is tried again every IPTS_MEI_LOCK_POLL_US. + */ +#define IPTS_MEI_LOCK_TIMEOUT_MS 1000 +#define IPTS_MEI_LOCK_POLL_US 200 +#define IPTS_MEI_SEND_TIMEOUT_MS 2000 + /** * ipts_mei_recv() - Receive data from a MEI device. * @mei: The IPTS MEI device context. @@ -37,7 +51,7 @@ struct ipts_mei { * @rsp: The address that the received data will be copied to. * @timeout: How many milliseconds the function will wait at most. * - * A negative timeout means to wait forever. + * A timeout of 0 means to check once and return. * * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received. */ From e7f4e4e47ee9437b3ababa7f3e89f128b1ac0bc0 Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:08:34 +0000 Subject: [PATCH 05/19] hid: ipts: Always remove the HID device when stopping When the receiver thread returned an error from its shutdown handshake, _ipts_control_stop() and ipts_control_stop() returned early and never called ipts_hid_free(). The module then unloaded with its hid_device still registered, and the next open or ioctl of its hidraw node ran freed module code: a page fault in hidraw_open() or inside __hid_hw_raw_request(), with "[last unloaded: ipts]" in the oops. Remove the HID device on every path once the receiver thread is gone. The only exception is a receiver thread that is still running after the bounded stop: it still feeds the HID device and uses the buffers, so nothing is freed and the error is passed on. Until a later change in this series counts the receiver's HID input, that exception still lets rmmod with a stuck receiver leave the HID device registered, with the hidraw problem above. And a receiver that died inside hid_input_report() may hold HID core locks, so destroying the HID device after it can block; the same later change makes the stop wait for running HID input first, and keep the HID device in that case. If the thread died, or the device did not confirm the stop, the device may still write to its DMA buffers. Leak them instead of freeing them; the next start allocates new ones. The stop now reads the thread's exit code itself. A stop that finds no thread, because a start failed before it started one, reports a clean exit instead of the exit code of the thread before. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 52 +++++++++++++++++++++++++++++++------ drivers/hid/ipts/receiver.c | 10 +++---- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 5360842d260ba..c600ff519acfb 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include "cmd.h" @@ -342,6 +344,19 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER); } +/* + * The device was never told to stop, or never confirmed it, so it may still write to its + * DMA buffers. They are never freed; the next start allocates new ones. The report buffer + * is driver memory only and is freed. + */ +static void ipts_control_leak_buffers(struct ipts_context *ipts) +{ + dev_warn(ipts->dev, "Device stop not confirmed, leaking its DMA buffers\n"); + + kfree(ipts->resources.report.address); + memset(&ipts->resources, 0, sizeof(ipts->resources)); +} + int ipts_control_start(struct ipts_context *ipts) { int ret = 0; @@ -434,17 +449,32 @@ static int _ipts_control_stop(struct ipts_context *ipts) dev_info(ipts->dev, "Stopping IPTS\n"); ret = ipts_receiver_stop(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret); + if (ret == -ETIMEDOUT) { + /* + * The thread is still running and can still touch the buffers and the HID + * device, so nothing is freed. + */ + dev_err(ipts->dev, "Receiver thread did not stop\n"); return ret; } - ret = ipts_resources_free(&ipts->resources); if (ret) { - dev_err(ipts->dev, "Failed to free resources: %d\n", ret); - return ret; + dev_err(ipts->dev, "Receiver thread died without stopping the device: %d\n", ret); + ipts_control_leak_buffers(ipts); + return 0; } + if (ipts->receiver_loop.exit_code) { + dev_err(ipts->dev, "Failed to stop the device: %d\n", + ipts->receiver_loop.exit_code); + ipts_control_leak_buffers(ipts); + return 0; + } + + ret = ipts_resources_free(&ipts->resources); + if (ret) + dev_err(ipts->dev, "Failed to free resources: %d\n", ret); + return 0; } @@ -452,15 +482,21 @@ int ipts_control_stop(struct ipts_context *ipts) { int ret = 0; + if (!ipts) + return -EFAULT; + + /* + * The HID device must be gone before the module can unload, or opening its hidraw node + * later runs freed module code. It is kept only while the receiver thread, which feeds + * it, is still running. + */ ret = _ipts_control_stop(ipts); if (ret) return ret; ret = ipts_hid_free(ipts); - if (ret) { + if (ret) dev_err(ipts->dev, "Failed to free HID device: %d\n", ret); - return ret; - } return 0; } diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c index 211315371a7f6..144c8e5ffcd96 100644 --- a/drivers/hid/ipts/receiver.c +++ b/drivers/hid/ipts/receiver.c @@ -252,10 +252,13 @@ int ipts_receiver_stop(struct ipts_context *ipts) return -EFAULT; /* - * A start that failed early never started the thread, so there is no reference to drop. + * A start that failed early never started the thread: there is no reference to drop, + * and the exit code of an earlier thread must not be reported again. */ - if (!ipts->receiver_loop.thread) + if (!ipts->receiver_loop.thread) { + ipts->receiver_loop.exit_code = 0; return 0; + } ret = ipts_thread_stop(&ipts->receiver_loop, IPTS_RECEIVER_STOP_TIMEOUT_MS); if (ret == -ETIMEDOUT) { @@ -271,9 +274,6 @@ int ipts_receiver_stop(struct ipts_context *ipts) */ ipts_context_put(ipts); - if (!ret) - ret = ipts->receiver_loop.exit_code; - if (ret) { dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret); return ret; From c3e3cb4d29c5d94cbcc5536cbd7140741c8d4715 Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:09:56 +0000 Subject: [PATCH 06/19] hid: ipts: Stop the receiver loops promptly and wait for the stop confirmation Once a stop is requested, the receiver loops still finished every buffer of the current round before they looked at the stop flag again, and the poll loop walked through every doorbell value it had missed, however far the doorbell had moved. Check the stop flag inside the inner loops too, and never walk back further than the buffers that can hold new data. The poll loop sent QUIESCE_IO only when it saw the stop flag at the top of a round. A stop requested while it handled buffers ended the loop at the bottom check without sending it, and the device was never told to stop. Send it after the loop instead, as the event loop does, and then give back the buffers the loop did not get to, as before: in poll mode the device confirms the stop only once every filled buffer was given back. At the end of a loop, the stop handshake gave up on the QUIESCE_IO confirmation as soon as the last READY_FOR_DATA reply was missing, and reported a missing reply as success. Starting the device again before it has stopped can leave its firmware unresponsive until the next boot, and freeing buffers it still writes to corrupts memory. Collect the last READY_FOR_DATA reply, then wait for the QUIESCE_IO confirmation for a bounded time in any case, and return success only when the device confirmed the stop. Otherwise the stop keeps the DMA buffers. That is a trade-off: a device that never confirms its stop leaks one set of DMA buffers per stop, and that repeats on every suspend and every restart, with a warning each time. Memory the device may still write to is never freed. Every step after the stop request is bounded now, apart from the waits inside the MEI core that a send can still make, so the time a stop waits for the receiver thread is the sum of those bounds instead of a guess. A slow device then still gets its stop confirmed, instead of leaving a thread behind that keeps the buffers. The bounds move to receiver.h, so that the waits of the stop path can be derived from them too. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/receiver.c | 136 ++++++++++++++++++++++-------------- drivers/hid/ipts/receiver.h | 24 +++++++ 2 files changed, 106 insertions(+), 54 deletions(-) diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c index 144c8e5ffcd96..ef76737b8157a 100644 --- a/drivers/hid/ipts/receiver.c +++ b/drivers/hid/ipts/receiver.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,21 @@ static u32 ipts_receiver_current_doorbell(struct ipts_context *ipts) return *doorbell; } +/* + * Only the last IPTS_BUFFERS buffers can hold new data, so never walk further back than that, + * whatever value the doorbell has. Returns the first buffer to look at. + */ +static u32 ipts_receiver_first_new(u32 doorbell, u32 lastdb) +{ + if ((s32)(doorbell - lastdb) < 0) + return doorbell; + + if (doorbell - lastdb > IPTS_BUFFERS) + return doorbell - IPTS_BUFFERS; + + return lastdb; +} + static void ipts_receiver_backoff(time64_t last, u32 n) { /* @@ -47,6 +63,62 @@ static void ipts_receiver_backoff(time64_t last, u32 n) msleep(200); } +/* + * End of a receiver loop, after QUIESCE_IO was sent: collect the last READY_FOR_DATA reply, + * then wait for the QUIESCE_IO confirmation, even if that reply was lost. Starting the + * device again before it has stopped can leave its firmware unresponsive until the next + * boot. + * + * Returns: 0 only if the device confirmed the stop. Otherwise the device may still write to + * its buffers, and the caller must not free them. + */ +static int ipts_receiver_finish(struct ipts_context *ipts) +{ + int ret = 0; + unsigned long end = 0; + + ret = ipts_control_wait_data(ipts, true); + if (ret && ret != -EAGAIN) + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret); + + end = jiffies + msecs_to_jiffies(IPTS_RECEIVER_STOP_CONFIRM_MS); + + do { + ret = ipts_control_wait_flush(ipts); + } while (ret == -EAGAIN && time_before(jiffies, end)); + + if (ret) { + dev_err(ipts->dev, "Device did not confirm the stop: %d\n", ret); + return ret == -EAGAIN ? -ETIMEDOUT : ret; + } + + return 0; +} + +/* + * In poll mode, the device confirms QUIESCE_IO only after every buffer it filled was given + * back. Give back the ones the loop did not get to, without passing their data on: HID input + * is already disabled. The time is bounded, and the first failed feedback ends it. + */ +static void ipts_receiver_return_buffers(struct ipts_context *ipts, u32 lastdb) +{ + int ret = 0; + u32 doorbell = ipts_receiver_current_doorbell(ipts); + unsigned long end = jiffies + msecs_to_jiffies(IPTS_RECEIVER_STOP_CONFIRM_MS); + + lastdb = ipts_receiver_first_new(doorbell, lastdb); + + while (lastdb != doorbell && time_before(jiffies, end)) { + ret = ipts_control_refill_buffer(ipts, lastdb % IPTS_BUFFERS); + if (ret) { + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret); + return; + } + + lastdb++; + } +} + static int ipts_receiver_event_loop(struct ipts_thread *thread) { int ret = 0; @@ -68,7 +140,7 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread) while (!ipts_thread_should_stop(thread)) { int i = 0; - for (i = 0; i < IPTS_BUFFERS; i++) { + for (i = 0; i < IPTS_BUFFERS && !ipts_thread_should_stop(thread); i++) { ret = ipts_control_wait_data(ipts, false); if (ret == -EAGAIN) break; @@ -105,27 +177,7 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread) return ret; } - ret = ipts_control_wait_data(ipts, true); - if (ret) { - dev_err(ipts->dev, "Failed to wait for data: %d\n", ret); - - if (ret != -EAGAIN) - return ret; - else - return 0; - } - - ret = ipts_control_wait_flush(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret); - - if (ret != -EAGAIN) - return ret; - else - return 0; - } - - return 0; + return ipts_receiver_finish(ipts); } static int ipts_receiver_poll_loop(struct ipts_thread *thread) @@ -149,23 +201,16 @@ static int ipts_receiver_poll_loop(struct ipts_thread *thread) dev_info(ipts->dev, "IPTS running in poll mode\n"); - while (true) { - if (ipts_thread_should_stop(thread)) { - ret = ipts_control_request_flush(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to request flush: %d\n", ret); - return ret; - } - } - + while (!ipts_thread_should_stop(thread)) { doorbell = ipts_receiver_current_doorbell(ipts); + lastdb = ipts_receiver_first_new(doorbell, lastdb); /* * After filling up one of the data buffers, IPTS will increment * the doorbell. The value of the doorbell stands for the *next* * buffer that IPTS is going to fill. */ - while (lastdb != doorbell) { + while (lastdb != doorbell && !ipts_thread_should_stop(thread)) { buffer = lastdb % IPTS_BUFFERS; ret = ipts_hid_input_data(ipts, buffer); @@ -186,27 +231,15 @@ static int ipts_receiver_poll_loop(struct ipts_thread *thread) ipts_receiver_backoff(last, 5); } - ret = ipts_control_wait_data(ipts, true); + ret = ipts_control_request_flush(ipts); if (ret) { - dev_err(ipts->dev, "Failed to wait for data: %d\n", ret); - - if (ret != -EAGAIN) - return ret; - else - return 0; + dev_err(ipts->dev, "Failed to request flush: %d\n", ret); + return ret; } - ret = ipts_control_wait_flush(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret); - - if (ret != -EAGAIN) - return ret; - else - return 0; - } + ipts_receiver_return_buffers(ipts, lastdb); - return 0; + return ipts_receiver_finish(ipts); } int ipts_receiver_start(struct ipts_context *ipts) @@ -239,11 +272,6 @@ int ipts_receiver_start(struct ipts_context *ipts) return 0; } -/* - * How long a stop waits for the receiver thread to return. - */ -#define IPTS_RECEIVER_STOP_TIMEOUT_MS 10000 - int ipts_receiver_stop(struct ipts_context *ipts) { int ret = 0; diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h index 3de7da62d40c1..30b60aaa45e0c 100644 --- a/drivers/hid/ipts/receiver.h +++ b/drivers/hid/ipts/receiver.h @@ -8,7 +8,31 @@ #ifndef IPTS_RECEIVER_H #define IPTS_RECEIVER_H +#include "cmd.h" #include "context.h" +#include "mei.h" + +/* + * How long the end of a stop waits for the device to confirm QUIESCE_IO. + */ +#define IPTS_RECEIVER_STOP_CONFIRM_MS 3000 + +/* + * The longest one command (a send and its reply) can take. + */ +#define IPTS_RECEIVER_CMD_MS (IPTS_MEI_SEND_TIMEOUT_MS + IPTS_CMD_DEFAULT_TIMEOUT) + +/* + * How long a stop waits for the receiver thread to return: the sum of the bounds of what the + * thread can still do once it is asked to stop. It finishes the buffer it is handling (a + * command and a send), sends QUIESCE_IO, gives back buffers in poll mode (the confirmation + * time plus one command), collects the last READY_FOR_DATA reply, and waits for the + * confirmation (that time plus one reply). One second more covers the back-off sleep and + * scheduling. + */ +#define IPTS_RECEIVER_STOP_TIMEOUT_MS \ + (2 * IPTS_RECEIVER_CMD_MS + 2 * IPTS_MEI_SEND_TIMEOUT_MS + \ + 2 * IPTS_RECEIVER_STOP_CONFIRM_MS + 2 * IPTS_CMD_DEFAULT_TIMEOUT + 1000) int ipts_receiver_start(struct ipts_context *ipts); int ipts_receiver_stop(struct ipts_context *ipts); From 011d5859e813653493f619c6670447a153e13c04 Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:11:40 +0000 Subject: [PATCH 07/19] hid: ipts: Run the EDS1 mode-switch restart from a work item On EDS1 devices, a HID request that switches the mode restarts the device from inside the HID callback, and nothing serialized that restart with ipts_remove(): both could stop the receiver thread and free the buffers at the same time. A stop that waits for running HID callbacks, as the next change makes it do, would also wait for the very callback that called it. Run the restart from a work item instead, under a mutex that remove takes for its own stop. A queued restart holds a context reference until it has run. Remove marks the context as going away first, so no new restart is scheduled, and a restart that is already running checks that mark again before it starts the device. Remove never waits for the work without a bound: the worker can be the task that is stuck, or that died with the mutex held, and reboot and suspend must still finish. It cancels a queued restart without waiting, before and after its stop, and takes the mutex with a bounded wait. The bound is derived from the bounds of a start and a stop, the way the receiver's stop timeout is: it covers whatever a running restart can still do once remove has marked the context. If a restart holds the mutex past that bound, it is stuck, and remove skips the stop and frees nothing that restart can still use: the restart holds a context reference, and the context holds the MEI device. A restart whose stop was not confirmed by the device, so that the DMA buffers were leaked, no longer starts the device again: starting a device that has not stopped can leave its firmware unresponsive until the next boot. The stop reports that case with a return value of its own. Touch input then stays off until the driver is probed again. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 16 +++++-- drivers/hid/ipts/control.c | 91 +++++++++++++++++++++++++++++++++++--- drivers/hid/ipts/control.h | 32 +++++++++++++- drivers/hid/ipts/main.c | 49 ++++++++++++++++++++ 4 files changed, 176 insertions(+), 12 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index db0f4c2793961..0a49c178c59d9 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "mei.h" #include "resources.h" @@ -46,9 +47,18 @@ struct ipts_context { bool hid_active; struct hid_device *hid; + /* + * Serializes a restart with remove. Restarts requested from a HID callback (the EDS1 + * mode switch) run from restart_work, never inside the callback. + */ + struct mutex ctl_lock; + struct work_struct restart_work; + bool removing; + /* * Freed when the last reference is dropped. Probe holds one (dropped by devres), and the - * receiver thread, the HID device and every holder of message_lock hold one each. + * receiver thread, the HID device, every holder of message_lock and a queued restart + * hold one each. */ struct kref refcount; @@ -69,8 +79,8 @@ struct ipts_context { void ipts_context_release(struct kref *refcount); /* - * Every user of the context (the receiver thread, the HID device, a holder of message_lock) - * takes a reference, so the context is freed only after the last user is gone. + * Every user of the context (the receiver thread, the HID device, a holder of message_lock, a + * queued restart) takes a reference, so the context is freed only after the last user is gone. */ static inline void ipts_context_get(struct ipts_context *ipts) { diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index c600ff519acfb..8867e51de3642 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -12,7 +12,9 @@ #include #include #include +#include #include +#include #include "cmd.h" #include "context.h" @@ -438,6 +440,12 @@ int ipts_control_start(struct ipts_context *ipts) return 0; } +/* + * Returned by _ipts_control_stop() when the receiver thread is gone, but the device did not + * confirm its stop: its DMA buffers were leaked, and it must not be started again. + */ +#define IPTS_STOP_LEAKED 1 + static int _ipts_control_stop(struct ipts_context *ipts) { int ret = 0; @@ -461,14 +469,14 @@ static int _ipts_control_stop(struct ipts_context *ipts) if (ret) { dev_err(ipts->dev, "Receiver thread died without stopping the device: %d\n", ret); ipts_control_leak_buffers(ipts); - return 0; + return IPTS_STOP_LEAKED; } if (ipts->receiver_loop.exit_code) { dev_err(ipts->dev, "Failed to stop the device: %d\n", ipts->receiver_loop.exit_code); ipts_control_leak_buffers(ipts); - return 0; + return IPTS_STOP_LEAKED; } ret = ipts_resources_free(&ipts->resources); @@ -491,7 +499,7 @@ int ipts_control_stop(struct ipts_context *ipts) * it, is still running. */ ret = _ipts_control_stop(ipts); - if (ret) + if (ret < 0) return ret; ret = ipts_hid_free(ipts); @@ -501,22 +509,91 @@ int ipts_control_stop(struct ipts_context *ipts) return 0; } -int ipts_control_restart(struct ipts_context *ipts) +static void ipts_control_restart_work(struct work_struct *work) { int ret = 0; + struct ipts_context *ipts = container_of(work, struct ipts_context, restart_work); + + mutex_lock(&ipts->ctl_lock); + + if (READ_ONCE(ipts->removing)) + goto out; ret = _ipts_control_stop(ipts); - if (ret) - return ret; + if (ret == IPTS_STOP_LEAKED) { + /* + * Starting a device that did not confirm its stop can leave its firmware + * unresponsive until the next boot. + */ + dev_err(ipts->dev, "Device stop not confirmed, not restarting IPTS\n"); + goto out; + } + + if (ret) { + dev_err(ipts->dev, "Failed to stop IPTS for a restart: %d\n", ret); + goto out; + } /* * Wait a second to give the sensor time to fully shut down. */ msleep(1000); + /* + * A remove that started meanwhile waits for this, and stops the device anyway. + */ + if (READ_ONCE(ipts->removing)) + goto out; + ret = ipts_control_start(ipts); if (ret) - return ret; + dev_err(ipts->dev, "Failed to restart IPTS: %d\n", ret); + +out: + mutex_unlock(&ipts->ctl_lock); + + /* + * Drop the reference that ipts_control_restart() took for this run. + */ + ipts_context_put(ipts); +} + +void ipts_control_init(struct ipts_context *ipts) +{ + mutex_init(&ipts->ctl_lock); + INIT_WORK(&ipts->restart_work, ipts_control_restart_work); + ipts->removing = false; +} + +int ipts_control_restart(struct ipts_context *ipts) +{ + if (!ipts) + return -EFAULT; + + /* + * Called from a HID callback, which the stop has to wait for, so the restart itself + * runs from a work item. + */ + if (READ_ONCE(ipts->removing)) + return -ENODEV; + + /* + * A queued restart holds a reference until it ran or was cancelled. If one is queued + * already, it holds its own. + */ + ipts_context_get(ipts); + if (!schedule_work(&ipts->restart_work)) + ipts_context_put(ipts); return 0; } + +void ipts_control_cancel_restart(struct ipts_context *ipts) +{ + /* + * Never waits: a restart that is running holds ctl_lock, which remove waits for with a + * bound. A queued one that is cancelled drops its reference here. + */ + if (cancel_work(&ipts->restart_work)) + ipts_context_put(ipts); +} diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h index 26629c5144edb..9a7be6cfc25be 100644 --- a/drivers/hid/ipts/control.h +++ b/drivers/hid/ipts/control.h @@ -11,6 +11,8 @@ #include #include "context.h" +#include "mei.h" +#include "receiver.h" #include "spec-data.h" #include "spec-device.h" @@ -99,6 +101,20 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff return ipts_control_send_feedback(ipts, buffer); } +/* + * The longest ipts_control_start() takes on a restart. Only EDS1 devices restart. They send + * three commands that wait for their reply (GET_DEVICE_INFO, SET_MODE and SET_MEM_WINDOW) and + * READY_FOR_DATA, which is only sent. GET_DESCRIPTOR is sent only to newer devices, since the + * EDS1 HID descriptor is built into the driver, and the rest of a start sends no command. The + * bound still counts four replies; the fourth is margin. + */ +#define IPTS_CONTROL_START_TIMEOUT_MS (4 * IPTS_RECEIVER_CMD_MS + IPTS_MEI_SEND_TIMEOUT_MS) + +/* + * The longest a stop takes: the stop of the receiver thread. + */ +#define IPTS_CONTROL_STOP_TIMEOUT_MS IPTS_RECEIVER_STOP_TIMEOUT_MS + /** * ipts_control_start() - Initialized the device and starts the data flow. * @ipts: The IPTS driver context. @@ -116,11 +132,23 @@ int ipts_control_start(struct ipts_context *ipts); int ipts_control_stop(struct ipts_context *ipts); /** - * ipts_control_restart() - Stops the device and starts it again. + * ipts_control_restart() - Schedules a stop and start of the device. * @ipts: The IPTS driver context. * - * Returns: 0 on success, <0 on error. + * Returns: 0 if the restart was scheduled, <0 on error. */ int ipts_control_restart(struct ipts_context *ipts); +/** + * ipts_control_cancel_restart() - Cancels a restart that is queued but not running yet. + * @ipts: The IPTS driver context. + */ +void ipts_control_cancel_restart(struct ipts_context *ipts); + +/** + * ipts_control_init() - Initializes the lock and the work item of the context. + * @ipts: The IPTS driver context. + */ +void ipts_control_init(struct ipts_context *ipts); + #endif /* IPTS_CONTROL_H */ diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index 6558a23c7c8ca..fa518f4ebb4dd 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -132,6 +132,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(ipts); mei_cldev_set_drvdata(cldev, ipts); @@ -154,6 +155,31 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id */ #define IPTS_REMOVE_TIMEOUT_MS 12000 +/* + * How long ipts_remove() waits for a running restart to release ctl_lock: the longest a + * restart can still hold it once removing is set. Before its last check of removing, that is + * the rest of a stop and the one-second pause; after it, a start. The sum of a start and a + * stop covers either. One second more covers scheduling. A restart that holds the lock for + * longer is stuck. + */ +#define IPTS_REMOVE_LOCK_TIMEOUT_MS \ + (IPTS_CONTROL_START_TIMEOUT_MS + IPTS_CONTROL_STOP_TIMEOUT_MS + 1000) +#define IPTS_REMOVE_LOCK_POLL_MS 10 + +static bool ipts_remove_lock(struct ipts_context *ipts) +{ + unsigned long end = jiffies + msecs_to_jiffies(IPTS_REMOVE_LOCK_TIMEOUT_MS); + + while (!mutex_trylock(&ipts->ctl_lock)) { + if (time_after(jiffies, end)) + return false; + + msleep(IPTS_REMOVE_LOCK_POLL_MS); + } + + return true; +} + static void ipts_remove(struct mei_cl_device *cldev) { int ret = 0; @@ -166,7 +192,30 @@ static void ipts_remove(struct mei_cl_device *cldev) ipts = mei_cldev_get_drvdata(cldev); + /* + * No new restarts, and cancel a queued one. A restart that is already running sees + * removing before it starts the device again, and is waited for, bounded, through + * ctl_lock. One scheduled by a HID callback that raced with this is cancelled after the + * stop, or sees removing and does nothing. + */ + WRITE_ONCE(ipts->removing, true); + ipts_control_cancel_restart(ipts); + + if (!ipts_remove_lock(ipts)) { + /* + * A restart is stuck with the device half stopped or started. Nothing it uses + * may be freed, so the stop is skipped. + */ + dev_err(&cldev->dev, "A restart of IPTS did not finish, not stopping it\n"); + mei_cldev_disable(cldev); + return; + } + ret = ipts_control_stop(ipts); + mutex_unlock(&ipts->ctl_lock); + + ipts_control_cancel_restart(ipts); + if (ret) dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret); From 17b09af3934ad01a70445cc93c544963937c1cbf Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:13:01 +0000 Subject: [PATCH 08/19] hid: ipts: Wait for running HID callbacks before stopping Stopping only cleared hid_active, which new HID callbacks check on entry. A callback that had already passed that check kept running while the stop freed the buffers it used: a SET_FEATURE request writing the hid2me buffer, or a GET_FEATURE request waiting for a reply in a buffer that was about to go away. Count the callbacks that run driver code, and make the stop wait for the count to drop to zero, with a timeout. A pending GET_FEATURE request is released at once instead of waiting for its own timeout, and one that starts while the stop runs returns at once. The receiver thread's HID input is counted the same way. Once the count has dropped to zero, the receiver can no longer reach the HID device, so the HID device is destroyed even when the receiver thread does not stop. Before, a receiver that did not stop kept the HID device registered, and after rmmod its hidraw node ran freed module code. The receiver thread is asked to stop in any case. A callback that returns while it stops was only slow, and the stop goes on as usual, except that the buffers are leaked instead of freed: the callback may have sent the device a command after the device confirmed its stop, and the device may answer it by writing to them. A callback that still runs after the timeout and the receiver stop is most likely stuck for good, in a task that died inside it, possibly with HID core locks held. That includes a receiver that died inside hid_input_report(). It can still touch the buffers, and destroying the HID device could block forever on those locks, so in that case nothing is freed and the error is passed on. A later stop then leaks the buffers instead of freeing them, although it finds no receiver thread anymore. So rmmod after a HID callback died, or while one never returns, still leaves the HID device registered, and its hidraw node runs freed module code. Destroying it would trade that for a remove that can block, and with it reboot and suspend. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 14 ++++++ drivers/hid/ipts/control.c | 77 +++++++++++++++++++++++++------ drivers/hid/ipts/control.h | 16 +++++-- drivers/hid/ipts/eds2.c | 10 ++++ drivers/hid/ipts/hid.c | 94 ++++++++++++++++++++++++++++++++------ drivers/hid/ipts/hid.h | 9 ++++ drivers/hid/ipts/main.c | 3 ++ 7 files changed, 193 insertions(+), 30 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index 0a49c178c59d9..c2b853ab5cd4b 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -47,6 +47,13 @@ struct ipts_context { bool hid_active; struct hid_device *hid; + /* + * Number of HID callbacks currently running driver code. Stopping waits, bounded, + * until it drops to 0. + */ + atomic_t hid_users; + wait_queue_head_t hid_users_wq; + /* * Serializes a restart with remove. Restarts requested from a HID callback (the EDS1 * mode switch) run from restart_work, never inside the callback. @@ -73,6 +80,13 @@ struct ipts_context { struct ipts_device_info info; struct ipts_resources resources; + /* + * Set by a stop whose wait for running HID callbacks timed out: a callback that outlived + * the wait may have sent the device a command after it confirmed the stop. The buffers + * are then leaked instead of freed, by that stop or, if it kept everything, by the next. + */ + bool buffers_unconfirmed; + struct ipts_thread receiver_loop; }; diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 8867e51de3642..edcb22185ffc1 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -357,6 +357,7 @@ static void ipts_control_leak_buffers(struct ipts_context *ipts) kfree(ipts->resources.report.address); memset(&ipts->resources, 0, sizeof(ipts->resources)); + ipts->buffers_unconfirmed = false; } int ipts_control_start(struct ipts_context *ipts) @@ -442,25 +443,58 @@ int ipts_control_start(struct ipts_context *ipts) /* * Returned by _ipts_control_stop() when the receiver thread is gone, but the device did not - * confirm its stop: its DMA buffers were leaked, and it must not be started again. + * confirm its stop, or a HID request overlapped it: its DMA buffers were leaked, and it must + * not be started again. */ #define IPTS_STOP_LEAKED 1 static int _ipts_control_stop(struct ipts_context *ipts) { int ret = 0; + bool busy = false; if (!ipts) return -EFAULT; - ipts_hid_disable(ipts); dev_info(ipts->dev, "Stopping IPTS\n"); + busy = ipts_hid_quiesce(ipts, IPTS_HID_QUIESCE_TIMEOUT_MS) == -ETIMEDOUT; + + /* + * A HID callback that outlives the wait may send the device a command after it confirmed + * the stop below, and the device may answer by writing to the buffers again. They are + * leaked then, by this stop or, if it keeps everything, by a later one. + */ + if (busy) + ipts->buffers_unconfirmed = true; + + /* + * The receiver is asked to stop in any case. A HID callback that was only slow may + * return meanwhile; once the count is zero, no new one starts, since HID input is off. + */ ret = ipts_receiver_stop(ipts); + + if (busy && atomic_read(&ipts->hid_users) == 0) + busy = false; + + if (busy) { + /* + * A HID request, or the receiver's HID input, is stuck in driver code, most + * likely in a task that died inside it. It can still touch the buffers, and + * destroying the HID device could block forever on HID core locks that task + * holds, so nothing is freed. A later stop must not free the buffers either, + * although it finds no thread anymore. + */ + dev_err(ipts->dev, "A HID request did not return (%d still running)\n", + atomic_read(&ipts->hid_users)); + return -EBUSY; + } + if (ret == -ETIMEDOUT) { /* - * The thread is still running and can still touch the buffers and the HID - * device, so nothing is freed. + * The thread is still running and can still touch the buffers, so they are + * kept. It can no longer reach the HID device: its HID input is counted, and + * nothing counted is running anymore. */ dev_err(ipts->dev, "Receiver thread did not stop\n"); return ret; @@ -479,6 +513,12 @@ static int _ipts_control_stop(struct ipts_context *ipts) return IPTS_STOP_LEAKED; } + if (ipts->buffers_unconfirmed) { + dev_err(ipts->dev, "Device may still use its buffers, leaking them\n"); + ipts_control_leak_buffers(ipts); + return IPTS_STOP_LEAKED; + } + ret = ipts_resources_free(&ipts->resources); if (ret) dev_err(ipts->dev, "Failed to free resources: %d\n", ret); @@ -489,24 +529,32 @@ static int _ipts_control_stop(struct ipts_context *ipts) int ipts_control_stop(struct ipts_context *ipts) { int ret = 0; + int err = 0; if (!ipts) return -EFAULT; /* * The HID device must be gone before the module can unload, or opening its hidraw node - * later runs freed module code. It is kept only while the receiver thread, which feeds - * it, is still running. + * later runs freed module code. Once no HID callback and no HID input of the receiver + * runs, and none can start, it is destroyed, even when the receiver thread did not stop. + * It is kept only while a HID callback is stuck. */ ret = _ipts_control_stop(ipts); - if (ret < 0) + if (ret == -EBUSY) return ret; - ret = ipts_hid_free(ipts); - if (ret) - dev_err(ipts->dev, "Failed to free HID device: %d\n", ret); + err = ipts_hid_free(ipts); + if (err) + dev_err(ipts->dev, "Failed to free HID device: %d\n", err); - return 0; + /* + * Leaked buffers are not an error for the callers. + */ + if (ret == IPTS_STOP_LEAKED) + return 0; + + return ret; } static void ipts_control_restart_work(struct work_struct *work) @@ -522,10 +570,11 @@ static void ipts_control_restart_work(struct work_struct *work) ret = _ipts_control_stop(ipts); if (ret == IPTS_STOP_LEAKED) { /* - * Starting a device that did not confirm its stop can leave its firmware - * unresponsive until the next boot. + * The device did not confirm its stop, or a HID request may have sent it a + * command after it did. Starting it again can leave its firmware unresponsive + * until the next boot. */ - dev_err(ipts->dev, "Device stop not confirmed, not restarting IPTS\n"); + dev_err(ipts->dev, "Device may not have stopped, not restarting IPTS\n"); goto out; } diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h index 9a7be6cfc25be..791d4e4cbe942 100644 --- a/drivers/hid/ipts/control.h +++ b/drivers/hid/ipts/control.h @@ -111,9 +111,16 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff #define IPTS_CONTROL_START_TIMEOUT_MS (4 * IPTS_RECEIVER_CMD_MS + IPTS_MEI_SEND_TIMEOUT_MS) /* - * The longest a stop takes: the stop of the receiver thread. + * How long stopping waits for running HID callbacks. The longest of them, a GET_FEATURE + * request, is released at once by ipts_hid_quiesce(). */ -#define IPTS_CONTROL_STOP_TIMEOUT_MS IPTS_RECEIVER_STOP_TIMEOUT_MS +#define IPTS_HID_QUIESCE_TIMEOUT_MS 12000 + +/* + * The longest a stop takes: the wait for running HID callbacks, then the stop of the receiver + * thread. + */ +#define IPTS_CONTROL_STOP_TIMEOUT_MS (IPTS_HID_QUIESCE_TIMEOUT_MS + IPTS_RECEIVER_STOP_TIMEOUT_MS) /** * ipts_control_start() - Initialized the device and starts the data flow. @@ -127,7 +134,10 @@ int ipts_control_start(struct ipts_context *ipts); * ipts_control_stop() - Stops the data flow and resets the device. * @ipts: The IPTS driver context. * - * Returns: 0 on success, <0 on error. + * Returns: 0 if the receiver thread stopped (the DMA buffers are leaked if the device did not + * confirm the stop, or a HID callback outlived the wait for it), -ETIMEDOUT if it is still + * running (the HID device is destroyed, the buffers are kept), -EBUSY if a HID callback did + * not return (everything is kept), <0 on other errors. */ int ipts_control_stop(struct ipts_context *ipts); diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c index 639940794615d..af00ca9d435b9 100644 --- a/drivers/hid/ipts/eds2.c +++ b/drivers/hid/ipts/eds2.c @@ -67,6 +67,16 @@ static int ipts_eds2_get_feature(struct ipts_context *ipts, u8 *buffer, size_t s memset(&ipts->feature_report, 0, sizeof(ipts->feature_report)); reinit_completion(&ipts->feature_event); + /* + * A stop that started before the reinit above already released the completion, so + * do not wait for a reply that will not come. + */ + smp_mb(); + if (!READ_ONCE(ipts->hid_active)) { + ret = -ENODEV; + goto out; + } + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size); if (ret) { dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret); diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c index 4e8e85f745a84..b11f2cfc09f6f 100644 --- a/drivers/hid/ipts/hid.c +++ b/drivers/hid/ipts/hid.c @@ -31,6 +31,54 @@ void ipts_hid_disable(struct ipts_context *ipts) WRITE_ONCE(ipts->hid_active, false); } +static void ipts_hid_exit(struct ipts_context *ipts) +{ + if (atomic_dec_and_test(&ipts->hid_users)) + wake_up_all(&ipts->hid_users_wq); +} + +/* + * Every HID callback that runs driver code, and the receiver's HID input, is counted, so that + * stopping can wait until none of them can touch the buffers or the HID device anymore. + */ +static bool ipts_hid_enter(struct ipts_context *ipts) +{ + atomic_inc(&ipts->hid_users); + /* + * Order the count above before reading hid_active, so a stop that cleared it cannot + * miss this callback. + */ + smp_mb__after_atomic(); + + if (!READ_ONCE(ipts->hid_active)) { + ipts_hid_exit(ipts); + return false; + } + + return true; +} + +int ipts_hid_quiesce(struct ipts_context *ipts, unsigned int timeout_ms) +{ + ipts_hid_disable(ipts); + /* + * Order the disable above before releasing the completion, so a callback that passed the + * check before it does not wait for a reply that will not come. + */ + smp_mb(); + + /* + * Release a GET_FEATURE request that waits for a reply which will not come anymore. + */ + complete_all(&ipts->feature_event); + + if (!wait_event_timeout(ipts->hid_users_wq, atomic_read(&ipts->hid_users) == 0, + msecs_to_jiffies(timeout_ms))) + return -ETIMEDOUT; + + return 0; +} + static int ipts_hid_start(struct hid_device *hid) { return 0; @@ -56,7 +104,7 @@ static int ipts_hid_parse(struct hid_device *hid) if (!ipts) return -EFAULT; - if (!READ_ONCE(ipts->hid_active)) + if (!ipts_hid_enter(ipts)) return -ENODEV; if (ipts->info.intf_eds == 1) @@ -64,6 +112,8 @@ static int ipts_hid_parse(struct hid_device *hid) else ret = ipts_eds2_get_descriptor(ipts, &buffer, &size); + ipts_hid_exit(ipts); + if (ret) { dev_err(ipts->dev, "Failed to allocate HID descriptor: %d\n", ret); return ret; @@ -83,6 +133,7 @@ static int ipts_hid_parse(struct hid_device *hid) static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, __u8 *buffer, size_t size, unsigned char report_type, int request_type) { + int ret = 0; struct ipts_context *ipts = NULL; if (!hid) @@ -93,16 +144,19 @@ static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, if (!ipts) return -EFAULT; - if (!READ_ONCE(ipts->hid_active)) + if (!ipts_hid_enter(ipts)) return -ENODEV; if (ipts->info.intf_eds == 1) { - return ipts_eds1_raw_request(ipts, buffer, size, report_id, report_type, - request_type); + ret = ipts_eds1_raw_request(ipts, buffer, size, report_id, report_type, + request_type); } else { - return ipts_eds2_raw_request(ipts, buffer, size, report_id, report_type, - request_type); + ret = ipts_eds2_raw_request(ipts, buffer, size, report_id, report_type, + request_type); } + + ipts_hid_exit(ipts); + return ret; } static struct hid_ll_driver ipts_hid_driver = { @@ -114,21 +168,15 @@ static struct hid_ll_driver ipts_hid_driver = { .raw_request = ipts_hid_raw_request, }; -int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer) +static int ipts_hid_handle_data(struct ipts_context *ipts, u32 buffer) { u8 *temp = NULL; struct ipts_hid_header *frame = NULL; struct ipts_data_header *header = NULL; - if (!ipts) - return -EFAULT; - if (!ipts->hid) return -ENODEV; - if (!READ_ONCE(ipts->hid_active)) - return -ENODEV; - header = (struct ipts_data_header *)ipts->resources.data[buffer].address; temp = ipts->resources.report.address; @@ -171,6 +219,26 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer) return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1); } +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer) +{ + int ret = 0; + + if (!ipts) + return -EFAULT; + + /* + * Counted like a HID callback. Once a stop has seen no callback running, the receiver + * thread can no longer reach the HID device, even if the thread itself does not stop. + */ + if (!ipts_hid_enter(ipts)) + return -ENODEV; + + ret = ipts_hid_handle_data(ipts, buffer); + ipts_hid_exit(ipts); + + return ret; +} + int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info) { int ret = 0; diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h index 1ebe77447903a..d3e89e412f179 100644 --- a/drivers/hid/ipts/hid.h +++ b/drivers/hid/ipts/hid.h @@ -16,6 +16,15 @@ void ipts_hid_enable(struct ipts_context *ipts); void ipts_hid_disable(struct ipts_context *ipts); +/** + * ipts_hid_quiesce() - Disables HID callbacks and waits for running ones to return. + * @ipts: The IPTS driver context. + * @timeout_ms: How long to wait at most. + * + * Returns: 0 when no callback is running anymore, -ETIMEDOUT if one did not return. + */ +int ipts_hid_quiesce(struct ipts_context *ipts, unsigned int timeout_ms); + int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer); int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info); diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index fa518f4ebb4dd..7fd2487882357 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -134,6 +134,9 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id init_completion(&ipts->feature_event); ipts_control_init(ipts); + atomic_set(&ipts->hid_users, 0); + init_waitqueue_head(&ipts->hid_users_wq); + mei_cldev_set_drvdata(cldev, ipts); ret = ipts_control_start(ipts); From 6e15adc69256ca8d34996737bd9ee6a1ea2d1feb Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:13:50 +0000 Subject: [PATCH 09/19] hid: ipts: Undo a failed start When a step of ipts_control_start() failed, it returned at once and left behind whatever the earlier steps had set up: the buffers, and a receiver thread that kept running against a device that was never told to stop. An allocation error from hid_allocate_device() was even kept in ipts->hid, where later code took it for a device. Tear down on failure what the start set up. Buffers the device was never given are freed. Buffers it may have been given are leaked, since it was never told to stop: that covers a GET_DESCRIPTOR request that got no reply, because the device may still write the descriptor. Once the receiver thread runs, the normal stop path stops it, and keeps what a thread that does not stop can still use. On an EDS1 restart, that stop also removes the HID device that probe created. A HID device that failed to register is no longer destroyed in ipts_hid_init(), while the receiver may already use it, but by that stop, once HID input has stopped. The receiver thread already runs while ipts_hid_init() sets up the HID device, so the device is now set up in a local variable and published in ipts->hid only once it is complete. The receiver never sees an error pointer or a half set-up device. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 39 ++++++++++++++++++++++----- drivers/hid/ipts/control.h | 3 +++ drivers/hid/ipts/hid.c | 54 ++++++++++++++++++++++++-------------- drivers/hid/ipts/main.c | 6 ++--- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index edcb22185ffc1..68586560b0d1d 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -397,7 +397,15 @@ int ipts_control_start(struct ipts_context *ipts) ret = ipts_control_get_descriptor(ipts); if (ret) { dev_err(ipts->dev, "Failed to fetch HID descriptor: %d\n", ret); - return ret; + + /* + * Unless the device replied, it may still write the descriptor into its + * buffer. + */ + if (ret == -EBADR || ret == -ENODATA) + goto err_free_resources; + + goto err_leak_resources; } /* @@ -409,25 +417,25 @@ int ipts_control_start(struct ipts_context *ipts) ret = ipts_control_set_mode(ipts, ipts->mode); if (ret) { dev_err(ipts->dev, "Failed to set mode: %d\n", ret); - return ret; + goto err_free_resources; } ret = ipts_control_set_mem_window(ipts, &ipts->resources); if (ret) { dev_err(ipts->dev, "Failed to set memory window: %d\n", ret); - return ret; + goto err_leak_resources; } ret = ipts_receiver_start(ipts); if (ret) { dev_err(ipts->dev, "Failed to start receiver: %d\n", ret); - return ret; + goto err_leak_resources; } ret = ipts_control_request_data(ipts); if (ret) { dev_err(ipts->dev, "Failed to request data: %d\n", ret); - return ret; + goto err_stop; } ipts_hid_enable(ipts); @@ -435,10 +443,29 @@ int ipts_control_start(struct ipts_context *ipts) ret = ipts_hid_init(ipts, info); if (ret) { dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret); - return ret; + goto err_stop; } return 0; + +err_stop: + /* + * The receiver is running: stop it through the normal stop path, which keeps whatever + * a receiver thread that does not stop can still use. + */ + ipts_control_stop(ipts); + return ret; + +err_leak_resources: + /* + * The device may already have been given these buffers and was never told to stop. + */ + ipts_control_leak_buffers(ipts); + return ret; + +err_free_resources: + ipts_resources_free(&ipts->resources); + return ret; } /* diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h index 791d4e4cbe942..ca7e8ba68a912 100644 --- a/drivers/hid/ipts/control.h +++ b/drivers/hid/ipts/control.h @@ -126,6 +126,9 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff * ipts_control_start() - Initialized the device and starts the data flow. * @ipts: The IPTS driver context. * + * On failure, what it set up is torn down again, except what the device or a receiver thread + * that does not stop may still use: DMA buffers the device may have been given are leaked. + * * Returns: 0 on success, <0 on error. */ int ipts_control_start(struct ipts_context *ipts); diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c index b11f2cfc09f6f..dccad5ccee2f0 100644 --- a/drivers/hid/ipts/hid.c +++ b/drivers/hid/ipts/hid.c @@ -171,10 +171,15 @@ static struct hid_ll_driver ipts_hid_driver = { static int ipts_hid_handle_data(struct ipts_context *ipts, u32 buffer) { u8 *temp = NULL; + struct hid_device *hid = NULL; struct ipts_hid_header *frame = NULL; struct ipts_data_header *header = NULL; - if (!ipts->hid) + /* + * Set by ipts_hid_init() while this thread is already running. + */ + hid = READ_ONCE(ipts->hid); + if (!hid) return -ENODEV; header = (struct ipts_data_header *)ipts->resources.data[buffer].address; @@ -189,7 +194,7 @@ static int ipts_hid_handle_data(struct ipts_context *ipts, u32 buffer) return 0; if (header->type == IPTS_DATA_TYPE_HID) - return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1); + return hid_input_report(hid, HID_INPUT_REPORT, header->data, header->size, 1); if (header->type == IPTS_DATA_TYPE_GET_FEATURES) { ipts->feature_report.address = header->data; @@ -216,7 +221,7 @@ static int ipts_hid_handle_data(struct ipts_context *ipts, u32 buffer) memcpy(frame->data, header->data, header->size); - return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1); + return hid_input_report(hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1); } int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer) @@ -242,6 +247,7 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer) int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info) { int ret = 0; + struct hid_device *hid = NULL; if (!ipts) return -EFAULT; @@ -249,35 +255,43 @@ int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info) if (ipts->hid) return 0; - ipts->hid = hid_allocate_device(); - if (IS_ERR(ipts->hid)) { - int err = PTR_ERR(ipts->hid); - - dev_err(ipts->dev, "Failed to allocate HID device: %d\n", err); - return err; + hid = hid_allocate_device(); + if (IS_ERR(hid)) { + ret = PTR_ERR(hid); + dev_err(ipts->dev, "Failed to allocate HID device: %d\n", ret); + return ret; } /* * The HID device uses the context in its callbacks until it is destroyed: hold a - * reference for its whole life. + * reference for its whole life, also when hid_add_device() fails and it is left for + * ipts_control_stop() to destroy. */ ipts_context_get(ipts); - ipts->hid->driver_data = ipts; - ipts->hid->dev.parent = ipts->dev; - ipts->hid->ll_driver = &ipts_hid_driver; + hid->driver_data = ipts; + hid->dev.parent = ipts->dev; + hid->ll_driver = &ipts_hid_driver; - ipts->hid->vendor = info.vendor; - ipts->hid->product = info.product; - ipts->hid->group = HID_GROUP_GENERIC; + hid->vendor = info.vendor; + hid->product = info.product; + hid->group = HID_GROUP_GENERIC; - snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor, - info.product); + snprintf(hid->name, sizeof(hid->name), "IPTS %04X:%04X", info.vendor, info.product); - ret = hid_add_device(ipts->hid); + /* + * The receiver thread is already running and reads ipts->hid, so the device is published + * only once it is set up. The release orders the setup before the pointer. + */ + smp_store_release(&ipts->hid, hid); + + /* + * On failure ipts->hid stays set: the receiver thread may already be using it, so it + * is destroyed by ipts_control_stop() once HID input has stopped. + */ + ret = hid_add_device(hid); if (ret) { dev_err(ipts->dev, "Failed to add HID device: %d\n", ret); - ipts_hid_free(ipts); return ret; } diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index 7fd2487882357..f508483bbd810 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -161,9 +161,9 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id /* * How long ipts_remove() waits for a running restart to release ctl_lock: the longest a * restart can still hold it once removing is set. Before its last check of removing, that is - * the rest of a stop and the one-second pause; after it, a start. The sum of a start and a - * stop covers either. One second more covers scheduling. A restart that holds the lock for - * longer is stuck. + * the rest of a stop and the one-second pause; after it, a start, and a stop if the start + * fails. The sum of a start and a stop covers either. One second more covers scheduling. A + * restart that holds the lock for longer is stuck. */ #define IPTS_REMOVE_LOCK_TIMEOUT_MS \ (IPTS_CONTROL_START_TIMEOUT_MS + IPTS_CONTROL_STOP_TIMEOUT_MS + 1000) From 7001787b5838ee08e9764aa34b243dc291ef1efd Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:36:56 +0000 Subject: [PATCH 10/19] hid: ipts: Keep each FEEDBACK reply with the command it answers The receiver loop, which gives back data buffers, and HID feature requests, which use the shared hid2me buffer, both send FEEDBACK. Replies are matched to their sender by the command code only, so with several FEEDBACKs in flight a sender can take the reply to another one and return while its own command is still pending. The device then stays a reply behind: it holds its newest reply until the next command arrives, and a QUIESCE_IO sent in that state is never answered, so stopping the device times out. Allow one FEEDBACK at a time with a new feedback_lock, held from the send until the reply is taken. The lock also guards the hid2me buffer, so a second feature request cannot overwrite it while the device may still read it. GET_FEATURE releases it before waiting for its report, because the receiver loop needs it to give back the buffer the report arrives in. Lock order is feature_lock before feedback_lock. Under the lock, drop any FEEDBACK reply that is already queued before sending: it answers a sender that gave up waiting. Then take only the reply whose first payload word names the buffer that was sent (the device echoes the buffer index there), dropping others with a warning, within the usual one second limit. Two hid2me replies look alike, but the lock allows only one hid2me FEEDBACK at a time. On a Surface Pro 7 (EDS2), reloading the driver 200 times while four processes send feature requests left 108 stops unconfirmed before this change and none after it. On a KASAN and lockdep build: 160 before, 3 after, with no reports. The buffer index echo was checked on EDS2 only. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 7 +++ drivers/hid/ipts/control.c | 96 +++++++++++++++++++++++++++++++++----- drivers/hid/ipts/main.c | 1 + 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index c2b853ab5cd4b..25806ddc02233 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -37,6 +37,13 @@ struct ipts_context { struct mutex feature_lock; struct completion feature_event; + /* + * Allows one FEEDBACK command at a time, held from the send until its reply is taken, + * and guards the hid2me buffer a hid2me FEEDBACK hands to the device. GET_FEATURE takes + * it while holding feature_lock; never take feature_lock while holding it. + */ + struct mutex feedback_lock; + /* * These are not inside of struct ipts_resources * because they don't own the memory they point to. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 68586560b0d1d..eb1491acb559e 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -8,12 +8,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include "cmd.h" @@ -280,14 +282,28 @@ int ipts_control_wait_data(struct ipts_context *ipts, bool shutdown) return 0; } -int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer) +static int ipts_control_send_feedback_locked(struct ipts_context *ipts, u32 buffer) { int ret = 0; + int late = 0; + long left = 0; + u32 answered = 0; + unsigned long end = 0; struct ipts_feedback cmd = { 0 }; struct ipts_response rsp = { 0 }; - if (!ipts) - return -EFAULT; + lockdep_assert_held(&ipts->feedback_lock); + + /* + * No other FEEDBACK is in flight while the lock is held, so a FEEDBACK reply that is + * already queued answers one whose sender gave up waiting. Drop it, or this send would + * take it as its own. + */ + while (!ipts_cmd_recv_timeout(ipts, IPTS_CMD_FEEDBACK, &rsp, 0)) + late++; + + if (late) + dev_warn(ipts->dev, "FEEDBACK: dropped %d late replies\n", late); cmd.buffer = buffer; @@ -297,11 +313,29 @@ int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer) return ret; } - ret = ipts_cmd_recv(ipts, IPTS_CMD_FEEDBACK, &rsp); - if (ret) { - dev_err(ipts->dev, "FEEDBACK: recv failed: %d\n", ret); - return ret; - } + /* + * The first word of a FEEDBACK reply's payload echoes the buffer index of the command it + * answers. A reply for another buffer answers a sender that gave up after the drop above + * ran: drop it too and keep waiting for ours, within the usual time limit. Two hid2me + * replies look alike, but the lock allows one hid2me FEEDBACK at a time. + */ + end = jiffies + msecs_to_jiffies(IPTS_CMD_DEFAULT_TIMEOUT); + + do { + left = (long)(end - jiffies); + + ret = ipts_cmd_recv_timeout(ipts, IPTS_CMD_FEEDBACK, &rsp, + left > 0 ? jiffies_to_msecs(left) : 0); + if (ret) { + dev_err(ipts->dev, "FEEDBACK: recv failed: %d\n", ret); + return ret; + } + + answered = get_unaligned_le32(rsp.payload); + if (answered != buffer) + dev_warn(ipts->dev, "FEEDBACK: dropped a reply for buffer %u, waiting for %u\n", + answered, buffer); + } while (answered != buffer); /* * We don't know what feedback data looks like so we are sending zeros. @@ -318,9 +352,34 @@ int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer) return 0; } +/* + * The receiver loop and HID feature requests both send FEEDBACK, and a reply is matched to + * its sender by the command code only. With several FEEDBACKs in flight, a sender can take + * the reply to another one and return while its own command is still pending. The device + * then stays a reply behind, holding its newest reply until the next command, and never + * confirms a QUIESCE_IO sent in that state. One FEEDBACK at a time, dropping a reply that + * arrived after its sender gave up, and taking only the reply that names the sender's buffer + * keep each reply with its sender. The lock is held for a bounded time: the send and the + * reply wait are both bounded. + */ +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer) +{ + int ret = 0; + + if (!ipts) + return -EFAULT; + + mutex_lock(&ipts->feedback_lock); + ret = ipts_control_send_feedback_locked(ipts, buffer); + mutex_unlock(&ipts->feedback_lock); + + return ret; +} + int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd, enum ipts_feedback_data_type type, void *data, size_t size) { + int ret = 0; struct ipts_feedback_header *header = NULL; if (!ipts) @@ -329,6 +388,15 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c if (!ipts->resources.hid2me.address) return -EFAULT; + /* + * There is one hid2me buffer. Fill it under the lock, which is held until the device + * answered this FEEDBACK, so a second request cannot overwrite it before then. (That the + * device is done reading the buffer once it answered is assumed, not known.) + * GET_FEATURE waits for its report only after this returns, with the lock released, + * because the receiver loop needs the lock to give back the buffer the report is in. + */ + mutex_lock(&ipts->feedback_lock); + memset(ipts->resources.hid2me.address, 0, ipts->resources.hid2me.size); header = (struct ipts_feedback_header *)ipts->resources.hid2me.address; @@ -337,13 +405,19 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c header->size = size; header->buffer = IPTS_HID2ME_BUFFER; - if (size + sizeof(*header) > ipts->resources.hid2me.size) - return -EINVAL; + if (size + sizeof(*header) > ipts->resources.hid2me.size) { + ret = -EINVAL; + goto out; + } if (data && size > 0) memcpy(header->payload, data, size); - return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER); + ret = ipts_control_send_feedback_locked(ipts, IPTS_HID2ME_BUFFER); + +out: + mutex_unlock(&ipts->feedback_lock); + return ret; } /* diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index f508483bbd810..30f52f6b1fae9 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -131,6 +131,7 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id ipts->mode = IPTS_MODE_EVENT; mutex_init(&ipts->feature_lock); + mutex_init(&ipts->feedback_lock); init_completion(&ipts->feature_event); ipts_control_init(ipts); From f8e0bf57e436a8b2358e5627b37c9e7109da7b31 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 1 Nov 2026 21:07:21 -0700 Subject: [PATCH 11/19] 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 [loafecho: rebased onto #187: ipts_mei_flush() takes the message lock with the bounded ipts_mei_lock_timeout() and frees with kfree(), because #187 allocates messages with kzalloc()] Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 10 ++++++++++ drivers/hid/ipts/mei.c | 23 +++++++++++++++++++++++ drivers/hid/ipts/mei.h | 8 ++++++++ 3 files changed, 41 insertions(+) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index eb1491acb559e..3d5ab9f3ef1d6 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -437,6 +437,7 @@ static void ipts_control_leak_buffers(struct ipts_context *ipts) int ipts_control_start(struct ipts_context *ipts) { int ret = 0; + size_t stale = 0; struct ipts_device_info info = { 0 }; if (!ipts) @@ -444,6 +445,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 bcca4c76f0c93..48677d1fa84bd 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -261,6 +261,29 @@ void ipts_mei_free_messages(struct ipts_mei *mei) up_write(&mei->message_lock); } +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; + + if (!ipts_mei_lock_timeout(mei, IPTS_MEI_LOCK_TIMEOUT_MS)) + return 0; + + list_for_each_entry_safe(entry, tmp, &mei->messages, list) { + list_del(&entry->list); + kfree(entry); + count++; + } + + ipts_mei_unlock(mei); + + 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 269cba241692a..9aea2ed199426 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -85,4 +85,12 @@ int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev); */ void ipts_mei_free_messages(struct ipts_mei *mei); +/** + * 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 f6bc7912a773d55ef64b779377eccabf2c7b3fe5 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sat, 31 Oct 2026 21:35:30 -0600 Subject: [PATCH 12/19] 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 [loafecho: rebased onto #187, which already restarts the EDS1 device from a work item serialized by ctl_lock, cancelled on remove without waiting. The retries and the return to the previous mode now live in that work item: a failed start restores the old mode and queues a retry after 2 s, up to 5 attempts; a retry skips the stop. No second lock or work item: taking a lock in the HID callback would deadlock with the stop, which waits for HID callbacks.] Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 9 ++++++- drivers/hid/ipts/control.c | 48 +++++++++++++++++++++++++++++++++----- drivers/hid/ipts/eds1.c | 1 + 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index 25806ddc02233..3cc9dfd710da8 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -66,9 +66,16 @@ struct ipts_context { * mode switch) run from restart_work, never inside the callback. */ struct mutex ctl_lock; - struct work_struct restart_work; + struct delayed_work restart_work; bool removing; + /* + * When a restart fails to start the device again: the mode to go back to, and how many + * restarts were tried. Retries run from restart_work, too. + */ + enum ipts_mode restart_old_mode; + unsigned int restart_attempts; + /* * Freed when the last reference is dropped. Probe holds one (dropped by devres), and the * receiver thread, the HID device, every holder of message_lock and a queued restart diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 3d5ab9f3ef1d6..b52632a00a386 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -28,6 +28,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; @@ -671,13 +674,20 @@ int ipts_control_stop(struct ipts_context *ipts) static void ipts_control_restart_work(struct work_struct *work) { int ret = 0; - struct ipts_context *ipts = container_of(work, struct ipts_context, restart_work); + struct ipts_context *ipts = + container_of(to_delayed_work(work), struct ipts_context, restart_work); mutex_lock(&ipts->ctl_lock); if (READ_ONCE(ipts->removing)) goto out; + /* + * A retry: the start failed, so the device is stopped already. + */ + if (ipts->restart_attempts) + goto start; + ret = _ipts_control_stop(ipts); if (ret == IPTS_STOP_LEAKED) { /* @@ -705,9 +715,34 @@ static void ipts_control_restart_work(struct work_struct *work) if (READ_ONCE(ipts->removing)) goto out; +start: ret = ipts_control_start(ipts); - if (ret) - dev_err(ipts->dev, "Failed to restart IPTS: %d\n", ret); + if (!ret) { + ipts->restart_attempts = 0; + goto out; + } + + dev_err(ipts->dev, "Failed to restart IPTS: %d\n", ret); + + if (++ipts->restart_attempts >= IPTS_RESTART_MAX_ATTEMPTS) { + dev_err(ipts->dev, "Giving up restarting IPTS: %d\n", ret); + ipts->restart_attempts = 0; + goto out; + } + + if (READ_ONCE(ipts->removing)) + goto out; + + /* + * The device is stopped, and the HID device is inactive, so userspace cannot retry. + * Restart the device in the previous mode, which works without iptsd, and iptsd can + * switch modes again once it is restarted. The retry holds its own reference. + */ + ipts->mode = ipts->restart_old_mode; + + ipts_context_get(ipts); + if (!schedule_delayed_work(&ipts->restart_work, msecs_to_jiffies(IPTS_RESTART_DELAY_MS))) + ipts_context_put(ipts); out: mutex_unlock(&ipts->ctl_lock); @@ -721,7 +756,8 @@ static void ipts_control_restart_work(struct work_struct *work) void ipts_control_init(struct ipts_context *ipts) { mutex_init(&ipts->ctl_lock); - INIT_WORK(&ipts->restart_work, ipts_control_restart_work); + INIT_DELAYED_WORK(&ipts->restart_work, ipts_control_restart_work); + ipts->restart_attempts = 0; ipts->removing = false; } @@ -742,7 +778,7 @@ int ipts_control_restart(struct ipts_context *ipts) * already, it holds its own. */ ipts_context_get(ipts); - if (!schedule_work(&ipts->restart_work)) + if (!schedule_delayed_work(&ipts->restart_work, 0)) ipts_context_put(ipts); return 0; @@ -754,6 +790,6 @@ void ipts_control_cancel_restart(struct ipts_context *ipts) * Never waits: a restart that is running holds ctl_lock, which remove waits for with a * bound. A queued one that is cancelled drops its reference here. */ - if (cancel_work(&ipts->restart_work)) + if (cancel_delayed_work(&ipts->restart_work)) ipts_context_put(ipts); } diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index 7b9f54388a9f6..ecf6c75958680 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -57,6 +57,7 @@ static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) if (ipts->mode == mode) return 0; + ipts->restart_old_mode = ipts->mode; ipts->mode = mode; ret = ipts_control_restart(ipts); From 43a71831bcbda6ee4ee8a22625c8e3f809279c93 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Mon, 2 Nov 2026 09:39:20 -0700 Subject: [PATCH 13/19] 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 [loafecho: rebased onto #187: the reconnect runs before each retry in #187's restart work item, as it did before each background attempt; a failed reconnect counts as a failed attempt] Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 22 +++++++++++++++++++++- drivers/hid/ipts/mei.c | 23 +++++++++++++++++++++++ drivers/hid/ipts/mei.h | 8 ++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index b52632a00a386..e0ca43edcd928 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -685,8 +685,27 @@ static void ipts_control_restart_work(struct work_struct *work) /* * A retry: the start failed, so the device is stopped already. */ - if (ipts->restart_attempts) + if (ipts->restart_attempts) { + dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); + + /* + * 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 failed; + } + + msleep(1000); + + if (READ_ONCE(ipts->removing)) + goto out; + goto start; + } ret = _ipts_control_stop(ipts); if (ret == IPTS_STOP_LEAKED) { @@ -724,6 +743,7 @@ static void ipts_control_restart_work(struct work_struct *work) dev_err(ipts->dev, "Failed to restart IPTS: %d\n", ret); +failed: if (++ipts->restart_attempts >= IPTS_RESTART_MAX_ATTEMPTS) { dev_err(ipts->dev, "Giving up restarting IPTS: %d\n", ret); ipts->restart_attempts = 0; diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 48677d1fa84bd..55e11114d8178 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -284,6 +284,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 9aea2ed199426..87b8d91a6cefb 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -68,6 +68,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. From 9bc1f5aaf03b3978c5e95d838bf04e2c10251ff8 Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:13:39 +0000 Subject: [PATCH 14/19] hid: ipts: Read every response that is ready when one arrives The MEI bus calls the receive callback from a work item that it schedules when a response arrives, and the callback read one response. IPTS is a fixed-address ME client, so the ME can send the next response before that work has run. Scheduling it again then does nothing: the callback runs once, and the second response waits with the bus until another arrives. When stopping in poll mode, QUIESCE_IO gets two responses: first the one for the READY_FOR_DATA sent at start, then its own, usually about half a millisecond later. When both arrive together, the QUIESCE_IO response is never read and the stop is not confirmed. Read responses in the callback until none is ready within a millisecond (at least one jiffy), so each run of it now ends with that short wait. The wait is bounded, so a call whose response an earlier call already read returns too, and since responses only answer commands, the loop ends one wait after the last answer. On a Surface Pro 7 (EDS2), 300 traced suspends left 3 stops unconfirmed, each one this way. Over 300 more suspends per driver, alternated in blocks of 50, the stop was left unconfirmed 3 times without this change and never with it. In the runs with this change the two responses never arrived together, so they show the rate, not this change catching it. Fixes: 820c589680bb ("hid: Add support for Intel Precise Touch and Stylus") Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/mei.c | 63 ++++++++++++++++++++++++++++++------------ drivers/hid/ipts/mei.h | 5 ++++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 55e11114d8178..8b753406e4a16 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -68,49 +68,47 @@ static void ipts_mei_unlock(struct ipts_mei *mei) ipts_context_put(ipts); } -static void ipts_mei_incoming(struct mei_cl_device *cldev) +/* + * Read one response from the bus and queue it. Returns false if none was ready within + * IPTS_MEI_RECV_MORE_MS or reading it failed. + */ +static bool ipts_mei_receive(struct ipts_context *ipts, struct mei_cl_device *cldev) { ssize_t ret = 0; struct ipts_mei_message *entry = NULL; - struct ipts_context *ipts = NULL; - - if (!cldev) { - pr_err("MEI device is NULL!"); - return; - } - - ipts = mei_cldev_get_drvdata(cldev); - if (!ipts) { - pr_err("IPTS driver context is NULL!"); - return; - } entry = kzalloc(sizeof(*entry), GFP_KERNEL); if (!entry) - return; + return false; INIT_LIST_HEAD(&entry->list); do { - ret = mei_cldev_recv(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp)); + ret = mei_cldev_recv_timeout(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp), + IPTS_MEI_RECV_MORE_MS); } while (ret == -EINTR); + if (ret == -ETIME) { + kfree(entry); + return false; + } + if (ret < 0) { dev_err(ipts->dev, "Error while reading response: %ld\n", ret); kfree(entry); - return; + return false; } if (ret == 0) { dev_err(ipts->dev, "Received empty response\n"); kfree(entry); - return; + return false; } if (!ipts_mei_lock_timeout(&ipts->mei, IPTS_MEI_LOCK_TIMEOUT_MS)) { dev_err_ratelimited(ipts->dev, "Message lock is busy, dropping a response\n"); kfree(entry); - return; + return true; } list_add(&entry->list, &ipts->mei.messages); @@ -118,6 +116,35 @@ static void ipts_mei_incoming(struct mei_cl_device *cldev) ipts_mei_unlock(&ipts->mei); wake_up_all(&ipts->mei.message_queue); + return true; +} + +static void ipts_mei_incoming(struct mei_cl_device *cldev) +{ + struct ipts_context *ipts = NULL; + + if (!cldev) { + pr_err("MEI device is NULL!"); + return; + } + + ipts = mei_cldev_get_drvdata(cldev); + if (!ipts) { + pr_err("IPTS driver context is NULL!"); + return; + } + + /* + * The bus calls this from a work item that it schedules when a response arrives. IPTS + * is a fixed-address client, so the ME can send the next response before that work has + * run, and scheduling it again then does nothing: one call, two responses ready. If + * only one was read, the other would wait with the bus until a later response arrives. + * When stopping, that is the QUIESCE_IO response, right behind the one for the pending + * READY_FOR_DATA, and the stop is never confirmed. So read until none is ready. The + * wait for each is bounded, so a call whose response an earlier call took returns too. + */ + while (ipts_mei_receive(ipts, cldev)) + ; } static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code, diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index 87b8d91a6cefb..ff282bef556c6 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -44,6 +44,11 @@ struct ipts_mei { #define IPTS_MEI_LOCK_POLL_US 200 #define IPTS_MEI_SEND_TIMEOUT_MS 2000 +/* + * How long the receive callback waits for one more response before it returns. + */ +#define IPTS_MEI_RECV_MORE_MS 1 + /** * ipts_mei_recv() - Receive data from a MEI device. * @mei: The IPTS MEI device context. From cf18c1f55a33b45a2a2e546f8d23dbd2efcee2b6 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 15 Nov 2026 22:40:52 -0700 Subject: [PATCH 15/19] hid: ipts: Stop the device before every restart attempt A retry of a failed restart went straight to ipts_control_start(), because the failed start left the device stopped. But a start that fails after starting the receiver stops it again, and that stop can fail: the receiver thread may not stop, or the device may not confirm it. ipts_thread_start() does not check for a running thread, so the retry then starts a second receiver thread over the first one and clears its should_stop. A first restart attempt never starts the device after a failed stop for that reason. Run the stop on retries too. On a stopped device it does nothing, and otherwise it keeps the device from being started again, as for any other restart. Reconnect to the ME after the stop and before the one-second pause, which now applies to retries, too. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 43 +++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index e0ca43edcd928..45c4f1f39f1e8 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -683,30 +683,11 @@ static void ipts_control_restart_work(struct work_struct *work) goto out; /* - * A retry: the start failed, so the device is stopped already. + * Also on a retry, although the failed start left the device stopped: on a stopped device + * this does nothing, but if the start failed after it started the receiver and that did + * not stop, or the device did not confirm its stop, it keeps the device from being + * started again, as for any other restart. */ - if (ipts->restart_attempts) { - dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); - - /* - * 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 failed; - } - - msleep(1000); - - if (READ_ONCE(ipts->removing)) - goto out; - - goto start; - } - ret = _ipts_control_stop(ipts); if (ret == IPTS_STOP_LEAKED) { /* @@ -723,6 +704,21 @@ static void ipts_control_restart_work(struct work_struct *work) goto out; } + if (ipts->restart_attempts) { + dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); + + /* + * 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 failed; + } + } + /* * Wait a second to give the sensor time to fully shut down. */ @@ -734,7 +730,6 @@ static void ipts_control_restart_work(struct work_struct *work) if (READ_ONCE(ipts->removing)) goto out; -start: ret = ipts_control_start(ipts); if (!ret) { ipts->restart_attempts = 0; From 9bdb25fbd93ae691d78b77e79504bb6fe74cc0ef Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 15 Nov 2026 22:41:21 -0700 Subject: [PATCH 16/19] hid: ipts: Retry a failed restart in the mode that was asked for When starting the device failed during an EDS1 mode switch, the retries went back to the previous mode. That made sense while the mode switch restarted the device inside the HID request: the request failed, and iptsd exited. Since the restart runs from a work item, the request has already succeeded when the restart runs, and iptsd keeps running in the new mode. Going back left the device in event mode under an iptsd that expects multitouch data, until iptsd was restarted. Retry in the mode that was asked for, and drop restart_old_mode. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 5 ++--- drivers/hid/ipts/control.c | 7 +++---- drivers/hid/ipts/eds1.c | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index 3cc9dfd710da8..4acfd5c5fd348 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -70,10 +70,9 @@ struct ipts_context { bool removing; /* - * When a restart fails to start the device again: the mode to go back to, and how many - * restarts were tried. Retries run from restart_work, too. + * When a restart fails to start the device again: how many restarts were tried. + * Retries run from restart_work, too. */ - enum ipts_mode restart_old_mode; unsigned int restart_attempts; /* diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 45c4f1f39f1e8..7d9ac7e275181 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -750,11 +750,10 @@ static void ipts_control_restart_work(struct work_struct *work) /* * The device is stopped, and the HID device is inactive, so userspace cannot retry. - * Restart the device in the previous mode, which works without iptsd, and iptsd can - * switch modes again once it is restarted. The retry holds its own reference. + * Retry in the mode that was asked for: the HID request that asked for it returned + * before the restart ran, so its sender expects that mode. The retry holds its own + * reference. */ - ipts->mode = ipts->restart_old_mode; - ipts_context_get(ipts); if (!schedule_delayed_work(&ipts->restart_work, msecs_to_jiffies(IPTS_RESTART_DELAY_MS))) ipts_context_put(ipts); diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index ecf6c75958680..7b9f54388a9f6 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -57,7 +57,6 @@ static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) if (ipts->mode == mode) return 0; - ipts->restart_old_mode = ipts->mode; ipts->mode = mode; ret = ipts_control_restart(ipts); From 72edf1f4cf1767927eafdec6e67df498990b7c6c Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 15 Nov 2026 22:44:33 -0700 Subject: [PATCH 17/19] hid: ipts: Accept EDS1 mode switches while the device restarts Since "hid: ipts: Run the EDS1 mode-switch restart from a work item", a mode switch returns before the restart has run, and the HID device is inactive until the restart is done. Restarting iptsd now leaves it dead on EDS1 devices: the old instance switches back to event mode as it exits, systemd starts the new one at once, and its switch to multitouch mode arrives during that restart. ipts_hid_raw_request() refuses it with -ENODEV, the new iptsd exits, and touch input stays in event mode. On a Surface Book 1, `systemctl restart iptsd` left iptsd running 4 out of 4 times with the stock driver and 0 out of 4 with #187. The EDS1 mode report only records the requested mode and queues a restart. It touches neither the device nor its buffers, so let it through while the HID device is inactive. It is still counted as a HID user, so a stop waits for it, and it is refused once remove started, or after a stop for a restart failed or leaked the buffers, since the device must not be started again then. The switch now records the mode in requested_mode, which restart_work applies under ctl_lock, so a switch during a restart does not change the mode under a start that is running. GET_REPORT returns the requested mode. A switch to the mode that was already asked for still restarts a device that is not running, so that after the restart retries gave up, starting iptsd again restarts the device. A restart that finds the requested mode applied and the device running does nothing. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/context.h | 13 +++++++++++++ drivers/hid/ipts/control.c | 24 ++++++++++++++++++++++-- drivers/hid/ipts/eds1.c | 11 ++++++++--- drivers/hid/ipts/hid.c | 29 ++++++++++++++++++++++++++++- drivers/hid/ipts/main.c | 1 + 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index 4acfd5c5fd348..4157361aa9574 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -75,6 +75,19 @@ struct ipts_context { */ unsigned int restart_attempts; + /* + * EDS1: the mode that the last mode switch asked for. restart_work applies it to mode + * under ctl_lock, so a switch that arrives during a restart does not change the mode + * under a start that is running. + */ + enum ipts_mode requested_mode; + + /* + * Set when a stop for a restart failed or leaked the buffers: the device must not be + * started again, and mode switches are refused, until the next probe. + */ + bool restart_blocked; + /* * Freed when the last reference is dropped. Probe holds one (dropped by devres), and the * receiver thread, the HID device, every holder of message_lock and a queued restart diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 7d9ac7e275181..56d267e95a1ac 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -682,6 +682,16 @@ static void ipts_control_restart_work(struct work_struct *work) if (READ_ONCE(ipts->removing)) goto out; + /* + * A mode switch that arrived while the previous restart ran queues another one. If that + * restart already applied the mode and the device runs, there is nothing to do. + */ + if (!ipts->restart_attempts && READ_ONCE(ipts->hid_active) && + ipts->mode == READ_ONCE(ipts->requested_mode)) + goto out; + + ipts->mode = READ_ONCE(ipts->requested_mode); + /* * Also on a retry, although the failed start left the device stopped: on a stopped device * this does nothing, but if the start failed after it started the receiver and that did @@ -696,12 +706,12 @@ static void ipts_control_restart_work(struct work_struct *work) * until the next boot. */ dev_err(ipts->dev, "Device may not have stopped, not restarting IPTS\n"); - goto out; + goto blocked; } if (ret) { dev_err(ipts->dev, "Failed to stop IPTS for a restart: %d\n", ret); - goto out; + goto blocked; } if (ipts->restart_attempts) { @@ -758,6 +768,15 @@ static void ipts_control_restart_work(struct work_struct *work) if (!schedule_delayed_work(&ipts->restart_work, msecs_to_jiffies(IPTS_RESTART_DELAY_MS))) ipts_context_put(ipts); + goto out; + +blocked: + /* + * Refuse further mode switches, which would only queue another restart. + */ + WRITE_ONCE(ipts->restart_blocked, true); + ipts->restart_attempts = 0; + out: mutex_unlock(&ipts->ctl_lock); @@ -772,6 +791,7 @@ void ipts_control_init(struct ipts_context *ipts) mutex_init(&ipts->ctl_lock); INIT_DELAYED_WORK(&ipts->restart_work, ipts_control_restart_work); ipts->restart_attempts = 0; + ipts->restart_blocked = false; ipts->removing = false; } diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index 7b9f54388a9f6..dacec6b3d367a 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -54,10 +54,15 @@ static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) if (!ipts) return -EFAULT; - if (ipts->mode == mode) + /* + * Only recorded here: restart_work applies it, also when this arrives during a restart. + * The same mode again restarts a device that is not running, for example after the + * restart retries gave up; restart_work does nothing if it runs by then. + */ + if (READ_ONCE(ipts->requested_mode) == mode && READ_ONCE(ipts->hid_active)) return 0; - ipts->mode = mode; + WRITE_ONCE(ipts->requested_mode, mode); ret = ipts_control_restart(ipts); if (ret) @@ -93,7 +98,7 @@ int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 if (request_type == HID_REQ_GET_REPORT) { memset(buffer, 0, size); buffer[0] = report_id; - buffer[1] = ipts->mode; + buffer[1] = READ_ONCE(ipts->requested_mode); } else if (request_type == HID_REQ_SET_REPORT) { return ipts_eds1_switch_mode(ipts, buffer[1]); } else { diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c index dccad5ccee2f0..8a0e2a830946b 100644 --- a/drivers/hid/ipts/hid.c +++ b/drivers/hid/ipts/hid.c @@ -130,6 +130,32 @@ static int ipts_hid_parse(struct hid_device *hid) return 0; } +/* + * On EDS1, a mode switch restarts the device from a work item, and the HID device is inactive + * until the restart is done. When iptsd is restarted, the old instance switches back to event + * mode as it exits, and the switch of the new one arrives during that restart. Refusing it + * would make the new iptsd exit, with touch input left in event mode. The EDS1 mode report + * only records the requested mode and queues a restart, and touches neither the device nor + * its buffers, so it is let through while the device restarts. It is still counted, so that a + * stop waits for it, and refused once remove started or after a failed stop. + */ +static bool ipts_hid_enter_restarting(struct ipts_context *ipts) +{ + atomic_inc(&ipts->hid_users); + /* + * Order the count above before reading removing, so a remove that set it cannot miss + * this callback: its stop orders that write before reading the count. + */ + smp_mb__after_atomic(); + + if (READ_ONCE(ipts->removing) || READ_ONCE(ipts->restart_blocked)) { + ipts_hid_exit(ipts); + return false; + } + + return true; +} + static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, __u8 *buffer, size_t size, unsigned char report_type, int request_type) { @@ -144,7 +170,8 @@ static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, if (!ipts) return -EFAULT; - if (!ipts_hid_enter(ipts)) + if (!ipts_hid_enter(ipts) && + (ipts->info.intf_eds != 1 || !ipts_hid_enter_restarting(ipts))) return -ENODEV; if (ipts->info.intf_eds == 1) { diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index 30f52f6b1fae9..470dd112ef03b 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -129,6 +129,7 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id ipts->dev = &cldev->dev; ipts->mode = IPTS_MODE_EVENT; + ipts->requested_mode = IPTS_MODE_EVENT; mutex_init(&ipts->feature_lock); mutex_init(&ipts->feedback_lock); From 0c6f05dd536eee53349111fbdb4baccea34f35b1 Mon Sep 17 00:00:00 2001 From: Jayden Milne Date: Sun, 15 Nov 2026 22:45:05 -0700 Subject: [PATCH 18/19] hid: ipts: Count the ME reconnect in how long remove waits for a restart ipts_remove() waits for a running restart for as long as a restart can hold ctl_lock: a start, a stop and one second. A retry of a failed restart now also reconnects to the ME before it checks for remove again, and the MEI core waits for the disconnect and the connect up to its client connect timeout each (30 s for slow firmware). Without that in the bound, a slow reconnect makes remove give up on the restart and skip stopping the device. Add the reconnect to the bound. Signed-off-by: Jayden Milne Patchset: ipts Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/main.c | 9 +++++---- drivers/hid/ipts/mei.h | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index 470dd112ef03b..8e7e0b5682c75 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -163,12 +163,13 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id /* * How long ipts_remove() waits for a running restart to release ctl_lock: the longest a * restart can still hold it once removing is set. Before its last check of removing, that is - * the rest of a stop and the one-second pause; after it, a start, and a stop if the start - * fails. The sum of a start and a stop covers either. One second more covers scheduling. A - * restart that holds the lock for longer is stuck. + * the rest of a stop, a reconnect to the ME on a retry, and the one-second pause; after it, a + * start, and a stop if the start fails. The sum of a start, a stop and a reconnect covers + * either. One second more covers scheduling. A restart that holds the lock for longer is stuck. */ #define IPTS_REMOVE_LOCK_TIMEOUT_MS \ - (IPTS_CONTROL_START_TIMEOUT_MS + IPTS_CONTROL_STOP_TIMEOUT_MS + 1000) + (IPTS_CONTROL_START_TIMEOUT_MS + IPTS_CONTROL_STOP_TIMEOUT_MS + \ + IPTS_MEI_RECONNECT_TIMEOUT_MS + 1000) #define IPTS_REMOVE_LOCK_POLL_MS 10 static bool ipts_remove_lock(struct ipts_context *ipts) diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index ff282bef556c6..9ebf15dc50a42 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -73,6 +73,12 @@ 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); +/* + * The longest ipts_mei_reconnect() takes: the MEI core waits for the disconnect and for the + * connect up to its client connect timeout each, which is 30 seconds for slow firmware. + */ +#define IPTS_MEI_RECONNECT_TIMEOUT_MS (2 * 30000) + /** * ipts_mei_reconnect() - Disconnects from the ME client and connects again. * @mei: The MEI device context. From 61ff82169a17752f0b5de8b8997e8f1bbfeb903c Mon Sep 17 00:00:00 2001 From: Tony Garcia <180770769+loafecho@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:07:11 +0000 Subject: [PATCH 19/19] hid: ipts: Keep a blocked device stopped, and check the EDS1 mode A mode switch that arrives while a restart's stop runs queues another restart. If that stop then leaks the buffers, it sets restart_blocked, but the queued run never reads it: its stop finds nothing to stop and it starts the device. Later switches through the normal HID path never check the flag either. Return early from restart_work and refuse EDS1 mode switches once the flag is set. A failed start stops the device through ipts_control_stop(), which turned a leak into 0, so the restart retry started a device that never confirmed its stop. Block restarts on any failed or leaking stop there, as restart_work does for its own stop. The EDS1 mode comes from userspace and every retry now applies it. Refuse values other than event and poll mode. Found by review with userspace harnesses built from the driver code; not yet run on an EDS1 device. Patchset: ipts Assisted-by: LLM Signed-off-by: Tony Garcia <180770769+loafecho@users.noreply.github.com> --- drivers/hid/ipts/control.c | 21 +++++++++++++++++++-- drivers/hid/ipts/eds1.c | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 56d267e95a1ac..cde37cde3603c 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -655,6 +655,14 @@ int ipts_control_stop(struct ipts_context *ipts) * It is kept only while a HID callback is stuck. */ ret = _ipts_control_stop(ipts); + + /* + * As after a failed stop for a restart: a failed start stops the device through here, + * and the restart retry must not start a device that did not stop or confirm its stop. + */ + if (ret) + WRITE_ONCE(ipts->restart_blocked, true); + if (ret == -EBUSY) return ret; @@ -682,6 +690,14 @@ static void ipts_control_restart_work(struct work_struct *work) if (READ_ONCE(ipts->removing)) goto out; + /* + * A mode switch can queue this again while a stop that leaked the buffers runs, before + * that stop sets restart_blocked. The device must not be started again until the next + * probe, so a run queued that way does nothing. + */ + if (READ_ONCE(ipts->restart_blocked)) + goto out; + /* * A mode switch that arrived while the previous restart ran queues another one. If that * restart already applied the mode and the device runs, there is nothing to do. @@ -695,8 +711,9 @@ static void ipts_control_restart_work(struct work_struct *work) /* * Also on a retry, although the failed start left the device stopped: on a stopped device * this does nothing, but if the start failed after it started the receiver and that did - * not stop, or the device did not confirm its stop, it keeps the device from being - * started again, as for any other restart. + * not stop, it keeps the device from being started again, as for any other restart. If + * the device did not confirm the failed start's stop, that stop set restart_blocked and + * the retry ended above. */ ret = _ipts_control_stop(ipts); if (ret == IPTS_STOP_LEAKED) { diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index dacec6b3d367a..f677e00ab334a 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -54,6 +54,20 @@ static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) if (!ipts) return -EFAULT; + /* + * The value comes from userspace, and every restart retry applies it. + */ + if (mode != IPTS_MODE_EVENT && mode != IPTS_MODE_POLL) + return -EINVAL; + + /* + * After a failed stop the device must not be started again until the next probe. The + * device can still be running then, if a restart queued before the flag was set + * started it. + */ + if (READ_ONCE(ipts->restart_blocked)) + return -ENODEV; + /* * Only recorded here: restart_work applies it, also when this arrives during a restart. * The same mode again restarts a device that is not running, for example after the