Skip to content

Commit ae6d2ed

Browse files
Roland McGrathtorvalds
authored andcommitted
signals: tracehook_notify_jctl change
This changes tracehook_notify_jctl() so it's called with the siglock held, and changes its argument and return value definition. These clean-ups make it a better fit for what new tracing hooks need to check. Tracing needs the siglock here, held from the time TASK_STOPPED was set, to avoid potential SIGCONT races if it wants to allow any blocking in its tracing hooks. This also folds the finish_stop() function into its caller do_signal_stop(). The function is short, called only once and only unconditionally. It aids readability to fold it in. [oleg@redhat.com: do not call tracehook_notify_jctl() in TASK_STOPPED state] [oleg@redhat.com: introduce tracehook_finish_jctl() helper] Signed-off-by: Roland McGrath <roland@redhat.com> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b6fe2d1 commit ae6d2ed

2 files changed

Lines changed: 72 additions & 59 deletions

File tree

‎include/linux/tracehook.h‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tracing hooks
33
*
4-
* Copyright (C) 2008 Red Hat, Inc. All rights reserved.
4+
* Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
55
*
66
* This copyrighted material is made available to anyone wishing to use,
77
* modify, copy, or redistribute it subject to the terms and conditions
@@ -463,22 +463,38 @@ static inline int tracehook_get_signal(struct task_struct *task,
463463

464464
/**
465465
* tracehook_notify_jctl - report about job control stop/continue
466-
* @notify: nonzero if this is the last thread in the group to stop
466+
* @notify: zero, %CLD_STOPPED or %CLD_CONTINUED
467467
* @why: %CLD_STOPPED or %CLD_CONTINUED
468468
*
469469
* This is called when we might call do_notify_parent_cldstop().
470-
* It's called when about to stop for job control; we are already in
471-
* %TASK_STOPPED state, about to call schedule(). It's also called when
472-
* a delayed %CLD_STOPPED or %CLD_CONTINUED report is ready to be made.
473470
*
474-
* Return nonzero to generate a %SIGCHLD with @why, which is
475-
* normal if @notify is nonzero.
471+
* @notify is zero if we would not ordinarily send a %SIGCHLD,
472+
* or is the %CLD_STOPPED or %CLD_CONTINUED .si_code for %SIGCHLD.
476473
*
477-
* Called with no locks held.
474+
* @why is %CLD_STOPPED when about to stop for job control;
475+
* we are already in %TASK_STOPPED state, about to call schedule().
476+
* It might also be that we have just exited (check %PF_EXITING),
477+
* but need to report that a group-wide stop is complete.
478+
*
479+
* @why is %CLD_CONTINUED when waking up after job control stop and
480+
* ready to make a delayed @notify report.
481+
*
482+
* Return the %CLD_* value for %SIGCHLD, or zero to generate no signal.
483+
*
484+
* Called with the siglock held.
478485
*/
479486
static inline int tracehook_notify_jctl(int notify, int why)
480487
{
481-
return notify || (current->ptrace & PT_PTRACED);
488+
return notify ?: (current->ptrace & PT_PTRACED) ? why : 0;
489+
}
490+
491+
/**
492+
* tracehook_finish_jctl - report about return from job control stop
493+
*
494+
* This is called by do_signal_stop() after wakeup.
495+
*/
496+
static inline void tracehook_finish_jctl(void)
497+
{
482498
}
483499

484500
#define DEATH_REAP -1

‎kernel/signal.c‎

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
705705

706706
if (why) {
707707
/*
708-
* The first thread which returns from finish_stop()
708+
* The first thread which returns from do_signal_stop()
709709
* will take ->siglock, notice SIGNAL_CLD_MASK, and
710710
* notify its parent. See get_signal_to_deliver().
711711
*/
@@ -1664,29 +1664,6 @@ void ptrace_notify(int exit_code)
16641664
spin_unlock_irq(&current->sighand->siglock);
16651665
}
16661666

1667-
static void
1668-
finish_stop(int stop_count)
1669-
{
1670-
/*
1671-
* If there are no other threads in the group, or if there is
1672-
* a group stop in progress and we are the last to stop,
1673-
* report to the parent. When ptraced, every thread reports itself.
1674-
*/
1675-
if (tracehook_notify_jctl(stop_count == 0, CLD_STOPPED)) {
1676-
read_lock(&tasklist_lock);
1677-
do_notify_parent_cldstop(current, CLD_STOPPED);
1678-
read_unlock(&tasklist_lock);
1679-
}
1680-
1681-
do {
1682-
schedule();
1683-
} while (try_to_freeze());
1684-
/*
1685-
* Now we don't run again until continued.
1686-
*/
1687-
current->exit_code = 0;
1688-
}
1689-
16901667
/*
16911668
* This performs the stopping for SIGSTOP and other stop signals.
16921669
* We have to stop all threads in the thread group.
@@ -1696,15 +1673,9 @@ finish_stop(int stop_count)
16961673
static int do_signal_stop(int signr)
16971674
{
16981675
struct signal_struct *sig = current->signal;
1699-
int stop_count;
1676+
int notify;
17001677

1701-
if (sig->group_stop_count > 0) {
1702-
/*
1703-
* There is a group stop in progress. We don't need to
1704-
* start another one.
1705-
*/
1706-
stop_count = --sig->group_stop_count;
1707-
} else {
1678+
if (!sig->group_stop_count) {
17081679
struct task_struct *t;
17091680

17101681
if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
@@ -1716,7 +1687,7 @@ static int do_signal_stop(int signr)
17161687
*/
17171688
sig->group_exit_code = signr;
17181689

1719-
stop_count = 0;
1690+
sig->group_stop_count = 1;
17201691
for (t = next_thread(current); t != current; t = next_thread(t))
17211692
/*
17221693
* Setting state to TASK_STOPPED for a group
@@ -1725,19 +1696,44 @@ static int do_signal_stop(int signr)
17251696
*/
17261697
if (!(t->flags & PF_EXITING) &&
17271698
!task_is_stopped_or_traced(t)) {
1728-
stop_count++;
1699+
sig->group_stop_count++;
17291700
signal_wake_up(t, 0);
17301701
}
1731-
sig->group_stop_count = stop_count;
17321702
}
1703+
/*
1704+
* If there are no other threads in the group, or if there is
1705+
* a group stop in progress and we are the last to stop, report
1706+
* to the parent. When ptraced, every thread reports itself.
1707+
*/
1708+
notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1709+
notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1710+
/*
1711+
* tracehook_notify_jctl() can drop and reacquire siglock, so
1712+
* we keep ->group_stop_count != 0 before the call. If SIGCONT
1713+
* or SIGKILL comes in between ->group_stop_count == 0.
1714+
*/
1715+
if (sig->group_stop_count) {
1716+
if (!--sig->group_stop_count)
1717+
sig->flags = SIGNAL_STOP_STOPPED;
1718+
current->exit_code = sig->group_exit_code;
1719+
__set_current_state(TASK_STOPPED);
1720+
}
1721+
spin_unlock_irq(&current->sighand->siglock);
17331722

1734-
if (stop_count == 0)
1735-
sig->flags = SIGNAL_STOP_STOPPED;
1736-
current->exit_code = sig->group_exit_code;
1737-
__set_current_state(TASK_STOPPED);
1723+
if (notify) {
1724+
read_lock(&tasklist_lock);
1725+
do_notify_parent_cldstop(current, notify);
1726+
read_unlock(&tasklist_lock);
1727+
}
1728+
1729+
/* Now we don't run again until woken by SIGCONT or SIGKILL */
1730+
do {
1731+
schedule();
1732+
} while (try_to_freeze());
1733+
1734+
tracehook_finish_jctl();
1735+
current->exit_code = 0;
17381736

1739-
spin_unlock_irq(&current->sighand->siglock);
1740-
finish_stop(stop_count);
17411737
return 1;
17421738
}
17431739

