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 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 } 119