From da3dc77332577d07c6e2eced119e03387e965fe2 Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:25 +0800 Subject: [PATCH 1/7] libbpf: Add BTF permutation support for type reordering Introduce btf__permute() API to allow in-place rearrangement of BTF types. This function reorganizes BTF type order according to a provided array of type IDs, updating all type references to maintain consistency. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- tools/lib/bpf/btf.c | 166 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 43 ++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 210 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 84a4b0abc8be9..71d464690db58 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5868,3 +5868,169 @@ int btf__relocate(struct btf *btf, const struct btf *base_btf) btf->owns_base = false; return libbpf_err(err); } + +struct btf_permute { + struct btf *btf; + __u32 *id_map; +}; + +/* Callback function to remap individual type ID references */ +static int btf_permute_remap_type_id(__u32 *type_id, void *ctx) +{ + struct btf_permute *p = ctx; + __u32 new_type_id = *type_id; + + /* skip references that point into the base BTF or VOID */ + if (new_type_id < p->btf->start_id) + return 0; + + /* invalid reference id */ + if (new_type_id >= btf__type_cnt(p->btf)) + return -EINVAL; + + new_type_id = p->id_map[new_type_id - p->btf->start_id]; + /* reference a dropped type is not allowed */ + if (new_type_id == 0) + return -EINVAL; + + *type_id = new_type_id; + return 0; +} + +int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt, + const struct btf_permute_opts *opts) +{ + struct btf_permute p; + struct btf_ext *btf_ext; + void *next_type, *end_type; + void *nt, *new_types = NULL; + int err = 0, i, new_type_len; + __u32 *order_map = NULL; + __u32 id, new_nr_types = 0; + + if (!OPTS_VALID(opts, btf_permute_opts) || id_map_cnt != btf->nr_types) + return libbpf_err(-EINVAL); + + /* used to record the storage sequence of types */ + order_map = calloc(btf->nr_types, sizeof(*id_map)); + if (!order_map) { + err = -ENOMEM; + goto done; + } + + new_types = calloc(btf->hdr->type_len, 1); + if (!new_types) { + err = -ENOMEM; + goto done; + } + + if (btf_ensure_modifiable(btf)) { + err = -ENOMEM; + goto done; + } + + for (i = 0; i < id_map_cnt; i++) { + id = id_map[i]; + /* Drop the specified type */ + if (id == 0) + continue; + /* Invalid id */ + if (id < btf->start_id || id >= btf__type_cnt(btf)) { + err = -EINVAL; + goto done; + } + id -= btf->start_id; + /* Multiple types cannot be mapped to the same ID */ + if (order_map[id]) { + err = -EINVAL; + goto done; + } + order_map[id] = i + btf->start_id; + new_nr_types = max(id + 1, new_nr_types); + } + + /* Check for missing IDs */ + for (i = 0; i < new_nr_types; i++) { + if (order_map[i] == 0) { + err = -EINVAL; + goto done; + } + } + + p.btf = btf; + p.id_map = id_map; + nt = new_types; + for (i = 0; i < new_nr_types; i++) { + struct btf_field_iter it; + const struct btf_type *t; + __u32 *type_id; + int type_size; + + id = order_map[i]; + /* must be a valid type ID */ + t = btf__type_by_id(btf, id); + if (!t) { + err = -EINVAL; + goto done; + } + type_size = btf_type_size(t); + memcpy(nt, t, type_size); + + /* Fix up referenced IDs for BTF */ + err = btf_field_iter_init(&it, nt, BTF_FIELD_ITER_IDS); + if (err) + goto done; + while ((type_id = btf_field_iter_next(&it))) { + err = btf_permute_remap_type_id(type_id, &p); + if (err) + goto done; + } + + nt += type_size; + } + + /* Fix up referenced IDs for btf_ext */ + btf_ext = OPTS_GET(opts, btf_ext, NULL); + if (btf_ext) { + err = btf_ext_visit_type_ids(btf_ext, btf_permute_remap_type_id, &p); + if (err) + goto done; + } + + new_type_len = nt - new_types; + next_type = new_types; + end_type = next_type + new_type_len; + i = 0; + while (next_type + sizeof(struct btf_type) <= end_type) { + btf->type_offs[i++] = next_type - new_types; + next_type += btf_type_size(next_type); + } + + /* Resize */ + if (new_type_len < btf->hdr->type_len) { + void *tmp_types; + + tmp_types = realloc(new_types, new_type_len); + if (new_type_len && !tmp_types) { + err = -ENOMEM; + goto done; + } + new_types = tmp_types; + btf->nr_types = new_nr_types; + btf->type_offs_cap = btf->nr_types; + btf->types_data_cap = new_type_len; + btf->hdr->type_len = new_type_len; + btf->hdr->str_off = new_type_len; + btf->raw_size = btf->hdr->hdr_len + btf->hdr->type_len + btf->hdr->str_len; + } + + free(order_map); + free(btf->types_data); + btf->types_data = new_types; + return 0; + +done: + free(order_map); + free(new_types); + return libbpf_err(err); +} diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index cc01494d62107..0177b4235819a 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -281,6 +281,49 @@ LIBBPF_API int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts); */ LIBBPF_API int btf__relocate(struct btf *btf, const struct btf *base_btf); +struct btf_permute_opts { + size_t sz; + /* optional .BTF.ext info along the main BTF info */ + struct btf_ext *btf_ext; + size_t :0; +}; +#define btf_permute_opts__last_field btf_ext + +/** + * @brief **btf__permute()** performs in-place BTF type rearrangement + * @param btf BTF object to permute + * @param id_map Array mapping original type IDs to new IDs + * @param id_map_cnt Number of elements in @id_map + * @param opts Optional parameters for BTF extension updates + * @return 0 on success, negative error code on failure + * + * **btf__permute()** rearranges BTF types according to the specified ID mapping. + * The @id_map array defines the new type ID for each original type ID. + * + * For **base BTF**: + * - @id_map must include all types from ID 1 to `btf__type_cnt(btf)-1` + * - @id_map_cnt should be `btf__type_cnt(btf) - 1` + * - Mapping uses `id_map[original_id - 1] = new_id` + * + * For **split BTF**: + * - @id_map should cover only split types + * - @id_map_cnt should be `btf__type_cnt(btf) - btf__type_cnt(btf__base_btf(btf))` + * - Mapping uses `id_map[original_id - btf__type_cnt(btf__base_btf(btf))] = new_id` + * + * Setting @id_map element to 0 drops the corresponding type. Dropped types must not + * be referenced by any retained types. After permutation, type references in BTF + * data and optional extension are updated automatically. + * + * Note: Dropping types may orphan some strings, requiring subsequent **btf__dedup()** + * to clean up unreferenced strings. + * + * On error, returns negative error code and sets errno: + * - `-EINVAL`: Invalid parameters or ID mapping (duplicates, out-of-range) + * - `-ENOMEM`: Memory allocation failure + */ +LIBBPF_API int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt, + const struct btf_permute_opts *opts); + struct btf_dump; struct btf_dump_opts { diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 8ed8749907d47..b778e5a5d0a88 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -451,4 +451,5 @@ LIBBPF_1.7.0 { global: bpf_map__set_exclusive_program; bpf_map__exclusive_program; + btf__permute; } LIBBPF_1.6.0; From 1145ca72fb511573d07e4f932bc60ceed049cf42 Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:26 +0800 Subject: [PATCH 2/7] selftests/bpf: Add test cases for btf__permute functionality This patch introduces test cases for the btf__permute function to ensure it works correctly with both base BTF and split BTF scenarios. The test suite includes: - test_permute_base: Validates permutation on base BTF - test_permute_split: Tests permutation on split BTF - test_permute_drop_base: Validates type dropping on base BTF - test_permute_drop_split: Tests type dropping on split BTF - test_permute_drop_dedup: Tests type dropping and deduping Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- .../selftests/bpf/prog_tests/btf_permute.c | 608 ++++++++++++++++++ 1 file changed, 608 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_permute.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/testing/selftests/bpf/prog_tests/btf_permute.c new file mode 100644 index 0000000000000..f67bf89519b30 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c @@ -0,0 +1,608 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Xiaomi */ + +#include +#include +#include "btf_helpers.h" + +/* Ensure btf__permute work as expected with base BTF */ +static void test_permute_base(void) +{ + struct btf *btf; + __u32 permute_ids[6]; + int start_id = 1; + int err; + + btf = btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(btf, 1); /* [2] ptr to int */ + btf__add_struct(btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(btf, 1); /* [5] int (*)(int *p); */ + btf__add_func_param(btf, "p", 2); + btf__add_func(btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] FUNC 'f' type_id=5 linkage=static"); + + permute_ids[1 - start_id] = 4; /* [1] -> [4] */ + permute_ids[2 - start_id] = 3; /* [2] -> [3] */ + permute_ids[3 - start_id] = 5; /* [3] -> [5] */ + permute_ids[4 - start_id] = 1; /* [4] -> [1] */ + permute_ids[5 - start_id] = 6; /* [5] -> [6] */ + permute_ids[6 - start_id] = 2; /* [6] -> [2] */ + err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[2] FUNC 'f' type_id=6 linkage=static", + "[3] PTR '(anon)' type_id=4", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[6] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=3"); + + /* + * For base BTF, id_map_cnt must equal to the number of types + * include VOID type + */ + permute_ids[1 - start_id] = 4; /* [1] -> [4] */ + permute_ids[2 - start_id] = 3; /* [2] -> [3] */ + permute_ids[3 - start_id] = 5; /* [3] -> [5] */ + permute_ids[4 - start_id] = 1; /* [4] -> [1] */ + permute_ids[5 - start_id] = 6; /* [5] -> [6] */ + permute_ids[6 - start_id] = 2; /* [6] -> [2] */ + err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids) - 1, NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[2] FUNC 'f' type_id=6 linkage=static", + "[3] PTR '(anon)' type_id=4", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[6] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=3"); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[1 - start_id] = 4; + permute_ids[2 - start_id] = 4; + permute_ids[3 - start_id] = 5; + permute_ids[4 - start_id] = 1; + permute_ids[5 - start_id] = 6; + permute_ids[6 - start_id] = 2; + err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[2] FUNC 'f' type_id=6 linkage=static", + "[3] PTR '(anon)' type_id=4", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[6] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=3"); + + /* Type ID must be valid */ + permute_ids[1 - start_id] = 4; + permute_ids[2 - start_id] = 3; + permute_ids[3 - start_id] = 5; + permute_ids[4 - start_id] = 1; + permute_ids[5 - start_id] = 7; + permute_ids[6 - start_id] = 2; + err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[2] FUNC 'f' type_id=6 linkage=static", + "[3] PTR '(anon)' type_id=4", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[6] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=3"); + +done: + btf__free(btf); +} + +/* Ensure btf__permute work as expected with split BTF */ +static void test_permute_split(void) +{ + struct btf *split_btf = NULL, *base_btf = NULL; + __u32 permute_ids[4]; + int err; + int start_id; + + base_btf = btf__new_empty(); + if (!ASSERT_OK_PTR(base_btf, "empty_main_btf")) + return; + + btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(base_btf, 1); /* [2] ptr to int */ + VALIDATE_RAW_BTF( + base_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1"); + split_btf = btf__new_empty_split(base_btf); + if (!ASSERT_OK_PTR(split_btf, "empty_split_btf")) + goto cleanup; + btf__add_struct(split_btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(split_btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(split_btf, 1); /* [5] int (*)(int p); */ + btf__add_func_param(split_btf, "p", 2); + btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] FUNC 'f' type_id=5 linkage=static"); + + start_id = btf__type_cnt(base_btf); + permute_ids[3 - start_id] = 6; /* [3] -> [6] */ + permute_ids[4 - start_id] = 3; /* [4] -> [3] */ + permute_ids[5 - start_id] = 5; /* [5] -> [5] */ + permute_ids[6 - start_id] = 4; /* [6] -> [4] */ + err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] FUNC 'f' type_id=5 linkage=static", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* + * For split BTF, id_map_cnt must equal to the number of types + * added on top of base BTF + */ + permute_ids[3 - start_id] = 4; + permute_ids[4 - start_id] = 3; + permute_ids[5 - start_id] = 5; + permute_ids[6 - start_id] = 6; + err = btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] FUNC 'f' type_id=5 linkage=static", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[3 - start_id] = 4; + permute_ids[4 - start_id] = 3; + permute_ids[5 - start_id] = 3; + permute_ids[6 - start_id] = 6; + err = btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] FUNC 'f' type_id=5 linkage=static", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* Can not map to base ID */ + permute_ids[3 - start_id] = 4; + permute_ids[4 - start_id] = 2; + permute_ids[5 - start_id] = 5; + permute_ids[6 - start_id] = 6; + err = btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] FUNC 'f' type_id=5 linkage=static", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + +cleanup: + btf__free(split_btf); + btf__free(base_btf); +} + +/* Verify btf__permute function drops types correctly with base_btf */ +static void test_permute_drop_base(void) +{ + struct btf *btf; + __u32 permute_ids[6]; + int start_id = 1; + int err; + + btf = btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(btf, 1); /* [2] ptr to int */ + btf__add_struct(btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(btf, 1); /* [5] int (*)(int *p); */ + btf__add_func_param(btf, "p", 2); + btf__add_func(btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] FUNC 'f' type_id=5 linkage=static"); + + /* Drop ID 4 */ + permute_ids[1 - start_id] = 5; /* [1] -> [5] */ + permute_ids[2 - start_id] = 1; /* [2] -> [1] */ + permute_ids[3 - start_id] = 2; /* [3] -> [2] */ + permute_ids[4 - start_id] = 0; /* Drop [4] */ + permute_ids[5 - start_id] = 3; /* [5] -> [3] */ + permute_ids[6 - start_id] = 4; /* [6] -> [4] */ + err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=5", + "[2] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=5 bits_offset=0", + "[3] FUNC_PROTO '(anon)' ret_type_id=5 vlen=1\n" + "\t'p' type_id=1", + "[4] FUNC 'f' type_id=3 linkage=static", + "[5] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED"); + + /* Continue dropping */ + permute_ids[1 - start_id] = 1; /* [1] -> [1] */ + permute_ids[2 - start_id] = 2; /* [2] -> [2] */ + permute_ids[3 - start_id] = 3; /* [3] -> [3] */ + permute_ids[4 - start_id] = 0; /* Drop [4] */ + permute_ids[5 - start_id] = 4; /* [5] -> [4] */ + err = btf__permute(btf, permute_ids, 5, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=4", + "[2] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[3] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=1", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED"); + + /* Cannot drop the ID referenced by others */ + permute_ids[1 - start_id] = 2; + permute_ids[2 - start_id] = 3; + permute_ids[3 - start_id] = 1; + permute_ids[4 - start_id] = 0; /* [4] is referenced by others */ + err = btf__permute(btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_drop_base_fail")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=4", + "[2] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=4 bits_offset=0", + "[3] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" + "\t'p' type_id=1", + "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED"); + + /* Drop 2 IDs at once */ + permute_ids[1 - start_id] = 2; /* [1] -> [2] */ + permute_ids[2 - start_id] = 0; /* Drop [2] */ + permute_ids[3 - start_id] = 0; /* Drop [3] */ + permute_ids[4 - start_id] = 1; /* [4] -> [1] */ + err = btf__permute(btf, permute_ids, 4, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1"); + + /* Drop all IDs */ + permute_ids[1 - start_id] = 0; /* Drop [1] */ + permute_ids[2 - start_id] = 0; /* Drop [2] */ + err = btf__permute(btf, permute_ids, 2, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + if (!ASSERT_EQ(btf__type_cnt(btf), 1, "btf__permute_drop_base all")) + goto done; + +done: + btf__free(btf); +} + +/* Verify btf__permute function drops types correctly with split BTF */ +static void test_permute_drop_split(void) +{ + struct btf *split_btf = NULL, *base_btf = NULL; + __u32 permute_ids[4]; + int err; + int start_id; + + base_btf = btf__new_empty(); + if (!ASSERT_OK_PTR(base_btf, "empty_main_btf")) + return; + + btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(base_btf, 1); /* [2] ptr to int */ + VALIDATE_RAW_BTF( + base_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1"); + split_btf = btf__new_empty_split(base_btf); + if (!ASSERT_OK_PTR(split_btf, "empty_split_btf")) + goto cleanup; + btf__add_struct(split_btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(split_btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(split_btf, 1); /* [5] int (*)(int p); */ + btf__add_func_param(split_btf, "p", 2); + btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[4] STRUCT 's2' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[6] FUNC 'f' type_id=5 linkage=static"); + + start_id = btf__type_cnt(base_btf); + + /* Drop ID 4 */ + permute_ids[3 - start_id] = 5; /* [3] -> [5] */ + permute_ids[4 - start_id] = 0; /* Drop [4] */ + permute_ids[5 - start_id] = 3; /* [5] -> [3] */ + permute_ids[6 - start_id] = 4; /* [6] -> [4] */ + err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[4] FUNC 'f' type_id=3 linkage=static", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* Can not drop the type referenced by others */ + permute_ids[3 - start_id] = 0; /* [3] is referenced by [4] */ + permute_ids[4 - start_id] = 4; + permute_ids[5 - start_id] = 3; + err = btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_ERR(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" + "\t'p' type_id=2", + "[4] FUNC 'f' type_id=3 linkage=static", + "[5] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* Continue dropping */ + permute_ids[3 - start_id] = 0; /* Drop [3] */ + permute_ids[4 - start_id] = 0; /* Drop [4] */ + permute_ids[5 - start_id] = 3; /* [5] -> [3] */ + err = btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1", + "[3] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0"); + + /* Continue dropping */ + permute_ids[3 - start_id] = 0; /* Drop [3] */ + err = btf__permute(split_btf, permute_ids, 1, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] PTR '(anon)' type_id=1"); + +cleanup: + btf__free(split_btf); + btf__free(base_btf); +} + +/* Verify btf__permute then btf__dedup work correctly */ +static void test_permute_drop_dedup(void) +{ + struct btf *btf, *new_btf = NULL; + const struct btf_header *hdr; + const void *btf_data; + char expect_strs[] = "\0int\0s1\0m\0tag1\0tag2\0tag3"; + char expect_strs_dedupped[] = "\0int\0s1\0m\0tag1"; + __u32 permute_ids[5], btf_size; + int start_id = 1; + int err; + + btf = btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_struct(btf, "s1", 4); /* [2] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_decl_tag(btf, "tag1", 2, -1); /* [3] tag -> s1: tag1 */ + btf__add_decl_tag(btf, "tag2", 2, 1); /* [4] tag -> s1/m: tag2 */ + btf__add_decl_tag(btf, "tag3", 2, 1); /* [5] tag -> s1/m: tag3 */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[2] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=1 bits_offset=0", + "[3] DECL_TAG 'tag1' type_id=2 component_idx=-1", + "[4] DECL_TAG 'tag2' type_id=2 component_idx=1", + "[5] DECL_TAG 'tag3' type_id=2 component_idx=1"); + + btf_data = btf__raw_data(btf, &btf_size); + if (!ASSERT_OK_PTR(btf_data, "btf__raw_data")) + goto done; + hdr = btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs), "expect_strs")) + goto done; + + new_btf = btf__new(btf_data, btf_size); + if (!ASSERT_OK_PTR(new_btf, "btf__new")) + goto done; + + /* Drop 2 IDs result in unreferenced strings */ + permute_ids[1 - start_id] = 3; /* [1] -> [3] */ + permute_ids[2 - start_id] = 1; /* [2] -> [1] */ + permute_ids[3 - start_id] = 2; /* [3] -> [2] */ + permute_ids[4 - start_id] = 0; /* Drop result in unreferenced "tag2" */ + permute_ids[5 - start_id] = 0; /* Drop result in unreferenced "tag3" */ + err = btf__permute(new_btf, permute_ids, 5, NULL); + if (!ASSERT_OK(err, "btf__permute")) + goto done; + + VALIDATE_RAW_BTF( + new_btf, + "[1] STRUCT 's1' size=4 vlen=1\n" + "\t'm' type_id=3 bits_offset=0", + "[2] DECL_TAG 'tag1' type_id=1 component_idx=-1", + "[3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED"); + + btf_data = btf__raw_data(new_btf, &btf_size); + if (!ASSERT_OK_PTR(btf_data, "btf__raw_data")) + goto done; + hdr = btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs), "expect_strs")) + goto done; + + err = btf__dedup(new_btf, NULL); + if (!ASSERT_OK(err, "btf__dedup")) + goto done; + + btf_data = btf__raw_data(new_btf, &btf_size); + if (!ASSERT_OK_PTR(btf_data, "btf__raw_data")) + goto done; + hdr = btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs_dedupped), "expect_strs_dedupped")) + goto done; + +done: + btf__free(btf); + btf__free(new_btf); +} + +void test_btf_permute(void) +{ + if (test__start_subtest("permute_base")) + test_permute_base(); + if (test__start_subtest("permute_split")) + test_permute_split(); + if (test__start_subtest("permute_drop_base")) + test_permute_drop_base(); + if (test__start_subtest("permute_drop_split")) + test_permute_drop_split(); + if (test__start_subtest("permute_drop_dedup")) + test_permute_drop_dedup(); +} From aee15499d65894cf70d45d05f7b4f86126716142 Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:27 +0800 Subject: [PATCH 3/7] tools/resolve_btfids: Add --btf_sort option for BTF name sorting 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 | 200 ++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf index db76335dd9176..d5eb4ee70e880 100644 --- a/scripts/Makefile.btf +++ b/scripts/Makefile.btf @@ -27,6 +27,7 @@ pahole-flags-$(call test-ge, $(pahole-ver), 130) += --btf_features=attributes ifneq ($(KBUILD_EXTMOD),) module-pahole-flags-$(call test-ge, $(pahole-ver), 128) += --btf_features=distilled_base +module-resolve_btfid-flags-y = --distilled_base endif endif @@ -35,3 +36,4 @@ pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE) += --lang_exclude=rust export PAHOLE_FLAGS := $(pahole-flags-y) export MODULE_PAHOLE_FLAGS := $(module-pahole-flags-y) +export MODULE_RESOLVE_BTFID_FLAGS := $(module-resolve_btfid-flags-y) diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index 542ba462ed3ec..4481dda2f4857 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -40,6 +40,7 @@ quiet_cmd_btf_ko = BTF [M] $@ printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \ else \ LLVM_OBJCOPY="$(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; diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 433849ff7529e..f21f6300815bd 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=" --fatal_warnings " fi + ${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_ARGS} --btf_sort "${VMLINUX}" ${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_ARGS} "${VMLINUX}" fi diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c index d47191c6e55e1..dc0badd6f375b 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -768,6 +768,195 @@ static int symbols_patch(struct object *obj) return err < 0 ? -1 : 0; } +/* Anonymous types (with empty names) are considered greater than named types + * and are sorted after them. Two anonymous types are considered equal. Named + * types are compared lexicographically. + */ +static int cmp_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf = (struct btf *)priv; + const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb = 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 = btf__str_by_offset(btf, ta->name_off); + nb = btf__str_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +static int update_btf_section(const char *path, const struct btf *btf, + const char *btf_secname) +{ + GElf_Shdr shdr_mem, *shdr; + Elf_Data *btf_data = NULL; + Elf_Scn *scn = NULL; + Elf *elf = NULL; + const void *raw_btf_data; + uint32_t raw_btf_size; + int fd, err = -1; + size_t strndx; + + fd = open(path, O_RDWR); + if (fd < 0) { + pr_err("FAILED to open %s\n", path); + return -1; + } + + if (elf_version(EV_CURRENT) == EV_NONE) { + pr_err("FAILED to set libelf version"); + goto out; + } + + elf = elf_begin(fd, ELF_C_RDWR, NULL); + if (elf == 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 = elf_nextscn(elf, scn)) != NULL) { + char *secname; + + shdr = gelf_getshdr(scn, &shdr_mem); + if (shdr == NULL) + continue; + secname = elf_strptr(elf, strndx, shdr->sh_name); + if (strcmp(secname, btf_secname) == 0) { + btf_data = elf_getdata(scn, btf_data); + break; + } + } + + raw_btf_data = btf__raw_data(btf, &raw_btf_size); + + if (btf_data) { + if (raw_btf_size != btf_data->d_size) { + pr_err("FAILED: size mismatch"); + goto out; + } + + btf_data->d_buf = (void *)raw_btf_data; + btf_data->d_type = ELF_T_WORD; + elf_flagdata(btf_data, ELF_C_SET, ELF_F_DIRTY); + + if (elf_update(elf, ELF_C_WRITE) >= 0) + err = 0; + } + +out: + if (fd != -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 = NULL; + struct btf *btf = NULL; + int start_id = 1, nr_types, id; + int err = 0, i; + __u32 *permute_ids = NULL, *id_map = NULL, btf_size; + const void *btf_data; + int fd; + + if (obj->base_btf_path) { + base_btf = btf__parse(obj->base_btf_path, NULL); + err = 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 = btf__parse_elf_split(obj->path, base_btf); + err = 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 = btf__type_cnt(base_btf); + nr_types = btf__type_cnt(btf) - start_id; + if (nr_types < 2) + goto out; + + permute_ids = calloc(nr_types, sizeof(*permute_ids)); + if (!permute_ids) { + err = -ENOMEM; + goto out; + } + + id_map = calloc(nr_types, sizeof(*id_map)); + if (!id_map) { + err = -ENOMEM; + goto out; + } + + for (i = 0, id = start_id; i < nr_types; i++, id++) + permute_ids[i] = id; + + qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names, btf); + + for (i = 0; i < nr_types; i++) { + id = permute_ids[i] - start_id; + id_map[id] = i + start_id; + } + + err = 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 = NULL, *distilled_base = 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 = update_btf_section(obj->path, new_btf, BTF_ELF_SEC); + if (!err) { + err = update_btf_section(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 = update_btf_section(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[] = { "resolve_btfids [] ", NULL @@ -787,6 +976,8 @@ int main(int argc, const char **argv) .sets = RB_ROOT, }; bool fatal_warnings = false; + bool btf_sort = false; + bool distilled_base = false; struct option btfid_options[] = { OPT_INCR('v', "verbose", &verbose, "be more verbose (show errors, etc)"), @@ -796,6 +987,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 in ascending order"), + OPT_BOOLEAN(0, "distilled_base", &distilled_base, + "distill base"), OPT_END() }; int err = -1; @@ -807,6 +1002,11 @@ int main(int argc, const char **argv) obj.path = argv[0]; + if (btf_sort) { + err = sort_update_btf(&obj, distilled_base); + goto out; + } + if (elf_collect(&obj)) goto out; From 5a4688d53483464d307246d0853395b9ca2a567d Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:28 +0800 Subject: [PATCH 4/7] libbpf: Optimize type lookup with binary search for sorted BTF This patch introduces binary search optimization for BTF type lookups when the BTF instance contains sorted types. The optimization significantly improves performance when searching for types in large BTF instances with sorted type names. For unsorted BTF or when nr_sorted_types is zero, the implementation falls back to the original linear search algorithm. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- tools/lib/bpf/btf.c | 104 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 71d464690db58..555703b375e3d 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -92,6 +92,12 @@ struct btf { * - for split BTF counts number of types added on top of base BTF. */ __u32 nr_types; + /* number of sorted and named types in this BTF instance: + * - doesn't include special [0] void type; + * - for split BTF counts number of sorted and named types added on + * top of base BTF. + */ + __u32 nr_sorted_types; /* if not NULL, points to the base BTF on top of which the current * split BTF is based */ @@ -897,44 +903,93 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id) return type_id; } -__s32 btf__find_by_name(const struct btf *btf, const char *type_name) +static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *name, + __s32 start_id, __s32 end_id) { - __u32 i, nr_types = btf__type_cnt(btf); + const struct btf_type *t; + const char *tname; + __s32 l, r, m; + + l = start_id; + r = end_id; + while (l <= r) { + m = l + (r - l) / 2; + t = btf_type_by_id(btf, m); + tname = btf__str_by_offset(btf, t->name_off); + if (strcmp(tname, name) >= 0) { + if (l == r) + return r; + r = m; + } else { + l = m + 1; + } + } - if (!strcmp(type_name, "void")) - return 0; + return btf__type_cnt(btf); +} - for (i = 1; i < nr_types; i++) { - const struct btf_type *t = btf__type_by_id(btf, i); - const char *name = btf__name_by_offset(btf, t->name_off); +static __s32 btf_find_type_by_name_kind(const struct btf *btf, int start_id, + const char *type_name, __u32 kind) +{ + const struct btf_type *t; + const char *tname; + int err = -ENOENT; + + if (start_id < btf->start_id) { + err = btf_find_type_by_name_kind(btf->base_btf, start_id, + type_name, kind); + if (err > 0) + goto out; + start_id = btf->start_id; + } + + if (btf->nr_sorted_types > 0) { + /* binary search */ + __s32 end_id; + int idx; + + end_id = btf->start_id + btf->nr_sorted_types - 1; + idx = btf_find_type_by_name_bsearch(btf, type_name, start_id, end_id); + for (; idx <= end_id; idx++) { + t = btf__type_by_id(btf, idx); + tname = btf__str_by_offset(btf, t->name_off); + if (strcmp(tname, type_name)) + goto out; + if (kind == -1 || btf_kind(t) == kind) + return idx; + } + } else { + /* linear search */ + __u32 i, total; - if (name && !strcmp(type_name, name)) - return i; + total = btf__type_cnt(btf); + for (i = start_id; i < total; i++) { + t = btf_type_by_id(btf, i); + if (kind != -1 && btf_kind(t) != kind) + continue; + tname = btf__str_by_offset(btf, t->name_off); + if (tname && !strcmp(tname, type_name)) + return i; + } } - return libbpf_err(-ENOENT); +out: + return err; } static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id, const char *type_name, __u32 kind) { - __u32 i, nr_types = btf__type_cnt(btf); - if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void")) return 0; - for (i = start_id; i < nr_types; i++) { - const struct btf_type *t = btf__type_by_id(btf, i); - const char *name; - - if (btf_kind(t) != kind) - continue; - name = btf__name_by_offset(btf, t->name_off); - if (name && !strcmp(type_name, name)) - return i; - } + return libbpf_err(btf_find_type_by_name_kind(btf, start_id, type_name, kind)); +} - return libbpf_err(-ENOENT); +/* the kind value of -1 indicates that kind matching should be skipped */ +__s32 btf__find_by_name(const struct btf *btf, const char *type_name) +{ + return btf_find_by_name_kind(btf, btf->start_id, type_name, -1); } __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name, @@ -1006,6 +1061,7 @@ static struct btf *btf_new_empty(struct btf *base_btf) btf->fd = -1; btf->ptr_sz = sizeof(void *); btf->swapped_endian = false; + btf->nr_sorted_types = 0; if (base_btf) { btf->base_btf = base_btf; @@ -1057,6 +1113,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b btf->start_id = 1; btf->start_str_off = 0; btf->fd = -1; + btf->nr_sorted_types = 0; if (base_btf) { btf->base_btf = base_btf; @@ -1715,6 +1772,7 @@ static void btf_invalidate_raw_data(struct btf *btf) free(btf->raw_data_swapped); btf->raw_data_swapped = NULL; } + btf->nr_sorted_types = 0; } /* Ensure BTF is ready to be modified (by splitting into a three memory From 967df3606ec7ae884f557835ae6ffacbddaa8c0d Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:29 +0800 Subject: [PATCH 5/7] libbpf: Implement BTF type sorting validation for binary search optimization This patch adds validation to verify BTF type name sorting, enabling binary search optimization for lookups. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- tools/lib/bpf/btf.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 555703b375e3d..e3b09a2152607 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -903,6 +903,64 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id) return type_id; } +/* Anonymous types (with empty names) are considered greater than named types + * and are sorted after them. Two anonymous types are considered equal. Named + * types are compared lexicographically. + */ +static int btf_compare_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf = (struct btf *)priv; + struct btf_type *ta = btf_type_by_id(btf, *(__u32 *)a); + struct btf_type *tb = btf_type_by_id(btf, *(__u32 *)b); + const char *na, *nb; + bool anon_a, anon_b; + + na = btf__str_by_offset(btf, ta->name_off); + nb = btf__str_by_offset(btf, tb->name_off); + anon_a = str_is_empty(na); + anon_b = str_is_empty(nb); + + if (anon_a && !anon_b) + return 1; + if (!anon_a && anon_b) + return -1; + if (anon_a && anon_b) + return 0; + + return strcmp(na, nb); +} + +/* Verifies that BTF types are sorted in ascending order according to their + * names, with named types appearing before anonymous types. If the ordering + * is correct, counts the number of named types and updates the BTF object's + * nr_sorted_types field. + */ +static void btf_check_sorted(struct btf *btf) +{ + const struct btf_type *t; + int i, k = 0, n, nr_sorted_types; + + if (btf->nr_types < 2) + return; + + nr_sorted_types = 0; + n = btf__type_cnt(btf) - 1; + for (i = btf->start_id; i < n; i++) { + k = i + 1; + if (btf_compare_type_names(&i, &k, btf) > 0) + return; + t = btf_type_by_id(btf, i); + if (!str_is_empty(btf__str_by_offset(btf, t->name_off))) + nr_sorted_types++; + } + + t = btf_type_by_id(btf, k); + if (!str_is_empty(btf__str_by_offset(btf, t->name_off))) + nr_sorted_types++; + if (nr_sorted_types) + btf->nr_sorted_types = nr_sorted_types; +} + static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *name, __s32 start_id, __s32 end_id) { @@ -1148,6 +1206,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b err = err ?: btf_sanity_check(btf); if (err) goto done; + btf_check_sorted(btf); done: if (err) { From bb782aee3e51074f1c2a1a09e1d5099b88a73c25 Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:30 +0800 Subject: [PATCH 6/7] btf: Optimize type lookup with binary search Improve btf_find_by_name_kind() performance by adding binary search support for sorted types. Falls back to linear search for compatibility. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- kernel/bpf/btf.c | 83 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 0de8fc8a0e0b3..5dd2c40d48740 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -259,6 +259,7 @@ struct btf { void *nohdr_data; struct btf_header hdr; u32 nr_types; /* includes VOID for base BTF */ + u32 nr_sorted_types; /* exclude VOID for base BTF */ u32 types_size; u32 data_size; refcount_t refcnt; @@ -494,6 +495,11 @@ static bool btf_type_is_modifier(const struct btf_type *t) return false; } +static int btf_start_id(const struct btf *btf) +{ + return btf->start_id + (btf->base_btf ? 0 : 1); +} + bool btf_type_is_void(const struct btf_type *t) { return t == &btf_void; @@ -544,24 +550,78 @@ u32 btf_nr_types(const struct btf *btf) return total; } +static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *name, + s32 start_id, s32 end_id) +{ + const struct btf_type *t; + const char *tname; + s32 l, r, m; + + l = start_id; + r = end_id; + while (l <= r) { + m = l + (r - l) / 2; + t = btf_type_by_id(btf, m); + tname = btf_name_by_offset(btf, t->name_off); + if (strcmp(tname, name) >= 0) { + if (l == r) + return r; + r = m; + } else { + l = m + 1; + } + } + + return btf_nr_types(btf); +} + s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) { + const struct btf *base_btf = btf_base_btf(btf); const struct btf_type *t; const char *tname; - u32 i, total; + int err = -ENOENT; - total = btf_nr_types(btf); - for (i = 1; i < total; i++) { - t = btf_type_by_id(btf, i); - if (BTF_INFO_KIND(t->info) != kind) - continue; + if (base_btf) { + err = btf_find_by_name_kind(base_btf, name, kind); + if (err > 0) + goto out; + } - tname = btf_name_by_offset(btf, t->name_off); - if (!strcmp(tname, name)) - return i; + if (btf->nr_sorted_types > 0) { + /* binary search */ + s32 start_id, end_id; + u32 idx; + + start_id = btf_start_id(btf); + end_id = start_id + btf->nr_sorted_types - 1; + idx = btf_find_by_name_kind_bsearch(btf, name, start_id, end_id); + for (; idx <= end_id; idx++) { + t = btf_type_by_id(btf, idx); + tname = btf_name_by_offset(btf, t->name_off); + if (strcmp(tname, name)) + goto out; + if (BTF_INFO_KIND(t->info) == kind) + return idx; + } + } else { + /* linear search */ + u32 i, total; + + total = btf_nr_types(btf); + for (i = btf_start_id(btf); i < total; i++) { + t = btf_type_by_id(btf, i); + if (BTF_INFO_KIND(t->info) != kind) + continue; + + tname = btf_name_by_offset(btf, t->name_off); + if (!strcmp(tname, name)) + return i; + } } - return -ENOENT; +out: + return err; } s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) @@ -5791,6 +5851,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat goto errout; } env->btf = btf; + btf->nr_sorted_types = 0; data = kvmalloc(attr->btf_size, GFP_KERNEL | __GFP_NOWARN); if (!data) { @@ -6210,6 +6271,7 @@ static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name btf->data = data; btf->data_size = data_size; btf->kernel_btf = true; + btf->nr_sorted_types = 0; snprintf(btf->name, sizeof(btf->name), "%s", name); err = btf_parse_hdr(env); @@ -6327,6 +6389,7 @@ static struct btf *btf_parse_module(const char *module_name, const void *data, btf->start_id = base_btf->nr_types; btf->start_str_off = base_btf->hdr.str_len; btf->kernel_btf = true; + btf->nr_sorted_types = 0; snprintf(btf->name, sizeof(btf->name), "%s", module_name); btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN); From 693bfba3bb6ca51d16e555d61f2a8a3bf02655b5 Mon Sep 17 00:00:00 2001 From: Donglin Peng Date: Wed, 19 Nov 2025 11:15:31 +0800 Subject: [PATCH 7/7] btf: Add sorting validation for binary search Implement validation of BTF type ordering to enable efficient binary search for sorted BTF. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- kernel/bpf/btf.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 5dd2c40d48740..e9d1023602924 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -550,6 +550,66 @@ u32 btf_nr_types(const struct btf *btf) return total; } +/* Anonymous types (with empty names) are considered greater than named types + * and are sorted after them. Two anonymous types are considered equal. Named + * types are compared lexicographically. + */ +static int btf_compare_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf = (struct btf *)priv; + const struct btf_type *ta = btf_type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb = 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 = btf_name_by_offset(btf, ta->name_off); + nb = btf_name_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +/* Verifies that BTF types are sorted in ascending order according to their + * names, with named types appearing before anonymous types. If the ordering + * is correct, counts the number of named types and updates the BTF object's + * nr_sorted_types field. Note that vmlinux and kernel module BTFs are sorted + * during the building phase, so the validation logic only needs to count the + * named types. + */ +static void btf_check_sorted(struct btf *btf) +{ + const struct btf_type *t; + int i, n, k = 0, nr_sorted_types; + bool skip_cmp = btf_is_kernel(btf); + + if (btf->nr_types < 2) + return; + + nr_sorted_types = 0; + n = btf_nr_types(btf) - 1; + for (i = btf_start_id(btf); i < n; i++) { + k = i + 1; + if (!skip_cmp && btf_compare_type_names(&i, &k, btf) > 0) + return; + + t = btf_type_by_id(btf, i); + if (t->name_off) + nr_sorted_types++; + else if (skip_cmp) + break; + } + + t = btf_type_by_id(btf, k); + if (t->name_off) + nr_sorted_types++; + if (nr_sorted_types) + btf->nr_sorted_types = nr_sorted_types; +} + static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *name, s32 start_id, s32 end_id) { @@ -5885,6 +5945,8 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat if (err) goto errout; + btf_check_sorted(btf); + struct_meta_tab = btf_parse_struct_metas(&env->log, btf); if (IS_ERR(struct_meta_tab)) { err = PTR_ERR(struct_meta_tab); @@ -6292,6 +6354,7 @@ static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name if (err) goto errout; + btf_check_sorted(btf); refcount_set(&btf->refcnt, 1); return btf; @@ -6426,6 +6489,7 @@ static struct btf *btf_parse_module(const char *module_name, const void *data, } btf_verifier_env_free(env); + btf_check_sorted(btf); refcount_set(&btf->refcnt, 1); return btf;