Skip to content

Commit b855e31

Browse files
borkmanngregkh
authored andcommitted
bpf: move tmp variable into ax register in interpreter
[ commit 144cd91 upstream ] 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> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 333a31c commit b855e31

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
@@ -52,6 +52,7 @@
5252
#define DST regs[insn->dst_reg]
5353
#define SRC regs[insn->src_reg]
5454
#define FP regs[BPF_REG_FP]
55+
#define AX regs[BPF_REG_AX]
5556
#define ARG1 regs[BPF_REG_ARG1]
5657
#define CTX regs[BPF_REG_CTX]
5758
#define IMM insn->imm
@@ -971,7 +972,6 @@ bool bpf_opcode_in_insntable(u8 code)
971972
*/
972973
static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
973974
{
974-
u64 tmp;
975975
#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
976976
#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
977977
static const void *jumptable[256] = {
@@ -1045,36 +1045,36 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
10451045
(*(s64 *) &DST) >>= IMM;
10461046
CONT;
10471047
ALU64_MOD_X:
1048-
div64_u64_rem(DST, SRC, &tmp);
1049-
DST = tmp;
1048+
div64_u64_rem(DST, SRC, &AX);
1049+
DST = AX;
10501050
CONT;
10511051
ALU_MOD_X:
1052-
tmp = (u32) DST;
1053-
DST = do_div(tmp, (u32) SRC);
1052+
AX = (u32) DST;
1053+
DST = do_div(AX, (u32) SRC);
10541054
CONT;
10551055
ALU64_MOD_K:
1056-
div64_u64_rem(DST, IMM, &tmp);
1057-
DST = tmp;
1056+
div64_u64_rem(DST, IMM, &AX);
1057+
DST = AX;
10581058
CONT;
10591059
ALU_MOD_K:
1060-
tmp = (u32) DST;
1061-
DST = do_div(tmp, (u32) IMM);
1060+
AX = (u32) DST;
1061+
DST = do_div(AX, (u32) IMM);
10621062
CONT;
10631063
ALU64_DIV_X:
10641064
DST = div64_u64(DST, SRC);
10651065
CONT;
10661066
ALU_DIV_X:
1067-
tmp = (u32) DST;
1068-
do_div(tmp, (u32) SRC);
1069-
DST = (u32) tmp;
1067+
AX = (u32) DST;
1068+
do_div(AX, (u32) SRC);
1069+
DST = (u32) AX;
10701070
CONT;
10711071
ALU64_DIV_K:
10721072
DST = div64_u64(DST, IMM);
10731073
CONT;
10741074
ALU_DIV_K:
1075-
tmp = (u32) DST;
1076-
do_div(tmp, (u32) IMM);
1077-
DST = (u32) tmp;
1075+
AX = (u32) DST;
1076+
do_div(AX, (u32) IMM);
1077+
DST = (u32) AX;
10781078
CONT;
10791079
ALU_END_TO_BE:
10801080
switch (IMM) {
@@ -1330,7 +1330,7 @@ STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
13301330
static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
13311331
{ \
13321332
u64 stack[stack_size / sizeof(u64)]; \
1333-
u64 regs[MAX_BPF_REG]; \
1333+
u64 regs[MAX_BPF_EXT_REG]; \
13341334
\
13351335
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
13361336
ARG1 = (u64) (unsigned long) ctx; \
@@ -1343,7 +1343,7 @@ static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
13431343
const struct bpf_insn *insn) \
13441344
{ \
13451345
u64 stack[stack_size / sizeof(u64)]; \
1346-
u64 regs[MAX_BPF_REG]; \
1346+
u64 regs[MAX_BPF_EXT_REG]; \
13471347
\
13481348
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
13491349
BPF_R1 = r1; \

0 commit comments

Comments
 (0)