Re: [PATCH v13 2/2] leds: Add driver for Qualcomm LPG

From: Doug Anderson
Date: Wed Mar 02 2022 - 19:03:51 EST


Hi,

On Fri, Feb 18, 2022 at 10:29 AM Bjorn Andersson
<bjorn.andersson@xxxxxxxxxx> wrote:
>
> +static void lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
> + struct pwm_state *state)
> +{
> + struct lpg *lpg = container_of(chip, struct lpg, pwm);
> + struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
> + unsigned int pre_div;
> + unsigned int refclk;
> + unsigned int val;
> + unsigned int m;
> + u16 pwm_value;
> + int ret;
> +
> + ret = regmap_read(lpg->map, chan->base + LPG_SIZE_CLK_REG, &val);
> + if (ret)
> + return;
> +
> + refclk = lpg_clk_rates[(val & PWM_CLK_SELECT_MASK) - 1];

I don't know why I didn't notice it before (maybe I was accidentally
not building with KASAN?), but in my recent boots I'm getting a KASAN
error pointing at the line above.

Sure enough, the above looks a bit on the unsafe side. If (val & 0x3)
is 0 then the "-1" will not be so wonderful. I put some printouts and,
indeed, it's not so great.

[ 7.201635] DOUG: val is 0x00000004

Amazingly my `refclk` ends up as 0 and I guess somehow this doesn't
cause a divide by 0.


-Doug