Skip to content

Commit ed10405

Browse files
iii-iKernel Patches Daemon
authored andcommitted
s390/bpf: Centralize frame offset calculations
The calculation of the distance from %r15 to the caller-allocated portion of the stack frame is copy-pasted into multiple places in the JIT code. Move it to bpf_jit_prog() and save the result into bpf_jit::frame_off, so that the other parts of the JIT can use it. Signed-off-by: Ilya Leoshkevich <[email protected]>
1 parent 3ae697b commit ed10405

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

arch/s390/net/bpf_jit_comp.c

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct bpf_jit {
5454
int prologue_plt; /* Start of prologue hotpatch PLT */
5555
int kern_arena; /* Pool offset of kernel arena address */
5656
u64 user_arena; /* User arena address */
57+
u32 frame_off; /* Offset of frame from %r15 */
5758
};
5859

5960
#define SEEN_MEM BIT(0) /* use mem[] for temporary storage */
@@ -443,12 +444,9 @@ static void save_regs(struct bpf_jit *jit, u32 rs, u32 re)
443444
/*
444445
* Restore registers from "rs" (register start) to "re" (register end) on stack
445446
*/
446-
static void restore_regs(struct bpf_jit *jit, u32 rs, u32 re, u32 stack_depth)
447+
static void restore_regs(struct bpf_jit *jit, u32 rs, u32 re)
447448
{
448-
u32 off = STK_OFF_R6 + (rs - 6) * 8;
449-
450-
if (jit->seen & SEEN_STACK)
451-
off += STK_OFF + stack_depth;
449+
u32 off = jit->frame_off + STK_OFF_R6 + (rs - 6) * 8;
452450

453451
if (rs == re)
454452
/* lg %rs,off(%r15) */
@@ -492,8 +490,7 @@ static int get_end(u16 seen_regs, int start)
492490
* Save and restore clobbered registers (6-15) on stack.
493491
* We save/restore registers in chunks with gap >= 2 registers.
494492
*/
495-
static void save_restore_regs(struct bpf_jit *jit, int op, u32 stack_depth,
496-
u16 extra_regs)
493+
static void save_restore_regs(struct bpf_jit *jit, int op, u16 extra_regs)
497494
{
498495
u16 seen_regs = jit->seen_regs | extra_regs;
499496
const int last = 15, save_restore_size = 6;
@@ -516,7 +513,7 @@ static void save_restore_regs(struct bpf_jit *jit, int op, u32 stack_depth,
516513
if (op == REGS_SAVE)
517514
save_regs(jit, rs, re);
518515
else
519-
restore_regs(jit, rs, re, stack_depth);
516+
restore_regs(jit, rs, re);
520517
re++;
521518
} while (re <= last);
522519
}
@@ -575,8 +572,7 @@ static void bpf_jit_plt(struct bpf_plt *plt, void *ret, void *target)
575572
* Save registers and create stack frame if necessary.
576573
* See stack frame layout description in "bpf_jit.h"!
577574
*/
578-
static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp,
579-
u32 stack_depth)
575+
static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp)
580576
{
581577
/* No-op for hotpatching */
582578
/* brcl 0,prologue_plt */
@@ -609,7 +605,7 @@ static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp,
609605
jit->seen_regs |= NVREGS;
610606
} else {
611607
/* Save registers */
612-
save_restore_regs(jit, REGS_SAVE, stack_depth,
608+
save_restore_regs(jit, REGS_SAVE,
613609
fp->aux->exception_boundary ? NVREGS : 0);
614610
}
615611
/* Setup literal pool */
@@ -631,8 +627,8 @@ static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp,
631627
EMIT4(0xb9040000, REG_W1, REG_15);
632628
/* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
633629
EMIT4_DISP(0x41000000, BPF_REG_FP, REG_15, STK_160_UNUSED);
634-
/* aghi %r15,-STK_OFF */
635-
EMIT4_IMM(0xa70b0000, REG_15, -(STK_OFF + stack_depth));
630+
/* aghi %r15,-frame_off */
631+
EMIT4_IMM(0xa70b0000, REG_15, -jit->frame_off);
636632
/* stg %w1,152(%r15) (backchain) */
637633
EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0,
638634
REG_15, 152);
@@ -669,13 +665,13 @@ static void call_r1(struct bpf_jit *jit)
669665
/*
670666
* Function epilogue
671667
*/
672-
static void bpf_jit_epilogue(struct bpf_jit *jit, u32 stack_depth)
668+
static void bpf_jit_epilogue(struct bpf_jit *jit)
673669
{
674670
jit->exit_ip = jit->prg;
675671
/* Load exit code: lgr %r2,%b0 */
676672
EMIT4(0xb9040000, REG_2, BPF_REG_0);
677673
/* Restore registers */
678-
save_restore_regs(jit, REGS_RESTORE, stack_depth, 0);
674+
save_restore_regs(jit, REGS_RESTORE, 0);
679675
EMIT_JUMP_REG(14);
680676

681677
jit->prg = ALIGN(jit->prg, 8);
@@ -857,7 +853,7 @@ static int sign_extend(struct bpf_jit *jit, int r, u8 size, u8 flags)
857853
* stack space for the large switch statement.
858854
*/
859855
static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
860-
int i, bool extra_pass, u32 stack_depth)
856+
int i, bool extra_pass)
861857
{
862858
struct bpf_insn *insn = &fp->insnsi[i];
863859
s32 branch_oc_off = insn->off;
@@ -1778,9 +1774,9 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
17781774
* Note 2: We assume that the verifier does not let us call the
17791775
* main program, which clears the tail call counter on entry.
17801776
*/
1781-
/* mvc STK_OFF_TCCNT(4,%r15),N(%r15) */
1777+
/* mvc STK_OFF_TCCNT(4,%r15),frame_off+STK_OFF_TCCNT(%r15) */
17821778
_EMIT6(0xd203f000 | STK_OFF_TCCNT,
1783-
0xf000 | (STK_OFF_TCCNT + STK_OFF + stack_depth));
1779+
0xf000 | (jit->frame_off + STK_OFF_TCCNT));
17841780

