LCOV - code coverage report
Current view: top level - kernel - fork.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 512 645 79.4 %
Date: 2014-04-16 Functions: 42 50 84.0 %
Branches: 247 325 76.0 %

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

Generated by: LCOV version 1.9