public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] comedi: fix device lifecycle handling in comedi_dev_kref_release()
@ 2026-01-30 22:35 Salah Triki
  2026-02-02 12:09 ` Ian Abbott
  0 siblings, 1 reply; 4+ messages in thread
From: Salah Triki @ 2026-01-30 22:35 UTC (permalink / raw)
  To: Ian Abbott, H Hartley Sweeten; +Cc: linux-kernel, Salah Triki

`comedi_dev_kref_release()` calls `put_device()` on `dev->class_dev` and
then immediately frees the `comedi_device` structure with `kfree()`.
If the device reference count reaches zero, the device model release
callback may be invoked, potentially accessing or freeing parts of
the structure. Calling `kfree()` directly after `put_device()` can
cause a use-after-free or double-free.

After `device_initialize()` and once the struct device is in the device
model, all cleanup must go through `put_device()` and the release
callback. The kref release should not free the structure directly,
ensuring that `comedi_device` lifetime is properly managed through
device reference counting and preventing potential memory corruption.

Fixes: 5b13ed94a7d24 ("staging: comedi: add a kref to comedi device")

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/comedi/comedi_fops.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/comedi/comedi_fops.c b/drivers/comedi/comedi_fops.c
index 2c3eb9e89571..24bb06fe6b45 100644
--- a/drivers/comedi/comedi_fops.c
+++ b/drivers/comedi/comedi_fops.c
@@ -118,7 +118,6 @@ static void comedi_dev_kref_release(struct kref *kref)
 
 	mutex_destroy(&dev->mutex);
 	put_device(dev->class_dev);
-	kfree(dev);
 }
 
 /**
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-02-04 20:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-30 22:35 [PATCH] comedi: fix device lifecycle handling in comedi_dev_kref_release() Salah Triki
2026-02-02 12:09 ` Ian Abbott
2026-02-02 15:12   ` Ian Abbott
2026-02-04 20:46     ` Salah Triki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox