Re: [PATCH 01/35] iio: core: add default_trigger and take reference in iio_device_register()

From: Linus Walleij

Date: Mon Nov 16 2026 - 05:14:25 EST


On Mon, Nov 16, 2026 at 2:48 AM Jonathan Cameron <jic23@xxxxxxxxxx> wrote:

> From: Jonathan Cameron <jonathan.cameron@xxxxxxxxxxxxxxxx>
>
> Some drivers choose to set a default trigger in probe() via
> indio_dev->trig = iio_trigger_get(trig). The matching iio_trigger_put()
> is in iio_device_unregister_trigger_consumer(), called from
> iio_dev_release() only if indio_dev->modes includes one of the triggered
> modes. If probe() fails after the reference is taken but before
> INDIO_BUFFER_TRIGGERED is set (e.g. by iio_triggered_buffer_setup()),
> the reference is leaked. In devm managed flows there is no simple way
> for the driver to drop the reference on error. Whilst it is possible
> to have enforce strict registration order so that the reference is
> only taken once conditions for tear down are met, the requirements are
> not intuitive. So move the point of reference acquire later in sequence
> to a point where cleanup will always happen.
>
> Add struct iio_dev::default_trigger. Drivers then set this without taking
> a reference. The reference is instead taken in
> iio_device_register_trigger_consumer(), which pairs with the existing put
> under the same modes check, so they always balance.
>
> Also check the return value of iio_device_register_trigger_consumer(),
> add an error_unreg_trigger_consumer unwind path for failures that occur
> later in registration, and clear indio_dev->trig after the put so the
> subsequent call from iio_dev_release() does not drop it again.
>
> Immutable triggers are a little more complex and left for a follow
> on patch.
>
> Signed-off-by: Jonathan Cameron <jonathan.cameron@xxxxxxxxxxxxxxxx>

I like the concept and reasoning here, and code looks good so a clear:
Reviewed-by: Linus Walleij <linusw@xxxxxxxxxx>

Yours,
Linus Walleij