LoongArch: BPF: Add dynamic code modification support
authorChenghao Duan <duanchenghao@kylinos.cn>
Tue, 5 Aug 2025 11:00:18 +0000 (19:00 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Tue, 5 Aug 2025 11:00:18 +0000 (19:00 +0800)
commit9fbd18cf4c69f512f7de3ab73235078f3e32ecec
treecb9f968dafa0a2544a74c262fff54806226217c9
parented1a1fe6ec5e73b23b310b434ace07d1e5060657
LoongArch: BPF: Add dynamic code modification support

This commit adds support for BPF dynamic code modification on the
LoongArch architecture:
1. Add bpf_arch_text_copy() for instruction block copying.
2. Add bpf_arch_text_poke() for runtime instruction patching.
3. Add bpf_arch_text_invalidate() for code invalidation.

On LoongArch, since symbol addresses in the direct mapping region can't
be reached via relative jump instructions from the paged mapping region,
we use the move_imm+jirl instruction pair as absolute jump instructions.
These require 2-5 instructions, so we reserve 5 NOP instructions in the
program as placeholders for function jumps.

The larch_insn_text_copy() function is solely used for BPF. And the use
of larch_insn_text_copy() requires PAGE_SIZE alignment. Currently, only
the size of the BPF trampoline is page-aligned.

Co-developed-by: George Guo <guodongtai@kylinos.cn>
Signed-off-by: George Guo <guodongtai@kylinos.cn>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/include/asm/inst.h
arch/loongarch/kernel/inst.c
arch/loongarch/net/bpf_jit.c