pid.c 10 KB

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