~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/fork.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/kernel/fork.c
  3  *
  4  *  Copyright (C) 1991, 1992  Linus Torvalds
  5  */
  6 
  7 /*
  8  *  'fork.c' contains the help-routines for the 'fork' system call
  9  * (see also entry.S and others).
 10  * Fork is rather simple, once you get the hang of it, but the memory
 11  * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
 12  */
 13 
 14 #include <linux/slab.h>
 15 #include <linux/init.h>
 16 #include <linux/unistd.h>
 17 #include <linux/module.h>
 18 #include <linux/vmalloc.h>
 19 #include <linux/completion.h>
 20 #include <linux/personality.h>
 21 #include <linux/mempolicy.h>
 22 #include <linux/sem.h>
 23 #include <linux/file.h>
 24 #include <linux/fdtable.h>
 25 #include <linux/iocontext.h>
 26 #include <linux/key.h>
 27 #include <linux/binfmts.h>
 28 #include <linux/mman.h>
 29 #include <linux/mmu_notifier.h>
 30 #include <linux/fs.h>
 31 #include <linux/mm.h>
 32 #include <linux/vmacache.h>
 33 #include <linux/nsproxy.h>
 34 #include <linux/capability.h>
 35 #include <linux/cpu.h>
 36 #include <linux/cgroup.h>
 37 #include <linux/security.h>
 38 #include <linux/hugetlb.h>
 39 #include <linux/seccomp.h>
 40 #include <linux/swap.h>
 41 #include <linux/syscalls.h>
 42 #include <linux/jiffies.h>
 43 #include <linux/futex.h>
 44 #include <linux/compat.h>
 45 #include <linux/kthread.h>
 46 #include <linux/task_io_accounting_ops.h>
 47 #include <linux/rcupdate.h>
 48 #include <linux/ptrace.h>
 49 #include <linux/mount.h>
 50 #include <linux/audit.h>
 51 #include <linux/memcontrol.h>
 52 #include <linux/ftrace.h>
 53 #include <linux/proc_fs.h>
 54 #include <linux/profile.h>
 55 #include <linux/rmap.h>
 56 #include <linux/ksm.h>
 57 #include <linux/acct.h>
 58 #include <linux/tsacct_kern.h>
 59 #include <linux/cn_proc.h>
 60 #include <linux/freezer.h>
 61 #include <linux/delayacct.h>
 62 #include <linux/taskstats_kern.h>
 63 #include <linux/random.h>
 64 #include <linux/tty.h>
 65 #include <linux/blkdev.h>
 66 #include <linux/fs_struct.h>
 67 #include <linux/magic.h>
 68 #include <linux/perf_event.h>
 69 #include <linux/posix-timers.h>
 70 #include <linux/user-return-notifier.h>
 71 #include <linux/oom.h>
 72 #include <linux/khugepaged.h>
 73 #include <linux/signalfd.h>
 74 #include <linux/uprobes.h>
 75 #include <linux/aio.h>
 76 #include <linux/compiler.h>
 77 #include <linux/sysctl.h>
 78 #include <linux/kcov.h>
 79 
 80 #include <asm/pgtable.h>
 81 #include <asm/pgalloc.h>
 82 #include <asm/uaccess.h>
 83 #include <asm/mmu_context.h>
 84 #include <asm/cacheflush.h>
 85 #include <asm/tlbflush.h>
 86 
 87 #include <trace/events/sched.h>
 88 
 89 #define CREATE_TRACE_POINTS
 90 #include <trace/events/task.h>
 91 
 92 /*
 93  * Minimum number of threads to boot the kernel
 94  */
 95 #define MIN_THREADS 20
 96 
 97 /*
 98  * Maximum number of threads
 99  */
