Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrea della Porta" <andrea.porta@suse.com>, devicetree@vger.kernel.org
Cc: linux-hwmon@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver
Date: Tue, 24 Nov 2026 17:46:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-231741@kernel.org> (raw)
In-Reply-To: <20261124173658.31030-3-andrea.porta@suse.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] pwm: rp1: u32 overflow and invalid offset in
  rp1_pwm_round_waveform_fromhw()
--

commit 44a8d1ea8978f906a50c0662a30ab9153df78f82
Author: Naushir Patuck <naush@raspberrypi.com>

pwm: rp1: Add RP1 PWM controller driver

This commit adds a PWM driver for the 4-channel PWM controller in the
Raspberry Pi RP1 southbridge.

> diff --git a/drivers/pwm/pwm-rp1.c b/drivers/pwm/pwm-rp1.c
> new file mode 100644
> index 0000000000000..69ce0db4e9aa0
> --- /dev/null
> +++ b/drivers/pwm/pwm-rp1.c
[ ... ]
> +static int rp1_pwm_round_waveform_fromhw(struct pwm_chip *chip,
> +					 struct pwm_device *pwm,
> +					 const void *_wfhw,
> +					 struct pwm_waveform *wf)
> +{
> +	struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
> +	const struct rp1_pwm_waveform *wfhw = _wfhw;
> +	u64 clk_rate = rp1->clk_rate;
> +	u64 ticks;
> +
> +	*wf = (struct pwm_waveform){ };
> +
> +	if (!wfhw->enabled)
> +		return 0;
> +
> +	wf->period_length_ns = DIV_ROUND_UP_ULL(((u64)wfhw->period_ticks + 1) * NSEC_PER_SEC,
> +						(u32)clk_rate);
> +
> +	if (!wfhw->inverted_polarity) {
> +		ticks = min_t(u64, wfhw->duty_ticks, (u64)wfhw->period_ticks + 1);
> +		wf->duty_length_ns = DIV_ROUND_UP_ULL(ticks * NSEC_PER_SEC,
> +						      (u32)clk_rate);
> +	} else {
> +		if (wfhw->duty_ticks > (u64)wfhw->period_ticks + 1) {
> +			/* 100% duty cycle case */
> +			ticks = 0;
> +		} else {
> +			ticks = (u64)wfhw->period_ticks + 1 - wfhw->duty_ticks;
> +		}
> +		wf->duty_length_ns = DIV_ROUND_UP_ULL(ticks * NSEC_PER_SEC, (u32)clk_rate);
> +		wf->duty_offset_ns = DIV_ROUND_UP_ULL((u64)(wfhw->period_ticks + 1 - ticks) *
> +						      NSEC_PER_SEC, (u32)clk_rate);
> +	}

[Severity: Medium]
Can the wf->duty_offset_ns calculation overflow u32 or produce an invalid
waveform when inverted_polarity is set?

In (u64)(wfhw->period_ticks + 1 - ticks), wfhw->period_ticks + 1 is
evaluated in 32-bit unsigned arithmetic before subtracting the u64 ticks,
so when wfhw->period_ticks is U32_MAX it wraps to 0 and underflows u64
for any ticks > 0.

Also, when wfhw->duty_ticks >= (u64)wfhw->period_ticks + 1, ticks is 0
(wf->duty_length_ns == 0) and wf->duty_offset_ns becomes equal to
wf->period_length_ns, which pwm_wf_valid() rejects because a non-zero
duty_offset_ns must be strictly less than period_length_ns.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124173658.31030-1-andrea.porta@suse.com?part=2

  reply	other threads:[~2026-11-24 17:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver Andrea della Porta
2026-11-24 17:46   ` sashiko-bot [this message]
2026-11-24 17:36 ` [PATCH v13 3/3] pwm: rp1: Register auxiliary device for the fan tachometer Andrea della Porta

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-231741@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=andrea.porta@suse.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --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