Re: [PATCH v3] iio: trigger: Fix error handling in viio_trigger_alloc

From: Andy Shevchenko

Date: Fri Nov 07 2025 - 03:04:23 EST


On Fri, Nov 7, 2025 at 4:02 AM Ma Ke <make24@xxxxxxxxxxx> wrote:
>
> viio_trigger_alloc() initializes the device with device_initialize()
> but uses kfree() directly in error paths, which bypasses the device's
> release callback iio_trig_release(). This could lead to memory leaks
> and inconsistent device state.
>
> Additionally, the current error handling has the following issues:
> 1. Potential double-free of IRQ descriptors when kvasprintf() fails.
> 2. The release function may attempt to free negative subirq_base.
> 3. Missing mutex_destroy() in release function.
>
> Fix these issues by:
> 1. Replacing kfree(trig) with put_device(&trig->dev) in error paths.
> 2. Removing the free_descs label and handling IRQ descriptor freeing
> directly in the kvasprintf() error path.
> 3. Adding missing mutex_destroy().
>
> Found by code review.

...

> trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
> - if (trig->name == NULL)
> - goto free_descs;
> + if (trig->name == NULL) {

> + irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

Why?

> + goto free_trig;
> + }

Please, slow down and think a bit. Also you may split the patch to at least two:
1) add missed mutex_destroy();
2) fix the clean up in the error path.


--
With Best Regards,
Andy Shevchenko