Skip to content

Commit c6fd91f

Browse files
bibo maoLinus Torvalds
authored andcommitted
[PATCH] kretprobe instance recycled by parent process
When kretprobe probes the schedule() function, if the probed process exits then schedule() will never return, so some kretprobe instances will never be recycled. In this patch the parent process will recycle retprobe instances of the probed function and there will be no memory leak of kretprobe instances. Signed-off-by: bibo mao <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Prasanna S Panchamukhi <[email protected]> Cc: Ananth N Mavinakayanahalli <[email protected]> Cc: Anil S Keshavamurthy <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c9becf5 commit c6fd91f

File tree

6 files changed

+14
-32
lines changed

6 files changed

+14
-32
lines changed

arch/i386/kernel/process.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <linux/kallsyms.h>
3939
#include <linux/ptrace.h>
4040
#include <linux/random.h>
41-
#include <linux/kprobes.h>
4241

4342
#include <asm/uaccess.h>
4443
#include <asm/pgtable.h>
@@ -364,13 +363,6 @@ void exit_thread(void)
364363
struct task_struct *tsk = current;
365364
struct thread_struct *t = &tsk->thread;
366365

367-
/*
368-
* Remove function-return probe instances associated with this task
369-
* and put them back on the free list. Do not insert an exit probe for
370-
* this function, it will be disabled by kprobe_flush_task if you do.
371-
*/
372-
kprobe_flush_task(tsk);
373-
374366
/* The process may have allocated an io port bitmap... nuke it. */
375367
if (unlikely(NULL != t->io_bitmap_ptr)) {
376368
int cpu = get_cpu();

arch/ia64/kernel/process.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <linux/efi.h>
3131
#include <linux/interrupt.h>
3232
#include <linux/delay.h>
33-
#include <linux/kprobes.h>
3433

3534
#include <asm/cpu.h>
3635
#include <asm/delay.h>
@@ -738,13 +737,6 @@ void
738737
exit_thread (void)
739738
{
740739

741-
/*
742-
* Remove function-return probe instances associated with this task
743-
* and put them back on the free list. Do not insert an exit probe for
744-
* this function, it will be disabled by kprobe_flush_task if you do.
745-
*/
746-
kprobe_flush_task(current);
747-
748740
ia64_drop_fpu(current);
749741
#ifdef CONFIG_PERFMON
750742
/* if needed, stop monitoring and flush state to perfmon context */

arch/powerpc/kernel/process.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/mqueue.h>
3636
#include <linux/hardirq.h>
3737
#include <linux/utsname.h>
38-
#include <linux/kprobes.h>
3938

4039
#include <asm/pgtable.h>
4140
#include <asm/uaccess.h>
@@ -460,7 +459,6 @@ void show_regs(struct pt_regs * regs)
460459

461460
void exit_thread(void)
462461
{
463-
kprobe_flush_task(current);
464462
discard_lazy_cpu_state();
465463
}
466464

arch/x86_64/kernel/process.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#include <linux/ptrace.h>
3636
#include <linux/utsname.h>
3737
#include <linux/random.h>
38-
#include <linux/kprobes.h>
3938
#include <linux/notifier.h>
39+
#include <linux/kprobes.h>
4040

4141
#include <asm/uaccess.h>
4242
#include <asm/pgtable.h>
@@ -353,13 +353,6 @@ void exit_thread(void)
353353
struct task_struct *me = current;
354354
struct thread_struct *t = &me->thread;
355355

356-
/*
357-
* Remove function-return probe instances associated with this task
358-
* and put them back on the free list. Do not insert an exit probe for
359-
* this function, it will be disabled by kprobe_flush_task if you do.
360-
*/
361-
kprobe_flush_task(me);
362-
363356
if (me->thread.io_bitmap_ptr) {
364357
struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
365358

kernel/kprobes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
323323
}
324324

325325
/*
326-
* This function is called from exit_thread or flush_thread when task tk's
327-
* stack is being recycled so that we can recycle any function-return probe
328-
* instances associated with this task. These left over instances represent
329-
* probed functions that have been called but will never return.
326+
* This function is called from finish_task_switch when task tk becomes dead,
327+
* so that we can recycle any function-return probe instances associated
328+
* with this task. These left over instances represent probed functions
329+
* that have been called but will never return.
330330
*/
331331
void __kprobes kprobe_flush_task(struct task_struct *tk)
332332
{
@@ -336,7 +336,7 @@ void __kprobes kprobe_flush_task(struct task_struct *tk)
336336
unsigned long flags = 0;
337337

338338
spin_lock_irqsave(&kretprobe_lock, flags);
339-
head = kretprobe_inst_table_head(current);
339+
head = kretprobe_inst_table_head(tk);
340340
hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
341341
if (ri->task == tk)
342342
recycle_rp_inst(ri);

kernel/sched.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <linux/syscalls.h>
5050
#include <linux/times.h>
5151
#include <linux/acct.h>
52+
#include <linux/kprobes.h>
5253
#include <asm/tlb.h>
5354

5455
#include <asm/unistd.h>
@@ -1546,8 +1547,14 @@ static inline void finish_task_switch(runqueue_t *rq, task_t *prev)
15461547
finish_lock_switch(rq, prev);
15471548
if (mm)
15481549
mmdrop(mm);
1549-
if (unlikely(prev_task_flags & PF_DEAD))
1550+
if (unlikely(prev_task_flags & PF_DEAD)) {
1551+
/*
1552+
* Remove function-return probe instances associated with this
1553+
* task and put them back on the free list.
1554+
*/
1555+
kprobe_flush_task(prev);
15501556
put_task_struct(prev);
1557+
}
15511558
}
15521559

15531560
/**

0 commit comments

Comments
 (0)