Kernel structs shared with BPF programs mark pointers into a BPF arena with __arena, which kernel BTF carries as a plain "arena" type tag that the verifier relies on. btf_dump prints the tag as __attribute__((btf_type_tag("arena"))) on a pointer in the default address space, so a program built against vmlinux.h has to cast such a pointer to the arena address space before using it. Attach a kflagged decl tag to each "arena" type tag in the dump split, so that btf_dump prints __bpf_arena instead. In the generated header: #define __bpf_arena __attribute__((address_space(1))) __attribute__((btf_type_tag("arena"))) So the program gets an arena pointer and its BTF keeps the tag the kernel expects. Programs can pre-define the macro. Use __bpf_arena to avoid conflicts with unguarded __arena definitions in libarena and sched_ext headers. Signed-off-by: Ihor Solodrai --- tools/bpf/bpftool/Documentation/bpftool-btf.rst | 6 ++++++ tools/bpf/bpftool/btf.c | 5 +++++ .../testing/selftests/bpf/bpftool_btf_dump_sorted.expected | 4 ++++ .../selftests/bpf/bpftool_btf_dump_unsorted.expected | 4 ++++ .../selftests/bpf/prog_tests/test_struct_ops_arena.c | 2 +- .../selftests/bpf/progs/struct_ops_arena_member_render.c | 7 +++---- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst index a3778f449d73..9fe035b80103 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst @@ -123,6 +123,12 @@ a type marked ``__bpf_static_offset``, including through ``bpf_core_field_*`` and the CO-RE bitfield helpers. Compile with ``-D__bpf_static_offset=`` to use these helpers with an affected compiler. +Pointers carrying the ``arena`` type tag in kernel BTF are rendered with +``__bpf_arena``. This macro expands to +``__attribute__((address_space(1))) __attribute__((btf_type_tag("arena")))``, +so programs can use the pointers without explicit casts. Define +``__bpf_arena`` before including the header to override it. + EXAMPLES ======== **# bpftool btf dump id 1226** diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 1329745c5854..c06b01f2c5b3 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -1103,6 +1103,8 @@ static const struct { } btf_c_type_tags[] = { /* the arena attribute in older kernel BTF */ { true, "address_space(1)", "__attribute__((address_space(1)))" }, + /* __arena in include/linux/bpf_compiler_types.h */ + { false, "arena", "__bpf_arena" }, }; static int btf_c_annotate_type_tags(struct btf *btf) @@ -1276,6 +1278,9 @@ static int dump_btf_c(struct btf *btf, printf("#define __bpf_static_offset\n"); printf("#endif\n"); printf("#endif\n\n"); + printf("#ifndef __bpf_arena\n"); + printf("#define __bpf_arena __attribute__((address_space(1))) __attribute__((btf_type_tag(\"arena\")))\n"); + printf("#endif\n\n"); printf("#ifndef __ksym\n"); printf("#define __ksym __attribute__((section(\".ksyms\")))\n"); printf("#endif\n\n"); diff --git a/tools/testing/selftests/bpf/bpftool_btf_dump_sorted.expected b/tools/testing/selftests/bpf/bpftool_btf_dump_sorted.expected index 190cf16e49a2..0ecf53b29042 100644 --- a/tools/testing/selftests/bpf/bpftool_btf_dump_sorted.expected +++ b/tools/testing/selftests/bpf/bpftool_btf_dump_sorted.expected @@ -17,6 +17,10 @@ #endif #endif +#ifndef __bpf_arena +#define __bpf_arena __attribute__((address_space(1))) __attribute__((btf_type_tag("arena"))) +#endif + #ifndef __ksym #define __ksym __attribute__((section(".ksyms"))) #endif diff --git a/tools/testing/selftests/bpf/bpftool_btf_dump_unsorted.expected b/tools/testing/selftests/bpf/bpftool_btf_dump_unsorted.expected index 1a0a7b016c50..24a8db96eab0 100644 --- a/tools/testing/selftests/bpf/bpftool_btf_dump_unsorted.expected +++ b/tools/testing/selftests/bpf/bpftool_btf_dump_unsorted.expected @@ -17,6 +17,10 @@ #endif #endif +#ifndef __bpf_arena +#define __bpf_arena __attribute__((address_space(1))) __attribute__((btf_type_tag("arena"))) +#endif + #ifndef __ksym #define __ksym __attribute__((section(".ksyms"))) #endif diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_arena.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_arena.c index e7580b9f0fc2..2e915b350cd2 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_arena.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_arena.c @@ -259,7 +259,7 @@ static void arena_autobind_excl(void) struct_ops_arena_autobind__destroy(skel); } -/* The same through the vmlinux.h rendering of the members, which takes casts. */ +/* The same through an older bpftool's vmlinux.h rendering of the members, which takes casts. */ static void arena_member_render(void) { struct struct_ops_arena_member_render *skel; diff --git a/tools/testing/selftests/bpf/progs/struct_ops_arena_member_render.c b/tools/testing/selftests/bpf/progs/struct_ops_arena_member_render.c index 253ffdd86d32..b6b66934266b 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_arena_member_render.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_arena_member_render.c @@ -11,10 +11,9 @@ char _license[] SEC("license") = "GPL"; /* - * The module types as vmlinux.h renders them: the tag is an attribute on a - * pointer in the kernel address space, so the program has to cast the member - * to the arena address space before using it. That is what a program built - * against vmlinux.h sees for a kernel struct with an __arena member. + * The module types as an older bpftool renders them in vmlinux.h: the tag is + * an attribute on a pointer in the kernel address space, so the program has + * to cast the member to the arena address space before using it. */ #define __arena_tag __attribute__((btf_type_tag("arena"))) #define __nullable_tag __attribute__((btf_type_tag("nullable"))) -- 2.56.0