pid.c 14 KB

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