Skip to content

Commit 1eda523

Browse files
Uwe Kleine-Königgroeck
authored andcommitted
hwmon: (pwm-fan) Ensure that calculation doesn't discard big period values
With MAX_PWM being defined to 255 the code unsigned long period; ... period = ctx->pwm->args.period; state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); calculates a too small value for duty_cycle if the configured period is big (either by discarding the 64 bit value ctx->pwm->args.period or by overflowing the multiplication). As this results in a too slow fan and so maybe an overheating machine better be safe than sorry and error out in .probe. Signed-off-by: Uwe Kleine-König <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Guenter Roeck <[email protected]>
1 parent e042f15 commit 1eda523

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

drivers/hwmon/pwm-fan.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,18 @@ static int pwm_fan_probe(struct platform_device *pdev)
334334

335335
ctx->pwm_value = MAX_PWM;
336336

337-
/* Set duty cycle to maximum allowed and enable PWM output */
338337
pwm_init_state(ctx->pwm, &state);
338+
/*
339+
* __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
340+
* long. Check this here to prevent the fan running at a too low
341+
* frequency.
342+
*/
343+
if (state.period > ULONG_MAX / MAX_PWM + 1) {
344+
dev_err(dev, "Configured period too big\n");
345+
return -EINVAL;
346+
}
347+
348+
/* Set duty cycle to maximum allowed and enable PWM output */
339349
state.duty_cycle = ctx->pwm->args.period - 1;
340350
state.enabled = true;
341351

0 commit comments

Comments
 (0)