* [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
2026-11-19 5:14 [PATCH bpf-next v2 0/4] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
@ 2026-11-19 5:14 ` Yonghong Song
2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 5:14 ` [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
` (2 subsequent siblings)
3 siblings, 1 reply; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 5:14 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] 10+ messages in thread* Re: [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
2026-11-19 5:14 ` [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song
@ 2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 15:55 ` Yonghong Song
0 siblings, 1 reply; 10+ messages in thread
From: bot+bpf-ci @ 2026-11-19 5:54 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: 1355 bytes --]
> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Commit message only: the Fixes tag may be wrong, since the
sign-extending enum read predates 23b2a3a8f63a and was introduced by
eacaaed784e2; the tag may need to change.
> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
>
> [ ... ]
>
> Fixes: 23b2a3a8f63a ("libbpf: Add enum64 relocation support")
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
The sign-extending read of btf_enum->val in bpf_core_calc_enumval_relo()
predates 23b2a3a8f63a, which only wrapped the existing read in an
if (btf_is_enum(t)) branch. The old code already had the same __s32 to
u64 sign extension:
e = btf_enum(t) + idx;
*val = e->val;
The sign-extending read was introduced by eacaaed784e2 ("libbpf: Implement
enum value-based CO-RE relocations"). Should the tag be:
Fixes: eacaaed784e2 ("libbpf: Implement enum value-based CO-RE relocations")
The unsigned-enum (kflag) distinction was added later, so the unsigned
symptom could arguably be dated to that, but the faulty read itself comes
from eacaaed784e2.
---
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/44465301903
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
2026-11-19 5:54 ` bot+bpf-ci
@ 2026-11-19 15:55 ` Yonghong Song
0 siblings, 0 replies; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 15:55 UTC (permalink / raw)
To: bpf
On 11/18/26 9:54 PM, bot+bpf-ci@kernel.org wrote:
>> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
> TL;DR: Commit message only: the Fixes tag may be wrong, since the
> sign-extending enum read predates 23b2a3a8f63a and was introduced by
> eacaaed784e2; the tag may need to change.
>
>> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
>>
>> [ ... ]
>>
>> Fixes: 23b2a3a8f63a ("libbpf: Add enum64 relocation support")
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> The sign-extending read of btf_enum->val in bpf_core_calc_enumval_relo()
> predates 23b2a3a8f63a, which only wrapped the existing read in an
> if (btf_is_enum(t)) branch. The old code already had the same __s32 to
> u64 sign extension:
>
> e = btf_enum(t) + idx;
> *val = e->val;
>
> The sign-extending read was introduced by eacaaed784e2 ("libbpf: Implement
> enum value-based CO-RE relocations"). Should the tag be:
>
> Fixes: eacaaed784e2 ("libbpf: Implement enum value-based CO-RE relocations")
>
> The unsigned-enum (kflag) distinction was added later, so the unsigned
> symptom could arguably be dated to that, but the faulty read itself comes
> from eacaaed784e2.
I don't think we should use the following:
Fixes: eacaaed784e2 ("libbpf: Implement enum value-based CO-RE relocations")
This commit is dated back to 2020:
commit eacaaed784e2c9da69dea3030c81062c1fd66a37
Author: Andrii Nakryiko <andriin@fb.com>
Date: Wed Aug 19 12:45:18 2020 -0700
libbpf: Implement enum value-based CO-RE relocations
and I don't think this can be backported due to in between a lot of changes.
I will choose this one:
Fixes: 776281652ddc ("libbpf: Permit 64bit relocation value")
commit 776281652ddcd98bb292335ea3634da341c849b4
Author: Yonghong Song <yhs@fb.com>
Date: Mon Jun 6 23:26:05 2022 -0700
libbpf: Permit 64bit relocation value
This commits allows 64bit relocation value and this is directly related
to this patch set.
>
>
> ---
> 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/44465301903
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
2026-11-19 5:14 [PATCH bpf-next v2 0/4] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-19 5:14 ` [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song
@ 2026-11-19 5:14 ` Yonghong Song
2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 5:14 ` [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations Yonghong Song
2026-11-19 5:15 ` [PATCH bpf-next v2 4/4] selftests/bpf: Take unsigned enum values as unsigned int for GCC Yonghong Song
3 siblings, 1 reply; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 5:14 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] 10+ messages in thread* Re: [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
2026-11-19 5:14 ` [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
@ 2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 15:57 ` Yonghong Song
0 siblings, 1 reply; 10+ messages in thread
From: bot+bpf-ci @ 2026-11-19 5:54 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: 3342 bytes --]
> selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
TL;DR: The new enum test likely fails core_reloc/enumval on the GCC-BPF flavor
at this commit, since GCC sign-extends the value; fixes come later in the
series, so it isn't bisectable.
> diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
> --- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
> +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
[ ... ]
> @@ -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);
Will this pass for the GCC-BPF flavor of the object (test_progs-bpf_gcc)?
Under !__clang__, bpf_core_enum_value() expands to
__builtin_preserve_enum_value(), which returns int, so GCC emits the
ld_imm64 sign-extended (for example lddw %r1,-2147483648 for 0x80000000).
With f3507c4b38e8 applied, bpf_core_calc_enumval_relo() gets orig_val from
btf_enum_value(), which is (__u32)e->val for a kflag 0 enum, and
bpf_core_patch_insn() then validates it against the instruction:
imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
if (res->validate && imm != orig_val) {
pr_warn("... unexpected insn #%d (LDIMM64) value: ...");
return -EINVAL;
}
For the GCC-built object imm is 0xffffffff80000000 while orig_val is
0x80000000, so bpf_object__load() should fail and ASSERT_OK(err,
"obj_load") would fail core_reloc/enumval, and core_reloc_btfgen/enumval
the same way. ENUMVAL_CASE(enumval) does not set .fails, neither subtest
is in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the gcc-bpf job
(KERNEL_TEST: test_progs-bpf_gcc) runs on x86_64.
Even with only the libbpf fix, the int result stored into the new long
uint_val1/uint_val2 fields would read back as 0xffffffff80000001, while
core_reloc.c expects 0x80000001 and 0xffffffff.
This looks to be fixed later in the series, and both are needed for GCC:
aa93e7e947e0 ("libbpf: Accept sign-extended 32-bit enum values in CO-RE
relocations") makes libbpf accept the sign-extended form.
ec81091d83f1 ("selftests/bpf: Take unsigned enum values as unsigned int
for GCC") adds UINT_ENUM_VALUE(), which casts to unsigned int under
!__clang__.
Until both land, test_progs-bpf_gcc fails at this commit, so the series is
not bisectable for that flavor. Could those changes be folded into this
commit and f3507c4b38e8, or the series reordered?
The commit message also describes only the clang behaviour ("whose values
clang emits zero-extended in the ld_imm64 of the relocations").
---
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/44465301903
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
2026-11-19 5:54 ` bot+bpf-ci
@ 2026-11-19 15:57 ` Yonghong Song
0 siblings, 0 replies; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 15:57 UTC (permalink / raw)
To: bot+bpf-ci, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, martin.lau, mason,
ihor.solodrai
On 11/18/26 9:54 PM, bot+bpf-ci@kernel.org wrote:
>> selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
> TL;DR: The new enum test likely fails core_reloc/enumval on the GCC-BPF flavor
> at this commit, since GCC sign-extends the value; fixes come later in the
> series, so it isn't bisectable.
>
>> diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
>> --- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
>> +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
> [ ... ]
>
>> @@ -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);
> Will this pass for the GCC-BPF flavor of the object (test_progs-bpf_gcc)?
>
> Under !__clang__, bpf_core_enum_value() expands to
> __builtin_preserve_enum_value(), which returns int, so GCC emits the
> ld_imm64 sign-extended (for example lddw %r1,-2147483648 for 0x80000000).
>
> With f3507c4b38e8 applied, bpf_core_calc_enumval_relo() gets orig_val from
> btf_enum_value(), which is (__u32)e->val for a kflag 0 enum, and
> bpf_core_patch_insn() then validates it against the instruction:
>
> imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
> if (res->validate && imm != orig_val) {
> pr_warn("... unexpected insn #%d (LDIMM64) value: ...");
> return -EINVAL;
> }
>
> For the GCC-built object imm is 0xffffffff80000000 while orig_val is
> 0x80000000, so bpf_object__load() should fail and ASSERT_OK(err,
> "obj_load") would fail core_reloc/enumval, and core_reloc_btfgen/enumval
> the same way. ENUMVAL_CASE(enumval) does not set .fails, neither subtest
> is in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the gcc-bpf job
> (KERNEL_TEST: test_progs-bpf_gcc) runs on x86_64.
>
> Even with only the libbpf fix, the int result stored into the new long
> uint_val1/uint_val2 fields would read back as 0xffffffff80000001, while
> core_reloc.c expects 0x80000001 and 0xffffffff.
>
> This looks to be fixed later in the series, and both are needed for GCC:
>
> aa93e7e947e0 ("libbpf: Accept sign-extended 32-bit enum values in CO-RE
> relocations") makes libbpf accept the sign-extended form.
>
> ec81091d83f1 ("selftests/bpf: Take unsigned enum values as unsigned int
> for GCC") adds UINT_ENUM_VALUE(), which casts to unsigned int under
> !__clang__.
>
> Until both land, test_progs-bpf_gcc fails at this commit, so the series is
> not bisectable for that flavor. Could those changes be folded into this
> commit and f3507c4b38e8, or the series reordered?
>
> The commit message also describes only the clang behaviour ("whose values
> clang emits zero-extended in the ld_imm64 of the relocations").
You are correct. this will still have gcc test failure.
So in the next revision, I will merge 1/3 as the first commit
and 2/4 as the second commit. This way, we can avoid the selftest failure
during bisecting.
>
>
> ---
> 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/44465301903
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
2026-11-19 5:14 [PATCH bpf-next v2 0/4] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-19 5:14 ` [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song
2026-11-19 5:14 ` [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
@ 2026-11-19 5:14 ` Yonghong Song
2026-11-19 5:21 ` sashiko-bot
2026-11-19 5:15 ` [PATCH bpf-next v2 4/4] selftests/bpf: Take unsigned enum values as unsigned int for GCC Yonghong Song
3 siblings, 1 reply; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 5:14 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
The value of a 32-bit enum in a BPF_CORE_ENUMVAL_VALUE relocation is in
the low 32 bits of the ld_imm64, and the compiler chooses how it is
extended to 64 bits. Clang zero-extends the value of an unsigned enum
and sign-extends that of a signed one. GCC sign-extends every value,
since its __builtin_preserve_enum_value() returns int:
enum E { V = 0x80000000 };
out = bpf_core_enum_value(enum E, V);
clang: r1 = 0x80000000 ll
gcc: lddw %r1,-2147483648
The previous patch extends the original value as given by the kflag of
the enum, which is the form clang emits. So an object built by GCC,
which was accepted when the value was sign-extended for all 32-bit
enums, is now refused:
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 given by the kflag of the target enum, 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.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/relo_core.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 7aa7eb4e4021..76efee8e876f 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1032,6 +1032,19 @@ static int insn_bytes_to_bpf_size(__u32 sz)
}
}
+/* A 32-bit enum value is in the low 32 bits of the ld_imm64, extended as
+ * the compiler chooses: clang zero-extends an unsigned value, GCC
+ * sign-extends every value, as its __builtin_preserve_enum_value() returns
+ * int. Check whether the instruction has the sign-extended form of an
+ * unsigned value, so that the target value gets that form too.
+ */
+static bool bpf_core_enumval_is_sext(const struct bpf_core_relo *relo,
+ __u64 imm, __u64 orig_val)
+{
+ return relo->kind == BPF_CORE_ENUMVAL_VALUE && orig_val <= UINT_MAX &&
+ imm != orig_val && imm == (__u64)(__s32)orig_val;
+}
+
/*
* Patch relocatable BPF instruction.
*
@@ -1148,6 +1161,11 @@ 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);
+ if (res->validate && bpf_core_enumval_is_sext(relo, imm, 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,
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
2026-11-19 5:14 ` [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations Yonghong Song
@ 2026-11-19 5:21 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-19 5:21 UTC (permalink / raw)
To: Yonghong Song, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] libbpf: non-standard multi-line comment style above
bpf_core_enumval_is_sext()
--
commit 2ec397921876fd311cd75d218c792d79da6cd995
Author: Yonghong Song <yonghong.song@linux.dev>
libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
This commit accepts sign-extended 32-bit unsigned enum values in
BPF_CORE_ENUMVAL_VALUE relocations and sign-extends the patched 32-bit
target value to match GCC's output.
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 7aa7eb4e4021b..76efee8e876f0 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
> @@ -1032,6 +1032,19 @@ static int insn_bytes_to_bpf_size(__u32 sz)
> }
> }
>
> +/* A 32-bit enum value is in the low 32 bits of the ld_imm64, extended as
> + * the compiler chooses: clang zero-extends an unsigned value, GCC
> + * sign-extends every value, as its __builtin_preserve_enum_value() returns
> + * int. Check whether the instruction has the sign-extended form of an
> + * unsigned value, so that the target value gets that form too.
> + */
> +static bool bpf_core_enumval_is_sext(const struct bpf_core_relo *relo,
> + __u64 imm, __u64 orig_val)
> +{
> + return relo->kind == BPF_CORE_ENUMVAL_VALUE && orig_val <= UINT_MAX &&
> + imm != orig_val && imm == (__u64)(__s32)orig_val;
> +}
[Severity: Low]
This isn't a bug, but BPF code uses the multi-line comment style where the
opening /* is placed on its own line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119051443.1902163-1-yonghong.song@linux.dev?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next v2 4/4] selftests/bpf: Take unsigned enum values as unsigned int for GCC
2026-11-19 5:14 [PATCH bpf-next v2 0/4] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
` (2 preceding siblings ...)
2026-11-19 5:14 ` [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations Yonghong Song
@ 2026-11-19 5:15 ` Yonghong Song
3 siblings, 0 replies; 10+ messages in thread
From: Yonghong Song @ 2026-11-19 5:15 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
GCC's __builtin_preserve_enum_value() returns int, so storing the
value of enum named_uint_enum into a long field sign-extends it. With
the previous patch, the program built by GCC loads, but uint_val1 reads
back as 0xffffffff80000001 while the test expects 0x80000001. 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.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/progs/test_core_reloc_enumval.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
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 964297b9bab5..dc15caf8c151 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
@@ -49,6 +49,13 @@ struct core_reloc_enumval_output {
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)
{
@@ -75,9 +82,9 @@ int test_core_enumval(void *ctx)
/* 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);
+ 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 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2);
+ out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
#else
data.skip = true;
#endif
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 10+ messages in thread