Skip to content

Commit fb40c9d

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-jit-cleanups'
Daniel Borkmann says: ==================== This series follows up mostly with with some minor cleanups on top of 'Move ld_abs/ld_ind to native BPF' as well as implements better 32/64 bit immediate load into register and saves tail call init on cBPF for the arm64 JIT. Last but not least we add a couple of test cases. For details please see individual patches. Thanks! v1 -> v2: - Minor fix in i64_i16_blocks() to remove 24 shift. - Added last two patches. - Added Acks from prior round. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 53ea24c + a82d8cd commit fb40c9d

7 files changed

Lines changed: 228 additions & 98 deletions

File tree

‎arch/arm/net/bpf_jit_32.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,11 @@ static void jit_fill_hole(void *area, unsigned int size)
234234
#define SCRATCH_SIZE 80
235235

236236
/* total stack size used in JITed code */
237-
#define _STACK_SIZE \
238-
(ctx->prog->aux->stack_depth + \
239-
+ SCRATCH_SIZE + \
240-
+ 4 /* extra for skb_copy_bits buffer */)
241-
242-
#define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
237+
#define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
238+
#define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
243239

244240
/* Get the offset of eBPF REGISTERs stored on scratch space. */
245-
#define STACK_VAR(off) (STACK_SIZE-off-4)
246-
247-
/* Offset of skb_copy_bits buffer */
248-
#define SKB_BUFFER STACK_VAR(SCRATCH_SIZE)
241+
#define STACK_VAR(off) (STACK_SIZE - off)
249242

250243
#if __LINUX_ARM_ARCH__ < 7
251244

‎arch/arm64/net/bpf_jit_comp.c‎

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <linux/bpf.h>
2222
#include <linux/filter.h>
2323
#include <linux/printk.h>
24-
#include <linux/skbuff.h>
2524
#include <linux/slab.h>
2625

2726
#include <asm/byteorder.h>
@@ -80,23 +79,66 @@ static inline void emit(const u32 insn, struct jit_ctx *ctx)
8079
ctx->idx++;
8180
}
8281

82+
static inline void emit_a64_mov_i(const int is64, const int reg,
83+
const s32 val, struct jit_ctx *ctx)
84+
{
85+
u16 hi = val >> 16;
86+
u16 lo = val & 0xffff;
87+
88+
if (hi & 0x8000) {
89+
if (hi == 0xffff) {
90+
emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
91+
} else {
92+
emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
93+
if (lo != 0xffff)
94+
emit(A64_MOVK(is64, reg, lo, 0), ctx);
95+
}
96+
} else {
97+
emit(A64_MOVZ(is64, reg, lo, 0), ctx);
98+
if (hi)
99+
emit(A64_MOVK(is64, reg, hi, 16), ctx);
100+
}
101+
}
102+
103+
static int i64_i16_blocks(const u64 val, bool inverse)
104+
{
105+
return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
106+
(((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
107+
(((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
108+
(((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
109+
}
110+
83111
static inline void emit_a64_mov_i64(const int reg, const u64 val,
84112
struct jit_ctx *ctx)
85113
{
86-
u64 tmp = val;
87-
int shift = 0;
88-
89-
emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
90-
tmp >>= 16;
91-
shift += 16;
92-
while (tmp) {
93-
if (tmp & 0xffff)
94-
emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
95-
tmp >>= 16;
96-
shift += 16;
114+
u64 nrm_tmp = val, rev_tmp = ~val;
115+
bool inverse;
116+
int shift;
117+
118+
if (!(nrm_tmp >> 32))
119+
return emit_a64_mov_i(0, reg, (u32)val, ctx);
120+
121+
inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
122+
shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
123+
(fls64(nrm_tmp) - 1)), 16), 0);
124+
if (inverse)
125+
emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
126+
else
127+
emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
128+
shift -= 16;
129+
while (shift >= 0) {
130+
if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
131+
emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
132+
shift -= 16;
97133
}
98134
}
99135

136+
/*
137+
* This is an unoptimized 64 immediate emission used for BPF to BPF call
138+
* addresses. It will always do a full 64 bit decomposition as otherwise
139+
* more complexity in the last extra pass is required since we previously
140+
* reserved 4 instructions for the address.
141+
*/
100142
static inline void emit_addr_mov_i64(const int reg, const u64 val,
101143
struct jit_ctx *ctx)
102144
{
@@ -111,26 +153,6 @@ static inline void emit_addr_mov_i64(const int reg, const u64 val,
111153
}
112154
}
113155

114-
static inline void emit_a64_mov_i(const int is64, const int reg,
115-
const s32 val, struct jit_ctx *ctx)
116-
{
117-
u16 hi = val >> 16;
118-
u16 lo = val & 0xffff;
119-
120-
if (hi & 0x8000) {
121-
if (hi == 0xffff) {
122-
emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
123-
} else {
124-
emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
125-
emit(A64_MOVK(is64, reg, lo, 0), ctx);
126-
}
127-
} else {
128-
emit(A64_MOVZ(is64, reg, lo, 0), ctx);
129-
if (hi)
130-
emit(A64_MOVK(is64, reg, hi, 16), ctx);
131-
}
132-
}
133-
134156
static inline int bpf2a64_offset(int bpf_to, int bpf_from,
135157
const struct jit_ctx *ctx)
136158
{
@@ -163,7 +185,7 @@ static inline int epilogue_offset(const struct jit_ctx *ctx)
163185
/* Tail call offset to jump into */
164186
#define PROLOGUE_OFFSET 7
165187

166-
static int build_prologue(struct jit_ctx *ctx)
188+
static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
167189
{
168190
const struct bpf_prog *prog = ctx->prog;
169191
const u8 r6 = bpf2a64[BPF_REG_6];
@@ -188,7 +210,7 @@ static int build_prologue(struct jit_ctx *ctx)
188210
* | ... | BPF prog stack
189211
* | |
190212
* +-----+ <= (BPF_FP - prog->aux->stack_depth)
191-
* |RSVD | JIT scratchpad
213+
* |RSVD | padding
192214
* current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
193215
* | |
194216
* | ... | Function call stack
@@ -210,19 +232,19 @@ static int build_prologue(struct jit_ctx *ctx)
210232
/* Set up BPF prog stack base register */
211233
emit(A64_MOV(1, fp, A64_SP), ctx);
212234

213-
/* Initialize tail_call_cnt */
214-
emit(A64_MOVZ(1, tcc, 0, 0), ctx);
235+
if (!ebpf_from_cbpf) {
236+
/* Initialize tail_call_cnt */
237+
emit(A64_MOVZ(1, tcc, 0, 0), ctx);
215238

216-
cur_offset = ctx->idx - idx0;
217-
if (cur_offset != PROLOGUE_OFFSET) {
218-
pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
219-
cur_offset, PROLOGUE_OFFSET);
220-
return -1;
239+
cur_offset = ctx->idx - idx0;
240+
if (cur_offset != PROLOGUE_OFFSET) {
241+
pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
242+
cur_offset, PROLOGUE_OFFSET);
243+
return -1;
244+
}
221245
}
222246

223-
/* 4 byte extra for skb_copy_bits buffer */
224-
ctx->stack_size = prog->aux->stack_depth + 4;
225-
ctx->stack_size = STACK_ALIGN(ctx->stack_size);
247+
ctx->stack_size = STACK_ALIGN(prog->aux->stack_depth);
226248

227249
/* Set up function call stack */
228250
emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
@@ -786,6 +808,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
786808
struct bpf_prog *tmp, *orig_prog = prog;
787809
struct bpf_binary_header *header;
788810
struct arm64_jit_data *jit_data;
811+
bool was_classic = bpf_prog_was_classic(prog);
789812
bool tmp_blinded = false;
790813
bool extra_pass = false;
791814
struct jit_ctx ctx;
@@ -840,7 +863,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
840863
goto out_off;
841864
}
842865

843-
if (build_prologue(&ctx)) {
866+
if (build_prologue(&ctx, was_classic)) {
844867
prog = orig_prog;
845868
goto out_off;
846869
}
@@ -863,7 +886,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
863886
skip_init_ctx:
864887
ctx.idx = 0;
865888

866-
build_prologue(&ctx);
889+
build_prologue(&ctx, was_classic);
867890

868891
if (build_body(&ctx)) {
869892
bpf_jit_binary_free(header);

‎arch/mips/net/ebpf_jit.c‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ enum reg_val_type {
9595
* struct jit_ctx - JIT context
9696
* @skf: The sk_filter
9797
* @stack_size: eBPF stack size
98-
* @tmp_offset: eBPF $sp offset to 8-byte temporary memory
9998
* @idx: Instruction index
10099
* @flags: JIT flags
101100
* @offsets: Instruction offsets
@@ -105,7 +104,6 @@ enum reg_val_type {
105104
struct jit_ctx {
106105
const struct bpf_prog *skf;
107106
int stack_size;
108-
int tmp_offset;
109107
u32 idx;
110108
u32 flags;
111109
u32 *offsets;
@@ -293,7 +291,6 @@ static int gen_int_prologue(struct jit_ctx *ctx)
293291
locals_size = (ctx->flags & EBPF_SEEN_FP) ? MAX_BPF_STACK : 0;
294292

295293
stack_adjust += locals_size;
296-
ctx->tmp_offset = locals_size;
297294

298295
ctx->stack_size = stack_adjust;
299296

@@ -399,7 +396,6 @@ static void gen_imm_to_reg(const struct bpf_insn *insn, int reg,
399396
emit_instr(ctx, lui, reg, upper >> 16);
400397
emit_instr(ctx, addiu, reg, reg, lower);
401398
}
402-
403399
}
404400

405401
static int gen_imm_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
@@ -547,28 +543,6 @@ static int gen_imm_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
547543
return 0;
548544
}
549545

550-
static void * __must_check
551-
ool_skb_header_pointer(const struct sk_buff *skb, int offset,
552-
int len, void *buffer)
553-
{
554-
return skb_header_pointer(skb, offset, len, buffer);
555-
}
556-
557-
static int size_to_len(const struct bpf_insn *insn)
558-
{
559-
switch (BPF_SIZE(insn->code)) {
560-
case BPF_B:
561-
return 1;
562-
case BPF_H:
563-
return 2;
564-
case BPF_W:
565-
return 4;
566-
case BPF_DW:
567-
return 8;
568-
}
569-
return 0;
570-
}
571-
572546
static void emit_const_to_reg(struct jit_ctx *ctx, int dst, u64 value)
573547
{
574548
if (value >= 0xffffffffffff8000ull || value < 0x8000ull) {

‎arch/sparc/net/bpf_jit_comp_64.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
894894
const int i = insn - ctx->prog->insnsi;
895895
const s16 off = insn->off;
896896
const s32 imm = insn->imm;
897-
u32 *func;
898897

899898
if (insn->src_reg == BPF_REG_FP)
900899
ctx->saw_frame_pointer = true;

‎arch/x86/include/asm/nospec-branch.h‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ do { \
301301
* jmp *%edx for x86_32
302302
*/
303303
#ifdef CONFIG_RETPOLINE
304-
#ifdef CONFIG_X86_64
305-
# define RETPOLINE_RAX_BPF_JIT_SIZE 17
306-
# define RETPOLINE_RAX_BPF_JIT() \
304+
# ifdef CONFIG_X86_64
305+
# define RETPOLINE_RAX_BPF_JIT_SIZE 17
306+
# define RETPOLINE_RAX_BPF_JIT() \
307307
do { \
308308
EMIT1_off32(0xE8, 7); /* callq do_rop */ \
309309
/* spec_trap: */ \
@@ -314,8 +314,8 @@ do { \
314314
EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
315315
EMIT1(0xC3); /* retq */ \
316316
} while (0)
317-
#else
318-
# define RETPOLINE_EDX_BPF_JIT() \
317+
# else /* !CONFIG_X86_64 */
318+
# define RETPOLINE_EDX_BPF_JIT() \
319319
do { \
320320
EMIT1_off32(0xE8, 7); /* call do_rop */ \
321321
/* spec_trap: */ \
@@ -326,17 +326,16 @@ do { \
326326
EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
327327
EMIT1(0xC3); /* ret */ \
328328
} while (0)
329-
#endif
329+
# endif
330330
#else /* !CONFIG_RETPOLINE */
331-
332-
#ifdef CONFIG_X86_64
333-
# define RETPOLINE_RAX_BPF_JIT_SIZE 2
334-
# define RETPOLINE_RAX_BPF_JIT() \
335-
EMIT2(0xFF, 0xE0); /* jmp *%rax */
336-
#else
337-
# define RETPOLINE_EDX_BPF_JIT() \
338-
EMIT2(0xFF, 0xE2) /* jmp *%edx */
339-
#endif
331+
# ifdef CONFIG_X86_64
332+
# define RETPOLINE_RAX_BPF_JIT_SIZE 2
333+
# define RETPOLINE_RAX_BPF_JIT() \
334+
EMIT2(0xFF, 0xE0); /* jmp *%rax */
335+
# else /* !CONFIG_X86_64 */
336+
# define RETPOLINE_EDX_BPF_JIT() \
337+
EMIT2(0xFF, 0xE2) /* jmp *%edx */
338+
# endif
340339
#endif
341340

342341
#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */

0 commit comments

Comments
 (0)