All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Arnav Kapoor <kapoorarnav43@gmail.com>
Cc: "Jyoti Bhayana" <jbhayana@google.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	"kernel test robot" <lkp@intel.com>
Subject: Re: [PATCH] iio: common: scmi_sensors: fix truncating 64-bit divisions
Date: Sat, 3 Oct 2026 22:26:51 +0300	[thread overview]
Message-ID: <asFW-3IPAgFIDViV@ashevche-desk.local> (raw)
In-Reply-To: <20261003055602.145118-1-kapoorarnav43@gmail.com>

On Sat, Oct 03, 2026 at 11:25:55AM +0530, Arnav Kapoor wrote:
> do_div() divides a 64-bit dividend by a 32-bit divisor: the divisor is
> stored in a uint32_t on all architectures. The driver passes 64-bit
> divisors to it in several places, so they are silently truncated:
> 
> - scmi_iio_set_odr_val() divides by uHz, which exceeds 32 bits for
>   sampling frequencies above ~4294 Hz.
> - convert_ns_to_freq() divides by interval_ns, which exceeds 32 bits
>   for update intervals of ~4.29 s and above. If its low 32 bits happen
>   to be zero, this is a division by zero.
> - scmi_iio_get_odr_val(), scmi_iio_convert_interval_to_ns(),
>   scmi_iio_sensor_update_cb() and scmi_iio_get_raw_available() divide
>   by int_pow(10, n), which returns u64 and exceeds 32 bits for n >= 10.
>   The SCMI exponent fields allow values down to -16.
> 
> Truncation results in wrong sampling frequency, timestamp and
> raw_available values being reported to userspace.
> 
> Use div64_u64() and div64_u64_rem(), which take a 64-bit divisor.

> Fixes: f774117c96f9 ("iio/scmi: Adding support for IIO SCMI Based Sensors")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202606040245.XfmRpBhA-lkp@intel.com/
> Assisted-by: Claude:claude-opus-5-5 coccinelle

Assisted-by: LLM coccinelle

> Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>

...

>  		tstamp_scale = sensor->sensor_info->tstamp_scale + 9;
>  		if (tstamp_scale < 0) {
> -			do_div(time, int_pow(10, abs(tstamp_scale)));
> -			time_ns = time;
> +			time_ns = div64_u64(time,
> +					    int_pow(10, abs(tstamp_scale)));

It's fine to have 82 character single line in this case.

>  		} else {
>  			time_ns = time * int_pow(10, tstamp_scale);
>  		}

...

>  static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
>  {
> -	u64 rem, freq;
> +	u64 rem;
>  
> -	freq = NSEC_PER_SEC;

Yeah... (see below)

> -	rem = do_div(freq, interval_ns);
> -	*hz = freq;
> -	*uhz = rem * 1000000UL;
> -	do_div(*uhz, interval_ns);
> +	*hz = div64_u64_rem(NSEC_PER_SEC, interval_ns, &rem);

I would expect to see this be a frequency unit to help with left side of the
equation

	*hz = div64_u64_rem(HZ_PER_GHZ, interval_ns, &rem);

> +	*uhz = div64_u64(rem * 1000000UL, interval_ns);

And MICROHZ_PER_HZ here respectively.

>  }

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-10-03 19:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-03  5:55 [PATCH] iio: common: scmi_sensors: fix truncating 64-bit divisions Arnav Kapoor
2026-10-03 19:26 ` Andy Shevchenko [this message]
2026-10-04 10:43   ` Arnav Kapoor

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=asFW-3IPAgFIDViV@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=geert@linux-m68k.org \
    --cc=jbhayana@google.com \
    --cc=jic23@kernel.org \
    --cc=kapoorarnav43@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=nuno.sa@analog.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.