Skip to content

Commit 4a2c7a7

Browse files
Oleg NesterovLinus Torvalds
authored andcommitted
[PATCH] make fork() atomic wrt pgrp/session signals
Eric W. Biederman wrote: > > Ok. SUSV3/Posix is clear, fork is atomic with respect > to signals. Either a signal comes before or after a > fork but not during. (See the rationale section). > http://www.opengroup.org/onlinepubs/000095399/functions/fork.html > > The tasklist_lock does not stop forks from adding to a process > group. The forks stall while the tasklist_lock is held, but a fork > that began before we grabbed the tasklist_lock simply completes > afterwards, and the child does not receive the signal. This also means that SIGSTOP or sig_kernel_coredump() signal can't be delivered to pgrp/session reliably. With this patch copy_process() returns -ERESTARTNOINTR when it detects a pending signal, fork() will be restarted transparently after handling the signals. This patch also deletes now unneeded "group_stop_count > 0" check, copy_process() can no longer succeed while group stop in progress. Signed-off-by: Oleg Nesterov <[email protected]> Acked-By: Eric Biederman <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 47e6532 commit 4a2c7a7

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

kernel/fork.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,16 +1136,6 @@ static task_t *copy_process(unsigned long clone_flags,
11361136
!cpu_online(task_cpu(p))))
11371137
set_task_cpu(p, smp_processor_id());
11381138

1139-
/*
1140-
* Check for pending SIGKILL! The new thread should not be allowed
1141-
* to slip out of an OOM kill. (or normal SIGKILL.)
1142-
*/
1143-
if (sigismember(&current->pending.signal, SIGKILL)) {
1144-
write_unlock_irq(&tasklist_lock);
1145-
retval = -EINTR;
1146-
goto bad_fork_cleanup_namespace;
1147-
}
1148-
11491139
/* CLONE_PARENT re-uses the old parent */
11501140
if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
11511141
p->real_parent = current->real_parent;
@@ -1154,6 +1144,23 @@ static task_t *copy_process(unsigned long clone_flags,
11541144
p->parent = p->real_parent;
11551145

11561146
spin_lock(&current->sighand->siglock);
1147+
1148+
/*
1149+
* Process group and session signals need to be delivered to just the
1150+
* parent before the fork or both the parent and the child after the
1151+
* fork. Restart if a signal comes in before we add the new process to
1152+
* it's process group.
1153+
* A fatal signal pending means that current will exit, so the new
1154+
* thread can't slip out of an OOM kill (or normal SIGKILL).
1155+
*/
1156+
recalc_sigpending();
1157+
if (signal_pending(current)) {
1158+
spin_unlock(&current->sighand->siglock);
1159+
write_unlock_irq(&tasklist_lock);
1160+
retval = -ERESTARTNOINTR;
1161+
goto bad_fork_cleanup_namespace;
1162+
}
1163+
11571164
if (clone_flags & CLONE_THREAD) {
11581165
/*
11591166
* Important: if an exit-all has been started then
@@ -1170,16 +1177,6 @@ static task_t *copy_process(unsigned long clone_flags,
11701177
p->group_leader = current->group_leader;
11711178
list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
11721179

1173-
if (current->signal->group_stop_count > 0) {
1174-
/*
1175-
* There is an all-stop in progress for the group.
1176-
* We ourselves will stop as soon as we check signals.
1177-
* Make the new thread part of that group stop too.
1178-
*/
1179-
current->signal->group_stop_count++;
1180-
set_tsk_thread_flag(p, TIF_SIGPENDING);
1181-
}
1182-
11831180
if (!cputime_eq(current->signal->it_virt_expires,
11841181
cputime_zero) ||
11851182
!cputime_eq(current->signal->it_prof_expires,

0 commit comments

Comments
 (0)