From: "Javier Carrasco" <javier.carrasco.cruz@gmail.com>
To: <sashiko-reviews@lists.linux.dev>,
"Javier Carrasco" <javier.carrasco.cruz@gmail.com>,
"Jonathan Cameron" <jic23@kernel.org>
Cc: <linux-iio@vger.kernel.org>
Subject: Re: [PATCH v8 4/4] iio: light: veml6031x00: add support for events and trigger
Date: Tue, 01 Sep 2026 22:19:51 +0200 [thread overview]
Message-ID: <DL49BADYC1U8.2N2F6YW5ZR5S2@gmail.com> (raw)
In-Reply-To: <20260901132208.EE64C1F000E9@smtp.kernel.org>
On Tue Sep 1, 2026 at 3:22 PM CEST, sashiko-bot wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
> - [High] Use-After-Free of iio->pollfunc in IRQ handler during device unbind or probe failure.
> - [Medium] Unintended I2C read of configuration register when pushing timestamp to buffer
> - [Medium] The interrupt_users counter can permanently underflow during error recovery, breaking hardware interrupt control.
> --
>
> commit c87f3d48e1ee614fc184995b816c8f8679a6b774
> Author: Javier Carrasco <javier.carrasco.cruz@gmail.com>
>
> iio: light: veml6031x00: add support for events and trigger
>
> The device provides a shared interrupt line to notify events and data ready.
> Add support for configurations with and without an interrupt line, providing
> events and trigger support when an interrupt line is available.
>
>> diff --git a/drivers/iio/light/veml6031x00.c b/drivers/iio/light/veml6031x00.c
>> index 3d5a27a0baaed..7bd7975eb1a76 100644
>> --- a/drivers/iio/light/veml6031x00.c
>> +++ b/drivers/iio/light/veml6031x00.c
> [ ... ]
>> +static irqreturn_t veml6031x00_irq(int irq, void *private)
>> +{
> [ ... ]
>> + if (trigger_poll) {
>> + iio->pollfunc->timestamp = timestamp;
>> + iio_trigger_poll_nested(data->trig);
>> + }
>> +
>> + pm_runtime_put(dev);
>> +
>> + return IRQ_HANDLED;
>> +}
>
> [Severity: High]
> Could this lead to a use-after-free of the pollfunc during device unbind or
> if probe fails?
>
> Because the IRQ is requested via devres inside veml6031x00_init_iiodev()
> before the triggered buffer is set up in veml6031x00_probe(), devres will
> tear down the buffer (and free iio->pollfunc) before freeing the IRQ. If an
> interrupt fires during this teardown window, the IRQ thread could be
> preempted and then dereference the freed iio->pollfunc here.
>
> Would it be safer to ensure devm_iio_triggered_buffer_setup() is called
> before the IRQ is requested?
>
I will register the IRQ right after calling
devm_iio_triggered_buffer_setup(), removing it from
veml6031x00_init_iiodev(). It was there not to check i2c->irq twice,
because that is also necessary to properly initialize the IIO device
with the right data, but it is true that the tear down is not clean at
the moment.
> [ ... ]
>> +static int __veml6031x00_set_trigger_state(struct veml6031x00_data *data, bool state)
>> + __must_hold(&data->irq_lock)
>> +{
> [ ... ]
>> + data->trig_en = state;
>> +
>> + return 0;
>> +
>> +clear_interrupt:
>> + veml6031x00_set_interrupt(data, !state);
>> +
>> + return ret;
>> +}
>
> [Severity: Medium]
> Can data->interrupt_users permanently underflow here during error recovery?
>
> If an I2C error occurs in __veml6031x00_set_trigger_state() while disabling
> the trigger, this error recovery path calls veml6031x00_set_interrupt() to
> revert the counter, but fails to update the software state (data->trig_en).
>
> If a subsequent successful disable happens later, the counter will be
> decremented again, potentially underflowing to UINT_MAX and silently skipping
> all future hardware writes.
>
> [ ... ]
That is not true because the error path will *increment* the counter to
re-enable the IRQ and keept it consistent with data->trig_en. If an I2C
operation fails, the trigger will stay enabled in the device registers,
and therefore data-trig_en should stay enabled.
I tested it by adding some hacks to force that situation, and what may
actually happen is that if *on top* of that failure, the I2C operation
in veml6031x00_set_interrupt() also fails, interrupt_users will be
decremented (as it should, because the interrupt could not be
re-enabled), and *then* a second call to disable the trigger will set
interrupt_users to UINT_MAX.
Although this is very unlikely, the fix seems to be trivial:
if (state) {
data->interrupt_users++;
if (data->interrupt_users > 1)
return 0;
} else {
+ if (!data->interrupt_users)
+ return 0;
data->interrupt_users--;
if (data->interrupt_users > 0)
return 0;
}
Note that this fix is not a hack to just get rid of reaching UINT_MAX, it
reflects the fact that the previous failed operations lead to a disabled
interrupt and an enabled trigger in the hardware, and there is no need
to act on the interrupt again when trying to disable the trigger a
second time.
I will add it for V9 after testing it with some hacks to force that
scenario.
Best regards,
Javier
next prev parent reply other threads:[~2026-09-01 20:19 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-01 13:05 [PATCH v8 0/4] iio: light: add support for veml6031x00 ALS series Javier Carrasco
2026-09-01 13:05 ` [PATCH v8 1/4] dt-bindings: iio: light: veml6030: add " Javier Carrasco
2026-09-01 13:05 ` [PATCH v8 2/4] iio: light: add support for " Javier Carrasco
2026-09-01 13:05 ` [PATCH v8 3/4] iio: light: veml6031x00: add support for triggered buffers Javier Carrasco
2026-09-01 13:21 ` sashiko-bot
2026-09-01 13:05 ` [PATCH v8 4/4] iio: light: veml6031x00: add support for events and trigger Javier Carrasco
2026-09-01 13:22 ` sashiko-bot
2026-09-01 20:19 ` Javier Carrasco [this message]
2026-09-02 14:58 ` Javier Carrasco
2026-09-07 1:58 ` Jonathan Cameron
2026-09-07 17:11 ` Javier Carrasco
2026-09-10 2:56 ` Jonathan Cameron
2026-09-07 2:03 ` [PATCH v8 0/4] iio: light: add support for veml6031x00 ALS series Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DL49BADYC1U8.2N2F6YW5ZR5S2@gmail.com \
--to=javier.carrasco.cruz@gmail.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.