Skip to content

Commit 89f9290

Browse files
iii-iKernel Patches Daemon
authored andcommitted
s390/bpf: Use direct calls and jumps where possible
After the V!=R rework (commit c98d2ec ("s390/mm: Uncouple physical vs virtual address spaces")), all kernel code and related data are allocated within a 4G region, making it possible to use relative addressing in BPF code more extensively. Convert as many indirect calls and jumps to direct calls as possible, namely: * BPF_CALL * __bpf_tramp_enter() * __bpf_tramp_exit() * __bpf_prog_enter() * __bpf_prog_exit() * fentry * fmod_ret * fexit * BPF_TRAMP_F_CALL_ORIG without BPF_TRAMP_F_ORIG_STACK * Trampoline returns without BPF_TRAMP_F_SKIP_FRAME and BPF_TRAMP_F_ORIG_STACK The following indirect calls and jumps remain: * Prog returns * Trampoline returns with BPF_TRAMP_F_SKIP_FRAME or BPF_TRAMP_F_ORIG_STACK * BPF_TAIL_CALL * BPF_TRAMP_F_CALL_ORIG with BPF_TRAMP_F_ORIG_STACK As a result, only one usage of call_r1() remains, so inline it. Signed-off-by: Ilya Leoshkevich <[email protected]>
1 parent 1a79214 commit 89f9290

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

arch/s390/net/bpf_jit_comp.c

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,6 @@ static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp)
674674
_EMIT2(0x07f0 | reg); \
675675
} while (0)
676676

677-
/*
678-
* Call r1 either directly or via __s390_indirect_jump_r1 thunk
679-
*/
680-
static void call_r1(struct bpf_jit *jit)
681-
{
682-
if (nospec_uses_trampoline())
683-
/* brasl %r14,__s390_indirect_jump_r1 */
684-
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14,
685-
__s390_indirect_jump_r1);
686-
else
687-
/* basr %r14,%r1 */
688-
EMIT2(0x0d00, REG_14, REG_1);
689-
}
690-
691677
/*
692678
* Function epilogue
693679
*/
@@ -1820,10 +1806,8 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
18201806
}
18211807
}
18221808

1823-
/* lgrl %w1,func */
1824-
EMIT6_PCREL_RILB(0xc4080000, REG_W1, _EMIT_CONST_U64(func));
1825-
/* %r1() */
1826-
call_r1(jit);
1809+
/* brasl %r14,func */
1810+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, (void *)func);
18271811
/* lgr %b0,%r2: load return value into %b0 */
18281812
EMIT4(0xb9040000, BPF_REG_0, REG_2);
18291813

@@ -2534,14 +2518,12 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
25342518
* goto skip;
25352519
*/
25362520

2537-
/* %r1 = __bpf_prog_enter */
2538-
load_imm64(jit, REG_1, (u64)bpf_trampoline_enter(p));
25392521
/* %r2 = p */
25402522
load_imm64(jit, REG_2, (u64)p);
25412523
/* la %r3,run_ctx_off(%r15) */
25422524
EMIT4_DISP(0x41000000, REG_3, REG_15, tjit->run_ctx_off);
2543-
/* %r1() */
2544-
call_r1(jit);
2525+
/* brasl %r14,__bpf_prog_enter */
2526+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, bpf_trampoline_enter(p));
25452527
/* ltgr %r7,%r2 */
25462528
EMIT4(0xb9020000, REG_7, REG_2);
25472529
/* brcl 8,skip */
@@ -2552,15 +2534,13 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
25522534
* retval = bpf_func(args, p->insnsi);
25532535
*/
25542536

2555-
/* %r1 = p->bpf_func */
2556-
load_imm64(jit, REG_1, (u64)p->bpf_func);
25572537
/* la %r2,bpf_args_off(%r15) */
25582538
EMIT4_DISP(0x41000000, REG_2, REG_15, tjit->bpf_args_off);
25592539
/* %r3 = p->insnsi */
25602540
if (!p->jited)
25612541
load_imm64(jit, REG_3, (u64)p->insnsi);
2562-
/* %r1() */
2563-
call_r1(jit);
2542+
/* brasl %r14,p->bpf_func */
2543+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, p->bpf_func);
25642544
/* stg %r2,retval_off(%r15) */
25652545
if (save_ret) {
25662546
if (sign_extend(jit, REG_2, m->ret_size, m->ret_flags))
@@ -2577,16 +2557,14 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
25772557
* __bpf_prog_exit(p, start, &run_ctx);
25782558
*/
25792559

2580-
/* %r1 = __bpf_prog_exit */
2581-
load_imm64(jit, REG_1, (u64)bpf_trampoline_exit(p));
25822560
/* %r2 = p */
25832561
load_imm64(jit, REG_2, (u64)p);
25842562
/* lgr %r3,%r7 */
25852563
EMIT4(0xb9040000, REG_3, REG_7);
25862564
/* la %r4,run_ctx_off(%r15) */
25872565
EMIT4_DISP(0x41000000, REG_4, REG_15, tjit->run_ctx_off);
2588-
/* %r1() */
2589-
call_r1(jit);
2566+
/* brasl %r14,__bpf_prog_exit */
2567+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, bpf_trampoline_exit(p));
25902568

