Re: [PATCH] comedi: defer freeing async areas that still have pollers

From: Ian Abbott

Date: Mon Nov 16 2026 - 05:15:06 EST


On 2026-11-16 08:53, YuanZhe Liu wrote:
comedi_device_detach_cleanup() frees the async area of every subdevice.
The async area contains the wait queue head that comedi_poll() queues
pollers on, and a poller is not removed while comedi_poll() runs:
comedi_poll() only holds dev->attach_lock while adding the wait queue
entry, and the entry is removed later by poll_freewait() when the poll
system call returns.

Detaching through the COMEDI_DEVCONFIG ioctl is guarded by
is_device_busy(), which refuses to detach while the wait queue has
sleepers. Removing the hardware is not: comedi_release_hardware_device()
calls comedi_free_board_dev(), which detaches unconditionally. Unplugging
a USB COMEDI device while a task is polling it therefore frees the async
area from under that task, and the task hits a use-after-free when
poll_freewait() locks the freed wait queue head:

BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave
Write of size 4 at addr ffff888004508498
Call Trace:
_raw_spin_lock_irqsave
remove_wait_queue
free_poll_entry
poll_freewait
do_sys_poll

Every task that can have an entry queued on async->wait_head holds a
reference to the COMEDI device file, and comedi_open() holds a reference
to the COMEDI device for as long as the file is open. The device
therefore cannot be released until the poll system call is done with the
wait queue, so move async areas that still have waiters to a list and
free them from comedi_dev_kref_release() instead of freeing them at
detach time.

Fixes: 2f3fdcd7ce93 ("staging: comedi: add rw_semaphore to protect against device detachment")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
drivers/comedi/comedi_fops.c | 8 ++++++++
drivers/comedi/drivers.c | 14 +++++++++++++-
include/linux/comedi/comedidev.h | 8 ++++++++
3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/drivers/comedi/comedi_fops.c b/drivers/comedi/comedi_fops.c
index 9a65dc5d62a4..932e35d7f68c 100644
--- a/drivers/comedi/comedi_fops.c
+++ b/drivers/comedi/comedi_fops.c
@@ -106,6 +106,7 @@ static struct cdev comedi_cdev;
static void comedi_device_init(struct comedi_device *dev)
{
kref_init(&dev->refcount);
+ INIT_LIST_HEAD(&dev->async_free_list);
spin_lock_init(&dev->spinlock);
mutex_init(&dev->mutex);
init_rwsem(&dev->attach_lock);
@@ -116,6 +117,13 @@ static void comedi_dev_kref_release(struct kref *kref)
{
struct comedi_device *dev =
container_of(kref, struct comedi_device, refcount);
+ struct comedi_async *async, *tmp;
+
+ list_for_each_entry_safe(async, tmp, &dev->async_free_list,
+ free_list) {
+ list_del(&async->free_list);
+ kfree(async);
+ }
mutex_destroy(&dev->mutex);
put_device(dev->class_dev);
diff --git a/drivers/comedi/drivers.c b/drivers/comedi/drivers.c
index 3f75485338a1..f91535fccd2b 100644
--- a/drivers/comedi/drivers.c
+++ b/drivers/comedi/drivers.c
@@ -168,7 +168,19 @@ static void comedi_device_detach_cleanup(struct comedi_device *dev)
comedi_free_subdevice_minor(s);
if (s->async) {
comedi_buf_alloc(dev, s, 0);
- kfree(s->async);
+ /*
+ * A task that is polling the device file may still
+ * have an entry queued on the wait queue inside the
+ * async area. Such a task holds a reference to the
+ * device file, which keeps the COMEDI device itself
+ * alive until the poll is done with it, so defer
+ * the release until the device is released.
+ */
+ if (wq_has_sleeper(&s->async->wait_head))
+ list_add_tail(&s->async->free_list,
+ &dev->async_free_list);
+ else
+ kfree(s->async);
}
kfree(s->readback);
}
diff --git a/include/linux/comedi/comedidev.h b/include/linux/comedi/comedidev.h
index 577a08f37aee..54e324c0614f 100644
--- a/include/linux/comedi/comedidev.h
+++ b/include/linux/comedi/comedidev.h
@@ -15,6 +15,7 @@
#include <linux/spinlock_types.h>
#include <linux/rwsem.h>
#include <linux/kref.h>
+#include <linux/list.h>
#include <linux/completion.h>
#include <linux/comedi.h>
@@ -365,6 +366,11 @@ struct comedi_async {
unsigned int cb_mask;
int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s,
unsigned int x);
+ /*
+ * Link in &struct comedi_device.async_free_list when the async area
+ * cannot be freed at detach time because pollers may still use it.
+ */
+ struct list_head free_list;
};
/**
@@ -558,6 +564,8 @@ struct comedi_device {
int n_subdevices;
struct comedi_subdevice *subdevices;
+ /* async areas waiting for the last user of the device */
+ struct list_head async_free_list;
/* dumb */
void __iomem *mmio;

The patch looks interesting, but I have already sent a patch that fixes the problem in a different way, by waiting for the queues to clear before freeing the storage.

https://lore.kernel.org/lkml/20261112173314.118594-1-abbotti@xxxxxxxxx/

--
-=( Ian Abbott <abbotti@xxxxxxxxx> || MEV Ltd. is a company )=-
-=( registered in England & Wales. Regd. number: 02862268. )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-