pid.c 12 KB

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