pid.c 12 KB

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