Skip to contentSkip to conversation

pwm: sysfs: Do not apply state to already disabled PWMs

PATCH Applied Marek Vasut 4 participants 5 messages May 12, 2023 – Jun 23, 2023 PWM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: sysfs: Do not apply state to already disabled PWMs

If the PWM is exported but not enabled, do not call pwm_class_apply_state(). First of all, in this case, period may still be unconfigured and this would make pwm_class_apply_state() return -EINVAL, and then suspend would fail. Second, it makes little sense to apply state onto PWM that is not enabled before suspend.

Failing case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo mem > /sys/power/state
...
pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22
pwm pwmchip4: PM: failed to suspend: error -22
PM: Some devices failed to suspend, or early wake event detected
"

Working case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo 100 > /sys/class/pwm/pwmchip4/pwm1/period
$ echo 10 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo mem > /sys/power/state
...
"

Do not call pwm_class_apply_state() in case the PWM is disabled to fix this issue.

Fixes 7fd4edc57bbae ("pwm: sysfs: Add suspend/resume support")

---
Cc: Brian Norris <[email protected]>
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Thierry Reding <[email protected]>
Cc: Yoshihiro Shimoda <[email protected]>
Cc: [email protected]

---
 drivers/pwm/sysfs.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/pwm/sysfs.c +17 -0
index 1a106ec329392..8d1254761e4dd 100644
@@ -424,6 +424,13 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
 		if (!export)
 			continue;
 
+		/* If pwmchip was not enabled before suspend, do nothing. */
+		if (!export->suspend.enabled) {
+			/* release lock taken in pwm_class_get_state */
+			mutex_unlock(&export->lock);
+			continue;
+		}
+
 		state.enabled = export->suspend.enabled;
 		ret = pwm_class_apply_state(export, pwm, &state);
 		if (ret < 0)
@@ -448,7 +455,17 @@ static int pwm_class_suspend(struct device *parent)
 		if (!export)
 			continue;
 
+		/*
+		 * If pwmchip was not enabled before suspend, save
+		 * state for resume time and do nothing else.
+		 */
 		export->suspend = state;
