Skip to contentSkip to conversation

bpf: Add 64bit enum value support

PATCH Superseded Yonghong Song 2 participants 45 messages May 14, 2022 – May 18, 2022

Subsystems (7)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Add 64bit enum value support

Currently, btf only supports upto 32bit enum value with BTF_KIND_ENUM. But in kernel, some enum 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),

};
With BTF_KIND_ENUM, the value for BPF_F_CTXLEN_MASK will be encoded as 0 which is incorrect.

To solve this problem, BTF_KIND_ENUM64 is proposed in this patch set to support enum 64bit values. Also, since sometimes there is a need to generate C code from btf, e.g., vmlinux.h, btf kflag support is also added for BTF_KIND_ENUM and BTF_KIND_ENUM64 to indicate signedness, helping proper value printout.

Changelog:
v1 -> v2:
- Changed kflag default from signed to unsigned
- Fixed sanitization issue
- Broke down libbpf related patches for easier review
- Added more tests
- More code refactorization
- Corresponding llvm patch (to support enum64) is also updated

Yonghong Song (18):
bpf: Add btf enum64 support
libbpf: Permit 64bit relocation value
libbpf: Fix an error in 64bit relocation value computation
libbpf: Refactor btf__add_enum() for future code sharing
libbpf: Add enum64 parsing and new enum64 public API
libbpf: Add enum64 deduplication support
libbpf: Add enum64 support for btf_dump
libbpf: Add enum64 sanitization
libbpf: Add enum64 support for bpf linking
libbpf: Add enum64 relocation support
bpftool: Add btf enum64 support
selftests/bpf: Fix selftests failure
selftests/bpf: Test new enum kflag and enum64 API functions selftests/bpf: Add BTF_KIND_ENUM64 unit tests selftests/bpf: Test BTF_KIND_ENUM64 for deduplication selftests/bpf: Add a test for enum64 value relocations selftests/bpf: Clarify llvm dependency with possible selftest failures docs/bpf: Update documentation for BTF_KIND_ENUM64 support

 Documentation/bpf/btf.rst                     |  34 ++-
 include/linux/btf.h                           |  28 +++
 include/uapi/linux/btf.h                      |  17 +-
 kernel/bpf/btf.c                              | 142 +++++++++++--
 kernel/bpf/verifier.c                         |   2 +-
 tools/bpf/bpftool/btf.c                       |  49 ++++-
 tools/bpf/bpftool/btf_dumper.c                |  29 +++
 tools/bpf/bpftool/gen.c                       |   1 +
 tools/include/uapi/linux/btf.h                |  17 +-
 tools/lib/bpf/btf.c                           | 194 +++++++++++++++---
 tools/lib/bpf/btf.h                           |  32 ++-
 tools/lib/bpf/btf_dump.c                      | 135 +++++++++---
 tools/lib/bpf/libbpf.c                        |  60 +++++-
 tools/lib/bpf/libbpf.map                      |   2 +
 tools/lib/bpf/libbpf_internal.h               |   2 +
 tools/lib/bpf/linker.c                        |   2 +
 tools/lib/bpf/relo_core.c                     | 100 +++++----
 tools/lib/bpf/relo_core.h                     |   4 +-
 tools/testing/selftests/bpf/README.rst        |  18 ++
 tools/testing/selftests/bpf/btf_helpers.c     |  21 +-
 tools/testing/selftests/bpf/prog_tests/btf.c  | 153 ++++++++++++--
 .../selftests/bpf/prog_tests/btf_write.c      | 114 +++++++---
 .../selftests/bpf/prog_tests/core_reloc.c     |  58 ++++++
 .../bpf/progs/btf__core_reloc_enum64val.c     |   3 +
 .../progs/btf__core_reloc_enum64val___diff.c  |   3 +
 .../btf__core_reloc_enum64val___err_missing.c |   3 +
 ...btf__core_reloc_enum64val___val3_missing.c |   3 +
 .../selftests/bpf/progs/core_reloc_types.h    |  78 +++++++
 .../bpf/progs/test_core_reloc_enum64val.c     |  70 +++++++
 tools/testing/selftests/bpf/test_btf.h        |   1 +
 30 files changed, 1189 insertions(+), 186 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/18 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

---
 include/linux/btf.h            |  28 +++++++
 include/uapi/linux/btf.h       |  17 +++-
 kernel/bpf/btf.c               | 142 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +-
 tools/include/uapi/linux/btf.h |  17 +++-
 5 files changed, 185 insertions(+), 21 deletions(-)
include/linux/btf.h +28 -0
index 2611cea2c2b6..0c113ba38ced 100644
@@ -177,6 +177,19 @@ static inline bool btf_type_is_enum(const struct btf_type *t)
 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
 }
 
+static inline bool btf_type_is_any_enum(const struct btf_type *t)
+{
+	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
+	       BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
+}
+
+static inline bool btf_kind_core_compat(const struct btf_type *t1,
+					const struct btf_type *t2)
+{
+	return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
+	       (btf_type_is_any_enum(t1) && btf_type_is_any_enum(t2));
+}
+
 static inline bool str_is_empty(const char *s)
 {
 	return !s || !s[0];
@@ -192,6 +205,16 @@ 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 u64 btf_enum64_value(const struct btf_enum64 *e)
+{
+	return ((u64)e->val_hi32 << 32) | e->val_lo32;
+}
+
 static inline bool btf_is_composite(const struct btf_type *t)
 {
 	u16 kind = btf_kind(t);
@@ -332,6 +355,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 const struct btf_var_secinfo *btf_type_var_secinfo(
 		const struct btf_type *t)
 {
include/uapi/linux/btf.h +14 -3
index a9162a6c0284..ec1798b6d3ff 100644
@@ -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__ */
kernel/bpf/btf.c +128 -14

252 lines not on this page · raw message

kernel/bpf/verifier.c +1 -1
index 05c1b6656824..133caeffdbd4 100644
@@ -10655,7 +10655,7 @@ static int check_btf_func(struct bpf_verifier_env *env,
 			goto err_free;
 		ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL);
 		scalar_return =
-			btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type);
+			btf_type_is_small_int(ret_type) || btf_type_is_any_enum(ret_type);
 		if (i && !scalar_return && env->subprog_info[i].has_ld_abs) {
 			verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n");
 			goto err_free;
tools/include/uapi/linux/btf.h +14 -3
index a9162a6c0284..ec1798b6d3ff 100644
@@ -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__ */
Signature · 1 line
2.30.2
Acked-by on v2 1/18
#2 lore raw

Re: [PATCH bpf-next v2 01/18] bpf: Add btf enum64 support

Quoted 40 lines · Yonghong Song

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.

Quoted 8 lines

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

---

LGTM.

Quoted 7 lines
 include/linux/btf.h            |  28 +++++++
 include/uapi/linux/btf.h       |  17 +++-
 kernel/bpf/btf.c               | 142 +++++++++++++++++++++++++++++----
 kernel/bpf/verifier.c          |   2 +-
 tools/include/uapi/linux/btf.h |  17 +++-
 5 files changed, 185 insertions(+), 21 deletions(-)

[...]

#3 lore raw

PATCH v2 2/18 libbpf: Permit 64bit relocation value

In reply to Yonghong Song

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.

---
 tools/lib/bpf/relo_core.c | 49 +++++++++++++++++++++------------------
 tools/lib/bpf/relo_core.h |  4 ++--
 2 files changed, 29 insertions(+), 24 deletions(-)
tools/lib/bpf/relo_core.c +27 -22
index ba4453dfd1ed..aea16343a8f1 100644
@@ -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;
 		}
 
tools/lib/bpf/relo_core.h +2 -2
index 073039d8ca4f..7df0da082f2c 100644
@@ -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 */
Signature · 1 line
2.30.2
Acked-by on v2 2/18
#4 lore raw

Re: [PATCH bpf-next v2 02/18] libbpf: Permit 64bit relocation value

Quoted 8 lines · Yonghong Song

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.

---

LGTM

Quoted 4 lines
 tools/lib/bpf/relo_core.c | 49 +++++++++++++++++++++------------------
 tools/lib/bpf/relo_core.h |  4 ++--
 2 files changed, 29 insertions(+), 24 deletions(-)

[...]

#5 lore raw

PATCH v2 3/18 libbpf: Fix an error in 64bit relocation value computation

In reply to Yonghong Song

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.

---
 tools/lib/bpf/relo_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/relo_core.c +1 -1
index aea16343a8f1..78b16cda86fa 100644
@@ -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,
Signature · 1 line
2.30.2
Acked-by on v2 3/18
#6 lore raw

Re: [PATCH bpf-next v2 03/18] libbpf: Fix an error in 64bit relocation value computation

Quoted 11 lines · Yonghong Song

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.

Quoted 8 lines

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.

---

Quoted 19 lines
 tools/lib/bpf/relo_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/relo_core.c +1 -2
index aea16343a8f1..78b16cda86fa 100644
@@ -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,
--

2.30.2

#7 lore raw

PATCH v2 4/18 libbpf: Refactor btf__add_enum() for future code sharing

In reply to Yonghong Song

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.

---
 tools/lib/bpf/btf.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
tools/lib/bpf/btf.c +21 -15
index bb1e06eb1eca..9dd7f886732a 100644
@@ -2115,20 +2115,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;
@@ -2153,12 +2141,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;
Signature · 1 line
2.30.2
Acked-by on v2 4/18
#8 lore raw

Re: [PATCH bpf-next v2 04/18] libbpf: Refactor btf__add_enum() for future code sharing

Quoted 8 lines · Yonghong Song

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.

---

[...]

#9 lore raw

PATCH v2 5/18 libbpf: Add enum64 parsing and new enum64 public API

In reply to Yonghong Song

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.

---
 tools/lib/bpf/btf.c      | 103 +++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/btf.h      |  12 +++++
 tools/lib/bpf/libbpf.map |   2 +
 3 files changed, 117 insertions(+)
tools/lib/bpf/btf.c +103 -0
index 9dd7f886732a..3bb6780f710d 100644
@@ -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);
@@ -597,6 +607,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;
@@ -644,6 +655,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:
@@ -2162,6 +2174,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);
 }
 
@@ -2212,6 +2228,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;
@@ -4723,6 +4815,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:
@@ -4817,6 +4910,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);
 
tools/lib/bpf/btf.h +12 -0
index 951ac7475794..a41463bf9060 100644
@@ -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);
tools/lib/bpf/libbpf.map +2 -0
index 008da8db1d94..ebe41b6ed056 100644
@@ -450,6 +450,8 @@ LIBBPF_0.8.0 {
 		bpf_program__attach_trace_opts;
 		bpf_program__attach_usdt;
 		bpf_program__set_insns;
+		btf__add_enum64;
+		btf__add_enum64_value;
 		libbpf_register_prog_handler;
 		libbpf_unregister_prog_handler;
 } LIBBPF_0.7.0;
Signature · 1 line
2.30.2
Acked-by on v2 5/18
#10 lore raw

Re: [PATCH bpf-next v2 05/18] libbpf: Add enum64 parsing and new enum64 public API

Quoted 5 lines · Yonghong Song

Add enum64 parsing support and two new enum64 public APIs:
btf__add_enum64
btf__add_enum64_value

