arch/loongarch/include/asm/inst.h | 3 + arch/loongarch/kernel/inst.c | 60 ++++ arch/loongarch/net/bpf_jit.c | 521 +++++++++++++++++++++++++++++- arch/loongarch/net/bpf_jit.h | 6 + 4 files changed, 589 insertions(+), 1 deletion(-)
v4: 1. Delete the #3 patch of version V3. 2. Add 5 NOP instructions in build_prologue(). Reserve space for the move_imm + jirl instruction. 3. Differentiate between direct jumps and ftrace jumps of trampoline: direct jumps skip 5 instructions. ftrace jumps skip 2 instructions. 4. Remove the generation of BL jump instructions in emit_jump_and_link(). After the trampoline ends, it will jump to the specified register. The BL instruction writes PC+4 to r1 instead of allowing the specification of rd. ----------------------------------------------------------------------- Historical Version: v3: 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. 2. Align the size calculated by arch_bpf_trampoline_size to page boundaries. 3. Add the flush icache operation to larch_insn_text_copy. 4. Unify the implementation of bpf_arch_xxx into the patch "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". 5. Change the patch order. Move the patch "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". URL for version v3: https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ --------- v2: 1. Change the fixmap in the instruction copy function to set_memory_xxx. 2. Change the implementation method of the following code. - arch_alloc_bpf_trampoline - arch_free_bpf_trampoline Use the BPF core's allocation and free functions. - bpf_arch_text_invalidate Operate with the function larch_insn_text_copy that carries memory attribute modifications. 3. Correct the incorrect code formatting. URL for version v2: https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ --------- v1: Support trampoline for LoongArch. The following feature tests have been completed: 1. fentry 2. fexit 3. fmod_ret TODO: The support for the struct_ops feature will be provided in subsequent patches. URL for version v1: https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ ----------------------------------------------------------------------- Chenghao Duan (4): LoongArch: Add larch_insn_gen_{beq,bne} helpers LoongArch: BPF: Update the code to rename validate_code to validate_ctx LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch LoongArch: BPF: Add bpf trampoline support for Loongarch Tiezhu Yang (1): LoongArch: BPF: Add struct ops support for trampoline arch/loongarch/include/asm/inst.h | 3 + arch/loongarch/kernel/inst.c | 60 ++++ arch/loongarch/net/bpf_jit.c | 521 +++++++++++++++++++++++++++++- arch/loongarch/net/bpf_jit.h | 6 + 4 files changed, 589 insertions(+), 1 deletion(-) -- 2.25.1
Hi Chenghao, Huacai, Tuezhu, I first discovered this Loongarch BPF trampoline issue when debugging MPTCP BPF selftests on a Loongarch machine last June (see my commit eef0532e900c "selftests/bpf: Null checks for links in bpf_tcp_ca"), and reported it to Huachui. Tiezhu and I started implementing BPF trampoline last June. I also called on more Chinese kernel engineers to participate in the development of the Loongarch BPF trampoline at the openEuler Developer Day 2024 and CLSF 2024 conferences. Although this work was finally handed over to Chenghao, it is also necessary to mention me as the reporter and our early developers in the commit log. Thanks, -Geliang On Thu, 2025-07-24 at 22:19 +0800, Chenghao Duan wrote: > v4: > 1. Delete the #3 patch of version V3. > > 2. Add 5 NOP instructions in build_prologue(). > Reserve space for the move_imm + jirl instruction. > > 3. Differentiate between direct jumps and ftrace jumps of trampoline: > direct jumps skip 5 instructions. > ftrace jumps skip 2 instructions. > > 4. Remove the generation of BL jump instructions in > emit_jump_and_link(). > After the trampoline ends, it will jump to the specified register. > The BL instruction writes PC+4 to r1 instead of allowing the > specification of rd. > > --------------------------------------------------------------------- > -- > Historical Version: > v3: > 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. > > 2. Align the size calculated by arch_bpf_trampoline_size to page > boundaries. > > 3. Add the flush icache operation to larch_insn_text_copy. > > 4. Unify the implementation of bpf_arch_xxx into the patch > "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". > > 5. Change the patch order. Move the patch > "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before > "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". > > URL for version v3: > https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ > --------- > v2: > 1. Change the fixmap in the instruction copy function to > set_memory_xxx. > > 2. Change the implementation method of the following code. > - arch_alloc_bpf_trampoline > - arch_free_bpf_trampoline > Use the BPF core's allocation and free functions. > > - bpf_arch_text_invalidate > Operate with the function larch_insn_text_copy that carries > memory attribute modifications. > > 3. Correct the incorrect code formatting. > > URL for version v2: > https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ > --------- > v1: > Support trampoline for LoongArch. The following feature tests have > been > completed: > 1. fentry > 2. fexit > 3. fmod_ret > > TODO: The support for the struct_ops feature will be provided in > subsequent patches. > > URL for version v1: > https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ > --------------------------------------------------------------------- > -- > > Chenghao Duan (4): > LoongArch: Add larch_insn_gen_{beq,bne} helpers > LoongArch: BPF: Update the code to rename validate_code to > validate_ctx > LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch > LoongArch: BPF: Add bpf trampoline support for Loongarch > > Tiezhu Yang (1): > LoongArch: BPF: Add struct ops support for trampoline > > arch/loongarch/include/asm/inst.h | 3 + > arch/loongarch/kernel/inst.c | 60 ++++ > arch/loongarch/net/bpf_jit.c | 521 > +++++++++++++++++++++++++++++- > arch/loongarch/net/bpf_jit.h | 6 + > 4 files changed, 589 insertions(+), 1 deletion(-)
On Sun, Jul 27, 2025 at 9:00 AM Geliang Tang <geliang@kernel.org> wrote: > > Hi Chenghao, Huacai, Tuezhu, > > I first discovered this Loongarch BPF trampoline issue when debugging > MPTCP BPF selftests on a Loongarch machine last June (see my commit > eef0532e900c "selftests/bpf: Null checks for links in bpf_tcp_ca"), and > reported it to Huachui. Tiezhu and I started implementing BPF > trampoline last June. I also called on more Chinese kernel engineers to > participate in the development of the Loongarch BPF trampoline at the > openEuler Developer Day 2024 and CLSF 2024 conferences. Although this > work was finally handed over to Chenghao, it is also necessary to > mention me as the reporter and our early developers in the commit log. Thank you for reminding me, since the 3rd patch need to be fixed, chenghao can do that as soon as possible, then adjust the SOB together in V5. Huacai > > Thanks, > -Geliang > > On Thu, 2025-07-24 at 22:19 +0800, Chenghao Duan wrote: > > v4: > > 1. Delete the #3 patch of version V3. > > > > 2. Add 5 NOP instructions in build_prologue(). > > Reserve space for the move_imm + jirl instruction. > > > > 3. Differentiate between direct jumps and ftrace jumps of trampoline: > > direct jumps skip 5 instructions. > > ftrace jumps skip 2 instructions. > > > > 4. Remove the generation of BL jump instructions in > > emit_jump_and_link(). > > After the trampoline ends, it will jump to the specified register. > > The BL instruction writes PC+4 to r1 instead of allowing the > > specification of rd. > > > > --------------------------------------------------------------------- > > -- > > Historical Version: > > v3: > > 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. > > > > 2. Align the size calculated by arch_bpf_trampoline_size to page > > boundaries. > > > > 3. Add the flush icache operation to larch_insn_text_copy. > > > > 4. Unify the implementation of bpf_arch_xxx into the patch > > "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". > > > > 5. Change the patch order. Move the patch > > "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before > > "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". > > > > URL for version v3: > > https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ > > --------- > > v2: > > 1. Change the fixmap in the instruction copy function to > > set_memory_xxx. > > > > 2. Change the implementation method of the following code. > > - arch_alloc_bpf_trampoline > > - arch_free_bpf_trampoline > > Use the BPF core's allocation and free functions. > > > > - bpf_arch_text_invalidate > > Operate with the function larch_insn_text_copy that carries > > memory attribute modifications. > > > > 3. Correct the incorrect code formatting. > > > > URL for version v2: > > https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ > > --------- > > v1: > > Support trampoline for LoongArch. The following feature tests have > > been > > completed: > > 1. fentry > > 2. fexit > > 3. fmod_ret > > > > TODO: The support for the struct_ops feature will be provided in > > subsequent patches. > > > > URL for version v1: > > https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ > > --------------------------------------------------------------------- > > -- > > > > Chenghao Duan (4): > > LoongArch: Add larch_insn_gen_{beq,bne} helpers > > LoongArch: BPF: Update the code to rename validate_code to > > validate_ctx > > LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch > > LoongArch: BPF: Add bpf trampoline support for Loongarch > > > > Tiezhu Yang (1): > > LoongArch: BPF: Add struct ops support for trampoline > > > > arch/loongarch/include/asm/inst.h | 3 + > > arch/loongarch/kernel/inst.c | 60 ++++ > > arch/loongarch/net/bpf_jit.c | 521 > > +++++++++++++++++++++++++++++- > > arch/loongarch/net/bpf_jit.h | 6 + > > 4 files changed, 589 insertions(+), 1 deletion(-)
On Thu, Jul 24, 2025 at 7:19 AM Chenghao Duan <duanchenghao@kylinos.cn> wrote: > > v4: > 1. Delete the #3 patch of version V3. > > 2. Add 5 NOP instructions in build_prologue(). > Reserve space for the move_imm + jirl instruction. > > 3. Differentiate between direct jumps and ftrace jumps of trampoline: > direct jumps skip 5 instructions. > ftrace jumps skip 2 instructions. > > 4. Remove the generation of BL jump instructions in emit_jump_and_link(). > After the trampoline ends, it will jump to the specified register. > The BL instruction writes PC+4 to r1 instead of allowing the > specification of rd. > > ----------------------------------------------------------------------- > Historical Version: > v3: > 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. > > 2. Align the size calculated by arch_bpf_trampoline_size to page > boundaries. > > 3. Add the flush icache operation to larch_insn_text_copy. > > 4. Unify the implementation of bpf_arch_xxx into the patch > "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". > > 5. Change the patch order. Move the patch > "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before > "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". > > URL for version v3: > https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ > --------- > v2: > 1. Change the fixmap in the instruction copy function to set_memory_xxx. > > 2. Change the implementation method of the following code. > - arch_alloc_bpf_trampoline > - arch_free_bpf_trampoline > Use the BPF core's allocation and free functions. > > - bpf_arch_text_invalidate > Operate with the function larch_insn_text_copy that carries > memory attribute modifications. > > 3. Correct the incorrect code formatting. > > URL for version v2: > https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ > --------- > v1: > Support trampoline for LoongArch. The following feature tests have been > completed: > 1. fentry > 2. fexit > 3. fmod_ret > > TODO: The support for the struct_ops feature will be provided in > subsequent patches. > > URL for version v1: > https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ > ----------------------------------------------------------------------- > > Chenghao Duan (4): > LoongArch: Add larch_insn_gen_{beq,bne} helpers > LoongArch: BPF: Update the code to rename validate_code to > validate_ctx > LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch > LoongArch: BPF: Add bpf trampoline support for Loongarch > > Tiezhu Yang (1): > LoongArch: BPF: Add struct ops support for trampoline > > arch/loongarch/include/asm/inst.h | 3 + > arch/loongarch/kernel/inst.c | 60 ++++ > arch/loongarch/net/bpf_jit.c | 521 +++++++++++++++++++++++++++++- > arch/loongarch/net/bpf_jit.h | 6 + > 4 files changed, 589 insertions(+), 1 deletion(-) > > -- > 2.25.1 > Tested the whole patch series and it resolved the xdp-tool xdp-filter issue [root@fedora ~]# xdp-loader status CURRENT XDP PROGRAM STATUS: Interface Prio Program name Mode ID Tag Chain actions -------------------------------------------------------------------------------------- lo xdp_dispatcher skb 53 4d7e87c0d30db711 => 10 xdpfilt_alw_all 62 320c53c06933a8fa XDP_PASS dummy0 <No XDP program loaded!> sit0 <No XDP program loaded!> enp0s3f0 <No XDP program loaded!> wlp3s0 <No XDP program loaded!> you can add Tested-by: Vincent Li <vincent.mc.li@gmail.com>
On Thu, Jul 24, 2025 at 08:30:35AM -0700, Vincent Li wrote: > On Thu, Jul 24, 2025 at 7:19 AM Chenghao Duan <duanchenghao@kylinos.cn> wrote: > > > > v4: > > 1. Delete the #3 patch of version V3. > > > > 2. Add 5 NOP instructions in build_prologue(). > > Reserve space for the move_imm + jirl instruction. > > > > 3. Differentiate between direct jumps and ftrace jumps of trampoline: > > direct jumps skip 5 instructions. > > ftrace jumps skip 2 instructions. > > > > 4. Remove the generation of BL jump instructions in emit_jump_and_link(). > > After the trampoline ends, it will jump to the specified register. > > The BL instruction writes PC+4 to r1 instead of allowing the > > specification of rd. > > > > ----------------------------------------------------------------------- > > Historical Version: > > v3: > > 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. > > > > 2. Align the size calculated by arch_bpf_trampoline_size to page > > boundaries. > > > > 3. Add the flush icache operation to larch_insn_text_copy. > > > > 4. Unify the implementation of bpf_arch_xxx into the patch > > "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". > > > > 5. Change the patch order. Move the patch > > "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before > > "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". > > > > URL for version v3: > > https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ > > --------- > > v2: > > 1. Change the fixmap in the instruction copy function to set_memory_xxx. > > > > 2. Change the implementation method of the following code. > > - arch_alloc_bpf_trampoline > > - arch_free_bpf_trampoline > > Use the BPF core's allocation and free functions. > > > > - bpf_arch_text_invalidate > > Operate with the function larch_insn_text_copy that carries > > memory attribute modifications. > > > > 3. Correct the incorrect code formatting. > > > > URL for version v2: > > https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ > > --------- > > v1: > > Support trampoline for LoongArch. The following feature tests have been > > completed: > > 1. fentry > > 2. fexit > > 3. fmod_ret > > > > TODO: The support for the struct_ops feature will be provided in > > subsequent patches. > > > > URL for version v1: > > https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ > > ----------------------------------------------------------------------- > > > > Chenghao Duan (4): > > LoongArch: Add larch_insn_gen_{beq,bne} helpers > > LoongArch: BPF: Update the code to rename validate_code to > > validate_ctx > > LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch > > LoongArch: BPF: Add bpf trampoline support for Loongarch > > > > Tiezhu Yang (1): > > LoongArch: BPF: Add struct ops support for trampoline > > > > arch/loongarch/include/asm/inst.h | 3 + > > arch/loongarch/kernel/inst.c | 60 ++++ > > arch/loongarch/net/bpf_jit.c | 521 +++++++++++++++++++++++++++++- > > arch/loongarch/net/bpf_jit.h | 6 + > > 4 files changed, 589 insertions(+), 1 deletion(-) > > > > -- > > 2.25.1 > > > > Tested the whole patch series and it resolved the xdp-tool xdp-filter issue > > [root@fedora ~]# xdp-loader status > CURRENT XDP PROGRAM STATUS: > > Interface Prio Program name Mode ID Tag > Chain actions > -------------------------------------------------------------------------------------- > lo xdp_dispatcher skb 53 4d7e87c0d30db711 > => 10 xdpfilt_alw_all 62 > 320c53c06933a8fa XDP_PASS > dummy0 <No XDP program loaded!> > sit0 <No XDP program loaded!> > enp0s3f0 <No XDP program loaded!> > wlp3s0 <No XDP program loaded!> > > you can add Tested-by: Vincent Li <vincent.mc.li@gmail.com> Hi Vincent, Okay, thank you very much for your support. The existing patch has included "Tested-by: Vincent Li vincent.mc.li@gmail.com". Brs Chenghao
On 7/25/25 12:18 PM, Chenghao Duan wrote: > On Thu, Jul 24, 2025 at 08:30:35AM -0700, Vincent Li wrote: >> On Thu, Jul 24, 2025 at 7:19 AM Chenghao Duan <duanchenghao@kylinos.cn> wrote: >>> >>> v4: >>> 1. Delete the #3 patch of version V3. >>> >>> 2. Add 5 NOP instructions in build_prologue(). >>> Reserve space for the move_imm + jirl instruction. >>> >>> 3. Differentiate between direct jumps and ftrace jumps of trampoline: >>> direct jumps skip 5 instructions. >>> ftrace jumps skip 2 instructions. >>> >>> 4. Remove the generation of BL jump instructions in emit_jump_and_link(). >>> After the trampoline ends, it will jump to the specified register. >>> The BL instruction writes PC+4 to r1 instead of allowing the >>> specification of rd. >>> >>> ----------------------------------------------------------------------- >>> Historical Version: >>> v3: >>> 1. Patch 0003 adds EXECMEM_BPF memory type to the execmem subsystem. >>> >>> 2. Align the size calculated by arch_bpf_trampoline_size to page >>> boundaries. >>> >>> 3. Add the flush icache operation to larch_insn_text_copy. >>> >>> 4. Unify the implementation of bpf_arch_xxx into the patch >>> "0004-LoongArch-BPF-Add-bpf_arch_xxxxx-support-for-Loong.patch". >>> >>> 5. Change the patch order. Move the patch >>> "0002-LoongArch-BPF-Update-the-code-to-rename-validate_.patch" before >>> "0005-LoongArch-BPF-Add-bpf-trampoline-support-for-Loon.patch". >>> >>> URL for version v3: >>> https://lore.kernel.org/all/20250709055029.723243-1-duanchenghao@kylinos.cn/ >>> --------- >>> v2: >>> 1. Change the fixmap in the instruction copy function to set_memory_xxx. >>> >>> 2. Change the implementation method of the following code. >>> - arch_alloc_bpf_trampoline >>> - arch_free_bpf_trampoline >>> Use the BPF core's allocation and free functions. >>> >>> - bpf_arch_text_invalidate >>> Operate with the function larch_insn_text_copy that carries >>> memory attribute modifications. >>> >>> 3. Correct the incorrect code formatting. >>> >>> URL for version v2: >>> https://lore.kernel.org/all/20250618105048.1510560-1-duanchenghao@kylinos.cn/ >>> --------- >>> v1: >>> Support trampoline for LoongArch. The following feature tests have been >>> completed: >>> 1. fentry >>> 2. fexit >>> 3. fmod_ret >>> >>> TODO: The support for the struct_ops feature will be provided in >>> subsequent patches. >>> >>> URL for version v1: >>> https://lore.kernel.org/all/20250611035952.111182-1-duanchenghao@kylinos.cn/ >>> ----------------------------------------------------------------------- >>> >>> Chenghao Duan (4): >>> LoongArch: Add larch_insn_gen_{beq,bne} helpers >>> LoongArch: BPF: Update the code to rename validate_code to >>> validate_ctx >>> LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch >>> LoongArch: BPF: Add bpf trampoline support for Loongarch >>> >>> Tiezhu Yang (1): >>> LoongArch: BPF: Add struct ops support for trampoline >>> >>> arch/loongarch/include/asm/inst.h | 3 + >>> arch/loongarch/kernel/inst.c | 60 ++++ >>> arch/loongarch/net/bpf_jit.c | 521 +++++++++++++++++++++++++++++- >>> arch/loongarch/net/bpf_jit.h | 6 + >>> 4 files changed, 589 insertions(+), 1 deletion(-) >>> >>> -- >>> 2.25.1 >>> >> >> Tested the whole patch series and it resolved the xdp-tool xdp-filter issue >> >> [root@fedora ~]# xdp-loader status >> CURRENT XDP PROGRAM STATUS: >> >> Interface Prio Program name Mode ID Tag >> Chain actions >> -------------------------------------------------------------------------------------- >> lo xdp_dispatcher skb 53 4d7e87c0d30db711 >> => 10 xdpfilt_alw_all 62 >> 320c53c06933a8fa XDP_PASS >> dummy0 <No XDP program loaded!> >> sit0 <No XDP program loaded!> >> enp0s3f0 <No XDP program loaded!> >> wlp3s0 <No XDP program loaded!> >> >> you can add Tested-by: Vincent Li <vincent.mc.li@gmail.com> > > Hi Vincent, > > Okay, thank you very much for your support. The existing patch has > included "Tested-by: Vincent Li vincent.mc.li@gmail.com". Huacai, I presume you'll route this series to Linus, correct? Thanks, Daniel
© 2016 - 2025 Red Hat, Inc.