libbpf: Resolve enum fwd as full enum64 and vice versa
Subsystems (4)
Eduard Zingerman
maintainerPATCH 1/2 libbpf: Resolve enum fwd as full enum64 and vice versa
Changes de-duplication logic for enums in the following way:
- update btf_hash_enum to ignore size and kind fields to get ENUM and ENUM64 types in a same hash bucket;
- update btf_compat_enum to consider enum fwd to be compatible with full enum64 (and vice versa);
This allows BTF de-duplication in the following case:
// CU #1
enum foo;
struct s {
enum foo *a;
} *x;
// CU #2
enum foo {
x = 0xfffffffff // big enough to force enum64
};
struct s {
enum foo *a;
} *y;De-duplicated BTF prior to this commit:
[1] ENUM64 'foo' encoding=UNSIGNED size=8 vlen=1
'x' val=68719476735ULL
[2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64
encoding=(none)
[3] STRUCT 's' size=8 vlen=1
'a' type_id=4 bits_offset=0
[4] PTR '(anon)' type_id=1
[5] PTR '(anon)' type_id=3
[6] STRUCT 's' size=8 vlen=1
'a' type_id=8 bits_offset=0
[7] ENUM 'foo' encoding=UNSIGNED size=4 vlen=0
[8] PTR '(anon)' type_id=7
[9] PTR '(anon)' type_id=6De-duplicated BTF after this commit:
[1] ENUM64 'foo' encoding=UNSIGNED size=8 vlen=1
'x' val=68719476735ULL
[2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64
encoding=(none)
[3] STRUCT 's' size=8 vlen=1
'a' type_id=4 bits_offset=0
[4] PTR '(anon)' type_id=1
[5] PTR '(anon)' type_id=3Enum forward declarations in C do not provide information about enumeration values range. Thus the btf_type->size field is meaningless for forward enum declarations. In fact, GCC does not encode size in DWARF for forward enum declarations
(but dwarves sets enumeration size to a default value of sizeof(int) * 8 when size is not specified see dwarf_loader.c:die__create_new_enumeration).
--- tools/lib/bpf/btf.c | 75 +++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 50 deletions(-)
tools/lib/bpf/btf.c +25 -50
@@ -3404,23 +3404,17 @@ static long btf_hash_enum(struct btf_type *t) {long h; -/* don't hash vlen and enum members to support enum fwd resolving */ +/* don't hash vlen, enum members and size to support enum fwd resolving */ h =hash_combine (0 , t->name_off); - h =hash_combine (h, t->info & ~0xffff ); - h =hash_combine (h, t->size);return h; } -/* Check structural equality of two ENUMs. */ -static bool btf_equal_enum (struct btf_type *t1,struct btf_type *t2) +static bool btf_equal_enum_members (struct btf_type *t1,struct btf_type *t2) {const struct btf_enum *m1, *m2;__u16 vlen;int i; -if (!btf_equal_common (t1, t2)) -return false; - vlen =btf_vlen (t1); m1 =btf_enum (t1); m2 =btf_enum (t2);
@@ -3433,15 +3427,12 @@ static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)return true; } -static bool btf_equal_enum64 (struct btf_type *t1,struct btf_type *t2) +static bool btf_equal_enum64_members (struct btf_type *t1,struct btf_type *t2) {const struct btf_enum64 *m1, *m2;__u16 vlen;int i; -if (!btf_equal_common (t1, t2)) -return false; - vlen =btf_vlen (t1); m1 =btf_enum64 (t1); m2 =btf_enum64 (t2);
@@ -3455,6 +3446,19 @@ static bool btf_equal_enum64(struct btf_type *t1, struct btf_type *t2)return true; } +/* Check structural equality of two ENUMs. */ +static bool btf_equal_enum (struct btf_type *t1,struct btf_type *t2) +{ +if (!btf_equal_common (t1, t2)) +return false; + +/* t1 & t2 kinds are identical because of btf_equal_common */ +if (btf_kind (t1) ==BTF_KIND_ENUM ) +return btf_equal_enum_members (t1, t2); +else +return btf_equal_enum64_members (t1, t2); +} +static inline bool btf_is_enum_fwd (struct btf_type *t) {return btf_is_any_enum (t) &&btf_vlen (t) ==0 ;
@@ -3464,21 +3468,14 @@ static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2) {if (!btf_is_enum_fwd (t1) && !btf_is_enum_fwd (t2))return btf_equal_enum (t1, t2); -/* ignore vlen when comparing */ -return t1->name_off == t2->name_off && - (t1->info & ~0xffff ) == (t2->info & ~0xffff ) && - t1->size == t2->size; -} - -static bool btf_compat_enum64 (struct btf_type *t1,struct btf_type *t2) -{ -if (!btf_is_enum_fwd (t1) && !btf_is_enum_fwd (t2)) -return btf_equal_enum64 (t1, t2); - -/* ignore vlen when comparing */ +/* At this point either t1 or t2 or both are forward declarations, thus: +* - skip comparing vlen because it is zero for forward declarations; +* - skip comparing size to allow enum forward declarations +* to be compatible with enum64 full declarations; +* - skip comparing kind for the same reason. +*/ return t1->name_off == t2->name_off && - (t1->info & ~0xffff ) == (t2->info & ~0xffff ) && - t1->size == t2->size; +btf_is_any_enum (t1) &&btf_is_any_enum (t2); }/*
@@ -3763,6 +3760,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)break ;case BTF_KIND_ENUM : +case BTF_KIND_ENUM64 : h =btf_hash_enum (t);for_each_dedup_cand (d, hash_entry, h) { cand_id = (__u32 )(long )hash_entry->value;
@@ -3783,27 +3781,6 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) }break ; -case BTF_KIND_ENUM64 : - h =btf_hash_enum (t); -for_each_dedup_cand (d, hash_entry, h) { - cand_id = (__u32 )(long )hash_entry->value; - cand =btf_type_by_id (d->btf, cand_id); -if (btf_equal_enum64 (t, cand)) { - new_id = cand_id; -break ; - } -if (btf_compat_enum64 (t, cand)) { -if (btf_is_enum_fwd (t)) { -/* resolve fwd to full enum */ - new_id = cand_id; -break ; - } -/* resolve canonical enum fwd to full enum */ - d->map[cand_id] = type_id; - } - } -break ; -case BTF_KIND_FWD :case BTF_KIND_FLOAT : h =btf_hash_common (t);
@@ -4099,10 +4076,8 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,return btf_equal_int_tag (cand_type, canon_type);case BTF_KIND_ENUM : -return btf_compat_enum (cand_type, canon_type); -case BTF_KIND_ENUM64 : -return btf_compat_enum64 (cand_type, canon_type); +return btf_compat_enum (cand_type, canon_type);case BTF_KIND_FWD :case BTF_KIND_FLOAT :
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerPATCH 2/2 selftests/bpf: Tests for enum fwd resolved as full enum64
A set of test cases to verify enum fwd resolution logic:
- verify that enum fwd can be resolved as full enum64;
- verify that enum64 fwd can be resolved as full enum;
- verify that enum size is considered when enums are compared for equivalence.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 88 ++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +83 -5
@@ -7120,7 +7120,7 @@ static struct btf_dedup_test dedup_tests[] = {BTF_ENUM_ENC (NAME_NTH (4 ),456 ),/* [4] fwd enum 'e2' after full enum */ BTF_TYPE_ENC (NAME_NTH (3 ),BTF_INFO_ENC (BTF_KIND_ENUM ,0 ,0 ),4 ), -/* [5] incompatible fwd enum with different size */ +/* [5] fwd enum with different size, size does not matter for fwd */ BTF_TYPE_ENC (NAME_NTH (1 ),BTF_INFO_ENC (BTF_KIND_ENUM ,0 ,0 ),1 ),/* [6] incompatible full enum with different value */ BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
@@ -7137,9 +7137,7 @@ static struct btf_dedup_test dedup_tests[] = {/* [2] full enum 'e2' */ BTF_TYPE_ENC (NAME_NTH (3 ),BTF_INFO_ENC (BTF_KIND_ENUM ,0 ,1 ),4 ),BTF_ENUM_ENC (NAME_NTH (4 ),456 ), -/* [3] incompatible fwd enum with different size */ -BTF_TYPE_ENC (NAME_NTH (1 ),BTF_INFO_ENC (BTF_KIND_ENUM ,0 ,0 ),1 ), -/* [4] incompatible full enum with different value */ +/* [3] incompatible full enum with different value */ BTF_TYPE_ENC (NAME_NTH (1 ),BTF_INFO_ENC (BTF_KIND_ENUM ,0 ,1 ),4 ),BTF_ENUM_ENC (NAME_NTH (2 ),321 ), BTF_END_RAW,
@@ -7598,7 +7596,87 @@ static struct btf_dedup_test dedup_tests[] = { BTF_STR_SEC("\0e1\0e1_val"), }, }, - +{ + .descr = "dedup: enum of different size: no dedup", + .input = { + .raw_types = { + /* [1] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), + BTF_ENUM_ENC(NAME_NTH(2), 1), + /* [2] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2), + BTF_ENUM_ENC(NAME_NTH(2), 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val"), + }, + .expect = { + .raw_types = { + /* [1] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), + BTF_ENUM_ENC(NAME_NTH(2), 1), + /* [2] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2), + BTF_ENUM_ENC(NAME_NTH(2), 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val"), + }, +}, +{ + .descr = "dedup: enum fwd to enum64", + .input = { + .raw_types = { + /* [1] enum64 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8), + BTF_ENUM64_ENC(NAME_NTH(2), 1, 0), + /* [2] enum 'e1' fwd */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4), + /* [3] typedef enum 'e1' td */ + BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val\0td"), + }, + .expect = { + .raw_types = { + /* [1] enum64 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8), + BTF_ENUM64_ENC(NAME_NTH(2), 1, 0), + /* [2] typedef enum 'e1' td */ + BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val\0td"), + }, +}, +{ + .descr = "dedup: enum64 fwd to enum", + .input = { + .raw_types = { + /* [1] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), + BTF_ENUM_ENC(NAME_NTH(2), 1), + /* [2] enum64 'e1' fwd */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8), + /* [3] typedef enum 'e1' td */ + BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val\0td"), + }, + .expect = { + .raw_types = { + /* [1] enum 'e1' */ + BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), + BTF_ENUM_ENC(NAME_NTH(2), 1), + /* [2] typedef enum 'e1' td */ + BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0e1\0e1_val\0td"), + }, +}, }; static int btf_type_size(const struct btf_type *t)
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Wed, 2 Nov 2022 01:54:12 +0200 you wrote:
Changes de-duplication logic for enums in the following way:
- updatebtf_hash_enumto ignore size and kind fields to get ENUM and ENUM64 types in a same hash bucket;
- updatebtf_compat_enumto consider enum fwd to be compatible with full enum64 (and vice versa);This allows BTF de-duplication in the following case:
[...]
Here is the summary with links:
- [bpf-next,1/2] libbpf: Resolve enum fwd as full enum64 and vice versa
https://git.kernel.org/bpf/bpf-next/c/de048b6ee865 - [bpf-next,2/2] selftests/bpf: Tests for enum fwd resolved as full enum64 https://git.kernel.org/bpf/bpf-next/c/2e20f50ff849
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html