diff --git a/backport-bpf-Add-btf-enum64-support.patch b/backport-bpf-Add-btf-enum64-support.patch new file mode 100644 index 0000000000000000000000000000000000000000..dccfe54797b44562f1248caf830e6bb7921755f4 --- /dev/null +++ b/backport-bpf-Add-btf-enum64-support.patch @@ -0,0 +1,107 @@ +From 9a976c6b98de8da85341b341adb9be5b9850ad10 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:00 -0700 +Subject: [PATCH] bpf: Add btf enum64 support + +Currently, BTF only supports upto 32bit enum value with BTF_KIND_ENUM. +But in kernel, some enum indeed has 64bit values, e.g., +in uapi bpf.h, we have + enum { + BPF_F_INDEX_MASK = 0xffffffffULL, + BPF_F_CURRENT_CPU = BPF_F_INDEX_MASK, + BPF_F_CTXLEN_MASK = (0xfffffULL << 32), + }; +In this case, BTF_KIND_ENUM will encode the value of BPF_F_CTXLEN_MASK +as 0, which certainly is incorrect. + +This patch added a new btf kind, BTF_KIND_ENUM64, which permits +64bit value to cover the above use case. The BTF_KIND_ENUM64 has +the following three fields followed by the common type: + struct bpf_enum64 { + __u32 nume_off; + __u32 val_lo32; + __u32 val_hi32; + }; +Currently, btf type section has an alignment of 4 as all element types +are u32. Representing the value with __u64 will introduce a pad +for bpf_enum64 and may also introduce misalignment for the 64bit value. +Hence, two members of val_hi32 and val_lo32 are chosen to avoid these issues. + +The kflag is also introduced for BTF_KIND_ENUM and BTF_KIND_ENUM64 +to indicate whether the value is signed or unsigned. The kflag intends +to provide consistent output of BTF C fortmat with the original +source code. For example, the original BTF_KIND_ENUM bit value is 0xffffffff. +The format C has two choices, printing out 0xffffffff or -1 and current libbpf +prints out as unsigned value. But if the signedness is preserved in btf, +the value can be printed the same as the original source code. +The kflag value 0 means unsigned values, which is consistent to the default +by libbpf and should also cover most cases as well. + +The new BTF_KIND_ENUM64 is intended to support the enum value represented as +64bit value. But it can represent all BTF_KIND_ENUM values as well. +The compiler ([1]) and pahole will generate BTF_KIND_ENUM64 only if the value has +to be represented with 64 bits. + +In addition, a static inline function btf_kind_core_compat() is introduced which +will be used later when libbpf relo_core.c changed. Here the kernel shares the +same relo_core.c with libbpf. + + [1] https://reviews.llvm.org/D124641 + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062600.3716578-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + include/uapi/linux/btf.h | 17 ++++++++++++++--- + 1 file changed, 14 insertions(+), 3 deletions(-) + +diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h +index a9162a6c0..ec1798b6d 100644 +--- a/include/uapi/linux/btf.h ++++ b/include/uapi/linux/btf.h +@@ -36,10 +36,10 @@ struct btf_type { + * bits 24-28: kind (e.g. int, ptr, array...etc) + * bits 29-30: unused + * bit 31: kind_flag, currently used by +- * struct, union and fwd ++ * struct, union, enum, fwd and enum64 + */ + __u32 info; +- /* "size" is used by INT, ENUM, STRUCT, UNION and DATASEC. ++ /* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. + * "size" tells the size of the type it is describing. + * + * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, +@@ -63,7 +63,7 @@ enum { + BTF_KIND_ARRAY = 3, /* Array */ + BTF_KIND_STRUCT = 4, /* Struct */ + BTF_KIND_UNION = 5, /* Union */ +- BTF_KIND_ENUM = 6, /* Enumeration */ ++ BTF_KIND_ENUM = 6, /* Enumeration up to 32-bit values */ + BTF_KIND_FWD = 7, /* Forward */ + BTF_KIND_TYPEDEF = 8, /* Typedef */ + BTF_KIND_VOLATILE = 9, /* Volatile */ +@@ -76,6 +76,7 @@ enum { + BTF_KIND_FLOAT = 16, /* Floating point */ + BTF_KIND_DECL_TAG = 17, /* Decl Tag */ + BTF_KIND_TYPE_TAG = 18, /* Type Tag */ ++ BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */ + + NR_BTF_KINDS, + BTF_KIND_MAX = NR_BTF_KINDS - 1, +@@ -186,4 +187,14 @@ struct btf_decl_tag { + __s32 component_idx; + }; + ++/* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64". ++ * The exact number of btf_enum64 is stored in the vlen (of the ++ * info in "struct btf_type"). ++ */ ++struct btf_enum64 { ++ __u32 name_off; ++ __u32 val_lo32; ++ __u32 val_hi32; ++}; ++ + #endif /* _UAPI__LINUX_BTF_H__ */ diff --git a/backport-libbpf-Add-enum64-deduplication-support.patch b/backport-libbpf-Add-enum64-deduplication-support.patch new file mode 100644 index 0000000000000000000000000000000000000000..5dcb48a89ed4dbb048edf678a5b5417b42f082b2 --- /dev/null +++ b/backport-libbpf-Add-enum64-deduplication-support.patch @@ -0,0 +1,143 @@ +From 25238de149fc6c187a245e615aadfecdbd7a559e Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:26 -0700 +Subject: [PATCH] libbpf: Add enum64 deduplication support + +Add enum64 deduplication support. BTF_KIND_ENUM64 handling +is very similar to BTF_KIND_ENUM. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062626.3720166-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- + src/btf.h | 5 +++++ + 2 files changed, 65 insertions(+), 2 deletions(-) + +diff --git a/src/btf.c b/src/btf.c +index 8676efb2b..ae1520f7e 100644 +--- a/src/btf.c ++++ b/src/btf.c +@@ -3582,7 +3582,7 @@ static bool btf_equal_int_tag(struct btf_type *t1, struct btf_type *t2) + return info1 == info2; + } + +-/* Calculate type signature hash of ENUM. */ ++/* Calculate type signature hash of ENUM/ENUM64. */ + static long btf_hash_enum(struct btf_type *t) + { + long h; +@@ -3616,9 +3616,31 @@ 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) ++{ ++ 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); ++ for (i = 0; i < vlen; i++) { ++ if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 || ++ m1->val_hi32 != m2->val_hi32) ++ return false; ++ m1++; ++ m2++; ++ } ++ return true; ++} ++ + static inline bool btf_is_enum_fwd(struct btf_type *t) + { +- return btf_is_enum(t) && btf_vlen(t) == 0; ++ return btf_is_any_enum(t) && btf_vlen(t) == 0; + } + + static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2) +@@ -3631,6 +3653,17 @@ static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2) + 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 */ ++ return t1->name_off == t2->name_off && ++ (t1->info & ~0xffff) == (t2->info & ~0xffff) && ++ t1->size == t2->size; ++} ++ + /* + * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs, + * as referenced type IDs equivalence is established separately during type +@@ -3843,6 +3876,7 @@ static int btf_dedup_prep(struct btf_dedup *d) + h = btf_hash_int_decl_tag(t); + break; + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + h = btf_hash_enum(t); + break; + case BTF_KIND_STRUCT: +@@ -3932,6 +3966,27 @@ 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); +@@ -4227,6 +4282,9 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, + case BTF_KIND_ENUM: + return btf_compat_enum(cand_type, canon_type); + ++ case BTF_KIND_ENUM64: ++ return btf_compat_enum64(cand_type, canon_type); ++ + case BTF_KIND_FWD: + case BTF_KIND_FLOAT: + return btf_equal_common(cand_type, canon_type); +diff --git a/src/btf.h b/src/btf.h +index a41463bf9..c7e8b1fdf 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -531,6 +531,11 @@ static inline bool btf_is_type_tag(const struct btf_type *t) + return btf_kind(t) == BTF_KIND_TYPE_TAG; + } + ++static inline bool btf_is_any_enum(const struct btf_type *t) ++{ ++ return btf_is_enum(t) || btf_is_enum64(t); ++} ++ + static inline __u8 btf_int_encoding(const struct btf_type *t) + { + return BTF_INT_ENCODING(*(__u32 *)(t + 1)); diff --git a/backport-libbpf-Add-enum64-parsing-and-new-enum64-public-API.patch b/backport-libbpf-Add-enum64-parsing-and-new-enum64-public-API.patch new file mode 100644 index 0000000000000000000000000000000000000000..4addba2ae325991ed385e5ef472c813570c09bcd --- /dev/null +++ b/backport-libbpf-Add-enum64-parsing-and-new-enum64-public-API.patch @@ -0,0 +1,244 @@ +From c3f8eecb16ad02ebb3a41a31349f1b851cebe46b Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:21 -0700 +Subject: [PATCH] libbpf: Add enum64 parsing and new enum64 public API + +Add enum64 parsing support and two new enum64 public APIs: + btf__add_enum64 + btf__add_enum64_value + +Also add support of signedness for BTF_KIND_ENUM. The +BTF_KIND_ENUM API signatures are not changed. The signedness +will be changed from unsigned to signed if btf__add_enum_value() +finds any negative values. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062621.3719391-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ + src/btf.h | 12 ++++++ + src/libbpf.map | 2 + + 3 files changed, 117 insertions(+) + +diff --git a/src/btf.c b/src/btf.c +index 1972d0c65..8676efb2b 100644 +--- a/src/btf.c ++++ b/src/btf.c +@@ -305,6 +305,8 @@ static int btf_type_size(const struct btf_type *t) + return base_size + sizeof(__u32); + case BTF_KIND_ENUM: + return base_size + vlen * sizeof(struct btf_enum); ++ case BTF_KIND_ENUM64: ++ return base_size + vlen * sizeof(struct btf_enum64); + case BTF_KIND_ARRAY: + return base_size + sizeof(struct btf_array); + case BTF_KIND_STRUCT: +@@ -334,6 +336,7 @@ static void btf_bswap_type_base(struct btf_type *t) + static int btf_bswap_type_rest(struct btf_type *t) + { + struct btf_var_secinfo *v; ++ struct btf_enum64 *e64; + struct btf_member *m; + struct btf_array *a; + struct btf_param *p; +@@ -361,6 +364,13 @@ static int btf_bswap_type_rest(struct btf_type *t) + e->val = bswap_32(e->val); + } + return 0; ++ case BTF_KIND_ENUM64: ++ for (i = 0, e64 = btf_enum64(t); i < vlen; i++, e64++) { ++ e64->name_off = bswap_32(e64->name_off); ++ e64->val_lo32 = bswap_32(e64->val_lo32); ++ e64->val_hi32 = bswap_32(e64->val_hi32); ++ } ++ return 0; + case BTF_KIND_ARRAY: + a = btf_array(t); + a->type = bswap_32(a->type); +@@ -611,6 +621,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_DATASEC: + case BTF_KIND_FLOAT: + size = t->size; +@@ -658,6 +669,7 @@ int btf__align_of(const struct btf *btf, __u32 id) + switch (kind) { + case BTF_KIND_INT: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FLOAT: + return min(btf_ptr_sz(btf), (size_t)t->size); + case BTF_KIND_PTR: +@@ -2176,6 +2188,10 @@ static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz, + */ + int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz) + { ++ /* ++ * set the signedness to be unsigned, it will change to signed ++ * if any later enumerator is negative. ++ */ + return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM); + } + +@@ -2226,6 +2242,82 @@ int btf__add_enum_value(struct btf *btf, const char *name, __s64 value) + t = btf_last_type(btf); + btf_type_inc_vlen(t); + ++ /* if negative value, set signedness to signed */ ++ if (value < 0) ++ t->info = btf_type_info(btf_kind(t), btf_vlen(t), true); ++ ++ btf->hdr->type_len += sz; ++ btf->hdr->str_off += sz; ++ return 0; ++} ++ ++/* ++ * Append new BTF_KIND_ENUM64 type with: ++ * - *name* - name of the enum, can be NULL or empty for anonymous enums; ++ * - *byte_sz* - size of the enum, in bytes. ++ * - *is_signed* - whether the enum values are signed or not; ++ * ++ * Enum initially has no enum values in it (and corresponds to enum forward ++ * declaration). Enumerator values can be added by btf__add_enum64_value() ++ * immediately after btf__add_enum64() succeeds. ++ * ++ * Returns: ++ * - >0, type ID of newly added BTF type; ++ * - <0, on error. ++ */ ++int btf__add_enum64(struct btf *btf, const char *name, __u32 byte_sz, ++ bool is_signed) ++{ ++ return btf_add_enum_common(btf, name, byte_sz, is_signed, ++ BTF_KIND_ENUM64); ++} ++ ++/* ++ * Append new enum value for the current ENUM64 type with: ++ * - *name* - name of the enumerator value, can't be NULL or empty; ++ * - *value* - integer value corresponding to enum value *name*; ++ * Returns: ++ * - 0, on success; ++ * - <0, on error. ++ */ ++int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value) ++{ ++ struct btf_enum64 *v; ++ struct btf_type *t; ++ int sz, name_off; ++ ++ /* last type should be BTF_KIND_ENUM64 */ ++ if (btf->nr_types == 0) ++ return libbpf_err(-EINVAL); ++ t = btf_last_type(btf); ++ if (!btf_is_enum64(t)) ++ return libbpf_err(-EINVAL); ++ ++ /* non-empty name */ ++ if (!name || !name[0]) ++ return libbpf_err(-EINVAL); ++ ++ /* decompose and invalidate raw data */ ++ if (btf_ensure_modifiable(btf)) ++ return libbpf_err(-ENOMEM); ++ ++ sz = sizeof(struct btf_enum64); ++ v = btf_add_type_mem(btf, sz); ++ if (!v) ++ return libbpf_err(-ENOMEM); ++ ++ name_off = btf__add_str(btf, name); ++ if (name_off < 0) ++ return name_off; ++ ++ v->name_off = name_off; ++ v->val_lo32 = (__u32)value; ++ v->val_hi32 = value >> 32; ++ ++ /* update parent type's vlen */ ++ t = btf_last_type(btf); ++ btf_type_inc_vlen(t); ++ + btf->hdr->type_len += sz; + btf->hdr->str_off += sz; + return 0; +@@ -4737,6 +4829,7 @@ int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ct + case BTF_KIND_INT: + case BTF_KIND_FLOAT: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + return 0; + + case BTF_KIND_FWD: +@@ -4831,6 +4924,16 @@ int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ct + } + break; + } ++ case BTF_KIND_ENUM64: { ++ struct btf_enum64 *m = btf_enum64(t); ++ ++ for (i = 0, n = btf_vlen(t); i < n; i++, m++) { ++ err = visit(&m->name_off, ctx); ++ if (err) ++ return err; ++ } ++ break; ++ } + case BTF_KIND_FUNC_PROTO: { + struct btf_param *m = btf_params(t); + +diff --git a/src/btf.h b/src/btf.h +index 951ac7475..a41463bf9 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -215,6 +215,8 @@ LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_ + /* enum construction APIs */ + LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz); + LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value); ++LIBBPF_API int btf__add_enum64(struct btf *btf, const char *name, __u32 bytes_sz, bool is_signed); ++LIBBPF_API int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value); + + enum btf_fwd_kind { + BTF_FWD_STRUCT = 0, +@@ -454,6 +456,11 @@ static inline bool btf_is_enum(const struct btf_type *t) + return btf_kind(t) == BTF_KIND_ENUM; + } + ++static inline bool btf_is_enum64(const struct btf_type *t) ++{ ++ return btf_kind(t) == BTF_KIND_ENUM64; ++} ++ + static inline bool btf_is_fwd(const struct btf_type *t) + { + return btf_kind(t) == BTF_KIND_FWD; +@@ -549,6 +556,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t) + return (struct btf_enum *)(t + 1); + } + ++static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) ++{ ++ return (struct btf_enum64 *)(t + 1); ++} ++ + static inline struct btf_member *btf_members(const struct btf_type *t) + { + return (struct btf_member *)(t + 1); +diff --git a/src/libbpf.map b/src/libbpf.map +index 38e284ff0..116a2a8ee 100644 +--- a/src/libbpf.map ++++ b/src/libbpf.map +@@ -462,6 +462,8 @@ LIBBPF_0.8.0 { + + LIBBPF_0.8.0 { + global: ++ btf__add_enum64; ++ btf__add_enum64_value; + bpf_map__autocreate; + bpf_map__get_next_key; + bpf_map__delete_elem; diff --git a/backport-libbpf-Add-enum64-relocation-support.patch b/backport-libbpf-Add-enum64-relocation-support.patch new file mode 100644 index 0000000000000000000000000000000000000000..4b19bd0ff5916e780c55c7cd46f5c0bf331a5c58 --- /dev/null +++ b/backport-libbpf-Add-enum64-relocation-support.patch @@ -0,0 +1,224 @@ +From 416351822ca817b77329f746fad47ed642f708b4 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:47 -0700 +Subject: [PATCH] libbpf: Add enum64 relocation support + +The enum64 relocation support is added. The bpf local type +could be either enum or enum64 and the remote type could be +either enum or enum64 too. The all combinations of local enum/enum64 +and remote enum/enum64 are supported. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062647.3721719-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.h | 7 +++++++ + src/libbpf.c | 7 ++++--- + src/relo_core.c | 54 +++++++++++++++++++++++++++++++++---------------- + 3 files changed, 48 insertions(+), 20 deletions(-) + +diff --git a/src/btf.h b/src/btf.h +index 83312c340..9fb416eb5 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -537,6 +537,13 @@ static inline bool btf_is_any_enum(const struct btf_type *t) + return btf_is_enum(t) || btf_is_enum64(t); + } + ++static inline bool btf_kind_core_compat(const struct btf_type *t1, ++ const struct btf_type *t2) ++{ ++ return btf_kind(t1) == btf_kind(t2) || ++ (btf_is_any_enum(t1) && btf_is_any_enum(t2)); ++} ++ + static inline __u8 btf_int_encoding(const struct btf_type *t) + { + return BTF_INT_ENCODING(*(__u32 *)(t + 1)); +diff --git a/src/libbpf.c b/src/libbpf.c +index a0f5aae86..ed3cf0911 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -5524,7 +5524,7 @@ int bpf_core_add_cands(struct bpf_core_cand *local_cand, + n = btf__type_cnt(targ_btf); + for (i = targ_start_id; i < n; i++) { + t = btf__type_by_id(targ_btf, i); +- if (btf_kind(t) != btf_kind(local_t)) ++ if (!btf_kind_core_compat(t, local_t)) + continue; + + targ_name = btf__name_by_offset(targ_btf, t->name_off); +@@ -5738,7 +5738,7 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, + /* caller made sure that names match (ignoring flavor suffix) */ + local_type = btf__type_by_id(local_btf, local_id); + targ_type = btf__type_by_id(targ_btf, targ_id); +- if (btf_kind(local_type) != btf_kind(targ_type)) ++ if (!btf_kind_core_compat(local_type, targ_type)) + return 0; + + recur: +@@ -5751,7 +5751,7 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, + if (!local_type || !targ_type) + return -EINVAL; + +- if (btf_kind(local_type) != btf_kind(targ_type)) ++ if (!btf_kind_core_compat(local_type, targ_type)) + return 0; + + switch (btf_kind(local_type)) { +@@ -5759,6 +5759,7 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id, + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FWD: + return 1; + case BTF_KIND_INT: +diff --git a/src/relo_core.c b/src/relo_core.c +index 073a54ed7..6ad3c3891 100644 +--- a/src/relo_core.c ++++ b/src/relo_core.c +@@ -186,7 +186,7 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, + struct bpf_core_accessor *acc; + const struct btf_type *t; + const char *name, *spec_str; +- __u32 id; ++ __u32 id, name_off; + __s64 sz; + + spec_str = btf__name_by_offset(btf, relo->access_str_off); +@@ -231,11 +231,13 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf, + spec->len++; + + if (core_relo_is_enumval_based(relo->kind)) { +- if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t)) ++ if (!btf_is_any_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t)) + return -EINVAL; + + /* record enumerator name in a first accessor */ +- acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off); ++ name_off = btf_is_enum(t) ? btf_enum(t)[access_idx].name_off ++ : btf_enum64(t)[access_idx].name_off; ++ acc->name = btf__name_by_offset(btf, name_off); + return 0; + } + +@@ -340,7 +342,7 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf, + + if (btf_is_composite(local_type) && btf_is_composite(targ_type)) + return 1; +- if (btf_kind(local_type) != btf_kind(targ_type)) ++ if (!btf_kind_core_compat(local_type, targ_type)) + return 0; + + switch (btf_kind(local_type)) { +@@ -348,6 +350,7 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf, + case BTF_KIND_FLOAT: + return 1; + case BTF_KIND_FWD: ++ case BTF_KIND_ENUM64: + case BTF_KIND_ENUM: { + const char *local_name, *targ_name; + size_t local_len, targ_len; +@@ -477,6 +480,7 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, + const struct bpf_core_accessor *local_acc; + struct bpf_core_accessor *targ_acc; + int i, sz, matched; ++ __u32 name_off; + + memset(targ_spec, 0, sizeof(*targ_spec)); + targ_spec->btf = targ_btf; +@@ -494,18 +498,22 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, + + if (core_relo_is_enumval_based(local_spec->relo_kind)) { + size_t local_essent_len, targ_essent_len; +- const struct btf_enum *e; + const char *targ_name; + + /* has to resolve to an enum */ + targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id); +- if (!btf_is_enum(targ_type)) ++ if (!btf_is_any_enum(targ_type)) + return 0; + + local_essent_len = bpf_core_essential_name_len(local_acc->name); + +- for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) { +- targ_name = btf__name_by_offset(targ_spec->btf, e->name_off); ++ for (i = 0; i < btf_vlen(targ_type); i++) { ++ if (btf_is_enum(targ_type)) ++ name_off = btf_enum(targ_type)[i].name_off; ++ else ++ name_off = btf_enum64(targ_type)[i].name_off; ++ ++ targ_name = btf__name_by_offset(targ_spec->btf, name_off); + targ_essent_len = bpf_core_essential_name_len(targ_name); + if (targ_essent_len != local_essent_len) + continue; +@@ -680,8 +688,7 @@ static int bpf_core_calc_field_relo(const char *prog_name, + *val = byte_sz; + break; + case BPF_CORE_FIELD_SIGNED: +- /* enums will be assumed unsigned */ +- *val = btf_is_enum(mt) || ++ *val = (btf_is_any_enum(mt) && BTF_INFO_KFLAG(mt->info)) || + (btf_int_encoding(mt) & BTF_INT_SIGNED); + if (validate) + *validate = true; /* signedness is never ambiguous */ +@@ -754,7 +761,6 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, + __u64 *val) + { + const struct btf_type *t; +- const struct btf_enum *e; + + switch (relo->kind) { + case BPF_CORE_ENUMVAL_EXISTS: +@@ -764,8 +770,10 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, + if (!spec) + return -EUCLEAN; /* request instruction poisoning */ + t = btf_type_by_id(spec->btf, spec->spec[0].type_id); +- e = btf_enum(t) + spec->spec[0].idx; +- *val = e->val; ++ if (btf_is_enum(t)) ++ *val = btf_enum(t)[spec->spec[0].idx].val; ++ else ++ *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx); + break; + default: + return -EOPNOTSUPP; +@@ -1060,7 +1068,6 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec) + { + const struct btf_type *t; +- const struct btf_enum *e; + const char *s; + __u32 type_id; + int i, len = 0; +@@ -1089,10 +1096,23 @@ int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s + + if (core_relo_is_enumval_based(spec->relo_kind)) { + t = skip_mods_and_typedefs(spec->btf, type_id, NULL); +- e = btf_enum(t) + spec->raw_spec[0]; +- s = btf__name_by_offset(spec->btf, e->name_off); ++ if (btf_is_enum(t)) { ++ const struct btf_enum *e; ++ const char *fmt_str; ++ ++ e = btf_enum(t) + spec->raw_spec[0]; ++ s = btf__name_by_offset(spec->btf, e->name_off); ++ fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %d" : "::%s = %u"; ++ append_buf(fmt_str, s, e->val); ++ } else { ++ const struct btf_enum64 *e; ++ const char *fmt_str; + +- append_buf("::%s = %u", s, e->val); ++ e = btf_enum64(t) + spec->raw_spec[0]; ++ s = btf__name_by_offset(spec->btf, e->name_off); ++ fmt_str = BTF_INFO_KFLAG(t->info) ? "::%s = %lld" : "::%s = %llu"; ++ append_buf(fmt_str, s, (unsigned long long)btf_enum64_value(e)); ++ } + return len; + } + diff --git a/backport-libbpf-Add-enum64-sanitization.patch b/backport-libbpf-Add-enum64-sanitization.patch new file mode 100644 index 0000000000000000000000000000000000000000..776198037acf0cc0ce7b73ed49d51846409b4c9e --- /dev/null +++ b/backport-libbpf-Add-enum64-sanitization.patch @@ -0,0 +1,177 @@ +From a945df2439020ca182513b6e2f24175cbf2a2dc4 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:36 -0700 +Subject: [PATCH] libbpf: Add enum64 sanitization + +When old kernel does not support enum64 but user space btf +contains non-zero enum kflag or enum64, libbpf needs to +do proper sanitization so modified btf can be accepted +by the kernel. + +Sanitization for enum kflag can be achieved by clearing +the kflag bit. For enum64, the type is replaced with an +union of integer member types and the integer member size +must be smaller than enum64 size. If such an integer +type cannot be found, a new type is created and used +for union members. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062636.3721375-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.h | 3 ++- + src/libbpf.c | 56 +++++++++++++++++++++++++++++++++++++++---- + src/libbpf_internal.h | 2 ++ + 3 files changed, 56 insertions(+), 5 deletions(-) + +diff --git a/src/btf.h b/src/btf.h +index dcb3f575a..83312c340 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -395,9 +395,10 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id, + #ifndef BTF_KIND_FLOAT + #define BTF_KIND_FLOAT 16 /* Floating point */ + #endif +-/* The kernel header switched to enums, so these two were never #defined */ ++/* The kernel header switched to enums, so the following were never #defined */ + #define BTF_KIND_DECL_TAG 17 /* Decl Tag */ + #define BTF_KIND_TYPE_TAG 18 /* Type Tag */ ++#define BTF_KIND_ENUM64 19 /* Enum for up-to 64bit values */ + + static inline __u16 btf_kind(const struct btf_type *t) + { +diff --git a/src/libbpf.c b/src/libbpf.c +index b03165687..a0f5aae86 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -2242,6 +2242,7 @@ static const char *__btf_kind_str(__u16 kind) + case BTF_KIND_FLOAT: return "float"; + case BTF_KIND_DECL_TAG: return "decl_tag"; + case BTF_KIND_TYPE_TAG: return "type_tag"; ++ case BTF_KIND_ENUM64: return "enum64"; + default: return "unknown"; + } + } +@@ -2770,12 +2771,13 @@ static bool btf_needs_sanitization(struct bpf_object *obj) + bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); + bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); + bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); ++ bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); + + return !has_func || !has_datasec || !has_func_global || !has_float || +- !has_decl_tag || !has_type_tag; ++ !has_decl_tag || !has_type_tag || !has_enum64; + } + +-static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) ++static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) + { + bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); + bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); +@@ -2783,6 +2785,8 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) + bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); + bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); + bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); ++ bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); ++ __u32 enum64_placeholder_id = 0; + struct btf_type *t; + int i, j, vlen; + +@@ -2845,8 +2849,32 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) + /* replace TYPE_TAG with a CONST */ + t->name_off = 0; + t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); +- } ++ } else if (!has_enum64 && btf_is_enum(t)) { ++ /* clear the kflag */ ++ t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); ++ } else if (!has_enum64 && btf_is_enum64(t)) { ++ /* replace ENUM64 with a union */ ++ struct btf_member *m; ++ ++ if (enum64_placeholder_id == 0) { ++ enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); ++ if (enum64_placeholder_id < 0) ++ return enum64_placeholder_id; ++ ++ t = (struct btf_type *)btf__type_by_id(btf, i); ++ } ++ ++ m = btf_members(t); ++ vlen = btf_vlen(t); ++ t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); ++ for (j = 0; j < vlen; j++, m++) { ++ m->type = enum64_placeholder_id; ++ m->offset = 0; ++ } ++ } + } ++ ++ return 0; + } + + static bool libbpf_needs_btf(const struct bpf_object *obj) +@@ -3184,7 +3212,9 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) + + /* enforce 8-byte pointers for BPF-targeted BTFs */ + btf__set_pointer_size(obj->btf, 8); +- bpf_object__sanitize_btf(obj, kern_btf); ++ err = bpf_object__sanitize_btf(obj, kern_btf); ++ if (err) ++ return err; + } + + if (obj->gen_loader) { +@@ -3691,6 +3721,10 @@ static enum kcfg_type find_kcfg_type(const struct btf *btf, int id, + if (strcmp(name, "libbpf_tristate")) + return KCFG_UNKNOWN; + return KCFG_TRISTATE; ++ case BTF_KIND_ENUM64: ++ if (strcmp(name, "libbpf_tristate")) ++ return KCFG_UNKNOWN; ++ return KCFG_TRISTATE; + case BTF_KIND_ARRAY: + if (btf_array(t)->nelems == 0) + return KCFG_UNKNOWN; +@@ -4874,6 +4908,17 @@ static int probe_kern_bpf_cookie(void) + return probe_fd(ret); + } + ++static int probe_kern_btf_enum64(void) ++{ ++ static const char strs[] = "\0enum64"; ++ __u32 types[] = { ++ BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8), ++ }; ++ ++ return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types), ++ strs, sizeof(strs))); ++} ++ + enum kern_feature_result { + FEAT_UNKNOWN = 0, + FEAT_SUPPORTED = 1, +@@ -4939,6 +4984,9 @@ static struct kern_feature_desc { + [FEAT_BPF_COOKIE] = { + "BPF cookie support", probe_kern_bpf_cookie, + }, ++ [FEAT_BTF_ENUM64] = { ++ "BTF_KIND_ENUM64 support", probe_kern_btf_enum64, ++ }, + }; + + bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) +diff --git a/src/libbpf_internal.h b/src/libbpf_internal.h +index ef5d97507..a1ad145ff 100644 +--- a/src/libbpf_internal.h ++++ b/src/libbpf_internal.h +@@ -351,6 +351,8 @@ enum kern_feature_id { + FEAT_MEMCG_ACCOUNT, + /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */ + FEAT_BPF_COOKIE, ++ /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */ ++ FEAT_BTF_ENUM64, + __FEAT_CNT, + }; + diff --git a/backport-libbpf-Add-enum64-support-for-bpf-linking.patch b/backport-libbpf-Add-enum64-support-for-bpf-linking.patch new file mode 100644 index 0000000000000000000000000000000000000000..9b90152b2f94fc9029d68fcacccd64b33ec2b447 --- /dev/null +++ b/backport-libbpf-Add-enum64-support-for-bpf-linking.patch @@ -0,0 +1,36 @@ +From 3f9d041e19429691d3650e031ba0b24c8e662221 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:42 -0700 +Subject: [PATCH] libbpf: Add enum64 support for bpf linking + +Add BTF_KIND_ENUM64 support for bpf linking, which is +very similar to BTF_KIND_ENUM. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062642.3721494-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/linker.c | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/src/linker.c b/src/linker.c +index 85c0fddf5..4ac02c28e 100644 +--- a/src/linker.c ++++ b/src/linker.c +@@ -1335,6 +1335,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact, + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FWD: + case BTF_KIND_FUNC: + case BTF_KIND_VAR: +@@ -1357,6 +1358,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact, + case BTF_KIND_INT: + case BTF_KIND_FLOAT: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + /* ignore encoding for int and enum values for enum */ + if (t1->size != t2->size) { + pr_warn("global '%s': incompatible %s '%s' size %u and %u\n", diff --git a/backport-libbpf-Add-enum64-support-for-btf_dump.patch b/backport-libbpf-Add-enum64-support-for-btf_dump.patch new file mode 100644 index 0000000000000000000000000000000000000000..096a0d5e2f31a6c7efde78ad5ab1d66379dadfd8 --- /dev/null +++ b/backport-libbpf-Add-enum64-support-for-btf_dump.patch @@ -0,0 +1,281 @@ +From f429a582bf2efed00f3ae92e04810e12a090630c Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:31 -0700 +Subject: [PATCH] libbpf: Add enum64 support for btf_dump + +Add enum64 btf dumping support. For long long and unsigned long long +dump, suffixes 'LL' and 'ULL' are added to avoid compilation errors +in some cases. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062631.3720526-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.h | 5 ++ + src/btf_dump.c | 137 +++++++++++++++++++++++++++++++++++++------------ + 2 files changed, 108 insertions(+), 34 deletions(-) + +diff --git a/src/btf.h b/src/btf.h +index c7e8b1fdf..dcb3f575a 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -566,6 +566,11 @@ static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) + return (struct btf_enum64 *)(t + 1); + } + ++static inline __u64 btf_enum64_value(const struct btf_enum64 *e) ++{ ++ return ((__u64)e->val_hi32 << 32) | e->val_lo32; ++} ++ + static inline struct btf_member *btf_members(const struct btf_type *t) + { + return (struct btf_member *)(t + 1); +diff --git a/src/btf_dump.c b/src/btf_dump.c +index 6b1bc1f43..f5275f819 100644 +--- a/src/btf_dump.c ++++ b/src/btf_dump.c +@@ -318,6 +318,7 @@ static int btf_dump_mark_referenced(struct btf_dump *d) + switch (btf_kind(t)) { + case BTF_KIND_INT: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FWD: + case BTF_KIND_FLOAT: + break; +@@ -538,6 +539,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr) + return 1; + } + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FWD: + /* + * non-anonymous or non-referenced enums are top-level +@@ -739,6 +741,7 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) + tstate->emit_state = EMITTED; + break; + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + if (top_level_def) { + btf_dump_emit_enum_def(d, id, t, 0); + btf_dump_printf(d, ";\n\n"); +@@ -989,38 +992,81 @@ static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, + btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id)); + } + +-static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id, +- const struct btf_type *t, +- int lvl) ++static void btf_dump_emit_enum32_val(struct btf_dump *d, ++ const struct btf_type *t, ++ int lvl, __u16 vlen) + { + const struct btf_enum *v = btf_enum(t); +- __u16 vlen = btf_vlen(t); ++ bool is_signed = btf_kflag(t); ++ const char *fmt_str; + const char *name; + size_t dup_cnt; + int i; + ++ for (i = 0; i < vlen; i++, v++) { ++ name = btf_name_of(d, v->name_off); ++ /* enumerators share namespace with typedef idents */ ++ dup_cnt = btf_dump_name_dups(d, d->ident_names, name); ++ if (dup_cnt > 1) { ++ fmt_str = is_signed ? "\n%s%s___%zd = %d," : "\n%s%s___%zd = %u,"; ++ btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, dup_cnt, v->val); ++ } else { ++ fmt_str = is_signed ? "\n%s%s = %d," : "\n%s%s = %u,"; ++ btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, v->val); ++ } ++ } ++} ++ ++static void btf_dump_emit_enum64_val(struct btf_dump *d, ++ const struct btf_type *t, ++ int lvl, __u16 vlen) ++{ ++ const struct btf_enum64 *v = btf_enum64(t); ++ bool is_signed = btf_kflag(t); ++ const char *fmt_str; ++ const char *name; ++ size_t dup_cnt; ++ __u64 val; ++ int i; ++ ++ for (i = 0; i < vlen; i++, v++) { ++ name = btf_name_of(d, v->name_off); ++ dup_cnt = btf_dump_name_dups(d, d->ident_names, name); ++ val = btf_enum64_value(v); ++ if (dup_cnt > 1) { ++ fmt_str = is_signed ? "\n%s%s___%zd = %lldLL," ++ : "\n%s%s___%zd = %lluULL,"; ++ btf_dump_printf(d, fmt_str, ++ pfx(lvl + 1), name, dup_cnt, ++ (unsigned long long)val); ++ } else { ++ fmt_str = is_signed ? "\n%s%s = %lldLL," ++ : "\n%s%s = %lluULL,"; ++ btf_dump_printf(d, fmt_str, ++ pfx(lvl + 1), name, ++ (unsigned long long)val); ++ } ++ } ++} ++static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id, ++ const struct btf_type *t, ++ int lvl) ++{ ++ __u16 vlen = btf_vlen(t); ++ + btf_dump_printf(d, "enum%s%s", + t->name_off ? " " : "", + btf_dump_type_name(d, id)); + +- if (vlen) { +- btf_dump_printf(d, " {"); +- for (i = 0; i < vlen; i++, v++) { +- name = btf_name_of(d, v->name_off); +- /* enumerators share namespace with typedef idents */ +- dup_cnt = btf_dump_name_dups(d, d->ident_names, name); +- if (dup_cnt > 1) { +- btf_dump_printf(d, "\n%s%s___%zu = %u,", +- pfx(lvl + 1), name, dup_cnt, +- (__u32)v->val); +- } else { +- btf_dump_printf(d, "\n%s%s = %u,", +- pfx(lvl + 1), name, +- (__u32)v->val); +- } +- } +- btf_dump_printf(d, "\n%s}", pfx(lvl)); +- } ++ if (!vlen) ++ return; ++ ++ btf_dump_printf(d, " {"); ++ if (btf_is_enum(t)) ++ btf_dump_emit_enum32_val(d, t, lvl, vlen); ++ else ++ btf_dump_emit_enum64_val(d, t, lvl, vlen); ++ btf_dump_printf(d, "\n%s}", pfx(lvl)); + } + + static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id, +@@ -1178,6 +1224,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id, + break; + case BTF_KIND_INT: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + case BTF_KIND_FWD: + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: +@@ -1312,6 +1359,7 @@ static void btf_dump_emit_type_chain(struct btf_dump *d, + btf_dump_emit_struct_fwd(d, id, t); + break; + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + btf_dump_emit_mods(d, decls); + /* inline anonymous enum */ + if (t->name_off == 0 && !d->skip_anon_defs) +@@ -1988,7 +2036,8 @@ static int btf_dump_get_enum_value(struct btf_dump *d, + __u32 id, + __s64 *value) + { +- /* handle unaligned enum value */ ++ bool is_signed = btf_kflag(t); ++ + if (!ptr_is_aligned(d->btf, id, data)) { + __u64 val; + int err; +@@ -2005,13 +2054,13 @@ static int btf_dump_get_enum_value(struct btf_dump *d, + *value = *(__s64 *)data; + return 0; + case 4: +- *value = *(__s32 *)data; ++ *value = is_signed ? *(__s32 *)data : *(__u32 *)data; + return 0; + case 2: +- *value = *(__s16 *)data; ++ *value = is_signed ? *(__s16 *)data : *(__u16 *)data; + return 0; + case 1: +- *value = *(__s8 *)data; ++ *value = is_signed ? *(__s8 *)data : *(__u8 *)data; + return 0; + default: + pr_warn("unexpected size %d for enum, id:[%u]\n", t->size, id); +@@ -2024,7 +2073,7 @@ static int btf_dump_enum_data(struct btf_dump *d, + __u32 id, + const void *data) + { +- const struct btf_enum *e; ++ bool is_signed; + __s64 value; + int i, err; + +@@ -2032,14 +2081,31 @@ static int btf_dump_enum_data(struct btf_dump *d, + if (err) + return err; + +- for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) { +- if (value != e->val) +- continue; +- btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off)); +- return 0; +- } ++ is_signed = btf_kflag(t); ++ if (btf_is_enum(t)) { ++ const struct btf_enum *e; ++ ++ for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) { ++ if (value != e->val) ++ continue; ++ btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off)); ++ return 0; ++ } + +- btf_dump_type_values(d, "%d", value); ++ btf_dump_type_values(d, is_signed ? "%d" : "%u", value); ++ } else { ++ const struct btf_enum64 *e; ++ ++ for (i = 0, e = btf_enum64(t); i < btf_vlen(t); i++, e++) { ++ if (value != btf_enum64_value(e)) ++ continue; ++ btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off)); ++ return 0; ++ } ++ ++ btf_dump_type_values(d, is_signed ? "%lldLL" : "%lluULL", ++ (unsigned long long)value); ++ } + return 0; + } + +@@ -2099,6 +2165,7 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d, + case BTF_KIND_FLOAT: + case BTF_KIND_PTR: + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + if (data + bits_offset / 8 + size > d->typed_dump->data_end) + return -E2BIG; + break; +@@ -2203,6 +2270,7 @@ static int btf_dump_type_data_check_zero(struct btf_dump *d, + return -ENODATA; + } + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + err = btf_dump_get_enum_value(d, t, data, id, &value); + if (err) + return err; +@@ -2275,6 +2343,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d, + err = btf_dump_struct_data(d, t, id, data); + break; + case BTF_KIND_ENUM: ++ case BTF_KIND_ENUM64: + /* handle bitfield and int enum values */ + if (bit_sz) { + __u64 print_num; diff --git a/backport-libbpf-Don-t-require-full-struct-enum64-in-UAPI-head.patch b/backport-libbpf-Don-t-require-full-struct-enum64-in-UAPI-head.patch new file mode 100644 index 0000000000000000000000000000000000000000..2ca6d7d2ed2991bb96bfe1054b88c1141d939dc2 --- /dev/null +++ b/backport-libbpf-Don-t-require-full-struct-enum64-in-UAPI-head.patch @@ -0,0 +1,77 @@ +From 85f8b7c4dc87bfbb019478c5cde14eaa833f403c Mon Sep 17 00:00:00 2001 +From: Andrii Nakryiko +Date: Mon, 26 Sep 2022 21:29:39 -0700 +Subject: [PATCH] libbpf: Don't require full struct enum64 in UAPI headers +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Drop the requirement for system-wide kernel UAPI headers to provide full +struct btf_enum64 definition. This is an unexpected requirement that +slipped in libbpf 1.0 and put unnecessary pressure ([0]) on users to have +a bleeding-edge kernel UAPI header from unreleased Linux 6.0. + +To achieve this, we forward declare struct btf_enum64. But that's not +enough as there is btf_enum64_value() helper that expects to know the +layout of struct btf_enum64. So we get a bit creative with +reinterpreting memory layout as array of __u32 and accesing lo32/hi32 +fields as array elements. Alternative way would be to have a local +pointer variable for anonymous struct with exactly the same layout as +struct btf_enum64, but that gets us into C++ compiler errors complaining +about invalid type casts. So play it safe, if ugly. + + [0] Closes: https://github.com/libbpf/libbpf/issues/562 + +Fixes: d90ec262b35b ("libbpf: Add enum64 support for btf_dump") +Reported-by: Toke Høiland-Jørgensen +Signed-off-by: Andrii Nakryiko +Signed-off-by: Daniel Borkmann +Acked-by: Toke Høiland-Jørgensen +Link: https://lore.kernel.org/bpf/20220927042940.147185-1-andrii@kernel.org +--- + src/btf.h | 25 ++++++++++++++++++++++++- + 1 file changed, 24 insertions(+), 1 deletion(-) + +diff --git a/src/btf.h b/src/btf.h +index ae543144e..8e6880d91 100644 +--- a/src/btf.h ++++ b/src/btf.h +@@ -486,6 +486,8 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t) + return (struct btf_enum *)(t + 1); + } + ++struct btf_enum64; ++ + static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) + { + return (struct btf_enum64 *)(t + 1); +@@ -493,7 +495,28 @@ static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) + + static inline __u64 btf_enum64_value(const struct btf_enum64 *e) + { +- return ((__u64)e->val_hi32 << 32) | e->val_lo32; ++ /* struct btf_enum64 is introduced in Linux 6.0, which is very ++ * bleeding-edge. Here we are avoiding relying on struct btf_enum64 ++ * definition coming from kernel UAPI headers to support wider range ++ * of system-wide kernel headers. ++ * ++ * Given this header can be also included from C++ applications, that ++ * further restricts C tricks we can use (like using compatible ++ * anonymous struct). So just treat struct btf_enum64 as ++ * a three-element array of u32 and access second (lo32) and third ++ * (hi32) elements directly. ++ * ++ * For reference, here is a struct btf_enum64 definition: ++ * ++ * const struct btf_enum64 { ++ * __u32 name_off; ++ * __u32 val_lo32; ++ * __u32 val_hi32; ++ * }; ++ */ ++ const __u32 *e64 = (const __u32 *)e; ++ ++ return ((__u64)e64[2] << 32) | e64[1]; + } + + static inline struct btf_member *btf_members(const struct btf_type *t) diff --git a/backport-libbpf-Fix-an-error-in-64bit-relocation-value-comput.patch b/backport-libbpf-Fix-an-error-in-64bit-relocation-value-comput.patch new file mode 100644 index 0000000000000000000000000000000000000000..a2e41b0c8df056a0f0379a1cf888be7aacd2e64d --- /dev/null +++ b/backport-libbpf-Fix-an-error-in-64bit-relocation-value-comput.patch @@ -0,0 +1,42 @@ +From 0167a883554df812013ae1778724943ed0c8a069 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:10 -0700 +Subject: [PATCH] libbpf: Fix an error in 64bit relocation value computation + +Currently, the 64bit relocation value in the instruction +is computed as follows: + __u64 imm = insn[0].imm + ((__u64)insn[1].imm << 32) + +Suppose insn[0].imm = -1 (0xffffffff) and insn[1].imm = 1. +With the above computation, insn[0].imm will first sign-extend +to 64bit -1 (0xffffffffFFFFFFFF) and then add 0x1FFFFFFFF, +producing incorrect value 0xFFFFFFFF. The correct value +should be 0x1FFFFFFFF. + +Changing insn[0].imm to __u32 first will prevent 64bit sign +extension and fix the issue. Merging high and low 32bit values +also changed from '+' to '|' to be consistent with other +similar occurences in kernel and libbpf. + +Acked-by: Andrii Nakryiko +Acked-by: Dave Marchevsky +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062610.3717378-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/relo_core.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/relo_core.c b/src/relo_core.c +index 0dce56448..073a54ed7 100644 +--- a/src/relo_core.c ++++ b/src/relo_core.c +@@ -1027,7 +1027,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + return -EINVAL; + } + +- imm = insn[0].imm + ((__u64)insn[1].imm << 32); ++ imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32); + if (res->validate && imm != orig_val) { + pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n", + prog_name, relo_idx, diff --git a/backport-libbpf-Fix-an-unsigned-0-bug.patch b/backport-libbpf-Fix-an-unsigned-0-bug.patch new file mode 100644 index 0000000000000000000000000000000000000000..7f0c7f1ff5df3267c6517e022e9f569c5c251d43 --- /dev/null +++ b/backport-libbpf-Fix-an-unsigned-0-bug.patch @@ -0,0 +1,41 @@ +From d3e41fc1aa82e39cb641131180a85b118a57fb98 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Sun, 12 Jun 2022 22:43:14 -0700 +Subject: [PATCH] libbpf: Fix an unsigned < 0 bug + +Andrii reported a bug with the following information: + + 2859 if (enum64_placeholder_id == 0) { + 2860 enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); + >>> CID 394804: Control flow issues (NO_EFFECT) + >>> This less-than-zero comparison of an unsigned value is never true. "enum64_placeholder_id < 0U". + 2861 if (enum64_placeholder_id < 0) + 2862 return enum64_placeholder_id; + 2863 ... + +Here enum64_placeholder_id declared as '__u32' so enum64_placeholder_id < 0 +is always false. Declare enum64_placeholder_id as 'int' in order to capture +the potential error properly. + +Fixes: f2a625889bb8 ("libbpf: Add enum64 sanitization") +Reported-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Signed-off-by: Daniel Borkmann +Link: https://lore.kernel.org/bpf/20220613054314.1251905-1-yhs@fb.com +--- + src/libbpf.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/libbpf.c b/src/libbpf.c +index 0781fae58..d989b0a17 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -2786,7 +2786,7 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) + bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); + bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); + bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); +- __u32 enum64_placeholder_id = 0; ++ int enum64_placeholder_id = 0; + struct btf_type *t; + int i, j, vlen; + diff --git a/backport-libbpf-Fix-sign-expansion-bug-in-btf_dump_get_enum_v.patch b/backport-libbpf-Fix-sign-expansion-bug-in-btf_dump_get_enum_v.patch new file mode 100644 index 0000000000000000000000000000000000000000..54093891371ca91c634b6cd25bfc28575cae7496 --- /dev/null +++ b/backport-libbpf-Fix-sign-expansion-bug-in-btf_dump_get_enum_v.patch @@ -0,0 +1,53 @@ +From 77e514d6266a94582cedb6aa8018d75cacb5eb1d Mon Sep 17 00:00:00 2001 +From: Dan Carpenter +Date: Tue, 19 Jul 2022 12:49:34 +0300 +Subject: [PATCH] libbpf: Fix sign expansion bug in btf_dump_get_enum_value() + +The code here is supposed to take a signed int and store it in a signed +long long. Unfortunately, the way that the type promotion works with +this conditional statement is that it takes a signed int, type promotes +it to a __u32, and then stores that as a signed long long. The result is +never negative. + +This is from static analysis, but I made a little test program just to +test it before I sent the patch: + + #include + + int main(void) + { + unsigned long long src = -1ULL; + signed long long dst1, dst2; + int is_signed = 1; + + dst1 = is_signed ? *(int *)&src : *(unsigned int *)0; + dst2 = is_signed ? (signed long long)*(int *)&src : *(unsigned int *)0; + + printf("%lld\n", dst1); + printf("%lld\n", dst2); + + return 0; + } + +Fixes: d90ec262b35b ("libbpf: Add enum64 support for btf_dump") +Signed-off-by: Dan Carpenter +Signed-off-by: Daniel Borkmann +Acked-by: Martin KaFai Lau +Link: https://lore.kernel.org/bpf/YtZ+LpgPADm7BeEd@kili +--- + src/btf_dump.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/btf_dump.c b/src/btf_dump.c +index 400e84fd0..627edb5bb 100644 +--- a/src/btf_dump.c ++++ b/src/btf_dump.c +@@ -2045,7 +2045,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d, + *value = *(__s64 *)data; + return 0; + case 4: +- *value = is_signed ? *(__s32 *)data : *(__u32 *)data; ++ *value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data; + return 0; + case 2: + *value = is_signed ? *(__s16 *)data : *(__u16 *)data; diff --git a/backport-libbpf-Permit-64bit-relocation-value.patch b/backport-libbpf-Permit-64bit-relocation-value.patch new file mode 100644 index 0000000000000000000000000000000000000000..0f4db7a01bc32f5324f584630a955051e450c282 --- /dev/null +++ b/backport-libbpf-Permit-64bit-relocation-value.patch @@ -0,0 +1,166 @@ +From 23e3d8cf3140e5da2406ad1c7cd6cf678bdef70c Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:05 -0700 +Subject: [PATCH] libbpf: Permit 64bit relocation value + +Currently, the libbpf limits the relocation value to be 32bit +since all current relocations have such a limit. But with +BTF_KIND_ENUM64 support, the enum value could be 64bit. +So let us permit 64bit relocation value in libbpf. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062605.3716779-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/relo_core.c | 49 +++++++++++++++++++++++++++---------------------- + src/relo_core.h | 4 ++-- + 2 files changed, 29 insertions(+), 24 deletions(-) + +diff --git a/src/relo_core.c b/src/relo_core.c +index d8ab4c61c..0dce56448 100644 +--- a/src/relo_core.c ++++ b/src/relo_core.c +@@ -583,7 +583,7 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec, + static int bpf_core_calc_field_relo(const char *prog_name, + const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, +- __u32 *val, __u32 *field_sz, __u32 *type_id, ++ __u64 *val, __u32 *field_sz, __u32 *type_id, + bool *validate) + { + const struct bpf_core_accessor *acc; +@@ -708,7 +708,7 @@ static int bpf_core_calc_field_relo(const char *prog_name, + + static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, +- __u32 *val, bool *validate) ++ __u64 *val, bool *validate) + { + __s64 sz; + +@@ -751,7 +751,7 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo, + + static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, + const struct bpf_core_spec *spec, +- __u32 *val) ++ __u64 *val) + { + const struct btf_type *t; + const struct btf_enum *e; +@@ -929,7 +929,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + int insn_idx, const struct bpf_core_relo *relo, + int relo_idx, const struct bpf_core_relo_res *res) + { +- __u32 orig_val, new_val; ++ __u64 orig_val, new_val; + __u8 class; + + class = BPF_CLASS(insn->code); +@@ -954,28 +954,30 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + if (BPF_SRC(insn->code) != BPF_K) + return -EINVAL; + if (res->validate && insn->imm != orig_val) { +- pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n", ++ pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %llu -> %llu\n", + prog_name, relo_idx, +- insn_idx, insn->imm, orig_val, new_val); ++ insn_idx, insn->imm, (unsigned long long)orig_val, ++ (unsigned long long)new_val); + return -EINVAL; + } + orig_val = insn->imm; + insn->imm = new_val; +- pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n", ++ pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n", + prog_name, relo_idx, insn_idx, +- orig_val, new_val); ++ (unsigned long long)orig_val, (unsigned long long)new_val); + break; + case BPF_LDX: + case BPF_ST: + case BPF_STX: + if (res->validate && insn->off != orig_val) { +- pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %u -> %u\n", +- prog_name, relo_idx, insn_idx, insn->off, orig_val, new_val); ++ pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %llu -> %llu\n", ++ prog_name, relo_idx, insn_idx, insn->off, (unsigned long long)orig_val, ++ (unsigned long long)new_val); + return -EINVAL; + } + if (new_val > SHRT_MAX) { +- pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n", +- prog_name, relo_idx, insn_idx, new_val); ++ pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %llu\n", ++ prog_name, relo_idx, insn_idx, (unsigned long long)new_val); + return -ERANGE; + } + if (res->fail_memsz_adjust) { +@@ -987,8 +989,9 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + + orig_val = insn->off; + insn->off = new_val; +- pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n", +- prog_name, relo_idx, insn_idx, orig_val, new_val); ++ pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %llu -> %llu\n", ++ prog_name, relo_idx, insn_idx, (unsigned long long)orig_val, ++ (unsigned long long)new_val); + + if (res->new_sz != res->orig_sz) { + int insn_bytes_sz, insn_bpf_sz; +@@ -1026,18 +1029,18 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, + + imm = insn[0].imm + ((__u64)insn[1].imm << 32); + if (res->validate && imm != orig_val) { +- pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %u -> %u\n", ++ pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n", + prog_name, relo_idx, + insn_idx, (unsigned long long)imm, +- orig_val, new_val); ++ (unsigned long long)orig_val, (unsigned long long)new_val); + return -EINVAL; + } + + insn[0].imm = new_val; +- insn[1].imm = 0; /* currently only 32-bit values are supported */ +- pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %u\n", ++ insn[1].imm = new_val >> 32; ++ pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n", + prog_name, relo_idx, insn_idx, +- (unsigned long long)imm, new_val); ++ (unsigned long long)imm, (unsigned long long)new_val); + break; + } + default: +@@ -1261,10 +1264,12 @@ int bpf_core_calc_relo_insn(const char *prog_name, + * decision and value, otherwise it's dangerous to + * proceed due to ambiguity + */ +- pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %u != %s %u\n", ++ pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %llu != %s %llu\n", + prog_name, relo_idx, +- cand_res.poison ? "failure" : "success", cand_res.new_val, +- targ_res->poison ? "failure" : "success", targ_res->new_val); ++ cand_res.poison ? "failure" : "success", ++ (unsigned long long)cand_res.new_val, ++ targ_res->poison ? "failure" : "success", ++ (unsigned long long)targ_res->new_val); + return -EINVAL; + } + +diff --git a/src/relo_core.h b/src/relo_core.h +index 073039d8c..7df0da082 100644 +--- a/src/relo_core.h ++++ b/src/relo_core.h +@@ -46,9 +46,9 @@ struct bpf_core_spec { + + struct bpf_core_relo_res { + /* expected value in the instruction, unless validate == false */ +- __u32 orig_val; ++ __u64 orig_val; + /* new value that needs to be patched up to */ +- __u32 new_val; ++ __u64 new_val; + /* relocation unsuccessful, poison instruction, but don't fail load */ + bool poison; + /* some relocations can't be validated against orig_val */ diff --git a/backport-libbpf-Refactor-btf__add_enum-for-future-code-sharin.patch b/backport-libbpf-Refactor-btf__add_enum-for-future-code-sharin.patch new file mode 100644 index 0000000000000000000000000000000000000000..61061c3cac0b3fc2480d0332cd0b00b28dd3bc09 --- /dev/null +++ b/backport-libbpf-Refactor-btf__add_enum-for-future-code-sharin.patch @@ -0,0 +1,77 @@ +From 25fd7a1cf58f79db2bd84c6a9de9457107f1ed21 Mon Sep 17 00:00:00 2001 +From: Yonghong Song +Date: Mon, 6 Jun 2022 23:26:15 -0700 +Subject: [PATCH] libbpf: Refactor btf__add_enum() for future code sharing + +Refactor btf__add_enum() function to create a separate +function btf_add_enum_common() so later the common function +can be used to add enum64 btf type. There is no functionality +change for this patch. + +Acked-by: Andrii Nakryiko +Signed-off-by: Yonghong Song +Link: https://lore.kernel.org/r/20220607062615.3718063-1-yhs@fb.com +Signed-off-by: Alexei Starovoitov +--- + src/btf.c | 36 +++++++++++++++++++++--------------- + 1 file changed, 21 insertions(+), 15 deletions(-) + +diff --git a/src/btf.c b/src/btf.c +index 2e9c23bcd..1972d0c65 100644 +--- a/src/btf.c ++++ b/src/btf.c +@@ -2129,20 +2129,8 @@ int btf__add_field(struct btf *btf, const char *name, int type_id, + return 0; + } + +-/* +- * Append new BTF_KIND_ENUM type with: +- * - *name* - name of the enum, can be NULL or empty for anonymous enums; +- * - *byte_sz* - size of the enum, in bytes. +- * +- * Enum initially has no enum values in it (and corresponds to enum forward +- * declaration). Enumerator values can be added by btf__add_enum_value() +- * immediately after btf__add_enum() succeeds. +- * +- * Returns: +- * - >0, type ID of newly added BTF type; +- * - <0, on error. +- */ +-int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz) ++static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz, ++ bool is_signed, __u8 kind) + { + struct btf_type *t; + int sz, name_off = 0; +@@ -2167,12 +2155,30 @@ int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz) + + /* start out with vlen=0; it will be adjusted when adding enum values */ + t->name_off = name_off; +- t->info = btf_type_info(BTF_KIND_ENUM, 0, 0); ++ t->info = btf_type_info(kind, 0, is_signed); + t->size = byte_sz; + + return btf_commit_type(btf, sz); + } + ++/* ++ * Append new BTF_KIND_ENUM type with: ++ * - *name* - name of the enum, can be NULL or empty for anonymous enums; ++ * - *byte_sz* - size of the enum, in bytes. ++ * ++ * Enum initially has no enum values in it (and corresponds to enum forward ++ * declaration). Enumerator values can be added by btf__add_enum_value() ++ * immediately after btf__add_enum() succeeds. ++ * ++ * Returns: ++ * - >0, type ID of newly added BTF type; ++ * - <0, on error. ++ */ ++int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz) ++{ ++ return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM); ++} ++ + /* + * Append new enum value for the current ENUM type with: + * - *name* - name of the enumerator value, can't be NULL or empty; diff --git a/libbpf.spec b/libbpf.spec index 175a49d8f00f66de6d73db5e29f64a0c0f2bf6a7..e686135a59971da6ae436cc457c21cfbcd71c1b1 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 10 +Release: 11 Summary: Libbpf library License: LGPLv2 or BSD @@ -33,6 +33,20 @@ Patch0017: backport-libbpf-Use-elf_getshdrnum-instead-of-e_shnum.patch Patch0018: 0001-sync-bpf-helper-funcs-from-kernel.patch Patch0019: backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch Patch0020: backport-libbpf-Ensure-libbpf-always-opens-files-with-O_CLOEX.patch +Patch0021: backport-bpf-Add-btf-enum64-support.patch +Patch0022: backport-libbpf-Permit-64bit-relocation-value.patch +Patch0023: backport-libbpf-Fix-an-error-in-64bit-relocation-value-comput.patch +Patch0024: backport-libbpf-Refactor-btf__add_enum-for-future-code-sharin.patch +Patch0025: backport-libbpf-Add-enum64-parsing-and-new-enum64-public-API.patch +Patch0026: backport-libbpf-Add-enum64-deduplication-support.patch +Patch0027: backport-libbpf-Add-enum64-support-for-btf_dump.patch +Patch0028: backport-libbpf-Add-enum64-sanitization.patch +Patch0029: backport-libbpf-Add-enum64-support-for-bpf-linking.patch +Patch0030: backport-libbpf-Add-enum64-relocation-support.patch +Patch0031: backport-libbpf-Fix-sign-expansion-bug-in-btf_dump_get_enum_v.patch +Patch0032: backport-libbpf-Don-t-require-full-struct-enum64-in-UAPI-head.patch +Patch0033: backport-libbpf-Fix-an-unsigned-0-bug.patch + # This package supersedes libbpf from kernel-tools, # which has default Epoch: 0. By having Epoch: 1 @@ -85,6 +99,9 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Wed Aug 30 2023 liuxin 2:0.8.1-11 +- backport enum64 feature + * Mon Aug 14 2023 zhangmingyi 2:0.8.1-10 - backport patches from upstream: backport-libbpf-Ensure-FD-3-during-bpf_map__reuse_fd.patch