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.
Signed-off-by: Samuel Wu <wusamuel@google.com>
---
kernel/bpf/Makefile | 1 +
kernel/bpf/wakeup_source_iter.c | 103 ++++++++++++++++++++++++++++++++
2 files changed, 104 insertions(+)
create mode 100644 kernel/bpf/wakeup_source_iter.c
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 232cbc97434d..6a479982469a 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
endif
+obj-$(CONFIG_BPF_SYSCALL) += wakeup_source_iter.o
CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
diff --git a/kernel/bpf/wakeup_source_iter.c b/kernel/bpf/wakeup_source_iter.c
new file mode 100644
index 000000000000..b8719f47428e
--- /dev/null
+++ b/kernel/bpf/wakeup_source_iter.c
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Google LLC */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/kernel.h>
+#include <linux/pm_wakeup.h>
+#include <linux/seq_file.h>
+
+struct bpf_iter__wakeup_source {
+ __bpf_md_ptr(struct bpf_iter_meta *, meta);
+ __bpf_md_ptr(struct wakeup_source *, wakeup_source);
+};
+
+static void *wakeup_source_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+ int *srcuidx = seq->private;
+ struct wakeup_source *ws;
+ loff_t i;
+
+ *srcuidx = wakeup_sources_read_lock();
+
+ ws = wakeup_sources_walk_start();
+ for (i = 0; ws && i < *pos; i++)
+ ws = wakeup_sources_walk_next(ws);
+
+ return ws;
+}
+
+static void *wakeup_source_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+ struct wakeup_source *ws = v;
+
+ ++*pos;
+
+ return wakeup_sources_walk_next(ws);
+}
+
+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;
+}
+
+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 = {
+ .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;
+}
+
+static int wakeup_source_iter_seq_show(struct seq_file *seq, void *v)
+{
+ return __wakeup_source_seq_show(seq, v, false);
+}
+
+static const struct seq_operations wakeup_source_iter_seq_ops = {
+ .start = wakeup_source_iter_seq_start,
+ .next = wakeup_source_iter_seq_next,
+ .stop = wakeup_source_iter_seq_stop,
+ .show = wakeup_source_iter_seq_show,
+};
+
+static const struct bpf_iter_seq_info wakeup_source_iter_seq_info = {
+ .seq_ops = &wakeup_source_iter_seq_ops,
+ .seq_priv_size = sizeof(int),
+};
+
+static struct bpf_iter_reg bpf_wakeup_source_reg_info = {
+ .target = "wakeup_source",
+ .ctx_arg_info_size = 1,
+ .ctx_arg_info = {
+ {
+ offsetof(struct bpf_iter__wakeup_source, wakeup_source),
+ PTR_TO_BTF_ID_OR_NULL
+ },
+ },
+ .seq_info = &wakeup_source_iter_seq_info,
+};
+
+DEFINE_BPF_ITER_FUNC(wakeup_source, struct bpf_iter_meta *meta,
+ struct wakeup_source *wakeup_source)
+BTF_ID_LIST_SINGLE(bpf_wakeup_source_btf_id, struct, wakeup_source)
+
+static int __init wakeup_source_iter_init(void)
+{
+ bpf_wakeup_source_reg_info.ctx_arg_info[0].btf_id = bpf_wakeup_source_btf_id[0];
+ return bpf_iter_reg_target(&bpf_wakeup_source_reg_info);
+}
+
+late_initcall(wakeup_source_iter_init);
--
2.52.0.177.g9f829587af-goog
Hi Samuel,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master shuah-kselftest/next shuah-kselftest/fixes linus/master v6.18 next-20251204]
[cannot apply to bpf-next/net]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Samuel-Wu/bpf-Add-wakeup_source-iterator/20251204-111108
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link: https://lore.kernel.org/r/20251204025003.3162056-2-wusamuel%40google.com
patch subject: [PATCH v1 1/4] bpf: Add wakeup_source iterator
config: um-randconfig-r052-20251205 (https://download.01.org/0day-ci/archive/20251205/202512051019.IljjrKRb-lkp@intel.com/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251205/202512051019.IljjrKRb-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512051019.IljjrKRb-lkp@intel.com/
All error/warnings (new ones prefixed by >>):
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_start':
>> kernel/bpf/wakeup_source_iter.c:20:20: error: implicit declaration of function 'wakeup_sources_read_lock'; did you mean 'wakeup_source_register'? [-Werror=implicit-function-declaration]
20 | *srcuidx = wakeup_sources_read_lock();
| ^~~~~~~~~~~~~~~~~~~~~~~~
| wakeup_source_register
>> kernel/bpf/wakeup_source_iter.c:22:14: error: implicit declaration of function 'wakeup_sources_walk_start'; did you mean 'wakeup_source_register'? [-Werror=implicit-function-declaration]
22 | ws = wakeup_sources_walk_start();
| ^~~~~~~~~~~~~~~~~~~~~~~~~
| wakeup_source_register
>> kernel/bpf/wakeup_source_iter.c:22:12: warning: assignment to 'struct wakeup_source *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
22 | ws = wakeup_sources_walk_start();
| ^
>> kernel/bpf/wakeup_source_iter.c:24:22: error: implicit declaration of function 'wakeup_sources_walk_next' [-Werror=implicit-function-declaration]
24 | ws = wakeup_sources_walk_next(ws);
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/wakeup_source_iter.c:24:20: warning: assignment to 'struct wakeup_source *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
24 | ws = wakeup_sources_walk_next(ws);
| ^
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_next':
>> kernel/bpf/wakeup_source_iter.c:35:16: warning: returning 'int' from a function with return type 'void *' makes pointer from integer without a cast [-Wint-conversion]
35 | return wakeup_sources_walk_next(ws);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_stop':
>> kernel/bpf/wakeup_source_iter.c:43:17: error: implicit declaration of function 'wakeup_sources_read_unlock' [-Werror=implicit-function-declaration]
43 | wakeup_sources_read_unlock(*srcuidx);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
vim +20 kernel/bpf/wakeup_source_iter.c
13
14 static void *wakeup_source_iter_seq_start(struct seq_file *seq, loff_t *pos)
15 {
16 int *srcuidx = seq->private;
17 struct wakeup_source *ws;
18 loff_t i;
19
> 20 *srcuidx = wakeup_sources_read_lock();
21
> 22 ws = wakeup_sources_walk_start();
23 for (i = 0; ws && i < *pos; i++)
> 24 ws = wakeup_sources_walk_next(ws);
25
26 return ws;
27 }
28
29 static void *wakeup_source_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
30 {
31 struct wakeup_source *ws = v;
32
33 ++*pos;
34
> 35 return wakeup_sources_walk_next(ws);
36 }
37
38 static void wakeup_source_iter_seq_stop(struct seq_file *seq, void *v)
39 {
40 int *srcuidx = seq->private;
41
42 if (*srcuidx >= 0)
> 43 wakeup_sources_read_unlock(*srcuidx);
44 *srcuidx = -1;
45 }
46
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi Samuel,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master shuah-kselftest/next shuah-kselftest/fixes linus/master v6.18 next-20251204]
[cannot apply to bpf-next/net]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Samuel-Wu/bpf-Add-wakeup_source-iterator/20251204-111108
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link: https://lore.kernel.org/r/20251204025003.3162056-2-wusamuel%40google.com
patch subject: [PATCH v1 1/4] bpf: Add wakeup_source iterator
config: um-randconfig-001-20251205 (https://download.01.org/0day-ci/archive/20251205/202512050548.YLgRm659-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251205/202512050548.YLgRm659-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512050548.YLgRm659-lkp@intel.com/
All errors (new ones prefixed by >>):
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_start':
kernel/bpf/wakeup_source_iter.c:20:20: error: implicit declaration of function 'wakeup_sources_read_lock'; did you mean 'wakeup_source_register'? [-Wimplicit-function-declaration]
20 | *srcuidx = wakeup_sources_read_lock();
| ^~~~~~~~~~~~~~~~~~~~~~~~
| wakeup_source_register
kernel/bpf/wakeup_source_iter.c:22:14: error: implicit declaration of function 'wakeup_sources_walk_start'; did you mean 'wakeup_source_register'? [-Wimplicit-function-declaration]
22 | ws = wakeup_sources_walk_start();
| ^~~~~~~~~~~~~~~~~~~~~~~~~
| wakeup_source_register
>> kernel/bpf/wakeup_source_iter.c:22:12: error: assignment to 'struct wakeup_source *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
22 | ws = wakeup_sources_walk_start();
| ^
kernel/bpf/wakeup_source_iter.c:24:22: error: implicit declaration of function 'wakeup_sources_walk_next' [-Wimplicit-function-declaration]
24 | ws = wakeup_sources_walk_next(ws);
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/wakeup_source_iter.c:24:20: error: assignment to 'struct wakeup_source *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
24 | ws = wakeup_sources_walk_next(ws);
| ^
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_next':
>> kernel/bpf/wakeup_source_iter.c:35:16: error: returning 'int' from a function with return type 'void *' makes pointer from integer without a cast [-Wint-conversion]
35 | return wakeup_sources_walk_next(ws);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/wakeup_source_iter.c: In function 'wakeup_source_iter_seq_stop':
kernel/bpf/wakeup_source_iter.c:43:17: error: implicit declaration of function 'wakeup_sources_read_unlock' [-Wimplicit-function-declaration]
43 | wakeup_sources_read_unlock(*srcuidx);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
vim +22 kernel/bpf/wakeup_source_iter.c
13
14 static void *wakeup_source_iter_seq_start(struct seq_file *seq, loff_t *pos)
15 {
16 int *srcuidx = seq->private;
17 struct wakeup_source *ws;
18 loff_t i;
19
20 *srcuidx = wakeup_sources_read_lock();
21
> 22 ws = wakeup_sources_walk_start();
23 for (i = 0; ws && i < *pos; i++)
24 ws = wakeup_sources_walk_next(ws);
25
26 return ws;
27 }
28
29 static void *wakeup_source_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
30 {
31 struct wakeup_source *ws = v;
32
33 ++*pos;
34
> 35 return wakeup_sources_walk_next(ws);
36 }
37
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.