6ed44f56ca456f91d2e1a941043a8628091d8f98
[linux-2.6.git] / kernel / pid.c
1 /*
2  * Generic pidhash and scalable, time-bounded PID allocator
3  *
4  * (C) 2002 William Irwin, IBM
5  * (C) 2002 Ingo Molnar, Red Hat
6  *
7  * pid-structures are backing objects for tasks sharing a given ID to chain
8  * against. There is very little to them aside from hashing them and
9  * parking tasks using given ID's on a list.
10  *
11  * The hash is always changed with the tasklist_lock write-acquired,
12  * and the hash is only accessed with the tasklist_lock at least
13  * read-acquired, so there's no additional SMP locking needed here.
14  *
15  * We have a list of bitmap pages, which bitmaps represent the PID space.
16  * Allocating and freeing PIDs is completely lockless. The worst-case
17  * allocation scenario when all but one out of 1 million PIDs possible are
18  * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
19  * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
20  */
21
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/bootmem.h>
27 #include <linux/hash.h>
28
29 #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
30 static struct list_head *pid_hash[PIDTYPE_MAX];
31 static int pidhash_shift;
32
33 int pid_max = PID_MAX_DEFAULT;
34 int last_pid;
35
36 #define RESERVED_PIDS           300
37
38 #define PIDMAP_ENTRIES          (PID_MAX_LIMIT/PAGE_SIZE/8)
39 #define BITS_PER_PAGE           (PAGE_SIZE*8)
40 #define BITS_PER_PAGE_MASK      (BITS_PER_PAGE-1)
41
42 /*
43  * PID-map pages start out as NULL, they get allocated upon
44  * first use and are never deallocated. This way a low pid_max
45  * value does not cause lots of bitmaps to be allocated, but
46  * the scheme scales to up to 4 million PIDs, runtime.
47  */
48 typedef struct pidmap {
49         atomic_t nr_free;
50         void *page;
51 } pidmap_t;
52
53 static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
54          { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
55
56 static pidmap_t *map_limit = pidmap_array + PIDMAP_ENTRIES;
57
58 static spinlock_t pidmap_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
59
60 fastcall void free_pidmap(int pid)
61 {
62         pidmap_t *map = pidmap_array + pid / BITS_PER_PAGE;
63         int offset = pid & BITS_PER_PAGE_MASK;
64
65         clear_bit(offset, map->page);
66         atomic_inc(&map->nr_free);
67 }
68
69 /*
70  * Here we search for the next map that has free bits left.
71  * Normally the next map has free PIDs.
72  */
73 static inline pidmap_t *next_free_map(pidmap_t *map, int *max_steps)
74 {
75         while (--*max_steps) {
76                 if (++map == map_limit)
77                         map = pidmap_array;
78                 if (unlikely(!map->page)) {
79                         unsigned long page = get_zeroed_page(GFP_KERNEL);
80                         /*
81                          * Free the page if someone raced with us
82                          * installing it:
83                          */
84                         spin_lock(&pidmap_lock);
85                         if (map->page)
86                                 free_page(page);
87                         else
88                                 map->page = (void *)page;
89                         spin_unlock(&pidmap_lock);
90
91                         if (!map->page)
92                                 break;
93                 }
94                 if (atomic_read(&map->nr_free))
95                         return map;
96         }
97         return NULL;
98 }
99
100 int alloc_pidmap(void)
101 {
102         int pid, offset, max_steps = PIDMAP_ENTRIES + 1;
103         pidmap_t *map;
104
105         pid = last_pid + 1;
106         if (pid >= pid_max)
107                 pid = RESERVED_PIDS;
108
109         offset = pid & BITS_PER_PAGE_MASK;
110         map = pidmap_array + pid / BITS_PER_PAGE;
111
112         if (likely(map->page && !test_and_set_bit(offset, map->page))) {
113                 /*
114                  * There is a small window for last_pid updates to race,
115                  * but in that case the next allocation will go into the
116                  * slowpath and that fixes things up.
117                  */
118 return_pid:
119                 atomic_dec(&map->nr_free);
120                 last_pid = pid;
121                 return pid;
122         }
123         
124         if (!offset || !atomic_read(&map->nr_free)) {
125 next_map:
126                 map = next_free_map(map, &max_steps);
127                 if (!map)
128                         goto failure;
129                 offset = 0;
130         }
131         /*
132          * Find the next zero bit:
133          */
134 scan_more:
135         offset = find_next_zero_bit(map->page, BITS_PER_PAGE, offset);
136         if (offset >= BITS_PER_PAGE)
137                 goto next_map;
138         if (test_and_set_bit(offset, map->page))
139                 goto scan_more;
140
141         /* we got the PID: */
142         pid = (map - pidmap_array) * BITS_PER_PAGE + offset;
143         goto return_pid;
144
145 failure:
146         return -1;
147 }
148
149 fastcall struct pid *find_pid(enum pid_type type, int nr)
150 {
151         struct list_head *elem, *bucket = &pid_hash[type][pid_hashfn(nr)];
152         struct pid *pid;
153
154         __list_for_each(elem, bucket) {
155                 pid = list_entry(elem, struct pid, hash_chain);
156                 if (pid->nr == nr)
157                         return pid;
158         }
159         return NULL;
160 }
161
162 void fastcall link_pid(task_t *task, struct pid_link *link, struct pid *pid)
163 {
164         atomic_inc(&pid->count);
165         list_add_tail(&link->pid_chain, &pid->task_list);
166         link->pidptr = pid;
167 }
168
169 int fastcall attach_pid(task_t *task, enum pid_type type, int nr)
170 {
171         struct pid *pid = find_pid(type, nr);
172
173         if (pid)
174                 atomic_inc(&pid->count);
175         else {
176                 pid = &task->pids[type].pid;
177                 pid->nr = nr;
178                 atomic_set(&pid->count, 1);
179                 INIT_LIST_HEAD(&pid->task_list);
180                 pid->task = task;
181                 get_task_struct(task);
182                 list_add(&pid->hash_chain, &pid_hash[type][pid_hashfn(nr)]);
183         }
184         list_add_tail(&task->pids[type].pid_chain, &pid->task_list);
185         task->pids[type].pidptr = pid;
186
187         return 0;
188 }
189
190 static inline int __detach_pid(task_t *task, enum pid_type type)
191 {
192         struct pid_link *link = task->pids + type;
193         struct pid *pid = link->pidptr;
194         int nr;
195
196         list_del(&link->pid_chain);
197         if (!atomic_dec_and_test(&pid->count))
198                 return 0;
199
200         nr = pid->nr;
201         list_del(&pid->hash_chain);
202         put_task_struct(pid->task);
203
204         return nr;
205 }
206
207 static void _detach_pid(task_t *task, enum pid_type type)
208 {
209         __detach_pid(task, type);
210 }
211
212 void fastcall detach_pid(task_t *task, enum pid_type type)
213 {
214         int nr = __detach_pid(task, type);
215
216         if (!nr)
217                 return;
218
219         for (type = 0; type < PIDTYPE_MAX; ++type)
220                 if (find_pid(type, nr))
221                         return;
222         free_pidmap(nr);
223 }
224
225 task_t *find_task_by_pid(int nr)
226 {
227         struct pid *pid = find_pid(PIDTYPE_PID, nr);
228
229         if (!pid)
230                 return NULL;
231         return pid_task(pid->task_list.next, PIDTYPE_PID);
232 }
233
234 EXPORT_SYMBOL(find_task_by_pid);
235
236 /*
237  * This function switches the PIDs if a non-leader thread calls
238  * sys_execve() - this must be done without releasing the PID.
239  * (which a detach_pid() would eventually do.)
240  */
241 void switch_exec_pids(task_t *leader, task_t *thread)
242 {
243         _detach_pid(leader, PIDTYPE_PID);
244         _detach_pid(leader, PIDTYPE_TGID);
245         _detach_pid(leader, PIDTYPE_PGID);
246         _detach_pid(leader, PIDTYPE_SID);
247
248         _detach_pid(thread, PIDTYPE_PID);
249         _detach_pid(thread, PIDTYPE_TGID);
250
251         leader->pid = leader->tgid = thread->pid;
252         thread->pid = thread->tgid;
253
254         attach_pid(thread, PIDTYPE_PID, thread->pid);
255         attach_pid(thread, PIDTYPE_TGID, thread->tgid);
256         attach_pid(thread, PIDTYPE_PGID, thread->signal->pgrp);
257         attach_pid(thread, PIDTYPE_SID, thread->signal->session);
258         list_add_tail(&thread->tasks, &init_task.tasks);
259
260         attach_pid(leader, PIDTYPE_PID, leader->pid);
261         attach_pid(leader, PIDTYPE_TGID, leader->tgid);
262         attach_pid(leader, PIDTYPE_PGID, leader->signal->pgrp);
263         attach_pid(leader, PIDTYPE_SID, leader->signal->session);
264 }
265
266 /*
267  * The pid hash table is scaled according to the amount of memory in the
268  * machine.  From a minimum of 16 slots up to 4096 slots at one gigabyte or
269  * more.
270  */
271 void __init pidhash_init(void)
272 {
273         int i, j, pidhash_size;
274         unsigned long megabytes = max_pfn >> (20 - PAGE_SHIFT);
275
276         pidhash_shift = max(4, fls(megabytes * 4));
277         pidhash_shift = min(12, pidhash_shift);
278         pidhash_size = 1 << pidhash_shift;
279
280         printk("PID hash table entries: %d (order %d: %Zd bytes)\n",
281                 pidhash_size, pidhash_shift,
282                 pidhash_size * sizeof(struct list_head));
283
284         for (i = 0; i < PIDTYPE_MAX; i++) {
285                 pid_hash[i] = alloc_bootmem(pidhash_size *
286                                         sizeof(struct list_head));
287                 if (!pid_hash[i])
288                         panic("Could not alloc pidhash!\n");
289                 for (j = 0; j < pidhash_size; j++)
290                         INIT_LIST_HEAD(&pid_hash[i][j]);
291         }
292 }
293
294 void __init pidmap_init(void)
295 {
296         int i;
297
298         pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
299         set_bit(0, pidmap_array->page);
300         atomic_dec(&pidmap_array->nr_free);
301
302         /*
303          * Allocate PID 0, and hash it via all PID types:
304          */
305
306         for (i = 0; i < PIDTYPE_MAX; i++)
307                 attach_pid(current, i, 0);
308 }