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

Configure Feed

Select the types of activity you want to include in your feed.

resolve_btfids: Introduce enum btf_id_kind

Instead of using multiple flags, make struct btf_id tagged with an
enum value indicating its kind in the context of resolve_btfids.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20251219181321.1283664-4-ihor.solodrai@linux.dev

authored by

Ihor Solodrai and committed by
Andrii Nakryiko
a4fa885b 5f347a0f

+60 -23
+60 -23
tools/bpf/resolve_btfids/main.c
··· 98 98 # error "Unknown machine endianness!" 99 99 #endif 100 100 101 + enum btf_id_kind { 102 + BTF_ID_KIND_NONE, 103 + BTF_ID_KIND_SYM, 104 + BTF_ID_KIND_SET, 105 + BTF_ID_KIND_SET8 106 + }; 107 + 101 108 struct btf_id { 102 109 struct rb_node rb_node; 103 110 char *name; ··· 112 105 int id; 113 106 int cnt; 114 107 }; 108 + enum btf_id_kind kind; 115 109 int addr_cnt; 116 - bool is_set; 117 - bool is_set8; 118 110 Elf64_Addr addr[ADDR_CNT]; 119 111 }; 120 112 ··· 203 197 return NULL; 204 198 } 205 199 206 - static struct btf_id * 207 - btf_id__add(struct rb_root *root, char *name, bool unique) 200 + static struct btf_id *__btf_id__add(struct rb_root *root, 201 + char *name, 202 + enum btf_id_kind kind, 203 + bool unique) 208 204 { 209 205 struct rb_node **p = &root->rb_node; 210 206 struct rb_node *parent = NULL; ··· 229 221 if (id) { 230 222 pr_debug("adding symbol %s\n", name); 231 223 id->name = name; 224 + id->kind = kind; 232 225 rb_link_node(&id->rb_node, parent, p); 233 226 rb_insert_color(&id->rb_node, root); 234 227 } 235 228 return id; 229 + } 230 + 231 + static inline struct btf_id *btf_id__add(struct rb_root *root, char *name, enum btf_id_kind kind) 232 + { 233 + return __btf_id__add(root, name, kind, false); 234 + } 235 + 236 + static inline struct btf_id *btf_id__add_unique(struct rb_root *root, char *name, enum btf_id_kind kind) 237 + { 238 + return __btf_id__add(root, name, kind, true); 236 239 } 237 240 238 241 static char *get_id(const char *prefix_end) ··· 279 260 return id; 280 261 } 281 262 282 - static struct btf_id *add_set(struct object *obj, char *name, bool is_set8) 263 + static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind kind) 283 264 { 265 + int len = strlen(name); 266 + int prefixlen; 267 + char *id; 268 + 284 269 /* 285 270 * __BTF_ID__set__name 286 271 * name = ^ 287 272 * id = ^ 288 273 */ 289 - char *id = name + (is_set8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__")) - 1; 290 - int len = strlen(name); 274 + switch (kind) { 275 + case BTF_ID_KIND_SET: 276 + prefixlen = sizeof(BTF_SET "__") - 1; 277 + break; 278 + case BTF_ID_KIND_SET8: 279 + prefixlen = sizeof(BTF_SET8 "__") - 1; 280 + break; 281 + default: 282 + pr_err("Unexpected kind %d passed to %s() for symbol %s\n", kind, __func__, name); 283 + return NULL; 284 + } 291 285 286 + id = name + prefixlen; 292 287 if (id >= name + len) { 293 288 pr_err("FAILED to parse set name: %s\n", name); 294 289 return NULL; 295 290 } 296 291 297 - return btf_id__add(&obj->sets, id, true); 292 + return btf_id__add_unique(&obj->sets, id, kind); 298 293 } 299 294 300 295 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size) ··· 321 288 return NULL; 322 289 } 323 290 324 - return btf_id__add(root, id, false); 291 + return btf_id__add(root, id, BTF_ID_KIND_SYM); 325 292 } 326 293 327 294 /* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */ ··· 524 491 id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1); 525 492 /* set8 */ 526 493 } else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) { 527 - id = add_set(obj, prefix, true); 494 + id = add_set(obj, prefix, BTF_ID_KIND_SET8); 528 495 /* 529 496 * SET8 objects store list's count, which is encoded 530 497 * in symbol's size, together with 'cnt' field hence 531 498 * that - 1. 532 499 */ 533 - if (id) { 500 + if (id) 534 501 id->cnt = sym.st_size / sizeof(uint64_t) - 1; 535 - id->is_set8 = true; 536 - } 537 502 /* set */ 538 503 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) { 539 - id = add_set(obj, prefix, false); 504 + id = add_set(obj, prefix, BTF_ID_KIND_SET); 540 505 /* 541 506 * SET objects store list's count, which is encoded 542 507 * in symbol's size, together with 'cnt' field hence 543 508 * that - 1. 544 509 */ 545 - if (id) { 510 + if (id) 546 511 id->cnt = sym.st_size / sizeof(int) - 1; 547 - id->is_set = true; 548 - } 549 512 } else { 550 513 pr_err("FAILED unsupported prefix %s\n", prefix); 551 514 return -1; 552 515 } 553 516 554 517 if (!id) 555 - return -ENOMEM; 518 + return -EINVAL; 556 519 557 520 if (id->addr_cnt >= ADDR_CNT) { 558 521 pr_err("FAILED symbol %s crossed the number of allowed lists\n", ··· 672 643 int i; 673 644 674 645 /* For set, set8, id->id may be 0 */ 675 - if (!id->id && !id->is_set && !id->is_set8) { 646 + if (!id->id && id->kind != BTF_ID_KIND_SET && id->kind != BTF_ID_KIND_SET8) { 676 647 pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name); 677 648 warnings++; 678 649 } ··· 725 696 { 726 697 Elf_Data *data = obj->efile.idlist; 727 698 struct rb_node *next; 699 + int cnt; 728 700 729 701 next = rb_first(&obj->sets); 730 702 while (next) { ··· 745 715 return -1; 746 716 } 747 717 748 - if (id->is_set) { 718 + switch (id->kind) { 719 + case BTF_ID_KIND_SET: 749 720 set = data->d_buf + off; 721 + cnt = set->cnt; 750 722 qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id); 751 - } else { 723 + break; 724 + case BTF_ID_KIND_SET8: 752 725 set8 = data->d_buf + off; 726 + cnt = set8->cnt; 753 727 /* 754 728 * Make sure id is at the beginning of the pairs 755 729 * struct, otherwise the below qsort would not work. ··· 778 744 bswap_32(set8->pairs[i].flags); 779 745 } 780 746 } 747 + break; 748 + default: 749 + pr_err("Unexpected btf_id_kind %d for set '%s'\n", id->kind, id->name); 750 + return -1; 781 751 } 782 752 783 - pr_debug("sorting addr %5lu: cnt %6d [%s]\n", 784 - off, id->is_set ? set->cnt : set8->cnt, id->name); 753 + pr_debug("sorting addr %5lu: cnt %6d [%s]\n", off, cnt, id->name); 785 754 786 755 next = rb_next(next); 787 756 }