Skip to contentSkip to conversation

libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

PATCH Superseded Yonghong Song 3 participants 11 messages Nov 20 – Nov 23

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

v5 cover libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

The BTF of a kernel before v6.0, or generated by pahole before v1.24, has the kflag 0 on every enum, signed or not, so there the kflag 0 doesn't mean unsigned. If the local enum is signed, patch 1 takes such a target value as signed too, so that, e.g., -1 doesn't become 0xffffffff.

Patch 2 tests the unsigned enum values, with clang and GCC. Patch 3 tests the signed ones against a target BTF without the kflag.

Changelog:
v4 -> v5:
- v4: https://lore.kernel.org/bpf/[email protected]/
- Keep the sign extension in bpf_core_calc_enumval_relo(), and zero-extend the 32-bit values of unsigned enums once in bpf_core_calc_relo(). This drops btf_enum_value() from include/linux/btf.h and the three helpers in relo_core.c. No
change in behavior (patch 1).
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Take a 32-bit target enum value as signed if the target enum has the kflag 0 but the local enum is signed, for the BTF of kernels without enum signedness (patch 1).
- Test it with a target BTF whose enums have the kflag cleared
(patch 3).
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Fold patch 3 into patch 1 and patch 4 into patch 2 to avoid test failures when bisecting.
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Accept the sign-extended enum values emitted by GCC (patch 3), and make the new test work with GCC (patch 4).

Yonghong Song (3):
libbpf: Fix CO-RE relocations of unsigned 32-bit enum values selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values selftests/bpf: Test CO-RE relocations of signed enum values without
kflag

 tools/lib/bpf/relo_core.c                     | 31 +++++++
 .../selftests/bpf/prog_tests/core_reloc.c     | 92 ++++++++++++++++++-
 .../btf__core_reloc_enumval___no_kflag.c      |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 24 +++++
 .../bpf/progs/test_core_reloc_enumval.c       | 35 +++++++
 5 files changed, 183 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
Signature · 1 line
2.53.1-Meta
author reviewer
#1 lore raw

PATCH v5 1/3 libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

For a BPF_CORE_ENUMVAL_VALUE relocation, bpf_core_calc_enumval_relo() reads the value of a 32-bit enumerator from btf_enum->val, which is __s32, so the value is sign-extended to 64 bits even if the enum is unsigned. A value in [0x80000000, 0xffffffff] of an unsigned enum becomes 0xffffffff80000000 or above.

Clang emits such a value zero-extended in the ld_imm64 of the relocation, so the check of the original value fails, and the whole object is refused even if the target value is the same:

  enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };
  out = bpf_core_enum_value(enum dentry_flags, DCACHE_PRIVATE);

libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got

          2147483648, exp 18446744071562067968 -> 18446744071562067968

libbpf: prog 'p': relo #0: failed to patch insn #0: -EINVAL

And a target value in that range is patched sign-extended, which is wrong wherever the program uses it as a 64-bit value. The x86 vmlinux has 29 enums with such values, e.g., dentry_flags, init_ns_ino and e820_type.

Commit 89e7a42e3030 ("libbpf: Normalize enum values according to signedness") fixed this for btf_dump and map fields, but not for CO-RE relocations, whose relo_core.c is also built into the kernel. Keep the sign extension in bpf_core_calc_enumval_relo(), and zero-extend the 32-bit values of unsigned enums, i.e., with the kflag 0, once in bpf_core_calc_relo().

The kflag 0 of the target enum doesn't always mean unsigned, though. Before commit 6089fb325cf7 ("bpf: Add btf enum64 support") in v6.0, the kernel refused an enum with the kflag set, so the BTF of an older kernel has the kflag 0 on every enum, signed or not. pahole doesn't record the signedness before v1.24 either, and the kernel still accepts pahole v1.22. On such a kernel, zero-extending a negative target value would make, e.g., 0xffffffff instead of -1:

long v = bpf_core_enum_value(enum perf_event_state,

                               PERF_EVENT_STATE_OFF);

The local enum has the kflag 1, so the original value matches the ld_imm64, and nothing would warn. So zero-extend the target value only if the local enum has the kflag 0 too. A target value is then zero-extended only if neither enum is signed.

GCC, however, sign-extends every 32-bit enum value, since its __builtin_preserve_enum_value() returns int. For the example above:

clang: r1 = 0x80000000 ll
gcc: lddw %r1,-2147483648

An object built by GCC is accepted today, because the original value is sign-extended too, and the change above alone would refuse it:

libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got

          18446744071562067968, exp 2147483648 -> 2147483648

The two forms differ only for an unsigned value in [0x80000000, 0xffffffff]. For such a value, accept the sign-extended form as well. In that case, also sign-extend the target value, since GCC code uses the ld_imm64 as the int value widened to 64 bits.