Quoted 7 lines

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.

---

Looks good, but new APIs will have to go into 1.0 in libbpf.map

Quoted 5 lines
 tools/lib/bpf/btf.c      | 103 +++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/btf.h      |  12 +++++
 tools/lib/bpf/libbpf.map |   2 +
 3 files changed, 117 insertions(+)

[...]

#11 lore raw

PATCH v2 6/18 libbpf: Add enum64 deduplication support

In reply to Yonghong Song

Add enum64 deduplication support. BTF_KIND_ENUM64 handling is very similar to BTF_KIND_ENUM.

---
 tools/lib/bpf/btf.c | 55 +++++++++++++++++++++++++++++++++------------
 tools/lib/bpf/btf.h |  5 +++++
 2 files changed, 46 insertions(+), 14 deletions(-)
tools/lib/bpf/btf.c +41 -14
index 3bb6780f710d..016cec84e3dd 100644
@@ -3568,7 +3568,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;
@@ -3580,17 +3580,12 @@ static long btf_hash_enum(struct btf_type *t)
 	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_enum32_val(struct btf_type *t1, struct btf_type *t2)
 {
 	const struct btf_enum *m1, *m2;
-	__u16 vlen;
+	__u16 vlen = btf_vlen(t1);
 	int i;
 
-	if (!btf_equal_common(t1, t2))
-		return false;
-
-	vlen = btf_vlen(t1);
 	m1 = btf_enum(t1);
 	m2 = btf_enum(t2);
 	for (i = 0; i < vlen; i++) {
@@ -3602,15 +3597,44 @@ static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
 	return true;
 }
 
+static bool btf_equal_enum64_val(struct btf_type *t1, struct btf_type *t2)
+{
+	const struct btf_enum64 *m1, *m2;
+	__u16 vlen = btf_vlen(t1);
+	int i;
+
+	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;
+}
+
+/* Check structural equality of two ENUMs. */
+static bool btf_equal_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)
+{
+	if (!btf_equal_common(t1, t2))
+		return false;
+
+	if (btf_is_enum(t1))
+		return btf_equal_enum32_val(t1, t2);
+	return btf_equal_enum64_val(t1, t2);
+}
+
 static inline bool btf_is_enum_fwd(struct btf_type *t)
 {
-	return btf_is_enum(t) && btf_vlen(t) == 0;
+	return btf_type_is_any_enum(t) && btf_vlen(t) == 0;
 }
 
-static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
+static bool btf_compat_enum_or_enum64(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);
+		return btf_equal_enum_or_enum64(t1, t2);
 	/* ignore vlen when comparing */
 	return t1->name_off == t2->name_off &&
 	       (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
@@ -3829,6 +3853,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:
@@ -3898,15 +3923,16 @@ 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;
 			cand = btf_type_by_id(d->btf, cand_id);
-			if (btf_equal_enum(t, cand)) {
+			if (btf_equal_enum_or_enum64(t, cand)) {
 				new_id = cand_id;
 				break;
 			}
-			if (btf_compat_enum(t, cand)) {
+			if (btf_compat_enum_or_enum64(t, cand)) {
 				if (btf_is_enum_fwd(t)) {
 					/* resolve fwd to full enum */
 					new_id = cand_id;
@@ -4211,7 +4237,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_enum_or_enum64(cand_type, canon_type);
 
 	case BTF_KIND_FWD:
 	case BTF_KIND_FLOAT:
tools/lib/bpf/btf.h +5 -0
index a41463bf9060..b22c648c69ff 100644
@@ -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_type_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));
Signature · 1 line
2.30.2
#12 lore raw

Re: [PATCH bpf-next v2 06/18] libbpf: Add enum64 deduplication support

Quoted 10 lines · Yonghong Song

Add enum64 deduplication support. BTF_KIND_ENUM64 handling is very similar to BTF_KIND_ENUM.

---
 tools/lib/bpf/btf.c | 55 +++++++++++++++++++++++++++++++++------------
 tools/lib/bpf/btf.h |  5 +++++
 2 files changed, 46 insertions(+), 14 deletions(-)

[...]

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+static bool btf_equal_enum64_val(struct btf_type *t1, struct btf_type *t2)
+{
+       const struct btf_enum64 *m1, *m2;
+       __u16 vlen = btf_vlen(t1);
+       int i;
+
+       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 ||

tools/lib/bpf/btf.c static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) · line 3619

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+                   m1->val_hi32 != m2->val_hi32)
+                       return false;
+               m1++;
+               m2++;
+       }
+       return true;
+}
+
+/* Check structural equality of two ENUMs. */
+static bool btf_equal_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)

I find this helper quite confusing. It implies it can compare any enum or enum64 with each other, but it really allows only enum vs enum and enum64 vs enum64 (as it should!). Let's keep btf_equal_enum()/btf_compat_enum() completely intact and add btf_equal_enum64()/btf_compat_enum64() separately (few lines of copy-pasted code is totally fine to keep them separate, IMO). See below.

Quoted 33 lines
tools/lib/bpf/btf.c +13 -3
+{
+       if (!btf_equal_common(t1, t2))
+               return false;
+
+       if (btf_is_enum(t1))
+               return btf_equal_enum32_val(t1, t2);
+       return btf_equal_enum64_val(t1, t2);
+}
+
 static inline bool btf_is_enum_fwd(struct btf_type *t)
 {
-       return btf_is_enum(t) && btf_vlen(t) == 0;
+       return btf_type_is_any_enum(t) && btf_vlen(t) == 0;
 }
 
-static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
+static bool btf_compat_enum_or_enum64(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);
+               return btf_equal_enum_or_enum64(t1, t2);
        /* ignore vlen when comparing */
        return t1->name_off == t2->name_off &&
               (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
@@ -3829,6 +3853,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:

tools/lib/bpf/btf.c static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) · line 3931

Quoted 10 lines
tools/lib/bpf/btf.c +2 -1
@@ -3898,15 +3923,16 @@ 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;
                        cand = btf_type_by_id(d->btf, cand_id);
-                       if (btf_equal_enum(t, cand)) {
+                       if (btf_equal_enum_or_enum64(t, cand)) {

I'd rather have this enum vs enum64 distinction right here:

if ((btf_is_enum(t) && btf_equal_enum(t, cand)) ||

    (btf_is_enum64(t) && btf_equal_enum64(t, cand))) { ... }

tools/lib/bpf/btf.c static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) · line 3935

Quoted 5 lines
tools/lib/bpf/btf.c +1 -1
                                new_id = cand_id;
                                break;
                        }
-                       if (btf_compat_enum(t, cand)) {
+                       if (btf_compat_enum_or_enum64(t, cand)) {

and same here with (btf_is_enum && btf_compat_enum) || (btf_is_num64 && btf_compat_enum64) ?

Basically, I'd like to avoid worrying if we are accidentally mixing enum and enum64 or not. WDYT?

tools/lib/bpf/btf.c static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, · line 4242

Quoted 11 lines
tools/lib/bpf/btf.c +2 -1
                                if (btf_is_enum_fwd(t)) {
                                        /* resolve fwd to full enum */
                                        new_id = cand_id;
@@ -4211,7 +4237,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_enum_or_enum64(cand_type, canon_type);
 

and here we just know whether we need btf_compat_enum vs btf_compat_enum64.

Quoted 21 lines
tools/lib/bpf/btf.c +0 -0
        case BTF_KIND_FWD:
        case BTF_KIND_FLOAT:
tools/lib/bpf/btf.h +5 -1
index a41463bf9060..b22c648c69ff 100644
@@ -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_type_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));
--

2.30.2

#13 lore raw

Re: [PATCH bpf-next v2 06/18] libbpf: Add enum64 deduplication support

Quoted 35 lines · Andrii Nakryiko
Quoted 10 lines · Yonghong Song

Add enum64 deduplication support. BTF_KIND_ENUM64 handling is very similar to BTF_KIND_ENUM.

---
  tools/lib/bpf/btf.c | 55 +++++++++++++++++++++++++++++++++------------
  tools/lib/bpf/btf.h |  5 +++++
  2 files changed, 46 insertions(+), 14 deletions(-)

[...]

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+static bool btf_equal_enum64_val(struct btf_type *t1, struct btf_type *t2)
+{
+       const struct btf_enum64 *m1, *m2;
+       __u16 vlen = btf_vlen(t1);
+       int i;
+
+       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 ||

tools/lib/bpf/btf.c static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) · line 3619

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+                   m1->val_hi32 != m2->val_hi32)
+                       return false;
+               m1++;
+               m2++;
+       }
+       return true;
+}
+
+/* Check structural equality of two ENUMs. */
+static bool btf_equal_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)
Quoted 7 lines

I find this helper quite confusing. It implies it can compare any enum or enum64 with each other, but it really allows only enum vs enum and enum64 vs enum64 (as it should!). Let's keep btf_equal_enum()/btf_compat_enum() completely intact and add btf_equal_enum64()/btf_compat_enum64() separately (few lines of copy-pasted code is totally fine to keep them separate, IMO). See below.

I debate with myself about whether I should use separate functions or use one function for both enum/enum64. My current approach will have less code changes. But I can do what you suggested to have separate functions for enum and enum64. This will apply to btf_compat_enum as well.

Quoted 16 lines
Quoted 16 lines
tools/lib/bpf/btf.c +10 -1
+{
+       if (!btf_equal_common(t1, t2))
+               return false;
+
+       if (btf_is_enum(t1))
+               return btf_equal_enum32_val(t1, t2);
+       return btf_equal_enum64_val(t1, t2);
+}
+
  static inline bool btf_is_enum_fwd(struct btf_type *t)
  {
-       return btf_is_enum(t) && btf_vlen(t) == 0;
+       return btf_type_is_any_enum(t) && btf_vlen(t) == 0;
  }

[...]

#14 lore raw

Re: [PATCH bpf-next v2 06/18] libbpf: Add enum64 deduplication support

Quoted 47 lines · Yonghong Song
Quoted 35 lines · Andrii Nakryiko
Quoted 10 lines · Yonghong Song

Add enum64 deduplication support. BTF_KIND_ENUM64 handling is very similar to BTF_KIND_ENUM.

---
  tools/lib/bpf/btf.c | 55 +++++++++++++++++++++++++++++++++------------
  tools/lib/bpf/btf.h |  5 +++++
  2 files changed, 46 insertions(+), 14 deletions(-)

[...]

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+static bool btf_equal_enum64_val(struct btf_type *t1, struct btf_type *t2)
+{
+       const struct btf_enum64 *m1, *m2;
+       __u16 vlen = btf_vlen(t1);
+       int i;
+
+       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 ||

tools/lib/bpf/btf.c static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) · line 3619

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+                   m1->val_hi32 != m2->val_hi32)
+                       return false;
+               m1++;
+               m2++;
+       }
+       return true;
+}
+
+/* Check structural equality of two ENUMs. */
+static bool btf_equal_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)
Quoted 7 lines

I find this helper quite confusing. It implies it can compare any enum or enum64 with each other, but it really allows only enum vs enum and enum64 vs enum64 (as it should!). Let's keep btf_equal_enum()/btf_compat_enum() completely intact and add btf_equal_enum64()/btf_compat_enum64() separately (few lines of copy-pasted code is totally fine to keep them separate, IMO). See below.

