* [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions
2026-11-25 12:46 FAILED: patch "[PATCH] iio: common: scmi_sensors: fix truncating 64-bit divisions" failed to apply to 6.12-stable tree gregkh
@ 2026-11-25 15:09 ` Arnav Kapoor
2026-11-25 15:14 ` Arnav Kapoor
2026-11-25 15:16 ` Arnav Kapoor
2 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-25 15:09 UTC (permalink / raw)
To: stable; +Cc: kernel test robot, Andy Shevchenko, Jonathan Cameron
commit 043ececf147863fbd253687aa0b9edcac358a711 upstream.
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/
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
[ Use NSEC_PER_SEC instead of HZ_PER_GHZ, which this tree's units.h
doesn't have (same value, and what the old code used). ]
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/common/scmi_sensors/scmi_iio.c | 27 +++++++++++-----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/common/scmi_sensors/scmi_iio.c b/drivers/iio/common/scmi_sensors/scmi_iio.c
index ed15dcbf4cf6..bcd85bdaebbe 100644
--- a/drivers/iio/common/scmi_sensors/scmi_iio.c
+++ b/drivers/iio/common/scmi_sensors/scmi_iio.c
@@ -14,6 +14,7 @@
#include <linux/iio/sysfs.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
+#include <linux/math64.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/scmi_protocol.h>
@@ -71,8 +72,7 @@ static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
tstamp_scale = sensor->sensor_info->tstamp_scale +
const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
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)));
} else {
time_ns = time * int_pow(10, tstamp_scale);
}
@@ -163,7 +163,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
sec = int_pow(10, mult) * MICROHZ_PER_HZ;
- do_div(sec, uHz);
+ sec = div64_u64(sec, uHz);
if (sec == 0) {
dev_err(&iio_dev->dev,
"Trying to set invalid sensor update value for sensor %s",
@@ -238,13 +238,10 @@ static int scmi_iio_read_avail(struct iio_dev *iio_dev,
static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
{
- u64 rem, freq;
+ u64 rem;
- freq = NSEC_PER_SEC;
- 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);
+ *uhz = div64_u64(rem * MICROHZ_PER_HZ, interval_ns);
}
static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -270,7 +267,8 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
@@ -397,9 +395,9 @@ static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
*/
exponent = exponent - scale;
if (exponent < 0) {
- rem = do_div(resolution,
- int_pow(10, abs(exponent))
- );
+ resolution = div64_u64_rem(resolution,
+ int_pow(10, abs(exponent)),
+ &rem);
len = sysfs_emit(buf,
"[%lld %llu.%llu %lld]\n", min_range,
resolution, rem, max_range);
@@ -504,7 +502,8 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
mult = SCMI_SENS_INTVL_GET_EXP(val);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions
2026-11-25 12:46 FAILED: patch "[PATCH] iio: common: scmi_sensors: fix truncating 64-bit divisions" failed to apply to 6.12-stable tree gregkh
2026-11-25 15:09 ` [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions Arnav Kapoor
@ 2026-11-25 15:14 ` Arnav Kapoor
2026-11-25 15:16 ` Arnav Kapoor
2 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-25 15:14 UTC (permalink / raw)
To: stable; +Cc: kernel test robot, Andy Shevchenko, Jonathan Cameron
commit 043ececf147863fbd253687aa0b9edcac358a711 upstream.
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/
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
[ Use NSEC_PER_SEC instead of HZ_PER_GHZ, which this tree's units.h
doesn't have (same value, and what the old code used). ]
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/common/scmi_sensors/scmi_iio.c | 27 +++++++++++-----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/common/scmi_sensors/scmi_iio.c b/drivers/iio/common/scmi_sensors/scmi_iio.c
index ed15dcbf4cf6..bcd85bdaebbe 100644
--- a/drivers/iio/common/scmi_sensors/scmi_iio.c
+++ b/drivers/iio/common/scmi_sensors/scmi_iio.c
@@ -14,6 +14,7 @@
#include <linux/iio/sysfs.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
+#include <linux/math64.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/scmi_protocol.h>
@@ -71,8 +72,7 @@ static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
tstamp_scale = sensor->sensor_info->tstamp_scale +
const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
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)));
} else {
time_ns = time * int_pow(10, tstamp_scale);
}
@@ -163,7 +163,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
sec = int_pow(10, mult) * MICROHZ_PER_HZ;
- do_div(sec, uHz);
+ sec = div64_u64(sec, uHz);
if (sec == 0) {
dev_err(&iio_dev->dev,
"Trying to set invalid sensor update value for sensor %s",
@@ -238,13 +238,10 @@ static int scmi_iio_read_avail(struct iio_dev *iio_dev,
static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
{
- u64 rem, freq;
+ u64 rem;
- freq = NSEC_PER_SEC;
- 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);
+ *uhz = div64_u64(rem * MICROHZ_PER_HZ, interval_ns);
}
static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -270,7 +267,8 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
@@ -397,9 +395,9 @@ static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
*/
exponent = exponent - scale;
if (exponent < 0) {
- rem = do_div(resolution,
- int_pow(10, abs(exponent))
- );
+ resolution = div64_u64_rem(resolution,
+ int_pow(10, abs(exponent)),
+ &rem);
len = sysfs_emit(buf,
"[%lld %llu.%llu %lld]\n", min_range,
resolution, rem, max_range);
@@ -504,7 +502,8 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
mult = SCMI_SENS_INTVL_GET_EXP(val);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions
2026-11-25 12:46 FAILED: patch "[PATCH] iio: common: scmi_sensors: fix truncating 64-bit divisions" failed to apply to 6.12-stable tree gregkh
2026-11-25 15:09 ` [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions Arnav Kapoor
2026-11-25 15:14 ` Arnav Kapoor
@ 2026-11-25 15:16 ` Arnav Kapoor
2026-11-27 17:01 ` Sasha Levin
2 siblings, 1 reply; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-25 15:16 UTC (permalink / raw)
To: stable; +Cc: kernel test robot, Andy Shevchenko, Jonathan Cameron
commit 043ececf147863fbd253687aa0b9edcac358a711 upstream.
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/
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
[ Use NSEC_PER_SEC instead of HZ_PER_GHZ, which this tree's units.h
doesn't have (same value, and what the old code used). ]
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/common/scmi_sensors/scmi_iio.c | 27 +++++++++++-----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/common/scmi_sensors/scmi_iio.c b/drivers/iio/common/scmi_sensors/scmi_iio.c
index ed15dcbf4cf6..bcd85bdaebbe 100644
--- a/drivers/iio/common/scmi_sensors/scmi_iio.c
+++ b/drivers/iio/common/scmi_sensors/scmi_iio.c
@@ -14,6 +14,7 @@
#include <linux/iio/sysfs.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
+#include <linux/math64.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/scmi_protocol.h>
@@ -71,8 +72,7 @@ static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
tstamp_scale = sensor->sensor_info->tstamp_scale +
const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
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)));
} else {
time_ns = time * int_pow(10, tstamp_scale);
}
@@ -163,7 +163,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
sec = int_pow(10, mult) * MICROHZ_PER_HZ;
- do_div(sec, uHz);
+ sec = div64_u64(sec, uHz);
if (sec == 0) {
dev_err(&iio_dev->dev,
"Trying to set invalid sensor update value for sensor %s",
@@ -238,13 +238,10 @@ static int scmi_iio_read_avail(struct iio_dev *iio_dev,
static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
{
- u64 rem, freq;
+ u64 rem;
- freq = NSEC_PER_SEC;
- 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);
+ *uhz = div64_u64(rem * MICROHZ_PER_HZ, interval_ns);
}
static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -270,7 +267,8 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
@@ -397,9 +395,9 @@ static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
*/
exponent = exponent - scale;
if (exponent < 0) {
- rem = do_div(resolution,
- int_pow(10, abs(exponent))
- );
+ resolution = div64_u64_rem(resolution,
+ int_pow(10, abs(exponent)),
+ &rem);
len = sysfs_emit(buf,
"[%lld %llu.%llu %lld]\n", min_range,
resolution, rem, max_range);
@@ -504,7 +502,8 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
mult = SCMI_SENS_INTVL_GET_EXP(val);
if (mult < 0) {
sensor_interval_mult = int_pow(10, abs(mult));
- do_div(sensor_update_interval, sensor_interval_mult);
+ sensor_update_interval = div64_u64(sensor_update_interval,
+ sensor_interval_mult);
} else {
sensor_interval_mult = int_pow(10, mult);
sensor_update_interval =
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH 6.12.y] iio: common: scmi_sensors: fix truncating 64-bit divisions
2026-11-25 15:16 ` Arnav Kapoor
@ 2026-11-27 17:01 ` Sasha Levin
0 siblings, 0 replies; 5+ messages in thread
From: Sasha Levin @ 2026-11-27 17:01 UTC (permalink / raw)
To: stable
Cc: Sasha Levin, kernel test robot, Andy Shevchenko, Jonathan Cameron,
Arnav Kapoor
> 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:
Queued for 6.12, 6.6, 6.1 and 5.15, thanks.
--
Thanks,
Sasha
^ permalink raw reply [flat|nested] 5+ messages in thread