@@ -312,9 +312,8 @@ static void pop_callee_regs(u8 **pprog, bool *callee_regs_used)
312312 * in arch/x86/kernel/alternative.c
313313 */
314314
315- static void emit_fineibt (u8 * * pprog , bool is_subprog )
315+ static void emit_fineibt (u8 * * pprog , u32 hash )
316316{
317- u32 hash = is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash ;
318317 u8 * prog = * pprog ;
319318
320319 EMIT_ENDBR ();
@@ -327,9 +326,8 @@ static void emit_fineibt(u8 **pprog, bool is_subprog)
327326 * pprog = prog ;
328327}
329328
330- static void emit_kcfi (u8 * * pprog , bool is_subprog )
329+ static void emit_kcfi (u8 * * pprog , u32 hash )
331330{
332- u32 hash = is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash ;
333331 u8 * prog = * pprog ;
334332
335333 EMIT1_off32 (0xb8 , hash ); /* movl $hash, %eax */
@@ -351,17 +349,17 @@ static void emit_kcfi(u8 **pprog, bool is_subprog)
351349 * pprog = prog ;
352350}
353351
354- static void emit_cfi (u8 * * pprog , bool is_subprog )
352+ static void emit_cfi (u8 * * pprog , u32 hash )
355353{
356354 u8 * prog = * pprog ;
357355
358356 switch (cfi_mode ) {
359357 case CFI_FINEIBT :
360- emit_fineibt (& prog , is_subprog );
358+ emit_fineibt (& prog , hash );
361359 break ;
362360
363361 case CFI_KCFI :
364- emit_kcfi (& prog , is_subprog );
362+ emit_kcfi (& prog , hash );
365363 break ;
366364
367365 default :
@@ -383,7 +381,7 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
383381{
384382 u8 * prog = * pprog ;
385383
386- emit_cfi (& prog , is_subprog );
384+ emit_cfi (& prog , is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash );
387385 /* BPF trampoline can be made to work without these nops,
388386 * but let's waste 5 bytes for now and optimize later
389387 */
@@ -2510,10 +2508,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
25102508 u8 * prog ;
25112509 bool save_ret ;
25122510
2511+ /*
2512+ * F_INDIRECT is only compatible with F_RET_FENTRY_RET, it is
2513+ * explicitly incompatible with F_CALL_ORIG | F_SKIP_FRAME | F_IP_ARG
2514+ * because @func_addr.
2515+ */
2516+ WARN_ON_ONCE ((flags & BPF_TRAMP_F_INDIRECT ) &&
2517+ (flags & ~(BPF_TRAMP_F_INDIRECT | BPF_TRAMP_F_RET_FENTRY_RET )));
2518+
25132519 /* extra registers for struct arguments */
2514- for (i = 0 ; i < m -> nr_args ; i ++ )
2520+ for (i = 0 ; i < m -> nr_args ; i ++ ) {
25152521 if (m -> arg_flags [i ] & BTF_FMODEL_STRUCT_ARG )
25162522 nr_regs += (m -> arg_size [i ] + 7 ) / 8 - 1 ;
2523+ }
25172524
25182525 /* x86-64 supports up to MAX_BPF_FUNC_ARGS arguments. 1-6
25192526 * are passed through regs, the remains are through stack.
@@ -2596,20 +2603,27 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
25962603
25972604 prog = rw_image ;
25982605
2599- EMIT_ENDBR ();
2600- /*
2601- * This is the direct-call trampoline, as such it needs accounting
2602- * for the __fentry__ call.
2603- */
2604- x86_call_depth_emit_accounting (& prog , NULL );
2606+ if (flags & BPF_TRAMP_F_INDIRECT ) {
2607+ /*
2608+ * Indirect call for bpf_struct_ops
2609+ */
2610+ emit_cfi (& prog , cfi_get_func_hash (func_addr ));
2611+ } else {
2612+ /*
2613+ * Direct-call fentry stub, as such it needs accounting for the
2614+ * __fentry__ call.
2615+ */
2616+ x86_call_depth_emit_accounting (& prog , NULL );
2617+ }
26052618 EMIT1 (0x55 ); /* push rbp */
26062619 EMIT3 (0x48 , 0x89 , 0xE5 ); /* mov rbp, rsp */
2607- if (!is_imm8 (stack_size ))
2620+ if (!is_imm8 (stack_size )) {
26082621 /* sub rsp, stack_size */
26092622 EMIT3_off32 (0x48 , 0x81 , 0xEC , stack_size );
2610- else
2623+ } else {
26112624 /* sub rsp, stack_size */
26122625 EMIT4 (0x48 , 0x83 , 0xEC , stack_size );
2626+ }
26132627 if (flags & BPF_TRAMP_F_TAIL_CALL_CTX )
26142628 EMIT1 (0x50 ); /* push rax */
26152629 /* mov QWORD PTR [rbp - rbx_off], rbx */
@@ -2643,10 +2657,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
26432657 }
26442658 }
26452659
2646- if (fentry -> nr_links )
2660+ if (fentry -> nr_links ) {
26472661 if (invoke_bpf (m , & prog , fentry , regs_off , run_ctx_off ,
26482662 flags & BPF_TRAMP_F_RET_FENTRY_RET , image , rw_image ))
26492663 return - EINVAL ;
2664+ }
26502665
26512666 if (fmod_ret -> nr_links ) {
26522667 branches = kcalloc (fmod_ret -> nr_links , sizeof (u8 * ),
@@ -2665,11 +2680,12 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
26652680 restore_regs (m , & prog , regs_off );
26662681 save_args (m , & prog , arg_stack_off , true);
26672682
2668- if (flags & BPF_TRAMP_F_TAIL_CALL_CTX )
2683+ if (flags & BPF_TRAMP_F_TAIL_CALL_CTX ) {
26692684 /* Before calling the original function, restore the
26702685 * tail_call_cnt from stack to rax.
26712686 */
26722687 RESTORE_TAIL_CALL_CNT (stack_size );
2688+ }
26732689
26742690 if (flags & BPF_TRAMP_F_ORIG_STACK ) {
26752691 emit_ldx (& prog , BPF_DW , BPF_REG_6 , BPF_REG_FP , 8 );
@@ -2698,17 +2714,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
26982714 /* Update the branches saved in invoke_bpf_mod_ret with the
26992715 * aligned address of do_fexit.
27002716 */
2701- for (i = 0 ; i < fmod_ret -> nr_links ; i ++ )
2717+ for (i = 0 ; i < fmod_ret -> nr_links ; i ++ ) {
27022718 emit_cond_near_jump (& branches [i ], image + (prog - (u8 * )rw_image ),
27032719 image + (branches [i ] - (u8 * )rw_image ), X86_JNE );
2720+ }
27042721 }
27052722
2706- if (fexit -> nr_links )
2723+ if (fexit -> nr_links ) {
27072724 if (invoke_bpf (m , & prog , fexit , regs_off , run_ctx_off ,
27082725 false, image , rw_image )) {
27092726 ret = - EINVAL ;
27102727 goto cleanup ;
27112728 }
2729+ }
27122730
27132731 if (flags & BPF_TRAMP_F_RESTORE_REGS )
27142732 restore_regs (m , & prog , regs_off );
@@ -2725,21 +2743,23 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
27252743 ret = - EINVAL ;
27262744 goto cleanup ;
27272745 }
2728- } else if (flags & BPF_TRAMP_F_TAIL_CALL_CTX )
2746+ } else if (flags & BPF_TRAMP_F_TAIL_CALL_CTX ) {
27292747 /* Before running the original function, restore the
27302748 * tail_call_cnt from stack to rax.
27312749 */
27322750 RESTORE_TAIL_CALL_CNT (stack_size );
2751+ }
27332752
27342753 /* restore return value of orig_call or fentry prog back into RAX */
27352754 if (save_ret )
27362755 emit_ldx (& prog , BPF_DW , BPF_REG_0 , BPF_REG_FP , -8 );
27372756
27382757 emit_ldx (& prog , BPF_DW , BPF_REG_6 , BPF_REG_FP , - rbx_off );
27392758 EMIT1 (0xC9 ); /* leave */
2740- if (flags & BPF_TRAMP_F_SKIP_FRAME )
2759+ if (flags & BPF_TRAMP_F_SKIP_FRAME ) {
27412760 /* skip our return address and return to parent */
27422761 EMIT4 (0x48 , 0x83 , 0xC4 , 8 ); /* add rsp, 8 */
2762+ }
27432763 emit_return (& prog , image + (prog - (u8 * )rw_image ));
27442764 /* Make sure the trampoline generation logic doesn't overflow */
27452765 if (WARN_ON_ONCE (prog > (u8 * )rw_image_end - BPF_INSN_SAFETY )) {
0 commit comments