selftests/bpf: Use both hrtimer enqueue helpers in vmlinux test
Subsystems (3)
Ihor Solodrai
reviewerPATCH v2 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].
Update the test to resolve the target symbol at runtime via libbpf_find_vmlinux_btf_id(). This is 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
---
v1->v2:
* Use bpf_program__set_attach_target() and bpf_program__attach_kprobe() to avoid duplicating BPF programs (Amery)
v1: https://lore.kernel.org/all/[email protected]/
--- .../selftests/bpf/prog_tests/vmlinux.c | 45 ++++++++++++++++++- .../selftests/bpf/progs/test_vmlinux.c | 4 +- 2 files changed, 45 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/vmlinux.c +43 -2
@@ -14,21 +14,61 @@ static void nsleep() (void )syscall (__NR_nanosleep, &ts,NULL ); } +static const char *hrtimer_func ="hrtimer_start_range_ns" ; + +static int setup_hrtimer_progs (struct test_vmlinux *skel) +{ +int err; + +if (libbpf_find_vmlinux_btf_id ("hrtimer_start_range_ns_user" ,BPF_TRACE_FENTRY ) >0 ) + hrtimer_func ="hrtimer_start_range_ns_user" ; + + err =bpf_program__set_attach_target (skel->progs.handle__fentry,0 , hrtimer_func); +if (err) +return err; + +/* +* Bare SEC("kprobe") has no target function, so attach it manually +* later after selecting the hrtimer function to probe. +*/ +bpf_program__set_autoattach (skel->progs.handle__kprobe, false); + +return 0 ; +} +void test_vmlinux (void ) {int err;struct test_vmlinux * skel;struct test_vmlinux__bss *bss; +struct bpf_link *kprobe_link =NULL ; - 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 ; + + err =setup_hrtimer_progs (skel); +if (!ASSERT_OK (err,"setup_hrtimer_progs" )) + goto cleanup; + + err =test_vmlinux__load (skel); +if (!ASSERT_OK (err,"test_vmlinux__load" )) + goto cleanup; + bss = skel->bss; err =test_vmlinux__attach (skel);if (!ASSERT_OK (err,"test_vmlinux__attach" )) goto cleanup; +/* manually attach kprobe with the selected function */ +if (hrtimer_func) { + kprobe_link =bpf_program__attach_kprobe (skel->progs.handle__kprobe, + false/* retprobe */ , hrtimer_func); +if (!ASSERT_OK_PTR (kprobe_link,"bpf_program__attach_kprobe" )) + goto cleanup; + } +/* trigger everything */ nsleep ();
@@ -39,5 +79,6 @@ void test_vmlinux(void)ASSERT_TRUE (bss->fentry_called,"fentry" ); cleanup: +bpf_link__destroy (kprobe_link);test_vmlinux__destroy (skel); }
tools/testing/selftests/bpf/progs/test_vmlinux.c +2 -2
@@ -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")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")int BPF_PROG (handle__fentry ,struct hrtimer *timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode) {
Signature · 1 line
2.54.0
Quoted 11 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 reacheshrtimer_start_range_ns_user()instead ofhrtimer_start_range_ns(). Hard-coding either symbol makes the test fail either on bpf tree or on linux-next [2].Update the test to resolve the target symbol at runtime via
libbpf_find_vmlinux_btf_id(). This is a nice example of how to modify a BPF program to work on both older and newer kernel revision.
iiuc this will help before the [1] fully propagates everywhere, also good as an example
jirka
Quoted 119 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/kernel-patches/bpf/actions/runs/25485909958/job/74782902203Signed-off-by Ihor Solodrai <[email protected]>---
v1->v2:
* Usebpf_program__set_attach_target()andbpf_program__attach_kprobe()to avoid duplicating BPF programs (Amery)
v1: https://lore.kernel.org/all/[email protected]/--- .../selftests/bpf/prog_tests/vmlinux.c | 45 ++++++++++++++++++- .../selftests/bpf/progs/test_vmlinux.c | 4 +- 2 files changed, 45 insertions(+), 4 deletions(-)tools/testing/selftests/bpf/prog_tests/vmlinux.c +43
-2@@ -14,21 +14,61 @@ static void nsleep() (void )syscall (__NR_nanosleep, &ts,NULL ); } +static const char *hrtimer_func ="hrtimer_start_range_ns" ; + +static int setup_hrtimer_progs (struct test_vmlinux *skel) +{ +int err; + +if (libbpf_find_vmlinux_btf_id ("hrtimer_start_range_ns_user" ,BPF_TRACE_FENTRY ) >0 ) + hrtimer_func ="hrtimer_start_range_ns_user" ; + + err =bpf_program__set_attach_target (skel->progs.handle__fentry,0 , hrtimer_func); +if (err) +return err; + +/* +* Bare SEC("kprobe") has no target function, so attach it manually +* later after selecting the hrtimer function to probe. +*/ +bpf_program__set_autoattach (skel->progs.handle__kprobe, false); + +return 0 ; +} +void test_vmlinux (void ) {int err;struct test_vmlinux * skel;struct test_vmlinux__bss *bss; +struct bpf_link *kprobe_link =NULL ; - 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 ; + + err =setup_hrtimer_progs (skel); +if (!ASSERT_OK (err,"setup_hrtimer_progs" )) + goto cleanup; + + err =test_vmlinux__load (skel); +if (!ASSERT_OK (err,"test_vmlinux__load" )) + goto cleanup; + bss = skel->bss; err =test_vmlinux__attach (skel);if (!ASSERT_OK (err,"test_vmlinux__attach" )) goto cleanup; +/* manually attach kprobe with the selected function */ +if (hrtimer_func) { + kprobe_link =bpf_program__attach_kprobe (skel->progs.handle__kprobe, + false/* retprobe */ , hrtimer_func); +if (!ASSERT_OK_PTR (kprobe_link,"bpf_program__attach_kprobe" )) + goto cleanup; + } +/* trigger everything */ nsleep ();@@ -39,5 +79,6 @@ void test_vmlinux(void)ASSERT_TRUE (bss->fentry_called,"fentry" ); cleanup: +bpf_link__destroy (kprobe_link);test_vmlinux__destroy (skel); }tools/testing/selftests/bpf/progs/test_vmlinux.c +2
-2@@ -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")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")int BPF_PROG (handle__fentry ,struct hrtimer *timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode) {Signature · 1 line
2.54.0
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Fri, 8 May 2026 17:57:30 -0700 you wrote:
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 reacheshrtimer_start_range_ns_user()instead ofhrtimer_start_range_ns(). Hard-coding either symbol makes the test fail either on bpf tree or on linux-next [2].[...]
Here is the summary with links:
- [bpf,v2] selftests/bpf: Use both hrtimer enqueue helpers in vmlinux test
https://git.kernel.org/bpf/bpf-next/c/25bb05dd06cc
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html