Skip to contentSkip to conversation

PM / wakeup: show wakeup sources stats in sysfs

PATCH Superseded Tri Vo 3 participants 12 messages Aug 5, 2019 – Aug 6, 2019

Subsystems (7)
VFS ACPI Driver core Suspend to RAM +3 more subsystems

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Tri Vo

author
#0 lore raw

v7 cover PM / wakeup: show wakeup sources stats in sysfs

Userspace can use wakeup_sources debugfs node to plot history of suspend blocking wakeup sources over device's boot cycle. This information can then be used (1) for power-specific bug reporting and (2) towards attributing battery consumption to specific processes over a period of time.

However, debugfs doesn't have stable ABI. For this reason, create a 'struct device' to expose wakeup sources statistics in sysfs under /sys/class/wakeup/wakeup<ID>/*.

Patch 1 and 2 do some cleanup to simplify our changes to how wakeup sources are created. Patch 3 implements wakeup sources stats in sysfs.

Tri Vo (3):
PM / wakeup: Drop wakeup_source_init(), wakeup_source_prepare() PM / wakeup: Use wakeup_source_register() in wakelock.c PM / wakeup: Show wakeup sources stats in sysfs

 Documentation/ABI/testing/sysfs-class-wakeup |  76 +++++++++
 drivers/acpi/device_pm.c                     |   3 +-
 drivers/base/power/Makefile                  |   2 +-
 drivers/base/power/power.h                   |   9 ++
 drivers/base/power/wakeup.c                  |  59 ++++---
 drivers/base/power/wakeup_stats.c            | 161 +++++++++++++++++++
 fs/eventpoll.c                               |   4 +-
 include/linux/pm_wakeup.h                    |  21 +--
 kernel/power/autosleep.c                     |   2 +-
 kernel/power/wakelock.c                      |  32 ++--
 kernel/time/alarmtimer.c                     |   2 +-
 11 files changed, 316 insertions(+), 55 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-wakeup
 create mode 100644 drivers/base/power/wakeup_stats.c

v2:
- Updated Documentation/ABI/, as per Greg.
- Removed locks in attribute functions, as per Greg.
- Lifetimes of struct wakelock and struck wakeup_source are now different due to the latter embedding a refcounted kobject. Changed it so that struct wakelock only has a pointer to struct wakeup_source, instead of embedding it.
- Added CONFIG_PM_SLEEP_STATS that enables/disables wakeup source statistics in sysfs.

v3:
Changes by Greg:
- Reworked code to use 'struct device' instead of raw kobjects.
- Updated documentation file.
- Only link wakeup_stats.o when CONFIG_PM_SLEEP_STATS is enabled. Changes by Tri:
- Reverted changes to kernel/power/wakelock.c. 'struct device' hides kobject operations. So no need to handle lifetimes in wakelock.c

v4:
- Added 'Co-developed-by:' and 'Tested-by:' fields to commit message.
- Moved new documentation to a separate file Documentation/ABI/testing/sysfs-class-wakeup, as per Greg.
- Fixed copyright header in drivers/base/power/wakeup_stats.c, as per Greg.

