Add wakeup_source iterators
Subsystems (8)
v3 cover Add wakeup_source iterators
This patch series introduces BPF iterators for wakeup_source, enabling BPF programs to efficiently traverse a device's wakeup sources.
Currently, inspecting wakeup sources typically involves reading interfaces like /sys/class/wakeup/* or debugfs. The repeated syscalls to query the sysfs nodes is inefficient, as there can be hundreds of wakeup_sources, and each wakeup source have multiple stats, with one sysfs node per stat. debugfs is unstable and insecure.
The iterators utilize pre-existing wakeup_sources_walk_* functions to traverse over the SRCU that backs the list of wakeup_sources.
Changes in v3:
- Update wakeup_sources_walk_start() to handle an empty list per bpf-ci
- Simplify read of a struct's field in BPF program selftest per Andrii
- Drop open coded iterators for wakeup_sources
- Fix condition from !get_ws_iter_stream to get_ws_iter_stream in selftest
- Read event_count instead of wakeup_count in selftest
- v2 link: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Guard BPF Makefile with CONFIG_PM_SLEEP to fix build errors
- Update copyright from 2025 to 2026
- v1 link: https://lore.kernel.org/all/[email protected]/
Samuel Wu (3):
PM: wakeup: Handle empty list in wakeup_sources_walk
bpf: Add wakeup_source iterator selftests/bpf: Add tests for wakeup_sources
drivers/base/power/wakeup.c | 4 +- kernel/bpf/Makefile | 3 + kernel/bpf/wakeup_source_iter.c | 103 +++++++ tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/wakeup_source_iter.c | 281 ++++++++++++++++++ .../selftests/bpf/progs/wakeup_source_iter.c | 60 ++++ 6 files changed, 449 insertions(+), 3 deletions(-) create mode 100644 kernel/bpf/wakeup_source_iter.c create mode 100644 tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source_iter.c
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v3 1/3 PM: wakeup: Handle empty list in wakeup_sources_walk
In the case of an empty wakeup_sources list, wakeup_sources_walk_start() will return an invalid but non-NULL address. This also affects wrappers of the aforementioned function, like for_each_wakeup_source().
This patch updates wakeup_sources_walk_start() to return NULL in case of an empty list.
--- drivers/base/power/wakeup.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/power/wakeup.c +1 -3
@@ -275,9 +275,7 @@ EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);*/ struct wakeup_source *wakeup_sources_walk_start (void ) { -struct list_head *ws_head = &wakeup_sources; - - return list_entry_rcu(ws_head->next, struct wakeup_source, entry); + return list_first_or_null_rcu(&wakeup_sources, struct wakeup_source, entry); }EXPORT_SYMBOL_GPL (wakeup_sources_walk_start);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Rafael J. Wysocki
maintainerRe: [PATCH bpf-next v3 1/3] PM: wakeup: Handle empty list in wakeup_sources_walk
Quoted 19 lines · Samuel Wu
In the case of an empty
wakeup_sourceslist,wakeup_sources_walk_start()will return an invalid but non-NULL address. This also affects wrappers of the aforementioned function, likefor_each_wakeup_source().This patch updates
wakeup_sources_walk_start()to return NULL in case of an empty list.Signed-off-by Samuel Wu <[email protected]>--- drivers/base/power/wakeup.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/base/power/wakeup.c +0
-0@@ -275,9 +275,7 @@ EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);*/
drivers/base/power/wakeup.c EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);
Quoted 10 lines
drivers/base/power/wakeup.c +1
-4struct wakeup_source *wakeup_sources_walk_start (void ) { -struct list_head *ws_head = &wakeup_sources; - - return list_entry_rcu(ws_head->next, struct wakeup_source, entry); + return list_first_or_null_rcu(&wakeup_sources, struct wakeup_source, entry); }EXPORT_SYMBOL_GPL (wakeup_sources_walk_start); --
This looks like a fix for the for_each_wakeup_source() users.
I can apply it without the next two patches.
Re: [PATCH bpf-next v3 1/3] PM: wakeup: Handle empty list in wakeup_sources_walk
Quoted 30 lines · Rafael J. Wysocki
Quoted 28 lines · Samuel Wu
In the case of an empty
wakeup_sourceslist,wakeup_sources_walk_start()will return an invalid but non-NULL address. This also affects wrappers of the aforementioned function, likefor_each_wakeup_source().This patch updates
wakeup_sources_walk_start()to return NULL in case of an empty list.Signed-off-by Samuel Wu <[email protected]>--- drivers/base/power/wakeup.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/base/power/wakeup.c +1
-3@@ -275,9 +275,7 @@ EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);*/ struct wakeup_source *wakeup_sources_walk_start (void ) { -struct list_head *ws_head = &wakeup_sources; - - return list_entry_rcu(ws_head->next, struct wakeup_source, entry); + return list_first_or_null_rcu(&wakeup_sources, struct wakeup_source, entry); }EXPORT_SYMBOL_GPL (wakeup_sources_walk_start);
Quoted 5 lines
Quoted 1 line
--
This looks like a fix for the
for_each_wakeup_source()users.I can apply it without the next two patches.
Yes please. Feel free to apply this patch independently, as it can exist as a standalone fix independent of the subsequent two patches in the series.
Thanks!
Rafael J. Wysocki
maintainerRe: [PATCH bpf-next v3 1/3] PM: wakeup: Handle empty list in wakeup_sources_walk
Quoted 36 lines · Samuel Wu
Quoted 34 lines · Rafael J. Wysocki
Quoted 19 lines · Samuel Wu
In the case of an empty
wakeup_sourceslist,wakeup_sources_walk_start()will return an invalid but non-NULL address. This also affects wrappers of the aforementioned function, likefor_each_wakeup_source().This patch updates
wakeup_sources_walk_start()to return NULL in case of an empty list.Signed-off-by Samuel Wu <[email protected]>--- drivers/base/power/wakeup.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/base/power/wakeup.c +0
-0@@ -275,9 +275,7 @@ EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);*/ drivers/base/power/wakeup.c EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);
Quoted 10 lines
drivers/base/power/wakeup.c +1
-4struct wakeup_source *wakeup_sources_walk_start (void ) { -struct list_head *ws_head = &wakeup_sources; - - return list_entry_rcu(ws_head->next, struct wakeup_source, entry); + return list_first_or_null_rcu(&wakeup_sources, struct wakeup_source, entry); }EXPORT_SYMBOL_GPL (wakeup_sources_walk_start); --This looks like a fix for the
for_each_wakeup_source()users.
Quoted 5 lines
Quoted 1 line
I can apply it without the next two patches.
Yes please. Feel free to apply this patch independently, as it can exist as a standalone fix independent of the subsequent two patches in the series.
OK, applied as 6.20/7.0 material, thanks!
PATCH v3 2/3 bpf: Add wakeup_source iterator
In reply to Samuel WuAdd a BPF iterator for traversing through wakeup_sources.
Setup iterators to traverse through a SRCUs of wakeup_sources. This is a more elegant and efficient traversal than going through the options today, such as at /sys/class/wakeup, or through debugfs.
--- kernel/bpf/Makefile | 3 + kernel/bpf/wakeup_source_iter.c | 103 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 kernel/bpf/wakeup_source_iter.c
kernel/bpf/Makefile +3 -0
@@ -66,6 +66,9 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o ifeq ($(CONFIG_DMA_SHARED_BUFFER),y) obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o endif +ifeq ($(CONFIG_PM_SLEEP),y) +obj-$(CONFIG_BPF_SYSCALL) += wakeup_source_iter.o +endif CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
kernel/bpf/wakeup_source_iter.c new file +103 -0
103 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
[email protected]
AI reviewRe: [PATCH bpf-next v3 2/3] bpf: Add wakeup_source iterator
Should this function call __wakeup_source_seq_show(seq, v, true) when v is NULL to notify the BPF program that iteration has completed?
Quoted 5 lines
kernel/bpf/wakeup_source_iter.c new file +0
-0
[ ... ]
kernel/bpf/wakeup_source_iter.c line 45
Quoted 8 lines
kernel/bpf/wakeup_source_iter.c new file +8
-0+static void wakeup_source_iter_seq_stop (struct seq_file *seq,void *v) +{ +int *srcuidx = seq->private; + +if (*srcuidx >=0 ) +wakeup_sources_read_unlock (*srcuidx); + *srcuidx =-1 ; +}
Should this function call __wakeup_source_seq_show(seq, v, true) when v is NULL to notify the BPF program that iteration has completed?
Looking at other BPF iterators like map_iter, prog_iter, link_iter, task_iter, and kmem_cache_iter, they all invoke the BPF program with in_stop=true when the stop callback receives a NULL pointer. For example, map_iter does:
static void bpf_map_seq_stop(struct seq_file *seq, void *v)
{
if (!v)
(void)__bpf_map_seq_show(seq, v, true);
...
}The bpf_seq_read() function in kernel/bpf/bpf_iter.c has this comment at the stop label: "/* bpf program called if !p */", which suggests this callback is expected.
The __wakeup_source_seq_show() function already accepts an in_stop parameter but is never called with true. Without this callback, BPF programs using this iterator cannot detect when iteration has finished.
---
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/21306880863
Re: [PATCH bpf-next v3 2/3] bpf: Add wakeup_source iterator
Quoted 9 lines · On Fri, Jan 23, 2026 at 5:50 PM <[email protected]> wrote:
Quoted 6 lines
kernel/bpf/wakeup_source_iter.c new file +0
-0[ ... ]
Quoted 11 lines
kernel/bpf/wakeup_source_iter.c line 45
Quoted 8 lines
kernel/bpf/wakeup_source_iter.c new file +8
-0+static void wakeup_source_iter_seq_stop (struct seq_file *seq,void *v) +{ +int *srcuidx = seq->private; + +if (*srcuidx >=0 ) +wakeup_sources_read_unlock (*srcuidx); + *srcuidx =-1 ; +}Should this function call
__wakeup_source_seq_show(seq, v, true)when v is NULL to notify the BPF program that iteration has completed?
My understanding is that above code is invoked as a N+1 case after iterating through a list of size N. If desired behavior is to do additional cleanup not already covered by _seq_stop() (e.g. printing closing footers), then v == NULL should be handled. However, there is no such need for wakeup_source_iter at the moment.
Quoted 13 lines
Looking at other BPF iterators like
map_iter,prog_iter,link_iter,task_iter, andkmem_cache_iter, they all invoke the BPF program with in_stop=true when the stop callback receives a NULL pointer. For example,map_iterdoes:static void bpf_map_seq_stop(struct seq_file *seq, void *v) { if (!v) (void)__bpf_map_seq_show(seq, v, true); ... }
Quoted 8 lines
The
bpf_seq_read()function inkernel/bpf/bpf_iter.chas this comment at the stop label: "/* bpf program called if !p */", which suggests this callback is expected.The
__wakeup_source_seq_show()function already accepts anin_stopparameter but is never called with true. Without this callback, BPF programs using this iterator cannot detect when iteration has finished.
If I understand correctly, this is not mandatory. For a straightforward iterator like this one, I'd like to keep it simple and not unnecessarily handle this case.
Quoted 6 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21306880863
PATCH v3 3/3 selftests/bpf: Add tests for wakeup_sources
In reply to Samuel WuSets up the framework to test wakeup_sources iterators using BPF, and adds a few basic tests.
Adds several helper functions that for grabbing and releasing a wakelock, abstracting out key functions to setup a framework for testing wakeup_sources.
Additionally, adds 3 tests:
1. check_active_count: Checks that stats related to active_count are properly set after several lock/unlock cycles
2. check_sleep_times: Checks that time accounting related to sleep are properly calculated
3. check_no_infinite_reads: Checks that the iterator traversal returns NULL at the end
--- tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/wakeup_source_iter.c | 281 ++++++++++++++++++ .../selftests/bpf/progs/wakeup_source_iter.c | 60 ++++ 3 files changed, 342 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source_iter.c
tools/testing/selftests/bpf/config +1 -0
@@ -111,6 +111,7 @@ CONFIG_IP6_NF_IPTABLES=y CONFIG_IP6_NF_FILTER=y CONFIG_NF_NAT=y CONFIG_PACKET=y +CONFIG_PM_WAKELOCKS=y CONFIG_RC_CORE=y CONFIG_SAMPLES=y CONFIG_SAMPLE_LIVEPATCH=m
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +281 -0
281 lines not on this page · raw message
tools/testing/selftests/bpf/progs/wakeup_source_iter.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
[email protected]
AI reviewRe: [PATCH bpf-next v3 3/3] selftests/bpf: Add tests for wakeup_sources
If bpf_iter_create() fails above, get_ws_iter_stream() returns -1 without writing to *iter_file.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +0
-0
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +4
-0+static int get_ws_iter_stream (struct wakeup_source_iter *skel,int *iter_fd, +FILE **iter_file) +{ + *iter_fd = bpf_iter_create(
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 146
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +10
-0+ bpf_link__fd(skel->links.wakeup_source_collector)); +if (!ASSERT_OK_FD (*iter_fd,"iter_create" )) +return -1 ; + + *iter_file =fdopen (*iter_fd,"r" ); +if (!ASSERT_OK_PTR (*iter_file,"fdopen" )) +return -1 ; + +return 0 ; +}
If bpf_iter_create() fails above, get_ws_iter_stream() returns -1 without writing to *iter_file.
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +2
-0+static void subtest_ws_iter_check_active_count (struct wakeup_source_iter *skel) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 156
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +4
-0+char *line =NULL ; +bool found_ws = false; +FILE *iter_file =NULL ; +int iter_fd =-1 ;
This function correctly initializes iter_file and iter_fd before calling get_ws_iter_stream().
[ ... ]
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 195
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +9
-0+} + +static void subtest_ws_iter_check_sleep_times (struct wakeup_source_iter *skel) +{ +bool found_test_ws = false; +struct WakeupSourceInfo ws_info; +char *line =NULL ; +FILE *iter_file; +int iter_fd;
In contrast to subtest_ws_iter_check_active_count(), iter_file and iter_fd are not initialized here.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +3
-0+ +if (get_ws_iter_stream (skel, &iter_fd, &iter_file)) + goto cleanup;
[ ... ]
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 187
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +6
-0+cleanup: +if (iter_file) +fclose (iter_file); +else if (iter_fd >=0 ) +close (iter_fd); +}
If get_ws_iter_stream() fails at bpf_iter_create(), can this access uninitialized iter_file and iter_fd? The cleanup code would then evaluate garbage values, potentially calling fclose() with an invalid pointer.
---
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/21306880863
Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add tests for wakeup_sources
Quoted 18 lines · On Fri, Jan 23, 2026 at 5:50 PM <[email protected]> wrote:
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +0
-0[ ... ]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +9
-0+static int get_ws_iter_stream (struct wakeup_source_iter *skel,int *iter_fd, +FILE **iter_file) +{ + *iter_fd =bpf_iter_create ( +bpf_link__fd (skel->links.wakeup_source_collector)); +if (!ASSERT_OK_FD (*iter_fd,"iter_create" )) +return -1 ; + + *iter_file =fdopen (*iter_fd,"r" );
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 146
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +5
-0+if (!ASSERT_OK_PTR (*iter_file,"fdopen" )) +return -1 ; + +return 0 ; +}If
bpf_iter_create()fails above,get_ws_iter_stream()returns -1 without writing to*iter_file.
This should be fine, if the BPF iter file isn't created, we will not write to that file.
Quoted 15 lines
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +2
-0+static void subtest_ws_iter_check_active_count (struct wakeup_source_iter *skel) +{[ ... ]
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 156
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +4
-0+char *line =NULL ; +bool found_ws = false; +FILE *iter_file =NULL ; +int iter_fd =-1 ;This function correctly initializes
iter_fileanditer_fdbefore callingget_ws_iter_stream().[ ... ]
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 195
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +9
-0+} + +static void subtest_ws_iter_check_sleep_times (struct wakeup_source_iter *skel) +{ +bool found_test_ws = false; +struct WakeupSourceInfo ws_info; +char *line =NULL ; +FILE *iter_file; +int iter_fd;In contrast to
subtest_ws_iter_check_active_count(),iter_fileanditer_fdare not initialized here.
Ack, this is important and I can address it in the v4 of the patch.
Quoted 6 lines
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +3
-0+ +if (get_ws_iter_stream (skel, &iter_fd, &iter_file)) + goto cleanup;[ ... ]
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c line 187
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file +6
-0+cleanup: +if (iter_file) +fclose (iter_file); +else if (iter_fd >=0 ) +close (iter_fd); +}If
get_ws_iter_stream()fails atbpf_iter_create(), can this access uninitializediter_fileanditer_fd? The cleanup code would then evaluate garbage values, potentially callingfclose()with an invalid pointer.
Correct, and this issue will be fixed when the variables are initialized as pointed out in the previous comment.
Quoted 6 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21306880863