signals: use __group_complete_signal() for the specific signals too
[safe/jmp/linux-2.6] / kernel / signal.c
index dee8cc9..fc1cb03 100644 (file)
@@ -566,9 +566,10 @@ static void do_notify_parent_cldstop(struct task_struct *tsk, int why);
  */
 static void handle_stop_signal(int sig, struct task_struct *p)
 {
+       struct signal_struct *signal = p->signal;
        struct task_struct *t;
 
-       if (p->signal->flags & SIGNAL_GROUP_EXIT)
+       if (signal->flags & SIGNAL_GROUP_EXIT)
                /*
                 * The process is in the middle of dying already.
                 */
@@ -578,19 +579,18 @@ static void handle_stop_signal(int sig, struct task_struct *p)
                /*
                 * This is a stop signal.  Remove SIGCONT from all queues.
                 */
-               rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
+               rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
                t = p;
                do {
                        rm_from_queue(sigmask(SIGCONT), &t->pending);
-                       t = next_thread(t);
-               } while (t != p);
+               } while_each_thread(p, t);
        } else if (sig == SIGCONT) {
                unsigned int why;
                /*
                 * Remove all stop signals from all queues,
                 * and wake all threads.
                 */
-               rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
+               rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
                t = p;
                do {
                        unsigned int state;
@@ -615,9 +615,7 @@ static void handle_stop_signal(int sig, struct task_struct *p)
                                state |= TASK_INTERRUPTIBLE;
                        }
                        wake_up_state(t, state);
-
-                       t = next_thread(t);
-               } while (t != p);
+               } while_each_thread(p, t);
 
                /*
                 * Notify the parent with CLD_CONTINUED if we were stopped.
@@ -628,30 +626,125 @@ static void handle_stop_signal(int sig, struct task_struct *p)
                 * CLD_CONTINUED was dropped.
                 */
                why = 0;
-               if (p->signal->flags & SIGNAL_STOP_STOPPED)
+               if (signal->flags & SIGNAL_STOP_STOPPED)
                        why |= SIGNAL_CLD_CONTINUED;
