* [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions
@ 2026-04-17 8:33 Antoine Tenart
2026-04-17 8:33 ` [PATCH bpf v2 2/2] selftests/bpf: ensure typedef are deduplicated in split BTF Antoine Tenart
2026-04-17 22:10 ` [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Alan Maguire
0 siblings, 2 replies; 3+ messages in thread
From: Antoine Tenart @ 2026-04-17 8:33 UTC (permalink / raw)
To: andrii, eddyz87; +Cc: Antoine Tenart, bpf, Paul Houssel, Alan Maguire
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 <paulhoussel2@gmail.com>
Fixes: 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions").
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Antoine Tenart <atenart@kernel.org>
---
tools/lib/bpf/btf.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index ceb57b46a878..771aeaa0262b 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -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.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH bpf v2 2/2] selftests/bpf: ensure typedef are deduplicated in split BTF 2026-04-17 8:33 [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Antoine Tenart @ 2026-04-17 8:33 ` Antoine Tenart 2026-04-17 22:10 ` [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Alan Maguire 1 sibling, 0 replies; 3+ messages in thread From: Antoine Tenart @ 2026-04-17 8:33 UTC (permalink / raw) To: andrii, eddyz87; +Cc: Antoine Tenart, bpf, Alan Maguire If a typedef is defined both in a base and in a split BTF, after deduplication a single instance should be found in the base BTF. Suggested-by: Alan Maguire <alan.maguire@oracle.com> Signed-off-by: Antoine Tenart <atenart@kernel.org> --- Since v1: - Added this patch. .../bpf/prog_tests/btf_dedup_split.c | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c index 5bc15bb6b7ce..0db7707ae90d 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c @@ -20,18 +20,22 @@ static void test_split_simple() { btf__add_struct(btf1, "s1", 4); /* [3] struct s1 { */ btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */ /* } */ + btf__add_typedef(btf1, "t1", 1); /* [4] typedef int */ VALIDATE_RAW_BTF( btf1, "[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'f1' type_id=1 bits_offset=0"); + "\t'f1' type_id=1 bits_offset=0", + "[4] TYPEDEF 't1' type_id=1"); ASSERT_STREQ(btf_type_c_dump(btf1), "\ struct s1 {\n\ int f1;\n\ -};\n\n", "c_dump"); +};\n\ +\n\ +typedef int t1;\n\n", "c_dump"); btf2 = btf__new_empty_split(btf1); if (!ASSERT_OK_PTR(btf2, "empty_split_btf")) @@ -49,39 +53,46 @@ struct s1 {\n\ ASSERT_EQ(btf_is_int(t), true, "int_kind"); ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "int", "int_name"); - btf__add_struct(btf2, "s2", 16); /* [4] struct s2 { */ - btf__add_field(btf2, "f1", 6, 0, 0); /* struct s1 f1; */ - btf__add_field(btf2, "f2", 5, 32, 0); /* int f2; */ + btf__add_struct(btf2, "s2", 16); /* [5] struct s2 { */ + btf__add_field(btf2, "f1", 7, 0, 0); /* struct s1 f1; */ + btf__add_field(btf2, "f2", 6, 32, 0); /* int f2; */ btf__add_field(btf2, "f3", 2, 64, 0); /* int *f3; */ /* } */ /* duplicated int */ - btf__add_int(btf2, "int", 4, BTF_INT_SIGNED); /* [5] int */ + btf__add_int(btf2, "int", 4, BTF_INT_SIGNED); /* [6] int */ /* duplicated struct s1 */ - btf__add_struct(btf2, "s1", 4); /* [6] struct s1 { */ - btf__add_field(btf2, "f1", 5, 0, 0); /* int f1; */ + btf__add_struct(btf2, "s1", 4); /* [7] struct s1 { */ + btf__add_field(btf2, "f1", 6, 0, 0); /* int f1; */ /* } */ + /* duplicated typedef t1 */ + btf__add_typedef(btf2, "t1", 6); /* [8] typedef int */ + VALIDATE_RAW_BTF( btf2, "[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'f1' type_id=1 bits_offset=0", - "[4] STRUCT 's2' size=16 vlen=3\n" - "\t'f1' type_id=6 bits_offset=0\n" - "\t'f2' type_id=5 bits_offset=32\n" + "[4] TYPEDEF 't1' type_id=1", + "[5] STRUCT 's2' size=16 vlen=3\n" + "\t'f1' type_id=7 bits_offset=0\n" + "\t'f2' type_id=6 bits_offset=32\n" "\t'f3' type_id=2 bits_offset=64", - "[5] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", - "[6] STRUCT 's1' size=4 vlen=1\n" - "\t'f1' type_id=5 bits_offset=0"); + "[6] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", + "[7] STRUCT 's1' size=4 vlen=1\n" + "\t'f1' type_id=6 bits_offset=0", + "[8] TYPEDEF 't1' type_id=6"); ASSERT_STREQ(btf_type_c_dump(btf2), "\ struct s1 {\n\ int f1;\n\ };\n\ \n\ +typedef int t1;\n\ +\n\ struct s1___2 {\n\ int f1;\n\ };\n\ @@ -90,7 +101,9 @@ struct s2 {\n\ struct s1___2 f1;\n\ int f2;\n\ int *f3;\n\ -};\n\n", "c_dump"); +};\n\ +\n\ +typedef int t1___2;\n\n", "c_dump"); err = btf__dedup(btf2, NULL); if (!ASSERT_OK(err, "btf_dedup")) @@ -102,7 +115,8 @@ struct s2 {\n\ "[2] PTR '(anon)' type_id=1", "[3] STRUCT 's1' size=4 vlen=1\n" "\t'f1' type_id=1 bits_offset=0", - "[4] STRUCT 's2' size=16 vlen=3\n" + "[4] TYPEDEF 't1' type_id=1", + "[5] STRUCT 's2' size=16 vlen=3\n" "\t'f1' type_id=3 bits_offset=0\n" "\t'f2' type_id=1 bits_offset=32\n" "\t'f3' type_id=2 bits_offset=64"); @@ -112,6 +126,8 @@ struct s1 {\n\ int f1;\n\ };\n\ \n\ +typedef int t1;\n\ +\n\ struct s2 {\n\ struct s1 f1;\n\ int f2;\n\ -- 2.53.0 ^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions 2026-04-17 8:33 [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Antoine Tenart 2026-04-17 8:33 ` [PATCH bpf v2 2/2] selftests/bpf: ensure typedef are deduplicated in split BTF Antoine Tenart @ 2026-04-17 22:10 ` Alan Maguire 1 sibling, 0 replies; 3+ messages in thread From: Alan Maguire @ 2026-04-17 22:10 UTC (permalink / raw) To: Antoine Tenart, andrii, eddyz87; +Cc: bpf, Paul Houssel On 17/04/2026 09:33, Antoine Tenart wrote: > 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 <paulhoussel2@gmail.com> > Fixes: 3781413465df ("libbpf: Fix BTF dedup to support recursive typedef definitions"). > Reviewed-by: Alan Maguire <alan.maguire@oracle.com> > Signed-off-by: Antoine Tenart <atenart@kernel.org> Thanks; verified that this patch resolves issues with duplicated typedefs in modules. Selftest in patch 2 fails prior to this patch, and succeeds after it is applied. So for the series Tested-by: Alan Maguire <alan.maguire@oracle.com> > --- > tools/lib/bpf/btf.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > index ceb57b46a878..771aeaa0262b 100644 > --- a/tools/lib/bpf/btf.c > +++ b/tools/lib/bpf/btf.c > @@ -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); ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-04-17 22:10 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-04-17 8:33 [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Antoine Tenart 2026-04-17 8:33 ` [PATCH bpf v2 2/2] selftests/bpf: ensure typedef are deduplicated in split BTF Antoine Tenart 2026-04-17 22:10 ` [PATCH bpf v2 1/2] libbpf: fix deduplication of typedef with base definitions Alan Maguire
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox