Re: [PATCH] iio: adc: ina2xx: Fix capture thread delay math
From: Jonathan Cameron
Date: Sat Dec 05 2026 - 18:44:41 EST
On Wed, 2 Dec 2026 01:20:36 +0800
Yu-Ti Kuo <yuti@xxxxxxxxxxxxxx> wrote:
> In ina2xx_capture_thread(), 1000 * sampling_us can evaluate to a
> negative 32-bit signed int in two cases.
>
> First, on INA219 with allow_async_readout disabled and both
> integration times set to 84 us, SAMPLING_PERIOD() returns 168 us,
> so subtracting 200 yields -32.
>
> Second, on INA226/INA236 with both channels at 1100 us and 1024x
> averaging, SAMPLING_PERIOD() is (1100 + 1100) * 1024 = 2,252,800 us
> (> 2,147,483 us), so 1000 * sampling_us overflows 32-bit signed int.
These a different enough things I'd split this into two more focused
patches. Also try and reduce the length of explanation.
>
> A negative int sign-extends to u64 in timespec64_add_ns(). That spins
> in __iter_div_u64_rem() and advances next far into the future, so
> usleep_range() blocks the capture thread and hangs kthread_stop() when
> disabling the buffer.
>
> Halve sampling_us when it is 200 us or less (168 -> 84 us) instead of
> subtracting 200 or skipping the sleep loop. That keeps existing > 200 us
> configurations unchanged while ensuring usleep_range() runs on every
> cycle, avoiding a non-yielding busy loop on non-sleeping adapters such
> as i2c-gpio (notably on CONFIG_PREEMPT_NONE -stable kernels). Multiply
> as (u64)sampling_us * NSEC_PER_USEC to prevent 32-bit overflow.
Put this reasoning inline - the /2 is non obvious so a very brief
explanation of why should be there as a comment.
>
> Fixes: 8ea2a6386951 ("iio: adc: ina2xx: Use a monotonic clock for delay calculation")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Yu-Ti Kuo <yuti@xxxxxxxxxxxxxx>
> ---
> Tested on Orange Pi 5 Plus (RK3588), 0 dmesg warnings:
> - TI INA219AID at 0x40 (84+84 us):
> - i2c-gpio (~100 kHz, allow_async_readout=0): 567 voluntary context
> switches/s via usleep_range(), stop 1.31 ms (vs 0/s without sleep)
> - i2c-rk3x (400 kHz): ~2040 scans/s (sync), ~2975 scans/s (async),
> stop 0.50 ms
> - Emulated INA226 via i2c-stub (1100+1100 us, 1024x = 2,252,800 us):
> - Before: kthread spun, blocked in usleep_range(); kthread_stop() hung
> - After: continuous capture + 3x stop cycles returned in 1.86-3.29 s
>
> drivers/iio/adc/ina2xx-adc.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
> index c412f5ed46a1..8ecfa2ab4ad7 100644
> --- a/drivers/iio/adc/ina2xx-adc.c
> +++ b/drivers/iio/adc/ina2xx-adc.c
> @@ -806,8 +806,12 @@ static int ina2xx_capture_thread(void *data)
> * Poll a bit faster than the chip internal Fs, in case
> * we wish to sync with the conversion ready flag.
> */
> - if (!chip->allow_async_readout)
> - sampling_us -= 200;
> + if (!chip->allow_async_readout) {
> + if (sampling_us > 200)
> + sampling_us -= 200;
> + else
> + sampling_us /= 2;
Why /2 ?
> + }
>
> ktime_get_ts64(&next);
>
> @@ -840,7 +844,8 @@ static int ina2xx_capture_thread(void *data)
> * multiple times, i.e. samples are dropped.
> */
> do {
> - timespec64_add_ns(&next, 1000 * sampling_us);
> + timespec64_add_ns(&next,
> + (u64)sampling_us * NSEC_PER_USEC);
> delta = timespec64_sub(next, now);
> delay_us = div_s64(timespec64_to_ns(&delta), 1000);
> } while (delay_us <= 0);
>
> base-commit: 2491ffb7ce9661fca8ecd40c74dd3e0e8dcef646