From: bibo mao 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 Cc: Masami Hiramatsu Cc: Prasanna S Panchamukhi Cc: Ananth N Mavinakayanahalli Cc: Anil S Keshavamurthy Signed-off-by: Andrew Morton --- arch/i386/kernel/process.c | 8 -------- arch/ia64/kernel/process.c | 8 -------- arch/powerpc/kernel/process.c | 2 -- arch/x86_64/kernel/process.c | 8 -------- kernel/kprobes.c | 10 +++++----- kernel/sched.c | 9 ++++++++- 6 files changed, 13 insertions(+), 32 deletions(-) diff -puN arch/i386/kernel/process.c~kretprobe-instance-recycled-by-parent-process arch/i386/kernel/process.c --- devel/arch/i386/kernel/process.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/arch/i386/kernel/process.c 2006-03-11 02:47:13.000000000 -0800 @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -364,13 +363,6 @@ void exit_thread(void) struct task_struct *tsk = current; struct thread_struct *t = &tsk->thread; - /* - * Remove function-return probe instances associated with this task - * and put them back on the free list. Do not insert an exit probe for - * this function, it will be disabled by kprobe_flush_task if you do. - */ - kprobe_flush_task(tsk); - /* The process may have allocated an io port bitmap... nuke it. */ if (unlikely(NULL != t->io_bitmap_ptr)) { int cpu = get_cpu(); diff -puN arch/ia64/kernel/process.c~kretprobe-instance-recycled-by-parent-process arch/ia64/kernel/process.c --- devel/arch/ia64/kernel/process.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/arch/ia64/kernel/process.c 2006-03-11 02:47:13.000000000 -0800 @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -738,13 +737,6 @@ void exit_thread (void) { - /* - * Remove function-return probe instances associated with this task - * and put them back on the free list. Do not insert an exit probe for - * this function, it will be disabled by kprobe_flush_task if you do. - */ - kprobe_flush_task(current); - ia64_drop_fpu(current); #ifdef CONFIG_PERFMON /* if needed, stop monitoring and flush state to perfmon context */ diff -puN arch/powerpc/kernel/process.c~kretprobe-instance-recycled-by-parent-process arch/powerpc/kernel/process.c --- devel/arch/powerpc/kernel/process.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/arch/powerpc/kernel/process.c 2006-03-11 02:47:13.000000000 -0800 @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -460,7 +459,6 @@ void show_regs(struct pt_regs * regs) void exit_thread(void) { - kprobe_flush_task(current); discard_lazy_cpu_state(); } diff -puN arch/x86_64/kernel/process.c~kretprobe-instance-recycled-by-parent-process arch/x86_64/kernel/process.c --- devel/arch/x86_64/kernel/process.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/arch/x86_64/kernel/process.c 2006-03-11 02:47:13.000000000 -0800 @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -353,13 +352,6 @@ void exit_thread(void) struct task_struct *me = current; struct thread_struct *t = &me->thread; - /* - * Remove function-return probe instances associated with this task - * and put them back on the free list. Do not insert an exit probe for - * this function, it will be disabled by kprobe_flush_task if you do. - */ - kprobe_flush_task(me); - if (me->thread.io_bitmap_ptr) { struct tss_struct *tss = &per_cpu(init_tss, get_cpu()); diff -puN kernel/kprobes.c~kretprobe-instance-recycled-by-parent-process kernel/kprobes.c --- devel/kernel/kprobes.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/kernel/kprobes.c 2006-03-11 02:47:13.000000000 -0800 @@ -323,10 +323,10 @@ struct hlist_head __kprobes *kretprobe_i } /* - * This function is called from exit_thread or flush_thread when task tk's - * stack is being recycled so that we can recycle any function-return probe - * instances associated with this task. These left over instances represent - * probed functions that have been called but will never return. + * This function is called from finish_task_switch when task tk becomes dead, + * so that we can recycle any function-return probe instances associated + * with this task. These left over instances represent probed functions + * that have been called but will never return. */ void __kprobes kprobe_flush_task(struct task_struct *tk) { @@ -336,7 +336,7 @@ void __kprobes kprobe_flush_task(struct unsigned long flags = 0; spin_lock_irqsave(&kretprobe_lock, flags); - head = kretprobe_inst_table_head(current); + head = kretprobe_inst_table_head(tk); hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { if (ri->task == tk) recycle_rp_inst(ri); diff -puN kernel/sched.c~kretprobe-instance-recycled-by-parent-process kernel/sched.c --- devel/kernel/sched.c~kretprobe-instance-recycled-by-parent-process 2006-03-11 02:47:13.000000000 -0800 +++ devel-akpm/kernel/sched.c 2006-03-11 02:47:13.000000000 -0800 @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -1551,8 +1552,14 @@ static inline void finish_task_switch(ru finish_lock_switch(rq, prev); if (mm) mmdrop(mm); - if (unlikely(prev_task_flags & PF_DEAD)) + if (unlikely(prev_task_flags & PF_DEAD)){ + /* + * Remove function-return probe instances associated with this task + * and put them back on the free list. + */ + kprobe_flush_task(prev); put_task_struct(prev); + } } /** _