-               else if (p->signal->group_stop_count)
+               else if (signal->group_stop_count)
                        why |= SIGNAL_CLD_STOPPED;
 
                if (why) {
-                       p->signal->flags = why | SIGNAL_STOP_CONTINUED;
-                       p->signal->group_stop_count = 0;
-                       p->signal->group_exit_code = 0;
+                       signal->flags = why | SIGNAL_STOP_CONTINUED;
+                       signal->group_stop_count = 0;
+                       signal->group_exit_code = 0;
                } else {
                        /*
                         * We are not stopped, but there could be a stop
                         * signal in the middle of being processed after
                         * being removed from the queue.  Clear that too.
                         */
-                       p->signal->flags &= ~SIGNAL_STOP_DEQUEUED;
+                       signal->flags &= ~SIGNAL_STOP_DEQUEUED;
                }
        } else if (sig == SIGKILL) {
                /*
                 * Make sure that any pending stop signal already dequeued
                 * is undone by the wakeup for SIGKILL.
                 */
-               p->signal->flags &= ~SIGNAL_STOP_DEQUEUED;
+               signal->flags &= ~SIGNAL_STOP_DEQUEUED;
+       }
+}
+
+/*
+ * Test if P wants to take SIG.  After we've checked all threads with this,
+ * it's equivalent to finding no threads not blocking SIG.  Any threads not
+ * blocking SIG were ruled out because they are not running and already
+ * have pending signals.  Such threads will dequeue from the shared queue
+ * as soon as they're available, so putting the signal on the shared queue
+ * will be equivalent to sending it to one such thread.
+ */
+static inline int wants_signal(int sig, struct task_struct *p)
+{
+       if (sigismember(&p->blocked, sig))
+               return 0;
+       if (p->flags & PF_EXITING)
+               return 0;
+       if (sig == SIGKILL)
+               return 1;
+       if (task_is_stopped_or_traced(p))
+               return 0;
+       return task_curr(p) || !signal_pending(p);
+}
+
+static void complete_signal(int sig, struct task_struct *p, int group)
+{
+       struct signal_struct *signal = p->signal;
+       struct task_struct *t;
+
+       /*
+        * Now find a thread we can wake up to take the signal off the queue.
+        *
+        * If the main thread wants the signal, it gets first crack.
+        * Probably the least surprising to the average bear.
+        */
+       if (wants_signal(sig, p))
+               t = p;
+       else if (!group || thread_group_empty(p))
+               /*
+                * There is just one thread and it does not need to be woken.
+                * It will dequeue unblocked signals before it runs again.
+                */
+               return;
+       else {
+               /*
+                * Otherwise try to find a suitable thread.
+                */
+               t = signal->curr_target;
+               while (!wants_signal(sig, t)) {
+                       t = next_thread(t);
+                       if (t == signal->curr_target)
+                               /*
+                                * No thread needs to be woken.
+                                * Any eligible threads will see
+                                * the signal in the queue soon.
+                                */
+                               return;
+               }
+               signal->curr_target = t;
+       }
+
+       /*
+        * Found a killable thread.  If the signal will be fatal,
+        * then start taking the whole group down immediately.
+        */
+       if (sig_fatal(p, sig) && !(signal->flags & SIGNAL_GROUP_EXIT) &&
+           !sigismember(&t->real_blocked, sig) &&
+           (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
+               /*
+                * This signal will be fatal to the whole group.
+                */
+               if (!sig_kernel_coredump(sig)) {
+                       /*
+                        * Start a group exit and wake everybody up.
+                        * This way we don't have other threads
+                        * running and doing things after a slower
+                        * thread has the fatal signal pending.
+                        */
+                       signal->flags = SIGNAL_GROUP_EXIT;
+                       signal->group_exit_code = sig;
+                       signal->group_stop_count = 0;
+                       t = p;
+                       do {
+                               sigaddset(&t->pending.signal, SIGKILL);
+                               signal_wake_up(t, 1);
+                       } while_each_thread(p, t);
+                       return;
+               }
        }
+
+       /*
+        * The signal is already in the shared-pending queue.
+        * Tell the chosen thread to wake up and dequeue it.
+        */
+       signal_wake_up(t, sig == SIGKILL);
+       return;
 }
 
 static inline int legacy_queue(struct sigpending *signals, int sig)
@@ -660,16 +753,21 @@ static inline int legacy_queue(struct sigpending *signals, int sig)
 }
 
 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
-                       struct sigpending *signals)
+                       int group)
 {
-       struct sigqueue * q = NULL;
+       struct sigpending *pending;
+       struct sigqueue *q;
+
+       assert_spin_locked(&t->sighand->siglock);
+       handle_stop_signal(sig, t);
 
+       pending = group ? &t->signal->shared_pending : &t->pending;
        /*
         * Short-circuit ignored signals and support queuing
         * exactly one non-rt signal, so that we can get more
         * detailed information about the cause of the signal.
         */
-       if (sig_ignored(t, sig) || legacy_queue(signals, sig))
+       if (sig_ignored(t, sig) || legacy_queue(pending, sig))
                return 0;
 
        /*
@@ -697,7 +795,7 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
                                             (is_si_special(info) ||
                                              info->si_code >= 0)));
        if (q) {
-               list_add_tail(&q->list, &signals->list);
+               list_add_tail(&q->list, &pending->list);
                switch ((unsigned long) info) {
                case (unsigned long) SEND_SIG_NOINFO:
                        q->info.si_signo = sig;
@@ -727,7 +825,7 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
        }
 
 out_set:
-       sigaddset(&signals->signal, sig);
+       sigaddset(&pending->signal, sig);
        return 1;
 }
 
@@ -768,15 +866,11 @@ specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
 {
        int ret;
 
-       BUG_ON(!irqs_disabled());
-       assert_spin_locked(&t->sighand->siglock);
-
-       ret = send_signal(sig, info, t, &t->pending);
+       ret = send_signal(sig, info, t, 0);
        if (ret <= 0)
                return ret;
 
-       if (!sigismember(&t->blocked, sig))
-               signal_wake_up(t, sig == SIGKILL);
+       complete_signal(sig, t, 0);
        return 0;
 }
 
@@ -820,123 +914,21 @@ force_sig_specific(int sig, struct task_struct *t)
        force_sig_info(sig, SEND_SIG_FORCED, t);
 }
 
-/*
- * Test if P wants to take SIG.  After we've checked all threads with this,
- * it's equivalent to finding no threads not blocking SIG.  Any threads not
- * blocking SIG were ruled out because they are not running and already
- * have pending signals.  Such threads will dequeue from the shared queue
- * as soon as they're available, so putting the signal on the shared queue
- * will be equivalent to sending it to one such thread.
- */
-static inline int wants_signal(int sig, struct task_struct *p)
-{
-       if (sigismember(&p->blocked, sig))
-               return 0;
-       if (p->flags & PF_EXITING)
-               return 0;
-       if (sig == SIGKILL)
-               return 1;
-       if (task_is_stopped_or_traced(p))
-               return 0;
-       return task_curr(p) || !signal_pending(p);
-}
-
-static void
-__group_complete_signal(int sig, struct task_struct *p)
-{
-       struct task_struct *t;
-
-       /*
-        * Now find a thread we can wake up to take the signal off the queue.
-        *
-        * If the main thread wants the signal, it gets first crack.
-        * Probably the least surprising to the average bear.
-        */
-       if (wants_signal(sig, p))
-               t = p;
-       else if (thread_group_empty(p))
-               /*
-                * There is just one thread and it does not need to be woken.
-                * It will dequeue unblocked signals before it runs again.
-                */
-               return;
-       else {
-               /*
-                * Otherwise try to find a suitable thread.
-                */
-               t = p->signal->curr_target;
-               if (t == NULL)
-                       /* restart balancing at this thread */
-                       t = p->signal->curr_target = p;
-
-               while (!wants_signal(sig, t)) {
-                       t = next_thread(t);
-                       if (t == p->signal->curr_target)
-                               /*
-                                * No thread needs to be woken.
-                                * Any eligible threads will see
-                                * the signal in the queue soon.
-                                */
-                               return;
-               }
-               p->signal->curr_target = t;
-       }
-
-       /*
-        * Found a killable thread.  If the signal will be fatal,
-        * then start taking the whole group down immediately.
-        */
-       if (sig_fatal(p, sig) && !(p->signal->flags & SIGNAL_GROUP_EXIT) &&
-           !sigismember(&t->real_blocked, sig) &&
-           (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
-               /*
-                * This signal will be fatal to the whole group.
-                */
-               if (!sig_kernel_coredump(sig)) {
-                       /*
-                        * Start a group exit and wake everybody up.
-                        * This way we don't have other threads
-                        * running and doing things after a slower
-                        * thread has the fatal signal pending.
-                        */
-                       p->signal->flags = SIGNAL_GROUP_EXIT;
-                       p->signal->group_exit_code = sig;
-                       p->signal->group_stop_count = 0;
-                       t = p;
-                       do {
-                               sigaddset(&t->pending.signal, SIGKILL);
-                               signal_wake_up(t, 1);
-                       } while_each_thread(p, t);
-                       return;
-               }
-       }
-
-       /*
-        * The signal is already in the shared-pending queue.
-        * Tell the chosen thread to wake up and dequeue it.
-        */
-       signal_wake_up(t, sig == SIGKILL);
-       return;
-}
-
 int
 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
 {
        int ret;
 
-       assert_spin_locked(&p->sighand->siglock);
-       handle_stop_signal(sig, p);
-
        /*
         * Put this signal on the shared-pending queue, or fail with EAGAIN.
         * We always use the shared queue for process-wide signals,
         * to avoid several races.
         */
-       ret = send_signal(sig, info, p, &p->signal->shared_pending);
+       ret = send_signal(sig, info, p, 1);
        if (ret <= 0)
                return ret;
 
-       __group_complete_signal(sig, p);
+       complete_signal(sig, p, 1);
        return 0;
 }
 
@@ -1143,8 +1135,7 @@ static int kill_something_info(int sig, struct siginfo *info, int pid)
  */
 
 /*
- * These two are the most common entry points.  They send a signal
- * just to the specific thread.
+ * The caller must ensure the task can't exit.
  */
 int
 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
@@ -1159,17 +1150,9 @@ send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
        if (!valid_signal(sig))
                return -EINVAL;
 
-       /*
-        * We need the tasklist lock even for the specific
-        * thread case (when we don't need to follow the group
-        * lists) in order to avoid races with "p->sighand"
-        * going away or changing from under us.
-        */
-       read_lock(&tasklist_lock);  
        spin_lock_irqsave(&p->sighand->siglock, flags);
        ret = specific_send_sig_info(sig, info, p);
        spin_unlock_irqrestore(&p->sighand->siglock, flags);
-       read_unlock(&tasklist_lock);
        return ret;
 }
 
@@ -1276,8 +1259,12 @@ void sigqueue_free(struct sigqueue *q)
 }
 
 static int do_send_sigqueue(int sig, struct sigqueue *q, struct task_struct *t,
-               struct sigpending *pending)
+                               int group)
 {
+       struct sigpending *pending;
+
+       handle_stop_signal(sig, t);
+
        if (unlikely(!list_empty(&q->list))) {
                /*
                 * If an SI_TIMER entry is already queue just increment
@@ -1293,8 +1280,10 @@ static int do_send_sigqueue(int sig, struct sigqueue *q, struct task_struct *t,
                return 1;
 
        signalfd_notify(t, sig);
+       pending = group ? &t->signal->shared_pending : &t->pending;
        list_add_tail(&q->list, &pending->list);
        sigaddset(&pending->signal, sig);
+
        return 0;
 }
 
@@ -1313,20 +1302,15 @@ int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
         * We return -1, when the task is marked exiting, so
         * posix_timer_event can redirect it to the group leader
         */
-       rcu_read_lock();
-
        if (!likely(lock_task_sighand(p, &flags)))
                goto out_err;
 
-       ret = do_send_sigqueue(sig, q, p, &p->pending);
+       ret = do_send_sigqueue(sig, q, p, 0);
 
-       if (!sigismember(&p->blocked, sig))
-               signal_wake_up(p, sig == SIGKILL);
+       complete_signal(sig, p, 0);
 
        unlock_task_sighand(p, &flags);
 out_err:
-       rcu_read_unlock();
-
        return ret;
 }
 
@@ -1338,17 +1322,15 @@ send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
 
        BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
 
-       read_lock(&tasklist_lock);
        /* Since it_lock is held, p->sighand cannot be NULL. */
        spin_lock_irqsave(&p->sighand->siglock, flags);
-       handle_stop_signal(sig, p);
 
-       ret = do_send_sigqueue(sig, q, p, &p->signal->shared_pending);
+       ret = do_send_sigqueue(sig, q, p, 1);
 
-       __group_complete_signal(sig, p);
+       complete_signal(sig, p, 1);
 
        spin_unlock_irqrestore(&p->sighand->siglock, flags);
-       read_unlock(&tasklist_lock);
+
        return ret;
 }
 
@@ -1755,8 +1737,9 @@ static int ptrace_signal(int signr, siginfo_t *info,
 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
                          struct pt_regs *regs, void *cookie)
 {
-       sigset_t *mask = &current->blocked;
-       int signr = 0;
+       struct sighand_struct *sighand = current->sighand;
+       struct signal_struct *signal = current->signal;
+       int signr;
 
 relock:
        /*
@@ -1767,13 +1750,13 @@ relock:
         */
        try_to_freeze();
 
-       spin_lock_irq(&current->sighand->siglock);
+       spin_lock_irq(&sighand->siglock);
 
-       if (unlikely(current->signal->flags & SIGNAL_CLD_MASK)) {
-               int why = (current->signal->flags & SIGNAL_STOP_CONTINUED)
+       if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
+               int why = (signal->flags & SIGNAL_STOP_CONTINUED)
                                ? CLD_CONTINUED : CLD_STOPPED;
-               current->signal->flags &= ~SIGNAL_CLD_MASK;
-               spin_unlock_irq(&current->sighand->siglock);
+               signal->flags &= ~SIGNAL_CLD_MASK;
+               spin_unlock_irq(&sighand->siglock);
 
                read_lock(&tasklist_lock);
                do_notify_parent_cldstop(current->group_leader, why);
@@ -1784,12 +1767,11 @@ relock:
        for (;;) {
                struct k_sigaction *ka;
 
-               if (unlikely(current->signal->group_stop_count > 0) &&
+               if (unlikely(signal->group_stop_count > 0) &&
                    do_signal_stop(0))
                        goto relock;
 
-               signr = dequeue_signal(current, mask, info);
-
+               signr = dequeue_signal(current, &current->blocked, info);
                if (!signr)
                        break; /* will return 0 */
 
@@ -1799,7 +1781,7 @@ relock:
                                continue;
                }
 
-               ka = &current->sighand->action[signr-1];
+               ka = &sighand->action[signr-1];
                if (ka->sa.sa_handler == SIG_IGN) /* Do nothing.  */
                        continue;
                if (ka->sa.sa_handler != SIG_DFL) {
@@ -1836,14 +1818,14 @@ relock:
                         * We need to check for that and bail out if necessary.
                         */
                        if (signr != SIGSTOP) {
-                               spin_unlock_irq(&current->sighand->siglock);
+                               spin_unlock_irq(&sighand->siglock);
 
                                /* signals can be posted during this window */
 
                                if (is_current_pgrp_orphaned())
                                        goto relock;
 
-                               spin_lock_irq(&current->sighand->siglock);
+                               spin_lock_irq(&sighand->siglock);
                        }
 
                        if (likely(do_signal_stop(signr))) {
@@ -1858,7 +1840,7 @@ relock:
                        continue;
                }
 
-               spin_unlock_irq(&current->sighand->siglock);
+               spin_unlock_irq(&sighand->siglock);
 
                /*
                 * Anything else is fatal, maybe with a core dump.
@@ -1884,7 +1866,7 @@ relock:
                do_group_exit(signr);
                /* NOTREACHED */
        }
-       spin_unlock_irq(&current->sighand->siglock);
+       spin_unlock_irq(&sighand->siglock);
        return signr;
 }
 
@@ -2228,6 +2210,7 @@ static int do_tkill(int tgid, int pid, int sig)
        int error;
        struct siginfo info;
        struct task_struct *p;
+       unsigned long flags;
 
        error = -ESRCH;
        info.si_signo = sig;
@@ -2236,22 +2219,24 @@ static int do_tkill(int tgid, int pid, int sig)
        info.si_pid = task_tgid_vnr(current);
        info.si_uid = current->uid;
 
-       read_lock(&tasklist_lock);
+       rcu_read_lock();
        p = find_task_by_vpid(pid);
        if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
                error = check_kill_permission(sig, &info, p);
                /*
                 * The null signal is a permissions and process existence
                 * probe.  No signal is actually delivered.
+                *
+                * If lock_task_sighand() fails we pretend the task dies
+                * after receiving the signal. The window is tiny, and the
+                * signal is private anyway.
                 */
-               if (!error && sig && p->sighand) {
-                       spin_lock_irq(&p->sighand->siglock);
-                       handle_stop_signal(sig, p);
+               if (!error && sig && lock_task_sighand(p, &flags)) {
                        error = specific_send_sig_info(sig, &info, p);
-                       spin_unlock_irq(&p->sighand->siglock);
+                       unlock_task_sighand(p, &flags);
                }
        }
-       read_unlock(&tasklist_lock);
+       rcu_read_unlock();
 
        return error;
 }