[PATCH v2] pwm: sifive: Always let the first pwm_apply_state succeed

Emil Renner Berthing emil.renner.berthing at canonical.com
Wed Nov 9 04:45:43 PST 2022


On Wed, 9 Nov 2022 at 13:01, Uwe Kleine-König
<u.kleine-koenig at pengutronix.de> wrote:
>
> Hello Emil,
>
> On Wed, Nov 09, 2022 at 12:37:24PM +0100, Emil Renner Berthing wrote:
> > Commit 2cfe9bbec56ea579135cdd92409fff371841904f added support for the
> > RGB and green PWM controlled LEDs on the HiFive Unmatched board
> > managed by the leds-pwm-multicolor and leds-pwm drivers respectively.
> > All three colours of the RGB LED and the green LED run from different
> > lines of the same PWM, but with the same period so this works fine when
> > the LED drivers are loaded one after the other.
> >
> > Unfortunately it does expose a race in the PWM driver when both LED
> > drivers are loaded at roughly the same time. Here is an example:
> >
> >   |          Thread A           |          Thread B           |
> >   |  led_pwm_mc_probe           |  led_pwm_probe              |
> >   |    devm_fwnode_pwm_get      |                             |
> >   |      pwm_sifive_request     |                             |
> >   |        ddata->user_count++  |                             |
> >   |                             |    devm_fwnode_pwm_get      |
> >   |                             |      pwm_sifive_request     |
> >   |                             |        ddata->user_count++  |
> >   |         ...                 |          ...                |
> >   |    pwm_state_apply          |    pwm_state_apply          |
> >   |      pwm_sifive_apply       |      pwm_sifive_apply       |
> >
> > Now both calls to pwm_sifive_apply will see that ddata->approx_period,
> > initially 0, is different from the requested period and the clock needs
> > to be updated. But since ddata->user_count >= 2 both calls will fail
> > with -EBUSY, which will then cause both LED drivers to fail to probe.
> >
> > Fix it by letting the first call to pwm_sifive_apply update the clock
> > even when ddata->user_count != 1.
> >
> > Fixes: 9e37a53eb051 ("pwm: sifive: Add a driver for SiFive SoC PWM")
> > Signed-off-by: Emil Renner Berthing <emil.renner.berthing at canonical.com>
> > ---
> >  drivers/pwm/pwm-sifive.c | 8 +++++++-
> >  1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c
> > index 2d4fa5e5fdd4..b3c60ec72a6e 100644
> > --- a/drivers/pwm/pwm-sifive.c
> > +++ b/drivers/pwm/pwm-sifive.c
> > @@ -159,7 +159,13 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> >
> >       mutex_lock(&ddata->lock);
> >       if (state->period != ddata->approx_period) {
> > -             if (ddata->user_count != 1) {
> > +             /*
> > +              * Don't let a 2nd user change the period underneath the 1st user.
> > +              * However if ddate->approx_period == 0 this is the first time we set
> > +              * any period, so let whoever gets here first set the period so other
> > +              * users who agree on the period won't fail.
> > +              */
> > +             if (ddata->user_count != 1 && ddata->approx_period) {
>
> While I'm convinced this works, we'd get some more uniform behaviour
> compared to other hardwares with similar restrictions if you lock the
> period on enabling the PWM instead of at request time. See for example
> drivers/pwm/pwm-pca9685.c.

Hmm.. that driver uses a pwms_enabled bitmap rather than a user count,
but it still sets the bit in the request method and refuses to change
period in the apply method if more than 1 bit is set. So as far as I
can tell it still suffers from the same race. However using a bitmap
instead of a user count would let us handle everything in the apply
method if we don't set the bit in the request method, but then the
behaviour would still be different. In any case it would still be a
large change to this driver.

How about we merge this bug fix that can easily be backported first
and then look at how it should be handled properly?

/Emil



More information about the linux-riscv mailing list