From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Arnav Kapoor <kapoorarnav43@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Yasin Lee" <yasin.lee.x@gmail.com>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq
Date: Fri, 27 Nov 2026 13:17:18 +0200 [thread overview]
Message-ID: <awlmvqPOw3eppj3u@ashevche-desk.local> (raw)
In-Reply-To: <20261126213710.1047676-2-kapoorarnav43@gmail.com>
On Fri, Nov 27, 2026 at 03:06:46AM +0530, Arnav Kapoor wrote:
> hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose
> divisor is a u32, so the sum is truncated to 32 bits. MEGA is an
> unsigned long, so on 64-bit the product itself does not overflow, but
> the truncation does, and on 32-bit the product already wraps. Either
> way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a
> zero divisor, e.g.
>
> echo 67108864 > sampling_frequency
>
> (67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit
> and 64-bit architectures. Negative values wrap the same way (-67108864
> also gives 0), which the check added by commit a318cfc08537 ("iio:
> proximity: hx9023s: Protect against division by zero in set_samp_freq")
> does not catch, as it only rejects val == 0 && val2 == 0.
>
> Reject negative values, and reject frequencies of 1 kHz and above: they
> give a period of at most 1 ms, below the smallest entry (2 ms) of
> hx9023s_samp_freq_table[], so they never matched anyway. With them gone
> the divisor stays below NANO and fits in 32 bits, so a plain division is
> enough.
...
> - period_ms = div_u64(NANO, (val * MEGA + val2));
> + /*
> + * 1 kHz or more gives a period below the smallest table entry. Rejecting
> + * it here also keeps the divisor below NANO, so it fits in 32 bits.
> + */
> + if (val >= KILO)
if (val >= (1 * HZ_PER_KHZ))
> + return -EINVAL;
> +
> + period_ms = NANO / (val * MEGA + val2);
Can you find dedicated multipliers like
period_ms = (MSEC_PER_SEC * KILO) / (val * HZ_PER_KHZ * KILO + val2);
? And seems this KILO multiplier has to be explained, as I don't get
the precision used here. Or maybe something even better in your mind?
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-11-27 11:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
2026-11-27 11:17 ` Andy Shevchenko [this message]
2026-11-26 21:36 ` [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
2026-11-27 11:21 ` Andy Shevchenko
2026-11-26 21:36 ` [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq 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=awlmvqPOw3eppj3u@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kapoorarnav43@gmail.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=yasin.lee.x@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®