Linux PWM subsystem development
 help / color / mirror / Atom feed
* [PATCH] pwm: crc: Avoid division by zero for a disabled PWM
@ 2026-11-30 16:58 Lin Huiyi
  2026-11-30 17:55 ` Uwe Kleine-König
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Huiyi @ 2026-11-30 16:58 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: linux-pwm, Lin Huiyi, stable

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;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] pwm: crc: Avoid division by zero for a disabled PWM
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Uwe Kleine-König @ 2026-11-30 17:55 UTC (permalink / raw)
  To: Lin Huiyi; +Cc: linux-pwm, stable

[-- 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 --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 17:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox