Skip to content

Commit 2429e15

Browse files
alan-maguireKernel Patches Daemon
authored andcommitted
selftests/bpf: Add test tracing inline site using SEC("kloc")
Add a test tracing a vmlinux inline function called from __sys_bpf() and ensure one of its arguments - if available - is as expected. A simple test as a starting point but it does demonstrate the viability of the approach. Ideally we would add a bunch of inlines to bpf_testmod, but need to have BTF distillation/relocation working for .BTF.extra sections; that is not yet implemented. Signed-off-by: Alan Maguire <[email protected]>
1 parent fc4c3df commit 2429e15

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025, Oracle and/or its affiliates. */
3+
4+
#include <test_progs.h>
5+
#include <sys/stat.h>
6+
7+
#include "kloc.skel.h"
8+
9+
void test_kloc(void)
10+
{
11+
int err = 0, duration = 0;
12+
struct kloc *skel;
13+
struct stat sb;
14+
15+
/* If CONFIG_DEBUG_INFO_BTF_EXTRA=m , ensure vmlinux BTF extra is
16+
* loaded.
17+
*/
18+
system("modprobe btf_extra");
19+
20+
/* Kernel may not have been compiled with extra BTF info or pahole
21+
* may not have support.
22+
*/
23+
if (stat("/sys/kernel/btf_extra/vmlinux", &sb) != 0)
24+
test__skip();
25+
26+
skel = kloc__open_and_load();
27+
if (CHECK(!skel, "skel_load", "skeleton failed: %d\n", err))
28+
goto cleanup;
29+
30+
skel->bss->test_pid = getpid();
31+
32+
err = kloc__attach(skel);
33+
if (!ASSERT_OK(err, "attach"))
34+
goto cleanup;
35+
/* trigger bpf syscall to trigger kloc */
36+
(void) bpf_obj_get("/sys/fs/bpf/noexist");
37+
38+
ASSERT_GT(skel->bss->kloc_triggered, 0, "verify kloc was triggered");
39+
40+
/* this is a conditional since it is possible the size parameter
41+
* is not available at the inline site.
42+
*
43+
* Expected size here is that from bpf_obj_get_opts(); see
44+
* tools/lib/bpf/bpf.c.
45+
*/
46+
if (skel->bss->kloc_size > 0)
47+
ASSERT_EQ(skel->bss->kloc_size, offsetofend(union bpf_attr, path_fd), "verify kloc size set");
48+
49+
cleanup:
50+
kloc__destroy(skel);
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025, Oracle and/or its affiliates. */
3+
4+
#include "vmlinux.h"
5+
6+
#include <bpf/bpf_helpers.h>
7+
#include <bpf/bpf_tracing.h>
8+
#include <bpf/bpf_core_read.h>
9+
#include <bpf/loc.bpf.h>
10+
11+
char _license[] SEC("license") = "GPL";
12+
13+
int kloc_triggered;
14+
size_t kloc_size;
15+
int test_pid;
16+
17+
/* This function is inlined to __sys_bpf() and we trigger a call to
18+
* it via bpf_obj_get_opts().
19+
*/
20+
SEC("kloc/vmlinux:copy_from_bpfptr_offset")
21+
int BPF_KLOC(trace_copy_from_bpfptr_offset, void *dst, void *uattr, size_t offset, size_t size)
22+
{
23+
int pid = bpf_get_current_pid_tgid() >> 32;
24+
long s;
25+
26+
if (test_pid != pid)
27+
return 0;
28+
29+
kloc_triggered++;
30+
31+
/* is arg available? */
32+
if (bpf_loc_arg(ctx, 3, &s) == 0)
33+
kloc_size = size;
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)