Skip to content

Commit 770546a

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a849750 commit 770546a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,8 +1351,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
13511351
break;
13521352

13531353
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)) {
13561355
if (dst_reg != src_reg)
13571356
/* 32-bit mov */
13581357
emit_mov_reg(&prog, false, dst_reg, src_reg);

include/linux/filter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
228228
return insn->code == (BPF_ALU | BPF_MOV | BPF_X) && insn->imm == 1;
229229
}
230230

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+
231241
/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
232242
#define BPF_LD_IMM64(DST, IMM) \
233243
BPF_LD_IMM64_RAW(DST, 0, IMM)

0 commit comments

Comments
 (0)