Skip to content

Commit 01b55f4

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: feature-detect arg:ctx tag support in kernel
Add feature detector of kernel-side arg:ctx (__arg_ctx) tag support. If this is detected, libbpf will avoid doing any __arg_ctx-related BTF rewriting and checks in favor of letting kernel handle this completely. test_global_funcs/ctx_arg_rewrite subtest is adjusted to do the same feature detection (albeit in much simpler, though round-about and inefficient, way), and skip the tests. This is done to still be able to execute this test on older kernels (like in libbpf CI). Note, BPF token series ([0]) does a major refactor and code moving of libbpf-internal feature detection "framework", so to avoid unnecessary conflicts we keep newly added feature detection stand-alone with ad-hoc result caching. Once things settle, there will be a small follow up to re-integrate everything back and move code into its final place in newly-added (by BPF token series) features.c file. [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=814209&state=* Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 33772ff commit 01b55f4

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6757,6 +6757,69 @@ static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_progr
67576757
return fn_id;
67586758
}
67596759

6760+
static int probe_kern_arg_ctx_tag(void)
6761+
{
6762+
/* To minimize merge conflicts with BPF token series that refactors
6763+
* feature detection code a lot, we don't integrate
6764+
* probe_kern_arg_ctx_tag() into kernel_supports() feature-detection
6765+
* framework yet, doing our own caching internally.
6766+
* This will be cleaned up a bit later when bpf/bpf-next trees settle.
6767+
*/
6768+
static int cached_result = -1;
6769+
static const char strs[] = "\0a\0b\0arg:ctx\0";
6770+
const __u32 types[] = {
6771+
/* [1] INT */
6772+
BTF_TYPE_INT_ENC(1 /* "a" */, BTF_INT_SIGNED, 0, 32, 4),
6773+
/* [2] PTR -> VOID */
6774+
BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
6775+
/* [3] FUNC_PROTO `int(void *a)` */
6776+
BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
6777+
BTF_PARAM_ENC(1 /* "a" */, 2),
6778+
/* [4] FUNC 'a' -> FUNC_PROTO (main prog) */
6779+
BTF_TYPE_ENC(1 /* "a" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),
6780+
/* [5] FUNC_PROTO `int(void *b __arg_ctx)` */
6781+
BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
6782+
BTF_PARAM_ENC(3 /* "b" */, 2),
6783+
/* [6] FUNC 'b' -> FUNC_PROTO (subprog) */
6784+
BTF_TYPE_ENC(3 /* "b" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 5),
6785+
/* [7] DECL_TAG 'arg:ctx' -> func 'b' arg 'b' */
6786+
BTF_TYPE_DECL_TAG_ENC(5 /* "arg:ctx" */, 6, 0),
6787+
};
6788+
const struct bpf_insn insns[] = {
6789+
/* main prog */
6790+
BPF_CALL_REL(+1),
6791+
BPF_EXIT_INSN(),
6792+
/* global subprog */
6793+
BPF_EMIT_CALL(BPF_FUNC_get_func_ip), /* needs PTR_TO_CTX */
6794+
BPF_EXIT_INSN(),
6795+
};
6796+
const struct bpf_func_info_min func_infos[] = {
6797+
{ 0, 4 }, /* main prog -> FUNC 'a' */
6798+
{ 2, 6 }, /* subprog -> FUNC 'b' */
6799+
};
6800+
LIBBPF_OPTS(bpf_prog_load_opts, opts);
6801+
int prog_fd, btf_fd, insn_cnt = ARRAY_SIZE(insns);
6802+
6803+
if (cached_result >= 0)
6804+
return cached_result;
6805+
6806+
btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs));
6807+
if (btf_fd < 0)
6808+
return 0;
6809+
6810+
opts.prog_btf_fd = btf_fd;
6811+
opts.func_info = &func_infos;
6812+
opts.func_info_cnt = ARRAY_SIZE(func_infos);
6813+
opts.func_info_rec_size = sizeof(func_infos[0]);
6814+
6815+
prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, "det_arg_ctx",
6816+
"GPL", insns, insn_cnt, &opts);
6817+
close(btf_fd);
6818+
6819+
cached_result = probe_fd(prog_fd);
6820+
return cached_result;
6821+
}
6822+
67606823
/* Check if main program or global subprog's function prototype has `arg:ctx`
67616824
* argument tags, and, if necessary, substitute correct type to match what BPF
67626825
* verifier would expect, taking into account specific program type. This
@@ -6780,6 +6843,10 @@ static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_progra
67806843
if (!obj->btf_ext || !prog->func_info)
67816844
return 0;
67826845

6846+
/* don't do any fix ups if kernel natively supports __arg_ctx */
6847+
if (probe_kern_arg_ctx_tag() > 0)
6848+
return 0;
6849+
67836850
/* some BPF program types just don't have named context structs, so
67846851
* this fallback mechanism doesn't work for them
67856852
*/

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ static void subtest_ctx_arg_rewrite(void)
4747
struct btf *btf = NULL;
4848
__u32 info_len = sizeof(info);
4949
int err, fd, i;
50+
struct btf *kern_btf = NULL;
51+
52+
kern_btf = btf__load_vmlinux_btf();
53+
if (!ASSERT_OK_PTR(kern_btf, "kern_btf_load"))
54+
return;
55+
56+
/* simple detection of kernel native arg:ctx tag support */
57+
if (btf__find_by_name_kind(kern_btf, "bpf_subprog_arg_info", BTF_KIND_STRUCT) > 0) {
58+
test__skip();
59+
btf__free(kern_btf);
60+
return;
61+
}
62+
btf__free(kern_btf);
5063

5164
skel = test_global_func_ctx_args__open();
5265
if (!ASSERT_OK_PTR(skel, "skel_open"))

0 commit comments

Comments
 (0)