xref: /linux/tools/perf/util/bpf-event.c (revision 7effbd18e496a954540fb6319db0d87d28a70496)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <bpf/bpf.h>
5 #include <bpf/btf.h>
6 #include <bpf/libbpf.h>
7 #include <linux/btf.h>
8 #include <linux/err.h>
9 #include <linux/string.h>
10 #include <internal/lib.h>
11 #include <symbol/kallsyms.h>
12 #include "bpf-event.h"
13 #include "bpf-utils.h"
14 #include "debug.h"
15 #include "dso.h"
16 #include "symbol.h"
17 #include "machine.h"
18 #include "env.h"
19 #include "session.h"
20 #include "map.h"
21 #include "evlist.h"
22 #include "record.h"
23 #include "util/synthetic-events.h"
24 
25 static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
26 {
27 	int ret = 0;
28 	size_t i;
29 
30 	for (i = 0; i < len; i++)
31 		ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
32 	return ret;
33 }
34 
35 static int machine__process_bpf_event_load(struct machine *machine,
36 					   union perf_event *event,
37 					   struct perf_sample *sample __maybe_unused)
38 {
39 	struct bpf_prog_info_node *info_node;
40 	struct perf_env *env = machine->env;
41 	struct perf_bpil *info_linear;
42 	int id = event->bpf.id;
43 	unsigned int i;
44 
45 	/* perf-record, no need to handle bpf-event */
46 	if (env == NULL)
47 		return 0;
48 
49 	info_node = perf_env__find_bpf_prog_info(env, id);
50 	if (!info_node)
51 		return 0;
52 	info_linear = info_node->info_linear;
53 
54 	for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
55 		u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
56 		u64 addr = addrs[i];
57 		struct map *map = maps__find(machine__kernel_maps(machine), addr);
58 
59 		if (map) {
60 			map->dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
61 			map->dso->bpf_prog.id = id;
62 			map->dso->bpf_prog.sub_id = i;
63 			map->dso->bpf_prog.env = env;
64 		}
65 	}
66 	return 0;
67 }
68 
69 int machine__process_bpf(struct machine *machine, union perf_event *event,
70 			 struct perf_sample *sample)
71 {
72 	if (dump_trace)
73 		perf_event__fprintf_bpf(event, stdout);
74 
75 	switch (event->bpf.type) {
76 	case PERF_BPF_EVENT_PROG_LOAD:
77 		return machine__process_bpf_event_load(machine, event, sample);
78 
79 	case PERF_BPF_EVENT_PROG_UNLOAD:
80 		/*
81 		 * Do not free bpf_prog_info and btf of the program here,
82 		 * as annotation still need them. They will be freed at
83 		 * the end of the session.
84 		 */
85 		break;
86 	default:
87 		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
88 		break;
89 	}
90 	return 0;
91 }
92 
93 static int perf_env__fetch_btf(struct perf_env *env,
94 			       u32 btf_id,
95 			       struct btf *btf)
96 {
97 	struct btf_node *node;
98 	u32 data_size;
99 	const void *data;
100 
101 	data = btf__raw_data(btf, &data_size);
102 
103 	node = malloc(data_size + sizeof(struct btf_node));
104 	if (!node)
105 		return -1;
106 
107 	node->id = btf_id;
108 	node->data_size = data_size;
109 	memcpy(node->data, data, data_size);
110 
111 	if (!perf_env__insert_btf(env, node)) {
112 		/* Insertion failed because of a duplicate. */
113 		free(node);
114 		return -1;
115 	}
116 	return 0;
117 }
118 
119 static int synthesize_bpf_prog_name(char *buf, int size,
120 				    struct bpf_prog_info *info,
121 				    struct btf *btf,
122 				    u32 sub_id)
123 {
124 	u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
125 	void *func_infos = (void *)(uintptr_t)(info->func_info);
126 	u32 sub_prog_cnt = info->nr_jited_ksyms;
127 	const struct bpf_func_info *finfo;
128 	const char *short_name = NULL;
129 	const struct btf_type *t;
130 	int name_len;
131 
132 	name_len = snprintf(buf, size, "bpf_prog_");
133 	name_len += snprintf_hex(buf + name_len, size - name_len,
134 				 prog_tags[sub_id], BPF_TAG_SIZE);
135 	if (btf) {
136 		finfo = func_infos + sub_id * info->func_info_rec_size;
137 		t = btf__type_by_id(btf, finfo->type_id);
138 		short_name = btf__name_by_offset(btf, t->name_off);
139 	} else if (sub_id == 0 && sub_prog_cnt == 1) {
140 		/* no subprog */
141 		if (info->name[0])
142 			short_name = info->name;
143 	} else
144 		short_name = "F";
145 	if (short_name)
146 		name_len += snprintf(buf + name_len, size - name_len,
147 				     "_%s", short_name);
148 	return name_len;
149 }
150 
151 /*
152  * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
153  * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
154  * one PERF_RECORD_KSYMBOL is generated for each sub program.
155  *
156  * Returns:
157  *    0 for success;
158  *   -1 for failures;
159  *   -2 for lack of kernel support.
160  */
161 static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
162 					       perf_event__handler_t process,
163 					       struct machine *machine,
164 					       int fd,
165 					       union perf_event *event,
166 					       struct record_opts *opts)
167 {
168 	struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;
169 	struct perf_record_bpf_event *bpf_event = &event->bpf;
170 	struct perf_tool *tool = session->tool;
171 	struct bpf_prog_info_node *info_node;
172 	struct perf_bpil *info_linear;
173 	struct bpf_prog_info *info;
174 	struct btf *btf = NULL;
175 	struct perf_env *env;
176 	u32 sub_prog_cnt, i;
177 	int err = 0;
178 	u64 arrays;
179 
180 	/*
181 	 * for perf-record and perf-report use header.env;
182 	 * otherwise, use global perf_env.
183 	 */
184 	env = session->data ? &session->header.env : &perf_env;
185 
186 	arrays = 1UL << PERF_BPIL_JITED_KSYMS;
187 	arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
188 	arrays |= 1UL << PERF_BPIL_FUNC_INFO;
189 	arrays |= 1UL << PERF_BPIL_PROG_TAGS;
190 	arrays |= 1UL << PERF_BPIL_JITED_INSNS;
191 	arrays |= 1UL << PERF_BPIL_LINE_INFO;
192 	arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
193 
194 	info_linear = get_bpf_prog_info_linear(fd, arrays);
195 	if (IS_ERR_OR_NULL(info_linear)) {
196 		info_linear = NULL;
197 		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
198 		return -1;
199 	}
200 
201 	if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
202 		free(info_linear);
203 		pr_debug("%s: the kernel is too old, aborting\n", __func__);
204 		return -2;
205 	}
206 
207 	info = &info_linear->info;
208 	if (!info->jited_ksyms) {
209 		free(info_linear);
210 		return -1;
211 	}
212 
213 	/* number of ksyms, func_lengths, and tags should match */
214 	sub_prog_cnt = info->nr_jited_ksyms;
215 	if (sub_prog_cnt != info->nr_prog_tags ||
216 	    sub_prog_cnt != info->nr_jited_func_lens) {
217 		free(info_linear);
218 		return -1;
219 	}
220 
221 	/* check BTF func info support */
222 	if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
223 		/* btf func info number should be same as sub_prog_cnt */
224 		if (sub_prog_cnt != info->nr_func_info) {
225 			pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
226 			free(info_linear);
227 			return -1;
228 		}
229 		btf = btf__load_from_kernel_by_id(info->btf_id);
230 		if (libbpf_get_error(btf)) {
231 			pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
232 			err = -1;
233 			goto out;
234 		}
235 		perf_env__fetch_btf(env, info->btf_id, btf);
236 	}
237 
238 	/* Synthesize PERF_RECORD_KSYMBOL */
239 	for (i = 0; i < sub_prog_cnt; i++) {
240 		__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
241 		__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
242 		int name_len;
243 
244 		*ksymbol_event = (struct perf_record_ksymbol) {
245 			.header = {
246 				.type = PERF_RECORD_KSYMBOL,
247 				.size = offsetof(struct perf_record_ksymbol, name),
248 			},
249 			.addr = prog_addrs[i],
250 			.len = prog_lens[i],
251 			.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
252 			.flags = 0,
253 		};
254 
255 		name_len = synthesize_bpf_prog_name(ksymbol_event->name,
256 						    KSYM_NAME_LEN, info, btf, i);
257 		ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
258 							 sizeof(u64));
259 
260 		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
261 		event->header.size += machine->id_hdr_size;
262 		err = perf_tool__process_synth_event(tool, event,
263 						     machine, process);
264 	}
265 
266 	if (!opts->no_bpf_event) {
267 		/* Synthesize PERF_RECORD_BPF_EVENT */
268 		*bpf_event = (struct perf_record_bpf_event) {
269 			.header = {
270 				.type = PERF_RECORD_BPF_EVENT,
271 				.size = sizeof(struct perf_record_bpf_event),
272 			},
273 			.type = PERF_BPF_EVENT_PROG_LOAD,
274 			.flags = 0,
275 			.id = info->id,
276 		};
277 		memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
278 		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
279 		event->header.size += machine->id_hdr_size;
280 
281 		/* save bpf_prog_info to env */
282 		info_node = malloc(sizeof(struct bpf_prog_info_node));
283 		if (!info_node) {
284 			err = -1;
285 			goto out;
286 		}
287 
288 		info_node->info_linear = info_linear;
289 		perf_env__insert_bpf_prog_info(env, info_node);
290 		info_linear = NULL;
291 
292 		/*
293 		 * process after saving bpf_prog_info to env, so that
294 		 * required information is ready for look up
295 		 */
296 		err = perf_tool__process_synth_event(tool, event,
297 						     machine, process);
298 	}
299 
300 out:
301 	free(info_linear);
302 	btf__free(btf);
303 	return err ? -1 : 0;
304 }
305 
306 struct kallsyms_parse {
307 	union perf_event	*event;
308 	perf_event__handler_t	 process;
309 	struct machine		*machine;
310 	struct perf_tool	*tool;
311 };
312 
313 static int
314 process_bpf_image(char *name, u64 addr, struct kallsyms_parse *data)
315 {
316 	struct machine *machine = data->machine;
317 	union perf_event *event = data->event;
318 	struct perf_record_ksymbol *ksymbol;
319 	int len;
320 
321 	ksymbol = &event->ksymbol;
322 
323 	*ksymbol = (struct perf_record_ksymbol) {
324 		.header = {
325 			.type = PERF_RECORD_KSYMBOL,
326 			.size = offsetof(struct perf_record_ksymbol, name),
327 		},
328 		.addr      = addr,
329 		.len       = page_size,
330 		.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
331 		.flags     = 0,
332 	};
333 
334 	len = scnprintf(ksymbol->name, KSYM_NAME_LEN, "%s", name);
335 	ksymbol->header.size += PERF_ALIGN(len + 1, sizeof(u64));
336 	memset((void *) event + event->header.size, 0, machine->id_hdr_size);
337 	event->header.size += machine->id_hdr_size;
338 
339 	return perf_tool__process_synth_event(data->tool, event, machine,
340 					      data->process);
341 }
342 
343 static int
344 kallsyms_process_symbol(void *data, const char *_name,
345 			char type __maybe_unused, u64 start)
346 {
347 	char disp[KSYM_NAME_LEN];
348 	char *module, *name;
349 	unsigned long id;
350 	int err = 0;
351 
352 	module = strchr(_name, '\t');
353 	if (!module)
354 		return 0;
355 
356 	/* We are going after [bpf] module ... */
357 	if (strcmp(module + 1, "[bpf]"))
358 		return 0;
359 
360 	name = memdup(_name, (module - _name) + 1);
361 	if (!name)
362 		return -ENOMEM;
363 
364 	name[module - _name] = 0;
365 
366 	/* .. and only for trampolines and dispatchers */
367 	if ((sscanf(name, "bpf_trampoline_%lu", &id) == 1) ||
368 	    (sscanf(name, "bpf_dispatcher_%s", disp) == 1))
369 		err = process_bpf_image(name, start, data);
370 
371 	free(name);
372 	return err;
373 }
374 
375 int perf_event__synthesize_bpf_events(struct perf_session *session,
376 				      perf_event__handler_t process,
377 				      struct machine *machine,
378 				      struct record_opts *opts)
379 {
380 	const char *kallsyms_filename = "/proc/kallsyms";
381 	struct kallsyms_parse arg;
382 	union perf_event *event;
383 	__u32 id = 0;
384 	int err;
385 	int fd;
386 
387 	event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size);
388 	if (!event)
389 		return -1;
390 
391 	/* Synthesize all the bpf programs in system. */
392 	while (true) {
393 		err = bpf_prog_get_next_id(id, &id);
394 		if (err) {
395 			if (errno == ENOENT) {
396 				err = 0;
397 				break;
398 			}
399 			pr_debug("%s: can't get next program: %s%s\n",
400 				 __func__, strerror(errno),
401 				 errno == EINVAL ? " -- kernel too old?" : "");
402 			/* don't report error on old kernel or EPERM  */
403 			err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
404 			break;
405 		}
406 		fd = bpf_prog_get_fd_by_id(id);
407 		if (fd < 0) {
408 			pr_debug("%s: failed to get fd for prog_id %u\n",
409 				 __func__, id);
410 			continue;
411 		}
412 
413 		err = perf_event__synthesize_one_bpf_prog(session, process,
414 							  machine, fd,
415 							  event, opts);
416 		close(fd);
417 		if (err) {
418 			/* do not return error for old kernel */
419 			if (err == -2)
420 				err = 0;
421 			break;
422 		}
423 	}
424 
425 	/* Synthesize all the bpf images - trampolines/dispatchers. */
426 	if (symbol_conf.kallsyms_name != NULL)
427 		kallsyms_filename = symbol_conf.kallsyms_name;
428 
429 	arg = (struct kallsyms_parse) {
430 		.event   = event,
431 		.process = process,
432 		.machine = machine,
433 		.tool    = session->tool,
434 	};
435 
436 	if (kallsyms__parse(kallsyms_filename, &arg, kallsyms_process_symbol)) {
437 		pr_err("%s: failed to synthesize bpf images: %s\n",
438 		       __func__, strerror(errno));
439 	}
440 
441 	free(event);
442 	return err;
443 }
444 
445 static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
446 {
447 	struct bpf_prog_info_node *info_node;
448 	struct perf_bpil *info_linear;
449 	struct btf *btf = NULL;
450 	u64 arrays;
451 	u32 btf_id;
452 	int fd;
453 
454 	fd = bpf_prog_get_fd_by_id(id);
455 	if (fd < 0)
456 		return;
457 
458 	arrays = 1UL << PERF_BPIL_JITED_KSYMS;
459 	arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
460 	arrays |= 1UL << PERF_BPIL_FUNC_INFO;
461 	arrays |= 1UL << PERF_BPIL_PROG_TAGS;
462 	arrays |= 1UL << PERF_BPIL_JITED_INSNS;
463 	arrays |= 1UL << PERF_BPIL_LINE_INFO;
464 	arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
465 
466 	info_linear = get_bpf_prog_info_linear(fd, arrays);
467 	if (IS_ERR_OR_NULL(info_linear)) {
468 		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
469 		goto out;
470 	}
471 
472 	btf_id = info_linear->info.btf_id;
473 
474 	info_node = malloc(sizeof(struct bpf_prog_info_node));
475 	if (info_node) {
476 		info_node->info_linear = info_linear;
477 		perf_env__insert_bpf_prog_info(env, info_node);
478 	} else
479 		free(info_linear);
480 
481 	if (btf_id == 0)
482 		goto out;
483 
484 	btf = btf__load_from_kernel_by_id(btf_id);
485 	if (libbpf_get_error(btf)) {
486 		pr_debug("%s: failed to get BTF of id %u, aborting\n",
487 			 __func__, btf_id);
488 		goto out;
489 	}
490 	perf_env__fetch_btf(env, btf_id, btf);
491 
492 out:
493 	btf__free(btf);
494 	close(fd);
495 }
496 
497 static int bpf_event__sb_cb(union perf_event *event, void *data)
498 {
499 	struct perf_env *env = data;
500 
501 	if (event->header.type != PERF_RECORD_BPF_EVENT)
502 		return -1;
503 
504 	switch (event->bpf.type) {
505 	case PERF_BPF_EVENT_PROG_LOAD:
506 		perf_env__add_bpf_info(env, event->bpf.id);
507 
508 	case PERF_BPF_EVENT_PROG_UNLOAD:
509 		/*
510 		 * Do not free bpf_prog_info and btf of the program here,
511 		 * as annotation still need them. They will be freed at
512 		 * the end of the session.
513 		 */
514 		break;
515 	default:
516 		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
517 		break;
518 	}
519 
520 	return 0;
521 }
522 
523 int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)
524 {
525 	struct perf_event_attr attr = {
526 		.type	          = PERF_TYPE_SOFTWARE,
527 		.config           = PERF_COUNT_SW_DUMMY,
528 		.sample_id_all    = 1,
529 		.watermark        = 1,
530 		.bpf_event        = 1,
531 		.size	   = sizeof(attr), /* to capture ABI version */
532 	};
533 
534 	/*
535 	 * Older gcc versions don't support designated initializers, like above,
536 	 * for unnamed union members, such as the following:
537 	 */
538 	attr.wakeup_watermark = 1;
539 
540 	return evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);
541 }
542 
543 void bpf_event__print_bpf_prog_info(struct bpf_prog_info *info,
544 				    struct perf_env *env,
545 				    FILE *fp)
546 {
547 	__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
548 	__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
549 	char name[KSYM_NAME_LEN];
550 	struct btf *btf = NULL;
551 	u32 sub_prog_cnt, i;
552 
553 	sub_prog_cnt = info->nr_jited_ksyms;
554 	if (sub_prog_cnt != info->nr_prog_tags ||
555 	    sub_prog_cnt != info->nr_jited_func_lens)
556 		return;
557 
558 	if (info->btf_id) {
559 		struct btf_node *node;
560 
561 		node = perf_env__find_btf(env, info->btf_id);
562 		if (node)
563 			btf = btf__new((__u8 *)(node->data),
564 				       node->data_size);
565 	}
566 
567 	if (sub_prog_cnt == 1) {
568 		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, 0);
569 		fprintf(fp, "# bpf_prog_info %u: %s addr 0x%llx size %u\n",
570 			info->id, name, prog_addrs[0], prog_lens[0]);
571 		goto out;
572 	}
573 
574 	fprintf(fp, "# bpf_prog_info %u:\n", info->id);
575 	for (i = 0; i < sub_prog_cnt; i++) {
576 		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, i);
577 
578 		fprintf(fp, "# \tsub_prog %u: %s addr 0x%llx size %u\n",
579 			i, name, prog_addrs[i], prog_lens[i]);
580 	}
581 out:
582 	btf__free(btf);
583 }
584