From: Tao Chen <chen.dylane@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
sparclinux@vger.kernel.org, Tao Chen <chen.dylane@gmail.com>
Subject: [PATCH bpf-next 2/2] bpf: Add BPF_CALL_FUNC* to simplify code
Date: Fri, 20 Sep 2024 23:37:06 +0800 [thread overview]
Message-ID: <20240920153706.919154-1-chen.dylane@gmail.com> (raw)
No logic changed, like macro BPF_CALL_IMM, add BPF_CALL_FUNC/_FUNC_ARGS
to simplify code.
Signed-off-by: Tao Chen <chen.dylane@gmail.com>
---
arch/sparc/net/bpf_jit_comp_64.c | 2 +-
arch/x86/net/bpf_jit_comp.c | 2 +-
arch/x86/net/bpf_jit_comp32.c | 5 ++---
include/linux/filter.h | 4 ++++
kernel/bpf/core.c | 6 +++---
5 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/arch/sparc/net/bpf_jit_comp_64.c b/arch/sparc/net/bpf_jit_comp_64.c
index 73bf0aea8baf..076b1f216360 100644
--- a/arch/sparc/net/bpf_jit_comp_64.c
+++ b/arch/sparc/net/bpf_jit_comp_64.c
@@ -1213,7 +1213,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
/* function call */
case BPF_JMP | BPF_CALL:
{
- u8 *func = ((u8 *)__bpf_call_base) + imm;
+ u8 *func = BPF_CALL_FUNC(imm);
ctx->saw_call = true;
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 06b080b61aa5..052e5cc65fc0 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -2126,7 +2126,7 @@ st: if (is_imm8(insn->off))
case BPF_JMP | BPF_CALL: {
u8 *ip = image + addrs[i - 1];
- func = (u8 *) __bpf_call_base + imm32;
+ func = BPF_CALL_FUNC(imm32);
if (tail_call_reachable) {
LOAD_TAIL_CALL_CNT_PTR(bpf_prog->aux->stack_depth);
ip += 7;
diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c
index de0f9e5f9f73..f7277639bd2c 100644
--- a/arch/x86/net/bpf_jit_comp32.c
+++ b/arch/x86/net/bpf_jit_comp32.c
@@ -1627,8 +1627,7 @@ static int emit_kfunc_call(const struct bpf_prog *bpf_prog, u8 *end_addr,
/* mov dword ptr [ebp+off],eax */
if (fm->ret_size)
end_addr -= 3;
-
- jmp_offset = (u8 *)__bpf_call_base + insn->imm - end_addr;
+ jmp_offset = BPF_CALL_FUNC(insn->imm) - end_addr;
if (!is_simm32(jmp_offset)) {
pr_err("unsupported BPF kernel function jmp_offset:%lld\n",
jmp_offset);
@@ -2103,7 +2102,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
break;
}
- func = (u8 *) __bpf_call_base + imm32;
+ func = BPF_CALL_FUNC(imm32);
jmp_offset = func - (image + addrs[i]);
if (!imm32 || !is_simm32(jmp_offset)) {
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 99b6fc83825b..d06526decc6d 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -461,6 +461,10 @@ static inline bool insn_is_cast_user(const struct bpf_insn *insn)
#define BPF_CALL_IMM(x) ((void *)(x) - (void *)__bpf_call_base)
+#define BPF_CALL_FUNC(x) ((x) + (u8 *)__bpf_call_base)
+
+#define BPF_CALL_FUNC_ARGS(x) ((x) + (u8 *)__bpf_call_base_args)
+
#define BPF_EMIT_CALL(FUNC) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_CALL, \
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 4e07cc057d6f..f965f0d586f3 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1278,7 +1278,7 @@ int bpf_jit_get_func_addr(const struct bpf_prog *prog,
* and the helper with imm relative to it are both in core
* kernel.
*/
- addr = (u8 *)__bpf_call_base + imm;
+ addr = BPF_CALL_FUNC(imm);
}
*func_addr = (unsigned long)addr;
@@ -2007,12 +2007,12 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
* preserves BPF_R6-BPF_R9, and stores return value
* into BPF_R0.
*/
- BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
+ BPF_R0 = BPF_CALL_FUNC(insn->imm)(BPF_R1, BPF_R2, BPF_R3,
BPF_R4, BPF_R5);
CONT;
JMP_CALL_ARGS:
- BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
+ BPF_R0 = BPF_CALL_FUNC_ARGS(insn->imm)(BPF_R1, BPF_R2,
BPF_R3, BPF_R4,
BPF_R5,
insn + insn->off + 1);
--
2.43.0
next reply other threads:[~2024-09-20 15:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-20 15:37 Tao Chen [this message]
2024-09-21 1:21 ` [PATCH bpf-next 2/2] bpf: Add BPF_CALL_FUNC* to simplify code kernel test robot
2024-09-21 1:52 ` kernel test robot
2024-09-21 3:19 ` Tao Chen
2024-09-22 19:36 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240920153706.919154-1-chen.dylane@gmail.com \
--to=chen.dylane@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=eddyz87@gmail.com \
--cc=hpa@zytor.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mingo@redhat.com \
--cc=song@kernel.org \
--cc=sparclinux@vger.kernel.org \
--cc=x86@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox