Add kfuncs to lock/unlock for safe traversal of wakeup sources.
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. A new structure, bpf_ws_lock, acts as
an opaque wrapper for the SRCU index. The head address of wakeup_sources
can be safely resolved through BPF helper functions or variable
attributes.
Doing the traversal in BPF is significantly more performant, and has an
output in a format that the user specifies; this solves all the
drawbacks of current interfaces.
Signed-off-by: Samuel Wu <wusamuel@google.com>
---
drivers/base/power/wakeup.c | 63 +++++++++++++++++++++++++++++++++++--
1 file changed, 61 insertions(+), 2 deletions(-)
diff --git a/drivers/base/power/wakeup.c b/drivers/base/power/wakeup.c
index b8e48a023bf0..7fc12ce125bc 100644
--- a/drivers/base/power/wakeup.c
+++ b/drivers/base/power/wakeup.c
@@ -1168,11 +1168,70 @@ 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>
+
+struct bpf_ws_lock { };
+
+__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);
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(wakeup_source_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_wakeup_sources_read_lock, KF_ACQUIRE)
+BTF_ID_FLAGS(func, bpf_wakeup_sources_read_unlock, KF_RELEASE)
+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);
--
2.53.0.959.g497ff81fa9-goog
Hi Samuel,
kernel test robot noticed the following build warnings:
[auto build test WARNING on rafael-pm/linux-next]
[also build test WARNING on rafael-pm/bleeding-edge linus/master v7.0-rc5 next-20260320]
[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/PM-wakeup-Add-kfuncs-to-lock-unlock-wakeup_sources/20260323-064540
base: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
patch link: https://lore.kernel.org/r/20260320160055.4114055-2-wusamuel%40google.com
patch subject: [PATCH v1 1/2] PM: wakeup: Add kfuncs to lock/unlock wakeup_sources
config: x86_64-randconfig-121-20260323 (https://download.01.org/0day-ci/archive/20260323/202603232018.pdjbZ3eL-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.4.0-5) 12.4.0
sparse: v0.6.5-rc1
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260323/202603232018.pdjbZ3eL-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/202603232018.pdjbZ3eL-lkp@intel.com/
sparse warnings: (new ones prefixed by >>)
>> drivers/base/power/wakeup.c:1189:32: sparse: sparse: symbol 'bpf_wakeup_sources_read_lock' was not declared. Should it be static?
>> drivers/base/power/wakeup.c:1202:18: sparse: sparse: symbol 'bpf_wakeup_sources_read_unlock' was not declared. Should it be static?
vim +/bpf_wakeup_sources_read_lock +1189 drivers/base/power/wakeup.c
1177
1178 /**
1179 * bpf_wakeup_sources_read_lock - Acquire the SRCU lock for wakeup sources
1180 *
1181 * The underlying SRCU lock returns an integer index. However, the BPF verifier
1182 * requires a pointer (PTR_TO_BTF_ID) to strictly track the state of acquired
1183 * resources using KF_ACQUIRE and KF_RELEASE semantics. We use an opaque
1184 * structure pointer (struct bpf_ws_lock *) to satisfy the verifier while
1185 * safely encoding the integer index within the pointer address itself.
1186 *
1187 * Return: An opaque pointer encoding the SRCU lock index + 1 (to avoid NULL).
1188 */
> 1189 __bpf_kfunc struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void)
1190 {
1191 return (struct bpf_ws_lock *)(long)(wakeup_sources_read_lock() + 1);
1192 }
1193
1194 /**
1195 * bpf_wakeup_sources_read_unlock - Release the SRCU lock for wakeup sources
1196 * @lock: The opaque pointer returned by bpf_wakeup_sources_read_lock()
1197 *
1198 * The BPF verifier guarantees that @lock is a valid, unreleased pointer from
1199 * the acquire function. We decode the pointer back into the integer SRCU index
1200 * by subtracting 1 and release the lock.
1201 */
> 1202 __bpf_kfunc void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock)
1203 {
1204 wakeup_sources_read_unlock((int)(long)lock - 1);
1205 }
1206
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.