aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2011-06-02 05:14:00 -0400
committerOleg Nesterov <oleg@redhat.com>2011-06-04 12:17:11 -0400
commit7dd3db54e77d21eb95e145f19ba53f68250d0e73 (patch)
tree628e44b22e6fbf2828cf2c533c41b3d24f3e3ec9
parent6dfca32984237a8a011b5bf367e53341a265b2a4 (diff)
job control: introduce task_set_jobctl_pending()
task->jobctl currently hosts JOBCTL_STOP_PENDING and will host TRAP pending bits too. Setting pending conditions on a dying task may make the task unkillable. Currently, each setting site is responsible for checking for the condition but with to-be-added job control traps this becomes too fragile. This patch adds task_set_jobctl_pending() which should be used when setting task->jobctl bits to schedule a stop or trap. The function performs the followings to ease setting pending bits. * Sanity checks. * If fatal signal is pending or PF_EXITING is set, no bit is set. * STOP_SIGMASK is automatically cleared if new value is being set. do_signal_stop() and ptrace_attach() are updated to use task_set_jobctl_pending() instead of setting STOP_PENDING explicitly. The surrounding structures around setting are changed to fit task_set_jobctl_pending() better but there should be no userland visible behavior difference. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
-rw-r--r--include/linux/sched.h2
-rw-r--r--kernel/ptrace.c6
-rw-r--r--kernel/signal.c46
3 files changed, 45 insertions, 9 deletions
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 5a958b17f9fe..5157bd9eee37 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1819,6 +1819,8 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *
1819 1819
1820#define JOBCTL_PENDING_MASK JOBCTL_STOP_PENDING 1820#define JOBCTL_PENDING_MASK JOBCTL_STOP_PENDING
1821 1821
1822extern bool task_set_jobctl_pending(struct task_struct *task,
1823 unsigned int mask);
1822extern void task_clear_jobctl_pending(struct task_struct *task, 1824extern void task_clear_jobctl_pending(struct task_struct *task,
1823 unsigned int mask); 1825 unsigned int mask);
1824 1826
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index eb191116edf7..0c37d999c8b8 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -256,10 +256,10 @@ static int ptrace_attach(struct task_struct *task)
256 * The following task_is_stopped() test is safe as both transitions 256 * The following task_is_stopped() test is safe as both transitions
257 * in and out of STOPPED are protected by siglock. 257 * in and out of STOPPED are protected by siglock.
258 */ 258 */
259 if (task_is_stopped(task)) { 259 if (task_is_stopped(task) &&
260 task->jobctl |= JOBCTL_STOP_PENDING | JOBCTL_TRAPPING; 260 task_set_jobctl_pending(task,
261 JOBCTL_STOP_PENDING | JOBCTL_TRAPPING))
261 signal_wake_up(task, 1); 262 signal_wake_up(task, 1);
262 }
263 263
264 spin_unlock(&task->sighand->siglock); 264 spin_unlock(&task->sighand->siglock);
265 265
diff --git a/kernel/signal.c b/kernel/signal.c
index 637a171b65b6..9ab91c516c3f 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -224,6 +224,39 @@ static inline void print_dropped_signal(int sig)
224} 224}
225 225
226/** 226/**
227 * task_set_jobctl_pending - set jobctl pending bits
228 * @task: target task
229 * @mask: pending bits to set
230 *
231 * Clear @mask from @task->jobctl. @mask must be subset of
232 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
233 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
234 * cleared. If @task is already being killed or exiting, this function
235 * becomes noop.
236 *
237 * CONTEXT:
238 * Must be called with @task->sighand->siglock held.
239 *
240 * RETURNS:
241 * %true if @mask is set, %false if made noop because @task was dying.
242 */
243bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask)
244{
245 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
246 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
247 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
248
249 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
250 return false;
251
252 if (mask & JOBCTL_STOP_SIGMASK)
253 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
254
255 task->jobctl |= mask;
256 return true;
257}
258
259/**
227 * task_clear_jobctl_trapping - clear jobctl trapping bit 260 * task_clear_jobctl_trapping - clear jobctl trapping bit
228 * @task: target task 261 * @task: target task
229 * 262 *
@@ -1902,19 +1935,20 @@ static int do_signal_stop(int signr)
1902 else 1935 else
1903 WARN_ON_ONCE(!task_ptrace(current)); 1936 WARN_ON_ONCE(!task_ptrace(current));
1904 1937
1905 current->jobctl &= ~JOBCTL_STOP_SIGMASK; 1938 sig->group_stop_count = 0;
1906 current->jobctl |= signr | gstop; 1939
1907 sig->group_stop_count = 1; 1940 if (task_set_jobctl_pending(current, signr | gstop))
1941 sig->group_stop_count++;
1942
1908 for (t = next_thread(current); t != current; 1943 for (t = next_thread(current); t != current;
1909 t = next_thread(t)) { 1944 t = next_thread(t)) {
1910 t->jobctl &= ~JOBCTL_STOP_SIGMASK;
1911 /* 1945 /*
1912 * Setting state to TASK_STOPPED for a group 1946 * Setting state to TASK_STOPPED for a group
1913 * stop is always done with the siglock held, 1947 * stop is always done with the siglock held,
1914 * so this check has no races. 1948 * so this check has no races.
1915 */ 1949 */
1916 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) { 1950 if (!task_is_stopped(t) &&
1917 t->jobctl |= signr | gstop; 1951 task_set_jobctl_pending(t, signr | gstop)) {
1918 sig->group_stop_count++; 1952 sig->group_stop_count++;
1919 signal_wake_up(t, 0); 1953 signal_wake_up(t, 0);
1920 } 1954 }