Quoted 5 lines

I debate with myself about whether I should use separate functions or use one function for both enum/enum64. My current approach will have less code changes. But I can do what you suggested to have separate functions for enum and enum64. This will apply to btf_compat_enum as well.

yep, thanks!

Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/btf.c +10 -1
+{
+       if (!btf_equal_common(t1, t2))
+               return false;
+
+       if (btf_is_enum(t1))
+               return btf_equal_enum32_val(t1, t2);
+       return btf_equal_enum64_val(t1, t2);
+}
+
  static inline bool btf_is_enum_fwd(struct btf_type *t)
  {
-       return btf_is_enum(t) && btf_vlen(t) == 0;
+       return btf_type_is_any_enum(t) && btf_vlen(t) == 0;
  }

[...]

#15 lore raw

PATCH v2 7/18 libbpf: Add enum64 support for btf_dump

In reply to Yonghong Song

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.

---
 tools/lib/bpf/btf.h      |   5 ++
 tools/lib/bpf/btf_dump.c | 135 ++++++++++++++++++++++++++++++---------
 2 files changed, 110 insertions(+), 30 deletions(-)
tools/lib/bpf/btf.h +5 -0
index b22c648c69ff..7da6970b8c9f 100644
@@ -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);
tools/lib/bpf/btf_dump.c +105 -30

212 lines not on this page · raw message

Signature · 1 line
2.30.2
#16 lore raw

Re: [PATCH bpf-next v2 07/18] libbpf: Add enum64 support for btf_dump

Quoted 11 lines · Yonghong Song

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.

---
 tools/lib/bpf/btf.h      |   5 ++
 tools/lib/bpf/btf_dump.c | 135 ++++++++++++++++++++++++++++++---------
 2 files changed, 110 insertions(+), 30 deletions(-)

[...]

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 997

Quoted 13 lines
tools/lib/bpf/btf_dump.c +3 -4
@@ -989,38 +992,88 @@ 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);

why passing it from outside if we can just get it from t? you don't do it for kflag, for example, so I see no reason to do that for vlen here

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 1006

Quoted 7 lines
tools/lib/bpf/btf_dump.c +3 -0
+       bool is_signed = btf_kflag(t);
+       const char *fmt_str;
        const char *name;
        size_t dup_cnt;
        int i;
 
+

nit: extra empty line?

Quoted 6 lines
tools/lib/bpf/btf_dump.c +6 -0
+       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,"

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 1022

Quoted 10 lines
tools/lib/bpf/btf_dump.c +10 -0
+                                           : "\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);

100 character lines are ok now, try to make all those statements single-line, if possible

Quoted 4 lines
tools/lib/bpf/btf_dump.c +4 -0
+               }
+       }
+}
+

[...]

#17 lore raw

Re: [PATCH bpf-next v2 07/18] libbpf: Add enum64 support for btf_dump

Quoted 19 lines · Andrii Nakryiko
Quoted 11 lines · Yonghong Song

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.

---
  tools/lib/bpf/btf.h      |   5 ++
  tools/lib/bpf/btf_dump.c | 135 ++++++++++++++++++++++++++++++---------
  2 files changed, 110 insertions(+), 30 deletions(-)

[...]

Quoted 4 lines
tools/lib/bpf/btf_dump.c +0 -0
@@ -989,38 +992,88 @@ 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));
  }
 
Quoted 12 lines

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 997

Quoted 9 lines
tools/lib/bpf/btf_dump.c +3 -4
-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);

why passing it from outside if we can just get it from t? you don't do it for kflag, for example, so I see no reason to do that for vlen here

We have a vlen passed in because we have a check for vlen outside btf_dump_emit_enum32_val. Basically this function and some other codes will not be executed if vlen == 0.

Quoted 10 lines

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 1006

Quoted 8 lines
tools/lib/bpf/btf_dump.c +3 -0
+       bool is_signed = btf_kflag(t);
+       const char *fmt_str;
         const char *name;
         size_t dup_cnt;
         int i;
 
+

nit: extra empty line?

sure.

Quoted 18 lines
Quoted 7 lines
tools/lib/bpf/btf_dump.c +6 -0
+       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,"

tools/lib/bpf/btf_dump.c static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, · line 1022

Quoted 10 lines
tools/lib/bpf/btf_dump.c +10 -0
+                                           : "\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);
Quoted 2 lines

100 character lines are ok now, try to make all those statements single-line, if possible

okay.

Quoted 7 lines
Quoted 5 lines
tools/lib/bpf/btf_dump.c +4 -0
+               }
+       }
+}
+

[...]

#18 lore raw

PATCH v2 8/18 libbpf: Add enum64 sanitization

In reply to Yonghong Song

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.

---
 tools/lib/bpf/btf.h             |  3 +-
 tools/lib/bpf/libbpf.c          | 53 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf_internal.h |  2 ++
 3 files changed, 55 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.h +2 -1
index 7da6970b8c9f..d4fe1300ed33 100644
@@ -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)
 {
tools/lib/bpf/libbpf.c +51 -2

100 lines not on this page · raw message

tools/lib/bpf/libbpf_internal.h +2 -0
index 4abdbe2fea9d..10c16acfa8ae 100644
@@ -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,
 };
 
Signature · 1 line
2.30.2
#19 lore raw

Re: [PATCH bpf-next v2 08/18] libbpf: Add enum64 sanitization

Quoted 73 lines · Yonghong Song

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.

---
 tools/lib/bpf/btf.h             |  3 +-
 tools/lib/bpf/libbpf.c          | 53 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf_internal.h |  2 ++
 3 files changed, 55 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.h +2 -1
index 7da6970b8c9f..d4fe1300ed33 100644
@@ -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)
 {
tools/lib/bpf/libbpf.c +9 -1
index 4867a930628b..f54e70b9953d 100644
@@ -2114,6 +2114,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";
        }
 }
@@ -2642,9 +2643,10 @@ 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)
@@ -2655,9 +2657,25 @@ 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);
+       int min_int_size = 32, min_enum64_size = 32, min_int_tid = 0;
        struct btf_type *t;
        int i, j, vlen;
 
+       if (!has_enum64) {
+               for (i = 1; i < btf__type_cnt(btf); i++) {
+                       t = (struct btf_type *)btf__type_by_id(btf, i);
+                       if (btf_is_int(t) && t->size < min_int_size) {

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) · line 2678

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+                               min_int_size = t->size;
+                               min_int_tid = i;
+                       } else if (btf_is_enum64(t) && t->size < min_enum64_size) {
+                               min_enum64_size = t->size;
+                       }
+               }
+               if (min_int_size > min_enum64_size)
+                       min_int_tid = btf__add_int(btf, "char", 1,  BTF_INT_SIGNED);
+       }
+

we do this search even if bpf_object's BTF doesn't have enum64, which seems overly pessimistic. How about we just lazily (but unconditionally) add new BTF_KIND_INT on first encountered enum64 and remember it's id (see below)

Quoted 5 lines
tools/lib/bpf/libbpf.c +0 -0
        for (i = 1; i < btf__type_cnt(btf); i++) {
                t = (struct btf_type *)btf__type_by_id(btf, i);
 
@@ -2717,7 +2735,20 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
                        /* replace TYPE_TAG with a CONST */

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) · line 2744

Quoted 10 lines
tools/lib/bpf/libbpf.c +7 -1
                        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 = btf_members(t);
+

so here we just

if (enum64_placeholder_id == 0) {
enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", t->size, 0); if (enum64_placeholder_id < 0) /* pr_warn and exit with error */
}

and then just use enum64_placeholder_id for each field type?

It seems much simpler than trying to find matching int (especially given potentially non-8-byte size), so it seems better to just add our own type.

Please make sure to re-initialize t and m after that because btf__add_int() invalidates underlying memory, so you need to re-fetch btf__type_by_id().

Quoted 65 lines
tools/lib/bpf/libbpf.c +25 -0
+                       vlen = btf_vlen(t);
+                       t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
+                       for (j = 0; j < vlen; j++, m++) {
+                               m->type = min_int_tid;
+                               m->offset = 0;
+                       }
+                }
        }
 }
 
@@ -3563,6 +3594,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;
@@ -4746,6 +4781,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,
@@ -4811,6 +4857,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)
tools/lib/bpf/libbpf_internal.h +2 -1
index 4abdbe2fea9d..10c16acfa8ae 100644
@@ -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,
 };
 
--

2.30.2

#20 lore raw

Re: [PATCH bpf-next v2 08/18] libbpf: Add enum64 sanitization

Quoted 113 lines · Andrii Nakryiko
Quoted 73 lines · Yonghong Song

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.

---
  tools/lib/bpf/btf.h             |  3 +-
  tools/lib/bpf/libbpf.c          | 53 +++++++++++++++++++++++++++++++--
  tools/lib/bpf/libbpf_internal.h |  2 ++
  3 files changed, 55 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.h +2 -1
index 7da6970b8c9f..d4fe1300ed33 100644
@@ -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)
  {
tools/lib/bpf/libbpf.c +9 -1
index 4867a930628b..f54e70b9953d 100644
@@ -2114,6 +2114,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";
         }
  }
@@ -2642,9 +2643,10 @@ 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)
@@ -2655,9 +2657,25 @@ 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);
+       int min_int_size = 32, min_enum64_size = 32, min_int_tid = 0;
         struct btf_type *t;
         int i, j, vlen;
 
+       if (!has_enum64) {
+               for (i = 1; i < btf__type_cnt(btf); i++) {
+                       t = (struct btf_type *)btf__type_by_id(btf, i);
+                       if (btf_is_int(t) && t->size < min_int_size) {

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) · line 2678

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+                               min_int_size = t->size;
+                               min_int_tid = i;
+                       } else if (btf_is_enum64(t) && t->size < min_enum64_size) {
+                               min_enum64_size = t->size;
+                       }
+               }
+               if (min_int_size > min_enum64_size)
+                       min_int_tid = btf__add_int(btf, "char", 1,  BTF_INT_SIGNED);
+       }
+

we do this search even if bpf_object's BTF doesn't have enum64, which seems overly pessimistic. How about we just lazily (but unconditionally) add new BTF_KIND_INT on first encountered enum64 and remember it's id (see below)

Quoted 5 lines
tools/lib/bpf/libbpf.c +0 -0
         for (i = 1; i < btf__type_cnt(btf); i++) {
                 t = (struct btf_type *)btf__type_by_id(btf, i);
 
@@ -2717,7 +2735,20 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
                         /* replace TYPE_TAG with a CONST */

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) · line 2744

Quoted 10 lines
tools/lib/bpf/libbpf.c +7 -1
                         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 = btf_members(t);
+

so here we just

if (enum64_placeholder_id == 0) {
enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", t->size, 0); if (enum64_placeholder_id < 0) /* pr_warn and exit with error */
}

Quoted 5 lines

and then just use enum64_placeholder_id for each field type?

It seems much simpler than trying to find matching int (especially given potentially non-8-byte size), so it seems better to just add our own type.

Besides this implementation, I had another idea to just add one int type unconditionally for enum64->union member type. To avoid adding this type unconditionally, I added the search part.

