fork.c 39 KB

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