fork.c 39 KB

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