[PATCH] proc: readdir race fix (take 3)
[pandora-kernel.git] / kernel / pid.c
index 8387e8c..ed89a73 100644 (file)
@@ -145,6 +145,23 @@ static int alloc_pidmap(void)
        return -1;
 }
 
+static int next_pidmap(int last)
+{
+       int offset;
+       pidmap_t *map;
+
+       offset = (last + 1) & BITS_PER_PAGE_MASK;
+       map = &pidmap_array[(last + 1)/BITS_PER_PAGE];
+       for (; map < &pidmap_array[PIDMAP_ENTRIES]; map++, offset = 0) {
+               if (unlikely(!map->page))
+                       continue;
+               offset = find_next_bit((map)->page, BITS_PER_PAGE, offset);
+               if (offset < BITS_PER_PAGE)
+                       return mk_pid(map, offset);
+       }
+       return -1;
+}
+
 fastcall void put_pid(struct pid *pid)
 {
        if (!pid)
@@ -302,6 +319,25 @@ struct pid *find_get_pid(pid_t nr)
        return pid;
 }
 
+/*
+ * Used by proc to find the first pid that is greater then or equal to nr.
+ *
+ * If there is a pid at nr this function is exactly the same as find_pid.
+ */
+struct pid *find_ge_pid(int nr)
+{
+       struct pid *pid;
+
+       do {
+               pid = find_pid(nr);
+               if (pid)
+                       break;
+               nr = next_pidmap(nr);
+       } while (nr > 0);
+
+       return pid;
+}
+
 /*
  * The pid hash table is scaled according to the amount of memory in the
  * machine.  From a minimum of 16 slots up to 4096 slots at one gigabyte or