BPF List
 help / color / mirror / Atom feed
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 v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Fri, 20 Nov 2026 13:49:57 -0800	[thread overview]
Message-ID: <20261120214957.868409-1-yonghong.song@linux.dev> (raw)

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.

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, so there the kflag 0
doesn't mean unsigned. If the local enum is signed, patch 1 takes such
a target value as signed too, so that, e.g., -1 doesn't become
0xffffffff.

Patch 2 tests the unsigned enum values, with clang and GCC. Patch 3
tests the signed ones against a target BTF without the kflag.

Changelog:
  v4 -> v5:
    - v4: https://lore.kernel.org/bpf/20261120165540.2074139-1-yonghong.song@linux.dev/
    - Keep the sign extension in bpf_core_calc_enumval_relo(), and
      zero-extend the 32-bit values of unsigned enums once in
      bpf_core_calc_relo(). This drops btf_enum_value() from
      include/linux/btf.h and the three helpers in relo_core.c. No
      change in behavior (patch 1).
  v3 -> v4:
    - v3: https://lore.kernel.org/bpf/20261119232647.3392045-1-yonghong.song@linux.dev/
    - Take a 32-bit target enum value as signed if the target enum has
      the kflag 0 but the local enum is signed, for the BTF of kernels
      without enum signedness (patch 1).
    - Test it with a target BTF whose enums have the kflag cleared
      (patch 3).
  v2 -> v3:
    - v2: https://lore.kernel.org/bpf/20261119051443.1902163-1-yonghong.song@linux.dev/
    - Fold patch 3 into patch 1 and patch 4 into patch 2 to avoid test
      failures when bisecting.
  v1 -> v2:
    - v1: https://lore.kernel.org/bpf/20261118161053.1768878-1-yonghong.song@linux.dev/
    - Accept the sign-extended enum values emitted by GCC (patch 3), and
      make the new test work with GCC (patch 4).

Yonghong Song (3):
  libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
  selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
  selftests/bpf: Test CO-RE relocations of signed enum values without
    kflag

 tools/lib/bpf/relo_core.c                     | 31 +++++++
 .../selftests/bpf/prog_tests/core_reloc.c     | 92 ++++++++++++++++++-
 .../btf__core_reloc_enumval___no_kflag.c      |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 24 +++++
 .../bpf/progs/test_core_reloc_enumval.c       | 35 +++++++
 5 files changed, 183 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c

-- 
2.53.1-Meta


             reply	other threads:[~2026-11-20 21:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:49 Yonghong Song [this message]
2026-11-20 21:50 ` [PATCH bpf-next v5 1/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values 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

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=20261120214957.868409-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