Skip to contentSkip to conversation

Support BPF traversal of wakeup sources

PATCH Superseded Samuel Wu 4 participants 9 messages Mar 26

Subsystems (7)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Samuel Wu

author
#0 lore raw

v2 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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 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                   |  72 +++++++++++-
 tools/testing/selftests/bpf/config            |   3 +-
 .../selftests/bpf/prog_tests/wakeup_source.c  | 101 +++++++++++++++++
 .../selftests/bpf/progs/test_wakeup_source.c  | 107 ++++++++++++++++++
 .../selftests/bpf/progs/wakeup_source.h       |  22 ++++
 .../selftests/bpf/progs/wakeup_source_fail.c  |  63 +++++++++++
 7 files changed, 372 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.53.0.1018.g2bb0e51243-goog
author
#1 lore raw

PATCH v2 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.

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 | 72 +++++++++++++++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 2 deletions(-)
drivers/base/power/power.h +7 -0
index 922ed457db19..ced563286ac5 100644
@@ -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);
+struct list_head *bpf_wakeup_sources_get_head(void);
+#endif
drivers/base/power/wakeup.c +70 -2

81 lines not on this page · raw message

Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#2 lore raw

Re: [PATCH v2 1/2] PM: wakeup: Add kfuncs to traverse over wakeup_sources

Quoted 76 lines · Samuel Wu

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.

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 | 72 +++++++++++++++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 2 deletions(-)
drivers/base/power/power.h +7 -0
index 922ed457db19..ced563286ac5 100644
@@ -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);
+struct list_head *bpf_wakeup_sources_get_head(void);
+#endif
drivers/base/power/wakeup.c +33 -1
index b8e48a023bf0..27a26a30b6ee 100644
@@ -1168,11 +1168,79 @@ static const struct file_operations wakeup_sources_stats_fops = {
 	.release = seq_release_private,
 };
 
-static int __init wakeup_sources_debugfs_init(void)
+#ifdef CONFIG_BPF_SYSCALL
+#include <linux/btf.h>
+
+__bpf_kfunc_start_defs();
+
+/**
+ * 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
+ * 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);
+}
+
+/**
+ * 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);
+}

drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1213

Quoted 10 lines
drivers/base/power/wakeup.c +10 -0
+
+/**
+ * bpf_wakeup_sources_get_head - Get the head of the wakeup sources list
+ *
+ * Return: The head of the wakeup sources list.
+ */
+__bpf_kfunc struct list_head *bpf_wakeup_sources_get_head(void)
+{
+	return &wakeup_sources;
+}

What stops a bpf program from using this kfunc to get the wakeup_sources pointer and iterating this list without taking the srcu lock?

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

drivers/base/power/wakeup.c static const struct file_operations wakeup_sources_stats_fops = { · line 1218

Quoted 5 lines
drivers/base/power/wakeup.c +5 -0
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(wakeup_source_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_wakeup_sources_read_lock, KF_ACQUIRE)

I think this needs KF_RET_NULL as well.

Quoted 31 lines
drivers/base/power/wakeup.c +22 -1
+BTF_ID_FLAGS(func, bpf_wakeup_sources_read_unlock, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_wakeup_sources_get_head)
+BTF_KFUNCS_END(wakeup_source_kfunc_ids)
+
+static const struct btf_kfunc_id_set wakeup_source_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set   = &wakeup_source_kfunc_ids,
+};
+
+static void __init wakeup_sources_bpf_init(void)
+{
+	if (register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &wakeup_source_kfunc_set))
+		pm_pr_dbg("Wakeup: failed to register BTF kfuncs\n");
+}
+#else
+static inline void wakeup_sources_bpf_init(void) {}
+#endif /* CONFIG_BPF_SYSCALL */
+
+static int __init wakeup_sources_init(void)
 {
 	debugfs_create_file("wakeup_sources", 0444, NULL, NULL,
 			    &wakeup_sources_stats_fops);
+	wakeup_sources_bpf_init();
+
 	return 0;
 }
 
-postcore_initcall(wakeup_sources_debugfs_init);
+postcore_initcall(wakeup_sources_init);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
author
#3 lore raw

PATCH v2 2/2 selftests/bpf: Add tests for wakeup_sources kfuncs

In reply to Samuel Wu

Introduce 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  | 101 +++++++++++++++++
 .../selftests/bpf/progs/test_wakeup_source.c  | 107 ++++++++++++++++++
 .../selftests/bpf/progs/wakeup_source.h       |  22 ++++
 .../selftests/bpf/progs/wakeup_source_fail.c  |  63 +++++++++++
 5 files changed, 295 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
index 24855381290d..bac60b444551 100644
@@ -130,4 +130,5 @@ CONFIG_INFINIBAND=y
 CONFIG_SMC=y
 CONFIG_SMC_HS_CTRL_BPF=y
 CONFIG_DIBS=y
-CONFIG_DIBS_LO=y
\ No newline at end of file
+CONFIG_DIBS_LO=y
+CONFIG_PM_WAKELOCKS=y
tools/testing/selftests/bpf/prog_tests/wakeup_source.c new file +101 -0

101 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_wakeup_source.c new file +107 -0

107 lines not on this page · raw message

tools/testing/selftests/bpf/progs/wakeup_source.h new file +22 -0
new file mode 100644
index 000000000000..cd74de92c82f
@@ -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 +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#4 lore raw
In reply to Samuel Wu
Quoted 24 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 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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_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.

