[PATCH] Move pidmap to pspace.h
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / pid.c
CommitLineData
1da177e4
LT
1/*
2 * Generic pidhash and scalable, time-bounded PID allocator
3 *
4 * (C) 2002-2003 William Irwin, IBM
5 * (C) 2004 William Irwin, Oracle
6 * (C) 2002-2004 Ingo Molnar, Red Hat
7 *
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.
11 *
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.
15 *
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).
21 */
22
23#include <linux/mm.h>
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>
aa5a6662 29#include <linux/pspace.h>
1da177e4
LT
30
31#define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
92476d7f 32static struct hlist_head *pid_hash;
1da177e4 33static int pidhash_shift;
92476d7f 34static kmem_cache_t *pid_cachep;
1da177e4
LT
35
36int pid_max = PID_MAX_DEFAULT;
37int last_pid;
38
39#define RESERVED_PIDS 300
40
41int pid_max_min = RESERVED_PIDS + 1;
42int pid_max_max = PID_MAX_LIMIT;
43
1da177e4
LT
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)
49
50/*
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.
55 */
6a1f3b84 56static struct pidmap pidmap_array[PIDMAP_ENTRIES] =
1da177e4
LT
57 { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
58
92476d7f
EB
59/*
60 * Note: disable interrupts while the pidmap_lock is held as an
61 * interrupt might come in and do read_lock(&tasklist_lock).
62 *
63 * If we don't disable interrupts there is a nasty deadlock between
64 * detach_pid()->free_pid() and another cpu that does
65 * spin_lock(&pidmap_lock) followed by an interrupt routine that does
66 * read_lock(&tasklist_lock);
67 *
68 * After we clean up the tasklist_lock and know there are no
69 * irq handlers that take it we can leave the interrupts enabled.
70 * For now it is easier to be safe than to prove it can't happen.
71 */
1da177e4
LT
72static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
73
92476d7f 74static fastcall void free_pidmap(int pid)
1da177e4 75{
6a1f3b84 76 struct pidmap *map = pidmap_array + pid / BITS_PER_PAGE;
1da177e4
LT
77 int offset = pid & BITS_PER_PAGE_MASK;
78
79 clear_bit(offset, map->page);
80 atomic_inc(&map->nr_free);
81}
82
92476d7f 83static int alloc_pidmap(void)
1da177e4
LT
84{
85 int i, offset, max_scan, pid, last = last_pid;
6a1f3b84 86 struct pidmap *map;
1da177e4
LT
87
88 pid = last + 1;
89 if (pid >= pid_max)
90 pid = RESERVED_PIDS;
91 offset = pid & BITS_PER_PAGE_MASK;
92 map = &pidmap_array[pid/BITS_PER_PAGE];
93 max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
94 for (i = 0; i <= max_scan; ++i) {
95 if (unlikely(!map->page)) {
96 unsigned long page = get_zeroed_page(GFP_KERNEL);
97 /*
98 * Free the page if someone raced with us
99 * installing it:
100 */
92476d7f 101 spin_lock_irq(&pidmap_lock);
1da177e4
LT
102 if (map->page)
103 free_page(page);
104 else
105 map->page = (void *)page;
92476d7f 106 spin_unlock_irq(&pidmap_lock);
1da177e4
LT
107 if (unlikely(!map->page))
108 break;
109 }
110 if (likely(atomic_read(&map->nr_free))) {
111 do {
112 if (!test_and_set_bit(offset, map->page)) {
113 atomic_dec(&map->nr_free);
114 last_pid = pid;
115 return pid;
116 }
117 offset = find_next_offset(map, offset);
118 pid = mk_pid(map, offset);
119 /*
120 * find_next_offset() found a bit, the pid from it
121 * is in-bounds, and if we fell back to the last
122 * bitmap block and the final block was the same
123 * as the starting point, pid is before last_pid.
124 */
125 } while (offset < BITS_PER_PAGE && pid < pid_max &&
126 (i != max_scan || pid < last ||
127 !((last+1) & BITS_PER_PAGE_MASK)));
128 }
129 if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
130 ++map;
131 offset = 0;
132 } else {
133 map = &pidmap_array[0];
134 offset = RESERVED_PIDS;
135 if (unlikely(last == offset))
136 break;
137 }
138 pid = mk_pid(map, offset);
139 }
140 return -1;
141}
142
0804ef4b
EB
143static int next_pidmap(int last)
144{
145 int offset;
c88be3eb 146 struct pidmap *map;
0804ef4b
EB
147
148 offset = (last + 1) & BITS_PER_PAGE_MASK;
149 map = &pidmap_array[(last + 1)/BITS_PER_PAGE];
150 for (; map < &pidmap_array[PIDMAP_ENTRIES]; map++, offset = 0) {
151 if (unlikely(!map->page))
152 continue;
153 offset = find_next_bit((map)->page, BITS_PER_PAGE, offset);
154 if (offset < BITS_PER_PAGE)
155 return mk_pid(map, offset);
156 }
157 return -1;
158}
159
92476d7f
EB
160fastcall void put_pid(struct pid *pid)
161{
162 if (!pid)
163 return;
164 if ((atomic_read(&pid->count) == 1) ||
165 atomic_dec_and_test(&pid->count))
166 kmem_cache_free(pid_cachep, pid);
167}
bbf73147 168EXPORT_SYMBOL_GPL(put_pid);
92476d7f
EB
169
170static void delayed_put_pid(struct rcu_head *rhp)
171{
172 struct pid *pid = container_of(rhp, struct pid, rcu);
173 put_pid(pid);
174}
175
176fastcall void free_pid(struct pid *pid)
177{
178 /* We can be called with write_lock_irq(&tasklist_lock) held */
179 unsigned long flags;
180
181 spin_lock_irqsave(&pidmap_lock, flags);
182 hlist_del_rcu(&pid->pid_chain);
183 spin_unlock_irqrestore(&pidmap_lock, flags);
184
185 free_pidmap(pid->nr);
186 call_rcu(&pid->rcu, delayed_put_pid);
187}
188
189struct pid *alloc_pid(void)
190{
191 struct pid *pid;
192 enum pid_type type;
193 int nr = -1;
194
195 pid = kmem_cache_alloc(pid_cachep, GFP_KERNEL);
196 if (!pid)
197 goto out;
198
199 nr = alloc_pidmap();
200 if (nr < 0)
201 goto out_free;
202
203 atomic_set(&pid->count, 1);
204 pid->nr = nr;
205 for (type = 0; type < PIDTYPE_MAX; ++type)
206 INIT_HLIST_HEAD(&pid->tasks[type]);
207
208 spin_lock_irq(&pidmap_lock);
209 hlist_add_head_rcu(&pid->pid_chain, &pid_hash[pid_hashfn(pid->nr)]);
210 spin_unlock_irq(&pidmap_lock);
211
212out:
213 return pid;
214
215out_free:
216 kmem_cache_free(pid_cachep, pid);
217 pid = NULL;
218 goto out;
219}
220
221struct pid * fastcall find_pid(int nr)
1da177e4
LT
222{
223 struct hlist_node *elem;
224 struct pid *pid;
225
e56d0903 226 hlist_for_each_entry_rcu(pid, elem,
92476d7f 227 &pid_hash[pid_hashfn(nr)], pid_chain) {
1da177e4
LT
228 if (pid->nr == nr)
229 return pid;
230 }
231 return NULL;
232}
bbf73147 233EXPORT_SYMBOL_GPL(find_pid);
1da177e4 234
36c8b586 235int fastcall attach_pid(struct task_struct *task, enum pid_type type, int nr)
1da177e4 236{
92476d7f
EB
237 struct pid_link *link;
238 struct pid *pid;
239
92476d7f
EB
240 link = &task->pids[type];
241 link->pid = pid = find_pid(nr);
242 hlist_add_head_rcu(&link->node, &pid->tasks[type]);
1da177e4
LT
243
244 return 0;
245}
246
36c8b586 247void fastcall detach_pid(struct task_struct *task, enum pid_type type)
1da177e4 248{
92476d7f
EB
249 struct pid_link *link;
250 struct pid *pid;
251 int tmp;
1da177e4 252
92476d7f
EB
253 link = &task->pids[type];
254 pid = link->pid;
1da177e4 255
92476d7f
EB
256 hlist_del_rcu(&link->node);
257 link->pid = NULL;
1da177e4 258
92476d7f
EB
259 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
260 if (!hlist_empty(&pid->tasks[tmp]))
261 return;
1da177e4 262
92476d7f 263 free_pid(pid);
1da177e4
LT
264}
265
c18258c6
EB
266/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
267void fastcall transfer_pid(struct task_struct *old, struct task_struct *new,
268 enum pid_type type)
269{
270 new->pids[type].pid = old->pids[type].pid;
271 hlist_replace_rcu(&old->pids[type].node, &new->pids[type].node);
272 old->pids[type].pid = NULL;
273}
274
92476d7f 275struct task_struct * fastcall pid_task(struct pid *pid, enum pid_type type)
1da177e4 276{
92476d7f
EB
277 struct task_struct *result = NULL;
278 if (pid) {
279 struct hlist_node *first;
280 first = rcu_dereference(pid->tasks[type].first);
281 if (first)
282 result = hlist_entry(first, struct task_struct, pids[(type)].node);
283 }
284 return result;
285}
1da177e4 286
92476d7f
EB
287/*
288 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
289 */
36c8b586 290struct task_struct *find_task_by_pid_type(int type, int nr)
92476d7f
EB
291{
292 return pid_task(find_pid(nr), type);
293}
1da177e4 294
92476d7f 295EXPORT_SYMBOL(find_task_by_pid_type);
1da177e4 296
92476d7f
EB
297struct task_struct *fastcall get_pid_task(struct pid *pid, enum pid_type type)
298{
299 struct task_struct *result;
300 rcu_read_lock();
301 result = pid_task(pid, type);
302 if (result)
303 get_task_struct(result);
304 rcu_read_unlock();
305 return result;
1da177e4
LT
306}
307
92476d7f 308struct pid *find_get_pid(pid_t nr)
1da177e4
LT
309{
310 struct pid *pid;
311
92476d7f
EB
312 rcu_read_lock();
313 pid = get_pid(find_pid(nr));
314 rcu_read_unlock();
1da177e4 315
92476d7f 316 return pid;
1da177e4
LT
317}
318
0804ef4b
EB
319/*
320 * Used by proc to find the first pid that is greater then or equal to nr.
321 *
322 * If there is a pid at nr this function is exactly the same as find_pid.
323 */
324struct pid *find_ge_pid(int nr)
325{
326 struct pid *pid;
327
328 do {
329 pid = find_pid(nr);
330 if (pid)
331 break;
332 nr = next_pidmap(nr);
333 } while (nr > 0);
334
335 return pid;
336}
bbf73147 337EXPORT_SYMBOL_GPL(find_get_pid);
0804ef4b 338
1da177e4
LT
339/*
340 * The pid hash table is scaled according to the amount of memory in the
341 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
342 * more.
343 */
344void __init pidhash_init(void)
345{
92476d7f 346 int i, pidhash_size;
1da177e4
LT
347 unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
348
349 pidhash_shift = max(4, fls(megabytes * 4));
350 pidhash_shift = min(12, pidhash_shift);
351 pidhash_size = 1 << pidhash_shift;
352
353 printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
354 pidhash_size, pidhash_shift,
92476d7f
EB
355 pidhash_size * sizeof(struct hlist_head));
356
357 pid_hash = alloc_bootmem(pidhash_size * sizeof(*(pid_hash)));
358 if (!pid_hash)
359 panic("Could not alloc pidhash!\n");
360 for (i = 0; i < pidhash_size; i++)
361 INIT_HLIST_HEAD(&pid_hash[i]);
1da177e4
LT
362}
363
364void __init pidmap_init(void)
365{
1da177e4 366 pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
73b9ebfe 367 /* Reserve PID 0. We never call free_pidmap(0) */
1da177e4
LT
368 set_bit(0, pidmap_array->page);
369 atomic_dec(&pidmap_array->nr_free);
92476d7f
EB
370
371 pid_cachep = kmem_cache_create("pid", sizeof(struct pid),
372 __alignof__(struct pid),
373 SLAB_PANIC, NULL, NULL);
1da177e4 374}