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