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