From nobody Tue Dec 2 03:00:28 2025 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E063A332ED9 for ; Mon, 17 Nov 2025 13:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763386002; cv=none; b=nqAI6ZF0ALT+Pw7RPGE/axArjk3xrCRzaCxdcBtPmrCzVovNmJCGziFPgikwieGqqV29gHqOvV5aMjxeRvIqFpVRunw3eRmLUBq8il0Ue4ro/0e85eCyL0xmlu+CPsbpCd1G5IN4Izbmi4EMLBd8fKh/6+6dSkYZ6tLtxvNwSB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763386002; c=relaxed/simple; bh=gKhZlZG9Uptrdn86em/7HgNFouQXhzpq2vdn6XCl1dw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ghAFcC9zKAr7gIetZp13kz15yyMRkSXqKLOx0F1+aYME2gfeDCw91UHQ/U5dcnj+yftyxZMuxWByRyYEbhm4t4uohndZNfsKuNFvW+PS12Krk1DyyX6xt/q3MCifNeeI+vEI4RG2EByg0BUJbwHEPy628xFMciKw3A3N5yHIEmM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KZoXMYHy; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KZoXMYHy" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-3410c86070dso3212624a91.1 for ; Mon, 17 Nov 2025 05:26:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763386000; x=1763990800; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mjdSgxLBu++Fq6ABtVyHLvDR0wgeK7sjNIs7L3tmdN8=; b=KZoXMYHygxsgjfcj3iiWtF3l0jGoAHL4DA0/mT/F+49ky2EP7wefpHtKNVBe5kO5HR 9H2LgG+qGuG1cVk+Naah/JXTF6hgyad/La8MwCto3PwTtdTpHauDh/Q4yuoJh0qu55m+ Py1IJ22QqMit+hBEzg/nccf8zHL8HhoU7TFB78hNZdAxzRS4XTQsO1MHrH/+JGPI1hxY /B60ne+TPKGZQ9WCl6Zzi1MviMNbkRGOraQtEOnc1pIMSnXKGzUKDpmnGTx4Wt5v6YHy 3BaE9xuGDJMpnx/unqkA7dfpPuKUjkMzveJp0NpMxaJbZkoEz17UdE8/+T8YKm+Ux8CI u2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763386000; x=1763990800; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=mjdSgxLBu++Fq6ABtVyHLvDR0wgeK7sjNIs7L3tmdN8=; b=cKftMgH/2/cxC91pjBMy60F+N9ueyYyIYzb2QVG/+7CfWK7S7CAegthA37BY6QT4ha tcvQO0Uys6MAy3eZ0uyfhH7t4Cy2fc/95OFKOtW5VWY8kSO3whxlDpGf2JnYiLp5V2ih wBd7c75Ay4YBHr4VDkHtD+0yYdruIMsD2ii36Io/+H6HvEz8JsVntp1o/GazNbHVqQ9G CI609vhMsLg2O7H72TvpkNUN0/jcOi3XfyA4x4sm9ChL5Qo5TZPzAP2Dkd6EKlrPNhWg vv+XkwGwPo2ddvsoBfAROhK4plWWr4jNQJTYy7LRCw48+nI6FwV16UI/HLn37FfriwVY Q/cw== X-Forwarded-Encrypted: i=1; AJvYcCXVevS6lwk2rDXBMsrmf7sUNcLnzYHyRCgkqpd06OCGb54t/tpMbwHWEnwSCuLcDbLE/QstAbSQtLdfCWw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3z/S/l7Cbq67/+KYxJBripxTkmnl98koCySuYJvLe/Zn2TBIm dq5PPIlMaJ3uxk6aMEw+h4/7/lBIyHwB4kBkDoosi/wQP2hOet77X+7N X-Gm-Gg: ASbGncv3Hzac5EpB6dJ4U+zR31DOpSBSiuLDpazSWAXIHjQbTzAwkdgrpYTGpSfU8mk 8brlxeCqgHAj9zrkwgFNTuL0uSbUWNiLrWv0FQLBj77MzV5aFJ7gic2xSASCElBdOfh+IIgIQXo 21mt02Ew2eekx9dlMnpxWIih3NMzmY9e4EvZhoLUkBEwxKbxukjU7Fx0hs2Wf4bDmVY3xeQpktz wSxNdG0TcHLTcoc1uFkg7JGueGALwIuGiPbKrAeY0kOMQl8JAmMJztI0YUB0w2HIxJQvh8wcSGh avR4dq0bwaeRzUxaaJHaIfUq6sPtxcpll9r7fM7Mzd+0uFFqBZUXvIQB5k+QTt5IbuCE6FhzeVV Omp4d3nwb1rDWjhdhGnSSKV8OVxncmnDFQpsor+XsVF6++FwAmoQ6w9saXYkhHIj+7AhrSI/PBn pGt69F0k9AuhEIv46r X-Google-Smtp-Source: AGHT+IEhHi7O4yt0EMTyloFaD8KlYsdb2aEOshF+gKxXsgJ79nlgnHKhh5nGs3RcG3o+qFAumLcqCw== X-Received: by 2002:a17:90b:1807:b0:340:ac7c:6387 with SMTP id 98e67ed59e1d1-343f9e90659mr16368075a91.7.1763386000187; Mon, 17 Nov 2025 05:26:40 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b924cd89bcsm13220953b3a.15.2025.11.17.05.26.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 05:26:39 -0800 (PST) From: Donglin Peng To: ast@kernel.org Cc: eddyz87@gmail.com, andrii.nakryiko@gmail.com, zhangxiaoqin@xiaomi.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Donglin Peng , Alan Maguire , Song Liu Subject: [RFC PATCH v6 3/7] tools/resolve_btfids: Add --btf_sort option for BTF name sorting Date: Mon, 17 Nov 2025 21:26:19 +0800 Message-Id: <20251117132623.3807094-4-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251117132623.3807094-1-dolinux.peng@gmail.com> References: <20251117132623.3807094-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Donglin Peng This patch introduces a new --btf_sort option that leverages libbpf's btf__permute interface to reorganize BTF layout. The implementation sorts BTF types by name in ascending order, placing anonymous types at the end to enable efficient binary search lookup. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- scripts/Makefile.btf | 2 + scripts/Makefile.modfinal | 1 + scripts/link-vmlinux.sh | 1 + tools/bpf/resolve_btfids/main.c | 203 ++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+) diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf index db76335dd917..d5eb4ee70e88 100644 --- a/scripts/Makefile.btf +++ b/scripts/Makefile.btf @@ -27,6 +27,7 @@ pahole-flags-$(call test-ge, $(pahole-ver), 130) +=3D --b= tf_features=3Dattributes =20 ifneq ($(KBUILD_EXTMOD),) module-pahole-flags-$(call test-ge, $(pahole-ver), 128) +=3D --btf_feature= s=3Ddistilled_base +module-resolve_btfid-flags-y =3D --distilled_base endif =20 endif @@ -35,3 +36,4 @@ pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE) +=3D --la= ng_exclude=3Drust =20 export PAHOLE_FLAGS :=3D $(pahole-flags-y) export MODULE_PAHOLE_FLAGS :=3D $(module-pahole-flags-y) +export MODULE_RESOLVE_BTFID_FLAGS :=3D $(module-resolve_btfid-flags-y) diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index 542ba462ed3e..4481dda2f485 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -40,6 +40,7 @@ quiet_cmd_btf_ko =3D BTF [M] $@ printf "Skipping BTF generation for %s due to unavailability of vmlinux\= n" $@ 1>&2; \ else \ LLVM_OBJCOPY=3D"$(OBJCOPY)" $(PAHOLE) -J $(PAHOLE_FLAGS) $(MODULE_PAHOLE= _FLAGS) --btf_base $(objtree)/vmlinux $@; \ + $(RESOLVE_BTFIDS) -b $(objtree)/vmlinux $(MODULE_RESOLVE_BTFID_FLAGS) --= btf_sort $@; \ $(RESOLVE_BTFIDS) -b $(objtree)/vmlinux $@; \ fi; =20 diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 433849ff7529..f21f6300815b 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -288,6 +288,7 @@ if is_enabled CONFIG_DEBUG_INFO_BTF; then if is_enabled CONFIG_WERROR; then RESOLVE_BTFIDS_ARGS=3D" --fatal_warnings " fi + ${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_ARGS} --btf_sort "${VMLINUX}" ${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_ARGS} "${VMLINUX}" fi =20 diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index d47191c6e55e..778909fe2faa 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -768,6 +768,198 @@ static int symbols_patch(struct object *obj) return err < 0 ? -1 : 0; } =20 +/* Anonymous types (with empty names) are considered greater than named ty= pes + * and are sorted after them. Two anonymous types are considered equal. Na= med + * types are compared lexicographically. + */ +static int cmp_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf =3D (struct btf *)priv; + const struct btf_type *ta =3D btf__type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb =3D btf__type_by_id(btf, *(__u32 *)b); + const char *na, *nb; + + if (!ta->name_off && tb->name_off) + return 1; + if (ta->name_off && !tb->name_off) + return -1; + if (!ta->name_off && !tb->name_off) + return 0; + + na =3D btf__str_by_offset(btf, ta->name_off); + nb =3D btf__str_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +static int update_elf(const char *path, const struct btf *btf, + const char *btf_secname) +{ + GElf_Shdr shdr_mem, *shdr; + Elf_Data *btf_data =3D NULL; + Elf_Scn *scn =3D NULL; + Elf *elf =3D NULL; + const void *raw_btf_data; + uint32_t raw_btf_size; + int fd, err =3D -1; + size_t strndx; + + fd =3D open(path, O_RDWR); + if (fd < 0) { + pr_err("FAILED to open %s\n", path); + return -1; + } + + if (elf_version(EV_CURRENT) =3D=3D EV_NONE) { + pr_err("FAILED to set libelf version"); + goto out; + } + + elf =3D elf_begin(fd, ELF_C_RDWR, NULL); + if (elf =3D=3D NULL) { + pr_err("FAILED to update ELF file"); + goto out; + } + + elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT); + + elf_getshdrstrndx(elf, &strndx); + while ((scn =3D elf_nextscn(elf, scn)) !=3D NULL) { + char *secname; + + shdr =3D gelf_getshdr(scn, &shdr_mem); + if (shdr =3D=3D NULL) + continue; + secname =3D elf_strptr(elf, strndx, shdr->sh_name); + if (strcmp(secname, btf_secname) =3D=3D 0) { + btf_data =3D elf_getdata(scn, btf_data); + break; + } + } + + raw_btf_data =3D btf__raw_data(btf, &raw_btf_size); + + if (btf_data) { + if (raw_btf_size !=3D btf_data->d_size) { + pr_err("FAILED: size mismatch"); + goto out; + } + + btf_data->d_buf =3D (void *)raw_btf_data; + btf_data->d_type =3D ELF_T_WORD; + elf_flagdata(btf_data, ELF_C_SET, ELF_F_DIRTY); + + if (elf_update(elf, ELF_C_WRITE) >=3D 0) + err =3D 0; + } + +out: + if (fd !=3D -1) + close(fd); + if (elf) + elf_end(elf); + return err; +} + +static int sort_update_btf(struct object *obj, bool distilled_base) +{ + struct btf *base_btf =3D NULL; + struct btf *btf =3D NULL; + int start_id =3D 0, nr_types, id; + int err =3D 0, offs, i; + __u32 *permute_ids =3D NULL, *id_map =3D NULL, btf_size; + const void *btf_data; + int fd; + + if (obj->base_btf_path) { + base_btf =3D btf__parse(obj->base_btf_path, NULL); + err =3D libbpf_get_error(base_btf); + if (err) { + pr_err("FAILED: load base BTF from %s: %s\n", + obj->base_btf_path, strerror(-err)); + return -1; + } + } + + btf =3D btf__parse_elf_split(obj->path, base_btf); + err =3D libbpf_get_error(btf); + if (err) { + pr_err("FAILED: load BTF from %s: %s\n", obj->path, strerror(-err)); + goto out; + } + + if (base_btf) + start_id =3D btf__type_cnt(base_btf); + nr_types =3D btf__type_cnt(btf) - start_id; + if (nr_types < 2) + goto out; + + offs =3D base_btf ? 0 : 1; + + permute_ids =3D calloc(nr_types, sizeof(*permute_ids)); + if (!permute_ids) { + err =3D -ENOMEM; + goto out; + } + + id_map =3D calloc(nr_types, sizeof(*id_map)); + if (!id_map) { + err =3D -ENOMEM; + goto out; + } + + for (i =3D 0, id =3D start_id; i < nr_types; i++, id++) + permute_ids[i] =3D id; + + qsort_r(permute_ids + offs, nr_types - offs, sizeof(*permute_ids), + cmp_type_names, btf); + + for (i =3D 0; i < nr_types; i++) { + id =3D permute_ids[i] - start_id; + id_map[id] =3D i + start_id; + } + + err =3D btf__permute(btf, id_map, nr_types, NULL); + if (err) { + pr_err("FAILED: btf permute: %s\n", strerror(-err)); + goto out; + } + + if (distilled_base) { + struct btf *new_btf =3D NULL, *distilled_base =3D NULL; + + if (btf__distill_base(btf, &distilled_base, &new_btf) < 0) { + pr_err("FAILED to generate distilled base BTF: %s\n", + strerror(errno)); + goto out; + } + + err =3D update_elf(obj->path, new_btf, BTF_ELF_SEC); + if (!err) { + err =3D update_elf(obj->path, distilled_base, BTF_BASE_ELF_SEC); + if (err < 0) + pr_err("FAILED to update '%s'\n", BTF_BASE_ELF_SEC); + } else { + pr_err("FAILED to update '%s'\n", BTF_ELF_SEC); + } + + btf__free(new_btf); + btf__free(distilled_base); + } else { + err =3D update_elf(obj->path, btf, BTF_ELF_SEC); + if (err < 0) { + pr_err("FAILED to update '%s'\n", BTF_ELF_SEC); + goto out; + } + } + +out: + free(permute_ids); + free(id_map); + btf__free(base_btf); + btf__free(btf); + return err; +} + static const char * const resolve_btfids_usage[] =3D { "resolve_btfids [] ", NULL @@ -787,6 +979,8 @@ int main(int argc, const char **argv) .sets =3D RB_ROOT, }; bool fatal_warnings =3D false; + bool btf_sort =3D false; + bool distilled_base =3D false; struct option btfid_options[] =3D { OPT_INCR('v', "verbose", &verbose, "be more verbose (show errors, etc)"), @@ -796,6 +990,10 @@ int main(int argc, const char **argv) "path of file providing base BTF"), OPT_BOOLEAN(0, "fatal_warnings", &fatal_warnings, "turn warnings into errors"), + OPT_BOOLEAN(0, "btf_sort", &btf_sort, + "sort BTF by name"), + OPT_BOOLEAN(0, "distilled_base", &distilled_base, + "update distilled base"), OPT_END() }; int err =3D -1; @@ -807,6 +1005,11 @@ int main(int argc, const char **argv) =20 obj.path =3D argv[0]; =20 + if (btf_sort) { + err =3D sort_update_btf(&obj, distilled_base); + goto out; + } + if (elf_collect(&obj)) goto out; =20 --=20 2.34.1