[PATCH bpf v2] riscv, bpf: Emit fixed-length instructions for BPF_PSEUDO_FUNC

Björn Töpel bjorn at kernel.org
Fri Dec 2 02:54:54 PST 2022


Pu Lehui <pulehui at huaweicloud.com> writes:

> From: Pu Lehui <pulehui at huawei.com>
>
> For BPF_PSEUDO_FUNC instruction, verifier will refill imm with
> correct addresses of bpf_calls and then run last pass of JIT.
> Since the emit_imm of RV64 is variable-length, which will emit
> appropriate length instructions accorroding to the imm, it may
> broke ctx->offset, and lead to unpredictable problem, such as
> inaccurate jump. So let's fix it with fixed-length instructions.
>
> Fixes: 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper")
> Signed-off-by: Pu Lehui <pulehui at huawei.com>
> Suggested-by: Björn Töpel <bjorn at rivosinc.com>
> ---
>  arch/riscv/net/bpf_jit_comp64.c | 20 +++++++++++++++++++-
>  1 file changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> index eb99df41fa33..9723f34f7a06 100644
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
> @@ -139,6 +139,19 @@ static bool in_auipc_jalr_range(s64 val)
>  		val < ((1L << 31) - (1L << 11));
>  }
>  
> +/* Emit fixed-length instructions for address */
> +static void emit_addr(u8 rd, u64 addr, struct rv_jit_context *ctx)
> +{
> +	u64 ip = (u64)(ctx->insns + ctx->ninsns);
> +	s64 off = addr - ip;
> +	s64 upper = (off + (1 << 11)) >> 12;
> +	s64 lower = ((off & 0xfff) << 52) >> 52;
> +
> +	emit(rv_auipc(rd, upper), ctx);
> +	emit(rv_addi(rd, rd, lower), ctx);
> +}

Nice! Two instructions are better than 6! :-)

One final thing. Please add a sanity check, that the range is correct,
e.g.:

  if (!(addr && in_auipc_addi_range(off)))
     return -1;

Have a look at emit_jump_and_link().


Thanks!
Björn



More information about the linux-riscv mailing list