An original value below 0x80000000 is the same in both forms, so the instruction doesn't show which compiler emitted it. The target value then gets the extension above, which is the form clang emits, as its builtin returns long. A target value that doesn't fit in 32 bits, e.g., from an enum64 in the target kernel, is patched unchanged.

In summary, a 32-bit target value is extended as follows, where kflag 1 is signed:

  local enum   target enum   clang   GCC
  ----------   -----------   -----   ----------
  kflag 1      kflag 1       sign    sign
  kflag 1      kflag 0       sign    sign
  kflag 0      kflag 1       sign    sign
  kflag 0      kflag 0       zero    zero, or sign (*)

(*) sign if the local value is in [0x80000000, 0xffffffff], since
GCC emits it sign-extended in the ld_imm64

Fixes 776281652ddc ("libbpf: Permit 64bit relocation value")
---
 tools/lib/bpf/relo_core.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/lib/bpf/relo_core.c +31 -0
index 2672623a4198..3f37a0879c40 100644
@@ -887,6 +887,28 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 	return 0;
 }
 
+/*
+ * btf_enum->val is __s32, so both values are sign-extended at this point.
+ * clang zero-extends the value of an unsigned enum.
+ * kflag == 0 is an unsigned enum or BTF without signedness (kernel before
+ * v6.0, pahole before v1.24), so new_val needs kflag == 0 on both sides.
+ */
+static void bpf_core_zext_enumval(const struct bpf_core_spec *local_spec,
+				  const struct bpf_core_spec *targ_spec,
+				  struct bpf_core_relo_res *res)
+{
+	const struct btf_type *local_t, *targ_t;
+
+	local_t = btf_type_by_id(local_spec->btf, local_spec->spec[0].type_id);
+	targ_t = btf_type_by_id(targ_spec->btf, targ_spec->spec[0].type_id);
+	if (BTF_INFO_KFLAG(local_t->info))
+		return;
+	if (btf_is_enum(local_t))
+		res->orig_val = (__u32)res->orig_val;
+	if (btf_is_enum(targ_t) && !BTF_INFO_KFLAG(targ_t->info))
+		res->new_val = (__u32)res->new_val;
+}
+
 /* Calculate original and target relocation values, given local and target
  * specs and relocation kind. These values are calculated for each candidate.
  * If there are multiple candidates, resulting values should all be consistent
@@ -962,6 +984,8 @@ static int bpf_core_calc_relo(const char *prog_name,
 	} else if (core_relo_is_enumval_based(relo->kind)) {
 		err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
 		err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
+		if (!err && relo->kind == BPF_CORE_ENUMVAL_VALUE)
+			bpf_core_zext_enumval(local_spec, targ_spec, res);
 	}
 
 done:
@@ -1144,6 +1168,13 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
 			return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
 
 		imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
+		/* GCC sign-extends the value of an unsigned enum. Keep its form. */
+		if (relo->kind == BPF_CORE_ENUMVAL_VALUE && orig_val <= UINT_MAX &&
+		    imm != orig_val && imm == (__u64)(__s32)orig_val) {
+			orig_val = imm;
+			if (new_val <= UINT_MAX)
+				new_val = (__s32)new_val;
+		}
 		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.53.1-Meta
author reviewer
#2 lore raw

PATCH v5 2/3 selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values

In reply to Yonghong Song

Test the extension of unsigned 32-bit enum values in CO-RE relocations from the previous patch.

test_core_reloc_enumval gets an optional local enum with values out of the range of int, i.e., an unsigned 4-byte enum, whose values clang emits zero-extended in the ld_imm64 of the relocations. The enumval target has the same enum with different values, 0x80000001 and 0xffffffff, which are expected zero-extended in the 64-bit outputs.

GCC's __builtin_preserve_enum_value() returns int, so storing the value of enum named_uint_enum into a long field sign-extends it, and uint_val1 would read back as 0xffffffff80000001. Convert the result to unsigned int for GCC to get the enumerator value. Clang still stores the 64-bit relocated value, so its upper 32 bits keep being checked.

Without the fix, the check of the original value fails, and the whole object is refused:

libbpf: prog 'test_core_enumval': relo #11: unexpected insn #53 (LDIMM64)

          value: got 2147483648, exp 18446744071562067968 -> 18446744071562067969

libbpf: prog 'test_core_enumval': relo #11: failed to patch insn #53: -EINVAL #122/71 core_reloc/enumval:FAIL

---
 .../selftests/bpf/prog_tests/core_reloc.c     |  2 ++
 .../selftests/bpf/progs/core_reloc_types.h    |  8 +++++++
 .../bpf/progs/test_core_reloc_enumval.c       | 21 +++++++++++++++++++
 3 files changed, 31 insertions(+)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -0