Let me try the approach to add the new int type in the fly when needed.

Quoted 15 lines

Please make sure to re-initialize t and m after that because btf__add_int() invalidates underlying memory, so you need to re-fetch btf__type_by_id().

Quoted 10 lines
tools/lib/bpf/libbpf.c +7 -0
+                       vlen = btf_vlen(t);
+                       t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
+                       for (j = 0; j < vlen; j++, m++) {
+                               m->type = min_int_tid;
+                               m->offset = 0;
+                       }
+                }
         }
  }
 

[...]

#21 lore raw

PATCH v2 9/18 libbpf: Add enum64 support for bpf linking

In reply to Yonghong Song

Add BTF_KIND_ENUM64 support for bpf linking, which is very similar to BTF_KIND_ENUM.

---
 tools/lib/bpf/linker.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/linker.c +2 -0
index 9aa016fb55aa..979b150affb9 100644
@@ -1340,6 +1340,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:
@@ -1362,6 +1363,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",
Signature · 1 line
2.30.2
Acked-by on v2 9/18
#22 lore raw

Re: [PATCH bpf-next v2 09/18] libbpf: Add enum64 support for bpf linking

Quoted 6 lines · Yonghong Song

Add BTF_KIND_ENUM64 support for bpf linking, which is very similar to BTF_KIND_ENUM.

---

LGTM :)

Quoted 26 lines
 tools/lib/bpf/linker.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/linker.c +2 -1
index 9aa016fb55aa..979b150affb9 100644
@@ -1340,6 +1340,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:
@@ -1362,6 +1363,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",
--

2.30.2

#23 lore raw

PATCH v2 10/18 libbpf: Add enum64 relocation support

In reply to Yonghong Song

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.

---
 tools/lib/bpf/btf.h       |  7 ++++++
 tools/lib/bpf/libbpf.c    |  7 +++---
 tools/lib/bpf/relo_core.c | 49 ++++++++++++++++++++++++++-------------
 3 files changed, 44 insertions(+), 19 deletions(-)
tools/lib/bpf/btf.h +7 -0
index d4fe1300ed33..aa2b38abdd2c 100644
@@ -537,6 +537,13 @@ static inline bool btf_type_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_type_is_any_enum(t1) && btf_type_is_any_enum(t2));
+}
+
 static inline __u8 btf_int_encoding(const struct btf_type *t)
 {
 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
tools/lib/bpf/libbpf.c +4 -3
index f54e70b9953d..99d3f21c5455 100644
@@ -5402,7 +5402,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);
@@ -5616,7 +5616,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:
@@ -5629,7 +5629,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)) {
@@ -5637,6 +5637,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:
tools/lib/bpf/relo_core.c +33 -16

127 lines not on this page · raw message

Signature · 1 line
2.30.2
#24 lore raw

Re: [PATCH bpf-next v2 10/18] libbpf: Add enum64 relocation support

Quoted 13 lines · Yonghong Song

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.

---
 tools/lib/bpf/btf.h       |  7 ++++++
 tools/lib/bpf/libbpf.c    |  7 +++---
 tools/lib/bpf/relo_core.c | 49 ++++++++++++++++++++++++++-------------
 3 files changed, 44 insertions(+), 19 deletions(-)

[...]

tools/lib/bpf/relo_core.c static int bpf_core_spec_match(struct bpf_core_spec *local_spec, · line 505

Quoted 13 lines
        memset(targ_spec, 0, sizeof(*targ_spec));
        targ_spec->btf = targ_btf;
tools/lib/bpf/relo_core.c +1 -2
@@ -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_type_is_any_enum(targ_type))

just noticed this discrepancy, can you please rename s/btf_type_is_any_enum/btf_is_any_enum/ so it's consistent with btf_is_enum and btf_is_enum64?

Quoted 17 lines
tools/lib/bpf/relo_core.c +7 -2
                        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;

tools/lib/bpf/relo_core.c static int bpf_core_calc_field_relo(const char *prog_name, · line 691

Quoted 3 lines
tools/lib/bpf/relo_core.c +0 -0
@@ -681,7 +689,7 @@ static int bpf_core_calc_field_relo(const char *prog_name,
                break;
        case BPF_CORE_FIELD_SIGNED:
                /* enums will be assumed unsigned */

we don't have to assume anymore, right? let's use kflag for btf_is_any_enum()?

Quoted 5 lines
tools/lib/bpf/relo_core.c +1 -1
-               *val = btf_is_enum(mt) ||
+               *val = btf_type_is_any_enum(mt) ||
                       (btf_int_encoding(mt) & BTF_INT_SIGNED);
                if (validate)
                        *validate = true; /* signedness is never ambiguous */

[...]

Quoted 9 lines
tools/lib/bpf/relo_core.c +2 -2
@@ -1089,10 +1097,19 @@ 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;
 

tools/lib/bpf/relo_core.c int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s · line 1111

Quoted 10 lines
tools/lib/bpf/relo_core.c +9 -1
-               append_buf("::%s = %u", s, e->val);
+                       e = btf_enum(t) + spec->raw_spec[0];
+                       s = btf__name_by_offset(spec->btf, e->name_off);
+                       append_buf("::%s = %u", s, e->val);
+               } else {
+                       const struct btf_enum64 *e;
+
+                       e = btf_enum64(t) + spec->raw_spec[0];
+                       s = btf__name_by_offset(spec->btf, e->name_off);
+                       append_buf("::%s = %llu", s, btf_enum64_value(e));

nit: we do have a sign bit now, so maybe let's print %lld or %llu (same for %d and %u above)? btw, please cast (unsigned long long) here

Quoted 7 lines
tools/lib/bpf/relo_core.c +1 -1
+               }
                return len;
        }
 
--

2.30.2

#25 lore raw

Re: [PATCH bpf-next v2 10/18] libbpf: Add enum64 relocation support

Quoted 26 lines · Andrii Nakryiko
Quoted 13 lines · Yonghong Song

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.

---
  tools/lib/bpf/btf.h       |  7 ++++++
  tools/lib/bpf/libbpf.c    |  7 +++---
  tools/lib/bpf/relo_core.c | 49 ++++++++++++++++++++++++++-------------
  3 files changed, 44 insertions(+), 19 deletions(-)

[...]

Quoted 9 lines
         memset(targ_spec, 0, sizeof(*targ_spec));
         targ_spec->btf = targ_btf;
tools/lib/bpf/relo_core.c +0 -1
@@ -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;
 
Quoted 8 lines

tools/lib/bpf/relo_core.c static int bpf_core_spec_match(struct bpf_core_spec *local_spec, · line 505

Quoted 4 lines
tools/lib/bpf/relo_core.c +1 -1
                 /* 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_type_is_any_enum(targ_type))

just noticed this discrepancy, can you please rename s/btf_type_is_any_enum/btf_is_any_enum/ so it's consistent with btf_is_enum and btf_is_enum64?

okay.

Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/relo_core.c +7 -2
                         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);
Quoted 8 lines

tools/lib/bpf/relo_core.c static int bpf_core_calc_field_relo(const char *prog_name, · line 691

Quoted 6 lines
tools/lib/bpf/relo_core.c +0 -0
                         if (targ_essent_len != local_essent_len)
                                 continue;
@@ -681,7 +689,7 @@ static int bpf_core_calc_field_relo(const char *prog_name,
                 break;
         case BPF_CORE_FIELD_SIGNED:
                 /* enums will be assumed unsigned */

we don't have to assume anymore, right? let's use kflag for btf_is_any_enum()?

old comment is not accurate any more, will remove.

Quoted 18 lines
Quoted 6 lines
tools/lib/bpf/relo_core.c +1 -1
-               *val = btf_is_enum(mt) ||
+               *val = btf_type_is_any_enum(mt) ||
                        (btf_int_encoding(mt) & BTF_INT_SIGNED);
                 if (validate)
                         *validate = true; /* signedness is never ambiguous */

[...]

Quoted 9 lines
tools/lib/bpf/relo_core.c +2 -2
@@ -1089,10 +1097,19 @@ 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;
 
Quoted 13 lines

tools/lib/bpf/relo_core.c int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s · line 1111

Quoted 10 lines
tools/lib/bpf/relo_core.c +9 -1
-               append_buf("::%s = %u", s, e->val);
+                       e = btf_enum(t) + spec->raw_spec[0];
+                       s = btf__name_by_offset(spec->btf, e->name_off);
+                       append_buf("::%s = %u", s, e->val);
+               } else {
+                       const struct btf_enum64 *e;
+
+                       e = btf_enum64(t) + spec->raw_spec[0];
+                       s = btf__name_by_offset(spec->btf, e->name_off);
+                       append_buf("::%s = %llu", s, btf_enum64_value(e));

nit: we do have a sign bit now, so maybe let's print %lld or %llu (same for %d and %u above)? btw, please cast (unsigned long long) here

will do.

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/relo_core.c +1 -1
+               }
                 return len;
         }
 
--

2.30.2

#26 lore raw

PATCH v2 11/18 bpftool: Add btf enum64 support

In reply to Yonghong Song

Add BTF_KIND_ENUM64 support.
For example, the following enum is defined in uapi bpf.h.

  $ cat core.c
  enum A {
        BPF_F_INDEX_MASK                = 0xffffffffULL,
        BPF_F_CURRENT_CPU               = BPF_F_INDEX_MASK,
        BPF_F_CTXLEN_MASK               = (0xfffffULL << 32),
  } g;
Compiled with
  clang -target bpf -O2 -g -c core.c
Using bpftool to dump types and generate format C file:
  $ bpftool btf dump file core.o
  ...
  [1] ENUM64 'A' size=8 vlen=3
        'BPF_F_INDEX_MASK' val=4294967295ULL
        'BPF_F_CURRENT_CPU' val=4294967295ULL
        'BPF_F_CTXLEN_MASK' val=4503595332403200ULL
  $ bpftool btf dump file core.o format c
  ...
  enum A {
        BPF_F_INDEX_MASK = 4294967295ULL,
        BPF_F_CURRENT_CPU = 4294967295ULL,
        BPF_F_CTXLEN_MASK = 4503595332403200ULL,
  };
  ...

The 64bit value is represented properly in BTF and C dump.

---
 tools/bpf/bpftool/btf.c        | 49 ++++++++++++++++++++++++++++++++--
 tools/bpf/bpftool/btf_dumper.c | 29 ++++++++++++++++++++
 tools/bpf/bpftool/gen.c        |  1 +
 3 files changed, 77 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +47 -2
index a2c665beda87..9e5db870fe53 100644
@@ -40,6 +40,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_FLOAT]	= "FLOAT",
 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
 	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
+	[BTF_KIND_ENUM64]	= "ENUM64",
 };
 
 struct btf_attach_point {
@@ -228,10 +229,54 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
 			if (json_output) {
 				jsonw_start_object(w);
 				jsonw_string_field(w, "name", name);
-				jsonw_uint_field(w, "val", v->val);
+				if (btf_kflag(t))
+					jsonw_int_field(w, "val", v->val);
+				else
+					jsonw_uint_field(w, "val", v->val);
 				jsonw_end_object(w);
 			} else {
-				printf("\n\t'%s' val=%u", name, v->val);
+				if (btf_kflag(t))
+					printf("\n\t'%s' val=%d", name, v->val);
+				else
+					printf("\n\t'%s' val=%u", name, v->val);
+			}
+		}
+		if (json_output)
+			jsonw_end_array(w);
+		break;
+	}
+	case BTF_KIND_ENUM64: {
+		const struct btf_enum64 *v = btf_enum64(t);
+		__u16 vlen = btf_vlen(t);
+		int i;
+
+		if (json_output) {
+			jsonw_uint_field(w, "size", t->size);
+			jsonw_uint_field(w, "vlen", vlen);
+			jsonw_name(w, "values");
+			jsonw_start_array(w);
+		} else {
+			printf(" size=%u vlen=%u", t->size, vlen);
+		}
+		for (i = 0; i < vlen; i++, v++) {
+			const char *name = btf_str(btf, v->name_off);
+			__u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32;
+
+			if (json_output) {
+				jsonw_start_object(w);
+				jsonw_string_field(w, "name", name);
+				if (btf_kflag(t))
+					jsonw_int_field(w, "val", val);
+				else
+					jsonw_uint_field(w, "val", val);
+				jsonw_end_object(w);
+			} else {
+				if (btf_kflag(t))
+					printf("\n\t'%s' val=%lldLL", name,
+					       (unsigned long long)val);
+				else
+					printf("\n\t'%s' val=%lluULL", name,
+					       (unsigned long long)val);
 			}
 		}
 		if (json_output)
tools/bpf/bpftool/btf_dumper.c +29 -0
index f5dddf8ef404..125798b0bc5d 100644
@@ -182,6 +182,32 @@ static int btf_dumper_enum(const struct btf_dumper *d,
 	return 0;
 }
 
+static int btf_dumper_enum64(const struct btf_dumper *d,
+			     const struct btf_type *t,
+			     const void *data)
+{
+	const struct btf_enum64 *enums = btf_enum64(t);
+	__u32 val_lo32, val_hi32;
+	__u64 value;
+	__u16 i;
+
+	value = *(__u64 *)data;
+	val_lo32 = (__u32)value;
+	val_hi32 = value >> 32;
+
+	for (i = 0; i < btf_vlen(t); i++) {
+		if (val_lo32 == enums[i].val_lo32 && val_hi32 == enums[i].val_hi32) {
+			jsonw_string(d->jw,
+				     btf__name_by_offset(d->btf,
+							 enums[i].name_off));
+			return 0;
+		}
+	}
+
+	jsonw_int(d->jw, value);
+	return 0;
+}
+
 static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
 			 const char *s)
 {
@@ -542,6 +568,8 @@ static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
 		return btf_dumper_array(d, type_id, data);
 	case BTF_KIND_ENUM:
 		return btf_dumper_enum(d, t, data);
+	case BTF_KIND_ENUM64:
+		return btf_dumper_enum64(d, t, data);
 	case BTF_KIND_PTR:
 		btf_dumper_ptr(d, t, data);
 		return 0;
@@ -618,6 +646,7 @@ static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
 			      btf__name_by_offset(btf, t->name_off));
 		break;
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		BTF_PRINT_ARG("enum %s ",
 			      btf__name_by_offset(btf, t->name_off));
 		break;
