tracehook: release_task

This moves the ptrace-related logic from release_task into tracehook.h and
ptrace.h inlines.  It provides clean hooks both before and after locking
tasklist_lock, for future tracing logic to do more cleanup without the
lock.

This also changes release_task() itself in the rare "zap_leader" case to
set the leader to EXIT_DEAD before iterating.  This maintains the
invariant that release_task() only ever handles a task in EXIT_DEAD.  This
is a common-sense invariant that is already always true except in this one
arcane case of zombie leader whose parent ignores SIGCHLD.

This change is harmless and only costs one store in this one rare case.
It keeps the expected state more consisently sane, which is nicer when
debugging weirdness in release_task().  It also lets some future code in
the tracehook entry points rely on this invariant for bookkeeping.

Signed-off-by: Roland McGrath <roland@redhat.com>
Cc: Oleg Nesterov <oleg@tv-sign.ru>
Reviewed-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Roland McGrath 2008-07-25 19:45:48 -07:00 committed by Linus Torvalds
parent daded34be9
commit dae33574dc
3 changed files with 50 additions and 12 deletions

View File

@ -176,6 +176,19 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
}
}
/**
* ptrace_release_task - final ptrace-related cleanup of a zombie being reaped
* @task: task in %EXIT_DEAD state
*
* Called with write_lock(&tasklist_lock) held.
*/
static inline void ptrace_release_task(struct task_struct *task)
{
BUG_ON(!list_empty(&task->ptraced));
ptrace_unlink(task);
BUG_ON(!list_empty(&task->ptrace_entry));
}
#ifndef force_successful_syscall_return
/*
* System call handlers that, upon successful completion, need to return a

View File

@ -228,4 +228,32 @@ static inline void tracehook_report_vfork_done(struct task_struct *child,
ptrace_event(PT_TRACE_VFORK_DONE, PTRACE_EVENT_VFORK_DONE, pid);
}
/**
* tracehook_prepare_release_task - task is being reaped, clean up tracing
* @task: task in %EXIT_DEAD state
*
* This is called in release_task() just before @task gets finally reaped
* and freed. This would be the ideal place to remove and clean up any
* tracing-related state for @task.
*
* Called with no locks held.
*/
static inline void tracehook_prepare_release_task(struct task_struct *task)
{
}
/**
* tracehook_finish_release_task - task is being reaped, clean up tracing
* @task: task in %EXIT_DEAD state
*
* This is called in release_task() when @task is being in the middle of
* being reaped. After this, there must be no tracing entanglements.
*
* Called with write_lock_irq(&tasklist_lock) held.
*/
static inline void tracehook_finish_release_task(struct task_struct *task)
{
ptrace_release_task(task);
}
#endif /* <linux/tracehook.h> */

View File

@ -163,27 +163,17 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
put_task_struct(container_of(rhp, struct task_struct, rcu));
}
/*
* Do final ptrace-related cleanup of a zombie being reaped.
*
* Called with write_lock(&tasklist_lock) held.
*/
static void ptrace_release_task(struct task_struct *p)
{
BUG_ON(!list_empty(&p->ptraced));
ptrace_unlink(p);
BUG_ON(!list_empty(&p->ptrace_entry));
}
void release_task(struct task_struct * p)
{
struct task_struct *leader;
int zap_leader;
repeat:
tracehook_prepare_release_task(p);
atomic_dec(&p->user->processes);
proc_flush_task(p);
write_lock_irq(&tasklist_lock);
ptrace_release_task(p);
tracehook_finish_release_task(p);
__exit_signal(p);
/*
@ -205,6 +195,13 @@ repeat:
* that case.
*/
zap_leader = task_detached(leader);
/*
* This maintains the invariant that release_task()
* only runs on a task in EXIT_DEAD, just for sanity.
*/
if (zap_leader)
leader->exit_state = EXIT_DEAD;
}
write_unlock_irq(&tasklist_lock);