UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / kernel / pid.c
index b2e5f78..6aeebc2 100644 (file)
@@ -40,7 +40,7 @@
 #define pid_hashfn(nr, ns)     \
        hash_long((unsigned long)nr + (unsigned long)ns, pidhash_shift)
 static struct hlist_head *pid_hash;
-static int pidhash_shift;
+static unsigned int pidhash_shift = 4;
 struct pid init_struct_pid = INIT_STRUCT_PID;
 
 int pid_max = PID_MAX_DEFAULT;
@@ -122,6 +122,43 @@ static void free_pidmap(struct upid *upid)
        atomic_inc(&map->nr_free);
 }
 
+/*
+ * If we started walking pids at 'base', is 'a' seen before 'b'?
+ */
+static int pid_before(int base, int a, int b)
+{
+       /*
+        * This is the same as saying
+        *
+        * (a - base + MAXUINT) % MAXUINT < (b - base + MAXUINT) % MAXUINT
+        * and that mapping orders 'a' and 'b' with respect to 'base'.
+        */
+       return (unsigned)(a - base) < (unsigned)(b - base);
+}
+
+/*
+ * We might be racing with someone else trying to set pid_ns->last_pid.
+ * We want the winner to have the "later" value, because if the
+ * "earlier" value prevails, then a pid may get reused immediately.
+ *
+ * Since pids rollover, it is not sufficient to just pick the bigger
+ * value.  We have to consider where we started counting from.
+ *
+ * 'base' is the value of pid_ns->last_pid that we observed when
+ * we started looking for a pid.
+ *
+ * 'pid' is the pid that we eventually found.
+ */
+static void set_last_pid(struct pid_namespace *pid_ns, int base, int pid)
+{
+       int prev;
+       int last_write = base;
+       do {
+               prev = last_write;
+               last_write = cmpxchg(&pid_ns->last_pid, prev, pid);
+       } while ((prev != last_write) && (pid_before(base, last_write, pid)));
+}
+
 static int alloc_pidmap(struct pid_namespace *pid_ns)
 {
        int i, offset, max_scan, pid, last = pid_ns->last_pid;
@@ -132,7 +169,12 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
                pid = RESERVED_PIDS;
        offset = pid & BITS_PER_PAGE_MASK;
        map = &pid_ns->pidmap[pid/BITS_PER_PAGE];
-       max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
+       /*
+        * If last_pid points into the middle of the map->page we
+        * want to scan this bitmap block twice, the second time
+        * we start with offset == 0 (or RESERVED_PIDS).
+        */
+       max_scan = DIV_ROUND_UP(pid_max, BITS_PER_PAGE) - !offset;
        for (i = 0; i <= max_scan; ++i) {
                if (unlikely(!map->page)) {
                        void *page = kzalloc(PAGE_SIZE, GFP_KERNEL);
@@ -141,11 +183,12 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
                         * installing it:
                         */
                        spin_lock_irq(&pidmap_lock);
-                       if (map->page)
-                               kfree(page);
-                       else
+                       if (!map->page) {
                                map->page = page;
+                               page = NULL;
+                       }
                        spin_unlock_irq(&pidmap_lock);
+                       kfree(page);
                        if (unlikely(!map->page))
                                break;
                }
@@ -153,20 +196,12 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
                        do {
                                if (!test_and_set_bit(offset, map->page)) {
                                        atomic_dec(&map->nr_free);
-                                       pid_ns->last_pid = pid;
+                                       set_last_pid(pid_ns, last, pid);
                                        return pid;
                                }
                                offset = find_next_offset(map, offset);
                                pid = mk_pid(pid_ns, map, offset);
-                       /*
-                        * find_next_offset() found a bit, the pid from it
-                        * is in-bounds, and if we fell back to the last
-                        * bitmap block and the final block was the same
-                        * as the starting point, pid is before last_pid.
-                        */
-                       } while (offset < BITS_PER_PAGE && pid < pid_max &&
-                                       (i != max_scan || pid < last ||
-                                           !((last+1) & BITS_PER_PAGE_MASK)));
+                       } while (offset < BITS_PER_PAGE && pid < pid_max);
                }
                if (map < &pid_ns->pidmap[(pid_max-1)/BITS_PER_PAGE]) {
                        ++map;
@@ -182,11 +217,14 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
        return -1;
 }
 
