fork.c 39 KB

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