xref: /openbmc/linux/tools/lib/bpf/bpf.c (revision 2d68bb26)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 /*
4  * common eBPF ELF 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  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation;
13  * version 2.1 of the License (not later!)
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this program; if not,  see <http://www.gnu.org/licenses>
22  */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include <memory.h>
27 #include <unistd.h>
28 #include <asm/unistd.h>
29 #include <errno.h>
30 #include <linux/bpf.h>
31 #include "bpf.h"
32 #include "libbpf.h"
33 #include "libbpf_internal.h"
34 
35 /*
36  * When building perf, unistd.h is overridden. __NR_bpf is
37  * required to be defined explicitly.
38  */
39 #ifndef __NR_bpf
40 # if defined(__i386__)
41 #  define __NR_bpf 357
42 # elif defined(__x86_64__)
43 #  define __NR_bpf 321
44 # elif defined(__aarch64__)
45 #  define __NR_bpf 280
46 # elif defined(__sparc__)
47 #  define __NR_bpf 349
48 # elif defined(__s390__)
49 #  define __NR_bpf 351
50 # elif defined(__arc__)
51 #  define __NR_bpf 280
52 # else
53 #  error __NR_bpf not defined. libbpf does not support your arch.
54 # endif
55 #endif
56 
57 static inline __u64 ptr_to_u64(const void *ptr)
58 {
59 	return (__u64) (unsigned long) ptr;
60 }
61 
62 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
63 			  unsigned int size)
64 {
65 	return syscall(__NR_bpf, cmd, attr, size);
66 }
67 
68 static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
69 {
70 	int fd;
71 
72 	do {
73 		fd = sys_bpf(BPF_PROG_LOAD, attr, size);
74 	} while (fd < 0 && errno == EAGAIN);
75 
76 	return fd;
77 }
78 
79 int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
80 {
81 	union bpf_attr attr;
82 
83 	memset(&attr, '\0', sizeof(attr));
84 
85 	attr.map_type = create_attr->map_type;
86 	attr.key_size = create_attr->key_size;
87 	attr.value_size = create_attr->value_size;
88 	attr.max_entries = create_attr->max_entries;
89 	attr.map_flags = create_attr->map_flags;
90 	if (create_attr->name)
91 		memcpy(attr.map_name, create_attr->name,
92 		       min(strlen(create_attr->name), BPF_OBJ_NAME_LEN - 1));
93 	attr.numa_node = create_attr->numa_node;
94 	attr.btf_fd = create_attr->btf_fd;
95 	attr.btf_key_type_id = create_attr->btf_key_type_id;
96 	attr.btf_value_type_id = create_attr->btf_value_type_id;
97 	attr.map_ifindex = create_attr->map_ifindex;
98 	attr.inner_map_fd = create_attr->inner_map_fd;
99 
100 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
101 }
102 
103 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
104 			int key_size, int value_size, int max_entries,
105 			__u32 map_flags, int node)
106 {
107 	struct bpf_create_map_attr map_attr = {};
108 
109 	map_attr.name = name;
110 	map_attr.map_type = map_type;
111 	map_attr.map_flags = map_flags;
112 	map_attr.key_size = key_size;
113 	map_attr.value_size = value_size;
114 	map_attr.max_entries = max_entries;
115 	if (node >= 0) {
116 		map_attr.numa_node = node;
117 		map_attr.map_flags |= BPF_F_NUMA_NODE;
118 	}
119 
120 	return bpf_create_map_xattr(&map_attr);
121 }
122 
123 int bpf_create_map(enum bpf_map_type map_type, int key_size,
124 		   int value_size, int max_entries, __u32 map_flags)
125 {
126 	struct bpf_create_map_attr map_attr = {};
127 
128 	map_attr.map_type = map_type;
129 	map_attr.map_flags = map_flags;
130 	map_attr.key_size = key_size;
131 	map_attr.value_size = value_size;
132 	map_attr.max_entries = max_entries;
133 
134 	return bpf_create_map_xattr(&map_attr);
135 }
136 
137 int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
138 			int key_size, int value_size, int max_entries,
139 			__u32 map_flags)
140 {
141 	struct bpf_create_map_attr map_attr = {};
142 
143 	map_attr.name = name;
144 	map_attr.map_type = map_type;
145 	map_attr.map_flags = map_flags;
146 	map_attr.key_size = key_size;
147 	map_attr.value_size = value_size;
148 	map_attr.max_entries = max_entries;
149 
150 	return bpf_create_map_xattr(&map_attr);
151 }
152 
153 int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
154 			       int key_size, int inner_map_fd, int max_entries,
155 			       __u32 map_flags, int node)
156 {
157 	union bpf_attr attr;
158 
159 	memset(&attr, '\0', sizeof(attr));
160 
161 	attr.map_type = map_type;
162 	attr.key_size = key_size;
163 	attr.value_size = 4;
164 	attr.inner_map_fd = inner_map_fd;
165 	attr.max_entries = max_entries;
166 	attr.map_flags = map_flags;
167 	if (name)
168 		memcpy(attr.map_name, name,
169 		       min(strlen(name), BPF_OBJ_NAME_LEN - 1));
170 
171 	if (node >= 0) {
172 		attr.map_flags |= BPF_F_NUMA_NODE;
173 		attr.numa_node = node;
174 	}
175 
176 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
177 }
178 
179 int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
180 			  int key_size, int inner_map_fd, int max_entries,
181 			  __u32 map_flags)
182 {
183 	return bpf_create_map_in_map_node(map_type, name, key_size,
184 					  inner_map_fd, max_entries, map_flags,
185 					  -1);
186 }
187 
188 static void *
189 alloc_zero_tailing_info(const void *orecord, __u32 cnt,
190 			__u32 actual_rec_size, __u32 expected_rec_size)
191 {
192 	__u64 info_len = (__u64)actual_rec_size * cnt;
193 	void *info, *nrecord;
194 	int i;
195 
196 	info = malloc(info_len);
197 	if (!info)
198 		return NULL;
199 
200 	/* zero out bytes kernel does not understand */
201 	nrecord = info;
202 	for (i = 0; i < cnt; i++) {
203 		memcpy(nrecord, orecord, expected_rec_size);
204 		memset(nrecord + expected_rec_size, 0,
205 		       actual_rec_size - expected_rec_size);
206 		orecord += actual_rec_size;
207 		nrecord += actual_rec_size;
208 	}
209 
210 	return info;
211 }
212 
213 int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
214 			   char *log_buf, size_t log_buf_sz)
215 {
216 	void *finfo = NULL, *linfo = NULL;
217 	union bpf_attr attr;
218 	__u32 log_level;
219 	int fd;
220 
221 	if (!load_attr || !log_buf != !log_buf_sz)
222 		return -EINVAL;
223 
224 	log_level = load_attr->log_level;
225 	if (log_level > (4 | 2 | 1) || (log_level && !log_buf))
226 		return -EINVAL;
227 
228 	memset(&attr, 0, sizeof(attr));
229 	attr.prog_type = load_attr->prog_type;
230 	attr.expected_attach_type = load_attr->expected_attach_type;
231 	if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
232 		attr.attach_btf_id = load_attr->attach_btf_id;
233 		attr.attach_prog_fd = load_attr->attach_prog_fd;
234 	} else {
235 		attr.prog_ifindex = load_attr->prog_ifindex;
236 		attr.kern_version = load_attr->kern_version;
237 	}
238 	attr.insn_cnt = (__u32)load_attr->insns_cnt;
239 	attr.insns = ptr_to_u64(load_attr->insns);
240 	attr.license = ptr_to_u64(load_attr->license);
241 
242 	attr.log_level = log_level;
243 	if (log_level) {
244 		attr.log_buf = ptr_to_u64(log_buf);
245 		attr.log_size = log_buf_sz;
246 	} else {
247 		attr.log_buf = ptr_to_u64(NULL);
248 		attr.log_size = 0;
249 	}
250 
251 	attr.prog_btf_fd = load_attr->prog_btf_fd;
252 	attr.func_info_rec_size = load_attr->func_info_rec_size;
253 	attr.func_info_cnt = load_attr->func_info_cnt;
254 	attr.func_info = ptr_to_u64(load_attr->func_info);
255 	attr.line_info_rec_size = load_attr->line_info_rec_size;
256 	attr.line_info_cnt = load_attr->line_info_cnt;
257 	attr.line_info = ptr_to_u64(load_attr->line_info);
258 	if (load_attr->name)
259 		memcpy(attr.prog_name, load_attr->name,
260 		       min(strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1));
261 	attr.prog_flags = load_attr->prog_flags;
262 
263 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
264 	if (fd >= 0)
265 		return fd;
266 
267 	/* After bpf_prog_load, the kernel may modify certain attributes
268 	 * to give user space a hint how to deal with loading failure.
269 	 * Check to see whether we can make some changes and load again.
270 	 */
271 	while (errno == E2BIG && (!finfo || !linfo)) {
272 		if (!finfo && attr.func_info_cnt &&
273 		    attr.func_info_rec_size < load_attr->func_info_rec_size) {
274 			/* try with corrected func info records */
275 			finfo = alloc_zero_tailing_info(load_attr->func_info,
276 							load_attr->func_info_cnt,
277 							load_attr->func_info_rec_size,
278 							attr.func_info_rec_size);
279 			if (!finfo)
280 				goto done;
281 
282 			attr.func_info = ptr_to_u64(finfo);
283 			attr.func_info_rec_size = load_attr->func_info_rec_size;
284 		} else if (!linfo && attr.line_info_cnt &&
285 			   attr.line_info_rec_size <
286 			   load_attr->line_info_rec_size) {
287 			linfo = alloc_zero_tailing_info(load_attr->line_info,
288 							load_attr->line_info_cnt,
289 							load_attr->line_info_rec_size,
290 							attr.line_info_rec_size);
291 			if (!linfo)
292 				goto done;
293 
294 			attr.line_info = ptr_to_u64(linfo);
295 			attr.line_info_rec_size = load_attr->line_info_rec_size;
296 		} else {
297 			break;
298 		}
299 
300 		fd = sys_bpf_prog_load(&attr, sizeof(attr));
301 
302 		if (fd >= 0)
303 			goto done;
304 	}
305 
306 	if (log_level || !log_buf)
307 		goto done;
308 
309 	/* Try again with log */
310 	attr.log_buf = ptr_to_u64(log_buf);
311 	attr.log_size = log_buf_sz;
312 	attr.log_level = 1;
313 	log_buf[0] = 0;
314 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
315 done:
316 	free(finfo);
317 	free(linfo);
318 	return fd;
319 }
320 
321 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
322 		     size_t insns_cnt, const char *license,
323 		     __u32 kern_version, char *log_buf,
324 		     size_t log_buf_sz)
325 {
326 	struct bpf_load_program_attr load_attr;
327 
328 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
329 	load_attr.prog_type = type;
330 	load_attr.expected_attach_type = 0;
331 	load_attr.name = NULL;
332 	load_attr.insns = insns;
333 	load_attr.insns_cnt = insns_cnt;
334 	load_attr.license = license;
335 	load_attr.kern_version = kern_version;
336 
337 	return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
338 }
339 
340 int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
341 		       size_t insns_cnt, __u32 prog_flags, const char *license,
342 		       __u32 kern_version, char *log_buf, size_t log_buf_sz,
343 		       int log_level)
344 {
345 	union bpf_attr attr;
346 
347 	memset(&attr, 0, sizeof(attr));
348 	attr.prog_type = type;
349 	attr.insn_cnt = (__u32)insns_cnt;
350 	attr.insns = ptr_to_u64(insns);
351 	attr.license = ptr_to_u64(license);
352 	attr.log_buf = ptr_to_u64(log_buf);
353 	attr.log_size = log_buf_sz;
354 	attr.log_level = log_level;
355 	log_buf[0] = 0;
356 	attr.kern_version = kern_version;
357 	attr.prog_flags = prog_flags;
358 
359 	return sys_bpf_prog_load(&attr, sizeof(attr));
360 }
361 
362 int bpf_map_update_elem(int fd, const void *key, const void *value,
363 			__u64 flags)
364 {
365 	union bpf_attr attr;
366 
367 	memset(&attr, 0, sizeof(attr));
368 	attr.map_fd = fd;
369 	attr.key = ptr_to_u64(key);
370 	attr.value = ptr_to_u64(value);
371 	attr.flags = flags;
372 
373 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
374 }
375 
376 int bpf_map_lookup_elem(int fd, const void *key, void *value)
377 {
378 	union bpf_attr attr;
379 
380 	memset(&attr, 0, sizeof(attr));
381 	attr.map_fd = fd;
382 	attr.key = ptr_to_u64(key);
383 	attr.value = ptr_to_u64(value);
384 
385 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
386 }
387 
388 int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
389 {
390 	union bpf_attr attr;
391 
392 	memset(&attr, 0, sizeof(attr));
393 	attr.map_fd = fd;
394 	attr.key = ptr_to_u64(key);
395 	attr.value = ptr_to_u64(value);
396 	attr.flags = flags;
397 
398 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
399 }
400 
401 int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
402 {
403 	union bpf_attr attr;
404 
405 	memset(&attr, 0, sizeof(attr));
406 	attr.map_fd = fd;
407 	attr.key = ptr_to_u64(key);
408 	attr.value = ptr_to_u64(value);
409 
410 	return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
411 }
412 
413 int bpf_map_delete_elem(int fd, const void *key)
414 {
415 	union bpf_attr attr;
416 
417 	memset(&attr, 0, sizeof(attr));
418 	attr.map_fd = fd;
419 	attr.key = ptr_to_u64(key);
420 
421 	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
422 }
423 
424 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
425 {
426 	union bpf_attr attr;
427 
428 	memset(&attr, 0, sizeof(attr));
429 	attr.map_fd = fd;
430 	attr.key = ptr_to_u64(key);
431 	attr.next_key = ptr_to_u64(next_key);
432 
433 	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
434 }
435 
436 int bpf_map_freeze(int fd)
437 {
438 	union bpf_attr attr;
439 
440 	memset(&attr, 0, sizeof(attr));
441 	attr.map_fd = fd;
442 
443 	return sys_bpf(BPF_MAP_FREEZE, &attr, sizeof(attr));
444 }
445 
446 int bpf_obj_pin(int fd, const char *pathname)
447 {
448 	union bpf_attr attr;
449 
450 	memset(&attr, 0, sizeof(attr));
451 	attr.pathname = ptr_to_u64((void *)pathname);
452 	attr.bpf_fd = fd;
453 
454 	return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
455 }
456 
457 int bpf_obj_get(const char *pathname)
458 {
459 	union bpf_attr attr;
460 
461 	memset(&attr, 0, sizeof(attr));
462 	attr.pathname = ptr_to_u64((void *)pathname);
463 
464 	return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
465 }
466 
467 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
468 		    unsigned int flags)
469 {
470 	union bpf_attr attr;
471 
472 	memset(&attr, 0, sizeof(attr));
473 	attr.target_fd	   = target_fd;
474 	attr.attach_bpf_fd = prog_fd;
475 	attr.attach_type   = type;
476 	attr.attach_flags  = flags;
477 
478 	return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
479 }
480 
481 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
482 {
483 	union bpf_attr attr;
484 
485 	memset(&attr, 0, sizeof(attr));
486 	attr.target_fd	 = target_fd;
487 	attr.attach_type = type;
488 
489 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
490 }
491 
492 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
493 {
494 	union bpf_attr attr;
495 
496 	memset(&attr, 0, sizeof(attr));
497 	attr.target_fd	 = target_fd;
498 	attr.attach_bpf_fd = prog_fd;
499 	attr.attach_type = type;
500 
501 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
502 }
503 
504 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
505 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
506 {
507 	union bpf_attr attr;
508 	int ret;
509 
510 	memset(&attr, 0, sizeof(attr));
511 	attr.query.target_fd	= target_fd;
512 	attr.query.attach_type	= type;
513 	attr.query.query_flags	= query_flags;
514 	attr.query.prog_cnt	= *prog_cnt;
515 	attr.query.prog_ids	= ptr_to_u64(prog_ids);
516 
517 	ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
518 	if (attach_flags)
519 		*attach_flags = attr.query.attach_flags;
520 	*prog_cnt = attr.query.prog_cnt;
521 	return ret;
522 }
523 
524 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
525 		      void *data_out, __u32 *size_out, __u32 *retval,
526 		      __u32 *duration)
527 {
528 	union bpf_attr attr;
529 	int ret;
530 
531 	memset(&attr, 0, sizeof(attr));
532 	attr.test.prog_fd = prog_fd;
533 	attr.test.data_in = ptr_to_u64(data);
534 	attr.test.data_out = ptr_to_u64(data_out);
535 	attr.test.data_size_in = size;
536 	attr.test.repeat = repeat;
537 
538 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
539 	if (size_out)
540 		*size_out = attr.test.data_size_out;
541 	if (retval)
542 		*retval = attr.test.retval;
543 	if (duration)
544 		*duration = attr.test.duration;
545 	return ret;
546 }
547 
548 int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
549 {
550 	union bpf_attr attr;
551 	int ret;
552 
553 	if (!test_attr->data_out && test_attr->data_size_out > 0)
554 		return -EINVAL;
555 
556 	memset(&attr, 0, sizeof(attr));
557 	attr.test.prog_fd = test_attr->prog_fd;
558 	attr.test.data_in = ptr_to_u64(test_attr->data_in);
559 	attr.test.data_out = ptr_to_u64(test_attr->data_out);
560 	attr.test.data_size_in = test_attr->data_size_in;
561 	attr.test.data_size_out = test_attr->data_size_out;
562 	attr.test.ctx_in = ptr_to_u64(test_attr->ctx_in);
563 	attr.test.ctx_out = ptr_to_u64(test_attr->ctx_out);
564 	attr.test.ctx_size_in = test_attr->ctx_size_in;
565 	attr.test.ctx_size_out = test_attr->ctx_size_out;
566 	attr.test.repeat = test_attr->repeat;
567 
568 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
569 	test_attr->data_size_out = attr.test.data_size_out;
570 	test_attr->ctx_size_out = attr.test.ctx_size_out;
571 	test_attr->retval = attr.test.retval;
572 	test_attr->duration = attr.test.duration;
573 	return ret;
574 }
575 
576 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
577 {
578 	union bpf_attr attr;
579 	int err;
580 
581 	memset(&attr, 0, sizeof(attr));
582 	attr.start_id = start_id;
583 
584 	err = sys_bpf(cmd, &attr, sizeof(attr));
585 	if (!err)
586 		*next_id = attr.next_id;
587 
588 	return err;
589 }
590 
591 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
592 {
593 	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
594 }
595 
596 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
597 {
598 	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
599 }
600 
601 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
602 {
603 	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
604 }
605 
606 int bpf_prog_get_fd_by_id(__u32 id)
607 {
608 	union bpf_attr attr;
609 
610 	memset(&attr, 0, sizeof(attr));
611 	attr.prog_id = id;
612 
613 	return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
614 }
615 
616 int bpf_map_get_fd_by_id(__u32 id)
617 {
618 	union bpf_attr attr;
619 
620 	memset(&attr, 0, sizeof(attr));
621 	attr.map_id = id;
622 
623 	return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
624 }
625 
626 int bpf_btf_get_fd_by_id(__u32 id)
627 {
628 	union bpf_attr attr;
629 
630 	memset(&attr, 0, sizeof(attr));
631 	attr.btf_id = id;
632 
633 	return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
634 }
635 
636 int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
637 {
638 	union bpf_attr attr;
639 	int err;
640 
641 	memset(&attr, 0, sizeof(attr));
642 	attr.info.bpf_fd = prog_fd;
643 	attr.info.info_len = *info_len;
644 	attr.info.info = ptr_to_u64(info);
645 
646 	err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
647 	if (!err)
648 		*info_len = attr.info.info_len;
649 
650 	return err;
651 }
652 
653 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
654 {
655 	union bpf_attr attr;
656 
657 	memset(&attr, 0, sizeof(attr));
658 	attr.raw_tracepoint.name = ptr_to_u64(name);
659 	attr.raw_tracepoint.prog_fd = prog_fd;
660 
661 	return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
662 }
663 
664 int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
665 		 bool do_log)
666 {
667 	union bpf_attr attr = {};
668 	int fd;
669 
670 	attr.btf = ptr_to_u64(btf);
671 	attr.btf_size = btf_size;
672 
673 retry:
674 	if (do_log && log_buf && log_buf_size) {
675 		attr.btf_log_level = 1;
676 		attr.btf_log_size = log_buf_size;
677 		attr.btf_log_buf = ptr_to_u64(log_buf);
678 	}
679 
680 	fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
681 	if (fd == -1 && !do_log && log_buf && log_buf_size) {
682 		do_log = true;
683 		goto retry;
684 	}
685 
686 	return fd;
687 }
688 
689 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
690 		      __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
691 		      __u64 *probe_addr)
692 {
693 	union bpf_attr attr = {};
694 	int err;
695 
696 	attr.task_fd_query.pid = pid;
697 	attr.task_fd_query.fd = fd;
698 	attr.task_fd_query.flags = flags;
699 	attr.task_fd_query.buf = ptr_to_u64(buf);
700 	attr.task_fd_query.buf_len = *buf_len;
701 
702 	err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
703 	*buf_len = attr.task_fd_query.buf_len;
704 	*prog_id = attr.task_fd_query.prog_id;
705 	*fd_type = attr.task_fd_query.fd_type;
706 	*probe_offset = attr.task_fd_query.probe_offset;
707 	*probe_addr = attr.task_fd_query.probe_addr;
708 
709 	return err;
710 }
711