@@ -1806,14 +1802,15 @@ int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
18061802
int why = (signal->flags & SIGNAL_STOP_CONTINUED)
18071803
? CLD_CONTINUED : CLD_STOPPED;
18081804
signal->flags &= ~SIGNAL_CLD_MASK;
1809-
spin_unlock_irq(&sighand->siglock);
18101805

1811-
if (unlikely(!tracehook_notify_jctl(1, why)))
1812-
goto relock;
1806+
why = tracehook_notify_jctl(why, CLD_CONTINUED);
1807+
spin_unlock_irq(&sighand->siglock);
18131808

1814-
read_lock(&tasklist_lock);
1815-
do_notify_parent_cldstop(current->group_leader, why);
1816-
read_unlock(&tasklist_lock);
1809+
if (why) {
1810+
read_lock(&tasklist_lock);
1811+
do_notify_parent_cldstop(current->group_leader, why);
1812+
read_unlock(&tasklist_lock);
1813+
}
18171814
goto relock;
18181815
}
18191816

@@ -1978,14 +1975,14 @@ void exit_signals(struct task_struct *tsk)
19781975
if (unlikely(tsk->signal->group_stop_count) &&
19791976
!--tsk->signal->group_stop_count) {
19801977
tsk->signal->flags = SIGNAL_STOP_STOPPED;
1981-
group_stop = 1;
1978+
group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
19821979
}
19831980
out:
19841981
spin_unlock_irq(&tsk->sighand->siglock);
19851982

1986-
if (unlikely(group_stop) && tracehook_notify_jctl(1, CLD_STOPPED)) {
1983+
if (unlikely(group_stop)) {
19871984
read_lock(&tasklist_lock);
1988-
do_notify_parent_cldstop(tsk, CLD_STOPPED);
1985+
do_notify_parent_cldstop(tsk, group_stop);
19891986
read_unlock(&tasklist_lock);
19901987
}
19911988
}

0 commit comments

Comments
 (0)