2019-04-01 18:00:16

by Martin Blumenstingl

[permalink] [raw]
Subject: [PATCH v2 0/1] pwm: meson: fix scheduling while atomic issue

Back in January a "BUG: scheduling while atomic" error showed up during
boot on my Meson8b Odroid-C1 (which uses a PWM regulator as CPU supply).
The call trace comes down to:
__mutex_lock
clk_prepare_lock
clk_core_get_rate
meson_pwm_apply
..
dev_pm_opp_set_rate
..

Jerome has also seen the same problem but from pwm-leds (instead of a
pwm-regulator). He posted a patch which replaces the spinlock with a
mutex. That works. I believe we can optimize this by reducing the time
where the lock is held - that also allows to keep the spin-lock.

Analyzing this issue helped me understand the pwm-meson driver better.
My plan is to send some cleanups after this single fix is merged. The
goal of these cleanups is to re-use more of the goodies from the PWM
core in the pwm-meson driver as well as to address issues spotted by
Uwe Kleine-König (these issues violate the PWM API, but none of these
result in breakage of the boards/.dts that we currently have). These
follow-up patches can be found here: [1].

Dependencies: none

Target version: please queue this for -fixes so it makes it's way into
v5.1-rc (so we can get it backported from there, because this issue has
existed since the pwm-meson driver was introduced).


changes since v1 at [2]:
- added comment about usage of the spinlock as suggested by
Uwe Kleine-König (thank you)
- collected Uwe Kleine-König's Reviewed-by


[0] http://lists.infradead.org/pipermail/linux-amlogic/2019-January/009690.html
[1] https://github.com/xdarklight/linux/commits/meson-pwm-for-5.2-v1
[2] https://patchwork.kernel.org/cover/10867757/


Martin Blumenstingl (1):
pwm: meson: use the spin-lock only to protect register modifications

drivers/pwm/pwm-meson.c | 25 +++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)

--
2.21.0


2019-04-01 17:59:08

by Martin Blumenstingl

[permalink] [raw]
Subject: [PATCH v2 1/1] pwm: meson: use the spin-lock only to protect register modifications

Holding the spin-lock for all of the code in meson_pwm_apply() can
result in a "BUG: scheduling while atomic". This can happen because
clk_get_rate() (which is called from meson_pwm_calc()) may sleep.
Only hold the spin-lock when modifying registers to solve this.

The reason why we need a spin-lock in the driver is because the
REG_MISC_AB register is shared between the two channels provided by one
PWM controller. The only functions where REG_MISC_AB is modified are
meson_pwm_enable() and meson_pwm_disable() so the register reads/writes
in there need to be protected by the spin-lock.

The original code also used the spin-lock to protect the values in
struct meson_pwm_channel. This could be necessary if two consumers can
use the same PWM channel. However, PWM core doesn't allow this so we
don't need to protect the values in struct meson_pwm_channel with a
lock.

Fixes: 211ed630753d2f ("pwm: Add support for Meson PWM Controller")
Signed-off-by: Martin Blumenstingl <[email protected]>
Reviewed-by: Uwe Kleine-König <[email protected]>
---
drivers/pwm/pwm-meson.c | 25 +++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index c1ed641b3e26..f6e738ad7bd9 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -111,6 +111,10 @@ struct meson_pwm {
const struct meson_pwm_data *data;
void __iomem *base;
u8 inverter_mask;
+ /*
+ * Protects register (write) access to the REG_MISC_AB register
+ * that is shared between the two PWMs.
+ */
spinlock_t lock;
};

