Skip to contentSkip to conversation

selftests/bpf: Use both hrtimer enqueue helpers in vmlinux test

PATCH Superseded Ihor Solodrai 2 participants 2 messages May 7

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH 1/1 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/[email protected]/
[2] https://github.com/kernel-patches/bpf/actions/runs/25485909958/job/74782902203

---
 .../selftests/bpf/prog_tests/vmlinux.c        | 26 +++++++++++++++++--
 .../selftests/bpf/progs/test_vmlinux.c        | 22 ++++++++++++++--
 2 files changed, 44 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/vmlinux.c +24 -2
index 6fb2217d940b..286feff31488 100644
@@ -14,15 +14,37 @@ static void nsleep()
 	(void)syscall(__NR_nanosleep, &ts, NULL);
 }
 
+static void enable_hrtimer_progs(struct test_vmlinux *skel)
+{
+	bool has_user_helper;
+
+	has_user_helper = libbpf_find_vmlinux_btf_id("hrtimer_start_range_ns_user",
+						    BPF_TRACE_FENTRY) > 0;
+	if (has_user_helper) {
+		bpf_program__set_autoload(skel->progs.handle__kprobe_user, true);
+		bpf_program__set_autoload(skel->progs.handle__fentry_user, true);
+	} else {
+		bpf_program__set_autoload(skel->progs.handle__kprobe, true);
+		bpf_program__set_autoload(skel->progs.handle__fentry, true);
+	}
+}
+
 void test_vmlinux(void)
 {
 	int err;
 	struct test_vmlinux* skel;
 	struct test_vmlinux__bss *bss;
 
-	skel = test_vmlinux__open_and_load();
-	if (!ASSERT_OK_PTR(skel, "test_vmlinux__open_and_load"))
+	skel = test_vmlinux__open();
+	if (!ASSERT_OK_PTR(skel, "test_vmlinux__open"))
 		return;
+
+	enable_hrtimer_progs(skel);
+
+	err = test_vmlinux__load(skel);
+	if (!ASSERT_OK(err, "test_vmlinux__load"))
+		goto cleanup;
+
 	bss = skel->bss;
 
 	err = test_vmlinux__attach(skel);
tools/testing/selftests/bpf/progs/test_vmlinux.c +20 -2
index 78b23934d9f8..c90f7ac63f91 100644
@@ -69,7 +69,7 @@ int BPF_PROG(handle__tp_btf, struct pt_regs *regs, long id)
 	return 0;
 }
 
-SEC("kprobe/hrtimer_start_range_ns")
+SEC("?kprobe/hrtimer_start_range_ns")
 int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
 	       const enum hrtimer_mode mode)
 {
@@ -78,7 +78,7 @@ int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
 	return 0;
 }
 
-SEC("fentry/hrtimer_start_range_ns")
+SEC("?fentry/hrtimer_start_range_ns")
 int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
 	     const enum hrtimer_mode mode)
 {
@@ -87,4 +87,22 @@ int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
 	return 0;
 }
 
+SEC("?kprobe/hrtimer_start_range_ns_user")
+int BPF_KPROBE(handle__kprobe_user, struct hrtimer *timer, ktime_t tim,
+	       u64 delta_ns, const enum hrtimer_mode mode)
+{
+	if (tim == MY_TV_NSEC)
+		kprobe_called = true;
+	return 0;
+}
+
+SEC("?fentry/hrtimer_start_range_ns_user")
+int BPF_PROG(handle__fentry_user, struct hrtimer *timer, ktime_t tim,
+	     u64 delta_ns, const enum hrtimer_mode mode)
+{
+	if (tim == MY_TV_NSEC)
+		fentry_called = true;
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.54.0
#1 lore raw
Quoted 9 lines · Ihor Solodrai

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

Quoted 4 lines

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.

Nice. Can you further use bpf_program__set_attach_target() to avoid duplicating the program as an example?

Quoted 103 lines

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/kernel-patches/bpf/actions/runs/25485909958/job/74782902203

---
 .../selftests/bpf/prog_tests/vmlinux.c        | 26 +++++++++++++++++--
 .../selftests/bpf/progs/test_vmlinux.c        | 22 ++++++++++++++--
 2 files changed, 44 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/vmlinux.c +24 -2
index 6fb2217d940b..286feff31488 100644
@@ -14,15 +14,37 @@ static void nsleep()
        (void)syscall(__NR_nanosleep, &ts, NULL);
 }
 
+static void enable_hrtimer_progs(struct test_vmlinux *skel)
+{
+       bool has_user_helper;
+
+       has_user_helper = libbpf_find_vmlinux_btf_id("hrtimer_start_range_ns_user",
+                                                   BPF_TRACE_FENTRY) > 0;
+       if (has_user_helper) {
+               bpf_program__set_autoload(skel->progs.handle__kprobe_user, true);
+               bpf_program__set_autoload(skel->progs.handle__fentry_user, true);
+       } else {
+               bpf_program__set_autoload(skel->progs.handle__kprobe, true);
+               bpf_program__set_autoload(skel->progs.handle__fentry, true);
+       }
+}
+
 void test_vmlinux(void)
 {
        int err;
        struct test_vmlinux* skel;
        struct test_vmlinux__bss *bss;
 
-       skel = test_vmlinux__open_and_load();
-       if (!ASSERT_OK_PTR(skel, "test_vmlinux__open_and_load"))
+       skel = test_vmlinux__open();
+       if (!ASSERT_OK_PTR(skel, "test_vmlinux__open"))
                return;
+
+       enable_hrtimer_progs(skel);
+
+       err = test_vmlinux__load(skel);
+       if (!ASSERT_OK(err, "test_vmlinux__load"))
+               goto cleanup;
+
        bss = skel->bss;
 
        err = test_vmlinux__attach(skel);
tools/testing/selftests/bpf/progs/test_vmlinux.c +20 -3
index 78b23934d9f8..c90f7ac63f91 100644
@@ -69,7 +69,7 @@ int BPF_PROG(handle__tp_btf, struct pt_regs *regs, long id)
        return 0;
 }
 
-SEC("kprobe/hrtimer_start_range_ns")
+SEC("?kprobe/hrtimer_start_range_ns")
 int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
               const enum hrtimer_mode mode)
 {
@@ -78,7 +78,7 @@ int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
        return 0;
 }
 
-SEC("fentry/hrtimer_start_range_ns")
+SEC("?fentry/hrtimer_start_range_ns")
 int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
             const enum hrtimer_mode mode)
 {
@@ -87,4 +87,22 @@ int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
        return 0;
 }
 
+SEC("?kprobe/hrtimer_start_range_ns_user")
+int BPF_KPROBE(handle__kprobe_user, struct hrtimer *timer, ktime_t tim,
+              u64 delta_ns, const enum hrtimer_mode mode)
+{
+       if (tim == MY_TV_NSEC)
+               kprobe_called = true;
+       return 0;
+}
+
+SEC("?fentry/hrtimer_start_range_ns_user")
+int BPF_PROG(handle__fentry_user, struct hrtimer *timer, ktime_t tim,
+            u64 delta_ns, const enum hrtimer_mode mode)
+{
+       if (tim == MY_TV_NSEC)
+               fentry_called = true;
+       return 0;
+}
+
 char _license[] SEC("license") = "GPL";
--

2.54.0