Skip to content

Commit

Permalink
tracehook: release_task
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Roland McGrath authored and Linus Torvalds committed Jul 26, 2008
1 parent daded34 commit dae3357
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
13 changes: 13 additions & 0 deletions include/linux/ptrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions include/linux/tracehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> */
21 changes: 9 additions & 12 deletions kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
Expand All @@ -205,6 +195,13 @@ void release_task(struct task_struct * p)
* 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);
Expand Down

0 comments on commit dae3357

Please sign in to comment.