@@ -235,6 +239,7 @@ static void meson_pwm_enable(struct meson_pwm *meson,
{
u32 value, clk_shift, clk_enable, enable;
unsigned int offset;
+ unsigned long flags;

switch (id) {
case 0:
@@ -255,6 +260,8 @@ static void meson_pwm_enable(struct meson_pwm *meson,
return;
}

+ spin_lock_irqsave(&meson->lock, flags);
+
value = readl(meson->base + REG_MISC_AB);
value &= ~(MISC_CLK_DIV_MASK << clk_shift);
value |= channel->pre_div << clk_shift;
@@ -267,11 +274,14 @@ static void meson_pwm_enable(struct meson_pwm *meson,
value = readl(meson->base + REG_MISC_AB);
value |= enable;
writel(value, meson->base + REG_MISC_AB);
+
+ spin_unlock_irqrestore(&meson->lock, flags);
}

static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
{
u32 value, enable;
+ unsigned long flags;

switch (id) {
case 0:
@@ -286,9 +296,13 @@ static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
return;
}

+ spin_lock_irqsave(&meson->lock, flags);
+
value = readl(meson->base + REG_MISC_AB);
value &= ~enable;
writel(value, meson->base + REG_MISC_AB);
+
+ spin_unlock_irqrestore(&meson->lock, flags);
}

static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -296,19 +310,16 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
{
struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
struct meson_pwm *meson = to_meson_pwm(chip);
- unsigned long flags;
int err = 0;

if (!state)
return -EINVAL;

- spin_lock_irqsave(&meson->lock, flags);
-
if (!state->enabled) {
meson_pwm_disable(meson, pwm->hwpwm);
channel->state.enabled = false;

- goto unlock;
+ return 0;
}

if (state->period != channel->state.period ||
@@ -329,7 +340,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
err = meson_pwm_calc(meson, channel, pwm->hwpwm,
state->duty_cycle, state->period);
if (err < 0)
- goto unlock;
+ return err;

channel->state.polarity = state->polarity;
channel->state.period = state->period;
@@ -341,9 +352,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
channel->state.enabled = true;
}

-unlock:
- spin_unlock_irqrestore(&meson->lock, flags);
- return err;
+ return 0;
}

static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
--
2.21.0

2019-04-03 11:22:25

by Neil Armstrong

[permalink] [raw]
Subject: Re: [PATCH v2 1/1] pwm: meson: use the spin-lock only to protect register modifications

On 01/04/2019 19:57, Martin Blumenstingl wrote:
> Holding the spin-lock for all of the code in meson_pwm_apply() can
> result in a "BUG: scheduling while atomic". This can happen because
> clk_get_rate() (which is called from meson_pwm_calc()) may sleep.
> Only hold the spin-lock when modifying registers to solve this.
>
> The reason why we need a spin-lock in the driver is because the
> REG_MISC_AB register is shared between the two channels provided by one
> PWM controller. The only functions where REG_MISC_AB is modified are
> meson_pwm_enable() and meson_pwm_disable() so the register reads/writes
> in there need to be protected by the spin-lock.
>
> The original code also used the spin-lock to protect the values in
> struct meson_pwm_channel. This could be necessary if two consumers can
> use the same PWM channel. However, PWM core doesn't allow this so we
> don't need to protect the values in struct meson_pwm_channel with a
> lock.
>
> Fixes: 211ed630753d2f ("pwm: Add support for Meson PWM Controller")
> Signed-off-by: Martin Blumenstingl <[email protected]>
> Reviewed-by: Uwe Kleine-König <[email protected]>
> ---
> drivers/pwm/pwm-meson.c | 25 +++++++++++++++++--------
> 1 file changed, 17 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
> index c1ed641b3e26..f6e738ad7bd9 100644
> --- a/drivers/pwm/pwm-meson.c
> +++ b/drivers/pwm/pwm-meson.c
> @@ -111,6 +111,10 @@ struct meson_pwm {
> const struct meson_pwm_data *data;
> void __iomem *base;
> u8 inverter_mask;
> + /*
> + * Protects register (write) access to the REG_MISC_AB register
> + * that is shared between the two PWMs.
> + */
> spinlock_t lock;
> };
>
> @@ -235,6 +239,7 @@ static void meson_pwm_enable(struct meson_pwm *meson,
> {
> u32 value, clk_shift, clk_enable, enable;
> unsigned int offset;
> + unsigned long flags;
>
> switch (id) {
> case 0:
> @@ -255,6 +260,8 @@ static void meson_pwm_enable(struct meson_pwm *meson,
> return;
> }
>
> + spin_lock_irqsave(&meson->lock, flags);
> +
> value = readl(meson->base + REG_MISC_AB);
> value &= ~(MISC_CLK_DIV_MASK << clk_shift);
> value |= channel->pre_div << clk_shift;
> @@ -267,11 +274,14 @@ static void meson_pwm_enable(struct meson_pwm *meson,
> value = readl(meson->base + REG_MISC_AB);
> value |= enable;
> writel(value, meson->base + REG_MISC_AB);
> +
> + spin_unlock_irqrestore(&meson->lock, flags);
> }
>
> static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
> {
> u32 value, enable;
> + unsigned long flags;
>
> switch (id) {
> case 0:
> @@ -286,9 +296,13 @@ static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
> return;
> }
>
> + spin_lock_irqsave(&meson->lock, flags);
> +
> value = readl(meson->base + REG_MISC_AB);
> value &= ~enable;
> writel(value, meson->base + REG_MISC_AB);
> +
> + spin_unlock_irqrestore(&meson->lock, flags);
> }
>
> static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> @@ -296,19 +310,16 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> {
> struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
> struct meson_pwm *meson = to_meson_pwm(chip);
> - unsigned long flags;
> int err = 0;
>
> if (!state)
> return -EINVAL;
>
> - spin_lock_irqsave(&meson->lock, flags);
> -
> if (!state->enabled) {
> meson_pwm_disable(meson, pwm->hwpwm);
> channel->state.enabled = false;
>
> - goto unlock;
> + return 0;
> }
>
> if (state->period != channel->state.period ||
> @@ -329,7 +340,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> err = meson_pwm_calc(meson, channel, pwm->hwpwm,
> state->duty_cycle, state->period);
> if (err < 0)
> - goto unlock;
> + return err;
>
> channel->state.polarity = state->polarity;
> channel->state.period = state->period;
> @@ -341,9 +352,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> channel->state.enabled = true;
> }
>
> -unlock:
> - spin_unlock_irqrestore(&meson->lock, flags);
> - return err;
> + return 0;
> }
>
> static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
>

Reviewed-by: Neil Armstrong <[email protected]>

2019-05-09 14:54:04

by Thierry Reding

[permalink] [raw]
Subject: Re: [PATCH v2 1/1] pwm: meson: use the spin-lock only to protect register modifications

On Mon, Apr 01, 2019 at 07:57:48PM +0200, Martin Blumenstingl wrote:
> Holding the spin-lock for all of the code in meson_pwm_apply() can
> result in a "BUG: scheduling while atomic". This can happen because
> clk_get_rate() (which is called from meson_pwm_calc()) may sleep.
> Only hold the spin-lock when modifying registers to solve this.
>
> The reason why we need a spin-lock in the driver is because the
> REG_MISC_AB register is shared between the two channels provided by one
> PWM controller. The only functions where REG_MISC_AB is modified are
> meson_pwm_enable() and meson_pwm_disable() so the register reads/writes
> in there need to be protected by the spin-lock.
>
> The original code also used the spin-lock to protect the values in
> struct meson_pwm_channel. This could be necessary if two consumers can
> use the same PWM channel. However, PWM core doesn't allow this so we
> don't need to protect the values in struct meson_pwm_channel with a
> lock.
>
> Fixes: 211ed630753d2f ("pwm: Add support for Meson PWM Controller")
> Signed-off-by: Martin Blumenstingl <[email protected]>
> Reviewed-by: Uwe Kleine-König <[email protected]>
> ---
> drivers/pwm/pwm-meson.c | 25 +++++++++++++++++--------
> 1 file changed, 17 insertions(+), 8 deletions(-)

Applied, thanks.

Thierry


Attachments:
(No filename) (1.33 kB)
signature.asc (849.00 B)
Download all attachments