fork.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /*
  2. * linux/kernel/fork.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'fork.c' contains the help-routines for the 'fork' system call
  8. * (see also entry.S and others).
  9. * Fork is rather simple, once you get the hang of it, but the memory
  10. * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/unistd.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/module.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/completion.h>
  19. #include <linux/namespace.h>
  20. #include <linux/personality.h>
  21. #include <linux/mempolicy.h>
  22. #include <linux/sem.h>
  23. #include <linux/file.h>
  24. #include <linux/key.h>
  25. #include <linux/binfmts.h>
  26. #include <linux/mman.h>
  27. #include <linux/fs.h>
  28. #include <linux/capability.h>
  29. #include <linux/cpu.h>
  30. #include <linux/cpuset.h>
  31. #include <linux/security.h>
  32. #include <linux/swap.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/futex.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/mount.h>
  39. #include <linux/audit.h>
  40. #include <linux/profile.h>
  41. #include <linux/rmap.h>
  42. #include <linux/acct.h>
  43. #include <linux/cn_proc.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/tlbflush.h>
  50. /*
  51. * Protected counters by write_lock_irq(&tasklist_lock)
  52. */
  53. unsigned long total_forks; /* Handle normal Linux uptimes. */
  54. int nr_threads; /* The idle threads do not count.. */
  55. int max_threads; /* tunable limit on nr_threads */
  56. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  57. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  58. EXPORT_SYMBOL(tasklist_lock);
  59. int nr_processes(void)
  60. {
  61. int cpu;
  62. int total = 0;
  63. for_each_online_cpu(cpu)
  64. total += per_cpu(process_counts, cpu);
  65. return total;
  66. }
  67. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  68. # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
  69. # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
  70. static kmem_cache_t *task_struct_cachep;
  71. #endif
  72. /* SLAB cache for signal_struct structures (tsk->signal) */
  73. static kmem_cache_t *signal_cachep;
  74. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  75. kmem_cache_t *sighand_cachep;
  76. /* SLAB cache for files_struct structures (tsk->files) */
  77. kmem_cache_t *files_cachep;
  78. /* SLAB cache for fs_struct structures (tsk->fs) */
  79. kmem_cache_t *fs_cachep;
  80. /* SLAB cache for vm_area_struct structures */
  81. kmem_cache_t *vm_area_cachep;
  82. /* SLAB cache for mm_struct structures (tsk->mm) */
  83. static kmem_cache_t *mm_cachep;
  84. void free_task(struct task_struct *tsk)
  85. {
  86. free_thread_info(tsk->thread_info);
  87. rt_mutex_debug_task_free(tsk);
  88. free_task_struct(tsk);
  89. }
  90. EXPORT_SYMBOL(free_task);
  91. void __put_task_struct(struct task_struct *tsk)
  92. {
  93. WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
  94. WARN_ON(atomic_read(&tsk->usage));
  95. WARN_ON(tsk == current);
  96. security_task_free(tsk);
  97. free_uid(tsk->user);
  98. put_group_info(tsk->group_info);
  99. if (!profile_handoff_task(tsk))
  100. free_task(tsk);
  101. }
  102. void __init fork_init(unsigned long mempages)
  103. {
  104. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  105. #ifndef ARCH_MIN_TASKALIGN
  106. #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
  107. #endif
  108. /* create a slab on which task_structs can be allocated */
  109. task_struct_cachep =
  110. kmem_cache_create("task_struct", sizeof(struct task_struct),
  111. ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
  112. #endif
  113. /*
  114. * The default maximum number of threads is set to a safe
  115. * value: the thread structures can take up at most half
  116. * of memory.
  117. */
  118. max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
  119. /*
  120. * we need to allow at least 20 threads to boot a system
  121. */
  122. if(max_threads < 20)
  123. max_threads = 20;
  124. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  125. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  126. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  127. init_task.signal->rlim[RLIMIT_NPROC];
  128. }
  129. static struct task_struct *dup_task_struct(struct task_struct *orig)
  130. {
  131. struct task_struct *tsk;
  132. struct thread_info *ti;
  133. prepare_to_copy(orig);
  134. tsk = alloc_task_struct();
  135. if (!tsk)
  136. return NULL;
  137. ti = alloc_thread_info(tsk);
  138. if (!ti) {
  139. free_task_struct(tsk);
  140. return NULL;
  141. }
  142. *tsk = *orig;
  143. tsk->thread_info = ti;
  144. setup_thread_stack(tsk, orig);
  145. /* One for us, one for whoever does the "release_task()" (usually parent) */
  146. atomic_set(&tsk->usage,2);
  147. atomic_set(&tsk->fs_excl, 0);
  148. tsk->btrace_seq = 0;
  149. tsk->splice_pipe = NULL;
  150. return tsk;
  151. }
  152. #ifdef CONFIG_MMU
  153. static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  154. {
  155. struct vm_area_struct *mpnt, *tmp, **pprev;
  156. struct rb_node **rb_link, *rb_parent;
  157. int retval;
  158. unsigned long charge;
  159. struct mempolicy *pol;
  160. down_write(&oldmm->mmap_sem);
  161. flush_cache_mm(oldmm);
  162. /*
  163. * Not linked in yet - no deadlock potential:
  164. */
  165. down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
  166. mm->locked_vm = 0;
  167. mm->mmap = NULL;
  168. mm->mmap_cache = NULL;
  169. mm->free_area_cache = oldmm->mmap_base;
  170. mm->cached_hole_size = ~0UL;
  171. mm->map_count = 0;
  172. cpus_clear(mm->cpu_vm_mask);
  173. mm->mm_rb = RB_ROOT;
  174. rb_link = &mm->mm_rb.rb_node;
  175. rb_parent = NULL;
  176. pprev = &mm->mmap;
  177. for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
  178. struct file *file;
  179. if (mpnt->vm_flags & VM_DONTCOPY) {
  180. long pages = vma_pages(mpnt);
  181. mm->total_vm -= pages;
  182. vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
  183. -pages);
  184. continue;
  185. }
  186. charge = 0;
  187. if (mpnt->vm_flags & VM_ACCOUNT) {
  188. unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  189. if (security_vm_enough_memory(len))
  190. goto fail_nomem;
  191. charge = len;
  192. }
  193. tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  194. if (!tmp)
  195. goto fail_nomem;
  196. *tmp = *mpnt;
  197. pol = mpol_copy(vma_policy(mpnt));
  198. retval = PTR_ERR(pol);
  199. if (IS_ERR(pol))
  200. goto fail_nomem_policy;
  201. vma_set_policy(tmp, pol);
  202. tmp->vm_flags &= ~VM_LOCKED;
  203. tmp->vm_mm = mm;
  204. tmp->vm_next = NULL;
  205. anon_vma_link(tmp);
  206. file = tmp->vm_file;
  207. if (file) {
  208. struct inode *inode = file->f_dentry->d_inode;
  209. get_file(file);
  210. if (tmp->vm_flags & VM_DENYWRITE)
  211. atomic_dec(&inode->i_writecount);
  212. /* insert tmp into the share list, just after mpnt */
  213. spin_lock(&file->f_mapping->i_mmap_lock);
  214. tmp->vm_truncate_count = mpnt->vm_truncate_count;
  215. flush_dcache_mmap_lock(file->f_mapping);
  216. vma_prio_tree_add(tmp, mpnt);
  217. flush_dcache_mmap_unlock(file->f_mapping);
  218. spin_unlock(&file->f_mapping->i_mmap_lock);
  219. }
  220. /*
  221. * Link in the new vma and copy the page table entries.
  222. */
  223. *pprev = tmp;
  224. pprev = &tmp->vm_next;
  225. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  226. rb_link = &tmp->vm_rb.rb_right;
  227. rb_parent = &tmp->vm_rb;
  228. mm->map_count++;
  229. retval = copy_page_range(mm, oldmm, mpnt);
  230. if (tmp->vm_ops && tmp->vm_ops->open)
  231. tmp->vm_ops->open(tmp);
  232. if (retval)
  233. goto out;
  234. }
  235. retval = 0;
  236. out:
  237. up_write(&mm->mmap_sem);
  238. flush_tlb_mm(oldmm);
  239. up_write(&oldmm->mmap_sem);
  240. return retval;
  241. fail_nomem_policy:
  242. kmem_cache_free(vm_area_cachep, tmp);
  243. fail_nomem:
  244. retval = -ENOMEM;
  245. vm_unacct_memory(charge);
  246. goto out;
  247. }
  248. static inline int mm_alloc_pgd(struct mm_struct * mm)
  249. {
  250. mm->pgd = pgd_alloc(mm);
  251. if (unlikely(!mm->pgd))
  252. return -ENOMEM;
  253. return 0;
  254. }
  255. static inline void mm_free_pgd(struct mm_struct * mm)
  256. {
  257. pgd_free(mm->pgd);
  258. }
  259. #else
  260. #define dup_mmap(mm, oldmm) (0)
  261. #define mm_alloc_pgd(mm) (0)
  262. #define mm_free_pgd(mm)
  263. #endif /* CONFIG_MMU */
  264. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  265. #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
  266. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  267. #include <linux/init_task.h>
  268. static struct mm_struct * mm_init(struct mm_struct * mm)
  269. {
  270. atomic_set(&mm->mm_users, 1);
  271. atomic_set(&mm->mm_count, 1);
  272. init_rwsem(&mm->mmap_sem);
  273. INIT_LIST_HEAD(&mm->mmlist);
  274. mm->core_waiters = 0;
  275. mm->nr_ptes = 0;
  276. set_mm_counter(mm, file_rss, 0);
  277. set_mm_counter(mm, anon_rss, 0);
  278. spin_lock_init(&mm->page_table_lock);
  279. rwlock_init(&mm->ioctx_list_lock);
  280. mm->ioctx_list = NULL;
  281. mm->free_area_cache = TASK_UNMAPPED_BASE;
  282. mm->cached_hole_size = ~0UL;
  283. if (likely(!mm_alloc_pgd(mm))) {
  284. mm->def_flags = 0;
  285. return mm;
  286. }
  287. free_mm(mm);
  288. return NULL;
  289. }
  290. /*
  291. * Allocate and initialize an mm_struct.
  292. */
  293. struct mm_struct * mm_alloc(void)
  294. {
  295. struct mm_struct * mm;
  296. mm = allocate_mm();
  297. if (mm) {
  298. memset(mm, 0, sizeof(*mm));
  299. mm = mm_init(mm);
  300. }
  301. return mm;
  302. }
  303. /*
  304. * Called when the last reference to the mm
  305. * is dropped: either by a lazy thread or by
  306. * mmput. Free the page directory and the mm.
  307. */
  308. void fastcall __mmdrop(struct mm_struct *mm)
  309. {
  310. BUG_ON(mm == &init_mm);
  311. mm_free_pgd(mm);
  312. destroy_context(mm);
  313. free_mm(mm);
  314. }
  315. /*
  316. * Decrement the use count and release all resources for an mm.
  317. */
  318. void mmput(struct mm_struct *mm)
  319. {
  320. might_sleep();
  321. if (atomic_dec_and_test(&mm->mm_users)) {
  322. exit_aio(mm);
  323. exit_mmap(mm);
  324. if (!list_empty(&mm->mmlist)) {
  325. spin_lock(&mmlist_lock);
  326. list_del(&mm->mmlist);
  327. spin_unlock(&mmlist_lock);
  328. }
  329. put_swap_token(mm);
  330. mmdrop(mm);
  331. }
  332. }
  333. EXPORT_SYMBOL_GPL(mmput);
  334. /**
  335. * get_task_mm - acquire a reference to the task's mm
  336. *
  337. * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
  338. * this kernel workthread has transiently adopted a user mm with use_mm,
  339. * to do its AIO) is not set and if so returns a reference to it, after
  340. * bumping up the use count. User must release the mm via mmput()
  341. * after use. Typically used by /proc and ptrace.
  342. */
  343. struct mm_struct *get_task_mm(struct task_struct *task)
  344. {
  345. struct mm_struct *mm;
  346. task_lock(task);
  347. mm = task->mm;
  348. if (mm) {
  349. if (task->flags & PF_BORROWED_MM)
  350. mm = NULL;
  351. else
  352. atomic_inc(&mm->mm_users);
  353. }
  354. task_unlock(task);
  355. return mm;
  356. }
  357. EXPORT_SYMBOL_GPL(get_task_mm);
  358. /* Please note the differences between mmput and mm_release.
  359. * mmput is called whenever we stop holding onto a mm_struct,
  360. * error success whatever.
  361. *
  362. * mm_release is called after a mm_struct has been removed
  363. * from the current process.
  364. *
  365. * This difference is important for error handling, when we
  366. * only half set up a mm_struct for a new process and need to restore
  367. * the old one. Because we mmput the new mm_struct before
  368. * restoring the old one. . .
  369. * Eric Biederman 10 January 1998
  370. */
  371. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  372. {
  373. struct completion *vfork_done = tsk->vfork_done;
  374. /* Get rid of any cached register state */
  375. deactivate_mm(tsk, mm);
  376. /* notify parent sleeping on vfork() */
  377. if (vfork_done) {
  378. tsk->vfork_done = NULL;
  379. complete(vfork_done);
  380. }
  381. if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
  382. u32 __user * tidptr = tsk->clear_child_tid;
  383. tsk->clear_child_tid = NULL;
  384. /*
  385. * We don't check the error code - if userspace has
  386. * not set up a proper pointer then tough luck.
  387. */
  388. put_user(0, tidptr);
  389. sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
  390. }
  391. }
  392. /*
  393. * Allocate a new mm structure and copy contents from the
  394. * mm structure of the passed in task structure.
  395. */
  396. static struct mm_struct *dup_mm(struct task_struct *tsk)
  397. {
  398. struct mm_struct *mm, *oldmm = current->mm;
  399. int err;
  400. if (!oldmm)
  401. return NULL;
  402. mm = allocate_mm();
  403. if (!mm)
  404. goto fail_nomem;
  405. memcpy(mm, oldmm, sizeof(*mm));
  406. if (!mm_init(mm))
  407. goto fail_nomem;
  408. if (init_new_context(tsk, mm))
  409. goto fail_nocontext;
  410. err = dup_mmap(mm, oldmm);
  411. if (err)
  412. goto free_pt;
  413. mm->hiwater_rss = get_mm_rss(mm);
  414. mm->hiwater_vm = mm->total_vm;
  415. return mm;
  416. free_pt:
  417. mmput(mm);
  418. fail_nomem:
  419. return NULL;
  420. fail_nocontext:
  421. /*
  422. * If init_new_context() failed, we cannot use mmput() to free the mm
  423. * because it calls destroy_context()
  424. */
  425. mm_free_pgd(mm);
  426. free_mm(mm);
  427. return NULL;
  428. }
  429. static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
  430. {
  431. struct mm_struct * mm, *oldmm;
  432. int retval;
  433. tsk->min_flt = tsk->maj_flt = 0;
  434. tsk->nvcsw = tsk->nivcsw = 0;
  435. tsk->mm = NULL;
  436. tsk->active_mm = NULL;
  437. /*
  438. * Are we cloning a kernel thread?
  439. *
  440. * We need to steal a active VM for that..
  441. */
  442. oldmm = current->mm;
  443. if (!oldmm)
  444. return 0;
  445. if (clone_flags & CLONE_VM) {
  446. atomic_inc(&oldmm->mm_users);
  447. mm = oldmm;
  448. goto good_mm;
  449. }
  450. retval = -ENOMEM;
  451. mm = dup_mm(tsk);
  452. if (!mm)
  453. goto fail_nomem;
  454. good_mm:
  455. tsk->mm = mm;
  456. tsk->active_mm = mm;
  457. return 0;
  458. fail_nomem:
  459. return retval;
  460. }
  461. static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
  462. {
  463. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  464. /* We don't need to lock fs - think why ;-) */
  465. if (fs) {
  466. atomic_set(&fs->count, 1);
  467. rwlock_init(&fs->lock);
  468. fs->umask = old->umask;
  469. read_lock(&old->lock);
  470. fs->rootmnt = mntget(old->rootmnt);
  471. fs->root = dget(old->root);
  472. fs->pwdmnt = mntget(old->pwdmnt);
  473. fs->pwd = dget(old->pwd);
  474. if (old->altroot) {
  475. fs->altrootmnt = mntget(old->altrootmnt);
  476. fs->altroot = dget(old->altroot);
  477. } else {
  478. fs->altrootmnt = NULL;
  479. fs->altroot = NULL;
  480. }
  481. read_unlock(&old->lock);
  482. }
  483. return fs;
  484. }
  485. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  486. {
  487. return __copy_fs_struct(old);
  488. }
  489. EXPORT_SYMBOL_GPL(copy_fs_struct);
  490. static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
  491. {
  492. if (clone_flags & CLONE_FS) {
  493. atomic_inc(&current->fs->count);
  494. return 0;
  495. }
  496. tsk->fs = __copy_fs_struct(current->fs);
  497. if (!tsk->fs)
  498. return -ENOMEM;
  499. return 0;
  500. }
  501. static int count_open_files(struct fdtable *fdt)
  502. {
  503. int size = fdt->max_fdset;
  504. int i;
  505. /* Find the last open fd */
  506. for (i = size/(8*sizeof(long)); i > 0; ) {
  507. if (fdt->open_fds->fds_bits[--i])
  508. break;
  509. }
  510. i = (i+1) * 8 * sizeof(long);
  511. return i;
  512. }
  513. static struct files_struct *alloc_files(void)
  514. {
  515. struct files_struct *newf;
  516. struct fdtable *fdt;
  517. newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
  518. if (!newf)
  519. goto out;
  520. atomic_set(&newf->count, 1);
  521. spin_lock_init(&newf->file_lock);
  522. newf->next_fd = 0;
  523. fdt = &newf->fdtab;
  524. fdt->max_fds = NR_OPEN_DEFAULT;
  525. fdt->max_fdset = EMBEDDED_FD_SET_SIZE;
  526. fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  527. fdt->open_fds = (fd_set *)&newf->open_fds_init;
  528. fdt->fd = &newf->fd_array[0];
  529. INIT_RCU_HEAD(&fdt->rcu);
  530. fdt->free_files = NULL;
  531. fdt->next = NULL;
  532. rcu_assign_pointer(newf->fdt, fdt);
  533. out:
  534. return newf;
  535. }
  536. /*
  537. * Allocate a new files structure and copy contents from the
  538. * passed in files structure.
  539. * errorp will be valid only when the returned files_struct is NULL.
  540. */
  541. static struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  542. {
  543. struct files_struct *newf;
  544. struct file **old_fds, **new_fds;
  545. int open_files, size, i, expand;
  546. struct fdtable *old_fdt, *new_fdt;
  547. *errorp = -ENOMEM;
  548. newf = alloc_files();
  549. if (!newf)
  550. goto out;
  551. spin_lock(&oldf->file_lock);
  552. old_fdt = files_fdtable(oldf);
  553. new_fdt = files_fdtable(newf);
  554. size = old_fdt->max_fdset;
  555. open_files = count_open_files(old_fdt);
  556. expand = 0;
  557. /*
  558. * Check whether we need to allocate a larger fd array or fd set.
  559. * Note: we're not a clone task, so the open count won't change.
  560. */
  561. if (open_files > new_fdt->max_fdset) {
  562. new_fdt->max_fdset = 0;
  563. expand = 1;
  564. }
  565. if (open_files > new_fdt->max_fds) {
  566. new_fdt->max_fds = 0;
  567. expand = 1;
  568. }
  569. /* if the old fdset gets grown now, we'll only copy up to "size" fds */
  570. if (expand) {
  571. spin_unlock(&oldf->file_lock);
  572. spin_lock(&newf->file_lock);
  573. *errorp = expand_files(newf, open_files-1);
  574. spin_unlock(&newf->file_lock);
  575. if (*errorp < 0)
  576. goto out_release;
  577. new_fdt = files_fdtable(newf);
  578. /*
  579. * Reacquire the oldf lock and a pointer to its fd table
  580. * who knows it may have a new bigger fd table. We need
  581. * the latest pointer.
  582. */
  583. spin_lock(&oldf->file_lock);
  584. old_fdt = files_fdtable(oldf);
  585. }
  586. old_fds = old_fdt->fd;
  587. new_fds = new_fdt->fd;
  588. memcpy(new_fdt->open_fds->fds_bits, old_fdt->open_fds->fds_bits, open_files/8);
  589. memcpy(new_fdt->close_on_exec->fds_bits, old_fdt->close_on_exec->fds_bits, open_files/8);
  590. for (i = open_files; i != 0; i--) {
  591. struct file *f = *old_fds++;
  592. if (f) {
  593. get_file(f);
  594. } else {
  595. /*
  596. * The fd may be claimed in the fd bitmap but not yet
  597. * instantiated in the files array if a sibling thread
  598. * is partway through open(). So make sure that this
  599. * fd is available to the new process.
  600. */
  601. FD_CLR(open_files - i, new_fdt->open_fds);
  602. }
  603. rcu_assign_pointer(*new_fds++, f);
  604. }
  605. spin_unlock(&oldf->file_lock);
  606. /* compute the remainder to be cleared */
  607. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  608. /* This is long word aligned thus could use a optimized version */
  609. memset(new_fds, 0, size);
  610. if (new_fdt->max_fdset > open_files) {
  611. int left = (new_fdt->max_fdset-open_files)/8;
  612. int start = open_files / (8 * sizeof(unsigned long));
  613. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  614. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  615. }
  616. out:
  617. return newf;
  618. out_release:
  619. free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
  620. free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
  621. free_fd_array(new_fdt->fd, new_fdt->max_fds);
  622. kmem_cache_free(files_cachep, newf);
  623. return NULL;
  624. }
  625. static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
  626. {
  627. struct files_struct *oldf, *newf;
  628. int error = 0;
  629. /*
  630. * A background process may not have any files ...
  631. */
  632. oldf = current->files;
  633. if (!oldf)
  634. goto out;
  635. if (clone_flags & CLONE_FILES) {
  636. atomic_inc(&oldf->count);
  637. goto out;
  638. }
  639. /*
  640. * Note: we may be using current for both targets (See exec.c)
  641. * This works because we cache current->files (old) as oldf. Don't
  642. * break this.
  643. */
  644. tsk->files = NULL;
  645. newf = dup_fd(oldf, &error);
  646. if (!newf)
  647. goto out;
  648. tsk->files = newf;
  649. error = 0;
  650. out:
  651. return error;
  652. }
  653. /*
  654. * Helper to unshare the files of the current task.
  655. * We don't want to expose copy_files internals to
  656. * the exec layer of the kernel.
  657. */
  658. int unshare_files(void)
  659. {
  660. struct files_struct *files = current->files;
  661. int rc;
  662. BUG_ON(!files);
  663. /* This can race but the race causes us to copy when we don't
  664. need to and drop the copy */
  665. if(atomic_read(&files->count) == 1)
  666. {
  667. atomic_inc(&files->count);
  668. return 0;
  669. }
  670. rc = copy_files(0, current);
  671. if(rc)
  672. current->files = files;
  673. return rc;
  674. }
  675. EXPORT_SYMBOL(unshare_files);
  676. static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
  677. {
  678. struct sighand_struct *sig;
  679. if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
  680. atomic_inc(&current->sighand->count);
  681. return 0;
  682. }
  683. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  684. rcu_assign_pointer(tsk->sighand, sig);
  685. if (!sig)
  686. return -ENOMEM;
  687. atomic_set(&sig->count, 1);
  688. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  689. return 0;
  690. }
  691. void __cleanup_sighand(struct sighand_struct *sighand)
  692. {
  693. if (atomic_dec_and_test(&sighand->count))
  694. kmem_cache_free(sighand_cachep, sighand);
  695. }
  696. static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
  697. {
  698. struct signal_struct *sig;
  699. int ret;
  700. if (clone_flags & CLONE_THREAD) {
  701. atomic_inc(&current->signal->count);
  702. atomic_inc(&current->signal->live);
  703. return 0;
  704. }
  705. sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  706. tsk->signal = sig;
  707. if (!sig)
  708. return -ENOMEM;
  709. ret = copy_thread_group_keys(tsk);
  710. if (ret < 0) {
  711. kmem_cache_free(signal_cachep, sig);
  712. return ret;
  713. }
  714. atomic_set(&sig->count, 1);
  715. atomic_set(&sig->live, 1);
  716. init_waitqueue_head(&sig->wait_chldexit);
  717. sig->flags = 0;
  718. sig->group_exit_code = 0;
  719. sig->group_exit_task = NULL;
  720. sig->group_stop_count = 0;
  721. sig->curr_target = NULL;
  722. init_sigpending(&sig->shared_pending);
  723. INIT_LIST_HEAD(&sig->posix_timers);
  724. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_REL);
  725. sig->it_real_incr.tv64 = 0;
  726. sig->real_timer.function = it_real_fn;
  727. sig->tsk = tsk;
  728. sig->it_virt_expires = cputime_zero;
  729. sig->it_virt_incr = cputime_zero;
  730. sig->it_prof_expires = cputime_zero;
  731. sig->it_prof_incr = cputime_zero;
  732. sig->leader = 0; /* session leadership doesn't inherit */
  733. sig->tty_old_pgrp = 0;
  734. sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
  735. sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
  736. sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
  737. sig->sched_time = 0;
  738. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  739. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  740. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  741. task_lock(current->group_leader);
  742. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  743. task_unlock(current->group_leader);
  744. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  745. /*
  746. * New sole thread in the process gets an expiry time
  747. * of the whole CPU time limit.
  748. */
  749. tsk->it_prof_expires =
  750. secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  751. }
  752. acct_init_pacct(&sig->pacct);
  753. return 0;
  754. }
  755. void __cleanup_signal(struct signal_struct *sig)
  756. {
  757. exit_thread_group_keys(sig);
  758. kmem_cache_free(signal_cachep, sig);
  759. }
  760. static inline void cleanup_signal(struct task_struct *tsk)
  761. {
  762. struct signal_struct *sig = tsk->signal;
  763. atomic_dec(&sig->live);
  764. if (atomic_dec_and_test(&sig->count))
  765. __cleanup_signal(sig);
  766. }
  767. static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
  768. {
  769. unsigned long new_flags = p->flags;
  770. new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
  771. new_flags |= PF_FORKNOEXEC;
  772. if (!(clone_flags & CLONE_PTRACE))
  773. p->ptrace = 0;
  774. p->flags = new_flags;
  775. }
  776. asmlinkage long sys_set_tid_address(int __user *tidptr)
  777. {
  778. current->clear_child_tid = tidptr;
  779. return current->pid;
  780. }
  781. static inline void rt_mutex_init_task(struct task_struct *p)
  782. {
  783. #ifdef CONFIG_RT_MUTEXES
  784. spin_lock_init(&p->pi_lock);
  785. plist_head_init(&p->pi_waiters, &p->pi_lock);
  786. p->pi_blocked_on = NULL;
  787. #endif
  788. }
  789. /*
  790. * This creates a new process as a copy of the old one,
  791. * but does not actually start it yet.
  792. *
  793. * It copies the registers, and all the appropriate
  794. * parts of the process environment (as per the clone
  795. * flags). The actual kick-off is left to the caller.
  796. */
  797. static struct task_struct *copy_process(unsigned long clone_flags,
  798. unsigned long stack_start,
  799. struct pt_regs *regs,
  800. unsigned long stack_size,
  801. int __user *parent_tidptr,
  802. int __user *child_tidptr,
  803. int pid)
  804. {
  805. int retval;
  806. struct task_struct *p = NULL;
  807. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  808. return ERR_PTR(-EINVAL);
  809. /*
  810. * Thread groups must share signals as well, and detached threads
  811. * can only be started up within the thread group.
  812. */
  813. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  814. return ERR_PTR(-EINVAL);
  815. /*
  816. * Shared signal handlers imply shared VM. By way of the above,
  817. * thread groups also imply shared VM. Blocking this case allows
  818. * for various simplifications in other code.
  819. */
  820. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  821. return ERR_PTR(-EINVAL);
  822. retval = security_task_create(clone_flags);
  823. if (retval)
  824. goto fork_out;
  825. retval = -ENOMEM;
  826. p = dup_task_struct(current);
  827. if (!p)
  828. goto fork_out;
  829. #ifdef CONFIG_TRACE_IRQFLAGS
  830. DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
  831. DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
  832. #endif
  833. retval = -EAGAIN;
  834. if (atomic_read(&p->user->processes) >=
  835. p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
  836. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
  837. p->user != &root_user)
  838. goto bad_fork_free;
  839. }
  840. atomic_inc(&p->user->__count);
  841. atomic_inc(&p->user->processes);
  842. get_group_info(p->group_info);
  843. /*
  844. * If multiple threads are within copy_process(), then this check
  845. * triggers too late. This doesn't hurt, the check is only there
  846. * to stop root fork bombs.
  847. */
  848. if (nr_threads >= max_threads)
  849. goto bad_fork_cleanup_count;
  850. if (!try_module_get(task_thread_info(p)->exec_domain->module))
  851. goto bad_fork_cleanup_count;
  852. if (p->binfmt && !try_module_get(p->binfmt->module))
  853. goto bad_fork_cleanup_put_domain;
  854. p->did_exec = 0;
  855. copy_flags(clone_flags, p);
  856. p->pid = pid;
  857. retval = -EFAULT;
  858. if (clone_flags & CLONE_PARENT_SETTID)
  859. if (put_user(p->pid, parent_tidptr))
  860. goto bad_fork_cleanup;
  861. INIT_LIST_HEAD(&p->children);
  862. INIT_LIST_HEAD(&p->sibling);
  863. p->vfork_done = NULL;
  864. spin_lock_init(&p->alloc_lock);
  865. clear_tsk_thread_flag(p, TIF_SIGPENDING);
  866. init_sigpending(&p->pending);
  867. p->utime = cputime_zero;
  868. p->stime = cputime_zero;
  869. p->sched_time = 0;
  870. p->rchar = 0; /* I/O counter: bytes read */
  871. p->wchar = 0; /* I/O counter: bytes written */
  872. p->syscr = 0; /* I/O counter: read syscalls */
  873. p->syscw = 0; /* I/O counter: write syscalls */
  874. acct_clear_integrals(p);
  875. p->it_virt_expires = cputime_zero;
  876. p->it_prof_expires = cputime_zero;
  877. p->it_sched_expires = 0;
  878. INIT_LIST_HEAD(&p->cpu_timers[0]);
  879. INIT_LIST_HEAD(&p->cpu_timers[1]);
  880. INIT_LIST_HEAD(&p->cpu_timers[2]);
  881. p->lock_depth = -1; /* -1 = no lock */
  882. do_posix_clock_monotonic_gettime(&p->start_time);
  883. p->security = NULL;
  884. p->io_context = NULL;
  885. p->io_wait = NULL;
  886. p->audit_context = NULL;
  887. cpuset_fork(p);
  888. #ifdef CONFIG_NUMA
  889. p->mempolicy = mpol_copy(p->mempolicy);
  890. if (IS_ERR(p->mempolicy)) {
  891. retval = PTR_ERR(p->mempolicy);
  892. p->mempolicy = NULL;
  893. goto bad_fork_cleanup_cpuset;
  894. }
  895. mpol_fix_fork_child_flag(p);
  896. #endif
  897. #ifdef CONFIG_TRACE_IRQFLAGS
  898. p->irq_events = 0;
  899. p->hardirqs_enabled = 0;
  900. p->hardirq_enable_ip = 0;
  901. p->hardirq_enable_event = 0;
  902. p->hardirq_disable_ip = _THIS_IP_;
  903. p->hardirq_disable_event = 0;
  904. p->softirqs_enabled = 1;
  905. p->softirq_enable_ip = _THIS_IP_;
  906. p->softirq_enable_event = 0;
  907. p->softirq_disable_ip = 0;
  908. p->softirq_disable_event = 0;
  909. p->hardirq_context = 0;
  910. p->softirq_context = 0;
  911. #endif
  912. #ifdef CONFIG_LOCKDEP
  913. p->lockdep_depth = 0; /* no locks held yet */
  914. p->curr_chain_key = 0;
  915. p->lockdep_recursion = 0;
  916. #endif
  917. rt_mutex_init_task(p);
  918. #ifdef CONFIG_DEBUG_MUTEXES
  919. p->blocked_on = NULL; /* not blocked yet */
  920. #endif
  921. p->tgid = p->pid;
  922. if (clone_flags & CLONE_THREAD)
  923. p->tgid = current->tgid;
  924. if ((retval = security_task_alloc(p)))
  925. goto bad_fork_cleanup_policy;
  926. if ((retval = audit_alloc(p)))
  927. goto bad_fork_cleanup_security;
  928. /* copy all the process information */
  929. if ((retval = copy_semundo(clone_flags, p)))
  930. goto bad_fork_cleanup_audit;
  931. if ((retval = copy_files(clone_flags, p)))
  932. goto bad_fork_cleanup_semundo;
  933. if ((retval = copy_fs(clone_flags, p)))
  934. goto bad_fork_cleanup_files;
  935. if ((retval = copy_sighand(clone_flags, p)))
  936. goto bad_fork_cleanup_fs;
  937. if ((retval = copy_signal(clone_flags, p)))
  938. goto bad_fork_cleanup_sighand;
  939. if ((retval = copy_mm(clone_flags, p)))
  940. goto bad_fork_cleanup_signal;
  941. if ((retval = copy_keys(clone_flags, p)))
  942. goto bad_fork_cleanup_mm;
  943. if ((retval = copy_namespace(clone_flags, p)))
  944. goto bad_fork_cleanup_keys;
  945. retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
  946. if (retval)
  947. goto bad_fork_cleanup_namespace;
  948. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  949. /*
  950. * Clear TID on mm_release()?
  951. */
  952. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
  953. p->robust_list = NULL;
  954. #ifdef CONFIG_COMPAT
  955. p->compat_robust_list = NULL;
  956. #endif
  957. INIT_LIST_HEAD(&p->pi_state_list);
  958. p->pi_state_cache = NULL;
  959. /*
  960. * sigaltstack should be cleared when sharing the same VM
  961. */
  962. if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
  963. p->sas_ss_sp = p->sas_ss_size = 0;
  964. /*
  965. * Syscall tracing should be turned off in the child regardless
  966. * of CLONE_PTRACE.
  967. */
  968. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  969. #ifdef TIF_SYSCALL_EMU
  970. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  971. #endif
  972. /* Our parent execution domain becomes current domain
  973. These must match for thread signalling to apply */
  974. p->parent_exec_id = p->self_exec_id;
  975. /* ok, now we should be set up.. */
  976. p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
  977. p->pdeath_signal = 0;
  978. p->exit_state = 0;
  979. /*
  980. * Ok, make it visible to the rest of the system.
  981. * We dont wake it up yet.
  982. */
  983. p->group_leader = p;
  984. INIT_LIST_HEAD(&p->thread_group);
  985. INIT_LIST_HEAD(&p->ptrace_children);
  986. INIT_LIST_HEAD(&p->ptrace_list);
  987. /* Perform scheduler related setup. Assign this task to a CPU. */
  988. sched_fork(p, clone_flags);
  989. /* Need tasklist lock for parent etc handling! */
  990. write_lock_irq(&tasklist_lock);
  991. /*
  992. * The task hasn't been attached yet, so its cpus_allowed mask will
  993. * not be changed, nor will its assigned CPU.
  994. *
  995. * The cpus_allowed mask of the parent may have changed after it was
  996. * copied first time - so re-copy it here, then check the child's CPU
  997. * to ensure it is on a valid CPU (and if not, just force it back to
  998. * parent's CPU). This avoids alot of nasty races.
  999. */
  1000. p->cpus_allowed = current->cpus_allowed;
  1001. if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
  1002. !cpu_online(task_cpu(p))))
  1003. set_task_cpu(p, smp_processor_id());
  1004. /* CLONE_PARENT re-uses the old parent */
  1005. if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
  1006. p->real_parent = current->real_parent;
  1007. else
  1008. p->real_parent = current;
  1009. p->parent = p->real_parent;
  1010. spin_lock(&current->sighand->siglock);
  1011. /*
  1012. * Process group and session signals need to be delivered to just the
  1013. * parent before the fork or both the parent and the child after the
  1014. * fork. Restart if a signal comes in before we add the new process to
  1015. * it's process group.
  1016. * A fatal signal pending means that current will exit, so the new
  1017. * thread can't slip out of an OOM kill (or normal SIGKILL).
  1018. */
  1019. recalc_sigpending();
  1020. if (signal_pending(current)) {
  1021. spin_unlock(&current->sighand->siglock);
  1022. write_unlock_irq(&tasklist_lock);
  1023. retval = -ERESTARTNOINTR;
  1024. goto bad_fork_cleanup_namespace;
  1025. }
  1026. if (clone_flags & CLONE_THREAD) {
  1027. p->group_leader = current->group_leader;
  1028. list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
  1029. if (!cputime_eq(current->signal->it_virt_expires,
  1030. cputime_zero) ||
  1031. !cputime_eq(current->signal->it_prof_expires,
  1032. cputime_zero) ||
  1033. current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
  1034. !list_empty(&current->signal->cpu_timers[0]) ||
  1035. !list_empty(&current->signal->cpu_timers[1]) ||
  1036. !list_empty(&current->signal->cpu_timers[2])) {
  1037. /*
  1038. * Have child wake up on its first tick to check
  1039. * for process CPU timers.
  1040. */
  1041. p->it_prof_expires = jiffies_to_cputime(1);
  1042. }
  1043. }
  1044. /*
  1045. * inherit ioprio
  1046. */
  1047. p->ioprio = current->ioprio;
  1048. if (likely(p->pid)) {
  1049. add_parent(p);
  1050. if (unlikely(p->ptrace & PT_PTRACED))
  1051. __ptrace_link(p, current->parent);
  1052. if (thread_group_leader(p)) {
  1053. p->signal->tty = current->signal->tty;
  1054. p->signal->pgrp = process_group(current);
  1055. p->signal->session = current->signal->session;
  1056. attach_pid(p, PIDTYPE_PGID, process_group(p));
  1057. attach_pid(p, PIDTYPE_SID, p->signal->session);
  1058. list_add_tail_rcu(&p->tasks, &init_task.tasks);
  1059. __get_cpu_var(process_counts)++;
  1060. }
  1061. attach_pid(p, PIDTYPE_PID, p->pid);
  1062. nr_threads++;
  1063. }
  1064. total_forks++;
  1065. spin_unlock(&current->sighand->siglock);
  1066. write_unlock_irq(&tasklist_lock);
  1067. proc_fork_connector(p);
  1068. return p;
  1069. bad_fork_cleanup_namespace:
  1070. exit_namespace(p);
  1071. bad_fork_cleanup_keys:
  1072. exit_keys(p);
  1073. bad_fork_cleanup_mm:
  1074. if (p->mm)
  1075. mmput(p->mm);
  1076. bad_fork_cleanup_signal:
  1077. cleanup_signal(p);
  1078. bad_fork_cleanup_sighand:
  1079. __cleanup_sighand(p->sighand);
  1080. bad_fork_cleanup_fs:
  1081. exit_fs(p); /* blocking */
  1082. bad_fork_cleanup_files:
  1083. exit_files(p); /* blocking */
  1084. bad_fork_cleanup_semundo:
  1085. exit_sem(p);
  1086. bad_fork_cleanup_audit:
  1087. audit_free(p);
  1088. bad_fork_cleanup_security:
  1089. security_task_free(p);
  1090. bad_fork_cleanup_policy:
  1091. #ifdef CONFIG_NUMA
  1092. mpol_free(p->mempolicy);
  1093. bad_fork_cleanup_cpuset:
  1094. #endif
  1095. cpuset_exit(p);
  1096. bad_fork_cleanup:
  1097. if (p->binfmt)
  1098. module_put(p->binfmt->module);
  1099. bad_fork_cleanup_put_domain:
  1100. module_put(task_thread_info(p)->exec_domain->module);
  1101. bad_fork_cleanup_count:
  1102. put_group_info(p->group_info);
  1103. atomic_dec(&p->user->processes);
  1104. free_uid(p->user);
  1105. bad_fork_free:
  1106. free_task(p);
  1107. fork_out:
  1108. return ERR_PTR(retval);
  1109. }
  1110. struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
  1111. {
  1112. memset(regs, 0, sizeof(struct pt_regs));
  1113. return regs;
  1114. }
  1115. struct task_struct * __devinit fork_idle(int cpu)
  1116. {
  1117. struct task_struct *task;
  1118. struct pt_regs regs;
  1119. task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
  1120. if (!task)
  1121. return ERR_PTR(-ENOMEM);
  1122. init_idle(task, cpu);
  1123. return task;
  1124. }
  1125. static inline int fork_traceflag (unsigned clone_flags)
  1126. {
  1127. if (clone_flags & CLONE_UNTRACED)
  1128. return 0;
  1129. else if (clone_flags & CLONE_VFORK) {
  1130. if (current->ptrace & PT_TRACE_VFORK)
  1131. return PTRACE_EVENT_VFORK;
  1132. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  1133. if (current->ptrace & PT_TRACE_CLONE)
  1134. return PTRACE_EVENT_CLONE;
  1135. } else if (current->ptrace & PT_TRACE_FORK)
  1136. return PTRACE_EVENT_FORK;
  1137. return 0;
  1138. }
  1139. /*
  1140. * Ok, this is the main fork-routine.
  1141. *
  1142. * It copies the process, and if successful kick-starts
  1143. * it and waits for it to finish using the VM if required.
  1144. */
  1145. long do_fork(unsigned long clone_flags,
  1146. unsigned long stack_start,
  1147. struct pt_regs *regs,
  1148. unsigned long stack_size,
  1149. int __user *parent_tidptr,
  1150. int __user *child_tidptr)
  1151. {
  1152. struct task_struct *p;
  1153. int trace = 0;
  1154. struct pid *pid = alloc_pid();
  1155. long nr;
  1156. if (!pid)
  1157. return -EAGAIN;
  1158. nr = pid->nr;
  1159. if (unlikely(current->ptrace)) {
  1160. trace = fork_traceflag (clone_flags);
  1161. if (trace)
  1162. clone_flags |= CLONE_PTRACE;
  1163. }
  1164. p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);
  1165. /*
  1166. * Do this prior waking up the new thread - the thread pointer
  1167. * might get invalid after that point, if the thread exits quickly.
  1168. */
  1169. if (!IS_ERR(p)) {
  1170. struct completion vfork;
  1171. if (clone_flags & CLONE_VFORK) {
  1172. p->vfork_done = &vfork;
  1173. init_completion(&vfork);
  1174. }
  1175. if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
  1176. /*
  1177. * We'll start up with an immediate SIGSTOP.
  1178. */
  1179. sigaddset(&p->pending.signal, SIGSTOP);
  1180. set_tsk_thread_flag(p, TIF_SIGPENDING);
  1181. }
  1182. if (!(clone_flags & CLONE_STOPPED))
  1183. wake_up_new_task(p, clone_flags);
  1184. else
  1185. p->state = TASK_STOPPED;
  1186. if (unlikely (trace)) {
  1187. current->ptrace_message = nr;
  1188. ptrace_notify ((trace << 8) | SIGTRAP);
  1189. }
  1190. if (clone_flags & CLONE_VFORK) {
  1191. wait_for_completion(&vfork);
  1192. if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
  1193. ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
  1194. }
  1195. } else {
  1196. free_pid(pid);
  1197. nr = PTR_ERR(p);
  1198. }
  1199. return nr;
  1200. }
  1201. #ifndef ARCH_MIN_MMSTRUCT_ALIGN
  1202. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  1203. #endif
  1204. static void sighand_ctor(void *data, kmem_cache_t *cachep, unsigned long flags)
  1205. {
  1206. struct sighand_struct *sighand = data;
  1207. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  1208. SLAB_CTOR_CONSTRUCTOR)
  1209. spin_lock_init(&sighand->siglock);
  1210. }
  1211. void __init proc_caches_init(void)
  1212. {
  1213. sighand_cachep = kmem_cache_create("sighand_cache",
  1214. sizeof(struct sighand_struct), 0,
  1215. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
  1216. sighand_ctor, NULL);
  1217. signal_cachep = kmem_cache_create("signal_cache",
  1218. sizeof(struct signal_struct), 0,
  1219. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1220. files_cachep = kmem_cache_create("files_cache",
  1221. sizeof(struct files_struct), 0,
  1222. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1223. fs_cachep = kmem_cache_create("fs_cache",
  1224. sizeof(struct fs_struct), 0,
  1225. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1226. vm_area_cachep = kmem_cache_create("vm_area_struct",
  1227. sizeof(struct vm_area_struct), 0,
  1228. SLAB_PANIC, NULL, NULL);
  1229. mm_cachep = kmem_cache_create("mm_struct",
  1230. sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
  1231. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1232. }
  1233. /*
  1234. * Check constraints on flags passed to the unshare system call and
  1235. * force unsharing of additional process context as appropriate.
  1236. */
  1237. static inline void check_unshare_flags(unsigned long *flags_ptr)
  1238. {
  1239. /*
  1240. * If unsharing a thread from a thread group, must also
  1241. * unshare vm.
  1242. */
  1243. if (*flags_ptr & CLONE_THREAD)
  1244. *flags_ptr |= CLONE_VM;
  1245. /*
  1246. * If unsharing vm, must also unshare signal handlers.
  1247. */
  1248. if (*flags_ptr & CLONE_VM)
  1249. *flags_ptr |= CLONE_SIGHAND;
  1250. /*
  1251. * If unsharing signal handlers and the task was created
  1252. * using CLONE_THREAD, then must unshare the thread
  1253. */
  1254. if ((*flags_ptr & CLONE_SIGHAND) &&
  1255. (atomic_read(&current->signal->count) > 1))
  1256. *flags_ptr |= CLONE_THREAD;
  1257. /*
  1258. * If unsharing namespace, must also unshare filesystem information.
  1259. */
  1260. if (*flags_ptr & CLONE_NEWNS)
  1261. *flags_ptr |= CLONE_FS;
  1262. }
  1263. /*
  1264. * Unsharing of tasks created with CLONE_THREAD is not supported yet
  1265. */
  1266. static int unshare_thread(unsigned long unshare_flags)
  1267. {
  1268. if (unshare_flags & CLONE_THREAD)
  1269. return -EINVAL;
  1270. return 0;
  1271. }
  1272. /*
  1273. * Unshare the filesystem structure if it is being shared
  1274. */
  1275. static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
  1276. {
  1277. struct fs_struct *fs = current->fs;
  1278. if ((unshare_flags & CLONE_FS) &&
  1279. (fs && atomic_read(&fs->count) > 1)) {
  1280. *new_fsp = __copy_fs_struct(current->fs);
  1281. if (!*new_fsp)
  1282. return -ENOMEM;
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * Unshare the namespace structure if it is being shared
  1288. */
  1289. static int unshare_namespace(unsigned long unshare_flags, struct namespace **new_nsp, struct fs_struct *new_fs)
  1290. {
  1291. struct namespace *ns = current->namespace;
  1292. if ((unshare_flags & CLONE_NEWNS) &&
  1293. (ns && atomic_read(&ns->count) > 1)) {
  1294. if (!capable(CAP_SYS_ADMIN))
  1295. return -EPERM;
  1296. *new_nsp = dup_namespace(current, new_fs ? new_fs : current->fs);
  1297. if (!*new_nsp)
  1298. return -ENOMEM;
  1299. }
  1300. return 0;
  1301. }
  1302. /*
  1303. * Unsharing of sighand for tasks created with CLONE_SIGHAND is not
  1304. * supported yet
  1305. */
  1306. static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
  1307. {
  1308. struct sighand_struct *sigh = current->sighand;
  1309. if ((unshare_flags & CLONE_SIGHAND) &&
  1310. (sigh && atomic_read(&sigh->count) > 1))
  1311. return -EINVAL;
  1312. else
  1313. return 0;
  1314. }
  1315. /*
  1316. * Unshare vm if it is being shared
  1317. */
  1318. static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
  1319. {
  1320. struct mm_struct *mm = current->mm;
  1321. if ((unshare_flags & CLONE_VM) &&
  1322. (mm && atomic_read(&mm->mm_users) > 1)) {
  1323. return -EINVAL;
  1324. }
  1325. return 0;
  1326. }
  1327. /*
  1328. * Unshare file descriptor table if it is being shared
  1329. */
  1330. static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
  1331. {
  1332. struct files_struct *fd = current->files;
  1333. int error = 0;
  1334. if ((unshare_flags & CLONE_FILES) &&
  1335. (fd && atomic_read(&fd->count) > 1)) {
  1336. *new_fdp = dup_fd(fd, &error);
  1337. if (!*new_fdp)
  1338. return error;
  1339. }
  1340. return 0;
  1341. }
  1342. /*
  1343. * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
  1344. * supported yet
  1345. */
  1346. static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
  1347. {
  1348. if (unshare_flags & CLONE_SYSVSEM)
  1349. return -EINVAL;
  1350. return 0;
  1351. }
  1352. /*
  1353. * unshare allows a process to 'unshare' part of the process
  1354. * context which was originally shared using clone. copy_*
  1355. * functions used by do_fork() cannot be used here directly
  1356. * because they modify an inactive task_struct that is being
  1357. * constructed. Here we are modifying the current, active,
  1358. * task_struct.
  1359. */
  1360. asmlinkage long sys_unshare(unsigned long unshare_flags)
  1361. {
  1362. int err = 0;
  1363. struct fs_struct *fs, *new_fs = NULL;
  1364. struct namespace *ns, *new_ns = NULL;
  1365. struct sighand_struct *sigh, *new_sigh = NULL;
  1366. struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
  1367. struct files_struct *fd, *new_fd = NULL;
  1368. struct sem_undo_list *new_ulist = NULL;
  1369. check_unshare_flags(&unshare_flags);
  1370. /* Return -EINVAL for all unsupported flags */
  1371. err = -EINVAL;
  1372. if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
  1373. CLONE_VM|CLONE_FILES|CLONE_SYSVSEM))
  1374. goto bad_unshare_out;
  1375. if ((err = unshare_thread(unshare_flags)))
  1376. goto bad_unshare_out;
  1377. if ((err = unshare_fs(unshare_flags, &new_fs)))
  1378. goto bad_unshare_cleanup_thread;
  1379. if ((err = unshare_namespace(unshare_flags, &new_ns, new_fs)))
  1380. goto bad_unshare_cleanup_fs;
  1381. if ((err = unshare_sighand(unshare_flags, &new_sigh)))
  1382. goto bad_unshare_cleanup_ns;
  1383. if ((err = unshare_vm(unshare_flags, &new_mm)))
  1384. goto bad_unshare_cleanup_sigh;
  1385. if ((err = unshare_fd(unshare_flags, &new_fd)))
  1386. goto bad_unshare_cleanup_vm;
  1387. if ((err = unshare_semundo(unshare_flags, &new_ulist)))
  1388. goto bad_unshare_cleanup_fd;
  1389. if (new_fs || new_ns || new_sigh || new_mm || new_fd || new_ulist) {
  1390. task_lock(current);
  1391. if (new_fs) {
  1392. fs = current->fs;
  1393. current->fs = new_fs;
  1394. new_fs = fs;
  1395. }
  1396. if (new_ns) {
  1397. ns = current->namespace;
  1398. current->namespace = new_ns;
  1399. new_ns = ns;
  1400. }
  1401. if (new_sigh) {
  1402. sigh = current->sighand;
  1403. rcu_assign_pointer(current->sighand, new_sigh);
  1404. new_sigh = sigh;
  1405. }
  1406. if (new_mm) {
  1407. mm = current->mm;
  1408. active_mm = current->active_mm;
  1409. current->mm = new_mm;
  1410. current->active_mm = new_mm;
  1411. activate_mm(active_mm, new_mm);
  1412. new_mm = mm;
  1413. }
  1414. if (new_fd) {
  1415. fd = current->files;
  1416. current->files = new_fd;
  1417. new_fd = fd;
  1418. }
  1419. task_unlock(current);
  1420. }
  1421. bad_unshare_cleanup_fd:
  1422. if (new_fd)
  1423. put_files_struct(new_fd);
  1424. bad_unshare_cleanup_vm:
  1425. if (new_mm)
  1426. mmput(new_mm);
  1427. bad_unshare_cleanup_sigh:
  1428. if (new_sigh)
  1429. if (atomic_dec_and_test(&new_sigh->count))
  1430. kmem_cache_free(sighand_cachep, new_sigh);
  1431. bad_unshare_cleanup_ns:
  1432. if (new_ns)
  1433. put_namespace(new_ns);
  1434. bad_unshare_cleanup_fs:
  1435. if (new_fs)
  1436. put_fs_struct(new_fs);
  1437. bad_unshare_cleanup_thread:
  1438. bad_unshare_out:
  1439. return err;
  1440. }