All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Paulius Zaleckas" <paulius.zaleckas@gmail.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v2 1/6] iio: pressure: dps310: fix overflow in the pressure compensation
Date: Tue, 13 Oct 2026 11:31:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-169629@kernel.org> (raw)
In-Reply-To: <20261013112124.1878296-2-paulius.zaleckas@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] iio: pressure: dps310: signed integer overflow in compensation edge cases
--

commit 0579c95e71d8f32f605a58fb3d4e9b5b43e70e29
Author: Paulius Zaleckas <paulius.zaleckas@gmail.com>
Subject: iio: pressure: dps310: fix overflow in the pressure compensation

This commit changes the pressure compensation polynomial in
dps310_calculate_pressure() to use Q20 fixed-point arithmetic. It replaces
the previous div64_s64_rem() approach to prevent overflows when evaluating
higher-order polynomial terms.

> diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
> index 85df58ac18090..3bc690cc86090 100644
> --- a/drivers/iio/pressure/dps310.c
> +++ b/drivers/iio/pressure/dps310.c
> @@ -660,71 +660,56 @@ static int dps310_write_raw(struct iio_dev *iio,
>  	}
>  }
>  
> +/* Fraction bits of the fixed point pressure compensation */
> +#define DPS310_Q	20
> +#define DPS310_Q_ONE	(1LL << DPS310_Q)
> +
> +static s64 dps310_mul_q(s64 a, s64 b)
> +{
> +	return (a * b) >> DPS310_Q;
> +}
> +
>  static int dps310_calculate_pressure(struct dps310_data *data, int *val)
>  	__must_hold(&data->lock)
>  {

[ ... ]

> +	/*
> +	 * Section 4.9.1 of the DPS310 spec in fixed point. Dividing p^3 * c30
> +	 * by kP^3 directly overflows s64 at most oversampling rates.
> +	 */
> +	ps = div_s64(p * DPS310_Q_ONE, kp);
> +	ts = div_s64(t * DPS310_Q_ONE, kt);
> +	x2 = dps310_mul_q(ps, ps);
> +	x3 = dps310_mul_q(x2, ps);
> +
> +	pressure = data->c00 * DPS310_Q_ONE;
> +	pressure += div_s64(p * data->c10 * DPS310_Q_ONE, kp);
> +	pressure += div_s64(t * data->c01 * DPS310_Q_ONE, kt);
> +	pressure += dps310_mul_q(data->c11 * ps, ts);

[Severity: Low]
Can this code still overflow s64 under maximum theoretical raw sensor
inputs?

In drivers/iio/pressure/dps310.c:dps310_calculate_pressure(), the
intermediate product data->c11 * ps is evaluated before being passed into
dps310_mul_q(). Once inside dps310_mul_q(), multiplying this product by ts
can exceed s64 bounds when extreme hardware values are returned.

> +	pressure += data->c20 * x2 + data->c30 * x3;
> +	pressure += dps310_mul_q(data->c21 * x2, ts);

[Severity: Low]
Does this nested product vastly exceed the maximum capacity of a signed
64-bit integer when extreme hardware values are returned?

For example, when the sensor returns maximum possible 24-bit raw values
(e.g., from a faulty or maliciously manipulated I2C sensor) combined with
the minimum configuration scale factor (kp/kt of 253952), ps and ts
approach ~33 * 2^20.

The 64-bit product (data->c21 * x2) * ts evaluated inside dps310_mul_q()
exceeds ~1.29 * 10^21, which easily overflows the s64 maximum of
~9.22 * 10^18.

Since the kernel's -fno-strict-overflow compiler flag guarantees
wrap-around rather than undefined behavior, this results in a wrapped
garbage output, defeating the patch's stated primary goal of fixing math
overflows in this calculation.

Could this overflow be prevented without meaningful precision loss by
reordering operations, such as computing data->c21 * dps310_mul_q(x2, ts)
instead of dps310_mul_q(data->c21 * x2, ts)?

This flaw persists and affects the new c31 term introduced later in the
series via commit "iio: pressure: dps310: add Zilltek ZTS8032 support".

> +	pressure >>= DPS310_Q;
>  	if (pressure < 0LL)
>  		return -ERANGE;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261013112124.1878296-1-paulius.zaleckas@gmail.com?part=1

  reply	other threads:[~2026-10-13 11:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 11:21 [PATCH v2 0/6] iio: pressure: dps310: fix pressure overflow, add Zilltek ZTS8032 Paulius Zaleckas
2026-10-13 11:21 ` [PATCH v2 1/6] iio: pressure: dps310: fix overflow in the pressure compensation Paulius Zaleckas
2026-10-13 11:31   ` sashiko-bot [this message]
2026-10-13 11:21 ` [PATCH v2 2/6] dt-bindings: vendor-prefixes: Add ZillTek Technology Paulius Zaleckas
2026-10-13 11:21 ` [PATCH v2 3/6] dt-bindings: iio: pressure: infineon,dps310: Add Zilltek ZTS8032 Paulius Zaleckas
2026-10-13 11:21 ` [PATCH v2 4/6] iio: pressure: dps310: add per-chip data Paulius Zaleckas
2026-10-13 16:52   ` Andy Shevchenko
2026-10-13 11:21 ` [PATCH v2 5/6] iio: pressure: dps310: add an OF match table Paulius Zaleckas
2026-10-13 11:21 ` [PATCH v2 6/6] iio: pressure: dps310: add Zilltek ZTS8032 support Paulius Zaleckas
2026-10-13 16:59   ` Andy Shevchenko

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=sashiko-outbox-169629@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=paulius.zaleckas@gmail.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.