From: "Uwe Kleine-König" <ukleinek@kernel.org>
To: Lin Huiyi <xinghui79@qq.com>
Cc: linux-pwm@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] pwm: crc: Avoid division by zero for a disabled PWM
Date: Mon, 30 Nov 2026 18:55:50 +0100 [thread overview]
Message-ID: <aw22oHhvjbMeXfW0@monoceros> (raw)
In-Reply-To: <tencent_D503EB3A48A6D7621A04B8C6CC398DBC7407@qq.com>
[-- Attachment #1: Type: text/plain, Size: 2717 bytes --]
Hello,
On Tue, Dec 01, 2026 at 12:58:55AM +0800, Lin Huiyi wrote:
> crc_pwm_apply() computes the duty cycle register value by dividing by the
> requested period:
>
> u64 level = state->duty_cycle * PWM_MAX_LEVEL;
>
> do_div(level, state->period);
>
> The PWM core accepts a period of zero for a disabled PWM:
> pwm_state_valid() returns true early in that case, and period_store()
> writes the user supplied value to state.period without further
> validation. Writing 0 to the period attribute of a disabled PWM
> therefore reaches this code with a zero divisor and crashes the kernel:
>
> $ echo 0 > /sys/class/pwm/pwmchip0/pwm0/period
> divide error: 0000 [#1] SMP
>
> Return early for disabled PWMs once the backlight has been switched off.
> The duty cycle and clock divider are irrelevant while the output is off
> and are programmed again when the PWM gets enabled.
>
> Fixes: a3f37a104bc4 ("pwm: crc: Add Crystalcove (CRC) PWM driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Lin Huiyi <xinghui79@qq.com>
> ---
> drivers/pwm/pwm-crc.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/pwm/pwm-crc.c b/drivers/pwm/pwm-crc.c
> index 98ee5cdbd0..05f6517443 100644
> --- a/drivers/pwm/pwm-crc.c
> +++ b/drivers/pwm/pwm-crc.c
> @@ -72,6 +72,9 @@ static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> }
> }
>
> + if (!state->enabled)
> + return 0;
> +
> if (pwm_get_duty_cycle(pwm) != state->duty_cycle ||
> pwm_get_period(pwm) != state->period) {
> u64 level = state->duty_cycle * PWM_MAX_LEVEL;
The find is valid, but I'd consider it more natural to do the following
instead:
diff --git a/drivers/pwm/pwm-crc.c b/drivers/pwm/pwm-crc.c
index 98ee5cdbd0ba..19f6cc9e0b81 100644
--- a/drivers/pwm/pwm-crc.c
+++ b/drivers/pwm/pwm-crc.c
@@ -64,12 +64,16 @@ static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
if (state->polarity != PWM_POLARITY_NORMAL)
return -EINVAL;
- if (pwm_is_enabled(pwm) && !state->enabled) {
- err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0);
- if (err) {
- dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
- return err;
+ if (!state->enabled) {
+ if (pwm_is_enabled(pwm)) {
+ err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0);
+ if (err) {
+ dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
+ return err;
+ }
}
+
+ return 0;
}
if (pwm_get_duty_cycle(pwm) != state->duty_cycle ||
Given that then state->enabled is always true further down in
crc_pwm_apply(), some simplifications can be done there (with both, your
patch and my alternative).
Best regards
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
prev parent reply other threads:[~2026-11-30 17:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 16:58 [PATCH] pwm: crc: Avoid division by zero for a disabled PWM Lin Huiyi
2026-11-30 17:55 ` Uwe Kleine-König [this message]
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=aw22oHhvjbMeXfW0@monoceros \
--to=ukleinek@kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=xinghui79@qq.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