Skip to contentSkip to conversation

iio: dummy: free the sw device on configfs release

PATCH In review Lin Tzu Yang 2 participants 2 messages Dec 2 – Dec 3 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: dummy: free the sw device on configfs release

iio_dummy_probe() allocates the struct iio_sw_device that embeds the configfs group of a dummy device, but nothing frees it:
iio_dummy_remove() frees the iio_dev only, and iio_dummy_type has no ->release(). Every mkdir/rmdir of a directory under /sys/kernel/config/iio/devices/dummy/ leaks it.

Free it from the ->release() callback of the config_item_type, which configfs calls when the last reference to the item is dropped, as commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on configfs release") does for the software triggers.

Fixes 3d85fb6f8104 ("iio: dummy: Convert IIO dummy to configfs")

---
Tested in a QEMU/KVM arm64 guest with kmemleak and KASAN, on configfs-next 2e0fad9d2eae, where this file is the same as on fixes-togreg. After 50 mkdir/rmdir pairs of a dummy device, kmemleak reported 29 unreferenced 192-byte objects allocated in iio_dummy_probe() before the patch, and none after. Removing a dummy device while its character device is open still works: reads from the open file fail afterwards, with no KASAN reports.

The patch is against fixes-togreg, on top of 8fb20cdf9c86.

This patch was written with the help of an AI coding assistant (Claude). I ran the tests above and reviewed the change.

 drivers/iio/dummy/iio_simple_dummy.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/iio/dummy/iio_simple_dummy.c +10 -0
index 28ae1e7b59..2f3ea3b419 100644
@@ -23,7 +23,17 @@
 #include <linux/iio/sw_device.h>
 #include "iio_simple_dummy.h"
 
+static void iio_dummy_release(struct config_item *item)
+{
+	kfree(to_iio_sw_device(item));
+}
+
+static const struct configfs_item_operations iio_dummy_item_ops = {
+	.release = iio_dummy_release,
+};
+
 static const struct config_item_type iio_dummy_type = {
+	.ct_item_ops = &iio_dummy_item_ops,
 	.ct_owner = THIS_MODULE,
 };
 
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Lin Tzu Yang

iio_dummy_probe() allocates the struct iio_sw_device that embeds the configfs group of a dummy device, but nothing frees it:
iio_dummy_remove() frees the iio_dev only, and iio_dummy_type has no ->release(). Every mkdir/rmdir of a directory under /sys/kernel/config/iio/devices/dummy/ leaks it.

Free it from the ->release() callback of the config_item_type, which configfs calls when the last reference to the item is dropped, as commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on configfs release") does for the software triggers.

Sounds correct to me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko