Skip to content

Commit 2ddec2c

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
riscv, bpf: inline bpf_get_smp_processor_id()
Inline the calls to bpf_get_smp_processor_id() in the riscv bpf jit. RISCV saves the pointer to the CPU's task_struct in the TP (thread pointer) register. This makes it trivial to get the CPU's processor id. As thread_info is the first member of task_struct, we can read the processor id from TP + offsetof(struct thread_info, cpu). RISCV64 JIT output for `call bpf_get_smp_processor_id` ====================================================== Before After -------- ------- auipc t1,0x848c ld a5,32(tp) jalr 604(t1) mv a5,a0 Benchmark using [1] on Qemu. ./benchs/run_bench_trigger.sh glob-arr-inc arr-inc hash-inc +---------------+------------------+------------------+--------------+ | Name | Before | After | % change | |---------------+------------------+------------------+--------------| | glob-arr-inc | 1.077 ± 0.006M/s | 1.336 ± 0.010M/s | + 24.04% | | arr-inc | 1.078 ± 0.002M/s | 1.332 ± 0.015M/s | + 23.56% | | hash-inc | 0.494 ± 0.004M/s | 0.653 ± 0.001M/s | + 32.18% | +---------------+------------------+------------------+--------------+ NOTE: This benchmark includes changes from this patch and the previous patch that implemented the per-cpu insn. [1] anakryiko/linux@8dec900975ef Signed-off-by: Puranjay Mohan <puranjay@kernel.org> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Acked-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Björn Töpel <bjorn@kernel.org> Link: https://lore.kernel.org/r/20240502151854.9810-3-puranjay@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 19c56d4 commit 2ddec2c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,22 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
14931493
bool fixed_addr;
14941494
u64 addr;
14951495

1496+
/* Inline calls to bpf_get_smp_processor_id()
1497+
*
1498+
* RV_REG_TP holds the address of the current CPU's task_struct and thread_info is
1499+
* at offset 0 in task_struct.
1500+
* Load cpu from thread_info:
1501+
* Set R0 to ((struct thread_info *)(RV_REG_TP))->cpu
1502+
*
1503+
* This replicates the implementation of raw_smp_processor_id() on RISCV
1504+
*/
1505+
if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
1506+
/* Load current CPU number in R0 */
1507+
emit_ld(bpf_to_rv_reg(BPF_REG_0, ctx), offsetof(struct thread_info, cpu),
1508+
RV_REG_TP, ctx);
1509+
break;
1510+
}
1511+
14961512
mark_call(ctx);
14971513
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
14981514
&addr, &fixed_addr);
@@ -2062,3 +2078,13 @@ bool bpf_jit_supports_percpu_insn(void)
20622078
{
20632079
return true;
20642080
}
2081+
2082+
bool bpf_jit_inlines_helper_call(s32 imm)
2083+
{
2084+
switch (imm) {
2085+
case BPF_FUNC_get_smp_processor_id:
2086+
return true;
2087+
default:
2088+
return false;
2089+
}
2090+
}

include/linux/filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
993993
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
994994
void bpf_jit_compile(struct bpf_prog *prog);
995995
bool bpf_jit_needs_zext(void);
996+
bool bpf_jit_inlines_helper_call(s32 imm);
996997
bool bpf_jit_supports_subprog_tailcalls(void);
997998
bool bpf_jit_supports_percpu_insn(void);
998999
bool bpf_jit_supports_kfunc_call(void);

kernel/bpf/core.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,17 @@ bool __weak bpf_jit_needs_zext(void)
29412941
return false;
29422942
}
29432943

2944+
/* Return true if the JIT inlines the call to the helper corresponding to
2945+
* the imm.
2946+
*
2947+
* The verifier will not patch the insn->imm for the call to the helper if
2948+
* this returns true.
2949+
*/
2950+
bool __weak bpf_jit_inlines_helper_call(s32 imm)
2951+
{
2952+
return false;
2953+
}
2954+
29442955
/* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls. */
29452956
bool __weak bpf_jit_supports_subprog_tailcalls(void)
29462957
{

kernel/bpf/verifier.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19996,6 +19996,10 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
1999619996
goto next_insn;
1999719997
}
1999819998

19999+
/* Skip inlining the helper call if the JIT does it. */
20000+
if (bpf_jit_inlines_helper_call(insn->imm))
20001+
goto next_insn;
20002+
1999920003
if (insn->imm == BPF_FUNC_get_route_realm)
2000020004
prog->dst_needed = 1;
2000120005
if (insn->imm == BPF_FUNC_get_prandom_u32)

0 commit comments

Comments
 (0)