Skip to content

Commit

Permalink
Merge branch 'ptrace' of git://git.kernel.org/pub/scm/linux/kernel/gi…
Browse files Browse the repository at this point in the history
…t/oleg/misc

* 'ptrace' of git://git.kernel.org/pub/scm/linux/kernel/git/oleg/misc: (41 commits)
  signal: trivial, fix the "timespec declared inside parameter list" warning
  job control: reorganize wait_task_stopped()
  ptrace: fix signal->wait_chldexit usage in task_clear_group_stop_trapping()
  signal: sys_sigprocmask() needs retarget_shared_pending()
  signal: cleanup sys_sigprocmask()
  signal: rename signandsets() to sigandnsets()
  signal: do_sigtimedwait() needs retarget_shared_pending()
  signal: introduce do_sigtimedwait() to factor out compat/native code
  signal: sys_rt_sigtimedwait: simplify the timeout logic
  signal: cleanup sys_rt_sigprocmask()
  x86: signal: sys_rt_sigreturn() should use set_current_blocked()
  x86: signal: handle_signal() should use set_current_blocked()
  signal: sigprocmask() should do retarget_shared_pending()
  signal: sigprocmask: narrow the scope of ->siglock
  signal: retarget_shared_pending: optimize while_each_thread() loop
  signal: retarget_shared_pending: consider shared/unblocked signals only
  signal: introduce retarget_shared_pending()
  ptrace: ptrace_check_attach() should not do s/STOPPED/TRACED/
  signal: Turn SIGNAL_STOP_DEQUEUED into GROUP_STOP_DEQUEUED
  signal: do_signal_stop: Remove the unneeded task_clear_group_stop_pending()
  ...
  • Loading branch information
Linus Torvalds committed May 20, 2011
2 parents ad94717 + bd715d9 commit 3ed4c05
Show file tree
Hide file tree
Showing 9 changed files with 659 additions and 366 deletions.
14 changes: 5 additions & 9 deletions arch/x86/kernel/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,7 @@ long sys_rt_sigreturn(struct pt_regs *regs)
goto badframe;

sigdelsetmask(&set, ~_BLOCKABLE);
spin_lock_irq(&current->sighand->siglock);
current->blocked = set;
recalc_sigpending();
spin_unlock_irq(&current->sighand->siglock);
set_current_blocked(&set);

