mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yu-Ti Kuo <yuti@eastwillow.dev>
To: Jonathan Cameron <jic23@kernel.org>
Cc: "Yu-Ti Kuo" <yuti@eastwillow.dev>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Bartosz Golaszewski" <bartosz.golaszewski@oss.qualcomm.com>,
	"Uwe Kleine-König (The Capable Hub)"
	<u.kleine-koenig@baylibre.com>,
	"Matti Vaittinen" <mazziesaccount@gmail.com>,
	"Chuang Zhu" <git@chuang.cz>,
	"Stefan Brüns" <stefan.bruens@rwth-aachen.de>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] iio: adc: ina2xx: Fix capture thread delay math
Date: Wed,  2 Dec 2026 01:20:36 +0800	[thread overview]
Message-ID: <20261201172037.107305-1-yuti@eastwillow.dev> (raw)

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


             reply	other threads:[~2026-12-01 17:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:20 Yu-Ti Kuo [this message]
2026-12-01 18:54 ` Arnav Kapoor

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=20261201172037.107305-1-yuti@eastwillow.dev \
    --to=yuti@eastwillow.dev \
    --cc=andy@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=dlechner@baylibre.com \
    --cc=git@chuang.cz \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=stefan.bruens@rwth-aachen.de \
    --cc=u.kleine-koenig@baylibre.com \
    /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 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®