bpf: implement insn_is_cast_user() helper for JITs
authorPuranjay Mohan <puranjay12@gmail.com>
Sun, 24 Mar 2024 18:32:26 +0000 (18:32 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 25 Mar 2024 16:10:51 +0000 (09:10 -0700)
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>
arch/x86/net/bpf_jit_comp.c
include/linux/filter.h

index 4900b1ee019fd1c408c8bc5cf393d3a1342f0fcd..3b639d6f2f54d2b3ccb8c868d5ca8033a150bca9 100644 (file)
@@ -1351,8 +1351,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
                        break;
 
                case BPF_ALU64 | BPF_MOV | BPF_X:
-                       if (insn->off == BPF_ADDR_SPACE_CAST &&
-                           insn->imm == 1U << 16) {
+                       if (insn_is_cast_user(insn)) {
                                if (dst_reg != src_reg)
                                        /* 32-bit mov */
                                        emit_mov_reg(&prog, false, dst_reg, src_reg);
index c0d51bff8f9652683dfaf33452f8e81a674f08e9..44934b968b57d9793ed321eb66bc98a91f9ce19f 100644 (file)
@@ -228,6 +228,16 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
        return insn->code == (BPF_ALU | BPF_MOV | BPF_X) && insn->imm == 1;
 }
 
+/* addr_space_cast from as(0) to as(1) is for converting bpf arena pointers
+ * to pointers in user vma.
+ */
+static inline bool insn_is_cast_user(const struct bpf_insn *insn)
+{
+       return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) &&
+                             insn->off == BPF_ADDR_SPACE_CAST &&
+                             insn->imm == 1U << 16;
+}
+
 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
 #define BPF_LD_IMM64(DST, IMM)                                 \
        BPF_LD_IMM64_RAW(DST, 0, IMM)