index 80432c4656b5..98b3c5fb1028 100644
@@ -904,6 +904,8 @@ static const struct core_reloc_test_case test_cases[] = {
 		.named_val2 = 2,
 		.anon_val1 = 0x10,
 		.anon_val2 = 0x20,
+		.uint_val1 = 0x80000001,
+		.uint_val2 = 0xffffffff,
 	}),
 	ENUMVAL_CASE(enumval___diff, {
 		.named_val1_exists = true,
tools/testing/selftests/bpf/progs/core_reloc_types.h +8 -0
index 5760ae015e09..6d758e74bd58 100644
@@ -1215,6 +1215,8 @@ struct core_reloc_enumval_output {
 	int named_val2;
 	int anon_val1;
 	int anon_val2;
+	long uint_val1;
+	long uint_val2;
 };
 
 struct core_reloc_enum64val_output {
@@ -1243,9 +1245,15 @@ typedef enum {
 	ANON_ENUM_VAL3 = 0x30,
 } anon_enum;
 
+enum named_uint_enum {
+	UINT_ENUM_VAL1 = 0x80000001,
+	UINT_ENUM_VAL2 = 0xffffffff,
+};
+
 struct core_reloc_enumval {
 	enum named_enum f1;
 	anon_enum f2;
+	enum named_uint_enum f3;
 };
 
 enum named_unsigned_enum64 {
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +21 -0
index e7ef3dada2bf..8b8abbaaf533 100644
@@ -27,6 +27,12 @@ typedef enum {
 	ANON_ENUM_VAL3 = 0x30,
 } anon_enum;
 
+/* 4-byte enum with values out of the range of int, so it is unsigned */
+enum named_uint_enum {
+	UINT_ENUM_VAL1 = 0x80000000,
+	UINT_ENUM_VAL2 = 0xfffffff0,
+};
+
 struct core_reloc_enumval_output {
 	bool named_val1_exists;
 	bool named_val2_exists;
@@ -39,8 +45,17 @@ struct core_reloc_enumval_output {
 	int named_val2;
 	int anon_val1;
 	int anon_val2;
+	long uint_val1;
+	long uint_val2;
 };
 
+#ifdef __clang__
+#define UINT_ENUM_VALUE(v) bpf_core_enum_value(enum named_uint_enum, v)
+#else
+/* GCC's __builtin_preserve_enum_value() returns int */
+#define UINT_ENUM_VALUE(v) ((unsigned int)bpf_core_enum_value(enum named_uint_enum, v))
+#endif
+
 SEC("raw_tracepoint/sys_enter")
 int test_core_enumval(void *ctx)
 {
@@ -64,6 +79,12 @@ int test_core_enumval(void *ctx)
 	out->anon_val1 = bpf_core_enum_value(anon, ANON_ENUM_VAL1);
 	out->anon_val2 = bpf_core_enum_value(anon, ANON_ENUM_VAL2);
 	/* ANON_ENUM_VAL3 value is optional */
+
+	/* enum named_uint_enum is optional */
+	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1))
+		out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1);
+	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2))
+		out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
 #else
 	data.skip = true;
 #endif
Signature · 1 line
2.53.1-Meta
author reviewer
#3 lore raw

PATCH v5 3/3 selftests/bpf: Test CO-RE relocations of signed enum values without kflag

In reply to Yonghong Song

The libbpf fix takes a 32-bit target enum value with the kflag 0 as signed if the local enum is signed, since the BTF of a kernel before v6.0 has the kflag 0 on every enum, signed or not. Test it.

test_core_reloc_enumval gets an optional signed local enum, named_int_enum, with the values -1 and 1, so the enum is signed while one of its values is not negative. The enumval target has the same enum with the values -2 and -3, which are expected sign-extended in the 64-bit outputs.

The new test case enumval___no_kflag has the same target types, but its target BTF gets the kflag of every enum cleared, as in the BTF of a kernel before v6.0. The signed enum then gets its signedness from the local enum. The test writes the modified BTF to a temporary file: one it creates itself in core_reloc, or the one btfgen already created in core_reloc_btfgen. The cleanup now removes the temporary file if the test case clears the kflag, not only if btfgen runs.

GCC's __builtin_preserve_enum_value() returns int, so with GCC the stores sign-extend the values anyway.

Without taking the signedness from the local enum, the upper 32 bits of both values are zero in enumval___no_kflag:

run_core_reloc_tests:FAIL:check_result input/output data don't match output byte #44: EXP 0xff GOT 0x00
...
output byte #55: EXP 0xff GOT 0x00
#122/72 core_reloc/enumval___no_kflag:FAIL

---
 .../selftests/bpf/prog_tests/core_reloc.c     | 90 ++++++++++++++++++-
 .../btf__core_reloc_enumval___no_kflag.c      |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 16 ++++
 .../bpf/progs/test_core_reloc_enumval.c       | 14 +++
 4 files changed, 121 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
tools/testing/selftests/bpf/prog_tests/core_reloc.c +88 -2
index 98b3c5fb1028..1c502e7f0c60 100644
@@ -370,6 +370,14 @@ static int duration = 0;
 	.fails = true,							\
 }
 
