summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>2021-03-08 10:16:29 +0100
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>2021-03-17 11:48:23 +0100
commit5bef4efc448a7f280139af49d71503ffe050c45f (patch)
tree13090c2a9ffef89db2f00bc8caaeb8376e445b74
parent9e346ba4a937b4633ceee2bb046117ab4e20a541 (diff)
downloadlinux-pwm-next.tar.gz
linux-pwm-next.tar.xz
pwm: Soften potential loss of precision in compat codepwm-next
The legacy callback .config() only uses int for period and duty_cycle while the corresponding values in struct pwm_state are u64. To prevent that a value bigger than INT_MAX is discarded to a very small value, explicitly check for big values and pass INT_MAX instead of discarding. Acked-by: Guru Das Srinagesh <gurus@codeaurora.org> Link: https://lore.kernel.org/r/20210315080050.2337075-1-u.kleine-koenig@pengutronix.de Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
-rw-r--r--drivers/pwm/core.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 4b3779d58c5a..b1adf3bb8508 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -605,9 +605,18 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
if (state->period != pwm->state.period ||
state->duty_cycle != pwm->state.duty_cycle) {
+ int duty_cycle, period;
+
+ /*
+ * The legacy callbacks use only (signed!) int for
+ * period and duty_cycle compared to u64 in struct
+ * pwm_state. So clamp the values to INT_MAX.
+ */
+ period = min(state->period, (u64)INT_MAX);
+ duty_cycle = min(state->duty_cycle, (u64)INT_MAX);
+
err = chip->ops->config(pwm->chip, pwm,
- state->duty_cycle,
- state->period);
+ duty_cycle, period);
if (err)
return err;