pid.c 9.3 KB

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