libbpf: fix deduplication of typedef with base definitions
PATCH 1/1 libbpf: fix deduplication of typedef with base definitions
When deduplicating definitions for a module, typedef defined in the base are not removed. This is because the hash used for base types differs from the one used in the deduplication logic in btf_dedup_struct_type.
This was introduced by the referenced commit when moving the typedef deduplication logic handling from btf_dedup_ref_type to btf_dedup_struct_type, as this also changed the hash logic (btf_hash_common to btf_hash_typedef).
This also impacts other types referencing those typedef (e.g. const). In my test, the BTF section size of the openvswitch module went from 31KB to 45KB.
--- tools/lib/bpf/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +3 -1
@@ -4578,12 +4578,14 @@ static int btf_dedup_prep(struct btf_dedup *d)case BTF_KIND_RESTRICT :case BTF_KIND_PTR :case BTF_KIND_FWD : -case BTF_KIND_TYPEDEF :case BTF_KIND_FUNC :case BTF_KIND_FLOAT :case BTF_KIND_TYPE_TAG : h =btf_hash_common (t);break ; +case BTF_KIND_TYPEDEF : + h =btf_hash_typedef (t); +break ;case BTF_KIND_INT :case BTF_KIND_DECL_TAG : h =btf_hash_int_decl_tag (t);
Signature · 1 line
2.53.0
Quoted 9 lines · Antoine Tenart
When deduplicating definitions for a module, typedef defined in the base are not removed. This is because the hash used for base types differs from the one used in the deduplication logic in
btf_dedup_struct_type.This was introduced by the referenced commit when moving the typedef deduplication logic handling from
btf_dedup_ref_typetobtf_dedup_struct_type, as this also changed the hash logic (btf_hash_commontobtf_hash_typedef).
Quoted 7 lines
This also impacts other types referencing those typedef (e.g. const). In my test, the BTF section size of the openvswitch module went from 31KB to 45KB.
Cc Paul Houssel <[email protected]>Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").Signed-off-by Antoine Tenart <[email protected]>
Nice catch! It would be great to have test coverage for this in prog_tests/btf_dedup_split.c ; adding a duplicated typedef to base and split BTF in test_split_simple() and ensuring it's removed from split BTF would do the trick I think.
Quoted 24 lines
--- tools/lib/bpf/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/lib/bpf/btf.c +3
-1@@ -4578,12 +4578,14 @@ static int btf_dedup_prep(struct btf_dedup *d)case BTF_KIND_RESTRICT :case BTF_KIND_PTR :case BTF_KIND_FWD : -case BTF_KIND_TYPEDEF :case BTF_KIND_FUNC :case BTF_KIND_FLOAT :case BTF_KIND_TYPE_TAG : h =btf_hash_common (t);break ; +case BTF_KIND_TYPEDEF : + h =btf_hash_typedef (t); +break ;case BTF_KIND_INT :case BTF_KIND_DECL_TAG : h =btf_hash_int_decl_tag (t);
Quoted 14 lines · Alan Maguire
Quoted 13 lines · Antoine Tenart
When deduplicating definitions for a module, typedef defined in the base are not removed. This is because the hash used for base types differs from the one used in the deduplication logic in
btf_dedup_struct_type.This was introduced by the referenced commit when moving the typedef deduplication logic handling from
btf_dedup_ref_typetobtf_dedup_struct_type, as this also changed the hash logic (btf_hash_commontobtf_hash_typedef).This also impacts other types referencing those typedef (e.g. const). In my test, the BTF section size of the openvswitch module went from 31KB to 45KB.
Quoted 8 lines
Quoted 3 lines
Cc Paul Houssel <[email protected]>Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").Signed-off-by Antoine Tenart <[email protected]>Nice catch! It would be great to have test coverage for this in
prog_tests/btf_dedup_split.c; adding a duplicated typedef to base and split BTF intest_split_simple()and ensuring it's removed from split BTF would do the trick I think.
Makes sense, I'll add a test for v2. Would you prefer a dedicated test or reusing an existing one?
Alan Maguire
Reviewed-byQuoted 19 lines · Antoine Tenart
Quoted 18 lines · Alan Maguire
Quoted 16 lines · Antoine Tenart
When deduplicating definitions for a module, typedef defined in the base are not removed. This is because the hash used for base types differs from the one used in the deduplication logic in
btf_dedup_struct_type.This was introduced by the referenced commit when moving the typedef deduplication logic handling from
btf_dedup_ref_typetobtf_dedup_struct_type, as this also changed the hash logic (btf_hash_commontobtf_hash_typedef).This also impacts other types referencing those typedef (e.g. const). In my test, the BTF section size of the openvswitch module went from 31KB to 45KB.
Cc Paul Houssel <[email protected]>Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").Signed-off-by Antoine Tenart <[email protected]>
Quoted 7 lines
Quoted 4 lines
Nice catch! It would be great to have test coverage for this in
prog_tests/btf_dedup_split.c; adding a duplicated typedef to base and split BTF intest_split_simple()and ensuring it's removed from split BTF would do the trick I think.Makes sense, I'll add a test for v2. Would you prefer a dedicated test or reusing an existing one?
Thanks! I'd suggest just adding identical typedefs to the base+split BTF in the existing test_split_simple() in prog_tests/btf_dedup_split.c, ensuring the deduped BTF only has one instance of the typedef (but in a separate patch from this one). That should be enough to verify this is fixed I think.
Feel free to add my
to the libbpf patch.