Skip to contentSkip to conversation

libbpf: fix deduplication of typedef with base definitions

PATCH Superseded Antoine Tenart 2 participants 4 messages Apr 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

Cc Paul Houssel <[email protected]>
Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").
---
 tools/lib/bpf/btf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +3 -1
index ceb57b46a878..771aeaa0262b 100644
@@ -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
#1 lore raw
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_type to btf_dedup_struct_type, as this also changed the hash logic (btf_hash_common to btf_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").

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
index ceb57b46a878..771aeaa0262b 100644
@@ -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);
#2 lore raw
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_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.

Quoted 8 lines
Quoted 3 lines
Cc Paul Houssel <[email protected]>
Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").

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.

Makes sense, I'll add a test for v2. Would you prefer a dedicated test or reusing an existing one?

Reviewed-by
#3 lore raw
Quoted 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_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.

Cc Paul Houssel <[email protected]>
Fixes 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").
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 in test_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.