Skip to contentSkip to conversation

bpf: Add wakeup_source iterators

PATCH Superseded Samuel Wu 2 participants 7 messages Dec 4, 2025 – Dec 5, 2025

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Samuel Wu

author
#0 lore raw

cover bpf: 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.

This series implements two types of iterators:
1. Standard BPF Iterator: Allows creating a BPF link to iterate over wakeup sources
2. Open-coded Iterator: Enables the use of wakeup_source iterators directly within BPF programs

Both iterators utilize pre-existing APIs wakeup_sources_walk_* to traverse over the SRCU that backs the list of wakeup_sources.

Samuel Wu (4):
bpf: Add wakeup_source iterator
bpf: Open coded BPF for wakeup_sources selftests/bpf: Add tests for wakeup_sources selftests/bpf: Open coded BPF wakeup_sources test

 kernel/bpf/Makefile                           |   1 +
 kernel/bpf/helpers.c                          |   3 +
 kernel/bpf/wakeup_source_iter.c               | 137 ++++++++
 .../testing/selftests/bpf/bpf_experimental.h  |   5 +
 tools/testing/selftests/bpf/config            |   1 +
 .../bpf/prog_tests/wakeup_source_iter.c       | 323 ++++++++++++++++++
 .../selftests/bpf/progs/wakeup_source_iter.c  | 117 +++++++
 7 files changed, 587 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.52.0.177.g9f829587af-goog
author
#1 lore raw

PATCH 1/4 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             |   1 +
 kernel/bpf/wakeup_source_iter.c | 103 ++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)
 create mode 100644 kernel/bpf/wakeup_source_iter.c
kernel/bpf/Makefile +1 -0
index 232cbc97434d..6a479982469a 100644
@@ -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)
kernel/bpf/wakeup_source_iter.c new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.52.0.177.g9f829587af-goog
#2 lore raw

Re: [PATCH v1 1/4] bpf: Add wakeup_source iterator

kernel test robot noticed the following build errors:

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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

Re: [PATCH v1 1/4] bpf: Add wakeup_source iterator

In reply to Samuel Wu
kernel test robot noticed the following build errors:

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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#4 lore raw

PATCH 2/4 bpf: Open coded BPF for wakeup_sources

In reply to Samuel Wu

Add open coded BPF iterators for wakeup_sources, which opens up more options for BPF programs that need to traverse through wakeup_sources.

---
 kernel/bpf/helpers.c            |  3 +++
 kernel/bpf/wakeup_source_iter.c | 34 +++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
kernel/bpf/helpers.c +3 -0
index db72b96f9c8c..a5f867de6bd6 100644
@@ -4518,6 +4518,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 #endif
+BTF_ID_FLAGS(func, bpf_iter_wakeup_source_new, KF_ITER_NEW | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_wakeup_source_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_iter_wakeup_source_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, __bpf_trap)
 BTF_ID_FLAGS(func, bpf_strcmp);
 BTF_ID_FLAGS(func, bpf_strcasecmp);
kernel/bpf/wakeup_source_iter.c +34 -0
index b8719f47428e..e2c0dcbfd02d 100644
@@ -90,6 +90,40 @@ static struct bpf_iter_reg bpf_wakeup_source_reg_info = {
 	.seq_info		= &wakeup_source_iter_seq_info,
 };
 
+struct bpf_iter_wakeup_source {
+	struct wakeup_source *ws;
+	int srcuidx;
+};
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc int bpf_iter_wakeup_source_new(struct bpf_iter_wakeup_source *it)
+{
+	it->srcuidx = wakeup_sources_read_lock();
+	it->ws = wakeup_sources_walk_start();
+
+	return 0;
+}
+
+__bpf_kfunc struct wakeup_source *bpf_iter_wakeup_source_next(struct bpf_iter_wakeup_source *it)
+{
+	struct wakeup_source *prev = it->ws;
+
+	if (!prev)
+		return NULL;
+
+	it->ws = wakeup_sources_walk_next(it->ws);
+
+	return prev;
+}
+
+__bpf_kfunc void bpf_iter_wakeup_source_destroy(struct bpf_iter_wakeup_source *it)
+{
+	wakeup_sources_read_unlock(it->srcuidx);
+}
+
+__bpf_kfunc_end_defs();
+
 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)
Signature · 1 line
2.52.0.177.g9f829587af-goog
author
#5 lore raw

PATCH 3/4 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  |  70 +++++
 3 files changed, 352 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 +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.52.0.177.g9f829587af-goog
author
#6 lore raw

PATCH 4/4 selftests/bpf: Open coded BPF wakeup_sources test

In reply to Samuel Wu

This commit introduces a new selftest for the BPF wakeup_source iterator to verify the functionality of open-coded iteration.

The test adds:
- A new BPF map test_ws_hash to track iterated wakeup source names.
- A BPF program iter_ws_for_each that iterates over wakeup sources and updates the test_ws_hash map with the names of found sources.
- A new subtest subtest_ws_iter_check_open_coded to trigger the BPF program and assert that the expected wakeup sources are marked in the map.

---
 .../testing/selftests/bpf/bpf_experimental.h  |  5 ++
 .../bpf/prog_tests/wakeup_source_iter.c       | 42 +++++++++++++++++
 .../selftests/bpf/progs/wakeup_source_iter.c  | 47 +++++++++++++++++++
 3 files changed, 94 insertions(+)
tools/testing/selftests/bpf/bpf_experimental.h +5 -0
index 2cd9165c7348..e532999b91ca 100644
@@ -598,6 +598,11 @@ extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym;
 
 extern int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
 				 struct bpf_dynptr *value_p) __weak __ksym;
+struct bpf_iter_wakeup_source;
+extern int bpf_iter_wakeup_source_new(struct bpf_iter_wakeup_source *it) __weak __ksym;
+extern struct wakeup_source *bpf_iter_wakeup_source_next(
+		struct bpf_iter_wakeup_source *it) __weak __ksym;
+extern void bpf_iter_wakeup_source_destroy(struct bpf_iter_wakeup_source *it) __weak __ksym;
 
 #define PREEMPT_BITS	8
 #define SOFTIRQ_BITS	8
tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c +42 -0

65 lines not on this page · raw message

tools/testing/selftests/bpf/progs/wakeup_source_iter.c +47 -0

56 lines not on this page · raw message

Signature · 1 line
2.52.0.177.g9f829587af-goog