Skip to contentSkip to conversation

Linux-2.5 fix for get_pid() hang

PATCH In review Paul Larson 2 participants 4 messages Jul 29, 2002 – Jul 30, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Linux-2.5 fix for get_pid() hang

This is a fix for the problem where get_pid will hang the machine if you request a new pid when all available pids are in use. This also adds the appropriate checking for p->tgid in get_pid that was somehow overlooked before. This patch has been in 2.4 since 2.4.19-pre9.

Please apply.

Paul Larson
Linux Test Project
http://ltp.sourceforge.net

kernel/fork.c +14 -1
diff -Nru a/kernel/fork.c b/kernel/fork.c
@@ -26,6 +26,7 @@
 #include <linux/mman.h>
 #include <linux/fs.h>
 #include <linux/security.h>
+#include <linux/compiler.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -136,12 +137,13 @@
 {
 	static int next_safe = PID_MAX;
 	struct task_struct *p;
-	int pid;
+	int pid, beginpid;
 
 	if (flags & CLONE_IDLETASK)
 		return 0;
 
 	spin_lock(&lastpid_lock);
+	beginpid = last_pid;
 	if((++last_pid) & 0xffff8000) {
 		last_pid = 300;		/* Skip daemons etc. */
 		goto inside;
@@ -161,12 +163,16 @@
 						last_pid = 300;
 					next_safe = PID_MAX;
 				}
+				if(unlikely(last_pid == beginpid))
+					goto nomorepids;
 				goto repeat;
 			}
 			if(p->pid > last_pid && next_safe > p->pid)
 				next_safe = p->pid;
 			if(p->pgrp > last_pid && next_safe > p->pgrp)
 				next_safe = p->pgrp;
+			if(p->tgid > last_pid && next_safe > p->tgid)
+				next_safe = p->tgid;
 			if(p->session > last_pid && next_safe > p->session)
 				next_safe = p->session;
 		}
@@ -176,6 +182,11 @@
 	spin_unlock(&lastpid_lock);
 
 	return pid;
+
+nomorepids:
+	read_unlock(&tasklist_lock);
+	spin_unlock(&lastpid_lock);
+	return 0;
 }
 
 static inline int dup_mmap(struct mm_struct * mm)
@@ -677,6 +688,8 @@
 
 	copy_flags(clone_flags, p);
 	p->pid = get_pid(clone_flags);
+	if (p->pid == 0 && !(clone_flags & CLONE_IDLETASK))
+		goto bad_fork_cleanup;
 	p->proc_dentry = NULL;
 
 	INIT_LIST_HEAD(&p->run_list);
#1 lore raw
Quoted 4 lines · Paul Larson

This is a fix for the problem where get_pid will hang the machine if you request a new pid when all available pids are in use. This also adds the appropriate checking for p->tgid in get_pid that was somehow overlooked before. This patch has been in 2.4 since 2.4.19-pre9.

no please, this below patch is superior and it fixes as well fixes a longstanding race condition in fork() (see the semaphore):

	http://www.us.kernel.org/pub/linux/kernel/people/andrea/kernels/v2.4/2.4.19rc3aa3/10_get_pid-no-deadlock-and-boosted-4

it may not apply cleanly but porting should be trivial.

Andrea

#2 lore raw
Quoted 11 lines · Andrea Arcangeli
Quoted 4 lines · Paul Larson

This is a fix for the problem where get_pid will hang the machine if you request a new pid when all available pids are in use. This also adds the appropriate checking for p->tgid in get_pid that was somehow overlooked before. This patch has been in 2.4 since 2.4.19-pre9.

no please, this below patch is superior and it fixes as well fixes a longstanding race condition in fork() (see the semaphore):

	http://www.us.kernel.org/pub/linux/kernel/people/andrea/kernels/v2.4/2.4.19rc3aa3/10_get_pid-no-deadlock-and-boosted-4

it may not apply cleanly but porting should be trivial.

Ported this forward to the latest 2.5 bk and ran my forkbomb test from ltp. Looks like it fixes the problem. I running the rest of the tests from LTP right now as a sanity check but so far everything looks ok.

Thanks,
Paul Larson
Linux Test Project
http://ltp.sourceforge.net

# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.536 -> 1.537
# include/linux/threads.h 1.2 -> 1.3
# kernel/fork.c 1.56 -> 1.57
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/07/30 [email protected] 1.537
#
# --------------------------------------------
#

include/linux/threads.h +1 -1
diff -Nru a/include/linux/threads.h b/include/linux/threads.h
@@ -19,6 +19,6 @@
 /*
  * This controls the maximum pid allocated to a process
  */
-#define PID_MAX 0x8000
+#define PID_NR 0x8000
 
 #endif
kernel/fork.c +107 -33

184 lines not on this page · raw message

#3 lore raw
In reply to Andrea Arcangeli

Please don't apply that last one, I got something out of order and it could cause problems. Here is the fixed version of just AA's patch. A couple of things to mention though... one of the things I had done differently was have get_pid return 0 instead of -1 for a failure. Of course some extra checks have to be done to make sure it isn't a valid 0 return, but I think I have a better way around all of this that I'll look at tonight. I've also been talking to Hubertus Franke who had a similar version of this to what Andrea had and it sounds like we might have some features from both that would be useful.

Personally, I just want to see the bug get fixed because I'm getting bitten by it quite frequently. All of these will do that though.

Paul Larson
Linux Test Project
http://ltp.sourceforge.net

include/linux/threads.h +1 -1
diff -Nru a/include/linux/threads.h b/include/linux/threads.h
@@ -19,6 +19,6 @@
 /*
  * This controls the maximum pid allocated to a process
  */
-#define PID_MAX 0x8000
+#define PID_NR 0x8000
 
 #endif
kernel/fork.c +107 -33

185 lines not on this page · raw message