Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: implement insn_is_cast_user() helper for JITs

Implement a helper function to check if an instruction is
addr_space_cast from as(0) to as(1). Use this helper in the x86 JIT.

Other JITs can use this helper when they add support for this instruction.

Signed-off-by: Puranjay Mohan <puranjay12@gmail.com>
Link: https://lore.kernel.org/r/20240324183226.29674-1-puranjay12@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Puranjay Mohan and committed by
Alexei Starovoitov
770546ae a8497506

+11 -2
+1 -2
arch/x86/net/bpf_jit_comp.c
··· 1351 1351 break; 1352 1352 1353 1353 case BPF_ALU64 | BPF_MOV | BPF_X: 1354 - if (insn->off == BPF_ADDR_SPACE_CAST && 1355 - insn->imm == 1U << 16) { 1354 + if (insn_is_cast_user(insn)) { 1356 1355 if (dst_reg != src_reg) 1357 1356 /* 32-bit mov */ 1358 1357 emit_mov_reg(&prog, false, dst_reg, src_reg);
+10
include/linux/filter.h
··· 228 228 return insn->code == (BPF_ALU | BPF_MOV | BPF_X) && insn->imm == 1; 229 229 } 230 230 231 + /* addr_space_cast from as(0) to as(1) is for converting bpf arena pointers 232 + * to pointers in user vma. 233 + */ 234 + static inline bool insn_is_cast_user(const struct bpf_insn *insn) 235 + { 236 + return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && 237 + insn->off == BPF_ADDR_SPACE_CAST && 238 + insn->imm == 1U << 16; 239 + } 240 + 231 241 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ 232 242 #define BPF_LD_IMM64(DST, IMM) \ 233 243 BPF_LD_IMM64_RAW(DST, 0, IMM)