if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
goto badframe;
Expand Down Expand Up @@ -682,6 +679,7 @@ static int
handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
sigset_t *oldset, struct pt_regs *regs)
{
sigset_t blocked;
int ret;

/* Are we from a system call? */
Expand Down Expand Up @@ -741,12 +739,10 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
*/
regs->flags &= ~X86_EFLAGS_TF;

spin_lock_irq(&current->sighand->siglock);
sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
if (!(ka->sa.sa_flags & SA_NODEFER))
sigaddset(&current->blocked, sig);
recalc_sigpending();
spin_unlock_irq(&current->sighand->siglock);
sigaddset(&blocked, sig);
set_current_blocked(&blocked);

tracehook_signal_handler(sig, info, ka, regs,
test_thread_flag(TIF_SINGLESTEP));
Expand Down
1 change: 1 addition & 0 deletions fs/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ static int zap_process(struct task_struct *start, int exit_code)

t = start;
do {
task_clear_group_stop_pending(t);
if (t != current && t->mm) {
sigaddset(&t->pending.signal, SIGKILL);
signal_wake_up(t, 1);
Expand Down
17 changes: 14 additions & 3 deletions include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,8 @@ struct signal_struct {
* Bits in flags field of signal_struct.
*/
#define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
#define SIGNAL_STOP_DEQUEUED 0x00000002 /* stop signal dequeued */
#define SIGNAL_STOP_CONTINUED 0x00000004 /* SIGCONT since WCONTINUED reap */
#define SIGNAL_GROUP_EXIT 0x00000008 /* group exit in progress */
#define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
#define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
/*
* Pending notifications to parent.
*/
Expand Down Expand Up @@ -1251,6 +1250,7 @@ struct task_struct {
int exit_state;
int exit_code, exit_signal;
int pdeath_signal; /* The signal sent when the parent dies */
unsigned int group_stop; /* GROUP_STOP_*, siglock protected */
/* ??? */
unsigned int personality;
unsigned did_exec:1;
Expand Down Expand Up @@ -1771,6 +1771,17 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *
#define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
#define used_math() tsk_used_math(current)

/*
* task->group_stop flags
*/
#define GROUP_STOP_SIGMASK 0xffff /* signr of the last group stop */
#define GROUP_STOP_PENDING (1 << 16) /* task should stop for group stop */
#define GROUP_STOP_CONSUME (1 << 17) /* consume group stop count */
#define GROUP_STOP_TRAPPING (1 << 18) /* switching from STOPPED to TRACED */
#define GROUP_STOP_DEQUEUED (1 << 19) /* stop signal dequeued */

extern void task_clear_group_stop_pending(struct task_struct *task);

#ifdef CONFIG_PREEMPT_RCU

#define RCU_READ_UNLOCK_BLOCKED (1 << 0) /* blocked while in RCU read-side. */
Expand Down
13 changes: 9 additions & 4 deletions include/linux/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ _SIG_SET_BINOP(sigorsets, _sig_or)
#define _sig_and(x,y) ((x) & (y))
_SIG_SET_BINOP(sigandsets, _sig_and)

#define _sig_nand(x,y) ((x) & ~(y))
_SIG_SET_BINOP(signandsets, _sig_nand)
#define _sig_andn(x,y) ((x) & ~(y))
_SIG_SET_BINOP(sigandnsets, _sig_andn)

#undef _SIG_SET_BINOP
#undef _sig_or
#undef _sig_and
#undef _sig_nand
#undef _sig_andn

#define _SIG_SET_OP(name, op) \
static inline void name(sigset_t *set) \
Expand Down Expand Up @@ -236,6 +236,9 @@ static inline int valid_signal(unsigned long sig)
return sig <= _NSIG ? 1 : 0;
}

struct timespec;
struct pt_regs;

extern int next_signal(struct sigpending *pending, sigset_t *mask);
extern int do_send_sig_info(int sig, struct siginfo *info,
struct task_struct *p, bool group);
Expand All @@ -244,10 +247,12 @@ extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
extern long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig,
siginfo_t *info);
extern long do_sigpending(void __user *, unsigned long);
extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
const struct timespec *);
extern int sigprocmask(int, sigset_t *, sigset_t *);
extern void set_current_blocked(const sigset_t *);
extern int show_unhandled_signals;

struct pt_regs;
extern int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka, struct pt_regs *regs, void *cookie);
extern void exit_signals(struct task_struct *tsk);

Expand Down
27 changes: 0 additions & 27 deletions include/linux/tracehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,33 +468,6 @@ static inline int tracehook_get_signal(struct task_struct *task,
return 0;
}

/**
* tracehook_notify_jctl - report about job control stop/continue
* @notify: zero, %CLD_STOPPED or %CLD_CONTINUED
* @why: %CLD_STOPPED or %CLD_CONTINUED
*
* This is called when we might call do_notify_parent_cldstop().
*
* @notify is zero if we would not ordinarily send a %SIGCHLD,
* or is the %CLD_STOPPED or %CLD_CONTINUED .si_code for %SIGCHLD.
*
* @why is %CLD_STOPPED when about to stop for job control;
* we are already in %TASK_STOPPED state, about to call schedule().
* It might also be that we have just exited (check %PF_EXITING),
* but need to report that a group-wide stop is complete.
*
* @why is %CLD_CONTINUED when waking up after job control stop and
* ready to make a delayed @notify report.
*
* Return the %CLD_* value for %SIGCHLD, or zero to generate no signal.
*
* Called with the siglock held.
*/
static inline int tracehook_notify_jctl(int notify, int why)
{
return notify ?: (current->ptrace & PT_PTRACED) ? why : 0;
}

/**
* tracehook_finish_jctl - report about return from job control stop
*
Expand Down
47 changes: 7 additions & 40 deletions kernel/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,62 +890,29 @@ compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
{
compat_sigset_t s32;
sigset_t s;
int sig;
struct timespec t;
siginfo_t info;
long ret, timeout = 0;
long ret;

if (sigsetsize != sizeof(sigset_t))
return -EINVAL;

if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
return -EFAULT;
sigset_from_compat(&s, &s32);
sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
signotset(&s);

if (uts) {
if (get_compat_timespec (&t, uts))
if (get_compat_timespec(&t, uts))
return -EFAULT;
if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
|| t.tv_sec < 0)
return -EINVAL;
}

spin_lock_irq(&current->sighand->siglock);
sig = dequeue_signal(current, &s, &info);
if (!sig) {
timeout = MAX_SCHEDULE_TIMEOUT;
if (uts)
timeout = timespec_to_jiffies(&t)
+(t.tv_sec || t.tv_nsec);
if (timeout) {
current->real_blocked = current->blocked;
sigandsets(&current->blocked, &current->blocked, &s);

recalc_sigpending();
spin_unlock_irq(&current->sighand->siglock);

timeout = schedule_timeout_interruptible(timeout);

spin_lock_irq(&current->sighand->siglock);
sig = dequeue_signal(current, &s, &info);
current->blocked = current->real_blocked;
siginitset(&current->real_blocked, 0);
recalc_sigpending();
}
}
spin_unlock_irq(&current->sighand->siglock);
ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);

if (sig) {
ret = sig;
if (uinfo) {
if (copy_siginfo_to_user32(uinfo, &info))
ret = -EFAULT;
}
}else {
ret = timeout?-EINTR:-EAGAIN;
if (ret > 0 && uinfo) {
if (copy_siginfo_to_user32(uinfo, &info))
ret = -EFAULT;
}

return ret;

}
Expand Down
110 changes: 88 additions & 22 deletions kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,11 +1377,23 @@ static int *task_stopped_code(struct task_struct *p, bool ptrace)
return NULL;
}

/*
* Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
* read_lock(&tasklist_lock) on entry. If we return zero, we still hold
* the lock and this task is uninteresting. If we return nonzero, we have
* released the lock and the system call should return.
/**
* wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
* @wo: wait options
* @ptrace: is the wait for ptrace
* @p: task to wait for
*
* Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
*
* CONTEXT:
* read_lock(&tasklist_lock), which is released if return value is
* non-zero. Also, grabs and releases @p->sighand->siglock.
*
* RETURNS:
* 0 if wait condition didn't exist and search for other wait conditions
* should continue. Non-zero return, -errno on failure and @p's pid on
* success, implies that tasklist_lock is released and wait condition
* search should terminate.
*/
static int wait_task_stopped(struct wait_opts *wo,
int ptrace, struct task_struct *p)
Expand All @@ -1397,6 +1409,9 @@ static int wait_task_stopped(struct wait_opts *wo,
if (!ptrace && !(wo->wo_flags & WUNTRACED))
return 0;

if (!task_stopped_code(p, ptrace))
return 0;

exit_code = 0;
spin_lock_irq(&p->sighand->siglock);

Expand Down Expand Up @@ -1538,33 +1553,84 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace,
return 0;
}

if (likely(!ptrace) && unlikely(task_ptrace(p))) {
/* dead body doesn't have much to contribute */
if (p->exit_state == EXIT_DEAD)
return 0;

/* slay zombie? */
if (p->exit_state == EXIT_ZOMBIE) {
/*
* A zombie ptracee is only visible to its ptracer.
* Notification and reaping will be cascaded to the real
* parent when the ptracer detaches.
*/
if (likely(!ptrace) && unlikely(task_ptrace(p))) {
/* it will become visible, clear notask_error */
wo->notask_error = 0;
return 0;
}

/* we don't reap group leaders with subthreads */
if (!delay_group_leader(p))
return wait_task_zombie(wo, p);

/*
* This child is hidden by ptrace.
* We aren't allowed to see it now, but eventually we will.
* Allow access to stopped/continued state via zombie by
* falling through. Clearing of notask_error is complex.
*
* When !@ptrace:
*
* If WEXITED is set, notask_error should naturally be
* cleared. If not, subset of WSTOPPED|WCONTINUED is set,
* so, if there are live subthreads, there are events to
* wait for. If all subthreads are dead, it's still safe
* to clear - this function will be called again in finite
* amount time once all the subthreads are released and
* will then return without clearing.
*
* When @ptrace:
*
* Stopped state is per-task and thus can't change once the
* target task dies. Only continued and exited can happen.
* Clear notask_error if WCONTINUED | WEXITED.
*/
if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
wo->notask_error = 0;
} else {
/*
* If @p is ptraced by a task in its real parent's group,
* hide group stop/continued state when looking at @p as
* the real parent; otherwise, a single stop can be
* reported twice as group and ptrace stops.
*
* If a ptracer wants to distinguish the two events for its
* own children, it should create a separate process which
* takes the role of real parent.
*/
if (likely(!ptrace) && task_ptrace(p) &&
same_thread_group(p->parent, p->real_parent))
return 0;

/*
* @p is alive and it's gonna stop, continue or exit, so
* there always is something to wait for.
*/
wo->notask_error = 0;
return 0;
}

if (p->exit_state == EXIT_DEAD)
return 0;

/*
* We don't reap group leaders with subthreads.
* Wait for stopped. Depending on @ptrace, different stopped state
* is used and the two don't interact with each other.
*/
if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
return wait_task_zombie(wo, p);
ret = wait_task_stopped(wo, ptrace, p);
if (ret)
return ret;

/*
* It's stopped or running now, so it might
* later continue, exit, or stop again.
* Wait for continued. There's only one continued state and the
* ptracer can consume it which can confuse the real parent. Don't
* use WCONTINUED from ptracer. You don't need or want it.
*/
wo->notask_error = 0;

if (task_stopped_code(p, ptrace))
return wait_task_stopped(wo, ptrace, p);

return wait_task_continued(wo, p);
}

Expand Down
Loading

0 comments on commit 3ed4c05

Please sign in to comment.