tools/bpf/bpftool/gen.c +1 -0
index 4c9477ff748d..14f5fe7f570e 100644
@@ -1747,6 +1747,7 @@ btfgen_mark_type(struct btfgen_info *info, unsigned int type_id, bool follow_poi
 	case BTF_KIND_INT:
 	case BTF_KIND_FLOAT:
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 	case BTF_KIND_STRUCT:
 	case BTF_KIND_UNION:
 		break;
Signature · 1 line
2.30.2
#27 lore raw

Re: [PATCH bpf-next v2 11/18] bpftool: Add btf enum64 support

Quoted 81 lines · Yonghong Song

Add BTF_KIND_ENUM64 support.
For example, the following enum is defined in uapi bpf.h.

  $ cat core.c
  enum A {
        BPF_F_INDEX_MASK                = 0xffffffffULL,
        BPF_F_CURRENT_CPU               = BPF_F_INDEX_MASK,
        BPF_F_CTXLEN_MASK               = (0xfffffULL << 32),
  } g;
Compiled with
  clang -target bpf -O2 -g -c core.c
Using bpftool to dump types and generate format C file:
  $ bpftool btf dump file core.o
  ...
  [1] ENUM64 'A' size=8 vlen=3
        'BPF_F_INDEX_MASK' val=4294967295ULL
        'BPF_F_CURRENT_CPU' val=4294967295ULL
        'BPF_F_CTXLEN_MASK' val=4503595332403200ULL
  $ bpftool btf dump file core.o format c
  ...
  enum A {
        BPF_F_INDEX_MASK = 4294967295ULL,
        BPF_F_CURRENT_CPU = 4294967295ULL,
        BPF_F_CTXLEN_MASK = 4503595332403200ULL,
  };
  ...

The 64bit value is represented properly in BTF and C dump.

---
 tools/bpf/bpftool/btf.c        | 49 ++++++++++++++++++++++++++++++++--
 tools/bpf/bpftool/btf_dumper.c | 29 ++++++++++++++++++++
 tools/bpf/bpftool/gen.c        |  1 +
 3 files changed, 77 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +25 -2
index a2c665beda87..9e5db870fe53 100644
@@ -40,6 +40,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
        [BTF_KIND_FLOAT]        = "FLOAT",
        [BTF_KIND_DECL_TAG]     = "DECL_TAG",
        [BTF_KIND_TYPE_TAG]     = "TYPE_TAG",
+       [BTF_KIND_ENUM64]       = "ENUM64",
 };
 
 struct btf_attach_point {
@@ -228,10 +229,54 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
                        if (json_output) {
                                jsonw_start_object(w);
                                jsonw_string_field(w, "name", name);
-                               jsonw_uint_field(w, "val", v->val);
+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", v->val);
+                               else
+                                       jsonw_uint_field(w, "val", v->val);
                                jsonw_end_object(w);
                        } else {
-                               printf("\n\t'%s' val=%u", name, v->val);
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%d", name, v->val);
+                               else
+                                       printf("\n\t'%s' val=%u", name, v->val);
+                       }
+               }
+               if (json_output)
+                       jsonw_end_array(w);
+               break;
+       }
+       case BTF_KIND_ENUM64: {
+               const struct btf_enum64 *v = btf_enum64(t);
+               __u16 vlen = btf_vlen(t);
+               int i;
+
+               if (json_output) {
+                       jsonw_uint_field(w, "size", t->size);
+                       jsonw_uint_field(w, "vlen", vlen);
+                       jsonw_name(w, "values");
+                       jsonw_start_array(w);

tools/bpf/bpftool/btf.c static int dump_btf_type(const struct btf *btf, __u32 id, · line 267

Quoted 10 lines
tools/bpf/bpftool/btf.c +10 -0
+               } else {
+                       printf(" size=%u vlen=%u", t->size, vlen);
+               }
+               for (i = 0; i < vlen; i++, v++) {
+                       const char *name = btf_str(btf, v->name_off);
+                       __u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32;
+
+                       if (json_output) {
+                               jsonw_start_object(w);
+                               jsonw_string_field(w, "name", name);

forgot emitting kflag itself (both in bpftool and in selftests), let's add that for both enum and enum64?

Quoted 15 lines
tools/bpf/bpftool/btf.c +12 -0
+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", val);
+                               else
+                                       jsonw_uint_field(w, "val", val);
+                               jsonw_end_object(w);
+                       } else {
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%lldLL", name,
+                                              (unsigned long long)val);
+                               else
+                                       printf("\n\t'%s' val=%lluULL", name,
+                                              (unsigned long long)val);
                        }
                }
                if (json_output)

[...]

#28 lore raw

Re: [PATCH bpf-next v2 11/18] bpftool: Add btf enum64 support

Quoted 87 lines · Andrii Nakryiko
Quoted 86 lines · Yonghong Song

Add BTF_KIND_ENUM64 support.
For example, the following enum is defined in uapi bpf.h.

   $ cat core.c
   enum A {
         BPF_F_INDEX_MASK                = 0xffffffffULL,
         BPF_F_CURRENT_CPU               = BPF_F_INDEX_MASK,
         BPF_F_CTXLEN_MASK               = (0xfffffULL << 32),
   } g;
Compiled with
   clang -target bpf -O2 -g -c core.c
Using bpftool to dump types and generate format C file:
   $ bpftool btf dump file core.o
   ...
   [1] ENUM64 'A' size=8 vlen=3
         'BPF_F_INDEX_MASK' val=4294967295ULL
         'BPF_F_CURRENT_CPU' val=4294967295ULL
         'BPF_F_CTXLEN_MASK' val=4503595332403200ULL
   $ bpftool btf dump file core.o format c
   ...
   enum A {
         BPF_F_INDEX_MASK = 4294967295ULL,
         BPF_F_CURRENT_CPU = 4294967295ULL,
         BPF_F_CTXLEN_MASK = 4503595332403200ULL,
   };
   ...

The 64bit value is represented properly in BTF and C dump.

---
  tools/bpf/bpftool/btf.c        | 49 ++++++++++++++++++++++++++++++++--
  tools/bpf/bpftool/btf_dumper.c | 29 ++++++++++++++++++++
  tools/bpf/bpftool/gen.c        |  1 +
  3 files changed, 77 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +30 -2
index a2c665beda87..9e5db870fe53 100644
@@ -40,6 +40,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
         [BTF_KIND_FLOAT]        = "FLOAT",
         [BTF_KIND_DECL_TAG]     = "DECL_TAG",
         [BTF_KIND_TYPE_TAG]     = "TYPE_TAG",
+       [BTF_KIND_ENUM64]       = "ENUM64",
  };
 
  struct btf_attach_point {
@@ -228,10 +229,54 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
                         if (json_output) {
                                 jsonw_start_object(w);
                                 jsonw_string_field(w, "name", name);
-                               jsonw_uint_field(w, "val", v->val);
+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", v->val);
+                               else
+                                       jsonw_uint_field(w, "val", v->val);
                                 jsonw_end_object(w);
                         } else {
-                               printf("\n\t'%s' val=%u", name, v->val);
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%d", name, v->val);
+                               else
+                                       printf("\n\t'%s' val=%u", name, v->val);
+                       }
+               }
+               if (json_output)
+                       jsonw_end_array(w);
+               break;
+       }
+       case BTF_KIND_ENUM64: {
+               const struct btf_enum64 *v = btf_enum64(t);
+               __u16 vlen = btf_vlen(t);
+               int i;
+
+               if (json_output) {
+                       jsonw_uint_field(w, "size", t->size);
+                       jsonw_uint_field(w, "vlen", vlen);
+                       jsonw_name(w, "values");
+                       jsonw_start_array(w);
+               } else {
+                       printf(" size=%u vlen=%u", t->size, vlen);
+               }
+               for (i = 0; i < vlen; i++, v++) {
+                       const char *name = btf_str(btf, v->name_off);
Quoted 8 lines

tools/bpf/bpftool/btf.c static int dump_btf_type(const struct btf *btf, __u32 id, · line 267

Quoted 5 lines
tools/bpf/bpftool/btf.c +5 -0
+                       __u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32;
+
+                       if (json_output) {
+                               jsonw_start_object(w);
+                               jsonw_string_field(w, "name", name);

forgot emitting kflag itself (both in bpftool and in selftests), let's add that for both enum and enum64?

will do. I will also check other places whether I missed or not.

Quoted 18 lines
Quoted 16 lines
tools/bpf/bpftool/btf.c +12 -0
+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", val);
+                               else
+                                       jsonw_uint_field(w, "val", val);
+                               jsonw_end_object(w);
+                       } else {
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%lldLL", name,
+                                              (unsigned long long)val);
+                               else
+                                       printf("\n\t'%s' val=%lluULL", name,
+                                              (unsigned long long)val);
                         }
                 }
                 if (json_output)

[...]

#29 lore raw

PATCH v2 12/18 selftests/bpf: Fix selftests failure

In reply to Yonghong Song

The kflag is supported now for BTF_KIND_ENUM.
So remove the test which tests verifier failure
due to existence of kflag.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 20 --------------------
 1 file changed, 20 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +0 -20
index ba5bde53d418..8e068e06b3e8 100644
@@ -2896,26 +2896,6 @@ static struct btf_raw_test raw_tests[] = {
 	.err_str = "Invalid btf_info kind_flag",
 },
 
-{
-	.descr = "invalid enum kind_flag",
-	.raw_types = {
-		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
-		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 1, 1), 4),	/* [2] */
-		BTF_ENUM_ENC(NAME_TBD, 0),
-		BTF_END_RAW,
-	},
-	BTF_STR_SEC("\0A"),
-	.map_type = BPF_MAP_TYPE_ARRAY,
-	.map_name = "enum_type_check_btf",
-	.key_size = sizeof(int),
-	.value_size = sizeof(int),
-	.key_type_id = 1,
-	.value_type_id = 1,
-	.max_entries = 4,
-	.btf_load_err = true,
-	.err_str = "Invalid btf_info kind_flag",
-},
-
 {
 	.descr = "valid fwd kind_flag",
 	.raw_types = {
Signature · 1 line
2.30.2
#31 lore raw

PATCH v2 13/18 selftests/bpf: Test new enum kflag and enum64 API functions

In reply to Yonghong Song

Add tests to use the new enum kflag and enum64 API functions in selftest btf_write.

---
 tools/testing/selftests/bpf/btf_helpers.c     |  21 +++-
 .../selftests/bpf/prog_tests/btf_write.c      | 114 +++++++++++++-----
 2 files changed, 105 insertions(+), 30 deletions(-)
tools/testing/selftests/bpf/btf_helpers.c +19 -2
index b5941d514e17..335ddd485f26 100644
@@ -26,11 +26,12 @@ static const char * const btf_kind_str_mapping[] = {
 	[BTF_KIND_FLOAT]	= "FLOAT",
 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
 	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
+	[BTF_KIND_ENUM64]	= "ENUM64",
 };
 
 static const char *btf_kind_str(__u16 kind)
 {
-	if (kind > BTF_KIND_TYPE_TAG)
+	if (kind > BTF_KIND_ENUM64)
 		return "UNKNOWN";
 	return btf_kind_str_mapping[kind];
 }
@@ -139,14 +140,30 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
 	}
 	case BTF_KIND_ENUM: {
 		const struct btf_enum *v = btf_enum(t);
+		const char *fmt_str;
 
+		fmt_str = btf_kflag(t) ? "\n\t'%s' val=%d" : "\n\t'%s' val=%u";
 		fprintf(out, " size=%u vlen=%u", t->size, vlen);
 		for (i = 0; i < vlen; i++, v++) {
-			fprintf(out, "\n\t'%s' val=%u",
+			fprintf(out, fmt_str,
 				btf_str(btf, v->name_off), v->val);
 		}
 		break;
 	}
+	case BTF_KIND_ENUM64: {
+		const struct btf_enum64 *v = btf_enum64(t);
+		const char *fmt_str;
+
+		fmt_str = btf_kflag(t) ? "\n\t'%s' val=%lld" : "\n\t'%s' val=%llu";
+
+		fprintf(out, " size=%u vlen=%u", t->size, vlen);
+		for (i = 0; i < vlen; i++, v++) {
+			fprintf(out, fmt_str,
+				btf_str(btf, v->name_off),
+				((__u64)v->val_hi32 << 32) | v->val_lo32);
+		}
+		break;
+	}
 	case BTF_KIND_FWD:
 		fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");
 		break;
tools/testing/selftests/bpf/prog_tests/btf_write.c +86 -28

148 lines not on this page · raw message

Signature · 1 line
2.30.2
#32 lore raw

Re: [PATCH bpf-next v2 13/18] selftests/bpf: Test new enum kflag and enum64 API functions

Quoted 9 lines · Yonghong Song

Add tests to use the new enum kflag and enum64 API functions in selftest btf_write.

---
 tools/testing/selftests/bpf/btf_helpers.c     |  21 +++-
 .../selftests/bpf/prog_tests/btf_write.c      | 114 +++++++++++++-----
 2 files changed, 105 insertions(+), 30 deletions(-)

[...]

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +13 -0
@@ -307,6 +308,48 @@ static void gen_btf(struct btf *btf)
        ASSERT_EQ(t->type, 1, "tag_type");
        ASSERT_STREQ(btf_type_raw_dump(btf, 20),
                     "[20] TYPE_TAG 'tag1' type_id=1", "raw_dump");
+
+       /* ENUM64 */
+       id = btf__add_enum64(btf, "e1", 8, true);
+       ASSERT_EQ(id, 21, "enum64_id");
+       err = btf__add_enum64_value(btf, "v1", -1);
+       ASSERT_OK(err, "v1_res");
+       err = btf__add_enum64_value(btf, "v2", 0x123456789); /* 4886718345 */
+       ASSERT_OK(err, "v2_res");
+       t = btf__type_by_id(btf, 21);
+       ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum64_name");
+       ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM64, "enum64_kind");
+       ASSERT_EQ(btf_vlen(t), 2, "enum64_vlen");
+       ASSERT_EQ(t->size, 8, "enum64_sz");

tools/testing/selftests/bpf/prog_tests/btf_write.c static void gen_btf(struct btf *btf) · line 333

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +10 -0
+       v64 = btf_enum64(t) + 0;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v1", "v1_name");
+       ASSERT_EQ(v64->val_hi32, 0xffffffff, "v1_val");
+       ASSERT_EQ(v64->val_lo32, 0xffffffff, "v1_val");
+       v64 = btf_enum64(t) + 1;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v2", "v2_name");
+       ASSERT_EQ(v64->val_hi32, 0x1, "v2_val");
+       ASSERT_EQ(v64->val_lo32, 0x23456789, "v2_val");
+       ASSERT_STREQ(btf_type_raw_dump(btf, 21),
+                    "[21] ENUM64 'e1' size=8 vlen=2\n"

we should emit and validate kflag for enum/enum64. Or maybe "encoding=SIGNED|UNSIGNED" to match INT's output, not sure which one is best, but we probably want to make sure that kflag is reflected in bpftool and selftests output, right?

Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +19 -0
+                    "\t'v1' val=-1\n"
+                    "\t'v2' val=4886718345", "raw_dump");
+
+       id = btf__add_enum64(btf, "e1", 8, false);
+       ASSERT_EQ(id, 22, "enum64_id");
+       err = btf__add_enum64_value(btf, "v1", 0xffffffffFFFFFFFF); /* 18446744073709551615 */
+       ASSERT_OK(err, "v1_res");
+       t = btf__type_by_id(btf, 22);
+       ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum64_name");
+       ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM64, "enum64_kind");
+       ASSERT_EQ(btf_vlen(t), 1, "enum64_vlen");
+       ASSERT_EQ(t->size, 8, "enum64_sz");
+       v64 = btf_enum64(t) + 0;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v1", "v1_name");
+       ASSERT_EQ(v64->val_hi32, 0xffffffff, "v1_val");
+       ASSERT_EQ(v64->val_lo32, 0xffffffff, "v1_val");
+       ASSERT_STREQ(btf_type_raw_dump(btf, 22),
+                    "[22] ENUM64 'e1' size=8 vlen=1\n"
+                    "\t'v1' val=18446744073709551615", "raw_dump");
 }
 
 static void test_btf_add()

[...]

#33 lore raw

Re: [PATCH bpf-next v2 13/18] selftests/bpf: Test new enum kflag and enum64 API functions

Quoted 37 lines · Andrii Nakryiko
Quoted 9 lines · Yonghong Song

Add tests to use the new enum kflag and enum64 API functions in selftest btf_write.

---
  tools/testing/selftests/bpf/btf_helpers.c     |  21 +++-
  .../selftests/bpf/prog_tests/btf_write.c      | 114 +++++++++++++-----
  2 files changed, 105 insertions(+), 30 deletions(-)

[...]

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +20 -0
@@ -307,6 +308,48 @@ static void gen_btf(struct btf *btf)
         ASSERT_EQ(t->type, 1, "tag_type");
         ASSERT_STREQ(btf_type_raw_dump(btf, 20),
                      "[20] TYPE_TAG 'tag1' type_id=1", "raw_dump");
+
+       /* ENUM64 */
+       id = btf__add_enum64(btf, "e1", 8, true);
+       ASSERT_EQ(id, 21, "enum64_id");
+       err = btf__add_enum64_value(btf, "v1", -1);
+       ASSERT_OK(err, "v1_res");
+       err = btf__add_enum64_value(btf, "v2", 0x123456789); /* 4886718345 */
+       ASSERT_OK(err, "v2_res");
+       t = btf__type_by_id(btf, 21);
+       ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum64_name");
+       ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM64, "enum64_kind");
+       ASSERT_EQ(btf_vlen(t), 2, "enum64_vlen");
+       ASSERT_EQ(t->size, 8, "enum64_sz");
+       v64 = btf_enum64(t) + 0;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v1", "v1_name");
+       ASSERT_EQ(v64->val_hi32, 0xffffffff, "v1_val");
+       ASSERT_EQ(v64->val_lo32, 0xffffffff, "v1_val");
+       v64 = btf_enum64(t) + 1;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v2", "v2_name");
+       ASSERT_EQ(v64->val_hi32, 0x1, "v2_val");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/btf_write.c static void gen_btf(struct btf *btf) · line 333

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +3 -0
+       ASSERT_EQ(v64->val_lo32, 0x23456789, "v2_val");
+       ASSERT_STREQ(btf_type_raw_dump(btf, 21),
+                    "[21] ENUM64 'e1' size=8 vlen=2\n"

we should emit and validate kflag for enum/enum64. Or maybe "encoding=SIGNED|UNSIGNED" to match INT's output, not sure which one is best, but we probably want to make sure that kflag is reflected in bpftool and selftests output, right?

My intention is the value print out itself will show it is signed or unsigned as below. But it doesn't hurt to explicit print out kflag as well. Will do.

Quoted 25 lines
Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/btf_write.c +19 -0
+                    "\t'v1' val=-1\n"
+                    "\t'v2' val=4886718345", "raw_dump");
+
+       id = btf__add_enum64(btf, "e1", 8, false);
+       ASSERT_EQ(id, 22, "enum64_id");
+       err = btf__add_enum64_value(btf, "v1", 0xffffffffFFFFFFFF); /* 18446744073709551615 */
+       ASSERT_OK(err, "v1_res");
+       t = btf__type_by_id(btf, 22);
+       ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum64_name");
+       ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM64, "enum64_kind");
+       ASSERT_EQ(btf_vlen(t), 1, "enum64_vlen");
+       ASSERT_EQ(t->size, 8, "enum64_sz");
+       v64 = btf_enum64(t) + 0;
+       ASSERT_STREQ(btf__str_by_offset(btf, v64->name_off), "v1", "v1_name");
+       ASSERT_EQ(v64->val_hi32, 0xffffffff, "v1_val");
+       ASSERT_EQ(v64->val_lo32, 0xffffffff, "v1_val");
+       ASSERT_STREQ(btf_type_raw_dump(btf, 22),
+                    "[22] ENUM64 'e1' size=8 vlen=1\n"
+                    "\t'v1' val=18446744073709551615", "raw_dump");
  }
 
  static void test_btf_add()

