public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fix race copy_process() vs de_thread()
@ 2009-08-24  4:01 Hiroshi Shimamoto
  2009-08-24  5:11 ` KAMEZAWA Hiroyuki
  2009-08-24  6:14 ` [PATCH] " Roland McGrath
  0 siblings, 2 replies; 13+ messages in thread
From: Hiroshi Shimamoto @ 2009-08-24  4:01 UTC (permalink / raw)
  To: Andrew Morton, Oleg Nesterov, Roland McGrath, linux-kernel

From: Hiroshi Shimamoto <h-shimamoto@ct.jp.nec.com>

There is a race between de_thread() and copy_process().
When a thread is during execve and another thread is during clone, exec-ing
thread may be hung up in de_thread() waiting other threads are finished.
The root cause is that cleanup_signal() which is called when fork() failed
doesn't cause wake up the waiting thread at de_thread(), because there is no
check signal->count.

We need the check signal->group_exit_task and signal->notify_count.

Here is a reproducer, it may generate a thread which never die.

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

void *null_thread(void *p)
{
	for (;;)
		sleep(1);

	return NULL;
}

void *exec_thread(void *p)
{
	execl("/bin/true", "/bin/true", NULL);

	return null_thread(p);
}

int main(int argc, char **argv)
{
	for (;;) {
		pid_t pid;
		int ret, status;

		pid = fork();
		if (pid < 0)
			break;

		if (!pid) {
			pthread_t tid;

			pthread_create(&tid, NULL, exec_thread, NULL);
			for (;;)
				pthread_create(&tid, NULL, null_thread, NULL);
		}

		do {
			ret = waitpid(pid, &status, 0);
		} while (ret == -1 && errno == EINTR);
	}

	return 0;
}

Signed-off-by: Hiroshi Shimamoto <h-shimamoto@ct.jp.nec.com>
---
 kernel/fork.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index 3ffa10f..be6c5b5 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -882,6 +882,9 @@ static void cleanup_signal(struct task_struct *tsk)
 {
 	struct signal_struct *sig = tsk->signal;
 
+	if (!sig)
+		return;
+
 	atomic_dec(&sig->live);
 
 	if (atomic_dec_and_test(&sig->count))
@@ -1230,6 +1233,15 @@ static struct task_struct *copy_process(unsigned long clone_flags,
  	 */
 	recalc_sigpending();
 	if (signal_pending(current)) {
+		/* If there is any task waiting for the group exit, notify it */
+		if ((clone_flags & CLONE_THREAD) &&
+		    p->signal->group_exit_task) {
+			atomic_dec(&p->signal->live);
+			atomic_dec(&p->signal->count);
+			if (atomic_read(&p->signal->count) == p->signal->notify_count)
+				wake_up_process(p->signal->group_exit_task);
+			p->signal = NULL;
+		}
 		spin_unlock(&current->sighand->siglock);
 		write_unlock_irq(&tasklist_lock);
 		retval = -ERESTARTNOINTR;
-- 
1.6.3.3


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2009-08-25  0:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-24  4:01 [PATCH] fix race copy_process() vs de_thread() Hiroshi Shimamoto
2009-08-24  5:11 ` KAMEZAWA Hiroyuki
2009-08-24  5:58   ` [PATCH v2] " Hiroshi Shimamoto
2009-08-24  6:14 ` [PATCH] " Roland McGrath
2009-08-24  6:20   ` Roland McGrath
2009-08-24  6:32   ` Hiroshi Shimamoto
2009-08-24  8:38     ` Oleg Nesterov
2009-08-24  8:53       ` Oleg Nesterov
2009-08-24  9:15         ` Roland McGrath
2009-08-24 10:50           ` Oleg Nesterov
2009-08-24 16:27       ` [PATCH v3] " Oleg Nesterov
2009-08-24 16:57         ` Roland McGrath
2009-08-25  0:10         ` Hiroshi Shimamoto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox