diff options
Diffstat (limited to 'kernel/signal.c')
-rw-r--r-- | kernel/signal.c | 442 |
1 files changed, 272 insertions, 170 deletions
diff --git a/kernel/signal.c b/kernel/signal.c index 415d85d6f6c6..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 | ||
125 | static int recalc_sigpending_tsk(struct task_struct *t) | 125 | static 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 | ||
151 | void recalc_sigpending(void) | 151 | void 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. | ||
234 | * | ||
235 | * CONTEXT: | ||
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. | ||
240 | */ | ||
241 | bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask) | ||
242 | { | ||
243 | BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME | | ||
244 | JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING)); | ||
245 | BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK)); | ||
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. | ||
233 | * | 265 | * |
234 | * CONTEXT: | 266 | * CONTEXT: |
235 | * Must be called with @task->sighand->siglock held. | 267 | * Must be called with @task->sighand->siglock held. |
236 | */ | 268 | */ |
237 | static void task_clear_group_stop_trapping(struct task_struct *task) | 269 | void task_clear_jobctl_trapping(struct task_struct *task) |
238 | { | 270 | { |
239 | if (unlikely(task->group_stop & GROUP_STOP_TRAPPING)) { | 271 | if (unlikely(task->jobctl & JOBCTL_TRAPPING)) { |
240 | task->group_stop &= ~GROUP_STOP_TRAPPING; | 272 | task->jobctl &= ~JOBCTL_TRAPPING; |
241 | __wake_up_sync_key(&task->parent->signal->wait_chldexit, | 273 | wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT); |
242 | TASK_UNINTERRUPTIBLE, 1, task); | ||
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 | ||
249 | * | 281 | * |
250 | * Clear group stop states for @task. | 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. | ||
285 | * | ||
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 | */ |
255 | void task_clear_group_stop_pending(struct task_struct *task) | 292 | void 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) | |||
277 | static bool task_participate_group_stop(struct task_struct *task) | 321 | static 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 | */ | ||
838 | static 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 */ |
@@ -1511,22 +1584,22 @@ ret: | |||
1511 | * Let a parent know about the death of a child. | 1584 | * Let a parent know about the death of a child. |
1512 | * 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. |
1513 | * | 1586 | * |
1514 | * 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 |
1515 | * self-reaping, or else @sig. | 1588 | * self-reaping. |
1516 | */ | 1589 | */ |
1517 | int do_notify_parent(struct task_struct *tsk, int sig) | 1590 | bool do_notify_parent(struct task_struct *tsk, int sig) |
1518 | { | 1591 | { |
1519 | struct siginfo info; | 1592 | struct siginfo info; |
1520 | unsigned long flags; | 1593 | unsigned long flags; |
1521 | struct sighand_struct *psig; | 1594 | struct sighand_struct *psig; |
1522 | int ret = sig; | 1595 | bool autoreap = false; |
1523 | 1596 | ||
1524 | BUG_ON(sig == -1); | 1597 | BUG_ON(sig == -1); |
1525 | 1598 | ||
1526 | /* do_notify_parent_cldstop should have been called instead. */ | 1599 | /* do_notify_parent_cldstop should have been called instead. */ |
1527 | BUG_ON(task_is_stopped_or_traced(tsk)); | 1600 | BUG_ON(task_is_stopped_or_traced(tsk)); |
1528 | 1601 | ||
1529 | BUG_ON(!task_ptrace(tsk) && | 1602 | BUG_ON(!tsk->ptrace && |
1530 | (tsk->group_leader != tsk || !thread_group_empty(tsk))); | 1603 | (tsk->group_leader != tsk || !thread_group_empty(tsk))); |
1531 | 1604 | ||
1532 | info.si_signo = sig; | 1605 | info.si_signo = sig; |
@@ -1565,7 +1638,7 @@ int do_notify_parent(struct task_struct *tsk, int sig) | |||
1565 | 1638 | ||
1566 | psig = tsk->parent->sighand; | 1639 | psig = tsk->parent->sighand; |
1567 | spin_lock_irqsave(&psig->siglock, flags); | 1640 | spin_lock_irqsave(&psig->siglock, flags); |
1568 | if (!task_ptrace(tsk) && sig == SIGCHLD && | 1641 | if (!tsk->ptrace && sig == SIGCHLD && |
1569 | (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN || | 1642 | (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN || |
1570 | (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) { | 1643 | (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) { |
1571 | /* | 1644 | /* |
@@ -1583,16 +1656,16 @@ int do_notify_parent(struct task_struct *tsk, int sig) | |||
1583 | * is implementation-defined: we do (if you don't want | 1656 | * is implementation-defined: we do (if you don't want |
1584 | * it, just use SIG_IGN instead). | 1657 | * it, just use SIG_IGN instead). |
1585 | */ | 1658 | */ |
1586 | ret = tsk->exit_signal = -1; | 1659 | autoreap = true; |
1587 | if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) | 1660 | if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) |
1588 | sig = -1; | 1661 | sig = 0; |
1589 | } | 1662 | } |
1590 | if (valid_signal(sig) && sig > 0) | 1663 | if (valid_signal(sig) && sig) |
1591 | __group_send_sig_info(sig, &info, tsk->parent); | 1664 | __group_send_sig_info(sig, &info, tsk->parent); |
1592 | __wake_up_parent(tsk, tsk->parent); | 1665 | __wake_up_parent(tsk, tsk->parent); |
1593 | spin_unlock_irqrestore(&psig->siglock, flags); | 1666 | spin_unlock_irqrestore(&psig->siglock, flags); |
1594 | 1667 | ||
1595 | return ret; | 1668 | return autoreap; |
1596 | } | 1669 | } |
1597 | 1670 | ||
1598 | /** | 1671 | /** |
@@ -1665,7 +1738,7 @@ static void do_notify_parent_cldstop(struct task_struct *tsk, | |||
1665 | 1738 | ||
1666 | static inline int may_ptrace_stop(void) | 1739 | static inline int may_ptrace_stop(void) |
1667 | { | 1740 | { |
1668 | if (!likely(task_ptrace(current))) | 1741 | if (!likely(current->ptrace)) |
1669 | return 0; | 1742 | return 0; |
1670 | /* | 1743 | /* |
1671 | * Are we in the middle of do_coredump? | 1744 | * Are we in the middle of do_coredump? |
@@ -1694,15 +1767,6 @@ static int sigkill_pending(struct task_struct *tsk) | |||
1694 | } | 1767 | } |
1695 | 1768 | ||
1696 | /* | 1769 | /* |
1697 | * Test whether the target task of the usual cldstop notification - the | ||
1698 | * real_parent of @child - is in the same group as the ptracer. | ||
1699 | */ | ||
1700 | static bool real_parent_is_ptracer(struct task_struct *child) | ||
1701 | { | ||
1702 | return same_thread_group(child->parent, child->real_parent); | ||
1703 | } | ||
1704 | |||
1705 | /* | ||
1706 | * This must be called with current->sighand->siglock held. | 1770 | * This must be called with current->sighand->siglock held. |
1707 | * | 1771 | * |
1708 | * This should be the path for all ptrace stops. | 1772 | * This should be the path for all ptrace stops. |
@@ -1739,31 +1803,34 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) | |||
1739 | } | 1803 | } |
1740 | 1804 | ||
1741 | /* | 1805 | /* |
1742 | * If @why is CLD_STOPPED, we're trapping to participate in a group | 1806 | * We're committing to trapping. TRACED should be visible before |
1743 | * stop. Do the bookkeeping. Note that if SIGCONT was delievered | 1807 | * TRAPPING is cleared; otherwise, the tracer might fail do_wait(). |
1744 | * while siglock was released for the arch hook, PENDING could be | 1808 | * Also, transition to TRACED and updates to ->jobctl should be |
1745 | * 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 |
1746 | * is entered - ignore it. | 1810 | * hook as siglock is released and regrabbed across it. |
1747 | */ | 1811 | */ |
1748 | if (why == CLD_STOPPED && (current->group_stop & GROUP_STOP_PENDING)) | 1812 | set_current_state(TASK_TRACED); |
1749 | gstop_done = task_participate_group_stop(current); | ||
1750 | 1813 | ||
1751 | current->last_siginfo = info; | 1814 | current->last_siginfo = info; |
1752 | current->exit_code = exit_code; | 1815 | current->exit_code = exit_code; |
1753 | 1816 | ||
1754 | /* | 1817 | /* |
1755 | * TRACED should be visible before TRAPPING is cleared; otherwise, | 1818 | * If @why is CLD_STOPPED, we're trapping to participate in a group |
1756 | * 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. | ||
1757 | */ | 1823 | */ |
1758 | set_current_state(TASK_TRACED); | 1824 | if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING)) |
1825 | gstop_done = task_participate_group_stop(current); | ||
1759 | 1826 | ||
1760 | /* | 1827 | /* any trap clears pending STOP trap, STOP trap clears NOTIFY */ |
1761 | * We're committing to trapping. Clearing GROUP_STOP_TRAPPING and | 1828 | task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP); |
1762 | * transition to TASK_TRACED should be atomic with respect to | 1829 | if (info && info->si_code >> 8 == PTRACE_EVENT_STOP) |
1763 | * siglock. This hsould be done after the arch hook as siglock is | 1830 | task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY); |
1764 | * released and regrabbed across it. | 1831 | |
1765 | */ | 1832 | /* entering a trap, clear TRAPPING */ |
1766 | task_clear_group_stop_trapping(current); | 1833 | task_clear_jobctl_trapping(current); |
1767 | 1834 | ||
1768 | spin_unlock_irq(¤t->sighand->siglock); | 1835 | spin_unlock_irq(¤t->sighand->siglock); |
1769 | read_lock(&tasklist_lock); | 1836 | read_lock(&tasklist_lock); |
@@ -1779,7 +1846,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) | |||
1779 | * separately unless they're gonna be duplicates. | 1846 | * separately unless they're gonna be duplicates. |
1780 | */ | 1847 | */ |
1781 | do_notify_parent_cldstop(current, true, why); | 1848 | do_notify_parent_cldstop(current, true, why); |
1782 | if (gstop_done && !real_parent_is_ptracer(current)) | 1849 | if (gstop_done && ptrace_reparented(current)) |
1783 | do_notify_parent_cldstop(current, false, why); | 1850 | do_notify_parent_cldstop(current, false, why); |
1784 | 1851 | ||
1785 | /* | 1852 | /* |
@@ -1799,9 +1866,9 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) | |||
1799 | * | 1866 | * |
1800 | * If @gstop_done, the ptracer went away between group stop | 1867 | * If @gstop_done, the ptracer went away between group stop |
1801 | * completion and here. During detach, it would have set | 1868 | * completion and here. During detach, it would have set |
1802 | * GROUP_STOP_PENDING on us and we'll re-enter TASK_STOPPED | 1869 | * JOBCTL_STOP_PENDING on us and we'll re-enter |
1803 | * in do_signal_stop() on return, so notifying the real | 1870 | * TASK_STOPPED in do_signal_stop() on return, so notifying |
1804 | * parent of the group stop completion is enough. | 1871 | * the real parent of the group stop completion is enough. |
1805 | */ | 1872 | */ |
1806 | if (gstop_done) | 1873 | if (gstop_done) |
1807 | do_notify_parent_cldstop(current, false, why); | 1874 | do_notify_parent_cldstop(current, false, why); |
@@ -1827,6 +1894,9 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) | |||
1827 | spin_lock_irq(¤t->sighand->siglock); | 1894 | spin_lock_irq(¤t->sighand->siglock); |
1828 | current->last_siginfo = NULL; | 1895 | current->last_siginfo = NULL; |
1829 | 1896 | ||
1897 | /* LISTENING can be set only during STOP traps, clear it */ | ||
1898 | current->jobctl &= ~JOBCTL_LISTENING; | ||
1899 | |||
1830 | /* | 1900 | /* |
1831 | * Queued signals ignored us while we were stopped for tracing. | 1901 | * Queued signals ignored us while we were stopped for tracing. |
1832 | * 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. |
@@ -1835,44 +1905,66 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) | |||
1835 | recalc_sigpending_tsk(current); | 1905 | recalc_sigpending_tsk(current); |
1836 | } | 1906 | } |
1837 | 1907 | ||
1838 | void ptrace_notify(int exit_code) | 1908 | static void ptrace_do_notify(int signr, int exit_code, int why) |
1839 | { | 1909 | { |
1840 | siginfo_t info; | 1910 | siginfo_t info; |
1841 | 1911 | ||
1842 | BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP); | ||
1843 | |||
1844 | memset(&info, 0, sizeof info); | 1912 | memset(&info, 0, sizeof info); |
1845 | info.si_signo = SIGTRAP; | 1913 | info.si_signo = signr; |
1846 | info.si_code = exit_code; | 1914 | info.si_code = exit_code; |
1847 | info.si_pid = task_pid_vnr(current); | 1915 | info.si_pid = task_pid_vnr(current); |
1848 | info.si_uid = current_uid(); | 1916 | info.si_uid = current_uid(); |
1849 | 1917 | ||
1850 | /* Let the debugger run. */ | 1918 | /* Let the debugger run. */ |
1919 | ptrace_stop(exit_code, why, 1, &info); | ||
1920 | } | ||
1921 | |||
1922 | void ptrace_notify(int exit_code) | ||
1923 | { | ||
1924 | BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP); | ||
1925 | |||
1851 | spin_lock_irq(¤t->sighand->siglock); | 1926 | spin_lock_irq(¤t->sighand->siglock); |
1852 | ptrace_stop(exit_code, CLD_TRAPPED, 1, &info); | 1927 | ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED); |
1853 | spin_unlock_irq(¤t->sighand->siglock); | 1928 | spin_unlock_irq(¤t->sighand->siglock); |
1854 | } | 1929 | } |
1855 | 1930 | ||
1856 | /* | 1931 | /** |
1857 | * This performs the stopping for SIGSTOP and other stop signals. | 1932 | * do_signal_stop - handle group stop for SIGSTOP and other stop signals |
1858 | * We have to stop all threads in the thread group. | 1933 | * @signr: signr causing group stop if initiating |
1859 | * Returns non-zero if we've actually stopped and released the siglock. | 1934 | * |
1860 | * 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. | ||
1861 | */ | 1952 | */ |
1862 | static int do_signal_stop(int signr) | 1953 | static bool do_signal_stop(int signr) |
1954 | __releases(¤t->sighand->siglock) | ||
1863 | { | 1955 | { |
1864 | struct signal_struct *sig = current->signal; | 1956 | struct signal_struct *sig = current->signal; |
1865 | 1957 | ||
1866 | if (!(current->group_stop & GROUP_STOP_PENDING)) { | 1958 | if (!(current->jobctl & JOBCTL_STOP_PENDING)) { |
1867 | unsigned int gstop = GROUP_STOP_PENDING | GROUP_STOP_CONSUME; | 1959 | unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME; |
1868 | struct task_struct *t; | 1960 | struct task_struct *t; |
1869 | 1961 | ||
1870 | /* signr will be recorded in task->group_stop for retries */ | 1962 | /* signr will be recorded in task->jobctl for retries */ |
1871 | WARN_ON_ONCE(signr & ~GROUP_STOP_SIGMASK); | 1963 | WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK); |
1872 | 1964 | ||
1873 | if (!likely(current->group_stop & GROUP_STOP_DEQUEUED) || | 1965 | if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) || |
1874 | unlikely(signal_group_exit(sig))) | 1966 | unlikely(signal_group_exit(sig))) |
1875 | return 0; | 1967 | return false; |
1876 | /* | 1968 | /* |
1877 | * There is no group stop already in progress. We must | 1969 | * There is no group stop already in progress. We must |
1878 | * initiate one now. | 1970 | * initiate one now. |
@@ -1895,28 +1987,32 @@ static int do_signal_stop(int signr) | |||
1895 | if (!(sig->flags & SIGNAL_STOP_STOPPED)) | 1987 | if (!(sig->flags & SIGNAL_STOP_STOPPED)) |
1896 | sig->group_exit_code = signr; | 1988 | sig->group_exit_code = signr; |
1897 | else | 1989 | else |
1898 | 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++; | ||
1899 | 1996 | ||
1900 | current->group_stop &= ~GROUP_STOP_SIGMASK; | ||
1901 | current->group_stop |= signr | gstop; | ||
1902 | sig->group_stop_count = 1; | ||
1903 | for (t = next_thread(current); t != current; | 1997 | for (t = next_thread(current); t != current; |
1904 | t = next_thread(t)) { | 1998 | t = next_thread(t)) { |
1905 | t->group_stop &= ~GROUP_STOP_SIGMASK; | ||
1906 | /* | 1999 | /* |
1907 | * Setting state to TASK_STOPPED for a group | 2000 | * Setting state to TASK_STOPPED for a group |
1908 | * stop is always done with the siglock held, | 2001 | * stop is always done with the siglock held, |
1909 | * so this check has no races. | 2002 | * so this check has no races. |
1910 | */ | 2003 | */ |
1911 | if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) { | 2004 | if (!task_is_stopped(t) && |
1912 | t->group_stop |= signr | gstop; | 2005 | task_set_jobctl_pending(t, signr | gstop)) { |
1913 | sig->group_stop_count++; | 2006 | sig->group_stop_count++; |
1914 | 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); | ||
1915 | } | 2011 | } |
1916 | } | 2012 | } |
1917 | } | 2013 | } |
1918 | retry: | 2014 | |
1919 | if (likely(!task_ptrace(current))) { | 2015 | if (likely(!current->ptrace)) { |
1920 | int notify = 0; | 2016 | int notify = 0; |
1921 | 2017 | ||
1922 | /* | 2018 | /* |
@@ -1947,43 +2043,65 @@ retry: | |||
1947 | 2043 | ||
1948 | /* 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 */ |
1949 | schedule(); | 2045 | schedule(); |
1950 | 2046 | return true; | |
1951 | spin_lock_irq(¤t->sighand->siglock); | ||
1952 | } else { | 2047 | } else { |
1953 | ptrace_stop(current->group_stop & GROUP_STOP_SIGMASK, | 2048 | /* |
1954 | CLD_STOPPED, 0, NULL); | 2049 | * While ptraced, group stop is handled by STOP trap. |
1955 | 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; | ||
1956 | } | 2054 | } |
2055 | } | ||
1957 | 2056 | ||
1958 | /* | 2057 | /** |
1959 | * GROUP_STOP_PENDING could be set if another group stop has | 2058 | * do_jobctl_trap - take care of ptrace jobctl traps |
1960 | * started since being woken up or ptrace wants us to transit | 2059 | * |
1961 | * between TASK_STOPPED and TRACED. Retry group stop. | 2060 | * When PT_SEIZED, it's used for both group stop and explicit |
1962 | */ | 2061 | * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with |
1963 | if (current->group_stop & GROUP_STOP_PENDING) { | 2062 | * accompanying siginfo. If stopped, lower eight bits of exit_code contain |
1964 | WARN_ON_ONCE(!(current->group_stop & GROUP_STOP_SIGMASK)); | 2063 | * the stop signal; otherwise, %SIGTRAP. |
1965 | 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 | */ | ||
2072 | static 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; | ||
1966 | } | 2088 | } |
1967 | |||
1968 | /* PTRACE_ATTACH might have raced with task killing, clear trapping */ | ||
1969 | task_clear_group_stop_trapping(current); | ||
1970 | |||
1971 | spin_unlock_irq(¤t->sighand->siglock); | ||
1972 | |||
1973 | tracehook_finish_jctl(); | ||
1974 | |||
1975 | return 1; | ||
1976 | } | 2089 | } |
1977 | 2090 | ||
1978 | static int ptrace_signal(int signr, siginfo_t *info, | 2091 | static int ptrace_signal(int signr, siginfo_t *info, |
1979 | struct pt_regs *regs, void *cookie) | 2092 | struct pt_regs *regs, void *cookie) |
1980 | { | 2093 | { |
1981 | if (!task_ptrace(current)) | ||
1982 | return signr; | ||
1983 | |||
1984 | ptrace_signal_deliver(regs, cookie); | 2094 | ptrace_signal_deliver(regs, cookie); |
1985 | 2095 | /* | |
1986 | /* 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; | ||
1987 | ptrace_stop(signr, CLD_TRAPPED, 0, info); | 2105 | ptrace_stop(signr, CLD_TRAPPED, 0, info); |
1988 | 2106 | ||
1989 | /* We're back. Did the debugger cancel the sig? */ | 2107 | /* We're back. Did the debugger cancel the sig? */ |
@@ -2039,7 +2157,6 @@ relock: | |||
2039 | * the CLD_ si_code into SIGNAL_CLD_MASK bits. | 2157 | * the CLD_ si_code into SIGNAL_CLD_MASK bits. |
2040 | */ | 2158 | */ |
2041 | if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { | 2159 | if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { |
2042 | struct task_struct *leader; | ||
2043 | int why; | 2160 | int why; |
2044 | 2161 | ||
2045 | if (signal->flags & SIGNAL_CLD_CONTINUED) | 2162 | if (signal->flags & SIGNAL_CLD_CONTINUED) |
@@ -2060,13 +2177,11 @@ relock: | |||
2060 | * a duplicate. | 2177 | * a duplicate. |
2061 | */ | 2178 | */ |
2062 | read_lock(&tasklist_lock); | 2179 | read_lock(&tasklist_lock); |
2063 | |||
2064 | do_notify_parent_cldstop(current, false, why); | 2180 | do_notify_parent_cldstop(current, false, why); |
2065 | 2181 | ||
2066 | leader = current->group_leader; | 2182 | if (ptrace_reparented(current->group_leader)) |
2067 | if (task_ptrace(leader) && !real_parent_is_ptracer(leader)) | 2183 | do_notify_parent_cldstop(current->group_leader, |
2068 | do_notify_parent_cldstop(leader, true, why); | 2184 | true, why); |
2069 | |||
2070 | read_unlock(&tasklist_lock); | 2185 | read_unlock(&tasklist_lock); |
2071 | 2186 | ||
2072 | goto relock; | 2187 | goto relock; |
@@ -2074,37 +2189,31 @@ relock: | |||
2074 | 2189 | ||
2075 | for (;;) { | 2190 | for (;;) { |
2076 | struct k_sigaction *ka; | 2191 | struct k_sigaction *ka; |
2077 | /* | 2192 | |
2078 | * Tracing can induce an artificial signal and choose sigaction. | 2193 | if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) && |
2079 | * The return value in @signr determines the default action, | 2194 | do_signal_stop(0)) |
2080 | * but @info->si_signo is the signal number we will report. | ||
2081 | */ | ||
2082 | signr = tracehook_get_signal(current, regs, info, return_ka); | ||
2083 | if (unlikely(signr < 0)) | ||
2084 | goto relock; | 2195 | goto relock; |
2085 | if (unlikely(signr != 0)) | ||
2086 | ka = return_ka; | ||
2087 | else { | ||
2088 | if (unlikely(current->group_stop & | ||
2089 | GROUP_STOP_PENDING) && do_signal_stop(0)) | ||
2090 | goto relock; | ||
2091 | 2196 | ||
2092 | signr = dequeue_signal(current, ¤t->blocked, | 2197 | if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) { |
2093 | info); | 2198 | do_jobctl_trap(); |
2199 | spin_unlock_irq(&sighand->siglock); | ||
2200 | goto relock; | ||
2201 | } | ||
2094 | 2202 | ||
2095 | if (!signr) | 2203 | signr = dequeue_signal(current, ¤t->blocked, info); |
2096 | break; /* will return 0 */ | ||
2097 | 2204 | ||
2098 | if (signr != SIGKILL) { | 2205 | if (!signr) |
2099 | signr = ptrace_signal(signr, info, | 2206 | break; /* will return 0 */ |
2100 | regs, cookie); | ||
2101 | if (!signr) | ||
2102 | continue; | ||
2103 | } | ||
2104 | 2207 | ||
2105 | 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; | ||
2106 | } | 2213 | } |
2107 | 2214 | ||
2215 | ka = &sighand->action[signr-1]; | ||
2216 | |||
2108 | /* Trace actually delivered signals. */ | 2217 | /* Trace actually delivered signals. */ |
2109 | trace_signal_deliver(signr, info, ka); | 2218 | trace_signal_deliver(signr, info, ka); |
2110 | 2219 | ||
@@ -2260,7 +2369,7 @@ void exit_signals(struct task_struct *tsk) | |||
2260 | signotset(&unblocked); | 2369 | signotset(&unblocked); |
2261 | retarget_shared_pending(tsk, &unblocked); | 2370 | retarget_shared_pending(tsk, &unblocked); |
2262 | 2371 | ||
2263 | if (unlikely(tsk->group_stop & GROUP_STOP_PENDING) && | 2372 | if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) && |
2264 | task_participate_group_stop(tsk)) | 2373 | task_participate_group_stop(tsk)) |
2265 | group_stop = CLD_STOPPED; | 2374 | group_stop = CLD_STOPPED; |
2266 | out: | 2375 | out: |
@@ -2993,15 +3102,11 @@ SYSCALL_DEFINE0(sgetmask) | |||
2993 | 3102 | ||
2994 | SYSCALL_DEFINE1(ssetmask, int, newmask) | 3103 | SYSCALL_DEFINE1(ssetmask, int, newmask) |
2995 | { | 3104 | { |
2996 | int old; | 3105 | int old = current->blocked.sig[0]; |
2997 | 3106 | sigset_t newset; | |
2998 | spin_lock_irq(¤t->sighand->siglock); | ||
2999 | old = current->blocked.sig[0]; | ||
3000 | 3107 | ||
3001 | siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)| | 3108 | siginitset(&newset, newmask & ~(sigmask(SIGKILL) | sigmask(SIGSTOP))); |
3002 | sigmask(SIGSTOP))); | 3109 | set_current_blocked(&newset); |
3003 | recalc_sigpending(); | ||
3004 | spin_unlock_irq(¤t->sighand->siglock); | ||
3005 | 3110 | ||
3006 | return old; | 3111 | return old; |
3007 | } | 3112 | } |
@@ -3058,11 +3163,8 @@ SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize) | |||
3058 | return -EFAULT; | 3163 | return -EFAULT; |
3059 | sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | 3164 | sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
3060 | 3165 | ||
3061 | spin_lock_irq(¤t->sighand->siglock); | ||
3062 | current->saved_sigmask = current->blocked; | 3166 | current->saved_sigmask = current->blocked; |
3063 | current->blocked = newset; | 3167 | set_current_blocked(&newset); |
3064 | recalc_sigpending(); | ||
3065 | spin_unlock_irq(¤t->sighand->siglock); | ||
3066 | 3168 | ||
3067 | current->state = TASK_INTERRUPTIBLE; | 3169 | current->state = TASK_INTERRUPTIBLE; |
3068 | schedule(); | 3170 | schedule(); |