fork.c 40 KB

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