From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=66.220.155.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795211416; b=M5Y2ZR+yqr4E+Kiz1PKAdnkBjVstDvEE2qwFqN86rtG3HsfuZJA0ShGIWNDRa7oVSR+0 kGJIfDj1CLNBEB0BogsdoWZl/3bp6NLhG/YoI7w4SoIkeM1Op2TxA5GT7sbgBgY89HRVM 8OTGbGXRo1U17M5MGHnXzO7pG7GC22+elKpOkuEFuJhiDE6ZeG7a1F6iu/sx2Lolvdfee C48yoPIm+grs0F/kdjM034I8D6sU+jTk28LT8LM3112Y+GcCZHVYHKtOWvkKXDshJNbHy Gk8mg+qeNTN6vSNuYV/+VfsfbMliuTQpsB4b2b0PbsZtn1QLM7UAaY1DECM/ahXtksg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795211416; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=jcKogSX6H29x04Y/ITjO1nU1cC3X886F9mfotzdA4xg=; b=ERU/JYGbUgwP/pe9b0YB6BRTr6nRk4AakSsQUnlYhANc0ReNUcLhMwRHkIl79QYtHP7B J4sjoQcc2mpfiYSwBK5yn15O2ceaQaJHFdsn2ydNfIxI5EqIAkECYgvPJt8uCzjRsaFIN VkX/HjLhxY/Admti6tQAWBsgLJXEBQ19k+Pxmm38QiJWfvzO/FIMiO+MUOwYoJy3xbqv7 adirFswISpTdvOzX185BG5VlVs5sjlNFAS/Sz6W7/63YtooKWZZQzTO2xdB6TmMUKqcjt mt135sugWIDkY4PteeUcrCGlqotPIHne7MJk4K0H2+4jkdf+/pfW+hSo+Ru1XsbjVeQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.remote-ip=66.220.155.178 Received: from 66-220-155-178.mail-mxout.facebook.com (66-220-155-178.mail-mxout.facebook.com [66.220.155.178]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EA6D606A6BC for ; Fri, 20 Nov 2026 21:50:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2EA6D606A6BC Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devvm50310.atn0.facebook.com (Postfix, from userid 128203) id 75C89594054C8; Fri, 20 Nov 2026 13:49:57 -0800 (PST) From: Yonghong Song To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , 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 Message-ID: <20261120214957.868409-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.53.1 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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.s= ong@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.s= ong@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.s= ong@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.s= ong@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_enu= mval___no_kflag.c --=20 2.53.1-Meta