Re: [PATCH v2] riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak
From: Masami Hiramatsu (Google)
Date: Mon Nov 09 2026 - 07:09:17 EST
On Tue, 13 Oct 2026 10:36:05 +0800, Xiaofeng Yuan <yuanxiaofeng@xxxxxxxxxxxxxxxxxx> wrote:
> When a kprobe is removed, arch_remove_kprobe() does not free the
> instruction slot allocated by arch_prepare_kprobe(). This causes
> a memory leak every time a kprobe is unregistered.
>
> Other architectures such as arm64, x86, and powerpc all free the
> slot in their arch_remove_kprobe() implementations. Add the
> missing free_insn_slot() call for RISC-V.
>
> Registered and unregistered a kprobe on kernel_clone in a loop
> inside a QEMU (RISC-V virt) guest: before the fix the number of
> kprobe_insn_page entries in /proc/kallsyms grew by one every 512
> iterations (each insn-slot page holds 512 eight-byte slots), and
> after the fix it stays at 1.
>
> Signed-off-by: Xiaofeng Yuan <yuanxiaofeng@xxxxxxxxxxxxxxxxxx>
Since this fixes a memory leak present since RISC-V kprobes support was
introduced, please add a Fixes tag (and Cc stable):
Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
Cc: stable@xxxxxxxxxxxxxxx
And
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
from kprobes point of view.
Thanks,
> ---
> Changes in v2:
> - Drop the stray blank line before the closing brace.
> - Extend the commit message with a reproduction/verification note.
> - Rebase onto linux-next 20261009.
>
> arch/riscv/kernel/probes/kprobes.c | 4 ++++
> 1 file changed, 4 insertions(+)
--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>