Support BPF traversal of wakeup sources
Subsystems (7)
v4 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, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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 v4:
- Removed .owner = THIS_MODULE for btf_kfunc_id_set per Greg
- Add a graceful exit in selftest if bpf_wakeup_sources_get_head() is not present due to kernel configs without CONFIG_PM_SLEEP (e.g. s390)
- Relaxed substr match in wakeup_source_unlock_null() selftest
- v3 link: 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 | 71 ++++++++++- tools/testing/selftests/bpf/config | 3 +- .../selftests/bpf/prog_tests/wakeup_source.c | 118 ++++++++++++++++++ .../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, 386 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.54.0.563.g4f69b47b94-goog
PATCH v4 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. There is at least a 30x speedup for walking 150 wakeup sources and all their attributes.
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 | 71 +++++++++++++++++++++++++++++++++++-- 2 files changed, 76 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 +69 -2
80 lines not on this page · raw message
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
What stops a BPF program from using bpf_wakeup_sources_get_head() to get the wakeup_sources pointer and iterating this list without taking the SRCU lock?
Quoted 4 lines
drivers/base/power/power.h +0
-0
[ ... ]
Quoted 4 lines
drivers/base/power/wakeup.c +0
-0
[ ... ]
Quoted 8 lines
drivers/base/power/wakeup.c +4
-0@@ -1168,11 +1168,78 @@ static const struct file_operations wakeup_sources_stats_fops = { .release = seq_release_private, }; +#ifdef CONFIG_BPF_SYSCALL +#include <linux/btf.h> + +__bpf_kfunc_start_defs ();
[ ... ]
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1213
Quoted 9 lines
drivers/base/power/wakeup.c +9
-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; +}
What stops a BPF program from using bpf_wakeup_sources_get_head() to get the wakeup_sources pointer and iterating this list without taking the SRCU lock?
The current API design requires BPF programs to manually call the lock/unlock functions, but there's no enforcement that they must do so before accessing the list. A program could call bpf_wakeup_sources_get_head() and directly iterate the list without proper SRCU protection, which could lead to race conditions or crashes.
An open-coded iterator would solve this by internalizing the lock:
Iterator new() acquires SRCU lock
Iterator next() returns next wakeup_source
Iterator destroy() releases SRCU lock
This design pattern ensures that the list can never be traversed without proper locking. Puranjay Mohan raised this concern in v2, but it was not addressed in v3 or v4.
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1219
Quoted 6 lines
drivers/base/power/wakeup.c +6
-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)
Should bpf_wakeup_sources_read_lock have the KF_RET_NULL flag?
The function returns an SRCU index cast to a pointer (incremented by 1). If wakeup_sources_read_lock() were to fail, the result would need to be checked for null. Without KF_RET_NULL, the BPF verifier won't require null checks on the returned pointer.
Looking at bpf_wakeup_sources_read_lock():
__bpf_kfunc struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void)
{
return (struct bpf_ws_lock *)(long)(wakeup_sources_read_lock() + 1);
}If the underlying wakeup_sources_read_lock() could return a value that, when incremented, equals zero, or if there's any error path, BPF programs would need to check for NULL before using the lock pointer.
Puranjay Mohan raised this in v2, but it was not addressed in v3 or v4.
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1221
Quoted 2 lines
drivers/base/power/wakeup.c +2
-0+BTF_ID_FLAGS(func, bpf_wakeup_sources_get_head) +BTF_KFUNCS_END (wakeup_source_kfunc_ids)
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25688185901
Re: [PATCH v4 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
On Mon, May 11, 2026 at 11:27 AM <[email protected]> wrote:
[ ... ]
Quoted 26 lines
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1213
Quoted 9 lines
drivers/base/power/wakeup.c +9
-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; +}What stops a BPF program from using
bpf_wakeup_sources_get_head()to get thewakeup_sourcespointer and iterating this list without taking the SRCU lock?The current API design requires BPF programs to manually call the lock/unlock functions, but there's no enforcement that they must do so before accessing the list. A program could call
bpf_wakeup_sources_get_head()and directly iterate the list without proper SRCU protection, which could lead to race conditions or crashes.An open-coded iterator would solve this by internalizing the lock:
Iterator
new()acquires SRCU lock
Iteratornext()returns nextwakeup_source
Iteratordestroy()releases SRCU lock
Quoted 5 lines
This design pattern ensures that the list can never be traversed without proper locking. Puranjay Mohan raised this concern in v2, but it was not addressed in v3 or v4.
This was already discussed; the response is at [1] and the BPF iterator attempt is at [2]. In short, BPF iterators and their tradeoffs were explored, but the current form was chosen as the best option.
[1]: https://lore.kernel.org/all/CAP01T74KNVT2SVRR+XPbFb1vy85W=Jkp4_yD6xvTSg2avQo8AQ@mail.gmail.com/
[2]: https://lore.kernel.org/all/[email protected]/
Quoted 21 lines
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1219
Quoted 6 lines
drivers/base/power/wakeup.c +6
-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)Should
bpf_wakeup_sources_read_lockhave theKF_RET_NULLflag?The function returns an SRCU index cast to a pointer (incremented by 1). If
wakeup_sources_read_lock()were to fail, the result would need to be checked for null. WithoutKF_RET_NULL, the BPF verifier won't require null checks on the returned pointer.Looking at
bpf_wakeup_sources_read_lock():
__bpf_kfuncstructbpf_ws_lock*bpf_wakeup_sources_read_lock(void){ return (struct bpf_ws_lock *)(long)(wakeup_sources_read_lock() + 1); }
Quoted 8 lines
If the underlying
wakeup_sources_read_lock()could return a value that, when incremented, equals zero, or if there's any error path, BPF programs would need to check for NULL before using the lock pointer.Puranjay Mohan raised this in v2, but it was not addressed in v3 or v4.
This is fine, the underlying srcu index return value is guaranteed to be non-negative, so the return value of bpf_wakeup_sources_read_lock() will be positive and not need the NULL check.
[email protected]
AI reviewRe: [PATCH v4 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
In reply to Samuel WuThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The kfunc `bpf_wakeup_sources_get_head()` returns `void *` instead of `struct list_head *`, which…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The kfunc bpf_wakeup_sources_get_head() returns void * instead of struct list_head *, which the BPF verifier downgrades to an unknown scalar, destroying BTF type tracking.
- [High] The verifier cannot enforce that the SRCU lock is actually held during list traversal, allowing trivial use-after-free vulnerabilities and violating BPF architectural patterns.
- [Medium] Wrapping the integer SRCU index in a fake struct bpf_ws_lock * pointer circumvents verifier safeguards and introduces invalid trusted pointers. --
commit 6dcbdc1d5aa4a12914c7281f95390e19498129d0
Author: Samuel Wu <[email protected]>
PM: wakeup: Add kfuncs to traverse over wakeup_sources
This patch introduces BPF kfuncs to allow in-kernel traversal of the wakeup_sources list. It adds functions to get the list head and manage the SRCU lock, using an opaque pointer to hold the SRCU index for the verifier.
Quoted 4 lines
drivers/base/power/wakeup.c +0
-0
[ ... ]
Quoted 5 lines
drivers/base/power/wakeup.c +5
-0+/** +* 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
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1190
Quoted 10 lines
drivers/base/power/wakeup.c +10
-0+* 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 ); +}
The verifier treats the returned struct bpf_ws_lock * as a trusted PTR_TO_BTF_ID. By casting the integer index returned by srcu_read_lock() into a pointer, we end up fabricating invalid kernel pointers pointing to near-zero addresses like 0x1 or 0x2.
Does this circumvent verifier safeguards by introducing invalid trusted pointers?
[ ... ]
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1203
Quoted 12 lines
drivers/base/power/wakeup.c +12
-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 ); +}
The patch uses KF_ACQUIRE and KF_RELEASE to ensure the lock is eventually unlocked. However, does the BPF verifier have a mechanism to tie this custom lock state to the list pointer returned by bpf_wakeup_sources_get_head()?
It seems a BPF program could fetch the list head and traverse it without ever acquiring the lock, or continue accessing elements after releasing the lock, resulting in a use-after-free.
Would it be safer to implement a dedicated BPF iterator (bpf_iter_wakeup_source) that internally manages the SRCU lock and safely yields elements, rather than exposing the raw lock and list head?
drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1213
Quoted 9 lines
drivers/base/power/wakeup.c +9
-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; +}
Since this kfunc returns void * instead of struct list_head *, the BPF verifier evaluates it as returning an unknown scalar.
Because the return value becomes a scalar, BPF programs cannot natively or safely dereference the list head. They would be forced to cast the scalar back into a pointer using bpf_rdonly_cast(), which the verifier marks as PTR_UNTRUSTED.
Since untrusted pointers cause field dereferences to be rewritten into slow PROBE_MEM operations, does this defeat the goal of high-performance in-kernel traversal?
Could the return type be changed to struct list_head * to be natively tracked as a PTR_TO_BTF_ID?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 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 | 118 ++++++++++++++++++ .../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, 310 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 +118 -0
118 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.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 2/2] selftests/bpf: Add tests for wakeup_sources kfuncs
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] BPF verifier rejection due to direct memory access on scalar register - [High] Incorrect expected…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] BPF verifier rejection due to direct memory access on scalar register
- [High] Incorrect expected failure messages will cause negative tests to fail
- [Medium] Early return upon skeleton load failure skips the negative test suite
- [Medium] Architecture portability issue due to layout mismatch in shared structure
- [Medium] Uninitialized memory leak in ring buffer
--
commit 4b176327f91f44d20f491d6779d845f94357d717
Author: Samuel Wu <[email protected]>
selftests/bpf: Add tests for wakeup_sources kfuncs
This commit introduces a set of BPF selftests to verify the safety and functionality of wakeup_source kfuncs. It includes a functional test iterating over the global wakeup_sources list and a negative test suite for verifier checks.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source.c new file +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source.c new file +0
-0@@ -73,46 +73,46 @@ void test_wakeup_source(void)return ; }if (test__start_subtest ("iterate_and_verify_times" )) {struct test_wakeup_source *skel;
tools/testing/selftests/bpf/prog_tests/wakeup_source.c void test_wakeup_source(void) · line 87
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source.c new file +0
-0struct ring_buffer *rb =NULL ;struct rb_ctx rb_ctx = { .name ="bpf_selftest_ws_times" , .found = false, };int err; skel =test_wakeup_source__open_and_load ();if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ;
If test_wakeup_source__open_and_load() fails, the code executes an early return. Does this silently skip running the negative test suite via RUN_TESTS(wakeup_source_fail) at the end of the function?
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +0
-0
[ ... ]
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +0
-0@@ -32,61 +32,61 @@ int iterate_wakeupsources(void *ctx) bpf_for(i, 0, MAX_LOOP_ITER) {if (bpf_core_read (&pos,sizeof (pos), &pos->next) || !pos || pos == head)break ;struct wakeup_event_t *e =bpf_ringbuf_reserve (&rb,sizeof (*e),0 );
tools/testing/selftests/bpf/progs/test_wakeup_source.c int iterate_wakeupsources(void *ctx) · line 48
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +0
-0if (!e)break ;struct wakeup_source *ws =bpf_core_cast ( (void *)pos - bpf_core_field_offset(struct wakeup_source, entry), struct wakeup_source);s64 active_time =0 ;bool active =BPF_CORE_READ_BITFIELD (ws, active);bool autosleep_enable =BPF_CORE_READ_BITFIELD (ws, autosleep_enabled);s64 last_time = ws->last_time;
Since pos is read from memory via bpf_core_read(), the verifier tracks it as a SCALAR_VALUE. Because ws is derived from pos via pointer arithmetic, will the BPF verifier reject direct memory dereferences like ws->last_time with an "invalid mem access 'scalar'" error?
Should these accesses be wrapped in BPF_CORE_READ() instead?
[ ... ]
tools/testing/selftests/bpf/progs/test_wakeup_source.c int iterate_wakeupsources(void *ctx) · line 86
Quoted 12 lines
tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +0
-0@@ -76,17 +76,17 @@ int iterate_wakeupsources(void *ctx) e->last_time_ns = last_time; e->max_time_ns = max_time; e->prevent_sleep_time_ns = prevent_sleep_time; e->total_time_ns = total_time; e->wakeup_count = ws->wakeup_count;if (bpf_probe_read_kernel_str ( e->name,WAKEUP_NAME_LEN , ws->name) <0 ) e->name[0 ] ='\0' ;bpf_ringbuf_submit (e,0 );
bpf_ringbuf_reserve() does not zero-initialize the memory, and bpf_probe_read_kernel_str() does not pad the remainder of the buffer with zeroes if the string is shorter than WAKEUP_NAME_LEN.
Could this expose uninitialized kernel memory from previous ring buffer uses to user-space through the remainder of e->name?
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/progs/wakeup_source.h new file +1
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */
tools/testing/selftests/bpf/progs/wakeup_source.h line 11
Quoted 10 lines
tools/testing/selftests/bpf/progs/wakeup_source.h new file +10
-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;
Because this structure is shared between BPF and user-space, could using architecture-dependent types like unsigned long cause a size and layout mismatch?
BPF programs are compiled for a 64-bit target where unsigned long is 8 bytes, but the user-space selftest may be compiled on a 32-bit host (like ARM32) where it is 4 bytes.
Should explicitly sized types like __u64 be used for shared structures?
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/wakeup_source_fail.c int wakeup_source_access_lock_fields(void *ctx) · line 52
Quoted 10 lines
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +0
-0@@ -44,19 +44,19 @@ int wakeup_source_access_lock_fields(void *ctx) val = *(int *)lock;bpf_wakeup_sources_read_unlock (lock);return val; } SEC("syscall") __failure __msg("type=scalar expected=fp") int wakeup_source_unlock_no_lock(void *ctx)
Is this the correct expected failure message for a kfunc argument?
The check_kfunc_args() function typically emits messages like "arg#0 pointer type mismatch" when an incorrect type is passed, whereas "expected=fp" is usually emitted by check_func_arg() for standard helpers.
Will this test fail because the verifier output doesn't match the expectation?
Quoted 7 lines
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +0
-0{struct bpf_ws_lock *lock = (void *)0x1 ;bpf_wakeup_sources_read_unlock (lock);return 0 ; }
[ ... ]
tools/testing/selftests/bpf/progs/wakeup_source_fail.c int wakeup_source_unlock_null(void *ctx) · line 71
Quoted 13 lines
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +0
-0@@ -60,17 +60,17 @@ int wakeup_source_unlock_null(void *ctx)bpf_wakeup_sources_read_unlock (NULL );return 0 ; } SEC("syscall") __failure __msg("R0 invalid mem access 'scalar'") int wakeup_source_unsafe_dereference(void *ctx) {struct list_head *head =bpf_wakeup_sources_get_head ();if (head->next)
Since bpf_wakeup_sources_get_head() returns a BTF pointer, the verifier tracks it as PTR_TO_BTF_ID.
Because the verifier allows direct dereferences of trusted pointers (resulting in PROBE_MEM), will this program successfully load instead of failing with the "invalid mem access 'scalar'" error?
Quoted 4 lines
tools/testing/selftests/bpf/progs/wakeup_source_fail.c new file +0
-0return 1 ;return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Quoted 25 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.Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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 4 lines
The initial attempts for BPF traversal of
wakeup_sourceswas with BPF iterators [1]. However, BPF already allows for traversing of a simple list withbpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.
This looks good to me, you can add for the set:
Quoted 2 lines
[...]
Quoted 27 lines · Kumar Kartikeya Dwivedi
Quoted 25 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.Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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
Quoted 4 lines
The initial attempts for BPF traversal of
wakeup_sourceswas with BPF iterators [1]. However, BPF already allows for traversing of a simple list withbpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.This looks good to me, you can add for the set:
Rafael,
how do you want to route it?
If you ack it we can take it into bpf-next.
I'd think patch 1 shouldn't conflict with other 'wakeup' changes.
Quoted 35 lines · Alexei Starovoitov
Quoted 32 lines · Kumar Kartikeya Dwivedi
Quoted 29 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.Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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_sourceswas with BPF iterators [1]. However, BPF already allows for traversing of a simple list withbpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.
Quoted 7 lines
Quoted 2 lines
This looks good to me, you can add for the set:
Rafael,
how do you want to route it?If you ack it we can take it into bpf-next.
I guess if someone really wants this, I have no particular reason to object, so please feel free to add
to the first patch.
Quoted 1 line
I'd think patch 1 shouldn't conflict with other 'wakeup' changes.
Sure, there are none ATM anyway.
Thanks!
Quoted 49 lines · Rafael J. Wysocki
Quoted 35 lines · Alexei Starovoitov
Quoted 32 lines · Kumar Kartikeya Dwivedi
Quoted 29 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.Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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_sourceswas with BPF iterators [1]. However, BPF already allows for traversing of a simple list withbpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.Quoted 7 lines
Quoted 2 lines
This looks good to me, you can add for the set:
Rafael,
how do you want to route it?If you ack it we can take it into bpf-next.
I guess if someone really wants this, I have no particular reason to object, so please feel free to add
Quoted 7 lines
to the first patch.
Quoted 1 line
I'd think patch 1 shouldn't conflict with other 'wakeup' changes.
Sure, there are none ATM anyway.
Thanks!
Greg,
are you ok with it too?
I hear your api concerns, but imo it's a nice improvement. And bpf is not a stable interface despite some sceptics saying otherwise. Just see how much sched-ext api surface has changed.
So if pm:wakeup folks need to tweak it later they're certainly free to do it. Of course, it's better to keep things backward compact and deprecate gradually. All options in developer's hands.
Quoted 66 lines · Alexei Starovoitov
Quoted 49 lines · Rafael J. Wysocki
Quoted 35 lines · Alexei Starovoitov
Quoted 32 lines · Kumar Kartikeya Dwivedi
Quoted 29 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.Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list, and a kfunc to get head of wakeup sources list is needed to start traversing the list.
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_sourceswas with BPF iterators [1]. However, BPF already allows for traversing of a simple list withbpf_for(), and this current patchset has the added benefit of being ~2-3x more performant than BPF iterators.Quoted 7 lines
Quoted 2 lines
This looks good to me, you can add for the set:
Rafael,
how do you want to route it?If you ack it we can take it into bpf-next.
I guess if someone really wants this, I have no particular reason to object, so please feel free to add
Quoted 7 lines
to the first patch.
Quoted 1 line
I'd think patch 1 shouldn't conflict with other 'wakeup' changes.
Sure, there are none ATM anyway.
Thanks!
Greg,
are you ok with it too?
I hear your api concerns, but imo it's a nice improvement. And bpf is not a stable interface despite some sceptics saying otherwise. Just see how much sched-ext api surface has changed.
Quoted 3 lines
So if pm:wakeup folks need to tweak it later they're certainly free to do it. Of course, it's better to keep things backward compact and deprecate gradually. All options in developer's hands.
I still feel it's really odd to be wanting to iterate over a zillion sysfs files all at once, but sure, if this fixes the problem for this user, might as well take it...
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Mon, 11 May 2026 10:45:55 -0700 you wrote:
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.[...]
Here is the summary with links:
- [v4,1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources
https://git.kernel.org/bpf/bpf-next/c/5ff44955447e - [v4,2/2] selftests/bpf: Add tests for wakeup_sources kfuncs https://git.kernel.org/bpf/bpf-next/c/9ef647114201
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html