+		if (!state.enabled) {
+			/* release lock taken in pwm_class_get_state */
+			mutex_unlock(&export->lock);
+			continue;
+		}
+
 		state.enabled = false;
 		ret = pwm_class_apply_state(export, pwm, &state);
 		if (ret < 0) {
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hello Marek,

Quoted 67 lines · Marek Vasut

If the PWM is exported but not enabled, do not call pwm_class_apply_state(). First of all, in this case, period may still be unconfigured and this would make pwm_class_apply_state() return -EINVAL, and then suspend would fail. Second, it makes little sense to apply state onto PWM that is not enabled before suspend.

Failing case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo mem > /sys/power/state
...
pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22
pwm pwmchip4: PM: failed to suspend: error -22
PM: Some devices failed to suspend, or early wake event detected
"

Working case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo 100 > /sys/class/pwm/pwmchip4/pwm1/period
$ echo 10 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo mem > /sys/power/state
...
"

Do not call pwm_class_apply_state() in case the PWM is disabled to fix this issue.

Fixes 7fd4edc57bbae ("pwm: sysfs: Add suspend/resume support")

---
Cc: Brian Norris <[email protected]>
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Thierry Reding <[email protected]>
Cc: Yoshihiro Shimoda <[email protected]>
Cc: [email protected]

---
 drivers/pwm/sysfs.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/pwm/sysfs.c +11 -0
index 1a106ec329392..8d1254761e4dd 100644
@@ -424,6 +424,13 @@ static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
 		if (!export)
 			continue;
 
+		/* If pwmchip was not enabled before suspend, do nothing. */
+		if (!export->suspend.enabled) {
+			/* release lock taken in pwm_class_get_state */
+			mutex_unlock(&export->lock);
+			continue;
+		}
+
 		state.enabled = export->suspend.enabled;
 		ret = pwm_class_apply_state(export, pwm, &state);
 		if (ret < 0)
@@ -448,7 +455,17 @@ static int pwm_class_suspend(struct device *parent)
 		if (!export)
 			continue;
 
+		/*
+		 * If pwmchip was not enabled before suspend, save
+		 * state for resume time and do nothing else.
+		 */

drivers/pwm/sysfs.c static int pwm_class_suspend(struct device *parent) · line 471

Quoted 10 lines
drivers/pwm/sysfs.c +6 -0
 		export->suspend = state;
+		if (!state.enabled) {
+			/* release lock taken in pwm_class_get_state */
+			mutex_unlock(&export->lock);
+			continue;
+		}
+
 		state.enabled = false;
 		ret = pwm_class_apply_state(export, pwm, &state);
 		if (ret < 0) {

The locking and code flow in these functions is really ugly. I see several patch opportunities here ...

Anyhow, these are orthogonal to your patch which looks good.

Thanks
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
Reviewed-by on v1 1/1
#2 lore raw
In reply to Marek Vasut

Hi,

Quoted 25 lines · Marek Vasut

If the PWM is exported but not enabled, do not call pwm_class_apply_state(). First of all, in this case, period may still be unconfigured and this would make pwm_class_apply_state() return -EINVAL, and then suspend would fail. Second, it makes little sense to apply state onto PWM that is not enabled before suspend.

Failing case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo mem > /sys/power/state
...
pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22
pwm pwmchip4: PM: failed to suspend: error -22
PM: Some devices failed to suspend, or early wake event detected
"

Working case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo 100 > /sys/class/pwm/pwmchip4/pwm1/period
$ echo 10 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo mem > /sys/power/state
...
"
Quoted 4 lines

Do not call pwm_class_apply_state() in case the PWM is disabled to fix this issue.

Fixes 7fd4edc57bbae ("pwm: sysfs: Add suspend/resume support")

My first thought was that this still belongs as:

Fixes ef2bf4997f7d ("pwm: Improve args checking in pwm_apply_state()")

but then I realized sysfs suspend/resume support was added *after* that, so indeed, your Fixes tag makes the most sense.

And yes, I think this solution (addressing sysfs.c directly) is best too:

Side question: I wonder if this belongs in linux-stable. It's definitely a bug fix, but the bug has been around a while, with a {under,non}-specified ABI, and it's easy enough to work around I suppose. But inevitably, *any* patch with a Fixes tag gets picked up by somebody's cherry-picking bot, so maybe it doesn't matter... ...Anyway, I guess I'm saying it's probably going to go to linux-stable, whether we want it to or not; and maybe that's OK :)

Thanks,
Brian

Quoted 11 lines

---
Cc: Brian Norris <[email protected]>
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Thierry Reding <[email protected]>
Cc: Yoshihiro Shimoda <[email protected]>
Cc: [email protected]

---
 drivers/pwm/sysfs.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
#3 lore raw
Quoted 1 line · Brian Norris

Hi,

Hi,

Quoted 43 lines · Marek Vasut
Quoted 25 lines

If the PWM is exported but not enabled, do not call pwm_class_apply_state(). First of all, in this case, period may still be unconfigured and this would make pwm_class_apply_state() return -EINVAL, and then suspend would fail. Second, it makes little sense to apply state onto PWM that is not enabled before suspend.

Failing case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo mem > /sys/power/state
...
pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22
pwm pwmchip4: PM: failed to suspend: error -22
PM: Some devices failed to suspend, or early wake event detected
"

Working case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo 100 > /sys/class/pwm/pwmchip4/pwm1/period
$ echo 10 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo mem > /sys/power/state
...
"
Quoted 4 lines

Do not call pwm_class_apply_state() in case the PWM is disabled to fix this issue.

Fixes 7fd4edc57bbae ("pwm: sysfs: Add suspend/resume support")

My first thought was that this still belongs as:

Fixes ef2bf4997f7d ("pwm: Improve args checking in pwm_apply_state()")

but then I realized sysfs suspend/resume support was added *after* that, so indeed, your Fixes tag makes the most sense.

And yes, I think this solution (addressing sysfs.c directly) is best too:

Quoted 7 lines

Side question: I wonder if this belongs in linux-stable. It's definitely a bug fix, but the bug has been around a while, with a {under,non}-specified ABI, and it's easy enough to work around I suppose. But inevitably, *any* patch with a Fixes tag gets picked up by somebody's cherry-picking bot, so maybe it doesn't matter... ...Anyway, I guess I'm saying it's probably going to go to linux-stable, whether we want it to or not; and maybe that's OK :)

To provide a bit more context. This was discovered a person writing userspace application who couldn't suspend the machine because of this, but they also couldn't find out why. So I think this is good candidate for stable as it makes this edge a little less sharp.

I agree it can be worked around and I told them how to update the user application, but still, this was inobvious and obscure.

#4 lore raw
In reply to Marek Vasut
Quoted 16 lines · Marek Vasut

If the PWM is exported but not enabled, do not call pwm_class_apply_state(). First of all, in this case, period may still be unconfigured and this would make pwm_class_apply_state() return -EINVAL, and then suspend would fail. Second, it makes little sense to apply state onto PWM that is not enabled before suspend.

Failing case:
"

$ echo 1 > /sys/class/pwm/pwmchip4/export
$ echo mem > /sys/power/state
...
pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22
pwm pwmchip4: PM: failed to suspend: error -22
PM: Some devices failed to suspend, or early wake event detected
"
Quoted 1 line

[...]

Applied, thanks!

[1/1] pwm: sysfs: Do not apply state to already disabled PWMs

      commit: 38ba83598633373f47951384cfc389181c8d1bed

Best regards,

Signature · 1 line
Thierry Reding <[email protected]>