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