From: Paul Larson <plars@austin.ibm.com>
To: Linus Torvalds <torvalds@transmeta.com>,
lkml <linux-kernel@vger.kernel.org>
Cc: davej@suse.de, frankeh@us.ibm.com, andrea@suse.de
Subject: [PATCH] Linux-2.5 fix/improve get_pid()
Date: 07 Aug 2002 17:03:54 -0500 [thread overview]
Message-ID: <1028757835.22405.300.camel@plars.austin.ibm.com> (raw)
This patch provides an improved version of get_pid() while also taking
care of the bug that causes the machine to hang when you hit PID_MAX.
It is based on both solutions to the problem provided by Hubertus Franke
and Andrea Arcangeli. This uses a bitmap to find an available pid and
uses Hubertus's adaptive implementation to only use this when it is more
beneficial than the old mechanism. The getpid_mutex from AA's patch is
also carried over to avoid the race where another cpu could get the same
pid before SET_LINKS was called.
This should patch cleanly against 2.5.30 or bk current.
Please apply.
--- a/kernel/fork.c Wed Aug 7 16:37:38 2002
+++ b/kernel/fork.c Wed Aug 7 16:05:22 2002
@@ -50,6 +50,12 @@
rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
+/*
+ * Protectes next_safe, last_pid and it avoids races
+ * between get_pid and SET_LINKS().
+ */
+static DECLARE_MUTEX(getpid_mutex);
+
void add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
{
unsigned long flags;
@@ -129,27 +135,107 @@
kmem_cache_free(task_struct_cachep,tsk);
}
-/* Protects next_safe and last_pid. */
-spinlock_t lastpid_lock = SPIN_LOCK_UNLOCKED;
+/* this should be provided in every architecture */
+#ifndef SHIFT_PER_LONG
+#if BITS_PER_LONG == 64
+# define SHIFT_PER_LONG 6
+#elif BITS_PER_LONG == 32
+# define SHIFT_PER_LONG 5
+#else
+#error "SHIFT_PER_LONG"
+#endif
+#endif
+
+#define RESERVED_PIDS (300)
+#define GETPID_THRESHOLD (22000) /* when to switch to a mapped algo */
+#define PID_MAP_SIZE (PID_MAX >> SHIFT_PER_LONG)
+static unsigned long pid_map[PID_MAP_SIZE];
+static int next_safe = PID_MAX;
+
+static inline void mark_pid(int pid)
+{
+ __set_bit(pid,pid_map);
+}
+
+static int get_pid_by_map(int lastpid)
+{
+ static int mark_and_sweep = 0;
+
+ int round = 0;
+ struct task_struct *p;
+ int i;
+ unsigned long mask;
+ int fpos;
+
+
+ if (mark_and_sweep) {
+repeat:
+ mark_and_sweep = 0;
+ memset(pid_map, 0, PID_MAP_SIZE * sizeof(unsigned long));
+ lastpid = RESERVED_PIDS;
+ }
+ for_each_task(p) {
+ mark_pid(p->pid);
+ mark_pid(p->pgrp);
+ mark_pid(p->tgid);
+ mark_pid(p->session);
+ }
+
+ /* find next free pid */
+ i = (lastpid >> SHIFT_PER_LONG);
+ mask = pid_map[i] | ((1 << ((lastpid & (BITS_PER_LONG-1)))) - 1);
+
+ while ((mask == ~0) && (++i < PID_MAP_SIZE))
+ mask = pid_map[i];
+
+ if (i == PID_MAP_SIZE) {
+ if (round == 0) {
+ round = 1;
+ goto repeat;
+ }
+ next_safe = RESERVED_PIDS;
+ mark_and_sweep = 1; /* mark next time */
+ return 0;
+ }
+
+ fpos = ffz(mask);
+ i &= (PID_MAX-1);
+ lastpid = (i << SHIFT_PER_LONG) + fpos;
+
+ /* find next save pid */
+ mask &= ~((1 << fpos) - 1);
+
+ while ((mask == 0) && (++i < PID_MAP_SIZE))
+ mask = pid_map[i];
+
+ if (i==PID_MAP_SIZE)
+ next_safe = PID_MAX;
+ else
+ next_safe = (i << SHIFT_PER_LONG) + ffs(mask) - 1;
+ return lastpid;
+}
static int get_pid(unsigned long flags)
{
- static int next_safe = PID_MAX;
struct task_struct *p;
int pid;
- if (flags & CLONE_IDLETASK)
- return 0;
-
- spin_lock(&lastpid_lock);
if((++last_pid) & 0xffff8000) {
- last_pid = 300; /* Skip daemons etc. */
+ last_pid = RESERVED_PIDS; /* Skip daemons etc. */
goto inside;
}
if(last_pid >= next_safe) {
inside:
next_safe = PID_MAX;
read_lock(&tasklist_lock);
+ if (nr_threads > GETPID_THRESHOLD) {
+ last_pid = get_pid_by_map(last_pid);
+ if (last_pid == 0) {
+ last_pid = PID_MAX;
+ goto nomorepids;
+ }
+ } else {
+ int beginpid = last_pid;
repeat:
for_each_task(p) {
if(p->pid == last_pid ||
@@ -158,24 +244,33 @@
p->session == last_pid) {
if(++last_pid >= next_safe) {
if(last_pid & 0xffff8000)
- last_pid = 300;
+ last_pid = RESERVED_PIDS;
next_safe = PID_MAX;
}
+ if(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;
}
+ }
read_unlock(&tasklist_lock);
}
pid = last_pid;
- spin_unlock(&lastpid_lock);
return pid;
+
+nomorepids:
+ next_safe = last_pid = PID_MAX;
+ read_unlock(&tasklist_lock);
+ return 0;
}
static inline int dup_mmap(struct mm_struct * mm)
@@ -669,7 +764,14 @@
p->state = TASK_UNINTERRUPTIBLE;
copy_flags(clone_flags, p);
- p->pid = get_pid(clone_flags);
+ down(&getpid_mutex);
+ if (clone_flags & CLONE_IDLETASK)
+ p->pid = 0;
+ else {
+ p->pid = get_pid(clone_flags);
+ if (p->pid == 0)
+ goto bad_fork_cleanup;
+ }
p->proc_dentry = NULL;
INIT_LIST_HEAD(&p->run_list);
@@ -793,10 +895,20 @@
list_add(&p->thread_group, ¤t->thread_group);
}
+ /*
+ * We must do the SET_LINKS() under the getpid_mutex, to avoid
+ * another CPU to get our same PID between the release of of the
+ * getpid_mutex and the SET_LINKS().
+ *
+ * In short to avoid SMP races the new child->pid must be just visible
+ * in the tasklist by the time we drop the getpid_mutex.
+ */
SET_LINKS(p);
+
hash_pid(p);
nr_threads++;
write_unlock_irq(&tasklist_lock);
+ up(&getpid_mutex);
retval = 0;
fork_out:
@@ -819,6 +931,7 @@
bad_fork_cleanup_security:
security_ops->task_free_security(p);
bad_fork_cleanup:
+ up(&getpid_mutex);
put_exec_domain(p->thread_info->exec_domain);
if (p->binfmt && p->binfmt->module)
__MOD_DEC_USE_COUNT(p->binfmt->module);
next reply other threads:[~2002-08-07 22:05 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-07 22:03 Paul Larson [this message]
2002-08-07 23:06 ` Andrew Morton
2002-08-08 0:24 ` Andries Brouwer
2002-08-08 19:42 ` William Lee Irwin III
2002-08-08 20:47 ` Andries Brouwer
2002-08-09 11:22 ` Analysis for Linux-2.5 fix/improve get_pid(), comparing various approaches Hubertus Franke
2002-08-09 15:36 ` Andries Brouwer
2002-08-09 18:14 ` Hubertus Franke
2002-08-09 16:05 ` Linus Torvalds
2002-08-09 18:18 ` Hubertus Franke
2002-08-08 20:24 ` [PATCH] Linux-2.5 fix/improve get_pid() Linus Torvalds
2002-08-08 21:30 ` H. Peter Anvin
2002-08-08 21:45 ` William Lee Irwin III
2002-08-09 4:42 ` William Lee Irwin III
2002-08-08 18:56 Hubertus Franke
2002-08-08 19:15 ` Rik van Riel
2002-08-08 19:18 ` Paul Larson
2002-08-08 21:43 Hubertus Franke
2002-08-08 22:02 ` Linus Torvalds
2002-08-08 22:26 ` Linus Torvalds
2002-08-08 23:09 ` Albert D. Cahalan
2002-08-09 3:26 ` Chris Adams
2002-08-09 7:04 ` Albert D. Cahalan
2002-08-09 8:48 ` Helge Hafting
2002-08-09 10:32 ` Alan Cox
2002-08-09 9:35 ` Andrew Morton
2002-08-09 10:37 ` Andries Brouwer
2002-08-09 13:00 ` Chris Adams
2002-08-09 14:39 ` Albert D. Cahalan
2002-08-09 13:59 ` Rik van Riel
2002-08-09 14:57 ` Albert D. Cahalan
2002-08-08 22:34 ` Paul Larson
2002-08-08 22:44 ` Rik van Riel
2002-08-08 22:37 ` Rik van Riel
2002-08-09 1:49 ` Andries Brouwer
2002-08-09 19:34 ` Paul Larson
2002-08-09 20:13 ` Paul Larson
2002-08-09 20:40 ` Andries Brouwer
2002-08-09 21:23 ` Linus Torvalds
2002-08-09 21:42 ` Linus Torvalds
2002-08-09 21:46 ` Paul Larson
2002-08-09 22:04 ` Linus Torvalds
2002-08-10 17:23 ` Jamie Lokier
2002-08-10 18:33 ` Linus Torvalds
2002-08-10 18:48 ` Jamie Lokier
2002-08-11 20:41 ` Alan Cox
2002-08-11 19:58 ` Jamie Lokier
2002-08-11 21:23 ` Alan Cox
2002-08-11 20:10 ` Jamie Lokier
2002-08-12 8:56 ` Padraig Brady
2002-08-12 10:37 ` Alan Cox
2002-08-12 9:21 ` Padraig Brady
2002-08-12 14:40 ` Paul Larson
2002-08-08 21:50 Hubertus Franke
2002-08-09 0:53 Hubertus Franke
2002-08-12 16:15 Jim Houston
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1028757835.22405.300.camel@plars.austin.ibm.com \
--to=plars@austin.ibm.com \
--cc=andrea@suse.de \
--cc=davej@suse.de \
--cc=frankeh@us.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@transmeta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®