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