pid.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
  29. static struct hlist_head *pid_hash[PIDTYPE_MAX];
  30. static int pidhash_shift;
  31. int pid_max = PID_MAX_DEFAULT;
  32. int last_pid;
  33. #define RESERVED_PIDS 300
  34. int pid_max_min = RESERVED_PIDS + 1;
  35. int pid_max_max = PID_MAX_LIMIT;
  36. #define PIDMAP_ENTRIES ((PID_MAX_LIMIT + 8*PAGE_SIZE - 1)/PAGE_SIZE/8)
  37. #define BITS_PER_PAGE (PAGE_SIZE*8)
  38. #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
  39. #define mk_pid(map, off) (((map) - pidmap_array)*BITS_PER_PAGE + (off))
  40. #define find_next_offset(map, off) \
  41. find_next_zero_bit((map)->page, BITS_PER_PAGE, off)
  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. static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
  53. { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
  54. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
  55. fastcall void free_pidmap(int pid)
  56. {
  57. pidmap_t *map = pidmap_array + pid / BITS_PER_PAGE;
  58. int offset = pid & BITS_PER_PAGE_MASK;
  59. clear_bit(offset, map->page);
  60. atomic_inc(&map->nr_free);
  61. }
  62. int alloc_pidmap(void)
  63. {
  64. int i, offset, max_scan, pid, last = last_pid;
  65. pidmap_t *map;
  66. pid = last + 1;
  67. if (pid >= pid_max)
  68. pid = RESERVED_PIDS;
  69. offset = pid & BITS_PER_PAGE_MASK;
  70. map = &pidmap_array[pid/BITS_PER_PAGE];
  71. max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
  72. for (i = 0; i <= max_scan; ++i) {
  73. if (unlikely(!map->page)) {
  74. unsigned long page = get_zeroed_page(GFP_KERNEL);
  75. /*
  76. * Free the page if someone raced with us
  77. * installing it:
  78. */
  79. spin_lock(&pidmap_lock);
  80. if (map->page)
  81. free_page(page);
  82. else
  83. map->page = (void *)page;
  84. spin_unlock(&pidmap_lock);
  85. if (unlikely(!map->page))
  86. break;
  87. }
  88. if (likely(atomic_read(&map->nr_free))) {
  89. do {
  90. if (!test_and_set_bit(offset, map->page)) {
  91. atomic_dec(&map->nr_free);
  92. last_pid = pid;
  93. return pid;
  94. }
  95. offset = find_next_offset(map, offset);
  96. pid = mk_pid(map, offset);
  97. /*
  98. * find_next_offset() found a bit, the pid from it
  99. * is in-bounds, and if we fell back to the last
  100. * bitmap block and the final block was the same
  101. * as the starting point, pid is before last_pid.
  102. */
  103. } while (offset < BITS_PER_PAGE && pid < pid_max &&
  104. (i != max_scan || pid < last ||
  105. !((last+1) & BITS_PER_PAGE_MASK)));
  106. }
  107. if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
  108. ++map;
  109. offset = 0;
  110. } else {
  111. map = &pidmap_array[0];
  112. offset = RESERVED_PIDS;
  113. if (unlikely(last == offset))
  114. break;
  115. }
  116. pid = mk_pid(map, offset);
  117. }
  118. return -1;
  119. }
  120. struct pid * fastcall find_pid(enum pid_type type, int nr)
  121. {
  122. struct hlist_node *elem;
  123. struct pid *pid;
  124. hlist_for_each_entry_rcu(pid, elem,
  125. &pid_hash[type][pid_hashfn(nr)], pid_chain) {
  126. if (pid->nr == nr)
  127. return pid;
  128. }
  129. return NULL;
  130. }
  131. int fastcall attach_pid(task_t *task, enum pid_type type, int nr)
  132. {
  133. struct pid *pid, *task_pid;
  134. task_pid = &task->pids[type];
  135. pid = find_pid(type, nr);
  136. task_pid->nr = nr;
  137. if (pid == NULL) {
  138. INIT_LIST_HEAD(&task_pid->pid_list);
  139. hlist_add_head_rcu(&task_pid->pid_chain,
  140. &pid_hash[type][pid_hashfn(nr)]);
  141. } else {
  142. INIT_HLIST_NODE(&task_pid->pid_chain);
  143. list_add_tail_rcu(&task_pid->pid_list, &pid->pid_list);
  144. }
  145. return 0;
  146. }
  147. static fastcall int __detach_pid(task_t *task, enum pid_type type)
  148. {
  149. struct pid *pid, *pid_next;
  150. int nr = 0;
  151. pid = &task->pids[type];
  152. if (!hlist_unhashed(&pid->pid_chain)) {
  153. if (list_empty(&pid->pid_list)) {
  154. nr = pid->nr;
  155. hlist_del_rcu(&pid->pid_chain);
  156. } else {
  157. pid_next = list_entry(pid->pid_list.next,
  158. struct pid, pid_list);
  159. /* insert next pid from pid_list to hash */
  160. hlist_replace_rcu(&pid->pid_chain,
  161. &pid_next->pid_chain);
  162. }
  163. }
  164. list_del_rcu(&pid->pid_list);
  165. pid->nr = 0;
  166. return nr;
  167. }
  168. void fastcall detach_pid(task_t *task, enum pid_type type)
  169. {
  170. int tmp, nr;
  171. nr = __detach_pid(task, type);
  172. if (!nr)
  173. return;
  174. for (tmp = PIDTYPE_MAX; --tmp >= 0; )
  175. if (tmp != type && find_pid(tmp, nr))
  176. return;
  177. free_pidmap(nr);
  178. }
  179. task_t *find_task_by_pid_type(int type, int nr)
  180. {
  181. struct pid *pid;
  182. pid = find_pid(type, nr);
  183. if (!pid)
  184. return NULL;
  185. return pid_task(&pid->pid_list, type);
  186. }
  187. EXPORT_SYMBOL(find_task_by_pid_type);
  188. /*
  189. * This function switches the PIDs if a non-leader thread calls
  190. * sys_execve() - this must be done without releasing the PID.
  191. * (which a detach_pid() would eventually do.)
  192. */
  193. void switch_exec_pids(task_t *leader, task_t *thread)
  194. {
  195. __detach_pid(leader, PIDTYPE_PID);
  196. __detach_pid(leader, PIDTYPE_TGID);
  197. __detach_pid(leader, PIDTYPE_PGID);
  198. __detach_pid(leader, PIDTYPE_SID);
  199. __detach_pid(thread, PIDTYPE_PID);
  200. __detach_pid(thread, PIDTYPE_TGID);
  201. leader->pid = leader->tgid = thread->pid;
  202. thread->pid = thread->tgid;
  203. attach_pid(thread, PIDTYPE_PID, thread->pid);
  204. attach_pid(thread, PIDTYPE_TGID, thread->tgid);
  205. attach_pid(thread, PIDTYPE_PGID, thread->signal->pgrp);
  206. attach_pid(thread, PIDTYPE_SID, thread->signal->session);
  207. list_add_tail(&thread->tasks, &init_task.tasks);
  208. attach_pid(leader, PIDTYPE_PID, leader->pid);
  209. attach_pid(leader, PIDTYPE_TGID, leader->tgid);
  210. attach_pid(leader, PIDTYPE_PGID, leader->signal->pgrp);
  211. attach_pid(leader, PIDTYPE_SID, leader->signal->session);
  212. }
  213. /*
  214. * The pid hash table is scaled according to the amount of memory in the
  215. * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
  216. * more.
  217. */
  218. void __init pidhash_init(void)
  219. {
  220. int i, j, pidhash_size;
  221. unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
  222. pidhash_shift = max(4, fls(megabytes * 4));
  223. pidhash_shift = min(12, pidhash_shift);
  224. pidhash_size = 1 << pidhash_shift;
  225. printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
  226. pidhash_size, pidhash_shift,
  227. PIDTYPE_MAX * pidhash_size * sizeof(struct hlist_head));
  228. for (i = 0; i < PIDTYPE_MAX; i++) {
  229. pid_hash[i] = alloc_bootmem(pidhash_size *
  230. sizeof(*(pid_hash[i])));
  231. if (!pid_hash[i])
  232. panic("Could not alloc pidhash!\n");
  233. for (j = 0; j < pidhash_size; j++)
  234. INIT_HLIST_HEAD(&pid_hash[i][j]);
  235. }
  236. }
  237. void __init pidmap_init(void)
  238. {
  239. int i;
  240. pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
  241. set_bit(0, pidmap_array->page);
  242. atomic_dec(&pidmap_array->nr_free);
  243. /*
  244. * Allocate PID 0, and hash it via all PID types:
  245. */
  246. for (i = 0; i < PIDTYPE_MAX; i++)
  247. attach_pid(current, i, 0);
  248. }