From e001361edfc20cc4d45c35d72ddbf53d9d64ccf5 Mon Sep 17 00:00:00 2001 From: Josh Spaulding Date: Wed, 2 Sep 2026 21:30:00 -0500 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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)