Skip to contentSkip to conversation

iio: core: fix error path in iio_device_alloc()

PATCH In review Andy Shevchenko 3 participants 6 messages Nov 12, 2025 – Nov 15, 2025 IIO

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes 847ec80bbaa7 ("Staging: IIO: core support for device registration and management")
Fixes ac917a81117c ("staging:iio:core set the iio_dev.info pointer to null on unregister under lock.")
---
 drivers/iio/industrialio-core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/industrialio-core.c +7 -2
index 88c3d585a1bd..93d6e5b101cf 100644
@@ -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
#2 lore raw

PATCH 2/3 iio: core: Clean up device correctly on iio_device_alloc() failure

In reply to Andy Shevchenko

Once we called device_initialize() we have to call put_device() on it. Refactor the code to make it in the right order.

Fixes fe6f45f6ba22 ("iio: core: check return value when calling dev_set_name()")
Fixes 847ec80bbaa7 ("Staging: IIO: core support for device registration and management")
---
 drivers/iio/industrialio-core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/industrialio-core.c +5 -5
index 93d6e5b101cf..5d2f35cf18bc 100644
@@ -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
#3 lore raw

PATCH 3/3 iio: core: Replace lockdep_set_class() + mutex_init() by combined call

In reply to Andy Shevchenko

Replace 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
index 5d2f35cf18bc..f69deefcfb6f 100644
@@ -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
Reviewed-by on the v1 series
#4 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

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.

Quoted 7 lines

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(-)

LGTM,

maintainer
#5 lore raw
Quoted 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 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

Quoted 7 lines
Quoted 2 lines

 drivers/iio/industrialio-core.c | 18 +++++++++++-------

 1 file changed, 11 insertions(+), 7 deletions(-)  

LGTM,

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