2 * Generic pidhash and scalable, time-bounded PID allocator
4 * (C) 2002-2003 William Irwin, IBM
5 * (C) 2004 William Irwin, Oracle
6 * (C) 2002-2004 Ingo Molnar, Red Hat
8 * pid-structures are backing objects for tasks sharing a given ID to chain
9 * against. There is very little to them aside from hashing them and
10 * parking tasks using given ID's on a list.
12 * The hash is always changed with the tasklist_lock write-acquired,
13 * and the hash is only accessed with the tasklist_lock at least
14 * read-acquired, so there's no additional SMP locking needed here.
16 * We have a list of bitmap pages, which bitmaps represent the PID space.
17 * Allocating and freeing PIDs is completely lockless. The worst-case
18 * allocation scenario when all but one out of 1 million PIDs possible are
19 * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
20 * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/bootmem.h>
28 #include <linux/hash.h>
29 #include <linux/vs_cvirt.h>
31 #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
32 static struct hlist_head *pid_hash[PIDTYPE_MAX];
33 static int pidhash_shift;
35 int pid_max = PID_MAX_DEFAULT;
38 #define RESERVED_PIDS 300
40 int pid_max_min = RESERVED_PIDS + 1;
41 int pid_max_max = PID_MAX_LIMIT;
43 #define PIDMAP_ENTRIES ((PID_MAX_LIMIT + 8*PAGE_SIZE - 1)/PAGE_SIZE/8)
44 #define BITS_PER_PAGE (PAGE_SIZE*8)
45 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
46 #define mk_pid(map, off) (((map) - pidmap_array)*BITS_PER_PAGE + (off))
47 #define find_next_offset(map, off) \
48 find_next_zero_bit((map)->page, BITS_PER_PAGE, off)
51 * PID-map pages start out as NULL, they get allocated upon
52 * first use and are never deallocated. This way a low pid_max
53 * value does not cause lots of bitmaps to be allocated, but
54 * the scheme scales to up to 4 million PIDs, runtime.
56 typedef struct pidmap {
61 static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
62 { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
64 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
66 fastcall void free_pidmap(int pid)
68 pidmap_t *map = pidmap_array + pid / BITS_PER_PAGE;
69 int offset = pid & BITS_PER_PAGE_MASK;
71 clear_bit(offset, map->page);
72 atomic_inc(&map->nr_free);
75 int alloc_pidmap(void)
77 int i, offset, max_scan, pid, last = last_pid;
83 offset = pid & BITS_PER_PAGE_MASK;
84 map = &pidmap_array[pid/BITS_PER_PAGE];
85 max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
86 for (i = 0; i <= max_scan; ++i) {
87 if (unlikely(!map->page)) {
88 unsigned long page = get_zeroed_page(GFP_KERNEL);
90 * Free the page if someone raced with us
93 spin_lock(&pidmap_lock);
97 map->page = (void *)page;
98 spin_unlock(&pidmap_lock);
99 if (unlikely(!map->page))
102 if (likely(atomic_read(&map->nr_free))) {
104 if (!test_and_set_bit(offset, map->page)) {
105 atomic_dec(&map->nr_free);
109 offset = find_next_offset(map, offset);
110 pid = mk_pid(map, offset);
112 * find_next_offset() found a bit, the pid from it
113 * is in-bounds, and if we fell back to the last
114 * bitmap block and the final block was the same
115 * as the starting point, pid is before last_pid.
117 } while (offset < BITS_PER_PAGE && pid < pid_max &&
118 (i != max_scan || pid < last ||
119 !((last+1) & BITS_PER_PAGE_MASK)));
121 if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
125 map = &pidmap_array[0];
126 offset = RESERVED_PIDS;
127 if (unlikely(last == offset))
130 pid = mk_pid(map, offset);
135 struct pid * fastcall find_pid(enum pid_type type, int nr)
137 struct hlist_node *elem;
140 hlist_for_each_entry_rcu(pid, elem,
141 &pid_hash[type][pid_hashfn(nr)], pid_chain) {
148 int fastcall attach_pid(task_t *task, enum pid_type type, int nr)
150 struct pid *pid, *task_pid;
152 task_pid = &task->pids[type];
153 pid = find_pid(type, nr);
156 INIT_LIST_HEAD(&task_pid->pid_list);
157 hlist_add_head_rcu(&task_pid->pid_chain,
158 &pid_hash[type][pid_hashfn(nr)]);
160 INIT_HLIST_NODE(&task_pid->pid_chain);
161 list_add_tail_rcu(&task_pid->pid_list, &pid->pid_list);
167 static fastcall int __detach_pid(task_t *task, enum pid_type type)
169 struct pid *pid, *pid_next;
172 pid = &task->pids[type];
173 if (!hlist_unhashed(&pid->pid_chain)) {
175 if (list_empty(&pid->pid_list)) {
177 hlist_del_rcu(&pid->pid_chain);
179 pid_next = list_entry(pid->pid_list.next,
180 struct pid, pid_list);
181 /* insert next pid from pid_list to hash */
182 hlist_replace_rcu(&pid->pid_chain,
183 &pid_next->pid_chain);
187 list_del_rcu(&pid->pid_list);
193 void fastcall detach_pid(task_t *task, enum pid_type type)
197 nr = __detach_pid(task, type);
201 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
202 if (tmp != type && find_pid(tmp, nr))
208 task_t *find_task_by_pid_type(int type, int nr)
212 if (type == PIDTYPE_REALPID)
214 else if (type == PIDTYPE_PID)
215 nr = vx_rmap_pid(nr);
217 pid = find_pid(type, nr);
221 return pid_task(&pid->pid_list, type);
224 EXPORT_SYMBOL(find_task_by_pid_type);
227 * This function switches the PIDs if a non-leader thread calls
228 * sys_execve() - this must be done without releasing the PID.
229 * (which a detach_pid() would eventually do.)
231 void switch_exec_pids(task_t *leader, task_t *thread)
233 __detach_pid(leader, PIDTYPE_PID);
234 __detach_pid(leader, PIDTYPE_TGID);
235 __detach_pid(leader, PIDTYPE_PGID);
236 __detach_pid(leader, PIDTYPE_SID);
238 __detach_pid(thread, PIDTYPE_PID);
239 __detach_pid(thread, PIDTYPE_TGID);
241 leader->pid = leader->tgid = thread->pid;
242 thread->pid = thread->tgid;
244 attach_pid(thread, PIDTYPE_PID, thread->pid);
245 attach_pid(thread, PIDTYPE_TGID, thread->tgid);
246 attach_pid(thread, PIDTYPE_PGID, thread->signal->pgrp);
247 attach_pid(thread, PIDTYPE_SID, thread->signal->session);
248 list_add_tail(&thread->tasks, &init_task.tasks);
250 attach_pid(leader, PIDTYPE_PID, leader->pid);
251 attach_pid(leader, PIDTYPE_TGID, leader->tgid);
252 attach_pid(leader, PIDTYPE_PGID, leader->signal->pgrp);
253 attach_pid(leader, PIDTYPE_SID, leader->signal->session);
257 * The pid hash table is scaled according to the amount of memory in the
258 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
261 void __init pidhash_init(void)
263 int i, j, pidhash_size;
264 unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
266 pidhash_shift = max(4, fls(megabytes * 4));
267 pidhash_shift = min(12, pidhash_shift);
268 pidhash_size = 1 << pidhash_shift;
270 printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
271 pidhash_size, pidhash_shift,
272 PIDTYPE_MAX * pidhash_size * sizeof(struct hlist_head));
274 for (i = 0; i < PIDTYPE_MAX; i++) {
275 pid_hash[i] = alloc_bootmem(pidhash_size *
276 sizeof(*(pid_hash[i])));
278 panic("Could not alloc pidhash!\n");
279 for (j = 0; j < pidhash_size; j++)
280 INIT_HLIST_HEAD(&pid_hash[i][j]);
284 void __init pidmap_init(void)
288 pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
289 set_bit(0, pidmap_array->page);
290 atomic_dec(&pidmap_array->nr_free);
293 * Allocate PID 0, and hash it via all PID types:
296 for (i = 0; i < PIDTYPE_MAX; i++)
297 attach_pid(current, i, 0);