Skip to content

Commit 0dffb6a

Browse files
Donglin PengKernel Patches Daemon
authored andcommitted
libbpf: Extract BTF type remapping logic into helper function
Refactor btf_dedup_remap_types() by extracting its core logic into a new btf_remap_types() helper function. This eliminates code duplication and improves modularity while maintaining the same functionality. The new function encapsulates iteration over BTF types and BTF ext sections, accepting a callback for flexible type ID remapping. This makes the type remapping logic more maintainable and reusable. Cc: Eduard Zingerman <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Alan Maguire <[email protected]> Cc: Song Liu <[email protected]> Cc: Xiaoqin Zhang <[email protected]> Signed-off-by: Donglin Peng <[email protected]> Signed-off-by: Donglin Peng <[email protected]> Reviewed-by: Eduard Zingerman <[email protected]>
1 parent 2456350 commit 0dffb6a

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

tools/lib/bpf/btf.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,37 @@ int btf_ext__set_endianness(struct btf_ext *btf_ext, enum btf_endianness endian)
34003400
return 0;
34013401
}
34023402

3403+
static int btf_remap_types(struct btf *btf, struct btf_ext *btf_ext,
3404+
type_id_visit_fn visit, void *ctx)
3405+
{
3406+
int i, r;
3407+
3408+
for (i = 0; i < btf->nr_types; i++) {
3409+
struct btf_type *t = btf_type_by_id(btf, btf->start_id + i);
3410+
struct btf_field_iter it;
3411+
__u32 *type_id;
3412+
3413+
r = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS);
3414+
if (r)
3415+
return r;
3416+
3417+
while ((type_id = btf_field_iter_next(&it))) {
3418+
r = visit(type_id, ctx);
3419+
if (r)
3420+
return r;
3421+
}
3422+
}
3423+
3424+
if (!btf_ext)
3425+
return 0;
3426+
3427+
r = btf_ext_visit_type_ids(btf_ext, visit, ctx);
3428+
if (r)
3429+
return r;
3430+
3431+
return 0;
3432+
}
3433+
34033434
struct btf_dedup;
34043435

34053436
static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts);
@@ -5320,37 +5351,7 @@ static int btf_dedup_remap_type_id(__u32 *type_id, void *ctx)
53205351
*/
53215352
static int btf_dedup_remap_types(struct btf_dedup *d)
53225353
{
5323-
int i, r;
5324-
5325-
for (i = 0; i < d->btf->nr_types; i++) {
5326-
struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i);
5327-
struct btf_field_iter it;
5328-
__u32 *type_id;
5329-
5330-
r = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS);
5331-
if (r)
5332-
return r;
5333-
5334-
while ((type_id = btf_field_iter_next(&it))) {
5335-
__u32 resolved_id, new_id;
5336-
5337-
resolved_id = resolve_type_id(d, *type_id);
5338-
new_id = d->hypot_map[resolved_id];
5339-
if (new_id > BTF_MAX_NR_TYPES)
5340-
return -EINVAL;
5341-
5342-
*type_id = new_id;
5343-
}
5344-
}
5345-
5346-
if (!d->btf_ext)
5347-
return 0;
5348-
5349-
r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d);
5350-
if (r)
5351-
return r;
5352-
5353-
return 0;
5354+
return btf_remap_types(d->btf, d->btf_ext, btf_dedup_remap_type_id, d);
53545355
}
53555356

53565357
/*

0 commit comments

Comments
 (0)