Skip to contentSkip to conversation

bpf: Add BPF_CALL_FUNC to simplify code

PATCH In review Tao Chen 2 participants 2 messages Oct 2, 2024

Subsystems (7)
lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH v2 2/2 bpf: Add BPF_CALL_FUNC to simplify code

No logic changed, like macro BPF_CALL_IMM, add BPF_CALL_FUNC to simplify code.

---
 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           | 2 ++
 kernel/bpf/core.c                | 2 +-
 5 files changed, 7 insertions(+), 6 deletions(-)

Change list:
- v2 -> v1:
- fix compile error reported by kernel test robot

arch/sparc/net/bpf_jit_comp_64.c +1 -1
index 73bf0aea8baf..076b1f216360 100644
@@ -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;
 
arch/x86/net/bpf_jit_comp.c +1 -1
index 06b080b61aa5..052e5cc65fc0 100644
@@ -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;
arch/x86/net/bpf_jit_comp32.c +2 -3
index de0f9e5f9f73..f7277639bd2c 100644
@@ -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)) {
include/linux/filter.h +2 -0
index 99b6fc83825b..9924b581aa71 100644
@@ -461,6 +461,8 @@ 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_EMIT_CALL(FUNC)					\
 	((struct bpf_insn) {					\
 		.code  = BPF_JMP | BPF_CALL,			\
kernel/bpf/core.c +1 -1
index 4e07cc057d6f..9832e878fbb3 100644
@@ -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;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 71 lines · Tao Chen

No logic changed, like macro BPF_CALL_IMM, add BPF_CALL_FUNC to simplify code.

---
 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           | 2 ++
 kernel/bpf/core.c                | 2 +-
 5 files changed, 7 insertions(+), 6 deletions(-)

Change list:
- v2 -> v1:
- fix compile error reported by kernel test robot

arch/sparc/net/bpf_jit_comp_64.c +1 -1
index 73bf0aea8baf..076b1f216360 100644
@@ -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;
 
arch/x86/net/bpf_jit_comp.c +1 -1
index 06b080b61aa5..052e5cc65fc0 100644
@@ -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;
arch/x86/net/bpf_jit_comp32.c +2 -3
index de0f9e5f9f73..f7277639bd2c 100644
@@ -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)) {
include/linux/filter.h +0 -0
index 99b6fc83825b..9924b581aa71 100644

include/linux/filter.h static inline bool insn_is_cast_user(const struct bpf_insn *insn) · line 465

Quoted 5 lines
include/linux/filter.h +2 -0
@@ -461,6 +461,8 @@ 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)
+

I don't like to hide code behind macros.
The code is cleaner as-is.

pw-bot: cr