[...]

#34 lore raw

PATCH v2 14/18 selftests/bpf: Add BTF_KIND_ENUM64 unit tests

In reply to Yonghong Song

Add unit tests for basic BTF_KIND_ENUM64 encoding.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 36 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |  1 +
 2 files changed, 37 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +36 -0
index 8e068e06b3e8..a986ee56c5f7 100644
@@ -4052,6 +4052,42 @@ static struct btf_raw_test raw_tests[] = {
 	.btf_load_err = true,
 	.err_str = "Type tags don't precede modifiers",
 },
+{
+	.descr = "enum64 test #1, unsigned, size 8",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
+		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [2] */
+		BTF_ENUM64_ENC(NAME_TBD, 0, 0),
+		BTF_ENUM64_ENC(NAME_TBD, 1, 1),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0a\0b\0c"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "tag_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 8,
+	.key_type_id = 1,
+	.value_type_id = 2,
+	.max_entries = 1,
+},
+{
+	.descr = "enum64 test #2, signed, size 4",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
+		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 1, 2), 4),	/* [2] */
+		BTF_ENUM64_ENC(NAME_TBD, -1, 0),
+		BTF_ENUM64_ENC(NAME_TBD, 1, 0),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0a\0b\0c"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "tag_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 2,
+	.max_entries = 1,
+},
 
 }; /* struct btf_raw_test raw_tests[] */
 
tools/testing/selftests/bpf/test_btf.h +1 -0
index 128989bed8b7..38782bd47fdc 100644
@@ -39,6 +39,7 @@
 #define BTF_MEMBER_ENC(name, type, bits_offset)	\
 	(name), (type), (bits_offset)
 #define BTF_ENUM_ENC(name, val) (name), (val)
+#define BTF_ENUM64_ENC(name, val_lo32, val_hi32) (name), (val_lo32), (val_hi32)
 #define BTF_MEMBER_OFFSET(bitfield_size, bits_offset) \
 	((bitfield_size) << 24 | (bits_offset))
 
Signature · 1 line
2.30.2
Acked-by on v2 14/18
#35 lore raw

Re: [PATCH bpf-next v2 14/18] selftests/bpf: Add BTF_KIND_ENUM64 unit tests

Quoted 5 lines · Yonghong Song

Add unit tests for basic BTF_KIND_ENUM64 encoding.

---

Quoted 4 lines
 tools/testing/selftests/bpf/prog_tests/btf.c | 36 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |  1 +
 2 files changed, 37 insertions(+)

[...]

#36 lore raw

PATCH v2 15/18 selftests/bpf: Test BTF_KIND_ENUM64 for deduplication

In reply to Yonghong Song

Add a few unit tests for BTF_KIND_ENUM64 deduplication.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 97 +++++++++++++++++++-
 1 file changed, 95 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +95 -2
index a986ee56c5f7..edb387163baa 100644
@@ -7016,9 +7016,12 @@ static struct btf_dedup_test dedup_tests[] = {
 			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
 			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
 			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
+			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
+				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
+				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
 	},
 	.expect = {
 		.raw_types = {
@@ -7046,9 +7049,12 @@ static struct btf_dedup_test dedup_tests[] = {
 			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
 			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
 			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
+			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
+				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
+				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
 	},
 },
 {
@@ -7509,6 +7515,91 @@ static struct btf_dedup_test dedup_tests[] = {
 		BTF_STR_SEC("\0tag1\0t\0m"),
 	},
 },
+{
+	.descr = "dedup: enum64, standalone",
+	.input = {
+		.raw_types = {
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0e1\0e1_val"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0e1\0e1_val"),
+	},
+},
+{
+	.descr = "dedup: enum64, fwd resolution",
+	.input = {
+		.raw_types = {
+			/* [1] fwd enum64 'e1' before full enum */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
+			/* [2] full enum64 'e1' after fwd */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
+			/* [3] full enum64 'e2' before fwd */
+			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
+			/* [4] fwd enum64 'e2' after full enum */
+			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
+			/* [5] incompatible full enum64 with different value */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
+	},
+	.expect = {
+		.raw_types = {
+			/* [1] full enum64 'e1' */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
+			/* [2] full enum64 'e2' */
+			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
+			/* [3] incompatible full enum64 with different value */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
+				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
+	},
+},
+{
+	.descr = "dedup: enum and enum64, 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] enum64 'e1' */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
+			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] enum64 'e1' */
+			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
+				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0e1\0e1_val"),
+	},
+},
 
 };
 
@@ -7533,6 +7624,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:
Signature · 1 line
2.30.2
Acked-by on v2 15/18
#37 lore raw

Re: [PATCH bpf-next v2 15/18] selftests/bpf: Test BTF_KIND_ENUM64 for deduplication

Quoted 5 lines · Yonghong Song

Add a few unit tests for BTF_KIND_ENUM64 deduplication.

---

Great test cases, thanks!

Quoted 3 lines
 tools/testing/selftests/bpf/prog_tests/btf.c | 97 +++++++++++++++++++-
 1 file changed, 95 insertions(+), 2 deletions(-)

[...]

#38 lore raw

PATCH v2 16/18 selftests/bpf: Add a test for enum64 value relocations

In reply to Yonghong Song

Add a test for enum64 value relocations.

---
 .../selftests/bpf/prog_tests/core_reloc.c     | 58 ++++++++++++++
 .../bpf/progs/btf__core_reloc_enum64val.c     |  3 +
 .../progs/btf__core_reloc_enum64val___diff.c  |  3 +
 .../btf__core_reloc_enum64val___err_missing.c |  3 +
 ...btf__core_reloc_enum64val___val3_missing.c |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 78 +++++++++++++++++++
 .../bpf/progs/test_core_reloc_enum64val.c     | 70 +++++++++++++++++
 7 files changed, 218 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c
tools/testing/selftests/bpf/prog_tests/core_reloc.c +58 -0
index 3712dfe1be59..47c1ef117275 100644
@@ -363,6 +363,25 @@ static int duration = 0;
 	.fails = true,							\
 }
 
+#define ENUM64VAL_CASE_COMMON(name)					\
+	.case_name = #name,						\
+	.bpf_obj_file = "test_core_reloc_enum64val.o",			\
+	.btf_src_file = "btf__core_reloc_" #name ".o",			\
+	.raw_tp_name = "sys_enter",					\
+	.prog_name = "test_core_enum64val"
+
+#define ENUM64VAL_CASE(name, ...) {					\
+	ENUM64VAL_CASE_COMMON(name),					\
+	.output = STRUCT_TO_CHAR_PTR(core_reloc_enum64val_output)	\
+			__VA_ARGS__,					\
+	.output_len = sizeof(struct core_reloc_enum64val_output),	\
+}
+
+#define ENUM64VAL_ERR_CASE(name) {					\
+	ENUM64VAL_CASE_COMMON(name),					\
+	.fails = true,							\
+}
+
 struct core_reloc_test_case;
 
 typedef int (*setup_test_fn)(struct core_reloc_test_case *test);
@@ -831,6 +850,45 @@ static const struct core_reloc_test_case test_cases[] = {
 		.anon_val2 = 0x222,
 	}),
 	ENUMVAL_ERR_CASE(enumval___err_missing),
+
+	/* 64bit enumerator value existence and value relocations */
+	ENUM64VAL_CASE(enum64val, {
+		.unsigned_val1_exists = true,
+		.unsigned_val2_exists = true,
+		.unsigned_val3_exists = true,
+		.signed_val1_exists = true,
+		.signed_val2_exists = true,
+		.signed_val3_exists = true,
+		.unsigned_val1 = 0x1ffffffffULL,
+		.unsigned_val2 = 0x2,
+		.signed_val1 = 0x1ffffffffLL,
+		.signed_val2 = -2,
+	}),
+	ENUM64VAL_CASE(enum64val___diff, {
+		.unsigned_val1_exists = true,
+		.unsigned_val2_exists = true,
+		.unsigned_val3_exists = true,
+		.signed_val1_exists = true,
+		.signed_val2_exists = true,
+		.signed_val3_exists = true,
+		.unsigned_val1 = 0x101ffffffffULL,
+		.unsigned_val2 = 0x202ffffffffULL,
+		.signed_val1 = -101,
+		.signed_val2 = -202,
+	}),
+	ENUM64VAL_CASE(enum64val___val3_missing, {
+		.unsigned_val1_exists = true,
+		.unsigned_val2_exists = true,
+		.unsigned_val3_exists = false,
+		.signed_val1_exists = true,
+		.signed_val2_exists = true,
+		.signed_val3_exists = false,
+		.unsigned_val1 = 0x111ffffffffULL,
+		.unsigned_val2 = 0x222,
+		.signed_val1 = 0x111ffffffffLL,
+		.signed_val2 = -222,
+	}),
+	ENUM64VAL_ERR_CASE(enum64val___err_missing),
 };
 
 struct data {
tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c new file +3 -0
new file mode 100644
index 000000000000..888e79db6a77
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enum64val x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c new file +3 -0
new file mode 100644
index 000000000000..194749130d87
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enum64val___diff x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c new file +3 -0
new file mode 100644
index 000000000000..3d732d4193e4
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enum64val___err_missing x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c new file +3 -0
new file mode 100644
index 000000000000..17cf5d6a848d
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enum64val___val3_missing x) {}
tools/testing/selftests/bpf/progs/core_reloc_types.h +78 -0

105 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.30.2
Acked-by on v2 16/18
#39 lore raw

Re: [PATCH bpf-next v2 16/18] selftests/bpf: Add a test for enum64 value relocations

Quoted 5 lines · Yonghong Song

Add a test for enum64 value relocations.

---

LGTM

Quoted 14 lines
 .../selftests/bpf/prog_tests/core_reloc.c     | 58 ++++++++++++++
 .../bpf/progs/btf__core_reloc_enum64val.c     |  3 +
 .../progs/btf__core_reloc_enum64val___diff.c  |  3 +
 .../btf__core_reloc_enum64val___err_missing.c |  3 +
 ...btf__core_reloc_enum64val___val3_missing.c |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 78 +++++++++++++++++++
 .../bpf/progs/test_core_reloc_enum64val.c     | 70 +++++++++++++++++
 7 files changed, 218 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___diff.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___val3_missing.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_enum64val.c

