From: Salah Triki <salah.triki@gmail.com>
To: Ian Abbott <abbotti@mev.co.uk>,
H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: linux-kernel@vger.kernel.org, Salah Triki <salah.triki@gmail.com>
Subject: [PATCH] comedi: fix device lifecycle handling in comedi_dev_kref_release()
Date: Fri, 30 Jan 2026 23:35:17 +0100 [thread overview]
Message-ID: <20260130223517.223996-1-salah.triki@gmail.com> (raw)
`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
next reply other threads:[~2026-01-30 22:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-30 22:35 Salah Triki [this message]
2026-02-02 12:09 ` [PATCH] comedi: fix device lifecycle handling in comedi_dev_kref_release() Ian Abbott
2026-02-02 15:12 ` Ian Abbott
2026-02-04 20:46 ` Salah Triki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260130223517.223996-1-salah.triki@gmail.com \
--to=salah.triki@gmail.com \
--cc=abbotti@mev.co.uk \
--cc=hsweeten@visionengravers.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox