Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / kernel / pid.c
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>
29 #include <linux/vs_cvirt.h>
30
31 #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
32 static struct hlist_head *pid_hash;
33 static int pidhash_shift;
34 static kmem_cache_t *pid_cachep;
35
36 int pid_max = PID_MAX_DEFAULT;
37 int last_pid;
38
39 #define RESERVED_PIDS           300
40
41 int pid_max_min = RESERVED_PIDS + 1;
42 int pid_max_max = PID_MAX_LIMIT;
43
44 #define PIDMAP_ENTRIES          ((PID_MAX_LIMIT + 8*PAGE_SIZE - 1)/PAGE_SIZE/8)
45 #define BITS_PER_PAGE           (PAGE_SIZE*8)
46 #define BITS_PER_PAGE_MASK      (BITS_PER_PAGE-1)
47 #define mk_pid(map, off)        (((map) - pidmap_array)*BITS_PER_PAGE + (off))
48 #define find_next_offset(map, off)                                      \
49                 find_next_zero_bit((map)->page, BITS_PER_PAGE, off)
50
51 /*
52  * PID-map pages start out as NULL, they get allocated upon
53  * first use and are never deallocated. This way a low pid_max
54  * value does not cause lots of bitmaps to be allocated, but
55  * the scheme scales to up to 4 million PIDs, runtime.
56  */
57 typedef struct pidmap {
58         atomic_t nr_free;
59         void *page;
60 } pidmap_t;
61
62 static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
63          { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
64
65 /*
66  * Note: disable interrupts while the pidmap_lock is held as an
67  * interrupt might come in and do read_lock(&tasklist_lock).
68  *
69  * If we don't disable interrupts there is a nasty deadlock between
70  * detach_pid()->free_pid() and another cpu that does
71  * spin_lock(&pidmap_lock) followed by an interrupt routine that does
72  * read_lock(&tasklist_lock);
73  *
74  * After we clean up the tasklist_lock and know there are no
75  * irq handlers that take it we can leave the interrupts enabled.
76  * For now it is easier to be safe than to prove it can't happen.
77  */
78 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
79
80 static fastcall void free_pidmap(int pid)
81 {
82         pidmap_t *map = pidmap_array + pid / BITS_PER_PAGE;
83         int offset = pid & BITS_PER_PAGE_MASK;
84
85         clear_bit(offset, map->page);
86         atomic_inc(&map->nr_free);
87 }
88
89 static int alloc_pidmap(void)
90 {
91         int i, offset, max_scan, pid, last = last_pid;
92         pidmap_t *map;
93
94         pid = last + 1;
95         if (pid >= pid_max)
96                 pid = RESERVED_PIDS;
97         offset = pid & BITS_PER_PAGE_MASK;
98         map = &pidmap_array[pid/BITS_PER_PAGE];
99         max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
100         for (i = 0; i <= max_scan; ++i) {
101                 if (unlikely(!map->page)) {
102                         unsigned long page = get_zeroed_page(GFP_KERNEL);
103                         /*
104                          * Free the page if someone raced with us
105                          * installing it:
106                          */
107                         spin_lock_irq(&pidmap_lock);
108                         if (map->page)
109                                 free_page(page);
110                         else
111                                 map->page = (void *)page;
112                         spin_unlock_irq(&pidmap_lock);
113                         if (unlikely(!map->page))
114                                 break;
115                 }
116                 if (likely(atomic_read(&map->nr_free))) {
117                         do {
118                                 if (!test_and_set_bit(offset, map->page)) {
119                                         atomic_dec(&map->nr_free);
120                                         last_pid = pid;
121                                         return pid;
122                                 }
123                                 offset = find_next_offset(map, offset);
124                                 pid = mk_pid(map, offset);
125                         /*
126                          * find_next_offset() found a bit, the pid from it
127                          * is in-bounds, and if we fell back to the last
128                          * bitmap block and the final block was the same
129                          * as the starting point, pid is before last_pid.
130                          */
131                         } while (offset < BITS_PER_PAGE && pid < pid_max &&
132                                         (i != max_scan || pid < last ||
133                                             !((last+1) & BITS_PER_PAGE_MASK)));
134                 }
135                 if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
136                         ++map;
137                         offset = 0;
138                 } else {
139                         map = &pidmap_array[0];
140                         offset = RESERVED_PIDS;
141                         if (unlikely(last == offset))
142                                 break;
143                 }
144                 pid = mk_pid(map, offset);
145         }
146         return -1;
147 }
148
149 fastcall void put_pid(struct pid *pid)
150 {
151         if (!pid)
152                 return;
153         if ((atomic_read(&pid->count) == 1) ||
154              atomic_dec_and_test(&pid->count))
155                 kmem_cache_free(pid_cachep, pid);
156 }
157
158 static void delayed_put_pid(struct rcu_head *rhp)
159 {
160         struct pid *pid = container_of(rhp, struct pid, rcu);
161         put_pid(pid);
162 }
163
164 fastcall void free_pid(struct pid *pid)
165 {
166         /* We can be called with write_lock_irq(&tasklist_lock) held */
167         unsigned long flags;
168
169         spin_lock_irqsave(&pidmap_lock, flags);
170         hlist_del_rcu(&pid->pid_chain);
171         spin_unlock_irqrestore(&pidmap_lock, flags);
172
173         free_pidmap(pid->nr);
174         call_rcu(&pid->rcu, delayed_put_pid);
175 }
176
177 struct pid *alloc_pid(void)
178 {
179         struct pid *pid;
180         enum pid_type type;
181         int nr = -1;
182
183         pid = kmem_cache_alloc(pid_cachep, GFP_KERNEL);
184         if (!pid)
185                 goto out;
186
187         nr = alloc_pidmap();
188         if (nr < 0)
189                 goto out_free;
190
191         atomic_set(&pid->count, 1);
192         pid->nr = nr;
193         for (type = 0; type < PIDTYPE_MAX; ++type)
194                 INIT_HLIST_HEAD(&pid->tasks[type]);
195
196         spin_lock_irq(&pidmap_lock);
197         hlist_add_head_rcu(&pid->pid_chain, &pid_hash[pid_hashfn(pid->nr)]);
198         spin_unlock_irq(&pidmap_lock);
199
200 out:
201         return pid;
202
203 out_free:
204         kmem_cache_free(pid_cachep, pid);
205         pid = NULL;
206         goto out;
207 }
208
209 struct pid * fastcall find_pid(int nr)
210 {
211         struct hlist_node *elem;
212         struct pid *pid;
213
214         hlist_for_each_entry_rcu(pid, elem,
215                         &pid_hash[pid_hashfn(nr)], pid_chain) {
216                 if (pid->nr == nr)
217                         return pid;
218         }
219         return NULL;
220 }
221
222 int fastcall attach_pid(struct task_struct *task, enum pid_type type, int nr)
223 {
224         struct pid_link *link;
225         struct pid *pid;
226
227         WARN_ON(!task->pid); /* to be removed soon */
228         WARN_ON(!nr); /* to be removed soon */
229
230         link = &task->pids[type];
231         link->pid = pid = find_pid(nr);
232         hlist_add_head_rcu(&link->node, &pid->tasks[type]);
233
234         return 0;
235 }
236
237 void fastcall detach_pid(struct task_struct *task, enum pid_type type)
238 {
239         struct pid_link *link;
240         struct pid *pid;
241         int tmp;
242
243         link = &task->pids[type];
244         pid = link->pid;
245
246         hlist_del_rcu(&link->node);
247         link->pid = NULL;
248
249         for (tmp = PIDTYPE_MAX; --tmp >= 0; )
250                 if (!hlist_empty(&pid->tasks[tmp]))
251                         return;
252
253         free_pid(pid);
254 }
255
256 struct task_struct * fastcall pid_task(struct pid *pid, enum pid_type type)
257 {
258         struct task_struct *result = NULL;
259         if (pid) {
260                 struct hlist_node *first;
261                 first = rcu_dereference(pid->tasks[type].first);
262                 if (first)
263                         result = hlist_entry(first, struct task_struct, pids[(type)].node);
264                 if (result && (pid->nr != 1) &&
265                         !vx_check(vx_task_xid(result), VX_WATCH|VX_ADMIN|VX_IDENT)) {
266                         vxwprintk((type == PIDTYPE_PID) && (current->xid),
267                                 "pid_task(%d,%d): task %p[#%u,%u] did lookup %p[#%u,%u]",
268                                 pid->nr, type, current, vx_current_xid(), current->pid,
269                                 result, vx_task_xid(result), result->pid);
270                         result = NULL;
271                 }
272         }
273         return result;
274 }
275
276 /*
277  * Must be called under rcu_read_lock() or with tasklist_lock read-held.
278  */
279 struct task_struct *find_task_by_pid_type(int type, int nr)
280 {
281         if (type == PIDTYPE_PID)
282                 nr = vx_rmap_pid(nr);
283         else if (type == PIDTYPE_REALPID)
284                 type = PIDTYPE_PID;
285         return pid_task(find_pid(nr), type);
286 }
287
288 EXPORT_SYMBOL(find_task_by_pid_type);
289
290 struct task_struct *fastcall get_pid_task(struct pid *pid, enum pid_type type)
291 {
292         struct task_struct *result;
293         rcu_read_lock();
294         result = pid_task(pid, type);
295         if (result)
296                 get_task_struct(result);
297         rcu_read_unlock();
298         return result;
299 }
300
301 struct pid *find_get_pid(pid_t nr)
302 {
303         struct pid *pid;
304
305         rcu_read_lock();
306         pid = get_pid(find_pid(nr));
307         rcu_read_unlock();
308
309         return pid;
310 }
311
312 /*
313  * The pid hash table is scaled according to the amount of memory in the
314  * machine.  From a minimum of 16 slots up to 4096 slots at one gigabyte or
315  * more.
316  */
317 void __init pidhash_init(void)
318 {
319         int i, pidhash_size;
320         unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
321
322         pidhash_shift = max(4, fls(megabytes * 4));
323         pidhash_shift = min(12, pidhash_shift);
324         pidhash_size = 1 << pidhash_shift;
325
326         printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
327                 pidhash_size, pidhash_shift,
328                 pidhash_size * sizeof(struct hlist_head));
329
330         pid_hash = alloc_bootmem(pidhash_size * sizeof(*(pid_hash)));
331         if (!pid_hash)
332                 panic("Could not alloc pidhash!\n");
333         for (i = 0; i < pidhash_size; i++)
334                 INIT_HLIST_HEAD(&pid_hash[i]);
335 }
336
337 void __init pidmap_init(void)
338 {
339         pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
340         /* Reserve PID 0. We never call free_pidmap(0) */
341         set_bit(0, pidmap_array->page);
342         atomic_dec(&pidmap_array->nr_free);
343
344         pid_cachep = kmem_cache_create("pid", sizeof(struct pid),
345                                         __alignof__(struct pid),
346                                         SLAB_PANIC, NULL, NULL);
347 }