Skip to content

Commit 8fca4f9

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf_iter-BTF_ID-at-build-time'
Yonghong Song says: ==================== Commit 5a2798a ("bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros") implemented a mechanism to compute btf_ids at kernel build time which can simplify kernel implementation and reduce runtime overhead by removing in-kernel btf_id calculation. This patch set tried to use this mechanism to compute btf_ids for bpf_skc_to_*() helpers and for btf_id_or_null ctx arguments specified during bpf iterator registration. Please see individual patch for details. Changelogs: v1 -> v2: - v1 ([1]) is only for bpf_skc_to_*() helpers. This version expanded it to cover ctx btf_id_or_null arguments - abandoned the change of "extern u32 name[]" to "static u32 name[]" for BPF_ID_LIST local "name" definition. gcc 9 incurred a compilation error. [1]: https://lore.kernel.org/bpf/20200717184706.3476992-1-yhs@fb.com/T ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents e4d9c23 + 951cf36 commit 8fca4f9

12 files changed

Lines changed: 153 additions & 73 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ struct bpf_jit_poke_descriptor {
668668
struct bpf_ctx_arg_aux {
669669
u32 offset;
670670
enum bpf_reg_type reg_type;
671+
u32 btf_id;
671672
};
672673

673674
struct bpf_prog_aux {
@@ -1541,7 +1542,6 @@ static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
15411542

15421543
struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
15431544
void bpf_map_offload_map_free(struct bpf_map *map);
1544-
void init_btf_sock_ids(struct btf *btf);
15451545
#else
15461546
static inline int bpf_prog_offload_init(struct bpf_prog *prog,
15471547
union bpf_attr *attr)
@@ -1567,9 +1567,6 @@ static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
15671567
static inline void bpf_map_offload_map_free(struct bpf_map *map)
15681568
{
15691569
}
1570-
static inline void init_btf_sock_ids(struct btf *btf)
1571-
{
1572-
}
15731570
#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
15741571

15751572
#if defined(CONFIG_BPF_STREAM_PARSER)

‎include/linux/btf_ids.h‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ asm( \
5757
* .zero 4
5858
*
5959
*/
60-
#define __BTF_ID_LIST(name) \
60+
#define __BTF_ID_LIST(name, scope) \
6161
asm( \
6262
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
63-
".local " #name "; \n" \
63+
"." #scope " " #name "; \n" \
6464
#name ":; \n" \
6565
".popsection; \n"); \
6666

6767
#define BTF_ID_LIST(name) \
68-
__BTF_ID_LIST(name) \
68+
__BTF_ID_LIST(name, local) \
6969
extern u32 name[];
7070

71+
#define BTF_ID_LIST_GLOBAL(name) \
72+
__BTF_ID_LIST(name, globl)
73+
7174
/*
7275
* The BTF_ID_UNUSED macro defines 4 zero bytes.
7376
* It's used when we want to define 'unused' entry
@@ -90,7 +93,38 @@ asm( \
9093
#define BTF_ID_LIST(name) static u32 name[5];
9194
#define BTF_ID(prefix, name)
9295
#define BTF_ID_UNUSED
96+
#define BTF_ID_LIST_GLOBAL(name) u32 name[1];
9397

9498
#endif /* CONFIG_DEBUG_INFO_BTF */
9599

100+
#ifdef CONFIG_NET
101+
/* Define a list of socket types which can be the argument for
102+
* skc_to_*_sock() helpers. All these sockets should have
103+
* sock_common as the first argument in its memory layout.
104+
*/
105+
#define BTF_SOCK_TYPE_xxx \
106+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, inet_sock) \
107+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, inet_connection_sock) \
108+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, inet_request_sock) \
109+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, inet_timewait_sock) \
110+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, request_sock) \
111+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, sock) \
112+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, sock_common) \
113+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, tcp_sock) \
114+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, tcp_request_sock) \
115+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, tcp_timewait_sock) \
116+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock) \
117+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock) \
118+
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)
119+
120+
enum {
121+
#define BTF_SOCK_TYPE(name, str) name,
122+
BTF_SOCK_TYPE_xxx
123+
#undef BTF_SOCK_TYPE
124+
MAX_BTF_SOCK_TYPE,
125+
};
126+
127+
extern u32 btf_sock_ids[];
128+
#endif
129+
96130
#endif

‎kernel/bpf/btf.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,7 +3672,6 @@ struct btf *btf_parse_vmlinux(void)
36723672
goto errout;
36733673

36743674
bpf_struct_ops_init(btf, log);
3675-
init_btf_sock_ids(btf);
36763675

36773676
btf_verifier_env_free(env);
36783677
refcount_set(&btf->refcnt, 1);
@@ -3818,16 +3817,17 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
38183817
return true;
38193818

38203819
/* this is a pointer to another type */
3821-
info->reg_type = PTR_TO_BTF_ID;
38223820
for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
38233821
const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
38243822

38253823
if (ctx_arg_info->offset == off) {
38263824
info->reg_type = ctx_arg_info->reg_type;
3827-
break;
3825+
info->btf_id = ctx_arg_info->btf_id;
3826+
return true;
38283827
}
38293828
}
38303829

