Skip to content

Commit 271e200

Browse files
theihorKernel Patches Daemon
authored andcommitted
selftests/bpf: Use both hrtimer enqueue helpers in vmlinux test
The vmlinux selftest triggers nanosleep and checks that both kprobe and fentry programs observe the hrtimer enqueue path. After the hrtimer_start_expires_user() conversion [1], nanosleep reaches hrtimer_start_range_ns_user() instead of hrtimer_start_range_ns(). Hard-coding either symbol makes the test fail either on bpf tree or on linux-next [2]. Keep both hook variants as optional BPF programs and enable the pair matching the running kernel before loading the object. This is also a nice example of how to modify a BPF program to work on both older and newer kernel revision. [1] https://lore.kernel.org/all/20260408114952.062400833@kernel.org/ [2] https://github.com/kernel-patches/bpf/actions/runs/25485909958/job/74782902203 Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
1 parent ee9631b commit 271e200

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

tools/testing/selftests/bpf/prog_tests/vmlinux.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,37 @@ static void nsleep()
1414
(void)syscall(__NR_nanosleep, &ts, NULL);
1515
}
1616

17+
static void enable_hrtimer_progs(struct test_vmlinux *skel)
18+
{
19+
bool has_user_helper;
20+
21+
has_user_helper = libbpf_find_vmlinux_btf_id("hrtimer_start_range_ns_user",
22+
BPF_TRACE_FENTRY) > 0;
23+
if (has_user_helper) {
24+
bpf_program__set_autoload(skel->progs.handle__kprobe_user, true);
25+
bpf_program__set_autoload(skel->progs.handle__fentry_user, true);
26+
} else {
27+
bpf_program__set_autoload(skel->progs.handle__kprobe, true);
28+
bpf_program__set_autoload(skel->progs.handle__fentry, true);
29+
}
30+
}
31+
1732
void test_vmlinux(void)
1833
{
1934
int err;
2035
struct test_vmlinux* skel;
2136
struct test_vmlinux__bss *bss;
2237

23-
skel = test_vmlinux__open_and_load();
24-
if (!ASSERT_OK_PTR(skel, "test_vmlinux__open_and_load"))
38+
skel = test_vmlinux__open();
39+
if (!ASSERT_OK_PTR(skel, "test_vmlinux__open"))
2540
return;
41+
42+
enable_hrtimer_progs(skel);
43+
44+
err = test_vmlinux__load(skel);
45+
if (!ASSERT_OK(err, "test_vmlinux__load"))
46+
goto cleanup;
47+
2648
bss = skel->bss;
2749

2850
err = test_vmlinux__attach(skel);

tools/testing/selftests/bpf/progs/test_vmlinux.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int BPF_PROG(handle__tp_btf, struct pt_regs *regs, long id)
6969
return 0;
7070
}
7171

72-
SEC("kprobe/hrtimer_start_range_ns")
72+
SEC("?kprobe/hrtimer_start_range_ns")
7373
int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
7474
const enum hrtimer_mode mode)
7575
{
@@ -78,7 +78,7 @@ int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
7878
return 0;
7979
}
8080

81-
SEC("fentry/hrtimer_start_range_ns")
81+
SEC("?fentry/hrtimer_start_range_ns")
8282
int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
8383
const enum hrtimer_mode mode)
8484
{
@@ -87,4 +87,22 @@ int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
8787
return 0;
8888
}
8989

90+
SEC("?kprobe/hrtimer_start_range_ns_user")
91+
int BPF_KPROBE(handle__kprobe_user, struct hrtimer *timer, ktime_t tim,
92+
u64 delta_ns, const enum hrtimer_mode mode)
93+
{
94+
if (tim == MY_TV_NSEC)
95+
kprobe_called = true;
96+
return 0;
97+
}
98+
99+
SEC("?fentry/hrtimer_start_range_ns_user")
100+
int BPF_PROG(handle__fentry_user, struct hrtimer *timer, ktime_t tim,
101+
u64 delta_ns, const enum hrtimer_mode mode)
102+
{
103+
if (tim == MY_TV_NSEC)
104+
fentry_called = true;
105+
return 0;
106+
}
107+
90108
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)