+#define ENUMVAL_NO_KFLAG_CASE(name, ...) {				\
+	ENUMVAL_CASE_COMMON(name),					\
+	.output = STRUCT_TO_CHAR_PTR(core_reloc_enumval_output)		\
+			__VA_ARGS__,					\
+	.output_len = sizeof(struct core_reloc_enumval_output),		\
+	.clear_enum_kflag = true,					\
+}
+
 #define ENUM64VAL_CASE_COMMON(name)					\
 	.case_name = #name,						\
 	.bpf_obj_file = "test_core_reloc_enum64val.bpf.o",		\
@@ -406,6 +414,7 @@ struct core_reloc_test_case {
 	bool run_btfgen_fails;
 	bool needs_testmod;
 	bool relaxed_core_relocs;
+	bool clear_enum_kflag;
 	const char *prog_name;
 	const char *raw_tp_name;
 	setup_test_fn setup;
@@ -906,6 +915,24 @@ static const struct core_reloc_test_case test_cases[] = {
 		.anon_val2 = 0x20,
 		.uint_val1 = 0x80000001,
 		.uint_val2 = 0xffffffff,
+		.int_val1 = -2,
+		.int_val2 = -3,
+	}),
+	ENUMVAL_NO_KFLAG_CASE(enumval___no_kflag, {
+		.named_val1_exists = true,
+		.named_val2_exists = true,
+		.named_val3_exists = true,
+		.anon_val1_exists = true,
+		.anon_val2_exists = true,
+		.anon_val3_exists = true,
+		.named_val1 = 1,
+		.named_val2 = 2,
+		.anon_val1 = 0x10,
+		.anon_val2 = 0x20,
+		.uint_val1 = 0x80000001,
+		.uint_val2 = 0xffffffff,
+		.int_val1 = -2,
+		.int_val2 = -3,
 	}),
 	ENUMVAL_CASE(enumval___diff, {
 		.named_val1_exists = true,
@@ -999,6 +1026,46 @@ static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objp
 	return run_bpftool_command(args);
 }
 
+/*
+ * Write the BTF of src to dst with the kflag of every enum cleared, as in the
+ * BTF of a kernel before v6.0, where the kflag 0 doesn't mean unsigned.
+ */
+static int write_btf_no_enum_kflag(const char *src, const char *dst)
+{
+	const void *raw_data;
+	struct btf_type *t;
+	struct btf *btf;
+	__u32 i, n, raw_sz;
+	FILE *f = NULL;
+	int err = -1;
+
+	btf = btf__parse(src, NULL);
+	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+		return -1;
+
+	n = btf__type_cnt(btf);
+	for (i = 1; i < n; i++) {
+		t = (struct btf_type *)btf__type_by_id(btf, i);
+		if (btf_is_enum(t))
+			t->info &= ~(1U << 31); /* the kflag */
+	}
+
+	raw_data = btf__raw_data(btf, &raw_sz);
+	if (!ASSERT_OK_PTR(raw_data, "btf_raw_data"))
+		goto out;
+	f = fopen(dst, "w");
+	if (!ASSERT_OK_PTR(f, "btf_fopen"))
+		goto out;
+	if (!ASSERT_EQ(fwrite(raw_data, 1, raw_sz, f), raw_sz, "btf_write"))
+		goto out;
+	err = 0;
+out:
+	if (f)
+		fclose(f);
+	btf__free(btf);
+	return err;
+}
+
 static void run_core_reloc_tests(bool use_btfgen)
 {
 	const size_t mmap_sz = roundup_page(sizeof(struct data));
@@ -1050,6 +1117,25 @@ static void run_core_reloc_tests(bool use_btfgen)
 			test_case->btf_src_file = btf_file;
 		}
 
+		/*
+		 * Without btfgen, create the temporary file here; with btfgen,
+		 * rewrite the one btfgen made.
+		 */
+		if (test_case->clear_enum_kflag) {
+			if (!use_btfgen) {
+				fd = mkstemp(btf_file);
+				if (!ASSERT_GE(fd, 0, "btf_tmp"))
+					continue;
+				close(fd); /* we only need the path */
+			}
+			err = write_btf_no_enum_kflag(test_case->btf_src_file, btf_file);
+			if (err) {
+				remove(btf_file);
+				continue;
+			}
+			test_case->btf_src_file = btf_file;
+		}
+
 		if (test_case->setup) {
 			err = test_case->setup(test_case);
 			if (CHECK(err, "test_setup", "test #%d setup failed: %d\n", i, err))
@@ -1141,8 +1227,8 @@ static void run_core_reloc_tests(bool use_btfgen)
 			CHECK_FAIL(munmap(mmap_data, mmap_sz));
 			mmap_data = NULL;
 		}
-		if (use_btfgen)
-			remove(test_case->btf_src_file);
+		if (use_btfgen || test_case->clear_enum_kflag)
+			remove(btf_file);
 		bpf_link__destroy(link);
 		link = NULL;
 		bpf_object__close(obj);
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c new file +3 -0
new file mode 100644
index 000000000000..f34e3e342efb
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___no_kflag x) {}
tools/testing/selftests/bpf/progs/core_reloc_types.h +16 -0
index 6d758e74bd58..35beb76068bd 100644
@@ -1217,6 +1217,8 @@ struct core_reloc_enumval_output {
 	int anon_val2;
 	long uint_val1;
 	long uint_val2;
+	long int_val1;
+	long int_val2;
 };
 
 struct core_reloc_enum64val_output {
@@ -1250,10 +1252,24 @@ enum named_uint_enum {
 	UINT_ENUM_VAL2 = 0xffffffff,
 };
 
+enum named_int_enum {
+	INT_ENUM_VAL1 = -2,
+	INT_ENUM_VAL2 = -3,
+};
+
 struct core_reloc_enumval {
 	enum named_enum f1;
 	anon_enum f2;
 	enum named_uint_enum f3;
+	enum named_int_enum f4;
+};
+
+/* the same types, whose BTF gets the kflag of every enum cleared */
+struct core_reloc_enumval___no_kflag {
+	enum named_enum f1;
+	anon_enum f2;
+	enum named_uint_enum f3;
+	enum named_int_enum f4;
 };
 
 enum named_unsigned_enum64 {
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +14 -0
index 8b8abbaaf533..a524e5028c77 100644
@@ -33,6 +33,12 @@ enum named_uint_enum {
 	UINT_ENUM_VAL2 = 0xfffffff0,
 };
 
+/* signed 4-byte enum, with a non-negative value too */
+enum named_int_enum {
+	INT_ENUM_VAL1 = -1,
+	INT_ENUM_VAL2 = 1,
+};
+
 struct core_reloc_enumval_output {
 	bool named_val1_exists;
 	bool named_val2_exists;
@@ -47,6 +53,8 @@ struct core_reloc_enumval_output {
 	int anon_val2;
 	long uint_val1;
 	long uint_val2;
+	long int_val1;
+	long int_val2;
 };
 
 #ifdef __clang__
@@ -85,6 +93,12 @@ int test_core_enumval(void *ctx)
 		out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1);
 	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2))
 		out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
