workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
PATCH 1/1 workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
From: Iain Hunter <[email protected]>
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)
--- drivers/iio/adc/ina2xx-adc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ina2xx-adc.c +3 -1
@@ -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; + delay_us =div_s64 (delta_ns,1000 ); }while (delay_us <=0 );usleep_range (delay_us, (delay_us *3 ) >>1 );
Signature · 1 line
2.17.1
Jonathan Cameron
maintainerQuoted 13 lines · On Sat, 11 Sep 2021 12:36:23 +0100 Iain Hunter <[email protected]> wrote:
From: Iain Hunter <[email protected]>
That change adds an error check to avoid saturation during multiplication to calculate nano seconds in
timespec64_to_ns().
Inina2xx_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 casetimespec64_to_ns()now clamps the -1 second value toKTIME_MAX. This essentially puts ina2xx thread to sleep forever.
Proposed patch is to replace the call totimespec64_to_ns()with the contents of that function without the overflow test introduced by the commit (ie revert to pre kernel 5.4 behaviour)
Quoted 1 line
Signed-off-by Iain Hunter <[email protected]>
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.
Quoted 17 lines
--- drivers/iio/adc/ina2xx-adc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/iio/adc/ina2xx-adc.c +1
-0@@ -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
drivers/iio/adc/ina2xx-adc.c static int ina2xx_capture_thread(void *data) · line 822
Quoted 5 lines
drivers/iio/adc/ina2xx-adc.c +1
-1@@ -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 +
drivers/iio/adc/ina2xx-adc.c static int ina2xx_capture_thread(void *data) · line 823
Quoted 1 line
drivers/iio/adc/ina2xx-adc.c +1
-0+ 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
Quoted 3 lines
drivers/iio/adc/ina2xx-adc.c +0
-0}while (delay_us <=0 );usleep_range (delay_us, (delay_us *3 ) >>1 );