Skip to contentSkip to conversation

iio: trigger: fix device initialization order in viio_trigger_alloc

PATCH In review Ma Ke 3 participants 3 messages Nov 10, 2025 IIO

lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v4 2/2 iio: trigger: fix device initialization order in viio_trigger_alloc

Move device initialization to the end of viio_trigger_alloc() to simplify error handling. This follows the pattern used in similar functions like spi_alloc_device(), where device_initialize() is called only after all resources have been successfully allocated.

This change eliminates the need for complex cleanup in error paths and ensures that the device release callback only runs when the device was fully initialized.

By moving device_initialize() after all resource allocations, we can use simple kfree() in error paths instead of put_device(), making the code more straightforward and less error-prone.

Found by code review.

---
Changes in v4:
- split the patch into two independent patches and modified according to developer's suggestions; Changes in v3:
- modified the patch;
Changes in v2:
- modified the patch, thanks for developer's suggestions.

---
 drivers/iio/industrialio-trigger.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/industrialio-trigger.c +7 -6
index 5baa83349e8f..760ae3e60639 100644
@@ -562,12 +562,6 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 	if (!trig)
 		return NULL;
 
-	trig->dev.parent = parent;
-	trig->dev.type = &iio_trig_type;
-	trig->dev.bus = &iio_bus_type;
-	device_initialize(&trig->dev);
-	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
-
 	mutex_init(&trig->pool_lock);
 	trig->subirq_base = irq_alloc_descs(-1, 0,
 					    CONFIG_IIO_CONSUMERS_PER_TRIGGER,
@@ -593,6 +587,13 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
 	}
 
+	/* Initialize device only after all resources are allocated */
+	trig->dev.parent = parent;
+	trig->dev.type = &iio_trig_type;
+	trig->dev.bus = &iio_bus_type;
+	device_initialize(&trig->dev);
+	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
+
 	return trig;
 
 free_descs:
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Ma Ke

Move device initialization to the end of viio_trigger_alloc() to simplify error handling. This follows the pattern used in similar functions like spi_alloc_device(), where device_initialize() is called only after all resources have been successfully allocated.

This change eliminates the need for complex cleanup in error paths and ensures that the device release callback only runs when the device was fully initialized.

Quoted 5 lines

By moving device_initialize() after all resource allocations, we can use simple kfree() in error paths instead of put_device(), making the code more straightforward and less error-prone.

Found by code review.

Thanks for the update, my comments below.

...

Quoted 4 lines
(quoted diff) +0 -4
-	trig->dev.parent = parent;
-	trig->dev.type = &iio_trig_type;
-	trig->dev.bus = &iio_bus_type;
-	device_initialize(&trig->dev);

Review

Quoted 10 lines
(quoted diff) +6 -1
-	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
 
 
+	/* Initialize device only after all resources are allocated */
+	trig->dev.parent = parent;
+	trig->dev.type = &iio_trig_type;
+	trig->dev.bus = &iio_bus_type;
+	device_initialize(&trig->dev);
 
+	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);

Why has this been moved?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v4 2/2
#2 lore raw
In reply to Ma Ke
Quoted 13 lines · Ma Ke

Move device initialization to the end of viio_trigger_alloc() to simplify error handling. This follows the pattern used in similar functions like spi_alloc_device(), where device_initialize() is called only after all resources have been successfully allocated.

This change eliminates the need for complex cleanup in error paths and ensures that the device release callback only runs when the device was fully initialized.

By moving device_initialize() after all resource allocations, we can use simple kfree() in error paths instead of put_device(), making the code more straightforward and less error-prone.

Quoted 5 lines

Found by code review.

---

Have the same question Andy has... With that addressed:

Quoted 41 lines

Changes in v4:
- split the patch into two independent patches and modified according to developer's suggestions; Changes in v3:
- modified the patch;
Changes in v2:
- modified the patch, thanks for developer's suggestions.
---
 drivers/iio/industrialio-trigger.c | 13 +++++++------

 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/industrialio-trigger.c +0 -0
index 5baa83349e8f..760ae3e60639 100644
@@ -562,12 +562,6 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
  	if (!trig)
  		return NULL;

drivers/iio/industrialio-trigger.c +0 -6
-	trig->dev.parent = parent;
-	trig->dev.type = &iio_trig_type;
-	trig->dev.bus = &iio_bus_type;
-	device_initialize(&trig->dev);
-	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
-

  mutex_init(&trig->pool_lock);
  trig->subirq_base = irq_alloc_descs(-1, 0, CONFIG_IIO_CONSUMERS_PER_TRIGGER,

drivers/iio/industrialio-trigger.c +0 -0
@@ -593,6 +587,13 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
  				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
  	}

drivers/iio/industrialio-trigger.c +7 -0
+	/* Initialize device only after all resources are allocated */
+	trig->dev.parent = parent;
+	trig->dev.type = &iio_trig_type;
+	trig->dev.bus = &iio_bus_type;
+	device_initialize(&trig->dev);
+	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
+

  return trig;

 free_descs: