Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: libbpf: Refactor and bug fix on the bpf_func_info loading logic

This patch refactor and fix a bug in the libbpf's bpf_func_info loading
logic. The bug fix and refactoring are targeting the same
commit 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections")
which is in the bpf-next branch.

1) In bpf_load_program_xattr(), it should retry when errno == E2BIG
regardless of log_buf and log_buf_sz. This patch fixes it.

2) btf_ext__reloc_init() and btf_ext__reloc() are essentially
the same except btf_ext__reloc_init() always has insns_cnt == 0.
Hence, btf_ext__reloc_init() is removed.

btf_ext__reloc() is also renamed to btf_ext__reloc_func_info()
to get ready for the line_info support in the next patch.

3) Consolidate func_info section logic from "btf_ext_parse_hdr()",
"btf_ext_validate_func_info()" and "btf_ext__new()" to
a new function "btf_ext_copy_func_info()" such that similar
logic can be reused by the later libbpf's line_info patch.

4) The next line_info patch will store line_info_cnt instead of
line_info_len in the bpf_program because the kernel is taking
line_info_cnt also. It will save a few "len" to "cnt" conversions
and will also save some function args.

Hence, this patch also makes bpf_program to store func_info_cnt
instead of func_info_len.

5) btf_ext depends on btf. e.g. the func_info's type_id
in ".BTF.ext" is not useful when ".BTF" is absent.
This patch only init the obj->btf_ext pointer after
it has successfully init the obj->btf pointer.

This can avoid always checking "obj->btf && obj->btf_ext"
together for accessing ".BTF.ext". Checking "obj->btf_ext"
alone will do.

6) Move "struct btf_sec_func_info" from btf.h to btf.c.
There is no external usage outside btf.c.

Fixes: 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections")
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
f0187f0b 4d6304c7