+
+	/* enum named_int_enum is optional */
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL1))
+		out->int_val1 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL1);
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL2))
+		out->int_val2 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL2);
 #else
 	data.skip = true;
 #endif
Signature · 1 line
2.53.1-Meta
#4 lore raw

Re: [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag

TL;DR: Test issue: the new signed named_int_enum checks in test_core_enumval() may fail the enumval and enumval___no_kflag subtests on clang 12-14, which lack enum signedness kflag; should they be…
Quoted 1 line

selftests/bpf: Test CO-RE relocations of signed enum values without kflag

TL;DR: Test issue: the new signed named_int_enum checks in test_core_enumval() may fail the enumval and enumval___no_kflag subtests on clang 12-14, which lack enum signedness kflag; should they be guarded by __clang_major__ >= 15?

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +0 -0

[ ... ]

tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c int test_core_enumval(void *ctx) · line 104

Quoted 13 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +6 -0
@@ -85,6 +93,12 @@ int test_core_enumval(void *ctx)
 		out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1);
 	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2))
 		out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
+
+	/* enum named_int_enum is optional */
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL1))
+		out->int_val1 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL1);
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL2))
+		out->int_val2 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL2);
 #else
 	data.skip = true;
 #endif

Is this part of test_core_enumval() safe with clang 12 to 14?

These checks sit under the same guard as the rest of the function, #if __has_builtin(__builtin_preserve_enum_value), which clang 12 and later already satisfy. But clang only records enum signedness in the BTF kflag starting with LLVM 15 (enum64 support). Before that, the local named_int_enum gets kflag 0, and so does the target enum built by the same compiler.

With the libbpf change in this series, bpf_core_zext_enumval() then does not return early, and zero-extends the target value:

    if (btf_is_enum(targ_t) && !BTF_INFO_KFLAG(targ_t->info))
        res->new_val = (__u32)res->new_val;

For INT_ENUM_VAL2 the local value is 1, so the GCC-form fixup in bpf_core_patch_insn() does not apply and the instruction is patched with 0xfffffffd. out->int_val2 would then be 4294967293 while run_core_reloc_tests() expects .int_val2 = -3. That would make both the existing enumval subtest and the new enumval___no_kflag subtest fail with clang 12 to 14.