v5:
- Removed CONFIG_PM_SLEEP_STATS
- Used PTR_ERR_OR_ZERO instead of if(IS_ERR(...)) + PTR_ERR, reported by kbuild test robot <[email protected]>
- Stephen reported that a call to device_init_wakeup() and writing 'enabled' to that device's power/wakeup file results in multiple wakeup source being allocated for that device. Changed device_wakeup_enable() to check if device wakeup was previously enabled.
Changes by Stephen:
- Changed stats location from /sys/class/wakeup/<name>/* to /sys/class/wakeup/wakeup<ID>/*, where ID is an IDA-allocated integer. This avoids name collisions in /sys/class/wakeup/ directory.
- Added a "name" attribute to wakeup sources, and updated documentation.
- Device registering the wakeup source is now the parent of the wakeup source. Updated wakeup_source_register()'s signature and its callers accordingly.

v6:
- Changed stats location to /sys/class/wakeup/ws<ID>/*
- Replaced ida_simple_get()/ida_simple_remove() with ida_alloc()/ida_free() as the former is deprecated.
- Reverted changes to device_init_wakeup(). Rafael is preparing a patch to deal with extra wakeup source allocation in a separate patch.

v7:
- Removed wakeup_source_init(), wakeup_source_prepare().
- Removed duplicate wakeup source creation code from kernel/power/wakelock.
- Moved ID allocation to wakeup source object creation time.
- Changed stats location back to /sys/class/wakeup/wakeup<ID>/*
- Remove wakeup source device's "power" attributes.

--
2.22.0.770.g0f2c4a37fd-goog

author
#1 lore raw

PATCH v7 1/3 PM / wakeup: Drop wakeup_source_init(), wakeup_source_prepare()

wakeup_source_init() has no users. Remove it.

As a result, wakeup_source_prepare() is only called from wakeup_source_create(). Merge wakeup_source_prepare() into wakeup_source_create() and remove it.

---
 drivers/base/power/wakeup.c | 33 +++++++++++++--------------------
 include/linux/pm_wakeup.h   | 11 -----------
 2 files changed, 13 insertions(+), 31 deletions(-)
drivers/base/power/wakeup.c +13 -20
index ee31d4f8d856..3938892c8903 100644
@@ -72,23 +72,6 @@ static struct wakeup_source deleted_ws = {
 	.lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
 };
 
-/**
- * wakeup_source_prepare - Prepare a new wakeup source for initialization.
- * @ws: Wakeup source to prepare.
- * @name: Pointer to the name of the new wakeup source.
- *
- * Callers must ensure that the @name string won't be freed when @ws is still in
- * use.
- */
-void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
-{
-	if (ws) {
-		memset(ws, 0, sizeof(*ws));
-		ws->name = name;
-	}
-}
-EXPORT_SYMBOL_GPL(wakeup_source_prepare);
-
 /**
  * wakeup_source_create - Create a struct wakeup_source object.
  * @name: Name of the new wakeup source.
@@ -96,13 +79,23 @@ EXPORT_SYMBOL_GPL(wakeup_source_prepare);
 struct wakeup_source *wakeup_source_create(const char *name)
 {
 	struct wakeup_source *ws;
+	const char *ws_name;
 
-	ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+	ws = kzalloc(sizeof(*ws), GFP_KERNEL);
 	if (!ws)
-		return NULL;
+		goto err_ws;
+
+	ws_name = kstrdup_const(name, GFP_KERNEL);
+	if (!ws_name)
+		goto err_name;
+	ws->name = ws_name;
 
-	wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
 	return ws;
+
+err_name:
+	kfree(ws);
+err_ws:
+	return NULL;
 }
 EXPORT_SYMBOL_GPL(wakeup_source_create);
 
include/linux/pm_wakeup.h +0 -11
index 91027602d137..c0cad2d8f800 100644
@@ -81,7 +81,6 @@ static inline void device_set_wakeup_path(struct device *dev)
 }
 
 /* drivers/base/power/wakeup.c */
-extern void wakeup_source_prepare(struct wakeup_source *ws, const char *name);
 extern struct wakeup_source *wakeup_source_create(const char *name);
 extern void wakeup_source_destroy(struct wakeup_source *ws);
 extern void wakeup_source_add(struct wakeup_source *ws);
@@ -112,9 +111,6 @@ static inline bool device_can_wakeup(struct device *dev)
 	return dev->power.can_wakeup;
 }
 
