Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'bpf-x86-inline-bpf_get_current_task-for-x86_64'

Menglong Dong says:

====================
bpf, x86: inline bpf_get_current_task() for x86_64

Inline bpf_get_current_task() and bpf_get_current_task_btf() for x86_64
to obtain better performance, and add the testcase for it.

Changes since v5:
* remove unnecessary 'ifdef' and __description in the selftests
* v5: https://lore.kernel.org/bpf/20260119070246.249499-1-dongml2@chinatelecom.cn/

Changes since v4:
* don't support the !CONFIG_SMP case
* v4: https://lore.kernel.org/bpf/20260112104529.224645-1-dongml2@chinatelecom.cn/

Changes since v3:
* handle the !CONFIG_SMP case
* ignore the !CONFIG_SMP case in the testcase, as we enable CONFIG_SMP
for x86_64 in the selftests

Changes since v2:
* implement it in the verifier with BPF_MOV64_PERCPU_REG() instead of in
x86_64 JIT (Alexei).

Changes since v1:
* add the testcase
* remove the usage of const_current_task
====================

Link: https://patch.msgid.link/20260120070555.233486-1-dongml2@chinatelecom.cn
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

+44
+22
kernel/bpf/verifier.c
··· 18130 18130 switch (imm) { 18131 18131 #ifdef CONFIG_X86_64 18132 18132 case BPF_FUNC_get_smp_processor_id: 18133 + #ifdef CONFIG_SMP 18134 + case BPF_FUNC_get_current_task_btf: 18135 + case BPF_FUNC_get_current_task: 18136 + #endif 18133 18137 return env->prog->jit_requested && bpf_jit_supports_percpu_insn(); 18134 18138 #endif 18135 18139 default: ··· 23710 23706 insn_buf[0] = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); 23711 23707 cnt = 1; 23712 23708 #endif 23709 + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); 23710 + if (!new_prog) 23711 + return -ENOMEM; 23712 + 23713 + delta += cnt - 1; 23714 + env->prog = prog = new_prog; 23715 + insn = new_prog->insnsi + i + delta; 23716 + goto next_insn; 23717 + } 23718 + 23719 + /* Implement bpf_get_current_task() and bpf_get_current_task_btf() inline. */ 23720 + if ((insn->imm == BPF_FUNC_get_current_task || insn->imm == BPF_FUNC_get_current_task_btf) && 23721 + verifier_inlines_helper_call(env, insn->imm)) { 23722 + insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&current_task); 23723 + insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0); 23724 + insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0); 23725 + cnt = 3; 23726 + 23713 23727 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); 23714 23728 if (!new_prog) 23715 23729 return -ENOMEM;
+2
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 112 112 #include "verifier_xdp_direct_packet_access.skel.h" 113 113 #include "verifier_bits_iter.skel.h" 114 114 #include "verifier_lsm.skel.h" 115 + #include "verifier_jit_inline.skel.h" 115 116 #include "irq.skel.h" 116 117 117 118 #define MAX_ENTRIES 11 ··· 256 255 void test_verifier_lsm(void) { RUN(verifier_lsm); } 257 256 void test_irq(void) { RUN(irq); } 258 257 void test_verifier_mtu(void) { RUN(verifier_mtu); } 258 + void test_verifier_jit_inline(void) { RUN(verifier_jit_inline); } 259 259 260 260 static int init_test_val_map(struct bpf_object *obj, char *map_name) 261 261 {
+20
tools/testing/selftests/bpf/progs/verifier_jit_inline.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include <vmlinux.h> 4 + #include <bpf/bpf_helpers.h> 5 + #include "bpf_misc.h" 6 + 7 + SEC("fentry/bpf_fentry_test1") 8 + __success __retval(0) 9 + __arch_x86_64 10 + __jited(" addq %gs:{{.*}}, %rax") 11 + __arch_arm64 12 + __jited(" mrs x7, SP_EL0") 13 + int inline_bpf_get_current_task(void) 14 + { 15 + bpf_get_current_task(); 16 + 17 + return 0; 18 + } 19 + 20 + char _license[] SEC("license") = "GPL";