aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/signal.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/signal.c')
-rw-r--r--kernel/signal.c686
1 files changed, 459 insertions, 227 deletions
diff --git a/kernel/signal.c b/kernel/signal.c
index 7165af5f1b11..ff7678603328 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -124,7 +124,7 @@ static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
124 124
125static int recalc_sigpending_tsk(struct task_struct *t) 125static int recalc_sigpending_tsk(struct task_struct *t)
126{ 126{
127 if (t->signal->group_stop_count > 0 || 127 if ((t->group_stop & GROUP_STOP_PENDING) ||
128 PENDING(&t->pending, &t->blocked) || 128 PENDING(&t->pending, &t->blocked) ||
129 PENDING(&t->signal->shared_pending, &t->blocked)) { 129 PENDING(&t->signal->shared_pending, &t->blocked)) {
130 set_tsk_thread_flag(t, TIF_SIGPENDING); 130 set_tsk_thread_flag(t, TIF_SIGPENDING);
@@ -223,6 +223,83 @@ static inline void print_dropped_signal(int sig)
223 current->comm, current->pid, sig); 223 current->comm, current->pid, sig);
224} 224}
225 225
226/**
227 * task_clear_group_stop_trapping - clear group stop trapping bit
228 * @task: target task
229 *
230 * If GROUP_STOP_TRAPPING is set, a ptracer is waiting for us. Clear it
231 * and wake up the ptracer. Note that we don't need any further locking.
232 * @task->siglock guarantees that @task->parent points to the ptracer.
233 *
234 * CONTEXT:
235 * Must be called with @task->sighand->siglock held.
236 */
237static void task_clear_group_stop_trapping(struct task_struct *task)
238{
239 if (unlikely(task->group_stop & GROUP_STOP_TRAPPING)) {
240 task->group_stop &= ~GROUP_STOP_TRAPPING;
241 __wake_up_sync_key(&task->parent->signal->wait_chldexit,
242 TASK_UNINTERRUPTIBLE, 1, task);
243 }
244}
245
246/**
247 * task_clear_group_stop_pending - clear pending group stop
248 * @task: target task
249 *
250 * Clear group stop states for @task.
251 *
252 * CONTEXT:
253 * Must be called with @task->sighand->siglock held.
254 */
255void task_clear_group_stop_pending(struct task_struct *task)
256{
257 task->group_stop &= ~(GROUP_STOP_PENDING | GROUP_STOP_CONSUME |
258 GROUP_STOP_DEQUEUED);
259}
260
261/**
262 * task_participate_group_stop - participate in a group stop
263 * @task: task participating in a group stop
264 *
265 * @task has GROUP_STOP_PENDING set and is participating in a group stop.
266 * Group stop states are cleared and the group stop count is consumed if
267 * %GROUP_STOP_CONSUME was set. If the consumption completes the group
268 * stop, the appropriate %SIGNAL_* flags are set.
269 *
270 * CONTEXT:
271 * Must be called with @task->sighand->siglock held.
272 *
273 * RETURNS:
274 * %true if group stop completion should be notified to the parent, %false
275 * otherwise.
276 */
277static bool task_participate_group_stop(struct task_struct *task)
278{
279 struct signal_struct *sig = task->signal;
280 bool consume = task->group_stop & GROUP_STOP_CONSUME;
281
282 WARN_ON_ONCE(!(task->group_stop & GROUP_STOP_PENDING));
283
284 task_clear_group_stop_pending(task);
285
286 if (!consume)
287 return false;
288
289 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
290 sig->group_stop_count--;
291
292 /*
293 * Tell the caller to notify completion iff we are entering into a
294 * fresh group stop. Read comment in do_signal_stop() for details.
295 */
296 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
297 sig->flags = SIGNAL_STOP_STOPPED;
298 return true;
299 }
300 return false;
301}
302
226/* 303/*
227 * allocate a new signal queue record 304 * allocate a new signal queue record
228 * - this may be called without locks if and only if t == current, otherwise an 305 * - this may be called without locks if and only if t == current, otherwise an
@@ -527,7 +604,7 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
527 * is to alert stop-signal processing code when another 604 * is to alert stop-signal processing code when another
528 * processor has come along and cleared the flag. 605 * processor has come along and cleared the flag.
529 */ 606 */
530 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED; 607 current->group_stop |= GROUP_STOP_DEQUEUED;
531 } 608 }
532 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) { 609 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
533 /* 610 /*
@@ -592,7 +669,7 @@ static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
592 if (sigisemptyset(&m)) 669 if (sigisemptyset(&m))
593 return 0; 670 return 0;
594 671
595 signandsets(&s->signal, &s->signal, mask); 672 sigandnsets(&s->signal, &s->signal, mask);
596 list_for_each_entry_safe(q, n, &s->list, list) { 673 list_for_each_entry_safe(q, n, &s->list, list) {
597 if (sigismember(mask, q->info.si_signo)) { 674 if (sigismember(mask, q->info.si_signo)) {
598 list_del_init(&q->list); 675 list_del_init(&q->list);
@@ -727,34 +804,14 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
727 } else if (sig == SIGCONT) { 804 } else if (sig == SIGCONT) {
728 unsigned int why; 805 unsigned int why;
729 /* 806 /*
730 * Remove all stop signals from all queues, 807 * Remove all stop signals from all queues, wake all threads.
731 * and wake all threads.
732 */ 808 */
733 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending); 809 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
734 t = p; 810 t = p;
735 do { 811 do {
736 unsigned int state; 812 task_clear_group_stop_pending(t);
737 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending); 813 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
738 /* 814 wake_up_state(t, __TASK_STOPPED);
739 * If there is a handler for SIGCONT, we must make
740 * sure that no thread returns to user mode before
741 * we post the signal, in case it was the only
742 * thread eligible to run the signal handler--then
743 * it must not do anything between resuming and
744 * running the handler. With the TIF_SIGPENDING
745 * flag set, the thread will pause and acquire the
746 * siglock that we hold now and until we've queued
747 * the pending signal.
748 *
749 * Wake up the stopped thread _after_ setting
750 * TIF_SIGPENDING
751 */
752 state = __TASK_STOPPED;
753 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
754 set_tsk_thread_flag(t, TIF_SIGPENDING);
755 state |= TASK_INTERRUPTIBLE;
756 }
757 wake_up_state(t, state);
758 } while_each_thread(p, t); 815 } while_each_thread(p, t);
759 816
760 /* 817 /*
@@ -780,13 +837,6 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
780 signal->flags = why | SIGNAL_STOP_CONTINUED; 837 signal->flags = why | SIGNAL_STOP_CONTINUED;
781 signal->group_stop_count = 0; 838 signal->group_stop_count = 0;
782 signal->group_exit_code = 0; 839 signal->group_exit_code = 0;
783 } else {
784 /*
785 * We are not stopped, but there could be a stop
786 * signal in the middle of being processed after
787 * being removed from the queue. Clear that too.
788 */
789 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
790 } 840 }
791 } 841 }
792 842
@@ -875,6 +925,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
875 signal->group_stop_count = 0; 925 signal->group_stop_count = 0;
876 t = p; 926 t = p;
877 do { 927 do {
928 task_clear_group_stop_pending(t);
878 sigaddset(&t->pending.signal, SIGKILL); 929 sigaddset(&t->pending.signal, SIGKILL);
879 signal_wake_up(t, 1); 930 signal_wake_up(t, 1);
880 } while_each_thread(p, t); 931 } while_each_thread(p, t);
@@ -1109,6 +1160,7 @@ int zap_other_threads(struct task_struct *p)
1109 p->signal->group_stop_count = 0; 1160 p->signal->group_stop_count = 0;
1110 1161
1111 while_each_thread(p, t) { 1162 while_each_thread(p, t) {
1163 task_clear_group_stop_pending(t);
1112 count++; 1164 count++;
1113 1165
1114 /* Don't bother with already dead threads */ 1166 /* Don't bother with already dead threads */
@@ -1536,16 +1588,30 @@ int do_notify_parent(struct task_struct *tsk, int sig)
1536 return ret; 1588 return ret;
1537} 1589}
1538 1590
1539static void do_notify_parent_cldstop(struct task_struct *tsk, int why) 1591/**
1592 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1593 * @tsk: task reporting the state change
1594 * @for_ptracer: the notification is for ptracer
1595 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1596 *
1597 * Notify @tsk's parent that the stopped/continued state has changed. If
1598 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1599 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1600 *
1601 * CONTEXT:
1602 * Must be called with tasklist_lock at least read locked.
1603 */
1604static void do_notify_parent_cldstop(struct task_struct *tsk,
1605 bool for_ptracer, int why)
1540{ 1606{
1541 struct siginfo info; 1607 struct siginfo info;
1542 unsigned long flags; 1608 unsigned long flags;
1543 struct task_struct *parent; 1609 struct task_struct *parent;
1544 struct sighand_struct *sighand; 1610 struct sighand_struct *sighand;
1545 1611
1546 if (task_ptrace(tsk)) 1612 if (for_ptracer) {
1547 parent = tsk->parent; 1613 parent = tsk->parent;
1548 else { 1614 } else {
1549 tsk = tsk->group_leader; 1615 tsk = tsk->group_leader;
1550 parent = tsk->real_parent; 1616 parent = tsk->real_parent;
1551 } 1617 }
@@ -1621,6 +1687,15 @@ static int sigkill_pending(struct task_struct *tsk)
1621} 1687}
1622 1688
1623/* 1689/*
1690 * Test whether the target task of the usual cldstop notification - the
1691 * real_parent of @child - is in the same group as the ptracer.
1692 */
1693static bool real_parent_is_ptracer(struct task_struct *child)
1694{
1695 return same_thread_group(child->parent, child->real_parent);
1696}
1697
1698/*
1624 * This must be called with current->sighand->siglock held. 1699 * This must be called with current->sighand->siglock held.
1625 * 1700 *
1626 * This should be the path for all ptrace stops. 1701 * This should be the path for all ptrace stops.
@@ -1631,10 +1706,12 @@ static int sigkill_pending(struct task_struct *tsk)
1631 * If we actually decide not to stop at all because the tracer 1706 * If we actually decide not to stop at all because the tracer
1632 * is gone, we keep current->exit_code unless clear_code. 1707 * is gone, we keep current->exit_code unless clear_code.
1633 */ 1708 */
1634static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info) 1709static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1635 __releases(&current->sighand->siglock) 1710 __releases(&current->sighand->siglock)
1636 __acquires(&current->sighand->siglock) 1711 __acquires(&current->sighand->siglock)
1637{ 1712{
1713 bool gstop_done = false;
1714
1638 if (arch_ptrace_stop_needed(exit_code, info)) { 1715 if (arch_ptrace_stop_needed(exit_code, info)) {
1639 /* 1716 /*
1640 * The arch code has something special to do before a 1717 * The arch code has something special to do before a
@@ -1655,21 +1732,49 @@ static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1655 } 1732 }
1656 1733
1657 /* 1734 /*
1658 * If there is a group stop in progress, 1735 * If @why is CLD_STOPPED, we're trapping to participate in a group
1659 * we must participate in the bookkeeping. 1736 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1737 * while siglock was released for the arch hook, PENDING could be
1738 * clear now. We act as if SIGCONT is received after TASK_TRACED
1739 * is entered - ignore it.
1660 */ 1740 */
1661 if (current->signal->group_stop_count > 0) 1741 if (why == CLD_STOPPED && (current->group_stop & GROUP_STOP_PENDING))
1662 --current->signal->group_stop_count; 1742 gstop_done = task_participate_group_stop(current);
1663 1743
1664 current->last_siginfo = info; 1744 current->last_siginfo = info;
1665 current->exit_code = exit_code; 1745 current->exit_code = exit_code;
1666 1746
1667 /* Let the debugger run. */ 1747 /*
1668 __set_current_state(TASK_TRACED); 1748 * TRACED should be visible before TRAPPING is cleared; otherwise,
1749 * the tracer might fail do_wait().
1750 */
1751 set_current_state(TASK_TRACED);
1752
1753 /*
1754 * We're committing to trapping. Clearing GROUP_STOP_TRAPPING and
1755 * transition to TASK_TRACED should be atomic with respect to
1756 * siglock. This hsould be done after the arch hook as siglock is
1757 * released and regrabbed across it.
1758 */
1759 task_clear_group_stop_trapping(current);
1760
1669 spin_unlock_irq(&current->sighand->siglock); 1761 spin_unlock_irq(&current->sighand->siglock);
1670 read_lock(&tasklist_lock); 1762 read_lock(&tasklist_lock);
1671 if (may_ptrace_stop()) { 1763 if (may_ptrace_stop()) {
1672 do_notify_parent_cldstop(current, CLD_TRAPPED); 1764 /*
1765 * Notify parents of the stop.
1766 *
1767 * While ptraced, there are two parents - the ptracer and
1768 * the real_parent of the group_leader. The ptracer should
1769 * know about every stop while the real parent is only
1770 * interested in the completion of group stop. The states
1771 * for the two don't interact with each other. Notify
1772 * separately unless they're gonna be duplicates.
1773 */
1774 do_notify_parent_cldstop(current, true, why);
1775 if (gstop_done && !real_parent_is_ptracer(current))
1776 do_notify_parent_cldstop(current, false, why);
1777
1673 /* 1778 /*
1674 * Don't want to allow preemption here, because 1779 * Don't want to allow preemption here, because
1675 * sys_ptrace() needs this task to be inactive. 1780 * sys_ptrace() needs this task to be inactive.
@@ -1684,7 +1789,16 @@ static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1684 /* 1789 /*
1685 * By the time we got the lock, our tracer went away. 1790 * By the time we got the lock, our tracer went away.
1686 * Don't drop the lock yet, another tracer may come. 1791 * Don't drop the lock yet, another tracer may come.
1792 *
1793 * If @gstop_done, the ptracer went away between group stop
1794 * completion and here. During detach, it would have set
1795 * GROUP_STOP_PENDING on us and we'll re-enter TASK_STOPPED
1796 * in do_signal_stop() on return, so notifying the real
1797 * parent of the group stop completion is enough.
1687 */ 1798 */
1799 if (gstop_done)
1800 do_notify_parent_cldstop(current, false, why);
1801
1688 __set_current_state(TASK_RUNNING); 1802 __set_current_state(TASK_RUNNING);
1689 if (clear_code) 1803 if (clear_code)
1690 current->exit_code = 0; 1804 current->exit_code = 0;
@@ -1728,7 +1842,7 @@ void ptrace_notify(int exit_code)
1728 1842
1729 /* Let the debugger run. */ 1843 /* Let the debugger run. */
1730 spin_lock_irq(&current->sighand->siglock); 1844 spin_lock_irq(&current->sighand->siglock);
1731 ptrace_stop(exit_code, 1, &info); 1845 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
1732 spin_unlock_irq(&current->sighand->siglock); 1846 spin_unlock_irq(&current->sighand->siglock);
1733} 1847}
1734 1848
@@ -1741,66 +1855,115 @@ void ptrace_notify(int exit_code)
1741static int do_signal_stop(int signr) 1855static int do_signal_stop(int signr)
1742{ 1856{
1743 struct signal_struct *sig = current->signal; 1857 struct signal_struct *sig = current->signal;
1744 int notify;
1745 1858
1746 if (!sig->group_stop_count) { 1859 if (!(current->group_stop & GROUP_STOP_PENDING)) {
1860 unsigned int gstop = GROUP_STOP_PENDING | GROUP_STOP_CONSUME;
1747 struct task_struct *t; 1861 struct task_struct *t;
1748 1862
1749 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) || 1863 /* signr will be recorded in task->group_stop for retries */
1864 WARN_ON_ONCE(signr & ~GROUP_STOP_SIGMASK);
1865
1866 if (!likely(current->group_stop & GROUP_STOP_DEQUEUED) ||
1750 unlikely(signal_group_exit(sig))) 1867 unlikely(signal_group_exit(sig)))
1751 return 0; 1868 return 0;
1752 /* 1869 /*
1753 * There is no group stop already in progress. 1870 * There is no group stop already in progress. We must
1754 * We must initiate one now. 1871 * initiate one now.
1872 *
1873 * While ptraced, a task may be resumed while group stop is
1874 * still in effect and then receive a stop signal and
1875 * initiate another group stop. This deviates from the
1876 * usual behavior as two consecutive stop signals can't
1877 * cause two group stops when !ptraced. That is why we
1878 * also check !task_is_stopped(t) below.
1879 *
1880 * The condition can be distinguished by testing whether
1881 * SIGNAL_STOP_STOPPED is already set. Don't generate
1882 * group_exit_code in such case.
1883 *
1884 * This is not necessary for SIGNAL_STOP_CONTINUED because
1885 * an intervening stop signal is required to cause two
1886 * continued events regardless of ptrace.
1755 */ 1887 */
1756 sig->group_exit_code = signr; 1888 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1889 sig->group_exit_code = signr;
1890 else
1891 WARN_ON_ONCE(!task_ptrace(current));
1757 1892
1893 current->group_stop &= ~GROUP_STOP_SIGMASK;
1894 current->group_stop |= signr | gstop;
1758 sig->group_stop_count = 1; 1895 sig->group_stop_count = 1;
1759 for (t = next_thread(current); t != current; t = next_thread(t)) 1896 for (t = next_thread(current); t != current;
1897 t = next_thread(t)) {
1898 t->group_stop &= ~GROUP_STOP_SIGMASK;
1760 /* 1899 /*
1761 * Setting state to TASK_STOPPED for a group 1900 * Setting state to TASK_STOPPED for a group
1762 * stop is always done with the siglock held, 1901 * stop is always done with the siglock held,
1763 * so this check has no races. 1902 * so this check has no races.
1764 */ 1903 */
1765 if (!(t->flags & PF_EXITING) && 1904 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
1766 !task_is_stopped_or_traced(t)) { 1905 t->group_stop |= signr | gstop;
1767 sig->group_stop_count++; 1906 sig->group_stop_count++;
1768 signal_wake_up(t, 0); 1907 signal_wake_up(t, 0);
1769 } 1908 }
1909 }
1770 } 1910 }
1771 /* 1911retry:
1772 * If there are no other threads in the group, or if there is 1912 if (likely(!task_ptrace(current))) {
1773 * a group stop in progress and we are the last to stop, report 1913 int notify = 0;
1774 * to the parent. When ptraced, every thread reports itself. 1914
1775 */ 1915 /*
1776 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0; 1916 * If there are no other threads in the group, or if there
1777 notify = tracehook_notify_jctl(notify, CLD_STOPPED); 1917 * is a group stop in progress and we are the last to stop,
1778 /* 1918 * report to the parent.
1779 * tracehook_notify_jctl() can drop and reacquire siglock, so 1919 */
1780 * we keep ->group_stop_count != 0 before the call. If SIGCONT 1920 if (task_participate_group_stop(current))
1781 * or SIGKILL comes in between ->group_stop_count == 0. 1921 notify = CLD_STOPPED;
1782 */ 1922
1783 if (sig->group_stop_count) {
1784 if (!--sig->group_stop_count)
1785 sig->flags = SIGNAL_STOP_STOPPED;
1786 current->exit_code = sig->group_exit_code;
1787 __set_current_state(TASK_STOPPED); 1923 __set_current_state(TASK_STOPPED);
1924 spin_unlock_irq(&current->sighand->siglock);
1925
1926 /*
1927 * Notify the parent of the group stop completion. Because
1928 * we're not holding either the siglock or tasklist_lock
1929 * here, ptracer may attach inbetween; however, this is for
1930 * group stop and should always be delivered to the real
1931 * parent of the group leader. The new ptracer will get
1932 * its notification when this task transitions into
1933 * TASK_TRACED.
1934 */
1935 if (notify) {
1936 read_lock(&tasklist_lock);
1937 do_notify_parent_cldstop(current, false, notify);
1938 read_unlock(&tasklist_lock);
1939 }
1940
1941 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1942 schedule();
1943
1944 spin_lock_irq(&current->sighand->siglock);
1945 } else {
1946 ptrace_stop(current->group_stop & GROUP_STOP_SIGMASK,
1947 CLD_STOPPED, 0, NULL);
1948 current->exit_code = 0;
1788 } 1949 }
1789 spin_unlock_irq(&current->sighand->siglock);
1790 1950
1791 if (notify) { 1951 /*
1792 read_lock(&tasklist_lock); 1952 * GROUP_STOP_PENDING could be set if another group stop has
1793 do_notify_parent_cldstop(current, notify); 1953 * started since being woken up or ptrace wants us to transit
1794 read_unlock(&tasklist_lock); 1954 * between TASK_STOPPED and TRACED. Retry group stop.
1955 */
1956 if (current->group_stop & GROUP_STOP_PENDING) {
1957 WARN_ON_ONCE(!(current->group_stop & GROUP_STOP_SIGMASK));
1958 goto retry;
1795 } 1959 }
1796 1960
1797 /* Now we don't run again until woken by SIGCONT or SIGKILL */ 1961 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
1798 do { 1962 task_clear_group_stop_trapping(current);
1799 schedule(); 1963
1800 } while (try_to_freeze()); 1964 spin_unlock_irq(&current->sighand->siglock);
1801 1965
1802 tracehook_finish_jctl(); 1966 tracehook_finish_jctl();
1803 current->exit_code = 0;
1804 1967
1805 return 1; 1968 return 1;
1806} 1969}
@@ -1814,7 +1977,7 @@ static int ptrace_signal(int signr, siginfo_t *info,
1814 ptrace_signal_deliver(regs, cookie); 1977 ptrace_signal_deliver(regs, cookie);
1815 1978
1816 /* Let the debugger run. */ 1979 /* Let the debugger run. */
1817 ptrace_stop(signr, 0, info); 1980 ptrace_stop(signr, CLD_TRAPPED, 0, info);
1818 1981
1819 /* We're back. Did the debugger cancel the sig? */ 1982 /* We're back. Did the debugger cancel the sig? */
1820 signr = current->exit_code; 1983 signr = current->exit_code;
@@ -1869,18 +2032,36 @@ relock:
1869 * the CLD_ si_code into SIGNAL_CLD_MASK bits. 2032 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1870 */ 2033 */
1871 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { 2034 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1872 int why = (signal->flags & SIGNAL_STOP_CONTINUED) 2035 struct task_struct *leader;
1873 ? CLD_CONTINUED : CLD_STOPPED; 2036 int why;
2037
2038 if (signal->flags & SIGNAL_CLD_CONTINUED)
2039 why = CLD_CONTINUED;
2040 else
2041 why = CLD_STOPPED;
2042
1874 signal->flags &= ~SIGNAL_CLD_MASK; 2043 signal->flags &= ~SIGNAL_CLD_MASK;
1875 2044
1876 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1877 spin_unlock_irq(&sighand->siglock); 2045 spin_unlock_irq(&sighand->siglock);
1878 2046
1879 if (why) { 2047 /*
1880 read_lock(&tasklist_lock); 2048 * Notify the parent that we're continuing. This event is
1881 do_notify_parent_cldstop(current->group_leader, why); 2049 * always per-process and doesn't make whole lot of sense
1882 read_unlock(&tasklist_lock); 2050 * for ptracers, who shouldn't consume the state via
1883 } 2051 * wait(2) either, but, for backward compatibility, notify
2052 * the ptracer of the group leader too unless it's gonna be
2053 * a duplicate.
2054 */
2055 read_lock(&tasklist_lock);
2056
2057 do_notify_parent_cldstop(current, false, why);
2058
2059 leader = current->group_leader;
2060 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2061 do_notify_parent_cldstop(leader, true, why);
2062
2063 read_unlock(&tasklist_lock);
2064
1884 goto relock; 2065 goto relock;
1885 } 2066 }
1886 2067
@@ -1897,8 +2078,8 @@ relock:
1897 if (unlikely(signr != 0)) 2078 if (unlikely(signr != 0))
1898 ka = return_ka; 2079 ka = return_ka;
1899 else { 2080 else {
1900 if (unlikely(signal->group_stop_count > 0) && 2081 if (unlikely(current->group_stop &
1901 do_signal_stop(0)) 2082 GROUP_STOP_PENDING) && do_signal_stop(0))
1902 goto relock; 2083 goto relock;
1903 2084
1904 signr = dequeue_signal(current, &current->blocked, 2085 signr = dequeue_signal(current, &current->blocked,
@@ -2017,10 +2198,42 @@ relock:
2017 return signr; 2198 return signr;
2018} 2199}
2019 2200
2201/*
2202 * It could be that complete_signal() picked us to notify about the
2203 * group-wide signal. Other threads should be notified now to take
2204 * the shared signals in @which since we will not.
2205 */
2206static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2207{
2208 sigset_t retarget;
2209 struct task_struct *t;
2210
2211 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2212 if (sigisemptyset(&retarget))
2213 return;
2214
2215 t = tsk;
2216 while_each_thread(tsk, t) {
2217 if (t->flags & PF_EXITING)
2218 continue;
2219
2220 if (!has_pending_signals(&retarget, &t->blocked))
2221 continue;
2222 /* Remove the signals this thread can handle. */
2223 sigandsets(&retarget, &retarget, &t->blocked);
2224
2225 if (!signal_pending(t))
2226 signal_wake_up(t, 0);
2227
2228 if (sigisemptyset(&retarget))
2229 break;
2230 }
2231}
2232
2020void exit_signals(struct task_struct *tsk) 2233void exit_signals(struct task_struct *tsk)
2021{ 2234{
2022 int group_stop = 0; 2235 int group_stop = 0;
2023 struct task_struct *t; 2236 sigset_t unblocked;
2024 2237
2025 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) { 2238 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2026 tsk->flags |= PF_EXITING; 2239 tsk->flags |= PF_EXITING;
@@ -2036,26 +2249,23 @@ void exit_signals(struct task_struct *tsk)
2036 if (!signal_pending(tsk)) 2249 if (!signal_pending(tsk))
2037 goto out; 2250 goto out;
2038 2251
2039 /* 2252 unblocked = tsk->blocked;
2040 * It could be that __group_complete_signal() choose us to 2253 signotset(&unblocked);
2041 * notify about group-wide signal. Another thread should be 2254 retarget_shared_pending(tsk, &unblocked);
2042 * woken now to take the signal since we will not.
2043 */
2044 for (t = tsk; (t = next_thread(t)) != tsk; )
2045 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2046 recalc_sigpending_and_wake(t);
2047 2255
2048 if (unlikely(tsk->signal->group_stop_count) && 2256 if (unlikely(tsk->group_stop & GROUP_STOP_PENDING) &&
2049 !--tsk->signal->group_stop_count) { 2257 task_participate_group_stop(tsk))
2050 tsk->signal->flags = SIGNAL_STOP_STOPPED; 2258 group_stop = CLD_STOPPED;
2051 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
2052 }
2053out: 2259out:
2054 spin_unlock_irq(&tsk->sighand->siglock); 2260 spin_unlock_irq(&tsk->sighand->siglock);
2055 2261
2262 /*
2263 * If group stop has completed, deliver the notification. This
2264 * should always go to the real parent of the group leader.
2265 */
2056 if (unlikely(group_stop)) { 2266 if (unlikely(group_stop)) {
2057 read_lock(&tasklist_lock); 2267 read_lock(&tasklist_lock);
2058 do_notify_parent_cldstop(tsk, group_stop); 2268 do_notify_parent_cldstop(tsk, false, group_stop);
2059 read_unlock(&tasklist_lock); 2269 read_unlock(&tasklist_lock);
2060 } 2270 }
2061} 2271}
@@ -2089,11 +2299,33 @@ long do_no_restart_syscall(struct restart_block *param)
2089 return -EINTR; 2299 return -EINTR;
2090} 2300}
2091 2301
2092/* 2302static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2093 * We don't need to get the kernel lock - this is all local to this 2303{
2094 * particular thread.. (and that's good, because this is _heavily_ 2304 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2095 * used by various programs) 2305 sigset_t newblocked;
2306 /* A set of now blocked but previously unblocked signals. */
2307 sigandnsets(&newblocked, newset, &current->blocked);
2308 retarget_shared_pending(tsk, &newblocked);
2309 }
2310 tsk->blocked = *newset;
2311 recalc_sigpending();
2312}
2313
2314/**
2315 * set_current_blocked - change current->blocked mask
2316 * @newset: new mask
2317 *
2318 * It is wrong to change ->blocked directly, this helper should be used
2319 * to ensure the process can't miss a shared signal we are going to block.
2096 */ 2320 */
2321void set_current_blocked(const sigset_t *newset)
2322{
2323 struct task_struct *tsk = current;
2324
2325 spin_lock_irq(&tsk->sighand->siglock);
2326 __set_task_blocked(tsk, newset);
2327 spin_unlock_irq(&tsk->sighand->siglock);
2328}
2097 2329
2098/* 2330/*
2099 * This is also useful for kernel threads that want to temporarily 2331 * This is also useful for kernel threads that want to temporarily
@@ -2105,73 +2337,66 @@ long do_no_restart_syscall(struct restart_block *param)
2105 */ 2337 */
2106int sigprocmask(int how, sigset_t *set, sigset_t *oldset) 2338int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2107{ 2339{
2108 int error; 2340 struct task_struct *tsk = current;
2341 sigset_t newset;
2109 2342
2110 spin_lock_irq(&current->sighand->siglock); 2343 /* Lockless, only current can change ->blocked, never from irq */
2111 if (oldset) 2344 if (oldset)
2112 *oldset = current->blocked; 2345 *oldset = tsk->blocked;
2113 2346
2114 error = 0;
2115 switch (how) { 2347 switch (how) {
2116 case SIG_BLOCK: 2348 case SIG_BLOCK:
2117 sigorsets(&current->blocked, &current->blocked, set); 2349 sigorsets(&newset, &tsk->blocked, set);
2118 break; 2350 break;
2119 case SIG_UNBLOCK: 2351 case SIG_UNBLOCK:
2120 signandsets(&current->blocked, &current->blocked, set); 2352 sigandnsets(&newset, &tsk->blocked, set);
2121 break; 2353 break;
2122 case SIG_SETMASK: 2354 case SIG_SETMASK:
2123 current->blocked = *set; 2355 newset = *set;
2124 break; 2356 break;
2125 default: 2357 default:
2126 error = -EINVAL; 2358 return -EINVAL;
2127 } 2359 }
2128 recalc_sigpending();
2129 spin_unlock_irq(&current->sighand->siglock);
2130 2360
2131 return error; 2361 set_current_blocked(&newset);
2362 return 0;
2132} 2363}
2133 2364
2134/** 2365/**
2135 * sys_rt_sigprocmask - change the list of currently blocked signals 2366 * sys_rt_sigprocmask - change the list of currently blocked signals
2136 * @how: whether to add, remove, or set signals 2367 * @how: whether to add, remove, or set signals
2137 * @set: stores pending signals 2368 * @nset: stores pending signals
2138 * @oset: previous value of signal mask if non-null 2369 * @oset: previous value of signal mask if non-null
2139 * @sigsetsize: size of sigset_t type 2370 * @sigsetsize: size of sigset_t type
2140 */ 2371 */
2141SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set, 2372SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2142 sigset_t __user *, oset, size_t, sigsetsize) 2373 sigset_t __user *, oset, size_t, sigsetsize)
2143{ 2374{
2144 int error = -EINVAL;
2145 sigset_t old_set, new_set; 2375 sigset_t old_set, new_set;
2376 int error;
2146 2377
2147 /* XXX: Don't preclude handling different sized sigset_t's. */ 2378 /* XXX: Don't preclude handling different sized sigset_t's. */
2148 if (sigsetsize != sizeof(sigset_t)) 2379 if (sigsetsize != sizeof(sigset_t))
2149 goto out; 2380 return -EINVAL;
2150 2381
2151 if (set) { 2382 old_set = current->blocked;
2152 error = -EFAULT; 2383
2153 if (copy_from_user(&new_set, set, sizeof(*set))) 2384 if (nset) {
2154 goto out; 2385 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2386 return -EFAULT;
2155 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 2387 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2156 2388
2157 error = sigprocmask(how, &new_set, &old_set); 2389 error = sigprocmask(how, &new_set, NULL);
2158 if (error) 2390 if (error)
2159 goto out; 2391 return error;
2160 if (oset) 2392 }
2161 goto set_old;
2162 } else if (oset) {
2163 spin_lock_irq(&current->sighand->siglock);
2164 old_set = current->blocked;
2165 spin_unlock_irq(&current->sighand->siglock);
2166 2393
2167 set_old: 2394 if (oset) {
2168 error = -EFAULT; 2395 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2169 if (copy_to_user(oset, &old_set, sizeof(*oset))) 2396 return -EFAULT;
2170 goto out;
2171 } 2397 }
2172 error = 0; 2398
2173out: 2399 return 0;
2174 return error;
2175} 2400}
2176 2401
2177long do_sigpending(void __user *set, unsigned long sigsetsize) 2402long do_sigpending(void __user *set, unsigned long sigsetsize)
@@ -2284,6 +2509,66 @@ int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2284#endif 2509#endif
2285 2510
2286/** 2511/**
2512 * do_sigtimedwait - wait for queued signals specified in @which
2513 * @which: queued signals to wait for
2514 * @info: if non-null, the signal's siginfo is returned here
2515 * @ts: upper bound on process time suspension
2516 */
2517int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2518 const struct timespec *ts)
2519{
2520 struct task_struct *tsk = current;
2521 long timeout = MAX_SCHEDULE_TIMEOUT;
2522 sigset_t mask = *which;
2523 int sig;
2524
2525 if (ts) {
2526 if (!timespec_valid(ts))
2527 return -EINVAL;
2528 timeout = timespec_to_jiffies(ts);
2529 /*
2530 * We can be close to the next tick, add another one
2531 * to ensure we will wait at least the time asked for.
2532 */
2533 if (ts->tv_sec || ts->tv_nsec)
2534 timeout++;
2535 }
2536
2537 /*
2538 * Invert the set of allowed signals to get those we want to block.
2539 */
2540 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2541 signotset(&mask);
2542
2543 spin_lock_irq(&tsk->sighand->siglock);
2544 sig = dequeue_signal(tsk, &mask, info);
2545 if (!sig && timeout) {
2546 /*
2547 * None ready, temporarily unblock those we're interested
2548 * while we are sleeping in so that we'll be awakened when
2549 * they arrive. Unblocking is always fine, we can avoid
2550 * set_current_blocked().
2551 */
2552 tsk->real_blocked = tsk->blocked;
2553 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2554 recalc_sigpending();
2555 spin_unlock_irq(&tsk->sighand->siglock);
2556
2557 timeout = schedule_timeout_interruptible(timeout);
2558
2559 spin_lock_irq(&tsk->sighand->siglock);
2560 __set_task_blocked(tsk, &tsk->real_blocked);
2561 siginitset(&tsk->real_blocked, 0);
2562 sig = dequeue_signal(tsk, &mask, info);
2563 }
2564 spin_unlock_irq(&tsk->sighand->siglock);
2565
2566 if (sig)
2567 return sig;
2568 return timeout ? -EINTR : -EAGAIN;
2569}
2570
2571/**
2287 * sys_rt_sigtimedwait - synchronously wait for queued signals specified 2572 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2288 * in @uthese 2573 * in @uthese
2289 * @uthese: queued signals to wait for 2574 * @uthese: queued signals to wait for
@@ -2295,11 +2580,10 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2295 siginfo_t __user *, uinfo, const struct timespec __user *, uts, 2580 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2296 size_t, sigsetsize) 2581 size_t, sigsetsize)
2297{ 2582{
2298 int ret, sig;
2299 sigset_t these; 2583 sigset_t these;
2300 struct timespec ts; 2584 struct timespec ts;
2301 siginfo_t info; 2585 siginfo_t info;
2302 long timeout = 0; 2586 int ret;
2303 2587
2304 /* XXX: Don't preclude handling different sized sigset_t's. */ 2588 /* XXX: Don't preclude handling different sized sigset_t's. */
2305 if (sigsetsize != sizeof(sigset_t)) 2589 if (sigsetsize != sizeof(sigset_t))
@@ -2308,61 +2592,16 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2308 if (copy_from_user(&these, uthese, sizeof(these))) 2592 if (copy_from_user(&these, uthese, sizeof(these)))
2309 return -EFAULT; 2593 return -EFAULT;
2310 2594
2311 /*
2312 * Invert the set of allowed signals to get those we
2313 * want to block.
2314 */
2315 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2316 signotset(&these);
2317
2318 if (uts) { 2595 if (uts) {
2319 if (copy_from_user(&ts, uts, sizeof(ts))) 2596 if (copy_from_user(&ts, uts, sizeof(ts)))
2320 return -EFAULT; 2597 return -EFAULT;
2321 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2322 || ts.tv_sec < 0)
2323 return -EINVAL;
2324 } 2598 }
2325 2599
2326 spin_lock_irq(&current->sighand->siglock); 2600 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
2327 sig = dequeue_signal(current, &these, &info);
2328 if (!sig) {
2329 timeout = MAX_SCHEDULE_TIMEOUT;
2330 if (uts)
2331 timeout = (timespec_to_jiffies(&ts)
2332 + (ts.tv_sec || ts.tv_nsec));
2333
2334 if (timeout) {
2335 /*
2336 * None ready -- temporarily unblock those we're
2337 * interested while we are sleeping in so that we'll
2338 * be awakened when they arrive.
2339 */
2340 current->real_blocked = current->blocked;
2341 sigandsets(&current->blocked, &current->blocked, &these);
2342 recalc_sigpending();
2343 spin_unlock_irq(&current->sighand->siglock);
2344
2345 timeout = schedule_timeout_interruptible(timeout);
2346
2347 spin_lock_irq(&current->sighand->siglock);
2348 sig = dequeue_signal(current, &these, &info);
2349 current->blocked = current->real_blocked;
2350 siginitset(&current->real_blocked, 0);
2351 recalc_sigpending();
2352 }
2353 }
2354 spin_unlock_irq(&current->sighand->siglock);
2355 2601
2356 if (sig) { 2602 if (ret > 0 && uinfo) {
2357 ret = sig; 2603 if (copy_siginfo_to_user(uinfo, &info))
2358 if (uinfo) { 2604 ret = -EFAULT;
2359 if (copy_siginfo_to_user(uinfo, &info))
2360 ret = -EFAULT;
2361 }
2362 } else {
2363 ret = -EAGAIN;
2364 if (timeout)
2365 ret = -EINTR;
2366 } 2605 }
2367 2606
2368 return ret; 2607 return ret;
@@ -2650,60 +2889,51 @@ SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2650/** 2889/**
2651 * sys_sigprocmask - examine and change blocked signals 2890 * sys_sigprocmask - examine and change blocked signals
2652 * @how: whether to add, remove, or set signals 2891 * @how: whether to add, remove, or set signals
2653 * @set: signals to add or remove (if non-null) 2892 * @nset: signals to add or remove (if non-null)
2654 * @oset: previous value of signal mask if non-null 2893 * @oset: previous value of signal mask if non-null
2655 * 2894 *
2656 * Some platforms have their own version with special arguments; 2895 * Some platforms have their own version with special arguments;
2657 * others support only sys_rt_sigprocmask. 2896 * others support only sys_rt_sigprocmask.
2658 */ 2897 */
2659 2898
2660SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set, 2899SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
2661 old_sigset_t __user *, oset) 2900 old_sigset_t __user *, oset)
2662{ 2901{
2663 int error;
2664 old_sigset_t old_set, new_set; 2902 old_sigset_t old_set, new_set;
2903 sigset_t new_blocked;
2665 2904
2666 if (set) { 2905 old_set = current->blocked.sig[0];
2667 error = -EFAULT; 2906
2668 if (copy_from_user(&new_set, set, sizeof(*set))) 2907 if (nset) {
2669 goto out; 2908 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2909 return -EFAULT;
2670 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP)); 2910 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2671 2911
2672 spin_lock_irq(&current->sighand->siglock); 2912 new_blocked = current->blocked;
2673 old_set = current->blocked.sig[0];
2674 2913
2675 error = 0;
2676 switch (how) { 2914 switch (how) {
2677 default:
2678 error = -EINVAL;
2679 break;
2680 case SIG_BLOCK: 2915 case SIG_BLOCK:
2681 sigaddsetmask(&current->blocked, new_set); 2916 sigaddsetmask(&new_blocked, new_set);
2682 break; 2917 break;
2683 case SIG_UNBLOCK: 2918 case SIG_UNBLOCK:
2684 sigdelsetmask(&current->blocked, new_set); 2919 sigdelsetmask(&new_blocked, new_set);
2685 break; 2920 break;
2686 case SIG_SETMASK: 2921 case SIG_SETMASK:
2687 current->blocked.sig[0] = new_set; 2922 new_blocked.sig[0] = new_set;
2688 break; 2923 break;
2924 default:
2925 return -EINVAL;
2689 } 2926 }
2690 2927
2691 recalc_sigpending(); 2928 set_current_blocked(&new_blocked);
2692 spin_unlock_irq(&current->sighand->siglock); 2929 }
2693 if (error) 2930
2694 goto out; 2931 if (oset) {
2695 if (oset)
2696 goto set_old;
2697 } else if (oset) {
2698 old_set = current->blocked.sig[0];
2699 set_old:
2700 error = -EFAULT;
2701 if (copy_to_user(oset, &old_set, sizeof(*oset))) 2932 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2702 goto out; 2933 return -EFAULT;
2703 } 2934 }
2704 error = 0; 2935
2705out: 2936 return 0;
2706 return error;
2707} 2937}
2708#endif /* __ARCH_WANT_SYS_SIGPROCMASK */ 2938#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2709 2939
@@ -2793,8 +3023,10 @@ SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2793 3023
2794SYSCALL_DEFINE0(pause) 3024SYSCALL_DEFINE0(pause)
2795{ 3025{
2796 current->state = TASK_INTERRUPTIBLE; 3026 while (!signal_pending(current)) {
2797 schedule(); 3027 current->state = TASK_INTERRUPTIBLE;
3028 schedule();
3029 }
2798 return -ERESTARTNOHAND; 3030 return -ERESTARTNOHAND;
2799} 3031}
2800 3032