linux/arch/arm64
Xu Kuohai ddbe9ec550 bpf, arm64: Jit BPF_CALL to direct call when possible
Currently, BPF_CALL is always jited to indirect call. When target is
within the range of direct call, BPF_CALL can be jited to direct call.

For example, the following BPF_CALL

    call __htab_map_lookup_elem

is always jited to indirect call:

    mov     x10, #0xffffffffffff18f4
    movk    x10, #0x821, lsl #16
    movk    x10, #0x8000, lsl #32
    blr     x10

When the address of target __htab_map_lookup_elem is within the range of
direct call, the BPF_CALL can be jited to:

    bl      0xfffffffffd33bc98

This patch does such jit optimization by emitting arm64 direct calls for
BPF_CALL when possible, indirect calls otherwise.

Without this patch, the jit works as follows.

1. First pass
   A. Determine jited position and size for each bpf instruction.
   B. Computed the jited image size.

2. Allocate jited image with size computed in step 1.

3. Second pass
   A. Adjust jump offset for jump instructions
   B. Write the final image.

This works because, for a given bpf prog, regardless of where the jited
image is allocated, the jited result for each instruction is fixed. The
second pass differs from the first only in adjusting the jump offsets,
like changing "jmp imm1" to "jmp imm2", while the position and size of
the "jmp" instruction remain unchanged.

Now considering whether to jit BPF_CALL to arm64 direct or indirect call
instruction. The choice depends solely on the jump offset: direct call
if the jump offset is within 128MB, indirect call otherwise.

For a given BPF_CALL, the target address is known, so the jump offset is
decided by the jited address of the BPF_CALL instruction. In other words,
for a given bpf prog, the jited result for each BPF_CALL is determined
by its jited address.

The jited address for a BPF_CALL is the jited image address plus the
total jited size of all preceding instructions. For a given bpf prog,
there are clearly no BPF_CALL instructions before the first BPF_CALL
instruction. Since the jited result for all other instructions other
than BPF_CALL are fixed, the total jited size preceding the first
BPF_CALL is also fixed. Therefore, once the jited image is allocated,
the jited address for the first BPF_CALL is fixed.

Now that the jited result for the first BPF_CALL is fixed, the jited
results for all instructions preceding the second BPF_CALL are fixed.
So the jited address and result for the second BPF_CALL are also fixed.

Similarly, we can conclude that the jited addresses and results for all
subsequent BPF_CALL instructions are fixed.

This means that, for a given bpf prog, once the jited image is allocated,
the jited address and result for all instructions, including all BPF_CALL
instructions, are fixed.

Based on the observation, with this patch, the jit works as follows.

1. First pass
   Estimate the maximum jited image size. In this pass, all BPF_CALLs
   are jited to arm64 indirect calls since the jump offsets are unknown
   because the jited image is not allocated.

2. Allocate jited image with size estimated in step 1.

3. Second pass
   A. Determine the jited result for each BPF_CALL.
   B. Determine jited address and size for each bpf instruction.

4. Third pass
   A. Adjust jump offset for jump instructions.
   B. Write the final image.

Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Reviewed-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20240903094407.601107-1-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-09-04 11:51:06 -07:00
..
boot arm64: dts: ti: k3-j784s4-main: Correct McASP DMAs 2024-08-06 06:39:23 -05:00
configs sound updates for 6.11-rc1 2024-07-19 12:39:34 -07:00
crypto crypto: arm64 - add missing MODULE_DESCRIPTION() macros 2024-06-21 22:04:16 +10:00
hyperv x86/hyperv: Use Hyper-V entropy to seed guest random number generator 2024-03-18 22:01:52 +00:00
include arm64 fixes: 2024-08-16 17:02:32 -07:00
kernel arm64 fixes: 2024-08-16 17:02:32 -07:00
kvm KVM: arm64: vgic: Hold config_lock while tearing down a CPU interface 2024-08-08 16:58:22 +00:00
lib arm64: crypto: use CC_FLAGS_FPU for NEON CFLAGS 2024-05-19 14:36:18 -07:00
mm mm: provide mm_struct and address to huge_ptep_get() 2024-07-12 15:52:15 -07:00
net bpf, arm64: Jit BPF_CALL to direct call when possible 2024-09-04 11:51:06 -07:00
tools arm64 fixes for -rc1 2024-07-26 10:39:10 -07:00
xen
Kbuild
Kconfig arm64: errata: Expand speculative SSBS workaround (again) 2024-08-01 16:11:28 +01:00
Kconfig.debug
Kconfig.platforms Updates for the interrupt subsystem: 2024-07-22 13:52:05 -07:00
Makefile arm64: allow installing compressed image by default 2024-07-26 10:07:56 -07:00