Skip to contentSkip to conversation

x86/bpf: Fix BPF percpu accesses

PATCH Applied Brian Gerst 3 participants 3 messages Feb 27, 2025 – Feb 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/bpf: Fix BPF percpu accesses

Due to commit 9d7de2aa8b41 ("Use relative percpu offsets"), percpu addresses went from positive offsets from the GSBASE to negative kernel virtual addresses. The BPF verifier has an optimization for x86-64 that loads the address of cpu_number into a register, but was only doing a 32-bit load which truncates negative addresses. Change it to a 64-bit load so that the address is properly sign-extended.

Fixes 9d7de2aa8b41 ("Use relative percpu offsets")
Cc Andrii Nakryiko <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index 60611df77957..f4859516b190 100644
@@ -21707,7 +21707,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			 * way, it's fine to back out this inlining logic
 			 */
 #ifdef CONFIG_SMP
-			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
+			insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
 			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
 			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
 			cnt = 3;
Signature · 1 line
2.48.1
merge bot Applied
#1 lore raw
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     18cdd90aba794333f4c6dce39f5c3fe642af5575
Gitweb:        https://git.kernel.org/tip/18cdd90aba794333f4c6dce39f5c3fe642af5575
Author:        Brian Gerst <[email protected]>
AuthorDate:    Thu, 27 Feb 2025 14:53:02 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 27 Feb 2025 21:10:03 +01:00

x86/bpf: Fix BPF percpu accesses

Due to this recent commit in the x86 tree:

9d7de2aa8b41 ("Use relative percpu offsets")

percpu addresses went from positive offsets from the GSBASE to negative kernel virtual addresses. The BPF verifier has an optimization for x86-64 that loads the address of cpu_number into a register, but was only doing a 32-bit load which truncates negative addresses.

Change it to a 64-bit load so that the address is properly sign-extended.

Fixes 9d7de2aa8b41 ("Use relative percpu offsets")
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Uros Bizjak <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index 9971c03..f74263b 100644
@@ -21692,7 +21692,7 @@ patch_map_ops_generic:
 			 * way, it's fine to back out this inlining logic
 			 */
 #ifdef CONFIG_SMP
-			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
+			insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
 			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
 			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
 			cnt = 3;
#2 lore raw
In reply to Brian Gerst
Quoted 21 lines · Brian Gerst

Due to commit 9d7de2aa8b41 ("Use relative percpu offsets"), percpu addresses went from positive offsets from the GSBASE to negative kernel virtual addresses. The BPF verifier has an optimization for x86-64 that loads the address of cpu_number into a register, but was only doing a 32-bit load which truncates negative addresses. Change it to a 64-bit load so that the address is properly sign-extended.

Fixes 9d7de2aa8b41 ("Use relative percpu offsets")
Cc Andrii Nakryiko <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index 60611df77957..f4859516b190 100644

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 21710

Quoted 5 lines
kernel/bpf/verifier.c +1 -1
@@ -21707,7 +21707,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
                         * way, it's fine to back out this inlining logic
                         */
 #ifdef CONFIG_SMP
-                       insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
+                       insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);

wouldn't you need to at least drop the (u32) cast? Otherwise what's really the point?

Quoted 6 lines
kernel/bpf/verifier.c +0 -1
                        insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
                        insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
                        cnt = 3;
--

2.48.1