pid.c 13 KB

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