Skip to contentSkip to conversation

PM: wakeup: set device_set_wakeup_capable to false in case of error

RFC PATCH In review Dhruva Gole 2 participants 3 messages Jan 29, 2025 – Jan 30, 2025 Power management core

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 PM: wakeup: set device_set_wakeup_capable to false in case of error

In device_init_wakeup enable, we first set device_set_wakeup_capable to true. However in case the device_wakeup_enable fails for whatever reason, there was no error handling being done.
Consequenty, there was no cleanup being done to device_set_wakeup_capable.

If a certain API is enabling something, it should take care of disabling it in error scenarios. In this case device_init_wakeup should on it's own check for errors and clean up accordingly.

---

This patch was briefly proposed in a related thread [1], where this discussion was taking place.
That probably got missed due to some confusion around the device_init_wakeup return value.

There is infact error returning being done in drivers/base/power/wakeup.c, and ideally we should be using that info as done in this patch.

If this patch get accepted, it might even bring forth few hidden bugs due to missing error handling, and it will also change the patch for devm_device_init_wakeup() helper slightly[2].

[1] https://lore.kernel.org/linux-pm/20241218064335.c72gmw56ogtp36a2@lcpd911/
[2] https://lore.kernel.org/linux-pm/[email protected]/

---
 include/linux/pm_wakeup.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/linux/pm_wakeup.h +5 -1
index d501c09c60cd..ed62a7055a54 100644
@@ -231,9 +231,13 @@ static inline void pm_wakeup_hard_event(struct device *dev)
  */
 static inline int device_init_wakeup(struct device *dev, bool enable)
 {
+	int err;
 	if (enable) {
 		device_set_wakeup_capable(dev, true);
-		return device_wakeup_enable(dev);
+		err = device_wakeup_enable(dev);
+		if (err)
+			device_set_wakeup_capable(dev, false);
+		return err;
 	}
 	device_wakeup_disable(dev);
 	device_set_wakeup_capable(dev, false);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Dhruva Gole

In device_init_wakeup enable, we first set device_set_wakeup_capable to true. However in case the device_wakeup_enable fails for whatever reason, there was no error handling being done.
Consequenty, there was no cleanup being done to device_set_wakeup_capable.

s/Consequenty/Consequently/

Quoted 3 lines

If a certain API is enabling something, it should take care of disabling it in error scenarios. In this case device_init_wakeup should on it's own check for errors and clean up accordingly.

Why do you think that failing device_wakeup_enable() will always mean that the device is not in fact wakeup capable?

Quoted 45 lines

---

This patch was briefly proposed in a related thread [1], where this discussion was taking place.
That probably got missed due to some confusion around the device_init_wakeup return value.

There is infact error returning being done in drivers/base/power/wakeup.c, and ideally we should be using that info as done in this patch.

If this patch get accepted, it might even bring forth few hidden bugs due to missing error handling, and it will also change the patch for devm_device_init_wakeup() helper slightly[2].

[1] https://lore.kernel.org/linux-pm/20241218064335.c72gmw56ogtp36a2@lcpd911/
[2] https://lore.kernel.org/linux-pm/[email protected]/

---
 include/linux/pm_wakeup.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/linux/pm_wakeup.h +5 -2
index d501c09c60cd..ed62a7055a54 100644
@@ -231,9 +231,13 @@ static inline void pm_wakeup_hard_event(struct device *dev)
  */
 static inline int device_init_wakeup(struct device *dev, bool enable)
 {
+       int err;
        if (enable) {
                device_set_wakeup_capable(dev, true);
-               return device_wakeup_enable(dev);
+               err = device_wakeup_enable(dev);
+               if (err)
+                       device_set_wakeup_capable(dev, false);
+               return err;
        }
        device_wakeup_disable(dev);
        device_set_wakeup_capable(dev, false);
--

2.34.1

#2 lore raw

Hi Rafael,

Quoted 8 lines · Rafael J. Wysocki
Quoted 5 lines · Dhruva Gole

In device_init_wakeup enable, we first set device_set_wakeup_capable to true. However in case the device_wakeup_enable fails for whatever reason, there was no error handling being done.
Consequenty, there was no cleanup being done to device_set_wakeup_capable.

s/Consequenty/Consequently/

My bad. I will fix it if we decide to revise this patch.

Quoted 7 lines
Quoted 4 lines

If a certain API is enabling something, it should take care of disabling it in error scenarios. In this case device_init_wakeup should on it's own check for errors and clean up accordingly.

Why do you think that failing device_wakeup_enable() will always mean that the device is not in fact wakeup capable?

Well, I was looking at it more from a mirror image perspective. By definition,
@enable: Whether or not to enable @dev as a wakeup device.

If this is false, then in that path we are marking device "not wakeup capable".
Then why not apply the same logic if something goes bad while trying to "enable" the device as wakeup?

Why must a device claim to be wakeup capable if device_wakeup_enable went bad?

Or, then the other way round, why must we device_set_wakeup_capable false in the case where we just want to disable the device wakeup? It's not like we're taking away the capability itself right? Just disabling it for that moment?

I am just trying to make sense of the definiton both ways, enable / disable.

Quoted 46 lines
Quoted 46 lines

---

This patch was briefly proposed in a related thread [1], where this discussion was taking place.
That probably got missed due to some confusion around the device_init_wakeup return value.

There is infact error returning being done in drivers/base/power/wakeup.c, and ideally we should be using that info as done in this patch.

If this patch get accepted, it might even bring forth few hidden bugs due to missing error handling, and it will also change the patch for devm_device_init_wakeup() helper slightly[2].

[1] https://lore.kernel.org/linux-pm/20241218064335.c72gmw56ogtp36a2@lcpd911/
[2] https://lore.kernel.org/linux-pm/[email protected]/

---
 include/linux/pm_wakeup.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/linux/pm_wakeup.h +5 -2
index d501c09c60cd..ed62a7055a54 100644
@@ -231,9 +231,13 @@ static inline void pm_wakeup_hard_event(struct device *dev)
  */
 static inline int device_init_wakeup(struct device *dev, bool enable)
 {
+       int err;
        if (enable) {
                device_set_wakeup_capable(dev, true);
-               return device_wakeup_enable(dev);
+               err = device_wakeup_enable(dev);
+               if (err)
+                       device_set_wakeup_capable(dev, false);
+               return err;
        }
        device_wakeup_disable(dev);
        device_set_wakeup_capable(dev, false);
--

2.34.1

Signature · 3 lines
Best regards,
Dhruva Gole
Texas Instruments Incorporated