100 #define MAX_THREADS FUTEX_TID_MASK
101 
102 /*
103  * Protected counters by write_lock_irq(&tasklist_lock)
104  */
105 unsigned long total_forks;      /* Handle normal Linux uptimes. */
106 int nr_threads;                 /* The idle threads do not count.. */
107 
108 int max_threads;                /* tunable limit on nr_threads */
109 
110 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
111 
112 __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
113 
114 #ifdef CONFIG_PROVE_RCU
115 int lockdep_tasklist_lock_is_held(void)
116 {
117         return lockdep_is_held(&tasklist_lock);
118 }
119 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
120 #endif /* #ifdef CONFIG_PROVE_RCU */
121 
122 int nr_processes(void)
123 {
124         int cpu;
125         int total = 0;
126 
127         for_each_possible_cpu(cpu)
128                 total += per_cpu(process_counts, cpu);
129 
130         return total;
131 }
132 
133 void __weak arch_release_task_struct(struct task_struct *tsk)
134 {
135 }
136 
137 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
138 static struct kmem_cache *task_struct_cachep;
139 
140 static inline struct task_struct *alloc_task_struct_node(int node)
141 {
142         return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
143 }
144 
145 static inline void free_task_struct(struct task_struct *tsk)
146 {
147         kmem_cache_free(task_struct_cachep, tsk);
148 }
149 #endif
150 
151 void __weak arch_release_thread_stack(unsigned long *stack)
152 {
153 }
154 
155 #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
156 
157 /*
158  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
159  * kmemcache based allocator.
160  */
161 # if THREAD_SIZE >= PAGE_SIZE
162 static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
163                                                   int node)
164 {
165         struct page *page = alloc_pages_node(node, THREADINFO_GFP,
166                                              THREAD_SIZE_ORDER);
167 
168         return page ? page_address(page) : NULL;
169 }
170 
171 static inline void free_thread_stack(unsigned long *stack)
172 {
173         __free_pages(virt_to_page(stack), THREAD_SIZE_ORDER);
174 }
175 # else
176 static struct kmem_cache *thread_stack_cache;
177 
178 static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
179                                                   int node)
180 {
181         return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
182 }
183 
184 static void free_thread_stack(unsigned long *stack)
185 {
186         kmem_cache_free(thread_stack_cache, stack);
187 }
188 
189 void thread_stack_cache_init(void)
190 {
191         thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE,
192                                               THREAD_SIZE, 0, NULL);
193         BUG_ON(thread_stack_cache == NULL);
194 }
195 # endif
196 #endif
197 
198 /* SLAB cache for signal_struct structures (tsk->signal) */
199 static struct kmem_cache *signal_cachep;
200 
201 /* SLAB cache for sighand_struct structures (tsk->sighand) */
202 struct kmem_cache *sighand_cachep;
203 
204 /* SLAB cache for files_struct structures (tsk->files) */
205 struct kmem_cache *files_cachep;
206 
207 /* SLAB cache for fs_struct structures (tsk->fs) */
208 struct kmem_cache *fs_cachep;
209 
210 /* SLAB cache for vm_area_struct structures */
211 struct kmem_cache *vm_area_cachep;
212 
213 /* SLAB cache for mm_struct structures (tsk->mm) */
214 static struct kmem_cache *mm_cachep;
215 
216 static void account_kernel_stack(unsigned long *stack, int account)
217 {
218         /* All stack pages are in the same zone and belong to the same memcg. */
219         struct page *first_page = virt_to_page(stack);
220 
221         mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
222                             THREAD_SIZE / 1024 * account);
223 
224         memcg_kmem_update_page_stat(
225                 first_page, MEMCG_KERNEL_STACK_KB,
226                 account * (THREAD_SIZE / 1024));
227 }
228 
229 void free_task(struct task_struct *tsk)
230 {
231         account_kernel_stack(tsk->stack, -1);
232         arch_release_thread_stack(tsk->stack);
233         free_thread_stack(tsk->stack);
234         rt_mutex_debug_task_free(tsk);
235         ftrace_graph_exit_task(tsk);
236         put_seccomp_filter(tsk);
237         arch_release_task_struct(tsk);
238         free_task_struct(tsk);
239 }
240 EXPORT_SYMBOL(free_task);
241 
242 static inline void free_signal_struct(struct signal_struct *sig)
243 {
244         taskstats_tgid_free(sig);
245         sched_autogroup_exit(sig);
246         kmem_cache_free(signal_cachep, sig);
247 }
248 
249 static inline void put_signal_struct(struct signal_struct *sig)
250 {
251         if (atomic_dec_and_test(&sig->sigcnt))
252                 free_signal_struct(sig);
253 }
254 
255 void __put_task_struct(struct task_struct *tsk)
256 {
257         WARN_ON(!tsk->exit_state);
258         WARN_ON(atomic_read(&tsk->usage));
259         WARN_ON(tsk == current);
260 
261         cgroup_free(tsk);
262         task_numa_free(tsk);
263         security_task_free(tsk);
264         exit_creds(tsk);
265         delayacct_tsk_free(tsk);
266         put_signal_struct(tsk->signal);
267 
268         ccs_free_task_security(tsk);
269         if (!profile_handoff_task(tsk))
270                 free_task(tsk);
271 }
272 EXPORT_SYMBOL_GPL(__put_task_struct);
273 
274 void __init __weak arch_task_cache_init(void) { }
275 
276 /*
277  * set_max_threads
278  */
279 static void set_max_threads(unsigned int max_threads_suggested)
280 {
281         u64 threads;
282 
283         /*
284          * The number of threads shall be limited such that the thread
285          * structures may only consume a small part of the available memory.
286          */
287         if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64)
288                 threads = MAX_THREADS;
289         else
290                 threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
291                                     (u64) THREAD_SIZE * 8UL);
292 
293         if (threads > max_threads_suggested)
294                 threads = max_threads_suggested;
295 
296         max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
297 }
298 
299 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
300 /* Initialized by the architecture: */
301 int arch_task_struct_size __read_mostly;
302 #endif
303 
304 void __init fork_init(void)
305 {
306 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
307 #ifndef ARCH_MIN_TASKALIGN
308 #define ARCH_MIN_TASKALIGN      L1_CACHE_BYTES
309 #endif
310         /* create a slab on which task_structs can be allocated */
311         task_struct_cachep = kmem_cache_create("task_struct",
312                         arch_task_struct_size, ARCH_MIN_TASKALIGN,
313                         SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL);
314 #endif
315 
316         /* do the arch specific task caches init */
317         arch_task_cache_init();
318 
319         set_max_threads(MAX_THREADS);
320 
321         init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
322         init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
323         init_task.signal->rlim[RLIMIT_SIGPENDING] =
324                 init_task.signal->rlim[RLIMIT_NPROC];
325 }
326 
327 int __weak arch_dup_task_struct(struct task_struct *dst,
328                                                struct task_struct *src)
329 {
330         *dst = *src;
331         return 0;
332 }
333 
334 void set_task_stack_end_magic(struct task_struct *tsk)
335 {
336         unsigned long *stackend;
337 
338         stackend = end_of_stack(tsk);
339         *stackend = STACK_END_MAGIC;    /* for overflow detection */
340 }
341 
342 static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
343 {
344         struct task_struct *tsk;
345         unsigned long *stack;
346         int err;
347 
348         if (node == NUMA_NO_NODE)
349                 node = tsk_fork_get_node(orig);
350         tsk = alloc_task_struct_node(node);
351         if (!tsk)
352                 return NULL;
353 
354         stack = alloc_thread_stack_node(tsk, node);
355         if (!stack)
356                 goto free_tsk;
357 
358         err = arch_dup_task_struct(tsk, orig);
359         if (err)
360                 goto free_stack;
361 
362         tsk->stack = stack;
363 #ifdef CONFIG_SECCOMP
364         /*
365          * We must handle setting up seccomp filters once we're under
366          * the sighand lock in case orig has changed between now and
367          * then. Until then, filter must be NULL to avoid messing up
368          * the usage counts on the error path calling free_task.
369          */
370         tsk->seccomp.filter = NULL;
371 #endif
372 
373         setup_thread_stack(tsk, orig);
374         clear_user_return_notifier(tsk);
375         clear_tsk_need_resched(tsk);
376         set_task_stack_end_magic(tsk);
377 
378 #ifdef CONFIG_CC_STACKPROTECTOR
379         tsk->stack_canary = get_random_int();
380 #endif
381 
382         /*
383          * One for us, one for whoever does the "release_task()" (usually
384          * parent)
385          */
386         atomic_set(&tsk->usage, 2);
387 #ifdef CONFIG_BLK_DEV_IO_TRACE
388         tsk->btrace_seq = 0;
389 #endif
390         tsk->splice_pipe = NULL;
391         tsk->task_frag.page = NULL;
392         tsk->wake_q.next = NULL;
393 
394         account_kernel_stack(stack, 1);
395 
396         kcov_task_init(tsk);
397 
398         return tsk;
399 
400 free_stack:
401         free_thread_stack(stack);
402 free_tsk:
403         free_task_struct(tsk);
404         return NULL;
405 }
406 
407 #ifdef CONFIG_MMU
408 static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
409 {
410         struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
411         struct rb_node **rb_link, *rb_parent;
412         int retval;
413         unsigned long charge;
414 
415         uprobe_start_dup_mmap();
416         if (down_write_killable(&oldmm->mmap_sem)) {
417                 retval = -EINTR;
418                 goto fail_uprobe_end;
419         }
420         flush_cache_dup_mm(oldmm);
421         uprobe_dup_mmap(oldmm, mm);
422         /*
423          * Not linked in yet - no deadlock potential:
424          */
425         down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
426 
427         /* No ordering required: file already has been exposed. */
428         RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
429 
430         mm->total_vm = oldmm->total_vm;
431         mm->data_vm = oldmm->data_vm;
432         mm->exec_vm = oldmm->exec_vm;
433         mm->stack_vm = oldmm->stack_vm;
434 
435         rb_link = &mm->mm_rb.rb_node;
436         rb_parent = NULL;
437         pprev = &mm->mmap;
438         retval = ksm_fork(mm, oldmm);
439         if (retval)
440                 goto out;
441         retval = khugepaged_fork(mm, oldmm);
442         if (retval)
443                 goto out;
444 
445         prev = NULL;
446         for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
447                 struct file *file;
448 
449                 if (mpnt->vm_flags & VM_DONTCOPY) {
450                         vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
451                         continue;
452                 }
453                 charge = 0;
454                 if (mpnt->vm_flags & VM_ACCOUNT) {
455                         unsigned long len = vma_pages(mpnt);
456 
457                         if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
458                                 goto fail_nomem;
459                         charge = len;
460                 }
461                 tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
462                 if (!tmp)
463                         goto fail_nomem;
464                 *tmp = *mpnt;
465                 INIT_LIST_HEAD(&tmp->anon_vma_chain);
466                 retval = vma_dup_policy(mpnt, tmp);
467                 if (retval)
468                         goto fail_nomem_policy;
469                 tmp->vm_mm = mm;
470                 if (anon_vma_fork(tmp, mpnt))
471                         goto fail_nomem_anon_vma_fork;
472                 tmp->vm_flags &=
473                         ~(VM_LOCKED|VM_LOCKONFAULT|VM_UFFD_MISSING|VM_UFFD_WP);
474                 tmp->vm_next = tmp->vm_prev = NULL;
475                 tmp->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
476                 file = tmp->vm_file;
477                 if (file) {
478                         struct inode *inode = file_inode(file);
479                         struct address_space *mapping = file->f_mapping;
480 
481                         get_file(file);
482                         if (tmp->vm_flags & VM_DENYWRITE)
483                                 atomic_dec(&inode->i_writecount);
484                         i_mmap_lock_write(mapping);
485                         if (tmp->vm_flags & VM_SHARED)
486                                 atomic_inc(&mapping->i_mmap_writable);
487                         flush_dcache_mmap_lock(mapping);
488                         /* insert tmp into the share list, just after mpnt */
489                         vma_interval_tree_insert_after(tmp, mpnt,
490                                         &mapping->i_mmap);
491                         flush_dcache_mmap_unlock(mapping);
492                         i_mmap_unlock_write(mapping);
493                 }
494 
495                 /*
496                  * Clear hugetlb-related page reserves for children. This only
497                  * affects MAP_PRIVATE mappings. Faults generated by the child
498                  * are not guaranteed to succeed, even if read-only
499                  */
500                 if (is_vm_hugetlb_page(tmp))
501                         reset_vma_resv_huge_pages(tmp);
502 
503                 /*
504                  * Link in the new vma and copy the page table entries.
505                  */
506                 *pprev = tmp;
507                 pprev = &tmp->vm_next;
508                 tmp->vm_prev = prev;
509                 prev = tmp;
510 
511                 __vma_link_rb(mm, tmp, rb_link, rb_parent);
512                 rb_link = &tmp->vm_rb.rb_right;
513                 rb_parent = &tmp->vm_rb;
514 
515                 mm->map_count++;
516                 retval = copy_page_range(mm, oldmm, mpnt);
517 
518                 if (tmp->vm_ops && tmp->vm_ops->open)
519                         tmp->vm_ops->open(tmp);
520 
521                 if (retval)
522                         goto out;
523         }
524         /* a new mm has just been created */
525         arch_dup_mmap(oldmm, mm);
526         retval = 0;
527 out:
528         up_write(&mm->mmap_sem);
529         flush_tlb_mm(oldmm);
530         up_write(&oldmm->mmap_sem);
531 fail_uprobe_end:
532         uprobe_end_dup_mmap();
533         return retval;
534 fail_nomem_anon_vma_fork:
535         mpol_put(vma_policy(tmp));
536 fail_nomem_policy:
537         kmem_cache_free(vm_area_cachep, tmp);
538 fail_nomem:
539         retval = -ENOMEM;
540         vm_unacct_memory(charge);
541         goto out;
542 }
543 
544 static inline int mm_alloc_pgd(struct mm_struct *mm)
545 {
546         mm->pgd = pgd_alloc(mm);
547         if (unlikely(!mm->pgd))
548                 return -ENOMEM;
549         return 0;
550 }
551 
552 static inline void mm_free_pgd(struct mm_struct *mm)
553 {
554         pgd_free(mm, mm->pgd);
555 }
556 #else
557 static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
558 {
559         down_write(&oldmm->mmap_sem);
560         RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
561         up_write(&oldmm->mmap_sem);
562         return 0;
563 }
564 #define mm_alloc_pgd(mm)        (0)
565 #define mm_free_pgd(mm)
566 #endif /* CONFIG_MMU */
567 
568 __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
569 
570 #define allocate_mm()   (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
571 #define free_mm(mm)     (kmem_cache_free(mm_cachep, (mm)))
572 
573 static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
574 
575 static int __init coredump_filter_setup(char *s)
576 {
577         default_dump_filter =
578                 (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
579                 MMF_DUMP_FILTER_MASK;
580         return 1;
581 }
582 
583 __setup("coredump_filter=", coredump_filter_setup);
584 
585 #include <linux/init_task.h>
586 
587 static void mm_init_aio(struct mm_struct *mm)
588 {
589 #ifdef CONFIG_AIO
590         spin_lock_init(&mm->ioctx_lock);
591         mm->ioctx_table = NULL;
592 #endif
593 }
594 
595 static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
596 {
597 #ifdef CONFIG_MEMCG
598         mm->owner = p;
599 #endif
600 }
601 
602 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
603         struct user_namespace *user_ns)
604 {
605         mm->mmap = NULL;
606         mm->mm_rb = RB_ROOT;
607         mm->vmacache_seqnum = 0;
608         atomic_set(&mm->mm_users, 1);
609         atomic_set(&mm->mm_count, 1);
610         init_rwsem(&mm->mmap_sem);
611         INIT_LIST_HEAD(&mm->mmlist);
612         mm->core_state = NULL;
613         atomic_long_set(&mm->nr_ptes, 0);
614         mm_nr_pmds_init(mm);
615         mm->map_count = 0;
616         mm->locked_vm = 0;
617         mm->pinned_vm = 0;
618         memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
619         spin_lock_init(&mm->page_table_lock);
620         mm_init_cpumask(mm);
621         mm_init_aio(mm);
622         mm_init_owner(mm, p);
623         mmu_notifier_mm_init(mm);
624         clear_tlb_flush_pending(mm);
625 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
626         mm->pmd_huge_pte = NULL;
627 #endif
628 
629         if (current->mm) {
630                 mm->flags = current->mm->flags & MMF_INIT_MASK;
631                 mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
632         } else {
633                 mm->flags = default_dump_filter;
634                 mm->def_flags = 0;
635         }
636 
637         if (mm_alloc_pgd(mm))
638                 goto fail_nopgd;
639 
640         if (init_new_context(p, mm))
641                 goto fail_nocontext;
642 
643         mm->user_ns = get_user_ns(user_ns);
644         return mm;
645 
646 fail_nocontext:
647         mm_free_pgd(mm);
648 fail_nopgd:
649         free_mm(mm);
650         return NULL;
651 }
652 
653 static void check_mm(struct mm_struct *mm)
654 {
655         int i;
656 
657         for (i = 0; i < NR_MM_COUNTERS; i++) {
658                 long x = atomic_long_read(&mm->rss_stat.count[i]);
659 
660                 if (unlikely(x))
661                         printk(KERN_ALERT "BUG: Bad rss-counter state "
662                                           "mm:%p idx:%d val:%ld\n", mm, i, x);
663         }
664 
665         if (atomic_long_read(&mm->nr_ptes))
666                 pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n",
667                                 atomic_long_read(&mm->nr_ptes));
668         if (mm_nr_pmds(mm))
669                 pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n",
670                                 mm_nr_pmds(mm));
671 
672 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
673         VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
674 #endif
675 }
676 
677 /*
678  * Allocate and initialize an mm_struct.
679  */
680 struct mm_struct *mm_alloc(void)
681 {
682         struct mm_struct *mm;
683 
684         mm = allocate_mm();
685         if (!mm)
686                 return NULL;
687 
688         memset(mm, 0, sizeof(*mm));
689         return mm_init(mm, current, current_user_ns());
690 }
691 
692 /*
693  * Called when the last reference to the mm
694  * is dropped: either by a lazy thread or by
695  * mmput. Free the page directory and the mm.
696  */
697 void __mmdrop(struct mm_struct *mm)
698 {
699         BUG_ON(mm == &init_mm);
700         mm_free_pgd(mm);
701         destroy_context(mm);
702         mmu_notifier_mm_destroy(mm);
703         check_mm(mm);
704         put_user_ns(mm->user_ns);
705         free_mm(mm);
706 }
707 EXPORT_SYMBOL_GPL(__mmdrop);
708 
709 static inline void __mmput(struct mm_struct *mm)
710 {
711         VM_BUG_ON(atomic_read(&mm->mm_users));
712 
713         uprobe_clear_state(mm);
714         exit_aio(mm);
715         ksm_exit(mm);
716         khugepaged_exit(mm); /* must run before exit_mmap */
717         exit_mmap(mm);
718         set_mm_exe_file(mm, NULL);
719         if (!list_empty(&mm->mmlist)) {
720                 spin_lock(&mmlist_lock);
721                 list_del(&mm->mmlist);
722                 spin_unlock(&mmlist_lock);
723         }
724         if (mm->binfmt)
725                 module_put(mm->binfmt->module);
726         mmdrop(mm);
727 }
728 
729 /*
730  * Decrement the use count and release all resources for an mm.
731  */
732 void mmput(struct mm_struct *mm)
733 {
734         might_sleep();
735 
736         if (atomic_dec_and_test(&mm->mm_users))
737                 __mmput(mm);
738 }
739 EXPORT_SYMBOL_GPL(mmput);
740 
741 #ifdef CONFIG_MMU
742 static void mmput_async_fn(struct work_struct *work)
743 {
744         struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work);
745         __mmput(mm);
746 }
747 
748 void mmput_async(struct mm_struct *mm)
749 {
750         if (atomic_dec_and_test(&mm->mm_users)) {
751                 INIT_WORK(&mm->async_put_work, mmput_async_fn);
752                 schedule_work(&mm->async_put_work);
753         }
754 }
755 #endif
756 
757 /**
758  * set_mm_exe_file - change a reference to the mm's executable file
759  *
760  * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
761  *
762  * Main users are mmput() and sys_execve(). Callers prevent concurrent
763  * invocations: in mmput() nobody alive left, in execve task is single
764  * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
765  * mm->exe_file, but does so without using set_mm_exe_file() in order
766  * to do avoid the need for any locks.
767  */
768 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
769 {
770         struct file *old_exe_file;
771 
772         /*
773          * It is safe to dereference the exe_file without RCU as
774          * this function is only called if nobody else can access
775          * this mm -- see comment above for justification.
776          */
777         old_exe_file = rcu_dereference_raw(mm->exe_file);
778 
779         if (new_exe_file)
780                 get_file(new_exe_file);
781         rcu_assign_pointer(mm->exe_file, new_exe_file);
782         if (old_exe_file)
783                 fput(old_exe_file);
784 }
785 
786 /**
787  * get_mm_exe_file - acquire a reference to the mm's executable file
788  *
789  * Returns %NULL if mm has no associated executable file.
790  * User must release file via fput().
791  */
792 struct file *get_mm_exe_file(struct mm_struct *mm)
793 {
794         struct file *exe_file;
795 
796         rcu_read_lock();
797         exe_file = rcu_dereference(mm->exe_file);
798         if (exe_file && !get_file_rcu(exe_file))
799                 exe_file = NULL;
800         rcu_read_unlock();
801         return exe_file;
802 }
803 EXPORT_SYMBOL(get_mm_exe_file);
804 
805 /**
806  * get_task_exe_file - acquire a reference to the task's executable file
807  *
808  * Returns %NULL if task's mm (if any) has no associated executable file or
809  * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
810  * User must release file via fput().
811  */
812 struct file *get_task_exe_file(struct task_struct *task)
813 {
814         struct file *exe_file = NULL;
815         struct mm_struct *mm;
816 
817         task_lock(task);
818         mm = task->mm;
819         if (mm) {
820                 if (!(task->flags & PF_KTHREAD))
821                         exe_file = get_mm_exe_file(mm);
822         }
823         task_unlock(task);
824         return exe_file;
825 }
826 EXPORT_SYMBOL(get_task_exe_file);
827 
828 /**
829  * get_task_mm - acquire a reference to the task's mm
830  *
831  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
832  * this kernel workthread has transiently adopted a user mm with use_mm,
833  * to do its AIO) is not set and if so returns a reference to it, after
834  * bumping up the use count.  User must release the mm via mmput()
835  * after use.  Typically used by /proc and ptrace.
836  */
837 struct mm_struct *get_task_mm(struct task_struct *task)
838 {
839         struct mm_struct *mm;
840 
841         task_lock(task);
842         mm = task->mm;
843         if (mm) {
844                 if (task->flags & PF_KTHREAD)
845                         mm = NULL;
846                 else
847                         atomic_inc(&mm->mm_users);
848         }
849         task_unlock(task);
850         return mm;
851 }
852 EXPORT_SYMBOL_GPL(get_task_mm);
853 
854 struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
855 {
856         struct mm_struct *mm;
857         int err;
858 
859         err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
860         if (err)
861                 return ERR_PTR(err);
862 
863         mm = get_task_mm(task);
864         if (mm && mm != current->mm &&
865                         !ptrace_may_access(task, mode)) {
866                 mmput(mm);
867                 mm = ERR_PTR(-EACCES);
868         }
869         mutex_unlock(&task->signal->cred_guard_mutex);
870 
871         return mm;
872 }
873 
874 static void complete_vfork_done(struct task_struct *tsk)
875 {
876         struct completion *vfork;
877 
878         task_lock(tsk);
879         vfork = tsk->vfork_done;
880         if (likely(vfork)) {
881                 tsk->vfork_done = NULL;
882                 complete(vfork);
883         }
884         task_unlock(tsk);
885 }
886 
887 static int wait_for_vfork_done(struct task_struct *child,
888                                 struct completion *vfork)
889 {
890         int killed;
891 
892         freezer_do_not_count();
893         killed = wait_for_completion_killable(vfork);
894         freezer_count();
895 
896         if (killed) {
897                 task_lock(child);
898                 child->vfork_done = NULL;
899                 task_unlock(child);
900         }
901 
902         put_task_struct(child);
903         return killed;
904 }
905 
906 /* Please note the differences between mmput and mm_release.
907  * mmput is called whenever we stop holding onto a mm_struct,
908  * error success whatever.
909  *
910  * mm_release is called after a mm_struct has been removed
911  * from the current process.
912  *
913  * This difference is important for error handling, when we
914  * only half set up a mm_struct for a new process and need to restore
915  * the old one.  Because we mmput the new mm_struct before
916  * restoring the old one. . .
917  * Eric Biederman 10 January 1998
918  */
919 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
920 {
921         /* Get rid of any futexes when releasing the mm */
922 #ifdef CONFIG_FUTEX
923         if (unlikely(tsk->robust_list)) {
924                 exit_robust_list(tsk);
925                 tsk->robust_list = NULL;
926         }
927 #ifdef CONFIG_COMPAT
928         if (unlikely(tsk->compat_robust_list)) {
929                 compat_exit_robust_list(tsk);
930                 tsk->compat_robust_list = NULL;
931         }
932 #endif
933         if (unlikely(!list_empty(&tsk->pi_state_list)))
934                 exit_pi_state_list(tsk);
935 #endif
936 
937         uprobe_free_utask(tsk);
938 
939         /* Get rid of any cached register state */
940         deactivate_mm(tsk, mm);
941 
942         /*
943          * Signal userspace if we're not exiting with a core dump
944          * because we want to leave the value intact for debugging
945          * purposes.
946          */
947         if (tsk->clear_child_tid) {
948                 if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) &&
949                     atomic_read(&mm->mm_users) > 1) {
950                         /*
951                          * We don't check the error code - if userspace has
952                          * not set up a proper pointer then tough luck.
953                          */
954                         put_user(0, tsk->clear_child_tid);
955                         sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
956                                         1, NULL, NULL, 0);
957                 }
958                 tsk->clear_child_tid = NULL;
959         }
960 
961         /*
962          * All done, finally we can wake up parent and return this mm to him.
963          * Also kthread_stop() uses this completion for synchronization.
964          */
965         if (tsk->vfork_done)
966                 complete_vfork_done(tsk);
967 }
968 
969 /*
970  * Allocate a new mm structure and copy contents from the
971  * mm structure of the passed in task structure.
972  */
973 static struct mm_struct *dup_mm(struct task_struct *tsk)
974 {
975         struct mm_struct *mm, *oldmm = current->mm;
976         int err;
977 
978         mm = allocate_mm();
979         if (!mm)
980                 goto fail_nomem;
981 
982         memcpy(mm, oldmm, sizeof(*mm));
983 
984         if (!mm_init(mm, tsk, mm->user_ns))
985                 goto fail_nomem;
986 
987         err = dup_mmap(mm, oldmm);
988         if (err)
989                 goto free_pt;
990 
991         mm->hiwater_rss = get_mm_rss(mm);
992         mm->hiwater_vm = mm->total_vm;
993 
994         if (mm->binfmt && !try_module_get(mm->binfmt->module))
995                 goto free_pt;
996 
997         return mm;
998 
999 free_pt:
1000         /* don't put binfmt in mmput, we haven't got module yet */
1001         mm->binfmt = NULL;
1002         mmput(mm);
1003 
1004 fail_nomem:
1005         return NULL;
1006 }
1007 
1008 static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
1009 {
1010         struct mm_struct *mm, *oldmm;
1011         int retval;
1012 
1013         tsk->min_flt = tsk->maj_flt = 0;
1014         tsk->nvcsw = tsk->nivcsw = 0;
1015 #ifdef CONFIG_DETECT_HUNG_TASK
1016         tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
1017 #endif
1018 
1019         tsk->mm = NULL;
1020         tsk->active_mm = NULL;
1021 
1022         /*
1023          * Are we cloning a kernel thread?
1024          *
1025          * We need to steal a active VM for that..
1026          */
1027         oldmm = current->mm;
1028         if (!oldmm)
1029                 return 0;
1030 
1031         /* initialize the new vmacache entries */
1032         vmacache_flush(tsk);
1033 
1034         if (clone_flags & CLONE_VM) {
1035                 atomic_inc(&oldmm->mm_users);
1036                 mm = oldmm;
1037                 goto good_mm;
1038         }
1039 
1040         retval = -ENOMEM;
1041         mm = dup_mm(tsk);
1042         if (!mm)
1043                 goto fail_nomem;
1044 
1045 good_mm:
1046         tsk->mm = mm;
1047         tsk->active_mm = mm;
1048         return 0;
1049 
1050 fail_nomem:
1051         return retval;
1052 }
1053 
1054 static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
1055 {
1056         struct fs_struct *fs = current->fs;
1057         if (clone_flags & CLONE_FS) {
1058                 /* tsk->fs is already what we want */
1059                 spin_lock(&fs->lock);
1060                 if (fs->in_exec) {
1061                         spin_unlock(&fs->lock);
1062                         return -EAGAIN;
1063                 }
1064                 fs->users++;
1065                 spin_unlock(&fs->lock);
1066                 return 0;
1067         }
1068         tsk->fs = copy_fs_struct(fs);
1069         if (!tsk->fs)
1070                 return -ENOMEM;
1071         return 0;
1072 }
1073 
1074 static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
1075 {
1076         struct files_struct *oldf, *newf;
1077         int error = 0;
1078 
1079         /*
1080          * A background process may not have any files ...
1081          */
1082         oldf = current->files;
1083         if (!oldf)
1084                 goto out;
1085 
1086         if (clone_flags & CLONE_FILES) {
1087                 atomic_inc(&oldf->count);
1088                 goto out;
1089         }
1090 
1091         newf = dup_fd(oldf, &error);
1092         if (!newf)
1093                 goto out;
1094 
1095         tsk->files = newf;
1096         error = 0;
1097 out:
1098         return error;
1099 }
1100 
1101 static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
1102 {
1103 #ifdef CONFIG_BLOCK
1104         struct io_context *ioc = current->io_context;
1105         struct io_context *new_ioc;
1106 
1107         if (!ioc)
1108                 return 0;
1109         /*
1110          * Share io context with parent, if CLONE_IO is set
1111          */
1112         if (clone_flags & CLONE_IO) {
1113                 ioc_task_link(ioc);
1114                 tsk->io_context = ioc;
1115         } else if (ioprio_valid(ioc->ioprio)) {
1116                 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
1117                 if (unlikely(!new_ioc))
1118                         return -ENOMEM;
1119 
1120                 new_ioc->ioprio = ioc->ioprio;
1121                 put_io_context(new_ioc);
1122         }
1123 #endif
1124         return 0;
1125 }
1126 
1127 static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
1128 {
1129         struct sighand_struct *sig;
1130 
1131         if (clone_flags & CLONE_SIGHAND) {
1132                 atomic_inc(&current->sighand->count);
1133                 return 0;
1134         }
1135         sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1136         rcu_assign_pointer(tsk->sighand, sig);
1137         if (!sig)
1138                 return -ENOMEM;
1139 
1140         atomic_set(&sig->count, 1);
1141         memcpy(sig->action, current->sighand->action, sizeof(sig->action));
1142         return 0;
1143 }
1144 
1145 void __cleanup_sighand(struct sighand_struct *sighand)
1146 {
1147         if (atomic_dec_and_test(&sighand->count)) {
1148                 signalfd_cleanup(sighand);
1149                 /*
1150                  * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it
1151                  * without an RCU grace period, see __lock_task_sighand().
1152                  */
1153                 kmem_cache_free(sighand_cachep, sighand);
1154         }
1155 }
1156 
1157 /*
1158  * Initialize POSIX timer handling for a thread group.
1159  */
1160 static void posix_cpu_timers_init_group(struct signal_struct *sig)
1161 {
1162         unsigned long cpu_limit;
1163 
1164         cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
1165         if (cpu_limit != RLIM_INFINITY) {
1166                 sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
1167                 sig->cputimer.running = true;
1168         }
1169 
1170         /* The timer lists. */
1171         INIT_LIST_HEAD(&sig->cpu_timers[0]);
1172         INIT_LIST_HEAD(&sig->cpu_timers[1]);
1173         INIT_LIST_HEAD(&sig->cpu_timers[2]);
1174 }
1175 
1176 static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
1177 {
1178         struct signal_struct *sig;
1179 
1180         if (clone_flags & CLONE_THREAD)
1181                 return 0;
1182 
1183         sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
1184         tsk->signal = sig;
1185         if (!sig)
1186                 return -ENOMEM;
1187 
1188         sig->nr_threads = 1;
1189         atomic_set(&sig->live, 1);
1190         atomic_set(&sig->sigcnt, 1);
1191 
1192         /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
1193         sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
1194         tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
1195 
1196         init_waitqueue_head(&sig->wait_chldexit);
1197         sig->curr_target = tsk;
1198         init_sigpending(&sig->shared_pending);
1199         INIT_LIST_HEAD(&sig->posix_timers);
1200         seqlock_init(&sig->stats_lock);
1201         prev_cputime_init(&sig->prev_cputime);
1202 
1203         hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1204         sig->real_timer.function = it_real_fn;
1205 
1206         task_lock(current->group_leader);
1207         memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
1208         task_unlock(current->group_leader);
1209 
1210         posix_cpu_timers_init_group(sig);
1211 
1212         tty_audit_fork(sig);
1213         sched_autogroup_fork(sig);
1214 
1215         sig->oom_score_adj = current->signal->oom_score_adj;
1216         sig->oom_score_adj_min = current->signal->oom_score_adj_min;
1217 
1218         sig->has_child_subreaper = current->signal->has_child_subreaper ||
1219                                    current->signal->is_child_subreaper;
1220 
1221         mutex_init(&sig->cred_guard_mutex);
1222 
1223         return 0;
1224 }
1225 
1226 static void copy_seccomp(struct task_struct *p)
1227 {
1228 #ifdef CONFIG_SECCOMP
1229         /*
1230          * Must be called with sighand->lock held, which is common to
1231          * all threads in the group. Holding cred_guard_mutex is not
1232          * needed because this new task is not yet running and cannot
1233          * be racing exec.
1234          */
1235         assert_spin_locked(&current->sighand->siglock);
1236 
1237         /* Ref-count the new filter user, and assign it. */
1238         get_seccomp_filter(current);
1239         p->seccomp = current->seccomp;
1240 
1241         /*
1242          * Explicitly enable no_new_privs here in case it got set
1243          * between the task_struct being duplicated and holding the
1244          * sighand lock. The seccomp state and nnp must be in sync.
1245          */
1246         if (task_no_new_privs(current))
1247                 task_set_no_new_privs(p);
1248 
1249         /*
1250          * If the parent gained a seccomp mode after copying thread
1251          * flags and between before we held the sighand lock, we have
1252          * to manually enable the seccomp thread flag here.
1253          */
1254         if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
1255                 set_tsk_thread_flag(p, TIF_SECCOMP);
1256 #endif
1257 }
1258 
1259 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
1260 {
1261         current->clear_child_tid = tidptr;
1262 
1263         return task_pid_vnr(current);
1264 }
1265 
1266 static void rt_mutex_init_task(struct task_struct *p)
1267 {
1268         raw_spin_lock_init(&p->pi_lock);
1269 #ifdef CONFIG_RT_MUTEXES
1270         p->pi_waiters = RB_ROOT;
1271         p->pi_waiters_leftmost = NULL;
1272         p->pi_blocked_on = NULL;
1273 #endif
1274 }
1275 
1276 /*
1277  * Initialize POSIX timer handling for a single task.
1278  */
1279 static void posix_cpu_timers_init(struct task_struct *tsk)
1280 {
1281         tsk->cputime_expires.prof_exp = 0;
1282         tsk->cputime_expires.virt_exp = 0;
1283         tsk->cputime_expires.sched_exp = 0;
1284         INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1285         INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1286         INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1287 }
1288 
1289 static inline void
1290 init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
1291 {
1292          task->pids[type].pid = pid;
1293 }
1294 
1295 /*
1296  * This creates a new process as a copy of the old one,
1297  * but does not actually start it yet.
1298  *
1299  * It copies the registers, and all the appropriate
1300  * parts of the process environment (as per the clone
1301  * flags). The actual kick-off is left to the caller.
1302  */
1303 static struct task_struct *copy_process(unsigned long clone_flags,
1304                                         unsigned long stack_start,
1305                                         unsigned long stack_size,
1306                                         int __user *child_tidptr,
1307                                         struct pid *pid,
1308                                         int trace,
1309                                         unsigned long tls,
1310                                         int node)
1311 {
1312         int retval;
1313         struct task_struct *p;
1314 
1315         if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
1316                 return ERR_PTR(-EINVAL);
1317 
1318         if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
1319                 return ERR_PTR(-EINVAL);
1320 
1321         /*
1322          * Thread groups must share signals as well, and detached threads
1323          * can only be started up within the thread group.
1324          */
1325         if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
1326                 return ERR_PTR(-EINVAL);
1327 
1328         /*
1329          * Shared signal handlers imply shared VM. By way of the above,
1330          * thread groups also imply shared VM. Blocking this case allows
1331          * for various simplifications in other code.
1332          */
1333         if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
1334                 return ERR_PTR(-EINVAL);
1335 
1336         /*
1337          * Siblings of global init remain as zombies on exit since they are
1338          * not reaped by their parent (swapper). To solve this and to avoid
1339          * multi-rooted process trees, prevent global and container-inits
1340          * from creating siblings.
1341          */
1342         if ((clone_flags & CLONE_PARENT) &&
1343                                 current->signal->flags & SIGNAL_UNKILLABLE)
1344                 return ERR_PTR(-EINVAL);
1345 
1346         /*
1347          * If the new process will be in a different pid or user namespace
1348          * do not allow it to share a thread group with the forking task.
1349          */
1350         if (clone_flags & CLONE_THREAD) {
1351                 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
1352                     (task_active_pid_ns(current) !=
1353                                 current->nsproxy->pid_ns_for_children))
1354                         return ERR_PTR(-EINVAL);
1355         }
1356 
1357         retval = security_task_create(clone_flags);
1358         if (retval)
1359                 goto fork_out;
1360 
1361         retval = -ENOMEM;
1362         p = dup_task_struct(current, node);
1363         if (!p)
1364                 goto fork_out;
1365 
1366         ftrace_graph_init_task(p);
1367 
1368         rt_mutex_init_task(p);
1369 
1370 #ifdef CONFIG_PROVE_LOCKING
1371         DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1372         DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1373 #endif
1374         retval = -EAGAIN;
1375         if (atomic_read(&p->real_cred->user->processes) >=
1376                         task_rlimit(p, RLIMIT_NPROC)) {
1377                 if (p->real_cred->user != INIT_USER &&
1378                     !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
1379                         goto bad_fork_free;
1380         }
1381         current->flags &= ~PF_NPROC_EXCEEDED;
1382 
1383         retval = copy_creds(p, clone_flags);
1384         if (retval < 0)
1385                 goto bad_fork_free;
1386 
1387         /*
1388          * If multiple threads are within copy_process(), then this check
1389          * triggers too late. This doesn't hurt, the check is only there
1390          * to stop root fork bombs.
1391          */
1392         retval = -EAGAIN;
1393         if (nr_threads >= max_threads)
1394                 goto bad_fork_cleanup_count;
1395 
1396         delayacct_tsk_init(p);  /* Must remain after dup_task_struct() */
1397         p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
1398         p->flags |= PF_FORKNOEXEC;
1399         INIT_LIST_HEAD(&p->children);
1400         INIT_LIST_HEAD(&p->sibling);
1401         rcu_copy_process(p);
1402         p->vfork_done = NULL;
1403         spin_lock_init(&p->alloc_lock);
1404 
1405         init_sigpending(&p->pending);
1406 
1407         p->utime = p->stime = p->gtime = 0;
1408         p->utimescaled = p->stimescaled = 0;
1409         prev_cputime_init(&p->prev_cputime);
1410 
1411 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1412         seqcount_init(&p->vtime_seqcount);
1413         p->vtime_snap = 0;
1414         p->vtime_snap_whence = VTIME_INACTIVE;
1415 #endif
1416 
1417 #if defined(SPLIT_RSS_COUNTING)
1418         memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1419 #endif
1420 
1421         p->default_timer_slack_ns = current->timer_slack_ns;
1422 
1423         task_io_accounting_init(&p->ioac);
1424         acct_clear_integrals(p);
1425 
1426         posix_cpu_timers_init(p);
1427 
1428         p->start_time = ktime_get_ns();
1429         p->real_start_time = ktime_get_boot_ns();
1430         p->io_context = NULL;
1431         p->audit_context = NULL;
1432         cgroup_fork(p);
1433 #ifdef CONFIG_NUMA
1434         p->mempolicy = mpol_dup(p->mempolicy);
1435         if (IS_ERR(p->mempolicy)) {
1436                 retval = PTR_ERR(p->mempolicy);
1437                 p->mempolicy = NULL;
1438                 goto bad_fork_cleanup_threadgroup_lock;
1439         }
1440 #endif
1441 #ifdef CONFIG_CPUSETS
1442         p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
1443         p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
1444         seqcount_init(&p->mems_allowed_seq);
1445 #endif
1446 #ifdef CONFIG_TRACE_IRQFLAGS
1447         p->irq_events = 0;
1448         p->hardirqs_enabled = 0;
1449         p->hardirq_enable_ip = 0;
1450         p->hardirq_enable_event = 0;
1451         p->hardirq_disable_ip = _THIS_IP_;
1452         p->hardirq_disable_event = 0;
1453         p->softirqs_enabled = 1;
1454         p->softirq_enable_ip = _THIS_IP_;
1455         p->softirq_enable_event = 0;
1456         p->softirq_disable_ip = 0;
1457         p->softirq_disable_event = 0;
1458         p->hardirq_context = 0;
1459         p->softirq_context = 0;
1460 #endif
1461 
1462         p->pagefault_disabled = 0;
1463 
1464 #ifdef CONFIG_LOCKDEP
1465         p->lockdep_depth = 0; /* no locks held yet */
1466         p->curr_chain_key = 0;
1467         p->lockdep_recursion = 0;
1468 #endif
1469 
1470 #ifdef CONFIG_DEBUG_MUTEXES
1471         p->blocked_on = NULL; /* not blocked yet */
1472 #endif
1473 #ifdef CONFIG_BCACHE
1474         p->sequential_io        = 0;
1475         p->sequential_io_avg    = 0;
1476 #endif
1477 
1478         /* Perform scheduler related setup. Assign this task to a CPU. */
1479         retval = sched_fork(clone_flags, p);
1480         if (retval)
1481                 goto bad_fork_cleanup_policy;
1482 
1483         retval = perf_event_init_task(p);
1484         if (retval)
1485                 goto bad_fork_cleanup_policy;
1486         retval = audit_alloc(p);
1487         if (retval)
1488                 goto bad_fork_cleanup_perf;
1489         /* copy all the process information */
1490         shm_init_task(p);
1491         retval = ccs_alloc_task_security(p);
1492         if (retval)
1493                 goto bad_fork_cleanup_audit;
1494         retval = copy_semundo(clone_flags, p);
1495         if (retval)
1496                 goto bad_fork_cleanup_audit;
1497         retval = copy_files(clone_flags, p);
1498         if (retval)
1499                 goto bad_fork_cleanup_semundo;
1500         retval = copy_fs(clone_flags, p);
1501         if (retval)
1502                 goto bad_fork_cleanup_files;
1503         retval = copy_sighand(clone_flags, p);
1504         if (retval)
1505                 goto bad_fork_cleanup_fs;
1506         retval = copy_signal(clone_flags, p);
1507         if (retval)
1508                 goto bad_fork_cleanup_sighand;
1509         retval = copy_mm(clone_flags, p);
1510         if (retval)
1511                 goto bad_fork_cleanup_signal;
1512         retval = copy_namespaces(clone_flags, p);
1513         if (retval)
1514                 goto bad_fork_cleanup_mm;
1515         retval = copy_io(clone_flags, p);
1516         if (retval)
1517                 goto bad_fork_cleanup_namespaces;
1518         retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
1519         if (retval)
1520                 goto bad_fork_cleanup_io;
1521 
1522         if (pid != &init_struct_pid) {
1523                 pid = alloc_pid(p->nsproxy->pid_ns_for_children);
1524                 if (IS_ERR(pid)) {
1525                         retval = PTR_ERR(pid);
1526                         goto bad_fork_cleanup_thread;
1527                 }
1528         }
1529 
1530         p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1531         /*
1532          * Clear TID on mm_release()?
1533          */
1534         p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
1535 #ifdef CONFIG_BLOCK
1536         p->plug = NULL;
1537 #endif
1538 #ifdef CONFIG_FUTEX
1539         p->robust_list = NULL;
1540 #ifdef CONFIG_COMPAT
1541         p->compat_robust_list = NULL;
1542 #endif
1543         INIT_LIST_HEAD(&p->pi_state_list);
1544         p->pi_state_cache = NULL;
1545 #endif
1546         /*
1547          * sigaltstack should be cleared when sharing the same VM
1548          */
1549         if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1550                 sas_ss_reset(p);
1551 
1552         /*
1553          * Syscall tracing and stepping should be turned off in the
1554          * child regardless of CLONE_PTRACE.
1555          */
1556         user_disable_single_step(p);
1557         clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1558 #ifdef TIF_SYSCALL_EMU
1559         clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1560 #endif
1561         clear_all_latency_tracing(p);
1562 
1563         /* ok, now we should be set up.. */
1564         p->pid = pid_nr(pid);
1565         if (clone_flags & CLONE_THREAD) {
1566                 p->exit_signal = -1;
1567                 p->group_leader = current->group_leader;
1568                 p->tgid = current->tgid;
1569         } else {
1570                 if (clone_flags & CLONE_PARENT)
1571                         p->exit_signal = current->group_leader->exit_signal;
1572                 else
1573                         p->exit_signal = (clone_flags & CSIGNAL);
1574                 p->group_leader = p;
1575                 p->tgid = p->pid;
1576         }
1577 
1578         p->nr_dirtied = 0;
1579         p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
1580         p->dirty_paused_when = 0;
1581 
1582         p->pdeath_signal = 0;
1583         INIT_LIST_HEAD(&p->thread_group);
1584         p->task_works = NULL;
1585 
1586         threadgroup_change_begin(current);
1587         /*
1588          * Ensure that the cgroup subsystem policies allow the new process to be
1589          * forked. It should be noted the the new process's css_set can be changed
1590          * between here and cgroup_post_fork() if an organisation operation is in
1591          * progress.
1592          */
1593         retval = cgroup_can_fork(p);
1594         if (retval)
1595                 goto bad_fork_free_pid;
1596 
1597         /*
1598          * Make it visible to the rest of the system, but dont wake it up yet.
1599          * Need tasklist lock for parent etc handling!
1600          */
1601         write_lock_irq(&tasklist_lock);
1602 
1603         /* CLONE_PARENT re-uses the old parent */
1604         if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
1605                 p->real_parent = current->real_parent;
1606                 p->parent_exec_id = current->parent_exec_id;
1607         } else {
1608                 p->real_parent = current;
1609                 p->parent_exec_id = current->self_exec_id;
1610         }
1611 
1612         spin_lock(&current->sighand->siglock);
1613 
1614         /*
1615          * Copy seccomp details explicitly here, in case they were changed
1616          * before holding sighand lock.
1617          */
1618         copy_seccomp(p);
1619 
1620         /*
1621          * Process group and session signals need to be delivered to just the
1622          * parent before the fork or both the parent and the child after the
1623          * fork. Restart if a signal comes in before we add the new process to
1624          * it's process group.
1625          * A fatal signal pending means that current will exit, so the new
1626          * thread can't slip out of an OOM kill (or normal SIGKILL).
1627         */
1628         recalc_sigpending();
1629         if (signal_pending(current)) {
1630                 spin_unlock(&current->sighand->siglock);
1631                 write_unlock_irq(&tasklist_lock);
1632                 retval = -ERESTARTNOINTR;
1633                 goto bad_fork_cancel_cgroup;
1634         }
1635 
1636         if (likely(p->pid)) {
1637                 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
1638 
1639                 init_task_pid(p, PIDTYPE_PID, pid);
1640                 if (thread_group_leader(p)) {
1641                         init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
1642                         init_task_pid(p, PIDTYPE_SID, task_session(current));
1643 
1644                         if (is_child_reaper(pid)) {
1645                                 ns_of_pid(pid)->child_reaper = p;
1646                                 p->signal->flags |= SIGNAL_UNKILLABLE;
1647                         }
1648 
1649                         p->signal->leader_pid = pid;
1650                         p->signal->tty = tty_kref_get(current->signal->tty);
1651                         list_add_tail(&p->sibling, &p->real_parent->children);
1652                         list_add_tail_rcu(&p->tasks, &init_task.tasks);
1653                         attach_pid(p, PIDTYPE_PGID);
1654                         attach_pid(p, PIDTYPE_SID);
1655                         __this_cpu_inc(process_counts);
1656                 } else {
1657                         current->signal->nr_threads++;
1658                         atomic_inc(&current->signal->live);
1659                         atomic_inc(&current->signal->sigcnt);
1660                         list_add_tail_rcu(&p->thread_group,
1661                                           &p->group_leader->thread_group);
1662                         list_add_tail_rcu(&p->thread_node,
1663                                           &p->signal->thread_head);
1664                 }
1665                 attach_pid(p, PIDTYPE_PID);
1666                 nr_threads++;
1667         }
1668 
1669         total_forks++;
1670         spin_unlock(&current->sighand->siglock);
1671         syscall_tracepoint_update(p);
1672         write_unlock_irq(&tasklist_lock);
1673 
1674         proc_fork_connector(p);
1675         cgroup_post_fork(p);
1676         threadgroup_change_end(current);
1677         perf_event_fork(p);
1678 
1679         trace_task_newtask(p, clone_flags);
1680         uprobe_copy_process(p, clone_flags);
1681 
1682         return p;
1683 
1684 bad_fork_cancel_cgroup:
1685         cgroup_cancel_fork(p);
1686 bad_fork_free_pid:
1687         threadgroup_change_end(current);
1688         if (pid != &init_struct_pid)
1689                 free_pid(pid);
1690 bad_fork_cleanup_thread:
1691         exit_thread(p);
1692 bad_fork_cleanup_io:
1693         if (p->io_context)
1694                 exit_io_context(p);
1695 bad_fork_cleanup_namespaces:
1696         exit_task_namespaces(p);
1697 bad_fork_cleanup_mm:
1698         if (p->mm)
1699                 mmput(p->mm);
1700 bad_fork_cleanup_signal:
1701         if (!(clone_flags & CLONE_THREAD))
1702                 free_signal_struct(p->signal);
1703 bad_fork_cleanup_sighand:
1704         __cleanup_sighand(p->sighand);
1705 bad_fork_cleanup_fs:
1706         exit_fs(p); /* blocking */
1707 bad_fork_cleanup_files:
1708         exit_files(p); /* blocking */
1709 bad_fork_cleanup_semundo:
1710         exit_sem(p);
1711 bad_fork_cleanup_audit:
1712         audit_free(p);
1713         ccs_free_task_security(p);
1714 bad_fork_cleanup_perf:
1715         perf_event_free_task(p);
1716 bad_fork_cleanup_policy:
1717 #ifdef CONFIG_NUMA
1718         mpol_put(p->mempolicy);
1719 bad_fork_cleanup_threadgroup_lock:
1720 #endif
1721         delayacct_tsk_free(p);
1722 bad_fork_cleanup_count:
1723         atomic_dec(&p->cred->user->processes);
1724         exit_creds(p);
1725 bad_fork_free:
1726         free_task(p);
1727 fork_out:
1728         return ERR_PTR(retval);
1729 }
1730 
1731 static inline void init_idle_pids(struct pid_link *links)
1732 {
1733         enum pid_type type;
1734 
1735         for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1736                 INIT_HLIST_NODE(&links[type].node); /* not really needed */
1737                 links[type].pid = &init_struct_pid;
1738         }
1739 }
1740 
1741 struct task_struct *fork_idle(int cpu)
1742 {
1743         struct task_struct *task;
1744         task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0,
1745                             cpu_to_node(cpu));
1746         if (!IS_ERR(task)) {
1747                 init_idle_pids(task->pids);
1748                 init_idle(task, cpu);
1749         }
1750 
1751         return task;
1752 }
1753 
1754 /*
1755  *  Ok, this is the main fork-routine.
1756  *
1757  * It copies the process, and if successful kick-starts
1758  * it and waits for it to finish using the VM if required.
1759  */
1760 long _do_fork(unsigned long clone_flags,
1761               unsigned long stack_start,
1762               unsigned long stack_size,
1763               int __user *parent_tidptr,
1764               int __user *child_tidptr,
1765               unsigned long tls)
1766 {
1767         struct task_struct *p;
1768         int trace = 0;
1769         long nr;
1770 
1771         /*
1772          * Determine whether and which event to report to ptracer.  When
1773          * called from kernel_thread or CLONE_UNTRACED is explicitly
1774          * requested, no event is reported; otherwise, report if the event
1775          * for the type of forking is enabled.
1776          */
1777         if (!(clone_flags & CLONE_UNTRACED)) {
1778                 if (clone_flags & CLONE_VFORK)
1779                         trace = PTRACE_EVENT_VFORK;
1780                 else if ((clone_flags & CSIGNAL) != SIGCHLD)
1781                         trace = PTRACE_EVENT_CLONE;
1782                 else
1783                         trace = PTRACE_EVENT_FORK;
1784 
1785                 if (likely(!ptrace_event_enabled(current, trace)))
1786                         trace = 0;
1787         }
1788 
1789         p = copy_process(clone_flags, stack_start, stack_size,
1790                          child_tidptr, NULL, trace, tls, NUMA_NO_NODE);
1791         /*
1792          * Do this prior waking up the new thread - the thread pointer
1793          * might get invalid after that point, if the thread exits quickly.
1794          */
1795         if (!IS_ERR(p)) {
1796                 struct completion vfork;
1797                 struct pid *pid;
1798 
1799                 trace_sched_process_fork(current, p);
1800 
1801                 pid = get_task_pid(p, PIDTYPE_PID);
1802                 nr = pid_vnr(pid);
1803 
1804                 if (clone_flags & CLONE_PARENT_SETTID)
1805                         put_user(nr, parent_tidptr);
1806 
1807                 if (clone_flags & CLONE_VFORK) {
1808                         p->vfork_done = &vfork;
1809                         init_completion(&vfork);
1810                         get_task_struct(p);
1811                 }
1812 
1813                 wake_up_new_task(p);
1814 
1815                 /* forking complete and child started to run, tell ptracer */
1816                 if (unlikely(trace))
1817                         ptrace_event_pid(trace, pid);
1818 
1819                 if (clone_flags & CLONE_VFORK) {
1820                         if (!wait_for_vfork_done(p, &vfork))
1821                                 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
1822                 }
1823 
1824                 put_pid(pid);
1825         } else {
1826                 nr = PTR_ERR(p);
1827         }
1828         return nr;
1829 }
1830 
1831 #ifndef CONFIG_HAVE_COPY_THREAD_TLS
1832 /* For compatibility with architectures that call do_fork directly rather than
1833  * using the syscall entry points below. */
1834 long do_fork(unsigned long clone_flags,
1835               unsigned long stack_start,
1836               unsigned long stack_size,
1837               int __user *parent_tidptr,
1838               int __user *child_tidptr)
1839 {
1840         return _do_fork(clone_flags, stack_start, stack_size,
1841                         parent_tidptr, child_tidptr, 0);
1842 }
1843 #endif
1844 
1845 /*
1846  * Create a kernel thread.
1847  */
1848 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
1849 {
1850         return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
1851                 (unsigned long)arg, NULL, NULL, 0);
1852 }
1853 
1854 #ifdef __ARCH_WANT_SYS_FORK
1855 SYSCALL_DEFINE0(fork)
1856 {
1857 #ifdef CONFIG_MMU
1858         return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0);
1859 #else
1860         /* can not support in nommu mode */
1861         return -EINVAL;
1862 #endif
1863 }
1864 #endif
1865 
1866 #ifdef __ARCH_WANT_SYS_VFORK
1867 SYSCALL_DEFINE0(vfork)
1868 {
1869         return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
1870                         0, NULL, NULL, 0);
1871 }
1872 #endif
1873 
1874 #ifdef __ARCH_WANT_SYS_CLONE
1875 #ifdef CONFIG_CLONE_BACKWARDS
1876 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1877                  int __user *, parent_tidptr,
1878                  unsigned long, tls,
1879                  int __user *, child_tidptr)
1880 #elif defined(CONFIG_CLONE_BACKWARDS2)
1881 SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
1882                  int __user *, parent_tidptr,
1883                  int __user *, child_tidptr,
1884                  unsigned long, tls)
1885 #elif defined(CONFIG_CLONE_BACKWARDS3)
1886 SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
1887                 int, stack_size,
1888                 int __user *, parent_tidptr,
1889                 int __user *, child_tidptr,
1890                 unsigned long, tls)
1891 #else
1892 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1893                  int __user *, parent_tidptr,
1894                  int __user *, child_tidptr,
1895                  unsigned long, tls)
1896 #endif
1897 {
1898         return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls);
1899 }
1900 #endif
1901 
1902 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
1903 #define ARCH_MIN_MMSTRUCT_ALIGN 0
1904 #endif
1905 
1906 static void sighand_ctor(void *data)
1907 {
1908         struct sighand_struct *sighand = data;
1909 
1910         spin_lock_init(&sighand->siglock);
1911         init_waitqueue_head(&sighand->signalfd_wqh);
1912 }
1913 
1914 void __init proc_caches_init(void)
1915 {
1916         sighand_cachep = kmem_cache_create("sighand_cache",
1917                         sizeof(struct sighand_struct), 0,
1918                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
1919                         SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor);
1920         signal_cachep = kmem_cache_create("signal_cache",
1921                         sizeof(struct signal_struct), 0,
1922                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
1923                         NULL);
1924         files_cachep = kmem_cache_create("files_cache",
1925                         sizeof(struct files_struct), 0,
1926                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
1927                         NULL);
1928         fs_cachep = kmem_cache_create("fs_cache",
1929                         sizeof(struct fs_struct), 0,
1930                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
1931                         NULL);
1932         /*
1933          * FIXME! The "sizeof(struct mm_struct)" currently includes the
1934          * whole struct cpumask for the OFFSTACK case. We could change
1935          * this to *only* allocate as much of it as required by the
1936          * maximum number of CPU's we can ever have.  The cpumask_allocation
1937          * is at the end of the structure, exactly for that reason.
1938          */
1939         mm_cachep = kmem_cache_create("mm_struct",
1940                         sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
1941                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
1942                         NULL);
1943         vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT);
1944         mmap_init();
1945         nsproxy_cache_init();
1946 }
1947 
1948 /*
1949  * Check constraints on flags passed to the unshare system call.
1950  */
1951 static int check_unshare_flags(unsigned long unshare_flags)
1952 {
1953         if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
1954                                 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
1955                                 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
1956                                 CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP))
1957                 return -EINVAL;
1958         /*
1959          * Not implemented, but pretend it works if there is nothing
1960          * to unshare.  Note that unsharing the address space or the
1961          * signal handlers also need to unshare the signal queues (aka
1962          * CLONE_THREAD).
1963          */
1964         if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
1965                 if (!thread_group_empty(current))
1966                         return -EINVAL;
1967         }
1968         if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
1969                 if (atomic_read(&current->sighand->count) > 1)
1970                         return -EINVAL;
1971         }
1972         if (unshare_flags & CLONE_VM) {
1973                 if (!current_is_single_threaded())
1974                         return -EINVAL;
1975         }
1976 
1977         return 0;
1978 }
1979 
1980 /*
1981  * Unshare the filesystem structure if it is being shared
1982  */
1983 static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1984 {
1985         struct fs_struct *fs = current->fs;
1986 
1987         if (!(unshare_flags & CLONE_FS) || !fs)
1988                 return 0;
1989 
1990         /* don't need lock here; in the worst case we'll do useless copy */
1991         if (fs->users == 1)
1992                 return 0;
1993 
1994         *new_fsp = copy_fs_struct(fs);
1995         if (!*new_fsp)
1996                 return -ENOMEM;
1997 
1998         return 0;
1999 }
2000 
2001 /*
2002  * Unshare file descriptor table if it is being shared
2003  */
2004 static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
2005 {
2006         struct files_struct *fd = current->files;
2007         int error = 0;
2008 
2009         if ((unshare_flags & CLONE_FILES) &&
2010             (fd && atomic_read(&fd->count) > 1)) {
2011                 *new_fdp = dup_fd(fd, &error);
2012                 if (!*new_fdp)
2013                         return error;
2014         }
2015 
2016         return 0;
2017 }
2018 
2019 /*
2020  * unshare allows a process to 'unshare' part of the process
2021  * context which was originally shared using clone.  copy_*
2022  * functions used by do_fork() cannot be used here directly
2023  * because they modify an inactive task_struct that is being
2024  * constructed. Here we are modifying the current, active,
2025  * task_struct.
2026  */
2027 SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
2028 {
2029         struct fs_struct *fs, *new_fs = NULL;
2030         struct files_struct *fd, *new_fd = NULL;
2031         struct cred *new_cred = NULL;
2032         struct nsproxy *new_nsproxy = NULL;
2033         int do_sysvsem = 0;
2034         int err;
2035 
2036         /*
2037          * If unsharing a user namespace must also unshare the thread group
2038          * and unshare the filesystem root and working directories.
2039          */
2040         if (unshare_flags & CLONE_NEWUSER)
2041                 unshare_flags |= CLONE_THREAD | CLONE_FS;
2042         /*
2043          * If unsharing vm, must also unshare signal handlers.
2044          */
2045         if (unshare_flags & CLONE_VM)
2046                 unshare_flags |= CLONE_SIGHAND;
2047         /*
2048          * If unsharing a signal handlers, must also unshare the signal queues.
2049          */
2050         if (unshare_flags & CLONE_SIGHAND)
2051                 unshare_flags |= CLONE_THREAD;
2052         /*
2053          * If unsharing namespace, must also unshare filesystem information.
2054          */
2055         if (unshare_flags & CLONE_NEWNS)
2056                 unshare_flags |= CLONE_FS;
2057 
2058         err = check_unshare_flags(unshare_flags);
2059         if (err)
2060                 goto bad_unshare_out;
2061         /*
2062          * CLONE_NEWIPC must also detach from the undolist: after switching
2063          * to a new ipc namespace, the semaphore arrays from the old
2064          * namespace are unreachable.
2065          */
2066         if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
2067                 do_sysvsem = 1;
2068         err = unshare_fs(unshare_flags, &new_fs);
2069         if (err)
2070                 goto bad_unshare_out;
2071         err = unshare_fd(unshare_flags, &new_fd);
2072         if (err)
2073                 goto bad_unshare_cleanup_fs;
2074         err = unshare_userns(unshare_flags, &new_cred);
2075         if (err)
2076                 goto bad_unshare_cleanup_fd;
2077         err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
2078                                          new_cred, new_fs);
2079         if (err)
2080                 goto bad_unshare_cleanup_cred;
2081 
2082         if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
2083                 if (do_sysvsem) {
2084                         /*
2085                          * CLONE_SYSVSEM is equivalent to sys_exit().
2086                          */
2087                         exit_sem(current);
2088                 }
2089                 if (unshare_flags & CLONE_NEWIPC) {
2090                         /* Orphan segments in old ns (see sem above). */
2091                         exit_shm(current);
2092                         shm_init_task(current);
2093                 }
2094 
2095                 if (new_nsproxy)
2096                         switch_task_namespaces(current, new_nsproxy);
2097 
2098                 task_lock(current);
2099 
2100                 if (new_fs) {
2101                         fs = current->fs;
2102                         spin_lock(&fs->lock);
2103                         current->fs = new_fs;
2104                         if (--fs->users)
2105                                 new_fs = NULL;
2106                         else
2107                                 new_fs = fs;
2108                         spin_unlock(&fs->lock);
2109                 }
2110 
2111                 if (new_fd) {
2112                         fd = current->files;
2113                         current->files = new_fd;
2114                         new_fd = fd;
2115                 }
2116 
2117                 task_unlock(current);
2118 
2119                 if (new_cred) {
2120                         /* Install the new user namespace */
2121                         commit_creds(new_cred);
2122                         new_cred = NULL;
2123                 }
2124         }
2125 
2126 bad_unshare_cleanup_cred:
2127         if (new_cred)
2128                 put_cred(new_cred);
2129 bad_unshare_cleanup_fd:
2130         if (new_fd)
2131                 put_files_struct(new_fd);
2132 
2133 bad_unshare_cleanup_fs:
2134         if (new_fs)
2135                 free_fs_struct(new_fs);
2136 
2137 bad_unshare_out:
2138         return err;
2139 }
2140 
2141 /*
2142  *      Helper to unshare the files of the current task.
2143  *      We don't want to expose copy_files internals to
2144  *      the exec layer of the kernel.
2145  */
2146 
2147 int unshare_files(struct files_struct **displaced)
2148 {
2149         struct task_struct *task = current;
2150         struct files_struct *copy = NULL;
2151         int error;
2152 
2153         error = unshare_fd(CLONE_FILES, &copy);
2154         if (error || !copy) {
2155                 *displaced = NULL;
2156                 return error;
2157         }
2158         *displaced = task->files;
2159         task_lock(task);
2160         task->files = copy;
2161         task_unlock(task);
2162         return 0;
2163 }
2164 
2165 int sysctl_max_threads(struct ctl_table *table, int write,
2166                        void __user *buffer, size_t *lenp, loff_t *ppos)
2167 {
2168         struct ctl_table t;
2169         int ret;
2170         int threads = max_threads;
2171         int min = MIN_THREADS;
2172         int max = MAX_THREADS;
2173 
2174         t = *table;
2175         t.data = &threads;
2176         t.extra1 = &min;
2177         t.extra2 = &max;
2178 
2179         ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2180         if (ret || !write)
2181                 return ret;
2182 
2183         set_max_threads(threads);
2184 
2185         return 0;
2186 }
2187 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp