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

selftests/bpf: Move newer bpf_iter_* type redefining to a new header file

Commit b9f4c01f3e0b ("selftest/bpf: Make bpf_iter selftest
compilable against old vmlinux.h") and Commit dda18a5c0b75
("selftests/bpf: Convert bpf_iter_test_kern{3, 4}.c to define
own bpf_iter_meta") redefined newly introduced types
in bpf programs so the bpf program can still compile
properly with old kernels although loading may fail.

Since this patch set introduced new types and the same
workaround is needed, so let us move the workaround
to a separate header file so they do not clutter
bpf programs.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200623230816.3988656-1-yhs@fb.com

authored by

Yonghong Song and committed by
Alexei Starovoitov
84544f56 0d4fad3e

+57 -136
+49
tools/testing/selftests/bpf/progs/bpf_iter.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (c) 2020 Facebook */ 3 + /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 + #define bpf_iter_meta bpf_iter_meta___not_used 5 + #define bpf_iter__bpf_map bpf_iter__bpf_map___not_used 6 + #define bpf_iter__ipv6_route bpf_iter__ipv6_route___not_used 7 + #define bpf_iter__netlink bpf_iter__netlink___not_used 8 + #define bpf_iter__task bpf_iter__task___not_used 9 + #define bpf_iter__task_file bpf_iter__task_file___not_used 10 + #include "vmlinux.h" 11 + #undef bpf_iter_meta 12 + #undef bpf_iter__bpf_map 13 + #undef bpf_iter__ipv6_route 14 + #undef bpf_iter__netlink 15 + #undef bpf_iter__task 16 + #undef bpf_iter__task_file 17 + 18 + struct bpf_iter_meta { 19 + struct seq_file *seq; 20 + __u64 session_id; 21 + __u64 seq_num; 22 + } __attribute__((preserve_access_index)); 23 + 24 + struct bpf_iter__ipv6_route { 25 + struct bpf_iter_meta *meta; 26 + struct fib6_info *rt; 27 + } __attribute__((preserve_access_index)); 28 + 29 + struct bpf_iter__netlink { 30 + struct bpf_iter_meta *meta; 31 + struct netlink_sock *sk; 32 + } __attribute__((preserve_access_index)); 33 + 34 + struct bpf_iter__task { 35 + struct bpf_iter_meta *meta; 36 + struct task_struct *task; 37 + } __attribute__((preserve_access_index)); 38 + 39 + struct bpf_iter__task_file { 40 + struct bpf_iter_meta *meta; 41 + struct task_struct *task; 42 + __u32 fd; 43 + struct file *file; 44 + } __attribute__((preserve_access_index)); 45 + 46 + struct bpf_iter__bpf_map { 47 + struct bpf_iter_meta *meta; 48 + struct bpf_map *map; 49 + } __attribute__((preserve_access_index));
+1 -17
tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__bpf_map bpf_iter__bpf_map___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__bpf_map 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 #include <bpf/bpf_tracing.h> 11 6 12 7 char _license[] SEC("license") = "GPL"; 13 - 14 - struct bpf_iter_meta { 15 - struct seq_file *seq; 16 - __u64 session_id; 17 - __u64 seq_num; 18 - } __attribute__((preserve_access_index)); 19 - 20 - struct bpf_iter__bpf_map { 21 - struct bpf_iter_meta *meta; 22 - struct bpf_map *map; 23 - } __attribute__((preserve_access_index)); 24 8 25 9 SEC("iter/bpf_map") 26 10 int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
+1 -17
tools/testing/selftests/bpf/progs/bpf_iter_ipv6_route.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__ipv6_route bpf_iter__ipv6_route___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__ipv6_route 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 #include <bpf/bpf_tracing.h> 11 - 12 - struct bpf_iter_meta { 13 - struct seq_file *seq; 14 - __u64 session_id; 15 - __u64 seq_num; 16 - } __attribute__((preserve_access_index)); 17 - 18 - struct bpf_iter__ipv6_route { 19 - struct bpf_iter_meta *meta; 20 - struct fib6_info *rt; 21 - } __attribute__((preserve_access_index)); 22 6 23 7 char _license[] SEC("license") = "GPL"; 24 8
+1 -17
tools/testing/selftests/bpf/progs/bpf_iter_netlink.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__netlink bpf_iter__netlink___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__netlink 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 #include <bpf/bpf_tracing.h> 11 6 ··· 8 13 9 14 #define sk_rmem_alloc sk_backlog.rmem_alloc 10 15 #define sk_refcnt __sk_common.skc_refcnt 11 - 12 - struct bpf_iter_meta { 13 - struct seq_file *seq; 14 - __u64 session_id; 15 - __u64 seq_num; 16 - } __attribute__((preserve_access_index)); 17 - 18 - struct bpf_iter__netlink { 19 - struct bpf_iter_meta *meta; 20 - struct netlink_sock *sk; 21 - } __attribute__((preserve_access_index)); 22 16 23 17 static inline struct inode *SOCK_INODE(struct socket *socket) 24 18 {
+1 -17
tools/testing/selftests/bpf/progs/bpf_iter_task.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__task bpf_iter__task___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__task 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 #include <bpf/bpf_tracing.h> 11 6 12 7 char _license[] SEC("license") = "GPL"; 13 - 14 - struct bpf_iter_meta { 15 - struct seq_file *seq; 16 - __u64 session_id; 17 - __u64 seq_num; 18 - } __attribute__((preserve_access_index)); 19 - 20 - struct bpf_iter__task { 21 - struct bpf_iter_meta *meta; 22 - struct task_struct *task; 23 - } __attribute__((preserve_access_index)); 24 8 25 9 SEC("iter/task") 26 10 int dump_task(struct bpf_iter__task *ctx)
+1 -19
tools/testing/selftests/bpf/progs/bpf_iter_task_file.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__task_file bpf_iter__task_file___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__task_file 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 #include <bpf/bpf_tracing.h> 11 6 12 7 char _license[] SEC("license") = "GPL"; 13 - 14 - struct bpf_iter_meta { 15 - struct seq_file *seq; 16 - __u64 session_id; 17 - __u64 seq_num; 18 - } __attribute__((preserve_access_index)); 19 - 20 - struct bpf_iter__task_file { 21 - struct bpf_iter_meta *meta; 22 - struct task_struct *task; 23 - __u32 fd; 24 - struct file *file; 25 - } __attribute__((preserve_access_index)); 26 8 27 9 SEC("iter/task_file") 28 10 int dump_task_file(struct bpf_iter__task_file *ctx)
+1 -16
tools/testing/selftests/bpf/progs/bpf_iter_test_kern3.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - #define bpf_iter_meta bpf_iter_meta___not_used 4 - #define bpf_iter__task bpf_iter__task___not_used 5 - #include "vmlinux.h" 6 - #undef bpf_iter_meta 7 - #undef bpf_iter__task 3 + #include "bpf_iter.h" 8 4 #include <bpf/bpf_helpers.h> 9 5 10 6 char _license[] SEC("license") = "GPL"; 11 - 12 - struct bpf_iter_meta { 13 - struct seq_file *seq; 14 - __u64 session_id; 15 - __u64 seq_num; 16 - } __attribute__((preserve_access_index)); 17 - 18 - struct bpf_iter__task { 19 - struct bpf_iter_meta *meta; 20 - struct task_struct *task; 21 - } __attribute__((preserve_access_index)); 22 7 23 8 SEC("iter/task") 24 9 int dump_task(struct bpf_iter__task *ctx)
+1 -16
tools/testing/selftests/bpf/progs/bpf_iter_test_kern4.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - #define bpf_iter_meta bpf_iter_meta___not_used 4 - #define bpf_iter__bpf_map bpf_iter__bpf_map___not_used 5 - #include "vmlinux.h" 6 - #undef bpf_iter_meta 7 - #undef bpf_iter__bpf_map 3 + #include "bpf_iter.h" 8 4 #include <bpf/bpf_helpers.h> 9 5 10 6 char _license[] SEC("license") = "GPL"; 11 - 12 - struct bpf_iter_meta { 13 - struct seq_file *seq; 14 - __u64 session_id; 15 - __u64 seq_num; 16 - } __attribute__((preserve_access_index)); 17 - 18 - struct bpf_iter__bpf_map { 19 - struct bpf_iter_meta *meta; 20 - struct bpf_map *map; 21 - } __attribute__((preserve_access_index)); 22 7 23 8 __u32 map1_id = 0, map2_id = 0; 24 9 __u32 map1_accessed = 0, map2_accessed = 0;
+1 -17
tools/testing/selftests/bpf/progs/bpf_iter_test_kern_common.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* Copyright (c) 2020 Facebook */ 3 - /* "undefine" structs in vmlinux.h, because we "override" them below */ 4 - #define bpf_iter_meta bpf_iter_meta___not_used 5 - #define bpf_iter__task bpf_iter__task___not_used 6 - #include "vmlinux.h" 7 - #undef bpf_iter_meta 8 - #undef bpf_iter__task 3 + #include "bpf_iter.h" 9 4 #include <bpf/bpf_helpers.h> 10 5 11 6 char _license[] SEC("license") = "GPL"; 12 7 int count = 0; 13 - 14 - struct bpf_iter_meta { 15 - struct seq_file *seq; 16 - __u64 session_id; 17 - __u64 seq_num; 18 - } __attribute__((preserve_access_index)); 19 - 20 - struct bpf_iter__task { 21 - struct bpf_iter_meta *meta; 22 - struct task_struct *task; 23 - } __attribute__((preserve_access_index)); 24 8 25 9 SEC("iter/task") 26 10 int dump_task(struct bpf_iter__task *ctx)