-int next_pidmap(struct pid_namespace *pid_ns, int last)
+int next_pidmap(struct pid_namespace *pid_ns, unsigned int last)
 {
        int offset;
        struct pidmap *map, *end;
 
+       if (last >= PID_MAX_LIMIT)
+               return -1;
+
        offset = (last + 1) & BITS_PER_PAGE_MASK;
        map = &pid_ns->pidmap[(last + 1)/BITS_PER_PAGE];
        end = &pid_ns->pidmap[PIDMAP_ENTRIES];
@@ -268,12 +306,11 @@ struct pid *alloc_pid(struct pid_namespace *ns)
        for (type = 0; type < PIDTYPE_MAX; ++type)
                INIT_HLIST_HEAD(&pid->tasks[type]);
 
+       upid = pid->numbers + ns->level;
        spin_lock_irq(&pidmap_lock);
-       for (i = ns->level; i >= 0; i--) {
-               upid = &pid->numbers[i];
+       for ( ; upid >= pid->numbers; --upid)
                hlist_add_head_rcu(&upid->pid_chain,
                                &pid_hash[pid_hashfn(upid->nr, upid->ns)]);
-       }
        spin_unlock_irq(&pidmap_lock);
 
 out:
@@ -367,7 +404,9 @@ struct task_struct *pid_task(struct pid *pid, enum pid_type type)
        struct task_struct *result = NULL;
        if (pid) {
                struct hlist_node *first;
-               first = rcu_dereference(pid->tasks[type].first);
+               first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
+                                             rcu_read_lock_held() ||
+                                             lockdep_tasklist_lock_is_held());
                if (first)
                        result = hlist_entry(first, struct task_struct, pids[(type)].node);
        }
@@ -376,28 +415,18 @@ struct task_struct *pid_task(struct pid *pid, enum pid_type type)
 EXPORT_SYMBOL(pid_task);
 
 /*
- * Must be called under rcu_read_lock() or with tasklist_lock read-held.
+ * Must be called under rcu_read_lock().
  */
-struct task_struct *find_task_by_pid_type_ns(int type, int nr,
-               struct pid_namespace *ns)
+struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
 {
-       return pid_task(find_pid_ns(nr, ns), type);
+       rcu_lockdep_assert(rcu_read_lock_held());
+       return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
 }
 
-EXPORT_SYMBOL(find_task_by_pid_type_ns);
-
 struct task_struct *find_task_by_vpid(pid_t vnr)
 {
-       return find_task_by_pid_type_ns(PIDTYPE_PID, vnr,
-                       current->nsproxy->pid_ns);
+       return find_task_by_pid_ns(vnr, current->nsproxy->pid_ns);
 }
-EXPORT_SYMBOL(find_task_by_vpid);
-
-struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
-{
-       return find_task_by_pid_type_ns(PIDTYPE_PID, nr, ns);
-}
-EXPORT_SYMBOL(find_task_by_pid_ns);
 
 struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
 {
@@ -510,25 +539,25 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
 void __init pidhash_init(void)
 {
        int i, pidhash_size;
-       unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
 
-       pidhash_shift = max(4, fls(megabytes * 4));
-       pidhash_shift = min(12, pidhash_shift);
+       pid_hash = alloc_large_system_hash("PID", sizeof(*pid_hash), 0, 18,
+                                          HASH_EARLY | HASH_SMALL,
+                                          &pidhash_shift, NULL, 4096);
        pidhash_size = 1 << pidhash_shift;
 
-       printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
-               pidhash_size, pidhash_shift,
-               pidhash_size * sizeof(struct hlist_head));
-
-       pid_hash = alloc_bootmem(pidhash_size * sizeof(*(pid_hash)));
-       if (!pid_hash)
-               panic("Could not alloc pidhash!\n");
        for (i = 0; i < pidhash_size; i++)
                INIT_HLIST_HEAD(&pid_hash[i]);
 }
 
 void __init pidmap_init(void)
 {
+       /* bump default and minimum pid_max based on number of cpus */
+       pid_max = min(pid_max_max, max_t(int, pid_max,
+                               PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
+       pid_max_min = max_t(int, pid_max_min,
+                               PIDS_PER_CPU_MIN * num_possible_cpus());
+       pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
+
        init_pid_ns.pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
        /* Reserve PID 0. We never call free_pidmap(0) */
        set_bit(0, init_pid_ns.pidmap[0].page);