I left some inline comments on patch 1, but the high level concern is that encoding the SRCU index into a fake pointer to get KF_ACQUIRE/ KF_RELEASE tracking is working against the verifier rather than with it. Nothing actually prevents a BPF program from walking the list without the lock, and the whole pointer encoding trick goes away if this is done as an open-coded iterator instead.

Thanks,
Puranjay

#5 lore raw
Quoted 32 lines · Puranjay Mohan
Quoted 24 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 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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_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.

Quoted 6 lines

I left some inline comments on patch 1, but the high level concern is that encoding the SRCU index into a fake pointer to get KF_ACQUIRE/ KF_RELEASE tracking is working against the verifier rather than with it. Nothing actually prevents a BPF program from walking the list without the lock, and the whole pointer encoding trick goes away if this is done as an open-coded iterator instead.

Which is fine, the critical section is only doing CO-RE accesses, and the SRCU lock is just to be able to read things in a valid state while walking the list. It is all best-effort.
Open coded iterators was already explored as an option in earlier iterations of the series and discarded as no-go.

Quoted 3 lines

Thanks,
Puranjay

#6 lore raw
Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 32 lines · Puranjay Mohan
Quoted 24 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 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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_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.

Quoted 6 lines

I left some inline comments on patch 1, but the high level concern is that encoding the SRCU index into a fake pointer to get KF_ACQUIRE/ KF_RELEASE tracking is working against the verifier rather than with it. Nothing actually prevents a BPF program from walking the list without the lock, and the whole pointer encoding trick goes away if this is done as an open-coded iterator instead.

Quoted 5 lines

Which is fine, the critical section is only doing CO-RE accesses, and the SRCU lock is just to be able to read things in a valid state while walking the list. It is all best-effort.
Open coded iterators was already explored as an option in earlier iterations of the series and discarded as no-go.

kinda best-effort...
the way it's written bpf_wakeup_sources_get_head() returns trusted list_head. It's then core-read-ed anyway. Ideally it should be trusted only within that srcu CS and invalidated by the verifier similar to KF_RCU_PROTECTED, but that's bigger task.
Instead let's make bpf_wakeup_sources_get_head() return 'void *', so it's clearly untrusted.

author
#7 lore raw
Quoted 50 lines · Alexei Starovoitov
Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 32 lines · Puranjay Mohan
Quoted 24 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 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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_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.

Quoted 6 lines

I left some inline comments on patch 1, but the high level concern is that encoding the SRCU index into a fake pointer to get KF_ACQUIRE/ KF_RELEASE tracking is working against the verifier rather than with it. Nothing actually prevents a BPF program from walking the list without the lock, and the whole pointer encoding trick goes away if this is done as an open-coded iterator instead.

Quoted 5 lines

Which is fine, the critical section is only doing CO-RE accesses, and the SRCU lock is just to be able to read things in a valid state while walking the list. It is all best-effort.
Open coded iterators was already explored as an option in earlier iterations of the series and discarded as no-go.

Quoted 8 lines

kinda best-effort...
the way it's written bpf_wakeup_sources_get_head() returns trusted list_head. It's then core-read-ed anyway. Ideally it should be trusted only within that srcu CS and invalidated by the verifier similar to KF_RCU_PROTECTED, but that's bigger task.
Instead let's make bpf_wakeup_sources_get_head() return 'void *', so it's clearly untrusted.

Thanks all for the fruitful discussion; this is more rigorous. I'll update v3 so that bpf_wakeup_sources_get_head()'s return type is void * and I can add a corresponding selftest that directly dereferences the head and expects a verifier failure.

#8 lore raw
Quoted 62 lines · Samuel Wu
Quoted 50 lines · Alexei Starovoitov
Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 32 lines · Puranjay Mohan
Quoted 24 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 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. The head address of wakeup_sources can safely be resolved through BPF helper functions or variable attributes.

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_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.

Quoted 6 lines

I left some inline comments on patch 1, but the high level concern is that encoding the SRCU index into a fake pointer to get KF_ACQUIRE/ KF_RELEASE tracking is working against the verifier rather than with it. Nothing actually prevents a BPF program from walking the list without the lock, and the whole pointer encoding trick goes away if this is done as an open-coded iterator instead.

Quoted 5 lines

Which is fine, the critical section is only doing CO-RE accesses, and the SRCU lock is just to be able to read things in a valid state while walking the list. It is all best-effort.
Open coded iterators was already explored as an option in earlier iterations of the series and discarded as no-go.

Quoted 8 lines

kinda best-effort...
the way it's written bpf_wakeup_sources_get_head() returns trusted list_head. It's then core-read-ed anyway. Ideally it should be trusted only within that srcu CS and invalidated by the verifier similar to KF_RCU_PROTECTED, but that's bigger task.
Instead let's make bpf_wakeup_sources_get_head() return 'void *', so it's clearly untrusted.

Quoted 4 lines

Thanks all for the fruitful discussion; this is more rigorous. I'll update v3 so that bpf_wakeup_sources_get_head()'s return type is void * and I can add a corresponding selftest that directly dereferences the head and expects a verifier failure.

You could also use bpf_core_cast() instead of using macros to read every field, should be equivalent. You may still need the macros for bitfields but it should work otherwise.