fork.c 40 KB

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