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