The neighbouring programs guard this compiler dependency, for example test_core_reloc_enum64val.c uses "#if __clang_major__ >= 15" and test_core_reloc_type_based.c uses "__has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15", both falling back to data.skip = true. tools/testing/selftests/bpf/README.rst also says core_reloc sub-tests are skipped if Clang is too old to support them rather than failing.

Should the named_int_enum part depend on __clang_major__ >= 15 so that it skips instead of failing, or silently leaving int_val1 and int_val2 at 0 (which would also fail the comparison)?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44827034192

author reviewer
#5 lore raw

Re: [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag

Quoted 53 lines · On 11/20/26 2:39 PM, [email protected] wrote:
Quoted 1 line

selftests/bpf: Test CO-RE relocations of signed enum values without kflag

TL;DR: Test issue: the new signed named_int_enum checks in test_core_enumval() may fail the enumval and enumval___no_kflag subtests on clang 12-14, which lack enum signedness kflag; should they be guarded by __clang_major__ >= 15?

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +0 -0

[ ... ]

tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c int test_core_enumval(void *ctx) · line 104

Quoted 13 lines
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +6 -0
@@ -85,6 +93,12 @@ int test_core_enumval(void *ctx)
  		out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1);
  	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2))
  		out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
+
+	/* enum named_int_enum is optional */
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL1))
+		out->int_val1 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL1);
+	if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL2))
+		out->int_val2 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL2);
  #else
  	data.skip = true;
  #endif

Is this part of test_core_enumval() safe with clang 12 to 14?

These checks sit under the same guard as the rest of the function, #if __has_builtin(__builtin_preserve_enum_value), which clang 12 and later already satisfy. But clang only records enum signedness in the BTF kflag starting with LLVM 15 (enum64 support). Before that, the local named_int_enum gets kflag 0, and so does the target enum built by the same compiler.

With the libbpf change in this series, bpf_core_zext_enumval() then does not return early, and zero-extends the target value:

     if (btf_is_enum(targ_t) && !BTF_INFO_KFLAG(targ_t->info))
         res->new_val = (__u32)res->new_val;

For INT_ENUM_VAL2 the local value is 1, so the GCC-form fixup in bpf_core_patch_insn() does not apply and the instruction is patched with 0xfffffffd. out->int_val2 would then be 4294967293 while run_core_reloc_tests() expects .int_val2 = -3. That would make both the existing enumval subtest and the new enumval___no_kflag subtest fail with clang 12 to 14.

The neighbouring programs guard this compiler dependency, for example test_core_reloc_enum64val.c uses "#if __clang_major__ >= 15" and test_core_reloc_type_based.c uses "__has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15", both falling back to data.skip = true. tools/testing/selftests/bpf/README.rst also says core_reloc sub-tests are skipped if Clang is too old to support them rather than failing.

Quoted 3 lines

Should the named_int_enum part depend on __clang_major__ >= 15 so that it skips instead of failing, or silently leaving int_val1 and int_val2 at 0 (which would also fail the comparison)?

I think we should be fine. In Documentation/process/changes.rst, the clang minimum version is 17.0.1. Also, for this selftest, I don't think people will build/run this selftest with `< 15' clang compiler.

Quoted 7 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44827034192

maintainer
#6 lore raw
In reply to Yonghong Song
Quoted 11 lines · Yonghong Song

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

Quoted 5 lines

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

Hi Yonghong,

I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). LLVM:

clang/include/clang/Basic/BuiltinsBPF.td:

  def PreserveEnumValue : TargetBuiltin {
    let Spellings = ["__builtin_preserve_enum_value"];
    let Attributes = [CustomTypeChecking];
    let Prototype = "long int(...)";
  }

---

gcc/config/bpf/bpf.cc:

def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE, build_function_type_list (integer_type_node, ptr_type_node,
integer_type_node, integer_type_node, 0));
gcc/tree.cc:

    integer_type_node = make_signed_type (INT_TYPE_SIZE);

Meaning that the two indeed disagree. Given that GCC version can't express 64-bit values, it appears that GCC side definition needs an update. Would this allow to avoid GCC-specific workaround in libbpf?

...

author reviewer
#7 lore raw
Quoted 44 lines · Eduard Zingerman
Quoted 11 lines · Yonghong Song

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

Quoted 5 lines

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

Hi Yonghong,

I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). LLVM:

   clang/include/clang/Basic/BuiltinsBPF.td:

   def PreserveEnumValue : TargetBuiltin {
     let Spellings = ["__builtin_preserve_enum_value"];
     let Attributes = [CustomTypeChecking];
     let Prototype = "long int(...)";
   }

   ---

   gcc/config/bpf/bpf.cc:

     def_builtin ("__builtin_preserve_enum_value",
                  BPF_BUILTIN_PRESERVE_ENUM_VALUE,
                  build_function_type_list (integer_type_node, ptr_type_node,
                                            integer_type_node, integer_type_node,
                                            0));
   gcc/tree.cc:

     integer_type_node = make_signed_type (INT_TYPE_SIZE);
