Skip to content

Commit c9cbde1

Browse files
seehearfeelKernel Patches Daemon
authored andcommitted
LoongArch: BPF: Inline bpf_get_smp_processor_id() helper
The pointer to thread_info is always available in the $tp register, the call to bpf_get_smp_processor_id() can be inlined into a single load instruction. (1) Here is the sample test.c: #include <linux/bpf.h> #include <bpf/bpf_helpers.h> SEC("raw_tp/sys_enter") int test_cpuid(void *ctx) { return bpf_get_smp_processor_id(); } char _license[] SEC("license") = "GPL"; (2) Here are the test steps: sudo yum install libbpf-devel kernel-devel bpftool clang -target bpf -O2 -c test.c -o test.o sudo sysctl -w net.core.bpf_jit_enable=1 sudo bpftool prog show name test_cpuid sudo rm -f /sys/fs/bpf/test_cpuid sudo bpftool prog load test.o /sys/fs/bpf/test_cpuid ID=$(sudo bpftool prog show pinned /sys/fs/bpf/test_cpuid | grep -oE '^[0-9]+') sudo bpftool prog dump jited id $ID (3) Here are the test results: Before: 6 instructions ... 64: lu12i.w $t1, 1213 68: ori $t1, $t1, 1680 6c: lu32i.d $t1, 0 70: lu52i.d $t1, $t1, -1792 74: jirl $ra, $t1, 0 78: move $a5, $a0 ... After: 1 instruction ... 64: ld.wu $a5, $tp, 16 ... This is similar with commit 2ddec2c ("riscv, bpf: inline bpf_get_smp_processor_id()"). Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
1 parent f5dacf0 commit c9cbde1

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

arch/loongarch/net/bpf_jit.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (C) 2022 Loongson Technology Corporation Limited
66
*/
77
#include <linux/memory.h>
8+
#include <asm/asm-offsets.h>
89
#include "bpf_jit.h"
910

1011
#define LOONGARCH_MAX_REG_ARGS 8
@@ -1154,6 +1155,12 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
11541155
break;
11551156
}
11561157

1158+
/* Implement helper call to bpf_get_smp_processor_id() inline */
1159+
if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
1160+
emit_insn(ctx, ldwu, regmap[BPF_REG_0], LOONGARCH_GPR_TP, TI_CPU);
1161+
break;
1162+
}
1163+
11571164
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
11581165
&func_addr, &func_addr_fixed);
11591166
if (ret < 0)
@@ -2384,6 +2391,7 @@ bool bpf_jit_inlines_helper_call(s32 imm)
23842391
switch (imm) {
23852392
case BPF_FUNC_get_current_task:
23862393
case BPF_FUNC_get_current_task_btf:
2394+
case BPF_FUNC_get_smp_processor_id:
23872395
return true;
23882396
default:
23892397
return false;

0 commit comments

Comments
 (0)