Skip to contentSkip to conversation

riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak

PATCH New Xiaofeng Yuan 2 participants 5 messages Jul 31 – Nov 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak

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.

---
 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +4 -0
index 9e2afabf94..a76eab2972 100644
@@ -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)
Signature · 7 lines
2.43.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 1/1 riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak

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.

---
 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +4 -0
index 9e2afabf94..a76eab2972 100644
@@ -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)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 1/1 riscv: kprobes: free insn slot in arch_remove_kprobe() to avoid memory leak

In reply to Xiaofeng Yuan

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.

---
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(+)
arch/riscv/kernel/probes/kprobes.c +4 -0
index 9e2afabf9..a76eab297 100644
@@ -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)
Signature · 1 line
2.34.1
#3 lore raw

Gentle ping. Is there anything I still need to address on this patch? Happy to rebase onto a current linux-next if that makes it easier to apply.

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Xiaofeng Yuan
Quoted 8 lines · Xiaofeng Yuan

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.

Quoted 7 lines

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.

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")

And

from kprobes point of view.

Thanks,

Quoted 8 lines

---
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(+)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>