Skip to content

Commit 144cd91

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: move tmp variable into ax register in interpreter
This change moves the on-stack 64 bit tmp variable in ___bpf_prog_run() into the hidden ax register. The latter is currently only used in JITs for constant blinding as a temporary scratch register, meaning the BPF interpreter will never see the use of ax. Therefore it is safe to use it for the cases where tmp has been used earlier. This is needed to later on allow restricted hidden use of ax in both interpreter and JITs. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent c08435e commit 144cd91

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ struct sock_reuseport;
6060
* constants. See JIT pre-step in bpf_jit_blind_constants().
6161
*/
6262
#define BPF_REG_AX MAX_BPF_REG
63-
#define MAX_BPF_JIT_REG (MAX_BPF_REG + 1)
63+
#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)
64+
#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG
6465

6566
/* unused opcode to mark special call to bpf_tail_call() helper */
6667
#define BPF_TAIL_CALL 0xf0

‎kernel/bpf/core.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#define DST regs[insn->dst_reg]
5555
#define SRC regs[insn->src_reg]
5656
#define FP regs[BPF_REG_FP]
57+
#define AX regs[BPF_REG_AX]
5758
#define ARG1 regs[BPF_REG_ARG1]
5859
#define CTX regs[BPF_REG_CTX]
5960
#define IMM insn->imm
@@ -1188,7 +1189,6 @@ bool bpf_opcode_in_insntable(u8 code)
11881189
*/
11891190
static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
11901191
{
1191-
u64 tmp;
11921192
#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
11931193
#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
11941194
static const void *jumptable[256] = {
@@ -1268,36 +1268,36 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
12681268
(*(s64 *) &DST) >>= IMM;
12691269
CONT;
12701270
ALU64_MOD_X:
1271-
div64_u64_rem(DST, SRC, &tmp);
1272-
DST = tmp;
1271+
div64_u64_rem(DST, SRC, &AX);
1272+
DST = AX;
12731273
CONT;
12741274
ALU_MOD_X:
1275-
tmp = (u32) DST;
1276-
DST = do_div(tmp, (u32) SRC);
1275+
AX = (u32) DST;
1276+
DST = do_div(AX, (u32) SRC);
12771277
CONT;
12781278
ALU64_MOD_K:
1279-
div64_u64_rem(DST, IMM, &tmp);
1280-
DST = tmp;
1279+
div64_u64_rem(DST, IMM, &AX);
1280+
DST = AX;
12811281
CONT;
12821282
ALU_MOD_K:
1283-
tmp = (u32) DST;
1284-
DST = do_div(tmp, (u32) IMM);
1283+
AX = (u32) DST;
1284+
DST = do_div(AX, (u32) IMM);
12851285
CONT;
12861286
ALU64_DIV_X:
12871287
DST = div64_u64(DST, SRC);
12881288
CONT;
12891289
ALU_DIV_X:
1290-
tmp = (u32) DST;
1291-
do_div(tmp, (u32) SRC);
1292-
DST = (u32) tmp;
1290+
AX = (u32) DST;
1291+
do_div(AX, (u32) SRC);
1292+
DST = (u32) AX;
12931293
CONT;
12941294
ALU64_DIV_K:
12951295
DST = div64_u64(DST, IMM);
12961296
CONT;
12971297
ALU_DIV_K:
1298-
tmp = (u32) DST;
1299-
do_div(tmp, (u32) IMM);
1300-
DST = (u32) tmp;
1298+
AX = (u32) DST;
1299+
do_div(AX, (u32) IMM);
1300+
DST = (u32) AX;
13011301
CONT;
13021302
ALU_END_TO_BE:
13031303
switch (IMM) {
@@ -1553,7 +1553,7 @@ STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
15531553
static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
15541554
{ \
15551555
u64 stack[stack_size / sizeof(u64)]; \
1556-
u64 regs[MAX_BPF_REG]; \
1556+
u64 regs[MAX_BPF_EXT_REG]; \
15571557
\
15581558
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
15591559
ARG1 = (u64) (unsigned long) ctx; \
@@ -1566,7 +1566,7 @@ static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
15661566
const struct bpf_insn *insn) \
15671567
{ \
15681568
u64 stack[stack_size / sizeof(u64)]; \
1569-
u64 regs[MAX_BPF_REG]; \
1569+
u64 regs[MAX_BPF_EXT_REG]; \
15701570
\
15711571
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
15721572
BPF_R1 = r1; \

0 commit comments

Comments
 (0)