All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Iain Hunter <drhunter95@gmail.com>
Cc: lothar.felten@gmail.com, iain@hunterembedded.co.uk,
	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: Re: [PATCH] workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
Date: Sat, 11 Sep 2021 14:23:26 +0100	[thread overview]
Message-ID: <20210911142326.4acd95a1@jic23-huawei> (raw)
In-Reply-To: <20210911113645.2547272-1-drhunter95@gmail.com>

On Sat, 11 Sep 2021 12:36:23 +0100
Iain Hunter <drhunter95@gmail.com> wrote:

> 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().
> 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 replace the call to timespec64_to_ns() with the
> contents of that function without the overflow test introduced by the
> commit (ie revert to pre kernel 5.4 behaviour)
> 
> Signed-off-by: Iain Hunter <iain@hunterembedded.co.uk>

Needs a fixes tag with the patch you mention above that added the check
so that we can tell how far back this needs to be backported.



> ---
>  drivers/iio/adc/ina2xx-adc.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
> index a4b2ff9e0..ba3e98fde 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;
> +	s64 delta_ns;
>  
>  	/*
>  	 * Poll a bit faster than the chip internal Fs, in case
> @@ -818,7 +819,8 @@ 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);
> +			delta_ns = (((s64)delta.tv_sec) * NSEC_PER_SEC)+delta.tv_nsec;

spaces around the +

> +			delay_us = div_s64(delta_ns, 1000);

Hmm. The negative timestamp is a bit of a mess anyway.  Perhaps we can do something
neater using the standard functions by checking the validity of the timestamp
using timespec64_valid_strict() in the while loop and dropping the div_s64 out
of the loop.

What do you think?  Would need a comment to explain why we the check on
it being valid though.

Jonathan


>  		} while (delay_us <= 0);
>  
>  		usleep_range(delay_us, (delay_us * 3) >> 1);


      reply	other threads:[~2021-09-11 13:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11 11:36 [PATCH] workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()") Iain Hunter
2021-09-11 13:23 ` Jonathan Cameron [this message]

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=20210911142326.4acd95a1@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=alexandru.ardelean@analog.com \
    --cc=drhunter95@gmail.com \
    --cc=gwendal@chromium.org \
    --cc=iain@hunterembedded.co.uk \
    --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 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.