Skip to content

Commit d352eca

Browse files
author
Alexei Starovoitov
committed
Merge branch 'support-bpf_fastcall-patterns-for-calls-to-kfuncs'
Eduard Zingerman says: ==================== support bpf_fastcall patterns for calls to kfuncs As an extension of [1], allow bpf_fastcall patterns for kfuncs: - pattern rules are the same as for helpers; - spill/fill removal is allowed only for kfuncs listed in the is_fastcall_kfunc_call (under assumption that such kfuncs would always be members of special_kfunc_list). Allow bpf_fastcall rewrite for bpf_cast_to_kern_ctx() and bpf_rdonly_cast() in order to conjure selftests for this feature. After this patch-set verifier would rewrite the program below: r2 = 1 *(u64 *)(r10 - 32) = r2 call %[bpf_cast_to_kern_ctx] r2 = *(u64 *)(r10 - 32) r0 = r2;" As follows: r2 = 1 /* spill/fill at r10[-32] is removed */ r0 = r1 /* replacement for bpf_cast_to_kern_ctx() */ r0 = r2 exit Also, attribute used by LLVM implementation of the feature had been changed from no_caller_saved_registers to bpf_fastcall (see [2]). This patch-set replaces references to nocsr by references to bpf_fastcall to keep LLVM and Kernel parts in sync. [1] no_caller_saved_registers attribute for helper calls https://lore.kernel.org/bpf/[email protected]/ [2] [BPF] introduce __attribute__((bpf_fastcall)) llvm/llvm-project#105417 Changes v2->v3: - added a patch fixing arch_mask handling in test_loader, otherwise newly added tests for the feature were skipped (a fix for regression introduced by a recent commit); - fixed warning regarding unused 'params' variable; - applied stylistical fixes suggested by Yonghong; - added acks from Yonghong; Changes v1->v2: - added two patches replacing all mentions of nocsr by bpf_fastcall (suggested by Andrii); - removed KF_NOCSR flag (suggested by Yonghong). v1: https://lore.kernel.org/bpf/[email protected]/ v2: https://lore.kernel.org/bpf/[email protected]/ ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 6d641ca + 8c2e043 commit d352eca

File tree

7 files changed

+192
-102
lines changed

7 files changed

+192
-102
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ struct bpf_func_proto {
808808
bool gpl_only;
809809
bool pkt_access;
810810
bool might_sleep;
811-
/* set to true if helper follows contract for gcc/llvm
812-
* attribute no_caller_saved_registers:
811+
/* set to true if helper follows contract for llvm
812+
* attribute bpf_fastcall:
813813
* - void functions do not scratch r0
814814
* - functions taking N arguments scratch only registers r1-rN
815815
*/
816-
bool allow_nocsr;
816+
bool allow_fastcall;
817817
enum bpf_return_type ret_type;
818818
union {
819819
struct {

include/linux/bpf_verifier.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,13 @@ struct bpf_insn_aux_data {
577577
bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
578578
u8 alu_state; /* used in combination with alu_limit */
579579
/* true if STX or LDX instruction is a part of a spill/fill
580-
* pattern for a no_caller_saved_registers call.
580+
* pattern for a bpf_fastcall call.
581581
*/
582-
u8 nocsr_pattern:1;
582+
u8 fastcall_pattern:1;
583583
/* for CALL instructions, a number of spill/fill pairs in the
584-
* no_caller_saved_registers pattern.
584+
* bpf_fastcall pattern.
585585
*/
586-
u8 nocsr_spills_num:3;
586+
u8 fastcall_spills_num:3;
587587

588588
/* below fields are initialized once */
589589
unsigned int orig_idx; /* original instruction index */
@@ -653,19 +653,19 @@ struct bpf_subprog_info {
653653
u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
654654
u16 stack_depth; /* max. stack depth used by this function */
655655
u16 stack_extra;
656-
/* offsets in range [stack_depth .. nocsr_stack_off)
657-
* are used for no_caller_saved_registers spills and fills.
656+
/* offsets in range [stack_depth .. fastcall_stack_off)
657+
* are used for bpf_fastcall spills and fills.
658658
*/
659-
s16 nocsr_stack_off;
659+
s16 fastcall_stack_off;
660660
bool has_tail_call: 1;
661661
bool tail_call_reachable: 1;
662662
bool has_ld_abs: 1;
663663
bool is_cb: 1;
664664
bool is_async_cb: 1;
665665
bool is_exception_cb: 1;
666666
bool args_cached: 1;
667-
/* true if nocsr stack region is used by functions that can't be inlined */
668-
bool keep_nocsr_stack: 1;
667+
/* true if bpf_fastcall stack region is used by functions that can't be inlined */
668+
bool keep_fastcall_stack: 1;
669669

670670
u8 arg_cnt;
671671
struct bpf_subprog_arg_info args[MAX_BPF_FUNC_REG_ARGS];

kernel/bpf/helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
158158
.func = bpf_get_smp_processor_id,
159159
.gpl_only = false,
160160
.ret_type = RET_INTEGER,
161-
.allow_nocsr = true,
161+
.allow_fastcall = true,
162162
};
163163

164164
BPF_CALL_0(bpf_get_numa_node_id)

0 commit comments

Comments
 (0)