-static inline void wakeup_source_prepare(struct wakeup_source *ws,
-					 const char *name) {}
-
 static inline struct wakeup_source *wakeup_source_create(const char *name)
 {
 	return NULL;
@@ -181,13 +177,6 @@ static inline void pm_wakeup_dev_event(struct device *dev, unsigned int msec,
 
 #endif /* !CONFIG_PM_SLEEP */
 
-static inline void wakeup_source_init(struct wakeup_source *ws,
-				      const char *name)
-{
-	wakeup_source_prepare(ws, name);
-	wakeup_source_add(ws);
-}
-
 static inline void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
 {
 	return pm_wakeup_ws_event(ws, msec, false);
Signature · 1 line
2.22.0.770.g0f2c4a37fd-goog
#2 lore raw

Re: [PATCH v7 1/3] PM / wakeup: Drop wakeup_source_init(), wakeup_source_prepare()

Quoting Tri Vo (2019-08-05 10:58:46)

Quoted 32 lines
drivers/base/power/wakeup.c +0 -17
index ee31d4f8d856..3938892c8903 100644
@@ -72,23 +72,6 @@ static struct wakeup_source deleted_ws = {
        .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
 };
 
-/**
- * wakeup_source_prepare - Prepare a new wakeup source for initialization.
- * @ws: Wakeup source to prepare.
- * @name: Pointer to the name of the new wakeup source.
- *
- * Callers must ensure that the @name string won't be freed when @ws is still in
- * use.
- */
-void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
-{
-       if (ws) {
-               memset(ws, 0, sizeof(*ws));
-               ws->name = name;
-       }
-}
-EXPORT_SYMBOL_GPL(wakeup_source_prepare);
-
 /**
  * wakeup_source_create - Create a struct wakeup_source object.
  * @name: Name of the new wakeup source.
@@ -96,13 +79,23 @@ EXPORT_SYMBOL_GPL(wakeup_source_prepare);
 struct wakeup_source *wakeup_source_create(const char *name)
 {
        struct wakeup_source *ws;

drivers/base/power/wakeup.c EXPORT_SYMBOL_GPL(wakeup_source_prepare); · line 89

Quoted 10 lines
drivers/base/power/wakeup.c +6 -2
+       const char *ws_name;
 
-       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+       ws = kzalloc(sizeof(*ws), GFP_KERNEL);
        if (!ws)
-               return NULL;
+               goto err_ws;
+
+       ws_name = kstrdup_const(name, GFP_KERNEL);
+       if (!ws_name)

Does this intentionally change this function to return an error if 'name' is NULL? Before, wakeup_source_prepare() would just assign ws->name to NULL, but now it errors out. I don't see how it's good or useful to allow NULL for the wakeup source name, but it is what it is.

Quoted 13 lines
drivers/base/power/wakeup.c +7 -1
+               goto err_name;
+       ws->name = ws_name;
 
-       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
        return ws;
+
+err_name:
+       kfree(ws);
+err_ws:
+       return NULL;
 }
 EXPORT_SYMBOL_GPL(wakeup_source_create);
 
author
#3 lore raw

Re: [PATCH v7 1/3] PM / wakeup: Drop wakeup_source_init(), wakeup_source_prepare()

Quoted 36 lines · Stephen Boyd

Quoting Tri Vo (2019-08-05 10:58:46)

Quoted 34 lines
drivers/base/power/wakeup.c +1 -17
index ee31d4f8d856..3938892c8903 100644
@@ -72,23 +72,6 @@ static struct wakeup_source deleted_ws = {
        .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
 };
 
-/**
- * wakeup_source_prepare - Prepare a new wakeup source for initialization.
- * @ws: Wakeup source to prepare.
- * @name: Pointer to the name of the new wakeup source.
- *
- * Callers must ensure that the @name string won't be freed when @ws is still in
- * use.
- */
-void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
-{
-       if (ws) {
-               memset(ws, 0, sizeof(*ws));
-               ws->name = name;
-       }
-}
-EXPORT_SYMBOL_GPL(wakeup_source_prepare);
-
 /**
  * wakeup_source_create - Create a struct wakeup_source object.
  * @name: Name of the new wakeup source.
@@ -96,13 +79,23 @@ EXPORT_SYMBOL_GPL(wakeup_source_prepare);
 struct wakeup_source *wakeup_source_create(const char *name)
 {
        struct wakeup_source *ws;
+       const char *ws_name;
 
Quoted 13 lines

drivers/base/power/wakeup.c EXPORT_SYMBOL_GPL(wakeup_source_prepare); · line 89

Quoted 8 lines
drivers/base/power/wakeup.c +5 -2
-       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+       ws = kzalloc(sizeof(*ws), GFP_KERNEL);
        if (!ws)
-               return NULL;
+               goto err_ws;
+
+       ws_name = kstrdup_const(name, GFP_KERNEL);
+       if (!ws_name)

Does this intentionally change this function to return an error if 'name' is NULL? Before, wakeup_source_prepare() would just assign ws->name to NULL, but now it errors out. I don't see how it's good or useful to allow NULL for the wakeup source name, but it is what it is.

Yes, the change to not allow ws->name to be NULL is intentional.

#4 lore raw

Re: [PATCH v7 1/3] PM / wakeup: Drop wakeup_source_init(), wakeup_source_prepare()

Quoting Tri Vo (2019-08-05 14:11:55)

Quoted 23 lines · Stephen Boyd
Quoted 8 lines

Quoting Tri Vo (2019-08-05 10:58:46)

Quoted 6 lines
drivers/base/power/wakeup.c +1 -0
@@ -96,13 +79,23 @@ EXPORT_SYMBOL_GPL(wakeup_source_prepare);
 struct wakeup_source *wakeup_source_create(const char *name)
 {
        struct wakeup_source *ws;
+       const char *ws_name;
 
Quoted 13 lines

drivers/base/power/wakeup.c EXPORT_SYMBOL_GPL(wakeup_source_prepare); · line 89

Quoted 8 lines
drivers/base/power/wakeup.c +5 -2
-       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+       ws = kzalloc(sizeof(*ws), GFP_KERNEL);
        if (!ws)
-               return NULL;
+               goto err_ws;
+
+       ws_name = kstrdup_const(name, GFP_KERNEL);
+       if (!ws_name)

Does this intentionally change this function to return an error if 'name' is NULL? Before, wakeup_source_prepare() would just assign ws->name to NULL, but now it errors out. I don't see how it's good or useful to allow NULL for the wakeup source name, but it is what it is.

Quoted 1 line

Yes, the change to not allow ws->name to be NULL is intentional.

Ok. It would be good to mention it in the commit text so we don't think it was a bug when looking back a few months later.

author
#5 lore raw

PATCH v7 2/3 PM / wakeup: Use wakeup_source_register() in wakelock.c

In reply to Tri Vo

kernel/power/wakelock.c duplicates wakeup source creation and registration code from drivers/base/power/wakeup.c.

Change struct wakelock's wakeup source to a pointer and use wakeup_source_register() function to create and register said wakeup source. Use wakeup_source_unregister() on cleanup path.

---
 kernel/power/wakelock.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
kernel/power/wakelock.c +19 -13
index 4210152e56f0..d1eb7fd98b64 100644
@@ -27,7 +27,7 @@ static DEFINE_MUTEX(wakelocks_lock);
 struct wakelock {
 	char			*name;
 	struct rb_node		node;
-	struct wakeup_source	ws;
+	struct wakeup_source	*ws;
 #ifdef CONFIG_PM_WAKELOCKS_GC
 	struct list_head	lru;
 #endif
@@ -46,7 +46,7 @@ ssize_t pm_show_wakelocks(char *buf, bool show_active)
 
 	for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
 		wl = rb_entry(node, struct wakelock, node);
-		if (wl->ws.active == show_active)
+		if (wl->ws->active == show_active)
 			str += scnprintf(str, end - str, "%s ", wl->name);
 	}
 	if (str > buf)
@@ -112,16 +112,16 @@ static void __wakelocks_gc(struct work_struct *work)
 		u64 idle_time_ns;
 		bool active;
 
-		spin_lock_irq(&wl->ws.lock);
-		idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
-		active = wl->ws.active;
-		spin_unlock_irq(&wl->ws.lock);
+		spin_lock_irq(&wl->ws->lock);
+		idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws->last_time));
+		active = wl->ws->active;
+		spin_unlock_irq(&wl->ws->lock);
 
 		if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
 			break;
 
 		if (!active) {
-			wakeup_source_remove(&wl->ws);
+			wakeup_source_unregister(wl->ws);
 			rb_erase(&wl->node, &wakelocks_tree);
 			list_del(&wl->lru);
 			kfree(wl->name);
@@ -187,9 +187,15 @@ static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
 		kfree(wl);
 		return ERR_PTR(-ENOMEM);
 	}
-	wl->ws.name = wl->name;
-	wl->ws.last_time = ktime_get();
-	wakeup_source_add(&wl->ws);
+
+	wl->ws = wakeup_source_register(wl->name);
+	if (!wl->ws) {
+		kfree(wl->name);
+		kfree(wl);
+		return ERR_PTR(-ENOMEM);
+	}
+	wl->ws->last_time = ktime_get();
+
 	rb_link_node(&wl->node, parent, node);
 	rb_insert_color(&wl->node, &wakelocks_tree);
 	wakelocks_lru_add(wl);
@@ -233,9 +239,9 @@ int pm_wake_lock(const char *buf)
 		u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
 
 		do_div(timeout_ms, NSEC_PER_MSEC);
-		__pm_wakeup_event(&wl->ws, timeout_ms);
+		__pm_wakeup_event(wl->ws, timeout_ms);
 	} else {
-		__pm_stay_awake(&wl->ws);
+		__pm_stay_awake(wl->ws);
 	}
 
 	wakelocks_lru_most_recent(wl);
@@ -271,7 +277,7 @@ int pm_wake_unlock(const char *buf)
 		ret = PTR_ERR(wl);
 		goto out;
 	}
-	__pm_relax(&wl->ws);
+	__pm_relax(wl->ws);
 
 	wakelocks_lru_most_recent(wl);
 	wakelocks_gc();
Signature · 1 line
2.22.0.770.g0f2c4a37fd-goog
Reviewed-by on v7 2/3
#6 lore raw

Re: [PATCH v7 2/3] PM / wakeup: Use wakeup_source_register() in wakelock.c

Quoting Tri Vo (2019-08-05 10:58:47)

Quoted 9 lines

kernel/power/wakelock.c duplicates wakeup source creation and registration code from drivers/base/power/wakeup.c.

Change struct wakelock's wakeup source to a pointer and use wakeup_source_register() function to create and register said wakeup source. Use wakeup_source_unregister() on cleanup path.

---

author
#7 lore raw

PATCH v7 3/3 PM / wakeup: Show wakeup sources stats in sysfs

In reply to Tri Vo

Add an ID and a device pointer to 'struct wakeup_source'. Use them to to expose wakeup sources statistics in sysfs under /sys/class/wakeup/wakeup<ID>/*.

---
 Documentation/ABI/testing/sysfs-class-wakeup |  76 +++++++++
 drivers/acpi/device_pm.c                     |   3 +-
 drivers/base/power/Makefile                  |   2 +-
 drivers/base/power/power.h                   |   9 ++
 drivers/base/power/wakeup.c                  |  28 +++-
 drivers/base/power/wakeup_stats.c            | 161 +++++++++++++++++++
 fs/eventpoll.c                               |   4 +-
 include/linux/pm_wakeup.h                    |  10 +-
 kernel/power/autosleep.c                     |   2 +-
 kernel/power/wakelock.c                      |   2 +-
 kernel/time/alarmtimer.c                     |   2 +-
 11 files changed, 286 insertions(+), 13 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-wakeup
 create mode 100644 drivers/base/power/wakeup_stats.c
Documentation/ABI/testing/sysfs-class-wakeup new file +76 -0
new file mode 100644
index 000000000000..754aab8b6dcd
@@ -0,0 +1,76 @@
+What:		/sys/class/wakeup/
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		The /sys/class/wakeup/ directory contains pointers to all
+		wakeup sources in the kernel at that moment in time.
+
+What:		/sys/class/wakeup/.../name
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the name of the wakeup source.
+
+What:		/sys/class/wakeup/.../active_count
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the number of times the wakeup source was
+		activated.
+
+What:		/sys/class/wakeup/.../event_count
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the number of signaled wakeup events
+		associated with the wakeup source.
+
+What:		/sys/class/wakeup/.../wakeup_count
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the number of times the wakeup source might
+		abort suspend.
+
+What:		/sys/class/wakeup/.../expire_count
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the number of times the wakeup source's
+		timeout has expired.
+
+What:		/sys/class/wakeup/.../active_time_ms
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the amount of time the wakeup source has
+		been continuously active, in milliseconds.  If the wakeup
+		source is not active, this file contains '0'.
+
+What:		/sys/class/wakeup/.../total_time_ms
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the total amount of time this wakeup source
+		has been active, in milliseconds.
+
+What:		/sys/class/wakeup/.../max_time_ms
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the maximum amount of time this wakeup
+		source has been continuously active, in milliseconds.
+
+What:		/sys/class/wakeup/.../last_change_ms
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		This file contains the monotonic clock time when the wakeup
+		source was touched last time, in milliseconds.
+
+What:		/sys/class/wakeup/.../prevent_suspend_time_ms
+Date:		June 2019
+Contact:	Tri Vo <[email protected]>
+Description:
+		The file contains the total amount of time this wakeup source
+		has been preventing autosleep, in milliseconds.
drivers/acpi/device_pm.c +2 -1
index 28cffaaf9d82..91634e2dba8a 100644
@@ -495,7 +495,8 @@ acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
 		goto out;
 
 	mutex_lock(&acpi_pm_notifier_lock);
-	adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
+	adev->wakeup.ws = wakeup_source_register(&adev->dev,
+						 dev_name(&adev->dev));
 	adev->wakeup.context.dev = dev;
 	adev->wakeup.context.func = func;
 	adev->wakeup.flags.notifier_present = true;
drivers/base/power/Makefile +1 -1
index e1bb691cf8f1..ec5bb190b9d0 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_PM)	+= sysfs.o generic_ops.o common.o qos.o runtime.o wakeirq.o
-obj-$(CONFIG_PM_SLEEP)	+= main.o wakeup.o
+obj-$(CONFIG_PM_SLEEP)	+= main.o wakeup.o wakeup_stats.o
 obj-$(CONFIG_PM_TRACE_RTC)	+= trace.o
 obj-$(CONFIG_PM_GENERIC_DOMAINS)	+=  domain.o domain_governor.o
 obj-$(CONFIG_HAVE_CLK)	+= clock_ops.o
drivers/base/power/power.h +9 -0
index ec33fbdb919b..57b1d1d88c8e 100644
@@ -149,3 +149,12 @@ static inline void device_pm_init(struct device *dev)
 	device_pm_sleep_init(dev);
 	pm_runtime_init(dev);
 }
+
+#ifdef CONFIG_PM_SLEEP
+
+/* drivers/base/power/wakeup_stats.c */
+extern int wakeup_source_sysfs_add(struct device *parent,
+				   struct wakeup_source *ws);
+extern void wakeup_source_sysfs_remove(struct wakeup_source *ws);
+
+#endif /* CONFIG_PM_SLEEP */
drivers/base/power/wakeup.c +24 -4
index 3938892c8903..973675f24314 100644
@@ -72,6 +72,8 @@ static struct wakeup_source deleted_ws = {
 	.lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
 };
 
+static DEFINE_IDA(wakeup_ida);
+
 /**
  * wakeup_source_create - Create a struct wakeup_source object.
  * @name: Name of the new wakeup source.
@@ -80,6 +82,7 @@ struct wakeup_source *wakeup_source_create(const char *name)
 {
 	struct wakeup_source *ws;
 	const char *ws_name;
+	int id;
 
 	ws = kzalloc(sizeof(*ws), GFP_KERNEL);
 	if (!ws)
@@ -90,8 +93,15 @@ struct wakeup_source *wakeup_source_create(const char *name)
 		goto err_name;
 	ws->name = ws_name;
 
+	id = ida_alloc(&wakeup_ida, GFP_KERNEL);
+	if (id < 0)
+		goto err_id;
+	ws->id = id;
+
 	return ws;
 
+err_id:
+	kfree_const(ws->name);
 err_name:
 	kfree(ws);
 err_ws:
@@ -140,6 +150,7 @@ void wakeup_source_destroy(struct wakeup_source *ws)
 
 	__pm_relax(ws);
 	wakeup_source_record(ws);
+	ida_free(&wakeup_ida, ws->id);
 	kfree_const(ws->name);
 	kfree(ws);
 }
@@ -193,16 +204,24 @@ EXPORT_SYMBOL_GPL(wakeup_source_remove);
 
 /**
  * wakeup_source_register - Create wakeup source and add it to the list.
+ * @dev: Device this wakeup source is associated with (or NULL if virtual).
  * @name: Name of the wakeup source to register.
  */
-struct wakeup_source *wakeup_source_register(const char *name)
+struct wakeup_source *wakeup_source_register(struct device *dev,
+					     const char *name)
 {
 	struct wakeup_source *ws;
+	int ret;
 
 	ws = wakeup_source_create(name);
-	if (ws)
+	if (ws) {
+		ret = wakeup_source_sysfs_add(dev, ws);
+		if (ret) {
+			wakeup_source_destroy(ws);
+			return NULL;
+		}
 		wakeup_source_add(ws);
-
+	}
 	return ws;
 }
 EXPORT_SYMBOL_GPL(wakeup_source_register);
@@ -215,6 +234,7 @@ void wakeup_source_unregister(struct wakeup_source *ws)
 {
 	if (ws) {
 		wakeup_source_remove(ws);
+		wakeup_source_sysfs_remove(ws);
 		wakeup_source_destroy(ws);
 	}
 }
@@ -258,7 +278,7 @@ int device_wakeup_enable(struct device *dev)
 	if (pm_suspend_target_state != PM_SUSPEND_ON)
 		dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
 
-	ws = wakeup_source_register(dev_name(dev));
+	ws = wakeup_source_register(dev, dev_name(dev));
 	if (!ws)
 		return -ENOMEM;
 
drivers/base/power/wakeup_stats.c new file +161 -0

161 lines not on this page · raw message

fs/eventpoll.c +2 -2
index d7f1f5011fac..c4159bcc05d9 100644
@@ -1459,13 +1459,13 @@ static int ep_create_wakeup_source(struct epitem *epi)
 	struct wakeup_source *ws;
 
 	if (!epi->ep->ws) {
-		epi->ep->ws = wakeup_source_register("eventpoll");
+		epi->ep->ws = wakeup_source_register(NULL, "eventpoll");
 		if (!epi->ep->ws)
 			return -ENOMEM;
 	}
 
 	name = epi->ffd.file->f_path.dentry->d_name.name;
-	ws = wakeup_source_register(name);
+	ws = wakeup_source_register(NULL, name);
 
 	if (!ws)
 		return -ENOMEM;
include/linux/pm_wakeup.h +8 -2
index c0cad2d8f800..661efa029c96 100644
@@ -21,6 +21,7 @@ struct wake_irq;
  * struct wakeup_source - Representation of wakeup sources
  *
  * @name: Name of the wakeup source
+ * @id: Wakeup source id
  * @entry: Wakeup source list entry
  * @lock: Wakeup source lock
  * @wakeirq: Optional device specific wakeirq
@@ -35,11 +36,13 @@ struct wake_irq;
  * @relax_count: Number of times the wakeup source was deactivated.
  * @expire_count: Number of times the wakeup source's timeout has expired.
  * @wakeup_count: Number of times the wakeup source might abort suspend.
+ * @dev: Struct device for sysfs statistics about the wakeup source.
  * @active: Status of the wakeup source.
  * @autosleep_enabled: Autosleep is active, so update @prevent_sleep_time.
  */
 struct wakeup_source {
 	const char 		*name;
+	int			id;
 	struct list_head	entry;
 	spinlock_t		lock;
 	struct wake_irq		*wakeirq;
@@ -55,6 +58,7 @@ struct wakeup_source {
 	unsigned long		relax_count;
 	unsigned long		expire_count;
 	unsigned long		wakeup_count;
+	struct device		*dev;
 	bool			active:1;
 	bool			autosleep_enabled:1;
 };
@@ -85,7 +89,8 @@ extern struct wakeup_source *wakeup_source_create(const char *name);
 extern void wakeup_source_destroy(struct wakeup_source *ws);
 extern void wakeup_source_add(struct wakeup_source *ws);
 extern void wakeup_source_remove(struct wakeup_source *ws);
-extern struct wakeup_source *wakeup_source_register(const char *name);
+extern struct wakeup_source *wakeup_source_register(struct device *dev,
+						    const char *name);
 extern void wakeup_source_unregister(struct wakeup_source *ws);
 extern int device_wakeup_enable(struct device *dev);
 extern int device_wakeup_disable(struct device *dev);
@@ -122,7 +127,8 @@ static inline void wakeup_source_add(struct wakeup_source *ws) {}
 
 static inline void wakeup_source_remove(struct wakeup_source *ws) {}
 
-static inline struct wakeup_source *wakeup_source_register(const char *name)
+static inline struct wakeup_source *wakeup_source_register(struct device *dev,
+							   const char *name)
 {
 	return NULL;
 }
kernel/power/autosleep.c +1 -1
index 41e83a779e19..9af5a50d3489 100644
@@ -116,7 +116,7 @@ int pm_autosleep_set_state(suspend_state_t state)
 
 int __init pm_autosleep_init(void)
 {
-	autosleep_ws = wakeup_source_register("autosleep");
+	autosleep_ws = wakeup_source_register(NULL, "autosleep");
 	if (!autosleep_ws)
 		return -ENOMEM;
 
kernel/power/wakelock.c +1 -1
index d1eb7fd98b64..105df4dfc783 100644
@@ -188,7 +188,7 @@ static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
 		return ERR_PTR(-ENOMEM);
 	}
 
-	wl->ws = wakeup_source_register(wl->name);
+	wl->ws = wakeup_source_register(NULL, wl->name);
 	if (!wl->ws) {
 		kfree(wl->name);
 		kfree(wl);
kernel/time/alarmtimer.c +1 -1
index 57518efc3810..93b382d9701c 100644
@@ -97,7 +97,7 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	if (!device_may_wakeup(rtc->dev.parent))
 		return -1;
 
-	__ws = wakeup_source_register("alarmtimer");
+	__ws = wakeup_source_register(dev, "alarmtimer");
 
 	spin_lock_irqsave(&rtcdev_lock, flags);
 	if (!rtcdev) {
Signature · 1 line
2.22.0.770.g0f2c4a37fd-goog
#8 lore raw

Re: [PATCH v7 3/3] PM / wakeup: Show wakeup sources stats in sysfs

Quoting Tri Vo (2019-08-05 10:58:48)

Quoted 9 lines
drivers/base/power/wakeup_stats.c new file +3 -0
new file mode 100644
index 000000000000..3a4f55028e27
@@ -0,0 +1,161 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Wakeup statistics in sysfs

drivers/base/power/wakeup_stats.c line 13

Quoted 10 lines
drivers/base/power/wakeup_stats.c new file +10 -0
+ *
+ * Copyright (c) 2019 Linux Foundation
+ * Copyright (c) 2019 Greg Kroah-Hartman <[email protected]>
+ * Copyright (c) 2019 Google Inc.
+ */
+
+#include <linux/idr.h>
+#include <linux/kdev_t.h>
+#include <linux/pm_runtime.h>
+#include <linux/slab.h>

Can you include init.h, device.h, timekeeping.h, and kernel. here? This file calls functions exported in those headers.

Quoted 18 lines
drivers/base/power/wakeup_stats.c new file +18 -0
+
+#include "power.h"
+
+static struct class *wakeup_class;
+
+#define wakeup_attr(_name)                                             \
+static ssize_t _name##_show(struct device *dev,                                \
+                           struct device_attribute *attr, char *buf)   \
+{                                                                      \
+       struct wakeup_source *ws = dev_get_drvdata(dev);                \
+                                                                       \
+       return sprintf(buf, "%lu\n", ws->_name);                        \
+}                                                                      \
+static DEVICE_ATTR_RO(_name)
+
+wakeup_attr(active_count);
+wakeup_attr(event_count);
+wakeup_attr(wakeup_count);
#9 lore raw

Re: [PATCH v7 3/3] PM / wakeup: Show wakeup sources stats in sysfs

In reply to Tri Vo

Quoting Tri Vo (2019-08-05 10:58:48)

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +0 -0
new file mode 100644
index 000000000000..3a4f55028e27
@@ -0,0 +1,161 @@

[...]

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +6 -0
+/**
+ * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
+ * @parent: Device given wakeup source is associated with (or NULL if virtual).
+ * @ws: Wakeup source to be added in sysfs.
+ */
+int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)

drivers/base/power/wakeup_stats.c line 139

Quoted 10 lines
drivers/base/power/wakeup_stats.c new file +10 -0
+{
+       struct device *dev;
+
+       dev = device_create_with_groups(wakeup_class, parent, MKDEV(0, 0), ws,
+                                       wakeup_source_groups, "wakeup%d",
+                                       ws->id);
+       if (IS_ERR(dev))
+               return PTR_ERR(dev);
+       ws->dev = dev;
+       pm_runtime_no_callbacks(ws->dev);

Does this only avoid adding runtime PM attributes?

I thought we would call device_set_pm_not_required() on the device here. Probably requiring a bit of copy/paste from device_create_with_groups() so that it can be set before the device is registered. Or another version of device_create_with_groups() that does everything besides call device_add().

Quoted 5 lines
drivers/base/power/wakeup_stats.c new file +5 -0
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(wakeup_source_sysfs_add);
+
author
#10 lore raw

Re: [PATCH v7 3/3] PM / wakeup: Show wakeup sources stats in sysfs

Quoted 28 lines · Stephen Boyd

Quoting Tri Vo (2019-08-05 10:58:48)

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +0 -0
new file mode 100644
index 000000000000..3a4f55028e27
@@ -0,0 +1,161 @@

[...]

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +6 -0
+/**
+ * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
+ * @parent: Device given wakeup source is associated with (or NULL if virtual).
+ * @ws: Wakeup source to be added in sysfs.
+ */
+int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)

drivers/base/power/wakeup_stats.c line 139

Quoted 10 lines
drivers/base/power/wakeup_stats.c new file +10 -0
+{
+       struct device *dev;
+
+       dev = device_create_with_groups(wakeup_class, parent, MKDEV(0, 0), ws,
+                                       wakeup_source_groups, "wakeup%d",
+                                       ws->id);
+       if (IS_ERR(dev))
+               return PTR_ERR(dev);
+       ws->dev = dev;
+       pm_runtime_no_callbacks(ws->dev);

Does this only avoid adding runtime PM attributes?

Quoted 5 lines

I thought we would call device_set_pm_not_required() on the device here. Probably requiring a bit of copy/paste from device_create_with_groups() so that it can be set before the device is registered. Or another version of device_create_with_groups() that does everything besides call device_add().

Comments on pm_runtime_no_callbacks() say,
"Set the power.no_callbacks flag, which tells the PM core that this
device is power-managed through its parent and has no runtime PM callbacks of its own. The runtime sysfs attributes will be removed."

Sound like it's appropriate to apply this function to the wakeup source.

#11 lore raw

Re: [PATCH v7 3/3] PM / wakeup: Show wakeup sources stats in sysfs

Quoted 36 lines · Tri Vo
Quoted 28 lines · Stephen Boyd

Quoting Tri Vo (2019-08-05 10:58:48)

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +0 -0
new file mode 100644
index 000000000000..3a4f55028e27
@@ -0,0 +1,161 @@

[...]

Quoted 6 lines
drivers/base/power/wakeup_stats.c new file +6 -0
+/**
+ * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
+ * @parent: Device given wakeup source is associated with (or NULL if virtual).
+ * @ws: Wakeup source to be added in sysfs.
+ */
+int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)

drivers/base/power/wakeup_stats.c line 139

Quoted 10 lines
drivers/base/power/wakeup_stats.c new file +10 -0
+{
+       struct device *dev;
+
+       dev = device_create_with_groups(wakeup_class, parent, MKDEV(0, 0), ws,
+                                       wakeup_source_groups, "wakeup%d",
+                                       ws->id);
+       if (IS_ERR(dev))
+               return PTR_ERR(dev);
+       ws->dev = dev;
+       pm_runtime_no_callbacks(ws->dev);

Does this only avoid adding runtime PM attributes?

Quoted 5 lines

I thought we would call device_set_pm_not_required() on the device here. Probably requiring a bit of copy/paste from device_create_with_groups() so that it can be set before the device is registered. Or another version of device_create_with_groups() that does everything besides call device_add().

Quoted 6 lines

Comments on pm_runtime_no_callbacks() say,
"Set the power.no_callbacks flag, which tells the PM core that this
device is power-managed through its parent and has no runtime PM callbacks of its own. The runtime sysfs attributes will be removed."

Sound like it's appropriate to apply this function to the wakeup source.

This is only useful if you ever enable PM-runtime for this device. Which you won't do.

You could use device_set_pm_not_required(), though.