mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ina2xx: Fix capture thread delay math
@ 2026-12-01 17:20 Yu-Ti Kuo
  2026-12-01 18:54 ` Arnav Kapoor
  0 siblings, 1 reply; 2+ messages in thread
From: Yu-Ti Kuo @ 2026-12-01 17:20 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Yu-Ti Kuo, David Lechner, Nuno Sá,
	Andy Shevchenko, Bartosz Golaszewski,
	Uwe Kleine-König (The Capable Hub),
	Matti Vaittinen, Chuang Zhu, Stefan Brüns,
	Greg Kroah-Hartman, linux-iio, linux-kernel

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.

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.

Fixes: 8ea2a6386951 ("iio: adc: ina2xx: Use a monotonic clock for delay calculation")
Cc: stable@vger.kernel.org
Signed-off-by: Yu-Ti Kuo <yuti@eastwillow.dev>
---
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;
+	}
 
 	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
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] iio: adc: ina2xx: Fix capture thread delay math
  2026-12-01 17:20 [PATCH] iio: adc: ina2xx: Fix capture thread delay math Yu-Ti Kuo
@ 2026-12-01 18:54 ` Arnav Kapoor
  0 siblings, 0 replies; 2+ messages in thread
From: Arnav Kapoor @ 2026-12-01 18:54 UTC (permalink / raw)
  To: Yu-Ti Kuo
  Cc: Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, Bartosz Golaszewski, Uwe Kleine-König,
	Matti Vaittinen, Chuang Zhu, Stefan Brüns,
	Greg Kroah-Hartman, linux-iio, linux-kernel

On Wed, Dec 02, 2026 at 01:20:36AM +0800, Yu-Ti Kuo wrote:
> In ina2xx_capture_thread(), 1000 * sampling_us can evaluate to a
> negative 32-bit signed int in two cases.

Reviewed-by: Arnav Kapoor <kapoorarnav43@gmail.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 18:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 17:20 [PATCH] iio: adc: ina2xx: Fix capture thread delay math Yu-Ti Kuo
2026-12-01 18:54 ` Arnav Kapoor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®