iio: core: fix error path in iio_device_alloc()
cover iio: core: fix error path in iio_device_alloc()
The series is induced by the similar fixes in viio_trigger_alloc(). Basically two things happen here: 1) add missed mutex_destroy() calls, and 2) refactor to make sure we clean resources with put_device() when appropriate.
Andy Shevchenko (3):
iio: core: add missing mutex_destroy in iio_dev_release()
iio: core: Clean up device correctly on iio_device_alloc() failure
iio: core: Replace lockdep_set_class() + mutex_init() by combined call
drivers/iio/industrialio-core.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/3 iio: core: add missing mutex_destroy in iio_dev_release()
Add missing mutex_destroy() call in iio_dev_release() to properly clean up the mutex initialized in iio_device_alloc(). Ensure proper resource cleanup and follows kernel practices.
Found by code review.
While at it, create a lockdep key before mutex initialisation. This will help with converting it to the better API in the future.
--- drivers/iio/industrialio-core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/industrialio-core.c +7 -2
@@ -1654,6 +1654,9 @@ static void iio_dev_release(struct device *device)iio_device_detach_buffers (indio_dev); +mutex_destroy (&iio_dev_opaque->info_exist_lock); +mutex_destroy (&iio_dev_opaque->mlock); +lockdep_unregister_key (&iio_dev_opaque->mlock_key);ida_free (&iio_ida, iio_dev_opaque->id);
@@ -1698,8 +1701,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) indio_dev->dev.type = &iio_device_type; indio_dev->dev.bus = &iio_bus_type;device_initialize (&indio_dev->dev); -mutex_init (&iio_dev_opaque->mlock); -mutex_init (&iio_dev_opaque->info_exist_lock); +INIT_LIST_HEAD (&iio_dev_opaque->channel_attr_list); iio_dev_opaque->id =ida_alloc (&iio_ida,GFP_KERNEL );
@@ -1722,6 +1724,9 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)lockdep_register_key (&iio_dev_opaque->mlock_key);lockdep_set_class (&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key); +mutex_init (&iio_dev_opaque->mlock); +mutex_init (&iio_dev_opaque->info_exist_lock); +return indio_dev; }EXPORT_SYMBOL (iio_device_alloc);
Signature · 1 line
2.50.1
PATCH 2/3 iio: core: Clean up device correctly on iio_device_alloc() failure
In reply to Andy ShevchenkoOnce we called device_initialize() we have to call put_device() on it. Refactor the code to make it in the right order.
--- drivers/iio/industrialio-core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/industrialio-core.c +5 -5
@@ -1697,11 +1697,6 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)ACCESS_PRIVATE (indio_dev, priv) = (char *)iio_dev_opaque +ALIGN (sizeof (*iio_dev_opaque),IIO_DMA_MINALIGN ); - indio_dev->dev.parent = parent; - indio_dev->dev.type = &iio_device_type; - indio_dev->dev.bus = &iio_bus_type; -device_initialize (&indio_dev->dev); -INIT_LIST_HEAD (&iio_dev_opaque->channel_attr_list); iio_dev_opaque->id =ida_alloc (&iio_ida,GFP_KERNEL );
@@ -1727,6 +1722,11 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)mutex_init (&iio_dev_opaque->mlock);mutex_init (&iio_dev_opaque->info_exist_lock); + indio_dev->dev.parent = parent; + indio_dev->dev.type = &iio_device_type; + indio_dev->dev.bus = &iio_bus_type; +device_initialize (&indio_dev->dev); +return indio_dev; }EXPORT_SYMBOL (iio_device_alloc);
Signature · 1 line
2.50.1
PATCH 3/3 iio: core: Replace lockdep_set_class() + mutex_init() by combined call
In reply to Andy ShevchenkoReplace lockdep_set_class() + mutex_init() by combined call mutex_init_with_key().
--- drivers/iio/industrialio-core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/industrialio-core.c +1 -2
@@ -1717,9 +1717,8 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)INIT_LIST_HEAD (&iio_dev_opaque->ioctl_handlers);lockdep_register_key (&iio_dev_opaque->mlock_key); -lockdep_set_class (&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key); -mutex_init (&iio_dev_opaque->mlock); +mutex_init_with_key (&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key);mutex_init (&iio_dev_opaque->info_exist_lock); indio_dev->dev.parent = parent;
Signature · 1 line
2.50.1
Quoted 5 lines · Andy Shevchenko
The series is induced by the similar fixes in
viio_trigger_alloc(). Basically two things happen here: 1) add missedmutex_destroy()calls, and 2) refactor to make sure we clean resources withput_device()when appropriate.
Quoted 7 lines
Andy Shevchenko (3):
iio: core: add missingmutex_destroyiniio_dev_release()
iio: core: Clean up device correctly oniio_device_alloc()failure
iio: core: Replacelockdep_set_class()+mutex_init()by combined calldrivers/iio/industrialio-core.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
LGTM,
Jonathan Cameron
maintainerQuoted 11 lines · On Wed, 12 Nov 2025 18:00:42 +0000 Nuno Sá <[email protected]> wrote:
Quoted 10 lines · Andy Shevchenko
The series is induced by the similar fixes in
viio_trigger_alloc(). Basically two things happen here: 1) add missedmutex_destroy()calls, and 2) refactor to make sure we clean resources withput_device()when appropriate.Andy Shevchenko (3):
iio: core: add missingmutex_destroyiniio_dev_release()
iio: core: Clean up device correctly oniio_device_alloc()failure
iio: core: Replacelockdep_set_class()+mutex_init()by combined call
Quoted 7 lines
Quoted 2 lines
drivers/iio/industrialio-core.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)LGTM,
Reviewed-by Nuno Sá <[email protected]>
I'm not going to rush these in given where we are in the cycle and the fact only some are fixes (and really no one cares that much about mutex_destroy() as it only does anything at all in a particular debug setup.)
So applied to the togreg branch of iio.git and pushed out as testing for now.
Thanks,
Jonathan