From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: [PATCH bpf-next v4 1/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Fri, 20 Nov 2026 08:55:45 -0800 [thread overview]
Message-ID: <20261120165545.2074537-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261120165540.2074139-1-yonghong.song@linux.dev>
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.
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 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.
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, a negative target value, which has been
sign-extended so far, would become, 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 if the target enum has the kflag 0
but the local enum is signed, take the 32-bit target value as signed
too. A target value is then zero-extended only if neither enum is
signed.
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")
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/btf.h | 5 ++++
tools/lib/bpf/relo_core.c | 59 ++++++++++++++++++++++++++++++++++++---
2 files changed, 60 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..b83d7b72ab05 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -13,6 +13,11 @@ static const char *btf_kind_str(const struct btf_type *t)
return btf_type_str(t);
}
+static bool btf_kflag(const struct btf_type *t)
+{
+ return btf_type_kflag(t);
+}
+
static bool is_ldimm64_insn(struct bpf_insn *insn)
{
return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
@@ -861,6 +866,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 +887,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;
@@ -887,6 +896,27 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
return 0;
}
+/*
+ * The kflag 0 of a 32-bit enum doesn't always mean unsigned: 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. If the local enum is signed, take a 32-bit
+ * target value with the kflag 0 as signed too, so that, e.g., -1 stays -1
+ * on such a kernel.
+ */
+static bool bpf_core_enumval_targ_is_signed(const struct bpf_core_relo *relo,
+ const struct bpf_core_spec *local_spec,
+ const struct bpf_core_spec *targ_spec)
+{
+ const struct btf_type *local_t, *targ_t;
+
+ if (relo->kind != BPF_CORE_ENUMVAL_VALUE)
+ return false;
+
+ 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);
+ return btf_is_enum(targ_t) && !btf_kflag(targ_t) && btf_kflag(local_t);
+}
+
/* 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 +992,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 && bpf_core_enumval_targ_is_signed(relo, local_spec, targ_spec))
+ res->new_val = (__s32)res->new_val;
}
done:
@@ -1028,6 +1060,20 @@ 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.
*
@@ -1144,6 +1190,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
next prev parent reply other threads:[~2026-11-20 16:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 16:55 [PATCH bpf-next v4 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-20 16:55 ` Yonghong Song [this message]
2026-11-20 19:28 ` [PATCH bpf-next v4 1/3] " Alexei Starovoitov
2026-11-20 16:55 ` [PATCH bpf-next v4 2/3] selftests/bpf: Test " Yonghong Song
2026-11-20 16:55 ` [PATCH bpf-next v4 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120165545.2074537-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).