All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	kernel-team@fb.com, vineet.gupta@linux.dev
Subject: Re: [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Mon, 23 Nov 2026 14:08:59 -0800	[thread overview]
Message-ID: <a189a17f-8544-43ed-8eff-116191c41a62@linux.dev> (raw)
In-Reply-To: <d678f6992169065c5f1e67b89d39ad6a463923c8.camel@gmail.com>



On 11/23/26 11:46 AM, Eduard Zingerman wrote:
> On Mon, 2026-11-23 at 10:01 -0800, Yonghong Song wrote:
>> On 11/23/26 9:45 AM, Eduard Zingerman wrote:
>>> On Fri, 2026-11-20 at 13:49 -0800, Yonghong Song wrote:
>>>> 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.
>>> 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?
>> Yes, I am aware of this issue. See
>>      https://lore.kernel.org/bpf/a4aec2b1-f754-48ed-a7d7-ef07bed43eb8@linux.dev/
>>
>> I even suggested a solution:
>>
>> 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;
>>         }
>>
>> 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?

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



      reply	other threads:[~2026-11-23 22:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:49 [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 1/3] " Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 2/3] selftests/bpf: Test " Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag Yonghong Song
2026-11-20 22:39   ` bot+bpf-ci
2026-11-21  0:21     ` Yonghong Song
2026-11-23 17:45 ` [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Eduard Zingerman
2026-11-23 18:01   ` Yonghong Song
2026-11-23 18:04     ` Yonghong Song
2026-11-23 19:46     ` Eduard Zingerman
2026-11-23 22:08       ` Yonghong Song [this message]

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=a189a17f-8544-43ed-8eff-116191c41a62@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 \
    --cc=vineet.gupta@linux.dev \
    /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.