[...]

#40 lore raw

PATCH v2 17/18 selftests/bpf: Clarify llvm dependency with possible selftest failures

In reply to Yonghong Song

Certain subtests in selftests core_reloc and core_reloc_btfgen requires llvm ENUM64 support in llvm15. If an older compiler is used, these subtests will fail. Make this requirement clear in selftests README.rst file.

---
 tools/testing/selftests/bpf/README.rst | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/README.rst +18 -0
index eb1b7541f39d..a83d78a58014 100644
@@ -266,3 +266,21 @@ from running test_progs will look like:
   test_xdpwall:FAIL:Does LLVM have https://reviews.llvm.org/D109073? unexpected error: -4007
 
 __ https://reviews.llvm.org/D109073
+
+ENUM64 support and Clang version
+================================
+
+There are a few selftests requiring LLVM ENUM64 support. The LLVM ENUM64 is
+introduced in `Clang 15` [0_]. Without proper compiler support, the following selftests
+will fail:
+
+.. code-block:: console
+
+  #45 /73    core_reloc/enum64val:FAIL
+  #45 /74    core_reloc/enum64val___diff:FAIL
+  #45 /75    core_reloc/enum64val___val3_missing:FAIL
+  #46 /73    core_reloc_btfgen/enum64val:FAIL
+  #46 /74    core_reloc_btfgen/enum64val___diff:FAIL
+  #46 /75    core_reloc_btfgen/enum64val___val3_missing:FAIL
+
+.. _0: https://reviews.llvm.org/D124641
Signature · 1 line
2.30.2
Acked-by on v2 17/18
#41 lore raw

Re: [PATCH bpf-next v2 17/18] selftests/bpf: Clarify llvm dependency with possible selftest failures

Quoted 8 lines · Yonghong Song

Certain subtests in selftests core_reloc and core_reloc_btfgen requires llvm ENUM64 support in llvm15. If an older compiler is used, these subtests will fail. Make this requirement clear in selftests README.rst file.

---

Quoted 32 lines
 tools/testing/selftests/bpf/README.rst | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/README.rst +18 -1
index eb1b7541f39d..a83d78a58014 100644
@@ -266,3 +266,21 @@ from running test_progs will look like:
   test_xdpwall:FAIL:Does LLVM have https://reviews.llvm.org/D109073? unexpected error: -4007
 
 __ https://reviews.llvm.org/D109073
+
+ENUM64 support and Clang version
+================================
+
+There are a few selftests requiring LLVM ENUM64 support. The LLVM ENUM64 is
+introduced in `Clang 15` [0_]. Without proper compiler support, the following selftests
+will fail:
+
+.. code-block:: console
+
+  #45 /73    core_reloc/enum64val:FAIL
+  #45 /74    core_reloc/enum64val___diff:FAIL
+  #45 /75    core_reloc/enum64val___val3_missing:FAIL
+  #46 /73    core_reloc_btfgen/enum64val:FAIL
+  #46 /74    core_reloc_btfgen/enum64val___diff:FAIL
+  #46 /75    core_reloc_btfgen/enum64val___val3_missing:FAIL
+
+.. _0: https://reviews.llvm.org/D124641
--

2.30.2

#42 lore raw

PATCH v2 18/18 docs/bpf: Update documentation for BTF_KIND_ENUM64 support

In reply to Yonghong Song

Add BTF_KIND_ENUM64 documentation in btf.rst. Also fixed a typo for section number for BTF_KIND_TYPE_TAG from 2.2.17 to 2.2.18.

---
 Documentation/bpf/btf.rst | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)
Documentation/bpf/btf.rst +29 -5
index 7940da9bc6c1..e2ddfb2b084d 100644
@@ -74,7 +74,7 @@ sequentially and type id is assigned to each recognized type starting from id
     #define BTF_KIND_ARRAY          3       /* Array        */
     #define BTF_KIND_STRUCT         4       /* Struct       */
     #define BTF_KIND_UNION          5       /* Union        */
-    #define BTF_KIND_ENUM           6       /* Enumeration  */
+    #define BTF_KIND_ENUM           6       /* Enumeration up to 32-bit values */
     #define BTF_KIND_FWD            7       /* Forward      */
     #define BTF_KIND_TYPEDEF        8       /* Typedef      */
     #define BTF_KIND_VOLATILE       9       /* Volatile     */
@@ -87,6 +87,7 @@ sequentially and type id is assigned to each recognized type starting from id
     #define BTF_KIND_FLOAT          16      /* Floating point       */
     #define BTF_KIND_DECL_TAG       17      /* Decl Tag     */
     #define BTF_KIND_TYPE_TAG       18      /* Type Tag     */
+    #define BTF_KIND_ENUM64         19      /* Enumeration up to 64-bit values */
 
 Note that the type section encodes debug info, not just pure types.
 ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -101,10 +102,10 @@ Each type contains the following common data::
          * 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, fwd, enum and enum64.
          */
         __u32 info;
-        /* "size" is used by INT, ENUM, STRUCT and UNION.
+        /* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
          * "size" tells the size of the type it is describing.
          *
          * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
@@ -281,7 +282,7 @@ modes exist:
 
 ``struct btf_type`` encoding requirement:
   * ``name_off``: 0 or offset to a valid C identifier
-  * ``info.kind_flag``: 0
+  * ``info.kind_flag``: 0 for unsigned, 1 for signed
   * ``info.kind``: BTF_KIND_ENUM
   * ``info.vlen``: number of enum values
   * ``size``: 4
@@ -493,7 +494,7 @@ the attribute is applied to a ``struct``/``union`` member or
 a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
 valid index (starting from 0) pointing to a member or an argument.
 
-2.2.17 BTF_KIND_TYPE_TAG
+2.2.18 BTF_KIND_TYPE_TAG
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
 ``struct btf_type`` encoding requirement:
@@ -516,6 +517,29 @@ type_tag, then zero or more const/volatile/restrict/typedef
 and finally the base type. The base type is one of
 int, ptr, array, struct, union, enum, func_proto and float types.
 
+2.2.19 BTF_KIND_ENUM64
+~~~~~~~~~~~~~~~~~~~~~~
+
+``struct btf_type`` encoding requirement:
+  * ``name_off``: 0 or offset to a valid C identifier
+  * ``info.kind_flag``: 0 for unsigned, 1 for signed
+  * ``info.kind``: BTF_KIND_ENUM64
+  * ``info.vlen``: number of enum values
+  * ``size``: 1/2/4/8
+
+``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
+
+    struct btf_enum64 {
+        __u32   name_off;
+        __u32   val_lo32;
+        __u32   val_hi32;
+    };
+
+The ``btf_enum64`` encoding:
+  * ``name_off``: offset to a valid C identifier
+  * ``val_lo32``: lower 32-bit value for a 64-bit value
+  * ``val_hi32``: high 32-bit value for a 64-bit value
+
 3. BTF Kernel API
 =================
 
Signature · 1 line
2.30.2
Acked-by on v2 18/18
#43 lore raw

Re: [PATCH bpf-next v2 18/18] docs/bpf: Update documentation for BTF_KIND_ENUM64 support

Quoted 7 lines · Yonghong Song

Add BTF_KIND_ENUM64 documentation in btf.rst. Also fixed a typo for section number for BTF_KIND_TYPE_TAG from 2.2.17 to 2.2.18.

---

LGTM, but see pedantic note below

Quoted 3 lines
 Documentation/bpf/btf.rst | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

[...]

Quoted 13 lines

+2.2.19 BTF_KIND_ENUM64
+~~~~~~~~~~~~~~~~~~~~~~

Documentation/bpf/btf.rst +11 -0
+
+``struct btf_type`` encoding requirement:
+  * ``name_off``: 0 or offset to a valid C identifier
+  * ``info.kind_flag``: 0 for unsigned, 1 for signed
+  * ``info.kind``: BTF_KIND_ENUM64
+  * ``info.vlen``: number of enum values
+  * ``size``: 1/2/4/8
+
+``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
+
+    struct btf_enum64 {

Documentation/bpf/btf.rst type_tag, then zero or more const/volatile/restrict/typedef · line 542

Quoted 10 lines
Documentation/bpf/btf.rst +10 -0
+        __u32   name_off;
+        __u32   val_lo32;
+        __u32   val_hi32;
+    };
+
+The ``btf_enum64`` encoding:
+  * ``name_off``: offset to a valid C identifier
+  * ``val_lo32``: lower 32-bit value for a 64-bit value
+  * ``val_hi32``: high 32-bit value for a 64-bit value
+

I presume if size is < 8 then val_hi32 will be sign-extended (i.e., 0xffffffff for signed enum and negative enumerator values, 0 otherwise), right? Should it be specified here?

Quoted 6 lines

3. BTF Kernel API
=================

--
2.30.2

#44 lore raw

Re: [PATCH bpf-next v2 18/18] docs/bpf: Update documentation for BTF_KIND_ENUM64 support

Quoted 38 lines · Andrii Nakryiko
Quoted 7 lines · Yonghong Song

Add BTF_KIND_ENUM64 documentation in btf.rst. Also fixed a typo for section number for BTF_KIND_TYPE_TAG from 2.2.17 to 2.2.18.

---

LGTM, but see pedantic note below

Quoted 3 lines
  Documentation/bpf/btf.rst | 34 +++++++++++++++++++++++++++++-----
  1 file changed, 29 insertions(+), 5 deletions(-)

[...]

Quoted 19 lines

+2.2.19 BTF_KIND_ENUM64
+~~~~~~~~~~~~~~~~~~~~~~

Documentation/bpf/btf.rst +17 -0
+
+``struct btf_type`` encoding requirement:
+  * ``name_off``: 0 or offset to a valid C identifier
+  * ``info.kind_flag``: 0 for unsigned, 1 for signed
+  * ``info.kind``: BTF_KIND_ENUM64
+  * ``info.vlen``: number of enum values
+  * ``size``: 1/2/4/8
+
+``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
+
+    struct btf_enum64 {
+        __u32   name_off;
+        __u32   val_lo32;
+        __u32   val_hi32;
+    };
+
+The ``btf_enum64`` encoding:
Quoted 8 lines

Documentation/bpf/btf.rst type_tag, then zero or more const/volatile/restrict/typedef · line 542

Quoted 4 lines
Documentation/bpf/btf.rst +4 -0
+  * ``name_off``: offset to a valid C identifier
+  * ``val_lo32``: lower 32-bit value for a 64-bit value
+  * ``val_hi32``: high 32-bit value for a 64-bit value
+

I presume if size is < 8 then val_hi32 will be sign-extended (i.e., 0xffffffff for signed enum and negative enumerator values, 0 otherwise), right? Should it be specified here?

Right. This is similar to btf_enum, e.g., btf_enum can have size 1, e.g.
enum T { A = -1, B, C, D } __attribute__((packed));
But for enumerator A, its value will be 0xffffffff.

Here, val_lo32 and val_hi32 are just low and high 32bits of unsigned long long value of the enumerator. I will add a few sentences to clarify here.

Quoted 7 lines
Quoted 7 lines

3. BTF Kernel API
=================

--
2.30.2