* [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
@ 2026-11-18 16:10 Yonghong Song
2026-11-18 16:10 ` [PATCH bpf-next 2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-18 16:59 ` [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations bot+bpf-ci
0 siblings, 2 replies; 4+ messages in thread
From: Yonghong Song @ 2026-11-18 16:10 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
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") added btf_enum_value(), which extends the value as given by
the kflag of the enum, for btf_dump and map fields, but not for CO-RE
relocations, whose relo_core.c is also built into the kernel. Use it in
bpf_core_calc_enumval_relo() as well, and add it to the kernel's
include/linux/btf.h, so that the kernel gets the same fix.
Fixes: 23b2a3a8f63a ("libbpf: Add enum64 relocation support")
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/btf.h | 5 +++++
tools/lib/bpf/relo_core.c | 12 ++++++++----
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 87fad4076f0e..ceb27984df14 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -525,6 +525,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t)
return (struct btf_enum *)(t + 1);
}
+static inline u64 btf_enum_value(const struct btf_type *t, const struct btf_enum *e)
+{
+ return btf_type_kflag(t) ? (s64)e->val : (u32)e->val;
+}
+
static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
{
return (struct btf_enum64 *)(t + 1);
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 2672623a4198..7aa7eb4e4021 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -861,6 +861,13 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
return 0;
}
+static __u64 bpf_core_enumval(const struct btf_type *t, int idx)
+{
+ if (btf_is_enum(t))
+ return btf_enum_value(t, btf_enum(t) + idx);
+ return btf_enum64_value(btf_enum64(t) + idx);
+}
+
static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
const struct bpf_core_spec *spec,
__u64 *val)
@@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
if (!spec)
return -EUCLEAN; /* request instruction poisoning */
t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
- if (btf_is_enum(t))
- *val = btf_enum(t)[spec->spec[0].idx].val;
- else
- *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
+ *val = bpf_core_enumval(t, spec->spec[0].idx);
break;
default:
return -EOPNOTSUPP;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH bpf-next 2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values 2026-11-18 16:10 [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song @ 2026-11-18 16:10 ` Yonghong Song 2026-11-18 16:59 ` [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations bot+bpf-ci 1 sibling, 0 replies; 4+ messages in thread From: Yonghong Song @ 2026-11-18 16:10 UTC (permalink / raw) To: bpf Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Eduard Zingerman, kernel-team Test the zero 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. 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 Signed-off-by: Yonghong Song <yonghong.song@linux.dev> --- .../testing/selftests/bpf/prog_tests/core_reloc.c | 2 ++ .../testing/selftests/bpf/progs/core_reloc_types.h | 8 ++++++++ .../selftests/bpf/progs/test_core_reloc_enumval.c | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c index 80432c4656b5..98b3c5fb1028 100644 --- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c @@ -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, diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h index 5760ae015e09..6d758e74bd58 100644 --- a/tools/testing/selftests/bpf/progs/core_reloc_types.h +++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h @@ -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 { diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c index e7ef3dada2bf..964297b9bab5 100644 --- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c @@ -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,6 +45,8 @@ struct core_reloc_enumval_output { int named_val2; int anon_val1; int anon_val2; + long uint_val1; + long uint_val2; }; SEC("raw_tracepoint/sys_enter") @@ -64,6 +72,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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2); #else data.skip = true; #endif -- 2.53.1-Meta ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations 2026-11-18 16:10 [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song 2026-11-18 16:10 ` [PATCH bpf-next 2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song @ 2026-11-18 16:59 ` bot+bpf-ci 2026-11-19 1:30 ` Yonghong Song 1 sibling, 1 reply; 4+ messages in thread From: bot+bpf-ci @ 2026-11-18 16:59 UTC (permalink / raw) To: yonghong.song, bpf Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai [-- Attachment #1: Type: text/plain, Size: 3159 bytes --] > libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations TL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may make GCC-built objects, which appear to emit sign-extended values (unverified), fail to load with -EINVAL, in libbpf and kernel bpf_core_apply(). > diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c > --- a/tools/lib/bpf/relo_core.c > +++ b/tools/lib/bpf/relo_core.c [ ... ] > @@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, > if (!spec) > return -EUCLEAN; /* request instruction poisoning */ > t = btf_type_by_id(spec->btf, spec->spec[0].type_id); > - if (btf_is_enum(t)) > - *val = btf_enum(t)[spec->spec[0].idx].val; > - else > - *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx); > + *val = bpf_core_enumval(t, spec->spec[0].idx); > break; Can this break objects built by GCC? bpf_core_patch_insn() compares the local (orig) value against the instruction immediate using a full 64-bit compare. In the ALU path that is insn->imm != orig_val, where the int imm is sign-extended, and in the LDIMM64 path it is: (__u32)insn[0].imm | ((__u64)insn[1].imm << 32) Clang emits unsigned enumerators zero-extended, which is the case this patch fixes. From reading the GCC source (not verified by compiling), GCC appears to emit every 32-bit enumerator sign-extended: gcc/config/bpf/core-builtins.cc:pack_enum_value() { ret.reloc_data.default_value = enum_value; } bpf_expand_core_builtin() then does expand_normal(data->default_value) and wraps it in an UNSPEC. An SImode CONST_INT is stored sign-extended in RTL, so 0x80000000 would be printed as 0xffffffff80000000. GCC's btfout.cc also sets btf_kflag to BTF_KF_ENUM_UNSIGNED, which is 0, for unsigned enums. For a GCC-built object calling bpf_core_enum_value() on an enumerator of 0x80000000 in an unsigned enum: Before this patch, *val was the sign-extended 0xffffffff80000000, which matched the instruction. After this patch, btf_enum_value() returns 0x80000000 for kflag == 0, so bpf_core_patch_insn() would report "unexpected insn #N (ALU/ALU64|LDIMM64) value: got ..., exp 2147483648" and return -EINVAL. The same code is built into the kernel through kernel/bpf/relo_core.c, so bpf_core_apply() is affected as well. Would objects built by Clang now load while GCC-built objects that loaded before are refused? The core_reloc/enumval tests are not in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the test added by 0aff1079d5e1 ("selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values") uses UINT_ENUM_VAL1 = 0x80000000, so it may show this in the test_progs-bpf_gcc lane. Could the validation for 32-bit enums compare only the low 32 bits and accept upper bits that are either zero or a sign extension? Alternatively, the GCC encoding could be determined before changing orig_val. --- 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/44347609693 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations 2026-11-18 16:59 ` [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations bot+bpf-ci @ 2026-11-19 1:30 ` Yonghong Song 0 siblings, 0 replies; 4+ messages in thread From: Yonghong Song @ 2026-11-19 1:30 UTC (permalink / raw) To: bot+bpf-ci, bpf Cc: ast, andrii, daniel, eddyz87, kernel-team, martin.lau, mason, ihor.solodrai On 11/18/26 8:59 AM, bot+bpf-ci@kernel.org wrote: >> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations > TL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may > make GCC-built objects, which appear to emit sign-extended values > (unverified), fail to load with -EINVAL, in libbpf and kernel > bpf_core_apply(). > >> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c >> --- a/tools/lib/bpf/relo_core.c >> +++ b/tools/lib/bpf/relo_core.c > [ ... ] > >> @@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, >> if (!spec) >> return -EUCLEAN; /* request instruction poisoning */ >> t = btf_type_by_id(spec->btf, spec->spec[0].type_id); >> - if (btf_is_enum(t)) >> - *val = btf_enum(t)[spec->spec[0].idx].val; >> - else >> - *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx); >> + *val = bpf_core_enumval(t, spec->spec[0].idx); >> break; > Can this break objects built by GCC? > > bpf_core_patch_insn() compares the local (orig) value against the > instruction immediate using a full 64-bit compare. In the ALU path that is > insn->imm != orig_val, where the int imm is sign-extended, and in the > LDIMM64 path it is: > > (__u32)insn[0].imm | ((__u64)insn[1].imm << 32) > > Clang emits unsigned enumerators zero-extended, which is the case this > patch fixes. From reading the GCC source (not verified by compiling), GCC > appears to emit every 32-bit enumerator sign-extended: > > gcc/config/bpf/core-builtins.cc:pack_enum_value() { > ret.reloc_data.default_value = enum_value; > } > > bpf_expand_core_builtin() then does expand_normal(data->default_value) and > wraps it in an UNSPEC. An SImode CONST_INT is stored sign-extended in RTL, > so 0x80000000 would be printed as 0xffffffff80000000. GCC's btfout.cc also > sets btf_kflag to BTF_KF_ENUM_UNSIGNED, which is 0, for unsigned enums. > > For a GCC-built object calling bpf_core_enum_value() on an enumerator of > 0x80000000 in an unsigned enum: > > Before this patch, *val was the sign-extended 0xffffffff80000000, which > matched the instruction. > > After this patch, btf_enum_value() returns 0x80000000 for kflag == 0, so > bpf_core_patch_insn() would report "unexpected insn #N (ALU/ALU64|LDIMM64) > value: got ..., exp 2147483648" and return -EINVAL. The same code is built > into the kernel through kernel/bpf/relo_core.c, so bpf_core_apply() is > affected as well. > > Would objects built by Clang now load while GCC-built objects that loaded > before are refused? The core_reloc/enumval tests are not in > ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the test added by > 0aff1079d5e1 ("selftests/bpf: Test CO-RE relocations of unsigned 32-bit > enum values") uses UINT_ENUM_VAL1 = 0x80000000, so it may show this in the > test_progs-bpf_gcc lane. Yes, this is indeed an issue for GCC. Specifically because: - BTF type is unsigned. This actually correct for the following: enum dentry_flags { DCACHE_PRIVATE = 0x80000000 }; - Without this patch, both clang and gcc will have enum val with sign extension. gcc enum return value has type int so naturally it will do sign extension, but clang return value is unsigned, but it use signed type, so this is wrong. This patch fixed clang to use unsigned type based on BTF and this is correct. For gcc, BTF type is unsigned (based on language), but the enum return value is signed. This caused a problem. gcc already generates code assuming signed type and we cannot change already generated code, so the only thing is to change gcc BTF type from 'unsigned' to 'signed'. This is doable though. > > Could the validation for 32-bit enums compare only the low 32 bits and > accept upper bits that are either zero or a sign extension? Alternatively, > the GCC encoding could be determined before changing orig_val. So yes, we can change orig_val to be signed (instead of original unsigned type) to fix the issue. Then enum value will be different, for clang, the value is 32bit unsigned. For gcc, the value will be 32bit sign-extension. The divergence between clang and gcc is in return type, clang (u64, unsigned) and gcc(int, signed). If gcc can have return value the same as clang, things can be simplified. The following gcc diff might be useful for gcc folks: diff --git a/gcc/config/bpf/bpf.cc b/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)); 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)); diff --git a/gcc/config/bpf/core-builtins.cc 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 ret.reloc_data.default_value = integer_one_node; } > > > --- > 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/44347609693 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-19 1:31 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-18 16:10 [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song 2026-11-18 16:10 ` [PATCH bpf-next 2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song 2026-11-18 16:59 ` [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations bot+bpf-ci 2026-11-19 1:30 ` Yonghong Song
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox