Re: [PATCH bpf v2 2/2] selftests/bpf: Check per-CPU address resolution per register

[email protected]
Newsgroups org.kernel.vger.stable,org.kernel.vger.bpf
Message-ID <7276efcb565f80eed9a05dd364570b432a68ba5d980d9d1676bf620501f10040@mail.kernel.org>
> diff --git a/tools/testing/selftests/bpf/progs/verifier_percpu_addr.c b/tools/testing/selftests/bpf/progs/verifier_percpu_addr.c
> new file mode 100644
> index 000000000000..962faea8ef90
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/verifier_percpu_addr.c
> @@ -0,0 +1,114 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <vmlinux.h>
> +#include <bpf/bpf_helpers.h>
> +#include "bpf_misc.h"
> +
> +int percpu_data SEC(".percpu");
> +
> +#if defined(__TARGET_ARCH_x86)
> +
> +/*
> + * An ld_imm64 of a per-CPU map value is followed by a mov_percpu_addr that
> + * reuses the same register, so the register the address lands in decides how
> + * the JIT encodes the add. On x86 R5, R7, R8 and R9 are the extended
> + * registers, whose high bit needs REX.R because the destination sits in
> + * ModRM.reg. Check one program per extended register, since getting the
> + * prefix wrong resolves the address into whichever register shares the low
> + * three bits instead.
> + */
> +
> +SEC("raw_tp")
> +__description("per-CPU address into r5")
> +__success
> +__arch_x86_64
> +__jited("	addq	%gs:{{.*}}, %r8")
> +__naked void percpu_addr_into_r5(void)
> +{
> +	asm volatile ("					\
> +	r5 = %[percpu_data] ll;				\
> +	r0 = *(u32 *)(r5 + 0);				\
> +	exit;						\
> +"	:
> +	: __imm_addr(percpu_data)
> +	: __clobber_all);
> +}

This isn't a bug, but would it be tidier to move the percpu_data
declaration inside the __TARGET_ARCH_x86 block?

Currently percpu_data is declared above the arch guard, but its only
readers (percpu_addr_into_r1/r5/r7/r8/r9) are inside the guard:

+int percpu_data SEC(".percpu");
+
+#if defined(__TARGET_ARCH_x86)
+
+/* ... test functions using percpu_data ... */

On every other target the object still carries a .percpu section, so
libbpf still creates an unused percpu data map for it.

Comparable files in the same directory keep objects used only by guarded
programs inside the guard. verifier_private_stack.c declares its 'struct
elem' and 'array SEC(".maps")' after the '#if defined(__TARGET_ARCH_x86)
|| defined(__TARGET_ARCH_arm64)' guard, and verifier_ldsx.c declares its
'arena SEC(".maps")' map inside its arch guard.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31839526403
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.