All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak
@ 2026-11-20  7:08 ` Xiaofeng Yuan
  0 siblings, 0 replies; 2+ messages in thread
From: Xiaofeng Yuan @ 2026-11-20  7:08 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti
  Cc: Masami Hiramatsu, linux-riscv, linux-kernel, linux-trace-kernel,
	stable

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.

Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
Cc: stable@vger.kernel.org
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>
---
Changes in v3:
 - Add Fixes tag and Cc stable (Masami Hiramatsu).
 - Add Reviewed-by from Masami Hiramatsu. No code change.

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index 9e2afabf9..a76eab297 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -128,6 +128,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+	if (p->ainsn.api.insn) {
+		free_insn_slot(p->ainsn.api.insn, 0);
+		p->ainsn.api.insn = NULL;
+	}
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH v3] riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak
@ 2026-11-20  7:08 ` Xiaofeng Yuan
  0 siblings, 0 replies; 2+ messages in thread
From: Xiaofeng Yuan @ 2026-11-20  7:08 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti
  Cc: Masami Hiramatsu, linux-riscv, linux-kernel, linux-trace-kernel,
	stable

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.

Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
Cc: stable@vger.kernel.org
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>
---
Changes in v3:
 - Add Fixes tag and Cc stable (Masami Hiramatsu).
 - Add Reviewed-by from Masami Hiramatsu. No code change.

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index 9e2afabf9..a76eab297 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -128,6 +128,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+	if (p->ainsn.api.insn) {
+		free_insn_slot(p->ainsn.api.insn, 0);
+		p->ainsn.api.insn = NULL;
+	}
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20  7:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  7:08 [PATCH v3] riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak Xiaofeng Yuan
2026-11-20  7:08 ` Xiaofeng Yuan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.