fork.c 40 KB

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