[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250528034712.138701-14-dongml2@chinatelecom.cn>
Date: Wed, 28 May 2025 11:47:00 +0800
From: Menglong Dong <menglong8.dong@...il.com>
To: alexei.starovoitov@...il.com,
rostedt@...dmis.org,
jolsa@...nel.org
Cc: bpf@...r.kernel.org,
Menglong Dong <dongml2@...natelecom.cn>,
linux-kernel@...r.kernel.org
Subject: [PATCH bpf-next 13/25] x86,bpf: factor out __arch_get_bpf_regs_nr
Factor the function __arch_get_bpf_regs_nr() to get the regs count that
used by the function args.
The arch_get_bpf_regs_nr() will return -ENOTSUPP if the regs is not enough
to hold the function args.
Signed-off-by: Menglong Dong <dongml2@...natelecom.cn>
---
arch/x86/net/bpf_jit_comp.c | 36 +++++++++++++++++++++++++++++-------
include/linux/bpf.h | 1 +
kernel/bpf/verifier.c | 5 +++++
3 files changed, 35 insertions(+), 7 deletions(-)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 9e5fe2ba858f..84bb668f3bee 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -2945,6 +2945,33 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
return 0;
}
+static int __arch_bpf_get_regs_nr(const struct btf_func_model *m)
+{
+ int nr_regs = m->nr_args;
+
+ /* extra registers for struct arguments */
+ for (int i = 0; i < m->nr_args; i++) {
+ if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
+ nr_regs += (m->arg_size[i] + 7) / 8 - 1;
+ }
+
+ return nr_regs;
+}
+
+int arch_bpf_get_regs_nr(const struct btf_func_model *m)
+{
+ int nr_regs = __arch_bpf_get_regs_nr(m);
+
+ /* The maximum number of registers that can be used to pass
+ * arguments is 6. If the number of registers exceeds this,
+ * return -ENOTSUPP.
+ */
+ if (nr_regs > 6)
+ return -EOPNOTSUPP;
+
+ return nr_regs;
+}
+
/* mov rax, qword ptr [rbp - rounded_stack_depth - 8] */
#define LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack) \
__LOAD_TCC_PTR(-round_up(stack, 8) - 8)
@@ -3015,7 +3042,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
struct bpf_tramp_links *tlinks,
void *func_addr)
{
- int i, ret, nr_regs = m->nr_args, stack_size = 0;
+ int i, ret, nr_regs, stack_size = 0;
int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off;
struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
@@ -3033,15 +3060,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
WARN_ON_ONCE((flags & BPF_TRAMP_F_INDIRECT) &&
(flags & ~(BPF_TRAMP_F_INDIRECT | BPF_TRAMP_F_RET_FENTRY_RET)));
- /* extra registers for struct arguments */
- for (i = 0; i < m->nr_args; i++) {
- if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
- nr_regs += (m->arg_size[i] + 7) / 8 - 1;
- }
-
/* x86-64 supports up to MAX_BPF_FUNC_ARGS arguments. 1-6
* are passed through regs, the remains are through stack.
*/
+ nr_regs = __arch_bpf_get_regs_nr(m);
if (nr_regs > MAX_BPF_FUNC_ARGS)
return -ENOTSUPP;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index c35da9d91125..080bb966d026 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1221,6 +1221,7 @@ void arch_free_bpf_trampoline(void *image, unsigned int size);
int __must_check arch_protect_bpf_trampoline(void *image, unsigned int size);
int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
struct bpf_tramp_links *tlinks, void *func_addr);
+int arch_bpf_get_regs_nr(const struct btf_func_model *m);
u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
struct bpf_tramp_run_ctx *run_ctx);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5d2e70425c1d..9c4e29bc98c0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -22901,6 +22901,11 @@ static int do_check_main(struct bpf_verifier_env *env)
}
+int __weak arch_bpf_get_regs_nr(const struct btf_func_model *m)
+{
+ return -ENODEV;
+}
+
static void print_verification_stats(struct bpf_verifier_env *env)
{
int i;
--
2.39.5
Powered by blists - more mailing lists