Re: [PATCH v4 2/2] iio: trigger: fix device initialization order in viio_trigger_alloc
From: Andy Shevchenko
Date: Mon Nov 10 2025 - 00:28:39 EST
On Mon, Nov 10, 2025 at 11:58:38AM +0800, Ma Ke wrote:
> 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.
Thanks for the update, my comments below.
...
> - 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);
> + /* 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?
--
With Best Regards,
Andy Shevchenko