diff mbox series

[bpf-next,v2,1/2] riscv, bpf: Add 12-argument support for RV64 bpf trampoline

Message ID 20240403041710.1416369-2-pulehui@huaweicloud.com (mailing list archive)
State Superseded
Headers show
Series Add 12-argument support for RV64 bpf trampoline | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR success PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 success .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 warning .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Pu Lehui April 3, 2024, 4:17 a.m. UTC
From: Pu Lehui <pulehui@huawei.com>

This patch adds 12 function arguments support for riscv64 bpf
trampoline. The current bpf trampoline supports <= sizeof(u64) bytes
scalar arguments [0] and <= 16 bytes struct arguments [1]. Therefore, we
focus on the situation where scalars are at most XLEN bits and
aggregates whose total size does not exceed 2×XLEN bits in the riscv
calling convention [2].

Link: https://elixir.bootlin.com/linux/v6.8/source/kernel/bpf/btf.c#L6184 [0]
Link: https://elixir.bootlin.com/linux/v6.8/source/kernel/bpf/btf.c#L6769 [1]
Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/releases/download/draft-20230929-e5c800e661a53efe3c2678d71a306323b60eb13b/riscv-abi.pdf [2]
Signed-off-by: Pu Lehui <pulehui@huawei.com>
Acked-by: Björn Töpel <bjorn@kernel.org>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
---
 arch/riscv/net/bpf_jit_comp64.c | 65 +++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 19 deletions(-)

Comments

Pu Lehui April 3, 2024, 7:26 a.m. UTC | #1
On 2024/4/3 12:17, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
> 
> This patch adds 12 function arguments support for riscv64 bpf
> trampoline. The current bpf trampoline supports <= sizeof(u64) bytes
> scalar arguments [0] and <= 16 bytes struct arguments [1]. Therefore, we
> focus on the situation where scalars are at most XLEN bits and
> aggregates whose total size does not exceed 2×XLEN bits in the riscv
> calling convention [2].
> 
> Link: https://elixir.bootlin.com/linux/v6.8/source/kernel/bpf/btf.c#L6184 [0]
> Link: https://elixir.bootlin.com/linux/v6.8/source/kernel/bpf/btf.c#L6769 [1]
> Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/releases/download/draft-20230929-e5c800e661a53efe3c2678d71a306323b60eb13b/riscv-abi.pdf [2]
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> Acked-by: Björn Töpel <bjorn@kernel.org>
> Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
> ---
>   arch/riscv/net/bpf_jit_comp64.c | 65 +++++++++++++++++++++++----------
>   1 file changed, 46 insertions(+), 19 deletions(-)
> 
> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> index 77ea306452d4..7e2d6e8c60c2 100644
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
> @@ -14,6 +14,7 @@
>   #include <asm/cfi.h>
>   #include "bpf_jit.h"
>   
> +#define RV_MAX_ARGS_REG 8

Sorry for the noise. The macro and variable names look really weird. I 
will send new patch for the name alignment:

nr_reg_args: number of args in reg
nr_stack_args: number of args on stack
RV_MAX_REG_ARGS: macro for riscv max args in reg

