All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ina2xx: Use 64-bit arithmetic instead of 32-bit
@ 2018-02-13 16:52 Gustavo A. R. Silva
  2018-02-17 12:38 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2018-02-13 16:52 UTC (permalink / raw)
  To: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler
  Cc: linux-iio, linux-kernel, Gustavo A. R. Silva

Add suffix ULL to constant 1000 in order to give the compiler complete
information about the proper arithmetic to use. Notice that this
constant is used in a context that expects an expression of type
u64 (64 bits, unsigned).

The expression 1000 * sampling_us is currently being evaluated
using 32-bit arithmetic.

Addresses-Coverity-ID: 1463793
Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
---
 drivers/iio/adc/ina2xx-adc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index 0635a79..8649700 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -810,7 +810,7 @@ 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, 1000ULL * sampling_us);
 			delta = timespec64_sub(next, now);
 			delay_us = div_s64(timespec64_to_ns(&delta), 1000);
 		} while (delay_us <= 0);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] iio: adc: ina2xx: Use 64-bit arithmetic instead of 32-bit
  2018-02-13 16:52 [PATCH] iio: adc: ina2xx: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
@ 2018-02-17 12:38 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2018-02-17 12:38 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel, Marc Titinger, Stefan Brüns

On Tue, 13 Feb 2018 10:52:58 -0600
"Gustavo A. R. Silva" <garsilva@embeddedor.com> wrote:

> Add suffix ULL to constant 1000 in order to give the compiler complete
> information about the proper arithmetic to use. Notice that this
> constant is used in a context that expects an expression of type
> u64 (64 bits, unsigned).
> 
> The expression 1000 * sampling_us is currently being evaluated
> using 32-bit arithmetic.
> 
> Addresses-Coverity-ID: 1463793
> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
I've been trying to figure out if this matters in reality.
i.e. whether or not sampling_us is big enough for us to need
64 bit multiplication.
It's equal to the output for the macro SAMPLING_PERIOD(c)
(int_time_vbus + int_time_vshunt) * avg
So taking max values
(8244 + 68100) * 1024 = 78176256 
Then * 1000 which brings it well into the > 32bit range.

So the next question is when was this introduced.
I guess it was Stefan's recent patch but haven't checked yet...

Marc / Stephan, could you check if we are correct in thinking this is a real
bug rather than just a numerical oddity?

Thanks,

Jonathan


> ---
>  drivers/iio/adc/ina2xx-adc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
> index 0635a79..8649700 100644
> --- a/drivers/iio/adc/ina2xx-adc.c
> +++ b/drivers/iio/adc/ina2xx-adc.c
> @@ -810,7 +810,7 @@ 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, 1000ULL * sampling_us);
>  			delta = timespec64_sub(next, now);
>  			delay_us = div_s64(timespec64_to_ns(&delta), 1000);
>  		} while (delay_us <= 0);

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-02-17 12:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-13 16:52 [PATCH] iio: adc: ina2xx: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
2018-02-17 12:38 ` Jonathan Cameron

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.