Skip to content

Commit f1c9eed

Browse files
borkmanndavem330
authored andcommitted
bpf, arm64: take advantage of stack_depth tracking
Make use of recently implemented stack_depth tracking for arm64 JIT, so that stack usage can be reduced heavily for programs not using tail calls at least. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 66e037c commit f1c9eed

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct jit_ctx {
6969
int epilogue_offset;
7070
int *offset;
7171
u32 *image;
72+
u32 stack_size;
7273
};
7374

7475
static inline void emit(const u32 insn, struct jit_ctx *ctx)
@@ -145,16 +146,11 @@ static inline int epilogue_offset(const struct jit_ctx *ctx)
145146
/* Stack must be multiples of 16B */
146147
#define STACK_ALIGN(sz) (((sz) + 15) & ~15)
147148

148-
#define _STACK_SIZE \
149-
(MAX_BPF_STACK \
150-
+ 4 /* extra for skb_copy_bits buffer */)
151-
152-
#define STACK_SIZE STACK_ALIGN(_STACK_SIZE)
153-
154149
#define PROLOGUE_OFFSET 8
155150

156151
static int build_prologue(struct jit_ctx *ctx)
157152
{
153+
const struct bpf_prog *prog = ctx->prog;
158154
const u8 r6 = bpf2a64[BPF_REG_6];
159155
const u8 r7 = bpf2a64[BPF_REG_7];
160156
const u8 r8 = bpf2a64[BPF_REG_8];
@@ -176,9 +172,9 @@ static int build_prologue(struct jit_ctx *ctx)
176172
* | |
177173
* | ... | BPF prog stack
178174
* | |
179-
* +-----+ <= (BPF_FP - MAX_BPF_STACK)
175+
* +-----+ <= (BPF_FP - prog->aux->stack_depth)
180176
* |RSVD | JIT scratchpad
181-
* current A64_SP => +-----+ <= (BPF_FP - STACK_SIZE)
177+
* current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
182178
* | |
183179
* | ... | Function call stack
184180
* | |
@@ -202,8 +198,12 @@ static int build_prologue(struct jit_ctx *ctx)
202198
/* Initialize tail_call_cnt */
203199
emit(A64_MOVZ(1, tcc, 0, 0), ctx);
204200

201+
/* 4 byte extra for skb_copy_bits buffer */
202+
ctx->stack_size = prog->aux->stack_depth + 4;
203+
ctx->stack_size = STACK_ALIGN(ctx->stack_size);
204+
205205
/* Set up function call stack */
206-
emit(A64_SUB_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
206+
emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
207207

208208
cur_offset = ctx->idx - idx0;
209209
if (cur_offset != PROLOGUE_OFFSET) {
@@ -288,7 +288,7 @@ static void build_epilogue(struct jit_ctx *ctx)
288288
const u8 fp = bpf2a64[BPF_REG_FP];
289289

290290
/* We're done with BPF stack */
291-
emit(A64_ADD_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
291+
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
292292

293293
/* Restore fs (x25) and x26 */
294294
emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
@@ -732,7 +732,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
732732
return -EINVAL;
733733
}
734734
emit_a64_mov_i64(r3, size, ctx);
735-
emit(A64_SUB_I(1, r4, fp, STACK_SIZE), ctx);
735+
emit(A64_SUB_I(1, r4, fp, ctx->stack_size), ctx);
736736
emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
737737
emit(A64_BLR(r5), ctx);
738738
emit(A64_MOV(1, r0, A64_R(0)), ctx);

0 commit comments

Comments
 (0)