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>
30 #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
31 static struct hlist_head
*pid_hash
;
32 static int pidhash_shift
;
33 static kmem_cache_t
*pid_cachep
;
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
} };
65 * Note: disable interrupts while the pidmap_lock is held as an
66 * interrupt might come in and do read_lock(&tasklist_lock).
68 * If we don't disable interrupts there is a nasty deadlock between
69 * detach_pid()->free_pid() and another cpu that does
70 * spin_lock(&pidmap_lock) followed by an interrupt routine that does
71 * read_lock(&tasklist_lock);
73 * After we clean up the tasklist_lock and know there are no
74 * irq handlers that take it we can leave the interrupts enabled.
75 * For now it is easier to be safe than to prove it can't happen.
77 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(pidmap_lock
);
79 static fastcall
void free_pidmap(int pid
)
81 pidmap_t
*map
= pidmap_array
+ pid
/ BITS_PER_PAGE
;
82 int offset
= pid
& BITS_PER_PAGE_MASK
;
84 clear_bit(offset
, map
->page
);
85 atomic_inc(&map
->nr_free
);
88 static int alloc_pidmap(void)
90 int i
, offset
, max_scan
, pid
, last
= last_pid
;
96 offset
= pid
& BITS_PER_PAGE_MASK
;
97 map
= &pidmap_array
[pid
/BITS_PER_PAGE
];
98 max_scan
= (pid_max
+ BITS_PER_PAGE
- 1)/BITS_PER_PAGE
- !offset
;
99 for (i
= 0; i
<= max_scan
; ++i
) {
100 if (unlikely(!map
->page
)) {
101 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
103 * Free the page if someone raced with us
106 spin_lock_irq(&pidmap_lock
);
110 map
->page
= (void *)page
;
111 spin_unlock_irq(&pidmap_lock
);
112 if (unlikely(!map
->page
))
115 if (likely(atomic_read(&map
->nr_free
))) {
117 if (!test_and_set_bit(offset
, map
->page
)) {
118 atomic_dec(&map
->nr_free
);
122 offset
= find_next_offset(map
, offset
);
123 pid
= mk_pid(map
, offset
);
125 * find_next_offset() found a bit, the pid from it
126 * is in-bounds, and if we fell back to the last
127 * bitmap block and the final block was the same
128 * as the starting point, pid is before last_pid.
130 } while (offset
< BITS_PER_PAGE
&& pid
< pid_max
&&
131 (i
!= max_scan
|| pid
< last
||
132 !((last
+1) & BITS_PER_PAGE_MASK
)));
134 if (map
< &pidmap_array
[(pid_max
-1)/BITS_PER_PAGE
]) {
138 map
= &pidmap_array
[0];
139 offset
= RESERVED_PIDS
;
140 if (unlikely(last
== offset
))
143 pid
= mk_pid(map
, offset
);
148 fastcall
void put_pid(struct pid
*pid
)
152 if ((atomic_read(&pid
->count
) == 1) ||
153 atomic_dec_and_test(&pid
->count
))
154 kmem_cache_free(pid_cachep
, pid
);
157 static void delayed_put_pid(struct rcu_head
*rhp
)
159 struct pid
*pid
= container_of(rhp
, struct pid
, rcu
);
163 fastcall
void free_pid(struct pid
*pid
)
165 /* We can be called with write_lock_irq(&tasklist_lock) held */
168 spin_lock_irqsave(&pidmap_lock
, flags
);
169 hlist_del_rcu(&pid
->pid_chain
);
170 spin_unlock_irqrestore(&pidmap_lock
, flags
);
172 free_pidmap(pid
->nr
);
173 call_rcu(&pid
->rcu
, delayed_put_pid
);
176 struct pid
*alloc_pid(void)
182 pid
= kmem_cache_alloc(pid_cachep
, GFP_KERNEL
);
190 atomic_set(&pid
->count
, 1);
192 for (type
= 0; type
< PIDTYPE_MAX
; ++type
)
193 INIT_HLIST_HEAD(&pid
->tasks
[type
]);
195 spin_lock_irq(&pidmap_lock
);
196 hlist_add_head_rcu(&pid
->pid_chain
, &pid_hash
[pid_hashfn(pid
->nr
)]);
197 spin_unlock_irq(&pidmap_lock
);
203 kmem_cache_free(pid_cachep
, pid
);
208 struct pid
* fastcall
find_pid(int nr
)
210 struct hlist_node
*elem
;
213 hlist_for_each_entry_rcu(pid
, elem
,
214 &pid_hash
[pid_hashfn(nr
)], pid_chain
) {
221 int fastcall
attach_pid(struct task_struct
*task
, enum pid_type type
, int nr
)
223 struct pid_link
*link
;
226 link
= &task
->pids
[type
];
227 link
->pid
= pid
= find_pid(nr
);
228 hlist_add_head_rcu(&link
->node
, &pid
->tasks
[type
]);
233 void fastcall
detach_pid(struct task_struct
*task
, enum pid_type type
)
235 struct pid_link
*link
;
239 link
= &task
->pids
[type
];
242 hlist_del_rcu(&link
->node
);
245 for (tmp
= PIDTYPE_MAX
; --tmp
>= 0; )
246 if (!hlist_empty(&pid
->tasks
[tmp
]))
252 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
253 void fastcall
transfer_pid(struct task_struct
*old
, struct task_struct
*new,
256 new->pids
[type
].pid
= old
->pids
[type
].pid
;
257 hlist_replace_rcu(&old
->pids
[type
].node
, &new->pids
[type
].node
);
258 old
->pids
[type
].pid
= NULL
;
261 struct task_struct
* fastcall
pid_task(struct pid
*pid
, enum pid_type type
)
263 struct task_struct
*result
= NULL
;
265 struct hlist_node
*first
;
266 first
= rcu_dereference(pid
->tasks
[type
].first
);
268 result
= hlist_entry(first
, struct task_struct
, pids
[(type
)].node
);
274 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
276 struct task_struct
*find_task_by_pid_type(int type
, int nr
)
278 return pid_task(find_pid(nr
), type
);
281 EXPORT_SYMBOL(find_task_by_pid_type
);
283 struct task_struct
*fastcall
get_pid_task(struct pid
*pid
, enum pid_type type
)
285 struct task_struct
*result
;
287 result
= pid_task(pid
, type
);
289 get_task_struct(result
);
294 struct pid
*find_get_pid(pid_t nr
)
299 pid
= get_pid(find_pid(nr
));
306 * The pid hash table is scaled according to the amount of memory in the
307 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
310 void __init
pidhash_init(void)
313 unsigned long megabytes
= nr_kernel_pages
>> (20 - PAGE_SHIFT
);
315 pidhash_shift
= max(4, fls(megabytes
* 4));
316 pidhash_shift
= min(12, pidhash_shift
);
317 pidhash_size
= 1 << pidhash_shift
;
319 printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
320 pidhash_size
, pidhash_shift
,
321 pidhash_size
* sizeof(struct hlist_head
));
323 pid_hash
= alloc_bootmem(pidhash_size
* sizeof(*(pid_hash
)));
325 panic("Could not alloc pidhash!\n");
326 for (i
= 0; i
< pidhash_size
; i
++)
327 INIT_HLIST_HEAD(&pid_hash
[i
]);
330 void __init
pidmap_init(void)
332 pidmap_array
->page
= (void *)get_zeroed_page(GFP_KERNEL
);
333 /* Reserve PID 0. We never call free_pidmap(0) */
334 set_bit(0, pidmap_array
->page
);
335 atomic_dec(&pidmap_array
->nr_free
);
337 pid_cachep
= kmem_cache_create("pid", sizeof(struct pid
),
338 __alignof__(struct pid
),
339 SLAB_PANIC
, NULL
, NULL
);