3232#include <asm/set_memory.h>
3333#include <asm/text-patching.h>
3434#include <asm/unwind.h>
35- #include "bpf_jit.h"
3635
3736struct bpf_jit {
3837 u32 seen ; /* Flags to remember seen eBPF instructions */
@@ -54,7 +53,7 @@ struct bpf_jit {
5453 int prologue_plt ; /* Start of prologue hotpatch PLT */
5554 int kern_arena ; /* Pool offset of kernel arena address */
5655 u64 user_arena ; /* User arena address */
57- u32 frame_off ; /* Offset of frame from %r15 */
56+ u32 frame_off ; /* Offset of struct bpf_prog from %r15 */
5857};
5958
6059#define SEEN_MEM BIT(0) /* use mem[] for temporary storage */
@@ -426,12 +425,26 @@ static void jit_fill_hole(void *area, unsigned int size)
426425 memset (area , 0 , size );
427426}
428427
428+ /*
429+ * Caller-allocated part of the frame.
430+ * Thanks to packed stack, its otherwise unused initial part can be used for
431+ * the BPF stack and for the next frame.
432+ */
433+ struct prog_frame {
434+ u64 unused [8 ];
435+ /* BPF stack starts here and grows towards 0 */
436+ u32 tail_call_cnt ;
437+ u32 pad ;
438+ u64 r6 [10 ]; /* r6 - r15 */
439+ u64 backchain ;
440+ } __packed ;
441+
429442/*
430443 * Save registers from "rs" (register start) to "re" (register end) on stack
431444 */
432445static void save_regs (struct bpf_jit * jit , u32 rs , u32 re )
433446{
434- u32 off = STK_OFF_R6 + (rs - 6 ) * 8 ;
447+ u32 off = offsetof( struct prog_frame , r6 ) + (rs - 6 ) * 8 ;
435448
436449 if (rs == re )
437450 /* stg %rs,off(%r15) */
@@ -446,7 +459,7 @@ static void save_regs(struct bpf_jit *jit, u32 rs, u32 re)
446459 */
447460static void restore_regs (struct bpf_jit * jit , u32 rs , u32 re )
448461{
449- u32 off = jit -> frame_off + STK_OFF_R6 + (rs - 6 ) * 8 ;
462+ u32 off = jit -> frame_off + offsetof( struct prog_frame , r6 ) + (rs - 6 ) * 8 ;
450463
451464 if (rs == re )
452465 /* lg %rs,off(%r15) */
@@ -570,19 +583,22 @@ static void bpf_jit_plt(struct bpf_plt *plt, void *ret, void *target)
570583 * Emit function prologue
571584 *
572585 * Save registers and create stack frame if necessary.
573- * See stack frame layout description in "bpf_jit.h"!
586+ * Stack frame layout is described by struct prog_frame.
574587 */
575588static void bpf_jit_prologue (struct bpf_jit * jit , struct bpf_prog * fp )
576589{
590+ BUILD_BUG_ON (sizeof (struct prog_frame ) != STACK_FRAME_OVERHEAD );
591+
577592 /* No-op for hotpatching */
578593 /* brcl 0,prologue_plt */
579594 EMIT6_PCREL_RILC (0xc0040000 , 0 , jit -> prologue_plt );
580595 jit -> prologue_plt_ret = jit -> prg ;
581596
582597 if (!bpf_is_subprog (fp )) {
583598 /* Initialize the tail call counter in the main program. */
584- /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
585- _EMIT6 (0xd703f000 | STK_OFF_TCCNT , 0xf000 | STK_OFF_TCCNT );
599+ /* xc tail_call_cnt(4,%r15),tail_call_cnt(%r15) */
600+ _EMIT6 (0xd703f000 | offsetof(struct prog_frame , tail_call_cnt ),
601+ 0xf000 | offsetof(struct prog_frame , tail_call_cnt ));
586602 } else {
587603 /*
588604 * Skip the tail call counter initialization in subprograms.
@@ -625,13 +641,15 @@ static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp)
625641 if (is_first_pass (jit ) || (jit -> seen & SEEN_STACK )) {
626642 /* lgr %w1,%r15 (backchain) */
627643 EMIT4 (0xb9040000 , REG_W1 , REG_15 );
628- /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
629- EMIT4_DISP (0x41000000 , BPF_REG_FP , REG_15 , STK_160_UNUSED );
644+ /* la %bfp,unused_end(%r15) (BPF frame pointer) */
645+ EMIT4_DISP (0x41000000 , BPF_REG_FP , REG_15 ,
646+ offsetofend (struct prog_frame , unused ));
630647 /* aghi %r15,-frame_off */
631648 EMIT4_IMM (0xa70b0000 , REG_15 , - jit -> frame_off );
632- /* stg %w1,152 (%r15) (backchain ) */
649+ /* stg %w1,backchain (%r15) */
633650 EMIT6_DISP_LH (0xe3000000 , 0x0024 , REG_W1 , REG_0 ,
634- REG_15 , 152 );
651+ REG_15 ,
652+ offsetof(struct prog_frame , backchain ));
635653 }
636654}
637655
@@ -1774,9 +1792,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
17741792 * Note 2: We assume that the verifier does not let us call the
17751793 * main program, which clears the tail call counter on entry.
17761794 */
1777- /* mvc STK_OFF_TCCNT(4,%r15),frame_off+STK_OFF_TCCNT(%r15) */
1778- _EMIT6 (0xd203f000 | STK_OFF_TCCNT ,
1779- 0xf000 | (jit -> frame_off + STK_OFF_TCCNT ));
1795+ /* mvc tail_call_cnt(4,%r15),frame_off+tail_call_cnt(%r15) */
1796+ _EMIT6 (0xd203f000 | offsetof(struct prog_frame , tail_call_cnt ),
1797+ 0xf000 | (jit -> frame_off +
1798+ offsetof(struct prog_frame , tail_call_cnt )));
17801799
17811800 /* Sign-extend the kfunc arguments. */
17821801 if (insn -> src_reg == BPF_PSEUDO_KFUNC_CALL ) {
@@ -1827,7 +1846,8 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
18271846 * goto out;
18281847 */
18291848
1830- off = jit -> frame_off + STK_OFF_TCCNT ;
1849+ off = jit -> frame_off +
1850+ offsetof(struct prog_frame , tail_call_cnt );
18311851 /* lhi %w0,1 */
18321852 EMIT4_IMM (0xa7080000 , REG_W0 , 1 );
18331853 /* laal %w1,%w0,off(%r15) */
@@ -2160,7 +2180,9 @@ static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp,
21602180 jit -> prg = 0 ;
21612181 jit -> excnt = 0 ;
21622182 if (is_first_pass (jit ) || (jit -> seen & SEEN_STACK ))
2163- jit -> frame_off = STK_OFF + round_up (fp -> aux -> stack_depth , 8 );
2183+ jit -> frame_off = sizeof (struct prog_frame ) -
2184+ offsetofend (struct prog_frame , unused ) +
2185+ round_up (fp -> aux -> stack_depth , 8 );
21642186 else
21652187 jit -> frame_off = 0 ;
21662188
@@ -2642,9 +2664,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
26422664 /* stg %r1,backchain_off(%r15) */
26432665 EMIT6_DISP_LH (0xe3000000 , 0x0024 , REG_1 , REG_0 , REG_15 ,
26442666 tjit -> backchain_off );
2645- /* mvc tccnt_off(4,%r15),stack_size+STK_OFF_TCCNT (%r15) */
2667+ /* mvc tccnt_off(4,%r15),stack_size+tail_call_cnt (%r15) */
26462668 _EMIT6 (0xd203f000 | tjit -> tccnt_off ,
2647- 0xf000 | (tjit -> stack_size + STK_OFF_TCCNT ));
2669+ 0xf000 | (tjit -> stack_size +
2670+ offsetof(struct prog_frame , tail_call_cnt )));
26482671 /* stmg %r2,%rN,fwd_reg_args_off(%r15) */
26492672 if (nr_reg_args )
26502673 EMIT6_DISP_LH (0xeb000000 , 0x0024 , REG_2 ,
@@ -2781,8 +2804,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
27812804 (nr_stack_args * sizeof (u64 ) - 1 ) << 16 |
27822805 tjit -> stack_args_off ,
27832806 0xf000 | tjit -> orig_stack_args_off );
2784- /* mvc STK_OFF_TCCNT(4,%r15),tccnt_off(%r15) */
2785- _EMIT6 (0xd203f000 | STK_OFF_TCCNT , 0xf000 | tjit -> tccnt_off );
2807+ /* mvc tail_call_cnt(4,%r15),tccnt_off(%r15) */
2808+ _EMIT6 (0xd203f000 | offsetof(struct prog_frame , tail_call_cnt ),
2809+ 0xf000 | tjit -> tccnt_off );
27862810 /* lgr %r1,%r8 */
27872811 EMIT4 (0xb9040000 , REG_1 , REG_8 );
27882812 /* %r1() */
@@ -2839,8 +2863,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
28392863 if (flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET ))
28402864 EMIT6_DISP_LH (0xe3000000 , 0x0004 , REG_2 , REG_0 , REG_15 ,
28412865 tjit -> retval_off );
2842- /* mvc stack_size+STK_OFF_TCCNT(4,%r15),tccnt_off(%r15) */
2843- _EMIT6 (0xd203f000 | (tjit -> stack_size + STK_OFF_TCCNT ),
2866+ /* mvc stack_size+tail_call_cnt(4,%r15),tccnt_off(%r15) */
2867+ _EMIT6 (0xd203f000 | (tjit -> stack_size +
2868+ offsetof(struct prog_frame , tail_call_cnt )),
28442869 0xf000 | tjit -> tccnt_off );
28452870 /* aghi %r15,stack_size */
28462871 EMIT4_IMM (0xa70b0000 , REG_15 , tjit -> stack_size );
0 commit comments