+177 -177
+5 -2
tools/lib/bpf/bpf.c
··· 205 205 min(name_len, BPF_OBJ_NAME_LEN - 1)); 206 206 207 207 fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); 208 - if (fd >= 0 || !log_buf || !log_buf_sz) 208 + if (fd >= 0) 209 209 return fd; 210 210 211 211 /* After bpf_prog_load, the kernel may modify certain attributes ··· 244 244 245 245 fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); 246 246 247 - if (fd >= 0 || !log_buf || !log_buf_sz) 247 + if (fd >= 0) 248 248 goto done; 249 249 } 250 + 251 + if (!log_buf || !log_buf_sz) 252 + goto done; 250 253 251 254 /* Try again with log */ 252 255 attr.log_buf = ptr_to_u64(log_buf);
+73 -118
tools/lib/bpf/btf.c
··· 43 43 __u32 func_info_len; 44 44 }; 45 45 46 + struct btf_sec_func_info { 47 + __u32 sec_name_off; 48 + __u32 num_func_info; 49 + /* Followed by num_func_info number of bpf func_info records */ 50 + __u8 data[0]; 51 + }; 52 + 46 53 /* The minimum bpf_func_info checked by the loader */ 47 54 struct bpf_func_info_min { 48 55 __u32 insn_off; ··· 486 479 return err; 487 480 } 488 481 489 - static int btf_ext_validate_func_info(const void *finfo, __u32 size, 490 - btf_print_fn_t err_log) 482 + static int btf_ext_copy_func_info(struct btf_ext *btf_ext, 483 + __u8 *data, __u32 data_size, 484 + btf_print_fn_t err_log) 491 485 { 492 - int sec_hdrlen = sizeof(struct btf_sec_func_info); 493 - __u32 size_left, num_records, record_size; 486 + const struct btf_ext_header *hdr = (struct btf_ext_header *)data; 494 487 const struct btf_sec_func_info *sinfo; 495 - __u64 total_record_size; 488 + __u32 info_left, record_size; 489 + /* The start of the info sec (including the __u32 record_size). */ 490 + const void *info; 491 + 492 + /* data and data_size do not include btf_ext_header from now on */ 493 + data = data + hdr->hdr_len; 494 + data_size -= hdr->hdr_len; 495 + 496 + if (hdr->func_info_off & 0x03) { 497 + elog("BTF.ext func_info section is not aligned to 4 bytes\n"); 498 + return -EINVAL; 499 + } 500 + 501 + if (data_size < hdr->func_info_off || 502 + hdr->func_info_len > data_size - hdr->func_info_off) { 503 + elog("func_info section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n", 504 + hdr->func_info_off, hdr->func_info_len); 505 + return -EINVAL; 506 + } 507 + 508 + info = data + hdr->func_info_off; 509 + info_left = hdr->func_info_len; 496 510 497 511 /* At least a func_info record size */ 498 - if (size < sizeof(__u32)) { 512 + if (info_left < sizeof(__u32)) { 499 513 elog("BTF.ext func_info record size not found"); 500 514 return -EINVAL; 501 515 } 502 516 503 - /* The record size needs to meet below minimum standard */ 504 - record_size = *(__u32 *)finfo; 517 + /* The record size needs to meet the minimum standard */ 518 + record_size = *(__u32 *)info; 505 519 if (record_size < sizeof(struct bpf_func_info_min) || 506 - record_size % sizeof(__u32)) { 520 + record_size & 0x03) { 507 521 elog("BTF.ext func_info invalid record size"); 508 522 return -EINVAL; 509 523 } 510 524 511 - sinfo = finfo + sizeof(__u32); 512 - size_left = size - sizeof(__u32); 525 + sinfo = info + sizeof(__u32); 526 + info_left -= sizeof(__u32); 513 527 514 528 /* If no func_info records, return failure now so .BTF.ext 515 529 * won't be used. 516 530 */ 517 - if (!size_left) { 531 + if (!info_left) { 518 532 elog("BTF.ext no func info records"); 519 533 return -EINVAL; 520 534 } 521 535 522 - while (size_left) { 523 - if (size_left < sec_hdrlen) { 536 + while (info_left) { 537 + unsigned int sec_hdrlen = sizeof(struct btf_sec_func_info); 538 + __u64 total_record_size; 539 + __u32 num_records; 540 + 541 + if (info_left < sec_hdrlen) { 524 542 elog("BTF.ext func_info header not found"); 525 543 return -EINVAL; 526 544 } ··· 558 526 559 527 total_record_size = sec_hdrlen + 560 528 (__u64)num_records * record_size; 561 - if (size_left < total_record_size) { 529 + if (info_left < total_record_size) { 562 530 elog("incorrect BTF.ext num_func_info"); 563 531 return -EINVAL; 564 532 } 565 533 566 - size_left -= total_record_size; 534 + info_left -= total_record_size; 567 535 sinfo = (void *)sinfo + total_record_size; 568 536 } 537 + 538 + btf_ext->func_info_len = hdr->func_info_len - sizeof(__u32); 539 + btf_ext->func_info_rec_size = record_size; 540 + btf_ext->func_info = malloc(btf_ext->func_info_len); 541 + if (!btf_ext->func_info) 542 + return -ENOMEM; 543 + memcpy(btf_ext->func_info, info + sizeof(__u32), 544 + btf_ext->func_info_len); 569 545 570 546 return 0; 571 547 } ··· 582 542 btf_print_fn_t err_log) 583 543 { 584 544 const struct btf_ext_header *hdr = (struct btf_ext_header *)data; 585 - __u32 meta_left, last_func_info_pos; 586 - void *finfo; 587 545 588 546 if (data_size < offsetof(struct btf_ext_header, func_info_off) || 589 547 data_size < hdr->hdr_len) { ··· 604 566 return -ENOTSUP; 605 567 } 606 568 607 - meta_left = data_size - hdr->hdr_len; 608 - if (!meta_left) { 569 + if (data_size == hdr->hdr_len) { 609 570 elog("BTF.ext has no data\n"); 610 571 return -EINVAL; 611 572 } 612 573 613 - if (meta_left < hdr->func_info_off) { 614 - elog("Invalid BTF.ext func_info section offset:%u\n", 615 - hdr->func_info_off); 616 - return -EINVAL; 617 - } 618 - 619 - if (hdr->func_info_off & 0x03) { 620 - elog("BTF.ext func_info section is not aligned to 4 bytes\n"); 621 - return -EINVAL; 622 - } 623 - 624 - last_func_info_pos = hdr->hdr_len + hdr->func_info_off + 625 - hdr->func_info_len; 626 - if (last_func_info_pos > data_size) { 627 - elog("Invalid BTF.ext func_info section size:%u\n", 628 - hdr->func_info_len); 629 - return -EINVAL; 630 - } 631 - 632 - finfo = data + hdr->hdr_len + hdr->func_info_off; 633 - return btf_ext_validate_func_info(finfo, hdr->func_info_len, 634 - err_log); 574 + return 0; 635 575 } 636 576 637 577 void btf_ext__free(struct btf_ext *btf_ext) ··· 623 607 624 608 struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log) 625 609 { 626 - const struct btf_ext_header *hdr; 627 610 struct btf_ext *btf_ext; 628 - void *org_fdata, *fdata; 629 - __u32 hdrlen, size_u32; 630 611 int err; 631 612 632 613 err = btf_ext_parse_hdr(data, size, err_log); ··· 634 621 if (!btf_ext) 635 622 return ERR_PTR(-ENOMEM); 636 623 637 - hdr = (const struct btf_ext_header *)data; 638 - hdrlen = hdr->hdr_len; 639 - size_u32 = sizeof(__u32); 640 - fdata = malloc(hdr->func_info_len - size_u32); 641 - if (!fdata) { 642 - free(btf_ext); 643 - return ERR_PTR(-ENOMEM); 624 + err = btf_ext_copy_func_info(btf_ext, data, size, err_log); 625 + if (err) { 626 + btf_ext__free(btf_ext); 627 + return ERR_PTR(err); 644 628 } 645 - 646 - /* remember record size and copy rest of func_info data */ 647 - org_fdata = data + hdrlen + hdr->func_info_off; 648 - btf_ext->func_info_rec_size = *(__u32 *)org_fdata; 649 - memcpy(fdata, org_fdata + size_u32, hdr->func_info_len - size_u32); 650 - btf_ext->func_info = fdata; 651 - btf_ext->func_info_len = hdr->func_info_len - size_u32; 652 629 653 630 return btf_ext; 654 631 } 655 632 656 - int btf_ext__reloc_init(struct btf *btf, struct btf_ext *btf_ext, 657 - const char *sec_name, void **func_info, 658 - __u32 *func_info_rec_size, __u32 *func_info_len) 659 - { 660 - __u32 sec_hdrlen = sizeof(struct btf_sec_func_info); 661 - __u32 i, record_size, records_len; 662 - struct btf_sec_func_info *sinfo; 663 - const char *info_sec_name; 664 - __s64 remain_len; 665 - void *data; 666 - 667 - record_size = btf_ext->func_info_rec_size; 668 - sinfo = btf_ext->func_info; 669 - remain_len = btf_ext->func_info_len; 670 - 671 - while (remain_len > 0) { 672 - records_len = sinfo->num_func_info * record_size; 673 - info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off); 674 - if (strcmp(info_sec_name, sec_name)) { 675 - remain_len -= sec_hdrlen + records_len; 676 - sinfo = (void *)sinfo + sec_hdrlen + records_len; 677 - continue; 678 - } 679 - 680 - data = malloc(records_len); 681 - if (!data) 682 - return -ENOMEM; 683 - 684 - memcpy(data, sinfo->data, records_len); 685 - 686 - /* adjust the insn_off, the data in .BTF.ext is 687 - * the actual byte offset, and the kernel expects 688 - * the offset in term of bpf_insn. 689 - * 690 - * adjust the insn offset only, the rest data will 691 - * be passed to kernel. 692 - */ 693 - for (i = 0; i < sinfo->num_func_info; i++) { 694 - struct bpf_func_info_min *record; 695 - 696 - record = data + i * record_size; 697 - record->insn_off /= sizeof(struct bpf_insn); 698 - } 699 - 700 - *func_info = data; 701 - *func_info_len = records_len; 702 - *func_info_rec_size = record_size; 703 - return 0; 704 - } 705 - 706 - return -EINVAL; 707 - } 708 - 709 - int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext, 710 - const char *sec_name, __u32 insns_cnt, 711 - void **func_info, __u32 *func_info_len) 633 + int btf_ext__reloc_func_info(struct btf *btf, struct btf_ext *btf_ext, 634 + const char *sec_name, __u32 insns_cnt, 635 + void **func_info, __u32 *cnt) 712 636 { 713 637 __u32 sec_hdrlen = sizeof(struct btf_sec_func_info); 714 638 __u32 i, record_size, existing_flen, records_len; ··· 666 716 continue; 667 717 } 668 718 669 - existing_flen = *func_info_len; 719 + existing_flen = (*cnt) * record_size; 670 720 data = realloc(*func_info, existing_flen + records_len); 671 721 if (!data) 672 722 return -ENOMEM; ··· 684 734 insns_cnt; 685 735 } 686 736 *func_info = data; 687 - *func_info_len = existing_flen + records_len; 737 + *cnt += sinfo->num_func_info; 688 738 return 0; 689 739 } 690 740 691 - return -EINVAL; 741 + return -ENOENT; 742 + } 743 + 744 + __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext) 745 + { 746 + return btf_ext->func_info_rec_size; 692 747 }
+4 -13
tools/lib/bpf/btf.h
··· 53 53 __u32 func_info_len; 54 54 }; 55 55 56 - struct btf_sec_func_info { 57 - __u32 sec_name_off; 58 - __u32 num_func_info; 59 - /* Followed by num_func_info number of bpf func_info records */ 60 - __u8 data[0]; 61 - }; 62 - 63 56 typedef int (*btf_print_fn_t)(const char *, ...) 64 57 __attribute__((format(printf, 1, 2))); 65 58 ··· 70 77 71 78 struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log); 72 79 void btf_ext__free(struct btf_ext *btf_ext); 73 - int btf_ext__reloc_init(struct btf *btf, struct btf_ext *btf_ext, 74 - const char *sec_name, void **func_info, 75 - __u32 *func_info_rec_size, __u32 *func_info_len); 76 - int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext, 77 - const char *sec_name, __u32 insns_cnt, void **func_info, 78 - __u32 *func_info_len); 80 + int btf_ext__reloc_func_info(struct btf *btf, struct btf_ext *btf_ext, 81 + const char *sec_name, __u32 insns_cnt, 82 + void **func_info, __u32 *func_info_len); 83 + __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); 79 84 80 85 #ifdef __cplusplus 81 86 } /* extern "C" */
+95 -44
tools/lib/bpf/libbpf.c
··· 167 167 int btf_fd; 168 168 void *func_info; 169 169 __u32 func_info_rec_size; 170 - __u32 func_info_len; 170 + __u32 func_info_cnt; 171 171 172 172 struct bpf_capabilities *caps; 173 173 }; ··· 779 779 { 780 780 Elf *elf = obj->efile.elf; 781 781 GElf_Ehdr *ep = &obj->efile.ehdr; 782 + Elf_Data *btf_ext_data = NULL; 782 783 Elf_Scn *scn = NULL; 783 784 int idx = 0, err = 0; 784 785 ··· 842 841 obj->btf = NULL; 843 842 } 844 843 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { 845 - obj->btf_ext = btf_ext__new(data->d_buf, data->d_size, 846 - __pr_debug); 847 - if (IS_ERR(obj->btf_ext)) { 848 - pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", 849 - BTF_EXT_ELF_SEC, 850 - PTR_ERR(obj->btf_ext)); 851 - obj->btf_ext = NULL; 852 - } 844 + btf_ext_data = data; 853 845 } else if (sh.sh_type == SHT_SYMTAB) { 854 846 if (obj->efile.symbols) { 855 847 pr_warning("bpf: multiple SYMTAB in %s\n", ··· 903 909 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) { 904 910 pr_warning("Corrupted ELF file: index of strtab invalid\n"); 905 911 return LIBBPF_ERRNO__FORMAT; 912 + } 913 + if (btf_ext_data) { 914 + if (!obj->btf) { 915 + pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", 916 + BTF_EXT_ELF_SEC, BTF_ELF_SEC); 917 + } else { 918 + obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, 919 + btf_ext_data->d_size, 920 + __pr_debug); 921 + if (IS_ERR(obj->btf_ext)) { 922 + pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", 923 + BTF_EXT_ELF_SEC, 924 + PTR_ERR(obj->btf_ext)); 925 + obj->btf_ext = NULL; 926 + } 927 + } 906 928 } 907 929 if (obj->efile.maps_shndx >= 0) { 908 930 err = bpf_object__init_maps(obj, flags); ··· 1286 1276 } 1287 1277 1288 1278 static int 1279 + check_btf_ext_reloc_err(struct bpf_program *prog, int err, 1280 + void *btf_prog_info, const char *info_name) 1281 + { 1282 + if (err != -ENOENT) { 1283 + pr_warning("Error in loading %s for sec %s.\n", 1284 + info_name, prog->section_name); 1285 + return err; 1286 + } 1287 + 1288 + /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */ 1289 + 1290 + if (btf_prog_info) { 1291 + /* 1292 + * Some info has already been found but has problem 1293 + * in the last btf_ext reloc. Must have to error 1294 + * out. 1295 + */ 1296 + pr_warning("Error in relocating %s for sec %s.\n", 1297 + info_name, prog->section_name); 1298 + return err; 1299 + } 1300 + 1301 + /* 1302 + * Have problem loading the very first info. Ignore 1303 + * the rest. 1304 + */ 1305 + pr_warning("Cannot find %s for main program sec %s. Ignore all %s.\n", 1306 + info_name, prog->section_name, info_name); 1307 + return 0; 1308 + } 1309 + 1310 + static int 1311 + bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj, 1312 + const char *section_name, __u32 insn_offset) 1313 + { 1314 + int err; 1315 + 1316 + if (!insn_offset || prog->func_info) { 1317 + /* 1318 + * !insn_offset => main program 1319 + * 1320 + * For sub prog, the main program's func_info has to 1321 + * be loaded first (i.e. prog->func_info != NULL) 1322 + */ 1323 + err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext, 1324 + section_name, insn_offset, 1325 + &prog->func_info, 1326 + &prog->func_info_cnt); 1327 + if (err) 1328 + return check_btf_ext_reloc_err(prog, err, 1329 + prog->func_info, 1330 + "bpf_func_info"); 1331 + 1332 + prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext); 1333 + } 1334 + 1335 + if (!insn_offset) 1336 + prog->btf_fd = btf__fd(obj->btf); 1337 + 1338 + return 0; 1339 + } 1340 + 1341 + static int 1289 1342 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, 1290 1343 struct reloc_desc *relo) 1291 1344 { ··· 1379 1306 return -ENOMEM; 1380 1307 } 1381 1308 1382 - if (obj->btf && obj->btf_ext) { 1383 - err = btf_ext__reloc(obj->btf, obj->btf_ext, 1384 - text->section_name, 1385 - prog->insns_cnt, 1386 - &prog->func_info, 1387 - &prog->func_info_len); 1388 - if (err) { 1389 - pr_warning("error in btf_ext__reloc for sec %s\n", 1390 - text->section_name); 1309 + if (obj->btf_ext) { 1310 + err = bpf_program_reloc_btf_ext(prog, obj, 1311 + text->section_name, 1312 + prog->insns_cnt); 1313 + if (err) 1391 1314 return err; 1392 - } 1393 1315 } 1394 1316 1395 1317 memcpy(new_insn + prog->insns_cnt, text->insns, ··· 1409 1341 if (!prog) 1410 1342 return 0; 1411 1343 1412 - if (obj->btf && obj->btf_ext) { 1413 - err = btf_ext__reloc_init(obj->btf, obj->btf_ext, 1414 - prog->section_name, 1415 - &prog->func_info, 1416 - &prog->func_info_rec_size, 1417 - &prog->func_info_len); 1418 - if (err) { 1419 - pr_warning("err in btf_ext__reloc_init for sec %s\n", 1420 - prog->section_name); 1344 + if (obj->btf_ext) { 1345 + err = bpf_program_reloc_btf_ext(prog, obj, 1346 + prog->section_name, 0); 1347 + if (err) 1421 1348 return err; 1422 - } 1423 - prog->btf_fd = btf__fd(obj->btf); 1424 1349 } 1425 1350 1426 1351 if (!prog->reloc_desc) ··· 1505 1444 1506 1445 static int 1507 1446 load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, 1508 - char *license, __u32 kern_version, int *pfd, 1509 - __u32 func_info_cnt) 1447 + char *license, __u32 kern_version, int *pfd) 1510 1448 { 1511 1449 struct bpf_load_program_attr load_attr; 1512 1450 char *cp, errmsg[STRERR_BUFSIZE]; ··· 1525 1465 load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0; 1526 1466 load_attr.func_info = prog->func_info; 1527 1467 load_attr.func_info_rec_size = prog->func_info_rec_size; 1528 - load_attr.func_info_cnt = func_info_cnt; 1529 - 1468 + load_attr.func_info_cnt = prog->func_info_cnt; 1530 1469 if (!load_attr.insns || !load_attr.insns_cnt) 1531 1470 return -EINVAL; 1532 1471 ··· 1582 1523 bpf_program__load(struct bpf_program *prog, 1583 1524 char *license, __u32 kern_version) 1584 1525 { 1585 - __u32 func_info_cnt; 1586 1526 int err = 0, fd, i; 1587 - 1588 - if (prog->func_info_len == 0) 1589 - func_info_cnt = 0; 1590 - else 1591 - func_info_cnt = prog->func_info_len / prog->func_info_rec_size; 1592 1527 1593 1528 if (prog->instances.nr < 0 || !prog->instances.fds) { 1594 1529 if (prog->preprocessor) { ··· 1606 1553 prog->section_name, prog->instances.nr); 1607 1554 } 1608 1555 err = load_program(prog, prog->insns, prog->insns_cnt, 1609 - license, kern_version, &fd, 1610 - func_info_cnt); 1556 + license, kern_version, &fd); 1611 1557 if (!err) 1612 1558 prog->instances.fds[0] = fd; 1613 1559 goto out; ··· 1636 1584 1637 1585 err = load_program(prog, result.new_insn_ptr, 1638 1586 result.new_insn_cnt, 1639 - license, kern_version, &fd, 1640 - func_info_cnt); 1587 + license, kern_version, &fd); 1641 1588 1642 1589 if (err) { 1643 1590 pr_warning("Loading the %dth instance of program '%s' failed\n",