Skip to contentSkip to conversation

pwm: sun4i: Move pwm_calculate out of spin_lock

RFC PATCH Superseded Clément Péron 3 participants 5 messages Jan 9, 2020 – Jan 13, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 pwm: sun4i: Move pwm_calculate out of spin_lock

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Move pwm_calculate out of this spin_lock.

Fixes c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")

---

Hi,

this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.

Thanks,
Clément

 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sun4i.c +3 -4
index 1afd41ebd3fd..6b230029dc49 100644
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 		}
 	}
 
-	spin_lock(&sun4i_pwm->ctrl_lock);
-	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
-
 	ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
 				  &bypass);
 	if (ret) {
 		dev_err(chip->dev, "period exceeds the maximum value\n");
-		spin_unlock(&sun4i_pwm->ctrl_lock);
 		if (!cstate.enabled)
 			clk_disable_unprepare(sun4i_pwm->clk);
 		return ret;
 	}
 
+	spin_lock(&sun4i_pwm->ctrl_lock);
+	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
+
 	if (sun4i_pwm->data->has_direct_mod_clk_output) {
 		if (bypass) {
 			ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
Signature · 7 lines
2.20.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v1 1/1
#1 lore raw

Hello Clément,

Quoted 3 lines · Clément Péron

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Slightly orthogonal to this issue, it might be a good idea to add a might_sleep() to clk_get_rate(). (Added clk maintainers to Cc: for this suggestion.)

Quoted 39 lines

Move pwm_calculate out of this spin_lock.

Fixes c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")

---

Hi,

this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.

Thanks,
Clément

 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sun4i.c +0 -4
index 1afd41ebd3fd..6b230029dc49 100644
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 		}
 	}
 
-	spin_lock(&sun4i_pwm->ctrl_lock);
-	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
-
 	ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
 				  &bypass);
 	if (ret) {
 		dev_err(chip->dev, "period exceeds the maximum value\n");
-		spin_unlock(&sun4i_pwm->ctrl_lock);
 		if (!cstate.enabled)

drivers/pwm/pwm-sun4i.c static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 265

Quoted 10 lines
drivers/pwm/pwm-sun4i.c +3 -0
 			clk_disable_unprepare(sun4i_pwm->clk);
 		return ret;
 	}
 
+	spin_lock(&sun4i_pwm->ctrl_lock);
+	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
+
 	if (sun4i_pwm->data->has_direct_mod_clk_output) {
 		if (bypass) {
 			ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);

As sun4i_pwm_calculate does nothing that depends on (or modifies) hardware state (apart from clk_get_rate(sun4i_pwm->clk) which can be assumed to be constant) the change looks good.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
Tested-by on v1 1/1
#2 lore raw
In reply to Clément Péron

drivers/pwm/pwm-sun4i.c static int sun4i_pwm_apply(struct pwm_chip *chip,

Quoted 32 lines · Clément Péron

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Move pwm_calculate out of this spin_lock.

Fixes c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")

---

Hi,

this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.

Thanks,
Clément

 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sun4i.c +0 -0
index 1afd41ebd3fd..6b230029dc49 100644
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip,

struct pwm_device *pwm,

drivers/pwm/pwm-sun4i.c static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

Quoted 7 lines
drivers/pwm/pwm-sun4i.c +0 -3
                }
        }
 
-       spin_lock(&sun4i_pwm->ctrl_lock);
-       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
-
        ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period,

&prescaler,

Quoted 16 lines
drivers/pwm/pwm-sun4i.c +3 -2
                                  &bypass);
        if (ret) {
                dev_err(chip->dev, "period exceeds the maximum value\n");
-               spin_unlock(&sun4i_pwm->ctrl_lock);
                if (!cstate.enabled)
                        clk_disable_unprepare(sun4i_pwm->clk);
                return ret;
        }
 
+       spin_lock(&sun4i_pwm->ctrl_lock);
+       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
+
        if (sun4i_pwm->data->has_direct_mod_clk_output) {
                if (bypass) {
                        ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
--
Quoted 1 line

2.20.1

"""""""""""""""""
Tested on my board Pine64+, problems occurs before the patch but not after...

"""""""""""""""

Tested-by on v1 1/1
#3 lore raw
In reply to Clément Péron
Quoted 55 lines · Clément Péron

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Move pwm_calculate out of this spin_lock.

Fixes c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")

---

Hi,

this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.

Thanks,
Clément

 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sun4i.c +3 -5
index 1afd41ebd3fd..6b230029dc49 100644
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
                }
        }
 
-       spin_lock(&sun4i_pwm->ctrl_lock);
-       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
-
        ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
                                  &bypass);
        if (ret) {
                dev_err(chip->dev, "period exceeds the maximum value\n");
-               spin_unlock(&sun4i_pwm->ctrl_lock);
                if (!cstate.enabled)
                        clk_disable_unprepare(sun4i_pwm->clk);
                return ret;
        }
 
+       spin_lock(&sun4i_pwm->ctrl_lock);
+       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
+
        if (sun4i_pwm->data->has_direct_mod_clk_output) {
                if (bypass) {
                        ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
--
Quoted 2 lines

2.20.1

"""""""""""""""""
Tested on my board Pine64+, problems occurs before the patch but not after...

"""""""""""""""

#4 lore raw

Hi,

Quoted 62 lines · Alexander
Quoted 55 lines · Clément Péron

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Move pwm_calculate out of this spin_lock.

Fixes c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")

---

Hi,

this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.

Thanks,
Clément

 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sun4i.c +3 -5
index 1afd41ebd3fd..6b230029dc49 100644
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
                }
        }
 
-       spin_lock(&sun4i_pwm->ctrl_lock);
-       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
-
        ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
                                  &bypass);
        if (ret) {
                dev_err(chip->dev, "period exceeds the maximum value\n");
-               spin_unlock(&sun4i_pwm->ctrl_lock);
                if (!cstate.enabled)
                        clk_disable_unprepare(sun4i_pwm->clk);
                return ret;
        }
 
+       spin_lock(&sun4i_pwm->ctrl_lock);
+       ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
+
        if (sun4i_pwm->data->has_direct_mod_clk_output) {
                if (bypass) {
                        ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
--
Quoted 2 lines

2.20.1

"""""""""""""""""
Tested on my board Pine64+, problems occurs before the patch but not after...

Quoted 1 line

Thanks, I will send a v2 with your proper name for the Reported-by tag

Regards,
Clement

Quoted 1 line

"""""""""""""""