@@ -324,6 +324,8 @@ struct jit_context {
324
324
#define X86_PATCH_SIZE 5
325
325
/* Number of bytes that will be skipped on tailcall */
326
326
#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
327
329
328
330
static void push_r12 (u8 * * pprog )
329
331
{
@@ -491,17 +493,13 @@ static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)
491
493
*/
492
494
static void emit_prologue (u8 * * pprog , u32 stack_depth , bool ebpf_from_cbpf ,
493
495
bool tail_call_reachable , bool is_subprog ,
494
- bool is_exception_cb )
496
+ bool is_exception_cb , bool is_extension )
495
497
{
496
498
u8 * prog = * pprog ;
497
499
498
500
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 );
503
501
if (!ebpf_from_cbpf ) {
504
- if (tail_call_reachable && !is_subprog )
502
+ if (tail_call_reachable && !is_extension && ! is_subprog )
505
503
/* When it's the entry of the whole tailcall context,
506
504
* zeroing rax means initialising tail_call_cnt.
507
505
*/
@@ -510,6 +508,10 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
510
508
/* Keep the same instruction layout. */
511
509
emit_nops (& prog , 3 ); /* nop3 */
512
510
}
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 );
513
515
/* Exception callback receives FP as third parameter */
514
516
if (is_exception_cb ) {
515
517
EMIT3 (0x48 , 0x89 , 0xF4 ); /* mov rsp, rsi */
@@ -623,10 +625,13 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
623
625
624
626
/*
625
627
* 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.
627
630
*/
628
631
if (is_endbr (* (u32 * )ip ))
629
632
ip += ENDBR_INSN_SIZE ;
633
+ if (is_bpf_text_address ((long )ip ))
634
+ ip += X86_POKE_EXTRA ;
630
635
631
636
return __bpf_arch_text_poke (ip , t , old_addr , new_addr );
632
637
}
@@ -1416,6 +1421,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
1416
1421
int oldproglen , struct jit_context * ctx , bool jmp_padding )
1417
1422
{
1418
1423
bool tail_call_reachable = bpf_prog -> aux -> tail_call_reachable ;
1424
+ bool is_extension = bpf_prog -> type == BPF_PROG_TYPE_EXT ;
1419
1425
struct bpf_insn * insn = bpf_prog -> insnsi ;
1420
1426
bool callee_regs_used [4 ] = {};
1421
1427
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
1434
1440
1435
1441
emit_prologue (& prog , bpf_prog -> aux -> stack_depth ,
1436
1442
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 );
1438
1445
/* Exception callback will clobber callee regs for its own use, and
1439
1446
* restore the original callee regs from main prog's stack frame.
1440
1447
*/
@@ -2973,6 +2980,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
2973
2980
*/
2974
2981
if (is_endbr (* (u32 * )orig_call ))
2975
2982
orig_call += ENDBR_INSN_SIZE ;
2983
+ if (is_bpf_text_address ((long )orig_call ))
2984
+ orig_call += X86_POKE_EXTRA ;
2976
2985
orig_call += X86_PATCH_SIZE ;
2977
2986
}
2978
2987
@@ -3075,6 +3084,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
3075
3084
/* remember return value in a stack for bpf prog to access */
3076
3085
emit_stx (& prog , BPF_DW , BPF_REG_FP , BPF_REG_0 , -8 );
3077
3086
im -> ip_after_call = image + (prog - (u8 * )rw_image );
3087
+ emit_nops (& prog , X86_POKE_EXTRA );
3078
3088
emit_nops (& prog , X86_PATCH_SIZE );
3079
3089
}
3080
3090
0 commit comments