Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Ensure snprintf_btf/bpf_iter tests compatibility with old vmlinux.h

Andrii reports that bpf selftests relying on "struct btf_ptr" and BTF_F_*
values will not build as vmlinux.h for older kernels will not include
"struct btf_ptr" or the BTF_F_* enum values. Undefine and redefine
them to work around this.

Fixes: b72091bd4ee4 ("selftests/bpf: Add test for bpf_seq_printf_btf helper")
Fixes: 076a95f5aff2 ("selftests/bpf: Add bpf_snprintf_btf helper tests")
Reported-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/1601379151-21449-3-git-send-email-alan.maguire@oracle.com

authored by

Alan Maguire and committed by
Alexei Starovoitov
cfe77683 96c48058

+51 -1
+23
tools/testing/selftests/bpf/progs/bpf_iter.h
··· 14 14 #define bpf_iter__bpf_map_elem bpf_iter__bpf_map_elem___not_used 15 15 #define bpf_iter__bpf_sk_storage_map bpf_iter__bpf_sk_storage_map___not_used 16 16 #define bpf_iter__sockmap bpf_iter__sockmap___not_used 17 + #define btf_ptr btf_ptr___not_used 18 + #define BTF_F_COMPACT BTF_F_COMPACT___not_used 19 + #define BTF_F_NONAME BTF_F_NONAME___not_used 20 + #define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used 21 + #define BTF_F_ZERO BTF_F_ZERO___not_used 17 22 #include "vmlinux.h" 18 23 #undef bpf_iter_meta 19 24 #undef bpf_iter__bpf_map ··· 33 28 #undef bpf_iter__bpf_map_elem 34 29 #undef bpf_iter__bpf_sk_storage_map 35 30 #undef bpf_iter__sockmap 31 + #undef btf_ptr 32 + #undef BTF_F_COMPACT 33 + #undef BTF_F_NONAME 34 + #undef BTF_F_PTR_RAW 35 + #undef BTF_F_ZERO 36 36 37 37 struct bpf_iter_meta { 38 38 struct seq_file *seq; ··· 114 104 struct bpf_map *map; 115 105 void *key; 116 106 struct sock *sk; 107 + }; 108 + 109 + struct btf_ptr { 110 + void *ptr; 111 + __u32 type_id; 112 + __u32 flags; 113 + }; 114 + 115 + enum { 116 + BTF_F_COMPACT = (1ULL << 0), 117 + BTF_F_NONAME = (1ULL << 1), 118 + BTF_F_PTR_RAW = (1ULL << 2), 119 + BTF_F_ZERO = (1ULL << 3), 117 120 };
+27
tools/testing/selftests/bpf/progs/btf_ptr.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (c) 2020, Oracle and/or its affiliates. */ 3 + /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 + #define btf_ptr btf_ptr___not_used 5 + #define BTF_F_COMPACT BTF_F_COMPACT___not_used 6 + #define BTF_F_NONAME BTF_F_NONAME___not_used 7 + #define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used 8 + #define BTF_F_ZERO BTF_F_ZERO___not_used 9 + #include "vmlinux.h" 10 + #undef btf_ptr 11 + #undef BTF_F_COMPACT 12 + #undef BTF_F_NONAME 13 + #undef BTF_F_PTR_RAW 14 + #undef BTF_F_ZERO 15 + 16 + struct btf_ptr { 17 + void *ptr; 18 + __u32 type_id; 19 + __u32 flags; 20 + }; 21 + 22 + enum { 23 + BTF_F_COMPACT = (1ULL << 0), 24 + BTF_F_NONAME = (1ULL << 1), 25 + BTF_F_PTR_RAW = (1ULL << 2), 26 + BTF_F_ZERO = (1ULL << 3), 27 + };
+1 -1
tools/testing/selftests/bpf/progs/netif_receive_skb.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020, Oracle and/or its affiliates. */ 3 3 4 - #include "vmlinux.h" 4 + #include "btf_ptr.h" 5 5 #include <bpf/bpf_helpers.h> 6 6 #include <bpf/bpf_tracing.h> 7 7 #include <bpf/bpf_core_read.h>