@@ -324,6 +324,8 @@ struct jit_context {
324324#define X86_PATCH_SIZE 5
325325/* Number of bytes that will be skipped on tailcall */
326326#define X86_TAIL_CALL_OFFSET (12 + ENDBR_INSN_SIZE)
327+ /* Number of extra bytes that will be skipped on poke */
328+ #define X86_POKE_EXTRA 3
327329
328330static void push_r12 (u8 * * pprog )
329331{
@@ -491,17 +493,13 @@ static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)
491493 */
492494static void emit_prologue (u8 * * pprog , u32 stack_depth , bool ebpf_from_cbpf ,
493495 bool tail_call_reachable , bool is_subprog ,
494- bool is_exception_cb )
496+ bool is_exception_cb , bool is_extension )
495497{
496498 u8 * prog = * pprog ;
497499
498500 emit_cfi (& prog , is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash );
499- /* BPF trampoline can be made to work without these nops,
500- * but let's waste 5 bytes for now and optimize later
501- */
502- emit_nops (& prog , X86_PATCH_SIZE );
503501 if (!ebpf_from_cbpf ) {
504- if (tail_call_reachable && !is_subprog )
502+ if (tail_call_reachable && !is_extension && ! is_subprog )
505503 /* When it's the entry of the whole tailcall context,
506504 * zeroing rax means initialising tail_call_cnt.
507505 */
@@ -510,6 +508,10 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
510508 /* Keep the same instruction layout. */
511509 emit_nops (& prog , 3 ); /* nop3 */
512510 }
511+ /* BPF trampoline can be made to work without these nops,
512+ * but let's waste 5 bytes for now and optimize later
513+ */
514+ emit_nops (& prog , X86_PATCH_SIZE );
513515 /* Exception callback receives FP as third parameter */
514516 if (is_exception_cb ) {
515517 EMIT3 (0x48 , 0x89 , 0xF4 ); /* mov rsp, rsi */
@@ -623,10 +625,13 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
623625
624626 /*
625627 * See emit_prologue(), for IBT builds the trampoline hook is preceded
626- * with an ENDBR instruction.
628+ * with an ENDBR instruction and 3 bytes tail_call_cnt initialization
629+ * instruction.
627630 */
628631 if (is_endbr (* (u32 * )ip ))
629632 ip += ENDBR_INSN_SIZE ;
633+ if (is_bpf_text_address ((long )ip ))
634+ ip += X86_POKE_EXTRA ;
630635
631636 return __bpf_arch_text_poke (ip , t , old_addr , new_addr );
632637}
@@ -1416,6 +1421,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
14161421 int oldproglen , struct jit_context * ctx , bool jmp_padding )
14171422{
14181423 bool tail_call_reachable = bpf_prog -> aux -> tail_call_reachable ;
1424+ bool is_extension = bpf_prog -> type == BPF_PROG_TYPE_EXT ;
14191425 struct bpf_insn * insn = bpf_prog -> insnsi ;
14201426 bool callee_regs_used [4 ] = {};
14211427 int insn_cnt = bpf_prog -> len ;
@@ -1434,7 +1440,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
14341440
14351441 emit_prologue (& prog , bpf_prog -> aux -> stack_depth ,
14361442 bpf_prog_was_classic (bpf_prog ), tail_call_reachable ,
1437- bpf_is_subprog (bpf_prog ), bpf_prog -> aux -> exception_cb );
1443+ bpf_is_subprog (bpf_prog ), bpf_prog -> aux -> exception_cb ,
1444+ is_extension );
14381445 /* Exception callback will clobber callee regs for its own use, and
14391446 * restore the original callee regs from main prog's stack frame.
14401447 */
@@ -2973,6 +2980,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
29732980 */
29742981 if (is_endbr (* (u32 * )orig_call ))
29752982 orig_call += ENDBR_INSN_SIZE ;
2983+ if (is_bpf_text_address ((long )orig_call ))
2984+ orig_call += X86_POKE_EXTRA ;
29762985 orig_call += X86_PATCH_SIZE ;
29772986 }
29782987
@@ -3075,6 +3084,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
30753084 /* remember return value in a stack for bpf prog to access */
30763085 emit_stx (& prog , BPF_DW , BPF_REG_FP , BPF_REG_0 , -8 );
30773086 im -> ip_after_call = image + (prog - (u8 * )rw_image );
3087+ emit_nops (& prog , X86_POKE_EXTRA );
30783088 emit_nops (& prog , X86_PATCH_SIZE );
30793089 }
30803090
0 commit comments