3830+
info->reg_type = PTR_TO_BTF_ID;
38313831
if (tgt_prog) {
38323832
ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg);
38333833
if (ret > 0) {

‎kernel/bpf/map_iter.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/fs.h>
55
#include <linux/filter.h>
66
#include <linux/kernel.h>
7+
#include <linux/btf_ids.h>
78

89
struct bpf_iter_seq_map_info {
910
u32 mid;
@@ -81,7 +82,10 @@ static const struct seq_operations bpf_map_seq_ops = {
8182
.show = bpf_map_seq_show,
8283
};
8384

84-
static const struct bpf_iter_reg bpf_map_reg_info = {
85+
BTF_ID_LIST(btf_bpf_map_id)
86+
BTF_ID(struct, bpf_map)
87+
88+
static struct bpf_iter_reg bpf_map_reg_info = {
8589
.target = "bpf_map",
8690
.seq_ops = &bpf_map_seq_ops,
8791
.init_seq_private = NULL,
@@ -96,6 +100,7 @@ static const struct bpf_iter_reg bpf_map_reg_info = {
96100

97101
static int __init bpf_map_iter_init(void)
98102
{
103+
bpf_map_reg_info.ctx_arg_info[0].btf_id = *btf_bpf_map_id;
99104
return bpf_iter_reg_target(&bpf_map_reg_info);
100105
}
101106

‎kernel/bpf/task_iter.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/fs.h>
88
#include <linux/fdtable.h>
99
#include <linux/filter.h>
10+
#include <linux/btf_ids.h>
1011

1112
struct bpf_iter_seq_task_common {
1213
struct pid_namespace *ns;
@@ -312,7 +313,11 @@ static const struct seq_operations task_file_seq_ops = {
312313
.show = task_file_seq_show,
313314
};
314315

315-
static const struct bpf_iter_reg task_reg_info = {
316+
BTF_ID_LIST(btf_task_file_ids)
317+
BTF_ID(struct, task_struct)
318+
BTF_ID(struct, file)
319+
320+
static struct bpf_iter_reg task_reg_info = {
316321
.target = "task",
317322
.seq_ops = &task_seq_ops,
318323
.init_seq_private = init_seq_pidns,
@@ -325,7 +330,7 @@ static const struct bpf_iter_reg task_reg_info = {
325330
},
326331
};
327332

328-
static const struct bpf_iter_reg task_file_reg_info = {
333+
static struct bpf_iter_reg task_file_reg_info = {
329334
.target = "task_file",
330335
.seq_ops = &task_file_seq_ops,
331336
.init_seq_private = init_seq_pidns,
@@ -344,10 +349,13 @@ static int __init task_iter_init(void)
344349
{
345350
int ret;
346351

352+
task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
347353
ret = bpf_iter_reg_target(&task_reg_info);
348354
if (ret)
349355
return ret;
350356

357+
task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
358+
task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
351359
return bpf_iter_reg_target(&task_file_reg_info);
352360
}
353361
late_initcall(task_iter_init);

‎net/core/filter.c‎

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9421,52 +9421,13 @@ void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
94219421
bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
94229422
}
94239423

9424-
/* Define a list of socket types which can be the argument for
9425-
* skc_to_*_sock() helpers. All these sockets should have
9426-
* sock_common as the first argument in its memory layout.
9427-
*/
9428-
#define BTF_SOCK_TYPE_xxx \
9429-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, "inet_sock") \
9430-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, "inet_connection_sock") \
9431-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, "inet_request_sock") \
9432-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, "inet_timewait_sock") \
9433-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, "request_sock") \
9434-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, "sock") \
9435-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, "sock_common") \
9436-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, "tcp_sock") \
9437-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, "tcp_request_sock") \
9438-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, "tcp_timewait_sock") \
9439-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, "tcp6_sock") \
9440-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, "udp_sock") \
9441-
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, "udp6_sock")
9442-
9443-
enum {
9444-
#define BTF_SOCK_TYPE(name, str) name,
9424+
#ifdef CONFIG_DEBUG_INFO_BTF
9425+
BTF_ID_LIST_GLOBAL(btf_sock_ids)
9426+
#define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
94459427
BTF_SOCK_TYPE_xxx
94469428
#undef BTF_SOCK_TYPE
9447-
MAX_BTF_SOCK_TYPE,
9448-
};
9449-
9450-
static int btf_sock_ids[MAX_BTF_SOCK_TYPE];
9451-
9452-
#ifdef CONFIG_BPF_SYSCALL
9453-
static const char *bpf_sock_types[] = {
9454-
#define BTF_SOCK_TYPE(name, str) str,
9455-
BTF_SOCK_TYPE_xxx
9456-
#undef BTF_SOCK_TYPE
9457-
};
9458-
9459-
void init_btf_sock_ids(struct btf *btf)
9460-
{
9461-
int i, btf_id;
9462-
9463-
for (i = 0; i < MAX_BTF_SOCK_TYPE; i++) {
9464-
btf_id = btf_find_by_name_kind(btf, bpf_sock_types[i],
9465-
BTF_KIND_STRUCT);
9466-
if (btf_id > 0)
9467-
btf_sock_ids[i] = btf_id;
9468-
}
9469-
}
9429+
#else
9430+
u32 btf_sock_ids[MAX_BTF_SOCK_TYPE];
94709431
#endif
94719432

