Skip to content

Commit 082108f

Browse files
eddyz87anakryiko
authored andcommitted
libbpf: Resolve unambigous forward declarations
Resolve forward declarations that don't take part in type graphs comparisons if declaration name is unambiguous. Example: CU #1: struct foo; // standalone forward declaration struct foo *some_global; CU #2: struct foo { int x; }; struct foo *another_global; The `struct foo` from CU #1 is not a part of any definition that is compared against another definition while `btf_dedup_struct_types` processes structural types. The the BTF after `btf_dedup_struct_types` the BTF looks as follows: [1] STRUCT 'foo' size=4 vlen=1 ... [2] INT 'int' size=4 ... [3] PTR '(anon)' type_id=1 [4] FWD 'foo' fwd_kind=struct [5] PTR '(anon)' type_id=4 This commit adds a new pass `btf_dedup_resolve_fwds`, that maps such forward declarations to structs or unions with identical name in case if the name is not ambiguous. The pass is positioned before `btf_dedup_ref_types` so that types [3] and [5] could be merged as a same type after [1] and [4] are merged. The final result for the example above looks as follows: [1] STRUCT 'foo' size=4 vlen=1 'x' type_id=2 bits_offset=0 [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED [3] PTR '(anon)' type_id=1 For defconfig kernel with BTF enabled this removes 63 forward declarations. Examples of removed declarations: `pt_regs`, `in6_addr`. The running time of `btf__dedup` function is increased by about 3%. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Reviewed-by: Alan Maguire <alan.maguire@oracle.com> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20221109142611.879983-3-eddyz87@gmail.com
1 parent c302378 commit 082108f

1 file changed

Lines changed: 139 additions & 4 deletions

File tree

tools/lib/bpf/btf.c

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ static int btf_dedup_strings(struct btf_dedup *d);
28812881
static int btf_dedup_prim_types(struct btf_dedup *d);
28822882
static int btf_dedup_struct_types(struct btf_dedup *d);
28832883
static int btf_dedup_ref_types(struct btf_dedup *d);
2884+
static int btf_dedup_resolve_fwds(struct btf_dedup *d);
28842885
static int btf_dedup_compact_types(struct btf_dedup *d);
28852886
static int btf_dedup_remap_types(struct btf_dedup *d);
28862887

@@ -2988,15 +2989,16 @@ static int btf_dedup_remap_types(struct btf_dedup *d);
29882989
* Algorithm summary
29892990
* =================
29902991
*
2991-
* Algorithm completes its work in 6 separate passes:
2992+
* Algorithm completes its work in 7 separate passes:
29922993
*
29932994
* 1. Strings deduplication.
29942995
* 2. Primitive types deduplication (int, enum, fwd).
29952996
* 3. Struct/union types deduplication.
2996-
* 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
2997+
* 4. Resolve unambiguous forward declarations.
2998+
* 5. Reference types deduplication (pointers, typedefs, arrays, funcs, func
29972999
* protos, and const/volatile/restrict modifiers).
2998-
* 5. Types compaction.
2999-
* 6. Types remapping.
3000+
* 6. Types compaction.
3001+
* 7. Types remapping.
30003002
*
30013003
* Algorithm determines canonical type descriptor, which is a single
30023004
* representative type for each truly unique type. This canonical type is the
@@ -3060,6 +3062,11 @@ int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts)
30603062
pr_debug("btf_dedup_struct_types failed:%d\n", err);
30613063
goto done;
30623064
}
3065+
err = btf_dedup_resolve_fwds(d);
3066+
if (err < 0) {
3067+
pr_debug("btf_dedup_resolve_fwds failed:%d\n", err);
3068+
goto done;
3069+
}
30633070
err = btf_dedup_ref_types(d);
30643071
if (err < 0) {
30653072
pr_debug("btf_dedup_ref_types failed:%d\n", err);
@@ -4501,6 +4508,134 @@ static int btf_dedup_ref_types(struct btf_dedup *d)
45014508
return 0;
45024509
}
45034510

