Skip to content

Commit 2ca3d1e

Browse files
bebarinorafaeljw
authored andcommitted
PM / wakeup: Register wakeup class kobj after device is added
The device_set_wakeup_enable() function can be called on a device that hasn't been registered with device_add() yet. This allows the device to be in a state where wakeup is enabled for it but the device isn't published to userspace in sysfs yet. After commit c8377ad ("PM / wakeup: Show wakeup sources stats in sysfs"), calling device_set_wakeup_enable() will fail for a device that hasn't been registered with the driver core via device_add(). This is because we try to create sysfs entries for the device and associate a wakeup class kobject with it before the device has been registered. Let's follow a similar approach that device_set_wakeup_capable() takes here and register the wakeup class either from device_set_wakeup_enable() when the device is already registered, or from dpm_sysfs_add() when the device is being registered with the driver core via device_add(). Fixes: c8377ad ("PM / wakeup: Show wakeup sources stats in sysfs") Reported-by: Qian Cai <cai@lca.pw> Reviewed-by: Tri Vo <trong@android.com> Signed-off-by: Stephen Boyd <swboyd@chromium.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent ae367b7 commit 2ca3d1e

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎drivers/base/power/power.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,13 @@ extern int wakeup_source_sysfs_add(struct device *parent,
157157
struct wakeup_source *ws);
158158
extern void wakeup_source_sysfs_remove(struct wakeup_source *ws);
159159

160+
extern int pm_wakeup_source_sysfs_add(struct device *parent);
161+
162+
#else /* !CONFIG_PM_SLEEP */
163+
164+
static inline int pm_wakeup_source_sysfs_add(struct device *parent)
165+
{
166+
return 0;
167+
}
168+
160169
#endif /* CONFIG_PM_SLEEP */

‎drivers/base/power/sysfs.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/export.h>
66
#include <linux/pm_qos.h>
77
#include <linux/pm_runtime.h>
8+
#include <linux/pm_wakeup.h>
89
#include <linux/atomic.h>
910
#include <linux/jiffies.h>
1011
#include "power.h"
@@ -667,8 +668,13 @@ int dpm_sysfs_add(struct device *dev)
667668
if (rc)
668669
goto err_wakeup;
669670
}
671+
rc = pm_wakeup_source_sysfs_add(dev);
672+
if (rc)
673+
goto err_latency;
670674
return 0;
671675

676+
err_latency:
677+
sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
672678
err_wakeup:
673679
sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
674680
err_runtime:

‎drivers/base/power/wakeup.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ struct wakeup_source *wakeup_source_register(struct device *dev,
220220

221221
ws = wakeup_source_create(name);
222222
if (ws) {
223-
ret = wakeup_source_sysfs_add(dev, ws);
224-
if (ret) {
225-
wakeup_source_free(ws);
226-
return NULL;
223+
if (!dev || device_is_registered(dev)) {
224+
ret = wakeup_source_sysfs_add(dev, ws);
225+
if (ret) {
226+
wakeup_source_free(ws);
227+
return NULL;
228+
}
227229
}
228230
wakeup_source_add(ws);
229231
}

‎drivers/base/power/wakeup_stats.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
184184
}
185185
EXPORT_SYMBOL_GPL(wakeup_source_sysfs_add);
186186

187+
/**
188+
* pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
189+
* for a device if they're missing.
190+
* @parent: Device given wakeup source is associated with
191+
*/
192+
int pm_wakeup_source_sysfs_add(struct device *parent)
193+
{
194+
if (!parent->power.wakeup || parent->power.wakeup->dev)
195+
return 0;
196+
197+
return wakeup_source_sysfs_add(parent, parent->power.wakeup);
198+
}
199+
187200
/**
188201
* wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
189202
* @ws: Wakeup source to be removed from sysfs.

0 commit comments

Comments
 (0)