linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Iain Hunter <drhunter95@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: lothar.felten@gmail.com, iain@hunterembedded.co.uk,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>,
	Matt Ranostay <matt.ranostay@konsulko.com>,
	Gwendal Grignou <gwendal@chromium.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
Date: Thu, 23 Sep 2021 09:23:16 +0100	[thread overview]
Message-ID: <20210923082319.1510936-1-drhunter95@gmail.com> (raw)

From: Iain Hunter <iain@hunterembedded.co.uk>

That change adds an error check to avoid saturation during multiplication
to calculate nano seconds in timespec64_to_ns(). This function was changed
in kernel 5.4.
In ina2xx_capture_thread() a timespec64 structure is used to calculate
the delta time until the next sample time. This delta can be negative if
the next sample time was in the past. In the -1 case timespec64_to_ns()
now clamps the -1 second value to KTIME_MAX. This essentially puts ina2xx
thread to sleep forever.
Proposed patch is to split the functionality in the loop into two parts:
- do while loop only does the test to see if the next sample time is in 
the future or in the past and so will be skipped and the sample time 
incremented until it is in the future. This comparision can be done with 
timespec64_compare() as we are only interested in the sign being positive
or negative.
The variable skip_next_sample is only used for clarity.
- after do while loop we know that next is later than now and so delta is
guaranteed to be positive. This means timespec64_to_ns() can be safely
used.

Signed-off-by: Iain Hunter <iain@hunterembedded.co.uk>

Fixes: regression introduced by
 cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
---
 drivers/iio/adc/ina2xx-adc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index a4b2ff9e0..e30012d0d 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -777,6 +777,7 @@ static int ina2xx_capture_thread(void *data)
 	int ret;
 	struct timespec64 next, now, delta;
 	s64 delay_us;
+	int skip_next_sample;
 
 	/*
 	 * Poll a bit faster than the chip internal Fs, in case
@@ -817,10 +818,15 @@ static int ina2xx_capture_thread(void *data)
 		 */
 		do {
 			timespec64_add_ns(&next, 1000 * sampling_us);
-			delta = timespec64_sub(next, now);
-			delay_us = div_s64(timespec64_to_ns(&delta), 1000);
-		} while (delay_us <= 0);
 
+			if (timespec64_compare(&next, &now) < 0)
+				skip_next_sample = 1;
+			else
+				skip_next_sample = 0;
+		} while (skip_next_sample);
+
+		delta = timespec64_sub(next, now);
+		delay_us = div_s64(timespec64_to_ns(&delta), 1000);
 		usleep_range(delay_us, (delay_us * 3) >> 1);
 
 	} while (!kthread_should_stop());
-- 
2.17.1


             reply	other threads:[~2021-09-23  8:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-23  8:23 Iain Hunter [this message]
2021-09-26 12:06 ` [PATCH v2] workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()") 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=20210923082319.1510936-1-drhunter95@gmail.com \
    --to=drhunter95@gmail.com \
    --cc=alexandru.ardelean@analog.com \
    --cc=gwendal@chromium.org \
    --cc=iain@hunterembedded.co.uk \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lothar.felten@gmail.com \
    --cc=matt.ranostay@konsulko.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;
as well as URLs for NNTP newsgroup(s).