From: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
To: sashiko-reviews@lists.linux.dev
Cc: Marcelo Schmitt <marcelo.schmitt@analog.com>,
robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org,
linux-iio@vger.kernel.org, marcelo.schmitt1@gmail.com
Subject: Re: [PATCH v7 3/4] iio: adc: ltc2378: Enable high-speed data capture
Date: Fri, 17 Jul 2026 13:35:14 -0300 [thread overview]
Message-ID: <alpZwoau3QVPtBsP@debian-BULLSEYE-live-builder-AMD64> (raw)
In-Reply-To: <20260716212451.923951F000E9@smtp.kernel.org>
On 07/16, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] Integer divide-by-zero leading to kernel panic in `ltc2378_update_conversion_rate` when a sampling frequency of 1 Hz is requested and the PWM driver rounds the period length above 1 second.
> --
>
> --- Patch [3]: [PATCH v7 3/4] iio: adc: ltc2378: Enable high-speed data capture ---
> commit ef54f2c79f2cac1649922032814adc7fcc5f9684
> Author: Marcelo Schmitt <marcelo.schmitt@analog.com>
> Subject: iio: adc: ltc2378: Enable high-speed data capture
>
> This commit enables high-speed data capture for the ltc2378 ADC driver by
> utilizing SPI transfer offloading. It allows data acquisition at faster sample
> rates up to 2 MSPS.
>
> > diff --git a/drivers/iio/adc/ltc2378.c b/drivers/iio/adc/ltc2378.c
> > index 5600c820c7f45..7d29f021cb7b4 100644
> > --- a/drivers/iio/adc/ltc2378.c
> > +++ b/drivers/iio/adc/ltc2378.c
>
> [ ... ]
>
> > + /*
> > + * Configure SPI offload PWM trigger.
> > + * The trigger should fire after tBUSYLH + tCONV + tDSDOBUSYL.
> > + * Minimum time needed: TBUSYLH (13ns) + TCONV (part-specific) + TDSDOBUSYL (5ns)
> > + *
> > + * Use the same period as CNV PWM to avoid timing issues.
> > + * Convert back from period to frequency for the SPI offload API.
> > + */
> > + offload_period_ns = cnv_wf.period_length_ns;
> > + config.periodic.frequency_hz = div_u64(HZ_PER_GHZ, offload_period_ns);
> > + min_read_offset = LTC2378_TBUSYLH_NS + st->info->tconv_ns + LTC2378_TDSDOBUSYL_NS;
> > + offload_offset_ns = min_read_offset;
> > + count = 100;
> > + do {
> > + config.periodic.offset_ns = offload_offset_ns;
> > + ret = spi_offload_trigger_validate(st->offload_trigger, &config);
> > + if (ret)
> > + return ret;
> > + offload_offset_ns += 10;
> > + } while (count-- && config.periodic.offset_ns < min_read_offset);
> > +
> > + /* Check the minimum CNV to SCLK delay is met */
> > + if (config.periodic.offset_ns < min_read_offset)
> > + return -EDOM;
> > +
> > + /* Check the PWM periods remain the same */
> > + offload_period_ns = div64_u64(HZ_PER_GHZ, config.periodic.frequency_hz);
>
> [Severity: High]
> Could this result in a kernel panic due to an integer division by zero?
>
> If a user writes 1 to the IIO in_voltage_sampling_frequency sysfs attribute,
> ltc2378_update_conversion_rate() is called with freq_Hz = 1, and the initial
> requested PWM period is calculated as 1,000,000,000 ns.
>
> If the underlying hardware PWM driver rounds the period slightly upwards
> (e.g., to 1,000,000,001 ns) during pwm_round_waveform_might_sleep() due to
> clock granularity, offload_period_ns will exceed HZ_PER_GHZ.
>
> This causes config.periodic.frequency_hz = div_u64(HZ_PER_GHZ, offload_period_ns)
> to evaluate to 0.
>
> When div64_u64(HZ_PER_GHZ, config.periodic.frequency_hz) is subsequently called,
> it divides by zero.
If we have cnv_wf.period_length_ns == NSEC_PER_SEC + 1 after the offset_ns loop
and config.periodic.frequency_hz == 0 as suggested, then spi_offload_trigger_validate()
will fail the frequency_hz check and ltc2378_update_conversion_rate() errors
out with -EINVAL before reaching div64_u64(HZ_PER_GHZ, config.periodic.frequency_hz).
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/spi/spi-offload-trigger-pwm.c?h=v7.2-rc3#n48
>
> > + if (cnv_wf.period_length_ns != offload_period_ns)
> > + return -EDOM;
> > +
> > + st->offload_trigger_config = config;
> > + st->cnv_wf = cnv_wf;
> > + st->cnv_Hz = DIV_ROUND_CLOSEST_ULL(HZ_PER_GHZ, cnv_wf.period_length_ns);
> > +
> > + return 0;
> > +}
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/cover.1784235595.git.marcelo.schmitt@analog.com?part=3
next prev parent reply other threads:[~2026-07-17 16:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-16 21:08 [PATCH v7 0/4] iio: adc: Add support for LTC2378 and similar ADCs Marcelo Schmitt
2026-07-16 21:09 ` [PATCH v7 1/4] dt-bindings: iio: adc: Add ltc2378 Marcelo Schmitt
2026-07-16 21:09 ` [PATCH v7 2/4] iio: adc: ltc2378: Add support for LTC2378-20 and similar ADCs Marcelo Schmitt
2026-07-16 21:28 ` sashiko-bot
2026-07-17 16:38 ` Marcelo Schmitt
2026-07-18 1:46 ` Jonathan Cameron
2026-07-18 18:48 ` Marcelo Schmitt
2026-07-18 21:39 ` Jonathan Cameron
2026-07-18 1:51 ` Jonathan Cameron
2026-07-16 21:10 ` [PATCH v7 3/4] iio: adc: ltc2378: Enable high-speed data capture Marcelo Schmitt
2026-07-16 21:24 ` sashiko-bot
2026-07-17 16:35 ` Marcelo Schmitt [this message]
2026-07-16 21:10 ` [PATCH v7 4/4] iio: adc: ltc2378: Enable triggered buffer " Marcelo Schmitt
2026-07-16 21:44 ` [PATCH v7 0/4] iio: adc: Add support for LTC2378 and similar ADCs 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=alpZwoau3QVPtBsP@debian-BULLSEYE-live-builder-AMD64 \
--to=marcelo.schmitt1@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=marcelo.schmitt@analog.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox