Re: [PATCH v5 6/9] riscv/kprobe: Add code to check if kprobe can be optimized

From: Björn Töpel
Date: Mon Jan 02 2023 - 13:04:13 EST


Chen Guokai <chenguokai17@xxxxxxxxxxxxxxxx> writes:

> From: Liao Chang <liaochang1@xxxxxxxxxx>

> diff --git a/arch/riscv/kernel/probes/opt.c b/arch/riscv/kernel/probes/opt.c
> index a0d2ab39e3fa..258a283c906d 100644
> --- a/arch/riscv/kernel/probes/opt.c
> +++ b/arch/riscv/kernel/probes/opt.c
> @@ -271,15 +271,103 @@ static void find_free_registers(struct kprobe *kp, struct optimized_kprobe *op,
> *ra = (kw == 1UL) ? 0 : __builtin_ctzl(kw & ~1UL);
> }
>
> +static bool insn_jump_into_range(unsigned long addr, unsigned long start,
> + unsigned long end)
> +{
> + kprobe_opcode_t insn = *(kprobe_opcode_t *)addr;
> + unsigned long target, offset = GET_INSN_LENGTH(insn);
> +
> +#ifdef CONFIG_RISCV_ISA_C
> + if (offset == RVC_INSN_LEN) {
> + if (riscv_insn_is_c_beqz(insn) || riscv_insn_is_c_bnez(insn))
> + target = addr + rvc_branch_imme(insn);
> + else if (riscv_insn_is_c_jal(insn) || riscv_insn_is_c_j(insn))
> + target = addr + rvc_jal_imme(insn);
> + else
> + target = 0;
> + return (target >= start) && (target < end);
> + }
> +#endif
> +
> + if (riscv_insn_is_branch(insn))
> + target = addr + rvi_branch_imme(insn);
> + else if (riscv_insn_is_jal(insn))
> + target = addr + rvi_jal_imme(insn);
> + else
> + target = 0;
> + return (target >= start) && (target < end);
> +}
> +
> +static int search_copied_insn(unsigned long paddr, struct optimized_kprobe *op)
> +{
> + int i = 1;
> + unsigned long offset = GET_INSN_LENGTH(*(kprobe_opcode_t *)paddr);
> +
> + while ((i++ < MAX_COPIED_INSN) && (offset < 2 * RVI_INSN_LEN)) {
> + if (riscv_probe_decode_insn((probe_opcode_t *)paddr + offset,
> + NULL) != INSN_GOOD)

If the second argument is NULL, and the insn is auipc, we'll splat with
NULL-ptr exception.

Hmm, probe_opcode_t is u32, right? And GET_INSN_LENGTH() returns 4 or 2
...then the pointer arithmetic will be a mess?


Björn