Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 c8377adfa781 ("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: c8377adfa781 ("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>

authored by

Stephen Boyd and committed by
Rafael J. Wysocki
2ca3d1ec ae367b79

+34 -4
+9
drivers/base/power/power.h
··· 157 157 struct wakeup_source *ws); 158 158 extern void wakeup_source_sysfs_remove(struct wakeup_source *ws); 159 159 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 + 160 169 #endif /* CONFIG_PM_SLEEP */
+6
drivers/base/power/sysfs.c
··· 5 5 #include <linux/export.h> 6 6 #include <linux/pm_qos.h> 7 7 #include <linux/pm_runtime.h> 8 + #include <linux/pm_wakeup.h> 8 9 #include <linux/atomic.h> 9 10 #include <linux/jiffies.h> 10 11 #include "power.h" ··· 668 667 if (rc) 669 668 goto err_wakeup; 670 669 } 670 + rc = pm_wakeup_source_sysfs_add(dev); 671 + if (rc) 672 + goto err_latency; 671 673 return 0; 672 674 675 + err_latency: 676 + sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group); 673 677 err_wakeup: 674 678 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group); 675 679 err_runtime:
+6 -4
drivers/base/power/wakeup.c
··· 220 220 221 221 ws = wakeup_source_create(name); 222 222 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 + } 227 229 } 228 230 wakeup_source_add(ws); 229 231 }
+13
drivers/base/power/wakeup_stats.c
··· 185 185 EXPORT_SYMBOL_GPL(wakeup_source_sysfs_add); 186 186 187 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 + 200 + /** 188 201 * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs. 189 202 * @ws: Wakeup source to be removed from sysfs. 190 203 */