Skip to contentSkip to conversation

2.4.18-rc2 Fix for get_pid hang

PATCH In review Paul Larson 4 participants 6 messages Feb 22, 2002 – Feb 25, 2002

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

PATCH 1/1 2.4.18-rc2 Fix for get_pid hang

Marcelo,

This was made against 2.4.18-rc2 but applies cleanly against 2.4.18-rc4. This is a fix for a problem where if we run out of available pids, get_pid will hang the system while it searches through the tasks for an available pid forever.

Thanks,
Paul Larson

kernel/fork.c +14 -1
diff -Naur linux-2.4.18-rc2/kernel/fork.c linux-getpid/kernel/fork.c
@@ -20,6 +20,7 @@
 #include <linux/vmalloc.h>
 #include <linux/completion.h>
 #include <linux/personality.h>
+#include <linux/compiler.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -85,12 +86,13 @@
 {
 	static int next_safe = PID_MAX;
 	struct task_struct *p;
-	int pid;
+	int pid, beginpid;
 
 	if (flags & CLONE_PID)
 		return current->pid;
 
 	spin_lock(&lastpid_lock);
+	beginpid = last_pid;
 	if((++last_pid) & 0xffff8000) {
 		last_pid = 300;		/* Skip daemons etc. */
 		goto inside;
@@ -110,12 +112,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;
 		}
@@ -125,6 +131,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)
@@ -620,6 +631,8 @@
 
 	copy_flags(clone_flags, p);
 	p->pid = get_pid(clone_flags);
+	if (p->pid == 0 && current->pid != 0)
+		goto bad_fork_cleanup;
 
 	p->run_list.next = NULL;
 	p->run_list.prev = NULL;
#1 lore raw
Quoted 4 lines

This was made against 2.4.18-rc2 but applies cleanly against 2.4.18-rc4. This is a fix for a problem where if we run out of available pids, get_pid will hang the system while it searches through the tasks for an available pid forever.

Wouldn't it be a much cleaner patch to limit the maximum number of processes to less than the number of pids available. You seem to be fixing a non problem by adding branches to the innards of a loop.

#2 lore raw
Quoted 8 lines · Alan Cox
Quoted 4 lines

This was made against 2.4.18-rc2 but applies cleanly against 2.4.18-rc4. This is a fix for a problem where if we run out of available pids, get_pid will hang the system while it searches through the tasks for an available pid forever.

Wouldn't it be a much cleaner patch to limit the maximum number of processes to less than the number of pids available. You seem to be fixing a non problem by adding branches to the innards of a loop.

The problem here is that thread and process groups share the pid namespace, so you the number of processes at which you run out of pids varies between 10700 and 32000 ...

regards,

Rik

Signature · 4 lines
"Linux holds advantages over the single-vendor commercial OS"
    -- Microsoft's "Competing with Linux" document

http://www.surriel.com/		http://distro.conectiva.com/
#3 lore raw
In reply to Alan Cox
Quoted 9 lines · Alan Cox
Quoted 4 lines

This was made against 2.4.18-rc2 but applies cleanly against 2.4.18-rc4. This is a fix for a problem where if we run out of available pids, get_pid will hang the system while it searches through the tasks for an available pid forever.

Wouldn't it be a much cleaner patch to limit the maximum number of processes to less than the number of pids available. You seem to be fixing a non problem by adding branches to the innards of a loop.

That was my original thought, but as Rik and WLI already pointed out, it won't account for process groups, tgids, etc. This isn't a purely theoretical problem either, as we have run up against it many times.

Thanks,
Paul Larson

#4 lore raw
Quoted 6 lines
Quoted 3 lines

to less than the number of pids available. You seem to be fixing a non problem by adding branches to the innards of a loop.

That was my original thought, but as Rik and WLI already pointed out, it won't account for process groups, tgids, etc. This isn't a purely theoretical problem either, as we have run up against it many times.

Agreed - and I don't have any better solutions except 32bit pid_t - which I suspect one day we are going to need. At least the O(1) scheduler now makes it feasible 8)

#5 lore raw
In reply to Paul Larson
Quoted 4 lines · Paul Larson
Quoted 4 lines

This was made against 2.4.18-rc2 but applies cleanly against 2.4.18-rc4. This is a fix for a problem where if we run out of available pids, get_pid will hang the system while it searches through the tasks for an available pid forever.

Quoted 3 lines · Alan Cox

Wouldn't it be a much cleaner patch to limit the maximum number of processes to less than the number of pids available. You seem to be fixing a non problem by adding branches to the innards of a loop.

I've seen this one before. It seems to kick in at 11K processes, where one would normally expect it much higher... so I'm not sure a constant upper bound on that counter suffices. Maybe clashes of pid's with pgrp's and sessions and tgrps are what does that, maybe it's something else.

and of course:

#include <stdgeek.h> /* Any hope of a non-O(tasks) solution? */

Cheers,
Bill