aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/signal.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/signal.c')
-rw-r--r--kernel/signal.c463
1 files changed, 286 insertions, 177 deletions
diff --git a/kernel/signal.c b/kernel/signal.c
index 86c32b884f8e..291c9700be75 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -87,7 +87,7 @@ static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
87 /* 87 /*
88 * Tracers may want to know about even ignored signals. 88 * Tracers may want to know about even ignored signals.
89 */ 89 */
90 return !tracehook_consider_ignored_signal(t, sig); 90 return !t->ptrace;
91} 91}
92 92
93/* 93/*
@@ -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->group_stop & GROUP_STOP_PENDING) || 127 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
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);
@@ -150,9 +150,7 @@ void recalc_sigpending_and_wake(struct task_struct *t)
150 150
151void recalc_sigpending(void) 151void recalc_sigpending(void)
152{ 152{
153 if (unlikely(tracehook_force_sigpending())) 153 if (!recalc_sigpending_tsk(current) && !freezing(current))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
156 clear_thread_flag(TIF_SIGPENDING); 154 clear_thread_flag(TIF_SIGPENDING);
157 155
158} 156}
@@ -224,47 +222,93 @@ static inline void print_dropped_signal(int sig)
224} 222}
225 223
226/** 224/**
227 * task_clear_group_stop_trapping - clear group stop trapping bit 225 * task_set_jobctl_pending - set jobctl pending bits
228 * @task: target task 226 * @task: target task
227 * @mask: pending bits to set
229 * 228 *
230 * If GROUP_STOP_TRAPPING is set, a ptracer is waiting for us. Clear it 229 * Clear @mask from @task->jobctl. @mask must be subset of
231 * and wake up the ptracer. Note that we don't need any further locking. 230 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
232 * @task->siglock guarantees that @task->parent points to the ptracer. 231 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
232 * cleared. If @task is already being killed or exiting, this function
233 * becomes noop.
233 * 234 *
234 * CONTEXT: 235 * CONTEXT:
235 * Must be called with @task->sighand->siglock held. 236 * Must be called with @task->sighand->siglock held.
237 *
238 * RETURNS:
239 * %true if @mask is set, %false if made noop because @task was dying.
236 */ 240 */
237static void task_clear_group_stop_trapping(struct task_struct *task) 241bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask)
238{ 242{
239 if (unlikely(task->group_stop & GROUP_STOP_TRAPPING)) { 243 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
240 task->group_stop &= ~GROUP_STOP_TRAPPING; 244 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
241 __wake_up_sync_key(&task->parent->signal->wait_chldexit, 245 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
242 TASK_UNINTERRUPTIBLE, 1, task); 246
247 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
248 return false;
249
250 if (mask & JOBCTL_STOP_SIGMASK)
251 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
252
253 task->jobctl |= mask;
254 return true;
255}
256
257/**
258 * task_clear_jobctl_trapping - clear jobctl trapping bit
259 * @task: target task
260 *
261 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
262 * Clear it and wake up the ptracer. Note that we don't need any further
263 * locking. @task->siglock guarantees that @task->parent points to the
264 * ptracer.
265 *
266 * CONTEXT:
267 * Must be called with @task->sighand->siglock held.
268 */
269void task_clear_jobctl_trapping(struct task_struct *task)
270{
271 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
272 task->jobctl &= ~JOBCTL_TRAPPING;
273 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
243 } 274 }
244} 275}
245 276
246/** 277/**
247 * task_clear_group_stop_pending - clear pending group stop 278 * task_clear_jobctl_pending - clear jobctl pending bits
248 * @task: target task 279 * @task: target task
280 * @mask: pending bits to clear
281 *
282 * Clear @mask from @task->jobctl. @mask must be subset of
283 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
284 * STOP bits are cleared together.
249 * 285 *
250 * Clear group stop states for @task. 286 * If clearing of @mask leaves no stop or trap pending, this function calls
287 * task_clear_jobctl_trapping().
251 * 288 *
252 * CONTEXT: 289 * CONTEXT:
253 * Must be called with @task->sighand->siglock held. 290 * Must be called with @task->sighand->siglock held.
254 */ 291 */
255void task_clear_group_stop_pending(struct task_struct *task) 292void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
256{ 293{
257 task->group_stop &= ~(GROUP_STOP_PENDING | GROUP_STOP_CONSUME | 294 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
258 GROUP_STOP_DEQUEUED); 295
296 if (mask & JOBCTL_STOP_PENDING)
297 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
298
299 task->jobctl &= ~mask;
300
301 if (!(task->jobctl & JOBCTL_PENDING_MASK))
302 task_clear_jobctl_trapping(task);
259} 303}
260 304
261/** 305/**
262 * task_participate_group_stop - participate in a group stop 306 * task_participate_group_stop - participate in a group stop
263 * @task: task participating in a group stop 307 * @task: task participating in a group stop
264 * 308 *
265 * @task has GROUP_STOP_PENDING set and is participating in a group stop. 309 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
266 * Group stop states are cleared and the group stop count is consumed if 310 * 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 311 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
268 * stop, the appropriate %SIGNAL_* flags are set. 312 * stop, the appropriate %SIGNAL_* flags are set.
269 * 313 *
270 * CONTEXT: 314 * CONTEXT:
@@ -277,11 +321,11 @@ void task_clear_group_stop_pending(struct task_struct *task)
277static bool task_participate_group_stop(struct task_struct *task) 321static bool task_participate_group_stop(struct task_struct *task)
278{ 322{
279 struct signal_struct *sig = task->signal; 323 struct signal_struct *sig = task->signal;
280 bool consume = task->group_stop & GROUP_STOP_CONSUME; 324 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
281 325
282 WARN_ON_ONCE(!(task->group_stop & GROUP_STOP_PENDING)); 326 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
283 327
284 task_clear_group_stop_pending(task); 328 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
285 329
286 if (!consume) 330 if (!consume)
287 return false; 331 return false;
@@ -449,7 +493,8 @@ int unhandled_signal(struct task_struct *tsk, int sig)
449 return 1; 493 return 1;
450 if (handler != SIG_IGN && handler != SIG_DFL) 494 if (handler != SIG_IGN && handler != SIG_DFL)
451 return 0; 495 return 0;
452 return !tracehook_consider_fatal_signal(tsk, sig); 496 /* if ptraced, let the tracer determine */
497 return !tsk->ptrace;
453} 498}
454 499
455/* 500/*
@@ -604,7 +649,7 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
604 * is to alert stop-signal processing code when another 649 * is to alert stop-signal processing code when another
605 * processor has come along and cleared the flag. 650 * processor has come along and cleared the flag.
606 */ 651 */
607 current->group_stop |= GROUP_STOP_DEQUEUED; 652 current->jobctl |= JOBCTL_STOP_DEQUEUED;
608 } 653 }
609 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) { 654 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
610 /* 655 /*
@@ -773,6 +818,32 @@ static int check_kill_permission(int sig, struct siginfo *info,
773 return security_task_kill(t, info, sig, 0); 818 return security_task_kill(t, info, sig, 0);
774} 819}
775 820
821/**
822 * ptrace_trap_notify - schedule trap to notify ptracer
823 * @t: tracee wanting to notify tracer
824 *
825 * This function schedules sticky ptrace trap which is cleared on the next
826 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
827 * ptracer.
828 *
829 * If @t is running, STOP trap will be taken. If trapped for STOP and
830 * ptracer is listening for events, tracee is woken up so that it can
831 * re-trap for the new event. If trapped otherwise, STOP trap will be
832 * eventually taken without returning to userland after the existing traps
833 * are finished by PTRACE_CONT.
834 *
835 * CONTEXT:
836 * Must be called with @task->sighand->siglock held.
837 */
838static void ptrace_trap_notify(struct task_struct *t)
839{
840 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
841 assert_spin_locked(&t->sighand->siglock);
842
843 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
844 signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
845}
846
776/* 847/*
777 * Handle magic process-wide effects of stop/continue signals. Unlike 848 * Handle magic process-wide effects of stop/continue signals. Unlike
778 * the signal actions, these happen immediately at signal-generation 849 * the signal actions, these happen immediately at signal-generation
@@ -809,9 +880,12 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
809 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending); 880 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
810 t = p; 881 t = p;
811 do { 882 do {
812 task_clear_group_stop_pending(t); 883 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
813 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending); 884 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
814 wake_up_state(t, __TASK_STOPPED); 885 if (likely(!(t->ptrace & PT_SEIZED)))
886 wake_up_state(t, __TASK_STOPPED);
887 else
888 ptrace_trap_notify(t);
815 } while_each_thread(p, t); 889 } while_each_thread(p, t);
816 890
817 /* 891 /*
@@ -908,8 +982,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
908 if (sig_fatal(p, sig) && 982 if (sig_fatal(p, sig) &&
909 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) && 983 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
910 !sigismember(&t->real_blocked, sig) && 984 !sigismember(&t->real_blocked, sig) &&
911 (sig == SIGKILL || 985 (sig == SIGKILL || !t->ptrace)) {
912 !tracehook_consider_fatal_signal(t, sig))) {
913 /* 986 /*
914 * This signal will be fatal to the whole group. 987 * This signal will be fatal to the whole group.
915 */ 988 */
@@ -925,7 +998,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
925 signal->group_stop_count = 0; 998 signal->group_stop_count = 0;
926 t = p; 999 t = p;
927 do { 1000 do {
928 task_clear_group_stop_pending(t); 1001 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
929 sigaddset(&t->pending.signal, SIGKILL); 1002 sigaddset(&t->pending.signal, SIGKILL);
930 signal_wake_up(t, 1); 1003 signal_wake_up(t, 1);
931 } while_each_thread(p, t); 1004 } while_each_thread(p, t);
@@ -1160,7 +1233,7 @@ int zap_other_threads(struct task_struct *p)
1160 p->signal->group_stop_count = 0; 1233 p->signal->group_stop_count = 0;
1161 1234
1162 while_each_thread(p, t) { 1235 while_each_thread(p, t) {
1163 task_clear_group_stop_pending(t); 1236 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1164 count++; 1237 count++;
1165 1238
1166 /* Don't bother with already dead threads */ 1239 /* Don't bother with already dead threads */
@@ -1178,18 +1251,25 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1178{ 1251{
1179 struct sighand_struct *sighand; 1252 struct sighand_struct *sighand;
1180 1253
1181 rcu_read_lock();
1182 for (;;) { 1254 for (;;) {
1255 local_irq_save(*flags);
1256 rcu_read_lock();
1183 sighand = rcu_dereference(tsk->sighand); 1257 sighand = rcu_dereference(tsk->sighand);
1184 if (unlikely(sighand == NULL)) 1258 if (unlikely(sighand == NULL)) {
1259 rcu_read_unlock();
1260 local_irq_restore(*flags);
1185 break; 1261 break;
1262 }
1186 1263
1187 spin_lock_irqsave(&sighand->siglock, *flags); 1264 spin_lock(&sighand->siglock);
1188 if (likely(sighand == tsk->sighand)) 1265 if (likely(sighand == tsk->sighand)) {
1266 rcu_read_unlock();
1189 break; 1267 break;
1190 spin_unlock_irqrestore(&sighand->siglock, *flags); 1268 }
1269 spin_unlock(&sighand->siglock);
1270 rcu_read_unlock();
1271 local_irq_restore(*flags);
1191 } 1272 }
1192 rcu_read_unlock();
1193 1273
1194 return sighand; 1274 return sighand;
1195} 1275}
@@ -1504,22 +1584,22 @@ ret:
1504 * Let a parent know about the death of a child. 1584 * Let a parent know about the death of a child.
1505 * For a stopped/continued status change, use do_notify_parent_cldstop instead. 1585 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1506 * 1586 *
1507 * Returns -1 if our parent ignored us and so we've switched to 1587 * Returns true if our parent ignored us and so we've switched to
1508 * self-reaping, or else @sig. 1588 * self-reaping.
1509 */ 1589 */
1510int do_notify_parent(struct task_struct *tsk, int sig) 1590bool do_notify_parent(struct task_struct *tsk, int sig)
1511{ 1591{
1512 struct siginfo info; 1592 struct siginfo info;
1513 unsigned long flags; 1593 unsigned long flags;
1514 struct sighand_struct *psig; 1594 struct sighand_struct *psig;
1515 int ret = sig; 1595 bool autoreap = false;
1516 1596
1517 BUG_ON(sig == -1); 1597 BUG_ON(sig == -1);
1518 1598
1519 /* do_notify_parent_cldstop should have been called instead. */ 1599 /* do_notify_parent_cldstop should have been called instead. */
1520 BUG_ON(task_is_stopped_or_traced(tsk)); 1600 BUG_ON(task_is_stopped_or_traced(tsk));
1521 1601
1522 BUG_ON(!task_ptrace(tsk) && 1602 BUG_ON(!tsk->ptrace &&
1523 (tsk->group_leader != tsk || !thread_group_empty(tsk))); 1603 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1524 1604
1525 info.si_signo = sig; 1605 info.si_signo = sig;
@@ -1558,7 +1638,7 @@ int do_notify_parent(struct task_struct *tsk, int sig)
1558 1638
1559 psig = tsk->parent->sighand; 1639 psig = tsk->parent->sighand;
1560 spin_lock_irqsave(&psig->siglock, flags); 1640 spin_lock_irqsave(&psig->siglock, flags);
1561 if (!task_ptrace(tsk) && sig == SIGCHLD && 1641 if (!tsk->ptrace && sig == SIGCHLD &&
1562 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN || 1642 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1563 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) { 1643 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1564 /* 1644 /*
@@ -1576,16 +1656,16 @@ int do_notify_parent(struct task_struct *tsk, int sig)
1576 * is implementation-defined: we do (if you don't want 1656 * is implementation-defined: we do (if you don't want
1577 * it, just use SIG_IGN instead). 1657 * it, just use SIG_IGN instead).
1578 */ 1658 */
1579 ret = tsk->exit_signal = -1; 1659 autoreap = true;
1580 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) 1660 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1581 sig = -1; 1661 sig = 0;
1582 } 1662 }
1583 if (valid_signal(sig) && sig > 0) 1663 if (valid_signal(sig) && sig)
1584 __group_send_sig_info(sig, &info, tsk->parent); 1664 __group_send_sig_info(sig, &info, tsk->parent);
1585 __wake_up_parent(tsk, tsk->parent); 1665 __wake_up_parent(tsk, tsk->parent);
1586 spin_unlock_irqrestore(&psig->siglock, flags); 1666 spin_unlock_irqrestore(&psig->siglock, flags);
1587 1667
1588 return ret; 1668 return autoreap;
1589} 1669}
1590 1670
1591/** 1671/**
@@ -1658,7 +1738,7 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
1658 1738
1659static inline int may_ptrace_stop(void) 1739static inline int may_ptrace_stop(void)
1660{ 1740{
1661 if (!likely(task_ptrace(current))) 1741 if (!likely(current->ptrace))
1662 return 0; 1742 return 0;
1663 /* 1743 /*
1664 * Are we in the middle of do_coredump? 1744 * Are we in the middle of do_coredump?
@@ -1687,15 +1767,6 @@ static int sigkill_pending(struct task_struct *tsk)
1687} 1767}
1688 1768
1689/* 1769/*
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/*
1699 * This must be called with current->sighand->siglock held. 1770 * This must be called with current->sighand->siglock held.
1700 * 1771 *
1701 * This should be the path for all ptrace stops. 1772 * This should be the path for all ptrace stops.
@@ -1732,31 +1803,34 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1732 } 1803 }
1733 1804
1734 /* 1805 /*
1735 * If @why is CLD_STOPPED, we're trapping to participate in a group 1806 * We're committing to trapping. TRACED should be visible before
1736 * stop. Do the bookkeeping. Note that if SIGCONT was delievered 1807 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1737 * while siglock was released for the arch hook, PENDING could be 1808 * Also, transition to TRACED and updates to ->jobctl should be
1738 * clear now. We act as if SIGCONT is received after TASK_TRACED 1809 * atomic with respect to siglock and should be done after the arch
1739 * is entered - ignore it. 1810 * hook as siglock is released and regrabbed across it.
1740 */ 1811 */
1741 if (why == CLD_STOPPED && (current->group_stop & GROUP_STOP_PENDING)) 1812 set_current_state(TASK_TRACED);
1742 gstop_done = task_participate_group_stop(current);
1743 1813
1744 current->last_siginfo = info; 1814 current->last_siginfo = info;
1745 current->exit_code = exit_code; 1815 current->exit_code = exit_code;
1746 1816
1747 /* 1817 /*
1748 * TRACED should be visible before TRAPPING is cleared; otherwise, 1818 * If @why is CLD_STOPPED, we're trapping to participate in a group
1749 * the tracer might fail do_wait(). 1819 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1820 * across siglock relocks since INTERRUPT was scheduled, PENDING
1821 * could be clear now. We act as if SIGCONT is received after
1822 * TASK_TRACED is entered - ignore it.
1750 */ 1823 */
1751 set_current_state(TASK_TRACED); 1824 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
1825 gstop_done = task_participate_group_stop(current);
1752 1826
1753 /* 1827 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
1754 * We're committing to trapping. Clearing GROUP_STOP_TRAPPING and 1828 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
1755 * transition to TASK_TRACED should be atomic with respect to 1829 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1756 * siglock. This hsould be done after the arch hook as siglock is 1830 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
1757 * released and regrabbed across it. 1831
1758 */ 1832 /* entering a trap, clear TRAPPING */
1759 task_clear_group_stop_trapping(current); 1833 task_clear_jobctl_trapping(current);
1760 1834
1761 spin_unlock_irq(&current->sighand->siglock); 1835 spin_unlock_irq(&current->sighand->siglock);
1762 read_lock(&tasklist_lock); 1836 read_lock(&tasklist_lock);
@@ -1772,7 +1846,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1772 * separately unless they're gonna be duplicates. 1846 * separately unless they're gonna be duplicates.
1773 */ 1847 */
1774 do_notify_parent_cldstop(current, true, why); 1848 do_notify_parent_cldstop(current, true, why);
1775 if (gstop_done && !real_parent_is_ptracer(current)) 1849 if (gstop_done && ptrace_reparented(current))
1776 do_notify_parent_cldstop(current, false, why); 1850 do_notify_parent_cldstop(current, false, why);
1777 1851
1778 /* 1852 /*
@@ -1792,9 +1866,9 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1792 * 1866 *
1793 * If @gstop_done, the ptracer went away between group stop 1867 * If @gstop_done, the ptracer went away between group stop
1794 * completion and here. During detach, it would have set 1868 * completion and here. During detach, it would have set
1795 * GROUP_STOP_PENDING on us and we'll re-enter TASK_STOPPED 1869 * JOBCTL_STOP_PENDING on us and we'll re-enter
1796 * in do_signal_stop() on return, so notifying the real 1870 * TASK_STOPPED in do_signal_stop() on return, so notifying
1797 * parent of the group stop completion is enough. 1871 * the real parent of the group stop completion is enough.
1798 */ 1872 */
1799 if (gstop_done) 1873 if (gstop_done)
1800 do_notify_parent_cldstop(current, false, why); 1874 do_notify_parent_cldstop(current, false, why);
@@ -1820,6 +1894,9 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1820 spin_lock_irq(&current->sighand->siglock); 1894 spin_lock_irq(&current->sighand->siglock);
1821 current->last_siginfo = NULL; 1895 current->last_siginfo = NULL;
1822 1896
1897 /* LISTENING can be set only during STOP traps, clear it */
1898 current->jobctl &= ~JOBCTL_LISTENING;
1899
1823 /* 1900 /*
1824 * Queued signals ignored us while we were stopped for tracing. 1901 * Queued signals ignored us while we were stopped for tracing.
1825 * So check for any that we should take before resuming user mode. 1902 * So check for any that we should take before resuming user mode.
@@ -1828,44 +1905,66 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1828 recalc_sigpending_tsk(current); 1905 recalc_sigpending_tsk(current);
1829} 1906}
1830 1907
1831void ptrace_notify(int exit_code) 1908static void ptrace_do_notify(int signr, int exit_code, int why)
1832{ 1909{
1833 siginfo_t info; 1910 siginfo_t info;
1834 1911
1835 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1836
1837 memset(&info, 0, sizeof info); 1912 memset(&info, 0, sizeof info);
1838 info.si_signo = SIGTRAP; 1913 info.si_signo = signr;
1839 info.si_code = exit_code; 1914 info.si_code = exit_code;
1840 info.si_pid = task_pid_vnr(current); 1915 info.si_pid = task_pid_vnr(current);
1841 info.si_uid = current_uid(); 1916 info.si_uid = current_uid();
1842 1917
1843 /* Let the debugger run. */ 1918 /* Let the debugger run. */
1919 ptrace_stop(exit_code, why, 1, &info);
1920}
1921
1922void ptrace_notify(int exit_code)
1923{
1924 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1925
1844 spin_lock_irq(&current->sighand->siglock); 1926 spin_lock_irq(&current->sighand->siglock);
1845 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info); 1927 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
1846 spin_unlock_irq(&current->sighand->siglock); 1928 spin_unlock_irq(&current->sighand->siglock);
1847} 1929}
1848 1930
1849/* 1931/**
1850 * This performs the stopping for SIGSTOP and other stop signals. 1932 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
1851 * We have to stop all threads in the thread group. 1933 * @signr: signr causing group stop if initiating
1852 * Returns non-zero if we've actually stopped and released the siglock. 1934 *
1853 * Returns zero if we didn't stop and still hold the siglock. 1935 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
1936 * and participate in it. If already set, participate in the existing
1937 * group stop. If participated in a group stop (and thus slept), %true is
1938 * returned with siglock released.
1939 *
1940 * If ptraced, this function doesn't handle stop itself. Instead,
1941 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
1942 * untouched. The caller must ensure that INTERRUPT trap handling takes
1943 * places afterwards.
1944 *
1945 * CONTEXT:
1946 * Must be called with @current->sighand->siglock held, which is released
1947 * on %true return.
1948 *
1949 * RETURNS:
1950 * %false if group stop is already cancelled or ptrace trap is scheduled.
1951 * %true if participated in group stop.
1854 */ 1952 */
1855static int do_signal_stop(int signr) 1953static bool do_signal_stop(int signr)
1954 __releases(&current->sighand->siglock)
1856{ 1955{
1857 struct signal_struct *sig = current->signal; 1956 struct signal_struct *sig = current->signal;
1858 1957
1859 if (!(current->group_stop & GROUP_STOP_PENDING)) { 1958 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1860 unsigned int gstop = GROUP_STOP_PENDING | GROUP_STOP_CONSUME; 1959 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
1861 struct task_struct *t; 1960 struct task_struct *t;
1862 1961
1863 /* signr will be recorded in task->group_stop for retries */ 1962 /* signr will be recorded in task->jobctl for retries */
1864 WARN_ON_ONCE(signr & ~GROUP_STOP_SIGMASK); 1963 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
1865 1964
1866 if (!likely(current->group_stop & GROUP_STOP_DEQUEUED) || 1965 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
1867 unlikely(signal_group_exit(sig))) 1966 unlikely(signal_group_exit(sig)))
1868 return 0; 1967 return false;
1869 /* 1968 /*
1870 * There is no group stop already in progress. We must 1969 * There is no group stop already in progress. We must
1871 * initiate one now. 1970 * initiate one now.
@@ -1888,28 +1987,32 @@ static int do_signal_stop(int signr)
1888 if (!(sig->flags & SIGNAL_STOP_STOPPED)) 1987 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1889 sig->group_exit_code = signr; 1988 sig->group_exit_code = signr;
1890 else 1989 else
1891 WARN_ON_ONCE(!task_ptrace(current)); 1990 WARN_ON_ONCE(!current->ptrace);
1991
1992 sig->group_stop_count = 0;
1993
1994 if (task_set_jobctl_pending(current, signr | gstop))
1995 sig->group_stop_count++;
1892 1996
1893 current->group_stop &= ~GROUP_STOP_SIGMASK;
1894 current->group_stop |= signr | gstop;
1895 sig->group_stop_count = 1;
1896 for (t = next_thread(current); t != current; 1997 for (t = next_thread(current); t != current;
1897 t = next_thread(t)) { 1998 t = next_thread(t)) {
1898 t->group_stop &= ~GROUP_STOP_SIGMASK;
1899 /* 1999 /*
1900 * Setting state to TASK_STOPPED for a group 2000 * Setting state to TASK_STOPPED for a group
1901 * stop is always done with the siglock held, 2001 * stop is always done with the siglock held,
1902 * so this check has no races. 2002 * so this check has no races.
1903 */ 2003 */
1904 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) { 2004 if (!task_is_stopped(t) &&
1905 t->group_stop |= signr | gstop; 2005 task_set_jobctl_pending(t, signr | gstop)) {
1906 sig->group_stop_count++; 2006 sig->group_stop_count++;
1907 signal_wake_up(t, 0); 2007 if (likely(!(t->ptrace & PT_SEIZED)))
2008 signal_wake_up(t, 0);
2009 else
2010 ptrace_trap_notify(t);
1908 } 2011 }
1909 } 2012 }
1910 } 2013 }
1911retry: 2014
1912 if (likely(!task_ptrace(current))) { 2015 if (likely(!current->ptrace)) {
1913 int notify = 0; 2016 int notify = 0;
1914 2017
1915 /* 2018 /*
@@ -1940,43 +2043,65 @@ retry:
1940 2043
1941 /* Now we don't run again until woken by SIGCONT or SIGKILL */ 2044 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1942 schedule(); 2045 schedule();
1943 2046 return true;
1944 spin_lock_irq(&current->sighand->siglock);
1945 } else { 2047 } else {
1946 ptrace_stop(current->group_stop & GROUP_STOP_SIGMASK, 2048 /*
1947 CLD_STOPPED, 0, NULL); 2049 * While ptraced, group stop is handled by STOP trap.
1948 current->exit_code = 0; 2050 * Schedule it and let the caller deal with it.
2051 */
2052 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2053 return false;
1949 } 2054 }
2055}
1950 2056
1951 /* 2057/**
1952 * GROUP_STOP_PENDING could be set if another group stop has 2058 * do_jobctl_trap - take care of ptrace jobctl traps
1953 * started since being woken up or ptrace wants us to transit 2059 *
1954 * between TASK_STOPPED and TRACED. Retry group stop. 2060 * When PT_SEIZED, it's used for both group stop and explicit
1955 */ 2061 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
1956 if (current->group_stop & GROUP_STOP_PENDING) { 2062 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
1957 WARN_ON_ONCE(!(current->group_stop & GROUP_STOP_SIGMASK)); 2063 * the stop signal; otherwise, %SIGTRAP.
1958 goto retry; 2064 *
2065 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2066 * number as exit_code and no siginfo.
2067 *
2068 * CONTEXT:
2069 * Must be called with @current->sighand->siglock held, which may be
2070 * released and re-acquired before returning with intervening sleep.
2071 */
2072static void do_jobctl_trap(void)
2073{
2074 struct signal_struct *signal = current->signal;
2075 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2076
2077 if (current->ptrace & PT_SEIZED) {
2078 if (!signal->group_stop_count &&
2079 !(signal->flags & SIGNAL_STOP_STOPPED))
2080 signr = SIGTRAP;
2081 WARN_ON_ONCE(!signr);
2082 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2083 CLD_STOPPED);
2084 } else {
2085 WARN_ON_ONCE(!signr);
2086 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2087 current->exit_code = 0;
1959 } 2088 }
1960
1961 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
1962 task_clear_group_stop_trapping(current);
1963
1964 spin_unlock_irq(&current->sighand->siglock);
1965
1966 tracehook_finish_jctl();
1967
1968 return 1;
1969} 2089}
1970 2090
1971static int ptrace_signal(int signr, siginfo_t *info, 2091static int ptrace_signal(int signr, siginfo_t *info,
1972 struct pt_regs *regs, void *cookie) 2092 struct pt_regs *regs, void *cookie)
1973{ 2093{
1974 if (!task_ptrace(current))
1975 return signr;
1976
1977 ptrace_signal_deliver(regs, cookie); 2094 ptrace_signal_deliver(regs, cookie);
1978 2095 /*
1979 /* Let the debugger run. */ 2096 * We do not check sig_kernel_stop(signr) but set this marker
2097 * unconditionally because we do not know whether debugger will
2098 * change signr. This flag has no meaning unless we are going
2099 * to stop after return from ptrace_stop(). In this case it will
2100 * be checked in do_signal_stop(), we should only stop if it was
2101 * not cleared by SIGCONT while we were sleeping. See also the
2102 * comment in dequeue_signal().
2103 */
2104 current->jobctl |= JOBCTL_STOP_DEQUEUED;
1980 ptrace_stop(signr, CLD_TRAPPED, 0, info); 2105 ptrace_stop(signr, CLD_TRAPPED, 0, info);
1981 2106
1982 /* We're back. Did the debugger cancel the sig? */ 2107 /* We're back. Did the debugger cancel the sig? */
@@ -2032,7 +2157,6 @@ relock:
2032 * the CLD_ si_code into SIGNAL_CLD_MASK bits. 2157 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2033 */ 2158 */
2034 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { 2159 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2035 struct task_struct *leader;
2036 int why; 2160 int why;
2037 2161
2038 if (signal->flags & SIGNAL_CLD_CONTINUED) 2162 if (signal->flags & SIGNAL_CLD_CONTINUED)
@@ -2053,13 +2177,11 @@ relock:
2053 * a duplicate. 2177 * a duplicate.
2054 */ 2178 */
2055 read_lock(&tasklist_lock); 2179 read_lock(&tasklist_lock);
2056
2057 do_notify_parent_cldstop(current, false, why); 2180 do_notify_parent_cldstop(current, false, why);
2058 2181
2059 leader = current->group_leader; 2182 if (ptrace_reparented(current->group_leader))
2060 if (task_ptrace(leader) && !real_parent_is_ptracer(leader)) 2183 do_notify_parent_cldstop(current->group_leader,
2061 do_notify_parent_cldstop(leader, true, why); 2184 true, why);
2062
2063 read_unlock(&tasklist_lock); 2185 read_unlock(&tasklist_lock);
2064 2186
2065 goto relock; 2187 goto relock;
@@ -2067,37 +2189,31 @@ relock:
2067 2189
2068 for (;;) { 2190 for (;;) {
2069 struct k_sigaction *ka; 2191 struct k_sigaction *ka;
2070 /* 2192
2071 * Tracing can induce an artificial signal and choose sigaction. 2193 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2072 * The return value in @signr determines the default action, 2194 do_signal_stop(0))
2073 * but @info->si_signo is the signal number we will report.
2074 */
2075 signr = tracehook_get_signal(current, regs, info, return_ka);
2076 if (unlikely(signr < 0))
2077 goto relock; 2195 goto relock;
2078 if (unlikely(signr != 0))
2079 ka = return_ka;
2080 else {
2081 if (unlikely(current->group_stop &
2082 GROUP_STOP_PENDING) && do_signal_stop(0))
2083 goto relock;
2084 2196
2085 signr = dequeue_signal(current, &current->blocked, 2197 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2086 info); 2198 do_jobctl_trap();
2199 spin_unlock_irq(&sighand->siglock);
2200 goto relock;
2201 }
2087 2202
2088 if (!signr) 2203 signr = dequeue_signal(current, &current->blocked, info);
2089 break; /* will return 0 */
2090 2204
2091 if (signr != SIGKILL) { 2205 if (!signr)
2092 signr = ptrace_signal(signr, info, 2206 break; /* will return 0 */
2093 regs, cookie);
2094 if (!signr)
2095 continue;
2096 }
2097 2207
2098 ka = &sighand->action[signr-1]; 2208 if (unlikely(current->ptrace) && signr != SIGKILL) {
2209 signr = ptrace_signal(signr, info,
2210 regs, cookie);
2211 if (!signr)
2212 continue;
2099 } 2213 }
2100 2214
2215 ka = &sighand->action[signr-1];
2216
2101 /* Trace actually delivered signals. */ 2217 /* Trace actually delivered signals. */
2102 trace_signal_deliver(signr, info, ka); 2218 trace_signal_deliver(signr, info, ka);
2103 2219
@@ -2253,7 +2369,7 @@ void exit_signals(struct task_struct *tsk)
2253 signotset(&unblocked); 2369 signotset(&unblocked);
2254 retarget_shared_pending(tsk, &unblocked); 2370 retarget_shared_pending(tsk, &unblocked);
2255 2371
2256 if (unlikely(tsk->group_stop & GROUP_STOP_PENDING) && 2372 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2257 task_participate_group_stop(tsk)) 2373 task_participate_group_stop(tsk))
2258 group_stop = CLD_STOPPED; 2374 group_stop = CLD_STOPPED;
2259out: 2375out:
@@ -2365,7 +2481,7 @@ int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2365/** 2481/**
2366 * sys_rt_sigprocmask - change the list of currently blocked signals 2482 * sys_rt_sigprocmask - change the list of currently blocked signals
2367 * @how: whether to add, remove, or set signals 2483 * @how: whether to add, remove, or set signals
2368 * @set: stores pending signals 2484 * @nset: stores pending signals
2369 * @oset: previous value of signal mask if non-null 2485 * @oset: previous value of signal mask if non-null
2370 * @sigsetsize: size of sigset_t type 2486 * @sigsetsize: size of sigset_t type
2371 */ 2487 */
@@ -2986,15 +3102,11 @@ SYSCALL_DEFINE0(sgetmask)
2986 3102
2987SYSCALL_DEFINE1(ssetmask, int, newmask) 3103SYSCALL_DEFINE1(ssetmask, int, newmask)
2988{ 3104{
2989 int old; 3105 int old = current->blocked.sig[0];
2990 3106 sigset_t newset;
2991 spin_lock_irq(&current->sighand->siglock);
2992 old = current->blocked.sig[0];
2993 3107
2994 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)| 3108 siginitset(&newset, newmask & ~(sigmask(SIGKILL) | sigmask(SIGSTOP)));
2995 sigmask(SIGSTOP))); 3109 set_current_blocked(&newset);
2996 recalc_sigpending();
2997 spin_unlock_irq(&current->sighand->siglock);
2998 3110
2999 return old; 3111 return old;
3000} 3112}
@@ -3051,11 +3163,8 @@ SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3051 return -EFAULT; 3163 return -EFAULT;
3052 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP)); 3164 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3053 3165
3054 spin_lock_irq(&current->sighand->siglock);
3055 current->saved_sigmask = current->blocked; 3166 current->saved_sigmask = current->blocked;
3056 current->blocked = newset; 3167 set_current_blocked(&newset);
3057 recalc_sigpending();
3058 spin_unlock_irq(&current->sighand->siglock);
3059 3168
3060 current->state = TASK_INTERRUPTIBLE; 3169 current->state = TASK_INTERRUPTIBLE;
3061 schedule(); 3170 schedule();