4511+
/*
4512+
* Collect a map from type names to type ids for all canonical structs
4513+
* and unions. If the same name is shared by several canonical types
4514+
* use a special value 0 to indicate this fact.
4515+
*/
4516+
static int btf_dedup_fill_unique_names_map(struct btf_dedup *d, struct hashmap *names_map)
4517+
{
4518+
__u32 nr_types = btf__type_cnt(d->btf);
4519+
struct btf_type *t;
4520+
__u32 type_id;
4521+
__u16 kind;
4522+
int err;
4523+
4524+
/*
4525+
* Iterate over base and split module ids in order to get all
4526+
* available structs in the map.
4527+
*/
4528+
for (type_id = 1; type_id < nr_types; ++type_id) {
4529+
t = btf_type_by_id(d->btf, type_id);
4530+
kind = btf_kind(t);
4531+
4532+
if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4533+
continue;
4534+
4535+
/* Skip non-canonical types */
4536+
if (type_id != d->map[type_id])
4537+
continue;
4538+
4539+
err = hashmap__add(names_map, t->name_off, type_id);
4540+
if (err == -EEXIST)
4541+
err = hashmap__set(names_map, t->name_off, 0, NULL, NULL);
4542+
4543+
if (err)
4544+
return err;
4545+
}
4546+
4547+
return 0;
4548+
}
4549+
4550+
static int btf_dedup_resolve_fwd(struct btf_dedup *d, struct hashmap *names_map, __u32 type_id)
4551+
{
4552+
struct btf_type *t = btf_type_by_id(d->btf, type_id);
4553+
enum btf_fwd_kind fwd_kind = btf_kflag(t);
4554+
__u16 cand_kind, kind = btf_kind(t);
4555+
struct btf_type *cand_t;
4556+
uintptr_t cand_id;
4557+
4558+
if (kind != BTF_KIND_FWD)
4559+
return 0;
4560+
4561+
/* Skip if this FWD already has a mapping */
4562+
if (type_id != d->map[type_id])
4563+
return 0;
4564+
4565+
if (!hashmap__find(names_map, t->name_off, &cand_id))
4566+
return 0;
4567+
4568+
/* Zero is a special value indicating that name is not unique */
4569+
if (!cand_id)
4570+
return 0;
4571+
4572+
cand_t = btf_type_by_id(d->btf, cand_id);
4573+
cand_kind = btf_kind(cand_t);
4574+
if ((cand_kind == BTF_KIND_STRUCT && fwd_kind != BTF_FWD_STRUCT) ||
4575+
(cand_kind == BTF_KIND_UNION && fwd_kind != BTF_FWD_UNION))
4576+
return 0;
4577+
4578+
d->map[type_id] = cand_id;
4579+
4580+
return 0;
4581+
}
4582+
4583+
/*
4584+
* Resolve unambiguous forward declarations.
4585+
*
4586+
* The lion's share of all FWD declarations is resolved during
4587+
* `btf_dedup_struct_types` phase when different type graphs are
4588+
* compared against each other. However, if in some compilation unit a
4589+
* FWD declaration is not a part of a type graph compared against
4590+
* another type graph that declaration's canonical type would not be
4591+
* changed. Example:
4592+
*
4593+
* CU #1:
4594+
*
4595+
* struct foo;
4596+
* struct foo *some_global;
4597+
*
4598+
* CU #2:
4599+
*
4600+
* struct foo { int u; };
4601+
* struct foo *another_global;
4602+
*
4603+
* After `btf_dedup_struct_types` the BTF looks as follows:
4604+
*
4605+
* [1] STRUCT 'foo' size=4 vlen=1 ...
4606+
* [2] INT 'int' size=4 ...
4607+
* [3] PTR '(anon)' type_id=1
4608+
* [4] FWD 'foo' fwd_kind=struct
4609+
* [5] PTR '(anon)' type_id=4
4610+
*
4611+
* This pass assumes that such FWD declarations should be mapped to
4612+
* structs or unions with identical name in case if the name is not
4613+
* ambiguous.
4614+
*/
4615+
static int btf_dedup_resolve_fwds(struct btf_dedup *d)
4616+
{
4617+
int i, err;
4618+
struct hashmap *names_map;
4619+
4620+
names_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL);
4621+
if (IS_ERR(names_map))
4622+
return PTR_ERR(names_map);
4623+
4624+
err = btf_dedup_fill_unique_names_map(d, names_map);
4625+
if (err < 0)
4626+
goto exit;
4627+
4628+
for (i = 0; i < d->btf->nr_types; i++) {
4629+
err = btf_dedup_resolve_fwd(d, names_map, d->btf->start_id + i);
4630+
if (err < 0)
4631+
break;
4632+
}
4633+
4634+
exit:
4635+
hashmap__free(names_map);
4636+
return err;
4637+
}
4638+
45044639
/*
45054640
* Compact types.
45064641
*

0 commit comments

Comments
 (0)