Quoted 3 lines

Meaning that the two indeed disagree. Given that GCC version can't express 64-bit values, it appears that GCC side definition needs an update. Would this allow to avoid GCC-specific workaround in libbpf?

Yes, I am aware of this issue. See
https://lore.kernel.org/bpf/[email protected]/

I even suggested a solution:

gcc/config/bpf/bpf.cc index 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0)); renamed from gcc/config/bpf/bpf.cc +0 -0

def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, + build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node,

                                          0));
gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@ pack_enum_value_fail: else renamed from gcc/config/bpf/core-builtins.cc +0 -0
      {
        ret.reloc_data.expr = tmp;
+ /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode + CONST_INT. */         if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert (long_long_unsigned_type_node, enum_value);         else
         ret.reloc_data.default_value = integer_one_node;
      }

Vineet Gupta will take a look.

Discussed with Alexei earlier, the suggestion is to proceed as is for now. It is not clear when gcc will implement this.

author reviewer
#8 lore raw
Quoted 74 lines · Yonghong Song
Quoted 41 lines · Eduard Zingerman
Quoted 11 lines · Yonghong Song

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

Quoted 5 lines

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

Hi Yonghong,

I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). LLVM:

   clang/include/clang/Basic/BuiltinsBPF.td:

   def PreserveEnumValue : TargetBuiltin {
     let Spellings = ["__builtin_preserve_enum_value"]; let Attributes = [CustomTypeChecking]; let Prototype = "long int(...)";
   }

   ---

   gcc/config/bpf/bpf.cc:

     def_builtin ("__builtin_preserve_enum_value", BPF_BUILTIN_PRESERVE_ENUM_VALUE, build_function_type_list (integer_type_node, ptr_type_node, integer_type_node, integer_type_node, 0));
   gcc/tree.cc:

Quoted 6 lines

     integer_type_node = make_signed_type (INT_TYPE_SIZE);

Meaning that the two indeed disagree. Given that GCC version can't express 64-bit values, it appears that GCC side definition needs an update. Would this allow to avoid GCC-specific workaround in libbpf?

Yes, I am aware of this issue. See
https://lore.kernel.org/bpf/[email protected]/

I even suggested a solution:

gcc/config/bpf/bpf.cc index 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0)); renamed from gcc/config/bpf/bpf.cc +0 -0

    def_builtin ("__builtin_preserve_enum_value", BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, + build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node, 0));

gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@ pack_enum_value_fail:     else renamed from gcc/config/bpf/core-builtins.cc +0 -0

      {
        ret.reloc_data.expr = tmp;
+ /* Extend the enumerator to 64 bits according to its own signedness,
+ as clang does, instead of expanding it as a sign-extended SImode +
CONST_INT. */         if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; +
ret.reloc_data.default_value + = fold_convert (long_long_unsigned_type_node, enum_value);         else ret.reloc_data.default_value = integer_one_node;
      }

Quoted 4 lines

Vineet Gupta will take a look.

Discussed with Alexei earlier, the suggestion is to proceed as is for now. It is not clear when gcc will implement this.

Sorry, messed up the above diff. The below is a better one:

gcc/config/bpf/bpf.cc index 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0)); renamed from gcc/config/bpf/bpf.cc +0 -0

def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, + build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node,

                                          0));
gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@ pack_enum_value_fail: else renamed from gcc/config/bpf/core-builtins.cc +0 -0
      {
        ret.reloc_data.expr = tmp;
+ /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode + CONST_INT. */         if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert (long_long_unsigned_type_node, enum_value);         else
         ret.reloc_data.default_value = integer_one_node;
      }
maintainer
#9 lore raw
In reply to Yonghong Song
Quoted 77 lines · Yonghong Song
Quoted 44 lines · Eduard Zingerman
Quoted 11 lines · Yonghong Song

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

Quoted 5 lines

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

Hi Yonghong,

I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). LLVM:

   clang/include/clang/Basic/BuiltinsBPF.td:

   def PreserveEnumValue : TargetBuiltin {
     let Spellings = ["__builtin_preserve_enum_value"];
     let Attributes = [CustomTypeChecking];
     let Prototype = "long int(...)";
   }

   ---

   gcc/config/bpf/bpf.cc:

     def_builtin ("__builtin_preserve_enum_value",
                  BPF_BUILTIN_PRESERVE_ENUM_VALUE,
                  build_function_type_list (integer_type_node, ptr_type_node,
                                            integer_type_node, integer_type_node,
                                            0));
   gcc/tree.cc:

     integer_type_node = make_signed_type (INT_TYPE_SIZE);
Quoted 3 lines

Meaning that the two indeed disagree. Given that GCC version can't express 64-bit values, it appears that GCC side definition needs an update. Would this allow to avoid GCC-specific workaround in libbpf?

