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/ring_buffer.h> |
37 | | #include <sys/epoll.h> |
38 | | #include <sys/ioctl.h> |
39 | | #include <sys/mman.h> |
40 | | #include <sys/stat.h> |
41 | | #include <sys/types.h> |
42 | | #include <sys/vfs.h> |
43 | | #include <sys/utsname.h> |
44 | | #include <sys/resource.h> |
45 | | #include <libelf.h> |
46 | | #include <gelf.h> |
47 | | #include <zlib.h> |
48 | | |
49 | | #include "libbpf.h" |
50 | | #include "bpf.h" |
51 | | #include "btf.h" |
52 | | #include "str_error.h" |
53 | | #include "libbpf_internal.h" |
54 | | #include "hashmap.h" |
55 | | #include "bpf_gen_internal.h" |
56 | | #include "zip.h" |
57 | | |
58 | | #ifndef BPF_FS_MAGIC |
59 | 0 | #define BPF_FS_MAGIC 0xcafe4a11 |
60 | | #endif |
61 | | |
62 | 98.5k | #define BPF_INSN_SZ (sizeof(struct bpf_insn)) |
63 | | |
64 | | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
65 | | * compilation if user enables corresponding warning. Disable it explicitly. |
66 | | */ |
67 | | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
68 | | |
69 | | #define __printf(a, b) __attribute__((format(printf, a, b))) |
70 | | |
71 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj); |
72 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog); |
73 | | |
74 | | static const char * const attach_type_name[] = { |
75 | | [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress", |
76 | | [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress", |
77 | | [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create", |
78 | | [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release", |
79 | | [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops", |
80 | | [BPF_CGROUP_DEVICE] = "cgroup_device", |
81 | | [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind", |
82 | | [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind", |
83 | | [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect", |
84 | | [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect", |
85 | | [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect", |
86 | | [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind", |
87 | | [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind", |
88 | | [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername", |
89 | | [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername", |
90 | | [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername", |
91 | | [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname", |
92 | | [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname", |
93 | | [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname", |
94 | | [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg", |
95 | | [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg", |
96 | | [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg", |
97 | | [BPF_CGROUP_SYSCTL] = "cgroup_sysctl", |
98 | | [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg", |
99 | | [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg", |
100 | | [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg", |
101 | | [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt", |
102 | | [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt", |
103 | | [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser", |
104 | | [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict", |
105 | | [BPF_SK_SKB_VERDICT] = "sk_skb_verdict", |
106 | | [BPF_SK_MSG_VERDICT] = "sk_msg_verdict", |
107 | | [BPF_LIRC_MODE2] = "lirc_mode2", |
108 | | [BPF_FLOW_DISSECTOR] = "flow_dissector", |
109 | | [BPF_TRACE_RAW_TP] = "trace_raw_tp", |
110 | | [BPF_TRACE_FENTRY] = "trace_fentry", |
111 | | [BPF_TRACE_FEXIT] = "trace_fexit", |
112 | | [BPF_MODIFY_RETURN] = "modify_return", |
113 | | [BPF_LSM_MAC] = "lsm_mac", |
114 | | [BPF_LSM_CGROUP] = "lsm_cgroup", |
115 | | [BPF_SK_LOOKUP] = "sk_lookup", |
116 | | [BPF_TRACE_ITER] = "trace_iter", |
117 | | [BPF_XDP_DEVMAP] = "xdp_devmap", |
118 | | [BPF_XDP_CPUMAP] = "xdp_cpumap", |
119 | | [BPF_XDP] = "xdp", |
120 | | [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select", |
121 | | [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate", |
122 | | [BPF_PERF_EVENT] = "perf_event", |
123 | | [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi", |
124 | | [BPF_STRUCT_OPS] = "struct_ops", |
125 | | [BPF_NETFILTER] = "netfilter", |
126 | | [BPF_TCX_INGRESS] = "tcx_ingress", |
127 | | [BPF_TCX_EGRESS] = "tcx_egress", |
128 | | [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi", |
129 | | [BPF_NETKIT_PRIMARY] = "netkit_primary", |
130 | | [BPF_NETKIT_PEER] = "netkit_peer", |
131 | | }; |
132 | | |
133 | | static const char * const link_type_name[] = { |
134 | | [BPF_LINK_TYPE_UNSPEC] = "unspec", |
135 | | [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
136 | | [BPF_LINK_TYPE_TRACING] = "tracing", |
137 | | [BPF_LINK_TYPE_CGROUP] = "cgroup", |
138 | | [BPF_LINK_TYPE_ITER] = "iter", |
139 | | [BPF_LINK_TYPE_NETNS] = "netns", |
140 | | [BPF_LINK_TYPE_XDP] = "xdp", |
141 | | [BPF_LINK_TYPE_PERF_EVENT] = "perf_event", |
142 | | [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi", |
143 | | [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops", |
144 | | [BPF_LINK_TYPE_NETFILTER] = "netfilter", |
145 | | [BPF_LINK_TYPE_TCX] = "tcx", |
146 | | [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi", |
147 | | [BPF_LINK_TYPE_NETKIT] = "netkit", |
148 | | }; |
149 | | |
150 | | static const char * const map_type_name[] = { |
151 | | [BPF_MAP_TYPE_UNSPEC] = "unspec", |
152 | | [BPF_MAP_TYPE_HASH] = "hash", |
153 | | [BPF_MAP_TYPE_ARRAY] = "array", |
154 | | [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", |
155 | | [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", |
156 | | [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", |
157 | | [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", |
158 | | [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", |
159 | | [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", |
160 | | [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", |
161 | | [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", |
162 | | [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", |
163 | | [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", |
164 | | [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", |
165 | | [BPF_MAP_TYPE_DEVMAP] = "devmap", |
166 | | [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash", |
167 | | [BPF_MAP_TYPE_SOCKMAP] = "sockmap", |
168 | | [BPF_MAP_TYPE_CPUMAP] = "cpumap", |
169 | | [BPF_MAP_TYPE_XSKMAP] = "xskmap", |
170 | | [BPF_MAP_TYPE_SOCKHASH] = "sockhash", |
171 | | [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage", |
172 | | [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray", |
173 | | [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage", |
174 | | [BPF_MAP_TYPE_QUEUE] = "queue", |
175 | | [BPF_MAP_TYPE_STACK] = "stack", |
176 | | [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", |
177 | | [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", |
178 | | [BPF_MAP_TYPE_RINGBUF] = "ringbuf", |
179 | | [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", |
180 | | [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", |
181 | | [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter", |
182 | | [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf", |
183 | | [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage", |
184 | | }; |
185 | | |
186 | | static const char * const prog_type_name[] = { |
187 | | [BPF_PROG_TYPE_UNSPEC] = "unspec", |
188 | | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", |
189 | | [BPF_PROG_TYPE_KPROBE] = "kprobe", |
190 | | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", |
191 | | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", |
192 | | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", |
193 | | [BPF_PROG_TYPE_XDP] = "xdp", |
194 | | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", |
195 | | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", |
196 | | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", |
197 | | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", |
198 | | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", |
199 | | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", |
200 | | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", |
201 | | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", |
202 | | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", |
203 | | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", |
204 | | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
205 | | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", |
206 | | [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", |
207 | | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", |
208 | | [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", |
209 | | [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", |
210 | | [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", |
211 | | [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable", |
212 | | [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", |
213 | | [BPF_PROG_TYPE_TRACING] = "tracing", |
214 | | [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops", |
215 | | [BPF_PROG_TYPE_EXT] = "ext", |
216 | | [BPF_PROG_TYPE_LSM] = "lsm", |
217 | | [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", |
218 | | [BPF_PROG_TYPE_SYSCALL] = "syscall", |
219 | | [BPF_PROG_TYPE_NETFILTER] = "netfilter", |
220 | | }; |
221 | | |
222 | | static int __base_pr(enum libbpf_print_level level, const char *format, |
223 | | va_list args) |
224 | 0 | { |
225 | 0 | if (level == LIBBPF_DEBUG) |
226 | 0 | return 0; |
227 | | |
228 | 0 | return vfprintf(stderr, format, args); |
229 | 0 | } |
230 | | |
231 | | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
232 | | |
233 | | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
234 | 10.3k | { |
235 | 10.3k | libbpf_print_fn_t old_print_fn; |
236 | | |
237 | 10.3k | old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED); |
238 | | |
239 | 10.3k | return old_print_fn; |
240 | 10.3k | } |
241 | | |
242 | | __printf(2, 3) |
243 | | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
244 | 105k | { |
245 | 105k | va_list args; |
246 | 105k | int old_errno; |
247 | 105k | libbpf_print_fn_t print_fn; |
248 | | |
249 | 105k | print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED); |
250 | 105k | if (!print_fn) |
251 | 0 | return; |
252 | | |
253 | 105k | old_errno = errno; |
254 | | |
255 | 105k | va_start(args, format); |
256 | 105k | __libbpf_pr(level, format, args); |
257 | 105k | va_end(args); |
258 | | |
259 | 105k | errno = old_errno; |
260 | 105k | } |
261 | | |
262 | | static void pr_perm_msg(int err) |
263 | 0 | { |
264 | 0 | struct rlimit limit; |
265 | 0 | char buf[100]; |
266 | |
|
267 | 0 | if (err != -EPERM || geteuid() != 0) |
268 | 0 | return; |
269 | | |
270 | 0 | err = getrlimit(RLIMIT_MEMLOCK, &limit); |
271 | 0 | if (err) |
272 | 0 | return; |
273 | | |
274 | 0 | if (limit.rlim_cur == RLIM_INFINITY) |
275 | 0 | return; |
276 | | |
277 | 0 | if (limit.rlim_cur < 1024) |
278 | 0 | snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur); |
279 | 0 | else if (limit.rlim_cur < 1024*1024) |
280 | 0 | snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024); |
281 | 0 | else |
282 | 0 | snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024)); |
283 | |
|
284 | 0 | pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", |
285 | 0 | buf); |
286 | 0 | } |
287 | | |
288 | | #define STRERR_BUFSIZE 128 |
289 | | |
290 | | /* Copied from tools/perf/util/util.h */ |
291 | | #ifndef zfree |
292 | 171k | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
293 | | #endif |
294 | | |
295 | | #ifndef zclose |
296 | 34.5k | # define zclose(fd) ({ \ |
297 | 34.5k | int ___err = 0; \ |
298 | 34.5k | if ((fd) >= 0) \ |
299 | 34.5k | ___err = close((fd)); \ |
300 | 34.5k | fd = -1; \ |
301 | 34.5k | ___err; }) |
302 | | #endif |
303 | | |
304 | | static inline __u64 ptr_to_u64(const void *ptr) |
305 | 0 | { |
306 | 0 | return (__u64) (unsigned long) ptr; |
307 | 0 | } |
308 | | |
309 | | int libbpf_set_strict_mode(enum libbpf_strict_mode mode) |
310 | 0 | { |
311 | | /* as of v1.0 libbpf_set_strict_mode() is a no-op */ |
312 | 0 | return 0; |
313 | 0 | } |
314 | | |
315 | | __u32 libbpf_major_version(void) |
316 | 0 | { |
317 | 0 | return LIBBPF_MAJOR_VERSION; |
318 | 0 | } |
319 | | |
320 | | __u32 libbpf_minor_version(void) |
321 | 0 | { |
322 | 0 | return LIBBPF_MINOR_VERSION; |
323 | 0 | } |
324 | | |
325 | | const char *libbpf_version_string(void) |
326 | 0 | { |
327 | 0 | #define __S(X) #X |
328 | 0 | #define _S(X) __S(X) |
329 | 0 | return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION); |
330 | 0 | #undef _S |
331 | 0 | #undef __S |
332 | 0 | } |
333 | | |
334 | | enum reloc_type { |
335 | | RELO_LD64, |
336 | | RELO_CALL, |
337 | | RELO_DATA, |
338 | | RELO_EXTERN_LD64, |
339 | | RELO_EXTERN_CALL, |
340 | | RELO_SUBPROG_ADDR, |
341 | | RELO_CORE, |
342 | | }; |
343 | | |
344 | | struct reloc_desc { |
345 | | enum reloc_type type; |
346 | | int insn_idx; |
347 | | union { |
348 | | const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */ |
349 | | struct { |
350 | | int map_idx; |
351 | | int sym_off; |
352 | | int ext_idx; |
353 | | }; |
354 | | }; |
355 | | }; |
356 | | |
357 | | /* stored as sec_def->cookie for all libbpf-supported SEC()s */ |
358 | | enum sec_def_flags { |
359 | | SEC_NONE = 0, |
360 | | /* expected_attach_type is optional, if kernel doesn't support that */ |
361 | | SEC_EXP_ATTACH_OPT = 1, |
362 | | /* legacy, only used by libbpf_get_type_names() and |
363 | | * libbpf_attach_type_by_name(), not used by libbpf itself at all. |
364 | | * This used to be associated with cgroup (and few other) BPF programs |
365 | | * that were attachable through BPF_PROG_ATTACH command. Pretty |
366 | | * meaningless nowadays, though. |
367 | | */ |
368 | | SEC_ATTACHABLE = 2, |
369 | | SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT, |
370 | | /* attachment target is specified through BTF ID in either kernel or |
371 | | * other BPF program's BTF object |
372 | | */ |
373 | | SEC_ATTACH_BTF = 4, |
374 | | /* BPF program type allows sleeping/blocking in kernel */ |
375 | | SEC_SLEEPABLE = 8, |
376 | | /* BPF program support non-linear XDP buffer */ |
377 | | SEC_XDP_FRAGS = 16, |
378 | | /* Setup proper attach type for usdt probes. */ |
379 | | SEC_USDT = 32, |
380 | | }; |
381 | | |
382 | | struct bpf_sec_def { |
383 | | char *sec; |
384 | | enum bpf_prog_type prog_type; |
385 | | enum bpf_attach_type expected_attach_type; |
386 | | long cookie; |
387 | | int handler_id; |
388 | | |
389 | | libbpf_prog_setup_fn_t prog_setup_fn; |
390 | | libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; |
391 | | libbpf_prog_attach_fn_t prog_attach_fn; |
392 | | }; |
393 | | |
394 | | /* |
395 | | * bpf_prog should be a better name but it has been used in |
396 | | * linux/filter.h. |
397 | | */ |
398 | | struct bpf_program { |
399 | | char *name; |
400 | | char *sec_name; |
401 | | size_t sec_idx; |
402 | | const struct bpf_sec_def *sec_def; |
403 | | /* this program's instruction offset (in number of instructions) |
404 | | * within its containing ELF section |
405 | | */ |
406 | | size_t sec_insn_off; |
407 | | /* number of original instructions in ELF section belonging to this |
408 | | * program, not taking into account subprogram instructions possible |
409 | | * appended later during relocation |
410 | | */ |
411 | | size_t sec_insn_cnt; |
412 | | /* Offset (in number of instructions) of the start of instruction |
413 | | * belonging to this BPF program within its containing main BPF |
414 | | * program. For the entry-point (main) BPF program, this is always |
415 | | * zero. For a sub-program, this gets reset before each of main BPF |
416 | | * programs are processed and relocated and is used to determined |
417 | | * whether sub-program was already appended to the main program, and |
418 | | * if yes, at which instruction offset. |
419 | | */ |
420 | | size_t sub_insn_off; |
421 | | |
422 | | /* instructions that belong to BPF program; insns[0] is located at |
423 | | * sec_insn_off instruction within its ELF section in ELF file, so |
424 | | * when mapping ELF file instruction index to the local instruction, |
425 | | * one needs to subtract sec_insn_off; and vice versa. |
426 | | */ |
427 | | struct bpf_insn *insns; |
428 | | /* actual number of instruction in this BPF program's image; for |
429 | | * entry-point BPF programs this includes the size of main program |
430 | | * itself plus all the used sub-programs, appended at the end |
431 | | */ |
432 | | size_t insns_cnt; |
433 | | |
434 | | struct reloc_desc *reloc_desc; |
435 | | int nr_reloc; |
436 | | |
437 | | /* BPF verifier log settings */ |
438 | | char *log_buf; |
439 | | size_t log_size; |
440 | | __u32 log_level; |
441 | | |
442 | | struct bpf_object *obj; |
443 | | |
444 | | int fd; |
445 | | bool autoload; |
446 | | bool autoattach; |
447 | | bool sym_global; |
448 | | bool mark_btf_static; |
449 | | enum bpf_prog_type type; |
450 | | enum bpf_attach_type expected_attach_type; |
451 | | int exception_cb_idx; |
452 | | |
453 | | int prog_ifindex; |
454 | | __u32 attach_btf_obj_fd; |
455 | | __u32 attach_btf_id; |
456 | | __u32 attach_prog_fd; |
457 | | |
458 | | void *func_info; |
459 | | __u32 func_info_rec_size; |
460 | | __u32 func_info_cnt; |
461 | | |
462 | | void *line_info; |
463 | | __u32 line_info_rec_size; |
464 | | __u32 line_info_cnt; |
465 | | __u32 prog_flags; |
466 | | }; |
467 | | |
468 | | struct bpf_struct_ops { |
469 | | const char *tname; |
470 | | const struct btf_type *type; |
471 | | struct bpf_program **progs; |
472 | | __u32 *kern_func_off; |
473 | | /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ |
474 | | void *data; |
475 | | /* e.g. struct bpf_struct_ops_tcp_congestion_ops in |
476 | | * btf_vmlinux's format. |
477 | | * struct bpf_struct_ops_tcp_congestion_ops { |
478 | | * [... some other kernel fields ...] |
479 | | * struct tcp_congestion_ops data; |
480 | | * } |
481 | | * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops) |
482 | | * bpf_map__init_kern_struct_ops() will populate the "kern_vdata" |
483 | | * from "data". |
484 | | */ |
485 | | void *kern_vdata; |
486 | | __u32 type_id; |
487 | | }; |
488 | | |
489 | 1.45k | #define DATA_SEC ".data" |
490 | 1.33k | #define BSS_SEC ".bss" |
491 | 940 | #define RODATA_SEC ".rodata" |
492 | 5.04k | #define KCONFIG_SEC ".kconfig" |
493 | 6.41k | #define KSYMS_SEC ".ksyms" |
494 | 3.56k | #define STRUCT_OPS_SEC ".struct_ops" |
495 | 3.37k | #define STRUCT_OPS_LINK_SEC ".struct_ops.link" |
496 | | |
497 | | enum libbpf_map_type { |
498 | | LIBBPF_MAP_UNSPEC, |
499 | | LIBBPF_MAP_DATA, |
500 | | LIBBPF_MAP_BSS, |
501 | | LIBBPF_MAP_RODATA, |
502 | | LIBBPF_MAP_KCONFIG, |
503 | | }; |
504 | | |
505 | | struct bpf_map_def { |
506 | | unsigned int type; |
507 | | unsigned int key_size; |
508 | | unsigned int value_size; |
509 | | unsigned int max_entries; |
510 | | unsigned int map_flags; |
511 | | }; |
512 | | |
513 | | struct bpf_map { |
514 | | struct bpf_object *obj; |
515 | | char *name; |
516 | | /* real_name is defined for special internal maps (.rodata*, |
517 | | * .data*, .bss, .kconfig) and preserves their original ELF section |
518 | | * name. This is important to be able to find corresponding BTF |
519 | | * DATASEC information. |
520 | | */ |
521 | | char *real_name; |
522 | | int fd; |
523 | | int sec_idx; |
524 | | size_t sec_offset; |
525 | | int map_ifindex; |
526 | | int inner_map_fd; |
527 | | struct bpf_map_def def; |
528 | | __u32 numa_node; |
529 | | __u32 btf_var_idx; |
530 | | __u32 btf_key_type_id; |
531 | | __u32 btf_value_type_id; |
532 | | __u32 btf_vmlinux_value_type_id; |
533 | | enum libbpf_map_type libbpf_type; |
534 | | void *mmaped; |
535 | | struct bpf_struct_ops *st_ops; |
536 | | struct bpf_map *inner_map; |
537 | | void **init_slots; |
538 | | int init_slots_sz; |
539 | | char *pin_path; |
540 | | bool pinned; |
541 | | bool reused; |
542 | | bool autocreate; |
543 | | __u64 map_extra; |
544 | | }; |
545 | | |
546 | | enum extern_type { |
547 | | EXT_UNKNOWN, |
548 | | EXT_KCFG, |
549 | | EXT_KSYM, |
550 | | }; |
551 | | |
552 | | enum kcfg_type { |
553 | | KCFG_UNKNOWN, |
554 | | KCFG_CHAR, |
555 | | KCFG_BOOL, |
556 | | KCFG_INT, |
557 | | KCFG_TRISTATE, |
558 | | KCFG_CHAR_ARR, |
559 | | }; |
560 | | |
561 | | struct extern_desc { |
562 | | enum extern_type type; |
563 | | int sym_idx; |
564 | | int btf_id; |
565 | | int sec_btf_id; |
566 | | const char *name; |
567 | | char *essent_name; |
568 | | bool is_set; |
569 | | bool is_weak; |
570 | | union { |
571 | | struct { |
572 | | enum kcfg_type type; |
573 | | int sz; |
574 | | int align; |
575 | | int data_off; |
576 | | bool is_signed; |
577 | | } kcfg; |
578 | | struct { |
579 | | unsigned long long addr; |
580 | | |
581 | | /* target btf_id of the corresponding kernel var. */ |
582 | | int kernel_btf_obj_fd; |
583 | | int kernel_btf_id; |
584 | | |
585 | | /* local btf_id of the ksym extern's type. */ |
586 | | __u32 type_id; |
587 | | /* BTF fd index to be patched in for insn->off, this is |
588 | | * 0 for vmlinux BTF, index in obj->fd_array for module |
589 | | * BTF |
590 | | */ |
591 | | __s16 btf_fd_idx; |
592 | | } ksym; |
593 | | }; |
594 | | }; |
595 | | |
596 | | struct module_btf { |
597 | | struct btf *btf; |
598 | | char *name; |
599 | | __u32 id; |
600 | | int fd; |
601 | | int fd_array_idx; |
602 | | }; |
603 | | |
604 | | enum sec_type { |
605 | | SEC_UNUSED = 0, |
606 | | SEC_RELO, |
607 | | SEC_BSS, |
608 | | SEC_DATA, |
609 | | SEC_RODATA, |
610 | | }; |
611 | | |
612 | | struct elf_sec_desc { |
613 | | enum sec_type sec_type; |
614 | | Elf64_Shdr *shdr; |
615 | | Elf_Data *data; |
616 | | }; |
617 | | |
618 | | struct elf_state { |
619 | | int fd; |
620 | | const void *obj_buf; |
621 | | size_t obj_buf_sz; |
622 | | Elf *elf; |
623 | | Elf64_Ehdr *ehdr; |
624 | | Elf_Data *symbols; |
625 | | Elf_Data *st_ops_data; |
626 | | Elf_Data *st_ops_link_data; |
627 | | size_t shstrndx; /* section index for section name strings */ |
628 | | size_t strtabidx; |
629 | | struct elf_sec_desc *secs; |
630 | | size_t sec_cnt; |
631 | | int btf_maps_shndx; |
632 | | __u32 btf_maps_sec_btf_id; |
633 | | int text_shndx; |
634 | | int symbols_shndx; |
635 | | int st_ops_shndx; |
636 | | int st_ops_link_shndx; |
637 | | }; |
638 | | |
639 | | struct usdt_manager; |
640 | | |
641 | | struct bpf_object { |
642 | | char name[BPF_OBJ_NAME_LEN]; |
643 | | char license[64]; |
644 | | __u32 kern_version; |
645 | | |
646 | | struct bpf_program *programs; |
647 | | size_t nr_programs; |
648 | | struct bpf_map *maps; |
649 | | size_t nr_maps; |
650 | | size_t maps_cap; |
651 | | |
652 | | char *kconfig; |
653 | | struct extern_desc *externs; |
654 | | int nr_extern; |
655 | | int kconfig_map_idx; |
656 | | |
657 | | bool loaded; |
658 | | bool has_subcalls; |
659 | | bool has_rodata; |
660 | | |
661 | | struct bpf_gen *gen_loader; |
662 | | |
663 | | /* Information when doing ELF related work. Only valid if efile.elf is not NULL */ |
664 | | struct elf_state efile; |
665 | | |
666 | | struct btf *btf; |
667 | | struct btf_ext *btf_ext; |
668 | | |
669 | | /* Parse and load BTF vmlinux if any of the programs in the object need |
670 | | * it at load time. |
671 | | */ |
672 | | struct btf *btf_vmlinux; |
673 | | /* Path to the custom BTF to be used for BPF CO-RE relocations as an |
674 | | * override for vmlinux BTF. |
675 | | */ |
676 | | char *btf_custom_path; |
677 | | /* vmlinux BTF override for CO-RE relocations */ |
678 | | struct btf *btf_vmlinux_override; |
679 | | /* Lazily initialized kernel module BTFs */ |
680 | | struct module_btf *btf_modules; |
681 | | bool btf_modules_loaded; |
682 | | size_t btf_module_cnt; |
683 | | size_t btf_module_cap; |
684 | | |
685 | | /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */ |
686 | | char *log_buf; |
687 | | size_t log_size; |
688 | | __u32 log_level; |
689 | | |
690 | | int *fd_array; |
691 | | size_t fd_array_cap; |
692 | | size_t fd_array_cnt; |
693 | | |
694 | | struct usdt_manager *usdt_man; |
695 | | |
696 | | char path[]; |
697 | | }; |
698 | | |
699 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off); |
700 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off); |
701 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx); |
702 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name); |
703 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn); |
704 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn); |
705 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn); |
706 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx); |
707 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx); |
708 | | |
709 | | void bpf_program__unload(struct bpf_program *prog) |
710 | 22.4k | { |
711 | 22.4k | if (!prog) |
712 | 0 | return; |
713 | | |
714 | 22.4k | zclose(prog->fd); |
715 | | |
716 | 22.4k | zfree(&prog->func_info); |
717 | 22.4k | zfree(&prog->line_info); |
718 | 22.4k | } |
719 | | |
720 | | static void bpf_program__exit(struct bpf_program *prog) |
721 | 11.2k | { |
722 | 11.2k | if (!prog) |
723 | 0 | return; |
724 | | |
725 | 11.2k | bpf_program__unload(prog); |
726 | 11.2k | zfree(&prog->name); |
727 | 11.2k | zfree(&prog->sec_name); |
728 | 11.2k | zfree(&prog->insns); |
729 | 11.2k | zfree(&prog->reloc_desc); |
730 | | |
731 | 11.2k | prog->nr_reloc = 0; |
732 | 11.2k | prog->insns_cnt = 0; |
733 | 11.2k | prog->sec_idx = -1; |
734 | 11.2k | } |
735 | | |
736 | | static bool insn_is_subprog_call(const struct bpf_insn *insn) |
737 | 0 | { |
738 | 0 | return BPF_CLASS(insn->code) == BPF_JMP && |
739 | 0 | BPF_OP(insn->code) == BPF_CALL && |
740 | 0 | BPF_SRC(insn->code) == BPF_K && |
741 | 0 | insn->src_reg == BPF_PSEUDO_CALL && |
742 | 0 | insn->dst_reg == 0 && |
743 | 0 | insn->off == 0; |
744 | 0 | } |
745 | | |
746 | | static bool is_call_insn(const struct bpf_insn *insn) |
747 | 1.90k | { |
748 | 1.90k | return insn->code == (BPF_JMP | BPF_CALL); |
749 | 1.90k | } |
750 | | |
751 | | static bool insn_is_pseudo_func(struct bpf_insn *insn) |
752 | 0 | { |
753 | 0 | return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; |
754 | 0 | } |
755 | | |
756 | | static int |
757 | | bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, |
758 | | const char *name, size_t sec_idx, const char *sec_name, |
759 | | size_t sec_off, void *insn_data, size_t insn_data_sz) |
760 | 11.2k | { |
761 | 11.2k | if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) { |
762 | 59 | pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n", |
763 | 59 | sec_name, name, sec_off, insn_data_sz); |
764 | 59 | return -EINVAL; |
765 | 59 | } |
766 | | |
767 | 11.2k | memset(prog, 0, sizeof(*prog)); |
768 | 11.2k | prog->obj = obj; |
769 | | |
770 | 11.2k | prog->sec_idx = sec_idx; |
771 | 11.2k | prog->sec_insn_off = sec_off / BPF_INSN_SZ; |
772 | 11.2k | prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; |
773 | | /* insns_cnt can later be increased by appending used subprograms */ |
774 | 11.2k | prog->insns_cnt = prog->sec_insn_cnt; |
775 | | |
776 | 11.2k | prog->type = BPF_PROG_TYPE_UNSPEC; |
777 | 11.2k | prog->fd = -1; |
778 | 11.2k | prog->exception_cb_idx = -1; |
779 | | |
780 | | /* libbpf's convention for SEC("?abc...") is that it's just like |
781 | | * SEC("abc...") but the corresponding bpf_program starts out with |
782 | | * autoload set to false. |
783 | | */ |
784 | 11.2k | if (sec_name[0] == '?') { |
785 | 222 | prog->autoload = false; |
786 | | /* from now on forget there was ? in section name */ |
787 | 222 | sec_name++; |
788 | 11.0k | } else { |
789 | 11.0k | prog->autoload = true; |
790 | 11.0k | } |
791 | | |
792 | 11.2k | prog->autoattach = true; |
793 | | |
794 | | /* inherit object's log_level */ |
795 | 11.2k | prog->log_level = obj->log_level; |
796 | | |
797 | 11.2k | prog->sec_name = strdup(sec_name); |
798 | 11.2k | if (!prog->sec_name) |
799 | 0 | goto errout; |
800 | | |
801 | 11.2k | prog->name = strdup(name); |
802 | 11.2k | if (!prog->name) |
803 | 0 | goto errout; |
804 | | |
805 | 11.2k | prog->insns = malloc(insn_data_sz); |
806 | 11.2k | if (!prog->insns) |
807 | 17 | goto errout; |
808 | 11.2k | memcpy(prog->insns, insn_data, insn_data_sz); |
809 | | |
810 | 11.2k | return 0; |
811 | 17 | errout: |
812 | 17 | pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name); |
813 | 17 | bpf_program__exit(prog); |
814 | 17 | return -ENOMEM; |
815 | 11.2k | } |
816 | | |
817 | | static int |
818 | | bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data, |
819 | | const char *sec_name, int sec_idx) |
820 | 1.20k | { |
821 | 1.20k | Elf_Data *symbols = obj->efile.symbols; |
822 | 1.20k | struct bpf_program *prog, *progs; |
823 | 1.20k | void *data = sec_data->d_buf; |
824 | 1.20k | size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; |
825 | 1.20k | int nr_progs, err, i; |
826 | 1.20k | const char *name; |
827 | 1.20k | Elf64_Sym *sym; |
828 | | |
829 | 1.20k | progs = obj->programs; |
830 | 1.20k | nr_progs = obj->nr_programs; |
831 | 1.20k | nr_syms = symbols->d_size / sizeof(Elf64_Sym); |
832 | | |
833 | 257k | for (i = 0; i < nr_syms; i++) { |
834 | 256k | sym = elf_sym_by_idx(obj, i); |
835 | | |
836 | 256k | if (sym->st_shndx != sec_idx) |
837 | 243k | continue; |
838 | 13.2k | if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) |
839 | 1.72k | continue; |
840 | | |
841 | 11.4k | prog_sz = sym->st_size; |
842 | 11.4k | sec_off = sym->st_value; |
843 | | |
844 | 11.4k | name = elf_sym_str(obj, sym->st_name); |
845 | 11.4k | if (!name) { |
846 | 68 | pr_warn("sec '%s': failed to get symbol name for offset %zu\n", |
847 | 68 | sec_name, sec_off); |
848 | 68 | return -LIBBPF_ERRNO__FORMAT; |
849 | 68 | } |
850 | | |
851 | 11.4k | if (sec_off + prog_sz > sec_sz) { |
852 | 121 | pr_warn("sec '%s': program at offset %zu crosses section boundary\n", |
853 | 121 | sec_name, sec_off); |
854 | 121 | return -LIBBPF_ERRNO__FORMAT; |
855 | 121 | } |
856 | | |
857 | 11.3k | if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { |
858 | 6 | pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name); |
859 | 6 | return -ENOTSUP; |
860 | 6 | } |
861 | | |
862 | 11.2k | pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n", |
863 | 22.5k | sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz); |
864 | | |
865 | 11.2k | progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs)); |
866 | 11.2k | if (!progs) { |
867 | | /* |
868 | | * In this case the original obj->programs |
869 | | * is still valid, so don't need special treat for |
870 | | * bpf_close_object(). |
871 | | */ |
872 | 0 | pr_warn("sec '%s': failed to alloc memory for new program '%s'\n", |
873 | 0 | sec_name, name); |
874 | 0 | return -ENOMEM; |
875 | 0 | } |
876 | 11.2k | obj->programs = progs; |
877 | | |
878 | 11.2k | prog = &progs[nr_progs]; |
879 | | |
880 | 11.2k | err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name, |
881 | 11.2k | sec_off, data + sec_off, prog_sz); |
882 | 11.2k | if (err) |
883 | 76 | return err; |
884 | | |
885 | 11.2k | if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) |
886 | 10.9k | prog->sym_global = true; |
887 | | |
888 | | /* if function is a global/weak symbol, but has restricted |
889 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC |
890 | | * as static to enable more permissive BPF verification mode |
891 | | * with more outside context available to BPF verifier |
892 | | */ |
893 | 11.2k | if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
894 | 10.9k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) |
895 | 7.93k | prog->mark_btf_static = true; |
896 | | |
897 | 11.2k | nr_progs++; |
898 | 11.2k | obj->nr_programs = nr_progs; |
899 | 11.2k | } |
900 | | |
901 | 929 | return 0; |
902 | 1.20k | } |
903 | | |
904 | | static const struct btf_member * |
905 | | find_member_by_offset(const struct btf_type *t, __u32 bit_offset) |
906 | 0 | { |
907 | 0 | struct btf_member *m; |
908 | 0 | int i; |
909 | |
|
910 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
911 | 0 | if (btf_member_bit_offset(t, i) == bit_offset) |
912 | 0 | return m; |
913 | 0 | } |
914 | | |
915 | 0 | return NULL; |
916 | 0 | } |
917 | | |
918 | | static const struct btf_member * |
919 | | find_member_by_name(const struct btf *btf, const struct btf_type *t, |
920 | | const char *name) |
921 | 0 | { |
922 | 0 | struct btf_member *m; |
923 | 0 | int i; |
924 | |
|
925 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
926 | 0 | if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) |
927 | 0 | return m; |
928 | 0 | } |
929 | | |
930 | 0 | return NULL; |
931 | 0 | } |
932 | | |
933 | 0 | #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" |
934 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
935 | | const char *name, __u32 kind); |
936 | | |
937 | | static int |
938 | | find_struct_ops_kern_types(const struct btf *btf, const char *tname, |
939 | | const struct btf_type **type, __u32 *type_id, |
940 | | const struct btf_type **vtype, __u32 *vtype_id, |
941 | | const struct btf_member **data_member) |
942 | 0 | { |
943 | 0 | const struct btf_type *kern_type, *kern_vtype; |
944 | 0 | const struct btf_member *kern_data_member; |
945 | 0 | __s32 kern_vtype_id, kern_type_id; |
946 | 0 | __u32 i; |
947 | |
|
948 | 0 | kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT); |
949 | 0 | if (kern_type_id < 0) { |
950 | 0 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", |
951 | 0 | tname); |
952 | 0 | return kern_type_id; |
953 | 0 | } |
954 | 0 | kern_type = btf__type_by_id(btf, kern_type_id); |
955 | | |
956 | | /* Find the corresponding "map_value" type that will be used |
957 | | * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example, |
958 | | * find "struct bpf_struct_ops_tcp_congestion_ops" from the |
959 | | * btf_vmlinux. |
960 | | */ |
961 | 0 | kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX, |
962 | 0 | tname, BTF_KIND_STRUCT); |
963 | 0 | if (kern_vtype_id < 0) { |
964 | 0 | pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n", |
965 | 0 | STRUCT_OPS_VALUE_PREFIX, tname); |
966 | 0 | return kern_vtype_id; |
967 | 0 | } |
968 | 0 | kern_vtype = btf__type_by_id(btf, kern_vtype_id); |
969 | | |
970 | | /* Find "struct tcp_congestion_ops" from |
971 | | * struct bpf_struct_ops_tcp_congestion_ops { |
972 | | * [ ... ] |
973 | | * struct tcp_congestion_ops data; |
974 | | * } |
975 | | */ |
976 | 0 | kern_data_member = btf_members(kern_vtype); |
977 | 0 | for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) { |
978 | 0 | if (kern_data_member->type == kern_type_id) |
979 | 0 | break; |
980 | 0 | } |
981 | 0 | if (i == btf_vlen(kern_vtype)) { |
982 | 0 | pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n", |
983 | 0 | tname, STRUCT_OPS_VALUE_PREFIX, tname); |
984 | 0 | return -EINVAL; |
985 | 0 | } |
986 | | |
987 | 0 | *type = kern_type; |
988 | 0 | *type_id = kern_type_id; |
989 | 0 | *vtype = kern_vtype; |
990 | 0 | *vtype_id = kern_vtype_id; |
991 | 0 | *data_member = kern_data_member; |
992 | |
|
993 | 0 | return 0; |
994 | 0 | } |
995 | | |
996 | | static bool bpf_map__is_struct_ops(const struct bpf_map *map) |
997 | 377 | { |
998 | 377 | return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; |
999 | 377 | } |
1000 | | |
1001 | | /* Init the map's fields that depend on kern_btf */ |
1002 | | static int bpf_map__init_kern_struct_ops(struct bpf_map *map, |
1003 | | const struct btf *btf, |
1004 | | const struct btf *kern_btf) |
1005 | 0 | { |
1006 | 0 | const struct btf_member *member, *kern_member, *kern_data_member; |
1007 | 0 | const struct btf_type *type, *kern_type, *kern_vtype; |
1008 | 0 | __u32 i, kern_type_id, kern_vtype_id, kern_data_off; |
1009 | 0 | struct bpf_struct_ops *st_ops; |
1010 | 0 | void *data, *kern_data; |
1011 | 0 | const char *tname; |
1012 | 0 | int err; |
1013 | |
|
1014 | 0 | st_ops = map->st_ops; |
1015 | 0 | type = st_ops->type; |
1016 | 0 | tname = st_ops->tname; |
1017 | 0 | err = find_struct_ops_kern_types(kern_btf, tname, |
1018 | 0 | &kern_type, &kern_type_id, |
1019 | 0 | &kern_vtype, &kern_vtype_id, |
1020 | 0 | &kern_data_member); |
1021 | 0 | if (err) |
1022 | 0 | return err; |
1023 | | |
1024 | 0 | pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n", |
1025 | 0 | map->name, st_ops->type_id, kern_type_id, kern_vtype_id); |
1026 | |
|
1027 | 0 | map->def.value_size = kern_vtype->size; |
1028 | 0 | map->btf_vmlinux_value_type_id = kern_vtype_id; |
1029 | |
|
1030 | 0 | st_ops->kern_vdata = calloc(1, kern_vtype->size); |
1031 | 0 | if (!st_ops->kern_vdata) |
1032 | 0 | return -ENOMEM; |
1033 | | |
1034 | 0 | data = st_ops->data; |
1035 | 0 | kern_data_off = kern_data_member->offset / 8; |
1036 | 0 | kern_data = st_ops->kern_vdata + kern_data_off; |
1037 | |
|
1038 | 0 | member = btf_members(type); |
1039 | 0 | for (i = 0; i < btf_vlen(type); i++, member++) { |
1040 | 0 | const struct btf_type *mtype, *kern_mtype; |
1041 | 0 | __u32 mtype_id, kern_mtype_id; |
1042 | 0 | void *mdata, *kern_mdata; |
1043 | 0 | __s64 msize, kern_msize; |
1044 | 0 | __u32 moff, kern_moff; |
1045 | 0 | __u32 kern_member_idx; |
1046 | 0 | const char *mname; |
1047 | |
|
1048 | 0 | mname = btf__name_by_offset(btf, member->name_off); |
1049 | 0 | kern_member = find_member_by_name(kern_btf, kern_type, mname); |
1050 | 0 | if (!kern_member) { |
1051 | 0 | pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", |
1052 | 0 | map->name, mname); |
1053 | 0 | return -ENOTSUP; |
1054 | 0 | } |
1055 | | |
1056 | 0 | kern_member_idx = kern_member - btf_members(kern_type); |
1057 | 0 | if (btf_member_bitfield_size(type, i) || |
1058 | 0 | btf_member_bitfield_size(kern_type, kern_member_idx)) { |
1059 | 0 | pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n", |
1060 | 0 | map->name, mname); |
1061 | 0 | return -ENOTSUP; |
1062 | 0 | } |
1063 | | |
1064 | 0 | moff = member->offset / 8; |
1065 | 0 | kern_moff = kern_member->offset / 8; |
1066 | |
|
1067 | 0 | mdata = data + moff; |
1068 | 0 | kern_mdata = kern_data + kern_moff; |
1069 | |
|
1070 | 0 | mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); |
1071 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, |
1072 | 0 | &kern_mtype_id); |
1073 | 0 | if (BTF_INFO_KIND(mtype->info) != |
1074 | 0 | BTF_INFO_KIND(kern_mtype->info)) { |
1075 | 0 | pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n", |
1076 | 0 | map->name, mname, BTF_INFO_KIND(mtype->info), |
1077 | 0 | BTF_INFO_KIND(kern_mtype->info)); |
1078 | 0 | return -ENOTSUP; |
1079 | 0 | } |
1080 | | |
1081 | 0 | if (btf_is_ptr(mtype)) { |
1082 | 0 | struct bpf_program *prog; |
1083 | |
|
1084 | 0 | prog = st_ops->progs[i]; |
1085 | 0 | if (!prog) |
1086 | 0 | continue; |
1087 | | |
1088 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, |
1089 | 0 | kern_mtype->type, |
1090 | 0 | &kern_mtype_id); |
1091 | | |
1092 | | /* mtype->type must be a func_proto which was |
1093 | | * guaranteed in bpf_object__collect_st_ops_relos(), |
1094 | | * so only check kern_mtype for func_proto here. |
1095 | | */ |
1096 | 0 | if (!btf_is_func_proto(kern_mtype)) { |
1097 | 0 | pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n", |
1098 | 0 | map->name, mname); |
1099 | 0 | return -ENOTSUP; |
1100 | 0 | } |
1101 | | |
1102 | 0 | prog->attach_btf_id = kern_type_id; |
1103 | 0 | prog->expected_attach_type = kern_member_idx; |
1104 | |
|
1105 | 0 | st_ops->kern_func_off[i] = kern_data_off + kern_moff; |
1106 | |
|
1107 | 0 | pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n", |
1108 | 0 | map->name, mname, prog->name, moff, |
1109 | 0 | kern_moff); |
1110 | |
|
1111 | 0 | continue; |
1112 | 0 | } |
1113 | | |
1114 | 0 | msize = btf__resolve_size(btf, mtype_id); |
1115 | 0 | kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); |
1116 | 0 | if (msize < 0 || kern_msize < 0 || msize != kern_msize) { |
1117 | 0 | pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", |
1118 | 0 | map->name, mname, (ssize_t)msize, |
1119 | 0 | (ssize_t)kern_msize); |
1120 | 0 | return -ENOTSUP; |
1121 | 0 | } |
1122 | | |
1123 | 0 | pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n", |
1124 | 0 | map->name, mname, (unsigned int)msize, |
1125 | 0 | moff, kern_moff); |
1126 | 0 | memcpy(kern_mdata, mdata, msize); |
1127 | 0 | } |
1128 | | |
1129 | 0 | return 0; |
1130 | 0 | } |
1131 | | |
1132 | | static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) |
1133 | 0 | { |
1134 | 0 | struct bpf_map *map; |
1135 | 0 | size_t i; |
1136 | 0 | int err; |
1137 | |
|
1138 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
1139 | 0 | map = &obj->maps[i]; |
1140 | |
|
1141 | 0 | if (!bpf_map__is_struct_ops(map)) |
1142 | 0 | continue; |
1143 | | |
1144 | 0 | err = bpf_map__init_kern_struct_ops(map, obj->btf, |
1145 | 0 | obj->btf_vmlinux); |
1146 | 0 | if (err) |
1147 | 0 | return err; |
1148 | 0 | } |
1149 | | |
1150 | 0 | return 0; |
1151 | 0 | } |
1152 | | |
1153 | | static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, |
1154 | | int shndx, Elf_Data *data, __u32 map_flags) |
1155 | 4.04k | { |
1156 | 4.04k | const struct btf_type *type, *datasec; |
1157 | 4.04k | const struct btf_var_secinfo *vsi; |
1158 | 4.04k | struct bpf_struct_ops *st_ops; |
1159 | 4.04k | const char *tname, *var_name; |
1160 | 4.04k | __s32 type_id, datasec_id; |
1161 | 4.04k | const struct btf *btf; |
1162 | 4.04k | struct bpf_map *map; |
1163 | 4.04k | __u32 i; |
1164 | | |
1165 | 4.04k | if (shndx == -1) |
1166 | 3.96k | return 0; |
1167 | | |
1168 | 80 | btf = obj->btf; |
1169 | 80 | datasec_id = btf__find_by_name_kind(btf, sec_name, |
1170 | 80 | BTF_KIND_DATASEC); |
1171 | 80 | if (datasec_id < 0) { |
1172 | 76 | pr_warn("struct_ops init: DATASEC %s not found\n", |
1173 | 76 | sec_name); |
1174 | 76 | return -EINVAL; |
1175 | 76 | } |
1176 | | |
1177 | 4 | datasec = btf__type_by_id(btf, datasec_id); |
1178 | 4 | vsi = btf_var_secinfos(datasec); |
1179 | 4 | for (i = 0; i < btf_vlen(datasec); i++, vsi++) { |
1180 | 0 | type = btf__type_by_id(obj->btf, vsi->type); |
1181 | 0 | var_name = btf__name_by_offset(obj->btf, type->name_off); |
1182 | |
|
1183 | 0 | type_id = btf__resolve_type(obj->btf, vsi->type); |
1184 | 0 | if (type_id < 0) { |
1185 | 0 | pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n", |
1186 | 0 | vsi->type, sec_name); |
1187 | 0 | return -EINVAL; |
1188 | 0 | } |
1189 | | |
1190 | 0 | type = btf__type_by_id(obj->btf, type_id); |
1191 | 0 | tname = btf__name_by_offset(obj->btf, type->name_off); |
1192 | 0 | if (!tname[0]) { |
1193 | 0 | pr_warn("struct_ops init: anonymous type is not supported\n"); |
1194 | 0 | return -ENOTSUP; |
1195 | 0 | } |
1196 | 0 | if (!btf_is_struct(type)) { |
1197 | 0 | pr_warn("struct_ops init: %s is not a struct\n", tname); |
1198 | 0 | return -EINVAL; |
1199 | 0 | } |
1200 | | |
1201 | 0 | map = bpf_object__add_map(obj); |
1202 | 0 | if (IS_ERR(map)) |
1203 | 0 | return PTR_ERR(map); |
1204 | | |
1205 | 0 | map->sec_idx = shndx; |
1206 | 0 | map->sec_offset = vsi->offset; |
1207 | 0 | map->name = strdup(var_name); |
1208 | 0 | if (!map->name) |
1209 | 0 | return -ENOMEM; |
1210 | | |
1211 | 0 | map->def.type = BPF_MAP_TYPE_STRUCT_OPS; |
1212 | 0 | map->def.key_size = sizeof(int); |
1213 | 0 | map->def.value_size = type->size; |
1214 | 0 | map->def.max_entries = 1; |
1215 | 0 | map->def.map_flags = map_flags; |
1216 | |
|
1217 | 0 | map->st_ops = calloc(1, sizeof(*map->st_ops)); |
1218 | 0 | if (!map->st_ops) |
1219 | 0 | return -ENOMEM; |
1220 | 0 | st_ops = map->st_ops; |
1221 | 0 | st_ops->data = malloc(type->size); |
1222 | 0 | st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); |
1223 | 0 | st_ops->kern_func_off = malloc(btf_vlen(type) * |
1224 | 0 | sizeof(*st_ops->kern_func_off)); |
1225 | 0 | if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) |
1226 | 0 | return -ENOMEM; |
1227 | | |
1228 | 0 | if (vsi->offset + type->size > data->d_size) { |
1229 | 0 | pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n", |
1230 | 0 | var_name, sec_name); |
1231 | 0 | return -EINVAL; |
1232 | 0 | } |
1233 | | |
1234 | 0 | memcpy(st_ops->data, |
1235 | 0 | data->d_buf + vsi->offset, |
1236 | 0 | type->size); |
1237 | 0 | st_ops->tname = tname; |
1238 | 0 | st_ops->type = type; |
1239 | 0 | st_ops->type_id = type_id; |
1240 | |
|
1241 | 0 | pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n", |
1242 | 0 | tname, type_id, var_name, vsi->offset); |
1243 | 0 | } |
1244 | | |
1245 | 4 | return 0; |
1246 | 4 | } |
1247 | | |
1248 | | static int bpf_object_init_struct_ops(struct bpf_object *obj) |
1249 | 2.03k | { |
1250 | 2.03k | int err; |
1251 | | |
1252 | 2.03k | err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx, |
1253 | 2.03k | obj->efile.st_ops_data, 0); |
1254 | 2.03k | err = err ?: init_struct_ops_maps(obj, STRUCT_OPS_LINK_SEC, |
1255 | 2.01k | obj->efile.st_ops_link_shndx, |
1256 | 2.01k | obj->efile.st_ops_link_data, |
1257 | 2.01k | BPF_F_LINK); |
1258 | 2.03k | return err; |
1259 | 2.03k | } |
1260 | | |
1261 | | static struct bpf_object *bpf_object__new(const char *path, |
1262 | | const void *obj_buf, |
1263 | | size_t obj_buf_sz, |
1264 | | const char *obj_name) |
1265 | 10.3k | { |
1266 | 10.3k | struct bpf_object *obj; |
1267 | 10.3k | char *end; |
1268 | | |
1269 | 10.3k | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
1270 | 10.3k | if (!obj) { |
1271 | 0 | pr_warn("alloc memory failed for %s\n", path); |
1272 | 0 | return ERR_PTR(-ENOMEM); |
1273 | 0 | } |
1274 | | |
1275 | 10.3k | strcpy(obj->path, path); |
1276 | 10.3k | if (obj_name) { |
1277 | 10.3k | libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); |
1278 | 10.3k | } else { |
1279 | | /* Using basename() GNU version which doesn't modify arg. */ |
1280 | 0 | libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); |
1281 | 0 | end = strchr(obj->name, '.'); |
1282 | 0 | if (end) |
1283 | 0 | *end = 0; |
1284 | 0 | } |
1285 | | |
1286 | 10.3k | obj->efile.fd = -1; |
1287 | | /* |
1288 | | * Caller of this function should also call |
1289 | | * bpf_object__elf_finish() after data collection to return |
1290 | | * obj_buf to user. If not, we should duplicate the buffer to |
1291 | | * avoid user freeing them before elf finish. |
1292 | | */ |
1293 | 10.3k | obj->efile.obj_buf = obj_buf; |
1294 | 10.3k | obj->efile.obj_buf_sz = obj_buf_sz; |
1295 | 10.3k | obj->efile.btf_maps_shndx = -1; |
1296 | 10.3k | obj->efile.st_ops_shndx = -1; |
1297 | 10.3k | obj->efile.st_ops_link_shndx = -1; |
1298 | 10.3k | obj->kconfig_map_idx = -1; |
1299 | | |
1300 | 10.3k | obj->kern_version = get_kernel_version(); |
1301 | 10.3k | obj->loaded = false; |
1302 | | |
1303 | 10.3k | return obj; |
1304 | 10.3k | } |
1305 | | |
1306 | | static void bpf_object__elf_finish(struct bpf_object *obj) |
1307 | 14.0k | { |
1308 | 14.0k | if (!obj->efile.elf) |
1309 | 3.86k | return; |
1310 | | |
1311 | 10.2k | elf_end(obj->efile.elf); |
1312 | 10.2k | obj->efile.elf = NULL; |
1313 | 10.2k | obj->efile.symbols = NULL; |
1314 | 10.2k | obj->efile.st_ops_data = NULL; |
1315 | 10.2k | obj->efile.st_ops_link_data = NULL; |
1316 | | |
1317 | 10.2k | zfree(&obj->efile.secs); |
1318 | 10.2k | obj->efile.sec_cnt = 0; |
1319 | 10.2k | zclose(obj->efile.fd); |
1320 | 10.2k | obj->efile.obj_buf = NULL; |
1321 | 10.2k | obj->efile.obj_buf_sz = 0; |
1322 | 10.2k | } |
1323 | | |
1324 | | static int bpf_object__elf_init(struct bpf_object *obj) |
1325 | 10.3k | { |
1326 | 10.3k | Elf64_Ehdr *ehdr; |
1327 | 10.3k | int err = 0; |
1328 | 10.3k | Elf *elf; |
1329 | | |
1330 | 10.3k | if (obj->efile.elf) { |
1331 | 0 | pr_warn("elf: init internal error\n"); |
1332 | 0 | return -LIBBPF_ERRNO__LIBELF; |
1333 | 0 | } |
1334 | | |
1335 | 10.3k | if (obj->efile.obj_buf_sz > 0) { |
1336 | | /* obj_buf should have been validated by bpf_object__open_mem(). */ |
1337 | 10.3k | elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); |
1338 | 10.3k | } else { |
1339 | 0 | obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); |
1340 | 0 | if (obj->efile.fd < 0) { |
1341 | 0 | char errmsg[STRERR_BUFSIZE], *cp; |
1342 | |
|
1343 | 0 | err = -errno; |
1344 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
1345 | 0 | pr_warn("elf: failed to open %s: %s\n", obj->path, cp); |
1346 | 0 | return err; |
1347 | 0 | } |
1348 | | |
1349 | 0 | elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); |
1350 | 0 | } |
1351 | | |
1352 | 10.3k | if (!elf) { |
1353 | 87 | pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); |
1354 | 87 | err = -LIBBPF_ERRNO__LIBELF; |
1355 | 87 | goto errout; |
1356 | 87 | } |
1357 | | |
1358 | 10.2k | obj->efile.elf = elf; |
1359 | | |
1360 | 10.2k | if (elf_kind(elf) != ELF_K_ELF) { |
1361 | 121 | err = -LIBBPF_ERRNO__FORMAT; |
1362 | 121 | pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); |
1363 | 121 | goto errout; |
1364 | 121 | } |
1365 | | |
1366 | 10.0k | if (gelf_getclass(elf) != ELFCLASS64) { |
1367 | 505 | err = -LIBBPF_ERRNO__FORMAT; |
1368 | 505 | pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); |
1369 | 505 | goto errout; |
1370 | 505 | } |
1371 | | |
1372 | 9.58k | obj->efile.ehdr = ehdr = elf64_getehdr(elf); |
1373 | 9.58k | if (!obj->efile.ehdr) { |
1374 | 0 | pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); |
1375 | 0 | err = -LIBBPF_ERRNO__FORMAT; |
1376 | 0 | goto errout; |
1377 | 0 | } |
1378 | | |
1379 | 9.58k | if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { |
1380 | 30 | pr_warn("elf: failed to get section names section index for %s: %s\n", |
1381 | 30 | obj->path, elf_errmsg(-1)); |
1382 | 30 | err = -LIBBPF_ERRNO__FORMAT; |
1383 | 30 | goto errout; |
1384 | 30 | } |
1385 | | |
1386 | | /* ELF is corrupted/truncated, avoid calling elf_strptr. */ |
1387 | 9.55k | if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { |
1388 | 1.10k | pr_warn("elf: failed to get section names strings from %s: %s\n", |
1389 | 1.10k | obj->path, elf_errmsg(-1)); |
1390 | 1.10k | err = -LIBBPF_ERRNO__FORMAT; |
1391 | 1.10k | goto errout; |
1392 | 1.10k | } |
1393 | | |
1394 | | /* Old LLVM set e_machine to EM_NONE */ |
1395 | 8.45k | if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { |
1396 | 476 | pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); |
1397 | 476 | err = -LIBBPF_ERRNO__FORMAT; |
1398 | 476 | goto errout; |
1399 | 476 | } |
1400 | | |
1401 | 7.97k | return 0; |
1402 | 2.32k | errout: |
1403 | 2.32k | bpf_object__elf_finish(obj); |
1404 | 2.32k | return err; |
1405 | 8.45k | } |
1406 | | |
1407 | | static int bpf_object__check_endianness(struct bpf_object *obj) |
1408 | 7.97k | { |
1409 | 7.97k | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
1410 | 7.97k | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) |
1411 | 7.95k | return 0; |
1412 | | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
1413 | | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
1414 | | return 0; |
1415 | | #else |
1416 | | # error "Unrecognized __BYTE_ORDER__" |
1417 | | #endif |
1418 | 17 | pr_warn("elf: endianness mismatch in %s.\n", obj->path); |
1419 | 17 | return -LIBBPF_ERRNO__ENDIAN; |
1420 | 7.97k | } |
1421 | | |
1422 | | static int |
1423 | | bpf_object__init_license(struct bpf_object *obj, void *data, size_t size) |
1424 | 229 | { |
1425 | 229 | if (!data) { |
1426 | 2 | pr_warn("invalid license section in %s\n", obj->path); |
1427 | 2 | return -LIBBPF_ERRNO__FORMAT; |
1428 | 2 | } |
1429 | | /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't |
1430 | | * go over allowed ELF data section buffer |
1431 | | */ |
1432 | 227 | libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); |
1433 | 227 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
1434 | 227 | return 0; |
1435 | 229 | } |
1436 | | |
1437 | | static int |
1438 | | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
1439 | 12 | { |
1440 | 12 | __u32 kver; |
1441 | | |
1442 | 12 | if (!data || size != sizeof(kver)) { |
1443 | 2 | pr_warn("invalid kver section in %s\n", obj->path); |
1444 | 2 | return -LIBBPF_ERRNO__FORMAT; |
1445 | 2 | } |
1446 | 10 | memcpy(&kver, data, sizeof(kver)); |
1447 | 10 | obj->kern_version = kver; |
1448 | 10 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
1449 | 10 | return 0; |
1450 | 12 | } |
1451 | | |
1452 | | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
1453 | 1 | { |
1454 | 1 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
1455 | 1 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
1456 | 0 | return true; |
1457 | 1 | return false; |
1458 | 1 | } |
1459 | | |
1460 | | static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size) |
1461 | 447 | { |
1462 | 447 | Elf_Data *data; |
1463 | 447 | Elf_Scn *scn; |
1464 | | |
1465 | 447 | if (!name) |
1466 | 0 | return -EINVAL; |
1467 | | |
1468 | 447 | scn = elf_sec_by_name(obj, name); |
1469 | 447 | data = elf_sec_data(obj, scn); |
1470 | 447 | if (data) { |
1471 | 294 | *size = data->d_size; |
1472 | 294 | return 0; /* found it */ |
1473 | 294 | } |
1474 | | |
1475 | 153 | return -ENOENT; |
1476 | 447 | } |
1477 | | |
1478 | | static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name) |
1479 | 982 | { |
1480 | 982 | Elf_Data *symbols = obj->efile.symbols; |
1481 | 982 | const char *sname; |
1482 | 982 | size_t si; |
1483 | | |
1484 | 11.4k | for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { |
1485 | 11.2k | Elf64_Sym *sym = elf_sym_by_idx(obj, si); |
1486 | | |
1487 | 11.2k | if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) |
1488 | 8.99k | continue; |
1489 | | |
1490 | 2.27k | if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && |
1491 | 2.27k | ELF64_ST_BIND(sym->st_info) != STB_WEAK) |
1492 | 1.15k | continue; |
1493 | | |
1494 | 1.12k | sname = elf_sym_str(obj, sym->st_name); |
1495 | 1.12k | if (!sname) { |
1496 | 58 | pr_warn("failed to get sym name string for var %s\n", name); |
1497 | 58 | return ERR_PTR(-EIO); |
1498 | 58 | } |
1499 | 1.06k | if (strcmp(name, sname) == 0) |
1500 | 708 | return sym; |
1501 | 1.06k | } |
1502 | | |
1503 | 216 | return ERR_PTR(-ENOENT); |
1504 | 982 | } |
1505 | | |
1506 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
1507 | 1.84k | { |
1508 | 1.84k | struct bpf_map *map; |
1509 | 1.84k | int err; |
1510 | | |
1511 | 1.84k | err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, |
1512 | 1.84k | sizeof(*obj->maps), obj->nr_maps + 1); |
1513 | 1.84k | if (err) |
1514 | 0 | return ERR_PTR(err); |
1515 | | |
1516 | 1.84k | map = &obj->maps[obj->nr_maps++]; |
1517 | 1.84k | map->obj = obj; |
1518 | 1.84k | map->fd = -1; |
1519 | 1.84k | map->inner_map_fd = -1; |
1520 | 1.84k | map->autocreate = true; |
1521 | | |
1522 | 1.84k | return map; |
1523 | 1.84k | } |
1524 | | |
1525 | | static size_t bpf_map_mmap_sz(unsigned int value_sz, unsigned int max_entries) |
1526 | 2.72k | { |
1527 | 2.72k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1528 | 2.72k | size_t map_sz; |
1529 | | |
1530 | 2.72k | map_sz = (size_t)roundup(value_sz, 8) * max_entries; |
1531 | 2.72k | map_sz = roundup(map_sz, page_sz); |
1532 | 2.72k | return map_sz; |
1533 | 2.72k | } |
1534 | | |
1535 | | static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz) |
1536 | 0 | { |
1537 | 0 | void *mmaped; |
1538 | |
|
1539 | 0 | if (!map->mmaped) |
1540 | 0 | return -EINVAL; |
1541 | | |
1542 | 0 | if (old_sz == new_sz) |
1543 | 0 | return 0; |
1544 | | |
1545 | 0 | mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1546 | 0 | if (mmaped == MAP_FAILED) |
1547 | 0 | return -errno; |
1548 | | |
1549 | 0 | memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); |
1550 | 0 | munmap(map->mmaped, old_sz); |
1551 | 0 | map->mmaped = mmaped; |
1552 | 0 | return 0; |
1553 | 0 | } |
1554 | | |
1555 | | static char *internal_map_name(struct bpf_object *obj, const char *real_name) |
1556 | 1.42k | { |
1557 | 1.42k | char map_name[BPF_OBJ_NAME_LEN], *p; |
1558 | 1.42k | int pfx_len, sfx_len = max((size_t)7, strlen(real_name)); |
1559 | | |
1560 | | /* This is one of the more confusing parts of libbpf for various |
1561 | | * reasons, some of which are historical. The original idea for naming |
1562 | | * internal names was to include as much of BPF object name prefix as |
1563 | | * possible, so that it can be distinguished from similar internal |
1564 | | * maps of a different BPF object. |
1565 | | * As an example, let's say we have bpf_object named 'my_object_name' |
1566 | | * and internal map corresponding to '.rodata' ELF section. The final |
1567 | | * map name advertised to user and to the kernel will be |
1568 | | * 'my_objec.rodata', taking first 8 characters of object name and |
1569 | | * entire 7 characters of '.rodata'. |
1570 | | * Somewhat confusingly, if internal map ELF section name is shorter |
1571 | | * than 7 characters, e.g., '.bss', we still reserve 7 characters |
1572 | | * for the suffix, even though we only have 4 actual characters, and |
1573 | | * resulting map will be called 'my_objec.bss', not even using all 15 |
1574 | | * characters allowed by the kernel. Oh well, at least the truncated |
1575 | | * object name is somewhat consistent in this case. But if the map |
1576 | | * name is '.kconfig', we'll still have entirety of '.kconfig' added |
1577 | | * (8 chars) and thus will be left with only first 7 characters of the |
1578 | | * object name ('my_obje'). Happy guessing, user, that the final map |
1579 | | * name will be "my_obje.kconfig". |
1580 | | * Now, with libbpf starting to support arbitrarily named .rodata.* |
1581 | | * and .data.* data sections, it's possible that ELF section name is |
1582 | | * longer than allowed 15 chars, so we now need to be careful to take |
1583 | | * only up to 15 first characters of ELF name, taking no BPF object |
1584 | | * name characters at all. So '.rodata.abracadabra' will result in |
1585 | | * '.rodata.abracad' kernel and user-visible name. |
1586 | | * We need to keep this convoluted logic intact for .data, .bss and |
1587 | | * .rodata maps, but for new custom .data.custom and .rodata.custom |
1588 | | * maps we use their ELF names as is, not prepending bpf_object name |
1589 | | * in front. We still need to truncate them to 15 characters for the |
1590 | | * kernel. Full name can be recovered for such maps by using DATASEC |
1591 | | * BTF type associated with such map's value type, though. |
1592 | | */ |
1593 | 1.42k | if (sfx_len >= BPF_OBJ_NAME_LEN) |
1594 | 241 | sfx_len = BPF_OBJ_NAME_LEN - 1; |
1595 | | |
1596 | | /* if there are two or more dots in map name, it's a custom dot map */ |
1597 | 1.42k | if (strchr(real_name + 1, '.') != NULL) |
1598 | 983 | pfx_len = 0; |
1599 | 443 | else |
1600 | 443 | pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); |
1601 | | |
1602 | 1.42k | snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, |
1603 | 1.42k | sfx_len, real_name); |
1604 | | |
1605 | | /* sanitise map name to characters allowed by kernel */ |
1606 | 18.0k | for (p = map_name; *p && p < map_name + sizeof(map_name); p++) |
1607 | 16.6k | if (!isalnum(*p) && *p != '_' && *p != '.') |
1608 | 2.67k | *p = '_'; |
1609 | | |
1610 | 1.42k | return strdup(map_name); |
1611 | 1.42k | } |
1612 | | |
1613 | | static int |
1614 | | map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map); |
1615 | | |
1616 | | /* Internal BPF map is mmap()'able only if at least one of corresponding |
1617 | | * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL |
1618 | | * variable and it's not marked as __hidden (which turns it into, effectively, |
1619 | | * a STATIC variable). |
1620 | | */ |
1621 | | static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map) |
1622 | 1.42k | { |
1623 | 1.42k | const struct btf_type *t, *vt; |
1624 | 1.42k | struct btf_var_secinfo *vsi; |
1625 | 1.42k | int i, n; |
1626 | | |
1627 | 1.42k | if (!map->btf_value_type_id) |
1628 | 1.23k | return false; |
1629 | | |
1630 | 187 | t = btf__type_by_id(obj->btf, map->btf_value_type_id); |
1631 | 187 | if (!btf_is_datasec(t)) |
1632 | 28 | return false; |
1633 | | |
1634 | 159 | vsi = btf_var_secinfos(t); |
1635 | 296 | for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) { |
1636 | 258 | vt = btf__type_by_id(obj->btf, vsi->type); |
1637 | 258 | if (!btf_is_var(vt)) |
1638 | 127 | continue; |
1639 | | |
1640 | 131 | if (btf_var(vt)->linkage != BTF_VAR_STATIC) |
1641 | 121 | return true; |
1642 | 131 | } |
1643 | | |
1644 | 38 | return false; |
1645 | 159 | } |
1646 | | |
1647 | | static int |
1648 | | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
1649 | | const char *real_name, int sec_idx, void *data, size_t data_sz) |
1650 | 1.42k | { |
1651 | 1.42k | struct bpf_map_def *def; |
1652 | 1.42k | struct bpf_map *map; |
1653 | 1.42k | size_t mmap_sz; |
1654 | 1.42k | int err; |
1655 | | |
1656 | 1.42k | map = bpf_object__add_map(obj); |
1657 | 1.42k | if (IS_ERR(map)) |
1658 | 0 | return PTR_ERR(map); |
1659 | | |
1660 | 1.42k | map->libbpf_type = type; |
1661 | 1.42k | map->sec_idx = sec_idx; |
1662 | 1.42k | map->sec_offset = 0; |
1663 | 1.42k | map->real_name = strdup(real_name); |
1664 | 1.42k | map->name = internal_map_name(obj, real_name); |
1665 | 1.42k | if (!map->real_name || !map->name) { |
1666 | 0 | zfree(&map->real_name); |
1667 | 0 | zfree(&map->name); |
1668 | 0 | return -ENOMEM; |
1669 | 0 | } |
1670 | | |
1671 | 1.42k | def = &map->def; |
1672 | 1.42k | def->type = BPF_MAP_TYPE_ARRAY; |
1673 | 1.42k | def->key_size = sizeof(int); |
1674 | 1.42k | def->value_size = data_sz; |
1675 | 1.42k | def->max_entries = 1; |
1676 | 1.42k | def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG |
1677 | 1.42k | ? BPF_F_RDONLY_PROG : 0; |
1678 | | |
1679 | | /* failures are fine because of maps like .rodata.str1.1 */ |
1680 | 1.42k | (void) map_fill_btf_type_info(obj, map); |
1681 | | |
1682 | 1.42k | if (map_is_mmapable(obj, map)) |
1683 | 121 | def->map_flags |= BPF_F_MMAPABLE; |
1684 | | |
1685 | 1.42k | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
1686 | 1.42k | map->name, map->sec_idx, map->sec_offset, def->map_flags); |
1687 | | |
1688 | 1.42k | mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); |
1689 | 1.42k | map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, |
1690 | 1.42k | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1691 | 1.42k | if (map->mmaped == MAP_FAILED) { |
1692 | 127 | err = -errno; |
1693 | 127 | map->mmaped = NULL; |
1694 | 127 | pr_warn("failed to alloc map '%s' content buffer: %d\n", |
1695 | 127 | map->name, err); |
1696 | 127 | zfree(&map->real_name); |
1697 | 127 | zfree(&map->name); |
1698 | 127 | return err; |
1699 | 127 | } |
1700 | | |
1701 | 1.29k | if (data) |
1702 | 721 | memcpy(map->mmaped, data, data_sz); |
1703 | | |
1704 | 1.29k | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
1705 | 1.29k | return 0; |
1706 | 1.42k | } |
1707 | | |
1708 | | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
1709 | 2.15k | { |
1710 | 2.15k | struct elf_sec_desc *sec_desc; |
1711 | 2.15k | const char *sec_name; |
1712 | 2.15k | int err = 0, sec_idx; |
1713 | | |
1714 | | /* |
1715 | | * Populate obj->maps with libbpf internal maps. |
1716 | | */ |
1717 | 15.2k | for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { |
1718 | 13.1k | sec_desc = &obj->efile.secs[sec_idx]; |
1719 | | |
1720 | | /* Skip recognized sections with size 0. */ |
1721 | 13.1k | if (!sec_desc->data || sec_desc->data->d_size == 0) |
1722 | 10.9k | continue; |
1723 | | |
1724 | 2.23k | switch (sec_desc->sec_type) { |
1725 | 482 | case SEC_DATA: |
1726 | 482 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1727 | 482 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
1728 | 482 | sec_name, sec_idx, |
1729 | 482 | sec_desc->data->d_buf, |
1730 | 482 | sec_desc->data->d_size); |
1731 | 482 | break; |
1732 | 239 | case SEC_RODATA: |
1733 | 239 | obj->has_rodata = true; |
1734 | 239 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1735 | 239 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
1736 | 239 | sec_name, sec_idx, |
1737 | 239 | sec_desc->data->d_buf, |
1738 | 239 | sec_desc->data->d_size); |
1739 | 239 | break; |
1740 | 576 | case SEC_BSS: |
1741 | 576 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1742 | 576 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
1743 | 576 | sec_name, sec_idx, |
1744 | 576 | NULL, |
1745 | 576 | sec_desc->data->d_size); |
1746 | 576 | break; |
1747 | 940 | default: |
1748 | | /* skip */ |
1749 | 940 | break; |
1750 | 2.23k | } |
1751 | 2.23k | if (err) |
1752 | 37 | return err; |
1753 | 2.23k | } |
1754 | 2.12k | return 0; |
1755 | 2.15k | } |
1756 | | |
1757 | | |
1758 | | static struct extern_desc *find_extern_by_name(const struct bpf_object *obj, |
1759 | | const void *name) |
1760 | 629 | { |
1761 | 629 | int i; |
1762 | | |
1763 | 1.39k | for (i = 0; i < obj->nr_extern; i++) { |
1764 | 1.06k | if (strcmp(obj->externs[i].name, name) == 0) |
1765 | 297 | return &obj->externs[i]; |
1766 | 1.06k | } |
1767 | 332 | return NULL; |
1768 | 629 | } |
1769 | | |
1770 | | static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, |
1771 | | char value) |
1772 | 0 | { |
1773 | 0 | switch (ext->kcfg.type) { |
1774 | 0 | case KCFG_BOOL: |
1775 | 0 | if (value == 'm') { |
1776 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n", |
1777 | 0 | ext->name, value); |
1778 | 0 | return -EINVAL; |
1779 | 0 | } |
1780 | 0 | *(bool *)ext_val = value == 'y' ? true : false; |
1781 | 0 | break; |
1782 | 0 | case KCFG_TRISTATE: |
1783 | 0 | if (value == 'y') |
1784 | 0 | *(enum libbpf_tristate *)ext_val = TRI_YES; |
1785 | 0 | else if (value == 'm') |
1786 | 0 | *(enum libbpf_tristate *)ext_val = TRI_MODULE; |
1787 | 0 | else /* value == 'n' */ |
1788 | 0 | *(enum libbpf_tristate *)ext_val = TRI_NO; |
1789 | 0 | break; |
1790 | 0 | case KCFG_CHAR: |
1791 | 0 | *(char *)ext_val = value; |
1792 | 0 | break; |
1793 | 0 | case KCFG_UNKNOWN: |
1794 | 0 | case KCFG_INT: |
1795 | 0 | case KCFG_CHAR_ARR: |
1796 | 0 | default: |
1797 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n", |
1798 | 0 | ext->name, value); |
1799 | 0 | return -EINVAL; |
1800 | 0 | } |
1801 | 0 | ext->is_set = true; |
1802 | 0 | return 0; |
1803 | 0 | } |
1804 | | |
1805 | | static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, |
1806 | | const char *value) |
1807 | 0 | { |
1808 | 0 | size_t len; |
1809 | |
|
1810 | 0 | if (ext->kcfg.type != KCFG_CHAR_ARR) { |
1811 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n", |
1812 | 0 | ext->name, value); |
1813 | 0 | return -EINVAL; |
1814 | 0 | } |
1815 | | |
1816 | 0 | len = strlen(value); |
1817 | 0 | if (value[len - 1] != '"') { |
1818 | 0 | pr_warn("extern (kcfg) '%s': invalid string config '%s'\n", |
1819 | 0 | ext->name, value); |
1820 | 0 | return -EINVAL; |
1821 | 0 | } |
1822 | | |
1823 | | /* strip quotes */ |
1824 | 0 | len -= 2; |
1825 | 0 | if (len >= ext->kcfg.sz) { |
1826 | 0 | pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n", |
1827 | 0 | ext->name, value, len, ext->kcfg.sz - 1); |
1828 | 0 | len = ext->kcfg.sz - 1; |
1829 | 0 | } |
1830 | 0 | memcpy(ext_val, value + 1, len); |
1831 | 0 | ext_val[len] = '\0'; |
1832 | 0 | ext->is_set = true; |
1833 | 0 | return 0; |
1834 | 0 | } |
1835 | | |
1836 | | static int parse_u64(const char *value, __u64 *res) |
1837 | 0 | { |
1838 | 0 | char *value_end; |
1839 | 0 | int err; |
1840 | |
|
1841 | 0 | errno = 0; |
1842 | 0 | *res = strtoull(value, &value_end, 0); |
1843 | 0 | if (errno) { |
1844 | 0 | err = -errno; |
1845 | 0 | pr_warn("failed to parse '%s' as integer: %d\n", value, err); |
1846 | 0 | return err; |
1847 | 0 | } |
1848 | 0 | if (*value_end) { |
1849 | 0 | pr_warn("failed to parse '%s' as integer completely\n", value); |
1850 | 0 | return -EINVAL; |
1851 | 0 | } |
1852 | 0 | return 0; |
1853 | 0 | } |
1854 | | |
1855 | | static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) |
1856 | 0 | { |
1857 | 0 | int bit_sz = ext->kcfg.sz * 8; |
1858 | |
|
1859 | 0 | if (ext->kcfg.sz == 8) |
1860 | 0 | return true; |
1861 | | |
1862 | | /* Validate that value stored in u64 fits in integer of `ext->sz` |
1863 | | * bytes size without any loss of information. If the target integer |
1864 | | * is signed, we rely on the following limits of integer type of |
1865 | | * Y bits and subsequent transformation: |
1866 | | * |
1867 | | * -2^(Y-1) <= X <= 2^(Y-1) - 1 |
1868 | | * 0 <= X + 2^(Y-1) <= 2^Y - 1 |
1869 | | * 0 <= X + 2^(Y-1) < 2^Y |
1870 | | * |
1871 | | * For unsigned target integer, check that all the (64 - Y) bits are |
1872 | | * zero. |
1873 | | */ |
1874 | 0 | if (ext->kcfg.is_signed) |
1875 | 0 | return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); |
1876 | 0 | else |
1877 | 0 | return (v >> bit_sz) == 0; |
1878 | 0 | } |
1879 | | |
1880 | | static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, |
1881 | | __u64 value) |
1882 | 0 | { |
1883 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && |
1884 | 0 | ext->kcfg.type != KCFG_BOOL) { |
1885 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n", |
1886 | 0 | ext->name, (unsigned long long)value); |
1887 | 0 | return -EINVAL; |
1888 | 0 | } |
1889 | 0 | if (ext->kcfg.type == KCFG_BOOL && value > 1) { |
1890 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n", |
1891 | 0 | ext->name, (unsigned long long)value); |
1892 | 0 | return -EINVAL; |
1893 | |
|
1894 | 0 | } |
1895 | 0 | if (!is_kcfg_value_in_range(ext, value)) { |
1896 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n", |
1897 | 0 | ext->name, (unsigned long long)value, ext->kcfg.sz); |
1898 | 0 | return -ERANGE; |
1899 | 0 | } |
1900 | 0 | switch (ext->kcfg.sz) { |
1901 | 0 | case 1: |
1902 | 0 | *(__u8 *)ext_val = value; |
1903 | 0 | break; |
1904 | 0 | case 2: |
1905 | 0 | *(__u16 *)ext_val = value; |
1906 | 0 | break; |
1907 | 0 | case 4: |
1908 | 0 | *(__u32 *)ext_val = value; |
1909 | 0 | break; |
1910 | 0 | case 8: |
1911 | 0 | *(__u64 *)ext_val = value; |
1912 | 0 | break; |
1913 | 0 | default: |
1914 | 0 | return -EINVAL; |
1915 | 0 | } |
1916 | 0 | ext->is_set = true; |
1917 | 0 | return 0; |
1918 | 0 | } |
1919 | | |
1920 | | static int bpf_object__process_kconfig_line(struct bpf_object *obj, |
1921 | | char *buf, void *data) |
1922 | 0 | { |
1923 | 0 | struct extern_desc *ext; |
1924 | 0 | char *sep, *value; |
1925 | 0 | int len, err = 0; |
1926 | 0 | void *ext_val; |
1927 | 0 | __u64 num; |
1928 | |
|
1929 | 0 | if (!str_has_pfx(buf, "CONFIG_")) |
1930 | 0 | return 0; |
1931 | | |
1932 | 0 | sep = strchr(buf, '='); |
1933 | 0 | if (!sep) { |
1934 | 0 | pr_warn("failed to parse '%s': no separator\n", buf); |
1935 | 0 | return -EINVAL; |
1936 | 0 | } |
1937 | | |
1938 | | /* Trim ending '\n' */ |
1939 | 0 | len = strlen(buf); |
1940 | 0 | if (buf[len - 1] == '\n') |
1941 | 0 | buf[len - 1] = '\0'; |
1942 | | /* Split on '=' and ensure that a value is present. */ |
1943 | 0 | *sep = '\0'; |
1944 | 0 | if (!sep[1]) { |
1945 | 0 | *sep = '='; |
1946 | 0 | pr_warn("failed to parse '%s': no value\n", buf); |
1947 | 0 | return -EINVAL; |
1948 | 0 | } |
1949 | | |
1950 | 0 | ext = find_extern_by_name(obj, buf); |
1951 | 0 | if (!ext || ext->is_set) |
1952 | 0 | return 0; |
1953 | | |
1954 | 0 | ext_val = data + ext->kcfg.data_off; |
1955 | 0 | value = sep + 1; |
1956 | |
|
1957 | 0 | switch (*value) { |
1958 | 0 | case 'y': case 'n': case 'm': |
1959 | 0 | err = set_kcfg_value_tri(ext, ext_val, *value); |
1960 | 0 | break; |
1961 | 0 | case '"': |
1962 | 0 | err = set_kcfg_value_str(ext, ext_val, value); |
1963 | 0 | break; |
1964 | 0 | default: |
1965 | | /* assume integer */ |
1966 | 0 | err = parse_u64(value, &num); |
1967 | 0 | if (err) { |
1968 | 0 | pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); |
1969 | 0 | return err; |
1970 | 0 | } |
1971 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { |
1972 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); |
1973 | 0 | return -EINVAL; |
1974 | 0 | } |
1975 | 0 | err = set_kcfg_value_num(ext, ext_val, num); |
1976 | 0 | break; |
1977 | 0 | } |
1978 | 0 | if (err) |
1979 | 0 | return err; |
1980 | 0 | pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); |
1981 | 0 | return 0; |
1982 | 0 | } |
1983 | | |
1984 | | static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) |
1985 | 0 | { |
1986 | 0 | char buf[PATH_MAX]; |
1987 | 0 | struct utsname uts; |
1988 | 0 | int len, err = 0; |
1989 | 0 | gzFile file; |
1990 | |
|
1991 | 0 | uname(&uts); |
1992 | 0 | len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); |
1993 | 0 | if (len < 0) |
1994 | 0 | return -EINVAL; |
1995 | 0 | else if (len >= PATH_MAX) |
1996 | 0 | return -ENAMETOOLONG; |
1997 | | |
1998 | | /* gzopen also accepts uncompressed files. */ |
1999 | 0 | file = gzopen(buf, "re"); |
2000 | 0 | if (!file) |
2001 | 0 | file = gzopen("/proc/config.gz", "re"); |
2002 | |
|
2003 | 0 | if (!file) { |
2004 | 0 | pr_warn("failed to open system Kconfig\n"); |
2005 | 0 | return -ENOENT; |
2006 | 0 | } |
2007 | | |
2008 | 0 | while (gzgets(file, buf, sizeof(buf))) { |
2009 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2010 | 0 | if (err) { |
2011 | 0 | pr_warn("error parsing system Kconfig line '%s': %d\n", |
2012 | 0 | buf, err); |
2013 | 0 | goto out; |
2014 | 0 | } |
2015 | 0 | } |
2016 | | |
2017 | 0 | out: |
2018 | 0 | gzclose(file); |
2019 | 0 | return err; |
2020 | 0 | } |
2021 | | |
2022 | | static int bpf_object__read_kconfig_mem(struct bpf_object *obj, |
2023 | | const char *config, void *data) |
2024 | 0 | { |
2025 | 0 | char buf[PATH_MAX]; |
2026 | 0 | int err = 0; |
2027 | 0 | FILE *file; |
2028 | |
|
2029 | 0 | file = fmemopen((void *)config, strlen(config), "r"); |
2030 | 0 | if (!file) { |
2031 | 0 | err = -errno; |
2032 | 0 | pr_warn("failed to open in-memory Kconfig: %d\n", err); |
2033 | 0 | return err; |
2034 | 0 | } |
2035 | | |
2036 | 0 | while (fgets(buf, sizeof(buf), file)) { |
2037 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2038 | 0 | if (err) { |
2039 | 0 | pr_warn("error parsing in-memory Kconfig line '%s': %d\n", |
2040 | 0 | buf, err); |
2041 | 0 | break; |
2042 | 0 | } |
2043 | 0 | } |
2044 | |
|
2045 | 0 | fclose(file); |
2046 | 0 | return err; |
2047 | 0 | } |
2048 | | |
2049 | | static int bpf_object__init_kconfig_map(struct bpf_object *obj) |
2050 | 2.12k | { |
2051 | 2.12k | struct extern_desc *last_ext = NULL, *ext; |
2052 | 2.12k | size_t map_sz; |
2053 | 2.12k | int i, err; |
2054 | | |
2055 | 2.48k | for (i = 0; i < obj->nr_extern; i++) { |
2056 | 364 | ext = &obj->externs[i]; |
2057 | 364 | if (ext->type == EXT_KCFG) |
2058 | 202 | last_ext = ext; |
2059 | 364 | } |
2060 | | |
2061 | 2.12k | if (!last_ext) |
2062 | 1.99k | return 0; |
2063 | | |
2064 | 129 | map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; |
2065 | 129 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG, |
2066 | 129 | ".kconfig", obj->efile.symbols_shndx, |
2067 | 129 | NULL, map_sz); |
2068 | 129 | if (err) |
2069 | 90 | return err; |
2070 | | |
2071 | 39 | obj->kconfig_map_idx = obj->nr_maps - 1; |
2072 | | |
2073 | 39 | return 0; |
2074 | 129 | } |
2075 | | |
2076 | | const struct btf_type * |
2077 | | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) |
2078 | 1.90k | { |
2079 | 1.90k | const struct btf_type *t = btf__type_by_id(btf, id); |
2080 | | |
2081 | 1.90k | if (res_id) |
2082 | 634 | *res_id = id; |
2083 | | |
2084 | 2.75k | while (btf_is_mod(t) || btf_is_typedef(t)) { |
2085 | 852 | if (res_id) |
2086 | 433 | *res_id = t->type; |
2087 | 852 | t = btf__type_by_id(btf, t->type); |
2088 | 852 | } |
2089 | | |
2090 | 1.90k | return t; |
2091 | 1.90k | } |
2092 | | |
2093 | | static const struct btf_type * |
2094 | | resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) |
2095 | 0 | { |
2096 | 0 | const struct btf_type *t; |
2097 | |
|
2098 | 0 | t = skip_mods_and_typedefs(btf, id, NULL); |
2099 | 0 | if (!btf_is_ptr(t)) |
2100 | 0 | return NULL; |
2101 | | |
2102 | 0 | t = skip_mods_and_typedefs(btf, t->type, res_id); |
2103 | |
|
2104 | 0 | return btf_is_func_proto(t) ? t : NULL; |
2105 | 0 | } |
2106 | | |
2107 | | static const char *__btf_kind_str(__u16 kind) |
2108 | 212 | { |
2109 | 212 | switch (kind) { |
2110 | 28 | case BTF_KIND_UNKN: return "void"; |
2111 | 67 | case BTF_KIND_INT: return "int"; |
2112 | 0 | case BTF_KIND_PTR: return "ptr"; |
2113 | 0 | case BTF_KIND_ARRAY: return "array"; |
2114 | 3 | case BTF_KIND_STRUCT: return "struct"; |
2115 | 1 | case BTF_KIND_UNION: return "union"; |
2116 | 4 | case BTF_KIND_ENUM: return "enum"; |
2117 | 4 | case BTF_KIND_FWD: return "fwd"; |
2118 | 0 | case BTF_KIND_TYPEDEF: return "typedef"; |
2119 | 0 | case BTF_KIND_VOLATILE: return "volatile"; |
2120 | 0 | case BTF_KIND_CONST: return "const"; |
2121 | 3 | case BTF_KIND_RESTRICT: return "restrict"; |
2122 | 7 | case BTF_KIND_FUNC: return "func"; |
2123 | 42 | case BTF_KIND_FUNC_PROTO: return "func_proto"; |
2124 | 17 | case BTF_KIND_VAR: return "var"; |
2125 | 25 | case BTF_KIND_DATASEC: return "datasec"; |
2126 | 9 | case BTF_KIND_FLOAT: return "float"; |
2127 | 1 | case BTF_KIND_DECL_TAG: return "decl_tag"; |
2128 | 1 | case BTF_KIND_TYPE_TAG: return "type_tag"; |
2129 | 0 | case BTF_KIND_ENUM64: return "enum64"; |
2130 | 0 | default: return "unknown"; |
2131 | 212 | } |
2132 | 212 | } |
2133 | | |
2134 | | const char *btf_kind_str(const struct btf_type *t) |
2135 | 212 | { |
2136 | 212 | return __btf_kind_str(btf_kind(t)); |
2137 | 212 | } |
2138 | | |
2139 | | /* |
2140 | | * Fetch integer attribute of BTF map definition. Such attributes are |
2141 | | * represented using a pointer to an array, in which dimensionality of array |
2142 | | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
2143 | | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
2144 | | * type definition, while using only sizeof(void *) space in ELF data section. |
2145 | | */ |
2146 | | static bool get_map_field_int(const char *map_name, const struct btf *btf, |
2147 | | const struct btf_member *m, __u32 *res) |
2148 | 16 | { |
2149 | 16 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2150 | 16 | const char *name = btf__name_by_offset(btf, m->name_off); |
2151 | 16 | const struct btf_array *arr_info; |
2152 | 16 | const struct btf_type *arr_t; |
2153 | | |
2154 | 16 | if (!btf_is_ptr(t)) { |
2155 | 13 | pr_warn("map '%s': attr '%s': expected PTR, got %s.\n", |
2156 | 13 | map_name, name, btf_kind_str(t)); |
2157 | 13 | return false; |
2158 | 13 | } |
2159 | | |
2160 | 3 | arr_t = btf__type_by_id(btf, t->type); |
2161 | 3 | if (!arr_t) { |
2162 | 0 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
2163 | 0 | map_name, name, t->type); |
2164 | 0 | return false; |
2165 | 0 | } |
2166 | 3 | if (!btf_is_array(arr_t)) { |
2167 | 2 | pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n", |
2168 | 2 | map_name, name, btf_kind_str(arr_t)); |
2169 | 2 | return false; |
2170 | 2 | } |
2171 | 1 | arr_info = btf_array(arr_t); |
2172 | 1 | *res = arr_info->nelems; |
2173 | 1 | return true; |
2174 | 3 | } |
2175 | | |
2176 | | static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) |
2177 | 0 | { |
2178 | 0 | int len; |
2179 | |
|
2180 | 0 | len = snprintf(buf, buf_sz, "%s/%s", path, name); |
2181 | 0 | if (len < 0) |
2182 | 0 | return -EINVAL; |
2183 | 0 | if (len >= buf_sz) |
2184 | 0 | return -ENAMETOOLONG; |
2185 | | |
2186 | 0 | return 0; |
2187 | 0 | } |
2188 | | |
2189 | | static int build_map_pin_path(struct bpf_map *map, const char *path) |
2190 | 0 | { |
2191 | 0 | char buf[PATH_MAX]; |
2192 | 0 | int err; |
2193 | |
|
2194 | 0 | if (!path) |
2195 | 0 | path = "/sys/fs/bpf"; |
2196 | |
|
2197 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
2198 | 0 | if (err) |
2199 | 0 | return err; |
2200 | | |
2201 | 0 | return bpf_map__set_pin_path(map, buf); |
2202 | 0 | } |
2203 | | |
2204 | | /* should match definition in bpf_helpers.h */ |
2205 | | enum libbpf_pin_type { |
2206 | | LIBBPF_PIN_NONE, |
2207 | | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ |
2208 | | LIBBPF_PIN_BY_NAME, |
2209 | | }; |
2210 | | |
2211 | | int parse_btf_map_def(const char *map_name, struct btf *btf, |
2212 | | const struct btf_type *def_t, bool strict, |
2213 | | struct btf_map_def *map_def, struct btf_map_def *inner_def) |
2214 | 418 | { |
2215 | 418 | const struct btf_type *t; |
2216 | 418 | const struct btf_member *m; |
2217 | 418 | bool is_inner = inner_def == NULL; |
2218 | 418 | int vlen, i; |
2219 | | |
2220 | 418 | vlen = btf_vlen(def_t); |
2221 | 418 | m = btf_members(def_t); |
2222 | 419 | for (i = 0; i < vlen; i++, m++) { |
2223 | 417 | const char *name = btf__name_by_offset(btf, m->name_off); |
2224 | | |
2225 | 417 | if (!name) { |
2226 | 0 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
2227 | 0 | return -EINVAL; |
2228 | 0 | } |
2229 | 417 | if (strcmp(name, "type") == 0) { |
2230 | 1 | if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) |
2231 | 1 | return -EINVAL; |
2232 | 0 | map_def->parts |= MAP_DEF_MAP_TYPE; |
2233 | 416 | } else if (strcmp(name, "max_entries") == 0) { |
2234 | 1 | if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) |
2235 | 1 | return -EINVAL; |
2236 | 0 | map_def->parts |= MAP_DEF_MAX_ENTRIES; |
2237 | 415 | } else if (strcmp(name, "map_flags") == 0) { |
2238 | 7 | if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) |
2239 | 6 | return -EINVAL; |
2240 | 1 | map_def->parts |= MAP_DEF_MAP_FLAGS; |
2241 | 408 | } else if (strcmp(name, "numa_node") == 0) { |
2242 | 2 | if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) |
2243 | 2 | return -EINVAL; |
2244 | 0 | map_def->parts |= MAP_DEF_NUMA_NODE; |
2245 | 406 | } else if (strcmp(name, "key_size") == 0) { |
2246 | 1 | __u32 sz; |
2247 | | |
2248 | 1 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2249 | 1 | return -EINVAL; |
2250 | 0 | if (map_def->key_size && map_def->key_size != sz) { |
2251 | 0 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
2252 | 0 | map_name, map_def->key_size, sz); |
2253 | 0 | return -EINVAL; |
2254 | 0 | } |
2255 | 0 | map_def->key_size = sz; |
2256 | 0 | map_def->parts |= MAP_DEF_KEY_SIZE; |
2257 | 405 | } else if (strcmp(name, "key") == 0) { |
2258 | 2 | __s64 sz; |
2259 | | |
2260 | 2 | t = btf__type_by_id(btf, m->type); |
2261 | 2 | if (!t) { |
2262 | 0 | pr_warn("map '%s': key type [%d] not found.\n", |
2263 | 0 | map_name, m->type); |
2264 | 0 | return -EINVAL; |
2265 | 0 | } |
2266 | 2 | if (!btf_is_ptr(t)) { |
2267 | 2 | pr_warn("map '%s': key spec is not PTR: %s.\n", |
2268 | 2 | map_name, btf_kind_str(t)); |
2269 | 2 | return -EINVAL; |
2270 | 2 | } |
2271 | 0 | sz = btf__resolve_size(btf, t->type); |
2272 | 0 | if (sz < 0) { |
2273 | 0 | pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n", |
2274 | 0 | map_name, t->type, (ssize_t)sz); |
2275 | 0 | return sz; |
2276 | 0 | } |
2277 | 0 | if (map_def->key_size && map_def->key_size != sz) { |
2278 | 0 | pr_warn("map '%s': conflicting key size %u != %zd.\n", |
2279 | 0 | map_name, map_def->key_size, (ssize_t)sz); |
2280 | 0 | return -EINVAL; |
2281 | 0 | } |
2282 | 0 | map_def->key_size = sz; |
2283 | 0 | map_def->key_type_id = t->type; |
2284 | 0 | map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; |
2285 | 403 | } else if (strcmp(name, "value_size") == 0) { |
2286 | 1 | __u32 sz; |
2287 | | |
2288 | 1 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2289 | 1 | return -EINVAL; |
2290 | 0 | if (map_def->value_size && map_def->value_size != sz) { |
2291 | 0 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
2292 | 0 | map_name, map_def->value_size, sz); |
2293 | 0 | return -EINVAL; |
2294 | 0 | } |
2295 | 0 | map_def->value_size = sz; |
2296 | 0 | map_def->parts |= MAP_DEF_VALUE_SIZE; |
2297 | 402 | } else if (strcmp(name, "value") == 0) { |
2298 | 2 | __s64 sz; |
2299 | | |
2300 | 2 | t = btf__type_by_id(btf, m->type); |
2301 | 2 | if (!t) { |
2302 | 0 | pr_warn("map '%s': value type [%d] not found.\n", |
2303 | 0 | map_name, m->type); |
2304 | 0 | return -EINVAL; |
2305 | 0 | } |
2306 | 2 | if (!btf_is_ptr(t)) { |
2307 | 2 | pr_warn("map '%s': value spec is not PTR: %s.\n", |
2308 | 2 | map_name, btf_kind_str(t)); |
2309 | 2 | return -EINVAL; |
2310 | 2 | } |
2311 | 0 | sz = btf__resolve_size(btf, t->type); |
2312 | 0 | if (sz < 0) { |
2313 | 0 | pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n", |
2314 | 0 | map_name, t->type, (ssize_t)sz); |
2315 | 0 | return sz; |
2316 | 0 | } |
2317 | 0 | if (map_def->value_size && map_def->value_size != sz) { |
2318 | 0 | pr_warn("map '%s': conflicting value size %u != %zd.\n", |
2319 | 0 | map_name, map_def->value_size, (ssize_t)sz); |
2320 | 0 | return -EINVAL; |
2321 | 0 | } |
2322 | 0 | map_def->value_size = sz; |
2323 | 0 | map_def->value_type_id = t->type; |
2324 | 0 | map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; |
2325 | 0 | } |
2326 | 400 | else if (strcmp(name, "values") == 0) { |
2327 | 1 | bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); |
2328 | 1 | bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; |
2329 | 1 | const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; |
2330 | 1 | char inner_map_name[128]; |
2331 | 1 | int err; |
2332 | | |
2333 | 1 | if (is_inner) { |
2334 | 0 | pr_warn("map '%s': multi-level inner maps not supported.\n", |
2335 | 0 | map_name); |
2336 | 0 | return -ENOTSUP; |
2337 | 0 | } |
2338 | 1 | if (i != vlen - 1) { |
2339 | 0 | pr_warn("map '%s': '%s' member should be last.\n", |
2340 | 0 | map_name, name); |
2341 | 0 | return -EINVAL; |
2342 | 0 | } |
2343 | 1 | if (!is_map_in_map && !is_prog_array) { |
2344 | 1 | pr_warn("map '%s': should be map-in-map or prog-array.\n", |
2345 | 1 | map_name); |
2346 | 1 | return -ENOTSUP; |
2347 | 1 | } |
2348 | 0 | if (map_def->value_size && map_def->value_size != 4) { |
2349 | 0 | pr_warn("map '%s': conflicting value size %u != 4.\n", |
2350 | 0 | map_name, map_def->value_size); |
2351 | 0 | return -EINVAL; |
2352 | 0 | } |
2353 | 0 | map_def->value_size = 4; |
2354 | 0 | t = btf__type_by_id(btf, m->type); |
2355 | 0 | if (!t) { |
2356 | 0 | pr_warn("map '%s': %s type [%d] not found.\n", |
2357 | 0 | map_name, desc, m->type); |
2358 | 0 | return -EINVAL; |
2359 | 0 | } |
2360 | 0 | if (!btf_is_array(t) || btf_array(t)->nelems) { |
2361 | 0 | pr_warn("map '%s': %s spec is not a zero-sized array.\n", |
2362 | 0 | map_name, desc); |
2363 | 0 | return -EINVAL; |
2364 | 0 | } |
2365 | 0 | t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); |
2366 | 0 | if (!btf_is_ptr(t)) { |
2367 | 0 | pr_warn("map '%s': %s def is of unexpected kind %s.\n", |
2368 | 0 | map_name, desc, btf_kind_str(t)); |
2369 | 0 | return -EINVAL; |
2370 | 0 | } |
2371 | 0 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
2372 | 0 | if (is_prog_array) { |
2373 | 0 | if (!btf_is_func_proto(t)) { |
2374 | 0 | pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", |
2375 | 0 | map_name, btf_kind_str(t)); |
2376 | 0 | return -EINVAL; |
2377 | 0 | } |
2378 | 0 | continue; |
2379 | 0 | } |
2380 | 0 | if (!btf_is_struct(t)) { |
2381 | 0 | pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", |
2382 | 0 | map_name, btf_kind_str(t)); |
2383 | 0 | return -EINVAL; |
2384 | 0 | } |
2385 | | |
2386 | 0 | snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name); |
2387 | 0 | err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL); |
2388 | 0 | if (err) |
2389 | 0 | return err; |
2390 | | |
2391 | 0 | map_def->parts |= MAP_DEF_INNER_MAP; |
2392 | 399 | } else if (strcmp(name, "pinning") == 0) { |
2393 | 2 | __u32 val; |
2394 | | |
2395 | 2 | if (is_inner) { |
2396 | 0 | pr_warn("map '%s': inner def can't be pinned.\n", map_name); |
2397 | 0 | return -EINVAL; |
2398 | 0 | } |
2399 | 2 | if (!get_map_field_int(map_name, btf, m, &val)) |
2400 | 2 | return -EINVAL; |
2401 | 0 | if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) { |
2402 | 0 | pr_warn("map '%s': invalid pinning value %u.\n", |
2403 | 0 | map_name, val); |
2404 | 0 | return -EINVAL; |
2405 | 0 | } |
2406 | 0 | map_def->pinning = val; |
2407 | 0 | map_def->parts |= MAP_DEF_PINNING; |
2408 | 397 | } else if (strcmp(name, "map_extra") == 0) { |
2409 | 1 | __u32 map_extra; |
2410 | | |
2411 | 1 | if (!get_map_field_int(map_name, btf, m, &map_extra)) |
2412 | 1 | return -EINVAL; |
2413 | 0 | map_def->map_extra = map_extra; |
2414 | 0 | map_def->parts |= MAP_DEF_MAP_EXTRA; |
2415 | 396 | } else { |
2416 | 396 | if (strict) { |
2417 | 396 | pr_warn("map '%s': unknown field '%s'.\n", map_name, name); |
2418 | 396 | return -ENOTSUP; |
2419 | 396 | } |
2420 | 0 | pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name); |
2421 | 0 | } |
2422 | 417 | } |
2423 | | |
2424 | 2 | if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { |
2425 | 2 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
2426 | 2 | return -EINVAL; |
2427 | 2 | } |
2428 | | |
2429 | 0 | return 0; |
2430 | 2 | } |
2431 | | |
2432 | | static size_t adjust_ringbuf_sz(size_t sz) |
2433 | 0 | { |
2434 | 0 | __u32 page_sz = sysconf(_SC_PAGE_SIZE); |
2435 | 0 | __u32 mul; |
2436 | | |
2437 | | /* if user forgot to set any size, make sure they see error */ |
2438 | 0 | if (sz == 0) |
2439 | 0 | return 0; |
2440 | | /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be |
2441 | | * a power-of-2 multiple of kernel's page size. If user diligently |
2442 | | * satisified these conditions, pass the size through. |
2443 | | */ |
2444 | 0 | if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz)) |
2445 | 0 | return sz; |
2446 | | |
2447 | | /* Otherwise find closest (page_sz * power_of_2) product bigger than |
2448 | | * user-set size to satisfy both user size request and kernel |
2449 | | * requirements and substitute correct max_entries for map creation. |
2450 | | */ |
2451 | 0 | for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) { |
2452 | 0 | if (mul * page_sz > sz) |
2453 | 0 | return mul * page_sz; |
2454 | 0 | } |
2455 | | |
2456 | | /* if it's impossible to satisfy the conditions (i.e., user size is |
2457 | | * very close to UINT_MAX but is not a power-of-2 multiple of |
2458 | | * page_size) then just return original size and let kernel reject it |
2459 | | */ |
2460 | 0 | return sz; |
2461 | 0 | } |
2462 | | |
2463 | | static bool map_is_ringbuf(const struct bpf_map *map) |
2464 | 0 | { |
2465 | 0 | return map->def.type == BPF_MAP_TYPE_RINGBUF || |
2466 | 0 | map->def.type == BPF_MAP_TYPE_USER_RINGBUF; |
2467 | 0 | } |
2468 | | |
2469 | | static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def) |
2470 | 0 | { |
2471 | 0 | map->def.type = def->map_type; |
2472 | 0 | map->def.key_size = def->key_size; |
2473 | 0 | map->def.value_size = def->value_size; |
2474 | 0 | map->def.max_entries = def->max_entries; |
2475 | 0 | map->def.map_flags = def->map_flags; |
2476 | 0 | map->map_extra = def->map_extra; |
2477 | |
|
2478 | 0 | map->numa_node = def->numa_node; |
2479 | 0 | map->btf_key_type_id = def->key_type_id; |
2480 | 0 | map->btf_value_type_id = def->value_type_id; |
2481 | | |
2482 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
2483 | 0 | if (map_is_ringbuf(map)) |
2484 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
2485 | |
|
2486 | 0 | if (def->parts & MAP_DEF_MAP_TYPE) |
2487 | 0 | pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); |
2488 | |
|
2489 | 0 | if (def->parts & MAP_DEF_KEY_TYPE) |
2490 | 0 | pr_debug("map '%s': found key [%u], sz = %u.\n", |
2491 | 0 | map->name, def->key_type_id, def->key_size); |
2492 | 0 | else if (def->parts & MAP_DEF_KEY_SIZE) |
2493 | 0 | pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); |
2494 | |
|
2495 | 0 | if (def->parts & MAP_DEF_VALUE_TYPE) |
2496 | 0 | pr_debug("map '%s': found value [%u], sz = %u.\n", |
2497 | 0 | map->name, def->value_type_id, def->value_size); |
2498 | 0 | else if (def->parts & MAP_DEF_VALUE_SIZE) |
2499 | 0 | pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); |
2500 | |
|
2501 | 0 | if (def->parts & MAP_DEF_MAX_ENTRIES) |
2502 | 0 | pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); |
2503 | 0 | if (def->parts & MAP_DEF_MAP_FLAGS) |
2504 | 0 | pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); |
2505 | 0 | if (def->parts & MAP_DEF_MAP_EXTRA) |
2506 | 0 | pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, |
2507 | 0 | (unsigned long long)def->map_extra); |
2508 | 0 | if (def->parts & MAP_DEF_PINNING) |
2509 | 0 | pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); |
2510 | 0 | if (def->parts & MAP_DEF_NUMA_NODE) |
2511 | 0 | pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); |
2512 | |
|
2513 | 0 | if (def->parts & MAP_DEF_INNER_MAP) |
2514 | 0 | pr_debug("map '%s': found inner map definition.\n", map->name); |
2515 | 0 | } |
2516 | | |
2517 | | static const char *btf_var_linkage_str(__u32 linkage) |
2518 | 3 | { |
2519 | 3 | switch (linkage) { |
2520 | 1 | case BTF_VAR_STATIC: return "static"; |
2521 | 0 | case BTF_VAR_GLOBAL_ALLOCATED: return "global"; |
2522 | 1 | case BTF_VAR_GLOBAL_EXTERN: return "extern"; |
2523 | 1 | default: return "unknown"; |
2524 | 3 | } |
2525 | 3 | } |
2526 | | |
2527 | | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
2528 | | const struct btf_type *sec, |
2529 | | int var_idx, int sec_idx, |
2530 | | const Elf_Data *data, bool strict, |
2531 | | const char *pin_root_path) |
2532 | 529 | { |
2533 | 529 | struct btf_map_def map_def = {}, inner_def = {}; |
2534 | 529 | const struct btf_type *var, *def; |
2535 | 529 | const struct btf_var_secinfo *vi; |
2536 | 529 | const struct btf_var *var_extra; |
2537 | 529 | const char *map_name; |
2538 | 529 | struct bpf_map *map; |
2539 | 529 | int err; |
2540 | | |
2541 | 529 | vi = btf_var_secinfos(sec) + var_idx; |
2542 | 529 | var = btf__type_by_id(obj->btf, vi->type); |
2543 | 529 | var_extra = btf_var(var); |
2544 | 529 | map_name = btf__name_by_offset(obj->btf, var->name_off); |
2545 | | |
2546 | 529 | if (map_name == NULL || map_name[0] == '\0') { |
2547 | 1 | pr_warn("map #%d: empty name.\n", var_idx); |
2548 | 1 | return -EINVAL; |
2549 | 1 | } |
2550 | 528 | if ((__u64)vi->offset + vi->size > data->d_size) { |
2551 | 46 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
2552 | 46 | return -EINVAL; |
2553 | 46 | } |
2554 | 482 | if (!btf_is_var(var)) { |
2555 | 0 | pr_warn("map '%s': unexpected var kind %s.\n", |
2556 | 0 | map_name, btf_kind_str(var)); |
2557 | 0 | return -EINVAL; |
2558 | 0 | } |
2559 | 482 | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { |
2560 | 3 | pr_warn("map '%s': unsupported map linkage %s.\n", |
2561 | 3 | map_name, btf_var_linkage_str(var_extra->linkage)); |
2562 | 3 | return -EOPNOTSUPP; |
2563 | 3 | } |
2564 | | |
2565 | 479 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
2566 | 479 | if (!btf_is_struct(def)) { |
2567 | 13 | pr_warn("map '%s': unexpected def kind %s.\n", |
2568 | 13 | map_name, btf_kind_str(var)); |
2569 | 13 | return -EINVAL; |
2570 | 13 | } |
2571 | 466 | if (def->size > vi->size) { |
2572 | 48 | pr_warn("map '%s': invalid def size.\n", map_name); |
2573 | 48 | return -EINVAL; |
2574 | 48 | } |
2575 | | |
2576 | 418 | map = bpf_object__add_map(obj); |
2577 | 418 | if (IS_ERR(map)) |
2578 | 0 | return PTR_ERR(map); |
2579 | 418 | map->name = strdup(map_name); |
2580 | 418 | if (!map->name) { |
2581 | 0 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
2582 | 0 | return -ENOMEM; |
2583 | 0 | } |
2584 | 418 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
2585 | 418 | map->def.type = BPF_MAP_TYPE_UNSPEC; |
2586 | 418 | map->sec_idx = sec_idx; |
2587 | 418 | map->sec_offset = vi->offset; |
2588 | 418 | map->btf_var_idx = var_idx; |
2589 | 418 | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
2590 | 418 | map_name, map->sec_idx, map->sec_offset); |
2591 | | |
2592 | 418 | err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); |
2593 | 418 | if (err) |
2594 | 418 | return err; |
2595 | | |
2596 | 0 | fill_map_from_def(map, &map_def); |
2597 | |
|
2598 | 0 | if (map_def.pinning == LIBBPF_PIN_BY_NAME) { |
2599 | 0 | err = build_map_pin_path(map, pin_root_path); |
2600 | 0 | if (err) { |
2601 | 0 | pr_warn("map '%s': couldn't build pin path.\n", map->name); |
2602 | 0 | return err; |
2603 | 0 | } |
2604 | 0 | } |
2605 | | |
2606 | 0 | if (map_def.parts & MAP_DEF_INNER_MAP) { |
2607 | 0 | map->inner_map = calloc(1, sizeof(*map->inner_map)); |
2608 | 0 | if (!map->inner_map) |
2609 | 0 | return -ENOMEM; |
2610 | 0 | map->inner_map->fd = -1; |
2611 | 0 | map->inner_map->sec_idx = sec_idx; |
2612 | 0 | map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); |
2613 | 0 | if (!map->inner_map->name) |
2614 | 0 | return -ENOMEM; |
2615 | 0 | sprintf(map->inner_map->name, "%s.inner", map_name); |
2616 | |
|
2617 | 0 | fill_map_from_def(map->inner_map, &inner_def); |
2618 | 0 | } |
2619 | | |
2620 | 0 | err = map_fill_btf_type_info(obj, map); |
2621 | 0 | if (err) |
2622 | 0 | return err; |
2623 | | |
2624 | 0 | return 0; |
2625 | 0 | } |
2626 | | |
2627 | | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
2628 | | const char *pin_root_path) |
2629 | 2.74k | { |
2630 | 2.74k | const struct btf_type *sec = NULL; |
2631 | 2.74k | int nr_types, i, vlen, err; |
2632 | 2.74k | const struct btf_type *t; |
2633 | 2.74k | const char *name; |
2634 | 2.74k | Elf_Data *data; |
2635 | 2.74k | Elf_Scn *scn; |
2636 | | |
2637 | 2.74k | if (obj->efile.btf_maps_shndx < 0) |
2638 | 2.15k | return 0; |
2639 | | |
2640 | 586 | scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); |
2641 | 586 | data = elf_sec_data(obj, scn); |
2642 | 586 | if (!scn || !data) { |
2643 | 0 | pr_warn("elf: failed to get %s map definitions for %s\n", |
2644 | 0 | MAPS_ELF_SEC, obj->path); |
2645 | 0 | return -EINVAL; |
2646 | 0 | } |
2647 | | |
2648 | 586 | nr_types = btf__type_cnt(obj->btf); |
2649 | 7.11k | for (i = 1; i < nr_types; i++) { |
2650 | 7.05k | t = btf__type_by_id(obj->btf, i); |
2651 | 7.05k | if (!btf_is_datasec(t)) |
2652 | 5.95k | continue; |
2653 | 1.10k | name = btf__name_by_offset(obj->btf, t->name_off); |
2654 | 1.10k | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
2655 | 530 | sec = t; |
2656 | 530 | obj->efile.btf_maps_sec_btf_id = i; |
2657 | 530 | break; |
2658 | 530 | } |
2659 | 1.10k | } |
2660 | | |
2661 | 586 | if (!sec) { |
2662 | 56 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
2663 | 56 | return -ENOENT; |
2664 | 56 | } |
2665 | | |
2666 | 530 | vlen = btf_vlen(sec); |
2667 | 530 | for (i = 0; i < vlen; i++) { |
2668 | 529 | err = bpf_object__init_user_btf_map(obj, sec, i, |
2669 | 529 | obj->efile.btf_maps_shndx, |
2670 | 529 | data, strict, |
2671 | 529 | pin_root_path); |
2672 | 529 | if (err) |
2673 | 529 | return err; |
2674 | 529 | } |
2675 | | |
2676 | 1 | return 0; |
2677 | 530 | } |
2678 | | |
2679 | | static int bpf_object__init_maps(struct bpf_object *obj, |
2680 | | const struct bpf_object_open_opts *opts) |
2681 | 2.74k | { |
2682 | 2.74k | const char *pin_root_path; |
2683 | 2.74k | bool strict; |
2684 | 2.74k | int err = 0; |
2685 | | |
2686 | 2.74k | strict = !OPTS_GET(opts, relaxed_maps, false); |
2687 | 2.74k | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
2688 | | |
2689 | 2.74k | err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
2690 | 2.74k | err = err ?: bpf_object__init_global_data_maps(obj); |
2691 | 2.74k | err = err ?: bpf_object__init_kconfig_map(obj); |
2692 | 2.74k | err = err ?: bpf_object_init_struct_ops(obj); |
2693 | | |
2694 | 2.74k | return err; |
2695 | 2.74k | } |
2696 | | |
2697 | | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
2698 | 2.42k | { |
2699 | 2.42k | Elf64_Shdr *sh; |
2700 | | |
2701 | 2.42k | sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx)); |
2702 | 2.42k | if (!sh) |
2703 | 0 | return false; |
2704 | | |
2705 | 2.42k | return sh->sh_flags & SHF_EXECINSTR; |
2706 | 2.42k | } |
2707 | | |
2708 | | static bool btf_needs_sanitization(struct bpf_object *obj) |
2709 | 0 | { |
2710 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
2711 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
2712 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
2713 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
2714 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
2715 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
2716 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
2717 | |
|
2718 | 0 | return !has_func || !has_datasec || !has_func_global || !has_float || |
2719 | 0 | !has_decl_tag || !has_type_tag || !has_enum64; |
2720 | 0 | } |
2721 | | |
2722 | | static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) |
2723 | 0 | { |
2724 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
2725 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
2726 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
2727 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
2728 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
2729 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
2730 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
2731 | 0 | int enum64_placeholder_id = 0; |
2732 | 0 | struct btf_type *t; |
2733 | 0 | int i, j, vlen; |
2734 | |
|
2735 | 0 | for (i = 1; i < btf__type_cnt(btf); i++) { |
2736 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
2737 | |
|
2738 | 0 | if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) { |
2739 | | /* replace VAR/DECL_TAG with INT */ |
2740 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
2741 | | /* |
2742 | | * using size = 1 is the safest choice, 4 will be too |
2743 | | * big and cause kernel BTF validation failure if |
2744 | | * original variable took less than 4 bytes |
2745 | | */ |
2746 | 0 | t->size = 1; |
2747 | 0 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
2748 | 0 | } else if (!has_datasec && btf_is_datasec(t)) { |
2749 | | /* replace DATASEC with STRUCT */ |
2750 | 0 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
2751 | 0 | struct btf_member *m = btf_members(t); |
2752 | 0 | struct btf_type *vt; |
2753 | 0 | char *name; |
2754 | |
|
2755 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
2756 | 0 | while (*name) { |
2757 | 0 | if (*name == '.') |
2758 | 0 | *name = '_'; |
2759 | 0 | name++; |
2760 | 0 | } |
2761 | |
|
2762 | 0 | vlen = btf_vlen(t); |
2763 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
2764 | 0 | for (j = 0; j < vlen; j++, v++, m++) { |
2765 | | /* order of field assignments is important */ |
2766 | 0 | m->offset = v->offset * 8; |
2767 | 0 | m->type = v->type; |
2768 | | /* preserve variable name as member name */ |
2769 | 0 | vt = (void *)btf__type_by_id(btf, v->type); |
2770 | 0 | m->name_off = vt->name_off; |
2771 | 0 | } |
2772 | 0 | } else if (!has_func && btf_is_func_proto(t)) { |
2773 | | /* replace FUNC_PROTO with ENUM */ |
2774 | 0 | vlen = btf_vlen(t); |
2775 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
2776 | 0 | t->size = sizeof(__u32); /* kernel enforced */ |
2777 | 0 | } else if (!has_func && btf_is_func(t)) { |
2778 | | /* replace FUNC with TYPEDEF */ |
2779 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
2780 | 0 | } else if (!has_func_global && btf_is_func(t)) { |
2781 | | /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */ |
2782 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); |
2783 | 0 | } else if (!has_float && btf_is_float(t)) { |
2784 | | /* replace FLOAT with an equally-sized empty STRUCT; |
2785 | | * since C compilers do not accept e.g. "float" as a |
2786 | | * valid struct name, make it anonymous |
2787 | | */ |
2788 | 0 | t->name_off = 0; |
2789 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); |
2790 | 0 | } else if (!has_type_tag && btf_is_type_tag(t)) { |
2791 | | /* replace TYPE_TAG with a CONST */ |
2792 | 0 | t->name_off = 0; |
2793 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); |
2794 | 0 | } else if (!has_enum64 && btf_is_enum(t)) { |
2795 | | /* clear the kflag */ |
2796 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); |
2797 | 0 | } else if (!has_enum64 && btf_is_enum64(t)) { |
2798 | | /* replace ENUM64 with a union */ |
2799 | 0 | struct btf_member *m; |
2800 | |
|
2801 | 0 | if (enum64_placeholder_id == 0) { |
2802 | 0 | enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); |
2803 | 0 | if (enum64_placeholder_id < 0) |
2804 | 0 | return enum64_placeholder_id; |
2805 | | |
2806 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
2807 | 0 | } |
2808 | | |
2809 | 0 | m = btf_members(t); |
2810 | 0 | vlen = btf_vlen(t); |
2811 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); |
2812 | 0 | for (j = 0; j < vlen; j++, m++) { |
2813 | 0 | m->type = enum64_placeholder_id; |
2814 | 0 | m->offset = 0; |
2815 | 0 | } |
2816 | 0 | } |
2817 | 0 | } |
2818 | | |
2819 | 0 | return 0; |
2820 | 0 | } |
2821 | | |
2822 | | static bool libbpf_needs_btf(const struct bpf_object *obj) |
2823 | 2.99k | { |
2824 | 2.99k | return obj->efile.btf_maps_shndx >= 0 || |
2825 | 2.99k | obj->efile.st_ops_shndx >= 0 || |
2826 | 2.99k | obj->efile.st_ops_link_shndx >= 0 || |
2827 | 2.99k | obj->nr_extern > 0; |
2828 | 2.99k | } |
2829 | | |
2830 | | static bool kernel_needs_btf(const struct bpf_object *obj) |
2831 | 0 | { |
2832 | 0 | return obj->efile.st_ops_shndx >= 0 || obj->efile.st_ops_link_shndx >= 0; |
2833 | 0 | } |
2834 | | |
2835 | | static int bpf_object__init_btf(struct bpf_object *obj, |
2836 | | Elf_Data *btf_data, |
2837 | | Elf_Data *btf_ext_data) |
2838 | 5.97k | { |
2839 | 5.97k | int err = -ENOENT; |
2840 | | |
2841 | 5.97k | if (btf_data) { |
2842 | 4.17k | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
2843 | 4.17k | err = libbpf_get_error(obj->btf); |
2844 | 4.17k | if (err) { |
2845 | 962 | obj->btf = NULL; |
2846 | 962 | pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err); |
2847 | 962 | goto out; |
2848 | 962 | } |
2849 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
2850 | 3.21k | btf__set_pointer_size(obj->btf, 8); |
2851 | 3.21k | } |
2852 | 5.01k | if (btf_ext_data) { |
2853 | 349 | struct btf_ext_info *ext_segs[3]; |
2854 | 349 | int seg_num, sec_num; |
2855 | | |
2856 | 349 | if (!obj->btf) { |
2857 | 4 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
2858 | 4 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
2859 | 4 | goto out; |
2860 | 4 | } |
2861 | 345 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); |
2862 | 345 | err = libbpf_get_error(obj->btf_ext); |
2863 | 345 | if (err) { |
2864 | 233 | pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n", |
2865 | 233 | BTF_EXT_ELF_SEC, err); |
2866 | 233 | obj->btf_ext = NULL; |
2867 | 233 | goto out; |
2868 | 233 | } |
2869 | | |
2870 | | /* setup .BTF.ext to ELF section mapping */ |
2871 | 112 | ext_segs[0] = &obj->btf_ext->func_info; |
2872 | 112 | ext_segs[1] = &obj->btf_ext->line_info; |
2873 | 112 | ext_segs[2] = &obj->btf_ext->core_relo_info; |
2874 | 448 | for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) { |
2875 | 336 | struct btf_ext_info *seg = ext_segs[seg_num]; |
2876 | 336 | const struct btf_ext_info_sec *sec; |
2877 | 336 | const char *sec_name; |
2878 | 336 | Elf_Scn *scn; |
2879 | | |
2880 | 336 | if (seg->sec_cnt == 0) |
2881 | 226 | continue; |
2882 | | |
2883 | 110 | seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); |
2884 | 110 | if (!seg->sec_idxs) { |
2885 | 0 | err = -ENOMEM; |
2886 | 0 | goto out; |
2887 | 0 | } |
2888 | | |
2889 | 110 | sec_num = 0; |
2890 | 434 | for_each_btf_ext_sec(seg, sec) { |
2891 | | /* preventively increment index to avoid doing |
2892 | | * this before every continue below |
2893 | | */ |
2894 | 434 | sec_num++; |
2895 | | |
2896 | 434 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
2897 | 434 | if (str_is_empty(sec_name)) |
2898 | 226 | continue; |
2899 | 208 | scn = elf_sec_by_name(obj, sec_name); |
2900 | 208 | if (!scn) |
2901 | 186 | continue; |
2902 | | |
2903 | 22 | seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); |
2904 | 22 | } |
2905 | 110 | } |
2906 | 112 | } |
2907 | 5.97k | out: |
2908 | 5.97k | if (err && libbpf_needs_btf(obj)) { |
2909 | 53 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
2910 | 53 | return err; |
2911 | 53 | } |
2912 | 5.92k | return 0; |
2913 | 5.97k | } |
2914 | | |
2915 | | static int compare_vsi_off(const void *_a, const void *_b) |
2916 | 1.42k | { |
2917 | 1.42k | const struct btf_var_secinfo *a = _a; |
2918 | 1.42k | const struct btf_var_secinfo *b = _b; |
2919 | | |
2920 | 1.42k | return a->offset - b->offset; |
2921 | 1.42k | } |
2922 | | |
2923 | | static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, |
2924 | | struct btf_type *t) |
2925 | 3.45k | { |
2926 | 3.45k | __u32 size = 0, i, vars = btf_vlen(t); |
2927 | 3.45k | const char *sec_name = btf__name_by_offset(btf, t->name_off); |
2928 | 3.45k | struct btf_var_secinfo *vsi; |
2929 | 3.45k | bool fixup_offsets = false; |
2930 | 3.45k | int err; |
2931 | | |
2932 | 3.45k | if (!sec_name) { |
2933 | 0 | pr_debug("No name found in string section for DATASEC kind.\n"); |
2934 | 0 | return -ENOENT; |
2935 | 0 | } |
2936 | | |
2937 | | /* Extern-backing datasecs (.ksyms, .kconfig) have their size and |
2938 | | * variable offsets set at the previous step. Further, not every |
2939 | | * extern BTF VAR has corresponding ELF symbol preserved, so we skip |
2940 | | * all fixups altogether for such sections and go straight to sorting |
2941 | | * VARs within their DATASEC. |
2942 | | */ |
2943 | 3.45k | if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0) |
2944 | 1.01k | goto sort_vars; |
2945 | | |
2946 | | /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to |
2947 | | * fix this up. But BPF static linker already fixes this up and fills |
2948 | | * all the sizes and offsets during static linking. So this step has |
2949 | | * to be optional. But the STV_HIDDEN handling is non-optional for any |
2950 | | * non-extern DATASEC, so the variable fixup loop below handles both |
2951 | | * functions at the same time, paying the cost of BTF VAR <-> ELF |
2952 | | * symbol matching just once. |
2953 | | */ |
2954 | 2.44k | if (t->size == 0) { |
2955 | 447 | err = find_elf_sec_sz(obj, sec_name, &size); |
2956 | 447 | if (err || !size) { |
2957 | 157 | pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n", |
2958 | 157 | sec_name, size, err); |
2959 | 157 | return -ENOENT; |
2960 | 157 | } |
2961 | | |
2962 | 290 | t->size = size; |
2963 | 290 | fixup_offsets = true; |
2964 | 290 | } |
2965 | | |
2966 | 3.20k | for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { |
2967 | 1.34k | const struct btf_type *t_var; |
2968 | 1.34k | struct btf_var *var; |
2969 | 1.34k | const char *var_name; |
2970 | 1.34k | Elf64_Sym *sym; |
2971 | | |
2972 | 1.34k | t_var = btf__type_by_id(btf, vsi->type); |
2973 | 1.34k | if (!t_var || !btf_is_var(t_var)) { |
2974 | 143 | pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); |
2975 | 143 | return -EINVAL; |
2976 | 143 | } |
2977 | | |
2978 | 1.19k | var = btf_var(t_var); |
2979 | 1.19k | if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) |
2980 | 215 | continue; |
2981 | | |
2982 | 982 | var_name = btf__name_by_offset(btf, t_var->name_off); |
2983 | 982 | if (!var_name) { |
2984 | 0 | pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n", |
2985 | 0 | sec_name, i); |
2986 | 0 | return -ENOENT; |
2987 | 0 | } |
2988 | | |
2989 | 982 | sym = find_elf_var_sym(obj, var_name); |
2990 | 982 | if (IS_ERR(sym)) { |
2991 | 274 | pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n", |
2992 | 274 | sec_name, var_name); |
2993 | 274 | return -ENOENT; |
2994 | 274 | } |
2995 | | |
2996 | 708 | if (fixup_offsets) |
2997 | 70 | vsi->offset = sym->st_value; |
2998 | | |
2999 | | /* if variable is a global/weak symbol, but has restricted |
3000 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR |
3001 | | * as static. This follows similar logic for functions (BPF |
3002 | | * subprogs) and influences libbpf's further decisions about |
3003 | | * whether to make global data BPF array maps as |
3004 | | * BPF_F_MMAPABLE. |
3005 | | */ |
3006 | 708 | if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
3007 | 708 | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) |
3008 | 35 | var->linkage = BTF_VAR_STATIC; |
3009 | 708 | } |
3010 | | |
3011 | 2.87k | sort_vars: |
3012 | 2.87k | qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off); |
3013 | 2.87k | return 0; |
3014 | 2.28k | } |
3015 | | |
3016 | | static int bpf_object_fixup_btf(struct bpf_object *obj) |
3017 | 3.31k | { |
3018 | 3.31k | int i, n, err = 0; |
3019 | | |
3020 | 3.31k | if (!obj->btf) |
3021 | 1.61k | return 0; |
3022 | | |
3023 | 1.70k | n = btf__type_cnt(obj->btf); |
3024 | 17.3k | for (i = 1; i < n; i++) { |
3025 | 16.2k | struct btf_type *t = btf_type_by_id(obj->btf, i); |
3026 | | |
3027 | | /* Loader needs to fix up some of the things compiler |
3028 | | * couldn't get its hands on while emitting BTF. This |
3029 | | * is section size and global variable offset. We use |
3030 | | * the info from the ELF itself for this purpose. |
3031 | | */ |
3032 | 16.2k | if (btf_is_datasec(t)) { |
3033 | 3.45k | err = btf_fixup_datasec(obj, obj->btf, t); |
3034 | 3.45k | if (err) |
3035 | 574 | return err; |
3036 | 3.45k | } |
3037 | 16.2k | } |
3038 | | |
3039 | 1.13k | return 0; |
3040 | 1.70k | } |
3041 | | |
3042 | | static bool prog_needs_vmlinux_btf(struct bpf_program *prog) |
3043 | 0 | { |
3044 | 0 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || |
3045 | 0 | prog->type == BPF_PROG_TYPE_LSM) |
3046 | 0 | return true; |
3047 | | |
3048 | | /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs |
3049 | | * also need vmlinux BTF |
3050 | | */ |
3051 | 0 | if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) |
3052 | 0 | return true; |
3053 | | |
3054 | 0 | return false; |
3055 | 0 | } |
3056 | | |
3057 | | static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) |
3058 | 0 | { |
3059 | 0 | struct bpf_program *prog; |
3060 | 0 | int i; |
3061 | | |
3062 | | /* CO-RE relocations need kernel BTF, only when btf_custom_path |
3063 | | * is not specified |
3064 | | */ |
3065 | 0 | if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) |
3066 | 0 | return true; |
3067 | | |
3068 | | /* Support for typed ksyms needs kernel BTF */ |
3069 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
3070 | 0 | const struct extern_desc *ext; |
3071 | |
|
3072 | 0 | ext = &obj->externs[i]; |
3073 | 0 | if (ext->type == EXT_KSYM && ext->ksym.type_id) |
3074 | 0 | return true; |
3075 | 0 | } |
3076 | | |
3077 | 0 | bpf_object__for_each_program(prog, obj) { |
3078 | 0 | if (!prog->autoload) |
3079 | 0 | continue; |
3080 | 0 | if (prog_needs_vmlinux_btf(prog)) |
3081 | 0 | return true; |
3082 | 0 | } |
3083 | | |
3084 | 0 | return false; |
3085 | 0 | } |
3086 | | |
3087 | | static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) |
3088 | 0 | { |
3089 | 0 | int err; |
3090 | | |
3091 | | /* btf_vmlinux could be loaded earlier */ |
3092 | 0 | if (obj->btf_vmlinux || obj->gen_loader) |
3093 | 0 | return 0; |
3094 | | |
3095 | 0 | if (!force && !obj_needs_vmlinux_btf(obj)) |
3096 | 0 | return 0; |
3097 | | |
3098 | 0 | obj->btf_vmlinux = btf__load_vmlinux_btf(); |
3099 | 0 | err = libbpf_get_error(obj->btf_vmlinux); |
3100 | 0 | if (err) { |
3101 | 0 | pr_warn("Error loading vmlinux BTF: %d\n", err); |
3102 | 0 | obj->btf_vmlinux = NULL; |
3103 | 0 | return err; |
3104 | 0 | } |
3105 | 0 | return 0; |
3106 | 0 | } |
3107 | | |
3108 | | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
3109 | 0 | { |
3110 | 0 | struct btf *kern_btf = obj->btf; |
3111 | 0 | bool btf_mandatory, sanitize; |
3112 | 0 | int i, err = 0; |
3113 | |
|
3114 | 0 | if (!obj->btf) |
3115 | 0 | return 0; |
3116 | | |
3117 | 0 | if (!kernel_supports(obj, FEAT_BTF)) { |
3118 | 0 | if (kernel_needs_btf(obj)) { |
3119 | 0 | err = -EOPNOTSUPP; |
3120 | 0 | goto report; |
3121 | 0 | } |
3122 | 0 | pr_debug("Kernel doesn't support BTF, skipping uploading it.\n"); |
3123 | 0 | return 0; |
3124 | 0 | } |
3125 | | |
3126 | | /* Even though some subprogs are global/weak, user might prefer more |
3127 | | * permissive BPF verification process that BPF verifier performs for |
3128 | | * static functions, taking into account more context from the caller |
3129 | | * functions. In such case, they need to mark such subprogs with |
3130 | | * __attribute__((visibility("hidden"))) and libbpf will adjust |
3131 | | * corresponding FUNC BTF type to be marked as static and trigger more |
3132 | | * involved BPF verification process. |
3133 | | */ |
3134 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
3135 | 0 | struct bpf_program *prog = &obj->programs[i]; |
3136 | 0 | struct btf_type *t; |
3137 | 0 | const char *name; |
3138 | 0 | int j, n; |
3139 | |
|
3140 | 0 | if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) |
3141 | 0 | continue; |
3142 | | |
3143 | 0 | n = btf__type_cnt(obj->btf); |
3144 | 0 | for (j = 1; j < n; j++) { |
3145 | 0 | t = btf_type_by_id(obj->btf, j); |
3146 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) |
3147 | 0 | continue; |
3148 | | |
3149 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
3150 | 0 | if (strcmp(name, prog->name) != 0) |
3151 | 0 | continue; |
3152 | | |
3153 | 0 | t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); |
3154 | 0 | break; |
3155 | 0 | } |
3156 | 0 | } |
3157 | |
|
3158 | 0 | if (!kernel_supports(obj, FEAT_BTF_DECL_TAG)) |
3159 | 0 | goto skip_exception_cb; |
3160 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
3161 | 0 | struct bpf_program *prog = &obj->programs[i]; |
3162 | 0 | int j, k, n; |
3163 | |
|
3164 | 0 | if (prog_is_subprog(obj, prog)) |
3165 | 0 | continue; |
3166 | 0 | n = btf__type_cnt(obj->btf); |
3167 | 0 | for (j = 1; j < n; j++) { |
3168 | 0 | const char *str = "exception_callback:", *name; |
3169 | 0 | size_t len = strlen(str); |
3170 | 0 | struct btf_type *t; |
3171 | |
|
3172 | 0 | t = btf_type_by_id(obj->btf, j); |
3173 | 0 | if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1) |
3174 | 0 | continue; |
3175 | | |
3176 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
3177 | 0 | if (strncmp(name, str, len)) |
3178 | 0 | continue; |
3179 | | |
3180 | 0 | t = btf_type_by_id(obj->btf, t->type); |
3181 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) { |
3182 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n", |
3183 | 0 | prog->name); |
3184 | 0 | return -EINVAL; |
3185 | 0 | } |
3186 | 0 | if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off))) |
3187 | 0 | continue; |
3188 | | /* Multiple callbacks are specified for the same prog, |
3189 | | * the verifier will eventually return an error for this |
3190 | | * case, hence simply skip appending a subprog. |
3191 | | */ |
3192 | 0 | if (prog->exception_cb_idx >= 0) { |
3193 | 0 | prog->exception_cb_idx = -1; |
3194 | 0 | break; |
3195 | 0 | } |
3196 | | |
3197 | 0 | name += len; |
3198 | 0 | if (str_is_empty(name)) { |
3199 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n", |
3200 | 0 | prog->name); |
3201 | 0 | return -EINVAL; |
3202 | 0 | } |
3203 | | |
3204 | 0 | for (k = 0; k < obj->nr_programs; k++) { |
3205 | 0 | struct bpf_program *subprog = &obj->programs[k]; |
3206 | |
|
3207 | 0 | if (!prog_is_subprog(obj, subprog)) |
3208 | 0 | continue; |
3209 | 0 | if (strcmp(name, subprog->name)) |
3210 | 0 | continue; |
3211 | | /* Enforce non-hidden, as from verifier point of |
3212 | | * view it expects global functions, whereas the |
3213 | | * mark_btf_static fixes up linkage as static. |
3214 | | */ |
3215 | 0 | if (!subprog->sym_global || subprog->mark_btf_static) { |
3216 | 0 | pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n", |
3217 | 0 | prog->name, subprog->name); |
3218 | 0 | return -EINVAL; |
3219 | 0 | } |
3220 | | /* Let's see if we already saw a static exception callback with the same name */ |
3221 | 0 | if (prog->exception_cb_idx >= 0) { |
3222 | 0 | pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n", |
3223 | 0 | prog->name, subprog->name); |
3224 | 0 | return -EINVAL; |
3225 | 0 | } |
3226 | 0 | prog->exception_cb_idx = k; |
3227 | 0 | break; |
3228 | 0 | } |
3229 | | |
3230 | 0 | if (prog->exception_cb_idx >= 0) |
3231 | 0 | continue; |
3232 | 0 | pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name); |
3233 | 0 | return -ENOENT; |
3234 | 0 | } |
3235 | 0 | } |
3236 | 0 | skip_exception_cb: |
3237 | |
|
3238 | 0 | sanitize = btf_needs_sanitization(obj); |
3239 | 0 | if (sanitize) { |
3240 | 0 | const void *raw_data; |
3241 | 0 | __u32 sz; |
3242 | | |
3243 | | /* clone BTF to sanitize a copy and leave the original intact */ |
3244 | 0 | raw_data = btf__raw_data(obj->btf, &sz); |
3245 | 0 | kern_btf = btf__new(raw_data, sz); |
3246 | 0 | err = libbpf_get_error(kern_btf); |
3247 | 0 | if (err) |
3248 | 0 | return err; |
3249 | | |
3250 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3251 | 0 | btf__set_pointer_size(obj->btf, 8); |
3252 | 0 | err = bpf_object__sanitize_btf(obj, kern_btf); |
3253 | 0 | if (err) |
3254 | 0 | return err; |
3255 | 0 | } |
3256 | | |
3257 | 0 | if (obj->gen_loader) { |
3258 | 0 | __u32 raw_size = 0; |
3259 | 0 | const void *raw_data = btf__raw_data(kern_btf, &raw_size); |
3260 | |
|
3261 | 0 | if (!raw_data) |
3262 | 0 | return -ENOMEM; |
3263 | 0 | bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); |
3264 | | /* Pretend to have valid FD to pass various fd >= 0 checks. |
3265 | | * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. |
3266 | | */ |
3267 | 0 | btf__set_fd(kern_btf, 0); |
3268 | 0 | } else { |
3269 | | /* currently BPF_BTF_LOAD only supports log_level 1 */ |
3270 | 0 | err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, |
3271 | 0 | obj->log_level ? 1 : 0); |
3272 | 0 | } |
3273 | 0 | if (sanitize) { |
3274 | 0 | if (!err) { |
3275 | | /* move fd to libbpf's BTF */ |
3276 | 0 | btf__set_fd(obj->btf, btf__fd(kern_btf)); |
3277 | 0 | btf__set_fd(kern_btf, -1); |
3278 | 0 | } |
3279 | 0 | btf__free(kern_btf); |
3280 | 0 | } |
3281 | 0 | report: |
3282 | 0 | if (err) { |
3283 | 0 | btf_mandatory = kernel_needs_btf(obj); |
3284 | 0 | pr_warn("Error loading .BTF into kernel: %d. %s\n", err, |
3285 | 0 | btf_mandatory ? "BTF is mandatory, can't proceed." |
3286 | 0 | : "BTF is optional, ignoring."); |
3287 | 0 | if (!btf_mandatory) |
3288 | 0 | err = 0; |
3289 | 0 | } |
3290 | 0 | return err; |
3291 | 0 | } |
3292 | | |
3293 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off) |
3294 | 23.4k | { |
3295 | 23.4k | const char *name; |
3296 | | |
3297 | 23.4k | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); |
3298 | 23.4k | if (!name) { |
3299 | 7.45k | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3300 | 7.45k | off, obj->path, elf_errmsg(-1)); |
3301 | 7.45k | return NULL; |
3302 | 7.45k | } |
3303 | | |
3304 | 15.9k | return name; |
3305 | 23.4k | } |
3306 | | |
3307 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off) |
3308 | 45.5k | { |
3309 | 45.5k | const char *name; |
3310 | | |
3311 | 45.5k | name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); |
3312 | 45.5k | if (!name) { |
3313 | 933 | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3314 | 933 | off, obj->path, elf_errmsg(-1)); |
3315 | 933 | return NULL; |
3316 | 933 | } |
3317 | | |
3318 | 44.6k | return name; |
3319 | 45.5k | } |
3320 | | |
3321 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx) |
3322 | 13.1k | { |
3323 | 13.1k | Elf_Scn *scn; |
3324 | | |
3325 | 13.1k | scn = elf_getscn(obj->efile.elf, idx); |
3326 | 13.1k | if (!scn) { |
3327 | 0 | pr_warn("elf: failed to get section(%zu) from %s: %s\n", |
3328 | 0 | idx, obj->path, elf_errmsg(-1)); |
3329 | 0 | return NULL; |
3330 | 0 | } |
3331 | 13.1k | return scn; |
3332 | 13.1k | } |
3333 | | |
3334 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name) |
3335 | 655 | { |
3336 | 655 | Elf_Scn *scn = NULL; |
3337 | 655 | Elf *elf = obj->efile.elf; |
3338 | 655 | const char *sec_name; |
3339 | | |
3340 | 4.42k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3341 | 4.08k | sec_name = elf_sec_name(obj, scn); |
3342 | 4.08k | if (!sec_name) |
3343 | 0 | return NULL; |
3344 | | |
3345 | 4.08k | if (strcmp(sec_name, name) != 0) |
3346 | 3.76k | continue; |
3347 | | |
3348 | 317 | return scn; |
3349 | 4.08k | } |
3350 | 338 | return NULL; |
3351 | 655 | } |
3352 | | |
3353 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn) |
3354 | 93.3k | { |
3355 | 93.3k | Elf64_Shdr *shdr; |
3356 | | |
3357 | 93.3k | if (!scn) |
3358 | 0 | return NULL; |
3359 | | |
3360 | 93.3k | shdr = elf64_getshdr(scn); |
3361 | 93.3k | if (!shdr) { |
3362 | 0 | pr_warn("elf: failed to get section(%zu) header from %s: %s\n", |
3363 | 0 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3364 | 0 | return NULL; |
3365 | 0 | } |
3366 | | |
3367 | 93.3k | return shdr; |
3368 | 93.3k | } |
3369 | | |
3370 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn) |
3371 | 9.13k | { |
3372 | 9.13k | const char *name; |
3373 | 9.13k | Elf64_Shdr *sh; |
3374 | | |
3375 | 9.13k | if (!scn) |
3376 | 0 | return NULL; |
3377 | | |
3378 | 9.13k | sh = elf_sec_hdr(obj, scn); |
3379 | 9.13k | if (!sh) |
3380 | 0 | return NULL; |
3381 | | |
3382 | 9.13k | name = elf_sec_str(obj, sh->sh_name); |
3383 | 9.13k | if (!name) { |
3384 | 837 | pr_warn("elf: failed to get section(%zu) name from %s: %s\n", |
3385 | 837 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3386 | 837 | return NULL; |
3387 | 837 | } |
3388 | | |
3389 | 8.29k | return name; |
3390 | 9.13k | } |
3391 | | |
3392 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn) |
3393 | 36.4k | { |
3394 | 36.4k | Elf_Data *data; |
3395 | | |
3396 | 36.4k | if (!scn) |
3397 | 152 | return NULL; |
3398 | | |
3399 | 36.3k | data = elf_getdata(scn, 0); |
3400 | 36.3k | if (!data) { |
3401 | 827 | pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n", |
3402 | 827 | elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>", |
3403 | 827 | obj->path, elf_errmsg(-1)); |
3404 | 827 | return NULL; |
3405 | 827 | } |
3406 | | |
3407 | 35.4k | return data; |
3408 | 36.3k | } |
3409 | | |
3410 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx) |
3411 | 595k | { |
3412 | 595k | if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) |
3413 | 165 | return NULL; |
3414 | | |
3415 | 595k | return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; |
3416 | 595k | } |
3417 | | |
3418 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx) |
3419 | 6.54k | { |
3420 | 6.54k | if (idx >= data->d_size / sizeof(Elf64_Rel)) |
3421 | 0 | return NULL; |
3422 | | |
3423 | 6.54k | return (Elf64_Rel *)data->d_buf + idx; |
3424 | 6.54k | } |
3425 | | |
3426 | | static bool is_sec_name_dwarf(const char *name) |
3427 | 31.6k | { |
3428 | | /* approximation, but the actual list is too long */ |
3429 | 31.6k | return str_has_pfx(name, ".debug_"); |
3430 | 31.6k | } |
3431 | | |
3432 | | static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name) |
3433 | 34.9k | { |
3434 | | /* no special handling of .strtab */ |
3435 | 34.9k | if (hdr->sh_type == SHT_STRTAB) |
3436 | 5.15k | return true; |
3437 | | |
3438 | | /* ignore .llvm_addrsig section as well */ |
3439 | 29.8k | if (hdr->sh_type == SHT_LLVM_ADDRSIG) |
3440 | 23 | return true; |
3441 | | |
3442 | | /* no subprograms will lead to an empty .text section, ignore it */ |
3443 | 29.7k | if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && |
3444 | 29.7k | strcmp(name, ".text") == 0) |
3445 | 80 | return true; |
3446 | | |
3447 | | /* DWARF sections */ |
3448 | 29.7k | if (is_sec_name_dwarf(name)) |
3449 | 2.90k | return true; |
3450 | | |
3451 | 26.8k | if (str_has_pfx(name, ".rel")) { |
3452 | 1.89k | name += sizeof(".rel") - 1; |
3453 | | /* DWARF section relocations */ |
3454 | 1.89k | if (is_sec_name_dwarf(name)) |
3455 | 233 | return true; |
3456 | | |
3457 | | /* .BTF and .BTF.ext don't need relocations */ |
3458 | 1.66k | if (strcmp(name, BTF_ELF_SEC) == 0 || |
3459 | 1.66k | strcmp(name, BTF_EXT_ELF_SEC) == 0) |
3460 | 419 | return true; |
3461 | 1.66k | } |
3462 | | |
3463 | 26.1k | return false; |
3464 | 26.8k | } |
3465 | | |
3466 | | static int cmp_progs(const void *_a, const void *_b) |
3467 | 43.2k | { |
3468 | 43.2k | const struct bpf_program *a = _a; |
3469 | 43.2k | const struct bpf_program *b = _b; |
3470 | | |
3471 | 43.2k | if (a->sec_idx != b->sec_idx) |
3472 | 751 | return a->sec_idx < b->sec_idx ? -1 : 1; |
3473 | | |
3474 | | /* sec_insn_off can't be the same within the section */ |
3475 | 42.5k | return a->sec_insn_off < b->sec_insn_off ? -1 : 1; |
3476 | 43.2k | } |
3477 | | |
3478 | | static int bpf_object__elf_collect(struct bpf_object *obj) |
3479 | 7.95k | { |
3480 | 7.95k | struct elf_sec_desc *sec_desc; |
3481 | 7.95k | Elf *elf = obj->efile.elf; |
3482 | 7.95k | Elf_Data *btf_ext_data = NULL; |
3483 | 7.95k | Elf_Data *btf_data = NULL; |
3484 | 7.95k | int idx = 0, err = 0; |
3485 | 7.95k | const char *name; |
3486 | 7.95k | Elf_Data *data; |
3487 | 7.95k | Elf_Scn *scn; |
3488 | 7.95k | Elf64_Shdr *sh; |
3489 | | |
3490 | | /* ELF section indices are 0-based, but sec #0 is special "invalid" |
3491 | | * section. Since section count retrieved by elf_getshdrnum() does |
3492 | | * include sec #0, it is already the necessary size of an array to keep |
3493 | | * all the sections. |
3494 | | */ |
3495 | 7.95k | if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { |
3496 | 0 | pr_warn("elf: failed to get the number of sections for %s: %s\n", |
3497 | 0 | obj->path, elf_errmsg(-1)); |
3498 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3499 | 0 | } |
3500 | 7.95k | obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); |
3501 | 7.95k | if (!obj->efile.secs) |
3502 | 0 | return -ENOMEM; |
3503 | | |
3504 | | /* a bunch of ELF parsing functionality depends on processing symbols, |
3505 | | * so do the first pass and find the symbol table |
3506 | | */ |
3507 | 7.95k | scn = NULL; |
3508 | 48.4k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3509 | 40.7k | sh = elf_sec_hdr(obj, scn); |
3510 | 40.7k | if (!sh) |
3511 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3512 | | |
3513 | 40.7k | if (sh->sh_type == SHT_SYMTAB) { |
3514 | 7.89k | if (obj->efile.symbols) { |
3515 | 3 | pr_warn("elf: multiple symbol tables in %s\n", obj->path); |
3516 | 3 | return -LIBBPF_ERRNO__FORMAT; |
3517 | 3 | } |
3518 | | |
3519 | 7.88k | data = elf_sec_data(obj, scn); |
3520 | 7.88k | if (!data) |
3521 | 240 | return -LIBBPF_ERRNO__FORMAT; |
3522 | | |
3523 | 7.64k | idx = elf_ndxscn(scn); |
3524 | | |
3525 | 7.64k | obj->efile.symbols = data; |
3526 | 7.64k | obj->efile.symbols_shndx = idx; |
3527 | 7.64k | obj->efile.strtabidx = sh->sh_link; |
3528 | 7.64k | } |
3529 | 40.7k | } |
3530 | | |
3531 | 7.71k | if (!obj->efile.symbols) { |
3532 | 70 | pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n", |
3533 | 70 | obj->path); |
3534 | 70 | return -ENOENT; |
3535 | 70 | } |
3536 | | |
3537 | 7.64k | scn = NULL; |
3538 | 41.8k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3539 | 35.0k | idx = elf_ndxscn(scn); |
3540 | 35.0k | sec_desc = &obj->efile.secs[idx]; |
3541 | | |
3542 | 35.0k | sh = elf_sec_hdr(obj, scn); |
3543 | 35.0k | if (!sh) |
3544 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3545 | | |
3546 | 35.0k | name = elf_sec_str(obj, sh->sh_name); |
3547 | 35.0k | if (!name) |
3548 | 96 | return -LIBBPF_ERRNO__FORMAT; |
3549 | | |
3550 | 34.9k | if (ignore_elf_section(sh, name)) |
3551 | 8.81k | continue; |
3552 | | |
3553 | 26.1k | data = elf_sec_data(obj, scn); |
3554 | 26.1k | if (!data) |
3555 | 181 | return -LIBBPF_ERRNO__FORMAT; |
3556 | | |
3557 | 25.9k | pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
3558 | 25.9k | idx, name, (unsigned long)data->d_size, |
3559 | 25.9k | (int)sh->sh_link, (unsigned long)sh->sh_flags, |
3560 | 25.9k | (int)sh->sh_type); |
3561 | | |
3562 | 25.9k | if (strcmp(name, "license") == 0) { |
3563 | 229 | err = bpf_object__init_license(obj, data->d_buf, data->d_size); |
3564 | 229 | if (err) |
3565 | 2 | return err; |
3566 | 25.7k | } else if (strcmp(name, "version") == 0) { |
3567 | 12 | err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); |
3568 | 12 | if (err) |
3569 | 2 | return err; |
3570 | 25.7k | } else if (strcmp(name, "maps") == 0) { |
3571 | 6 | pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n"); |
3572 | 6 | return -ENOTSUP; |
3573 | 25.7k | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
3574 | 822 | obj->efile.btf_maps_shndx = idx; |
3575 | 24.9k | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
3576 | 4.31k | if (sh->sh_type != SHT_PROGBITS) |
3577 | 57 | return -LIBBPF_ERRNO__FORMAT; |
3578 | 4.25k | btf_data = data; |
3579 | 20.5k | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
3580 | 531 | if (sh->sh_type != SHT_PROGBITS) |
3581 | 53 | return -LIBBPF_ERRNO__FORMAT; |
3582 | 478 | btf_ext_data = data; |
3583 | 20.0k | } else if (sh->sh_type == SHT_SYMTAB) { |
3584 | | /* already processed during the first pass above */ |
3585 | 13.2k | } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { |
3586 | 2.65k | if (sh->sh_flags & SHF_EXECINSTR) { |
3587 | 1.20k | if (strcmp(name, ".text") == 0) |
3588 | 194 | obj->efile.text_shndx = idx; |
3589 | 1.20k | err = bpf_object__add_programs(obj, data, name, idx); |
3590 | 1.20k | if (err) |
3591 | 271 | return err; |
3592 | 1.45k | } else if (strcmp(name, DATA_SEC) == 0 || |
3593 | 1.45k | str_has_pfx(name, DATA_SEC ".")) { |
3594 | 514 | sec_desc->sec_type = SEC_DATA; |
3595 | 514 | sec_desc->shdr = sh; |
3596 | 514 | sec_desc->data = data; |
3597 | 940 | } else if (strcmp(name, RODATA_SEC) == 0 || |
3598 | 940 | str_has_pfx(name, RODATA_SEC ".")) { |
3599 | 246 | sec_desc->sec_type = SEC_RODATA; |
3600 | 246 | sec_desc->shdr = sh; |
3601 | 246 | sec_desc->data = data; |
3602 | 694 | } else if (strcmp(name, STRUCT_OPS_SEC) == 0) { |
3603 | 91 | obj->efile.st_ops_data = data; |
3604 | 91 | obj->efile.st_ops_shndx = idx; |
3605 | 603 | } else if (strcmp(name, STRUCT_OPS_LINK_SEC) == 0) { |
3606 | 153 | obj->efile.st_ops_link_data = data; |
3607 | 153 | obj->efile.st_ops_link_shndx = idx; |
3608 | 450 | } else { |
3609 | 450 | pr_info("elf: skipping unrecognized data section(%d) %s\n", |
3610 | 450 | idx, name); |
3611 | 450 | } |
3612 | 10.5k | } else if (sh->sh_type == SHT_REL) { |
3613 | 2.60k | int targ_sec_idx = sh->sh_info; /* points to other section */ |
3614 | | |
3615 | 2.60k | if (sh->sh_entsize != sizeof(Elf64_Rel) || |
3616 | 2.60k | targ_sec_idx >= obj->efile.sec_cnt) |
3617 | 183 | return -LIBBPF_ERRNO__FORMAT; |
3618 | | |
3619 | | /* Only do relo for section with exec instructions */ |
3620 | 2.42k | if (!section_have_execinstr(obj, targ_sec_idx) && |
3621 | 2.42k | strcmp(name, ".rel" STRUCT_OPS_SEC) && |
3622 | 2.42k | strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && |
3623 | 2.42k | strcmp(name, ".rel" MAPS_ELF_SEC)) { |
3624 | 598 | pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", |
3625 | 598 | idx, name, targ_sec_idx, |
3626 | 598 | elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>"); |
3627 | 598 | continue; |
3628 | 598 | } |
3629 | | |
3630 | 1.82k | sec_desc->sec_type = SEC_RELO; |
3631 | 1.82k | sec_desc->shdr = sh; |
3632 | 1.82k | sec_desc->data = data; |
3633 | 7.98k | } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || |
3634 | 1.33k | str_has_pfx(name, BSS_SEC "."))) { |
3635 | 656 | sec_desc->sec_type = SEC_BSS; |
3636 | 656 | sec_desc->shdr = sh; |
3637 | 656 | sec_desc->data = data; |
3638 | 7.32k | } else { |
3639 | 7.32k | pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name, |
3640 | 7.32k | (size_t)sh->sh_size); |
3641 | 7.32k | } |
3642 | 25.9k | } |
3643 | | |
3644 | 6.79k | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
3645 | 820 | pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); |
3646 | 820 | return -LIBBPF_ERRNO__FORMAT; |
3647 | 820 | } |
3648 | | |
3649 | | /* sort BPF programs by section name and in-section instruction offset |
3650 | | * for faster search |
3651 | | */ |
3652 | 5.97k | if (obj->nr_programs) |
3653 | 614 | qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); |
3654 | | |
3655 | 5.97k | return bpf_object__init_btf(obj, btf_data, btf_ext_data); |
3656 | 6.79k | } |
3657 | | |
3658 | | static bool sym_is_extern(const Elf64_Sym *sym) |
3659 | 322k | { |
3660 | 322k | int bind = ELF64_ST_BIND(sym->st_info); |
3661 | | /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */ |
3662 | 322k | return sym->st_shndx == SHN_UNDEF && |
3663 | 322k | (bind == STB_GLOBAL || bind == STB_WEAK) && |
3664 | 322k | ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; |
3665 | 322k | } |
3666 | | |
3667 | | static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx) |
3668 | 779 | { |
3669 | 779 | int bind = ELF64_ST_BIND(sym->st_info); |
3670 | 779 | int type = ELF64_ST_TYPE(sym->st_info); |
3671 | | |
3672 | | /* in .text section */ |
3673 | 779 | if (sym->st_shndx != text_shndx) |
3674 | 475 | return false; |
3675 | | |
3676 | | /* local function */ |
3677 | 304 | if (bind == STB_LOCAL && type == STT_SECTION) |
3678 | 101 | return true; |
3679 | | |
3680 | | /* global function */ |
3681 | 203 | return bind == STB_GLOBAL && type == STT_FUNC; |
3682 | 304 | } |
3683 | | |
3684 | | static int find_extern_btf_id(const struct btf *btf, const char *ext_name) |
3685 | 1.88k | { |
3686 | 1.88k | const struct btf_type *t; |
3687 | 1.88k | const char *tname; |
3688 | 1.88k | int i, n; |
3689 | | |
3690 | 1.88k | if (!btf) |
3691 | 41 | return -ESRCH; |
3692 | | |
3693 | 1.83k | n = btf__type_cnt(btf); |
3694 | 14.9k | for (i = 1; i < n; i++) { |
3695 | 14.8k | t = btf__type_by_id(btf, i); |
3696 | | |
3697 | 14.8k | if (!btf_is_var(t) && !btf_is_func(t)) |
3698 | 11.4k | continue; |
3699 | | |
3700 | 3.40k | tname = btf__name_by_offset(btf, t->name_off); |
3701 | 3.40k | if (strcmp(tname, ext_name)) |
3702 | 1.73k | continue; |
3703 | | |
3704 | 1.66k | if (btf_is_var(t) && |
3705 | 1.66k | btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) |
3706 | 44 | return -EINVAL; |
3707 | | |
3708 | 1.61k | if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN) |
3709 | 16 | return -EINVAL; |
3710 | | |
3711 | 1.60k | return i; |
3712 | 1.61k | } |
3713 | | |
3714 | 178 | return -ENOENT; |
3715 | 1.83k | } |
3716 | | |
3717 | 1.60k | static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) { |
3718 | 1.60k | const struct btf_var_secinfo *vs; |
3719 | 1.60k | const struct btf_type *t; |
3720 | 1.60k | int i, j, n; |
3721 | | |
3722 | 1.60k | if (!btf) |
3723 | 0 | return -ESRCH; |
3724 | | |
3725 | 1.60k | n = btf__type_cnt(btf); |
3726 | 14.5k | for (i = 1; i < n; i++) { |
3727 | 14.5k | t = btf__type_by_id(btf, i); |
3728 | | |
3729 | 14.5k | if (!btf_is_datasec(t)) |
3730 | 11.0k | continue; |
3731 | | |
3732 | 3.47k | vs = btf_var_secinfos(t); |
3733 | 6.32k | for (j = 0; j < btf_vlen(t); j++, vs++) { |
3734 | 4.43k | if (vs->type == ext_btf_id) |
3735 | 1.58k | return i; |
3736 | 4.43k | } |
3737 | 3.47k | } |
3738 | | |
3739 | 12 | return -ENOENT; |
3740 | 1.60k | } |
3741 | | |
3742 | | static enum kcfg_type find_kcfg_type(const struct btf *btf, int id, |
3743 | | bool *is_signed) |
3744 | 775 | { |
3745 | 775 | const struct btf_type *t; |
3746 | 775 | const char *name; |
3747 | | |
3748 | 775 | t = skip_mods_and_typedefs(btf, id, NULL); |
3749 | 775 | name = btf__name_by_offset(btf, t->name_off); |
3750 | | |
3751 | 775 | if (is_signed) |
3752 | 611 | *is_signed = false; |
3753 | 775 | switch (btf_kind(t)) { |
3754 | 354 | case BTF_KIND_INT: { |
3755 | 354 | int enc = btf_int_encoding(t); |
3756 | | |
3757 | 354 | if (enc & BTF_INT_BOOL) |
3758 | 57 | return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; |
3759 | 297 | if (is_signed) |
3760 | 278 | *is_signed = enc & BTF_INT_SIGNED; |
3761 | 297 | if (t->size == 1) |
3762 | 96 | return KCFG_CHAR; |
3763 | 201 | if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) |
3764 | 36 | return KCFG_UNKNOWN; |
3765 | 165 | return KCFG_INT; |
3766 | 201 | } |
3767 | 130 | case BTF_KIND_ENUM: |
3768 | 130 | if (t->size != 4) |
3769 | 16 | return KCFG_UNKNOWN; |
3770 | 114 | if (strcmp(name, "libbpf_tristate")) |
3771 | 113 | return KCFG_UNKNOWN; |
3772 | 1 | return KCFG_TRISTATE; |
3773 | 92 | case BTF_KIND_ENUM64: |
3774 | 92 | if (strcmp(name, "libbpf_tristate")) |
3775 | 82 | return KCFG_UNKNOWN; |
3776 | 10 | return KCFG_TRISTATE; |
3777 | 164 | case BTF_KIND_ARRAY: |
3778 | 164 | if (btf_array(t)->nelems == 0) |
3779 | 0 | return KCFG_UNKNOWN; |
3780 | 164 | if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) |
3781 | 146 | return KCFG_UNKNOWN; |
3782 | 18 | return KCFG_CHAR_ARR; |
3783 | 35 | default: |
3784 | 35 | return KCFG_UNKNOWN; |
3785 | 775 | } |
3786 | 775 | } |
3787 | | |
3788 | | static int cmp_externs(const void *_a, const void *_b) |
3789 | 851 | { |
3790 | 851 | const struct extern_desc *a = _a; |
3791 | 851 | const struct extern_desc *b = _b; |
3792 | | |
3793 | 851 | if (a->type != b->type) |
3794 | 0 | return a->type < b->type ? -1 : 1; |
3795 | | |
3796 | 851 | if (a->type == EXT_KCFG) { |
3797 | | /* descending order by alignment requirements */ |
3798 | 132 | if (a->kcfg.align != b->kcfg.align) |
3799 | 0 | return a->kcfg.align > b->kcfg.align ? -1 : 1; |
3800 | | /* ascending order by size, within same alignment class */ |
3801 | 132 | if (a->kcfg.sz != b->kcfg.sz) |
3802 | 0 | return a->kcfg.sz < b->kcfg.sz ? -1 : 1; |
3803 | 132 | } |
3804 | | |
3805 | | /* resolve ties by name */ |
3806 | 851 | return strcmp(a->name, b->name); |
3807 | 851 | } |
3808 | | |
3809 | | static int find_int_btf_id(const struct btf *btf) |
3810 | 724 | { |
3811 | 724 | const struct btf_type *t; |
3812 | 724 | int i, n; |
3813 | | |
3814 | 724 | n = btf__type_cnt(btf); |
3815 | 10.1k | for (i = 1; i < n; i++) { |
3816 | 9.50k | t = btf__type_by_id(btf, i); |
3817 | | |
3818 | 9.50k | if (btf_is_int(t) && btf_int_bits(t) == 32) |
3819 | 31 | return i; |
3820 | 9.50k | } |
3821 | | |
3822 | 693 | return 0; |
3823 | 724 | } |
3824 | | |
3825 | | static int add_dummy_ksym_var(struct btf *btf) |
3826 | 4.50k | { |
3827 | 4.50k | int i, int_btf_id, sec_btf_id, dummy_var_btf_id; |
3828 | 4.50k | const struct btf_var_secinfo *vs; |
3829 | 4.50k | const struct btf_type *sec; |
3830 | | |
3831 | 4.50k | if (!btf) |
3832 | 1.65k | return 0; |
3833 | | |
3834 | 2.85k | sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC, |
3835 | 2.85k | BTF_KIND_DATASEC); |
3836 | 2.85k | if (sec_btf_id < 0) |
3837 | 2.16k | return 0; |
3838 | | |
3839 | 685 | sec = btf__type_by_id(btf, sec_btf_id); |
3840 | 685 | vs = btf_var_secinfos(sec); |
3841 | 1.59k | for (i = 0; i < btf_vlen(sec); i++, vs++) { |
3842 | 1.41k | const struct btf_type *vt; |
3843 | | |
3844 | 1.41k | vt = btf__type_by_id(btf, vs->type); |
3845 | 1.41k | if (btf_is_func(vt)) |
3846 | 508 | break; |
3847 | 1.41k | } |
3848 | | |
3849 | | /* No func in ksyms sec. No need to add dummy var. */ |
3850 | 685 | if (i == btf_vlen(sec)) |
3851 | 177 | return 0; |
3852 | | |
3853 | 508 | int_btf_id = find_int_btf_id(btf); |
3854 | 508 | dummy_var_btf_id = btf__add_var(btf, |
3855 | 508 | "dummy_ksym", |
3856 | 508 | BTF_VAR_GLOBAL_ALLOCATED, |
3857 | 508 | int_btf_id); |
3858 | 508 | if (dummy_var_btf_id < 0) |
3859 | 508 | pr_warn("cannot create a dummy_ksym var\n"); |
3860 | | |
3861 | 508 | return dummy_var_btf_id; |
3862 | 685 | } |
3863 | | |
3864 | | static int bpf_object__collect_externs(struct bpf_object *obj) |
3865 | 5.92k | { |
3866 | 5.92k | struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL; |
3867 | 5.92k | const struct btf_type *t; |
3868 | 5.92k | struct extern_desc *ext; |
3869 | 5.92k | int i, n, off, dummy_var_btf_id; |
3870 | 5.92k | const char *ext_name, *sec_name; |
3871 | 5.92k | size_t ext_essent_len; |
3872 | 5.92k | Elf_Scn *scn; |
3873 | 5.92k | Elf64_Shdr *sh; |
3874 | | |
3875 | 5.92k | if (!obj->efile.symbols) |
3876 | 0 | return 0; |
3877 | | |
3878 | 5.92k | scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); |
3879 | 5.92k | sh = elf_sec_hdr(obj, scn); |
3880 | 5.92k | if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) |
3881 | 1.41k | return -LIBBPF_ERRNO__FORMAT; |
3882 | | |
3883 | 4.50k | dummy_var_btf_id = add_dummy_ksym_var(obj->btf); |
3884 | 4.50k | if (dummy_var_btf_id < 0) |
3885 | 0 | return dummy_var_btf_id; |
3886 | | |
3887 | 4.50k | n = sh->sh_size / sh->sh_entsize; |
3888 | 4.50k | pr_debug("looking for externs among %d symbols...\n", n); |
3889 | | |
3890 | 324k | for (i = 0; i < n; i++) { |
3891 | 321k | Elf64_Sym *sym = elf_sym_by_idx(obj, i); |
3892 | | |
3893 | 321k | if (!sym) |
3894 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3895 | 321k | if (!sym_is_extern(sym)) |
3896 | 316k | continue; |
3897 | 5.10k | ext_name = elf_sym_str(obj, sym->st_name); |
3898 | 5.10k | if (!ext_name || !ext_name[0]) |
3899 | 3.22k | continue; |
3900 | | |
3901 | 1.88k | ext = obj->externs; |
3902 | 1.88k | ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); |
3903 | 1.88k | if (!ext) |
3904 | 0 | return -ENOMEM; |
3905 | 1.88k | obj->externs = ext; |
3906 | 1.88k | ext = &ext[obj->nr_extern]; |
3907 | 1.88k | memset(ext, 0, sizeof(*ext)); |
3908 | 1.88k | obj->nr_extern++; |
3909 | | |
3910 | 1.88k | ext->btf_id = find_extern_btf_id(obj->btf, ext_name); |
3911 | 1.88k | if (ext->btf_id <= 0) { |
3912 | 279 | pr_warn("failed to find BTF for extern '%s': %d\n", |
3913 | 279 | ext_name, ext->btf_id); |
3914 | 279 | return ext->btf_id; |
3915 | 279 | } |
3916 | 1.60k | t = btf__type_by_id(obj->btf, ext->btf_id); |
3917 | 1.60k | ext->name = btf__name_by_offset(obj->btf, t->name_off); |
3918 | 1.60k | ext->sym_idx = i; |
3919 | 1.60k | ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; |
3920 | | |
3921 | 1.60k | ext_essent_len = bpf_core_essential_name_len(ext->name); |
3922 | 1.60k | ext->essent_name = NULL; |
3923 | 1.60k | if (ext_essent_len != strlen(ext->name)) { |
3924 | 43 | ext->essent_name = strndup(ext->name, ext_essent_len); |
3925 | 43 | if (!ext->essent_name) |
3926 | 0 | return -ENOMEM; |
3927 | 43 | } |
3928 | | |
3929 | 1.60k | ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); |
3930 | 1.60k | if (ext->sec_btf_id <= 0) { |
3931 | 12 | pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n", |
3932 | 12 | ext_name, ext->btf_id, ext->sec_btf_id); |
3933 | 12 | return ext->sec_btf_id; |
3934 | 12 | } |
3935 | 1.58k | sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); |
3936 | 1.58k | sec_name = btf__name_by_offset(obj->btf, sec->name_off); |
3937 | | |
3938 | 1.58k | if (strcmp(sec_name, KCONFIG_SEC) == 0) { |
3939 | 841 | if (btf_is_func(t)) { |
3940 | 1 | pr_warn("extern function %s is unsupported under %s section\n", |
3941 | 1 | ext->name, KCONFIG_SEC); |
3942 | 1 | return -ENOTSUP; |
3943 | 1 | } |
3944 | 840 | kcfg_sec = sec; |
3945 | 840 | ext->type = EXT_KCFG; |
3946 | 840 | ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); |
3947 | 840 | if (ext->kcfg.sz <= 0) { |
3948 | 217 | pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n", |
3949 | 217 | ext_name, ext->kcfg.sz); |
3950 | 217 | return ext->kcfg.sz; |
3951 | 217 | } |
3952 | 623 | ext->kcfg.align = btf__align_of(obj->btf, t->type); |
3953 | 623 | if (ext->kcfg.align <= 0) { |
3954 | 12 | pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n", |
3955 | 12 | ext_name, ext->kcfg.align); |
3956 | 12 | return -EINVAL; |
3957 | 12 | } |
3958 | 611 | ext->kcfg.type = find_kcfg_type(obj->btf, t->type, |
3959 | 611 | &ext->kcfg.is_signed); |
3960 | 611 | if (ext->kcfg.type == KCFG_UNKNOWN) { |
3961 | 308 | pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name); |
3962 | 308 | return -ENOTSUP; |
3963 | 308 | } |
3964 | 748 | } else if (strcmp(sec_name, KSYMS_SEC) == 0) { |
3965 | 634 | ksym_sec = sec; |
3966 | 634 | ext->type = EXT_KSYM; |
3967 | 634 | skip_mods_and_typedefs(obj->btf, t->type, |
3968 | 634 | &ext->ksym.type_id); |
3969 | 634 | } else { |
3970 | 114 | pr_warn("unrecognized extern section '%s'\n", sec_name); |
3971 | 114 | return -ENOTSUP; |
3972 | 114 | } |
3973 | 1.58k | } |
3974 | 3.56k | pr_debug("collected %d externs total\n", obj->nr_extern); |
3975 | | |
3976 | 3.56k | if (!obj->nr_extern) |
3977 | 3.15k | return 0; |
3978 | | |
3979 | | /* sort externs by type, for kcfg ones also by (align, size, name) */ |
3980 | 408 | qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); |
3981 | | |
3982 | | /* for .ksyms section, we need to turn all externs into allocated |
3983 | | * variables in BTF to pass kernel verification; we do this by |
3984 | | * pretending that each extern is a 8-byte variable |
3985 | | */ |
3986 | 408 | if (ksym_sec) { |
3987 | | /* find existing 4-byte integer type in BTF to use for fake |
3988 | | * extern variables in DATASEC |
3989 | | */ |
3990 | 216 | int int_btf_id = find_int_btf_id(obj->btf); |
3991 | | /* For extern function, a dummy_var added earlier |
3992 | | * will be used to replace the vs->type and |
3993 | | * its name string will be used to refill |
3994 | | * the missing param's name. |
3995 | | */ |
3996 | 216 | const struct btf_type *dummy_var; |
3997 | | |
3998 | 216 | dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); |
3999 | 810 | for (i = 0; i < obj->nr_extern; i++) { |
4000 | 594 | ext = &obj->externs[i]; |
4001 | 594 | if (ext->type != EXT_KSYM) |
4002 | 0 | continue; |
4003 | 594 | pr_debug("extern (ksym) #%d: symbol %d, name %s\n", |
4004 | 594 | i, ext->sym_idx, ext->name); |
4005 | 594 | } |
4006 | | |
4007 | 216 | sec = ksym_sec; |
4008 | 216 | n = btf_vlen(sec); |
4009 | 427 | for (i = 0, off = 0; i < n; i++, off += sizeof(int)) { |
4010 | 391 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4011 | 391 | struct btf_type *vt; |
4012 | | |
4013 | 391 | vt = (void *)btf__type_by_id(obj->btf, vs->type); |
4014 | 391 | ext_name = btf__name_by_offset(obj->btf, vt->name_off); |
4015 | 391 | ext = find_extern_by_name(obj, ext_name); |
4016 | 391 | if (!ext) { |
4017 | 180 | pr_warn("failed to find extern definition for BTF %s '%s'\n", |
4018 | 180 | btf_kind_str(vt), ext_name); |
4019 | 180 | return -ESRCH; |
4020 | 180 | } |
4021 | 211 | if (btf_is_func(vt)) { |
4022 | 105 | const struct btf_type *func_proto; |
4023 | 105 | struct btf_param *param; |
4024 | 105 | int j; |
4025 | | |
4026 | 105 | func_proto = btf__type_by_id(obj->btf, |
4027 | 105 | vt->type); |
4028 | 105 | param = btf_params(func_proto); |
4029 | | /* Reuse the dummy_var string if the |
4030 | | * func proto does not have param name. |
4031 | | */ |
4032 | 217 | for (j = 0; j < btf_vlen(func_proto); j++) |
4033 | 112 | if (param[j].type && !param[j].name_off) |
4034 | 29 | param[j].name_off = |
4035 | 29 | dummy_var->name_off; |
4036 | 105 | vs->type = dummy_var_btf_id; |
4037 | 105 | vt->info &= ~0xffff; |
4038 | 105 | vt->info |= BTF_FUNC_GLOBAL; |
4039 | 106 | } else { |
4040 | 106 | btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4041 | 106 | vt->type = int_btf_id; |
4042 | 106 | } |
4043 | 211 | vs->offset = off; |
4044 | 211 | vs->size = sizeof(int); |
4045 | 211 | } |
4046 | 36 | sec->size = off; |
4047 | 36 | } |
4048 | | |
4049 | 228 | if (kcfg_sec) { |
4050 | 192 | sec = kcfg_sec; |
4051 | | /* for kcfg externs calculate their offsets within a .kconfig map */ |
4052 | 192 | off = 0; |
4053 | 488 | for (i = 0; i < obj->nr_extern; i++) { |
4054 | 296 | ext = &obj->externs[i]; |
4055 | 296 | if (ext->type != EXT_KCFG) |
4056 | 0 | continue; |
4057 | | |
4058 | 296 | ext->kcfg.data_off = roundup(off, ext->kcfg.align); |
4059 | 296 | off = ext->kcfg.data_off + ext->kcfg.sz; |
4060 | 296 | pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n", |
4061 | 296 | i, ext->sym_idx, ext->kcfg.data_off, ext->name); |
4062 | 296 | } |
4063 | 192 | sec->size = off; |
4064 | 192 | n = btf_vlen(sec); |
4065 | 278 | for (i = 0; i < n; i++) { |
4066 | 238 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4067 | | |
4068 | 238 | t = btf__type_by_id(obj->btf, vs->type); |
4069 | 238 | ext_name = btf__name_by_offset(obj->btf, t->name_off); |
4070 | 238 | ext = find_extern_by_name(obj, ext_name); |
4071 | 238 | if (!ext) { |
4072 | 152 | pr_warn("failed to find extern definition for BTF var '%s'\n", |
4073 | 152 | ext_name); |
4074 | 152 | return -ESRCH; |
4075 | 152 | } |
4076 | 86 | btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4077 | 86 | vs->offset = ext->kcfg.data_off; |
4078 | 86 | } |
4079 | 192 | } |
4080 | 76 | return 0; |
4081 | 228 | } |
4082 | | |
4083 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog) |
4084 | 8.80k | { |
4085 | 8.80k | return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; |
4086 | 8.80k | } |
4087 | | |
4088 | | struct bpf_program * |
4089 | | bpf_object__find_program_by_name(const struct bpf_object *obj, |
4090 | | const char *name) |
4091 | 0 | { |
4092 | 0 | struct bpf_program *prog; |
4093 | |
|
4094 | 0 | bpf_object__for_each_program(prog, obj) { |
4095 | 0 | if (prog_is_subprog(obj, prog)) |
4096 | 0 | continue; |
4097 | 0 | if (!strcmp(prog->name, name)) |
4098 | 0 | return prog; |
4099 | 0 | } |
4100 | 0 | return errno = ENOENT, NULL; |
4101 | 0 | } |
4102 | | |
4103 | | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
4104 | | int shndx) |
4105 | 465 | { |
4106 | 465 | switch (obj->efile.secs[shndx].sec_type) { |
4107 | 45 | case SEC_BSS: |
4108 | 342 | case SEC_DATA: |
4109 | 463 | case SEC_RODATA: |
4110 | 463 | return true; |
4111 | 2 | default: |
4112 | 2 | return false; |
4113 | 465 | } |
4114 | 465 | } |
4115 | | |
4116 | | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
4117 | | int shndx) |
4118 | 21 | { |
4119 | 21 | return shndx == obj->efile.btf_maps_shndx; |
4120 | 21 | } |
4121 | | |
4122 | | static enum libbpf_map_type |
4123 | | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
4124 | 486 | { |
4125 | 486 | if (shndx == obj->efile.symbols_shndx) |
4126 | 2 | return LIBBPF_MAP_KCONFIG; |
4127 | | |
4128 | 484 | switch (obj->efile.secs[shndx].sec_type) { |
4129 | 45 | case SEC_BSS: |
4130 | 45 | return LIBBPF_MAP_BSS; |
4131 | 297 | case SEC_DATA: |
4132 | 297 | return LIBBPF_MAP_DATA; |
4133 | 121 | case SEC_RODATA: |
4134 | 121 | return LIBBPF_MAP_RODATA; |
4135 | 21 | default: |
4136 | 21 | return LIBBPF_MAP_UNSPEC; |
4137 | 484 | } |
4138 | 484 | } |
4139 | | |
4140 | | static int bpf_program__record_reloc(struct bpf_program *prog, |
4141 | | struct reloc_desc *reloc_desc, |
4142 | | __u32 insn_idx, const char *sym_name, |
4143 | | const Elf64_Sym *sym, const Elf64_Rel *rel) |
4144 | 981 | { |
4145 | 981 | struct bpf_insn *insn = &prog->insns[insn_idx]; |
4146 | 981 | size_t map_idx, nr_maps = prog->obj->nr_maps; |
4147 | 981 | struct bpf_object *obj = prog->obj; |
4148 | 981 | __u32 shdr_idx = sym->st_shndx; |
4149 | 981 | enum libbpf_map_type type; |
4150 | 981 | const char *sym_sec_name; |
4151 | 981 | struct bpf_map *map; |
4152 | | |
4153 | 981 | if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) { |
4154 | 56 | pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n", |
4155 | 56 | prog->name, sym_name, insn_idx, insn->code); |
4156 | 56 | return -LIBBPF_ERRNO__RELOC; |
4157 | 56 | } |
4158 | | |
4159 | 925 | if (sym_is_extern(sym)) { |
4160 | 2 | int sym_idx = ELF64_R_SYM(rel->r_info); |
4161 | 2 | int i, n = obj->nr_extern; |
4162 | 2 | struct extern_desc *ext; |
4163 | | |
4164 | 2 | for (i = 0; i < n; i++) { |
4165 | 0 | ext = &obj->externs[i]; |
4166 | 0 | if (ext->sym_idx == sym_idx) |
4167 | 0 | break; |
4168 | 0 | } |
4169 | 2 | if (i >= n) { |
4170 | 2 | pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n", |
4171 | 2 | prog->name, sym_name, sym_idx); |
4172 | 2 | return -LIBBPF_ERRNO__RELOC; |
4173 | 2 | } |
4174 | 0 | pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n", |
4175 | 0 | prog->name, i, ext->name, ext->sym_idx, insn_idx); |
4176 | 0 | if (insn->code == (BPF_JMP | BPF_CALL)) |
4177 | 0 | reloc_desc->type = RELO_EXTERN_CALL; |
4178 | 0 | else |
4179 | 0 | reloc_desc->type = RELO_EXTERN_LD64; |
4180 | 0 | reloc_desc->insn_idx = insn_idx; |
4181 | 0 | reloc_desc->ext_idx = i; |
4182 | 0 | return 0; |
4183 | 2 | } |
4184 | | |
4185 | | /* sub-program call relocation */ |
4186 | 923 | if (is_call_insn(insn)) { |
4187 | 141 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
4188 | 6 | pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); |
4189 | 6 | return -LIBBPF_ERRNO__RELOC; |
4190 | 6 | } |
4191 | | /* text_shndx can be 0, if no default "main" program exists */ |
4192 | 135 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
4193 | 14 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4194 | 14 | pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n", |
4195 | 14 | prog->name, sym_name, sym_sec_name); |
4196 | 14 | return -LIBBPF_ERRNO__RELOC; |
4197 | 14 | } |
4198 | 121 | if (sym->st_value % BPF_INSN_SZ) { |
4199 | 3 | pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n", |
4200 | 3 | prog->name, sym_name, (size_t)sym->st_value); |
4201 | 3 | return -LIBBPF_ERRNO__RELOC; |
4202 | 3 | } |
4203 | 118 | reloc_desc->type = RELO_CALL; |
4204 | 118 | reloc_desc->insn_idx = insn_idx; |
4205 | 118 | reloc_desc->sym_off = sym->st_value; |
4206 | 118 | return 0; |
4207 | 121 | } |
4208 | | |
4209 | 782 | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
4210 | 3 | pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n", |
4211 | 3 | prog->name, sym_name, shdr_idx); |
4212 | 3 | return -LIBBPF_ERRNO__RELOC; |
4213 | 3 | } |
4214 | | |
4215 | | /* loading subprog addresses */ |
4216 | 779 | if (sym_is_subprog(sym, obj->efile.text_shndx)) { |
4217 | | /* global_func: sym->st_value = offset in the section, insn->imm = 0. |
4218 | | * local_func: sym->st_value = 0, insn->imm = offset in the section. |
4219 | | */ |
4220 | 293 | if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { |
4221 | 2 | pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n", |
4222 | 2 | prog->name, sym_name, (size_t)sym->st_value, insn->imm); |
4223 | 2 | return -LIBBPF_ERRNO__RELOC; |
4224 | 2 | } |
4225 | | |
4226 | 291 | reloc_desc->type = RELO_SUBPROG_ADDR; |
4227 | 291 | reloc_desc->insn_idx = insn_idx; |
4228 | 291 | reloc_desc->sym_off = sym->st_value; |
4229 | 291 | return 0; |
4230 | 293 | } |
4231 | | |
4232 | 486 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
4233 | 486 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4234 | | |
4235 | | /* generic map reference relocation */ |
4236 | 486 | if (type == LIBBPF_MAP_UNSPEC) { |
4237 | 21 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
4238 | 21 | pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n", |
4239 | 21 | prog->name, sym_name, sym_sec_name); |
4240 | 21 | return -LIBBPF_ERRNO__RELOC; |
4241 | 21 | } |
4242 | 0 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4243 | 0 | map = &obj->maps[map_idx]; |
4244 | 0 | if (map->libbpf_type != type || |
4245 | 0 | map->sec_idx != sym->st_shndx || |
4246 | 0 | map->sec_offset != sym->st_value) |
4247 | 0 | continue; |
4248 | 0 | pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n", |
4249 | 0 | prog->name, map_idx, map->name, map->sec_idx, |
4250 | 0 | map->sec_offset, insn_idx); |
4251 | 0 | break; |
4252 | 0 | } |
4253 | 0 | if (map_idx >= nr_maps) { |
4254 | 0 | pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n", |
4255 | 0 | prog->name, sym_sec_name, (size_t)sym->st_value); |
4256 | 0 | return -LIBBPF_ERRNO__RELOC; |
4257 | 0 | } |
4258 | 0 | reloc_desc->type = RELO_LD64; |
4259 | 0 | reloc_desc->insn_idx = insn_idx; |
4260 | 0 | reloc_desc->map_idx = map_idx; |
4261 | 0 | reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ |
4262 | 0 | return 0; |
4263 | 0 | } |
4264 | | |
4265 | | /* global data map relocation */ |
4266 | 465 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
4267 | 2 | pr_warn("prog '%s': bad data relo against section '%s'\n", |
4268 | 2 | prog->name, sym_sec_name); |
4269 | 2 | return -LIBBPF_ERRNO__RELOC; |
4270 | 2 | } |
4271 | 1.01k | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4272 | 1.00k | map = &obj->maps[map_idx]; |
4273 | 1.00k | if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) |
4274 | 547 | continue; |
4275 | 461 | pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n", |
4276 | 461 | prog->name, map_idx, map->name, map->sec_idx, |
4277 | 461 | map->sec_offset, insn_idx); |
4278 | 461 | break; |
4279 | 1.00k | } |
4280 | 463 | if (map_idx >= nr_maps) { |
4281 | 2 | pr_warn("prog '%s': data relo failed to find map for section '%s'\n", |
4282 | 2 | prog->name, sym_sec_name); |
4283 | 2 | return -LIBBPF_ERRNO__RELOC; |
4284 | 2 | } |
4285 | | |
4286 | 461 | reloc_desc->type = RELO_DATA; |
4287 | 461 | reloc_desc->insn_idx = insn_idx; |
4288 | 461 | reloc_desc->map_idx = map_idx; |
4289 | 461 | reloc_desc->sym_off = sym->st_value; |
4290 | 461 | return 0; |
4291 | 463 | } |
4292 | | |
4293 | | static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx) |
4294 | 1.43k | { |
4295 | 1.43k | return insn_idx >= prog->sec_insn_off && |
4296 | 1.43k | insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; |
4297 | 1.43k | } |
4298 | | |
4299 | | static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj, |
4300 | | size_t sec_idx, size_t insn_idx) |
4301 | 6.16k | { |
4302 | 6.16k | int l = 0, r = obj->nr_programs - 1, m; |
4303 | 6.16k | struct bpf_program *prog; |
4304 | | |
4305 | 6.16k | if (!obj->nr_programs) |
4306 | 4.31k | return NULL; |
4307 | | |
4308 | 2.93k | while (l < r) { |
4309 | 1.08k | m = l + (r - l + 1) / 2; |
4310 | 1.08k | prog = &obj->programs[m]; |
4311 | | |
4312 | 1.08k | if (prog->sec_idx < sec_idx || |
4313 | 1.08k | (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) |
4314 | 516 | l = m; |
4315 | 565 | else |
4316 | 565 | r = m - 1; |
4317 | 1.08k | } |
4318 | | /* matching program could be at index l, but it still might be the |
4319 | | * wrong one, so we need to double check conditions for the last time |
4320 | | */ |
4321 | 1.85k | prog = &obj->programs[l]; |
4322 | 1.85k | if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) |
4323 | 981 | return prog; |
4324 | 874 | return NULL; |
4325 | 1.85k | } |
4326 | | |
4327 | | static int |
4328 | | bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data) |
4329 | 1.37k | { |
4330 | 1.37k | const char *relo_sec_name, *sec_name; |
4331 | 1.37k | size_t sec_idx = shdr->sh_info, sym_idx; |
4332 | 1.37k | struct bpf_program *prog; |
4333 | 1.37k | struct reloc_desc *relos; |
4334 | 1.37k | int err, i, nrels; |
4335 | 1.37k | const char *sym_name; |
4336 | 1.37k | __u32 insn_idx; |
4337 | 1.37k | Elf_Scn *scn; |
4338 | 1.37k | Elf_Data *scn_data; |
4339 | 1.37k | Elf64_Sym *sym; |
4340 | 1.37k | Elf64_Rel *rel; |
4341 | | |
4342 | 1.37k | if (sec_idx >= obj->efile.sec_cnt) |
4343 | 0 | return -EINVAL; |
4344 | | |
4345 | 1.37k | scn = elf_sec_by_idx(obj, sec_idx); |
4346 | 1.37k | scn_data = elf_sec_data(obj, scn); |
4347 | | |
4348 | 1.37k | relo_sec_name = elf_sec_str(obj, shdr->sh_name); |
4349 | 1.37k | sec_name = elf_sec_name(obj, scn); |
4350 | 1.37k | if (!relo_sec_name || !sec_name) |
4351 | 19 | return -EINVAL; |
4352 | | |
4353 | 1.35k | pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n", |
4354 | 1.35k | relo_sec_name, sec_idx, sec_name); |
4355 | 1.35k | nrels = shdr->sh_size / shdr->sh_entsize; |
4356 | | |
4357 | 7.41k | for (i = 0; i < nrels; i++) { |
4358 | 6.54k | rel = elf_rel_by_idx(data, i); |
4359 | 6.54k | if (!rel) { |
4360 | 0 | pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i); |
4361 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4362 | 0 | } |
4363 | | |
4364 | 6.54k | sym_idx = ELF64_R_SYM(rel->r_info); |
4365 | 6.54k | sym = elf_sym_by_idx(obj, sym_idx); |
4366 | 6.54k | if (!sym) { |
4367 | 165 | pr_warn("sec '%s': symbol #%zu not found for relo #%d\n", |
4368 | 165 | relo_sec_name, sym_idx, i); |
4369 | 165 | return -LIBBPF_ERRNO__FORMAT; |
4370 | 165 | } |
4371 | | |
4372 | 6.37k | if (sym->st_shndx >= obj->efile.sec_cnt) { |
4373 | 18 | pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n", |
4374 | 18 | relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); |
4375 | 18 | return -LIBBPF_ERRNO__FORMAT; |
4376 | 18 | } |
4377 | | |
4378 | 6.35k | if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { |
4379 | 190 | pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n", |
4380 | 190 | relo_sec_name, (size_t)rel->r_offset, i); |
4381 | 190 | return -LIBBPF_ERRNO__FORMAT; |
4382 | 190 | } |
4383 | | |
4384 | 6.16k | insn_idx = rel->r_offset / BPF_INSN_SZ; |
4385 | | /* relocations against static functions are recorded as |
4386 | | * relocations against the section that contains a function; |
4387 | | * in such case, symbol will be STT_SECTION and sym.st_name |
4388 | | * will point to empty string (0), so fetch section name |
4389 | | * instead |
4390 | | */ |
4391 | 6.16k | if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) |
4392 | 459 | sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); |
4393 | 5.71k | else |
4394 | 5.71k | sym_name = elf_sym_str(obj, sym->st_name); |
4395 | 6.16k | sym_name = sym_name ?: "<?"; |
4396 | | |
4397 | 6.16k | pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n", |
4398 | 6.16k | relo_sec_name, i, insn_idx, sym_name); |
4399 | | |
4400 | 6.16k | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
4401 | 6.16k | if (!prog) { |
4402 | 5.18k | pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n", |
4403 | 5.18k | relo_sec_name, i, sec_name, insn_idx); |
4404 | 5.18k | continue; |
4405 | 5.18k | } |
4406 | | |
4407 | 981 | relos = libbpf_reallocarray(prog->reloc_desc, |
4408 | 981 | prog->nr_reloc + 1, sizeof(*relos)); |
4409 | 981 | if (!relos) |
4410 | 0 | return -ENOMEM; |
4411 | 981 | prog->reloc_desc = relos; |
4412 | | |
4413 | | /* adjust insn_idx to local BPF program frame of reference */ |
4414 | 981 | insn_idx -= prog->sec_insn_off; |
4415 | 981 | err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], |
4416 | 981 | insn_idx, sym_name, sym, rel); |
4417 | 981 | if (err) |
4418 | 111 | return err; |
4419 | | |
4420 | 870 | prog->nr_reloc++; |
4421 | 870 | } |
4422 | 868 | return 0; |
4423 | 1.35k | } |
4424 | | |
4425 | | static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map) |
4426 | 1.42k | { |
4427 | 1.42k | int id; |
4428 | | |
4429 | 1.42k | if (!obj->btf) |
4430 | 1.04k | return -ENOENT; |
4431 | | |
4432 | | /* if it's BTF-defined map, we don't need to search for type IDs. |
4433 | | * For struct_ops map, it does not need btf_key_type_id and |
4434 | | * btf_value_type_id. |
4435 | | */ |
4436 | 377 | if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) |
4437 | 0 | return 0; |
4438 | | |
4439 | | /* |
4440 | | * LLVM annotates global data differently in BTF, that is, |
4441 | | * only as '.data', '.bss' or '.rodata'. |
4442 | | */ |
4443 | 377 | if (!bpf_map__is_internal(map)) |
4444 | 0 | return -ENOENT; |
4445 | | |
4446 | 377 | id = btf__find_by_name(obj->btf, map->real_name); |
4447 | 377 | if (id < 0) |
4448 | 190 | return id; |
4449 | | |
4450 | 187 | map->btf_key_type_id = 0; |
4451 | 187 | map->btf_value_type_id = id; |
4452 | 187 | return 0; |
4453 | 377 | } |
4454 | | |
4455 | | static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) |
4456 | 0 | { |
4457 | 0 | char file[PATH_MAX], buff[4096]; |
4458 | 0 | FILE *fp; |
4459 | 0 | __u32 val; |
4460 | 0 | int err; |
4461 | |
|
4462 | 0 | snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd); |
4463 | 0 | memset(info, 0, sizeof(*info)); |
4464 | |
|
4465 | 0 | fp = fopen(file, "re"); |
4466 | 0 | if (!fp) { |
4467 | 0 | err = -errno; |
4468 | 0 | pr_warn("failed to open %s: %d. No procfs support?\n", file, |
4469 | 0 | err); |
4470 | 0 | return err; |
4471 | 0 | } |
4472 | | |
4473 | 0 | while (fgets(buff, sizeof(buff), fp)) { |
4474 | 0 | if (sscanf(buff, "map_type:\t%u", &val) == 1) |
4475 | 0 | info->type = val; |
4476 | 0 | else if (sscanf(buff, "key_size:\t%u", &val) == 1) |
4477 | 0 | info->key_size = val; |
4478 | 0 | else if (sscanf(buff, "value_size:\t%u", &val) == 1) |
4479 | 0 | info->value_size = val; |
4480 | 0 | else if (sscanf(buff, "max_entries:\t%u", &val) == 1) |
4481 | 0 | info->max_entries = val; |
4482 | 0 | else if (sscanf(buff, "map_flags:\t%i", &val) == 1) |
4483 | 0 | info->map_flags = val; |
4484 | 0 | } |
4485 | |
|
4486 | 0 | fclose(fp); |
4487 | |
|
4488 | 0 | return 0; |
4489 | 0 | } |
4490 | | |
4491 | | bool bpf_map__autocreate(const struct bpf_map *map) |
4492 | 0 | { |
4493 | 0 | return map->autocreate; |
4494 | 0 | } |
4495 | | |
4496 | | int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) |
4497 | 0 | { |
4498 | 0 | if (map->obj->loaded) |
4499 | 0 | return libbpf_err(-EBUSY); |
4500 | | |
4501 | 0 | map->autocreate = autocreate; |
4502 | 0 | return 0; |
4503 | 0 | } |
4504 | | |
4505 | | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
4506 | 0 | { |
4507 | 0 | struct bpf_map_info info; |
4508 | 0 | __u32 len = sizeof(info), name_len; |
4509 | 0 | int new_fd, err; |
4510 | 0 | char *new_name; |
4511 | |
|
4512 | 0 | memset(&info, 0, len); |
4513 | 0 | err = bpf_map_get_info_by_fd(fd, &info, &len); |
4514 | 0 | if (err && errno == EINVAL) |
4515 | 0 | err = bpf_get_map_info_from_fdinfo(fd, &info); |
4516 | 0 | if (err) |
4517 | 0 | return libbpf_err(err); |
4518 | | |
4519 | 0 | name_len = strlen(info.name); |
4520 | 0 | if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) |
4521 | 0 | new_name = strdup(map->name); |
4522 | 0 | else |
4523 | 0 | new_name = strdup(info.name); |
4524 | |
|
4525 | 0 | if (!new_name) |
4526 | 0 | return libbpf_err(-errno); |
4527 | | |
4528 | | /* |
4529 | | * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. |
4530 | | * This is similar to what we do in ensure_good_fd(), but without |
4531 | | * closing original FD. |
4532 | | */ |
4533 | 0 | new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
4534 | 0 | if (new_fd < 0) { |
4535 | 0 | err = -errno; |
4536 | 0 | goto err_free_new_name; |
4537 | 0 | } |
4538 | | |
4539 | 0 | err = zclose(map->fd); |
4540 | 0 | if (err) { |
4541 | 0 | err = -errno; |
4542 | 0 | goto err_close_new_fd; |
4543 | 0 | } |
4544 | 0 | free(map->name); |
4545 | |
|
4546 | 0 | map->fd = new_fd; |
4547 | 0 | map->name = new_name; |
4548 | 0 | map->def.type = info.type; |
4549 | 0 | map->def.key_size = info.key_size; |
4550 | 0 | map->def.value_size = info.value_size; |
4551 | 0 | map->def.max_entries = info.max_entries; |
4552 | 0 | map->def.map_flags = info.map_flags; |
4553 | 0 | map->btf_key_type_id = info.btf_key_type_id; |
4554 | 0 | map->btf_value_type_id = info.btf_value_type_id; |
4555 | 0 | map->reused = true; |
4556 | 0 | map->map_extra = info.map_extra; |
4557 | |
|
4558 | 0 | return 0; |
4559 | | |
4560 | 0 | err_close_new_fd: |
4561 | 0 | close(new_fd); |
4562 | 0 | err_free_new_name: |
4563 | 0 | free(new_name); |
4564 | 0 | return libbpf_err(err); |
4565 | 0 | } |
4566 | | |
4567 | | __u32 bpf_map__max_entries(const struct bpf_map *map) |
4568 | 0 | { |
4569 | 0 | return map->def.max_entries; |
4570 | 0 | } |
4571 | | |
4572 | | struct bpf_map *bpf_map__inner_map(struct bpf_map *map) |
4573 | 0 | { |
4574 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) |
4575 | 0 | return errno = EINVAL, NULL; |
4576 | | |
4577 | 0 | return map->inner_map; |
4578 | 0 | } |
4579 | | |
4580 | | int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries) |
4581 | 0 | { |
4582 | 0 | if (map->obj->loaded) |
4583 | 0 | return libbpf_err(-EBUSY); |
4584 | | |
4585 | 0 | map->def.max_entries = max_entries; |
4586 | | |
4587 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
4588 | 0 | if (map_is_ringbuf(map)) |
4589 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
4590 | |
|
4591 | 0 | return 0; |
4592 | 0 | } |
4593 | | |
4594 | | static int |
4595 | | bpf_object__probe_loading(struct bpf_object *obj) |
4596 | 0 | { |
4597 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
4598 | 0 | struct bpf_insn insns[] = { |
4599 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4600 | 0 | BPF_EXIT_INSN(), |
4601 | 0 | }; |
4602 | 0 | int ret, insn_cnt = ARRAY_SIZE(insns); |
4603 | |
|
4604 | 0 | if (obj->gen_loader) |
4605 | 0 | return 0; |
4606 | | |
4607 | 0 | ret = bump_rlimit_memlock(); |
4608 | 0 | if (ret) |
4609 | 0 | pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret); |
4610 | | |
4611 | | /* make sure basic loading works */ |
4612 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL); |
4613 | 0 | if (ret < 0) |
4614 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL); |
4615 | 0 | if (ret < 0) { |
4616 | 0 | ret = errno; |
4617 | 0 | cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); |
4618 | 0 | pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF " |
4619 | 0 | "program. Make sure your kernel supports BPF " |
4620 | 0 | "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is " |
4621 | 0 | "set to big enough value.\n", __func__, cp, ret); |
4622 | 0 | return -ret; |
4623 | 0 | } |
4624 | 0 | close(ret); |
4625 | |
|
4626 | 0 | return 0; |
4627 | 0 | } |
4628 | | |
4629 | | static int probe_fd(int fd) |
4630 | 0 | { |
4631 | 0 | if (fd >= 0) |
4632 | 0 | close(fd); |
4633 | 0 | return fd >= 0; |
4634 | 0 | } |
4635 | | |
4636 | | static int probe_kern_prog_name(void) |
4637 | 0 | { |
4638 | 0 | const size_t attr_sz = offsetofend(union bpf_attr, prog_name); |
4639 | 0 | struct bpf_insn insns[] = { |
4640 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4641 | 0 | BPF_EXIT_INSN(), |
4642 | 0 | }; |
4643 | 0 | union bpf_attr attr; |
4644 | 0 | int ret; |
4645 | |
|
4646 | 0 | memset(&attr, 0, attr_sz); |
4647 | 0 | attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
4648 | 0 | attr.license = ptr_to_u64("GPL"); |
4649 | 0 | attr.insns = ptr_to_u64(insns); |
4650 | 0 | attr.insn_cnt = (__u32)ARRAY_SIZE(insns); |
4651 | 0 | libbpf_strlcpy(attr.prog_name, "libbpf_nametest", sizeof(attr.prog_name)); |
4652 | | |
4653 | | /* make sure loading with name works */ |
4654 | 0 | ret = sys_bpf_prog_load(&attr, attr_sz, PROG_LOAD_ATTEMPTS); |
4655 | 0 | return probe_fd(ret); |
4656 | 0 | } |
4657 | | |
4658 | | static int probe_kern_global_data(void) |
4659 | 0 | { |
4660 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
4661 | 0 | struct bpf_insn insns[] = { |
4662 | 0 | BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16), |
4663 | 0 | BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42), |
4664 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4665 | 0 | BPF_EXIT_INSN(), |
4666 | 0 | }; |
4667 | 0 | int ret, map, insn_cnt = ARRAY_SIZE(insns); |
4668 | |
|
4669 | 0 | map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL); |
4670 | 0 | if (map < 0) { |
4671 | 0 | ret = -errno; |
4672 | 0 | cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); |
4673 | 0 | pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n", |
4674 | 0 | __func__, cp, -ret); |
4675 | 0 | return ret; |
4676 | 0 | } |
4677 | | |
4678 | 0 | insns[0].imm = map; |
4679 | |
|
4680 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL); |
4681 | 0 | close(map); |
4682 | 0 | return probe_fd(ret); |
4683 | 0 | } |
4684 | | |
4685 | | static int probe_kern_btf(void) |
4686 | 0 | { |
4687 | 0 | static const char strs[] = "\0int"; |
4688 | 0 | __u32 types[] = { |
4689 | | /* int */ |
4690 | 0 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), |
4691 | 0 | }; |
4692 | |
|
4693 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4694 | 0 | strs, sizeof(strs))); |
4695 | 0 | } |
4696 | | |
4697 | | static int probe_kern_btf_func(void) |
4698 | 0 | { |
4699 | 0 | static const char strs[] = "\0int\0x\0a"; |
4700 | | /* void x(int a) {} */ |
4701 | 0 | __u32 types[] = { |
4702 | | /* int */ |
4703 | 0 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
4704 | | /* FUNC_PROTO */ /* [2] */ |
4705 | 0 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0), |
4706 | 0 | BTF_PARAM_ENC(7, 1), |
4707 | | /* FUNC x */ /* [3] */ |
4708 | 0 | BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), |
4709 | 0 | }; |
4710 | |
|
4711 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4712 | 0 | strs, sizeof(strs))); |
4713 | 0 | } |
4714 | | |
4715 | | static int probe_kern_btf_func_global(void) |
4716 | 0 | { |
4717 | 0 | static const char strs[] = "\0int\0x\0a"; |
4718 | | /* static void x(int a) {} */ |
4719 | 0 | __u32 types[] = { |
4720 | | /* int */ |
4721 | 0 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
4722 | | /* FUNC_PROTO */ /* [2] */ |
4723 | 0 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0), |
4724 | 0 | BTF_PARAM_ENC(7, 1), |
4725 | | /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */ |
4726 | 0 | BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2), |
4727 | 0 | }; |
4728 | |
|
4729 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4730 | 0 | strs, sizeof(strs))); |
4731 | 0 | } |
4732 | | |
4733 | | static int probe_kern_btf_datasec(void) |
4734 | 0 | { |
4735 | 0 | static const char strs[] = "\0x\0.data"; |
4736 | | /* static int a; */ |
4737 | 0 | __u32 types[] = { |
4738 | | /* int */ |
4739 | 0 | BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
4740 | | /* VAR x */ /* [2] */ |
4741 | 0 | BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), |
4742 | 0 | BTF_VAR_STATIC, |
4743 | | /* DATASEC val */ /* [3] */ |
4744 | 0 | BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), |
4745 | 0 | BTF_VAR_SECINFO_ENC(2, 0, 4), |
4746 | 0 | }; |
4747 | |
|
4748 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4749 | 0 | strs, sizeof(strs))); |
4750 | 0 | } |
4751 | | |
4752 | | static int probe_kern_btf_float(void) |
4753 | 0 | { |
4754 | 0 | static const char strs[] = "\0float"; |
4755 | 0 | __u32 types[] = { |
4756 | | /* float */ |
4757 | 0 | BTF_TYPE_FLOAT_ENC(1, 4), |
4758 | 0 | }; |
4759 | |
|
4760 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4761 | 0 | strs, sizeof(strs))); |
4762 | 0 | } |
4763 | | |
4764 | | static int probe_kern_btf_decl_tag(void) |
4765 | 0 | { |
4766 | 0 | static const char strs[] = "\0tag"; |
4767 | 0 | __u32 types[] = { |
4768 | | /* int */ |
4769 | 0 | BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
4770 | | /* VAR x */ /* [2] */ |
4771 | 0 | BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), |
4772 | 0 | BTF_VAR_STATIC, |
4773 | | /* attr */ |
4774 | 0 | BTF_TYPE_DECL_TAG_ENC(1, 2, -1), |
4775 | 0 | }; |
4776 | |
|
4777 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4778 | 0 | strs, sizeof(strs))); |
4779 | 0 | } |
4780 | | |
4781 | | static int probe_kern_btf_type_tag(void) |
4782 | 0 | { |
4783 | 0 | static const char strs[] = "\0tag"; |
4784 | 0 | __u32 types[] = { |
4785 | | /* int */ |
4786 | 0 | BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
4787 | | /* attr */ |
4788 | 0 | BTF_TYPE_TYPE_TAG_ENC(1, 1), /* [2] */ |
4789 | | /* ptr */ |
4790 | 0 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2), /* [3] */ |
4791 | 0 | }; |
4792 | |
|
4793 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4794 | 0 | strs, sizeof(strs))); |
4795 | 0 | } |
4796 | | |
4797 | | static int probe_kern_array_mmap(void) |
4798 | 0 | { |
4799 | 0 | LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); |
4800 | 0 | int fd; |
4801 | |
|
4802 | 0 | fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts); |
4803 | 0 | return probe_fd(fd); |
4804 | 0 | } |
4805 | | |
4806 | | static int probe_kern_exp_attach_type(void) |
4807 | 0 | { |
4808 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, opts, .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE); |
4809 | 0 | struct bpf_insn insns[] = { |
4810 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4811 | 0 | BPF_EXIT_INSN(), |
4812 | 0 | }; |
4813 | 0 | int fd, insn_cnt = ARRAY_SIZE(insns); |
4814 | | |
4815 | | /* use any valid combination of program type and (optional) |
4816 | | * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS) |
4817 | | * to see if kernel supports expected_attach_type field for |
4818 | | * BPF_PROG_LOAD command |
4819 | | */ |
4820 | 0 | fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns, insn_cnt, &opts); |
4821 | 0 | return probe_fd(fd); |
4822 | 0 | } |
4823 | | |
4824 | | static int probe_kern_probe_read_kernel(void) |
4825 | 0 | { |
4826 | 0 | struct bpf_insn insns[] = { |
4827 | 0 | BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */ |
4828 | 0 | BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */ |
4829 | 0 | BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */ |
4830 | 0 | BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */ |
4831 | 0 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel), |
4832 | 0 | BPF_EXIT_INSN(), |
4833 | 0 | }; |
4834 | 0 | int fd, insn_cnt = ARRAY_SIZE(insns); |
4835 | |
|
4836 | 0 | fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL); |
4837 | 0 | return probe_fd(fd); |
4838 | 0 | } |
4839 | | |
4840 | | static int probe_prog_bind_map(void) |
4841 | 0 | { |
4842 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
4843 | 0 | struct bpf_insn insns[] = { |
4844 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4845 | 0 | BPF_EXIT_INSN(), |
4846 | 0 | }; |
4847 | 0 | int ret, map, prog, insn_cnt = ARRAY_SIZE(insns); |
4848 | |
|
4849 | 0 | map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL); |
4850 | 0 | if (map < 0) { |
4851 | 0 | ret = -errno; |
4852 | 0 | cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); |
4853 | 0 | pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n", |
4854 | 0 | __func__, cp, -ret); |
4855 | 0 | return ret; |
4856 | 0 | } |
4857 | | |
4858 | 0 | prog = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL); |
4859 | 0 | if (prog < 0) { |
4860 | 0 | close(map); |
4861 | 0 | return 0; |
4862 | 0 | } |
4863 | | |
4864 | 0 | ret = bpf_prog_bind_map(prog, map, NULL); |
4865 | |
|
4866 | 0 | close(map); |
4867 | 0 | close(prog); |
4868 | |
|
4869 | 0 | return ret >= 0; |
4870 | 0 | } |
4871 | | |
4872 | | static int probe_module_btf(void) |
4873 | 0 | { |
4874 | 0 | static const char strs[] = "\0int"; |
4875 | 0 | __u32 types[] = { |
4876 | | /* int */ |
4877 | 0 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), |
4878 | 0 | }; |
4879 | 0 | struct bpf_btf_info info; |
4880 | 0 | __u32 len = sizeof(info); |
4881 | 0 | char name[16]; |
4882 | 0 | int fd, err; |
4883 | |
|
4884 | 0 | fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs)); |
4885 | 0 | if (fd < 0) |
4886 | 0 | return 0; /* BTF not supported at all */ |
4887 | | |
4888 | 0 | memset(&info, 0, sizeof(info)); |
4889 | 0 | info.name = ptr_to_u64(name); |
4890 | 0 | info.name_len = sizeof(name); |
4891 | | |
4892 | | /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer; |
4893 | | * kernel's module BTF support coincides with support for |
4894 | | * name/name_len fields in struct bpf_btf_info. |
4895 | | */ |
4896 | 0 | err = bpf_btf_get_info_by_fd(fd, &info, &len); |
4897 | 0 | close(fd); |
4898 | 0 | return !err; |
4899 | 0 | } |
4900 | | |
4901 | | static int probe_perf_link(void) |
4902 | 0 | { |
4903 | 0 | struct bpf_insn insns[] = { |
4904 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4905 | 0 | BPF_EXIT_INSN(), |
4906 | 0 | }; |
4907 | 0 | int prog_fd, link_fd, err; |
4908 | |
|
4909 | 0 | prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", |
4910 | 0 | insns, ARRAY_SIZE(insns), NULL); |
4911 | 0 | if (prog_fd < 0) |
4912 | 0 | return -errno; |
4913 | | |
4914 | | /* use invalid perf_event FD to get EBADF, if link is supported; |
4915 | | * otherwise EINVAL should be returned |
4916 | | */ |
4917 | 0 | link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL); |
4918 | 0 | err = -errno; /* close() can clobber errno */ |
4919 | |
|
4920 | 0 | if (link_fd >= 0) |
4921 | 0 | close(link_fd); |
4922 | 0 | close(prog_fd); |
4923 | |
|
4924 | 0 | return link_fd < 0 && err == -EBADF; |
4925 | 0 | } |
4926 | | |
4927 | | static int probe_uprobe_multi_link(void) |
4928 | 0 | { |
4929 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, load_opts, |
4930 | 0 | .expected_attach_type = BPF_TRACE_UPROBE_MULTI, |
4931 | 0 | ); |
4932 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_opts); |
4933 | 0 | struct bpf_insn insns[] = { |
4934 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4935 | 0 | BPF_EXIT_INSN(), |
4936 | 0 | }; |
4937 | 0 | int prog_fd, link_fd, err; |
4938 | 0 | unsigned long offset = 0; |
4939 | |
|
4940 | 0 | prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", |
4941 | 0 | insns, ARRAY_SIZE(insns), &load_opts); |
4942 | 0 | if (prog_fd < 0) |
4943 | 0 | return -errno; |
4944 | | |
4945 | | /* Creating uprobe in '/' binary should fail with -EBADF. */ |
4946 | 0 | link_opts.uprobe_multi.path = "/"; |
4947 | 0 | link_opts.uprobe_multi.offsets = &offset; |
4948 | 0 | link_opts.uprobe_multi.cnt = 1; |
4949 | |
|
4950 | 0 | link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts); |
4951 | 0 | err = -errno; /* close() can clobber errno */ |
4952 | |
|
4953 | 0 | if (link_fd >= 0) |
4954 | 0 | close(link_fd); |
4955 | 0 | close(prog_fd); |
4956 | |
|
4957 | 0 | return link_fd < 0 && err == -EBADF; |
4958 | 0 | } |
4959 | | |
4960 | | static int probe_kern_bpf_cookie(void) |
4961 | 0 | { |
4962 | 0 | struct bpf_insn insns[] = { |
4963 | 0 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_attach_cookie), |
4964 | 0 | BPF_EXIT_INSN(), |
4965 | 0 | }; |
4966 | 0 | int ret, insn_cnt = ARRAY_SIZE(insns); |
4967 | |
|
4968 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", insns, insn_cnt, NULL); |
4969 | 0 | return probe_fd(ret); |
4970 | 0 | } |
4971 | | |
4972 | | static int probe_kern_btf_enum64(void) |
4973 | 0 | { |
4974 | 0 | static const char strs[] = "\0enum64"; |
4975 | 0 | __u32 types[] = { |
4976 | 0 | BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8), |
4977 | 0 | }; |
4978 | |
|
4979 | 0 | return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), |
4980 | 0 | strs, sizeof(strs))); |
4981 | 0 | } |
4982 | | |
4983 | | static int probe_kern_syscall_wrapper(void); |
4984 | | |
4985 | | enum kern_feature_result { |
4986 | | FEAT_UNKNOWN = 0, |
4987 | | FEAT_SUPPORTED = 1, |
4988 | | FEAT_MISSING = 2, |
4989 | | }; |
4990 | | |
4991 | | typedef int (*feature_probe_fn)(void); |
4992 | | |
4993 | | static struct kern_feature_desc { |
4994 | | const char *desc; |
4995 | | feature_probe_fn probe; |
4996 | | enum kern_feature_result res; |
4997 | | } feature_probes[__FEAT_CNT] = { |
4998 | | [FEAT_PROG_NAME] = { |
4999 | | "BPF program name", probe_kern_prog_name, |
5000 | | }, |
5001 | | [FEAT_GLOBAL_DATA] = { |
5002 | | "global variables", probe_kern_global_data, |
5003 | | }, |
5004 | | [FEAT_BTF] = { |
5005 | | "minimal BTF", probe_kern_btf, |
5006 | | }, |
5007 | | [FEAT_BTF_FUNC] = { |
5008 | | "BTF functions", probe_kern_btf_func, |
5009 | | }, |
5010 | | [FEAT_BTF_GLOBAL_FUNC] = { |
5011 | | "BTF global function", probe_kern_btf_func_global, |
5012 | | }, |
5013 | | [FEAT_BTF_DATASEC] = { |
5014 | | "BTF data section and variable", probe_kern_btf_datasec, |
5015 | | }, |
5016 | | [FEAT_ARRAY_MMAP] = { |
5017 | | "ARRAY map mmap()", probe_kern_array_mmap, |
5018 | | }, |
5019 | | [FEAT_EXP_ATTACH_TYPE] = { |
5020 | | "BPF_PROG_LOAD expected_attach_type attribute", |
5021 | | probe_kern_exp_attach_type, |
5022 | | }, |
5023 | | [FEAT_PROBE_READ_KERN] = { |
5024 | | "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel, |
5025 | | }, |
5026 | | [FEAT_PROG_BIND_MAP] = { |
5027 | | "BPF_PROG_BIND_MAP support", probe_prog_bind_map, |
5028 | | }, |
5029 | | [FEAT_MODULE_BTF] = { |
5030 | | "module BTF support", probe_module_btf, |
5031 | | }, |
5032 | | [FEAT_BTF_FLOAT] = { |
5033 | | "BTF_KIND_FLOAT support", probe_kern_btf_float, |
5034 | | }, |
5035 | | [FEAT_PERF_LINK] = { |
5036 | | "BPF perf link support", probe_perf_link, |
5037 | | }, |
5038 | | [FEAT_BTF_DECL_TAG] = { |
5039 | | "BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag, |
5040 | | }, |
5041 | | [FEAT_BTF_TYPE_TAG] = { |
5042 | | "BTF_KIND_TYPE_TAG support", probe_kern_btf_type_tag, |
5043 | | }, |
5044 | | [FEAT_MEMCG_ACCOUNT] = { |
5045 | | "memcg-based memory accounting", probe_memcg_account, |
5046 | | }, |
5047 | | [FEAT_BPF_COOKIE] = { |
5048 | | "BPF cookie support", probe_kern_bpf_cookie, |
5049 | | }, |
5050 | | [FEAT_BTF_ENUM64] = { |
5051 | | "BTF_KIND_ENUM64 support", probe_kern_btf_enum64, |
5052 | | }, |
5053 | | [FEAT_SYSCALL_WRAPPER] = { |
5054 | | "Kernel using syscall wrapper", probe_kern_syscall_wrapper, |
5055 | | }, |
5056 | | [FEAT_UPROBE_MULTI_LINK] = { |
5057 | | "BPF multi-uprobe link support", probe_uprobe_multi_link, |
5058 | | }, |
5059 | | }; |
5060 | | |
5061 | | bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) |
5062 | 0 | { |
5063 | 0 | struct kern_feature_desc *feat = &feature_probes[feat_id]; |
5064 | 0 | int ret; |
5065 | |
|
5066 | 0 | if (obj && obj->gen_loader) |
5067 | | /* To generate loader program assume the latest kernel |
5068 | | * to avoid doing extra prog_load, map_create syscalls. |
5069 | | */ |
5070 | 0 | return true; |
5071 | | |
5072 | 0 | if (READ_ONCE(feat->res) == FEAT_UNKNOWN) { |
5073 | 0 | ret = feat->probe(); |
5074 | 0 | if (ret > 0) { |
5075 | 0 | WRITE_ONCE(feat->res, FEAT_SUPPORTED); |
5076 | 0 | } else if (ret == 0) { |
5077 | 0 | WRITE_ONCE(feat->res, FEAT_MISSING); |
5078 | 0 | } else { |
5079 | 0 | pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret); |
5080 | 0 | WRITE_ONCE(feat->res, FEAT_MISSING); |
5081 | 0 | } |
5082 | 0 | } |
5083 | |
|
5084 | 0 | return READ_ONCE(feat->res) == FEAT_SUPPORTED; |
5085 | 0 | } |
5086 | | |
5087 | | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
5088 | 0 | { |
5089 | 0 | struct bpf_map_info map_info; |
5090 | 0 | char msg[STRERR_BUFSIZE]; |
5091 | 0 | __u32 map_info_len = sizeof(map_info); |
5092 | 0 | int err; |
5093 | |
|
5094 | 0 | memset(&map_info, 0, map_info_len); |
5095 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len); |
5096 | 0 | if (err && errno == EINVAL) |
5097 | 0 | err = bpf_get_map_info_from_fdinfo(map_fd, &map_info); |
5098 | 0 | if (err) { |
5099 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", map_fd, |
5100 | 0 | libbpf_strerror_r(errno, msg, sizeof(msg))); |
5101 | 0 | return false; |
5102 | 0 | } |
5103 | | |
5104 | 0 | return (map_info.type == map->def.type && |
5105 | 0 | map_info.key_size == map->def.key_size && |
5106 | 0 | map_info.value_size == map->def.value_size && |
5107 | 0 | map_info.max_entries == map->def.max_entries && |
5108 | 0 | map_info.map_flags == map->def.map_flags && |
5109 | 0 | map_info.map_extra == map->map_extra); |
5110 | 0 | } |
5111 | | |
5112 | | static int |
5113 | | bpf_object__reuse_map(struct bpf_map *map) |
5114 | 0 | { |
5115 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5116 | 0 | int err, pin_fd; |
5117 | |
|
5118 | 0 | pin_fd = bpf_obj_get(map->pin_path); |
5119 | 0 | if (pin_fd < 0) { |
5120 | 0 | err = -errno; |
5121 | 0 | if (err == -ENOENT) { |
5122 | 0 | pr_debug("found no pinned map to reuse at '%s'\n", |
5123 | 0 | map->pin_path); |
5124 | 0 | return 0; |
5125 | 0 | } |
5126 | | |
5127 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
5128 | 0 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
5129 | 0 | map->pin_path, cp); |
5130 | 0 | return err; |
5131 | 0 | } |
5132 | | |
5133 | 0 | if (!map_is_reuse_compat(map, pin_fd)) { |
5134 | 0 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
5135 | 0 | map->pin_path); |
5136 | 0 | close(pin_fd); |
5137 | 0 | return -EINVAL; |
5138 | 0 | } |
5139 | | |
5140 | 0 | err = bpf_map__reuse_fd(map, pin_fd); |
5141 | 0 | close(pin_fd); |
5142 | 0 | if (err) |
5143 | 0 | return err; |
5144 | | |
5145 | 0 | map->pinned = true; |
5146 | 0 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
5147 | |
|
5148 | 0 | return 0; |
5149 | 0 | } |
5150 | | |
5151 | | static int |
5152 | | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
5153 | 0 | { |
5154 | 0 | enum libbpf_map_type map_type = map->libbpf_type; |
5155 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5156 | 0 | int err, zero = 0; |
5157 | |
|
5158 | 0 | if (obj->gen_loader) { |
5159 | 0 | bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, |
5160 | 0 | map->mmaped, map->def.value_size); |
5161 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) |
5162 | 0 | bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); |
5163 | 0 | return 0; |
5164 | 0 | } |
5165 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); |
5166 | 0 | if (err) { |
5167 | 0 | err = -errno; |
5168 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5169 | 0 | pr_warn("Error setting initial map(%s) contents: %s\n", |
5170 | 0 | map->name, cp); |
5171 | 0 | return err; |
5172 | 0 | } |
5173 | | |
5174 | | /* Freeze .rodata and .kconfig map as read-only from syscall side. */ |
5175 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) { |
5176 | 0 | err = bpf_map_freeze(map->fd); |
5177 | 0 | if (err) { |
5178 | 0 | err = -errno; |
5179 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5180 | 0 | pr_warn("Error freezing map(%s) as read-only: %s\n", |
5181 | 0 | map->name, cp); |
5182 | 0 | return err; |
5183 | 0 | } |
5184 | 0 | } |
5185 | 0 | return 0; |
5186 | 0 | } |
5187 | | |
5188 | | static void bpf_map__destroy(struct bpf_map *map); |
5189 | | |
5190 | | static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner) |
5191 | 0 | { |
5192 | 0 | LIBBPF_OPTS(bpf_map_create_opts, create_attr); |
5193 | 0 | struct bpf_map_def *def = &map->def; |
5194 | 0 | const char *map_name = NULL; |
5195 | 0 | int err = 0; |
5196 | |
|
5197 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
5198 | 0 | map_name = map->name; |
5199 | 0 | create_attr.map_ifindex = map->map_ifindex; |
5200 | 0 | create_attr.map_flags = def->map_flags; |
5201 | 0 | create_attr.numa_node = map->numa_node; |
5202 | 0 | create_attr.map_extra = map->map_extra; |
5203 | |
|
5204 | 0 | if (bpf_map__is_struct_ops(map)) |
5205 | 0 | create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
5206 | |
|
5207 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0) { |
5208 | 0 | create_attr.btf_fd = btf__fd(obj->btf); |
5209 | 0 | create_attr.btf_key_type_id = map->btf_key_type_id; |
5210 | 0 | create_attr.btf_value_type_id = map->btf_value_type_id; |
5211 | 0 | } |
5212 | |
|
5213 | 0 | if (bpf_map_type__is_map_in_map(def->type)) { |
5214 | 0 | if (map->inner_map) { |
5215 | 0 | err = bpf_object__create_map(obj, map->inner_map, true); |
5216 | 0 | if (err) { |
5217 | 0 | pr_warn("map '%s': failed to create inner map: %d\n", |
5218 | 0 | map->name, err); |
5219 | 0 | return err; |
5220 | 0 | } |
5221 | 0 | map->inner_map_fd = bpf_map__fd(map->inner_map); |
5222 | 0 | } |
5223 | 0 | if (map->inner_map_fd >= 0) |
5224 | 0 | create_attr.inner_map_fd = map->inner_map_fd; |
5225 | 0 | } |
5226 | | |
5227 | 0 | switch (def->type) { |
5228 | 0 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
5229 | 0 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
5230 | 0 | case BPF_MAP_TYPE_STACK_TRACE: |
5231 | 0 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
5232 | 0 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
5233 | 0 | case BPF_MAP_TYPE_DEVMAP: |
5234 | 0 | case BPF_MAP_TYPE_DEVMAP_HASH: |
5235 | 0 | case BPF_MAP_TYPE_CPUMAP: |
5236 | 0 | case BPF_MAP_TYPE_XSKMAP: |
5237 | 0 | case BPF_MAP_TYPE_SOCKMAP: |
5238 | 0 | case BPF_MAP_TYPE_SOCKHASH: |
5239 | 0 | case BPF_MAP_TYPE_QUEUE: |
5240 | 0 | case BPF_MAP_TYPE_STACK: |
5241 | 0 | create_attr.btf_fd = 0; |
5242 | 0 | create_attr.btf_key_type_id = 0; |
5243 | 0 | create_attr.btf_value_type_id = 0; |
5244 | 0 | map->btf_key_type_id = 0; |
5245 | 0 | map->btf_value_type_id = 0; |
5246 | 0 | default: |
5247 | 0 | break; |
5248 | 0 | } |
5249 | | |
5250 | 0 | if (obj->gen_loader) { |
5251 | 0 | bpf_gen__map_create(obj->gen_loader, def->type, map_name, |
5252 | 0 | def->key_size, def->value_size, def->max_entries, |
5253 | 0 | &create_attr, is_inner ? -1 : map - obj->maps); |
5254 | | /* Pretend to have valid FD to pass various fd >= 0 checks. |
5255 | | * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. |
5256 | | */ |
5257 | 0 | map->fd = 0; |
5258 | 0 | } else { |
5259 | 0 | map->fd = bpf_map_create(def->type, map_name, |
5260 | 0 | def->key_size, def->value_size, |
5261 | 0 | def->max_entries, &create_attr); |
5262 | 0 | } |
5263 | 0 | if (map->fd < 0 && (create_attr.btf_key_type_id || |
5264 | 0 | create_attr.btf_value_type_id)) { |
5265 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5266 | |
|
5267 | 0 | err = -errno; |
5268 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5269 | 0 | pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", |
5270 | 0 | map->name, cp, err); |
5271 | 0 | create_attr.btf_fd = 0; |
5272 | 0 | create_attr.btf_key_type_id = 0; |
5273 | 0 | create_attr.btf_value_type_id = 0; |
5274 | 0 | map->btf_key_type_id = 0; |
5275 | 0 | map->btf_value_type_id = 0; |
5276 | 0 | map->fd = bpf_map_create(def->type, map_name, |
5277 | 0 | def->key_size, def->value_size, |
5278 | 0 | def->max_entries, &create_attr); |
5279 | 0 | } |
5280 | |
|
5281 | 0 | err = map->fd < 0 ? -errno : 0; |
5282 | |
|
5283 | 0 | if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { |
5284 | 0 | if (obj->gen_loader) |
5285 | 0 | map->inner_map->fd = -1; |
5286 | 0 | bpf_map__destroy(map->inner_map); |
5287 | 0 | zfree(&map->inner_map); |
5288 | 0 | } |
5289 | |
|
5290 | 0 | return err; |
5291 | 0 | } |
5292 | | |
5293 | | static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map) |
5294 | 0 | { |
5295 | 0 | const struct bpf_map *targ_map; |
5296 | 0 | unsigned int i; |
5297 | 0 | int fd, err = 0; |
5298 | |
|
5299 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5300 | 0 | if (!map->init_slots[i]) |
5301 | 0 | continue; |
5302 | | |
5303 | 0 | targ_map = map->init_slots[i]; |
5304 | 0 | fd = bpf_map__fd(targ_map); |
5305 | |
|
5306 | 0 | if (obj->gen_loader) { |
5307 | 0 | bpf_gen__populate_outer_map(obj->gen_loader, |
5308 | 0 | map - obj->maps, i, |
5309 | 0 | targ_map - obj->maps); |
5310 | 0 | } else { |
5311 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5312 | 0 | } |
5313 | 0 | if (err) { |
5314 | 0 | err = -errno; |
5315 | 0 | pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", |
5316 | 0 | map->name, i, targ_map->name, fd, err); |
5317 | 0 | return err; |
5318 | 0 | } |
5319 | 0 | pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", |
5320 | 0 | map->name, i, targ_map->name, fd); |
5321 | 0 | } |
5322 | | |
5323 | 0 | zfree(&map->init_slots); |
5324 | 0 | map->init_slots_sz = 0; |
5325 | |
|
5326 | 0 | return 0; |
5327 | 0 | } |
5328 | | |
5329 | | static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map) |
5330 | 0 | { |
5331 | 0 | const struct bpf_program *targ_prog; |
5332 | 0 | unsigned int i; |
5333 | 0 | int fd, err; |
5334 | |
|
5335 | 0 | if (obj->gen_loader) |
5336 | 0 | return -ENOTSUP; |
5337 | | |
5338 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5339 | 0 | if (!map->init_slots[i]) |
5340 | 0 | continue; |
5341 | | |
5342 | 0 | targ_prog = map->init_slots[i]; |
5343 | 0 | fd = bpf_program__fd(targ_prog); |
5344 | |
|
5345 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5346 | 0 | if (err) { |
5347 | 0 | err = -errno; |
5348 | 0 | pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n", |
5349 | 0 | map->name, i, targ_prog->name, fd, err); |
5350 | 0 | return err; |
5351 | 0 | } |
5352 | 0 | pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n", |
5353 | 0 | map->name, i, targ_prog->name, fd); |
5354 | 0 | } |
5355 | | |
5356 | 0 | zfree(&map->init_slots); |
5357 | 0 | map->init_slots_sz = 0; |
5358 | |
|
5359 | 0 | return 0; |
5360 | 0 | } |
5361 | | |
5362 | | static int bpf_object_init_prog_arrays(struct bpf_object *obj) |
5363 | 0 | { |
5364 | 0 | struct bpf_map *map; |
5365 | 0 | int i, err; |
5366 | |
|
5367 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5368 | 0 | map = &obj->maps[i]; |
5369 | |
|
5370 | 0 | if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) |
5371 | 0 | continue; |
5372 | | |
5373 | 0 | err = init_prog_array_slots(obj, map); |
5374 | 0 | if (err < 0) { |
5375 | 0 | zclose(map->fd); |
5376 | 0 | return err; |
5377 | 0 | } |
5378 | 0 | } |
5379 | 0 | return 0; |
5380 | 0 | } |
5381 | | |
5382 | | static int map_set_def_max_entries(struct bpf_map *map) |
5383 | 0 | { |
5384 | 0 | if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { |
5385 | 0 | int nr_cpus; |
5386 | |
|
5387 | 0 | nr_cpus = libbpf_num_possible_cpus(); |
5388 | 0 | if (nr_cpus < 0) { |
5389 | 0 | pr_warn("map '%s': failed to determine number of system CPUs: %d\n", |
5390 | 0 | map->name, nr_cpus); |
5391 | 0 | return nr_cpus; |
5392 | 0 | } |
5393 | 0 | pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); |
5394 | 0 | map->def.max_entries = nr_cpus; |
5395 | 0 | } |
5396 | | |
5397 | 0 | return 0; |
5398 | 0 | } |
5399 | | |
5400 | | static int |
5401 | | bpf_object__create_maps(struct bpf_object *obj) |
5402 | 0 | { |
5403 | 0 | struct bpf_map *map; |
5404 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5405 | 0 | unsigned int i, j; |
5406 | 0 | int err; |
5407 | 0 | bool retried; |
5408 | |
|
5409 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5410 | 0 | map = &obj->maps[i]; |
5411 | | |
5412 | | /* To support old kernels, we skip creating global data maps |
5413 | | * (.rodata, .data, .kconfig, etc); later on, during program |
5414 | | * loading, if we detect that at least one of the to-be-loaded |
5415 | | * programs is referencing any global data map, we'll error |
5416 | | * out with program name and relocation index logged. |
5417 | | * This approach allows to accommodate Clang emitting |
5418 | | * unnecessary .rodata.str1.1 sections for string literals, |
5419 | | * but also it allows to have CO-RE applications that use |
5420 | | * global variables in some of BPF programs, but not others. |
5421 | | * If those global variable-using programs are not loaded at |
5422 | | * runtime due to bpf_program__set_autoload(prog, false), |
5423 | | * bpf_object loading will succeed just fine even on old |
5424 | | * kernels. |
5425 | | */ |
5426 | 0 | if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA)) |
5427 | 0 | map->autocreate = false; |
5428 | |
|
5429 | 0 | if (!map->autocreate) { |
5430 | 0 | pr_debug("map '%s': skipped auto-creating...\n", map->name); |
5431 | 0 | continue; |
5432 | 0 | } |
5433 | | |
5434 | 0 | err = map_set_def_max_entries(map); |
5435 | 0 | if (err) |
5436 | 0 | goto err_out; |
5437 | | |
5438 | 0 | retried = false; |
5439 | 0 | retry: |
5440 | 0 | if (map->pin_path) { |
5441 | 0 | err = bpf_object__reuse_map(map); |
5442 | 0 | if (err) { |
5443 | 0 | pr_warn("map '%s': error reusing pinned map\n", |
5444 | 0 | map->name); |
5445 | 0 | goto err_out; |
5446 | 0 | } |
5447 | 0 | if (retried && map->fd < 0) { |
5448 | 0 | pr_warn("map '%s': cannot find pinned map\n", |
5449 | 0 | map->name); |
5450 | 0 | err = -ENOENT; |
5451 | 0 | goto err_out; |
5452 | 0 | } |
5453 | 0 | } |
5454 | | |
5455 | 0 | if (map->fd >= 0) { |
5456 | 0 | pr_debug("map '%s': skipping creation (preset fd=%d)\n", |
5457 | 0 | map->name, map->fd); |
5458 | 0 | } else { |
5459 | 0 | err = bpf_object__create_map(obj, map, false); |
5460 | 0 | if (err) |
5461 | 0 | goto err_out; |
5462 | | |
5463 | 0 | pr_debug("map '%s': created successfully, fd=%d\n", |
5464 | 0 | map->name, map->fd); |
5465 | |
|
5466 | 0 | if (bpf_map__is_internal(map)) { |
5467 | 0 | err = bpf_object__populate_internal_map(obj, map); |
5468 | 0 | if (err < 0) { |
5469 | 0 | zclose(map->fd); |
5470 | 0 | goto err_out; |
5471 | 0 | } |
5472 | 0 | } |
5473 | | |
5474 | 0 | if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { |
5475 | 0 | err = init_map_in_map_slots(obj, map); |
5476 | 0 | if (err < 0) { |
5477 | 0 | zclose(map->fd); |
5478 | 0 | goto err_out; |
5479 | 0 | } |
5480 | 0 | } |
5481 | 0 | } |
5482 | | |
5483 | 0 | if (map->pin_path && !map->pinned) { |
5484 | 0 | err = bpf_map__pin(map, NULL); |
5485 | 0 | if (err) { |
5486 | 0 | zclose(map->fd); |
5487 | 0 | if (!retried && err == -EEXIST) { |
5488 | 0 | retried = true; |
5489 | 0 | goto retry; |
5490 | 0 | } |
5491 | 0 | pr_warn("map '%s': failed to auto-pin at '%s': %d\n", |
5492 | 0 | map->name, map->pin_path, err); |
5493 | 0 | goto err_out; |
5494 | 0 | } |
5495 | 0 | } |
5496 | 0 | } |
5497 | | |
5498 | 0 | return 0; |
5499 | | |
5500 | 0 | err_out: |
5501 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5502 | 0 | pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err); |
5503 | 0 | pr_perm_msg(err); |
5504 | 0 | for (j = 0; j < i; j++) |
5505 | 0 | zclose(obj->maps[j].fd); |
5506 | 0 | return err; |
5507 | 0 | } |
5508 | | |
5509 | | static bool bpf_core_is_flavor_sep(const char *s) |
5510 | 11.1k | { |
5511 | | /* check X___Y name pattern, where X and Y are not underscores */ |
5512 | 11.1k | return s[0] != '_' && /* X */ |
5513 | 11.1k | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ |
5514 | 11.1k | s[4] != '_'; /* Y */ |
5515 | 11.1k | } |
5516 | | |
5517 | | /* Given 'some_struct_name___with_flavor' return the length of a name prefix |
5518 | | * before last triple underscore. Struct name part after last triple |
5519 | | * underscore is ignored by BPF CO-RE relocation during relocation matching. |
5520 | | */ |
5521 | | size_t bpf_core_essential_name_len(const char *name) |
5522 | 1.60k | { |
5523 | 1.60k | size_t n = strlen(name); |
5524 | 1.60k | int i; |
5525 | | |
5526 | 12.7k | for (i = n - 5; i >= 0; i--) { |
5527 | 11.1k | if (bpf_core_is_flavor_sep(name + i)) |
5528 | 43 | return i + 1; |
5529 | 11.1k | } |
5530 | 1.55k | return n; |
5531 | 1.60k | } |
5532 | | |
5533 | | void bpf_core_free_cands(struct bpf_core_cand_list *cands) |
5534 | 0 | { |
5535 | 0 | if (!cands) |
5536 | 0 | return; |
5537 | | |
5538 | 0 | free(cands->cands); |
5539 | 0 | free(cands); |
5540 | 0 | } |
5541 | | |
5542 | | int bpf_core_add_cands(struct bpf_core_cand *local_cand, |
5543 | | size_t local_essent_len, |
5544 | | const struct btf *targ_btf, |
5545 | | const char *targ_btf_name, |
5546 | | int targ_start_id, |
5547 | | struct bpf_core_cand_list *cands) |
5548 | 0 | { |
5549 | 0 | struct bpf_core_cand *new_cands, *cand; |
5550 | 0 | const struct btf_type *t, *local_t; |
5551 | 0 | const char *targ_name, *local_name; |
5552 | 0 | size_t targ_essent_len; |
5553 | 0 | int n, i; |
5554 | |
|
5555 | 0 | local_t = btf__type_by_id(local_cand->btf, local_cand->id); |
5556 | 0 | local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); |
5557 | |
|
5558 | 0 | n = btf__type_cnt(targ_btf); |
5559 | 0 | for (i = targ_start_id; i < n; i++) { |
5560 | 0 | t = btf__type_by_id(targ_btf, i); |
5561 | 0 | if (!btf_kind_core_compat(t, local_t)) |
5562 | 0 | continue; |
5563 | | |
5564 | 0 | targ_name = btf__name_by_offset(targ_btf, t->name_off); |
5565 | 0 | if (str_is_empty(targ_name)) |
5566 | 0 | continue; |
5567 | | |
5568 | 0 | targ_essent_len = bpf_core_essential_name_len(targ_name); |
5569 | 0 | if (targ_essent_len != local_essent_len) |
5570 | 0 | continue; |
5571 | | |
5572 | 0 | if (strncmp(local_name, targ_name, local_essent_len) != 0) |
5573 | 0 | continue; |
5574 | | |
5575 | 0 | pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n", |
5576 | 0 | local_cand->id, btf_kind_str(local_t), |
5577 | 0 | local_name, i, btf_kind_str(t), targ_name, |
5578 | 0 | targ_btf_name); |
5579 | 0 | new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, |
5580 | 0 | sizeof(*cands->cands)); |
5581 | 0 | if (!new_cands) |
5582 | 0 | return -ENOMEM; |
5583 | | |
5584 | 0 | cand = &new_cands[cands->len]; |
5585 | 0 | cand->btf = targ_btf; |
5586 | 0 | cand->id = i; |
5587 | |
|
5588 | 0 | cands->cands = new_cands; |
5589 | 0 | cands->len++; |
5590 | 0 | } |
5591 | 0 | return 0; |
5592 | 0 | } |
5593 | | |
5594 | | static int load_module_btfs(struct bpf_object *obj) |
5595 | 0 | { |
5596 | 0 | struct bpf_btf_info info; |
5597 | 0 | struct module_btf *mod_btf; |
5598 | 0 | struct btf *btf; |
5599 | 0 | char name[64]; |
5600 | 0 | __u32 id = 0, len; |
5601 | 0 | int err, fd; |
5602 | |
|
5603 | 0 | if (obj->btf_modules_loaded) |
5604 | 0 | return 0; |
5605 | | |
5606 | 0 | if (obj->gen_loader) |
5607 | 0 | return 0; |
5608 | | |
5609 | | /* don't do this again, even if we find no module BTFs */ |
5610 | 0 | obj->btf_modules_loaded = true; |
5611 | | |
5612 | | /* kernel too old to support module BTFs */ |
5613 | 0 | if (!kernel_supports(obj, FEAT_MODULE_BTF)) |
5614 | 0 | return 0; |
5615 | | |
5616 | 0 | while (true) { |
5617 | 0 | err = bpf_btf_get_next_id(id, &id); |
5618 | 0 | if (err && errno == ENOENT) |
5619 | 0 | return 0; |
5620 | 0 | if (err && errno == EPERM) { |
5621 | 0 | pr_debug("skipping module BTFs loading, missing privileges\n"); |
5622 | 0 | return 0; |
5623 | 0 | } |
5624 | 0 | if (err) { |
5625 | 0 | err = -errno; |
5626 | 0 | pr_warn("failed to iterate BTF objects: %d\n", err); |
5627 | 0 | return err; |
5628 | 0 | } |
5629 | | |
5630 | 0 | fd = bpf_btf_get_fd_by_id(id); |
5631 | 0 | if (fd < 0) { |
5632 | 0 | if (errno == ENOENT) |
5633 | 0 | continue; /* expected race: BTF was unloaded */ |
5634 | 0 | err = -errno; |
5635 | 0 | pr_warn("failed to get BTF object #%d FD: %d\n", id, err); |
5636 | 0 | return err; |
5637 | 0 | } |
5638 | | |
5639 | 0 | len = sizeof(info); |
5640 | 0 | memset(&info, 0, sizeof(info)); |
5641 | 0 | info.name = ptr_to_u64(name); |
5642 | 0 | info.name_len = sizeof(name); |
5643 | |
|
5644 | 0 | err = bpf_btf_get_info_by_fd(fd, &info, &len); |
5645 | 0 | if (err) { |
5646 | 0 | err = -errno; |
5647 | 0 | pr_warn("failed to get BTF object #%d info: %d\n", id, err); |
5648 | 0 | goto err_out; |
5649 | 0 | } |
5650 | | |
5651 | | /* ignore non-module BTFs */ |
5652 | 0 | if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) { |
5653 | 0 | close(fd); |
5654 | 0 | continue; |
5655 | 0 | } |
5656 | | |
5657 | 0 | btf = btf_get_from_fd(fd, obj->btf_vmlinux); |
5658 | 0 | err = libbpf_get_error(btf); |
5659 | 0 | if (err) { |
5660 | 0 | pr_warn("failed to load module [%s]'s BTF object #%d: %d\n", |
5661 | 0 | name, id, err); |
5662 | 0 | goto err_out; |
5663 | 0 | } |
5664 | | |
5665 | 0 | err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, |
5666 | 0 | sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); |
5667 | 0 | if (err) |
5668 | 0 | goto err_out; |
5669 | | |
5670 | 0 | mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; |
5671 | |
|
5672 | 0 | mod_btf->btf = btf; |
5673 | 0 | mod_btf->id = id; |
5674 | 0 | mod_btf->fd = fd; |
5675 | 0 | mod_btf->name = strdup(name); |
5676 | 0 | if (!mod_btf->name) { |
5677 | 0 | err = -ENOMEM; |
5678 | 0 | goto err_out; |
5679 | 0 | } |
5680 | 0 | continue; |
5681 | | |
5682 | 0 | err_out: |
5683 | 0 | close(fd); |
5684 | 0 | return err; |
5685 | 0 | } |
5686 | | |
5687 | 0 | return 0; |
5688 | 0 | } |
5689 | | |
5690 | | static struct bpf_core_cand_list * |
5691 | | bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id) |
5692 | 0 | { |
5693 | 0 | struct bpf_core_cand local_cand = {}; |
5694 | 0 | struct bpf_core_cand_list *cands; |
5695 | 0 | const struct btf *main_btf; |
5696 | 0 | const struct btf_type *local_t; |
5697 | 0 | const char *local_name; |
5698 | 0 | size_t local_essent_len; |
5699 | 0 | int err, i; |
5700 | |
|
5701 | 0 | local_cand.btf = local_btf; |
5702 | 0 | local_cand.id = local_type_id; |
5703 | 0 | local_t = btf__type_by_id(local_btf, local_type_id); |
5704 | 0 | if (!local_t) |
5705 | 0 | return ERR_PTR(-EINVAL); |
5706 | | |
5707 | 0 | local_name = btf__name_by_offset(local_btf, local_t->name_off); |
5708 | 0 | if (str_is_empty(local_name)) |
5709 | 0 | return ERR_PTR(-EINVAL); |
5710 | 0 | local_essent_len = bpf_core_essential_name_len(local_name); |
5711 | |
|
5712 | 0 | cands = calloc(1, sizeof(*cands)); |
5713 | 0 | if (!cands) |
5714 | 0 | return ERR_PTR(-ENOMEM); |
5715 | | |
5716 | | /* Attempt to find target candidates in vmlinux BTF first */ |
5717 | 0 | main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; |
5718 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands); |
5719 | 0 | if (err) |
5720 | 0 | goto err_out; |
5721 | | |
5722 | | /* if vmlinux BTF has any candidate, don't got for module BTFs */ |
5723 | 0 | if (cands->len) |
5724 | 0 | return cands; |
5725 | | |
5726 | | /* if vmlinux BTF was overridden, don't attempt to load module BTFs */ |
5727 | 0 | if (obj->btf_vmlinux_override) |
5728 | 0 | return cands; |
5729 | | |
5730 | | /* now look through module BTFs, trying to still find candidates */ |
5731 | 0 | err = load_module_btfs(obj); |
5732 | 0 | if (err) |
5733 | 0 | goto err_out; |
5734 | | |
5735 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
5736 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, |
5737 | 0 | obj->btf_modules[i].btf, |
5738 | 0 | obj->btf_modules[i].name, |
5739 | 0 | btf__type_cnt(obj->btf_vmlinux), |
5740 | 0 | cands); |
5741 | 0 | if (err) |
5742 | 0 | goto err_out; |
5743 | 0 | } |
5744 | | |
5745 | 0 | return cands; |
5746 | 0 | err_out: |
5747 | 0 | bpf_core_free_cands(cands); |
5748 | 0 | return ERR_PTR(err); |
5749 | 0 | } |
5750 | | |
5751 | | /* Check local and target types for compatibility. This check is used for |
5752 | | * type-based CO-RE relocations and follow slightly different rules than |
5753 | | * field-based relocations. This function assumes that root types were already |
5754 | | * checked for name match. Beyond that initial root-level name check, names |
5755 | | * are completely ignored. Compatibility rules are as follows: |
5756 | | * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but |
5757 | | * kind should match for local and target types (i.e., STRUCT is not |
5758 | | * compatible with UNION); |
5759 | | * - for ENUMs, the size is ignored; |
5760 | | * - for INT, size and signedness are ignored; |
5761 | | * - for ARRAY, dimensionality is ignored, element types are checked for |
5762 | | * compatibility recursively; |
5763 | | * - CONST/VOLATILE/RESTRICT modifiers are ignored; |
5764 | | * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; |
5765 | | * - FUNC_PROTOs are compatible if they have compatible signature: same |
5766 | | * number of input args and compatible return and argument types. |
5767 | | * These rules are not set in stone and probably will be adjusted as we get |
5768 | | * more experience with using BPF CO-RE relocations. |
5769 | | */ |
5770 | | int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, |
5771 | | const struct btf *targ_btf, __u32 targ_id) |
5772 | 0 | { |
5773 | 0 | return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32); |
5774 | 0 | } |
5775 | | |
5776 | | int bpf_core_types_match(const struct btf *local_btf, __u32 local_id, |
5777 | | const struct btf *targ_btf, __u32 targ_id) |
5778 | 0 | { |
5779 | 0 | return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32); |
5780 | 0 | } |
5781 | | |
5782 | | static size_t bpf_core_hash_fn(const long key, void *ctx) |
5783 | 0 | { |
5784 | 0 | return key; |
5785 | 0 | } |
5786 | | |
5787 | | static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx) |
5788 | 0 | { |
5789 | 0 | return k1 == k2; |
5790 | 0 | } |
5791 | | |
5792 | | static int record_relo_core(struct bpf_program *prog, |
5793 | | const struct bpf_core_relo *core_relo, int insn_idx) |
5794 | 0 | { |
5795 | 0 | struct reloc_desc *relos, *relo; |
5796 | |
|
5797 | 0 | relos = libbpf_reallocarray(prog->reloc_desc, |
5798 | 0 | prog->nr_reloc + 1, sizeof(*relos)); |
5799 | 0 | if (!relos) |
5800 | 0 | return -ENOMEM; |
5801 | 0 | relo = &relos[prog->nr_reloc]; |
5802 | 0 | relo->type = RELO_CORE; |
5803 | 0 | relo->insn_idx = insn_idx; |
5804 | 0 | relo->core_relo = core_relo; |
5805 | 0 | prog->reloc_desc = relos; |
5806 | 0 | prog->nr_reloc++; |
5807 | 0 | return 0; |
5808 | 0 | } |
5809 | | |
5810 | | static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx) |
5811 | 0 | { |
5812 | 0 | struct reloc_desc *relo; |
5813 | 0 | int i; |
5814 | |
|
5815 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
5816 | 0 | relo = &prog->reloc_desc[i]; |
5817 | 0 | if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) |
5818 | 0 | continue; |
5819 | | |
5820 | 0 | return relo->core_relo; |
5821 | 0 | } |
5822 | | |
5823 | 0 | return NULL; |
5824 | 0 | } |
5825 | | |
5826 | | static int bpf_core_resolve_relo(struct bpf_program *prog, |
5827 | | const struct bpf_core_relo *relo, |
5828 | | int relo_idx, |
5829 | | const struct btf *local_btf, |
5830 | | struct hashmap *cand_cache, |
5831 | | struct bpf_core_relo_res *targ_res) |
5832 | 0 | { |
5833 | 0 | struct bpf_core_spec specs_scratch[3] = {}; |
5834 | 0 | struct bpf_core_cand_list *cands = NULL; |
5835 | 0 | const char *prog_name = prog->name; |
5836 | 0 | const struct btf_type *local_type; |
5837 | 0 | const char *local_name; |
5838 | 0 | __u32 local_id = relo->type_id; |
5839 | 0 | int err; |
5840 | |
|
5841 | 0 | local_type = btf__type_by_id(local_btf, local_id); |
5842 | 0 | if (!local_type) |
5843 | 0 | return -EINVAL; |
5844 | | |
5845 | 0 | local_name = btf__name_by_offset(local_btf, local_type->name_off); |
5846 | 0 | if (!local_name) |
5847 | 0 | return -EINVAL; |
5848 | | |
5849 | 0 | if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && |
5850 | 0 | !hashmap__find(cand_cache, local_id, &cands)) { |
5851 | 0 | cands = bpf_core_find_cands(prog->obj, local_btf, local_id); |
5852 | 0 | if (IS_ERR(cands)) { |
5853 | 0 | pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n", |
5854 | 0 | prog_name, relo_idx, local_id, btf_kind_str(local_type), |
5855 | 0 | local_name, PTR_ERR(cands)); |
5856 | 0 | return PTR_ERR(cands); |
5857 | 0 | } |
5858 | 0 | err = hashmap__set(cand_cache, local_id, cands, NULL, NULL); |
5859 | 0 | if (err) { |
5860 | 0 | bpf_core_free_cands(cands); |
5861 | 0 | return err; |
5862 | 0 | } |
5863 | 0 | } |
5864 | | |
5865 | 0 | return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch, |
5866 | 0 | targ_res); |
5867 | 0 | } |
5868 | | |
5869 | | static int |
5870 | | bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) |
5871 | 0 | { |
5872 | 0 | const struct btf_ext_info_sec *sec; |
5873 | 0 | struct bpf_core_relo_res targ_res; |
5874 | 0 | const struct bpf_core_relo *rec; |
5875 | 0 | const struct btf_ext_info *seg; |
5876 | 0 | struct hashmap_entry *entry; |
5877 | 0 | struct hashmap *cand_cache = NULL; |
5878 | 0 | struct bpf_program *prog; |
5879 | 0 | struct bpf_insn *insn; |
5880 | 0 | const char *sec_name; |
5881 | 0 | int i, err = 0, insn_idx, sec_idx, sec_num; |
5882 | |
|
5883 | 0 | if (obj->btf_ext->core_relo_info.len == 0) |
5884 | 0 | return 0; |
5885 | | |
5886 | 0 | if (targ_btf_path) { |
5887 | 0 | obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); |
5888 | 0 | err = libbpf_get_error(obj->btf_vmlinux_override); |
5889 | 0 | if (err) { |
5890 | 0 | pr_warn("failed to parse target BTF: %d\n", err); |
5891 | 0 | return err; |
5892 | 0 | } |
5893 | 0 | } |
5894 | | |
5895 | 0 | cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); |
5896 | 0 | if (IS_ERR(cand_cache)) { |
5897 | 0 | err = PTR_ERR(cand_cache); |
5898 | 0 | goto out; |
5899 | 0 | } |
5900 | | |
5901 | 0 | seg = &obj->btf_ext->core_relo_info; |
5902 | 0 | sec_num = 0; |
5903 | 0 | for_each_btf_ext_sec(seg, sec) { |
5904 | 0 | sec_idx = seg->sec_idxs[sec_num]; |
5905 | 0 | sec_num++; |
5906 | |
|
5907 | 0 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
5908 | 0 | if (str_is_empty(sec_name)) { |
5909 | 0 | err = -EINVAL; |
5910 | 0 | goto out; |
5911 | 0 | } |
5912 | | |
5913 | 0 | pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info); |
5914 | |
|
5915 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5916 | 0 | if (rec->insn_off % BPF_INSN_SZ) |
5917 | 0 | return -EINVAL; |
5918 | 0 | insn_idx = rec->insn_off / BPF_INSN_SZ; |
5919 | 0 | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
5920 | 0 | if (!prog) { |
5921 | | /* When __weak subprog is "overridden" by another instance |
5922 | | * of the subprog from a different object file, linker still |
5923 | | * appends all the .BTF.ext info that used to belong to that |
5924 | | * eliminated subprogram. |
5925 | | * This is similar to what x86-64 linker does for relocations. |
5926 | | * So just ignore such relocations just like we ignore |
5927 | | * subprog instructions when discovering subprograms. |
5928 | | */ |
5929 | 0 | pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n", |
5930 | 0 | sec_name, i, insn_idx); |
5931 | 0 | continue; |
5932 | 0 | } |
5933 | | /* no need to apply CO-RE relocation if the program is |
5934 | | * not going to be loaded |
5935 | | */ |
5936 | 0 | if (!prog->autoload) |
5937 | 0 | continue; |
5938 | | |
5939 | | /* adjust insn_idx from section frame of reference to the local |
5940 | | * program's frame of reference; (sub-)program code is not yet |
5941 | | * relocated, so it's enough to just subtract in-section offset |
5942 | | */ |
5943 | 0 | insn_idx = insn_idx - prog->sec_insn_off; |
5944 | 0 | if (insn_idx >= prog->insns_cnt) |
5945 | 0 | return -EINVAL; |
5946 | 0 | insn = &prog->insns[insn_idx]; |
5947 | |
|
5948 | 0 | err = record_relo_core(prog, rec, insn_idx); |
5949 | 0 | if (err) { |
5950 | 0 | pr_warn("prog '%s': relo #%d: failed to record relocation: %d\n", |
5951 | 0 | prog->name, i, err); |
5952 | 0 | goto out; |
5953 | 0 | } |
5954 | | |
5955 | 0 | if (prog->obj->gen_loader) |
5956 | 0 | continue; |
5957 | | |
5958 | 0 | err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); |
5959 | 0 | if (err) { |
5960 | 0 | pr_warn("prog '%s': relo #%d: failed to relocate: %d\n", |
5961 | 0 | prog->name, i, err); |
5962 | 0 | goto out; |
5963 | 0 | } |
5964 | | |
5965 | 0 | err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); |
5966 | 0 | if (err) { |
5967 | 0 | pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n", |
5968 | 0 | prog->name, i, insn_idx, err); |
5969 | 0 | goto out; |
5970 | 0 | } |
5971 | 0 | } |
5972 | 0 | } |
5973 | | |
5974 | 0 | out: |
5975 | | /* obj->btf_vmlinux and module BTFs are freed after object load */ |
5976 | 0 | btf__free(obj->btf_vmlinux_override); |
5977 | 0 | obj->btf_vmlinux_override = NULL; |
5978 | |
|
5979 | 0 | if (!IS_ERR_OR_NULL(cand_cache)) { |
5980 | 0 | hashmap__for_each_entry(cand_cache, entry, i) { |
5981 | 0 | bpf_core_free_cands(entry->pvalue); |
5982 | 0 | } |
5983 | 0 | hashmap__free(cand_cache); |
5984 | 0 | } |
5985 | 0 | return err; |
5986 | 0 | } |
5987 | | |
5988 | | /* base map load ldimm64 special constant, used also for log fixup logic */ |
5989 | 0 | #define POISON_LDIMM64_MAP_BASE 2001000000 |
5990 | | #define POISON_LDIMM64_MAP_PFX "200100" |
5991 | | |
5992 | | static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx, |
5993 | | int insn_idx, struct bpf_insn *insn, |
5994 | | int map_idx, const struct bpf_map *map) |
5995 | 0 | { |
5996 | 0 | int i; |
5997 | |
|
5998 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n", |
5999 | 0 | prog->name, relo_idx, insn_idx, map_idx, map->name); |
6000 | | |
6001 | | /* we turn single ldimm64 into two identical invalid calls */ |
6002 | 0 | for (i = 0; i < 2; i++) { |
6003 | 0 | insn->code = BPF_JMP | BPF_CALL; |
6004 | 0 | insn->dst_reg = 0; |
6005 | 0 | insn->src_reg = 0; |
6006 | 0 | insn->off = 0; |
6007 | | /* if this instruction is reachable (not a dead code), |
6008 | | * verifier will complain with something like: |
6009 | | * invalid func unknown#2001000123 |
6010 | | * where lower 123 is map index into obj->maps[] array |
6011 | | */ |
6012 | 0 | insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; |
6013 | |
|
6014 | 0 | insn++; |
6015 | 0 | } |
6016 | 0 | } |
6017 | | |
6018 | | /* unresolved kfunc call special constant, used also for log fixup logic */ |
6019 | 0 | #define POISON_CALL_KFUNC_BASE 2002000000 |
6020 | | #define POISON_CALL_KFUNC_PFX "2002" |
6021 | | |
6022 | | static void poison_kfunc_call(struct bpf_program *prog, int relo_idx, |
6023 | | int insn_idx, struct bpf_insn *insn, |
6024 | | int ext_idx, const struct extern_desc *ext) |
6025 | 0 | { |
6026 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n", |
6027 | 0 | prog->name, relo_idx, insn_idx, ext->name); |
6028 | | |
6029 | | /* we turn kfunc call into invalid helper call with identifiable constant */ |
6030 | 0 | insn->code = BPF_JMP | BPF_CALL; |
6031 | 0 | insn->dst_reg = 0; |
6032 | 0 | insn->src_reg = 0; |
6033 | 0 | insn->off = 0; |
6034 | | /* if this instruction is reachable (not a dead code), |
6035 | | * verifier will complain with something like: |
6036 | | * invalid func unknown#2001000123 |
6037 | | * where lower 123 is extern index into obj->externs[] array |
6038 | | */ |
6039 | 0 | insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; |
6040 | 0 | } |
6041 | | |
6042 | | /* Relocate data references within program code: |
6043 | | * - map references; |
6044 | | * - global variable references; |
6045 | | * - extern references. |
6046 | | */ |
6047 | | static int |
6048 | | bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) |
6049 | 0 | { |
6050 | 0 | int i; |
6051 | |
|
6052 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
6053 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
6054 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
6055 | 0 | const struct bpf_map *map; |
6056 | 0 | struct extern_desc *ext; |
6057 | |
|
6058 | 0 | switch (relo->type) { |
6059 | 0 | case RELO_LD64: |
6060 | 0 | map = &obj->maps[relo->map_idx]; |
6061 | 0 | if (obj->gen_loader) { |
6062 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX; |
6063 | 0 | insn[0].imm = relo->map_idx; |
6064 | 0 | } else if (map->autocreate) { |
6065 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_FD; |
6066 | 0 | insn[0].imm = map->fd; |
6067 | 0 | } else { |
6068 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6069 | 0 | relo->map_idx, map); |
6070 | 0 | } |
6071 | 0 | break; |
6072 | 0 | case RELO_DATA: |
6073 | 0 | map = &obj->maps[relo->map_idx]; |
6074 | 0 | insn[1].imm = insn[0].imm + relo->sym_off; |
6075 | 0 | if (obj->gen_loader) { |
6076 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6077 | 0 | insn[0].imm = relo->map_idx; |
6078 | 0 | } else if (map->autocreate) { |
6079 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6080 | 0 | insn[0].imm = map->fd; |
6081 | 0 | } else { |
6082 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6083 | 0 | relo->map_idx, map); |
6084 | 0 | } |
6085 | 0 | break; |
6086 | 0 | case RELO_EXTERN_LD64: |
6087 | 0 | ext = &obj->externs[relo->ext_idx]; |
6088 | 0 | if (ext->type == EXT_KCFG) { |
6089 | 0 | if (obj->gen_loader) { |
6090 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6091 | 0 | insn[0].imm = obj->kconfig_map_idx; |
6092 | 0 | } else { |
6093 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6094 | 0 | insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; |
6095 | 0 | } |
6096 | 0 | insn[1].imm = ext->kcfg.data_off; |
6097 | 0 | } else /* EXT_KSYM */ { |
6098 | 0 | if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ |
6099 | 0 | insn[0].src_reg = BPF_PSEUDO_BTF_ID; |
6100 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6101 | 0 | insn[1].imm = ext->ksym.kernel_btf_obj_fd; |
6102 | 0 | } else { /* typeless ksyms or unresolved typed ksyms */ |
6103 | 0 | insn[0].imm = (__u32)ext->ksym.addr; |
6104 | 0 | insn[1].imm = ext->ksym.addr >> 32; |
6105 | 0 | } |
6106 | 0 | } |
6107 | 0 | break; |
6108 | 0 | case RELO_EXTERN_CALL: |
6109 | 0 | ext = &obj->externs[relo->ext_idx]; |
6110 | 0 | insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL; |
6111 | 0 | if (ext->is_set) { |
6112 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6113 | 0 | insn[0].off = ext->ksym.btf_fd_idx; |
6114 | 0 | } else { /* unresolved weak kfunc call */ |
6115 | 0 | poison_kfunc_call(prog, i, relo->insn_idx, insn, |
6116 | 0 | relo->ext_idx, ext); |
6117 | 0 | } |
6118 | 0 | break; |
6119 | 0 | case RELO_SUBPROG_ADDR: |
6120 | 0 | if (insn[0].src_reg != BPF_PSEUDO_FUNC) { |
6121 | 0 | pr_warn("prog '%s': relo #%d: bad insn\n", |
6122 | 0 | prog->name, i); |
6123 | 0 | return -EINVAL; |
6124 | 0 | } |
6125 | | /* handled already */ |
6126 | 0 | break; |
6127 | 0 | case RELO_CALL: |
6128 | | /* handled already */ |
6129 | 0 | break; |
6130 | 0 | case RELO_CORE: |
6131 | | /* will be handled by bpf_program_record_relos() */ |
6132 | 0 | break; |
6133 | 0 | default: |
6134 | 0 | pr_warn("prog '%s': relo #%d: bad relo type %d\n", |
6135 | 0 | prog->name, i, relo->type); |
6136 | 0 | return -EINVAL; |
6137 | 0 | } |
6138 | 0 | } |
6139 | | |
6140 | 0 | return 0; |
6141 | 0 | } |
6142 | | |
6143 | | static int adjust_prog_btf_ext_info(const struct bpf_object *obj, |
6144 | | const struct bpf_program *prog, |
6145 | | const struct btf_ext_info *ext_info, |
6146 | | void **prog_info, __u32 *prog_rec_cnt, |
6147 | | __u32 *prog_rec_sz) |
6148 | 0 | { |
6149 | 0 | void *copy_start = NULL, *copy_end = NULL; |
6150 | 0 | void *rec, *rec_end, *new_prog_info; |
6151 | 0 | const struct btf_ext_info_sec *sec; |
6152 | 0 | size_t old_sz, new_sz; |
6153 | 0 | int i, sec_num, sec_idx, off_adj; |
6154 | |
|
6155 | 0 | sec_num = 0; |
6156 | 0 | for_each_btf_ext_sec(ext_info, sec) { |
6157 | 0 | sec_idx = ext_info->sec_idxs[sec_num]; |
6158 | 0 | sec_num++; |
6159 | 0 | if (prog->sec_idx != sec_idx) |
6160 | 0 | continue; |
6161 | | |
6162 | 0 | for_each_btf_ext_rec(ext_info, sec, i, rec) { |
6163 | 0 | __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ; |
6164 | |
|
6165 | 0 | if (insn_off < prog->sec_insn_off) |
6166 | 0 | continue; |
6167 | 0 | if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) |
6168 | 0 | break; |
6169 | | |
6170 | 0 | if (!copy_start) |
6171 | 0 | copy_start = rec; |
6172 | 0 | copy_end = rec + ext_info->rec_size; |
6173 | 0 | } |
6174 | |
|
6175 | 0 | if (!copy_start) |
6176 | 0 | return -ENOENT; |
6177 | | |
6178 | | /* append func/line info of a given (sub-)program to the main |
6179 | | * program func/line info |
6180 | | */ |
6181 | 0 | old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; |
6182 | 0 | new_sz = old_sz + (copy_end - copy_start); |
6183 | 0 | new_prog_info = realloc(*prog_info, new_sz); |
6184 | 0 | if (!new_prog_info) |
6185 | 0 | return -ENOMEM; |
6186 | 0 | *prog_info = new_prog_info; |
6187 | 0 | *prog_rec_cnt = new_sz / ext_info->rec_size; |
6188 | 0 | memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); |
6189 | | |
6190 | | /* Kernel instruction offsets are in units of 8-byte |
6191 | | * instructions, while .BTF.ext instruction offsets generated |
6192 | | * by Clang are in units of bytes. So convert Clang offsets |
6193 | | * into kernel offsets and adjust offset according to program |
6194 | | * relocated position. |
6195 | | */ |
6196 | 0 | off_adj = prog->sub_insn_off - prog->sec_insn_off; |
6197 | 0 | rec = new_prog_info + old_sz; |
6198 | 0 | rec_end = new_prog_info + new_sz; |
6199 | 0 | for (; rec < rec_end; rec += ext_info->rec_size) { |
6200 | 0 | __u32 *insn_off = rec; |
6201 | |
|
6202 | 0 | *insn_off = *insn_off / BPF_INSN_SZ + off_adj; |
6203 | 0 | } |
6204 | 0 | *prog_rec_sz = ext_info->rec_size; |
6205 | 0 | return 0; |
6206 | 0 | } |
6207 | | |
6208 | 0 | return -ENOENT; |
6209 | 0 | } |
6210 | | |
6211 | | static int |
6212 | | reloc_prog_func_and_line_info(const struct bpf_object *obj, |
6213 | | struct bpf_program *main_prog, |
6214 | | const struct bpf_program *prog) |
6215 | 0 | { |
6216 | 0 | int err; |
6217 | | |
6218 | | /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't |
6219 | | * supprot func/line info |
6220 | | */ |
6221 | 0 | if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) |
6222 | 0 | return 0; |
6223 | | |
6224 | | /* only attempt func info relocation if main program's func_info |
6225 | | * relocation was successful |
6226 | | */ |
6227 | 0 | if (main_prog != prog && !main_prog->func_info) |
6228 | 0 | goto line_info; |
6229 | | |
6230 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, |
6231 | 0 | &main_prog->func_info, |
6232 | 0 | &main_prog->func_info_cnt, |
6233 | 0 | &main_prog->func_info_rec_size); |
6234 | 0 | if (err) { |
6235 | 0 | if (err != -ENOENT) { |
6236 | 0 | pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n", |
6237 | 0 | prog->name, err); |
6238 | 0 | return err; |
6239 | 0 | } |
6240 | 0 | if (main_prog->func_info) { |
6241 | | /* |
6242 | | * Some info has already been found but has problem |
6243 | | * in the last btf_ext reloc. Must have to error out. |
6244 | | */ |
6245 | 0 | pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); |
6246 | 0 | return err; |
6247 | 0 | } |
6248 | | /* Have problem loading the very first info. Ignore the rest. */ |
6249 | 0 | pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n", |
6250 | 0 | prog->name); |
6251 | 0 | } |
6252 | | |
6253 | 0 | line_info: |
6254 | | /* don't relocate line info if main program's relocation failed */ |
6255 | 0 | if (main_prog != prog && !main_prog->line_info) |
6256 | 0 | return 0; |
6257 | | |
6258 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, |
6259 | 0 | &main_prog->line_info, |
6260 | 0 | &main_prog->line_info_cnt, |
6261 | 0 | &main_prog->line_info_rec_size); |
6262 | 0 | if (err) { |
6263 | 0 | if (err != -ENOENT) { |
6264 | 0 | pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n", |
6265 | 0 | prog->name, err); |
6266 | 0 | return err; |
6267 | 0 | } |
6268 | 0 | if (main_prog->line_info) { |
6269 | | /* |
6270 | | * Some info has already been found but has problem |
6271 | | * in the last btf_ext reloc. Must have to error out. |
6272 | | */ |
6273 | 0 | pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); |
6274 | 0 | return err; |
6275 | 0 | } |
6276 | | /* Have problem loading the very first info. Ignore the rest. */ |
6277 | 0 | pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n", |
6278 | 0 | prog->name); |
6279 | 0 | } |
6280 | 0 | return 0; |
6281 | 0 | } |
6282 | | |
6283 | | static int cmp_relo_by_insn_idx(const void *key, const void *elem) |
6284 | 0 | { |
6285 | 0 | size_t insn_idx = *(const size_t *)key; |
6286 | 0 | const struct reloc_desc *relo = elem; |
6287 | |
|
6288 | 0 | if (insn_idx == relo->insn_idx) |
6289 | 0 | return 0; |
6290 | 0 | return insn_idx < relo->insn_idx ? -1 : 1; |
6291 | 0 | } |
6292 | | |
6293 | | static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx) |
6294 | 0 | { |
6295 | 0 | if (!prog->nr_reloc) |
6296 | 0 | return NULL; |
6297 | 0 | return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, |
6298 | 0 | sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); |
6299 | 0 | } |
6300 | | |
6301 | | static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog) |
6302 | 0 | { |
6303 | 0 | int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; |
6304 | 0 | struct reloc_desc *relos; |
6305 | 0 | int i; |
6306 | |
|
6307 | 0 | if (main_prog == subprog) |
6308 | 0 | return 0; |
6309 | 0 | relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); |
6310 | | /* if new count is zero, reallocarray can return a valid NULL result; |
6311 | | * in this case the previous pointer will be freed, so we *have to* |
6312 | | * reassign old pointer to the new value (even if it's NULL) |
6313 | | */ |
6314 | 0 | if (!relos && new_cnt) |
6315 | 0 | return -ENOMEM; |
6316 | 0 | if (subprog->nr_reloc) |
6317 | 0 | memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, |
6318 | 0 | sizeof(*relos) * subprog->nr_reloc); |
6319 | |
|
6320 | 0 | for (i = main_prog->nr_reloc; i < new_cnt; i++) |
6321 | 0 | relos[i].insn_idx += subprog->sub_insn_off; |
6322 | | /* After insn_idx adjustment the 'relos' array is still sorted |
6323 | | * by insn_idx and doesn't break bsearch. |
6324 | | */ |
6325 | 0 | main_prog->reloc_desc = relos; |
6326 | 0 | main_prog->nr_reloc = new_cnt; |
6327 | 0 | return 0; |
6328 | 0 | } |
6329 | | |
6330 | | static int |
6331 | | bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6332 | | struct bpf_program *subprog) |
6333 | 0 | { |
6334 | 0 | struct bpf_insn *insns; |
6335 | 0 | size_t new_cnt; |
6336 | 0 | int err; |
6337 | |
|
6338 | 0 | subprog->sub_insn_off = main_prog->insns_cnt; |
6339 | |
|
6340 | 0 | new_cnt = main_prog->insns_cnt + subprog->insns_cnt; |
6341 | 0 | insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); |
6342 | 0 | if (!insns) { |
6343 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); |
6344 | 0 | return -ENOMEM; |
6345 | 0 | } |
6346 | 0 | main_prog->insns = insns; |
6347 | 0 | main_prog->insns_cnt = new_cnt; |
6348 | |
|
6349 | 0 | memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, |
6350 | 0 | subprog->insns_cnt * sizeof(*insns)); |
6351 | |
|
6352 | 0 | pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", |
6353 | 0 | main_prog->name, subprog->insns_cnt, subprog->name); |
6354 | | |
6355 | | /* The subprog insns are now appended. Append its relos too. */ |
6356 | 0 | err = append_subprog_relos(main_prog, subprog); |
6357 | 0 | if (err) |
6358 | 0 | return err; |
6359 | 0 | return 0; |
6360 | 0 | } |
6361 | | |
6362 | | static int |
6363 | | bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6364 | | struct bpf_program *prog) |
6365 | 0 | { |
6366 | 0 | size_t sub_insn_idx, insn_idx; |
6367 | 0 | struct bpf_program *subprog; |
6368 | 0 | struct reloc_desc *relo; |
6369 | 0 | struct bpf_insn *insn; |
6370 | 0 | int err; |
6371 | |
|
6372 | 0 | err = reloc_prog_func_and_line_info(obj, main_prog, prog); |
6373 | 0 | if (err) |
6374 | 0 | return err; |
6375 | | |
6376 | 0 | for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { |
6377 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6378 | 0 | if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn)) |
6379 | 0 | continue; |
6380 | | |
6381 | 0 | relo = find_prog_insn_relo(prog, insn_idx); |
6382 | 0 | if (relo && relo->type == RELO_EXTERN_CALL) |
6383 | | /* kfunc relocations will be handled later |
6384 | | * in bpf_object__relocate_data() |
6385 | | */ |
6386 | 0 | continue; |
6387 | 0 | if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { |
6388 | 0 | pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n", |
6389 | 0 | prog->name, insn_idx, relo->type); |
6390 | 0 | return -LIBBPF_ERRNO__RELOC; |
6391 | 0 | } |
6392 | 0 | if (relo) { |
6393 | | /* sub-program instruction index is a combination of |
6394 | | * an offset of a symbol pointed to by relocation and |
6395 | | * call instruction's imm field; for global functions, |
6396 | | * call always has imm = -1, but for static functions |
6397 | | * relocation is against STT_SECTION and insn->imm |
6398 | | * points to a start of a static function |
6399 | | * |
6400 | | * for subprog addr relocation, the relo->sym_off + insn->imm is |
6401 | | * the byte offset in the corresponding section. |
6402 | | */ |
6403 | 0 | if (relo->type == RELO_CALL) |
6404 | 0 | sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; |
6405 | 0 | else |
6406 | 0 | sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; |
6407 | 0 | } else if (insn_is_pseudo_func(insn)) { |
6408 | | /* |
6409 | | * RELO_SUBPROG_ADDR relo is always emitted even if both |
6410 | | * functions are in the same section, so it shouldn't reach here. |
6411 | | */ |
6412 | 0 | pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n", |
6413 | 0 | prog->name, insn_idx); |
6414 | 0 | return -LIBBPF_ERRNO__RELOC; |
6415 | 0 | } else { |
6416 | | /* if subprogram call is to a static function within |
6417 | | * the same ELF section, there won't be any relocation |
6418 | | * emitted, but it also means there is no additional |
6419 | | * offset necessary, insns->imm is relative to |
6420 | | * instruction's original position within the section |
6421 | | */ |
6422 | 0 | sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; |
6423 | 0 | } |
6424 | | |
6425 | | /* we enforce that sub-programs should be in .text section */ |
6426 | 0 | subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); |
6427 | 0 | if (!subprog) { |
6428 | 0 | pr_warn("prog '%s': no .text section found yet sub-program call exists\n", |
6429 | 0 | prog->name); |
6430 | 0 | return -LIBBPF_ERRNO__RELOC; |
6431 | 0 | } |
6432 | | |
6433 | | /* if it's the first call instruction calling into this |
6434 | | * subprogram (meaning this subprog hasn't been processed |
6435 | | * yet) within the context of current main program: |
6436 | | * - append it at the end of main program's instructions blog; |
6437 | | * - process is recursively, while current program is put on hold; |
6438 | | * - if that subprogram calls some other not yet processes |
6439 | | * subprogram, same thing will happen recursively until |
6440 | | * there are no more unprocesses subprograms left to append |
6441 | | * and relocate. |
6442 | | */ |
6443 | 0 | if (subprog->sub_insn_off == 0) { |
6444 | 0 | err = bpf_object__append_subprog_code(obj, main_prog, subprog); |
6445 | 0 | if (err) |
6446 | 0 | return err; |
6447 | 0 | err = bpf_object__reloc_code(obj, main_prog, subprog); |
6448 | 0 | if (err) |
6449 | 0 | return err; |
6450 | 0 | } |
6451 | | |
6452 | | /* main_prog->insns memory could have been re-allocated, so |
6453 | | * calculate pointer again |
6454 | | */ |
6455 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6456 | | /* calculate correct instruction position within current main |
6457 | | * prog; each main prog can have a different set of |
6458 | | * subprograms appended (potentially in different order as |
6459 | | * well), so position of any subprog can be different for |
6460 | | * different main programs |
6461 | | */ |
6462 | 0 | insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; |
6463 | |
|
6464 | 0 | pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n", |
6465 | 0 | prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); |
6466 | 0 | } |
6467 | | |
6468 | 0 | return 0; |
6469 | 0 | } |
6470 | | |
6471 | | /* |
6472 | | * Relocate sub-program calls. |
6473 | | * |
6474 | | * Algorithm operates as follows. Each entry-point BPF program (referred to as |
6475 | | * main prog) is processed separately. For each subprog (non-entry functions, |
6476 | | * that can be called from either entry progs or other subprogs) gets their |
6477 | | * sub_insn_off reset to zero. This serves as indicator that this subprogram |
6478 | | * hasn't been yet appended and relocated within current main prog. Once its |
6479 | | * relocated, sub_insn_off will point at the position within current main prog |
6480 | | * where given subprog was appended. This will further be used to relocate all |
6481 | | * the call instructions jumping into this subprog. |
6482 | | * |
6483 | | * We start with main program and process all call instructions. If the call |
6484 | | * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off |
6485 | | * is zero), subprog instructions are appended at the end of main program's |
6486 | | * instruction array. Then main program is "put on hold" while we recursively |
6487 | | * process newly appended subprogram. If that subprogram calls into another |
6488 | | * subprogram that hasn't been appended, new subprogram is appended again to |
6489 | | * the *main* prog's instructions (subprog's instructions are always left |
6490 | | * untouched, as they need to be in unmodified state for subsequent main progs |
6491 | | * and subprog instructions are always sent only as part of a main prog) and |
6492 | | * the process continues recursively. Once all the subprogs called from a main |
6493 | | * prog or any of its subprogs are appended (and relocated), all their |
6494 | | * positions within finalized instructions array are known, so it's easy to |
6495 | | * rewrite call instructions with correct relative offsets, corresponding to |
6496 | | * desired target subprog. |
6497 | | * |
6498 | | * Its important to realize that some subprogs might not be called from some |
6499 | | * main prog and any of its called/used subprogs. Those will keep their |
6500 | | * subprog->sub_insn_off as zero at all times and won't be appended to current |
6501 | | * main prog and won't be relocated within the context of current main prog. |
6502 | | * They might still be used from other main progs later. |
6503 | | * |
6504 | | * Visually this process can be shown as below. Suppose we have two main |
6505 | | * programs mainA and mainB and BPF object contains three subprogs: subA, |
6506 | | * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and |
6507 | | * subC both call subB: |
6508 | | * |
6509 | | * +--------+ +-------+ |
6510 | | * | v v | |
6511 | | * +--+---+ +--+-+-+ +---+--+ |
6512 | | * | subA | | subB | | subC | |
6513 | | * +--+---+ +------+ +---+--+ |
6514 | | * ^ ^ |
6515 | | * | | |
6516 | | * +---+-------+ +------+----+ |
6517 | | * | mainA | | mainB | |
6518 | | * +-----------+ +-----------+ |
6519 | | * |
6520 | | * We'll start relocating mainA, will find subA, append it and start |
6521 | | * processing sub A recursively: |
6522 | | * |
6523 | | * +-----------+------+ |
6524 | | * | mainA | subA | |
6525 | | * +-----------+------+ |
6526 | | * |
6527 | | * At this point we notice that subB is used from subA, so we append it and |
6528 | | * relocate (there are no further subcalls from subB): |
6529 | | * |
6530 | | * +-----------+------+------+ |
6531 | | * | mainA | subA | subB | |
6532 | | * +-----------+------+------+ |
6533 | | * |
6534 | | * At this point, we relocate subA calls, then go one level up and finish with |
6535 | | * relocatin mainA calls. mainA is done. |
6536 | | * |
6537 | | * For mainB process is similar but results in different order. We start with |
6538 | | * mainB and skip subA and subB, as mainB never calls them (at least |
6539 | | * directly), but we see subC is needed, so we append and start processing it: |
6540 | | * |
6541 | | * +-----------+------+ |
6542 | | * | mainB | subC | |
6543 | | * +-----------+------+ |
6544 | | * Now we see subC needs subB, so we go back to it, append and relocate it: |
6545 | | * |
6546 | | * +-----------+------+------+ |
6547 | | * | mainB | subC | subB | |
6548 | | * +-----------+------+------+ |
6549 | | * |
6550 | | * At this point we unwind recursion, relocate calls in subC, then in mainB. |
6551 | | */ |
6552 | | static int |
6553 | | bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog) |
6554 | 0 | { |
6555 | 0 | struct bpf_program *subprog; |
6556 | 0 | int i, err; |
6557 | | |
6558 | | /* mark all subprogs as not relocated (yet) within the context of |
6559 | | * current main program |
6560 | | */ |
6561 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6562 | 0 | subprog = &obj->programs[i]; |
6563 | 0 | if (!prog_is_subprog(obj, subprog)) |
6564 | 0 | continue; |
6565 | | |
6566 | 0 | subprog->sub_insn_off = 0; |
6567 | 0 | } |
6568 | |
|
6569 | 0 | err = bpf_object__reloc_code(obj, prog, prog); |
6570 | 0 | if (err) |
6571 | 0 | return err; |
6572 | | |
6573 | 0 | return 0; |
6574 | 0 | } |
6575 | | |
6576 | | static void |
6577 | | bpf_object__free_relocs(struct bpf_object *obj) |
6578 | 0 | { |
6579 | 0 | struct bpf_program *prog; |
6580 | 0 | int i; |
6581 | | |
6582 | | /* free up relocation descriptors */ |
6583 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6584 | 0 | prog = &obj->programs[i]; |
6585 | 0 | zfree(&prog->reloc_desc); |
6586 | 0 | prog->nr_reloc = 0; |
6587 | 0 | } |
6588 | 0 | } |
6589 | | |
6590 | | static int cmp_relocs(const void *_a, const void *_b) |
6591 | 1.59k | { |
6592 | 1.59k | const struct reloc_desc *a = _a; |
6593 | 1.59k | const struct reloc_desc *b = _b; |
6594 | | |
6595 | 1.59k | if (a->insn_idx != b->insn_idx) |
6596 | 65 | return a->insn_idx < b->insn_idx ? -1 : 1; |
6597 | | |
6598 | | /* no two relocations should have the same insn_idx, but ... */ |
6599 | 1.52k | if (a->type != b->type) |
6600 | 86 | return a->type < b->type ? -1 : 1; |
6601 | | |
6602 | 1.44k | return 0; |
6603 | 1.52k | } |
6604 | | |
6605 | | static void bpf_object__sort_relos(struct bpf_object *obj) |
6606 | 1.45k | { |
6607 | 1.45k | int i; |
6608 | | |
6609 | 8.63k | for (i = 0; i < obj->nr_programs; i++) { |
6610 | 7.18k | struct bpf_program *p = &obj->programs[i]; |
6611 | | |
6612 | 7.18k | if (!p->nr_reloc) |
6613 | 7.14k | continue; |
6614 | | |
6615 | 43 | qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); |
6616 | 43 | } |
6617 | 1.45k | } |
6618 | | |
6619 | | static int |
6620 | | bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) |
6621 | 0 | { |
6622 | 0 | struct bpf_program *prog; |
6623 | 0 | size_t i, j; |
6624 | 0 | int err; |
6625 | |
|
6626 | 0 | if (obj->btf_ext) { |
6627 | 0 | err = bpf_object__relocate_core(obj, targ_btf_path); |
6628 | 0 | if (err) { |
6629 | 0 | pr_warn("failed to perform CO-RE relocations: %d\n", |
6630 | 0 | err); |
6631 | 0 | return err; |
6632 | 0 | } |
6633 | 0 | bpf_object__sort_relos(obj); |
6634 | 0 | } |
6635 | | |
6636 | | /* Before relocating calls pre-process relocations and mark |
6637 | | * few ld_imm64 instructions that points to subprogs. |
6638 | | * Otherwise bpf_object__reloc_code() later would have to consider |
6639 | | * all ld_imm64 insns as relocation candidates. That would |
6640 | | * reduce relocation speed, since amount of find_prog_insn_relo() |
6641 | | * would increase and most of them will fail to find a relo. |
6642 | | */ |
6643 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6644 | 0 | prog = &obj->programs[i]; |
6645 | 0 | for (j = 0; j < prog->nr_reloc; j++) { |
6646 | 0 | struct reloc_desc *relo = &prog->reloc_desc[j]; |
6647 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
6648 | | |
6649 | | /* mark the insn, so it's recognized by insn_is_pseudo_func() */ |
6650 | 0 | if (relo->type == RELO_SUBPROG_ADDR) |
6651 | 0 | insn[0].src_reg = BPF_PSEUDO_FUNC; |
6652 | 0 | } |
6653 | 0 | } |
6654 | | |
6655 | | /* relocate subprogram calls and append used subprograms to main |
6656 | | * programs; each copy of subprogram code needs to be relocated |
6657 | | * differently for each main program, because its code location might |
6658 | | * have changed. |
6659 | | * Append subprog relos to main programs to allow data relos to be |
6660 | | * processed after text is completely relocated. |
6661 | | */ |
6662 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6663 | 0 | prog = &obj->programs[i]; |
6664 | | /* sub-program's sub-calls are relocated within the context of |
6665 | | * its main program only |
6666 | | */ |
6667 | 0 | if (prog_is_subprog(obj, prog)) |
6668 | 0 | continue; |
6669 | 0 | if (!prog->autoload) |
6670 | 0 | continue; |
6671 | | |
6672 | 0 | err = bpf_object__relocate_calls(obj, prog); |
6673 | 0 | if (err) { |
6674 | 0 | pr_warn("prog '%s': failed to relocate calls: %d\n", |
6675 | 0 | prog->name, err); |
6676 | 0 | return err; |
6677 | 0 | } |
6678 | | |
6679 | | /* Now, also append exception callback if it has not been done already. */ |
6680 | 0 | if (prog->exception_cb_idx >= 0) { |
6681 | 0 | struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx]; |
6682 | | |
6683 | | /* Calling exception callback directly is disallowed, which the |
6684 | | * verifier will reject later. In case it was processed already, |
6685 | | * we can skip this step, otherwise for all other valid cases we |
6686 | | * have to append exception callback now. |
6687 | | */ |
6688 | 0 | if (subprog->sub_insn_off == 0) { |
6689 | 0 | err = bpf_object__append_subprog_code(obj, prog, subprog); |
6690 | 0 | if (err) |
6691 | 0 | return err; |
6692 | 0 | err = bpf_object__reloc_code(obj, prog, subprog); |
6693 | 0 | if (err) |
6694 | 0 | return err; |
6695 | 0 | } |
6696 | 0 | } |
6697 | 0 | } |
6698 | | /* Process data relos for main programs */ |
6699 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6700 | 0 | prog = &obj->programs[i]; |
6701 | 0 | if (prog_is_subprog(obj, prog)) |
6702 | 0 | continue; |
6703 | 0 | if (!prog->autoload) |
6704 | 0 | continue; |
6705 | 0 | err = bpf_object__relocate_data(obj, prog); |
6706 | 0 | if (err) { |
6707 | 0 | pr_warn("prog '%s': failed to relocate data references: %d\n", |
6708 | 0 | prog->name, err); |
6709 | 0 | return err; |
6710 | 0 | } |
6711 | 0 | } |
6712 | | |
6713 | 0 | return 0; |
6714 | 0 | } |
6715 | | |
6716 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
6717 | | Elf64_Shdr *shdr, Elf_Data *data); |
6718 | | |
6719 | | static int bpf_object__collect_map_relos(struct bpf_object *obj, |
6720 | | Elf64_Shdr *shdr, Elf_Data *data) |
6721 | 0 | { |
6722 | 0 | const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *); |
6723 | 0 | int i, j, nrels, new_sz; |
6724 | 0 | const struct btf_var_secinfo *vi = NULL; |
6725 | 0 | const struct btf_type *sec, *var, *def; |
6726 | 0 | struct bpf_map *map = NULL, *targ_map = NULL; |
6727 | 0 | struct bpf_program *targ_prog = NULL; |
6728 | 0 | bool is_prog_array, is_map_in_map; |
6729 | 0 | const struct btf_member *member; |
6730 | 0 | const char *name, *mname, *type; |
6731 | 0 | unsigned int moff; |
6732 | 0 | Elf64_Sym *sym; |
6733 | 0 | Elf64_Rel *rel; |
6734 | 0 | void *tmp; |
6735 | |
|
6736 | 0 | if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) |
6737 | 0 | return -EINVAL; |
6738 | 0 | sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); |
6739 | 0 | if (!sec) |
6740 | 0 | return -EINVAL; |
6741 | | |
6742 | 0 | nrels = shdr->sh_size / shdr->sh_entsize; |
6743 | 0 | for (i = 0; i < nrels; i++) { |
6744 | 0 | rel = elf_rel_by_idx(data, i); |
6745 | 0 | if (!rel) { |
6746 | 0 | pr_warn(".maps relo #%d: failed to get ELF relo\n", i); |
6747 | 0 | return -LIBBPF_ERRNO__FORMAT; |
6748 | 0 | } |
6749 | | |
6750 | 0 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
6751 | 0 | if (!sym) { |
6752 | 0 | pr_warn(".maps relo #%d: symbol %zx not found\n", |
6753 | 0 | i, (size_t)ELF64_R_SYM(rel->r_info)); |
6754 | 0 | return -LIBBPF_ERRNO__FORMAT; |
6755 | 0 | } |
6756 | 0 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
6757 | |
|
6758 | 0 | pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n", |
6759 | 0 | i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, |
6760 | 0 | (size_t)rel->r_offset, sym->st_name, name); |
6761 | |
|
6762 | 0 | for (j = 0; j < obj->nr_maps; j++) { |
6763 | 0 | map = &obj->maps[j]; |
6764 | 0 | if (map->sec_idx != obj->efile.btf_maps_shndx) |
6765 | 0 | continue; |
6766 | | |
6767 | 0 | vi = btf_var_secinfos(sec) + map->btf_var_idx; |
6768 | 0 | if (vi->offset <= rel->r_offset && |
6769 | 0 | rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) |
6770 | 0 | break; |
6771 | 0 | } |
6772 | 0 | if (j == obj->nr_maps) { |
6773 | 0 | pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", |
6774 | 0 | i, name, (size_t)rel->r_offset); |
6775 | 0 | return -EINVAL; |
6776 | 0 | } |
6777 | | |
6778 | 0 | is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); |
6779 | 0 | is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; |
6780 | 0 | type = is_map_in_map ? "map" : "prog"; |
6781 | 0 | if (is_map_in_map) { |
6782 | 0 | if (sym->st_shndx != obj->efile.btf_maps_shndx) { |
6783 | 0 | pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", |
6784 | 0 | i, name); |
6785 | 0 | return -LIBBPF_ERRNO__RELOC; |
6786 | 0 | } |
6787 | 0 | if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && |
6788 | 0 | map->def.key_size != sizeof(int)) { |
6789 | 0 | pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", |
6790 | 0 | i, map->name, sizeof(int)); |
6791 | 0 | return -EINVAL; |
6792 | 0 | } |
6793 | 0 | targ_map = bpf_object__find_map_by_name(obj, name); |
6794 | 0 | if (!targ_map) { |
6795 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n", |
6796 | 0 | i, name); |
6797 | 0 | return -ESRCH; |
6798 | 0 | } |
6799 | 0 | } else if (is_prog_array) { |
6800 | 0 | targ_prog = bpf_object__find_program_by_name(obj, name); |
6801 | 0 | if (!targ_prog) { |
6802 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n", |
6803 | 0 | i, name); |
6804 | 0 | return -ESRCH; |
6805 | 0 | } |
6806 | 0 | if (targ_prog->sec_idx != sym->st_shndx || |
6807 | 0 | targ_prog->sec_insn_off * 8 != sym->st_value || |
6808 | 0 | prog_is_subprog(obj, targ_prog)) { |
6809 | 0 | pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", |
6810 | 0 | i, name); |
6811 | 0 | return -LIBBPF_ERRNO__RELOC; |
6812 | 0 | } |
6813 | 0 | } else { |
6814 | 0 | return -EINVAL; |
6815 | 0 | } |
6816 | | |
6817 | 0 | var = btf__type_by_id(obj->btf, vi->type); |
6818 | 0 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
6819 | 0 | if (btf_vlen(def) == 0) |
6820 | 0 | return -EINVAL; |
6821 | 0 | member = btf_members(def) + btf_vlen(def) - 1; |
6822 | 0 | mname = btf__name_by_offset(obj->btf, member->name_off); |
6823 | 0 | if (strcmp(mname, "values")) |
6824 | 0 | return -EINVAL; |
6825 | | |
6826 | 0 | moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; |
6827 | 0 | if (rel->r_offset - vi->offset < moff) |
6828 | 0 | return -EINVAL; |
6829 | | |
6830 | 0 | moff = rel->r_offset - vi->offset - moff; |
6831 | | /* here we use BPF pointer size, which is always 64 bit, as we |
6832 | | * are parsing ELF that was built for BPF target |
6833 | | */ |
6834 | 0 | if (moff % bpf_ptr_sz) |
6835 | 0 | return -EINVAL; |
6836 | 0 | moff /= bpf_ptr_sz; |
6837 | 0 | if (moff >= map->init_slots_sz) { |
6838 | 0 | new_sz = moff + 1; |
6839 | 0 | tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); |
6840 | 0 | if (!tmp) |
6841 | 0 | return -ENOMEM; |
6842 | 0 | map->init_slots = tmp; |
6843 | 0 | memset(map->init_slots + map->init_slots_sz, 0, |
6844 | 0 | (new_sz - map->init_slots_sz) * host_ptr_sz); |
6845 | 0 | map->init_slots_sz = new_sz; |
6846 | 0 | } |
6847 | 0 | map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; |
6848 | |
|
6849 | 0 | pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n", |
6850 | 0 | i, map->name, moff, type, name); |
6851 | 0 | } |
6852 | | |
6853 | 0 | return 0; |
6854 | 0 | } |
6855 | | |
6856 | | static int bpf_object__collect_relos(struct bpf_object *obj) |
6857 | 1.95k | { |
6858 | 1.95k | int i, err; |
6859 | | |
6860 | 14.1k | for (i = 0; i < obj->efile.sec_cnt; i++) { |
6861 | 12.6k | struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; |
6862 | 12.6k | Elf64_Shdr *shdr; |
6863 | 12.6k | Elf_Data *data; |
6864 | 12.6k | int idx; |
6865 | | |
6866 | 12.6k | if (sec_desc->sec_type != SEC_RELO) |
6867 | 11.2k | continue; |
6868 | | |
6869 | 1.37k | shdr = sec_desc->shdr; |
6870 | 1.37k | data = sec_desc->data; |
6871 | 1.37k | idx = shdr->sh_info; |
6872 | | |
6873 | 1.37k | if (shdr->sh_type != SHT_REL) { |
6874 | 0 | pr_warn("internal error at %d\n", __LINE__); |
6875 | 0 | return -LIBBPF_ERRNO__INTERNAL; |
6876 | 0 | } |
6877 | | |
6878 | 1.37k | if (idx == obj->efile.st_ops_shndx || idx == obj->efile.st_ops_link_shndx) |
6879 | 0 | err = bpf_object__collect_st_ops_relos(obj, shdr, data); |
6880 | 1.37k | else if (idx == obj->efile.btf_maps_shndx) |
6881 | 0 | err = bpf_object__collect_map_relos(obj, shdr, data); |
6882 | 1.37k | else |
6883 | 1.37k | err = bpf_object__collect_prog_relos(obj, shdr, data); |
6884 | 1.37k | if (err) |
6885 | 503 | return err; |
6886 | 1.37k | } |
6887 | | |
6888 | 1.45k | bpf_object__sort_relos(obj); |
6889 | 1.45k | return 0; |
6890 | 1.95k | } |
6891 | | |
6892 | | static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id) |
6893 | 0 | { |
6894 | 0 | if (BPF_CLASS(insn->code) == BPF_JMP && |
6895 | 0 | BPF_OP(insn->code) == BPF_CALL && |
6896 | 0 | BPF_SRC(insn->code) == BPF_K && |
6897 | 0 | insn->src_reg == 0 && |
6898 | 0 | insn->dst_reg == 0) { |
6899 | 0 | *func_id = insn->imm; |
6900 | 0 | return true; |
6901 | 0 | } |
6902 | 0 | return false; |
6903 | 0 | } |
6904 | | |
6905 | | static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog) |
6906 | 0 | { |
6907 | 0 | struct bpf_insn *insn = prog->insns; |
6908 | 0 | enum bpf_func_id func_id; |
6909 | 0 | int i; |
6910 | |
|
6911 | 0 | if (obj->gen_loader) |
6912 | 0 | return 0; |
6913 | | |
6914 | 0 | for (i = 0; i < prog->insns_cnt; i++, insn++) { |
6915 | 0 | if (!insn_is_helper_call(insn, &func_id)) |
6916 | 0 | continue; |
6917 | | |
6918 | | /* on kernels that don't yet support |
6919 | | * bpf_probe_read_{kernel,user}[_str] helpers, fall back |
6920 | | * to bpf_probe_read() which works well for old kernels |
6921 | | */ |
6922 | 0 | switch (func_id) { |
6923 | 0 | case BPF_FUNC_probe_read_kernel: |
6924 | 0 | case BPF_FUNC_probe_read_user: |
6925 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
6926 | 0 | insn->imm = BPF_FUNC_probe_read; |
6927 | 0 | break; |
6928 | 0 | case BPF_FUNC_probe_read_kernel_str: |
6929 | 0 | case BPF_FUNC_probe_read_user_str: |
6930 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
6931 | 0 | insn->imm = BPF_FUNC_probe_read_str; |
6932 | 0 | break; |
6933 | 0 | default: |
6934 | 0 | break; |
6935 | 0 | } |
6936 | 0 | } |
6937 | 0 | return 0; |
6938 | 0 | } |
6939 | | |
6940 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
6941 | | int *btf_obj_fd, int *btf_type_id); |
6942 | | |
6943 | | /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */ |
6944 | | static int libbpf_prepare_prog_load(struct bpf_program *prog, |
6945 | | struct bpf_prog_load_opts *opts, long cookie) |
6946 | 0 | { |
6947 | 0 | enum sec_def_flags def = cookie; |
6948 | | |
6949 | | /* old kernels might not support specifying expected_attach_type */ |
6950 | 0 | if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) |
6951 | 0 | opts->expected_attach_type = 0; |
6952 | |
|
6953 | 0 | if (def & SEC_SLEEPABLE) |
6954 | 0 | opts->prog_flags |= BPF_F_SLEEPABLE; |
6955 | |
|
6956 | 0 | if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) |
6957 | 0 | opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; |
6958 | | |
6959 | | /* special check for usdt to use uprobe_multi link */ |
6960 | 0 | if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) |
6961 | 0 | prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; |
6962 | |
|
6963 | 0 | if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { |
6964 | 0 | int btf_obj_fd = 0, btf_type_id = 0, err; |
6965 | 0 | const char *attach_name; |
6966 | |
|
6967 | 0 | attach_name = strchr(prog->sec_name, '/'); |
6968 | 0 | if (!attach_name) { |
6969 | | /* if BPF program is annotated with just SEC("fentry") |
6970 | | * (or similar) without declaratively specifying |
6971 | | * target, then it is expected that target will be |
6972 | | * specified with bpf_program__set_attach_target() at |
6973 | | * runtime before BPF object load step. If not, then |
6974 | | * there is nothing to load into the kernel as BPF |
6975 | | * verifier won't be able to validate BPF program |
6976 | | * correctness anyways. |
6977 | | */ |
6978 | 0 | pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n", |
6979 | 0 | prog->name); |
6980 | 0 | return -EINVAL; |
6981 | 0 | } |
6982 | 0 | attach_name++; /* skip over / */ |
6983 | |
|
6984 | 0 | err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id); |
6985 | 0 | if (err) |
6986 | 0 | return err; |
6987 | | |
6988 | | /* cache resolved BTF FD and BTF type ID in the prog */ |
6989 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
6990 | 0 | prog->attach_btf_id = btf_type_id; |
6991 | | |
6992 | | /* but by now libbpf common logic is not utilizing |
6993 | | * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because |
6994 | | * this callback is called after opts were populated by |
6995 | | * libbpf, so this callback has to update opts explicitly here |
6996 | | */ |
6997 | 0 | opts->attach_btf_obj_fd = btf_obj_fd; |
6998 | 0 | opts->attach_btf_id = btf_type_id; |
6999 | 0 | } |
7000 | 0 | return 0; |
7001 | 0 | } |
7002 | | |
7003 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz); |
7004 | | |
7005 | | static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog, |
7006 | | struct bpf_insn *insns, int insns_cnt, |
7007 | | const char *license, __u32 kern_version, int *prog_fd) |
7008 | 0 | { |
7009 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, load_attr); |
7010 | 0 | const char *prog_name = NULL; |
7011 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
7012 | 0 | size_t log_buf_size = 0; |
7013 | 0 | char *log_buf = NULL, *tmp; |
7014 | 0 | int btf_fd, ret, err; |
7015 | 0 | bool own_log_buf = true; |
7016 | 0 | __u32 log_level = prog->log_level; |
7017 | |
|
7018 | 0 | if (prog->type == BPF_PROG_TYPE_UNSPEC) { |
7019 | | /* |
7020 | | * The program type must be set. Most likely we couldn't find a proper |
7021 | | * section definition at load time, and thus we didn't infer the type. |
7022 | | */ |
7023 | 0 | pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n", |
7024 | 0 | prog->name, prog->sec_name); |
7025 | 0 | return -EINVAL; |
7026 | 0 | } |
7027 | | |
7028 | 0 | if (!insns || !insns_cnt) |
7029 | 0 | return -EINVAL; |
7030 | | |
7031 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
7032 | 0 | prog_name = prog->name; |
7033 | 0 | load_attr.attach_prog_fd = prog->attach_prog_fd; |
7034 | 0 | load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; |
7035 | 0 | load_attr.attach_btf_id = prog->attach_btf_id; |
7036 | 0 | load_attr.kern_version = kern_version; |
7037 | 0 | load_attr.prog_ifindex = prog->prog_ifindex; |
7038 | | |
7039 | | /* specify func_info/line_info only if kernel supports them */ |
7040 | 0 | btf_fd = bpf_object__btf_fd(obj); |
7041 | 0 | if (btf_fd >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) { |
7042 | 0 | load_attr.prog_btf_fd = btf_fd; |
7043 | 0 | load_attr.func_info = prog->func_info; |
7044 | 0 | load_attr.func_info_rec_size = prog->func_info_rec_size; |
7045 | 0 | load_attr.func_info_cnt = prog->func_info_cnt; |
7046 | 0 | load_attr.line_info = prog->line_info; |
7047 | 0 | load_attr.line_info_rec_size = prog->line_info_rec_size; |
7048 | 0 | load_attr.line_info_cnt = prog->line_info_cnt; |
7049 | 0 | } |
7050 | 0 | load_attr.log_level = log_level; |
7051 | 0 | load_attr.prog_flags = prog->prog_flags; |
7052 | 0 | load_attr.fd_array = obj->fd_array; |
7053 | | |
7054 | | /* adjust load_attr if sec_def provides custom preload callback */ |
7055 | 0 | if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { |
7056 | 0 | err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); |
7057 | 0 | if (err < 0) { |
7058 | 0 | pr_warn("prog '%s': failed to prepare load attributes: %d\n", |
7059 | 0 | prog->name, err); |
7060 | 0 | return err; |
7061 | 0 | } |
7062 | 0 | insns = prog->insns; |
7063 | 0 | insns_cnt = prog->insns_cnt; |
7064 | 0 | } |
7065 | | |
7066 | | /* allow prog_prepare_load_fn to change expected_attach_type */ |
7067 | 0 | load_attr.expected_attach_type = prog->expected_attach_type; |
7068 | |
|
7069 | 0 | if (obj->gen_loader) { |
7070 | 0 | bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, |
7071 | 0 | license, insns, insns_cnt, &load_attr, |
7072 | 0 | prog - obj->programs); |
7073 | 0 | *prog_fd = -1; |
7074 | 0 | return 0; |
7075 | 0 | } |
7076 | | |
7077 | 0 | retry_load: |
7078 | | /* if log_level is zero, we don't request logs initially even if |
7079 | | * custom log_buf is specified; if the program load fails, then we'll |
7080 | | * bump log_level to 1 and use either custom log_buf or we'll allocate |
7081 | | * our own and retry the load to get details on what failed |
7082 | | */ |
7083 | 0 | if (log_level) { |
7084 | 0 | if (prog->log_buf) { |
7085 | 0 | log_buf = prog->log_buf; |
7086 | 0 | log_buf_size = prog->log_size; |
7087 | 0 | own_log_buf = false; |
7088 | 0 | } else if (obj->log_buf) { |
7089 | 0 | log_buf = obj->log_buf; |
7090 | 0 | log_buf_size = obj->log_size; |
7091 | 0 | own_log_buf = false; |
7092 | 0 | } else { |
7093 | 0 | log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2); |
7094 | 0 | tmp = realloc(log_buf, log_buf_size); |
7095 | 0 | if (!tmp) { |
7096 | 0 | ret = -ENOMEM; |
7097 | 0 | goto out; |
7098 | 0 | } |
7099 | 0 | log_buf = tmp; |
7100 | 0 | log_buf[0] = '\0'; |
7101 | 0 | own_log_buf = true; |
7102 | 0 | } |
7103 | 0 | } |
7104 | | |
7105 | 0 | load_attr.log_buf = log_buf; |
7106 | 0 | load_attr.log_size = log_buf_size; |
7107 | 0 | load_attr.log_level = log_level; |
7108 | |
|
7109 | 0 | ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); |
7110 | 0 | if (ret >= 0) { |
7111 | 0 | if (log_level && own_log_buf) { |
7112 | 0 | pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
7113 | 0 | prog->name, log_buf); |
7114 | 0 | } |
7115 | |
|
7116 | 0 | if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { |
7117 | 0 | struct bpf_map *map; |
7118 | 0 | int i; |
7119 | |
|
7120 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
7121 | 0 | map = &prog->obj->maps[i]; |
7122 | 0 | if (map->libbpf_type != LIBBPF_MAP_RODATA) |
7123 | 0 | continue; |
7124 | | |
7125 | 0 | if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) { |
7126 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
7127 | 0 | pr_warn("prog '%s': failed to bind map '%s': %s\n", |
7128 | 0 | prog->name, map->real_name, cp); |
7129 | | /* Don't fail hard if can't bind rodata. */ |
7130 | 0 | } |
7131 | 0 | } |
7132 | 0 | } |
7133 | |
|
7134 | 0 | *prog_fd = ret; |
7135 | 0 | ret = 0; |
7136 | 0 | goto out; |
7137 | 0 | } |
7138 | | |
7139 | 0 | if (log_level == 0) { |
7140 | 0 | log_level = 1; |
7141 | 0 | goto retry_load; |
7142 | 0 | } |
7143 | | /* On ENOSPC, increase log buffer size and retry, unless custom |
7144 | | * log_buf is specified. |
7145 | | * Be careful to not overflow u32, though. Kernel's log buf size limit |
7146 | | * isn't part of UAPI so it can always be bumped to full 4GB. So don't |
7147 | | * multiply by 2 unless we are sure we'll fit within 32 bits. |
7148 | | * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). |
7149 | | */ |
7150 | 0 | if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2) |
7151 | 0 | goto retry_load; |
7152 | | |
7153 | 0 | ret = -errno; |
7154 | | |
7155 | | /* post-process verifier log to improve error descriptions */ |
7156 | 0 | fixup_verifier_log(prog, log_buf, log_buf_size); |
7157 | |
|
7158 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
7159 | 0 | pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp); |
7160 | 0 | pr_perm_msg(ret); |
7161 | |
|
7162 | 0 | if (own_log_buf && log_buf && log_buf[0] != '\0') { |
7163 | 0 | pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
7164 | 0 | prog->name, log_buf); |
7165 | 0 | } |
7166 | |
|
7167 | 0 | out: |
7168 | 0 | if (own_log_buf) |
7169 | 0 | free(log_buf); |
7170 | 0 | return ret; |
7171 | 0 | } |
7172 | | |
7173 | | static char *find_prev_line(char *buf, char *cur) |
7174 | 0 | { |
7175 | 0 | char *p; |
7176 | |
|
7177 | 0 | if (cur == buf) /* end of a log buf */ |
7178 | 0 | return NULL; |
7179 | | |
7180 | 0 | p = cur - 1; |
7181 | 0 | while (p - 1 >= buf && *(p - 1) != '\n') |
7182 | 0 | p--; |
7183 | |
|
7184 | 0 | return p; |
7185 | 0 | } |
7186 | | |
7187 | | static void patch_log(char *buf, size_t buf_sz, size_t log_sz, |
7188 | | char *orig, size_t orig_sz, const char *patch) |
7189 | 0 | { |
7190 | | /* size of the remaining log content to the right from the to-be-replaced part */ |
7191 | 0 | size_t rem_sz = (buf + log_sz) - (orig + orig_sz); |
7192 | 0 | size_t patch_sz = strlen(patch); |
7193 | |
|
7194 | 0 | if (patch_sz != orig_sz) { |
7195 | | /* If patch line(s) are longer than original piece of verifier log, |
7196 | | * shift log contents by (patch_sz - orig_sz) bytes to the right |
7197 | | * starting from after to-be-replaced part of the log. |
7198 | | * |
7199 | | * If patch line(s) are shorter than original piece of verifier log, |
7200 | | * shift log contents by (orig_sz - patch_sz) bytes to the left |
7201 | | * starting from after to-be-replaced part of the log |
7202 | | * |
7203 | | * We need to be careful about not overflowing available |
7204 | | * buf_sz capacity. If that's the case, we'll truncate the end |
7205 | | * of the original log, as necessary. |
7206 | | */ |
7207 | 0 | if (patch_sz > orig_sz) { |
7208 | 0 | if (orig + patch_sz >= buf + buf_sz) { |
7209 | | /* patch is big enough to cover remaining space completely */ |
7210 | 0 | patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; |
7211 | 0 | rem_sz = 0; |
7212 | 0 | } else if (patch_sz - orig_sz > buf_sz - log_sz) { |
7213 | | /* patch causes part of remaining log to be truncated */ |
7214 | 0 | rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); |
7215 | 0 | } |
7216 | 0 | } |
7217 | | /* shift remaining log to the right by calculated amount */ |
7218 | 0 | memmove(orig + patch_sz, orig + orig_sz, rem_sz); |
7219 | 0 | } |
7220 | |
|
7221 | 0 | memcpy(orig, patch, patch_sz); |
7222 | 0 | } |
7223 | | |
7224 | | static void fixup_log_failed_core_relo(struct bpf_program *prog, |
7225 | | char *buf, size_t buf_sz, size_t log_sz, |
7226 | | char *line1, char *line2, char *line3) |
7227 | 0 | { |
7228 | | /* Expected log for failed and not properly guarded CO-RE relocation: |
7229 | | * line1 -> 123: (85) call unknown#195896080 |
7230 | | * line2 -> invalid func unknown#195896080 |
7231 | | * line3 -> <anything else or end of buffer> |
7232 | | * |
7233 | | * "123" is the index of the instruction that was poisoned. We extract |
7234 | | * instruction index to find corresponding CO-RE relocation and |
7235 | | * replace this part of the log with more relevant information about |
7236 | | * failed CO-RE relocation. |
7237 | | */ |
7238 | 0 | const struct bpf_core_relo *relo; |
7239 | 0 | struct bpf_core_spec spec; |
7240 | 0 | char patch[512], spec_buf[256]; |
7241 | 0 | int insn_idx, err, spec_len; |
7242 | |
|
7243 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1) |
7244 | 0 | return; |
7245 | | |
7246 | 0 | relo = find_relo_core(prog, insn_idx); |
7247 | 0 | if (!relo) |
7248 | 0 | return; |
7249 | | |
7250 | 0 | err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); |
7251 | 0 | if (err) |
7252 | 0 | return; |
7253 | | |
7254 | 0 | spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec); |
7255 | 0 | snprintf(patch, sizeof(patch), |
7256 | 0 | "%d: <invalid CO-RE relocation>\n" |
7257 | 0 | "failed to resolve CO-RE relocation %s%s\n", |
7258 | 0 | insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : ""); |
7259 | |
|
7260 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7261 | 0 | } |
7262 | | |
7263 | | static void fixup_log_missing_map_load(struct bpf_program *prog, |
7264 | | char *buf, size_t buf_sz, size_t log_sz, |
7265 | | char *line1, char *line2, char *line3) |
7266 | 0 | { |
7267 | | /* Expected log for failed and not properly guarded map reference: |
7268 | | * line1 -> 123: (85) call unknown#2001000345 |
7269 | | * line2 -> invalid func unknown#2001000345 |
7270 | | * line3 -> <anything else or end of buffer> |
7271 | | * |
7272 | | * "123" is the index of the instruction that was poisoned. |
7273 | | * "345" in "2001000345" is a map index in obj->maps to fetch map name. |
7274 | | */ |
7275 | 0 | struct bpf_object *obj = prog->obj; |
7276 | 0 | const struct bpf_map *map; |
7277 | 0 | int insn_idx, map_idx; |
7278 | 0 | char patch[128]; |
7279 | |
|
7280 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2) |
7281 | 0 | return; |
7282 | | |
7283 | 0 | map_idx -= POISON_LDIMM64_MAP_BASE; |
7284 | 0 | if (map_idx < 0 || map_idx >= obj->nr_maps) |
7285 | 0 | return; |
7286 | 0 | map = &obj->maps[map_idx]; |
7287 | |
|
7288 | 0 | snprintf(patch, sizeof(patch), |
7289 | 0 | "%d: <invalid BPF map reference>\n" |
7290 | 0 | "BPF map '%s' is referenced but wasn't created\n", |
7291 | 0 | insn_idx, map->name); |
7292 | |
|
7293 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7294 | 0 | } |
7295 | | |
7296 | | static void fixup_log_missing_kfunc_call(struct bpf_program *prog, |
7297 | | char *buf, size_t buf_sz, size_t log_sz, |
7298 | | char *line1, char *line2, char *line3) |
7299 | 0 | { |
7300 | | /* Expected log for failed and not properly guarded kfunc call: |
7301 | | * line1 -> 123: (85) call unknown#2002000345 |
7302 | | * line2 -> invalid func unknown#2002000345 |
7303 | | * line3 -> <anything else or end of buffer> |
7304 | | * |
7305 | | * "123" is the index of the instruction that was poisoned. |
7306 | | * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. |
7307 | | */ |
7308 | 0 | struct bpf_object *obj = prog->obj; |
7309 | 0 | const struct extern_desc *ext; |
7310 | 0 | int insn_idx, ext_idx; |
7311 | 0 | char patch[128]; |
7312 | |
|
7313 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2) |
7314 | 0 | return; |
7315 | | |
7316 | 0 | ext_idx -= POISON_CALL_KFUNC_BASE; |
7317 | 0 | if (ext_idx < 0 || ext_idx >= obj->nr_extern) |
7318 | 0 | return; |
7319 | 0 | ext = &obj->externs[ext_idx]; |
7320 | |
|
7321 | 0 | snprintf(patch, sizeof(patch), |
7322 | 0 | "%d: <invalid kfunc call>\n" |
7323 | 0 | "kfunc '%s' is referenced but wasn't resolved\n", |
7324 | 0 | insn_idx, ext->name); |
7325 | |
|
7326 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
7327 | 0 | } |
7328 | | |
7329 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz) |
7330 | 0 | { |
7331 | | /* look for familiar error patterns in last N lines of the log */ |
7332 | 0 | const size_t max_last_line_cnt = 10; |
7333 | 0 | char *prev_line, *cur_line, *next_line; |
7334 | 0 | size_t log_sz; |
7335 | 0 | int i; |
7336 | |
|
7337 | 0 | if (!buf) |
7338 | 0 | return; |
7339 | | |
7340 | 0 | log_sz = strlen(buf) + 1; |
7341 | 0 | next_line = buf + log_sz - 1; |
7342 | |
|
7343 | 0 | for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) { |
7344 | 0 | cur_line = find_prev_line(buf, next_line); |
7345 | 0 | if (!cur_line) |
7346 | 0 | return; |
7347 | | |
7348 | 0 | if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) { |
7349 | 0 | prev_line = find_prev_line(buf, cur_line); |
7350 | 0 | if (!prev_line) |
7351 | 0 | continue; |
7352 | | |
7353 | | /* failed CO-RE relocation case */ |
7354 | 0 | fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz, |
7355 | 0 | prev_line, cur_line, next_line); |
7356 | 0 | return; |
7357 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) { |
7358 | 0 | prev_line = find_prev_line(buf, cur_line); |
7359 | 0 | if (!prev_line) |
7360 | 0 | continue; |
7361 | | |
7362 | | /* reference to uncreated BPF map */ |
7363 | 0 | fixup_log_missing_map_load(prog, buf, buf_sz, log_sz, |
7364 | 0 | prev_line, cur_line, next_line); |
7365 | 0 | return; |
7366 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) { |
7367 | 0 | prev_line = find_prev_line(buf, cur_line); |
7368 | 0 | if (!prev_line) |
7369 | 0 | continue; |
7370 | | |
7371 | | /* reference to unresolved kfunc */ |
7372 | 0 | fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz, |
7373 | 0 | prev_line, cur_line, next_line); |
7374 | 0 | return; |
7375 | 0 | } |
7376 | 0 | } |
7377 | 0 | } |
7378 | | |
7379 | | static int bpf_program_record_relos(struct bpf_program *prog) |
7380 | 0 | { |
7381 | 0 | struct bpf_object *obj = prog->obj; |
7382 | 0 | int i; |
7383 | |
|
7384 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
7385 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
7386 | 0 | struct extern_desc *ext = &obj->externs[relo->ext_idx]; |
7387 | 0 | int kind; |
7388 | |
|
7389 | 0 | switch (relo->type) { |
7390 | 0 | case RELO_EXTERN_LD64: |
7391 | 0 | if (ext->type != EXT_KSYM) |
7392 | 0 | continue; |
7393 | 0 | kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? |
7394 | 0 | BTF_KIND_VAR : BTF_KIND_FUNC; |
7395 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
7396 | 0 | ext->is_weak, !ext->ksym.type_id, |
7397 | 0 | true, kind, relo->insn_idx); |
7398 | 0 | break; |
7399 | 0 | case RELO_EXTERN_CALL: |
7400 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
7401 | 0 | ext->is_weak, false, false, BTF_KIND_FUNC, |
7402 | 0 | relo->insn_idx); |
7403 | 0 | break; |
7404 | 0 | case RELO_CORE: { |
7405 | 0 | struct bpf_core_relo cr = { |
7406 | 0 | .insn_off = relo->insn_idx * 8, |
7407 | 0 | .type_id = relo->core_relo->type_id, |
7408 | 0 | .access_str_off = relo->core_relo->access_str_off, |
7409 | 0 | .kind = relo->core_relo->kind, |
7410 | 0 | }; |
7411 | |
|
7412 | 0 | bpf_gen__record_relo_core(obj->gen_loader, &cr); |
7413 | 0 | break; |
7414 | 0 | } |
7415 | 0 | default: |
7416 | 0 | continue; |
7417 | 0 | } |
7418 | 0 | } |
7419 | 0 | return 0; |
7420 | 0 | } |
7421 | | |
7422 | | static int |
7423 | | bpf_object__load_progs(struct bpf_object *obj, int log_level) |
7424 | 0 | { |
7425 | 0 | struct bpf_program *prog; |
7426 | 0 | size_t i; |
7427 | 0 | int err; |
7428 | |
|
7429 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7430 | 0 | prog = &obj->programs[i]; |
7431 | 0 | err = bpf_object__sanitize_prog(obj, prog); |
7432 | 0 | if (err) |
7433 | 0 | return err; |
7434 | 0 | } |
7435 | | |
7436 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7437 | 0 | prog = &obj->programs[i]; |
7438 | 0 | if (prog_is_subprog(obj, prog)) |
7439 | 0 | continue; |
7440 | 0 | if (!prog->autoload) { |
7441 | 0 | pr_debug("prog '%s': skipped loading\n", prog->name); |
7442 | 0 | continue; |
7443 | 0 | } |
7444 | 0 | prog->log_level |= log_level; |
7445 | |
|
7446 | 0 | if (obj->gen_loader) |
7447 | 0 | bpf_program_record_relos(prog); |
7448 | |
|
7449 | 0 | err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, |
7450 | 0 | obj->license, obj->kern_version, &prog->fd); |
7451 | 0 | if (err) { |
7452 | 0 | pr_warn("prog '%s': failed to load: %d\n", prog->name, err); |
7453 | 0 | return err; |
7454 | 0 | } |
7455 | 0 | } |
7456 | | |
7457 | 0 | bpf_object__free_relocs(obj); |
7458 | 0 | return 0; |
7459 | 0 | } |
7460 | | |
7461 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name); |
7462 | | |
7463 | | static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts) |
7464 | 1.95k | { |
7465 | 1.95k | struct bpf_program *prog; |
7466 | 1.95k | int err; |
7467 | | |
7468 | 5.82k | bpf_object__for_each_program(prog, obj) { |
7469 | 5.82k | prog->sec_def = find_sec_def(prog->sec_name); |
7470 | 5.82k | if (!prog->sec_def) { |
7471 | | /* couldn't guess, but user might manually specify */ |
7472 | 4.94k | pr_debug("prog '%s': unrecognized ELF section name '%s'\n", |
7473 | 4.94k | prog->name, prog->sec_name); |
7474 | 4.94k | continue; |
7475 | 4.94k | } |
7476 | | |
7477 | 879 | prog->type = prog->sec_def->prog_type; |
7478 | 879 | prog->expected_attach_type = prog->sec_def->expected_attach_type; |
7479 | | |
7480 | | /* sec_def can have custom callback which should be called |
7481 | | * after bpf_program is initialized to adjust its properties |
7482 | | */ |
7483 | 879 | if (prog->sec_def->prog_setup_fn) { |
7484 | 0 | err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); |
7485 | 0 | if (err < 0) { |
7486 | 0 | pr_warn("prog '%s': failed to initialize: %d\n", |
7487 | 0 | prog->name, err); |
7488 | 0 | return err; |
7489 | 0 | } |
7490 | 0 | } |
7491 | 879 | } |
7492 | | |
7493 | 1.95k | return 0; |
7494 | 1.95k | } |
7495 | | |
7496 | | static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, |
7497 | | const struct bpf_object_open_opts *opts) |
7498 | 10.3k | { |
7499 | 10.3k | const char *obj_name, *kconfig, *btf_tmp_path; |
7500 | 10.3k | struct bpf_object *obj; |
7501 | 10.3k | char tmp_name[64]; |
7502 | 10.3k | int err; |
7503 | 10.3k | char *log_buf; |
7504 | 10.3k | size_t log_size; |
7505 | 10.3k | __u32 log_level; |
7506 | | |
7507 | 10.3k | if (elf_version(EV_CURRENT) == EV_NONE) { |
7508 | 0 | pr_warn("failed to init libelf for %s\n", |
7509 | 0 | path ? : "(mem buf)"); |
7510 | 0 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
7511 | 0 | } |
7512 | | |
7513 | 10.3k | if (!OPTS_VALID(opts, bpf_object_open_opts)) |
7514 | 0 | return ERR_PTR(-EINVAL); |
7515 | | |
7516 | 10.3k | obj_name = OPTS_GET(opts, object_name, NULL); |
7517 | 10.3k | if (obj_buf) { |
7518 | 10.3k | if (!obj_name) { |
7519 | 0 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
7520 | 0 | (unsigned long)obj_buf, |
7521 | 0 | (unsigned long)obj_buf_sz); |
7522 | 0 | obj_name = tmp_name; |
7523 | 0 | } |
7524 | 10.3k | path = obj_name; |
7525 | 10.3k | pr_debug("loading object '%s' from buffer\n", obj_name); |
7526 | 10.3k | } |
7527 | | |
7528 | 10.3k | log_buf = OPTS_GET(opts, kernel_log_buf, NULL); |
7529 | 10.3k | log_size = OPTS_GET(opts, kernel_log_size, 0); |
7530 | 10.3k | log_level = OPTS_GET(opts, kernel_log_level, 0); |
7531 | 10.3k | if (log_size > UINT_MAX) |
7532 | 0 | return ERR_PTR(-EINVAL); |
7533 | 10.3k | if (log_size && !log_buf) |
7534 | 0 | return ERR_PTR(-EINVAL); |
7535 | | |
7536 | 10.3k | obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name); |
7537 | 10.3k | if (IS_ERR(obj)) |
7538 | 0 | return obj; |
7539 | | |
7540 | 10.3k | obj->log_buf = log_buf; |
7541 | 10.3k | obj->log_size = log_size; |
7542 | 10.3k | obj->log_level = log_level; |
7543 | | |
7544 | 10.3k | btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL); |
7545 | 10.3k | if (btf_tmp_path) { |
7546 | 0 | if (strlen(btf_tmp_path) >= PATH_MAX) { |
7547 | 0 | err = -ENAMETOOLONG; |
7548 | 0 | goto out; |
7549 | 0 | } |
7550 | 0 | obj->btf_custom_path = strdup(btf_tmp_path); |
7551 | 0 | if (!obj->btf_custom_path) { |
7552 | 0 | err = -ENOMEM; |
7553 | 0 | goto out; |
7554 | 0 | } |
7555 | 0 | } |
7556 | | |
7557 | 10.3k | kconfig = OPTS_GET(opts, kconfig, NULL); |
7558 | 10.3k | if (kconfig) { |
7559 | 0 | obj->kconfig = strdup(kconfig); |
7560 | 0 | if (!obj->kconfig) { |
7561 | 0 | err = -ENOMEM; |
7562 | 0 | goto out; |
7563 | 0 | } |
7564 | 0 | } |
7565 | | |
7566 | 10.3k | err = bpf_object__elf_init(obj); |
7567 | 10.3k | err = err ? : bpf_object__check_endianness(obj); |
7568 | 10.3k | err = err ? : bpf_object__elf_collect(obj); |
7569 | 10.3k | err = err ? : bpf_object__collect_externs(obj); |
7570 | 10.3k | err = err ? : bpf_object_fixup_btf(obj); |
7571 | 10.3k | err = err ? : bpf_object__init_maps(obj, opts); |
7572 | 10.3k | err = err ? : bpf_object_init_progs(obj, opts); |
7573 | 10.3k | err = err ? : bpf_object__collect_relos(obj); |
7574 | 10.3k | if (err) |
7575 | 8.84k | goto out; |
7576 | | |
7577 | 1.45k | bpf_object__elf_finish(obj); |
7578 | | |
7579 | 1.45k | return obj; |
7580 | 8.84k | out: |
7581 | 8.84k | bpf_object__close(obj); |
7582 | 8.84k | return ERR_PTR(err); |
7583 | 10.3k | } |
7584 | | |
7585 | | struct bpf_object * |
7586 | | bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts) |
7587 | 0 | { |
7588 | 0 | if (!path) |
7589 | 0 | return libbpf_err_ptr(-EINVAL); |
7590 | | |
7591 | 0 | pr_debug("loading %s\n", path); |
7592 | |
|
7593 | 0 | return libbpf_ptr(bpf_object_open(path, NULL, 0, opts)); |
7594 | 0 | } |
7595 | | |
7596 | | struct bpf_object *bpf_object__open(const char *path) |
7597 | 0 | { |
7598 | 0 | return bpf_object__open_file(path, NULL); |
7599 | 0 | } |
7600 | | |
7601 | | struct bpf_object * |
7602 | | bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, |
7603 | | const struct bpf_object_open_opts *opts) |
7604 | 10.3k | { |
7605 | 10.3k | if (!obj_buf || obj_buf_sz == 0) |
7606 | 0 | return libbpf_err_ptr(-EINVAL); |
7607 | | |
7608 | 10.3k | return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts)); |
7609 | 10.3k | } |
7610 | | |
7611 | | static int bpf_object_unload(struct bpf_object *obj) |
7612 | 10.3k | { |
7613 | 10.3k | size_t i; |
7614 | | |
7615 | 10.3k | if (!obj) |
7616 | 0 | return libbpf_err(-EINVAL); |
7617 | | |
7618 | 12.1k | for (i = 0; i < obj->nr_maps; i++) { |
7619 | 1.84k | zclose(obj->maps[i].fd); |
7620 | 1.84k | if (obj->maps[i].st_ops) |
7621 | 0 | zfree(&obj->maps[i].st_ops->kern_vdata); |
7622 | 1.84k | } |
7623 | | |
7624 | 21.5k | for (i = 0; i < obj->nr_programs; i++) |
7625 | 11.2k | bpf_program__unload(&obj->programs[i]); |
7626 | | |
7627 | 10.3k | return 0; |
7628 | 10.3k | } |
7629 | | |
7630 | | static int bpf_object__sanitize_maps(struct bpf_object *obj) |
7631 | 0 | { |
7632 | 0 | struct bpf_map *m; |
7633 | |
|
7634 | 0 | bpf_object__for_each_map(m, obj) { |
7635 | 0 | if (!bpf_map__is_internal(m)) |
7636 | 0 | continue; |
7637 | 0 | if (!kernel_supports(obj, FEAT_ARRAY_MMAP)) |
7638 | 0 | m->def.map_flags &= ~BPF_F_MMAPABLE; |
7639 | 0 | } |
7640 | |
|
7641 | 0 | return 0; |
7642 | 0 | } |
7643 | | |
7644 | | int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx) |
7645 | 0 | { |
7646 | 0 | char sym_type, sym_name[500]; |
7647 | 0 | unsigned long long sym_addr; |
7648 | 0 | int ret, err = 0; |
7649 | 0 | FILE *f; |
7650 | |
|
7651 | 0 | f = fopen("/proc/kallsyms", "re"); |
7652 | 0 | if (!f) { |
7653 | 0 | err = -errno; |
7654 | 0 | pr_warn("failed to open /proc/kallsyms: %d\n", err); |
7655 | 0 | return err; |
7656 | 0 | } |
7657 | | |
7658 | 0 | while (true) { |
7659 | 0 | ret = fscanf(f, "%llx %c %499s%*[^\n]\n", |
7660 | 0 | &sym_addr, &sym_type, sym_name); |
7661 | 0 | if (ret == EOF && feof(f)) |
7662 | 0 | break; |
7663 | 0 | if (ret != 3) { |
7664 | 0 | pr_warn("failed to read kallsyms entry: %d\n", ret); |
7665 | 0 | err = -EINVAL; |
7666 | 0 | break; |
7667 | 0 | } |
7668 | | |
7669 | 0 | err = cb(sym_addr, sym_type, sym_name, ctx); |
7670 | 0 | if (err) |
7671 | 0 | break; |
7672 | 0 | } |
7673 | |
|
7674 | 0 | fclose(f); |
7675 | 0 | return err; |
7676 | 0 | } |
7677 | | |
7678 | | static int kallsyms_cb(unsigned long long sym_addr, char sym_type, |
7679 | | const char *sym_name, void *ctx) |
7680 | 0 | { |
7681 | 0 | struct bpf_object *obj = ctx; |
7682 | 0 | const struct btf_type *t; |
7683 | 0 | struct extern_desc *ext; |
7684 | |
|
7685 | 0 | ext = find_extern_by_name(obj, sym_name); |
7686 | 0 | if (!ext || ext->type != EXT_KSYM) |
7687 | 0 | return 0; |
7688 | | |
7689 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
7690 | 0 | if (!btf_is_var(t)) |
7691 | 0 | return 0; |
7692 | | |
7693 | 0 | if (ext->is_set && ext->ksym.addr != sym_addr) { |
7694 | 0 | pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n", |
7695 | 0 | sym_name, ext->ksym.addr, sym_addr); |
7696 | 0 | return -EINVAL; |
7697 | 0 | } |
7698 | 0 | if (!ext->is_set) { |
7699 | 0 | ext->is_set = true; |
7700 | 0 | ext->ksym.addr = sym_addr; |
7701 | 0 | pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr); |
7702 | 0 | } |
7703 | 0 | return 0; |
7704 | 0 | } |
7705 | | |
7706 | | static int bpf_object__read_kallsyms_file(struct bpf_object *obj) |
7707 | 0 | { |
7708 | 0 | return libbpf_kallsyms_parse(kallsyms_cb, obj); |
7709 | 0 | } |
7710 | | |
7711 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
7712 | | __u16 kind, struct btf **res_btf, |
7713 | | struct module_btf **res_mod_btf) |
7714 | 0 | { |
7715 | 0 | struct module_btf *mod_btf; |
7716 | 0 | struct btf *btf; |
7717 | 0 | int i, id, err; |
7718 | |
|
7719 | 0 | btf = obj->btf_vmlinux; |
7720 | 0 | mod_btf = NULL; |
7721 | 0 | id = btf__find_by_name_kind(btf, ksym_name, kind); |
7722 | |
|
7723 | 0 | if (id == -ENOENT) { |
7724 | 0 | err = load_module_btfs(obj); |
7725 | 0 | if (err) |
7726 | 0 | return err; |
7727 | | |
7728 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
7729 | | /* we assume module_btf's BTF FD is always >0 */ |
7730 | 0 | mod_btf = &obj->btf_modules[i]; |
7731 | 0 | btf = mod_btf->btf; |
7732 | 0 | id = btf__find_by_name_kind_own(btf, ksym_name, kind); |
7733 | 0 | if (id != -ENOENT) |
7734 | 0 | break; |
7735 | 0 | } |
7736 | 0 | } |
7737 | 0 | if (id <= 0) |
7738 | 0 | return -ESRCH; |
7739 | | |
7740 | 0 | *res_btf = btf; |
7741 | 0 | *res_mod_btf = mod_btf; |
7742 | 0 | return id; |
7743 | 0 | } |
7744 | | |
7745 | | static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj, |
7746 | | struct extern_desc *ext) |
7747 | 0 | { |
7748 | 0 | const struct btf_type *targ_var, *targ_type; |
7749 | 0 | __u32 targ_type_id, local_type_id; |
7750 | 0 | struct module_btf *mod_btf = NULL; |
7751 | 0 | const char *targ_var_name; |
7752 | 0 | struct btf *btf = NULL; |
7753 | 0 | int id, err; |
7754 | |
|
7755 | 0 | id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); |
7756 | 0 | if (id < 0) { |
7757 | 0 | if (id == -ESRCH && ext->is_weak) |
7758 | 0 | return 0; |
7759 | 0 | pr_warn("extern (var ksym) '%s': not found in kernel BTF\n", |
7760 | 0 | ext->name); |
7761 | 0 | return id; |
7762 | 0 | } |
7763 | | |
7764 | | /* find local type_id */ |
7765 | 0 | local_type_id = ext->ksym.type_id; |
7766 | | |
7767 | | /* find target type_id */ |
7768 | 0 | targ_var = btf__type_by_id(btf, id); |
7769 | 0 | targ_var_name = btf__name_by_offset(btf, targ_var->name_off); |
7770 | 0 | targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); |
7771 | |
|
7772 | 0 | err = bpf_core_types_are_compat(obj->btf, local_type_id, |
7773 | 0 | btf, targ_type_id); |
7774 | 0 | if (err <= 0) { |
7775 | 0 | const struct btf_type *local_type; |
7776 | 0 | const char *targ_name, *local_name; |
7777 | |
|
7778 | 0 | local_type = btf__type_by_id(obj->btf, local_type_id); |
7779 | 0 | local_name = btf__name_by_offset(obj->btf, local_type->name_off); |
7780 | 0 | targ_name = btf__name_by_offset(btf, targ_type->name_off); |
7781 | |
|
7782 | 0 | pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n", |
7783 | 0 | ext->name, local_type_id, |
7784 | 0 | btf_kind_str(local_type), local_name, targ_type_id, |
7785 | 0 | btf_kind_str(targ_type), targ_name); |
7786 | 0 | return -EINVAL; |
7787 | 0 | } |
7788 | | |
7789 | 0 | ext->is_set = true; |
7790 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
7791 | 0 | ext->ksym.kernel_btf_id = id; |
7792 | 0 | pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n", |
7793 | 0 | ext->name, id, btf_kind_str(targ_var), targ_var_name); |
7794 | |
|
7795 | 0 | return 0; |
7796 | 0 | } |
7797 | | |
7798 | | static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, |
7799 | | struct extern_desc *ext) |
7800 | 0 | { |
7801 | 0 | int local_func_proto_id, kfunc_proto_id, kfunc_id; |
7802 | 0 | struct module_btf *mod_btf = NULL; |
7803 | 0 | const struct btf_type *kern_func; |
7804 | 0 | struct btf *kern_btf = NULL; |
7805 | 0 | int ret; |
7806 | |
|
7807 | 0 | local_func_proto_id = ext->ksym.type_id; |
7808 | |
|
7809 | 0 | kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf, |
7810 | 0 | &mod_btf); |
7811 | 0 | if (kfunc_id < 0) { |
7812 | 0 | if (kfunc_id == -ESRCH && ext->is_weak) |
7813 | 0 | return 0; |
7814 | 0 | pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n", |
7815 | 0 | ext->name); |
7816 | 0 | return kfunc_id; |
7817 | 0 | } |
7818 | | |
7819 | 0 | kern_func = btf__type_by_id(kern_btf, kfunc_id); |
7820 | 0 | kfunc_proto_id = kern_func->type; |
7821 | |
|
7822 | 0 | ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, |
7823 | 0 | kern_btf, kfunc_proto_id); |
7824 | 0 | if (ret <= 0) { |
7825 | 0 | if (ext->is_weak) |
7826 | 0 | return 0; |
7827 | | |
7828 | 0 | pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n", |
7829 | 0 | ext->name, local_func_proto_id, |
7830 | 0 | mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); |
7831 | 0 | return -EINVAL; |
7832 | 0 | } |
7833 | | |
7834 | | /* set index for module BTF fd in fd_array, if unset */ |
7835 | 0 | if (mod_btf && !mod_btf->fd_array_idx) { |
7836 | | /* insn->off is s16 */ |
7837 | 0 | if (obj->fd_array_cnt == INT16_MAX) { |
7838 | 0 | pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n", |
7839 | 0 | ext->name, mod_btf->fd_array_idx); |
7840 | 0 | return -E2BIG; |
7841 | 0 | } |
7842 | | /* Cannot use index 0 for module BTF fd */ |
7843 | 0 | if (!obj->fd_array_cnt) |
7844 | 0 | obj->fd_array_cnt = 1; |
7845 | |
|
7846 | 0 | ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), |
7847 | 0 | obj->fd_array_cnt + 1); |
7848 | 0 | if (ret) |
7849 | 0 | return ret; |
7850 | 0 | mod_btf->fd_array_idx = obj->fd_array_cnt; |
7851 | | /* we assume module BTF FD is always >0 */ |
7852 | 0 | obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; |
7853 | 0 | } |
7854 | | |
7855 | 0 | ext->is_set = true; |
7856 | 0 | ext->ksym.kernel_btf_id = kfunc_id; |
7857 | 0 | ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; |
7858 | | /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data() |
7859 | | * populates FD into ld_imm64 insn when it's used to point to kfunc. |
7860 | | * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. |
7861 | | * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. |
7862 | | */ |
7863 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
7864 | 0 | pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n", |
7865 | 0 | ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); |
7866 | |
|
7867 | 0 | return 0; |
7868 | 0 | } |
7869 | | |
7870 | | static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) |
7871 | 0 | { |
7872 | 0 | const struct btf_type *t; |
7873 | 0 | struct extern_desc *ext; |
7874 | 0 | int i, err; |
7875 | |
|
7876 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
7877 | 0 | ext = &obj->externs[i]; |
7878 | 0 | if (ext->type != EXT_KSYM || !ext->ksym.type_id) |
7879 | 0 | continue; |
7880 | | |
7881 | 0 | if (obj->gen_loader) { |
7882 | 0 | ext->is_set = true; |
7883 | 0 | ext->ksym.kernel_btf_obj_fd = 0; |
7884 | 0 | ext->ksym.kernel_btf_id = 0; |
7885 | 0 | continue; |
7886 | 0 | } |
7887 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
7888 | 0 | if (btf_is_var(t)) |
7889 | 0 | err = bpf_object__resolve_ksym_var_btf_id(obj, ext); |
7890 | 0 | else |
7891 | 0 | err = bpf_object__resolve_ksym_func_btf_id(obj, ext); |
7892 | 0 | if (err) |
7893 | 0 | return err; |
7894 | 0 | } |
7895 | 0 | return 0; |
7896 | 0 | } |
7897 | | |
7898 | | static int bpf_object__resolve_externs(struct bpf_object *obj, |
7899 | | const char *extra_kconfig) |
7900 | 0 | { |
7901 | 0 | bool need_config = false, need_kallsyms = false; |
7902 | 0 | bool need_vmlinux_btf = false; |
7903 | 0 | struct extern_desc *ext; |
7904 | 0 | void *kcfg_data = NULL; |
7905 | 0 | int err, i; |
7906 | |
|
7907 | 0 | if (obj->nr_extern == 0) |
7908 | 0 | return 0; |
7909 | | |
7910 | 0 | if (obj->kconfig_map_idx >= 0) |
7911 | 0 | kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; |
7912 | |
|
7913 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
7914 | 0 | ext = &obj->externs[i]; |
7915 | |
|
7916 | 0 | if (ext->type == EXT_KSYM) { |
7917 | 0 | if (ext->ksym.type_id) |
7918 | 0 | need_vmlinux_btf = true; |
7919 | 0 | else |
7920 | 0 | need_kallsyms = true; |
7921 | 0 | continue; |
7922 | 0 | } else if (ext->type == EXT_KCFG) { |
7923 | 0 | void *ext_ptr = kcfg_data + ext->kcfg.data_off; |
7924 | 0 | __u64 value = 0; |
7925 | | |
7926 | | /* Kconfig externs need actual /proc/config.gz */ |
7927 | 0 | if (str_has_pfx(ext->name, "CONFIG_")) { |
7928 | 0 | need_config = true; |
7929 | 0 | continue; |
7930 | 0 | } |
7931 | | |
7932 | | /* Virtual kcfg externs are customly handled by libbpf */ |
7933 | 0 | if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { |
7934 | 0 | value = get_kernel_version(); |
7935 | 0 | if (!value) { |
7936 | 0 | pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); |
7937 | 0 | return -EINVAL; |
7938 | 0 | } |
7939 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { |
7940 | 0 | value = kernel_supports(obj, FEAT_BPF_COOKIE); |
7941 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { |
7942 | 0 | value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER); |
7943 | 0 | } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { |
7944 | | /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed |
7945 | | * __kconfig externs, where LINUX_ ones are virtual and filled out |
7946 | | * customly by libbpf (their values don't come from Kconfig). |
7947 | | * If LINUX_xxx variable is not recognized by libbpf, but is marked |
7948 | | * __weak, it defaults to zero value, just like for CONFIG_xxx |
7949 | | * externs. |
7950 | | */ |
7951 | 0 | pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); |
7952 | 0 | return -EINVAL; |
7953 | 0 | } |
7954 | | |
7955 | 0 | err = set_kcfg_value_num(ext, ext_ptr, value); |
7956 | 0 | if (err) |
7957 | 0 | return err; |
7958 | 0 | pr_debug("extern (kcfg) '%s': set to 0x%llx\n", |
7959 | 0 | ext->name, (long long)value); |
7960 | 0 | } else { |
7961 | 0 | pr_warn("extern '%s': unrecognized extern kind\n", ext->name); |
7962 | 0 | return -EINVAL; |
7963 | 0 | } |
7964 | 0 | } |
7965 | 0 | if (need_config && extra_kconfig) { |
7966 | 0 | err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data); |
7967 | 0 | if (err) |
7968 | 0 | return -EINVAL; |
7969 | 0 | need_config = false; |
7970 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
7971 | 0 | ext = &obj->externs[i]; |
7972 | 0 | if (ext->type == EXT_KCFG && !ext->is_set) { |
7973 | 0 | need_config = true; |
7974 | 0 | break; |
7975 | 0 | } |
7976 | 0 | } |
7977 | 0 | } |
7978 | 0 | if (need_config) { |
7979 | 0 | err = bpf_object__read_kconfig_file(obj, kcfg_data); |
7980 | 0 | if (err) |
7981 | 0 | return -EINVAL; |
7982 | 0 | } |
7983 | 0 | if (need_kallsyms) { |
7984 | 0 | err = bpf_object__read_kallsyms_file(obj); |
7985 | 0 | if (err) |
7986 | 0 | return -EINVAL; |
7987 | 0 | } |
7988 | 0 | if (need_vmlinux_btf) { |
7989 | 0 | err = bpf_object__resolve_ksyms_btf_id(obj); |
7990 | 0 | if (err) |
7991 | 0 | return -EINVAL; |
7992 | 0 | } |
7993 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
7994 | 0 | ext = &obj->externs[i]; |
7995 | |
|
7996 | 0 | if (!ext->is_set && !ext->is_weak) { |
7997 | 0 | pr_warn("extern '%s' (strong): not resolved\n", ext->name); |
7998 | 0 | return -ESRCH; |
7999 | 0 | } else if (!ext->is_set) { |
8000 | 0 | pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n", |
8001 | 0 | ext->name); |
8002 | 0 | } |
8003 | 0 | } |
8004 | | |
8005 | 0 | return 0; |
8006 | 0 | } |
8007 | | |
8008 | | static void bpf_map_prepare_vdata(const struct bpf_map *map) |
8009 | 0 | { |
8010 | 0 | struct bpf_struct_ops *st_ops; |
8011 | 0 | __u32 i; |
8012 | |
|
8013 | 0 | st_ops = map->st_ops; |
8014 | 0 | for (i = 0; i < btf_vlen(st_ops->type); i++) { |
8015 | 0 | struct bpf_program *prog = st_ops->progs[i]; |
8016 | 0 | void *kern_data; |
8017 | 0 | int prog_fd; |
8018 | |
|
8019 | 0 | if (!prog) |
8020 | 0 | continue; |
8021 | | |
8022 | 0 | prog_fd = bpf_program__fd(prog); |
8023 | 0 | kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; |
8024 | 0 | *(unsigned long *)kern_data = prog_fd; |
8025 | 0 | } |
8026 | 0 | } |
8027 | | |
8028 | | static int bpf_object_prepare_struct_ops(struct bpf_object *obj) |
8029 | 0 | { |
8030 | 0 | int i; |
8031 | |
|
8032 | 0 | for (i = 0; i < obj->nr_maps; i++) |
8033 | 0 | if (bpf_map__is_struct_ops(&obj->maps[i])) |
8034 | 0 | bpf_map_prepare_vdata(&obj->maps[i]); |
8035 | |
|
8036 | 0 | return 0; |
8037 | 0 | } |
8038 | | |
8039 | | static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path) |
8040 | 0 | { |
8041 | 0 | int err, i; |
8042 | |
|
8043 | 0 | if (!obj) |
8044 | 0 | return libbpf_err(-EINVAL); |
8045 | | |
8046 | 0 | if (obj->loaded) { |
8047 | 0 | pr_warn("object '%s': load can't be attempted twice\n", obj->name); |
8048 | 0 | return libbpf_err(-EINVAL); |
8049 | 0 | } |
8050 | | |
8051 | 0 | if (obj->gen_loader) |
8052 | 0 | bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); |
8053 | |
|
8054 | 0 | err = bpf_object__probe_loading(obj); |
8055 | 0 | err = err ? : bpf_object__load_vmlinux_btf(obj, false); |
8056 | 0 | err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); |
8057 | 0 | err = err ? : bpf_object__sanitize_and_load_btf(obj); |
8058 | 0 | err = err ? : bpf_object__sanitize_maps(obj); |
8059 | 0 | err = err ? : bpf_object__init_kern_struct_ops_maps(obj); |
8060 | 0 | err = err ? : bpf_object__create_maps(obj); |
8061 | 0 | err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); |
8062 | 0 | err = err ? : bpf_object__load_progs(obj, extra_log_level); |
8063 | 0 | err = err ? : bpf_object_init_prog_arrays(obj); |
8064 | 0 | err = err ? : bpf_object_prepare_struct_ops(obj); |
8065 | |
|
8066 | 0 | if (obj->gen_loader) { |
8067 | | /* reset FDs */ |
8068 | 0 | if (obj->btf) |
8069 | 0 | btf__set_fd(obj->btf, -1); |
8070 | 0 | for (i = 0; i < obj->nr_maps; i++) |
8071 | 0 | obj->maps[i].fd = -1; |
8072 | 0 | if (!err) |
8073 | 0 | err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); |
8074 | 0 | } |
8075 | | |
8076 | | /* clean up fd_array */ |
8077 | 0 | zfree(&obj->fd_array); |
8078 | | |
8079 | | /* clean up module BTFs */ |
8080 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
8081 | 0 | close(obj->btf_modules[i].fd); |
8082 | 0 | btf__free(obj->btf_modules[i].btf); |
8083 | 0 | free(obj->btf_modules[i].name); |
8084 | 0 | } |
8085 | 0 | free(obj->btf_modules); |
8086 | | |
8087 | | /* clean up vmlinux BTF */ |
8088 | 0 | btf__free(obj->btf_vmlinux); |
8089 | 0 | obj->btf_vmlinux = NULL; |
8090 | |
|
8091 | 0 | obj->loaded = true; /* doesn't matter if successfully or not */ |
8092 | |
|
8093 | 0 | if (err) |
8094 | 0 | goto out; |
8095 | | |
8096 | 0 | return 0; |
8097 | 0 | out: |
8098 | | /* unpin any maps that were auto-pinned during load */ |
8099 | 0 | for (i = 0; i < obj->nr_maps; i++) |
8100 | 0 | if (obj->maps[i].pinned && !obj->maps[i].reused) |
8101 | 0 | bpf_map__unpin(&obj->maps[i], NULL); |
8102 | |
|
8103 | 0 | bpf_object_unload(obj); |
8104 | 0 | pr_warn("failed to load object '%s'\n", obj->path); |
8105 | 0 | return libbpf_err(err); |
8106 | 0 | } |
8107 | | |
8108 | | int bpf_object__load(struct bpf_object *obj) |
8109 | 0 | { |
8110 | 0 | return bpf_object_load(obj, 0, NULL); |
8111 | 0 | } |
8112 | | |
8113 | | static int make_parent_dir(const char *path) |
8114 | 0 | { |
8115 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8116 | 0 | char *dname, *dir; |
8117 | 0 | int err = 0; |
8118 | |
|
8119 | 0 | dname = strdup(path); |
8120 | 0 | if (dname == NULL) |
8121 | 0 | return -ENOMEM; |
8122 | | |
8123 | 0 | dir = dirname(dname); |
8124 | 0 | if (mkdir(dir, 0700) && errno != EEXIST) |
8125 | 0 | err = -errno; |
8126 | |
|
8127 | 0 | free(dname); |
8128 | 0 | if (err) { |
8129 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
8130 | 0 | pr_warn("failed to mkdir %s: %s\n", path, cp); |
8131 | 0 | } |
8132 | 0 | return err; |
8133 | 0 | } |
8134 | | |
8135 | | static int check_path(const char *path) |
8136 | 0 | { |
8137 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8138 | 0 | struct statfs st_fs; |
8139 | 0 | char *dname, *dir; |
8140 | 0 | int err = 0; |
8141 | |
|
8142 | 0 | if (path == NULL) |
8143 | 0 | return -EINVAL; |
8144 | | |
8145 | 0 | dname = strdup(path); |
8146 | 0 | if (dname == NULL) |
8147 | 0 | return -ENOMEM; |
8148 | | |
8149 | 0 | dir = dirname(dname); |
8150 | 0 | if (statfs(dir, &st_fs)) { |
8151 | 0 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
8152 | 0 | pr_warn("failed to statfs %s: %s\n", dir, cp); |
8153 | 0 | err = -errno; |
8154 | 0 | } |
8155 | 0 | free(dname); |
8156 | |
|
8157 | 0 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
8158 | 0 | pr_warn("specified path %s is not on BPF FS\n", path); |
8159 | 0 | err = -EINVAL; |
8160 | 0 | } |
8161 | |
|
8162 | 0 | return err; |
8163 | 0 | } |
8164 | | |
8165 | | int bpf_program__pin(struct bpf_program *prog, const char *path) |
8166 | 0 | { |
8167 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8168 | 0 | int err; |
8169 | |
|
8170 | 0 | if (prog->fd < 0) { |
8171 | 0 | pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); |
8172 | 0 | return libbpf_err(-EINVAL); |
8173 | 0 | } |
8174 | | |
8175 | 0 | err = make_parent_dir(path); |
8176 | 0 | if (err) |
8177 | 0 | return libbpf_err(err); |
8178 | | |
8179 | 0 | err = check_path(path); |
8180 | 0 | if (err) |
8181 | 0 | return libbpf_err(err); |
8182 | | |
8183 | 0 | if (bpf_obj_pin(prog->fd, path)) { |
8184 | 0 | err = -errno; |
8185 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
8186 | 0 | pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp); |
8187 | 0 | return libbpf_err(err); |
8188 | 0 | } |
8189 | | |
8190 | 0 | pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); |
8191 | 0 | return 0; |
8192 | 0 | } |
8193 | | |
8194 | | int bpf_program__unpin(struct bpf_program *prog, const char *path) |
8195 | 0 | { |
8196 | 0 | int err; |
8197 | |
|
8198 | 0 | if (prog->fd < 0) { |
8199 | 0 | pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); |
8200 | 0 | return libbpf_err(-EINVAL); |
8201 | 0 | } |
8202 | | |
8203 | 0 | err = check_path(path); |
8204 | 0 | if (err) |
8205 | 0 | return libbpf_err(err); |
8206 | | |
8207 | 0 | err = unlink(path); |
8208 | 0 | if (err) |
8209 | 0 | return libbpf_err(-errno); |
8210 | | |
8211 | 0 | pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); |
8212 | 0 | return 0; |
8213 | 0 | } |
8214 | | |
8215 | | int bpf_map__pin(struct bpf_map *map, const char *path) |
8216 | 0 | { |
8217 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
8218 | 0 | int err; |
8219 | |
|
8220 | 0 | if (map == NULL) { |
8221 | 0 | pr_warn("invalid map pointer\n"); |
8222 | 0 | return libbpf_err(-EINVAL); |
8223 | 0 | } |
8224 | | |
8225 | 0 | if (map->pin_path) { |
8226 | 0 | if (path && strcmp(path, map->pin_path)) { |
8227 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
8228 | 0 | bpf_map__name(map), map->pin_path, path); |
8229 | 0 | return libbpf_err(-EINVAL); |
8230 | 0 | } else if (map->pinned) { |
8231 | 0 | pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", |
8232 | 0 | bpf_map__name(map), map->pin_path); |
8233 | 0 | return 0; |
8234 | 0 | } |
8235 | 0 | } else { |
8236 | 0 | if (!path) { |
8237 | 0 | pr_warn("missing a path to pin map '%s' at\n", |
8238 | 0 | bpf_map__name(map)); |
8239 | 0 | return libbpf_err(-EINVAL); |
8240 | 0 | } else if (map->pinned) { |
8241 | 0 | pr_warn("map '%s' already pinned\n", bpf_map__name(map)); |
8242 | 0 | return libbpf_err(-EEXIST); |
8243 | 0 | } |
8244 | | |
8245 | 0 | map->pin_path = strdup(path); |
8246 | 0 | if (!map->pin_path) { |
8247 | 0 | err = -errno; |
8248 | 0 | goto out_err; |
8249 | 0 | } |
8250 | 0 | } |
8251 | | |
8252 | 0 | err = make_parent_dir(map->pin_path); |
8253 | 0 | if (err) |
8254 | 0 | return libbpf_err(err); |
8255 | | |
8256 | 0 | err = check_path(map->pin_path); |
8257 | 0 | if (err) |
8258 | 0 | return libbpf_err(err); |
8259 | | |
8260 | 0 | if (bpf_obj_pin(map->fd, map->pin_path)) { |
8261 | 0 | err = -errno; |
8262 | 0 | goto out_err; |
8263 | 0 | } |
8264 | | |
8265 | 0 | map->pinned = true; |
8266 | 0 | pr_debug("pinned map '%s'\n", map->pin_path); |
8267 | |
|
8268 | 0 | return 0; |
8269 | | |
8270 | 0 | out_err: |
8271 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
8272 | 0 | pr_warn("failed to pin map: %s\n", cp); |
8273 | 0 | return libbpf_err(err); |
8274 | 0 | } |
8275 | | |
8276 | | int bpf_map__unpin(struct bpf_map *map, const char *path) |
8277 | 0 | { |
8278 | 0 | int err; |
8279 | |
|
8280 | 0 | if (map == NULL) { |
8281 | 0 | pr_warn("invalid map pointer\n"); |
8282 | 0 | return libbpf_err(-EINVAL); |
8283 | 0 | } |
8284 | | |
8285 | 0 | if (map->pin_path) { |
8286 | 0 | if (path && strcmp(path, map->pin_path)) { |
8287 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
8288 | 0 | bpf_map__name(map), map->pin_path, path); |
8289 | 0 | return libbpf_err(-EINVAL); |
8290 | 0 | } |
8291 | 0 | path = map->pin_path; |
8292 | 0 | } else if (!path) { |
8293 | 0 | pr_warn("no path to unpin map '%s' from\n", |
8294 | 0 | bpf_map__name(map)); |
8295 | 0 | return libbpf_err(-EINVAL); |
8296 | 0 | } |
8297 | | |
8298 | 0 | err = check_path(path); |
8299 | 0 | if (err) |
8300 | 0 | return libbpf_err(err); |
8301 | | |
8302 | 0 | err = unlink(path); |
8303 | 0 | if (err != 0) |
8304 | 0 | return libbpf_err(-errno); |
8305 | | |
8306 | 0 | map->pinned = false; |
8307 | 0 | pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path); |
8308 | |
|
8309 | 0 | return 0; |
8310 | 0 | } |
8311 | | |
8312 | | int bpf_map__set_pin_path(struct bpf_map *map, const char *path) |
8313 | 0 | { |
8314 | 0 | char *new = NULL; |
8315 | |
|
8316 | 0 | if (path) { |
8317 | 0 | new = strdup(path); |
8318 | 0 | if (!new) |
8319 | 0 | return libbpf_err(-errno); |
8320 | 0 | } |
8321 | | |
8322 | 0 | free(map->pin_path); |
8323 | 0 | map->pin_path = new; |
8324 | 0 | return 0; |
8325 | 0 | } |
8326 | | |
8327 | | __alias(bpf_map__pin_path) |
8328 | | const char *bpf_map__get_pin_path(const struct bpf_map *map); |
8329 | | |
8330 | | const char *bpf_map__pin_path(const struct bpf_map *map) |
8331 | 0 | { |
8332 | 0 | return map->pin_path; |
8333 | 0 | } |
8334 | | |
8335 | | bool bpf_map__is_pinned(const struct bpf_map *map) |
8336 | 0 | { |
8337 | 0 | return map->pinned; |
8338 | 0 | } |
8339 | | |
8340 | | static void sanitize_pin_path(char *s) |
8341 | 0 | { |
8342 | | /* bpffs disallows periods in path names */ |
8343 | 0 | while (*s) { |
8344 | 0 | if (*s == '.') |
8345 | 0 | *s = '_'; |
8346 | 0 | s++; |
8347 | 0 | } |
8348 | 0 | } |
8349 | | |
8350 | | int bpf_object__pin_maps(struct bpf_object *obj, const char *path) |
8351 | 0 | { |
8352 | 0 | struct bpf_map *map; |
8353 | 0 | int err; |
8354 | |
|
8355 | 0 | if (!obj) |
8356 | 0 | return libbpf_err(-ENOENT); |
8357 | | |
8358 | 0 | if (!obj->loaded) { |
8359 | 0 | pr_warn("object not yet loaded; load it first\n"); |
8360 | 0 | return libbpf_err(-ENOENT); |
8361 | 0 | } |
8362 | | |
8363 | 0 | bpf_object__for_each_map(map, obj) { |
8364 | 0 | char *pin_path = NULL; |
8365 | 0 | char buf[PATH_MAX]; |
8366 | |
|
8367 | 0 | if (!map->autocreate) |
8368 | 0 | continue; |
8369 | | |
8370 | 0 | if (path) { |
8371 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
8372 | 0 | if (err) |
8373 | 0 | goto err_unpin_maps; |
8374 | 0 | sanitize_pin_path(buf); |
8375 | 0 | pin_path = buf; |
8376 | 0 | } else if (!map->pin_path) { |
8377 | 0 | continue; |
8378 | 0 | } |
8379 | | |
8380 | 0 | err = bpf_map__pin(map, pin_path); |
8381 | 0 | if (err) |
8382 | 0 | goto err_unpin_maps; |
8383 | 0 | } |
8384 | | |
8385 | 0 | return 0; |
8386 | | |
8387 | 0 | err_unpin_maps: |
8388 | 0 | while ((map = bpf_object__prev_map(obj, map))) { |
8389 | 0 | if (!map->pin_path) |
8390 | 0 | continue; |
8391 | | |
8392 | 0 | bpf_map__unpin(map, NULL); |
8393 | 0 | } |
8394 | |
|
8395 | 0 | return libbpf_err(err); |
8396 | 0 | } |
8397 | | |
8398 | | int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) |
8399 | 0 | { |
8400 | 0 | struct bpf_map *map; |
8401 | 0 | int err; |
8402 | |
|
8403 | 0 | if (!obj) |
8404 | 0 | return libbpf_err(-ENOENT); |
8405 | | |
8406 | 0 | bpf_object__for_each_map(map, obj) { |
8407 | 0 | char *pin_path = NULL; |
8408 | 0 | char buf[PATH_MAX]; |
8409 | |
|
8410 | 0 | if (path) { |
8411 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
8412 | 0 | if (err) |
8413 | 0 | return libbpf_err(err); |
8414 | 0 | sanitize_pin_path(buf); |
8415 | 0 | pin_path = buf; |
8416 | 0 | } else if (!map->pin_path) { |
8417 | 0 | continue; |
8418 | 0 | } |
8419 | | |
8420 | 0 | err = bpf_map__unpin(map, pin_path); |
8421 | 0 | if (err) |
8422 | 0 | return libbpf_err(err); |
8423 | 0 | } |
8424 | | |
8425 | 0 | return 0; |
8426 | 0 | } |
8427 | | |
8428 | | int bpf_object__pin_programs(struct bpf_object *obj, const char *path) |
8429 | 0 | { |
8430 | 0 | struct bpf_program *prog; |
8431 | 0 | char buf[PATH_MAX]; |
8432 | 0 | int err; |
8433 | |
|
8434 | 0 | if (!obj) |
8435 | 0 | return libbpf_err(-ENOENT); |
8436 | | |
8437 | 0 | if (!obj->loaded) { |
8438 | 0 | pr_warn("object not yet loaded; load it first\n"); |
8439 | 0 | return libbpf_err(-ENOENT); |
8440 | 0 | } |
8441 | | |
8442 | 0 | bpf_object__for_each_program(prog, obj) { |
8443 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
8444 | 0 | if (err) |
8445 | 0 | goto err_unpin_programs; |
8446 | | |
8447 | 0 | err = bpf_program__pin(prog, buf); |
8448 | 0 | if (err) |
8449 | 0 | goto err_unpin_programs; |
8450 | 0 | } |
8451 | | |
8452 | 0 | return 0; |
8453 | | |
8454 | 0 | err_unpin_programs: |
8455 | 0 | while ((prog = bpf_object__prev_program(obj, prog))) { |
8456 | 0 | if (pathname_concat(buf, sizeof(buf), path, prog->name)) |
8457 | 0 | continue; |
8458 | | |
8459 | 0 | bpf_program__unpin(prog, buf); |
8460 | 0 | } |
8461 | |
|
8462 | 0 | return libbpf_err(err); |
8463 | 0 | } |
8464 | | |
8465 | | int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) |
8466 | 0 | { |
8467 | 0 | struct bpf_program *prog; |
8468 | 0 | int err; |
8469 | |
|
8470 | 0 | if (!obj) |
8471 | 0 | return libbpf_err(-ENOENT); |
8472 | | |
8473 | 0 | bpf_object__for_each_program(prog, obj) { |
8474 | 0 | char buf[PATH_MAX]; |
8475 | |
|
8476 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
8477 | 0 | if (err) |
8478 | 0 | return libbpf_err(err); |
8479 | | |
8480 | 0 | err = bpf_program__unpin(prog, buf); |
8481 | 0 | if (err) |
8482 | 0 | return libbpf_err(err); |
8483 | 0 | } |
8484 | | |
8485 | 0 | return 0; |
8486 | 0 | } |
8487 | | |
8488 | | int bpf_object__pin(struct bpf_object *obj, const char *path) |
8489 | 0 | { |
8490 | 0 | int err; |
8491 | |
|
8492 | 0 | err = bpf_object__pin_maps(obj, path); |
8493 | 0 | if (err) |
8494 | 0 | return libbpf_err(err); |
8495 | | |
8496 | 0 | err = bpf_object__pin_programs(obj, path); |
8497 | 0 | if (err) { |
8498 | 0 | bpf_object__unpin_maps(obj, path); |
8499 | 0 | return libbpf_err(err); |
8500 | 0 | } |
8501 | | |
8502 | 0 | return 0; |
8503 | 0 | } |
8504 | | |
8505 | | int bpf_object__unpin(struct bpf_object *obj, const char *path) |
8506 | 0 | { |
8507 | 0 | int err; |
8508 | |
|
8509 | 0 | err = bpf_object__unpin_programs(obj, path); |
8510 | 0 | if (err) |
8511 | 0 | return libbpf_err(err); |
8512 | | |
8513 | 0 | err = bpf_object__unpin_maps(obj, path); |
8514 | 0 | if (err) |
8515 | 0 | return libbpf_err(err); |
8516 | | |
8517 | 0 | return 0; |
8518 | 0 | } |
8519 | | |
8520 | | static void bpf_map__destroy(struct bpf_map *map) |
8521 | 1.84k | { |
8522 | 1.84k | if (map->inner_map) { |
8523 | 0 | bpf_map__destroy(map->inner_map); |
8524 | 0 | zfree(&map->inner_map); |
8525 | 0 | } |
8526 | | |
8527 | 1.84k | zfree(&map->init_slots); |
8528 | 1.84k | map->init_slots_sz = 0; |
8529 | | |
8530 | 1.84k | if (map->mmaped) { |
8531 | 1.29k | size_t mmap_sz; |
8532 | | |
8533 | 1.29k | mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); |
8534 | 1.29k | munmap(map->mmaped, mmap_sz); |
8535 | 1.29k | map->mmaped = NULL; |
8536 | 1.29k | } |
8537 | | |
8538 | 1.84k | if (map->st_ops) { |
8539 | 0 | zfree(&map->st_ops->data); |
8540 | 0 | zfree(&map->st_ops->progs); |
8541 | 0 | zfree(&map->st_ops->kern_func_off); |
8542 | 0 | zfree(&map->st_ops); |
8543 | 0 | } |
8544 | | |
8545 | 1.84k | zfree(&map->name); |
8546 | 1.84k | zfree(&map->real_name); |
8547 | 1.84k | zfree(&map->pin_path); |
8548 | | |
8549 | 1.84k | if (map->fd >= 0) |
8550 | 0 | zclose(map->fd); |
8551 | 1.84k | } |
8552 | | |
8553 | | void bpf_object__close(struct bpf_object *obj) |
8554 | 10.3k | { |
8555 | 10.3k | size_t i; |
8556 | | |
8557 | 10.3k | if (IS_ERR_OR_NULL(obj)) |
8558 | 0 | return; |
8559 | | |
8560 | 10.3k | usdt_manager_free(obj->usdt_man); |
8561 | 10.3k | obj->usdt_man = NULL; |
8562 | | |
8563 | 10.3k | bpf_gen__free(obj->gen_loader); |
8564 | 10.3k | bpf_object__elf_finish(obj); |
8565 | 10.3k | bpf_object_unload(obj); |
8566 | 10.3k | btf__free(obj->btf); |
8567 | 10.3k | btf__free(obj->btf_vmlinux); |
8568 | 10.3k | btf_ext__free(obj->btf_ext); |
8569 | | |
8570 | 12.1k | for (i = 0; i < obj->nr_maps; i++) |
8571 | 1.84k | bpf_map__destroy(&obj->maps[i]); |
8572 | | |
8573 | 10.3k | zfree(&obj->btf_custom_path); |
8574 | 10.3k | zfree(&obj->kconfig); |
8575 | | |
8576 | 12.1k | for (i = 0; i < obj->nr_extern; i++) |
8577 | 1.88k | zfree(&obj->externs[i].essent_name); |
8578 | | |
8579 | 10.3k | zfree(&obj->externs); |
8580 | 10.3k | obj->nr_extern = 0; |
8581 | | |
8582 | 10.3k | zfree(&obj->maps); |
8583 | 10.3k | obj->nr_maps = 0; |
8584 | | |
8585 | 10.3k | if (obj->programs && obj->nr_programs) { |
8586 | 11.9k | for (i = 0; i < obj->nr_programs; i++) |
8587 | 11.2k | bpf_program__exit(&obj->programs[i]); |
8588 | 681 | } |
8589 | 10.3k | zfree(&obj->programs); |
8590 | | |
8591 | 10.3k | free(obj); |
8592 | 10.3k | } |
8593 | | |
8594 | | const char *bpf_object__name(const struct bpf_object *obj) |
8595 | 0 | { |
8596 | 0 | return obj ? obj->name : libbpf_err_ptr(-EINVAL); |
8597 | 0 | } |
8598 | | |
8599 | | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
8600 | 0 | { |
8601 | 0 | return obj ? obj->kern_version : 0; |
8602 | 0 | } |
8603 | | |
8604 | | struct btf *bpf_object__btf(const struct bpf_object *obj) |
8605 | 0 | { |
8606 | 0 | return obj ? obj->btf : NULL; |
8607 | 0 | } |
8608 | | |
8609 | | int bpf_object__btf_fd(const struct bpf_object *obj) |
8610 | 0 | { |
8611 | 0 | return obj->btf ? btf__fd(obj->btf) : -1; |
8612 | 0 | } |
8613 | | |
8614 | | int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version) |
8615 | 0 | { |
8616 | 0 | if (obj->loaded) |
8617 | 0 | return libbpf_err(-EINVAL); |
8618 | | |
8619 | 0 | obj->kern_version = kern_version; |
8620 | |
|
8621 | 0 | return 0; |
8622 | 0 | } |
8623 | | |
8624 | | int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts) |
8625 | 0 | { |
8626 | 0 | struct bpf_gen *gen; |
8627 | |
|
8628 | 0 | if (!opts) |
8629 | 0 | return -EFAULT; |
8630 | 0 | if (!OPTS_VALID(opts, gen_loader_opts)) |
8631 | 0 | return -EINVAL; |
8632 | 0 | gen = calloc(sizeof(*gen), 1); |
8633 | 0 | if (!gen) |
8634 | 0 | return -ENOMEM; |
8635 | 0 | gen->opts = opts; |
8636 | 0 | obj->gen_loader = gen; |
8637 | 0 | return 0; |
8638 | 0 | } |
8639 | | |
8640 | | static struct bpf_program * |
8641 | | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
8642 | | bool forward) |
8643 | 10.7k | { |
8644 | 10.7k | size_t nr_programs = obj->nr_programs; |
8645 | 10.7k | ssize_t idx; |
8646 | | |
8647 | 10.7k | if (!nr_programs) |
8648 | 1.35k | return NULL; |
8649 | | |
8650 | 9.41k | if (!p) |
8651 | | /* Iter from the beginning */ |
8652 | 604 | return forward ? &obj->programs[0] : |
8653 | 604 | &obj->programs[nr_programs - 1]; |
8654 | | |
8655 | 8.80k | if (p->obj != obj) { |
8656 | 0 | pr_warn("error: program handler doesn't match object\n"); |
8657 | 0 | return errno = EINVAL, NULL; |
8658 | 0 | } |
8659 | | |
8660 | 8.80k | idx = (p - obj->programs) + (forward ? 1 : -1); |
8661 | 8.80k | if (idx >= obj->nr_programs || idx < 0) |
8662 | 604 | return NULL; |
8663 | 8.20k | return &obj->programs[idx]; |
8664 | 8.80k | } |
8665 | | |
8666 | | struct bpf_program * |
8667 | | bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev) |
8668 | 7.77k | { |
8669 | 7.77k | struct bpf_program *prog = prev; |
8670 | | |
8671 | 10.7k | do { |
8672 | 10.7k | prog = __bpf_program__iter(prog, obj, true); |
8673 | 10.7k | } while (prog && prog_is_subprog(obj, prog)); |
8674 | | |
8675 | 7.77k | return prog; |
8676 | 7.77k | } |
8677 | | |
8678 | | struct bpf_program * |
8679 | | bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next) |
8680 | 0 | { |
8681 | 0 | struct bpf_program *prog = next; |
8682 | |
|
8683 | 0 | do { |
8684 | 0 | prog = __bpf_program__iter(prog, obj, false); |
8685 | 0 | } while (prog && prog_is_subprog(obj, prog)); |
8686 | |
|
8687 | 0 | return prog; |
8688 | 0 | } |
8689 | | |
8690 | | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
8691 | 0 | { |
8692 | 0 | prog->prog_ifindex = ifindex; |
8693 | 0 | } |
8694 | | |
8695 | | const char *bpf_program__name(const struct bpf_program *prog) |
8696 | 0 | { |
8697 | 0 | return prog->name; |
8698 | 0 | } |
8699 | | |
8700 | | const char *bpf_program__section_name(const struct bpf_program *prog) |
8701 | 0 | { |
8702 | 0 | return prog->sec_name; |
8703 | 0 | } |
8704 | | |
8705 | | bool bpf_program__autoload(const struct bpf_program *prog) |
8706 | 0 | { |
8707 | 0 | return prog->autoload; |
8708 | 0 | } |
8709 | | |
8710 | | int bpf_program__set_autoload(struct bpf_program *prog, bool autoload) |
8711 | 0 | { |
8712 | 0 | if (prog->obj->loaded) |
8713 | 0 | return libbpf_err(-EINVAL); |
8714 | | |
8715 | 0 | prog->autoload = autoload; |
8716 | 0 | return 0; |
8717 | 0 | } |
8718 | | |
8719 | | bool bpf_program__autoattach(const struct bpf_program *prog) |
8720 | 0 | { |
8721 | 0 | return prog->autoattach; |
8722 | 0 | } |
8723 | | |
8724 | | void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach) |
8725 | 0 | { |
8726 | 0 | prog->autoattach = autoattach; |
8727 | 0 | } |
8728 | | |
8729 | | const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog) |
8730 | 0 | { |
8731 | 0 | return prog->insns; |
8732 | 0 | } |
8733 | | |
8734 | | size_t bpf_program__insn_cnt(const struct bpf_program *prog) |
8735 | 0 | { |
8736 | 0 | return prog->insns_cnt; |
8737 | 0 | } |
8738 | | |
8739 | | int bpf_program__set_insns(struct bpf_program *prog, |
8740 | | struct bpf_insn *new_insns, size_t new_insn_cnt) |
8741 | 0 | { |
8742 | 0 | struct bpf_insn *insns; |
8743 | |
|
8744 | 0 | if (prog->obj->loaded) |
8745 | 0 | return -EBUSY; |
8746 | | |
8747 | 0 | insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); |
8748 | | /* NULL is a valid return from reallocarray if the new count is zero */ |
8749 | 0 | if (!insns && new_insn_cnt) { |
8750 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", prog->name); |
8751 | 0 | return -ENOMEM; |
8752 | 0 | } |
8753 | 0 | memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns)); |
8754 | |
|
8755 | 0 | prog->insns = insns; |
8756 | 0 | prog->insns_cnt = new_insn_cnt; |
8757 | 0 | return 0; |
8758 | 0 | } |
8759 | | |
8760 | | int bpf_program__fd(const struct bpf_program *prog) |
8761 | 0 | { |
8762 | 0 | if (!prog) |
8763 | 0 | return libbpf_err(-EINVAL); |
8764 | | |
8765 | 0 | if (prog->fd < 0) |
8766 | 0 | return libbpf_err(-ENOENT); |
8767 | | |
8768 | 0 | return prog->fd; |
8769 | 0 | } |
8770 | | |
8771 | | __alias(bpf_program__type) |
8772 | | enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog); |
8773 | | |
8774 | | enum bpf_prog_type bpf_program__type(const struct bpf_program *prog) |
8775 | 0 | { |
8776 | 0 | return prog->type; |
8777 | 0 | } |
8778 | | |
8779 | | static size_t custom_sec_def_cnt; |
8780 | | static struct bpf_sec_def *custom_sec_defs; |
8781 | | static struct bpf_sec_def custom_fallback_def; |
8782 | | static bool has_custom_fallback_def; |
8783 | | static int last_custom_sec_def_handler_id; |
8784 | | |
8785 | | int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) |
8786 | 0 | { |
8787 | 0 | if (prog->obj->loaded) |
8788 | 0 | return libbpf_err(-EBUSY); |
8789 | | |
8790 | | /* if type is not changed, do nothing */ |
8791 | 0 | if (prog->type == type) |
8792 | 0 | return 0; |
8793 | | |
8794 | 0 | prog->type = type; |
8795 | | |
8796 | | /* If a program type was changed, we need to reset associated SEC() |
8797 | | * handler, as it will be invalid now. The only exception is a generic |
8798 | | * fallback handler, which by definition is program type-agnostic and |
8799 | | * is a catch-all custom handler, optionally set by the application, |
8800 | | * so should be able to handle any type of BPF program. |
8801 | | */ |
8802 | 0 | if (prog->sec_def != &custom_fallback_def) |
8803 | 0 | prog->sec_def = NULL; |
8804 | 0 | return 0; |
8805 | 0 | } |
8806 | | |
8807 | | __alias(bpf_program__expected_attach_type) |
8808 | | enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog); |
8809 | | |
8810 | | enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog) |
8811 | 0 | { |
8812 | 0 | return prog->expected_attach_type; |
8813 | 0 | } |
8814 | | |
8815 | | int bpf_program__set_expected_attach_type(struct bpf_program *prog, |
8816 | | enum bpf_attach_type type) |
8817 | 0 | { |
8818 | 0 | if (prog->obj->loaded) |
8819 | 0 | return libbpf_err(-EBUSY); |
8820 | | |
8821 | 0 | prog->expected_attach_type = type; |
8822 | 0 | return 0; |
8823 | 0 | } |
8824 | | |
8825 | | __u32 bpf_program__flags(const struct bpf_program *prog) |
8826 | 0 | { |
8827 | 0 | return prog->prog_flags; |
8828 | 0 | } |
8829 | | |
8830 | | int bpf_program__set_flags(struct bpf_program *prog, __u32 flags) |
8831 | 0 | { |
8832 | 0 | if (prog->obj->loaded) |
8833 | 0 | return libbpf_err(-EBUSY); |
8834 | | |
8835 | 0 | prog->prog_flags = flags; |
8836 | 0 | return 0; |
8837 | 0 | } |
8838 | | |
8839 | | __u32 bpf_program__log_level(const struct bpf_program *prog) |
8840 | 0 | { |
8841 | 0 | return prog->log_level; |
8842 | 0 | } |
8843 | | |
8844 | | int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level) |
8845 | 0 | { |
8846 | 0 | if (prog->obj->loaded) |
8847 | 0 | return libbpf_err(-EBUSY); |
8848 | | |
8849 | 0 | prog->log_level = log_level; |
8850 | 0 | return 0; |
8851 | 0 | } |
8852 | | |
8853 | | const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size) |
8854 | 0 | { |
8855 | 0 | *log_size = prog->log_size; |
8856 | 0 | return prog->log_buf; |
8857 | 0 | } |
8858 | | |
8859 | | int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size) |
8860 | 0 | { |
8861 | 0 | if (log_size && !log_buf) |
8862 | 0 | return -EINVAL; |
8863 | 0 | if (prog->log_size > UINT_MAX) |
8864 | 0 | return -EINVAL; |
8865 | 0 | if (prog->obj->loaded) |
8866 | 0 | return -EBUSY; |
8867 | | |
8868 | 0 | prog->log_buf = log_buf; |
8869 | 0 | prog->log_size = log_size; |
8870 | 0 | return 0; |
8871 | 0 | } |
8872 | | |
8873 | | #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \ |
8874 | | .sec = (char *)sec_pfx, \ |
8875 | | .prog_type = BPF_PROG_TYPE_##ptype, \ |
8876 | | .expected_attach_type = atype, \ |
8877 | | .cookie = (long)(flags), \ |
8878 | | .prog_prepare_load_fn = libbpf_prepare_prog_load, \ |
8879 | | __VA_ARGS__ \ |
8880 | | } |
8881 | | |
8882 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8883 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8884 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8885 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8886 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8887 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8888 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8889 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8890 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8891 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8892 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
8893 | | |
8894 | | static const struct bpf_sec_def section_defs[] = { |
8895 | | SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE), |
8896 | | SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE), |
8897 | | SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE), |
8898 | | SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
8899 | | SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
8900 | | SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
8901 | | SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
8902 | | SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
8903 | | SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
8904 | | SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
8905 | | SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
8906 | | SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
8907 | | SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
8908 | | SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
8909 | | SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
8910 | | SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
8911 | | SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
8912 | | SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt), |
8913 | | SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt), |
8914 | | SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */ |
8915 | | SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */ |
8916 | | SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), |
8917 | | SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), |
8918 | | SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
8919 | | SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
8920 | | SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
8921 | | SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE), |
8922 | | SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE), |
8923 | | SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
8924 | | SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
8925 | | SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
8926 | | SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
8927 | | SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
8928 | | SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
8929 | | SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace), |
8930 | | SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace), |
8931 | | SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace), |
8932 | | SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace), |
8933 | | SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
8934 | | SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
8935 | | SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
8936 | | SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace), |
8937 | | SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm), |
8938 | | SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm), |
8939 | | SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF), |
8940 | | SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter), |
8941 | | SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter), |
8942 | | SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE), |
8943 | | SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS), |
8944 | | SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE), |
8945 | | SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS), |
8946 | | SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE), |
8947 | | SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS), |
8948 | | SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT), |
8949 | | SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE), |
8950 | | SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE), |
8951 | | SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE), |
8952 | | SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE), |
8953 | | SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE), |
8954 | | SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT), |
8955 | | SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT), |
8956 | | SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT), |
8957 | | SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE), |
8958 | | SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT), |
8959 | | SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT), |
8960 | | SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT), |
8961 | | SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT), |
8962 | | SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT), |
8963 | | SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE), |
8964 | | SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE), |
8965 | | SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE), |
8966 | | SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT), |
8967 | | SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE), |
8968 | | SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE), |
8969 | | SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE), |
8970 | | SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE), |
8971 | | SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE), |
8972 | | SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE), |
8973 | | SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE), |
8974 | | SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE), |
8975 | | SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE), |
8976 | | SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE), |
8977 | | SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE), |
8978 | | SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE), |
8979 | | SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE), |
8980 | | SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE), |
8981 | | SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE), |
8982 | | SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE), |
8983 | | SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE), |
8984 | | SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE), |
8985 | | SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE), |
8986 | | SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE), |
8987 | | SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE), |
8988 | | SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE), |
8989 | | SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT), |
8990 | | SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), |
8991 | | SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), |
8992 | | SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), |
8993 | | SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE), |
8994 | | }; |
8995 | | |
8996 | | int libbpf_register_prog_handler(const char *sec, |
8997 | | enum bpf_prog_type prog_type, |
8998 | | enum bpf_attach_type exp_attach_type, |
8999 | | const struct libbpf_prog_handler_opts *opts) |
9000 | 0 | { |
9001 | 0 | struct bpf_sec_def *sec_def; |
9002 | |
|
9003 | 0 | if (!OPTS_VALID(opts, libbpf_prog_handler_opts)) |
9004 | 0 | return libbpf_err(-EINVAL); |
9005 | | |
9006 | 0 | if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */ |
9007 | 0 | return libbpf_err(-E2BIG); |
9008 | | |
9009 | 0 | if (sec) { |
9010 | 0 | sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1, |
9011 | 0 | sizeof(*sec_def)); |
9012 | 0 | if (!sec_def) |
9013 | 0 | return libbpf_err(-ENOMEM); |
9014 | | |
9015 | 0 | custom_sec_defs = sec_def; |
9016 | 0 | sec_def = &custom_sec_defs[custom_sec_def_cnt]; |
9017 | 0 | } else { |
9018 | 0 | if (has_custom_fallback_def) |
9019 | 0 | return libbpf_err(-EBUSY); |
9020 | | |
9021 | 0 | sec_def = &custom_fallback_def; |
9022 | 0 | } |
9023 | | |
9024 | 0 | sec_def->sec = sec ? strdup(sec) : NULL; |
9025 | 0 | if (sec && !sec_def->sec) |
9026 | 0 | return libbpf_err(-ENOMEM); |
9027 | | |
9028 | 0 | sec_def->prog_type = prog_type; |
9029 | 0 | sec_def->expected_attach_type = exp_attach_type; |
9030 | 0 | sec_def->cookie = OPTS_GET(opts, cookie, 0); |
9031 | |
|
9032 | 0 | sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); |
9033 | 0 | sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); |
9034 | 0 | sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); |
9035 | |
|
9036 | 0 | sec_def->handler_id = ++last_custom_sec_def_handler_id; |
9037 | |
|
9038 | 0 | if (sec) |
9039 | 0 | custom_sec_def_cnt++; |
9040 | 0 | else |
9041 | 0 | has_custom_fallback_def = true; |
9042 | |
|
9043 | 0 | return sec_def->handler_id; |
9044 | 0 | } |
9045 | | |
9046 | | int libbpf_unregister_prog_handler(int handler_id) |
9047 | 0 | { |
9048 | 0 | struct bpf_sec_def *sec_defs; |
9049 | 0 | int i; |
9050 | |
|
9051 | 0 | if (handler_id <= 0) |
9052 | 0 | return libbpf_err(-EINVAL); |
9053 | | |
9054 | 0 | if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) { |
9055 | 0 | memset(&custom_fallback_def, 0, sizeof(custom_fallback_def)); |
9056 | 0 | has_custom_fallback_def = false; |
9057 | 0 | return 0; |
9058 | 0 | } |
9059 | | |
9060 | 0 | for (i = 0; i < custom_sec_def_cnt; i++) { |
9061 | 0 | if (custom_sec_defs[i].handler_id == handler_id) |
9062 | 0 | break; |
9063 | 0 | } |
9064 | |
|
9065 | 0 | if (i == custom_sec_def_cnt) |
9066 | 0 | return libbpf_err(-ENOENT); |
9067 | | |
9068 | 0 | free(custom_sec_defs[i].sec); |
9069 | 0 | for (i = i + 1; i < custom_sec_def_cnt; i++) |
9070 | 0 | custom_sec_defs[i - 1] = custom_sec_defs[i]; |
9071 | 0 | custom_sec_def_cnt--; |
9072 | | |
9073 | | /* try to shrink the array, but it's ok if we couldn't */ |
9074 | 0 | sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs)); |
9075 | | /* if new count is zero, reallocarray can return a valid NULL result; |
9076 | | * in this case the previous pointer will be freed, so we *have to* |
9077 | | * reassign old pointer to the new value (even if it's NULL) |
9078 | | */ |
9079 | 0 | if (sec_defs || custom_sec_def_cnt == 0) |
9080 | 0 | custom_sec_defs = sec_defs; |
9081 | |
|
9082 | 0 | return 0; |
9083 | 0 | } |
9084 | | |
9085 | | static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name) |
9086 | 519k | { |
9087 | 519k | size_t len = strlen(sec_def->sec); |
9088 | | |
9089 | | /* "type/" always has to have proper SEC("type/extras") form */ |
9090 | 519k | if (sec_def->sec[len - 1] == '/') { |
9091 | 0 | if (str_has_pfx(sec_name, sec_def->sec)) |
9092 | 0 | return true; |
9093 | 0 | return false; |
9094 | 0 | } |
9095 | | |
9096 | | /* "type+" means it can be either exact SEC("type") or |
9097 | | * well-formed SEC("type/extras") with proper '/' separator |
9098 | | */ |
9099 | 519k | if (sec_def->sec[len - 1] == '+') { |
9100 | 201k | len--; |
9101 | | /* not even a prefix */ |
9102 | 201k | if (strncmp(sec_name, sec_def->sec, len) != 0) |
9103 | 197k | return false; |
9104 | | /* exact match or has '/' separator */ |
9105 | 4.21k | if (sec_name[len] == '\0' || sec_name[len] == '/') |
9106 | 700 | return true; |
9107 | 3.51k | return false; |
9108 | 4.21k | } |
9109 | | |
9110 | 317k | return strcmp(sec_name, sec_def->sec) == 0; |
9111 | 519k | } |
9112 | | |
9113 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name) |
9114 | 5.82k | { |
9115 | 5.82k | const struct bpf_sec_def *sec_def; |
9116 | 5.82k | int i, n; |
9117 | | |
9118 | 5.82k | n = custom_sec_def_cnt; |
9119 | 5.82k | for (i = 0; i < n; i++) { |
9120 | 0 | sec_def = &custom_sec_defs[i]; |
9121 | 0 | if (sec_def_matches(sec_def, sec_name)) |
9122 | 0 | return sec_def; |
9123 | 0 | } |
9124 | | |
9125 | 5.82k | n = ARRAY_SIZE(section_defs); |
9126 | 524k | for (i = 0; i < n; i++) { |
9127 | 519k | sec_def = §ion_defs[i]; |
9128 | 519k | if (sec_def_matches(sec_def, sec_name)) |
9129 | 879 | return sec_def; |
9130 | 519k | } |
9131 | | |
9132 | 4.94k | if (has_custom_fallback_def) |
9133 | 0 | return &custom_fallback_def; |
9134 | | |
9135 | 4.94k | return NULL; |
9136 | 4.94k | } |
9137 | | |
9138 | 0 | #define MAX_TYPE_NAME_SIZE 32 |
9139 | | |
9140 | | static char *libbpf_get_type_names(bool attach_type) |
9141 | 0 | { |
9142 | 0 | int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE; |
9143 | 0 | char *buf; |
9144 | |
|
9145 | 0 | buf = malloc(len); |
9146 | 0 | if (!buf) |
9147 | 0 | return NULL; |
9148 | | |
9149 | 0 | buf[0] = '\0'; |
9150 | | /* Forge string buf with all available names */ |
9151 | 0 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
9152 | 0 | const struct bpf_sec_def *sec_def = §ion_defs[i]; |
9153 | |
|
9154 | 0 | if (attach_type) { |
9155 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
9156 | 0 | continue; |
9157 | | |
9158 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
9159 | 0 | continue; |
9160 | 0 | } |
9161 | | |
9162 | 0 | if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) { |
9163 | 0 | free(buf); |
9164 | 0 | return NULL; |
9165 | 0 | } |
9166 | 0 | strcat(buf, " "); |
9167 | 0 | strcat(buf, section_defs[i].sec); |
9168 | 0 | } |
9169 | | |
9170 | 0 | return buf; |
9171 | 0 | } |
9172 | | |
9173 | | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
9174 | | enum bpf_attach_type *expected_attach_type) |
9175 | 0 | { |
9176 | 0 | const struct bpf_sec_def *sec_def; |
9177 | 0 | char *type_names; |
9178 | |
|
9179 | 0 | if (!name) |
9180 | 0 | return libbpf_err(-EINVAL); |
9181 | | |
9182 | 0 | sec_def = find_sec_def(name); |
9183 | 0 | if (sec_def) { |
9184 | 0 | *prog_type = sec_def->prog_type; |
9185 | 0 | *expected_attach_type = sec_def->expected_attach_type; |
9186 | 0 | return 0; |
9187 | 0 | } |
9188 | | |
9189 | 0 | pr_debug("failed to guess program type from ELF section '%s'\n", name); |
9190 | 0 | type_names = libbpf_get_type_names(false); |
9191 | 0 | if (type_names != NULL) { |
9192 | 0 | pr_debug("supported section(type) names are:%s\n", type_names); |
9193 | 0 | free(type_names); |
9194 | 0 | } |
9195 | |
|
9196 | 0 | return libbpf_err(-ESRCH); |
9197 | 0 | } |
9198 | | |
9199 | | const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t) |
9200 | 0 | { |
9201 | 0 | if (t < 0 || t >= ARRAY_SIZE(attach_type_name)) |
9202 | 0 | return NULL; |
9203 | | |
9204 | 0 | return attach_type_name[t]; |
9205 | 0 | } |
9206 | | |
9207 | | const char *libbpf_bpf_link_type_str(enum bpf_link_type t) |
9208 | 0 | { |
9209 | 0 | if (t < 0 || t >= ARRAY_SIZE(link_type_name)) |
9210 | 0 | return NULL; |
9211 | | |
9212 | 0 | return link_type_name[t]; |
9213 | 0 | } |
9214 | | |
9215 | | const char *libbpf_bpf_map_type_str(enum bpf_map_type t) |
9216 | 0 | { |
9217 | 0 | if (t < 0 || t >= ARRAY_SIZE(map_type_name)) |
9218 | 0 | return NULL; |
9219 | | |
9220 | 0 | return map_type_name[t]; |
9221 | 0 | } |
9222 | | |
9223 | | const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t) |
9224 | 0 | { |
9225 | 0 | if (t < 0 || t >= ARRAY_SIZE(prog_type_name)) |
9226 | 0 | return NULL; |
9227 | | |
9228 | 0 | return prog_type_name[t]; |
9229 | 0 | } |
9230 | | |
9231 | | static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj, |
9232 | | int sec_idx, |
9233 | | size_t offset) |
9234 | 0 | { |
9235 | 0 | struct bpf_map *map; |
9236 | 0 | size_t i; |
9237 | |
|
9238 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
9239 | 0 | map = &obj->maps[i]; |
9240 | 0 | if (!bpf_map__is_struct_ops(map)) |
9241 | 0 | continue; |
9242 | 0 | if (map->sec_idx == sec_idx && |
9243 | 0 | map->sec_offset <= offset && |
9244 | 0 | offset - map->sec_offset < map->def.value_size) |
9245 | 0 | return map; |
9246 | 0 | } |
9247 | | |
9248 | 0 | return NULL; |
9249 | 0 | } |
9250 | | |
9251 | | /* Collect the reloc from ELF and populate the st_ops->progs[] */ |
9252 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
9253 | | Elf64_Shdr *shdr, Elf_Data *data) |
9254 | 0 | { |
9255 | 0 | const struct btf_member *member; |
9256 | 0 | struct bpf_struct_ops *st_ops; |
9257 | 0 | struct bpf_program *prog; |
9258 | 0 | unsigned int shdr_idx; |
9259 | 0 | const struct btf *btf; |
9260 | 0 | struct bpf_map *map; |
9261 | 0 | unsigned int moff, insn_idx; |
9262 | 0 | const char *name; |
9263 | 0 | __u32 member_idx; |
9264 | 0 | Elf64_Sym *sym; |
9265 | 0 | Elf64_Rel *rel; |
9266 | 0 | int i, nrels; |
9267 | |
|
9268 | 0 | btf = obj->btf; |
9269 | 0 | nrels = shdr->sh_size / shdr->sh_entsize; |
9270 | 0 | for (i = 0; i < nrels; i++) { |
9271 | 0 | rel = elf_rel_by_idx(data, i); |
9272 | 0 | if (!rel) { |
9273 | 0 | pr_warn("struct_ops reloc: failed to get %d reloc\n", i); |
9274 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9275 | 0 | } |
9276 | | |
9277 | 0 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
9278 | 0 | if (!sym) { |
9279 | 0 | pr_warn("struct_ops reloc: symbol %zx not found\n", |
9280 | 0 | (size_t)ELF64_R_SYM(rel->r_info)); |
9281 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9282 | 0 | } |
9283 | | |
9284 | 0 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
9285 | 0 | map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); |
9286 | 0 | if (!map) { |
9287 | 0 | pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", |
9288 | 0 | (size_t)rel->r_offset); |
9289 | 0 | return -EINVAL; |
9290 | 0 | } |
9291 | | |
9292 | 0 | moff = rel->r_offset - map->sec_offset; |
9293 | 0 | shdr_idx = sym->st_shndx; |
9294 | 0 | st_ops = map->st_ops; |
9295 | 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", |
9296 | 0 | map->name, |
9297 | 0 | (long long)(rel->r_info >> 32), |
9298 | 0 | (long long)sym->st_value, |
9299 | 0 | shdr_idx, (size_t)rel->r_offset, |
9300 | 0 | map->sec_offset, sym->st_name, name); |
9301 | |
|
9302 | 0 | if (shdr_idx >= SHN_LORESERVE) { |
9303 | 0 | pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n", |
9304 | 0 | map->name, (size_t)rel->r_offset, shdr_idx); |
9305 | 0 | return -LIBBPF_ERRNO__RELOC; |
9306 | 0 | } |
9307 | 0 | if (sym->st_value % BPF_INSN_SZ) { |
9308 | 0 | pr_warn("struct_ops reloc %s: invalid target program offset %llu\n", |
9309 | 0 | map->name, (unsigned long long)sym->st_value); |
9310 | 0 | return -LIBBPF_ERRNO__FORMAT; |
9311 | 0 | } |
9312 | 0 | insn_idx = sym->st_value / BPF_INSN_SZ; |
9313 | |
|
9314 | 0 | member = find_member_by_offset(st_ops->type, moff * 8); |
9315 | 0 | if (!member) { |
9316 | 0 | pr_warn("struct_ops reloc %s: cannot find member at moff %u\n", |
9317 | 0 | map->name, moff); |
9318 | 0 | return -EINVAL; |
9319 | 0 | } |
9320 | 0 | member_idx = member - btf_members(st_ops->type); |
9321 | 0 | name = btf__name_by_offset(btf, member->name_off); |
9322 | |
|
9323 | 0 | if (!resolve_func_ptr(btf, member->type, NULL)) { |
9324 | 0 | pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n", |
9325 | 0 | map->name, name); |
9326 | 0 | return -EINVAL; |
9327 | 0 | } |
9328 | | |
9329 | 0 | prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx); |
9330 | 0 | if (!prog) { |
9331 | 0 | pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n", |
9332 | 0 | map->name, shdr_idx, name); |
9333 | 0 | return -EINVAL; |
9334 | 0 | } |
9335 | | |
9336 | | /* prevent the use of BPF prog with invalid type */ |
9337 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { |
9338 | 0 | pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n", |
9339 | 0 | map->name, prog->name); |
9340 | 0 | return -EINVAL; |
9341 | 0 | } |
9342 | | |
9343 | | /* if we haven't yet processed this BPF program, record proper |
9344 | | * attach_btf_id and member_idx |
9345 | | */ |
9346 | 0 | if (!prog->attach_btf_id) { |
9347 | 0 | prog->attach_btf_id = st_ops->type_id; |
9348 | 0 | prog->expected_attach_type = member_idx; |
9349 | 0 | } |
9350 | | |
9351 | | /* struct_ops BPF prog can be re-used between multiple |
9352 | | * .struct_ops & .struct_ops.link as long as it's the |
9353 | | * same struct_ops struct definition and the same |
9354 | | * function pointer field |
9355 | | */ |
9356 | 0 | if (prog->attach_btf_id != st_ops->type_id || |
9357 | 0 | prog->expected_attach_type != member_idx) { |
9358 | 0 | pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n", |
9359 | 0 | map->name, prog->name, prog->sec_name, prog->type, |
9360 | 0 | prog->attach_btf_id, prog->expected_attach_type, name); |
9361 | 0 | return -EINVAL; |
9362 | 0 | } |
9363 | | |
9364 | 0 | st_ops->progs[member_idx] = prog; |
9365 | 0 | } |
9366 | | |
9367 | 0 | return 0; |
9368 | 0 | } |
9369 | | |
9370 | 0 | #define BTF_TRACE_PREFIX "btf_trace_" |
9371 | 0 | #define BTF_LSM_PREFIX "bpf_lsm_" |
9372 | 0 | #define BTF_ITER_PREFIX "bpf_iter_" |
9373 | | #define BTF_MAX_NAME_SIZE 128 |
9374 | | |
9375 | | void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, |
9376 | | const char **prefix, int *kind) |
9377 | 0 | { |
9378 | 0 | switch (attach_type) { |
9379 | 0 | case BPF_TRACE_RAW_TP: |
9380 | 0 | *prefix = BTF_TRACE_PREFIX; |
9381 | 0 | *kind = BTF_KIND_TYPEDEF; |
9382 | 0 | break; |
9383 | 0 | case BPF_LSM_MAC: |
9384 | 0 | case BPF_LSM_CGROUP: |
9385 | 0 | *prefix = BTF_LSM_PREFIX; |
9386 | 0 | *kind = BTF_KIND_FUNC; |
9387 | 0 | break; |
9388 | 0 | case BPF_TRACE_ITER: |
9389 | 0 | *prefix = BTF_ITER_PREFIX; |
9390 | 0 | *kind = BTF_KIND_FUNC; |
9391 | 0 | break; |
9392 | 0 | default: |
9393 | 0 | *prefix = ""; |
9394 | 0 | *kind = BTF_KIND_FUNC; |
9395 | 0 | } |
9396 | 0 | } |
9397 | | |
9398 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
9399 | | const char *name, __u32 kind) |
9400 | 0 | { |
9401 | 0 | char btf_type_name[BTF_MAX_NAME_SIZE]; |
9402 | 0 | int ret; |
9403 | |
|
9404 | 0 | ret = snprintf(btf_type_name, sizeof(btf_type_name), |
9405 | 0 | "%s%s", prefix, name); |
9406 | | /* snprintf returns the number of characters written excluding the |
9407 | | * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it |
9408 | | * indicates truncation. |
9409 | | */ |
9410 | 0 | if (ret < 0 || ret >= sizeof(btf_type_name)) |
9411 | 0 | return -ENAMETOOLONG; |
9412 | 0 | return btf__find_by_name_kind(btf, btf_type_name, kind); |
9413 | 0 | } |
9414 | | |
9415 | | static inline int find_attach_btf_id(struct btf *btf, const char *name, |
9416 | | enum bpf_attach_type attach_type) |
9417 | 0 | { |
9418 | 0 | const char *prefix; |
9419 | 0 | int kind; |
9420 | |
|
9421 | 0 | btf_get_kernel_prefix_kind(attach_type, &prefix, &kind); |
9422 | 0 | return find_btf_by_prefix_kind(btf, prefix, name, kind); |
9423 | 0 | } |
9424 | | |
9425 | | int libbpf_find_vmlinux_btf_id(const char *name, |
9426 | | enum bpf_attach_type attach_type) |
9427 | 0 | { |
9428 | 0 | struct btf *btf; |
9429 | 0 | int err; |
9430 | |
|
9431 | 0 | btf = btf__load_vmlinux_btf(); |
9432 | 0 | err = libbpf_get_error(btf); |
9433 | 0 | if (err) { |
9434 | 0 | pr_warn("vmlinux BTF is not found\n"); |
9435 | 0 | return libbpf_err(err); |
9436 | 0 | } |
9437 | | |
9438 | 0 | err = find_attach_btf_id(btf, name, attach_type); |
9439 | 0 | if (err <= 0) |
9440 | 0 | pr_warn("%s is not found in vmlinux BTF\n", name); |
9441 | |
|
9442 | 0 | btf__free(btf); |
9443 | 0 | return libbpf_err(err); |
9444 | 0 | } |
9445 | | |
9446 | | static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) |
9447 | 0 | { |
9448 | 0 | struct bpf_prog_info info; |
9449 | 0 | __u32 info_len = sizeof(info); |
9450 | 0 | struct btf *btf; |
9451 | 0 | int err; |
9452 | |
|
9453 | 0 | memset(&info, 0, info_len); |
9454 | 0 | err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len); |
9455 | 0 | if (err) { |
9456 | 0 | pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %d\n", |
9457 | 0 | attach_prog_fd, err); |
9458 | 0 | return err; |
9459 | 0 | } |
9460 | | |
9461 | 0 | err = -EINVAL; |
9462 | 0 | if (!info.btf_id) { |
9463 | 0 | pr_warn("The target program doesn't have BTF\n"); |
9464 | 0 | goto out; |
9465 | 0 | } |
9466 | 0 | btf = btf__load_from_kernel_by_id(info.btf_id); |
9467 | 0 | err = libbpf_get_error(btf); |
9468 | 0 | if (err) { |
9469 | 0 | pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err); |
9470 | 0 | goto out; |
9471 | 0 | } |
9472 | 0 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
9473 | 0 | btf__free(btf); |
9474 | 0 | if (err <= 0) { |
9475 | 0 | pr_warn("%s is not found in prog's BTF\n", name); |
9476 | 0 | goto out; |
9477 | 0 | } |
9478 | 0 | out: |
9479 | 0 | return err; |
9480 | 0 | } |
9481 | | |
9482 | | static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name, |
9483 | | enum bpf_attach_type attach_type, |
9484 | | int *btf_obj_fd, int *btf_type_id) |
9485 | 0 | { |
9486 | 0 | int ret, i; |
9487 | |
|
9488 | 0 | ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type); |
9489 | 0 | if (ret > 0) { |
9490 | 0 | *btf_obj_fd = 0; /* vmlinux BTF */ |
9491 | 0 | *btf_type_id = ret; |
9492 | 0 | return 0; |
9493 | 0 | } |
9494 | 0 | if (ret != -ENOENT) |
9495 | 0 | return ret; |
9496 | | |
9497 | 0 | ret = load_module_btfs(obj); |
9498 | 0 | if (ret) |
9499 | 0 | return ret; |
9500 | | |
9501 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
9502 | 0 | const struct module_btf *mod = &obj->btf_modules[i]; |
9503 | |
|
9504 | 0 | ret = find_attach_btf_id(mod->btf, attach_name, attach_type); |
9505 | 0 | if (ret > 0) { |
9506 | 0 | *btf_obj_fd = mod->fd; |
9507 | 0 | *btf_type_id = ret; |
9508 | 0 | return 0; |
9509 | 0 | } |
9510 | 0 | if (ret == -ENOENT) |
9511 | 0 | continue; |
9512 | | |
9513 | 0 | return ret; |
9514 | 0 | } |
9515 | | |
9516 | 0 | return -ESRCH; |
9517 | 0 | } |
9518 | | |
9519 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
9520 | | int *btf_obj_fd, int *btf_type_id) |
9521 | 0 | { |
9522 | 0 | enum bpf_attach_type attach_type = prog->expected_attach_type; |
9523 | 0 | __u32 attach_prog_fd = prog->attach_prog_fd; |
9524 | 0 | int err = 0; |
9525 | | |
9526 | | /* BPF program's BTF ID */ |
9527 | 0 | if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { |
9528 | 0 | if (!attach_prog_fd) { |
9529 | 0 | pr_warn("prog '%s': attach program FD is not set\n", prog->name); |
9530 | 0 | return -EINVAL; |
9531 | 0 | } |
9532 | 0 | err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd); |
9533 | 0 | if (err < 0) { |
9534 | 0 | pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n", |
9535 | 0 | prog->name, attach_prog_fd, attach_name, err); |
9536 | 0 | return err; |
9537 | 0 | } |
9538 | 0 | *btf_obj_fd = 0; |
9539 | 0 | *btf_type_id = err; |
9540 | 0 | return 0; |
9541 | 0 | } |
9542 | | |
9543 | | /* kernel/module BTF ID */ |
9544 | 0 | if (prog->obj->gen_loader) { |
9545 | 0 | bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); |
9546 | 0 | *btf_obj_fd = 0; |
9547 | 0 | *btf_type_id = 1; |
9548 | 0 | } else { |
9549 | 0 | err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id); |
9550 | 0 | } |
9551 | 0 | if (err) { |
9552 | 0 | pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n", |
9553 | 0 | prog->name, attach_name, err); |
9554 | 0 | return err; |
9555 | 0 | } |
9556 | 0 | return 0; |
9557 | 0 | } |
9558 | | |
9559 | | int libbpf_attach_type_by_name(const char *name, |
9560 | | enum bpf_attach_type *attach_type) |
9561 | 0 | { |
9562 | 0 | char *type_names; |
9563 | 0 | const struct bpf_sec_def *sec_def; |
9564 | |
|
9565 | 0 | if (!name) |
9566 | 0 | return libbpf_err(-EINVAL); |
9567 | | |
9568 | 0 | sec_def = find_sec_def(name); |
9569 | 0 | if (!sec_def) { |
9570 | 0 | pr_debug("failed to guess attach type based on ELF section name '%s'\n", name); |
9571 | 0 | type_names = libbpf_get_type_names(true); |
9572 | 0 | if (type_names != NULL) { |
9573 | 0 | pr_debug("attachable section(type) names are:%s\n", type_names); |
9574 | 0 | free(type_names); |
9575 | 0 | } |
9576 | |
|
9577 | 0 | return libbpf_err(-EINVAL); |
9578 | 0 | } |
9579 | | |
9580 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
9581 | 0 | return libbpf_err(-EINVAL); |
9582 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
9583 | 0 | return libbpf_err(-EINVAL); |
9584 | | |
9585 | 0 | *attach_type = sec_def->expected_attach_type; |
9586 | 0 | return 0; |
9587 | 0 | } |
9588 | | |
9589 | | int bpf_map__fd(const struct bpf_map *map) |
9590 | 0 | { |
9591 | 0 | return map ? map->fd : libbpf_err(-EINVAL); |
9592 | 0 | } |
9593 | | |
9594 | | static bool map_uses_real_name(const struct bpf_map *map) |
9595 | 0 | { |
9596 | | /* Since libbpf started to support custom .data.* and .rodata.* maps, |
9597 | | * their user-visible name differs from kernel-visible name. Users see |
9598 | | * such map's corresponding ELF section name as a map name. |
9599 | | * This check distinguishes .data/.rodata from .data.* and .rodata.* |
9600 | | * maps to know which name has to be returned to the user. |
9601 | | */ |
9602 | 0 | if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) |
9603 | 0 | return true; |
9604 | 0 | if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) |
9605 | 0 | return true; |
9606 | 0 | return false; |
9607 | 0 | } |
9608 | | |
9609 | | const char *bpf_map__name(const struct bpf_map *map) |
9610 | 0 | { |
9611 | 0 | if (!map) |
9612 | 0 | return NULL; |
9613 | | |
9614 | 0 | if (map_uses_real_name(map)) |
9615 | 0 | return map->real_name; |
9616 | | |
9617 | 0 | return map->name; |
9618 | 0 | } |
9619 | | |
9620 | | enum bpf_map_type bpf_map__type(const struct bpf_map *map) |
9621 | 0 | { |
9622 | 0 | return map->def.type; |
9623 | 0 | } |
9624 | | |
9625 | | int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type) |
9626 | 0 | { |
9627 | 0 | if (map->fd >= 0) |
9628 | 0 | return libbpf_err(-EBUSY); |
9629 | 0 | map->def.type = type; |
9630 | 0 | return 0; |
9631 | 0 | } |
9632 | | |
9633 | | __u32 bpf_map__map_flags(const struct bpf_map *map) |
9634 | 0 | { |
9635 | 0 | return map->def.map_flags; |
9636 | 0 | } |
9637 | | |
9638 | | int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags) |
9639 | 0 | { |
9640 | 0 | if (map->fd >= 0) |
9641 | 0 | return libbpf_err(-EBUSY); |
9642 | 0 | map->def.map_flags = flags; |
9643 | 0 | return 0; |
9644 | 0 | } |
9645 | | |
9646 | | __u64 bpf_map__map_extra(const struct bpf_map *map) |
9647 | 0 | { |
9648 | 0 | return map->map_extra; |
9649 | 0 | } |
9650 | | |
9651 | | int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra) |
9652 | 0 | { |
9653 | 0 | if (map->fd >= 0) |
9654 | 0 | return libbpf_err(-EBUSY); |
9655 | 0 | map->map_extra = map_extra; |
9656 | 0 | return 0; |
9657 | 0 | } |
9658 | | |
9659 | | __u32 bpf_map__numa_node(const struct bpf_map *map) |
9660 | 0 | { |
9661 | 0 | return map->numa_node; |
9662 | 0 | } |
9663 | | |
9664 | | int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node) |
9665 | 0 | { |
9666 | 0 | if (map->fd >= 0) |
9667 | 0 | return libbpf_err(-EBUSY); |
9668 | 0 | map->numa_node = numa_node; |
9669 | 0 | return 0; |
9670 | 0 | } |
9671 | | |
9672 | | __u32 bpf_map__key_size(const struct bpf_map *map) |
9673 | 0 | { |
9674 | 0 | return map->def.key_size; |
9675 | 0 | } |
9676 | | |
9677 | | int bpf_map__set_key_size(struct bpf_map *map, __u32 size) |
9678 | 0 | { |
9679 | 0 | if (map->fd >= 0) |
9680 | 0 | return libbpf_err(-EBUSY); |
9681 | 0 | map->def.key_size = size; |
9682 | 0 | return 0; |
9683 | 0 | } |
9684 | | |
9685 | | __u32 bpf_map__value_size(const struct bpf_map *map) |
9686 | 0 | { |
9687 | 0 | return map->def.value_size; |
9688 | 0 | } |
9689 | | |
9690 | | static int map_btf_datasec_resize(struct bpf_map *map, __u32 size) |
9691 | 0 | { |
9692 | 0 | struct btf *btf; |
9693 | 0 | struct btf_type *datasec_type, *var_type; |
9694 | 0 | struct btf_var_secinfo *var; |
9695 | 0 | const struct btf_type *array_type; |
9696 | 0 | const struct btf_array *array; |
9697 | 0 | int vlen, element_sz, new_array_id; |
9698 | 0 | __u32 nr_elements; |
9699 | | |
9700 | | /* check btf existence */ |
9701 | 0 | btf = bpf_object__btf(map->obj); |
9702 | 0 | if (!btf) |
9703 | 0 | return -ENOENT; |
9704 | | |
9705 | | /* verify map is datasec */ |
9706 | 0 | datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map)); |
9707 | 0 | if (!btf_is_datasec(datasec_type)) { |
9708 | 0 | pr_warn("map '%s': cannot be resized, map value type is not a datasec\n", |
9709 | 0 | bpf_map__name(map)); |
9710 | 0 | return -EINVAL; |
9711 | 0 | } |
9712 | | |
9713 | | /* verify datasec has at least one var */ |
9714 | 0 | vlen = btf_vlen(datasec_type); |
9715 | 0 | if (vlen == 0) { |
9716 | 0 | pr_warn("map '%s': cannot be resized, map value datasec is empty\n", |
9717 | 0 | bpf_map__name(map)); |
9718 | 0 | return -EINVAL; |
9719 | 0 | } |
9720 | | |
9721 | | /* verify last var in the datasec is an array */ |
9722 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
9723 | 0 | var_type = btf_type_by_id(btf, var->type); |
9724 | 0 | array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); |
9725 | 0 | if (!btf_is_array(array_type)) { |
9726 | 0 | pr_warn("map '%s': cannot be resized, last var must be an array\n", |
9727 | 0 | bpf_map__name(map)); |
9728 | 0 | return -EINVAL; |
9729 | 0 | } |
9730 | | |
9731 | | /* verify request size aligns with array */ |
9732 | 0 | array = btf_array(array_type); |
9733 | 0 | element_sz = btf__resolve_size(btf, array->type); |
9734 | 0 | if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { |
9735 | 0 | pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n", |
9736 | 0 | bpf_map__name(map), element_sz, size); |
9737 | 0 | return -EINVAL; |
9738 | 0 | } |
9739 | | |
9740 | | /* create a new array based on the existing array, but with new length */ |
9741 | 0 | nr_elements = (size - var->offset) / element_sz; |
9742 | 0 | new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); |
9743 | 0 | if (new_array_id < 0) |
9744 | 0 | return new_array_id; |
9745 | | |
9746 | | /* adding a new btf type invalidates existing pointers to btf objects, |
9747 | | * so refresh pointers before proceeding |
9748 | | */ |
9749 | 0 | datasec_type = btf_type_by_id(btf, map->btf_value_type_id); |
9750 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
9751 | 0 | var_type = btf_type_by_id(btf, var->type); |
9752 | | |
9753 | | /* finally update btf info */ |
9754 | 0 | datasec_type->size = size; |
9755 | 0 | var->size = size - var->offset; |
9756 | 0 | var_type->type = new_array_id; |
9757 | |
|
9758 | 0 | return 0; |
9759 | 0 | } |
9760 | | |
9761 | | int bpf_map__set_value_size(struct bpf_map *map, __u32 size) |
9762 | 0 | { |
9763 | 0 | if (map->fd >= 0) |
9764 | 0 | return libbpf_err(-EBUSY); |
9765 | | |
9766 | 0 | if (map->mmaped) { |
9767 | 0 | int err; |
9768 | 0 | size_t mmap_old_sz, mmap_new_sz; |
9769 | |
|
9770 | 0 | mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); |
9771 | 0 | mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries); |
9772 | 0 | err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz); |
9773 | 0 | if (err) { |
9774 | 0 | pr_warn("map '%s': failed to resize memory-mapped region: %d\n", |
9775 | 0 | bpf_map__name(map), err); |
9776 | 0 | return err; |
9777 | 0 | } |
9778 | 0 | err = map_btf_datasec_resize(map, size); |
9779 | 0 | if (err && err != -ENOENT) { |
9780 | 0 | pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n", |
9781 | 0 | bpf_map__name(map), err); |
9782 | 0 | map->btf_value_type_id = 0; |
9783 | 0 | map->btf_key_type_id = 0; |
9784 | 0 | } |
9785 | 0 | } |
9786 | | |
9787 | 0 | map->def.value_size = size; |
9788 | 0 | return 0; |
9789 | 0 | } |
9790 | | |
9791 | | __u32 bpf_map__btf_key_type_id(const struct bpf_map *map) |
9792 | 0 | { |
9793 | 0 | return map ? map->btf_key_type_id : 0; |
9794 | 0 | } |
9795 | | |
9796 | | __u32 bpf_map__btf_value_type_id(const struct bpf_map *map) |
9797 | 0 | { |
9798 | 0 | return map ? map->btf_value_type_id : 0; |
9799 | 0 | } |
9800 | | |
9801 | | int bpf_map__set_initial_value(struct bpf_map *map, |
9802 | | const void *data, size_t size) |
9803 | 0 | { |
9804 | 0 | if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG || |
9805 | 0 | size != map->def.value_size || map->fd >= 0) |
9806 | 0 | return libbpf_err(-EINVAL); |
9807 | | |
9808 | 0 | memcpy(map->mmaped, data, size); |
9809 | 0 | return 0; |
9810 | 0 | } |
9811 | | |
9812 | | void *bpf_map__initial_value(struct bpf_map *map, size_t *psize) |
9813 | 0 | { |
9814 | 0 | if (!map->mmaped) |
9815 | 0 | return NULL; |
9816 | 0 | *psize = map->def.value_size; |
9817 | 0 | return map->mmaped; |
9818 | 0 | } |
9819 | | |
9820 | | bool bpf_map__is_internal(const struct bpf_map *map) |
9821 | 377 | { |
9822 | 377 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
9823 | 377 | } |
9824 | | |
9825 | | __u32 bpf_map__ifindex(const struct bpf_map *map) |
9826 | 0 | { |
9827 | 0 | return map->map_ifindex; |
9828 | 0 | } |
9829 | | |
9830 | | int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
9831 | 0 | { |
9832 | 0 | if (map->fd >= 0) |
9833 | 0 | return libbpf_err(-EBUSY); |
9834 | 0 | map->map_ifindex = ifindex; |
9835 | 0 | return 0; |
9836 | 0 | } |
9837 | | |
9838 | | int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) |
9839 | 0 | { |
9840 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) { |
9841 | 0 | pr_warn("error: unsupported map type\n"); |
9842 | 0 | return libbpf_err(-EINVAL); |
9843 | 0 | } |
9844 | 0 | if (map->inner_map_fd != -1) { |
9845 | 0 | pr_warn("error: inner_map_fd already specified\n"); |
9846 | 0 | return libbpf_err(-EINVAL); |
9847 | 0 | } |
9848 | 0 | if (map->inner_map) { |
9849 | 0 | bpf_map__destroy(map->inner_map); |
9850 | 0 | zfree(&map->inner_map); |
9851 | 0 | } |
9852 | 0 | map->inner_map_fd = fd; |
9853 | 0 | return 0; |
9854 | 0 | } |
9855 | | |
9856 | | static struct bpf_map * |
9857 | | __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) |
9858 | 0 | { |
9859 | 0 | ssize_t idx; |
9860 | 0 | struct bpf_map *s, *e; |
9861 | |
|
9862 | 0 | if (!obj || !obj->maps) |
9863 | 0 | return errno = EINVAL, NULL; |
9864 | | |
9865 | 0 | s = obj->maps; |
9866 | 0 | e = obj->maps + obj->nr_maps; |
9867 | |
|
9868 | 0 | if ((m < s) || (m >= e)) { |
9869 | 0 | pr_warn("error in %s: map handler doesn't belong to object\n", |
9870 | 0 | __func__); |
9871 | 0 | return errno = EINVAL, NULL; |
9872 | 0 | } |
9873 | | |
9874 | 0 | idx = (m - obj->maps) + i; |
9875 | 0 | if (idx >= obj->nr_maps || idx < 0) |
9876 | 0 | return NULL; |
9877 | 0 | return &obj->maps[idx]; |
9878 | 0 | } |
9879 | | |
9880 | | struct bpf_map * |
9881 | | bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev) |
9882 | 0 | { |
9883 | 0 | if (prev == NULL) |
9884 | 0 | return obj->maps; |
9885 | | |
9886 | 0 | return __bpf_map__iter(prev, obj, 1); |
9887 | 0 | } |
9888 | | |
9889 | | struct bpf_map * |
9890 | | bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next) |
9891 | 0 | { |
9892 | 0 | if (next == NULL) { |
9893 | 0 | if (!obj->nr_maps) |
9894 | 0 | return NULL; |
9895 | 0 | return obj->maps + obj->nr_maps - 1; |
9896 | 0 | } |
9897 | | |
9898 | 0 | return __bpf_map__iter(next, obj, -1); |
9899 | 0 | } |
9900 | | |
9901 | | struct bpf_map * |
9902 | | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name) |
9903 | 0 | { |
9904 | 0 | struct bpf_map *pos; |
9905 | |
|
9906 | 0 | bpf_object__for_each_map(pos, obj) { |
9907 | | /* if it's a special internal map name (which always starts |
9908 | | * with dot) then check if that special name matches the |
9909 | | * real map name (ELF section name) |
9910 | | */ |
9911 | 0 | if (name[0] == '.') { |
9912 | 0 | if (pos->real_name && strcmp(pos->real_name, name) == 0) |
9913 | 0 | return pos; |
9914 | 0 | continue; |
9915 | 0 | } |
9916 | | /* otherwise map name has to be an exact match */ |
9917 | 0 | if (map_uses_real_name(pos)) { |
9918 | 0 | if (strcmp(pos->real_name, name) == 0) |
9919 | 0 | return pos; |
9920 | 0 | continue; |
9921 | 0 | } |
9922 | 0 | if (strcmp(pos->name, name) == 0) |
9923 | 0 | return pos; |
9924 | 0 | } |
9925 | 0 | return errno = ENOENT, NULL; |
9926 | 0 | } |
9927 | | |
9928 | | int |
9929 | | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) |
9930 | 0 | { |
9931 | 0 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
9932 | 0 | } |
9933 | | |
9934 | | static int validate_map_op(const struct bpf_map *map, size_t key_sz, |
9935 | | size_t value_sz, bool check_value_sz) |
9936 | 0 | { |
9937 | 0 | if (map->fd <= 0) |
9938 | 0 | return -ENOENT; |
9939 | | |
9940 | 0 | if (map->def.key_size != key_sz) { |
9941 | 0 | pr_warn("map '%s': unexpected key size %zu provided, expected %u\n", |
9942 | 0 | map->name, key_sz, map->def.key_size); |
9943 | 0 | return -EINVAL; |
9944 | 0 | } |
9945 | | |
9946 | 0 | if (!check_value_sz) |
9947 | 0 | return 0; |
9948 | | |
9949 | 0 | switch (map->def.type) { |
9950 | 0 | case BPF_MAP_TYPE_PERCPU_ARRAY: |
9951 | 0 | case BPF_MAP_TYPE_PERCPU_HASH: |
9952 | 0 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
9953 | 0 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: { |
9954 | 0 | int num_cpu = libbpf_num_possible_cpus(); |
9955 | 0 | size_t elem_sz = roundup(map->def.value_size, 8); |
9956 | |
|
9957 | 0 | if (value_sz != num_cpu * elem_sz) { |
9958 | 0 | pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n", |
9959 | 0 | map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); |
9960 | 0 | return -EINVAL; |
9961 | 0 | } |
9962 | 0 | break; |
9963 | 0 | } |
9964 | 0 | default: |
9965 | 0 | if (map->def.value_size != value_sz) { |
9966 | 0 | pr_warn("map '%s': unexpected value size %zu provided, expected %u\n", |
9967 | 0 | map->name, value_sz, map->def.value_size); |
9968 | 0 | return -EINVAL; |
9969 | 0 | } |
9970 | 0 | break; |
9971 | 0 | } |
9972 | 0 | return 0; |
9973 | 0 | } |
9974 | | |
9975 | | int bpf_map__lookup_elem(const struct bpf_map *map, |
9976 | | const void *key, size_t key_sz, |
9977 | | void *value, size_t value_sz, __u64 flags) |
9978 | 0 | { |
9979 | 0 | int err; |
9980 | |
|
9981 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
9982 | 0 | if (err) |
9983 | 0 | return libbpf_err(err); |
9984 | | |
9985 | 0 | return bpf_map_lookup_elem_flags(map->fd, key, value, flags); |
9986 | 0 | } |
9987 | | |
9988 | | int bpf_map__update_elem(const struct bpf_map *map, |
9989 | | const void *key, size_t key_sz, |
9990 | | const void *value, size_t value_sz, __u64 flags) |
9991 | 0 | { |
9992 | 0 | int err; |
9993 | |
|
9994 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
9995 | 0 | if (err) |
9996 | 0 | return libbpf_err(err); |
9997 | | |
9998 | 0 | return bpf_map_update_elem(map->fd, key, value, flags); |
9999 | 0 | } |
10000 | | |
10001 | | int bpf_map__delete_elem(const struct bpf_map *map, |
10002 | | const void *key, size_t key_sz, __u64 flags) |
10003 | 0 | { |
10004 | 0 | int err; |
10005 | |
|
10006 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */); |
10007 | 0 | if (err) |
10008 | 0 | return libbpf_err(err); |
10009 | | |
10010 | 0 | return bpf_map_delete_elem_flags(map->fd, key, flags); |
10011 | 0 | } |
10012 | | |
10013 | | int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, |
10014 | | const void *key, size_t key_sz, |
10015 | | void *value, size_t value_sz, __u64 flags) |
10016 | 0 | { |
10017 | 0 | int err; |
10018 | |
|
10019 | 0 | err = validate_map_op(map, key_sz, value_sz, true); |
10020 | 0 | if (err) |
10021 | 0 | return libbpf_err(err); |
10022 | | |
10023 | 0 | return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); |
10024 | 0 | } |
10025 | | |
10026 | | int bpf_map__get_next_key(const struct bpf_map *map, |
10027 | | const void *cur_key, void *next_key, size_t key_sz) |
10028 | 0 | { |
10029 | 0 | int err; |
10030 | |
|
10031 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */); |
10032 | 0 | if (err) |
10033 | 0 | return libbpf_err(err); |
10034 | | |
10035 | 0 | return bpf_map_get_next_key(map->fd, cur_key, next_key); |
10036 | 0 | } |
10037 | | |
10038 | | long libbpf_get_error(const void *ptr) |
10039 | 14.8k | { |
10040 | 14.8k | if (!IS_ERR_OR_NULL(ptr)) |
10041 | 4.78k | return 0; |
10042 | | |
10043 | 10.0k | if (IS_ERR(ptr)) |
10044 | 0 | errno = -PTR_ERR(ptr); |
10045 | | |
10046 | | /* If ptr == NULL, then errno should be already set by the failing |
10047 | | * API, because libbpf never returns NULL on success and it now always |
10048 | | * sets errno on error. So no extra errno handling for ptr == NULL |
10049 | | * case. |
10050 | | */ |
10051 | 10.0k | return -errno; |
10052 | 14.8k | } |
10053 | | |
10054 | | /* Replace link's underlying BPF program with the new one */ |
10055 | | int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog) |
10056 | 0 | { |
10057 | 0 | int ret; |
10058 | |
|
10059 | 0 | ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL); |
10060 | 0 | return libbpf_err_errno(ret); |
10061 | 0 | } |
10062 | | |
10063 | | /* Release "ownership" of underlying BPF resource (typically, BPF program |
10064 | | * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected |
10065 | | * link, when destructed through bpf_link__destroy() call won't attempt to |
10066 | | * detach/unregisted that BPF resource. This is useful in situations where, |
10067 | | * say, attached BPF program has to outlive userspace program that attached it |
10068 | | * in the system. Depending on type of BPF program, though, there might be |
10069 | | * additional steps (like pinning BPF program in BPF FS) necessary to ensure |
10070 | | * exit of userspace program doesn't trigger automatic detachment and clean up |
10071 | | * inside the kernel. |
10072 | | */ |
10073 | | void bpf_link__disconnect(struct bpf_link *link) |
10074 | 0 | { |
10075 | 0 | link->disconnected = true; |
10076 | 0 | } |
10077 | | |
10078 | | int bpf_link__destroy(struct bpf_link *link) |
10079 | 0 | { |
10080 | 0 | int err = 0; |
10081 | |
|
10082 | 0 | if (IS_ERR_OR_NULL(link)) |
10083 | 0 | return 0; |
10084 | | |
10085 | 0 | if (!link->disconnected && link->detach) |
10086 | 0 | err = link->detach(link); |
10087 | 0 | if (link->pin_path) |
10088 | 0 | free(link->pin_path); |
10089 | 0 | if (link->dealloc) |
10090 | 0 | link->dealloc(link); |
10091 | 0 | else |
10092 | 0 | free(link); |
10093 | |
|
10094 | 0 | return libbpf_err(err); |
10095 | 0 | } |
10096 | | |
10097 | | int bpf_link__fd(const struct bpf_link *link) |
10098 | 0 | { |
10099 | 0 | return link->fd; |
10100 | 0 | } |
10101 | | |
10102 | | const char *bpf_link__pin_path(const struct bpf_link *link) |
10103 | 0 | { |
10104 | 0 | return link->pin_path; |
10105 | 0 | } |
10106 | | |
10107 | | static int bpf_link__detach_fd(struct bpf_link *link) |
10108 | 0 | { |
10109 | 0 | return libbpf_err_errno(close(link->fd)); |
10110 | 0 | } |
10111 | | |
10112 | | struct bpf_link *bpf_link__open(const char *path) |
10113 | 0 | { |
10114 | 0 | struct bpf_link *link; |
10115 | 0 | int fd; |
10116 | |
|
10117 | 0 | fd = bpf_obj_get(path); |
10118 | 0 | if (fd < 0) { |
10119 | 0 | fd = -errno; |
10120 | 0 | pr_warn("failed to open link at %s: %d\n", path, fd); |
10121 | 0 | return libbpf_err_ptr(fd); |
10122 | 0 | } |
10123 | | |
10124 | 0 | link = calloc(1, sizeof(*link)); |
10125 | 0 | if (!link) { |
10126 | 0 | close(fd); |
10127 | 0 | return libbpf_err_ptr(-ENOMEM); |
10128 | 0 | } |
10129 | 0 | link->detach = &bpf_link__detach_fd; |
10130 | 0 | link->fd = fd; |
10131 | |
|
10132 | 0 | link->pin_path = strdup(path); |
10133 | 0 | if (!link->pin_path) { |
10134 | 0 | bpf_link__destroy(link); |
10135 | 0 | return libbpf_err_ptr(-ENOMEM); |
10136 | 0 | } |
10137 | | |
10138 | 0 | return link; |
10139 | 0 | } |
10140 | | |
10141 | | int bpf_link__detach(struct bpf_link *link) |
10142 | 0 | { |
10143 | 0 | return bpf_link_detach(link->fd) ? -errno : 0; |
10144 | 0 | } |
10145 | | |
10146 | | int bpf_link__pin(struct bpf_link *link, const char *path) |
10147 | 0 | { |
10148 | 0 | int err; |
10149 | |
|
10150 | 0 | if (link->pin_path) |
10151 | 0 | return libbpf_err(-EBUSY); |
10152 | 0 | err = make_parent_dir(path); |
10153 | 0 | if (err) |
10154 | 0 | return libbpf_err(err); |
10155 | 0 | err = check_path(path); |
10156 | 0 | if (err) |
10157 | 0 | return libbpf_err(err); |
10158 | | |
10159 | 0 | link->pin_path = strdup(path); |
10160 | 0 | if (!link->pin_path) |
10161 | 0 | return libbpf_err(-ENOMEM); |
10162 | | |
10163 | 0 | if (bpf_obj_pin(link->fd, link->pin_path)) { |
10164 | 0 | err = -errno; |
10165 | 0 | zfree(&link->pin_path); |
10166 | 0 | return libbpf_err(err); |
10167 | 0 | } |
10168 | | |
10169 | 0 | pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); |
10170 | 0 | return 0; |
10171 | 0 | } |
10172 | | |
10173 | | int bpf_link__unpin(struct bpf_link *link) |
10174 | 0 | { |
10175 | 0 | int err; |
10176 | |
|
10177 | 0 | if (!link->pin_path) |
10178 | 0 | return libbpf_err(-EINVAL); |
10179 | | |
10180 | 0 | err = unlink(link->pin_path); |
10181 | 0 | if (err != 0) |
10182 | 0 | return -errno; |
10183 | | |
10184 | 0 | pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); |
10185 | 0 | zfree(&link->pin_path); |
10186 | 0 | return 0; |
10187 | 0 | } |
10188 | | |
10189 | | struct bpf_link_perf { |
10190 | | struct bpf_link link; |
10191 | | int perf_event_fd; |
10192 | | /* legacy kprobe support: keep track of probe identifier and type */ |
10193 | | char *legacy_probe_name; |
10194 | | bool legacy_is_kprobe; |
10195 | | bool legacy_is_retprobe; |
10196 | | }; |
10197 | | |
10198 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe); |
10199 | | static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe); |
10200 | | |
10201 | | static int bpf_link_perf_detach(struct bpf_link *link) |
10202 | 0 | { |
10203 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
10204 | 0 | int err = 0; |
10205 | |
|
10206 | 0 | if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) |
10207 | 0 | err = -errno; |
10208 | |
|
10209 | 0 | if (perf_link->perf_event_fd != link->fd) |
10210 | 0 | close(perf_link->perf_event_fd); |
10211 | 0 | close(link->fd); |
10212 | | |
10213 | | /* legacy uprobe/kprobe needs to be removed after perf event fd closure */ |
10214 | 0 | if (perf_link->legacy_probe_name) { |
10215 | 0 | if (perf_link->legacy_is_kprobe) { |
10216 | 0 | err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, |
10217 | 0 | perf_link->legacy_is_retprobe); |
10218 | 0 | } else { |
10219 | 0 | err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, |
10220 | 0 | perf_link->legacy_is_retprobe); |
10221 | 0 | } |
10222 | 0 | } |
10223 | |
|
10224 | 0 | return err; |
10225 | 0 | } |
10226 | | |
10227 | | static void bpf_link_perf_dealloc(struct bpf_link *link) |
10228 | 0 | { |
10229 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
10230 | |
|
10231 | 0 | free(perf_link->legacy_probe_name); |
10232 | 0 | free(perf_link); |
10233 | 0 | } |
10234 | | |
10235 | | struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, |
10236 | | const struct bpf_perf_event_opts *opts) |
10237 | 0 | { |
10238 | 0 | char errmsg[STRERR_BUFSIZE]; |
10239 | 0 | struct bpf_link_perf *link; |
10240 | 0 | int prog_fd, link_fd = -1, err; |
10241 | 0 | bool force_ioctl_attach; |
10242 | |
|
10243 | 0 | if (!OPTS_VALID(opts, bpf_perf_event_opts)) |
10244 | 0 | return libbpf_err_ptr(-EINVAL); |
10245 | | |
10246 | 0 | if (pfd < 0) { |
10247 | 0 | pr_warn("prog '%s': invalid perf event FD %d\n", |
10248 | 0 | prog->name, pfd); |
10249 | 0 | return libbpf_err_ptr(-EINVAL); |
10250 | 0 | } |
10251 | 0 | prog_fd = bpf_program__fd(prog); |
10252 | 0 | if (prog_fd < 0) { |
10253 | 0 | pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n", |
10254 | 0 | prog->name); |
10255 | 0 | return libbpf_err_ptr(-EINVAL); |
10256 | 0 | } |
10257 | | |
10258 | 0 | link = calloc(1, sizeof(*link)); |
10259 | 0 | if (!link) |
10260 | 0 | return libbpf_err_ptr(-ENOMEM); |
10261 | 0 | link->link.detach = &bpf_link_perf_detach; |
10262 | 0 | link->link.dealloc = &bpf_link_perf_dealloc; |
10263 | 0 | link->perf_event_fd = pfd; |
10264 | |
|
10265 | 0 | force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false); |
10266 | 0 | if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { |
10267 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts, |
10268 | 0 | .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0)); |
10269 | |
|
10270 | 0 | link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts); |
10271 | 0 | if (link_fd < 0) { |
10272 | 0 | err = -errno; |
10273 | 0 | pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n", |
10274 | 0 | prog->name, pfd, |
10275 | 0 | err, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10276 | 0 | goto err_out; |
10277 | 0 | } |
10278 | 0 | link->link.fd = link_fd; |
10279 | 0 | } else { |
10280 | 0 | if (OPTS_GET(opts, bpf_cookie, 0)) { |
10281 | 0 | pr_warn("prog '%s': user context value is not supported\n", prog->name); |
10282 | 0 | err = -EOPNOTSUPP; |
10283 | 0 | goto err_out; |
10284 | 0 | } |
10285 | | |
10286 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { |
10287 | 0 | err = -errno; |
10288 | 0 | pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n", |
10289 | 0 | prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10290 | 0 | if (err == -EPROTO) |
10291 | 0 | pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n", |
10292 | 0 | prog->name, pfd); |
10293 | 0 | goto err_out; |
10294 | 0 | } |
10295 | 0 | link->link.fd = pfd; |
10296 | 0 | } |
10297 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
10298 | 0 | err = -errno; |
10299 | 0 | pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n", |
10300 | 0 | prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10301 | 0 | goto err_out; |
10302 | 0 | } |
10303 | | |
10304 | 0 | return &link->link; |
10305 | 0 | err_out: |
10306 | 0 | if (link_fd >= 0) |
10307 | 0 | close(link_fd); |
10308 | 0 | free(link); |
10309 | 0 | return libbpf_err_ptr(err); |
10310 | 0 | } |
10311 | | |
10312 | | struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd) |
10313 | 0 | { |
10314 | 0 | return bpf_program__attach_perf_event_opts(prog, pfd, NULL); |
10315 | 0 | } |
10316 | | |
10317 | | /* |
10318 | | * this function is expected to parse integer in the range of [0, 2^31-1] from |
10319 | | * given file using scanf format string fmt. If actual parsed value is |
10320 | | * negative, the result might be indistinguishable from error |
10321 | | */ |
10322 | | static int parse_uint_from_file(const char *file, const char *fmt) |
10323 | 0 | { |
10324 | 0 | char buf[STRERR_BUFSIZE]; |
10325 | 0 | int err, ret; |
10326 | 0 | FILE *f; |
10327 | |
|
10328 | 0 | f = fopen(file, "re"); |
10329 | 0 | if (!f) { |
10330 | 0 | err = -errno; |
10331 | 0 | pr_debug("failed to open '%s': %s\n", file, |
10332 | 0 | libbpf_strerror_r(err, buf, sizeof(buf))); |
10333 | 0 | return err; |
10334 | 0 | } |
10335 | 0 | err = fscanf(f, fmt, &ret); |
10336 | 0 | if (err != 1) { |
10337 | 0 | err = err == EOF ? -EIO : -errno; |
10338 | 0 | pr_debug("failed to parse '%s': %s\n", file, |
10339 | 0 | libbpf_strerror_r(err, buf, sizeof(buf))); |
10340 | 0 | fclose(f); |
10341 | 0 | return err; |
10342 | 0 | } |
10343 | 0 | fclose(f); |
10344 | 0 | return ret; |
10345 | 0 | } |
10346 | | |
10347 | | static int determine_kprobe_perf_type(void) |
10348 | 0 | { |
10349 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/type"; |
10350 | |
|
10351 | 0 | return parse_uint_from_file(file, "%d\n"); |
10352 | 0 | } |
10353 | | |
10354 | | static int determine_uprobe_perf_type(void) |
10355 | 0 | { |
10356 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/type"; |
10357 | |
|
10358 | 0 | return parse_uint_from_file(file, "%d\n"); |
10359 | 0 | } |
10360 | | |
10361 | | static int determine_kprobe_retprobe_bit(void) |
10362 | 0 | { |
10363 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe"; |
10364 | |
|
10365 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
10366 | 0 | } |
10367 | | |
10368 | | static int determine_uprobe_retprobe_bit(void) |
10369 | 0 | { |
10370 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; |
10371 | |
|
10372 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
10373 | 0 | } |
10374 | | |
10375 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32 |
10376 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32 |
10377 | | |
10378 | | static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, |
10379 | | uint64_t offset, int pid, size_t ref_ctr_off) |
10380 | 0 | { |
10381 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
10382 | 0 | struct perf_event_attr attr; |
10383 | 0 | char errmsg[STRERR_BUFSIZE]; |
10384 | 0 | int type, pfd; |
10385 | |
|
10386 | 0 | if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS)) |
10387 | 0 | return -EINVAL; |
10388 | | |
10389 | 0 | memset(&attr, 0, attr_sz); |
10390 | |
|
10391 | 0 | type = uprobe ? determine_uprobe_perf_type() |
10392 | 0 | : determine_kprobe_perf_type(); |
10393 | 0 | if (type < 0) { |
10394 | 0 | pr_warn("failed to determine %s perf type: %s\n", |
10395 | 0 | uprobe ? "uprobe" : "kprobe", |
10396 | 0 | libbpf_strerror_r(type, errmsg, sizeof(errmsg))); |
10397 | 0 | return type; |
10398 | 0 | } |
10399 | 0 | if (retprobe) { |
10400 | 0 | int bit = uprobe ? determine_uprobe_retprobe_bit() |
10401 | 0 | : determine_kprobe_retprobe_bit(); |
10402 | |
|
10403 | 0 | if (bit < 0) { |
10404 | 0 | pr_warn("failed to determine %s retprobe bit: %s\n", |
10405 | 0 | uprobe ? "uprobe" : "kprobe", |
10406 | 0 | libbpf_strerror_r(bit, errmsg, sizeof(errmsg))); |
10407 | 0 | return bit; |
10408 | 0 | } |
10409 | 0 | attr.config |= 1 << bit; |
10410 | 0 | } |
10411 | 0 | attr.size = attr_sz; |
10412 | 0 | attr.type = type; |
10413 | 0 | attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
10414 | 0 | attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ |
10415 | 0 | attr.config2 = offset; /* kprobe_addr or probe_offset */ |
10416 | | |
10417 | | /* pid filter is meaningful only for uprobes */ |
10418 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
10419 | 0 | pid < 0 ? -1 : pid /* pid */, |
10420 | 0 | pid == -1 ? 0 : -1 /* cpu */, |
10421 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
10422 | 0 | return pfd >= 0 ? pfd : -errno; |
10423 | 0 | } |
10424 | | |
10425 | | static int append_to_file(const char *file, const char *fmt, ...) |
10426 | 0 | { |
10427 | 0 | int fd, n, err = 0; |
10428 | 0 | va_list ap; |
10429 | 0 | char buf[1024]; |
10430 | |
|
10431 | 0 | va_start(ap, fmt); |
10432 | 0 | n = vsnprintf(buf, sizeof(buf), fmt, ap); |
10433 | 0 | va_end(ap); |
10434 | |
|
10435 | 0 | if (n < 0 || n >= sizeof(buf)) |
10436 | 0 | return -EINVAL; |
10437 | | |
10438 | 0 | fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0); |
10439 | 0 | if (fd < 0) |
10440 | 0 | return -errno; |
10441 | | |
10442 | 0 | if (write(fd, buf, n) < 0) |
10443 | 0 | err = -errno; |
10444 | |
|
10445 | 0 | close(fd); |
10446 | 0 | return err; |
10447 | 0 | } |
10448 | | |
10449 | 0 | #define DEBUGFS "/sys/kernel/debug/tracing" |
10450 | 0 | #define TRACEFS "/sys/kernel/tracing" |
10451 | | |
10452 | | static bool use_debugfs(void) |
10453 | 0 | { |
10454 | 0 | static int has_debugfs = -1; |
10455 | |
|
10456 | 0 | if (has_debugfs < 0) |
10457 | 0 | has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0; |
10458 | |
|
10459 | 0 | return has_debugfs == 1; |
10460 | 0 | } |
10461 | | |
10462 | | static const char *tracefs_path(void) |
10463 | 0 | { |
10464 | 0 | return use_debugfs() ? DEBUGFS : TRACEFS; |
10465 | 0 | } |
10466 | | |
10467 | | static const char *tracefs_kprobe_events(void) |
10468 | 0 | { |
10469 | 0 | return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events"; |
10470 | 0 | } |
10471 | | |
10472 | | static const char *tracefs_uprobe_events(void) |
10473 | 0 | { |
10474 | 0 | return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events"; |
10475 | 0 | } |
10476 | | |
10477 | | static const char *tracefs_available_filter_functions(void) |
10478 | 0 | { |
10479 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions" |
10480 | 0 | : TRACEFS"/available_filter_functions"; |
10481 | 0 | } |
10482 | | |
10483 | | static const char *tracefs_available_filter_functions_addrs(void) |
10484 | 0 | { |
10485 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs" |
10486 | 0 | : TRACEFS"/available_filter_functions_addrs"; |
10487 | 0 | } |
10488 | | |
10489 | | static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz, |
10490 | | const char *kfunc_name, size_t offset) |
10491 | 0 | { |
10492 | 0 | static int index = 0; |
10493 | 0 | int i; |
10494 | |
|
10495 | 0 | snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset, |
10496 | 0 | __sync_fetch_and_add(&index, 1)); |
10497 | | |
10498 | | /* sanitize binary_path in the probe name */ |
10499 | 0 | for (i = 0; buf[i]; i++) { |
10500 | 0 | if (!isalnum(buf[i])) |
10501 | 0 | buf[i] = '_'; |
10502 | 0 | } |
10503 | 0 | } |
10504 | | |
10505 | | static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, |
10506 | | const char *kfunc_name, size_t offset) |
10507 | 0 | { |
10508 | 0 | return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx", |
10509 | 0 | retprobe ? 'r' : 'p', |
10510 | 0 | retprobe ? "kretprobes" : "kprobes", |
10511 | 0 | probe_name, kfunc_name, offset); |
10512 | 0 | } |
10513 | | |
10514 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe) |
10515 | 0 | { |
10516 | 0 | return append_to_file(tracefs_kprobe_events(), "-:%s/%s", |
10517 | 0 | retprobe ? "kretprobes" : "kprobes", probe_name); |
10518 | 0 | } |
10519 | | |
10520 | | static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
10521 | 0 | { |
10522 | 0 | char file[256]; |
10523 | |
|
10524 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
10525 | 0 | tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name); |
10526 | |
|
10527 | 0 | return parse_uint_from_file(file, "%d\n"); |
10528 | 0 | } |
10529 | | |
10530 | | static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, |
10531 | | const char *kfunc_name, size_t offset, int pid) |
10532 | 0 | { |
10533 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
10534 | 0 | struct perf_event_attr attr; |
10535 | 0 | char errmsg[STRERR_BUFSIZE]; |
10536 | 0 | int type, pfd, err; |
10537 | |
|
10538 | 0 | err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset); |
10539 | 0 | if (err < 0) { |
10540 | 0 | pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n", |
10541 | 0 | kfunc_name, offset, |
10542 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10543 | 0 | return err; |
10544 | 0 | } |
10545 | 0 | type = determine_kprobe_perf_type_legacy(probe_name, retprobe); |
10546 | 0 | if (type < 0) { |
10547 | 0 | err = type; |
10548 | 0 | pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n", |
10549 | 0 | kfunc_name, offset, |
10550 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10551 | 0 | goto err_clean_legacy; |
10552 | 0 | } |
10553 | | |
10554 | 0 | memset(&attr, 0, attr_sz); |
10555 | 0 | attr.size = attr_sz; |
10556 | 0 | attr.config = type; |
10557 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
10558 | |
|
10559 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
10560 | 0 | pid < 0 ? -1 : pid, /* pid */ |
10561 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
10562 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
10563 | 0 | if (pfd < 0) { |
10564 | 0 | err = -errno; |
10565 | 0 | pr_warn("legacy kprobe perf_event_open() failed: %s\n", |
10566 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10567 | 0 | goto err_clean_legacy; |
10568 | 0 | } |
10569 | 0 | return pfd; |
10570 | | |
10571 | 0 | err_clean_legacy: |
10572 | | /* Clear the newly added legacy kprobe_event */ |
10573 | 0 | remove_kprobe_event_legacy(probe_name, retprobe); |
10574 | 0 | return err; |
10575 | 0 | } |
10576 | | |
10577 | | static const char *arch_specific_syscall_pfx(void) |
10578 | 0 | { |
10579 | 0 | #if defined(__x86_64__) |
10580 | 0 | return "x64"; |
10581 | | #elif defined(__i386__) |
10582 | | return "ia32"; |
10583 | | #elif defined(__s390x__) |
10584 | | return "s390x"; |
10585 | | #elif defined(__s390__) |
10586 | | return "s390"; |
10587 | | #elif defined(__arm__) |
10588 | | return "arm"; |
10589 | | #elif defined(__aarch64__) |
10590 | | return "arm64"; |
10591 | | #elif defined(__mips__) |
10592 | | return "mips"; |
10593 | | #elif defined(__riscv) |
10594 | | return "riscv"; |
10595 | | #elif defined(__powerpc__) |
10596 | | return "powerpc"; |
10597 | | #elif defined(__powerpc64__) |
10598 | | return "powerpc64"; |
10599 | | #else |
10600 | | return NULL; |
10601 | | #endif |
10602 | 0 | } |
10603 | | |
10604 | | static int probe_kern_syscall_wrapper(void) |
10605 | 0 | { |
10606 | 0 | char syscall_name[64]; |
10607 | 0 | const char *ksys_pfx; |
10608 | |
|
10609 | 0 | ksys_pfx = arch_specific_syscall_pfx(); |
10610 | 0 | if (!ksys_pfx) |
10611 | 0 | return 0; |
10612 | | |
10613 | 0 | snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx); |
10614 | |
|
10615 | 0 | if (determine_kprobe_perf_type() >= 0) { |
10616 | 0 | int pfd; |
10617 | |
|
10618 | 0 | pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0); |
10619 | 0 | if (pfd >= 0) |
10620 | 0 | close(pfd); |
10621 | |
|
10622 | 0 | return pfd >= 0 ? 1 : 0; |
10623 | 0 | } else { /* legacy mode */ |
10624 | 0 | char probe_name[128]; |
10625 | |
|
10626 | 0 | gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0); |
10627 | 0 | if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0) |
10628 | 0 | return 0; |
10629 | | |
10630 | 0 | (void)remove_kprobe_event_legacy(probe_name, false); |
10631 | 0 | return 1; |
10632 | 0 | } |
10633 | 0 | } |
10634 | | |
10635 | | struct bpf_link * |
10636 | | bpf_program__attach_kprobe_opts(const struct bpf_program *prog, |
10637 | | const char *func_name, |
10638 | | const struct bpf_kprobe_opts *opts) |
10639 | 0 | { |
10640 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
10641 | 0 | enum probe_attach_mode attach_mode; |
10642 | 0 | char errmsg[STRERR_BUFSIZE]; |
10643 | 0 | char *legacy_probe = NULL; |
10644 | 0 | struct bpf_link *link; |
10645 | 0 | size_t offset; |
10646 | 0 | bool retprobe, legacy; |
10647 | 0 | int pfd, err; |
10648 | |
|
10649 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_opts)) |
10650 | 0 | return libbpf_err_ptr(-EINVAL); |
10651 | | |
10652 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
10653 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
10654 | 0 | offset = OPTS_GET(opts, offset, 0); |
10655 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
10656 | |
|
10657 | 0 | legacy = determine_kprobe_perf_type() < 0; |
10658 | 0 | switch (attach_mode) { |
10659 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
10660 | 0 | legacy = true; |
10661 | 0 | pe_opts.force_ioctl_attach = true; |
10662 | 0 | break; |
10663 | 0 | case PROBE_ATTACH_MODE_PERF: |
10664 | 0 | if (legacy) |
10665 | 0 | return libbpf_err_ptr(-ENOTSUP); |
10666 | 0 | pe_opts.force_ioctl_attach = true; |
10667 | 0 | break; |
10668 | 0 | case PROBE_ATTACH_MODE_LINK: |
10669 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
10670 | 0 | return libbpf_err_ptr(-ENOTSUP); |
10671 | 0 | break; |
10672 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
10673 | 0 | break; |
10674 | 0 | default: |
10675 | 0 | return libbpf_err_ptr(-EINVAL); |
10676 | 0 | } |
10677 | | |
10678 | 0 | if (!legacy) { |
10679 | 0 | pfd = perf_event_open_probe(false /* uprobe */, retprobe, |
10680 | 0 | func_name, offset, |
10681 | 0 | -1 /* pid */, 0 /* ref_ctr_off */); |
10682 | 0 | } else { |
10683 | 0 | char probe_name[256]; |
10684 | |
|
10685 | 0 | gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), |
10686 | 0 | func_name, offset); |
10687 | |
|
10688 | 0 | legacy_probe = strdup(probe_name); |
10689 | 0 | if (!legacy_probe) |
10690 | 0 | return libbpf_err_ptr(-ENOMEM); |
10691 | | |
10692 | 0 | pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name, |
10693 | 0 | offset, -1 /* pid */); |
10694 | 0 | } |
10695 | 0 | if (pfd < 0) { |
10696 | 0 | err = -errno; |
10697 | 0 | pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n", |
10698 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
10699 | 0 | func_name, offset, |
10700 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10701 | 0 | goto err_out; |
10702 | 0 | } |
10703 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
10704 | 0 | err = libbpf_get_error(link); |
10705 | 0 | if (err) { |
10706 | 0 | close(pfd); |
10707 | 0 | pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n", |
10708 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
10709 | 0 | func_name, offset, |
10710 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
10711 | 0 | goto err_clean_legacy; |
10712 | 0 | } |
10713 | 0 | if (legacy) { |
10714 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
10715 | |
|
10716 | 0 | perf_link->legacy_probe_name = legacy_probe; |
10717 | 0 | perf_link->legacy_is_kprobe = true; |
10718 | 0 | perf_link->legacy_is_retprobe = retprobe; |
10719 | 0 | } |
10720 | |
|
10721 | 0 | return link; |
10722 | | |
10723 | 0 | err_clean_legacy: |
10724 | 0 | if (legacy) |
10725 | 0 | remove_kprobe_event_legacy(legacy_probe, retprobe); |
10726 | 0 | err_out: |
10727 | 0 | free(legacy_probe); |
10728 | 0 | return libbpf_err_ptr(err); |
10729 | 0 | } |
10730 | | |
10731 | | struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, |
10732 | | bool retprobe, |
10733 | | const char *func_name) |
10734 | 0 | { |
10735 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts, |
10736 | 0 | .retprobe = retprobe, |
10737 | 0 | ); |
10738 | |
|
10739 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &opts); |
10740 | 0 | } |
10741 | | |
10742 | | struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog, |
10743 | | const char *syscall_name, |
10744 | | const struct bpf_ksyscall_opts *opts) |
10745 | 0 | { |
10746 | 0 | LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); |
10747 | 0 | char func_name[128]; |
10748 | |
|
10749 | 0 | if (!OPTS_VALID(opts, bpf_ksyscall_opts)) |
10750 | 0 | return libbpf_err_ptr(-EINVAL); |
10751 | | |
10752 | 0 | if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { |
10753 | | /* arch_specific_syscall_pfx() should never return NULL here |
10754 | | * because it is guarded by kernel_supports(). However, since |
10755 | | * compiler does not know that we have an explicit conditional |
10756 | | * as well. |
10757 | | */ |
10758 | 0 | snprintf(func_name, sizeof(func_name), "__%s_sys_%s", |
10759 | 0 | arch_specific_syscall_pfx() ? : "", syscall_name); |
10760 | 0 | } else { |
10761 | 0 | snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name); |
10762 | 0 | } |
10763 | |
|
10764 | 0 | kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false); |
10765 | 0 | kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
10766 | |
|
10767 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts); |
10768 | 0 | } |
10769 | | |
10770 | | /* Adapted from perf/util/string.c */ |
10771 | | bool glob_match(const char *str, const char *pat) |
10772 | 0 | { |
10773 | 0 | while (*str && *pat && *pat != '*') { |
10774 | 0 | if (*pat == '?') { /* Matches any single character */ |
10775 | 0 | str++; |
10776 | 0 | pat++; |
10777 | 0 | continue; |
10778 | 0 | } |
10779 | 0 | if (*str != *pat) |
10780 | 0 | return false; |
10781 | 0 | str++; |
10782 | 0 | pat++; |
10783 | 0 | } |
10784 | | /* Check wild card */ |
10785 | 0 | if (*pat == '*') { |
10786 | 0 | while (*pat == '*') |
10787 | 0 | pat++; |
10788 | 0 | if (!*pat) /* Tail wild card matches all */ |
10789 | 0 | return true; |
10790 | 0 | while (*str) |
10791 | 0 | if (glob_match(str++, pat)) |
10792 | 0 | return true; |
10793 | 0 | } |
10794 | 0 | return !*str && !*pat; |
10795 | 0 | } |
10796 | | |
10797 | | struct kprobe_multi_resolve { |
10798 | | const char *pattern; |
10799 | | unsigned long *addrs; |
10800 | | size_t cap; |
10801 | | size_t cnt; |
10802 | | }; |
10803 | | |
10804 | | struct avail_kallsyms_data { |
10805 | | char **syms; |
10806 | | size_t cnt; |
10807 | | struct kprobe_multi_resolve *res; |
10808 | | }; |
10809 | | |
10810 | | static int avail_func_cmp(const void *a, const void *b) |
10811 | 0 | { |
10812 | 0 | return strcmp(*(const char **)a, *(const char **)b); |
10813 | 0 | } |
10814 | | |
10815 | | static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type, |
10816 | | const char *sym_name, void *ctx) |
10817 | 0 | { |
10818 | 0 | struct avail_kallsyms_data *data = ctx; |
10819 | 0 | struct kprobe_multi_resolve *res = data->res; |
10820 | 0 | int err; |
10821 | |
|
10822 | 0 | if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) |
10823 | 0 | return 0; |
10824 | | |
10825 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); |
10826 | 0 | if (err) |
10827 | 0 | return err; |
10828 | | |
10829 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
10830 | 0 | return 0; |
10831 | 0 | } |
10832 | | |
10833 | | static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res) |
10834 | 0 | { |
10835 | 0 | const char *available_functions_file = tracefs_available_filter_functions(); |
10836 | 0 | struct avail_kallsyms_data data; |
10837 | 0 | char sym_name[500]; |
10838 | 0 | FILE *f; |
10839 | 0 | int err = 0, ret, i; |
10840 | 0 | char **syms = NULL; |
10841 | 0 | size_t cap = 0, cnt = 0; |
10842 | |
|
10843 | 0 | f = fopen(available_functions_file, "re"); |
10844 | 0 | if (!f) { |
10845 | 0 | err = -errno; |
10846 | 0 | pr_warn("failed to open %s: %d\n", available_functions_file, err); |
10847 | 0 | return err; |
10848 | 0 | } |
10849 | | |
10850 | 0 | while (true) { |
10851 | 0 | char *name; |
10852 | |
|
10853 | 0 | ret = fscanf(f, "%499s%*[^\n]\n", sym_name); |
10854 | 0 | if (ret == EOF && feof(f)) |
10855 | 0 | break; |
10856 | | |
10857 | 0 | if (ret != 1) { |
10858 | 0 | pr_warn("failed to parse available_filter_functions entry: %d\n", ret); |
10859 | 0 | err = -EINVAL; |
10860 | 0 | goto cleanup; |
10861 | 0 | } |
10862 | | |
10863 | 0 | if (!glob_match(sym_name, res->pattern)) |
10864 | 0 | continue; |
10865 | | |
10866 | 0 | err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1); |
10867 | 0 | if (err) |
10868 | 0 | goto cleanup; |
10869 | | |
10870 | 0 | name = strdup(sym_name); |
10871 | 0 | if (!name) { |
10872 | 0 | err = -errno; |
10873 | 0 | goto cleanup; |
10874 | 0 | } |
10875 | | |
10876 | 0 | syms[cnt++] = name; |
10877 | 0 | } |
10878 | | |
10879 | | /* no entries found, bail out */ |
10880 | 0 | if (cnt == 0) { |
10881 | 0 | err = -ENOENT; |
10882 | 0 | goto cleanup; |
10883 | 0 | } |
10884 | | |
10885 | | /* sort available functions */ |
10886 | 0 | qsort(syms, cnt, sizeof(*syms), avail_func_cmp); |
10887 | |
|
10888 | 0 | data.syms = syms; |
10889 | 0 | data.res = res; |
10890 | 0 | data.cnt = cnt; |
10891 | 0 | libbpf_kallsyms_parse(avail_kallsyms_cb, &data); |
10892 | |
|
10893 | 0 | if (res->cnt == 0) |
10894 | 0 | err = -ENOENT; |
10895 | |
|
10896 | 0 | cleanup: |
10897 | 0 | for (i = 0; i < cnt; i++) |
10898 | 0 | free((char *)syms[i]); |
10899 | 0 | free(syms); |
10900 | |
|
10901 | 0 | fclose(f); |
10902 | 0 | return err; |
10903 | 0 | } |
10904 | | |
10905 | | static bool has_available_filter_functions_addrs(void) |
10906 | 0 | { |
10907 | 0 | return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; |
10908 | 0 | } |
10909 | | |
10910 | | static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res) |
10911 | 0 | { |
10912 | 0 | const char *available_path = tracefs_available_filter_functions_addrs(); |
10913 | 0 | char sym_name[500]; |
10914 | 0 | FILE *f; |
10915 | 0 | int ret, err = 0; |
10916 | 0 | unsigned long long sym_addr; |
10917 | |
|
10918 | 0 | f = fopen(available_path, "re"); |
10919 | 0 | if (!f) { |
10920 | 0 | err = -errno; |
10921 | 0 | pr_warn("failed to open %s: %d\n", available_path, err); |
10922 | 0 | return err; |
10923 | 0 | } |
10924 | | |
10925 | 0 | while (true) { |
10926 | 0 | ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name); |
10927 | 0 | if (ret == EOF && feof(f)) |
10928 | 0 | break; |
10929 | | |
10930 | 0 | if (ret != 2) { |
10931 | 0 | pr_warn("failed to parse available_filter_functions_addrs entry: %d\n", |
10932 | 0 | ret); |
10933 | 0 | err = -EINVAL; |
10934 | 0 | goto cleanup; |
10935 | 0 | } |
10936 | | |
10937 | 0 | if (!glob_match(sym_name, res->pattern)) |
10938 | 0 | continue; |
10939 | | |
10940 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, |
10941 | 0 | sizeof(*res->addrs), res->cnt + 1); |
10942 | 0 | if (err) |
10943 | 0 | goto cleanup; |
10944 | | |
10945 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
10946 | 0 | } |
10947 | | |
10948 | 0 | if (res->cnt == 0) |
10949 | 0 | err = -ENOENT; |
10950 | |
|
10951 | 0 | cleanup: |
10952 | 0 | fclose(f); |
10953 | 0 | return err; |
10954 | 0 | } |
10955 | | |
10956 | | struct bpf_link * |
10957 | | bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, |
10958 | | const char *pattern, |
10959 | | const struct bpf_kprobe_multi_opts *opts) |
10960 | 0 | { |
10961 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
10962 | 0 | struct kprobe_multi_resolve res = { |
10963 | 0 | .pattern = pattern, |
10964 | 0 | }; |
10965 | 0 | struct bpf_link *link = NULL; |
10966 | 0 | char errmsg[STRERR_BUFSIZE]; |
10967 | 0 | const unsigned long *addrs; |
10968 | 0 | int err, link_fd, prog_fd; |
10969 | 0 | const __u64 *cookies; |
10970 | 0 | const char **syms; |
10971 | 0 | bool retprobe; |
10972 | 0 | size_t cnt; |
10973 | |
|
10974 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_multi_opts)) |
10975 | 0 | return libbpf_err_ptr(-EINVAL); |
10976 | | |
10977 | 0 | syms = OPTS_GET(opts, syms, false); |
10978 | 0 | addrs = OPTS_GET(opts, addrs, false); |
10979 | 0 | cnt = OPTS_GET(opts, cnt, false); |
10980 | 0 | cookies = OPTS_GET(opts, cookies, false); |
10981 | |
|
10982 | 0 | if (!pattern && !addrs && !syms) |
10983 | 0 | return libbpf_err_ptr(-EINVAL); |
10984 | 0 | if (pattern && (addrs || syms || cookies || cnt)) |
10985 | 0 | return libbpf_err_ptr(-EINVAL); |
10986 | 0 | if (!pattern && !cnt) |
10987 | 0 | return libbpf_err_ptr(-EINVAL); |
10988 | 0 | if (addrs && syms) |
10989 | 0 | return libbpf_err_ptr(-EINVAL); |
10990 | | |
10991 | 0 | if (pattern) { |
10992 | 0 | if (has_available_filter_functions_addrs()) |
10993 | 0 | err = libbpf_available_kprobes_parse(&res); |
10994 | 0 | else |
10995 | 0 | err = libbpf_available_kallsyms_parse(&res); |
10996 | 0 | if (err) |
10997 | 0 | goto error; |
10998 | 0 | addrs = res.addrs; |
10999 | 0 | cnt = res.cnt; |
11000 | 0 | } |
11001 | | |
11002 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
11003 | |
|
11004 | 0 | lopts.kprobe_multi.syms = syms; |
11005 | 0 | lopts.kprobe_multi.addrs = addrs; |
11006 | 0 | lopts.kprobe_multi.cookies = cookies; |
11007 | 0 | lopts.kprobe_multi.cnt = cnt; |
11008 | 0 | lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0; |
11009 | |
|
11010 | 0 | link = calloc(1, sizeof(*link)); |
11011 | 0 | if (!link) { |
11012 | 0 | err = -ENOMEM; |
11013 | 0 | goto error; |
11014 | 0 | } |
11015 | 0 | link->detach = &bpf_link__detach_fd; |
11016 | |
|
11017 | 0 | prog_fd = bpf_program__fd(prog); |
11018 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &lopts); |
11019 | 0 | if (link_fd < 0) { |
11020 | 0 | err = -errno; |
11021 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
11022 | 0 | prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11023 | 0 | goto error; |
11024 | 0 | } |
11025 | 0 | link->fd = link_fd; |
11026 | 0 | free(res.addrs); |
11027 | 0 | return link; |
11028 | | |
11029 | 0 | error: |
11030 | 0 | free(link); |
11031 | 0 | free(res.addrs); |
11032 | 0 | return libbpf_err_ptr(err); |
11033 | 0 | } |
11034 | | |
11035 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11036 | 0 | { |
11037 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); |
11038 | 0 | unsigned long offset = 0; |
11039 | 0 | const char *func_name; |
11040 | 0 | char *func; |
11041 | 0 | int n; |
11042 | |
|
11043 | 0 | *link = NULL; |
11044 | | |
11045 | | /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ |
11046 | 0 | if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) |
11047 | 0 | return 0; |
11048 | | |
11049 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); |
11050 | 0 | if (opts.retprobe) |
11051 | 0 | func_name = prog->sec_name + sizeof("kretprobe/") - 1; |
11052 | 0 | else |
11053 | 0 | func_name = prog->sec_name + sizeof("kprobe/") - 1; |
11054 | |
|
11055 | 0 | n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); |
11056 | 0 | if (n < 1) { |
11057 | 0 | pr_warn("kprobe name is invalid: %s\n", func_name); |
11058 | 0 | return -EINVAL; |
11059 | 0 | } |
11060 | 0 | if (opts.retprobe && offset != 0) { |
11061 | 0 | free(func); |
11062 | 0 | pr_warn("kretprobes do not support offset specification\n"); |
11063 | 0 | return -EINVAL; |
11064 | 0 | } |
11065 | | |
11066 | 0 | opts.offset = offset; |
11067 | 0 | *link = bpf_program__attach_kprobe_opts(prog, func, &opts); |
11068 | 0 | free(func); |
11069 | 0 | return libbpf_get_error(*link); |
11070 | 0 | } |
11071 | | |
11072 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11073 | 0 | { |
11074 | 0 | LIBBPF_OPTS(bpf_ksyscall_opts, opts); |
11075 | 0 | const char *syscall_name; |
11076 | |
|
11077 | 0 | *link = NULL; |
11078 | | |
11079 | | /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ |
11080 | 0 | if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) |
11081 | 0 | return 0; |
11082 | | |
11083 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); |
11084 | 0 | if (opts.retprobe) |
11085 | 0 | syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; |
11086 | 0 | else |
11087 | 0 | syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; |
11088 | |
|
11089 | 0 | *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts); |
11090 | 0 | return *link ? 0 : -errno; |
11091 | 0 | } |
11092 | | |
11093 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11094 | 0 | { |
11095 | 0 | LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); |
11096 | 0 | const char *spec; |
11097 | 0 | char *pattern; |
11098 | 0 | int n; |
11099 | |
|
11100 | 0 | *link = NULL; |
11101 | | |
11102 | | /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ |
11103 | 0 | if (strcmp(prog->sec_name, "kprobe.multi") == 0 || |
11104 | 0 | strcmp(prog->sec_name, "kretprobe.multi") == 0) |
11105 | 0 | return 0; |
11106 | | |
11107 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); |
11108 | 0 | if (opts.retprobe) |
11109 | 0 | spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; |
11110 | 0 | else |
11111 | 0 | spec = prog->sec_name + sizeof("kprobe.multi/") - 1; |
11112 | |
|
11113 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); |
11114 | 0 | if (n < 1) { |
11115 | 0 | pr_warn("kprobe multi pattern is invalid: %s\n", pattern); |
11116 | 0 | return -EINVAL; |
11117 | 0 | } |
11118 | | |
11119 | 0 | *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts); |
11120 | 0 | free(pattern); |
11121 | 0 | return libbpf_get_error(*link); |
11122 | 0 | } |
11123 | | |
11124 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11125 | 0 | { |
11126 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL; |
11127 | 0 | LIBBPF_OPTS(bpf_uprobe_multi_opts, opts); |
11128 | 0 | int n, ret = -EINVAL; |
11129 | |
|
11130 | 0 | *link = NULL; |
11131 | |
|
11132 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
11133 | 0 | &probe_type, &binary_path, &func_name); |
11134 | 0 | switch (n) { |
11135 | 0 | case 1: |
11136 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
11137 | 0 | ret = 0; |
11138 | 0 | break; |
11139 | 0 | case 3: |
11140 | 0 | opts.retprobe = strcmp(probe_type, "uretprobe.multi") == 0; |
11141 | 0 | *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); |
11142 | 0 | ret = libbpf_get_error(*link); |
11143 | 0 | break; |
11144 | 0 | default: |
11145 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
11146 | 0 | prog->sec_name); |
11147 | 0 | break; |
11148 | 0 | } |
11149 | 0 | free(probe_type); |
11150 | 0 | free(binary_path); |
11151 | 0 | free(func_name); |
11152 | 0 | return ret; |
11153 | 0 | } |
11154 | | |
11155 | | static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz, |
11156 | | const char *binary_path, uint64_t offset) |
11157 | 0 | { |
11158 | 0 | int i; |
11159 | |
|
11160 | 0 | snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset); |
11161 | | |
11162 | | /* sanitize binary_path in the probe name */ |
11163 | 0 | for (i = 0; buf[i]; i++) { |
11164 | 0 | if (!isalnum(buf[i])) |
11165 | 0 | buf[i] = '_'; |
11166 | 0 | } |
11167 | 0 | } |
11168 | | |
11169 | | static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe, |
11170 | | const char *binary_path, size_t offset) |
11171 | 0 | { |
11172 | 0 | return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx", |
11173 | 0 | retprobe ? 'r' : 'p', |
11174 | 0 | retprobe ? "uretprobes" : "uprobes", |
11175 | 0 | probe_name, binary_path, offset); |
11176 | 0 | } |
11177 | | |
11178 | | static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe) |
11179 | 0 | { |
11180 | 0 | return append_to_file(tracefs_uprobe_events(), "-:%s/%s", |
11181 | 0 | retprobe ? "uretprobes" : "uprobes", probe_name); |
11182 | 0 | } |
11183 | | |
11184 | | static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
11185 | 0 | { |
11186 | 0 | char file[512]; |
11187 | |
|
11188 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
11189 | 0 | tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name); |
11190 | |
|
11191 | 0 | return parse_uint_from_file(file, "%d\n"); |
11192 | 0 | } |
11193 | | |
11194 | | static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, |
11195 | | const char *binary_path, size_t offset, int pid) |
11196 | 0 | { |
11197 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11198 | 0 | struct perf_event_attr attr; |
11199 | 0 | int type, pfd, err; |
11200 | |
|
11201 | 0 | err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset); |
11202 | 0 | if (err < 0) { |
11203 | 0 | pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n", |
11204 | 0 | binary_path, (size_t)offset, err); |
11205 | 0 | return err; |
11206 | 0 | } |
11207 | 0 | type = determine_uprobe_perf_type_legacy(probe_name, retprobe); |
11208 | 0 | if (type < 0) { |
11209 | 0 | err = type; |
11210 | 0 | pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n", |
11211 | 0 | binary_path, offset, err); |
11212 | 0 | goto err_clean_legacy; |
11213 | 0 | } |
11214 | | |
11215 | 0 | memset(&attr, 0, attr_sz); |
11216 | 0 | attr.size = attr_sz; |
11217 | 0 | attr.config = type; |
11218 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
11219 | |
|
11220 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
11221 | 0 | pid < 0 ? -1 : pid, /* pid */ |
11222 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
11223 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11224 | 0 | if (pfd < 0) { |
11225 | 0 | err = -errno; |
11226 | 0 | pr_warn("legacy uprobe perf_event_open() failed: %d\n", err); |
11227 | 0 | goto err_clean_legacy; |
11228 | 0 | } |
11229 | 0 | return pfd; |
11230 | | |
11231 | 0 | err_clean_legacy: |
11232 | | /* Clear the newly added legacy uprobe_event */ |
11233 | 0 | remove_uprobe_event_legacy(probe_name, retprobe); |
11234 | 0 | return err; |
11235 | 0 | } |
11236 | | |
11237 | | /* Find offset of function name in archive specified by path. Currently |
11238 | | * supported are .zip files that do not compress their contents, as used on |
11239 | | * Android in the form of APKs, for example. "file_name" is the name of the ELF |
11240 | | * file inside the archive. "func_name" matches symbol name or name@@LIB for |
11241 | | * library functions. |
11242 | | * |
11243 | | * An overview of the APK format specifically provided here: |
11244 | | * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents |
11245 | | */ |
11246 | | static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name, |
11247 | | const char *func_name) |
11248 | 0 | { |
11249 | 0 | struct zip_archive *archive; |
11250 | 0 | struct zip_entry entry; |
11251 | 0 | long ret; |
11252 | 0 | Elf *elf; |
11253 | |
|
11254 | 0 | archive = zip_archive_open(archive_path); |
11255 | 0 | if (IS_ERR(archive)) { |
11256 | 0 | ret = PTR_ERR(archive); |
11257 | 0 | pr_warn("zip: failed to open %s: %ld\n", archive_path, ret); |
11258 | 0 | return ret; |
11259 | 0 | } |
11260 | | |
11261 | 0 | ret = zip_archive_find_entry(archive, file_name, &entry); |
11262 | 0 | if (ret) { |
11263 | 0 | pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name, |
11264 | 0 | archive_path, ret); |
11265 | 0 | goto out; |
11266 | 0 | } |
11267 | 0 | pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path, |
11268 | 0 | (unsigned long)entry.data_offset); |
11269 | |
|
11270 | 0 | if (entry.compression) { |
11271 | 0 | pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name, |
11272 | 0 | archive_path); |
11273 | 0 | ret = -LIBBPF_ERRNO__FORMAT; |
11274 | 0 | goto out; |
11275 | 0 | } |
11276 | | |
11277 | 0 | elf = elf_memory((void *)entry.data, entry.data_length); |
11278 | 0 | if (!elf) { |
11279 | 0 | pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path, |
11280 | 0 | elf_errmsg(-1)); |
11281 | 0 | ret = -LIBBPF_ERRNO__LIBELF; |
11282 | 0 | goto out; |
11283 | 0 | } |
11284 | | |
11285 | 0 | ret = elf_find_func_offset(elf, file_name, func_name); |
11286 | 0 | if (ret > 0) { |
11287 | 0 | pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n", |
11288 | 0 | func_name, file_name, archive_path, entry.data_offset, ret, |
11289 | 0 | ret + entry.data_offset); |
11290 | 0 | ret += entry.data_offset; |
11291 | 0 | } |
11292 | 0 | elf_end(elf); |
11293 | |
|
11294 | 0 | out: |
11295 | 0 | zip_archive_close(archive); |
11296 | 0 | return ret; |
11297 | 0 | } |
11298 | | |
11299 | | static const char *arch_specific_lib_paths(void) |
11300 | 0 | { |
11301 | | /* |
11302 | | * Based on https://packages.debian.org/sid/libc6. |
11303 | | * |
11304 | | * Assume that the traced program is built for the same architecture |
11305 | | * as libbpf, which should cover the vast majority of cases. |
11306 | | */ |
11307 | 0 | #if defined(__x86_64__) |
11308 | 0 | return "/lib/x86_64-linux-gnu"; |
11309 | | #elif defined(__i386__) |
11310 | | return "/lib/i386-linux-gnu"; |
11311 | | #elif defined(__s390x__) |
11312 | | return "/lib/s390x-linux-gnu"; |
11313 | | #elif defined(__s390__) |
11314 | | return "/lib/s390-linux-gnu"; |
11315 | | #elif defined(__arm__) && defined(__SOFTFP__) |
11316 | | return "/lib/arm-linux-gnueabi"; |
11317 | | #elif defined(__arm__) && !defined(__SOFTFP__) |
11318 | | return "/lib/arm-linux-gnueabihf"; |
11319 | | #elif defined(__aarch64__) |
11320 | | return "/lib/aarch64-linux-gnu"; |
11321 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64 |
11322 | | return "/lib/mips64el-linux-gnuabi64"; |
11323 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32 |
11324 | | return "/lib/mipsel-linux-gnu"; |
11325 | | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
11326 | | return "/lib/powerpc64le-linux-gnu"; |
11327 | | #elif defined(__sparc__) && defined(__arch64__) |
11328 | | return "/lib/sparc64-linux-gnu"; |
11329 | | #elif defined(__riscv) && __riscv_xlen == 64 |
11330 | | return "/lib/riscv64-linux-gnu"; |
11331 | | #else |
11332 | | return NULL; |
11333 | | #endif |
11334 | 0 | } |
11335 | | |
11336 | | /* Get full path to program/shared library. */ |
11337 | | static int resolve_full_path(const char *file, char *result, size_t result_sz) |
11338 | 0 | { |
11339 | 0 | const char *search_paths[3] = {}; |
11340 | 0 | int i, perm; |
11341 | |
|
11342 | 0 | if (str_has_sfx(file, ".so") || strstr(file, ".so.")) { |
11343 | 0 | search_paths[0] = getenv("LD_LIBRARY_PATH"); |
11344 | 0 | search_paths[1] = "/usr/lib64:/usr/lib"; |
11345 | 0 | search_paths[2] = arch_specific_lib_paths(); |
11346 | 0 | perm = R_OK; |
11347 | 0 | } else { |
11348 | 0 | search_paths[0] = getenv("PATH"); |
11349 | 0 | search_paths[1] = "/usr/bin:/usr/sbin"; |
11350 | 0 | perm = R_OK | X_OK; |
11351 | 0 | } |
11352 | |
|
11353 | 0 | for (i = 0; i < ARRAY_SIZE(search_paths); i++) { |
11354 | 0 | const char *s; |
11355 | |
|
11356 | 0 | if (!search_paths[i]) |
11357 | 0 | continue; |
11358 | 0 | for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) { |
11359 | 0 | char *next_path; |
11360 | 0 | int seg_len; |
11361 | |
|
11362 | 0 | if (s[0] == ':') |
11363 | 0 | s++; |
11364 | 0 | next_path = strchr(s, ':'); |
11365 | 0 | seg_len = next_path ? next_path - s : strlen(s); |
11366 | 0 | if (!seg_len) |
11367 | 0 | continue; |
11368 | 0 | snprintf(result, result_sz, "%.*s/%s", seg_len, s, file); |
11369 | | /* ensure it has required permissions */ |
11370 | 0 | if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0) |
11371 | 0 | continue; |
11372 | 0 | pr_debug("resolved '%s' to '%s'\n", file, result); |
11373 | 0 | return 0; |
11374 | 0 | } |
11375 | 0 | } |
11376 | 0 | return -ENOENT; |
11377 | 0 | } |
11378 | | |
11379 | | struct bpf_link * |
11380 | | bpf_program__attach_uprobe_multi(const struct bpf_program *prog, |
11381 | | pid_t pid, |
11382 | | const char *path, |
11383 | | const char *func_pattern, |
11384 | | const struct bpf_uprobe_multi_opts *opts) |
11385 | 0 | { |
11386 | 0 | const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL; |
11387 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
11388 | 0 | unsigned long *resolved_offsets = NULL; |
11389 | 0 | int err = 0, link_fd, prog_fd; |
11390 | 0 | struct bpf_link *link = NULL; |
11391 | 0 | char errmsg[STRERR_BUFSIZE]; |
11392 | 0 | char full_path[PATH_MAX]; |
11393 | 0 | const __u64 *cookies; |
11394 | 0 | const char **syms; |
11395 | 0 | size_t cnt; |
11396 | |
|
11397 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_multi_opts)) |
11398 | 0 | return libbpf_err_ptr(-EINVAL); |
11399 | | |
11400 | 0 | syms = OPTS_GET(opts, syms, NULL); |
11401 | 0 | offsets = OPTS_GET(opts, offsets, NULL); |
11402 | 0 | ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL); |
11403 | 0 | cookies = OPTS_GET(opts, cookies, NULL); |
11404 | 0 | cnt = OPTS_GET(opts, cnt, 0); |
11405 | | |
11406 | | /* |
11407 | | * User can specify 2 mutually exclusive set of inputs: |
11408 | | * |
11409 | | * 1) use only path/func_pattern/pid arguments |
11410 | | * |
11411 | | * 2) use path/pid with allowed combinations of: |
11412 | | * syms/offsets/ref_ctr_offsets/cookies/cnt |
11413 | | * |
11414 | | * - syms and offsets are mutually exclusive |
11415 | | * - ref_ctr_offsets and cookies are optional |
11416 | | * |
11417 | | * Any other usage results in error. |
11418 | | */ |
11419 | |
|
11420 | 0 | if (!path) |
11421 | 0 | return libbpf_err_ptr(-EINVAL); |
11422 | 0 | if (!func_pattern && cnt == 0) |
11423 | 0 | return libbpf_err_ptr(-EINVAL); |
11424 | | |
11425 | 0 | if (func_pattern) { |
11426 | 0 | if (syms || offsets || ref_ctr_offsets || cookies || cnt) |
11427 | 0 | return libbpf_err_ptr(-EINVAL); |
11428 | 0 | } else { |
11429 | 0 | if (!!syms == !!offsets) |
11430 | 0 | return libbpf_err_ptr(-EINVAL); |
11431 | 0 | } |
11432 | | |
11433 | 0 | if (func_pattern) { |
11434 | 0 | if (!strchr(path, '/')) { |
11435 | 0 | err = resolve_full_path(path, full_path, sizeof(full_path)); |
11436 | 0 | if (err) { |
11437 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
11438 | 0 | prog->name, path, err); |
11439 | 0 | return libbpf_err_ptr(err); |
11440 | 0 | } |
11441 | 0 | path = full_path; |
11442 | 0 | } |
11443 | | |
11444 | 0 | err = elf_resolve_pattern_offsets(path, func_pattern, |
11445 | 0 | &resolved_offsets, &cnt); |
11446 | 0 | if (err < 0) |
11447 | 0 | return libbpf_err_ptr(err); |
11448 | 0 | offsets = resolved_offsets; |
11449 | 0 | } else if (syms) { |
11450 | 0 | err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets); |
11451 | 0 | if (err < 0) |
11452 | 0 | return libbpf_err_ptr(err); |
11453 | 0 | offsets = resolved_offsets; |
11454 | 0 | } |
11455 | | |
11456 | 0 | lopts.uprobe_multi.path = path; |
11457 | 0 | lopts.uprobe_multi.offsets = offsets; |
11458 | 0 | lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets; |
11459 | 0 | lopts.uprobe_multi.cookies = cookies; |
11460 | 0 | lopts.uprobe_multi.cnt = cnt; |
11461 | 0 | lopts.uprobe_multi.flags = OPTS_GET(opts, retprobe, false) ? BPF_F_UPROBE_MULTI_RETURN : 0; |
11462 | |
|
11463 | 0 | if (pid == 0) |
11464 | 0 | pid = getpid(); |
11465 | 0 | if (pid > 0) |
11466 | 0 | lopts.uprobe_multi.pid = pid; |
11467 | |
|
11468 | 0 | link = calloc(1, sizeof(*link)); |
11469 | 0 | if (!link) { |
11470 | 0 | err = -ENOMEM; |
11471 | 0 | goto error; |
11472 | 0 | } |
11473 | 0 | link->detach = &bpf_link__detach_fd; |
11474 | |
|
11475 | 0 | prog_fd = bpf_program__fd(prog); |
11476 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &lopts); |
11477 | 0 | if (link_fd < 0) { |
11478 | 0 | err = -errno; |
11479 | 0 | pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", |
11480 | 0 | prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11481 | 0 | goto error; |
11482 | 0 | } |
11483 | 0 | link->fd = link_fd; |
11484 | 0 | free(resolved_offsets); |
11485 | 0 | return link; |
11486 | | |
11487 | 0 | error: |
11488 | 0 | free(resolved_offsets); |
11489 | 0 | free(link); |
11490 | 0 | return libbpf_err_ptr(err); |
11491 | 0 | } |
11492 | | |
11493 | | LIBBPF_API struct bpf_link * |
11494 | | bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, |
11495 | | const char *binary_path, size_t func_offset, |
11496 | | const struct bpf_uprobe_opts *opts) |
11497 | 0 | { |
11498 | 0 | const char *archive_path = NULL, *archive_sep = NULL; |
11499 | 0 | char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL; |
11500 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
11501 | 0 | enum probe_attach_mode attach_mode; |
11502 | 0 | char full_path[PATH_MAX]; |
11503 | 0 | struct bpf_link *link; |
11504 | 0 | size_t ref_ctr_off; |
11505 | 0 | int pfd, err; |
11506 | 0 | bool retprobe, legacy; |
11507 | 0 | const char *func_name; |
11508 | |
|
11509 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
11510 | 0 | return libbpf_err_ptr(-EINVAL); |
11511 | | |
11512 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
11513 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
11514 | 0 | ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0); |
11515 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
11516 | |
|
11517 | 0 | if (!binary_path) |
11518 | 0 | return libbpf_err_ptr(-EINVAL); |
11519 | | |
11520 | | /* Check if "binary_path" refers to an archive. */ |
11521 | 0 | archive_sep = strstr(binary_path, "!/"); |
11522 | 0 | if (archive_sep) { |
11523 | 0 | full_path[0] = '\0'; |
11524 | 0 | libbpf_strlcpy(full_path, binary_path, |
11525 | 0 | min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); |
11526 | 0 | archive_path = full_path; |
11527 | 0 | binary_path = archive_sep + 2; |
11528 | 0 | } else if (!strchr(binary_path, '/')) { |
11529 | 0 | err = resolve_full_path(binary_path, full_path, sizeof(full_path)); |
11530 | 0 | if (err) { |
11531 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
11532 | 0 | prog->name, binary_path, err); |
11533 | 0 | return libbpf_err_ptr(err); |
11534 | 0 | } |
11535 | 0 | binary_path = full_path; |
11536 | 0 | } |
11537 | 0 | func_name = OPTS_GET(opts, func_name, NULL); |
11538 | 0 | if (func_name) { |
11539 | 0 | long sym_off; |
11540 | |
|
11541 | 0 | if (archive_path) { |
11542 | 0 | sym_off = elf_find_func_offset_from_archive(archive_path, binary_path, |
11543 | 0 | func_name); |
11544 | 0 | binary_path = archive_path; |
11545 | 0 | } else { |
11546 | 0 | sym_off = elf_find_func_offset_from_file(binary_path, func_name); |
11547 | 0 | } |
11548 | 0 | if (sym_off < 0) |
11549 | 0 | return libbpf_err_ptr(sym_off); |
11550 | 0 | func_offset += sym_off; |
11551 | 0 | } |
11552 | | |
11553 | 0 | legacy = determine_uprobe_perf_type() < 0; |
11554 | 0 | switch (attach_mode) { |
11555 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
11556 | 0 | legacy = true; |
11557 | 0 | pe_opts.force_ioctl_attach = true; |
11558 | 0 | break; |
11559 | 0 | case PROBE_ATTACH_MODE_PERF: |
11560 | 0 | if (legacy) |
11561 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11562 | 0 | pe_opts.force_ioctl_attach = true; |
11563 | 0 | break; |
11564 | 0 | case PROBE_ATTACH_MODE_LINK: |
11565 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
11566 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11567 | 0 | break; |
11568 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
11569 | 0 | break; |
11570 | 0 | default: |
11571 | 0 | return libbpf_err_ptr(-EINVAL); |
11572 | 0 | } |
11573 | | |
11574 | 0 | if (!legacy) { |
11575 | 0 | pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path, |
11576 | 0 | func_offset, pid, ref_ctr_off); |
11577 | 0 | } else { |
11578 | 0 | char probe_name[PATH_MAX + 64]; |
11579 | |
|
11580 | 0 | if (ref_ctr_off) |
11581 | 0 | return libbpf_err_ptr(-EINVAL); |
11582 | | |
11583 | 0 | gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name), |
11584 | 0 | binary_path, func_offset); |
11585 | |
|
11586 | 0 | legacy_probe = strdup(probe_name); |
11587 | 0 | if (!legacy_probe) |
11588 | 0 | return libbpf_err_ptr(-ENOMEM); |
11589 | | |
11590 | 0 | pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe, |
11591 | 0 | binary_path, func_offset, pid); |
11592 | 0 | } |
11593 | 0 | if (pfd < 0) { |
11594 | 0 | err = -errno; |
11595 | 0 | pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n", |
11596 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
11597 | 0 | binary_path, func_offset, |
11598 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11599 | 0 | goto err_out; |
11600 | 0 | } |
11601 | | |
11602 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
11603 | 0 | err = libbpf_get_error(link); |
11604 | 0 | if (err) { |
11605 | 0 | close(pfd); |
11606 | 0 | pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n", |
11607 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
11608 | 0 | binary_path, func_offset, |
11609 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11610 | 0 | goto err_clean_legacy; |
11611 | 0 | } |
11612 | 0 | if (legacy) { |
11613 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
11614 | |
|
11615 | 0 | perf_link->legacy_probe_name = legacy_probe; |
11616 | 0 | perf_link->legacy_is_kprobe = false; |
11617 | 0 | perf_link->legacy_is_retprobe = retprobe; |
11618 | 0 | } |
11619 | 0 | return link; |
11620 | | |
11621 | 0 | err_clean_legacy: |
11622 | 0 | if (legacy) |
11623 | 0 | remove_uprobe_event_legacy(legacy_probe, retprobe); |
11624 | 0 | err_out: |
11625 | 0 | free(legacy_probe); |
11626 | 0 | return libbpf_err_ptr(err); |
11627 | 0 | } |
11628 | | |
11629 | | /* Format of u[ret]probe section definition supporting auto-attach: |
11630 | | * u[ret]probe/binary:function[+offset] |
11631 | | * |
11632 | | * binary can be an absolute/relative path or a filename; the latter is resolved to a |
11633 | | * full binary path via bpf_program__attach_uprobe_opts. |
11634 | | * |
11635 | | * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be |
11636 | | * specified (and auto-attach is not possible) or the above format is specified for |
11637 | | * auto-attach. |
11638 | | */ |
11639 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11640 | 0 | { |
11641 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); |
11642 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off; |
11643 | 0 | int n, c, ret = -EINVAL; |
11644 | 0 | long offset = 0; |
11645 | |
|
11646 | 0 | *link = NULL; |
11647 | |
|
11648 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
11649 | 0 | &probe_type, &binary_path, &func_name); |
11650 | 0 | switch (n) { |
11651 | 0 | case 1: |
11652 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
11653 | 0 | ret = 0; |
11654 | 0 | break; |
11655 | 0 | case 2: |
11656 | 0 | pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n", |
11657 | 0 | prog->name, prog->sec_name); |
11658 | 0 | break; |
11659 | 0 | case 3: |
11660 | | /* check if user specifies `+offset`, if yes, this should be |
11661 | | * the last part of the string, make sure sscanf read to EOL |
11662 | | */ |
11663 | 0 | func_off = strrchr(func_name, '+'); |
11664 | 0 | if (func_off) { |
11665 | 0 | n = sscanf(func_off, "+%li%n", &offset, &c); |
11666 | 0 | if (n == 1 && *(func_off + c) == '\0') |
11667 | 0 | func_off[0] = '\0'; |
11668 | 0 | else |
11669 | 0 | offset = 0; |
11670 | 0 | } |
11671 | 0 | opts.retprobe = strcmp(probe_type, "uretprobe") == 0 || |
11672 | 0 | strcmp(probe_type, "uretprobe.s") == 0; |
11673 | 0 | if (opts.retprobe && offset != 0) { |
11674 | 0 | pr_warn("prog '%s': uretprobes do not support offset specification\n", |
11675 | 0 | prog->name); |
11676 | 0 | break; |
11677 | 0 | } |
11678 | 0 | opts.func_name = func_name; |
11679 | 0 | *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); |
11680 | 0 | ret = libbpf_get_error(*link); |
11681 | 0 | break; |
11682 | 0 | default: |
11683 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
11684 | 0 | prog->sec_name); |
11685 | 0 | break; |
11686 | 0 | } |
11687 | 0 | free(probe_type); |
11688 | 0 | free(binary_path); |
11689 | 0 | free(func_name); |
11690 | |
|
11691 | 0 | return ret; |
11692 | 0 | } |
11693 | | |
11694 | | struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, |
11695 | | bool retprobe, pid_t pid, |
11696 | | const char *binary_path, |
11697 | | size_t func_offset) |
11698 | 0 | { |
11699 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe); |
11700 | |
|
11701 | 0 | return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts); |
11702 | 0 | } |
11703 | | |
11704 | | struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog, |
11705 | | pid_t pid, const char *binary_path, |
11706 | | const char *usdt_provider, const char *usdt_name, |
11707 | | const struct bpf_usdt_opts *opts) |
11708 | 0 | { |
11709 | 0 | char resolved_path[512]; |
11710 | 0 | struct bpf_object *obj = prog->obj; |
11711 | 0 | struct bpf_link *link; |
11712 | 0 | __u64 usdt_cookie; |
11713 | 0 | int err; |
11714 | |
|
11715 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
11716 | 0 | return libbpf_err_ptr(-EINVAL); |
11717 | | |
11718 | 0 | if (bpf_program__fd(prog) < 0) { |
11719 | 0 | pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n", |
11720 | 0 | prog->name); |
11721 | 0 | return libbpf_err_ptr(-EINVAL); |
11722 | 0 | } |
11723 | | |
11724 | 0 | if (!binary_path) |
11725 | 0 | return libbpf_err_ptr(-EINVAL); |
11726 | | |
11727 | 0 | if (!strchr(binary_path, '/')) { |
11728 | 0 | err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path)); |
11729 | 0 | if (err) { |
11730 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %d\n", |
11731 | 0 | prog->name, binary_path, err); |
11732 | 0 | return libbpf_err_ptr(err); |
11733 | 0 | } |
11734 | 0 | binary_path = resolved_path; |
11735 | 0 | } |
11736 | | |
11737 | | /* USDT manager is instantiated lazily on first USDT attach. It will |
11738 | | * be destroyed together with BPF object in bpf_object__close(). |
11739 | | */ |
11740 | 0 | if (IS_ERR(obj->usdt_man)) |
11741 | 0 | return libbpf_ptr(obj->usdt_man); |
11742 | 0 | if (!obj->usdt_man) { |
11743 | 0 | obj->usdt_man = usdt_manager_new(obj); |
11744 | 0 | if (IS_ERR(obj->usdt_man)) |
11745 | 0 | return libbpf_ptr(obj->usdt_man); |
11746 | 0 | } |
11747 | | |
11748 | 0 | usdt_cookie = OPTS_GET(opts, usdt_cookie, 0); |
11749 | 0 | link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, |
11750 | 0 | usdt_provider, usdt_name, usdt_cookie); |
11751 | 0 | err = libbpf_get_error(link); |
11752 | 0 | if (err) |
11753 | 0 | return libbpf_err_ptr(err); |
11754 | 0 | return link; |
11755 | 0 | } |
11756 | | |
11757 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11758 | 0 | { |
11759 | 0 | char *path = NULL, *provider = NULL, *name = NULL; |
11760 | 0 | const char *sec_name; |
11761 | 0 | int n, err; |
11762 | |
|
11763 | 0 | sec_name = bpf_program__section_name(prog); |
11764 | 0 | if (strcmp(sec_name, "usdt") == 0) { |
11765 | | /* no auto-attach for just SEC("usdt") */ |
11766 | 0 | *link = NULL; |
11767 | 0 | return 0; |
11768 | 0 | } |
11769 | | |
11770 | 0 | n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name); |
11771 | 0 | if (n != 3) { |
11772 | 0 | pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n", |
11773 | 0 | sec_name); |
11774 | 0 | err = -EINVAL; |
11775 | 0 | } else { |
11776 | 0 | *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, |
11777 | 0 | provider, name, NULL); |
11778 | 0 | err = libbpf_get_error(*link); |
11779 | 0 | } |
11780 | 0 | free(path); |
11781 | 0 | free(provider); |
11782 | 0 | free(name); |
11783 | 0 | return err; |
11784 | 0 | } |
11785 | | |
11786 | | static int determine_tracepoint_id(const char *tp_category, |
11787 | | const char *tp_name) |
11788 | 0 | { |
11789 | 0 | char file[PATH_MAX]; |
11790 | 0 | int ret; |
11791 | |
|
11792 | 0 | ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
11793 | 0 | tracefs_path(), tp_category, tp_name); |
11794 | 0 | if (ret < 0) |
11795 | 0 | return -errno; |
11796 | 0 | if (ret >= sizeof(file)) { |
11797 | 0 | pr_debug("tracepoint %s/%s path is too long\n", |
11798 | 0 | tp_category, tp_name); |
11799 | 0 | return -E2BIG; |
11800 | 0 | } |
11801 | 0 | return parse_uint_from_file(file, "%d\n"); |
11802 | 0 | } |
11803 | | |
11804 | | static int perf_event_open_tracepoint(const char *tp_category, |
11805 | | const char *tp_name) |
11806 | 0 | { |
11807 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11808 | 0 | struct perf_event_attr attr; |
11809 | 0 | char errmsg[STRERR_BUFSIZE]; |
11810 | 0 | int tp_id, pfd, err; |
11811 | |
|
11812 | 0 | tp_id = determine_tracepoint_id(tp_category, tp_name); |
11813 | 0 | if (tp_id < 0) { |
11814 | 0 | pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n", |
11815 | 0 | tp_category, tp_name, |
11816 | 0 | libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg))); |
11817 | 0 | return tp_id; |
11818 | 0 | } |
11819 | | |
11820 | 0 | memset(&attr, 0, attr_sz); |
11821 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
11822 | 0 | attr.size = attr_sz; |
11823 | 0 | attr.config = tp_id; |
11824 | |
|
11825 | 0 | pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, |
11826 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11827 | 0 | if (pfd < 0) { |
11828 | 0 | err = -errno; |
11829 | 0 | pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n", |
11830 | 0 | tp_category, tp_name, |
11831 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11832 | 0 | return err; |
11833 | 0 | } |
11834 | 0 | return pfd; |
11835 | 0 | } |
11836 | | |
11837 | | struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, |
11838 | | const char *tp_category, |
11839 | | const char *tp_name, |
11840 | | const struct bpf_tracepoint_opts *opts) |
11841 | 0 | { |
11842 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
11843 | 0 | char errmsg[STRERR_BUFSIZE]; |
11844 | 0 | struct bpf_link *link; |
11845 | 0 | int pfd, err; |
11846 | |
|
11847 | 0 | if (!OPTS_VALID(opts, bpf_tracepoint_opts)) |
11848 | 0 | return libbpf_err_ptr(-EINVAL); |
11849 | | |
11850 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
11851 | |
|
11852 | 0 | pfd = perf_event_open_tracepoint(tp_category, tp_name); |
11853 | 0 | if (pfd < 0) { |
11854 | 0 | pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n", |
11855 | 0 | prog->name, tp_category, tp_name, |
11856 | 0 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
11857 | 0 | return libbpf_err_ptr(pfd); |
11858 | 0 | } |
11859 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
11860 | 0 | err = libbpf_get_error(link); |
11861 | 0 | if (err) { |
11862 | 0 | close(pfd); |
11863 | 0 | pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n", |
11864 | 0 | prog->name, tp_category, tp_name, |
11865 | 0 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
11866 | 0 | return libbpf_err_ptr(err); |
11867 | 0 | } |
11868 | 0 | return link; |
11869 | 0 | } |
11870 | | |
11871 | | struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog, |
11872 | | const char *tp_category, |
11873 | | const char *tp_name) |
11874 | 0 | { |
11875 | 0 | return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL); |
11876 | 0 | } |
11877 | | |
11878 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11879 | 0 | { |
11880 | 0 | char *sec_name, *tp_cat, *tp_name; |
11881 | |
|
11882 | 0 | *link = NULL; |
11883 | | |
11884 | | /* no auto-attach for SEC("tp") or SEC("tracepoint") */ |
11885 | 0 | if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0) |
11886 | 0 | return 0; |
11887 | | |
11888 | 0 | sec_name = strdup(prog->sec_name); |
11889 | 0 | if (!sec_name) |
11890 | 0 | return -ENOMEM; |
11891 | | |
11892 | | /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */ |
11893 | 0 | if (str_has_pfx(prog->sec_name, "tp/")) |
11894 | 0 | tp_cat = sec_name + sizeof("tp/") - 1; |
11895 | 0 | else |
11896 | 0 | tp_cat = sec_name + sizeof("tracepoint/") - 1; |
11897 | 0 | tp_name = strchr(tp_cat, '/'); |
11898 | 0 | if (!tp_name) { |
11899 | 0 | free(sec_name); |
11900 | 0 | return -EINVAL; |
11901 | 0 | } |
11902 | 0 | *tp_name = '\0'; |
11903 | 0 | tp_name++; |
11904 | |
|
11905 | 0 | *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name); |
11906 | 0 | free(sec_name); |
11907 | 0 | return libbpf_get_error(*link); |
11908 | 0 | } |
11909 | | |
11910 | | struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, |
11911 | | const char *tp_name) |
11912 | 0 | { |
11913 | 0 | char errmsg[STRERR_BUFSIZE]; |
11914 | 0 | struct bpf_link *link; |
11915 | 0 | int prog_fd, pfd; |
11916 | |
|
11917 | 0 | prog_fd = bpf_program__fd(prog); |
11918 | 0 | if (prog_fd < 0) { |
11919 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
11920 | 0 | return libbpf_err_ptr(-EINVAL); |
11921 | 0 | } |
11922 | | |
11923 | 0 | link = calloc(1, sizeof(*link)); |
11924 | 0 | if (!link) |
11925 | 0 | return libbpf_err_ptr(-ENOMEM); |
11926 | 0 | link->detach = &bpf_link__detach_fd; |
11927 | |
|
11928 | 0 | pfd = bpf_raw_tracepoint_open(tp_name, prog_fd); |
11929 | 0 | if (pfd < 0) { |
11930 | 0 | pfd = -errno; |
11931 | 0 | free(link); |
11932 | 0 | pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n", |
11933 | 0 | prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
11934 | 0 | return libbpf_err_ptr(pfd); |
11935 | 0 | } |
11936 | 0 | link->fd = pfd; |
11937 | 0 | return link; |
11938 | 0 | } |
11939 | | |
11940 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
11941 | 0 | { |
11942 | 0 | static const char *const prefixes[] = { |
11943 | 0 | "raw_tp", |
11944 | 0 | "raw_tracepoint", |
11945 | 0 | "raw_tp.w", |
11946 | 0 | "raw_tracepoint.w", |
11947 | 0 | }; |
11948 | 0 | size_t i; |
11949 | 0 | const char *tp_name = NULL; |
11950 | |
|
11951 | 0 | *link = NULL; |
11952 | |
|
11953 | 0 | for (i = 0; i < ARRAY_SIZE(prefixes); i++) { |
11954 | 0 | size_t pfx_len; |
11955 | |
|
11956 | 0 | if (!str_has_pfx(prog->sec_name, prefixes[i])) |
11957 | 0 | continue; |
11958 | | |
11959 | 0 | pfx_len = strlen(prefixes[i]); |
11960 | | /* no auto-attach case of, e.g., SEC("raw_tp") */ |
11961 | 0 | if (prog->sec_name[pfx_len] == '\0') |
11962 | 0 | return 0; |
11963 | | |
11964 | 0 | if (prog->sec_name[pfx_len] != '/') |
11965 | 0 | continue; |
11966 | | |
11967 | 0 | tp_name = prog->sec_name + pfx_len + 1; |
11968 | 0 | break; |
11969 | 0 | } |
11970 | | |
11971 | 0 | if (!tp_name) { |
11972 | 0 | pr_warn("prog '%s': invalid section name '%s'\n", |
11973 | 0 | prog->name, prog->sec_name); |
11974 | 0 | return -EINVAL; |
11975 | 0 | } |
11976 | | |
11977 | 0 | *link = bpf_program__attach_raw_tracepoint(prog, tp_name); |
11978 | 0 | return libbpf_get_error(*link); |
11979 | 0 | } |
11980 | | |
11981 | | /* Common logic for all BPF program types that attach to a btf_id */ |
11982 | | static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog, |
11983 | | const struct bpf_trace_opts *opts) |
11984 | 0 | { |
11985 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_opts); |
11986 | 0 | char errmsg[STRERR_BUFSIZE]; |
11987 | 0 | struct bpf_link *link; |
11988 | 0 | int prog_fd, pfd; |
11989 | |
|
11990 | 0 | if (!OPTS_VALID(opts, bpf_trace_opts)) |
11991 | 0 | return libbpf_err_ptr(-EINVAL); |
11992 | | |
11993 | 0 | prog_fd = bpf_program__fd(prog); |
11994 | 0 | if (prog_fd < 0) { |
11995 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
11996 | 0 | return libbpf_err_ptr(-EINVAL); |
11997 | 0 | } |
11998 | | |
11999 | 0 | link = calloc(1, sizeof(*link)); |
12000 | 0 | if (!link) |
12001 | 0 | return libbpf_err_ptr(-ENOMEM); |
12002 | 0 | link->detach = &bpf_link__detach_fd; |
12003 | | |
12004 | | /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */ |
12005 | 0 | link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0); |
12006 | 0 | pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts); |
12007 | 0 | if (pfd < 0) { |
12008 | 0 | pfd = -errno; |
12009 | 0 | free(link); |
12010 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
12011 | 0 | prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
12012 | 0 | return libbpf_err_ptr(pfd); |
12013 | 0 | } |
12014 | 0 | link->fd = pfd; |
12015 | 0 | return link; |
12016 | 0 | } |
12017 | | |
12018 | | struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog) |
12019 | 0 | { |
12020 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
12021 | 0 | } |
12022 | | |
12023 | | struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog, |
12024 | | const struct bpf_trace_opts *opts) |
12025 | 0 | { |
12026 | 0 | return bpf_program__attach_btf_id(prog, opts); |
12027 | 0 | } |
12028 | | |
12029 | | struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog) |
12030 | 0 | { |
12031 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
12032 | 0 | } |
12033 | | |
12034 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12035 | 0 | { |
12036 | 0 | *link = bpf_program__attach_trace(prog); |
12037 | 0 | return libbpf_get_error(*link); |
12038 | 0 | } |
12039 | | |
12040 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12041 | 0 | { |
12042 | 0 | *link = bpf_program__attach_lsm(prog); |
12043 | 0 | return libbpf_get_error(*link); |
12044 | 0 | } |
12045 | | |
12046 | | static struct bpf_link * |
12047 | | bpf_program_attach_fd(const struct bpf_program *prog, |
12048 | | int target_fd, const char *target_name, |
12049 | | const struct bpf_link_create_opts *opts) |
12050 | 0 | { |
12051 | 0 | enum bpf_attach_type attach_type; |
12052 | 0 | char errmsg[STRERR_BUFSIZE]; |
12053 | 0 | struct bpf_link *link; |
12054 | 0 | int prog_fd, link_fd; |
12055 | |
|
12056 | 0 | prog_fd = bpf_program__fd(prog); |
12057 | 0 | if (prog_fd < 0) { |
12058 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12059 | 0 | return libbpf_err_ptr(-EINVAL); |
12060 | 0 | } |
12061 | | |
12062 | 0 | link = calloc(1, sizeof(*link)); |
12063 | 0 | if (!link) |
12064 | 0 | return libbpf_err_ptr(-ENOMEM); |
12065 | 0 | link->detach = &bpf_link__detach_fd; |
12066 | |
|
12067 | 0 | attach_type = bpf_program__expected_attach_type(prog); |
12068 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts); |
12069 | 0 | if (link_fd < 0) { |
12070 | 0 | link_fd = -errno; |
12071 | 0 | free(link); |
12072 | 0 | pr_warn("prog '%s': failed to attach to %s: %s\n", |
12073 | 0 | prog->name, target_name, |
12074 | 0 | libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12075 | 0 | return libbpf_err_ptr(link_fd); |
12076 | 0 | } |
12077 | 0 | link->fd = link_fd; |
12078 | 0 | return link; |
12079 | 0 | } |
12080 | | |
12081 | | struct bpf_link * |
12082 | | bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd) |
12083 | 0 | { |
12084 | 0 | return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL); |
12085 | 0 | } |
12086 | | |
12087 | | struct bpf_link * |
12088 | | bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd) |
12089 | 0 | { |
12090 | 0 | return bpf_program_attach_fd(prog, netns_fd, "netns", NULL); |
12091 | 0 | } |
12092 | | |
12093 | | struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex) |
12094 | 0 | { |
12095 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
12096 | 0 | return bpf_program_attach_fd(prog, ifindex, "xdp", NULL); |
12097 | 0 | } |
12098 | | |
12099 | | struct bpf_link * |
12100 | | bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, |
12101 | | const struct bpf_tcx_opts *opts) |
12102 | 0 | { |
12103 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12104 | 0 | __u32 relative_id; |
12105 | 0 | int relative_fd; |
12106 | |
|
12107 | 0 | if (!OPTS_VALID(opts, bpf_tcx_opts)) |
12108 | 0 | return libbpf_err_ptr(-EINVAL); |
12109 | | |
12110 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
12111 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
12112 | | |
12113 | | /* validate we don't have unexpected combinations of non-zero fields */ |
12114 | 0 | if (!ifindex) { |
12115 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
12116 | 0 | prog->name); |
12117 | 0 | return libbpf_err_ptr(-EINVAL); |
12118 | 0 | } |
12119 | 0 | if (relative_fd && relative_id) { |
12120 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
12121 | 0 | prog->name); |
12122 | 0 | return libbpf_err_ptr(-EINVAL); |
12123 | 0 | } |
12124 | | |
12125 | 0 | link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0); |
12126 | 0 | link_create_opts.tcx.relative_fd = relative_fd; |
12127 | 0 | link_create_opts.tcx.relative_id = relative_id; |
12128 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
12129 | | |
12130 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
12131 | 0 | return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts); |
12132 | 0 | } |
12133 | | |
12134 | | struct bpf_link * |
12135 | | bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, |
12136 | | const struct bpf_netkit_opts *opts) |
12137 | 0 | { |
12138 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12139 | 0 | __u32 relative_id; |
12140 | 0 | int relative_fd; |
12141 | |
|
12142 | 0 | if (!OPTS_VALID(opts, bpf_netkit_opts)) |
12143 | 0 | return libbpf_err_ptr(-EINVAL); |
12144 | | |
12145 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
12146 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
12147 | | |
12148 | | /* validate we don't have unexpected combinations of non-zero fields */ |
12149 | 0 | if (!ifindex) { |
12150 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
12151 | 0 | prog->name); |
12152 | 0 | return libbpf_err_ptr(-EINVAL); |
12153 | 0 | } |
12154 | 0 | if (relative_fd && relative_id) { |
12155 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
12156 | 0 | prog->name); |
12157 | 0 | return libbpf_err_ptr(-EINVAL); |
12158 | 0 | } |
12159 | | |
12160 | 0 | link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0); |
12161 | 0 | link_create_opts.netkit.relative_fd = relative_fd; |
12162 | 0 | link_create_opts.netkit.relative_id = relative_id; |
12163 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
12164 | |
|
12165 | 0 | return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts); |
12166 | 0 | } |
12167 | | |
12168 | | struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog, |
12169 | | int target_fd, |
12170 | | const char *attach_func_name) |
12171 | 0 | { |
12172 | 0 | int btf_id; |
12173 | |
|
12174 | 0 | if (!!target_fd != !!attach_func_name) { |
12175 | 0 | pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n", |
12176 | 0 | prog->name); |
12177 | 0 | return libbpf_err_ptr(-EINVAL); |
12178 | 0 | } |
12179 | | |
12180 | 0 | if (prog->type != BPF_PROG_TYPE_EXT) { |
12181 | 0 | pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace", |
12182 | 0 | prog->name); |
12183 | 0 | return libbpf_err_ptr(-EINVAL); |
12184 | 0 | } |
12185 | | |
12186 | 0 | if (target_fd) { |
12187 | 0 | LIBBPF_OPTS(bpf_link_create_opts, target_opts); |
12188 | |
|
12189 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd); |
12190 | 0 | if (btf_id < 0) |
12191 | 0 | return libbpf_err_ptr(btf_id); |
12192 | | |
12193 | 0 | target_opts.target_btf_id = btf_id; |
12194 | |
|
12195 | 0 | return bpf_program_attach_fd(prog, target_fd, "freplace", |
12196 | 0 | &target_opts); |
12197 | 0 | } else { |
12198 | | /* no target, so use raw_tracepoint_open for compatibility |
12199 | | * with old kernels |
12200 | | */ |
12201 | 0 | return bpf_program__attach_trace(prog); |
12202 | 0 | } |
12203 | 0 | } |
12204 | | |
12205 | | struct bpf_link * |
12206 | | bpf_program__attach_iter(const struct bpf_program *prog, |
12207 | | const struct bpf_iter_attach_opts *opts) |
12208 | 0 | { |
12209 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
12210 | 0 | char errmsg[STRERR_BUFSIZE]; |
12211 | 0 | struct bpf_link *link; |
12212 | 0 | int prog_fd, link_fd; |
12213 | 0 | __u32 target_fd = 0; |
12214 | |
|
12215 | 0 | if (!OPTS_VALID(opts, bpf_iter_attach_opts)) |
12216 | 0 | return libbpf_err_ptr(-EINVAL); |
12217 | | |
12218 | 0 | link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0); |
12219 | 0 | link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0); |
12220 | |
|
12221 | 0 | prog_fd = bpf_program__fd(prog); |
12222 | 0 | if (prog_fd < 0) { |
12223 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12224 | 0 | return libbpf_err_ptr(-EINVAL); |
12225 | 0 | } |
12226 | | |
12227 | 0 | link = calloc(1, sizeof(*link)); |
12228 | 0 | if (!link) |
12229 | 0 | return libbpf_err_ptr(-ENOMEM); |
12230 | 0 | link->detach = &bpf_link__detach_fd; |
12231 | |
|
12232 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, |
12233 | 0 | &link_create_opts); |
12234 | 0 | if (link_fd < 0) { |
12235 | 0 | link_fd = -errno; |
12236 | 0 | free(link); |
12237 | 0 | pr_warn("prog '%s': failed to attach to iterator: %s\n", |
12238 | 0 | prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12239 | 0 | return libbpf_err_ptr(link_fd); |
12240 | 0 | } |
12241 | 0 | link->fd = link_fd; |
12242 | 0 | return link; |
12243 | 0 | } |
12244 | | |
12245 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12246 | 0 | { |
12247 | 0 | *link = bpf_program__attach_iter(prog, NULL); |
12248 | 0 | return libbpf_get_error(*link); |
12249 | 0 | } |
12250 | | |
12251 | | struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog, |
12252 | | const struct bpf_netfilter_opts *opts) |
12253 | 0 | { |
12254 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
12255 | 0 | struct bpf_link *link; |
12256 | 0 | int prog_fd, link_fd; |
12257 | |
|
12258 | 0 | if (!OPTS_VALID(opts, bpf_netfilter_opts)) |
12259 | 0 | return libbpf_err_ptr(-EINVAL); |
12260 | | |
12261 | 0 | prog_fd = bpf_program__fd(prog); |
12262 | 0 | if (prog_fd < 0) { |
12263 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
12264 | 0 | return libbpf_err_ptr(-EINVAL); |
12265 | 0 | } |
12266 | | |
12267 | 0 | link = calloc(1, sizeof(*link)); |
12268 | 0 | if (!link) |
12269 | 0 | return libbpf_err_ptr(-ENOMEM); |
12270 | | |
12271 | 0 | link->detach = &bpf_link__detach_fd; |
12272 | |
|
12273 | 0 | lopts.netfilter.pf = OPTS_GET(opts, pf, 0); |
12274 | 0 | lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0); |
12275 | 0 | lopts.netfilter.priority = OPTS_GET(opts, priority, 0); |
12276 | 0 | lopts.netfilter.flags = OPTS_GET(opts, flags, 0); |
12277 | |
|
12278 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts); |
12279 | 0 | if (link_fd < 0) { |
12280 | 0 | char errmsg[STRERR_BUFSIZE]; |
12281 | |
|
12282 | 0 | link_fd = -errno; |
12283 | 0 | free(link); |
12284 | 0 | pr_warn("prog '%s': failed to attach to netfilter: %s\n", |
12285 | 0 | prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); |
12286 | 0 | return libbpf_err_ptr(link_fd); |
12287 | 0 | } |
12288 | 0 | link->fd = link_fd; |
12289 | |
|
12290 | 0 | return link; |
12291 | 0 | } |
12292 | | |
12293 | | struct bpf_link *bpf_program__attach(const struct bpf_program *prog) |
12294 | 0 | { |
12295 | 0 | struct bpf_link *link = NULL; |
12296 | 0 | int err; |
12297 | |
|
12298 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
12299 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
12300 | | |
12301 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); |
12302 | 0 | if (err) |
12303 | 0 | return libbpf_err_ptr(err); |
12304 | | |
12305 | | /* When calling bpf_program__attach() explicitly, auto-attach support |
12306 | | * is expected to work, so NULL returned link is considered an error. |
12307 | | * This is different for skeleton's attach, see comment in |
12308 | | * bpf_object__attach_skeleton(). |
12309 | | */ |
12310 | 0 | if (!link) |
12311 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
12312 | | |
12313 | 0 | return link; |
12314 | 0 | } |
12315 | | |
12316 | | struct bpf_link_struct_ops { |
12317 | | struct bpf_link link; |
12318 | | int map_fd; |
12319 | | }; |
12320 | | |
12321 | | static int bpf_link__detach_struct_ops(struct bpf_link *link) |
12322 | 0 | { |
12323 | 0 | struct bpf_link_struct_ops *st_link; |
12324 | 0 | __u32 zero = 0; |
12325 | |
|
12326 | 0 | st_link = container_of(link, struct bpf_link_struct_ops, link); |
12327 | |
|
12328 | 0 | if (st_link->map_fd < 0) |
12329 | | /* w/o a real link */ |
12330 | 0 | return bpf_map_delete_elem(link->fd, &zero); |
12331 | | |
12332 | 0 | return close(link->fd); |
12333 | 0 | } |
12334 | | |
12335 | | struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map) |
12336 | 0 | { |
12337 | 0 | struct bpf_link_struct_ops *link; |
12338 | 0 | __u32 zero = 0; |
12339 | 0 | int err, fd; |
12340 | |
|
12341 | 0 | if (!bpf_map__is_struct_ops(map) || map->fd == -1) |
12342 | 0 | return libbpf_err_ptr(-EINVAL); |
12343 | | |
12344 | 0 | link = calloc(1, sizeof(*link)); |
12345 | 0 | if (!link) |
12346 | 0 | return libbpf_err_ptr(-EINVAL); |
12347 | | |
12348 | | /* kern_vdata should be prepared during the loading phase. */ |
12349 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
12350 | | /* It can be EBUSY if the map has been used to create or |
12351 | | * update a link before. We don't allow updating the value of |
12352 | | * a struct_ops once it is set. That ensures that the value |
12353 | | * never changed. So, it is safe to skip EBUSY. |
12354 | | */ |
12355 | 0 | if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { |
12356 | 0 | free(link); |
12357 | 0 | return libbpf_err_ptr(err); |
12358 | 0 | } |
12359 | | |
12360 | 0 | link->link.detach = bpf_link__detach_struct_ops; |
12361 | |
|
12362 | 0 | if (!(map->def.map_flags & BPF_F_LINK)) { |
12363 | | /* w/o a real link */ |
12364 | 0 | link->link.fd = map->fd; |
12365 | 0 | link->map_fd = -1; |
12366 | 0 | return &link->link; |
12367 | 0 | } |
12368 | | |
12369 | 0 | fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); |
12370 | 0 | if (fd < 0) { |
12371 | 0 | free(link); |
12372 | 0 | return libbpf_err_ptr(fd); |
12373 | 0 | } |
12374 | | |
12375 | 0 | link->link.fd = fd; |
12376 | 0 | link->map_fd = map->fd; |
12377 | |
|
12378 | 0 | return &link->link; |
12379 | 0 | } |
12380 | | |
12381 | | /* |
12382 | | * Swap the back struct_ops of a link with a new struct_ops map. |
12383 | | */ |
12384 | | int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map) |
12385 | 0 | { |
12386 | 0 | struct bpf_link_struct_ops *st_ops_link; |
12387 | 0 | __u32 zero = 0; |
12388 | 0 | int err; |
12389 | |
|
12390 | 0 | if (!bpf_map__is_struct_ops(map) || map->fd < 0) |
12391 | 0 | return -EINVAL; |
12392 | | |
12393 | 0 | st_ops_link = container_of(link, struct bpf_link_struct_ops, link); |
12394 | | /* Ensure the type of a link is correct */ |
12395 | 0 | if (st_ops_link->map_fd < 0) |
12396 | 0 | return -EINVAL; |
12397 | | |
12398 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
12399 | | /* It can be EBUSY if the map has been used to create or |
12400 | | * update a link before. We don't allow updating the value of |
12401 | | * a struct_ops once it is set. That ensures that the value |
12402 | | * never changed. So, it is safe to skip EBUSY. |
12403 | | */ |
12404 | 0 | if (err && err != -EBUSY) |
12405 | 0 | return err; |
12406 | | |
12407 | 0 | err = bpf_link_update(link->fd, map->fd, NULL); |
12408 | 0 | if (err < 0) |
12409 | 0 | return err; |
12410 | | |
12411 | 0 | st_ops_link->map_fd = map->fd; |
12412 | |
|
12413 | 0 | return 0; |
12414 | 0 | } |
12415 | | |
12416 | | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr, |
12417 | | void *private_data); |
12418 | | |
12419 | | static enum bpf_perf_event_ret |
12420 | | perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
12421 | | void **copy_mem, size_t *copy_size, |
12422 | | bpf_perf_event_print_t fn, void *private_data) |
12423 | 0 | { |
12424 | 0 | struct perf_event_mmap_page *header = mmap_mem; |
12425 | 0 | __u64 data_head = ring_buffer_read_head(header); |
12426 | 0 | __u64 data_tail = header->data_tail; |
12427 | 0 | void *base = ((__u8 *)header) + page_size; |
12428 | 0 | int ret = LIBBPF_PERF_EVENT_CONT; |
12429 | 0 | struct perf_event_header *ehdr; |
12430 | 0 | size_t ehdr_size; |
12431 | |
|
12432 | 0 | while (data_head != data_tail) { |
12433 | 0 | ehdr = base + (data_tail & (mmap_size - 1)); |
12434 | 0 | ehdr_size = ehdr->size; |
12435 | |
|
12436 | 0 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { |
12437 | 0 | void *copy_start = ehdr; |
12438 | 0 | size_t len_first = base + mmap_size - copy_start; |
12439 | 0 | size_t len_secnd = ehdr_size - len_first; |
12440 | |
|
12441 | 0 | if (*copy_size < ehdr_size) { |
12442 | 0 | free(*copy_mem); |
12443 | 0 | *copy_mem = malloc(ehdr_size); |
12444 | 0 | if (!*copy_mem) { |
12445 | 0 | *copy_size = 0; |
12446 | 0 | ret = LIBBPF_PERF_EVENT_ERROR; |
12447 | 0 | break; |
12448 | 0 | } |
12449 | 0 | *copy_size = ehdr_size; |
12450 | 0 | } |
12451 | | |
12452 | 0 | memcpy(*copy_mem, copy_start, len_first); |
12453 | 0 | memcpy(*copy_mem + len_first, base, len_secnd); |
12454 | 0 | ehdr = *copy_mem; |
12455 | 0 | } |
12456 | | |
12457 | 0 | ret = fn(ehdr, private_data); |
12458 | 0 | data_tail += ehdr_size; |
12459 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
12460 | 0 | break; |
12461 | 0 | } |
12462 | |
|
12463 | 0 | ring_buffer_write_tail(header, data_tail); |
12464 | 0 | return libbpf_err(ret); |
12465 | 0 | } |
12466 | | |
12467 | | struct perf_buffer; |
12468 | | |
12469 | | struct perf_buffer_params { |
12470 | | struct perf_event_attr *attr; |
12471 | | /* if event_cb is specified, it takes precendence */ |
12472 | | perf_buffer_event_fn event_cb; |
12473 | | /* sample_cb and lost_cb are higher-level common-case callbacks */ |
12474 | | perf_buffer_sample_fn sample_cb; |
12475 | | perf_buffer_lost_fn lost_cb; |
12476 | | void *ctx; |
12477 | | int cpu_cnt; |
12478 | | int *cpus; |
12479 | | int *map_keys; |
12480 | | }; |
12481 | | |
12482 | | struct perf_cpu_buf { |
12483 | | struct perf_buffer *pb; |
12484 | | void *base; /* mmap()'ed memory */ |
12485 | | void *buf; /* for reconstructing segmented data */ |
12486 | | size_t buf_size; |
12487 | | int fd; |
12488 | | int cpu; |
12489 | | int map_key; |
12490 | | }; |
12491 | | |
12492 | | struct perf_buffer { |
12493 | | perf_buffer_event_fn event_cb; |
12494 | | perf_buffer_sample_fn sample_cb; |
12495 | | perf_buffer_lost_fn lost_cb; |
12496 | | void *ctx; /* passed into callbacks */ |
12497 | | |
12498 | | size_t page_size; |
12499 | | size_t mmap_size; |
12500 | | struct perf_cpu_buf **cpu_bufs; |
12501 | | struct epoll_event *events; |
12502 | | int cpu_cnt; /* number of allocated CPU buffers */ |
12503 | | int epoll_fd; /* perf event FD */ |
12504 | | int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ |
12505 | | }; |
12506 | | |
12507 | | static void perf_buffer__free_cpu_buf(struct perf_buffer *pb, |
12508 | | struct perf_cpu_buf *cpu_buf) |
12509 | 0 | { |
12510 | 0 | if (!cpu_buf) |
12511 | 0 | return; |
12512 | 0 | if (cpu_buf->base && |
12513 | 0 | munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) |
12514 | 0 | pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); |
12515 | 0 | if (cpu_buf->fd >= 0) { |
12516 | 0 | ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); |
12517 | 0 | close(cpu_buf->fd); |
12518 | 0 | } |
12519 | 0 | free(cpu_buf->buf); |
12520 | 0 | free(cpu_buf); |
12521 | 0 | } |
12522 | | |
12523 | | void perf_buffer__free(struct perf_buffer *pb) |
12524 | 0 | { |
12525 | 0 | int i; |
12526 | |
|
12527 | 0 | if (IS_ERR_OR_NULL(pb)) |
12528 | 0 | return; |
12529 | 0 | if (pb->cpu_bufs) { |
12530 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
12531 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
12532 | |
|
12533 | 0 | if (!cpu_buf) |
12534 | 0 | continue; |
12535 | | |
12536 | 0 | bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); |
12537 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
12538 | 0 | } |
12539 | 0 | free(pb->cpu_bufs); |
12540 | 0 | } |
12541 | 0 | if (pb->epoll_fd >= 0) |
12542 | 0 | close(pb->epoll_fd); |
12543 | 0 | free(pb->events); |
12544 | 0 | free(pb); |
12545 | 0 | } |
12546 | | |
12547 | | static struct perf_cpu_buf * |
12548 | | perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, |
12549 | | int cpu, int map_key) |
12550 | 0 | { |
12551 | 0 | struct perf_cpu_buf *cpu_buf; |
12552 | 0 | char msg[STRERR_BUFSIZE]; |
12553 | 0 | int err; |
12554 | |
|
12555 | 0 | cpu_buf = calloc(1, sizeof(*cpu_buf)); |
12556 | 0 | if (!cpu_buf) |
12557 | 0 | return ERR_PTR(-ENOMEM); |
12558 | | |
12559 | 0 | cpu_buf->pb = pb; |
12560 | 0 | cpu_buf->cpu = cpu; |
12561 | 0 | cpu_buf->map_key = map_key; |
12562 | |
|
12563 | 0 | cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, |
12564 | 0 | -1, PERF_FLAG_FD_CLOEXEC); |
12565 | 0 | if (cpu_buf->fd < 0) { |
12566 | 0 | err = -errno; |
12567 | 0 | pr_warn("failed to open perf buffer event on cpu #%d: %s\n", |
12568 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
12569 | 0 | goto error; |
12570 | 0 | } |
12571 | | |
12572 | 0 | cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, |
12573 | 0 | PROT_READ | PROT_WRITE, MAP_SHARED, |
12574 | 0 | cpu_buf->fd, 0); |
12575 | 0 | if (cpu_buf->base == MAP_FAILED) { |
12576 | 0 | cpu_buf->base = NULL; |
12577 | 0 | err = -errno; |
12578 | 0 | pr_warn("failed to mmap perf buffer on cpu #%d: %s\n", |
12579 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
12580 | 0 | goto error; |
12581 | 0 | } |
12582 | | |
12583 | 0 | if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
12584 | 0 | err = -errno; |
12585 | 0 | pr_warn("failed to enable perf buffer event on cpu #%d: %s\n", |
12586 | 0 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
12587 | 0 | goto error; |
12588 | 0 | } |
12589 | | |
12590 | 0 | return cpu_buf; |
12591 | | |
12592 | 0 | error: |
12593 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
12594 | 0 | return (struct perf_cpu_buf *)ERR_PTR(err); |
12595 | 0 | } |
12596 | | |
12597 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
12598 | | struct perf_buffer_params *p); |
12599 | | |
12600 | | struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, |
12601 | | perf_buffer_sample_fn sample_cb, |
12602 | | perf_buffer_lost_fn lost_cb, |
12603 | | void *ctx, |
12604 | | const struct perf_buffer_opts *opts) |
12605 | 0 | { |
12606 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
12607 | 0 | struct perf_buffer_params p = {}; |
12608 | 0 | struct perf_event_attr attr; |
12609 | 0 | __u32 sample_period; |
12610 | |
|
12611 | 0 | if (!OPTS_VALID(opts, perf_buffer_opts)) |
12612 | 0 | return libbpf_err_ptr(-EINVAL); |
12613 | | |
12614 | 0 | sample_period = OPTS_GET(opts, sample_period, 1); |
12615 | 0 | if (!sample_period) |
12616 | 0 | sample_period = 1; |
12617 | |
|
12618 | 0 | memset(&attr, 0, attr_sz); |
12619 | 0 | attr.size = attr_sz; |
12620 | 0 | attr.config = PERF_COUNT_SW_BPF_OUTPUT; |
12621 | 0 | attr.type = PERF_TYPE_SOFTWARE; |
12622 | 0 | attr.sample_type = PERF_SAMPLE_RAW; |
12623 | 0 | attr.sample_period = sample_period; |
12624 | 0 | attr.wakeup_events = sample_period; |
12625 | |
|
12626 | 0 | p.attr = &attr; |
12627 | 0 | p.sample_cb = sample_cb; |
12628 | 0 | p.lost_cb = lost_cb; |
12629 | 0 | p.ctx = ctx; |
12630 | |
|
12631 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
12632 | 0 | } |
12633 | | |
12634 | | struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt, |
12635 | | struct perf_event_attr *attr, |
12636 | | perf_buffer_event_fn event_cb, void *ctx, |
12637 | | const struct perf_buffer_raw_opts *opts) |
12638 | 0 | { |
12639 | 0 | struct perf_buffer_params p = {}; |
12640 | |
|
12641 | 0 | if (!attr) |
12642 | 0 | return libbpf_err_ptr(-EINVAL); |
12643 | | |
12644 | 0 | if (!OPTS_VALID(opts, perf_buffer_raw_opts)) |
12645 | 0 | return libbpf_err_ptr(-EINVAL); |
12646 | | |
12647 | 0 | p.attr = attr; |
12648 | 0 | p.event_cb = event_cb; |
12649 | 0 | p.ctx = ctx; |
12650 | 0 | p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0); |
12651 | 0 | p.cpus = OPTS_GET(opts, cpus, NULL); |
12652 | 0 | p.map_keys = OPTS_GET(opts, map_keys, NULL); |
12653 | |
|
12654 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
12655 | 0 | } |
12656 | | |
12657 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
12658 | | struct perf_buffer_params *p) |
12659 | 0 | { |
12660 | 0 | const char *online_cpus_file = "/sys/devices/system/cpu/online"; |
12661 | 0 | struct bpf_map_info map; |
12662 | 0 | char msg[STRERR_BUFSIZE]; |
12663 | 0 | struct perf_buffer *pb; |
12664 | 0 | bool *online = NULL; |
12665 | 0 | __u32 map_info_len; |
12666 | 0 | int err, i, j, n; |
12667 | |
|
12668 | 0 | if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { |
12669 | 0 | pr_warn("page count should be power of two, but is %zu\n", |
12670 | 0 | page_cnt); |
12671 | 0 | return ERR_PTR(-EINVAL); |
12672 | 0 | } |
12673 | | |
12674 | | /* best-effort sanity checks */ |
12675 | 0 | memset(&map, 0, sizeof(map)); |
12676 | 0 | map_info_len = sizeof(map); |
12677 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len); |
12678 | 0 | if (err) { |
12679 | 0 | err = -errno; |
12680 | | /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return |
12681 | | * -EBADFD, -EFAULT, or -E2BIG on real error |
12682 | | */ |
12683 | 0 | if (err != -EINVAL) { |
12684 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", |
12685 | 0 | map_fd, libbpf_strerror_r(err, msg, sizeof(msg))); |
12686 | 0 | return ERR_PTR(err); |
12687 | 0 | } |
12688 | 0 | pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n", |
12689 | 0 | map_fd); |
12690 | 0 | } else { |
12691 | 0 | if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
12692 | 0 | pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
12693 | 0 | map.name); |
12694 | 0 | return ERR_PTR(-EINVAL); |
12695 | 0 | } |
12696 | 0 | } |
12697 | | |
12698 | 0 | pb = calloc(1, sizeof(*pb)); |
12699 | 0 | if (!pb) |
12700 | 0 | return ERR_PTR(-ENOMEM); |
12701 | | |
12702 | 0 | pb->event_cb = p->event_cb; |
12703 | 0 | pb->sample_cb = p->sample_cb; |
12704 | 0 | pb->lost_cb = p->lost_cb; |
12705 | 0 | pb->ctx = p->ctx; |
12706 | |
|
12707 | 0 | pb->page_size = getpagesize(); |
12708 | 0 | pb->mmap_size = pb->page_size * page_cnt; |
12709 | 0 | pb->map_fd = map_fd; |
12710 | |
|
12711 | 0 | pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
12712 | 0 | if (pb->epoll_fd < 0) { |
12713 | 0 | err = -errno; |
12714 | 0 | pr_warn("failed to create epoll instance: %s\n", |
12715 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
12716 | 0 | goto error; |
12717 | 0 | } |
12718 | | |
12719 | 0 | if (p->cpu_cnt > 0) { |
12720 | 0 | pb->cpu_cnt = p->cpu_cnt; |
12721 | 0 | } else { |
12722 | 0 | pb->cpu_cnt = libbpf_num_possible_cpus(); |
12723 | 0 | if (pb->cpu_cnt < 0) { |
12724 | 0 | err = pb->cpu_cnt; |
12725 | 0 | goto error; |
12726 | 0 | } |
12727 | 0 | if (map.max_entries && map.max_entries < pb->cpu_cnt) |
12728 | 0 | pb->cpu_cnt = map.max_entries; |
12729 | 0 | } |
12730 | | |
12731 | 0 | pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); |
12732 | 0 | if (!pb->events) { |
12733 | 0 | err = -ENOMEM; |
12734 | 0 | pr_warn("failed to allocate events: out of memory\n"); |
12735 | 0 | goto error; |
12736 | 0 | } |
12737 | 0 | pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); |
12738 | 0 | if (!pb->cpu_bufs) { |
12739 | 0 | err = -ENOMEM; |
12740 | 0 | pr_warn("failed to allocate buffers: out of memory\n"); |
12741 | 0 | goto error; |
12742 | 0 | } |
12743 | | |
12744 | 0 | err = parse_cpu_mask_file(online_cpus_file, &online, &n); |
12745 | 0 | if (err) { |
12746 | 0 | pr_warn("failed to get online CPU mask: %d\n", err); |
12747 | 0 | goto error; |
12748 | 0 | } |
12749 | | |
12750 | 0 | for (i = 0, j = 0; i < pb->cpu_cnt; i++) { |
12751 | 0 | struct perf_cpu_buf *cpu_buf; |
12752 | 0 | int cpu, map_key; |
12753 | |
|
12754 | 0 | cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; |
12755 | 0 | map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; |
12756 | | |
12757 | | /* in case user didn't explicitly requested particular CPUs to |
12758 | | * be attached to, skip offline/not present CPUs |
12759 | | */ |
12760 | 0 | if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) |
12761 | 0 | continue; |
12762 | | |
12763 | 0 | cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); |
12764 | 0 | if (IS_ERR(cpu_buf)) { |
12765 | 0 | err = PTR_ERR(cpu_buf); |
12766 | 0 | goto error; |
12767 | 0 | } |
12768 | | |
12769 | 0 | pb->cpu_bufs[j] = cpu_buf; |
12770 | |
|
12771 | 0 | err = bpf_map_update_elem(pb->map_fd, &map_key, |
12772 | 0 | &cpu_buf->fd, 0); |
12773 | 0 | if (err) { |
12774 | 0 | err = -errno; |
12775 | 0 | pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", |
12776 | 0 | cpu, map_key, cpu_buf->fd, |
12777 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
12778 | 0 | goto error; |
12779 | 0 | } |
12780 | | |
12781 | 0 | pb->events[j].events = EPOLLIN; |
12782 | 0 | pb->events[j].data.ptr = cpu_buf; |
12783 | 0 | if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, |
12784 | 0 | &pb->events[j]) < 0) { |
12785 | 0 | err = -errno; |
12786 | 0 | pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", |
12787 | 0 | cpu, cpu_buf->fd, |
12788 | 0 | libbpf_strerror_r(err, msg, sizeof(msg))); |
12789 | 0 | goto error; |
12790 | 0 | } |
12791 | 0 | j++; |
12792 | 0 | } |
12793 | 0 | pb->cpu_cnt = j; |
12794 | 0 | free(online); |
12795 | |
|
12796 | 0 | return pb; |
12797 | | |
12798 | 0 | error: |
12799 | 0 | free(online); |
12800 | 0 | if (pb) |
12801 | 0 | perf_buffer__free(pb); |
12802 | 0 | return ERR_PTR(err); |
12803 | 0 | } |
12804 | | |
12805 | | struct perf_sample_raw { |
12806 | | struct perf_event_header header; |
12807 | | uint32_t size; |
12808 | | char data[]; |
12809 | | }; |
12810 | | |
12811 | | struct perf_sample_lost { |
12812 | | struct perf_event_header header; |
12813 | | uint64_t id; |
12814 | | uint64_t lost; |
12815 | | uint64_t sample_id; |
12816 | | }; |
12817 | | |
12818 | | static enum bpf_perf_event_ret |
12819 | | perf_buffer__process_record(struct perf_event_header *e, void *ctx) |
12820 | 0 | { |
12821 | 0 | struct perf_cpu_buf *cpu_buf = ctx; |
12822 | 0 | struct perf_buffer *pb = cpu_buf->pb; |
12823 | 0 | void *data = e; |
12824 | | |
12825 | | /* user wants full control over parsing perf event */ |
12826 | 0 | if (pb->event_cb) |
12827 | 0 | return pb->event_cb(pb->ctx, cpu_buf->cpu, e); |
12828 | | |
12829 | 0 | switch (e->type) { |
12830 | 0 | case PERF_RECORD_SAMPLE: { |
12831 | 0 | struct perf_sample_raw *s = data; |
12832 | |
|
12833 | 0 | if (pb->sample_cb) |
12834 | 0 | pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); |
12835 | 0 | break; |
12836 | 0 | } |
12837 | 0 | case PERF_RECORD_LOST: { |
12838 | 0 | struct perf_sample_lost *s = data; |
12839 | |
|
12840 | 0 | if (pb->lost_cb) |
12841 | 0 | pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); |
12842 | 0 | break; |
12843 | 0 | } |
12844 | 0 | default: |
12845 | 0 | pr_warn("unknown perf sample type %d\n", e->type); |
12846 | 0 | return LIBBPF_PERF_EVENT_ERROR; |
12847 | 0 | } |
12848 | 0 | return LIBBPF_PERF_EVENT_CONT; |
12849 | 0 | } |
12850 | | |
12851 | | static int perf_buffer__process_records(struct perf_buffer *pb, |
12852 | | struct perf_cpu_buf *cpu_buf) |
12853 | 0 | { |
12854 | 0 | enum bpf_perf_event_ret ret; |
12855 | |
|
12856 | 0 | ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, |
12857 | 0 | pb->page_size, &cpu_buf->buf, |
12858 | 0 | &cpu_buf->buf_size, |
12859 | 0 | perf_buffer__process_record, cpu_buf); |
12860 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
12861 | 0 | return ret; |
12862 | 0 | return 0; |
12863 | 0 | } |
12864 | | |
12865 | | int perf_buffer__epoll_fd(const struct perf_buffer *pb) |
12866 | 0 | { |
12867 | 0 | return pb->epoll_fd; |
12868 | 0 | } |
12869 | | |
12870 | | int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) |
12871 | 0 | { |
12872 | 0 | int i, cnt, err; |
12873 | |
|
12874 | 0 | cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); |
12875 | 0 | if (cnt < 0) |
12876 | 0 | return -errno; |
12877 | | |
12878 | 0 | for (i = 0; i < cnt; i++) { |
12879 | 0 | struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; |
12880 | |
|
12881 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
12882 | 0 | if (err) { |
12883 | 0 | pr_warn("error while processing records: %d\n", err); |
12884 | 0 | return libbpf_err(err); |
12885 | 0 | } |
12886 | 0 | } |
12887 | 0 | return cnt; |
12888 | 0 | } |
12889 | | |
12890 | | /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer |
12891 | | * manager. |
12892 | | */ |
12893 | | size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb) |
12894 | 0 | { |
12895 | 0 | return pb->cpu_cnt; |
12896 | 0 | } |
12897 | | |
12898 | | /* |
12899 | | * Return perf_event FD of a ring buffer in *buf_idx* slot of |
12900 | | * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using |
12901 | | * select()/poll()/epoll() Linux syscalls. |
12902 | | */ |
12903 | | int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx) |
12904 | 0 | { |
12905 | 0 | struct perf_cpu_buf *cpu_buf; |
12906 | |
|
12907 | 0 | if (buf_idx >= pb->cpu_cnt) |
12908 | 0 | return libbpf_err(-EINVAL); |
12909 | | |
12910 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
12911 | 0 | if (!cpu_buf) |
12912 | 0 | return libbpf_err(-ENOENT); |
12913 | | |
12914 | 0 | return cpu_buf->fd; |
12915 | 0 | } |
12916 | | |
12917 | | int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size) |
12918 | 0 | { |
12919 | 0 | struct perf_cpu_buf *cpu_buf; |
12920 | |
|
12921 | 0 | if (buf_idx >= pb->cpu_cnt) |
12922 | 0 | return libbpf_err(-EINVAL); |
12923 | | |
12924 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
12925 | 0 | if (!cpu_buf) |
12926 | 0 | return libbpf_err(-ENOENT); |
12927 | | |
12928 | 0 | *buf = cpu_buf->base; |
12929 | 0 | *buf_size = pb->mmap_size; |
12930 | 0 | return 0; |
12931 | 0 | } |
12932 | | |
12933 | | /* |
12934 | | * Consume data from perf ring buffer corresponding to slot *buf_idx* in |
12935 | | * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to |
12936 | | * consume, do nothing and return success. |
12937 | | * Returns: |
12938 | | * - 0 on success; |
12939 | | * - <0 on failure. |
12940 | | */ |
12941 | | int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx) |
12942 | 0 | { |
12943 | 0 | struct perf_cpu_buf *cpu_buf; |
12944 | |
|
12945 | 0 | if (buf_idx >= pb->cpu_cnt) |
12946 | 0 | return libbpf_err(-EINVAL); |
12947 | | |
12948 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
12949 | 0 | if (!cpu_buf) |
12950 | 0 | return libbpf_err(-ENOENT); |
12951 | | |
12952 | 0 | return perf_buffer__process_records(pb, cpu_buf); |
12953 | 0 | } |
12954 | | |
12955 | | int perf_buffer__consume(struct perf_buffer *pb) |
12956 | 0 | { |
12957 | 0 | int i, err; |
12958 | |
|
12959 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
12960 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
12961 | |
|
12962 | 0 | if (!cpu_buf) |
12963 | 0 | continue; |
12964 | | |
12965 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
12966 | 0 | if (err) { |
12967 | 0 | pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err); |
12968 | 0 | return libbpf_err(err); |
12969 | 0 | } |
12970 | 0 | } |
12971 | 0 | return 0; |
12972 | 0 | } |
12973 | | |
12974 | | int bpf_program__set_attach_target(struct bpf_program *prog, |
12975 | | int attach_prog_fd, |
12976 | | const char *attach_func_name) |
12977 | 0 | { |
12978 | 0 | int btf_obj_fd = 0, btf_id = 0, err; |
12979 | |
|
12980 | 0 | if (!prog || attach_prog_fd < 0) |
12981 | 0 | return libbpf_err(-EINVAL); |
12982 | | |
12983 | 0 | if (prog->obj->loaded) |
12984 | 0 | return libbpf_err(-EINVAL); |
12985 | | |
12986 | 0 | if (attach_prog_fd && !attach_func_name) { |
12987 | | /* remember attach_prog_fd and let bpf_program__load() find |
12988 | | * BTF ID during the program load |
12989 | | */ |
12990 | 0 | prog->attach_prog_fd = attach_prog_fd; |
12991 | 0 | return 0; |
12992 | 0 | } |
12993 | | |
12994 | 0 | if (attach_prog_fd) { |
12995 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, |
12996 | 0 | attach_prog_fd); |
12997 | 0 | if (btf_id < 0) |
12998 | 0 | return libbpf_err(btf_id); |
12999 | 0 | } else { |
13000 | 0 | if (!attach_func_name) |
13001 | 0 | return libbpf_err(-EINVAL); |
13002 | | |
13003 | | /* load btf_vmlinux, if not yet */ |
13004 | 0 | err = bpf_object__load_vmlinux_btf(prog->obj, true); |
13005 | 0 | if (err) |
13006 | 0 | return libbpf_err(err); |
13007 | 0 | err = find_kernel_btf_id(prog->obj, attach_func_name, |
13008 | 0 | prog->expected_attach_type, |
13009 | 0 | &btf_obj_fd, &btf_id); |
13010 | 0 | if (err) |
13011 | 0 | return libbpf_err(err); |
13012 | 0 | } |
13013 | | |
13014 | 0 | prog->attach_btf_id = btf_id; |
13015 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
13016 | 0 | prog->attach_prog_fd = attach_prog_fd; |
13017 | 0 | return 0; |
13018 | 0 | } |
13019 | | |
13020 | | int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz) |
13021 | 0 | { |
13022 | 0 | int err = 0, n, len, start, end = -1; |
13023 | 0 | bool *tmp; |
13024 | |
|
13025 | 0 | *mask = NULL; |
13026 | 0 | *mask_sz = 0; |
13027 | | |
13028 | | /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ |
13029 | 0 | while (*s) { |
13030 | 0 | if (*s == ',' || *s == '\n') { |
13031 | 0 | s++; |
13032 | 0 | continue; |
13033 | 0 | } |
13034 | 0 | n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); |
13035 | 0 | if (n <= 0 || n > 2) { |
13036 | 0 | pr_warn("Failed to get CPU range %s: %d\n", s, n); |
13037 | 0 | err = -EINVAL; |
13038 | 0 | goto cleanup; |
13039 | 0 | } else if (n == 1) { |
13040 | 0 | end = start; |
13041 | 0 | } |
13042 | 0 | if (start < 0 || start > end) { |
13043 | 0 | pr_warn("Invalid CPU range [%d,%d] in %s\n", |
13044 | 0 | start, end, s); |
13045 | 0 | err = -EINVAL; |
13046 | 0 | goto cleanup; |
13047 | 0 | } |
13048 | 0 | tmp = realloc(*mask, end + 1); |
13049 | 0 | if (!tmp) { |
13050 | 0 | err = -ENOMEM; |
13051 | 0 | goto cleanup; |
13052 | 0 | } |
13053 | 0 | *mask = tmp; |
13054 | 0 | memset(tmp + *mask_sz, 0, start - *mask_sz); |
13055 | 0 | memset(tmp + start, 1, end - start + 1); |
13056 | 0 | *mask_sz = end + 1; |
13057 | 0 | s += len; |
13058 | 0 | } |
13059 | 0 | if (!*mask_sz) { |
13060 | 0 | pr_warn("Empty CPU range\n"); |
13061 | 0 | return -EINVAL; |
13062 | 0 | } |
13063 | 0 | return 0; |
13064 | 0 | cleanup: |
13065 | 0 | free(*mask); |
13066 | 0 | *mask = NULL; |
13067 | 0 | return err; |
13068 | 0 | } |
13069 | | |
13070 | | int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) |
13071 | 0 | { |
13072 | 0 | int fd, err = 0, len; |
13073 | 0 | char buf[128]; |
13074 | |
|
13075 | 0 | fd = open(fcpu, O_RDONLY | O_CLOEXEC); |
13076 | 0 | if (fd < 0) { |
13077 | 0 | err = -errno; |
13078 | 0 | pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err); |
13079 | 0 | return err; |
13080 | 0 | } |
13081 | 0 | len = read(fd, buf, sizeof(buf)); |
13082 | 0 | close(fd); |
13083 | 0 | if (len <= 0) { |
13084 | 0 | err = len ? -errno : -EINVAL; |
13085 | 0 | pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err); |
13086 | 0 | return err; |
13087 | 0 | } |
13088 | 0 | if (len >= sizeof(buf)) { |
13089 | 0 | pr_warn("CPU mask is too big in file %s\n", fcpu); |
13090 | 0 | return -E2BIG; |
13091 | 0 | } |
13092 | 0 | buf[len] = '\0'; |
13093 | |
|
13094 | 0 | return parse_cpu_mask_str(buf, mask, mask_sz); |
13095 | 0 | } |
13096 | | |
13097 | | int libbpf_num_possible_cpus(void) |
13098 | 0 | { |
13099 | 0 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
13100 | 0 | static int cpus; |
13101 | 0 | int err, n, i, tmp_cpus; |
13102 | 0 | bool *mask; |
13103 | |
|
13104 | 0 | tmp_cpus = READ_ONCE(cpus); |
13105 | 0 | if (tmp_cpus > 0) |
13106 | 0 | return tmp_cpus; |
13107 | | |
13108 | 0 | err = parse_cpu_mask_file(fcpu, &mask, &n); |
13109 | 0 | if (err) |
13110 | 0 | return libbpf_err(err); |
13111 | | |
13112 | 0 | tmp_cpus = 0; |
13113 | 0 | for (i = 0; i < n; i++) { |
13114 | 0 | if (mask[i]) |
13115 | 0 | tmp_cpus++; |
13116 | 0 | } |
13117 | 0 | free(mask); |
13118 | |
|
13119 | 0 | WRITE_ONCE(cpus, tmp_cpus); |
13120 | 0 | return tmp_cpus; |
13121 | 0 | } |
13122 | | |
13123 | | static int populate_skeleton_maps(const struct bpf_object *obj, |
13124 | | struct bpf_map_skeleton *maps, |
13125 | | size_t map_cnt) |
13126 | 0 | { |
13127 | 0 | int i; |
13128 | |
|
13129 | 0 | for (i = 0; i < map_cnt; i++) { |
13130 | 0 | struct bpf_map **map = maps[i].map; |
13131 | 0 | const char *name = maps[i].name; |
13132 | 0 | void **mmaped = maps[i].mmaped; |
13133 | |
|
13134 | 0 | *map = bpf_object__find_map_by_name(obj, name); |
13135 | 0 | if (!*map) { |
13136 | 0 | pr_warn("failed to find skeleton map '%s'\n", name); |
13137 | 0 | return -ESRCH; |
13138 | 0 | } |
13139 | | |
13140 | | /* externs shouldn't be pre-setup from user code */ |
13141 | 0 | if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) |
13142 | 0 | *mmaped = (*map)->mmaped; |
13143 | 0 | } |
13144 | 0 | return 0; |
13145 | 0 | } |
13146 | | |
13147 | | static int populate_skeleton_progs(const struct bpf_object *obj, |
13148 | | struct bpf_prog_skeleton *progs, |
13149 | | size_t prog_cnt) |
13150 | 0 | { |
13151 | 0 | int i; |
13152 | |
|
13153 | 0 | for (i = 0; i < prog_cnt; i++) { |
13154 | 0 | struct bpf_program **prog = progs[i].prog; |
13155 | 0 | const char *name = progs[i].name; |
13156 | |
|
13157 | 0 | *prog = bpf_object__find_program_by_name(obj, name); |
13158 | 0 | if (!*prog) { |
13159 | 0 | pr_warn("failed to find skeleton program '%s'\n", name); |
13160 | 0 | return -ESRCH; |
13161 | 0 | } |
13162 | 0 | } |
13163 | 0 | return 0; |
13164 | 0 | } |
13165 | | |
13166 | | int bpf_object__open_skeleton(struct bpf_object_skeleton *s, |
13167 | | const struct bpf_object_open_opts *opts) |
13168 | 0 | { |
13169 | 0 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts, |
13170 | 0 | .object_name = s->name, |
13171 | 0 | ); |
13172 | 0 | struct bpf_object *obj; |
13173 | 0 | int err; |
13174 | | |
13175 | | /* Attempt to preserve opts->object_name, unless overriden by user |
13176 | | * explicitly. Overwriting object name for skeletons is discouraged, |
13177 | | * as it breaks global data maps, because they contain object name |
13178 | | * prefix as their own map name prefix. When skeleton is generated, |
13179 | | * bpftool is making an assumption that this name will stay the same. |
13180 | | */ |
13181 | 0 | if (opts) { |
13182 | 0 | memcpy(&skel_opts, opts, sizeof(*opts)); |
13183 | 0 | if (!opts->object_name) |
13184 | 0 | skel_opts.object_name = s->name; |
13185 | 0 | } |
13186 | |
|
13187 | 0 | obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); |
13188 | 0 | err = libbpf_get_error(obj); |
13189 | 0 | if (err) { |
13190 | 0 | pr_warn("failed to initialize skeleton BPF object '%s': %d\n", |
13191 | 0 | s->name, err); |
13192 | 0 | return libbpf_err(err); |
13193 | 0 | } |
13194 | | |
13195 | 0 | *s->obj = obj; |
13196 | 0 | err = populate_skeleton_maps(obj, s->maps, s->map_cnt); |
13197 | 0 | if (err) { |
13198 | 0 | pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); |
13199 | 0 | return libbpf_err(err); |
13200 | 0 | } |
13201 | | |
13202 | 0 | err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); |
13203 | 0 | if (err) { |
13204 | 0 | pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); |
13205 | 0 | return libbpf_err(err); |
13206 | 0 | } |
13207 | | |
13208 | 0 | return 0; |
13209 | 0 | } |
13210 | | |
13211 | | int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) |
13212 | 0 | { |
13213 | 0 | int err, len, var_idx, i; |
13214 | 0 | const char *var_name; |
13215 | 0 | const struct bpf_map *map; |
13216 | 0 | struct btf *btf; |
13217 | 0 | __u32 map_type_id; |
13218 | 0 | const struct btf_type *map_type, *var_type; |
13219 | 0 | const struct bpf_var_skeleton *var_skel; |
13220 | 0 | struct btf_var_secinfo *var; |
13221 | |
|
13222 | 0 | if (!s->obj) |
13223 | 0 | return libbpf_err(-EINVAL); |
13224 | | |
13225 | 0 | btf = bpf_object__btf(s->obj); |
13226 | 0 | if (!btf) { |
13227 | 0 | pr_warn("subskeletons require BTF at runtime (object %s)\n", |
13228 | 0 | bpf_object__name(s->obj)); |
13229 | 0 | return libbpf_err(-errno); |
13230 | 0 | } |
13231 | | |
13232 | 0 | err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); |
13233 | 0 | if (err) { |
13234 | 0 | pr_warn("failed to populate subskeleton maps: %d\n", err); |
13235 | 0 | return libbpf_err(err); |
13236 | 0 | } |
13237 | | |
13238 | 0 | err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); |
13239 | 0 | if (err) { |
13240 | 0 | pr_warn("failed to populate subskeleton maps: %d\n", err); |
13241 | 0 | return libbpf_err(err); |
13242 | 0 | } |
13243 | | |
13244 | 0 | for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { |
13245 | 0 | var_skel = &s->vars[var_idx]; |
13246 | 0 | map = *var_skel->map; |
13247 | 0 | map_type_id = bpf_map__btf_value_type_id(map); |
13248 | 0 | map_type = btf__type_by_id(btf, map_type_id); |
13249 | |
|
13250 | 0 | if (!btf_is_datasec(map_type)) { |
13251 | 0 | pr_warn("type for map '%1$s' is not a datasec: %2$s", |
13252 | 0 | bpf_map__name(map), |
13253 | 0 | __btf_kind_str(btf_kind(map_type))); |
13254 | 0 | return libbpf_err(-EINVAL); |
13255 | 0 | } |
13256 | | |
13257 | 0 | len = btf_vlen(map_type); |
13258 | 0 | var = btf_var_secinfos(map_type); |
13259 | 0 | for (i = 0; i < len; i++, var++) { |
13260 | 0 | var_type = btf__type_by_id(btf, var->type); |
13261 | 0 | var_name = btf__name_by_offset(btf, var_type->name_off); |
13262 | 0 | if (strcmp(var_name, var_skel->name) == 0) { |
13263 | 0 | *var_skel->addr = map->mmaped + var->offset; |
13264 | 0 | break; |
13265 | 0 | } |
13266 | 0 | } |
13267 | 0 | } |
13268 | 0 | return 0; |
13269 | 0 | } |
13270 | | |
13271 | | void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) |
13272 | 0 | { |
13273 | 0 | if (!s) |
13274 | 0 | return; |
13275 | 0 | free(s->maps); |
13276 | 0 | free(s->progs); |
13277 | 0 | free(s->vars); |
13278 | 0 | free(s); |
13279 | 0 | } |
13280 | | |
13281 | | int bpf_object__load_skeleton(struct bpf_object_skeleton *s) |
13282 | 0 | { |
13283 | 0 | int i, err; |
13284 | |
|
13285 | 0 | err = bpf_object__load(*s->obj); |
13286 | 0 | if (err) { |
13287 | 0 | pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err); |
13288 | 0 | return libbpf_err(err); |
13289 | 0 | } |
13290 | | |
13291 | 0 | for (i = 0; i < s->map_cnt; i++) { |
13292 | 0 | struct bpf_map *map = *s->maps[i].map; |
13293 | 0 | size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); |
13294 | 0 | int prot, map_fd = bpf_map__fd(map); |
13295 | 0 | void **mmaped = s->maps[i].mmaped; |
13296 | |
|
13297 | 0 | if (!mmaped) |
13298 | 0 | continue; |
13299 | | |
13300 | 0 | if (!(map->def.map_flags & BPF_F_MMAPABLE)) { |
13301 | 0 | *mmaped = NULL; |
13302 | 0 | continue; |
13303 | 0 | } |
13304 | | |
13305 | 0 | if (map->def.map_flags & BPF_F_RDONLY_PROG) |
13306 | 0 | prot = PROT_READ; |
13307 | 0 | else |
13308 | 0 | prot = PROT_READ | PROT_WRITE; |
13309 | | |
13310 | | /* Remap anonymous mmap()-ed "map initialization image" as |
13311 | | * a BPF map-backed mmap()-ed memory, but preserving the same |
13312 | | * memory address. This will cause kernel to change process' |
13313 | | * page table to point to a different piece of kernel memory, |
13314 | | * but from userspace point of view memory address (and its |
13315 | | * contents, being identical at this point) will stay the |
13316 | | * same. This mapping will be released by bpf_object__close() |
13317 | | * as per normal clean up procedure, so we don't need to worry |
13318 | | * about it from skeleton's clean up perspective. |
13319 | | */ |
13320 | 0 | *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0); |
13321 | 0 | if (*mmaped == MAP_FAILED) { |
13322 | 0 | err = -errno; |
13323 | 0 | *mmaped = NULL; |
13324 | 0 | pr_warn("failed to re-mmap() map '%s': %d\n", |
13325 | 0 | bpf_map__name(map), err); |
13326 | 0 | return libbpf_err(err); |
13327 | 0 | } |
13328 | 0 | } |
13329 | | |
13330 | 0 | return 0; |
13331 | 0 | } |
13332 | | |
13333 | | int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) |
13334 | 0 | { |
13335 | 0 | int i, err; |
13336 | |
|
13337 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
13338 | 0 | struct bpf_program *prog = *s->progs[i].prog; |
13339 | 0 | struct bpf_link **link = s->progs[i].link; |
13340 | |
|
13341 | 0 | if (!prog->autoload || !prog->autoattach) |
13342 | 0 | continue; |
13343 | | |
13344 | | /* auto-attaching not supported for this program */ |
13345 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
13346 | 0 | continue; |
13347 | | |
13348 | | /* if user already set the link manually, don't attempt auto-attach */ |
13349 | 0 | if (*link) |
13350 | 0 | continue; |
13351 | | |
13352 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); |
13353 | 0 | if (err) { |
13354 | 0 | pr_warn("prog '%s': failed to auto-attach: %d\n", |
13355 | 0 | bpf_program__name(prog), err); |
13356 | 0 | return libbpf_err(err); |
13357 | 0 | } |
13358 | | |
13359 | | /* It's possible that for some SEC() definitions auto-attach |
13360 | | * is supported in some cases (e.g., if definition completely |
13361 | | * specifies target information), but is not in other cases. |
13362 | | * SEC("uprobe") is one such case. If user specified target |
13363 | | * binary and function name, such BPF program can be |
13364 | | * auto-attached. But if not, it shouldn't trigger skeleton's |
13365 | | * attach to fail. It should just be skipped. |
13366 | | * attach_fn signals such case with returning 0 (no error) and |
13367 | | * setting link to NULL. |
13368 | | */ |
13369 | 0 | } |
13370 | | |
13371 | 0 | return 0; |
13372 | 0 | } |
13373 | | |
13374 | | void bpf_object__detach_skeleton(struct bpf_object_skeleton *s) |
13375 | 0 | { |
13376 | 0 | int i; |
13377 | |
|
13378 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
13379 | 0 | struct bpf_link **link = s->progs[i].link; |
13380 | |
|
13381 | 0 | bpf_link__destroy(*link); |
13382 | 0 | *link = NULL; |
13383 | 0 | } |
13384 | 0 | } |
13385 | | |
13386 | | void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) |
13387 | 0 | { |
13388 | 0 | if (!s) |
13389 | 0 | return; |
13390 | | |
13391 | 0 | if (s->progs) |
13392 | 0 | bpf_object__detach_skeleton(s); |
13393 | 0 | if (s->obj) |
13394 | 0 | bpf_object__close(*s->obj); |
13395 | 0 | free(s->maps); |
13396 | 0 | free(s->progs); |
13397 | 0 | free(s); |
13398 | 0 | } |