Skip to content

Commit 5e8eef8

Browse files
committed
selftests/bpf: Add tests for wakeup_sources kfuncs
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. Signed-off-by: Samuel Wu <wusamuel@google.com>
1 parent 2c262ef commit 5e8eef8

5 files changed

Lines changed: 310 additions & 1 deletion

File tree

tools/testing/selftests/bpf/config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ CONFIG_INFINIBAND=y
130130
CONFIG_SMC=y
131131
CONFIG_SMC_HS_CTRL_BPF=y
132132
CONFIG_DIBS=y
133-
CONFIG_DIBS_LO=y
133+
CONFIG_DIBS_LO=y
134+
CONFIG_PM_WAKELOCKS=y
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright 2026 Google LLC */
3+
4+
#include <test_progs.h>
5+
#include <bpf/btf.h>
6+
#include <fcntl.h>
7+
#include "test_wakeup_source.skel.h"
8+
#include "wakeup_source_fail.skel.h"
9+
#include "progs/wakeup_source.h"
10+
11+
static int lock_ws(const char *name)
12+
{
13+
int fd;
14+
ssize_t bytes;
15+
16+
fd = open("/sys/power/wake_lock", O_WRONLY);
17+
if (!ASSERT_OK_FD(fd, "open /sys/power/wake_lock"))
18+
return -1;
19+
20+
bytes = write(fd, name, strlen(name));
21+
close(fd);
22+
if (!ASSERT_EQ(bytes, strlen(name), "write to wake_lock"))
23+
return -1;
24+
25+
return 0;
26+
}
27+
28+
static void unlock_ws(const char *name)
29+
{
30+
int fd;
31+
32+
fd = open("/sys/power/wake_unlock", O_WRONLY);
33+
if (fd < 0)
34+
return;
35+
36+
write(fd, name, strlen(name));
37+
close(fd);
38+
}
39+
40+
struct rb_ctx {
41+
const char *name;
42+
bool found;
43+
long long active_time_ns;
44+
long long total_time_ns;
45+
};
46+
47+
static int process_sample(void *ctx, void *data, size_t len)
48+
{
49+
struct rb_ctx *rb_ctx = ctx;
50+
struct wakeup_event_t *e = data;
51+
52+
if (strcmp(e->name, rb_ctx->name) == 0) {
53+
rb_ctx->found = true;
54+
rb_ctx->active_time_ns = e->active_time_ns;
55+
rb_ctx->total_time_ns = e->total_time_ns;
56+
}
57+
return 0;
58+
}
59+
60+
void test_wakeup_source(void)
61+
{
62+
struct btf *btf;
63+
int id;
64+
65+
btf = btf__load_vmlinux_btf();
66+
if (!ASSERT_OK_PTR(btf, "btf_vmlinux"))
67+
return;
68+
69+
id = btf__find_by_name_kind(btf, "bpf_wakeup_sources_get_head", BTF_KIND_FUNC);
70+
btf__free(btf);
71+
72+
if (id < 0) {
73+
printf("%s:SKIP:bpf_wakeup_sources_get_head kfunc not found in kernel BTF\n", __func__);
74+
test__skip();
75+
return;
76+
}
77+
78+
if (test__start_subtest("iterate_and_verify_times")) {
79+
struct test_wakeup_source *skel;
80+
struct ring_buffer *rb = NULL;
81+
struct rb_ctx rb_ctx = {
82+
.name = "bpf_selftest_ws_times",
83+
.found = false,
84+
};
85+
int err;
86+
87+
skel = test_wakeup_source__open_and_load();
88+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
89+
return;
90+
91+
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), process_sample, &rb_ctx, NULL);
92+
if (!ASSERT_OK_PTR(rb, "ring_buffer__new"))
93+
goto destroy;
94+
95+
/* Create a temporary wakeup source */
96+
if (!ASSERT_OK(lock_ws(rb_ctx.name), "lock_ws"))
97+
goto unlock;
98+
99+
err = bpf_prog_test_run_opts(bpf_program__fd(
100+
skel->progs.iterate_wakeupsources), NULL);
101+
ASSERT_OK(err, "bpf_prog_test_run");
102+
103+
ring_buffer__consume(rb);
104+
105+
ASSERT_TRUE(rb_ctx.found, "found_test_ws_in_rb");
106+
ASSERT_GT(rb_ctx.active_time_ns, 0, "active_time_gt_0");
107+
ASSERT_GT(rb_ctx.total_time_ns, 0, "total_time_gt_0");
108+
109+
unlock:
110+
unlock_ws(rb_ctx.name);
111+
destroy:
112+
if (rb)
113+
ring_buffer__free(rb);
114+
test_wakeup_source__destroy(skel);
115+
}
116+
117+
RUN_TESTS(wakeup_source_fail);
118+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright 2026 Google LLC */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_core_read.h>
7+
#include "bpf_experimental.h"
8+
#include "bpf_misc.h"
9+
#include "wakeup_source.h"
10+
11+
#define MAX_LOOP_ITER 1000
12+
#define RB_SIZE (16384 * 4)
13+
14+
struct {
15+
__uint(type, BPF_MAP_TYPE_RINGBUF);
16+
__uint(max_entries, RB_SIZE);
17+
} rb SEC(".maps");
18+
19+
struct bpf_ws_lock;
20+
struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void) __ksym;
21+
void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock) __ksym;
22+
void *bpf_wakeup_sources_get_head(void) __ksym;
23+
24+
SEC("syscall")
25+
__success __retval(0)
26+
int iterate_wakeupsources(void *ctx)
27+
{
28+
struct list_head *head = bpf_wakeup_sources_get_head();
29+
struct list_head *pos = head;
30+
struct bpf_ws_lock *lock;
31+
int i;
32+
33+
lock = bpf_wakeup_sources_read_lock();
34+
if (!lock)
35+
return 0;
36+
37+
bpf_for(i, 0, MAX_LOOP_ITER) {
38+
if (bpf_core_read(&pos, sizeof(pos), &pos->next) || !pos || pos == head)
39+
break;
40+
41+
struct wakeup_event_t *e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
42+
43+
if (!e)
44+
break;
45+
46+
struct wakeup_source *ws = bpf_core_cast(
47+
(void *)pos - bpf_core_field_offset(struct wakeup_source, entry),
48+
struct wakeup_source);
49+
s64 active_time = 0;
50+
bool active = BPF_CORE_READ_BITFIELD(ws, active);
51+
bool autosleep_enable = BPF_CORE_READ_BITFIELD(ws, autosleep_enabled);
52+
s64 last_time = ws->last_time;
53+
s64 max_time = ws->max_time;
54+
s64 prevent_sleep_time = ws->prevent_sleep_time;
55+
s64 total_time = ws->total_time;
56+
57+
if (active) {
58+
s64 curr_time = bpf_ktime_get_ns();
59+
s64 prevent_time = ws->start_prevent_time;
60+
61+
if (curr_time > last_time)
62+
active_time = curr_time - last_time;
63+
64+
total_time += active_time;
65+
if (active_time > max_time)
66+
max_time = active_time;
67+
if (autosleep_enable && curr_time > prevent_time)
68+
prevent_sleep_time += curr_time - prevent_time;
69+
}
70+
71+
e->active_count = ws->active_count;
72+
e->active_time_ns = active_time;
73+
e->event_count = ws->event_count;
74+
e->expire_count = ws->expire_count;
75+
e->last_time_ns = last_time;
76+
e->max_time_ns = max_time;
77+
e->prevent_sleep_time_ns = prevent_sleep_time;
78+
e->total_time_ns = total_time;
79+
e->wakeup_count = ws->wakeup_count;
80+
81+
if (bpf_probe_read_kernel_str(
82+
e->name, WAKEUP_NAME_LEN, ws->name) < 0)
83+
e->name[0] = '\0';
84+
85+
bpf_ringbuf_submit(e, 0);
86+
}
87+
88+
bpf_wakeup_sources_read_unlock(lock);
89+
return 0;
90+
}
91+
92+
char _license[] SEC("license") = "GPL";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright 2026 Google LLC */
3+
4+
#ifndef __WAKEUP_SOURCE_H__
5+
#define __WAKEUP_SOURCE_H__
6+
7+
#define WAKEUP_NAME_LEN 128
8+
9+
struct wakeup_event_t {
10+
unsigned long active_count;
11+
long long active_time_ns;
12+
unsigned long event_count;
13+
unsigned long expire_count;
14+
long long last_time_ns;
15+
long long max_time_ns;
16+
long long prevent_sleep_time_ns;
17+
long long total_time_ns;
18+
unsigned long wakeup_count;
19+
char name[WAKEUP_NAME_LEN];
20+
};
21+
22+
#endif /* __WAKEUP_SOURCE_H__ */
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright 2026 Google LLC */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
7+
8+
struct bpf_ws_lock;
9+
10+
struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void) __ksym;
11+
void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock) __ksym;
12+
void *bpf_wakeup_sources_get_head(void) __ksym;
13+
14+
SEC("syscall")
15+
__failure __msg("BPF_EXIT instruction in main prog would lead to reference leak")
16+
int wakeup_source_lock_no_unlock(void *ctx)
17+
{
18+
struct bpf_ws_lock *lock;
19+
20+
lock = bpf_wakeup_sources_read_lock();
21+
if (!lock)
22+
return 0;
23+
24+
return 0;
25+
}
26+
27+
SEC("syscall")
28+
__failure __msg("access beyond struct")
29+
int wakeup_source_access_lock_fields(void *ctx)
30+
{
31+
struct bpf_ws_lock *lock;
32+
int val;
33+
34+
lock = bpf_wakeup_sources_read_lock();
35+
if (!lock)
36+
return 0;
37+
38+
val = *(int *)lock;
39+
40+
bpf_wakeup_sources_read_unlock(lock);
41+
return val;
42+
}
43+
44+
SEC("syscall")
45+
__failure __msg("type=scalar expected=fp")
46+
int wakeup_source_unlock_no_lock(void *ctx)
47+
{
48+
struct bpf_ws_lock *lock = (void *)0x1;
49+
50+
bpf_wakeup_sources_read_unlock(lock);
51+
52+
return 0;
53+
}
54+
55+
SEC("syscall")
56+
__failure __msg("Possibly NULL pointer passed to trusted arg0")
57+
int wakeup_source_unlock_null(void *ctx)
58+
{
59+
bpf_wakeup_sources_read_unlock(NULL);
60+
61+
return 0;
62+
}
63+
64+
SEC("syscall")
65+
__failure __msg("R0 invalid mem access 'scalar'")
66+
int wakeup_source_unsafe_dereference(void *ctx)
67+
{
68+
struct list_head *head = bpf_wakeup_sources_get_head();
69+
70+
if (head->next)
71+
return 1;
72+
73+
return 0;
74+
}
75+
76+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)