From: Yonghong Song <yonghong.song@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Cc: Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next v4 1/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Fri, 20 Nov 2026 12:48:56 -0800 [thread overview]
Message-ID: <b3d75779-ac46-4182-ab22-a74e2010073e@linux.dev> (raw)
In-Reply-To: <DN0ABWWQD6EE.3K5F7Q9N05BCL@gmail.com>
On 11/20/26 11:28 AM, Alexei Starovoitov wrote:
> On Fri, Nov 20, 2026 at 08:55 AM Yonghong Song <yonghong.song@linux.dev> wrote:
>> 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.
> That rule is one 'if', but new_val gets there in three steps:
>
> step new_val
> ----------------------------------- --------------------------------
> 1 bpf_core_enumval() extended by the target kflag
> 2 bpf_core_enumval_targ_is_signed() sign-extended by the local kflag
> 3 bpf_core_enumval_is_sext() sign-extended by the insn
>
> Another idea...
> Keep the sign extension in bpf_core_calc_enumval_relo() as it is today.
> Zero-extend once in bpf_core_calc_relo() when neither enum is signed.
> Steps 1 and 2 become one function.
> BTF_INFO_KFLAG() works in libbpf and in the kernel.
> bpf_core_format_spec() uses it already.
> The table in the commit log stays the same.
> Then no need for btf_enum_value() in include/linux/btf.h, btf_kflag(),
> bpf_core_enumval(), bpf_core_enumval_targ_is_signed(), etc.
>
> Instead of patch 1. kernel/bpf/relo_core.o builds with it, no test
> was run:
>
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 2672623..3f37a08 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
> @@ -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,
>
> pw-bot: cr
Yes, your code is much simpler than mine with the same functionality.
I will use your code then. Thanks!
next prev parent reply other threads:[~2026-11-20 20:49 UTC|newest]
Thread overview: 6+ 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 ` [PATCH bpf-next v4 1/3] " Yonghong Song
2026-11-20 19:28 ` Alexei Starovoitov
2026-11-20 20:48 ` Yonghong Song [this message]
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=b3d75779-ac46-4182-ab22-a74e2010073e@linux.dev \
--to=yonghong.song@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.