>   #define RV_FENTRY_NINSNS 2
>   
>   #define RV_REG_TCC RV_REG_A6
> @@ -688,26 +689,44 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
>   	return ret;
>   }
>   
> -static void store_args(int nregs, int args_off, struct rv_jit_context *ctx)
> +static void store_args(int nr_arg_slots, int args_off, struct rv_jit_context *ctx)
>   {
>   	int i;
>   
> -	for (i = 0; i < nregs; i++) {
> -		emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
> +	for (i = 0; i < nr_arg_slots; i++) {
> +		if (i < RV_MAX_ARGS_REG) {
> +			emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
> +		} else {
> +			/* skip slots for T0 and FP of traced function */
> +			emit_ld(RV_REG_T1, 16 + (i - RV_MAX_ARGS_REG) * 8, RV_REG_FP, ctx);
> +			emit_sd(RV_REG_FP, -args_off, RV_REG_T1, ctx);
> +		}
>   		args_off -= 8;
>   	}
>   }
>   
> -static void restore_args(int nregs, int args_off, struct rv_jit_context *ctx)
> +static void restore_args(int nr_args_reg, int args_off, struct rv_jit_context *ctx)
>   {
>   	int i;
>   
> -	for (i = 0; i < nregs; i++) {
> +	for (i = 0; i < nr_args_reg; i++) {
>   		emit_ld(RV_REG_A0 + i, -args_off, RV_REG_FP, ctx);
>   		args_off -= 8;
>   	}
>   }
>   
> +static void restore_stack_args(int nr_args_stack, int args_off, int stk_arg_off, struct rv_jit_context *ctx)
> +{
> +	int i;
> +
> +	for (i = 0; i < nr_args_stack; i++) {
> +		emit_ld(RV_REG_T1, -(args_off - RV_MAX_ARGS_REG * 8), RV_REG_FP, ctx);
> +		emit_sd(RV_REG_FP, -stk_arg_off, RV_REG_T1, ctx);
> +		args_off -= 8;
> +		stk_arg_off -= 8;
> +	}
> +}
> +
>   static int invoke_bpf_prog(struct bpf_tramp_link *l, int args_off, int retval_off,
>   			   int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
>   {
> @@ -780,8 +799,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   {
>   	int i, ret, offset;
>   	int *branches_off = NULL;
> -	int stack_size = 0, nregs = m->nr_args;
> -	int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off;
> +	int stack_size = 0, nr_arg_slots = 0;
> +	int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_arg_off;
>   	struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
>   	struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
>   	struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
> @@ -827,20 +846,21 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   	 * FP - sreg_off    [ callee saved reg	]
>   	 *
>   	 *		    [ pads              ] pads for 16 bytes alignment
> +	 *
> +	 *		    [ stack_argN        ]
> +	 *		    [ ...               ]
> +	 * FP - stk_arg_off [ stack_arg1        ] BPF_TRAMP_F_CALL_ORIG
>   	 */
>   
>   	if (flags & (BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SHARE_IPMODIFY))
>   		return -ENOTSUPP;
>   
> -	/* extra regiters for struct arguments */
> -	for (i = 0; i < m->nr_args; i++)
> -		if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
> -			nregs += round_up(m->arg_size[i], 8) / 8 - 1;
> -
> -	/* 8 arguments passed by registers */
> -	if (nregs > 8)
> +	if (m->nr_args > MAX_BPF_FUNC_ARGS)
>   		return -ENOTSUPP;
>   
> +	for (i = 0; i < m->nr_args; i++)
> +		nr_arg_slots += round_up(m->arg_size[i], 8) / 8;
> +
>   	/* room of trampoline frame to store return address and frame pointer */
>   	stack_size += 16;
>   
> @@ -850,7 +870,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   		retval_off = stack_size;
>   	}
>   
> -	stack_size += nregs * 8;
> +	stack_size += nr_arg_slots * 8;
>   	args_off = stack_size;
>   
>   	stack_size += 8;
> @@ -867,8 +887,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   	stack_size += 8;
>   	sreg_off = stack_size;
>   
> +	if (nr_arg_slots - RV_MAX_ARGS_REG > 0)
> +		stack_size += (nr_arg_slots - RV_MAX_ARGS_REG) * 8;
> +
>   	stack_size = round_up(stack_size, 16);
>   
> +	/* room for args on stack must be at the top of stack*/
> +	stk_arg_off = stack_size;
> +
>   	if (!is_struct_ops) {
>   		/* For the trampoline called from function entry,
>   		 * the frame of traced function and the frame of
> @@ -904,10 +930,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   		emit_sd(RV_REG_FP, -ip_off, RV_REG_T1, ctx);
>   	}
>   
> -	emit_li(RV_REG_T1, nregs, ctx);
> +	emit_li(RV_REG_T1, nr_arg_slots, ctx);
>   	emit_sd(RV_REG_FP, -nregs_off, RV_REG_T1, ctx);
>   
> -	store_args(nregs, args_off, ctx);
> +	store_args(nr_arg_slots, args_off, ctx);
>   
>   	/* skip to actual body of traced function */
>   	if (flags & BPF_TRAMP_F_SKIP_FRAME)
> @@ -947,7 +973,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   	}
>   
>   	if (flags & BPF_TRAMP_F_CALL_ORIG) {
> -		restore_args(nregs, args_off, ctx);
> +		restore_args(min_t(int, nr_arg_slots, RV_MAX_ARGS_REG), args_off, ctx);
> +		restore_stack_args(nr_arg_slots - RV_MAX_ARGS_REG, args_off, stk_arg_off, ctx);
>   		ret = emit_call((const u64)orig_call, true, ctx);
>   		if (ret)
>   			goto out;
> @@ -982,7 +1009,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
>   	}
>   
>   	if (flags & BPF_TRAMP_F_RESTORE_REGS)
> -		restore_args(nregs, args_off, ctx);
> +		restore_args(min_t(int, nr_arg_slots, RV_MAX_ARGS_REG), args_off, ctx);
>   
>   	if (save_ret) {
>   		emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx);
diff mbox series

Patch

diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 77ea306452d4..7e2d6e8c60c2 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -14,6 +14,7 @@ 
 #include <asm/cfi.h>
 #include "bpf_jit.h"
 
+#define RV_MAX_ARGS_REG 8
 #define RV_FENTRY_NINSNS 2
 
 #define RV_REG_TCC RV_REG_A6
@@ -688,26 +689,44 @@  int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
 	return ret;
 }
 
-static void store_args(int nregs, int args_off, struct rv_jit_context *ctx)
+static void store_args(int nr_arg_slots, int args_off, struct rv_jit_context *ctx)
 {
 	int i;
 
-	for (i = 0; i < nregs; i++) {
-		emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
+	for (i = 0; i < nr_arg_slots; i++) {
+		if (i < RV_MAX_ARGS_REG) {
+			emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
+		} else {
+			/* skip slots for T0 and FP of traced function */
+			emit_ld(RV_REG_T1, 16 + (i - RV_MAX_ARGS_REG) * 8, RV_REG_FP, ctx);
+			emit_sd(RV_REG_FP, -args_off, RV_REG_T1, ctx);
+		}
 		args_off -= 8;
 	}
 }
 
-static void restore_args(int nregs, int args_off, struct rv_jit_context *ctx)
+static void restore_args(int nr_args_reg, int args_off, struct rv_jit_context *ctx)
 {
 	int i;
 
-	for (i = 0; i < nregs; i++) {
+	for (i = 0; i < nr_args_reg; i++) {
 		emit_ld(RV_REG_A0 + i, -args_off, RV_REG_FP, ctx);
 		args_off -= 8;
 	}
 }
 
+static void restore_stack_args(int nr_args_stack, int args_off, int stk_arg_off, struct rv_jit_context *ctx)
+{
+	int i;
+
+	for (i = 0; i < nr_args_stack; i++) {
+		emit_ld(RV_REG_T1, -(args_off - RV_MAX_ARGS_REG * 8), RV_REG_FP, ctx);
+		emit_sd(RV_REG_FP, -stk_arg_off, RV_REG_T1, ctx);
+		args_off -= 8;
+		stk_arg_off -= 8;
+	}
+}
+
 static int invoke_bpf_prog(struct bpf_tramp_link *l, int args_off, int retval_off,
 			   int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
 {
@@ -780,8 +799,8 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 {
 	int i, ret, offset;
 	int *branches_off = NULL;
-	int stack_size = 0, nregs = m->nr_args;
-	int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off;
+	int stack_size = 0, nr_arg_slots = 0;
+	int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_arg_off;
 	struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
 	struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
 	struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
@@ -827,20 +846,21 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	 * FP - sreg_off    [ callee saved reg	]
 	 *
 	 *		    [ pads              ] pads for 16 bytes alignment
+	 *
+	 *		    [ stack_argN        ]
+	 *		    [ ...               ]
+	 * FP - stk_arg_off [ stack_arg1        ] BPF_TRAMP_F_CALL_ORIG
 	 */
 
 	if (flags & (BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SHARE_IPMODIFY))
 		return -ENOTSUPP;
 
-	/* extra regiters for struct arguments */
-	for (i = 0; i < m->nr_args; i++)
-		if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
-			nregs += round_up(m->arg_size[i], 8) / 8 - 1;
-
-	/* 8 arguments passed by registers */
-	if (nregs > 8)
+	if (m->nr_args > MAX_BPF_FUNC_ARGS)
 		return -ENOTSUPP;
 
+	for (i = 0; i < m->nr_args; i++)
+		nr_arg_slots += round_up(m->arg_size[i], 8) / 8;
+
 	/* room of trampoline frame to store return address and frame pointer */
 	stack_size += 16;
 
@@ -850,7 +870,7 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		retval_off = stack_size;
 	}
 
-	stack_size += nregs * 8;
+	stack_size += nr_arg_slots * 8;
 	args_off = stack_size;
 
 	stack_size += 8;
@@ -867,8 +887,14 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	stack_size += 8;
 	sreg_off = stack_size;
 
+	if (nr_arg_slots - RV_MAX_ARGS_REG > 0)
+		stack_size += (nr_arg_slots - RV_MAX_ARGS_REG) * 8;
+
 	stack_size = round_up(stack_size, 16);
 
+	/* room for args on stack must be at the top of stack*/
+	stk_arg_off = stack_size;
+
 	if (!is_struct_ops) {
 		/* For the trampoline called from function entry,
 		 * the frame of traced function and the frame of
@@ -904,10 +930,10 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		emit_sd(RV_REG_FP, -ip_off, RV_REG_T1, ctx);
 	}
 
-	emit_li(RV_REG_T1, nregs, ctx);
+	emit_li(RV_REG_T1, nr_arg_slots, ctx);
 	emit_sd(RV_REG_FP, -nregs_off, RV_REG_T1, ctx);
 
-	store_args(nregs, args_off, ctx);
+	store_args(nr_arg_slots, args_off, ctx);
 
 	/* skip to actual body of traced function */
 	if (flags & BPF_TRAMP_F_SKIP_FRAME)
@@ -947,7 +973,8 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		restore_args(nregs, args_off, ctx);
+		restore_args(min_t(int, nr_arg_slots, RV_MAX_ARGS_REG), args_off, ctx);
+		restore_stack_args(nr_arg_slots - RV_MAX_ARGS_REG, args_off, stk_arg_off, ctx);
 		ret = emit_call((const u64)orig_call, true, ctx);
 		if (ret)
 			goto out;
@@ -982,7 +1009,7 @@  static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	}
 
 	if (flags & BPF_TRAMP_F_RESTORE_REGS)
-		restore_args(nregs, args_off, ctx);
+		restore_args(min_t(int, nr_arg_slots, RV_MAX_ARGS_REG), args_off, ctx);
 
 	if (save_ret) {
 		emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx);