Line | Count | Source |
1 | | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
2 | | |
3 | | /* |
4 | | * Common eBPF ELF object loading operations. |
5 | | * |
6 | | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
7 | | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
8 | | * Copyright (C) 2015 Huawei Inc. |
9 | | * Copyright (C) 2017 Nicira, Inc. |
10 | | * Copyright (C) 2019 Isovalent, Inc. |
11 | | */ |
12 | | |
13 | | #ifndef _GNU_SOURCE |
14 | | #define _GNU_SOURCE |
15 | | #endif |
16 | | #include <stdlib.h> |
17 | | #include <stdio.h> |
18 | | #include <stdarg.h> |
19 | | #include <libgen.h> |
20 | | #include <inttypes.h> |
21 | | #include <limits.h> |
22 | | #include <string.h> |
23 | | #include <unistd.h> |
24 | | #include <endian.h> |
25 | | #include <fcntl.h> |
26 | | #include <errno.h> |
27 | | #include <ctype.h> |
28 | | #include <asm/unistd.h> |
29 | | #include <linux/err.h> |
30 | | #include <linux/kernel.h> |
31 | | #include <linux/bpf.h> |
32 | | #include <linux/btf.h> |
33 | | #include <linux/filter.h> |
34 | | #include <linux/limits.h> |
35 | | #include <linux/perf_event.h> |
36 | | #include <linux/bpf_perf_event.h> |
37 | | #include <linux/ring_buffer.h> |
38 | | #include <sys/epoll.h> |
39 | | #include <sys/ioctl.h> |
40 | | #include <sys/mman.h> |
41 | | #include <sys/stat.h> |
42 | | #include <sys/types.h> |
43 | | #include <sys/vfs.h> |
44 | | #include <sys/utsname.h> |
45 | | #include <sys/resource.h> |
46 | | #include <libelf.h> |
47 | | #include <gelf.h> |
48 | | #include <zlib.h> |
49 | | |
50 | | #include "libbpf.h" |
51 | | #include "bpf.h" |
52 | | #include "btf.h" |
53 | | #include "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 | | #define MAX_EVENT_NAME_LEN 64 |
63 | | |
64 | 0 | #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf" |
65 | | |
66 | 105k | #define BPF_INSN_SZ (sizeof(struct bpf_insn)) |
67 | | |
68 | | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
69 | | * compilation if user enables corresponding warning. Disable it explicitly. |
70 | | */ |
71 | | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
72 | | |
73 | | #define __printf(a, b) __attribute__((format(printf, a, b))) |
74 | | |
75 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj); |
76 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog); |
77 | | static int map_set_def_max_entries(struct bpf_map *map); |
78 | | |
79 | | static const char * const attach_type_name[] = { |
80 | | [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress", |
81 | | [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress", |
82 | | [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create", |
83 | | [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release", |
84 | | [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops", |
85 | | [BPF_CGROUP_DEVICE] = "cgroup_device", |
86 | | [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind", |
87 | | [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind", |
88 | | [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect", |
89 | | [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect", |
90 | | [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect", |
91 | | [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind", |
92 | | [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind", |
93 | | [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername", |
94 | | [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername", |
95 | | [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername", |
96 | | [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname", |
97 | | [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname", |
98 | | [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname", |
99 | | [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg", |
100 | | [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg", |
101 | | [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg", |
102 | | [BPF_CGROUP_SYSCTL] = "cgroup_sysctl", |
103 | | [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg", |
104 | | [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg", |
105 | | [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg", |
106 | | [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt", |
107 | | [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt", |
108 | | [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser", |
109 | | [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict", |
110 | | [BPF_SK_SKB_VERDICT] = "sk_skb_verdict", |
111 | | [BPF_SK_MSG_VERDICT] = "sk_msg_verdict", |
112 | | [BPF_LIRC_MODE2] = "lirc_mode2", |
113 | | [BPF_FLOW_DISSECTOR] = "flow_dissector", |
114 | | [BPF_TRACE_RAW_TP] = "trace_raw_tp", |
115 | | [BPF_TRACE_FENTRY] = "trace_fentry", |
116 | | [BPF_TRACE_FEXIT] = "trace_fexit", |
117 | | [BPF_MODIFY_RETURN] = "modify_return", |
118 | | [BPF_TRACE_FSESSION] = "trace_fsession", |
119 | | [BPF_LSM_MAC] = "lsm_mac", |
120 | | [BPF_LSM_CGROUP] = "lsm_cgroup", |
121 | | [BPF_SK_LOOKUP] = "sk_lookup", |
122 | | [BPF_TRACE_ITER] = "trace_iter", |
123 | | [BPF_XDP_DEVMAP] = "xdp_devmap", |
124 | | [BPF_XDP_CPUMAP] = "xdp_cpumap", |
125 | | [BPF_XDP] = "xdp", |
126 | | [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select", |
127 | | [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate", |
128 | | [BPF_PERF_EVENT] = "perf_event", |
129 | | [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi", |
130 | | [BPF_STRUCT_OPS] = "struct_ops", |
131 | | [BPF_NETFILTER] = "netfilter", |
132 | | [BPF_TCX_INGRESS] = "tcx_ingress", |
133 | | [BPF_TCX_EGRESS] = "tcx_egress", |
134 | | [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi", |
135 | | [BPF_NETKIT_PRIMARY] = "netkit_primary", |
136 | | [BPF_NETKIT_PEER] = "netkit_peer", |
137 | | [BPF_TRACE_KPROBE_SESSION] = "trace_kprobe_session", |
138 | | [BPF_TRACE_UPROBE_SESSION] = "trace_uprobe_session", |
139 | | [BPF_TRACE_FENTRY_MULTI] = "trace_fentry_multi", |
140 | | [BPF_TRACE_FEXIT_MULTI] = "trace_fexit_multi", |
141 | | [BPF_TRACE_FSESSION_MULTI] = "trace_fsession_multi", |
142 | | }; |
143 | | |
144 | | static const char * const link_type_name[] = { |
145 | | [BPF_LINK_TYPE_UNSPEC] = "unspec", |
146 | | [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
147 | | [BPF_LINK_TYPE_TRACING] = "tracing", |
148 | | [BPF_LINK_TYPE_CGROUP] = "cgroup", |
149 | | [BPF_LINK_TYPE_ITER] = "iter", |
150 | | [BPF_LINK_TYPE_NETNS] = "netns", |
151 | | [BPF_LINK_TYPE_XDP] = "xdp", |
152 | | [BPF_LINK_TYPE_PERF_EVENT] = "perf_event", |
153 | | [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi", |
154 | | [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops", |
155 | | [BPF_LINK_TYPE_NETFILTER] = "netfilter", |
156 | | [BPF_LINK_TYPE_TCX] = "tcx", |
157 | | [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi", |
158 | | [BPF_LINK_TYPE_NETKIT] = "netkit", |
159 | | [BPF_LINK_TYPE_SOCKMAP] = "sockmap", |
160 | | [BPF_LINK_TYPE_TRACING_MULTI] = "tracing_multi", |
161 | | }; |
162 | | |
163 | | static const char * const map_type_name[] = { |
164 | | [BPF_MAP_TYPE_UNSPEC] = "unspec", |
165 | | [BPF_MAP_TYPE_HASH] = "hash", |
166 | | [BPF_MAP_TYPE_ARRAY] = "array", |
167 | | [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", |
168 | | [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", |
169 | | [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", |
170 | | [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", |
171 | | [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", |
172 | | [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", |
173 | | [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", |
174 | | [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", |
175 | | [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", |
176 | | [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", |
177 | | [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", |
178 | | [BPF_MAP_TYPE_DEVMAP] = "devmap", |
179 | | [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash", |
180 | | [BPF_MAP_TYPE_SOCKMAP] = "sockmap", |
181 | | [BPF_MAP_TYPE_CPUMAP] = "cpumap", |
182 | | [BPF_MAP_TYPE_XSKMAP] = "xskmap", |
183 | | [BPF_MAP_TYPE_SOCKHASH] = "sockhash", |
184 | | [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage", |
185 | | [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray", |
186 | | [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage", |
187 | | [BPF_MAP_TYPE_QUEUE] = "queue", |
188 | | [BPF_MAP_TYPE_STACK] = "stack", |
189 | | [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", |
190 | | [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", |
191 | | [BPF_MAP_TYPE_RINGBUF] = "ringbuf", |
192 | | [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", |
193 | | [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", |
194 | | [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter", |
195 | | [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf", |
196 | | [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage", |
197 | | [BPF_MAP_TYPE_ARENA] = "arena", |
198 | | [BPF_MAP_TYPE_INSN_ARRAY] = "insn_array", |
199 | | [BPF_MAP_TYPE_RHASH] = "rhash", |
200 | | }; |
201 | | |
202 | | static const char * const prog_type_name[] = { |
203 | | [BPF_PROG_TYPE_UNSPEC] = "unspec", |
204 | | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", |
205 | | [BPF_PROG_TYPE_KPROBE] = "kprobe", |
206 | | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", |
207 | | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", |
208 | | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", |
209 | | [BPF_PROG_TYPE_XDP] = "xdp", |
210 | | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", |
211 | | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", |
212 | | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", |
213 | | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", |
214 | | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", |
215 | | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", |
216 | | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", |
217 | | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", |
218 | | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", |
219 | | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", |
220 | | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
221 | | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", |
222 | | [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", |
223 | | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", |
224 | | [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", |
225 | | [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", |
226 | | [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", |
227 | | [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable", |
228 | | [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", |
229 | | [BPF_PROG_TYPE_TRACING] = "tracing", |
230 | | [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops", |
231 | | [BPF_PROG_TYPE_EXT] = "ext", |
232 | | [BPF_PROG_TYPE_LSM] = "lsm", |
233 | | [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", |
234 | | [BPF_PROG_TYPE_SYSCALL] = "syscall", |
235 | | [BPF_PROG_TYPE_NETFILTER] = "netfilter", |
236 | | }; |
237 | | |
238 | | static int __base_pr(enum libbpf_print_level level, const char *format, |
239 | | va_list args) |
240 | 0 | { |
241 | 0 | const char *env_var = "LIBBPF_LOG_LEVEL"; |
242 | 0 | static enum libbpf_print_level min_level = LIBBPF_INFO; |
243 | 0 | static bool initialized; |
244 | |
|
245 | 0 | if (!initialized) { |
246 | 0 | char *verbosity; |
247 | |
|
248 | 0 | initialized = true; |
249 | 0 | verbosity = getenv(env_var); |
250 | 0 | if (verbosity) { |
251 | 0 | if (strcasecmp(verbosity, "warn") == 0) |
252 | 0 | min_level = LIBBPF_WARN; |
253 | 0 | else if (strcasecmp(verbosity, "debug") == 0) |
254 | 0 | min_level = LIBBPF_DEBUG; |
255 | 0 | else if (strcasecmp(verbosity, "info") == 0) |
256 | 0 | min_level = LIBBPF_INFO; |
257 | 0 | else |
258 | 0 | fprintf(stderr, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n", |
259 | 0 | env_var, verbosity); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | /* if too verbose, skip logging */ |
264 | 0 | if (level > min_level) |
265 | 0 | return 0; |
266 | | |
267 | 0 | return vfprintf(stderr, format, args); |
268 | 0 | } |
269 | | |
270 | | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
271 | | |
272 | | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
273 | 10.9k | { |
274 | 10.9k | libbpf_print_fn_t old_print_fn; |
275 | | |
276 | 10.9k | old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED); |
277 | | |
278 | 10.9k | return old_print_fn; |
279 | 10.9k | } |
280 | | |
281 | | __printf(2, 3) |
282 | | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
283 | 168k | { |
284 | 168k | va_list args; |
285 | 168k | int old_errno; |
286 | 168k | libbpf_print_fn_t print_fn; |
287 | | |
288 | 168k | print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED); |
289 | 168k | if (!print_fn) |
290 | 0 | return; |
291 | | |
292 | 168k | old_errno = errno; |
293 | | |
294 | 168k | va_start(args, format); |
295 | 168k | print_fn(level, format, args); |
296 | 168k | va_end(args); |
297 | | |
298 | 168k | errno = old_errno; |
299 | 168k | } |
300 | | |
301 | | static void pr_perm_msg(int err) |
302 | 0 | { |
303 | 0 | struct rlimit limit; |
304 | 0 | char buf[100]; |
305 | |
|
306 | 0 | if (err != -EPERM || geteuid() != 0) |
307 | 0 | return; |
308 | | |
309 | 0 | err = getrlimit(RLIMIT_MEMLOCK, &limit); |
310 | 0 | if (err) |
311 | 0 | return; |
312 | | |
313 | 0 | if (limit.rlim_cur == RLIM_INFINITY) |
314 | 0 | return; |
315 | | |
316 | 0 | if (limit.rlim_cur < 1024) |
317 | 0 | snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur); |
318 | 0 | else if (limit.rlim_cur < 1024*1024) |
319 | 0 | snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024); |
320 | 0 | else |
321 | 0 | snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024)); |
322 | |
|
323 | 0 | pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", |
324 | 0 | buf); |
325 | 0 | } |
326 | | |
327 | | /* Copied from tools/perf/util/util.h */ |
328 | | #ifndef zfree |
329 | 257k | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
330 | | #endif |
331 | | |
332 | | #ifndef zclose |
333 | 33.8k | # define zclose(fd) ({ \ |
334 | 20.8k | int ___err = 0; \ |
335 | 33.8k | if ((fd) >= 0) \ |
336 | 20.8k | ___err = close((fd)); \ |
337 | 20.8k | fd = -1; \ |
338 | 20.8k | ___err; }) |
339 | | #endif |
340 | | |
341 | | static inline __u64 ptr_to_u64(const void *ptr) |
342 | 0 | { |
343 | 0 | return (__u64) (unsigned long) ptr; |
344 | 0 | } |
345 | | |
346 | | int libbpf_set_strict_mode(enum libbpf_strict_mode mode) |
347 | 0 | { |
348 | | /* as of v1.0 libbpf_set_strict_mode() is a no-op */ |
349 | 0 | return 0; |
350 | 0 | } |
351 | | |
352 | | __u32 libbpf_major_version(void) |
353 | 0 | { |
354 | 0 | return LIBBPF_MAJOR_VERSION; |
355 | 0 | } |
356 | | |
357 | | __u32 libbpf_minor_version(void) |
358 | 0 | { |
359 | 0 | return LIBBPF_MINOR_VERSION; |
360 | 0 | } |
361 | | |
362 | | const char *libbpf_version_string(void) |
363 | 0 | { |
364 | 0 | #define __S(X) #X |
365 | 0 | #define _S(X) __S(X) |
366 | 0 | return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION); |
367 | 0 | #undef _S |
368 | 0 | #undef __S |
369 | 0 | } |
370 | | |
371 | | enum reloc_type { |
372 | | RELO_LD64, |
373 | | RELO_CALL, |
374 | | RELO_DATA, |
375 | | RELO_EXTERN_LD64, |
376 | | RELO_EXTERN_CALL, |
377 | | RELO_SUBPROG_ADDR, |
378 | | RELO_CORE, |
379 | | RELO_INSN_ARRAY, |
380 | | }; |
381 | | |
382 | | struct reloc_desc { |
383 | | enum reloc_type type; |
384 | | int insn_idx; |
385 | | union { |
386 | | const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */ |
387 | | struct { |
388 | | int map_idx; |
389 | | unsigned int sym_off; |
390 | | /* |
391 | | * The following two fields can be unionized, as the |
392 | | * ext_idx field is used for extern symbols, and the |
393 | | * sym_size is used for jump tables, which are never |
394 | | * extern |
395 | | */ |
396 | | union { |
397 | | int ext_idx; |
398 | | int sym_size; |
399 | | }; |
400 | | }; |
401 | | }; |
402 | | }; |
403 | | |
404 | | /* stored as sec_def->cookie for all libbpf-supported SEC()s */ |
405 | | enum sec_def_flags { |
406 | | SEC_NONE = 0, |
407 | | /* expected_attach_type is optional, if kernel doesn't support that */ |
408 | | SEC_EXP_ATTACH_OPT = 1, |
409 | | /* legacy, only used by libbpf_get_type_names() and |
410 | | * libbpf_attach_type_by_name(), not used by libbpf itself at all. |
411 | | * This used to be associated with cgroup (and few other) BPF programs |
412 | | * that were attachable through BPF_PROG_ATTACH command. Pretty |
413 | | * meaningless nowadays, though. |
414 | | */ |
415 | | SEC_ATTACHABLE = 2, |
416 | | SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT, |
417 | | /* attachment target is specified through BTF ID in either kernel or |
418 | | * other BPF program's BTF object |
419 | | */ |
420 | | SEC_ATTACH_BTF = 4, |
421 | | /* BPF program type allows sleeping/blocking in kernel */ |
422 | | SEC_SLEEPABLE = 8, |
423 | | /* BPF program support non-linear XDP buffer */ |
424 | | SEC_XDP_FRAGS = 16, |
425 | | /* Setup proper attach type for usdt probes. */ |
426 | | SEC_USDT = 32, |
427 | | }; |
428 | | |
429 | | struct bpf_sec_def { |
430 | | char *sec; |
431 | | enum bpf_prog_type prog_type; |
432 | | enum bpf_attach_type expected_attach_type; |
433 | | long cookie; |
434 | | int handler_id; |
435 | | |
436 | | libbpf_prog_setup_fn_t prog_setup_fn; |
437 | | libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; |
438 | | libbpf_prog_attach_fn_t prog_attach_fn; |
439 | | }; |
440 | | |
441 | | struct bpf_light_subprog { |
442 | | __u32 sec_insn_off; |
443 | | __u32 sub_insn_off; |
444 | | }; |
445 | | |
446 | | /* |
447 | | * bpf_prog should be a better name but it has been used in |
448 | | * linux/filter.h. |
449 | | */ |
450 | | struct bpf_program { |
451 | | char *name; |
452 | | char *sec_name; |
453 | | size_t sec_idx; |
454 | | const struct bpf_sec_def *sec_def; |
455 | | /* this program's instruction offset (in number of instructions) |
456 | | * within its containing ELF section |
457 | | */ |
458 | | size_t sec_insn_off; |
459 | | /* number of original instructions in ELF section belonging to this |
460 | | * program, not taking into account subprogram instructions possible |
461 | | * appended later during relocation |
462 | | */ |
463 | | size_t sec_insn_cnt; |
464 | | /* Offset (in number of instructions) of the start of instruction |
465 | | * belonging to this BPF program within its containing main BPF |
466 | | * program. For the entry-point (main) BPF program, this is always |
467 | | * zero. For a sub-program, this gets reset before each of main BPF |
468 | | * programs are processed and relocated and is used to determined |
469 | | * whether sub-program was already appended to the main program, and |
470 | | * if yes, at which instruction offset. |
471 | | */ |
472 | | size_t sub_insn_off; |
473 | | |
474 | | /* instructions that belong to BPF program; insns[0] is located at |
475 | | * sec_insn_off instruction within its ELF section in ELF file, so |
476 | | * when mapping ELF file instruction index to the local instruction, |
477 | | * one needs to subtract sec_insn_off; and vice versa. |
478 | | */ |
479 | | struct bpf_insn *insns; |
480 | | /* actual number of instruction in this BPF program's image; for |
481 | | * entry-point BPF programs this includes the size of main program |
482 | | * itself plus all the used sub-programs, appended at the end |
483 | | */ |
484 | | size_t insns_cnt; |
485 | | |
486 | | struct reloc_desc *reloc_desc; |
487 | | int nr_reloc; |
488 | | |
489 | | /* BPF verifier log settings */ |
490 | | char *log_buf; |
491 | | size_t log_size; |
492 | | __u32 log_level; |
493 | | |
494 | | struct bpf_object *obj; |
495 | | |
496 | | int fd; |
497 | | bool autoload; |
498 | | bool autoattach; |
499 | | bool sym_global; |
500 | | bool mark_btf_static; |
501 | | enum bpf_prog_type type; |
502 | | enum bpf_attach_type expected_attach_type; |
503 | | int exception_cb_idx; |
504 | | |
505 | | int prog_ifindex; |
506 | | __u32 attach_btf_obj_fd; |
507 | | __u32 attach_btf_id; |
508 | | __u32 attach_prog_fd; |
509 | | |
510 | | void *func_info; |
511 | | __u32 func_info_rec_size; |
512 | | __u32 func_info_cnt; |
513 | | |
514 | | void *line_info; |
515 | | __u32 line_info_rec_size; |
516 | | __u32 line_info_cnt; |
517 | | __u32 prog_flags; |
518 | | __u8 hash[SHA256_DIGEST_LENGTH]; |
519 | | |
520 | | struct bpf_light_subprog *subprogs; |
521 | | __u32 subprog_cnt; |
522 | | }; |
523 | | |
524 | | struct bpf_struct_ops { |
525 | | struct bpf_program **progs; |
526 | | __u32 *kern_func_off; |
527 | | /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ |
528 | | void *data; |
529 | | /* e.g. struct bpf_struct_ops_tcp_congestion_ops in |
530 | | * btf_vmlinux's format. |
531 | | * struct bpf_struct_ops_tcp_congestion_ops { |
532 | | * [... some other kernel fields ...] |
533 | | * struct tcp_congestion_ops data; |
534 | | * } |
535 | | * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops) |
536 | | * bpf_map__init_kern_struct_ops() will populate the "kern_vdata" |
537 | | * from "data". |
538 | | */ |
539 | | void *kern_vdata; |
540 | | __u32 type_id; |
541 | | }; |
542 | | |
543 | 3.55k | #define DATA_SEC ".data" |
544 | 3.24k | #define BSS_SEC ".bss" |
545 | 2.97k | #define RODATA_SEC ".rodata" |
546 | 3.07k | #define KCONFIG_SEC ".kconfig" |
547 | 4.85k | #define KSYMS_SEC ".ksyms" |
548 | 7.95k | #define STRUCT_OPS_SEC ".struct_ops" |
549 | 7.64k | #define STRUCT_OPS_LINK_SEC ".struct_ops.link" |
550 | 2.00k | #define ARENA_SEC ".addr_space.1" |
551 | | |
552 | | enum libbpf_map_type { |
553 | | LIBBPF_MAP_UNSPEC, |
554 | | LIBBPF_MAP_DATA, |
555 | | LIBBPF_MAP_BSS, |
556 | | LIBBPF_MAP_RODATA, |
557 | | LIBBPF_MAP_KCONFIG, |
558 | | }; |
559 | | |
560 | | struct bpf_map_def { |
561 | | unsigned int type; |
562 | | unsigned int key_size; |
563 | | unsigned int value_size; |
564 | | unsigned int max_entries; |
565 | | unsigned int map_flags; |
566 | | }; |
567 | | |
568 | | struct bpf_map { |
569 | | struct bpf_object *obj; |
570 | | char *name; |
571 | | /* real_name is defined for special internal maps (.rodata*, |
572 | | * .data*, .bss, .kconfig) and preserves their original ELF section |
573 | | * name. This is important to be able to find corresponding BTF |
574 | | * DATASEC information. |
575 | | */ |
576 | | char *real_name; |
577 | | int fd; |
578 | | int sec_idx; |
579 | | size_t sec_offset; |
580 | | int map_ifindex; |
581 | | int inner_map_fd; |
582 | | struct bpf_map_def def; |
583 | | __u32 numa_node; |
584 | | __u32 btf_var_idx; |
585 | | int mod_btf_fd; |
586 | | __u32 btf_key_type_id; |
587 | | __u32 btf_value_type_id; |
588 | | __u32 btf_vmlinux_value_type_id; |
589 | | enum libbpf_map_type libbpf_type; |
590 | | void *mmaped; |
591 | | struct bpf_struct_ops *st_ops; |
592 | | struct bpf_map *inner_map; |
593 | | void **init_slots; |
594 | | int init_slots_sz; |
595 | | char *pin_path; |
596 | | bool pinned; |
597 | | bool reused; |
598 | | bool autocreate; |
599 | | bool autoattach; |
600 | | __u64 map_extra; |
601 | | struct bpf_program *excl_prog; |
602 | | }; |
603 | | |
604 | | enum extern_type { |
605 | | EXT_UNKNOWN, |
606 | | EXT_KCFG, |
607 | | EXT_KSYM, |
608 | | }; |
609 | | |
610 | | enum kcfg_type { |
611 | | KCFG_UNKNOWN, |
612 | | KCFG_CHAR, |
613 | | KCFG_BOOL, |
614 | | KCFG_INT, |
615 | | KCFG_TRISTATE, |
616 | | KCFG_CHAR_ARR, |
617 | | }; |
618 | | |
619 | | struct extern_desc { |
620 | | enum extern_type type; |
621 | | int sym_idx; |
622 | | int btf_id; |
623 | | int sec_btf_id; |
624 | | char *name; |
625 | | char *essent_name; |
626 | | bool is_set; |
627 | | bool is_weak; |
628 | | union { |
629 | | struct { |
630 | | enum kcfg_type type; |
631 | | int sz; |
632 | | int align; |
633 | | int data_off; |
634 | | bool is_signed; |
635 | | } kcfg; |
636 | | struct { |
637 | | unsigned long long addr; |
638 | | |
639 | | /* target btf_id of the corresponding kernel var. */ |
640 | | int kernel_btf_obj_fd; |
641 | | int kernel_btf_id; |
642 | | |
643 | | /* local btf_id of the ksym extern's type. */ |
644 | | __u32 type_id; |
645 | | /* BTF fd index to be patched in for insn->off, this is |
646 | | * 0 for vmlinux BTF, index in obj->fd_array for module |
647 | | * BTF |
648 | | */ |
649 | | __s16 btf_fd_idx; |
650 | | } ksym; |
651 | | }; |
652 | | }; |
653 | | |
654 | | struct module_btf { |
655 | | struct btf *btf; |
656 | | char *name; |
657 | | __u32 id; |
658 | | int fd; |
659 | | int fd_array_idx; |
660 | | }; |
661 | | |
662 | | enum sec_type { |
663 | | SEC_UNUSED = 0, |
664 | | SEC_RELO, |
665 | | SEC_BSS, |
666 | | SEC_DATA, |
667 | | SEC_RODATA, |
668 | | SEC_ST_OPS, |
669 | | }; |
670 | | |
671 | | struct elf_sec_desc { |
672 | | enum sec_type sec_type; |
673 | | Elf64_Shdr *shdr; |
674 | | Elf_Data *data; |
675 | | }; |
676 | | |
677 | | struct elf_state { |
678 | | int fd; |
679 | | const void *obj_buf; |
680 | | size_t obj_buf_sz; |
681 | | Elf *elf; |
682 | | Elf64_Ehdr *ehdr; |
683 | | Elf_Data *symbols; |
684 | | Elf_Data *arena_data; |
685 | | size_t shstrndx; /* section index for section name strings */ |
686 | | size_t strtabidx; |
687 | | struct elf_sec_desc *secs; |
688 | | size_t sec_cnt; |
689 | | int btf_maps_shndx; |
690 | | __u32 btf_maps_sec_btf_id; |
691 | | int text_shndx; |
692 | | int symbols_shndx; |
693 | | bool has_st_ops; |
694 | | int arena_data_shndx; |
695 | | int jumptables_data_shndx; |
696 | | }; |
697 | | |
698 | | struct usdt_manager; |
699 | | |
700 | | enum bpf_object_state { |
701 | | OBJ_OPEN, |
702 | | OBJ_PREPARED, |
703 | | OBJ_LOADED, |
704 | | }; |
705 | | |
706 | | struct bpf_object { |
707 | | char name[BPF_OBJ_NAME_LEN]; |
708 | | char license[64]; |
709 | | __u32 kern_version; |
710 | | |
711 | | enum bpf_object_state state; |
712 | | struct bpf_program *programs; |
713 | | size_t nr_programs; |
714 | | struct bpf_map *maps; |
715 | | size_t nr_maps; |
716 | | size_t maps_cap; |
717 | | |
718 | | char *kconfig; |
719 | | struct extern_desc *externs; |
720 | | int nr_extern; |
721 | | int kconfig_map_idx; |
722 | | |
723 | | bool has_subcalls; |
724 | | bool has_rodata; |
725 | | |
726 | | struct bpf_gen *gen_loader; |
727 | | |
728 | | /* Information when doing ELF related work. Only valid if efile.elf is not NULL */ |
729 | | struct elf_state efile; |
730 | | |
731 | | unsigned char byteorder; |
732 | | |
733 | | struct btf *btf; |
734 | | struct btf_ext *btf_ext; |
735 | | |
736 | | /* Parse and load BTF vmlinux if any of the programs in the object need |
737 | | * it at load time. |
738 | | */ |
739 | | struct btf *btf_vmlinux; |
740 | | /* Path to the custom BTF to be used for BPF CO-RE relocations as an |
741 | | * override for vmlinux BTF. |
742 | | */ |
743 | | char *btf_custom_path; |
744 | | /* vmlinux BTF override for CO-RE relocations */ |
745 | | struct btf *btf_vmlinux_override; |
746 | | /* Lazily initialized kernel module BTFs */ |
747 | | struct module_btf *btf_modules; |
748 | | bool btf_modules_loaded; |
749 | | size_t btf_module_cnt; |
750 | | size_t btf_module_cap; |
751 | | |
752 | | /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */ |
753 | | char *log_buf; |
754 | | size_t log_size; |
755 | | __u32 log_level; |
756 | | |
757 | | int *fd_array; |
758 | | size_t fd_array_cap; |
759 | | size_t fd_array_cnt; |
760 | | |
761 | | struct usdt_manager *usdt_man; |
762 | | |
763 | | int arena_map_idx; |
764 | | void *arena_data; |
765 | | size_t arena_data_sz; |
766 | | size_t arena_data_off; |
767 | | |
768 | | void *jumptables_data; |
769 | | size_t jumptables_data_sz; |
770 | | |
771 | | struct { |
772 | | struct bpf_program *prog; |
773 | | unsigned int sym_off; |
774 | | int fd; |
775 | | } *jumptable_maps; |
776 | | size_t jumptable_map_cnt; |
777 | | |
778 | | struct kern_feature_cache *feat_cache; |
779 | | char *token_path; |
780 | | int token_fd; |
781 | | |
782 | | char path[]; |
783 | | }; |
784 | | |
785 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off); |
786 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off); |
787 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx); |
788 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name); |
789 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn); |
790 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn); |
791 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn); |
792 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx); |
793 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx); |
794 | | |
795 | | void bpf_program__unload(struct bpf_program *prog) |
796 | 20.8k | { |
797 | 20.8k | if (!prog) |
798 | 0 | return; |
799 | | |
800 | 20.8k | zclose(prog->fd); |
801 | | |
802 | 20.8k | zfree(&prog->func_info); |
803 | 20.8k | zfree(&prog->line_info); |
804 | 20.8k | zfree(&prog->subprogs); |
805 | 20.8k | } |
806 | | |
807 | | static void bpf_program__exit(struct bpf_program *prog) |
808 | 10.4k | { |
809 | 10.4k | if (!prog) |
810 | 0 | return; |
811 | | |
812 | 10.4k | bpf_program__unload(prog); |
813 | 10.4k | zfree(&prog->name); |
814 | 10.4k | zfree(&prog->sec_name); |
815 | 10.4k | zfree(&prog->insns); |
816 | 10.4k | zfree(&prog->reloc_desc); |
817 | | |
818 | 10.4k | prog->nr_reloc = 0; |
819 | 10.4k | prog->insns_cnt = 0; |
820 | 10.4k | prog->sec_idx = -1; |
821 | 10.4k | } |
822 | | |
823 | | static bool insn_is_subprog_call(const struct bpf_insn *insn) |
824 | 0 | { |
825 | 0 | return BPF_CLASS(insn->code) == BPF_JMP && |
826 | 0 | BPF_OP(insn->code) == BPF_CALL && |
827 | 0 | BPF_SRC(insn->code) == BPF_K && |
828 | 0 | insn->src_reg == BPF_PSEUDO_CALL && |
829 | 0 | insn->dst_reg == 0 && |
830 | 0 | insn->off == 0; |
831 | 0 | } |
832 | | |
833 | | static bool is_call_insn(const struct bpf_insn *insn) |
834 | 4.10k | { |
835 | 4.10k | return insn->code == (BPF_JMP | BPF_CALL); |
836 | 4.10k | } |
837 | | |
838 | | static bool insn_is_pseudo_func(struct bpf_insn *insn) |
839 | 0 | { |
840 | 0 | return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; |
841 | 0 | } |
842 | | |
843 | | static int |
844 | | bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, |
845 | | const char *name, size_t sec_idx, const char *sec_name, |
846 | | size_t sec_off, void *insn_data, size_t insn_data_sz) |
847 | 10.4k | { |
848 | 10.4k | if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) { |
849 | 23 | pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n", |
850 | 23 | sec_name, name, sec_off, insn_data_sz); |
851 | 23 | return -EINVAL; |
852 | 23 | } |
853 | | |
854 | 10.4k | memset(prog, 0, sizeof(*prog)); |
855 | 10.4k | prog->obj = obj; |
856 | | |
857 | 10.4k | prog->sec_idx = sec_idx; |
858 | 10.4k | prog->sec_insn_off = sec_off / BPF_INSN_SZ; |
859 | 10.4k | prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; |
860 | | /* insns_cnt can later be increased by appending used subprograms */ |
861 | 10.4k | prog->insns_cnt = prog->sec_insn_cnt; |
862 | | |
863 | 10.4k | prog->type = BPF_PROG_TYPE_UNSPEC; |
864 | 10.4k | prog->fd = -1; |
865 | 10.4k | prog->exception_cb_idx = -1; |
866 | | |
867 | | /* libbpf's convention for SEC("?abc...") is that it's just like |
868 | | * SEC("abc...") but the corresponding bpf_program starts out with |
869 | | * autoload set to false. |
870 | | */ |
871 | 10.4k | if (sec_name[0] == '?') { |
872 | 322 | prog->autoload = false; |
873 | | /* from now on forget there was ? in section name */ |
874 | 322 | sec_name++; |
875 | 10.1k | } else { |
876 | 10.1k | prog->autoload = true; |
877 | 10.1k | } |
878 | | |
879 | 10.4k | prog->autoattach = true; |
880 | | |
881 | | /* inherit object's log_level */ |
882 | 10.4k | prog->log_level = obj->log_level; |
883 | | |
884 | 10.4k | prog->sec_name = strdup(sec_name); |
885 | 10.4k | if (!prog->sec_name) |
886 | 0 | goto errout; |
887 | | |
888 | 10.4k | prog->name = strdup(name); |
889 | 10.4k | if (!prog->name) |
890 | 0 | goto errout; |
891 | | |
892 | 10.4k | prog->insns = malloc(insn_data_sz); |
893 | 10.4k | if (!prog->insns) |
894 | 0 | goto errout; |
895 | 10.4k | memcpy(prog->insns, insn_data, insn_data_sz); |
896 | | |
897 | 10.4k | return 0; |
898 | 0 | errout: |
899 | 0 | pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name); |
900 | 0 | bpf_program__exit(prog); |
901 | 0 | return -ENOMEM; |
902 | 10.4k | } |
903 | | |
904 | | static int |
905 | | bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data, |
906 | | const char *sec_name, int sec_idx) |
907 | 1.95k | { |
908 | 1.95k | Elf_Data *symbols = obj->efile.symbols; |
909 | 1.95k | struct bpf_program *prog, *progs; |
910 | 1.95k | void *data = sec_data->d_buf; |
911 | 1.95k | size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; |
912 | 1.95k | int nr_progs, err, i; |
913 | 1.95k | const char *name; |
914 | 1.95k | Elf64_Sym *sym; |
915 | | |
916 | 1.95k | progs = obj->programs; |
917 | 1.95k | nr_progs = obj->nr_programs; |
918 | 1.95k | nr_syms = symbols->d_size / sizeof(Elf64_Sym); |
919 | | |
920 | 175k | for (i = 0; i < nr_syms; i++) { |
921 | 173k | sym = elf_sym_by_idx(obj, i); |
922 | | |
923 | 173k | if (sym->st_shndx != sec_idx) |
924 | 161k | continue; |
925 | 12.3k | if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) |
926 | 1.67k | continue; |
927 | | |
928 | 10.7k | prog_sz = sym->st_size; |
929 | 10.7k | sec_off = sym->st_value; |
930 | | |
931 | 10.7k | name = elf_sym_str(obj, sym->st_name); |
932 | 10.7k | if (!name) { |
933 | 56 | pr_warn("sec '%s': failed to get symbol name for offset %zu\n", |
934 | 56 | sec_name, sec_off); |
935 | 56 | return -LIBBPF_ERRNO__FORMAT; |
936 | 56 | } |
937 | | |
938 | 10.6k | if (sec_off + prog_sz > sec_sz || sec_off + prog_sz < sec_off) { |
939 | 182 | pr_warn("sec '%s': program at offset %zu crosses section boundary\n", |
940 | 182 | sec_name, sec_off); |
941 | 182 | return -LIBBPF_ERRNO__FORMAT; |
942 | 182 | } |
943 | | |
944 | 10.4k | if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { |
945 | 4 | pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name); |
946 | 4 | return -ENOTSUP; |
947 | 4 | } |
948 | | |
949 | 10.4k | pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n", |
950 | 20.9k | sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz); |
951 | | |
952 | 10.4k | progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs)); |
953 | 10.4k | if (!progs) { |
954 | | /* |
955 | | * In this case the original obj->programs |
956 | | * is still valid, so don't need special treat for |
957 | | * bpf_close_object(). |
958 | | */ |
959 | 0 | pr_warn("sec '%s': failed to alloc memory for new program '%s'\n", |
960 | 0 | sec_name, name); |
961 | 0 | return -ENOMEM; |
962 | 0 | } |
963 | 10.4k | obj->programs = progs; |
964 | | |
965 | 10.4k | prog = &progs[nr_progs]; |
966 | | |
967 | 10.4k | err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name, |
968 | 10.4k | sec_off, data + sec_off, prog_sz); |
969 | 10.4k | if (err) |
970 | 23 | return err; |
971 | | |
972 | 10.4k | if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) |
973 | 10.2k | prog->sym_global = true; |
974 | | |
975 | | /* if function is a global/weak symbol, but has restricted |
976 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC |
977 | | * as static to enable more permissive BPF verification mode |
978 | | * with more outside context available to BPF verifier |
979 | | */ |
980 | 10.4k | if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
981 | 4.51k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) |
982 | 7.71k | prog->mark_btf_static = true; |
983 | | |
984 | 10.4k | nr_progs++; |
985 | 10.4k | obj->nr_programs = nr_progs; |
986 | 10.4k | } |
987 | | |
988 | 1.68k | return 0; |
989 | 1.95k | } |
990 | | |
991 | | static void bpf_object_bswap_progs(struct bpf_object *obj) |
992 | 92 | { |
993 | 92 | struct bpf_program *prog = obj->programs; |
994 | 92 | struct bpf_insn *insn; |
995 | 92 | int p, i; |
996 | | |
997 | 1.70k | for (p = 0; p < obj->nr_programs; p++, prog++) { |
998 | 1.61k | insn = prog->insns; |
999 | 32.2k | for (i = 0; i < prog->insns_cnt; i++, insn++) |
1000 | 30.6k | bpf_insn_bswap(insn); |
1001 | 1.61k | } |
1002 | 92 | pr_debug("converted %zu BPF programs to native byte order\n", obj->nr_programs); |
1003 | 92 | } |
1004 | | |
1005 | | static const struct btf_member * |
1006 | | find_member_by_offset(const struct btf_type *t, __u32 bit_offset) |
1007 | 1 | { |
1008 | 1 | struct btf_member *m; |
1009 | 1 | int i; |
1010 | | |
1011 | 2 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
1012 | 1 | if (btf_member_bit_offset(t, i) == bit_offset) |
1013 | 0 | return m; |
1014 | 1 | } |
1015 | | |
1016 | 1 | return NULL; |
1017 | 1 | } |
1018 | | |
1019 | | static const struct btf_member * |
1020 | | find_member_by_name(const struct btf *btf, const struct btf_type *t, |
1021 | | const char *name) |
1022 | 0 | { |
1023 | 0 | struct btf_member *m; |
1024 | 0 | int i; |
1025 | |
|
1026 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
1027 | 0 | if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) |
1028 | 0 | return m; |
1029 | 0 | } |
1030 | | |
1031 | 0 | return NULL; |
1032 | 0 | } |
1033 | | |
1034 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
1035 | | __u16 kind, struct btf **res_btf, |
1036 | | struct module_btf **res_mod_btf); |
1037 | | |
1038 | 0 | #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" |
1039 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
1040 | | const char *name, __u32 kind); |
1041 | | |
1042 | | static int |
1043 | | find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw, |
1044 | | struct module_btf **mod_btf, |
1045 | | const struct btf_type **type, __u32 *type_id, |
1046 | | const struct btf_type **vtype, __u32 *vtype_id, |
1047 | | const struct btf_member **data_member) |
1048 | 0 | { |
1049 | 0 | const struct btf_type *kern_type, *kern_vtype; |
1050 | 0 | const struct btf_member *kern_data_member; |
1051 | 0 | struct btf *btf = NULL; |
1052 | 0 | __s32 kern_vtype_id, kern_type_id; |
1053 | 0 | char tname[192], stname[256]; |
1054 | 0 | __u32 i; |
1055 | |
|
1056 | 0 | snprintf(tname, sizeof(tname), "%.*s", |
1057 | 0 | (int)bpf_core_essential_name_len(tname_raw), tname_raw); |
1058 | |
|
1059 | 0 | snprintf(stname, sizeof(stname), "%s%s", STRUCT_OPS_VALUE_PREFIX, tname); |
1060 | | |
1061 | | /* Look for the corresponding "map_value" type that will be used |
1062 | | * in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf |
1063 | | * and the mod_btf. |
1064 | | * For example, find "struct bpf_struct_ops_tcp_congestion_ops". |
1065 | | */ |
1066 | 0 | kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT, &btf, mod_btf); |
1067 | 0 | if (kern_vtype_id < 0) { |
1068 | 0 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", stname); |
1069 | 0 | return kern_vtype_id; |
1070 | 0 | } |
1071 | 0 | kern_vtype = btf__type_by_id(btf, kern_vtype_id); |
1072 | |
|
1073 | 0 | kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT); |
1074 | 0 | if (kern_type_id < 0) { |
1075 | 0 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname); |
1076 | 0 | return kern_type_id; |
1077 | 0 | } |
1078 | 0 | kern_type = btf__type_by_id(btf, kern_type_id); |
1079 | | |
1080 | | /* Find "struct tcp_congestion_ops" from |
1081 | | * struct bpf_struct_ops_tcp_congestion_ops { |
1082 | | * [ ... ] |
1083 | | * struct tcp_congestion_ops data; |
1084 | | * } |
1085 | | */ |
1086 | 0 | kern_data_member = btf_members(kern_vtype); |
1087 | 0 | for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) { |
1088 | 0 | if (kern_data_member->type == kern_type_id) |
1089 | 0 | break; |
1090 | 0 | } |
1091 | 0 | if (i == btf_vlen(kern_vtype)) { |
1092 | 0 | pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n", |
1093 | 0 | tname, stname); |
1094 | 0 | return -EINVAL; |
1095 | 0 | } |
1096 | | |
1097 | 0 | *type = kern_type; |
1098 | 0 | *type_id = kern_type_id; |
1099 | 0 | *vtype = kern_vtype; |
1100 | 0 | *vtype_id = kern_vtype_id; |
1101 | 0 | *data_member = kern_data_member; |
1102 | |
|
1103 | 0 | return 0; |
1104 | 0 | } |
1105 | | |
1106 | | static bool bpf_map__is_struct_ops(const struct bpf_map *map) |
1107 | 608 | { |
1108 | 608 | return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; |
1109 | 608 | } |
1110 | | |
1111 | | static bool is_valid_st_ops_program(struct bpf_object *obj, |
1112 | | const struct bpf_program *prog) |
1113 | 0 | { |
1114 | 0 | int i; |
1115 | |
|
1116 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
1117 | 0 | if (&obj->programs[i] == prog) |
1118 | 0 | return prog->type == BPF_PROG_TYPE_STRUCT_OPS; |
1119 | 0 | } |
1120 | | |
1121 | 0 | return false; |
1122 | 0 | } |
1123 | | |
1124 | | /* For each struct_ops program P, referenced from some struct_ops map M, |
1125 | | * enable P.autoload if there are Ms for which M.autocreate is true, |
1126 | | * disable P.autoload if for all Ms M.autocreate is false. |
1127 | | * Don't change P.autoload for programs that are not referenced from any maps. |
1128 | | */ |
1129 | | static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj) |
1130 | 0 | { |
1131 | 0 | struct bpf_program *prog, *slot_prog; |
1132 | 0 | struct bpf_map *map; |
1133 | 0 | int i, j, k, vlen; |
1134 | |
|
1135 | 0 | for (i = 0; i < obj->nr_programs; ++i) { |
1136 | 0 | int should_load = false; |
1137 | 0 | int use_cnt = 0; |
1138 | |
|
1139 | 0 | prog = &obj->programs[i]; |
1140 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) |
1141 | 0 | continue; |
1142 | | |
1143 | 0 | for (j = 0; j < obj->nr_maps; ++j) { |
1144 | 0 | const struct btf_type *type; |
1145 | |
|
1146 | 0 | map = &obj->maps[j]; |
1147 | 0 | if (!bpf_map__is_struct_ops(map)) |
1148 | 0 | continue; |
1149 | | |
1150 | 0 | type = btf__type_by_id(obj->btf, map->st_ops->type_id); |
1151 | 0 | vlen = btf_vlen(type); |
1152 | 0 | for (k = 0; k < vlen; ++k) { |
1153 | 0 | slot_prog = map->st_ops->progs[k]; |
1154 | 0 | if (prog != slot_prog) |
1155 | 0 | continue; |
1156 | | |
1157 | 0 | use_cnt++; |
1158 | 0 | if (map->autocreate) |
1159 | 0 | should_load = true; |
1160 | 0 | } |
1161 | 0 | } |
1162 | 0 | if (use_cnt) |
1163 | 0 | prog->autoload = should_load; |
1164 | 0 | } |
1165 | |
|
1166 | 0 | return 0; |
1167 | 0 | } |
1168 | | |
1169 | | /* Init the map's fields that depend on kern_btf */ |
1170 | | static int bpf_map__init_kern_struct_ops(struct bpf_map *map) |
1171 | 0 | { |
1172 | 0 | const struct btf_member *member, *kern_member, *kern_data_member; |
1173 | 0 | const struct btf_type *type, *kern_type, *kern_vtype; |
1174 | 0 | __u32 i, kern_type_id, kern_vtype_id, kern_data_off; |
1175 | 0 | struct bpf_object *obj = map->obj; |
1176 | 0 | const struct btf *btf = obj->btf; |
1177 | 0 | struct bpf_struct_ops *st_ops; |
1178 | 0 | const struct btf *kern_btf; |
1179 | 0 | struct module_btf *mod_btf = NULL; |
1180 | 0 | void *data, *kern_data; |
1181 | 0 | const char *tname; |
1182 | 0 | int err; |
1183 | |
|
1184 | 0 | st_ops = map->st_ops; |
1185 | 0 | type = btf__type_by_id(btf, st_ops->type_id); |
1186 | 0 | tname = btf__name_by_offset(btf, type->name_off); |
1187 | 0 | err = find_struct_ops_kern_types(obj, tname, &mod_btf, |
1188 | 0 | &kern_type, &kern_type_id, |
1189 | 0 | &kern_vtype, &kern_vtype_id, |
1190 | 0 | &kern_data_member); |
1191 | 0 | if (err) |
1192 | 0 | return err; |
1193 | | |
1194 | 0 | kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux; |
1195 | |
|
1196 | 0 | pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n", |
1197 | 0 | map->name, st_ops->type_id, kern_type_id, kern_vtype_id); |
1198 | |
|
1199 | 0 | map->mod_btf_fd = mod_btf ? mod_btf->fd : -1; |
1200 | 0 | map->def.value_size = kern_vtype->size; |
1201 | 0 | map->btf_vmlinux_value_type_id = kern_vtype_id; |
1202 | |
|
1203 | 0 | st_ops->kern_vdata = calloc(1, kern_vtype->size); |
1204 | 0 | if (!st_ops->kern_vdata) |
1205 | 0 | return -ENOMEM; |
1206 | | |
1207 | 0 | data = st_ops->data; |
1208 | 0 | kern_data_off = kern_data_member->offset / 8; |
1209 | 0 | kern_data = st_ops->kern_vdata + kern_data_off; |
1210 | |
|
1211 | 0 | member = btf_members(type); |
1212 | 0 | for (i = 0; i < btf_vlen(type); i++, member++) { |
1213 | 0 | const struct btf_type *mtype, *kern_mtype; |
1214 | 0 | __u32 mtype_id, kern_mtype_id; |
1215 | 0 | void *mdata, *kern_mdata; |
1216 | 0 | struct bpf_program *prog; |
1217 | 0 | __s64 msize, kern_msize; |
1218 | 0 | __u32 moff, kern_moff; |
1219 | 0 | __u32 kern_member_idx; |
1220 | 0 | const char *mname; |
1221 | |
|
1222 | 0 | mname = btf__name_by_offset(btf, member->name_off); |
1223 | 0 | moff = member->offset / 8; |
1224 | 0 | mdata = data + moff; |
1225 | 0 | msize = btf__resolve_size(btf, member->type); |
1226 | 0 | if (msize < 0) { |
1227 | 0 | pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n", |
1228 | 0 | map->name, mname); |
1229 | 0 | return msize; |
1230 | 0 | } |
1231 | | |
1232 | 0 | kern_member = find_member_by_name(kern_btf, kern_type, mname); |
1233 | 0 | if (!kern_member) { |
1234 | 0 | if (!libbpf_is_mem_zeroed(mdata, msize)) { |
1235 | 0 | pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", |
1236 | 0 | map->name, mname); |
1237 | 0 | return -ENOTSUP; |
1238 | 0 | } |
1239 | | |
1240 | 0 | if (st_ops->progs[i]) { |
1241 | | /* If we had declaratively set struct_ops callback, we need to |
1242 | | * force its autoload to false, because it doesn't have |
1243 | | * a chance of succeeding from POV of the current struct_ops map. |
1244 | | * If this program is still referenced somewhere else, though, |
1245 | | * then bpf_object_adjust_struct_ops_autoload() will update its |
1246 | | * autoload accordingly. |
1247 | | */ |
1248 | 0 | st_ops->progs[i]->autoload = false; |
1249 | 0 | st_ops->progs[i] = NULL; |
1250 | 0 | } |
1251 | | |
1252 | | /* Skip all-zero/NULL fields if they are not present in the kernel BTF */ |
1253 | 0 | pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n", |
1254 | 0 | map->name, mname); |
1255 | 0 | continue; |
1256 | 0 | } |
1257 | | |
1258 | 0 | kern_member_idx = kern_member - btf_members(kern_type); |
1259 | 0 | if (btf_member_bitfield_size(type, i) || |
1260 | 0 | btf_member_bitfield_size(kern_type, kern_member_idx)) { |
1261 | 0 | pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n", |
1262 | 0 | map->name, mname); |
1263 | 0 | return -ENOTSUP; |
1264 | 0 | } |
1265 | | |
1266 | 0 | kern_moff = kern_member->offset / 8; |
1267 | 0 | kern_mdata = kern_data + kern_moff; |
1268 | |
|
1269 | 0 | mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); |
1270 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, |
1271 | 0 | &kern_mtype_id); |
1272 | 0 | if (BTF_INFO_KIND(mtype->info) != |
1273 | 0 | BTF_INFO_KIND(kern_mtype->info)) { |
1274 | 0 | pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n", |
1275 | 0 | map->name, mname, BTF_INFO_KIND(mtype->info), |
1276 | 0 | BTF_INFO_KIND(kern_mtype->info)); |
1277 | 0 | return -ENOTSUP; |
1278 | 0 | } |
1279 | | |
1280 | 0 | if (btf_is_ptr(mtype)) { |
1281 | 0 | prog = *(void **)mdata; |
1282 | | /* just like for !kern_member case above, reset declaratively |
1283 | | * set (at compile time) program's autload to false, |
1284 | | * if user replaced it with another program or NULL |
1285 | | */ |
1286 | 0 | if (st_ops->progs[i] && st_ops->progs[i] != prog) |
1287 | 0 | st_ops->progs[i]->autoload = false; |
1288 | | |
1289 | | /* Update the value from the shadow type */ |
1290 | 0 | st_ops->progs[i] = prog; |
1291 | 0 | if (!prog) |
1292 | 0 | continue; |
1293 | | |
1294 | 0 | if (!is_valid_st_ops_program(obj, prog)) { |
1295 | 0 | pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n", |
1296 | 0 | map->name, mname); |
1297 | 0 | return -ENOTSUP; |
1298 | 0 | } |
1299 | | |
1300 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, |
1301 | 0 | kern_mtype->type, |
1302 | 0 | &kern_mtype_id); |
1303 | | |
1304 | | /* mtype->type must be a func_proto which was |
1305 | | * guaranteed in bpf_object__collect_st_ops_relos(), |
1306 | | * so only check kern_mtype for func_proto here. |
1307 | | */ |
1308 | 0 | if (!btf_is_func_proto(kern_mtype)) { |
1309 | 0 | pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n", |
1310 | 0 | map->name, mname); |
1311 | 0 | return -ENOTSUP; |
1312 | 0 | } |
1313 | | |
1314 | 0 | if (mod_btf) |
1315 | 0 | prog->attach_btf_obj_fd = mod_btf->fd; |
1316 | | |
1317 | | /* if we haven't yet processed this BPF program, record proper |
1318 | | * attach_btf_id and member_idx |
1319 | | */ |
1320 | 0 | if (!prog->attach_btf_id) { |
1321 | 0 | prog->attach_btf_id = kern_type_id; |
1322 | 0 | prog->expected_attach_type = kern_member_idx; |
1323 | 0 | } |
1324 | | |
1325 | | /* struct_ops BPF prog can be re-used between multiple |
1326 | | * .struct_ops & .struct_ops.link as long as it's the |
1327 | | * same struct_ops struct definition and the same |
1328 | | * function pointer field |
1329 | | */ |
1330 | 0 | if (prog->attach_btf_id != kern_type_id) { |
1331 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: attach_btf_id %u != kern_type_id %u\n", |
1332 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1333 | 0 | prog->attach_btf_id, kern_type_id); |
1334 | 0 | return -EINVAL; |
1335 | 0 | } |
1336 | 0 | if (prog->expected_attach_type != kern_member_idx) { |
1337 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: expected_attach_type %u != kern_member_idx %u\n", |
1338 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1339 | 0 | prog->expected_attach_type, kern_member_idx); |
1340 | 0 | return -EINVAL; |
1341 | 0 | } |
1342 | | |
1343 | 0 | st_ops->kern_func_off[i] = kern_data_off + kern_moff; |
1344 | |
|
1345 | 0 | pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n", |
1346 | 0 | map->name, mname, prog->name, moff, |
1347 | 0 | kern_moff); |
1348 | |
|
1349 | 0 | continue; |
1350 | 0 | } |
1351 | | |
1352 | 0 | kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); |
1353 | 0 | if (kern_msize < 0 || msize != kern_msize) { |
1354 | 0 | pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", |
1355 | 0 | map->name, mname, (ssize_t)msize, |
1356 | 0 | (ssize_t)kern_msize); |
1357 | 0 | return -ENOTSUP; |
1358 | 0 | } |
1359 | | |
1360 | 0 | pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n", |
1361 | 0 | map->name, mname, (unsigned int)msize, |
1362 | 0 | moff, kern_moff); |
1363 | 0 | memcpy(kern_mdata, mdata, msize); |
1364 | 0 | } |
1365 | | |
1366 | 0 | return 0; |
1367 | 0 | } |
1368 | | |
1369 | | static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) |
1370 | 0 | { |
1371 | 0 | struct bpf_map *map; |
1372 | 0 | size_t i; |
1373 | 0 | int err; |
1374 | |
|
1375 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
1376 | 0 | map = &obj->maps[i]; |
1377 | |
|
1378 | 0 | if (!bpf_map__is_struct_ops(map)) |
1379 | 0 | continue; |
1380 | | |
1381 | 0 | if (!map->autocreate) |
1382 | 0 | continue; |
1383 | | |
1384 | 0 | err = bpf_map__init_kern_struct_ops(map); |
1385 | 0 | if (err) |
1386 | 0 | return err; |
1387 | 0 | } |
1388 | | |
1389 | 0 | return 0; |
1390 | 0 | } |
1391 | | |
1392 | | static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, |
1393 | | int shndx, Elf_Data *data) |
1394 | 134 | { |
1395 | 134 | const struct btf_type *type, *datasec; |
1396 | 134 | const struct btf_var_secinfo *vsi; |
1397 | 134 | struct bpf_struct_ops *st_ops; |
1398 | 134 | const char *tname, *var_name; |
1399 | 134 | __s32 type_id, datasec_id; |
1400 | 134 | const struct btf *btf; |
1401 | 134 | struct bpf_map *map; |
1402 | 134 | __u32 i; |
1403 | | |
1404 | 134 | if (shndx == -1) |
1405 | 0 | return 0; |
1406 | | |
1407 | 134 | btf = obj->btf; |
1408 | 134 | datasec_id = btf__find_by_name_kind(btf, sec_name, |
1409 | 134 | BTF_KIND_DATASEC); |
1410 | 134 | if (datasec_id < 0) { |
1411 | 4 | pr_warn("struct_ops init: DATASEC %s not found\n", |
1412 | 4 | sec_name); |
1413 | 4 | return -EINVAL; |
1414 | 4 | } |
1415 | | |
1416 | 130 | datasec = btf__type_by_id(btf, datasec_id); |
1417 | 130 | vsi = btf_var_secinfos(datasec); |
1418 | 206 | for (i = 0; i < btf_vlen(datasec); i++, vsi++) { |
1419 | 124 | type = btf__type_by_id(obj->btf, vsi->type); |
1420 | 124 | var_name = btf__name_by_offset(obj->btf, type->name_off); |
1421 | | |
1422 | 124 | type_id = btf__resolve_type(obj->btf, vsi->type); |
1423 | 124 | if (type_id < 0) { |
1424 | 14 | pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n", |
1425 | 14 | vsi->type, sec_name); |
1426 | 14 | return -EINVAL; |
1427 | 14 | } |
1428 | | |
1429 | 110 | type = btf__type_by_id(obj->btf, type_id); |
1430 | 110 | tname = btf__name_by_offset(obj->btf, type->name_off); |
1431 | 110 | if (!tname[0]) { |
1432 | 6 | pr_warn("struct_ops init: anonymous type is not supported\n"); |
1433 | 6 | return -ENOTSUP; |
1434 | 6 | } |
1435 | 104 | if (!btf_is_struct(type)) { |
1436 | 8 | pr_warn("struct_ops init: %s is not a struct\n", tname); |
1437 | 8 | return -EINVAL; |
1438 | 8 | } |
1439 | | |
1440 | 96 | map = bpf_object__add_map(obj); |
1441 | 96 | if (IS_ERR(map)) |
1442 | 0 | return PTR_ERR(map); |
1443 | | |
1444 | 96 | map->sec_idx = shndx; |
1445 | 96 | map->sec_offset = vsi->offset; |
1446 | 96 | map->name = strdup(var_name); |
1447 | 96 | if (!map->name) |
1448 | 0 | return -ENOMEM; |
1449 | 96 | map->btf_value_type_id = type_id; |
1450 | | |
1451 | | /* Follow same convention as for programs autoload: |
1452 | | * SEC("?.struct_ops") means map is not created by default. |
1453 | | */ |
1454 | 96 | if (sec_name[0] == '?') { |
1455 | 0 | map->autocreate = false; |
1456 | | /* from now on forget there was ? in section name */ |
1457 | 0 | sec_name++; |
1458 | 0 | } |
1459 | | |
1460 | 96 | map->def.type = BPF_MAP_TYPE_STRUCT_OPS; |
1461 | 96 | map->def.key_size = sizeof(int); |
1462 | 96 | map->def.value_size = type->size; |
1463 | 96 | map->def.max_entries = 1; |
1464 | 96 | map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0; |
1465 | 96 | map->autoattach = true; |
1466 | | |
1467 | 96 | map->st_ops = calloc(1, sizeof(*map->st_ops)); |
1468 | 96 | if (!map->st_ops) |
1469 | 0 | return -ENOMEM; |
1470 | 96 | st_ops = map->st_ops; |
1471 | 96 | st_ops->data = malloc(type->size); |
1472 | 96 | st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); |
1473 | 96 | st_ops->kern_func_off = malloc(btf_vlen(type) * |
1474 | 96 | sizeof(*st_ops->kern_func_off)); |
1475 | 96 | if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) |
1476 | 0 | return -ENOMEM; |
1477 | | |
1478 | 96 | if (vsi->offset + type->size > data->d_size) { |
1479 | 20 | pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n", |
1480 | 20 | var_name, sec_name); |
1481 | 20 | return -EINVAL; |
1482 | 20 | } |
1483 | | |
1484 | 76 | memcpy(st_ops->data, |
1485 | 76 | data->d_buf + vsi->offset, |
1486 | 76 | type->size); |
1487 | 76 | st_ops->type_id = type_id; |
1488 | | |
1489 | 76 | pr_debug("struct_ops init: struct %s(type_id=%d) %s found at offset %u\n", |
1490 | 76 | tname, type_id, var_name, vsi->offset); |
1491 | 76 | } |
1492 | | |
1493 | 82 | return 0; |
1494 | 130 | } |
1495 | | |
1496 | | static int bpf_object_init_struct_ops(struct bpf_object *obj) |
1497 | 2.13k | { |
1498 | 2.13k | const char *sec_name; |
1499 | 2.13k | int sec_idx, err; |
1500 | | |
1501 | 22.8k | for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) { |
1502 | 20.7k | struct elf_sec_desc *desc = &obj->efile.secs[sec_idx]; |
1503 | | |
1504 | 20.7k | if (desc->sec_type != SEC_ST_OPS) |
1505 | 20.6k | continue; |
1506 | | |
1507 | 134 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1508 | 134 | if (!sec_name) |
1509 | 0 | return -LIBBPF_ERRNO__FORMAT; |
1510 | | |
1511 | 134 | err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data); |
1512 | 134 | if (err) |
1513 | 52 | return err; |
1514 | 134 | } |
1515 | | |
1516 | 2.07k | return 0; |
1517 | 2.13k | } |
1518 | | |
1519 | | static struct bpf_object *bpf_object__new(const char *path, |
1520 | | const void *obj_buf, |
1521 | | size_t obj_buf_sz, |
1522 | | const char *obj_name) |
1523 | 10.9k | { |
1524 | 10.9k | struct bpf_object *obj; |
1525 | 10.9k | char *end; |
1526 | | |
1527 | 10.9k | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
1528 | 10.9k | if (!obj) { |
1529 | 0 | pr_warn("alloc memory failed for %s\n", path); |
1530 | 0 | return ERR_PTR(-ENOMEM); |
1531 | 0 | } |
1532 | | |
1533 | 10.9k | strcpy(obj->path, path); |
1534 | 10.9k | if (obj_name) { |
1535 | 10.9k | libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); |
1536 | 10.9k | } else { |
1537 | | /* Using basename() GNU version which doesn't modify arg. */ |
1538 | 0 | libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); |
1539 | 0 | end = strchr(obj->name, '.'); |
1540 | 0 | if (end) |
1541 | 0 | *end = 0; |
1542 | 0 | } |
1543 | | |
1544 | 10.9k | obj->efile.fd = -1; |
1545 | | /* |
1546 | | * Caller of this function should also call |
1547 | | * bpf_object__elf_finish() after data collection to return |
1548 | | * obj_buf to user. If not, we should duplicate the buffer to |
1549 | | * avoid user freeing them before elf finish. |
1550 | | */ |
1551 | 10.9k | obj->efile.obj_buf = obj_buf; |
1552 | 10.9k | obj->efile.obj_buf_sz = obj_buf_sz; |
1553 | 10.9k | obj->efile.btf_maps_shndx = -1; |
1554 | 10.9k | obj->kconfig_map_idx = -1; |
1555 | 10.9k | obj->arena_map_idx = -1; |
1556 | | |
1557 | 10.9k | obj->kern_version = get_kernel_version(); |
1558 | 10.9k | obj->state = OBJ_OPEN; |
1559 | | |
1560 | 10.9k | return obj; |
1561 | 10.9k | } |
1562 | | |
1563 | | static void bpf_object__elf_finish(struct bpf_object *obj) |
1564 | 14.7k | { |
1565 | 14.7k | if (!obj->efile.elf) |
1566 | 3.84k | return; |
1567 | | |
1568 | 10.8k | elf_end(obj->efile.elf); |
1569 | 10.8k | obj->efile.elf = NULL; |
1570 | 10.8k | obj->efile.ehdr = NULL; |
1571 | 10.8k | obj->efile.symbols = NULL; |
1572 | 10.8k | obj->efile.arena_data = NULL; |
1573 | | |
1574 | 10.8k | zfree(&obj->efile.secs); |
1575 | 10.8k | obj->efile.sec_cnt = 0; |
1576 | 10.8k | zclose(obj->efile.fd); |
1577 | 10.8k | obj->efile.obj_buf = NULL; |
1578 | 10.8k | obj->efile.obj_buf_sz = 0; |
1579 | 10.8k | } |
1580 | | |
1581 | | static int bpf_object__elf_init(struct bpf_object *obj) |
1582 | 10.9k | { |
1583 | 10.9k | Elf64_Ehdr *ehdr; |
1584 | 10.9k | int err = 0; |
1585 | 10.9k | Elf *elf; |
1586 | | |
1587 | 10.9k | if (obj->efile.elf) { |
1588 | 0 | pr_warn("elf: init internal error\n"); |
1589 | 0 | return -LIBBPF_ERRNO__LIBELF; |
1590 | 0 | } |
1591 | | |
1592 | 10.9k | if (obj->efile.obj_buf_sz > 0) { |
1593 | | /* obj_buf should have been validated by bpf_object__open_mem(). */ |
1594 | 10.9k | elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); |
1595 | 10.9k | } else { |
1596 | 0 | obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); |
1597 | 0 | if (obj->efile.fd < 0) { |
1598 | 0 | err = -errno; |
1599 | 0 | pr_warn("elf: failed to open %s: %s\n", obj->path, errstr(err)); |
1600 | 0 | return err; |
1601 | 0 | } |
1602 | | |
1603 | 0 | elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); |
1604 | 0 | } |
1605 | | |
1606 | 10.9k | if (!elf) { |
1607 | 95 | pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); |
1608 | 95 | err = -LIBBPF_ERRNO__LIBELF; |
1609 | 95 | goto errout; |
1610 | 95 | } |
1611 | | |
1612 | 10.8k | obj->efile.elf = elf; |
1613 | | |
1614 | 10.8k | if (elf_kind(elf) != ELF_K_ELF) { |
1615 | 113 | err = -LIBBPF_ERRNO__FORMAT; |
1616 | 113 | pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); |
1617 | 113 | goto errout; |
1618 | 113 | } |
1619 | | |
1620 | 10.7k | if (gelf_getclass(elf) != ELFCLASS64) { |
1621 | 493 | err = -LIBBPF_ERRNO__FORMAT; |
1622 | 493 | pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); |
1623 | 493 | goto errout; |
1624 | 493 | } |
1625 | | |
1626 | 10.2k | obj->efile.ehdr = ehdr = elf64_getehdr(elf); |
1627 | 10.2k | if (!obj->efile.ehdr) { |
1628 | 0 | pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); |
1629 | 0 | err = -LIBBPF_ERRNO__FORMAT; |
1630 | 0 | goto errout; |
1631 | 0 | } |
1632 | | |
1633 | | /* Validate ELF object endianness... */ |
1634 | 10.2k | if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB && |
1635 | 2.14k | ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { |
1636 | 0 | err = -LIBBPF_ERRNO__ENDIAN; |
1637 | 0 | pr_warn("elf: '%s' has unknown byte order\n", obj->path); |
1638 | 0 | goto errout; |
1639 | 0 | } |
1640 | | /* and save after bpf_object_open() frees ELF data */ |
1641 | 10.2k | obj->byteorder = ehdr->e_ident[EI_DATA]; |
1642 | | |
1643 | 10.2k | if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { |
1644 | 23 | pr_warn("elf: failed to get section names section index for %s: %s\n", |
1645 | 23 | obj->path, elf_errmsg(-1)); |
1646 | 23 | err = -LIBBPF_ERRNO__FORMAT; |
1647 | 23 | goto errout; |
1648 | 23 | } |
1649 | | |
1650 | | /* ELF is corrupted/truncated, avoid calling elf_strptr. */ |
1651 | 10.2k | if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { |
1652 | 1.13k | pr_warn("elf: failed to get section names strings from %s: %s\n", |
1653 | 1.13k | obj->path, elf_errmsg(-1)); |
1654 | 1.13k | err = -LIBBPF_ERRNO__FORMAT; |
1655 | 1.13k | goto errout; |
1656 | 1.13k | } |
1657 | | |
1658 | | /* Old LLVM set e_machine to EM_NONE */ |
1659 | 9.10k | if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { |
1660 | 355 | pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); |
1661 | 355 | err = -LIBBPF_ERRNO__FORMAT; |
1662 | 355 | goto errout; |
1663 | 355 | } |
1664 | | |
1665 | 8.75k | return 0; |
1666 | 2.21k | errout: |
1667 | 2.21k | bpf_object__elf_finish(obj); |
1668 | 2.21k | return err; |
1669 | 9.10k | } |
1670 | | |
1671 | | static bool is_native_endianness(struct bpf_object *obj) |
1672 | 5.19k | { |
1673 | 5.19k | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
1674 | 5.19k | return obj->byteorder == ELFDATA2LSB; |
1675 | | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
1676 | | return obj->byteorder == ELFDATA2MSB; |
1677 | | #else |
1678 | | # error "Unrecognized __BYTE_ORDER__" |
1679 | | #endif |
1680 | 5.19k | } |
1681 | | |
1682 | | static int |
1683 | | bpf_object__init_license(struct bpf_object *obj, void *data, size_t size) |
1684 | 949 | { |
1685 | 949 | if (!data) { |
1686 | 5 | pr_warn("invalid license section in %s\n", obj->path); |
1687 | 5 | return -LIBBPF_ERRNO__FORMAT; |
1688 | 5 | } |
1689 | | /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't |
1690 | | * go over allowed ELF data section buffer |
1691 | | */ |
1692 | 944 | libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); |
1693 | 944 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
1694 | 944 | return 0; |
1695 | 949 | } |
1696 | | |
1697 | | static int |
1698 | | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
1699 | 70 | { |
1700 | 70 | __u32 kver; |
1701 | | |
1702 | 70 | if (!data || size != sizeof(kver)) { |
1703 | 13 | pr_warn("invalid kver section in %s\n", obj->path); |
1704 | 13 | return -LIBBPF_ERRNO__FORMAT; |
1705 | 13 | } |
1706 | 57 | memcpy(&kver, data, sizeof(kver)); |
1707 | 57 | obj->kern_version = kver; |
1708 | 57 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
1709 | 57 | return 0; |
1710 | 70 | } |
1711 | | |
1712 | | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
1713 | 0 | { |
1714 | 0 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
1715 | 0 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
1716 | 0 | return true; |
1717 | 0 | return false; |
1718 | 0 | } |
1719 | | |
1720 | | static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size) |
1721 | 605 | { |
1722 | 605 | Elf_Data *data; |
1723 | 605 | Elf_Scn *scn; |
1724 | | |
1725 | 605 | if (!name) |
1726 | 0 | return -EINVAL; |
1727 | | |
1728 | 605 | scn = elf_sec_by_name(obj, name); |
1729 | 605 | data = elf_sec_data(obj, scn); |
1730 | 605 | if (data) { |
1731 | 303 | *size = data->d_size; |
1732 | 303 | return 0; /* found it */ |
1733 | 303 | } |
1734 | | |
1735 | 302 | return -ENOENT; |
1736 | 605 | } |
1737 | | |
1738 | | static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name) |
1739 | 868 | { |
1740 | 868 | Elf_Data *symbols = obj->efile.symbols; |
1741 | 868 | const char *sname; |
1742 | 868 | size_t si; |
1743 | | |
1744 | 21.3k | for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { |
1745 | 21.0k | Elf64_Sym *sym = elf_sym_by_idx(obj, si); |
1746 | | |
1747 | 21.0k | if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) |
1748 | 17.6k | continue; |
1749 | | |
1750 | 3.41k | if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && |
1751 | 2.74k | ELF64_ST_BIND(sym->st_info) != STB_WEAK) |
1752 | 2.14k | continue; |
1753 | | |
1754 | 1.27k | sname = elf_sym_str(obj, sym->st_name); |
1755 | 1.27k | if (!sname) { |
1756 | 103 | pr_warn("failed to get sym name string for var %s\n", name); |
1757 | 103 | return ERR_PTR(-EIO); |
1758 | 103 | } |
1759 | 1.17k | if (strcmp(name, sname) == 0) |
1760 | 460 | return sym; |
1761 | 1.17k | } |
1762 | | |
1763 | 305 | return ERR_PTR(-ENOENT); |
1764 | 868 | } |
1765 | | |
1766 | | #ifndef MFD_CLOEXEC |
1767 | | #define MFD_CLOEXEC 0x0001U |
1768 | | #endif |
1769 | | #ifndef MFD_NOEXEC_SEAL |
1770 | 4.13k | #define MFD_NOEXEC_SEAL 0x0008U |
1771 | | #endif |
1772 | | |
1773 | | static int create_placeholder_fd(void) |
1774 | 2.06k | { |
1775 | 2.06k | unsigned int flags = MFD_CLOEXEC | MFD_NOEXEC_SEAL; |
1776 | 2.06k | const char *name = "libbpf-placeholder-fd"; |
1777 | 2.06k | int fd; |
1778 | | |
1779 | 2.06k | fd = ensure_good_fd(sys_memfd_create(name, flags)); |
1780 | 2.06k | if (fd >= 0) |
1781 | 0 | return fd; |
1782 | 2.06k | else if (errno != EINVAL) |
1783 | 0 | return -errno; |
1784 | | |
1785 | | /* Possibly running on kernel without MFD_NOEXEC_SEAL */ |
1786 | 2.06k | fd = ensure_good_fd(sys_memfd_create(name, flags & ~MFD_NOEXEC_SEAL)); |
1787 | 2.06k | if (fd < 0) |
1788 | 0 | return -errno; |
1789 | 2.06k | return fd; |
1790 | 2.06k | } |
1791 | | |
1792 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
1793 | 2.06k | { |
1794 | 2.06k | struct bpf_map *map; |
1795 | 2.06k | int err; |
1796 | | |
1797 | 2.06k | err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, |
1798 | 2.06k | sizeof(*obj->maps), obj->nr_maps + 1); |
1799 | 2.06k | if (err) |
1800 | 0 | return ERR_PTR(err); |
1801 | | |
1802 | 2.06k | map = &obj->maps[obj->nr_maps++]; |
1803 | 2.06k | map->obj = obj; |
1804 | | /* Preallocate map FD without actually creating BPF map just yet. |
1805 | | * These map FD "placeholders" will be reused later without changing |
1806 | | * FD value when map is actually created in the kernel. |
1807 | | * |
1808 | | * This is useful to be able to perform BPF program relocations |
1809 | | * without having to create BPF maps before that step. This allows us |
1810 | | * to finalize and load BTF very late in BPF object's loading phase, |
1811 | | * right before BPF maps have to be created and BPF programs have to |
1812 | | * be loaded. By having these map FD placeholders we can perform all |
1813 | | * the sanitizations, relocations, and any other adjustments before we |
1814 | | * start creating actual BPF kernel objects (BTF, maps, progs). |
1815 | | */ |
1816 | 2.06k | map->fd = create_placeholder_fd(); |
1817 | 2.06k | if (map->fd < 0) |
1818 | 0 | return ERR_PTR(map->fd); |
1819 | 2.06k | map->inner_map_fd = -1; |
1820 | 2.06k | map->autocreate = true; |
1821 | | |
1822 | 2.06k | return map; |
1823 | 2.06k | } |
1824 | | |
1825 | | static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries) |
1826 | 3.91k | { |
1827 | 3.91k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1828 | 3.91k | size_t map_sz; |
1829 | | |
1830 | 3.91k | map_sz = (size_t)roundup(value_sz, 8) * max_entries; |
1831 | 3.91k | map_sz = roundup(map_sz, page_sz); |
1832 | 3.91k | return map_sz; |
1833 | 3.91k | } |
1834 | | |
1835 | | static size_t bpf_map_mmap_sz(const struct bpf_map *map) |
1836 | 3.91k | { |
1837 | 3.91k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1838 | | |
1839 | 3.91k | switch (map->def.type) { |
1840 | 3.91k | case BPF_MAP_TYPE_ARRAY: |
1841 | 3.91k | return array_map_mmap_sz(map->def.value_size, map->def.max_entries); |
1842 | 0 | case BPF_MAP_TYPE_ARENA: |
1843 | 0 | return page_sz * map->def.max_entries; |
1844 | 0 | default: |
1845 | 0 | return 0; /* not supported */ |
1846 | 3.91k | } |
1847 | 3.91k | } |
1848 | | |
1849 | | static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz) |
1850 | 0 | { |
1851 | 0 | void *mmaped; |
1852 | |
|
1853 | 0 | if (!map->mmaped) |
1854 | 0 | return -EINVAL; |
1855 | | |
1856 | 0 | if (old_sz == new_sz) |
1857 | 0 | return 0; |
1858 | | |
1859 | 0 | mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1860 | 0 | if (mmaped == MAP_FAILED) |
1861 | 0 | return -errno; |
1862 | | |
1863 | 0 | memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); |
1864 | 0 | munmap(map->mmaped, old_sz); |
1865 | 0 | map->mmaped = mmaped; |
1866 | 0 | return 0; |
1867 | 0 | } |
1868 | | |
1869 | | static char *internal_map_name(struct bpf_object *obj, const char *real_name) |
1870 | 1.97k | { |
1871 | 1.97k | char map_name[BPF_OBJ_NAME_LEN], *p; |
1872 | 1.97k | int pfx_len, sfx_len = max((size_t)7, strlen(real_name)); |
1873 | | |
1874 | | /* This is one of the more confusing parts of libbpf for various |
1875 | | * reasons, some of which are historical. The original idea for naming |
1876 | | * internal names was to include as much of BPF object name prefix as |
1877 | | * possible, so that it can be distinguished from similar internal |
1878 | | * maps of a different BPF object. |
1879 | | * As an example, let's say we have bpf_object named 'my_object_name' |
1880 | | * and internal map corresponding to '.rodata' ELF section. The final |
1881 | | * map name advertised to user and to the kernel will be |
1882 | | * 'my_objec.rodata', taking first 8 characters of object name and |
1883 | | * entire 7 characters of '.rodata'. |
1884 | | * Somewhat confusingly, if internal map ELF section name is shorter |
1885 | | * than 7 characters, e.g., '.bss', we still reserve 7 characters |
1886 | | * for the suffix, even though we only have 4 actual characters, and |
1887 | | * resulting map will be called 'my_objec.bss', not even using all 15 |
1888 | | * characters allowed by the kernel. Oh well, at least the truncated |
1889 | | * object name is somewhat consistent in this case. But if the map |
1890 | | * name is '.kconfig', we'll still have entirety of '.kconfig' added |
1891 | | * (8 chars) and thus will be left with only first 7 characters of the |
1892 | | * object name ('my_obje'). Happy guessing, user, that the final map |
1893 | | * name will be "my_obje.kconfig". |
1894 | | * Now, with libbpf starting to support arbitrarily named .rodata.* |
1895 | | * and .data.* data sections, it's possible that ELF section name is |
1896 | | * longer than allowed 15 chars, so we now need to be careful to take |
1897 | | * only up to 15 first characters of ELF name, taking no BPF object |
1898 | | * name characters at all. So '.rodata.abracadabra' will result in |
1899 | | * '.rodata.abracad' kernel and user-visible name. |
1900 | | * We need to keep this convoluted logic intact for .data, .bss and |
1901 | | * .rodata maps, but for new custom .data.custom and .rodata.custom |
1902 | | * maps we use their ELF names as is, not prepending bpf_object name |
1903 | | * in front. We still need to truncate them to 15 characters for the |
1904 | | * kernel. Full name can be recovered for such maps by using DATASEC |
1905 | | * BTF type associated with such map's value type, though. |
1906 | | */ |
1907 | 1.97k | if (sfx_len >= BPF_OBJ_NAME_LEN) |
1908 | 560 | sfx_len = BPF_OBJ_NAME_LEN - 1; |
1909 | | |
1910 | | /* if there are two or more dots in map name, it's a custom dot map */ |
1911 | 1.97k | if (strchr(real_name + 1, '.') != NULL) |
1912 | 1.34k | pfx_len = 0; |
1913 | 630 | else |
1914 | 630 | pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); |
1915 | | |
1916 | 1.97k | snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, |
1917 | 1.97k | sfx_len, real_name); |
1918 | | |
1919 | | /* sanities map name to characters allowed by kernel */ |
1920 | 26.9k | for (p = map_name; *p && p < map_name + sizeof(map_name); p++) |
1921 | 24.9k | if (!isalnum(*p) && *p != '_' && *p != '.') |
1922 | 4.30k | *p = '_'; |
1923 | | |
1924 | 1.97k | return strdup(map_name); |
1925 | 1.97k | } |
1926 | | |
1927 | | static int |
1928 | | map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map); |
1929 | | |
1930 | | /* Internal BPF map is mmap()'able only if at least one of corresponding |
1931 | | * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL |
1932 | | * variable and it's not marked as __hidden (which turns it into, effectively, |
1933 | | * a STATIC variable). |
1934 | | */ |
1935 | | static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map) |
1936 | 1.97k | { |
1937 | 1.97k | const struct btf_type *t, *vt; |
1938 | 1.97k | struct btf_var_secinfo *vsi; |
1939 | 1.97k | int i, n; |
1940 | | |
1941 | 1.97k | if (!map->btf_value_type_id) |
1942 | 1.84k | return false; |
1943 | | |
1944 | 129 | t = btf__type_by_id(obj->btf, map->btf_value_type_id); |
1945 | 129 | if (!btf_is_datasec(t)) |
1946 | 28 | return false; |
1947 | | |
1948 | 101 | vsi = btf_var_secinfos(t); |
1949 | 129 | for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) { |
1950 | 97 | vt = btf__type_by_id(obj->btf, vsi->type); |
1951 | 97 | if (!btf_is_var(vt)) |
1952 | 3 | continue; |
1953 | | |
1954 | 94 | if (btf_var(vt)->linkage != BTF_VAR_STATIC) |
1955 | 69 | return true; |
1956 | 94 | } |
1957 | | |
1958 | 32 | return false; |
1959 | 101 | } |
1960 | | |
1961 | | static int |
1962 | | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
1963 | | const char *real_name, int sec_idx, void *data, size_t data_sz) |
1964 | 1.97k | { |
1965 | 1.97k | struct bpf_map_def *def; |
1966 | 1.97k | struct bpf_map *map; |
1967 | 1.97k | size_t mmap_sz; |
1968 | 1.97k | int err; |
1969 | | |
1970 | 1.97k | map = bpf_object__add_map(obj); |
1971 | 1.97k | if (IS_ERR(map)) |
1972 | 0 | return PTR_ERR(map); |
1973 | | |
1974 | 1.97k | map->libbpf_type = type; |
1975 | 1.97k | map->sec_idx = sec_idx; |
1976 | 1.97k | map->sec_offset = 0; |
1977 | 1.97k | map->real_name = strdup(real_name); |
1978 | 1.97k | map->name = internal_map_name(obj, real_name); |
1979 | 1.97k | if (!map->real_name || !map->name) { |
1980 | 0 | zfree(&map->real_name); |
1981 | 0 | zfree(&map->name); |
1982 | 0 | return -ENOMEM; |
1983 | 0 | } |
1984 | | |
1985 | 1.97k | def = &map->def; |
1986 | 1.97k | def->type = BPF_MAP_TYPE_ARRAY; |
1987 | 1.97k | def->key_size = sizeof(int); |
1988 | 1.97k | def->value_size = data_sz; |
1989 | 1.97k | def->max_entries = 1; |
1990 | 1.97k | def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG |
1991 | 1.97k | ? BPF_F_RDONLY_PROG : 0; |
1992 | | |
1993 | | /* failures are fine because of maps like .rodata.str1.1 */ |
1994 | 1.97k | (void) map_fill_btf_type_info(obj, map); |
1995 | | |
1996 | 1.97k | if (map_is_mmapable(obj, map)) |
1997 | 69 | def->map_flags |= BPF_F_MMAPABLE; |
1998 | | |
1999 | 1.97k | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
2000 | 1.97k | map->name, map->sec_idx, map->sec_offset, def->map_flags); |
2001 | | |
2002 | 1.97k | mmap_sz = bpf_map_mmap_sz(map); |
2003 | 1.97k | map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, |
2004 | 1.97k | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
2005 | 1.97k | if (map->mmaped == MAP_FAILED) { |
2006 | 31 | err = -errno; |
2007 | 31 | map->mmaped = NULL; |
2008 | 31 | pr_warn("failed to alloc map '%s' content buffer: %s\n", map->name, errstr(err)); |
2009 | 31 | zfree(&map->real_name); |
2010 | 31 | zfree(&map->name); |
2011 | 31 | return err; |
2012 | 31 | } |
2013 | | |
2014 | 1.94k | if (data) |
2015 | 1.01k | memcpy(map->mmaped, data, data_sz); |
2016 | | |
2017 | 1.94k | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
2018 | 1.94k | return 0; |
2019 | 1.97k | } |
2020 | | |
2021 | | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
2022 | 2.16k | { |
2023 | 2.16k | struct elf_sec_desc *sec_desc; |
2024 | 2.16k | const char *sec_name; |
2025 | 2.16k | int err = 0, sec_idx; |
2026 | | |
2027 | | /* |
2028 | | * Populate obj->maps with libbpf internal maps. |
2029 | | */ |
2030 | 21.7k | for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { |
2031 | 19.5k | sec_desc = &obj->efile.secs[sec_idx]; |
2032 | | |
2033 | | /* Skip recognized sections with size 0. */ |
2034 | 19.5k | if (!sec_desc->data || sec_desc->data->d_size == 0) |
2035 | 16.4k | continue; |
2036 | | |
2037 | 3.15k | switch (sec_desc->sec_type) { |
2038 | 543 | case SEC_DATA: |
2039 | 543 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
2040 | 543 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
2041 | 543 | sec_name, sec_idx, |
2042 | 543 | sec_desc->data->d_buf, |
2043 | 543 | sec_desc->data->d_size); |
2044 | 543 | break; |
2045 | 476 | case SEC_RODATA: |
2046 | 476 | obj->has_rodata = true; |
2047 | 476 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
2048 | 476 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
2049 | 476 | sec_name, sec_idx, |
2050 | 476 | sec_desc->data->d_buf, |
2051 | 476 | sec_desc->data->d_size); |
2052 | 476 | break; |
2053 | 917 | case SEC_BSS: |
2054 | 917 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
2055 | 917 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
2056 | 917 | sec_name, sec_idx, |
2057 | 917 | NULL, |
2058 | 917 | sec_desc->data->d_size); |
2059 | 917 | break; |
2060 | 1.21k | default: |
2061 | | /* skip */ |
2062 | 1.21k | break; |
2063 | 3.15k | } |
2064 | 3.15k | if (err) |
2065 | 26 | return err; |
2066 | 3.15k | } |
2067 | 2.13k | return 0; |
2068 | 2.16k | } |
2069 | | |
2070 | | |
2071 | | static struct extern_desc *find_extern_by_name(const struct bpf_object *obj, |
2072 | | const void *name) |
2073 | 363 | { |
2074 | 363 | int i; |
2075 | | |
2076 | 634 | for (i = 0; i < obj->nr_extern; i++) { |
2077 | 530 | if (strcmp(obj->externs[i].name, name) == 0) |
2078 | 259 | return &obj->externs[i]; |
2079 | 530 | } |
2080 | 104 | return NULL; |
2081 | 363 | } |
2082 | | |
2083 | | static struct extern_desc *find_extern_by_name_with_len(const struct bpf_object *obj, |
2084 | | const void *name, int len) |
2085 | 0 | { |
2086 | 0 | const char *ext_name; |
2087 | 0 | int i; |
2088 | |
|
2089 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
2090 | 0 | ext_name = obj->externs[i].name; |
2091 | 0 | if (strlen(ext_name) == len && strncmp(ext_name, name, len) == 0) |
2092 | 0 | return &obj->externs[i]; |
2093 | 0 | } |
2094 | 0 | return NULL; |
2095 | 0 | } |
2096 | | |
2097 | | static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, |
2098 | | char value) |
2099 | 0 | { |
2100 | 0 | switch (ext->kcfg.type) { |
2101 | 0 | case KCFG_BOOL: |
2102 | 0 | if (value == 'm') { |
2103 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n", |
2104 | 0 | ext->name, value); |
2105 | 0 | return -EINVAL; |
2106 | 0 | } |
2107 | 0 | *(bool *)ext_val = value == 'y' ? true : false; |
2108 | 0 | break; |
2109 | 0 | case KCFG_TRISTATE: |
2110 | 0 | if (value == 'y') |
2111 | 0 | *(enum libbpf_tristate *)ext_val = TRI_YES; |
2112 | 0 | else if (value == 'm') |
2113 | 0 | *(enum libbpf_tristate *)ext_val = TRI_MODULE; |
2114 | 0 | else /* value == 'n' */ |
2115 | 0 | *(enum libbpf_tristate *)ext_val = TRI_NO; |
2116 | 0 | break; |
2117 | 0 | case KCFG_CHAR: |
2118 | 0 | *(char *)ext_val = value; |
2119 | 0 | break; |
2120 | 0 | case KCFG_UNKNOWN: |
2121 | 0 | case KCFG_INT: |
2122 | 0 | case KCFG_CHAR_ARR: |
2123 | 0 | default: |
2124 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n", |
2125 | 0 | ext->name, value); |
2126 | 0 | return -EINVAL; |
2127 | 0 | } |
2128 | 0 | ext->is_set = true; |
2129 | 0 | return 0; |
2130 | 0 | } |
2131 | | |
2132 | | static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, |
2133 | | const char *value) |
2134 | 0 | { |
2135 | 0 | size_t len; |
2136 | |
|
2137 | 0 | if (ext->kcfg.type != KCFG_CHAR_ARR) { |
2138 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n", |
2139 | 0 | ext->name, value); |
2140 | 0 | return -EINVAL; |
2141 | 0 | } |
2142 | | |
2143 | 0 | len = strlen(value); |
2144 | 0 | if (len < 2 || value[len - 1] != '"') { |
2145 | 0 | pr_warn("extern (kcfg) '%s': invalid string config '%s'\n", |
2146 | 0 | ext->name, value); |
2147 | 0 | return -EINVAL; |
2148 | 0 | } |
2149 | | |
2150 | | /* strip quotes */ |
2151 | 0 | len -= 2; |
2152 | 0 | if (len >= ext->kcfg.sz) { |
2153 | 0 | pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n", |
2154 | 0 | ext->name, value, len, ext->kcfg.sz - 1); |
2155 | 0 | len = ext->kcfg.sz - 1; |
2156 | 0 | } |
2157 | 0 | memcpy(ext_val, value + 1, len); |
2158 | 0 | ext_val[len] = '\0'; |
2159 | 0 | ext->is_set = true; |
2160 | 0 | return 0; |
2161 | 0 | } |
2162 | | |
2163 | | static int parse_u64(const char *value, __u64 *res) |
2164 | 0 | { |
2165 | 0 | char *value_end; |
2166 | 0 | int err; |
2167 | |
|
2168 | 0 | errno = 0; |
2169 | 0 | *res = strtoull(value, &value_end, 0); |
2170 | 0 | if (errno) { |
2171 | 0 | err = -errno; |
2172 | 0 | pr_warn("failed to parse '%s': %s\n", value, errstr(err)); |
2173 | 0 | return err; |
2174 | 0 | } |
2175 | 0 | if (*value_end) { |
2176 | 0 | pr_warn("failed to parse '%s' as integer completely\n", value); |
2177 | 0 | return -EINVAL; |
2178 | 0 | } |
2179 | 0 | return 0; |
2180 | 0 | } |
2181 | | |
2182 | | static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) |
2183 | 0 | { |
2184 | 0 | int bit_sz = ext->kcfg.sz * 8; |
2185 | |
|
2186 | 0 | if (ext->kcfg.sz == 8) |
2187 | 0 | return true; |
2188 | | |
2189 | | /* Validate that value stored in u64 fits in integer of `ext->sz` |
2190 | | * bytes size without any loss of information. If the target integer |
2191 | | * is signed, we rely on the following limits of integer type of |
2192 | | * Y bits and subsequent transformation: |
2193 | | * |
2194 | | * -2^(Y-1) <= X <= 2^(Y-1) - 1 |
2195 | | * 0 <= X + 2^(Y-1) <= 2^Y - 1 |
2196 | | * 0 <= X + 2^(Y-1) < 2^Y |
2197 | | * |
2198 | | * For unsigned target integer, check that all the (64 - Y) bits are |
2199 | | * zero. |
2200 | | */ |
2201 | 0 | if (ext->kcfg.is_signed) |
2202 | 0 | return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); |
2203 | 0 | else |
2204 | 0 | return (v >> bit_sz) == 0; |
2205 | 0 | } |
2206 | | |
2207 | | static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, |
2208 | | __u64 value) |
2209 | 0 | { |
2210 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && |
2211 | 0 | ext->kcfg.type != KCFG_BOOL) { |
2212 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n", |
2213 | 0 | ext->name, (unsigned long long)value); |
2214 | 0 | return -EINVAL; |
2215 | 0 | } |
2216 | 0 | if (ext->kcfg.type == KCFG_BOOL && value > 1) { |
2217 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n", |
2218 | 0 | ext->name, (unsigned long long)value); |
2219 | 0 | return -EINVAL; |
2220 | |
|
2221 | 0 | } |
2222 | 0 | if (!is_kcfg_value_in_range(ext, value)) { |
2223 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n", |
2224 | 0 | ext->name, (unsigned long long)value, ext->kcfg.sz); |
2225 | 0 | return -ERANGE; |
2226 | 0 | } |
2227 | 0 | switch (ext->kcfg.sz) { |
2228 | 0 | case 1: |
2229 | 0 | *(__u8 *)ext_val = value; |
2230 | 0 | break; |
2231 | 0 | case 2: |
2232 | 0 | *(__u16 *)ext_val = value; |
2233 | 0 | break; |
2234 | 0 | case 4: |
2235 | 0 | *(__u32 *)ext_val = value; |
2236 | 0 | break; |
2237 | 0 | case 8: |
2238 | 0 | *(__u64 *)ext_val = value; |
2239 | 0 | break; |
2240 | 0 | default: |
2241 | 0 | return -EINVAL; |
2242 | 0 | } |
2243 | 0 | ext->is_set = true; |
2244 | 0 | return 0; |
2245 | 0 | } |
2246 | | |
2247 | | static int bpf_object__process_kconfig_line(struct bpf_object *obj, |
2248 | | char *buf, void *data) |
2249 | 0 | { |
2250 | 0 | struct extern_desc *ext; |
2251 | 0 | char *sep, *value; |
2252 | 0 | int len, err = 0; |
2253 | 0 | void *ext_val; |
2254 | 0 | __u64 num; |
2255 | |
|
2256 | 0 | if (!str_has_pfx(buf, "CONFIG_")) |
2257 | 0 | return 0; |
2258 | | |
2259 | 0 | sep = strchr(buf, '='); |
2260 | 0 | if (!sep) { |
2261 | 0 | pr_warn("failed to parse '%s': no separator\n", buf); |
2262 | 0 | return -EINVAL; |
2263 | 0 | } |
2264 | | |
2265 | | /* Trim ending '\n' */ |
2266 | 0 | len = strlen(buf); |
2267 | 0 | if (buf[len - 1] == '\n') |
2268 | 0 | buf[len - 1] = '\0'; |
2269 | | /* Split on '=' and ensure that a value is present. */ |
2270 | 0 | *sep = '\0'; |
2271 | 0 | if (!sep[1]) { |
2272 | 0 | *sep = '='; |
2273 | 0 | pr_warn("failed to parse '%s': no value\n", buf); |
2274 | 0 | return -EINVAL; |
2275 | 0 | } |
2276 | | |
2277 | 0 | ext = find_extern_by_name(obj, buf); |
2278 | 0 | if (!ext || ext->is_set) |
2279 | 0 | return 0; |
2280 | | |
2281 | 0 | ext_val = data + ext->kcfg.data_off; |
2282 | 0 | value = sep + 1; |
2283 | |
|
2284 | 0 | switch (*value) { |
2285 | 0 | case 'y': case 'n': case 'm': |
2286 | 0 | err = set_kcfg_value_tri(ext, ext_val, *value); |
2287 | 0 | break; |
2288 | 0 | case '"': |
2289 | 0 | err = set_kcfg_value_str(ext, ext_val, value); |
2290 | 0 | break; |
2291 | 0 | default: |
2292 | | /* assume integer */ |
2293 | 0 | err = parse_u64(value, &num); |
2294 | 0 | if (err) { |
2295 | 0 | pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); |
2296 | 0 | return err; |
2297 | 0 | } |
2298 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { |
2299 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); |
2300 | 0 | return -EINVAL; |
2301 | 0 | } |
2302 | 0 | err = set_kcfg_value_num(ext, ext_val, num); |
2303 | 0 | break; |
2304 | 0 | } |
2305 | 0 | if (err) |
2306 | 0 | return err; |
2307 | 0 | pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); |
2308 | 0 | return 0; |
2309 | 0 | } |
2310 | | |
2311 | | static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) |
2312 | 0 | { |
2313 | 0 | char buf[PATH_MAX]; |
2314 | 0 | struct utsname uts; |
2315 | 0 | int len, err = 0; |
2316 | 0 | gzFile file; |
2317 | |
|
2318 | 0 | uname(&uts); |
2319 | 0 | len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); |
2320 | 0 | if (len < 0) |
2321 | 0 | return -EINVAL; |
2322 | 0 | else if (len >= PATH_MAX) |
2323 | 0 | return -ENAMETOOLONG; |
2324 | | |
2325 | | /* gzopen also accepts uncompressed files. */ |
2326 | 0 | file = gzopen(buf, "re"); |
2327 | 0 | if (!file) |
2328 | 0 | file = gzopen("/proc/config.gz", "re"); |
2329 | |
|
2330 | 0 | if (!file) { |
2331 | 0 | pr_warn("failed to open system Kconfig\n"); |
2332 | 0 | return -ENOENT; |
2333 | 0 | } |
2334 | | |
2335 | 0 | while (gzgets(file, buf, sizeof(buf))) { |
2336 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2337 | 0 | if (err) { |
2338 | 0 | pr_warn("error parsing system Kconfig line '%s': %s\n", |
2339 | 0 | buf, errstr(err)); |
2340 | 0 | goto out; |
2341 | 0 | } |
2342 | 0 | } |
2343 | | |
2344 | 0 | out: |
2345 | 0 | gzclose(file); |
2346 | 0 | return err; |
2347 | 0 | } |
2348 | | |
2349 | | static int bpf_object__read_kconfig_mem(struct bpf_object *obj, |
2350 | | const char *config, void *data) |
2351 | 0 | { |
2352 | 0 | char buf[PATH_MAX]; |
2353 | 0 | int err = 0; |
2354 | 0 | FILE *file; |
2355 | |
|
2356 | 0 | file = fmemopen((void *)config, strlen(config), "r"); |
2357 | 0 | if (!file) { |
2358 | 0 | err = -errno; |
2359 | 0 | pr_warn("failed to open in-memory Kconfig: %s\n", errstr(err)); |
2360 | 0 | return err; |
2361 | 0 | } |
2362 | | |
2363 | 0 | while (fgets(buf, sizeof(buf), file)) { |
2364 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2365 | 0 | if (err) { |
2366 | 0 | pr_warn("error parsing in-memory Kconfig line '%s': %s\n", |
2367 | 0 | buf, errstr(err)); |
2368 | 0 | break; |
2369 | 0 | } |
2370 | 0 | } |
2371 | |
|
2372 | 0 | fclose(file); |
2373 | 0 | return err; |
2374 | 0 | } |
2375 | | |
2376 | | static int bpf_object__init_kconfig_map(struct bpf_object *obj) |
2377 | 2.13k | { |
2378 | 2.13k | struct extern_desc *last_ext = NULL, *ext; |
2379 | 2.13k | size_t map_sz; |
2380 | 2.13k | int i, err; |
2381 | | |
2382 | 2.34k | for (i = 0; i < obj->nr_extern; i++) { |
2383 | 207 | ext = &obj->externs[i]; |
2384 | 207 | if (ext->type == EXT_KCFG) |
2385 | 88 | last_ext = ext; |
2386 | 207 | } |
2387 | | |
2388 | 2.13k | if (!last_ext) |
2389 | 2.09k | return 0; |
2390 | | |
2391 | 37 | map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; |
2392 | 37 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG, |
2393 | 37 | ".kconfig", obj->efile.symbols_shndx, |
2394 | 37 | NULL, map_sz); |
2395 | 37 | if (err) |
2396 | 5 | return err; |
2397 | | |
2398 | 32 | obj->kconfig_map_idx = obj->nr_maps - 1; |
2399 | | |
2400 | 32 | return 0; |
2401 | 37 | } |
2402 | | |
2403 | | const struct btf_type * |
2404 | | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) |
2405 | 833 | { |
2406 | 833 | const struct btf_type *t = btf__type_by_id(btf, id); |
2407 | | |
2408 | 833 | if (res_id) |
2409 | 423 | *res_id = id; |
2410 | | |
2411 | 1.25k | while (btf_is_mod(t) || btf_is_typedef(t)) { |
2412 | 419 | if (res_id) |
2413 | 187 | *res_id = t->type; |
2414 | 419 | t = btf__type_by_id(btf, t->type); |
2415 | 419 | } |
2416 | | |
2417 | 833 | return t; |
2418 | 833 | } |
2419 | | |
2420 | | static const struct btf_type * |
2421 | | resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) |
2422 | 0 | { |
2423 | 0 | const struct btf_type *t; |
2424 | |
|
2425 | 0 | t = skip_mods_and_typedefs(btf, id, NULL); |
2426 | 0 | if (!btf_is_ptr(t)) |
2427 | 0 | return NULL; |
2428 | | |
2429 | 0 | t = skip_mods_and_typedefs(btf, t->type, res_id); |
2430 | |
|
2431 | 0 | return btf_is_func_proto(t) ? t : NULL; |
2432 | 0 | } |
2433 | | |
2434 | | static const char *__btf_kind_str(__u16 kind) |
2435 | 80 | { |
2436 | 80 | switch (kind) { |
2437 | 10 | case BTF_KIND_UNKN: return "void"; |
2438 | 3 | case BTF_KIND_INT: return "int"; |
2439 | 0 | case BTF_KIND_PTR: return "ptr"; |
2440 | 5 | case BTF_KIND_ARRAY: return "array"; |
2441 | 9 | case BTF_KIND_STRUCT: return "struct"; |
2442 | 1 | case BTF_KIND_UNION: return "union"; |
2443 | 2 | case BTF_KIND_ENUM: return "enum"; |
2444 | 0 | case BTF_KIND_FWD: return "fwd"; |
2445 | 1 | case BTF_KIND_TYPEDEF: return "typedef"; |
2446 | 1 | case BTF_KIND_VOLATILE: return "volatile"; |
2447 | 0 | case BTF_KIND_CONST: return "const"; |
2448 | 1 | case BTF_KIND_RESTRICT: return "restrict"; |
2449 | 27 | case BTF_KIND_FUNC: return "func"; |
2450 | 0 | case BTF_KIND_FUNC_PROTO: return "func_proto"; |
2451 | 11 | case BTF_KIND_VAR: return "var"; |
2452 | 6 | case BTF_KIND_DATASEC: return "datasec"; |
2453 | 0 | case BTF_KIND_FLOAT: return "float"; |
2454 | 1 | case BTF_KIND_DECL_TAG: return "decl_tag"; |
2455 | 0 | case BTF_KIND_TYPE_TAG: return "type_tag"; |
2456 | 2 | case BTF_KIND_ENUM64: return "enum64"; |
2457 | 0 | default: return "unknown"; |
2458 | 80 | } |
2459 | 80 | } |
2460 | | |
2461 | | const char *btf_kind_str(const struct btf_type *t) |
2462 | 80 | { |
2463 | 80 | return __btf_kind_str(btf_kind(t)); |
2464 | 80 | } |
2465 | | |
2466 | | /* |
2467 | | * Fetch integer attribute of BTF map definition. Such attributes are |
2468 | | * represented using a pointer to an array, in which dimensionality of array |
2469 | | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
2470 | | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
2471 | | * type definition, while using only sizeof(void *) space in ELF data section. |
2472 | | */ |
2473 | | static bool get_map_field_int(const char *map_name, const struct btf *btf, |
2474 | | const struct btf_member *m, __u32 *res) |
2475 | 0 | { |
2476 | 0 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2477 | 0 | const char *name = btf__name_by_offset(btf, m->name_off); |
2478 | 0 | const struct btf_array *arr_info; |
2479 | 0 | const struct btf_type *arr_t; |
2480 | |
|
2481 | 0 | if (!btf_is_ptr(t)) { |
2482 | 0 | pr_warn("map '%s': attr '%s': expected PTR, got %s.\n", |
2483 | 0 | map_name, name, btf_kind_str(t)); |
2484 | 0 | return false; |
2485 | 0 | } |
2486 | | |
2487 | 0 | arr_t = btf__type_by_id(btf, t->type); |
2488 | 0 | if (!arr_t) { |
2489 | 0 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
2490 | 0 | map_name, name, t->type); |
2491 | 0 | return false; |
2492 | 0 | } |
2493 | 0 | if (!btf_is_array(arr_t)) { |
2494 | 0 | pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n", |
2495 | 0 | map_name, name, btf_kind_str(arr_t)); |
2496 | 0 | return false; |
2497 | 0 | } |
2498 | 0 | arr_info = btf_array(arr_t); |
2499 | 0 | *res = arr_info->nelems; |
2500 | 0 | return true; |
2501 | 0 | } |
2502 | | |
2503 | | static bool get_map_field_long(const char *map_name, const struct btf *btf, |
2504 | | const struct btf_member *m, __u64 *res) |
2505 | 0 | { |
2506 | 0 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2507 | 0 | const char *name = btf__name_by_offset(btf, m->name_off); |
2508 | |
|
2509 | 0 | if (btf_is_ptr(t)) { |
2510 | 0 | __u32 res32; |
2511 | 0 | bool ret; |
2512 | |
|
2513 | 0 | ret = get_map_field_int(map_name, btf, m, &res32); |
2514 | 0 | if (ret) |
2515 | 0 | *res = (__u64)res32; |
2516 | 0 | return ret; |
2517 | 0 | } |
2518 | | |
2519 | 0 | if (!btf_is_enum(t) && !btf_is_enum64(t)) { |
2520 | 0 | pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n", |
2521 | 0 | map_name, name, btf_kind_str(t)); |
2522 | 0 | return false; |
2523 | 0 | } |
2524 | | |
2525 | 0 | if (btf_vlen(t) != 1) { |
2526 | 0 | pr_warn("map '%s': attr '%s': invalid __ulong\n", |
2527 | 0 | map_name, name); |
2528 | 0 | return false; |
2529 | 0 | } |
2530 | | |
2531 | 0 | if (btf_is_enum(t)) { |
2532 | 0 | const struct btf_enum *e = btf_enum(t); |
2533 | |
|
2534 | 0 | *res = e->val; |
2535 | 0 | } else { |
2536 | 0 | const struct btf_enum64 *e = btf_enum64(t); |
2537 | |
|
2538 | 0 | *res = btf_enum64_value(e); |
2539 | 0 | } |
2540 | 0 | return true; |
2541 | 0 | } |
2542 | | |
2543 | | static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) |
2544 | 0 | { |
2545 | 0 | int len; |
2546 | |
|
2547 | 0 | len = snprintf(buf, buf_sz, "%s/%s", path, name); |
2548 | 0 | if (len < 0) |
2549 | 0 | return -EINVAL; |
2550 | 0 | if (len >= buf_sz) |
2551 | 0 | return -ENAMETOOLONG; |
2552 | | |
2553 | 0 | return 0; |
2554 | 0 | } |
2555 | | |
2556 | | static int build_map_pin_path(struct bpf_map *map, const char *path) |
2557 | 0 | { |
2558 | 0 | char buf[PATH_MAX]; |
2559 | 0 | int err; |
2560 | |
|
2561 | 0 | if (!path) |
2562 | 0 | path = BPF_FS_DEFAULT_PATH; |
2563 | |
|
2564 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
2565 | 0 | if (err) |
2566 | 0 | return err; |
2567 | | |
2568 | 0 | return bpf_map__set_pin_path(map, buf); |
2569 | 0 | } |
2570 | | |
2571 | | /* should match definition in bpf_helpers.h */ |
2572 | | enum libbpf_pin_type { |
2573 | | LIBBPF_PIN_NONE, |
2574 | | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ |
2575 | | LIBBPF_PIN_BY_NAME, |
2576 | | }; |
2577 | | |
2578 | | int parse_btf_map_def(const char *map_name, struct btf *btf, |
2579 | | const struct btf_type *def_t, bool strict, |
2580 | | struct btf_map_def *map_def, struct btf_map_def *inner_def) |
2581 | 0 | { |
2582 | 0 | const struct btf_type *t; |
2583 | 0 | const struct btf_member *m; |
2584 | 0 | bool is_inner = inner_def == NULL; |
2585 | 0 | int vlen, i; |
2586 | |
|
2587 | 0 | vlen = btf_vlen(def_t); |
2588 | 0 | m = btf_members(def_t); |
2589 | 0 | for (i = 0; i < vlen; i++, m++) { |
2590 | 0 | const char *name = btf__name_by_offset(btf, m->name_off); |
2591 | |
|
2592 | 0 | if (!name) { |
2593 | 0 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
2594 | 0 | return -EINVAL; |
2595 | 0 | } |
2596 | 0 | if (strcmp(name, "type") == 0) { |
2597 | 0 | if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) |
2598 | 0 | return -EINVAL; |
2599 | 0 | map_def->parts |= MAP_DEF_MAP_TYPE; |
2600 | 0 | } else if (strcmp(name, "max_entries") == 0) { |
2601 | 0 | if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) |
2602 | 0 | return -EINVAL; |
2603 | 0 | map_def->parts |= MAP_DEF_MAX_ENTRIES; |
2604 | 0 | } else if (strcmp(name, "map_flags") == 0) { |
2605 | 0 | if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) |
2606 | 0 | return -EINVAL; |
2607 | 0 | map_def->parts |= MAP_DEF_MAP_FLAGS; |
2608 | 0 | } else if (strcmp(name, "numa_node") == 0) { |
2609 | 0 | if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) |
2610 | 0 | return -EINVAL; |
2611 | 0 | map_def->parts |= MAP_DEF_NUMA_NODE; |
2612 | 0 | } else if (strcmp(name, "key_size") == 0) { |
2613 | 0 | __u32 sz; |
2614 | |
|
2615 | 0 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2616 | 0 | return -EINVAL; |
2617 | 0 | if (map_def->key_size && map_def->key_size != sz) { |
2618 | 0 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
2619 | 0 | map_name, map_def->key_size, sz); |
2620 | 0 | return -EINVAL; |
2621 | 0 | } |
2622 | 0 | map_def->key_size = sz; |
2623 | 0 | map_def->parts |= MAP_DEF_KEY_SIZE; |
2624 | 0 | } else if (strcmp(name, "key") == 0) { |
2625 | 0 | __s64 sz; |
2626 | |
|
2627 | 0 | t = btf__type_by_id(btf, m->type); |
2628 | 0 | if (!t) { |
2629 | 0 | pr_warn("map '%s': key type [%u] not found.\n", |
2630 | 0 | map_name, m->type); |
2631 | 0 | return -EINVAL; |
2632 | 0 | } |
2633 | 0 | if (!btf_is_ptr(t)) { |
2634 | 0 | pr_warn("map '%s': key spec is not PTR: %s.\n", |
2635 | 0 | map_name, btf_kind_str(t)); |
2636 | 0 | return -EINVAL; |
2637 | 0 | } |
2638 | 0 | sz = btf__resolve_size(btf, t->type); |
2639 | 0 | if (sz < 0) { |
2640 | 0 | pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n", |
2641 | 0 | map_name, t->type, (ssize_t)sz); |
2642 | 0 | return sz; |
2643 | 0 | } |
2644 | 0 | if (map_def->key_size && map_def->key_size != sz) { |
2645 | 0 | pr_warn("map '%s': conflicting key size %u != %zd.\n", |
2646 | 0 | map_name, map_def->key_size, (ssize_t)sz); |
2647 | 0 | return -EINVAL; |
2648 | 0 | } |
2649 | 0 | map_def->key_size = sz; |
2650 | 0 | map_def->key_type_id = t->type; |
2651 | 0 | map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; |
2652 | 0 | } else if (strcmp(name, "value_size") == 0) { |
2653 | 0 | __u32 sz; |
2654 | |
|
2655 | 0 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2656 | 0 | return -EINVAL; |
2657 | 0 | if (map_def->value_size && map_def->value_size != sz) { |
2658 | 0 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
2659 | 0 | map_name, map_def->value_size, sz); |
2660 | 0 | return -EINVAL; |
2661 | 0 | } |
2662 | 0 | map_def->value_size = sz; |
2663 | 0 | map_def->parts |= MAP_DEF_VALUE_SIZE; |
2664 | 0 | } else if (strcmp(name, "value") == 0) { |
2665 | 0 | __s64 sz; |
2666 | |
|
2667 | 0 | t = btf__type_by_id(btf, m->type); |
2668 | 0 | if (!t) { |
2669 | 0 | pr_warn("map '%s': value type [%u] not found.\n", |
2670 | 0 | map_name, m->type); |
2671 | 0 | return -EINVAL; |
2672 | 0 | } |
2673 | 0 | if (!btf_is_ptr(t)) { |
2674 | 0 | pr_warn("map '%s': value spec is not PTR: %s.\n", |
2675 | 0 | map_name, btf_kind_str(t)); |
2676 | 0 | return -EINVAL; |
2677 | 0 | } |
2678 | 0 | sz = btf__resolve_size(btf, t->type); |
2679 | 0 | if (sz < 0) { |
2680 | 0 | pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n", |
2681 | 0 | map_name, t->type, (ssize_t)sz); |
2682 | 0 | return sz; |
2683 | 0 | } |
2684 | 0 | if (map_def->value_size && map_def->value_size != sz) { |
2685 | 0 | pr_warn("map '%s': conflicting value size %u != %zd.\n", |
2686 | 0 | map_name, map_def->value_size, (ssize_t)sz); |
2687 | 0 | return -EINVAL; |
2688 | 0 | } |
2689 | 0 | map_def->value_size = sz; |
2690 | 0 | map_def->value_type_id = t->type; |
2691 | 0 | map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; |
2692 | 0 | } |
2693 | 0 | else if (strcmp(name, "values") == 0) { |
2694 | 0 | bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); |
2695 | 0 | bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; |
2696 | 0 | const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; |
2697 | 0 | char inner_map_name[128]; |
2698 | 0 | int err; |
2699 | |
|
2700 | 0 | if (is_inner) { |
2701 | 0 | pr_warn("map '%s': multi-level inner maps not supported.\n", |
2702 | 0 | map_name); |
2703 | 0 | return -ENOTSUP; |
2704 | 0 | } |
2705 | 0 | if (i != vlen - 1) { |
2706 | 0 | pr_warn("map '%s': '%s' member should be last.\n", |
2707 | 0 | map_name, name); |
2708 | 0 | return -EINVAL; |
2709 | 0 | } |
2710 | 0 | if (!is_map_in_map && !is_prog_array) { |
2711 | 0 | pr_warn("map '%s': should be map-in-map or prog-array.\n", |
2712 | 0 | map_name); |
2713 | 0 | return -ENOTSUP; |
2714 | 0 | } |
2715 | 0 | if (map_def->value_size && map_def->value_size != 4) { |
2716 | 0 | pr_warn("map '%s': conflicting value size %u != 4.\n", |
2717 | 0 | map_name, map_def->value_size); |
2718 | 0 | return -EINVAL; |
2719 | 0 | } |
2720 | 0 | map_def->value_size = 4; |
2721 | 0 | t = btf__type_by_id(btf, m->type); |
2722 | 0 | if (!t) { |
2723 | 0 | pr_warn("map '%s': %s type [%u] not found.\n", |
2724 | 0 | map_name, desc, m->type); |
2725 | 0 | return -EINVAL; |
2726 | 0 | } |
2727 | 0 | if (!btf_is_array(t) || btf_array(t)->nelems) { |
2728 | 0 | pr_warn("map '%s': %s spec is not a zero-sized array.\n", |
2729 | 0 | map_name, desc); |
2730 | 0 | return -EINVAL; |
2731 | 0 | } |
2732 | 0 | t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); |
2733 | 0 | if (!btf_is_ptr(t)) { |
2734 | 0 | pr_warn("map '%s': %s def is of unexpected kind %s.\n", |
2735 | 0 | map_name, desc, btf_kind_str(t)); |
2736 | 0 | return -EINVAL; |
2737 | 0 | } |
2738 | 0 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
2739 | 0 | if (is_prog_array) { |
2740 | 0 | if (!btf_is_func_proto(t)) { |
2741 | 0 | pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", |
2742 | 0 | map_name, btf_kind_str(t)); |
2743 | 0 | return -EINVAL; |
2744 | 0 | } |
2745 | 0 | continue; |
2746 | 0 | } |
2747 | 0 | if (!btf_is_struct(t)) { |
2748 | 0 | pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", |
2749 | 0 | map_name, btf_kind_str(t)); |
2750 | 0 | return -EINVAL; |
2751 | 0 | } |
2752 | | |
2753 | 0 | snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name); |
2754 | 0 | err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL); |
2755 | 0 | if (err) |
2756 | 0 | return err; |
2757 | | |
2758 | 0 | map_def->parts |= MAP_DEF_INNER_MAP; |
2759 | 0 | } else if (strcmp(name, "pinning") == 0) { |
2760 | 0 | __u32 val; |
2761 | |
|
2762 | 0 | if (is_inner) { |
2763 | 0 | pr_warn("map '%s': inner def can't be pinned.\n", map_name); |
2764 | 0 | return -EINVAL; |
2765 | 0 | } |
2766 | 0 | if (!get_map_field_int(map_name, btf, m, &val)) |
2767 | 0 | return -EINVAL; |
2768 | 0 | if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) { |
2769 | 0 | pr_warn("map '%s': invalid pinning value %u.\n", |
2770 | 0 | map_name, val); |
2771 | 0 | return -EINVAL; |
2772 | 0 | } |
2773 | 0 | map_def->pinning = val; |
2774 | 0 | map_def->parts |= MAP_DEF_PINNING; |
2775 | 0 | } else if (strcmp(name, "map_extra") == 0) { |
2776 | 0 | __u64 map_extra; |
2777 | |
|
2778 | 0 | if (!get_map_field_long(map_name, btf, m, &map_extra)) |
2779 | 0 | return -EINVAL; |
2780 | 0 | map_def->map_extra = map_extra; |
2781 | 0 | map_def->parts |= MAP_DEF_MAP_EXTRA; |
2782 | 0 | } else { |
2783 | 0 | if (strict) { |
2784 | 0 | pr_warn("map '%s': unknown field '%s'.\n", map_name, name); |
2785 | 0 | return -ENOTSUP; |
2786 | 0 | } |
2787 | 0 | pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name); |
2788 | 0 | } |
2789 | 0 | } |
2790 | | |
2791 | 0 | if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { |
2792 | 0 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
2793 | 0 | return -EINVAL; |
2794 | 0 | } |
2795 | | |
2796 | 0 | return 0; |
2797 | 0 | } |
2798 | | |
2799 | | static size_t adjust_ringbuf_sz(size_t sz) |
2800 | 0 | { |
2801 | 0 | __u32 page_sz = sysconf(_SC_PAGE_SIZE); |
2802 | 0 | __u32 mul; |
2803 | | |
2804 | | /* if user forgot to set any size, make sure they see error */ |
2805 | 0 | if (sz == 0) |
2806 | 0 | return 0; |
2807 | | /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be |
2808 | | * a power-of-2 multiple of kernel's page size. If user diligently |
2809 | | * satisified these conditions, pass the size through. |
2810 | | */ |
2811 | 0 | if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz)) |
2812 | 0 | return sz; |
2813 | | |
2814 | | /* Otherwise find closest (page_sz * power_of_2) product bigger than |
2815 | | * user-set size to satisfy both user size request and kernel |
2816 | | * requirements and substitute correct max_entries for map creation. |
2817 | | */ |
2818 | 0 | for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) { |
2819 | 0 | if (mul * page_sz > sz) |
2820 | 0 | return mul * page_sz; |
2821 | 0 | } |
2822 | | |
2823 | | /* if it's impossible to satisfy the conditions (i.e., user size is |
2824 | | * very close to UINT_MAX but is not a power-of-2 multiple of |
2825 | | * page_size) then just return original size and let kernel reject it |
2826 | | */ |
2827 | 0 | return sz; |
2828 | 0 | } |
2829 | | |
2830 | | static bool map_is_ringbuf(const struct bpf_map *map) |
2831 | 0 | { |
2832 | 0 | return map->def.type == BPF_MAP_TYPE_RINGBUF || |
2833 | 0 | map->def.type == BPF_MAP_TYPE_USER_RINGBUF; |
2834 | 0 | } |
2835 | | |
2836 | | static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def) |
2837 | 0 | { |
2838 | 0 | map->def.type = def->map_type; |
2839 | 0 | map->def.key_size = def->key_size; |
2840 | 0 | map->def.value_size = def->value_size; |
2841 | 0 | map->def.max_entries = def->max_entries; |
2842 | 0 | map->def.map_flags = def->map_flags; |
2843 | 0 | map->map_extra = def->map_extra; |
2844 | |
|
2845 | 0 | map->numa_node = def->numa_node; |
2846 | 0 | map->btf_key_type_id = def->key_type_id; |
2847 | 0 | map->btf_value_type_id = def->value_type_id; |
2848 | | |
2849 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
2850 | 0 | if (map_is_ringbuf(map)) |
2851 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
2852 | |
|
2853 | 0 | if (def->parts & MAP_DEF_MAP_TYPE) |
2854 | 0 | pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); |
2855 | |
|
2856 | 0 | if (def->parts & MAP_DEF_KEY_TYPE) |
2857 | 0 | pr_debug("map '%s': found key [%u], sz = %u.\n", |
2858 | 0 | map->name, def->key_type_id, def->key_size); |
2859 | 0 | else if (def->parts & MAP_DEF_KEY_SIZE) |
2860 | 0 | pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); |
2861 | |
|
2862 | 0 | if (def->parts & MAP_DEF_VALUE_TYPE) |
2863 | 0 | pr_debug("map '%s': found value [%u], sz = %u.\n", |
2864 | 0 | map->name, def->value_type_id, def->value_size); |
2865 | 0 | else if (def->parts & MAP_DEF_VALUE_SIZE) |
2866 | 0 | pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); |
2867 | |
|
2868 | 0 | if (def->parts & MAP_DEF_MAX_ENTRIES) |
2869 | 0 | pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); |
2870 | 0 | if (def->parts & MAP_DEF_MAP_FLAGS) |
2871 | 0 | pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); |
2872 | 0 | if (def->parts & MAP_DEF_MAP_EXTRA) |
2873 | 0 | pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, |
2874 | 0 | (unsigned long long)def->map_extra); |
2875 | 0 | if (def->parts & MAP_DEF_PINNING) |
2876 | 0 | pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); |
2877 | 0 | if (def->parts & MAP_DEF_NUMA_NODE) |
2878 | 0 | pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); |
2879 | |
|
2880 | 0 | if (def->parts & MAP_DEF_INNER_MAP) |
2881 | 0 | pr_debug("map '%s': found inner map definition.\n", map->name); |
2882 | 0 | } |
2883 | | |
2884 | | static const char *btf_var_linkage_str(__u32 linkage) |
2885 | 2 | { |
2886 | 2 | switch (linkage) { |
2887 | 1 | case BTF_VAR_STATIC: return "static"; |
2888 | 0 | case BTF_VAR_GLOBAL_ALLOCATED: return "global"; |
2889 | 1 | case BTF_VAR_GLOBAL_EXTERN: return "extern"; |
2890 | 0 | default: return "unknown"; |
2891 | 2 | } |
2892 | 2 | } |
2893 | | |
2894 | | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
2895 | | const struct btf_type *sec, |
2896 | | int var_idx, int sec_idx, |
2897 | | const Elf_Data *data, bool strict, |
2898 | | const char *pin_root_path) |
2899 | 30 | { |
2900 | 30 | struct btf_map_def map_def = {}, inner_def = {}; |
2901 | 30 | const struct btf_type *var, *def; |
2902 | 30 | const struct btf_var_secinfo *vi; |
2903 | 30 | const struct btf_var *var_extra; |
2904 | 30 | const char *map_name; |
2905 | 30 | struct bpf_map *map; |
2906 | 30 | int err; |
2907 | | |
2908 | 30 | vi = btf_var_secinfos(sec) + var_idx; |
2909 | 30 | var = btf__type_by_id(obj->btf, vi->type); |
2910 | 30 | var_extra = btf_var(var); |
2911 | 30 | map_name = btf__name_by_offset(obj->btf, var->name_off); |
2912 | | |
2913 | 30 | if (str_is_empty(map_name)) { |
2914 | 1 | pr_warn("map #%d: empty name.\n", var_idx); |
2915 | 1 | return -EINVAL; |
2916 | 1 | } |
2917 | 29 | if ((__u64)vi->offset + vi->size > data->d_size) { |
2918 | 27 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
2919 | 27 | return -EINVAL; |
2920 | 27 | } |
2921 | 2 | if (!btf_is_var(var)) { |
2922 | 0 | pr_warn("map '%s': unexpected var kind %s.\n", |
2923 | 0 | map_name, btf_kind_str(var)); |
2924 | 0 | return -EINVAL; |
2925 | 0 | } |
2926 | 2 | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { |
2927 | 2 | pr_warn("map '%s': unsupported map linkage %s.\n", |
2928 | 2 | map_name, btf_var_linkage_str(var_extra->linkage)); |
2929 | 2 | return -EOPNOTSUPP; |
2930 | 2 | } |
2931 | | |
2932 | 0 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
2933 | 0 | if (!btf_is_struct(def)) { |
2934 | 0 | pr_warn("map '%s': unexpected def kind %s.\n", |
2935 | 0 | map_name, btf_kind_str(var)); |
2936 | 0 | return -EINVAL; |
2937 | 0 | } |
2938 | 0 | if (def->size > vi->size) { |
2939 | 0 | pr_warn("map '%s': invalid def size.\n", map_name); |
2940 | 0 | return -EINVAL; |
2941 | 0 | } |
2942 | | |
2943 | 0 | map = bpf_object__add_map(obj); |
2944 | 0 | if (IS_ERR(map)) |
2945 | 0 | return PTR_ERR(map); |
2946 | 0 | map->name = strdup(map_name); |
2947 | 0 | if (!map->name) { |
2948 | 0 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
2949 | 0 | return -ENOMEM; |
2950 | 0 | } |
2951 | 0 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
2952 | 0 | map->def.type = BPF_MAP_TYPE_UNSPEC; |
2953 | 0 | map->sec_idx = sec_idx; |
2954 | 0 | map->sec_offset = vi->offset; |
2955 | 0 | map->btf_var_idx = var_idx; |
2956 | 0 | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
2957 | 0 | map_name, map->sec_idx, map->sec_offset); |
2958 | |
|
2959 | 0 | err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); |
2960 | 0 | if (err) |
2961 | 0 | return err; |
2962 | | |
2963 | 0 | fill_map_from_def(map, &map_def); |
2964 | |
|
2965 | 0 | if (map_def.pinning == LIBBPF_PIN_BY_NAME) { |
2966 | 0 | err = build_map_pin_path(map, pin_root_path); |
2967 | 0 | if (err) { |
2968 | 0 | pr_warn("map '%s': couldn't build pin path.\n", map->name); |
2969 | 0 | return err; |
2970 | 0 | } |
2971 | 0 | } |
2972 | | |
2973 | 0 | if (map_def.parts & MAP_DEF_INNER_MAP) { |
2974 | 0 | map->inner_map = calloc(1, sizeof(*map->inner_map)); |
2975 | 0 | if (!map->inner_map) |
2976 | 0 | return -ENOMEM; |
2977 | 0 | map->inner_map->fd = create_placeholder_fd(); |
2978 | 0 | if (map->inner_map->fd < 0) |
2979 | 0 | return map->inner_map->fd; |
2980 | 0 | map->inner_map->sec_idx = sec_idx; |
2981 | 0 | map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); |
2982 | 0 | if (!map->inner_map->name) |
2983 | 0 | return -ENOMEM; |
2984 | 0 | sprintf(map->inner_map->name, "%s.inner", map_name); |
2985 | |
|
2986 | 0 | fill_map_from_def(map->inner_map, &inner_def); |
2987 | 0 | } |
2988 | | |
2989 | 0 | err = map_fill_btf_type_info(obj, map); |
2990 | 0 | if (err) |
2991 | 0 | return err; |
2992 | | |
2993 | 0 | return 0; |
2994 | 0 | } |
2995 | | |
2996 | | static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, |
2997 | | const char *sec_name, int sec_idx, |
2998 | | void *data, size_t data_sz) |
2999 | 0 | { |
3000 | 0 | const long page_sz = sysconf(_SC_PAGE_SIZE); |
3001 | 0 | const size_t data_alloc_sz = roundup(data_sz, page_sz); |
3002 | 0 | size_t mmap_sz; |
3003 | |
|
3004 | 0 | mmap_sz = bpf_map_mmap_sz(map); |
3005 | 0 | if (data_alloc_sz > mmap_sz) { |
3006 | 0 | pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n", |
3007 | 0 | sec_name, mmap_sz, data_sz); |
3008 | 0 | return -E2BIG; |
3009 | 0 | } |
3010 | | |
3011 | 0 | obj->arena_data = malloc(data_sz); |
3012 | 0 | if (!obj->arena_data) |
3013 | 0 | return -ENOMEM; |
3014 | 0 | memcpy(obj->arena_data, data, data_sz); |
3015 | 0 | obj->arena_data_sz = data_sz; |
3016 | | |
3017 | | /* make bpf_map__init_value() work for ARENA maps */ |
3018 | 0 | map->mmaped = obj->arena_data; |
3019 | |
|
3020 | 0 | return 0; |
3021 | 0 | } |
3022 | | |
3023 | | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
3024 | | const char *pin_root_path) |
3025 | 2.34k | { |
3026 | 2.34k | const struct btf_type *sec = NULL; |
3027 | 2.34k | int nr_types, i, vlen, err; |
3028 | 2.34k | const struct btf_type *t; |
3029 | 2.34k | const char *name; |
3030 | 2.34k | Elf_Data *data; |
3031 | 2.34k | Elf_Scn *scn; |
3032 | | |
3033 | 2.34k | if (obj->efile.btf_maps_shndx < 0) |
3034 | 2.12k | return 0; |
3035 | | |
3036 | 219 | scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); |
3037 | 219 | data = elf_sec_data(obj, scn); |
3038 | 219 | if (!data) { |
3039 | 0 | pr_warn("elf: failed to get %s map definitions for %s\n", |
3040 | 0 | MAPS_ELF_SEC, obj->path); |
3041 | 0 | return -EINVAL; |
3042 | 0 | } |
3043 | | |
3044 | 219 | nr_types = btf__type_cnt(obj->btf); |
3045 | 1.35k | for (i = 1; i < nr_types; i++) { |
3046 | 1.19k | t = btf__type_by_id(obj->btf, i); |
3047 | 1.19k | if (!btf_is_datasec(t)) |
3048 | 985 | continue; |
3049 | 214 | name = btf__name_by_offset(obj->btf, t->name_off); |
3050 | 214 | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
3051 | 67 | sec = t; |
3052 | 67 | obj->efile.btf_maps_sec_btf_id = i; |
3053 | 67 | break; |
3054 | 67 | } |
3055 | 214 | } |
3056 | | |
3057 | 219 | if (!sec) { |
3058 | 152 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
3059 | 152 | return -ENOENT; |
3060 | 152 | } |
3061 | | |
3062 | 67 | vlen = btf_vlen(sec); |
3063 | 67 | for (i = 0; i < vlen; i++) { |
3064 | 30 | err = bpf_object__init_user_btf_map(obj, sec, i, |
3065 | 30 | obj->efile.btf_maps_shndx, |
3066 | 30 | data, strict, |
3067 | 30 | pin_root_path); |
3068 | 30 | if (err) |
3069 | 30 | return err; |
3070 | 30 | } |
3071 | | |
3072 | 37 | for (i = 0; i < obj->nr_maps; i++) { |
3073 | 0 | struct bpf_map *map = &obj->maps[i]; |
3074 | |
|
3075 | 0 | if (map->def.type != BPF_MAP_TYPE_ARENA) |
3076 | 0 | continue; |
3077 | | |
3078 | 0 | if (obj->arena_map_idx >= 0) { |
3079 | 0 | pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n", |
3080 | 0 | map->name, obj->maps[obj->arena_map_idx].name); |
3081 | 0 | return -EINVAL; |
3082 | 0 | } |
3083 | 0 | obj->arena_map_idx = i; |
3084 | |
|
3085 | 0 | if (obj->efile.arena_data) { |
3086 | 0 | err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx, |
3087 | 0 | obj->efile.arena_data->d_buf, |
3088 | 0 | obj->efile.arena_data->d_size); |
3089 | 0 | if (err) |
3090 | 0 | return err; |
3091 | 0 | } |
3092 | 0 | } |
3093 | 37 | if (obj->efile.arena_data && obj->arena_map_idx < 0) { |
3094 | 0 | pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n", |
3095 | 0 | ARENA_SEC); |
3096 | 0 | return -ENOENT; |
3097 | 0 | } |
3098 | | |
3099 | 37 | return 0; |
3100 | 37 | } |
3101 | | |
3102 | | static int bpf_object__init_maps(struct bpf_object *obj, |
3103 | | const struct bpf_object_open_opts *opts) |
3104 | 2.34k | { |
3105 | 2.34k | const char *pin_root_path; |
3106 | 2.34k | bool strict; |
3107 | 2.34k | int err = 0; |
3108 | | |
3109 | 2.34k | strict = !OPTS_GET(opts, relaxed_maps, false); |
3110 | 2.34k | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
3111 | | |
3112 | 2.34k | err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
3113 | 2.34k | err = err ?: bpf_object__init_global_data_maps(obj); |
3114 | 2.34k | err = err ?: bpf_object__init_kconfig_map(obj); |
3115 | 2.34k | err = err ?: bpf_object_init_struct_ops(obj); |
3116 | | |
3117 | 2.34k | return err; |
3118 | 2.34k | } |
3119 | | |
3120 | | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
3121 | 3.06k | { |
3122 | 3.06k | Elf64_Shdr *sh; |
3123 | | |
3124 | 3.06k | sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx)); |
3125 | 3.06k | if (!sh) |
3126 | 0 | return false; |
3127 | | |
3128 | 3.06k | return sh->sh_flags & SHF_EXECINSTR; |
3129 | 3.06k | } |
3130 | | |
3131 | | static bool starts_with_qmark(const char *s) |
3132 | 0 | { |
3133 | 0 | return s && s[0] == '?'; |
3134 | 0 | } |
3135 | | |
3136 | | static bool btf_needs_sanitization(struct bpf_object *obj) |
3137 | 0 | { |
3138 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3139 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3140 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3141 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3142 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3143 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3144 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3145 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3146 | 0 | bool has_layout = kernel_supports(obj, FEAT_BTF_LAYOUT); |
3147 | |
|
3148 | 0 | return !has_func || !has_datasec || !has_func_global || !has_float || |
3149 | 0 | !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec || |
3150 | 0 | !has_layout; |
3151 | 0 | } |
3152 | | |
3153 | | struct btf *bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *orig_btf) |
3154 | 0 | { |
3155 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3156 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3157 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3158 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3159 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3160 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3161 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3162 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3163 | 0 | bool has_layout = kernel_supports(obj, FEAT_BTF_LAYOUT); |
3164 | 0 | int enum64_placeholder_id = 0; |
3165 | 0 | const struct btf_header *hdr; |
3166 | 0 | struct btf *btf = NULL; |
3167 | 0 | const void *raw_data; |
3168 | 0 | struct btf_type *t; |
3169 | 0 | int i, j, vlen; |
3170 | 0 | __u32 sz; |
3171 | 0 | int err; |
3172 | | |
3173 | | /* clone BTF to sanitize a copy and leave the original intact */ |
3174 | 0 | raw_data = btf__raw_data(orig_btf, &sz); |
3175 | 0 | if (!raw_data) |
3176 | 0 | return ERR_PTR(-ENOMEM); |
3177 | | /* btf_header() gives us endian-safe header info */ |
3178 | 0 | hdr = btf_header(orig_btf); |
3179 | |
|
3180 | 0 | if (!has_layout && hdr->hdr_len >= sizeof(struct btf_header) && |
3181 | 0 | (hdr->layout_len != 0 || hdr->layout_off != 0)) { |
3182 | 0 | const struct btf_header *old_hdr = raw_data; |
3183 | 0 | struct btf_header *new_hdr; |
3184 | 0 | void *new_raw_data; |
3185 | 0 | __u32 new_str_off; |
3186 | | |
3187 | | /* |
3188 | | * Need to rewrite BTF to exclude layout information and |
3189 | | * move string section to immediately after types. |
3190 | | */ |
3191 | 0 | new_raw_data = malloc(sz); |
3192 | 0 | if (!new_raw_data) |
3193 | 0 | return ERR_PTR(-ENOMEM); |
3194 | | |
3195 | 0 | memcpy(new_raw_data, raw_data, sz); |
3196 | 0 | new_hdr = new_raw_data; |
3197 | 0 | new_hdr->layout_off = 0; |
3198 | 0 | new_hdr->layout_len = 0; |
3199 | 0 | new_str_off = hdr->type_off + hdr->type_len; |
3200 | | /* Handle swapped endian case */ |
3201 | 0 | if (old_hdr->magic != hdr->magic) |
3202 | 0 | new_hdr->str_off = bswap_32(new_str_off); |
3203 | 0 | else |
3204 | 0 | new_hdr->str_off = new_str_off; |
3205 | |
|
3206 | 0 | memmove(new_raw_data + hdr->hdr_len + new_str_off, |
3207 | 0 | new_raw_data + hdr->hdr_len + hdr->str_off, |
3208 | 0 | hdr->str_len); |
3209 | 0 | sz = hdr->hdr_len + hdr->type_off + hdr->type_len + hdr->str_len; |
3210 | 0 | btf = btf__new(new_raw_data, sz); |
3211 | 0 | free(new_raw_data); |
3212 | 0 | } else { |
3213 | 0 | btf = btf__new(raw_data, sz); |
3214 | 0 | } |
3215 | 0 | err = libbpf_get_error(btf); |
3216 | 0 | if (err) |
3217 | 0 | return ERR_PTR(err); |
3218 | | |
3219 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3220 | 0 | btf__set_pointer_size(btf, 8); |
3221 | |
|
3222 | 0 | for (i = 1; i < btf__type_cnt(btf); i++) { |
3223 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3224 | |
|
3225 | 0 | if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) { |
3226 | | /* replace VAR/DECL_TAG with INT */ |
3227 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
3228 | | /* |
3229 | | * using size = 1 is the safest choice, 4 will be too |
3230 | | * big and cause kernel BTF validation failure if |
3231 | | * original variable took less than 4 bytes |
3232 | | */ |
3233 | 0 | t->size = 1; |
3234 | 0 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
3235 | 0 | } else if (!has_datasec && btf_is_datasec(t)) { |
3236 | | /* replace DATASEC with STRUCT */ |
3237 | 0 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
3238 | 0 | struct btf_member *m = btf_members(t); |
3239 | 0 | struct btf_type *vt; |
3240 | 0 | char *name; |
3241 | |
|
3242 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3243 | 0 | while (*name) { |
3244 | 0 | if (*name == '.' || *name == '?') |
3245 | 0 | *name = '_'; |
3246 | 0 | name++; |
3247 | 0 | } |
3248 | |
|
3249 | 0 | vlen = btf_vlen(t); |
3250 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
3251 | 0 | for (j = 0; j < vlen; j++, v++, m++) { |
3252 | | /* order of field assignments is important */ |
3253 | 0 | m->offset = v->offset * 8; |
3254 | 0 | m->type = v->type; |
3255 | | /* preserve variable name as member name */ |
3256 | 0 | vt = (void *)btf__type_by_id(btf, v->type); |
3257 | 0 | m->name_off = vt->name_off; |
3258 | 0 | } |
3259 | 0 | } else if (!has_qmark_datasec && btf_is_datasec(t) && |
3260 | 0 | starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { |
3261 | | /* replace '?' prefix with '_' for DATASEC names */ |
3262 | 0 | char *name; |
3263 | |
|
3264 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3265 | 0 | if (name[0] == '?') |
3266 | 0 | name[0] = '_'; |
3267 | 0 | } else if (!has_func && btf_is_func_proto(t)) { |
3268 | | /* replace FUNC_PROTO with ENUM */ |
3269 | 0 | vlen = btf_vlen(t); |
3270 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
3271 | 0 | t->size = sizeof(__u32); /* kernel enforced */ |
3272 | 0 | } else if (!has_func && btf_is_func(t)) { |
3273 | | /* replace FUNC with TYPEDEF */ |
3274 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
3275 | 0 | } else if (!has_func_global && btf_is_func(t)) { |
3276 | | /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */ |
3277 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); |
3278 | 0 | } else if (!has_float && btf_is_float(t)) { |
3279 | | /* replace FLOAT with an equally-sized empty STRUCT; |
3280 | | * since C compilers do not accept e.g. "float" as a |
3281 | | * valid struct name, make it anonymous |
3282 | | */ |
3283 | 0 | t->name_off = 0; |
3284 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); |
3285 | 0 | } else if (!has_type_tag && btf_is_type_tag(t)) { |
3286 | | /* replace TYPE_TAG with a CONST */ |
3287 | 0 | t->name_off = 0; |
3288 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); |
3289 | 0 | } else if (!has_enum64 && btf_is_enum(t)) { |
3290 | | /* clear the kflag */ |
3291 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); |
3292 | 0 | } else if (!has_enum64 && btf_is_enum64(t)) { |
3293 | | /* replace ENUM64 with a union */ |
3294 | 0 | struct btf_member *m; |
3295 | |
|
3296 | 0 | if (enum64_placeholder_id == 0) { |
3297 | 0 | enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); |
3298 | 0 | if (enum64_placeholder_id < 0) { |
3299 | 0 | btf__free(btf); |
3300 | 0 | return ERR_PTR(enum64_placeholder_id); |
3301 | 0 | } |
3302 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3303 | 0 | } |
3304 | | |
3305 | 0 | m = btf_members(t); |
3306 | 0 | vlen = btf_vlen(t); |
3307 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); |
3308 | 0 | for (j = 0; j < vlen; j++, m++) { |
3309 | 0 | m->type = enum64_placeholder_id; |
3310 | 0 | m->offset = 0; |
3311 | 0 | } |
3312 | 0 | } |
3313 | 0 | } |
3314 | | |
3315 | 0 | return btf; |
3316 | 0 | } |
3317 | | |
3318 | | static bool libbpf_needs_btf(const struct bpf_object *obj) |
3319 | 3.20k | { |
3320 | 3.20k | return obj->efile.btf_maps_shndx >= 0 || |
3321 | 3.15k | obj->efile.has_st_ops || |
3322 | 3.14k | obj->nr_extern > 0; |
3323 | 3.20k | } |
3324 | | |
3325 | | static bool kernel_needs_btf(const struct bpf_object *obj) |
3326 | 0 | { |
3327 | 0 | return obj->efile.has_st_ops; |
3328 | 0 | } |
3329 | | |
3330 | | static int bpf_object__init_btf(struct bpf_object *obj, |
3331 | | Elf_Data *btf_data, |
3332 | | Elf_Data *btf_ext_data) |
3333 | 5.19k | { |
3334 | 5.19k | int err = -ENOENT; |
3335 | | |
3336 | 5.19k | if (btf_data) { |
3337 | 3.42k | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
3338 | 3.42k | err = libbpf_get_error(obj->btf); |
3339 | 3.42k | if (err) { |
3340 | 1.07k | obj->btf = NULL; |
3341 | 1.07k | pr_warn("Error loading ELF section %s: %s.\n", BTF_ELF_SEC, errstr(err)); |
3342 | 1.07k | goto out; |
3343 | 1.07k | } |
3344 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3345 | 2.35k | btf__set_pointer_size(obj->btf, 8); |
3346 | 2.35k | } |
3347 | 4.11k | if (btf_ext_data) { |
3348 | 475 | struct btf_ext_info *ext_segs[3]; |
3349 | 475 | int seg_num, sec_num; |
3350 | | |
3351 | 475 | if (!obj->btf) { |
3352 | 9 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
3353 | 9 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
3354 | 9 | goto out; |
3355 | 9 | } |
3356 | 466 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); |
3357 | 466 | err = libbpf_get_error(obj->btf_ext); |
3358 | 466 | if (err) { |
3359 | 360 | pr_warn("Error loading ELF section %s: %s. Ignored and continue.\n", |
3360 | 360 | BTF_EXT_ELF_SEC, errstr(err)); |
3361 | 360 | obj->btf_ext = NULL; |
3362 | 360 | goto out; |
3363 | 360 | } |
3364 | | |
3365 | | /* setup .BTF.ext to ELF section mapping */ |
3366 | 106 | ext_segs[0] = &obj->btf_ext->func_info; |
3367 | 106 | ext_segs[1] = &obj->btf_ext->line_info; |
3368 | 106 | ext_segs[2] = &obj->btf_ext->core_relo_info; |
3369 | 424 | for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) { |
3370 | 318 | struct btf_ext_info *seg = ext_segs[seg_num]; |
3371 | 318 | const struct btf_ext_info_sec *sec; |
3372 | 318 | const char *sec_name; |
3373 | 318 | Elf_Scn *scn; |
3374 | | |
3375 | 318 | if (seg->sec_cnt == 0) |
3376 | 214 | continue; |
3377 | | |
3378 | 104 | seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); |
3379 | 104 | if (!seg->sec_idxs) { |
3380 | 0 | err = -ENOMEM; |
3381 | 0 | goto out; |
3382 | 0 | } |
3383 | | |
3384 | 104 | sec_num = 0; |
3385 | 137 | for_each_btf_ext_sec(seg, sec) { |
3386 | | /* preventively increment index to avoid doing |
3387 | | * this before every continue below |
3388 | | */ |
3389 | 137 | sec_num++; |
3390 | | |
3391 | 137 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
3392 | 137 | if (str_is_empty(sec_name)) |
3393 | 28 | continue; |
3394 | 109 | scn = elf_sec_by_name(obj, sec_name); |
3395 | 109 | if (!scn) |
3396 | 101 | continue; |
3397 | | |
3398 | 8 | seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); |
3399 | 8 | } |
3400 | 104 | } |
3401 | 106 | } |
3402 | 5.19k | out: |
3403 | 5.19k | if (err && libbpf_needs_btf(obj)) { |
3404 | 62 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
3405 | 62 | return err; |
3406 | 62 | } |
3407 | 5.13k | return 0; |
3408 | 5.19k | } |
3409 | | |
3410 | | static int compare_vsi_off(const void *_a, const void *_b) |
3411 | 99 | { |
3412 | 99 | const struct btf_var_secinfo *a = _a; |
3413 | 99 | const struct btf_var_secinfo *b = _b; |
3414 | | |
3415 | 99 | return a->offset - b->offset; |
3416 | 99 | } |
3417 | | |
3418 | | static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, |
3419 | | struct btf_type *t) |
3420 | 2.22k | { |
3421 | 2.22k | __u32 size = 0, i, vars = btf_vlen(t); |
3422 | 2.22k | const char *sec_name = btf__name_by_offset(btf, t->name_off); |
3423 | 2.22k | struct btf_var_secinfo *vsi; |
3424 | 2.22k | bool fixup_offsets = false; |
3425 | 2.22k | int err; |
3426 | | |
3427 | 2.22k | if (!sec_name) { |
3428 | 0 | pr_debug("No name found in string section for DATASEC kind.\n"); |
3429 | 0 | return -ENOENT; |
3430 | 0 | } |
3431 | | |
3432 | | /* Extern-backing datasecs (.ksyms, .kconfig) have their size and |
3433 | | * variable offsets set at the previous step. Further, not every |
3434 | | * extern BTF VAR has corresponding ELF symbol preserved, so we skip |
3435 | | * all fixups altogether for such sections and go straight to sorting |
3436 | | * VARs within their DATASEC. |
3437 | | */ |
3438 | 2.22k | if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0) |
3439 | 232 | goto sort_vars; |
3440 | | |
3441 | | /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to |
3442 | | * fix this up. But BPF static linker already fixes this up and fills |
3443 | | * all the sizes and offsets during static linking. So this step has |
3444 | | * to be optional. But the STV_HIDDEN handling is non-optional for any |
3445 | | * non-extern DATASEC, so the variable fixup loop below handles both |
3446 | | * functions at the same time, paying the cost of BTF VAR <-> ELF |
3447 | | * symbol matching just once. |
3448 | | */ |
3449 | 1.99k | if (t->size == 0) { |
3450 | 605 | err = find_elf_sec_sz(obj, sec_name, &size); |
3451 | 605 | if (err || !size) { |
3452 | 320 | pr_debug("sec '%s': failed to determine size from ELF: size %u, err %s\n", |
3453 | 320 | sec_name, size, errstr(err)); |
3454 | 320 | return -ENOENT; |
3455 | 320 | } |
3456 | | |
3457 | 285 | t->size = size; |
3458 | 285 | fixup_offsets = true; |
3459 | 285 | } |
3460 | | |
3461 | 2.27k | for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { |
3462 | 1.15k | const struct btf_type *t_var; |
3463 | 1.15k | struct btf_var *var; |
3464 | 1.15k | const char *var_name; |
3465 | 1.15k | Elf64_Sym *sym; |
3466 | | |
3467 | 1.15k | t_var = btf__type_by_id(btf, vsi->type); |
3468 | 1.15k | if (!t_var || !btf_is_var(t_var)) { |
3469 | 152 | pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); |
3470 | 152 | return -EINVAL; |
3471 | 152 | } |
3472 | | |
3473 | 1.00k | var = btf_var(t_var); |
3474 | 1.00k | if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) |
3475 | 139 | continue; |
3476 | | |
3477 | 868 | var_name = btf__name_by_offset(btf, t_var->name_off); |
3478 | 868 | if (!var_name) { |
3479 | 0 | pr_debug("sec '%s': failed to find name of DATASEC's member #%u\n", |
3480 | 0 | sec_name, i); |
3481 | 0 | return -ENOENT; |
3482 | 0 | } |
3483 | | |
3484 | 868 | sym = find_elf_var_sym(obj, var_name); |
3485 | 868 | if (IS_ERR(sym)) { |
3486 | 408 | pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n", |
3487 | 408 | sec_name, var_name); |
3488 | 408 | return -ENOENT; |
3489 | 408 | } |
3490 | | |
3491 | 460 | if (fixup_offsets) |
3492 | 102 | vsi->offset = sym->st_value; |
3493 | | |
3494 | | /* if variable is a global/weak symbol, but has restricted |
3495 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR |
3496 | | * as static. This follows similar logic for functions (BPF |
3497 | | * subprogs) and influences libbpf's further decisions about |
3498 | | * whether to make global data BPF array maps as |
3499 | | * BPF_F_MMAPABLE. |
3500 | | */ |
3501 | 460 | if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
3502 | 415 | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) |
3503 | 111 | var->linkage = BTF_VAR_STATIC; |
3504 | 460 | } |
3505 | | |
3506 | 1.34k | sort_vars: |
3507 | 1.34k | qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off); |
3508 | 1.34k | return 0; |
3509 | 1.67k | } |
3510 | | |
3511 | | static int bpf_object_fixup_btf(struct bpf_object *obj) |
3512 | 3.22k | { |
3513 | 3.22k | int i, n, err = 0; |
3514 | | |
3515 | 3.22k | if (!obj->btf) |
3516 | 1.58k | return 0; |
3517 | | |
3518 | 1.63k | n = btf__type_cnt(obj->btf); |
3519 | 15.8k | for (i = 1; i < n; i++) { |
3520 | 15.1k | struct btf_type *t = btf_type_by_id(obj->btf, i); |
3521 | | |
3522 | | /* Loader needs to fix up some of the things compiler |
3523 | | * couldn't get its hands on while emitting BTF. This |
3524 | | * is section size and global variable offset. We use |
3525 | | * the info from the ELF itself for this purpose. |
3526 | | */ |
3527 | 15.1k | if (btf_is_datasec(t)) { |
3528 | 2.22k | err = btf_fixup_datasec(obj, obj->btf, t); |
3529 | 2.22k | if (err) |
3530 | 880 | return err; |
3531 | 2.22k | } |
3532 | 15.1k | } |
3533 | | |
3534 | 759 | return 0; |
3535 | 1.63k | } |
3536 | | |
3537 | | static bool prog_needs_vmlinux_btf(struct bpf_program *prog) |
3538 | 0 | { |
3539 | 0 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || |
3540 | 0 | prog->type == BPF_PROG_TYPE_LSM) |
3541 | 0 | return true; |
3542 | | |
3543 | | /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs |
3544 | | * also need vmlinux BTF |
3545 | | */ |
3546 | 0 | if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) |
3547 | 0 | return true; |
3548 | | |
3549 | 0 | return false; |
3550 | 0 | } |
3551 | | |
3552 | | static bool map_needs_vmlinux_btf(struct bpf_map *map) |
3553 | 0 | { |
3554 | 0 | return bpf_map__is_struct_ops(map); |
3555 | 0 | } |
3556 | | |
3557 | | static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) |
3558 | 0 | { |
3559 | 0 | struct bpf_program *prog; |
3560 | 0 | struct bpf_map *map; |
3561 | 0 | int i; |
3562 | | |
3563 | | /* CO-RE relocations need kernel BTF, only when btf_custom_path |
3564 | | * is not specified |
3565 | | */ |
3566 | 0 | if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) |
3567 | 0 | return true; |
3568 | | |
3569 | | /* Support for typed ksyms needs kernel BTF */ |
3570 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
3571 | 0 | const struct extern_desc *ext; |
3572 | |
|
3573 | 0 | ext = &obj->externs[i]; |
3574 | 0 | if (ext->type == EXT_KSYM && ext->ksym.type_id) |
3575 | 0 | return true; |
3576 | 0 | } |
3577 | | |
3578 | 0 | bpf_object__for_each_program(prog, obj) { |
3579 | 0 | if (!prog->autoload) |
3580 | 0 | continue; |
3581 | 0 | if (prog_needs_vmlinux_btf(prog)) |
3582 | 0 | return true; |
3583 | 0 | } |
3584 | | |
3585 | 0 | bpf_object__for_each_map(map, obj) { |
3586 | 0 | if (map_needs_vmlinux_btf(map)) |
3587 | 0 | return true; |
3588 | 0 | } |
3589 | | |
3590 | 0 | return false; |
3591 | 0 | } |
3592 | | |
3593 | | static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) |
3594 | 0 | { |
3595 | 0 | int err; |
3596 | | |
3597 | | /* btf_vmlinux could be loaded earlier */ |
3598 | 0 | if (obj->btf_vmlinux || obj->gen_loader) |
3599 | 0 | return 0; |
3600 | | |
3601 | 0 | if (!force && !obj_needs_vmlinux_btf(obj)) |
3602 | 0 | return 0; |
3603 | | |
3604 | 0 | obj->btf_vmlinux = btf__load_vmlinux_btf(); |
3605 | 0 | err = libbpf_get_error(obj->btf_vmlinux); |
3606 | 0 | if (err) { |
3607 | 0 | pr_warn("Error loading vmlinux BTF: %s\n", errstr(err)); |
3608 | 0 | obj->btf_vmlinux = NULL; |
3609 | 0 | return err; |
3610 | 0 | } |
3611 | 0 | return 0; |
3612 | 0 | } |
3613 | | |
3614 | | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
3615 | 0 | { |
3616 | 0 | struct btf *kern_btf = obj->btf; |
3617 | 0 | bool btf_mandatory, sanitize; |
3618 | 0 | int i, err = 0; |
3619 | |
|
3620 | 0 | if (!obj->btf) |
3621 | 0 | return 0; |
3622 | | |
3623 | 0 | if (!kernel_supports(obj, FEAT_BTF)) { |
3624 | 0 | if (kernel_needs_btf(obj)) { |
3625 | 0 | err = -EOPNOTSUPP; |
3626 | 0 | goto report; |
3627 | 0 | } |
3628 | 0 | pr_debug("Kernel doesn't support BTF, skipping uploading it.\n"); |
3629 | 0 | return 0; |
3630 | 0 | } |
3631 | | |
3632 | | /* Even though some subprogs are global/weak, user might prefer more |
3633 | | * permissive BPF verification process that BPF verifier performs for |
3634 | | * static functions, taking into account more context from the caller |
3635 | | * functions. In such case, they need to mark such subprogs with |
3636 | | * __attribute__((visibility("hidden"))) and libbpf will adjust |
3637 | | * corresponding FUNC BTF type to be marked as static and trigger more |
3638 | | * involved BPF verification process. |
3639 | | */ |
3640 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
3641 | 0 | struct bpf_program *prog = &obj->programs[i]; |
3642 | 0 | struct btf_type *t; |
3643 | 0 | const char *name; |
3644 | 0 | int j, n; |
3645 | |
|
3646 | 0 | if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) |
3647 | 0 | continue; |
3648 | | |
3649 | 0 | n = btf__type_cnt(obj->btf); |
3650 | 0 | for (j = 1; j < n; j++) { |
3651 | 0 | t = btf_type_by_id(obj->btf, j); |
3652 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) |
3653 | 0 | continue; |
3654 | | |
3655 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
3656 | 0 | if (strcmp(name, prog->name) != 0) |
3657 | 0 | continue; |
3658 | | |
3659 | 0 | t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); |
3660 | 0 | break; |
3661 | 0 | } |
3662 | 0 | } |
3663 | |
|
3664 | 0 | sanitize = btf_needs_sanitization(obj); |
3665 | 0 | if (sanitize) { |
3666 | 0 | kern_btf = bpf_object__sanitize_btf(obj, obj->btf); |
3667 | 0 | if (IS_ERR(kern_btf)) |
3668 | 0 | return PTR_ERR(kern_btf); |
3669 | 0 | } |
3670 | | |
3671 | 0 | if (obj->gen_loader) { |
3672 | 0 | __u32 raw_size = 0; |
3673 | 0 | const void *raw_data = btf__raw_data(kern_btf, &raw_size); |
3674 | |
|
3675 | 0 | if (!raw_data) |
3676 | 0 | return -ENOMEM; |
3677 | 0 | bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); |
3678 | | /* Pretend to have valid FD to pass various fd >= 0 checks. |
3679 | | * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. |
3680 | | */ |
3681 | 0 | btf__set_fd(kern_btf, 0); |
3682 | 0 | } else { |
3683 | | /* currently BPF_BTF_LOAD only supports log_level 1 */ |
3684 | 0 | err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, |
3685 | 0 | obj->log_level ? 1 : 0, obj->token_fd); |
3686 | 0 | } |
3687 | 0 | if (sanitize) { |
3688 | 0 | if (!err) { |
3689 | | /* move fd to libbpf's BTF */ |
3690 | 0 | btf__set_fd(obj->btf, btf__fd(kern_btf)); |
3691 | 0 | btf__set_fd(kern_btf, -1); |
3692 | 0 | } |
3693 | 0 | btf__free(kern_btf); |
3694 | 0 | } |
3695 | 0 | report: |
3696 | 0 | if (err) { |
3697 | 0 | btf_mandatory = kernel_needs_btf(obj); |
3698 | 0 | if (btf_mandatory) { |
3699 | 0 | pr_warn("Error loading .BTF into kernel: %s. BTF is mandatory, can't proceed.\n", |
3700 | 0 | errstr(err)); |
3701 | 0 | } else { |
3702 | 0 | pr_info("Error loading .BTF into kernel: %s. BTF is optional, ignoring.\n", |
3703 | 0 | errstr(err)); |
3704 | 0 | err = 0; |
3705 | 0 | } |
3706 | 0 | } |
3707 | 0 | return err; |
3708 | 0 | } |
3709 | | |
3710 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off) |
3711 | 24.3k | { |
3712 | 24.3k | const char *name; |
3713 | | |
3714 | 24.3k | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); |
3715 | 24.3k | if (!name) { |
3716 | 7.07k | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3717 | 7.07k | off, obj->path, elf_errmsg(-1)); |
3718 | 7.07k | return NULL; |
3719 | 7.07k | } |
3720 | | |
3721 | 17.3k | return name; |
3722 | 24.3k | } |
3723 | | |
3724 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off) |
3725 | 76.7k | { |
3726 | 76.7k | const char *name; |
3727 | | |
3728 | 76.7k | name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); |
3729 | 76.7k | if (!name) { |
3730 | 1.61k | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3731 | 1.61k | off, obj->path, elf_errmsg(-1)); |
3732 | 1.61k | return NULL; |
3733 | 1.61k | } |
3734 | | |
3735 | 75.1k | return name; |
3736 | 76.7k | } |
3737 | | |
3738 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx) |
3739 | 13.8k | { |
3740 | 13.8k | Elf_Scn *scn; |
3741 | | |
3742 | 13.8k | scn = elf_getscn(obj->efile.elf, idx); |
3743 | 13.8k | if (!scn) { |
3744 | 0 | pr_warn("elf: failed to get section(%zu) from %s: %s\n", |
3745 | 0 | idx, obj->path, elf_errmsg(-1)); |
3746 | 0 | return NULL; |
3747 | 0 | } |
3748 | 13.8k | return scn; |
3749 | 13.8k | } |
3750 | | |
3751 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name) |
3752 | 714 | { |
3753 | 714 | Elf_Scn *scn = NULL; |
3754 | 714 | Elf *elf = obj->efile.elf; |
3755 | 714 | const char *sec_name; |
3756 | | |
3757 | 6.49k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3758 | 6.09k | sec_name = elf_sec_name(obj, scn); |
3759 | 6.09k | if (!sec_name) |
3760 | 0 | return NULL; |
3761 | | |
3762 | 6.09k | if (strcmp(sec_name, name) != 0) |
3763 | 5.77k | continue; |
3764 | | |
3765 | 318 | return scn; |
3766 | 6.09k | } |
3767 | 396 | return NULL; |
3768 | 714 | } |
3769 | | |
3770 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn) |
3771 | 170k | { |
3772 | 170k | Elf64_Shdr *shdr; |
3773 | | |
3774 | 170k | if (!scn) |
3775 | 0 | return NULL; |
3776 | | |
3777 | 170k | shdr = elf64_getshdr(scn); |
3778 | 170k | if (!shdr) { |
3779 | 0 | pr_warn("elf: failed to get section(%zu) header from %s: %s\n", |
3780 | 0 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3781 | 0 | return NULL; |
3782 | 0 | } |
3783 | | |
3784 | 170k | return shdr; |
3785 | 170k | } |
3786 | | |
3787 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn) |
3788 | 12.1k | { |
3789 | 12.1k | const char *name; |
3790 | 12.1k | Elf64_Shdr *sh; |
3791 | | |
3792 | 12.1k | if (!scn) |
3793 | 0 | return NULL; |
3794 | | |
3795 | 12.1k | sh = elf_sec_hdr(obj, scn); |
3796 | 12.1k | if (!sh) |
3797 | 0 | return NULL; |
3798 | | |
3799 | 12.1k | name = elf_sec_str(obj, sh->sh_name); |
3800 | 12.1k | if (!name) { |
3801 | 788 | pr_warn("elf: failed to get section(%zu) name from %s: %s\n", |
3802 | 788 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3803 | 788 | return NULL; |
3804 | 788 | } |
3805 | | |
3806 | 11.3k | return name; |
3807 | 12.1k | } |
3808 | | |
3809 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn) |
3810 | 65.5k | { |
3811 | 65.5k | Elf_Data *data; |
3812 | | |
3813 | 65.5k | if (!scn) |
3814 | 295 | return NULL; |
3815 | | |
3816 | 65.2k | data = elf_getdata(scn, 0); |
3817 | 65.2k | if (!data) { |
3818 | 627 | pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n", |
3819 | 627 | elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>", |
3820 | 627 | obj->path, elf_errmsg(-1)); |
3821 | 627 | return NULL; |
3822 | 627 | } |
3823 | | |
3824 | 64.6k | return data; |
3825 | 65.2k | } |
3826 | | |
3827 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx) |
3828 | 356k | { |
3829 | 356k | if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) |
3830 | 188 | return NULL; |
3831 | | |
3832 | 355k | return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; |
3833 | 356k | } |
3834 | | |
3835 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx) |
3836 | 10.3k | { |
3837 | 10.3k | if (idx >= data->d_size / sizeof(Elf64_Rel)) |
3838 | 0 | return NULL; |
3839 | | |
3840 | 10.3k | return (Elf64_Rel *)data->d_buf + idx; |
3841 | 10.3k | } |
3842 | | |
3843 | | static bool is_sec_name_dwarf(const char *name) |
3844 | 59.8k | { |
3845 | | /* approximation, but the actual list is too long */ |
3846 | 59.8k | return str_has_pfx(name, ".debug_"); |
3847 | 59.8k | } |
3848 | | |
3849 | | static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name) |
3850 | 62.7k | { |
3851 | | /* no special handling of .strtab */ |
3852 | 62.7k | if (hdr->sh_type == SHT_STRTAB) |
3853 | 4.29k | return true; |
3854 | | |
3855 | | /* ignore .llvm_addrsig section as well */ |
3856 | 58.4k | if (hdr->sh_type == SHT_LLVM_ADDRSIG) |
3857 | 1.14k | return true; |
3858 | | |
3859 | | /* no subprograms will lead to an empty .text section, ignore it */ |
3860 | 57.3k | if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && |
3861 | 764 | strcmp(name, ".text") == 0) |
3862 | 167 | return true; |
3863 | | |
3864 | | /* DWARF sections */ |
3865 | 57.1k | if (is_sec_name_dwarf(name)) |
3866 | 1.12k | return true; |
3867 | | |
3868 | 56.0k | if (str_has_pfx(name, ".rel")) { |
3869 | 2.67k | name += sizeof(".rel") - 1; |
3870 | | /* DWARF section relocations */ |
3871 | 2.67k | if (is_sec_name_dwarf(name)) |
3872 | 240 | return true; |
3873 | | |
3874 | | /* .BTF and .BTF.ext don't need relocations */ |
3875 | 2.43k | if (strcmp(name, BTF_ELF_SEC) == 0 || |
3876 | 2.14k | strcmp(name, BTF_EXT_ELF_SEC) == 0) |
3877 | 752 | return true; |
3878 | 2.43k | } |
3879 | | |
3880 | 55.0k | return false; |
3881 | 56.0k | } |
3882 | | |
3883 | | static int cmp_progs(const void *_a, const void *_b) |
3884 | 50.4k | { |
3885 | 50.4k | const struct bpf_program *a = _a; |
3886 | 50.4k | const struct bpf_program *b = _b; |
3887 | | |
3888 | 50.4k | if (a->sec_idx != b->sec_idx) |
3889 | 413 | return a->sec_idx < b->sec_idx ? -1 : 1; |
3890 | | |
3891 | | /* sec_insn_off can't be the same within the section */ |
3892 | 50.0k | return a->sec_insn_off < b->sec_insn_off ? -1 : 1; |
3893 | 50.4k | } |
3894 | | |
3895 | | static int bpf_object__elf_collect(struct bpf_object *obj) |
3896 | 8.75k | { |
3897 | 8.75k | struct elf_sec_desc *sec_desc; |
3898 | 8.75k | Elf *elf = obj->efile.elf; |
3899 | 8.75k | Elf_Data *btf_ext_data = NULL; |
3900 | 8.75k | Elf_Data *btf_data = NULL; |
3901 | 8.75k | int idx = 0, err = 0; |
3902 | 8.75k | const char *name; |
3903 | 8.75k | Elf_Data *data; |
3904 | 8.75k | Elf_Scn *scn; |
3905 | 8.75k | Elf64_Shdr *sh; |
3906 | | |
3907 | | /* ELF section indices are 0-based, but sec #0 is special "invalid" |
3908 | | * section. Since section count retrieved by elf_getshdrnum() does |
3909 | | * include sec #0, it is already the necessary size of an array to keep |
3910 | | * all the sections. |
3911 | | */ |
3912 | 8.75k | if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { |
3913 | 0 | pr_warn("elf: failed to get the number of sections for %s: %s\n", |
3914 | 0 | obj->path, elf_errmsg(-1)); |
3915 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3916 | 0 | } |
3917 | 8.75k | obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); |
3918 | 8.75k | if (!obj->efile.secs) |
3919 | 0 | return -ENOMEM; |
3920 | | |
3921 | | /* a bunch of ELF parsing functionality depends on processing symbols, |
3922 | | * so do the first pass and find the symbol table |
3923 | | */ |
3924 | 8.75k | scn = NULL; |
3925 | 95.3k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3926 | 86.9k | sh = elf_sec_hdr(obj, scn); |
3927 | 86.9k | if (!sh) |
3928 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3929 | | |
3930 | 86.9k | if (sh->sh_type == SHT_SYMTAB) { |
3931 | 8.64k | if (obj->efile.symbols) { |
3932 | 4 | pr_warn("elf: multiple symbol tables in %s\n", obj->path); |
3933 | 4 | return -LIBBPF_ERRNO__FORMAT; |
3934 | 4 | } |
3935 | | |
3936 | 8.64k | data = elf_sec_data(obj, scn); |
3937 | 8.64k | if (!data) |
3938 | 279 | return -LIBBPF_ERRNO__FORMAT; |
3939 | | |
3940 | 8.36k | idx = elf_ndxscn(scn); |
3941 | | |
3942 | 8.36k | obj->efile.symbols = data; |
3943 | 8.36k | obj->efile.symbols_shndx = idx; |
3944 | 8.36k | obj->efile.strtabidx = sh->sh_link; |
3945 | 8.36k | } |
3946 | 86.9k | } |
3947 | | |
3948 | 8.46k | if (!obj->efile.symbols) { |
3949 | 106 | pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n", |
3950 | 106 | obj->path); |
3951 | 106 | return -ENOENT; |
3952 | 106 | } |
3953 | | |
3954 | 8.36k | scn = NULL; |
3955 | 70.2k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3956 | 63.5k | idx = elf_ndxscn(scn); |
3957 | 63.5k | sec_desc = &obj->efile.secs[idx]; |
3958 | | |
3959 | 63.5k | sh = elf_sec_hdr(obj, scn); |
3960 | 63.5k | if (!sh) |
3961 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3962 | | |
3963 | 63.5k | name = elf_sec_str(obj, sh->sh_name); |
3964 | 63.5k | if (!name) |
3965 | 823 | return -LIBBPF_ERRNO__FORMAT; |
3966 | | |
3967 | 62.7k | if (ignore_elf_section(sh, name)) |
3968 | 7.72k | continue; |
3969 | | |
3970 | 55.0k | data = elf_sec_data(obj, scn); |
3971 | 55.0k | if (!data) |
3972 | 330 | return -LIBBPF_ERRNO__FORMAT; |
3973 | | |
3974 | 54.7k | pr_debug("elf: section(%d) %s, size %lu, link %d, flags %lx, type=%d\n", |
3975 | 54.7k | idx, name, (unsigned long)data->d_size, |
3976 | 54.7k | (int)sh->sh_link, (unsigned long)sh->sh_flags, |
3977 | 54.7k | (int)sh->sh_type); |
3978 | | |
3979 | 54.7k | if (strcmp(name, "license") == 0) { |
3980 | 949 | err = bpf_object__init_license(obj, data->d_buf, data->d_size); |
3981 | 949 | if (err) |
3982 | 5 | return err; |
3983 | 53.7k | } else if (strcmp(name, "version") == 0) { |
3984 | 70 | err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); |
3985 | 70 | if (err) |
3986 | 13 | return err; |
3987 | 53.6k | } else if (strcmp(name, "maps") == 0) { |
3988 | 9 | pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n"); |
3989 | 9 | return -ENOTSUP; |
3990 | 53.6k | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
3991 | 998 | obj->efile.btf_maps_shndx = idx; |
3992 | 52.6k | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
3993 | 3.57k | if (sh->sh_type != SHT_PROGBITS) |
3994 | 59 | return -LIBBPF_ERRNO__FORMAT; |
3995 | 3.51k | btf_data = data; |
3996 | 49.1k | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
3997 | 597 | if (sh->sh_type != SHT_PROGBITS) |
3998 | 42 | return -LIBBPF_ERRNO__FORMAT; |
3999 | 555 | btf_ext_data = data; |
4000 | 48.5k | } else if (sh->sh_type == SHT_SYMTAB) { |
4001 | | /* already processed during the first pass above */ |
4002 | 41.2k | } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { |
4003 | 5.50k | if (sh->sh_flags & SHF_EXECINSTR) { |
4004 | 1.95k | if (strcmp(name, ".text") == 0) |
4005 | 237 | obj->efile.text_shndx = idx; |
4006 | 1.95k | err = bpf_object__add_programs(obj, data, name, idx); |
4007 | 1.95k | if (err) |
4008 | 265 | return err; |
4009 | 3.55k | } else if (strcmp(name, DATA_SEC) == 0 || |
4010 | 3.45k | str_has_pfx(name, DATA_SEC ".")) { |
4011 | 586 | sec_desc->sec_type = SEC_DATA; |
4012 | 586 | sec_desc->shdr = sh; |
4013 | 586 | sec_desc->data = data; |
4014 | 2.97k | } else if (strcmp(name, RODATA_SEC) == 0 || |
4015 | 2.75k | str_has_pfx(name, RODATA_SEC ".")) { |
4016 | 542 | sec_desc->sec_type = SEC_RODATA; |
4017 | 542 | sec_desc->shdr = sh; |
4018 | 542 | sec_desc->data = data; |
4019 | 2.43k | } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || |
4020 | 2.20k | strcmp(name, STRUCT_OPS_LINK_SEC) == 0 || |
4021 | 2.13k | strcmp(name, "?" STRUCT_OPS_SEC) == 0 || |
4022 | 2.06k | strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) { |
4023 | 431 | sec_desc->sec_type = SEC_ST_OPS; |
4024 | 431 | sec_desc->shdr = sh; |
4025 | 431 | sec_desc->data = data; |
4026 | 431 | obj->efile.has_st_ops = true; |
4027 | 2.00k | } else if (strcmp(name, ARENA_SEC) == 0) { |
4028 | 67 | obj->efile.arena_data = data; |
4029 | 67 | obj->efile.arena_data_shndx = idx; |
4030 | 1.93k | } else if (strcmp(name, JUMPTABLES_SEC) == 0) { |
4031 | 73 | obj->jumptables_data = malloc(data->d_size); |
4032 | 73 | if (!obj->jumptables_data) |
4033 | 0 | return -ENOMEM; |
4034 | 73 | memcpy(obj->jumptables_data, data->d_buf, data->d_size); |
4035 | 73 | obj->jumptables_data_sz = data->d_size; |
4036 | 73 | obj->efile.jumptables_data_shndx = idx; |
4037 | 1.86k | } else { |
4038 | 1.86k | pr_info("elf: skipping unrecognized data section(%d) %s\n", |
4039 | 1.86k | idx, name); |
4040 | 1.86k | } |
4041 | 35.7k | } else if (sh->sh_type == SHT_REL) { |
4042 | 3.24k | int targ_sec_idx = sh->sh_info; /* points to other section */ |
4043 | | |
4044 | 3.24k | if (sh->sh_entsize != sizeof(Elf64_Rel) || |
4045 | 3.14k | targ_sec_idx >= obj->efile.sec_cnt) |
4046 | 179 | return -LIBBPF_ERRNO__FORMAT; |
4047 | | |
4048 | | /* Only do relo for section with exec instructions */ |
4049 | 3.06k | if (!section_have_execinstr(obj, targ_sec_idx) && |
4050 | 1.75k | strcmp(name, ".rel" STRUCT_OPS_SEC) && |
4051 | 1.71k | strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && |
4052 | 1.63k | strcmp(name, ".rel?" STRUCT_OPS_SEC) && |
4053 | 1.56k | strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) && |
4054 | 1.49k | strcmp(name, ".rel" MAPS_ELF_SEC)) { |
4055 | 1.42k | pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", |
4056 | 1.42k | idx, name, targ_sec_idx, |
4057 | 1.42k | elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>"); |
4058 | 1.42k | continue; |
4059 | 1.42k | } |
4060 | | |
4061 | 1.64k | sec_desc->sec_type = SEC_RELO; |
4062 | 1.64k | sec_desc->shdr = sh; |
4063 | 1.64k | sec_desc->data = data; |
4064 | 32.4k | } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || |
4065 | 2.26k | str_has_pfx(name, BSS_SEC "."))) { |
4066 | 2.13k | sec_desc->sec_type = SEC_BSS; |
4067 | 2.13k | sec_desc->shdr = sh; |
4068 | 2.13k | sec_desc->data = data; |
4069 | 30.3k | } else { |
4070 | 30.3k | pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name, |
4071 | 30.3k | (size_t)sh->sh_size); |
4072 | 30.3k | } |
4073 | 54.7k | } |
4074 | | |
4075 | 6.63k | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
4076 | 1.44k | pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); |
4077 | 1.44k | return -LIBBPF_ERRNO__FORMAT; |
4078 | 1.44k | } |
4079 | | |
4080 | | /* change BPF program insns to native endianness for introspection */ |
4081 | 5.19k | if (!is_native_endianness(obj)) |
4082 | 92 | bpf_object_bswap_progs(obj); |
4083 | | |
4084 | | /* sort BPF programs by section name and in-section instruction offset |
4085 | | * for faster search |
4086 | | */ |
4087 | 5.19k | if (obj->nr_programs) |
4088 | 608 | qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); |
4089 | | |
4090 | 5.19k | return bpf_object__init_btf(obj, btf_data, btf_ext_data); |
4091 | 6.63k | } |
4092 | | |
4093 | | static bool sym_is_extern(const Elf64_Sym *sym) |
4094 | 153k | { |
4095 | 153k | int bind = ELF64_ST_BIND(sym->st_info); |
4096 | | /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */ |
4097 | 153k | return sym->st_shndx == SHN_UNDEF && |
4098 | 68.4k | (bind == STB_GLOBAL || bind == STB_WEAK) && |
4099 | 6.15k | ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; |
4100 | 153k | } |
4101 | | |
4102 | | static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx) |
4103 | 1.38k | { |
4104 | 1.38k | int bind = ELF64_ST_BIND(sym->st_info); |
4105 | 1.38k | int type = ELF64_ST_TYPE(sym->st_info); |
4106 | | |
4107 | | /* in .text section */ |
4108 | 1.38k | if (sym->st_shndx != text_shndx) |
4109 | 463 | return false; |
4110 | | |
4111 | | /* local function */ |
4112 | 918 | if (bind == STB_LOCAL && type == STT_SECTION) |
4113 | 274 | return true; |
4114 | | |
4115 | | /* global function */ |
4116 | 644 | return (bind == STB_GLOBAL || bind == STB_WEAK) && type == STT_FUNC; |
4117 | 918 | } |
4118 | | |
4119 | | static int find_extern_btf_id(const struct btf *btf, const char *ext_name) |
4120 | 1.11k | { |
4121 | 1.11k | const struct btf_type *t; |
4122 | 1.11k | const char *tname; |
4123 | 1.11k | int i, n; |
4124 | | |
4125 | 1.11k | if (!btf) |
4126 | 31 | return -ESRCH; |
4127 | | |
4128 | 1.08k | n = btf__type_cnt(btf); |
4129 | 9.26k | for (i = 1; i < n; i++) { |
4130 | 9.11k | t = btf__type_by_id(btf, i); |
4131 | | |
4132 | 9.11k | if (!btf_is_var(t) && !btf_is_func(t)) |
4133 | 6.80k | continue; |
4134 | | |
4135 | 2.30k | tname = btf__name_by_offset(btf, t->name_off); |
4136 | 2.30k | if (strcmp(tname, ext_name)) |
4137 | 1.38k | continue; |
4138 | | |
4139 | 924 | if (btf_is_var(t) && |
4140 | 764 | btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) |
4141 | 48 | return -EINVAL; |
4142 | | |
4143 | 876 | if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN) |
4144 | 3 | return -EINVAL; |
4145 | | |
4146 | 873 | return i; |
4147 | 876 | } |
4148 | | |
4149 | 158 | return -ENOENT; |
4150 | 1.08k | } |
4151 | | |
4152 | 873 | static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) { |
4153 | 873 | const struct btf_var_secinfo *vs; |
4154 | 873 | const struct btf_type *t; |
4155 | 873 | int i, j, n; |
4156 | | |
4157 | 873 | if (!btf) |
4158 | 0 | return -ESRCH; |
4159 | | |
4160 | 873 | n = btf__type_cnt(btf); |
4161 | 11.4k | for (i = 1; i < n; i++) { |
4162 | 11.4k | t = btf__type_by_id(btf, i); |
4163 | | |
4164 | 11.4k | if (!btf_is_datasec(t)) |
4165 | 9.82k | continue; |
4166 | | |
4167 | 1.64k | vs = btf_var_secinfos(t); |
4168 | 2.54k | for (j = 0; j < btf_vlen(t); j++, vs++) { |
4169 | 1.74k | if (vs->type == ext_btf_id) |
4170 | 854 | return i; |
4171 | 1.74k | } |
4172 | 1.64k | } |
4173 | | |
4174 | 19 | return -ENOENT; |
4175 | 873 | } |
4176 | | |
4177 | | static enum kcfg_type find_kcfg_type(const struct btf *btf, int id, |
4178 | | bool *is_signed) |
4179 | 410 | { |
4180 | 410 | const struct btf_type *t; |
4181 | 410 | const char *name; |
4182 | | |
4183 | 410 | t = skip_mods_and_typedefs(btf, id, NULL); |
4184 | 410 | name = btf__name_by_offset(btf, t->name_off); |
4185 | | |
4186 | 410 | if (is_signed) |
4187 | 260 | *is_signed = false; |
4188 | 410 | switch (btf_kind(t)) { |
4189 | 228 | case BTF_KIND_INT: { |
4190 | 228 | int enc = btf_int_encoding(t); |
4191 | | |
4192 | 228 | if (enc & BTF_INT_BOOL) |
4193 | 39 | return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; |
4194 | 189 | if (is_signed) |
4195 | 57 | *is_signed = enc & BTF_INT_SIGNED; |
4196 | 189 | if (t->size == 1) |
4197 | 149 | return KCFG_CHAR; |
4198 | 40 | if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) |
4199 | 20 | return KCFG_UNKNOWN; |
4200 | 20 | return KCFG_INT; |
4201 | 40 | } |
4202 | 4 | case BTF_KIND_ENUM: |
4203 | 4 | if (t->size != 4) |
4204 | 3 | return KCFG_UNKNOWN; |
4205 | 1 | if (strcmp(name, "libbpf_tristate")) |
4206 | 1 | return KCFG_UNKNOWN; |
4207 | 0 | return KCFG_TRISTATE; |
4208 | 3 | case BTF_KIND_ENUM64: |
4209 | 3 | if (strcmp(name, "libbpf_tristate")) |
4210 | 3 | return KCFG_UNKNOWN; |
4211 | 0 | return KCFG_TRISTATE; |
4212 | 150 | case BTF_KIND_ARRAY: |
4213 | 150 | if (btf_array(t)->nelems == 0) |
4214 | 0 | return KCFG_UNKNOWN; |
4215 | 150 | if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) |
4216 | 22 | return KCFG_UNKNOWN; |
4217 | 128 | return KCFG_CHAR_ARR; |
4218 | 25 | default: |
4219 | 25 | return KCFG_UNKNOWN; |
4220 | 410 | } |
4221 | 410 | } |
4222 | | |
4223 | | static int cmp_externs(const void *_a, const void *_b) |
4224 | 521 | { |
4225 | 521 | const struct extern_desc *a = _a; |
4226 | 521 | const struct extern_desc *b = _b; |
4227 | | |
4228 | 521 | if (a->type != b->type) |
4229 | 0 | return a->type < b->type ? -1 : 1; |
4230 | | |
4231 | 521 | if (a->type == EXT_KCFG) { |
4232 | | /* descending order by alignment requirements */ |
4233 | 128 | if (a->kcfg.align != b->kcfg.align) |
4234 | 0 | return a->kcfg.align > b->kcfg.align ? -1 : 1; |
4235 | | /* ascending order by size, within same alignment class */ |
4236 | 128 | if (a->kcfg.sz != b->kcfg.sz) |
4237 | 0 | return a->kcfg.sz < b->kcfg.sz ? -1 : 1; |
4238 | 128 | } |
4239 | | |
4240 | | /* resolve ties by name */ |
4241 | 521 | return strcmp(a->name, b->name); |
4242 | 521 | } |
4243 | | |
4244 | | static int find_int_btf_id(const struct btf *btf) |
4245 | 532 | { |
4246 | 532 | const struct btf_type *t; |
4247 | 532 | int i, n; |
4248 | | |
4249 | 532 | n = btf__type_cnt(btf); |
4250 | 6.18k | for (i = 1; i < n; i++) { |
4251 | 5.86k | t = btf__type_by_id(btf, i); |
4252 | | |
4253 | 5.86k | if (btf_is_int(t) && btf_int_bits(t) == 32) |
4254 | 210 | return i; |
4255 | 5.86k | } |
4256 | | |
4257 | 322 | return 0; |
4258 | 532 | } |
4259 | | |
4260 | | static int add_dummy_ksym_var(struct btf *btf) |
4261 | 3.82k | { |
4262 | 3.82k | int i, int_btf_id, sec_btf_id, dummy_var_btf_id; |
4263 | 3.82k | const struct btf_var_secinfo *vs; |
4264 | 3.82k | const struct btf_type *sec; |
4265 | | |
4266 | 3.82k | if (!btf) |
4267 | 1.61k | return 0; |
4268 | | |
4269 | 2.21k | sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC, |
4270 | 2.21k | BTF_KIND_DATASEC); |
4271 | 2.21k | if (sec_btf_id < 0) |
4272 | 1.69k | return 0; |
4273 | | |
4274 | 516 | sec = btf__type_by_id(btf, sec_btf_id); |
4275 | 516 | vs = btf_var_secinfos(sec); |
4276 | 782 | for (i = 0; i < btf_vlen(sec); i++, vs++) { |
4277 | 666 | const struct btf_type *vt; |
4278 | | |
4279 | 666 | vt = btf__type_by_id(btf, vs->type); |
4280 | 666 | if (btf_is_func(vt)) |
4281 | 400 | break; |
4282 | 666 | } |
4283 | | |
4284 | | /* No func in ksyms sec. No need to add dummy var. */ |
4285 | 516 | if (i == btf_vlen(sec)) |
4286 | 116 | return 0; |
4287 | | |
4288 | 400 | int_btf_id = find_int_btf_id(btf); |
4289 | 400 | dummy_var_btf_id = btf__add_var(btf, |
4290 | 400 | "dummy_ksym", |
4291 | 400 | BTF_VAR_GLOBAL_ALLOCATED, |
4292 | 400 | int_btf_id); |
4293 | 400 | if (dummy_var_btf_id < 0) |
4294 | 400 | pr_warn("cannot create a dummy_ksym var\n"); |
4295 | | |
4296 | 400 | return dummy_var_btf_id; |
4297 | 516 | } |
4298 | | |
4299 | | static int bpf_object__collect_externs(struct bpf_object *obj) |
4300 | 5.13k | { |
4301 | 5.13k | struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL; |
4302 | 5.13k | const struct btf_type *t; |
4303 | 5.13k | struct extern_desc *ext; |
4304 | 5.13k | int i, n, off, dummy_var_btf_id; |
4305 | 5.13k | const char *ext_name, *sec_name; |
4306 | 5.13k | size_t ext_essent_len; |
4307 | 5.13k | Elf_Scn *scn; |
4308 | 5.13k | Elf64_Shdr *sh; |
4309 | | |
4310 | 5.13k | if (!obj->efile.symbols) |
4311 | 0 | return 0; |
4312 | | |
4313 | 5.13k | scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); |
4314 | 5.13k | sh = elf_sec_hdr(obj, scn); |
4315 | 5.13k | if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) |
4316 | 1.30k | return -LIBBPF_ERRNO__FORMAT; |
4317 | | |
4318 | 3.82k | dummy_var_btf_id = add_dummy_ksym_var(obj->btf); |
4319 | 3.82k | if (dummy_var_btf_id < 0) |
4320 | 0 | return dummy_var_btf_id; |
4321 | | |
4322 | 3.82k | n = sh->sh_size / sh->sh_entsize; |
4323 | 3.82k | pr_debug("looking for externs among %d symbols...\n", n); |
4324 | | |
4325 | 154k | for (i = 0; i < n; i++) { |
4326 | 151k | Elf64_Sym *sym = elf_sym_by_idx(obj, i); |
4327 | | |
4328 | 151k | if (!sym) |
4329 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4330 | 151k | if (!sym_is_extern(sym)) |
4331 | 148k | continue; |
4332 | 2.87k | ext_name = elf_sym_str(obj, sym->st_name); |
4333 | 2.87k | if (str_is_empty(ext_name)) |
4334 | 1.75k | continue; |
4335 | | |
4336 | 1.11k | ext = obj->externs; |
4337 | 1.11k | ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); |
4338 | 1.11k | if (!ext) |
4339 | 0 | return -ENOMEM; |
4340 | 1.11k | obj->externs = ext; |
4341 | 1.11k | ext = &ext[obj->nr_extern]; |
4342 | 1.11k | memset(ext, 0, sizeof(*ext)); |
4343 | 1.11k | obj->nr_extern++; |
4344 | | |
4345 | 1.11k | ext->btf_id = find_extern_btf_id(obj->btf, ext_name); |
4346 | 1.11k | if (ext->btf_id <= 0) { |
4347 | 240 | pr_warn("failed to find BTF for extern '%s': %d\n", |
4348 | 240 | ext_name, ext->btf_id); |
4349 | 240 | return ext->btf_id; |
4350 | 240 | } |
4351 | 873 | t = btf__type_by_id(obj->btf, ext->btf_id); |
4352 | 873 | ext->name = strdup(btf__name_by_offset(obj->btf, t->name_off)); |
4353 | 873 | if (!ext->name) |
4354 | 0 | return -ENOMEM; |
4355 | 873 | ext->sym_idx = i; |
4356 | 873 | ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; |
4357 | | |
4358 | 873 | ext_essent_len = bpf_core_essential_name_len(ext->name); |
4359 | 873 | ext->essent_name = NULL; |
4360 | 873 | if (ext_essent_len != strlen(ext->name)) { |
4361 | 11 | ext->essent_name = strndup(ext->name, ext_essent_len); |
4362 | 11 | if (!ext->essent_name) |
4363 | 0 | return -ENOMEM; |
4364 | 11 | } |
4365 | | |
4366 | 873 | ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); |
4367 | 873 | if (ext->sec_btf_id <= 0) { |
4368 | 19 | pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n", |
4369 | 19 | ext_name, ext->btf_id, ext->sec_btf_id); |
4370 | 19 | return ext->sec_btf_id; |
4371 | 19 | } |
4372 | 854 | sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); |
4373 | 854 | sec_name = btf__name_by_offset(obj->btf, sec->name_off); |
4374 | | |
4375 | 854 | if (strcmp(sec_name, KCONFIG_SEC) == 0) { |
4376 | 351 | if (btf_is_func(t)) { |
4377 | 1 | pr_warn("extern function %s is unsupported under %s section\n", |
4378 | 1 | ext->name, KCONFIG_SEC); |
4379 | 1 | return -ENOTSUP; |
4380 | 1 | } |
4381 | 350 | kcfg_sec = sec; |
4382 | 350 | ext->type = EXT_KCFG; |
4383 | 350 | ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); |
4384 | 350 | if (ext->kcfg.sz <= 0) { |
4385 | 86 | pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n", |
4386 | 86 | ext_name, ext->kcfg.sz); |
4387 | 86 | return ext->kcfg.sz; |
4388 | 86 | } |
4389 | 264 | ext->kcfg.align = btf__align_of(obj->btf, t->type); |
4390 | 264 | if (ext->kcfg.align <= 0) { |
4391 | 4 | pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n", |
4392 | 4 | ext_name, ext->kcfg.align); |
4393 | 4 | return -EINVAL; |
4394 | 4 | } |
4395 | 260 | ext->kcfg.type = find_kcfg_type(obj->btf, t->type, |
4396 | 260 | &ext->kcfg.is_signed); |
4397 | 260 | if (ext->kcfg.type == KCFG_UNKNOWN) { |
4398 | 75 | pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name); |
4399 | 75 | return -ENOTSUP; |
4400 | 75 | } |
4401 | 503 | } else if (strcmp(sec_name, KSYMS_SEC) == 0) { |
4402 | 423 | ksym_sec = sec; |
4403 | 423 | ext->type = EXT_KSYM; |
4404 | 423 | skip_mods_and_typedefs(obj->btf, t->type, |
4405 | 423 | &ext->ksym.type_id); |
4406 | 423 | } else { |
4407 | 80 | pr_warn("unrecognized extern section '%s'\n", sec_name); |
4408 | 80 | return -ENOTSUP; |
4409 | 80 | } |
4410 | 854 | } |
4411 | 3.32k | pr_debug("collected %d externs total\n", obj->nr_extern); |
4412 | | |
4413 | 3.32k | if (!obj->nr_extern) |
4414 | 3.11k | return 0; |
4415 | | |
4416 | | /* sort externs by type, for kcfg ones also by (align, size, name) */ |
4417 | 205 | qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); |
4418 | | |
4419 | | /* for .ksyms section, we need to turn all externs into allocated |
4420 | | * variables in BTF to pass kernel verification; we do this by |
4421 | | * pretending that each extern is a 8-byte variable |
4422 | | */ |
4423 | 205 | if (ksym_sec) { |
4424 | | /* find existing 4-byte integer type in BTF to use for fake |
4425 | | * extern variables in DATASEC |
4426 | | */ |
4427 | 132 | int int_btf_id = find_int_btf_id(obj->btf); |
4428 | | /* For extern function, a dummy_var added earlier |
4429 | | * will be used to replace the vs->type and |
4430 | | * its name string will be used to refill |
4431 | | * the missing param's name. |
4432 | | */ |
4433 | 132 | const struct btf_type *dummy_var; |
4434 | | |
4435 | 132 | dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); |
4436 | 539 | for (i = 0; i < obj->nr_extern; i++) { |
4437 | 407 | ext = &obj->externs[i]; |
4438 | 407 | if (ext->type != EXT_KSYM) |
4439 | 0 | continue; |
4440 | 407 | pr_debug("extern (ksym) #%d: symbol %d, name %s\n", |
4441 | 407 | i, ext->sym_idx, ext->name); |
4442 | 407 | } |
4443 | | |
4444 | 132 | sec = ksym_sec; |
4445 | 132 | n = btf_vlen(sec); |
4446 | 315 | for (i = 0, off = 0; i < n; i++, off += sizeof(int)) { |
4447 | 263 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4448 | 263 | struct btf_type *vt; |
4449 | | |
4450 | 263 | vt = (void *)btf__type_by_id(obj->btf, vs->type); |
4451 | 263 | ext_name = btf__name_by_offset(obj->btf, vt->name_off); |
4452 | 263 | ext = find_extern_by_name(obj, ext_name); |
4453 | 263 | if (!ext) { |
4454 | 80 | pr_warn("failed to find extern definition for BTF %s '%s'\n", |
4455 | 80 | btf_kind_str(vt), ext_name); |
4456 | 80 | return -ESRCH; |
4457 | 80 | } |
4458 | 183 | if (btf_is_func(vt)) { |
4459 | 84 | const struct btf_type *func_proto; |
4460 | 84 | struct btf_param *param; |
4461 | 84 | int j; |
4462 | | |
4463 | 84 | func_proto = btf__type_by_id(obj->btf, |
4464 | 84 | vt->type); |
4465 | 84 | param = btf_params(func_proto); |
4466 | | /* Reuse the dummy_var string if the |
4467 | | * func proto does not have param name. |
4468 | | */ |
4469 | 148 | for (j = 0; j < btf_vlen(func_proto); j++) |
4470 | 64 | if (param[j].type && !param[j].name_off) |
4471 | 17 | param[j].name_off = |
4472 | 17 | dummy_var->name_off; |
4473 | 84 | vs->type = dummy_var_btf_id; |
4474 | 84 | vt->info &= ~0xffff; |
4475 | 84 | vt->info |= BTF_FUNC_GLOBAL; |
4476 | 99 | } else { |
4477 | 99 | btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4478 | 99 | vt->type = int_btf_id; |
4479 | 99 | } |
4480 | 183 | vs->offset = off; |
4481 | 183 | vs->size = sizeof(int); |
4482 | 183 | } |
4483 | 52 | sec->size = off; |
4484 | 52 | } |
4485 | | |
4486 | 125 | if (kcfg_sec) { |
4487 | 73 | sec = kcfg_sec; |
4488 | | /* for kcfg externs calculate their offsets within a .kconfig map */ |
4489 | 73 | off = 0; |
4490 | 257 | for (i = 0; i < obj->nr_extern; i++) { |
4491 | 184 | ext = &obj->externs[i]; |
4492 | 184 | if (ext->type != EXT_KCFG) |
4493 | 0 | continue; |
4494 | | |
4495 | 184 | ext->kcfg.data_off = roundup(off, ext->kcfg.align); |
4496 | 184 | off = ext->kcfg.data_off + ext->kcfg.sz; |
4497 | 184 | pr_debug("extern (kcfg) #%d: symbol %d, off %d, name %s\n", |
4498 | 184 | i, ext->sym_idx, ext->kcfg.data_off, ext->name); |
4499 | 184 | } |
4500 | 73 | sec->size = off; |
4501 | 73 | n = btf_vlen(sec); |
4502 | 149 | for (i = 0; i < n; i++) { |
4503 | 100 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4504 | | |
4505 | 100 | t = btf__type_by_id(obj->btf, vs->type); |
4506 | 100 | ext_name = btf__name_by_offset(obj->btf, t->name_off); |
4507 | 100 | ext = find_extern_by_name(obj, ext_name); |
4508 | 100 | if (!ext) { |
4509 | 24 | pr_warn("failed to find extern definition for BTF var '%s'\n", |
4510 | 24 | ext_name); |
4511 | 24 | return -ESRCH; |
4512 | 24 | } |
4513 | 76 | btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4514 | 76 | vs->offset = ext->kcfg.data_off; |
4515 | 76 | } |
4516 | 73 | } |
4517 | 101 | return 0; |
4518 | 125 | } |
4519 | | |
4520 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog) |
4521 | 9.72k | { |
4522 | 9.72k | return prog->sec_idx == obj->efile.text_shndx; |
4523 | 9.72k | } |
4524 | | |
4525 | | struct bpf_program * |
4526 | | bpf_object__find_program_by_name(const struct bpf_object *obj, |
4527 | | const char *name) |
4528 | 0 | { |
4529 | 0 | struct bpf_program *prog; |
4530 | |
|
4531 | 0 | bpf_object__for_each_program(prog, obj) { |
4532 | 0 | if (prog_is_subprog(obj, prog)) |
4533 | 0 | continue; |
4534 | 0 | if (!strcmp(prog->name, name)) |
4535 | 0 | return prog; |
4536 | 0 | } |
4537 | 0 | return errno = ENOENT, NULL; |
4538 | 0 | } |
4539 | | |
4540 | | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
4541 | | int shndx) |
4542 | 437 | { |
4543 | 437 | switch (obj->efile.secs[shndx].sec_type) { |
4544 | 105 | case SEC_BSS: |
4545 | 336 | case SEC_DATA: |
4546 | 434 | case SEC_RODATA: |
4547 | 434 | return true; |
4548 | 3 | default: |
4549 | 3 | return false; |
4550 | 437 | } |
4551 | 437 | } |
4552 | | |
4553 | | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
4554 | | int shndx) |
4555 | 30 | { |
4556 | 30 | return shndx == obj->efile.btf_maps_shndx; |
4557 | 30 | } |
4558 | | |
4559 | | static enum libbpf_map_type |
4560 | | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
4561 | 480 | { |
4562 | 480 | if (shndx == obj->efile.symbols_shndx) |
4563 | 3 | return LIBBPF_MAP_KCONFIG; |
4564 | | |
4565 | 477 | switch (obj->efile.secs[shndx].sec_type) { |
4566 | 105 | case SEC_BSS: |
4567 | 105 | return LIBBPF_MAP_BSS; |
4568 | 231 | case SEC_DATA: |
4569 | 231 | return LIBBPF_MAP_DATA; |
4570 | 98 | case SEC_RODATA: |
4571 | 98 | return LIBBPF_MAP_RODATA; |
4572 | 43 | default: |
4573 | 43 | return LIBBPF_MAP_UNSPEC; |
4574 | 477 | } |
4575 | 477 | } |
4576 | | |
4577 | | static int bpf_prog_compute_hash(struct bpf_program *prog) |
4578 | 0 | { |
4579 | 0 | struct bpf_insn *purged; |
4580 | 0 | int i, err = 0; |
4581 | |
|
4582 | 0 | purged = calloc(prog->insns_cnt, BPF_INSN_SZ); |
4583 | 0 | if (!purged) |
4584 | 0 | return -ENOMEM; |
4585 | | |
4586 | | /* If relocations have been done, the map_fd needs to be |
4587 | | * discarded for the digest calculation. |
4588 | | */ |
4589 | 0 | for (i = 0; i < prog->insns_cnt; i++) { |
4590 | 0 | purged[i] = prog->insns[i]; |
4591 | 0 | if (purged[i].code == (BPF_LD | BPF_IMM | BPF_DW) && |
4592 | 0 | (purged[i].src_reg == BPF_PSEUDO_MAP_FD || |
4593 | 0 | purged[i].src_reg == BPF_PSEUDO_MAP_VALUE)) { |
4594 | 0 | purged[i].imm = 0; |
4595 | 0 | i++; |
4596 | 0 | if (i >= prog->insns_cnt || |
4597 | 0 | prog->insns[i].code != 0 || |
4598 | 0 | prog->insns[i].dst_reg != 0 || |
4599 | 0 | prog->insns[i].src_reg != 0 || |
4600 | 0 | prog->insns[i].off != 0) { |
4601 | 0 | err = -EINVAL; |
4602 | 0 | goto out; |
4603 | 0 | } |
4604 | 0 | purged[i] = prog->insns[i]; |
4605 | 0 | purged[i].imm = 0; |
4606 | 0 | } |
4607 | 0 | } |
4608 | 0 | libbpf_sha256(purged, prog->insns_cnt * sizeof(struct bpf_insn), |
4609 | 0 | prog->hash); |
4610 | 0 | out: |
4611 | 0 | free(purged); |
4612 | 0 | return err; |
4613 | 0 | } |
4614 | | |
4615 | | static int bpf_program__record_reloc(struct bpf_program *prog, |
4616 | | struct reloc_desc *reloc_desc, |
4617 | | __u32 insn_idx, const char *sym_name, |
4618 | | const Elf64_Sym *sym, const Elf64_Rel *rel) |
4619 | 2.06k | { |
4620 | 2.06k | struct bpf_insn *insn = &prog->insns[insn_idx]; |
4621 | 2.06k | size_t map_idx, nr_maps = prog->obj->nr_maps; |
4622 | 2.06k | struct bpf_object *obj = prog->obj; |
4623 | 2.06k | __u32 shdr_idx = sym->st_shndx; |
4624 | 2.06k | enum libbpf_map_type type; |
4625 | 2.06k | const char *sym_sec_name; |
4626 | 2.06k | struct bpf_map *map; |
4627 | | |
4628 | 2.06k | if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) { |
4629 | 21 | pr_warn("prog '%s': invalid relo against '%s' for insns[%u].code 0x%x\n", |
4630 | 21 | prog->name, sym_name, insn_idx, insn->code); |
4631 | 21 | return -LIBBPF_ERRNO__RELOC; |
4632 | 21 | } |
4633 | | |
4634 | 2.04k | if (sym_is_extern(sym)) { |
4635 | 1 | int sym_idx = ELF64_R_SYM(rel->r_info); |
4636 | 1 | int i, n = obj->nr_extern; |
4637 | 1 | struct extern_desc *ext; |
4638 | | |
4639 | 1 | for (i = 0; i < n; i++) { |
4640 | 0 | ext = &obj->externs[i]; |
4641 | 0 | if (ext->sym_idx == sym_idx) |
4642 | 0 | break; |
4643 | 0 | } |
4644 | 1 | if (i >= n) { |
4645 | 1 | pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n", |
4646 | 1 | prog->name, sym_name, sym_idx); |
4647 | 1 | return -LIBBPF_ERRNO__RELOC; |
4648 | 1 | } |
4649 | 0 | pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n", |
4650 | 0 | prog->name, i, ext->name, ext->sym_idx, insn_idx); |
4651 | 0 | if (insn->code == (BPF_JMP | BPF_CALL)) |
4652 | 0 | reloc_desc->type = RELO_EXTERN_CALL; |
4653 | 0 | else |
4654 | 0 | reloc_desc->type = RELO_EXTERN_LD64; |
4655 | 0 | reloc_desc->insn_idx = insn_idx; |
4656 | 0 | reloc_desc->ext_idx = i; |
4657 | 0 | return 0; |
4658 | 1 | } |
4659 | | |
4660 | | /* sub-program call relocation */ |
4661 | 2.03k | if (is_call_insn(insn)) { |
4662 | 656 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
4663 | 4 | pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); |
4664 | 4 | return -LIBBPF_ERRNO__RELOC; |
4665 | 4 | } |
4666 | | /* text_shndx can be 0, if no default "main" program exists */ |
4667 | 652 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
4668 | 16 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4669 | 16 | pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n", |
4670 | 16 | prog->name, sym_name, sym_sec_name); |
4671 | 16 | return -LIBBPF_ERRNO__RELOC; |
4672 | 16 | } |
4673 | 636 | if (sym->st_value % BPF_INSN_SZ) { |
4674 | 1 | pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n", |
4675 | 1 | prog->name, sym_name, (size_t)sym->st_value); |
4676 | 1 | return -LIBBPF_ERRNO__RELOC; |
4677 | 1 | } |
4678 | 635 | reloc_desc->type = RELO_CALL; |
4679 | 635 | reloc_desc->insn_idx = insn_idx; |
4680 | 635 | reloc_desc->sym_off = sym->st_value; |
4681 | 635 | return 0; |
4682 | 636 | } |
4683 | | |
4684 | 1.38k | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
4685 | 2 | pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n", |
4686 | 2 | prog->name, sym_name, shdr_idx); |
4687 | 2 | return -LIBBPF_ERRNO__RELOC; |
4688 | 2 | } |
4689 | | |
4690 | | /* loading subprog addresses */ |
4691 | 1.38k | if (sym_is_subprog(sym, obj->efile.text_shndx)) { |
4692 | | /* global_func: sym->st_value = offset in the section, insn->imm = 0. |
4693 | | * local_func: sym->st_value = 0, insn->imm = offset in the section. |
4694 | | */ |
4695 | 901 | if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { |
4696 | 2 | pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n", |
4697 | 2 | prog->name, sym_name, (size_t)sym->st_value, insn->imm); |
4698 | 2 | return -LIBBPF_ERRNO__RELOC; |
4699 | 2 | } |
4700 | | |
4701 | 899 | reloc_desc->type = RELO_SUBPROG_ADDR; |
4702 | 899 | reloc_desc->insn_idx = insn_idx; |
4703 | 899 | reloc_desc->sym_off = sym->st_value; |
4704 | 899 | return 0; |
4705 | 901 | } |
4706 | | |
4707 | 480 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
4708 | 480 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4709 | | |
4710 | | /* arena data relocation */ |
4711 | 480 | if (shdr_idx == obj->efile.arena_data_shndx) { |
4712 | 1 | if (obj->arena_map_idx < 0) { |
4713 | 1 | pr_warn("prog '%s': bad arena data relocation at insn %u, no arena maps defined\n", |
4714 | 1 | prog->name, insn_idx); |
4715 | 1 | return -LIBBPF_ERRNO__RELOC; |
4716 | 1 | } |
4717 | 0 | reloc_desc->type = RELO_DATA; |
4718 | 0 | reloc_desc->insn_idx = insn_idx; |
4719 | 0 | reloc_desc->map_idx = obj->arena_map_idx; |
4720 | 0 | reloc_desc->sym_off = sym->st_value; |
4721 | |
|
4722 | 0 | map = &obj->maps[obj->arena_map_idx]; |
4723 | 0 | pr_debug("prog '%s': found arena map %d (%s, sec %d, off %zu) for insn %u\n", |
4724 | 0 | prog->name, obj->arena_map_idx, map->name, map->sec_idx, |
4725 | 0 | map->sec_offset, insn_idx); |
4726 | 0 | return 0; |
4727 | 1 | } |
4728 | | |
4729 | | /* jump table data relocation */ |
4730 | 479 | if (shdr_idx == obj->efile.jumptables_data_shndx) { |
4731 | 12 | reloc_desc->type = RELO_INSN_ARRAY; |
4732 | 12 | reloc_desc->insn_idx = insn_idx; |
4733 | 12 | reloc_desc->map_idx = -1; |
4734 | 12 | reloc_desc->sym_off = sym->st_value; |
4735 | 12 | reloc_desc->sym_size = sym->st_size; |
4736 | 12 | return 0; |
4737 | 12 | } |
4738 | | |
4739 | | /* generic map reference relocation */ |
4740 | 467 | if (type == LIBBPF_MAP_UNSPEC) { |
4741 | 30 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
4742 | 30 | pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n", |
4743 | 30 | prog->name, sym_name, sym_sec_name); |
4744 | 30 | return -LIBBPF_ERRNO__RELOC; |
4745 | 30 | } |
4746 | 0 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4747 | 0 | map = &obj->maps[map_idx]; |
4748 | 0 | if (map->libbpf_type != type || |
4749 | 0 | map->sec_idx != sym->st_shndx || |
4750 | 0 | map->sec_offset != sym->st_value) |
4751 | 0 | continue; |
4752 | 0 | pr_debug("prog '%s': found map %zu (%s, sec %d, off %zu) for insn #%u\n", |
4753 | 0 | prog->name, map_idx, map->name, map->sec_idx, |
4754 | 0 | map->sec_offset, insn_idx); |
4755 | 0 | break; |
4756 | 0 | } |
4757 | 0 | if (map_idx >= nr_maps) { |
4758 | 0 | pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n", |
4759 | 0 | prog->name, sym_sec_name, (size_t)sym->st_value); |
4760 | 0 | return -LIBBPF_ERRNO__RELOC; |
4761 | 0 | } |
4762 | 0 | reloc_desc->type = RELO_LD64; |
4763 | 0 | reloc_desc->insn_idx = insn_idx; |
4764 | 0 | reloc_desc->map_idx = map_idx; |
4765 | 0 | reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ |
4766 | 0 | return 0; |
4767 | 0 | } |
4768 | | |
4769 | | /* global data map relocation */ |
4770 | 437 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
4771 | 3 | pr_warn("prog '%s': bad data relo against section '%s'\n", |
4772 | 3 | prog->name, sym_sec_name); |
4773 | 3 | return -LIBBPF_ERRNO__RELOC; |
4774 | 3 | } |
4775 | 714 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4776 | 711 | map = &obj->maps[map_idx]; |
4777 | 711 | if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) |
4778 | 280 | continue; |
4779 | 431 | pr_debug("prog '%s': found data map %zu (%s, sec %d, off %zu) for insn %u\n", |
4780 | 431 | prog->name, map_idx, map->name, map->sec_idx, |
4781 | 431 | map->sec_offset, insn_idx); |
4782 | 431 | break; |
4783 | 711 | } |
4784 | 434 | if (map_idx >= nr_maps) { |
4785 | 3 | pr_warn("prog '%s': data relo failed to find map for section '%s'\n", |
4786 | 3 | prog->name, sym_sec_name); |
4787 | 3 | return -LIBBPF_ERRNO__RELOC; |
4788 | 3 | } |
4789 | | |
4790 | 431 | reloc_desc->type = RELO_DATA; |
4791 | 431 | reloc_desc->insn_idx = insn_idx; |
4792 | 431 | reloc_desc->map_idx = map_idx; |
4793 | 431 | reloc_desc->sym_off = sym->st_value; |
4794 | 431 | return 0; |
4795 | 434 | } |
4796 | | |
4797 | | static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx) |
4798 | 2.96k | { |
4799 | 2.96k | return insn_idx >= prog->sec_insn_off && |
4800 | 2.15k | insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; |
4801 | 2.96k | } |
4802 | | |
4803 | | static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj, |
4804 | | size_t sec_idx, size_t insn_idx) |
4805 | 9.95k | { |
4806 | 9.95k | int l = 0, r = obj->nr_programs - 1, m; |
4807 | 9.95k | struct bpf_program *prog; |
4808 | | |
4809 | 9.95k | if (!obj->nr_programs) |
4810 | 6.41k | return NULL; |
4811 | | |
4812 | 5.28k | while (l < r) { |
4813 | 1.74k | m = l + (r - l + 1) / 2; |
4814 | 1.74k | prog = &obj->programs[m]; |
4815 | | |
4816 | 1.74k | if (prog->sec_idx < sec_idx || |
4817 | 1.45k | (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) |
4818 | 981 | l = m; |
4819 | 763 | else |
4820 | 763 | r = m - 1; |
4821 | 1.74k | } |
4822 | | /* matching program could be at index l, but it still might be the |
4823 | | * wrong one, so we need to double check conditions for the last time |
4824 | | */ |
4825 | 3.54k | prog = &obj->programs[l]; |
4826 | 3.54k | if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) |
4827 | 2.06k | return prog; |
4828 | 1.47k | return NULL; |
4829 | 3.54k | } |
4830 | | |
4831 | | static int |
4832 | | bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data) |
4833 | 1.02k | { |
4834 | 1.02k | const char *relo_sec_name, *sec_name; |
4835 | 1.02k | size_t sec_idx = shdr->sh_info, sym_idx; |
4836 | 1.02k | struct bpf_program *prog; |
4837 | 1.02k | struct reloc_desc *relos; |
4838 | 1.02k | int err, i, nrels; |
4839 | 1.02k | const char *sym_name; |
4840 | 1.02k | __u32 insn_idx; |
4841 | 1.02k | Elf_Scn *scn; |
4842 | 1.02k | Elf_Data *scn_data; |
4843 | 1.02k | Elf64_Sym *sym; |
4844 | 1.02k | Elf64_Rel *rel; |
4845 | | |
4846 | 1.02k | if (sec_idx >= obj->efile.sec_cnt) |
4847 | 0 | return -EINVAL; |
4848 | | |
4849 | 1.02k | scn = elf_sec_by_idx(obj, sec_idx); |
4850 | 1.02k | scn_data = elf_sec_data(obj, scn); |
4851 | 1.02k | if (!scn_data) |
4852 | 11 | return -LIBBPF_ERRNO__FORMAT; |
4853 | | |
4854 | 1.01k | relo_sec_name = elf_sec_str(obj, shdr->sh_name); |
4855 | 1.01k | sec_name = elf_sec_name(obj, scn); |
4856 | 1.01k | if (!relo_sec_name || !sec_name) |
4857 | 17 | return -EINVAL; |
4858 | | |
4859 | 1.00k | pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n", |
4860 | 1.00k | relo_sec_name, sec_idx, sec_name); |
4861 | 1.00k | nrels = shdr->sh_size / shdr->sh_entsize; |
4862 | | |
4863 | 10.8k | for (i = 0; i < nrels; i++) { |
4864 | 10.3k | rel = elf_rel_by_idx(data, i); |
4865 | 10.3k | if (!rel) { |
4866 | 0 | pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i); |
4867 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4868 | 0 | } |
4869 | | |
4870 | 10.3k | sym_idx = ELF64_R_SYM(rel->r_info); |
4871 | 10.3k | sym = elf_sym_by_idx(obj, sym_idx); |
4872 | 10.3k | if (!sym) { |
4873 | 156 | pr_warn("sec '%s': symbol #%zu not found for relo #%d\n", |
4874 | 156 | relo_sec_name, sym_idx, i); |
4875 | 156 | return -LIBBPF_ERRNO__FORMAT; |
4876 | 156 | } |
4877 | | |
4878 | 10.1k | if (sym->st_shndx >= obj->efile.sec_cnt) { |
4879 | 16 | pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n", |
4880 | 16 | relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); |
4881 | 16 | return -LIBBPF_ERRNO__FORMAT; |
4882 | 16 | } |
4883 | | |
4884 | 10.1k | if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { |
4885 | 205 | pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n", |
4886 | 205 | relo_sec_name, (size_t)rel->r_offset, i); |
4887 | 205 | return -LIBBPF_ERRNO__FORMAT; |
4888 | 205 | } |
4889 | | |
4890 | 9.95k | insn_idx = rel->r_offset / BPF_INSN_SZ; |
4891 | | /* relocations against static functions are recorded as |
4892 | | * relocations against the section that contains a function; |
4893 | | * in such case, symbol will be STT_SECTION and sym.st_name |
4894 | | * will point to empty string (0), so fetch section name |
4895 | | * instead |
4896 | | */ |
4897 | 9.95k | if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) |
4898 | 431 | sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); |
4899 | 9.52k | else |
4900 | 9.52k | sym_name = elf_sym_str(obj, sym->st_name); |
4901 | 9.95k | sym_name = sym_name ?: "<?"; |
4902 | | |
4903 | 9.95k | pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n", |
4904 | 9.95k | relo_sec_name, i, insn_idx, sym_name); |
4905 | | |
4906 | 9.95k | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
4907 | 9.95k | if (!prog) { |
4908 | 7.89k | pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n", |
4909 | 7.89k | relo_sec_name, i, sec_name, insn_idx); |
4910 | 7.89k | continue; |
4911 | 7.89k | } |
4912 | | |
4913 | 2.06k | relos = libbpf_reallocarray(prog->reloc_desc, |
4914 | 2.06k | prog->nr_reloc + 1, sizeof(*relos)); |
4915 | 2.06k | if (!relos) |
4916 | 0 | return -ENOMEM; |
4917 | 2.06k | prog->reloc_desc = relos; |
4918 | | |
4919 | | /* adjust insn_idx to local BPF program frame of reference */ |
4920 | 2.06k | insn_idx -= prog->sec_insn_off; |
4921 | 2.06k | err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], |
4922 | 2.06k | insn_idx, sym_name, sym, rel); |
4923 | 2.06k | if (err) |
4924 | 84 | return err; |
4925 | | |
4926 | 1.97k | prog->nr_reloc++; |
4927 | 1.97k | } |
4928 | 539 | return 0; |
4929 | 1.00k | } |
4930 | | |
4931 | | static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map) |
4932 | 1.97k | { |
4933 | 1.97k | int id; |
4934 | | |
4935 | 1.97k | if (!obj->btf) |
4936 | 1.38k | return -ENOENT; |
4937 | | |
4938 | | /* if it's BTF-defined map, we don't need to search for type IDs. |
4939 | | * For struct_ops map, it does not need btf_key_type_id and |
4940 | | * btf_value_type_id. |
4941 | | */ |
4942 | 587 | if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) |
4943 | 0 | return 0; |
4944 | | |
4945 | | /* |
4946 | | * LLVM annotates global data differently in BTF, that is, |
4947 | | * only as '.data', '.bss' or '.rodata'. |
4948 | | */ |
4949 | 587 | if (!bpf_map__is_internal(map)) |
4950 | 0 | return -ENOENT; |
4951 | | |
4952 | 587 | id = btf__find_by_name(obj->btf, map->real_name); |
4953 | 587 | if (id < 0) |
4954 | 458 | return id; |
4955 | | |
4956 | 129 | map->btf_key_type_id = 0; |
4957 | 129 | map->btf_value_type_id = id; |
4958 | 129 | return 0; |
4959 | 587 | } |
4960 | | |
4961 | | static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) |
4962 | 0 | { |
4963 | 0 | char file[PATH_MAX], buff[4096]; |
4964 | 0 | FILE *fp; |
4965 | 0 | __u32 val; |
4966 | 0 | int err; |
4967 | |
|
4968 | 0 | snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd); |
4969 | 0 | memset(info, 0, sizeof(*info)); |
4970 | |
|
4971 | 0 | fp = fopen(file, "re"); |
4972 | 0 | if (!fp) { |
4973 | 0 | err = -errno; |
4974 | 0 | pr_warn("failed to open %s: %s. No procfs support?\n", file, |
4975 | 0 | errstr(err)); |
4976 | 0 | return err; |
4977 | 0 | } |
4978 | | |
4979 | 0 | while (fgets(buff, sizeof(buff), fp)) { |
4980 | 0 | if (sscanf(buff, "map_type:\t%u", &val) == 1) |
4981 | 0 | info->type = val; |
4982 | 0 | else if (sscanf(buff, "key_size:\t%u", &val) == 1) |
4983 | 0 | info->key_size = val; |
4984 | 0 | else if (sscanf(buff, "value_size:\t%u", &val) == 1) |
4985 | 0 | info->value_size = val; |
4986 | 0 | else if (sscanf(buff, "max_entries:\t%u", &val) == 1) |
4987 | 0 | info->max_entries = val; |
4988 | 0 | else if (sscanf(buff, "map_flags:\t%x", &val) == 1) |
4989 | 0 | info->map_flags = val; |
4990 | 0 | } |
4991 | |
|
4992 | 0 | fclose(fp); |
4993 | |
|
4994 | 0 | return 0; |
4995 | 0 | } |
4996 | | |
4997 | | static bool map_is_created(const struct bpf_map *map) |
4998 | 0 | { |
4999 | 0 | return map->obj->state >= OBJ_PREPARED || map->reused; |
5000 | 0 | } |
5001 | | |
5002 | | bool bpf_map__autocreate(const struct bpf_map *map) |
5003 | 0 | { |
5004 | 0 | return map->autocreate; |
5005 | 0 | } |
5006 | | |
5007 | | int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) |
5008 | 0 | { |
5009 | 0 | if (map_is_created(map)) |
5010 | 0 | return libbpf_err(-EBUSY); |
5011 | | |
5012 | 0 | map->autocreate = autocreate; |
5013 | 0 | return 0; |
5014 | 0 | } |
5015 | | |
5016 | | int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach) |
5017 | 0 | { |
5018 | 0 | if (!bpf_map__is_struct_ops(map)) |
5019 | 0 | return libbpf_err(-EINVAL); |
5020 | | |
5021 | 0 | map->autoattach = autoattach; |
5022 | 0 | return 0; |
5023 | 0 | } |
5024 | | |
5025 | | bool bpf_map__autoattach(const struct bpf_map *map) |
5026 | 0 | { |
5027 | 0 | return map->autoattach; |
5028 | 0 | } |
5029 | | |
5030 | | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
5031 | 0 | { |
5032 | 0 | struct bpf_map_info info; |
5033 | 0 | __u32 len = sizeof(info), name_len; |
5034 | 0 | int new_fd, err; |
5035 | 0 | char *new_name; |
5036 | |
|
5037 | 0 | memset(&info, 0, len); |
5038 | 0 | err = bpf_map_get_info_by_fd(fd, &info, &len); |
5039 | 0 | if (err && errno == EINVAL) |
5040 | 0 | err = bpf_get_map_info_from_fdinfo(fd, &info); |
5041 | 0 | if (err) |
5042 | 0 | return libbpf_err(err); |
5043 | | |
5044 | 0 | name_len = strlen(info.name); |
5045 | 0 | if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) |
5046 | 0 | new_name = strdup(map->name); |
5047 | 0 | else |
5048 | 0 | new_name = strdup(info.name); |
5049 | |
|
5050 | 0 | if (!new_name) |
5051 | 0 | return libbpf_err(-errno); |
5052 | | |
5053 | | /* |
5054 | | * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. |
5055 | | * This is similar to what we do in ensure_good_fd(), but without |
5056 | | * closing original FD. |
5057 | | */ |
5058 | 0 | new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
5059 | 0 | if (new_fd < 0) { |
5060 | 0 | err = -errno; |
5061 | 0 | goto err_free_new_name; |
5062 | 0 | } |
5063 | | |
5064 | 0 | err = reuse_fd(map->fd, new_fd); |
5065 | 0 | if (err) |
5066 | 0 | goto err_free_new_name; |
5067 | | |
5068 | 0 | free(map->name); |
5069 | |
|
5070 | 0 | map->name = new_name; |
5071 | 0 | map->def.type = info.type; |
5072 | 0 | map->def.key_size = info.key_size; |
5073 | 0 | map->def.value_size = info.value_size; |
5074 | 0 | map->def.max_entries = info.max_entries; |
5075 | 0 | map->def.map_flags = info.map_flags; |
5076 | 0 | map->btf_key_type_id = info.btf_key_type_id; |
5077 | 0 | map->btf_value_type_id = info.btf_value_type_id; |
5078 | 0 | map->reused = true; |
5079 | 0 | map->map_extra = info.map_extra; |
5080 | |
|
5081 | 0 | return 0; |
5082 | | |
5083 | 0 | err_free_new_name: |
5084 | 0 | free(new_name); |
5085 | 0 | return libbpf_err(err); |
5086 | 0 | } |
5087 | | |
5088 | | __u32 bpf_map__max_entries(const struct bpf_map *map) |
5089 | 0 | { |
5090 | 0 | return map->def.max_entries; |
5091 | 0 | } |
5092 | | |
5093 | | struct bpf_map *bpf_map__inner_map(struct bpf_map *map) |
5094 | 0 | { |
5095 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) |
5096 | 0 | return errno = EINVAL, NULL; |
5097 | | |
5098 | 0 | return map->inner_map; |
5099 | 0 | } |
5100 | | |
5101 | | int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries) |
5102 | 0 | { |
5103 | 0 | if (map_is_created(map)) |
5104 | 0 | return libbpf_err(-EBUSY); |
5105 | | |
5106 | 0 | map->def.max_entries = max_entries; |
5107 | | |
5108 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
5109 | 0 | if (map_is_ringbuf(map)) |
5110 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
5111 | |
|
5112 | 0 | return 0; |
5113 | 0 | } |
5114 | | |
5115 | | static int bpf_object_prepare_token(struct bpf_object *obj) |
5116 | 0 | { |
5117 | 0 | const char *bpffs_path; |
5118 | 0 | int bpffs_fd = -1, token_fd, err; |
5119 | 0 | bool mandatory; |
5120 | 0 | enum libbpf_print_level level; |
5121 | | |
5122 | | /* token is explicitly prevented */ |
5123 | 0 | if (obj->token_path && obj->token_path[0] == '\0') { |
5124 | 0 | pr_debug("object '%s': token is prevented, skipping...\n", obj->name); |
5125 | 0 | return 0; |
5126 | 0 | } |
5127 | | |
5128 | 0 | mandatory = obj->token_path != NULL; |
5129 | 0 | level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG; |
5130 | |
|
5131 | 0 | bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH; |
5132 | 0 | bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR); |
5133 | 0 | if (bpffs_fd < 0) { |
5134 | 0 | err = -errno; |
5135 | 0 | __pr(level, "object '%s': failed (%s) to open BPF FS mount at '%s'%s\n", |
5136 | 0 | obj->name, errstr(err), bpffs_path, |
5137 | 0 | mandatory ? "" : ", skipping optional step..."); |
5138 | 0 | return mandatory ? err : 0; |
5139 | 0 | } |
5140 | | |
5141 | 0 | token_fd = bpf_token_create(bpffs_fd, 0); |
5142 | 0 | close(bpffs_fd); |
5143 | 0 | if (token_fd < 0) { |
5144 | 0 | if (!mandatory && token_fd == -ENOENT) { |
5145 | 0 | pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n", |
5146 | 0 | obj->name, bpffs_path); |
5147 | 0 | return 0; |
5148 | 0 | } |
5149 | 0 | __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n", |
5150 | 0 | obj->name, token_fd, bpffs_path, |
5151 | 0 | mandatory ? "" : ", skipping optional step..."); |
5152 | 0 | return mandatory ? token_fd : 0; |
5153 | 0 | } |
5154 | | |
5155 | 0 | obj->feat_cache = calloc(1, sizeof(*obj->feat_cache)); |
5156 | 0 | if (!obj->feat_cache) { |
5157 | 0 | close(token_fd); |
5158 | 0 | return -ENOMEM; |
5159 | 0 | } |
5160 | | |
5161 | 0 | obj->token_fd = token_fd; |
5162 | 0 | obj->feat_cache->token_fd = token_fd; |
5163 | |
|
5164 | 0 | return 0; |
5165 | 0 | } |
5166 | | |
5167 | | static int |
5168 | | bpf_object__probe_loading(struct bpf_object *obj) |
5169 | 0 | { |
5170 | 0 | struct bpf_insn insns[] = { |
5171 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
5172 | 0 | BPF_EXIT_INSN(), |
5173 | 0 | }; |
5174 | 0 | int ret, insn_cnt = ARRAY_SIZE(insns); |
5175 | |
|
5176 | 0 | if (obj->gen_loader || obj->token_fd) |
5177 | 0 | return 0; |
5178 | | |
5179 | 0 | ret = bump_rlimit_memlock(); |
5180 | 0 | if (ret) |
5181 | 0 | pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %s), you might need to do it explicitly!\n", |
5182 | 0 | errstr(ret)); |
5183 | | |
5184 | | /* make sure basic loading works */ |
5185 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL); |
5186 | 0 | if (ret < 0) |
5187 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL); |
5188 | 0 | if (ret < 0) { |
5189 | 0 | ret = errno; |
5190 | 0 | pr_warn("Error in %s(): %s. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value.\n", |
5191 | 0 | __func__, errstr(ret)); |
5192 | 0 | return -ret; |
5193 | 0 | } |
5194 | 0 | close(ret); |
5195 | |
|
5196 | 0 | return 0; |
5197 | 0 | } |
5198 | | |
5199 | | bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) |
5200 | 0 | { |
5201 | 0 | if (obj->gen_loader) |
5202 | | /* To generate loader program assume the latest kernel |
5203 | | * to avoid doing extra prog_load, map_create syscalls. |
5204 | | */ |
5205 | 0 | return true; |
5206 | | |
5207 | 0 | if (obj->feat_cache) |
5208 | 0 | return feat_supported(obj->feat_cache, feat_id); |
5209 | | |
5210 | 0 | return feat_supported(NULL, feat_id); |
5211 | 0 | } |
5212 | | |
5213 | | /* Used in testing to simulate missing features. */ |
5214 | | void bpf_object_set_feat_cache(struct bpf_object *obj, struct kern_feature_cache *cache) |
5215 | 0 | { |
5216 | 0 | if (obj->feat_cache) |
5217 | 0 | free(obj->feat_cache); |
5218 | 0 | obj->feat_cache = cache; |
5219 | 0 | } |
5220 | | |
5221 | | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
5222 | 0 | { |
5223 | 0 | struct bpf_map_info map_info; |
5224 | 0 | __u32 map_info_len = sizeof(map_info); |
5225 | 0 | int err; |
5226 | |
|
5227 | 0 | memset(&map_info, 0, map_info_len); |
5228 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len); |
5229 | 0 | if (err && errno == EINVAL) |
5230 | 0 | err = bpf_get_map_info_from_fdinfo(map_fd, &map_info); |
5231 | 0 | if (err) { |
5232 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", map_fd, |
5233 | 0 | errstr(err)); |
5234 | 0 | return false; |
5235 | 0 | } |
5236 | | |
5237 | | /* |
5238 | | * bpf_get_map_info_by_fd() for DEVMAP will always return flags with |
5239 | | * BPF_F_RDONLY_PROG set, but it generally is not set at map creation time. |
5240 | | * Thus, ignore the BPF_F_RDONLY_PROG flag in the flags returned from |
5241 | | * bpf_get_map_info_by_fd() when checking for compatibility with an |
5242 | | * existing DEVMAP. |
5243 | | */ |
5244 | 0 | if (map->def.type == BPF_MAP_TYPE_DEVMAP || map->def.type == BPF_MAP_TYPE_DEVMAP_HASH) |
5245 | 0 | map_info.map_flags &= ~BPF_F_RDONLY_PROG; |
5246 | |
|
5247 | 0 | return (map_info.type == map->def.type && |
5248 | 0 | map_info.key_size == map->def.key_size && |
5249 | 0 | map_info.value_size == map->def.value_size && |
5250 | 0 | map_info.max_entries == map->def.max_entries && |
5251 | 0 | map_info.map_flags == map->def.map_flags && |
5252 | 0 | map_info.map_extra == map->map_extra); |
5253 | 0 | } |
5254 | | |
5255 | | static int |
5256 | | bpf_object__reuse_map(struct bpf_map *map) |
5257 | 0 | { |
5258 | 0 | int err, pin_fd; |
5259 | |
|
5260 | 0 | pin_fd = bpf_obj_get(map->pin_path); |
5261 | 0 | if (pin_fd < 0) { |
5262 | 0 | err = -errno; |
5263 | 0 | if (err == -ENOENT) { |
5264 | 0 | pr_debug("found no pinned map to reuse at '%s'\n", |
5265 | 0 | map->pin_path); |
5266 | 0 | return 0; |
5267 | 0 | } |
5268 | | |
5269 | 0 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
5270 | 0 | map->pin_path, errstr(err)); |
5271 | 0 | return err; |
5272 | 0 | } |
5273 | | |
5274 | 0 | if (!map_is_reuse_compat(map, pin_fd)) { |
5275 | 0 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
5276 | 0 | map->pin_path); |
5277 | 0 | close(pin_fd); |
5278 | 0 | return -EINVAL; |
5279 | 0 | } |
5280 | | |
5281 | 0 | err = bpf_map__reuse_fd(map, pin_fd); |
5282 | 0 | close(pin_fd); |
5283 | 0 | if (err) |
5284 | 0 | return err; |
5285 | | |
5286 | 0 | map->pinned = true; |
5287 | 0 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
5288 | |
|
5289 | 0 | return 0; |
5290 | 0 | } |
5291 | | |
5292 | | static int |
5293 | | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
5294 | 0 | { |
5295 | 0 | enum libbpf_map_type map_type = map->libbpf_type; |
5296 | 0 | int err, zero = 0; |
5297 | 0 | size_t mmap_sz; |
5298 | |
|
5299 | 0 | if (obj->gen_loader) { |
5300 | 0 | bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, |
5301 | 0 | map->mmaped, map->def.value_size); |
5302 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) |
5303 | 0 | bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); |
5304 | 0 | return 0; |
5305 | 0 | } |
5306 | | |
5307 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); |
5308 | 0 | if (err) { |
5309 | 0 | err = -errno; |
5310 | 0 | pr_warn("map '%s': failed to set initial contents: %s\n", |
5311 | 0 | bpf_map__name(map), errstr(err)); |
5312 | 0 | return err; |
5313 | 0 | } |
5314 | | |
5315 | | /* Freeze .rodata and .kconfig map as read-only from syscall side. */ |
5316 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) { |
5317 | 0 | err = bpf_map_freeze(map->fd); |
5318 | 0 | if (err) { |
5319 | 0 | err = -errno; |
5320 | 0 | pr_warn("map '%s': failed to freeze as read-only: %s\n", |
5321 | 0 | bpf_map__name(map), errstr(err)); |
5322 | 0 | return err; |
5323 | 0 | } |
5324 | 0 | } |
5325 | | |
5326 | | /* Remap anonymous mmap()-ed "map initialization image" as |
5327 | | * a BPF map-backed mmap()-ed memory, but preserving the same |
5328 | | * memory address. This will cause kernel to change process' |
5329 | | * page table to point to a different piece of kernel memory, |
5330 | | * but from userspace point of view memory address (and its |
5331 | | * contents, being identical at this point) will stay the |
5332 | | * same. This mapping will be released by bpf_object__close() |
5333 | | * as per normal clean up procedure. |
5334 | | */ |
5335 | 0 | mmap_sz = bpf_map_mmap_sz(map); |
5336 | 0 | if (map->def.map_flags & BPF_F_MMAPABLE) { |
5337 | 0 | void *mmaped; |
5338 | 0 | int prot; |
5339 | |
|
5340 | 0 | if (map->def.map_flags & BPF_F_RDONLY_PROG) |
5341 | 0 | prot = PROT_READ; |
5342 | 0 | else |
5343 | 0 | prot = PROT_READ | PROT_WRITE; |
5344 | 0 | mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map->fd, 0); |
5345 | 0 | if (mmaped == MAP_FAILED) { |
5346 | 0 | err = -errno; |
5347 | 0 | pr_warn("map '%s': failed to re-mmap() contents: %s\n", |
5348 | 0 | bpf_map__name(map), errstr(err)); |
5349 | 0 | return err; |
5350 | 0 | } |
5351 | 0 | map->mmaped = mmaped; |
5352 | 0 | } else if (map->mmaped) { |
5353 | 0 | munmap(map->mmaped, mmap_sz); |
5354 | 0 | map->mmaped = NULL; |
5355 | 0 | } |
5356 | | |
5357 | 0 | return 0; |
5358 | 0 | } |
5359 | | |
5360 | | static void bpf_map__destroy(struct bpf_map *map); |
5361 | | |
5362 | | static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner) |
5363 | 0 | { |
5364 | 0 | LIBBPF_OPTS(bpf_map_create_opts, create_attr); |
5365 | 0 | struct bpf_map_def *def = &map->def; |
5366 | 0 | const char *map_name = NULL; |
5367 | 0 | int err = 0, map_fd; |
5368 | |
|
5369 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
5370 | 0 | map_name = map->name; |
5371 | 0 | create_attr.map_ifindex = map->map_ifindex; |
5372 | 0 | create_attr.map_flags = def->map_flags; |
5373 | 0 | create_attr.numa_node = map->numa_node; |
5374 | 0 | create_attr.map_extra = map->map_extra; |
5375 | 0 | create_attr.token_fd = obj->token_fd; |
5376 | 0 | if (obj->token_fd) |
5377 | 0 | create_attr.map_flags |= BPF_F_TOKEN_FD; |
5378 | 0 | if (map->excl_prog) { |
5379 | 0 | err = bpf_prog_compute_hash(map->excl_prog); |
5380 | 0 | if (err) |
5381 | 0 | return err; |
5382 | | |
5383 | 0 | create_attr.excl_prog_hash = map->excl_prog->hash; |
5384 | 0 | create_attr.excl_prog_hash_size = SHA256_DIGEST_LENGTH; |
5385 | 0 | } |
5386 | | |
5387 | 0 | if (bpf_map__is_struct_ops(map)) { |
5388 | 0 | create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
5389 | 0 | if (map->mod_btf_fd >= 0) { |
5390 | 0 | create_attr.value_type_btf_obj_fd = map->mod_btf_fd; |
5391 | 0 | create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD; |
5392 | 0 | } |
5393 | 0 | } |
5394 | |
|
5395 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0) { |
5396 | 0 | create_attr.btf_fd = btf__fd(obj->btf); |
5397 | 0 | create_attr.btf_key_type_id = map->btf_key_type_id; |
5398 | 0 | create_attr.btf_value_type_id = map->btf_value_type_id; |
5399 | 0 | } |
5400 | |
|
5401 | 0 | if (bpf_map_type__is_map_in_map(def->type)) { |
5402 | 0 | if (map->inner_map) { |
5403 | 0 | err = map_set_def_max_entries(map->inner_map); |
5404 | 0 | if (err) |
5405 | 0 | return err; |
5406 | 0 | err = bpf_object__create_map(obj, map->inner_map, true); |
5407 | 0 | if (err) { |
5408 | 0 | pr_warn("map '%s': failed to create inner map: %s\n", |
5409 | 0 | map->name, errstr(err)); |
5410 | 0 | return err; |
5411 | 0 | } |
5412 | 0 | map->inner_map_fd = map->inner_map->fd; |
5413 | 0 | } |
5414 | 0 | if (map->inner_map_fd >= 0) |
5415 | 0 | create_attr.inner_map_fd = map->inner_map_fd; |
5416 | 0 | } |
5417 | | |
5418 | 0 | switch (def->type) { |
5419 | 0 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
5420 | 0 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
5421 | 0 | case BPF_MAP_TYPE_STACK_TRACE: |
5422 | 0 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
5423 | 0 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
5424 | 0 | case BPF_MAP_TYPE_DEVMAP: |
5425 | 0 | case BPF_MAP_TYPE_DEVMAP_HASH: |
5426 | 0 | case BPF_MAP_TYPE_CPUMAP: |
5427 | 0 | case BPF_MAP_TYPE_XSKMAP: |
5428 | 0 | case BPF_MAP_TYPE_SOCKMAP: |
5429 | 0 | case BPF_MAP_TYPE_SOCKHASH: |
5430 | 0 | case BPF_MAP_TYPE_QUEUE: |
5431 | 0 | case BPF_MAP_TYPE_STACK: |
5432 | 0 | case BPF_MAP_TYPE_ARENA: |
5433 | 0 | create_attr.btf_fd = 0; |
5434 | 0 | create_attr.btf_key_type_id = 0; |
5435 | 0 | create_attr.btf_value_type_id = 0; |
5436 | 0 | map->btf_key_type_id = 0; |
5437 | 0 | map->btf_value_type_id = 0; |
5438 | 0 | break; |
5439 | 0 | case BPF_MAP_TYPE_STRUCT_OPS: |
5440 | 0 | create_attr.btf_value_type_id = 0; |
5441 | 0 | break; |
5442 | 0 | default: |
5443 | 0 | break; |
5444 | 0 | } |
5445 | | |
5446 | 0 | if (obj->gen_loader) { |
5447 | 0 | bpf_gen__map_create(obj->gen_loader, def->type, map_name, |
5448 | 0 | def->key_size, def->value_size, def->max_entries, |
5449 | 0 | &create_attr, is_inner ? -1 : map - obj->maps); |
5450 | | /* We keep pretenting we have valid FD to pass various fd >= 0 |
5451 | | * checks by just keeping original placeholder FDs in place. |
5452 | | * See bpf_object__add_map() comment. |
5453 | | * This placeholder fd will not be used with any syscall and |
5454 | | * will be reset to -1 eventually. |
5455 | | */ |
5456 | 0 | map_fd = map->fd; |
5457 | 0 | } else { |
5458 | 0 | map_fd = bpf_map_create(def->type, map_name, |
5459 | 0 | def->key_size, def->value_size, |
5460 | 0 | def->max_entries, &create_attr); |
5461 | 0 | } |
5462 | 0 | if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) { |
5463 | 0 | err = -errno; |
5464 | 0 | pr_warn("Error in bpf_create_map_xattr(%s): %s. Retrying without BTF.\n", |
5465 | 0 | map->name, errstr(err)); |
5466 | 0 | create_attr.btf_fd = 0; |
5467 | 0 | create_attr.btf_key_type_id = 0; |
5468 | 0 | create_attr.btf_value_type_id = 0; |
5469 | 0 | map->btf_key_type_id = 0; |
5470 | 0 | map->btf_value_type_id = 0; |
5471 | 0 | map_fd = bpf_map_create(def->type, map_name, |
5472 | 0 | def->key_size, def->value_size, |
5473 | 0 | def->max_entries, &create_attr); |
5474 | 0 | } |
5475 | |
|
5476 | 0 | if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { |
5477 | 0 | if (obj->gen_loader) |
5478 | 0 | map->inner_map->fd = -1; |
5479 | 0 | bpf_map__destroy(map->inner_map); |
5480 | 0 | zfree(&map->inner_map); |
5481 | 0 | } |
5482 | |
|
5483 | 0 | if (map_fd < 0) |
5484 | 0 | return map_fd; |
5485 | | |
5486 | | /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */ |
5487 | 0 | if (map->fd == map_fd) |
5488 | 0 | return 0; |
5489 | | |
5490 | | /* Keep placeholder FD value but now point it to the BPF map object. |
5491 | | * This way everything that relied on this map's FD (e.g., relocated |
5492 | | * ldimm64 instructions) will stay valid and won't need adjustments. |
5493 | | * map->fd stays valid but now point to what map_fd points to. |
5494 | | */ |
5495 | 0 | return reuse_fd(map->fd, map_fd); |
5496 | 0 | } |
5497 | | |
5498 | | static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map) |
5499 | 0 | { |
5500 | 0 | const struct bpf_map *targ_map; |
5501 | 0 | unsigned int i; |
5502 | 0 | int fd, err = 0; |
5503 | |
|
5504 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5505 | 0 | if (!map->init_slots[i]) |
5506 | 0 | continue; |
5507 | | |
5508 | 0 | targ_map = map->init_slots[i]; |
5509 | 0 | fd = targ_map->fd; |
5510 | |
|
5511 | 0 | if (obj->gen_loader) { |
5512 | 0 | bpf_gen__populate_outer_map(obj->gen_loader, |
5513 | 0 | map - obj->maps, i, |
5514 | 0 | targ_map - obj->maps); |
5515 | 0 | } else { |
5516 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5517 | 0 | } |
5518 | 0 | if (err) { |
5519 | 0 | err = -errno; |
5520 | 0 | pr_warn("map '%s': failed to initialize slot [%u] to map '%s' fd=%d: %s\n", |
5521 | 0 | map->name, i, targ_map->name, fd, errstr(err)); |
5522 | 0 | return err; |
5523 | 0 | } |
5524 | 0 | pr_debug("map '%s': slot [%u] set to map '%s' fd=%d\n", |
5525 | 0 | map->name, i, targ_map->name, fd); |
5526 | 0 | } |
5527 | | |
5528 | 0 | zfree(&map->init_slots); |
5529 | 0 | map->init_slots_sz = 0; |
5530 | |
|
5531 | 0 | return 0; |
5532 | 0 | } |
5533 | | |
5534 | | static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map) |
5535 | 0 | { |
5536 | 0 | const struct bpf_program *targ_prog; |
5537 | 0 | unsigned int i; |
5538 | 0 | int fd, err; |
5539 | |
|
5540 | 0 | if (obj->gen_loader) |
5541 | 0 | return -ENOTSUP; |
5542 | | |
5543 | 0 | for (i = 0; i < map->init_slots_sz; i++) { |
5544 | 0 | if (!map->init_slots[i]) |
5545 | 0 | continue; |
5546 | | |
5547 | 0 | targ_prog = map->init_slots[i]; |
5548 | 0 | fd = bpf_program__fd(targ_prog); |
5549 | |
|
5550 | 0 | err = bpf_map_update_elem(map->fd, &i, &fd, 0); |
5551 | 0 | if (err) { |
5552 | 0 | err = -errno; |
5553 | 0 | pr_warn("map '%s': failed to initialize slot [%u] to prog '%s' fd=%d: %s\n", |
5554 | 0 | map->name, i, targ_prog->name, fd, errstr(err)); |
5555 | 0 | return err; |
5556 | 0 | } |
5557 | 0 | pr_debug("map '%s': slot [%u] set to prog '%s' fd=%d\n", |
5558 | 0 | map->name, i, targ_prog->name, fd); |
5559 | 0 | } |
5560 | | |
5561 | 0 | zfree(&map->init_slots); |
5562 | 0 | map->init_slots_sz = 0; |
5563 | |
|
5564 | 0 | return 0; |
5565 | 0 | } |
5566 | | |
5567 | | static int bpf_object_init_prog_arrays(struct bpf_object *obj) |
5568 | 0 | { |
5569 | 0 | struct bpf_map *map; |
5570 | 0 | int i, err; |
5571 | |
|
5572 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5573 | 0 | map = &obj->maps[i]; |
5574 | |
|
5575 | 0 | if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY) |
5576 | 0 | continue; |
5577 | | |
5578 | 0 | err = init_prog_array_slots(obj, map); |
5579 | 0 | if (err < 0) |
5580 | 0 | return err; |
5581 | 0 | } |
5582 | 0 | return 0; |
5583 | 0 | } |
5584 | | |
5585 | | static int map_set_def_max_entries(struct bpf_map *map) |
5586 | 0 | { |
5587 | 0 | if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) { |
5588 | 0 | int nr_cpus; |
5589 | |
|
5590 | 0 | nr_cpus = libbpf_num_possible_cpus(); |
5591 | 0 | if (nr_cpus < 0) { |
5592 | 0 | pr_warn("map '%s': failed to determine number of system CPUs: %d\n", |
5593 | 0 | map->name, nr_cpus); |
5594 | 0 | return nr_cpus; |
5595 | 0 | } |
5596 | 0 | pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus); |
5597 | 0 | map->def.max_entries = nr_cpus; |
5598 | 0 | } |
5599 | | |
5600 | 0 | return 0; |
5601 | 0 | } |
5602 | | |
5603 | | static int |
5604 | | bpf_object__create_maps(struct bpf_object *obj) |
5605 | 0 | { |
5606 | 0 | struct bpf_map *map; |
5607 | 0 | unsigned int i, j; |
5608 | 0 | int err; |
5609 | 0 | bool retried; |
5610 | |
|
5611 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
5612 | 0 | map = &obj->maps[i]; |
5613 | | |
5614 | | /* To support old kernels, we skip creating global data maps |
5615 | | * (.rodata, .data, .kconfig, etc); later on, during program |
5616 | | * loading, if we detect that at least one of the to-be-loaded |
5617 | | * programs is referencing any global data map, we'll error |
5618 | | * out with program name and relocation index logged. |
5619 | | * This approach allows to accommodate Clang emitting |
5620 | | * unnecessary .rodata.str1.1 sections for string literals, |
5621 | | * but also it allows to have CO-RE applications that use |
5622 | | * global variables in some of BPF programs, but not others. |
5623 | | * If those global variable-using programs are not loaded at |
5624 | | * runtime due to bpf_program__set_autoload(prog, false), |
5625 | | * bpf_object loading will succeed just fine even on old |
5626 | | * kernels. |
5627 | | */ |
5628 | 0 | if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA)) |
5629 | 0 | map->autocreate = false; |
5630 | |
|
5631 | 0 | if (!map->autocreate) { |
5632 | 0 | pr_debug("map '%s': skipped auto-creating...\n", map->name); |
5633 | 0 | continue; |
5634 | 0 | } |
5635 | | |
5636 | 0 | err = map_set_def_max_entries(map); |
5637 | 0 | if (err) |
5638 | 0 | goto err_out; |
5639 | | |
5640 | 0 | retried = false; |
5641 | 0 | retry: |
5642 | 0 | if (map->pin_path) { |
5643 | 0 | err = bpf_object__reuse_map(map); |
5644 | 0 | if (err) { |
5645 | 0 | pr_warn("map '%s': error reusing pinned map\n", |
5646 | 0 | map->name); |
5647 | 0 | goto err_out; |
5648 | 0 | } |
5649 | 0 | if (retried && map->fd < 0) { |
5650 | 0 | pr_warn("map '%s': cannot find pinned map\n", |
5651 | 0 | map->name); |
5652 | 0 | err = -ENOENT; |
5653 | 0 | goto err_out; |
5654 | 0 | } |
5655 | 0 | } |
5656 | | |
5657 | 0 | if (map->reused) { |
5658 | 0 | pr_debug("map '%s': skipping creation (preset fd=%d)\n", |
5659 | 0 | map->name, map->fd); |
5660 | 0 | } else { |
5661 | 0 | err = bpf_object__create_map(obj, map, false); |
5662 | 0 | if (err) |
5663 | 0 | goto err_out; |
5664 | | |
5665 | 0 | pr_debug("map '%s': created successfully, fd=%d\n", |
5666 | 0 | map->name, map->fd); |
5667 | |
|
5668 | 0 | if (bpf_map__is_internal(map)) { |
5669 | 0 | err = bpf_object__populate_internal_map(obj, map); |
5670 | 0 | if (err < 0) |
5671 | 0 | goto err_out; |
5672 | 0 | } else if (map->def.type == BPF_MAP_TYPE_ARENA) { |
5673 | 0 | map->mmaped = mmap((void *)(long)map->map_extra, |
5674 | 0 | bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE, |
5675 | 0 | map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED, |
5676 | 0 | map->fd, 0); |
5677 | 0 | if (map->mmaped == MAP_FAILED) { |
5678 | 0 | err = -errno; |
5679 | 0 | map->mmaped = NULL; |
5680 | 0 | pr_warn("map '%s': failed to mmap arena: %s\n", |
5681 | 0 | map->name, errstr(err)); |
5682 | 0 | return err; |
5683 | 0 | } |
5684 | 0 | if (obj->arena_data) { |
5685 | 0 | memcpy(map->mmaped + obj->arena_data_off, obj->arena_data, |
5686 | 0 | obj->arena_data_sz); |
5687 | 0 | zfree(&obj->arena_data); |
5688 | 0 | } |
5689 | 0 | } |
5690 | 0 | if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) { |
5691 | 0 | err = init_map_in_map_slots(obj, map); |
5692 | 0 | if (err < 0) |
5693 | 0 | goto err_out; |
5694 | 0 | } |
5695 | 0 | } |
5696 | | |
5697 | 0 | if (map->pin_path && !map->pinned) { |
5698 | 0 | err = bpf_map__pin(map, NULL); |
5699 | 0 | if (err) { |
5700 | 0 | if (!retried && err == -EEXIST) { |
5701 | 0 | retried = true; |
5702 | 0 | goto retry; |
5703 | 0 | } |
5704 | 0 | pr_warn("map '%s': failed to auto-pin at '%s': %s\n", |
5705 | 0 | map->name, map->pin_path, errstr(err)); |
5706 | 0 | goto err_out; |
5707 | 0 | } |
5708 | 0 | } |
5709 | 0 | } |
5710 | | |
5711 | 0 | return 0; |
5712 | | |
5713 | 0 | err_out: |
5714 | 0 | pr_warn("map '%s': failed to create: %s\n", map->name, errstr(err)); |
5715 | 0 | pr_perm_msg(err); |
5716 | 0 | for (j = 0; j < i; j++) |
5717 | 0 | zclose(obj->maps[j].fd); |
5718 | 0 | return err; |
5719 | 0 | } |
5720 | | |
5721 | | static bool bpf_core_is_flavor_sep(const char *s) |
5722 | 1.05k | { |
5723 | | /* check X___Y name pattern, where X and Y are not underscores */ |
5724 | 1.05k | return s[0] != '_' && /* X */ |
5725 | 937 | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ |
5726 | 28 | s[4] != '_'; /* Y */ |
5727 | 1.05k | } |
5728 | | |
5729 | | /* Given 'some_struct_name___with_flavor' return the length of a name prefix |
5730 | | * before last triple underscore. Struct name part after last triple |
5731 | | * underscore is ignored by BPF CO-RE relocation during relocation matching. |
5732 | | */ |
5733 | | size_t bpf_core_essential_name_len(const char *name) |
5734 | 873 | { |
5735 | 873 | size_t n = strlen(name); |
5736 | 873 | int i; |
5737 | | |
5738 | 1.92k | for (i = n - 5; i >= 0; i--) { |
5739 | 1.05k | if (bpf_core_is_flavor_sep(name + i)) |
5740 | 11 | return i + 1; |
5741 | 1.05k | } |
5742 | 862 | return n; |
5743 | 873 | } |
5744 | | |
5745 | | void bpf_core_free_cands(struct bpf_core_cand_list *cands) |
5746 | 0 | { |
5747 | 0 | if (!cands) |
5748 | 0 | return; |
5749 | | |
5750 | 0 | free(cands->cands); |
5751 | 0 | free(cands); |
5752 | 0 | } |
5753 | | |
5754 | | int bpf_core_add_cands(struct bpf_core_cand *local_cand, |
5755 | | size_t local_essent_len, |
5756 | | const struct btf *targ_btf, |
5757 | | const char *targ_btf_name, |
5758 | | int targ_start_id, |
5759 | | struct bpf_core_cand_list *cands) |
5760 | 0 | { |
5761 | 0 | struct bpf_core_cand *new_cands, *cand; |
5762 | 0 | const struct btf_type *t, *local_t; |
5763 | 0 | const char *targ_name, *local_name; |
5764 | 0 | size_t targ_essent_len; |
5765 | 0 | int n, i; |
5766 | |
|
5767 | 0 | local_t = btf__type_by_id(local_cand->btf, local_cand->id); |
5768 | 0 | local_name = btf__str_by_offset(local_cand->btf, local_t->name_off); |
5769 | |
|
5770 | 0 | n = btf__type_cnt(targ_btf); |
5771 | 0 | for (i = targ_start_id; i < n; i++) { |
5772 | 0 | t = btf__type_by_id(targ_btf, i); |
5773 | 0 | if (!btf_kind_core_compat(t, local_t)) |
5774 | 0 | continue; |
5775 | | |
5776 | 0 | targ_name = btf__name_by_offset(targ_btf, t->name_off); |
5777 | 0 | if (str_is_empty(targ_name)) |
5778 | 0 | continue; |
5779 | | |
5780 | 0 | targ_essent_len = bpf_core_essential_name_len(targ_name); |
5781 | 0 | if (targ_essent_len != local_essent_len) |
5782 | 0 | continue; |
5783 | | |
5784 | 0 | if (strncmp(local_name, targ_name, local_essent_len) != 0) |
5785 | 0 | continue; |
5786 | | |
5787 | 0 | pr_debug("CO-RE relocating [%u] %s %s: found target candidate [%d] %s %s in [%s]\n", |
5788 | 0 | local_cand->id, btf_kind_str(local_t), |
5789 | 0 | local_name, i, btf_kind_str(t), targ_name, |
5790 | 0 | targ_btf_name); |
5791 | 0 | new_cands = libbpf_reallocarray(cands->cands, cands->len + 1, |
5792 | 0 | sizeof(*cands->cands)); |
5793 | 0 | if (!new_cands) |
5794 | 0 | return -ENOMEM; |
5795 | | |
5796 | 0 | cand = &new_cands[cands->len]; |
5797 | 0 | cand->btf = targ_btf; |
5798 | 0 | cand->id = i; |
5799 | |
|
5800 | 0 | cands->cands = new_cands; |
5801 | 0 | cands->len++; |
5802 | 0 | } |
5803 | 0 | return 0; |
5804 | 0 | } |
5805 | | |
5806 | | static int load_module_btfs(struct bpf_object *obj) |
5807 | 0 | { |
5808 | 0 | struct bpf_btf_info info; |
5809 | 0 | struct module_btf *mod_btf; |
5810 | 0 | struct btf *btf; |
5811 | 0 | char name[64]; |
5812 | 0 | __u32 id = 0, len; |
5813 | 0 | int err, fd; |
5814 | |
|
5815 | 0 | if (obj->btf_modules_loaded) |
5816 | 0 | return 0; |
5817 | | |
5818 | 0 | if (obj->gen_loader) |
5819 | 0 | return 0; |
5820 | | |
5821 | | /* don't do this again, even if we find no module BTFs */ |
5822 | 0 | obj->btf_modules_loaded = true; |
5823 | | |
5824 | | /* kernel too old to support module BTFs */ |
5825 | 0 | if (!kernel_supports(obj, FEAT_MODULE_BTF)) |
5826 | 0 | return 0; |
5827 | | |
5828 | 0 | while (true) { |
5829 | 0 | err = bpf_btf_get_next_id(id, &id); |
5830 | 0 | if (err && errno == ENOENT) |
5831 | 0 | return 0; |
5832 | 0 | if (err && errno == EPERM) { |
5833 | 0 | pr_debug("skipping module BTFs loading, missing privileges\n"); |
5834 | 0 | return 0; |
5835 | 0 | } |
5836 | 0 | if (err) { |
5837 | 0 | err = -errno; |
5838 | 0 | pr_warn("failed to iterate BTF objects: %s\n", errstr(err)); |
5839 | 0 | return err; |
5840 | 0 | } |
5841 | | |
5842 | 0 | fd = bpf_btf_get_fd_by_id(id); |
5843 | 0 | if (fd < 0) { |
5844 | 0 | if (errno == ENOENT) |
5845 | 0 | continue; /* expected race: BTF was unloaded */ |
5846 | 0 | err = -errno; |
5847 | 0 | pr_warn("failed to get BTF object #%u FD: %s\n", id, errstr(err)); |
5848 | 0 | return err; |
5849 | 0 | } |
5850 | | |
5851 | 0 | len = sizeof(info); |
5852 | 0 | memset(&info, 0, sizeof(info)); |
5853 | 0 | info.name = ptr_to_u64(name); |
5854 | 0 | info.name_len = sizeof(name); |
5855 | |
|
5856 | 0 | btf = NULL; |
5857 | 0 | err = bpf_btf_get_info_by_fd(fd, &info, &len); |
5858 | 0 | if (err) { |
5859 | 0 | err = -errno; |
5860 | 0 | pr_warn("failed to get BTF object #%u info: %s\n", id, errstr(err)); |
5861 | 0 | break; |
5862 | 0 | } |
5863 | | |
5864 | | /* ignore non-module BTFs */ |
5865 | 0 | if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) { |
5866 | 0 | close(fd); |
5867 | 0 | continue; |
5868 | 0 | } |
5869 | | |
5870 | 0 | btf = btf_get_from_fd(fd, obj->btf_vmlinux); |
5871 | 0 | err = libbpf_get_error(btf); |
5872 | 0 | if (err) { |
5873 | 0 | pr_warn("failed to load module [%s]'s BTF object #%u: %s\n", |
5874 | 0 | name, id, errstr(err)); |
5875 | 0 | break; |
5876 | 0 | } |
5877 | | |
5878 | 0 | err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap, |
5879 | 0 | sizeof(*obj->btf_modules), obj->btf_module_cnt + 1); |
5880 | 0 | if (err) |
5881 | 0 | break; |
5882 | | |
5883 | 0 | mod_btf = &obj->btf_modules[obj->btf_module_cnt]; |
5884 | |
|
5885 | 0 | mod_btf->btf = btf; |
5886 | 0 | mod_btf->id = id; |
5887 | 0 | mod_btf->fd = fd; |
5888 | 0 | mod_btf->name = strdup(name); |
5889 | 0 | if (!mod_btf->name) { |
5890 | 0 | err = -ENOMEM; |
5891 | 0 | break; |
5892 | 0 | } |
5893 | 0 | obj->btf_module_cnt++; |
5894 | 0 | } |
5895 | | |
5896 | 0 | if (err) { |
5897 | 0 | btf__free(btf); |
5898 | 0 | close(fd); |
5899 | 0 | } |
5900 | 0 | return err; |
5901 | 0 | } |
5902 | | |
5903 | | static struct bpf_core_cand_list * |
5904 | | bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id) |
5905 | 0 | { |
5906 | 0 | struct bpf_core_cand local_cand = {}; |
5907 | 0 | struct bpf_core_cand_list *cands; |
5908 | 0 | const struct btf *main_btf; |
5909 | 0 | const struct btf_type *local_t; |
5910 | 0 | const char *local_name; |
5911 | 0 | size_t local_essent_len; |
5912 | 0 | int err, i; |
5913 | |
|
5914 | 0 | local_cand.btf = local_btf; |
5915 | 0 | local_cand.id = local_type_id; |
5916 | 0 | local_t = btf__type_by_id(local_btf, local_type_id); |
5917 | 0 | if (!local_t) |
5918 | 0 | return ERR_PTR(-EINVAL); |
5919 | | |
5920 | 0 | local_name = btf__name_by_offset(local_btf, local_t->name_off); |
5921 | 0 | if (str_is_empty(local_name)) |
5922 | 0 | return ERR_PTR(-EINVAL); |
5923 | 0 | local_essent_len = bpf_core_essential_name_len(local_name); |
5924 | |
|
5925 | 0 | cands = calloc(1, sizeof(*cands)); |
5926 | 0 | if (!cands) |
5927 | 0 | return ERR_PTR(-ENOMEM); |
5928 | | |
5929 | | /* Attempt to find target candidates in vmlinux BTF first */ |
5930 | 0 | main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; |
5931 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands); |
5932 | 0 | if (err) |
5933 | 0 | goto err_out; |
5934 | | |
5935 | | /* if vmlinux BTF has any candidate, don't got for module BTFs */ |
5936 | 0 | if (cands->len) |
5937 | 0 | return cands; |
5938 | | |
5939 | | /* if vmlinux BTF was overridden, don't attempt to load module BTFs */ |
5940 | 0 | if (obj->btf_vmlinux_override) |
5941 | 0 | return cands; |
5942 | | |
5943 | | /* now look through module BTFs, trying to still find candidates */ |
5944 | 0 | err = load_module_btfs(obj); |
5945 | 0 | if (err) |
5946 | 0 | goto err_out; |
5947 | | |
5948 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
5949 | 0 | err = bpf_core_add_cands(&local_cand, local_essent_len, |
5950 | 0 | obj->btf_modules[i].btf, |
5951 | 0 | obj->btf_modules[i].name, |
5952 | 0 | btf__type_cnt(obj->btf_vmlinux), |
5953 | 0 | cands); |
5954 | 0 | if (err) |
5955 | 0 | goto err_out; |
5956 | 0 | } |
5957 | | |
5958 | 0 | return cands; |
5959 | 0 | err_out: |
5960 | 0 | bpf_core_free_cands(cands); |
5961 | 0 | return ERR_PTR(err); |
5962 | 0 | } |
5963 | | |
5964 | | /* Check local and target types for compatibility. This check is used for |
5965 | | * type-based CO-RE relocations and follow slightly different rules than |
5966 | | * field-based relocations. This function assumes that root types were already |
5967 | | * checked for name match. Beyond that initial root-level name check, names |
5968 | | * are completely ignored. Compatibility rules are as follows: |
5969 | | * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but |
5970 | | * kind should match for local and target types (i.e., STRUCT is not |
5971 | | * compatible with UNION); |
5972 | | * - for ENUMs, the size is ignored; |
5973 | | * - for INT, size and signedness are ignored; |
5974 | | * - for ARRAY, dimensionality is ignored, element types are checked for |
5975 | | * compatibility recursively; |
5976 | | * - CONST/VOLATILE/RESTRICT modifiers are ignored; |
5977 | | * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible; |
5978 | | * - FUNC_PROTOs are compatible if they have compatible signature: same |
5979 | | * number of input args and compatible return and argument types. |
5980 | | * These rules are not set in stone and probably will be adjusted as we get |
5981 | | * more experience with using BPF CO-RE relocations. |
5982 | | */ |
5983 | | int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, |
5984 | | const struct btf *targ_btf, __u32 targ_id) |
5985 | 0 | { |
5986 | 0 | return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32); |
5987 | 0 | } |
5988 | | |
5989 | | int bpf_core_types_match(const struct btf *local_btf, __u32 local_id, |
5990 | | const struct btf *targ_btf, __u32 targ_id) |
5991 | 0 | { |
5992 | 0 | return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32); |
5993 | 0 | } |
5994 | | |
5995 | | static size_t bpf_core_hash_fn(const long key, void *ctx) |
5996 | 0 | { |
5997 | 0 | return key; |
5998 | 0 | } |
5999 | | |
6000 | | static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx) |
6001 | 0 | { |
6002 | 0 | return k1 == k2; |
6003 | 0 | } |
6004 | | |
6005 | | static int record_relo_core(struct bpf_program *prog, |
6006 | | const struct bpf_core_relo *core_relo, int insn_idx) |
6007 | 0 | { |
6008 | 0 | struct reloc_desc *relos, *relo; |
6009 | |
|
6010 | 0 | relos = libbpf_reallocarray(prog->reloc_desc, |
6011 | 0 | prog->nr_reloc + 1, sizeof(*relos)); |
6012 | 0 | if (!relos) |
6013 | 0 | return -ENOMEM; |
6014 | 0 | relo = &relos[prog->nr_reloc]; |
6015 | 0 | relo->type = RELO_CORE; |
6016 | 0 | relo->insn_idx = insn_idx; |
6017 | 0 | relo->core_relo = core_relo; |
6018 | 0 | prog->reloc_desc = relos; |
6019 | 0 | prog->nr_reloc++; |
6020 | 0 | return 0; |
6021 | 0 | } |
6022 | | |
6023 | | static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx) |
6024 | 0 | { |
6025 | 0 | struct reloc_desc *relo; |
6026 | 0 | int i; |
6027 | |
|
6028 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
6029 | 0 | relo = &prog->reloc_desc[i]; |
6030 | 0 | if (relo->type != RELO_CORE || relo->insn_idx != insn_idx) |
6031 | 0 | continue; |
6032 | | |
6033 | 0 | return relo->core_relo; |
6034 | 0 | } |
6035 | | |
6036 | 0 | return NULL; |
6037 | 0 | } |
6038 | | |
6039 | | static int bpf_core_resolve_relo(struct bpf_program *prog, |
6040 | | const struct bpf_core_relo *relo, |
6041 | | int relo_idx, |
6042 | | const struct btf *local_btf, |
6043 | | struct hashmap *cand_cache, |
6044 | | struct bpf_core_relo_res *targ_res) |
6045 | 0 | { |
6046 | 0 | struct bpf_core_spec specs_scratch[3] = {}; |
6047 | 0 | struct bpf_core_cand_list *cands = NULL; |
6048 | 0 | const char *prog_name = prog->name; |
6049 | 0 | const struct btf_type *local_type; |
6050 | 0 | const char *local_name; |
6051 | 0 | __u32 local_id = relo->type_id; |
6052 | 0 | int err; |
6053 | |
|
6054 | 0 | local_type = btf__type_by_id(local_btf, local_id); |
6055 | 0 | if (!local_type) |
6056 | 0 | return -EINVAL; |
6057 | | |
6058 | 0 | local_name = btf__name_by_offset(local_btf, local_type->name_off); |
6059 | 0 | if (!local_name) |
6060 | 0 | return -EINVAL; |
6061 | | |
6062 | 0 | if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && |
6063 | 0 | !hashmap__find(cand_cache, local_id, &cands)) { |
6064 | 0 | cands = bpf_core_find_cands(prog->obj, local_btf, local_id); |
6065 | 0 | if (IS_ERR(cands)) { |
6066 | 0 | pr_warn("prog '%s': relo #%d: target candidate search failed for [%u] %s %s: %ld\n", |
6067 | 0 | prog_name, relo_idx, local_id, btf_kind_str(local_type), |
6068 | 0 | local_name, PTR_ERR(cands)); |
6069 | 0 | return PTR_ERR(cands); |
6070 | 0 | } |
6071 | 0 | err = hashmap__set(cand_cache, local_id, cands, NULL, NULL); |
6072 | 0 | if (err) { |
6073 | 0 | bpf_core_free_cands(cands); |
6074 | 0 | return err; |
6075 | 0 | } |
6076 | 0 | } |
6077 | | |
6078 | 0 | return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch, |
6079 | 0 | targ_res); |
6080 | 0 | } |
6081 | | |
6082 | | static int |
6083 | | bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) |
6084 | 0 | { |
6085 | 0 | const struct btf_ext_info_sec *sec; |
6086 | 0 | struct bpf_core_relo_res targ_res; |
6087 | 0 | const struct bpf_core_relo *rec; |
6088 | 0 | const struct btf_ext_info *seg; |
6089 | 0 | struct hashmap_entry *entry; |
6090 | 0 | struct hashmap *cand_cache = NULL; |
6091 | 0 | struct bpf_program *prog; |
6092 | 0 | struct bpf_insn *insn; |
6093 | 0 | const char *sec_name; |
6094 | 0 | int i, err = 0, insn_idx, sec_idx, sec_num; |
6095 | |
|
6096 | 0 | if (obj->btf_ext->core_relo_info.len == 0) |
6097 | 0 | return 0; |
6098 | | |
6099 | 0 | if (targ_btf_path) { |
6100 | 0 | obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL); |
6101 | 0 | err = libbpf_get_error(obj->btf_vmlinux_override); |
6102 | 0 | if (err) { |
6103 | 0 | pr_warn("failed to parse target BTF: %s\n", errstr(err)); |
6104 | 0 | return err; |
6105 | 0 | } |
6106 | 0 | } |
6107 | | |
6108 | 0 | cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); |
6109 | 0 | if (IS_ERR(cand_cache)) { |
6110 | 0 | err = PTR_ERR(cand_cache); |
6111 | 0 | goto out; |
6112 | 0 | } |
6113 | | |
6114 | 0 | seg = &obj->btf_ext->core_relo_info; |
6115 | 0 | sec_num = 0; |
6116 | 0 | for_each_btf_ext_sec(seg, sec) { |
6117 | 0 | sec_idx = seg->sec_idxs[sec_num]; |
6118 | 0 | sec_num++; |
6119 | |
|
6120 | 0 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
6121 | 0 | if (str_is_empty(sec_name)) { |
6122 | 0 | err = -EINVAL; |
6123 | 0 | goto out; |
6124 | 0 | } |
6125 | | |
6126 | 0 | pr_debug("sec '%s': found %u CO-RE relocations\n", sec_name, sec->num_info); |
6127 | |
|
6128 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
6129 | 0 | if (rec->insn_off % BPF_INSN_SZ) |
6130 | 0 | return -EINVAL; |
6131 | 0 | insn_idx = rec->insn_off / BPF_INSN_SZ; |
6132 | 0 | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
6133 | 0 | if (!prog) { |
6134 | | /* When __weak subprog is "overridden" by another instance |
6135 | | * of the subprog from a different object file, linker still |
6136 | | * appends all the .BTF.ext info that used to belong to that |
6137 | | * eliminated subprogram. |
6138 | | * This is similar to what x86-64 linker does for relocations. |
6139 | | * So just ignore such relocations just like we ignore |
6140 | | * subprog instructions when discovering subprograms. |
6141 | | */ |
6142 | 0 | pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n", |
6143 | 0 | sec_name, i, insn_idx); |
6144 | 0 | continue; |
6145 | 0 | } |
6146 | | /* no need to apply CO-RE relocation if the program is |
6147 | | * not going to be loaded |
6148 | | */ |
6149 | 0 | if (!prog->autoload) |
6150 | 0 | continue; |
6151 | | |
6152 | | /* adjust insn_idx from section frame of reference to the local |
6153 | | * program's frame of reference; (sub-)program code is not yet |
6154 | | * relocated, so it's enough to just subtract in-section offset |
6155 | | */ |
6156 | 0 | insn_idx = insn_idx - prog->sec_insn_off; |
6157 | 0 | if (insn_idx >= prog->insns_cnt) |
6158 | 0 | return -EINVAL; |
6159 | 0 | insn = &prog->insns[insn_idx]; |
6160 | |
|
6161 | 0 | err = record_relo_core(prog, rec, insn_idx); |
6162 | 0 | if (err) { |
6163 | 0 | pr_warn("prog '%s': relo #%d: failed to record relocation: %s\n", |
6164 | 0 | prog->name, i, errstr(err)); |
6165 | 0 | goto out; |
6166 | 0 | } |
6167 | | |
6168 | 0 | if (prog->obj->gen_loader) |
6169 | 0 | continue; |
6170 | | |
6171 | 0 | err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res); |
6172 | 0 | if (err) { |
6173 | 0 | pr_warn("prog '%s': relo #%d: failed to relocate: %s\n", |
6174 | 0 | prog->name, i, errstr(err)); |
6175 | 0 | goto out; |
6176 | 0 | } |
6177 | | |
6178 | 0 | err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res); |
6179 | 0 | if (err) { |
6180 | 0 | pr_warn("prog '%s': relo #%d: failed to patch insn #%d: %s\n", |
6181 | 0 | prog->name, i, insn_idx, errstr(err)); |
6182 | 0 | goto out; |
6183 | 0 | } |
6184 | 0 | } |
6185 | 0 | } |
6186 | | |
6187 | 0 | out: |
6188 | | /* obj->btf_vmlinux and module BTFs are freed after object load */ |
6189 | 0 | btf__free(obj->btf_vmlinux_override); |
6190 | 0 | obj->btf_vmlinux_override = NULL; |
6191 | |
|
6192 | 0 | if (!IS_ERR_OR_NULL(cand_cache)) { |
6193 | 0 | hashmap__for_each_entry(cand_cache, entry, i) { |
6194 | 0 | bpf_core_free_cands(entry->pvalue); |
6195 | 0 | } |
6196 | 0 | hashmap__free(cand_cache); |
6197 | 0 | } |
6198 | 0 | return err; |
6199 | 0 | } |
6200 | | |
6201 | | /* base map load ldimm64 special constant, used also for log fixup logic */ |
6202 | 0 | #define POISON_LDIMM64_MAP_BASE 2001000000 |
6203 | | #define POISON_LDIMM64_MAP_PFX "200100" |
6204 | | |
6205 | | static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx, |
6206 | | int insn_idx, struct bpf_insn *insn, |
6207 | | int map_idx, const struct bpf_map *map) |
6208 | 0 | { |
6209 | 0 | int i; |
6210 | |
|
6211 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n", |
6212 | 0 | prog->name, relo_idx, insn_idx, map_idx, map->name); |
6213 | | |
6214 | | /* we turn single ldimm64 into two identical invalid calls */ |
6215 | 0 | for (i = 0; i < 2; i++) { |
6216 | 0 | insn->code = BPF_JMP | BPF_CALL; |
6217 | 0 | insn->dst_reg = 0; |
6218 | 0 | insn->src_reg = 0; |
6219 | 0 | insn->off = 0; |
6220 | | /* if this instruction is reachable (not a dead code), |
6221 | | * verifier will complain with something like: |
6222 | | * invalid func unknown#2001000123 |
6223 | | * where lower 123 is map index into obj->maps[] array |
6224 | | */ |
6225 | 0 | insn->imm = POISON_LDIMM64_MAP_BASE + map_idx; |
6226 | |
|
6227 | 0 | insn++; |
6228 | 0 | } |
6229 | 0 | } |
6230 | | |
6231 | | /* unresolved kfunc call special constant, used also for log fixup logic */ |
6232 | 0 | #define POISON_CALL_KFUNC_BASE 2002000000 |
6233 | | #define POISON_CALL_KFUNC_PFX "2002" |
6234 | | |
6235 | | static void poison_kfunc_call(struct bpf_program *prog, int relo_idx, |
6236 | | int insn_idx, struct bpf_insn *insn, |
6237 | | int ext_idx, const struct extern_desc *ext) |
6238 | 0 | { |
6239 | 0 | pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n", |
6240 | 0 | prog->name, relo_idx, insn_idx, ext->name); |
6241 | | |
6242 | | /* we turn kfunc call into invalid helper call with identifiable constant */ |
6243 | 0 | insn->code = BPF_JMP | BPF_CALL; |
6244 | 0 | insn->dst_reg = 0; |
6245 | 0 | insn->src_reg = 0; |
6246 | 0 | insn->off = 0; |
6247 | | /* if this instruction is reachable (not a dead code), |
6248 | | * verifier will complain with something like: |
6249 | | * invalid func unknown#2001000123 |
6250 | | * where lower 123 is extern index into obj->externs[] array |
6251 | | */ |
6252 | 0 | insn->imm = POISON_CALL_KFUNC_BASE + ext_idx; |
6253 | 0 | } |
6254 | | |
6255 | | static int find_jt_map(struct bpf_object *obj, struct bpf_program *prog, unsigned int sym_off) |
6256 | 0 | { |
6257 | 0 | size_t i; |
6258 | |
|
6259 | 0 | for (i = 0; i < obj->jumptable_map_cnt; i++) { |
6260 | | /* |
6261 | | * This might happen that same offset is used for two different |
6262 | | * programs (as jump tables can be the same). However, for |
6263 | | * different programs different maps should be created. |
6264 | | */ |
6265 | 0 | if (obj->jumptable_maps[i].sym_off == sym_off && |
6266 | 0 | obj->jumptable_maps[i].prog == prog) |
6267 | 0 | return obj->jumptable_maps[i].fd; |
6268 | 0 | } |
6269 | | |
6270 | 0 | return -ENOENT; |
6271 | 0 | } |
6272 | | |
6273 | | static int add_jt_map(struct bpf_object *obj, struct bpf_program *prog, unsigned int sym_off, int map_fd) |
6274 | 0 | { |
6275 | 0 | size_t cnt = obj->jumptable_map_cnt; |
6276 | 0 | size_t size = sizeof(obj->jumptable_maps[0]); |
6277 | 0 | void *tmp; |
6278 | |
|
6279 | 0 | tmp = libbpf_reallocarray(obj->jumptable_maps, cnt + 1, size); |
6280 | 0 | if (!tmp) |
6281 | 0 | return -ENOMEM; |
6282 | | |
6283 | 0 | obj->jumptable_maps = tmp; |
6284 | 0 | obj->jumptable_maps[cnt].prog = prog; |
6285 | 0 | obj->jumptable_maps[cnt].sym_off = sym_off; |
6286 | 0 | obj->jumptable_maps[cnt].fd = map_fd; |
6287 | 0 | obj->jumptable_map_cnt++; |
6288 | |
|
6289 | 0 | return 0; |
6290 | 0 | } |
6291 | | |
6292 | | static int find_subprog_idx(struct bpf_program *prog, int insn_idx) |
6293 | 0 | { |
6294 | 0 | int i; |
6295 | |
|
6296 | 0 | for (i = prog->subprog_cnt - 1; i >= 0; i--) { |
6297 | 0 | if (insn_idx >= prog->subprogs[i].sub_insn_off) |
6298 | 0 | return i; |
6299 | 0 | } |
6300 | | |
6301 | 0 | return -1; |
6302 | 0 | } |
6303 | | |
6304 | | static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struct reloc_desc *relo) |
6305 | 0 | { |
6306 | 0 | const __u32 jt_entry_size = 8; |
6307 | 0 | unsigned int sym_off = relo->sym_off; |
6308 | 0 | int jt_size = relo->sym_size; |
6309 | 0 | __u32 max_entries = jt_size / jt_entry_size; |
6310 | 0 | __u32 value_size = sizeof(struct bpf_insn_array_value); |
6311 | 0 | struct bpf_insn_array_value val = {}; |
6312 | 0 | int subprog_idx; |
6313 | 0 | int map_fd, err; |
6314 | 0 | __u64 insn_off; |
6315 | 0 | __u64 *jt; |
6316 | 0 | __u32 i; |
6317 | |
|
6318 | 0 | map_fd = find_jt_map(obj, prog, sym_off); |
6319 | 0 | if (map_fd >= 0) |
6320 | 0 | return map_fd; |
6321 | | |
6322 | 0 | if (sym_off % jt_entry_size) { |
6323 | 0 | pr_warn("map '.jumptables': jumptable start %u should be multiple of %u\n", |
6324 | 0 | sym_off, jt_entry_size); |
6325 | 0 | return -EINVAL; |
6326 | 0 | } |
6327 | | |
6328 | 0 | if (jt_size % jt_entry_size) { |
6329 | 0 | pr_warn("map '.jumptables': jumptable size %d should be multiple of %u\n", |
6330 | 0 | jt_size, jt_entry_size); |
6331 | 0 | return -EINVAL; |
6332 | 0 | } |
6333 | | |
6334 | 0 | map_fd = bpf_map_create(BPF_MAP_TYPE_INSN_ARRAY, ".jumptables", |
6335 | 0 | 4, value_size, max_entries, NULL); |
6336 | 0 | if (map_fd < 0) |
6337 | 0 | return map_fd; |
6338 | | |
6339 | 0 | if (!obj->jumptables_data) { |
6340 | 0 | pr_warn("map '.jumptables': ELF file is missing jump table data\n"); |
6341 | 0 | err = -EINVAL; |
6342 | 0 | goto err_close; |
6343 | 0 | } |
6344 | 0 | if (sym_off + jt_size > obj->jumptables_data_sz) { |
6345 | 0 | pr_warn("map '.jumptables': jumptables_data size is %zu, trying to access %u\n", |
6346 | 0 | obj->jumptables_data_sz, sym_off + jt_size); |
6347 | 0 | err = -EINVAL; |
6348 | 0 | goto err_close; |
6349 | 0 | } |
6350 | | |
6351 | 0 | subprog_idx = -1; /* main program */ |
6352 | 0 | if (relo->insn_idx < 0 || relo->insn_idx >= prog->insns_cnt) { |
6353 | 0 | pr_warn("map '.jumptables': invalid instruction index %d\n", relo->insn_idx); |
6354 | 0 | err = -EINVAL; |
6355 | 0 | goto err_close; |
6356 | 0 | } |
6357 | 0 | if (prog->subprogs) |
6358 | 0 | subprog_idx = find_subprog_idx(prog, relo->insn_idx); |
6359 | |
|
6360 | 0 | jt = (__u64 *)(obj->jumptables_data + sym_off); |
6361 | 0 | for (i = 0; i < max_entries; i++) { |
6362 | | /* |
6363 | | * The offset should be made to be relative to the beginning of |
6364 | | * the main function, not the subfunction. |
6365 | | */ |
6366 | 0 | insn_off = jt[i]/sizeof(struct bpf_insn); |
6367 | 0 | if (subprog_idx >= 0) { |
6368 | 0 | insn_off -= prog->subprogs[subprog_idx].sec_insn_off; |
6369 | 0 | insn_off += prog->subprogs[subprog_idx].sub_insn_off; |
6370 | 0 | } else { |
6371 | 0 | insn_off -= prog->sec_insn_off; |
6372 | 0 | } |
6373 | | |
6374 | | /* |
6375 | | * LLVM-generated jump tables contain u64 records, however |
6376 | | * should contain values that fit in u32. |
6377 | | */ |
6378 | 0 | if (insn_off > UINT32_MAX) { |
6379 | 0 | pr_warn("map '.jumptables': invalid jump table value 0x%llx at offset %u\n", |
6380 | 0 | (unsigned long long)jt[i], sym_off + i * jt_entry_size); |
6381 | 0 | err = -EINVAL; |
6382 | 0 | goto err_close; |
6383 | 0 | } |
6384 | | |
6385 | 0 | val.orig_off = insn_off; |
6386 | 0 | err = bpf_map_update_elem(map_fd, &i, &val, 0); |
6387 | 0 | if (err) |
6388 | 0 | goto err_close; |
6389 | 0 | } |
6390 | | |
6391 | 0 | err = bpf_map_freeze(map_fd); |
6392 | 0 | if (err) |
6393 | 0 | goto err_close; |
6394 | | |
6395 | 0 | err = add_jt_map(obj, prog, sym_off, map_fd); |
6396 | 0 | if (err) |
6397 | 0 | goto err_close; |
6398 | | |
6399 | 0 | return map_fd; |
6400 | | |
6401 | 0 | err_close: |
6402 | 0 | close(map_fd); |
6403 | 0 | return err; |
6404 | 0 | } |
6405 | | |
6406 | | /* Relocate data references within program code: |
6407 | | * - map references; |
6408 | | * - global variable references; |
6409 | | * - extern references. |
6410 | | */ |
6411 | | static int |
6412 | | bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) |
6413 | 0 | { |
6414 | 0 | int i; |
6415 | |
|
6416 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
6417 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
6418 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
6419 | 0 | const struct bpf_map *map; |
6420 | 0 | struct extern_desc *ext; |
6421 | |
|
6422 | 0 | switch (relo->type) { |
6423 | 0 | case RELO_LD64: |
6424 | 0 | map = &obj->maps[relo->map_idx]; |
6425 | 0 | if (obj->gen_loader) { |
6426 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX; |
6427 | 0 | insn[0].imm = relo->map_idx; |
6428 | 0 | } else if (map->autocreate) { |
6429 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_FD; |
6430 | 0 | insn[0].imm = map->fd; |
6431 | 0 | } else { |
6432 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6433 | 0 | relo->map_idx, map); |
6434 | 0 | } |
6435 | 0 | break; |
6436 | 0 | case RELO_DATA: |
6437 | 0 | map = &obj->maps[relo->map_idx]; |
6438 | 0 | insn[1].imm = insn[0].imm + relo->sym_off; |
6439 | |
|
6440 | 0 | if (relo->map_idx == obj->arena_map_idx) |
6441 | 0 | insn[1].imm += obj->arena_data_off; |
6442 | |
|
6443 | 0 | if (obj->gen_loader) { |
6444 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6445 | 0 | insn[0].imm = relo->map_idx; |
6446 | 0 | } else if (map->autocreate) { |
6447 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6448 | 0 | insn[0].imm = map->fd; |
6449 | 0 | } else { |
6450 | 0 | poison_map_ldimm64(prog, i, relo->insn_idx, insn, |
6451 | 0 | relo->map_idx, map); |
6452 | 0 | } |
6453 | 0 | break; |
6454 | 0 | case RELO_EXTERN_LD64: |
6455 | 0 | ext = &obj->externs[relo->ext_idx]; |
6456 | 0 | if (ext->type == EXT_KCFG) { |
6457 | 0 | if (obj->gen_loader) { |
6458 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; |
6459 | 0 | insn[0].imm = obj->kconfig_map_idx; |
6460 | 0 | } else { |
6461 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6462 | 0 | insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; |
6463 | 0 | } |
6464 | 0 | insn[1].imm = ext->kcfg.data_off; |
6465 | 0 | } else /* EXT_KSYM */ { |
6466 | 0 | if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */ |
6467 | 0 | insn[0].src_reg = BPF_PSEUDO_BTF_ID; |
6468 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6469 | 0 | insn[1].imm = ext->ksym.kernel_btf_obj_fd; |
6470 | 0 | } else { /* typeless ksyms or unresolved typed ksyms */ |
6471 | 0 | insn[0].imm = (__u32)ext->ksym.addr; |
6472 | 0 | insn[1].imm = ext->ksym.addr >> 32; |
6473 | 0 | } |
6474 | 0 | } |
6475 | 0 | break; |
6476 | 0 | case RELO_EXTERN_CALL: |
6477 | 0 | ext = &obj->externs[relo->ext_idx]; |
6478 | 0 | insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL; |
6479 | 0 | if (ext->is_set) { |
6480 | 0 | insn[0].imm = ext->ksym.kernel_btf_id; |
6481 | 0 | insn[0].off = ext->ksym.btf_fd_idx; |
6482 | 0 | } else { /* unresolved weak kfunc call */ |
6483 | 0 | poison_kfunc_call(prog, i, relo->insn_idx, insn, |
6484 | 0 | relo->ext_idx, ext); |
6485 | 0 | } |
6486 | 0 | break; |
6487 | 0 | case RELO_SUBPROG_ADDR: |
6488 | 0 | if (insn[0].src_reg != BPF_PSEUDO_FUNC) { |
6489 | 0 | pr_warn("prog '%s': relo #%d: bad insn\n", |
6490 | 0 | prog->name, i); |
6491 | 0 | return -EINVAL; |
6492 | 0 | } |
6493 | | /* handled already */ |
6494 | 0 | break; |
6495 | 0 | case RELO_CALL: |
6496 | | /* handled already */ |
6497 | 0 | break; |
6498 | 0 | case RELO_CORE: |
6499 | | /* will be handled by bpf_program_record_relos() */ |
6500 | 0 | break; |
6501 | 0 | case RELO_INSN_ARRAY: { |
6502 | 0 | int map_fd; |
6503 | |
|
6504 | 0 | map_fd = create_jt_map(obj, prog, relo); |
6505 | 0 | if (map_fd < 0) { |
6506 | 0 | pr_warn("prog '%s': relo #%d: can't create jump table: sym_off %u\n", |
6507 | 0 | prog->name, i, relo->sym_off); |
6508 | 0 | return map_fd; |
6509 | 0 | } |
6510 | 0 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
6511 | 0 | insn->imm = map_fd; |
6512 | 0 | insn->off = 0; |
6513 | 0 | } |
6514 | 0 | break; |
6515 | 0 | default: |
6516 | 0 | pr_warn("prog '%s': relo #%d: bad relo type %u\n", |
6517 | 0 | prog->name, i, relo->type); |
6518 | 0 | return -EINVAL; |
6519 | 0 | } |
6520 | 0 | } |
6521 | | |
6522 | 0 | return 0; |
6523 | 0 | } |
6524 | | |
6525 | | static int adjust_prog_btf_ext_info(const struct bpf_object *obj, |
6526 | | const struct bpf_program *prog, |
6527 | | const struct btf_ext_info *ext_info, |
6528 | | void **prog_info, __u32 *prog_rec_cnt, |
6529 | | __u32 *prog_rec_sz) |
6530 | 0 | { |
6531 | 0 | void *copy_start = NULL, *copy_end = NULL; |
6532 | 0 | void *rec, *rec_end, *new_prog_info; |
6533 | 0 | const struct btf_ext_info_sec *sec; |
6534 | 0 | size_t old_sz, new_sz; |
6535 | 0 | int i, sec_num, sec_idx, off_adj; |
6536 | |
|
6537 | 0 | sec_num = 0; |
6538 | 0 | for_each_btf_ext_sec(ext_info, sec) { |
6539 | 0 | sec_idx = ext_info->sec_idxs[sec_num]; |
6540 | 0 | sec_num++; |
6541 | 0 | if (prog->sec_idx != sec_idx) |
6542 | 0 | continue; |
6543 | | |
6544 | 0 | for_each_btf_ext_rec(ext_info, sec, i, rec) { |
6545 | 0 | __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ; |
6546 | |
|
6547 | 0 | if (insn_off < prog->sec_insn_off) |
6548 | 0 | continue; |
6549 | 0 | if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt) |
6550 | 0 | break; |
6551 | | |
6552 | 0 | if (!copy_start) |
6553 | 0 | copy_start = rec; |
6554 | 0 | copy_end = rec + ext_info->rec_size; |
6555 | 0 | } |
6556 | |
|
6557 | 0 | if (!copy_start) |
6558 | 0 | return -ENOENT; |
6559 | | |
6560 | | /* append func/line info of a given (sub-)program to the main |
6561 | | * program func/line info |
6562 | | */ |
6563 | 0 | old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size; |
6564 | 0 | new_sz = old_sz + (copy_end - copy_start); |
6565 | 0 | new_prog_info = realloc(*prog_info, new_sz); |
6566 | 0 | if (!new_prog_info) |
6567 | 0 | return -ENOMEM; |
6568 | 0 | *prog_info = new_prog_info; |
6569 | 0 | *prog_rec_cnt = new_sz / ext_info->rec_size; |
6570 | 0 | memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start); |
6571 | | |
6572 | | /* Kernel instruction offsets are in units of 8-byte |
6573 | | * instructions, while .BTF.ext instruction offsets generated |
6574 | | * by Clang are in units of bytes. So convert Clang offsets |
6575 | | * into kernel offsets and adjust offset according to program |
6576 | | * relocated position. |
6577 | | */ |
6578 | 0 | off_adj = prog->sub_insn_off - prog->sec_insn_off; |
6579 | 0 | rec = new_prog_info + old_sz; |
6580 | 0 | rec_end = new_prog_info + new_sz; |
6581 | 0 | for (; rec < rec_end; rec += ext_info->rec_size) { |
6582 | 0 | __u32 *insn_off = rec; |
6583 | |
|
6584 | 0 | *insn_off = *insn_off / BPF_INSN_SZ + off_adj; |
6585 | 0 | } |
6586 | 0 | *prog_rec_sz = ext_info->rec_size; |
6587 | 0 | return 0; |
6588 | 0 | } |
6589 | | |
6590 | 0 | return -ENOENT; |
6591 | 0 | } |
6592 | | |
6593 | | static int |
6594 | | reloc_prog_func_and_line_info(const struct bpf_object *obj, |
6595 | | struct bpf_program *main_prog, |
6596 | | const struct bpf_program *prog) |
6597 | 0 | { |
6598 | 0 | int err; |
6599 | | |
6600 | | /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't |
6601 | | * support func/line info |
6602 | | */ |
6603 | 0 | if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC)) |
6604 | 0 | return 0; |
6605 | | |
6606 | | /* only attempt func info relocation if main program's func_info |
6607 | | * relocation was successful |
6608 | | */ |
6609 | 0 | if (main_prog != prog && !main_prog->func_info) |
6610 | 0 | goto line_info; |
6611 | | |
6612 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info, |
6613 | 0 | &main_prog->func_info, |
6614 | 0 | &main_prog->func_info_cnt, |
6615 | 0 | &main_prog->func_info_rec_size); |
6616 | 0 | if (err) { |
6617 | 0 | if (err != -ENOENT) { |
6618 | 0 | pr_warn("prog '%s': error relocating .BTF.ext function info: %s\n", |
6619 | 0 | prog->name, errstr(err)); |
6620 | 0 | return err; |
6621 | 0 | } |
6622 | 0 | if (main_prog->func_info) { |
6623 | | /* |
6624 | | * Some info has already been found but has problem |
6625 | | * in the last btf_ext reloc. Must have to error out. |
6626 | | */ |
6627 | 0 | pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name); |
6628 | 0 | return err; |
6629 | 0 | } |
6630 | | /* Have problem loading the very first info. Ignore the rest. */ |
6631 | 0 | pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n", |
6632 | 0 | prog->name); |
6633 | 0 | } |
6634 | | |
6635 | 0 | line_info: |
6636 | | /* don't relocate line info if main program's relocation failed */ |
6637 | 0 | if (main_prog != prog && !main_prog->line_info) |
6638 | 0 | return 0; |
6639 | | |
6640 | 0 | err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info, |
6641 | 0 | &main_prog->line_info, |
6642 | 0 | &main_prog->line_info_cnt, |
6643 | 0 | &main_prog->line_info_rec_size); |
6644 | 0 | if (err) { |
6645 | 0 | if (err != -ENOENT) { |
6646 | 0 | pr_warn("prog '%s': error relocating .BTF.ext line info: %s\n", |
6647 | 0 | prog->name, errstr(err)); |
6648 | 0 | return err; |
6649 | 0 | } |
6650 | 0 | if (main_prog->line_info) { |
6651 | | /* |
6652 | | * Some info has already been found but has problem |
6653 | | * in the last btf_ext reloc. Must have to error out. |
6654 | | */ |
6655 | 0 | pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name); |
6656 | 0 | return err; |
6657 | 0 | } |
6658 | | /* Have problem loading the very first info. Ignore the rest. */ |
6659 | 0 | pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n", |
6660 | 0 | prog->name); |
6661 | 0 | } |
6662 | 0 | return 0; |
6663 | 0 | } |
6664 | | |
6665 | | static int cmp_relo_by_insn_idx(const void *key, const void *elem) |
6666 | 0 | { |
6667 | 0 | size_t insn_idx = *(const size_t *)key; |
6668 | 0 | const struct reloc_desc *relo = elem; |
6669 | |
|
6670 | 0 | if (insn_idx == relo->insn_idx) |
6671 | 0 | return 0; |
6672 | 0 | return insn_idx < relo->insn_idx ? -1 : 1; |
6673 | 0 | } |
6674 | | |
6675 | | static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx) |
6676 | 0 | { |
6677 | 0 | if (!prog->nr_reloc) |
6678 | 0 | return NULL; |
6679 | 0 | return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc, |
6680 | 0 | sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx); |
6681 | 0 | } |
6682 | | |
6683 | | static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog) |
6684 | 0 | { |
6685 | 0 | int new_cnt = main_prog->nr_reloc + subprog->nr_reloc; |
6686 | 0 | struct reloc_desc *relos; |
6687 | 0 | int i; |
6688 | |
|
6689 | 0 | if (main_prog == subprog) |
6690 | 0 | return 0; |
6691 | 0 | relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos)); |
6692 | | /* if new count is zero, reallocarray can return a valid NULL result; |
6693 | | * in this case the previous pointer will be freed, so we *have to* |
6694 | | * reassign old pointer to the new value (even if it's NULL) |
6695 | | */ |
6696 | 0 | if (!relos && new_cnt) |
6697 | 0 | return -ENOMEM; |
6698 | 0 | if (subprog->nr_reloc) |
6699 | 0 | memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc, |
6700 | 0 | sizeof(*relos) * subprog->nr_reloc); |
6701 | |
|
6702 | 0 | for (i = main_prog->nr_reloc; i < new_cnt; i++) |
6703 | 0 | relos[i].insn_idx += subprog->sub_insn_off; |
6704 | | /* After insn_idx adjustment the 'relos' array is still sorted |
6705 | | * by insn_idx and doesn't break bsearch. |
6706 | | */ |
6707 | 0 | main_prog->reloc_desc = relos; |
6708 | 0 | main_prog->nr_reloc = new_cnt; |
6709 | 0 | return 0; |
6710 | 0 | } |
6711 | | |
6712 | | static int save_subprog_offsets(struct bpf_program *main_prog, struct bpf_program *subprog) |
6713 | 0 | { |
6714 | 0 | size_t size = sizeof(main_prog->subprogs[0]); |
6715 | 0 | int cnt = main_prog->subprog_cnt; |
6716 | 0 | void *tmp; |
6717 | |
|
6718 | 0 | tmp = libbpf_reallocarray(main_prog->subprogs, cnt + 1, size); |
6719 | 0 | if (!tmp) |
6720 | 0 | return -ENOMEM; |
6721 | | |
6722 | 0 | main_prog->subprogs = tmp; |
6723 | 0 | main_prog->subprogs[cnt].sec_insn_off = subprog->sec_insn_off; |
6724 | 0 | main_prog->subprogs[cnt].sub_insn_off = subprog->sub_insn_off; |
6725 | 0 | main_prog->subprog_cnt++; |
6726 | |
|
6727 | 0 | return 0; |
6728 | 0 | } |
6729 | | |
6730 | | static int |
6731 | | bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6732 | | struct bpf_program *subprog) |
6733 | 0 | { |
6734 | 0 | struct bpf_insn *insns; |
6735 | 0 | size_t new_cnt; |
6736 | 0 | int err; |
6737 | |
|
6738 | 0 | subprog->sub_insn_off = main_prog->insns_cnt; |
6739 | |
|
6740 | 0 | new_cnt = main_prog->insns_cnt + subprog->insns_cnt; |
6741 | 0 | insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns)); |
6742 | 0 | if (!insns) { |
6743 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name); |
6744 | 0 | return -ENOMEM; |
6745 | 0 | } |
6746 | 0 | main_prog->insns = insns; |
6747 | 0 | main_prog->insns_cnt = new_cnt; |
6748 | |
|
6749 | 0 | memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns, |
6750 | 0 | subprog->insns_cnt * sizeof(*insns)); |
6751 | |
|
6752 | 0 | pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n", |
6753 | 0 | main_prog->name, subprog->insns_cnt, subprog->name); |
6754 | | |
6755 | | /* The subprog insns are now appended. Append its relos too. */ |
6756 | 0 | err = append_subprog_relos(main_prog, subprog); |
6757 | 0 | if (err) |
6758 | 0 | return err; |
6759 | | |
6760 | 0 | err = save_subprog_offsets(main_prog, subprog); |
6761 | 0 | if (err) { |
6762 | 0 | pr_warn("prog '%s': failed to add subprog offsets: %s\n", |
6763 | 0 | main_prog->name, errstr(err)); |
6764 | 0 | return err; |
6765 | 0 | } |
6766 | | |
6767 | 0 | return 0; |
6768 | 0 | } |
6769 | | |
6770 | | static int |
6771 | | bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog, |
6772 | | struct bpf_program *prog) |
6773 | 0 | { |
6774 | 0 | size_t sub_insn_idx, insn_idx; |
6775 | 0 | struct bpf_program *subprog; |
6776 | 0 | struct reloc_desc *relo; |
6777 | 0 | struct bpf_insn *insn; |
6778 | 0 | int err; |
6779 | |
|
6780 | 0 | err = reloc_prog_func_and_line_info(obj, main_prog, prog); |
6781 | 0 | if (err) |
6782 | 0 | return err; |
6783 | | |
6784 | 0 | for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) { |
6785 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6786 | 0 | if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn)) |
6787 | 0 | continue; |
6788 | | |
6789 | 0 | relo = find_prog_insn_relo(prog, insn_idx); |
6790 | 0 | if (relo && relo->type == RELO_EXTERN_CALL) |
6791 | | /* kfunc relocations will be handled later |
6792 | | * in bpf_object__relocate_data() |
6793 | | */ |
6794 | 0 | continue; |
6795 | 0 | if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) { |
6796 | 0 | pr_warn("prog '%s': unexpected relo for insn #%zu, type %u\n", |
6797 | 0 | prog->name, insn_idx, relo->type); |
6798 | 0 | return -LIBBPF_ERRNO__RELOC; |
6799 | 0 | } |
6800 | 0 | if (relo) { |
6801 | | /* sub-program instruction index is a combination of |
6802 | | * an offset of a symbol pointed to by relocation and |
6803 | | * call instruction's imm field; for global functions, |
6804 | | * call always has imm = -1, but for static functions |
6805 | | * relocation is against STT_SECTION and insn->imm |
6806 | | * points to a start of a static function |
6807 | | * |
6808 | | * for subprog addr relocation, the relo->sym_off + insn->imm is |
6809 | | * the byte offset in the corresponding section. |
6810 | | */ |
6811 | 0 | if (relo->type == RELO_CALL) |
6812 | 0 | sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1; |
6813 | 0 | else |
6814 | 0 | sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ; |
6815 | 0 | } else if (insn_is_pseudo_func(insn)) { |
6816 | | /* |
6817 | | * RELO_SUBPROG_ADDR relo is always emitted even if both |
6818 | | * functions are in the same section, so it shouldn't reach here. |
6819 | | */ |
6820 | 0 | pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n", |
6821 | 0 | prog->name, insn_idx); |
6822 | 0 | return -LIBBPF_ERRNO__RELOC; |
6823 | 0 | } else { |
6824 | | /* if subprogram call is to a static function within |
6825 | | * the same ELF section, there won't be any relocation |
6826 | | * emitted, but it also means there is no additional |
6827 | | * offset necessary, insns->imm is relative to |
6828 | | * instruction's original position within the section |
6829 | | */ |
6830 | 0 | sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1; |
6831 | 0 | } |
6832 | | |
6833 | | /* we enforce that sub-programs should be in .text section */ |
6834 | 0 | subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx); |
6835 | 0 | if (!subprog) { |
6836 | 0 | pr_warn("prog '%s': no .text section found yet sub-program call exists\n", |
6837 | 0 | prog->name); |
6838 | 0 | return -LIBBPF_ERRNO__RELOC; |
6839 | 0 | } |
6840 | | |
6841 | | /* if it's the first call instruction calling into this |
6842 | | * subprogram (meaning this subprog hasn't been processed |
6843 | | * yet) within the context of current main program: |
6844 | | * - append it at the end of main program's instructions blog; |
6845 | | * - process is recursively, while current program is put on hold; |
6846 | | * - if that subprogram calls some other not yet processes |
6847 | | * subprogram, same thing will happen recursively until |
6848 | | * there are no more unprocesses subprograms left to append |
6849 | | * and relocate. |
6850 | | */ |
6851 | 0 | if (subprog->sub_insn_off == 0) { |
6852 | 0 | err = bpf_object__append_subprog_code(obj, main_prog, subprog); |
6853 | 0 | if (err) |
6854 | 0 | return err; |
6855 | 0 | err = bpf_object__reloc_code(obj, main_prog, subprog); |
6856 | 0 | if (err) |
6857 | 0 | return err; |
6858 | 0 | } |
6859 | | |
6860 | | /* main_prog->insns memory could have been re-allocated, so |
6861 | | * calculate pointer again |
6862 | | */ |
6863 | 0 | insn = &main_prog->insns[prog->sub_insn_off + insn_idx]; |
6864 | | /* calculate correct instruction position within current main |
6865 | | * prog; each main prog can have a different set of |
6866 | | * subprograms appended (potentially in different order as |
6867 | | * well), so position of any subprog can be different for |
6868 | | * different main programs |
6869 | | */ |
6870 | 0 | insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1; |
6871 | |
|
6872 | 0 | pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n", |
6873 | 0 | prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off); |
6874 | 0 | } |
6875 | | |
6876 | 0 | return 0; |
6877 | 0 | } |
6878 | | |
6879 | | /* |
6880 | | * Relocate sub-program calls. |
6881 | | * |
6882 | | * Algorithm operates as follows. Each entry-point BPF program (referred to as |
6883 | | * main prog) is processed separately. For each subprog (non-entry functions, |
6884 | | * that can be called from either entry progs or other subprogs) gets their |
6885 | | * sub_insn_off reset to zero. This serves as indicator that this subprogram |
6886 | | * hasn't been yet appended and relocated within current main prog. Once its |
6887 | | * relocated, sub_insn_off will point at the position within current main prog |
6888 | | * where given subprog was appended. This will further be used to relocate all |
6889 | | * the call instructions jumping into this subprog. |
6890 | | * |
6891 | | * We start with main program and process all call instructions. If the call |
6892 | | * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off |
6893 | | * is zero), subprog instructions are appended at the end of main program's |
6894 | | * instruction array. Then main program is "put on hold" while we recursively |
6895 | | * process newly appended subprogram. If that subprogram calls into another |
6896 | | * subprogram that hasn't been appended, new subprogram is appended again to |
6897 | | * the *main* prog's instructions (subprog's instructions are always left |
6898 | | * untouched, as they need to be in unmodified state for subsequent main progs |
6899 | | * and subprog instructions are always sent only as part of a main prog) and |
6900 | | * the process continues recursively. Once all the subprogs called from a main |
6901 | | * prog or any of its subprogs are appended (and relocated), all their |
6902 | | * positions within finalized instructions array are known, so it's easy to |
6903 | | * rewrite call instructions with correct relative offsets, corresponding to |
6904 | | * desired target subprog. |
6905 | | * |
6906 | | * Its important to realize that some subprogs might not be called from some |
6907 | | * main prog and any of its called/used subprogs. Those will keep their |
6908 | | * subprog->sub_insn_off as zero at all times and won't be appended to current |
6909 | | * main prog and won't be relocated within the context of current main prog. |
6910 | | * They might still be used from other main progs later. |
6911 | | * |
6912 | | * Visually this process can be shown as below. Suppose we have two main |
6913 | | * programs mainA and mainB and BPF object contains three subprogs: subA, |
6914 | | * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and |
6915 | | * subC both call subB: |
6916 | | * |
6917 | | * +--------+ +-------+ |
6918 | | * | v v | |
6919 | | * +--+---+ +--+-+-+ +---+--+ |
6920 | | * | subA | | subB | | subC | |
6921 | | * +--+---+ +------+ +---+--+ |
6922 | | * ^ ^ |
6923 | | * | | |
6924 | | * +---+-------+ +------+----+ |
6925 | | * | mainA | | mainB | |
6926 | | * +-----------+ +-----------+ |
6927 | | * |
6928 | | * We'll start relocating mainA, will find subA, append it and start |
6929 | | * processing sub A recursively: |
6930 | | * |
6931 | | * +-----------+------+ |
6932 | | * | mainA | subA | |
6933 | | * +-----------+------+ |
6934 | | * |
6935 | | * At this point we notice that subB is used from subA, so we append it and |
6936 | | * relocate (there are no further subcalls from subB): |
6937 | | * |
6938 | | * +-----------+------+------+ |
6939 | | * | mainA | subA | subB | |
6940 | | * +-----------+------+------+ |
6941 | | * |
6942 | | * At this point, we relocate subA calls, then go one level up and finish with |
6943 | | * relocatin mainA calls. mainA is done. |
6944 | | * |
6945 | | * For mainB process is similar but results in different order. We start with |
6946 | | * mainB and skip subA and subB, as mainB never calls them (at least |
6947 | | * directly), but we see subC is needed, so we append and start processing it: |
6948 | | * |
6949 | | * +-----------+------+ |
6950 | | * | mainB | subC | |
6951 | | * +-----------+------+ |
6952 | | * Now we see subC needs subB, so we go back to it, append and relocate it: |
6953 | | * |
6954 | | * +-----------+------+------+ |
6955 | | * | mainB | subC | subB | |
6956 | | * +-----------+------+------+ |
6957 | | * |
6958 | | * At this point we unwind recursion, relocate calls in subC, then in mainB. |
6959 | | */ |
6960 | | static int |
6961 | | bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog) |
6962 | 0 | { |
6963 | 0 | struct bpf_program *subprog; |
6964 | 0 | int i, err; |
6965 | | |
6966 | | /* mark all subprogs as not relocated (yet) within the context of |
6967 | | * current main program |
6968 | | */ |
6969 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6970 | 0 | subprog = &obj->programs[i]; |
6971 | 0 | if (!prog_is_subprog(obj, subprog)) |
6972 | 0 | continue; |
6973 | | |
6974 | 0 | subprog->sub_insn_off = 0; |
6975 | 0 | } |
6976 | |
|
6977 | 0 | err = bpf_object__reloc_code(obj, prog, prog); |
6978 | 0 | if (err) |
6979 | 0 | return err; |
6980 | | |
6981 | 0 | return 0; |
6982 | 0 | } |
6983 | | |
6984 | | static void |
6985 | | bpf_object__free_relocs(struct bpf_object *obj) |
6986 | 0 | { |
6987 | 0 | struct bpf_program *prog; |
6988 | 0 | int i; |
6989 | | |
6990 | | /* free up relocation descriptors */ |
6991 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
6992 | 0 | prog = &obj->programs[i]; |
6993 | 0 | zfree(&prog->reloc_desc); |
6994 | 0 | prog->nr_reloc = 0; |
6995 | 0 | } |
6996 | 0 | } |
6997 | | |
6998 | | static int cmp_relocs(const void *_a, const void *_b) |
6999 | 4.71k | { |
7000 | 4.71k | const struct reloc_desc *a = _a; |
7001 | 4.71k | const struct reloc_desc *b = _b; |
7002 | | |
7003 | 4.71k | if (a->insn_idx != b->insn_idx) |
7004 | 284 | return a->insn_idx < b->insn_idx ? -1 : 1; |
7005 | | |
7006 | | /* no two relocations should have the same insn_idx, but ... */ |
7007 | 4.43k | if (a->type != b->type) |
7008 | 315 | return a->type < b->type ? -1 : 1; |
7009 | | |
7010 | 4.12k | return 0; |
7011 | 4.43k | } |
7012 | | |
7013 | | static void bpf_object__sort_relos(struct bpf_object *obj) |
7014 | 1.53k | { |
7015 | 1.53k | int i; |
7016 | | |
7017 | 9.72k | for (i = 0; i < obj->nr_programs; i++) { |
7018 | 8.19k | struct bpf_program *p = &obj->programs[i]; |
7019 | | |
7020 | 8.19k | if (!p->nr_reloc) |
7021 | 8.12k | continue; |
7022 | | |
7023 | 67 | qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs); |
7024 | 67 | } |
7025 | 1.53k | } |
7026 | | |
7027 | | static int bpf_prog_assign_exc_cb(struct bpf_object *obj, struct bpf_program *prog) |
7028 | 0 | { |
7029 | 0 | const char *str = "exception_callback:"; |
7030 | 0 | size_t pfx_len = strlen(str); |
7031 | 0 | int i, j, n; |
7032 | |
|
7033 | 0 | if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG)) |
7034 | 0 | return 0; |
7035 | | |
7036 | 0 | n = btf__type_cnt(obj->btf); |
7037 | 0 | for (i = 1; i < n; i++) { |
7038 | 0 | const char *name; |
7039 | 0 | struct btf_type *t; |
7040 | |
|
7041 | 0 | t = btf_type_by_id(obj->btf, i); |
7042 | 0 | if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1) |
7043 | 0 | continue; |
7044 | | |
7045 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
7046 | 0 | if (strncmp(name, str, pfx_len) != 0) |
7047 | 0 | continue; |
7048 | | |
7049 | 0 | t = btf_type_by_id(obj->btf, t->type); |
7050 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) { |
7051 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n", |
7052 | 0 | prog->name); |
7053 | 0 | return -EINVAL; |
7054 | 0 | } |
7055 | 0 | if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0) |
7056 | 0 | continue; |
7057 | | /* Multiple callbacks are specified for the same prog, |
7058 | | * the verifier will eventually return an error for this |
7059 | | * case, hence simply skip appending a subprog. |
7060 | | */ |
7061 | 0 | if (prog->exception_cb_idx >= 0) { |
7062 | 0 | prog->exception_cb_idx = -1; |
7063 | 0 | break; |
7064 | 0 | } |
7065 | | |
7066 | 0 | name += pfx_len; |
7067 | 0 | if (str_is_empty(name)) { |
7068 | 0 | pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n", |
7069 | 0 | prog->name); |
7070 | 0 | return -EINVAL; |
7071 | 0 | } |
7072 | | |
7073 | 0 | for (j = 0; j < obj->nr_programs; j++) { |
7074 | 0 | struct bpf_program *subprog = &obj->programs[j]; |
7075 | |
|
7076 | 0 | if (!prog_is_subprog(obj, subprog)) |
7077 | 0 | continue; |
7078 | 0 | if (strcmp(name, subprog->name) != 0) |
7079 | 0 | continue; |
7080 | | /* Enforce non-hidden, as from verifier point of |
7081 | | * view it expects global functions, whereas the |
7082 | | * mark_btf_static fixes up linkage as static. |
7083 | | */ |
7084 | 0 | if (!subprog->sym_global || subprog->mark_btf_static) { |
7085 | 0 | pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n", |
7086 | 0 | prog->name, subprog->name); |
7087 | 0 | return -EINVAL; |
7088 | 0 | } |
7089 | | /* Let's see if we already saw a static exception callback with the same name */ |
7090 | 0 | if (prog->exception_cb_idx >= 0) { |
7091 | 0 | pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n", |
7092 | 0 | prog->name, subprog->name); |
7093 | 0 | return -EINVAL; |
7094 | 0 | } |
7095 | 0 | prog->exception_cb_idx = j; |
7096 | 0 | break; |
7097 | 0 | } |
7098 | | |
7099 | 0 | if (prog->exception_cb_idx >= 0) |
7100 | 0 | continue; |
7101 | | |
7102 | 0 | pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name); |
7103 | 0 | return -ENOENT; |
7104 | 0 | } |
7105 | | |
7106 | 0 | return 0; |
7107 | 0 | } |
7108 | | |
7109 | | static struct { |
7110 | | enum bpf_prog_type prog_type; |
7111 | | const char *ctx_name; |
7112 | | } global_ctx_map[] = { |
7113 | | { BPF_PROG_TYPE_CGROUP_DEVICE, "bpf_cgroup_dev_ctx" }, |
7114 | | { BPF_PROG_TYPE_CGROUP_SKB, "__sk_buff" }, |
7115 | | { BPF_PROG_TYPE_CGROUP_SOCK, "bpf_sock" }, |
7116 | | { BPF_PROG_TYPE_CGROUP_SOCK_ADDR, "bpf_sock_addr" }, |
7117 | | { BPF_PROG_TYPE_CGROUP_SOCKOPT, "bpf_sockopt" }, |
7118 | | { BPF_PROG_TYPE_CGROUP_SYSCTL, "bpf_sysctl" }, |
7119 | | { BPF_PROG_TYPE_FLOW_DISSECTOR, "__sk_buff" }, |
7120 | | { BPF_PROG_TYPE_KPROBE, "bpf_user_pt_regs_t" }, |
7121 | | { BPF_PROG_TYPE_LWT_IN, "__sk_buff" }, |
7122 | | { BPF_PROG_TYPE_LWT_OUT, "__sk_buff" }, |
7123 | | { BPF_PROG_TYPE_LWT_SEG6LOCAL, "__sk_buff" }, |
7124 | | { BPF_PROG_TYPE_LWT_XMIT, "__sk_buff" }, |
7125 | | { BPF_PROG_TYPE_NETFILTER, "bpf_nf_ctx" }, |
7126 | | { BPF_PROG_TYPE_PERF_EVENT, "bpf_perf_event_data" }, |
7127 | | { BPF_PROG_TYPE_RAW_TRACEPOINT, "bpf_raw_tracepoint_args" }, |
7128 | | { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, "bpf_raw_tracepoint_args" }, |
7129 | | { BPF_PROG_TYPE_SCHED_ACT, "__sk_buff" }, |
7130 | | { BPF_PROG_TYPE_SCHED_CLS, "__sk_buff" }, |
7131 | | { BPF_PROG_TYPE_SK_LOOKUP, "bpf_sk_lookup" }, |
7132 | | { BPF_PROG_TYPE_SK_MSG, "sk_msg_md" }, |
7133 | | { BPF_PROG_TYPE_SK_REUSEPORT, "sk_reuseport_md" }, |
7134 | | { BPF_PROG_TYPE_SK_SKB, "__sk_buff" }, |
7135 | | { BPF_PROG_TYPE_SOCK_OPS, "bpf_sock_ops" }, |
7136 | | { BPF_PROG_TYPE_SOCKET_FILTER, "__sk_buff" }, |
7137 | | { BPF_PROG_TYPE_XDP, "xdp_md" }, |
7138 | | /* all other program types don't have "named" context structs */ |
7139 | | }; |
7140 | | |
7141 | | /* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef, |
7142 | | * for below __builtin_types_compatible_p() checks; |
7143 | | * with this approach we don't need any extra arch-specific #ifdef guards |
7144 | | */ |
7145 | | struct pt_regs; |
7146 | | struct user_pt_regs; |
7147 | | struct user_regs_struct; |
7148 | | |
7149 | | static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog, |
7150 | | const char *subprog_name, int arg_idx, |
7151 | | int arg_type_id, const char *ctx_name) |
7152 | 0 | { |
7153 | 0 | const struct btf_type *t; |
7154 | 0 | const char *tname; |
7155 | | |
7156 | | /* check if existing parameter already matches verifier expectations */ |
7157 | 0 | t = skip_mods_and_typedefs(btf, arg_type_id, NULL); |
7158 | 0 | if (!btf_is_ptr(t)) |
7159 | 0 | goto out_warn; |
7160 | | |
7161 | | /* typedef bpf_user_pt_regs_t is a special PITA case, valid for kprobe |
7162 | | * and perf_event programs, so check this case early on and forget |
7163 | | * about it for subsequent checks |
7164 | | */ |
7165 | 0 | while (btf_is_mod(t)) |
7166 | 0 | t = btf__type_by_id(btf, t->type); |
7167 | 0 | if (btf_is_typedef(t) && |
7168 | 0 | (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) { |
7169 | 0 | tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; |
7170 | 0 | if (strcmp(tname, "bpf_user_pt_regs_t") == 0) |
7171 | 0 | return false; /* canonical type for kprobe/perf_event */ |
7172 | 0 | } |
7173 | | |
7174 | | /* now we can ignore typedefs moving forward */ |
7175 | 0 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
7176 | | |
7177 | | /* if it's `void *`, definitely fix up BTF info */ |
7178 | 0 | if (btf_is_void(t)) |
7179 | 0 | return true; |
7180 | | |
7181 | | /* if it's already proper canonical type, no need to fix up */ |
7182 | 0 | tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>"; |
7183 | 0 | if (btf_is_struct(t) && strcmp(tname, ctx_name) == 0) |
7184 | 0 | return false; |
7185 | | |
7186 | | /* special cases */ |
7187 | 0 | switch (prog->type) { |
7188 | 0 | case BPF_PROG_TYPE_KPROBE: |
7189 | | /* `struct pt_regs *` is expected, but we need to fix up */ |
7190 | 0 | if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0) |
7191 | 0 | return true; |
7192 | 0 | break; |
7193 | 0 | case BPF_PROG_TYPE_PERF_EVENT: |
7194 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) && |
7195 | 0 | btf_is_struct(t) && strcmp(tname, "pt_regs") == 0) |
7196 | 0 | return true; |
7197 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) && |
7198 | 0 | btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0) |
7199 | 0 | return true; |
7200 | 0 | if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) && |
7201 | 0 | btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0) |
7202 | 0 | return true; |
7203 | 0 | break; |
7204 | 0 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
7205 | 0 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
7206 | | /* allow u64* as ctx */ |
7207 | 0 | if (btf_is_int(t) && t->size == 8) |
7208 | 0 | return true; |
7209 | 0 | break; |
7210 | 0 | default: |
7211 | 0 | break; |
7212 | 0 | } |
7213 | | |
7214 | 0 | out_warn: |
7215 | 0 | pr_warn("prog '%s': subprog '%s' arg#%d is expected to be of `struct %s *` type\n", |
7216 | 0 | prog->name, subprog_name, arg_idx, ctx_name); |
7217 | 0 | return false; |
7218 | 0 | } |
7219 | | |
7220 | | static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_program *prog) |
7221 | 0 | { |
7222 | 0 | int fn_id, fn_proto_id, ret_type_id, orig_proto_id; |
7223 | 0 | int i, err, arg_cnt, fn_name_off, linkage; |
7224 | 0 | struct btf_type *fn_t, *fn_proto_t, *t; |
7225 | 0 | struct btf_param *p; |
7226 | | |
7227 | | /* caller already validated FUNC -> FUNC_PROTO validity */ |
7228 | 0 | fn_t = btf_type_by_id(btf, orig_fn_id); |
7229 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
7230 | | |
7231 | | /* Note that each btf__add_xxx() operation invalidates |
7232 | | * all btf_type and string pointers, so we need to be |
7233 | | * very careful when cloning BTF types. BTF type |
7234 | | * pointers have to be always refetched. And to avoid |
7235 | | * problems with invalidated string pointers, we |
7236 | | * add empty strings initially, then just fix up |
7237 | | * name_off offsets in place. Offsets are stable for |
7238 | | * existing strings, so that works out. |
7239 | | */ |
7240 | 0 | fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */ |
7241 | 0 | linkage = btf_func_linkage(fn_t); |
7242 | 0 | orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */ |
7243 | 0 | ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */ |
7244 | 0 | arg_cnt = btf_vlen(fn_proto_t); |
7245 | | |
7246 | | /* clone FUNC_PROTO and its params */ |
7247 | 0 | fn_proto_id = btf__add_func_proto(btf, ret_type_id); |
7248 | 0 | if (fn_proto_id < 0) |
7249 | 0 | return -EINVAL; |
7250 | | |
7251 | 0 | for (i = 0; i < arg_cnt; i++) { |
7252 | 0 | int name_off; |
7253 | | |
7254 | | /* copy original parameter data */ |
7255 | 0 | t = btf_type_by_id(btf, orig_proto_id); |
7256 | 0 | p = &btf_params(t)[i]; |
7257 | 0 | name_off = p->name_off; |
7258 | |
|
7259 | 0 | err = btf__add_func_param(btf, "", p->type); |
7260 | 0 | if (err) |
7261 | 0 | return err; |
7262 | | |
7263 | 0 | fn_proto_t = btf_type_by_id(btf, fn_proto_id); |
7264 | 0 | p = &btf_params(fn_proto_t)[i]; |
7265 | 0 | p->name_off = name_off; /* use remembered str offset */ |
7266 | 0 | } |
7267 | | |
7268 | | /* clone FUNC now, btf__add_func() enforces non-empty name, so use |
7269 | | * entry program's name as a placeholder, which we replace immediately |
7270 | | * with original name_off |
7271 | | */ |
7272 | 0 | fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id); |
7273 | 0 | if (fn_id < 0) |
7274 | 0 | return -EINVAL; |
7275 | | |
7276 | 0 | fn_t = btf_type_by_id(btf, fn_id); |
7277 | 0 | fn_t->name_off = fn_name_off; /* reuse original string */ |
7278 | |
|
7279 | 0 | return fn_id; |
7280 | 0 | } |
7281 | | |
7282 | | /* Check if main program or global subprog's function prototype has `arg:ctx` |
7283 | | * argument tags, and, if necessary, substitute correct type to match what BPF |
7284 | | * verifier would expect, taking into account specific program type. This |
7285 | | * allows to support __arg_ctx tag transparently on old kernels that don't yet |
7286 | | * have a native support for it in the verifier, making user's life much |
7287 | | * easier. |
7288 | | */ |
7289 | | static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_program *prog) |
7290 | 0 | { |
7291 | 0 | const char *ctx_name = NULL, *ctx_tag = "arg:ctx", *fn_name; |
7292 | 0 | struct bpf_func_info_min *func_rec; |
7293 | 0 | struct btf_type *fn_t, *fn_proto_t; |
7294 | 0 | struct btf *btf = obj->btf; |
7295 | 0 | const struct btf_type *t; |
7296 | 0 | struct btf_param *p; |
7297 | 0 | int ptr_id = 0, struct_id, tag_id, orig_fn_id; |
7298 | 0 | int i, n, arg_idx, arg_cnt, err, rec_idx; |
7299 | 0 | int *orig_ids; |
7300 | | |
7301 | | /* no .BTF.ext, no problem */ |
7302 | 0 | if (!obj->btf_ext || !prog->func_info) |
7303 | 0 | return 0; |
7304 | | |
7305 | | /* don't do any fix ups if kernel natively supports __arg_ctx */ |
7306 | 0 | if (kernel_supports(obj, FEAT_ARG_CTX_TAG)) |
7307 | 0 | return 0; |
7308 | | |
7309 | | /* some BPF program types just don't have named context structs, so |
7310 | | * this fallback mechanism doesn't work for them |
7311 | | */ |
7312 | 0 | for (i = 0; i < ARRAY_SIZE(global_ctx_map); i++) { |
7313 | 0 | if (global_ctx_map[i].prog_type != prog->type) |
7314 | 0 | continue; |
7315 | 0 | ctx_name = global_ctx_map[i].ctx_name; |
7316 | 0 | break; |
7317 | 0 | } |
7318 | 0 | if (!ctx_name) |
7319 | 0 | return 0; |
7320 | | |
7321 | | /* remember original func BTF IDs to detect if we already cloned them */ |
7322 | 0 | orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids)); |
7323 | 0 | if (!orig_ids) |
7324 | 0 | return -ENOMEM; |
7325 | 0 | for (i = 0; i < prog->func_info_cnt; i++) { |
7326 | 0 | func_rec = prog->func_info + prog->func_info_rec_size * i; |
7327 | 0 | orig_ids[i] = func_rec->type_id; |
7328 | 0 | } |
7329 | | |
7330 | | /* go through each DECL_TAG with "arg:ctx" and see if it points to one |
7331 | | * of our subprogs; if yes and subprog is global and needs adjustment, |
7332 | | * clone and adjust FUNC -> FUNC_PROTO combo |
7333 | | */ |
7334 | 0 | for (i = 1, n = btf__type_cnt(btf); i < n; i++) { |
7335 | | /* only DECL_TAG with "arg:ctx" value are interesting */ |
7336 | 0 | t = btf__type_by_id(btf, i); |
7337 | 0 | if (!btf_is_decl_tag(t)) |
7338 | 0 | continue; |
7339 | 0 | if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0) |
7340 | 0 | continue; |
7341 | | |
7342 | | /* only global funcs need adjustment, if at all */ |
7343 | 0 | orig_fn_id = t->type; |
7344 | 0 | fn_t = btf_type_by_id(btf, orig_fn_id); |
7345 | 0 | if (!btf_is_func(fn_t) || btf_func_linkage(fn_t) != BTF_FUNC_GLOBAL) |
7346 | 0 | continue; |
7347 | | |
7348 | | /* sanity check FUNC -> FUNC_PROTO chain, just in case */ |
7349 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
7350 | 0 | if (!fn_proto_t || !btf_is_func_proto(fn_proto_t)) |
7351 | 0 | continue; |
7352 | | |
7353 | | /* find corresponding func_info record */ |
7354 | 0 | func_rec = NULL; |
7355 | 0 | for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) { |
7356 | 0 | if (orig_ids[rec_idx] == t->type) { |
7357 | 0 | func_rec = prog->func_info + prog->func_info_rec_size * rec_idx; |
7358 | 0 | break; |
7359 | 0 | } |
7360 | 0 | } |
7361 | | /* current main program doesn't call into this subprog */ |
7362 | 0 | if (!func_rec) |
7363 | 0 | continue; |
7364 | | |
7365 | | /* some more sanity checking of DECL_TAG */ |
7366 | 0 | arg_cnt = btf_vlen(fn_proto_t); |
7367 | 0 | arg_idx = btf_decl_tag(t)->component_idx; |
7368 | 0 | if (arg_idx < 0 || arg_idx >= arg_cnt) |
7369 | 0 | continue; |
7370 | | |
7371 | | /* check if we should fix up argument type */ |
7372 | 0 | p = &btf_params(fn_proto_t)[arg_idx]; |
7373 | 0 | fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>"; |
7374 | 0 | if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name)) |
7375 | 0 | continue; |
7376 | | |
7377 | | /* clone fn/fn_proto, unless we already did it for another arg */ |
7378 | 0 | if (func_rec->type_id == orig_fn_id) { |
7379 | 0 | int fn_id; |
7380 | |
|
7381 | 0 | fn_id = clone_func_btf_info(btf, orig_fn_id, prog); |
7382 | 0 | if (fn_id < 0) { |
7383 | 0 | err = fn_id; |
7384 | 0 | goto err_out; |
7385 | 0 | } |
7386 | | |
7387 | | /* point func_info record to a cloned FUNC type */ |
7388 | 0 | func_rec->type_id = fn_id; |
7389 | 0 | } |
7390 | | |
7391 | | /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument; |
7392 | | * we do it just once per main BPF program, as all global |
7393 | | * funcs share the same program type, so need only PTR -> |
7394 | | * STRUCT type chain |
7395 | | */ |
7396 | 0 | if (ptr_id == 0) { |
7397 | 0 | struct_id = btf__add_struct(btf, ctx_name, 0); |
7398 | 0 | ptr_id = btf__add_ptr(btf, struct_id); |
7399 | 0 | if (ptr_id < 0 || struct_id < 0) { |
7400 | 0 | err = -EINVAL; |
7401 | 0 | goto err_out; |
7402 | 0 | } |
7403 | 0 | } |
7404 | | |
7405 | | /* for completeness, clone DECL_TAG and point it to cloned param */ |
7406 | 0 | tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx); |
7407 | 0 | if (tag_id < 0) { |
7408 | 0 | err = -EINVAL; |
7409 | 0 | goto err_out; |
7410 | 0 | } |
7411 | | |
7412 | | /* all the BTF manipulations invalidated pointers, refetch them */ |
7413 | 0 | fn_t = btf_type_by_id(btf, func_rec->type_id); |
7414 | 0 | fn_proto_t = btf_type_by_id(btf, fn_t->type); |
7415 | | |
7416 | | /* fix up type ID pointed to by param */ |
7417 | 0 | p = &btf_params(fn_proto_t)[arg_idx]; |
7418 | 0 | p->type = ptr_id; |
7419 | 0 | } |
7420 | | |
7421 | 0 | free(orig_ids); |
7422 | 0 | return 0; |
7423 | 0 | err_out: |
7424 | 0 | free(orig_ids); |
7425 | 0 | return err; |
7426 | 0 | } |
7427 | | |
7428 | | static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) |
7429 | 0 | { |
7430 | 0 | struct bpf_program *prog; |
7431 | 0 | size_t i, j; |
7432 | 0 | int err; |
7433 | |
|
7434 | 0 | if (obj->btf_ext) { |
7435 | 0 | err = bpf_object__relocate_core(obj, targ_btf_path); |
7436 | 0 | if (err) { |
7437 | 0 | pr_warn("failed to perform CO-RE relocations: %s\n", |
7438 | 0 | errstr(err)); |
7439 | 0 | return err; |
7440 | 0 | } |
7441 | 0 | bpf_object__sort_relos(obj); |
7442 | 0 | } |
7443 | | |
7444 | | /* place globals at the end of the arena (if supported) */ |
7445 | 0 | if (obj->arena_map_idx >= 0 && kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)) { |
7446 | 0 | struct bpf_map *arena_map = &obj->maps[obj->arena_map_idx]; |
7447 | |
|
7448 | 0 | obj->arena_data_off = bpf_map_mmap_sz(arena_map) - |
7449 | 0 | roundup(obj->arena_data_sz, sysconf(_SC_PAGE_SIZE)); |
7450 | 0 | } |
7451 | | |
7452 | | /* Before relocating calls pre-process relocations and mark |
7453 | | * few ld_imm64 instructions that points to subprogs. |
7454 | | * Otherwise bpf_object__reloc_code() later would have to consider |
7455 | | * all ld_imm64 insns as relocation candidates. That would |
7456 | | * reduce relocation speed, since amount of find_prog_insn_relo() |
7457 | | * would increase and most of them will fail to find a relo. |
7458 | | */ |
7459 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7460 | 0 | prog = &obj->programs[i]; |
7461 | 0 | for (j = 0; j < prog->nr_reloc; j++) { |
7462 | 0 | struct reloc_desc *relo = &prog->reloc_desc[j]; |
7463 | 0 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
7464 | | |
7465 | | /* mark the insn, so it's recognized by insn_is_pseudo_func() */ |
7466 | 0 | if (relo->type == RELO_SUBPROG_ADDR) |
7467 | 0 | insn[0].src_reg = BPF_PSEUDO_FUNC; |
7468 | 0 | } |
7469 | 0 | } |
7470 | | |
7471 | | /* relocate subprogram calls and append used subprograms to main |
7472 | | * programs; each copy of subprogram code needs to be relocated |
7473 | | * differently for each main program, because its code location might |
7474 | | * have changed. |
7475 | | * Append subprog relos to main programs to allow data relos to be |
7476 | | * processed after text is completely relocated. |
7477 | | */ |
7478 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7479 | 0 | prog = &obj->programs[i]; |
7480 | | /* sub-program's sub-calls are relocated within the context of |
7481 | | * its main program only |
7482 | | */ |
7483 | 0 | if (prog_is_subprog(obj, prog)) |
7484 | 0 | continue; |
7485 | 0 | if (!prog->autoload) |
7486 | 0 | continue; |
7487 | | |
7488 | 0 | err = bpf_object__relocate_calls(obj, prog); |
7489 | 0 | if (err) { |
7490 | 0 | pr_warn("prog '%s': failed to relocate calls: %s\n", |
7491 | 0 | prog->name, errstr(err)); |
7492 | 0 | return err; |
7493 | 0 | } |
7494 | | |
7495 | 0 | err = bpf_prog_assign_exc_cb(obj, prog); |
7496 | 0 | if (err) |
7497 | 0 | return err; |
7498 | | /* Now, also append exception callback if it has not been done already. */ |
7499 | 0 | if (prog->exception_cb_idx >= 0) { |
7500 | 0 | struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx]; |
7501 | | |
7502 | | /* Calling exception callback directly is disallowed, which the |
7503 | | * verifier will reject later. In case it was processed already, |
7504 | | * we can skip this step, otherwise for all other valid cases we |
7505 | | * have to append exception callback now. |
7506 | | */ |
7507 | 0 | if (subprog->sub_insn_off == 0) { |
7508 | 0 | err = bpf_object__append_subprog_code(obj, prog, subprog); |
7509 | 0 | if (err) |
7510 | 0 | return err; |
7511 | 0 | err = bpf_object__reloc_code(obj, prog, subprog); |
7512 | 0 | if (err) |
7513 | 0 | return err; |
7514 | 0 | } |
7515 | 0 | } |
7516 | 0 | } |
7517 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
7518 | 0 | prog = &obj->programs[i]; |
7519 | 0 | if (prog_is_subprog(obj, prog)) |
7520 | 0 | continue; |
7521 | 0 | if (!prog->autoload) |
7522 | 0 | continue; |
7523 | | |
7524 | | /* Process data relos for main programs */ |
7525 | 0 | err = bpf_object__relocate_data(obj, prog); |
7526 | 0 | if (err) { |
7527 | 0 | pr_warn("prog '%s': failed to relocate data references: %s\n", |
7528 | 0 | prog->name, errstr(err)); |
7529 | 0 | return err; |
7530 | 0 | } |
7531 | | |
7532 | | /* Fix up .BTF.ext information, if necessary */ |
7533 | 0 | err = bpf_program_fixup_func_info(obj, prog); |
7534 | 0 | if (err) { |
7535 | 0 | pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %s\n", |
7536 | 0 | prog->name, errstr(err)); |
7537 | 0 | return err; |
7538 | 0 | } |
7539 | 0 | } |
7540 | | |
7541 | 0 | return 0; |
7542 | 0 | } |
7543 | | |
7544 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
7545 | | Elf64_Shdr *shdr, Elf_Data *data); |
7546 | | |
7547 | | static int bpf_object__collect_map_relos(struct bpf_object *obj, |
7548 | | Elf64_Shdr *shdr, Elf_Data *data) |
7549 | 38 | { |
7550 | 38 | const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *); |
7551 | 38 | int i, j, nrels, new_sz; |
7552 | 38 | const struct btf_var_secinfo *vi = NULL; |
7553 | 38 | const struct btf_type *sec, *var, *def; |
7554 | 38 | struct bpf_map *map = NULL, *targ_map = NULL; |
7555 | 38 | struct bpf_program *targ_prog = NULL; |
7556 | 38 | bool is_prog_array, is_map_in_map; |
7557 | 38 | const struct btf_member *member; |
7558 | 38 | const char *name, *mname, *type; |
7559 | 38 | unsigned int moff; |
7560 | 38 | Elf64_Sym *sym; |
7561 | 38 | Elf64_Rel *rel; |
7562 | 38 | void *tmp; |
7563 | | |
7564 | 38 | if (!obj->efile.btf_maps_sec_btf_id || !obj->btf) |
7565 | 0 | return -EINVAL; |
7566 | 38 | sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id); |
7567 | 38 | if (!sec) |
7568 | 0 | return -EINVAL; |
7569 | | |
7570 | 38 | nrels = shdr->sh_size / shdr->sh_entsize; |
7571 | 38 | for (i = 0; i < nrels; i++) { |
7572 | 30 | rel = elf_rel_by_idx(data, i); |
7573 | 30 | if (!rel) { |
7574 | 0 | pr_warn(".maps relo #%d: failed to get ELF relo\n", i); |
7575 | 0 | return -LIBBPF_ERRNO__FORMAT; |
7576 | 0 | } |
7577 | | |
7578 | 30 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
7579 | 30 | if (!sym) { |
7580 | 21 | pr_warn(".maps relo #%d: symbol %zx not found\n", |
7581 | 21 | i, (size_t)ELF64_R_SYM(rel->r_info)); |
7582 | 21 | return -LIBBPF_ERRNO__FORMAT; |
7583 | 21 | } |
7584 | 9 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
7585 | | |
7586 | 9 | pr_debug(".maps relo #%d: for %zd value %zu rel->r_offset %zu name %u ('%s')\n", |
7587 | 9 | i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value, |
7588 | 9 | (size_t)rel->r_offset, sym->st_name, name); |
7589 | | |
7590 | 24 | for (j = 0; j < obj->nr_maps; j++) { |
7591 | 15 | map = &obj->maps[j]; |
7592 | 15 | if (map->sec_idx != obj->efile.btf_maps_shndx) |
7593 | 15 | continue; |
7594 | | |
7595 | 0 | vi = btf_var_secinfos(sec) + map->btf_var_idx; |
7596 | 0 | if (vi->offset <= rel->r_offset && |
7597 | 0 | rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size) |
7598 | 0 | break; |
7599 | 0 | } |
7600 | 9 | if (j == obj->nr_maps) { |
7601 | 9 | pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n", |
7602 | 9 | i, name, (size_t)rel->r_offset); |
7603 | 9 | return -EINVAL; |
7604 | 9 | } |
7605 | | |
7606 | 0 | is_map_in_map = bpf_map_type__is_map_in_map(map->def.type); |
7607 | 0 | is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY; |
7608 | 0 | type = is_map_in_map ? "map" : "prog"; |
7609 | 0 | if (is_map_in_map) { |
7610 | 0 | if (sym->st_shndx != obj->efile.btf_maps_shndx) { |
7611 | 0 | pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n", |
7612 | 0 | i, name); |
7613 | 0 | return -LIBBPF_ERRNO__RELOC; |
7614 | 0 | } |
7615 | 0 | if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS && |
7616 | 0 | map->def.key_size != sizeof(int)) { |
7617 | 0 | pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n", |
7618 | 0 | i, map->name, sizeof(int)); |
7619 | 0 | return -EINVAL; |
7620 | 0 | } |
7621 | 0 | targ_map = bpf_object__find_map_by_name(obj, name); |
7622 | 0 | if (!targ_map) { |
7623 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n", |
7624 | 0 | i, name); |
7625 | 0 | return -ESRCH; |
7626 | 0 | } |
7627 | 0 | } else if (is_prog_array) { |
7628 | 0 | targ_prog = bpf_object__find_program_by_name(obj, name); |
7629 | 0 | if (!targ_prog) { |
7630 | 0 | pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n", |
7631 | 0 | i, name); |
7632 | 0 | return -ESRCH; |
7633 | 0 | } |
7634 | 0 | if (targ_prog->sec_idx != sym->st_shndx || |
7635 | 0 | targ_prog->sec_insn_off * 8 != sym->st_value || |
7636 | 0 | prog_is_subprog(obj, targ_prog)) { |
7637 | 0 | pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n", |
7638 | 0 | i, name); |
7639 | 0 | return -LIBBPF_ERRNO__RELOC; |
7640 | 0 | } |
7641 | 0 | } else { |
7642 | 0 | return -EINVAL; |
7643 | 0 | } |
7644 | | |
7645 | 0 | var = btf__type_by_id(obj->btf, vi->type); |
7646 | 0 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
7647 | 0 | if (btf_vlen(def) == 0) |
7648 | 0 | return -EINVAL; |
7649 | 0 | member = btf_members(def) + btf_vlen(def) - 1; |
7650 | 0 | mname = btf__name_by_offset(obj->btf, member->name_off); |
7651 | 0 | if (strcmp(mname, "values")) |
7652 | 0 | return -EINVAL; |
7653 | | |
7654 | 0 | moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8; |
7655 | 0 | if (rel->r_offset - vi->offset < moff) |
7656 | 0 | return -EINVAL; |
7657 | | |
7658 | 0 | moff = rel->r_offset - vi->offset - moff; |
7659 | | /* here we use BPF pointer size, which is always 64 bit, as we |
7660 | | * are parsing ELF that was built for BPF target |
7661 | | */ |
7662 | 0 | if (moff % bpf_ptr_sz) |
7663 | 0 | return -EINVAL; |
7664 | 0 | moff /= bpf_ptr_sz; |
7665 | 0 | if (moff >= map->init_slots_sz) { |
7666 | 0 | new_sz = moff + 1; |
7667 | 0 | tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz); |
7668 | 0 | if (!tmp) |
7669 | 0 | return -ENOMEM; |
7670 | 0 | map->init_slots = tmp; |
7671 | 0 | memset(map->init_slots + map->init_slots_sz, 0, |
7672 | 0 | (new_sz - map->init_slots_sz) * host_ptr_sz); |
7673 | 0 | map->init_slots_sz = new_sz; |
7674 | 0 | } |
7675 | 0 | map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog; |
7676 | |
|
7677 | 0 | pr_debug(".maps relo #%d: map '%s' slot [%u] points to %s '%s'\n", |
7678 | 0 | i, map->name, moff, type, name); |
7679 | 0 | } |
7680 | | |
7681 | 8 | return 0; |
7682 | 38 | } |
7683 | | |
7684 | | static int bpf_object__collect_relos(struct bpf_object *obj) |
7685 | 2.07k | { |
7686 | 2.07k | int i, err; |
7687 | | |
7688 | 20.8k | for (i = 0; i < obj->efile.sec_cnt; i++) { |
7689 | 19.2k | struct elf_sec_desc *sec_desc = &obj->efile.secs[i]; |
7690 | 19.2k | Elf64_Shdr *shdr; |
7691 | 19.2k | Elf_Data *data; |
7692 | 19.2k | int idx; |
7693 | | |
7694 | 19.2k | if (sec_desc->sec_type != SEC_RELO) |
7695 | 18.2k | continue; |
7696 | | |
7697 | 1.08k | shdr = sec_desc->shdr; |
7698 | 1.08k | data = sec_desc->data; |
7699 | 1.08k | idx = shdr->sh_info; |
7700 | | |
7701 | 1.08k | if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) { |
7702 | 0 | pr_warn("internal error at %d\n", __LINE__); |
7703 | 0 | return -LIBBPF_ERRNO__INTERNAL; |
7704 | 0 | } |
7705 | | |
7706 | 1.08k | if (obj->efile.secs[idx].sec_type == SEC_ST_OPS) |
7707 | 23 | err = bpf_object__collect_st_ops_relos(obj, shdr, data); |
7708 | 1.06k | else if (idx == obj->efile.btf_maps_shndx) |
7709 | 38 | err = bpf_object__collect_map_relos(obj, shdr, data); |
7710 | 1.02k | else |
7711 | 1.02k | err = bpf_object__collect_prog_relos(obj, shdr, data); |
7712 | 1.08k | if (err) |
7713 | 541 | return err; |
7714 | 1.08k | } |
7715 | | |
7716 | 1.53k | bpf_object__sort_relos(obj); |
7717 | 1.53k | return 0; |
7718 | 2.07k | } |
7719 | | |
7720 | | static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id) |
7721 | 0 | { |
7722 | 0 | if (BPF_CLASS(insn->code) == BPF_JMP && |
7723 | 0 | BPF_OP(insn->code) == BPF_CALL && |
7724 | 0 | BPF_SRC(insn->code) == BPF_K && |
7725 | 0 | insn->src_reg == 0 && |
7726 | 0 | insn->dst_reg == 0) { |
7727 | 0 | *func_id = insn->imm; |
7728 | 0 | return true; |
7729 | 0 | } |
7730 | 0 | return false; |
7731 | 0 | } |
7732 | | |
7733 | | static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog) |
7734 | 0 | { |
7735 | 0 | struct bpf_insn *insn = prog->insns; |
7736 | 0 | enum bpf_func_id func_id; |
7737 | 0 | int i; |
7738 | |
|
7739 | 0 | if (obj->gen_loader) |
7740 | 0 | return 0; |
7741 | | |
7742 | 0 | for (i = 0; i < prog->insns_cnt; i++, insn++) { |
7743 | 0 | if (!insn_is_helper_call(insn, &func_id)) |
7744 | 0 | continue; |
7745 | | |
7746 | | /* on kernels that don't yet support |
7747 | | * bpf_probe_read_{kernel,user}[_str] helpers, fall back |
7748 | | * to bpf_probe_read() which works well for old kernels |
7749 | | */ |
7750 | 0 | switch (func_id) { |
7751 | 0 | case BPF_FUNC_probe_read_kernel: |
7752 | 0 | case BPF_FUNC_probe_read_user: |
7753 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
7754 | 0 | insn->imm = BPF_FUNC_probe_read; |
7755 | 0 | break; |
7756 | 0 | case BPF_FUNC_probe_read_kernel_str: |
7757 | 0 | case BPF_FUNC_probe_read_user_str: |
7758 | 0 | if (!kernel_supports(obj, FEAT_PROBE_READ_KERN)) |
7759 | 0 | insn->imm = BPF_FUNC_probe_read_str; |
7760 | 0 | break; |
7761 | 0 | default: |
7762 | 0 | break; |
7763 | 0 | } |
7764 | 0 | } |
7765 | 0 | return 0; |
7766 | 0 | } |
7767 | | |
7768 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
7769 | | int *btf_obj_fd, int *btf_type_id); |
7770 | | |
7771 | | static inline bool is_tracing_multi(enum bpf_attach_type type) |
7772 | 0 | { |
7773 | 0 | return type == BPF_TRACE_FENTRY_MULTI || type == BPF_TRACE_FEXIT_MULTI || |
7774 | 0 | type == BPF_TRACE_FSESSION_MULTI; |
7775 | 0 | } |
7776 | | |
7777 | | static const struct module_btf *find_attach_module(struct bpf_object *obj, const char *attach) |
7778 | 0 | { |
7779 | 0 | const char *sep, *mod_name = NULL; |
7780 | 0 | int i, mod_len, err; |
7781 | | |
7782 | | /* |
7783 | | * We expect attach string in the form of either |
7784 | | * - function_pattern or |
7785 | | * - <module>:function_pattern |
7786 | | */ |
7787 | 0 | sep = strchr(attach, ':'); |
7788 | 0 | if (sep) { |
7789 | 0 | mod_name = attach; |
7790 | 0 | mod_len = sep - mod_name; |
7791 | 0 | } |
7792 | 0 | if (!mod_name) |
7793 | 0 | return NULL; |
7794 | | |
7795 | 0 | err = load_module_btfs(obj); |
7796 | 0 | if (err) |
7797 | 0 | return NULL; |
7798 | | |
7799 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
7800 | 0 | const struct module_btf *mod = &obj->btf_modules[i]; |
7801 | |
|
7802 | 0 | if (strncmp(mod->name, mod_name, mod_len) == 0 && mod->name[mod_len] == '\0') |
7803 | 0 | return mod; |
7804 | 0 | } |
7805 | 0 | return NULL; |
7806 | 0 | } |
7807 | | |
7808 | | static int tracing_multi_mod_fd(struct bpf_program *prog, int *btf_obj_fd) |
7809 | 0 | { |
7810 | 0 | const char *attach_name, *sep; |
7811 | 0 | const struct module_btf *mod; |
7812 | |
|
7813 | 0 | *btf_obj_fd = 0; |
7814 | 0 | attach_name = strchr(prog->sec_name, '/'); |
7815 | | |
7816 | | /* Program with no details in spec, using kernel btf. */ |
7817 | 0 | if (!attach_name) |
7818 | 0 | return 0; |
7819 | | |
7820 | | /* Program with no module section, using kernel btf. */ |
7821 | 0 | sep = strchr(++attach_name, ':'); |
7822 | 0 | if (!sep) |
7823 | 0 | return 0; |
7824 | | |
7825 | | /* Program with module specified, get its btf fd. */ |
7826 | 0 | mod = find_attach_module(prog->obj, attach_name); |
7827 | 0 | if (!mod) |
7828 | 0 | return -EINVAL; |
7829 | | |
7830 | 0 | *btf_obj_fd = mod->fd; |
7831 | 0 | return 0; |
7832 | 0 | } |
7833 | | |
7834 | | /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */ |
7835 | | static int libbpf_prepare_prog_load(struct bpf_program *prog, |
7836 | | struct bpf_prog_load_opts *opts, long cookie) |
7837 | 0 | { |
7838 | 0 | enum sec_def_flags def = cookie; |
7839 | | |
7840 | | /* old kernels might not support specifying expected_attach_type */ |
7841 | 0 | if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE)) |
7842 | 0 | opts->expected_attach_type = 0; |
7843 | |
|
7844 | 0 | if (def & SEC_SLEEPABLE) |
7845 | 0 | opts->prog_flags |= BPF_F_SLEEPABLE; |
7846 | |
|
7847 | 0 | if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) |
7848 | 0 | opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; |
7849 | | |
7850 | | /* special check for usdt to use uprobe_multi link */ |
7851 | 0 | if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) { |
7852 | | /* for BPF_TRACE_UPROBE_MULTI, user might want to query expected_attach_type |
7853 | | * in prog, and expected_attach_type we set in kernel is from opts, so we |
7854 | | * update both. |
7855 | | */ |
7856 | 0 | prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; |
7857 | 0 | opts->expected_attach_type = BPF_TRACE_UPROBE_MULTI; |
7858 | 0 | } |
7859 | |
|
7860 | 0 | if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { |
7861 | 0 | int btf_obj_fd = 0, btf_type_id = 0, err; |
7862 | 0 | const char *attach_name; |
7863 | |
|
7864 | 0 | attach_name = strchr(prog->sec_name, '/'); |
7865 | 0 | if (!attach_name) { |
7866 | | /* if BPF program is annotated with just SEC("fentry") |
7867 | | * (or similar) without declaratively specifying |
7868 | | * target, then it is expected that target will be |
7869 | | * specified with bpf_program__set_attach_target() at |
7870 | | * runtime before BPF object load step. If not, then |
7871 | | * there is nothing to load into the kernel as BPF |
7872 | | * verifier won't be able to validate BPF program |
7873 | | * correctness anyways. |
7874 | | */ |
7875 | 0 | pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n", |
7876 | 0 | prog->name); |
7877 | 0 | return -EINVAL; |
7878 | 0 | } |
7879 | 0 | attach_name++; /* skip over / */ |
7880 | |
|
7881 | 0 | err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id); |
7882 | 0 | if (err) |
7883 | 0 | return err; |
7884 | | |
7885 | | /* cache resolved BTF FD and BTF type ID in the prog */ |
7886 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
7887 | 0 | prog->attach_btf_id = btf_type_id; |
7888 | | |
7889 | | /* but by now libbpf common logic is not utilizing |
7890 | | * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because |
7891 | | * this callback is called after opts were populated by |
7892 | | * libbpf, so this callback has to update opts explicitly here |
7893 | | */ |
7894 | 0 | opts->attach_btf_obj_fd = btf_obj_fd; |
7895 | 0 | opts->attach_btf_id = btf_type_id; |
7896 | 0 | } |
7897 | | |
7898 | 0 | if (is_tracing_multi(prog->expected_attach_type)) { |
7899 | 0 | int err, btf_obj_fd = 0; |
7900 | |
|
7901 | 0 | err = tracing_multi_mod_fd(prog, &btf_obj_fd); |
7902 | 0 | if (err < 0) |
7903 | 0 | return err; |
7904 | | |
7905 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
7906 | 0 | opts->attach_btf_obj_fd = btf_obj_fd; |
7907 | 0 | } |
7908 | | |
7909 | 0 | return 0; |
7910 | 0 | } |
7911 | | |
7912 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz); |
7913 | | |
7914 | | static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog, |
7915 | | struct bpf_insn *insns, int insns_cnt, |
7916 | | const char *license, __u32 kern_version, int *prog_fd) |
7917 | 0 | { |
7918 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, load_attr); |
7919 | 0 | const char *prog_name = NULL; |
7920 | 0 | size_t log_buf_size = 0; |
7921 | 0 | char *log_buf = NULL, *tmp; |
7922 | 0 | bool own_log_buf = true; |
7923 | 0 | __u32 log_level = prog->log_level; |
7924 | 0 | int ret, err; |
7925 | | |
7926 | | /* Be more helpful by rejecting programs that can't be validated early |
7927 | | * with more meaningful and actionable error message. |
7928 | | */ |
7929 | 0 | switch (prog->type) { |
7930 | 0 | case BPF_PROG_TYPE_UNSPEC: |
7931 | | /* |
7932 | | * The program type must be set. Most likely we couldn't find a proper |
7933 | | * section definition at load time, and thus we didn't infer the type. |
7934 | | */ |
7935 | 0 | pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n", |
7936 | 0 | prog->name, prog->sec_name); |
7937 | 0 | return -EINVAL; |
7938 | 0 | case BPF_PROG_TYPE_STRUCT_OPS: |
7939 | 0 | if (prog->attach_btf_id == 0) { |
7940 | 0 | pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n", |
7941 | 0 | prog->name); |
7942 | 0 | return -EINVAL; |
7943 | 0 | } |
7944 | 0 | break; |
7945 | 0 | default: |
7946 | 0 | break; |
7947 | 0 | } |
7948 | | |
7949 | 0 | if (!insns || !insns_cnt) |
7950 | 0 | return -EINVAL; |
7951 | | |
7952 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
7953 | 0 | prog_name = prog->name; |
7954 | 0 | load_attr.attach_prog_fd = prog->attach_prog_fd; |
7955 | 0 | load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; |
7956 | 0 | load_attr.attach_btf_id = prog->attach_btf_id; |
7957 | 0 | load_attr.kern_version = kern_version; |
7958 | 0 | load_attr.prog_ifindex = prog->prog_ifindex; |
7959 | 0 | load_attr.expected_attach_type = prog->expected_attach_type; |
7960 | | |
7961 | | /* specify func_info/line_info only if kernel supports them */ |
7962 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) { |
7963 | 0 | load_attr.prog_btf_fd = btf__fd(obj->btf); |
7964 | 0 | load_attr.func_info = prog->func_info; |
7965 | 0 | load_attr.func_info_rec_size = prog->func_info_rec_size; |
7966 | 0 | load_attr.func_info_cnt = prog->func_info_cnt; |
7967 | 0 | load_attr.line_info = prog->line_info; |
7968 | 0 | load_attr.line_info_rec_size = prog->line_info_rec_size; |
7969 | 0 | load_attr.line_info_cnt = prog->line_info_cnt; |
7970 | 0 | } |
7971 | 0 | load_attr.log_level = log_level; |
7972 | 0 | load_attr.prog_flags = prog->prog_flags; |
7973 | 0 | load_attr.fd_array = obj->fd_array; |
7974 | |
|
7975 | 0 | load_attr.token_fd = obj->token_fd; |
7976 | 0 | if (obj->token_fd) |
7977 | 0 | load_attr.prog_flags |= BPF_F_TOKEN_FD; |
7978 | | |
7979 | | /* adjust load_attr if sec_def provides custom preload callback */ |
7980 | 0 | if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { |
7981 | 0 | err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie); |
7982 | 0 | if (err < 0) { |
7983 | 0 | pr_warn("prog '%s': failed to prepare load attributes: %s\n", |
7984 | 0 | prog->name, errstr(err)); |
7985 | 0 | return err; |
7986 | 0 | } |
7987 | 0 | insns = prog->insns; |
7988 | 0 | insns_cnt = prog->insns_cnt; |
7989 | 0 | } |
7990 | | |
7991 | 0 | if (obj->gen_loader) { |
7992 | 0 | bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, |
7993 | 0 | license, insns, insns_cnt, &load_attr, |
7994 | 0 | prog - obj->programs); |
7995 | 0 | *prog_fd = -1; |
7996 | 0 | return 0; |
7997 | 0 | } |
7998 | | |
7999 | 0 | retry_load: |
8000 | | /* if log_level is zero, we don't request logs initially even if |
8001 | | * custom log_buf is specified; if the program load fails, then we'll |
8002 | | * bump log_level to 1 and use either custom log_buf or we'll allocate |
8003 | | * our own and retry the load to get details on what failed |
8004 | | */ |
8005 | 0 | if (log_level) { |
8006 | 0 | if (prog->log_buf) { |
8007 | 0 | log_buf = prog->log_buf; |
8008 | 0 | log_buf_size = prog->log_size; |
8009 | 0 | own_log_buf = false; |
8010 | 0 | } else if (obj->log_buf) { |
8011 | 0 | log_buf = obj->log_buf; |
8012 | 0 | log_buf_size = obj->log_size; |
8013 | 0 | own_log_buf = false; |
8014 | 0 | } else { |
8015 | 0 | log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2); |
8016 | 0 | tmp = realloc(log_buf, log_buf_size); |
8017 | 0 | if (!tmp) { |
8018 | 0 | ret = -ENOMEM; |
8019 | 0 | goto out; |
8020 | 0 | } |
8021 | 0 | log_buf = tmp; |
8022 | 0 | log_buf[0] = '\0'; |
8023 | 0 | own_log_buf = true; |
8024 | 0 | } |
8025 | 0 | } |
8026 | | |
8027 | 0 | load_attr.log_buf = log_buf; |
8028 | 0 | load_attr.log_size = log_buf_size; |
8029 | 0 | load_attr.log_level = log_level; |
8030 | |
|
8031 | 0 | ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr); |
8032 | 0 | if (ret >= 0) { |
8033 | 0 | if (log_level && own_log_buf) { |
8034 | 0 | pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
8035 | 0 | prog->name, log_buf); |
8036 | 0 | } |
8037 | |
|
8038 | 0 | if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) { |
8039 | 0 | struct bpf_map *map; |
8040 | 0 | int i; |
8041 | |
|
8042 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
8043 | 0 | map = &prog->obj->maps[i]; |
8044 | 0 | if (map->libbpf_type != LIBBPF_MAP_RODATA) |
8045 | 0 | continue; |
8046 | | |
8047 | 0 | if (bpf_prog_bind_map(ret, map->fd, NULL)) { |
8048 | 0 | pr_warn("prog '%s': failed to bind map '%s': %s\n", |
8049 | 0 | prog->name, map->real_name, errstr(errno)); |
8050 | | /* Don't fail hard if can't bind rodata. */ |
8051 | 0 | } |
8052 | 0 | } |
8053 | 0 | } |
8054 | |
|
8055 | 0 | *prog_fd = ret; |
8056 | 0 | ret = 0; |
8057 | 0 | goto out; |
8058 | 0 | } |
8059 | | |
8060 | 0 | if (log_level == 0) { |
8061 | 0 | log_level = 1; |
8062 | 0 | goto retry_load; |
8063 | 0 | } |
8064 | | /* On ENOSPC, increase log buffer size and retry, unless custom |
8065 | | * log_buf is specified. |
8066 | | * Be careful to not overflow u32, though. Kernel's log buf size limit |
8067 | | * isn't part of UAPI so it can always be bumped to full 4GB. So don't |
8068 | | * multiply by 2 unless we are sure we'll fit within 32 bits. |
8069 | | * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2). |
8070 | | */ |
8071 | 0 | if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2) |
8072 | 0 | goto retry_load; |
8073 | | |
8074 | 0 | ret = -errno; |
8075 | | |
8076 | | /* post-process verifier log to improve error descriptions */ |
8077 | 0 | fixup_verifier_log(prog, log_buf, log_buf_size); |
8078 | |
|
8079 | 0 | pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, errstr(errno)); |
8080 | 0 | pr_perm_msg(ret); |
8081 | |
|
8082 | 0 | if (own_log_buf && log_buf && log_buf[0] != '\0') { |
8083 | 0 | pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n", |
8084 | 0 | prog->name, log_buf); |
8085 | 0 | } |
8086 | |
|
8087 | 0 | out: |
8088 | 0 | if (own_log_buf) |
8089 | 0 | free(log_buf); |
8090 | 0 | return ret; |
8091 | 0 | } |
8092 | | |
8093 | | static char *find_prev_line(char *buf, char *cur) |
8094 | 0 | { |
8095 | 0 | char *p; |
8096 | |
|
8097 | 0 | if (cur == buf) /* end of a log buf */ |
8098 | 0 | return NULL; |
8099 | | |
8100 | 0 | p = cur - 1; |
8101 | 0 | while (p - 1 >= buf && *(p - 1) != '\n') |
8102 | 0 | p--; |
8103 | |
|
8104 | 0 | return p; |
8105 | 0 | } |
8106 | | |
8107 | | static void patch_log(char *buf, size_t buf_sz, size_t log_sz, |
8108 | | char *orig, size_t orig_sz, const char *patch) |
8109 | 0 | { |
8110 | | /* size of the remaining log content to the right from the to-be-replaced part */ |
8111 | 0 | size_t rem_sz = (buf + log_sz) - (orig + orig_sz); |
8112 | 0 | size_t patch_sz = strlen(patch); |
8113 | |
|
8114 | 0 | if (patch_sz != orig_sz) { |
8115 | | /* If patch line(s) are longer than original piece of verifier log, |
8116 | | * shift log contents by (patch_sz - orig_sz) bytes to the right |
8117 | | * starting from after to-be-replaced part of the log. |
8118 | | * |
8119 | | * If patch line(s) are shorter than original piece of verifier log, |
8120 | | * shift log contents by (orig_sz - patch_sz) bytes to the left |
8121 | | * starting from after to-be-replaced part of the log |
8122 | | * |
8123 | | * We need to be careful about not overflowing available |
8124 | | * buf_sz capacity. If that's the case, we'll truncate the end |
8125 | | * of the original log, as necessary. |
8126 | | */ |
8127 | 0 | if (patch_sz > orig_sz) { |
8128 | 0 | if (orig + patch_sz >= buf + buf_sz) { |
8129 | | /* patch is big enough to cover remaining space completely */ |
8130 | 0 | patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1; |
8131 | 0 | rem_sz = 0; |
8132 | 0 | } else if (patch_sz - orig_sz > buf_sz - log_sz) { |
8133 | | /* patch causes part of remaining log to be truncated */ |
8134 | 0 | rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz); |
8135 | 0 | } |
8136 | 0 | } |
8137 | | /* shift remaining log to the right by calculated amount */ |
8138 | 0 | memmove(orig + patch_sz, orig + orig_sz, rem_sz); |
8139 | 0 | } |
8140 | |
|
8141 | 0 | memcpy(orig, patch, patch_sz); |
8142 | 0 | } |
8143 | | |
8144 | | static void fixup_log_failed_core_relo(struct bpf_program *prog, |
8145 | | char *buf, size_t buf_sz, size_t log_sz, |
8146 | | char *line1, char *line2, char *line3) |
8147 | 0 | { |
8148 | | /* Expected log for failed and not properly guarded CO-RE relocation: |
8149 | | * line1 -> 123: (85) call unknown#195896080 |
8150 | | * line2 -> invalid func unknown#195896080 |
8151 | | * line3 -> <anything else or end of buffer> |
8152 | | * |
8153 | | * "123" is the index of the instruction that was poisoned. We extract |
8154 | | * instruction index to find corresponding CO-RE relocation and |
8155 | | * replace this part of the log with more relevant information about |
8156 | | * failed CO-RE relocation. |
8157 | | */ |
8158 | 0 | const struct bpf_core_relo *relo; |
8159 | 0 | struct bpf_core_spec spec; |
8160 | 0 | char patch[512], spec_buf[256]; |
8161 | 0 | int insn_idx, err, spec_len; |
8162 | |
|
8163 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1) |
8164 | 0 | return; |
8165 | | |
8166 | 0 | relo = find_relo_core(prog, insn_idx); |
8167 | 0 | if (!relo) |
8168 | 0 | return; |
8169 | | |
8170 | 0 | err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec); |
8171 | 0 | if (err) |
8172 | 0 | return; |
8173 | | |
8174 | 0 | spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec); |
8175 | 0 | snprintf(patch, sizeof(patch), |
8176 | 0 | "%d: <invalid CO-RE relocation>\n" |
8177 | 0 | "failed to resolve CO-RE relocation %s%s\n", |
8178 | 0 | insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : ""); |
8179 | |
|
8180 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
8181 | 0 | } |
8182 | | |
8183 | | static void fixup_log_missing_map_load(struct bpf_program *prog, |
8184 | | char *buf, size_t buf_sz, size_t log_sz, |
8185 | | char *line1, char *line2, char *line3) |
8186 | 0 | { |
8187 | | /* Expected log for failed and not properly guarded map reference: |
8188 | | * line1 -> 123: (85) call unknown#2001000345 |
8189 | | * line2 -> invalid func unknown#2001000345 |
8190 | | * line3 -> <anything else or end of buffer> |
8191 | | * |
8192 | | * "123" is the index of the instruction that was poisoned. |
8193 | | * "345" in "2001000345" is a map index in obj->maps to fetch map name. |
8194 | | */ |
8195 | 0 | struct bpf_object *obj = prog->obj; |
8196 | 0 | const struct bpf_map *map; |
8197 | 0 | int insn_idx, map_idx; |
8198 | 0 | char patch[128]; |
8199 | |
|
8200 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2) |
8201 | 0 | return; |
8202 | | |
8203 | 0 | map_idx -= POISON_LDIMM64_MAP_BASE; |
8204 | 0 | if (map_idx < 0 || map_idx >= obj->nr_maps) |
8205 | 0 | return; |
8206 | 0 | map = &obj->maps[map_idx]; |
8207 | |
|
8208 | 0 | snprintf(patch, sizeof(patch), |
8209 | 0 | "%d: <invalid BPF map reference>\n" |
8210 | 0 | "BPF map '%s' is referenced but wasn't created\n", |
8211 | 0 | insn_idx, map->name); |
8212 | |
|
8213 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
8214 | 0 | } |
8215 | | |
8216 | | static void fixup_log_missing_kfunc_call(struct bpf_program *prog, |
8217 | | char *buf, size_t buf_sz, size_t log_sz, |
8218 | | char *line1, char *line2, char *line3) |
8219 | 0 | { |
8220 | | /* Expected log for failed and not properly guarded kfunc call: |
8221 | | * line1 -> 123: (85) call unknown#2002000345 |
8222 | | * line2 -> invalid func unknown#2002000345 |
8223 | | * line3 -> <anything else or end of buffer> |
8224 | | * |
8225 | | * "123" is the index of the instruction that was poisoned. |
8226 | | * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name. |
8227 | | */ |
8228 | 0 | struct bpf_object *obj = prog->obj; |
8229 | 0 | const struct extern_desc *ext; |
8230 | 0 | int insn_idx, ext_idx; |
8231 | 0 | char patch[128]; |
8232 | |
|
8233 | 0 | if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2) |
8234 | 0 | return; |
8235 | | |
8236 | 0 | ext_idx -= POISON_CALL_KFUNC_BASE; |
8237 | 0 | if (ext_idx < 0 || ext_idx >= obj->nr_extern) |
8238 | 0 | return; |
8239 | 0 | ext = &obj->externs[ext_idx]; |
8240 | |
|
8241 | 0 | snprintf(patch, sizeof(patch), |
8242 | 0 | "%d: <invalid kfunc call>\n" |
8243 | 0 | "kfunc '%s' is referenced but wasn't resolved\n", |
8244 | 0 | insn_idx, ext->name); |
8245 | |
|
8246 | 0 | patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch); |
8247 | 0 | } |
8248 | | |
8249 | | static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz) |
8250 | 0 | { |
8251 | | /* look for familiar error patterns in last N lines of the log */ |
8252 | 0 | const size_t max_last_line_cnt = 10; |
8253 | 0 | char *prev_line, *cur_line, *next_line; |
8254 | 0 | size_t log_sz; |
8255 | 0 | int i; |
8256 | |
|
8257 | 0 | if (!buf) |
8258 | 0 | return; |
8259 | | |
8260 | 0 | log_sz = strlen(buf) + 1; |
8261 | 0 | next_line = buf + log_sz - 1; |
8262 | |
|
8263 | 0 | for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) { |
8264 | 0 | cur_line = find_prev_line(buf, next_line); |
8265 | 0 | if (!cur_line) |
8266 | 0 | return; |
8267 | | |
8268 | 0 | if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) { |
8269 | 0 | prev_line = find_prev_line(buf, cur_line); |
8270 | 0 | if (!prev_line) |
8271 | 0 | continue; |
8272 | | |
8273 | | /* failed CO-RE relocation case */ |
8274 | 0 | fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz, |
8275 | 0 | prev_line, cur_line, next_line); |
8276 | 0 | return; |
8277 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) { |
8278 | 0 | prev_line = find_prev_line(buf, cur_line); |
8279 | 0 | if (!prev_line) |
8280 | 0 | continue; |
8281 | | |
8282 | | /* reference to uncreated BPF map */ |
8283 | 0 | fixup_log_missing_map_load(prog, buf, buf_sz, log_sz, |
8284 | 0 | prev_line, cur_line, next_line); |
8285 | 0 | return; |
8286 | 0 | } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) { |
8287 | 0 | prev_line = find_prev_line(buf, cur_line); |
8288 | 0 | if (!prev_line) |
8289 | 0 | continue; |
8290 | | |
8291 | | /* reference to unresolved kfunc */ |
8292 | 0 | fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz, |
8293 | 0 | prev_line, cur_line, next_line); |
8294 | 0 | return; |
8295 | 0 | } |
8296 | 0 | } |
8297 | 0 | } |
8298 | | |
8299 | | static int bpf_program_record_relos(struct bpf_program *prog) |
8300 | 0 | { |
8301 | 0 | struct bpf_object *obj = prog->obj; |
8302 | 0 | int i; |
8303 | |
|
8304 | 0 | for (i = 0; i < prog->nr_reloc; i++) { |
8305 | 0 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
8306 | 0 | struct extern_desc *ext = &obj->externs[relo->ext_idx]; |
8307 | 0 | int kind; |
8308 | |
|
8309 | 0 | switch (relo->type) { |
8310 | 0 | case RELO_EXTERN_LD64: |
8311 | 0 | if (ext->type != EXT_KSYM) |
8312 | 0 | continue; |
8313 | 0 | kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ? |
8314 | 0 | BTF_KIND_VAR : BTF_KIND_FUNC; |
8315 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
8316 | 0 | ext->is_weak, !ext->ksym.type_id, |
8317 | 0 | true, kind, relo->insn_idx); |
8318 | 0 | break; |
8319 | 0 | case RELO_EXTERN_CALL: |
8320 | 0 | bpf_gen__record_extern(obj->gen_loader, ext->name, |
8321 | 0 | ext->is_weak, false, false, BTF_KIND_FUNC, |
8322 | 0 | relo->insn_idx); |
8323 | 0 | break; |
8324 | 0 | case RELO_CORE: { |
8325 | 0 | struct bpf_core_relo cr = { |
8326 | 0 | .insn_off = relo->insn_idx * 8, |
8327 | 0 | .type_id = relo->core_relo->type_id, |
8328 | 0 | .access_str_off = relo->core_relo->access_str_off, |
8329 | 0 | .kind = relo->core_relo->kind, |
8330 | 0 | }; |
8331 | |
|
8332 | 0 | bpf_gen__record_relo_core(obj->gen_loader, &cr); |
8333 | 0 | break; |
8334 | 0 | } |
8335 | 0 | default: |
8336 | 0 | continue; |
8337 | 0 | } |
8338 | 0 | } |
8339 | 0 | return 0; |
8340 | 0 | } |
8341 | | |
8342 | | static int |
8343 | | bpf_object__load_progs(struct bpf_object *obj, int log_level) |
8344 | 0 | { |
8345 | 0 | struct bpf_program *prog; |
8346 | 0 | size_t i; |
8347 | 0 | int err; |
8348 | |
|
8349 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
8350 | 0 | prog = &obj->programs[i]; |
8351 | 0 | if (prog_is_subprog(obj, prog)) |
8352 | 0 | continue; |
8353 | 0 | if (!prog->autoload) { |
8354 | 0 | pr_debug("prog '%s': skipped loading\n", prog->name); |
8355 | 0 | continue; |
8356 | 0 | } |
8357 | 0 | prog->log_level |= log_level; |
8358 | |
|
8359 | 0 | if (obj->gen_loader) |
8360 | 0 | bpf_program_record_relos(prog); |
8361 | |
|
8362 | 0 | err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt, |
8363 | 0 | obj->license, obj->kern_version, &prog->fd); |
8364 | 0 | if (err) { |
8365 | 0 | pr_warn("prog '%s': failed to load: %s\n", prog->name, errstr(err)); |
8366 | 0 | return err; |
8367 | 0 | } |
8368 | 0 | } |
8369 | | |
8370 | 0 | bpf_object__free_relocs(obj); |
8371 | 0 | return 0; |
8372 | 0 | } |
8373 | | |
8374 | | static int bpf_object_prepare_progs(struct bpf_object *obj) |
8375 | 0 | { |
8376 | 0 | struct bpf_program *prog; |
8377 | 0 | size_t i; |
8378 | 0 | int err; |
8379 | |
|
8380 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
8381 | 0 | prog = &obj->programs[i]; |
8382 | 0 | err = bpf_object__sanitize_prog(obj, prog); |
8383 | 0 | if (err) |
8384 | 0 | return err; |
8385 | 0 | } |
8386 | 0 | return 0; |
8387 | 0 | } |
8388 | | |
8389 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name); |
8390 | | |
8391 | | static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts) |
8392 | 2.07k | { |
8393 | 2.07k | struct bpf_program *prog; |
8394 | 2.07k | int err; |
8395 | | |
8396 | 8.14k | bpf_object__for_each_program(prog, obj) { |
8397 | 8.14k | prog->sec_def = find_sec_def(prog->sec_name); |
8398 | 8.14k | if (!prog->sec_def) { |
8399 | | /* couldn't guess, but user might manually specify */ |
8400 | 7.11k | pr_debug("prog '%s': unrecognized ELF section name '%s'\n", |
8401 | 7.11k | prog->name, prog->sec_name); |
8402 | 7.11k | continue; |
8403 | 7.11k | } |
8404 | | |
8405 | 1.03k | prog->type = prog->sec_def->prog_type; |
8406 | 1.03k | prog->expected_attach_type = prog->sec_def->expected_attach_type; |
8407 | | |
8408 | | /* sec_def can have custom callback which should be called |
8409 | | * after bpf_program is initialized to adjust its properties |
8410 | | */ |
8411 | 1.03k | if (prog->sec_def->prog_setup_fn) { |
8412 | 0 | err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie); |
8413 | 0 | if (err < 0) { |
8414 | 0 | pr_warn("prog '%s': failed to initialize: %s\n", |
8415 | 0 | prog->name, errstr(err)); |
8416 | 0 | return err; |
8417 | 0 | } |
8418 | 0 | } |
8419 | 1.03k | } |
8420 | | |
8421 | 2.07k | return 0; |
8422 | 2.07k | } |
8423 | | |
8424 | | static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz, |
8425 | | const char *obj_name, |
8426 | | const struct bpf_object_open_opts *opts) |
8427 | 10.9k | { |
8428 | 10.9k | const char *kconfig, *btf_tmp_path, *token_path; |
8429 | 10.9k | struct bpf_object *obj; |
8430 | 10.9k | int err; |
8431 | 10.9k | char *log_buf; |
8432 | 10.9k | size_t log_size; |
8433 | 10.9k | __u32 log_level; |
8434 | | |
8435 | 10.9k | if (obj_buf && !obj_name) |
8436 | 0 | return ERR_PTR(-EINVAL); |
8437 | | |
8438 | 10.9k | if (elf_version(EV_CURRENT) == EV_NONE) { |
8439 | 0 | pr_warn("failed to init libelf for %s\n", |
8440 | 0 | path ? : "(mem buf)"); |
8441 | 0 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
8442 | 0 | } |
8443 | | |
8444 | 10.9k | if (!OPTS_VALID(opts, bpf_object_open_opts)) |
8445 | 0 | return ERR_PTR(-EINVAL); |
8446 | | |
8447 | 10.9k | obj_name = OPTS_GET(opts, object_name, NULL) ?: obj_name; |
8448 | 10.9k | if (obj_buf) { |
8449 | 10.9k | path = obj_name; |
8450 | 10.9k | pr_debug("loading object '%s' from buffer\n", obj_name); |
8451 | 10.9k | } else { |
8452 | 0 | pr_debug("loading object from %s\n", path); |
8453 | 0 | } |
8454 | | |
8455 | 10.9k | log_buf = OPTS_GET(opts, kernel_log_buf, NULL); |
8456 | 10.9k | log_size = OPTS_GET(opts, kernel_log_size, 0); |
8457 | 10.9k | log_level = OPTS_GET(opts, kernel_log_level, 0); |
8458 | 10.9k | if (log_size > UINT_MAX) |
8459 | 0 | return ERR_PTR(-EINVAL); |
8460 | 10.9k | if (log_size && !log_buf) |
8461 | 0 | return ERR_PTR(-EINVAL); |
8462 | | |
8463 | 10.9k | token_path = OPTS_GET(opts, bpf_token_path, NULL); |
8464 | | /* if user didn't specify bpf_token_path explicitly, check if |
8465 | | * LIBBPF_BPF_TOKEN_PATH envvar was set and treat it as bpf_token_path |
8466 | | * option |
8467 | | */ |
8468 | 10.9k | if (!token_path) |
8469 | 10.9k | token_path = getenv("LIBBPF_BPF_TOKEN_PATH"); |
8470 | 10.9k | if (token_path && strlen(token_path) >= PATH_MAX) |
8471 | 0 | return ERR_PTR(-ENAMETOOLONG); |
8472 | | |
8473 | 10.9k | obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name); |
8474 | 10.9k | if (IS_ERR(obj)) |
8475 | 0 | return obj; |
8476 | | |
8477 | 10.9k | obj->log_buf = log_buf; |
8478 | 10.9k | obj->log_size = log_size; |
8479 | 10.9k | obj->log_level = log_level; |
8480 | | |
8481 | 10.9k | if (token_path) { |
8482 | 0 | obj->token_path = strdup(token_path); |
8483 | 0 | if (!obj->token_path) { |
8484 | 0 | err = -ENOMEM; |
8485 | 0 | goto out; |
8486 | 0 | } |
8487 | 0 | } |
8488 | | |
8489 | 10.9k | btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL); |
8490 | 10.9k | if (btf_tmp_path) { |
8491 | 0 | if (strlen(btf_tmp_path) >= PATH_MAX) { |
8492 | 0 | err = -ENAMETOOLONG; |
8493 | 0 | goto out; |
8494 | 0 | } |
8495 | 0 | obj->btf_custom_path = strdup(btf_tmp_path); |
8496 | 0 | if (!obj->btf_custom_path) { |
8497 | 0 | err = -ENOMEM; |
8498 | 0 | goto out; |
8499 | 0 | } |
8500 | 0 | } |
8501 | | |
8502 | 10.9k | kconfig = OPTS_GET(opts, kconfig, NULL); |
8503 | 10.9k | if (kconfig) { |
8504 | 0 | obj->kconfig = strdup(kconfig); |
8505 | 0 | if (!obj->kconfig) { |
8506 | 0 | err = -ENOMEM; |
8507 | 0 | goto out; |
8508 | 0 | } |
8509 | 0 | } |
8510 | | |
8511 | 10.9k | err = bpf_object__elf_init(obj); |
8512 | 10.9k | err = err ? : bpf_object__elf_collect(obj); |
8513 | 10.9k | err = err ? : bpf_object__collect_externs(obj); |
8514 | 10.9k | err = err ? : bpf_object_fixup_btf(obj); |
8515 | 10.9k | err = err ? : bpf_object__init_maps(obj, opts); |
8516 | 10.9k | err = err ? : bpf_object_init_progs(obj, opts); |
8517 | 10.9k | err = err ? : bpf_object__collect_relos(obj); |
8518 | 10.9k | if (err) |
8519 | 9.42k | goto out; |
8520 | | |
8521 | 1.53k | bpf_object__elf_finish(obj); |
8522 | | |
8523 | 1.53k | return obj; |
8524 | 9.42k | out: |
8525 | 9.42k | bpf_object__close(obj); |
8526 | 9.42k | return ERR_PTR(err); |
8527 | 10.9k | } |
8528 | | |
8529 | | struct bpf_object * |
8530 | | bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts) |
8531 | 0 | { |
8532 | 0 | if (!path) |
8533 | 0 | return libbpf_err_ptr(-EINVAL); |
8534 | | |
8535 | 0 | return libbpf_ptr(bpf_object_open(path, NULL, 0, NULL, opts)); |
8536 | 0 | } |
8537 | | |
8538 | | struct bpf_object *bpf_object__open(const char *path) |
8539 | 0 | { |
8540 | 0 | return bpf_object__open_file(path, NULL); |
8541 | 0 | } |
8542 | | |
8543 | | struct bpf_object * |
8544 | | bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, |
8545 | | const struct bpf_object_open_opts *opts) |
8546 | 10.9k | { |
8547 | 10.9k | char tmp_name[64]; |
8548 | | |
8549 | 10.9k | if (!obj_buf || obj_buf_sz == 0) |
8550 | 0 | return libbpf_err_ptr(-EINVAL); |
8551 | | |
8552 | | /* create a (quite useless) default "name" for this memory buffer object */ |
8553 | 10.9k | snprintf(tmp_name, sizeof(tmp_name), "%lx-%zx", (unsigned long)obj_buf, obj_buf_sz); |
8554 | | |
8555 | 10.9k | return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, tmp_name, opts)); |
8556 | 10.9k | } |
8557 | | |
8558 | | static int bpf_object_unload(struct bpf_object *obj) |
8559 | 10.9k | { |
8560 | 10.9k | size_t i; |
8561 | | |
8562 | 10.9k | if (!obj) |
8563 | 0 | return libbpf_err(-EINVAL); |
8564 | | |
8565 | 13.0k | for (i = 0; i < obj->nr_maps; i++) { |
8566 | 2.06k | zclose(obj->maps[i].fd); |
8567 | 2.06k | if (obj->maps[i].st_ops) |
8568 | 96 | zfree(&obj->maps[i].st_ops->kern_vdata); |
8569 | 2.06k | } |
8570 | | |
8571 | 21.3k | for (i = 0; i < obj->nr_programs; i++) |
8572 | 10.4k | bpf_program__unload(&obj->programs[i]); |
8573 | | |
8574 | 10.9k | return 0; |
8575 | 10.9k | } |
8576 | | |
8577 | | static int bpf_object__sanitize_maps(struct bpf_object *obj) |
8578 | 0 | { |
8579 | 0 | struct bpf_map *m; |
8580 | |
|
8581 | 0 | bpf_object__for_each_map(m, obj) { |
8582 | 0 | if (!bpf_map__is_internal(m)) |
8583 | 0 | continue; |
8584 | 0 | if (!kernel_supports(obj, FEAT_ARRAY_MMAP)) |
8585 | 0 | m->def.map_flags &= ~BPF_F_MMAPABLE; |
8586 | 0 | } |
8587 | |
|
8588 | 0 | return 0; |
8589 | 0 | } |
8590 | | |
8591 | | typedef int (*kallsyms_cb_t)(unsigned long long sym_addr, char sym_type, |
8592 | | const char *sym_name, void *ctx); |
8593 | | |
8594 | | static int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx) |
8595 | 0 | { |
8596 | 0 | char sym_type, sym_name[500]; |
8597 | 0 | unsigned long long sym_addr; |
8598 | 0 | int ret, err = 0; |
8599 | 0 | FILE *f; |
8600 | |
|
8601 | 0 | f = fopen("/proc/kallsyms", "re"); |
8602 | 0 | if (!f) { |
8603 | 0 | err = -errno; |
8604 | 0 | pr_warn("failed to open /proc/kallsyms: %s\n", errstr(err)); |
8605 | 0 | return err; |
8606 | 0 | } |
8607 | | |
8608 | 0 | while (true) { |
8609 | 0 | ret = fscanf(f, "%llx %c %499s%*[^\n]\n", |
8610 | 0 | &sym_addr, &sym_type, sym_name); |
8611 | 0 | if (ret == EOF && feof(f)) |
8612 | 0 | break; |
8613 | 0 | if (ret != 3) { |
8614 | 0 | pr_warn("failed to read kallsyms entry: %d\n", ret); |
8615 | 0 | err = -EINVAL; |
8616 | 0 | break; |
8617 | 0 | } |
8618 | | |
8619 | 0 | err = cb(sym_addr, sym_type, sym_name, ctx); |
8620 | 0 | if (err) |
8621 | 0 | break; |
8622 | 0 | } |
8623 | |
|
8624 | 0 | fclose(f); |
8625 | 0 | return err; |
8626 | 0 | } |
8627 | | |
8628 | | static int kallsyms_cb(unsigned long long sym_addr, char sym_type, |
8629 | | const char *sym_name, void *ctx) |
8630 | 0 | { |
8631 | 0 | struct bpf_object *obj = ctx; |
8632 | 0 | const struct btf_type *t; |
8633 | 0 | struct extern_desc *ext; |
8634 | 0 | const char *res; |
8635 | |
|
8636 | 0 | res = strstr(sym_name, ".llvm."); |
8637 | 0 | if (sym_type == 'd' && res) |
8638 | 0 | ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name); |
8639 | 0 | else |
8640 | 0 | ext = find_extern_by_name(obj, sym_name); |
8641 | 0 | if (!ext || ext->type != EXT_KSYM) |
8642 | 0 | return 0; |
8643 | | |
8644 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
8645 | 0 | if (!btf_is_var(t)) |
8646 | 0 | return 0; |
8647 | | |
8648 | 0 | if (ext->is_set && ext->ksym.addr != sym_addr) { |
8649 | 0 | pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n", |
8650 | 0 | sym_name, ext->ksym.addr, sym_addr); |
8651 | 0 | return -EINVAL; |
8652 | 0 | } |
8653 | 0 | if (!ext->is_set) { |
8654 | 0 | ext->is_set = true; |
8655 | 0 | ext->ksym.addr = sym_addr; |
8656 | 0 | pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr); |
8657 | 0 | } |
8658 | 0 | return 0; |
8659 | 0 | } |
8660 | | |
8661 | | static int bpf_object__read_kallsyms_file(struct bpf_object *obj) |
8662 | 0 | { |
8663 | 0 | return libbpf_kallsyms_parse(kallsyms_cb, obj); |
8664 | 0 | } |
8665 | | |
8666 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
8667 | | __u16 kind, struct btf **res_btf, |
8668 | | struct module_btf **res_mod_btf) |
8669 | 0 | { |
8670 | 0 | struct module_btf *mod_btf; |
8671 | 0 | struct btf *btf; |
8672 | 0 | int i, id, err; |
8673 | |
|
8674 | 0 | btf = obj->btf_vmlinux; |
8675 | 0 | mod_btf = NULL; |
8676 | 0 | id = btf__find_by_name_kind(btf, ksym_name, kind); |
8677 | |
|
8678 | 0 | if (id == -ENOENT) { |
8679 | 0 | err = load_module_btfs(obj); |
8680 | 0 | if (err) |
8681 | 0 | return err; |
8682 | | |
8683 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
8684 | | /* we assume module_btf's BTF FD is always >0 */ |
8685 | 0 | mod_btf = &obj->btf_modules[i]; |
8686 | 0 | btf = mod_btf->btf; |
8687 | 0 | id = btf__find_by_name_kind_own(btf, ksym_name, kind); |
8688 | 0 | if (id != -ENOENT) |
8689 | 0 | break; |
8690 | 0 | } |
8691 | 0 | } |
8692 | 0 | if (id <= 0) |
8693 | 0 | return -ESRCH; |
8694 | | |
8695 | 0 | *res_btf = btf; |
8696 | 0 | *res_mod_btf = mod_btf; |
8697 | 0 | return id; |
8698 | 0 | } |
8699 | | |
8700 | | static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj, |
8701 | | struct extern_desc *ext) |
8702 | 0 | { |
8703 | 0 | const struct btf_type *targ_var, *targ_type; |
8704 | 0 | __u32 targ_type_id, local_type_id; |
8705 | 0 | struct module_btf *mod_btf = NULL; |
8706 | 0 | const char *targ_var_name; |
8707 | 0 | struct btf *btf = NULL; |
8708 | 0 | int id, err; |
8709 | |
|
8710 | 0 | id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf); |
8711 | 0 | if (id < 0) { |
8712 | 0 | if (id == -ESRCH && ext->is_weak) |
8713 | 0 | return 0; |
8714 | 0 | pr_warn("extern (var ksym) '%s': not found in kernel BTF\n", |
8715 | 0 | ext->name); |
8716 | 0 | return id; |
8717 | 0 | } |
8718 | | |
8719 | | /* find local type_id */ |
8720 | 0 | local_type_id = ext->ksym.type_id; |
8721 | | |
8722 | | /* find target type_id */ |
8723 | 0 | targ_var = btf__type_by_id(btf, id); |
8724 | 0 | targ_var_name = btf__name_by_offset(btf, targ_var->name_off); |
8725 | 0 | targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id); |
8726 | |
|
8727 | 0 | err = bpf_core_types_are_compat(obj->btf, local_type_id, |
8728 | 0 | btf, targ_type_id); |
8729 | 0 | if (err <= 0) { |
8730 | 0 | const struct btf_type *local_type; |
8731 | 0 | const char *targ_name, *local_name; |
8732 | |
|
8733 | 0 | local_type = btf__type_by_id(obj->btf, local_type_id); |
8734 | 0 | local_name = btf__name_by_offset(obj->btf, local_type->name_off); |
8735 | 0 | targ_name = btf__name_by_offset(btf, targ_type->name_off); |
8736 | |
|
8737 | 0 | pr_warn("extern (var ksym) '%s': incompatible types, expected [%u] %s %s, but kernel has [%u] %s %s\n", |
8738 | 0 | ext->name, local_type_id, |
8739 | 0 | btf_kind_str(local_type), local_name, targ_type_id, |
8740 | 0 | btf_kind_str(targ_type), targ_name); |
8741 | 0 | return -EINVAL; |
8742 | 0 | } |
8743 | | |
8744 | 0 | ext->is_set = true; |
8745 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
8746 | 0 | ext->ksym.kernel_btf_id = id; |
8747 | 0 | pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n", |
8748 | 0 | ext->name, id, btf_kind_str(targ_var), targ_var_name); |
8749 | |
|
8750 | 0 | return 0; |
8751 | 0 | } |
8752 | | |
8753 | | static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, |
8754 | | struct extern_desc *ext) |
8755 | 0 | { |
8756 | 0 | int local_func_proto_id, kfunc_proto_id, kfunc_id; |
8757 | 0 | struct module_btf *mod_btf = NULL; |
8758 | 0 | const struct btf_type *kern_func; |
8759 | 0 | struct btf *kern_btf = NULL; |
8760 | 0 | int ret; |
8761 | |
|
8762 | 0 | local_func_proto_id = ext->ksym.type_id; |
8763 | |
|
8764 | 0 | kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf, |
8765 | 0 | &mod_btf); |
8766 | 0 | if (kfunc_id < 0) { |
8767 | 0 | if (kfunc_id == -ESRCH && ext->is_weak) |
8768 | 0 | return 0; |
8769 | 0 | pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n", |
8770 | 0 | ext->name); |
8771 | 0 | return kfunc_id; |
8772 | 0 | } |
8773 | | |
8774 | 0 | kern_func = btf__type_by_id(kern_btf, kfunc_id); |
8775 | 0 | kfunc_proto_id = kern_func->type; |
8776 | |
|
8777 | 0 | ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id, |
8778 | 0 | kern_btf, kfunc_proto_id); |
8779 | 0 | if (ret <= 0) { |
8780 | 0 | if (ext->is_weak) |
8781 | 0 | return 0; |
8782 | | |
8783 | 0 | pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n", |
8784 | 0 | ext->name, local_func_proto_id, |
8785 | 0 | mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); |
8786 | 0 | return -EINVAL; |
8787 | 0 | } |
8788 | | |
8789 | | /* set index for module BTF fd in fd_array, if unset */ |
8790 | 0 | if (mod_btf && !mod_btf->fd_array_idx) { |
8791 | | /* insn->off is s16 */ |
8792 | 0 | if (obj->fd_array_cnt == INT16_MAX) { |
8793 | 0 | pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n", |
8794 | 0 | ext->name, mod_btf->fd_array_idx); |
8795 | 0 | return -E2BIG; |
8796 | 0 | } |
8797 | | /* Cannot use index 0 for module BTF fd */ |
8798 | 0 | if (!obj->fd_array_cnt) |
8799 | 0 | obj->fd_array_cnt = 1; |
8800 | |
|
8801 | 0 | ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int), |
8802 | 0 | obj->fd_array_cnt + 1); |
8803 | 0 | if (ret) |
8804 | 0 | return ret; |
8805 | 0 | mod_btf->fd_array_idx = obj->fd_array_cnt; |
8806 | | /* we assume module BTF FD is always >0 */ |
8807 | 0 | obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd; |
8808 | 0 | } |
8809 | | |
8810 | 0 | ext->is_set = true; |
8811 | 0 | ext->ksym.kernel_btf_id = kfunc_id; |
8812 | 0 | ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; |
8813 | | /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data() |
8814 | | * populates FD into ld_imm64 insn when it's used to point to kfunc. |
8815 | | * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. |
8816 | | * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. |
8817 | | */ |
8818 | 0 | ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; |
8819 | 0 | pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n", |
8820 | 0 | ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); |
8821 | |
|
8822 | 0 | return 0; |
8823 | 0 | } |
8824 | | |
8825 | | static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) |
8826 | 0 | { |
8827 | 0 | const struct btf_type *t; |
8828 | 0 | struct extern_desc *ext; |
8829 | 0 | int i, err; |
8830 | |
|
8831 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8832 | 0 | ext = &obj->externs[i]; |
8833 | 0 | if (ext->type != EXT_KSYM || !ext->ksym.type_id) |
8834 | 0 | continue; |
8835 | | |
8836 | 0 | if (obj->gen_loader) { |
8837 | 0 | ext->is_set = true; |
8838 | 0 | ext->ksym.kernel_btf_obj_fd = 0; |
8839 | 0 | ext->ksym.kernel_btf_id = 0; |
8840 | 0 | continue; |
8841 | 0 | } |
8842 | 0 | t = btf__type_by_id(obj->btf, ext->btf_id); |
8843 | 0 | if (btf_is_var(t)) |
8844 | 0 | err = bpf_object__resolve_ksym_var_btf_id(obj, ext); |
8845 | 0 | else |
8846 | 0 | err = bpf_object__resolve_ksym_func_btf_id(obj, ext); |
8847 | 0 | if (err) |
8848 | 0 | return err; |
8849 | 0 | } |
8850 | 0 | return 0; |
8851 | 0 | } |
8852 | | |
8853 | | static int bpf_object__resolve_externs(struct bpf_object *obj, |
8854 | | const char *extra_kconfig) |
8855 | 0 | { |
8856 | 0 | bool need_config = false, need_kallsyms = false; |
8857 | 0 | bool need_vmlinux_btf = false; |
8858 | 0 | struct extern_desc *ext; |
8859 | 0 | void *kcfg_data = NULL; |
8860 | 0 | int err, i; |
8861 | |
|
8862 | 0 | if (obj->nr_extern == 0) |
8863 | 0 | return 0; |
8864 | | |
8865 | 0 | if (obj->kconfig_map_idx >= 0) |
8866 | 0 | kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped; |
8867 | |
|
8868 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8869 | 0 | ext = &obj->externs[i]; |
8870 | |
|
8871 | 0 | if (ext->type == EXT_KSYM) { |
8872 | 0 | if (ext->ksym.type_id) |
8873 | 0 | need_vmlinux_btf = true; |
8874 | 0 | else |
8875 | 0 | need_kallsyms = true; |
8876 | 0 | continue; |
8877 | 0 | } else if (ext->type == EXT_KCFG) { |
8878 | 0 | void *ext_ptr = kcfg_data + ext->kcfg.data_off; |
8879 | 0 | __u64 value = 0; |
8880 | | |
8881 | | /* Kconfig externs need actual /proc/config.gz */ |
8882 | 0 | if (str_has_pfx(ext->name, "CONFIG_")) { |
8883 | 0 | need_config = true; |
8884 | 0 | continue; |
8885 | 0 | } |
8886 | | |
8887 | | /* Virtual kcfg externs are customly handled by libbpf */ |
8888 | 0 | if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { |
8889 | 0 | value = get_kernel_version(); |
8890 | 0 | if (!value) { |
8891 | 0 | pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name); |
8892 | 0 | return -EINVAL; |
8893 | 0 | } |
8894 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) { |
8895 | 0 | value = kernel_supports(obj, FEAT_BPF_COOKIE); |
8896 | 0 | } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) { |
8897 | 0 | value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER); |
8898 | 0 | } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) { |
8899 | | /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed |
8900 | | * __kconfig externs, where LINUX_ ones are virtual and filled out |
8901 | | * customly by libbpf (their values don't come from Kconfig). |
8902 | | * If LINUX_xxx variable is not recognized by libbpf, but is marked |
8903 | | * __weak, it defaults to zero value, just like for CONFIG_xxx |
8904 | | * externs. |
8905 | | */ |
8906 | 0 | pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name); |
8907 | 0 | return -EINVAL; |
8908 | 0 | } |
8909 | | |
8910 | 0 | err = set_kcfg_value_num(ext, ext_ptr, value); |
8911 | 0 | if (err) |
8912 | 0 | return err; |
8913 | 0 | pr_debug("extern (kcfg) '%s': set to 0x%llx\n", |
8914 | 0 | ext->name, (unsigned long long)value); |
8915 | 0 | } else { |
8916 | 0 | pr_warn("extern '%s': unrecognized extern kind\n", ext->name); |
8917 | 0 | return -EINVAL; |
8918 | 0 | } |
8919 | 0 | } |
8920 | 0 | if (need_config && extra_kconfig) { |
8921 | 0 | err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data); |
8922 | 0 | if (err) |
8923 | 0 | return -EINVAL; |
8924 | 0 | need_config = false; |
8925 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8926 | 0 | ext = &obj->externs[i]; |
8927 | 0 | if (ext->type == EXT_KCFG && !ext->is_set) { |
8928 | 0 | need_config = true; |
8929 | 0 | break; |
8930 | 0 | } |
8931 | 0 | } |
8932 | 0 | } |
8933 | 0 | if (need_config) { |
8934 | 0 | err = bpf_object__read_kconfig_file(obj, kcfg_data); |
8935 | 0 | if (err) |
8936 | 0 | return -EINVAL; |
8937 | 0 | } |
8938 | 0 | if (need_kallsyms) { |
8939 | 0 | err = bpf_object__read_kallsyms_file(obj); |
8940 | 0 | if (err) |
8941 | 0 | return -EINVAL; |
8942 | 0 | } |
8943 | 0 | if (need_vmlinux_btf) { |
8944 | 0 | err = bpf_object__resolve_ksyms_btf_id(obj); |
8945 | 0 | if (err) |
8946 | 0 | return -EINVAL; |
8947 | 0 | } |
8948 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
8949 | 0 | ext = &obj->externs[i]; |
8950 | |
|
8951 | 0 | if (!ext->is_set && !ext->is_weak) { |
8952 | 0 | pr_warn("extern '%s' (strong): not resolved\n", ext->name); |
8953 | 0 | return -ESRCH; |
8954 | 0 | } else if (!ext->is_set) { |
8955 | 0 | pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n", |
8956 | 0 | ext->name); |
8957 | 0 | } |
8958 | 0 | } |
8959 | | |
8960 | 0 | return 0; |
8961 | 0 | } |
8962 | | |
8963 | | static void bpf_map_prepare_vdata(const struct bpf_map *map) |
8964 | 0 | { |
8965 | 0 | const struct btf_type *type; |
8966 | 0 | struct bpf_struct_ops *st_ops; |
8967 | 0 | __u32 i; |
8968 | |
|
8969 | 0 | st_ops = map->st_ops; |
8970 | 0 | type = btf__type_by_id(map->obj->btf, st_ops->type_id); |
8971 | 0 | for (i = 0; i < btf_vlen(type); i++) { |
8972 | 0 | struct bpf_program *prog = st_ops->progs[i]; |
8973 | 0 | void *kern_data; |
8974 | 0 | int prog_fd; |
8975 | |
|
8976 | 0 | if (!prog) |
8977 | 0 | continue; |
8978 | | |
8979 | 0 | prog_fd = bpf_program__fd(prog); |
8980 | 0 | kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; |
8981 | 0 | *(unsigned long *)kern_data = prog_fd; |
8982 | 0 | } |
8983 | 0 | } |
8984 | | |
8985 | | static int bpf_object_prepare_struct_ops(struct bpf_object *obj) |
8986 | 0 | { |
8987 | 0 | struct bpf_map *map; |
8988 | 0 | int i; |
8989 | |
|
8990 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
8991 | 0 | map = &obj->maps[i]; |
8992 | |
|
8993 | 0 | if (!bpf_map__is_struct_ops(map)) |
8994 | 0 | continue; |
8995 | | |
8996 | 0 | if (!map->autocreate) |
8997 | 0 | continue; |
8998 | | |
8999 | 0 | bpf_map_prepare_vdata(map); |
9000 | 0 | } |
9001 | |
|
9002 | 0 | return 0; |
9003 | 0 | } |
9004 | | |
9005 | | static void bpf_object_unpin(struct bpf_object *obj) |
9006 | 0 | { |
9007 | 0 | int i; |
9008 | | |
9009 | | /* unpin any maps that were auto-pinned during load */ |
9010 | 0 | for (i = 0; i < obj->nr_maps; i++) |
9011 | 0 | if (obj->maps[i].pinned && !obj->maps[i].reused) |
9012 | 0 | bpf_map__unpin(&obj->maps[i], NULL); |
9013 | 0 | } |
9014 | | |
9015 | | static void bpf_object_cleanup_btf(struct bpf_object *obj) |
9016 | 10.9k | { |
9017 | 10.9k | int i; |
9018 | | |
9019 | | /* clean up module BTFs */ |
9020 | 10.9k | for (i = 0; i < obj->btf_module_cnt; i++) { |
9021 | 0 | close(obj->btf_modules[i].fd); |
9022 | 0 | btf__free(obj->btf_modules[i].btf); |
9023 | 0 | free(obj->btf_modules[i].name); |
9024 | 0 | } |
9025 | 10.9k | obj->btf_module_cnt = 0; |
9026 | 10.9k | obj->btf_module_cap = 0; |
9027 | 10.9k | obj->btf_modules_loaded = false; |
9028 | 10.9k | zfree(&obj->btf_modules); |
9029 | | |
9030 | | /* clean up vmlinux BTF */ |
9031 | 10.9k | btf__free(obj->btf_vmlinux); |
9032 | 10.9k | obj->btf_vmlinux = NULL; |
9033 | 10.9k | } |
9034 | | |
9035 | | static void bpf_object_post_load_cleanup(struct bpf_object *obj) |
9036 | 10.9k | { |
9037 | | /* clean up fd_array */ |
9038 | 10.9k | zfree(&obj->fd_array); |
9039 | | |
9040 | | /* clean up BTF */ |
9041 | 10.9k | bpf_object_cleanup_btf(obj); |
9042 | 10.9k | } |
9043 | | |
9044 | | static int bpf_object_prepare(struct bpf_object *obj, const char *target_btf_path) |
9045 | 0 | { |
9046 | 0 | int err; |
9047 | |
|
9048 | 0 | if (obj->state >= OBJ_PREPARED) { |
9049 | 0 | pr_warn("object '%s': prepare loading can't be attempted twice\n", obj->name); |
9050 | 0 | return -EINVAL; |
9051 | 0 | } |
9052 | | |
9053 | 0 | err = bpf_object_prepare_token(obj); |
9054 | 0 | err = err ? : bpf_object__probe_loading(obj); |
9055 | 0 | err = err ? : bpf_object__load_vmlinux_btf(obj, false); |
9056 | 0 | err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); |
9057 | 0 | err = err ? : bpf_object__sanitize_maps(obj); |
9058 | 0 | err = err ? : bpf_object__init_kern_struct_ops_maps(obj); |
9059 | 0 | err = err ? : bpf_object_adjust_struct_ops_autoload(obj); |
9060 | 0 | err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path); |
9061 | 0 | err = err ? : bpf_object__sanitize_and_load_btf(obj); |
9062 | 0 | err = err ? : bpf_object__create_maps(obj); |
9063 | 0 | err = err ? : bpf_object_prepare_progs(obj); |
9064 | |
|
9065 | 0 | if (err) { |
9066 | 0 | bpf_object_unpin(obj); |
9067 | 0 | bpf_object_unload(obj); |
9068 | 0 | obj->state = OBJ_LOADED; |
9069 | 0 | return err; |
9070 | 0 | } |
9071 | | |
9072 | 0 | obj->state = OBJ_PREPARED; |
9073 | 0 | return 0; |
9074 | 0 | } |
9075 | | |
9076 | | static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path) |
9077 | 0 | { |
9078 | 0 | int err; |
9079 | |
|
9080 | 0 | if (!obj) |
9081 | 0 | return libbpf_err(-EINVAL); |
9082 | | |
9083 | 0 | if (obj->state >= OBJ_LOADED) { |
9084 | 0 | pr_warn("object '%s': load can't be attempted twice\n", obj->name); |
9085 | 0 | return libbpf_err(-EINVAL); |
9086 | 0 | } |
9087 | | |
9088 | | /* Disallow kernel loading programs of non-native endianness but |
9089 | | * permit cross-endian creation of "light skeleton". |
9090 | | */ |
9091 | 0 | if (obj->gen_loader) { |
9092 | 0 | bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); |
9093 | 0 | } else if (!is_native_endianness(obj)) { |
9094 | 0 | pr_warn("object '%s': loading non-native endianness is unsupported\n", obj->name); |
9095 | 0 | return libbpf_err(-LIBBPF_ERRNO__ENDIAN); |
9096 | 0 | } |
9097 | | |
9098 | 0 | if (obj->state < OBJ_PREPARED) { |
9099 | 0 | err = bpf_object_prepare(obj, target_btf_path); |
9100 | 0 | if (err) |
9101 | 0 | return libbpf_err(err); |
9102 | 0 | } |
9103 | 0 | err = bpf_object__load_progs(obj, extra_log_level); |
9104 | 0 | err = err ? : bpf_object_init_prog_arrays(obj); |
9105 | 0 | err = err ? : bpf_object_prepare_struct_ops(obj); |
9106 | |
|
9107 | 0 | if (obj->gen_loader) { |
9108 | | /* reset FDs */ |
9109 | 0 | if (obj->btf) |
9110 | 0 | btf__set_fd(obj->btf, -1); |
9111 | 0 | if (!err) |
9112 | 0 | err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps); |
9113 | 0 | } |
9114 | |
|
9115 | 0 | bpf_object_post_load_cleanup(obj); |
9116 | 0 | obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */ |
9117 | |
|
9118 | 0 | if (err) { |
9119 | 0 | bpf_object_unpin(obj); |
9120 | 0 | bpf_object_unload(obj); |
9121 | 0 | pr_warn("failed to load object '%s'\n", obj->path); |
9122 | 0 | return libbpf_err(err); |
9123 | 0 | } |
9124 | | |
9125 | 0 | return 0; |
9126 | 0 | } |
9127 | | |
9128 | | int bpf_object__prepare(struct bpf_object *obj) |
9129 | 0 | { |
9130 | 0 | return libbpf_err(bpf_object_prepare(obj, NULL)); |
9131 | 0 | } |
9132 | | |
9133 | | int bpf_object__load(struct bpf_object *obj) |
9134 | 0 | { |
9135 | 0 | return bpf_object_load(obj, 0, NULL); |
9136 | 0 | } |
9137 | | |
9138 | | static int make_parent_dir(const char *path) |
9139 | 0 | { |
9140 | 0 | char *dname, *dir; |
9141 | 0 | int err = 0; |
9142 | |
|
9143 | 0 | dname = strdup(path); |
9144 | 0 | if (dname == NULL) |
9145 | 0 | return -ENOMEM; |
9146 | | |
9147 | 0 | dir = dirname(dname); |
9148 | 0 | if (mkdir(dir, 0700) && errno != EEXIST) |
9149 | 0 | err = -errno; |
9150 | |
|
9151 | 0 | free(dname); |
9152 | 0 | if (err) { |
9153 | 0 | pr_warn("failed to mkdir %s: %s\n", path, errstr(err)); |
9154 | 0 | } |
9155 | 0 | return err; |
9156 | 0 | } |
9157 | | |
9158 | | static int check_path(const char *path) |
9159 | 0 | { |
9160 | 0 | struct statfs st_fs; |
9161 | 0 | char *dname, *dir; |
9162 | 0 | int err = 0; |
9163 | |
|
9164 | 0 | if (path == NULL) |
9165 | 0 | return -EINVAL; |
9166 | | |
9167 | 0 | dname = strdup(path); |
9168 | 0 | if (dname == NULL) |
9169 | 0 | return -ENOMEM; |
9170 | | |
9171 | 0 | dir = dirname(dname); |
9172 | 0 | if (statfs(dir, &st_fs)) { |
9173 | 0 | pr_warn("failed to statfs %s: %s\n", dir, errstr(errno)); |
9174 | 0 | err = -errno; |
9175 | 0 | } |
9176 | 0 | free(dname); |
9177 | |
|
9178 | 0 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
9179 | 0 | pr_warn("specified path %s is not on BPF FS\n", path); |
9180 | 0 | err = -EINVAL; |
9181 | 0 | } |
9182 | |
|
9183 | 0 | return err; |
9184 | 0 | } |
9185 | | |
9186 | | int bpf_program__pin(struct bpf_program *prog, const char *path) |
9187 | 0 | { |
9188 | 0 | int err; |
9189 | |
|
9190 | 0 | if (prog->fd < 0) { |
9191 | 0 | pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name); |
9192 | 0 | return libbpf_err(-EINVAL); |
9193 | 0 | } |
9194 | | |
9195 | 0 | err = make_parent_dir(path); |
9196 | 0 | if (err) |
9197 | 0 | return libbpf_err(err); |
9198 | | |
9199 | 0 | err = check_path(path); |
9200 | 0 | if (err) |
9201 | 0 | return libbpf_err(err); |
9202 | | |
9203 | 0 | if (bpf_obj_pin(prog->fd, path)) { |
9204 | 0 | err = -errno; |
9205 | 0 | pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, errstr(err)); |
9206 | 0 | return libbpf_err(err); |
9207 | 0 | } |
9208 | | |
9209 | 0 | pr_debug("prog '%s': pinned at '%s'\n", prog->name, path); |
9210 | 0 | return 0; |
9211 | 0 | } |
9212 | | |
9213 | | int bpf_program__unpin(struct bpf_program *prog, const char *path) |
9214 | 0 | { |
9215 | 0 | int err; |
9216 | |
|
9217 | 0 | if (prog->fd < 0) { |
9218 | 0 | pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name); |
9219 | 0 | return libbpf_err(-EINVAL); |
9220 | 0 | } |
9221 | | |
9222 | 0 | err = check_path(path); |
9223 | 0 | if (err) |
9224 | 0 | return libbpf_err(err); |
9225 | | |
9226 | 0 | err = unlink(path); |
9227 | 0 | if (err) |
9228 | 0 | return libbpf_err(-errno); |
9229 | | |
9230 | 0 | pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path); |
9231 | 0 | return 0; |
9232 | 0 | } |
9233 | | |
9234 | | int bpf_map__pin(struct bpf_map *map, const char *path) |
9235 | 0 | { |
9236 | 0 | int err; |
9237 | |
|
9238 | 0 | if (map == NULL) { |
9239 | 0 | pr_warn("invalid map pointer\n"); |
9240 | 0 | return libbpf_err(-EINVAL); |
9241 | 0 | } |
9242 | | |
9243 | 0 | if (map->fd < 0) { |
9244 | 0 | pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map->name); |
9245 | 0 | return libbpf_err(-EINVAL); |
9246 | 0 | } |
9247 | | |
9248 | 0 | if (map->pin_path) { |
9249 | 0 | if (path && strcmp(path, map->pin_path)) { |
9250 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
9251 | 0 | bpf_map__name(map), map->pin_path, path); |
9252 | 0 | return libbpf_err(-EINVAL); |
9253 | 0 | } else if (map->pinned) { |
9254 | 0 | pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", |
9255 | 0 | bpf_map__name(map), map->pin_path); |
9256 | 0 | return 0; |
9257 | 0 | } |
9258 | 0 | } else { |
9259 | 0 | if (!path) { |
9260 | 0 | pr_warn("missing a path to pin map '%s' at\n", |
9261 | 0 | bpf_map__name(map)); |
9262 | 0 | return libbpf_err(-EINVAL); |
9263 | 0 | } else if (map->pinned) { |
9264 | 0 | pr_warn("map '%s' already pinned\n", bpf_map__name(map)); |
9265 | 0 | return libbpf_err(-EEXIST); |
9266 | 0 | } |
9267 | | |
9268 | 0 | map->pin_path = strdup(path); |
9269 | 0 | if (!map->pin_path) { |
9270 | 0 | err = -errno; |
9271 | 0 | goto out_err; |
9272 | 0 | } |
9273 | 0 | } |
9274 | | |
9275 | 0 | err = make_parent_dir(map->pin_path); |
9276 | 0 | if (err) |
9277 | 0 | return libbpf_err(err); |
9278 | | |
9279 | 0 | err = check_path(map->pin_path); |
9280 | 0 | if (err) |
9281 | 0 | return libbpf_err(err); |
9282 | | |
9283 | 0 | if (bpf_obj_pin(map->fd, map->pin_path)) { |
9284 | 0 | err = -errno; |
9285 | 0 | goto out_err; |
9286 | 0 | } |
9287 | | |
9288 | 0 | map->pinned = true; |
9289 | 0 | pr_debug("pinned map '%s'\n", map->pin_path); |
9290 | |
|
9291 | 0 | return 0; |
9292 | | |
9293 | 0 | out_err: |
9294 | 0 | pr_warn("failed to pin map: %s\n", errstr(err)); |
9295 | 0 | return libbpf_err(err); |
9296 | 0 | } |
9297 | | |
9298 | | int bpf_map__unpin(struct bpf_map *map, const char *path) |
9299 | 0 | { |
9300 | 0 | int err; |
9301 | |
|
9302 | 0 | if (map == NULL) { |
9303 | 0 | pr_warn("invalid map pointer\n"); |
9304 | 0 | return libbpf_err(-EINVAL); |
9305 | 0 | } |
9306 | | |
9307 | 0 | if (map->pin_path) { |
9308 | 0 | if (path && strcmp(path, map->pin_path)) { |
9309 | 0 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
9310 | 0 | bpf_map__name(map), map->pin_path, path); |
9311 | 0 | return libbpf_err(-EINVAL); |
9312 | 0 | } |
9313 | 0 | path = map->pin_path; |
9314 | 0 | } else if (!path) { |
9315 | 0 | pr_warn("no path to unpin map '%s' from\n", |
9316 | 0 | bpf_map__name(map)); |
9317 | 0 | return libbpf_err(-EINVAL); |
9318 | 0 | } |
9319 | | |
9320 | 0 | err = check_path(path); |
9321 | 0 | if (err) |
9322 | 0 | return libbpf_err(err); |
9323 | | |
9324 | 0 | err = unlink(path); |
9325 | 0 | if (err != 0) |
9326 | 0 | return libbpf_err(-errno); |
9327 | | |
9328 | 0 | map->pinned = false; |
9329 | 0 | pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path); |
9330 | |
|
9331 | 0 | return 0; |
9332 | 0 | } |
9333 | | |
9334 | | int bpf_map__set_pin_path(struct bpf_map *map, const char *path) |
9335 | 0 | { |
9336 | 0 | char *new = NULL; |
9337 | |
|
9338 | 0 | if (path) { |
9339 | 0 | new = strdup(path); |
9340 | 0 | if (!new) |
9341 | 0 | return libbpf_err(-errno); |
9342 | 0 | } |
9343 | | |
9344 | 0 | free(map->pin_path); |
9345 | 0 | map->pin_path = new; |
9346 | 0 | return 0; |
9347 | 0 | } |
9348 | | |
9349 | | __alias(bpf_map__pin_path) |
9350 | | const char *bpf_map__get_pin_path(const struct bpf_map *map); |
9351 | | |
9352 | | const char *bpf_map__pin_path(const struct bpf_map *map) |
9353 | 0 | { |
9354 | 0 | return map->pin_path; |
9355 | 0 | } |
9356 | | |
9357 | | bool bpf_map__is_pinned(const struct bpf_map *map) |
9358 | 0 | { |
9359 | 0 | return map->pinned; |
9360 | 0 | } |
9361 | | |
9362 | | static void sanitize_pin_path(char *s) |
9363 | 0 | { |
9364 | | /* bpffs disallows periods in path names */ |
9365 | 0 | while (*s) { |
9366 | 0 | if (*s == '.') |
9367 | 0 | *s = '_'; |
9368 | 0 | s++; |
9369 | 0 | } |
9370 | 0 | } |
9371 | | |
9372 | | int bpf_object__pin_maps(struct bpf_object *obj, const char *path) |
9373 | 0 | { |
9374 | 0 | struct bpf_map *map; |
9375 | 0 | int err; |
9376 | |
|
9377 | 0 | if (!obj) |
9378 | 0 | return libbpf_err(-ENOENT); |
9379 | | |
9380 | 0 | if (obj->state < OBJ_PREPARED) { |
9381 | 0 | pr_warn("object not yet loaded; load it first\n"); |
9382 | 0 | return libbpf_err(-ENOENT); |
9383 | 0 | } |
9384 | | |
9385 | 0 | bpf_object__for_each_map(map, obj) { |
9386 | 0 | char *pin_path = NULL; |
9387 | 0 | char buf[PATH_MAX]; |
9388 | |
|
9389 | 0 | if (!map->autocreate) |
9390 | 0 | continue; |
9391 | | |
9392 | 0 | if (path) { |
9393 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
9394 | 0 | if (err) |
9395 | 0 | goto err_unpin_maps; |
9396 | 0 | sanitize_pin_path(buf); |
9397 | 0 | pin_path = buf; |
9398 | 0 | } else if (!map->pin_path) { |
9399 | 0 | continue; |
9400 | 0 | } |
9401 | | |
9402 | 0 | err = bpf_map__pin(map, pin_path); |
9403 | 0 | if (err) |
9404 | 0 | goto err_unpin_maps; |
9405 | 0 | } |
9406 | | |
9407 | 0 | return 0; |
9408 | | |
9409 | 0 | err_unpin_maps: |
9410 | 0 | while ((map = bpf_object__prev_map(obj, map))) { |
9411 | 0 | if (!map->pin_path) |
9412 | 0 | continue; |
9413 | | |
9414 | 0 | bpf_map__unpin(map, NULL); |
9415 | 0 | } |
9416 | |
|
9417 | 0 | return libbpf_err(err); |
9418 | 0 | } |
9419 | | |
9420 | | int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) |
9421 | 0 | { |
9422 | 0 | struct bpf_map *map; |
9423 | 0 | int err; |
9424 | |
|
9425 | 0 | if (!obj) |
9426 | 0 | return libbpf_err(-ENOENT); |
9427 | | |
9428 | 0 | bpf_object__for_each_map(map, obj) { |
9429 | 0 | char *pin_path = NULL; |
9430 | 0 | char buf[PATH_MAX]; |
9431 | |
|
9432 | 0 | if (path) { |
9433 | 0 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
9434 | 0 | if (err) |
9435 | 0 | return libbpf_err(err); |
9436 | 0 | sanitize_pin_path(buf); |
9437 | 0 | pin_path = buf; |
9438 | 0 | } else if (!map->pin_path) { |
9439 | 0 | continue; |
9440 | 0 | } |
9441 | | |
9442 | 0 | err = bpf_map__unpin(map, pin_path); |
9443 | 0 | if (err) |
9444 | 0 | return libbpf_err(err); |
9445 | 0 | } |
9446 | | |
9447 | 0 | return 0; |
9448 | 0 | } |
9449 | | |
9450 | | int bpf_object__pin_programs(struct bpf_object *obj, const char *path) |
9451 | 0 | { |
9452 | 0 | struct bpf_program *prog; |
9453 | 0 | char buf[PATH_MAX]; |
9454 | 0 | int err; |
9455 | |
|
9456 | 0 | if (!obj) |
9457 | 0 | return libbpf_err(-ENOENT); |
9458 | | |
9459 | 0 | if (obj->state < OBJ_LOADED) { |
9460 | 0 | pr_warn("object not yet loaded; load it first\n"); |
9461 | 0 | return libbpf_err(-ENOENT); |
9462 | 0 | } |
9463 | | |
9464 | 0 | bpf_object__for_each_program(prog, obj) { |
9465 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
9466 | 0 | if (err) |
9467 | 0 | goto err_unpin_programs; |
9468 | | |
9469 | 0 | err = bpf_program__pin(prog, buf); |
9470 | 0 | if (err) |
9471 | 0 | goto err_unpin_programs; |
9472 | 0 | } |
9473 | | |
9474 | 0 | return 0; |
9475 | | |
9476 | 0 | err_unpin_programs: |
9477 | 0 | while ((prog = bpf_object__prev_program(obj, prog))) { |
9478 | 0 | if (pathname_concat(buf, sizeof(buf), path, prog->name)) |
9479 | 0 | continue; |
9480 | | |
9481 | 0 | bpf_program__unpin(prog, buf); |
9482 | 0 | } |
9483 | |
|
9484 | 0 | return libbpf_err(err); |
9485 | 0 | } |
9486 | | |
9487 | | int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) |
9488 | 0 | { |
9489 | 0 | struct bpf_program *prog; |
9490 | 0 | int err; |
9491 | |
|
9492 | 0 | if (!obj) |
9493 | 0 | return libbpf_err(-ENOENT); |
9494 | | |
9495 | 0 | bpf_object__for_each_program(prog, obj) { |
9496 | 0 | char buf[PATH_MAX]; |
9497 | |
|
9498 | 0 | err = pathname_concat(buf, sizeof(buf), path, prog->name); |
9499 | 0 | if (err) |
9500 | 0 | return libbpf_err(err); |
9501 | | |
9502 | 0 | err = bpf_program__unpin(prog, buf); |
9503 | 0 | if (err) |
9504 | 0 | return libbpf_err(err); |
9505 | 0 | } |
9506 | | |
9507 | 0 | return 0; |
9508 | 0 | } |
9509 | | |
9510 | | int bpf_object__pin(struct bpf_object *obj, const char *path) |
9511 | 0 | { |
9512 | 0 | int err; |
9513 | |
|
9514 | 0 | err = bpf_object__pin_maps(obj, path); |
9515 | 0 | if (err) |
9516 | 0 | return libbpf_err(err); |
9517 | | |
9518 | 0 | err = bpf_object__pin_programs(obj, path); |
9519 | 0 | if (err) { |
9520 | 0 | bpf_object__unpin_maps(obj, path); |
9521 | 0 | return libbpf_err(err); |
9522 | 0 | } |
9523 | | |
9524 | 0 | return 0; |
9525 | 0 | } |
9526 | | |
9527 | | int bpf_object__unpin(struct bpf_object *obj, const char *path) |
9528 | 0 | { |
9529 | 0 | int err; |
9530 | |
|
9531 | 0 | err = bpf_object__unpin_programs(obj, path); |
9532 | 0 | if (err) |
9533 | 0 | return libbpf_err(err); |
9534 | | |
9535 | 0 | err = bpf_object__unpin_maps(obj, path); |
9536 | 0 | if (err) |
9537 | 0 | return libbpf_err(err); |
9538 | | |
9539 | 0 | return 0; |
9540 | 0 | } |
9541 | | |
9542 | | static void bpf_map__destroy(struct bpf_map *map) |
9543 | 2.06k | { |
9544 | 2.06k | if (map->inner_map) { |
9545 | 0 | bpf_map__destroy(map->inner_map); |
9546 | 0 | zfree(&map->inner_map); |
9547 | 0 | } |
9548 | | |
9549 | 2.06k | zfree(&map->init_slots); |
9550 | 2.06k | map->init_slots_sz = 0; |
9551 | | |
9552 | 2.06k | if (map->mmaped && map->mmaped != map->obj->arena_data) |
9553 | 1.94k | munmap(map->mmaped, bpf_map_mmap_sz(map)); |
9554 | 2.06k | map->mmaped = NULL; |
9555 | | |
9556 | 2.06k | if (map->st_ops) { |
9557 | 96 | zfree(&map->st_ops->data); |
9558 | 96 | zfree(&map->st_ops->progs); |
9559 | 96 | zfree(&map->st_ops->kern_func_off); |
9560 | 96 | zfree(&map->st_ops); |
9561 | 96 | } |
9562 | | |
9563 | 2.06k | zfree(&map->name); |
9564 | 2.06k | zfree(&map->real_name); |
9565 | 2.06k | zfree(&map->pin_path); |
9566 | | |
9567 | 2.06k | if (map->fd >= 0) |
9568 | 0 | zclose(map->fd); |
9569 | 2.06k | } |
9570 | | |
9571 | | void bpf_object__close(struct bpf_object *obj) |
9572 | 10.9k | { |
9573 | 10.9k | size_t i; |
9574 | | |
9575 | 10.9k | if (IS_ERR_OR_NULL(obj)) |
9576 | 0 | return; |
9577 | | |
9578 | | /* |
9579 | | * if user called bpf_object__prepare() without ever getting to |
9580 | | * bpf_object__load(), we need to clean up stuff that is normally |
9581 | | * cleaned up at the end of loading step |
9582 | | */ |
9583 | 10.9k | bpf_object_post_load_cleanup(obj); |
9584 | | |
9585 | 10.9k | usdt_manager_free(obj->usdt_man); |
9586 | 10.9k | obj->usdt_man = NULL; |
9587 | | |
9588 | 10.9k | bpf_gen__free(obj->gen_loader); |
9589 | 10.9k | bpf_object__elf_finish(obj); |
9590 | 10.9k | bpf_object_unload(obj); |
9591 | 10.9k | btf__free(obj->btf); |
9592 | 10.9k | btf__free(obj->btf_vmlinux); |
9593 | 10.9k | btf_ext__free(obj->btf_ext); |
9594 | | |
9595 | 13.0k | for (i = 0; i < obj->nr_maps; i++) |
9596 | 2.06k | bpf_map__destroy(&obj->maps[i]); |
9597 | | |
9598 | 10.9k | zfree(&obj->btf_custom_path); |
9599 | 10.9k | zfree(&obj->kconfig); |
9600 | | |
9601 | 12.0k | for (i = 0; i < obj->nr_extern; i++) { |
9602 | 1.11k | zfree(&obj->externs[i].name); |
9603 | 1.11k | zfree(&obj->externs[i].essent_name); |
9604 | 1.11k | } |
9605 | | |
9606 | 10.9k | zfree(&obj->externs); |
9607 | 10.9k | obj->nr_extern = 0; |
9608 | | |
9609 | 10.9k | zfree(&obj->maps); |
9610 | 10.9k | obj->nr_maps = 0; |
9611 | | |
9612 | 10.9k | if (obj->programs && obj->nr_programs) { |
9613 | 11.1k | for (i = 0; i < obj->nr_programs; i++) |
9614 | 10.4k | bpf_program__exit(&obj->programs[i]); |
9615 | 695 | } |
9616 | 10.9k | zfree(&obj->programs); |
9617 | | |
9618 | 10.9k | zfree(&obj->feat_cache); |
9619 | 10.9k | zfree(&obj->token_path); |
9620 | 10.9k | if (obj->token_fd > 0) |
9621 | 0 | close(obj->token_fd); |
9622 | | |
9623 | 10.9k | zfree(&obj->arena_data); |
9624 | | |
9625 | 10.9k | zfree(&obj->jumptables_data); |
9626 | 10.9k | obj->jumptables_data_sz = 0; |
9627 | | |
9628 | 10.9k | for (i = 0; i < obj->jumptable_map_cnt; i++) |
9629 | 0 | close(obj->jumptable_maps[i].fd); |
9630 | 10.9k | zfree(&obj->jumptable_maps); |
9631 | | |
9632 | 10.9k | free(obj); |
9633 | 10.9k | } |
9634 | | |
9635 | | const char *bpf_object__name(const struct bpf_object *obj) |
9636 | 0 | { |
9637 | 0 | return obj ? obj->name : libbpf_err_ptr(-EINVAL); |
9638 | 0 | } |
9639 | | |
9640 | | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
9641 | 0 | { |
9642 | 0 | return obj ? obj->kern_version : 0; |
9643 | 0 | } |
9644 | | |
9645 | | int bpf_object__token_fd(const struct bpf_object *obj) |
9646 | 0 | { |
9647 | 0 | return obj->token_fd ?: -1; |
9648 | 0 | } |
9649 | | |
9650 | | struct btf *bpf_object__btf(const struct bpf_object *obj) |
9651 | 0 | { |
9652 | 0 | return obj ? obj->btf : NULL; |
9653 | 0 | } |
9654 | | |
9655 | | int bpf_object__btf_fd(const struct bpf_object *obj) |
9656 | 0 | { |
9657 | 0 | return obj->btf ? btf__fd(obj->btf) : -1; |
9658 | 0 | } |
9659 | | |
9660 | | int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version) |
9661 | 0 | { |
9662 | 0 | if (obj->state >= OBJ_LOADED) |
9663 | 0 | return libbpf_err(-EINVAL); |
9664 | | |
9665 | 0 | obj->kern_version = kern_version; |
9666 | |
|
9667 | 0 | return 0; |
9668 | 0 | } |
9669 | | |
9670 | | int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts) |
9671 | 0 | { |
9672 | 0 | struct bpf_gen *gen; |
9673 | |
|
9674 | 0 | if (!opts) |
9675 | 0 | return libbpf_err(-EFAULT); |
9676 | 0 | if (!OPTS_VALID(opts, gen_loader_opts)) |
9677 | 0 | return libbpf_err(-EINVAL); |
9678 | 0 | gen = calloc(1, sizeof(*gen)); |
9679 | 0 | if (!gen) |
9680 | 0 | return libbpf_err(-ENOMEM); |
9681 | 0 | gen->opts = opts; |
9682 | 0 | gen->swapped_endian = !is_native_endianness(obj); |
9683 | 0 | obj->gen_loader = gen; |
9684 | 0 | return 0; |
9685 | 0 | } |
9686 | | |
9687 | | static struct bpf_program * |
9688 | | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
9689 | | bool forward) |
9690 | 11.8k | { |
9691 | 11.8k | size_t nr_programs = obj->nr_programs; |
9692 | 11.8k | ssize_t idx; |
9693 | | |
9694 | 11.8k | if (!nr_programs) |
9695 | 1.49k | return NULL; |
9696 | | |
9697 | 10.3k | if (!p) |
9698 | | /* Iter from the beginning */ |
9699 | 587 | return forward ? &obj->programs[0] : |
9700 | 587 | &obj->programs[nr_programs - 1]; |
9701 | | |
9702 | 9.72k | if (p->obj != obj) { |
9703 | 0 | pr_warn("error: program handler doesn't match object\n"); |
9704 | 0 | return errno = EINVAL, NULL; |
9705 | 0 | } |
9706 | | |
9707 | 9.72k | idx = (p - obj->programs) + (forward ? 1 : -1); |
9708 | 9.72k | if (idx >= obj->nr_programs || idx < 0) |
9709 | 587 | return NULL; |
9710 | 9.14k | return &obj->programs[idx]; |
9711 | 9.72k | } |
9712 | | |
9713 | | struct bpf_program * |
9714 | | bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev) |
9715 | 10.2k | { |
9716 | 10.2k | struct bpf_program *prog = prev; |
9717 | | |
9718 | 11.8k | do { |
9719 | 11.8k | prog = __bpf_program__iter(prog, obj, true); |
9720 | 11.8k | } while (prog && prog_is_subprog(obj, prog)); |
9721 | | |
9722 | 10.2k | return prog; |
9723 | 10.2k | } |
9724 | | |
9725 | | struct bpf_program * |
9726 | | bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next) |
9727 | 0 | { |
9728 | 0 | struct bpf_program *prog = next; |
9729 | |
|
9730 | 0 | do { |
9731 | 0 | prog = __bpf_program__iter(prog, obj, false); |
9732 | 0 | } while (prog && prog_is_subprog(obj, prog)); |
9733 | |
|
9734 | 0 | return prog; |
9735 | 0 | } |
9736 | | |
9737 | | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
9738 | 0 | { |
9739 | 0 | prog->prog_ifindex = ifindex; |
9740 | 0 | } |
9741 | | |
9742 | | const char *bpf_program__name(const struct bpf_program *prog) |
9743 | 0 | { |
9744 | 0 | return prog->name; |
9745 | 0 | } |
9746 | | |
9747 | | const char *bpf_program__section_name(const struct bpf_program *prog) |
9748 | 0 | { |
9749 | 0 | return prog->sec_name; |
9750 | 0 | } |
9751 | | |
9752 | | bool bpf_program__autoload(const struct bpf_program *prog) |
9753 | 0 | { |
9754 | 0 | return prog->autoload; |
9755 | 0 | } |
9756 | | |
9757 | | int bpf_program__set_autoload(struct bpf_program *prog, bool autoload) |
9758 | 0 | { |
9759 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9760 | 0 | return libbpf_err(-EINVAL); |
9761 | | |
9762 | 0 | prog->autoload = autoload; |
9763 | 0 | return 0; |
9764 | 0 | } |
9765 | | |
9766 | | bool bpf_program__autoattach(const struct bpf_program *prog) |
9767 | 0 | { |
9768 | 0 | return prog->autoattach; |
9769 | 0 | } |
9770 | | |
9771 | | void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach) |
9772 | 0 | { |
9773 | 0 | prog->autoattach = autoattach; |
9774 | 0 | } |
9775 | | |
9776 | | const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog) |
9777 | 0 | { |
9778 | 0 | return prog->insns; |
9779 | 0 | } |
9780 | | |
9781 | | size_t bpf_program__insn_cnt(const struct bpf_program *prog) |
9782 | 0 | { |
9783 | 0 | return prog->insns_cnt; |
9784 | 0 | } |
9785 | | |
9786 | | int bpf_program__set_insns(struct bpf_program *prog, |
9787 | | struct bpf_insn *new_insns, size_t new_insn_cnt) |
9788 | 0 | { |
9789 | 0 | struct bpf_insn *insns; |
9790 | |
|
9791 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9792 | 0 | return libbpf_err(-EBUSY); |
9793 | | |
9794 | 0 | insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns)); |
9795 | | /* NULL is a valid return from reallocarray if the new count is zero */ |
9796 | 0 | if (!insns && new_insn_cnt) { |
9797 | 0 | pr_warn("prog '%s': failed to realloc prog code\n", prog->name); |
9798 | 0 | return libbpf_err(-ENOMEM); |
9799 | 0 | } |
9800 | 0 | memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns)); |
9801 | |
|
9802 | 0 | prog->insns = insns; |
9803 | 0 | prog->insns_cnt = new_insn_cnt; |
9804 | 0 | return 0; |
9805 | 0 | } |
9806 | | |
9807 | | int bpf_program__fd(const struct bpf_program *prog) |
9808 | 0 | { |
9809 | 0 | if (!prog) |
9810 | 0 | return libbpf_err(-EINVAL); |
9811 | | |
9812 | 0 | if (prog->fd < 0) |
9813 | 0 | return libbpf_err(-ENOENT); |
9814 | | |
9815 | 0 | return prog->fd; |
9816 | 0 | } |
9817 | | |
9818 | | __alias(bpf_program__type) |
9819 | | enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog); |
9820 | | |
9821 | | enum bpf_prog_type bpf_program__type(const struct bpf_program *prog) |
9822 | 0 | { |
9823 | 0 | return prog->type; |
9824 | 0 | } |
9825 | | |
9826 | | static size_t custom_sec_def_cnt; |
9827 | | static struct bpf_sec_def *custom_sec_defs; |
9828 | | static struct bpf_sec_def custom_fallback_def; |
9829 | | static bool has_custom_fallback_def; |
9830 | | static int last_custom_sec_def_handler_id; |
9831 | | |
9832 | | int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) |
9833 | 0 | { |
9834 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9835 | 0 | return libbpf_err(-EBUSY); |
9836 | | |
9837 | | /* if type is not changed, do nothing */ |
9838 | 0 | if (prog->type == type) |
9839 | 0 | return 0; |
9840 | | |
9841 | 0 | prog->type = type; |
9842 | | |
9843 | | /* If a program type was changed, we need to reset associated SEC() |
9844 | | * handler, as it will be invalid now. The only exception is a generic |
9845 | | * fallback handler, which by definition is program type-agnostic and |
9846 | | * is a catch-all custom handler, optionally set by the application, |
9847 | | * so should be able to handle any type of BPF program. |
9848 | | */ |
9849 | 0 | if (prog->sec_def != &custom_fallback_def) |
9850 | 0 | prog->sec_def = NULL; |
9851 | 0 | return 0; |
9852 | 0 | } |
9853 | | |
9854 | | __alias(bpf_program__expected_attach_type) |
9855 | | enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog); |
9856 | | |
9857 | | enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog) |
9858 | 0 | { |
9859 | 0 | return prog->expected_attach_type; |
9860 | 0 | } |
9861 | | |
9862 | | int bpf_program__set_expected_attach_type(struct bpf_program *prog, |
9863 | | enum bpf_attach_type type) |
9864 | 0 | { |
9865 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9866 | 0 | return libbpf_err(-EBUSY); |
9867 | | |
9868 | 0 | prog->expected_attach_type = type; |
9869 | 0 | return 0; |
9870 | 0 | } |
9871 | | |
9872 | | __u32 bpf_program__flags(const struct bpf_program *prog) |
9873 | 0 | { |
9874 | 0 | return prog->prog_flags; |
9875 | 0 | } |
9876 | | |
9877 | | int bpf_program__set_flags(struct bpf_program *prog, __u32 flags) |
9878 | 0 | { |
9879 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9880 | 0 | return libbpf_err(-EBUSY); |
9881 | | |
9882 | 0 | prog->prog_flags = flags; |
9883 | 0 | return 0; |
9884 | 0 | } |
9885 | | |
9886 | | __u32 bpf_program__log_level(const struct bpf_program *prog) |
9887 | 0 | { |
9888 | 0 | return prog->log_level; |
9889 | 0 | } |
9890 | | |
9891 | | int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level) |
9892 | 0 | { |
9893 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9894 | 0 | return libbpf_err(-EBUSY); |
9895 | | |
9896 | 0 | prog->log_level = log_level; |
9897 | 0 | return 0; |
9898 | 0 | } |
9899 | | |
9900 | | const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size) |
9901 | 0 | { |
9902 | 0 | *log_size = prog->log_size; |
9903 | 0 | return prog->log_buf; |
9904 | 0 | } |
9905 | | |
9906 | | int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size) |
9907 | 0 | { |
9908 | 0 | if (log_size && !log_buf) |
9909 | 0 | return libbpf_err(-EINVAL); |
9910 | 0 | if (prog->log_size > UINT_MAX) |
9911 | 0 | return libbpf_err(-EINVAL); |
9912 | 0 | if (prog->obj->state >= OBJ_LOADED) |
9913 | 0 | return libbpf_err(-EBUSY); |
9914 | | |
9915 | 0 | prog->log_buf = log_buf; |
9916 | 0 | prog->log_size = log_size; |
9917 | 0 | return 0; |
9918 | 0 | } |
9919 | | |
9920 | | struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog) |
9921 | 0 | { |
9922 | 0 | if (prog->func_info_rec_size != sizeof(struct bpf_func_info)) |
9923 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
9924 | 0 | return prog->func_info; |
9925 | 0 | } |
9926 | | |
9927 | | __u32 bpf_program__func_info_cnt(const struct bpf_program *prog) |
9928 | 0 | { |
9929 | 0 | return prog->func_info_cnt; |
9930 | 0 | } |
9931 | | |
9932 | | struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog) |
9933 | 0 | { |
9934 | 0 | if (prog->line_info_rec_size != sizeof(struct bpf_line_info)) |
9935 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
9936 | 0 | return prog->line_info; |
9937 | 0 | } |
9938 | | |
9939 | | __u32 bpf_program__line_info_cnt(const struct bpf_program *prog) |
9940 | 0 | { |
9941 | 0 | return prog->line_info_cnt; |
9942 | 0 | } |
9943 | | |
9944 | | int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts *opts) |
9945 | 0 | { |
9946 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, attr); |
9947 | 0 | struct bpf_object *obj; |
9948 | 0 | const void *info; |
9949 | 0 | __u32 info_cnt, info_rec_size; |
9950 | 0 | int err, fd, prog_btf_fd; |
9951 | |
|
9952 | 0 | if (!prog) |
9953 | 0 | return libbpf_err(-EINVAL); |
9954 | | |
9955 | 0 | if (!OPTS_VALID(opts, bpf_prog_load_opts)) |
9956 | 0 | return libbpf_err(-EINVAL); |
9957 | | |
9958 | 0 | obj = prog->obj; |
9959 | 0 | if (obj->state < OBJ_PREPARED) |
9960 | 0 | return libbpf_err(-EINVAL); |
9961 | | |
9962 | | /* |
9963 | | * Caller-provided opts take priority; fall back to |
9964 | | * prog/object defaults when the caller leaves them zero. |
9965 | | */ |
9966 | 0 | attr.attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0) ?: prog->attach_prog_fd; |
9967 | 0 | attr.prog_flags = OPTS_GET(opts, prog_flags, 0) ?: prog->prog_flags; |
9968 | 0 | attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0) ?: prog->prog_ifindex; |
9969 | 0 | attr.kern_version = OPTS_GET(opts, kern_version, 0) ?: obj->kern_version; |
9970 | 0 | attr.fd_array = OPTS_GET(opts, fd_array, NULL) ?: obj->fd_array; |
9971 | 0 | attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0) ?: obj->fd_array_cnt; |
9972 | 0 | attr.token_fd = OPTS_GET(opts, token_fd, 0) ?: obj->token_fd; |
9973 | 0 | if (attr.token_fd) |
9974 | 0 | attr.prog_flags |= BPF_F_TOKEN_FD; |
9975 | |
|
9976 | 0 | prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0); |
9977 | 0 | if (!prog_btf_fd && obj->btf) |
9978 | 0 | prog_btf_fd = btf__fd(obj->btf); |
9979 | | |
9980 | | /* BTF func/line info: only pass if kernel supports it */ |
9981 | 0 | if (kernel_supports(obj, FEAT_BTF_FUNC) && prog_btf_fd > 0) { |
9982 | 0 | attr.prog_btf_fd = prog_btf_fd; |
9983 | | |
9984 | | /* func_info/line_info triples: all-or-nothing from caller */ |
9985 | 0 | info = OPTS_GET(opts, func_info, NULL); |
9986 | 0 | info_cnt = OPTS_GET(opts, func_info_cnt, 0); |
9987 | 0 | info_rec_size = OPTS_GET(opts, func_info_rec_size, 0); |
9988 | 0 | if (!!info != !!info_cnt || !!info != !!info_rec_size) { |
9989 | 0 | pr_warn("prog '%s': func_info, func_info_cnt, and func_info_rec_size must all be specified or all omitted\n", |
9990 | 0 | prog->name); |
9991 | 0 | return libbpf_err(-EINVAL); |
9992 | 0 | } |
9993 | 0 | attr.func_info = info ?: prog->func_info; |
9994 | 0 | attr.func_info_cnt = info ? info_cnt : prog->func_info_cnt; |
9995 | 0 | attr.func_info_rec_size = info ? info_rec_size : prog->func_info_rec_size; |
9996 | |
|
9997 | 0 | info = OPTS_GET(opts, line_info, NULL); |
9998 | 0 | info_cnt = OPTS_GET(opts, line_info_cnt, 0); |
9999 | 0 | info_rec_size = OPTS_GET(opts, line_info_rec_size, 0); |
10000 | 0 | if (!!info != !!info_cnt || !!info != !!info_rec_size) { |
10001 | 0 | pr_warn("prog '%s': line_info, line_info_cnt, and line_info_rec_size must all be specified or all omitted\n", |
10002 | 0 | prog->name); |
10003 | 0 | return libbpf_err(-EINVAL); |
10004 | 0 | } |
10005 | 0 | attr.line_info = info ?: prog->line_info; |
10006 | 0 | attr.line_info_cnt = info ? info_cnt : prog->line_info_cnt; |
10007 | 0 | attr.line_info_rec_size = info ? info_rec_size : prog->line_info_rec_size; |
10008 | 0 | } |
10009 | | |
10010 | | /* Logging is caller-controlled; no fallback to prog/obj log settings */ |
10011 | 0 | attr.log_buf = OPTS_GET(opts, log_buf, NULL); |
10012 | 0 | attr.log_size = OPTS_GET(opts, log_size, 0); |
10013 | 0 | attr.log_level = OPTS_GET(opts, log_level, 0); |
10014 | | |
10015 | | /* |
10016 | | * Fields below may be mutated by prog_prepare_load_fn: |
10017 | | * Seed them from prog/obj defaults here; |
10018 | | * Later override with caller-provided opts. |
10019 | | */ |
10020 | 0 | attr.expected_attach_type = prog->expected_attach_type; |
10021 | 0 | attr.attach_btf_id = prog->attach_btf_id; |
10022 | 0 | attr.attach_btf_obj_fd = prog->attach_btf_obj_fd; |
10023 | |
|
10024 | 0 | if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) { |
10025 | 0 | err = prog->sec_def->prog_prepare_load_fn(prog, &attr, prog->sec_def->cookie); |
10026 | 0 | if (err) |
10027 | 0 | return libbpf_err(err); |
10028 | 0 | } |
10029 | | |
10030 | | /* Re-apply caller overrides for output fields */ |
10031 | 0 | if (OPTS_GET(opts, expected_attach_type, 0)) |
10032 | 0 | attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0); |
10033 | 0 | if (OPTS_GET(opts, attach_btf_id, 0)) |
10034 | 0 | attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0); |
10035 | 0 | if (OPTS_GET(opts, attach_btf_obj_fd, 0)) |
10036 | 0 | attr.attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0); |
10037 | | |
10038 | | /* |
10039 | | * Unlike bpf_object_load_prog(), we intentionally do not call bpf_prog_bind_map() |
10040 | | * for RODATA maps here to avoid mutating the object's state. Callers can bind the |
10041 | | * required maps themselves using bpf_prog_bind_map(). |
10042 | | */ |
10043 | 0 | fd = bpf_prog_load(prog->type, prog->name, obj->license, prog->insns, prog->insns_cnt, |
10044 | 0 | &attr); |
10045 | |
|
10046 | 0 | return libbpf_err(fd); |
10047 | 0 | } |
10048 | | |
10049 | | #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \ |
10050 | | .sec = (char *)sec_pfx, \ |
10051 | | .prog_type = BPF_PROG_TYPE_##ptype, \ |
10052 | | .expected_attach_type = atype, \ |
10053 | | .cookie = (long)(flags), \ |
10054 | | .prog_prepare_load_fn = libbpf_prepare_prog_load, \ |
10055 | | __VA_ARGS__ \ |
10056 | | } |
10057 | | |
10058 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10059 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10060 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10061 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10062 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10063 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10064 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10065 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10066 | | static int attach_kprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10067 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10068 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10069 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10070 | | static int attach_tracing_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); |
10071 | | |
10072 | | static const struct bpf_sec_def section_defs[] = { |
10073 | | SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE), |
10074 | | SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE), |
10075 | | SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE), |
10076 | | SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
10077 | | SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
10078 | | SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
10079 | | SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe), |
10080 | | SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe), |
10081 | | SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe), |
10082 | | SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
10083 | | SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi), |
10084 | | SEC_DEF("kprobe.session+", KPROBE, BPF_TRACE_KPROBE_SESSION, SEC_NONE, attach_kprobe_session), |
10085 | | SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
10086 | | SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), |
10087 | | SEC_DEF("uprobe.session+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_NONE, attach_uprobe_multi), |
10088 | | SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
10089 | | SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), |
10090 | | SEC_DEF("uprobe.session.s+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_SLEEPABLE, attach_uprobe_multi), |
10091 | | SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
10092 | | SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), |
10093 | | SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt), |
10094 | | SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt), |
10095 | | SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */ |
10096 | | SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */ |
10097 | | SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), |
10098 | | SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), |
10099 | | SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
10100 | | SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
10101 | | SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */ |
10102 | | SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE), |
10103 | | SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE), |
10104 | | SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
10105 | | SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp), |
10106 | | SEC_DEF("tracepoint.s+", TRACEPOINT, 0, SEC_SLEEPABLE, attach_tp), |
10107 | | SEC_DEF("tp.s+", TRACEPOINT, 0, SEC_SLEEPABLE, attach_tp), |
10108 | | SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
10109 | | SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp), |
10110 | | SEC_DEF("raw_tracepoint.s+", RAW_TRACEPOINT, 0, SEC_SLEEPABLE, attach_raw_tp), |
10111 | | SEC_DEF("raw_tp.s+", RAW_TRACEPOINT, 0, SEC_SLEEPABLE, attach_raw_tp), |
10112 | | SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
10113 | | SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp), |
10114 | | SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace), |
10115 | | SEC_DEF("tp_btf.s+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
10116 | | SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace), |
10117 | | SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace), |
10118 | | SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace), |
10119 | | SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
10120 | | SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
10121 | | SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
10122 | | SEC_DEF("fsession+", TRACING, BPF_TRACE_FSESSION, SEC_ATTACH_BTF, attach_trace), |
10123 | | SEC_DEF("fsession.s+", TRACING, BPF_TRACE_FSESSION, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace), |
10124 | | SEC_DEF("fsession.multi+", TRACING, BPF_TRACE_FSESSION_MULTI, 0, attach_tracing_multi), |
10125 | | SEC_DEF("fsession.multi.s+", TRACING, BPF_TRACE_FSESSION_MULTI, SEC_SLEEPABLE, attach_tracing_multi), |
10126 | | SEC_DEF("fentry.multi+", TRACING, BPF_TRACE_FENTRY_MULTI, 0, attach_tracing_multi), |
10127 | | SEC_DEF("fexit.multi+", TRACING, BPF_TRACE_FEXIT_MULTI, 0, attach_tracing_multi), |
10128 | | SEC_DEF("fentry.multi.s+", TRACING, BPF_TRACE_FENTRY_MULTI, SEC_SLEEPABLE, attach_tracing_multi), |
10129 | | SEC_DEF("fexit.multi.s+", TRACING, BPF_TRACE_FEXIT_MULTI, SEC_SLEEPABLE, attach_tracing_multi), |
10130 | | SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace), |
10131 | | SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm), |
10132 | | SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm), |
10133 | | SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF), |
10134 | | SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter), |
10135 | | SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter), |
10136 | | SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE), |
10137 | | SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS), |
10138 | | SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE), |
10139 | | SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS), |
10140 | | SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE), |
10141 | | SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS), |
10142 | | SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT), |
10143 | | SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE), |
10144 | | SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE), |
10145 | | SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE), |
10146 | | SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE), |
10147 | | SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE), |
10148 | | SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT), |
10149 | | SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT), |
10150 | | SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT), |
10151 | | SEC_DEF("sk_skb/verdict", SK_SKB, BPF_SK_SKB_VERDICT, SEC_ATTACHABLE_OPT), |
10152 | | SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE), |
10153 | | SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT), |
10154 | | SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT), |
10155 | | SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT), |
10156 | | SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT), |
10157 | | SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT), |
10158 | | SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE), |
10159 | | SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE), |
10160 | | SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE), |
10161 | | SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT), |
10162 | | SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE), |
10163 | | SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE), |
10164 | | SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE), |
10165 | | SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE), |
10166 | | SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE), |
10167 | | SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE), |
10168 | | SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE), |
10169 | | SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE), |
10170 | | SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE), |
10171 | | SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE), |
10172 | | SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE), |
10173 | | SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE), |
10174 | | SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE), |
10175 | | SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE), |
10176 | | SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE), |
10177 | | SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE), |
10178 | | SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE), |
10179 | | SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE), |
10180 | | SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE), |
10181 | | SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE), |
10182 | | SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE), |
10183 | | SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE), |
10184 | | SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT), |
10185 | | SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), |
10186 | | SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), |
10187 | | SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), |
10188 | | SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE), |
10189 | | }; |
10190 | | |
10191 | | int libbpf_register_prog_handler(const char *sec, |
10192 | | enum bpf_prog_type prog_type, |
10193 | | enum bpf_attach_type exp_attach_type, |
10194 | | const struct libbpf_prog_handler_opts *opts) |
10195 | 0 | { |
10196 | 0 | struct bpf_sec_def *sec_def; |
10197 | |
|
10198 | 0 | if (!OPTS_VALID(opts, libbpf_prog_handler_opts)) |
10199 | 0 | return libbpf_err(-EINVAL); |
10200 | | |
10201 | 0 | if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */ |
10202 | 0 | return libbpf_err(-E2BIG); |
10203 | | |
10204 | 0 | if (sec) { |
10205 | 0 | sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1, |
10206 | 0 | sizeof(*sec_def)); |
10207 | 0 | if (!sec_def) |
10208 | 0 | return libbpf_err(-ENOMEM); |
10209 | | |
10210 | 0 | custom_sec_defs = sec_def; |
10211 | 0 | sec_def = &custom_sec_defs[custom_sec_def_cnt]; |
10212 | 0 | } else { |
10213 | 0 | if (has_custom_fallback_def) |
10214 | 0 | return libbpf_err(-EBUSY); |
10215 | | |
10216 | 0 | sec_def = &custom_fallback_def; |
10217 | 0 | } |
10218 | | |
10219 | 0 | sec_def->sec = sec ? strdup(sec) : NULL; |
10220 | 0 | if (sec && !sec_def->sec) |
10221 | 0 | return libbpf_err(-ENOMEM); |
10222 | | |
10223 | 0 | sec_def->prog_type = prog_type; |
10224 | 0 | sec_def->expected_attach_type = exp_attach_type; |
10225 | 0 | sec_def->cookie = OPTS_GET(opts, cookie, 0); |
10226 | |
|
10227 | 0 | sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL); |
10228 | 0 | sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL); |
10229 | 0 | sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL); |
10230 | |
|
10231 | 0 | sec_def->handler_id = ++last_custom_sec_def_handler_id; |
10232 | |
|
10233 | 0 | if (sec) |
10234 | 0 | custom_sec_def_cnt++; |
10235 | 0 | else |
10236 | 0 | has_custom_fallback_def = true; |
10237 | |
|
10238 | 0 | return sec_def->handler_id; |
10239 | 0 | } |
10240 | | |
10241 | | int libbpf_unregister_prog_handler(int handler_id) |
10242 | 0 | { |
10243 | 0 | struct bpf_sec_def *sec_defs; |
10244 | 0 | int i; |
10245 | |
|
10246 | 0 | if (handler_id <= 0) |
10247 | 0 | return libbpf_err(-EINVAL); |
10248 | | |
10249 | 0 | if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) { |
10250 | 0 | memset(&custom_fallback_def, 0, sizeof(custom_fallback_def)); |
10251 | 0 | has_custom_fallback_def = false; |
10252 | 0 | return 0; |
10253 | 0 | } |
10254 | | |
10255 | 0 | for (i = 0; i < custom_sec_def_cnt; i++) { |
10256 | 0 | if (custom_sec_defs[i].handler_id == handler_id) |
10257 | 0 | break; |
10258 | 0 | } |
10259 | |
|
10260 | 0 | if (i == custom_sec_def_cnt) |
10261 | 0 | return libbpf_err(-ENOENT); |
10262 | | |
10263 | 0 | free(custom_sec_defs[i].sec); |
10264 | 0 | for (i = i + 1; i < custom_sec_def_cnt; i++) |
10265 | 0 | custom_sec_defs[i - 1] = custom_sec_defs[i]; |
10266 | 0 | custom_sec_def_cnt--; |
10267 | | |
10268 | | /* try to shrink the array, but it's ok if we couldn't */ |
10269 | 0 | sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs)); |
10270 | | /* if new count is zero, reallocarray can return a valid NULL result; |
10271 | | * in this case the previous pointer will be freed, so we *have to* |
10272 | | * reassign old pointer to the new value (even if it's NULL) |
10273 | | */ |
10274 | 0 | if (sec_defs || custom_sec_def_cnt == 0) |
10275 | 0 | custom_sec_defs = sec_defs; |
10276 | |
|
10277 | 0 | return 0; |
10278 | 0 | } |
10279 | | |
10280 | | static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name) |
10281 | 888k | { |
10282 | 888k | size_t len = strlen(sec_def->sec); |
10283 | | |
10284 | | /* "type/" always has to have proper SEC("type/extras") form */ |
10285 | 888k | if (sec_def->sec[len - 1] == '/') { |
10286 | 0 | if (str_has_pfx(sec_name, sec_def->sec)) |
10287 | 0 | return true; |
10288 | 0 | return false; |
10289 | 0 | } |
10290 | | |
10291 | | /* "type+" means it can be either exact SEC("type") or |
10292 | | * well-formed SEC("type/extras") with proper '/' separator |
10293 | | */ |
10294 | 888k | if (sec_def->sec[len - 1] == '+') { |
10295 | 416k | len--; |
10296 | | /* not even a prefix */ |
10297 | 416k | if (strncmp(sec_name, sec_def->sec, len) != 0) |
10298 | 414k | return false; |
10299 | | /* exact match or has '/' separator */ |
10300 | 2.08k | if (sec_name[len] == '\0' || sec_name[len] == '/') |
10301 | 898 | return true; |
10302 | 1.18k | return false; |
10303 | 2.08k | } |
10304 | | |
10305 | 472k | return strcmp(sec_name, sec_def->sec) == 0; |
10306 | 888k | } |
10307 | | |
10308 | | static const struct bpf_sec_def *find_sec_def(const char *sec_name) |
10309 | 8.14k | { |
10310 | 8.14k | const struct bpf_sec_def *sec_def; |
10311 | 8.14k | int i, n; |
10312 | | |
10313 | 8.14k | n = custom_sec_def_cnt; |
10314 | 8.14k | for (i = 0; i < n; i++) { |
10315 | 0 | sec_def = &custom_sec_defs[i]; |
10316 | 0 | if (sec_def_matches(sec_def, sec_name)) |
10317 | 0 | return sec_def; |
10318 | 0 | } |
10319 | | |
10320 | 8.14k | n = ARRAY_SIZE(section_defs); |
10321 | 895k | for (i = 0; i < n; i++) { |
10322 | 888k | sec_def = §ion_defs[i]; |
10323 | 888k | if (sec_def_matches(sec_def, sec_name)) |
10324 | 1.03k | return sec_def; |
10325 | 888k | } |
10326 | | |
10327 | 7.11k | if (has_custom_fallback_def) |
10328 | 0 | return &custom_fallback_def; |
10329 | | |
10330 | 7.11k | return NULL; |
10331 | 7.11k | } |
10332 | | |
10333 | 0 | #define MAX_TYPE_NAME_SIZE 32 |
10334 | | |
10335 | | static char *libbpf_get_type_names(bool attach_type) |
10336 | 0 | { |
10337 | 0 | int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE; |
10338 | 0 | char *buf; |
10339 | |
|
10340 | 0 | buf = malloc(len); |
10341 | 0 | if (!buf) |
10342 | 0 | return NULL; |
10343 | | |
10344 | 0 | buf[0] = '\0'; |
10345 | | /* Forge string buf with all available names */ |
10346 | 0 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
10347 | 0 | const struct bpf_sec_def *sec_def = §ion_defs[i]; |
10348 | |
|
10349 | 0 | if (attach_type) { |
10350 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
10351 | 0 | continue; |
10352 | | |
10353 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
10354 | 0 | continue; |
10355 | 0 | } |
10356 | | |
10357 | 0 | if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) { |
10358 | 0 | free(buf); |
10359 | 0 | return NULL; |
10360 | 0 | } |
10361 | 0 | strcat(buf, " "); |
10362 | 0 | strcat(buf, section_defs[i].sec); |
10363 | 0 | } |
10364 | | |
10365 | 0 | return buf; |
10366 | 0 | } |
10367 | | |
10368 | | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
10369 | | enum bpf_attach_type *expected_attach_type) |
10370 | 0 | { |
10371 | 0 | const struct bpf_sec_def *sec_def; |
10372 | 0 | char *type_names; |
10373 | |
|
10374 | 0 | if (!name) |
10375 | 0 | return libbpf_err(-EINVAL); |
10376 | | |
10377 | 0 | sec_def = find_sec_def(name); |
10378 | 0 | if (sec_def) { |
10379 | 0 | *prog_type = sec_def->prog_type; |
10380 | 0 | *expected_attach_type = sec_def->expected_attach_type; |
10381 | 0 | return 0; |
10382 | 0 | } |
10383 | | |
10384 | 0 | pr_debug("failed to guess program type from ELF section '%s'\n", name); |
10385 | 0 | type_names = libbpf_get_type_names(false); |
10386 | 0 | if (type_names != NULL) { |
10387 | 0 | pr_debug("supported section(type) names are:%s\n", type_names); |
10388 | 0 | free(type_names); |
10389 | 0 | } |
10390 | |
|
10391 | 0 | return libbpf_err(-ESRCH); |
10392 | 0 | } |
10393 | | |
10394 | | const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t) |
10395 | 0 | { |
10396 | 0 | if (t < 0 || t >= ARRAY_SIZE(attach_type_name)) |
10397 | 0 | return NULL; |
10398 | | |
10399 | 0 | return attach_type_name[t]; |
10400 | 0 | } |
10401 | | |
10402 | | const char *libbpf_bpf_link_type_str(enum bpf_link_type t) |
10403 | 0 | { |
10404 | 0 | if (t < 0 || t >= ARRAY_SIZE(link_type_name)) |
10405 | 0 | return NULL; |
10406 | | |
10407 | 0 | return link_type_name[t]; |
10408 | 0 | } |
10409 | | |
10410 | | const char *libbpf_bpf_map_type_str(enum bpf_map_type t) |
10411 | 0 | { |
10412 | 0 | if (t < 0 || t >= ARRAY_SIZE(map_type_name)) |
10413 | 0 | return NULL; |
10414 | | |
10415 | 0 | return map_type_name[t]; |
10416 | 0 | } |
10417 | | |
10418 | | const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t) |
10419 | 0 | { |
10420 | 0 | if (t < 0 || t >= ARRAY_SIZE(prog_type_name)) |
10421 | 0 | return NULL; |
10422 | | |
10423 | 0 | return prog_type_name[t]; |
10424 | 0 | } |
10425 | | |
10426 | | static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj, |
10427 | | int sec_idx, |
10428 | | size_t offset) |
10429 | 11 | { |
10430 | 11 | struct bpf_map *map; |
10431 | 11 | size_t i; |
10432 | | |
10433 | 29 | for (i = 0; i < obj->nr_maps; i++) { |
10434 | 21 | map = &obj->maps[i]; |
10435 | 21 | if (!bpf_map__is_struct_ops(map)) |
10436 | 1 | continue; |
10437 | 20 | if (map->sec_idx == sec_idx && |
10438 | 11 | map->sec_offset <= offset && |
10439 | 10 | offset - map->sec_offset < map->def.value_size) |
10440 | 3 | return map; |
10441 | 20 | } |
10442 | | |
10443 | 8 | return NULL; |
10444 | 11 | } |
10445 | | |
10446 | | /* Collect the reloc from ELF, populate the st_ops->progs[], and update |
10447 | | * st_ops->data for shadow type. |
10448 | | */ |
10449 | | static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, |
10450 | | Elf64_Shdr *shdr, Elf_Data *data) |
10451 | 23 | { |
10452 | 23 | const struct btf_type *type; |
10453 | 23 | const struct btf_member *member; |
10454 | 23 | struct bpf_struct_ops *st_ops; |
10455 | 23 | struct bpf_program *prog; |
10456 | 23 | unsigned int shdr_idx; |
10457 | 23 | const struct btf *btf; |
10458 | 23 | struct bpf_map *map; |
10459 | 23 | unsigned int moff, insn_idx; |
10460 | 23 | const char *name; |
10461 | 23 | __u32 member_idx; |
10462 | 23 | Elf64_Sym *sym; |
10463 | 23 | Elf64_Rel *rel; |
10464 | 23 | int i, nrels; |
10465 | | |
10466 | 23 | btf = obj->btf; |
10467 | 23 | nrels = shdr->sh_size / shdr->sh_entsize; |
10468 | 23 | for (i = 0; i < nrels; i++) { |
10469 | 22 | rel = elf_rel_by_idx(data, i); |
10470 | 22 | if (!rel) { |
10471 | 0 | pr_warn("struct_ops reloc: failed to get %d reloc\n", i); |
10472 | 0 | return -LIBBPF_ERRNO__FORMAT; |
10473 | 0 | } |
10474 | | |
10475 | 22 | sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)); |
10476 | 22 | if (!sym) { |
10477 | 11 | pr_warn("struct_ops reloc: symbol %zx not found\n", |
10478 | 11 | (size_t)ELF64_R_SYM(rel->r_info)); |
10479 | 11 | return -LIBBPF_ERRNO__FORMAT; |
10480 | 11 | } |
10481 | | |
10482 | 11 | name = elf_sym_str(obj, sym->st_name) ?: "<?>"; |
10483 | 11 | map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset); |
10484 | 11 | if (!map) { |
10485 | 8 | pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n", |
10486 | 8 | (size_t)rel->r_offset); |
10487 | 8 | return -EINVAL; |
10488 | 8 | } |
10489 | | |
10490 | 3 | moff = rel->r_offset - map->sec_offset; |
10491 | 3 | shdr_idx = sym->st_shndx; |
10492 | 3 | st_ops = map->st_ops; |
10493 | 3 | pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %u (\'%s\')\n", |
10494 | 3 | map->name, |
10495 | 3 | (long long)(rel->r_info >> 32), |
10496 | 3 | (long long)sym->st_value, |
10497 | 3 | shdr_idx, (size_t)rel->r_offset, |
10498 | 3 | map->sec_offset, sym->st_name, name); |
10499 | | |
10500 | 3 | if (shdr_idx >= SHN_LORESERVE) { |
10501 | 1 | pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n", |
10502 | 1 | map->name, (size_t)rel->r_offset, shdr_idx); |
10503 | 1 | return -LIBBPF_ERRNO__RELOC; |
10504 | 1 | } |
10505 | 2 | if (sym->st_value % BPF_INSN_SZ) { |
10506 | 1 | pr_warn("struct_ops reloc %s: invalid target program offset %llu\n", |
10507 | 1 | map->name, (unsigned long long)sym->st_value); |
10508 | 1 | return -LIBBPF_ERRNO__FORMAT; |
10509 | 1 | } |
10510 | 1 | insn_idx = sym->st_value / BPF_INSN_SZ; |
10511 | | |
10512 | 1 | type = btf__type_by_id(btf, st_ops->type_id); |
10513 | 1 | member = find_member_by_offset(type, moff * 8); |
10514 | 1 | if (!member) { |
10515 | 1 | pr_warn("struct_ops reloc %s: cannot find member at moff %u\n", |
10516 | 1 | map->name, moff); |
10517 | 1 | return -EINVAL; |
10518 | 1 | } |
10519 | 0 | member_idx = member - btf_members(type); |
10520 | 0 | name = btf__name_by_offset(btf, member->name_off); |
10521 | |
|
10522 | 0 | if (!resolve_func_ptr(btf, member->type, NULL)) { |
10523 | 0 | pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n", |
10524 | 0 | map->name, name); |
10525 | 0 | return -EINVAL; |
10526 | 0 | } |
10527 | | |
10528 | 0 | prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx); |
10529 | 0 | if (!prog) { |
10530 | 0 | pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n", |
10531 | 0 | map->name, shdr_idx, name); |
10532 | 0 | return -EINVAL; |
10533 | 0 | } |
10534 | | |
10535 | | /* prevent the use of BPF prog with invalid type */ |
10536 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) { |
10537 | 0 | pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n", |
10538 | 0 | map->name, prog->name); |
10539 | 0 | return -EINVAL; |
10540 | 0 | } |
10541 | | |
10542 | 0 | st_ops->progs[member_idx] = prog; |
10543 | | |
10544 | | /* st_ops->data will be exposed to users, being returned by |
10545 | | * bpf_map__initial_value() as a pointer to the shadow |
10546 | | * type. All function pointers in the original struct type |
10547 | | * should be converted to a pointer to struct bpf_program |
10548 | | * in the shadow type. |
10549 | | */ |
10550 | 0 | *((struct bpf_program **)(st_ops->data + moff)) = prog; |
10551 | 0 | } |
10552 | | |
10553 | 1 | return 0; |
10554 | 23 | } |
10555 | | |
10556 | 0 | #define BTF_TRACE_PREFIX "btf_trace_" |
10557 | 0 | #define BTF_LSM_PREFIX "bpf_lsm_" |
10558 | 0 | #define BTF_ITER_PREFIX "bpf_iter_" |
10559 | | #define BTF_MAX_NAME_SIZE 128 |
10560 | | |
10561 | | void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, |
10562 | | const char **prefix, int *kind) |
10563 | 0 | { |
10564 | 0 | switch (attach_type) { |
10565 | 0 | case BPF_TRACE_RAW_TP: |
10566 | 0 | *prefix = BTF_TRACE_PREFIX; |
10567 | 0 | *kind = BTF_KIND_TYPEDEF; |
10568 | 0 | break; |
10569 | 0 | case BPF_LSM_MAC: |
10570 | 0 | case BPF_LSM_CGROUP: |
10571 | 0 | *prefix = BTF_LSM_PREFIX; |
10572 | 0 | *kind = BTF_KIND_FUNC; |
10573 | 0 | break; |
10574 | 0 | case BPF_TRACE_ITER: |
10575 | 0 | *prefix = BTF_ITER_PREFIX; |
10576 | 0 | *kind = BTF_KIND_FUNC; |
10577 | 0 | break; |
10578 | 0 | default: |
10579 | 0 | *prefix = ""; |
10580 | 0 | *kind = BTF_KIND_FUNC; |
10581 | 0 | } |
10582 | 0 | } |
10583 | | |
10584 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
10585 | | const char *name, __u32 kind) |
10586 | 0 | { |
10587 | 0 | char btf_type_name[BTF_MAX_NAME_SIZE]; |
10588 | 0 | int ret; |
10589 | |
|
10590 | 0 | ret = snprintf(btf_type_name, sizeof(btf_type_name), |
10591 | 0 | "%s%s", prefix, name); |
10592 | | /* snprintf returns the number of characters written excluding the |
10593 | | * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it |
10594 | | * indicates truncation. |
10595 | | */ |
10596 | 0 | if (ret < 0 || ret >= sizeof(btf_type_name)) |
10597 | 0 | return -ENAMETOOLONG; |
10598 | 0 | return btf__find_by_name_kind(btf, btf_type_name, kind); |
10599 | 0 | } |
10600 | | |
10601 | | static inline int find_attach_btf_id(struct btf *btf, const char *name, |
10602 | | enum bpf_attach_type attach_type) |
10603 | 0 | { |
10604 | 0 | const char *prefix; |
10605 | 0 | int kind; |
10606 | |
|
10607 | 0 | btf_get_kernel_prefix_kind(attach_type, &prefix, &kind); |
10608 | 0 | return find_btf_by_prefix_kind(btf, prefix, name, kind); |
10609 | 0 | } |
10610 | | |
10611 | | int libbpf_find_vmlinux_btf_id(const char *name, |
10612 | | enum bpf_attach_type attach_type) |
10613 | 0 | { |
10614 | 0 | struct btf *btf; |
10615 | 0 | int err; |
10616 | |
|
10617 | 0 | btf = btf__load_vmlinux_btf(); |
10618 | 0 | err = libbpf_get_error(btf); |
10619 | 0 | if (err) { |
10620 | 0 | pr_warn("vmlinux BTF is not found\n"); |
10621 | 0 | return libbpf_err(err); |
10622 | 0 | } |
10623 | | |
10624 | 0 | err = find_attach_btf_id(btf, name, attach_type); |
10625 | 0 | if (err <= 0) |
10626 | 0 | pr_warn("%s is not found in vmlinux BTF\n", name); |
10627 | |
|
10628 | 0 | btf__free(btf); |
10629 | 0 | return libbpf_err(err); |
10630 | 0 | } |
10631 | | |
10632 | | static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int token_fd) |
10633 | 0 | { |
10634 | 0 | struct bpf_prog_info info; |
10635 | 0 | __u32 info_len = sizeof(info); |
10636 | 0 | struct btf *btf; |
10637 | 0 | int err; |
10638 | |
|
10639 | 0 | memset(&info, 0, info_len); |
10640 | 0 | err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len); |
10641 | 0 | if (err) { |
10642 | 0 | pr_warn("failed bpf_prog_get_info_by_fd for FD %u: %s\n", |
10643 | 0 | attach_prog_fd, errstr(err)); |
10644 | 0 | return err; |
10645 | 0 | } |
10646 | | |
10647 | 0 | err = -EINVAL; |
10648 | 0 | if (!info.btf_id) { |
10649 | 0 | pr_warn("The target program doesn't have BTF\n"); |
10650 | 0 | goto out; |
10651 | 0 | } |
10652 | 0 | btf = btf_load_from_kernel(info.btf_id, NULL, token_fd); |
10653 | 0 | err = libbpf_get_error(btf); |
10654 | 0 | if (err) { |
10655 | 0 | pr_warn("Failed to get BTF %u of the program: %s\n", info.btf_id, errstr(err)); |
10656 | 0 | goto out; |
10657 | 0 | } |
10658 | 0 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
10659 | 0 | btf__free(btf); |
10660 | 0 | if (err <= 0) { |
10661 | 0 | pr_warn("%s is not found in prog's BTF\n", name); |
10662 | 0 | goto out; |
10663 | 0 | } |
10664 | 0 | out: |
10665 | 0 | return err; |
10666 | 0 | } |
10667 | | |
10668 | | static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name, |
10669 | | enum bpf_attach_type attach_type, |
10670 | | int *btf_obj_fd, int *btf_type_id) |
10671 | 0 | { |
10672 | 0 | int ret, i, mod_len = 0; |
10673 | 0 | const char *fn_name, *mod_name = NULL; |
10674 | |
|
10675 | 0 | fn_name = strchr(attach_name, ':'); |
10676 | 0 | if (fn_name) { |
10677 | 0 | mod_name = attach_name; |
10678 | 0 | mod_len = fn_name - mod_name; |
10679 | 0 | fn_name++; |
10680 | 0 | } |
10681 | |
|
10682 | 0 | if (!mod_name || strncmp(mod_name, "vmlinux", mod_len) == 0) { |
10683 | 0 | ret = find_attach_btf_id(obj->btf_vmlinux, |
10684 | 0 | mod_name ? fn_name : attach_name, |
10685 | 0 | attach_type); |
10686 | 0 | if (ret > 0) { |
10687 | 0 | *btf_obj_fd = 0; /* vmlinux BTF */ |
10688 | 0 | *btf_type_id = ret; |
10689 | 0 | return 0; |
10690 | 0 | } |
10691 | 0 | if (ret != -ENOENT) |
10692 | 0 | return ret; |
10693 | 0 | } |
10694 | | |
10695 | 0 | ret = load_module_btfs(obj); |
10696 | 0 | if (ret) |
10697 | 0 | return ret; |
10698 | | |
10699 | 0 | for (i = 0; i < obj->btf_module_cnt; i++) { |
10700 | 0 | const struct module_btf *mod = &obj->btf_modules[i]; |
10701 | |
|
10702 | 0 | if (mod_name && strncmp(mod->name, mod_name, mod_len) != 0) |
10703 | 0 | continue; |
10704 | | |
10705 | 0 | ret = find_attach_btf_id(mod->btf, |
10706 | 0 | mod_name ? fn_name : attach_name, |
10707 | 0 | attach_type); |
10708 | 0 | if (ret > 0) { |
10709 | 0 | *btf_obj_fd = mod->fd; |
10710 | 0 | *btf_type_id = ret; |
10711 | 0 | return 0; |
10712 | 0 | } |
10713 | 0 | if (ret == -ENOENT) |
10714 | 0 | continue; |
10715 | | |
10716 | 0 | return ret; |
10717 | 0 | } |
10718 | | |
10719 | 0 | return -ESRCH; |
10720 | 0 | } |
10721 | | |
10722 | | static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name, |
10723 | | int *btf_obj_fd, int *btf_type_id) |
10724 | 0 | { |
10725 | 0 | enum bpf_attach_type attach_type = prog->expected_attach_type; |
10726 | 0 | __u32 attach_prog_fd = prog->attach_prog_fd; |
10727 | 0 | int err = 0; |
10728 | | |
10729 | | /* BPF program's BTF ID */ |
10730 | 0 | if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { |
10731 | 0 | if (!attach_prog_fd) { |
10732 | 0 | pr_warn("prog '%s': attach program FD is not set\n", prog->name); |
10733 | 0 | return -EINVAL; |
10734 | 0 | } |
10735 | 0 | err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd, prog->obj->token_fd); |
10736 | 0 | if (err < 0) { |
10737 | 0 | pr_warn("prog '%s': failed to find BPF program (FD %u) BTF ID for '%s': %s\n", |
10738 | 0 | prog->name, attach_prog_fd, attach_name, errstr(err)); |
10739 | 0 | return err; |
10740 | 0 | } |
10741 | 0 | *btf_obj_fd = 0; |
10742 | 0 | *btf_type_id = err; |
10743 | 0 | return 0; |
10744 | 0 | } |
10745 | | |
10746 | | /* kernel/module BTF ID */ |
10747 | 0 | if (prog->obj->gen_loader) { |
10748 | 0 | bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type); |
10749 | 0 | *btf_obj_fd = 0; |
10750 | 0 | *btf_type_id = 1; |
10751 | 0 | } else { |
10752 | 0 | err = find_kernel_btf_id(prog->obj, attach_name, |
10753 | 0 | attach_type, btf_obj_fd, |
10754 | 0 | btf_type_id); |
10755 | 0 | } |
10756 | 0 | if (err) { |
10757 | 0 | pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %s\n", |
10758 | 0 | prog->name, attach_name, errstr(err)); |
10759 | 0 | return err; |
10760 | 0 | } |
10761 | 0 | return 0; |
10762 | 0 | } |
10763 | | |
10764 | | int libbpf_attach_type_by_name(const char *name, |
10765 | | enum bpf_attach_type *attach_type) |
10766 | 0 | { |
10767 | 0 | char *type_names; |
10768 | 0 | const struct bpf_sec_def *sec_def; |
10769 | |
|
10770 | 0 | if (!name) |
10771 | 0 | return libbpf_err(-EINVAL); |
10772 | | |
10773 | 0 | sec_def = find_sec_def(name); |
10774 | 0 | if (!sec_def) { |
10775 | 0 | pr_debug("failed to guess attach type based on ELF section name '%s'\n", name); |
10776 | 0 | type_names = libbpf_get_type_names(true); |
10777 | 0 | if (type_names != NULL) { |
10778 | 0 | pr_debug("attachable section(type) names are:%s\n", type_names); |
10779 | 0 | free(type_names); |
10780 | 0 | } |
10781 | |
|
10782 | 0 | return libbpf_err(-EINVAL); |
10783 | 0 | } |
10784 | | |
10785 | 0 | if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load) |
10786 | 0 | return libbpf_err(-EINVAL); |
10787 | 0 | if (!(sec_def->cookie & SEC_ATTACHABLE)) |
10788 | 0 | return libbpf_err(-EINVAL); |
10789 | | |
10790 | 0 | *attach_type = sec_def->expected_attach_type; |
10791 | 0 | return 0; |
10792 | 0 | } |
10793 | | |
10794 | | int bpf_map__fd(const struct bpf_map *map) |
10795 | 0 | { |
10796 | 0 | if (!map) |
10797 | 0 | return libbpf_err(-EINVAL); |
10798 | 0 | if (!map_is_created(map)) |
10799 | 0 | return -1; |
10800 | 0 | return map->fd; |
10801 | 0 | } |
10802 | | |
10803 | | static bool map_uses_real_name(const struct bpf_map *map) |
10804 | 0 | { |
10805 | | /* Since libbpf started to support custom .data.* and .rodata.* maps, |
10806 | | * their user-visible name differs from kernel-visible name. Users see |
10807 | | * such map's corresponding ELF section name as a map name. |
10808 | | * This check distinguishes .data/.rodata from .data.* and .rodata.* |
10809 | | * maps to know which name has to be returned to the user. |
10810 | | */ |
10811 | 0 | if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0) |
10812 | 0 | return true; |
10813 | 0 | if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0) |
10814 | 0 | return true; |
10815 | 0 | return false; |
10816 | 0 | } |
10817 | | |
10818 | | const char *bpf_map__name(const struct bpf_map *map) |
10819 | 0 | { |
10820 | 0 | if (!map) |
10821 | 0 | return NULL; |
10822 | | |
10823 | 0 | if (map_uses_real_name(map)) |
10824 | 0 | return map->real_name; |
10825 | | |
10826 | 0 | return map->name; |
10827 | 0 | } |
10828 | | |
10829 | | enum bpf_map_type bpf_map__type(const struct bpf_map *map) |
10830 | 0 | { |
10831 | 0 | return map->def.type; |
10832 | 0 | } |
10833 | | |
10834 | | int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type) |
10835 | 0 | { |
10836 | 0 | if (map_is_created(map)) |
10837 | 0 | return libbpf_err(-EBUSY); |
10838 | 0 | map->def.type = type; |
10839 | 0 | return 0; |
10840 | 0 | } |
10841 | | |
10842 | | __u32 bpf_map__map_flags(const struct bpf_map *map) |
10843 | 0 | { |
10844 | 0 | return map->def.map_flags; |
10845 | 0 | } |
10846 | | |
10847 | | int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags) |
10848 | 0 | { |
10849 | 0 | if (map_is_created(map)) |
10850 | 0 | return libbpf_err(-EBUSY); |
10851 | 0 | map->def.map_flags = flags; |
10852 | 0 | return 0; |
10853 | 0 | } |
10854 | | |
10855 | | __u64 bpf_map__map_extra(const struct bpf_map *map) |
10856 | 0 | { |
10857 | 0 | return map->map_extra; |
10858 | 0 | } |
10859 | | |
10860 | | int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra) |
10861 | 0 | { |
10862 | 0 | if (map_is_created(map)) |
10863 | 0 | return libbpf_err(-EBUSY); |
10864 | 0 | map->map_extra = map_extra; |
10865 | 0 | return 0; |
10866 | 0 | } |
10867 | | |
10868 | | __u32 bpf_map__numa_node(const struct bpf_map *map) |
10869 | 0 | { |
10870 | 0 | return map->numa_node; |
10871 | 0 | } |
10872 | | |
10873 | | int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node) |
10874 | 0 | { |
10875 | 0 | if (map_is_created(map)) |
10876 | 0 | return libbpf_err(-EBUSY); |
10877 | 0 | map->numa_node = numa_node; |
10878 | 0 | return 0; |
10879 | 0 | } |
10880 | | |
10881 | | __u32 bpf_map__key_size(const struct bpf_map *map) |
10882 | 0 | { |
10883 | 0 | return map->def.key_size; |
10884 | 0 | } |
10885 | | |
10886 | | int bpf_map__set_key_size(struct bpf_map *map, __u32 size) |
10887 | 0 | { |
10888 | 0 | if (map_is_created(map)) |
10889 | 0 | return libbpf_err(-EBUSY); |
10890 | 0 | map->def.key_size = size; |
10891 | 0 | return 0; |
10892 | 0 | } |
10893 | | |
10894 | | __u32 bpf_map__value_size(const struct bpf_map *map) |
10895 | 0 | { |
10896 | 0 | return map->def.value_size; |
10897 | 0 | } |
10898 | | |
10899 | | static int map_btf_datasec_resize(struct bpf_map *map, __u32 size) |
10900 | 0 | { |
10901 | 0 | struct btf *btf; |
10902 | 0 | struct btf_type *datasec_type, *var_type; |
10903 | 0 | struct btf_var_secinfo *var; |
10904 | 0 | const struct btf_type *array_type; |
10905 | 0 | const struct btf_array *array; |
10906 | 0 | int vlen, element_sz, new_array_id; |
10907 | 0 | __u32 nr_elements; |
10908 | | |
10909 | | /* check btf existence */ |
10910 | 0 | btf = bpf_object__btf(map->obj); |
10911 | 0 | if (!btf) |
10912 | 0 | return -ENOENT; |
10913 | | |
10914 | | /* verify map is datasec */ |
10915 | 0 | datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map)); |
10916 | 0 | if (!btf_is_datasec(datasec_type)) { |
10917 | 0 | pr_warn("map '%s': cannot be resized, map value type is not a datasec\n", |
10918 | 0 | bpf_map__name(map)); |
10919 | 0 | return -EINVAL; |
10920 | 0 | } |
10921 | | |
10922 | | /* verify datasec has at least one var */ |
10923 | 0 | vlen = btf_vlen(datasec_type); |
10924 | 0 | if (vlen == 0) { |
10925 | 0 | pr_warn("map '%s': cannot be resized, map value datasec is empty\n", |
10926 | 0 | bpf_map__name(map)); |
10927 | 0 | return -EINVAL; |
10928 | 0 | } |
10929 | | |
10930 | | /* verify last var in the datasec is an array */ |
10931 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
10932 | 0 | var_type = btf_type_by_id(btf, var->type); |
10933 | 0 | array_type = skip_mods_and_typedefs(btf, var_type->type, NULL); |
10934 | 0 | if (!btf_is_array(array_type)) { |
10935 | 0 | pr_warn("map '%s': cannot be resized, last var must be an array\n", |
10936 | 0 | bpf_map__name(map)); |
10937 | 0 | return -EINVAL; |
10938 | 0 | } |
10939 | | |
10940 | | /* verify request size aligns with array */ |
10941 | 0 | array = btf_array(array_type); |
10942 | 0 | element_sz = btf__resolve_size(btf, array->type); |
10943 | 0 | if (element_sz <= 0 || (size - var->offset) % element_sz != 0) { |
10944 | 0 | pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n", |
10945 | 0 | bpf_map__name(map), element_sz, size); |
10946 | 0 | return -EINVAL; |
10947 | 0 | } |
10948 | | |
10949 | | /* create a new array based on the existing array, but with new length */ |
10950 | 0 | nr_elements = (size - var->offset) / element_sz; |
10951 | 0 | new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements); |
10952 | 0 | if (new_array_id < 0) |
10953 | 0 | return new_array_id; |
10954 | | |
10955 | | /* adding a new btf type invalidates existing pointers to btf objects, |
10956 | | * so refresh pointers before proceeding |
10957 | | */ |
10958 | 0 | datasec_type = btf_type_by_id(btf, map->btf_value_type_id); |
10959 | 0 | var = &btf_var_secinfos(datasec_type)[vlen - 1]; |
10960 | 0 | var_type = btf_type_by_id(btf, var->type); |
10961 | | |
10962 | | /* finally update btf info */ |
10963 | 0 | datasec_type->size = size; |
10964 | 0 | var->size = size - var->offset; |
10965 | 0 | var_type->type = new_array_id; |
10966 | |
|
10967 | 0 | return 0; |
10968 | 0 | } |
10969 | | |
10970 | | int bpf_map__set_value_size(struct bpf_map *map, __u32 size) |
10971 | 0 | { |
10972 | 0 | if (map_is_created(map)) |
10973 | 0 | return libbpf_err(-EBUSY); |
10974 | | |
10975 | 0 | if (map->mmaped) { |
10976 | 0 | size_t mmap_old_sz, mmap_new_sz; |
10977 | 0 | int err; |
10978 | |
|
10979 | 0 | if (map->def.type != BPF_MAP_TYPE_ARRAY) |
10980 | 0 | return libbpf_err(-EOPNOTSUPP); |
10981 | | |
10982 | 0 | mmap_old_sz = bpf_map_mmap_sz(map); |
10983 | 0 | mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries); |
10984 | 0 | err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz); |
10985 | 0 | if (err) { |
10986 | 0 | pr_warn("map '%s': failed to resize memory-mapped region: %s\n", |
10987 | 0 | bpf_map__name(map), errstr(err)); |
10988 | 0 | return libbpf_err(err); |
10989 | 0 | } |
10990 | 0 | err = map_btf_datasec_resize(map, size); |
10991 | 0 | if (err && err != -ENOENT) { |
10992 | 0 | pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %s\n", |
10993 | 0 | bpf_map__name(map), errstr(err)); |
10994 | 0 | map->btf_value_type_id = 0; |
10995 | 0 | map->btf_key_type_id = 0; |
10996 | 0 | } |
10997 | 0 | } |
10998 | | |
10999 | 0 | map->def.value_size = size; |
11000 | 0 | return 0; |
11001 | 0 | } |
11002 | | |
11003 | | __u32 bpf_map__btf_key_type_id(const struct bpf_map *map) |
11004 | 0 | { |
11005 | 0 | return map ? map->btf_key_type_id : 0; |
11006 | 0 | } |
11007 | | |
11008 | | __u32 bpf_map__btf_value_type_id(const struct bpf_map *map) |
11009 | 0 | { |
11010 | 0 | return map ? map->btf_value_type_id : 0; |
11011 | 0 | } |
11012 | | |
11013 | | int bpf_map__set_initial_value(struct bpf_map *map, |
11014 | | const void *data, size_t size) |
11015 | 0 | { |
11016 | 0 | size_t actual_sz; |
11017 | |
|
11018 | 0 | if (map_is_created(map)) |
11019 | 0 | return libbpf_err(-EBUSY); |
11020 | | |
11021 | 0 | if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG) |
11022 | 0 | return libbpf_err(-EINVAL); |
11023 | | |
11024 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) |
11025 | 0 | actual_sz = map->obj->arena_data_sz; |
11026 | 0 | else |
11027 | 0 | actual_sz = map->def.value_size; |
11028 | 0 | if (size != actual_sz) |
11029 | 0 | return libbpf_err(-EINVAL); |
11030 | | |
11031 | 0 | memcpy(map->mmaped, data, size); |
11032 | 0 | return 0; |
11033 | 0 | } |
11034 | | |
11035 | | void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize) |
11036 | 0 | { |
11037 | 0 | if (bpf_map__is_struct_ops(map)) { |
11038 | 0 | if (psize) |
11039 | 0 | *psize = map->def.value_size; |
11040 | 0 | return map->st_ops->data; |
11041 | 0 | } |
11042 | | |
11043 | 0 | if (!map->mmaped) |
11044 | 0 | return NULL; |
11045 | | |
11046 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) |
11047 | 0 | *psize = map->obj->arena_data_sz; |
11048 | 0 | else |
11049 | 0 | *psize = map->def.value_size; |
11050 | |
|
11051 | 0 | return map->mmaped; |
11052 | 0 | } |
11053 | | |
11054 | | bool bpf_map__is_internal(const struct bpf_map *map) |
11055 | 587 | { |
11056 | 587 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
11057 | 587 | } |
11058 | | |
11059 | | __u32 bpf_map__ifindex(const struct bpf_map *map) |
11060 | 0 | { |
11061 | 0 | return map->map_ifindex; |
11062 | 0 | } |
11063 | | |
11064 | | int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
11065 | 0 | { |
11066 | 0 | if (map_is_created(map)) |
11067 | 0 | return libbpf_err(-EBUSY); |
11068 | 0 | map->map_ifindex = ifindex; |
11069 | 0 | return 0; |
11070 | 0 | } |
11071 | | |
11072 | | int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) |
11073 | 0 | { |
11074 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) { |
11075 | 0 | pr_warn("error: unsupported map type\n"); |
11076 | 0 | return libbpf_err(-EINVAL); |
11077 | 0 | } |
11078 | 0 | if (map->inner_map_fd != -1) { |
11079 | 0 | pr_warn("error: inner_map_fd already specified\n"); |
11080 | 0 | return libbpf_err(-EINVAL); |
11081 | 0 | } |
11082 | 0 | if (map->inner_map) { |
11083 | 0 | bpf_map__destroy(map->inner_map); |
11084 | 0 | zfree(&map->inner_map); |
11085 | 0 | } |
11086 | 0 | map->inner_map_fd = fd; |
11087 | 0 | return 0; |
11088 | 0 | } |
11089 | | |
11090 | | int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_program *prog) |
11091 | 0 | { |
11092 | 0 | if (map_is_created(map)) { |
11093 | 0 | pr_warn("exclusive programs must be set before map creation\n"); |
11094 | 0 | return libbpf_err(-EINVAL); |
11095 | 0 | } |
11096 | | |
11097 | 0 | if (map->obj != prog->obj) { |
11098 | 0 | pr_warn("excl_prog and map must be from the same bpf object\n"); |
11099 | 0 | return libbpf_err(-EINVAL); |
11100 | 0 | } |
11101 | | |
11102 | 0 | map->excl_prog = prog; |
11103 | 0 | return 0; |
11104 | 0 | } |
11105 | | |
11106 | | struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map) |
11107 | 0 | { |
11108 | 0 | return map->excl_prog; |
11109 | 0 | } |
11110 | | |
11111 | | static struct bpf_map * |
11112 | | __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) |
11113 | 0 | { |
11114 | 0 | ssize_t idx; |
11115 | 0 | struct bpf_map *s, *e; |
11116 | |
|
11117 | 0 | if (!obj || !obj->maps) |
11118 | 0 | return errno = EINVAL, NULL; |
11119 | | |
11120 | 0 | s = obj->maps; |
11121 | 0 | e = obj->maps + obj->nr_maps; |
11122 | |
|
11123 | 0 | if ((m < s) || (m >= e)) { |
11124 | 0 | pr_warn("error in %s: map handler doesn't belong to object\n", |
11125 | 0 | __func__); |
11126 | 0 | return errno = EINVAL, NULL; |
11127 | 0 | } |
11128 | | |
11129 | 0 | idx = (m - obj->maps) + i; |
11130 | 0 | if (idx >= obj->nr_maps || idx < 0) |
11131 | 0 | return NULL; |
11132 | 0 | return &obj->maps[idx]; |
11133 | 0 | } |
11134 | | |
11135 | | struct bpf_map * |
11136 | | bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev) |
11137 | 0 | { |
11138 | 0 | if (prev == NULL && obj != NULL) |
11139 | 0 | return obj->maps; |
11140 | | |
11141 | 0 | return __bpf_map__iter(prev, obj, 1); |
11142 | 0 | } |
11143 | | |
11144 | | struct bpf_map * |
11145 | | bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next) |
11146 | 0 | { |
11147 | 0 | if (next == NULL && obj != NULL) { |
11148 | 0 | if (!obj->nr_maps) |
11149 | 0 | return NULL; |
11150 | 0 | return obj->maps + obj->nr_maps - 1; |
11151 | 0 | } |
11152 | | |
11153 | 0 | return __bpf_map__iter(next, obj, -1); |
11154 | 0 | } |
11155 | | |
11156 | | struct bpf_map * |
11157 | | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name) |
11158 | 0 | { |
11159 | 0 | struct bpf_map *pos; |
11160 | |
|
11161 | 0 | bpf_object__for_each_map(pos, obj) { |
11162 | | /* if it's a special internal map name (which always starts |
11163 | | * with dot) then check if that special name matches the |
11164 | | * real map name (ELF section name) |
11165 | | */ |
11166 | 0 | if (name[0] == '.') { |
11167 | 0 | if (pos->real_name && strcmp(pos->real_name, name) == 0) |
11168 | 0 | return pos; |
11169 | 0 | continue; |
11170 | 0 | } |
11171 | | /* otherwise map name has to be an exact match */ |
11172 | 0 | if (map_uses_real_name(pos)) { |
11173 | 0 | if (strcmp(pos->real_name, name) == 0) |
11174 | 0 | return pos; |
11175 | 0 | continue; |
11176 | 0 | } |
11177 | 0 | if (strcmp(pos->name, name) == 0) |
11178 | 0 | return pos; |
11179 | 0 | } |
11180 | 0 | return errno = ENOENT, NULL; |
11181 | 0 | } |
11182 | | |
11183 | | int |
11184 | | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) |
11185 | 0 | { |
11186 | 0 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
11187 | 0 | } |
11188 | | |
11189 | | static int validate_map_op(const struct bpf_map *map, size_t key_sz, |
11190 | | size_t value_sz, bool check_value_sz, __u64 flags) |
11191 | 0 | { |
11192 | 0 | if (!map_is_created(map)) /* map is not yet created */ |
11193 | 0 | return -ENOENT; |
11194 | | |
11195 | 0 | if (map->def.key_size != key_sz) { |
11196 | 0 | pr_warn("map '%s': unexpected key size %zu provided, expected %u\n", |
11197 | 0 | map->name, key_sz, map->def.key_size); |
11198 | 0 | return -EINVAL; |
11199 | 0 | } |
11200 | | |
11201 | 0 | if (map->fd < 0) { |
11202 | 0 | pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); |
11203 | 0 | return -EINVAL; |
11204 | 0 | } |
11205 | | |
11206 | 0 | if (!check_value_sz) |
11207 | 0 | return 0; |
11208 | | |
11209 | 0 | switch (map->def.type) { |
11210 | 0 | case BPF_MAP_TYPE_PERCPU_ARRAY: |
11211 | 0 | case BPF_MAP_TYPE_PERCPU_HASH: |
11212 | 0 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
11213 | 0 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: { |
11214 | 0 | int num_cpu = libbpf_num_possible_cpus(); |
11215 | 0 | size_t elem_sz = roundup(map->def.value_size, 8); |
11216 | |
|
11217 | 0 | if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) { |
11218 | 0 | if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) { |
11219 | 0 | pr_warn("map '%s': BPF_F_CPU and BPF_F_ALL_CPUS are mutually exclusive\n", |
11220 | 0 | map->name); |
11221 | 0 | return -EINVAL; |
11222 | 0 | } |
11223 | 0 | if (map->def.value_size != value_sz) { |
11224 | 0 | pr_warn("map '%s': unexpected value size %zu provided for either BPF_F_CPU or BPF_F_ALL_CPUS, expected %u\n", |
11225 | 0 | map->name, value_sz, map->def.value_size); |
11226 | 0 | return -EINVAL; |
11227 | 0 | } |
11228 | 0 | break; |
11229 | 0 | } |
11230 | | |
11231 | 0 | if (value_sz != num_cpu * elem_sz) { |
11232 | 0 | pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zu\n", |
11233 | 0 | map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); |
11234 | 0 | return -EINVAL; |
11235 | 0 | } |
11236 | 0 | break; |
11237 | 0 | } |
11238 | 0 | default: |
11239 | 0 | if (map->def.value_size != value_sz) { |
11240 | 0 | pr_warn("map '%s': unexpected value size %zu provided, expected %u\n", |
11241 | 0 | map->name, value_sz, map->def.value_size); |
11242 | 0 | return -EINVAL; |
11243 | 0 | } |
11244 | 0 | break; |
11245 | 0 | } |
11246 | 0 | return 0; |
11247 | 0 | } |
11248 | | |
11249 | | int bpf_map__lookup_elem(const struct bpf_map *map, |
11250 | | const void *key, size_t key_sz, |
11251 | | void *value, size_t value_sz, __u64 flags) |
11252 | 0 | { |
11253 | 0 | int err; |
11254 | |
|
11255 | 0 | err = validate_map_op(map, key_sz, value_sz, true, flags); |
11256 | 0 | if (err) |
11257 | 0 | return libbpf_err(err); |
11258 | | |
11259 | 0 | return bpf_map_lookup_elem_flags(map->fd, key, value, flags); |
11260 | 0 | } |
11261 | | |
11262 | | int bpf_map__update_elem(const struct bpf_map *map, |
11263 | | const void *key, size_t key_sz, |
11264 | | const void *value, size_t value_sz, __u64 flags) |
11265 | 0 | { |
11266 | 0 | int err; |
11267 | |
|
11268 | 0 | err = validate_map_op(map, key_sz, value_sz, true, flags); |
11269 | 0 | if (err) |
11270 | 0 | return libbpf_err(err); |
11271 | | |
11272 | 0 | return bpf_map_update_elem(map->fd, key, value, flags); |
11273 | 0 | } |
11274 | | |
11275 | | int bpf_map__delete_elem(const struct bpf_map *map, |
11276 | | const void *key, size_t key_sz, __u64 flags) |
11277 | 0 | { |
11278 | 0 | int err; |
11279 | |
|
11280 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags); |
11281 | 0 | if (err) |
11282 | 0 | return libbpf_err(err); |
11283 | | |
11284 | 0 | return bpf_map_delete_elem_flags(map->fd, key, flags); |
11285 | 0 | } |
11286 | | |
11287 | | int bpf_map__lookup_and_delete_elem(const struct bpf_map *map, |
11288 | | const void *key, size_t key_sz, |
11289 | | void *value, size_t value_sz, __u64 flags) |
11290 | 0 | { |
11291 | 0 | int err; |
11292 | |
|
11293 | 0 | err = validate_map_op(map, key_sz, value_sz, true, flags); |
11294 | 0 | if (err) |
11295 | 0 | return libbpf_err(err); |
11296 | | |
11297 | 0 | return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags); |
11298 | 0 | } |
11299 | | |
11300 | | int bpf_map__get_next_key(const struct bpf_map *map, |
11301 | | const void *cur_key, void *next_key, size_t key_sz) |
11302 | 0 | { |
11303 | 0 | int err; |
11304 | |
|
11305 | 0 | err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0); |
11306 | 0 | if (err) |
11307 | 0 | return libbpf_err(err); |
11308 | | |
11309 | 0 | return bpf_map_get_next_key(map->fd, cur_key, next_key); |
11310 | 0 | } |
11311 | | |
11312 | | long libbpf_get_error(const void *ptr) |
11313 | 14.8k | { |
11314 | 14.8k | if (!IS_ERR_OR_NULL(ptr)) |
11315 | 3.99k | return 0; |
11316 | | |
11317 | 10.8k | if (IS_ERR(ptr)) |
11318 | 10.8k | errno = -PTR_ERR(ptr); |
11319 | | |
11320 | | /* If ptr == NULL, then errno should be already set by the failing |
11321 | | * API, because libbpf never returns NULL on success and it now always |
11322 | | * sets errno on error. So no extra errno handling for ptr == NULL |
11323 | | * case. |
11324 | | */ |
11325 | 10.8k | return -errno; |
11326 | 14.8k | } |
11327 | | |
11328 | | /* Replace link's underlying BPF program with the new one */ |
11329 | | int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog) |
11330 | 0 | { |
11331 | 0 | int ret; |
11332 | 0 | int prog_fd = bpf_program__fd(prog); |
11333 | |
|
11334 | 0 | if (prog_fd < 0) { |
11335 | 0 | pr_warn("prog '%s': can't use BPF program without FD (was it loaded?)\n", |
11336 | 0 | prog->name); |
11337 | 0 | return libbpf_err(-EINVAL); |
11338 | 0 | } |
11339 | | |
11340 | 0 | ret = bpf_link_update(bpf_link__fd(link), prog_fd, NULL); |
11341 | 0 | return libbpf_err_errno(ret); |
11342 | 0 | } |
11343 | | |
11344 | | /* Release "ownership" of underlying BPF resource (typically, BPF program |
11345 | | * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected |
11346 | | * link, when destructed through bpf_link__destroy() call won't attempt to |
11347 | | * detach/unregisted that BPF resource. This is useful in situations where, |
11348 | | * say, attached BPF program has to outlive userspace program that attached it |
11349 | | * in the system. Depending on type of BPF program, though, there might be |
11350 | | * additional steps (like pinning BPF program in BPF FS) necessary to ensure |
11351 | | * exit of userspace program doesn't trigger automatic detachment and clean up |
11352 | | * inside the kernel. |
11353 | | */ |
11354 | | void bpf_link__disconnect(struct bpf_link *link) |
11355 | 0 | { |
11356 | 0 | link->disconnected = true; |
11357 | 0 | } |
11358 | | |
11359 | | int bpf_link__destroy(struct bpf_link *link) |
11360 | 0 | { |
11361 | 0 | int err = 0; |
11362 | |
|
11363 | 0 | if (IS_ERR_OR_NULL(link)) |
11364 | 0 | return 0; |
11365 | | |
11366 | 0 | if (!link->disconnected && link->detach) |
11367 | 0 | err = link->detach(link); |
11368 | 0 | if (link->pin_path) |
11369 | 0 | free(link->pin_path); |
11370 | 0 | if (link->dealloc) |
11371 | 0 | link->dealloc(link); |
11372 | 0 | else |
11373 | 0 | free(link); |
11374 | |
|
11375 | 0 | return libbpf_err(err); |
11376 | 0 | } |
11377 | | |
11378 | | int bpf_link__fd(const struct bpf_link *link) |
11379 | 0 | { |
11380 | 0 | return link->fd; |
11381 | 0 | } |
11382 | | |
11383 | | const char *bpf_link__pin_path(const struct bpf_link *link) |
11384 | 0 | { |
11385 | 0 | return link->pin_path; |
11386 | 0 | } |
11387 | | |
11388 | | static int bpf_link__detach_fd(struct bpf_link *link) |
11389 | 0 | { |
11390 | 0 | return libbpf_err_errno(close(link->fd)); |
11391 | 0 | } |
11392 | | |
11393 | | struct bpf_link *bpf_link__open(const char *path) |
11394 | 0 | { |
11395 | 0 | struct bpf_link *link; |
11396 | 0 | int fd; |
11397 | |
|
11398 | 0 | fd = bpf_obj_get(path); |
11399 | 0 | if (fd < 0) { |
11400 | 0 | fd = -errno; |
11401 | 0 | pr_warn("failed to open link at %s: %d\n", path, fd); |
11402 | 0 | return libbpf_err_ptr(fd); |
11403 | 0 | } |
11404 | | |
11405 | 0 | link = calloc(1, sizeof(*link)); |
11406 | 0 | if (!link) { |
11407 | 0 | close(fd); |
11408 | 0 | return libbpf_err_ptr(-ENOMEM); |
11409 | 0 | } |
11410 | 0 | link->detach = &bpf_link__detach_fd; |
11411 | 0 | link->fd = fd; |
11412 | |
|
11413 | 0 | link->pin_path = strdup(path); |
11414 | 0 | if (!link->pin_path) { |
11415 | 0 | bpf_link__destroy(link); |
11416 | 0 | return libbpf_err_ptr(-ENOMEM); |
11417 | 0 | } |
11418 | | |
11419 | 0 | return link; |
11420 | 0 | } |
11421 | | |
11422 | | int bpf_link__detach(struct bpf_link *link) |
11423 | 0 | { |
11424 | 0 | return bpf_link_detach(link->fd) ? -errno : 0; |
11425 | 0 | } |
11426 | | |
11427 | | int bpf_link__pin(struct bpf_link *link, const char *path) |
11428 | 0 | { |
11429 | 0 | int err; |
11430 | |
|
11431 | 0 | if (link->pin_path) |
11432 | 0 | return libbpf_err(-EBUSY); |
11433 | 0 | err = make_parent_dir(path); |
11434 | 0 | if (err) |
11435 | 0 | return libbpf_err(err); |
11436 | 0 | err = check_path(path); |
11437 | 0 | if (err) |
11438 | 0 | return libbpf_err(err); |
11439 | | |
11440 | 0 | link->pin_path = strdup(path); |
11441 | 0 | if (!link->pin_path) |
11442 | 0 | return libbpf_err(-ENOMEM); |
11443 | | |
11444 | 0 | if (bpf_obj_pin(link->fd, link->pin_path)) { |
11445 | 0 | err = -errno; |
11446 | 0 | zfree(&link->pin_path); |
11447 | 0 | return libbpf_err(err); |
11448 | 0 | } |
11449 | | |
11450 | 0 | pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); |
11451 | 0 | return 0; |
11452 | 0 | } |
11453 | | |
11454 | | int bpf_link__unpin(struct bpf_link *link) |
11455 | 0 | { |
11456 | 0 | int err; |
11457 | |
|
11458 | 0 | if (!link->pin_path) |
11459 | 0 | return libbpf_err(-EINVAL); |
11460 | | |
11461 | 0 | err = unlink(link->pin_path); |
11462 | 0 | if (err != 0) |
11463 | 0 | return -errno; |
11464 | | |
11465 | 0 | pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); |
11466 | 0 | zfree(&link->pin_path); |
11467 | 0 | return 0; |
11468 | 0 | } |
11469 | | |
11470 | | struct bpf_link_perf { |
11471 | | struct bpf_link link; |
11472 | | int perf_event_fd; |
11473 | | /* legacy kprobe support: keep track of probe identifier and type */ |
11474 | | char *legacy_probe_name; |
11475 | | bool legacy_is_kprobe; |
11476 | | bool legacy_is_retprobe; |
11477 | | }; |
11478 | | |
11479 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe); |
11480 | | static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe); |
11481 | | |
11482 | | static int bpf_link_perf_detach(struct bpf_link *link) |
11483 | 0 | { |
11484 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
11485 | 0 | int err = 0; |
11486 | |
|
11487 | 0 | if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0) |
11488 | 0 | err = -errno; |
11489 | |
|
11490 | 0 | if (perf_link->perf_event_fd != link->fd) |
11491 | 0 | close(perf_link->perf_event_fd); |
11492 | 0 | close(link->fd); |
11493 | | |
11494 | | /* legacy uprobe/kprobe needs to be removed after perf event fd closure */ |
11495 | 0 | if (perf_link->legacy_probe_name) { |
11496 | 0 | if (perf_link->legacy_is_kprobe) { |
11497 | 0 | err = remove_kprobe_event_legacy(perf_link->legacy_probe_name, |
11498 | 0 | perf_link->legacy_is_retprobe); |
11499 | 0 | } else { |
11500 | 0 | err = remove_uprobe_event_legacy(perf_link->legacy_probe_name, |
11501 | 0 | perf_link->legacy_is_retprobe); |
11502 | 0 | } |
11503 | 0 | } |
11504 | |
|
11505 | 0 | return err; |
11506 | 0 | } |
11507 | | |
11508 | | static void bpf_link_perf_dealloc(struct bpf_link *link) |
11509 | 0 | { |
11510 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
11511 | |
|
11512 | 0 | free(perf_link->legacy_probe_name); |
11513 | 0 | free(perf_link); |
11514 | 0 | } |
11515 | | |
11516 | | struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd, |
11517 | | const struct bpf_perf_event_opts *opts) |
11518 | 0 | { |
11519 | 0 | struct bpf_link_perf *link; |
11520 | 0 | int prog_fd, link_fd = -1, err; |
11521 | 0 | bool force_ioctl_attach; |
11522 | |
|
11523 | 0 | if (!OPTS_VALID(opts, bpf_perf_event_opts)) |
11524 | 0 | return libbpf_err_ptr(-EINVAL); |
11525 | | |
11526 | 0 | if (pfd < 0) { |
11527 | 0 | pr_warn("prog '%s': invalid perf event FD %d\n", |
11528 | 0 | prog->name, pfd); |
11529 | 0 | return libbpf_err_ptr(-EINVAL); |
11530 | 0 | } |
11531 | 0 | prog_fd = bpf_program__fd(prog); |
11532 | 0 | if (prog_fd < 0) { |
11533 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
11534 | 0 | prog->name); |
11535 | 0 | return libbpf_err_ptr(-EINVAL); |
11536 | 0 | } |
11537 | | |
11538 | 0 | link = calloc(1, sizeof(*link)); |
11539 | 0 | if (!link) |
11540 | 0 | return libbpf_err_ptr(-ENOMEM); |
11541 | 0 | link->link.detach = &bpf_link_perf_detach; |
11542 | 0 | link->link.dealloc = &bpf_link_perf_dealloc; |
11543 | 0 | link->perf_event_fd = pfd; |
11544 | |
|
11545 | 0 | force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false); |
11546 | 0 | if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) { |
11547 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts, |
11548 | 0 | .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0)); |
11549 | |
|
11550 | 0 | link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts); |
11551 | 0 | if (link_fd < 0) { |
11552 | 0 | err = -errno; |
11553 | 0 | pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %s\n", |
11554 | 0 | prog->name, pfd, errstr(err)); |
11555 | 0 | goto err_out; |
11556 | 0 | } |
11557 | 0 | link->link.fd = link_fd; |
11558 | 0 | } else { |
11559 | 0 | if (OPTS_GET(opts, bpf_cookie, 0)) { |
11560 | 0 | pr_warn("prog '%s': user context value is not supported\n", prog->name); |
11561 | 0 | err = -EOPNOTSUPP; |
11562 | 0 | goto err_out; |
11563 | 0 | } |
11564 | | |
11565 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { |
11566 | 0 | err = -errno; |
11567 | 0 | pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n", |
11568 | 0 | prog->name, pfd, errstr(err)); |
11569 | 0 | if (err == -EPROTO) |
11570 | 0 | pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n", |
11571 | 0 | prog->name, pfd); |
11572 | 0 | goto err_out; |
11573 | 0 | } |
11574 | 0 | link->link.fd = pfd; |
11575 | 0 | } |
11576 | | |
11577 | 0 | if (!OPTS_GET(opts, dont_enable, false)) { |
11578 | 0 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
11579 | 0 | err = -errno; |
11580 | 0 | pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n", |
11581 | 0 | prog->name, pfd, errstr(err)); |
11582 | 0 | goto err_out; |
11583 | 0 | } |
11584 | 0 | } |
11585 | | |
11586 | 0 | return &link->link; |
11587 | 0 | err_out: |
11588 | 0 | if (link_fd >= 0) |
11589 | 0 | close(link_fd); |
11590 | 0 | free(link); |
11591 | 0 | return libbpf_err_ptr(err); |
11592 | 0 | } |
11593 | | |
11594 | | struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd) |
11595 | 0 | { |
11596 | 0 | return bpf_program__attach_perf_event_opts(prog, pfd, NULL); |
11597 | 0 | } |
11598 | | |
11599 | | /* |
11600 | | * this function is expected to parse integer in the range of [0, 2^31-1] from |
11601 | | * given file using scanf format string fmt. If actual parsed value is |
11602 | | * negative, the result might be indistinguishable from error |
11603 | | */ |
11604 | | static int parse_uint_from_file(const char *file, const char *fmt) |
11605 | 0 | { |
11606 | 0 | int err, ret; |
11607 | 0 | FILE *f; |
11608 | |
|
11609 | 0 | f = fopen(file, "re"); |
11610 | 0 | if (!f) { |
11611 | 0 | err = -errno; |
11612 | 0 | pr_debug("failed to open '%s': %s\n", file, errstr(err)); |
11613 | 0 | return err; |
11614 | 0 | } |
11615 | 0 | err = fscanf(f, fmt, &ret); |
11616 | 0 | if (err != 1) { |
11617 | 0 | err = err == EOF ? -EIO : -errno; |
11618 | 0 | pr_debug("failed to parse '%s': %s\n", file, errstr(err)); |
11619 | 0 | fclose(f); |
11620 | 0 | return err; |
11621 | 0 | } |
11622 | 0 | fclose(f); |
11623 | 0 | return ret; |
11624 | 0 | } |
11625 | | |
11626 | | static int determine_kprobe_perf_type(void) |
11627 | 0 | { |
11628 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/type"; |
11629 | |
|
11630 | 0 | return parse_uint_from_file(file, "%d\n"); |
11631 | 0 | } |
11632 | | |
11633 | | static int determine_uprobe_perf_type(void) |
11634 | 0 | { |
11635 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/type"; |
11636 | |
|
11637 | 0 | return parse_uint_from_file(file, "%d\n"); |
11638 | 0 | } |
11639 | | |
11640 | | static int determine_kprobe_retprobe_bit(void) |
11641 | 0 | { |
11642 | 0 | const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe"; |
11643 | |
|
11644 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
11645 | 0 | } |
11646 | | |
11647 | | static int determine_uprobe_retprobe_bit(void) |
11648 | 0 | { |
11649 | 0 | const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; |
11650 | |
|
11651 | 0 | return parse_uint_from_file(file, "config:%d\n"); |
11652 | 0 | } |
11653 | | |
11654 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32 |
11655 | 0 | #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32 |
11656 | | |
11657 | | static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, |
11658 | | uint64_t offset, int pid, size_t ref_ctr_off) |
11659 | 0 | { |
11660 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11661 | 0 | struct perf_event_attr attr; |
11662 | 0 | int type, pfd; |
11663 | |
|
11664 | 0 | if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS)) |
11665 | 0 | return -EINVAL; |
11666 | | |
11667 | 0 | memset(&attr, 0, attr_sz); |
11668 | |
|
11669 | 0 | type = uprobe ? determine_uprobe_perf_type() |
11670 | 0 | : determine_kprobe_perf_type(); |
11671 | 0 | if (type < 0) { |
11672 | 0 | pr_warn("failed to determine %s perf type: %s\n", |
11673 | 0 | uprobe ? "uprobe" : "kprobe", |
11674 | 0 | errstr(type)); |
11675 | 0 | return type; |
11676 | 0 | } |
11677 | 0 | if (retprobe) { |
11678 | 0 | int bit = uprobe ? determine_uprobe_retprobe_bit() |
11679 | 0 | : determine_kprobe_retprobe_bit(); |
11680 | |
|
11681 | 0 | if (bit < 0) { |
11682 | 0 | pr_warn("failed to determine %s retprobe bit: %s\n", |
11683 | 0 | uprobe ? "uprobe" : "kprobe", |
11684 | 0 | errstr(bit)); |
11685 | 0 | return bit; |
11686 | 0 | } |
11687 | 0 | attr.config |= 1 << bit; |
11688 | 0 | } |
11689 | 0 | attr.size = attr_sz; |
11690 | 0 | attr.type = type; |
11691 | 0 | attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT; |
11692 | 0 | attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ |
11693 | 0 | attr.config2 = offset; /* kprobe_addr or probe_offset */ |
11694 | | |
11695 | | /* pid filter is meaningful only for uprobes */ |
11696 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
11697 | 0 | pid < 0 ? -1 : pid /* pid */, |
11698 | 0 | pid == -1 ? 0 : -1 /* cpu */, |
11699 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11700 | 0 | return pfd >= 0 ? pfd : -errno; |
11701 | 0 | } |
11702 | | |
11703 | | static int append_to_file(const char *file, const char *fmt, ...) |
11704 | 0 | { |
11705 | 0 | int fd, n, err = 0; |
11706 | 0 | va_list ap; |
11707 | 0 | char buf[1024]; |
11708 | |
|
11709 | 0 | va_start(ap, fmt); |
11710 | 0 | n = vsnprintf(buf, sizeof(buf), fmt, ap); |
11711 | 0 | va_end(ap); |
11712 | |
|
11713 | 0 | if (n < 0 || n >= sizeof(buf)) |
11714 | 0 | return -EINVAL; |
11715 | | |
11716 | 0 | fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0); |
11717 | 0 | if (fd < 0) |
11718 | 0 | return -errno; |
11719 | | |
11720 | 0 | if (write(fd, buf, n) < 0) |
11721 | 0 | err = -errno; |
11722 | |
|
11723 | 0 | close(fd); |
11724 | 0 | return err; |
11725 | 0 | } |
11726 | | |
11727 | 0 | #define DEBUGFS "/sys/kernel/debug/tracing" |
11728 | 0 | #define TRACEFS "/sys/kernel/tracing" |
11729 | | |
11730 | | static bool use_debugfs(void) |
11731 | 0 | { |
11732 | 0 | static int has_debugfs = -1; |
11733 | |
|
11734 | 0 | if (has_debugfs < 0) |
11735 | 0 | has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0; |
11736 | |
|
11737 | 0 | return has_debugfs == 1; |
11738 | 0 | } |
11739 | | |
11740 | | static const char *tracefs_path(void) |
11741 | 0 | { |
11742 | 0 | return use_debugfs() ? DEBUGFS : TRACEFS; |
11743 | 0 | } |
11744 | | |
11745 | | static const char *tracefs_kprobe_events(void) |
11746 | 0 | { |
11747 | 0 | return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events"; |
11748 | 0 | } |
11749 | | |
11750 | | static const char *tracefs_uprobe_events(void) |
11751 | 0 | { |
11752 | 0 | return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events"; |
11753 | 0 | } |
11754 | | |
11755 | | static const char *tracefs_available_filter_functions(void) |
11756 | 0 | { |
11757 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions" |
11758 | 0 | : TRACEFS"/available_filter_functions"; |
11759 | 0 | } |
11760 | | |
11761 | | static const char *tracefs_available_filter_functions_addrs(void) |
11762 | 0 | { |
11763 | 0 | return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs" |
11764 | 0 | : TRACEFS"/available_filter_functions_addrs"; |
11765 | 0 | } |
11766 | | |
11767 | | static void gen_probe_legacy_event_name(char *buf, size_t buf_sz, |
11768 | | const char *name, size_t offset) |
11769 | 0 | { |
11770 | 0 | static int index = 0; |
11771 | 0 | int i; |
11772 | |
|
11773 | 0 | snprintf(buf, buf_sz, "libbpf_%d_%d_%s_0x%zx", getpid(), |
11774 | 0 | __sync_fetch_and_add(&index, 1), name, offset); |
11775 | | |
11776 | | /* sanitize name in the probe name */ |
11777 | 0 | for (i = 0; buf[i]; i++) { |
11778 | 0 | if (!isalnum(buf[i])) |
11779 | 0 | buf[i] = '_'; |
11780 | 0 | } |
11781 | 0 | } |
11782 | | |
11783 | | static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, |
11784 | | const char *kfunc_name, size_t offset) |
11785 | 0 | { |
11786 | 0 | return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx", |
11787 | 0 | retprobe ? 'r' : 'p', |
11788 | 0 | retprobe ? "kretprobes" : "kprobes", |
11789 | 0 | probe_name, kfunc_name, offset); |
11790 | 0 | } |
11791 | | |
11792 | | static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe) |
11793 | 0 | { |
11794 | 0 | return append_to_file(tracefs_kprobe_events(), "-:%s/%s", |
11795 | 0 | retprobe ? "kretprobes" : "kprobes", probe_name); |
11796 | 0 | } |
11797 | | |
11798 | | static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
11799 | 0 | { |
11800 | 0 | char file[256]; |
11801 | |
|
11802 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
11803 | 0 | tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name); |
11804 | |
|
11805 | 0 | return parse_uint_from_file(file, "%d\n"); |
11806 | 0 | } |
11807 | | |
11808 | | static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, |
11809 | | const char *kfunc_name, size_t offset, int pid) |
11810 | 0 | { |
11811 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
11812 | 0 | struct perf_event_attr attr; |
11813 | 0 | int type, pfd, err; |
11814 | |
|
11815 | 0 | err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset); |
11816 | 0 | if (err < 0) { |
11817 | 0 | pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n", |
11818 | 0 | kfunc_name, offset, |
11819 | 0 | errstr(err)); |
11820 | 0 | return err; |
11821 | 0 | } |
11822 | 0 | type = determine_kprobe_perf_type_legacy(probe_name, retprobe); |
11823 | 0 | if (type < 0) { |
11824 | 0 | err = type; |
11825 | 0 | pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n", |
11826 | 0 | kfunc_name, offset, |
11827 | 0 | errstr(err)); |
11828 | 0 | goto err_clean_legacy; |
11829 | 0 | } |
11830 | | |
11831 | 0 | memset(&attr, 0, attr_sz); |
11832 | 0 | attr.size = attr_sz; |
11833 | 0 | attr.config = type; |
11834 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
11835 | |
|
11836 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
11837 | 0 | pid < 0 ? -1 : pid, /* pid */ |
11838 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
11839 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
11840 | 0 | if (pfd < 0) { |
11841 | 0 | err = -errno; |
11842 | 0 | pr_warn("legacy kprobe perf_event_open() failed: %s\n", |
11843 | 0 | errstr(err)); |
11844 | 0 | goto err_clean_legacy; |
11845 | 0 | } |
11846 | 0 | return pfd; |
11847 | | |
11848 | 0 | err_clean_legacy: |
11849 | | /* Clear the newly added legacy kprobe_event */ |
11850 | 0 | remove_kprobe_event_legacy(probe_name, retprobe); |
11851 | 0 | return err; |
11852 | 0 | } |
11853 | | |
11854 | | static const char *arch_specific_syscall_pfx(void) |
11855 | 0 | { |
11856 | 0 | #if defined(__x86_64__) |
11857 | 0 | return "x64"; |
11858 | | #elif defined(__i386__) |
11859 | | return "ia32"; |
11860 | | #elif defined(__s390x__) |
11861 | | return "s390x"; |
11862 | | #elif defined(__arm__) |
11863 | | return "arm"; |
11864 | | #elif defined(__aarch64__) |
11865 | | return "arm64"; |
11866 | | #elif defined(__mips__) |
11867 | | return "mips"; |
11868 | | #elif defined(__riscv) |
11869 | | return "riscv"; |
11870 | | #elif defined(__powerpc__) |
11871 | | return "powerpc"; |
11872 | | #elif defined(__powerpc64__) |
11873 | | return "powerpc64"; |
11874 | | #else |
11875 | | return NULL; |
11876 | | #endif |
11877 | 0 | } |
11878 | | |
11879 | | int probe_kern_syscall_wrapper(int token_fd) |
11880 | 0 | { |
11881 | 0 | char syscall_name[64]; |
11882 | 0 | const char *ksys_pfx; |
11883 | |
|
11884 | 0 | ksys_pfx = arch_specific_syscall_pfx(); |
11885 | 0 | if (!ksys_pfx) |
11886 | 0 | return 0; |
11887 | | |
11888 | 0 | snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx); |
11889 | |
|
11890 | 0 | if (determine_kprobe_perf_type() >= 0) { |
11891 | 0 | int pfd; |
11892 | |
|
11893 | 0 | pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0); |
11894 | 0 | if (pfd >= 0) |
11895 | 0 | close(pfd); |
11896 | |
|
11897 | 0 | return pfd >= 0 ? 1 : 0; |
11898 | 0 | } else { /* legacy mode */ |
11899 | 0 | char probe_name[MAX_EVENT_NAME_LEN]; |
11900 | |
|
11901 | 0 | gen_probe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0); |
11902 | 0 | if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0) |
11903 | 0 | return 0; |
11904 | | |
11905 | 0 | (void)remove_kprobe_event_legacy(probe_name, false); |
11906 | 0 | return 1; |
11907 | 0 | } |
11908 | 0 | } |
11909 | | |
11910 | | struct bpf_link * |
11911 | | bpf_program__attach_kprobe_opts(const struct bpf_program *prog, |
11912 | | const char *func_name, |
11913 | | const struct bpf_kprobe_opts *opts) |
11914 | 0 | { |
11915 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
11916 | 0 | enum probe_attach_mode attach_mode; |
11917 | 0 | char *legacy_probe = NULL; |
11918 | 0 | struct bpf_link *link; |
11919 | 0 | size_t offset; |
11920 | 0 | bool retprobe, legacy; |
11921 | 0 | int pfd, err; |
11922 | |
|
11923 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_opts)) |
11924 | 0 | return libbpf_err_ptr(-EINVAL); |
11925 | | |
11926 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
11927 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
11928 | 0 | offset = OPTS_GET(opts, offset, 0); |
11929 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
11930 | |
|
11931 | 0 | legacy = determine_kprobe_perf_type() < 0; |
11932 | 0 | switch (attach_mode) { |
11933 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
11934 | 0 | legacy = true; |
11935 | 0 | pe_opts.force_ioctl_attach = true; |
11936 | 0 | break; |
11937 | 0 | case PROBE_ATTACH_MODE_PERF: |
11938 | 0 | if (legacy) |
11939 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11940 | 0 | pe_opts.force_ioctl_attach = true; |
11941 | 0 | break; |
11942 | 0 | case PROBE_ATTACH_MODE_LINK: |
11943 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
11944 | 0 | return libbpf_err_ptr(-ENOTSUP); |
11945 | 0 | break; |
11946 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
11947 | 0 | break; |
11948 | 0 | default: |
11949 | 0 | return libbpf_err_ptr(-EINVAL); |
11950 | 0 | } |
11951 | 0 | if (!func_name && legacy) |
11952 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
11953 | | |
11954 | 0 | if (!legacy) { |
11955 | 0 | pfd = perf_event_open_probe(false /* uprobe */, retprobe, |
11956 | 0 | func_name, offset, |
11957 | 0 | -1 /* pid */, 0 /* ref_ctr_off */); |
11958 | 0 | } else { |
11959 | 0 | char probe_name[MAX_EVENT_NAME_LEN]; |
11960 | |
|
11961 | 0 | gen_probe_legacy_event_name(probe_name, sizeof(probe_name), |
11962 | 0 | func_name, offset); |
11963 | |
|
11964 | 0 | legacy_probe = strdup(probe_name); |
11965 | 0 | if (!legacy_probe) |
11966 | 0 | return libbpf_err_ptr(-ENOMEM); |
11967 | | |
11968 | 0 | pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name, |
11969 | 0 | offset, -1 /* pid */); |
11970 | 0 | } |
11971 | 0 | if (pfd < 0) { |
11972 | 0 | err = pfd; |
11973 | 0 | pr_warn("prog '%s': failed to create %s '%s%s0x%zx' perf event: %s\n", |
11974 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
11975 | 0 | func_name ?: "", func_name ? "+" : "", |
11976 | 0 | offset, errstr(err)); |
11977 | 0 | goto err_out; |
11978 | 0 | } |
11979 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
11980 | 0 | err = libbpf_get_error(link); |
11981 | 0 | if (err) { |
11982 | 0 | close(pfd); |
11983 | 0 | pr_warn("prog '%s': failed to attach to %s '%s%s0x%zx': %s\n", |
11984 | 0 | prog->name, retprobe ? "kretprobe" : "kprobe", |
11985 | 0 | func_name ?: "", func_name ? "+" : "", |
11986 | 0 | offset, errstr(err)); |
11987 | 0 | goto err_clean_legacy; |
11988 | 0 | } |
11989 | 0 | if (legacy) { |
11990 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
11991 | |
|
11992 | 0 | perf_link->legacy_probe_name = legacy_probe; |
11993 | 0 | perf_link->legacy_is_kprobe = true; |
11994 | 0 | perf_link->legacy_is_retprobe = retprobe; |
11995 | 0 | } |
11996 | |
|
11997 | 0 | return link; |
11998 | | |
11999 | 0 | err_clean_legacy: |
12000 | 0 | if (legacy) |
12001 | 0 | remove_kprobe_event_legacy(legacy_probe, retprobe); |
12002 | 0 | err_out: |
12003 | 0 | free(legacy_probe); |
12004 | 0 | return libbpf_err_ptr(err); |
12005 | 0 | } |
12006 | | |
12007 | | struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, |
12008 | | bool retprobe, |
12009 | | const char *func_name) |
12010 | 0 | { |
12011 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts, |
12012 | 0 | .retprobe = retprobe, |
12013 | 0 | ); |
12014 | |
|
12015 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &opts); |
12016 | 0 | } |
12017 | | |
12018 | | struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog, |
12019 | | const char *syscall_name, |
12020 | | const struct bpf_ksyscall_opts *opts) |
12021 | 0 | { |
12022 | 0 | LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); |
12023 | 0 | char func_name[128]; |
12024 | |
|
12025 | 0 | if (!OPTS_VALID(opts, bpf_ksyscall_opts)) |
12026 | 0 | return libbpf_err_ptr(-EINVAL); |
12027 | | |
12028 | 0 | if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) { |
12029 | | /* arch_specific_syscall_pfx() should never return NULL here |
12030 | | * because it is guarded by kernel_supports(). However, since |
12031 | | * compiler does not know that we have an explicit conditional |
12032 | | * as well. |
12033 | | */ |
12034 | 0 | snprintf(func_name, sizeof(func_name), "__%s_sys_%s", |
12035 | 0 | arch_specific_syscall_pfx() ? : "", syscall_name); |
12036 | 0 | } else { |
12037 | 0 | snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name); |
12038 | 0 | } |
12039 | |
|
12040 | 0 | kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false); |
12041 | 0 | kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
12042 | |
|
12043 | 0 | return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts); |
12044 | 0 | } |
12045 | | |
12046 | | /* Adapted from perf/util/string.c */ |
12047 | | bool glob_match(const char *str, const char *pat) |
12048 | 0 | { |
12049 | 0 | while (*str && *pat && *pat != '*') { |
12050 | 0 | if (*pat == '?') { /* Matches any single character */ |
12051 | 0 | str++; |
12052 | 0 | pat++; |
12053 | 0 | continue; |
12054 | 0 | } |
12055 | 0 | if (*str != *pat) |
12056 | 0 | return false; |
12057 | 0 | str++; |
12058 | 0 | pat++; |
12059 | 0 | } |
12060 | | /* Check wild card */ |
12061 | 0 | if (*pat == '*') { |
12062 | 0 | while (*pat == '*') |
12063 | 0 | pat++; |
12064 | 0 | if (!*pat) /* Tail wild card matches all */ |
12065 | 0 | return true; |
12066 | 0 | while (*str) |
12067 | 0 | if (glob_match(str++, pat)) |
12068 | 0 | return true; |
12069 | 0 | } |
12070 | 0 | return !*str && !*pat; |
12071 | 0 | } |
12072 | | |
12073 | | struct kprobe_multi_resolve { |
12074 | | const char *pattern; |
12075 | | unsigned long *addrs; |
12076 | | size_t cap; |
12077 | | size_t cnt; |
12078 | | }; |
12079 | | |
12080 | | struct avail_kallsyms_data { |
12081 | | char **syms; |
12082 | | size_t cnt; |
12083 | | struct kprobe_multi_resolve *res; |
12084 | | }; |
12085 | | |
12086 | | static int avail_func_cmp(const void *a, const void *b) |
12087 | 0 | { |
12088 | 0 | return strcmp(*(const char **)a, *(const char **)b); |
12089 | 0 | } |
12090 | | |
12091 | | static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type, |
12092 | | const char *sym_name, void *ctx) |
12093 | 0 | { |
12094 | 0 | struct avail_kallsyms_data *data = ctx; |
12095 | 0 | struct kprobe_multi_resolve *res = data->res; |
12096 | 0 | int err; |
12097 | |
|
12098 | 0 | if (!glob_match(sym_name, res->pattern)) |
12099 | 0 | return 0; |
12100 | | |
12101 | 0 | if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) { |
12102 | | /* Some versions of kernel strip out .llvm.<hash> suffix from |
12103 | | * function names reported in available_filter_functions, but |
12104 | | * don't do so for kallsyms. While this is clearly a kernel |
12105 | | * bug (fixed by [0]) we try to accommodate that in libbpf to |
12106 | | * make multi-kprobe usability a bit better: if no match is |
12107 | | * found, we will strip .llvm. suffix and try one more time. |
12108 | | * |
12109 | | * [0] fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG") |
12110 | | */ |
12111 | 0 | char sym_trim[256], *psym_trim = sym_trim; |
12112 | 0 | const char *sym_sfx; |
12113 | |
|
12114 | 0 | if (!(sym_sfx = strstr(sym_name, ".llvm."))) |
12115 | 0 | return 0; |
12116 | | |
12117 | | /* psym_trim vs sym_trim dance is done to avoid pointer vs array |
12118 | | * coercion differences and get proper `const char **` pointer |
12119 | | * which avail_func_cmp() expects |
12120 | | */ |
12121 | 0 | snprintf(sym_trim, sizeof(sym_trim), "%.*s", (int)(sym_sfx - sym_name), sym_name); |
12122 | 0 | if (!bsearch(&psym_trim, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) |
12123 | 0 | return 0; |
12124 | 0 | } |
12125 | | |
12126 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1); |
12127 | 0 | if (err) |
12128 | 0 | return err; |
12129 | | |
12130 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
12131 | 0 | return 0; |
12132 | 0 | } |
12133 | | |
12134 | | static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res) |
12135 | 0 | { |
12136 | 0 | const char *available_functions_file = tracefs_available_filter_functions(); |
12137 | 0 | struct avail_kallsyms_data data; |
12138 | 0 | char sym_name[500]; |
12139 | 0 | FILE *f; |
12140 | 0 | int err = 0, ret, i; |
12141 | 0 | char **syms = NULL; |
12142 | 0 | size_t cap = 0, cnt = 0; |
12143 | |
|
12144 | 0 | f = fopen(available_functions_file, "re"); |
12145 | 0 | if (!f) { |
12146 | 0 | err = -errno; |
12147 | 0 | pr_warn("failed to open %s: %s\n", available_functions_file, errstr(err)); |
12148 | 0 | return err; |
12149 | 0 | } |
12150 | | |
12151 | 0 | while (true) { |
12152 | 0 | char *name; |
12153 | |
|
12154 | 0 | ret = fscanf(f, "%499s%*[^\n]\n", sym_name); |
12155 | 0 | if (ret == EOF && feof(f)) |
12156 | 0 | break; |
12157 | | |
12158 | 0 | if (ret != 1) { |
12159 | 0 | pr_warn("failed to parse available_filter_functions entry: %d\n", ret); |
12160 | 0 | err = -EINVAL; |
12161 | 0 | goto cleanup; |
12162 | 0 | } |
12163 | | |
12164 | 0 | if (!glob_match(sym_name, res->pattern)) |
12165 | 0 | continue; |
12166 | | |
12167 | 0 | err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1); |
12168 | 0 | if (err) |
12169 | 0 | goto cleanup; |
12170 | | |
12171 | 0 | name = strdup(sym_name); |
12172 | 0 | if (!name) { |
12173 | 0 | err = -errno; |
12174 | 0 | goto cleanup; |
12175 | 0 | } |
12176 | | |
12177 | 0 | syms[cnt++] = name; |
12178 | 0 | } |
12179 | | |
12180 | | /* no entries found, bail out */ |
12181 | 0 | if (cnt == 0) { |
12182 | 0 | err = -ENOENT; |
12183 | 0 | goto cleanup; |
12184 | 0 | } |
12185 | | |
12186 | | /* sort available functions */ |
12187 | 0 | qsort(syms, cnt, sizeof(*syms), avail_func_cmp); |
12188 | |
|
12189 | 0 | data.syms = syms; |
12190 | 0 | data.res = res; |
12191 | 0 | data.cnt = cnt; |
12192 | 0 | libbpf_kallsyms_parse(avail_kallsyms_cb, &data); |
12193 | |
|
12194 | 0 | if (res->cnt == 0) |
12195 | 0 | err = -ENOENT; |
12196 | |
|
12197 | 0 | cleanup: |
12198 | 0 | for (i = 0; i < cnt; i++) |
12199 | 0 | free((char *)syms[i]); |
12200 | 0 | free(syms); |
12201 | |
|
12202 | 0 | fclose(f); |
12203 | 0 | return err; |
12204 | 0 | } |
12205 | | |
12206 | | static bool has_available_filter_functions_addrs(void) |
12207 | 0 | { |
12208 | 0 | return access(tracefs_available_filter_functions_addrs(), R_OK) != -1; |
12209 | 0 | } |
12210 | | |
12211 | | static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res) |
12212 | 0 | { |
12213 | 0 | const char *available_path = tracefs_available_filter_functions_addrs(); |
12214 | 0 | char sym_name[500]; |
12215 | 0 | FILE *f; |
12216 | 0 | int ret, err = 0; |
12217 | 0 | unsigned long long sym_addr; |
12218 | |
|
12219 | 0 | f = fopen(available_path, "re"); |
12220 | 0 | if (!f) { |
12221 | 0 | err = -errno; |
12222 | 0 | pr_warn("failed to open %s: %s\n", available_path, errstr(err)); |
12223 | 0 | return err; |
12224 | 0 | } |
12225 | | |
12226 | 0 | while (true) { |
12227 | 0 | ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name); |
12228 | 0 | if (ret == EOF && feof(f)) |
12229 | 0 | break; |
12230 | | |
12231 | 0 | if (ret != 2) { |
12232 | 0 | pr_warn("failed to parse available_filter_functions_addrs entry: %d\n", |
12233 | 0 | ret); |
12234 | 0 | err = -EINVAL; |
12235 | 0 | goto cleanup; |
12236 | 0 | } |
12237 | | |
12238 | 0 | if (!glob_match(sym_name, res->pattern)) |
12239 | 0 | continue; |
12240 | | |
12241 | 0 | err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, |
12242 | 0 | sizeof(*res->addrs), res->cnt + 1); |
12243 | 0 | if (err) |
12244 | 0 | goto cleanup; |
12245 | | |
12246 | 0 | res->addrs[res->cnt++] = (unsigned long)sym_addr; |
12247 | 0 | } |
12248 | | |
12249 | 0 | if (res->cnt == 0) |
12250 | 0 | err = -ENOENT; |
12251 | |
|
12252 | 0 | cleanup: |
12253 | 0 | fclose(f); |
12254 | 0 | return err; |
12255 | 0 | } |
12256 | | |
12257 | | struct bpf_link * |
12258 | | bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, |
12259 | | const char *pattern, |
12260 | | const struct bpf_kprobe_multi_opts *opts) |
12261 | 0 | { |
12262 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
12263 | 0 | struct kprobe_multi_resolve res = { |
12264 | 0 | .pattern = pattern, |
12265 | 0 | }; |
12266 | 0 | enum bpf_attach_type attach_type; |
12267 | 0 | struct bpf_link *link = NULL; |
12268 | 0 | const unsigned long *addrs; |
12269 | 0 | int err, link_fd, prog_fd; |
12270 | 0 | bool retprobe, session, unique_match; |
12271 | 0 | const __u64 *cookies; |
12272 | 0 | const char **syms; |
12273 | 0 | size_t cnt; |
12274 | |
|
12275 | 0 | if (!OPTS_VALID(opts, bpf_kprobe_multi_opts)) |
12276 | 0 | return libbpf_err_ptr(-EINVAL); |
12277 | | |
12278 | 0 | prog_fd = bpf_program__fd(prog); |
12279 | 0 | if (prog_fd < 0) { |
12280 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
12281 | 0 | prog->name); |
12282 | 0 | return libbpf_err_ptr(-EINVAL); |
12283 | 0 | } |
12284 | | |
12285 | 0 | syms = OPTS_GET(opts, syms, false); |
12286 | 0 | addrs = OPTS_GET(opts, addrs, false); |
12287 | 0 | cnt = OPTS_GET(opts, cnt, false); |
12288 | 0 | cookies = OPTS_GET(opts, cookies, false); |
12289 | 0 | unique_match = OPTS_GET(opts, unique_match, false); |
12290 | |
|
12291 | 0 | if (!pattern && !addrs && !syms) |
12292 | 0 | return libbpf_err_ptr(-EINVAL); |
12293 | 0 | if (pattern && (addrs || syms || cookies || cnt)) |
12294 | 0 | return libbpf_err_ptr(-EINVAL); |
12295 | 0 | if (!pattern && !cnt) |
12296 | 0 | return libbpf_err_ptr(-EINVAL); |
12297 | 0 | if (!pattern && unique_match) |
12298 | 0 | return libbpf_err_ptr(-EINVAL); |
12299 | 0 | if (addrs && syms) |
12300 | 0 | return libbpf_err_ptr(-EINVAL); |
12301 | | |
12302 | | /* |
12303 | | * Exact function name (no wildcards) without unique_match: |
12304 | | * bypass kallsyms parsing and pass the symbol directly to the |
12305 | | * kernel via syms[] array. When unique_match is set, fall |
12306 | | * through to the slow path which detects duplicate symbols. |
12307 | | */ |
12308 | 0 | if (pattern && !strpbrk(pattern, "*?") && !unique_match) { |
12309 | 0 | syms = &pattern; |
12310 | 0 | cnt = 1; |
12311 | 0 | } else if (pattern) { |
12312 | 0 | if (has_available_filter_functions_addrs()) |
12313 | 0 | err = libbpf_available_kprobes_parse(&res); |
12314 | 0 | else |
12315 | 0 | err = libbpf_available_kallsyms_parse(&res); |
12316 | 0 | if (err) |
12317 | 0 | goto error; |
12318 | | |
12319 | 0 | if (unique_match && res.cnt != 1) { |
12320 | 0 | pr_warn("prog '%s': failed to find a unique match for '%s' (%zu matches)\n", |
12321 | 0 | prog->name, pattern, res.cnt); |
12322 | 0 | err = -EINVAL; |
12323 | 0 | goto error; |
12324 | 0 | } |
12325 | | |
12326 | 0 | addrs = res.addrs; |
12327 | 0 | cnt = res.cnt; |
12328 | 0 | } |
12329 | | |
12330 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
12331 | 0 | session = OPTS_GET(opts, session, false); |
12332 | |
|
12333 | 0 | if (retprobe && session) |
12334 | 0 | return libbpf_err_ptr(-EINVAL); |
12335 | | |
12336 | 0 | attach_type = session ? BPF_TRACE_KPROBE_SESSION : BPF_TRACE_KPROBE_MULTI; |
12337 | |
|
12338 | 0 | lopts.kprobe_multi.syms = syms; |
12339 | 0 | lopts.kprobe_multi.addrs = addrs; |
12340 | 0 | lopts.kprobe_multi.cookies = cookies; |
12341 | 0 | lopts.kprobe_multi.cnt = cnt; |
12342 | 0 | lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0; |
12343 | |
|
12344 | 0 | link = calloc(1, sizeof(*link)); |
12345 | 0 | if (!link) { |
12346 | 0 | err = -ENOMEM; |
12347 | 0 | goto error; |
12348 | 0 | } |
12349 | 0 | link->detach = &bpf_link__detach_fd; |
12350 | |
|
12351 | 0 | link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts); |
12352 | 0 | if (link_fd < 0) { |
12353 | 0 | err = -errno; |
12354 | | /* |
12355 | | * Normalize error code: when exact name bypasses kallsyms |
12356 | | * parsing, kernel returns ESRCH from ftrace_lookup_symbols(). |
12357 | | * Convert to ENOENT for API consistency with the pattern |
12358 | | * matching path which returns ENOENT from userspace. |
12359 | | */ |
12360 | 0 | if (err == -ESRCH) |
12361 | 0 | err = -ENOENT; |
12362 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
12363 | 0 | prog->name, errstr(err)); |
12364 | 0 | goto error; |
12365 | 0 | } |
12366 | 0 | link->fd = link_fd; |
12367 | 0 | free(res.addrs); |
12368 | 0 | return link; |
12369 | | |
12370 | 0 | error: |
12371 | 0 | free(link); |
12372 | 0 | free(res.addrs); |
12373 | 0 | return libbpf_err_ptr(err); |
12374 | 0 | } |
12375 | | |
12376 | | static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12377 | 0 | { |
12378 | 0 | DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); |
12379 | 0 | long offset = 0; |
12380 | 0 | const char *func_name; |
12381 | 0 | char *func; |
12382 | 0 | int n; |
12383 | |
|
12384 | 0 | *link = NULL; |
12385 | | |
12386 | | /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */ |
12387 | 0 | if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0) |
12388 | 0 | return 0; |
12389 | | |
12390 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/"); |
12391 | 0 | if (opts.retprobe) |
12392 | 0 | func_name = prog->sec_name + sizeof("kretprobe/") - 1; |
12393 | 0 | else |
12394 | 0 | func_name = prog->sec_name + sizeof("kprobe/") - 1; |
12395 | |
|
12396 | 0 | n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); |
12397 | 0 | if (n < 1) { |
12398 | 0 | pr_warn("kprobe name is invalid: %s\n", func_name); |
12399 | 0 | return -EINVAL; |
12400 | 0 | } |
12401 | | |
12402 | 0 | if (offset < 0) { |
12403 | 0 | free(func); |
12404 | 0 | pr_warn("kprobe offset must be a non-negative integer: %li\n", offset); |
12405 | 0 | return -EINVAL; |
12406 | 0 | } |
12407 | | |
12408 | 0 | if (opts.retprobe && offset != 0) { |
12409 | 0 | free(func); |
12410 | 0 | pr_warn("kretprobes do not support offset specification\n"); |
12411 | 0 | return -EINVAL; |
12412 | 0 | } |
12413 | | |
12414 | 0 | opts.offset = offset; |
12415 | 0 | *link = bpf_program__attach_kprobe_opts(prog, func, &opts); |
12416 | 0 | free(func); |
12417 | 0 | return libbpf_get_error(*link); |
12418 | 0 | } |
12419 | | |
12420 | | static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12421 | 0 | { |
12422 | 0 | LIBBPF_OPTS(bpf_ksyscall_opts, opts); |
12423 | 0 | const char *syscall_name; |
12424 | |
|
12425 | 0 | *link = NULL; |
12426 | | |
12427 | | /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */ |
12428 | 0 | if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0) |
12429 | 0 | return 0; |
12430 | | |
12431 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/"); |
12432 | 0 | if (opts.retprobe) |
12433 | 0 | syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1; |
12434 | 0 | else |
12435 | 0 | syscall_name = prog->sec_name + sizeof("ksyscall/") - 1; |
12436 | |
|
12437 | 0 | *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts); |
12438 | 0 | return *link ? 0 : -errno; |
12439 | 0 | } |
12440 | | |
12441 | | static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12442 | 0 | { |
12443 | 0 | LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); |
12444 | 0 | const char *spec; |
12445 | 0 | char *pattern; |
12446 | 0 | int n; |
12447 | |
|
12448 | 0 | *link = NULL; |
12449 | | |
12450 | | /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */ |
12451 | 0 | if (strcmp(prog->sec_name, "kprobe.multi") == 0 || |
12452 | 0 | strcmp(prog->sec_name, "kretprobe.multi") == 0) |
12453 | 0 | return 0; |
12454 | | |
12455 | 0 | opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/"); |
12456 | 0 | if (opts.retprobe) |
12457 | 0 | spec = prog->sec_name + sizeof("kretprobe.multi/") - 1; |
12458 | 0 | else |
12459 | 0 | spec = prog->sec_name + sizeof("kprobe.multi/") - 1; |
12460 | |
|
12461 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); |
12462 | 0 | if (n < 1) { |
12463 | 0 | pr_warn("kprobe multi pattern is invalid: %s\n", spec); |
12464 | 0 | return -EINVAL; |
12465 | 0 | } |
12466 | | |
12467 | 0 | *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts); |
12468 | 0 | free(pattern); |
12469 | 0 | return libbpf_get_error(*link); |
12470 | 0 | } |
12471 | | |
12472 | | static int attach_kprobe_session(const struct bpf_program *prog, long cookie, |
12473 | | struct bpf_link **link) |
12474 | 0 | { |
12475 | 0 | LIBBPF_OPTS(bpf_kprobe_multi_opts, opts, .session = true); |
12476 | 0 | const char *spec; |
12477 | 0 | char *pattern; |
12478 | 0 | int n; |
12479 | |
|
12480 | 0 | *link = NULL; |
12481 | | |
12482 | | /* no auto-attach for SEC("kprobe.session") */ |
12483 | 0 | if (strcmp(prog->sec_name, "kprobe.session") == 0) |
12484 | 0 | return 0; |
12485 | | |
12486 | 0 | spec = prog->sec_name + sizeof("kprobe.session/") - 1; |
12487 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern); |
12488 | 0 | if (n < 1) { |
12489 | 0 | pr_warn("kprobe session pattern is invalid: %s\n", spec); |
12490 | 0 | return -EINVAL; |
12491 | 0 | } |
12492 | | |
12493 | 0 | *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts); |
12494 | 0 | free(pattern); |
12495 | 0 | return *link ? 0 : -errno; |
12496 | 0 | } |
12497 | | |
12498 | | static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12499 | 0 | { |
12500 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL; |
12501 | 0 | LIBBPF_OPTS(bpf_uprobe_multi_opts, opts); |
12502 | 0 | int n, ret = -EINVAL; |
12503 | |
|
12504 | 0 | *link = NULL; |
12505 | |
|
12506 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
12507 | 0 | &probe_type, &binary_path, &func_name); |
12508 | 0 | switch (n) { |
12509 | 0 | case 1: |
12510 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
12511 | 0 | ret = 0; |
12512 | 0 | break; |
12513 | 0 | case 3: |
12514 | 0 | opts.session = str_has_pfx(probe_type, "uprobe.session"); |
12515 | 0 | opts.retprobe = str_has_pfx(probe_type, "uretprobe.multi"); |
12516 | |
|
12517 | 0 | *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); |
12518 | 0 | ret = libbpf_get_error(*link); |
12519 | 0 | break; |
12520 | 0 | default: |
12521 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
12522 | 0 | prog->sec_name); |
12523 | 0 | break; |
12524 | 0 | } |
12525 | 0 | free(probe_type); |
12526 | 0 | free(binary_path); |
12527 | 0 | free(func_name); |
12528 | 0 | return ret; |
12529 | 0 | } |
12530 | | |
12531 | 0 | #define MAX_BPF_FUNC_ARGS 12 |
12532 | | |
12533 | | static bool btf_type_is_modifier(const struct btf_type *t) |
12534 | 0 | { |
12535 | 0 | switch (BTF_INFO_KIND(t->info)) { |
12536 | 0 | case BTF_KIND_TYPEDEF: |
12537 | 0 | case BTF_KIND_VOLATILE: |
12538 | 0 | case BTF_KIND_CONST: |
12539 | 0 | case BTF_KIND_RESTRICT: |
12540 | 0 | case BTF_KIND_TYPE_TAG: |
12541 | 0 | return true; |
12542 | 0 | default: |
12543 | 0 | return false; |
12544 | 0 | } |
12545 | 0 | } |
12546 | | |
12547 | 0 | #define MAX_RESOLVE_DEPTH 32 |
12548 | | |
12549 | | static int btf_get_type_size(const struct btf *btf, __u32 type_id, |
12550 | | const struct btf_type **ret_type) |
12551 | 0 | { |
12552 | 0 | const struct btf_type *t; |
12553 | 0 | int i; |
12554 | |
|
12555 | 0 | *ret_type = btf__type_by_id(btf, 0); |
12556 | 0 | if (!type_id) |
12557 | 0 | return 0; |
12558 | 0 | t = btf__type_by_id(btf, type_id); |
12559 | 0 | for (i = 0; i < MAX_RESOLVE_DEPTH && t && btf_type_is_modifier(t); i++) |
12560 | 0 | t = btf__type_by_id(btf, t->type); |
12561 | 0 | if (!t || i == MAX_RESOLVE_DEPTH) |
12562 | 0 | return -EINVAL; |
12563 | 0 | *ret_type = t; |
12564 | 0 | if (btf_is_ptr(t)) |
12565 | 0 | return btf__pointer_size(btf); |
12566 | 0 | if (btf_is_int(t) || btf_is_any_enum(t) || btf_is_struct(t) || btf_is_union(t)) |
12567 | 0 | return t->size; |
12568 | 0 | return -EINVAL; |
12569 | 0 | } |
12570 | | |
12571 | | bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t) |
12572 | 0 | { |
12573 | 0 | const struct btf_param *args; |
12574 | 0 | const struct btf_type *proto; |
12575 | 0 | __u32 i, nargs; |
12576 | 0 | int ret; |
12577 | |
|
12578 | 0 | if (!btf_is_func(t)) |
12579 | 0 | return false; |
12580 | 0 | proto = btf__type_by_id(btf, t->type); |
12581 | 0 | if (!proto || !btf_is_func_proto(proto)) |
12582 | 0 | return false; |
12583 | | |
12584 | 0 | args = (const struct btf_param *)(proto + 1); |
12585 | 0 | nargs = btf_vlen(proto); |
12586 | 0 | if (nargs > MAX_BPF_FUNC_ARGS) |
12587 | 0 | return false; |
12588 | | |
12589 | | /* No support for struct return type. */ |
12590 | 0 | ret = btf_get_type_size(btf, proto->type, &t); |
12591 | 0 | if (ret < 0 || btf_is_struct(t) || btf_is_union(t)) |
12592 | 0 | return false; |
12593 | | |
12594 | 0 | for (i = 0; i < nargs; i++) { |
12595 | | /* No support for variable args. */ |
12596 | 0 | if (i == nargs - 1 && args[i].type == 0) |
12597 | 0 | return false; |
12598 | 0 | ret = btf_get_type_size(btf, args[i].type, &t); |
12599 | | /* No support of struct argument size greater than 16 bytes. */ |
12600 | 0 | if (ret < 0 || ret > 16) |
12601 | 0 | return false; |
12602 | | /* No support for void argument. */ |
12603 | 0 | if (ret == 0) |
12604 | 0 | return false; |
12605 | 0 | } |
12606 | | |
12607 | 0 | return true; |
12608 | 0 | } |
12609 | | |
12610 | | static int |
12611 | | collect_btf_func_ids_by_glob(const struct btf *btf, const char *pattern, __u32 **ids) |
12612 | 0 | { |
12613 | 0 | __u32 type_id, nr_types = btf__type_cnt(btf); |
12614 | 0 | size_t cap = 0, cnt = 0; |
12615 | |
|
12616 | 0 | if (!pattern) |
12617 | 0 | return -EINVAL; |
12618 | | |
12619 | 0 | for (type_id = 1; type_id < nr_types; type_id++) { |
12620 | 0 | const struct btf_type *t = btf__type_by_id(btf, type_id); |
12621 | 0 | const char *name; |
12622 | 0 | int err; |
12623 | |
|
12624 | 0 | if (btf_kind(t) != BTF_KIND_FUNC) |
12625 | 0 | continue; |
12626 | 0 | name = btf__name_by_offset(btf, t->name_off); |
12627 | 0 | if (!name) |
12628 | 0 | continue; |
12629 | | |
12630 | 0 | if (!glob_match(name, pattern)) |
12631 | 0 | continue; |
12632 | 0 | if (!btf_type_is_traceable_func(btf, t)) |
12633 | 0 | continue; |
12634 | | |
12635 | 0 | err = libbpf_ensure_mem((void **) ids, &cap, sizeof(**ids), cnt + 1); |
12636 | 0 | if (err) { |
12637 | 0 | free(*ids); |
12638 | 0 | return -ENOMEM; |
12639 | 0 | } |
12640 | 0 | (*ids)[cnt++] = type_id; |
12641 | 0 | } |
12642 | | |
12643 | 0 | return cnt; |
12644 | 0 | } |
12645 | | |
12646 | | static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *pattern, __u32 **ids) |
12647 | 0 | { |
12648 | 0 | struct bpf_object *obj = prog->obj; |
12649 | 0 | const struct module_btf *mod; |
12650 | 0 | struct btf *btf = NULL; |
12651 | 0 | const char *sep; |
12652 | 0 | int err; |
12653 | |
|
12654 | 0 | err = bpf_object__load_vmlinux_btf(obj, true); |
12655 | 0 | if (err) |
12656 | 0 | return err; |
12657 | | |
12658 | | /* In case we have module specified, we will find its btf and use that. */ |
12659 | 0 | sep = strchr(pattern, ':'); |
12660 | 0 | if (sep) { |
12661 | 0 | mod = find_attach_module(obj, pattern); |
12662 | 0 | if (!mod) { |
12663 | 0 | err = -EINVAL; |
12664 | 0 | goto cleanup; |
12665 | 0 | } |
12666 | 0 | btf = mod->btf; |
12667 | 0 | pattern = sep + 1; |
12668 | 0 | } else { |
12669 | | /* Program is loaded for kernel module. */ |
12670 | 0 | if (prog->attach_btf_obj_fd) { |
12671 | 0 | err = -EINVAL; |
12672 | 0 | goto cleanup; |
12673 | 0 | } |
12674 | 0 | btf = obj->btf_vmlinux; |
12675 | 0 | } |
12676 | | |
12677 | 0 | err = collect_btf_func_ids_by_glob(btf, pattern, ids); |
12678 | |
|
12679 | 0 | cleanup: |
12680 | 0 | bpf_object_cleanup_btf(obj); |
12681 | 0 | return err; |
12682 | 0 | } |
12683 | | |
12684 | | struct bpf_link * |
12685 | | bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pattern, |
12686 | | const struct bpf_tracing_multi_opts *opts) |
12687 | 0 | { |
12688 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
12689 | 0 | int prog_fd, link_fd, err, cnt; |
12690 | 0 | __u32 *free_ids = NULL; |
12691 | 0 | struct bpf_link *link; |
12692 | 0 | const __u64 *cookies; |
12693 | 0 | const __u32 *ids; |
12694 | |
|
12695 | 0 | if (!OPTS_VALID(opts, bpf_tracing_multi_opts)) |
12696 | 0 | return libbpf_err_ptr(-EINVAL); |
12697 | | |
12698 | 0 | prog_fd = bpf_program__fd(prog); |
12699 | 0 | if (prog_fd < 0) { |
12700 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
12701 | 0 | prog->name); |
12702 | 0 | return libbpf_err_ptr(-EINVAL); |
12703 | 0 | } |
12704 | | |
12705 | 0 | cnt = OPTS_GET(opts, cnt, 0); |
12706 | 0 | ids = OPTS_GET(opts, ids, NULL); |
12707 | 0 | cookies = OPTS_GET(opts, cookies, NULL); |
12708 | |
|
12709 | 0 | if (!!ids != !!cnt) |
12710 | 0 | return libbpf_err_ptr(-EINVAL); |
12711 | 0 | if (pattern && (ids || cookies)) |
12712 | 0 | return libbpf_err_ptr(-EINVAL); |
12713 | 0 | if (!pattern && !ids) |
12714 | 0 | return libbpf_err_ptr(-EINVAL); |
12715 | | |
12716 | 0 | if (pattern) { |
12717 | 0 | cnt = collect_func_ids_by_glob(prog, pattern, &free_ids); |
12718 | 0 | if (cnt < 0) |
12719 | 0 | return libbpf_err_ptr(cnt); |
12720 | 0 | if (cnt == 0) |
12721 | 0 | return libbpf_err_ptr(-EINVAL); |
12722 | 0 | ids = (const __u32 *) free_ids; |
12723 | 0 | } |
12724 | | |
12725 | 0 | lopts.tracing_multi.ids = ids; |
12726 | 0 | lopts.tracing_multi.cookies = cookies; |
12727 | 0 | lopts.tracing_multi.cnt = cnt; |
12728 | |
|
12729 | 0 | link = calloc(1, sizeof(*link)); |
12730 | 0 | if (!link) { |
12731 | 0 | err = -ENOMEM; |
12732 | 0 | goto error; |
12733 | 0 | } |
12734 | 0 | link->detach = &bpf_link__detach_fd; |
12735 | |
|
12736 | 0 | link_fd = bpf_link_create(prog_fd, 0, prog->expected_attach_type, &lopts); |
12737 | 0 | if (link_fd < 0) { |
12738 | 0 | err = -errno; |
12739 | 0 | pr_warn("prog '%s': failed to attach: %s\n", prog->name, errstr(err)); |
12740 | 0 | goto error; |
12741 | 0 | } |
12742 | 0 | link->fd = link_fd; |
12743 | 0 | free(free_ids); |
12744 | 0 | return link; |
12745 | | |
12746 | 0 | error: |
12747 | 0 | free(link); |
12748 | 0 | free(free_ids); |
12749 | 0 | return libbpf_err_ptr(err); |
12750 | 0 | } |
12751 | | |
12752 | | static int attach_tracing_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
12753 | 0 | { |
12754 | 0 | static const char *const prefixes[] = { |
12755 | 0 | "fentry.multi", |
12756 | 0 | "fexit.multi", |
12757 | 0 | "fsession.multi", |
12758 | 0 | "fentry.multi.s", |
12759 | 0 | "fexit.multi.s", |
12760 | 0 | "fsession.multi.s", |
12761 | 0 | }; |
12762 | 0 | const char *spec = NULL; |
12763 | 0 | char *pattern; |
12764 | 0 | size_t i; |
12765 | 0 | int n; |
12766 | |
|
12767 | 0 | *link = NULL; |
12768 | |
|
12769 | 0 | for (i = 0; i < ARRAY_SIZE(prefixes); i++) { |
12770 | 0 | size_t pfx_len; |
12771 | |
|
12772 | 0 | if (!str_has_pfx(prog->sec_name, prefixes[i])) |
12773 | 0 | continue; |
12774 | | |
12775 | 0 | pfx_len = strlen(prefixes[i]); |
12776 | | /* no auto-attach case of, e.g., SEC("fentry.multi") */ |
12777 | 0 | if (prog->sec_name[pfx_len] == '\0') |
12778 | 0 | return 0; |
12779 | | |
12780 | 0 | if (prog->sec_name[pfx_len] != '/') |
12781 | 0 | continue; |
12782 | | |
12783 | 0 | spec = prog->sec_name + pfx_len + 1; |
12784 | 0 | break; |
12785 | 0 | } |
12786 | | |
12787 | 0 | if (!spec) { |
12788 | 0 | pr_warn("prog '%s': invalid section name '%s'\n", |
12789 | 0 | prog->name, prog->sec_name); |
12790 | 0 | return -EINVAL; |
12791 | 0 | } |
12792 | | |
12793 | 0 | n = sscanf(spec, "%m[a-zA-Z0-9_.*?:]", &pattern); |
12794 | 0 | if (n < 1) { |
12795 | 0 | pr_warn("tracing multi pattern is invalid: %s\n", spec); |
12796 | 0 | return -EINVAL; |
12797 | 0 | } |
12798 | | |
12799 | 0 | *link = bpf_program__attach_tracing_multi(prog, pattern, NULL); |
12800 | 0 | free(pattern); |
12801 | 0 | return libbpf_get_error(*link); |
12802 | 0 | } |
12803 | | |
12804 | | static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe, |
12805 | | const char *binary_path, size_t offset) |
12806 | 0 | { |
12807 | 0 | return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx", |
12808 | 0 | retprobe ? 'r' : 'p', |
12809 | 0 | retprobe ? "uretprobes" : "uprobes", |
12810 | 0 | probe_name, binary_path, offset); |
12811 | 0 | } |
12812 | | |
12813 | | static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe) |
12814 | 0 | { |
12815 | 0 | return append_to_file(tracefs_uprobe_events(), "-:%s/%s", |
12816 | 0 | retprobe ? "uretprobes" : "uprobes", probe_name); |
12817 | 0 | } |
12818 | | |
12819 | | static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe) |
12820 | 0 | { |
12821 | 0 | char file[512]; |
12822 | |
|
12823 | 0 | snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
12824 | 0 | tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name); |
12825 | |
|
12826 | 0 | return parse_uint_from_file(file, "%d\n"); |
12827 | 0 | } |
12828 | | |
12829 | | static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, |
12830 | | const char *binary_path, size_t offset, int pid) |
12831 | 0 | { |
12832 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
12833 | 0 | struct perf_event_attr attr; |
12834 | 0 | int type, pfd, err; |
12835 | |
|
12836 | 0 | err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset); |
12837 | 0 | if (err < 0) { |
12838 | 0 | pr_warn("failed to add legacy uprobe event for %s:0x%zx: %s\n", |
12839 | 0 | binary_path, (size_t)offset, errstr(err)); |
12840 | 0 | return err; |
12841 | 0 | } |
12842 | 0 | type = determine_uprobe_perf_type_legacy(probe_name, retprobe); |
12843 | 0 | if (type < 0) { |
12844 | 0 | err = type; |
12845 | 0 | pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %s\n", |
12846 | 0 | binary_path, offset, errstr(err)); |
12847 | 0 | goto err_clean_legacy; |
12848 | 0 | } |
12849 | | |
12850 | 0 | memset(&attr, 0, attr_sz); |
12851 | 0 | attr.size = attr_sz; |
12852 | 0 | attr.config = type; |
12853 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
12854 | |
|
12855 | 0 | pfd = syscall(__NR_perf_event_open, &attr, |
12856 | 0 | pid < 0 ? -1 : pid, /* pid */ |
12857 | 0 | pid == -1 ? 0 : -1, /* cpu */ |
12858 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
12859 | 0 | if (pfd < 0) { |
12860 | 0 | err = -errno; |
12861 | 0 | pr_warn("legacy uprobe perf_event_open() failed: %s\n", errstr(err)); |
12862 | 0 | goto err_clean_legacy; |
12863 | 0 | } |
12864 | 0 | return pfd; |
12865 | | |
12866 | 0 | err_clean_legacy: |
12867 | | /* Clear the newly added legacy uprobe_event */ |
12868 | 0 | remove_uprobe_event_legacy(probe_name, retprobe); |
12869 | 0 | return err; |
12870 | 0 | } |
12871 | | |
12872 | | /* Find offset of function name in archive specified by path. Currently |
12873 | | * supported are .zip files that do not compress their contents, as used on |
12874 | | * Android in the form of APKs, for example. "file_name" is the name of the ELF |
12875 | | * file inside the archive. "func_name" matches symbol name or name@@LIB for |
12876 | | * library functions. |
12877 | | * |
12878 | | * An overview of the APK format specifically provided here: |
12879 | | * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents |
12880 | | */ |
12881 | | static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name, |
12882 | | const char *func_name) |
12883 | 0 | { |
12884 | 0 | struct zip_archive *archive; |
12885 | 0 | struct zip_entry entry; |
12886 | 0 | long ret; |
12887 | 0 | Elf *elf; |
12888 | |
|
12889 | 0 | archive = zip_archive_open(archive_path); |
12890 | 0 | if (IS_ERR(archive)) { |
12891 | 0 | ret = PTR_ERR(archive); |
12892 | 0 | pr_warn("zip: failed to open %s: %ld\n", archive_path, ret); |
12893 | 0 | return ret; |
12894 | 0 | } |
12895 | | |
12896 | 0 | ret = zip_archive_find_entry(archive, file_name, &entry); |
12897 | 0 | if (ret) { |
12898 | 0 | pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name, |
12899 | 0 | archive_path, ret); |
12900 | 0 | goto out; |
12901 | 0 | } |
12902 | 0 | pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path, |
12903 | 0 | (unsigned long)entry.data_offset); |
12904 | |
|
12905 | 0 | if (entry.compression) { |
12906 | 0 | pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name, |
12907 | 0 | archive_path); |
12908 | 0 | ret = -LIBBPF_ERRNO__FORMAT; |
12909 | 0 | goto out; |
12910 | 0 | } |
12911 | | |
12912 | 0 | elf = elf_memory((void *)entry.data, entry.data_length); |
12913 | 0 | if (!elf) { |
12914 | 0 | pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path, |
12915 | 0 | elf_errmsg(-1)); |
12916 | 0 | ret = -LIBBPF_ERRNO__LIBELF; |
12917 | 0 | goto out; |
12918 | 0 | } |
12919 | | |
12920 | 0 | ret = elf_find_func_offset(elf, file_name, func_name); |
12921 | 0 | if (ret > 0) { |
12922 | 0 | pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n", |
12923 | 0 | func_name, file_name, archive_path, entry.data_offset, (unsigned long)ret, |
12924 | 0 | (unsigned long)(ret + entry.data_offset)); |
12925 | 0 | ret += entry.data_offset; |
12926 | 0 | } |
12927 | 0 | elf_end(elf); |
12928 | |
|
12929 | 0 | out: |
12930 | 0 | zip_archive_close(archive); |
12931 | 0 | return ret; |
12932 | 0 | } |
12933 | | |
12934 | | static const char *arch_specific_lib_paths(void) |
12935 | 0 | { |
12936 | | /* |
12937 | | * Based on https://packages.debian.org/sid/libc6. |
12938 | | * |
12939 | | * Assume that the traced program is built for the same architecture |
12940 | | * as libbpf, which should cover the vast majority of cases. |
12941 | | */ |
12942 | 0 | #if defined(__x86_64__) |
12943 | 0 | return "/lib/x86_64-linux-gnu"; |
12944 | | #elif defined(__i386__) |
12945 | | return "/lib/i386-linux-gnu"; |
12946 | | #elif defined(__s390x__) |
12947 | | return "/lib/s390x-linux-gnu"; |
12948 | | #elif defined(__arm__) && defined(__SOFTFP__) |
12949 | | return "/lib/arm-linux-gnueabi"; |
12950 | | #elif defined(__arm__) && !defined(__SOFTFP__) |
12951 | | return "/lib/arm-linux-gnueabihf"; |
12952 | | #elif defined(__aarch64__) |
12953 | | return "/lib/aarch64-linux-gnu"; |
12954 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64 |
12955 | | return "/lib/mips64el-linux-gnuabi64"; |
12956 | | #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32 |
12957 | | return "/lib/mipsel-linux-gnu"; |
12958 | | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
12959 | | return "/lib/powerpc64le-linux-gnu"; |
12960 | | #elif defined(__sparc__) && defined(__arch64__) |
12961 | | return "/lib/sparc64-linux-gnu"; |
12962 | | #elif defined(__riscv) && __riscv_xlen == 64 |
12963 | | return "/lib/riscv64-linux-gnu"; |
12964 | | #else |
12965 | | return NULL; |
12966 | | #endif |
12967 | 0 | } |
12968 | | |
12969 | | /* Get full path to program/shared library. */ |
12970 | | static int resolve_full_path(const char *file, char *result, size_t result_sz) |
12971 | 0 | { |
12972 | 0 | const char *search_paths[3] = {}; |
12973 | 0 | int i, perm; |
12974 | |
|
12975 | 0 | if (str_has_sfx(file, ".so") || strstr(file, ".so.")) { |
12976 | 0 | search_paths[0] = getenv("LD_LIBRARY_PATH"); |
12977 | 0 | search_paths[1] = "/usr/lib64:/usr/lib"; |
12978 | 0 | search_paths[2] = arch_specific_lib_paths(); |
12979 | 0 | perm = R_OK; |
12980 | 0 | } else { |
12981 | 0 | search_paths[0] = getenv("PATH"); |
12982 | 0 | search_paths[1] = "/usr/bin:/usr/sbin"; |
12983 | 0 | perm = R_OK | X_OK; |
12984 | 0 | } |
12985 | |
|
12986 | 0 | for (i = 0; i < ARRAY_SIZE(search_paths); i++) { |
12987 | 0 | const char *s; |
12988 | |
|
12989 | 0 | if (!search_paths[i]) |
12990 | 0 | continue; |
12991 | 0 | for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) { |
12992 | 0 | const char *next_path; |
12993 | 0 | int seg_len; |
12994 | |
|
12995 | 0 | if (s[0] == ':') |
12996 | 0 | s++; |
12997 | 0 | next_path = strchr(s, ':'); |
12998 | 0 | seg_len = next_path ? next_path - s : strlen(s); |
12999 | 0 | if (!seg_len) |
13000 | 0 | continue; |
13001 | 0 | snprintf(result, result_sz, "%.*s/%s", seg_len, s, file); |
13002 | | /* ensure it has required permissions */ |
13003 | 0 | if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0) |
13004 | 0 | continue; |
13005 | 0 | pr_debug("resolved '%s' to '%s'\n", file, result); |
13006 | 0 | return 0; |
13007 | 0 | } |
13008 | 0 | } |
13009 | 0 | return -ENOENT; |
13010 | 0 | } |
13011 | | |
13012 | | struct bpf_link * |
13013 | | bpf_program__attach_uprobe_multi(const struct bpf_program *prog, |
13014 | | pid_t pid, |
13015 | | const char *path, |
13016 | | const char *func_pattern, |
13017 | | const struct bpf_uprobe_multi_opts *opts) |
13018 | 0 | { |
13019 | 0 | const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL; |
13020 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
13021 | 0 | unsigned long *resolved_offsets = NULL; |
13022 | 0 | enum bpf_attach_type attach_type; |
13023 | 0 | int err = 0, link_fd, prog_fd; |
13024 | 0 | struct bpf_link *link = NULL; |
13025 | 0 | char full_path[PATH_MAX]; |
13026 | 0 | bool retprobe, session; |
13027 | 0 | const __u64 *cookies; |
13028 | 0 | const char **syms; |
13029 | 0 | size_t cnt; |
13030 | |
|
13031 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_multi_opts)) |
13032 | 0 | return libbpf_err_ptr(-EINVAL); |
13033 | | |
13034 | 0 | prog_fd = bpf_program__fd(prog); |
13035 | 0 | if (prog_fd < 0) { |
13036 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
13037 | 0 | prog->name); |
13038 | 0 | return libbpf_err_ptr(-EINVAL); |
13039 | 0 | } |
13040 | | |
13041 | 0 | syms = OPTS_GET(opts, syms, NULL); |
13042 | 0 | offsets = OPTS_GET(opts, offsets, NULL); |
13043 | 0 | ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL); |
13044 | 0 | cookies = OPTS_GET(opts, cookies, NULL); |
13045 | 0 | cnt = OPTS_GET(opts, cnt, 0); |
13046 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
13047 | 0 | session = OPTS_GET(opts, session, false); |
13048 | | |
13049 | | /* |
13050 | | * User can specify 2 mutually exclusive set of inputs: |
13051 | | * |
13052 | | * 1) use only path/func_pattern/pid arguments |
13053 | | * |
13054 | | * 2) use path/pid with allowed combinations of: |
13055 | | * syms/offsets/ref_ctr_offsets/cookies/cnt |
13056 | | * |
13057 | | * - syms and offsets are mutually exclusive |
13058 | | * - ref_ctr_offsets and cookies are optional |
13059 | | * |
13060 | | * Any other usage results in error. |
13061 | | */ |
13062 | |
|
13063 | 0 | if (!path) |
13064 | 0 | return libbpf_err_ptr(-EINVAL); |
13065 | 0 | if (!func_pattern && cnt == 0) |
13066 | 0 | return libbpf_err_ptr(-EINVAL); |
13067 | | |
13068 | 0 | if (func_pattern) { |
13069 | 0 | if (syms || offsets || ref_ctr_offsets || cookies || cnt) |
13070 | 0 | return libbpf_err_ptr(-EINVAL); |
13071 | 0 | } else { |
13072 | 0 | if (!!syms == !!offsets) |
13073 | 0 | return libbpf_err_ptr(-EINVAL); |
13074 | 0 | } |
13075 | | |
13076 | 0 | if (retprobe && session) |
13077 | 0 | return libbpf_err_ptr(-EINVAL); |
13078 | | |
13079 | 0 | if (func_pattern) { |
13080 | 0 | if (!strchr(path, '/')) { |
13081 | 0 | err = resolve_full_path(path, full_path, sizeof(full_path)); |
13082 | 0 | if (err) { |
13083 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %s\n", |
13084 | 0 | prog->name, path, errstr(err)); |
13085 | 0 | return libbpf_err_ptr(err); |
13086 | 0 | } |
13087 | 0 | path = full_path; |
13088 | 0 | } |
13089 | | |
13090 | 0 | err = elf_resolve_pattern_offsets(path, func_pattern, |
13091 | 0 | &resolved_offsets, &cnt); |
13092 | 0 | if (err < 0) |
13093 | 0 | return libbpf_err_ptr(err); |
13094 | 0 | offsets = resolved_offsets; |
13095 | 0 | } else if (syms) { |
13096 | 0 | err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC); |
13097 | 0 | if (err < 0) |
13098 | 0 | return libbpf_err_ptr(err); |
13099 | 0 | offsets = resolved_offsets; |
13100 | 0 | } |
13101 | | |
13102 | 0 | attach_type = session ? BPF_TRACE_UPROBE_SESSION : BPF_TRACE_UPROBE_MULTI; |
13103 | |
|
13104 | 0 | lopts.uprobe_multi.path = path; |
13105 | 0 | lopts.uprobe_multi.offsets = offsets; |
13106 | 0 | lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets; |
13107 | 0 | lopts.uprobe_multi.cookies = cookies; |
13108 | 0 | lopts.uprobe_multi.cnt = cnt; |
13109 | 0 | lopts.uprobe_multi.flags = retprobe ? BPF_F_UPROBE_MULTI_RETURN : 0; |
13110 | |
|
13111 | 0 | if (pid == 0) |
13112 | 0 | pid = getpid(); |
13113 | 0 | if (pid > 0) |
13114 | 0 | lopts.uprobe_multi.pid = pid; |
13115 | |
|
13116 | 0 | link = calloc(1, sizeof(*link)); |
13117 | 0 | if (!link) { |
13118 | 0 | err = -ENOMEM; |
13119 | 0 | goto error; |
13120 | 0 | } |
13121 | 0 | link->detach = &bpf_link__detach_fd; |
13122 | |
|
13123 | 0 | link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts); |
13124 | 0 | if (link_fd < 0) { |
13125 | 0 | err = -errno; |
13126 | 0 | pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", |
13127 | 0 | prog->name, errstr(err)); |
13128 | 0 | goto error; |
13129 | 0 | } |
13130 | 0 | link->fd = link_fd; |
13131 | 0 | free(resolved_offsets); |
13132 | 0 | return link; |
13133 | | |
13134 | 0 | error: |
13135 | 0 | free(resolved_offsets); |
13136 | 0 | free(link); |
13137 | 0 | return libbpf_err_ptr(err); |
13138 | 0 | } |
13139 | | |
13140 | | LIBBPF_API struct bpf_link * |
13141 | | bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, |
13142 | | const char *binary_path, size_t func_offset, |
13143 | | const struct bpf_uprobe_opts *opts) |
13144 | 0 | { |
13145 | 0 | const char *archive_path = NULL, *archive_sep = NULL; |
13146 | 0 | char *legacy_probe = NULL; |
13147 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
13148 | 0 | enum probe_attach_mode attach_mode; |
13149 | 0 | char full_path[PATH_MAX]; |
13150 | 0 | struct bpf_link *link; |
13151 | 0 | size_t ref_ctr_off; |
13152 | 0 | int pfd, err; |
13153 | 0 | bool retprobe, legacy; |
13154 | 0 | const char *func_name; |
13155 | |
|
13156 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
13157 | 0 | return libbpf_err_ptr(-EINVAL); |
13158 | | |
13159 | 0 | attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT); |
13160 | 0 | retprobe = OPTS_GET(opts, retprobe, false); |
13161 | 0 | ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0); |
13162 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
13163 | |
|
13164 | 0 | if (!binary_path) |
13165 | 0 | return libbpf_err_ptr(-EINVAL); |
13166 | | |
13167 | | /* Check if "binary_path" refers to an archive. */ |
13168 | 0 | archive_sep = strstr(binary_path, "!/"); |
13169 | 0 | if (archive_sep) { |
13170 | 0 | full_path[0] = '\0'; |
13171 | 0 | libbpf_strlcpy(full_path, binary_path, |
13172 | 0 | min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1))); |
13173 | 0 | archive_path = full_path; |
13174 | 0 | binary_path = archive_sep + 2; |
13175 | 0 | } else if (!strchr(binary_path, '/')) { |
13176 | 0 | err = resolve_full_path(binary_path, full_path, sizeof(full_path)); |
13177 | 0 | if (err) { |
13178 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %s\n", |
13179 | 0 | prog->name, binary_path, errstr(err)); |
13180 | 0 | return libbpf_err_ptr(err); |
13181 | 0 | } |
13182 | 0 | binary_path = full_path; |
13183 | 0 | } |
13184 | 0 | func_name = OPTS_GET(opts, func_name, NULL); |
13185 | 0 | if (func_name) { |
13186 | 0 | long sym_off; |
13187 | |
|
13188 | 0 | if (archive_path) { |
13189 | 0 | sym_off = elf_find_func_offset_from_archive(archive_path, binary_path, |
13190 | 0 | func_name); |
13191 | 0 | binary_path = archive_path; |
13192 | 0 | } else { |
13193 | 0 | sym_off = elf_find_func_offset_from_file(binary_path, func_name); |
13194 | 0 | } |
13195 | 0 | if (sym_off < 0) |
13196 | 0 | return libbpf_err_ptr(sym_off); |
13197 | 0 | func_offset += sym_off; |
13198 | 0 | } |
13199 | | |
13200 | 0 | legacy = determine_uprobe_perf_type() < 0; |
13201 | 0 | switch (attach_mode) { |
13202 | 0 | case PROBE_ATTACH_MODE_LEGACY: |
13203 | 0 | legacy = true; |
13204 | 0 | pe_opts.force_ioctl_attach = true; |
13205 | 0 | break; |
13206 | 0 | case PROBE_ATTACH_MODE_PERF: |
13207 | 0 | if (legacy) |
13208 | 0 | return libbpf_err_ptr(-ENOTSUP); |
13209 | 0 | pe_opts.force_ioctl_attach = true; |
13210 | 0 | break; |
13211 | 0 | case PROBE_ATTACH_MODE_LINK: |
13212 | 0 | if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK)) |
13213 | 0 | return libbpf_err_ptr(-ENOTSUP); |
13214 | 0 | break; |
13215 | 0 | case PROBE_ATTACH_MODE_DEFAULT: |
13216 | 0 | break; |
13217 | 0 | default: |
13218 | 0 | return libbpf_err_ptr(-EINVAL); |
13219 | 0 | } |
13220 | | |
13221 | 0 | if (!legacy) { |
13222 | 0 | pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path, |
13223 | 0 | func_offset, pid, ref_ctr_off); |
13224 | 0 | } else { |
13225 | 0 | char probe_name[MAX_EVENT_NAME_LEN]; |
13226 | |
|
13227 | 0 | if (ref_ctr_off) |
13228 | 0 | return libbpf_err_ptr(-EINVAL); |
13229 | | |
13230 | 0 | gen_probe_legacy_event_name(probe_name, sizeof(probe_name), |
13231 | 0 | strrchr(binary_path, '/') ? : binary_path, |
13232 | 0 | func_offset); |
13233 | |
|
13234 | 0 | legacy_probe = strdup(probe_name); |
13235 | 0 | if (!legacy_probe) |
13236 | 0 | return libbpf_err_ptr(-ENOMEM); |
13237 | | |
13238 | 0 | pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe, |
13239 | 0 | binary_path, func_offset, pid); |
13240 | 0 | } |
13241 | 0 | if (pfd < 0) { |
13242 | 0 | err = pfd; |
13243 | 0 | pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n", |
13244 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
13245 | 0 | binary_path, func_offset, |
13246 | 0 | errstr(err)); |
13247 | 0 | goto err_out; |
13248 | 0 | } |
13249 | | |
13250 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
13251 | 0 | err = libbpf_get_error(link); |
13252 | 0 | if (err) { |
13253 | 0 | close(pfd); |
13254 | 0 | pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n", |
13255 | 0 | prog->name, retprobe ? "uretprobe" : "uprobe", |
13256 | 0 | binary_path, func_offset, |
13257 | 0 | errstr(err)); |
13258 | 0 | goto err_clean_legacy; |
13259 | 0 | } |
13260 | 0 | if (legacy) { |
13261 | 0 | struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); |
13262 | |
|
13263 | 0 | perf_link->legacy_probe_name = legacy_probe; |
13264 | 0 | perf_link->legacy_is_kprobe = false; |
13265 | 0 | perf_link->legacy_is_retprobe = retprobe; |
13266 | 0 | } |
13267 | 0 | return link; |
13268 | | |
13269 | 0 | err_clean_legacy: |
13270 | 0 | if (legacy) |
13271 | 0 | remove_uprobe_event_legacy(legacy_probe, retprobe); |
13272 | 0 | err_out: |
13273 | 0 | free(legacy_probe); |
13274 | 0 | return libbpf_err_ptr(err); |
13275 | 0 | } |
13276 | | |
13277 | | /* Format of u[ret]probe section definition supporting auto-attach: |
13278 | | * u[ret]probe/binary:function[+offset] |
13279 | | * |
13280 | | * binary can be an absolute/relative path or a filename; the latter is resolved to a |
13281 | | * full binary path via bpf_program__attach_uprobe_opts. |
13282 | | * |
13283 | | * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be |
13284 | | * specified (and auto-attach is not possible) or the above format is specified for |
13285 | | * auto-attach. |
13286 | | */ |
13287 | | static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13288 | 0 | { |
13289 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); |
13290 | 0 | char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off; |
13291 | 0 | int n, c, ret = -EINVAL; |
13292 | 0 | long offset = 0; |
13293 | |
|
13294 | 0 | *link = NULL; |
13295 | |
|
13296 | 0 | n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]", |
13297 | 0 | &probe_type, &binary_path, &func_name); |
13298 | 0 | switch (n) { |
13299 | 0 | case 1: |
13300 | | /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ |
13301 | 0 | ret = 0; |
13302 | 0 | break; |
13303 | 0 | case 2: |
13304 | 0 | pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n", |
13305 | 0 | prog->name, prog->sec_name); |
13306 | 0 | break; |
13307 | 0 | case 3: |
13308 | | /* check if user specifies `+offset`, if yes, this should be |
13309 | | * the last part of the string, make sure sscanf read to EOL |
13310 | | */ |
13311 | 0 | func_off = strrchr(func_name, '+'); |
13312 | 0 | if (func_off) { |
13313 | 0 | n = sscanf(func_off, "+%li%n", &offset, &c); |
13314 | 0 | if (n == 1 && *(func_off + c) == '\0') |
13315 | 0 | func_off[0] = '\0'; |
13316 | 0 | else |
13317 | 0 | offset = 0; |
13318 | 0 | } |
13319 | 0 | opts.retprobe = strcmp(probe_type, "uretprobe") == 0 || |
13320 | 0 | strcmp(probe_type, "uretprobe.s") == 0; |
13321 | 0 | if (opts.retprobe && offset != 0) { |
13322 | 0 | pr_warn("prog '%s': uretprobes do not support offset specification\n", |
13323 | 0 | prog->name); |
13324 | 0 | break; |
13325 | 0 | } |
13326 | 0 | opts.func_name = func_name; |
13327 | 0 | *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); |
13328 | 0 | ret = libbpf_get_error(*link); |
13329 | 0 | break; |
13330 | 0 | default: |
13331 | 0 | pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, |
13332 | 0 | prog->sec_name); |
13333 | 0 | break; |
13334 | 0 | } |
13335 | 0 | free(probe_type); |
13336 | 0 | free(binary_path); |
13337 | 0 | free(func_name); |
13338 | |
|
13339 | 0 | return ret; |
13340 | 0 | } |
13341 | | |
13342 | | struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, |
13343 | | bool retprobe, pid_t pid, |
13344 | | const char *binary_path, |
13345 | | size_t func_offset) |
13346 | 0 | { |
13347 | 0 | DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe); |
13348 | |
|
13349 | 0 | return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts); |
13350 | 0 | } |
13351 | | |
13352 | | struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog, |
13353 | | pid_t pid, const char *binary_path, |
13354 | | const char *usdt_provider, const char *usdt_name, |
13355 | | const struct bpf_usdt_opts *opts) |
13356 | 0 | { |
13357 | 0 | char resolved_path[512]; |
13358 | 0 | struct bpf_object *obj = prog->obj; |
13359 | 0 | struct bpf_link *link; |
13360 | 0 | __u64 usdt_cookie; |
13361 | 0 | int err; |
13362 | |
|
13363 | 0 | if (!OPTS_VALID(opts, bpf_uprobe_opts)) |
13364 | 0 | return libbpf_err_ptr(-EINVAL); |
13365 | | |
13366 | 0 | if (bpf_program__fd(prog) < 0) { |
13367 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
13368 | 0 | prog->name); |
13369 | 0 | return libbpf_err_ptr(-EINVAL); |
13370 | 0 | } |
13371 | | |
13372 | 0 | if (!binary_path) |
13373 | 0 | return libbpf_err_ptr(-EINVAL); |
13374 | | |
13375 | 0 | if (!strchr(binary_path, '/')) { |
13376 | 0 | err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path)); |
13377 | 0 | if (err) { |
13378 | 0 | pr_warn("prog '%s': failed to resolve full path for '%s': %s\n", |
13379 | 0 | prog->name, binary_path, errstr(err)); |
13380 | 0 | return libbpf_err_ptr(err); |
13381 | 0 | } |
13382 | 0 | binary_path = resolved_path; |
13383 | 0 | } |
13384 | | |
13385 | | /* USDT manager is instantiated lazily on first USDT attach. It will |
13386 | | * be destroyed together with BPF object in bpf_object__close(). |
13387 | | */ |
13388 | 0 | if (IS_ERR(obj->usdt_man)) |
13389 | 0 | return libbpf_ptr(obj->usdt_man); |
13390 | 0 | if (!obj->usdt_man) { |
13391 | 0 | obj->usdt_man = usdt_manager_new(obj); |
13392 | 0 | if (IS_ERR(obj->usdt_man)) |
13393 | 0 | return libbpf_ptr(obj->usdt_man); |
13394 | 0 | } |
13395 | | |
13396 | 0 | usdt_cookie = OPTS_GET(opts, usdt_cookie, 0); |
13397 | 0 | link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path, |
13398 | 0 | usdt_provider, usdt_name, usdt_cookie); |
13399 | 0 | err = libbpf_get_error(link); |
13400 | 0 | if (err) |
13401 | 0 | return libbpf_err_ptr(err); |
13402 | 0 | return link; |
13403 | 0 | } |
13404 | | |
13405 | | static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13406 | 0 | { |
13407 | 0 | char *path = NULL, *provider = NULL, *name = NULL; |
13408 | 0 | const char *sec_name; |
13409 | 0 | int n, err; |
13410 | |
|
13411 | 0 | sec_name = bpf_program__section_name(prog); |
13412 | 0 | if (strcmp(sec_name, "usdt") == 0) { |
13413 | | /* no auto-attach for just SEC("usdt") */ |
13414 | 0 | *link = NULL; |
13415 | 0 | return 0; |
13416 | 0 | } |
13417 | | |
13418 | 0 | n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name); |
13419 | 0 | if (n != 3) { |
13420 | 0 | pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n", |
13421 | 0 | sec_name); |
13422 | 0 | err = -EINVAL; |
13423 | 0 | } else { |
13424 | 0 | *link = bpf_program__attach_usdt(prog, -1 /* any process */, path, |
13425 | 0 | provider, name, NULL); |
13426 | 0 | err = libbpf_get_error(*link); |
13427 | 0 | } |
13428 | 0 | free(path); |
13429 | 0 | free(provider); |
13430 | 0 | free(name); |
13431 | 0 | return err; |
13432 | 0 | } |
13433 | | |
13434 | | static int determine_tracepoint_id(const char *tp_category, |
13435 | | const char *tp_name) |
13436 | 0 | { |
13437 | 0 | char file[PATH_MAX]; |
13438 | 0 | int ret; |
13439 | |
|
13440 | 0 | ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id", |
13441 | 0 | tracefs_path(), tp_category, tp_name); |
13442 | 0 | if (ret < 0) |
13443 | 0 | return -errno; |
13444 | 0 | if (ret >= sizeof(file)) { |
13445 | 0 | pr_debug("tracepoint %s/%s path is too long\n", |
13446 | 0 | tp_category, tp_name); |
13447 | 0 | return -E2BIG; |
13448 | 0 | } |
13449 | 0 | return parse_uint_from_file(file, "%d\n"); |
13450 | 0 | } |
13451 | | |
13452 | | static int perf_event_open_tracepoint(const char *tp_category, |
13453 | | const char *tp_name) |
13454 | 0 | { |
13455 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
13456 | 0 | struct perf_event_attr attr; |
13457 | 0 | int tp_id, pfd, err; |
13458 | |
|
13459 | 0 | tp_id = determine_tracepoint_id(tp_category, tp_name); |
13460 | 0 | if (tp_id < 0) { |
13461 | 0 | pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n", |
13462 | 0 | tp_category, tp_name, |
13463 | 0 | errstr(tp_id)); |
13464 | 0 | return tp_id; |
13465 | 0 | } |
13466 | | |
13467 | 0 | memset(&attr, 0, attr_sz); |
13468 | 0 | attr.type = PERF_TYPE_TRACEPOINT; |
13469 | 0 | attr.size = attr_sz; |
13470 | 0 | attr.config = tp_id; |
13471 | |
|
13472 | 0 | pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, |
13473 | 0 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
13474 | 0 | if (pfd < 0) { |
13475 | 0 | err = -errno; |
13476 | 0 | pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n", |
13477 | 0 | tp_category, tp_name, |
13478 | 0 | errstr(err)); |
13479 | 0 | return err; |
13480 | 0 | } |
13481 | 0 | return pfd; |
13482 | 0 | } |
13483 | | |
13484 | | struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, |
13485 | | const char *tp_category, |
13486 | | const char *tp_name, |
13487 | | const struct bpf_tracepoint_opts *opts) |
13488 | 0 | { |
13489 | 0 | DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); |
13490 | 0 | struct bpf_link *link; |
13491 | 0 | int pfd, err; |
13492 | |
|
13493 | 0 | if (!OPTS_VALID(opts, bpf_tracepoint_opts)) |
13494 | 0 | return libbpf_err_ptr(-EINVAL); |
13495 | | |
13496 | 0 | pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); |
13497 | |
|
13498 | 0 | pfd = perf_event_open_tracepoint(tp_category, tp_name); |
13499 | 0 | if (pfd < 0) { |
13500 | 0 | pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n", |
13501 | 0 | prog->name, tp_category, tp_name, |
13502 | 0 | errstr(pfd)); |
13503 | 0 | return libbpf_err_ptr(pfd); |
13504 | 0 | } |
13505 | 0 | link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); |
13506 | 0 | err = libbpf_get_error(link); |
13507 | 0 | if (err) { |
13508 | 0 | close(pfd); |
13509 | 0 | pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n", |
13510 | 0 | prog->name, tp_category, tp_name, |
13511 | 0 | errstr(err)); |
13512 | 0 | return libbpf_err_ptr(err); |
13513 | 0 | } |
13514 | 0 | return link; |
13515 | 0 | } |
13516 | | |
13517 | | struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog, |
13518 | | const char *tp_category, |
13519 | | const char *tp_name) |
13520 | 0 | { |
13521 | 0 | return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL); |
13522 | 0 | } |
13523 | | |
13524 | | /* |
13525 | | * Match section name against a prefix array. Returns pointer past |
13526 | | * "prefix/" on match, empty string for bare sections (exact prefix |
13527 | | * match), or NULL if no prefix matches. |
13528 | | */ |
13529 | | static const char *sec_name_match_prefix(const char *sec_name, |
13530 | | const char *const *prefixes, |
13531 | | size_t n) |
13532 | 0 | { |
13533 | 0 | size_t i; |
13534 | |
|
13535 | 0 | for (i = 0; i < n; i++) { |
13536 | 0 | size_t pfx_len; |
13537 | |
|
13538 | 0 | if (!str_has_pfx(sec_name, prefixes[i])) |
13539 | 0 | continue; |
13540 | | |
13541 | 0 | pfx_len = strlen(prefixes[i]); |
13542 | 0 | if (sec_name[pfx_len] == '\0') |
13543 | 0 | return sec_name + pfx_len; |
13544 | | |
13545 | 0 | if (sec_name[pfx_len] != '/' || sec_name[pfx_len + 1] == '\0') |
13546 | 0 | continue; |
13547 | | |
13548 | 0 | return sec_name + pfx_len + 1; |
13549 | 0 | } |
13550 | 0 | return NULL; |
13551 | 0 | } |
13552 | | |
13553 | | static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13554 | 0 | { |
13555 | 0 | static const char *const prefixes[] = { |
13556 | 0 | "tp.s", |
13557 | 0 | "tp", |
13558 | 0 | "tracepoint.s", |
13559 | 0 | "tracepoint", |
13560 | 0 | }; |
13561 | 0 | char *sec_name, *tp_cat, *tp_name; |
13562 | 0 | const char *match; |
13563 | |
|
13564 | 0 | *link = NULL; |
13565 | |
|
13566 | 0 | match = sec_name_match_prefix(prog->sec_name, prefixes, ARRAY_SIZE(prefixes)); |
13567 | 0 | if (!match) { |
13568 | 0 | pr_warn("prog '%s': invalid section name '%s'\n", prog->name, prog->sec_name); |
13569 | 0 | return -EINVAL; |
13570 | 0 | } |
13571 | 0 | if (!match[0]) /* bare section name no autoattach */ |
13572 | 0 | return 0; |
13573 | | |
13574 | 0 | sec_name = strdup(prog->sec_name); |
13575 | 0 | if (!sec_name) |
13576 | 0 | return -ENOMEM; |
13577 | | |
13578 | 0 | tp_cat = sec_name + (match - prog->sec_name); |
13579 | 0 | tp_name = strchr(tp_cat, '/'); |
13580 | 0 | if (!tp_name) { |
13581 | 0 | free(sec_name); |
13582 | 0 | return -EINVAL; |
13583 | 0 | } |
13584 | 0 | *tp_name = '\0'; |
13585 | 0 | tp_name++; |
13586 | |
|
13587 | 0 | *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name); |
13588 | 0 | free(sec_name); |
13589 | 0 | return libbpf_get_error(*link); |
13590 | 0 | } |
13591 | | |
13592 | | struct bpf_link * |
13593 | | bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, |
13594 | | const char *tp_name, |
13595 | | struct bpf_raw_tracepoint_opts *opts) |
13596 | 0 | { |
13597 | 0 | LIBBPF_OPTS(bpf_raw_tp_opts, raw_opts); |
13598 | 0 | struct bpf_link *link; |
13599 | 0 | int prog_fd, pfd; |
13600 | |
|
13601 | 0 | if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts)) |
13602 | 0 | return libbpf_err_ptr(-EINVAL); |
13603 | | |
13604 | 0 | prog_fd = bpf_program__fd(prog); |
13605 | 0 | if (prog_fd < 0) { |
13606 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
13607 | 0 | return libbpf_err_ptr(-EINVAL); |
13608 | 0 | } |
13609 | | |
13610 | 0 | link = calloc(1, sizeof(*link)); |
13611 | 0 | if (!link) |
13612 | 0 | return libbpf_err_ptr(-ENOMEM); |
13613 | 0 | link->detach = &bpf_link__detach_fd; |
13614 | |
|
13615 | 0 | raw_opts.tp_name = tp_name; |
13616 | 0 | raw_opts.cookie = OPTS_GET(opts, cookie, 0); |
13617 | 0 | pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts); |
13618 | 0 | if (pfd < 0) { |
13619 | 0 | pfd = -errno; |
13620 | 0 | free(link); |
13621 | 0 | pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n", |
13622 | 0 | prog->name, tp_name, errstr(pfd)); |
13623 | 0 | return libbpf_err_ptr(pfd); |
13624 | 0 | } |
13625 | 0 | link->fd = pfd; |
13626 | 0 | return link; |
13627 | 0 | } |
13628 | | |
13629 | | struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, |
13630 | | const char *tp_name) |
13631 | 0 | { |
13632 | 0 | return bpf_program__attach_raw_tracepoint_opts(prog, tp_name, NULL); |
13633 | 0 | } |
13634 | | |
13635 | | static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13636 | 0 | { |
13637 | 0 | static const char *const prefixes[] = { |
13638 | 0 | "raw_tp", |
13639 | 0 | "raw_tracepoint", |
13640 | 0 | "raw_tp.w", |
13641 | 0 | "raw_tracepoint.w", |
13642 | 0 | "raw_tp.s", |
13643 | 0 | "raw_tracepoint.s", |
13644 | 0 | }; |
13645 | 0 | const char *match; |
13646 | |
|
13647 | 0 | *link = NULL; |
13648 | |
|
13649 | 0 | match = sec_name_match_prefix(prog->sec_name, prefixes, ARRAY_SIZE(prefixes)); |
13650 | 0 | if (!match) { |
13651 | 0 | pr_warn("prog '%s': invalid section name '%s'\n", prog->name, prog->sec_name); |
13652 | 0 | return -EINVAL; |
13653 | 0 | } |
13654 | 0 | if (!match[0]) |
13655 | 0 | return 0; |
13656 | | |
13657 | 0 | *link = bpf_program__attach_raw_tracepoint(prog, match); |
13658 | 0 | return libbpf_get_error(*link); |
13659 | 0 | } |
13660 | | |
13661 | | /* Common logic for all BPF program types that attach to a btf_id */ |
13662 | | static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog, |
13663 | | const struct bpf_trace_opts *opts) |
13664 | 0 | { |
13665 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_opts); |
13666 | 0 | struct bpf_link *link; |
13667 | 0 | int prog_fd, pfd; |
13668 | |
|
13669 | 0 | if (!OPTS_VALID(opts, bpf_trace_opts)) |
13670 | 0 | return libbpf_err_ptr(-EINVAL); |
13671 | | |
13672 | 0 | prog_fd = bpf_program__fd(prog); |
13673 | 0 | if (prog_fd < 0) { |
13674 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
13675 | 0 | return libbpf_err_ptr(-EINVAL); |
13676 | 0 | } |
13677 | | |
13678 | 0 | link = calloc(1, sizeof(*link)); |
13679 | 0 | if (!link) |
13680 | 0 | return libbpf_err_ptr(-ENOMEM); |
13681 | 0 | link->detach = &bpf_link__detach_fd; |
13682 | | |
13683 | | /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */ |
13684 | 0 | link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0); |
13685 | 0 | pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts); |
13686 | 0 | if (pfd < 0) { |
13687 | 0 | pfd = -errno; |
13688 | 0 | free(link); |
13689 | 0 | pr_warn("prog '%s': failed to attach: %s\n", |
13690 | 0 | prog->name, errstr(pfd)); |
13691 | 0 | return libbpf_err_ptr(pfd); |
13692 | 0 | } |
13693 | 0 | link->fd = pfd; |
13694 | 0 | return link; |
13695 | 0 | } |
13696 | | |
13697 | | struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog) |
13698 | 0 | { |
13699 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
13700 | 0 | } |
13701 | | |
13702 | | struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog, |
13703 | | const struct bpf_trace_opts *opts) |
13704 | 0 | { |
13705 | 0 | return bpf_program__attach_btf_id(prog, opts); |
13706 | 0 | } |
13707 | | |
13708 | | struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog) |
13709 | 0 | { |
13710 | 0 | return bpf_program__attach_btf_id(prog, NULL); |
13711 | 0 | } |
13712 | | |
13713 | | static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13714 | 0 | { |
13715 | 0 | *link = bpf_program__attach_trace(prog); |
13716 | 0 | return libbpf_get_error(*link); |
13717 | 0 | } |
13718 | | |
13719 | | static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13720 | 0 | { |
13721 | 0 | *link = bpf_program__attach_lsm(prog); |
13722 | 0 | return libbpf_get_error(*link); |
13723 | 0 | } |
13724 | | |
13725 | | static struct bpf_link * |
13726 | | bpf_program_attach_fd(const struct bpf_program *prog, |
13727 | | int target_fd, const char *target_name, |
13728 | | const struct bpf_link_create_opts *opts) |
13729 | 0 | { |
13730 | 0 | enum bpf_attach_type attach_type; |
13731 | 0 | struct bpf_link *link; |
13732 | 0 | int prog_fd, link_fd; |
13733 | |
|
13734 | 0 | prog_fd = bpf_program__fd(prog); |
13735 | 0 | if (prog_fd < 0) { |
13736 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
13737 | 0 | return libbpf_err_ptr(-EINVAL); |
13738 | 0 | } |
13739 | | |
13740 | 0 | link = calloc(1, sizeof(*link)); |
13741 | 0 | if (!link) |
13742 | 0 | return libbpf_err_ptr(-ENOMEM); |
13743 | 0 | link->detach = &bpf_link__detach_fd; |
13744 | |
|
13745 | 0 | attach_type = bpf_program__expected_attach_type(prog); |
13746 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts); |
13747 | 0 | if (link_fd < 0) { |
13748 | 0 | link_fd = -errno; |
13749 | 0 | free(link); |
13750 | 0 | pr_warn("prog '%s': failed to attach to %s: %s\n", |
13751 | 0 | prog->name, target_name, |
13752 | 0 | errstr(link_fd)); |
13753 | 0 | return libbpf_err_ptr(link_fd); |
13754 | 0 | } |
13755 | 0 | link->fd = link_fd; |
13756 | 0 | return link; |
13757 | 0 | } |
13758 | | |
13759 | | struct bpf_link * |
13760 | | bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd) |
13761 | 0 | { |
13762 | 0 | return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL); |
13763 | 0 | } |
13764 | | |
13765 | | struct bpf_link * |
13766 | | bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd) |
13767 | 0 | { |
13768 | 0 | return bpf_program_attach_fd(prog, netns_fd, "netns", NULL); |
13769 | 0 | } |
13770 | | |
13771 | | struct bpf_link * |
13772 | | bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd) |
13773 | 0 | { |
13774 | 0 | return bpf_program_attach_fd(prog, map_fd, "sockmap", NULL); |
13775 | 0 | } |
13776 | | |
13777 | | struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex) |
13778 | 0 | { |
13779 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
13780 | 0 | return bpf_program_attach_fd(prog, ifindex, "xdp", NULL); |
13781 | 0 | } |
13782 | | |
13783 | | struct bpf_link * |
13784 | | bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd, |
13785 | | const struct bpf_cgroup_opts *opts) |
13786 | 0 | { |
13787 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
13788 | 0 | __u32 relative_id; |
13789 | 0 | int relative_fd; |
13790 | |
|
13791 | 0 | if (!OPTS_VALID(opts, bpf_cgroup_opts)) |
13792 | 0 | return libbpf_err_ptr(-EINVAL); |
13793 | | |
13794 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
13795 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
13796 | |
|
13797 | 0 | if (relative_fd && relative_id) { |
13798 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
13799 | 0 | prog->name); |
13800 | 0 | return libbpf_err_ptr(-EINVAL); |
13801 | 0 | } |
13802 | | |
13803 | 0 | link_create_opts.cgroup.expected_revision = OPTS_GET(opts, expected_revision, 0); |
13804 | 0 | link_create_opts.cgroup.relative_fd = relative_fd; |
13805 | 0 | link_create_opts.cgroup.relative_id = relative_id; |
13806 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
13807 | |
|
13808 | 0 | return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", &link_create_opts); |
13809 | 0 | } |
13810 | | |
13811 | | struct bpf_link * |
13812 | | bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, |
13813 | | const struct bpf_tcx_opts *opts) |
13814 | 0 | { |
13815 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
13816 | 0 | __u32 relative_id; |
13817 | 0 | int relative_fd; |
13818 | |
|
13819 | 0 | if (!OPTS_VALID(opts, bpf_tcx_opts)) |
13820 | 0 | return libbpf_err_ptr(-EINVAL); |
13821 | | |
13822 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
13823 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
13824 | | |
13825 | | /* validate we don't have unexpected combinations of non-zero fields */ |
13826 | 0 | if (!ifindex) { |
13827 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
13828 | 0 | prog->name); |
13829 | 0 | return libbpf_err_ptr(-EINVAL); |
13830 | 0 | } |
13831 | 0 | if (relative_fd && relative_id) { |
13832 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
13833 | 0 | prog->name); |
13834 | 0 | return libbpf_err_ptr(-EINVAL); |
13835 | 0 | } |
13836 | | |
13837 | 0 | link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0); |
13838 | 0 | link_create_opts.tcx.relative_fd = relative_fd; |
13839 | 0 | link_create_opts.tcx.relative_id = relative_id; |
13840 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
13841 | | |
13842 | | /* target_fd/target_ifindex use the same field in LINK_CREATE */ |
13843 | 0 | return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts); |
13844 | 0 | } |
13845 | | |
13846 | | struct bpf_link * |
13847 | | bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, |
13848 | | const struct bpf_netkit_opts *opts) |
13849 | 0 | { |
13850 | 0 | LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
13851 | 0 | __u32 relative_id; |
13852 | 0 | int relative_fd; |
13853 | |
|
13854 | 0 | if (!OPTS_VALID(opts, bpf_netkit_opts)) |
13855 | 0 | return libbpf_err_ptr(-EINVAL); |
13856 | | |
13857 | 0 | relative_id = OPTS_GET(opts, relative_id, 0); |
13858 | 0 | relative_fd = OPTS_GET(opts, relative_fd, 0); |
13859 | | |
13860 | | /* validate we don't have unexpected combinations of non-zero fields */ |
13861 | 0 | if (!ifindex) { |
13862 | 0 | pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", |
13863 | 0 | prog->name); |
13864 | 0 | return libbpf_err_ptr(-EINVAL); |
13865 | 0 | } |
13866 | 0 | if (relative_fd && relative_id) { |
13867 | 0 | pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n", |
13868 | 0 | prog->name); |
13869 | 0 | return libbpf_err_ptr(-EINVAL); |
13870 | 0 | } |
13871 | | |
13872 | 0 | link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0); |
13873 | 0 | link_create_opts.netkit.relative_fd = relative_fd; |
13874 | 0 | link_create_opts.netkit.relative_id = relative_id; |
13875 | 0 | link_create_opts.flags = OPTS_GET(opts, flags, 0); |
13876 | |
|
13877 | 0 | return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts); |
13878 | 0 | } |
13879 | | |
13880 | | struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog, |
13881 | | int target_fd, |
13882 | | const char *attach_func_name) |
13883 | 0 | { |
13884 | 0 | int btf_id; |
13885 | |
|
13886 | 0 | if (!!target_fd != !!attach_func_name) { |
13887 | 0 | pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n", |
13888 | 0 | prog->name); |
13889 | 0 | return libbpf_err_ptr(-EINVAL); |
13890 | 0 | } |
13891 | | |
13892 | 0 | if (prog->type != BPF_PROG_TYPE_EXT) { |
13893 | 0 | pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace\n", |
13894 | 0 | prog->name); |
13895 | 0 | return libbpf_err_ptr(-EINVAL); |
13896 | 0 | } |
13897 | | |
13898 | 0 | if (target_fd) { |
13899 | 0 | LIBBPF_OPTS(bpf_link_create_opts, target_opts); |
13900 | |
|
13901 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd, prog->obj->token_fd); |
13902 | 0 | if (btf_id < 0) |
13903 | 0 | return libbpf_err_ptr(btf_id); |
13904 | | |
13905 | 0 | target_opts.target_btf_id = btf_id; |
13906 | |
|
13907 | 0 | return bpf_program_attach_fd(prog, target_fd, "freplace", |
13908 | 0 | &target_opts); |
13909 | 0 | } else { |
13910 | | /* no target, so use raw_tracepoint_open for compatibility |
13911 | | * with old kernels |
13912 | | */ |
13913 | 0 | return bpf_program__attach_trace(prog); |
13914 | 0 | } |
13915 | 0 | } |
13916 | | |
13917 | | struct bpf_link * |
13918 | | bpf_program__attach_iter(const struct bpf_program *prog, |
13919 | | const struct bpf_iter_attach_opts *opts) |
13920 | 0 | { |
13921 | 0 | DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); |
13922 | 0 | struct bpf_link *link; |
13923 | 0 | int prog_fd, link_fd; |
13924 | 0 | __u32 target_fd = 0; |
13925 | |
|
13926 | 0 | if (!OPTS_VALID(opts, bpf_iter_attach_opts)) |
13927 | 0 | return libbpf_err_ptr(-EINVAL); |
13928 | | |
13929 | 0 | link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0); |
13930 | 0 | link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0); |
13931 | |
|
13932 | 0 | prog_fd = bpf_program__fd(prog); |
13933 | 0 | if (prog_fd < 0) { |
13934 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
13935 | 0 | return libbpf_err_ptr(-EINVAL); |
13936 | 0 | } |
13937 | | |
13938 | 0 | link = calloc(1, sizeof(*link)); |
13939 | 0 | if (!link) |
13940 | 0 | return libbpf_err_ptr(-ENOMEM); |
13941 | 0 | link->detach = &bpf_link__detach_fd; |
13942 | |
|
13943 | 0 | link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, |
13944 | 0 | &link_create_opts); |
13945 | 0 | if (link_fd < 0) { |
13946 | 0 | link_fd = -errno; |
13947 | 0 | free(link); |
13948 | 0 | pr_warn("prog '%s': failed to attach to iterator: %s\n", |
13949 | 0 | prog->name, errstr(link_fd)); |
13950 | 0 | return libbpf_err_ptr(link_fd); |
13951 | 0 | } |
13952 | 0 | link->fd = link_fd; |
13953 | 0 | return link; |
13954 | 0 | } |
13955 | | |
13956 | | static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link) |
13957 | 0 | { |
13958 | 0 | *link = bpf_program__attach_iter(prog, NULL); |
13959 | 0 | return libbpf_get_error(*link); |
13960 | 0 | } |
13961 | | |
13962 | | struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog, |
13963 | | const struct bpf_netfilter_opts *opts) |
13964 | 0 | { |
13965 | 0 | LIBBPF_OPTS(bpf_link_create_opts, lopts); |
13966 | 0 | struct bpf_link *link; |
13967 | 0 | int prog_fd, link_fd; |
13968 | |
|
13969 | 0 | if (!OPTS_VALID(opts, bpf_netfilter_opts)) |
13970 | 0 | return libbpf_err_ptr(-EINVAL); |
13971 | | |
13972 | 0 | prog_fd = bpf_program__fd(prog); |
13973 | 0 | if (prog_fd < 0) { |
13974 | 0 | pr_warn("prog '%s': can't attach before loaded\n", prog->name); |
13975 | 0 | return libbpf_err_ptr(-EINVAL); |
13976 | 0 | } |
13977 | | |
13978 | 0 | link = calloc(1, sizeof(*link)); |
13979 | 0 | if (!link) |
13980 | 0 | return libbpf_err_ptr(-ENOMEM); |
13981 | | |
13982 | 0 | link->detach = &bpf_link__detach_fd; |
13983 | |
|
13984 | 0 | lopts.netfilter.pf = OPTS_GET(opts, pf, 0); |
13985 | 0 | lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0); |
13986 | 0 | lopts.netfilter.priority = OPTS_GET(opts, priority, 0); |
13987 | 0 | lopts.netfilter.flags = OPTS_GET(opts, flags, 0); |
13988 | |
|
13989 | 0 | link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts); |
13990 | 0 | if (link_fd < 0) { |
13991 | 0 | link_fd = -errno; |
13992 | 0 | free(link); |
13993 | 0 | pr_warn("prog '%s': failed to attach to netfilter: %s\n", |
13994 | 0 | prog->name, errstr(link_fd)); |
13995 | 0 | return libbpf_err_ptr(link_fd); |
13996 | 0 | } |
13997 | 0 | link->fd = link_fd; |
13998 | |
|
13999 | 0 | return link; |
14000 | 0 | } |
14001 | | |
14002 | | struct bpf_link *bpf_program__attach(const struct bpf_program *prog) |
14003 | 0 | { |
14004 | 0 | struct bpf_link *link = NULL; |
14005 | 0 | int err; |
14006 | |
|
14007 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
14008 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
14009 | | |
14010 | 0 | if (bpf_program__fd(prog) < 0) { |
14011 | 0 | pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n", |
14012 | 0 | prog->name); |
14013 | 0 | return libbpf_err_ptr(-EINVAL); |
14014 | 0 | } |
14015 | | |
14016 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link); |
14017 | 0 | if (err) |
14018 | 0 | return libbpf_err_ptr(err); |
14019 | | |
14020 | | /* When calling bpf_program__attach() explicitly, auto-attach support |
14021 | | * is expected to work, so NULL returned link is considered an error. |
14022 | | * This is different for skeleton's attach, see comment in |
14023 | | * bpf_object__attach_skeleton(). |
14024 | | */ |
14025 | 0 | if (!link) |
14026 | 0 | return libbpf_err_ptr(-EOPNOTSUPP); |
14027 | | |
14028 | 0 | return link; |
14029 | 0 | } |
14030 | | |
14031 | | struct bpf_link_struct_ops { |
14032 | | struct bpf_link link; |
14033 | | int map_fd; |
14034 | | }; |
14035 | | |
14036 | | static int bpf_link__detach_struct_ops(struct bpf_link *link) |
14037 | 0 | { |
14038 | 0 | struct bpf_link_struct_ops *st_link; |
14039 | 0 | __u32 zero = 0; |
14040 | |
|
14041 | 0 | st_link = container_of(link, struct bpf_link_struct_ops, link); |
14042 | |
|
14043 | 0 | if (st_link->map_fd < 0) |
14044 | | /* w/o a real link */ |
14045 | 0 | return bpf_map_delete_elem(link->fd, &zero); |
14046 | | |
14047 | 0 | return close(link->fd); |
14048 | 0 | } |
14049 | | |
14050 | | struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map) |
14051 | 0 | { |
14052 | 0 | struct bpf_link_struct_ops *link; |
14053 | 0 | __u32 zero = 0; |
14054 | 0 | int err, fd; |
14055 | |
|
14056 | 0 | if (!bpf_map__is_struct_ops(map)) { |
14057 | 0 | pr_warn("map '%s': can't attach non-struct_ops map\n", map->name); |
14058 | 0 | return libbpf_err_ptr(-EINVAL); |
14059 | 0 | } |
14060 | | |
14061 | 0 | if (map->fd < 0) { |
14062 | 0 | pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name); |
14063 | 0 | return libbpf_err_ptr(-EINVAL); |
14064 | 0 | } |
14065 | | |
14066 | 0 | link = calloc(1, sizeof(*link)); |
14067 | 0 | if (!link) |
14068 | 0 | return libbpf_err_ptr(-EINVAL); |
14069 | | |
14070 | | /* kern_vdata should be prepared during the loading phase. */ |
14071 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
14072 | | /* It can be EBUSY if the map has been used to create or |
14073 | | * update a link before. We don't allow updating the value of |
14074 | | * a struct_ops once it is set. That ensures that the value |
14075 | | * never changed. So, it is safe to skip EBUSY. |
14076 | | */ |
14077 | 0 | if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) { |
14078 | 0 | free(link); |
14079 | 0 | return libbpf_err_ptr(err); |
14080 | 0 | } |
14081 | | |
14082 | 0 | link->link.detach = bpf_link__detach_struct_ops; |
14083 | |
|
14084 | 0 | if (!(map->def.map_flags & BPF_F_LINK)) { |
14085 | | /* w/o a real link */ |
14086 | 0 | link->link.fd = map->fd; |
14087 | 0 | link->map_fd = -1; |
14088 | 0 | return &link->link; |
14089 | 0 | } |
14090 | | |
14091 | 0 | fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL); |
14092 | 0 | if (fd < 0) { |
14093 | 0 | free(link); |
14094 | 0 | return libbpf_err_ptr(fd); |
14095 | 0 | } |
14096 | | |
14097 | 0 | link->link.fd = fd; |
14098 | 0 | link->map_fd = map->fd; |
14099 | |
|
14100 | 0 | return &link->link; |
14101 | 0 | } |
14102 | | |
14103 | | /* |
14104 | | * Swap the back struct_ops of a link with a new struct_ops map. |
14105 | | */ |
14106 | | int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map) |
14107 | 0 | { |
14108 | 0 | struct bpf_link_struct_ops *st_ops_link; |
14109 | 0 | __u32 zero = 0; |
14110 | 0 | int err; |
14111 | |
|
14112 | 0 | if (!bpf_map__is_struct_ops(map)) |
14113 | 0 | return libbpf_err(-EINVAL); |
14114 | | |
14115 | 0 | if (map->fd < 0) { |
14116 | 0 | pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name); |
14117 | 0 | return libbpf_err(-EINVAL); |
14118 | 0 | } |
14119 | | |
14120 | 0 | st_ops_link = container_of(link, struct bpf_link_struct_ops, link); |
14121 | | /* Ensure the type of a link is correct */ |
14122 | 0 | if (st_ops_link->map_fd < 0) |
14123 | 0 | return libbpf_err(-EINVAL); |
14124 | | |
14125 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0); |
14126 | | /* It can be EBUSY if the map has been used to create or |
14127 | | * update a link before. We don't allow updating the value of |
14128 | | * a struct_ops once it is set. That ensures that the value |
14129 | | * never changed. So, it is safe to skip EBUSY. |
14130 | | */ |
14131 | 0 | if (err && err != -EBUSY) |
14132 | 0 | return err; |
14133 | | |
14134 | 0 | err = bpf_link_update(link->fd, map->fd, NULL); |
14135 | 0 | if (err < 0) |
14136 | 0 | return err; |
14137 | | |
14138 | 0 | st_ops_link->map_fd = map->fd; |
14139 | |
|
14140 | 0 | return 0; |
14141 | 0 | } |
14142 | | |
14143 | | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr, |
14144 | | void *private_data); |
14145 | | |
14146 | | static enum bpf_perf_event_ret |
14147 | | perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
14148 | | void **copy_mem, size_t *copy_size, |
14149 | | bpf_perf_event_print_t fn, void *private_data) |
14150 | 0 | { |
14151 | 0 | struct perf_event_mmap_page *header = mmap_mem; |
14152 | 0 | __u64 data_head = ring_buffer_read_head(header); |
14153 | 0 | __u64 data_tail = header->data_tail; |
14154 | 0 | void *base = ((__u8 *)header) + page_size; |
14155 | 0 | int ret = LIBBPF_PERF_EVENT_CONT; |
14156 | 0 | struct perf_event_header *ehdr; |
14157 | 0 | size_t ehdr_size; |
14158 | |
|
14159 | 0 | while (data_head != data_tail) { |
14160 | 0 | ehdr = base + (data_tail & (mmap_size - 1)); |
14161 | 0 | ehdr_size = ehdr->size; |
14162 | |
|
14163 | 0 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { |
14164 | 0 | void *copy_start = ehdr; |
14165 | 0 | size_t len_first = base + mmap_size - copy_start; |
14166 | 0 | size_t len_secnd = ehdr_size - len_first; |
14167 | |
|
14168 | 0 | if (*copy_size < ehdr_size) { |
14169 | 0 | free(*copy_mem); |
14170 | 0 | *copy_mem = malloc(ehdr_size); |
14171 | 0 | if (!*copy_mem) { |
14172 | 0 | *copy_size = 0; |
14173 | 0 | ret = LIBBPF_PERF_EVENT_ERROR; |
14174 | 0 | break; |
14175 | 0 | } |
14176 | 0 | *copy_size = ehdr_size; |
14177 | 0 | } |
14178 | | |
14179 | 0 | memcpy(*copy_mem, copy_start, len_first); |
14180 | 0 | memcpy(*copy_mem + len_first, base, len_secnd); |
14181 | 0 | ehdr = *copy_mem; |
14182 | 0 | } |
14183 | | |
14184 | 0 | ret = fn(ehdr, private_data); |
14185 | 0 | data_tail += ehdr_size; |
14186 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
14187 | 0 | break; |
14188 | 0 | } |
14189 | |
|
14190 | 0 | ring_buffer_write_tail(header, data_tail); |
14191 | 0 | return libbpf_err(ret); |
14192 | 0 | } |
14193 | | |
14194 | | struct perf_buffer; |
14195 | | |
14196 | | struct perf_buffer_params { |
14197 | | struct perf_event_attr *attr; |
14198 | | /* if event_cb is specified, it takes precendence */ |
14199 | | perf_buffer_event_fn event_cb; |
14200 | | /* sample_cb and lost_cb are higher-level common-case callbacks */ |
14201 | | perf_buffer_sample_fn sample_cb; |
14202 | | perf_buffer_lost_fn lost_cb; |
14203 | | void *ctx; |
14204 | | int cpu_cnt; |
14205 | | int *cpus; |
14206 | | int *map_keys; |
14207 | | }; |
14208 | | |
14209 | | struct perf_cpu_buf { |
14210 | | struct perf_buffer *pb; |
14211 | | void *base; /* mmap()'ed memory */ |
14212 | | void *buf; /* for reconstructing segmented data */ |
14213 | | size_t buf_size; |
14214 | | int fd; |
14215 | | int cpu; |
14216 | | int map_key; |
14217 | | }; |
14218 | | |
14219 | | struct perf_buffer { |
14220 | | perf_buffer_event_fn event_cb; |
14221 | | perf_buffer_sample_fn sample_cb; |
14222 | | perf_buffer_lost_fn lost_cb; |
14223 | | void *ctx; /* passed into callbacks */ |
14224 | | |
14225 | | size_t page_size; |
14226 | | size_t mmap_size; |
14227 | | struct perf_cpu_buf **cpu_bufs; |
14228 | | struct epoll_event *events; |
14229 | | int cpu_cnt; /* number of allocated CPU buffers */ |
14230 | | int epoll_fd; /* perf event FD */ |
14231 | | int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ |
14232 | | }; |
14233 | | |
14234 | | static void perf_buffer__free_cpu_buf(struct perf_buffer *pb, |
14235 | | struct perf_cpu_buf *cpu_buf) |
14236 | 0 | { |
14237 | 0 | if (!cpu_buf) |
14238 | 0 | return; |
14239 | 0 | if (cpu_buf->base && |
14240 | 0 | munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) |
14241 | 0 | pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); |
14242 | 0 | if (cpu_buf->fd >= 0) { |
14243 | 0 | ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); |
14244 | 0 | close(cpu_buf->fd); |
14245 | 0 | } |
14246 | 0 | free(cpu_buf->buf); |
14247 | 0 | free(cpu_buf); |
14248 | 0 | } |
14249 | | |
14250 | | void perf_buffer__free(struct perf_buffer *pb) |
14251 | 0 | { |
14252 | 0 | int i; |
14253 | |
|
14254 | 0 | if (IS_ERR_OR_NULL(pb)) |
14255 | 0 | return; |
14256 | 0 | if (pb->cpu_bufs) { |
14257 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
14258 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
14259 | |
|
14260 | 0 | if (!cpu_buf) |
14261 | 0 | continue; |
14262 | | |
14263 | 0 | bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); |
14264 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
14265 | 0 | } |
14266 | 0 | free(pb->cpu_bufs); |
14267 | 0 | } |
14268 | 0 | if (pb->epoll_fd >= 0) |
14269 | 0 | close(pb->epoll_fd); |
14270 | 0 | free(pb->events); |
14271 | 0 | free(pb); |
14272 | 0 | } |
14273 | | |
14274 | | static struct perf_cpu_buf * |
14275 | | perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, |
14276 | | int cpu, int map_key) |
14277 | 0 | { |
14278 | 0 | struct perf_cpu_buf *cpu_buf; |
14279 | 0 | int err; |
14280 | |
|
14281 | 0 | cpu_buf = calloc(1, sizeof(*cpu_buf)); |
14282 | 0 | if (!cpu_buf) |
14283 | 0 | return ERR_PTR(-ENOMEM); |
14284 | | |
14285 | 0 | cpu_buf->pb = pb; |
14286 | 0 | cpu_buf->cpu = cpu; |
14287 | 0 | cpu_buf->map_key = map_key; |
14288 | |
|
14289 | 0 | cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, |
14290 | 0 | -1, PERF_FLAG_FD_CLOEXEC); |
14291 | 0 | if (cpu_buf->fd < 0) { |
14292 | 0 | err = -errno; |
14293 | 0 | pr_warn("failed to open perf buffer event on cpu #%d: %s\n", |
14294 | 0 | cpu, errstr(err)); |
14295 | 0 | goto error; |
14296 | 0 | } |
14297 | | |
14298 | 0 | cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, |
14299 | 0 | PROT_READ | PROT_WRITE, MAP_SHARED, |
14300 | 0 | cpu_buf->fd, 0); |
14301 | 0 | if (cpu_buf->base == MAP_FAILED) { |
14302 | 0 | cpu_buf->base = NULL; |
14303 | 0 | err = -errno; |
14304 | 0 | pr_warn("failed to mmap perf buffer on cpu #%d: %s\n", |
14305 | 0 | cpu, errstr(err)); |
14306 | 0 | goto error; |
14307 | 0 | } |
14308 | | |
14309 | 0 | if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
14310 | 0 | err = -errno; |
14311 | 0 | pr_warn("failed to enable perf buffer event on cpu #%d: %s\n", |
14312 | 0 | cpu, errstr(err)); |
14313 | 0 | goto error; |
14314 | 0 | } |
14315 | | |
14316 | 0 | return cpu_buf; |
14317 | | |
14318 | 0 | error: |
14319 | 0 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
14320 | 0 | return (struct perf_cpu_buf *)ERR_PTR(err); |
14321 | 0 | } |
14322 | | |
14323 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
14324 | | struct perf_buffer_params *p); |
14325 | | |
14326 | | struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, |
14327 | | perf_buffer_sample_fn sample_cb, |
14328 | | perf_buffer_lost_fn lost_cb, |
14329 | | void *ctx, |
14330 | | const struct perf_buffer_opts *opts) |
14331 | 0 | { |
14332 | 0 | const size_t attr_sz = sizeof(struct perf_event_attr); |
14333 | 0 | struct perf_buffer_params p = {}; |
14334 | 0 | struct perf_event_attr attr; |
14335 | 0 | __u32 sample_period; |
14336 | |
|
14337 | 0 | if (!OPTS_VALID(opts, perf_buffer_opts)) |
14338 | 0 | return libbpf_err_ptr(-EINVAL); |
14339 | | |
14340 | 0 | sample_period = OPTS_GET(opts, sample_period, 1); |
14341 | 0 | if (!sample_period) |
14342 | 0 | sample_period = 1; |
14343 | |
|
14344 | 0 | memset(&attr, 0, attr_sz); |
14345 | 0 | attr.size = attr_sz; |
14346 | 0 | attr.config = PERF_COUNT_SW_BPF_OUTPUT; |
14347 | 0 | attr.type = PERF_TYPE_SOFTWARE; |
14348 | 0 | attr.sample_type = PERF_SAMPLE_RAW; |
14349 | 0 | attr.wakeup_events = sample_period; |
14350 | |
|
14351 | 0 | p.attr = &attr; |
14352 | 0 | p.sample_cb = sample_cb; |
14353 | 0 | p.lost_cb = lost_cb; |
14354 | 0 | p.ctx = ctx; |
14355 | |
|
14356 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
14357 | 0 | } |
14358 | | |
14359 | | struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt, |
14360 | | struct perf_event_attr *attr, |
14361 | | perf_buffer_event_fn event_cb, void *ctx, |
14362 | | const struct perf_buffer_raw_opts *opts) |
14363 | 0 | { |
14364 | 0 | struct perf_buffer_params p = {}; |
14365 | |
|
14366 | 0 | if (!attr) |
14367 | 0 | return libbpf_err_ptr(-EINVAL); |
14368 | | |
14369 | 0 | if (!OPTS_VALID(opts, perf_buffer_raw_opts)) |
14370 | 0 | return libbpf_err_ptr(-EINVAL); |
14371 | | |
14372 | 0 | p.attr = attr; |
14373 | 0 | p.event_cb = event_cb; |
14374 | 0 | p.ctx = ctx; |
14375 | 0 | p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0); |
14376 | 0 | p.cpus = OPTS_GET(opts, cpus, NULL); |
14377 | 0 | p.map_keys = OPTS_GET(opts, map_keys, NULL); |
14378 | |
|
14379 | 0 | return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p)); |
14380 | 0 | } |
14381 | | |
14382 | | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
14383 | | struct perf_buffer_params *p) |
14384 | 0 | { |
14385 | 0 | const char *online_cpus_file = "/sys/devices/system/cpu/online"; |
14386 | 0 | struct bpf_map_info map; |
14387 | 0 | struct perf_buffer *pb; |
14388 | 0 | bool *online = NULL; |
14389 | 0 | __u32 map_info_len; |
14390 | 0 | int err, i, j, n; |
14391 | |
|
14392 | 0 | if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) { |
14393 | 0 | pr_warn("page count should be power of two, but is %zu\n", |
14394 | 0 | page_cnt); |
14395 | 0 | return ERR_PTR(-EINVAL); |
14396 | 0 | } |
14397 | | |
14398 | | /* best-effort sanity checks */ |
14399 | 0 | memset(&map, 0, sizeof(map)); |
14400 | 0 | map_info_len = sizeof(map); |
14401 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len); |
14402 | 0 | if (err) { |
14403 | 0 | err = -errno; |
14404 | | /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return |
14405 | | * -EBADFD, -EFAULT, or -E2BIG on real error |
14406 | | */ |
14407 | 0 | if (err != -EINVAL) { |
14408 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", |
14409 | 0 | map_fd, errstr(err)); |
14410 | 0 | return ERR_PTR(err); |
14411 | 0 | } |
14412 | 0 | pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n", |
14413 | 0 | map_fd); |
14414 | 0 | } else { |
14415 | 0 | if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
14416 | 0 | pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
14417 | 0 | map.name); |
14418 | 0 | return ERR_PTR(-EINVAL); |
14419 | 0 | } |
14420 | 0 | } |
14421 | | |
14422 | 0 | pb = calloc(1, sizeof(*pb)); |
14423 | 0 | if (!pb) |
14424 | 0 | return ERR_PTR(-ENOMEM); |
14425 | | |
14426 | 0 | pb->event_cb = p->event_cb; |
14427 | 0 | pb->sample_cb = p->sample_cb; |
14428 | 0 | pb->lost_cb = p->lost_cb; |
14429 | 0 | pb->ctx = p->ctx; |
14430 | |
|
14431 | 0 | pb->page_size = getpagesize(); |
14432 | 0 | pb->mmap_size = pb->page_size * page_cnt; |
14433 | 0 | pb->map_fd = map_fd; |
14434 | |
|
14435 | 0 | pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
14436 | 0 | if (pb->epoll_fd < 0) { |
14437 | 0 | err = -errno; |
14438 | 0 | pr_warn("failed to create epoll instance: %s\n", |
14439 | 0 | errstr(err)); |
14440 | 0 | goto error; |
14441 | 0 | } |
14442 | | |
14443 | 0 | if (p->cpu_cnt > 0) { |
14444 | 0 | pb->cpu_cnt = p->cpu_cnt; |
14445 | 0 | } else { |
14446 | 0 | pb->cpu_cnt = libbpf_num_possible_cpus(); |
14447 | 0 | if (pb->cpu_cnt < 0) { |
14448 | 0 | err = pb->cpu_cnt; |
14449 | 0 | goto error; |
14450 | 0 | } |
14451 | 0 | if (map.max_entries && map.max_entries < pb->cpu_cnt) |
14452 | 0 | pb->cpu_cnt = map.max_entries; |
14453 | 0 | } |
14454 | | |
14455 | 0 | pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); |
14456 | 0 | if (!pb->events) { |
14457 | 0 | err = -ENOMEM; |
14458 | 0 | pr_warn("failed to allocate events: out of memory\n"); |
14459 | 0 | goto error; |
14460 | 0 | } |
14461 | 0 | pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); |
14462 | 0 | if (!pb->cpu_bufs) { |
14463 | 0 | err = -ENOMEM; |
14464 | 0 | pr_warn("failed to allocate buffers: out of memory\n"); |
14465 | 0 | goto error; |
14466 | 0 | } |
14467 | | |
14468 | 0 | err = parse_cpu_mask_file(online_cpus_file, &online, &n); |
14469 | 0 | if (err) { |
14470 | 0 | pr_warn("failed to get online CPU mask: %s\n", errstr(err)); |
14471 | 0 | goto error; |
14472 | 0 | } |
14473 | | |
14474 | 0 | for (i = 0, j = 0; i < pb->cpu_cnt; i++) { |
14475 | 0 | struct perf_cpu_buf *cpu_buf; |
14476 | 0 | int cpu, map_key; |
14477 | |
|
14478 | 0 | cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; |
14479 | 0 | map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; |
14480 | | |
14481 | | /* in case user didn't explicitly requested particular CPUs to |
14482 | | * be attached to, skip offline/not present CPUs |
14483 | | */ |
14484 | 0 | if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) |
14485 | 0 | continue; |
14486 | | |
14487 | 0 | cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); |
14488 | 0 | if (IS_ERR(cpu_buf)) { |
14489 | 0 | err = PTR_ERR(cpu_buf); |
14490 | 0 | goto error; |
14491 | 0 | } |
14492 | | |
14493 | 0 | pb->cpu_bufs[j] = cpu_buf; |
14494 | |
|
14495 | 0 | err = bpf_map_update_elem(pb->map_fd, &map_key, |
14496 | 0 | &cpu_buf->fd, 0); |
14497 | 0 | if (err) { |
14498 | 0 | err = -errno; |
14499 | 0 | pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", |
14500 | 0 | cpu, map_key, cpu_buf->fd, |
14501 | 0 | errstr(err)); |
14502 | 0 | goto error; |
14503 | 0 | } |
14504 | | |
14505 | 0 | pb->events[j].events = EPOLLIN; |
14506 | 0 | pb->events[j].data.ptr = cpu_buf; |
14507 | 0 | if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, |
14508 | 0 | &pb->events[j]) < 0) { |
14509 | 0 | err = -errno; |
14510 | 0 | pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", |
14511 | 0 | cpu, cpu_buf->fd, |
14512 | 0 | errstr(err)); |
14513 | 0 | goto error; |
14514 | 0 | } |
14515 | 0 | j++; |
14516 | 0 | } |
14517 | 0 | pb->cpu_cnt = j; |
14518 | 0 | free(online); |
14519 | |
|
14520 | 0 | return pb; |
14521 | | |
14522 | 0 | error: |
14523 | 0 | free(online); |
14524 | 0 | if (pb) |
14525 | 0 | perf_buffer__free(pb); |
14526 | 0 | return ERR_PTR(err); |
14527 | 0 | } |
14528 | | |
14529 | | struct perf_sample_raw { |
14530 | | struct perf_event_header header; |
14531 | | uint32_t size; |
14532 | | char data[]; |
14533 | | }; |
14534 | | |
14535 | | struct perf_sample_lost { |
14536 | | struct perf_event_header header; |
14537 | | uint64_t id; |
14538 | | uint64_t lost; |
14539 | | uint64_t sample_id; |
14540 | | }; |
14541 | | |
14542 | | static enum bpf_perf_event_ret |
14543 | | perf_buffer__process_record(struct perf_event_header *e, void *ctx) |
14544 | 0 | { |
14545 | 0 | struct perf_cpu_buf *cpu_buf = ctx; |
14546 | 0 | struct perf_buffer *pb = cpu_buf->pb; |
14547 | 0 | void *data = e; |
14548 | | |
14549 | | /* user wants full control over parsing perf event */ |
14550 | 0 | if (pb->event_cb) |
14551 | 0 | return pb->event_cb(pb->ctx, cpu_buf->cpu, e); |
14552 | | |
14553 | 0 | switch (e->type) { |
14554 | 0 | case PERF_RECORD_SAMPLE: { |
14555 | 0 | struct perf_sample_raw *s = data; |
14556 | |
|
14557 | 0 | if (pb->sample_cb) |
14558 | 0 | pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); |
14559 | 0 | break; |
14560 | 0 | } |
14561 | 0 | case PERF_RECORD_LOST: { |
14562 | 0 | struct perf_sample_lost *s = data; |
14563 | |
|
14564 | 0 | if (pb->lost_cb) |
14565 | 0 | pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); |
14566 | 0 | break; |
14567 | 0 | } |
14568 | 0 | default: |
14569 | 0 | pr_warn("unknown perf sample type %u\n", e->type); |
14570 | 0 | return LIBBPF_PERF_EVENT_ERROR; |
14571 | 0 | } |
14572 | 0 | return LIBBPF_PERF_EVENT_CONT; |
14573 | 0 | } |
14574 | | |
14575 | | static int perf_buffer__process_records(struct perf_buffer *pb, |
14576 | | struct perf_cpu_buf *cpu_buf) |
14577 | 0 | { |
14578 | 0 | enum bpf_perf_event_ret ret; |
14579 | |
|
14580 | 0 | ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size, |
14581 | 0 | pb->page_size, &cpu_buf->buf, |
14582 | 0 | &cpu_buf->buf_size, |
14583 | 0 | perf_buffer__process_record, cpu_buf); |
14584 | 0 | if (ret != LIBBPF_PERF_EVENT_CONT) |
14585 | 0 | return ret; |
14586 | 0 | return 0; |
14587 | 0 | } |
14588 | | |
14589 | | int perf_buffer__epoll_fd(const struct perf_buffer *pb) |
14590 | 0 | { |
14591 | 0 | return pb->epoll_fd; |
14592 | 0 | } |
14593 | | |
14594 | | int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) |
14595 | 0 | { |
14596 | 0 | int i, cnt, err; |
14597 | |
|
14598 | 0 | cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); |
14599 | 0 | if (cnt < 0) |
14600 | 0 | return -errno; |
14601 | | |
14602 | 0 | for (i = 0; i < cnt; i++) { |
14603 | 0 | struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; |
14604 | |
|
14605 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
14606 | 0 | if (err) { |
14607 | 0 | pr_warn("error while processing records: %s\n", errstr(err)); |
14608 | 0 | return libbpf_err(err); |
14609 | 0 | } |
14610 | 0 | } |
14611 | 0 | return cnt; |
14612 | 0 | } |
14613 | | |
14614 | | /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer |
14615 | | * manager. |
14616 | | */ |
14617 | | size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb) |
14618 | 0 | { |
14619 | 0 | return pb->cpu_cnt; |
14620 | 0 | } |
14621 | | |
14622 | | /* |
14623 | | * Return perf_event FD of a ring buffer in *buf_idx* slot of |
14624 | | * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using |
14625 | | * select()/poll()/epoll() Linux syscalls. |
14626 | | */ |
14627 | | int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx) |
14628 | 0 | { |
14629 | 0 | struct perf_cpu_buf *cpu_buf; |
14630 | |
|
14631 | 0 | if (buf_idx >= pb->cpu_cnt) |
14632 | 0 | return libbpf_err(-EINVAL); |
14633 | | |
14634 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
14635 | 0 | if (!cpu_buf) |
14636 | 0 | return libbpf_err(-ENOENT); |
14637 | | |
14638 | 0 | return cpu_buf->fd; |
14639 | 0 | } |
14640 | | |
14641 | | int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size) |
14642 | 0 | { |
14643 | 0 | struct perf_cpu_buf *cpu_buf; |
14644 | |
|
14645 | 0 | if (buf_idx >= pb->cpu_cnt) |
14646 | 0 | return libbpf_err(-EINVAL); |
14647 | | |
14648 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
14649 | 0 | if (!cpu_buf) |
14650 | 0 | return libbpf_err(-ENOENT); |
14651 | | |
14652 | 0 | *buf = cpu_buf->base; |
14653 | 0 | *buf_size = pb->mmap_size; |
14654 | 0 | return 0; |
14655 | 0 | } |
14656 | | |
14657 | | /* |
14658 | | * Consume data from perf ring buffer corresponding to slot *buf_idx* in |
14659 | | * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to |
14660 | | * consume, do nothing and return success. |
14661 | | * Returns: |
14662 | | * - 0 on success; |
14663 | | * - <0 on failure. |
14664 | | */ |
14665 | | int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx) |
14666 | 0 | { |
14667 | 0 | struct perf_cpu_buf *cpu_buf; |
14668 | |
|
14669 | 0 | if (buf_idx >= pb->cpu_cnt) |
14670 | 0 | return libbpf_err(-EINVAL); |
14671 | | |
14672 | 0 | cpu_buf = pb->cpu_bufs[buf_idx]; |
14673 | 0 | if (!cpu_buf) |
14674 | 0 | return libbpf_err(-ENOENT); |
14675 | | |
14676 | 0 | return perf_buffer__process_records(pb, cpu_buf); |
14677 | 0 | } |
14678 | | |
14679 | | int perf_buffer__consume(struct perf_buffer *pb) |
14680 | 0 | { |
14681 | 0 | int i, err; |
14682 | |
|
14683 | 0 | for (i = 0; i < pb->cpu_cnt; i++) { |
14684 | 0 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
14685 | |
|
14686 | 0 | if (!cpu_buf) |
14687 | 0 | continue; |
14688 | | |
14689 | 0 | err = perf_buffer__process_records(pb, cpu_buf); |
14690 | 0 | if (err) { |
14691 | 0 | pr_warn("perf_buffer: failed to process records in buffer #%d: %s\n", |
14692 | 0 | i, errstr(err)); |
14693 | 0 | return libbpf_err(err); |
14694 | 0 | } |
14695 | 0 | } |
14696 | 0 | return 0; |
14697 | 0 | } |
14698 | | |
14699 | | int bpf_program__set_attach_target(struct bpf_program *prog, |
14700 | | int attach_prog_fd, |
14701 | | const char *attach_func_name) |
14702 | 0 | { |
14703 | 0 | int btf_obj_fd = 0, btf_id = 0, err; |
14704 | |
|
14705 | 0 | if (!prog || attach_prog_fd < 0) |
14706 | 0 | return libbpf_err(-EINVAL); |
14707 | | |
14708 | 0 | if (prog->obj->state >= OBJ_LOADED) |
14709 | 0 | return libbpf_err(-EINVAL); |
14710 | | |
14711 | 0 | if (attach_prog_fd && !attach_func_name) { |
14712 | | /* Store attach_prog_fd. The BTF ID will be resolved later during |
14713 | | * the normal object/program load phase. |
14714 | | */ |
14715 | 0 | prog->attach_prog_fd = attach_prog_fd; |
14716 | 0 | return 0; |
14717 | 0 | } |
14718 | | |
14719 | 0 | if (attach_prog_fd) { |
14720 | 0 | btf_id = libbpf_find_prog_btf_id(attach_func_name, |
14721 | 0 | attach_prog_fd, prog->obj->token_fd); |
14722 | 0 | if (btf_id < 0) |
14723 | 0 | return libbpf_err(btf_id); |
14724 | 0 | } else { |
14725 | 0 | if (!attach_func_name) |
14726 | 0 | return libbpf_err(-EINVAL); |
14727 | | |
14728 | | /* load btf_vmlinux, if not yet */ |
14729 | 0 | err = bpf_object__load_vmlinux_btf(prog->obj, true); |
14730 | 0 | if (err) |
14731 | 0 | return libbpf_err(err); |
14732 | 0 | err = find_kernel_btf_id(prog->obj, attach_func_name, |
14733 | 0 | prog->expected_attach_type, |
14734 | 0 | &btf_obj_fd, &btf_id); |
14735 | 0 | if (err) |
14736 | 0 | return libbpf_err(err); |
14737 | 0 | } |
14738 | | |
14739 | 0 | prog->attach_btf_id = btf_id; |
14740 | 0 | prog->attach_btf_obj_fd = btf_obj_fd; |
14741 | 0 | prog->attach_prog_fd = attach_prog_fd; |
14742 | 0 | return 0; |
14743 | 0 | } |
14744 | | |
14745 | | int bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map, |
14746 | | struct bpf_prog_assoc_struct_ops_opts *opts) |
14747 | 0 | { |
14748 | 0 | int prog_fd, map_fd; |
14749 | |
|
14750 | 0 | prog_fd = bpf_program__fd(prog); |
14751 | 0 | if (prog_fd < 0) { |
14752 | 0 | pr_warn("prog '%s': can't associate BPF program without FD (was it loaded?)\n", |
14753 | 0 | prog->name); |
14754 | 0 | return libbpf_err(-EINVAL); |
14755 | 0 | } |
14756 | | |
14757 | 0 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) { |
14758 | 0 | pr_warn("prog '%s': can't associate struct_ops program\n", prog->name); |
14759 | 0 | return libbpf_err(-EINVAL); |
14760 | 0 | } |
14761 | | |
14762 | 0 | map_fd = bpf_map__fd(map); |
14763 | 0 | if (map_fd < 0) { |
14764 | 0 | pr_warn("map '%s': can't associate BPF map without FD (was it created?)\n", map->name); |
14765 | 0 | return libbpf_err(-EINVAL); |
14766 | 0 | } |
14767 | | |
14768 | 0 | if (!bpf_map__is_struct_ops(map)) { |
14769 | 0 | pr_warn("map '%s': can't associate non-struct_ops map\n", map->name); |
14770 | 0 | return libbpf_err(-EINVAL); |
14771 | 0 | } |
14772 | | |
14773 | 0 | return bpf_prog_assoc_struct_ops(prog_fd, map_fd, opts); |
14774 | 0 | } |
14775 | | |
14776 | | int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz) |
14777 | 0 | { |
14778 | 0 | int err = 0, n, len, start, end = -1; |
14779 | 0 | bool *tmp; |
14780 | |
|
14781 | 0 | *mask = NULL; |
14782 | 0 | *mask_sz = 0; |
14783 | | |
14784 | | /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ |
14785 | 0 | while (*s) { |
14786 | 0 | if (*s == ',' || *s == '\n') { |
14787 | 0 | s++; |
14788 | 0 | continue; |
14789 | 0 | } |
14790 | 0 | n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); |
14791 | 0 | if (n <= 0 || n > 2) { |
14792 | 0 | pr_warn("Failed to get CPU range %s: %d\n", s, n); |
14793 | 0 | err = -EINVAL; |
14794 | 0 | goto cleanup; |
14795 | 0 | } else if (n == 1) { |
14796 | 0 | end = start; |
14797 | 0 | } |
14798 | 0 | if (start < 0 || start > end) { |
14799 | 0 | pr_warn("Invalid CPU range [%d,%d] in %s\n", |
14800 | 0 | start, end, s); |
14801 | 0 | err = -EINVAL; |
14802 | 0 | goto cleanup; |
14803 | 0 | } |
14804 | 0 | tmp = realloc(*mask, end + 1); |
14805 | 0 | if (!tmp) { |
14806 | 0 | err = -ENOMEM; |
14807 | 0 | goto cleanup; |
14808 | 0 | } |
14809 | 0 | *mask = tmp; |
14810 | 0 | memset(tmp + *mask_sz, 0, start - *mask_sz); |
14811 | 0 | memset(tmp + start, 1, end - start + 1); |
14812 | 0 | *mask_sz = end + 1; |
14813 | 0 | s += len; |
14814 | 0 | } |
14815 | 0 | if (!*mask_sz) { |
14816 | 0 | pr_warn("Empty CPU range\n"); |
14817 | 0 | return -EINVAL; |
14818 | 0 | } |
14819 | 0 | return 0; |
14820 | 0 | cleanup: |
14821 | 0 | free(*mask); |
14822 | 0 | *mask = NULL; |
14823 | 0 | return err; |
14824 | 0 | } |
14825 | | |
14826 | | int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) |
14827 | 0 | { |
14828 | 0 | int fd, err = 0, len; |
14829 | 0 | char buf[128]; |
14830 | |
|
14831 | 0 | fd = open(fcpu, O_RDONLY | O_CLOEXEC); |
14832 | 0 | if (fd < 0) { |
14833 | 0 | err = -errno; |
14834 | 0 | pr_warn("Failed to open cpu mask file %s: %s\n", fcpu, errstr(err)); |
14835 | 0 | return err; |
14836 | 0 | } |
14837 | 0 | len = read(fd, buf, sizeof(buf)); |
14838 | 0 | close(fd); |
14839 | 0 | if (len <= 0) { |
14840 | 0 | err = len ? -errno : -EINVAL; |
14841 | 0 | pr_warn("Failed to read cpu mask from %s: %s\n", fcpu, errstr(err)); |
14842 | 0 | return err; |
14843 | 0 | } |
14844 | 0 | if (len >= sizeof(buf)) { |
14845 | 0 | pr_warn("CPU mask is too big in file %s\n", fcpu); |
14846 | 0 | return -E2BIG; |
14847 | 0 | } |
14848 | 0 | buf[len] = '\0'; |
14849 | |
|
14850 | 0 | return parse_cpu_mask_str(buf, mask, mask_sz); |
14851 | 0 | } |
14852 | | |
14853 | | int libbpf_num_possible_cpus(void) |
14854 | 0 | { |
14855 | 0 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
14856 | 0 | static int cpus; |
14857 | 0 | int err, n, i, tmp_cpus; |
14858 | 0 | bool *mask; |
14859 | |
|
14860 | 0 | tmp_cpus = READ_ONCE(cpus); |
14861 | 0 | if (tmp_cpus > 0) |
14862 | 0 | return tmp_cpus; |
14863 | | |
14864 | 0 | err = parse_cpu_mask_file(fcpu, &mask, &n); |
14865 | 0 | if (err) |
14866 | 0 | return libbpf_err(err); |
14867 | | |
14868 | 0 | tmp_cpus = 0; |
14869 | 0 | for (i = 0; i < n; i++) { |
14870 | 0 | if (mask[i]) |
14871 | 0 | tmp_cpus++; |
14872 | 0 | } |
14873 | 0 | free(mask); |
14874 | |
|
14875 | 0 | WRITE_ONCE(cpus, tmp_cpus); |
14876 | 0 | return tmp_cpus; |
14877 | 0 | } |
14878 | | |
14879 | | static int populate_skeleton_maps(const struct bpf_object *obj, |
14880 | | struct bpf_map_skeleton *maps, |
14881 | | size_t map_cnt, size_t map_skel_sz) |
14882 | 0 | { |
14883 | 0 | int i; |
14884 | |
|
14885 | 0 | for (i = 0; i < map_cnt; i++) { |
14886 | 0 | struct bpf_map_skeleton *map_skel = (void *)maps + i * map_skel_sz; |
14887 | 0 | struct bpf_map **map = map_skel->map; |
14888 | 0 | const char *name = map_skel->name; |
14889 | 0 | void **mmaped = map_skel->mmaped; |
14890 | |
|
14891 | 0 | *map = bpf_object__find_map_by_name(obj, name); |
14892 | 0 | if (!*map) { |
14893 | 0 | pr_warn("failed to find skeleton map '%s'\n", name); |
14894 | 0 | return -ESRCH; |
14895 | 0 | } |
14896 | | |
14897 | | /* externs shouldn't be pre-setup from user code */ |
14898 | 0 | if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) |
14899 | 0 | *mmaped = (*map)->mmaped; |
14900 | 0 | } |
14901 | 0 | return 0; |
14902 | 0 | } |
14903 | | |
14904 | | static int populate_skeleton_progs(const struct bpf_object *obj, |
14905 | | struct bpf_prog_skeleton *progs, |
14906 | | size_t prog_cnt, size_t prog_skel_sz) |
14907 | 0 | { |
14908 | 0 | int i; |
14909 | |
|
14910 | 0 | for (i = 0; i < prog_cnt; i++) { |
14911 | 0 | struct bpf_prog_skeleton *prog_skel = (void *)progs + i * prog_skel_sz; |
14912 | 0 | struct bpf_program **prog = prog_skel->prog; |
14913 | 0 | const char *name = prog_skel->name; |
14914 | |
|
14915 | 0 | *prog = bpf_object__find_program_by_name(obj, name); |
14916 | 0 | if (!*prog) { |
14917 | 0 | pr_warn("failed to find skeleton program '%s'\n", name); |
14918 | 0 | return -ESRCH; |
14919 | 0 | } |
14920 | 0 | } |
14921 | 0 | return 0; |
14922 | 0 | } |
14923 | | |
14924 | | int bpf_object__open_skeleton(struct bpf_object_skeleton *s, |
14925 | | const struct bpf_object_open_opts *opts) |
14926 | 0 | { |
14927 | 0 | struct bpf_object *obj; |
14928 | 0 | int err; |
14929 | |
|
14930 | 0 | obj = bpf_object_open(NULL, s->data, s->data_sz, s->name, opts); |
14931 | 0 | if (IS_ERR(obj)) { |
14932 | 0 | err = PTR_ERR(obj); |
14933 | 0 | pr_warn("failed to initialize skeleton BPF object '%s': %s\n", |
14934 | 0 | s->name, errstr(err)); |
14935 | 0 | return libbpf_err(err); |
14936 | 0 | } |
14937 | | |
14938 | 0 | *s->obj = obj; |
14939 | 0 | err = populate_skeleton_maps(obj, s->maps, s->map_cnt, s->map_skel_sz); |
14940 | 0 | if (err) { |
14941 | 0 | pr_warn("failed to populate skeleton maps for '%s': %s\n", s->name, errstr(err)); |
14942 | 0 | return libbpf_err(err); |
14943 | 0 | } |
14944 | | |
14945 | 0 | err = populate_skeleton_progs(obj, s->progs, s->prog_cnt, s->prog_skel_sz); |
14946 | 0 | if (err) { |
14947 | 0 | pr_warn("failed to populate skeleton progs for '%s': %s\n", s->name, errstr(err)); |
14948 | 0 | return libbpf_err(err); |
14949 | 0 | } |
14950 | | |
14951 | 0 | return 0; |
14952 | 0 | } |
14953 | | |
14954 | | int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) |
14955 | 0 | { |
14956 | 0 | int err, len, var_idx, i; |
14957 | 0 | const char *var_name; |
14958 | 0 | const struct bpf_map *map; |
14959 | 0 | struct btf *btf; |
14960 | 0 | __u32 map_type_id; |
14961 | 0 | const struct btf_type *map_type, *var_type; |
14962 | 0 | const struct bpf_var_skeleton *var_skel; |
14963 | 0 | struct btf_var_secinfo *var; |
14964 | |
|
14965 | 0 | if (!s->obj) |
14966 | 0 | return libbpf_err(-EINVAL); |
14967 | | |
14968 | 0 | btf = bpf_object__btf(s->obj); |
14969 | 0 | if (!btf) { |
14970 | 0 | pr_warn("subskeletons require BTF at runtime (object %s)\n", |
14971 | 0 | bpf_object__name(s->obj)); |
14972 | 0 | return libbpf_err(-errno); |
14973 | 0 | } |
14974 | | |
14975 | 0 | err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt, s->map_skel_sz); |
14976 | 0 | if (err) { |
14977 | 0 | pr_warn("failed to populate subskeleton maps: %s\n", errstr(err)); |
14978 | 0 | return libbpf_err(err); |
14979 | 0 | } |
14980 | | |
14981 | 0 | err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt, s->prog_skel_sz); |
14982 | 0 | if (err) { |
14983 | 0 | pr_warn("failed to populate subskeleton maps: %s\n", errstr(err)); |
14984 | 0 | return libbpf_err(err); |
14985 | 0 | } |
14986 | | |
14987 | 0 | for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { |
14988 | 0 | var_skel = (void *)s->vars + var_idx * s->var_skel_sz; |
14989 | 0 | map = *var_skel->map; |
14990 | 0 | map_type_id = bpf_map__btf_value_type_id(map); |
14991 | 0 | map_type = btf__type_by_id(btf, map_type_id); |
14992 | |
|
14993 | 0 | if (!btf_is_datasec(map_type)) { |
14994 | 0 | pr_warn("type for map '%1$s' is not a datasec: %2$s\n", |
14995 | 0 | bpf_map__name(map), |
14996 | 0 | __btf_kind_str(btf_kind(map_type))); |
14997 | 0 | return libbpf_err(-EINVAL); |
14998 | 0 | } |
14999 | | |
15000 | 0 | len = btf_vlen(map_type); |
15001 | 0 | var = btf_var_secinfos(map_type); |
15002 | 0 | for (i = 0; i < len; i++, var++) { |
15003 | 0 | var_type = btf__type_by_id(btf, var->type); |
15004 | 0 | var_name = btf__name_by_offset(btf, var_type->name_off); |
15005 | 0 | if (strcmp(var_name, var_skel->name) == 0) { |
15006 | 0 | *var_skel->addr = map->mmaped + var->offset; |
15007 | 0 | break; |
15008 | 0 | } |
15009 | 0 | } |
15010 | 0 | } |
15011 | 0 | return 0; |
15012 | 0 | } |
15013 | | |
15014 | | void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) |
15015 | 0 | { |
15016 | 0 | if (!s) |
15017 | 0 | return; |
15018 | 0 | free(s->maps); |
15019 | 0 | free(s->progs); |
15020 | 0 | free(s->vars); |
15021 | 0 | free(s); |
15022 | 0 | } |
15023 | | |
15024 | | int bpf_object__load_skeleton(struct bpf_object_skeleton *s) |
15025 | 0 | { |
15026 | 0 | int i, err; |
15027 | |
|
15028 | 0 | err = bpf_object__load(*s->obj); |
15029 | 0 | if (err) { |
15030 | 0 | pr_warn("failed to load BPF skeleton '%s': %s\n", s->name, errstr(err)); |
15031 | 0 | return libbpf_err(err); |
15032 | 0 | } |
15033 | | |
15034 | 0 | for (i = 0; i < s->map_cnt; i++) { |
15035 | 0 | struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; |
15036 | 0 | struct bpf_map *map = *map_skel->map; |
15037 | |
|
15038 | 0 | if (!map_skel->mmaped) |
15039 | 0 | continue; |
15040 | | |
15041 | 0 | if (map->def.type == BPF_MAP_TYPE_ARENA) |
15042 | 0 | *map_skel->mmaped = map->mmaped + map->obj->arena_data_off; |
15043 | 0 | else |
15044 | 0 | *map_skel->mmaped = map->mmaped; |
15045 | 0 | } |
15046 | |
|
15047 | 0 | return 0; |
15048 | 0 | } |
15049 | | |
15050 | | int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) |
15051 | 0 | { |
15052 | 0 | int i, err; |
15053 | |
|
15054 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
15055 | 0 | struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz; |
15056 | 0 | struct bpf_program *prog = *prog_skel->prog; |
15057 | 0 | struct bpf_link **link = prog_skel->link; |
15058 | |
|
15059 | 0 | if (!prog->autoload || !prog->autoattach) |
15060 | 0 | continue; |
15061 | | |
15062 | | /* auto-attaching not supported for this program */ |
15063 | 0 | if (!prog->sec_def || !prog->sec_def->prog_attach_fn) |
15064 | 0 | continue; |
15065 | | |
15066 | | /* if user already set the link manually, don't attempt auto-attach */ |
15067 | 0 | if (*link) |
15068 | 0 | continue; |
15069 | | |
15070 | 0 | err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link); |
15071 | 0 | if (err) { |
15072 | 0 | pr_warn("prog '%s': failed to auto-attach: %s\n", |
15073 | 0 | bpf_program__name(prog), errstr(err)); |
15074 | 0 | return libbpf_err(err); |
15075 | 0 | } |
15076 | | |
15077 | | /* It's possible that for some SEC() definitions auto-attach |
15078 | | * is supported in some cases (e.g., if definition completely |
15079 | | * specifies target information), but is not in other cases. |
15080 | | * SEC("uprobe") is one such case. If user specified target |
15081 | | * binary and function name, such BPF program can be |
15082 | | * auto-attached. But if not, it shouldn't trigger skeleton's |
15083 | | * attach to fail. It should just be skipped. |
15084 | | * attach_fn signals such case with returning 0 (no error) and |
15085 | | * setting link to NULL. |
15086 | | */ |
15087 | 0 | } |
15088 | | |
15089 | | |
15090 | 0 | for (i = 0; i < s->map_cnt; i++) { |
15091 | 0 | struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; |
15092 | 0 | struct bpf_map *map = *map_skel->map; |
15093 | 0 | struct bpf_link **link; |
15094 | |
|
15095 | 0 | if (!map->autocreate || !map->autoattach) |
15096 | 0 | continue; |
15097 | | |
15098 | | /* only struct_ops maps can be attached */ |
15099 | 0 | if (!bpf_map__is_struct_ops(map)) |
15100 | 0 | continue; |
15101 | | |
15102 | | /* skeleton is created with earlier version of bpftool, notify user */ |
15103 | 0 | if (s->map_skel_sz < offsetofend(struct bpf_map_skeleton, link)) { |
15104 | 0 | pr_warn("map '%s': BPF skeleton version is old, skipping map auto-attachment...\n", |
15105 | 0 | bpf_map__name(map)); |
15106 | 0 | continue; |
15107 | 0 | } |
15108 | | |
15109 | 0 | link = map_skel->link; |
15110 | 0 | if (!link) { |
15111 | 0 | pr_warn("map '%s': BPF map skeleton link is uninitialized\n", |
15112 | 0 | bpf_map__name(map)); |
15113 | 0 | continue; |
15114 | 0 | } |
15115 | | |
15116 | 0 | if (*link) |
15117 | 0 | continue; |
15118 | | |
15119 | 0 | *link = bpf_map__attach_struct_ops(map); |
15120 | 0 | if (!*link) { |
15121 | 0 | err = -errno; |
15122 | 0 | pr_warn("map '%s': failed to auto-attach: %s\n", |
15123 | 0 | bpf_map__name(map), errstr(err)); |
15124 | 0 | return libbpf_err(err); |
15125 | 0 | } |
15126 | 0 | } |
15127 | | |
15128 | 0 | return 0; |
15129 | 0 | } |
15130 | | |
15131 | | void bpf_object__detach_skeleton(struct bpf_object_skeleton *s) |
15132 | 0 | { |
15133 | 0 | int i; |
15134 | |
|
15135 | 0 | for (i = 0; i < s->prog_cnt; i++) { |
15136 | 0 | struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz; |
15137 | 0 | struct bpf_link **link = prog_skel->link; |
15138 | |
|
15139 | 0 | bpf_link__destroy(*link); |
15140 | 0 | *link = NULL; |
15141 | 0 | } |
15142 | |
|
15143 | 0 | if (s->map_skel_sz < sizeof(struct bpf_map_skeleton)) |
15144 | 0 | return; |
15145 | | |
15146 | 0 | for (i = 0; i < s->map_cnt; i++) { |
15147 | 0 | struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz; |
15148 | 0 | struct bpf_link **link = map_skel->link; |
15149 | |
|
15150 | 0 | if (link) { |
15151 | 0 | bpf_link__destroy(*link); |
15152 | 0 | *link = NULL; |
15153 | 0 | } |
15154 | 0 | } |
15155 | 0 | } |
15156 | | |
15157 | | void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) |
15158 | 0 | { |
15159 | 0 | if (!s) |
15160 | 0 | return; |
15161 | | |
15162 | 0 | bpf_object__detach_skeleton(s); |
15163 | 0 | if (s->obj) |
15164 | 0 | bpf_object__close(*s->obj); |
15165 | 0 | free(s->maps); |
15166 | 0 | free(s->progs); |
15167 | 0 | free(s); |
15168 | 0 | } |