Support BPF traversal of wakeup sources
Subsystems (7)
v3 cover Support BPF traversal of wakeup sources
This patchset adds requisite kfuncs for BPF programs to safely traverse wakeup_sources, and puts a config flag around the sysfs interface.
Currently, a traversal of wakeup sources require going through /sys/class/wakeup/* or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds of wakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.
Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.
On a quiescent Pixel 6 traversing 150 wakeup_sources, I am seeing ~34x speedup (sampled 75 times in table below). For a device under load, the speedup is greater.
+-------+----+----------+----------+ | | n | AVG (ms) | STD (ms) | +-------+----+----------+----------+ | sysfs | 75 | 44.9 | 12.6 | +-------+----+----------+----------+ | BPF | 75 | 1.3 | 0.7 | +-------+----+----------+----------+
The initial attempts for BPF traversal of wakeup_sources was with BPF iterators [1]. However, BPF already allows for traversing of a simple list with bpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.
[1]: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Changed return type of bpf_wakeup_sources_get_head() to void * per Alexei
- Added failure test for direct dereference of wakeup source head
- Use bpf_core_cast() instead of macros in BPF program per Kumar
- v2 link: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Dropped CONFIG_PM_WAKEUP_STATS_SYSFS patch for future patchset
- Added declarations for kfuncs to .h to fix sparse and checkpatch warnings
- Added kfunc to get address of wakeup_source's head
- Added example bpf prog selftest for traversal of wakeup sources per Kumar
- Added *_fail.c selftest per Kumar
- More concise commit message in patch 1/2
- v1 link: https://lore.kernel.org/all/[email protected]/
Samuel Wu (2):
PM: wakeup: Add kfuncs to traverse over wakeup_sources selftests/bpf: Add tests for wakeup_sources kfuncs
drivers/base/power/power.h | 7 ++ drivers/base/power/wakeup.c | 72 ++++++++++++- tools/testing/selftests/bpf/config | 3 +- .../selftests/bpf/prog_tests/wakeup_source.c | 101 ++++++++++++++++++ .../selftests/bpf/progs/test_wakeup_source.c | 92 ++++++++++++++++ .../selftests/bpf/progs/wakeup_source.h | 22 ++++ .../selftests/bpf/progs/wakeup_source_fail.c | 76 +++++++++++++ 7 files changed, 370 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/wakeup_source.c create mode 100644 tools/testing/selftests/bpf/progs/test_wakeup_source.c create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source.h create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source_fail.c
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v3 1/2 PM: wakeup: Add kfuncs to traverse over wakeup_sources
Iterating through wakeup sources via sysfs or debugfs can be inefficient or restricted. Introduce BPF kfuncs to allow high-performance and safe in-kernel traversal of the wakeup_sources list.
The new kfuncs include:
- bpf_wakeup_sources_get_head() to obtain the list head.
- bpf_wakeup_sources_read_lock/unlock() to manage the SRCU lock.
For verifier safety, the underlying SRCU index is wrapped in an opaque 'struct bpf_ws_lock' pointer. This enables the use of KF_ACQUIRE and KF_RELEASE flags, allowing the BPF verifier to strictly enforce paired lock/unlock cycles and prevent resource leaks.
--- drivers/base/power/power.h | 7 ++++ drivers/base/power/wakeup.c | 72 +++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-)
drivers/base/power/power.h +7 -0
@@ -168,3 +168,10 @@ static inline void device_pm_init(struct device *dev)device_pm_sleep_init (dev);pm_runtime_init (dev); } + +#ifdef CONFIG_BPF_SYSCALL +struct bpf_ws_lock { }; +struct bpf_ws_lock *bpf_wakeup_sources_read_lock (void ); +void bpf_wakeup_sources_read_unlock (struct bpf_ws_lock *lock); +void *bpf_wakeup_sources_get_head (void ); +#endif
drivers/base/power/wakeup.c +70 -2
81 lines not on this page · raw message
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Greg Kroah-Hartman
maintainerRe: [PATCH v3 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
Quoted 3 lines · Samuel Wu
Iterating through wakeup sources via sysfs or debugfs can be inefficient or restricted. Introduce BPF kfuncs to allow high-performance and safe in-kernel traversal of the
wakeup_sourceslist.
What exactly is "inefficient"? I think you might have some numbers in your 0/2 patch, but putting it in here would be best.
And who is going to be calling these functions, just ebpf scripts?
Quoted 3 lines
The new kfuncs include:
-bpf_wakeup_sources_get_head()to obtain the list head.
- bpf_wakeup_sources_read_lock/unlock() to manage the SRCU lock.
Does this mean we can stop exporting wakeup_sources_read_lock() now?
Quoted 4 lines
For verifier safety, the underlying SRCU index is wrapped in an opaque 'struct
bpf_ws_lock' pointer. This enables the use ofKF_ACQUIREandKF_RELEASEflags, allowing the BPF verifier to strictly enforce paired lock/unlock cycles and prevent resource leaks.
But it's an index, not a lock. Is this just a verifier thing?
Quoted 12 lines
Signed-off-by Samuel Wu <[email protected]>--- drivers/base/power/power.h | 7 ++++ drivers/base/power/wakeup.c | 72 +++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-)drivers/base/power/power.h +0
-0
drivers/base/power/power.h static inline void device_pm_init(struct device *dev) · line 173
Quoted 6 lines
drivers/base/power/power.h +3
-0@@ -168,3 +168,10 @@ static inline void device_pm_init(struct device *dev)device_pm_sleep_init (dev);pm_runtime_init (dev); } + +#ifdef CONFIG_BPF_SYSCALL +struct bpf_ws_lock { };
An empty structure? This is just an int, so you are casting an int to a pointer? Can we make wakeup_sources_read_lock() actually use a structure instead to make this simpler?
Quoted 22 lines
drivers/base/power/power.h +4
-0+struct bpf_ws_lock *bpf_wakeup_sources_read_lock (void ); +void bpf_wakeup_sources_read_unlock (struct bpf_ws_lock *lock); +void *bpf_wakeup_sources_get_head (void ); +#endif drivers/base/power/wakeup.c +9
-1@@ -1168,11 +1168,79 @@ static const struct file_operations wakeup_sources_stats_fops = { .release = seq_release_private, }; -static int __init wakeup_sources_debugfs_init (void ) +#ifdef CONFIG_BPF_SYSCALL +#include <linux/btf.h> + +__bpf_kfunc_start_defs (); + +/** +* bpf_wakeup_sources_read_lock - Acquire the SRCU lock for wakeup sources +* +* The underlying SRCU lock returns an integer index. However, the BPF verifier
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1189
Quoted 10 lines
drivers/base/power/wakeup.c +10
-0+* requires a pointer (PTR_TO_BTF_ID) to strictly track the state of acquired +* resources using KF_ACQUIRE and KF_RELEASE semantics. We use an opaque +* structure pointer (struct bpf_ws_lock *) to satisfy the verifier while +* safely encoding the integer index within the pointer address itself. +* +* Return: An opaque pointer encoding the SRCU lock index + 1 (to avoid NULL). +*/ +__bpf_kfunc struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void) +{ +return (struct bpf_ws_lock *)(long )(wakeup_sources_read_lock () +1 );
Why are you incrementing this by 1?
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1202
Quoted 13 lines
drivers/base/power/wakeup.c +13
-0+} + +/** +* bpf_wakeup_sources_read_unlock - Release the SRCU lock for wakeup sources +* @lock: The opaque pointer returned by bpf_wakeup_sources_read_lock() +* +* The BPF verifier guarantees that @lock is a valid, unreleased pointer from +* the acquire function. We decode the pointer back into the integer SRCU index +* by subtracting 1 and release the lock. +*/ +__bpf_kfunc void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock) +{ +wakeup_sources_read_unlock ((int )(long )lock -1 );
Why decrementing by one?
So it's really an int, but you are casting it to a pointer, incrementing it by one to make it a "fake" pointer value (i.e. unaligned mess), and then when unlocking casting the pointer back to an int, and then decrementing the value?
This feels "odd" :(
Quoted 12 lines
drivers/base/power/wakeup.c +12
-0+} + +/** +* bpf_wakeup_sources_get_head - Get the head of the wakeup sources list +* +* Return: The head of the wakeup sources list. +*/ +__bpf_kfunc void *bpf_wakeup_sources_get_head(void) +{ +return &wakeup_sources; +} +
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1224
Quoted 10 lines
drivers/base/power/wakeup.c +10
-0+__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (wakeup_source_kfunc_ids) +BTF_ID_FLAGS (func ,bpf_wakeup_sources_read_lock ,KF_ACQUIRE ) +BTF_ID_FLAGS (func, bpf_wakeup_sources_read_unlock,KF_RELEASE ) +BTF_ID_FLAGS (func, bpf_wakeup_sources_get_head) +BTF_KFUNCS_END (wakeup_source_kfunc_ids) + +static const struct btf_kfunc_id_set wakeup_source_kfunc_set = { + .owner =THIS_MODULE ,
This isn't a module.
thanks,
greg k-h
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH v3 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
Quoted 15 lines · Greg Kroah-Hartman
Quoted 3 lines · Samuel Wu
Iterating through wakeup sources via sysfs or debugfs can be inefficient or restricted. Introduce BPF kfuncs to allow high-performance and safe in-kernel traversal of the
wakeup_sourceslist.What exactly is "inefficient"? I think you might have some numbers in your 0/2 patch, but putting it in here would be best.
And who is going to be calling these functions, just ebpf scripts?
Quoted 3 lines
The new kfuncs include:
-bpf_wakeup_sources_get_head()to obtain the list head.
- bpf_wakeup_sources_read_lock/unlock() to manage the SRCU lock.
Quoted 8 lines
Does this mean we can stop exporting
wakeup_sources_read_lock()now?Quoted 4 lines
For verifier safety, the underlying SRCU index is wrapped in an opaque 'struct
bpf_ws_lock' pointer. This enables the use ofKF_ACQUIREandKF_RELEASEflags, allowing the BPF verifier to strictly enforce paired lock/unlock cycles and prevent resource leaks.But it's an index, not a lock. Is this just a verifier thing?
It's a verifier thing. The index must be passed to SRCU unlock wrapped by the unlock kfunc for correctness. The verifier understands such acquire/release tracking for pointers (e.g., taking refcount and putting it), but not for scalar values, so we need to launder it through a pointer to an empty struct, which isn't really usable except for passing it eventually to unlock. If the program doesn't do the unlock, the verifier will reject it.
Quoted 8 lines
Quoted 8 lines
Signed-off-by Samuel Wu <[email protected]>--- drivers/base/power/power.h | 7 ++++ drivers/base/power/wakeup.c | 72 +++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-)
Quoted 15 lines
drivers/base/power/power.h static inline void device_pm_init(struct device *dev) · line 173
Quoted 11 lines
drivers/base/power/power.h +3
-0@@ -168,3 +168,10 @@ static inline void device_pm_init(struct device *dev)device_pm_sleep_init (dev);pm_runtime_init (dev); } + +#ifdef CONFIG_BPF_SYSCALL +struct bpf_ws_lock { };An empty structure? This is just an int, so you are casting an int to a pointer? Can we make
wakeup_sources_read_lock()actually use a structure instead to make this simpler?
See above.
Quoted 27 lines
Quoted 27 lines
drivers/base/power/power.h +4
-0+struct bpf_ws_lock *bpf_wakeup_sources_read_lock (void ); +void bpf_wakeup_sources_read_unlock (struct bpf_ws_lock *lock); +void *bpf_wakeup_sources_get_head (void ); +#endif drivers/base/power/wakeup.c +13
-1@@ -1168,11 +1168,79 @@ static const struct file_operations wakeup_sources_stats_fops = { .release = seq_release_private, }; -static int __init wakeup_sources_debugfs_init (void ) +#ifdef CONFIG_BPF_SYSCALL +#include <linux/btf.h> + +__bpf_kfunc_start_defs (); + +/** +* bpf_wakeup_sources_read_lock - Acquire the SRCU lock for wakeup sources +* +* The underlying SRCU lock returns an integer index. However, the BPF verifier +* requires a pointer (PTR_TO_BTF_ID) to strictly track the state of acquired +* resources using KF_ACQUIRE and KF_RELEASE semantics. We use an opaque +* structure pointer (struct bpf_ws_lock *) to satisfy the verifier while +* safely encoding the integer index within the pointer address itself.
Quoted 8 lines
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1189
Quoted 6 lines
drivers/base/power/wakeup.c +6
-0+* +* Return: An opaque pointer encoding the SRCU lock index + 1 (to avoid NULL). +*/ +__bpf_kfunc struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void) +{ +return (struct bpf_ws_lock *)(long )(wakeup_sources_read_lock () +1 );Why are you incrementing this by 1?
I think SRCU indices can be 0, so it would appear as a NULL pointer to the program.
Quoted 17 lines
Quoted 4 lines
drivers/base/power/wakeup.c +3
-0+} + +/** drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1202
Quoted 10 lines
drivers/base/power/wakeup.c +10
-0+* bpf_wakeup_sources_read_unlock - Release the SRCU lock for wakeup sources +* @lock: The opaque pointer returned by bpf_wakeup_sources_read_lock() +* +* The BPF verifier guarantees that @lock is a valid, unreleased pointer from +* the acquire function. We decode the pointer back into the integer SRCU index +* by subtracting 1 and release the lock. +*/ +__bpf_kfunc void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock) +{ +wakeup_sources_read_unlock ((int )(long )lock -1 );Why decrementing by one?
Quoted 6 lines
So it's really an int, but you are casting it to a pointer, incrementing it by one to make it a "fake" pointer value (i.e. unaligned mess), and then when unlocking casting the pointer back to an int, and then decrementing the value?
This feels "odd" :(
It isn't readable, though, because it's an empty struct, so I don't think it would cause any issues in practice.
Quoted 2 lines
[...]
PATCH v3 2/2 selftests/bpf: Add tests for wakeup_sources kfuncs
In reply to Samuel WuIntroduce a set of BPF selftests to verify the safety and functionality of wakeup_source kfuncs.
The suite includes:
1. A functional test (test_wakeup_source.c) that iterates over the global wakeup_sources list. It uses CO-RE to read timing statistics and validates them in user-space via the BPF ring buffer.
2. A negative test suite (wakeup_source_fail.c) ensuring the BPF verifier correctly enforces reference tracking and type safety.
3. Enable CONFIG_PM_WAKELOCKS in the test config, allowing creation of wakeup sources via /sys/power/wake_lock.
A shared header (wakeup_source.h) is introduced to ensure consistent memory layout for the Ring Buffer data between BPF and user-space.
--- tools/testing/selftests/bpf/config | 3 +- .../selftests/bpf/prog_tests/wakeup_source.c | 101 ++++++++++++++++++ .../selftests/bpf/progs/test_wakeup_source.c | 92 ++++++++++++++++ .../selftests/bpf/progs/wakeup_source.h | 22 ++++ .../selftests/bpf/progs/wakeup_source_fail.c | 76 +++++++++++++ 5 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/wakeup_source.c create mode 100644 tools/testing/selftests/bpf/progs/test_wakeup_source.c create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source.h create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source_fail.c
tools/testing/selftests/bpf/config +2 -1
@@ -130,4 +130,5 @@ CONFIG_INFINIBAND=y CONFIG_SMC=y CONFIG_SMC_HS_CTRL_BPF=y CONFIG_DIBS=y -CONFIG_DIBS_LO=y +CONFIG_DIBS_LO=y +CONFIG_PM_WAKELOCKS=y
tools/testing/selftests/bpf/prog_tests/wakeup_source.c new file +101 -0
101 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +92 -0
92 lines not on this page · raw message
tools/testing/selftests/bpf/progs/wakeup_source.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright 2026 Google LLC */ + +#ifndef __WAKEUP_SOURCE_H__ +#define __WAKEUP_SOURCE_H__ + +#define WAKEUP_NAME_LEN 128 + +struct wakeup_event_t { +unsigned long active_count; +long long active_time_ns; +unsigned long event_count; +unsigned long expire_count; +long long last_time_ns; +long long max_time_ns; +long long prevent_sleep_time_ns; +long long total_time_ns; +unsigned long wakeup_count; +char name[WAKEUP_NAME_LEN ]; +}; + +#endif /* __WAKEUP_SOURCE_H__ */
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Greg Kroah-Hartman
maintainerQuoted 8 lines · Samuel Wu
This patchset adds requisite kfuncs for BPF programs to safely traverse
wakeup_sources, and puts a config flag around the sysfs interface.Currently, a traversal of wakeup sources require going through
/sys/class/wakeup/*or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds ofwakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.
Describe "inefficient" please?
And if you really think that doing an open/read/close on a virtual filesystem is inefficient, then I have the syscall for you!
I've been trying to get readfile() accepted every few years, looks like I last tried in 2020:
https://lore.kernel.org/r/[email protected]
but I keep the patchset up to date in my local tree all the time.
Would that help you out here instead?
Quoted 3 lines
Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list. The head address of
wakeup_sourcescan safely be resolved through BPF helper functions or variable attributes.
Who is going to be calling this?
Quoted 10 lines
On a quiescent Pixel 6 traversing 150
wakeup_sources, I am seeing ~34x speedup (sampled 75 times in table below). For a device under load, the speedup is greater.+-------+----+----------+----------+ | | n | AVG (ms) | STD (ms) | +-------+----+----------+----------+ | sysfs | 75 | 44.9 | 12.6 | +-------+----+----------+----------+ | BPF | 75 | 1.3 | 0.7 | +-------+----+----------+----------+
150 sysfs calls in 44.9 ms feels very slow. but really, what are you expecting here, sysfs should NEVER be on a "fast path" that you care about performance. Why are you hammering on sysfs here? What HAS to have this type of performance?
In other words, what problem are you trying to solve that having access to 150+ sysfs files all at once in a faster way is going to fix?
thanks,
greg k-h
Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Samuel Wu
This patchset adds requisite kfuncs for BPF programs to safely traverse
wakeup_sources, and puts a config flag around the sysfs interface.
Quoted 7 lines
Quoted 5 lines
Currently, a traversal of wakeup sources require going through
/sys/class/wakeup/*or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds ofwakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.Describe "inefficient" please?
Ack; I’ll provide a more detailed breakdown in the v4 cover letter. To
summarize: the "inefficiency" isn't just the number of sources (150), but the fact that each source has 10 attributes. We are looking at 1,500+ sysfs nodes to get a full snapshot of the system.
Quoted 10 lines
And if you really think that doing an open/read/close on a virtual filesystem is inefficient, then I have the syscall for you!
I've been trying to get
readfile()accepted every few years, looks like I last tried in 2020:https://lore.kernel.org/r/[email protected]but I keep the patchset up to date in my local tree all the time.
Would that help you out here instead?
readfile() seems like it would be a great optimization for many usecases, but it doesn't solve the context switch bottleneck. Additionally, current userspace implementations attempt to speed up this traversal by caching fds, so this new syscall wouldn't help as much as one might initially expect.
Quoted 5 lines
Quoted 3 lines
Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list. The head address of
wakeup_sourcescan safely be resolved through BPF helper functions or variable attributes.Who is going to be calling this?
BPF programs call the new kfuncs. I realized I left some stale text in the v2 cover letter regarding the interface; I'll clean that up for the next version to make this point clearer.
Quoted 11 lines
Quoted 10 lines
On a quiescent Pixel 6 traversing 150
wakeup_sources, I am seeing ~34x speedup (sampled 75 times in table below). For a device under load, the speedup is greater.+-------+----+----------+----------+ | | n | AVG (ms) | STD (ms) | +-------+----+----------+----------+ | sysfs | 75 | 44.9 | 12.6 | +-------+----+----------+----------+ | BPF | 75 | 1.3 | 0.7 | +-------+----+----------+----------+
Quoted 7 lines
150 sysfs calls in 44.9 ms feels very slow. but really, what are you expecting here, sysfs should NEVER be on a "fast path" that you care about performance. Why are you hammering on sysfs here? What HAS to have this type of performance?
In other words, what problem are you trying to solve that having access to 150+ sysfs files all at once in a faster way is going to fix?
The 44.9ms is the cost of reading ~1,500 sysfs nodes (150 sources * 10 attributes). This is even worse on wearables, where the compute and power constrained platform exacerbates performance sensitivity even more.
On these platforms, the CPU is suspended as much as possible. A byproduct of this is that the wakeup source traversal occurs on a user-perceptible path, which impacts battery life and UI responsiveness.
Beyond the performance improvement, moving to BPF offers other benefits:
1. Reduce Memory: Drop the fd caching logic
2. Simplify Security: Consolidate SELinux permissions rather than managing labels for every single *possible* wakeup_source
Thanks Greg!
-- Sam
Greg Kroah-Hartman
maintainerQuoted 13 lines · Samuel Wu
Quoted 11 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
This patchset adds requisite kfuncs for BPF programs to safely traverse
wakeup_sources, and puts a config flag around the sysfs interface.Currently, a traversal of wakeup sources require going through
/sys/class/wakeup/*or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds ofwakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.
Quoted 6 lines
Quoted 1 line
Describe "inefficient" please?
Ack; I’ll provide a more detailed breakdown in the v4 cover letter. To
summarize: the "inefficiency" isn't just the number of sources (150), but the fact that each source has 10 attributes. We are looking at 1,500+ sysfs nodes to get a full snapshot of the system.
Wait, no, something is wrong here. You should NEVER be wanting to combine multiple sysfs files at the same time into a "snapshot" of the system because by virtue of how this works, it's going to change while you are actually traversing all of those files!
Why are you trying to read 1500+ sysfs files at once, and what are you doing with that information? And if you really need it "all at once", why can't we provide it for you in a sane manner, instead of being forced to either walk the whole sysfs tree, or rely on a bpf script?
So, what problem are you trying to solve that looking at all of these files solves for you at the moment?
thanks,
greg k-h
Quoted 22 lines · Greg Kroah-Hartman
Quoted 13 lines · Samuel Wu
Quoted 11 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
This patchset adds requisite kfuncs for BPF programs to safely traverse
wakeup_sources, and puts a config flag around the sysfs interface.Currently, a traversal of wakeup sources require going through
/sys/class/wakeup/*or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds ofwakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.Quoted 6 lines
Quoted 1 line
Describe "inefficient" please?
Ack; I’ll provide a more detailed breakdown in the v4 cover letter. To
summarize: the "inefficiency" isn't just the number of sources (150), but the fact that each source has 10 attributes. We are looking at 1,500+ sysfs nodes to get a full snapshot of the system.
Quoted 4 lines
Wait, no, something is wrong here. You should NEVER be wanting to combine multiple sysfs files at the same time into a "snapshot" of the system because by virtue of how this works, it's going to change while you are actually traversing all of those files!
Agree, the current approach with sysfs might have stale values. The BPF approach holds a lock while traversing the list. It's not a perfect snapshot, but it's internally consistent and arguably better than the current sysfs implementation.
Quoted 4 lines
Why are you trying to read 1500+ sysfs files at once, and what are you doing with that information? And if you really need it "all at once", why can't we provide it for you in a sane manner, instead of being forced to either walk the whole sysfs tree, or rely on a bpf script?
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
[1]: https://lore.kernel.org/all/[email protected]/
Thanks!
Sam
Greg Kroah-Hartman
maintainerQuoted 39 lines · Samuel Wu
Quoted 22 lines · Greg Kroah-Hartman
Quoted 13 lines · Samuel Wu
Quoted 11 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
This patchset adds requisite kfuncs for BPF programs to safely traverse
wakeup_sources, and puts a config flag around the sysfs interface.Currently, a traversal of wakeup sources require going through
/sys/class/wakeup/*or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds ofwakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure.Quoted 6 lines
Quoted 1 line
Describe "inefficient" please?
Ack; I’ll provide a more detailed breakdown in the v4 cover letter. To
summarize: the "inefficiency" isn't just the number of sources (150), but the fact that each source has 10 attributes. We are looking at 1,500+ sysfs nodes to get a full snapshot of the system.Quoted 4 lines
Wait, no, something is wrong here. You should NEVER be wanting to combine multiple sysfs files at the same time into a "snapshot" of the system because by virtue of how this works, it's going to change while you are actually traversing all of those files!
Agree, the current approach with sysfs might have stale values. The BPF approach holds a lock while traversing the list. It's not a perfect snapshot, but it's internally consistent and arguably better than the current sysfs implementation.
Quoted 4 lines
Why are you trying to read 1500+ sysfs files at once, and what are you doing with that information? And if you really need it "all at once", why can't we provide it for you in a sane manner, instead of being forced to either walk the whole sysfs tree, or rely on a bpf script?
Quoted 6 lines
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
Ok, as the sysfs api doesn't work your use case anymore, why do we need to keep it around at all?
Quoted 6 lines
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
I'm all for making this more scalable and work for your systems now, but consider if you could drop the sysfs api entirely, would you want this to be a different type of api entirely instead of having to plug through these using ebpf?
thanks,
greg k-h
Quoted 10 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Samuel Wu
Quoted 2 lines · Greg Kroah-Hartman
On Tue, Mar 31, 2026 at 08:34:09AM -0700, Samuel Wu wrote:
[ ... ]
Quoted 17 lines
Quoted 6 lines
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
Ok, as the sysfs api doesn't work your use case anymore, why do we need to keep it around at all?
Quoted 6 lines
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
Quoted 4 lines
I'm all for making this more scalable and work for your systems now, but consider if you could drop the sysfs api entirely, would you want this to be a different type of api entirely instead of having to plug through these using ebpf?
Almost all use cases want all this data at once, so AFAICT BPF offers the best performance for that. But of course, open to discussion if there is an alternative API that matches BPF's performance for this use case.
I'm not opposed to dropping the sysfs approach, and I attempted to do so in the v1 patch [1]. I'm not sure who else currently uses those sysfs nodes, but a config flag should remove friction and could be a stepping stone toward deprecation/removal.
[1]: https://lore.kernel.org/all/[email protected]/
Thanks!
-- Sam
Quoted 43 lines · Samuel Wu
Quoted 10 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Samuel Wu
Quoted 2 lines · Greg Kroah-Hartman
On Tue, Mar 31, 2026 at 08:34:09AM -0700, Samuel Wu wrote:
[ ... ]
Quoted 17 lines
Quoted 6 lines
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
Ok, as the sysfs api doesn't work your use case anymore, why do we need to keep it around at all?
Quoted 6 lines
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
Quoted 4 lines
I'm all for making this more scalable and work for your systems now, but consider if you could drop the sysfs api entirely, would you want this to be a different type of api entirely instead of having to plug through these using ebpf?
Almost all use cases want all this data at once, so AFAICT BPF offers the best performance for that. But of course, open to discussion if there is an alternative API that matches BPF's performance for this use case.
Quoted 6 lines
I'm not opposed to dropping the sysfs approach, and I attempted to do so in the v1 patch [1]. I'm not sure who else currently uses those sysfs nodes, but a config flag should remove friction and could be a stepping stone toward deprecation/removal.
The patches make sense to me.
Patch 2 adds a bpf selftest and corresponding:
+CONFIG_DIBS_LO=y
+CONFIG_PM_WAKELOCKS=y
and almost green in BPF CI.
Except s390 that fails with:
Error: #682/1 wakeup_source/iterate_and_verify_times
Error: #682/1 wakeup_source/iterate_and_verify_times
libbpf: extern (func ksym) 'bpf_wakeup_sources_get_head': not found in kernel or module BTFs
libbpf: failed to load BPF skeleton 'test_wakeup_source': -EINVAL test_wakeup_source:FAIL:skel_open_and_load unexpected error: -22
We can still land it into bpf-next for this merge window.
Greg,
any objection ?
If not, Samuel, could you resend and address s390 issue? I'm guessing wakeup sources just don't exist there.
Greg Kroah-Hartman
maintainerQuoted 68 lines · Alexei Starovoitov
Quoted 43 lines · Samuel Wu
Quoted 10 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Samuel Wu
Quoted 2 lines · Greg Kroah-Hartman
On Tue, Mar 31, 2026 at 08:34:09AM -0700, Samuel Wu wrote:
[ ... ]
Quoted 17 lines
Quoted 6 lines
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
Ok, as the sysfs api doesn't work your use case anymore, why do we need to keep it around at all?
Quoted 6 lines
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
Quoted 4 lines
I'm all for making this more scalable and work for your systems now, but consider if you could drop the sysfs api entirely, would you want this to be a different type of api entirely instead of having to plug through these using ebpf?
Almost all use cases want all this data at once, so AFAICT BPF offers the best performance for that. But of course, open to discussion if there is an alternative API that matches BPF's performance for this use case.
Quoted 6 lines
I'm not opposed to dropping the sysfs approach, and I attempted to do so in the v1 patch [1]. I'm not sure who else currently uses those sysfs nodes, but a config flag should remove friction and could be a stepping stone toward deprecation/removal.
The patches make sense to me.
Patch 2 adds a bpf selftest and corresponding:
(quoted diff) +2
-0+CONFIG_DIBS_LO=y +CONFIG_PM_WAKELOCKS=yand almost green in BPF CI.
Except s390 that fails with:
Error: #682/1 wakeup_source/iterate_and_verify_times
Error: #682/1 wakeup_source/iterate_and_verify_times
libbpf: extern (func ksym) 'bpf_wakeup_sources_get_head': not found in kernel or module BTFs
libbpf: failed to load BPF skeleton 'test_wakeup_source':-EINVALtest_wakeup_source:FAIL:skel_open_and_load unexpected error: -22
Quoted 4 lines
We can still land it into bpf-next for this merge window.
Greg,
any objection ?
Yes, it is too late for 7.1-rc1, sorry, there will have not been any time in linux-next to add it. Let's revisit it after -rc1 is out, and again, I feel that "walk all sysfs devices in bpf" is not the correct solution for a system-wide snapshot interface you want to have, especially as the one you previously added you feel is now obsolete.
thanks,
greg k-h
Quoted 72 lines · Greg Kroah-Hartman
Quoted 70 lines · Alexei Starovoitov
Quoted 43 lines · Samuel Wu
Quoted 10 lines · Greg Kroah-Hartman
Quoted 8 lines · Samuel Wu
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Samuel Wu
Quoted 2 lines · Greg Kroah-Hartman
On Tue, Mar 31, 2026 at 08:34:09AM -0700, Samuel Wu wrote:
[ ... ]
Quoted 17 lines
Quoted 6 lines
The data is fundamental for debugging and improving power at scale. The original discussion and patch [1] provide more context of the intent. To summarize the history, debugfs was unstable and insecure, leading to the current sysfs implementation. However, sysfs has the constraint of one attribute per node, requiring 10 sysfs accesses per wakeup source.
Ok, as the sysfs api doesn't work your use case anymore, why do we need to keep it around at all?
Quoted 6 lines
That said, I completely agree that reading 1500+ sysfs files at once is unreasonable. Perhaps the sysfs approach was manageable at the time of [1], but moving forward we need a more scalable solution. This is the main motivator and makes BPF the sane approach, as it improves traversal in nearly every aspect (e.g. cycles, memory, simplicity, scalability).
Quoted 4 lines
I'm all for making this more scalable and work for your systems now, but consider if you could drop the sysfs api entirely, would you want this to be a different type of api entirely instead of having to plug through these using ebpf?
Almost all use cases want all this data at once, so AFAICT BPF offers the best performance for that. But of course, open to discussion if there is an alternative API that matches BPF's performance for this use case.
Quoted 6 lines
I'm not opposed to dropping the sysfs approach, and I attempted to do so in the v1 patch [1]. I'm not sure who else currently uses those sysfs nodes, but a config flag should remove friction and could be a stepping stone toward deprecation/removal.
The patches make sense to me.
Patch 2 adds a bpf selftest and corresponding:
(quoted diff) +2
-0+CONFIG_DIBS_LO=y +CONFIG_PM_WAKELOCKS=yand almost green in BPF CI.
Except s390 that fails with:
Error: #682/1 wakeup_source/iterate_and_verify_times
Error: #682/1 wakeup_source/iterate_and_verify_times
libbpf: extern (func ksym) 'bpf_wakeup_sources_get_head': not found in kernel or module BTFs
libbpf: failed to load BPF skeleton 'test_wakeup_source':-EINVALtest_wakeup_source:FAIL:skel_open_and_load unexpected error: -22We can still land it into bpf-next for this merge window.
Quoted 8 lines
Quoted 2 lines
Greg,
any objection ?Yes, it is too late for 7.1-rc1, sorry, there will have not been any time in linux-next to add it. Let's revisit it after -rc1 is out, and again, I feel that "walk all sysfs devices in bpf" is not the correct solution for a system-wide snapshot interface you want to have, especially as the one you previously added you feel is now obsolete.
Sounds good, when the merge window closes, I'll send out v4 with the s390 errors and remaining comments fixed.
Regarding surfacing a system-wide snapshot of wakeup sources to userspace, can you elaborate on the solution you're envisioning? My understanding is that "walk all sysfs devices in bpf" is a canonical BPF use case, especially in this scenario, where we see improvements in every core metric.