Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
2 | | |
3 | | /* |
4 | | * Common eBPF ELF object loading operations. |
5 | | * |
6 | | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
7 | | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
8 | | * Copyright (C) 2015 Huawei Inc. |
9 | | * Copyright (C) 2017 Nicira, Inc. |
10 | | * Copyright (C) 2019 Isovalent, Inc. |
11 | | */ |
12 | | |
13 | | #ifndef _GNU_SOURCE |
14 | | #define _GNU_SOURCE |
15 | | #endif |
16 | | #include <stdlib.h> |
17 | | #include <stdio.h> |
18 | | #include <stdarg.h> |
19 | | #include <libgen.h> |
20 | | #include <inttypes.h> |
21 | | #include <limits.h> |
22 | | #include <string.h> |
23 | | #include <unistd.h> |
24 | | #include <endian.h> |
25 | | #include <fcntl.h> |
26 | | #include <errno.h> |
27 | | #include <ctype.h> |
28 | | #include <asm/unistd.h> |
29 | | #include <linux/err.h> |
30 | | #include <linux/kernel.h> |
31 | | #include <linux/bpf.h> |
32 | | #include <linux/btf.h> |
33 | | #include <linux/filter.h> |
34 | | #include <linux/limits.h> |
35 | | #include <linux/perf_event.h> |
36 | | #include <linux/bpf_perf_event.h> |
37 | | #include <linux/ring_buffer.h> |
38 | | #include <sys/epoll.h> |
39 | | #include <sys/ioctl.h> |
40 | | #include <sys/mman.h> |
41 | | #include <sys/stat.h> |
42 | | #include <sys/types.h> |
43 | | #include <sys/vfs.h> |
44 | | #include <sys/utsname.h> |
45 | | #include <sys/resource.h> |
46 | | #include <libelf.h> |
47 | | #include <gelf.h> |
48 | | #include <zlib.h> |
49 | | |
50 | | #include "libbpf.h" |
51 | | #include "bpf.h" |
52 | | #include "btf.h" |
53 | | #include "str_error.h" |
54 | | #include "libbpf_internal.h" |
55 | | #include "hashmap.h" |
56 | | #include "bpf_gen_internal.h" |
57 | | #include "zip.h" |
58 | | |
59 | | #ifndef BPF_FS_MAGIC |
60 | 0 | #define BPF_FS_MAGIC 0xcafe4a11 |
61 | | #endif |
62 | | |
63 | 1 | #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf" |
64 | | |
65 | 125k | #define BPF_INSN_SZ (sizeof(struct bpf_insn)) |
66 | | |
67 | | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
68 | | * compilation if user enables corresponding warning. Disable it explicitly. |
69 | | */ |
70 | | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
71 | | |
72 | | #define __printf(a, b) __attribute__((format(printf, a, b))) |
73 | | |
74 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj); |
75 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog); |
76 | | static int map_set_def_max_entries(struct bpf_map *map); |
77 | | |
78 | | static const char * const attach_type_name[] = { |
79 | | [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress", |
80 | | [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress", |
81 | | [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create", |
82 | | [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release", |
83 | | [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops", |
84 | | [BPF_CGROUP_DEVICE] = "cgroup_device", |
85 | | [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind", |
86 | | [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind", |
87 | | [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect", |
88 | | [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect", |
89 | | [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect", |
90 | | [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind", |
91 | | [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind", |
92 | | [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername", |
93 | | [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername", |
94 | | [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername", |
95 | | [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname", |
96 | | [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname", |
97 | | [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname", |
98 | | [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg", |
99 | | [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg", |
100 | | [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg", |
101 | | [BPF_CGROUP_SYSCTL] = "cgroup_sysctl", |
102 | | [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg", |
103 | | [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg", |
104 | | [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg", |
105 | | [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt", |
106 | | [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt", |
107 | | [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser", |
108 | | [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict", |
109 | | [BPF_SK_SKB_VERDICT] = "sk_skb_verdict", |
110 | | [BPF_SK_MSG_VERDICT] = "sk_msg_verdict", |
111 | | [BPF_LIRC_MODE2] = "lirc_mode2", |
112 | | [BPF_FLOW_DISSECTOR] = "flow_dissector", |
113 | | [BPF_TRACE_RAW_TP] = "trace_raw_tp", |
114 | | [BPF_TRACE_FENTRY] = "trace_fentry", |
115 | | [BPF_TRACE_FEXIT] = "trace_fexit", |
116 | | [BPF_MODIFY_RETURN] = "modify_return", |
117 | | [BPF_LSM_MAC] = "lsm_mac", |
118 | | [BPF_LSM_CGROUP] = "lsm_cgroup", |
119 | | [BPF_SK_LOOKUP] = "sk_lookup", |
120 | | [BPF_TRACE_ITER] = "trace_iter", |
121 | | [BPF_XDP_DEVMAP] = "xdp_devmap", |
122 | | [BPF_XDP_CPUMAP] = "xdp_cpumap", |
123 | | [BPF_XDP] = "xdp", |
124 | | [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select", |
125 | | [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate", |
126 | | [BPF_PERF_EVENT] = "perf_event", |
127 | | [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi", |
128 | | [BPF_STRUCT_OPS] = "struct_ops", |
129 | | [BPF_NETFILTER] = "netfilter", |
130 | | [BPF_TCX_INGRESS] = "tcx_ingress", |
131 | | [BPF_TCX_EGRESS] = "tcx_egress", |
132 | | [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi", |
133 | | [BPF_NETKIT_PRIMARY] = "netkit_primary", |
134 | | [BPF_NETKIT_PEER] = "netkit_peer", |
135 | | [BPF_TRACE_KPROBE_SESSION] = "trace_kprobe_session", |
136 | | }; |
137 | | |
138 | | static const char * const link_type_name[] = { |
139 | | [BPF_LINK_TYPE_UNSPEC] = "unspec", |
140 | | [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
141 | | [BPF_LINK_TYPE_TRACING] = "tracing", |
142 | | [BPF_LINK_TYPE_CGROUP] = "cgroup", |
143 | | [BPF_LINK_TYPE_ITER] = "iter", |
144 | | [BPF_LINK_TYPE_NETNS] = "netns", |
145 | | [BPF_LINK_TYPE_XDP] = "xdp", |
146 | | [BPF_LINK_TYPE_PERF_EVENT] = "perf_event", |
147 | | [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi", |
148 | | [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops", |
149 | | [BPF_LINK_TYPE_NETFILTER] = "netfilter", |
150 | | [BPF_LINK_TYPE_TCX] = "tcx", |
151 | | [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi", |
152 | | [BPF_LINK_TYPE_NETKIT] = "netkit", |
153 | | [BPF_LINK_TYPE_SOCKMAP] = "sockmap", |
154 | | }; |
155 | | |
156 | | static const char * const map_type_name[] = { |
157 | | [BPF_MAP_TYPE_UNSPEC] = "unspec", |
158 | | [BPF_MAP_TYPE_HASH] = "hash", |
159 | | [BPF_MAP_TYPE_ARRAY] = "array", |
160 | | [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", |
161 | | [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", |
162 | | [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", |
163 | | [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", |
164 | | [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", |
165 | | [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", |
166 | | [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", |
167 | | [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", |
168 | | [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", |
169 | | [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", |
170 | | [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", |
171 | | [BPF_MAP_TYPE_DEVMAP] = "devmap", |
172 | | [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash", |
173 | | [BPF_MAP_TYPE_SOCKMAP] = "sockmap", |
174 | | [BPF_MAP_TYPE_CPUMAP] = "cpumap", |
175 | | [BPF_MAP_TYPE_XSKMAP] = "xskmap", |
176 | | [BPF_MAP_TYPE_SOCKHASH] = "sockhash", |
177 | | [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage", |
178 | | [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray", |
179 | | [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage", |
180 | | [BPF_MAP_TYPE_QUEUE] = "queue", |
181 | | [BPF_MAP_TYPE_STACK] = "stack", |
182 | | [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", |
183 | | [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", |
184 | | [BPF_MAP_TYPE_RINGBUF] = "ringbuf", |
185 | | [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", |
186 | | [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", |
187 | | [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter", |
188 | | [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf", |
189 | | [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage", |
190 | | [BPF_MAP_TYPE_ARENA] = "arena", |
191 | | }; |
192 | | |
193 | | static const char * const prog_type_name[] = { |
194 | | [BPF_PROG_TYPE_UNSPEC] = "unspec", |
195 | | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", |
196 | | [BPF_PROG_TYPE_KPROBE] = "kprobe", |
197 | | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", |
198 | | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", |
199 | | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", |
200 | | [BPF_PROG_TYPE_XDP] = "xdp", |
201 | | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", |
202 | | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", |
203 | | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", |
204 | | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", |
205 | | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", |
206 | | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", |
207 | | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", |
208 | | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", |
209 | | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", |
210 | | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", |
211 | | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
212 | | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", |
213 | | [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", |
214 | | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", |
215 | | [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", |
216 | | [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", |
217 | | [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", |
218 | | [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable", |
219 | | [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", |
220 | | [BPF_PROG_TYPE_TRACING] = "tracing", |
221 | | [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops", |
222 | | [BPF_PROG_TYPE_EXT] = "ext", |
223 | | [BPF_PROG_TYPE_LSM] = "lsm", |
224 | | [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", |
225 | | [BPF_PROG_TYPE_SYSCALL] = "syscall", |
226 | | [BPF_PROG_TYPE_NETFILTER] = "netfilter", |
227 | | }; |
228 | | |
229 | | static int __base_pr(enum libbpf_print_level level, const char *format, |
230 | | va_list args) |
231 | 0 | { |
232 | 0 | if (level == LIBBPF_DEBUG) |
233 | 0 | return 0; |
234 | | |
235 | 0 | return vfprintf(stderr, format, args); |
236 | 0 | } |
237 | | |
238 | | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
239 | | |
240 | | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
241 | 11.0k | { |
242 | 11.0k | libbpf_print_fn_t old_print_fn; |
243 | | |
244 | 11.0k | old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED); |
245 | | |
246 | 11.0k | return old_print_fn; |
247 | 11.0k | } |
248 | | |
249 | | __printf(2, 3) |
250 | | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
251 | 128k | { |
252 | 128k | va_list args; |
253 | 128k | int old_errno; |
254 | 128k | libbpf_print_fn_t print_fn; |
255 | | |
256 | 128k | print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED); |
257 | 128k | if (!print_fn) |
258 | 0 | return; |
259 | | |
260 | 128k | old_errno = errno; |
261 | | |
262 | 128k | va_start(args, format); |
263 | 128k | __libbpf_pr(level, format, args); |
264 | 128k | va_end(args); |
265 | | |
266 | 128k | errno = old_errno; |
267 | 128k | } |
268 | | |
269 | | static void pr_perm_msg(int err) |
270 | 0 | { |
271 | 0 | struct rlimit limit; |
272 | 0 | char buf[100]; |
273 | |
|
274 | 0 | if (err != -EPERM || geteuid() != 0) |
275 | 0 | return; |
276 | | |
277 | 0 | err = getrlimit(RLIMIT_MEMLOCK, &limit); |
278 | 0 | if (err) |
279 | 0 | return; |
280 | | |
281 | 0 | if (limit.rlim_cur == RLIM_INFINITY) |
282 | 0 | return; |
283 | | |
284 | 0 | if (limit.rlim_cur < 1024) |
285 | 0 | snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur); |
286 | 0 | else if (limit.rlim_cur < 1024*1024) |
287 | 0 | snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024); |
288 | 0 | else |
289 | 0 | snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024)); |
290 | |
|
291 | 0 | pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", |
292 | 0 | buf); |
293 | 0 | } |
294 | | |
295 | | #define STRERR_BUFSIZE 128 |
296 | | |
297 | | /* Copied from tools/perf/util/util.h */ |
298 | | #ifndef zfree |
299 | 240k | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
300 | | #endif |
301 | | |
302 | | #ifndef zclose |
303 | 42.4k | # define zclose(fd) ({ \ |
304 | 42.4k | int ___err = 0; \ |
305 | 42.4k | if ((fd) >= 0) \ |
306 | 42.4k | ___err = close((fd)); \ |
307 | 42.4k | fd = -1; \ |
308 | 42.4k | ___err; }) |
309 | | #endif |
310 | | |
311 | | static inline __u64 ptr_to_u64(const void *ptr) |
312 | 0 | { |
313 | 0 | return (__u64) (unsigned long) ptr; |
314 | 0 | } |
315 | | |
316 | | int libbpf_set_strict_mode(enum libbpf_strict_mode mode) |
317 | 0 | { |
318 | | /* as of v1.0 libbpf_set_strict_mode() is a no-op */ |
319 | 0 | return 0; |
320 | 0 | } |
321 | | |
322 | | __u32 libbpf_major_version(void) |
323 | 0 | { |
324 | 0 | return LIBBPF_MAJOR_VERSION; |
325 | 0 | } |
326 | | |
327 | | __u32 libbpf_minor_version(void) |
328 | 0 | { |
329 | 0 | return LIBBPF_MINOR_VERSION; |
330 | 0 | } |
331 | | |
332 | | const char *libbpf_version_string(void) |
333 | 0 | { |
334 | 0 | #define __S(X) #X |
335 | 0 | #define _S(X) __S(X) |
336 | 0 | return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION); |
337 | 0 | #undef _S |
338 | 0 | #undef __S |
339 | 0 | } |
340 | | |
341 | | enum reloc_type { |
342 | | RELO_LD64, |
343 | | RELO_CALL, |
344 | | RELO_DATA, |
345 | | RELO_EXTERN_LD64, |
346 | | RELO_EXTERN_CALL, |
347 | | RELO_SUBPROG_ADDR, |
348 | | RELO_CORE, |
349 | | }; |
350 | | |
351 | | struct reloc_desc { |
352 | | enum reloc_type type; |
353 | | int insn_idx; |
354 | | union { |
355 | | const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */ |
356 | | struct { |
357 | | int map_idx; |
358 | | int sym_off; |
359 | | int ext_idx; |
360 | | }; |
361 | | }; |
362 | | }; |
363 | | |
364 | | /* stored as sec_def->cookie for all libbpf-supported SEC()s */ |
365 | | enum sec_def_flags { |
366 | | SEC_NONE = 0, |
367 | | /* expected_attach_type is optional, if kernel doesn't support that */ |
368 | | SEC_EXP_ATTACH_OPT = 1, |
369 | | /* legacy, only used by libbpf_get_type_names() and |
370 | | * libbpf_attach_type_by_name(), not used by libbpf itself at all. |
371 | | * This used to be associated with cgroup (and few other) BPF programs |
372 | | * that were attachable through BPF_PROG_ATTACH command. Pretty |
373 | | * meaningless nowadays, though. |
374 | | */ |
375 | | SEC_ATTACHABLE = 2, |
376 | | SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT, |
377 | | /* attachment target is specified through BTF ID in either kernel or |
378 | | * other BPF program's BTF object |
379 | | */ |
380 | | SEC_ATTACH_BTF = 4, |
381 | | /* BPF program type allows sleeping/blocking in kernel */ |
382 | | SEC_SLEEPABLE = 8, |
383 | | /* BPF program support non-linear XDP buffer */ |
384 | | SEC_XDP_FRAGS = 16, |
385 | | /* Setup proper attach type for usdt probes. */ |
386 | | SEC_USDT = 32, |
387 | | }; |
388 | | |
389 | | struct bpf_sec_def { |
390 | | char *sec; |
391 | | enum bpf_prog_type prog_type; |
392 | | enum bpf_attach_type expected_attach_type; |
393 | | long cookie; |
394 | | int handler_id; |
395 | | |
396 | | libbpf_prog_setup_fn_t prog_setup_fn; |
397 | | libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; |
398 | | libbpf_prog_attach_fn_t prog_attach_fn; |
399 | | }; |
400 | | |
401 | | /* |
402 | | * bpf_prog should be a better name but it has been used in |
403 | | * linux/filter.h. |
404 | | */ |
405 | | struct bpf_program { |
406 | | char *name; |
407 | | char *sec_name; |
408 | | size_t sec_idx; |
409 | | const struct bpf_sec_def *sec_def; |
410 | | /* this program's instruction offset (in number of instructions) |
411 | | * within its containing ELF section |
412 | | */ |
413 | | size_t sec_insn_off; |
414 | | /* number of original instructions in ELF section belonging to this |
415 | | * program, not taking into account subprogram instructions possible |
416 | | * appended later during relocation |
417 | | */ |
418 | | size_t sec_insn_cnt; |
419 | | /* Offset (in number of instructions) of the start of instruction |
420 | | * belonging to this BPF program within its containing main BPF |
421 | | * program. For the entry-point (main) BPF program, this is always |
422 | | * zero. For a sub-program, this gets reset before each of main BPF |
423 | | * programs are processed and relocated and is used to determined |
424 | | * whether sub-program was already appended to the main program, and |
425 | | * if yes, at which instruction offset. |
426 | | */ |
427 | | size_t sub_insn_off; |
428 | | |
429 | | /* instructions that belong to BPF program; insns[0] is located at |
430 | | * sec_insn_off instruction within its ELF section in ELF file, so |
431 | | * when mapping ELF file instruction index to the local instruction, |
432 | | * one needs to subtract sec_insn_off; and vice versa. |
433 | | */ |
434 | | struct bpf_insn *insns; |
435 | | /* actual number of instruction in this BPF program's image; for |
436 | | * entry-point BPF programs this includes the size of main program |
437 | | * itself plus all the used sub-programs, appended at the end |
438 | | */ |
439 | | size_t insns_cnt; |
440 | | |
441 | | struct reloc_desc *reloc_desc; |
442 | | int nr_reloc; |
443 | | |
444 | | /* BPF verifier log settings */ |
445 | | char *log_buf; |
446 | | size_t log_size; |
447 | | __u32 log_level; |
448 | | |
449 | | struct bpf_object *obj; |
450 | | |
451 | | int fd; |
452 | | bool autoload; |
453 | | bool autoattach; |
454 | | bool sym_global; |
455 | | bool mark_btf_static; |
456 | | enum bpf_prog_type type; |
457 | | enum bpf_attach_type expected_attach_type; |
458 | | int exception_cb_idx; |
459 | | |
460 | | int prog_ifindex; |
461 | | __u32 attach_btf_obj_fd; |
462 | | __u32 attach_btf_id; |
463 | | __u32 attach_prog_fd; |
464 | | |
465 | | void *func_info; |
466 | | __u32 func_info_rec_size; |
467 | | __u32 func_info_cnt; |
468 | | |
469 | | void *line_info; |
470 | | __u32 line_info_rec_size; |
471 | | __u32 line_info_cnt; |
472 | | __u32 prog_flags; |
473 | | }; |
474 | | |
475 | | struct bpf_struct_ops { |
476 | | const char *tname; |
477 | | const struct btf_type *type; |
478 | | struct bpf_program **progs; |
479 | | __u32 *kern_func_off; |
480 | | /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ |
481 | | void *data; |
482 | | /* e.g. struct bpf_struct_ops_tcp_congestion_ops in |
483 | | * btf_vmlinux's format. |
484 | | * struct bpf_struct_ops_tcp_congestion_ops { |
485 | | * [... some other kernel fields ...] |
486 | | * struct tcp_congestion_ops data; |
487 | | * } |
488 | | * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops) |
489 | | * bpf_map__init_kern_struct_ops() will populate the "kern_vdata" |
490 | | * from "data". |
491 | | */ |
492 | | void *kern_vdata; |
493 | | __u32 type_id; |
494 | | }; |
495 | | |
496 | 2.22k | #define DATA_SEC ".data" |
497 | 2.18k | #define BSS_SEC ".bss" |
498 | 1.70k | #define RODATA_SEC ".rodata" |
499 | 7.25k | #define KCONFIG_SEC ".kconfig" |
500 | 9.51k | #define KSYMS_SEC ".ksyms" |
501 | 4.11k | #define STRUCT_OPS_SEC ".struct_ops" |
502 | 3.81k | #define STRUCT_OPS_LINK_SEC ".struct_ops.link" |
503 | 840 | #define ARENA_SEC ".addr_space.1" |
504 | | |
505 | | enum libbpf_map_type { |
506 | | LIBBPF_MAP_UNSPEC, |
507 | | LIBBPF_MAP_DATA, |
508 | | LIBBPF_MAP_BSS, |
509 | | LIBBPF_MAP_RODATA, |
510 | | LIBBPF_MAP_KCONFIG, |
511 | | }; |
512 | | |
513 | | struct bpf_map_def { |
514 | | unsigned int type; |
515 | | unsigned int key_size; |
516 | | unsigned int value_size; |
517 | | unsigned int max_entries; |
518 | | unsigned int map_flags; |
519 | | }; |
520 | | |
521 | | struct bpf_map { |
522 | | struct bpf_object *obj; |
523 | | char *name; |
524 | | /* real_name is defined for special internal maps (.rodata*, |
525 | | * .data*, .bss, .kconfig) and preserves their original ELF section |
526 | | * name. This is important to be able to find corresponding BTF |
527 | | * DATASEC information. |
528 | | */ |
529 | | char *real_name; |
530 | | int fd; |
531 | | int sec_idx; |
532 | | size_t sec_offset; |
533 | | int map_ifindex; |
534 | | int inner_map_fd; |
535 | | struct bpf_map_def def; |
536 | | __u32 numa_node; |
537 | | __u32 btf_var_idx; |
538 | | int mod_btf_fd; |
539 | | __u32 btf_key_type_id; |
540 | | __u32 btf_value_type_id; |
541 | | __u32 btf_vmlinux_value_type_id; |
542 | | enum libbpf_map_type libbpf_type; |
543 | | void *mmaped; |
544 | | struct bpf_struct_ops *st_ops; |
545 | | struct bpf_map *inner_map; |
546 | | void **init_slots; |
547 | | int init_slots_sz; |
548 | | char *pin_path; |
549 | | bool pinned; |
550 | | bool reused; |
551 | | bool autocreate; |
552 | | __u64 map_extra; |
553 | | }; |
554 | | |
555 | | enum extern_type { |
556 | | EXT_UNKNOWN, |
557 | | EXT_KCFG, |
558 | | EXT_KSYM, |
559 | | }; |
560 | | |
561 | | enum kcfg_type { |
562 | | KCFG_UNKNOWN, |
563 | | KCFG_CHAR, |
564 | | KCFG_BOOL, |
565 | | KCFG_INT, |
566 | | KCFG_TRISTATE, |
567 | | KCFG_CHAR_ARR, |
568 | | }; |
569 | | |
570 | | struct extern_desc { |
571 | | enum extern_type type; |
572 | | int sym_idx; |
573 | | int btf_id; |
574 | | int sec_btf_id; |
575 | | const char *name; |
576 | | char *essent_name; |
577 | | bool is_set; |
578 | | bool is_weak; |
579 | | union { |
580 | | struct { |
581 | | enum kcfg_type type; |
582 | | int sz; |
583 | | int align; |
584 | | int data_off; |
585 | | bool is_signed; |
586 | | } kcfg; |
587 | | struct { |
588 | | unsigned long long addr; |
589 | | |
590 | | /* target btf_id of the corresponding kernel var. */ |
591 | | int kernel_btf_obj_fd; |
592 | | int kernel_btf_id; |
593 | | |
594 | | /* local btf_id of the ksym extern's type. */ |
595 | | __u32 type_id; |
596 | | /* BTF fd index to be patched in for insn->off, this is |
597 | | * 0 for vmlinux BTF, index in obj->fd_array for module |
598 | | * BTF |
599 | | */ |
600 | | __s16 btf_fd_idx; |
601 | | } ksym; |
602 | | }; |
603 | | }; |
604 | | |
605 | | struct module_btf { |
606 | | struct btf *btf; |
607 | | char *name; |
608 | | __u32 id; |
609 | | int fd; |
610 | | int fd_array_idx; |
611 | | }; |
612 | | |
613 | | enum sec_type { |
614 | | SEC_UNUSED = 0, |
615 | | SEC_RELO, |
616 | | SEC_BSS, |
617 | | SEC_DATA, |
618 | | SEC_RODATA, |
619 | | SEC_ST_OPS, |
620 | | }; |
621 | | |
622 | | struct elf_sec_desc { |
623 | | enum sec_type sec_type; |
624 | | Elf64_Shdr *shdr; |
625 | | Elf_Data *data; |
626 | | }; |
627 | | |
628 | | struct elf_state { |
629 | | int fd; |
630 | | const void *obj_buf; |
631 | | size_t obj_buf_sz; |
632 | | Elf *elf; |
633 | | Elf64_Ehdr *ehdr; |
634 | | Elf_Data *symbols; |
635 | | Elf_Data *arena_data; |
636 | | size_t shstrndx; /* section index for section name strings */ |
637 | | size_t strtabidx; |
638 | | struct elf_sec_desc *secs; |
639 | | size_t sec_cnt; |
640 | | int btf_maps_shndx; |
641 | | __u32 btf_maps_sec_btf_id; |
642 | | int text_shndx; |
643 | | int symbols_shndx; |
644 | | bool has_st_ops; |
645 | | int arena_data_shndx; |
646 | | }; |
647 | | |
648 | | struct usdt_manager; |
649 | | |
650 | | struct bpf_object { |
651 | | char name[BPF_OBJ_NAME_LEN]; |
652 | | char license[64]; |
653 | | __u32 kern_version; |
654 | | |
655 | | struct bpf_program *programs; |
656 | | size_t nr_programs; |
657 | | struct bpf_map *maps; |
658 | | size_t nr_maps; |
659 | | size_t maps_cap; |
660 | | |
661 | | char *kconfig; |
662 | | struct extern_desc *externs; |
663 | | int nr_extern; |
664 | | int kconfig_map_idx; |
665 | | |
666 | | bool loaded; |
667 | | bool has_subcalls; |
668 | | bool has_rodata; |
669 | | |
670 | | struct bpf_gen *gen_loader; |
671 | | |
672 | | /* Information when doing ELF related work. Only valid if efile.elf is not NULL */ |
673 | | struct elf_state efile; |
674 | | |
675 | | struct btf *btf; |
676 | | struct btf_ext *btf_ext; |
677 | | |
678 | | /* Parse and load BTF vmlinux if any of the programs in the object need |
679 | | * it at load time. |
680 | | */ |
681 | | struct btf *btf_vmlinux; |
682 | | /* Path to the custom BTF to be used for BPF CO-RE relocations as an |
683 | | * override for vmlinux BTF. |
684 | | */ |
685 | | char *btf_custom_path; |
686 | | /* vmlinux BTF override for CO-RE relocations */ |
687 | | struct btf *btf_vmlinux_override; |
688 | | /* Lazily initialized kernel module BTFs */ |
689 | | struct module_btf *btf_modules; |
690 | | bool btf_modules_loaded; |
691 | | size_t btf_module_cnt; |
692 | | size_t btf_module_cap; |
693 | | |
694 | | /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */ |
695 | | char *log_buf; |
696 | | size_t log_size; |
697 | | __u32 log_level; |
698 | | |
699 | | int *fd_array; |
700 | | size_t fd_array_cap; |
701 | | size_t fd_array_cnt; |
702 | | |
703 | | struct usdt_manager *usdt_man; |
704 | | |
705 | | struct bpf_map *arena_map; |
706 | | void *arena_data; |
707 | | size_t arena_data_sz; |
708 | | |
709 | | struct kern_feature_cache *feat_cache; |
710 | | char *token_path; |
711 | | int token_fd; |
712 | | |
713 | | char path[]; |
714 | | }; |
715 | | |
716 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off); |
717 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off); |
718 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx); |
719 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name); |
720 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn); |
721 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn); |
722 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn); |
723 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx); |
724 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx); |
725 | | |
726 | | void bpf_program__unload(struct bpf_program *prog) |
727 | 28.6k | { |
728 | 28.6k | if (!prog) |
729 | 0 | return; |
730 | | |
731 | 28.6k | zclose(prog->fd); |
732 | | |
733 | 28.6k | zfree(&prog->func_info); |
734 | 28.6k | zfree(&prog->line_info); |
735 | 28.6k | } |
736 | | |
737 | | static void bpf_program__exit(struct bpf_program *prog) |
738 | 14.3k | { |
739 | 14.3k | if (!prog) |
740 | 0 | return; |
741 | | |
742 | 14.3k | bpf_program__unload(prog); |
743 | 14.3k | zfree(&prog->name); |
744 | 14.3k | zfree(&prog->sec_name); |
745 | 14.3k | zfree(&prog->insns); |
746 | 14.3k | zfree(&prog->reloc_desc); |
747 | | |
748 | 14.3k | prog->nr_reloc = 0; |
749 | 14.3k | prog->insns_cnt = 0; |
750 | 14.3k | prog->sec_idx = -1; |
751 | 14.3k | } |
752 | | |
753 | | static bool insn_is_subprog_call(const struct bpf_insn *insn) |
754 | 0 | { |
755 | 0 | return BPF_CLASS(insn->code) == BPF_JMP && |
756 | 0 | BPF_OP(insn->code) == BPF_CALL && |
757 | 0 | BPF_SRC(insn->code) == BPF_K && |
758 | 0 | insn->src_reg == BPF_PSEUDO_CALL && |
759 | 0 | insn->dst_reg == 0 && |
760 | 0 | insn->off == 0; |
761 | 0 | } |
762 | | |
763 | | static bool is_call_insn(const struct bpf_insn *insn) |
764 | 3.52k | { |
765 | 3.52k | return insn->code == (BPF_JMP | BPF_CALL); |
766 | 3.52k | } |
767 | | |
768 | | static bool insn_is_pseudo_func(struct bpf_insn *insn) |
769 | 0 | { |
770 | 0 | return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; |
771 | 0 | } |
772 | | |
773 | | static int |
774 | | bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, |
775 | | const char *name, size_t sec_idx, const char *sec_name, |
776 | | size_t sec_off, void *insn_data, size_t insn_data_sz) |
777 | 14.4k | { |
778 | 14.4k | if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) { |
779 | 58 | pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n", |
780 | 58 | sec_name, name, sec_off, insn_data_sz); |
781 | 58 | return -EINVAL; |
782 | 58 | } |
783 | | |
784 | 14.3k | memset(prog, 0, sizeof(*prog)); |
785 | 14.3k | prog->obj = obj; |
786 | | |
787 | 14.3k | prog->sec_idx = sec_idx; |
788 | 14.3k | prog->sec_insn_off = sec_off / BPF_INSN_SZ; |
789 | 14.3k | prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; |
790 | | /* insns_cnt can later be increased by appending used subprograms */ |
791 | 14.3k | prog->insns_cnt = prog->sec_insn_cnt; |
792 | | |
793 | 14.3k | prog->type = BPF_PROG_TYPE_UNSPEC; |
794 | 14.3k | prog->fd = -1; |
795 | 14.3k | prog->exception_cb_idx = -1; |
796 | | |
797 | | /* libbpf's convention for SEC("?abc...") is that it's just like |
798 | | * SEC("abc...") but the corresponding bpf_program starts out with |
799 | | * autoload set to false. |
800 | | */ |
801 | 14.3k | if (sec_name[0] == '?') { |
802 | 214 | prog->autoload = false; |
803 | | /* from now on forget there was ? in section name */ |
804 | 214 | sec_name++; |
805 | 14.1k | } else { |
806 | 14.1k | prog->autoload = true; |
807 | 14.1k | } |
808 | | |
809 | 14.3k | prog->autoattach = true; |
810 | | |
811 | | /* inherit object's log_level */ |
812 | 14.3k | prog->log_level = obj->log_level; |
813 | | |
814 | 14.3k | prog->sec_name = strdup(sec_name); |
815 | 14.3k | if (!prog->sec_name) |
816 | 0 | goto errout; |
817 | | |
818 | 14.3k | prog->name = strdup(name); |
819 | 14.3k | if (!prog->name) |
820 | 0 | goto errout; |
821 | | |
822 | 14.3k | prog->insns = malloc(insn_data_sz); |
823 | 14.3k | if (!prog->insns) |
824 | 21 | goto errout; |
825 | 14.3k | memcpy(prog->insns, insn_data, insn_data_sz); |
826 | | |
827 | 14.3k | return 0; |
828 | 21 | errout: |
829 | 21 | pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name); |
830 | 21 | bpf_program__exit(prog); |
831 | 21 | return -ENOMEM; |
832 | 14.3k | } |
833 | | |
834 | | static int |
835 | | bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data, |
836 | | const char *sec_name, int sec_idx) |
837 | 1.12k | { |
838 | 1.12k | Elf_Data *symbols = obj->efile.symbols; |
839 | 1.12k | struct bpf_program *prog, *progs; |
840 | 1.12k | void *data = sec_data->d_buf; |
841 | 1.12k | size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; |
842 | 1.12k | int nr_progs, err, i; |
843 | 1.12k | const char *name; |
844 | 1.12k | Elf64_Sym *sym; |
845 | | |
846 | 1.12k | progs = obj->programs; |
847 | 1.12k | nr_progs = obj->nr_programs; |
848 | 1.12k | nr_syms = symbols->d_size / sizeof(Elf64_Sym); |
849 | | |
850 | 124k | for (i = 0; i < nr_syms; i++) { |
851 | 123k | sym = elf_sym_by_idx(obj, i); |
852 | | |
853 | 123k | if (sym->st_shndx != sec_idx) |
854 | 107k | continue; |
855 | 16.1k | if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) |
856 | 1.54k | continue; |
857 | | |
858 | 14.5k | prog_sz = sym->st_size; |
859 | 14.5k | sec_off = sym->st_value; |
860 | | |
861 | 14.5k | name = elf_sym_str(obj, sym->st_name); |
862 | 14.5k | if (!name) { |
863 | 59 | pr_warn("sec '%s': failed to get symbol name for offset %zu\n", |
864 | 59 | sec_name, sec_off); |
865 | 59 | return -LIBBPF_ERRNO__FORMAT; |
866 | 59 | } |
867 | | |
868 | 14.5k | if (sec_off + prog_sz > sec_sz) { |
869 | 106 | pr_warn("sec '%s': program at offset %zu crosses section boundary\n", |
870 | 106 | sec_name, sec_off); |
871 | 106 | return -LIBBPF_ERRNO__FORMAT; |
872 | 106 | } |
873 | | |
874 | 14.4k | if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { |
875 | 3 | pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name); |
876 | 3 | return -ENOTSUP; |
877 | 3 | } |
878 | | |
879 | 14.4k | pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n", |
880 | 28.8k | sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz); |
881 | | |
882 | 14.4k | progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs)); |
883 | 14.4k | if (!progs) { |
884 | | /* |
885 | | * In this case the original obj->programs |
886 | | * is still valid, so don't need special treat for |
887 | | * bpf_close_object(). |
888 | | */ |
889 | 0 | pr_warn("sec '%s': failed to alloc memory for new program '%s'\n", |
890 | 0 | sec_name, name); |
891 | 0 | return -ENOMEM; |
892 | 0 | } |
893 | 14.4k | obj->programs = progs; |
894 | | |
895 | 14.4k | prog = &progs[nr_progs]; |
896 | | |
897 | 14.4k | err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name, |
898 | 14.4k | sec_off, data + sec_off, prog_sz); |
899 | 14.4k | if (err) |
900 | 79 | return err; |
901 | | |
902 | 14.3k | if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) |
903 | 14.1k | prog->sym_global = true; |
904 | | |
905 | | /* if function is a global/weak symbol, but has restricted |
906 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC |
907 | | * as static to enable more permissive BPF verification mode |
908 | | * with more outside context available to BPF verifier |
909 | | */ |
910 | 14.3k | if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
911 | 14.1k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) |
912 | 10.8k | prog->mark_btf_static = true; |
913 | | |
914 | 14.3k | nr_progs++; |
915 | 14.3k | obj->nr_programs = nr_progs; |
916 | 14.3k | } |
917 | | |
918 | 881 | return 0; |
919 | 1.12k | } |
920 | | |
921 | | static const struct btf_member * |
922 | | find_member_by_offset(const struct btf_type *t, __u32 bit_offset) |
923 | 0 | { |
924 | 0 | struct btf_member *m; |
925 | 0 | int i; |
926 | |
|
927 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
928 | 0 | if (btf_member_bit_offset(t, i) == bit_offset) |
929 | 0 | return m; |
930 | 0 | } |
931 | | |
932 | 0 | return NULL; |
933 | 0 | } |
934 | | |
935 | | static const struct btf_member * |
936 | | find_member_by_name(const struct btf *btf, const struct btf_type *t, |
937 | | const char *name) |
938 | 0 | { |
939 | 0 | struct btf_member *m; |
940 | 0 | int i; |
941 | |
|
942 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
943 | 0 | if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) |
944 | 0 | return m; |
945 | 0 | } |
946 | | |
947 | 0 | return NULL; |
948 | 0 | } |
949 | | |
950 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
951 | | __u16 kind, struct btf **res_btf, |
952 | | struct module_btf **res_mod_btf); |
953 | | |
954 | 0 | #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" |
955 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
956 | | const char *name, __u32 kind); |
957 | | |
958 | | static int |
959 | | find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw, |
960 | | struct module_btf **mod_btf, |
961 | | const struct btf_type **type, __u32 *type_id, |
962 | | const struct btf_type **vtype, __u32 *vtype_id, |
963 | | const struct btf_member **data_member) |
964 | 0 | { |
965 | 0 | const struct btf_type *kern_type, *kern_vtype; |
966 | 0 | const struct btf_member *kern_data_member; |
967 | 0 | struct btf *btf; |
968 | 0 | __s32 kern_vtype_id, kern_type_id; |
969 | 0 | char tname[256]; |
970 | 0 | __u32 i; |
971 | |
|
972 | 0 | snprintf(tname, sizeof(tname), "%.*s", |
973 | 0 | (int)bpf_core_essential_name_len(tname_raw), tname_raw); |
974 | |
|
975 | 0 | kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT, |
976 | 0 | &btf, mod_btf); |
977 | 0 | if (kern_type_id < 0) { |
978 | 0 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", |
979 | 0 | tname); |
980 | 0 | return kern_type_id; |
981 | 0 | } |
982 | 0 | kern_type = btf__type_by_id(btf, kern_type_id); |
983 | | |
984 | | /* Find the corresponding "map_value" type that will be used |
985 | | * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example, |
986 | | * find "struct bpf_struct_ops_tcp_congestion_ops" from the |
987 | | * btf_vmlinux. |
988 | | */ |
989 | 0 | kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX, |
990 | 0 | tname, BTF_KIND_STRUCT); |
991 | 0 | if (kern_vtype_id < 0) { |
992 | 0 | pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n", |
993 | 0 | STRUCT_OPS_VALUE_PREFIX, tname); |
994 | 0 | return kern_vtype_id; |
995 | 0 | } |
996 | 0 | kern_vtype = btf__type_by_id(btf, kern_vtype_id); |
997 | | |
998 | | /* Find "struct tcp_congestion_ops" from |
999 | | * struct bpf_struct_ops_tcp_congestion_ops { |
1000 | | * [ ... ] |
1001 | | * struct tcp_congestion_ops data; |
1002 | | * } |
1003 | | */ |
1004 | 0 | kern_data_member = btf_members(kern_vtype); |
1005 | 0 | for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) { |
1006 | 0 | if (kern_data_member->type == kern_type_id) |
1007 | 0 | break; |
1008 | 0 | } |
1009 | 0 | if (i == btf_vlen(kern_vtype)) { |
1010 | 0 | pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n", |
1011 | 0 | tname, STRUCT_OPS_VALUE_PREFIX, tname); |
1012 | 0 | return -EINVAL; |
1013 | 0 | } |
1014 | | |
1015 | 0 | *type = kern_type; |
1016 | 0 | *type_id = kern_type_id; |
1017 | 0 | *vtype = kern_vtype; |
1018 | 0 | *vtype_id = kern_vtype_id; |
1019 | 0 | *data_member = kern_data_member; |
1020 | |
|
1021 | 0 | return 0; |
1022 | 0 | } |
1023 | | |
1024 | | static bool bpf_map__is_struct_ops(const struct bpf_map *map) |
1025 | 282 | { |
1026 | 282 | return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; |
1027 | 282 | } |
1028 | | |
1029 | | static bool is_valid_st_ops_program(struct bpf_object *obj, |
1030 | | const struct bpf_program *prog) |
1031 | 0 | { |
1032 | 0 | int i; |
1033 | |
|
1034 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
1035 | 0 | if (&obj->programs[i] == prog) |
1036 | 0 | return prog->type == BPF_PROG_TYPE_STRUCT_OPS; |
1037 | 0 | } |
1038 | | |
1039 | 0 | return false; |
1040 | 0 | } |
1041 | | |
1042 | | /* For each struct_ops program P, referenced from some struct_ops map M, |
1043 | | * enable P.autoload if there are Ms for which M.autocreate is true, |
1044 | | * disable P.autoload if for all Ms M.autocreate is false. |
1045 | | * Don't change P.autoload for programs that are not referenced from any maps. |
1046 | | */ |
1047 | | static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj) |
1048 | 0 | { |
1049 | 0 | struct bpf_program *prog, *slot_prog; |
1050 | 0 | struct bpf_map *map; |
1051 | 0 | int i, j, k, vlen; |
1052 | |
|
1053 | 0 | for (i = 0; i < obj->nr_programs; ++i) { |
1054 | 0 | int should_load = false; |
1055 | 0 | int use_cnt = 0; |
1056 | |
|
1057 | 0 | prog = &obj->programs[i]; |
1058 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) |
1059 | 0 | continue; |
1060 | | |
1061 | 0 | for (j = 0; j < obj->nr_maps; ++j) { |
1062 | 0 | map = &obj->maps[j]; |
1063 | 0 | if (!bpf_map__is_struct_ops(map)) |
1064 | 0 | continue; |
1065 | | |
1066 | 0 | vlen = btf_vlen(map->st_ops->type); |
1067 | 0 | for (k = 0; k < vlen; ++k) { |
1068 | 0 | slot_prog = map->st_ops->progs[k]; |
1069 | 0 | if (prog != slot_prog) |
1070 | 0 | continue; |
1071 | | |
1072 | 0 | use_cnt++; |
1073 | 0 | if (map->autocreate) |
1074 | 0 | should_load = true; |
1075 | 0 | } |
1076 | 0 | } |
1077 | 0 | if (use_cnt) |
1078 | 0 | prog->autoload = should_load; |
1079 | 0 | } |
1080 | |
|
1081 | 0 | return 0; |
1082 | 0 | } |
1083 | | |
1084 | | /* Init the map's fields that depend on kern_btf */ |
1085 | | static int bpf_map__init_kern_struct_ops(struct bpf_map *map) |
1086 | 0 | { |
1087 | 0 | const struct btf_member *member, *kern_member, *kern_data_member; |
1088 | 0 | const struct btf_type *type, *kern_type, *kern_vtype; |
1089 | 0 | __u32 i, kern_type_id, kern_vtype_id, kern_data_off; |
1090 | 0 | struct bpf_object *obj = map->obj; |
1091 | 0 | const struct btf *btf = obj->btf; |
1092 | 0 | struct bpf_struct_ops *st_ops; |
1093 | 0 | const struct btf *kern_btf; |
1094 | 0 | struct module_btf *mod_btf; |
1095 | 0 | void *data, *kern_data; |
1096 | 0 | const char *tname; |
1097 | 0 | int err; |
1098 | |
|
1099 | 0 | st_ops = map->st_ops; |
1100 | 0 | type = st_ops->type; |
1101 | 0 | tname = st_ops->tname; |
1102 | 0 | err = find_struct_ops_kern_types(obj, tname, &mod_btf, |
1103 | 0 | &kern_type, &kern_type_id, |
1104 | 0 | &kern_vtype, &kern_vtype_id, |
1105 | 0 | &kern_data_member); |
1106 | 0 | if (err) |
1107 | 0 | return err; |
1108 | | |
1109 | 0 | kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux; |
1110 | |
|
1111 | 0 | pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n", |
1112 | 0 | map->name, st_ops->type_id, kern_type_id, kern_vtype_id); |
1113 | |
|
1114 | 0 | map->mod_btf_fd = mod_btf ? mod_btf->fd : -1; |
1115 | 0 | map->def.value_size = kern_vtype->size; |
1116 | 0 | map->btf_vmlinux_value_type_id = kern_vtype_id; |
1117 | |
|
1118 | 0 | st_ops->kern_vdata = calloc(1, kern_vtype->size); |
1119 | 0 | if (!st_ops->kern_vdata) |
1120 | 0 | return -ENOMEM; |
1121 | | |
1122 | 0 | data = st_ops->data; |
1123 | 0 | kern_data_off = kern_data_member->offset / 8; |
1124 | 0 | kern_data = st_ops->kern_vdata + kern_data_off; |
1125 | |
|
1126 | 0 | member = btf_members(type); |
1127 | 0 | for (i = 0; i < btf_vlen(type); i++, member++) { |
1128 | 0 | const struct btf_type *mtype, *kern_mtype; |
1129 | 0 | __u32 mtype_id, kern_mtype_id; |
1130 | 0 | void *mdata, *kern_mdata; |
1131 | 0 | struct bpf_program *prog; |
1132 | 0 | __s64 msize, kern_msize; |
1133 | 0 | __u32 moff, kern_moff; |
1134 | 0 | __u32 kern_member_idx; |
1135 | 0 | const char *mname; |
1136 | |
|
1137 | 0 | mname = btf__name_by_offset(btf, member->name_off); |
1138 | 0 | moff = member->offset / 8; |
1139 | 0 | mdata = data + moff; |
1140 | 0 | msize = btf__resolve_size(btf, member->type); |
1141 | 0 | if (msize < 0) { |
1142 | 0 | pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n", |
1143 | 0 | map->name, mname); |
1144 | 0 | return msize; |
1145 | 0 | } |
1146 | | |
1147 | 0 | kern_member = find_member_by_name(kern_btf, kern_type, mname); |
1148 | 0 | if (!kern_member) { |
1149 | 0 | if (!libbpf_is_mem_zeroed(mdata, msize)) { |
1150 | 0 | pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", |
1151 | 0 | map->name, mname); |
1152 | 0 | return -ENOTSUP; |
1153 | 0 | } |
1154 | | |
1155 | 0 | if (st_ops->progs[i]) { |
1156 | | /* If we had declaratively set struct_ops callback, we need to |
1157 | | * force its autoload to false, because it doesn't have |
1158 | | * a chance of succeeding from POV of the current struct_ops map. |
1159 | | * If this program is still referenced somewhere else, though, |
1160 | | * then bpf_object_adjust_struct_ops_autoload() will update its |
1161 | | * autoload accordingly. |
1162 | | */ |
1163 | 0 | st_ops->progs[i]->autoload = false; |
1164 | 0 | st_ops->progs[i] = NULL; |
1165 | 0 | } |
1166 | | |
1167 | | /* Skip all-zero/NULL fields if they are not present in the kernel BTF */ |
1168 | 0 | pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n", |
1169 | 0 | map->name, mname); |
1170 | 0 | continue; |
1171 | 0 | } |
1172 | | |
1173 | 0 | kern_member_idx = kern_member - btf_members(kern_type); |
1174 | 0 | if (btf_member_bitfield_size(type, i) || |
1175 | 0 | btf_member_bitfield_size(kern_type, kern_member_idx)) { |
1176 | 0 | pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n", |
1177 | 0 | map->name, mname); |
1178 | 0 | return -ENOTSUP; |
1179 | 0 | } |
1180 | | |
1181 | 0 | kern_moff = kern_member->offset / 8; |
1182 | 0 | kern_mdata = kern_data + kern_moff; |
1183 | |
|
1184 | 0 | mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); |
1185 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, |
1186 | 0 | &kern_mtype_id); |
1187 | 0 | if (BTF_INFO_KIND(mtype->info) != |
1188 | 0 | BTF_INFO_KIND(kern_mtype->info)) { |
1189 | 0 | pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n", |
1190 | 0 | map->name, mname, BTF_INFO_KIND(mtype->info), |
1191 | 0 | BTF_INFO_KIND(kern_mtype->info)); |
1192 | 0 | return -ENOTSUP; |
1193 | 0 | } |
1194 | | |
1195 | 0 | if (btf_is_ptr(mtype)) { |
1196 | 0 | prog = *(void **)mdata; |
1197 | | /* just like for !kern_member case above, reset declaratively |
1198 | | * set (at compile time) program's autload to false, |
1199 | | * if user replaced it with another program or NULL |
1200 | | */ |
1201 | 0 | if (st_ops->progs[i] && st_ops->progs[i] != prog) |
1202 | 0 | st_ops->progs[i]->autoload = false; |
1203 | | |
1204 | | /* Update the value from the shadow type */ |
1205 | 0 | st_ops->progs[i] = prog; |
1206 | 0 | if (!prog) |
1207 | 0 | continue; |
1208 | | |
1209 | 0 | if (!is_valid_st_ops_program(obj, prog)) { |
1210 | 0 | pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n", |
1211 | 0 | map->name, mname); |
1212 | 0 | return -ENOTSUP; |
1213 | 0 | } |
1214 | | |
1215 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, |
1216 | 0 | kern_mtype->type, |
1217 | 0 | &kern_mtype_id); |
1218 | | |
1219 | | /* mtype->type must be a func_proto which was |
1220 | | * guaranteed in bpf_object__collect_st_ops_relos(), |
1221 | | * so only check kern_mtype for func_proto here. |
1222 | | */ |
1223 | 0 | if (!btf_is_func_proto(kern_mtype)) { |
1224 | 0 | pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n", |
1225 | 0 | map->name, mname); |
1226 | 0 | return -ENOTSUP; |
1227 | 0 | } |
1228 | | |
1229 | 0 | if (mod_btf) |
1230 | 0 | prog->attach_btf_obj_fd = mod_btf->fd; |
1231 | | |
1232 | | /* if we haven't yet processed this BPF program, record proper |
1233 | | * attach_btf_id and member_idx |
1234 | | */ |
1235 | 0 | if (!prog->attach_btf_id) { |
1236 | 0 | prog->attach_btf_id = kern_type_id; |
1237 | 0 | prog->expected_attach_type = kern_member_idx; |
1238 | 0 | } |
1239 | | |
1240 | | /* struct_ops BPF prog can be re-used between multiple |
1241 | | * .struct_ops & .struct_ops.link as long as it's the |
1242 | | * same struct_ops struct definition and the same |
1243 | | * function pointer field |
1244 | | */ |
1245 | 0 | if (prog->attach_btf_id != kern_type_id) { |
1246 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: attach_btf_id %u != kern_type_id %u\n", |
1247 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1248 | 0 | prog->attach_btf_id, kern_type_id); |
1249 | 0 | return -EINVAL; |
1250 | 0 | } |
1251 | 0 | if (prog->expected_attach_type != kern_member_idx) { |
1252 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: expected_attach_type %u != kern_member_idx %u\n", |
1253 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1254 | 0 | prog->expected_attach_type, kern_member_idx); |
1255 | 0 | return -EINVAL; |
1256 | 0 | } |
1257 | | |
1258 | 0 | st_ops->kern_func_off[i] = kern_data_off + kern_moff; |
1259 | |
|
1260 | 0 | pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n", |
1261 | 0 | map->name, mname, prog->name, moff, |
1262 | 0 | kern_moff); |
1263 | |
|
1264 | 0 | continue; |
1265 | 0 | } |
1266 | | |
1267 | 0 | kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); |
1268 | 0 | if (kern_msize < 0 || msize != kern_msize) { |
1269 | 0 | pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", |
1270 | 0 | map->name, mname, (ssize_t)msize, |
1271 | 0 | (ssize_t)kern_msize); |
1272 | 0 | return -ENOTSUP; |
1273 | 0 | } |
1274 | | |
1275 | 0 | pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n", |
1276 | 0 | map->name, mname, (unsigned int)msize, |
1277 | 0 | moff, kern_moff); |
1278 | 0 | memcpy(kern_mdata, mdata, msize); |
1279 | 0 | } |
1280 | | |
1281 | 0 | return 0; |
1282 | 0 | } |
1283 | | |
1284 | | static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) |
1285 | 0 | { |
1286 | 0 | struct bpf_map *map; |
1287 | 0 | size_t i; |
1288 | 0 | int err; |
1289 | |
|
1290 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
1291 | 0 | map = &obj->maps[i]; |
1292 | |
|
1293 | 0 | if (!bpf_map__is_struct_ops(map)) |
1294 | 0 | continue; |
1295 | | |
1296 | 0 | if (!map->autocreate) |
1297 | 0 | continue; |
1298 | | |
1299 | 0 | err = bpf_map__init_kern_struct_ops(map); |
1300 | 0 | if (err) |
1301 | 0 | return err; |
1302 | 0 | } |
1303 | | |
1304 | 0 | return 0; |
1305 | 0 | } |
1306 | | |
1307 | | static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, |
1308 | | int shndx, Elf_Data *data) |
1309 | 175 | { |
1310 | 175 | const struct btf_type *type, *datasec; |
1311 | 175 | const struct btf_var_secinfo *vsi; |
1312 | 175 | struct bpf_struct_ops *st_ops; |
1313 | 175 | const char *tname, *var_name; |
1314 | 175 | __s32 type_id, datasec_id; |
1315 | 175 | const struct btf *btf; |
1316 | 175 | struct bpf_map *map; |
1317 | 175 | __u32 i; |
1318 | | |
1319 | 175 | if (shndx == -1) |
1320 | 0 | return 0; |
1321 | | |
1322 | 175 | btf = obj->btf; |
1323 | 175 | datasec_id = btf__find_by_name_kind(btf, sec_name, |
1324 | 175 | BTF_KIND_DATASEC); |
1325 | 175 | if (datasec_id < 0) { |
1326 | 81 | pr_warn("struct_ops init: DATASEC %s not found\n", |
1327 | 81 | sec_name); |
1328 | 81 | return -EINVAL; |
1329 | 81 | } |
1330 | | |
1331 | 94 | datasec = btf__type_by_id(btf, datasec_id); |
1332 | 94 | vsi = btf_var_secinfos(datasec); |
1333 | 122 | for (i = 0; i < btf_vlen(datasec); i++, vsi++) { |
1334 | 70 | type = btf__type_by_id(obj->btf, vsi->type); |
1335 | 70 | var_name = btf__name_by_offset(obj->btf, type->name_off); |
1336 | | |
1337 | 70 | type_id = btf__resolve_type(obj->btf, vsi->type); |
1338 | 70 | if (type_id < 0) { |
1339 | 11 | pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n", |
1340 | 11 | vsi->type, sec_name); |
1341 | 11 | return -EINVAL; |
1342 | 11 | } |
1343 | | |
1344 | 59 | type = btf__type_by_id(obj->btf, type_id); |
1345 | 59 | tname = btf__name_by_offset(obj->btf, type->name_off); |
1346 | 59 | if (!tname[0]) { |
1347 | 1 | pr_warn("struct_ops init: anonymous type is not supported\n"); |
1348 | 1 | return -ENOTSUP; |
1349 | 1 | } |
1350 | 58 | if (!btf_is_struct(type)) { |
1351 | 10 | pr_warn("struct_ops init: %s is not a struct\n", tname); |
1352 | 10 | return -EINVAL; |
1353 | 10 | } |
1354 | | |
1355 | 48 | map = bpf_object__add_map(obj); |
1356 | 48 | if (IS_ERR(map)) |
1357 | 0 | return PTR_ERR(map); |
1358 | | |
1359 | 48 | map->sec_idx = shndx; |
1360 | 48 | map->sec_offset = vsi->offset; |
1361 | 48 | map->name = strdup(var_name); |
1362 | 48 | if (!map->name) |
1363 | 0 | return -ENOMEM; |
1364 | 48 | map->btf_value_type_id = type_id; |
1365 | | |
1366 | | /* Follow same convention as for programs autoload: |
1367 | | * SEC("?.struct_ops") means map is not created by default. |
1368 | | */ |
1369 | 48 | if (sec_name[0] == '?') { |
1370 | 1 | map->autocreate = false; |
1371 | | /* from now on forget there was ? in section name */ |
1372 | 1 | sec_name++; |
1373 | 1 | } |
1374 | | |
1375 | 48 | map->def.type = BPF_MAP_TYPE_STRUCT_OPS; |
1376 | 48 | map->def.key_size = sizeof(int); |
1377 | 48 | map->def.value_size = type->size; |
1378 | 48 | map->def.max_entries = 1; |
1379 | 48 | map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0; |
1380 | | |
1381 | 48 | map->st_ops = calloc(1, sizeof(*map->st_ops)); |
1382 | 48 | if (!map->st_ops) |
1383 | 0 | return -ENOMEM; |
1384 | 48 | st_ops = map->st_ops; |
1385 | 48 | st_ops->data = malloc(type->size); |
1386 | 48 | st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); |
1387 | 48 | st_ops->kern_func_off = malloc(btf_vlen(type) * |
1388 | 48 | sizeof(*st_ops->kern_func_off)); |
1389 | 48 | if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) |
1390 | 0 | return -ENOMEM; |
1391 | | |
1392 | 48 | if (vsi->offset + type->size > data->d_size) { |
1393 | 20 | pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n", |
1394 | 20 | var_name, sec_name); |
1395 | 20 | return -EINVAL; |
1396 | 20 | } |
1397 | | |
1398 | 28 | memcpy(st_ops->data, |
1399 | 28 | data->d_buf + vsi->offset, |
1400 | 28 | type->size); |
1401 | 28 | st_ops->tname = tname; |
1402 | 28 | st_ops->type = type; |
1403 | 28 | st_ops->type_id = type_id; |
1404 | | |
1405 | 28 | pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n", |
1406 | 28 | tname, type_id, var_name, vsi->offset); |
1407 | 28 | } |
1408 | | |
1409 | 52 | return 0; |
1410 | 94 | } |
1411 | | |
1412 | | static int bpf_object_init_struct_ops(struct bpf_object *obj) |
1413 | 2.06k | { |
1414 | 2.06k | const char *sec_name; |
1415 | 2.06k | int sec_idx, err; |
1416 | | |
1417 | 17.1k | for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) { |
1418 | 15.1k | struct elf_sec_desc *desc = &obj->efile.secs[sec_idx]; |
1419 | | |
1420 | 15.1k | if (desc->sec_type != SEC_ST_OPS) |
1421 | 14.9k | continue; |
1422 | | |
1423 | 175 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1424 | 175 | if (!sec_name) |
1425 | 0 | return -LIBBPF_ERRNO__FORMAT; |
1426 | | |
1427 | 175 | err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data); |
1428 | 175 | if (err) |
1429 | 123 | return err; |
1430 | 175 | } |
1431 | | |
1432 | 1.94k | return 0; |
1433 | 2.06k | } |
1434 | | |
1435 | | static struct bpf_object *bpf_object__new(const char *path, |
1436 | | const void *obj_buf, |
1437 | | size_t obj_buf_sz, |
1438 | | const char *obj_name) |
1439 | 11.0k | { |
1440 | 11.0k | struct bpf_object *obj; |
1441 | 11.0k | char *end; |
1442 | | |
1443 | 11.0k | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
1444 | 11.0k | if (!obj) { |
1445 | 0 | pr_warn("alloc memory failed for %s\n", path); |
1446 | 0 | return ERR_PTR(-ENOMEM); |
1447 | 0 | } |
1448 | | |
1449 | 11.0k | strcpy(obj->path, path); |
1450 | 11.0k | if (obj_name) { |
1451 | 11.0k | libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); |
1452 | 11.0k | } else { |
1453 | | /* Using basename() GNU version which doesn't modify arg. */ |
1454 | 0 | libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); |
1455 | 0 | end = strchr(obj->name, '.'); |
1456 | 0 | if (end) |
1457 | 0 | *end = 0; |
1458 | 0 | } |
1459 | | |
1460 | 11.0k | obj->efile.fd = -1; |
1461 | | /* |
1462 | | * Caller of this function should also call |
1463 | | * bpf_object__elf_finish() after data collection to return |
1464 | | * obj_buf to user. If not, we should duplicate the buffer to |
1465 | | * avoid user freeing them before elf finish. |
1466 | | */ |
1467 | 11.0k | obj->efile.obj_buf = obj_buf; |
1468 | 11.0k | obj->efile.obj_buf_sz = obj_buf_sz; |
1469 | 11.0k | obj->efile.btf_maps_shndx = -1; |
1470 | 11.0k | obj->kconfig_map_idx = -1; |
1471 | | |
1472 | 11.0k | obj->kern_version = get_kernel_version(); |
1473 | 11.0k | obj->loaded = false; |
1474 | | |
1475 | 11.0k | return obj; |
1476 | 11.0k | } |
1477 | | |
1478 | | static void bpf_object__elf_finish(struct bpf_object *obj) |
1479 | 14.8k | { |
1480 | 14.8k | if (!obj->efile.elf) |
1481 | 3.85k | return; |
1482 | | |
1483 | 10.9k | elf_end(obj->efile.elf); |
1484 | 10.9k | obj->efile.elf = NULL; |
1485 | 10.9k | obj->efile.symbols = NULL; |
1486 | 10.9k | obj->efile.arena_data = NULL; |
1487 | | |
1488 | 10.9k | zfree(&obj->efile.secs); |
1489 | 10.9k | obj->efile.sec_cnt = 0; |
1490 | 10.9k | zclose(obj->efile.fd); |
1491 | 10.9k | obj->efile.obj_buf = NULL; |
1492 | 10.9k | obj->efile.obj_buf_sz = 0; |
1493 | 10.9k | } |
1494 | | |
1495 | | static int bpf_object__elf_init(struct bpf_object *obj) |
1496 | 11.0k | { |
1497 | 11.0k | Elf64_Ehdr *ehdr; |
1498 | 11.0k | int err = 0; |
1499 | 11.0k | Elf *elf; |
1500 | | |
1501 | 11.0k | if (obj->efile.elf) { |
1502 | 0 | pr_warn("elf: init internal error\n"); |
1503 | 0 | return -LIBBPF_ERRNO__LIBELF; |
1504 | 0 | } |
1505 | | |
1506 | 11.0k | if (obj->efile.obj_buf_sz > 0) { |
1507 | | /* obj_buf should have been validated by bpf_object__open_mem(). */ |
1508 | 11.0k | elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); |
1509 | 11.0k | } else { |
1510 | 0 | obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); |
1511 | 0 | if (obj->efile.fd < 0) { |
1512 | 0 | char errmsg[STRERR_BUFSIZE], *cp; |
1513 | |
|
1514 | 0 | err = -errno; |
1515 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
1516 | 0 | pr_warn("elf: failed to open %s: %s\n", obj->path, cp); |
1517 | 0 | return err; |
1518 | 0 | } |
1519 | | |
1520 | 0 | elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); |
1521 | 0 | } |
1522 | | |
1523 | 11.0k | if (!elf) { |
1524 | 81 | pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); |
1525 | 81 | err = -LIBBPF_ERRNO__LIBELF; |
1526 | 81 | goto errout; |
1527 | 81 | } |
1528 | | |
1529 | 10.9k | obj->efile.elf = elf; |
1530 | | |
1531 | 10.9k | if (elf_kind(elf) != ELF_K_ELF) { |
1532 | 115 | err = -LIBBPF_ERRNO__FORMAT; |
1533 | 115 | pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); |
1534 | 115 | goto errout; |
1535 | 115 | } |
1536 | | |
1537 | 10.8k | if (gelf_getclass(elf) != ELFCLASS64) { |
1538 | 476 | err = -LIBBPF_ERRNO__FORMAT; |
1539 | 476 | pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); |
1540 | 476 | goto errout; |
1541 | 476 | } |
1542 | | |
1543 | 10.3k | obj->efile.ehdr = ehdr = elf64_getehdr(elf); |
1544 | 10.3k | if (!obj->efile.ehdr) { |
1545 | 0 | pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); |
1546 | 0 | err = -LIBBPF_ERRNO__FORMAT; |
1547 | 0 | goto errout; |
1548 | 0 | } |
1549 | | |
1550 | 10.3k | if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { |
1551 | 42 | pr_warn("elf: failed to get section names section index for %s: %s\n", |
1552 | 42 | obj->path, elf_errmsg(-1)); |
1553 | 42 | err = -LIBBPF_ERRNO__FORMAT; |
1554 | 42 | goto errout; |
1555 | 42 | } |
1556 | | |
1557 | | /* ELF is corrupted/truncated, avoid calling elf_strptr. */ |
1558 | 10.3k | if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { |
1559 | 1.09k | pr_warn("elf: failed to get section names strings from %s: %s\n", |
1560 | 1.09k | obj->path, elf_errmsg(-1)); |
1561 | 1.09k | err = -LIBBPF_ERRNO__FORMAT; |
1562 | 1.09k | goto errout; |
1563 | 1.09k | } |
1564 | | |
1565 | | /* Old LLVM set e_machine to EM_NONE */ |
1566 | 9.26k | if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { |
1567 | 440 | pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); |
1568 | 440 | err = -LIBBPF_ERRNO__FORMAT; |
1569 | 440 | goto errout; |
1570 | 440 | } |
1571 | | |
1572 | 8.82k | return 0; |
1573 | 2.24k | errout: |
1574 | 2.24k | bpf_object__elf_finish(obj); |
1575 | 2.24k | return err; |
1576 | 9.26k | } |
1577 | | |
1578 | | static int bpf_object__check_endianness(struct bpf_object *obj) |
1579 | 8.82k | { |
1580 | 8.82k | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
1581 | 8.82k | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) |
1582 | 8.79k | return 0; |
1583 | | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
1584 | | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
1585 | | return 0; |
1586 | | #else |
1587 | | # error "Unrecognized __BYTE_ORDER__" |
1588 | | #endif |
1589 | 24 | pr_warn("elf: endianness mismatch in %s.\n", obj->path); |
1590 | 24 | return -LIBBPF_ERRNO__ENDIAN; |
1591 | 8.82k | } |
1592 | | |
1593 | | static int |
1594 | | bpf_object__init_license(struct bpf_object *obj, void *data, size_t size) |
1595 | 611 | { |
1596 | 611 | if (!data) { |
1597 | 2 | pr_warn("invalid license section in %s\n", obj->path); |
1598 | 2 | return -LIBBPF_ERRNO__FORMAT; |
1599 | 2 | } |
1600 | | /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't |
1601 | | * go over allowed ELF data section buffer |
1602 | | */ |
1603 | 609 | libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); |
1604 | 609 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
1605 | 609 | return 0; |
1606 | 611 | } |
1607 | | |
1608 | | static int |
1609 | | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
1610 | 87 | { |
1611 | 87 | __u32 kver; |
1612 | | |
1613 | 87 | if (!data || size != sizeof(kver)) { |
1614 | 17 | pr_warn("invalid kver section in %s\n", obj->path); |
1615 | 17 | return -LIBBPF_ERRNO__FORMAT; |
1616 | 17 | } |
1617 | 70 | memcpy(&kver, data, sizeof(kver)); |
1618 | 70 | obj->kern_version = kver; |
1619 | 70 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
1620 | 70 | return 0; |
1621 | 87 | } |
1622 | | |
1623 | | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
1624 | 133 | { |
1625 | 133 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
1626 | 133 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
1627 | 47 | return true; |
1628 | 86 | return false; |
1629 | 133 | } |
1630 | | |
1631 | | static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size) |
1632 | 401 | { |
1633 | 401 | Elf_Data *data; |
1634 | 401 | Elf_Scn *scn; |
1635 | | |
1636 | 401 | if (!name) |
1637 | 0 | return -EINVAL; |
1638 | | |
1639 | 401 | scn = elf_sec_by_name(obj, name); |
1640 | 401 | data = elf_sec_data(obj, scn); |
1641 | 401 | if (data) { |
1642 | 260 | *size = data->d_size; |
1643 | 260 | return 0; /* found it */ |
1644 | 260 | } |
1645 | | |
1646 | 141 | return -ENOENT; |
1647 | 401 | } |
1648 | | |
1649 | | static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name) |
1650 | 1.75k | { |
1651 | 1.75k | Elf_Data *symbols = obj->efile.symbols; |
1652 | 1.75k | const char *sname; |
1653 | 1.75k | size_t si; |
1654 | | |
1655 | 76.8k | for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { |
1656 | 76.5k | Elf64_Sym *sym = elf_sym_by_idx(obj, si); |
1657 | | |
1658 | 76.5k | if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) |
1659 | 69.1k | continue; |
1660 | | |
1661 | 7.45k | if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && |
1662 | 7.45k | ELF64_ST_BIND(sym->st_info) != STB_WEAK) |
1663 | 4.94k | continue; |
1664 | | |
1665 | 2.50k | sname = elf_sym_str(obj, sym->st_name); |
1666 | 2.50k | if (!sname) { |
1667 | 33 | pr_warn("failed to get sym name string for var %s\n", name); |
1668 | 33 | return ERR_PTR(-EIO); |
1669 | 33 | } |
1670 | 2.47k | if (strcmp(name, sname) == 0) |
1671 | 1.49k | return sym; |
1672 | 2.47k | } |
1673 | | |
1674 | 224 | return ERR_PTR(-ENOENT); |
1675 | 1.75k | } |
1676 | | |
1677 | | /* Some versions of Android don't provide memfd_create() in their libc |
1678 | | * implementation, so avoid complications and just go straight to Linux |
1679 | | * syscall. |
1680 | | */ |
1681 | | static int sys_memfd_create(const char *name, unsigned flags) |
1682 | 2.75k | { |
1683 | 2.75k | return syscall(__NR_memfd_create, name, flags); |
1684 | 2.75k | } |
1685 | | |
1686 | | #ifndef MFD_CLOEXEC |
1687 | | #define MFD_CLOEXEC 0x0001U |
1688 | | #endif |
1689 | | |
1690 | | static int create_placeholder_fd(void) |
1691 | 2.75k | { |
1692 | 2.75k | int fd; |
1693 | | |
1694 | 2.75k | fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC)); |
1695 | 2.75k | if (fd < 0) |
1696 | 0 | return -errno; |
1697 | 2.75k | return fd; |
1698 | 2.75k | } |
1699 | | |
1700 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
1701 | 2.75k | { |
1702 | 2.75k | struct bpf_map *map; |
1703 | 2.75k | int err; |
1704 | | |
1705 | 2.75k | err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, |
1706 | 2.75k | sizeof(*obj->maps), obj->nr_maps + 1); |
1707 | 2.75k | if (err) |
1708 | 0 | return ERR_PTR(err); |
1709 | | |
1710 | 2.75k | map = &obj->maps[obj->nr_maps++]; |
1711 | 2.75k | map->obj = obj; |
1712 | | /* Preallocate map FD without actually creating BPF map just yet. |
1713 | | * These map FD "placeholders" will be reused later without changing |
1714 | | * FD value when map is actually created in the kernel. |
1715 | | * |
1716 | | * This is useful to be able to perform BPF program relocations |
1717 | | * without having to create BPF maps before that step. This allows us |
1718 | | * to finalize and load BTF very late in BPF object's loading phase, |
1719 | | * right before BPF maps have to be created and BPF programs have to |
1720 | | * be loaded. By having these map FD placeholders we can perform all |
1721 | | * the sanitizations, relocations, and any other adjustments before we |
1722 | | * start creating actual BPF kernel objects (BTF, maps, progs). |
1723 | | */ |
1724 | 2.75k | map->fd = create_placeholder_fd(); |
1725 | 2.75k | if (map->fd < 0) |
1726 | 0 | return ERR_PTR(map->fd); |
1727 | 2.75k | map->inner_map_fd = -1; |
1728 | 2.75k | map->autocreate = true; |
1729 | | |
1730 | 2.75k | return map; |
1731 | 2.75k | } |
1732 | | |
1733 | | static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries) |
1734 | 3.43k | { |
1735 | 3.43k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1736 | 3.43k | size_t map_sz; |
1737 | | |
1738 | 3.43k | map_sz = (size_t)roundup(value_sz, 8) * max_entries; |
1739 | 3.43k | map_sz = roundup(map_sz, page_sz); |
1740 | 3.43k | return map_sz; |
1741 | 3.43k | } |
1742 | | |
1743 | | static size_t bpf_map_mmap_sz(const struct bpf_map *map) |
1744 | 3.43k | { |
1745 | 3.43k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1746 | | |
1747 | 3.43k | switch (map->def.type) { |
1748 | 3.43k | case BPF_MAP_TYPE_ARRAY: |
1749 | 3.43k | return array_map_mmap_sz(map->def.value_size, map->def.max_entries); |
1750 | 0 | case BPF_MAP_TYPE_ARENA: |
1751 | 0 | return page_sz * map->def.max_entries; |
1752 | 0 | default: |
1753 | 0 | return 0; /* not supported */ |
1754 | 3.43k | } |
1755 | 3.43k | } |
1756 | | |
1757 | | static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz) |
1758 | 0 | { |
1759 | 0 | void *mmaped; |
1760 | |
|
1761 | 0 | if (!map->mmaped) |
1762 | 0 | return -EINVAL; |
1763 | | |
1764 | 0 | if (old_sz == new_sz) |
1765 | 0 | return 0; |
1766 | | |
1767 | 0 | mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1768 | 0 | if (mmaped == MAP_FAILED) |
1769 | 0 | return -errno; |
1770 | | |
1771 | 0 | memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); |
1772 | 0 | munmap(map->mmaped, old_sz); |
1773 | 0 | map->mmaped = mmaped; |
1774 | 0 | return 0; |
1775 | 0 | } |
1776 | | |
1777 | | static char *internal_map_name(struct bpf_object *obj, const char *real_name) |
1778 | 1.76k | { |
1779 | 1.76k | char map_name[BPF_OBJ_NAME_LEN], *p; |
1780 | 1.76k | int pfx_len, sfx_len = max((size_t)7, strlen(real_name)); |
1781 | | |
1782 | | /* This is one of the more confusing parts of libbpf for various |
1783 | | * reasons, some of which are historical. The original idea for naming |
1784 | | * internal names was to include as much of BPF object name prefix as |
1785 | | * possible, so that it can be distinguished from similar internal |
1786 | | * maps of a different BPF object. |
1787 | | * As an example, let's say we have bpf_object named 'my_object_name' |
1788 | | * and internal map corresponding to '.rodata' ELF section. The final |
1789 | | * map name advertised to user and to the kernel will be |
1790 | | * 'my_objec.rodata', taking first 8 characters of object name and |
1791 | | * entire 7 characters of '.rodata'. |
1792 | | * Somewhat confusingly, if internal map ELF section name is shorter |
1793 | | * than 7 characters, e.g., '.bss', we still reserve 7 characters |
1794 | | * for the suffix, even though we only have 4 actual characters, and |
1795 | | * resulting map will be called 'my_objec.bss', not even using all 15 |
1796 | | * characters allowed by the kernel. Oh well, at least the truncated |
1797 | | * object name is somewhat consistent in this case. But if the map |
1798 | | * name is '.kconfig', we'll still have entirety of '.kconfig' added |
1799 | | * (8 chars) and thus will be left with only first 7 characters of the |
1800 | | * object name ('my_obje'). Happy guessing, user, that the final map |
1801 | | * name will be "my_obje.kconfig". |
1802 | | * Now, with libbpf starting to support arbitrarily named .rodata.* |
1803 | | * and .data.* data sections, it's possible that ELF section name is |
1804 | | * longer than allowed 15 chars, so we now need to be careful to take |
1805 | | * only up to 15 first characters of ELF name, taking no BPF object |
1806 | | * name characters at all. So '.rodata.abracadabra' will result in |
1807 | | * '.rodata.abracad' kernel and user-visible name. |
1808 | | * We need to keep this convoluted logic intact for .data, .bss and |
1809 | | * .rodata maps, but for new custom .data.custom and .rodata.custom |
1810 | | * maps we use their ELF names as is, not prepending bpf_object name |
1811 | | * in front. We still need to truncate them to 15 characters for the |
1812 | | * kernel. Full name can be recovered for such maps by using DATASEC |
1813 | | * BTF type associated with such map's value type, though. |
1814 | | */ |
1815 | 1.76k | if (sfx_len >= BPF_OBJ_NAME_LEN) |
1816 | 348 | sfx_len = BPF_OBJ_NAME_LEN - 1; |
1817 | | |
1818 | | /* if there are two or more dots in map name, it's a custom dot map */ |
1819 | 1.76k | if (strchr(real_name + 1, '.') != NULL) |
1820 | 1.31k | pfx_len = 0; |
1821 | 451 | else |
1822 | 451 | pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); |
1823 | | |
1824 | 1.76k | snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, |
1825 | 1.76k | sfx_len, real_name); |
1826 | | |
1827 | | /* sanitise map name to characters allowed by kernel */ |
1828 | 23.4k | for (p = map_name; *p && p < map_name + sizeof(map_name); p++) |
1829 | 21.6k | if (!isalnum(*p) && *p != '_' && *p != '.') |
1830 | 3.45k | *p = '_'; |
1831 | | |
1832 | 1.76k | return strdup(map_name); |
1833 | 1.76k | } |
1834 | | |
1835 | | static int |
1836 | | map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map); |
1837 | | |
1838 | | /* Internal BPF map is mmap()'able only if at least one of corresponding |
1839 | | * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL |
1840 | | * variable and it's not marked as __hidden (which turns it into, effectively, |
1841 | | * a STATIC variable). |
1842 | | */ |
1843 | | static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map) |
1844 | 1.76k | { |
1845 | 1.76k | const struct btf_type *t, *vt; |
1846 | 1.76k | struct btf_var_secinfo *vsi; |
1847 | 1.76k | int i, n; |
1848 | | |
1849 | 1.76k | if (!map->btf_value_type_id) |
1850 | 1.60k | return false; |
1851 | | |
1852 | 158 | t = btf__type_by_id(obj->btf, map->btf_value_type_id); |
1853 | 158 | if (!btf_is_datasec(t)) |
1854 | 28 | return false; |
1855 | | |
1856 | 130 | vsi = btf_var_secinfos(t); |
1857 | 215 | for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) { |
1858 | 176 | vt = btf__type_by_id(obj->btf, vsi->type); |
1859 | 176 | if (!btf_is_var(vt)) |
1860 | 64 | continue; |
1861 | | |
1862 | 112 | if (btf_var(vt)->linkage != BTF_VAR_STATIC) |
1863 | 91 | return true; |
1864 | 112 | } |
1865 | | |
1866 | 39 | return false; |
1867 | 130 | } |
1868 | | |
1869 | | static int |
1870 | | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
1871 | | const char *real_name, int sec_idx, void *data, size_t data_sz) |
1872 | 1.76k | { |
1873 | 1.76k | struct bpf_map_def *def; |
1874 | 1.76k | struct bpf_map *map; |
1875 | 1.76k | size_t mmap_sz; |
1876 | 1.76k | int err; |
1877 | | |
1878 | 1.76k | map = bpf_object__add_map(obj); |
1879 | 1.76k | if (IS_ERR(map)) |
1880 | 0 | return PTR_ERR(map); |
1881 | | |
1882 | 1.76k | map->libbpf_type = type; |
1883 | 1.76k | map->sec_idx = sec_idx; |
1884 | 1.76k | map->sec_offset = 0; |
1885 | 1.76k | map->real_name = strdup(real_name); |
1886 | 1.76k | map->name = internal_map_name(obj, real_name); |
1887 | 1.76k | if (!map->real_name || !map->name) { |
1888 | 0 | zfree(&map->real_name); |
1889 | 0 | zfree(&map->name); |
1890 | 0 | return -ENOMEM; |
1891 | 0 | } |
1892 | | |
1893 | 1.76k | def = &map->def; |
1894 | 1.76k | def->type = BPF_MAP_TYPE_ARRAY; |
1895 | 1.76k | def->key_size = sizeof(int); |
1896 | 1.76k | def->value_size = data_sz; |
1897 | 1.76k | def->max_entries = 1; |
1898 | 1.76k | def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG |
1899 | 1.76k | ? BPF_F_RDONLY_PROG : 0; |
1900 | | |
1901 | | /* failures are fine because of maps like .rodata.str1.1 */ |
1902 | 1.76k | (void) map_fill_btf_type_info(obj, map); |
1903 | | |
1904 | 1.76k | if (map_is_mmapable(obj, map)) |
1905 | 91 | def->map_flags |= BPF_F_MMAPABLE; |
1906 | | |
1907 | 1.76k | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
1908 | 1.76k | map->name, map->sec_idx, map->sec_offset, def->map_flags); |
1909 | | |
1910 | 1.76k | mmap_sz = bpf_map_mmap_sz(map); |
1911 | 1.76k | map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, |
1912 | 1.76k | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1913 | 1.76k | if (map->mmaped == MAP_FAILED) { |
1914 | 94 | err = -errno; |
1915 | 94 | map->mmaped = NULL; |
1916 | 94 | pr_warn("failed to alloc map '%s' content buffer: %d\n", |
1917 | 94 | map->name, err); |
1918 | 94 | zfree(&map->real_name); |
1919 | 94 | zfree(&map->name); |
1920 | 94 | return err; |
1921 | 94 | } |
1922 | | |
1923 | 1.67k | if (data) |
1924 | 861 | memcpy(map->mmaped, data, data_sz); |
1925 | | |
1926 | 1.67k | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
1927 | 1.67k | return 0; |
1928 | 1.76k | } |
1929 | | |
1930 | | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
1931 | 2.15k | { |
1932 | 2.15k | struct elf_sec_desc *sec_desc; |
1933 | 2.15k | const char *sec_name; |
1934 | 2.15k | int err = 0, sec_idx; |
1935 | | |
1936 | | /* |
1937 | | * Populate obj->maps with libbpf internal maps. |
1938 | | */ |
1939 | 17.1k | for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { |
1940 | 14.9k | sec_desc = &obj->efile.secs[sec_idx]; |
1941 | | |
1942 | | /* Skip recognized sections with size 0. */ |
1943 | 14.9k | if (!sec_desc->data || sec_desc->data->d_size == 0) |
1944 | 12.1k | continue; |
1945 | | |
1946 | 2.80k | switch (sec_desc->sec_type) { |
1947 | 502 | case SEC_DATA: |
1948 | 502 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1949 | 502 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
1950 | 502 | sec_name, sec_idx, |
1951 | 502 | sec_desc->data->d_buf, |
1952 | 502 | sec_desc->data->d_size); |
1953 | 502 | break; |
1954 | 359 | case SEC_RODATA: |
1955 | 359 | obj->has_rodata = true; |
1956 | 359 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1957 | 359 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
1958 | 359 | sec_name, sec_idx, |
1959 | 359 | sec_desc->data->d_buf, |
1960 | 359 | sec_desc->data->d_size); |
1961 | 359 | break; |
1962 | 824 | case SEC_BSS: |
1963 | 824 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1964 | 824 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
1965 | 824 | sec_name, sec_idx, |
1966 | 824 | NULL, |
1967 | 824 | sec_desc->data->d_size); |
1968 | 824 | break; |
1969 | 1.12k | default: |
1970 | | /* skip */ |
1971 | 1.12k | break; |
1972 | 2.80k | } |
1973 | 2.80k | if (err) |
1974 | 29 | return err; |
1975 | 2.80k | } |
1976 | 2.12k | return 0; |
1977 | 2.15k | } |
1978 | | |
1979 | | |
1980 | | static struct extern_desc *find_extern_by_name(const struct bpf_object *obj, |
1981 | | const void *name) |
1982 | 950 | { |
1983 | 950 | int i; |
1984 | | |
1985 | 2.69k | for (i = 0; i < obj->nr_extern; i++) { |
1986 | 2.20k | if (strcmp(obj->externs[i].name, name) == 0) |
1987 | 458 | return &obj->externs[i]; |
1988 | 2.20k | } |
1989 | 492 | return NULL; |
1990 | 950 | } |
1991 | | |
1992 | | static struct extern_desc *find_extern_by_name_with_len(const struct bpf_object *obj, |
1993 | | const void *name, int len) |
1994 | 0 | { |
1995 | 0 | const char *ext_name; |
1996 | 0 | int i; |
1997 | |
|
1998 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
1999 | 0 | ext_name = obj->externs[i].name; |
2000 | 0 | if (strlen(ext_name) == len && strncmp(ext_name, name, len) == 0) |
2001 | 0 | return &obj->externs[i]; |
2002 | 0 | } |
2003 | 0 | return NULL; |
2004 | 0 | } |
2005 | | |
2006 | | static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, |
2007 | | char value) |
2008 | 0 | { |
2009 | 0 | switch (ext->kcfg.type) { |
2010 | 0 | case KCFG_BOOL: |
2011 | 0 | if (value == 'm') { |
2012 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n", |
2013 | 0 | ext->name, value); |
2014 | 0 | return -EINVAL; |
2015 | 0 | } |
2016 | 0 | *(bool *)ext_val = value == 'y' ? true : false; |
2017 | 0 | break; |
2018 | 0 | case KCFG_TRISTATE: |
2019 | 0 | if (value == 'y') |
2020 | 0 | *(enum libbpf_tristate *)ext_val = TRI_YES; |
2021 | 0 | else if (value == 'm') |
2022 | 0 | *(enum libbpf_tristate *)ext_val = TRI_MODULE; |
2023 | 0 | else /* value == 'n' */ |
2024 | 0 | *(enum libbpf_tristate *)ext_val = TRI_NO; |
2025 | 0 | break; |
2026 | 0 | case KCFG_CHAR: |
2027 | 0 | *(char *)ext_val = value; |
2028 | 0 | break; |
2029 | 0 | case KCFG_UNKNOWN: |
2030 | 0 | case KCFG_INT: |
2031 | 0 | case KCFG_CHAR_ARR: |
2032 | 0 | default: |
2033 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n", |
2034 | 0 | ext->name, value); |
2035 | 0 | return -EINVAL; |
2036 | 0 | } |
2037 | 0 | ext->is_set = true; |
2038 | 0 | return 0; |
2039 | 0 | } |
2040 | | |
2041 | | static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, |
2042 | | const char *value) |
2043 | 0 | { |
2044 | 0 | size_t len; |
2045 | |
|
2046 | 0 | if (ext->kcfg.type != KCFG_CHAR_ARR) { |
2047 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n", |
2048 | 0 | ext->name, value); |
2049 | 0 | return -EINVAL; |
2050 | 0 | } |
2051 | | |
2052 | 0 | len = strlen(value); |
2053 | 0 | if (value[len - 1] != '"') { |
2054 | 0 | pr_warn("extern (kcfg) '%s': invalid string config '%s'\n", |
2055 | 0 | ext->name, value); |
2056 | 0 | return -EINVAL; |
2057 | 0 | } |
2058 | | |
2059 | | /* strip quotes */ |
2060 | 0 | len -= 2; |
2061 | 0 | if (len >= ext->kcfg.sz) { |
2062 | 0 | pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n", |
2063 | 0 | ext->name, value, len, ext->kcfg.sz - 1); |
2064 | 0 | len = ext->kcfg.sz - 1; |
2065 | 0 | } |
2066 | 0 | memcpy(ext_val, value + 1, len); |
2067 | 0 | ext_val[len] = '\0'; |
2068 | 0 | ext->is_set = true; |
2069 | 0 | return 0; |
2070 | 0 | } |
2071 | | |
2072 | | static int parse_u64(const char *value, __u64 *res) |
2073 | 0 | { |
2074 | 0 | char *value_end; |
2075 | 0 | int err; |
2076 | |
|
2077 | 0 | errno = 0; |
2078 | 0 | *res = strtoull(value, &value_end, 0); |
2079 | 0 | if (errno) { |
2080 | 0 | err = -errno; |
2081 | 0 | pr_warn("failed to parse '%s' as integer: %d\n", value, err); |
2082 | 0 | return err; |
2083 | 0 | } |
2084 | 0 | if (*value_end) { |
2085 | 0 | pr_warn("failed to parse '%s' as integer completely\n", value); |
2086 | 0 | return -EINVAL; |
2087 | 0 | } |
2088 | 0 | return 0; |
2089 | 0 | } |
2090 | | |
2091 | | static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) |
2092 | 0 | { |
2093 | 0 | int bit_sz = ext->kcfg.sz * 8; |
2094 | |
|
2095 | 0 | if (ext->kcfg.sz == 8) |
2096 | 0 | return true; |
2097 | | |
2098 | | /* Validate that value stored in u64 fits in integer of `ext->sz` |
2099 | | * bytes size without any loss of information. If the target integer |
2100 | | * is signed, we rely on the following limits of integer type of |
2101 | | * Y bits and subsequent transformation: |
2102 | | * |
2103 | | * -2^(Y-1) <= X <= 2^(Y-1) - 1 |
2104 | | * 0 <= X + 2^(Y-1) <= 2^Y - 1 |
2105 | | * 0 <= X + 2^(Y-1) < 2^Y |
2106 | | * |
2107 | | * For unsigned target integer, check that all the (64 - Y) bits are |
2108 | | * zero. |
2109 | | */ |
2110 | 0 | if (ext->kcfg.is_signed) |
2111 | 0 | return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); |
2112 | 0 | else |
2113 | 0 | return (v >> bit_sz) == 0; |
2114 | 0 | } |
2115 | | |
2116 | | static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, |
2117 | | __u64 value) |
2118 | 0 | { |
2119 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && |
2120 | 0 | ext->kcfg.type != KCFG_BOOL) { |
2121 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n", |
2122 | 0 | ext->name, (unsigned long long)value); |
2123 | 0 | return -EINVAL; |
2124 | 0 | } |
2125 | 0 | if (ext->kcfg.type == KCFG_BOOL && value > 1) { |
2126 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n", |
2127 | 0 | ext->name, (unsigned long long)value); |
2128 | 0 | return -EINVAL; |
2129 | |
|
2130 | 0 | } |
2131 | 0 | if (!is_kcfg_value_in_range(ext, value)) { |
2132 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n", |
2133 | 0 | ext->name, (unsigned long long)value, ext->kcfg.sz); |
2134 | 0 | return -ERANGE; |
2135 | 0 | } |
2136 | 0 | switch (ext->kcfg.sz) { |
2137 | 0 | case 1: |
2138 | 0 | *(__u8 *)ext_val = value; |
2139 | 0 | break; |
2140 | 0 | case 2: |
2141 | 0 | *(__u16 *)ext_val = value; |
2142 | 0 | break; |
2143 | 0 | case 4: |
2144 | 0 | *(__u32 *)ext_val = value; |
2145 | 0 | break; |
2146 | 0 | case 8: |
2147 | 0 | *(__u64 *)ext_val = value; |
2148 | 0 | break; |
2149 | 0 | default: |
2150 | 0 | return -EINVAL; |
2151 | 0 | } |
2152 | 0 | ext->is_set = true; |
2153 | 0 | return 0; |
2154 | 0 | } |
2155 | | |
2156 | | static int bpf_object__process_kconfig_line(struct bpf_object *obj, |
2157 | | char *buf, void *data) |
2158 | 0 | { |
2159 | 0 | struct extern_desc *ext; |
2160 | 0 | char *sep, *value; |
2161 | 0 | int len, err = 0; |
2162 | 0 | void *ext_val; |
2163 | 0 | __u64 num; |
2164 | |
|
2165 | 0 | if (!str_has_pfx(buf, "CONFIG_")) |
2166 | 0 | return 0; |
2167 | | |
2168 | 0 | sep = strchr(buf, '='); |
2169 | 0 | if (!sep) { |
2170 | 0 | pr_warn("failed to parse '%s': no separator\n", buf); |
2171 | 0 | return -EINVAL; |
2172 | 0 | } |
2173 | | |
2174 | | /* Trim ending '\n' */ |
2175 | 0 | len = strlen(buf); |
2176 | 0 | if (buf[len - 1] == '\n') |
2177 | 0 | buf[len - 1] = '\0'; |
2178 | | /* Split on '=' and ensure that a value is present. */ |
2179 | 0 | *sep = '\0'; |
2180 | 0 | if (!sep[1]) { |
2181 | 0 | *sep = '='; |
2182 | 0 | pr_warn("failed to parse '%s': no value\n", buf); |
2183 | 0 | return -EINVAL; |
2184 | 0 | } |
2185 | | |
2186 | 0 | ext = find_extern_by_name(obj, buf); |
2187 | 0 | if (!ext || ext->is_set) |
2188 | 0 | return 0; |
2189 | | |
2190 | 0 | ext_val = data + ext->kcfg.data_off; |
2191 | 0 | value = sep + 1; |
2192 | |
|
2193 | 0 | switch (*value) { |
2194 | 0 | case 'y': case 'n': case 'm': |
2195 | 0 | err = set_kcfg_value_tri(ext, ext_val, *value); |
2196 | 0 | break; |
2197 | 0 | case '"': |
2198 | 0 | err = set_kcfg_value_str(ext, ext_val, value); |
2199 | 0 | break; |
2200 | 0 | default: |
2201 | | /* assume integer */ |
2202 | 0 | err = parse_u64(value, &num); |
2203 | 0 | if (err) { |
2204 | 0 | pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); |
2205 | 0 | return err; |
2206 | 0 | } |
2207 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { |
2208 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); |
2209 | 0 | return -EINVAL; |
2210 | 0 | } |
2211 | 0 | err = set_kcfg_value_num(ext, ext_val, num); |
2212 | 0 | break; |
2213 | 0 | } |
2214 | 0 | if (err) |
2215 | 0 | return err; |
2216 | 0 | pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); |
2217 | 0 | return 0; |
2218 | 0 | } |
2219 | | |
2220 | | static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) |
2221 | 0 | { |
2222 | 0 | char buf[PATH_MAX]; |
2223 | 0 | struct utsname uts; |
2224 | 0 | int len, err = 0; |
2225 | 0 | gzFile file; |
2226 | |
|
2227 | 0 | uname(&uts); |
2228 | 0 | len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); |
2229 | 0 | if (len < 0) |
2230 | 0 | return -EINVAL; |
2231 | 0 | else if (len >= PATH_MAX) |
2232 | 0 | return -ENAMETOOLONG; |
2233 | | |
2234 | | /* gzopen also accepts uncompressed files. */ |
2235 | 0 | file = gzopen(buf, "re"); |
2236 | 0 | if (!file) |
2237 | 0 | file = gzopen("/proc/config.gz", "re"); |
2238 | |
|
2239 | 0 | if (!file) { |
2240 | 0 | pr_warn("failed to open system Kconfig\n"); |
2241 | 0 | return -ENOENT; |
2242 | 0 | } |
2243 | | |
2244 | 0 | while (gzgets(file, buf, sizeof(buf))) { |
2245 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2246 | 0 | if (err) { |
2247 | 0 | pr_warn("error parsing system Kconfig line '%s': %d\n", |
2248 | 0 | buf, err); |
2249 | 0 | goto out; |
2250 | 0 | } |
2251 | 0 | } |
2252 | | |
2253 | 0 | out: |
2254 | 0 | gzclose(file); |
2255 | 0 | return err; |
2256 | 0 | } |
2257 | | |
2258 | | static int bpf_object__read_kconfig_mem(struct bpf_object *obj, |
2259 | | const char *config, void *data) |
2260 | 0 | { |
2261 | 0 | char buf[PATH_MAX]; |
2262 | 0 | int err = 0; |
2263 | 0 | FILE *file; |
2264 | |
|
2265 | 0 | file = fmemopen((void *)config, strlen(config), "r"); |
2266 | 0 | if (!file) { |
2267 | 0 | err = -errno; |
2268 | 0 | pr_warn("failed to open in-memory Kconfig: %d\n", err); |
2269 | 0 | return err; |
2270 | 0 | } |
2271 | | |
2272 | 0 | while (fgets(buf, sizeof(buf), file)) { |
2273 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2274 | 0 | if (err) { |
2275 | 0 | pr_warn("error parsing in-memory Kconfig line '%s': %d\n", |
2276 | 0 | buf, err); |
2277 | 0 | break; |
2278 | 0 | } |
2279 | 0 | } |
2280 | |
|
2281 | 0 | fclose(file); |
2282 | 0 | return err; |
2283 | 0 | } |
2284 | | |
2285 | | static int bpf_object__init_kconfig_map(struct bpf_object *obj) |
2286 | 2.12k | { |
2287 | 2.12k | struct extern_desc *last_ext = NULL, *ext; |
2288 | 2.12k | size_t map_sz; |
2289 | 2.12k | int i, err; |
2290 | | |
2291 | 3.52k | for (i = 0; i < obj->nr_extern; i++) { |
2292 | 1.39k | ext = &obj->externs[i]; |
2293 | 1.39k | if (ext->type == EXT_KCFG) |
2294 | 133 | last_ext = ext; |
2295 | 1.39k | } |
2296 | | |
2297 | 2.12k | if (!last_ext) |
2298 | 2.04k | return 0; |
2299 | | |
2300 | 79 | map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; |
2301 | 79 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG, |
2302 | 79 | ".kconfig", obj->efile.symbols_shndx, |
2303 | 79 | NULL, map_sz); |
2304 | 79 | if (err) |
2305 | 65 | return err; |
2306 | | |
2307 | 14 | obj->kconfig_map_idx = obj->nr_maps - 1; |
2308 | | |
2309 | 14 | return 0; |
2310 | 79 | } |
2311 | | |
2312 | | const struct btf_type * |
2313 | | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) |
2314 | 5.29k | { |
2315 | 5.29k | const struct btf_type *t = btf__type_by_id(btf, id); |
2316 | | |
2317 | 5.29k | if (res_id) |
2318 | 2.48k | *res_id = id; |
2319 | | |
2320 | 7.31k | while (btf_is_mod(t) || btf_is_typedef(t)) { |
2321 | 2.02k | if (res_id) |
2322 | 1.35k | *res_id = t->type; |
2323 | 2.02k | t = btf__type_by_id(btf, t->type); |
2324 | 2.02k | } |
2325 | | |
2326 | 5.29k | return t; |
2327 | 5.29k | } |
2328 | | |
2329 | | static const struct btf_type * |
2330 | | resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) |
2331 | 0 | { |
2332 | 0 | const struct btf_type *t; |
2333 | |
|
2334 | 0 | t = skip_mods_and_typedefs(btf, id, NULL); |
2335 | 0 | if (!btf_is_ptr(t)) |
2336 | 0 | return NULL; |
2337 | | |
2338 | 0 | t = skip_mods_and_typedefs(btf, t->type, res_id); |
2339 | |
|
2340 | 0 | return btf_is_func_proto(t) ? t : NULL; |
2341 | 0 | } |
2342 | | |
2343 | | static const char *__btf_kind_str(__u16 kind) |
2344 | 397 | { |
2345 | 397 | switch (kind) { |
2346 | 48 | case BTF_KIND_UNKN: return "void"; |
2347 | 14 | case BTF_KIND_INT: return "int"; |
2348 | 3 | case BTF_KIND_PTR: return "ptr"; |
2349 | 10 | case BTF_KIND_ARRAY: return "array"; |
2350 | 7 | case BTF_KIND_STRUCT: return "struct"; |
2351 | 4 | case BTF_KIND_UNION: return "union"; |
2352 | 2 | case BTF_KIND_ENUM: return "enum"; |
2353 | 5 | case BTF_KIND_FWD: return "fwd"; |
2354 | 6 | case BTF_KIND_TYPEDEF: return "typedef"; |
2355 | 2 | case BTF_KIND_VOLATILE: return "volatile"; |
2356 | 4 | case BTF_KIND_CONST: return "const"; |
2357 | 3 | case BTF_KIND_RESTRICT: return "restrict"; |
2358 | 156 | case BTF_KIND_FUNC: return "func"; |
2359 | 13 | case BTF_KIND_FUNC_PROTO: return "func_proto"; |
2360 | 51 | case BTF_KIND_VAR: return "var"; |
2361 | 52 | case BTF_KIND_DATASEC: return "datasec"; |
2362 | 5 | case BTF_KIND_FLOAT: return "float"; |
2363 | 3 | case BTF_KIND_DECL_TAG: return "decl_tag"; |
2364 | 2 | case BTF_KIND_TYPE_TAG: return "type_tag"; |
2365 | 7 | case BTF_KIND_ENUM64: return "enum64"; |
2366 | 0 | default: return "unknown"; |
2367 | 397 | } |
2368 | 397 | } |
2369 | | |
2370 | | const char *btf_kind_str(const struct btf_type *t) |
2371 | 397 | { |
2372 | 397 | return __btf_kind_str(btf_kind(t)); |
2373 | 397 | } |
2374 | | |
2375 | | /* |
2376 | | * Fetch integer attribute of BTF map definition. Such attributes are |
2377 | | * represented using a pointer to an array, in which dimensionality of array |
2378 | | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
2379 | | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
2380 | | * type definition, while using only sizeof(void *) space in ELF data section. |
2381 | | */ |
2382 | | static bool get_map_field_int(const char *map_name, const struct btf *btf, |
2383 | | const struct btf_member *m, __u32 *res) |
2384 | 718 | { |
2385 | 718 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2386 | 718 | const char *name = btf__name_by_offset(btf, m->name_off); |
2387 | 718 | const struct btf_array *arr_info; |
2388 | 718 | const struct btf_type *arr_t; |
2389 | | |
2390 | 718 | if (!btf_is_ptr(t)) { |
2391 | 21 | pr_warn("map '%s': attr '%s': expected PTR, got %s.\n", |
2392 | 21 | map_name, name, btf_kind_str(t)); |
2393 | 21 | return false; |
2394 | 21 | } |
2395 | | |
2396 | 697 | arr_t = btf__type_by_id(btf, t->type); |
2397 | 697 | if (!arr_t) { |
2398 | 0 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
2399 | 0 | map_name, name, t->type); |
2400 | 0 | return false; |
2401 | 0 | } |
2402 | 697 | if (!btf_is_array(arr_t)) { |
2403 | 12 | pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n", |
2404 | 12 | map_name, name, btf_kind_str(arr_t)); |
2405 | 12 | return false; |
2406 | 12 | } |
2407 | 685 | arr_info = btf_array(arr_t); |
2408 | 685 | *res = arr_info->nelems; |
2409 | 685 | return true; |
2410 | 697 | } |
2411 | | |
2412 | | static bool get_map_field_long(const char *map_name, const struct btf *btf, |
2413 | | const struct btf_member *m, __u64 *res) |
2414 | 38 | { |
2415 | 38 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2416 | 38 | const char *name = btf__name_by_offset(btf, m->name_off); |
2417 | | |
2418 | 38 | if (btf_is_ptr(t)) { |
2419 | 22 | __u32 res32; |
2420 | 22 | bool ret; |
2421 | | |
2422 | 22 | ret = get_map_field_int(map_name, btf, m, &res32); |
2423 | 22 | if (ret) |
2424 | 21 | *res = (__u64)res32; |
2425 | 22 | return ret; |
2426 | 22 | } |
2427 | | |
2428 | 16 | if (!btf_is_enum(t) && !btf_is_enum64(t)) { |
2429 | 9 | pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n", |
2430 | 9 | map_name, name, btf_kind_str(t)); |
2431 | 9 | return false; |
2432 | 9 | } |
2433 | | |
2434 | 7 | if (btf_vlen(t) != 1) { |
2435 | 1 | pr_warn("map '%s': attr '%s': invalid __ulong\n", |
2436 | 1 | map_name, name); |
2437 | 1 | return false; |
2438 | 1 | } |
2439 | | |
2440 | 6 | if (btf_is_enum(t)) { |
2441 | 3 | const struct btf_enum *e = btf_enum(t); |
2442 | | |
2443 | 3 | *res = e->val; |
2444 | 3 | } else { |
2445 | 3 | const struct btf_enum64 *e = btf_enum64(t); |
2446 | | |
2447 | 3 | *res = btf_enum64_value(e); |
2448 | 3 | } |
2449 | 6 | return true; |
2450 | 7 | } |
2451 | | |
2452 | | static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) |
2453 | 1 | { |
2454 | 1 | int len; |
2455 | | |
2456 | 1 | len = snprintf(buf, buf_sz, "%s/%s", path, name); |
2457 | 1 | if (len < 0) |
2458 | 0 | return -EINVAL; |
2459 | 1 | if (len >= buf_sz) |
2460 | 0 | return -ENAMETOOLONG; |
2461 | | |
2462 | 1 | return 0; |
2463 | 1 | } |
2464 | | |
2465 | | static int build_map_pin_path(struct bpf_map *map, const char *path) |
2466 | 1 | { |
2467 | 1 | char buf[PATH_MAX]; |
2468 | 1 | int err; |
2469 | | |
2470 | 1 | if (!path) |
2471 | 1 | path = BPF_FS_DEFAULT_PATH; |
2472 | | |
2473 | 1 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
2474 | 1 | if (err) |
2475 | 0 | return err; |
2476 | | |
2477 | 1 | return bpf_map__set_pin_path(map, buf); |
2478 | 1 | } |
2479 | | |
2480 | | /* should match definition in bpf_helpers.h */ |
2481 | | enum libbpf_pin_type { |
2482 | | LIBBPF_PIN_NONE, |
2483 | | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ |
2484 | | LIBBPF_PIN_BY_NAME, |
2485 | | }; |
2486 | | |
2487 | | int parse_btf_map_def(const char *map_name, struct btf *btf, |
2488 | | const struct btf_type *def_t, bool strict, |
2489 | | struct btf_map_def *map_def, struct btf_map_def *inner_def) |
2490 | 949 | { |
2491 | 949 | const struct btf_type *t; |
2492 | 949 | const struct btf_member *m; |
2493 | 949 | bool is_inner = inner_def == NULL; |
2494 | 949 | int vlen, i; |
2495 | | |
2496 | 949 | vlen = btf_vlen(def_t); |
2497 | 949 | m = btf_members(def_t); |
2498 | 2.01k | for (i = 0; i < vlen; i++, m++) { |
2499 | 1.90k | const char *name = btf__name_by_offset(btf, m->name_off); |
2500 | | |
2501 | 1.90k | if (!name) { |
2502 | 0 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
2503 | 0 | return -EINVAL; |
2504 | 0 | } |
2505 | 1.90k | if (strcmp(name, "type") == 0) { |
2506 | 259 | if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) |
2507 | 3 | return -EINVAL; |
2508 | 256 | map_def->parts |= MAP_DEF_MAP_TYPE; |
2509 | 1.64k | } else if (strcmp(name, "max_entries") == 0) { |
2510 | 26 | if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) |
2511 | 1 | return -EINVAL; |
2512 | 25 | map_def->parts |= MAP_DEF_MAX_ENTRIES; |
2513 | 1.62k | } else if (strcmp(name, "map_flags") == 0) { |
2514 | 21 | if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) |
2515 | 4 | return -EINVAL; |
2516 | 17 | map_def->parts |= MAP_DEF_MAP_FLAGS; |
2517 | 1.59k | } else if (strcmp(name, "numa_node") == 0) { |
2518 | 18 | if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) |
2519 | 4 | return -EINVAL; |
2520 | 14 | map_def->parts |= MAP_DEF_NUMA_NODE; |
2521 | 1.58k | } else if (strcmp(name, "key_size") == 0) { |
2522 | 192 | __u32 sz; |
2523 | | |
2524 | 192 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2525 | 16 | return -EINVAL; |
2526 | 176 | if (map_def->key_size && map_def->key_size != sz) { |
2527 | 40 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
2528 | 40 | map_name, map_def->key_size, sz); |
2529 | 40 | return -EINVAL; |
2530 | 40 | } |
2531 | 136 | map_def->key_size = sz; |
2532 | 136 | map_def->parts |= MAP_DEF_KEY_SIZE; |
2533 | 1.38k | } else if (strcmp(name, "key") == 0) { |
2534 | 199 | __s64 sz; |
2535 | | |
2536 | 199 | t = btf__type_by_id(btf, m->type); |
2537 | 199 | if (!t) { |
2538 | 0 | pr_warn("map '%s': key type [%d] not found.\n", |
2539 | 0 | map_name, m->type); |
2540 | 0 | return -EINVAL; |
2541 | 0 | } |
2542 | 199 | if (!btf_is_ptr(t)) { |
2543 | 4 | pr_warn("map '%s': key spec is not PTR: %s.\n", |
2544 | 4 | map_name, btf_kind_str(t)); |
2545 | 4 | return -EINVAL; |
2546 | 4 | } |
2547 | 195 | sz = btf__resolve_size(btf, t->type); |
2548 | 195 | if (sz < 0) { |
2549 | 11 | pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n", |
2550 | 11 | map_name, t->type, (ssize_t)sz); |
2551 | 11 | return sz; |
2552 | 11 | } |
2553 | 184 | if (map_def->key_size && map_def->key_size != sz) { |
2554 | 5 | pr_warn("map '%s': conflicting key size %u != %zd.\n", |
2555 | 5 | map_name, map_def->key_size, (ssize_t)sz); |
2556 | 5 | return -EINVAL; |
2557 | 5 | } |
2558 | 179 | map_def->key_size = sz; |
2559 | 179 | map_def->key_type_id = t->type; |
2560 | 179 | map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; |
2561 | 1.19k | } else if (strcmp(name, "value_size") == 0) { |
2562 | 118 | __u32 sz; |
2563 | | |
2564 | 118 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2565 | 2 | return -EINVAL; |
2566 | 116 | if (map_def->value_size && map_def->value_size != sz) { |
2567 | 51 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
2568 | 51 | map_name, map_def->value_size, sz); |
2569 | 51 | return -EINVAL; |
2570 | 51 | } |
2571 | 65 | map_def->value_size = sz; |
2572 | 65 | map_def->parts |= MAP_DEF_VALUE_SIZE; |
2573 | 1.07k | } else if (strcmp(name, "value") == 0) { |
2574 | 348 | __s64 sz; |
2575 | | |
2576 | 348 | t = btf__type_by_id(btf, m->type); |
2577 | 348 | if (!t) { |
2578 | 0 | pr_warn("map '%s': value type [%d] not found.\n", |
2579 | 0 | map_name, m->type); |
2580 | 0 | return -EINVAL; |
2581 | 0 | } |
2582 | 348 | if (!btf_is_ptr(t)) { |
2583 | 7 | pr_warn("map '%s': value spec is not PTR: %s.\n", |
2584 | 7 | map_name, btf_kind_str(t)); |
2585 | 7 | return -EINVAL; |
2586 | 7 | } |
2587 | 341 | sz = btf__resolve_size(btf, t->type); |
2588 | 341 | if (sz < 0) { |
2589 | 9 | pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n", |
2590 | 9 | map_name, t->type, (ssize_t)sz); |
2591 | 9 | return sz; |
2592 | 9 | } |
2593 | 332 | if (map_def->value_size && map_def->value_size != sz) { |
2594 | 2 | pr_warn("map '%s': conflicting value size %u != %zd.\n", |
2595 | 2 | map_name, map_def->value_size, (ssize_t)sz); |
2596 | 2 | return -EINVAL; |
2597 | 2 | } |
2598 | 330 | map_def->value_size = sz; |
2599 | 330 | map_def->value_type_id = t->type; |
2600 | 330 | map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; |
2601 | 330 | } |
2602 | 724 | else if (strcmp(name, "values") == 0) { |
2603 | 133 | bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); |
2604 | 133 | bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; |
2605 | 133 | const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; |
2606 | 133 | char inner_map_name[128]; |
2607 | 133 | int err; |
2608 | | |
2609 | 133 | if (is_inner) { |
2610 | 2 | pr_warn("map '%s': multi-level inner maps not supported.\n", |
2611 | 2 | map_name); |
2612 | 2 | return -ENOTSUP; |
2613 | 2 | } |
2614 | 131 | if (i != vlen - 1) { |
2615 | 41 | pr_warn("map '%s': '%s' member should be last.\n", |
2616 | 41 | map_name, name); |
2617 | 41 | return -EINVAL; |
2618 | 41 | } |
2619 | 90 | if (!is_map_in_map && !is_prog_array) { |
2620 | 23 | pr_warn("map '%s': should be map-in-map or prog-array.\n", |
2621 | 23 | map_name); |
2622 | 23 | return -ENOTSUP; |
2623 | 23 | } |
2624 | 67 | if (map_def->value_size && map_def->value_size != 4) { |
2625 | 42 | pr_warn("map '%s': conflicting value size %u != 4.\n", |
2626 | 42 | map_name, map_def->value_size); |
2627 | 42 | return -EINVAL; |
2628 | 42 | } |
2629 | 25 | map_def->value_size = 4; |
2630 | 25 | t = btf__type_by_id(btf, m->type); |
2631 | 25 | if (!t) { |
2632 | 0 | pr_warn("map '%s': %s type [%d] not found.\n", |
2633 | 0 | map_name, desc, m->type); |
2634 | 0 | return -EINVAL; |
2635 | 0 | } |
2636 | 25 | if (!btf_is_array(t) || btf_array(t)->nelems) { |
2637 | 16 | pr_warn("map '%s': %s spec is not a zero-sized array.\n", |
2638 | 16 | map_name, desc); |
2639 | 16 | return -EINVAL; |
2640 | 16 | } |
2641 | 9 | t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); |
2642 | 9 | if (!btf_is_ptr(t)) { |
2643 | 2 | pr_warn("map '%s': %s def is of unexpected kind %s.\n", |
2644 | 2 | map_name, desc, btf_kind_str(t)); |
2645 | 2 | return -EINVAL; |
2646 | 2 | } |
2647 | 7 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
2648 | 7 | if (is_prog_array) { |
2649 | 1 | if (!btf_is_func_proto(t)) { |
2650 | 1 | pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", |
2651 | 1 | map_name, btf_kind_str(t)); |
2652 | 1 | return -EINVAL; |
2653 | 1 | } |
2654 | 0 | continue; |
2655 | 1 | } |
2656 | 6 | if (!btf_is_struct(t)) { |
2657 | 4 | pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", |
2658 | 4 | map_name, btf_kind_str(t)); |
2659 | 4 | return -EINVAL; |
2660 | 4 | } |
2661 | | |
2662 | 2 | snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name); |
2663 | 2 | err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL); |
2664 | 2 | if (err) |
2665 | 2 | return err; |
2666 | | |
2667 | 0 | map_def->parts |= MAP_DEF_INNER_MAP; |
2668 | 591 | } else if (strcmp(name, "pinning") == 0) { |
2669 | 62 | __u32 val; |
2670 | | |
2671 | 62 | if (is_inner) { |
2672 | 0 | pr_warn("map '%s': inner def can't be pinned.\n", map_name); |
2673 | 0 | return -EINVAL; |
2674 | 0 | } |
2675 | 62 | if (!get_map_field_int(map_name, btf, m, &val)) |
2676 | 2 | return -EINVAL; |
2677 | 60 | if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) { |
2678 | 48 | pr_warn("map '%s': invalid pinning value %u.\n", |
2679 | 48 | map_name, val); |
2680 | 48 | return -EINVAL; |
2681 | 48 | } |
2682 | 12 | map_def->pinning = val; |
2683 | 12 | map_def->parts |= MAP_DEF_PINNING; |
2684 | 529 | } else if (strcmp(name, "map_extra") == 0) { |
2685 | 38 | __u64 map_extra; |
2686 | | |
2687 | 38 | if (!get_map_field_long(map_name, btf, m, &map_extra)) |
2688 | 11 | return -EINVAL; |
2689 | 27 | map_def->map_extra = map_extra; |
2690 | 27 | map_def->parts |= MAP_DEF_MAP_EXTRA; |
2691 | 491 | } else { |
2692 | 491 | if (strict) { |
2693 | 491 | pr_warn("map '%s': unknown field '%s'.\n", map_name, name); |
2694 | 491 | return -ENOTSUP; |
2695 | 491 | } |
2696 | 0 | pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name); |
2697 | 0 | } |
2698 | 1.90k | } |
2699 | | |
2700 | 105 | if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { |
2701 | 34 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
2702 | 34 | return -EINVAL; |
2703 | 34 | } |
2704 | | |
2705 | 71 | return 0; |
2706 | 105 | } |
2707 | | |
2708 | | static size_t adjust_ringbuf_sz(size_t sz) |
2709 | 3 | { |
2710 | 3 | __u32 page_sz = sysconf(_SC_PAGE_SIZE); |
2711 | 3 | __u32 mul; |
2712 | | |
2713 | | /* if user forgot to set any size, make sure they see error */ |
2714 | 3 | if (sz == 0) |
2715 | 1 | return 0; |
2716 | | /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be |
2717 | | * a power-of-2 multiple of kernel's page size. If user diligently |
2718 | | * satisified these conditions, pass the size through. |
2719 | | */ |
2720 | 2 | if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz)) |
2721 | 0 | return sz; |
2722 | | |
2723 | | /* Otherwise find closest (page_sz * power_of_2) product bigger than |
2724 | | * user-set size to satisfy both user size request and kernel |
2725 | | * requirements and substitute correct max_entries for map creation. |
2726 | | */ |
2727 | 2 | for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) { |
2728 | 2 | if (mul * page_sz > sz) |
2729 | 2 | return mul * page_sz; |
2730 | 2 | } |
2731 | | |
2732 | | /* if it's impossible to satisfy the conditions (i.e., user size is |
2733 | | * very close to UINT_MAX but is not a power-of-2 multiple of |
2734 | | * page_size) then just return original size and let kernel reject it |
2735 | | */ |
2736 | 0 | return sz; |
2737 | 2 | } |
2738 | | |
2739 | | static bool map_is_ringbuf(const struct bpf_map *map) |
2740 | 71 | { |
2741 | 71 | return map->def.type == BPF_MAP_TYPE_RINGBUF || |
2742 | 71 | map->def.type == BPF_MAP_TYPE_USER_RINGBUF; |
2743 | 71 | } |
2744 | | |
2745 | | static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def) |
2746 | 71 | { |
2747 | 71 | map->def.type = def->map_type; |
2748 | 71 | map->def.key_size = def->key_size; |
2749 | 71 | map->def.value_size = def->value_size; |
2750 | 71 | map->def.max_entries = def->max_entries; |
2751 | 71 | map->def.map_flags = def->map_flags; |
2752 | 71 | map->map_extra = def->map_extra; |
2753 | | |
2754 | 71 | map->numa_node = def->numa_node; |
2755 | 71 | map->btf_key_type_id = def->key_type_id; |
2756 | 71 | map->btf_value_type_id = def->value_type_id; |
2757 | | |
2758 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
2759 | 71 | if (map_is_ringbuf(map)) |
2760 | 3 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
2761 | | |
2762 | 71 | if (def->parts & MAP_DEF_MAP_TYPE) |
2763 | 71 | pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); |
2764 | | |
2765 | 71 | if (def->parts & MAP_DEF_KEY_TYPE) |
2766 | 71 | pr_debug("map '%s': found key [%u], sz = %u.\n", |
2767 | 64 | map->name, def->key_type_id, def->key_size); |
2768 | 64 | else if (def->parts & MAP_DEF_KEY_SIZE) |
2769 | 25 | pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); |
2770 | | |
2771 | 71 | if (def->parts & MAP_DEF_VALUE_TYPE) |
2772 | 71 | pr_debug("map '%s': found value [%u], sz = %u.\n", |
2773 | 63 | map->name, def->value_type_id, def->value_size); |
2774 | 63 | else if (def->parts & MAP_DEF_VALUE_SIZE) |
2775 | 7 | pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); |
2776 | | |
2777 | 71 | if (def->parts & MAP_DEF_MAX_ENTRIES) |
2778 | 71 | pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); |
2779 | 71 | if (def->parts & MAP_DEF_MAP_FLAGS) |
2780 | 71 | pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); |
2781 | 71 | if (def->parts & MAP_DEF_MAP_EXTRA) |
2782 | 71 | pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, |
2783 | 71 | (unsigned long long)def->map_extra); |
2784 | 71 | if (def->parts & MAP_DEF_PINNING) |
2785 | 71 | pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); |
2786 | 71 | if (def->parts & MAP_DEF_NUMA_NODE) |
2787 | 71 | pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); |
2788 | | |
2789 | 71 | if (def->parts & MAP_DEF_INNER_MAP) |
2790 | 71 | pr_debug("map '%s': found inner map definition.\n", map->name); |
2791 | 71 | } |
2792 | | |
2793 | | static const char *btf_var_linkage_str(__u32 linkage) |
2794 | 66 | { |
2795 | 66 | switch (linkage) { |
2796 | 4 | case BTF_VAR_STATIC: return "static"; |
2797 | 0 | case BTF_VAR_GLOBAL_ALLOCATED: return "global"; |
2798 | 1 | case BTF_VAR_GLOBAL_EXTERN: return "extern"; |
2799 | 61 | default: return "unknown"; |
2800 | 66 | } |
2801 | 66 | } |
2802 | | |
2803 | | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
2804 | | const struct btf_type *sec, |
2805 | | int var_idx, int sec_idx, |
2806 | | const Elf_Data *data, bool strict, |
2807 | | const char *pin_root_path) |
2808 | 1.06k | { |
2809 | 1.06k | struct btf_map_def map_def = {}, inner_def = {}; |
2810 | 1.06k | const struct btf_type *var, *def; |
2811 | 1.06k | const struct btf_var_secinfo *vi; |
2812 | 1.06k | const struct btf_var *var_extra; |
2813 | 1.06k | const char *map_name; |
2814 | 1.06k | struct bpf_map *map; |
2815 | 1.06k | int err; |
2816 | | |
2817 | 1.06k | vi = btf_var_secinfos(sec) + var_idx; |
2818 | 1.06k | var = btf__type_by_id(obj->btf, vi->type); |
2819 | 1.06k | var_extra = btf_var(var); |
2820 | 1.06k | map_name = btf__name_by_offset(obj->btf, var->name_off); |
2821 | | |
2822 | 1.06k | if (map_name == NULL || map_name[0] == '\0') { |
2823 | 1 | pr_warn("map #%d: empty name.\n", var_idx); |
2824 | 1 | return -EINVAL; |
2825 | 1 | } |
2826 | 1.06k | if ((__u64)vi->offset + vi->size > data->d_size) { |
2827 | 1 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
2828 | 1 | return -EINVAL; |
2829 | 1 | } |
2830 | 1.06k | if (!btf_is_var(var)) { |
2831 | 0 | pr_warn("map '%s': unexpected var kind %s.\n", |
2832 | 0 | map_name, btf_kind_str(var)); |
2833 | 0 | return -EINVAL; |
2834 | 0 | } |
2835 | 1.06k | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { |
2836 | 66 | pr_warn("map '%s': unsupported map linkage %s.\n", |
2837 | 66 | map_name, btf_var_linkage_str(var_extra->linkage)); |
2838 | 66 | return -EOPNOTSUPP; |
2839 | 66 | } |
2840 | | |
2841 | 1.00k | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
2842 | 1.00k | if (!btf_is_struct(def)) { |
2843 | 14 | pr_warn("map '%s': unexpected def kind %s.\n", |
2844 | 14 | map_name, btf_kind_str(var)); |
2845 | 14 | return -EINVAL; |
2846 | 14 | } |
2847 | 987 | if (def->size > vi->size) { |
2848 | 40 | pr_warn("map '%s': invalid def size.\n", map_name); |
2849 | 40 | return -EINVAL; |
2850 | 40 | } |
2851 | | |
2852 | 947 | map = bpf_object__add_map(obj); |
2853 | 947 | if (IS_ERR(map)) |
2854 | 0 | return PTR_ERR(map); |
2855 | 947 | map->name = strdup(map_name); |
2856 | 947 | if (!map->name) { |
2857 | 0 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
2858 | 0 | return -ENOMEM; |
2859 | 0 | } |
2860 | 947 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
2861 | 947 | map->def.type = BPF_MAP_TYPE_UNSPEC; |
2862 | 947 | map->sec_idx = sec_idx; |
2863 | 947 | map->sec_offset = vi->offset; |
2864 | 947 | map->btf_var_idx = var_idx; |
2865 | 947 | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
2866 | 947 | map_name, map->sec_idx, map->sec_offset); |
2867 | | |
2868 | 947 | err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); |
2869 | 947 | if (err) |
2870 | 876 | return err; |
2871 | | |
2872 | 71 | fill_map_from_def(map, &map_def); |
2873 | | |
2874 | 71 | if (map_def.pinning == LIBBPF_PIN_BY_NAME) { |
2875 | 1 | err = build_map_pin_path(map, pin_root_path); |
2876 | 1 | if (err) { |
2877 | 0 | pr_warn("map '%s': couldn't build pin path.\n", map->name); |
2878 | 0 | return err; |
2879 | 0 | } |
2880 | 1 | } |
2881 | | |
2882 | 71 | if (map_def.parts & MAP_DEF_INNER_MAP) { |
2883 | 0 | map->inner_map = calloc(1, sizeof(*map->inner_map)); |
2884 | 0 | if (!map->inner_map) |
2885 | 0 | return -ENOMEM; |
2886 | 0 | map->inner_map->fd = create_placeholder_fd(); |
2887 | 0 | if (map->inner_map->fd < 0) |
2888 | 0 | return map->inner_map->fd; |
2889 | 0 | map->inner_map->sec_idx = sec_idx; |
2890 | 0 | map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); |
2891 | 0 | if (!map->inner_map->name) |
2892 | 0 | return -ENOMEM; |
2893 | 0 | sprintf(map->inner_map->name, "%s.inner", map_name); |
2894 | |
|
2895 | 0 | fill_map_from_def(map->inner_map, &inner_def); |
2896 | 0 | } |
2897 | | |
2898 | 71 | err = map_fill_btf_type_info(obj, map); |
2899 | 71 | if (err) |
2900 | 0 | return err; |
2901 | | |
2902 | 71 | return 0; |
2903 | 71 | } |
2904 | | |
2905 | | static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, |
2906 | | const char *sec_name, int sec_idx, |
2907 | | void *data, size_t data_sz) |
2908 | 0 | { |
2909 | 0 | const long page_sz = sysconf(_SC_PAGE_SIZE); |
2910 | 0 | size_t mmap_sz; |
2911 | |
|
2912 | 0 | mmap_sz = bpf_map_mmap_sz(obj->arena_map); |
2913 | 0 | if (roundup(data_sz, page_sz) > mmap_sz) { |
2914 | 0 | pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n", |
2915 | 0 | sec_name, mmap_sz, data_sz); |
2916 | 0 | return -E2BIG; |
2917 | 0 | } |
2918 | | |
2919 | 0 | obj->arena_data = malloc(data_sz); |
2920 | 0 | if (!obj->arena_data) |
2921 | 0 | return -ENOMEM; |
2922 | 0 | memcpy(obj->arena_data, data, data_sz); |
2923 | 0 | obj->arena_data_sz = data_sz; |
2924 | | |
2925 | | /* make bpf_map__init_value() work for ARENA maps */ |
2926 | 0 | map->mmaped = obj->arena_data; |
2927 | |
|
2928 | 0 | return 0; |
2929 | 0 | } |
2930 | | |
2931 | | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
2932 | | const char *pin_root_path) |
2933 | 3.20k | { |
2934 | 3.20k | const struct btf_type *sec = NULL; |
2935 | 3.20k | int nr_types, i, vlen, err; |
2936 | 3.20k | const struct btf_type *t; |
2937 | 3.20k | const char *name; |
2938 | 3.20k | Elf_Data *data; |
2939 | 3.20k | Elf_Scn *scn; |
2940 | | |
2941 | 3.20k | if (obj->efile.btf_maps_shndx < 0) |
2942 | 2.08k | return 0; |
2943 | | |
2944 | 1.12k | scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); |
2945 | 1.12k | data = elf_sec_data(obj, scn); |
2946 | 1.12k | if (!scn || !data) { |
2947 | 0 | pr_warn("elf: failed to get %s map definitions for %s\n", |
2948 | 0 | MAPS_ELF_SEC, obj->path); |
2949 | 0 | return -EINVAL; |
2950 | 0 | } |
2951 | | |
2952 | 1.12k | nr_types = btf__type_cnt(obj->btf); |
2953 | 13.8k | for (i = 1; i < nr_types; i++) { |
2954 | 13.8k | t = btf__type_by_id(obj->btf, i); |
2955 | 13.8k | if (!btf_is_datasec(t)) |
2956 | 12.0k | continue; |
2957 | 1.78k | name = btf__name_by_offset(obj->btf, t->name_off); |
2958 | 1.78k | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
2959 | 1.07k | sec = t; |
2960 | 1.07k | obj->efile.btf_maps_sec_btf_id = i; |
2961 | 1.07k | break; |
2962 | 1.07k | } |
2963 | 1.78k | } |
2964 | | |
2965 | 1.12k | if (!sec) { |
2966 | 48 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
2967 | 48 | return -ENOENT; |
2968 | 48 | } |
2969 | | |
2970 | 1.07k | vlen = btf_vlen(sec); |
2971 | 1.14k | for (i = 0; i < vlen; i++) { |
2972 | 1.06k | err = bpf_object__init_user_btf_map(obj, sec, i, |
2973 | 1.06k | obj->efile.btf_maps_shndx, |
2974 | 1.06k | data, strict, |
2975 | 1.06k | pin_root_path); |
2976 | 1.06k | if (err) |
2977 | 998 | return err; |
2978 | 1.06k | } |
2979 | | |
2980 | 147 | for (i = 0; i < obj->nr_maps; i++) { |
2981 | 71 | struct bpf_map *map = &obj->maps[i]; |
2982 | | |
2983 | 71 | if (map->def.type != BPF_MAP_TYPE_ARENA) |
2984 | 69 | continue; |
2985 | | |
2986 | 2 | if (obj->arena_map) { |
2987 | 0 | pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n", |
2988 | 0 | map->name, obj->arena_map->name); |
2989 | 0 | return -EINVAL; |
2990 | 0 | } |
2991 | 2 | obj->arena_map = map; |
2992 | | |
2993 | 2 | if (obj->efile.arena_data) { |
2994 | 0 | err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx, |
2995 | 0 | obj->efile.arena_data->d_buf, |
2996 | 0 | obj->efile.arena_data->d_size); |
2997 | 0 | if (err) |
2998 | 0 | return err; |
2999 | 0 | } |
3000 | 2 | } |
3001 | 76 | if (obj->efile.arena_data && !obj->arena_map) { |
3002 | 1 | pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n", |
3003 | 1 | ARENA_SEC); |
3004 | 1 | return -ENOENT; |
3005 | 1 | } |
3006 | | |
3007 | 75 | return 0; |
3008 | 76 | } |
3009 | | |
3010 | | static int bpf_object__init_maps(struct bpf_object *obj, |
3011 | | const struct bpf_object_open_opts *opts) |
3012 | 3.20k | { |
3013 | 3.20k | const char *pin_root_path; |
3014 | 3.20k | bool strict; |
3015 | 3.20k | int err = 0; |
3016 | | |
3017 | 3.20k | strict = !OPTS_GET(opts, relaxed_maps, false); |
3018 | 3.20k | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
3019 | | |
3020 | 3.20k | err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
3021 | 3.20k | err = err ?: bpf_object__init_global_data_maps(obj); |
3022 | 3.20k | err = err ?: bpf_object__init_kconfig_map(obj); |
3023 | 18.4E | err = err ?: bpf_object_init_struct_ops(obj); |
3024 | | |
3025 | 18.4E | return err; |
3026 | 1.08k | } |
3027 | | |
3028 | | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
3029 | 1.97k | { |
3030 | 1.97k | Elf64_Shdr *sh; |
3031 | | |
3032 | 1.97k | sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx)); |
3033 | 1.97k | if (!sh) |
3034 | 0 | return false; |
3035 | | |
3036 | 1.97k | return sh->sh_flags & SHF_EXECINSTR; |
3037 | 1.97k | } |
3038 | | |
3039 | | static bool starts_with_qmark(const char *s) |
3040 | 0 | { |
3041 | 0 | return s && s[0] == '?'; |
3042 | 0 | } |
3043 | | |
3044 | | static bool btf_needs_sanitization(struct bpf_object *obj) |
3045 | 0 | { |
3046 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3047 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3048 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3049 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3050 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3051 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3052 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3053 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3054 | |
|
3055 | 0 | return !has_func || !has_datasec || !has_func_global || !has_float || |
3056 | 0 | !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec; |
3057 | 0 | } |
3058 | | |
3059 | | static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) |
3060 | 0 | { |
3061 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3062 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3063 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3064 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3065 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3066 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3067 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3068 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3069 | 0 | int enum64_placeholder_id = 0; |
3070 | 0 | struct btf_type *t; |
3071 | 0 | int i, j, vlen; |
3072 | |
|
3073 | 0 | for (i = 1; i < btf__type_cnt(btf); i++) { |
3074 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3075 | |
|
3076 | 0 | if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) { |
3077 | | /* replace VAR/DECL_TAG with INT */ |
3078 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
3079 | | /* |
3080 | | * using size = 1 is the safest choice, 4 will be too |
3081 | | * big and cause kernel BTF validation failure if |
3082 | | * original variable took less than 4 bytes |
3083 | | */ |
3084 | 0 | t->size = 1; |
3085 | 0 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
3086 | 0 | } else if (!has_datasec && btf_is_datasec(t)) { |
3087 | | /* replace DATASEC with STRUCT */ |
3088 | 0 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
3089 | 0 | struct btf_member *m = btf_members(t); |
3090 | 0 | struct btf_type *vt; |
3091 | 0 | char *name; |
3092 | |
|
3093 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3094 | 0 | while (*name) { |
3095 | 0 | if (*name == '.' || *name == '?') |
3096 | 0 | *name = '_'; |
3097 | 0 | name++; |
3098 | 0 | } |
3099 | |
|
3100 | 0 | vlen = btf_vlen(t); |
3101 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
3102 | 0 | for (j = 0; j < vlen; j++, v++, m++) { |
3103 | | /* order of field assignments is important */ |
3104 | 0 | m->offset = v->offset * 8; |
3105 | 0 | m->type = v->type; |
3106 | | /* preserve variable name as member name */ |
3107 | 0 | vt = (void *)btf__type_by_id(btf, v->type); |
3108 | 0 | m->name_off = vt->name_off; |
3109 | 0 | } |
3110 | 0 | } else if (!has_qmark_datasec && btf_is_datasec(t) && |
3111 | 0 | starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { |
3112 | | /* replace '?' prefix with '_' for DATASEC names */ |
3113 | 0 | char *name; |
3114 | |
|
3115 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3116 | 0 | if (name[0] == '?') |
3117 | 0 | name[0] = '_'; |
3118 | 0 | } else if (!has_func && btf_is_func_proto(t)) { |
3119 | | /* replace FUNC_PROTO with ENUM */ |
3120 | 0 | vlen = btf_vlen(t); |
3121 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
3122 | 0 | t->size = sizeof(__u32); /* kernel enforced */ |
3123 | 0 | } else if (!has_func && btf_is_func(t)) { |
3124 | | /* replace FUNC with TYPEDEF */ |
3125 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
3126 | 0 | } else if (!has_func_global && btf_is_func(t)) { |
3127 | | /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */ |
3128 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); |
3129 | 0 | } else if (!has_float && btf_is_float(t)) { |
3130 | | /* replace FLOAT with an equally-sized empty STRUCT; |
3131 | | * since C compilers do not accept e.g. "float" as a |
3132 | | * valid struct name, make it anonymous |
3133 | | */ |
3134 | 0 | t->name_off = 0; |
3135 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); |
3136 | 0 | } else if (!has_type_tag && btf_is_type_tag(t)) { |
3137 | | /* replace TYPE_TAG with a CONST */ |
3138 | 0 | t->name_off = 0; |
3139 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); |
3140 | 0 | } else if (!has_enum64 && btf_is_enum(t)) { |
3141 | | /* clear the kflag */ |
3142 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); |
3143 | 0 | } else if (!has_enum64 && btf_is_enum64(t)) { |
3144 | | /* replace ENUM64 with a union */ |
3145 | 0 | struct btf_member *m; |
3146 | |
|
3147 | 0 | if (enum64_placeholder_id == 0) { |
3148 | 0 | enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); |
3149 | 0 | if (enum64_placeholder_id < 0) |
3150 | 0 | return enum64_placeholder_id; |
3151 | | |
3152 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3153 | 0 | } |
3154 | | |
3155 | 0 | m = btf_members(t); |
3156 | 0 | vlen = btf_vlen(t); |
3157 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); |
3158 | 0 | for (j = 0; j < vlen; j++, m++) { |
3159 | 0 | m->type = enum64_placeholder_id; |
3160 | 0 | m->offset = 0; |
3161 | 0 | } |
3162 | 0 | } |
3163 | 0 | } |
3164 | | |
3165 | 0 | return 0; |
3166 | 0 | } |
3167 | | |
3168 | | static bool libbpf_needs_btf(const struct bpf_object *obj) |
3169 | 2.85k | { |
3170 | 2.85k | return obj->efile.btf_maps_shndx >= 0 || |
3171 | 2.85k | obj->efile.has_st_ops || |
3172 | 2.85k | obj->nr_extern > 0; |
3173 | 2.85k | } |
3174 | | |
3175 | | static bool kernel_needs_btf(const struct bpf_object *obj) |
3176 | 0 | { |
3177 | 0 | return obj->efile.has_st_ops; |
3178 | 0 | } |
3179 | | |
3180 | | static int bpf_object__init_btf(struct bpf_object *obj, |
3181 | | Elf_Data *btf_data, |
3182 | | Elf_Data *btf_ext_data) |
3183 | 6.54k | { |
3184 | 6.54k | int err = -ENOENT; |
3185 | | |
3186 | 6.54k | if (btf_data) { |
3187 | 4.86k | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
3188 | 4.86k | err = libbpf_get_error(obj->btf); |
3189 | 4.86k | if (err) { |
3190 | 927 | obj->btf = NULL; |
3191 | 927 | pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err); |
3192 | 927 | goto out; |
3193 | 927 | } |
3194 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3195 | 3.93k | btf__set_pointer_size(obj->btf, 8); |
3196 | 3.93k | } |
3197 | 5.61k | if (btf_ext_data) { |
3198 | 347 | struct btf_ext_info *ext_segs[3]; |
3199 | 347 | int seg_num, sec_num; |
3200 | | |
3201 | 347 | if (!obj->btf) { |
3202 | 3 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
3203 | 3 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
3204 | 3 | goto out; |
3205 | 3 | } |
3206 | 344 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); |
3207 | 344 | err = libbpf_get_error(obj->btf_ext); |
3208 | 344 | if (err) { |
3209 | 247 | pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n", |
3210 | 247 | BTF_EXT_ELF_SEC, err); |
3211 | 247 | obj->btf_ext = NULL; |
3212 | 247 | goto out; |
3213 | 247 | } |
3214 | | |
3215 | | /* setup .BTF.ext to ELF section mapping */ |
3216 | 97 | ext_segs[0] = &obj->btf_ext->func_info; |
3217 | 97 | ext_segs[1] = &obj->btf_ext->line_info; |
3218 | 97 | ext_segs[2] = &obj->btf_ext->core_relo_info; |
3219 | 388 | for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) { |
3220 | 291 | struct btf_ext_info *seg = ext_segs[seg_num]; |
3221 | 291 | const struct btf_ext_info_sec *sec; |
3222 | 291 | const char *sec_name; |
3223 | 291 | Elf_Scn *scn; |
3224 | | |
3225 | 291 | if (seg->sec_cnt == 0) |
3226 | 188 | continue; |
3227 | | |
3228 | 103 | seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); |
3229 | 103 | if (!seg->sec_idxs) { |
3230 | 0 | err = -ENOMEM; |
3231 | 0 | goto out; |
3232 | 0 | } |
3233 | | |
3234 | 103 | sec_num = 0; |
3235 | 403 | for_each_btf_ext_sec(seg, sec) { |
3236 | | /* preventively increment index to avoid doing |
3237 | | * this before every continue below |
3238 | | */ |
3239 | 403 | sec_num++; |
3240 | | |
3241 | 403 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
3242 | 403 | if (str_is_empty(sec_name)) |
3243 | 194 | continue; |
3244 | 209 | scn = elf_sec_by_name(obj, sec_name); |
3245 | 209 | if (!scn) |
3246 | 191 | continue; |
3247 | | |
3248 | 18 | seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); |
3249 | 18 | } |
3250 | 103 | } |
3251 | 97 | } |
3252 | 6.54k | out: |
3253 | 6.54k | if (err && libbpf_needs_btf(obj)) { |
3254 | 60 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
3255 | 60 | return err; |
3256 | 60 | } |
3257 | 6.48k | return 0; |
3258 | 6.54k | } |
3259 | | |
3260 | | static int compare_vsi_off(const void *_a, const void *_b) |
3261 | 1.49k | { |
3262 | 1.49k | const struct btf_var_secinfo *a = _a; |
3263 | 1.49k | const struct btf_var_secinfo *b = _b; |
3264 | | |
3265 | 1.49k | return a->offset - b->offset; |
3266 | 1.49k | } |
3267 | | |
3268 | | static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, |
3269 | | struct btf_type *t) |
3270 | 3.48k | { |
3271 | 3.48k | __u32 size = 0, i, vars = btf_vlen(t); |
3272 | 3.48k | const char *sec_name = btf__name_by_offset(btf, t->name_off); |
3273 | 3.48k | struct btf_var_secinfo *vsi; |
3274 | 3.48k | bool fixup_offsets = false; |
3275 | 3.48k | int err; |
3276 | | |
3277 | 3.48k | if (!sec_name) { |
3278 | 0 | pr_debug("No name found in string section for DATASEC kind.\n"); |
3279 | 0 | return -ENOENT; |
3280 | 0 | } |
3281 | | |
3282 | | /* Extern-backing datasecs (.ksyms, .kconfig) have their size and |
3283 | | * variable offsets set at the previous step. Further, not every |
3284 | | * extern BTF VAR has corresponding ELF symbol preserved, so we skip |
3285 | | * all fixups altogether for such sections and go straight to sorting |
3286 | | * VARs within their DATASEC. |
3287 | | */ |
3288 | 3.48k | if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0) |
3289 | 434 | goto sort_vars; |
3290 | | |
3291 | | /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to |
3292 | | * fix this up. But BPF static linker already fixes this up and fills |
3293 | | * all the sizes and offsets during static linking. So this step has |
3294 | | * to be optional. But the STV_HIDDEN handling is non-optional for any |
3295 | | * non-extern DATASEC, so the variable fixup loop below handles both |
3296 | | * functions at the same time, paying the cost of BTF VAR <-> ELF |
3297 | | * symbol matching just once. |
3298 | | */ |
3299 | 3.05k | if (t->size == 0) { |
3300 | 401 | err = find_elf_sec_sz(obj, sec_name, &size); |
3301 | 401 | if (err || !size) { |
3302 | 143 | pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n", |
3303 | 143 | sec_name, size, err); |
3304 | 143 | return -ENOENT; |
3305 | 143 | } |
3306 | | |
3307 | 258 | t->size = size; |
3308 | 258 | fixup_offsets = true; |
3309 | 258 | } |
3310 | | |
3311 | 4.58k | for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { |
3312 | 2.05k | const struct btf_type *t_var; |
3313 | 2.05k | struct btf_var *var; |
3314 | 2.05k | const char *var_name; |
3315 | 2.05k | Elf64_Sym *sym; |
3316 | | |
3317 | 2.05k | t_var = btf__type_by_id(btf, vsi->type); |
3318 | 2.05k | if (!t_var || !btf_is_var(t_var)) { |
3319 | 118 | pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); |
3320 | 118 | return -EINVAL; |
3321 | 118 | } |
3322 | | |
3323 | 1.93k | var = btf_var(t_var); |
3324 | 1.93k | if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) |
3325 | 183 | continue; |
3326 | | |
3327 | 1.75k | var_name = btf__name_by_offset(btf, t_var->name_off); |
3328 | 1.75k | if (!var_name) { |
3329 | 0 | pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n", |
3330 | 0 | sec_name, i); |
3331 | 0 | return -ENOENT; |
3332 | 0 | } |
3333 | | |
3334 | 1.75k | sym = find_elf_var_sym(obj, var_name); |
3335 | 1.75k | if (IS_ERR(sym)) { |
3336 | 257 | pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n", |
3337 | 257 | sec_name, var_name); |
3338 | 257 | return -ENOENT; |
3339 | 257 | } |
3340 | | |
3341 | 1.49k | if (fixup_offsets) |
3342 | 69 | vsi->offset = sym->st_value; |
3343 | | |
3344 | | /* if variable is a global/weak symbol, but has restricted |
3345 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR |
3346 | | * as static. This follows similar logic for functions (BPF |
3347 | | * subprogs) and influences libbpf's further decisions about |
3348 | | * whether to make global data BPF array maps as |
3349 | | * BPF_F_MMAPABLE. |
3350 | | */ |
3351 | 1.49k | if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
3352 | 1.49k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) |
3353 | 66 | var->linkage = BTF_VAR_STATIC; |
3354 | 1.49k | } |
3355 | | |
3356 | 2.96k | sort_vars: |
3357 | 2.96k | qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off); |
3358 | 2.96k | return 0; |
3359 | 2.90k | } |
3360 | | |
3361 | | static int bpf_object_fixup_btf(struct bpf_object *obj) |
3362 | 3.72k | { |
3363 | 3.72k | int i, n, err = 0; |
3364 | | |
3365 | 3.72k | if (!obj->btf) |
3366 | 1.51k | return 0; |
3367 | | |
3368 | 2.21k | n = btf__type_cnt(obj->btf); |
3369 | 24.0k | for (i = 1; i < n; i++) { |
3370 | 22.3k | struct btf_type *t = btf_type_by_id(obj->btf, i); |
3371 | | |
3372 | | /* Loader needs to fix up some of the things compiler |
3373 | | * couldn't get its hands on while emitting BTF. This |
3374 | | * is section size and global variable offset. We use |
3375 | | * the info from the ELF itself for this purpose. |
3376 | | */ |
3377 | 22.3k | if (btf_is_datasec(t)) { |
3378 | 3.48k | err = btf_fixup_datasec(obj, obj->btf, t); |
3379 | 3.48k | if (err) |
3380 | 518 | return err; |
3381 | 3.48k | } |
3382 | 22.3k | } |
3383 | | |
3384 | 1.69k | return 0; |
3385 | 2.21k | } |
3386 | | |
3387 | | static bool prog_needs_vmlinux_btf(struct bpf_program *prog) |
3388 | 0 | { |
3389 | 0 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || |
3390 | 0 | prog->type == BPF_PROG_TYPE_LSM) |
3391 | 0 | return true; |
3392 | | |
3393 | | /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs |
3394 | | * also need vmlinux BTF |
3395 | | */ |
3396 | 0 | if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) |
3397 | 0 | return true; |
3398 | | |
3399 | 0 | return false; |
3400 | 0 | } |
3401 | | |
3402 | | static bool map_needs_vmlinux_btf(struct bpf_map *map) |
3403 | 0 | { |
3404 | 0 | return bpf_map__is_struct_ops(map); |
3405 | 0 | } |
3406 | | |
3407 | | static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) |
3408 | 0 | { |
3409 | 0 | struct bpf_program *prog; |
3410 | 0 | struct bpf_map *map; |
3411 | 0 | int i; |
3412 | | |
3413 | | /* CO-RE relocations need kernel BTF, only when btf_custom_path |
3414 | | * is not specified |
3415 | | */ |
3416 | 0 | if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) |
3417 | 0 | return true; |
3418 | | |
3419 | | /* Support for typed ksyms needs kernel BTF */ |
3420 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
3421 | 0 | const struct extern_desc *ext; |
3422 | |
|
3423 | 0 | ext = &obj->externs[i]; |
3424 | 0 | if (ext->type == EXT_KSYM && ext->ksym.type_id) |
3425 | 0 | return true; |
3426 | 0 | } |
3427 | | |
3428 | 0 | bpf_object__for_each_program(prog, obj) { |
3429 | 0 | if (!prog->autoload) |
3430 | 0 | continue; |
3431 | 0 | if (prog_needs_vmlinux_btf(prog)) |
3432 | 0 | return true; |
3433 | 0 | } |
3434 | | |
3435 | 0 | bpf_object__for_each_map(map, obj) { |
3436 | 0 | if (map_needs_vmlinux_btf(map)) |
3437 | 0 | return true; |
3438 | 0 | } |
3439 | | |
3440 | 0 | return false; |
3441 | 0 | } |
3442 | | |
3443 | | static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) |
3444 | 0 | { |
3445 | 0 | int err; |
3446 | | |
3447 | | /* btf_vmlinux could be loaded earlier */ |
3448 | 0 | if (obj->btf_vmlinux || obj->gen_loader) |
3449 | 0 | return 0; |
3450 | | |
3451 | 0 | if (!force && !obj_needs_vmlinux_btf(obj)) |
3452 | 0 | return 0; |
3453 | | |
3454 | 0 | obj->btf_vmlinux = btf__load_vmlinux_btf(); |
3455 | 0 | err = libbpf_get_error(obj->btf_vmlinux); |
3456 | 0 | if (err) { |
3457 | 0 | pr_warn("Error loading vmlinux BTF: %d\n", err); |
3458 | 0 | obj->btf_vmlinux = NULL; |
3459 | 0 | return err; |
3460 | 0 | } |
3461 | 0 | return 0; |
3462 | 0 | } |
3463 | | |
3464 | | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
3465 | 0 | { |
3466 | 0 | struct btf *kern_btf = obj->btf; |
3467 | 0 | bool btf_mandatory, sanitize; |
3468 | 0 | int i, err = 0; |
3469 | |
|
3470 | 0 | if (!obj->btf) |
3471 | 0 | return 0; |
3472 | | |
3473 | 0 | if (!kernel_supports(obj, FEAT_BTF)) { |
3474 | 0 | if (kernel_needs_btf(obj)) { |
3475 | 0 | err = -EOPNOTSUPP; |
3476 | 0 | goto report; |
3477 | 0 | } |
3478 | 0 | pr_debug("Kernel doesn't support BTF, skipping uploading it.\n"); |
3479 | 0 | return 0; |
3480 | 0 | } |
3481 | | |
3482 | | /* Even though some subprogs are global/weak, user might prefer more |
3483 | | * permissive BPF verification process that BPF verifier performs for |
3484 | | * static functions, taking into account more context from the caller |
3485 | | * functions. In such case, they need to mark such subprogs with |
3486 | | * __attribute__((visibility("hidden"))) and libbpf will adjust |
3487 | | * corresponding FUNC BTF type to be marked as static and trigger more |
3488 | | * involved BPF verification process. |
3489 | | */ |
3490 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
3491 | 0 | struct bpf_program *prog = &obj->programs[i]; |
3492 | 0 | struct btf_type *t; |
3493 | 0 | const char *name; |
3494 | 0 | int j, n; |
3495 | |
|
3496 | 0 | if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) |
3497 | 0 | continue; |
3498 | | |
3499 | 0 | n = btf__type_cnt(obj->btf); |
3500 | 0 | for (j = 1; j < n; j++) { |
3501 | 0 | t = btf_type_by_id(obj->btf, j); |
3502 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) |
3503 | 0 | continue; |
3504 | | |
3505 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
3506 | 0 | if (strcmp(name, prog->name) != 0) |
3507 | 0 | continue; |
3508 | | |
3509 | 0 | t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); |
3510 | 0 | break; |
3511 | 0 | } |
3512 | 0 | } |
3513 | |
|
3514 | 0 | sanitize = btf_needs_sanitization(obj); |
3515 | 0 | if (sanitize) { |
3516 | 0 | const void *raw_data; |
3517 | 0 | __u32 sz; |
3518 | | |
3519 | | /* clone BTF to sanitize a copy and leave the original intact */ |
3520 | 0 | raw_data = btf__raw_data(obj->btf, &sz); |
3521 | 0 | kern_btf = btf__new(raw_data, sz); |
3522 | 0 | err = libbpf_get_error(kern_btf); |
3523 | 0 | if (err) |
3524 | 0 | return err; |
3525 | | |
3526 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3527 | 0 | btf__set_pointer_size(obj->btf, 8); |
3528 | 0 | err = bpf_object__sanitize_btf(obj, kern_btf); |
3529 | 0 | if (err) |
3530 | 0 | return err; |
3531 | 0 | } |
3532 | | |
3533 | 0 | if (obj->gen_loader) { |
3534 | 0 | __u32 raw_size = 0; |
3535 | 0 | const void *raw_data = btf__raw_data(kern_btf, &raw_size); |
3536 | |
|
3537 | 0 | if (!raw_data) |
3538 | 0 | return -ENOMEM; |
3539 | 0 | bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); |
3540 | | /* Pretend to have valid FD to pass various fd >= 0 checks. |
3541 | | * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. |
3542 | | */ |
3543 | 0 | btf__set_fd(kern_btf, 0); |
3544 | 0 | } else { |
3545 | | /* currently BPF_BTF_LOAD only supports log_level 1 */ |
3546 | 0 | err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, |
3547 | 0 | obj->log_level ? 1 : 0, obj->token_fd); |
3548 | 0 | } |
3549 | 0 | if (sanitize) { |
3550 | 0 | if (!err) { |
3551 | | /* move fd to libbpf's BTF */ |
3552 | 0 | btf__set_fd(obj->btf, btf__fd(kern_btf)); |
3553 | 0 | btf__set_fd(kern_btf, -1); |
3554 | 0 | } |
3555 | 0 | btf__free(kern_btf); |
3556 | 0 | } |
3557 | 0 | report: |
3558 | 0 | if (err) { |
3559 | 0 | btf_mandatory = kernel_needs_btf(obj); |
3560 | 0 | pr_warn("Error loading .BTF into kernel: %d. %s\n", err, |
3561 | 0 | btf_mandatory ? "BTF is mandatory, can't proceed." |
3562 | 0 | : "BTF is optional, ignoring."); |
3563 | 0 | if (!btf_mandatory) |
3564 | 0 | err = 0; |
3565 | 0 | } |
3566 | 0 | return err; |
3567 | 0 | } |
3568 | | |
3569 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off) |
3570 | 31.0k | { |
3571 | 31.0k | const char *name; |
3572 | | |
3573 | 31.0k | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); |
3574 | 31.0k | if (!name) { |
3575 | 7.94k | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3576 | 7.94k | off, obj->path, elf_errmsg(-1)); |
3577 | 7.94k | return NULL; |
3578 | 7.94k | } |
3579 | | |
3580 | 23.0k | return name; |
3581 | 31.0k | } |
3582 | | |
3583 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off) |
3584 | 50.1k | { |
3585 | 50.1k | const char *name; |
3586 | | |
3587 | 50.1k | name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); |
3588 | 50.1k | if (!name) { |
3589 | 859 | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3590 | 859 | off, obj->path, elf_errmsg(-1)); |
3591 | 859 | return NULL; |
3592 | 859 | } |
3593 | | |
3594 | 49.3k | return name; |
3595 | 50.1k | } |
3596 | | |
3597 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx) |
3598 | 13.9k | { |
3599 | 13.9k | Elf_Scn *scn; |
3600 | | |
3601 | 13.9k | scn = elf_getscn(obj->efile.elf, idx); |
3602 | 13.9k | if (!scn) { |
3603 | 0 | pr_warn("elf: failed to get section(%zu) from %s: %s\n", |
3604 | 0 | idx, obj->path, elf_errmsg(-1)); |
3605 | 0 | return NULL; |
3606 | 0 | } |
3607 | 13.9k | return scn; |
3608 | 13.9k | } |
3609 | | |
3610 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name) |
3611 | 610 | { |
3612 | 610 | Elf_Scn *scn = NULL; |
3613 | 610 | Elf *elf = obj->efile.elf; |
3614 | 610 | const char *sec_name; |
3615 | | |
3616 | 4.38k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3617 | 4.05k | sec_name = elf_sec_name(obj, scn); |
3618 | 4.05k | if (!sec_name) |
3619 | 0 | return NULL; |
3620 | | |
3621 | 4.05k | if (strcmp(sec_name, name) != 0) |
3622 | 3.77k | continue; |
3623 | | |
3624 | 279 | return scn; |
3625 | 4.05k | } |
3626 | 331 | return NULL; |
3627 | 610 | } |
3628 | | |
3629 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn) |
3630 | 103k | { |
3631 | 103k | Elf64_Shdr *shdr; |
3632 | | |
3633 | 103k | if (!scn) |
3634 | 0 | return NULL; |
3635 | | |
3636 | 103k | shdr = elf64_getshdr(scn); |
3637 | 103k | if (!shdr) { |
3638 | 0 | pr_warn("elf: failed to get section(%zu) header from %s: %s\n", |
3639 | 0 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3640 | 0 | return NULL; |
3641 | 0 | } |
3642 | | |
3643 | 103k | return shdr; |
3644 | 103k | } |
3645 | | |
3646 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn) |
3647 | 8.81k | { |
3648 | 8.81k | const char *name; |
3649 | 8.81k | Elf64_Shdr *sh; |
3650 | | |
3651 | 8.81k | if (!scn) |
3652 | 0 | return NULL; |
3653 | | |
3654 | 8.81k | sh = elf_sec_hdr(obj, scn); |
3655 | 8.81k | if (!sh) |
3656 | 0 | return NULL; |
3657 | | |
3658 | 8.81k | name = elf_sec_str(obj, sh->sh_name); |
3659 | 8.81k | if (!name) { |
3660 | 777 | pr_warn("elf: failed to get section(%zu) name from %s: %s\n", |
3661 | 777 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3662 | 777 | return NULL; |
3663 | 777 | } |
3664 | | |
3665 | 8.03k | return name; |
3666 | 8.81k | } |
3667 | | |
3668 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn) |
3669 | 42.3k | { |
3670 | 42.3k | Elf_Data *data; |
3671 | | |
3672 | 42.3k | if (!scn) |
3673 | 140 | return NULL; |
3674 | | |
3675 | 42.2k | data = elf_getdata(scn, 0); |
3676 | 42.2k | if (!data) { |
3677 | 431 | pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n", |
3678 | 431 | elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>", |
3679 | 431 | obj->path, elf_errmsg(-1)); |
3680 | 431 | return NULL; |
3681 | 431 | } |
3682 | | |
3683 | 41.8k | return data; |
3684 | 42.2k | } |
3685 | | |
3686 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx) |
3687 | 565k | { |
3688 | 565k | if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) |
3689 | 126 | return NULL; |
3690 | | |
3691 | 565k | return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; |
3692 | 565k | } |
3693 | | |
3694 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx) |
3695 | 7.66k | { |
3696 | 7.66k | if (idx >= data->d_size / sizeof(Elf64_Rel)) |
3697 | 0 | return NULL; |
3698 | | |
3699 | 7.66k | return (Elf64_Rel *)data->d_buf + idx; |
3700 | 7.66k | } |
3701 | | |
3702 | | static bool is_sec_name_dwarf(const char *name) |
3703 | 36.7k | { |
3704 | | /* approximation, but the actual list is too long */ |
3705 | 36.7k | return str_has_pfx(name, ".debug_"); |
3706 | 36.7k | } |
3707 | | |
3708 | | static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name) |
3709 | 40.4k | { |
3710 | | /* no special handling of .strtab */ |
3711 | 40.4k | if (hdr->sh_type == SHT_STRTAB) |
3712 | 5.54k | return true; |
3713 | | |
3714 | | /* ignore .llvm_addrsig section as well */ |
3715 | 34.8k | if (hdr->sh_type == SHT_LLVM_ADDRSIG) |
3716 | 41 | return true; |
3717 | | |
3718 | | /* no subprograms will lead to an empty .text section, ignore it */ |
3719 | 34.8k | if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && |
3720 | 34.8k | strcmp(name, ".text") == 0) |
3721 | 77 | return true; |
3722 | | |
3723 | | /* DWARF sections */ |
3724 | 34.7k | if (is_sec_name_dwarf(name)) |
3725 | 2.90k | return true; |
3726 | | |
3727 | 31.8k | if (str_has_pfx(name, ".rel")) { |
3728 | 1.97k | name += sizeof(".rel") - 1; |
3729 | | /* DWARF section relocations */ |
3730 | 1.97k | if (is_sec_name_dwarf(name)) |
3731 | 207 | return true; |
3732 | | |
3733 | | /* .BTF and .BTF.ext don't need relocations */ |
3734 | 1.77k | if (strcmp(name, BTF_ELF_SEC) == 0 || |
3735 | 1.77k | strcmp(name, BTF_EXT_ELF_SEC) == 0) |
3736 | 453 | return true; |
3737 | 1.77k | } |
3738 | | |
3739 | 31.2k | return false; |
3740 | 31.8k | } |
3741 | | |
3742 | | static int cmp_progs(const void *_a, const void *_b) |
3743 | 72.8k | { |
3744 | 72.8k | const struct bpf_program *a = _a; |
3745 | 72.8k | const struct bpf_program *b = _b; |
3746 | | |
3747 | 72.8k | if (a->sec_idx != b->sec_idx) |
3748 | 513 | return a->sec_idx < b->sec_idx ? -1 : 1; |
3749 | | |
3750 | | /* sec_insn_off can't be the same within the section */ |
3751 | 72.3k | return a->sec_insn_off < b->sec_insn_off ? -1 : 1; |
3752 | 72.8k | } |
3753 | | |
3754 | | static int bpf_object__elf_collect(struct bpf_object *obj) |
3755 | 8.79k | { |
3756 | 8.79k | struct elf_sec_desc *sec_desc; |
3757 | 8.79k | Elf *elf = obj->efile.elf; |
3758 | 8.79k | Elf_Data *btf_ext_data = NULL; |
3759 | 8.79k | Elf_Data *btf_data = NULL; |
3760 | 8.79k | int idx = 0, err = 0; |
3761 | 8.79k | const char *name; |
3762 | 8.79k | Elf_Data *data; |
3763 | 8.79k | Elf_Scn *scn; |
3764 | 8.79k | Elf64_Shdr *sh; |
3765 | | |
3766 | | /* ELF section indices are 0-based, but sec #0 is special "invalid" |
3767 | | * section. Since section count retrieved by elf_getshdrnum() does |
3768 | | * include sec #0, it is already the necessary size of an array to keep |
3769 | | * all the sections. |
3770 | | */ |
3771 | 8.79k | if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { |
3772 | 0 | pr_warn("elf: failed to get the number of sections for %s: %s\n", |
3773 | 0 | obj->path, elf_errmsg(-1)); |
3774 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3775 | 0 | } |
3776 | 8.79k | obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); |
3777 | 8.79k | if (!obj->efile.secs) |
3778 | 0 | return -ENOMEM; |
3779 | | |
3780 | | /* a bunch of ELF parsing functionality depends on processing symbols, |
3781 | | * so do the first pass and find the symbol table |
3782 | | */ |
3783 | 8.79k | scn = NULL; |
3784 | 53.9k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3785 | 45.3k | sh = elf_sec_hdr(obj, scn); |
3786 | 45.3k | if (!sh) |
3787 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3788 | | |
3789 | 45.3k | if (sh->sh_type == SHT_SYMTAB) { |
3790 | 8.77k | if (obj->efile.symbols) { |
3791 | 1 | pr_warn("elf: multiple symbol tables in %s\n", obj->path); |
3792 | 1 | return -LIBBPF_ERRNO__FORMAT; |
3793 | 1 | } |
3794 | | |
3795 | 8.77k | data = elf_sec_data(obj, scn); |
3796 | 8.77k | if (!data) |
3797 | 239 | return -LIBBPF_ERRNO__FORMAT; |
3798 | | |
3799 | 8.53k | idx = elf_ndxscn(scn); |
3800 | | |
3801 | 8.53k | obj->efile.symbols = data; |
3802 | 8.53k | obj->efile.symbols_shndx = idx; |
3803 | 8.53k | obj->efile.strtabidx = sh->sh_link; |
3804 | 8.53k | } |
3805 | 45.3k | } |
3806 | | |
3807 | 8.55k | if (!obj->efile.symbols) { |
3808 | 21 | pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n", |
3809 | 21 | obj->path); |
3810 | 21 | return -ENOENT; |
3811 | 21 | } |
3812 | | |
3813 | 8.53k | scn = NULL; |
3814 | 47.9k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3815 | 40.5k | idx = elf_ndxscn(scn); |
3816 | 40.5k | sec_desc = &obj->efile.secs[idx]; |
3817 | | |
3818 | 40.5k | sh = elf_sec_hdr(obj, scn); |
3819 | 40.5k | if (!sh) |
3820 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3821 | | |
3822 | 40.5k | name = elf_sec_str(obj, sh->sh_name); |
3823 | 40.5k | if (!name) |
3824 | 82 | return -LIBBPF_ERRNO__FORMAT; |
3825 | | |
3826 | 40.4k | if (ignore_elf_section(sh, name)) |
3827 | 9.22k | continue; |
3828 | | |
3829 | 31.2k | data = elf_sec_data(obj, scn); |
3830 | 31.2k | if (!data) |
3831 | 175 | return -LIBBPF_ERRNO__FORMAT; |
3832 | | |
3833 | 31.0k | pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
3834 | 31.0k | idx, name, (unsigned long)data->d_size, |
3835 | 31.0k | (int)sh->sh_link, (unsigned long)sh->sh_flags, |
3836 | 31.0k | (int)sh->sh_type); |
3837 | | |
3838 | 31.0k | if (strcmp(name, "license") == 0) { |
3839 | 611 | err = bpf_object__init_license(obj, data->d_buf, data->d_size); |
3840 | 611 | if (err) |
3841 | 2 | return err; |
3842 | 30.4k | } else if (strcmp(name, "version") == 0) { |
3843 | 87 | err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); |
3844 | 87 | if (err) |
3845 | 17 | return err; |
3846 | 30.3k | } else if (strcmp(name, "maps") == 0) { |
3847 | 7 | pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n"); |
3848 | 7 | return -ENOTSUP; |
3849 | 30.3k | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
3850 | 1.63k | obj->efile.btf_maps_shndx = idx; |
3851 | 28.7k | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
3852 | 5.01k | if (sh->sh_type != SHT_PROGBITS) |
3853 | 62 | return -LIBBPF_ERRNO__FORMAT; |
3854 | 4.94k | btf_data = data; |
3855 | 23.6k | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
3856 | 452 | if (sh->sh_type != SHT_PROGBITS) |
3857 | 38 | return -LIBBPF_ERRNO__FORMAT; |
3858 | 414 | btf_ext_data = data; |
3859 | 23.2k | } else if (sh->sh_type == SHT_SYMTAB) { |
3860 | | /* already processed during the first pass above */ |
3861 | 16.0k | } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { |
3862 | 3.35k | if (sh->sh_flags & SHF_EXECINSTR) { |
3863 | 1.12k | if (strcmp(name, ".text") == 0) |
3864 | 226 | obj->efile.text_shndx = idx; |
3865 | 1.12k | err = bpf_object__add_programs(obj, data, name, idx); |
3866 | 1.12k | if (err) |
3867 | 247 | return err; |
3868 | 2.22k | } else if (strcmp(name, DATA_SEC) == 0 || |
3869 | 2.22k | str_has_pfx(name, DATA_SEC ".")) { |
3870 | 519 | sec_desc->sec_type = SEC_DATA; |
3871 | 519 | sec_desc->shdr = sh; |
3872 | 519 | sec_desc->data = data; |
3873 | 1.70k | } else if (strcmp(name, RODATA_SEC) == 0 || |
3874 | 1.70k | str_has_pfx(name, RODATA_SEC ".")) { |
3875 | 373 | sec_desc->sec_type = SEC_RODATA; |
3876 | 373 | sec_desc->shdr = sh; |
3877 | 373 | sec_desc->data = data; |
3878 | 1.33k | } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || |
3879 | 1.33k | strcmp(name, STRUCT_OPS_LINK_SEC) == 0 || |
3880 | 1.33k | strcmp(name, "?" STRUCT_OPS_SEC) == 0 || |
3881 | 1.33k | strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) { |
3882 | 491 | sec_desc->sec_type = SEC_ST_OPS; |
3883 | 491 | sec_desc->shdr = sh; |
3884 | 491 | sec_desc->data = data; |
3885 | 491 | obj->efile.has_st_ops = true; |
3886 | 839 | } else if (strcmp(name, ARENA_SEC) == 0) { |
3887 | 78 | obj->efile.arena_data = data; |
3888 | 78 | obj->efile.arena_data_shndx = idx; |
3889 | 761 | } else { |
3890 | 761 | pr_info("elf: skipping unrecognized data section(%d) %s\n", |
3891 | 761 | idx, name); |
3892 | 761 | } |
3893 | 12.6k | } else if (sh->sh_type == SHT_REL) { |
3894 | 2.13k | int targ_sec_idx = sh->sh_info; /* points to other section */ |
3895 | | |
3896 | 2.13k | if (sh->sh_entsize != sizeof(Elf64_Rel) || |
3897 | 2.13k | targ_sec_idx >= obj->efile.sec_cnt) |
3898 | 163 | return -LIBBPF_ERRNO__FORMAT; |
3899 | | |
3900 | | /* Only do relo for section with exec instructions */ |
3901 | 1.97k | if (!section_have_execinstr(obj, targ_sec_idx) && |
3902 | 1.97k | strcmp(name, ".rel" STRUCT_OPS_SEC) && |
3903 | 1.97k | strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && |
3904 | 1.97k | strcmp(name, ".rel?" STRUCT_OPS_SEC) && |
3905 | 1.97k | strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) && |
3906 | 1.97k | strcmp(name, ".rel" MAPS_ELF_SEC)) { |
3907 | 566 | pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", |
3908 | 566 | idx, name, targ_sec_idx, |
3909 | 566 | elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>"); |
3910 | 566 | continue; |
3911 | 566 | } |
3912 | | |
3913 | 1.40k | sec_desc->sec_type = SEC_RELO; |
3914 | 1.40k | sec_desc->shdr = sh; |
3915 | 1.40k | sec_desc->data = data; |
3916 | 10.5k | } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || |
3917 | 2.18k | str_has_pfx(name, BSS_SEC "."))) { |
3918 | 1.35k | sec_desc->sec_type = SEC_BSS; |
3919 | 1.35k | sec_desc->shdr = sh; |
3920 | 1.35k | sec_desc->data = data; |
3921 | 9.19k | } else { |
3922 | 9.19k | pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name, |
3923 | 9.19k | (size_t)sh->sh_size); |
3924 | 9.19k | } |
3925 | 31.0k | } |
3926 | | |
3927 | 7.64k | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
3928 | 1.20k | pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); |
3929 | 1.20k | return -LIBBPF_ERRNO__FORMAT; |
3930 | 1.20k | } |
3931 | | |
3932 | | /* sort BPF programs by section name and in-section instruction offset |
3933 | | * for faster search |
3934 | | */ |
3935 | 6.23k | if (obj->nr_programs) |
3936 | 569 | qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); |
3937 | | |
3938 | 6.23k | return bpf_object__init_btf(obj, btf_data, btf_ext_data); |
3939 | 7.43k | } |
3940 | | |
3941 | | static bool sym_is_extern(const Elf64_Sym *sym) |
3942 | 358k | { |
3943 | 358k | int bind = ELF64_ST_BIND(sym->st_info); |
3944 | | /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */ |
3945 | 358k | return sym->st_shndx == SHN_UNDEF && |
3946 | 358k | (bind == STB_GLOBAL || bind == STB_WEAK) && |
3947 | 358k | ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; |
3948 | 358k | } |
3949 | | |
3950 | | static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx) |
3951 | 1.43k | { |
3952 | 1.43k | int bind = ELF64_ST_BIND(sym->st_info); |
3953 | 1.43k | int type = ELF64_ST_TYPE(sym->st_info); |
3954 | | |
3955 | | /* in .text section */ |
3956 | 1.43k | if (sym->st_shndx != text_shndx) |
3957 | 485 | return false; |
3958 | | |
3959 | | /* local function */ |
3960 | 954 | if (bind == STB_LOCAL && type == STT_SECTION) |
3961 | 647 | return true; |
3962 | | |
3963 | | /* global function */ |
3964 | 307 | return bind == STB_GLOBAL && type == STT_FUNC; |
3965 | 954 | } |
3966 | | |
3967 | | static int find_extern_btf_id(const struct btf *btf, const char *ext_name) |
3968 | 4.06k | { |
3969 | 4.06k | const struct btf_type *t; |
3970 | 4.06k | const char *tname; |
3971 | 4.06k | int i, n; |
3972 | | |
3973 | 4.06k | if (!btf) |
3974 | 38 | return -ESRCH; |
3975 | | |
3976 | 4.02k | n = btf__type_cnt(btf); |
3977 | 30.3k | for (i = 1; i < n; i++) { |
3978 | 30.1k | t = btf__type_by_id(btf, i); |
3979 | | |
3980 | 30.1k | if (!btf_is_var(t) && !btf_is_func(t)) |
3981 | 22.4k | continue; |
3982 | | |
3983 | 7.71k | tname = btf__name_by_offset(btf, t->name_off); |
3984 | 7.71k | if (strcmp(tname, ext_name)) |
3985 | 3.83k | continue; |
3986 | | |
3987 | 3.87k | if (btf_is_var(t) && |
3988 | 3.87k | btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) |
3989 | 53 | return -EINVAL; |
3990 | | |
3991 | 3.81k | if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN) |
3992 | 12 | return -EINVAL; |
3993 | | |
3994 | 3.80k | return i; |
3995 | 3.81k | } |
3996 | | |
3997 | 153 | return -ENOENT; |
3998 | 4.02k | } |
3999 | | |
4000 | 3.80k | static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) { |
4001 | 3.80k | const struct btf_var_secinfo *vs; |
4002 | 3.80k | const struct btf_type *t; |
4003 | 3.80k | int i, j, n; |
4004 | | |
4005 | 3.80k | if (!btf) |
4006 | 0 | return -ESRCH; |
4007 | | |
4008 | 3.80k | n = btf__type_cnt(btf); |
4009 | 22.6k | for (i = 1; i < n; i++) { |
4010 | 22.5k | t = btf__type_by_id(btf, i); |
4011 | | |
4012 | 22.5k | if (!btf_is_datasec(t)) |
4013 | 15.0k | continue; |
4014 | | |
4015 | 7.48k | vs = btf_var_secinfos(t); |
4016 | 15.3k | for (j = 0; j < btf_vlen(t); j++, vs++) { |
4017 | 11.6k | if (vs->type == ext_btf_id) |
4018 | 3.77k | return i; |
4019 | 11.6k | } |
4020 | 7.48k | } |
4021 | | |
4022 | 35 | return -ENOENT; |
4023 | 3.80k | } |
4024 | | |
4025 | | static enum kcfg_type find_kcfg_type(const struct btf *btf, int id, |
4026 | | bool *is_signed) |
4027 | 1.03k | { |
4028 | 1.03k | const struct btf_type *t; |
4029 | 1.03k | const char *name; |
4030 | | |
4031 | 1.03k | t = skip_mods_and_typedefs(btf, id, NULL); |
4032 | 1.03k | name = btf__name_by_offset(btf, t->name_off); |
4033 | | |
4034 | 1.03k | if (is_signed) |
4035 | 970 | *is_signed = false; |
4036 | 1.03k | switch (btf_kind(t)) { |
4037 | 666 | case BTF_KIND_INT: { |
4038 | 666 | int enc = btf_int_encoding(t); |
4039 | | |
4040 | 666 | if (enc & BTF_INT_BOOL) |
4041 | 257 | return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; |
4042 | 409 | if (is_signed) |
4043 | 371 | *is_signed = enc & BTF_INT_SIGNED; |
4044 | 409 | if (t->size == 1) |
4045 | 189 | return KCFG_CHAR; |
4046 | 220 | if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) |
4047 | 36 | return KCFG_UNKNOWN; |
4048 | 184 | return KCFG_INT; |
4049 | 220 | } |
4050 | 163 | case BTF_KIND_ENUM: |
4051 | 163 | if (t->size != 4) |
4052 | 34 | return KCFG_UNKNOWN; |
4053 | 129 | if (strcmp(name, "libbpf_tristate")) |
4054 | 119 | return KCFG_UNKNOWN; |
4055 | 10 | return KCFG_TRISTATE; |
4056 | 97 | case BTF_KIND_ENUM64: |
4057 | 97 | if (strcmp(name, "libbpf_tristate")) |
4058 | 87 | return KCFG_UNKNOWN; |
4059 | 10 | return KCFG_TRISTATE; |
4060 | 63 | case BTF_KIND_ARRAY: |
4061 | 63 | if (btf_array(t)->nelems == 0) |
4062 | 0 | return KCFG_UNKNOWN; |
4063 | 63 | if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) |
4064 | 33 | return KCFG_UNKNOWN; |
4065 | 30 | return KCFG_CHAR_ARR; |
4066 | 44 | default: |
4067 | 44 | return KCFG_UNKNOWN; |
4068 | 1.03k | } |
4069 | 1.03k | } |
4070 | | |
4071 | | static int cmp_externs(const void *_a, const void *_b) |
4072 | 6.39k | { |
4073 | 6.39k | const struct extern_desc *a = _a; |
4074 | 6.39k | const struct extern_desc *b = _b; |
4075 | | |
4076 | 6.39k | if (a->type != b->type) |
4077 | 0 | return a->type < b->type ? -1 : 1; |
4078 | | |
4079 | 6.39k | if (a->type == EXT_KCFG) { |
4080 | | /* descending order by alignment requirements */ |
4081 | 994 | if (a->kcfg.align != b->kcfg.align) |
4082 | 0 | return a->kcfg.align > b->kcfg.align ? -1 : 1; |
4083 | | /* ascending order by size, within same alignment class */ |
4084 | 994 | if (a->kcfg.sz != b->kcfg.sz) |
4085 | 0 | return a->kcfg.sz < b->kcfg.sz ? -1 : 1; |
4086 | 994 | } |
4087 | | |
4088 | | /* resolve ties by name */ |
4089 | 6.39k | return strcmp(a->name, b->name); |
4090 | 6.39k | } |
4091 | | |
4092 | | static int find_int_btf_id(const struct btf *btf) |
4093 | 916 | { |
4094 | 916 | const struct btf_type *t; |
4095 | 916 | int i, n; |
4096 | | |
4097 | 916 | n = btf__type_cnt(btf); |
4098 | 9.84k | for (i = 1; i < n; i++) { |
4099 | 9.01k | t = btf__type_by_id(btf, i); |
4100 | | |
4101 | 9.01k | if (btf_is_int(t) && btf_int_bits(t) == 32) |
4102 | 80 | return i; |
4103 | 9.01k | } |
4104 | | |
4105 | 836 | return 0; |
4106 | 916 | } |
4107 | | |
4108 | | static int add_dummy_ksym_var(struct btf *btf) |
4109 | 5.10k | { |
4110 | 5.10k | int i, int_btf_id, sec_btf_id, dummy_var_btf_id; |
4111 | 5.10k | const struct btf_var_secinfo *vs; |
4112 | 5.10k | const struct btf_type *sec; |
4113 | | |
4114 | 5.10k | if (!btf) |
4115 | 1.54k | return 0; |
4116 | | |
4117 | 3.55k | sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC, |
4118 | 3.55k | BTF_KIND_DATASEC); |
4119 | 3.55k | if (sec_btf_id < 0) |
4120 | 2.81k | return 0; |
4121 | | |
4122 | 743 | sec = btf__type_by_id(btf, sec_btf_id); |
4123 | 743 | vs = btf_var_secinfos(sec); |
4124 | 1.68k | for (i = 0; i < btf_vlen(sec); i++, vs++) { |
4125 | 1.48k | const struct btf_type *vt; |
4126 | | |
4127 | 1.48k | vt = btf__type_by_id(btf, vs->type); |
4128 | 1.48k | if (btf_is_func(vt)) |
4129 | 545 | break; |
4130 | 1.48k | } |
4131 | | |
4132 | | /* No func in ksyms sec. No need to add dummy var. */ |
4133 | 743 | if (i == btf_vlen(sec)) |
4134 | 198 | return 0; |
4135 | | |
4136 | 545 | int_btf_id = find_int_btf_id(btf); |
4137 | 545 | dummy_var_btf_id = btf__add_var(btf, |
4138 | 545 | "dummy_ksym", |
4139 | 545 | BTF_VAR_GLOBAL_ALLOCATED, |
4140 | 545 | int_btf_id); |
4141 | 545 | if (dummy_var_btf_id < 0) |
4142 | 545 | pr_warn("cannot create a dummy_ksym var\n"); |
4143 | | |
4144 | 545 | return dummy_var_btf_id; |
4145 | 743 | } |
4146 | | |
4147 | | static int bpf_object__collect_externs(struct bpf_object *obj) |
4148 | 6.48k | { |
4149 | 6.48k | struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL; |
4150 | 6.48k | const struct btf_type *t; |
4151 | 6.48k | struct extern_desc *ext; |
4152 | 6.48k | int i, n, off, dummy_var_btf_id; |
4153 | 6.48k | const char *ext_name, *sec_name; |
4154 | 6.48k | size_t ext_essent_len; |
4155 | 6.48k | Elf_Scn *scn; |
4156 | 6.48k | Elf64_Shdr *sh; |
4157 | | |
4158 | 6.48k | if (!obj->efile.symbols) |
4159 | 0 | return 0; |
4160 | | |
4161 | 6.48k | scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); |
4162 | 6.48k | sh = elf_sec_hdr(obj, scn); |
4163 | 6.48k | if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) |
4164 | 1.37k | return -LIBBPF_ERRNO__FORMAT; |
4165 | | |
4166 | 5.10k | dummy_var_btf_id = add_dummy_ksym_var(obj->btf); |
4167 | 5.10k | if (dummy_var_btf_id < 0) |
4168 | 0 | return dummy_var_btf_id; |
4169 | | |
4170 | 5.10k | n = sh->sh_size / sh->sh_entsize; |
4171 | 5.10k | pr_debug("looking for externs among %d symbols...\n", n); |
4172 | | |
4173 | 361k | for (i = 0; i < n; i++) { |
4174 | 357k | Elf64_Sym *sym = elf_sym_by_idx(obj, i); |
4175 | | |
4176 | 357k | if (!sym) |
4177 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4178 | 357k | if (!sym_is_extern(sym)) |
4179 | 349k | continue; |
4180 | 7.09k | ext_name = elf_sym_str(obj, sym->st_name); |
4181 | 7.09k | if (!ext_name || !ext_name[0]) |
4182 | 3.03k | continue; |
4183 | | |
4184 | 4.06k | ext = obj->externs; |
4185 | 4.06k | ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); |
4186 | 4.06k | if (!ext) |
4187 | 0 | return -ENOMEM; |
4188 | 4.06k | obj->externs = ext; |
4189 | 4.06k | ext = &ext[obj->nr_extern]; |
4190 | 4.06k | memset(ext, 0, sizeof(*ext)); |
4191 | 4.06k | obj->nr_extern++; |
4192 | | |
4193 | 4.06k | ext->btf_id = find_extern_btf_id(obj->btf, ext_name); |
4194 | 4.06k | if (ext->btf_id <= 0) { |
4195 | 256 | pr_warn("failed to find BTF for extern '%s': %d\n", |
4196 | 256 | ext_name, ext->btf_id); |
4197 | 256 | return ext->btf_id; |
4198 | 256 | } |
4199 | 3.80k | t = btf__type_by_id(obj->btf, ext->btf_id); |
4200 | 3.80k | ext->name = btf__name_by_offset(obj->btf, t->name_off); |
4201 | 3.80k | ext->sym_idx = i; |
4202 | 3.80k | ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; |
4203 | | |
4204 | 3.80k | ext_essent_len = bpf_core_essential_name_len(ext->name); |
4205 | 3.80k | ext->essent_name = NULL; |
4206 | 3.80k | if (ext_essent_len != strlen(ext->name)) { |
4207 | 246 | ext->essent_name = strndup(ext->name, ext_essent_len); |
4208 | 246 | if (!ext->essent_name) |
4209 | 0 | return -ENOMEM; |
4210 | 246 | } |
4211 | | |
4212 | 3.80k | ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); |
4213 | 3.80k | if (ext->sec_btf_id <= 0) { |
4214 | 35 | pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n", |
4215 | 35 | ext_name, ext->btf_id, ext->sec_btf_id); |
4216 | 35 | return ext->sec_btf_id; |
4217 | 35 | } |
4218 | 3.77k | sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); |
4219 | 3.77k | sec_name = btf__name_by_offset(obj->btf, sec->name_off); |
4220 | | |
4221 | 3.77k | if (strcmp(sec_name, KCONFIG_SEC) == 0) { |
4222 | 1.18k | if (btf_is_func(t)) { |
4223 | 1 | pr_warn("extern function %s is unsupported under %s section\n", |
4224 | 1 | ext->name, KCONFIG_SEC); |
4225 | 1 | return -ENOTSUP; |
4226 | 1 | } |
4227 | 1.18k | kcfg_sec = sec; |
4228 | 1.18k | ext->type = EXT_KCFG; |
4229 | 1.18k | ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); |
4230 | 1.18k | if (ext->kcfg.sz <= 0) { |
4231 | 170 | pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n", |
4232 | 170 | ext_name, ext->kcfg.sz); |
4233 | 170 | return ext->kcfg.sz; |
4234 | 170 | } |
4235 | 1.01k | ext->kcfg.align = btf__align_of(obj->btf, t->type); |
4236 | 1.01k | if (ext->kcfg.align <= 0) { |
4237 | 40 | pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n", |
4238 | 40 | ext_name, ext->kcfg.align); |
4239 | 40 | return -EINVAL; |
4240 | 40 | } |
4241 | 970 | ext->kcfg.type = find_kcfg_type(obj->btf, t->type, |
4242 | 970 | &ext->kcfg.is_signed); |
4243 | 970 | if (ext->kcfg.type == KCFG_UNKNOWN) { |
4244 | 348 | pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name); |
4245 | 348 | return -ENOTSUP; |
4246 | 348 | } |
4247 | 2.59k | } else if (strcmp(sec_name, KSYMS_SEC) == 0) { |
4248 | 2.48k | ksym_sec = sec; |
4249 | 2.48k | ext->type = EXT_KSYM; |
4250 | 2.48k | skip_mods_and_typedefs(obj->btf, t->type, |
4251 | 2.48k | &ext->ksym.type_id); |
4252 | 2.48k | } else { |
4253 | 107 | pr_warn("unrecognized extern section '%s'\n", sec_name); |
4254 | 107 | return -ENOTSUP; |
4255 | 107 | } |
4256 | 3.77k | } |
4257 | 4.14k | pr_debug("collected %d externs total\n", obj->nr_extern); |
4258 | | |
4259 | 4.14k | if (!obj->nr_extern) |
4260 | 3.59k | return 0; |
4261 | | |
4262 | | /* sort externs by type, for kcfg ones also by (align, size, name) */ |
4263 | 555 | qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); |
4264 | | |
4265 | | /* for .ksyms section, we need to turn all externs into allocated |
4266 | | * variables in BTF to pass kernel verification; we do this by |
4267 | | * pretending that each extern is a 8-byte variable |
4268 | | */ |
4269 | 555 | if (ksym_sec) { |
4270 | | /* find existing 4-byte integer type in BTF to use for fake |
4271 | | * extern variables in DATASEC |
4272 | | */ |
4273 | 371 | int int_btf_id = find_int_btf_id(obj->btf); |
4274 | | /* For extern function, a dummy_var added earlier |
4275 | | * will be used to replace the vs->type and |
4276 | | * its name string will be used to refill |
4277 | | * the missing param's name. |
4278 | | */ |
4279 | 371 | const struct btf_type *dummy_var; |
4280 | | |
4281 | 371 | dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); |
4282 | 2.69k | for (i = 0; i < obj->nr_extern; i++) { |
4283 | 2.32k | ext = &obj->externs[i]; |
4284 | 2.32k | if (ext->type != EXT_KSYM) |
4285 | 0 | continue; |
4286 | 2.32k | pr_debug("extern (ksym) #%d: symbol %d, name %s\n", |
4287 | 2.32k | i, ext->sym_idx, ext->name); |
4288 | 2.32k | } |
4289 | | |
4290 | 371 | sec = ksym_sec; |
4291 | 371 | n = btf_vlen(sec); |
4292 | 779 | for (i = 0, off = 0; i < n; i++, off += sizeof(int)) { |
4293 | 731 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4294 | 731 | struct btf_type *vt; |
4295 | | |
4296 | 731 | vt = (void *)btf__type_by_id(obj->btf, vs->type); |
4297 | 731 | ext_name = btf__name_by_offset(obj->btf, vt->name_off); |
4298 | 731 | ext = find_extern_by_name(obj, ext_name); |
4299 | 731 | if (!ext) { |
4300 | 323 | pr_warn("failed to find extern definition for BTF %s '%s'\n", |
4301 | 323 | btf_kind_str(vt), ext_name); |
4302 | 323 | return -ESRCH; |
4303 | 323 | } |
4304 | 408 | if (btf_is_func(vt)) { |
4305 | 63 | const struct btf_type *func_proto; |
4306 | 63 | struct btf_param *param; |
4307 | 63 | int j; |
4308 | | |
4309 | 63 | func_proto = btf__type_by_id(obj->btf, |
4310 | 63 | vt->type); |
4311 | 63 | param = btf_params(func_proto); |
4312 | | /* Reuse the dummy_var string if the |
4313 | | * func proto does not have param name. |
4314 | | */ |
4315 | 156 | for (j = 0; j < btf_vlen(func_proto); j++) |
4316 | 93 | if (param[j].type && !param[j].name_off) |
4317 | 23 | param[j].name_off = |
4318 | 23 | dummy_var->name_off; |
4319 | 63 | vs->type = dummy_var_btf_id; |
4320 | 63 | vt->info &= ~0xffff; |
4321 | 63 | vt->info |= BTF_FUNC_GLOBAL; |
4322 | 345 | } else { |
4323 | 345 | btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4324 | 345 | vt->type = int_btf_id; |
4325 | 345 | } |
4326 | 408 | vs->offset = off; |
4327 | 408 | vs->size = sizeof(int); |
4328 | 408 | } |
4329 | 48 | sec->size = off; |
4330 | 48 | } |
4331 | | |
4332 | 232 | if (kcfg_sec) { |
4333 | 184 | sec = kcfg_sec; |
4334 | | /* for kcfg externs calculate their offsets within a .kconfig map */ |
4335 | 184 | off = 0; |
4336 | 799 | for (i = 0; i < obj->nr_extern; i++) { |
4337 | 615 | ext = &obj->externs[i]; |
4338 | 615 | if (ext->type != EXT_KCFG) |
4339 | 0 | continue; |
4340 | | |
4341 | 615 | ext->kcfg.data_off = roundup(off, ext->kcfg.align); |
4342 | 615 | off = ext->kcfg.data_off + ext->kcfg.sz; |
4343 | 615 | pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n", |
4344 | 615 | i, ext->sym_idx, ext->kcfg.data_off, ext->name); |
4345 | 615 | } |
4346 | 184 | sec->size = off; |
4347 | 184 | n = btf_vlen(sec); |
4348 | 234 | for (i = 0; i < n; i++) { |
4349 | 219 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4350 | | |
4351 | 219 | t = btf__type_by_id(obj->btf, vs->type); |
4352 | 219 | ext_name = btf__name_by_offset(obj->btf, t->name_off); |
4353 | 219 | ext = find_extern_by_name(obj, ext_name); |
4354 | 219 | if (!ext) { |
4355 | 169 | pr_warn("failed to find extern definition for BTF var '%s'\n", |
4356 | 169 | ext_name); |
4357 | 169 | return -ESRCH; |
4358 | 169 | } |
4359 | 50 | btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4360 | 50 | vs->offset = ext->kcfg.data_off; |
4361 | 50 | } |
4362 | 184 | } |
4363 | 63 | return 0; |
4364 | 232 | } |
4365 | | |
4366 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog) |
4367 | 13.4k | { |
4368 | 13.4k | return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; |
4369 | 13.4k | } |
4370 | | |
4371 | | struct bpf_program * |
4372 | | bpf_object__find_program_by_name(const struct bpf_object *obj, |
4373 | | const char *name) |
4374 | 0 | { |
4375 | 0 | struct bpf_program *prog; |
4376 | |
|
4377 | 0 | bpf_object__for_each_program(prog, obj) { |
4378 | 0 | if (prog_is_subprog(obj, prog)) |
4379 | 0 | continue; |
4380 | 0 | if (!strcmp(prog->name, name)) |
4381 | 0 | return prog; |
4382 | 0 | } |
4383 | 0 | return errno = ENOENT, NULL; |
4384 | 0 | } |
4385 | | |
4386 | | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
4387 | | int shndx) |
4388 | 401 | { |
4389 | 401 | switch (obj->efile.secs[shndx].sec_type) { |
4390 | 90 | case SEC_BSS: |
4391 | 276 | case SEC_DATA: |
4392 | 399 | case SEC_RODATA: |
4393 | 399 | return true; |
4394 | 2 | default: |
4395 | 2 | return false; |
4396 | 401 | } |
4397 | 401 | } |
4398 | | |
4399 | | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
4400 | | int shndx) |
4401 | 27 | { |
4402 | 27 | return shndx == obj->efile.btf_maps_shndx; |
4403 | 27 | } |
4404 | | |
4405 | | static enum libbpf_map_type |
4406 | | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
4407 | 499 | { |
4408 | 499 | if (shndx == obj->efile.symbols_shndx) |
4409 | 2 | return LIBBPF_MAP_KCONFIG; |
4410 | | |
4411 | 497 | switch (obj->efile.secs[shndx].sec_type) { |
4412 | 90 | case SEC_BSS: |
4413 | 90 | return LIBBPF_MAP_BSS; |
4414 | 186 | case SEC_DATA: |
4415 | 186 | return LIBBPF_MAP_DATA; |
4416 | 123 | case SEC_RODATA: |
4417 | 123 | return LIBBPF_MAP_RODATA; |
4418 | 98 | default: |
4419 | 98 | return LIBBPF_MAP_UNSPEC; |
4420 | 497 | } |
4421 | 497 | } |
4422 | | |
4423 | | static int bpf_program__record_reloc(struct bpf_program *prog, |
4424 | | struct reloc_desc *reloc_desc, |
4425 | | __u32 insn_idx, const char *sym_name, |
4426 | | const Elf64_Sym *sym, const Elf64_Rel *rel) |
4427 | 1.78k | { |
4428 | 1.78k | struct bpf_insn *insn = &prog->insns[insn_idx]; |
4429 | 1.78k | size_t map_idx, nr_maps = prog->obj->nr_maps; |
4430 | 1.78k | struct bpf_object *obj = prog->obj; |
4431 | 1.78k | __u32 shdr_idx = sym->st_shndx; |
4432 | 1.78k | enum libbpf_map_type type; |
4433 | 1.78k | const char *sym_sec_name; |
4434 | 1.78k | struct bpf_map *map; |
4435 | | |
4436 | 1.78k | if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) { |
4437 | 38 | pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n", |
4438 | 38 | prog->name, sym_name, insn_idx, insn->code); |
4439 | 38 | return -LIBBPF_ERRNO__RELOC; |
4440 | 38 | } |
4441 | | |
4442 | 1.74k | if (sym_is_extern(sym)) { |
4443 | 1 | int sym_idx = ELF64_R_SYM(rel->r_info); |
4444 | 1 | int i, n = obj->nr_extern; |
4445 | 1 | struct extern_desc *ext; |
4446 | | |
4447 | 1 | for (i = 0; i < n; i++) { |
4448 | 0 | ext = &obj->externs[i]; |
4449 | 0 | if (ext->sym_idx == sym_idx) |
4450 | 0 | break; |
4451 | 0 | } |
4452 | 1 | if (i >= n) { |
4453 | 1 | pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n", |
4454 | 1 | prog->name, sym_name, sym_idx); |
4455 | 1 | return -LIBBPF_ERRNO__RELOC; |
4456 | 1 | } |
4457 | 0 | pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n", |
4458 | 0 | prog->name, i, ext->name, ext->sym_idx, insn_idx); |
4459 | 0 | if (insn->code == (BPF_JMP | BPF_CALL)) |
4460 | 0 | reloc_desc->type = RELO_EXTERN_CALL; |
4461 | 0 | else |
4462 | 0 | reloc_desc->type = RELO_EXTERN_LD64; |
4463 | 0 | reloc_desc->insn_idx = insn_idx; |
4464 | 0 | reloc_desc->ext_idx = i; |
4465 | 0 | return 0; |
4466 | 1 | } |
4467 | | |
4468 | | /* sub-program call relocation */ |
4469 | 1.74k | if (is_call_insn(insn)) { |
4470 | 298 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
4471 | 3 | pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); |
4472 | 3 | return -LIBBPF_ERRNO__RELOC; |
4473 | 3 | } |
4474 | | /* text_shndx can be 0, if no default "main" program exists */ |
4475 | 295 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
4476 | 10 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4477 | 10 | pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n", |
4478 | 10 | prog->name, sym_name, sym_sec_name); |
4479 | 10 | return -LIBBPF_ERRNO__RELOC; |
4480 | 10 | } |
4481 | 285 | if (sym->st_value % BPF_INSN_SZ) { |
4482 | 3 | pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n", |
4483 | 3 | prog->name, sym_name, (size_t)sym->st_value); |
4484 | 3 | return -LIBBPF_ERRNO__RELOC; |
4485 | 3 | } |
4486 | 282 | reloc_desc->type = RELO_CALL; |
4487 | 282 | reloc_desc->insn_idx = insn_idx; |
4488 | 282 | reloc_desc->sym_off = sym->st_value; |
4489 | 282 | return 0; |
4490 | 285 | } |
4491 | | |
4492 | 1.44k | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
4493 | 4 | pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n", |
4494 | 4 | prog->name, sym_name, shdr_idx); |
4495 | 4 | return -LIBBPF_ERRNO__RELOC; |
4496 | 4 | } |
4497 | | |
4498 | | /* loading subprog addresses */ |
4499 | 1.43k | if (sym_is_subprog(sym, obj->efile.text_shndx)) { |
4500 | | /* global_func: sym->st_value = offset in the section, insn->imm = 0. |
4501 | | * local_func: sym->st_value = 0, insn->imm = offset in the section. |
4502 | | */ |
4503 | 940 | if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { |
4504 | 6 | pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n", |
4505 | 6 | prog->name, sym_name, (size_t)sym->st_value, insn->imm); |
4506 | 6 | return -LIBBPF_ERRNO__RELOC; |
4507 | 6 | } |
4508 | | |
4509 | 934 | reloc_desc->type = RELO_SUBPROG_ADDR; |
4510 | 934 | reloc_desc->insn_idx = insn_idx; |
4511 | 934 | reloc_desc->sym_off = sym->st_value; |
4512 | 934 | return 0; |
4513 | 940 | } |
4514 | | |
4515 | 499 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
4516 | 499 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4517 | | |
4518 | | /* arena data relocation */ |
4519 | 499 | if (shdr_idx == obj->efile.arena_data_shndx) { |
4520 | 71 | reloc_desc->type = RELO_DATA; |
4521 | 71 | reloc_desc->insn_idx = insn_idx; |
4522 | 71 | reloc_desc->map_idx = obj->arena_map - obj->maps; |
4523 | 71 | reloc_desc->sym_off = sym->st_value; |
4524 | 71 | return 0; |
4525 | 71 | } |
4526 | | |
4527 | | /* generic map reference relocation */ |
4528 | 428 | if (type == LIBBPF_MAP_UNSPEC) { |
4529 | 27 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
4530 | 27 | pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n", |
4531 | 27 | prog->name, sym_name, sym_sec_name); |
4532 | 27 | return -LIBBPF_ERRNO__RELOC; |
4533 | 27 | } |
4534 | 0 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4535 | 0 | map = &obj->maps[map_idx]; |
4536 | 0 | if (map->libbpf_type != type || |
4537 | 0 | map->sec_idx != sym->st_shndx || |
4538 | 0 | map->sec_offset != sym->st_value) |
4539 | 0 | continue; |
4540 | 0 | pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n", |
4541 | 0 | prog->name, map_idx, map->name, map->sec_idx, |
4542 | 0 | map->sec_offset, insn_idx); |
4543 | 0 | break; |
4544 | 0 | } |
4545 | 0 | if (map_idx >= nr_maps) { |
4546 | 0 | pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n", |
4547 | 0 | prog->name, sym_sec_name, (size_t)sym->st_value); |
4548 | 0 | return -LIBBPF_ERRNO__RELOC; |
4549 | 0 | } |
4550 | 0 | reloc_desc->type = RELO_LD64; |
4551 | 0 | reloc_desc->insn_idx = insn_idx; |
4552 | 0 | reloc_desc->map_idx = map_idx; |
4553 | 0 | reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ |
4554 | 0 | return 0; |
4555 | 0 | } |
4556 | | |
4557 | | /* global data map relocation */ |
4558 | 401 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
4559 | 2 | pr_warn("prog '%s': bad data relo against section '%s'\n", |
4560 | 2 | prog->name, sym_sec_name); |
4561 | 2 | return -LIBBPF_ERRNO__RELOC; |
4562 | 2 | } |
4563 | 582 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4564 | 580 | map = &obj->maps[map_idx]; |
4565 | 580 | if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) |
4566 | 183 | continue; |
4567 | 397 | pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n", |
4568 | 397 | prog->name, map_idx, map->name, map->sec_idx, |
4569 | 397 | map->sec_offset, insn_idx); |
4570 | 397 | break; |
4571 | 580 | } |
4572 | 399 | if (map_idx >= nr_maps) { |
4573 | 2 | pr_warn("prog '%s': data relo failed to find map for section '%s'\n", |
4574 | 2 | prog->name, sym_sec_name); |
4575 | 2 | return -LIBBPF_ERRNO__RELOC; |
4576 | 2 | } |
4577 | | |
4578 | 397 | reloc_desc->type = RELO_DATA; |
4579 | 397 | reloc_desc->insn_idx = insn_idx; |
4580 | 397 | reloc_desc->map_idx = map_idx; |
4581 | 397 | reloc_desc->sym_off = sym->st_value; |
4582 | 397 | return 0; |
4583 | 399 | } |
4584 | | |
4585 | | static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx) |
4586 | 2.50k | { |
4587 | 2.50k | return insn_idx >= prog->sec_insn_off && |
4588 | 2.50k | insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; |
4589 | 2.50k | } |
4590 | | |
4591 | | static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj, |
4592 | | size_t sec_idx, size_t insn_idx) |
4593 | 7.38k | { |
4594 | 7.38k | int l = 0, r = obj->nr_programs - 1, m; |
4595 | 7.38k | struct bpf_program *prog; |
4596 | | |
4597 | 7.38k | if (!obj->nr_programs) |
4598 | 4.48k | return NULL; |
4599 | | |
4600 | 4.35k | while (l < r) { |
4601 | 1.46k | m = l + (r - l + 1) / 2; |
4602 | 1.46k | prog = &obj->programs[m]; |
4603 | | |
4604 | 1.46k | if (prog->sec_idx < sec_idx || |
4605 | 1.46k | (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) |
4606 | 577 | l = m; |
4607 | 883 | else |
4608 | 883 | r = m - 1; |
4609 | 1.46k | } |
4610 | | /* matching program could be at index l, but it still might be the |
4611 | | * wrong one, so we need to double check conditions for the last time |
4612 | | */ |
4613 | 2.89k | prog = &obj->programs[l]; |
4614 | 2.89k | if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) |
4615 | 1.78k | return prog; |
4616 | 1.11k | return NULL; |
4617 | 2.89k | } |
4618 | | |
4619 | | static int |
4620 | | bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data) |
4621 | 860 | { |
4622 | 860 | const char *relo_sec_name, *sec_name; |
4623 | 860 | size_t sec_idx = shdr->sh_info, sym_idx; |
4624 | 860 | struct bpf_program *prog; |
4625 | 860 | struct reloc_desc *relos; |
4626 | 860 | int err, i, nrels; |
4627 | 860 | const char *sym_name; |
4628 | 860 | __u32 insn_idx; |
4629 | 860 | Elf_Scn *scn; |
4630 | 860 | Elf_Data *scn_data; |
4631 | 860 | Elf64_Sym *sym; |
4632 | 860 | Elf64_Rel *rel; |
4633 | | |
4634 | 860 | if (sec_idx >= obj->efile.sec_cnt) |
4635 | 0 | return -EINVAL; |
4636 | | |
4637 | 860 | scn = elf_sec_by_idx(obj, sec_idx); |
4638 | 860 | scn_data = elf_sec_data(obj, scn); |
4639 | 860 | if (!scn_data) |
4640 | 16 | return -LIBBPF_ERRNO__FORMAT; |
4641 | | |
4642 | 844 | relo_sec_name = elf_sec_str(obj, shdr->sh_name); |
4643 | 844 | sec_name = elf_sec_name(obj, scn); |
4644 | 844 | if (!relo_sec_name || !sec_name) |
4645 | 20 | return -EINVAL; |
4646 | | |
4647 | 824 | pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n", |
4648 | 824 | relo_sec_name, sec_idx, sec_name); |
4649 | 824 | nrels = shdr->sh_size / shdr->sh_entsize; |
4650 | | |
4651 | 7.66k | for (i = 0; i < nrels; i++) { |
4652 | 7.66k | rel = elf_rel_by_idx(data, i); |
4653 | 7.66k | if (!rel) { |
4654 | 0 | pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i); |
4655 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4656 | 0 | } |
4657 | | |
4658 | 7.66k | sym_idx = ELF64_R_SYM(rel->r_info); |
4659 | 7.66k | sym = elf_sym_by_idx(obj, sym_idx); |
4660 | 7.66k | if (!sym) { |
4661 | 126 | pr_warn("sec '%s': symbol #%zu not found for relo #%d\n", |
4662 | 126 | relo_sec_name, sym_idx, i); |
4663 | 126 | return -LIBBPF_ERRNO__FORMAT; |
4664 | 126 | } |
4665 | | |
4666 | 7.53k | if (sym->st_shndx >= obj->efile.sec_cnt) { |
4667 | 28 | pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n", |
4668 | 28 | relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); |
4669 | 28 | return -LIBBPF_ERRNO__FORMAT; |
4670 | 28 | } |
4671 | | |
4672 | 7.51k | if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { |
4673 | 127 | pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n", |
4674 | 127 | relo_sec_name, (size_t)rel->r_offset, i); |
4675 | 127 | return -LIBBPF_ERRNO__FORMAT; |
4676 | 127 | } |
4677 | | |
4678 | 7.38k | insn_idx = rel->r_offset / BPF_INSN_SZ; |
4679 | | /* relocations against static functions are recorded as |
4680 | | * relocations against the section that contains a function; |
4681 | | * in such case, symbol will be STT_SECTION and sym.st_name |
4682 | | * will point to empty string (0), so fetch section name |
4683 | | * instead |
4684 | | */ |
4685 | 7.38k | if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) |
4686 | 544 | sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); |
4687 | 6.83k | else |
4688 | 6.83k | sym_name = elf_sym_str(obj, sym->st_name); |
4689 | 7.38k | sym_name = sym_name ?: "<?"; |
4690 | | |
4691 | 7.38k | pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n", |
4692 | 5.70k | relo_sec_name, i, insn_idx, sym_name); |
4693 | | |
4694 | 5.70k | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
4695 | 5.70k | if (!prog) { |
4696 | 5.60k | pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n", |
4697 | 5.60k | relo_sec_name, i, sec_name, insn_idx); |
4698 | 5.60k | continue; |
4699 | 5.60k | } |
4700 | | |
4701 | 103 | relos = libbpf_reallocarray(prog->reloc_desc, |
4702 | 103 | prog->nr_reloc + 1, sizeof(*relos)); |
4703 | 103 | if (!relos) |
4704 | 0 | return -ENOMEM; |
4705 | 103 | prog->reloc_desc = relos; |
4706 | | |
4707 | | /* adjust insn_idx to local BPF program frame of reference */ |
4708 | 103 | insn_idx -= prog->sec_insn_off; |
4709 | 103 | err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], |
4710 | 103 | insn_idx, sym_name, sym, rel); |
4711 | 103 | if (err) |
4712 | 96 | return err; |
4713 | | |
4714 | 7 | prog->nr_reloc++; |
4715 | 7 | } |
4716 | 18.4E | return 0; |
4717 | 824 | } |
4718 | | |
4719 | | static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map) |
4720 | 1.83k | { |
4721 | 1.83k | int id; |
4722 | | |
4723 | 1.83k | if (!obj->btf) |
4724 | 1.48k | return -ENOENT; |
4725 | | |
4726 | | /* if it's BTF-defined map, we don't need to search for type IDs. |
4727 | | * For struct_ops map, it does not need btf_key_type_id and |
4728 | | * btf_value_type_id. |
4729 | | */ |
4730 | 353 | if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) |
4731 | 71 | return 0; |
4732 | | |
4733 | | /* |
4734 | | * LLVM annotates global data differently in BTF, that is, |
4735 | | * only as '.data', '.bss' or '.rodata'. |
4736 | | */ |
4737 | 282 | if (!bpf_map__is_internal(map)) |
4738 | 0 | return -ENOENT; |
4739 | | |
4740 | 282 | id = btf__find_by_name(obj->btf, map->real_name); |
4741 | 282 | if (id < 0) |
4742 | 124 | return id; |
4743 | | |
4744 | 158 | map->btf_key_type_id = 0; |
4745 | 158 | map->btf_value_type_id = id; |
4746 | 158 | return 0; |
4747 | 282 | } |
4748 | | |
4749 | | static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) |
4750 | 0 | { |
4751 | 0 | char file[PATH_MAX], buff[4096]; |
4752 | 0 | FILE *fp; |
4753 | 0 | __u32 val; |
4754 | 0 | int err; |
4755 | |
|
4756 | 0 | snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd); |
4757 | 0 | memset(info, 0, sizeof(*info)); |
4758 | |
|
4759 | 0 | fp = fopen(file, "re"); |
4760 | 0 | if (!fp) { |
4761 | 0 | err = -errno; |
4762 | 0 | pr_warn("failed to open %s: %d. No procfs support?\n", file, |
4763 | 0 | err); |
4764 | 0 | return err; |
4765 | 0 | } |
4766 | | |
4767 | 0 | while (fgets(buff, sizeof(buff), fp)) { |
4768 | 0 | if (sscanf(buff, "map_type:\t%u", &val) == 1) |
4769 | 0 | info->type = val; |
4770 | 0 | else if (sscanf(buff, "key_size:\t%u", &val) == 1) |
4771 | 0 | info->key_size = val; |
4772 | 0 | else if (sscanf(buff, "value_size:\t%u", &val) == 1) |
4773 | 0 | info->value_size = val; |
4774 | 0 | else if (sscanf(buff, "max_entries:\t%u", &val) == 1) |
4775 | 0 | info->max_entries = val; |
4776 | 0 | else if (sscanf(buff, "map_flags:\t%i", &val) == 1) |
4777 | 0 | info->map_flags = val; |
4778 | 0 | } |
4779 | |
|
4780 | 0 | fclose(fp); |
4781 | |
|
4782 | 0 | return 0; |
4783 | 0 | } |
4784 | | |
4785 | | bool bpf_map__autocreate(const struct bpf_map *map) |
4786 | 0 | { |
4787 | 0 | return map->autocreate; |
4788 | 0 | } |
4789 | | |
4790 | | int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) |
4791 | 0 | { |
4792 | 0 | if (map->obj->loaded) |
4793 | 0 | return libbpf_err(-EBUSY); |
4794 | | |
4795 | 0 | map->autocreate = autocreate; |
4796 | 0 | return 0; |
4797 | 0 | } |
4798 | | |
4799 | | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
4800 | 0 | { |
4801 | 0 | struct bpf_map_info info; |
4802 | 0 | __u32 len = sizeof(info), name_len; |
4803 | 0 | int new_fd, err; |
4804 | 0 | char *new_name; |
4805 | |
|
4806 | 0 | memset(&info, 0, len); |
4807 | 0 | err = bpf_map_get_info_by_fd(fd, &info, &len); |
4808 | 0 | if (err && errno == EINVAL) |
4809 | 0 | err = bpf_get_map_info_from_fdinfo(fd, &info); |
4810 | 0 | if (err) |
4811 | 0 | return libbpf_err(err); |
4812 | | |
4813 | 0 | name_len = strlen(info.name); |
4814 | 0 | if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) |
4815 | 0 | new_name = strdup(map->name); |
4816 | 0 | else |
4817 | 0 | new_name = strdup(info.name); |
4818 | |
|
4819 | 0 | if (!new_name) |
4820 | 0 | return libbpf_err(-errno); |
4821 | | |
4822 | | /* |
4823 | | * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. |
4824 | | * This is similar to what we do in ensure_good_fd(), but without |
4825 | | * closing original FD. |
4826 | | */ |
4827 | 0 | new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
4828 | 0 | if (new_fd < 0) { |
4829 | 0 | err = -errno; |
4830 | 0 | goto err_free_new_name; |
4831 | 0 | } |
4832 | | |
4833 | 0 | err = reuse_fd(map->fd, new_fd); |
4834 | 0 | if (err) |
4835 | 0 | goto err_free_new_name; |
4836 | | |
4837 | 0 | free(map->name); |
4838 | |
|
4839 | 0 | map->name = new_name; |
4840 | 0 | map->def.type = info.type; |
4841 | 0 | map->def.key_size = info.key_size; |
4842 | 0 | map->def.value_size = info.value_size; |
4843 | 0 | map->def.max_entries = info.max_entries; |
4844 | 0 | map->def.map_flags = info.map_flags; |
4845 | 0 | map->btf_key_type_id = info.btf_key_type_id; |
4846 | 0 | map->btf_value_type_id = info.btf_value_type_id; |
4847 | 0 | map->reused = true; |
4848 | 0 | map->map_extra = info.map_extra; |
4849 | |
|
4850 | 0 | return 0; |
4851 | | |
4852 | 0 | err_free_new_name: |
4853 | 0 | free(new_name); |
4854 | 0 | return libbpf_err(err); |
4855 | 0 | } |
4856 | | |
4857 | | __u32 bpf_map__max_entries(const struct bpf_map *map) |
4858 | 0 | { |
4859 | 0 | return map->def.max_entries; |
4860 | 0 | } |
4861 | | |
4862 | | struct bpf_map *bpf_map__inner_map(struct bpf_map *map) |
4863 | 0 | { |
4864 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) |
4865 | 0 | return errno = EINVAL, NULL; |
4866 | | |
4867 | 0 | return map->inner_map; |
4868 | 0 | } |
4869 | | |
4870 | | int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries) |
4871 | 0 | { |
4872 | 0 | if (map->obj->loaded) |
4873 | 0 | return libbpf_err(-EBUSY); |
4874 | | |
4875 | 0 | map->def.max_entries = max_entries; |
4876 | | |
4877 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
4878 | 0 | if (map_is_ringbuf(map)) |
4879 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
4880 | |
|
4881 | 0 | return 0; |
4882 | 0 | } |
4883 | | |
4884 | | static int bpf_object_prepare_token(struct bpf_object *obj) |
4885 | 0 | { |
4886 | 0 | const char *bpffs_path; |
4887 | 0 | int bpffs_fd = -1, token_fd, err; |
4888 | 0 | bool mandatory; |
4889 | 0 | enum libbpf_print_level level; |
4890 | | |
4891 | | /* token is explicitly prevented */ |
4892 | 0 | if (obj->token_path && obj->token_path[0] == '\0') { |
4893 | 0 | pr_debug("object '%s': token is prevented, skipping...\n", obj->name); |
4894 | 0 | return 0; |
4895 | 0 | } |
4896 | | |
4897 | 0 | mandatory = obj->token_path != NULL; |
4898 | 0 | level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG; |
4899 | |
|
4900 | 0 | bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH; |
4901 | 0 | bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR); |
4902 | 0 | if (bpffs_fd < 0) { |
4903 | 0 | err = -errno; |
4904 | 0 | __pr(level, "object '%s': failed (%d) to open BPF FS mount at '%s'%s\n", |
4905 | 0 | obj->name, err, bpffs_path, |
4906 | 0 | mandatory ? "" : ", skipping optional step..."); |
4907 | 0 | return mandatory ? err : 0; |
4908 | 0 | } |
4909 | | |
4910 | 0 | token_fd = bpf_token_create(bpffs_fd, 0); |
4911 | 0 | close(bpffs_fd); |
4912 | 0 | if (token_fd < 0) { |
4913 | 0 | if (!mandatory && token_fd == -ENOENT) { |
4914 | 0 | pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n", |
4915 | 0 | obj->name, bpffs_path); |
4916 | 0 | return 0; |
4917 | 0 | } |
4918 | 0 | __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n", |
4919 | 0 | obj->name, token_fd, bpffs_path, |
4920 | 0 | mandatory ? "" : ", skipping optional step..."); |
4921 | 0 | return mandatory ? token_fd : 0; |
4922 | 0 | } |
4923 | | |
4924 | 0 | obj->feat_cache = calloc(1, sizeof(*obj->feat_cache)); |
4925 | 0 | if (!obj->feat_cache) { |
4926 | 0 | close(token_fd); |
4927 | 0 | return -ENOMEM; |
4928 | 0 | } |
4929 | | |
4930 | 0 | obj->token_fd = token_fd; |
4931 | 0 | obj->feat_cache->token_fd = token_fd; |
4932 | |
|
4933 | 0 | return 0; |
4934 | 0 | } |
4935 | | |
4936 | | static int |
4937 | | bpf_object__probe_loading(struct bpf_object *obj) |
4938 | 0 | { |
4939 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
4940 | 0 | struct bpf_insn insns[] = { |
4941 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4942 | 0 | BPF_EXIT_INSN(), |
4943 | 0 | }; |
4944 | 0 | int ret, insn_cnt = ARRAY_SIZE(insns); |
4945 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, opts, |
4946 | 0 | .token_fd = obj->token_fd, |
4947 | 0 | .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0, |
4948 | 0 | ); |
4949 | |
|
4950 | 0 | if (obj->gen_loader) |
4951 | 0 | return 0; |
4952 | | |
4953 | 0 | ret = bump_rlimit_memlock(); |
4954 | 0 | if (ret) |
4955 | 0 | pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret); |
4956 | | |
4957 | | /* make sure basic loading works */ |
4958 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &opts); |
4959 | 0 | if (ret < 0) |
4960 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts); |
4961 | 0 | if (ret < 0) { |
4962 | 0 | ret = errno; |
4963 | 0 | cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); |
4964 | 0 | pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF " |
4965 | 0 | "program. Make sure your kernel supports BPF " |
4966 | 0 | "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is " |
4967 | 0 | "set to big enough value.\n", __func__, cp, ret); |
4968 | 0 | return -ret; |
4969 | 0 | } |
4970 | 0 | close(ret); |
4971 | |
|
4972 | 0 | return 0; |
4973 | 0 | } |
4974 | | |
4975 | | bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) |
4976 | 0 | { |
4977 | 0 | if (obj->gen_loader) |
4978 | | /* To generate loader program assume the latest kernel |
4979 | | * to avoid doing extra prog_load, map_create syscalls. |
4980 | | */ |
4981 | 0 | return true; |
4982 | | |
4983 | 0 | if (obj->token_fd) |
4984 | 0 | return feat_supported(obj->feat_cache, feat_id); |
4985 | | |
4986 | 0 | return feat_supported(NULL, feat_id); |
4987 | 0 | } |
4988 | | |
4989 | | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
4990 | 0 | { |
4991 | 0 | struct bpf_map_info map_info; |
4992 | 0 | char msg[STRERR_BUFSIZE]; |
4993 | 0 | __u32 map_info_len = sizeof(map_info); |
4994 | 0 | int err; |
4995 | |
|
4996 | 0 | memset(&map_info, 0, map_info_len); |
4997 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len); |
4998 | 0 | if (err && errno == EINVAL) |
4999 | 0 | err = bpf_get_map_info_from_fdinfo(map_fd, &map_info); |
5000 | 0 | if (err) { |
5001 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", map_fd, |
5002 | 0 | libbpf_strerror_r(errno, msg, sizeof(msg))); |
5003 | 0 | return false; |
5004 | 0 | } |
5005 | | |
5006 | 0 | return (map_info.type == map->def.type && |
5007 | 0 | map_info.key_size == map->def.key_size && |
5008 | 0 | map_info.value_size == map->def.value_size && |
5009 | 0 | map_info.max_entries == map->def.max_entries && |
5010 | 0 | map_info.map_flags == map->def.map_flags && |
5011 | 0 | map_info.map_extra == map->map_extra); |
5012 | 0 | } |
5013 | | |
5014 | | static int |
5015 | | bpf_object__reuse_map(struct bpf_map *map) |
5016 | 0 | { |
5017 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5018 | 0 | int err, pin_fd; |
5019 | |
|
5020 | 0 | pin_fd = bpf_obj_get(map->pin_path); |
5021 | 0 | if (pin_fd < 0) { |
5022 | 0 | err = -errno; |
5023 | 0 | if (err == -ENOENT) { |
5024 | 0 | pr_debug("found no pinned map to reuse at '%s'\n", |
5025 | 0 | map->pin_path); |
5026 | 0 | return 0; |
5027 | 0 | } |
5028 | | |
5029 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
5030 | 0 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
5031 | 0 | map->pin_path, cp); |
5032 | 0 | return err; |
5033 | 0 | } |
5034 | | |
5035 | 0 | if (!map_is_reuse_compat(map, pin_fd)) { |
5036 | 0 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
5037 | 0 | map->pin_path); |
5038 | 0 | close(pin_fd); |
5039 | 0 | return -EINVAL; |
5040 | 0 | } |
5041 | | |
5042 | 0 | err = bpf_map__reuse_fd(map, pin_fd); |
5043 | 0 | close(pin_fd); |
5044 | 0 | if (err) |
5045 | 0 | return err; |
5046 | | |
5047 | 0 | map->pinned = true; |
5048 | 0 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
5049 | |
|
5050 | 0 | return 0; |
5051 | 0 | } |
5052 | | |
5053 | | static int |
5054 | | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
5055 | 0 | { |
5056 | 0 | enum libbpf_map_type map_type = map->libbpf_type; |
5057 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5058 | 0 | int err, zero = 0; |
5059 | |
|
5060 | 0 | if (obj->gen_loader) { |
5061 | 0 | bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, |
5062 | 0 | map->mmaped, map->def.value_size); |
5063 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) |
5064 | 0 | bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); |
5065 | 0 | return 0; |
5066 | 0 | } |
5067 | | |
5068 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); |
5069 | 0 | if (err) { |
5070 | 0 | err = -errno; |
5071 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5072 | 0 | pr_warn("Error setting initial map(%s) contents: %s\n", |
5073 | 0 | map->name, cp); |
5074 | 0 | return err; |
5075 | 0 | } |
5076 | | |
5077 | | /* Freeze .rodata and .kconfig map as read-only from syscall side. */ |
5078 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) { |
5079 | 0 | err = bpf_map_freeze(map->fd); |
5080 | 0 | if (err) { |
5081 | 0 | err = -errno; |
5082 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5083 | 0 | pr_warn("Error freezing map(%s) as read-only: %s\n", |
5084 | 0 | map->name, cp); |
5085 | 0 | return err; |
5086 | 0 | } |
5087 | 0 | } |
5088 | 0 | return 0; |
5089 | 0 | } |
5090 | | |
5091 | | static void bpf_map__destroy(struct bpf_map *map); |
5092 | | |
5093 | | static bool map_is_created(const struct bpf_map *map) |
5094 | 0 | { |
5095 | 0 | return map->obj->loaded || map->reused; |
5096 | 0 | } |
5097 | | |
5098 | | static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner) |
5099 | 0 | { |
5100 | 0 | LIBBPF_OPTS(bpf_map_create_opts, create_attr); |
5101 | 0 | struct bpf_map_def *def = &map->def; |
5102 | 0 | const char *map_name = NULL; |
5103 | 0 | int err = 0, map_fd; |
5104 | |
|
5105 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
5106 | 0 | map_name = map->name; |
5107 | 0 | create_attr.map_ifindex = map->map_ifindex; |
5108 | 0 | create_attr.map_flags = def->map_flags; |
5109 | 0 | create_attr.numa_node = map->numa_node; |
5110 | 0 | create_attr.map_extra = map->map_extra; |
5111 | 0 | create_attr.token_fd = obj->token_fd; |
5112 | 0 | if (obj->token_fd) |
5113 | 0 | create_attr.map_flags |= BPF_F_TOKEN_FD; |
5114 | |
|
5115 | 0 | if (bpf_map__is_struct_ops(map)) { |
5116 | 0 | create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
5117 | 0 | if (map->mod_btf_fd >= 0) { |
5118 | 0 | create_attr.value_type_btf_obj_fd = map->mod_btf_fd; |
5119 | 0 | create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD; |
5120 | 0 | } |
5121 | 0 | } |
5122 | |
|
5123 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0) { |
5124 | 0 | create_attr.btf_fd = btf__fd(obj->btf); |
5125 | 0 | create_attr.btf_key_type_id = map->btf_key_type_id; |
5126 | 0 | create_attr.btf_value_type_id = map->btf_value_type_id; |
5127 | 0 | } |
5128 | |
|
5129 | 0 | if (bpf_map_type__is_map_in_map(def->type)) { |
5130 | 0 | if (map->inner_map) { |
5131 | 0 | err = map_set_def_max_entries(map->inner_map); |
5132 | 0 | if (err) |
5133 | 0 | return err; |
5134 | 0 | err = bpf_object__create_map(obj, map->inner_map, true); |
5135 | 0 | if (err) { |
5136 | 0 | pr_warn("map '%s': failed to create inner map: %d\n", |
5137 | 0 | map->name, err); |
5138 | 0 | return err; |
5139 | 0 | } |
5140 | 0 | map->inner_map_fd = map->inner_map->fd; |
5141 | 0 | } |
5142 | 0 | if (map->inner_map_fd >= 0) |
5143 | 0 | create_attr.inner_map_fd = map->inner_map_fd; |
5144 | 0 | } |
5145 | | |
5146 | 0 | switch (def->type) { |
5147 | 0 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
5148 | 0 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
5149 | 0 | case BPF_MAP_TYPE_STACK_TRACE: |
5150 | 0 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
5151 | 0 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
5152 | 0 | case BPF_MAP_TYPE_DEVMAP: |
5153 | 0 | case BPF_MAP_TYPE_DEVMAP_HASH: |
5154 | 0 | case BPF_MAP_TYPE_CPUMAP: |
5155 | 0 | case BPF_MAP_TYPE_XSKMAP: |
5156 | 0 | case BPF_MAP_TYPE_SOCKMAP: |
5157 | 0 | case BPF_MAP_TYPE_SOCKHASH: |
5158 | 0 | case BPF_MAP_TYPE_QUEUE: |
5159 | 0 | case BPF_MAP_TYPE_STACK: |
5160 | 0 | case BPF_MAP_TYPE_ARENA: |
5161 | 0 | create_attr.btf_fd = 0; |
5162 | 0 | create_attr.btf_key_type_id = 0; |
5163 | 0 | create_attr.btf_value_type_id = 0; |
5164 | 0 | map->btf_key_type_id = 0; |
5165 | 0 | map->btf_value_type_id = 0; |
5166 | 0 | break; |
5167 | 0 | case BPF_MAP_TYPE_STRUCT_OPS: |
5168 | 0 | create_attr.btf_value_type_id = 0; |
5169 | 0 | break; |
5170 | 0 | default: |
5171 | 0 | break; |
5172 | 0 | } |
5173 | | |
5174 | 0 | if (obj->gen_loader) { |
5175 | 0 | bpf_gen__map_create(obj->gen_loader, def->type, map_name, |
5176 | 0 | def->key_size, def->value_size, def->max_entries, |
5177 | 0 | &create_attr, is_inner ? -1 : map - obj->maps); |
5178 | | /* We keep pretenting we have valid FD to pass various fd >= 0 |
5179 | | * checks by just keeping original placeholder FDs in place. |
5180 | | * See bpf_object__add_map() comment. |
5181 | | * This placeholder fd will not be used with any syscall and |
5182 | | * will be reset to -1 eventually. |
5183 | | */ |
5184 | 0 | map_fd = map->fd; |
5185 | 0 | } else { |
5186 | 0 | map_fd = bpf_map_create(def->type, map_name, |
5187 | 0 | def->key_size, def->value_size, |
5188 | 0 | def->max_entries, &create_attr); |
5189 | 0 | } |
5190 | 0 | if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) { |
5191 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5192 | |
|
5193 | 0 | err = -errno; |
5194 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5195 | 0 | pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", |
5196 | 0 | map->name, cp, err); |
5197 | 0 | create_attr.btf_fd = 0; |
5198 | 0 | create_attr.btf_key_type_id = 0; |
5199 | 0 | create_attr.btf_value_type_id = 0; |
5200 | 0 | map->btf_key_type_id = 0; |
5201 | 0 | map->btf_value_type_id = 0; |
5202 | 0 | map_fd = bpf_map_create(def->type, map_name, |
5203 | 0 | def->key_size, def->value_size, |
5204 | 0 | def->max_entries, &create_attr); |
5205 | 0 | } |
5206 | |
|
5207 | 0 | if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { |
5208 | 0 | if (obj->gen_loader) |
5209 | 0 | map->inner_map->fd = -1; |
5210 | 0 | bpf_map__destroy(map->inner_map); |
5211 | 0 | zfree(&map->inner_map); |
5212 | 0 | } |
5213 | |
|
5214 | 0 | if (map_fd < 0) |
5215 | 0 | return map_fd; |
5216 | | |
5217 | | /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */ |
5218 | 0 | if (map->fd == map_fd) |
5219 | 0 | return 0; |
5220 | | |
5221 | | /* Keep placeholder FD value but now point it to the BPF map object. |
5222 | | * This way everything that relied on this map's FD (e.g., relocated |
5223 | | * ldimm64 instructions) will stay valid and won't need adjustments. |
5224 | | * map->fd stays valid but now point to what map_fd points to. |
5225 | | */ |
5226 | 0 | return reuse_fd(map->fd, map_fd); |
5227 | 0 | } |
5228 | | |
5229 | | static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map) |
5230 | 0 | { |
5231 | 0 | const struct bpf_map *targ_map; |
5232 | 0 | unsigned int i; |
5233 | 0 | int fd, err = 0; |
5234 | |
|
5235 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5236 | 0 | if (!map->init_slots[i]) |
5237 | 0 | continue; |
5238 | | |
5239 | 0 | targ_map = map->init_slots[i]; |
5240 | 0 | fd = targ_map->fd; |
5241 | |
|
5242 | 0 | if (obj->gen_loader) { |
5243 | 0 | bpf_gen__populate_outer_map(obj->gen_loader, |
5244 | 0 | map - obj->maps, i, |
5245 | 0 | targ_map - obj->maps); |
5246 | 0 | } else { |
5247 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5248 | 0 | } |
5249 | 0 | if (err) { |
5250 | 0 | err = -errno; |
5251 | 0 | pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", |
5252 | 0 | map->name, i, targ_map->name, fd, err); |
5253 | 0 | return err; |
5254 | 0 | } |
5255 | 0 | pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", |
5256 | 0 | map->name, i, targ_map->name, fd); |
5257 | 0 | } |
5258 | | |
5259 | 0 | zfree(&map->init_slots); |
5260 | 0 | map->init_slots_sz = 0; |
5261 | |
|
5262 | 0 | return 0; |
5263 | 0 | } |
5264 | | |
5265 | | static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map) |
5266 | 0 | { |
5267 | 0 | const struct bpf_program *targ_prog; |
5268 | 0 | unsigned int i; |
5269 | 0 | int fd, err; |
5270 | |
|
5271 | 0 | if (obj->gen_loader) |
5272 | 0 | return -ENOTSUP; |
5273 | | |
5274 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5275 | 0 | if (!map->init_slots[i]) |
5276 | 0 | continue; |
5277 | | |
5278 | 0 | targ_prog = map->init_slots[i]; |
5279 | 0 | fd = bpf_program__fd(targ_prog); |
5280 | |
|
5281 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5282 | 0 | if (err) { |
5283 | 0 | err = -errno; |
5284 | 0 | pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n", |
5285 | 0 | map->name, i, targ_prog->name, fd, err); |
5286 | 0 | return err; |
5287 | 0 | } |
5288 | 0 | pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n", |
5289 | 0 | map->name, i, targ_prog->name, fd); |
5290 | 0 | } |
5291 | | |
5292 | 0 | zfree(&map->init_slots); |
5293 | 0 | map->init_slots_sz = 0; |
5294 | |
|
5295 | 0 | return 0; |
5296 | 0 | } |
5297 | | |
5298 | | static int bpf_object_init_prog_arrays(struct bpf_object *obj) |
5299 | 0 | { |
5300 | 0 | struct bpf_map *map; |
5301 | 0 | int i, err; |
5302 | |
|
5303 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5304 | 0 | map = &obj->maps[i]; |
5305 | |
|
5306 | 0 | if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) |
5307 | 0 | continue; |
5308 | | |
5309 | 0 | err = init_prog_array_slots(obj, map); |
5310 | 0 | if (err < 0) |
5311 | 0 | return err; |
5312 | 0 | } |
5313 | 0 | return 0; |
5314 | 0 | } |
5315 | | |
5316 | | static int map_set_def_max_entries(struct bpf_map *map) |
5317 | 0 | { |
5318 | 0 | if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { |
5319 | 0 | int nr_cpus; |
5320 | |
|
5321 | 0 | nr_cpus = libbpf_num_possible_cpus(); |
5322 | 0 | if (nr_cpus < 0) { |
5323 | 0 | pr_warn("map '%s': failed to determine number of system CPUs: %d\n", |
5324 | 0 | map->name, nr_cpus); |
5325 | 0 | return nr_cpus; |
5326 | 0 | } |
5327 | 0 | pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); |
5328 | 0 | map->def.max_entries = nr_cpus; |
5329 | 0 | } |
5330 | | |
5331 | 0 | return 0; |
5332 | 0 | } |
5333 | | |
5334 | | static int |
5335 | | bpf_object__create_maps(struct bpf_object *obj) |
5336 | 0 | { |
5337 | 0 | struct bpf_map *map; |
5338 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5339 | 0 | unsigned int i, j; |
5340 | 0 | int err; |
5341 | 0 | bool retried; |
5342 | |
|
5343 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5344 | 0 | map = &obj->maps[i]; |
5345 | | |
5346 | | /* To support old kernels, we skip creating global data maps |
5347 | | * (.rodata, .data, .kconfig, etc); later on, during program |
5348 | | * loading, if we detect that at least one of the to-be-loaded |
5349 | | * programs is referencing any global data map, we'll error |
5350 | | * out with program name and relocation index logged. |
5351 | | * This approach allows to accommodate Clang emitting |
5352 | | * unnecessary .rodata.str1.1 sections for string literals, |
5353 | | * but also it allows to have CO-RE applications that use |
5354 | | * global variables in some of BPF programs, but not others. |
5355 | | * If those global variable-using programs are not loaded at |
5356 | | * runtime due to bpf_program__set_autoload(prog, false), |
5357 | | * bpf_object loading will succeed just fine even on old |
5358 | | * kernels. |
5359 | | */ |
5360 | 0 | if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA)) |
5361 | 0 | map->autocreate = false; |
5362 | |
|
5363 | 0 | if (!map->autocreate) { |
5364 | 0 | pr_debug("map '%s': skipped auto-creating...\n", map->name); |
5365 | 0 | continue; |
5366 | 0 | } |
5367 | | |
5368 | 0 | err = map_set_def_max_entries(map); |
5369 | 0 | if (err) |
5370 | 0 | goto err_out; |
5371 | | |
5372 | 0 | retried = false; |
5373 | 0 | retry: |
5374 | 0 | if (map->pin_path) { |
5375 | 0 | err = bpf_object__reuse_map(map); |
5376 | 0 | if (err) { |
5377 | 0 | pr_warn("map '%s': error reusing pinned map\n", |
5378 | 0 | map->name); |
5379 | 0 | goto err_out; |
5380 | 0 | } |
5381 | 0 | if (retried && map->fd < 0) { |
5382 | 0 | pr_warn("map '%s': cannot find pinned map\n", |
5383 | 0 | map->name); |
5384 | 0 | err = -ENOENT; |
5385 | 0 | goto err_out; |
5386 | 0 | } |
5387 | 0 | } |
5388 | | |
5389 | 0 | if (map->reused) { |
5390 | 0 | pr_debug("map '%s': skipping creation (preset fd=%d)\n", |
5391 | 0 | map->name, map->fd); |
5392 | 0 | } else { |
5393 | 0 | err = bpf_object__create_map(obj, map, false); |
5394 | 0 | if (err) |
5395 | 0 | goto err_out; |
5396 | | |
5397 | 0 | pr_debug("map '%s': created successfully, fd=%d\n", |
5398 | 0 | map->name, map->fd); |
5399 | |
|
5400 | 0 | if (bpf_map__is_internal(map)) { |
5401 | 0 | err = bpf_object__populate_internal_map(obj, map); |
5402 | 0 | if (err < 0) |
5403 | 0 | goto err_out; |
5404 | 0 | } |
5405 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) { |
5406 | 0 | map->mmaped = mmap((void *)(long)map->map_extra, |
5407 | 0 | bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE, |
5408 | 0 | map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED, |
5409 | 0 | map->fd, 0); |
5410 | 0 | if (map->mmaped == MAP_FAILED) { |
5411 | 0 | err = -errno; |
5412 | 0 | map->mmaped = NULL; |
5413 | 0 | pr_warn("map '%s': failed to mmap arena: %d\n", |
5414 | 0 | map->name, err); |
5415 | 0 | return err; |
5416 | 0 | } |
5417 | 0 | if (obj->arena_data) { |
5418 | 0 | memcpy(map->mmaped, obj->arena_data, obj->arena_data_sz); |
5419 | 0 | zfree(&obj->arena_data); |
5420 | 0 | } |
5421 | 0 | } |
5422 | 0 | if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { |
5423 | 0 | err = init_map_in_map_slots(obj, map); |
5424 | 0 | if (err < 0) |
5425 | 0 | goto err_out; |
5426 | 0 | } |
5427 | 0 | } |
5428 | | |
5429 | 0 | if (map->pin_path && !map->pinned) { |
5430 | 0 | err = bpf_map__pin(map, NULL); |
5431 | 0 | if (err) { |
5432 | 0 | if (!retried && err == -EEXIST) { |
5433 | 0 | retried = true; |
5434 | 0 | goto retry; |
5435 | 0 | } |
5436 | 0 | pr_warn("map '%s': failed to auto-pin at '%s': %d\n", |
5437 | 0 | map->name, map->pin_path, err); |
5438 | 0 | goto err_out; |
5439 | 0 | } |
5440 | 0 | } |
5441 | 0 | } |
5442 | | |
5443 | 0 | return 0; |
5444 | | |
5445 | 0 | err_out: |
5446 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5447 | 0 | pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err); |
5448 | 0 | pr_perm_msg(err); |
5449 | 0 | for (j = 0; j < i; j++) |
5450 | 0 | zclose(obj->maps[j].fd); |
5451 | 0 | return err; |
5452 | 0 | } |
5453 | | |
5454 | | static bool bpf_core_is_flavor_sep(const char *s) |
5455 | 29.2k | { |
5456 | | /* check X___Y name pattern, where X and Y are not underscores */ |
5457 | 29.2k | return s[0] != '_' && /* X */ |
5458 | 29.2k | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ |
5459 | 29.2k | s[4] != '_'; /* Y */ |
5460 | 29.2k | } |
5461 | | |
5462 | | /* Given 'some_struct_name___with_flavor' return the length of a name prefix |
5463 | | * before last triple underscore. Struct name part after last triple |
5464 | | * underscore is ignored by BPF CO-RE relocation during relocation matching. |
5465 | | */ |
5466 | | size_t bpf_core_essential_name_len(const char *name) |
5467 | 3.80k | { |
5468 | 3.80k | size_t n = strlen(name); |
5469 | 3.80k | int i; |
5470 | | |
5471 | 32.7k | for (i = n - 5; i >= 0; i--) { |
5472 | 29.2k | if (bpf_core_is_flavor_sep(name + i)) |
5473 | 246 | return i + 1; |
5474 | 29.2k | } |
5475 | 3.56k | return n; |
5476 | 3.80k | } |
5477 | | |
5478 | | void bpf_core_free_cands(struct bpf_core_cand_list *cands) |
5479 | 0 | { |
5480 | 0 | if (!cands) |
5481 | 0 | return; |
5482 | | |
5483 | 0 | free(cands->cands); |
5484 | 0 | free(cands); |
5485 | 0 | } |
5486 | | |
5487 | | int bpf_core_add_cands(struct bpf_core_cand *local_cand, |
5488 | | size_t local_essent_len, |
5489 | | const struct btf *targ_btf, |
5490 | | const char *targ_btf_name, |
5491 | | int targ_start_id, |
5492 | | struct bpf_core_cand_list *cands) |
5493 | 0 | { |
5494 | 0 | struct bpf_core_cand *new_cands, *cand; |
5495 | 0 | const struct btf_type *t, *local_t; |
5496 | 0 | const char *targ_name, *local_name; |
5497 | 0 | size_t targ_essent_len; |
5498 | 0 | int n, i; |
5499 | |
|
5500 | 0 | local_t = btf__type_by_id(local_cand->btf, local_cand->id); |
5501 | 0 | local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); |
5502 | |
|
5503 | 0 | n = btf__type_cnt(targ_btf); |
5504 | 0 | for (i = targ_start_id; i < n; i++) { |
5505 | 0 | t = btf__type_by_id(targ_btf, i); |
5506 | 0 | if (!btf_kind_core_compat(t, local_t)) |
5507 | 0 | continue; |
5508 | | |
5509 | 0 | targ_name = btf__name_by_offset(targ_btf, t->name_off); |
5510 | 0 | if (str_is_empty(targ_name)) |
5511 | 0 | continue; |
5512 | | |
5513 | 0 | targ_essent_len = bpf_core_essential_name_len(targ_name); |
5514 | 0 | if (targ_essent_len != local_essent_len) |
5515 | 0 | continue; |
5516 | | |
5517 | 0 | if (strncmp(local_name, targ_name, local_essent_len) != 0) |
5518 | 0 | continue; |
5519 | | |
5520 | 0 | pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n", |
5521 | 0 | local_cand->id, btf_kind_str(local_t), |
5522 | 0 | local_name, i, btf_kind_str(t), targ_name, |
5523 | 0 | targ_btf_name); |
5524 | 0 | new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, |
5525 | 0 | sizeof(*cands->cands)); |
5526 | 0 | if (!new_cands) |
5527 | 0 | return -ENOMEM; |
5528 | | |
5529 | 0 | cand = &new_cands[cands->len]; |
5530 | 0 | cand->btf = targ_btf; |
5531 | 0 | cand->id = i; |
5532 | |
|
5533 | 0 | cands->cands = new_cands; |
5534 | 0 | cands->len++; |
5535 | 0 | } |
5536 | 0 | return 0; |
5537 | 0 | } |
5538 | | |
5539 | | static int load_module_btfs(struct bpf_object *obj) |
5540 | 0 | { |
5541 | 0 | struct bpf_btf_info info; |
5542 | 0 | struct module_btf *mod_btf; |
5543 | 0 | struct btf *btf; |
5544 | 0 | char name[64]; |
5545 | 0 | __u32 id = 0, len; |
5546 | 0 | int err, fd; |
5547 | |
|
5548 | 0 | if (obj->btf_modules_loaded) |
5549 | 0 | return 0; |
5550 | | |
5551 | 0 | if (obj->gen_loader) |
5552 | 0 | return 0; |
5553 | | |
5554 | | /* don't do this again, even if we find no module BTFs */ |
5555 | 0 | obj->btf_modules_loaded = true; |
5556 | | |
5557 | | /* kernel too old to support module BTFs */ |
5558 | 0 | if (!kernel_supports(obj, FEAT_MODULE_BTF)) |
5559 | 0 | return 0; |
5560 | | |
5561 | 0 | while (true) { |
5562 | 0 | err = bpf_btf_get_next_id(id, &id); |
5563 | 0 | if (err && errno == ENOENT) |
5564 | 0 | return 0; |
5565 | 0 | if (err && errno == EPERM) { |
5566 | 0 | pr_debug("skipping module BTFs loading, missing privileges\n"); |
5567 | 0 | return 0; |
5568 | 0 | } |
5569 | 0 | if (err) { |
5570 | 0 | err = -errno; |
5571 | 0 | pr_warn("failed to iterate BTF objects: %d\n", err); |
5572 | 0 | return err; |
5573 | 0 | } |
5574 | | |
5575 | 0 | fd = bpf_btf_get_fd_by_id(id); |
5576 | 0 | if (fd < 0) { |
5577 | 0 | if (errno == ENOENT) |
5578 | 0 | continue; /* expected race: BTF was unloaded */ |
5579 | 0 | err = -errno; |
5580 | 0 | pr_warn("failed to get BTF object #%d FD: %d\n", id, err); |
5581 | 0 | return err; |
5582 | 0 | } |
5583 | | |
5584 | 0 | len = sizeof(info); |
5585 | 0 | memset(&info, 0, sizeof(info)); |
5586 | 0 | info.name = ptr_to_u64(name); |
5587 | 0 | info.name_len = sizeof(name); |
5588 | |
|
5589 | 0 | err = bpf_btf_get_info_by_fd(fd, &info, &len); |
5590 | 0 | if (err) { |
5591 | 0 | err = -errno; |
5592 | 0 | pr_warn("failed to get BTF object #%d info: %d\n", id, err); |
5593 | 0 | goto err_out; |
5594 | 0 | } |
5595 | | |
5596 | | /* ignore non-module BTFs */ |
5597 | 0 | if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) { |
5598 | 0 | close(fd); |
5599 | 0 | continue; |
5600 | 0 | } |
5601 | | |
5602 | 0 | btf = btf_get_from_fd(fd, obj->btf_vmlinux); |
5603 | 0 | err = libbpf_get_error(btf); |
5604 | 0 | if (err) { |
5605 | 0 | pr_warn("failed to load module [%s]'s BTF object #%d: %d\n", |
5606 | 0 | name, id, err); |
5607 | 0 | goto err_out; |
5608 | 0 | } |
5609 | | |
5610 | 0 | err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, |
5611 | 0 | sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); |
5612 | 0 | if (err) |
5613 | 0 | goto err_out; |
5614 | | |
5615 | 0 | mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; |
5616 | |
|
5617 | 0 | mod_btf->btf = btf; |
5618 | 0 | mod_btf->id = id; |
5619 | 0 | mod_btf->fd = fd; |
5620 | 0 | mod_btf->name = strdup(name); |
5621 | 0 | if (!mod_btf->name) { |
5622 | 0 | err = -ENOMEM; |
5623 | 0 | goto err_out; |
5624 | 0 | } |
5625 | 0 | continue; |
5626 | | |
5627 | 0 | err_out: |
5628 | 0 | close(fd); |
5629 | 0 | return err; |
5630 | 0 | } |
5631 | | |
5632 | 0 | return 0; |
5633 | 0 | } |
5634 | | |
5635 | | static struct bpf_core_cand_list * |
5636 | | bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id) |
5637 | 0 | { |
5638 | 0 | struct bpf_core_cand local_cand = {}; |
5639 | 0 | struct bpf_core_cand_list *cands; |
5640 | 0 | const struct btf *main_btf; |
5641 | 0 | const struct btf_type *local_t; |
5642 | 0 | const char *local_name; |
5643 | 0 | size_t local_essent_len; |
5644 | 0 | int err, i; |
5645 | |
|
5646 | 0 | local_cand.btf = local_btf; |
5647 | 0 | local_cand.id = local_type_id; |
5648 | 0 | local_t = btf__type_by_id(local_btf, local_type_id); |
5649 | 0 | if (!local_t) |
5650 | 0 | return ERR_PTR(-EINVAL); |
5651 | | |
5652 | 0 | local_name = btf__name_by_offset(local_btf, local_t->name_off); |
5653 | 0 | if (str_is_empty(local_name)) |
5654 | 0 | return ERR_PTR(-EINVAL); |
5655 | 0 | local_essent_len = bpf_core_essential_name_len(local_name); |
5656 | |
|
5657 | 0 | cands = calloc(1, sizeof(*cands)); |
5658 | 0 | if (!cands) |
5659 | 0 | return ERR_PTR(-ENOMEM); |
5660 | | |
5661 | | /* Attempt to find target candidates in vmlinux BTF first */ |
5662 | 0 | main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; |
5663 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands); |
5664 | 0 | if (err) |
5665 | 0 | goto err_out; |
5666 | | |
5667 | | /* if vmlinux BTF has any candidate, don't got for module BTFs */ |
5668 | 0 | if (cands->len) |
5669 | 0 | return cands; |
5670 | | |
5671 | | /* if vmlinux BTF was overridden, don't attempt to load module BTFs */ |
5672 | 0 | if (obj->btf_vmlinux_override) |
5673 | 0 | return cands; |
5674 | | |
5675 | | /* now look through module BTFs, trying to still find candidates */ |
5676 | 0 | err = load_module_btfs(obj); |
5677 | 0 | if (err) |
5678 | 0 | goto err_out; |
5679 | | |
5680 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
5681 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, |
5682 | 0 | obj->btf_modules[i].btf, |
5683 | 0 | obj->btf_modules[i].name, |
5684 | 0 | btf__type_cnt(obj->btf_vmlinux), |
5685 | 0 | cands); |
5686 | 0 | if (err) |
5687 | 0 | goto err_out; |
5688 | 0 | } |
5689 | | |
5690 | 0 | return cands; |
5691 | 0 | err_out: |
5692 | 0 | bpf_core_free_cands(cands); |
5693 | 0 | return ERR_PTR(err); |
5694 | 0 | } |
5695 | | |
5696 | | /* Check local and target types for compatibility. This check is used for |
5697 | | * type-based CO-RE relocations and follow slightly different rules than |
5698 | | * field-based relocations. This function assumes that root types were already |
5699 | | * checked for name match. Beyond that initial root-level name check, names |
5700 | | * are completely ignored. Compatibility rules are as follows: |
5701 | | * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but |
5702 | | * kind should match for local and target types (i.e., STRUCT is not |
5703 | | * compatible with UNION); |
5704 | | * - for ENUMs, the size is ignored; |
5705 | | * - for INT, size and signedness are ignored; |
5706 | | * - for ARRAY, dimensionality is ignored, element types are checked for |
5707 | | * compatibility recursively; |
5708 | | * - CONST/VOLATILE/RESTRICT modifiers are ignored; |
5709 | | * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; |
5710 | | * - FUNC_PROTOs are compatible if they have compatible signature: same |
5711 | | * number of input args and compatible return and argument types. |
5712 | | * These rules are not set in stone and probably will be adjusted as we get |
5713 | | * more experience with using BPF CO-RE relocations. |
5714 | | */ |
5715 | | int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, |
5716 | | const struct btf *targ_btf, __u32 targ_id) |
5717 | 0 | { |
5718 | 0 | return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32); |
5719 | 0 | } |
5720 | | |
5721 | | int bpf_core_types_match(const struct btf *local_btf, __u32 local_id, |
5722 | | const struct btf *targ_btf, __u32 targ_id) |
5723 | 0 | { |
5724 | 0 | return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32); |
5725 | 0 | } |
5726 | | |
5727 | | static size_t bpf_core_hash_fn(const long key, void *ctx) |
5728 | 0 | { |
5729 | 0 | return key; |
5730 | 0 | } |
5731 | | |
5732 | | static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx) |
5733 | 0 | { |
5734 | 0 | return k1 == k2; |
5735 | 0 | } |
5736 | | |
5737 | | static int record_relo_core(struct bpf_program *prog, |
5738 | | const struct bpf_core_relo *core_relo, int insn_idx) |
5739 | 0 | { |
5740 | 0 | struct reloc_desc *relos, *relo; |
5741 | |
|
5742 | 0 | relos = libbpf_reallocarray(prog->reloc_desc, |
5743 | 0 | prog->nr_reloc + 1, sizeof(*relos)); |
5744 | 0 | if (!relos) |
5745 | 0 | return -ENOMEM; |
5746 | 0 | relo = &relos[prog->nr_reloc]; |
5747 | 0 | relo->type = RELO_CORE; |
5748 | 0 | relo->insn_idx = insn_idx; |
5749 | 0 | relo->core_relo = core_relo; |
5750 | 0 | prog->reloc_desc = relos; |
5751 | 0 | prog->nr_reloc++; |
5752 | 0 | return 0; |
5753 | 0 | } |
5754 | | |
5755 | | static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx) |
5756 | 0 | { |
5757 | 0 | struct reloc_desc *relo; |
5758 | 0 | int i; |
5759 | |
|
5760 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
5761 | 0 | relo = &prog->reloc_desc[i]; |
5762 | 0 | if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) |
5763 | 0 | continue; |
5764 | | |
5765 | 0 | return relo->core_relo; |
5766 | 0 | } |
5767 | | |
5768 | 0 | return NULL; |
5769 | 0 | } |
5770 | | |
5771 | | static int bpf_core_resolve_relo(struct bpf_program *prog, |
5772 | | const struct bpf_core_relo *relo, |
5773 | | int relo_idx, |
5774 | | const struct btf *local_btf, |
5775 | | struct hashmap *cand_cache, |
5776 | | struct bpf_core_relo_res *targ_res) |
5777 | 0 | { |
5778 | 0 | struct bpf_core_spec specs_scratch[3] = {}; |
5779 | 0 | struct bpf_core_cand_list *cands = NULL; |
5780 | 0 | const char *prog_name = prog->name; |
5781 | 0 | const struct btf_type *local_type; |
5782 | 0 | const char *local_name; |
5783 | 0 | __u32 local_id = relo->type_id; |
5784 | 0 | int err; |
5785 | |
|
5786 | 0 | local_type = btf__type_by_id(local_btf, local_id); |
5787 | 0 | if (!local_type) |
5788 | 0 | return -EINVAL; |
5789 | | |
5790 | 0 | local_name = btf__name_by_offset(local_btf, local_type->name_off); |
5791 | 0 | if (!local_name) |
5792 | 0 | return -EINVAL; |
5793 | | |
5794 | 0 | if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && |
5795 | 0 | !hashmap__find(cand_cache, local_id, &cands)) { |
5796 | 0 | cands = bpf_core_find_cands(prog->obj, local_btf, local_id); |
5797 | 0 | if (IS_ERR(cands)) { |
5798 | 0 | pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n", |
5799 | 0 | prog_name, relo_idx, local_id, btf_kind_str(local_type), |
5800 | 0 | local_name, PTR_ERR(cands)); |
5801 | 0 | return PTR_ERR(cands); |
5802 | 0 | } |
5803 | 0 | err = hashmap__set(cand_cache, local_id, cands, NULL, NULL); |
5804 | 0 | if (err) { |
5805 | 0 | bpf_core_free_cands(cands); |
5806 | 0 | return err; |
5807 | 0 | } |
5808 | 0 | } |
5809 | | |
5810 | 0 | return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch, |
5811 | 0 | targ_res); |
5812 | 0 | } |
5813 | | |
5814 | | static int |
5815 | | bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) |
5816 | 0 | { |
5817 | 0 | const struct btf_ext_info_sec *sec; |
5818 | 0 | struct bpf_core_relo_res targ_res; |
5819 | 0 | const struct bpf_core_relo *rec; |
5820 | 0 | const struct btf_ext_info *seg; |
5821 | 0 | struct hashmap_entry *entry; |
5822 | 0 | struct hashmap *cand_cache = NULL; |
5823 | 0 | struct bpf_program *prog; |
5824 | 0 | struct bpf_insn *insn; |
5825 | 0 | const char *sec_name; |
5826 | 0 | int i, err = 0, insn_idx, sec_idx, sec_num; |
5827 | |
|
5828 | 0 | if (obj->btf_ext->core_relo_info.len == 0) |
5829 | 0 | return 0; |
5830 | | |
5831 | 0 | if (targ_btf_path) { |
5832 | 0 | obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); |
5833 | 0 | err = libbpf_get_error(obj->btf_vmlinux_override); |
5834 | 0 | if (err) { |
5835 | 0 | pr_warn("failed to parse target BTF: %d\n", err); |
5836 | 0 | return err; |
5837 | 0 | } |
5838 | 0 | } |
5839 | | |
5840 | 0 | cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); |
5841 | 0 | if (IS_ERR(cand_cache)) { |
5842 | 0 | err = PTR_ERR(cand_cache); |
5843 | 0 | goto out; |
5844 | 0 | } |
5845 | | |
5846 | 0 | seg = &obj->btf_ext->core_relo_info; |
5847 | 0 | sec_num = 0; |
5848 | 0 | for_each_btf_ext_sec(seg, sec) { |
5849 | 0 | sec_idx = seg->sec_idxs[sec_num]; |
5850 | 0 | sec_num++; |
5851 | |
|
5852 | 0 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
5853 | 0 | if (str_is_empty(sec_name)) { |
5854 | 0 | err = -EINVAL; |
5855 | 0 | goto out; |
5856 | 0 | } |
5857 | | |
5858 | 0 | pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info); |
5859 | |
|
5860 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5861 | 0 | if (rec->insn_off % BPF_INSN_SZ) |
5862 | 0 | return -EINVAL; |
5863 | 0 | insn_idx = rec->insn_off / BPF_INSN_SZ; |
5864 | 0 | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
5865 | 0 | if (!prog) { |
5866 | | /* When __weak subprog is "overridden" by another instance |
5867 | | * of the subprog from a different object file, linker still |
5868 | | * appends all the .BTF.ext info that used to belong to that |
5869 | | * eliminated subprogram. |
5870 | | * This is similar to what x86-64 linker does for relocations. |
5871 | | * So just ignore such relocations just like we ignore |
5872 | | * subprog instructions when discovering subprograms. |
5873 | | */ |
5874 | 0 | pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n", |
5875 | 0 | sec_name, i, insn_idx); |
5876 | 0 | continue; |
5877 | 0 | } |
5878 | | /* no need to apply CO-RE relocation if the program is |
5879 | | * not going to be loaded |
5880 | | */ |
5881 | 0 | if (!prog->autoload) |
5882 | 0 | continue; |
5883 | | |
5884 | | /* adjust insn_idx from section frame of reference to the local |
5885 | | * program's frame of reference; (sub-)program code is not yet |
5886 | | * relocated, so it's enough to just subtract in-section offset |
5887 | | */ |
5888 | 0 | insn_idx = insn_idx - prog->sec_insn_off; |
5889 | 0 | if (insn_idx >= prog->insns_cnt) |
5890 | 0 | return -EINVAL; |
5891 | 0 | insn = &prog->insns[insn_idx]; |
5892 | |
|
5893 | 0 | err = record_relo_core(prog, rec, insn_idx); |
5894 | 0 | if (err) { |
5895 | 0 | pr_warn("prog '%s': relo #%d: failed to record relocation: %d\n", |
5896 | 0 | prog->name, i, err); |
5897 | 0 | goto out; |
5898 | 0 | } |
5899 | | |
5900 | 0 | if (prog->obj->gen_loader) |
5901 | 0 | continue; |
5902 | | |
5903 | 0 | err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); |
5904 | 0 | if (err) { |
5905 | 0 | pr_warn("prog '%s': relo #%d: failed to relocate: %d\n", |
5906 | 0 | prog->name, i, err); |
5907 | 0 | goto out; |
5908 | 0 | } |
5909 | | |
5910 | 0 | err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); |
5911 | 0 | if (err) { |
5912 | 0 | pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n", |
5913 | 0 | prog->name, i, insn_idx, err); |
5914 | 0 | goto out; |
5915 | 0 | } |
5916 | 0 | } |
5917 | 0 | } |
5918 | | |
5919 | 0 | out: |
5920 | | /* obj->btf_vmlinux and module BTFs are freed after object load */ |
5921 | 0 | btf__free(obj->btf_vmlinux_override); |
5922 | 0 | obj->btf_vmlinux_override = NULL; |
5923 | |
|
5924 | 0 | if (!IS_ERR_OR_NULL(cand_cache)) { |
5925 | 0 | hashmap__for_each_entry(cand_cache, entry, i) { |
5926 | 0 | bpf_core_free_cands(entry->pvalue); |
5927 | 0 | } |
5928 | 0 | hashmap__free(cand_cache); |
5929 | 0 | } |
5930 | 0 | return err; |
5931 | 0 | } |
5932 | | |
5933 | | /* base map load ldimm64 special constant, used also for log fixup logic */ |
5934 | 0 | #define POISON_LDIMM64_MAP_BASE 2001000000 |
5935 | | #define POISON_LDIMM64_MAP_PFX "200100" |
5936 | | |
5937 | | static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx, |
5938 | | int insn_idx, struct bpf_insn *insn, |
5939 | | int map_idx, const struct bpf_map *map) |
5940 | 0 | { |
5941 | 0 | int i; |
5942 | |
|
5943 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n", |
5944 | 0 | prog->name, relo_idx, insn_idx, map_idx, map->name); |
5945 | | |
5946 | | /* we turn single ldimm64 into two identical invalid calls */ |
5947 | 0 | for (i = 0; i < 2; i++) { |
5948 | 0 | insn->code = BPF_JMP | BPF_CALL; |
5949 | 0 | insn->dst_reg = 0; |
5950 | 0 | insn->src_reg = 0; |
5951 | 0 | insn->off = 0; |
5952 | | /* if this instruction is reachable (not a dead code), |
5953 | | * verifier will complain with something like: |
5954 | | * invalid func unknown#2001000123 |
5955 | | * where lower 123 is map index into obj->maps[] array |
5956 | | */ |
5957 | 0 | insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; |
5958 | |
|
5959 | 0 | insn++; |
5960 | 0 | } |
5961 | 0 | } |
5962 | | |
5963 | | /* unresolved kfunc call special constant, used also for log fixup logic */ |
5964 | 0 | #define POISON_CALL_KFUNC_BASE 2002000000 |
5965 | | #define POISON_CALL_KFUNC_PFX "2002" |
5966 | | |
5967 | | static void poison_kfunc_call(struct bpf_program *prog, int relo_idx, |
5968 | | int insn_idx, struct bpf_insn *insn, |
5969 | | int ext_idx, const struct extern_desc *ext) |
5970 | 0 | { |
5971 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n", |
5972 | 0 | prog->name, relo_idx, insn_idx, ext->name); |
5973 | | |
5974 | | /* we turn kfunc call into invalid helper call with identifiable constant */ |
5975 | 0 | insn->code = BPF_JMP | BPF_CALL; |
5976 | 0 | insn->dst_reg = 0; |
5977 | 0 | insn->src_reg = 0; |
5978 | 0 | insn->off = 0; |
5979 | | /* if this instruction is reachable (not a dead code), |
5980 | | * verifier will complain with something like: |
5981 | | * invalid func unknown#2001000123 |
5982 | | * where lower 123 is extern index into obj->externs[] array |
5983 | | */ |
5984 | 0 | insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; |
5985 | 0 | } |
5986 | | |
5987 | | /* Relocate data references within program code: |
5988 | | * - map references; |
5989 | | * - global variable references; |
5990 | | * - extern references. |
5991 | | */ |
5992 | | static int |
5993 | | bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) |
5994 | 0 | { |
5995 | 0 | int i; |
5996 | |
|
5997 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
5998 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
5999 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
6000 | 0 | const struct bpf_map *map; |
6001 | 0 | struct extern_desc *ext; |
6002 | |
|
6003 | 0 | switch (relo->type) { |
6004 | 0 | case RELO_LD64: |
6005 | 0 | map = &obj->maps[relo->map_idx]; |
6006 | 0 | if (obj->gen_loader) { |
6007 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX; |
6008 | 0 | insn[0].imm = relo->map_idx; |
6009 | 0 | } else if (map->autocreate) { |
6010 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_FD; |
6011 | 0 | insn[0].imm = map->fd; |
6012 | 0 | } else { |
6013 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6014 | 0 | relo->map_idx, map); |
6015 | 0 | } |
6016 | 0 | break; |
6017 | 0 | case RELO_DATA: |
6018 | 0 | map = &obj->maps[relo->map_idx]; |
6019 | 0 | insn[1].imm = insn[0].imm + relo->sym_off; |
6020 | 0 | if (obj->gen_loader) { |
6021 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6022 | 0 | insn[0].imm = relo->map_idx; |
6023 | 0 | } else if (map->autocreate) { |
6024 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6025 | 0 | insn[0].imm = map->fd; |
6026 | 0 | } else { |
6027 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6028 | 0 | relo->map_idx, map); |
6029 | 0 | } |
6030 | 0 | break; |
6031 | 0 | case RELO_EXTERN_LD64: |
6032 | 0 | ext = &obj->externs[relo->ext_idx]; |
6033 | 0 | if (ext->type == EXT_KCFG) { |
6034 | 0 | if (obj->gen_loader) { |
6035 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6036 | 0 | insn[0].imm = obj->kconfig_map_idx; |
6037 | 0 | } else { |
6038 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6039 | 0 | insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; |
6040 | 0 | } |
6041 | 0 | insn[1].imm = ext->kcfg.data_off; |
6042 | 0 | } else /* EXT_KSYM */ { |
6043 | 0 | if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ |
6044 | 0 | insn[0].src_reg = BPF_PSEUDO_BTF_ID; |
6045 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6046 | 0 | insn[1].imm = ext->ksym.kernel_btf_obj_fd; |
6047 | 0 | } else { /* typeless ksyms or unresolved typed ksyms */ |
6048 | 0 | insn[0].imm = (__u32)ext->ksym.addr; |
6049 | 0 | insn[1].imm = ext->ksym.addr >> 32; |
6050 | 0 | } |
6051 | 0 | } |
6052 | 0 | break; |
6053 | 0 | case RELO_EXTERN_CALL: |
6054 | 0 | ext = &obj->externs[relo->ext_idx]; |
6055 | 0 | insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL; |
6056 | 0 | if (ext->is_set) { |
6057 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6058 | 0 | insn[0].off = ext->ksym.btf_fd_idx; |
6059 | 0 | } else { /* unresolved weak kfunc call */ |
6060 | 0 | poison_kfunc_call(prog, i, relo->insn_idx, insn, |
6061 | 0 | relo->ext_idx, ext); |
6062 | 0 | } |
6063 | 0 | break; |
6064 | 0 | case RELO_SUBPROG_ADDR: |
6065 | 0 | if (insn[0].src_reg != BPF_PSEUDO_FUNC) { |
6066 | 0 | pr_warn("prog '%s': relo #%d: bad insn\n", |
6067 | 0 | prog->name, i); |
6068 | 0 | return -EINVAL; |
6069 | 0 | } |
6070 | | /* handled already */ |
6071 | 0 | break; |
6072 | 0 | case RELO_CALL: |
6073 | | /* handled already */ |
6074 | 0 | break; |
6075 | 0 | case RELO_CORE: |
6076 | | /* will be handled by bpf_program_record_relos() */ |
6077 | 0 | break; |
6078 | 0 | default: |
6079 | 0 | pr_warn("prog '%s': relo #%d: bad relo type %d\n", |
6080 | 0 | prog->name, i, relo->type); |
6081 | 0 | return -EINVAL; |
6082 | 0 | } |
6083 | 0 | } |
6084 | | |
6085 | 0 | return 0; |
6086 | 0 | } |
6087 | | |
6088 | | static int adjust_prog_btf_ext_info(const struct bpf_object *obj, |
6089 | | const struct bpf_program *prog, |
6090 | | const struct btf_ext_info *ext_info, |
6091 | | void **prog_info, __u32 *prog_rec_cnt, |
6092 | | __u32 *prog_rec_sz) |
6093 | 0 | { |
6094 | 0 | void *copy_start = NULL, *copy_end = NULL; |
6095 | 0 | void *rec, *rec_end, *new_prog_info; |
6096 | 0 | const struct btf_ext_info_sec *sec; |
6097 | 0 | size_t old_sz, new_sz; |
6098 | 0 | int i, sec_num, sec_idx, off_adj; |
6099 | |
|
6100 | 0 | sec_num = 0; |
6101 | 0 | for_each_btf_ext_sec(ext_info, sec) { |
6102 | 0 | sec_idx = ext_info->sec_idxs[sec_num]; |
6103 | 0 | sec_num++; |
6104 | 0 | if (prog->sec_idx != sec_idx) |
6105 | 0 | continue; |
6106 | | |
6107 | 0 | for_each_btf_ext_rec(ext_info, sec, i, rec) { |
6108 | 0 | __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ; |
6109 | |
|
6110 | 0 | if (insn_off < prog->sec_insn_off) |
6111 | 0 | continue; |
6112 | 0 | if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) |
6113 | 0 | break; |
6114 | | |
6115 | 0 | if (!copy_start) |
6116 | 0 | copy_start = rec; |
6117 | 0 | copy_end = rec + ext_info->rec_size; |
6118 | 0 | } |
6119 | |
|
6120 | 0 | if (!copy_start) |
6121 | 0 | return -ENOENT; |
6122 | | |
6123 | | /* append func/line info of a given (sub-)program to the main |
6124 | | * program func/line info |
6125 | | */ |
6126 | 0 | old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; |
6127 | 0 | new_sz = old_sz + (copy_end - copy_start); |
6128 | 0 | new_prog_info = realloc(*prog_info, new_sz); |
6129 | 0 | if (!new_prog_info) |
6130 | 0 | return -ENOMEM; |
6131 | 0 | *prog_info = new_prog_info; |
6132 | 0 | *prog_rec_cnt = new_sz / ext_info->rec_size; |
6133 | 0 | memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); |
6134 | | |
6135 | | /* Kernel instruction offsets are in units of 8-byte |
6136 | | * instructions, while .BTF.ext instruction offsets generated |
6137 | | * by Clang are in units of bytes. So convert Clang offsets |
6138 | | * into kernel offsets and adjust offset according to program |
6139 | | * relocated position. |
6140 | | */ |
6141 | 0 | off_adj = prog->sub_insn_off - prog->sec_insn_off; |
6142 | 0 | rec = new_prog_info + old_sz; |
6143 | 0 | rec_end = new_prog_info + new_sz; |
6144 | 0 | for (; rec < rec_end; rec += ext_info->rec_size) { |
6145 | 0 | __u32 *insn_off = rec; |
6146 | |
|
6147 | 0 | *insn_off = *insn_off / BPF_INSN_SZ + off_adj; |
6148 | 0 | } |
6149 | 0 | *prog_rec_sz = ext_info->rec_size; |
6150 | 0 | return 0; |
6151 | 0 | } |
6152 | | |
6153 | 0 | return -ENOENT; |
6154 | 0 | } |
6155 | | |
6156 | | static int |
6157 | | reloc_prog_func_and_line_info(const struct bpf_object *obj, |
6158 | | struct bpf_program *main_prog, |
6159 | | const struct bpf_program *prog) |
6160 | 0 | { |
6161 | 0 | int err; |
6162 | | |
6163 | | /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't |
6164 | | * support func/line info |
6165 | | */ |
6166 | 0 | if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) |
6167 | 0 | return 0; |
6168 | | |
6169 | | /* only attempt func info relocation if main program's func_info |
6170 | | * relocation was successful |
6171 | | */ |
6172 | 0 | if (main_prog != prog && !main_prog->func_info) |
6173 | 0 | goto line_info; |
6174 | | |
6175 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, |
6176 | 0 | &main_prog->func_info, |
6177 | 0 | &main_prog->func_info_cnt, |
6178 | 0 | &main_prog->func_info_rec_size); |
6179 | 0 | if (err) { |
6180 | 0 | if (err != -ENOENT) { |
6181 | 0 | pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n", |
6182 | 0 | prog->name, err); |
6183 | 0 | return err; |
6184 | 0 | } |
6185 | 0 | if (main_prog->func_info) { |
6186 | | /* |
6187 | | * Some info has already been found but has problem |
6188 | | * in the last btf_ext reloc. Must have to error out. |
6189 | | */ |
6190 | 0 | pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); |
6191 | 0 | return err; |
6192 | 0 | } |
6193 | | /* Have problem loading the very first info. Ignore the rest. */ |
6194 | 0 | pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n", |
6195 | 0 | prog->name); |
6196 | 0 | } |
6197 | | |
6198 | 0 | line_info: |
6199 | | /* don't relocate line info if main program's relocation failed */ |
6200 | 0 | if (main_prog != prog && !main_prog->line_info) |
6201 | 0 | return 0; |
6202 | | |
6203 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, |
6204 | 0 | &main_prog->line_info, |
6205 | 0 | &main_prog->line_info_cnt, |
6206 | 0 | &main_prog->line_info_rec_size); |
6207 | 0 | if (err) { |
6208 | 0 | if (err != -ENOENT) { |
6209 | 0 | pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n", |
6210 | 0 | prog->name, err); |
6211 | 0 | return err; |
6212 | 0 | } |
6213 | 0 | if (main_prog->line_info) { |
6214 | | /* |
6215 | | * Some info has already been found but has problem |
6216 | | * in the last btf_ext reloc. Must have to error out. |
6217 | | */ |
6218 | 0 | pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); |
6219 | 0 | return err; |
6220 | 0 | } |
6221 | | /* Have problem loading the very first info. Ignore the rest. */ |
6222 | 0 | pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n", |
6223 | 0 | prog->name); |
6224 | 0 | } |
6225 | 0 | return 0; |
6226 | 0 | } |
6227 | | |
6228 | | static int cmp_relo_by_insn_idx(const void *key, const void *elem) |
6229 | 0 | { |
6230 | 0 | size_t insn_idx = *(const size_t *)key; |
6231 | 0 | const struct reloc_desc *relo = elem; |
6232 | |
|
6233 | 0 | if (insn_idx == relo->insn_idx) |
6234 | 0 | return 0; |
6235 | 0 | return insn_idx < relo->insn_idx ? -1 : 1; |
6236 | 0 | } |
6237 | | |
6238 | | static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx) |
6239 | 0 | { |
6240 | 0 | if (!prog->nr_reloc) |
6241 | 0 | return NULL; |
6242 | 0 | return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, |
6243 | 0 | sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); |
6244 | 0 | } |
6245 | | |
6246 | | static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog) |
6247 | 0 | { |
6248 | 0 | int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; |
6249 | 0 | struct reloc_desc *relos; |
6250 | 0 | int i; |
6251 | |
|
6252 | 0 | if (main_prog == subprog) |
6253 | 0 | return 0; |
6254 | 0 | relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); |
6255 | | /* if new count is zero, reallocarray can return a valid NULL result; |
6256 | | * in this case the previous pointer will be freed, so we *have to* |
6257 | | * reassign old pointer to the new value (even if it's NULL) |
6258 | | */ |
6259 | 0 | if (!relos && new_cnt) |
6260 | 0 | return -ENOMEM; |
6261 | 0 | if (subprog->nr_reloc) |
6262 | 0 | memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, |
6263 | 0 | sizeof(*relos) * subprog->nr_reloc); |
6264 | |
|
6265 | 0 | for (i = main_prog->nr_reloc; i < new_cnt; i++) |
6266 | 0 | relos[i].insn_idx += subprog->sub_insn_off; |
6267 | | /* After insn_idx adjustment the 'relos' array is still sorted |
6268 | | * by insn_idx and doesn't break bsearch. |
6269 | | */ |
6270 | 0 | main_prog->reloc_desc = relos; |
6271 | 0 | main_prog->nr_reloc = new_cnt; |
6272 | 0 | return 0; |
6273 | 0 | } |
6274 | | |
6275 | | static int |
6276 | | bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6277 | | struct bpf_program *subprog) |
6278 | 0 | { |
6279 | 0 | struct bpf_insn *insns; |
6280 | 0 | size_t new_cnt; |
6281 | 0 | int err; |
6282 | |
|
6283 | 0 | subprog->sub_insn_off = main_prog->insns_cnt; |
6284 | |
|
6285 | 0 | new_cnt = main_prog->insns_cnt + subprog->insns_cnt; |
6286 | 0 | insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); |
6287 | 0 | if (!insns) { |
6288 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); |
6289 | 0 | return -ENOMEM; |
6290 | 0 | } |
6291 | 0 | main_prog->insns = insns; |
6292 | 0 | main_prog->insns_cnt = new_cnt; |
6293 | |
|
6294 | 0 | memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, |
6295 | 0 | subprog->insns_cnt * sizeof(*insns)); |
6296 | |
|
6297 | 0 | pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", |
6298 | 0 | main_prog->name, subprog->insns_cnt, subprog->name); |
6299 | | |
6300 | | /* The subprog insns are now appended. Append its relos too. */ |
6301 | 0 | err = append_subprog_relos(main_prog, subprog); |
6302 | 0 | if (err) |
6303 | 0 | return err; |
6304 | 0 | return 0; |
6305 | 0 | } |
6306 | | |
6307 | | static int |
6308 | | bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6309 | | struct bpf_program *prog) |
6310 | 0 | { |
6311 | 0 | size_t sub_insn_idx, insn_idx; |
6312 | 0 | struct bpf_program *subprog; |
6313 | 0 | struct reloc_desc *relo; |
6314 | 0 | struct bpf_insn *insn; |
6315 | 0 | int err; |
6316 | |
|
6317 | 0 | err = reloc_prog_func_and_line_info(obj, main_prog, prog); |
6318 | 0 | if (err) |
6319 | 0 | return err; |
6320 | | |
6321 | 0 | for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { |
6322 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6323 | 0 | if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn)) |
6324 | 0 | continue; |
6325 | | |
6326 | 0 | relo = find_prog_insn_relo(prog, insn_idx); |
6327 | 0 | if (relo && relo->type == RELO_EXTERN_CALL) |
6328 | | /* kfunc relocations will be handled later |
6329 | | * in bpf_object__relocate_data() |
6330 | | */ |
6331 | 0 | continue; |
6332 | 0 | if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { |
6333 | 0 | pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n", |
6334 | 0 | prog->name, insn_idx, relo->type); |
6335 | 0 | return -LIBBPF_ERRNO__RELOC; |
6336 | 0 | } |
6337 | 0 | if (relo) { |
6338 | | /* sub-program instruction index is a combination of |
6339 | | * an offset of a symbol pointed to by relocation and |
6340 | | * call instruction's imm field; for global functions, |
6341 | | * call always has imm = -1, but for static functions |
6342 | | * relocation is against STT_SECTION and insn->imm |
6343 | | * points to a start of a static function |
6344 | | * |
6345 | | * for subprog addr relocation, the relo->sym_off + insn->imm is |
6346 | | * the byte offset in the corresponding section. |
6347 | | */ |
6348 | 0 | if (relo->type == RELO_CALL) |
6349 | 0 | sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; |
6350 | 0 | else |
6351 | 0 | sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; |
6352 | 0 | } else if (insn_is_pseudo_func(insn)) { |
6353 | | /* |
6354 | | * RELO_SUBPROG_ADDR relo is always emitted even if both |
6355 | | * functions are in the same section, so it shouldn't reach here. |
6356 | | */ |
6357 | 0 | pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n", |
6358 | 0 | prog->name, insn_idx); |
6359 | 0 | return -LIBBPF_ERRNO__RELOC; |
6360 | 0 | } else { |
6361 | | /* if subprogram call is to a static function within |
6362 | | * the same ELF section, there won't be any relocation |
6363 | | * emitted, but it also means there is no additional |
6364 | | * offset necessary, insns->imm is relative to |
6365 | | * instruction's original position within the section |
6366 | | */ |
6367 | 0 | sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; |
6368 | 0 | } |
6369 | | |
6370 | | /* we enforce that sub-programs should be in .text section */ |
6371 | 0 | subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); |
6372 | 0 | if (!subprog) { |
6373 | 0 | pr_warn("prog '%s': no .text section found yet sub-program call exists\n", |
6374 | 0 | prog->name); |
6375 | 0 | return -LIBBPF_ERRNO__RELOC; |
6376 | 0 | } |
6377 | | |
6378 | | /* if it's the first call instruction calling into this |
6379 | | * subprogram (meaning this subprog hasn't been processed |
6380 | | * yet) within the context of current main program: |
6381 | | * - append it at the end of main program's instructions blog; |
6382 | | * - process is recursively, while current program is put on hold; |
6383 | | * - if that subprogram calls some other not yet processes |
6384 | | * subprogram, same thing will happen recursively until |
6385 | | * there are no more unprocesses subprograms left to append |
6386 | | * and relocate. |
6387 | | */ |
6388 | 0 | if (subprog->sub_insn_off == 0) { |
6389 | 0 | err = bpf_object__append_subprog_code(obj, main_prog, subprog); |
6390 | 0 | if (err) |
6391 | 0 | return err; |
6392 | 0 | err = bpf_object__reloc_code(obj, main_prog, subprog); |
6393 | 0 | if (err) |
6394 | 0 | return err; |
6395 | 0 | } |
6396 | | |
6397 | | /* main_prog->insns memory could have been re-allocated, so |
6398 | | * calculate pointer again |
6399 | | */ |
6400 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6401 | | /* calculate correct instruction position within current main |
6402 | | * prog; each main prog can have a different set of |
6403 | | * subprograms appended (potentially in different order as |
6404 | | * well), so position of any subprog can be different for |
6405 | | * different main programs |
6406 | | */ |
6407 | 0 | insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; |
6408 | |
|
6409 | 0 | pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n", |
6410 | 0 | prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); |
6411 | 0 | } |
6412 | | |
6413 | 0 | return 0; |
6414 | 0 | } |
6415 | | |
6416 | | /* |
6417 | | * Relocate sub-program calls. |
6418 | | * |
6419 | | * Algorithm operates as follows. Each entry-point BPF program (referred to as |
6420 | | * main prog) is processed separately. For each subprog (non-entry functions, |
6421 | | * that can be called from either entry progs or other subprogs) gets their |
6422 | | * sub_insn_off reset to zero. This serves as indicator that this subprogram |
6423 | | * hasn't been yet appended and relocated within current main prog. Once its |
6424 | | * relocated, sub_insn_off will point at the position within current main prog |
6425 | | * where given subprog was appended. This will further be used to relocate all |
6426 | | * the call instructions jumping into this subprog. |
6427 | | * |
6428 | | * We start with main program and process all call instructions. If the call |
6429 | | * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off |
6430 | | * is zero), subprog instructions are appended at the end of main program's |
6431 | | * instruction array. Then main program is "put on hold" while we recursively |
6432 | | * process newly appended subprogram. If that subprogram calls into another |
6433 | | * subprogram that hasn't been appended, new subprogram is appended again to |
6434 | | * the *main* prog's instructions (subprog's instructions are always left |
6435 | | * untouched, as they need to be in unmodified state for subsequent main progs |
6436 | | * and subprog instructions are always sent only as part of a main prog) and |
6437 | | * the process continues recursively. Once all the subprogs called from a main |
6438 | | * prog or any of its subprogs are appended (and relocated), all their |
6439 | | * positions within finalized instructions array are known, so it's easy to |
6440 | | * rewrite call instructions with correct relative offsets, corresponding to |
6441 | | * desired target subprog. |
6442 | | * |
6443 | | * Its important to realize that some subprogs might not be called from some |
6444 | | * main prog and any of its called/used subprogs. Those will keep their |
6445 | | * subprog->sub_insn_off as zero at all times and won't be appended to current |
6446 | | * main prog and won't be relocated within the context of current main prog. |
6447 | | * They might still be used from other main progs later. |
6448 | | * |
6449 | | * Visually this process can be shown as below. Suppose we have two main |
6450 | | * programs mainA and mainB and BPF object contains three subprogs: subA, |
6451 | | * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and |
6452 | | * subC both call subB: |
6453 | | * |
6454 | | * +--------+ +-------+ |
6455 | | * | v v | |
6456 | | * +--+---+ +--+-+-+ +---+--+ |
6457 | | * | subA | | subB | | subC | |
6458 | | * +--+---+ +------+ +---+--+ |
6459 | | * ^ ^ |
6460 | | * | | |
6461 | | * +---+-------+ +------+----+ |
6462 | | * | mainA | | mainB | |
6463 | | * +-----------+ +-----------+ |
6464 | | * |
6465 | | * We'll start relocating mainA, will find subA, append it and start |
6466 | | * processing sub A recursively: |
6467 | | * |
6468 | | * +-----------+------+ |
6469 | | * | mainA | subA | |
6470 | | * +-----------+------+ |
6471 | | * |
6472 | | * At this point we notice that subB is used from subA, so we append it and |
6473 | | * relocate (there are no further subcalls from subB): |
6474 | | * |
6475 | | * +-----------+------+------+ |
6476 | | * | mainA | subA | subB | |
6477 | | * +-----------+------+------+ |
6478 | | * |
6479 | | * At this point, we relocate subA calls, then go one level up and finish with |
6480 | | * relocatin mainA calls. mainA is done. |
6481 | | * |
6482 | | * For mainB process is similar but results in different order. We start with |
6483 | | * mainB and skip subA and subB, as mainB never calls them (at least |
6484 | | * directly), but we see subC is needed, so we append and start processing it: |
6485 | | * |
6486 | | * +-----------+------+ |
6487 | | * | mainB | subC | |
6488 | | * +-----------+------+ |
6489 | | * Now we see subC needs subB, so we go back to it, append and relocate it: |
6490 | | * |
6491 | | * +-----------+------+------+ |
6492 | | * | mainB | subC | subB | |
6493 | | * +-----------+------+------+ |
6494 | | * |
6495 | | * At this point we unwind recursion, relocate calls in subC, then in mainB. |
6496 | | */ |
6497 | | static int |
6498 | | bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog) |
6499 | 0 | { |
6500 | 0 | struct bpf_program *subprog; |
6501 | 0 | int i, err; |
6502 | | |
6503 | | /* mark all subprogs as not relocated (yet) within the context of |
6504 | | * current main program |
6505 | | */ |
6506 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6507 | 0 | subprog = &obj->programs[i]; |
6508 | 0 | if (!prog_is_subprog(obj, subprog)) |
6509 | 0 | continue; |
6510 | | |
6511 | 0 | subprog->sub_insn_off = 0; |
6512 | 0 | } |
6513 | |
|
6514 | 0 | err = bpf_object__reloc_code(obj, prog, prog); |
6515 | 0 | if (err) |
6516 | 0 | return err; |
6517 | | |
6518 | 0 | return 0; |
6519 | 0 | } |
6520 | | |
6521 | | static void |
6522 | | bpf_object__free_relocs(struct bpf_object *obj) |
6523 | 0 | { |
6524 | 0 | struct bpf_program *prog; |
6525 | 0 | int i; |
6526 | | |
6527 | | /* free up relocation descriptors */ |
6528 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6529 | 0 | prog = &obj->programs[i]; |
6530 | 0 | zfree(&prog->reloc_desc); |
6531 | 0 | prog->nr_reloc = 0; |
6532 | 0 | } |
6533 | 0 | } |
6534 | | |
6535 | | static int cmp_relocs(const void *_a, const void *_b) |
6536 | 4.70k | { |
6537 | 4.70k | const struct reloc_desc *a = _a; |
6538 | 4.70k | const struct reloc_desc *b = _b; |
6539 | | |
6540 | 4.70k | if (a->insn_idx != b->insn_idx) |
6541 | 738 | return a->insn_idx < b->insn_idx ? -1 : 1; |
6542 | | |
6543 | | /* no two relocations should have the same insn_idx, but ... */ |
6544 | 3.96k | if (a->type != b->type) |
6545 | 185 | return a->type < b->type ? -1 : 1; |
6546 | | |
6547 | 3.78k | return 0; |
6548 | 3.96k | } |
6549 | | |
6550 | | static void bpf_object__sort_relos(struct bpf_object *obj) |
6551 | 1.52k | { |
6552 | 1.52k | int i; |
6553 | | |
6554 | 10.4k | for (i = 0; i < obj->nr_programs; i++) { |
6555 | 8.94k | struct bpf_program *p = &obj->programs[i]; |
6556 | | |
6557 | 8.94k | if (!p->nr_reloc) |
6558 | 8.90k | continue; |
6559 | | |
6560 | 47 | qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); |
6561 | 47 | } |
6562 | 1.52k | } |
6563 | | |
6564 | | static int bpf_prog_assign_exc_cb(struct bpf_object *obj, struct bpf_program *prog) |
6565 | 0 | { |
6566 | 0 | const char *str = "exception_callback:"; |
6567 | 0 | size_t pfx_len = strlen(str); |
6568 | 0 | int i, j, n; |
6569 | |
|
6570 | 0 | if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG)) |
6571 | 0 | return 0; |
6572 | | |
6573 | 0 | n = btf__type_cnt(obj->btf); |
6574 | 0 | for (i = 1; i < n; i++) { |
6575 | 0 | const char *name; |
6576 | 0 | struct btf_type *t; |
6577 | |
|
6578 | 0 | t = btf_type_by_id(obj->btf, i); |
6579 | 0 | if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1) |
6580 | 0 | continue; |
6581 | | |
6582 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
6583 | 0 | if (strncmp(name, str, pfx_len) != 0) |
6584 | 0 | continue; |
6585 | | |
6586 | 0 | t = btf_type_by_id(obj->btf, t->type); |
6587 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) { |
6588 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n", |
6589 | 0 | prog->name); |
6590 | 0 | return -EINVAL; |
6591 | 0 | } |
6592 | 0 | if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0) |
6593 | 0 | continue; |
6594 | | /* Multiple callbacks are specified for the same prog, |
6595 | | * the verifier will eventually return an error for this |
6596 | | * case, hence simply skip appending a subprog. |
6597 | | */ |
6598 | 0 | if (prog->exception_cb_idx >= 0) { |
6599 | 0 | prog->exception_cb_idx = -1; |
6600 | 0 | break; |
6601 | 0 | } |
6602 | | |
6603 | 0 | name += pfx_len; |
6604 | 0 | if (str_is_empty(name)) { |
6605 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n", |
6606 | 0 | prog->name); |
6607 | 0 | return -EINVAL; |
6608 | 0 | } |
6609 | | |
6610 | 0 | for (j = 0; j < obj->nr_programs; j++) { |
6611 | 0 | struct bpf_program *subprog = &obj->programs[j]; |
6612 | |
|
6613 | 0 | if (!prog_is_subprog(obj, subprog)) |
6614 | 0 | continue; |
6615 | 0 | if (strcmp(name, subprog->name) != 0) |
6616 | 0 | continue; |
6617 | | /* Enforce non-hidden, as from verifier point of |
6618 | | * view it expects global functions, whereas the |
6619 | | * mark_btf_static fixes up linkage as static. |
6620 | | */ |
6621 | 0 | if (!subprog->sym_global || subprog->mark_btf_static) { |
6622 | 0 | pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n", |
6623 | 0 | prog->name, subprog->name); |
6624 | 0 | return -EINVAL; |
6625 | 0 | } |
6626 | | /* Let's see if we already saw a static exception callback with the same name */ |
6627 | 0 | if (prog->exception_cb_idx >= 0) { |
6628 | 0 | pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n", |
6629 | 0 | prog->name, subprog->name); |
6630 | 0 | return -EINVAL; |
6631 | 0 | } |
6632 | 0 | prog->exception_cb_idx = j; |
6633 | 0 | break; |
6634 | 0 | } |
6635 | | |
6636 | 0 | if (prog->exception_cb_idx >= 0) |
6637 | 0 | continue; |
6638 | | |
6639 | 0 | pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name); |
6640 | 0 | return -ENOENT; |
6641 | 0 | } |
6642 | | |
6643 | 0 | return 0; |
6644 | 0 | } |
6645 | | |
6646 | | static struct { |
6647 | | enum bpf_prog_type prog_type; |
6648 | | const char *ctx_name; |
6649 | | } global_ctx_map[] = { |
6650 | | { BPF_PROG_TYPE_CGROUP_DEVICE, "bpf_cgroup_dev_ctx" }, |
6651 | | { BPF_PROG_TYPE_CGROUP_SKB, "__sk_buff" }, |
6652 | | { BPF_PROG_TYPE_CGROUP_SOCK, "bpf_sock" }, |
6653 | | { BPF_PROG_TYPE_CGROUP_SOCK_ADDR, "bpf_sock_addr" }, |
6654 | | { BPF_PROG_TYPE_CGROUP_SOCKOPT, "bpf_sockopt" }, |
6655 | | { BPF_PROG_TYPE_CGROUP_SYSCTL, "bpf_sysctl" }, |
6656 | | { BPF_PROG_TYPE_FLOW_DISSECTOR, "__sk_buff" }, |
6657 | | { BPF_PROG_TYPE_KPROBE, "bpf_user_pt_regs_t" }, |
6658 | | { BPF_PROG_TYPE_LWT_IN, "__sk_buff" }, |
6659 | | { BPF_PROG_TYPE_LWT_OUT, "__sk_buff" }, |
6660 | | { BPF_PROG_TYPE_LWT_SEG6LOCAL, "__sk_buff" }, |
6661 | | { BPF_PROG_TYPE_LWT_XMIT, "__sk_buff" }, |
6662 | | { BPF_PROG_TYPE_NETFILTER, "bpf_nf_ctx" }, |
6663 | | { BPF_PROG_TYPE_PERF_EVENT, "bpf_perf_event_data" }, |
6664 | | { BPF_PROG_TYPE_RAW_TRACEPOINT, "bpf_raw_tracepoint_args" }, |
6665 | | { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, "bpf_raw_tracepoint_args" }, |
6666 | | { BPF_PROG_TYPE_SCHED_ACT, "__sk_buff" }, |
6667 | | { BPF_PROG_TYPE_SCHED_CLS, "__sk_buff" }, |
6668 | | { BPF_PROG_TYPE_SK_LOOKUP, "bpf_sk_lookup" }, |
6669 | | { BPF_PROG_TYPE_SK_MSG, "sk_msg_md" }, |
6670 | | { BPF_PROG_TYPE_SK_REUSEPORT, "sk_reuseport_md" }, |
6671 | | { BPF_PROG_TYPE_SK_SKB, "__sk_buff" }, |
6672 | | { BPF_PROG_TYPE_SOCK_OPS, "bpf_sock_ops" }, |
6673 | | { BPF_PROG_TYPE_SOCKET_FILTER, "__sk_buff" }, |
6674 | | { BPF_PROG_TYPE_XDP, "xdp_md" }, |
6675 | | /* all other program types don't have "named" context structs */ |
6676 | | }; |
6677 | | |
6678 | | /* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef, |
6679 | | * for below __builtin_types_compatible_p() checks; |
6680 | | * with this approach we don't need any extra arch-specific #ifdef guards |
6681 | | */ |
6682 | | struct pt_regs; |
6683 | | struct user_pt_regs; |
6684 | | struct user_regs_struct; |
6685 | | |
6686 | | static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog, |
6687 | | const char *subprog_name, int arg_idx, |
6688 | | int arg_type_id, const char *ctx_name) |
6689 | 0 | { |
6690 | 0 | const struct btf_type *t; |
6691 | 0 | const char *tname; |
6692 | | |
6693 | | /* check if existing parameter already matches verifier expectations */ |
6694 | 0 | t = skip_mods_and_typedefs(btf, arg_type_id, NULL); |
6695 | 0 | if (!btf_is_ptr(t)) |
6696 | 0 | goto out_warn; |
6697 | | |
6698 | | /* typedef bpf_user_pt_regs_t is a special PITA case, valid for kprobe |
6699 | | * and perf_event programs, so check this case early on and forget |
6700 | | * about it for subsequent checks |
6701 | | */ |
6702 | 0 | while (btf_is_mod(t)) |
6703 | 0 | t = btf__type_by_id(btf, t->type); |
6704 | 0 | if (btf_is_typedef(t) && |
6705 | 0 | (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) { |
6706 | 0 | tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; |
6707 | 0 | if (strcmp(tname, "bpf_user_pt_regs_t") == 0) |
6708 | 0 | return false; /* canonical type for kprobe/perf_event */ |
6709 | 0 | } |
6710 | | |
6711 | | /* now we can ignore typedefs moving forward */ |
6712 | 0 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
6713 | | |
6714 | | /* if it's `void *`, definitely fix up BTF info */ |
6715 | 0 | if (btf_is_void(t)) |
6716 | 0 | return true; |
6717 | | |
6718 | | /* if it's already proper canonical type, no need to fix up */ |
6719 | 0 | tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; |
6720 | 0 | if (btf_is_struct(t) && strcmp(tname, ctx_name) == 0) |
6721 | 0 | return false; |
6722 | | |
6723 | | /* special cases */ |
6724 | 0 | switch (prog->type) { |
6725 | 0 | case BPF_PROG_TYPE_KPROBE: |
6726 | | /* `struct pt_regs *` is expected, but we need to fix up */ |
6727 | 0 | if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0) |
6728 | 0 | return true; |
6729 | 0 | break; |
6730 | 0 | case BPF_PROG_TYPE_PERF_EVENT: |
6731 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) && |
6732 | 0 | btf_is_struct(t) && strcmp(tname, "pt_regs") == 0) |
6733 | 0 | return true; |
6734 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) && |
6735 | 0 | btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0) |
6736 | 0 | return true; |
6737 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) && |
6738 | 0 | btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0) |
6739 | 0 | return true; |
6740 | 0 | break; |
6741 | 0 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
6742 | 0 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
6743 | | /* allow u64* as ctx */ |
6744 | 0 | if (btf_is_int(t) && t->size == 8) |
6745 | 0 | return true; |
6746 | 0 | break; |
6747 | 0 | default: |
6748 | 0 | break; |
6749 | 0 | } |
6750 | | |
6751 | 0 | out_warn: |
6752 | 0 | pr_warn("prog '%s': subprog '%s' arg#%d is expected to be of `struct %s *` type\n", |
6753 | 0 | prog->name, subprog_name, arg_idx, ctx_name); |
6754 | 0 | return false; |
6755 | 0 | } |
6756 | | |
6757 | | static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_program *prog) |
6758 | 0 | { |
6759 | 0 | int fn_id, fn_proto_id, ret_type_id, orig_proto_id; |
6760 | 0 | int i, err, arg_cnt, fn_name_off, linkage; |
6761 | 0 | struct btf_type *fn_t, *fn_proto_t, *t; |
6762 | 0 | struct btf_param *p; |
6763 | | |
6764 | | /* caller already validated FUNC -> FUNC_PROTO validity */ |
6765 | 0 | fn_t = btf_type_by_id(btf, orig_fn_id); |
6766 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
6767 | | |
6768 | | /* Note that each btf__add_xxx() operation invalidates |
6769 | | * all btf_type and string pointers, so we need to be |
6770 | | * very careful when cloning BTF types. BTF type |
6771 | | * pointers have to be always refetched. And to avoid |
6772 | | * problems with invalidated string pointers, we |
6773 | | * add empty strings initially, then just fix up |
6774 | | * name_off offsets in place. Offsets are stable for |
6775 | | * existing strings, so that works out. |
6776 | | */ |
6777 | 0 | fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */ |
6778 | 0 | linkage = btf_func_linkage(fn_t); |
6779 | 0 | orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */ |
6780 | 0 | ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */ |
6781 | 0 | arg_cnt = btf_vlen(fn_proto_t); |
6782 | | |
6783 | | /* clone FUNC_PROTO and its params */ |
6784 | 0 | fn_proto_id = btf__add_func_proto(btf, ret_type_id); |
6785 | 0 | if (fn_proto_id < 0) |
6786 | 0 | return -EINVAL; |
6787 | | |
6788 | 0 | for (i = 0; i < arg_cnt; i++) { |
6789 | 0 | int name_off; |
6790 | | |
6791 | | /* copy original parameter data */ |
6792 | 0 | t = btf_type_by_id(btf, orig_proto_id); |
6793 | 0 | p = &btf_params(t)[i]; |
6794 | 0 | name_off = p->name_off; |
6795 | |
|
6796 | 0 | err = btf__add_func_param(btf, "", p->type); |
6797 | 0 | if (err) |
6798 | 0 | return err; |
6799 | | |
6800 | 0 | fn_proto_t = btf_type_by_id(btf, fn_proto_id); |
6801 | 0 | p = &btf_params(fn_proto_t)[i]; |
6802 | 0 | p->name_off = name_off; /* use remembered str offset */ |
6803 | 0 | } |
6804 | | |
6805 | | /* clone FUNC now, btf__add_func() enforces non-empty name, so use |
6806 | | * entry program's name as a placeholder, which we replace immediately |
6807 | | * with original name_off |
6808 | | */ |
6809 | 0 | fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id); |
6810 | 0 | if (fn_id < 0) |
6811 | 0 | return -EINVAL; |
6812 | | |
6813 | 0 | fn_t = btf_type_by_id(btf, fn_id); |
6814 | 0 | fn_t->name_off = fn_name_off; /* reuse original string */ |
6815 | |
|
6816 | 0 | return fn_id; |
6817 | 0 | } |
6818 | | |
6819 | | /* Check if main program or global subprog's function prototype has `arg:ctx` |
6820 | | * argument tags, and, if necessary, substitute correct type to match what BPF |
6821 | | * verifier would expect, taking into account specific program type. This |
6822 | | * allows to support __arg_ctx tag transparently on old kernels that don't yet |
6823 | | * have a native support for it in the verifier, making user's life much |
6824 | | * easier. |
6825 | | */ |
6826 | | static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_program *prog) |
6827 | 0 | { |
6828 | 0 | const char *ctx_name = NULL, *ctx_tag = "arg:ctx", *fn_name; |
6829 | 0 | struct bpf_func_info_min *func_rec; |
6830 | 0 | struct btf_type *fn_t, *fn_proto_t; |
6831 | 0 | struct btf *btf = obj->btf; |
6832 | 0 | const struct btf_type *t; |
6833 | 0 | struct btf_param *p; |
6834 | 0 | int ptr_id = 0, struct_id, tag_id, orig_fn_id; |
6835 | 0 | int i, n, arg_idx, arg_cnt, err, rec_idx; |
6836 | 0 | int *orig_ids; |
6837 | | |
6838 | | /* no .BTF.ext, no problem */ |
6839 | 0 | if (!obj->btf_ext || !prog->func_info) |
6840 | 0 | return 0; |
6841 | | |
6842 | | /* don't do any fix ups if kernel natively supports __arg_ctx */ |
6843 | 0 | if (kernel_supports(obj, FEAT_ARG_CTX_TAG)) |
6844 | 0 | return 0; |
6845 | | |
6846 | | /* some BPF program types just don't have named context structs, so |
6847 | | * this fallback mechanism doesn't work for them |
6848 | | */ |
6849 | 0 | for (i = 0; i < ARRAY_SIZE(global_ctx_map); i++) { |
6850 | 0 | if (global_ctx_map[i].prog_type != prog->type) |
6851 | 0 | continue; |
6852 | 0 | ctx_name = global_ctx_map[i].ctx_name; |
6853 | 0 | break; |
6854 | 0 | } |
6855 | 0 | if (!ctx_name) |
6856 | 0 | return 0; |
6857 | | |
6858 | | /* remember original func BTF IDs to detect if we already cloned them */ |
6859 | 0 | orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids)); |
6860 | 0 | if (!orig_ids) |
6861 | 0 | return -ENOMEM; |
6862 | 0 | for (i = 0; i < prog->func_info_cnt; i++) { |
6863 | 0 | func_rec = prog->func_info + prog->func_info_rec_size * i; |
6864 | 0 | orig_ids[i] = func_rec->type_id; |
6865 | 0 | } |
6866 | | |
6867 | | /* go through each DECL_TAG with "arg:ctx" and see if it points to one |
6868 | | * of our subprogs; if yes and subprog is global and needs adjustment, |
6869 | | * clone and adjust FUNC -> FUNC_PROTO combo |
6870 | | */ |
6871 | 0 | for (i = 1, n = btf__type_cnt(btf); i < n; i++) { |
6872 | | /* only DECL_TAG with "arg:ctx" value are interesting */ |
6873 | 0 | t = btf__type_by_id(btf, i); |
6874 | 0 | if (!btf_is_decl_tag(t)) |
6875 | 0 | continue; |
6876 | 0 | if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0) |
6877 | 0 | continue; |
6878 | | |
6879 | | /* only global funcs need adjustment, if at all */ |
6880 | 0 | orig_fn_id = t->type; |
6881 | 0 | fn_t = btf_type_by_id(btf, orig_fn_id); |
6882 | 0 | if (!btf_is_func(fn_t) || btf_func_linkage(fn_t) != BTF_FUNC_GLOBAL) |
6883 | 0 | continue; |
6884 | | |
6885 | | /* sanity check FUNC -> FUNC_PROTO chain, just in case */ |
6886 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
6887 | 0 | if (!fn_proto_t || !btf_is_func_proto(fn_proto_t)) |
6888 | 0 | continue; |
6889 | | |
6890 | | /* find corresponding func_info record */ |
6891 | 0 | func_rec = NULL; |
6892 | 0 | for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) { |
6893 | 0 | if (orig_ids[rec_idx] == t->type) { |
6894 | 0 | func_rec = prog->func_info + prog->func_info_rec_size * rec_idx; |
6895 | 0 | break; |
6896 | 0 | } |
6897 | 0 | } |
6898 | | /* current main program doesn't call into this subprog */ |
6899 | 0 | if (!func_rec) |
6900 | 0 | continue; |
6901 | | |
6902 | | /* some more sanity checking of DECL_TAG */ |
6903 | 0 | arg_cnt = btf_vlen(fn_proto_t); |
6904 | 0 | arg_idx = btf_decl_tag(t)->component_idx; |
6905 | 0 | if (arg_idx < 0 || arg_idx >= arg_cnt) |
6906 | 0 | continue; |
6907 | | |
6908 | | /* check if we should fix up argument type */ |
6909 | 0 | p = &btf_params(fn_proto_t)[arg_idx]; |
6910 | 0 | fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>"; |
6911 | 0 | if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name)) |
6912 | 0 | continue; |
6913 | | |
6914 | | /* clone fn/fn_proto, unless we already did it for another arg */ |
6915 | 0 | if (func_rec->type_id == orig_fn_id) { |
6916 | 0 | int fn_id; |
6917 | |
|
6918 | 0 | fn_id = clone_func_btf_info(btf, orig_fn_id, prog); |
6919 | 0 | if (fn_id < 0) { |
6920 | 0 | err = fn_id; |
6921 | 0 | goto err_out; |
6922 | 0 | } |
6923 | | |
6924 | | /* point func_info record to a cloned FUNC type */ |
6925 | 0 | func_rec->type_id = fn_id; |
6926 | 0 | } |
6927 | | |
6928 | | /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument; |
6929 | | * we do it just once per main BPF program, as all global |
6930 | | * funcs share the same program type, so need only PTR -> |
6931 | | * STRUCT type chain |
6932 | | */ |
6933 | 0 | if (ptr_id == 0) { |
6934 | 0 | struct_id = btf__add_struct(btf, ctx_name, 0); |
6935 | 0 | ptr_id = btf__add_ptr(btf, struct_id); |
6936 | 0 | if (ptr_id < 0 || struct_id < 0) { |
6937 | 0 | err = -EINVAL; |
6938 | 0 | goto err_out; |
6939 | 0 | } |
6940 | 0 | } |
6941 | | |
6942 | | /* for completeness, clone DECL_TAG and point it to cloned param */ |
6943 | 0 | tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx); |
6944 | 0 | if (tag_id < 0) { |
6945 | 0 | err = -EINVAL; |
6946 | 0 | goto err_out; |
6947 | 0 | } |
6948 | | |
6949 | | /* all the BTF manipulations invalidated pointers, refetch them */ |
6950 | 0 | fn_t = btf_type_by_id(btf, func_rec->type_id); |
6951 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
6952 | | |
6953 | | /* fix up type ID pointed to by param */ |
6954 | 0 | p = &btf_params(fn_proto_t)[arg_idx]; |
6955 | 0 | p->type = ptr_id; |
6956 | 0 | } |
6957 | | |
6958 | 0 | free(orig_ids); |
6959 | 0 | return 0; |
6960 | 0 | err_out: |
6961 | 0 | free(orig_ids); |
6962 | 0 | return err; |
6963 | 0 | } |
6964 | | |
6965 | | static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) |
6966 | 0 | { |
6967 | 0 | struct bpf_program *prog; |
6968 | 0 | size_t i, j; |
6969 | 0 | int err; |
6970 | |
|
6971 | 0 | if (obj->btf_ext) { |
6972 | 0 | err = bpf_object__relocate_core(obj, targ_btf_path); |
6973 | 0 | if (err) { |
6974 | 0 | pr_warn("failed to perform CO-RE relocations: %d\n", |
6975 | 0 | err); |
6976 | 0 | return err; |
6977 | 0 | } |
6978 | 0 | bpf_object__sort_relos(obj); |
6979 | 0 | } |
6980 | | |
6981 | | /* Before relocating calls pre-process relocations and mark |
6982 | | * few ld_imm64 instructions that points to subprogs. |
6983 | | * Otherwise bpf_object__reloc_code() later would have to consider |
6984 | | * all ld_imm64 insns as relocation candidates. That would |
6985 | | * reduce relocation speed, since amount of find_prog_insn_relo() |
6986 | | * would increase and most of them will fail to find a relo. |
6987 | | */ |
6988 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6989 | 0 | prog = &obj->programs[i]; |
6990 | 0 | for (j = 0; j < prog->nr_reloc; j++) { |
6991 | 0 | struct reloc_desc *relo = &prog->reloc_desc[j]; |
6992 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
6993 | | |
6994 | | /* mark the insn, so it's recognized by insn_is_pseudo_func() */ |
6995 | 0 | if (relo->type == RELO_SUBPROG_ADDR) |
6996 | 0 | insn[0].src_reg = BPF_PSEUDO_FUNC; |
6997 | 0 | } |
6998 | 0 | } |
6999 | | |
7000 | | /* relocate subprogram calls and append used subprograms to main |
7001 | | * programs; each copy of subprogram code needs to be relocated |
7002 | | * differently for each main program, because its code location might |
7003 | | * have changed. |
7004 | | * Append subprog relos to main programs to allow data relos to be |
7005 | | * processed after text is completely relocated. |
7006 | | */ |
7007 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7008 | 0 | prog = &obj->programs[i]; |
7009 | | /* sub-program's sub-calls are relocated within the context of |
7010 | | * its main program only |
7011 | | */ |
7012 | 0 | if (prog_is_subprog(obj, prog)) |
7013 | 0 | continue; |
7014 | 0 | if (!prog->autoload) |
7015 | 0 | continue; |
7016 | | |
7017 | 0 | err = bpf_object__relocate_calls(obj, prog); |
7018 | 0 | if (err) { |
7019 | 0 | pr_warn("prog '%s': failed to relocate calls: %d\n", |
7020 | 0 | prog->name, err); |
7021 | 0 | return err; |
7022 | 0 | } |
7023 | | |
7024 | 0 | err = bpf_prog_assign_exc_cb(obj, prog); |
7025 | 0 | if (err) |
7026 | 0 | return err; |
7027 | | /* Now, also append exception callback if it has not been done already. */ |
7028 | 0 | if (prog->exception_cb_idx >= 0) { |
7029 | 0 | struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx]; |
7030 | | |
7031 | | /* Calling exception callback directly is disallowed, which the |
7032 | | * verifier will reject later. In case it was processed already, |
7033 | | * we can skip this step, otherwise for all other valid cases we |
7034 | | * have to append exception callback now. |
7035 | | */ |
7036 | 0 | if (subprog->sub_insn_off == 0) { |
7037 | 0 | err = bpf_object__append_subprog_code(obj, prog, subprog); |
7038 | 0 | if (err) |
7039 | 0 | return err; |
7040 | 0 | err = bpf_object__reloc_code(obj, prog, subprog); |
7041 | 0 | if (err) |
7042 | 0 | return err; |
7043 | 0 | } |
7044 | 0 | } |
7045 | 0 | } |
7046 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7047 | 0 | prog = &obj->programs[i]; |
7048 | 0 | if (prog_is_subprog(obj, prog)) |
7049 | 0 | continue; |
7050 | 0 | if (!prog->autoload) |
7051 | 0 | continue; |
7052 | | |
7053 | | /* Process data relos for main programs */ |
7054 | 0 | err = bpf_object__relocate_data(obj, prog); |
7055 | 0 | if (err) { |
7056 | 0 | pr_warn("prog '%s': failed to relocate data references: %d\n", |
7057 | 0 | prog->name, err); |
7058 | 0 | return err; |
7059 | 0 | } |
7060 | | |
7061 | | /* Fix up .BTF.ext information, if necessary */ |
7062 | 0 | err = bpf_program_fixup_func_info(obj, prog); |
7063 | 0 | if (err) { |
7064 | 0 | pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %d\n", |
7065 | 0 | prog->name, err); |
7066 | 0 | return err; |
7067 | 0 | } |
7068 | 0 | } |
7069 | | |
7070 | 0 | return 0; |
7071 | 0 | } |
7072 | | |
7073 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
7074 | | Elf64_Shdr *shdr, Elf_Data *data); |
7075 | | |
7076 | | static int bpf_object__collect_map_relos(struct bpf_object *obj, |
7077 | | Elf64_Shdr *shdr, Elf_Data *data) |
7078 | 0 | { |
7079 | 0 | const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *); |
7080 | 0 | int i, j, nrels, new_sz; |
7081 | 0 | const struct btf_var_secinfo *vi = NULL; |
7082 | 0 | const struct btf_type *sec, *var, *def; |
7083 | 0 | struct bpf_map *map = NULL, *targ_map = NULL; |
7084 | 0 | struct bpf_program *targ_prog = NULL; |
7085 | 0 | bool is_prog_array, is_map_in_map; |
7086 | 0 | const struct btf_member *member; |
7087 | 0 | const char *name, *mname, *type; |
7088 | 0 | unsigned int moff; |
7089 | 0 | Elf64_Sym *sym; |
7090 | 0 | Elf64_Rel *rel; |
7091 | 0 | void *tmp; |
7092 | |
|
7093 | 0 | if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) |
7094 | 0 | return -EINVAL; |
7095 | 0 | sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); |
7096 | 0 | if (!sec) |
7097 | 0 | return -EINVAL; |
7098 | | |
7099 | 0 | nrels = shdr->sh_size / shdr->sh_entsize; |
7100 | 0 | for (i = 0; i < nrels; i++) { |
7101 | 0 | rel = elf_rel_by_idx(data, i); |
7102 | 0 | if (!rel) { |
7103 | 0 | pr_warn(".maps relo #%d: failed to get ELF relo\n", i); |
7104 | 0 | return -LIBBPF_ERRNO__FORMAT; |
7105 | 0 | } |
7106 | | |
7107 | 0 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
7108 | 0 | if (!sym) { |
7109 | 0 | pr_warn(".maps relo #%d: symbol %zx not found\n", |
7110 | 0 | i, (size_t)ELF64_R_SYM(rel->r_info)); |
7111 | 0 | return -LIBBPF_ERRNO__FORMAT; |
7112 | 0 | } |
7113 | 0 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
7114 | |
|
7115 | 0 | pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n", |
7116 | 0 | i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, |
7117 | 0 | (size_t)rel->r_offset, sym->st_name, name); |
7118 | |
|
7119 | 0 | for (j = 0; j < obj->nr_maps; j++) { |
7120 | 0 | map = &obj->maps[j]; |
7121 | 0 | if (map->sec_idx != obj->efile.btf_maps_shndx) |
7122 | 0 | continue; |
7123 | | |
7124 | 0 | vi = btf_var_secinfos(sec) + map->btf_var_idx; |
7125 | 0 | if (vi->offset <= rel->r_offset && |
7126 | 0 | rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) |
7127 | 0 | break; |
7128 | 0 | } |
7129 | 0 | if (j == obj->nr_maps) { |
7130 | 0 | pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", |
7131 | 0 | i, name, (size_t)rel->r_offset); |
7132 | 0 | return -EINVAL; |
7133 | 0 | } |
7134 | | |
7135 | 0 | is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); |
7136 | 0 | is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; |
7137 | 0 | type = is_map_in_map ? "map" : "prog"; |
7138 | 0 | if (is_map_in_map) { |
7139 | 0 | if (sym->st_shndx != obj->efile.btf_maps_shndx) { |
7140 | 0 | pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", |
7141 | 0 | i, name); |
7142 | 0 | return -LIBBPF_ERRNO__RELOC; |
7143 | 0 | } |
7144 | 0 | if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && |
7145 | 0 | map->def.key_size != sizeof(int)) { |
7146 | 0 | pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", |
7147 | 0 | i, map->name, sizeof(int)); |
7148 | 0 | return -EINVAL; |
7149 | 0 | } |
7150 | 0 | targ_map = bpf_object__find_map_by_name(obj, name); |
7151 | 0 | if (!targ_map) { |
7152 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n", |
7153 | 0 | i, name); |
7154 | 0 | return -ESRCH; |
7155 | 0 | } |
7156 | 0 | } else if (is_prog_array) { |
7157 | 0 | targ_prog = bpf_object__find_program_by_name(obj, name); |
7158 | 0 | if (!targ_prog) { |
7159 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n", |
7160 | 0 | i, name); |
7161 | 0 | return -ESRCH; |
7162 | 0 | } |
7163 | 0 | if (targ_prog->sec_idx != sym->st_shndx || |
7164 | 0 | targ_prog->sec_insn_off * 8 != sym->st_value || |
7165 | 0 | prog_is_subprog(obj, targ_prog)) { |
7166 | 0 | pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", |
7167 | 0 | i, name); |
7168 | 0 | return -LIBBPF_ERRNO__RELOC; |
7169 | 0 | } |
7170 | 0 | } else { |
7171 | 0 | return -EINVAL; |
7172 | 0 | } |
7173 | | |
7174 | 0 | var = btf__type_by_id(obj->btf, vi->type); |
7175 | 0 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
7176 | 0 | if (btf_vlen(def) == 0) |
7177 | 0 | return -EINVAL; |
7178 | 0 | member = btf_members(def) + btf_vlen(def) - 1; |
7179 | 0 | mname = btf__name_by_offset(obj->btf, member->name_off); |
7180 | 0 | if (strcmp(mname, "values")) |
7181 | 0 | return -EINVAL; |
7182 | | |
7183 | 0 | moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; |
7184 | 0 | if (rel->r_offset - vi->offset < moff) |
7185 | 0 | return -EINVAL; |
7186 | | |
7187 | 0 | moff = rel->r_offset - vi->offset - moff; |
7188 | | /* here we use BPF pointer size, which is always 64 bit, as we |
7189 | | * are parsing ELF that was built for BPF target |
7190 | | */ |
7191 | 0 | if (moff % bpf_ptr_sz) |
7192 | 0 | return -EINVAL; |
7193 | 0 | moff /= bpf_ptr_sz; |
7194 | 0 | if (moff >= map->init_slots_sz) { |
7195 | 0 | new_sz = moff + 1; |
7196 | 0 | tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); |
7197 | 0 | if (!tmp) |
7198 | 0 | return -ENOMEM; |
7199 | 0 | map->init_slots = tmp; |
7200 | 0 | memset(map->init_slots + map->init_slots_sz, 0, |
7201 | 0 | (new_sz - map->init_slots_sz) * host_ptr_sz); |
7202 | 0 | map->init_slots_sz = new_sz; |
7203 | 0 | } |
7204 | 0 | map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; |
7205 | |
|
7206 | 0 | pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n", |
7207 | 0 | i, map->name, moff, type, name); |
7208 | 0 | } |
7209 | | |
7210 | 0 | return 0; |
7211 | 0 | } |
7212 | | |
7213 | | static int bpf_object__collect_relos(struct bpf_object *obj) |
7214 | 1.94k | { |
7215 | 1.94k | int i, err; |
7216 | | |
7217 | 14.7k | for (i = 0; i < obj->efile.sec_cnt; i++) { |
7218 | 13.1k | struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; |
7219 | 13.1k | Elf64_Shdr *shdr; |
7220 | 13.1k | Elf_Data *data; |
7221 | 13.1k | int idx; |
7222 | | |
7223 | 13.1k | if (sec_desc->sec_type != SEC_RELO) |
7224 | 12.3k | continue; |
7225 | | |
7226 | 860 | shdr = sec_desc->shdr; |
7227 | 860 | data = sec_desc->data; |
7228 | 860 | idx = shdr->sh_info; |
7229 | | |
7230 | 860 | if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) { |
7231 | 0 | pr_warn("internal error at %d\n", __LINE__); |
7232 | 0 | return -LIBBPF_ERRNO__INTERNAL; |
7233 | 0 | } |
7234 | | |
7235 | 860 | if (obj->efile.secs[idx].sec_type == SEC_ST_OPS) |
7236 | 0 | err = bpf_object__collect_st_ops_relos(obj, shdr, data); |
7237 | 860 | else if (idx == obj->efile.btf_maps_shndx) |
7238 | 0 | err = bpf_object__collect_map_relos(obj, shdr, data); |
7239 | 860 | else |
7240 | 860 | err = bpf_object__collect_prog_relos(obj, shdr, data); |
7241 | 860 | if (err) |
7242 | 413 | return err; |
7243 | 860 | } |
7244 | | |
7245 | 1.52k | bpf_object__sort_relos(obj); |
7246 | 1.52k | return 0; |
7247 | 1.94k | } |
7248 | | |
7249 | | static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id) |
7250 | 0 | { |
7251 | 0 | if (BPF_CLASS(insn->code) == BPF_JMP && |
7252 | 0 | BPF_OP(insn->code) == BPF_CALL && |
7253 | 0 | BPF_SRC(insn->code) == BPF_K && |
7254 | 0 | insn->src_reg == 0 && |
7255 | 0 | insn->dst_reg == 0) { |
7256 | 0 | *func_id = insn->imm; |
7257 | 0 | return true; |
7258 | 0 | } |
7259 | 0 | return false; |
7260 | 0 | } |
7261 | | |
7262 | | static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog) |
7263 | 0 | { |
7264 | 0 | struct bpf_insn *insn = prog->insns; |
7265 | 0 | enum bpf_func_id func_id; |
7266 | 0 | int i; |
7267 | |
|
7268 | 0 | if (obj->gen_loader) |
7269 | 0 | return 0; |
7270 | | |
7271 | 0 | for (i = 0; i < prog->insns_cnt; i++, insn++) { |
7272 | 0 | if (!insn_is_helper_call(insn, &func_id)) |
7273 | 0 | continue; |
7274 | | |
7275 | | /* on kernels that don't yet support |
7276 | | * bpf_probe_read_{kernel,user}[_str] helpers, fall back |
7277 | | * to bpf_probe_read() which works well for old kernels |
7278 | | */ |
7279 | 0 | switch (func_id) { |
7280 | 0 | case BPF_FUNC_probe_read_kernel: |
7281 | 0 | case BPF_FUNC_probe_read_user: |
7282 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
7283 | 0 | insn->imm = BPF_FUNC_probe_read; |
7284 | 0 | break; |
7285 | 0 | case BPF_FUNC_probe_read_kernel_str: |
7286 | 0 | case BPF_FUNC_probe_read_user_str: |
7287 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
7288 | 0 | insn->imm = BPF_FUNC_probe_read_str; |
7289 | 0 | break; |
7290 | 0 | default: |
7291 | 0 | break; |
7292 | 0 | } |
7293 | 0 | } |
7294 | 0 | return 0; |
7295 | 0 | } |
7296 | | |
7297 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
7298 | | int *btf_obj_fd, int *btf_type_id); |
7299 | | |
7300 | | /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */ |
7301 | | static int libbpf_prepare_prog_load(struct bpf_program *prog, |
7302 | | struct bpf_prog_load_opts *opts, long cookie) |
7303 | 0 | { |
7304 | 0 | enum sec_def_flags def = cookie; |
7305 | | |
7306 | | /* old kernels might not support specifying expected_attach_type */ |
7307 | 0 | if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) |
7308 | 0 | opts->expected_attach_type = 0; |
7309 | |
|
7310 | 0 | if (def & SEC_SLEEPABLE) |
7311 | 0 | opts->prog_flags |= BPF_F_SLEEPABLE; |
7312 | |
|
7313 | 0 | if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) |
7314 | 0 | opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; |
7315 | | |
7316 | | /* special check for usdt to use uprobe_multi link */ |
7317 | 0 | if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) |
7318 | 0 | prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; |
7319 | |
|
7320 | 0 | if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { |
7321 | 0 | int btf_obj_fd = 0, btf_type_id = 0, err; |
7322 | 0 | const char *attach_name; |
7323 | |
|
7324 | 0 | attach_name = strchr(prog->sec_name, '/'); |
7325 | 0 | if (!attach_name) { |
7326 | | /* if BPF program is annotated with just SEC("fentry") |
7327 | | * (or similar) without declaratively specifying |
7328 | | * target, then it is expected that target will be |
7329 | | * specified with bpf_program__set_attach_target() at |
7330 | | * runtime before BPF object load step. If not, then |
7331 | | * there is nothing to load into the kernel as BPF |
7332 | | * verifier won't be able to validate BPF program |
7333 | | * correctness anyways. |
7334 | | */ |
7335 | 0 | pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n", |
7336 | 0 | prog->name); |
7337 | 0 | return -EINVAL; |
7338 | 0 | } |
7339 | 0 | attach_name++; /* skip over / */ |
7340 | |
|
7341 | 0 | err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id); |
7342 | 0 | if (err) |
7343 | 0 | return err; |
7344 | | |
7345 | | /* cache resolved BTF FD and BTF type ID in the prog */ |
7346 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
7347 | 0 | prog->attach_btf_id = btf_type_id; |
7348 | | |
7349 | | /* but by now libbpf common logic is not utilizing |
7350 | | * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because |
7351 | | * this callback is called after opts were populated by |
7352 | | * libbpf, so this callback has to update opts explicitly here |
7353 | | */ |
7354 | 0 | opts->attach_btf_obj_fd = btf_obj_fd; |
7355 | 0 | opts->attach_btf_id = btf_type_id; |
7356 | 0 | } |
7357 | 0 | return 0; |
7358 | 0 | } |
7359 | | |
7360 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz); |
7361 | | |
7362 | | static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog, |
7363 | | struct bpf_insn *insns, int insns_cnt, |
7364 | | const char *license, __u32 kern_version, int *prog_fd) |
7365 | 0 | { |
7366 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, load_attr); |
7367 | 0 | const char *prog_name = NULL; |
7368 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
7369 | 0 | size_t log_buf_size = 0; |
7370 | 0 | char *log_buf = NULL, *tmp; |
7371 | 0 | bool own_log_buf = true; |
7372 | 0 | __u32 log_level = prog->log_level; |
7373 | 0 | int ret, err; |
7374 | | |
7375 | | /* Be more helpful by rejecting programs that can't be validated early |
7376 | | * with more meaningful and actionable error message. |
7377 | | */ |
7378 | 0 | switch (prog->type) { |
7379 | 0 | case BPF_PROG_TYPE_UNSPEC: |
7380 | | /* |
7381 | | * The program type must be set. Most likely we couldn't find a proper |
7382 | | * section definition at load time, and thus we didn't infer the type. |
7383 | | */ |
7384 | 0 | pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n", |
7385 | 0 | prog->name, prog->sec_name); |
7386 | 0 | return -EINVAL; |
7387 | 0 | case BPF_PROG_TYPE_STRUCT_OPS: |
7388 | 0 | if (prog->attach_btf_id == 0) { |
7389 | 0 | pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n", |
7390 | 0 | prog->name); |
7391 | 0 | return -EINVAL; |
7392 | 0 | } |
7393 | 0 | break; |
7394 | 0 | default: |
7395 | 0 | break; |
7396 | 0 | } |
7397 | | |
7398 | 0 | if (!insns || !insns_cnt) |
7399 | 0 | return -EINVAL; |
7400 | | |
7401 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
7402 | 0 | prog_name = prog->name; |
7403 | 0 | load_attr.attach_prog_fd = prog->attach_prog_fd; |
7404 | 0 | load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; |
7405 | 0 | load_attr.attach_btf_id = prog->attach_btf_id; |
7406 | 0 | load_attr.kern_version = kern_version; |
7407 | 0 | load_attr.prog_ifindex = prog->prog_ifindex; |
7408 | | |
7409 | | /* specify func_info/line_info only if kernel supports them */ |
7410 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) { |
7411 | 0 | load_attr.prog_btf_fd = btf__fd(obj->btf); |
7412 | 0 | load_attr.func_info = prog->func_info; |
7413 | 0 | load_attr.func_info_rec_size = prog->func_info_rec_size; |
7414 | 0 | load_attr.func_info_cnt = prog->func_info_cnt; |
7415 | 0 | load_attr.line_info = prog->line_info; |
7416 | 0 | load_attr.line_info_rec_size = prog->line_info_rec_size; |
7417 | 0 | load_attr.line_info_cnt = prog->line_info_cnt; |
7418 | 0 | } |
7419 | 0 | load_attr.log_level = log_level; |
7420 | 0 | load_attr.prog_flags = prog->prog_flags; |
7421 | 0 | load_attr.fd_array = obj->fd_array; |
7422 | |
|
7423 | 0 | load_attr.token_fd = obj->token_fd; |
7424 | 0 | if (obj->token_fd) |
7425 | 0 | load_attr.prog_flags |= BPF_F_TOKEN_FD; |
7426 | | |
7427 | | /* adjust load_attr if sec_def provides custom preload callback */ |
7428 | 0 | if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { |
7429 | 0 | err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); |
7430 | 0 | if (err < 0) { |
7431 | 0 | pr_warn("prog '%s': failed to prepare load attributes: %d\n", |
7432 | 0 | prog->name, err); |
7433 | 0 | return err; |
7434 | 0 | } |
7435 | 0 | insns = prog->insns; |
7436 | 0 | insns_cnt = prog->insns_cnt; |
7437 | 0 | } |
7438 | | |
7439 | | /* allow prog_prepare_load_fn to change expected_attach_type */ |
7440 | 0 | load_attr.expected_attach_type = prog->expected_attach_type; |
7441 | |
|
7442 | 0 | if (obj->gen_loader) { |
7443 | 0 | bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, |
7444 | 0 | license, insns, insns_cnt, &load_attr, |
7445 | 0 | prog - obj->programs); |
7446 | 0 | *prog_fd = -1; |
7447 | 0 | return 0; |
7448 | 0 | } |
7449 | | |
7450 | 0 | retry_load: |
7451 | | /* if log_level is zero, we don't request logs initially even if |
7452 | | * custom log_buf is specified; if the program load fails, then we'll |
7453 | | * bump log_level to 1 and use either custom log_buf or we'll allocate |
7454 | | * our own and retry the load to get details on what failed |
7455 | | */ |
7456 | 0 | if (log_level) { |
7457 | 0 | if (prog->log_buf) { |
7458 | 0 | log_buf = prog->log_buf; |
7459 | 0 | log_buf_size = prog->log_size; |
7460 | 0 | own_log_buf = false; |
7461 | 0 | } else if (obj->log_buf) { |
7462 | 0 | log_buf = obj->log_buf; |
7463 | 0 | log_buf_size = obj->log_size; |
7464 | 0 | own_log_buf = false; |
7465 | 0 | } else { |
7466 | 0 | log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2); |
7467 | 0 | tmp = realloc(log_buf, log_buf_size); |
7468 | 0 | if (!tmp) { |
7469 | 0 | ret = -ENOMEM; |
7470 | 0 | goto out; |
7471 | 0 | } |
7472 | 0 | log_buf = tmp; |
7473 | 0 | log_buf[0] = '\0'; |
7474 | 0 | own_log_buf = true; |
7475 | 0 | } |
7476 | 0 | } |
7477 | | |
7478 | 0 | load_attr.log_buf = log_buf; |
7479 | 0 | load_attr.log_size = log_buf_size; |
7480 | 0 | load_attr.log_level = log_level; |
7481 | |
|
7482 | 0 | ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); |
7483 | 0 | if (ret >= 0) { |
7484 | 0 | if (log_level && own_log_buf) { |
7485 | 0 | pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
7486 | 0 | prog->name, log_buf); |
7487 | 0 | } |
7488 | |
|
7489 | 0 | if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { |
7490 | 0 | struct bpf_map *map; |
7491 | 0 | int i; |
7492 | |
|
7493 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
7494 | 0 | map = &prog->obj->maps[i]; |
7495 | 0 | if (map->libbpf_type != LIBBPF_MAP_RODATA) |
7496 | 0 | continue; |
7497 | | |
7498 | 0 | if (bpf_prog_bind_map(ret, map->fd, NULL)) { |
7499 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
7500 | 0 | pr_warn("prog '%s': failed to bind map '%s': %s\n", |
7501 | 0 | prog->name, map->real_name, cp); |
7502 | | /* Don't fail hard if can't bind rodata. */ |
7503 | 0 | } |
7504 | 0 | } |
7505 | 0 | } |
7506 | |
|
7507 | 0 | *prog_fd = ret; |
7508 | 0 | ret = 0; |
7509 | 0 | goto out; |
7510 | 0 | } |
7511 | | |
7512 | 0 | if (log_level == 0) { |
7513 | 0 | log_level = 1; |
7514 | 0 | goto retry_load; |
7515 | 0 | } |
7516 | | /* On ENOSPC, increase log buffer size and retry, unless custom |
7517 | | * log_buf is specified. |
7518 | | * Be careful to not overflow u32, though. Kernel's log buf size limit |
7519 | | * isn't part of UAPI so it can always be bumped to full 4GB. So don't |
7520 | | * multiply by 2 unless we are sure we'll fit within 32 bits. |
7521 | | * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). |
7522 | | */ |
7523 | 0 | if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2) |
7524 | 0 | goto retry_load; |
7525 | | |
7526 | 0 | ret = -errno; |
7527 | | |
7528 | | /* post-process verifier log to improve error descriptions */ |
7529 | 0 | fixup_verifier_log(prog, log_buf, log_buf_size); |
7530 | |
|
7531 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
7532 | 0 | pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp); |
7533 | 0 | pr_perm_msg(ret); |
7534 | |
|
7535 | 0 | if (own_log_buf && log_buf && log_buf[0] != '\0') { |
7536 | 0 | pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
7537 | 0 | prog->name, log_buf); |
7538 | 0 | } |
7539 | |
|
7540 | 0 | out: |
7541 | 0 | if (own_log_buf) |
7542 | 0 | free(log_buf); |
7543 | 0 | return ret; |
7544 | 0 | } |
7545 | | |
7546 | | static char *find_prev_line(char *buf, char *cur) |
7547 | 0 | { |
7548 | 0 | char *p; |
7549 | |
|
7550 | 0 | if (cur == buf) /* end of a log buf */ |
7551 | 0 | return NULL; |
7552 | | |
7553 | 0 | p = cur - 1; |
7554 | 0 | while (p - 1 >= buf && *(p - 1) != '\n') |
7555 | 0 | p--; |
7556 | |
|
7557 | 0 | return p; |
7558 | 0 | } |
7559 | | |
7560 | | static void patch_log(char *buf, size_t buf_sz, size_t log_sz, |
7561 | | char *orig, size_t orig_sz, const char *patch) |
7562 | 0 | { |
7563 | | /* size of the remaining log content to the right from the to-be-replaced part */ |
7564 | 0 | size_t rem_sz = (buf + log_sz) - (orig + orig_sz); |
7565 | 0 | size_t patch_sz = strlen(patch); |
7566 | |
|
7567 | 0 | if (patch_sz != orig_sz) { |
7568 | | /* If patch line(s) are longer than original piece of verifier log, |
7569 | | * shift log contents by (patch_sz - orig_sz) bytes to the right |
7570 | | * starting from after to-be-replaced part of the log. |
7571 | | * |
7572 | | * If patch line(s) are shorter than original piece of verifier log, |
7573 | | * shift log contents by (orig_sz - patch_sz) bytes to the left |
7574 | | * starting from after to-be-replaced part of the log |
7575 | | * |
7576 | | * We need to be careful about not overflowing available |
7577 | | * buf_sz capacity. If that's the case, we'll truncate the end |
7578 | | * of the original log, as necessary. |
7579 | | */ |
7580 | 0 | if (patch_sz > orig_sz) { |
7581 | 0 | if (orig + patch_sz >= buf + buf_sz) { |
7582 | | /* patch is big enough to cover remaining space completely */ |
7583 | 0 | patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; |
7584 | 0 | rem_sz = 0; |
7585 | 0 | } else if (patch_sz - orig_sz > buf_sz - log_sz) { |
7586 | | /* patch causes part of remaining log to be truncated */ |
7587 | 0 | rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); |
7588 | 0 | } |
7589 | 0 | } |
7590 | | /* shift remaining log to the right by calculated amount */ |
7591 | 0 | memmove(orig + patch_sz, orig + orig_sz, rem_sz); |
7592 | 0 | } |
7593 | |
|
7594 | 0 | memcpy(orig, patch, patch_sz); |
7595 | 0 | } |
7596 | | |
7597 | | static void fixup_log_failed_core_relo(struct bpf_program *prog, |
7598 | | char *buf, size_t buf_sz, size_t log_sz, |
7599 | | char *line1, char *line2, char *line3) |
7600 | 0 | { |
7601 | | /* Expected log for failed and not properly guarded CO-RE relocation: |
7602 | | * line1 -> 123: (85) call unknown#195896080 |
7603 | | * line2 -> invalid func unknown#195896080 |
7604 | | * line3 -> <anything else or end of buffer> |
7605 | | * |
7606 | | * "123" is the index of the instruction that was poisoned. We extract |
7607 | | * instruction index to find corresponding CO-RE relocation and |
7608 | | * replace this part of the log with more relevant information about |
7609 | | * failed CO-RE relocation. |
7610 | | */ |
7611 | 0 | const struct bpf_core_relo *relo; |
7612 | 0 | struct bpf_core_spec spec; |
7613 | 0 | char patch[512], spec_buf[256]; |
7614 | 0 | int insn_idx, err, spec_len; |
7615 | |
|
7616 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1) |
7617 | 0 | return; |
7618 | | |
7619 | 0 | relo = find_relo_core(prog, insn_idx); |
7620 | 0 | if (!relo) |
7621 | 0 | return; |
7622 | | |
7623 | 0 | err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); |
7624 | 0 | if (err) |
7625 | 0 | return; |
7626 | | |
7627 | 0 | spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec); |
7628 | 0 | snprintf(patch, sizeof(patch), |
7629 | 0 | "%d: <invalid CO-RE relocation>\n" |
7630 | 0 | "failed to resolve CO-RE relocation %s%s\n", |
7631 | 0 | insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : ""); |
7632 | |
|
7633 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7634 | 0 | } |
7635 | | |
7636 | | static void fixup_log_missing_map_load(struct bpf_program *prog, |
7637 | | char *buf, size_t buf_sz, size_t log_sz, |
7638 | | char *line1, char *line2, char *line3) |
7639 | 0 | { |
7640 | | /* Expected log for failed and not properly guarded map reference: |
7641 | | * line1 -> 123: (85) call unknown#2001000345 |
7642 | | * line2 -> invalid func unknown#2001000345 |
7643 | | * line3 -> <anything else or end of buffer> |
7644 | | * |
7645 | | * "123" is the index of the instruction that was poisoned. |
7646 | | * "345" in "2001000345" is a map index in obj->maps to fetch map name. |
7647 | | */ |
7648 | 0 | struct bpf_object *obj = prog->obj; |
7649 | 0 | const struct bpf_map *map; |
7650 | 0 | int insn_idx, map_idx; |
7651 | 0 | char patch[128]; |
7652 | |
|
7653 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2) |
7654 | 0 | return; |
7655 | | |
7656 | 0 | map_idx -= POISON_LDIMM64_MAP_BASE; |
7657 | 0 | if (map_idx < 0 || map_idx >= obj->nr_maps) |
7658 | 0 | return; |
7659 | 0 | map = &obj->maps[map_idx]; |
7660 | |
|
7661 | 0 | snprintf(patch, sizeof(patch), |
7662 | 0 | "%d: <invalid BPF map reference>\n" |
7663 | 0 | "BPF map '%s' is referenced but wasn't created\n", |
7664 | 0 | insn_idx, map->name); |
7665 | |
|
7666 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7667 | 0 | } |
7668 | | |
7669 | | static void fixup_log_missing_kfunc_call(struct bpf_program *prog, |
7670 | | char *buf, size_t buf_sz, size_t log_sz, |
7671 | | char *line1, char *line2, char *line3) |
7672 | 0 | { |
7673 | | /* Expected log for failed and not properly guarded kfunc call: |
7674 | | * line1 -> 123: (85) call unknown#2002000345 |
7675 | | * line2 -> invalid func unknown#2002000345 |
7676 | | * line3 -> <anything else or end of buffer> |
7677 | | * |
7678 | | * "123" is the index of the instruction that was poisoned. |
7679 | | * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. |
7680 | | */ |
7681 | 0 | struct bpf_object *obj = prog->obj; |
7682 | 0 | const struct extern_desc *ext; |
7683 | 0 | int insn_idx, ext_idx; |
7684 | 0 | char patch[128]; |
7685 | |
|
7686 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2) |
7687 | 0 | return; |
7688 | | |
7689 | 0 | ext_idx -= POISON_CALL_KFUNC_BASE; |
7690 | 0 | if (ext_idx < 0 || ext_idx >= obj->nr_extern) |
7691 | 0 | return; |
7692 | 0 | ext = &obj->externs[ext_idx]; |
7693 | |
|
7694 | 0 | snprintf(patch, sizeof(patch), |
7695 | 0 | "%d: <invalid kfunc call>\n" |
7696 | 0 | "kfunc '%s' is referenced but wasn't resolved\n", |
7697 | 0 | insn_idx, ext->name); |
7698 | |
|
7699 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7700 | 0 | } |
7701 | | |
7702 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz) |
7703 | 0 | { |
7704 | | /* look for familiar error patterns in last N lines of the log */ |
7705 | 0 | const size_t max_last_line_cnt = 10; |
7706 | 0 | char *prev_line, *cur_line, *next_line; |
7707 | 0 | size_t log_sz; |
7708 | 0 | int i; |
7709 | |
|
7710 | 0 | if (!buf) |
7711 | 0 | return; |
7712 | | |
7713 | 0 | log_sz = strlen(buf) + 1; |
7714 | 0 | next_line = buf + log_sz - 1; |
7715 | |
|
7716 | 0 | for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) { |
7717 | 0 | cur_line = find_prev_line(buf, next_line); |
7718 | 0 | if (!cur_line) |
7719 | 0 | return; |
7720 | | |
7721 | 0 | if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) { |
7722 | 0 | prev_line = find_prev_line(buf, cur_line); |
7723 | 0 | if (!prev_line) |
7724 | 0 | continue; |
7725 | | |
7726 | | /* failed CO-RE relocation case */ |
7727 | 0 | fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz, |
7728 | 0 | prev_line, cur_line, next_line); |
7729 | 0 | return; |
7730 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) { |
7731 | 0 | prev_line = find_prev_line(buf, cur_line); |
7732 | 0 | if (!prev_line) |
7733 | 0 | continue; |
7734 | | |
7735 | | /* reference to uncreated BPF map */ |
7736 | 0 | fixup_log_missing_map_load(prog, buf, buf_sz, log_sz, |
7737 | 0 | prev_line, cur_line, next_line); |
7738 | 0 | return; |
7739 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) { |
7740 | 0 | prev_line = find_prev_line(buf, cur_line); |
7741 | 0 | if (!prev_line) |
7742 | 0 | continue; |
7743 | | |
7744 | | /* reference to unresolved kfunc */ |
7745 | 0 | fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz, |
7746 | 0 | prev_line, cur_line, next_line); |
7747 | 0 | return; |
7748 | 0 | } |
7749 | 0 | } |
7750 | 0 | } |
7751 | | |
7752 | | static int bpf_program_record_relos(struct bpf_program *prog) |
7753 | 0 | { |
7754 | 0 | struct bpf_object *obj = prog->obj; |
7755 | 0 | int i; |
7756 | |
|
7757 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
7758 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
7759 | 0 | struct extern_desc *ext = &obj->externs[relo->ext_idx]; |
7760 | 0 | int kind; |
7761 | |
|
7762 | 0 | switch (relo->type) { |
7763 | 0 | case RELO_EXTERN_LD64: |
7764 | 0 | if (ext->type != EXT_KSYM) |
7765 | 0 | continue; |
7766 | 0 | kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? |
7767 | 0 | BTF_KIND_VAR : BTF_KIND_FUNC; |
7768 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
7769 | 0 | ext->is_weak, !ext->ksym.type_id, |
7770 | 0 | true, kind, relo->insn_idx); |
7771 | 0 | break; |
7772 | 0 | case RELO_EXTERN_CALL: |
7773 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
7774 | 0 | ext->is_weak, false, false, BTF_KIND_FUNC, |
7775 | 0 | relo->insn_idx); |
7776 | 0 | break; |
7777 | 0 | case RELO_CORE: { |
7778 | 0 | struct bpf_core_relo cr = { |
7779 | 0 | .insn_off = relo->insn_idx * 8, |
7780 | 0 | .type_id = relo->core_relo->type_id, |
7781 | 0 | .access_str_off = relo->core_relo->access_str_off, |
7782 | 0 | .kind = relo->core_relo->kind, |
7783 | 0 | }; |
7784 | |
|
7785 | 0 | bpf_gen__record_relo_core(obj->gen_loader, &cr); |
7786 | 0 | break; |
7787 | 0 | } |
7788 | 0 | default: |
7789 | 0 | continue; |
7790 | 0 | } |
7791 | 0 | } |
7792 | 0 | return 0; |
7793 | 0 | } |
7794 | | |
7795 | | static int |
7796 | | bpf_object__load_progs(struct bpf_object *obj, int log_level) |
7797 | 0 | { |
7798 | 0 | struct bpf_program *prog; |
7799 | 0 | size_t i; |
7800 | 0 | int err; |
7801 | |
|
7802 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7803 | 0 | prog = &obj->programs[i]; |
7804 | 0 | err = bpf_object__sanitize_prog(obj, prog); |
7805 | 0 | if (err) |
7806 | 0 | return err; |
7807 | 0 | } |
7808 | | |
7809 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7810 | 0 | prog = &obj->programs[i]; |
7811 | 0 | if (prog_is_subprog(obj, prog)) |
7812 | 0 | continue; |
7813 | 0 | if (!prog->autoload) { |
7814 | 0 | pr_debug("prog '%s': skipped loading\n", prog->name); |
7815 | 0 | continue; |
7816 | 0 | } |
7817 | 0 | prog->log_level |= log_level; |
7818 | |
|
7819 | 0 | if (obj->gen_loader) |
7820 | 0 | bpf_program_record_relos(prog); |
7821 | |
|
7822 | 0 | err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, |
7823 | 0 | obj->license, obj->kern_version, &prog->fd); |
7824 | 0 | if (err) { |
7825 | 0 | pr_warn("prog '%s': failed to load: %d\n", prog->name, err); |
7826 | 0 | return err; |
7827 | 0 | } |
7828 | 0 | } |
7829 | | |
7830 | 0 | bpf_object__free_relocs(obj); |
7831 | 0 | return 0; |
7832 | 0 | } |
7833 | | |
7834 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name); |
7835 | | |
7836 | | static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts) |
7837 | 1.94k | { |
7838 | 1.94k | struct bpf_program *prog; |
7839 | 1.94k | int err; |
7840 | | |
7841 | 10.7k | bpf_object__for_each_program(prog, obj) { |
7842 | 10.7k | prog->sec_def = find_sec_def(prog->sec_name); |
7843 | 10.7k | if (!prog->sec_def) { |
7844 | | /* couldn't guess, but user might manually specify */ |
7845 | 9.50k | pr_debug("prog '%s': unrecognized ELF section name '%s'\n", |
7846 | 9.50k | prog->name, prog->sec_name); |
7847 | 9.50k | continue; |
7848 | 9.50k | } |
7849 | | |
7850 | 1.24k | prog->type = prog->sec_def->prog_type; |
7851 | 1.24k | prog->expected_attach_type = prog->sec_def->expected_attach_type; |
7852 | | |
7853 | | /* sec_def can have custom callback which should be called |
7854 | | * after bpf_program is initialized to adjust its properties |
7855 | | */ |
7856 | 1.24k | if (prog->sec_def->prog_setup_fn) { |
7857 | 0 | err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); |
7858 | 0 | if (err < 0) { |
7859 | 0 | pr_warn("prog '%s': failed to initialize: %d\n", |
7860 | 0 | prog->name, err); |
7861 | 0 | return err; |
7862 | 0 | } |
7863 | 0 | } |
7864 | 1.24k | } |
7865 | | |
7866 | 1.94k | return 0; |
7867 | 1.94k | } |
7868 | | |
7869 | | static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, |
7870 | | const struct bpf_object_open_opts *opts) |
7871 | 11.0k | { |
7872 | 11.0k | const char *obj_name, *kconfig, *btf_tmp_path, *token_path; |
7873 | 11.0k | struct bpf_object *obj; |
7874 | 11.0k | char tmp_name[64]; |
7875 | 11.0k | int err; |
7876 | 11.0k | char *log_buf; |
7877 | 11.0k | size_t log_size; |
7878 | 11.0k | __u32 log_level; |
7879 | | |
7880 | 11.0k | if (elf_version(EV_CURRENT) == EV_NONE) { |
7881 | 0 | pr_warn("failed to init libelf for %s\n", |
7882 | 0 | path ? : "(mem buf)"); |
7883 | 0 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
7884 | 0 | } |
7885 | | |
7886 | 11.0k | if (!OPTS_VALID(opts, bpf_object_open_opts)) |
7887 | 0 | return ERR_PTR(-EINVAL); |
7888 | | |
7889 | 11.0k | obj_name = OPTS_GET(opts, object_name, NULL); |
7890 | 11.0k | if (obj_buf) { |
7891 | 11.0k | if (!obj_name) { |
7892 | 0 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
7893 | 0 | (unsigned long)obj_buf, |
7894 | 0 | (unsigned long)obj_buf_sz); |
7895 | 0 | obj_name = tmp_name; |
7896 | 0 | } |
7897 | 11.0k | path = obj_name; |
7898 | 11.0k | pr_debug("loading object '%s' from buffer\n", obj_name); |
7899 | 11.0k | } |
7900 | | |
7901 | 11.0k | log_buf = OPTS_GET(opts, kernel_log_buf, NULL); |
7902 | 11.0k | log_size = OPTS_GET(opts, kernel_log_size, 0); |
7903 | 11.0k | log_level = OPTS_GET(opts, kernel_log_level, 0); |
7904 | 11.0k | if (log_size > UINT_MAX) |
7905 | 0 | return ERR_PTR(-EINVAL); |
7906 | 11.0k | if (log_size && !log_buf) |
7907 | 0 | return ERR_PTR(-EINVAL); |
7908 | | |
7909 | 11.0k | token_path = OPTS_GET(opts, bpf_token_path, NULL); |
7910 | | /* if user didn't specify bpf_token_path explicitly, check if |
7911 | | * LIBBPF_BPF_TOKEN_PATH envvar was set and treat it as bpf_token_path |
7912 | | * option |
7913 | | */ |
7914 | 11.0k | if (!token_path) |
7915 | 11.0k | token_path = getenv("LIBBPF_BPF_TOKEN_PATH"); |
7916 | 11.0k | if (token_path && strlen(token_path) >= PATH_MAX) |
7917 | 0 | return ERR_PTR(-ENAMETOOLONG); |
7918 | | |
7919 | 11.0k | obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name); |
7920 | 11.0k | if (IS_ERR(obj)) |
7921 | 0 | return obj; |
7922 | | |
7923 | 11.0k | obj->log_buf = log_buf; |
7924 | 11.0k | obj->log_size = log_size; |
7925 | 11.0k | obj->log_level = log_level; |
7926 | | |
7927 | 11.0k | if (token_path) { |
7928 | 0 | obj->token_path = strdup(token_path); |
7929 | 0 | if (!obj->token_path) { |
7930 | 0 | err = -ENOMEM; |
7931 | 0 | goto out; |
7932 | 0 | } |
7933 | 0 | } |
7934 | | |
7935 | 11.0k | btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL); |
7936 | 11.0k | if (btf_tmp_path) { |
7937 | 0 | if (strlen(btf_tmp_path) >= PATH_MAX) { |
7938 | 0 | err = -ENAMETOOLONG; |
7939 | 0 | goto out; |
7940 | 0 | } |
7941 | 0 | obj->btf_custom_path = strdup(btf_tmp_path); |
7942 | 0 | if (!obj->btf_custom_path) { |
7943 | 0 | err = -ENOMEM; |
7944 | 0 | goto out; |
7945 | 0 | } |
7946 | 0 | } |
7947 | | |
7948 | 11.0k | kconfig = OPTS_GET(opts, kconfig, NULL); |
7949 | 11.0k | if (kconfig) { |
7950 | 0 | obj->kconfig = strdup(kconfig); |
7951 | 0 | if (!obj->kconfig) { |
7952 | 0 | err = -ENOMEM; |
7953 | 0 | goto out; |
7954 | 0 | } |
7955 | 0 | } |
7956 | | |
7957 | 11.0k | err = bpf_object__elf_init(obj); |
7958 | 11.0k | err = err ? : bpf_object__check_endianness(obj); |
7959 | 11.0k | err = err ? : bpf_object__elf_collect(obj); |
7960 | 8.82k | err = err ? : bpf_object__collect_externs(obj); |
7961 | 18.4E | err = err ? : bpf_object_fixup_btf(obj); |
7962 | 18.4E | err = err ? : bpf_object__init_maps(obj, opts); |
7963 | 18.4E | err = err ? : bpf_object_init_progs(obj, opts); |
7964 | 18.4E | err = err ? : bpf_object__collect_relos(obj); |
7965 | 18.4E | if (err) |
7966 | 9.53k | goto out; |
7967 | | |
7968 | 18.4E | bpf_object__elf_finish(obj); |
7969 | | |
7970 | 18.4E | return obj; |
7971 | 9.53k | out: |
7972 | 9.53k | bpf_object__close(obj); |
7973 | 9.53k | return ERR_PTR(err); |
7974 | 18.4E | } |
7975 | | |
7976 | | struct bpf_object * |
7977 | | bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts) |
7978 | 0 | { |
7979 | 0 | if (!path) |
7980 | 0 | return libbpf_err_ptr(-EINVAL); |
7981 | | |
7982 | 0 | pr_debug("loading %s\n", path); |
7983 | |
|
7984 | 0 | return libbpf_ptr(bpf_object_open(path, NULL, 0, opts)); |
7985 | 0 | } |
7986 | | |
7987 | | struct bpf_object *bpf_object__open(const char *path) |
7988 | 0 | { |
7989 | 0 | return bpf_object__open_file(path, NULL); |
7990 | 0 | } |
7991 | | |
7992 | | struct bpf_object * |
7993 | | bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, |
7994 | | const struct bpf_object_open_opts *opts) |
7995 | 11.0k | { |
7996 | 11.0k | if (!obj_buf || obj_buf_sz == 0) |
7997 | 0 | return libbpf_err_ptr(-EINVAL); |
7998 | | |
7999 | 11.0k | return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts)); |
8000 | 11.0k | } |
8001 | | |
8002 | | static int bpf_object_unload(struct bpf_object *obj) |
8003 | 11.0k | { |
8004 | 11.0k | size_t i; |
8005 | | |
8006 | 11.0k | if (!obj) |
8007 | 0 | return libbpf_err(-EINVAL); |
8008 | | |
8009 | 13.8k | for (i = 0; i < obj->nr_maps; i++) { |
8010 | 2.75k | zclose(obj->maps[i].fd); |
8011 | 2.75k | if (obj->maps[i].st_ops) |
8012 | 48 | zfree(&obj->maps[i].st_ops->kern_vdata); |
8013 | 2.75k | } |
8014 | | |
8015 | 25.4k | for (i = 0; i < obj->nr_programs; i++) |
8016 | 14.3k | bpf_program__unload(&obj->programs[i]); |
8017 | | |
8018 | 11.0k | return 0; |
8019 | 11.0k | } |
8020 | | |
8021 | | static int bpf_object__sanitize_maps(struct bpf_object *obj) |
8022 | 0 | { |
8023 | 0 | struct bpf_map *m; |
8024 | |
|
8025 | 0 | bpf_object__for_each_map(m, obj) { |
8026 | 0 | if (!bpf_map__is_internal(m)) |
8027 | 0 | continue; |
8028 | 0 | if (!kernel_supports(obj, FEAT_ARRAY_MMAP)) |
8029 | 0 | m->def.map_flags &= ~BPF_F_MMAPABLE; |
8030 | 0 | } |
8031 | |
|
8032 | 0 | return 0; |
8033 | 0 | } |
8034 | | |
8035 | | typedef int (*kallsyms_cb_t)(unsigned long long sym_addr, char sym_type, |
8036 | | const char *sym_name, void *ctx); |
8037 | | |
8038 | | static int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx) |
8039 | 0 | { |
8040 | 0 | char sym_type, sym_name[500]; |
8041 | 0 | unsigned long long sym_addr; |
8042 | 0 | int ret, err = 0; |
8043 | 0 | FILE *f; |
8044 | |
|
8045 | 0 | f = fopen("/proc/kallsyms", "re"); |
8046 | 0 | if (!f) { |
8047 | 0 | err = -errno; |
8048 | 0 | pr_warn("failed to open /proc/kallsyms: %d\n", err); |
8049 | 0 | return err; |
8050 | 0 | } |
8051 | | |
8052 | 0 | while (true) { |
8053 | 0 | ret = fscanf(f, "%llx %c %499s%*[^\n]\n", |
8054 | 0 | &sym_addr, &sym_type, sym_name); |
8055 | 0 | if (ret == EOF && feof(f)) |
8056 | 0 | break; |
8057 | 0 | if (ret != 3) { |
8058 | 0 | pr_warn("failed to read kallsyms entry: %d\n", ret); |
8059 | 0 | err = -EINVAL; |
8060 | 0 | break; |
8061 | 0 | } |
8062 | | |
8063 | 0 | err = cb(sym_addr, sym_type, sym_name, ctx); |
8064 | 0 | if (err) |
8065 | 0 | break; |
8066 | 0 | } |
8067 | |
|
8068 | 0 | fclose(f); |
8069 | 0 | return err; |
8070 | 0 | } |
8071 | | |
8072 | | static int kallsyms_cb(unsigned long long sym_addr, char sym_type, |
8073 | | const char *sym_name, void *ctx) |
8074 | 0 | { |
8075 | 0 | struct bpf_object *obj = ctx; |
8076 | 0 | const struct btf_type *t; |
8077 | 0 | struct extern_desc *ext; |
8078 | 0 | char *res; |
8079 | |
|
8080 | 0 | res = strstr(sym_name, ".llvm."); |
8081 | 0 | if (sym_type == 'd' && res) |
8082 | 0 | ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name); |
8083 | 0 | else |
8084 | 0 | ext = find_extern_by_name(obj, sym_name); |
8085 | 0 | if (!ext || ext->type != EXT_KSYM) |
8086 | 0 | return 0; |
8087 | | |
8088 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
8089 | 0 | if (!btf_is_var(t)) |
8090 | 0 | return 0; |
8091 | | |
8092 | 0 | if (ext->is_set && ext->ksym.addr != sym_addr) { |
8093 | 0 | pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n", |
8094 | 0 | sym_name, ext->ksym.addr, sym_addr); |
8095 | 0 | return -EINVAL; |
8096 | 0 | } |
8097 | 0 | if (!ext->is_set) { |
8098 | 0 | ext->is_set = true; |
8099 | 0 | ext->ksym.addr = sym_addr; |
8100 | 0 | pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr); |
8101 | 0 | } |
8102 | 0 | return 0; |
8103 | 0 | } |
8104 | | |
8105 | | static int bpf_object__read_kallsyms_file(struct bpf_object *obj) |
8106 | 0 | { |
8107 | 0 | return libbpf_kallsyms_parse(kallsyms_cb, obj); |
8108 | 0 | } |
8109 | | |
8110 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
8111 | | __u16 kind, struct btf **res_btf, |
8112 | | struct module_btf **res_mod_btf) |
8113 | 0 | { |
8114 | 0 | struct module_btf *mod_btf; |
8115 | 0 | struct btf *btf; |
8116 | 0 | int i, id, err; |
8117 | |
|
8118 | 0 | btf = obj->btf_vmlinux; |
8119 | 0 | mod_btf = NULL; |
8120 | 0 | id = btf__find_by_name_kind(btf, ksym_name, kind); |
8121 | |
|
8122 | 0 | if (id == -ENOENT) { |
8123 | 0 | err = load_module_btfs(obj); |
8124 | 0 | if (err) |
8125 | 0 | return err; |
8126 | | |
8127 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
8128 | | /* we assume module_btf's BTF FD is always >0 */ |
8129 | 0 | mod_btf = &obj->btf_modules[i]; |
8130 | 0 | btf = mod_btf->btf; |
8131 | 0 | id = btf__find_by_name_kind_own(btf, ksym_name, kind); |
8132 | 0 | if (id != -ENOENT) |
8133 | 0 | break; |
8134 | 0 | } |
8135 | 0 | } |
8136 | 0 | if (id <= 0) |
8137 | 0 | return -ESRCH; |
8138 | | |
8139 | 0 | *res_btf = btf; |
8140 | 0 | *res_mod_btf = mod_btf; |
8141 | 0 | return id; |
8142 | 0 | } |
8143 | | |
8144 | | static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj, |
8145 | | struct extern_desc *ext) |
8146 | 0 | { |
8147 | 0 | const struct btf_type *targ_var, *targ_type; |
8148 | 0 | __u32 targ_type_id, local_type_id; |
8149 | 0 | struct module_btf *mod_btf = NULL; |
8150 | 0 | const char *targ_var_name; |
8151 | 0 | struct btf *btf = NULL; |
8152 | 0 | int id, err; |
8153 | |
|
8154 | 0 | id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); |
8155 | 0 | if (id < 0) { |
8156 | 0 | if (id == -ESRCH && ext->is_weak) |
8157 | 0 | return 0; |
8158 | 0 | pr_warn("extern (var ksym) '%s': not found in kernel BTF\n", |
8159 | 0 | ext->name); |
8160 | 0 | return id; |
8161 | 0 | } |
8162 | | |
8163 | | /* find local type_id */ |
8164 | 0 | local_type_id = ext->ksym.type_id; |
8165 | | |
8166 | | /* find target type_id */ |
8167 | 0 | targ_var = btf__type_by_id(btf, id); |
8168 | 0 | targ_var_name = btf__name_by_offset(btf, targ_var->name_off); |
8169 | 0 | targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); |
8170 | |
|
8171 | 0 | err = bpf_core_types_are_compat(obj->btf, local_type_id, |
8172 | 0 | btf, targ_type_id); |
8173 | 0 | if (err <= 0) { |
8174 | 0 | const struct btf_type *local_type; |
8175 | 0 | const char *targ_name, *local_name; |
8176 | |
|
8177 | 0 | local_type = btf__type_by_id(obj->btf, local_type_id); |
8178 | 0 | local_name = btf__name_by_offset(obj->btf, local_type->name_off); |
8179 | 0 | targ_name = btf__name_by_offset(btf, targ_type->name_off); |
8180 | |
|
8181 | 0 | pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n", |
8182 | 0 | ext->name, local_type_id, |
8183 | 0 | btf_kind_str(local_type), local_name, targ_type_id, |
8184 | 0 | btf_kind_str(targ_type), targ_name); |
8185 | 0 | return -EINVAL; |
8186 | 0 | } |
8187 | | |
8188 | 0 | ext->is_set = true; |
8189 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
8190 | 0 | ext->ksym.kernel_btf_id = id; |
8191 | 0 | pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n", |
8192 | 0 | ext->name, id, btf_kind_str(targ_var), targ_var_name); |
8193 | |
|
8194 | 0 | return 0; |
8195 | 0 | } |
8196 | | |
8197 | | static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, |
8198 | | struct extern_desc *ext) |
8199 | 0 | { |
8200 | 0 | int local_func_proto_id, kfunc_proto_id, kfunc_id; |
8201 | 0 | struct module_btf *mod_btf = NULL; |
8202 | 0 | const struct btf_type *kern_func; |
8203 | 0 | struct btf *kern_btf = NULL; |
8204 | 0 | int ret; |
8205 | |
|
8206 | 0 | local_func_proto_id = ext->ksym.type_id; |
8207 | |
|
8208 | 0 | kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf, |
8209 | 0 | &mod_btf); |
8210 | 0 | if (kfunc_id < 0) { |
8211 | 0 | if (kfunc_id == -ESRCH && ext->is_weak) |
8212 | 0 | return 0; |
8213 | 0 | pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n", |
8214 | 0 | ext->name); |
8215 | 0 | return kfunc_id; |
8216 | 0 | } |
8217 | | |
8218 | 0 | kern_func = btf__type_by_id(kern_btf, kfunc_id); |
8219 | 0 | kfunc_proto_id = kern_func->type; |
8220 | |
|
8221 | 0 | ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, |
8222 | 0 | kern_btf, kfunc_proto_id); |
8223 | 0 | if (ret <= 0) { |
8224 | 0 | if (ext->is_weak) |
8225 | 0 | return 0; |
8226 | | |
8227 | 0 | pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n", |
8228 | 0 | ext->name, local_func_proto_id, |
8229 | 0 | mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); |
8230 | 0 | return -EINVAL; |
8231 | 0 | } |
8232 | | |
8233 | | /* set index for module BTF fd in fd_array, if unset */ |
8234 | 0 | if (mod_btf && !mod_btf->fd_array_idx) { |
8235 | | /* insn->off is s16 */ |
8236 | 0 | if (obj->fd_array_cnt == INT16_MAX) { |
8237 | 0 | pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n", |
8238 | 0 | ext->name, mod_btf->fd_array_idx); |
8239 | 0 | return -E2BIG; |
8240 | 0 | } |
8241 | | /* Cannot use index 0 for module BTF fd */ |
8242 | 0 | if (!obj->fd_array_cnt) |
8243 | 0 | obj->fd_array_cnt = 1; |
8244 | |
|
8245 | 0 | ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), |
8246 | 0 | obj->fd_array_cnt + 1); |
8247 | 0 | if (ret) |
8248 | 0 | return ret; |
8249 | 0 | mod_btf->fd_array_idx = obj->fd_array_cnt; |
8250 | | /* we assume module BTF FD is always >0 */ |
8251 | 0 | obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; |
8252 | 0 | } |
8253 | | |
8254 | 0 | ext->is_set = true; |
8255 | 0 | ext->ksym.kernel_btf_id = kfunc_id; |
8256 | 0 | ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; |
8257 | | /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data() |
8258 | | * populates FD into ld_imm64 insn when it's used to point to kfunc. |
8259 | | * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. |
8260 | | * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. |
8261 | | */ |
8262 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
8263 | 0 | pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n", |
8264 | 0 | ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); |
8265 | |
|
8266 | 0 | return 0; |
8267 | 0 | } |
8268 | | |
8269 | | static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) |
8270 | 0 | { |
8271 | 0 | const struct btf_type *t; |
8272 | 0 | struct extern_desc *ext; |
8273 | 0 | int i, err; |
8274 | |
|
8275 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8276 | 0 | ext = &obj->externs[i]; |
8277 | 0 | if (ext->type != EXT_KSYM || !ext->ksym.type_id) |
8278 | 0 | continue; |
8279 | | |
8280 | 0 | if (obj->gen_loader) { |
8281 | 0 | ext->is_set = true; |
8282 | 0 | ext->ksym.kernel_btf_obj_fd = 0; |
8283 | 0 | ext->ksym.kernel_btf_id = 0; |
8284 | 0 | continue; |
8285 | 0 | } |
8286 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
8287 | 0 | if (btf_is_var(t)) |
8288 | 0 | err = bpf_object__resolve_ksym_var_btf_id(obj, ext); |
8289 | 0 | else |
8290 | 0 | err = bpf_object__resolve_ksym_func_btf_id(obj, ext); |
8291 | 0 | if (err) |
8292 | 0 | return err; |
8293 | 0 | } |
8294 | 0 | return 0; |
8295 | 0 | } |
8296 | | |
8297 | | static int bpf_object__resolve_externs(struct bpf_object *obj, |
8298 | | const char *extra_kconfig) |
8299 | 0 | { |
8300 | 0 | bool need_config = false, need_kallsyms = false; |
8301 | 0 | bool need_vmlinux_btf = false; |
8302 | 0 | struct extern_desc *ext; |
8303 | 0 | void *kcfg_data = NULL; |
8304 | 0 | int err, i; |
8305 | |
|
8306 | 0 | if (obj->nr_extern == 0) |
8307 | 0 | return 0; |
8308 | | |
8309 | 0 | if (obj->kconfig_map_idx >= 0) |
8310 | 0 | kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; |
8311 | |
|
8312 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8313 | 0 | ext = &obj->externs[i]; |
8314 | |
|
8315 | 0 | if (ext->type == EXT_KSYM) { |
8316 | 0 | if (ext->ksym.type_id) |
8317 | 0 | need_vmlinux_btf = true; |
8318 | 0 | else |
8319 | 0 | need_kallsyms = true; |
8320 | 0 | continue; |
8321 | 0 | } else if (ext->type == EXT_KCFG) { |
8322 | 0 | void *ext_ptr = kcfg_data + ext->kcfg.data_off; |
8323 | 0 | __u64 value = 0; |
8324 | | |
8325 | | /* Kconfig externs need actual /proc/config.gz */ |
8326 | 0 | if (str_has_pfx(ext->name, "CONFIG_")) { |
8327 | 0 | need_config = true; |
8328 | 0 | continue; |
8329 | 0 | } |
8330 | | |
8331 | | /* Virtual kcfg externs are customly handled by libbpf */ |
8332 | 0 | if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { |
8333 | 0 | value = get_kernel_version(); |
8334 | 0 | if (!value) { |
8335 | 0 | pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); |
8336 | 0 | return -EINVAL; |
8337 | 0 | } |
8338 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { |
8339 | 0 | value = kernel_supports(obj, FEAT_BPF_COOKIE); |
8340 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { |
8341 | 0 | value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER); |
8342 | 0 | } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { |
8343 | | /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed |
8344 | | * __kconfig externs, where LINUX_ ones are virtual and filled out |
8345 | | * customly by libbpf (their values don't come from Kconfig). |
8346 | | * If LINUX_xxx variable is not recognized by libbpf, but is marked |
8347 | | * __weak, it defaults to zero value, just like for CONFIG_xxx |
8348 | | * externs. |
8349 | | */ |
8350 | 0 | pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); |
8351 | 0 | return -EINVAL; |
8352 | 0 | } |
8353 | | |
8354 | 0 | err = set_kcfg_value_num(ext, ext_ptr, value); |
8355 | 0 | if (err) |
8356 | 0 | return err; |
8357 | 0 | pr_debug("extern (kcfg) '%s': set to 0x%llx\n", |
8358 | 0 | ext->name, (long long)value); |
8359 | 0 | } else { |
8360 | 0 | pr_warn("extern '%s': unrecognized extern kind\n", ext->name); |
8361 | 0 | return -EINVAL; |
8362 | 0 | } |
8363 | 0 | } |
8364 | 0 | if (need_config && extra_kconfig) { |
8365 | 0 | err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data); |
8366 | 0 | if (err) |
8367 | 0 | return -EINVAL; |
8368 | 0 | need_config = false; |
8369 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8370 | 0 | ext = &obj->externs[i]; |
8371 | 0 | if (ext->type == EXT_KCFG && !ext->is_set) { |
8372 | 0 | need_config = true; |
8373 | 0 | break; |
8374 | 0 | } |
8375 | 0 | } |
8376 | 0 | } |
8377 | 0 | if (need_config) { |
8378 | 0 | err = bpf_object__read_kconfig_file(obj, kcfg_data); |
8379 | 0 | if (err) |
8380 | 0 | return -EINVAL; |
8381 | 0 | } |
8382 | 0 | if (need_kallsyms) { |
8383 | 0 | err = bpf_object__read_kallsyms_file(obj); |
8384 | 0 | if (err) |
8385 | 0 | return -EINVAL; |
8386 | 0 | } |
8387 | 0 | if (need_vmlinux_btf) { |
8388 | 0 | err = bpf_object__resolve_ksyms_btf_id(obj); |
8389 | 0 | if (err) |
8390 | 0 | return -EINVAL; |
8391 | 0 | } |
8392 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8393 | 0 | ext = &obj->externs[i]; |
8394 | |
|
8395 | 0 | if (!ext->is_set && !ext->is_weak) { |
8396 | 0 | pr_warn("extern '%s' (strong): not resolved\n", ext->name); |
8397 | 0 | return -ESRCH; |
8398 | 0 | } else if (!ext->is_set) { |
8399 | 0 | pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n", |
8400 | 0 | ext->name); |
8401 | 0 | } |
8402 | 0 | } |
8403 | | |
8404 | 0 | return 0; |
8405 | 0 | } |
8406 | | |
8407 | | static void bpf_map_prepare_vdata(const struct bpf_map *map) |
8408 | 0 | { |
8409 | 0 | struct bpf_struct_ops *st_ops; |
8410 | 0 | __u32 i; |
8411 | |
|
8412 | 0 | st_ops = map->st_ops; |
8413 | 0 | for (i = 0; i < btf_vlen(st_ops->type); i++) { |
8414 | 0 | struct bpf_program *prog = st_ops->progs[i]; |
8415 | 0 | void *kern_data; |
8416 | 0 | int prog_fd; |
8417 | |
|
8418 | 0 | if (!prog) |
8419 | 0 | continue; |
8420 | | |
8421 | 0 | prog_fd = bpf_program__fd(prog); |
8422 | 0 | kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; |
8423 | 0 | *(unsigned long *)kern_data = prog_fd; |
8424 | 0 | } |
8425 | 0 | } |
8426 | | |
8427 | | static int bpf_object_prepare_struct_ops(struct bpf_object *obj) |
8428 | 0 | { |
8429 | 0 | struct bpf_map *map; |
8430 | 0 | int i; |
8431 | |
|
8432 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
8433 | 0 | map = &obj->maps[i]; |
8434 | |
|
8435 | 0 | if (!bpf_map__is_struct_ops(map)) |
8436 | 0 | continue; |
8437 | | |
8438 | 0 | if (!map->autocreate) |
8439 | 0 | continue; |
8440 | | |
8441 | 0 | bpf_map_prepare_vdata(map); |
8442 | 0 | } |
8443 | |
|
8444 | 0 | return 0; |
8445 | 0 | } |
8446 | | |
8447 | | static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path) |
8448 | 0 | { |
8449 | 0 | int err, i; |
8450 | |
|
8451 | 0 | if (!obj) |
8452 | 0 | return libbpf_err(-EINVAL); |
8453 | | |
8454 | 0 | if (obj->loaded) { |
8455 | 0 | pr_warn("object '%s': load can't be attempted twice\n", obj->name); |
8456 | 0 | return libbpf_err(-EINVAL); |
8457 | 0 | } |
8458 | | |
8459 | 0 | if (obj->gen_loader) |
8460 | 0 | bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); |
8461 | |
|
8462 | 0 | err = bpf_object_prepare_token(obj); |
8463 | 0 | err = err ? : bpf_object__probe_loading(obj); |
8464 | 0 | err = err ? : bpf_object__load_vmlinux_btf(obj, false); |
8465 | 0 | err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); |
8466 | 0 | err = err ? : bpf_object__sanitize_maps(obj); |
8467 | 0 | err = err ? : bpf_object__init_kern_struct_ops_maps(obj); |
8468 | 0 | err = err ? : bpf_object_adjust_struct_ops_autoload(obj); |
8469 | 0 | err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); |
8470 | 0 | err = err ? : bpf_object__sanitize_and_load_btf(obj); |
8471 | 0 | err = err ? : bpf_object__create_maps(obj); |
8472 | 0 | err = err ? : bpf_object__load_progs(obj, extra_log_level); |
8473 | 0 | err = err ? : bpf_object_init_prog_arrays(obj); |
8474 | 0 | err = err ? : bpf_object_prepare_struct_ops(obj); |
8475 | |
|
8476 | 0 | if (obj->gen_loader) { |
8477 | | /* reset FDs */ |
8478 | 0 | if (obj->btf) |
8479 | 0 | btf__set_fd(obj->btf, -1); |
8480 | 0 | if (!err) |
8481 | 0 | err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); |
8482 | 0 | } |
8483 | | |
8484 | | /* clean up fd_array */ |
8485 | 0 | zfree(&obj->fd_array); |
8486 | | |
8487 | | /* clean up module BTFs */ |
8488 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
8489 | 0 | close(obj->btf_modules[i].fd); |
8490 | 0 | btf__free(obj->btf_modules[i].btf); |
8491 | 0 | free(obj->btf_modules[i].name); |
8492 | 0 | } |
8493 | 0 | free(obj->btf_modules); |
8494 | | |
8495 | | /* clean up vmlinux BTF */ |
8496 | 0 | btf__free(obj->btf_vmlinux); |
8497 | 0 | obj->btf_vmlinux = NULL; |
8498 | |
|
8499 | 0 | obj->loaded = true; /* doesn't matter if successfully or not */ |
8500 | |
|
8501 | 0 | if (err) |
8502 | 0 | goto out; |
8503 | | |
8504 | 0 | return 0; |
8505 | 0 | out: |
8506 | | /* unpin any maps that were auto-pinned during load */ |
8507 | 0 | for (i = 0; i < obj->nr_maps; i++) |
8508 | 0 | if (obj->maps[i].pinned && !obj->maps[i].reused) |
8509 | 0 | bpf_map__unpin(&obj->maps[i], NULL); |
8510 | |
|
8511 | 0 | bpf_object_unload(obj); |
8512 | 0 | pr_warn("failed to load object '%s'\n", obj->path); |
8513 | 0 | return libbpf_err(err); |
8514 | 0 | } |
8515 | | |
8516 | | int bpf_object__load(struct bpf_object *obj) |
8517 | 0 | { |
8518 | 0 | return bpf_object_load(obj, 0, NULL); |
8519 | 0 | } |
8520 | | |
8521 | | static int make_parent_dir(const char *path) |
8522 | 0 | { |
8523 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8524 | 0 | char *dname, *dir; |
8525 | 0 | int err = 0; |
8526 | |
|
8527 | 0 | dname = strdup(path); |
8528 | 0 | if (dname == NULL) |
8529 | 0 | return -ENOMEM; |
8530 | | |
8531 | 0 | dir = dirname(dname); |
8532 | 0 | if (mkdir(dir, 0700) && errno != EEXIST) |
8533 | 0 | err = -errno; |
8534 | |
|
8535 | 0 | free(dname); |
8536 | 0 | if (err) { |
8537 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
8538 | 0 | pr_warn("failed to mkdir %s: %s\n", path, cp); |
8539 | 0 | } |
8540 | 0 | return err; |
8541 | 0 | } |
8542 | | |
8543 | | static int check_path(const char *path) |
8544 | 0 | { |
8545 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8546 | 0 | struct statfs st_fs; |
8547 | 0 | char *dname, *dir; |
8548 | 0 | int err = 0; |
8549 | |
|
8550 | 0 | if (path == NULL) |
8551 | 0 | return -EINVAL; |
8552 | | |
8553 | 0 | dname = strdup(path); |
8554 | 0 | if (dname == NULL) |
8555 | 0 | return -ENOMEM; |
8556 | | |
8557 | 0 | dir = dirname(dname); |
8558 | 0 | if (statfs(dir, &st_fs)) { |
8559 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
8560 | 0 | pr_warn("failed to statfs %s: %s\n", dir, cp); |
8561 | 0 | err = -errno; |
8562 | 0 | } |
8563 | 0 | free(dname); |
8564 | |
|
8565 | 0 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
8566 | 0 | pr_warn("specified path %s is not on BPF FS\n", path); |
8567 | 0 | err = -EINVAL; |
8568 | 0 | } |
8569 | |
|
8570 | 0 | return err; |
8571 | 0 | } |
8572 | | |
8573 | | int bpf_program__pin(struct bpf_program *prog, const char *path) |
8574 | 0 | { |
8575 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8576 | 0 | int err; |
8577 | |
|
8578 | 0 | if (prog->fd < 0) { |
8579 | 0 | pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); |
8580 | 0 | return libbpf_err(-EINVAL); |
8581 | 0 | } |
8582 | | |
8583 | 0 | err = make_parent_dir(path); |
8584 | 0 | if (err) |
8585 | 0 | return libbpf_err(err); |
8586 | | |
8587 | 0 | err = check_path(path); |
8588 | 0 | if (err) |
8589 | 0 | return libbpf_err(err); |
8590 | | |
8591 | 0 | if (bpf_obj_pin(prog->fd, path)) { |
8592 | 0 | err = -errno; |
8593 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
8594 | 0 | pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp); |
8595 | 0 | return libbpf_err(err); |
8596 | 0 | } |
8597 | | |
8598 | 0 | pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); |
8599 | 0 | return 0; |
8600 | 0 | } |
8601 | | |
8602 | | int bpf_program__unpin(struct bpf_program *prog, const char *path) |
8603 | 0 | { |
8604 | 0 | int err; |
8605 | |
|
8606 | 0 | if (prog->fd < 0) { |
8607 | 0 | pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); |
8608 | 0 | return libbpf_err(-EINVAL); |
8609 | 0 | } |
8610 | | |
8611 | 0 | err = check_path(path); |
8612 | 0 | if (err) |
8613 | 0 | return libbpf_err(err); |
8614 | | |
8615 | 0 | err = unlink(path); |
8616 | 0 | if (err) |
8617 | 0 | return libbpf_err(-errno); |
8618 | | |
8619 | 0 | pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); |
8620 | 0 | return 0; |
8621 | 0 | } |
8622 | | |
8623 | | int bpf_map__pin(struct bpf_map *map, const char *path) |
8624 | 0 | { |
8625 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8626 | 0 | int err; |
8627 | |
|
8628 | 0 | if (map == NULL) { |
8629 | 0 | pr_warn("invalid map pointer\n"); |
8630 | 0 | return libbpf_err(-EINVAL); |
8631 | 0 | } |
8632 | | |
8633 | 0 | if (map->fd < 0) { |
8634 | 0 | pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map->name); |
8635 | 0 | return libbpf_err(-EINVAL); |
8636 | 0 | } |
8637 | | |
8638 | 0 | if (map->pin_path) { |
8639 | 0 | if (path && strcmp(path, map->pin_path)) { |
8640 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
8641 | 0 | bpf_map__name(map), map->pin_path, path); |
8642 | 0 | return libbpf_err(-EINVAL); |
8643 | 0 | } else if (map->pinned) { |
8644 | 0 | pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", |
8645 | 0 | bpf_map__name(map), map->pin_path); |
8646 | 0 | return 0; |
8647 | 0 | } |
8648 | 0 | } else { |
8649 | 0 | if (!path) { |
8650 | 0 | pr_warn("missing a path to pin map '%s' at\n", |
8651 | 0 | bpf_map__name(map)); |
8652 | 0 | return libbpf_err(-EINVAL); |
8653 | 0 | } else if (map->pinned) { |
8654 | 0 | pr_warn("map '%s' already pinned\n", bpf_map__name(map)); |
8655 | 0 | return libbpf_err(-EEXIST); |
8656 | 0 | } |
8657 | | |
8658 | 0 | map->pin_path = strdup(path); |
8659 | 0 | if (!map->pin_path) { |
8660 | 0 | err = -errno; |
8661 | 0 | goto out_err; |
8662 | 0 | } |
8663 | 0 | } |
8664 | | |
8665 | 0 | err = make_parent_dir(map->pin_path); |
8666 | 0 | if (err) |
8667 | 0 | return libbpf_err(err); |
8668 | | |
8669 | 0 | err = check_path(map->pin_path); |
8670 | 0 | if (err) |
8671 | 0 | return libbpf_err(err); |
8672 | | |
8673 | 0 | if (bpf_obj_pin(map->fd, map->pin_path)) { |
8674 | 0 | err = -errno; |
8675 | 0 | goto out_err; |
8676 | 0 | } |
8677 | | |
8678 | 0 | map->pinned = true; |
8679 | 0 | pr_debug("pinned map '%s'\n", map->pin_path); |
8680 | |
|
8681 | 0 | return 0; |
8682 | | |
8683 | 0 | out_err: |
8684 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
8685 | 0 | pr_warn("failed to pin map: %s\n", cp); |
8686 | 0 | return libbpf_err(err); |
8687 | 0 | } |
8688 | | |
8689 | | int bpf_map__unpin(struct bpf_map *map, const char *path) |
8690 | 0 | { |
8691 | 0 | int err; |
8692 | |
|
8693 | 0 | if (map == NULL) { |
8694 | 0 | pr_warn("invalid map pointer\n"); |
8695 | 0 | return libbpf_err(-EINVAL); |
8696 | 0 | } |
8697 | | |
8698 | 0 | if (map->pin_path) { |
8699 | 0 | if (path && strcmp(path, map->pin_path)) { |
8700 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
8701 | 0 | bpf_map__name(map), map->pin_path, path); |
8702 | 0 | return libbpf_err(-EINVAL); |
8703 | 0 | } |
8704 | 0 | path = map->pin_path; |
8705 | 0 | } else if (!path) { |
8706 | 0 | pr_warn("no path to unpin map '%s' from\n", |
8707 | 0 | bpf_map__name(map)); |
8708 | 0 | return libbpf_err(-EINVAL); |
8709 | 0 | } |
8710 | | |
8711 | 0 | err = check_path(path); |
8712 | 0 | if (err) |
8713 | 0 | return libbpf_err(err); |
8714 | | |
8715 | 0 | err = unlink(path); |
8716 | 0 | if (err != 0) |
8717 | 0 | return libbpf_err(-errno); |
8718 | | |
8719 | 0 | map->pinned = false; |
8720 | 0 | pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path); |
8721 | |
|
8722 | 0 | return 0; |
8723 | 0 | } |
8724 | | |
8725 | | int bpf_map__set_pin_path(struct bpf_map *map, const char *path) |
8726 | 1 | { |
8727 | 1 | char *new = NULL; |
8728 | | |
8729 | 1 | if (path) { |
8730 | 1 | new = strdup(path); |
8731 | 1 | if (!new) |
8732 | 0 | return libbpf_err(-errno); |
8733 | 1 | } |
8734 | | |
8735 | 1 | free(map->pin_path); |
8736 | 1 | map->pin_path = new; |
8737 | 1 | return 0; |
8738 | 1 | } |
8739 | | |
8740 | | __alias(bpf_map__pin_path) |
8741 | | const char *bpf_map__get_pin_path(const struct bpf_map *map); |
8742 | | |
8743 | | const char *bpf_map__pin_path(const struct bpf_map *map) |
8744 | 0 | { |
8745 | 0 | return map->pin_path; |
8746 | 0 | } |
8747 | | |
8748 | | bool bpf_map__is_pinned(const struct bpf_map *map) |
8749 | 0 | { |
8750 | 0 | return map->pinned; |
8751 | 0 | } |
8752 | | |
8753 | | static void sanitize_pin_path(char *s) |
8754 | 0 | { |
8755 | | /* bpffs disallows periods in path names */ |
8756 | 0 | while (*s) { |
8757 | 0 | if (*s == '.') |
8758 | 0 | *s = '_'; |
8759 | 0 | s++; |
8760 | 0 | } |
8761 | 0 | } |
8762 | | |
8763 | | int bpf_object__pin_maps(struct bpf_object *obj, const char *path) |
8764 | 0 | { |
8765 | 0 | struct bpf_map *map; |
8766 | 0 | int err; |
8767 | |
|
8768 | 0 | if (!obj) |
8769 | 0 | return libbpf_err(-ENOENT); |
8770 | | |
8771 | 0 | if (!obj->loaded) { |
8772 | 0 | pr_warn("object not yet loaded; load it first\n"); |
8773 | 0 | return libbpf_err(-ENOENT); |
8774 | 0 | } |
8775 | | |
8776 | 0 | bpf_object__for_each_map(map, obj) { |
8777 | 0 | char *pin_path = NULL; |
8778 | 0 | char buf[PATH_MAX]; |
8779 | |
|
8780 | 0 | if (!map->autocreate) |
8781 | 0 | continue; |
8782 | | |
8783 | 0 | if (path) { |
8784 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
8785 | 0 | if (err) |
8786 | 0 | goto err_unpin_maps; |
8787 | 0 | sanitize_pin_path(buf); |
8788 | 0 | pin_path = buf; |
8789 | 0 | } else if (!map->pin_path) { |
8790 | 0 | continue; |
8791 | 0 | } |
8792 | | |
8793 | 0 | err = bpf_map__pin(map, pin_path); |
8794 | 0 | if (err) |
8795 | 0 | goto err_unpin_maps; |
8796 | 0 | } |
8797 | | |
8798 | 0 | return 0; |
8799 | | |
8800 | 0 | err_unpin_maps: |
8801 | 0 | while ((map = bpf_object__prev_map(obj, map))) { |
8802 | 0 | if (!map->pin_path) |
8803 | 0 | continue; |
8804 | | |
8805 | 0 | bpf_map__unpin(map, NULL); |
8806 | 0 | } |
8807 | |
|
8808 | 0 | return libbpf_err(err); |
8809 | 0 | } |
8810 | | |
8811 | | int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) |
8812 | 0 | { |
8813 | 0 | struct bpf_map *map; |
8814 | 0 | int err; |
8815 | |
|
8816 | 0 | if (!obj) |
8817 | 0 | return libbpf_err(-ENOENT); |
8818 | | |
8819 | 0 | bpf_object__for_each_map(map, obj) { |
8820 | 0 | char *pin_path = NULL; |
8821 | 0 | char buf[PATH_MAX]; |
8822 | |
|
8823 | 0 | if (path) { |
8824 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
8825 | 0 | if (err) |
8826 | 0 | return libbpf_err(err); |
8827 | 0 | sanitize_pin_path(buf); |
8828 | 0 | pin_path = buf; |
8829 | 0 | } else if (!map->pin_path) { |
8830 | 0 | continue; |
8831 | 0 | } |
8832 | | |
8833 | 0 | err = bpf_map__unpin(map, pin_path); |
8834 | 0 | if (err) |
8835 | 0 | return libbpf_err(err); |
8836 | 0 | } |
8837 | | |
8838 | 0 | return 0; |
8839 | 0 | } |
8840 | | |
8841 | | int bpf_object__pin_programs(struct bpf_object *obj, const char *path) |
8842 | 0 | { |
8843 | 0 | struct bpf_program *prog; |
8844 | 0 | char buf[PATH_MAX]; |
8845 | 0 | int err; |
8846 | |
|
8847 | 0 | if (!obj) |
8848 | 0 | return libbpf_err(-ENOENT); |
8849 | | |
8850 | 0 | if (!obj->loaded) { |
8851 | 0 | pr_warn("object not yet loaded; load it first\n"); |
8852 | 0 | return libbpf_err(-ENOENT); |
8853 | 0 | } |
8854 | | |
8855 | 0 | bpf_object__for_each_program(prog, obj) { |
8856 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
8857 | 0 | if (err) |
8858 | 0 | goto err_unpin_programs; |
8859 | | |
8860 | 0 | err = bpf_program__pin(prog, buf); |
8861 | 0 | if (err) |
8862 | 0 | goto err_unpin_programs; |
8863 | 0 | } |
8864 | | |
8865 | 0 | return 0; |
8866 | | |
8867 | 0 | err_unpin_programs: |
8868 | 0 | while ((prog = bpf_object__prev_program(obj, prog))) { |
8869 | 0 | if (pathname_concat(buf, sizeof(buf), path, prog->name)) |
8870 | 0 | continue; |
8871 | | |
8872 | 0 | bpf_program__unpin(prog, buf); |
8873 | 0 | } |
8874 | |
|
8875 | 0 | return libbpf_err(err); |
8876 | 0 | } |
8877 | | |
8878 | | int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) |
8879 | 0 | { |
8880 | 0 | struct bpf_program *prog; |
8881 | 0 | int err; |
8882 | |
|
8883 | 0 | if (!obj) |
8884 | 0 | return libbpf_err(-ENOENT); |
8885 | | |
8886 | 0 | bpf_object__for_each_program(prog, obj) { |
8887 | 0 | char buf[PATH_MAX]; |
8888 | |
|
8889 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
8890 | 0 | if (err) |
8891 | 0 | return libbpf_err(err); |
8892 | | |
8893 | 0 | err = bpf_program__unpin(prog, buf); |
8894 | 0 | if (err) |
8895 | 0 | return libbpf_err(err); |
8896 | 0 | } |
8897 | | |
8898 | 0 | return 0; |
8899 | 0 | } |
8900 | | |
8901 | | int bpf_object__pin(struct bpf_object *obj, const char *path) |
8902 | 0 | { |
8903 | 0 | int err; |
8904 | |
|
8905 | 0 | err = bpf_object__pin_maps(obj, path); |
8906 | 0 | if (err) |
8907 | 0 | return libbpf_err(err); |
8908 | | |
8909 | 0 | err = bpf_object__pin_programs(obj, path); |
8910 | 0 | if (err) { |
8911 | 0 | bpf_object__unpin_maps(obj, path); |
8912 | 0 | return libbpf_err(err); |
8913 | 0 | } |
8914 | | |
8915 | 0 | return 0; |
8916 | 0 | } |
8917 | | |
8918 | | int bpf_object__unpin(struct bpf_object *obj, const char *path) |
8919 | 0 | { |
8920 | 0 | int err; |
8921 | |
|
8922 | 0 | err = bpf_object__unpin_programs(obj, path); |
8923 | 0 | if (err) |
8924 | 0 | return libbpf_err(err); |
8925 | | |
8926 | 0 | err = bpf_object__unpin_maps(obj, path); |
8927 | 0 | if (err) |
8928 | 0 | return libbpf_err(err); |
8929 | | |
8930 | 0 | return 0; |
8931 | 0 | } |
8932 | | |
8933 | | static void bpf_map__destroy(struct bpf_map *map) |
8934 | 2.75k | { |
8935 | 2.75k | if (map->inner_map) { |
8936 | 0 | bpf_map__destroy(map->inner_map); |
8937 | 0 | zfree(&map->inner_map); |
8938 | 0 | } |
8939 | | |
8940 | 2.75k | zfree(&map->init_slots); |
8941 | 2.75k | map->init_slots_sz = 0; |
8942 | | |
8943 | 2.75k | if (map->mmaped && map->mmaped != map->obj->arena_data) |
8944 | 1.67k | munmap(map->mmaped, bpf_map_mmap_sz(map)); |
8945 | 2.75k | map->mmaped = NULL; |
8946 | | |
8947 | 2.75k | if (map->st_ops) { |
8948 | 48 | zfree(&map->st_ops->data); |
8949 | 48 | zfree(&map->st_ops->progs); |
8950 | 48 | zfree(&map->st_ops->kern_func_off); |
8951 | 48 | zfree(&map->st_ops); |
8952 | 48 | } |
8953 | | |
8954 | 2.75k | zfree(&map->name); |
8955 | 2.75k | zfree(&map->real_name); |
8956 | 2.75k | zfree(&map->pin_path); |
8957 | | |
8958 | 2.75k | if (map->fd >= 0) |
8959 | 0 | zclose(map->fd); |
8960 | 2.75k | } |
8961 | | |
8962 | | void bpf_object__close(struct bpf_object *obj) |
8963 | 11.0k | { |
8964 | 11.0k | size_t i; |
8965 | | |
8966 | 11.0k | if (IS_ERR_OR_NULL(obj)) |
8967 | 0 | return; |
8968 | | |
8969 | 11.0k | usdt_manager_free(obj->usdt_man); |
8970 | 11.0k | obj->usdt_man = NULL; |
8971 | | |
8972 | 11.0k | bpf_gen__free(obj->gen_loader); |
8973 | 11.0k | bpf_object__elf_finish(obj); |
8974 | 11.0k | bpf_object_unload(obj); |
8975 | 11.0k | btf__free(obj->btf); |
8976 | 11.0k | btf__free(obj->btf_vmlinux); |
8977 | 11.0k | btf_ext__free(obj->btf_ext); |
8978 | | |
8979 | 13.8k | for (i = 0; i < obj->nr_maps; i++) |
8980 | 2.75k | bpf_map__destroy(&obj->maps[i]); |
8981 | | |
8982 | 11.0k | zfree(&obj->btf_custom_path); |
8983 | 11.0k | zfree(&obj->kconfig); |
8984 | | |
8985 | 15.1k | for (i = 0; i < obj->nr_extern; i++) |
8986 | 4.06k | zfree(&obj->externs[i].essent_name); |
8987 | | |
8988 | 11.0k | zfree(&obj->externs); |
8989 | 11.0k | obj->nr_extern = 0; |
8990 | | |
8991 | 11.0k | zfree(&obj->maps); |
8992 | 11.0k | obj->nr_maps = 0; |
8993 | | |
8994 | 11.0k | if (obj->programs && obj->nr_programs) { |
8995 | 14.9k | for (i = 0; i < obj->nr_programs; i++) |
8996 | 14.3k | bpf_program__exit(&obj->programs[i]); |
8997 | 625 | } |
8998 | 11.0k | zfree(&obj->programs); |
8999 | | |
9000 | 11.0k | zfree(&obj->feat_cache); |
9001 | 11.0k | zfree(&obj->token_path); |
9002 | 11.0k | if (obj->token_fd > 0) |
9003 | 0 | close(obj->token_fd); |
9004 | | |
9005 | 11.0k | zfree(&obj->arena_data); |
9006 | | |
9007 | 11.0k | free(obj); |
9008 | 11.0k | } |
9009 | | |
9010 | | const char *bpf_object__name(const struct bpf_object *obj) |
9011 | 0 | { |
9012 | 0 | return obj ? obj->name : libbpf_err_ptr(-EINVAL); |
9013 | 0 | } |
9014 | | |
9015 | | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
9016 | 0 | { |
9017 | 0 | return obj ? obj->kern_version : 0; |
9018 | 0 | } |
9019 | | |
9020 | | struct btf *bpf_object__btf(const struct bpf_object *obj) |
9021 | 0 | { |
9022 | 0 | return obj ? obj->btf : NULL; |
9023 | 0 | } |
9024 | | |
9025 | | int bpf_object__btf_fd(const struct bpf_object *obj) |
9026 | 0 | { |
9027 | 0 | return obj->btf ? btf__fd(obj->btf) : -1; |
9028 | 0 | } |
9029 | | |
9030 | | int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version) |
9031 | 0 | { |
9032 | 0 | if (obj->loaded) |
9033 | 0 | return libbpf_err(-EINVAL); |
9034 | | |
9035 | 0 | obj->kern_version = kern_version; |
9036 | |
|
9037 | 0 | return 0; |
9038 | 0 | } |
9039 | | |
9040 | | int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts) |
9041 | 0 | { |
9042 | 0 | struct bpf_gen *gen; |
9043 | |
|
9044 | 0 | if (!opts) |
9045 | 0 | return -EFAULT; |
9046 | 0 | if (!OPTS_VALID(opts, gen_loader_opts)) |
9047 | 0 | return -EINVAL; |
9048 | 0 | gen = calloc(sizeof(*gen), 1); |
9049 | 0 | if (!gen) |
9050 | 0 | return -ENOMEM; |
9051 | 0 | gen->opts = opts; |
9052 | 0 | obj->gen_loader = gen; |
9053 | 0 | return 0; |
9054 | 0 | } |
9055 | | |
9056 | | static struct bpf_program * |
9057 | | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
9058 | | bool forward) |
9059 | 15.3k | { |
9060 | 15.3k | size_t nr_programs = obj->nr_programs; |
9061 | 15.3k | ssize_t idx; |
9062 | | |
9063 | 15.3k | if (!nr_programs) |
9064 | 1.37k | return NULL; |
9065 | | |
9066 | 13.9k | if (!p) |
9067 | | /* Iter from the beginning */ |
9068 | 562 | return forward ? &obj->programs[0] : |
9069 | 562 | &obj->programs[nr_programs - 1]; |
9070 | | |
9071 | 13.4k | if (p->obj != obj) { |
9072 | 0 | pr_warn("error: program handler doesn't match object\n"); |
9073 | 0 | return errno = EINVAL, NULL; |
9074 | 0 | } |
9075 | | |
9076 | 13.4k | idx = (p - obj->programs) + (forward ? 1 : -1); |
9077 | 13.4k | if (idx >= obj->nr_programs || idx < 0) |
9078 | 562 | return NULL; |
9079 | 12.8k | return &obj->programs[idx]; |
9080 | 13.4k | } |
9081 | | |
9082 | | struct bpf_program * |
9083 | | bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev) |
9084 | 12.6k | { |
9085 | 12.6k | struct bpf_program *prog = prev; |
9086 | | |
9087 | 15.3k | do { |
9088 | 15.3k | prog = __bpf_program__iter(prog, obj, true); |
9089 | 15.3k | } while (prog && prog_is_subprog(obj, prog)); |
9090 | | |
9091 | 12.6k | return prog; |
9092 | 12.6k | } |
9093 | | |
9094 | | struct bpf_program * |
9095 | | bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next) |
9096 | 0 | { |
9097 | 0 | struct bpf_program *prog = next; |
9098 | |
|
9099 | 0 | do { |
9100 | 0 | prog = __bpf_program__iter(prog, obj, false); |
9101 | 0 | } while (prog && prog_is_subprog(obj, prog)); |
9102 | |
|
9103 | 0 | return prog; |
9104 | 0 | } |
9105 | | |
9106 | | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
9107 | 0 | { |
9108 | 0 | prog->prog_ifindex = ifindex; |
9109 | 0 | } |
9110 | | |
9111 | | const char *bpf_program__name(const struct bpf_program *prog) |
9112 | 0 | { |
9113 | 0 | return prog->name; |
9114 | 0 | } |
9115 | | |
9116 | | const char *bpf_program__section_name(const struct bpf_program *prog) |
9117 | 0 | { |
9118 | 0 | return prog->sec_name; |
9119 | 0 | } |
9120 | | |
9121 | | bool bpf_program__autoload(const struct bpf_program *prog) |
9122 | 0 | { |
9123 | 0 | return prog->autoload; |
9124 | 0 | } |
9125 | | |
9126 | | int bpf_program__set_autoload(struct bpf_program *prog, bool autoload) |
9127 | 0 | { |
9128 | 0 | if (prog->obj->loaded) |
9129 | 0 | return libbpf_err(-EINVAL); |
9130 | | |
9131 | 0 | prog->autoload = autoload; |
9132 | 0 | return 0; |
9133 | 0 | } |
9134 | | |
9135 | | bool bpf_program__autoattach(const struct bpf_program *prog) |
9136 | 0 | { |
9137 | 0 | return prog->autoattach; |
9138 | 0 | } |
9139 | | |
9140 | | void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach) |
9141 | 0 | { |
9142 | 0 | prog->autoattach = autoattach; |
9143 | 0 | } |
9144 | | |
9145 | | const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog) |
9146 | 0 | { |
9147 | 0 | return prog->insns; |
9148 | 0 | } |
9149 | | |
9150 | | size_t bpf_program__insn_cnt(const struct bpf_program *prog) |
9151 | 0 | { |
9152 | 0 | return prog->insns_cnt; |
9153 | 0 | } |
9154 | | |
9155 | | int bpf_program__set_insns(struct bpf_program *prog, |
9156 | | struct bpf_insn *new_insns, size_t new_insn_cnt) |
9157 | 0 | { |
9158 | 0 | struct bpf_insn *insns; |
9159 | |
|
9160 | 0 | if (prog->obj->loaded) |
9161 | 0 | return -EBUSY; |
9162 | | |
9163 | 0 | insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); |
9164 | | /* NULL is a valid return from reallocarray if the new count is zero */ |
9165 | 0 | if (!insns && new_insn_cnt) { |
9166 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", prog->name); |
9167 | 0 | return -ENOMEM; |
9168 | 0 | } |
9169 | 0 | memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns)); |
9170 | |
|
9171 | 0 | prog->insns = insns; |
9172 | 0 | prog->insns_cnt = new_insn_cnt; |
9173 | 0 | return 0; |
9174 | 0 | } |
9175 | | |
9176 | | int bpf_program__fd(const struct bpf_program *prog) |
9177 | 0 | { |
9178 | 0 | if (!prog) |
9179 | 0 | return libbpf_err(-EINVAL); |
9180 | | |
9181 | 0 | if (prog->fd < 0) |
9182 | 0 | return libbpf_err(-ENOENT); |
9183 | | |
9184 | 0 | return prog->fd; |
9185 | 0 | } |
9186 | | |
9187 | | __alias(bpf_program__type) |
9188 | | enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog); |
9189 | | |
9190 | | enum bpf_prog_type bpf_program__type(const struct bpf_program *prog) |
9191 | 0 | { |
9192 | 0 | return prog->type; |
9193 | 0 | } |
9194 | | |
9195 | | static size_t custom_sec_def_cnt; |
9196 | | static struct bpf_sec_def *custom_sec_defs; |
9197 | | static struct bpf_sec_def custom_fallback_def; |
9198 | | static bool has_custom_fallback_def; |
9199 | | static int last_custom_sec_def_handler_id; |
9200 | | |
9201 | | int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) |
9202 | 0 | { |
9203 | 0 | if (prog->obj->loaded) |
9204 | 0 | return libbpf_err(-EBUSY); |
9205 | | |
9206 | | /* if type is not changed, do nothing */ |
9207 | 0 | if (prog->type == type) |
9208 | 0 | return 0; |
9209 | | |
9210 | 0 | prog->type = type; |
9211 | | |
9212 | | /* If a program type was changed, we need to reset associated SEC() |
9213 | | * handler, as it will be invalid now. The only exception is a generic |
9214 | | * fallback handler, which by definition is program type-agnostic and |
9215 | | * is a catch-all custom handler, optionally set by the application, |
9216 | | * so should be able to handle any type of BPF program. |
9217 | | */ |
9218 | 0 | if (prog->sec_def != &custom_fallback_def) |
9219 | 0 | prog->sec_def = NULL; |
9220 | 0 | return 0; |
9221 | 0 | } |
9222 | | |
9223 | | __alias(bpf_program__expected_attach_type) |
9224 | | enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog); |
9225 | | |
9226 | | enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog) |
9227 | 0 | { |
9228 | 0 | return prog->expected_attach_type; |
9229 | 0 | } |
9230 | | |
9231 | | int bpf_program__set_expected_attach_type(struct bpf_program *prog, |
9232 | | enum bpf_attach_type type) |
9233 | 0 | { |
9234 | 0 | if (prog->obj->loaded) |
9235 | 0 | return libbpf_err(-EBUSY); |
9236 | | |
9237 | 0 | prog->expected_attach_type = type; |
9238 | 0 | return 0; |
9239 | 0 | } |
9240 | | |
9241 | | __u32 bpf_program__flags(const struct bpf_program *prog) |
9242 | 0 | { |
9243 | 0 | return prog->prog_flags; |
9244 | 0 | } |
9245 | | |
9246 | | int bpf_program__set_flags(struct bpf_program *prog, __u32 flags) |
9247 | 0 | { |
9248 | 0 | if (prog->obj->loaded) |
9249 | 0 | return libbpf_err(-EBUSY); |
9250 | | |
9251 | 0 | prog->prog_flags = flags; |
9252 | 0 | return 0; |
9253 | 0 | } |
9254 | | |
9255 | | __u32 bpf_program__log_level(const struct bpf_program *prog) |
9256 | 0 | { |
9257 | 0 | return prog->log_level; |
9258 | 0 | } |
9259 | | |
9260 | | int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level) |
9261 | 0 | { |
9262 | 0 | if (prog->obj->loaded) |
9263 | 0 | return libbpf_err(-EBUSY); |
9264 | | |
9265 | 0 | prog->log_level = log_level; |
9266 | 0 | return 0; |
9267 | 0 | } |
9268 | | |
9269 | | const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size) |
9270 | 0 | { |
9271 | 0 | *log_size = prog->log_size; |
9272 | 0 | return prog->log_buf; |
9273 | 0 | } |
9274 | | |
9275 | | int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size) |
9276 | 0 | { |
9277 | 0 | if (log_size && !log_buf) |
9278 | 0 | return -EINVAL; |
9279 | 0 | if (prog->log_size > UINT_MAX) |
9280 | 0 | return -EINVAL; |
9281 | 0 | if (prog->obj->loaded) |
9282 | 0 | return -EBUSY; |
9283 | | |
9284 | 0 | prog->log_buf = log_buf; |
9285 | 0 | prog->log_size = log_size; |
9286 | 0 | return 0; |
9287 | 0 | } |
9288 | | |
9289 | | #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \ |
9290 | | .sec = (char *)sec_pfx, \ |
9291 | | .prog_type = BPF_PROG_TYPE_##ptype, \ |
9292 | | .expected_attach_type = atype, \ |
9293 | | .cookie = (long)(flags), \ |
9294 | | .prog_prepare_load_fn = libbpf_prepare_prog_load, \ |
9295 | | __VA_ARGS__ \ |
9296 | | } |
9297 | | |
9298 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9299 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9300 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9301 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9302 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9303 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9304 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9305 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9306 | | static int attach_kprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9307 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9308 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9309 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
9310 | | |
9311 | | static const struct bpf_sec_def section_defs[] = { |
9312 | | SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE), |
9313 | | SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE), |
9314 | | SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE), |
9315 | | SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
9316 | | SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
9317 | | SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
9318 | | SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
9319 | | SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
9320 | | SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
9321 | | SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
9322 | | SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
9323 | | SEC_DEF("kprobe.session+", KPROBE, BPF_TRACE_KPROBE_SESSION, SEC_NONE, attach_kprobe_session), |
9324 | | SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
9325 | | SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
9326 | | SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
9327 | | SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
9328 | | SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
9329 | | SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
9330 | | SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt), |
9331 | | SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt), |
9332 | | SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */ |
9333 | | SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */ |
9334 | | SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), |
9335 | | SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), |
9336 | | SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
9337 | | SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
9338 | | SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
9339 | | SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE), |
9340 | | SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE), |
9341 | | SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
9342 | | SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
9343 | | SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
9344 | | SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
9345 | | SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
9346 | | SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
9347 | | SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace), |
9348 | | SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace), |
9349 | | SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace), |
9350 | | SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace), |
9351 | | SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
9352 | | SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
9353 | | SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
9354 | | SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace), |
9355 | | SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm), |
9356 | | SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm), |
9357 | | SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF), |
9358 | | SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter), |
9359 | | SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter), |
9360 | | SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE), |
9361 | | SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS), |
9362 | | SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE), |
9363 | | SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS), |
9364 | | SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE), |
9365 | | SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS), |
9366 | | SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT), |
9367 | | SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE), |
9368 | | SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE), |
9369 | | SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE), |
9370 | | SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE), |
9371 | | SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE), |
9372 | | SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT), |
9373 | | SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT), |
9374 | | SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT), |
9375 | | SEC_DEF("sk_skb/verdict", SK_SKB, BPF_SK_SKB_VERDICT, SEC_ATTACHABLE_OPT), |
9376 | | SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE), |
9377 | | SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT), |
9378 | | SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT), |
9379 | | SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT), |
9380 | | SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT), |
9381 | | SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT), |
9382 | | SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE), |
9383 | | SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE), |
9384 | | SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE), |
9385 | | SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT), |
9386 | | SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE), |
9387 | | SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE), |
9388 | | SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE), |
9389 | | SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE), |
9390 | | SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE), |
9391 | | SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE), |
9392 | | SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE), |
9393 | | SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE), |
9394 | | SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE), |
9395 | | SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE), |
9396 | | SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE), |
9397 | | SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE), |
9398 | | SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE), |
9399 | | SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE), |
9400 | | SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE), |
9401 | | SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE), |
9402 | | SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE), |
9403 | | SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE), |
9404 | | SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE), |
9405 | | SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE), |
9406 | | SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE), |
9407 | | SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE), |
9408 | | SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT), |
9409 | | SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), |
9410 | | SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), |
9411 | | SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), |
9412 | | SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE), |
9413 | | }; |
9414 | | |
9415 | | int libbpf_register_prog_handler(const char *sec, |
9416 | | enum bpf_prog_type prog_type, |
9417 | | enum bpf_attach_type exp_attach_type, |
9418 | | const struct libbpf_prog_handler_opts *opts) |
9419 | 0 | { |
9420 | 0 | struct bpf_sec_def *sec_def; |
9421 | |
|
9422 | 0 | if (!OPTS_VALID(opts, libbpf_prog_handler_opts)) |
9423 | 0 | return libbpf_err(-EINVAL); |
9424 | | |
9425 | 0 | if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */ |
9426 | 0 | return libbpf_err(-E2BIG); |
9427 | | |
9428 | 0 | if (sec) { |
9429 | 0 | sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1, |
9430 | 0 | sizeof(*sec_def)); |
9431 | 0 | if (!sec_def) |
9432 | 0 | return libbpf_err(-ENOMEM); |
9433 | | |
9434 | 0 | custom_sec_defs = sec_def; |
9435 | 0 | sec_def = &custom_sec_defs[custom_sec_def_cnt]; |
9436 | 0 | } else { |
9437 | 0 | if (has_custom_fallback_def) |
9438 | 0 | return libbpf_err(-EBUSY); |
9439 | | |
9440 | 0 | sec_def = &custom_fallback_def; |
9441 | 0 | } |
9442 | | |
9443 | 0 | sec_def->sec = sec ? strdup(sec) : NULL; |
9444 | 0 | if (sec && !sec_def->sec) |
9445 | 0 | return libbpf_err(-ENOMEM); |
9446 | | |
9447 | 0 | sec_def->prog_type = prog_type; |
9448 | 0 | sec_def->expected_attach_type = exp_attach_type; |
9449 | 0 | sec_def->cookie = OPTS_GET(opts, cookie, 0); |
9450 | |
|
9451 | 0 | sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); |
9452 | 0 | sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); |
9453 | 0 | sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); |
9454 | |
|
9455 | 0 | sec_def->handler_id = ++last_custom_sec_def_handler_id; |
9456 | |
|
9457 | 0 | if (sec) |
9458 | 0 | custom_sec_def_cnt++; |
9459 | 0 | else |
9460 | 0 | has_custom_fallback_def = true; |
9461 | |
|
9462 | 0 | return sec_def->handler_id; |
9463 | 0 | } |
9464 | | |
9465 | | int libbpf_unregister_prog_handler(int handler_id) |
9466 | 0 | { |
9467 | 0 | struct bpf_sec_def *sec_defs; |
9468 | 0 | int i; |
9469 | |
|
9470 | 0 | if (handler_id <= 0) |
9471 | 0 | return libbpf_err(-EINVAL); |
9472 | | |
9473 | 0 | if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) { |
9474 | 0 | memset(&custom_fallback_def, 0, sizeof(custom_fallback_def)); |
9475 | 0 | has_custom_fallback_def = false; |
9476 | 0 | return 0; |
9477 | 0 | } |
9478 | | |
9479 | 0 | for (i = 0; i < custom_sec_def_cnt; i++) { |
9480 | 0 | if (custom_sec_defs[i].handler_id == handler_id) |
9481 | 0 | break; |
9482 | 0 | } |
9483 | |
|
9484 | 0 | if (i == custom_sec_def_cnt) |
9485 | 0 | return libbpf_err(-ENOENT); |
9486 | | |
9487 | 0 | free(custom_sec_defs[i].sec); |
9488 | 0 | for (i = i + 1; i < custom_sec_def_cnt; i++) |
9489 | 0 | custom_sec_defs[i - 1] = custom_sec_defs[i]; |
9490 | 0 | custom_sec_def_cnt--; |
9491 | | |
9492 | | /* try to shrink the array, but it's ok if we couldn't */ |
9493 | 0 | sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs)); |
9494 | | /* if new count is zero, reallocarray can return a valid NULL result; |
9495 | | * in this case the previous pointer will be freed, so we *have to* |
9496 | | * reassign old pointer to the new value (even if it's NULL) |
9497 | | */ |
9498 | 0 | if (sec_defs || custom_sec_def_cnt == 0) |
9499 | 0 | custom_sec_defs = sec_defs; |
9500 | |
|
9501 | 0 | return 0; |
9502 | 0 | } |
9503 | | |
9504 | | static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name) |
9505 | 1.03M | { |
9506 | 1.03M | size_t len = strlen(sec_def->sec); |
9507 | | |
9508 | | /* "type/" always has to have proper SEC("type/extras") form */ |
9509 | 1.03M | if (sec_def->sec[len - 1] == '/') { |
9510 | 0 | if (str_has_pfx(sec_name, sec_def->sec)) |
9511 | 0 | return true; |
9512 | 0 | return false; |
9513 | 0 | } |
9514 | | |
9515 | | /* "type+" means it can be either exact SEC("type") or |
9516 | | * well-formed SEC("type/extras") with proper '/' separator |
9517 | | */ |
9518 | 1.03M | if (sec_def->sec[len - 1] == '+') { |
9519 | 401k | len--; |
9520 | | /* not even a prefix */ |
9521 | 401k | if (strncmp(sec_name, sec_def->sec, len) != 0) |
9522 | 399k | return false; |
9523 | | /* exact match or has '/' separator */ |
9524 | 1.68k | if (sec_name[len] == '\0' || sec_name[len] == '/') |
9525 | 727 | return true; |
9526 | 953 | return false; |
9527 | 1.68k | } |
9528 | | |
9529 | 631k | return strcmp(sec_name, sec_def->sec) == 0; |
9530 | 1.03M | } |
9531 | | |
9532 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name) |
9533 | 10.7k | { |
9534 | 10.7k | const struct bpf_sec_def *sec_def; |
9535 | 10.7k | int i, n; |
9536 | | |
9537 | 10.7k | n = custom_sec_def_cnt; |
9538 | 10.7k | for (i = 0; i < n; i++) { |
9539 | 0 | sec_def = &custom_sec_defs[i]; |
9540 | 0 | if (sec_def_matches(sec_def, sec_name)) |
9541 | 0 | return sec_def; |
9542 | 0 | } |
9543 | | |
9544 | 10.7k | n = ARRAY_SIZE(section_defs); |
9545 | 1.04M | for (i = 0; i < n; i++) { |
9546 | 1.03M | sec_def = §ion_defs[i]; |
9547 | 1.03M | if (sec_def_matches(sec_def, sec_name)) |
9548 | 1.24k | return sec_def; |
9549 | 1.03M | } |
9550 | | |
9551 | 9.50k | if (has_custom_fallback_def) |
9552 | 0 | return &custom_fallback_def; |
9553 | | |
9554 | 9.50k | return NULL; |
9555 | 9.50k | } |
9556 | | |
9557 | 0 | #define MAX_TYPE_NAME_SIZE 32 |
9558 | | |
9559 | | static char *libbpf_get_type_names(bool attach_type) |
9560 | 0 | { |
9561 | 0 | int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE; |
9562 | 0 | char *buf; |
9563 | |
|
9564 | 0 | buf = malloc(len); |
9565 | 0 | if (!buf) |
9566 | 0 | return NULL; |
9567 | | |
9568 | 0 | buf[0] = '\0'; |
9569 | | /* Forge string buf with all available names */ |
9570 | 0 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
9571 | 0 | const struct bpf_sec_def *sec_def = §ion_defs[i]; |
9572 | |
|
9573 | 0 | if (attach_type) { |
9574 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
9575 | 0 | continue; |
9576 | | |
9577 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
9578 | 0 | continue; |
9579 | 0 | } |
9580 | | |
9581 | 0 | if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) { |
9582 | 0 | free(buf); |
9583 | 0 | return NULL; |
9584 | 0 | } |
9585 | 0 | strcat(buf, " "); |
9586 | 0 | strcat(buf, section_defs[i].sec); |
9587 | 0 | } |
9588 | | |
9589 | 0 | return buf; |
9590 | 0 | } |
9591 | | |
9592 | | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
9593 | | enum bpf_attach_type *expected_attach_type) |
9594 | 0 | { |
9595 | 0 | const struct bpf_sec_def *sec_def; |
9596 | 0 | char *type_names; |
9597 | |
|
9598 | 0 | if (!name) |
9599 | 0 | return libbpf_err(-EINVAL); |
9600 | | |
9601 | 0 | sec_def = find_sec_def(name); |
9602 | 0 | if (sec_def) { |
9603 | 0 | *prog_type = sec_def->prog_type; |
9604 | 0 | *expected_attach_type = sec_def->expected_attach_type; |
9605 | 0 | return 0; |
9606 | 0 | } |
9607 | | |
9608 | 0 | pr_debug("failed to guess program type from ELF section '%s'\n", name); |
9609 | 0 | type_names = libbpf_get_type_names(false); |
9610 | 0 | if (type_names != NULL) { |
9611 | 0 | pr_debug("supported section(type) names are:%s\n", type_names); |
9612 | 0 | free(type_names); |
9613 | 0 | } |
9614 | |
|
9615 | 0 | return libbpf_err(-ESRCH); |
9616 | 0 | } |
9617 | | |
9618 | | const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t) |
9619 | 0 | { |
9620 | 0 | if (t < 0 || t >= ARRAY_SIZE(attach_type_name)) |
9621 | 0 | return NULL; |
9622 | | |
9623 | 0 | return attach_type_name[t]; |
9624 | 0 | } |
9625 | | |
9626 | | const char *libbpf_bpf_link_type_str(enum bpf_link_type t) |
9627 | 0 | { |
9628 | 0 | if (t < 0 || t >= ARRAY_SIZE(link_type_name)) |
9629 | 0 | return NULL; |
9630 | | |
9631 | 0 | return link_type_name[t]; |
9632 | 0 | } |
9633 | | |
9634 | | const char *libbpf_bpf_map_type_str(enum bpf_map_type t) |
9635 | 0 | { |
9636 | 0 | if (t < 0 || t >= ARRAY_SIZE(map_type_name)) |
9637 | 0 | return NULL; |
9638 | | |
9639 | 0 | return map_type_name[t]; |
9640 | 0 | } |
9641 | | |
9642 | | const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t) |
9643 | 0 | { |
9644 | 0 | if (t < 0 || t >= ARRAY_SIZE(prog_type_name)) |
9645 | 0 | return NULL; |
9646 | | |
9647 | 0 | return prog_type_name[t]; |
9648 | 0 | } |
9649 | | |
9650 | | static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj, |
9651 | | int sec_idx, |
9652 | | size_t offset) |
9653 | 0 | { |
9654 | 0 | struct bpf_map *map; |
9655 | 0 | size_t i; |
9656 | |
|
9657 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
9658 | 0 | map = &obj->maps[i]; |
9659 | 0 | if (!bpf_map__is_struct_ops(map)) |
9660 | 0 | continue; |
9661 | 0 | if (map->sec_idx == sec_idx && |
9662 | 0 | map->sec_offset <= offset && |
9663 | 0 | offset - map->sec_offset < map->def.value_size) |
9664 | 0 | return map; |
9665 | 0 | } |
9666 | | |
9667 | 0 | return NULL; |
9668 | 0 | } |
9669 | | |
9670 | | /* Collect the reloc from ELF, populate the st_ops->progs[], and update |
9671 | | * st_ops->data for shadow type. |
9672 | | */ |
9673 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
9674 | | Elf64_Shdr *shdr, Elf_Data *data) |
9675 | 0 | { |
9676 | 0 | const struct btf_member *member; |
9677 | 0 | struct bpf_struct_ops *st_ops; |
9678 | 0 | struct bpf_program *prog; |
9679 | 0 | unsigned int shdr_idx; |
9680 | 0 | const struct btf *btf; |
9681 | 0 | struct bpf_map *map; |
9682 | 0 | unsigned int moff, insn_idx; |
9683 | 0 | const char *name; |
9684 | 0 | __u32 member_idx; |
9685 | 0 | Elf64_Sym *sym; |
9686 | 0 | Elf64_Rel *rel; |
9687 | 0 | int i, nrels; |
9688 | |
|
9689 | 0 | btf = obj->btf; |
9690 | 0 | nrels = shdr->sh_size / shdr->sh_entsize; |
9691 | 0 | for (i = 0; i < nrels; i++) { |
9692 | 0 | rel = elf_rel_by_idx(data, i); |
9693 | 0 | if (!rel) { |
9694 | 0 | pr_warn("struct_ops reloc: failed to get %d reloc\n", i); |
9695 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9696 | 0 | } |
9697 | | |
9698 | 0 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
9699 | 0 | if (!sym) { |
9700 | 0 | pr_warn("struct_ops reloc: symbol %zx not found\n", |
9701 | 0 | (size_t)ELF64_R_SYM(rel->r_info)); |
9702 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9703 | 0 | } |
9704 | | |
9705 | 0 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
9706 | 0 | map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); |
9707 | 0 | if (!map) { |
9708 | 0 | pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", |
9709 | 0 | (size_t)rel->r_offset); |
9710 | 0 | return -EINVAL; |
9711 | 0 | } |
9712 | | |
9713 | 0 | moff = rel->r_offset - map->sec_offset; |
9714 | 0 | shdr_idx = sym->st_shndx; |
9715 | 0 | st_ops = map->st_ops; |
9716 | 0 | pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %d (\'%s\')\n", |
9717 | 0 | map->name, |
9718 | 0 | (long long)(rel->r_info >> 32), |
9719 | 0 | (long long)sym->st_value, |
9720 | 0 | shdr_idx, (size_t)rel->r_offset, |
9721 | 0 | map->sec_offset, sym->st_name, name); |
9722 | |
|
9723 | 0 | if (shdr_idx >= SHN_LORESERVE) { |
9724 | 0 | pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n", |
9725 | 0 | map->name, (size_t)rel->r_offset, shdr_idx); |
9726 | 0 | return -LIBBPF_ERRNO__RELOC; |
9727 | 0 | } |
9728 | 0 | if (sym->st_value % BPF_INSN_SZ) { |
9729 | 0 | pr_warn("struct_ops reloc %s: invalid target program offset %llu\n", |
9730 | 0 | map->name, (unsigned long long)sym->st_value); |
9731 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9732 | 0 | } |
9733 | 0 | insn_idx = sym->st_value / BPF_INSN_SZ; |
9734 | |
|
9735 | 0 | member = find_member_by_offset(st_ops->type, moff * 8); |
9736 | 0 | if (!member) { |
9737 | 0 | pr_warn("struct_ops reloc %s: cannot find member at moff %u\n", |
9738 | 0 | map->name, moff); |
9739 | 0 | return -EINVAL; |
9740 | 0 | } |
9741 | 0 | member_idx = member - btf_members(st_ops->type); |
9742 | 0 | name = btf__name_by_offset(btf, member->name_off); |
9743 | |
|
9744 | 0 | if (!resolve_func_ptr(btf, member->type, NULL)) { |
9745 | 0 | pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n", |
9746 | 0 | map->name, name); |
9747 | 0 | return -EINVAL; |
9748 | 0 | } |
9749 | | |
9750 | 0 | prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx); |
9751 | 0 | if (!prog) { |
9752 | 0 | pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n", |
9753 | 0 | map->name, shdr_idx, name); |
9754 | 0 | return -EINVAL; |
9755 | 0 | } |
9756 | | |
9757 | | /* prevent the use of BPF prog with invalid type */ |
9758 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { |
9759 | 0 | pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n", |
9760 | 0 | map->name, prog->name); |
9761 | 0 | return -EINVAL; |
9762 | 0 | } |
9763 | | |
9764 | 0 | st_ops->progs[member_idx] = prog; |
9765 | | |
9766 | | /* st_ops->data will be exposed to users, being returned by |
9767 | | * bpf_map__initial_value() as a pointer to the shadow |
9768 | | * type. All function pointers in the original struct type |
9769 | | * should be converted to a pointer to struct bpf_program |
9770 | | * in the shadow type. |
9771 | | */ |
9772 | 0 | *((struct bpf_program **)(st_ops->data + moff)) = prog; |
9773 | 0 | } |
9774 | | |
9775 | 0 | return 0; |
9776 | 0 | } |
9777 | | |
9778 | 0 | #define BTF_TRACE_PREFIX "btf_trace_" |
9779 | 0 | #define BTF_LSM_PREFIX "bpf_lsm_" |
9780 | 0 | #define BTF_ITER_PREFIX "bpf_iter_" |
9781 | | #define BTF_MAX_NAME_SIZE 128 |
9782 | | |
9783 | | void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, |
9784 | | const char **prefix, int *kind) |
9785 | 0 | { |
9786 | 0 | switch (attach_type) { |
9787 | 0 | case BPF_TRACE_RAW_TP: |
9788 | 0 | *prefix = BTF_TRACE_PREFIX; |
9789 | 0 | *kind = BTF_KIND_TYPEDEF; |
9790 | 0 | break; |
9791 | 0 | case BPF_LSM_MAC: |
9792 | 0 | case BPF_LSM_CGROUP: |
9793 | 0 | *prefix = BTF_LSM_PREFIX; |
9794 | 0 | *kind = BTF_KIND_FUNC; |
9795 | 0 | break; |
9796 | 0 | case BPF_TRACE_ITER: |
9797 | 0 | *prefix = BTF_ITER_PREFIX; |
9798 | 0 | *kind = BTF_KIND_FUNC; |
9799 | 0 | break; |
9800 | 0 | default: |
9801 | 0 | *prefix = ""; |
9802 | 0 | *kind = BTF_KIND_FUNC; |
9803 | 0 | } |
9804 | 0 | } |
9805 | | |
9806 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
9807 | | const char *name, __u32 kind) |
9808 | 0 | { |
9809 | 0 | char btf_type_name[BTF_MAX_NAME_SIZE]; |
9810 | 0 | int ret; |
9811 | |
|
9812 | 0 | ret = snprintf(btf_type_name, sizeof(btf_type_name), |
9813 | 0 | "%s%s", prefix, name); |
9814 | | /* snprintf returns the number of characters written excluding the |
9815 | | * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it |
9816 | | * indicates truncation. |
9817 | | */ |
9818 | 0 | if (ret < 0 || ret >= sizeof(btf_type_name)) |
9819 | 0 | return -ENAMETOOLONG; |
9820 | 0 | return btf__find_by_name_kind(btf, btf_type_name, kind); |
9821 | 0 | } |
9822 | | |
9823 | | static inline int find_attach_btf_id(struct btf *btf, const char *name, |
9824 | | enum bpf_attach_type attach_type) |
9825 | 0 | { |
9826 | 0 | const char *prefix; |
9827 | 0 | int kind; |
9828 | |
|
9829 | 0 | btf_get_kernel_prefix_kind(attach_type, &prefix, &kind); |
9830 | 0 | return find_btf_by_prefix_kind(btf, prefix, name, kind); |
9831 | 0 | } |
9832 | | |
9833 | | int libbpf_find_vmlinux_btf_id(const char *name, |
9834 | | enum bpf_attach_type attach_type) |
9835 | 0 | { |
9836 | 0 | struct btf *btf; |
9837 | 0 | int err; |
9838 | |
|
9839 | 0 | btf = btf__load_vmlinux_btf(); |
9840 | 0 | err = libbpf_get_error(btf); |
9841 | 0 | if (err) { |
9842 | 0 | pr_warn("vmlinux BTF is not found\n"); |
9843 | 0 | return libbpf_err(err); |
9844 | 0 | } |
9845 | | |
9846 | 0 | err = find_attach_btf_id(btf, name, attach_type); |
9847 | 0 | if (err <= 0) |
9848 | 0 | pr_warn("%s is not found in vmlinux BTF\n", name); |
9849 | |
|
9850 | 0 | btf__free(btf); |
9851 | 0 | return libbpf_err(err); |
9852 | 0 | } |
9853 | | |
9854 | | static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) |
9855 | 0 | { |
9856 | 0 | struct bpf_prog_info info; |
9857 | 0 | __u32 info_len = sizeof(info); |
9858 | 0 | struct btf *btf; |
9859 | 0 | int err; |
9860 | |
|
9861 | 0 | memset(&info, 0, info_len); |
9862 | 0 | err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len); |
9863 | 0 | if (err) { |
9864 | 0 | pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %d\n", |
9865 | 0 | attach_prog_fd, err); |
9866 | 0 | return err; |
9867 | 0 | } |
9868 | | |
9869 | 0 | err = -EINVAL; |
9870 | 0 | if (!info.btf_id) { |
9871 | 0 | pr_warn("The target program doesn't have BTF\n"); |
9872 | 0 | goto out; |
9873 | 0 | } |
9874 | 0 | btf = btf__load_from_kernel_by_id(info.btf_id); |
9875 | 0 | err = libbpf_get_error(btf); |
9876 | 0 | if (err) { |
9877 | 0 | pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err); |
9878 | 0 | goto out; |
9879 | 0 | } |
9880 | 0 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
9881 | 0 | btf__free(btf); |
9882 | 0 | if (err <= 0) { |
9883 | 0 | pr_warn("%s is not found in prog's BTF\n", name); |
9884 | 0 | goto out; |
9885 | 0 | } |
9886 | 0 | out: |
9887 | 0 | return err; |
9888 | 0 | } |
9889 | | |
9890 | | static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name, |
9891 | | enum bpf_attach_type attach_type, |
9892 | | int *btf_obj_fd, int *btf_type_id) |
9893 | 0 | { |
9894 | 0 | int ret, i, mod_len; |
9895 | 0 | const char *fn_name, *mod_name = NULL; |
9896 | |
|
9897 | 0 | fn_name = strchr(attach_name, ':'); |
9898 | 0 | if (fn_name) { |
9899 | 0 | mod_name = attach_name; |
9900 | 0 | mod_len = fn_name - mod_name; |
9901 | 0 | fn_name++; |
9902 | 0 | } |
9903 | |
|
9904 | 0 | if (!mod_name || strncmp(mod_name, "vmlinux", mod_len) == 0) { |
9905 | 0 | ret = find_attach_btf_id(obj->btf_vmlinux, |
9906 | 0 | mod_name ? fn_name : attach_name, |
9907 | 0 | attach_type); |
9908 | 0 | if (ret > 0) { |
9909 | 0 | *btf_obj_fd = 0; /* vmlinux BTF */ |
9910 | 0 | *btf_type_id = ret; |
9911 | 0 | return 0; |
9912 | 0 | } |
9913 | 0 | if (ret != -ENOENT) |
9914 | 0 | return ret; |
9915 | 0 | } |
9916 | | |
9917 | 0 | ret = load_module_btfs(obj); |
9918 | 0 | if (ret) |
9919 | 0 | return ret; |
9920 | | |
9921 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
9922 | 0 | const struct module_btf *mod = &obj->btf_modules[i]; |
9923 | |
|
9924 | 0 | if (mod_name && strncmp(mod->name, mod_name, mod_len) != 0) |
9925 | 0 | continue; |
9926 | | |
9927 | 0 | ret = find_attach_btf_id(mod->btf, |
9928 | 0 | mod_name ? fn_name : attach_name, |
9929 | 0 | attach_type); |
9930 | 0 | if (ret > 0) { |
9931 | 0 | *btf_obj_fd = mod->fd; |
9932 | 0 | *btf_type_id = ret; |
9933 | 0 | return 0; |
9934 | 0 | } |
9935 | 0 | if (ret == -ENOENT) |
9936 | 0 | continue; |
9937 | | |
9938 | 0 | return ret; |
9939 | 0 | } |
9940 | | |
9941 | 0 | return -ESRCH; |
9942 | 0 | } |
9943 | | |
9944 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
9945 | | int *btf_obj_fd, int *btf_type_id) |
9946 | 0 | { |
9947 | 0 | enum bpf_attach_type attach_type = prog->expected_attach_type; |
9948 | 0 | __u32 attach_prog_fd = prog->attach_prog_fd; |
9949 | 0 | int err = 0; |
9950 | | |
9951 | | /* BPF program's BTF ID */ |
9952 | 0 | if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { |
9953 | 0 | if (!attach_prog_fd) { |
9954 | 0 | pr_warn("prog '%s': attach program FD is not set\n", prog->name); |
9955 | 0 | return -EINVAL; |
9956 | 0 | } |
9957 | 0 | err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd); |
9958 | 0 | if (err < 0) { |
9959 | 0 | pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n", |
9960 | 0 | prog->name, attach_prog_fd, attach_name, err); |
9961 | 0 | return err; |
9962 | 0 | } |
9963 | 0 | *btf_obj_fd = 0; |
9964 | 0 | *btf_type_id = err; |
9965 | 0 | return 0; |
9966 | 0 | } |
9967 | | |
9968 | | /* kernel/module BTF ID */ |
9969 | 0 | if (prog->obj->gen_loader) { |
9970 | 0 | bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); |
9971 | 0 | *btf_obj_fd = 0; |
9972 | 0 | *btf_type_id = 1; |
9973 | 0 | } else { |
9974 | 0 | err = find_kernel_btf_id(prog->obj, attach_name, |
9975 | 0 | attach_type, btf_obj_fd, |
9976 | 0 | btf_type_id); |
9977 | 0 | } |
9978 | 0 | if (err) { |
9979 | 0 | pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n", |
9980 | 0 | prog->name, attach_name, err); |
9981 | 0 | return err; |
9982 | 0 | } |
9983 | 0 | return 0; |
9984 | 0 | } |
9985 | | |
9986 | | int libbpf_attach_type_by_name(const char *name, |
9987 | | enum bpf_attach_type *attach_type) |
9988 | 0 | { |
9989 | 0 | char *type_names; |
9990 | 0 | const struct bpf_sec_def *sec_def; |
9991 | |
|
9992 | 0 | if (!name) |
9993 | 0 | return libbpf_err(-EINVAL); |
9994 | | |
9995 | 0 | sec_def = find_sec_def(name); |
9996 | 0 | if (!sec_def) { |
9997 | 0 | pr_debug("failed to guess attach type based on ELF section name '%s'\n", name); |
9998 | 0 | type_names = libbpf_get_type_names(true); |
9999 | 0 | if (type_names != NULL) { |
10000 | 0 | pr_debug("attachable section(type) names are:%s\n", type_names); |
10001 | 0 | free(type_names); |
10002 | 0 | } |
10003 | |
|
10004 | 0 | return libbpf_err(-EINVAL); |
10005 | 0 | } |
10006 | | |
10007 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
10008 | 0 | return libbpf_err(-EINVAL); |
10009 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
10010 | 0 | return libbpf_err(-EINVAL); |
10011 | | |
10012 | 0 | *attach_type = sec_def->expected_attach_type; |
10013 | 0 | return 0; |
10014 | 0 | } |
10015 | | |
10016 | | int bpf_map__fd(const struct bpf_map *map) |
10017 | 0 | { |
10018 | 0 | if (!map) |
10019 | 0 | return libbpf_err(-EINVAL); |
10020 | 0 | if (!map_is_created(map)) |
10021 | 0 | return -1; |
10022 | 0 | return map->fd; |
10023 | 0 | } |
10024 | | |
10025 | | static bool map_uses_real_name(const struct bpf_map *map) |
10026 | 1 | { |
10027 | | /* Since libbpf started to support custom .data.* and .rodata.* maps, |
10028 | | * their user-visible name differs from kernel-visible name. Users see |
10029 | | * such map's corresponding ELF section name as a map name. |
10030 | | * This check distinguishes .data/.rodata from .data.* and .rodata.* |
10031 | | * maps to know which name has to be returned to the user. |
10032 | | */ |
10033 | 1 | if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) |
10034 | 0 | return true; |
10035 | 1 | if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) |
10036 | 0 | return true; |
10037 | 1 | return false; |
10038 | 1 | } |
10039 | | |
10040 | | const char *bpf_map__name(const struct bpf_map *map) |
10041 | 1 | { |
10042 | 1 | if (!map) |
10043 | 0 | return NULL; |
10044 | | |
10045 | 1 | if (map_uses_real_name(map)) |
10046 | 0 | return map->real_name; |
10047 | | |
10048 | 1 | return map->name; |
10049 | 1 | } |
10050 | | |
10051 | | enum bpf_map_type bpf_map__type(const struct bpf_map *map) |
10052 | 0 | { |
10053 | 0 | return map->def.type; |
10054 | 0 | } |
10055 | | |
10056 | | int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type) |
10057 | 0 | { |
10058 | 0 | if (map_is_created(map)) |
10059 | 0 | return libbpf_err(-EBUSY); |
10060 | 0 | map->def.type = type; |
10061 | 0 | return 0; |
10062 | 0 | } |
10063 | | |
10064 | | __u32 bpf_map__map_flags(const struct bpf_map *map) |
10065 | 0 | { |
10066 | 0 | return map->def.map_flags; |
10067 | 0 | } |
10068 | | |
10069 | | int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags) |
10070 | 0 | { |
10071 | 0 | if (map_is_created(map)) |
10072 | 0 | return libbpf_err(-EBUSY); |
10073 | 0 | map->def.map_flags = flags; |
10074 | 0 | return 0; |
10075 | 0 | } |
10076 | | |
10077 | | __u64 bpf_map__map_extra(const struct bpf_map *map) |
10078 | 0 | { |
10079 | 0 | return map->map_extra; |
10080 | 0 | } |
10081 | | |
10082 | | int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra) |
10083 | 0 | { |
10084 | 0 | if (map_is_created(map)) |
10085 | 0 | return libbpf_err(-EBUSY); |
10086 | 0 | map->map_extra = map_extra; |
10087 | 0 | return 0; |
10088 | 0 | } |
10089 | | |
10090 | | __u32 bpf_map__numa_node(const struct bpf_map *map) |
10091 | 0 | { |
10092 | 0 | return map->numa_node; |
10093 | 0 | } |
10094 | | |
10095 | | int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node) |
10096 | 0 | { |
10097 | 0 | if (map_is_created(map)) |
10098 | 0 | return libbpf_err(-EBUSY); |
10099 | 0 | map->numa_node = numa_node; |
10100 | 0 | return 0; |
10101 | 0 | } |
10102 | | |
10103 | | __u32 bpf_map__key_size(const struct bpf_map *map) |
10104 | 0 | { |
10105 | 0 | return map->def.key_size; |
10106 | 0 | } |
10107 | | |
10108 | | int bpf_map__set_key_size(struct bpf_map *map, __u32 size) |
10109 | 0 | { |
10110 | 0 | if (map_is_created(map)) |
10111 | 0 | return libbpf_err(-EBUSY); |
10112 | 0 | map->def.key_size = size; |
10113 | 0 | return 0; |
10114 | 0 | } |
10115 | | |
10116 | | __u32 bpf_map__value_size(const struct bpf_map *map) |
10117 | 0 | { |
10118 | 0 | return map->def.value_size; |
10119 | 0 | } |
10120 | | |
10121 | | static int map_btf_datasec_resize(struct bpf_map *map, __u32 size) |
10122 | 0 | { |
10123 | 0 | struct btf *btf; |
10124 | 0 | struct btf_type *datasec_type, *var_type; |
10125 | 0 | struct btf_var_secinfo *var; |
10126 | 0 | const struct btf_type *array_type; |
10127 | 0 | const struct btf_array *array; |
10128 | 0 | int vlen, element_sz, new_array_id; |
10129 | 0 | __u32 nr_elements; |
10130 | | |
10131 | | /* check btf existence */ |
10132 | 0 | btf = bpf_object__btf(map->obj); |
10133 | 0 | if (!btf) |
10134 | 0 | return -ENOENT; |
10135 | | |
10136 | | /* verify map is datasec */ |
10137 | 0 | datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map)); |
10138 | 0 | if (!btf_is_datasec(datasec_type)) { |
10139 | 0 | pr_warn("map '%s': cannot be resized, map value type is not a datasec\n", |
10140 | 0 | bpf_map__name(map)); |
10141 | 0 | return -EINVAL; |
10142 | 0 | } |
10143 | | |
10144 | | /* verify datasec has at least one var */ |
10145 | 0 | vlen = btf_vlen(datasec_type); |
10146 | 0 | if (vlen == 0) { |
10147 | 0 | pr_warn("map '%s': cannot be resized, map value datasec is empty\n", |
10148 | 0 | bpf_map__name(map)); |
10149 | 0 | return -EINVAL; |
10150 | 0 | } |
10151 | | |
10152 | | /* verify last var in the datasec is an array */ |
10153 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
10154 | 0 | var_type = btf_type_by_id(btf, var->type); |
10155 | 0 | array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); |
10156 | 0 | if (!btf_is_array(array_type)) { |
10157 | 0 | pr_warn("map '%s': cannot be resized, last var must be an array\n", |
10158 | 0 | bpf_map__name(map)); |
10159 | 0 | return -EINVAL; |
10160 | 0 | } |
10161 | | |
10162 | | /* verify request size aligns with array */ |
10163 | 0 | array = btf_array(array_type); |
10164 | 0 | element_sz = btf__resolve_size(btf, array->type); |
10165 | 0 | if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { |
10166 | 0 | pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n", |
10167 | 0 | bpf_map__name(map), element_sz, size); |
10168 | 0 | return -EINVAL; |
10169 | 0 | } |
10170 | | |
10171 | | /* create a new array based on the existing array, but with new length */ |
10172 | 0 | nr_elements = (size - var->offset) / element_sz; |
10173 | 0 | new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); |
10174 | 0 | if (new_array_id < 0) |
10175 | 0 | return new_array_id; |
10176 | | |
10177 | | /* adding a new btf type invalidates existing pointers to btf objects, |
10178 | | * so refresh pointers before proceeding |
10179 | | */ |
10180 | 0 | datasec_type = btf_type_by_id(btf, map->btf_value_type_id); |
10181 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
10182 | 0 | var_type = btf_type_by_id(btf, var->type); |
10183 | | |
10184 | | /* finally update btf info */ |
10185 | 0 | datasec_type->size = size; |
10186 | 0 | var->size = size - var->offset; |
10187 | 0 | var_type->type = new_array_id; |
10188 | |
|
10189 | 0 | return 0; |
10190 | 0 | } |
10191 | | |
10192 | | int bpf_map__set_value_size(struct bpf_map *map, __u32 size) |
10193 | 0 | { |
10194 | 0 | if (map->obj->loaded || map->reused) |
10195 | 0 | return libbpf_err(-EBUSY); |
10196 | | |
10197 | 0 | if (map->mmaped) { |
10198 | 0 | size_t mmap_old_sz, mmap_new_sz; |
10199 | 0 | int err; |
10200 | |
|
10201 | 0 | if (map->def.type != BPF_MAP_TYPE_ARRAY) |
10202 | 0 | return -EOPNOTSUPP; |
10203 | | |
10204 | 0 | mmap_old_sz = bpf_map_mmap_sz(map); |
10205 | 0 | mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries); |
10206 | 0 | err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz); |
10207 | 0 | if (err) { |
10208 | 0 | pr_warn("map '%s': failed to resize memory-mapped region: %d\n", |
10209 | 0 | bpf_map__name(map), err); |
10210 | 0 | return err; |
10211 | 0 | } |
10212 | 0 | err = map_btf_datasec_resize(map, size); |
10213 | 0 | if (err && err != -ENOENT) { |
10214 | 0 | pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n", |
10215 | 0 | bpf_map__name(map), err); |
10216 | 0 | map->btf_value_type_id = 0; |
10217 | 0 | map->btf_key_type_id = 0; |
10218 | 0 | } |
10219 | 0 | } |
10220 | | |
10221 | 0 | map->def.value_size = size; |
10222 | 0 | return 0; |
10223 | 0 | } |
10224 | | |
10225 | | __u32 bpf_map__btf_key_type_id(const struct bpf_map *map) |
10226 | 0 | { |
10227 | 0 | return map ? map->btf_key_type_id : 0; |
10228 | 0 | } |
10229 | | |
10230 | | __u32 bpf_map__btf_value_type_id(const struct bpf_map *map) |
10231 | 0 | { |
10232 | 0 | return map ? map->btf_value_type_id : 0; |
10233 | 0 | } |
10234 | | |
10235 | | int bpf_map__set_initial_value(struct bpf_map *map, |
10236 | | const void *data, size_t size) |
10237 | 0 | { |
10238 | 0 | size_t actual_sz; |
10239 | |
|
10240 | 0 | if (map->obj->loaded || map->reused) |
10241 | 0 | return libbpf_err(-EBUSY); |
10242 | | |
10243 | 0 | if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG) |
10244 | 0 | return libbpf_err(-EINVAL); |
10245 | | |
10246 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) |
10247 | 0 | actual_sz = map->obj->arena_data_sz; |
10248 | 0 | else |
10249 | 0 | actual_sz = map->def.value_size; |
10250 | 0 | if (size != actual_sz) |
10251 | 0 | return libbpf_err(-EINVAL); |
10252 | | |
10253 | 0 | memcpy(map->mmaped, data, size); |
10254 | 0 | return 0; |
10255 | 0 | } |
10256 | | |
10257 | | void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize) |
10258 | 0 | { |
10259 | 0 | if (bpf_map__is_struct_ops(map)) { |
10260 | 0 | if (psize) |
10261 | 0 | *psize = map->def.value_size; |
10262 | 0 | return map->st_ops->data; |
10263 | 0 | } |
10264 | | |
10265 | 0 | if (!map->mmaped) |
10266 | 0 | return NULL; |
10267 | | |
10268 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) |
10269 | 0 | *psize = map->obj->arena_data_sz; |
10270 | 0 | else |
10271 | 0 | *psize = map->def.value_size; |
10272 | |
|
10273 | 0 | return map->mmaped; |
10274 | 0 | } |
10275 | | |
10276 | | bool bpf_map__is_internal(const struct bpf_map *map) |
10277 | 282 | { |
10278 | 282 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
10279 | 282 | } |
10280 | | |
10281 | | __u32 bpf_map__ifindex(const struct bpf_map *map) |
10282 | 0 | { |
10283 | 0 | return map->map_ifindex; |
10284 | 0 | } |
10285 | | |
10286 | | int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
10287 | 0 | { |
10288 | 0 | if (map_is_created(map)) |
10289 | 0 | return libbpf_err(-EBUSY); |
10290 | 0 | map->map_ifindex = ifindex; |
10291 | 0 | return 0; |
10292 | 0 | } |
10293 | | |
10294 | | int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) |
10295 | 0 | { |
10296 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) { |
10297 | 0 | pr_warn("error: unsupported map type\n"); |
10298 | 0 | return libbpf_err(-EINVAL); |
10299 | 0 | } |
10300 | 0 | if (map->inner_map_fd != -1) { |
10301 | 0 | pr_warn("error: inner_map_fd already specified\n"); |
10302 | 0 | return libbpf_err(-EINVAL); |
10303 | 0 | } |
10304 | 0 | if (map->inner_map) { |
10305 | 0 | bpf_map__destroy(map->inner_map); |
10306 | 0 | zfree(&map->inner_map); |
10307 | 0 | } |
10308 | 0 | map->inner_map_fd = fd; |
10309 | 0 | return 0; |
10310 | 0 | } |
10311 | | |
10312 | | static struct bpf_map * |
10313 | | __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) |
10314 | 0 | { |
10315 | 0 | ssize_t idx; |
10316 | 0 | struct bpf_map *s, *e; |
10317 | |
|
10318 | 0 | if (!obj || !obj->maps) |
10319 | 0 | return errno = EINVAL, NULL; |
10320 | | |
10321 | 0 | s = obj->maps; |
10322 | 0 | e = obj->maps + obj->nr_maps; |
10323 | |
|
10324 | 0 | if ((m < s) || (m >= e)) { |
10325 | 0 | pr_warn("error in %s: map handler doesn't belong to object\n", |
10326 | 0 | __func__); |
10327 | 0 | return errno = EINVAL, NULL; |
10328 | 0 | } |
10329 | | |
10330 | 0 | idx = (m - obj->maps) + i; |
10331 | 0 | if (idx >= obj->nr_maps || idx < 0) |
10332 | 0 | return NULL; |
10333 | 0 | return &obj->maps[idx]; |
10334 | 0 | } |
10335 | | |
10336 | | struct bpf_map * |
10337 | | bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev) |
10338 | 0 | { |
10339 | 0 | if (prev == NULL) |
10340 | 0 | return obj->maps; |
10341 | | |
10342 | 0 | return __bpf_map__iter(prev, obj, 1); |
10343 | 0 | } |
10344 | | |
10345 | | struct bpf_map * |
10346 | | bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next) |
10347 | 0 | { |
10348 | 0 | if (next == NULL) { |
10349 | 0 | if (!obj->nr_maps) |
10350 | 0 | return NULL; |
10351 | 0 | return obj->maps + obj->nr_maps - 1; |
10352 | 0 | } |
10353 | | |
10354 | 0 | return __bpf_map__iter(next, obj, -1); |
10355 | 0 | } |
10356 | | |
10357 | | struct bpf_map * |
10358 | | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name) |
10359 | 0 | { |
10360 | 0 | struct bpf_map *pos; |
10361 | |
|
10362 | 0 | bpf_object__for_each_map(pos, obj) { |
10363 | | /* if it's a special internal map name (which always starts |
10364 | | * with dot) then check if that special name matches the |
10365 | | * real map name (ELF section name) |
10366 | | */ |
10367 | 0 | if (name[0] == '.') { |
10368 | 0 | if (pos->real_name && strcmp(pos->real_name, name) == 0) |
10369 | 0 | return pos; |
10370 | 0 | continue; |
10371 | 0 | } |
10372 | | /* otherwise map name has to be an exact match */ |
10373 | 0 | if (map_uses_real_name(pos)) { |
10374 | 0 | if (strcmp(pos->real_name, name) == 0) |
10375 | 0 | return pos; |
10376 | 0 | continue; |
10377 | 0 | } |
10378 | 0 | if (strcmp(pos->name, name) == 0) |
10379 | 0 | return pos; |
10380 | 0 | } |
10381 | 0 | return errno = ENOENT, NULL; |
10382 | 0 | } |
10383 | | |
10384 | | int |
10385 | | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) |
10386 | 0 | { |
10387 | 0 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
10388 | 0 | } |
10389 | | |
10390 | | static int validate_map_op(const struct bpf_map *map, size_t key_sz, |
10391 | | size_t value_sz, bool check_value_sz) |
10392 | 0 | { |
10393 | 0 | if (!map_is_created(map)) /* map is not yet created */ |
10394 | 0 | return -ENOENT; |
10395 | | |
10396 | 0 | if (map->def.key_size != key_sz) { |
10397 | 0 | pr_warn("map '%s': unexpected key size %zu provided, expected %u\n", |
10398 | 0 | map->name, key_sz, map->def.key_size); |
10399 | 0 | return -EINVAL; |
10400 | 0 | } |
10401 | | |
10402 | 0 | if (map->fd < 0) { |
10403 | 0 | pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); |
10404 | 0 | return -EINVAL; |
10405 | 0 | } |
10406 | | |
10407 | 0 | if (!check_value_sz) |
10408 | 0 | return 0; |
10409 | | |
10410 | 0 | switch (map->def.type) { |
10411 | 0 | case BPF_MAP_TYPE_PERCPU_ARRAY: |
10412 | 0 | case BPF_MAP_TYPE_PERCPU_HASH: |
10413 | 0 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
10414 | 0 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: { |
10415 | 0 | int num_cpu = libbpf_num_possible_cpus(); |
10416 | 0 | size_t elem_sz = roundup(map->def.value_size, 8); |
10417 | |
|
10418 | 0 | if (value_sz != num_cpu * elem_sz) { |
10419 | 0 | pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n", |
10420 | 0 | map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); |
10421 | 0 | return -EINVAL; |
10422 | 0 | } |
10423 | 0 | break; |
10424 | 0 | } |
10425 | 0 | default: |
10426 | 0 | if (map->def.value_size != value_sz) { |
10427 | 0 | pr_warn("map '%s': unexpected value size %zu provided, expected %u\n", |
10428 | 0 | map->name, value_sz, map->def.value_size); |
10429 | 0 | return -EINVAL; |
10430 | 0 | } |
10431 | 0 | break; |
10432 | 0 | } |
10433 | 0 | return 0; |
10434 | 0 | } |
10435 | | |
10436 | | int bpf_map__lookup_elem(const struct bpf_map *map, |
10437 | | const void *key, size_t key_sz, |
10438 | | void *value, size_t value_sz, __u64 flags) |
10439 | 0 | { |
10440 | 0 | int err; |
10441 | |
|
10442 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
10443 | 0 | if (err) |
10444 | 0 | return libbpf_err(err); |
10445 | | |
10446 | 0 | return bpf_map_lookup_elem_flags(map->fd, key, value, flags); |
10447 | 0 | } |
10448 | | |
10449 | | int bpf_map__update_elem(const struct bpf_map *map, |
10450 | | const void *key, size_t key_sz, |
10451 | | const void *value, size_t value_sz, __u64 flags) |
10452 | 0 | { |
10453 | 0 | int err; |
10454 | |
|
10455 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
10456 | 0 | if (err) |
10457 | 0 | return libbpf_err(err); |
10458 | | |
10459 | 0 | return bpf_map_update_elem(map->fd, key, value, flags); |
10460 | 0 | } |
10461 | | |
10462 | | int bpf_map__delete_elem(const struct bpf_map *map, |
10463 | | const void *key, size_t key_sz, __u64 flags) |
10464 | 0 | { |
10465 | 0 | int err; |
10466 | |
|
10467 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */); |
10468 | 0 | if (err) |
10469 | 0 | return libbpf_err(err); |
10470 | | |
10471 | 0 | return bpf_map_delete_elem_flags(map->fd, key, flags); |
10472 | 0 | } |
10473 | | |
10474 | | int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, |
10475 | | const void *key, size_t key_sz, |
10476 | | void *value, size_t value_sz, __u64 flags) |
10477 | 0 | { |
10478 | 0 | int err; |
10479 | |
|
10480 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
10481 | 0 | if (err) |
10482 | 0 | return libbpf_err(err); |
10483 | | |
10484 | 0 | return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); |
10485 | 0 | } |
10486 | | |
10487 | | int bpf_map__get_next_key(const struct bpf_map *map, |
10488 | | const void *cur_key, void *next_key, size_t key_sz) |
10489 | 0 | { |
10490 | 0 | int err; |
10491 | |
|
10492 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */); |
10493 | 0 | if (err) |
10494 | 0 | return libbpf_err(err); |
10495 | | |
10496 | 0 | return bpf_map_get_next_key(map->fd, cur_key, next_key); |
10497 | 0 | } |
10498 | | |
10499 | | long libbpf_get_error(const void *ptr) |
10500 | 16.2k | { |
10501 | 16.2k | if (!IS_ERR_OR_NULL(ptr)) |
10502 | 5.56k | return 0; |
10503 | | |
10504 | 10.7k | if (IS_ERR(ptr)) |
10505 | 0 | errno = -PTR_ERR(ptr); |
10506 | | |
10507 | | /* If ptr == NULL, then errno should be already set by the failing |
10508 | | * API, because libbpf never returns NULL on success and it now always |
10509 | | * sets errno on error. So no extra errno handling for ptr == NULL |
10510 | | * case. |
10511 | | */ |
10512 | 10.7k | return -errno; |
10513 | 16.2k | } |
10514 | | |
10515 | | /* Replace link's underlying BPF program with the new one */ |
10516 | | int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog) |
10517 | 0 | { |
10518 | 0 | int ret; |
10519 | 0 | int prog_fd = bpf_program__fd(prog); |
10520 | |
|
10521 | 0 | if (prog_fd < 0) { |
10522 | 0 | pr_warn("prog '%s': can't use BPF program without FD (was it loaded?)\n", |
10523 | 0 | prog->name); |
10524 | 0 | return libbpf_err(-EINVAL); |
10525 | 0 | } |
10526 | | |
10527 | 0 | ret = bpf_link_update(bpf_link__fd(link), prog_fd, NULL); |
10528 | 0 | return libbpf_err_errno(ret); |
10529 | 0 | } |
10530 | | |
10531 | | /* Release "ownership" of underlying BPF resource (typically, BPF program |
10532 | | * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected |
10533 | | * link, when destructed through bpf_link__destroy() call won't attempt to |
10534 | | * detach/unregisted that BPF resource. This is useful in situations where, |
10535 | | * say, attached BPF program has to outlive userspace program that attached it |
10536 | | * in the system. Depending on type of BPF program, though, there might be |
10537 | | * additional steps (like pinning BPF program in BPF FS) necessary to ensure |
10538 | | * exit of userspace program doesn't trigger automatic detachment and clean up |
10539 | | * inside the kernel. |
10540 | | */ |
10541 | | void bpf_link__disconnect(struct bpf_link *link) |
10542 | 0 | { |
10543 | 0 | link->disconnected = true; |
10544 | 0 | } |
10545 | | |
10546 | | int bpf_link__destroy(struct bpf_link *link) |
10547 | 0 | { |
10548 | 0 | int err = 0; |
10549 | |
|
10550 | 0 | if (IS_ERR_OR_NULL(link)) |
10551 | 0 | return 0; |
10552 | | |
10553 | 0 | if (!link->disconnected && link->detach) |
10554 | 0 | err = link->detach(link); |
10555 | 0 | if (link->pin_path) |
10556 | 0 | free(link->pin_path); |
10557 | 0 | if (link->dealloc) |
10558 | 0 | link->dealloc(link); |
10559 | 0 | else |
10560 | 0 | free(link); |
10561 | |
|
10562 | 0 | return libbpf_err(err); |
10563 | 0 | } |
10564 | | |
10565 | | int bpf_link__fd(const struct bpf_link *link) |
10566 | 0 | { |
10567 | 0 | return link->fd; |
10568 | 0 | } |
10569 | | |
10570 | | const char *bpf_link__pin_path(const struct bpf_link *link) |
10571 | 0 | { |
10572 | 0 | return link->pin_path; |
10573 | 0 | } |
10574 | | |
10575 | | static int bpf_link__detach_fd(struct bpf_link *link) |
10576 | 0 | { |
10577 | 0 | return libbpf_err_errno(close(link->fd)); |
10578 | 0 | } |
10579 | | |
10580 | | struct bpf_link *bpf_link__open(const char *path) |
10581 | 0 | { |
10582 | 0 | struct bpf_link *link; |
10583 | 0 | int fd; |
10584 | |
|
10585 | 0 | fd = bpf_obj_get(path); |
10586 | 0 | if (fd < 0) { |
10587 | 0 | fd = -errno; |
10588 | 0 | pr_warn("failed to open link at %s: %d\n", path, fd); |
10589 | 0 | return libbpf_err_ptr(fd); |
10590 | 0 | } |
10591 | | |
10592 | 0 | link = calloc(1, sizeof(*link)); |
10593 | 0 | if (!link) { |
10594 | 0 | close(fd); |
10595 | 0 | return libbpf_err_ptr(-ENOMEM); |
10596 | 0 | } |
10597 | 0 | link->detach = &bpf_link__detach_fd; |
10598 | 0 | link->fd = fd; |
10599 | |
|
10600 | 0 | link->pin_path = strdup(path); |
10601 | 0 | if (!link->pin_path) { |
10602 | 0 | bpf_link__destroy(link); |
10603 | 0 | return libbpf_err_ptr(-ENOMEM); |
10604 | 0 | } |
10605 | | |
10606 | 0 | return link; |
10607 | 0 | } |
10608 | | |
10609 | | int bpf_link__detach(struct bpf_link *link) |
10610 | 0 | { |
10611 | 0 | return bpf_link_detach(link->fd) ? -errno : 0; |
10612 | 0 | } |
10613 | | |
10614 | | int bpf_link__pin(struct bpf_link *link, const char *path) |
10615 | 0 | { |
10616 | 0 | int err; |
10617 | |
|
10618 | 0 | if (link->pin_path) |
10619 | 0 | return libbpf_err(-EBUSY); |
10620 | 0 | err = make_parent_dir(path); |
10621 | 0 | if (err) |
10622 | 0 | return libbpf_err(err); |
10623 | 0 | err = check_path(path); |
10624 | 0 | if (err) |
10625 | 0 | return libbpf_err(err); |
10626 | | |
10627 | 0 | link->pin_path = strdup(path); |
10628 | 0 | if (!link->pin_path) |
10629 | 0 | return libbpf_err(-ENOMEM); |
10630 | | |
10631 | 0 | if (bpf_obj_pin(link->fd, link->pin_path)) { |
10632 | 0 | err = -errno; |
10633 | 0 | zfree(&link->pin_path); |
10634 | 0 | return libbpf_err(err); |
10635 | 0 | } |
10636 | | |
10637 | 0 | pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); |
10638 | 0 | return 0; |
10639 | 0 | } |
10640 | | |
10641 | | int bpf_link__unpin(struct bpf_link *link) |
10642 | 0 | { |
10643 | 0 | int err; |
10644 | |
|
10645 | 0 | if (!link->pin_path) |
10646 | 0 | return libbpf_err(-EINVAL); |
10647 | | |
10648 | 0 | err = unlink(link->pin_path); |
10649 | 0 | if (err != 0) |
10650 | 0 | return -errno; |
10651 | | |
10652 | 0 | pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); |
10653 | 0 | zfree(&link->pin_path); |
10654 | 0 | return 0; |
10655 | 0 | } |
10656 | | |
10657 | | struct bpf_link_perf { |
10658 | | struct bpf_link link; |
10659 | | int perf_event_fd; |
10660 | | /* legacy kprobe support: keep track of probe identifier and type */ |
10661 | | char *legacy_probe_name; |
10662 | | bool legacy_is_kprobe; |
10663 | | bool legacy_is_retprobe; |
10664 | | }; |
10665 | | |
10666 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe); |
10667 | | static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe); |
10668 | | |
10669 | | static int bpf_link_perf_detach(struct bpf_link *link) |
10670 | 0 | { |
10671 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
10672 | 0 | int err = 0; |
10673 | |
|
10674 | 0 | if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) |
10675 | 0 | err = -errno; |
10676 | |
|
10677 | 0 | if (perf_link->perf_event_fd != link->fd) |
10678 | 0 | close(perf_link->perf_event_fd); |
10679 | 0 | close(link->fd); |
10680 | | |
10681 | | /* legacy uprobe/kprobe needs to be removed after perf event fd closure */ |
10682 | 0 | if (perf_link->legacy_probe_name) { |
10683 | 0 | if (perf_link->legacy_is_kprobe) { |
10684 | 0 | err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, |
10685 | 0 | perf_link->legacy_is_retprobe); |
10686 | 0 | } else { |
10687 | 0 | err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, |
10688 | 0 | perf_link->legacy_is_retprobe); |
10689 | 0 | } |
10690 | 0 | } |
10691 | |
|
10692 | 0 | return err; |
10693 | 0 | } |
10694 | | |
10695 | | static void bpf_link_perf_dealloc(struct bpf_link *link) |
10696 | 0 | { |
10697 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
10698 | |
|
10699 | 0 | free(perf_link->legacy_probe_name); |
10700 | 0 | free(perf_link); |
10701 | 0 | } |
10702 | | |
10703 | | struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, |
10704 | | const struct bpf_perf_event_opts *opts) |
10705 | 0 | { |
10706 | 0 | char errmsg[STRERR_BUFSIZE]; |
10707 | 0 | struct bpf_link_perf *link; |
10708 | 0 | int prog_fd, link_fd = -1, err; |
10709 | 0 | bool force_ioctl_attach; |
10710 | |
|
10711 | 0 | if (!OPTS_VALID(opts, bpf_perf_event_opts)) |
10712 | 0 | return libbpf_err_ptr(-EINVAL); |
10713 | | |
10714 | 0 | if (pfd < 0) { |
10715 | 0 | pr_warn("prog '%s': invalid perf event FD %d\n", |
10716 | 0 | prog->name, pfd); |
10717 | 0 | return libbpf_err_ptr(-EINVAL); |
10718 | 0 | } |
10719 | 0 | prog_fd = bpf_program__fd(prog); |
10720 | 0 | if (prog_fd < 0) { |
10721 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
10722 | 0 | prog->name); |
10723 | 0 | return libbpf_err_ptr(-EINVAL); |
10724 | 0 | } |
10725 | | |
10726 | 0 | link = calloc(1, sizeof(*link)); |
10727 | 0 | if (!link) |
10728 | 0 | return libbpf_err_ptr(-ENOMEM); |
10729 | 0 | link->link.detach = &bpf_link_perf_detach; |
10730 | 0 | link->link.dealloc = &bpf_link_perf_dealloc; |
10731 | 0 | link->perf_event_fd = pfd; |
10732 | |
|
10733 | 0 | force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false); |
10734 | 0 | if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { |
10735 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts, |
10736 | 0 | .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0)); |
10737 | |
|
10738 | 0 | link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts); |
10739 | 0 | if (link_fd < 0) { |
10740 | 0 | err = -errno; |
10741 | 0 | pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n", |
10742 | 0 | prog->name, pfd, |
10743 | 0 | err, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10744 | 0 | goto err_out; |
10745 | 0 | } |
10746 | 0 | link->link.fd = link_fd; |
10747 | 0 | } else { |
10748 | 0 | if (OPTS_GET(opts, bpf_cookie, 0)) { |
10749 | 0 | pr_warn("prog '%s': user context value is not supported\n", prog->name); |
10750 | 0 | err = -EOPNOTSUPP; |
10751 | 0 | goto err_out; |
10752 | 0 | } |
10753 | | |
10754 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { |
10755 | 0 | err = -errno; |
10756 | 0 | pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n", |
10757 | 0 | prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10758 | 0 | if (err == -EPROTO) |
10759 | 0 | pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n", |
10760 | 0 | prog->name, pfd); |
10761 | 0 | goto err_out; |
10762 | 0 | } |
10763 | 0 | link->link.fd = pfd; |
10764 | 0 | } |
10765 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
10766 | 0 | err = -errno; |
10767 | 0 | pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n", |
10768 | 0 | prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10769 | 0 | goto err_out; |
10770 | 0 | } |
10771 | | |
10772 | 0 | return &link->link; |
10773 | 0 | err_out: |
10774 | 0 | if (link_fd >= 0) |
10775 | 0 | close(link_fd); |
10776 | 0 | free(link); |
10777 | 0 | return libbpf_err_ptr(err); |
10778 | 0 | } |
10779 | | |
10780 | | struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd) |
10781 | 0 | { |
10782 | 0 | return bpf_program__attach_perf_event_opts(prog, pfd, NULL); |
10783 | 0 | } |
10784 | | |
10785 | | /* |
10786 | | * this function is expected to parse integer in the range of [0, 2^31-1] from |
10787 | | * given file using scanf format string fmt. If actual parsed value is |
10788 | | * negative, the result might be indistinguishable from error |
10789 | | */ |
10790 | | static int parse_uint_from_file(const char *file, const char *fmt) |
10791 | 0 | { |
10792 | 0 | char buf[STRERR_BUFSIZE]; |
10793 | 0 | int err, ret; |
10794 | 0 | FILE *f; |
10795 | |
|
10796 | 0 | f = fopen(file, "re"); |
10797 | 0 | if (!f) { |
10798 | 0 | err = -errno; |
10799 | 0 | pr_debug("failed to open '%s': %s\n", file, |
10800 | 0 | libbpf_strerror_r(err, buf, sizeof(buf))); |
10801 | 0 | return err; |
10802 | 0 | } |
10803 | 0 | err = fscanf(f, fmt, &ret); |
10804 | 0 | if (err != 1) { |
10805 | 0 | err = err == EOF ? -EIO : -errno; |
10806 | 0 | pr_debug("failed to parse '%s': %s\n", file, |
10807 | 0 | libbpf_strerror_r(err, buf, sizeof(buf))); |
10808 | 0 | fclose(f); |
10809 | 0 | return err; |
10810 | 0 | } |
10811 | 0 | fclose(f); |
10812 | 0 | return ret; |
10813 | 0 | } |
10814 | | |
10815 | | static int determine_kprobe_perf_type(void) |
10816 | 0 | { |
10817 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/type"; |
10818 | |
|
10819 | 0 | return parse_uint_from_file(file, "%d\n"); |
10820 | 0 | } |
10821 | | |
10822 | | static int determine_uprobe_perf_type(void) |
10823 | 0 | { |
10824 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/type"; |
10825 | |
|
10826 | 0 | return parse_uint_from_file(file, "%d\n"); |
10827 | 0 | } |
10828 | | |
10829 | | static int determine_kprobe_retprobe_bit(void) |
10830 | 0 | { |
10831 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe"; |
10832 | |
|
10833 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
10834 | 0 | } |
10835 | | |
10836 | | static int determine_uprobe_retprobe_bit(void) |
10837 | 0 | { |
10838 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; |
10839 | |
|
10840 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
10841 | 0 | } |
10842 | | |
10843 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32 |
10844 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32 |
10845 | | |
10846 | | static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, |
10847 | | uint64_t offset, int pid, size_t ref_ctr_off) |
10848 | 0 | { |
10849 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
10850 | 0 | struct perf_event_attr attr; |
10851 | 0 | char errmsg[STRERR_BUFSIZE]; |
10852 | 0 | int type, pfd; |
10853 | |
|
10854 | 0 | if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS)) |
10855 | 0 | return -EINVAL; |
10856 | | |
10857 | 0 | memset(&attr, 0, attr_sz); |
10858 | |
|
10859 | 0 | type = uprobe ? determine_uprobe_perf_type() |
10860 | 0 | : determine_kprobe_perf_type(); |
10861 | 0 | if (type < 0) { |
10862 | 0 | pr_warn("failed to determine %s perf type: %s\n", |
10863 | 0 | uprobe ? "uprobe" : "kprobe", |
10864 | 0 | libbpf_strerror_r(type, errmsg, sizeof(errmsg))); |
10865 | 0 | return type; |
10866 | 0 | } |
10867 | 0 | if (retprobe) { |
10868 | 0 | int bit = uprobe ? determine_uprobe_retprobe_bit() |
10869 | 0 | : determine_kprobe_retprobe_bit(); |
10870 | |
|
10871 | 0 | if (bit < 0) { |
10872 | 0 | pr_warn("failed to determine %s retprobe bit: %s\n", |
10873 | 0 | uprobe ? "uprobe" : "kprobe", |
10874 | 0 | libbpf_strerror_r(bit, errmsg, sizeof(errmsg))); |
10875 | 0 | return bit; |
10876 | 0 | } |
10877 | 0 | attr.config |= 1 << bit; |
10878 | 0 | } |
10879 | 0 | attr.size = attr_sz; |
10880 | 0 | attr.type = type; |
10881 | 0 | attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
10882 | 0 | attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ |
10883 | 0 | attr.config2 = offset; /* kprobe_addr or probe_offset */ |
10884 | | |
10885 | | /* pid filter is meaningful only for uprobes */ |
10886 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
10887 | 0 | pid < 0 ? -1 : pid /* pid */, |
10888 | 0 | pid == -1 ? 0 : -1 /* cpu */, |
10889 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
10890 | 0 | return pfd >= 0 ? pfd : -errno; |
10891 | 0 | } |
10892 | | |
10893 | | static int append_to_file(const char *file, const char *fmt, ...) |
10894 | 0 | { |
10895 | 0 | int fd, n, err = 0; |
10896 | 0 | va_list ap; |
10897 | 0 | char buf[1024]; |
10898 | |
|
10899 | 0 | va_start(ap, fmt); |
10900 | 0 | n = vsnprintf(buf, sizeof(buf), fmt, ap); |
10901 | 0 | va_end(ap); |
10902 | |
|
10903 | 0 | if (n < 0 || n >= sizeof(buf)) |
10904 | 0 | return -EINVAL; |
10905 | | |
10906 | 0 | fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0); |
10907 | 0 | if (fd < 0) |
10908 | 0 | return -errno; |
10909 | | |
10910 | 0 | if (write(fd, buf, n) < 0) |
10911 | 0 | err = -errno; |
10912 | |
|
10913 | 0 | close(fd); |
10914 | 0 | return err; |
10915 | 0 | } |
10916 | | |
10917 | 0 | #define DEBUGFS "/sys/kernel/debug/tracing" |
10918 | 0 | #define TRACEFS "/sys/kernel/tracing" |
10919 | | |
10920 | | static bool use_debugfs(void) |
10921 | 0 | { |
10922 | 0 | static int has_debugfs = -1; |
10923 | |
|
10924 | 0 | if (has_debugfs < 0) |
10925 | 0 | has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0; |
10926 | |
|
10927 | 0 | return has_debugfs == 1; |
10928 | 0 | } |
10929 | | |
10930 | | static const char *tracefs_path(void) |
10931 | 0 | { |
10932 | 0 | return use_debugfs() ? DEBUGFS : TRACEFS; |
10933 | 0 | } |
10934 | | |
10935 | | static const char *tracefs_kprobe_events(void) |
10936 | 0 | { |
10937 | 0 | return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events"; |
10938 | 0 | } |
10939 | | |
10940 | | static const char *tracefs_uprobe_events(void) |
10941 | 0 | { |
10942 | 0 | return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events"; |
10943 | 0 | } |
10944 | | |
10945 | | static const char *tracefs_available_filter_functions(void) |
10946 | 0 | { |
10947 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions" |
10948 | 0 | : TRACEFS"/available_filter_functions"; |
10949 | 0 | } |
10950 | | |
10951 | | static const char *tracefs_available_filter_functions_addrs(void) |
10952 | 0 | { |
10953 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs" |
10954 | 0 | : TRACEFS"/available_filter_functions_addrs"; |
10955 | 0 | } |
10956 | | |
10957 | | static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz, |
10958 | | const char *kfunc_name, size_t offset) |
10959 | 0 | { |
10960 | 0 | static int index = 0; |
10961 | 0 | int i; |
10962 | |
|
10963 | 0 | snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset, |
10964 | 0 | __sync_fetch_and_add(&index, 1)); |
10965 | | |
10966 | | /* sanitize binary_path in the probe name */ |
10967 | 0 | for (i = 0; buf[i]; i++) { |
10968 | 0 | if (!isalnum(buf[i])) |
10969 | 0 | buf[i] = '_'; |
10970 | 0 | } |
10971 | 0 | } |
10972 | | |
10973 | | static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, |
10974 | | const char *kfunc_name, size_t offset) |
10975 | 0 | { |
10976 | 0 | return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx", |
10977 | 0 | retprobe ? 'r' : 'p', |
10978 | 0 | retprobe ? "kretprobes" : "kprobes", |
10979 | 0 | probe_name, kfunc_name, offset); |
10980 | 0 | } |
10981 | | |
10982 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe) |
10983 | 0 | { |
10984 | 0 | return append_to_file(tracefs_kprobe_events(), "-:%s/%s", |
10985 | 0 | retprobe ? "kretprobes" : "kprobes", probe_name); |
10986 | 0 | } |
10987 | | |
10988 | | static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
10989 | 0 | { |
10990 | 0 | char file[256]; |
10991 | |
|
10992 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
10993 | 0 | tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name); |
10994 | |
|
10995 | 0 | return parse_uint_from_file(file, "%d\n"); |
10996 | 0 | } |
10997 | | |
10998 | | static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, |
10999 | | const char *kfunc_name, size_t offset, int pid) |
11000 | 0 | { |
11001 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11002 | 0 | struct perf_event_attr attr; |
11003 | 0 | char errmsg[STRERR_BUFSIZE]; |
11004 | 0 | int type, pfd, err; |
11005 | |
|
11006 | 0 | err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset); |
11007 | 0 | if (err < 0) { |
11008 | 0 | pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n", |
11009 | 0 | kfunc_name, offset, |
11010 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11011 | 0 | return err; |
11012 | 0 | } |
11013 | 0 | type = determine_kprobe_perf_type_legacy(probe_name, retprobe); |
11014 | 0 | if (type < 0) { |
11015 | 0 | err = type; |
11016 | 0 | pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n", |
11017 | 0 | kfunc_name, offset, |
11018 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11019 | 0 | goto err_clean_legacy; |
11020 | 0 | } |
11021 | | |
11022 | 0 | memset(&attr, 0, attr_sz); |
11023 | 0 | attr.size = attr_sz; |
11024 | 0 | attr.config = type; |
11025 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
11026 | |
|
11027 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
11028 | 0 | pid < 0 ? -1 : pid, /* pid */ |
11029 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
11030 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11031 | 0 | if (pfd < 0) { |
11032 | 0 | err = -errno; |
11033 | 0 | pr_warn("legacy kprobe perf_event_open() failed: %s\n", |
11034 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11035 | 0 | goto err_clean_legacy; |
11036 | 0 | } |
11037 | 0 | return pfd; |
11038 | | |
11039 | 0 | err_clean_legacy: |
11040 | | /* Clear the newly added legacy kprobe_event */ |
11041 | 0 | remove_kprobe_event_legacy(probe_name, retprobe); |
11042 | 0 | return err; |
11043 | 0 | } |
11044 | | |
11045 | | static const char *arch_specific_syscall_pfx(void) |
11046 | 0 | { |
11047 | 0 | #if defined(__x86_64__) |
11048 | 0 | return "x64"; |
11049 | | #elif defined(__i386__) |
11050 | | return "ia32"; |
11051 | | #elif defined(__s390x__) |
11052 | | return "s390x"; |
11053 | | #elif defined(__s390__) |
11054 | | return "s390"; |
11055 | | #elif defined(__arm__) |
11056 | | return "arm"; |
11057 | | #elif defined(__aarch64__) |
11058 | | return "arm64"; |
11059 | | #elif defined(__mips__) |
11060 | | return "mips"; |
11061 | | #elif defined(__riscv) |
11062 | | return "riscv"; |
11063 | | #elif defined(__powerpc__) |
11064 | | return "powerpc"; |
11065 | | #elif defined(__powerpc64__) |
11066 | | return "powerpc64"; |
11067 | | #else |
11068 | | return NULL; |
11069 | | #endif |
11070 | 0 | } |
11071 | | |
11072 | | int probe_kern_syscall_wrapper(int token_fd) |
11073 | 0 | { |
11074 | 0 | char syscall_name[64]; |
11075 | 0 | const char *ksys_pfx; |
11076 | |
|
11077 | 0 | ksys_pfx = arch_specific_syscall_pfx(); |
11078 | 0 | if (!ksys_pfx) |
11079 | 0 | return 0; |
11080 | | |
11081 | 0 | snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx); |
11082 | |
|
11083 | 0 | if (determine_kprobe_perf_type() >= 0) { |
11084 | 0 | int pfd; |
11085 | |
|
11086 | 0 | pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0); |
11087 | 0 | if (pfd >= 0) |
11088 | 0 | close(pfd); |
11089 | |
|
11090 | 0 | return pfd >= 0 ? 1 : 0; |
11091 | 0 | } else { /* legacy mode */ |
11092 | 0 | char probe_name[128]; |
11093 | |
|
11094 | 0 | gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0); |
11095 | 0 | if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0) |
11096 | 0 | return 0; |
11097 | | |
11098 | 0 | (void)remove_kprobe_event_legacy(probe_name, false); |
11099 | 0 | return 1; |
11100 | 0 | } |
11101 | 0 | } |
11102 | | |
11103 | | struct bpf_link * |
11104 | | bpf_program__attach_kprobe_opts(const struct bpf_program *prog, |
11105 | | const char *func_name, |
11106 | | const struct bpf_kprobe_opts *opts) |
11107 | 0 | { |
11108 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
11109 | 0 | enum probe_attach_mode attach_mode; |
11110 | 0 | char errmsg[STRERR_BUFSIZE]; |
11111 | 0 | char *legacy_probe = NULL; |
11112 | 0 | struct bpf_link *link; |
11113 | 0 | size_t offset; |
11114 | 0 | bool retprobe, legacy; |
11115 | 0 | int pfd, err; |
11116 | |
|
11117 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_opts)) |
11118 | 0 | return libbpf_err_ptr(-EINVAL); |
11119 | | |
11120 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
11121 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
11122 | 0 | offset = OPTS_GET(opts, offset, 0); |
11123 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
11124 | |
|
11125 | 0 | legacy = determine_kprobe_perf_type() < 0; |
11126 | 0 | switch (attach_mode) { |
11127 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
11128 | 0 | legacy = true; |
11129 | 0 | pe_opts.force_ioctl_attach = true; |
11130 | 0 | break; |
11131 | 0 | case PROBE_ATTACH_MODE_PERF: |
11132 | 0 | if (legacy) |
11133 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11134 | 0 | pe_opts.force_ioctl_attach = true; |
11135 | 0 | break; |
11136 | 0 | case PROBE_ATTACH_MODE_LINK: |
11137 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
11138 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11139 | 0 | break; |
11140 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
11141 | 0 | break; |
11142 | 0 | default: |
11143 | 0 | return libbpf_err_ptr(-EINVAL); |
11144 | 0 | } |
11145 | | |
11146 | 0 | if (!legacy) { |
11147 | 0 | pfd = perf_event_open_probe(false /* uprobe */, retprobe, |
11148 | 0 | func_name, offset, |
11149 | 0 | -1 /* pid */, 0 /* ref_ctr_off */); |
11150 | 0 | } else { |
11151 | 0 | char probe_name[256]; |
11152 | |
|
11153 | 0 | gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), |
11154 | 0 | func_name, offset); |
11155 | |
|
11156 | 0 | legacy_probe = strdup(probe_name); |
11157 | 0 | if (!legacy_probe) |
11158 | 0 | return libbpf_err_ptr(-ENOMEM); |
11159 | | |
11160 | 0 | pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name, |
11161 | 0 | offset, -1 /* pid */); |
11162 | 0 | } |
11163 | 0 | if (pfd < 0) { |
11164 | 0 | err = -errno; |
11165 | 0 | pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n", |
11166 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
11167 | 0 | func_name, offset, |
11168 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11169 | 0 | goto err_out; |
11170 | 0 | } |
11171 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
11172 | 0 | err = libbpf_get_error(link); |
11173 | 0 | if (err) { |
11174 | 0 | close(pfd); |
11175 | 0 | pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n", |
11176 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
11177 | 0 | func_name, offset, |
11178 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11179 | 0 | goto err_clean_legacy; |
11180 | 0 | } |
11181 | 0 | if (legacy) { |
11182 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
11183 | |
|
11184 | 0 | perf_link->legacy_probe_name = legacy_probe; |
11185 | 0 | perf_link->legacy_is_kprobe = true; |
11186 | 0 | perf_link->legacy_is_retprobe = retprobe; |
11187 | 0 | } |
11188 | |
|
11189 | 0 | return link; |
11190 | | |
11191 | 0 | err_clean_legacy: |
11192 | 0 | if (legacy) |
11193 | 0 | remove_kprobe_event_legacy(legacy_probe, retprobe); |
11194 | 0 | err_out: |
11195 | 0 | free(legacy_probe); |
11196 | 0 | return libbpf_err_ptr(err); |
11197 | 0 | } |
11198 | | |
11199 | | struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, |
11200 | | bool retprobe, |
11201 | | const char *func_name) |
11202 | 0 | { |
11203 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts, |
11204 | 0 | .retprobe = retprobe, |
11205 | 0 | ); |
11206 | |
|
11207 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &opts); |
11208 | 0 | } |
11209 | | |
11210 | | struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog, |
11211 | | const char *syscall_name, |
11212 | | const struct bpf_ksyscall_opts *opts) |
11213 | 0 | { |
11214 | 0 | LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); |
11215 | 0 | char func_name[128]; |
11216 | |
|
11217 | 0 | if (!OPTS_VALID(opts, bpf_ksyscall_opts)) |
11218 | 0 | return libbpf_err_ptr(-EINVAL); |
11219 | | |
11220 | 0 | if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { |
11221 | | /* arch_specific_syscall_pfx() should never return NULL here |
11222 | | * because it is guarded by kernel_supports(). However, since |
11223 | | * compiler does not know that we have an explicit conditional |
11224 | | * as well. |
11225 | | */ |
11226 | 0 | snprintf(func_name, sizeof(func_name), "__%s_sys_%s", |
11227 | 0 | arch_specific_syscall_pfx() ? : "", syscall_name); |
11228 | 0 | } else { |
11229 | 0 | snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name); |
11230 | 0 | } |
11231 | |
|
11232 | 0 | kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false); |
11233 | 0 | kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
11234 | |
|
11235 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts); |
11236 | 0 | } |
11237 | | |
11238 | | /* Adapted from perf/util/string.c */ |
11239 | | bool glob_match(const char *str, const char *pat) |
11240 | 0 | { |
11241 | 0 | while (*str && *pat && *pat != '*') { |
11242 | 0 | if (*pat == '?') { /* Matches any single character */ |
11243 | 0 | str++; |
11244 | 0 | pat++; |
11245 | 0 | continue; |
11246 | 0 | } |
11247 | 0 | if (*str != *pat) |
11248 | 0 | return false; |
11249 | 0 | str++; |
11250 | 0 | pat++; |
11251 | 0 | } |
11252 | | /* Check wild card */ |
11253 | 0 | if (*pat == '*') { |
11254 | 0 | while (*pat == '*') |
11255 | 0 | pat++; |
11256 | 0 | if (!*pat) /* Tail wild card matches all */ |
11257 | 0 | return true; |
11258 | 0 | while (*str) |
11259 | 0 | if (glob_match(str++, pat)) |
11260 | 0 | return true; |
11261 | 0 | } |
11262 | 0 | return !*str && !*pat; |
11263 | 0 | } |
11264 | | |
11265 | | struct kprobe_multi_resolve { |
11266 | | const char *pattern; |
11267 | | unsigned long *addrs; |
11268 | | size_t cap; |
11269 | | size_t cnt; |
11270 | | }; |
11271 | | |
11272 | | struct avail_kallsyms_data { |
11273 | | char **syms; |
11274 | | size_t cnt; |
11275 | | struct kprobe_multi_resolve *res; |
11276 | | }; |
11277 | | |
11278 | | static int avail_func_cmp(const void *a, const void *b) |
11279 | 0 | { |
11280 | 0 | return strcmp(*(const char **)a, *(const char **)b); |
11281 | 0 | } |
11282 | | |
11283 | | static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type, |
11284 | | const char *sym_name, void *ctx) |
11285 | 0 | { |
11286 | 0 | struct avail_kallsyms_data *data = ctx; |
11287 | 0 | struct kprobe_multi_resolve *res = data->res; |
11288 | 0 | int err; |
11289 | |
|
11290 | 0 | if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) |
11291 | 0 | return 0; |
11292 | | |
11293 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); |
11294 | 0 | if (err) |
11295 | 0 | return err; |
11296 | | |
11297 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
11298 | 0 | return 0; |
11299 | 0 | } |
11300 | | |
11301 | | static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res) |
11302 | 0 | { |
11303 | 0 | const char *available_functions_file = tracefs_available_filter_functions(); |
11304 | 0 | struct avail_kallsyms_data data; |
11305 | 0 | char sym_name[500]; |
11306 | 0 | FILE *f; |
11307 | 0 | int err = 0, ret, i; |
11308 | 0 | char **syms = NULL; |
11309 | 0 | size_t cap = 0, cnt = 0; |
11310 | |
|
11311 | 0 | f = fopen(available_functions_file, "re"); |
11312 | 0 | if (!f) { |
11313 | 0 | err = -errno; |
11314 | 0 | pr_warn("failed to open %s: %d\n", available_functions_file, err); |
11315 | 0 | return err; |
11316 | 0 | } |
11317 | | |
11318 | 0 | while (true) { |
11319 | 0 | char *name; |
11320 | |
|
11321 | 0 | ret = fscanf(f, "%499s%*[^\n]\n", sym_name); |
11322 | 0 | if (ret == EOF && feof(f)) |
11323 | 0 | break; |
11324 | | |
11325 | 0 | if (ret != 1) { |
11326 | 0 | pr_warn("failed to parse available_filter_functions entry: %d\n", ret); |
11327 | 0 | err = -EINVAL; |
11328 | 0 | goto cleanup; |
11329 | 0 | } |
11330 | | |
11331 | 0 | if (!glob_match(sym_name, res->pattern)) |
11332 | 0 | continue; |
11333 | | |
11334 | 0 | err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1); |
11335 | 0 | if (err) |
11336 | 0 | goto cleanup; |
11337 | | |
11338 | 0 | name = strdup(sym_name); |
11339 | 0 | if (!name) { |
11340 | 0 | err = -errno; |
11341 | 0 | goto cleanup; |
11342 | 0 | } |
11343 | | |
11344 | 0 | syms[cnt++] = name; |
11345 | 0 | } |
11346 | | |
11347 | | /* no entries found, bail out */ |
11348 | 0 | if (cnt == 0) { |
11349 | 0 | err = -ENOENT; |
11350 | 0 | goto cleanup; |
11351 | 0 | } |
11352 | | |
11353 | | /* sort available functions */ |
11354 | 0 | qsort(syms, cnt, sizeof(*syms), avail_func_cmp); |
11355 | |
|
11356 | 0 | data.syms = syms; |
11357 | 0 | data.res = res; |
11358 | 0 | data.cnt = cnt; |
11359 | 0 | libbpf_kallsyms_parse(avail_kallsyms_cb, &data); |
11360 | |
|
11361 | 0 | if (res->cnt == 0) |
11362 | 0 | err = -ENOENT; |
11363 | |
|
11364 | 0 | cleanup: |
11365 | 0 | for (i = 0; i < cnt; i++) |
11366 | 0 | free((char *)syms[i]); |
11367 | 0 | free(syms); |
11368 | |
|
11369 | 0 | fclose(f); |
11370 | 0 | return err; |
11371 | 0 | } |
11372 | | |
11373 | | static bool has_available_filter_functions_addrs(void) |
11374 | 0 | { |
11375 | 0 | return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; |
11376 | 0 | } |
11377 | | |
11378 | | static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res) |
11379 | 0 | { |
11380 | 0 | const char *available_path = tracefs_available_filter_functions_addrs(); |
11381 | 0 | char sym_name[500]; |
11382 | 0 | FILE *f; |
11383 | 0 | int ret, err = 0; |
11384 | 0 | unsigned long long sym_addr; |
11385 | |
|
11386 | 0 | f = fopen(available_path, "re"); |
11387 | 0 | if (!f) { |
11388 | 0 | err = -errno; |
11389 | 0 | pr_warn("failed to open %s: %d\n", available_path, err); |
11390 | 0 | return err; |
11391 | 0 | } |
11392 | | |
11393 | 0 | while (true) { |
11394 | 0 | ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name); |
11395 | 0 | if (ret == EOF && feof(f)) |
11396 | 0 | break; |
11397 | | |
11398 | 0 | if (ret != 2) { |
11399 | 0 | pr_warn("failed to parse available_filter_functions_addrs entry: %d\n", |
11400 | 0 | ret); |
11401 | 0 | err = -EINVAL; |
11402 | 0 | goto cleanup; |
11403 | 0 | } |
11404 | | |
11405 | 0 | if (!glob_match(sym_name, res->pattern)) |
11406 | 0 | continue; |
11407 | | |
11408 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, |
11409 | 0 | sizeof(*res->addrs), res->cnt + 1); |
11410 | 0 | if (err) |
11411 | 0 | goto cleanup; |
11412 | | |
11413 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
11414 | 0 | } |
11415 | | |
11416 | 0 | if (res->cnt == 0) |
11417 | 0 | err = -ENOENT; |
11418 | |
|
11419 | 0 | cleanup: |
11420 | 0 | fclose(f); |
11421 | 0 | return err; |
11422 | 0 | } |
11423 | | |
11424 | | struct bpf_link * |
11425 | | bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, |
11426 | | const char *pattern, |
11427 | | const struct bpf_kprobe_multi_opts *opts) |
11428 | 0 | { |
11429 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
11430 | 0 | struct kprobe_multi_resolve res = { |
11431 | 0 | .pattern = pattern, |
11432 | 0 | }; |
11433 | 0 | enum bpf_attach_type attach_type; |
11434 | 0 | struct bpf_link *link = NULL; |
11435 | 0 | char errmsg[STRERR_BUFSIZE]; |
11436 | 0 | const unsigned long *addrs; |
11437 | 0 | int err, link_fd, prog_fd; |
11438 | 0 | bool retprobe, session; |
11439 | 0 | const __u64 *cookies; |
11440 | 0 | const char **syms; |
11441 | 0 | size_t cnt; |
11442 | |
|
11443 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_multi_opts)) |
11444 | 0 | return libbpf_err_ptr(-EINVAL); |
11445 | | |
11446 | 0 | prog_fd = bpf_program__fd(prog); |
11447 | 0 | if (prog_fd < 0) { |
11448 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
11449 | 0 | prog->name); |
11450 | 0 | return libbpf_err_ptr(-EINVAL); |
11451 | 0 | } |
11452 | | |
11453 | 0 | syms = OPTS_GET(opts, syms, false); |
11454 | 0 | addrs = OPTS_GET(opts, addrs, false); |
11455 | 0 | cnt = OPTS_GET(opts, cnt, false); |
11456 | 0 | cookies = OPTS_GET(opts, cookies, false); |
11457 | |
|
11458 | 0 | if (!pattern && !addrs && !syms) |
11459 | 0 | return libbpf_err_ptr(-EINVAL); |
11460 | 0 | if (pattern && (addrs || syms || cookies || cnt)) |
11461 | 0 | return libbpf_err_ptr(-EINVAL); |
11462 | 0 | if (!pattern && !cnt) |
11463 | 0 | return libbpf_err_ptr(-EINVAL); |
11464 | 0 | if (addrs && syms) |
11465 | 0 | return libbpf_err_ptr(-EINVAL); |
11466 | | |
11467 | 0 | if (pattern) { |
11468 | 0 | if (has_available_filter_functions_addrs()) |
11469 | 0 | err = libbpf_available_kprobes_parse(&res); |
11470 | 0 | else |
11471 | 0 | err = libbpf_available_kallsyms_parse(&res); |
11472 | 0 | if (err) |
11473 | 0 | goto error; |
11474 | 0 | addrs = res.addrs; |
11475 | 0 | cnt = res.cnt; |
11476 | 0 | } |
11477 | | |
11478 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
11479 | 0 | session = OPTS_GET(opts, session, false); |
11480 | |
|
11481 | 0 | if (retprobe && session) |
11482 | 0 | return libbpf_err_ptr(-EINVAL); |
11483 | | |
11484 | 0 | attach_type = session ? BPF_TRACE_KPROBE_SESSION : BPF_TRACE_KPROBE_MULTI; |
11485 | |
|
11486 | 0 | lopts.kprobe_multi.syms = syms; |
11487 | 0 | lopts.kprobe_multi.addrs = addrs; |
11488 | 0 | lopts.kprobe_multi.cookies = cookies; |
11489 | 0 | lopts.kprobe_multi.cnt = cnt; |
11490 | 0 | lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0; |
11491 | |
|
11492 | 0 | link = calloc(1, sizeof(*link)); |
11493 | 0 | if (!link) { |
11494 | 0 | err = -ENOMEM; |
11495 | 0 | goto error; |
11496 | 0 | } |
11497 | 0 | link->detach = &bpf_link__detach_fd; |
11498 | |
|
11499 | 0 | link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts); |
11500 | 0 | if (link_fd < 0) { |
11501 | 0 | err = -errno; |
11502 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
11503 | 0 | prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11504 | 0 | goto error; |
11505 | 0 | } |
11506 | 0 | link->fd = link_fd; |
11507 | 0 | free(res.addrs); |
11508 | 0 | return link; |
11509 | | |
11510 | 0 | error: |
11511 | 0 | free(link); |
11512 | 0 | free(res.addrs); |
11513 | 0 | return libbpf_err_ptr(err); |
11514 | 0 | } |
11515 | | |
11516 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11517 | 0 | { |
11518 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); |
11519 | 0 | unsigned long offset = 0; |
11520 | 0 | const char *func_name; |
11521 | 0 | char *func; |
11522 | 0 | int n; |
11523 | |
|
11524 | 0 | *link = NULL; |
11525 | | |
11526 | | /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ |
11527 | 0 | if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) |
11528 | 0 | return 0; |
11529 | | |
11530 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); |
11531 | 0 | if (opts.retprobe) |
11532 | 0 | func_name = prog->sec_name + sizeof("kretprobe/") - 1; |
11533 | 0 | else |
11534 | 0 | func_name = prog->sec_name + sizeof("kprobe/") - 1; |
11535 | |
|
11536 | 0 | n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); |
11537 | 0 | if (n < 1) { |
11538 | 0 | pr_warn("kprobe name is invalid: %s\n", func_name); |
11539 | 0 | return -EINVAL; |
11540 | 0 | } |
11541 | 0 | if (opts.retprobe && offset != 0) { |
11542 | 0 | free(func); |
11543 | 0 | pr_warn("kretprobes do not support offset specification\n"); |
11544 | 0 | return -EINVAL; |
11545 | 0 | } |
11546 | | |
11547 | 0 | opts.offset = offset; |
11548 | 0 | *link = bpf_program__attach_kprobe_opts(prog, func, &opts); |
11549 | 0 | free(func); |
11550 | 0 | return libbpf_get_error(*link); |
11551 | 0 | } |
11552 | | |
11553 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11554 | 0 | { |
11555 | 0 | LIBBPF_OPTS(bpf_ksyscall_opts, opts); |
11556 | 0 | const char *syscall_name; |
11557 | |
|
11558 | 0 | *link = NULL; |
11559 | | |
11560 | | /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ |
11561 | 0 | if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) |
11562 | 0 | return 0; |
11563 | | |
11564 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); |
11565 | 0 | if (opts.retprobe) |
11566 | 0 | syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; |
11567 | 0 | else |
11568 | 0 | syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; |
11569 | |
|
11570 | 0 | *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts); |
11571 | 0 | return *link ? 0 : -errno; |
11572 | 0 | } |
11573 | | |
11574 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11575 | 0 | { |
11576 | 0 | LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); |
11577 | 0 | const char *spec; |
11578 | 0 | char *pattern; |
11579 | 0 | int n; |
11580 | |
|
11581 | 0 | *link = NULL; |
11582 | | |
11583 | | /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ |
11584 | 0 | if (strcmp(prog->sec_name, "kprobe.multi") == 0 || |
11585 | 0 | strcmp(prog->sec_name, "kretprobe.multi") == 0) |
11586 | 0 | return 0; |
11587 | | |
11588 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); |
11589 | 0 | if (opts.retprobe) |
11590 | 0 | spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; |
11591 | 0 | else |
11592 | 0 | spec = prog->sec_name + sizeof("kprobe.multi/") - 1; |
11593 | |
|
11594 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); |
11595 | 0 | if (n < 1) { |
11596 | 0 | pr_warn("kprobe multi pattern is invalid: %s\n", spec); |
11597 | 0 | return -EINVAL; |
11598 | 0 | } |
11599 | | |
11600 | 0 | *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts); |
11601 | 0 | free(pattern); |
11602 | 0 | return libbpf_get_error(*link); |
11603 | 0 | } |
11604 | | |
11605 | | static int attach_kprobe_session(const struct bpf_program *prog, long cookie, |
11606 | | struct bpf_link **link) |
11607 | 0 | { |
11608 | 0 | LIBBPF_OPTS(bpf_kprobe_multi_opts, opts, .session = true); |
11609 | 0 | const char *spec; |
11610 | 0 | char *pattern; |
11611 | 0 | int n; |
11612 | |
|
11613 | 0 | *link = NULL; |
11614 | | |
11615 | | /* no auto-attach for SEC("kprobe.session") */ |
11616 | 0 | if (strcmp(prog->sec_name, "kprobe.session") == 0) |
11617 | 0 | return 0; |
11618 | | |
11619 | 0 | spec = prog->sec_name + sizeof("kprobe.session/") - 1; |
11620 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); |
11621 | 0 | if (n < 1) { |
11622 | 0 | pr_warn("kprobe session pattern is invalid: %s\n", spec); |
11623 | 0 | return -EINVAL; |
11624 | 0 | } |
11625 | | |
11626 | 0 | *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts); |
11627 | 0 | free(pattern); |
11628 | 0 | return *link ? 0 : -errno; |
11629 | 0 | } |
11630 | | |
11631 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11632 | 0 | { |
11633 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL; |
11634 | 0 | LIBBPF_OPTS(bpf_uprobe_multi_opts, opts); |
11635 | 0 | int n, ret = -EINVAL; |
11636 | |
|
11637 | 0 | *link = NULL; |
11638 | |
|
11639 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
11640 | 0 | &probe_type, &binary_path, &func_name); |
11641 | 0 | switch (n) { |
11642 | 0 | case 1: |
11643 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
11644 | 0 | ret = 0; |
11645 | 0 | break; |
11646 | 0 | case 3: |
11647 | 0 | opts.retprobe = strcmp(probe_type, "uretprobe.multi") == 0; |
11648 | 0 | *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); |
11649 | 0 | ret = libbpf_get_error(*link); |
11650 | 0 | break; |
11651 | 0 | default: |
11652 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
11653 | 0 | prog->sec_name); |
11654 | 0 | break; |
11655 | 0 | } |
11656 | 0 | free(probe_type); |
11657 | 0 | free(binary_path); |
11658 | 0 | free(func_name); |
11659 | 0 | return ret; |
11660 | 0 | } |
11661 | | |
11662 | | static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz, |
11663 | | const char *binary_path, uint64_t offset) |
11664 | 0 | { |
11665 | 0 | int i; |
11666 | |
|
11667 | 0 | snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset); |
11668 | | |
11669 | | /* sanitize binary_path in the probe name */ |
11670 | 0 | for (i = 0; buf[i]; i++) { |
11671 | 0 | if (!isalnum(buf[i])) |
11672 | 0 | buf[i] = '_'; |
11673 | 0 | } |
11674 | 0 | } |
11675 | | |
11676 | | static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe, |
11677 | | const char *binary_path, size_t offset) |
11678 | 0 | { |
11679 | 0 | return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx", |
11680 | 0 | retprobe ? 'r' : 'p', |
11681 | 0 | retprobe ? "uretprobes" : "uprobes", |
11682 | 0 | probe_name, binary_path, offset); |
11683 | 0 | } |
11684 | | |
11685 | | static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe) |
11686 | 0 | { |
11687 | 0 | return append_to_file(tracefs_uprobe_events(), "-:%s/%s", |
11688 | 0 | retprobe ? "uretprobes" : "uprobes", probe_name); |
11689 | 0 | } |
11690 | | |
11691 | | static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
11692 | 0 | { |
11693 | 0 | char file[512]; |
11694 | |
|
11695 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
11696 | 0 | tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name); |
11697 | |
|
11698 | 0 | return parse_uint_from_file(file, "%d\n"); |
11699 | 0 | } |
11700 | | |
11701 | | static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, |
11702 | | const char *binary_path, size_t offset, int pid) |
11703 | 0 | { |
11704 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11705 | 0 | struct perf_event_attr attr; |
11706 | 0 | int type, pfd, err; |
11707 | |
|
11708 | 0 | err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset); |
11709 | 0 | if (err < 0) { |
11710 | 0 | pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n", |
11711 | 0 | binary_path, (size_t)offset, err); |
11712 | 0 | return err; |
11713 | 0 | } |
11714 | 0 | type = determine_uprobe_perf_type_legacy(probe_name, retprobe); |
11715 | 0 | if (type < 0) { |
11716 | 0 | err = type; |
11717 | 0 | pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n", |
11718 | 0 | binary_path, offset, err); |
11719 | 0 | goto err_clean_legacy; |
11720 | 0 | } |
11721 | | |
11722 | 0 | memset(&attr, 0, attr_sz); |
11723 | 0 | attr.size = attr_sz; |
11724 | 0 | attr.config = type; |
11725 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
11726 | |
|
11727 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
11728 | 0 | pid < 0 ? -1 : pid, /* pid */ |
11729 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
11730 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11731 | 0 | if (pfd < 0) { |
11732 | 0 | err = -errno; |
11733 | 0 | pr_warn("legacy uprobe perf_event_open() failed: %d\n", err); |
11734 | 0 | goto err_clean_legacy; |
11735 | 0 | } |
11736 | 0 | return pfd; |
11737 | | |
11738 | 0 | err_clean_legacy: |
11739 | | /* Clear the newly added legacy uprobe_event */ |
11740 | 0 | remove_uprobe_event_legacy(probe_name, retprobe); |
11741 | 0 | return err; |
11742 | 0 | } |
11743 | | |
11744 | | /* Find offset of function name in archive specified by path. Currently |
11745 | | * supported are .zip files that do not compress their contents, as used on |
11746 | | * Android in the form of APKs, for example. "file_name" is the name of the ELF |
11747 | | * file inside the archive. "func_name" matches symbol name or name@@LIB for |
11748 | | * library functions. |
11749 | | * |
11750 | | * An overview of the APK format specifically provided here: |
11751 | | * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents |
11752 | | */ |
11753 | | static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name, |
11754 | | const char *func_name) |
11755 | 0 | { |
11756 | 0 | struct zip_archive *archive; |
11757 | 0 | struct zip_entry entry; |
11758 | 0 | long ret; |
11759 | 0 | Elf *elf; |
11760 | |
|
11761 | 0 | archive = zip_archive_open(archive_path); |
11762 | 0 | if (IS_ERR(archive)) { |
11763 | 0 | ret = PTR_ERR(archive); |
11764 | 0 | pr_warn("zip: failed to open %s: %ld\n", archive_path, ret); |
11765 | 0 | return ret; |
11766 | 0 | } |
11767 | | |
11768 | 0 | ret = zip_archive_find_entry(archive, file_name, &entry); |
11769 | 0 | if (ret) { |
11770 | 0 | pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name, |
11771 | 0 | archive_path, ret); |
11772 | 0 | goto out; |
11773 | 0 | } |
11774 | 0 | pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path, |
11775 | 0 | (unsigned long)entry.data_offset); |
11776 | |
|
11777 | 0 | if (entry.compression) { |
11778 | 0 | pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name, |
11779 | 0 | archive_path); |
11780 | 0 | ret = -LIBBPF_ERRNO__FORMAT; |
11781 | 0 | goto out; |
11782 | 0 | } |
11783 | | |
11784 | 0 | elf = elf_memory((void *)entry.data, entry.data_length); |
11785 | 0 | if (!elf) { |
11786 | 0 | pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path, |
11787 | 0 | elf_errmsg(-1)); |
11788 | 0 | ret = -LIBBPF_ERRNO__LIBELF; |
11789 | 0 | goto out; |
11790 | 0 | } |
11791 | | |
11792 | 0 | ret = elf_find_func_offset(elf, file_name, func_name); |
11793 | 0 | if (ret > 0) { |
11794 | 0 | pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n", |
11795 | 0 | func_name, file_name, archive_path, entry.data_offset, ret, |
11796 | 0 | ret + entry.data_offset); |
11797 | 0 | ret += entry.data_offset; |
11798 | 0 | } |
11799 | 0 | elf_end(elf); |
11800 | |
|
11801 | 0 | out: |
11802 | 0 | zip_archive_close(archive); |
11803 | 0 | return ret; |
11804 | 0 | } |
11805 | | |
11806 | | static const char *arch_specific_lib_paths(void) |
11807 | 0 | { |
11808 | | /* |
11809 | | * Based on https://packages.debian.org/sid/libc6. |
11810 | | * |
11811 | | * Assume that the traced program is built for the same architecture |
11812 | | * as libbpf, which should cover the vast majority of cases. |
11813 | | */ |
11814 | 0 | #if defined(__x86_64__) |
11815 | 0 | return "/lib/x86_64-linux-gnu"; |
11816 | | #elif defined(__i386__) |
11817 | | return "/lib/i386-linux-gnu"; |
11818 | | #elif defined(__s390x__) |
11819 | | return "/lib/s390x-linux-gnu"; |
11820 | | #elif defined(__s390__) |
11821 | | return "/lib/s390-linux-gnu"; |
11822 | | #elif defined(__arm__) && defined(__SOFTFP__) |
11823 | | return "/lib/arm-linux-gnueabi"; |
11824 | | #elif defined(__arm__) && !defined(__SOFTFP__) |
11825 | | return "/lib/arm-linux-gnueabihf"; |
11826 | | #elif defined(__aarch64__) |
11827 | | return "/lib/aarch64-linux-gnu"; |
11828 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64 |
11829 | | return "/lib/mips64el-linux-gnuabi64"; |
11830 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32 |
11831 | | return "/lib/mipsel-linux-gnu"; |
11832 | | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
11833 | | return "/lib/powerpc64le-linux-gnu"; |
11834 | | #elif defined(__sparc__) && defined(__arch64__) |
11835 | | return "/lib/sparc64-linux-gnu"; |
11836 | | #elif defined(__riscv) && __riscv_xlen == 64 |
11837 | | return "/lib/riscv64-linux-gnu"; |
11838 | | #else |
11839 | | return NULL; |
11840 | | #endif |
11841 | 0 | } |
11842 | | |
11843 | | /* Get full path to program/shared library. */ |
11844 | | static int resolve_full_path(const char *file, char *result, size_t result_sz) |
11845 | 0 | { |
11846 | 0 | const char *search_paths[3] = {}; |
11847 | 0 | int i, perm; |
11848 | |
|
11849 | 0 | if (str_has_sfx(file, ".so") || strstr(file, ".so.")) { |
11850 | 0 | search_paths[0] = getenv("LD_LIBRARY_PATH"); |
11851 | 0 | search_paths[1] = "/usr/lib64:/usr/lib"; |
11852 | 0 | search_paths[2] = arch_specific_lib_paths(); |
11853 | 0 | perm = R_OK; |
11854 | 0 | } else { |
11855 | 0 | search_paths[0] = getenv("PATH"); |
11856 | 0 | search_paths[1] = "/usr/bin:/usr/sbin"; |
11857 | 0 | perm = R_OK | X_OK; |
11858 | 0 | } |
11859 | |
|
11860 | 0 | for (i = 0; i < ARRAY_SIZE(search_paths); i++) { |
11861 | 0 | const char *s; |
11862 | |
|
11863 | 0 | if (!search_paths[i]) |
11864 | 0 | continue; |
11865 | 0 | for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) { |
11866 | 0 | char *next_path; |
11867 | 0 | int seg_len; |
11868 | |
|
11869 | 0 | if (s[0] == ':') |
11870 | 0 | s++; |
11871 | 0 | next_path = strchr(s, ':'); |
11872 | 0 | seg_len = next_path ? next_path - s : strlen(s); |
11873 | 0 | if (!seg_len) |
11874 | 0 | continue; |
11875 | 0 | snprintf(result, result_sz, "%.*s/%s", seg_len, s, file); |
11876 | | /* ensure it has required permissions */ |
11877 | 0 | if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0) |
11878 | 0 | continue; |
11879 | 0 | pr_debug("resolved '%s' to '%s'\n", file, result); |
11880 | 0 | return 0; |
11881 | 0 | } |
11882 | 0 | } |
11883 | 0 | return -ENOENT; |
11884 | 0 | } |
11885 | | |
11886 | | struct bpf_link * |
11887 | | bpf_program__attach_uprobe_multi(const struct bpf_program *prog, |
11888 | | pid_t pid, |
11889 | | const char *path, |
11890 | | const char *func_pattern, |
11891 | | const struct bpf_uprobe_multi_opts *opts) |
11892 | 0 | { |
11893 | 0 | const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL; |
11894 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
11895 | 0 | unsigned long *resolved_offsets = NULL; |
11896 | 0 | int err = 0, link_fd, prog_fd; |
11897 | 0 | struct bpf_link *link = NULL; |
11898 | 0 | char errmsg[STRERR_BUFSIZE]; |
11899 | 0 | char full_path[PATH_MAX]; |
11900 | 0 | const __u64 *cookies; |
11901 | 0 | const char **syms; |
11902 | 0 | size_t cnt; |
11903 | |
|
11904 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_multi_opts)) |
11905 | 0 | return libbpf_err_ptr(-EINVAL); |
11906 | | |
11907 | 0 | prog_fd = bpf_program__fd(prog); |
11908 | 0 | if (prog_fd < 0) { |
11909 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
11910 | 0 | prog->name); |
11911 | 0 | return libbpf_err_ptr(-EINVAL); |
11912 | 0 | } |
11913 | | |
11914 | 0 | syms = OPTS_GET(opts, syms, NULL); |
11915 | 0 | offsets = OPTS_GET(opts, offsets, NULL); |
11916 | 0 | ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL); |
11917 | 0 | cookies = OPTS_GET(opts, cookies, NULL); |
11918 | 0 | cnt = OPTS_GET(opts, cnt, 0); |
11919 | | |
11920 | | /* |
11921 | | * User can specify 2 mutually exclusive set of inputs: |
11922 | | * |
11923 | | * 1) use only path/func_pattern/pid arguments |
11924 | | * |
11925 | | * 2) use path/pid with allowed combinations of: |
11926 | | * syms/offsets/ref_ctr_offsets/cookies/cnt |
11927 | | * |
11928 | | * - syms and offsets are mutually exclusive |
11929 | | * - ref_ctr_offsets and cookies are optional |
11930 | | * |
11931 | | * Any other usage results in error. |
11932 | | */ |
11933 | |
|
11934 | 0 | if (!path) |
11935 | 0 | return libbpf_err_ptr(-EINVAL); |
11936 | 0 | if (!func_pattern && cnt == 0) |
11937 | 0 | return libbpf_err_ptr(-EINVAL); |
11938 | | |
11939 | 0 | if (func_pattern) { |
11940 | 0 | if (syms || offsets || ref_ctr_offsets || cookies || cnt) |
11941 | 0 | return libbpf_err_ptr(-EINVAL); |
11942 | 0 | } else { |
11943 | 0 | if (!!syms == !!offsets) |
11944 | 0 | return libbpf_err_ptr(-EINVAL); |
11945 | 0 | } |
11946 | | |
11947 | 0 | if (func_pattern) { |
11948 | 0 | if (!strchr(path, '/')) { |
11949 | 0 | err = resolve_full_path(path, full_path, sizeof(full_path)); |
11950 | 0 | if (err) { |
11951 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
11952 | 0 | prog->name, path, err); |
11953 | 0 | return libbpf_err_ptr(err); |
11954 | 0 | } |
11955 | 0 | path = full_path; |
11956 | 0 | } |
11957 | | |
11958 | 0 | err = elf_resolve_pattern_offsets(path, func_pattern, |
11959 | 0 | &resolved_offsets, &cnt); |
11960 | 0 | if (err < 0) |
11961 | 0 | return libbpf_err_ptr(err); |
11962 | 0 | offsets = resolved_offsets; |
11963 | 0 | } else if (syms) { |
11964 | 0 | err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC); |
11965 | 0 | if (err < 0) |
11966 | 0 | return libbpf_err_ptr(err); |
11967 | 0 | offsets = resolved_offsets; |
11968 | 0 | } |
11969 | | |
11970 | 0 | lopts.uprobe_multi.path = path; |
11971 | 0 | lopts.uprobe_multi.offsets = offsets; |
11972 | 0 | lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets; |
11973 | 0 | lopts.uprobe_multi.cookies = cookies; |
11974 | 0 | lopts.uprobe_multi.cnt = cnt; |
11975 | 0 | lopts.uprobe_multi.flags = OPTS_GET(opts, retprobe, false) ? BPF_F_UPROBE_MULTI_RETURN : 0; |
11976 | |
|
11977 | 0 | if (pid == 0) |
11978 | 0 | pid = getpid(); |
11979 | 0 | if (pid > 0) |
11980 | 0 | lopts.uprobe_multi.pid = pid; |
11981 | |
|
11982 | 0 | link = calloc(1, sizeof(*link)); |
11983 | 0 | if (!link) { |
11984 | 0 | err = -ENOMEM; |
11985 | 0 | goto error; |
11986 | 0 | } |
11987 | 0 | link->detach = &bpf_link__detach_fd; |
11988 | |
|
11989 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &lopts); |
11990 | 0 | if (link_fd < 0) { |
11991 | 0 | err = -errno; |
11992 | 0 | pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", |
11993 | 0 | prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11994 | 0 | goto error; |
11995 | 0 | } |
11996 | 0 | link->fd = link_fd; |
11997 | 0 | free(resolved_offsets); |
11998 | 0 | return link; |
11999 | | |
12000 | 0 | error: |
12001 | 0 | free(resolved_offsets); |
12002 | 0 | free(link); |
12003 | 0 | return libbpf_err_ptr(err); |
12004 | 0 | } |
12005 | | |
12006 | | LIBBPF_API struct bpf_link * |
12007 | | bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, |
12008 | | const char *binary_path, size_t func_offset, |
12009 | | const struct bpf_uprobe_opts *opts) |
12010 | 0 | { |
12011 | 0 | const char *archive_path = NULL, *archive_sep = NULL; |
12012 | 0 | char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL; |
12013 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
12014 | 0 | enum probe_attach_mode attach_mode; |
12015 | 0 | char full_path[PATH_MAX]; |
12016 | 0 | struct bpf_link *link; |
12017 | 0 | size_t ref_ctr_off; |
12018 | 0 | int pfd, err; |
12019 | 0 | bool retprobe, legacy; |
12020 | 0 | const char *func_name; |
12021 | |
|
12022 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
12023 | 0 | return libbpf_err_ptr(-EINVAL); |
12024 | | |
12025 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
12026 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
12027 | 0 | ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0); |
12028 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
12029 | |
|
12030 | 0 | if (!binary_path) |
12031 | 0 | return libbpf_err_ptr(-EINVAL); |
12032 | | |
12033 | | /* Check if "binary_path" refers to an archive. */ |
12034 | 0 | archive_sep = strstr(binary_path, "!/"); |
12035 | 0 | if (archive_sep) { |
12036 | 0 | full_path[0] = '\0'; |
12037 | 0 | libbpf_strlcpy(full_path, binary_path, |
12038 | 0 | min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); |
12039 | 0 | archive_path = full_path; |
12040 | 0 | binary_path = archive_sep + 2; |
12041 | 0 | } else if (!strchr(binary_path, '/')) { |
12042 | 0 | err = resolve_full_path(binary_path, full_path, sizeof(full_path)); |
12043 | 0 | if (err) { |
12044 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
12045 | 0 | prog->name, binary_path, err); |
12046 | 0 | return libbpf_err_ptr(err); |
12047 | 0 | } |
12048 | 0 | binary_path = full_path; |
12049 | 0 | } |
12050 | 0 | func_name = OPTS_GET(opts, func_name, NULL); |
12051 | 0 | if (func_name) { |
12052 | 0 | long sym_off; |
12053 | |
|
12054 | 0 | if (archive_path) { |
12055 | 0 | sym_off = elf_find_func_offset_from_archive(archive_path, binary_path, |
12056 | 0 | func_name); |
12057 | 0 | binary_path = archive_path; |
12058 | 0 | } else { |
12059 | 0 | sym_off = elf_find_func_offset_from_file(binary_path, func_name); |
12060 | 0 | } |
12061 | 0 | if (sym_off < 0) |
12062 | 0 | return libbpf_err_ptr(sym_off); |
12063 | 0 | func_offset += sym_off; |
12064 | 0 | } |
12065 | | |
12066 | 0 | legacy = determine_uprobe_perf_type() < 0; |
12067 | 0 | switch (attach_mode) { |
12068 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
12069 | 0 | legacy = true; |
12070 | 0 | pe_opts.force_ioctl_attach = true; |
12071 | 0 | break; |
12072 | 0 | case PROBE_ATTACH_MODE_PERF: |
12073 | 0 | if (legacy) |
12074 | 0 | return libbpf_err_ptr(-ENOTSUP); |
12075 | 0 | pe_opts.force_ioctl_attach = true; |
12076 | 0 | break; |
12077 | 0 | case PROBE_ATTACH_MODE_LINK: |
12078 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
12079 | 0 | return libbpf_err_ptr(-ENOTSUP); |
12080 | 0 | break; |
12081 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
12082 | 0 | break; |
12083 | 0 | default: |
12084 | 0 | return libbpf_err_ptr(-EINVAL); |
12085 | 0 | } |
12086 | | |
12087 | 0 | if (!legacy) { |
12088 | 0 | pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path, |
12089 | 0 | func_offset, pid, ref_ctr_off); |
12090 | 0 | } else { |
12091 | 0 | char probe_name[PATH_MAX + 64]; |
12092 | |
|
12093 | 0 | if (ref_ctr_off) |
12094 | 0 | return libbpf_err_ptr(-EINVAL); |
12095 | | |
12096 | 0 | gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name), |
12097 | 0 | binary_path, func_offset); |
12098 | |
|
12099 | 0 | legacy_probe = strdup(probe_name); |
12100 | 0 | if (!legacy_probe) |
12101 | 0 | return libbpf_err_ptr(-ENOMEM); |
12102 | | |
12103 | 0 | pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe, |
12104 | 0 | binary_path, func_offset, pid); |
12105 | 0 | } |
12106 | 0 | if (pfd < 0) { |
12107 | 0 | err = -errno; |
12108 | 0 | pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n", |
12109 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
12110 | 0 | binary_path, func_offset, |
12111 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
12112 | 0 | goto err_out; |
12113 | 0 | } |
12114 | | |
12115 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
12116 | 0 | err = libbpf_get_error(link); |
12117 | 0 | if (err) { |
12118 | 0 | close(pfd); |
12119 | 0 | pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n", |
12120 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
12121 | 0 | binary_path, func_offset, |
12122 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
12123 | 0 | goto err_clean_legacy; |
12124 | 0 | } |
12125 | 0 | if (legacy) { |
12126 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
12127 | |
|
12128 | 0 | perf_link->legacy_probe_name = legacy_probe; |
12129 | 0 | perf_link->legacy_is_kprobe = false; |
12130 | 0 | perf_link->legacy_is_retprobe = retprobe; |
12131 | 0 | } |
12132 | 0 | return link; |
12133 | | |
12134 | 0 | err_clean_legacy: |
12135 | 0 | if (legacy) |
12136 | 0 | remove_uprobe_event_legacy(legacy_probe, retprobe); |
12137 | 0 | err_out: |
12138 | 0 | free(legacy_probe); |
12139 | 0 | return libbpf_err_ptr(err); |
12140 | 0 | } |
12141 | | |
12142 | | /* Format of u[ret]probe section definition supporting auto-attach: |
12143 | | * u[ret]probe/binary:function[+offset] |
12144 | | * |
12145 | | * binary can be an absolute/relative path or a filename; the latter is resolved to a |
12146 | | * full binary path via bpf_program__attach_uprobe_opts. |
12147 | | * |
12148 | | * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be |
12149 | | * specified (and auto-attach is not possible) or the above format is specified for |
12150 | | * auto-attach. |
12151 | | */ |
12152 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12153 | 0 | { |
12154 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); |
12155 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off; |
12156 | 0 | int n, c, ret = -EINVAL; |
12157 | 0 | long offset = 0; |
12158 | |
|
12159 | 0 | *link = NULL; |
12160 | |
|
12161 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
12162 | 0 | &probe_type, &binary_path, &func_name); |
12163 | 0 | switch (n) { |
12164 | 0 | case 1: |
12165 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
12166 | 0 | ret = 0; |
12167 | 0 | break; |
12168 | 0 | case 2: |
12169 | 0 | pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n", |
12170 | 0 | prog->name, prog->sec_name); |
12171 | 0 | break; |
12172 | 0 | case 3: |
12173 | | /* check if user specifies `+offset`, if yes, this should be |
12174 | | * the last part of the string, make sure sscanf read to EOL |
12175 | | */ |
12176 | 0 | func_off = strrchr(func_name, '+'); |
12177 | 0 | if (func_off) { |
12178 | 0 | n = sscanf(func_off, "+%li%n", &offset, &c); |
12179 | 0 | if (n == 1 && *(func_off + c) == '\0') |
12180 | 0 | func_off[0] = '\0'; |
12181 | 0 | else |
12182 | 0 | offset = 0; |
12183 | 0 | } |
12184 | 0 | opts.retprobe = strcmp(probe_type, "uretprobe") == 0 || |
12185 | 0 | strcmp(probe_type, "uretprobe.s") == 0; |
12186 | 0 | if (opts.retprobe && offset != 0) { |
12187 | 0 | pr_warn("prog '%s': uretprobes do not support offset specification\n", |
12188 | 0 | prog->name); |
12189 | 0 | break; |
12190 | 0 | } |
12191 | 0 | opts.func_name = func_name; |
12192 | 0 | *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); |
12193 | 0 | ret = libbpf_get_error(*link); |
12194 | 0 | break; |
12195 | 0 | default: |
12196 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
12197 | 0 | prog->sec_name); |
12198 | 0 | break; |
12199 | 0 | } |
12200 | 0 | free(probe_type); |
12201 | 0 | free(binary_path); |
12202 | 0 | free(func_name); |
12203 | |
|
12204 | 0 | return ret; |
12205 | 0 | } |
12206 | | |
12207 | | struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, |
12208 | | bool retprobe, pid_t pid, |
12209 | | const char *binary_path, |
12210 | | size_t func_offset) |
12211 | 0 | { |
12212 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe); |
12213 | |
|
12214 | 0 | return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts); |
12215 | 0 | } |
12216 | | |
12217 | | struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog, |
12218 | | pid_t pid, const char *binary_path, |
12219 | | const char *usdt_provider, const char *usdt_name, |
12220 | | const struct bpf_usdt_opts *opts) |
12221 | 0 | { |
12222 | 0 | char resolved_path[512]; |
12223 | 0 | struct bpf_object *obj = prog->obj; |
12224 | 0 | struct bpf_link *link; |
12225 | 0 | __u64 usdt_cookie; |
12226 | 0 | int err; |
12227 | |
|
12228 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
12229 | 0 | return libbpf_err_ptr(-EINVAL); |
12230 | | |
12231 | 0 | if (bpf_program__fd(prog) < 0) { |
12232 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
12233 | 0 | prog->name); |
12234 | 0 | return libbpf_err_ptr(-EINVAL); |
12235 | 0 | } |
12236 | | |
12237 | 0 | if (!binary_path) |
12238 | 0 | return libbpf_err_ptr(-EINVAL); |
12239 | | |
12240 | 0 | if (!strchr(binary_path, '/')) { |
12241 | 0 | err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path)); |
12242 | 0 | if (err) { |
12243 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
12244 | 0 | prog->name, binary_path, err); |
12245 | 0 | return libbpf_err_ptr(err); |
12246 | 0 | } |
12247 | 0 | binary_path = resolved_path; |
12248 | 0 | } |
12249 | | |
12250 | | /* USDT manager is instantiated lazily on first USDT attach. It will |
12251 | | * be destroyed together with BPF object in bpf_object__close(). |
12252 | | */ |
12253 | 0 | if (IS_ERR(obj->usdt_man)) |
12254 | 0 | return libbpf_ptr(obj->usdt_man); |
12255 | 0 | if (!obj->usdt_man) { |
12256 | 0 | obj->usdt_man = usdt_manager_new(obj); |
12257 | 0 | if (IS_ERR(obj->usdt_man)) |
12258 | 0 | return libbpf_ptr(obj->usdt_man); |
12259 | 0 | } |
12260 | | |
12261 | 0 | usdt_cookie = OPTS_GET(opts, usdt_cookie, 0); |
12262 | 0 | link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, |
12263 | 0 | usdt_provider, usdt_name, usdt_cookie); |
12264 | 0 | err = libbpf_get_error(link); |
12265 | 0 | if (err) |
12266 | 0 | return libbpf_err_ptr(err); |
12267 | 0 | return link; |
12268 | 0 | } |
12269 | | |
12270 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12271 | 0 | { |
12272 | 0 | char *path = NULL, *provider = NULL, *name = NULL; |
12273 | 0 | const char *sec_name; |
12274 | 0 | int n, err; |
12275 | |
|
12276 | 0 | sec_name = bpf_program__section_name(prog); |
12277 | 0 | if (strcmp(sec_name, "usdt") == 0) { |
12278 | | /* no auto-attach for just SEC("usdt") */ |
12279 | 0 | *link = NULL; |
12280 | 0 | return 0; |
12281 | 0 | } |
12282 | | |
12283 | 0 | n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name); |
12284 | 0 | if (n != 3) { |
12285 | 0 | pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n", |
12286 | 0 | sec_name); |
12287 | 0 | err = -EINVAL; |
12288 | 0 | } else { |
12289 | 0 | *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, |
12290 | 0 | provider, name, NULL); |
12291 | 0 | err = libbpf_get_error(*link); |
12292 | 0 | } |
12293 | 0 | free(path); |
12294 | 0 | free(provider); |
12295 | 0 | free(name); |
12296 | 0 | return err; |
12297 | 0 | } |
12298 | | |
12299 | | static int determine_tracepoint_id(const char *tp_category, |
12300 | | const char *tp_name) |
12301 | 0 | { |
12302 | 0 | char file[PATH_MAX]; |
12303 | 0 | int ret; |
12304 | |
|
12305 | 0 | ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
12306 | 0 | tracefs_path(), tp_category, tp_name); |
12307 | 0 | if (ret < 0) |
12308 | 0 | return -errno; |
12309 | 0 | if (ret >= sizeof(file)) { |
12310 | 0 | pr_debug("tracepoint %s/%s path is too long\n", |
12311 | 0 | tp_category, tp_name); |
12312 | 0 | return -E2BIG; |
12313 | 0 | } |
12314 | 0 | return parse_uint_from_file(file, "%d\n"); |
12315 | 0 | } |
12316 | | |
12317 | | static int perf_event_open_tracepoint(const char *tp_category, |
12318 | | const char *tp_name) |
12319 | 0 | { |
12320 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
12321 | 0 | struct perf_event_attr attr; |
12322 | 0 | char errmsg[STRERR_BUFSIZE]; |
12323 | 0 | int tp_id, pfd, err; |
12324 | |
|
12325 | 0 | tp_id = determine_tracepoint_id(tp_category, tp_name); |
12326 | 0 | if (tp_id < 0) { |
12327 | 0 | pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n", |
12328 | 0 | tp_category, tp_name, |
12329 | 0 | libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg))); |
12330 | 0 | return tp_id; |
12331 | 0 | } |
12332 | | |
12333 | 0 | memset(&attr, 0, attr_sz); |
12334 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
12335 | 0 | attr.size = attr_sz; |
12336 | 0 | attr.config = tp_id; |
12337 | |
|
12338 | 0 | pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, |
12339 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
12340 | 0 | if (pfd < 0) { |
12341 | 0 | err = -errno; |
12342 | 0 | pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n", |
12343 | 0 | tp_category, tp_name, |
12344 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
12345 | 0 | return err; |
12346 | 0 | } |
12347 | 0 | return pfd; |
12348 | 0 | } |
12349 | | |
12350 | | struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, |
12351 | | const char *tp_category, |
12352 | | const char *tp_name, |
12353 | | const struct bpf_tracepoint_opts *opts) |
12354 | 0 | { |
12355 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
12356 | 0 | char errmsg[STRERR_BUFSIZE]; |
12357 | 0 | struct bpf_link *link; |
12358 | 0 | int pfd, err; |
12359 | |
|
12360 | 0 | if (!OPTS_VALID(opts, bpf_tracepoint_opts)) |
12361 | 0 | return libbpf_err_ptr(-EINVAL); |
12362 | | |
12363 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
12364 | |
|
12365 | 0 | pfd = perf_event_open_tracepoint(tp_category, tp_name); |
12366 | 0 | if (pfd < 0) { |
12367 | 0 | pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n", |
12368 | 0 | prog->name, tp_category, tp_name, |
12369 | 0 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
12370 | 0 | return libbpf_err_ptr(pfd); |
12371 | 0 | } |
12372 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
12373 | 0 | err = libbpf_get_error(link); |
12374 | 0 | if (err) { |
12375 | 0 | close(pfd); |
12376 | 0 | pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n", |
12377 | 0 | prog->name, tp_category, tp_name, |
12378 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
12379 | 0 | return libbpf_err_ptr(err); |
12380 | 0 | } |
12381 | 0 | return link; |
12382 | 0 | } |
12383 | | |
12384 | | struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog, |
12385 | | const char *tp_category, |
12386 | | const char *tp_name) |
12387 | 0 | { |
12388 | 0 | return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL); |
12389 | 0 | } |
12390 | | |
12391 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12392 | 0 | { |
12393 | 0 | char *sec_name, *tp_cat, *tp_name; |
12394 | |
|
12395 | 0 | *link = NULL; |
12396 | | |
12397 | | /* no auto-attach for SEC("tp") or SEC("tracepoint") */ |
12398 | 0 | if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0) |
12399 | 0 | return 0; |
12400 | | |
12401 | 0 | sec_name = strdup(prog->sec_name); |
12402 | 0 | if (!sec_name) |
12403 | 0 | return -ENOMEM; |
12404 | | |
12405 | | /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */ |
12406 | 0 | if (str_has_pfx(prog->sec_name, "tp/")) |
12407 | 0 | tp_cat = sec_name + sizeof("tp/") - 1; |
12408 | 0 | else |
12409 | 0 | tp_cat = sec_name + sizeof("tracepoint/") - 1; |
12410 | 0 | tp_name = strchr(tp_cat, '/'); |
12411 | 0 | if (!tp_name) { |
12412 | 0 | free(sec_name); |
12413 | 0 | return -EINVAL; |
12414 | 0 | } |
12415 | 0 | *tp_name = '\0'; |
12416 | 0 | tp_name++; |
12417 | |
|
12418 | 0 | *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name); |
12419 | 0 | free(sec_name); |
12420 | 0 | return libbpf_get_error(*link); |
12421 | 0 | } |
12422 | | |
12423 | | struct bpf_link * |
12424 | | bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, |
12425 | | const char *tp_name, |
12426 | | struct bpf_raw_tracepoint_opts *opts) |
12427 | 0 | { |
12428 | 0 | LIBBPF_OPTS(bpf_raw_tp_opts, raw_opts); |
12429 | 0 | char errmsg[STRERR_BUFSIZE]; |
12430 | 0 | struct bpf_link *link; |
12431 | 0 | int prog_fd, pfd; |
12432 | |
|
12433 | 0 | if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts)) |
12434 | 0 | return libbpf_err_ptr(-EINVAL); |
12435 | | |
12436 | 0 | prog_fd = bpf_program__fd(prog); |
12437 | 0 | if (prog_fd < 0) { |
12438 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12439 | 0 | return libbpf_err_ptr(-EINVAL); |
12440 | 0 | } |
12441 | | |
12442 | 0 | link = calloc(1, sizeof(*link)); |
12443 | 0 | if (!link) |
12444 | 0 | return libbpf_err_ptr(-ENOMEM); |
12445 | 0 | link->detach = &bpf_link__detach_fd; |
12446 | |
|
12447 | 0 | raw_opts.tp_name = tp_name; |
12448 | 0 | raw_opts.cookie = OPTS_GET(opts, cookie, 0); |
12449 | 0 | pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts); |
12450 | 0 | if (pfd < 0) { |
12451 | 0 | pfd = -errno; |
12452 | 0 | free(link); |
12453 | 0 | pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n", |
12454 | 0 | prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
12455 | 0 | return libbpf_err_ptr(pfd); |
12456 | 0 | } |
12457 | 0 | link->fd = pfd; |
12458 | 0 | return link; |
12459 | 0 | } |
12460 | | |
12461 | | struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, |
12462 | | const char *tp_name) |
12463 | 0 | { |
12464 | 0 | return bpf_program__attach_raw_tracepoint_opts(prog, tp_name, NULL); |
12465 | 0 | } |
12466 | | |
12467 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12468 | 0 | { |
12469 | 0 | static const char *const prefixes[] = { |
12470 | 0 | "raw_tp", |
12471 | 0 | "raw_tracepoint", |
12472 | 0 | "raw_tp.w", |
12473 | 0 | "raw_tracepoint.w", |
12474 | 0 | }; |
12475 | 0 | size_t i; |
12476 | 0 | const char *tp_name = NULL; |
12477 | |
|
12478 | 0 | *link = NULL; |
12479 | |
|
12480 | 0 | for (i = 0; i < ARRAY_SIZE(prefixes); i++) { |
12481 | 0 | size_t pfx_len; |
12482 | |
|
12483 | 0 | if (!str_has_pfx(prog->sec_name, prefixes[i])) |
12484 | 0 | continue; |
12485 | | |
12486 | 0 | pfx_len = strlen(prefixes[i]); |
12487 | | /* no auto-attach case of, e.g., SEC("raw_tp") */ |
12488 | 0 | if (prog->sec_name[pfx_len] == '\0') |
12489 | 0 | return 0; |
12490 | | |
12491 | 0 | if (prog->sec_name[pfx_len] != '/') |
12492 | 0 | continue; |
12493 | | |
12494 | 0 | tp_name = prog->sec_name + pfx_len + 1; |
12495 | 0 | break; |
12496 | 0 | } |
12497 | | |
12498 | 0 | if (!tp_name) { |
12499 | 0 | pr_warn("prog '%s': invalid section name '%s'\n", |
12500 | 0 | prog->name, prog->sec_name); |
12501 | 0 | return -EINVAL; |
12502 | 0 | } |
12503 | | |
12504 | 0 | *link = bpf_program__attach_raw_tracepoint(prog, tp_name); |
12505 | 0 | return libbpf_get_error(*link); |
12506 | 0 | } |
12507 | | |
12508 | | /* Common logic for all BPF program types that attach to a btf_id */ |
12509 | | static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog, |
12510 | | const struct bpf_trace_opts *opts) |
12511 | 0 | { |
12512 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_opts); |
12513 | 0 | char errmsg[STRERR_BUFSIZE]; |
12514 | 0 | struct bpf_link *link; |
12515 | 0 | int prog_fd, pfd; |
12516 | |
|
12517 | 0 | if (!OPTS_VALID(opts, bpf_trace_opts)) |
12518 | 0 | return libbpf_err_ptr(-EINVAL); |
12519 | | |
12520 | 0 | prog_fd = bpf_program__fd(prog); |
12521 | 0 | if (prog_fd < 0) { |
12522 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12523 | 0 | return libbpf_err_ptr(-EINVAL); |
12524 | 0 | } |
12525 | | |
12526 | 0 | link = calloc(1, sizeof(*link)); |
12527 | 0 | if (!link) |
12528 | 0 | return libbpf_err_ptr(-ENOMEM); |
12529 | 0 | link->detach = &bpf_link__detach_fd; |
12530 | | |
12531 | | /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */ |
12532 | 0 | link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0); |
12533 | 0 | pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts); |
12534 | 0 | if (pfd < 0) { |
12535 | 0 | pfd = -errno; |
12536 | 0 | free(link); |
12537 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
12538 | 0 | prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
12539 | 0 | return libbpf_err_ptr(pfd); |
12540 | 0 | } |
12541 | 0 | link->fd = pfd; |
12542 | 0 | return link; |
12543 | 0 | } |
12544 | | |
12545 | | struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog) |
12546 | 0 | { |
12547 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
12548 | 0 | } |
12549 | | |
12550 | | struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog, |
12551 | | const struct bpf_trace_opts *opts) |
12552 | 0 | { |
12553 | 0 | return bpf_program__attach_btf_id(prog, opts); |
12554 | 0 | } |
12555 | | |
12556 | | struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog) |
12557 | 0 | { |
12558 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
12559 | 0 | } |
12560 | | |
12561 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12562 | 0 | { |
12563 | 0 | *link = bpf_program__attach_trace(prog); |
12564 | 0 | return libbpf_get_error(*link); |
12565 | 0 | } |
12566 | | |
12567 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12568 | 0 | { |
12569 | 0 | *link = bpf_program__attach_lsm(prog); |
12570 | 0 | return libbpf_get_error(*link); |
12571 | 0 | } |
12572 | | |
12573 | | static struct bpf_link * |
12574 | | bpf_program_attach_fd(const struct bpf_program *prog, |
12575 | | int target_fd, const char *target_name, |
12576 | | const struct bpf_link_create_opts *opts) |
12577 | 0 | { |
12578 | 0 | enum bpf_attach_type attach_type; |
12579 | 0 | char errmsg[STRERR_BUFSIZE]; |
12580 | 0 | struct bpf_link *link; |
12581 | 0 | int prog_fd, link_fd; |
12582 | |
|
12583 | 0 | prog_fd = bpf_program__fd(prog); |
12584 | 0 | if (prog_fd < 0) { |
12585 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12586 | 0 | return libbpf_err_ptr(-EINVAL); |
12587 | 0 | } |
12588 | | |
12589 | 0 | link = calloc(1, sizeof(*link)); |
12590 | 0 | if (!link) |
12591 | 0 | return libbpf_err_ptr(-ENOMEM); |
12592 | 0 | link->detach = &bpf_link__detach_fd; |
12593 | |
|
12594 | 0 | attach_type = bpf_program__expected_attach_type(prog); |
12595 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts); |
12596 | 0 | if (link_fd < 0) { |
12597 | 0 | link_fd = -errno; |
12598 | 0 | free(link); |
12599 | 0 | pr_warn("prog '%s': failed to attach to %s: %s\n", |
12600 | 0 | prog->name, target_name, |
12601 | 0 | libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12602 | 0 | return libbpf_err_ptr(link_fd); |
12603 | 0 | } |
12604 | 0 | link->fd = link_fd; |
12605 | 0 | return link; |
12606 | 0 | } |
12607 | | |
12608 | | struct bpf_link * |
12609 | | bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd) |
12610 | 0 | { |
12611 | 0 | return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL); |
12612 | 0 | } |
12613 | | |
12614 | | struct bpf_link * |
12615 | | bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd) |
12616 | 0 | { |
12617 | 0 | return bpf_program_attach_fd(prog, netns_fd, "netns", NULL); |
12618 | 0 | } |
12619 | | |
12620 | | struct bpf_link * |
12621 | | bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd) |
12622 | 0 | { |
12623 | 0 | return bpf_program_attach_fd(prog, map_fd, "sockmap", NULL); |
12624 | 0 | } |
12625 | | |
12626 | | struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex) |
12627 | 0 | { |
12628 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
12629 | 0 | return bpf_program_attach_fd(prog, ifindex, "xdp", NULL); |
12630 | 0 | } |
12631 | | |
12632 | | struct bpf_link * |
12633 | | bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, |
12634 | | const struct bpf_tcx_opts *opts) |
12635 | 0 | { |
12636 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12637 | 0 | __u32 relative_id; |
12638 | 0 | int relative_fd; |
12639 | |
|
12640 | 0 | if (!OPTS_VALID(opts, bpf_tcx_opts)) |
12641 | 0 | return libbpf_err_ptr(-EINVAL); |
12642 | | |
12643 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
12644 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
12645 | | |
12646 | | /* validate we don't have unexpected combinations of non-zero fields */ |
12647 | 0 | if (!ifindex) { |
12648 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
12649 | 0 | prog->name); |
12650 | 0 | return libbpf_err_ptr(-EINVAL); |
12651 | 0 | } |
12652 | 0 | if (relative_fd && relative_id) { |
12653 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
12654 | 0 | prog->name); |
12655 | 0 | return libbpf_err_ptr(-EINVAL); |
12656 | 0 | } |
12657 | | |
12658 | 0 | link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0); |
12659 | 0 | link_create_opts.tcx.relative_fd = relative_fd; |
12660 | 0 | link_create_opts.tcx.relative_id = relative_id; |
12661 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
12662 | | |
12663 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
12664 | 0 | return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts); |
12665 | 0 | } |
12666 | | |
12667 | | struct bpf_link * |
12668 | | bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, |
12669 | | const struct bpf_netkit_opts *opts) |
12670 | 0 | { |
12671 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12672 | 0 | __u32 relative_id; |
12673 | 0 | int relative_fd; |
12674 | |
|
12675 | 0 | if (!OPTS_VALID(opts, bpf_netkit_opts)) |
12676 | 0 | return libbpf_err_ptr(-EINVAL); |
12677 | | |
12678 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
12679 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
12680 | | |
12681 | | /* validate we don't have unexpected combinations of non-zero fields */ |
12682 | 0 | if (!ifindex) { |
12683 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
12684 | 0 | prog->name); |
12685 | 0 | return libbpf_err_ptr(-EINVAL); |
12686 | 0 | } |
12687 | 0 | if (relative_fd && relative_id) { |
12688 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
12689 | 0 | prog->name); |
12690 | 0 | return libbpf_err_ptr(-EINVAL); |
12691 | 0 | } |
12692 | | |
12693 | 0 | link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0); |
12694 | 0 | link_create_opts.netkit.relative_fd = relative_fd; |
12695 | 0 | link_create_opts.netkit.relative_id = relative_id; |
12696 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
12697 | |
|
12698 | 0 | return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts); |
12699 | 0 | } |
12700 | | |
12701 | | struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog, |
12702 | | int target_fd, |
12703 | | const char *attach_func_name) |
12704 | 0 | { |
12705 | 0 | int btf_id; |
12706 | |
|
12707 | 0 | if (!!target_fd != !!attach_func_name) { |
12708 | 0 | pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n", |
12709 | 0 | prog->name); |
12710 | 0 | return libbpf_err_ptr(-EINVAL); |
12711 | 0 | } |
12712 | | |
12713 | 0 | if (prog->type != BPF_PROG_TYPE_EXT) { |
12714 | 0 | pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace", |
12715 | 0 | prog->name); |
12716 | 0 | return libbpf_err_ptr(-EINVAL); |
12717 | 0 | } |
12718 | | |
12719 | 0 | if (target_fd) { |
12720 | 0 | LIBBPF_OPTS(bpf_link_create_opts, target_opts); |
12721 | |
|
12722 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd); |
12723 | 0 | if (btf_id < 0) |
12724 | 0 | return libbpf_err_ptr(btf_id); |
12725 | | |
12726 | 0 | target_opts.target_btf_id = btf_id; |
12727 | |
|
12728 | 0 | return bpf_program_attach_fd(prog, target_fd, "freplace", |
12729 | 0 | &target_opts); |
12730 | 0 | } else { |
12731 | | /* no target, so use raw_tracepoint_open for compatibility |
12732 | | * with old kernels |
12733 | | */ |
12734 | 0 | return bpf_program__attach_trace(prog); |
12735 | 0 | } |
12736 | 0 | } |
12737 | | |
12738 | | struct bpf_link * |
12739 | | bpf_program__attach_iter(const struct bpf_program *prog, |
12740 | | const struct bpf_iter_attach_opts *opts) |
12741 | 0 | { |
12742 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12743 | 0 | char errmsg[STRERR_BUFSIZE]; |
12744 | 0 | struct bpf_link *link; |
12745 | 0 | int prog_fd, link_fd; |
12746 | 0 | __u32 target_fd = 0; |
12747 | |
|
12748 | 0 | if (!OPTS_VALID(opts, bpf_iter_attach_opts)) |
12749 | 0 | return libbpf_err_ptr(-EINVAL); |
12750 | | |
12751 | 0 | link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0); |
12752 | 0 | link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0); |
12753 | |
|
12754 | 0 | prog_fd = bpf_program__fd(prog); |
12755 | 0 | if (prog_fd < 0) { |
12756 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12757 | 0 | return libbpf_err_ptr(-EINVAL); |
12758 | 0 | } |
12759 | | |
12760 | 0 | link = calloc(1, sizeof(*link)); |
12761 | 0 | if (!link) |
12762 | 0 | return libbpf_err_ptr(-ENOMEM); |
12763 | 0 | link->detach = &bpf_link__detach_fd; |
12764 | |
|
12765 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, |
12766 | 0 | &link_create_opts); |
12767 | 0 | if (link_fd < 0) { |
12768 | 0 | link_fd = -errno; |
12769 | 0 | free(link); |
12770 | 0 | pr_warn("prog '%s': failed to attach to iterator: %s\n", |
12771 | 0 | prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12772 | 0 | return libbpf_err_ptr(link_fd); |
12773 | 0 | } |
12774 | 0 | link->fd = link_fd; |
12775 | 0 | return link; |
12776 | 0 | } |
12777 | | |
12778 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12779 | 0 | { |
12780 | 0 | *link = bpf_program__attach_iter(prog, NULL); |
12781 | 0 | return libbpf_get_error(*link); |
12782 | 0 | } |
12783 | | |
12784 | | struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog, |
12785 | | const struct bpf_netfilter_opts *opts) |
12786 | 0 | { |
12787 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
12788 | 0 | struct bpf_link *link; |
12789 | 0 | int prog_fd, link_fd; |
12790 | |
|
12791 | 0 | if (!OPTS_VALID(opts, bpf_netfilter_opts)) |
12792 | 0 | return libbpf_err_ptr(-EINVAL); |
12793 | | |
12794 | 0 | prog_fd = bpf_program__fd(prog); |
12795 | 0 | if (prog_fd < 0) { |
12796 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12797 | 0 | return libbpf_err_ptr(-EINVAL); |
12798 | 0 | } |
12799 | | |
12800 | 0 | link = calloc(1, sizeof(*link)); |
12801 | 0 | if (!link) |
12802 | 0 | return libbpf_err_ptr(-ENOMEM); |
12803 | | |
12804 | 0 | link->detach = &bpf_link__detach_fd; |
12805 | |
|
12806 | 0 | lopts.netfilter.pf = OPTS_GET(opts, pf, 0); |
12807 | 0 | lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0); |
12808 | 0 | lopts.netfilter.priority = OPTS_GET(opts, priority, 0); |
12809 | 0 | lopts.netfilter.flags = OPTS_GET(opts, flags, 0); |
12810 | |
|
12811 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts); |
12812 | 0 | if (link_fd < 0) { |
12813 | 0 | char errmsg[STRERR_BUFSIZE]; |
12814 | |
|
12815 | 0 | link_fd = -errno; |
12816 | 0 | free(link); |
12817 | 0 | pr_warn("prog '%s': failed to attach to netfilter: %s\n", |
12818 | 0 | prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12819 | 0 | return libbpf_err_ptr(link_fd); |
12820 | 0 | } |
12821 | 0 | link->fd = link_fd; |
12822 | |
|
12823 | 0 | return link; |
12824 | 0 | } |
12825 | | |
12826 | | struct bpf_link *bpf_program__attach(const struct bpf_program *prog) |
12827 | 0 | { |
12828 | 0 | struct bpf_link *link = NULL; |
12829 | 0 | int err; |
12830 | |
|
12831 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
12832 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
12833 | | |
12834 | 0 | if (bpf_program__fd(prog) < 0) { |
12835 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
12836 | 0 | prog->name); |
12837 | 0 | return libbpf_err_ptr(-EINVAL); |
12838 | 0 | } |
12839 | | |
12840 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); |
12841 | 0 | if (err) |
12842 | 0 | return libbpf_err_ptr(err); |
12843 | | |
12844 | | /* When calling bpf_program__attach() explicitly, auto-attach support |
12845 | | * is expected to work, so NULL returned link is considered an error. |
12846 | | * This is different for skeleton's attach, see comment in |
12847 | | * bpf_object__attach_skeleton(). |
12848 | | */ |
12849 | 0 | if (!link) |
12850 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
12851 | | |
12852 | 0 | return link; |
12853 | 0 | } |
12854 | | |
12855 | | struct bpf_link_struct_ops { |
12856 | | struct bpf_link link; |
12857 | | int map_fd; |
12858 | | }; |
12859 | | |
12860 | | static int bpf_link__detach_struct_ops(struct bpf_link *link) |
12861 | 0 | { |
12862 | 0 | struct bpf_link_struct_ops *st_link; |
12863 | 0 | __u32 zero = 0; |
12864 | |
|
12865 | 0 | st_link = container_of(link, struct bpf_link_struct_ops, link); |
12866 | |
|
12867 | 0 | if (st_link->map_fd < 0) |
12868 | | /* w/o a real link */ |
12869 | 0 | return bpf_map_delete_elem(link->fd, &zero); |
12870 | | |
12871 | 0 | return close(link->fd); |
12872 | 0 | } |
12873 | | |
12874 | | struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map) |
12875 | 0 | { |
12876 | 0 | struct bpf_link_struct_ops *link; |
12877 | 0 | __u32 zero = 0; |
12878 | 0 | int err, fd; |
12879 | |
|
12880 | 0 | if (!bpf_map__is_struct_ops(map)) |
12881 | 0 | return libbpf_err_ptr(-EINVAL); |
12882 | | |
12883 | 0 | if (map->fd < 0) { |
12884 | 0 | pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name); |
12885 | 0 | return libbpf_err_ptr(-EINVAL); |
12886 | 0 | } |
12887 | | |
12888 | 0 | link = calloc(1, sizeof(*link)); |
12889 | 0 | if (!link) |
12890 | 0 | return libbpf_err_ptr(-EINVAL); |
12891 | | |
12892 | | /* kern_vdata should be prepared during the loading phase. */ |
12893 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
12894 | | /* It can be EBUSY if the map has been used to create or |
12895 | | * update a link before. We don't allow updating the value of |
12896 | | * a struct_ops once it is set. That ensures that the value |
12897 | | * never changed. So, it is safe to skip EBUSY. |
12898 | | */ |
12899 | 0 | if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { |
12900 | 0 | free(link); |
12901 | 0 | return libbpf_err_ptr(err); |
12902 | 0 | } |
12903 | | |
12904 | 0 | link->link.detach = bpf_link__detach_struct_ops; |
12905 | |
|
12906 | 0 | if (!(map->def.map_flags & BPF_F_LINK)) { |
12907 | | /* w/o a real link */ |
12908 | 0 | link->link.fd = map->fd; |
12909 | 0 | link->map_fd = -1; |
12910 | 0 | return &link->link; |
12911 | 0 | } |
12912 | | |
12913 | 0 | fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); |
12914 | 0 | if (fd < 0) { |
12915 | 0 | free(link); |
12916 | 0 | return libbpf_err_ptr(fd); |
12917 | 0 | } |
12918 | | |
12919 | 0 | link->link.fd = fd; |
12920 | 0 | link->map_fd = map->fd; |
12921 | |
|
12922 | 0 | return &link->link; |
12923 | 0 | } |
12924 | | |
12925 | | /* |
12926 | | * Swap the back struct_ops of a link with a new struct_ops map. |
12927 | | */ |
12928 | | int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map) |
12929 | 0 | { |
12930 | 0 | struct bpf_link_struct_ops *st_ops_link; |
12931 | 0 | __u32 zero = 0; |
12932 | 0 | int err; |
12933 | |
|
12934 | 0 | if (!bpf_map__is_struct_ops(map)) |
12935 | 0 | return -EINVAL; |
12936 | | |
12937 | 0 | if (map->fd < 0) { |
12938 | 0 | pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); |
12939 | 0 | return -EINVAL; |
12940 | 0 | } |
12941 | | |
12942 | 0 | st_ops_link = container_of(link, struct bpf_link_struct_ops, link); |
12943 | | /* Ensure the type of a link is correct */ |
12944 | 0 | if (st_ops_link->map_fd < 0) |
12945 | 0 | return -EINVAL; |
12946 | | |
12947 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
12948 | | /* It can be EBUSY if the map has been used to create or |
12949 | | * update a link before. We don't allow updating the value of |
12950 | | * a struct_ops once it is set. That ensures that the value |
12951 | | * never changed. So, it is safe to skip EBUSY. |
12952 | | */ |
12953 | 0 | if (err && err != -EBUSY) |
12954 | 0 | return err; |
12955 | | |
12956 | 0 | err = bpf_link_update(link->fd, map->fd, NULL); |
12957 | 0 | if (err < 0) |
12958 | 0 | return err; |
12959 | | |
12960 | 0 | st_ops_link->map_fd = map->fd; |
12961 | |
|
12962 | 0 | return 0; |
12963 | 0 | } |
12964 | | |
12965 | | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr, |
12966 | | void *private_data); |
12967 | | |
12968 | | static enum bpf_perf_event_ret |
12969 | | perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
12970 | | void **copy_mem, size_t *copy_size, |
12971 | | bpf_perf_event_print_t fn, void *private_data) |
12972 | 0 | { |
12973 | 0 | struct perf_event_mmap_page *header = mmap_mem; |
12974 | 0 | __u64 data_head = ring_buffer_read_head(header); |
12975 | 0 | __u64 data_tail = header->data_tail; |
12976 | 0 | void *base = ((__u8 *)header) + page_size; |
12977 | 0 | int ret = LIBBPF_PERF_EVENT_CONT; |
12978 | 0 | struct perf_event_header *ehdr; |
12979 | 0 | size_t ehdr_size; |
12980 | |
|
12981 | 0 | while (data_head != data_tail) { |
12982 | 0 | ehdr = base + (data_tail & (mmap_size - 1)); |
12983 | 0 | ehdr_size = ehdr->size; |
12984 | |
|
12985 | 0 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { |
12986 | 0 | void *copy_start = ehdr; |
12987 | 0 | size_t len_first = base + mmap_size - copy_start; |
12988 | 0 | size_t len_secnd = ehdr_size - len_first; |
12989 | |
|
12990 | 0 | if (*copy_size < ehdr_size) { |
12991 | 0 | free(*copy_mem); |
12992 | 0 | *copy_mem = malloc(ehdr_size); |
12993 | 0 | if (!*copy_mem) { |
12994 | 0 | *copy_size = 0; |
12995 | 0 | ret = LIBBPF_PERF_EVENT_ERROR; |
12996 | 0 | break; |
12997 | 0 | } |
12998 | 0 | *copy_size = ehdr_size; |
12999 | 0 | } |
13000 | | |
13001 | 0 | memcpy(*copy_mem, copy_start, len_first); |
13002 | 0 | memcpy(*copy_mem + len_first, base, len_secnd); |
13003 | 0 | ehdr = *copy_mem; |
13004 | 0 | } |
13005 | | |
13006 | 0 | ret = fn(ehdr, private_data); |
13007 | 0 | data_tail += ehdr_size; |
13008 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
13009 | 0 | break; |
13010 | 0 | } |
13011 | |
|
13012 | 0 | ring_buffer_write_tail(header, data_tail); |
13013 | 0 | return libbpf_err(ret); |
13014 | 0 | } |
13015 | | |
13016 | | struct perf_buffer; |
13017 | | |
13018 | | struct perf_buffer_params { |
13019 | | struct perf_event_attr *attr; |
13020 | | /* if event_cb is specified, it takes precendence */ |
13021 | | perf_buffer_event_fn event_cb; |
13022 | | /* sample_cb and lost_cb are higher-level common-case callbacks */ |
13023 | | perf_buffer_sample_fn sample_cb; |
13024 | | perf_buffer_lost_fn lost_cb; |
13025 | | void *ctx; |
13026 | | int cpu_cnt; |
13027 | | int *cpus; |
13028 | | int *map_keys; |
13029 | | }; |
13030 | | |
13031 | | struct perf_cpu_buf { |
13032 | | struct perf_buffer *pb; |
13033 | | void *base; /* mmap()'ed memory */ |
13034 | | void *buf; /* for reconstructing segmented data */ |
13035 | | size_t buf_size; |
13036 | | int fd; |
13037 | | int cpu; |
13038 | | int map_key; |
13039 | | }; |
13040 | | |
13041 | | struct perf_buffer { |
13042 | | perf_buffer_event_fn event_cb; |
13043 | | perf_buffer_sample_fn sample_cb; |
13044 | | perf_buffer_lost_fn lost_cb; |
13045 | | void *ctx; /* passed into callbacks */ |
13046 | | |
13047 | | size_t page_size; |
13048 | | size_t mmap_size; |
13049 | | struct perf_cpu_buf **cpu_bufs; |
13050 | | struct epoll_event *events; |
13051 | | int cpu_cnt; /* number of allocated CPU buffers */ |
13052 | | int epoll_fd; /* perf event FD */ |
13053 | | int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ |
13054 | | }; |
13055 | | |
13056 | | static void perf_buffer__free_cpu_buf(struct perf_buffer *pb, |
13057 | | struct perf_cpu_buf *cpu_buf) |
13058 | 0 | { |
13059 | 0 | if (!cpu_buf) |
13060 | 0 | return; |
13061 | 0 | if (cpu_buf->base && |
13062 | 0 | munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) |
13063 | 0 | pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); |
13064 | 0 | if (cpu_buf->fd >= 0) { |
13065 | 0 | ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); |
13066 | 0 | close(cpu_buf->fd); |
13067 | 0 | } |
13068 | 0 | free(cpu_buf->buf); |
13069 | 0 | free(cpu_buf); |
13070 | 0 | } |
13071 | | |
13072 | | void perf_buffer__free(struct perf_buffer *pb) |
13073 | 0 | { |
13074 | 0 | int i; |
13075 | |
|
13076 | 0 | if (IS_ERR_OR_NULL(pb)) |
13077 | 0 | return; |
13078 | 0 | if (pb->cpu_bufs) { |
13079 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
13080 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
13081 | |
|
13082 | 0 | if (!cpu_buf) |
13083 | 0 | continue; |
13084 | | |
13085 | 0 | bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); |
13086 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
13087 | 0 | } |
13088 | 0 | free(pb->cpu_bufs); |
13089 | 0 | } |
13090 | 0 | if (pb->epoll_fd >= 0) |
13091 | 0 | close(pb->epoll_fd); |
13092 | 0 | free(pb->events); |
13093 | 0 | free(pb); |
13094 | 0 | } |
13095 | | |
13096 | | static struct perf_cpu_buf * |
13097 | | perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, |
13098 | | int cpu, int map_key) |
13099 | 0 | { |
13100 | 0 | struct perf_cpu_buf *cpu_buf; |
13101 | 0 | char msg[STRERR_BUFSIZE]; |
13102 | 0 | int err; |
13103 | |
|
13104 | 0 | cpu_buf = calloc(1, sizeof(*cpu_buf)); |
13105 | 0 | if (!cpu_buf) |
13106 | 0 | return ERR_PTR(-ENOMEM); |
13107 | | |
13108 | 0 | cpu_buf->pb = pb; |
13109 | 0 | cpu_buf->cpu = cpu; |
13110 | 0 | cpu_buf->map_key = map_key; |
13111 | |
|
13112 | 0 | cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, |
13113 | 0 | -1, PERF_FLAG_FD_CLOEXEC); |
13114 | 0 | if (cpu_buf->fd < 0) { |
13115 | 0 | err = -errno; |
13116 | 0 | pr_warn("failed to open perf buffer event on cpu #%d: %s\n", |
13117 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
13118 | 0 | goto error; |
13119 | 0 | } |
13120 | | |
13121 | 0 | cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, |
13122 | 0 | PROT_READ | PROT_WRITE, MAP_SHARED, |
13123 | 0 | cpu_buf->fd, 0); |
13124 | 0 | if (cpu_buf->base == MAP_FAILED) { |
13125 | 0 | cpu_buf->base = NULL; |
13126 | 0 | err = -errno; |
13127 | 0 | pr_warn("failed to mmap perf buffer on cpu #%d: %s\n", |
13128 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
13129 | 0 | goto error; |
13130 | 0 | } |
13131 | | |
13132 | 0 | if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
13133 | 0 | err = -errno; |
13134 | 0 | pr_warn("failed to enable perf buffer event on cpu #%d: %s\n", |
13135 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
13136 | 0 | goto error; |
13137 | 0 | } |
13138 | | |
13139 | 0 | return cpu_buf; |
13140 | | |
13141 | 0 | error: |
13142 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
13143 | 0 | return (struct perf_cpu_buf *)ERR_PTR(err); |
13144 | 0 | } |
13145 | | |
13146 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
13147 | | struct perf_buffer_params *p); |
13148 | | |
13149 | | struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, |
13150 | | perf_buffer_sample_fn sample_cb, |
13151 | | perf_buffer_lost_fn lost_cb, |
13152 | | void *ctx, |
13153 | | const struct perf_buffer_opts *opts) |
13154 | 0 | { |
13155 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
13156 | 0 | struct perf_buffer_params p = {}; |
13157 | 0 | struct perf_event_attr attr; |
13158 | 0 | __u32 sample_period; |
13159 | |
|
13160 | 0 | if (!OPTS_VALID(opts, perf_buffer_opts)) |
13161 | 0 | return libbpf_err_ptr(-EINVAL); |
13162 | | |
13163 | 0 | sample_period = OPTS_GET(opts, sample_period, 1); |
13164 | 0 | if (!sample_period) |
13165 | 0 | sample_period = 1; |
13166 | |
|
13167 | 0 | memset(&attr, 0, attr_sz); |
13168 | 0 | attr.size = attr_sz; |
13169 | 0 | attr.config = PERF_COUNT_SW_BPF_OUTPUT; |
13170 | 0 | attr.type = PERF_TYPE_SOFTWARE; |
13171 | 0 | attr.sample_type = PERF_SAMPLE_RAW; |
13172 | 0 | attr.sample_period = sample_period; |
13173 | 0 | attr.wakeup_events = sample_period; |
13174 | |
|
13175 | 0 | p.attr = &attr; |
13176 | 0 | p.sample_cb = sample_cb; |
13177 | 0 | p.lost_cb = lost_cb; |
13178 | 0 | p.ctx = ctx; |
13179 | |
|
13180 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
13181 | 0 | } |
13182 | | |
13183 | | struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt, |
13184 | | struct perf_event_attr *attr, |
13185 | | perf_buffer_event_fn event_cb, void *ctx, |
13186 | | const struct perf_buffer_raw_opts *opts) |
13187 | 0 | { |
13188 | 0 | struct perf_buffer_params p = {}; |
13189 | |
|
13190 | 0 | if (!attr) |
13191 | 0 | return libbpf_err_ptr(-EINVAL); |
13192 | | |
13193 | 0 | if (!OPTS_VALID(opts, perf_buffer_raw_opts)) |
13194 | 0 | return libbpf_err_ptr(-EINVAL); |
13195 | | |
13196 | 0 | p.attr = attr; |
13197 | 0 | p.event_cb = event_cb; |
13198 | 0 | p.ctx = ctx; |
13199 | 0 | p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0); |
13200 | 0 | p.cpus = OPTS_GET(opts, cpus, NULL); |
13201 | 0 | p.map_keys = OPTS_GET(opts, map_keys, NULL); |
13202 | |
|
13203 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
13204 | 0 | } |
13205 | | |
13206 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
13207 | | struct perf_buffer_params *p) |
13208 | 0 | { |
13209 | 0 | const char *online_cpus_file = "/sys/devices/system/cpu/online"; |
13210 | 0 | struct bpf_map_info map; |
13211 | 0 | char msg[STRERR_BUFSIZE]; |
13212 | 0 | struct perf_buffer *pb; |
13213 | 0 | bool *online = NULL; |
13214 | 0 | __u32 map_info_len; |
13215 | 0 | int err, i, j, n; |
13216 | |
|
13217 | 0 | if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { |
13218 | 0 | pr_warn("page count should be power of two, but is %zu\n", |
13219 | 0 | page_cnt); |
13220 | 0 | return ERR_PTR(-EINVAL); |
13221 | 0 | } |
13222 | | |
13223 | | /* best-effort sanity checks */ |
13224 | 0 | memset(&map, 0, sizeof(map)); |
13225 | 0 | map_info_len = sizeof(map); |
13226 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len); |
13227 | 0 | if (err) { |
13228 | 0 | err = -errno; |
13229 | | /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return |
13230 | | * -EBADFD, -EFAULT, or -E2BIG on real error |
13231 | | */ |
13232 | 0 | if (err != -EINVAL) { |
13233 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", |
13234 | 0 | map_fd, libbpf_strerror_r(err, msg, sizeof(msg))); |
13235 | 0 | return ERR_PTR(err); |
13236 | 0 | } |
13237 | 0 | pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n", |
13238 | 0 | map_fd); |
13239 | 0 | } else { |
13240 | 0 | if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
13241 | 0 | pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
13242 | 0 | map.name); |
13243 | 0 | return ERR_PTR(-EINVAL); |
13244 | 0 | } |
13245 | 0 | } |
13246 | | |
13247 | 0 | pb = calloc(1, sizeof(*pb)); |
13248 | 0 | if (!pb) |
13249 | 0 | return ERR_PTR(-ENOMEM); |
13250 | | |
13251 | 0 | pb->event_cb = p->event_cb; |
13252 | 0 | pb->sample_cb = p->sample_cb; |
13253 | 0 | pb->lost_cb = p->lost_cb; |
13254 | 0 | pb->ctx = p->ctx; |
13255 | |
|
13256 | 0 | pb->page_size = getpagesize(); |
13257 | 0 | pb->mmap_size = pb->page_size * page_cnt; |
13258 | 0 | pb->map_fd = map_fd; |
13259 | |
|
13260 | 0 | pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
13261 | 0 | if (pb->epoll_fd < 0) { |
13262 | 0 | err = -errno; |
13263 | 0 | pr_warn("failed to create epoll instance: %s\n", |
13264 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
13265 | 0 | goto error; |
13266 | 0 | } |
13267 | | |
13268 | 0 | if (p->cpu_cnt > 0) { |
13269 | 0 | pb->cpu_cnt = p->cpu_cnt; |
13270 | 0 | } else { |
13271 | 0 | pb->cpu_cnt = libbpf_num_possible_cpus(); |
13272 | 0 | if (pb->cpu_cnt < 0) { |
13273 | 0 | err = pb->cpu_cnt; |
13274 | 0 | goto error; |
13275 | 0 | } |
13276 | 0 | if (map.max_entries && map.max_entries < pb->cpu_cnt) |
13277 | 0 | pb->cpu_cnt = map.max_entries; |
13278 | 0 | } |
13279 | | |
13280 | 0 | pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); |
13281 | 0 | if (!pb->events) { |
13282 | 0 | err = -ENOMEM; |
13283 | 0 | pr_warn("failed to allocate events: out of memory\n"); |
13284 | 0 | goto error; |
13285 | 0 | } |
13286 | 0 | pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); |
13287 | 0 | if (!pb->cpu_bufs) { |
13288 | 0 | err = -ENOMEM; |
13289 | 0 | pr_warn("failed to allocate buffers: out of memory\n"); |
13290 | 0 | goto error; |
13291 | 0 | } |
13292 | | |
13293 | 0 | err = parse_cpu_mask_file(online_cpus_file, &online, &n); |
13294 | 0 | if (err) { |
13295 | 0 | pr_warn("failed to get online CPU mask: %d\n", err); |
13296 | 0 | goto error; |
13297 | 0 | } |
13298 | | |
13299 | 0 | for (i = 0, j = 0; i < pb->cpu_cnt; i++) { |
13300 | 0 | struct perf_cpu_buf *cpu_buf; |
13301 | 0 | int cpu, map_key; |
13302 | |
|
13303 | 0 | cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; |
13304 | 0 | map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; |
13305 | | |
13306 | | /* in case user didn't explicitly requested particular CPUs to |
13307 | | * be attached to, skip offline/not present CPUs |
13308 | | */ |
13309 | 0 | if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) |
13310 | 0 | continue; |
13311 | | |
13312 | 0 | cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); |
13313 | 0 | if (IS_ERR(cpu_buf)) { |
13314 | 0 | err = PTR_ERR(cpu_buf); |
13315 | 0 | goto error; |
13316 | 0 | } |
13317 | | |
13318 | 0 | pb->cpu_bufs[j] = cpu_buf; |
13319 | |
|
13320 | 0 | err = bpf_map_update_elem(pb->map_fd, &map_key, |
13321 | 0 | &cpu_buf->fd, 0); |
13322 | 0 | if (err) { |
13323 | 0 | err = -errno; |
13324 | 0 | pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", |
13325 | 0 | cpu, map_key, cpu_buf->fd, |
13326 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
13327 | 0 | goto error; |
13328 | 0 | } |
13329 | | |
13330 | 0 | pb->events[j].events = EPOLLIN; |
13331 | 0 | pb->events[j].data.ptr = cpu_buf; |
13332 | 0 | if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, |
13333 | 0 | &pb->events[j]) < 0) { |
13334 | 0 | err = -errno; |
13335 | 0 | pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", |
13336 | 0 | cpu, cpu_buf->fd, |
13337 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
13338 | 0 | goto error; |
13339 | 0 | } |
13340 | 0 | j++; |
13341 | 0 | } |
13342 | 0 | pb->cpu_cnt = j; |
13343 | 0 | free(online); |
13344 | |
|
13345 | 0 | return pb; |
13346 | | |
13347 | 0 | error: |
13348 | 0 | free(online); |
13349 | 0 | if (pb) |
13350 | 0 | perf_buffer__free(pb); |
13351 | 0 | return ERR_PTR(err); |
13352 | 0 | } |
13353 | | |
13354 | | struct perf_sample_raw { |
13355 | | struct perf_event_header header; |
13356 | | uint32_t size; |
13357 | | char data[]; |
13358 | | }; |
13359 | | |
13360 | | struct perf_sample_lost { |
13361 | | struct perf_event_header header; |
13362 | | uint64_t id; |
13363 | | uint64_t lost; |
13364 | | uint64_t sample_id; |
13365 | | }; |
13366 | | |
13367 | | static enum bpf_perf_event_ret |
13368 | | perf_buffer__process_record(struct perf_event_header *e, void *ctx) |
13369 | 0 | { |
13370 | 0 | struct perf_cpu_buf *cpu_buf = ctx; |
13371 | 0 | struct perf_buffer *pb = cpu_buf->pb; |
13372 | 0 | void *data = e; |
13373 | | |
13374 | | /* user wants full control over parsing perf event */ |
13375 | 0 | if (pb->event_cb) |
13376 | 0 | return pb->event_cb(pb->ctx, cpu_buf->cpu, e); |
13377 | | |
13378 | 0 | switch (e->type) { |
13379 | 0 | case PERF_RECORD_SAMPLE: { |
13380 | 0 | struct perf_sample_raw *s = data; |
13381 | |
|
13382 | 0 | if (pb->sample_cb) |
13383 | 0 | pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); |
13384 | 0 | break; |
13385 | 0 | } |
13386 | 0 | case PERF_RECORD_LOST: { |
13387 | 0 | struct perf_sample_lost *s = data; |
13388 | |
|
13389 | 0 | if (pb->lost_cb) |
13390 | 0 | pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); |
13391 | 0 | break; |
13392 | 0 | } |
13393 | 0 | default: |
13394 | 0 | pr_warn("unknown perf sample type %d\n", e->type); |
13395 | 0 | return LIBBPF_PERF_EVENT_ERROR; |
13396 | 0 | } |
13397 | 0 | return LIBBPF_PERF_EVENT_CONT; |
13398 | 0 | } |
13399 | | |
13400 | | static int perf_buffer__process_records(struct perf_buffer *pb, |
13401 | | struct perf_cpu_buf *cpu_buf) |
13402 | 0 | { |
13403 | 0 | enum bpf_perf_event_ret ret; |
13404 | |
|
13405 | 0 | ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, |
13406 | 0 | pb->page_size, &cpu_buf->buf, |
13407 | 0 | &cpu_buf->buf_size, |
13408 | 0 | perf_buffer__process_record, cpu_buf); |
13409 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
13410 | 0 | return ret; |
13411 | 0 | return 0; |
13412 | 0 | } |
13413 | | |
13414 | | int perf_buffer__epoll_fd(const struct perf_buffer *pb) |
13415 | 0 | { |
13416 | 0 | return pb->epoll_fd; |
13417 | 0 | } |
13418 | | |
13419 | | int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) |
13420 | 0 | { |
13421 | 0 | int i, cnt, err; |
13422 | |
|
13423 | 0 | cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); |
13424 | 0 | if (cnt < 0) |
13425 | 0 | return -errno; |
13426 | | |
13427 | 0 | for (i = 0; i < cnt; i++) { |
13428 | 0 | struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; |
13429 | |
|
13430 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
13431 | 0 | if (err) { |
13432 | 0 | pr_warn("error while processing records: %d\n", err); |
13433 | 0 | return libbpf_err(err); |
13434 | 0 | } |
13435 | 0 | } |
13436 | 0 | return cnt; |
13437 | 0 | } |
13438 | | |
13439 | | /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer |
13440 | | * manager. |
13441 | | */ |
13442 | | size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb) |
13443 | 0 | { |
13444 | 0 | return pb->cpu_cnt; |
13445 | 0 | } |
13446 | | |
13447 | | /* |
13448 | | * Return perf_event FD of a ring buffer in *buf_idx* slot of |
13449 | | * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using |
13450 | | * select()/poll()/epoll() Linux syscalls. |
13451 | | */ |
13452 | | int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx) |
13453 | 0 | { |
13454 | 0 | struct perf_cpu_buf *cpu_buf; |
13455 | |
|
13456 | 0 | if (buf_idx >= pb->cpu_cnt) |
13457 | 0 | return libbpf_err(-EINVAL); |
13458 | | |
13459 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
13460 | 0 | if (!cpu_buf) |
13461 | 0 | return libbpf_err(-ENOENT); |
13462 | | |
13463 | 0 | return cpu_buf->fd; |
13464 | 0 | } |
13465 | | |
13466 | | int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size) |
13467 | 0 | { |
13468 | 0 | struct perf_cpu_buf *cpu_buf; |
13469 | |
|
13470 | 0 | if (buf_idx >= pb->cpu_cnt) |
13471 | 0 | return libbpf_err(-EINVAL); |
13472 | | |
13473 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
13474 | 0 | if (!cpu_buf) |
13475 | 0 | return libbpf_err(-ENOENT); |
13476 | | |
13477 | 0 | *buf = cpu_buf->base; |
13478 | 0 | *buf_size = pb->mmap_size; |
13479 | 0 | return 0; |
13480 | 0 | } |
13481 | | |
13482 | | /* |
13483 | | * Consume data from perf ring buffer corresponding to slot *buf_idx* in |
13484 | | * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to |
13485 | | * consume, do nothing and return success. |
13486 | | * Returns: |
13487 | | * - 0 on success; |
13488 | | * - <0 on failure. |
13489 | | */ |
13490 | | int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx) |
13491 | 0 | { |
13492 | 0 | struct perf_cpu_buf *cpu_buf; |
13493 | |
|
13494 | 0 | if (buf_idx >= pb->cpu_cnt) |
13495 | 0 | return libbpf_err(-EINVAL); |
13496 | | |
13497 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
13498 | 0 | if (!cpu_buf) |
13499 | 0 | return libbpf_err(-ENOENT); |
13500 | | |
13501 | 0 | return perf_buffer__process_records(pb, cpu_buf); |
13502 | 0 | } |
13503 | | |
13504 | | int perf_buffer__consume(struct perf_buffer *pb) |
13505 | 0 | { |
13506 | 0 | int i, err; |
13507 | |
|
13508 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
13509 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
13510 | |
|
13511 | 0 | if (!cpu_buf) |
13512 | 0 | continue; |
13513 | | |
13514 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
13515 | 0 | if (err) { |
13516 | 0 | pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err); |
13517 | 0 | return libbpf_err(err); |
13518 | 0 | } |
13519 | 0 | } |
13520 | 0 | return 0; |
13521 | 0 | } |
13522 | | |
13523 | | int bpf_program__set_attach_target(struct bpf_program *prog, |
13524 | | int attach_prog_fd, |
13525 | | const char *attach_func_name) |
13526 | 0 | { |
13527 | 0 | int btf_obj_fd = 0, btf_id = 0, err; |
13528 | |
|
13529 | 0 | if (!prog || attach_prog_fd < 0) |
13530 | 0 | return libbpf_err(-EINVAL); |
13531 | | |
13532 | 0 | if (prog->obj->loaded) |
13533 | 0 | return libbpf_err(-EINVAL); |
13534 | | |
13535 | 0 | if (attach_prog_fd && !attach_func_name) { |
13536 | | /* remember attach_prog_fd and let bpf_program__load() find |
13537 | | * BTF ID during the program load |
13538 | | */ |
13539 | 0 | prog->attach_prog_fd = attach_prog_fd; |
13540 | 0 | return 0; |
13541 | 0 | } |
13542 | | |
13543 | 0 | if (attach_prog_fd) { |
13544 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, |
13545 | 0 | attach_prog_fd); |
13546 | 0 | if (btf_id < 0) |
13547 | 0 | return libbpf_err(btf_id); |
13548 | 0 | } else { |
13549 | 0 | if (!attach_func_name) |
13550 | 0 | return libbpf_err(-EINVAL); |
13551 | | |
13552 | | /* load btf_vmlinux, if not yet */ |
13553 | 0 | err = bpf_object__load_vmlinux_btf(prog->obj, true); |
13554 | 0 | if (err) |
13555 | 0 | return libbpf_err(err); |
13556 | 0 | err = find_kernel_btf_id(prog->obj, attach_func_name, |
13557 | 0 | prog->expected_attach_type, |
13558 | 0 | &btf_obj_fd, &btf_id); |
13559 | 0 | if (err) |
13560 | 0 | return libbpf_err(err); |
13561 | 0 | } |
13562 | | |
13563 | 0 | prog->attach_btf_id = btf_id; |
13564 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
13565 | 0 | prog->attach_prog_fd = attach_prog_fd; |
13566 | 0 | return 0; |
13567 | 0 | } |
13568 | | |
13569 | | int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz) |
13570 | 0 | { |
13571 | 0 | int err = 0, n, len, start, end = -1; |
13572 | 0 | bool *tmp; |
13573 | |
|
13574 | 0 | *mask = NULL; |
13575 | 0 | *mask_sz = 0; |
13576 | | |
13577 | | /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ |
13578 | 0 | while (*s) { |
13579 | 0 | if (*s == ',' || *s == '\n') { |
13580 | 0 | s++; |
13581 | 0 | continue; |
13582 | 0 | } |
13583 | 0 | n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); |
13584 | 0 | if (n <= 0 || n > 2) { |
13585 | 0 | pr_warn("Failed to get CPU range %s: %d\n", s, n); |
13586 | 0 | err = -EINVAL; |
13587 | 0 | goto cleanup; |
13588 | 0 | } else if (n == 1) { |
13589 | 0 | end = start; |
13590 | 0 | } |
13591 | 0 | if (start < 0 || start > end) { |
13592 | 0 | pr_warn("Invalid CPU range [%d,%d] in %s\n", |
13593 | 0 | start, end, s); |
13594 | 0 | err = -EINVAL; |
13595 | 0 | goto cleanup; |
13596 | 0 | } |
13597 | 0 | tmp = realloc(*mask, end + 1); |
13598 | 0 | if (!tmp) { |
13599 | 0 | err = -ENOMEM; |
13600 | 0 | goto cleanup; |
13601 | 0 | } |
13602 | 0 | *mask = tmp; |
13603 | 0 | memset(tmp + *mask_sz, 0, start - *mask_sz); |
13604 | 0 | memset(tmp + start, 1, end - start + 1); |
13605 | 0 | *mask_sz = end + 1; |
13606 | 0 | s += len; |
13607 | 0 | } |
13608 | 0 | if (!*mask_sz) { |
13609 | 0 | pr_warn("Empty CPU range\n"); |
13610 | 0 | return -EINVAL; |
13611 | 0 | } |
13612 | 0 | return 0; |
13613 | 0 | cleanup: |
13614 | 0 | free(*mask); |
13615 | 0 | *mask = NULL; |
13616 | 0 | return err; |
13617 | 0 | } |
13618 | | |
13619 | | int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) |
13620 | 0 | { |
13621 | 0 | int fd, err = 0, len; |
13622 | 0 | char buf[128]; |
13623 | |
|
13624 | 0 | fd = open(fcpu, O_RDONLY | O_CLOEXEC); |
13625 | 0 | if (fd < 0) { |
13626 | 0 | err = -errno; |
13627 | 0 | pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err); |
13628 | 0 | return err; |
13629 | 0 | } |
13630 | 0 | len = read(fd, buf, sizeof(buf)); |
13631 | 0 | close(fd); |
13632 | 0 | if (len <= 0) { |
13633 | 0 | err = len ? -errno : -EINVAL; |
13634 | 0 | pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err); |
13635 | 0 | return err; |
13636 | 0 | } |
13637 | 0 | if (len >= sizeof(buf)) { |
13638 | 0 | pr_warn("CPU mask is too big in file %s\n", fcpu); |
13639 | 0 | return -E2BIG; |
13640 | 0 | } |
13641 | 0 | buf[len] = '\0'; |
13642 | |
|
13643 | 0 | return parse_cpu_mask_str(buf, mask, mask_sz); |
13644 | 0 | } |
13645 | | |
13646 | | int libbpf_num_possible_cpus(void) |
13647 | 0 | { |
13648 | 0 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
13649 | 0 | static int cpus; |
13650 | 0 | int err, n, i, tmp_cpus; |
13651 | 0 | bool *mask; |
13652 | |
|
13653 | 0 | tmp_cpus = READ_ONCE(cpus); |
13654 | 0 | if (tmp_cpus > 0) |
13655 | 0 | return tmp_cpus; |
13656 | | |
13657 | 0 | err = parse_cpu_mask_file(fcpu, &mask, &n); |
13658 | 0 | if (err) |
13659 | 0 | return libbpf_err(err); |
13660 | | |
13661 | 0 | tmp_cpus = 0; |
13662 | 0 | for (i = 0; i < n; i++) { |
13663 | 0 | if (mask[i]) |
13664 | 0 | tmp_cpus++; |
13665 | 0 | } |
13666 | 0 | free(mask); |
13667 | |
|
13668 | 0 | WRITE_ONCE(cpus, tmp_cpus); |
13669 | 0 | return tmp_cpus; |
13670 | 0 | } |
13671 | | |
13672 | | static int populate_skeleton_maps(const struct bpf_object *obj, |
13673 | | struct bpf_map_skeleton *maps, |
13674 | | size_t map_cnt) |
13675 | 0 | { |
13676 | 0 | int i; |
13677 | |
|
13678 | 0 | for (i = 0; i < map_cnt; i++) { |
13679 | 0 | struct bpf_map **map = maps[i].map; |
13680 | 0 | const char *name = maps[i].name; |
13681 | 0 | void **mmaped = maps[i].mmaped; |
13682 | |
|
13683 | 0 | *map = bpf_object__find_map_by_name(obj, name); |
13684 | 0 | if (!*map) { |
13685 | 0 | pr_warn("failed to find skeleton map '%s'\n", name); |
13686 | 0 | return -ESRCH; |
13687 | 0 | } |
13688 | | |
13689 | | /* externs shouldn't be pre-setup from user code */ |
13690 | 0 | if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) |
13691 | 0 | *mmaped = (*map)->mmaped; |
13692 | 0 | } |
13693 | 0 | return 0; |
13694 | 0 | } |
13695 | | |
13696 | | static int populate_skeleton_progs(const struct bpf_object *obj, |
13697 | | struct bpf_prog_skeleton *progs, |
13698 | | size_t prog_cnt) |
13699 | 0 | { |
13700 | 0 | int i; |
13701 | |
|
13702 | 0 | for (i = 0; i < prog_cnt; i++) { |
13703 | 0 | struct bpf_program **prog = progs[i].prog; |
13704 | 0 | const char *name = progs[i].name; |
13705 | |
|
13706 | 0 | *prog = bpf_object__find_program_by_name(obj, name); |
13707 | 0 | if (!*prog) { |
13708 | 0 | pr_warn("failed to find skeleton program '%s'\n", name); |
13709 | 0 | return -ESRCH; |
13710 | 0 | } |
13711 | 0 | } |
13712 | 0 | return 0; |
13713 | 0 | } |
13714 | | |
13715 | | int bpf_object__open_skeleton(struct bpf_object_skeleton *s, |
13716 | | const struct bpf_object_open_opts *opts) |
13717 | 0 | { |
13718 | 0 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts, |
13719 | 0 | .object_name = s->name, |
13720 | 0 | ); |
13721 | 0 | struct bpf_object *obj; |
13722 | 0 | int err; |
13723 | | |
13724 | | /* Attempt to preserve opts->object_name, unless overriden by user |
13725 | | * explicitly. Overwriting object name for skeletons is discouraged, |
13726 | | * as it breaks global data maps, because they contain object name |
13727 | | * prefix as their own map name prefix. When skeleton is generated, |
13728 | | * bpftool is making an assumption that this name will stay the same. |
13729 | | */ |
13730 | 0 | if (opts) { |
13731 | 0 | memcpy(&skel_opts, opts, sizeof(*opts)); |
13732 | 0 | if (!opts->object_name) |
13733 | 0 | skel_opts.object_name = s->name; |
13734 | 0 | } |
13735 | |
|
13736 | 0 | obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); |
13737 | 0 | err = libbpf_get_error(obj); |
13738 | 0 | if (err) { |
13739 | 0 | pr_warn("failed to initialize skeleton BPF object '%s': %d\n", |
13740 | 0 | s->name, err); |
13741 | 0 | return libbpf_err(err); |
13742 | 0 | } |
13743 | | |
13744 | 0 | *s->obj = obj; |
13745 | 0 | err = populate_skeleton_maps(obj, s->maps, s->map_cnt); |
13746 | 0 | if (err) { |
13747 | 0 | pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); |
13748 | 0 | return libbpf_err(err); |
13749 | 0 | } |
13750 | | |
13751 | 0 | err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); |
13752 | 0 | if (err) { |
13753 | 0 | pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); |
13754 | 0 | return libbpf_err(err); |
13755 | 0 | } |
13756 | | |
13757 | 0 | return 0; |
13758 | 0 | } |
13759 | | |
13760 | | int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) |
13761 | 0 | { |
13762 | 0 | int err, len, var_idx, i; |
13763 | 0 | const char *var_name; |
13764 | 0 | const struct bpf_map *map; |
13765 | 0 | struct btf *btf; |
13766 | 0 | __u32 map_type_id; |
13767 | 0 | const struct btf_type *map_type, *var_type; |
13768 | 0 | const struct bpf_var_skeleton *var_skel; |
13769 | 0 | struct btf_var_secinfo *var; |
13770 | |
|
13771 | 0 | if (!s->obj) |
13772 | 0 | return libbpf_err(-EINVAL); |
13773 | | |
13774 | 0 | btf = bpf_object__btf(s->obj); |
13775 | 0 | if (!btf) { |
13776 | 0 | pr_warn("subskeletons require BTF at runtime (object %s)\n", |
13777 | 0 | bpf_object__name(s->obj)); |
13778 | 0 | return libbpf_err(-errno); |
13779 | 0 | } |
13780 | | |
13781 | 0 | err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); |
13782 | 0 | if (err) { |
13783 | 0 | pr_warn("failed to populate subskeleton maps: %d\n", err); |
13784 | 0 | return libbpf_err(err); |
13785 | 0 | } |
13786 | | |
13787 | 0 | err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); |
13788 | 0 | if (err) { |
13789 | 0 | pr_warn("failed to populate subskeleton maps: %d\n", err); |
13790 | 0 | return libbpf_err(err); |
13791 | 0 | } |
13792 | | |
13793 | 0 | for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { |
13794 | 0 | var_skel = &s->vars[var_idx]; |
13795 | 0 | map = *var_skel->map; |
13796 | 0 | map_type_id = bpf_map__btf_value_type_id(map); |
13797 | 0 | map_type = btf__type_by_id(btf, map_type_id); |
13798 | |
|
13799 | 0 | if (!btf_is_datasec(map_type)) { |
13800 | 0 | pr_warn("type for map '%1$s' is not a datasec: %2$s", |
13801 | 0 | bpf_map__name(map), |
13802 | 0 | __btf_kind_str(btf_kind(map_type))); |
13803 | 0 | return libbpf_err(-EINVAL); |
13804 | 0 | } |
13805 | | |
13806 | 0 | len = btf_vlen(map_type); |
13807 | 0 | var = btf_var_secinfos(map_type); |
13808 | 0 | for (i = 0; i < len; i++, var++) { |
13809 | 0 | var_type = btf__type_by_id(btf, var->type); |
13810 | 0 | var_name = btf__name_by_offset(btf, var_type->name_off); |
13811 | 0 | if (strcmp(var_name, var_skel->name) == 0) { |
13812 | 0 | *var_skel->addr = map->mmaped + var->offset; |
13813 | 0 | break; |
13814 | 0 | } |
13815 | 0 | } |
13816 | 0 | } |
13817 | 0 | return 0; |
13818 | 0 | } |
13819 | | |
13820 | | void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) |
13821 | 0 | { |
13822 | 0 | if (!s) |
13823 | 0 | return; |
13824 | 0 | free(s->maps); |
13825 | 0 | free(s->progs); |
13826 | 0 | free(s->vars); |
13827 | 0 | free(s); |
13828 | 0 | } |
13829 | | |
13830 | | int bpf_object__load_skeleton(struct bpf_object_skeleton *s) |
13831 | 0 | { |
13832 | 0 | int i, err; |
13833 | |
|
13834 | 0 | err = bpf_object__load(*s->obj); |
13835 | 0 | if (err) { |
13836 | 0 | pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err); |
13837 | 0 | return libbpf_err(err); |
13838 | 0 | } |
13839 | | |
13840 | 0 | for (i = 0; i < s->map_cnt; i++) { |
13841 | 0 | struct bpf_map *map = *s->maps[i].map; |
13842 | 0 | size_t mmap_sz = bpf_map_mmap_sz(map); |
13843 | 0 | int prot, map_fd = map->fd; |
13844 | 0 | void **mmaped = s->maps[i].mmaped; |
13845 | |
|
13846 | 0 | if (!mmaped) |
13847 | 0 | continue; |
13848 | | |
13849 | 0 | if (!(map->def.map_flags & BPF_F_MMAPABLE)) { |
13850 | 0 | *mmaped = NULL; |
13851 | 0 | continue; |
13852 | 0 | } |
13853 | | |
13854 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) { |
13855 | 0 | *mmaped = map->mmaped; |
13856 | 0 | continue; |
13857 | 0 | } |
13858 | | |
13859 | 0 | if (map->def.map_flags & BPF_F_RDONLY_PROG) |
13860 | 0 | prot = PROT_READ; |
13861 | 0 | else |
13862 | 0 | prot = PROT_READ | PROT_WRITE; |
13863 | | |
13864 | | /* Remap anonymous mmap()-ed "map initialization image" as |
13865 | | * a BPF map-backed mmap()-ed memory, but preserving the same |
13866 | | * memory address. This will cause kernel to change process' |
13867 | | * page table to point to a different piece of kernel memory, |
13868 | | * but from userspace point of view memory address (and its |
13869 | | * contents, being identical at this point) will stay the |
13870 | | * same. This mapping will be released by bpf_object__close() |
13871 | | * as per normal clean up procedure, so we don't need to worry |
13872 | | * about it from skeleton's clean up perspective. |
13873 | | */ |
13874 | 0 | *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0); |
13875 | 0 | if (*mmaped == MAP_FAILED) { |
13876 | 0 | err = -errno; |
13877 | 0 | *mmaped = NULL; |
13878 | 0 | pr_warn("failed to re-mmap() map '%s': %d\n", |
13879 | 0 | bpf_map__name(map), err); |
13880 | 0 | return libbpf_err(err); |
13881 | 0 | } |
13882 | 0 | } |
13883 | | |
13884 | 0 | return 0; |
13885 | 0 | } |
13886 | | |
13887 | | int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) |
13888 | 0 | { |
13889 | 0 | int i, err; |
13890 | |
|
13891 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
13892 | 0 | struct bpf_program *prog = *s->progs[i].prog; |
13893 | 0 | struct bpf_link **link = s->progs[i].link; |
13894 | |
|
13895 | 0 | if (!prog->autoload || !prog->autoattach) |
13896 | 0 | continue; |
13897 | | |
13898 | | /* auto-attaching not supported for this program */ |
13899 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
13900 | 0 | continue; |
13901 | | |
13902 | | /* if user already set the link manually, don't attempt auto-attach */ |
13903 | 0 | if (*link) |
13904 | 0 | continue; |
13905 | | |
13906 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); |
13907 | 0 | if (err) { |
13908 | 0 | pr_warn("prog '%s': failed to auto-attach: %d\n", |
13909 | 0 | bpf_program__name(prog), err); |
13910 | 0 | return libbpf_err(err); |
13911 | 0 | } |
13912 | | |
13913 | | /* It's possible that for some SEC() definitions auto-attach |
13914 | | * is supported in some cases (e.g., if definition completely |
13915 | | * specifies target information), but is not in other cases. |
13916 | | * SEC("uprobe") is one such case. If user specified target |
13917 | | * binary and function name, such BPF program can be |
13918 | | * auto-attached. But if not, it shouldn't trigger skeleton's |
13919 | | * attach to fail. It should just be skipped. |
13920 | | * attach_fn signals such case with returning 0 (no error) and |
13921 | | * setting link to NULL. |
13922 | | */ |
13923 | 0 | } |
13924 | | |
13925 | 0 | return 0; |
13926 | 0 | } |
13927 | | |
13928 | | void bpf_object__detach_skeleton(struct bpf_object_skeleton *s) |
13929 | 0 | { |
13930 | 0 | int i; |
13931 | |
|
13932 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
13933 | 0 | struct bpf_link **link = s->progs[i].link; |
13934 | |
|
13935 | 0 | bpf_link__destroy(*link); |
13936 | 0 | *link = NULL; |
13937 | 0 | } |
13938 | 0 | } |
13939 | | |
13940 | | void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) |
13941 | 0 | { |
13942 | 0 | if (!s) |
13943 | 0 | return; |
13944 | | |
13945 | 0 | if (s->progs) |
13946 | 0 | bpf_object__detach_skeleton(s); |
13947 | 0 | if (s->obj) |
13948 | 0 | bpf_object__close(*s->obj); |
13949 | 0 | free(s->maps); |
13950 | 0 | free(s->progs); |
13951 | 0 | free(s); |
13952 | 0 | } |