From 62149c2a160abe17fa1fa4cb4af65a12171f3163 Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Fri, 28 Sep 2012 13:06:54 +0100 Subject: [PATCH] Fix memory leak for saved channel list When `do_cmd_ioctl()` allocates memory for the kernel copy of a channel list, it frees any previously allocated channel list in `async->cmd.chanlist` and replaces it with the new one. However, if the device is ever removed (or "detached") the cleanup code in `cleanup_device()` in "drivers.c" does not free this memory so it is lost. A sensible place to free the kernel copy of the channel list is in `do_become_nonbusy()` as at that point the comedi asynchronous command associated with the channel list is no longer valid. Free the channel list in `do_become_nonbusy()` instead of `do_cmd_ioctl()` and clear the pointer to prevent it being freed more than once. Signed-off-by: Ian Abbott --- comedi/comedi_fops.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comedi/comedi_fops.c b/comedi/comedi_fops.c index d4e209db..5f64100c 100644 --- a/comedi/comedi_fops.c +++ b/comedi/comedi_fops.c @@ -1129,8 +1129,6 @@ static int do_cmd_ioctl(comedi_device * dev, comedi_cmd __user *arg, void *file) goto cleanup; } - if (async->cmd.chanlist) - kfree(async->cmd.chanlist); async->cmd = user_cmd; async->cmd.data = NULL; /* load channel/gain list */ @@ -1873,6 +1871,8 @@ void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s) if (async) { comedi_reset_async_buf(async); async->inttrig = NULL; + kfree(async->cmd.chanlist); + async->cmd.chanlist = NULL; } else { printk("BUG: (?) do_become_nonbusy called with async=0\n"); } -- 2.26.2