pwm: imx27: Fix variable truncation in .apply()
Subsystems (2)
PATCH v2 1/1 pwm: imx27: Fix variable truncation in .apply()
Fix a variable truncation when calculating period in microseconds as part of the solution for the ERR051198 in .apply() callback.
Example scenario:
- Period of 3us (PWMPR = 196 and prescaler = 1)
- Expected value in tmp: 198000000000 (NSEC_PER_SEC * (196 + 2) * 1)
- Actual value is 431504384 (truncation to u32)
---
Changes in v2:
- Added example with actual PWMPR/prescaler values per Frank Li's feedback
- Dropped testing section
--- drivers/pwm/pwm-imx27.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/pwm/pwm-imx27.c +7 -1
@@ -200,7 +200,7 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,static int pwm_imx27_apply (struct pwm_chip *chip,struct pwm_device *pwm,const struct pwm_state *state) { -unsigned long period_cycles, duty_cycles, prescale, period_us, tmp; +unsigned long period_cycles, duty_cycles, prescale, period_us;struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);unsigned long long c;unsigned long long clkrate;
@@ -208,6 +208,7 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,int val;int ret;u32 cr; +u64 tmp; clkrate =clk_get_rate (imx->clks[PWM_IMX27_PER ].clk); c = clkrate * state->period;
@@ -249,6 +250,11 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, val =readl (imx->mmio_base +MX3_PWMPR ); val = val >=MX3_PWMPR_MAX ?MX3_PWMPR_MAX : val; cr =readl (imx->mmio_base +MX3_PWMCR ); + +/* +* tmp stores period in nanoseconds. Result fits in u64 since +* val <= 0xfffe and prescaler in [1, 0x1000]. +*/ tmp =NSEC_PER_SEC * (u64)(val +2 ) *MX3_PWMCR_PRESCALER_GET (cr); tmp =DIV_ROUND_UP_ULL (tmp, clkrate); period_us =DIV_ROUND_UP_ULL (tmp,1000 );
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Division by zero kernel panic if clk_get_rate() returns 0.
- [High] Missing bounds check on state->period causes integer overflows and silent hardware misconfiguration.
- [High] The 1.5us safety margin in the ERR051198 workaround mixes unprescaled and prescaled clock units. --
commit e248ca7d296d611230c3b9bc5da0d73a70d0c358
Author: Ronaldo Nunez <[email protected]>
pwm: imx27: Fix variable truncation in .apply()
Fix a variable truncation when calculating period in microseconds as part of the solution for the ERR051198 in .apply() callback.
Quoted 4 lines
drivers/pwm/pwm-imx27.c +0
-0
[ ... ]
drivers/pwm/pwm-imx27.c static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 214
Quoted 8 lines
drivers/pwm/pwm-imx27.c +1
-0@@ -208,6 +208,7 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,int val;int ret;u32 cr; +u64 tmp; clkrate =clk_get_rate (imx->clks[PWM_IMX27_PER ].clk); c = clkrate * state->period;
[Severity: High]
This is a pre-existing issue, but does this calculation handle excessively large periods correctly?
Userspace can request an arbitrarily large state->period. This can cause a cascading failure:
1. c = clkrate * state->period can overflow a 64-bit integer.
2. The 64-bit result is assigned to unsigned long period_cycles, which truncates it on 32-bit ARM architectures.
3. The calculation prescale = period_cycles / 0x10000 + 1 can produce a value exceeding the hardware's 12-bit limit (4096).
4. MX3_PWMCR_PRESCALER_SET(prescale) uses FIELD_PREP, which masks out higher bits, writing incorrect configurations to the hardware instead of rejecting unachievable periods with -EINVAL.
Could this lead to silent hardware misconfiguration?
[ ... ]
drivers/pwm/pwm-imx27.c static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 259
Quoted 11 lines
drivers/pwm/pwm-imx27.c +5
-0@@ -249,6 +250,11 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, val =readl (imx->mmio_base +MX3_PWMPR ); val = val >=MX3_PWMPR_MAX ?MX3_PWMPR_MAX : val; cr =readl (imx->mmio_base +MX3_PWMCR ); + +/* +* tmp stores period in nanoseconds. Result fits in u64 since +* val <= 0xfffe and prescaler in [1, 0x1000]. +*/ tmp =NSEC_PER_SEC * (u64)(val +2 ) *MX3_PWMCR_PRESCALER_GET (cr); tmp =DIV_ROUND_UP_ULL (tmp, clkrate);
[Severity: High]
This is a pre-existing issue, but could this lead to a division by zero panic?
If the clock is disabled, unparented, or misconfigured, clk_get_rate() will return 0.
Since clkrate is directly used as a divisor here, the kernel would panic. Should the driver validate that clkrate > 0 before proceeding?
drivers/pwm/pwm-imx27.c static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 260
Quoted 1 line
drivers/pwm/pwm-imx27.c +0
-0period_us =DIV_ROUND_UP_ULL (tmp,1000 );
[Severity: High]
This is a pre-existing issue, but is the 1.5us safety margin mixing unprescaled and prescaled clock units?
Later in pwm_imx27_apply(), the safety margin is calculated as:
c = clkrate * 1500; do_div(c, NSEC_PER_SEC);
This yields unprescaled clock cycles. However, val is read from MX3_PWMCNR, which increments in prescaled units:
if ((val + c >= duty_cycles && val < imx->duty_cycle) || val + c >= period_cycles)
Adding c to val directly adds unprescaled ticks to prescaled ticks. When the active prescaler is large (e.g., 4096), does this overestimate the safety margin by a factor of 4096 (e.g., ~6.1ms instead of 1.5us)?
Could this artificially force the workaround to trigger and unnecessarily delay valid duty cycle updates by a full PWM period?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 10 lines · Ronaldo Nunez
Fix a variable truncation when calculating period in microseconds as part of the solution for the ERR051198 in .apply() callback.
Example scenario:
- Period of 3us (PWMPR = 196 and prescaler = 1)
- Expected value in tmp: 198000000000 (NSEC_PER_SEC* (196 + 2) * 1)
- Actual value is 431504384 (truncation to u32)Signed-off-by Ronaldo Nunez <[email protected]>---
Quoted 43 lines
Changes in v2:
- Added example with actual PWMPR/prescaler values per Frank Li's feedback
- Dropped testing section--- drivers/pwm/pwm-imx27.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/pwm/pwm-imx27.c +7
-2@@ -200,7 +200,7 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,static int pwm_imx27_apply (struct pwm_chip *chip,struct pwm_device *pwm,const struct pwm_state *state) { -unsigned long period_cycles, duty_cycles, prescale, period_us, tmp; +unsigned long period_cycles, duty_cycles, prescale, period_us;struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);unsigned long long c;unsigned long long clkrate;@@ -208,6 +208,7 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,int val;int ret;u32 cr; +u64 tmp; clkrate =clk_get_rate (imx->clks[PWM_IMX27_PER ].clk); c = clkrate * state->period;@@ -249,6 +250,11 @@ static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, val =readl (imx->mmio_base +MX3_PWMPR ); val = val >=MX3_PWMPR_MAX ?MX3_PWMPR_MAX : val; cr =readl (imx->mmio_base +MX3_PWMCR ); + +/* +* tmp stores period in nanoseconds. Result fits in u64 since +* val <= 0xfffe and prescaler in [1, 0x1000]. +*/ tmp =NSEC_PER_SEC * (u64)(val +2 ) *MX3_PWMCR_PRESCALER_GET (cr); tmp =DIV_ROUND_UP_ULL (tmp, clkrate); period_us =DIV_ROUND_UP_ULL (tmp,1000 ); --2.53.0
Uwe Kleine-König
maintainerHello Ronaldo,
Quoted 9 lines · Ronaldo Nunez
Fix a variable truncation when calculating period in microseconds as part of the solution for the ERR051198 in .apply() callback.
Example scenario:
- Period of 3us (PWMPR = 196 and prescaler = 1)
- Expected value in tmp: 198000000000 (NSEC_PER_SEC* (196 + 2) * 1)
- Actual value is 431504384 (truncation to u32)Signed-off-by Ronaldo Nunez <[email protected]>
Thanks for your patch. I added
to the trailers and applied it to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/fixes
. I haven't made up my mind if I create another pull request to get the patches from the pwm/fixes branch into 7.1. If not, it will go into 7.2-rc1.
Sashiko found a few more issues in the imx27 driver, would you like to address these, too?
Best regards
Uwe
Hi Uwe,
Quoted 3 lines · Uwe Kleine-König
. I haven't made up my mind if I create another pull request to get the patches from the pwm/fixes branch into 7.1. If not, it will go into 7.2-rc1.
Sounds good, thanks.
Quoted 2 lines
Sashiko found a few more issues in the imx27 driver, would you like to address these, too?
Yes, I'm already taking a look on those issues.
Best,
Ronaldo