Yes, I am aware of this issue. See

    https://lore.kernel.org/bpf/[email protected]/

I even suggested a solution:

gcc/config/bpf/bpf.cc index renamed from gcc/config/bpf/bpf.cc +0 -0

3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++
b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0));
def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, +
build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node,

                                           0));
(quoted diff) +0 -0

b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@
pack_enum_value_fail: else

       {
         ret.reloc_data.expr = tmp;

+ /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode +
CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert
(long_long_unsigned_type_node, enum_value); else

Quoted 8 lines
          ret.reloc_data.default_value = integer_one_node;
       }

Vineet Gupta will take a look.

Discussed with Alexei earlier, the suggestion is to proceed as is for now. It is not clear when gcc will implement this.

Had a discussion with Andrii on this topic with the following conclusions:
- pahole 1.24 is 4 years old, it should be acceptable to bump minimal pahole version to get reliable signed/unsigned enum information.
- such a bump simplifies the fix, effectively moving it to:

static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,

				      const struct bpf_core_spec *spec,
				      __u64 *val)
  {
	const struct btf_type *t;

	switch (relo->kind) {
	case BPF_CORE_ENUMVAL_EXISTS:
		*val = spec ? 1 : 0;
		break;
	case BPF_CORE_ENUMVAL_VALUE:
		if (!spec)
			return -EUCLEAN; /* request instruction poisoning */
		t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
		if (btf_is_enum(t))
			*val = btf_enum(t)[spec->spec[0].idx].val; // <------- take care of a sign flag here.
		else
			*val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;

}

- we don't want to workaround GCC bug on the libbpf side, let's assume that __builtin_preserve_enum_value() always return long.

Wdyt?

author reviewer
#10 lore raw
Quoted 115 lines · Eduard Zingerman
Quoted 75 lines · Yonghong Song
Quoted 44 lines · Eduard Zingerman
Quoted 11 lines · Yonghong Song

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with the kflag 0, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

Quoted 5 lines

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

Hi Yonghong,

I checked LLVM and GCC definitions for __builtin_preserve_enum_value(). LLVM:

    clang/include/clang/Basic/BuiltinsBPF.td:

    def PreserveEnumValue : TargetBuiltin {
      let Spellings = ["__builtin_preserve_enum_value"];
      let Attributes = [CustomTypeChecking];
      let Prototype = "long int(...)";
    }

    ---

    gcc/config/bpf/bpf.cc:

      def_builtin ("__builtin_preserve_enum_value",
                   BPF_BUILTIN_PRESERVE_ENUM_VALUE,
                   build_function_type_list (integer_type_node, ptr_type_node,
                                             integer_type_node, integer_type_node,
                                             0));
    gcc/tree.cc:

      integer_type_node = make_signed_type (INT_TYPE_SIZE);
Quoted 3 lines

Meaning that the two indeed disagree. Given that GCC version can't express 64-bit values, it appears that GCC side definition needs an update. Would this allow to avoid GCC-specific workaround in libbpf?

Yes, I am aware of this issue. See

     https://lore.kernel.org/bpf/[email protected]/

I even suggested a solution:

gcc/config/bpf/bpf.cc index renamed from gcc/config/bpf/bpf.cc +0 -0

3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++
b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0));
def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, +
build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node,

                                            0));
(quoted diff) +0 -0

b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@
pack_enum_value_fail: else

        {
          ret.reloc_data.expr = tmp;

+ /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode +
CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert
(long_long_unsigned_type_node, enum_value); else

Quoted 8 lines
           ret.reloc_data.default_value = integer_one_node;
        }

Vineet Gupta will take a look.

Discussed with Alexei earlier, the suggestion is to proceed as is for now. It is not clear when gcc will implement this.

Had a discussion with Andrii on this topic with the following conclusions:
- pahole 1.24 is 4 years old, it should be acceptable to bump minimal pahole version to get reliable signed/unsigned enum information.
- such a bump simplifies the fix, effectively moving it to:

   static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
				      const struct bpf_core_spec *spec,
				      __u64 *val)
   {
	const struct btf_type *t;

	switch (relo->kind) {
	case BPF_CORE_ENUMVAL_EXISTS:
		*val = spec ? 1 : 0;
		break;
	case BPF_CORE_ENUMVAL_VALUE:
		if (!spec)
			return -EUCLEAN; /* request instruction poisoning */
		t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
		if (btf_is_enum(t))
			*val = btf_enum(t)[spec->spec[0].idx].val; // <------- take care of a sign flag here.
		else
			*val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;
   }
Quoted 4 lines

- we don't want to workaround GCC bug on the libbpf side, let's assume that __builtin_preserve_enum_value() always return long.

Wdyt?

Sounds good. I will have two patch set then. One is to bump kernel pahole version, and another is to fix libbpf.