25912569
return 0;
25922570
}
@@ -2746,9 +2724,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
27462724

27472725
/* lgr %r8,%r0 */
27482726
EMIT4(0xb9040000, REG_8, REG_0);
2749-
} else {
2750-
/* %r8 = func_addr + S390X_PATCH_SIZE */
2751-
load_imm64(jit, REG_8, (u64)func_addr + S390X_PATCH_SIZE);
27522727
}
27532728

27542729
/*
@@ -2774,12 +2749,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
27742749
* __bpf_tramp_enter(im);
27752750
*/
27762751

2777-
/* %r1 = __bpf_tramp_enter */
2778-
load_imm64(jit, REG_1, (u64)__bpf_tramp_enter);
27792752
/* %r2 = im */
27802753
load_imm64(jit, REG_2, (u64)im);
2781-
/* %r1() */
2782-
call_r1(jit);
2754+
/* brasl %r14,__bpf_tramp_enter */
2755+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, __bpf_tramp_enter);
27832756
}
27842757

27852758
for (i = 0; i < fentry->nr_links; i++)
@@ -2832,10 +2805,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
28322805
/* mvc tail_call_cnt(4,%r15),tccnt_off(%r15) */
28332806
_EMIT6(0xd203f000 | offsetof(struct prog_frame, tail_call_cnt),
28342807
0xf000 | tjit->tccnt_off);
2835-
/* lgr %r1,%r8 */
2836-
EMIT4(0xb9040000, REG_1, REG_8);
2837-
/* %r1() */
2838-
call_r1(jit);
2808+
if (flags & BPF_TRAMP_F_ORIG_STACK) {
2809+
if (nospec_uses_trampoline())
2810+
/* brasl %r14,__s390_indirect_jump_r8 */
2811+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14,
2812+
__s390_indirect_jump_r8);
2813+
else
2814+
/* basr %r14,%r8 */
2815+
EMIT2(0x0d00, REG_14, REG_8);
2816+
} else {
2817+
/* brasl %r14,func_addr+S390X_PATCH_SIZE */
2818+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14,
2819+
func_addr + S390X_PATCH_SIZE);
2820+
}
28392821
/* stg %r2,retval_off(%r15) */
28402822
EMIT6_DISP_LH(0xe3000000, 0x0024, REG_2, REG_0, REG_15,
28412823
tjit->retval_off);
@@ -2866,12 +2848,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
28662848
* __bpf_tramp_exit(im);
28672849
*/
28682850

2869-
/* %r1 = __bpf_tramp_exit */
2870-
load_imm64(jit, REG_1, (u64)__bpf_tramp_exit);
28712851
/* %r2 = im */
28722852
load_imm64(jit, REG_2, (u64)im);
2873-
/* %r1() */
2874-
call_r1(jit);
2853+
/* brasl %r14,__bpf_tramp_exit */
2854+
EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, __bpf_tramp_exit);
28752855
}
28762856

28772857
/* lmg %r2,%rN,reg_args_off(%r15) */
@@ -2880,7 +2860,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
28802860
REG_2 + (nr_reg_args - 1), REG_15,
28812861
tjit->reg_args_off);
28822862
/* lgr %r1,%r8 */
2883-
if (!(flags & BPF_TRAMP_F_SKIP_FRAME))
2863+
if (!(flags & BPF_TRAMP_F_SKIP_FRAME) &&
2864+
(flags & BPF_TRAMP_F_ORIG_STACK))
28842865
EMIT4(0xb9040000, REG_1, REG_8);
28852866
/* lmg %r7,%r8,r7_r8_off(%r15) */
28862867
EMIT6_DISP_LH(0xeb000000, 0x0004, REG_7, REG_8, REG_15,
@@ -2899,9 +2880,12 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
28992880
EMIT4_IMM(0xa70b0000, REG_15, tjit->stack_size);
29002881
if (flags & BPF_TRAMP_F_SKIP_FRAME)
29012882
EMIT_JUMP_REG(14);
2902-
else
2883+
else if (flags & BPF_TRAMP_F_ORIG_STACK)
29032884
EMIT_JUMP_REG(1);
2904-
2885+
else
2886+
/* brcl 0xf,func_addr+S390X_PATCH_SIZE */
2887+
EMIT6_PCREL_RILC_PTR(0xc0040000, 0xf,
2888+
func_addr + S390X_PATCH_SIZE);
29052889
return 0;
29062890
}
29072891

0 commit comments

Comments
 (0)