Skip to content

Commit 596f2e6

Browse files
Pu Lehuiborkmann
authored andcommitted
riscv, bpf: Add bpf_arch_text_poke support for RV64
Implement bpf_arch_text_poke for RV64. For call scenario, to make BPF trampoline compatible with the kernel and BPF context, we follow the framework of RV64 ftrace to reserve 4 nops for BPF programs as function entry, and use auipc+jalr instructions for function call. However, since auipc+jalr call instruction is non-atomic operation, we need to use stop-machine to make sure instructions patching in atomic context. Also, we use auipc+jalr pair and need to patch in stop-machine context for jump scenario. Signed-off-by: Pu Lehui <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Björn Töpel <[email protected]> Acked-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0fd1fd0 commit 596f2e6

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

arch/riscv/net/bpf_jit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,11 @@ static inline u32 rv_fence(u8 pred, u8 succ)
573573
return rv_i_insn(imm11_0, 0, 0, 0, 0xf);
574574
}
575575

576+
static inline u32 rv_nop(void)
577+
{
578+
return rv_i_insn(0, 0, 0, 0, 0x13);
579+
}
580+
576581
/* RVC instrutions. */
577582

578583
static inline u16 rvc_addi4spn(u8 rd, u32 imm10)

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <linux/bitfield.h>
99
#include <linux/bpf.h>
1010
#include <linux/filter.h>
11+
#include <linux/memory.h>
12+
#include <linux/stop_machine.h>
1113
#include "bpf_jit.h"
1214

1315
#define RV_REG_TCC RV_REG_A6
@@ -238,7 +240,7 @@ static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
238240
if (!is_tail_call)
239241
emit_mv(RV_REG_A0, RV_REG_A5, ctx);
240242
emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
241-
is_tail_call ? 4 : 0, /* skip TCC init */
243+
is_tail_call ? 20 : 0, /* skip reserved nops and TCC init */
242244
ctx);
243245
}
244246

@@ -615,6 +617,84 @@ static int add_exception_handler(const struct bpf_insn *insn,
615617
return 0;
616618
}
617619

620+
static int gen_call_or_nops(void *target, void *ip, u32 *insns)
621+
{
622+
s64 rvoff;
623+
int i, ret;
624+
struct rv_jit_context ctx;
625+
626+
ctx.ninsns = 0;
627+
ctx.insns = (u16 *)insns;
628+
629+
if (!target) {
630+
for (i = 0; i < 4; i++)
631+
emit(rv_nop(), &ctx);
632+
return 0;
633+
}
634+
635+
rvoff = (s64)(target - (ip + 4));
636+
emit(rv_sd(RV_REG_SP, -8, RV_REG_RA), &ctx);
637+
ret = emit_jump_and_link(RV_REG_RA, rvoff, false, &ctx);
638+
if (ret)
639+
return ret;
640+
emit(rv_ld(RV_REG_RA, -8, RV_REG_SP), &ctx);
641+
642+
return 0;
643+
}
644+
645+
static int gen_jump_or_nops(void *target, void *ip, u32 *insns)
646+
{
647+
s64 rvoff;
648+
struct rv_jit_context ctx;
649+
650+
ctx.ninsns = 0;
651+
ctx.insns = (u16 *)insns;
652+
653+
if (!target) {
654+
emit(rv_nop(), &ctx);
655+
emit(rv_nop(), &ctx);
656+
return 0;
657+
}
658+
659+
rvoff = (s64)(target - ip);
660+
return emit_jump_and_link(RV_REG_ZERO, rvoff, false, &ctx);
661+
}
662+
663+
int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
664+
void *old_addr, void *new_addr)
665+
{
666+
u32 old_insns[4], new_insns[4];
667+
bool is_call = poke_type == BPF_MOD_CALL;
668+
int (*gen_insns)(void *target, void *ip, u32 *insns);
669+
int ninsns = is_call ? 4 : 2;
670+
int ret;
671+
672+
if (!is_bpf_text_address((unsigned long)ip))
673+
return -ENOTSUPP;
674+
675+
gen_insns = is_call ? gen_call_or_nops : gen_jump_or_nops;
676+
677+
ret = gen_insns(old_addr, ip, old_insns);
678+
if (ret)
679+
return ret;
680+
681+
if (memcmp(ip, old_insns, ninsns * 4))
682+
return -EFAULT;
683+
684+
ret = gen_insns(new_addr, ip, new_insns);
685+
if (ret)
686+
return ret;
687+
688+
cpus_read_lock();
689+
mutex_lock(&text_mutex);
690+
if (memcmp(ip, new_insns, ninsns * 4))
691+
ret = patch_text(ip, new_insns, ninsns);
692+
mutex_unlock(&text_mutex);
693+
cpus_read_unlock();
694+
695+
return ret;
696+
}
697+
618698
int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
619699
bool extra_pass)
620700
{
@@ -1266,7 +1346,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
12661346

12671347
void bpf_jit_build_prologue(struct rv_jit_context *ctx)
12681348
{
1269-
int stack_adjust = 0, store_offset, bpf_stack_adjust;
1349+
int i, stack_adjust = 0, store_offset, bpf_stack_adjust;
12701350

12711351
bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
12721352
if (bpf_stack_adjust)
@@ -1293,6 +1373,10 @@ void bpf_jit_build_prologue(struct rv_jit_context *ctx)
12931373

12941374
store_offset = stack_adjust - 8;
12951375

1376+
/* reserve 4 nop insns */
1377+
for (i = 0; i < 4; i++)
1378+
emit(rv_nop(), ctx);
1379+
12961380
/* First instruction is always setting the tail-call-counter
12971381
* (TCC) register. This instruction is skipped for tail calls.
12981382
* Force using a 4-byte (non-compressed) instruction.

0 commit comments

Comments
 (0)