94729433
static bool check_arg_btf_id(u32 btf_id, u32 arg)

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <linux/proc_fs.h>
7777
#include <linux/seq_file.h>
7878
#include <linux/inetdevice.h>
79+
#include <linux/btf_ids.h>
7980

8081
#include <crypto/hash.h>
8182
#include <linux/scatterlist.h>
@@ -2954,7 +2955,7 @@ static void bpf_iter_fini_tcp(void *priv_data)
29542955
bpf_iter_fini_seq_net(priv_data);
29552956
}
29562957

2957-
static const struct bpf_iter_reg tcp_reg_info = {
2958+
static struct bpf_iter_reg tcp_reg_info = {
29582959
.target = "tcp",
29592960
.seq_ops = &bpf_iter_tcp_seq_ops,
29602961
.init_seq_private = bpf_iter_init_tcp,
@@ -2969,6 +2970,7 @@ static const struct bpf_iter_reg tcp_reg_info = {
29692970

29702971
static void __init bpf_iter_register(void)
29712972
{
2973+
tcp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON];
29722974
if (bpf_iter_reg_target(&tcp_reg_info))
29732975
pr_warn("Warning: could not register bpf iterator tcp\n");
29742976
}

‎net/ipv4/udp.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#include <net/xfrm.h>
107107
#include <trace/events/udp.h>
108108
#include <linux/static_key.h>
109+
#include <linux/btf_ids.h>
109110
#include <trace/events/skb.h>
110111
#include <net/busy_poll.h>
111112
#include "udp_impl.h"
@@ -3232,7 +3233,7 @@ static void bpf_iter_fini_udp(void *priv_data)
32323233
bpf_iter_fini_seq_net(priv_data);
32333234
}
32343235

3235-
static const struct bpf_iter_reg udp_reg_info = {
3236+
static struct bpf_iter_reg udp_reg_info = {
32363237
.target = "udp",
32373238
.seq_ops = &bpf_iter_udp_seq_ops,
32383239
.init_seq_private = bpf_iter_init_udp,
@@ -3247,6 +3248,7 @@ static const struct bpf_iter_reg udp_reg_info = {
32473248

32483249
static void __init bpf_iter_register(void)
32493250
{
3251+
udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP];
32503252
if (bpf_iter_reg_target(&udp_reg_info))
32513253
pr_warn("Warning: could not register bpf iterator udp\n");
32523254
}

‎net/ipv6/route.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <net/l3mdev.h>
6262
#include <net/ip.h>
6363
#include <linux/uaccess.h>
64+
#include <linux/btf_ids.h>
6465

6566
#ifdef CONFIG_SYSCTL
6667
#include <linux/sysctl.h>
@@ -6423,7 +6424,10 @@ void __init ip6_route_init_special_entries(void)
64236424
#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
64246425
DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
64256426

6426-
static const struct bpf_iter_reg ipv6_route_reg_info = {
6427+
BTF_ID_LIST(btf_fib6_info_id)
6428+
BTF_ID(struct, fib6_info)
6429+
6430+
static struct bpf_iter_reg ipv6_route_reg_info = {
64276431
.target = "ipv6_route",
64286432
.seq_ops = &ipv6_route_seq_ops,
64296433
.init_seq_private = bpf_iter_init_seq_net,
@@ -6438,6 +6442,7 @@ static const struct bpf_iter_reg ipv6_route_reg_info = {
64386442

64396443
static int __init bpf_iter_register(void)
64406444
{
6445+
ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
64416446
return bpf_iter_reg_target(&ipv6_route_reg_info);
64426447
}
64436448

‎net/netlink/af_netlink.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <linux/genetlink.h>
6161
#include <linux/net_namespace.h>
6262
#include <linux/nospec.h>
63+
#include <linux/btf_ids.h>
6364

6465
#include <net/net_namespace.h>
6566
#include <net/netns/generic.h>
@@ -2803,7 +2804,10 @@ static const struct rhashtable_params netlink_rhashtable_params = {
28032804
};
28042805

28052806
#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
2806-
static const struct bpf_iter_reg netlink_reg_info = {
2807+
BTF_ID_LIST(btf_netlink_sock_id)
2808+
BTF_ID(struct, netlink_sock)
2809+
2810+
static struct bpf_iter_reg netlink_reg_info = {
28072811
.target = "netlink",
28082812
.seq_ops = &netlink_seq_ops,
28092813
.init_seq_private = bpf_iter_init_seq_net,
@@ -2818,6 +2822,7 @@ static const struct bpf_iter_reg netlink_reg_info = {
28182822

28192823
static int __init bpf_iter_register(void)
28202824
{
2825+
netlink_reg_info.ctx_arg_info[0].btf_id = *btf_netlink_sock_id;
28212826
return bpf_iter_reg_target(&netlink_reg_info);
28222827
}
28232828
#endif

0 commit comments

Comments
 (0)