17851781
/* Sign-extend the kfunc arguments. */
17861782
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
@@ -1831,10 +1827,7 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
18311827
* goto out;
18321828
*/
18331829

1834-
if (jit->seen & SEEN_STACK)
1835-
off = STK_OFF_TCCNT + STK_OFF + stack_depth;
1836-
else
1837-
off = STK_OFF_TCCNT;
1830+
off = jit->frame_off + STK_OFF_TCCNT;
18381831
/* lhi %w0,1 */
18391832
EMIT4_IMM(0xa7080000, REG_W0, 1);
18401833
/* laal %w1,%w0,off(%r15) */
@@ -1864,7 +1857,7 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
18641857
/*
18651858
* Restore registers before calling function
18661859
*/
1867-
save_restore_regs(jit, REGS_RESTORE, stack_depth, 0);
1860+
save_restore_regs(jit, REGS_RESTORE, 0);
18681861

18691862
/*
18701863
* goto *(prog->bpf_func + tail_call_start);
@@ -2157,7 +2150,7 @@ static int bpf_set_addr(struct bpf_jit *jit, int i)
21572150
* Compile eBPF program into s390x code
21582151
*/
21592152
static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp,
2160-
bool extra_pass, u32 stack_depth)
2153+
bool extra_pass)
21612154
{
21622155
int i, insn_count, lit32_size, lit64_size;
21632156
u64 kern_arena;
@@ -2166,24 +2159,28 @@ static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp,
21662159
jit->lit64 = jit->lit64_start;
21672160
jit->prg = 0;
21682161
jit->excnt = 0;
2162+
if (is_first_pass(jit) || (jit->seen & SEEN_STACK))
2163+
jit->frame_off = STK_OFF + round_up(fp->aux->stack_depth, 8);
2164+
else
2165+
jit->frame_off = 0;
21692166

21702167
kern_arena = bpf_arena_get_kern_vm_start(fp->aux->arena);
21712168
if (kern_arena)
21722169
jit->kern_arena = _EMIT_CONST_U64(kern_arena);
21732170
jit->user_arena = bpf_arena_get_user_vm_start(fp->aux->arena);
21742171

2175-
bpf_jit_prologue(jit, fp, stack_depth);
2172+
bpf_jit_prologue(jit, fp);
21762173
if (bpf_set_addr(jit, 0) < 0)
21772174
return -1;
21782175
for (i = 0; i < fp->len; i += insn_count) {
2179-
insn_count = bpf_jit_insn(jit, fp, i, extra_pass, stack_depth);
2176+
insn_count = bpf_jit_insn(jit, fp, i, extra_pass);
21802177
if (insn_count < 0)
21812178
return -1;
21822179
/* Next instruction address */
21832180
if (bpf_set_addr(jit, i + insn_count) < 0)
21842181
return -1;
21852182
}
2186-
bpf_jit_epilogue(jit, stack_depth);
2183+
bpf_jit_epilogue(jit);
21872184

21882185
lit32_size = jit->lit32 - jit->lit32_start;
21892186
lit64_size = jit->lit64 - jit->lit64_start;
@@ -2259,7 +2256,6 @@ static struct bpf_binary_header *bpf_jit_alloc(struct bpf_jit *jit,
22592256
*/
22602257
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
22612258
{
2262-
u32 stack_depth = round_up(fp->aux->stack_depth, 8);
22632259
struct bpf_prog *tmp, *orig_fp = fp;
22642260
struct bpf_binary_header *header;
22652261
struct s390_jit_data *jit_data;
@@ -2312,7 +2308,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
23122308
* - 3: Calculate program size and addrs array
23132309
*/
23142310
for (pass = 1; pass <= 3; pass++) {
2315-
if (bpf_jit_prog(&jit, fp, extra_pass, stack_depth)) {
2311+
if (bpf_jit_prog(&jit, fp, extra_pass)) {
23162312
fp = orig_fp;
23172313
goto free_addrs;
23182314
}
@@ -2326,7 +2322,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
23262322
goto free_addrs;
23272323
}
23282324
skip_init_ctx:
2329-
if (bpf_jit_prog(&jit, fp, extra_pass, stack_depth)) {
2325+
if (bpf_jit_prog(&jit, fp, extra_pass)) {
23302326
bpf_jit_binary_free(header);
23312327
fp = orig_fp;
23322328
goto free_addrs;

0 commit comments

Comments
 (0)