workaround regression in ina2xx introduced by cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
PATCH v2 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(). 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.
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(-)
drivers/iio/adc/ina2xx-adc.c +9 -3
@@ -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 ());
Signature · 1 line
2.17.1
Jonathan Cameron
maintainerQuoted 21 lines · On Thu, 23 Sep 2021 09:23:16 +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(). This function was changed in kernel 5.4.
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 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 withtimespec64_compare()as we are only interested in the sign being positive or negative.
The variableskip_next_sampleis 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 meanstimespec64_to_ns()can be safely used.
Quoted 4 lines
Signed-off-by Iain Hunter <[email protected]>Fixes: regression introduced by
cb47755725da("time: Prevent undefined behaviour in timespec64_to_ns()")
Please check how to format a fixes tag. As they are used in automated tooling it must be exactly what is documented in
https://www.kernel.org/doc/Documentation/process/submitting-patches.rst
Approach looks sound but I think we can simplify things a little.
Quoted 19 lines
--- drivers/iio/adc/ina2xx-adc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)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; +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 {
drivers/iio/adc/ina2xx-adc.c static int ina2xx_capture_thread(void *data) · line 826
Quoted 10 lines
drivers/iio/adc/ina2xx-adc.c +5
-3timespec64_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);
the local variable doesn't add much and should be a boolean given it can only take true or false.
do {
timespec64_add_ns(&next, 1000 * sampling_us);
} while (timespec64_compare(&next, &now) < 0);Is probably the neatest option.
Quoted 6 lines
drivers/iio/adc/ina2xx-adc.c +3
-0+ + 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 ());