The BPF_MOV64_PERCPU_REG insn requires JIT to emit native code to for
'dst_reg = src_reg + <percpu_base_off>'.

However, the interpreter ignores the 'off' at its ALU64_MOV_X label.
The 'off' indicates the insn is BPF_MOV64_PERCPU_REG insn. Then, when
the interpreter loads memory from the register, it will hit a page
fault.

[    2.545572] BUG: unable to handle page fault for address: ffffffffacaaf034
[    2.546485] #PF: supervisor read access in kernel mode
[    2.547167] #PF: error_code(0x0000) - not-present page
[    2.547850] PGD 134e63067 P4D 134e63067 PUD 134e64063 PMD 10021c063 PTE 
800ffffeca550062
[    2.548912] Oops: Oops: 0000 [#1] SMP PTI

Set jit_required as true in order to disallow interpreter fallback in
core.c::__bpf_prog_select_runtime(), if any BPF_ADDR_PERCPU insn is
patched to the prog.

BTW, rename the helper bpf_map_supports_cpu_flags() to
bpf_map_is_percpu_map().

Fixes: 7bdbf7446305 ("bpf: add special internal-only MOV instruction to resolve 
per-CPU addrs")
Signed-off-by: Leon Hwang <[email protected]>
---
 include/linux/bpf.h | 4 ++--
 kernel/bpf/fixups.c | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 31181e0c2b80..d9542127dfdf 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4163,7 +4163,7 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 
*offsets, void *image)
 }
 #endif
 
-static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type)
+static inline bool bpf_map_is_percpu_map(enum bpf_map_type map_type)
 {
        switch (map_type) {
        case BPF_MAP_TYPE_PERCPU_ARRAY:
@@ -4190,7 +4190,7 @@ static inline int bpf_map_check_op_flags(struct bpf_map 
*map, u64 flags, u64 all
                return -EINVAL;
 
        if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) {
-               if (!bpf_map_supports_cpu_flags(map->map_type))
+               if (!bpf_map_is_percpu_map(map->map_type))
                        return -EINVAL;
                if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS))
                        return -EINVAL;
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index d3be972714b2..a0bddada7964 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2008,6 +2008,9 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
                                        return -EFAULT;
                                }
 
+                               if (bpf_map_is_percpu_map(map_ptr->map_type))
+                                       prog->jit_required = true;
+
                                new_prog = bpf_patch_insn_data(env, i + delta,
                                                               insn_buf, cnt);
                                if (!new_prog)
@@ -2112,6 +2115,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
                         * way, it's fine to back out this inlining logic
                         */
 #ifdef CONFIG_SMP
+                       prog->jit_required = true;
                        insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned 
long)&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);
@@ -2133,6 +2137,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
                /* Implement bpf_get_current_task() and 
bpf_get_current_task_btf() inline. */
                if ((insn->imm == BPF_FUNC_get_current_task || insn->imm == 
BPF_FUNC_get_current_task_btf) &&
                    bpf_verifier_inlines_helper_call(env, insn->imm)) {
+                       prog->jit_required = true;
                        insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned 
long)&current_task);
                        insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, 
BPF_REG_0);
                        insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 
0);
-- 
2.55.0


Reply via email to