Skip to contentSkip to conversation

Add wakeup_source iterators

PATCH Superseded Samuel Wu 2 participants 5 messages Feb 11 – Feb 12

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Samuel Wu

author
#0 lore raw

v4 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 v4:
- Proper init of variables in ws_iter_check_sleep_times() test per bpf-ci
- Remove PM patch since it's already part of rebase
- v3 link: https://lore.kernel.org/all/[email protected]/

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 (2):
bpf: Add wakeup_source iterator
selftests/bpf: Add tests for wakeup_sources

 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 ++++
 5 files changed, 448 insertions(+)
 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.53.0.273.g2a3d683680-goog
author
#1 lore raw

PATCH v4 1/2 bpf: Add wakeup_source iterator

Add 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
index 79cf22860a99..1259373298e1 100644
@@ -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.53.0.273.g2a3d683680-goog
#2 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Add wakeup_source iterator

Should seq_stop invoke the BPF program with a NULL wakeup_source when iteration completes? Other BPF iterators follow this pattern:
Quoted 5 lines
kernel/bpf/wakeup_source_iter.c new file +0 -0
new file mode 100644
index 000000000000..ab83d212a1f9

[ ... ]

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 seq_stop invoke the BPF program with a NULL wakeup_source when iteration completes? Other BPF iterators follow this pattern:

kernel/bpf/map_iter.c:bpf_map_seq_stop():

    if (!v)
        (void)__bpf_map_seq_show(seq, v, true);

kernel/bpf/prog_iter.c:bpf_prog_seq_stop():

    if (!v)
        (void)__bpf_prog_seq_show(seq, v, true);

kernel/bpf/link_iter.c:bpf_link_seq_stop():

    if (!v)
        (void)__bpf_link_seq_show(seq, v, true);

The __wakeup_source_seq_show() function already accepts the in_stop parameter but is never called with in_stop=true from seq_stop. This would allow BPF programs to know when iteration is complete and perform any finalization (e.g., outputting summary statistics).

Quoted 6 lines
kernel/bpf/wakeup_source_iter.c new file +6 -0
+static int __wakeup_source_seq_show(struct seq_file *seq, void *v, bool in_stop)
+{
+	struct bpf_iter_meta meta = {
+		.seq = seq,
+	};
+	struct bpf_iter__wakeup_source ctx = {

kernel/bpf/wakeup_source_iter.c line 62

Quoted 10 lines
kernel/bpf/wakeup_source_iter.c new file +10 -0
+		.meta = &meta,
+		.wakeup_source = v,
+	};
+	struct bpf_prog *prog = bpf_iter_get_info(&meta, in_stop);
+
+	if (prog)
+		return bpf_iter_run_prog(prog, &ctx);
+
+	return 0;
+}

[ ... ]

---
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/21927983378

AI-authorship-score: low
AI-authorship-explanation: The code follows standard kernel BPF iterator patterns with no unusual characteristics suggesting AI generation.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Missing end-of-iteration callback is an API consistency issue, not a crash or security problem.

author
#3 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Add wakeup_source iterator

Quoted 29 lines · On Wed, Feb 11, 2026 at 4:19 PM <[email protected]> wrote:
Quoted 6 lines
kernel/bpf/wakeup_source_iter.c new file +0 -0
new file mode 100644
index 000000000000..ab83d212a1f9

[ ... ]

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 seq_stop invoke the BPF program with a NULL wakeup_source when iteration completes? Other BPF iterators follow this pattern:

kernel/bpf/map_iter.c:bpf_map_seq_stop():

    if (!v)
        (void)__bpf_map_seq_show(seq, v, true);

kernel/bpf/prog_iter.c:bpf_prog_seq_stop():

    if (!v)
        (void)__bpf_prog_seq_show(seq, v, true);
Quoted 8 lines

kernel/bpf/link_iter.c:bpf_link_seq_stop():

    if (!v)
        (void)__bpf_link_seq_show(seq, v, true);

The __wakeup_source_seq_show() function already accepts the in_stop parameter but is never called with in_stop=true from seq_stop. This would allow BPF programs to know when iteration is complete and perform any finalization (e.g., outputting summary statistics).

As noted in the v3 email thread, I don't see a current need for additional handling at the end of the wakeup_sources iterator.

Quoted 31 lines
Quoted 6 lines
kernel/bpf/wakeup_source_iter.c new file +6 -0
+static int __wakeup_source_seq_show(struct seq_file *seq, void *v, bool in_stop)
+{
+     struct bpf_iter_meta meta = {
+             .seq = seq,
+     };
+     struct bpf_iter__wakeup_source ctx = {

kernel/bpf/wakeup_source_iter.c line 62

Quoted 10 lines
kernel/bpf/wakeup_source_iter.c new file +10 -0
+             .meta = &meta,
+             .wakeup_source = v,
+     };
+     struct bpf_prog *prog = bpf_iter_get_info(&meta, in_stop);
+
+     if (prog)
+             return bpf_iter_run_prog(prog, &ctx);
+
+     return 0;
+}

[ ... ]

---
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/21927983378

AI-authorship-score: low
AI-authorship-explanation: The code follows standard kernel BPF iterator patterns with no unusual characteristics suggesting AI generation.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Missing end-of-iteration callback is an API consistency issue, not a crash or security problem.

author
#4 lore raw

PATCH v4 2/2 selftests/bpf: Add tests for wakeup_sources

In reply to Samuel Wu

Sets 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
index 558839e3c185..c12c5e04b81f 100644
@@ -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.53.0.273.g2a3d683680-goog