LCOV - code coverage report
Current view: top level - kernel - fork.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 505 635 79.5 %
Date: 2014-02-18 Functions: 42 50 84.0 %
Branches: 238 331 71.9 %

           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         [ +  + ]:          7 :         for_each_possible_cpu(cpu)
     113                 :          5 :                 total += per_cpu(process_counts, cpu);
     114                 :            : 
     115                 :          1 :         return total;
     116                 :            : }
     117                 :            : 
     118                 :          0 : void __weak arch_release_task_struct(struct task_struct *tsk)
     119                 :            : {
     120                 :    1151929 : }
     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                 :    1151913 :         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                 :    1151929 :         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                 :    1151723 : }
     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                 :    1151925 : 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         [ +  - ]:    1151929 :         return page ? page_address(page) : NULL;
     154                 :            : }
     155                 :            : 
     156                 :            : static inline void free_thread_info(struct thread_info *ti)
     157                 :            : {
     158                 :    1151724 :         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                 :    6910035 :         struct zone *zone = page_zone(virt_to_page(ti));
     207                 :            : 
     208                 :    2303345 :         mod_zone_page_state(zone, NR_KERNEL_STACK, account);
     209                 :    2303555 : }
     210                 :            : 
     211                 :          0 : void free_task(struct task_struct *tsk)
     212                 :            : {
     213                 :    1151825 :         account_kernel_stack(tsk->stack, -1);
     214                 :    1151720 :         arch_release_thread_info(tsk->stack);
     215                 :    1151724 :         free_thread_info(tsk->stack);
     216                 :            :         rt_mutex_debug_task_free(tsk);
     217                 :            :         ftrace_graph_exit_task(tsk);
     218                 :    1151929 :         put_seccomp_filter(tsk);
     219                 :    1151929 :         arch_release_task_struct(tsk);
     220                 :            :         free_task_struct(tsk);
     221                 :    1151882 : }
     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                 :    1149302 :         kmem_cache_free(signal_cachep, sig);
     229                 :            : }
     230                 :            : 
     231                 :            : static inline void put_signal_struct(struct signal_struct *sig)
     232                 :            : {
     233         [ +  + ]:    1151665 :         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         [ -  + ]:    1151612 :         WARN_ON(!tsk->exit_state);
     252         [ -  + ]:    1151673 :         WARN_ON(atomic_read(&tsk->usage));
     253         [ -  + ]:    1151673 :         WARN_ON(tsk == current);
     254                 :            : 
     255                 :    1151673 :         security_task_free(tsk);
     256                 :    1151507 :         exit_creds(tsk);
     257                 :            :         delayacct_tsk_free(tsk);
     258                 :    1151461 :         put_signal_struct(tsk->signal);
     259                 :            : 
     260                 :    1151885 :         atomic_notifier_call_chain(&task_free_notifier, 0, tsk);
     261            [ + ]:    1151763 :         if (!profile_handoff_task(tsk))
     262                 :    1151694 :                 free_task(tsk);
     263                 :    1151865 : }
     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                 :    1151928 :         *dst = *src;
     306                 :    1151928 :         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                 :    1151917 :         int node = tsk_fork_get_node(orig);
     315                 :            :         int err;
     316                 :            : 
     317                 :            :         tsk = alloc_task_struct_node(node);
     318         [ +  + ]:    1151922 :         if (!tsk)
     319                 :            :                 return NULL;
     320                 :            : 
     321                 :    1151921 :         ti = alloc_thread_info_node(tsk, node);
     322         [ +  - ]:    1151929 :         if (!ti)
     323                 :            :                 goto free_tsk;
     324                 :            : 
     325                 :    1151929 :         err = arch_dup_task_struct(tsk, orig);
     326         [ +  - ]:    1151926 :         if (err)
     327                 :            :                 goto free_ti;
     328                 :            : 
     329                 :    1151926 :         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                 :    1151929 :         *stackend = STACK_END_MAGIC;    /* for overflow detection */
     336                 :            : 
     337                 :            : #ifdef CONFIG_CC_STACKPROTECTOR
     338                 :    1151929 :         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                 :    1151895 :         atomic_set(&tsk->usage, 2);
     346                 :            : #ifdef CONFIG_BLK_DEV_IO_TRACE
     347                 :            :         tsk->btrace_seq = 0;
     348                 :            : #endif
     349                 :    1151895 :         tsk->splice_pipe = NULL;
     350                 :    1151895 :         tsk->task_frag.page = NULL;
     351                 :            : 
     352                 :    1151895 :         account_kernel_stack(ti, 1);
     353                 :            : 
     354                 :    1151918 :         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                 :   12765498 :         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                 :            :         uprobe_start_dup_mmap();
     372                 :    1137473 :         down_write(&oldmm->mmap_sem);
     373                 :    1137474 :         flush_cache_dup_mm(oldmm);
     374                 :            :         uprobe_dup_mmap(oldmm, mm);
     375                 :            :         /*
     376                 :            :          * Not linked in yet - no deadlock potential:
     377                 :            :          */
     378                 :    1137475 :         down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
     379                 :            : 
     380                 :    1137473 :         mm->locked_vm = 0;
     381                 :    1137473 :         mm->mmap = NULL;
     382                 :    1137473 :         mm->mmap_cache = NULL;
     383                 :    1137473 :         mm->map_count = 0;
     384                 :            :         cpumask_clear(mm_cpumask(mm));
     385                 :    1137473 :         mm->mm_rb = RB_ROOT;
     386                 :    1137473 :         rb_link = &mm->mm_rb.rb_node;
     387                 :            :         rb_parent = NULL;
     388                 :    1137473 :         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         [ +  + ]:   20240348 :         for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
     398                 :   12288608 :                 struct file *file;
     399                 :            : 
     400         [ +  + ]:   19102879 :                 if (mpnt->vm_flags & VM_DONTCOPY) {
     401                 :        181 :                         vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
     402                 :        181 :                                                         -vma_pages(mpnt));
     403                 :          0 :                         continue;
     404                 :            :                 }
     405                 :            :                 charge = 0;
     406         [ +  + ]:   19102698 :                 if (mpnt->vm_flags & VM_ACCOUNT) {
     407                 :            :                         unsigned long len = vma_pages(mpnt);
     408                 :            : 
     409            [ + ]:   12765317 :                         if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
     410                 :            :                                 goto fail_nomem;
     411                 :            :                         charge = len;
     412                 :            :                 }
     413                 :   19102717 :                 tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
     414            [ + ]:   19102907 :                 if (!tmp)
     415                 :            :                         goto fail_nomem;
     416                 :   19102913 :                 *tmp = *mpnt;
     417                 :   19102913 :                 INIT_LIST_HEAD(&tmp->anon_vma_chain);
     418                 :            :                 retval = vma_dup_policy(mpnt, tmp);
     419                 :            :                 if (retval)
     420                 :            :                         goto fail_nomem_policy;
     421                 :   19102913 :                 tmp->vm_mm = mm;
     422         [ +  - ]:   19102913 :                 if (anon_vma_fork(tmp, mpnt))
     423                 :            :                         goto fail_nomem_anon_vma_fork;
     424                 :   19102826 :                 tmp->vm_flags &= ~VM_LOCKED;
     425                 :   19102826 :                 tmp->vm_next = tmp->vm_prev = NULL;
     426                 :   19102826 :                 file = tmp->vm_file;
     427         [ +  + ]:   19102826 :                 if (file) {
     428                 :            :                         struct inode *inode = file_inode(file);
     429                 :   12288608 :                         struct address_space *mapping = file->f_mapping;
     430                 :            : 
     431                 :            :                         get_file(file);
     432         [ +  + ]:   12288711 :                         if (tmp->vm_flags & VM_DENYWRITE)
     433                 :    6824839 :                                 atomic_dec(&inode->i_writecount);
     434                 :   12288681 :                         mutex_lock(&mapping->i_mmap_mutex);
     435         [ +  + ]:   12288655 :                         if (tmp->vm_flags & VM_SHARED)
     436                 :      14301 :                                 mapping->i_mmap_writable++;
     437                 :            :                         flush_dcache_mmap_lock(mapping);
     438                 :            :                         /* insert tmp into the share list, just after mpnt */
     439         [ -  + ]:   12288683 :                         if (unlikely(tmp->vm_flags & VM_NONLINEAR))
     440                 :          0 :                                 vma_nonlinear_insert(tmp,
     441                 :            :                                                 &mapping->i_mmap_nonlinear);
     442                 :            :                         else
     443                 :   12288683 :                                 vma_interval_tree_insert_after(tmp, mpnt,
     444                 :            :                                                         &mapping->i_mmap);
     445                 :            :                         flush_dcache_mmap_unlock(mapping);
     446                 :   12288727 :                         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                 :   19102928 :                 *pprev = tmp;
     461                 :   19102928 :                 pprev = &tmp->vm_next;
     462                 :   19102928 :                 tmp->vm_prev = prev;
     463                 :            :                 prev = tmp;
     464                 :            : 
     465                 :   19102928 :                 __vma_link_rb(mm, tmp, rb_link, rb_parent);
     466                 :   19102876 :                 rb_link = &tmp->vm_rb.rb_right;
     467                 :   19102876 :                 rb_parent = &tmp->vm_rb;
     468                 :            : 
     469                 :   19102876 :                 mm->map_count++;
     470                 :   19102876 :                 retval = copy_page_range(mm, oldmm, mpnt);
     471                 :            : 
     472 [ +  + ][ +  + ]:   19102875 :                 if (tmp->vm_ops && tmp->vm_ops->open)
     473                 :          8 :                         tmp->vm_ops->open(tmp);
     474                 :            : 
     475         [ +  - ]:   19102875 :                 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                 :    1137469 :         up_write(&mm->mmap_sem);
     483                 :    1137475 :         flush_tlb_mm(oldmm);
     484                 :    1137475 :         up_write(&oldmm->mmap_sem);
     485                 :            :         uprobe_end_dup_mmap();
     486                 :    1137475 :         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                 :    1196455 :         mm->pgd = pgd_alloc(mm);
     500         [ +  - ]:    2392890 :         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                 :    1196459 :         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                 :    1196456 :         spin_lock_init(&mm->ioctx_lock);
     538                 :    1196456 :         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                 :    1196452 :         atomic_set(&mm->mm_users, 1);
     545                 :    1196452 :         atomic_set(&mm->mm_count, 1);
     546                 :    1196452 :         init_rwsem(&mm->mmap_sem);
     547                 :    1196426 :         INIT_LIST_HEAD(&mm->mmlist);
     548                 :    2392852 :         mm->flags = (current->mm) ?
     549         [ +  + ]:    1196426 :                 (current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
     550                 :    1196426 :         mm->core_state = NULL;
     551                 :            :         atomic_long_set(&mm->nr_ptes, 0);
     552                 :    1196426 :         memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
     553                 :    1196456 :         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         [ +  - ]:    1196438 :         if (likely(!mm_alloc_pgd(mm))) {
     559                 :    1196438 :                 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         [ +  + ]:    4785836 :         for (i = 0; i < NR_MM_COUNTERS; i++) {
     573                 :    3589377 :                 long x = atomic_long_read(&mm->rss_stat.count[i]);
     574                 :            : 
     575         [ -  + ]:    3589377 :                 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                 :    1196459 : }
     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                 :      58980 :         mm = allocate_mm();
     593         [ +  - ]:      58976 :         if (!mm)
     594                 :            :                 return NULL;
     595                 :            : 
     596                 :      58976 :         memset(mm, 0, sizeof(*mm));
     597                 :            :         mm_init_cpumask(mm);
     598                 :      58984 :         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         [ -  + ]:    1196459 :         BUG_ON(mm == &init_mm);
     609                 :            :         mm_free_pgd(mm);
     610                 :            :         destroy_context(mm);
     611                 :            :         mmu_notifier_mm_destroy(mm);
     612                 :    1196459 :         check_mm(mm);
     613                 :    1196459 :         free_mm(mm);
     614                 :    1196458 : }
     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         [ +  + ]:    1349385 :         if (atomic_dec_and_test(&mm->mm_users)) {
     625                 :            :                 uprobe_clear_state(mm);
     626                 :    1196425 :                 exit_aio(mm);
     627                 :            :                 ksm_exit(mm);
     628                 :            :                 khugepaged_exit(mm); /* must run before exit_mmap */
     629                 :    1196345 :                 exit_mmap(mm);
     630                 :    1196459 :                 set_mm_exe_file(mm, NULL);
     631         [ -  + ]:    1196458 :                 if (!list_empty(&mm->mmlist)) {
     632                 :            :                         spin_lock(&mmlist_lock);
     633                 :            :                         list_del(&mm->mmlist);
     634                 :            :                         spin_unlock(&mmlist_lock);
     635                 :            :                 }
     636         [ +  + ]:    1196459 :                 if (mm->binfmt)
     637                 :    1196451 :                         module_put(mm->binfmt->module);
     638                 :            :                 mmdrop(mm);
     639                 :            :         }
     640                 :          6 : }
     641                 :            : EXPORT_SYMBOL_GPL(mmput);
     642                 :            : 
     643                 :          0 : void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
     644                 :            : {
     645         [ +  + ]:    1255435 :         if (new_exe_file)
     646                 :            :                 get_file(new_exe_file);
     647         [ +  + ]:    1255422 :         if (mm->exe_file)
     648                 :    1196450 :                 fput(mm->exe_file);
     649                 :    1255422 :         mm->exe_file = new_exe_file;
     650                 :    1255422 : }
     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                 :    1139982 :         down_read(&mm->mmap_sem);
     658                 :    1139970 :         exe_file = mm->exe_file;
     659         [ +  + ]:    1139970 :         if (exe_file)
     660                 :            :                 get_file(exe_file);
     661                 :    1139983 :         up_read(&mm->mmap_sem);
     662                 :    1139978 :         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                 :    1137472 :         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                 :     156150 :         mm = task->mm;
     687         [ +  + ]:     156150 :         if (mm) {
     688         [ +  - ]:     138965 :                 if (task->flags & PF_KTHREAD)
     689                 :            :                         mm = NULL;
     690                 :            :                 else
     691                 :     138965 :                         atomic_inc(&mm->mm_users);
     692                 :            :         }
     693                 :            :         task_unlock(task);
     694                 :     156150 :         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                 :      71732 :         err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
     704         [ -  + ]:      71732 :         if (err)
     705                 :          0 :                 return ERR_PTR(err);
     706                 :            : 
     707                 :      71732 :         mm = get_task_mm(task);
     708         [ +  - ]:     130718 :         if (mm && mm != current->mm &&
           [ +  +  +  + ]
     709         [ +  - ]:      58988 :                         !ptrace_may_access(task, mode) &&
     710                 :          2 :                         !capable(CAP_SYS_RESOURCE)) {
     711                 :          2 :                 mmput(mm);
     712                 :            :                 mm = ERR_PTR(-EACCES);
     713                 :            :         }
     714                 :      71732 :         mutex_unlock(&task->signal->cred_guard_mutex);
     715                 :            : 
     716                 :      71732 :         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                 :      12088 :         vfork = tsk->vfork_done;
     725         [ +  - ]:      12088 :         if (likely(vfork)) {
     726                 :      12088 :                 tsk->vfork_done = NULL;
     727                 :      12088 :                 complete(vfork);
     728                 :            :         }
     729                 :            :         task_unlock(tsk);
     730                 :      12088 : }
     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                 :      11743 :         killed = wait_for_completion_killable(vfork);
     739                 :            :         freezer_count();
     740                 :            : 
     741         [ -  + ]:      11743 :         if (killed) {
     742                 :            :                 task_lock(child);
     743                 :          0 :                 child->vfork_done = NULL;
     744                 :            :                 task_unlock(child);
     745                 :            :         }
     746                 :            : 
     747                 :            :         put_task_struct(child);
     748                 :      11743 :         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         [ +  + ]:    1210889 :         if (unlikely(tsk->robust_list)) {
     769                 :      16707 :                 exit_robust_list(tsk);
     770                 :      16706 :                 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         [ -  + ]:    1210888 :         if (unlikely(!list_empty(&tsk->pi_state_list)))
     779                 :          0 :                 exit_pi_state_list(tsk);
     780                 :            : #endif
     781                 :            : 
     782                 :            :         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         [ +  + ]:    2421767 :         if (tsk->clear_child_tid) {
     795 [ +  + ][ +  + ]:    1147857 :                 if (!(tsk->flags & PF_SIGNALED) &&
     796                 :    1147521 :                     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                 :       2267 :                         put_user(0, tsk->clear_child_tid);
     802                 :       2266 :                         sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
     803                 :            :                                         1, NULL, NULL, 0);
     804                 :            :                 }
     805                 :    1147829 :                 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         [ +  + ]:    1210850 :         if (tsk->vfork_done)
     813                 :      12088 :                 complete_vfork_done(tsk);
     814                 :    1210850 : }
     815                 :            : 
     816                 :            : /*
     817                 :            :  * Allocate a new mm structure and copy contents from the
     818                 :            :  * mm structure of the passed in task structure.
     819                 :            :  */
     820                 :          0 : struct mm_struct *dup_mm(struct task_struct *tsk)
     821                 :            : {
     822                 :    1137475 :         struct mm_struct *mm, *oldmm = current->mm;
     823                 :            :         int err;
     824                 :            : 
     825         [ +  + ]:    1137475 :         if (!oldmm)
     826                 :            :                 return NULL;
     827                 :            : 
     828                 :    1137468 :         mm = allocate_mm();
     829            [ + ]:    1137460 :         if (!mm)
     830                 :            :                 goto fail_nomem;
     831                 :            : 
     832                 :    1137461 :         memcpy(mm, oldmm, sizeof(*mm));
     833                 :            :         mm_init_cpumask(mm);
     834                 :            : 
     835                 :            : #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
     836                 :            :         mm->pmd_huge_pte = NULL;
     837                 :            : #endif
     838            [ + ]:    1137461 :         if (!mm_init(mm, tsk))
     839                 :            :                 goto fail_nomem;
     840                 :            : 
     841                 :    1137463 :         if (init_new_context(tsk, mm))
     842                 :            :                 goto fail_nocontext;
     843                 :            : 
     844                 :            :         dup_mm_exe_file(oldmm, mm);
     845                 :            : 
     846                 :    1137470 :         err = dup_mmap(mm, oldmm);
     847         [ +  - ]:    1137475 :         if (err)
     848                 :            :                 goto free_pt;
     849                 :            : 
     850                 :    1137475 :         mm->hiwater_rss = get_mm_rss(mm);
     851                 :    1137475 :         mm->hiwater_vm = mm->total_vm;
     852                 :            : 
     853       [ + ][ + ]:    1137475 :         if (mm->binfmt && !try_module_get(mm->binfmt->module))
     854                 :            :                 goto free_pt;
     855                 :            : 
     856                 :    1137475 :         return mm;
     857                 :            : 
     858                 :            : free_pt:
     859                 :            :         /* don't put binfmt in mmput, we haven't got module yet */
     860                 :          0 :         mm->binfmt = NULL;
     861                 :          0 :         mmput(mm);
     862                 :            : 
     863                 :            : fail_nomem:
     864                 :            :         return NULL;
     865                 :            : 
     866                 :            : fail_nocontext:
     867                 :            :         /*
     868                 :            :          * If init_new_context() failed, we cannot use mmput() to free the mm
     869                 :            :          * because it calls destroy_context()
     870                 :            :          */
     871                 :            :         mm_free_pgd(mm);
     872                 :            :         free_mm(mm);
     873                 :            :         return NULL;
     874                 :            : }
     875                 :            : 
     876                 :          0 : static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
     877                 :            : {
     878                 :            :         struct mm_struct *mm, *oldmm;
     879                 :            :         int retval;
     880                 :            : 
     881                 :    1151915 :         tsk->min_flt = tsk->maj_flt = 0;
     882                 :    1151915 :         tsk->nvcsw = tsk->nivcsw = 0;
     883                 :            : #ifdef CONFIG_DETECT_HUNG_TASK
     884                 :            :         tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
     885                 :            : #endif
     886                 :            : 
     887                 :    1151915 :         tsk->mm = NULL;
     888                 :    1151915 :         tsk->active_mm = NULL;
     889                 :            : 
     890                 :            :         /*
     891                 :            :          * Are we cloning a kernel thread?
     892                 :            :          *
     893                 :            :          * We need to steal a active VM for that..
     894                 :            :          */
     895                 :    1151915 :         oldmm = current->mm;
     896         [ +  + ]:    1151915 :         if (!oldmm)
     897                 :            :                 return 0;
     898                 :            : 
     899         [ +  + ]:    1151417 :         if (clone_flags & CLONE_VM) {
     900                 :      13948 :                 atomic_inc(&oldmm->mm_users);
     901                 :            :                 mm = oldmm;
     902                 :      13949 :                 goto good_mm;
     903                 :            :         }
     904                 :            : 
     905                 :            :         retval = -ENOMEM;
     906                 :    1137469 :         mm = dup_mm(tsk);
     907         [ +  - ]:    1137475 :         if (!mm)
     908                 :            :                 goto fail_nomem;
     909                 :            : 
     910                 :            : good_mm:
     911                 :    1151424 :         tsk->mm = mm;
     912                 :    1151424 :         tsk->active_mm = mm;
     913                 :    1151424 :         return 0;
     914                 :            : 
     915                 :            : fail_nomem:
     916                 :            :         return retval;
     917                 :            : }
     918                 :            : 
     919                 :          0 : static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
     920                 :            : {
     921                 :    1151904 :         struct fs_struct *fs = current->fs;
     922         [ +  + ]:    1151904 :         if (clone_flags & CLONE_FS) {
     923                 :            :                 /* tsk->fs is already what we want */
     924                 :            :                 spin_lock(&fs->lock);
     925         [ -  + ]:       2685 :                 if (fs->in_exec) {
     926                 :            :                         spin_unlock(&fs->lock);
     927                 :          0 :                         return -EAGAIN;
     928                 :            :                 }
     929                 :       2685 :                 fs->users++;
     930                 :            :                 spin_unlock(&fs->lock);
     931                 :       2685 :                 return 0;
     932                 :            :         }
     933                 :    1149219 :         tsk->fs = copy_fs_struct(fs);
     934         [ +  - ]:    1149244 :         if (!tsk->fs)
     935                 :            :                 return -ENOMEM;
     936                 :    1149244 :         return 0;
     937                 :            : }
     938                 :            : 
     939                 :          0 : static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
     940                 :            : {
     941                 :            :         struct files_struct *oldf, *newf;
     942                 :    1151878 :         int error = 0;
     943                 :            : 
     944                 :            :         /*
     945                 :            :          * A background process may not have any files ...
     946                 :            :          */
     947                 :    1151878 :         oldf = current->files;
     948            [ + ]:    1151878 :         if (!oldf)
     949                 :            :                 goto out;
     950                 :            : 
     951         [ +  + ]:    1151908 :         if (clone_flags & CLONE_FILES) {
     952                 :       2685 :                 atomic_inc(&oldf->count);
     953                 :            :                 goto out;
     954                 :            :         }
     955                 :            : 
     956                 :    1149223 :         newf = dup_fd(oldf, &error);
     957         [ +  - ]:    1149244 :         if (!newf)
     958                 :            :                 goto out;
     959                 :            : 
     960                 :    1149244 :         tsk->files = newf;
     961                 :    1149244 :         error = 0;
     962                 :            : out:
     963                 :         21 :         return error;
     964                 :            : }
     965                 :            : 
     966                 :          0 : static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
     967                 :            : {
     968                 :            : #ifdef CONFIG_BLOCK
     969                 :    1151924 :         struct io_context *ioc = current->io_context;
     970                 :            :         struct io_context *new_ioc;
     971                 :            : 
     972         [ +  + ]:    1151924 :         if (!ioc)
     973                 :            :                 return 0;
     974                 :            :         /*
     975                 :            :          * Share io context with parent, if CLONE_IO is set
     976                 :            :          */
     977         [ -  + ]:      83211 :         if (clone_flags & CLONE_IO) {
     978                 :            :                 ioc_task_link(ioc);
     979                 :          0 :                 tsk->io_context = ioc;
     980         [ -  + ]:      83211 :         } else if (ioprio_valid(ioc->ioprio)) {
     981                 :          0 :                 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
     982         [ #  # ]:          0 :                 if (unlikely(!new_ioc))
     983                 :            :                         return -ENOMEM;
     984                 :            : 
     985                 :          0 :                 new_ioc->ioprio = ioc->ioprio;
     986                 :          0 :                 put_io_context(new_ioc);
     987                 :            :         }
     988                 :            : #endif
     989                 :            :         return 0;
     990                 :            : }
     991                 :            : 
     992                 :          0 : static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
     993                 :            : {
     994                 :            :         struct sighand_struct *sig;
     995                 :            : 
     996         [ +  + ]:    1151928 :         if (clone_flags & CLONE_SIGHAND) {
     997                 :       2317 :                 atomic_inc(&current->sighand->count);
     998                 :       2316 :                 return 0;
     999                 :            :         }
    1000                 :    1149611 :         sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
    1001                 :    1149609 :         rcu_assign_pointer(tsk->sighand, sig);
    1002         [ +  + ]:    1149608 :         if (!sig)
    1003                 :            :                 return -ENOMEM;
    1004                 :    1149606 :         atomic_set(&sig->count, 1);
    1005                 :    2299212 :         memcpy(sig->action, current->sighand->action, sizeof(sig->action));
    1006                 :    1149606 :         return 0;
    1007                 :            : }
    1008                 :            : 
    1009                 :          0 : void __cleanup_sighand(struct sighand_struct *sighand)
    1010                 :            : {
    1011         [ +  + ]:    1151929 :         if (atomic_dec_and_test(&sighand->count)) {
    1012                 :    1149612 :                 signalfd_cleanup(sighand);
    1013                 :    1149612 :                 kmem_cache_free(sighand_cachep, sighand);
    1014                 :            :         }
    1015                 :          0 : }
    1016                 :            : 
    1017                 :            : 
    1018                 :            : /*
    1019                 :            :  * Initialize POSIX timer handling for a thread group.
    1020                 :            :  */
    1021                 :            : static void posix_cpu_timers_init_group(struct signal_struct *sig)
    1022                 :            : {
    1023                 :            :         unsigned long cpu_limit;
    1024                 :            : 
    1025                 :            :         /* Thread group counters. */
    1026                 :            :         thread_group_cputime_init(sig);
    1027                 :            : 
    1028                 :    1149612 :         cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
    1029         [ -  + ]:    1149612 :         if (cpu_limit != RLIM_INFINITY) {
    1030                 :          0 :                 sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
    1031                 :          0 :                 sig->cputimer.running = 1;
    1032                 :            :         }
    1033                 :            : 
    1034                 :            :         /* The timer lists. */
    1035                 :    1149612 :         INIT_LIST_HEAD(&sig->cpu_timers[0]);
    1036                 :    1149612 :         INIT_LIST_HEAD(&sig->cpu_timers[1]);
    1037                 :    1149612 :         INIT_LIST_HEAD(&sig->cpu_timers[2]);
    1038                 :            : }
    1039                 :            : 
    1040                 :          0 : static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
    1041                 :            : {
    1042                 :            :         struct signal_struct *sig;
    1043                 :            : 
    1044         [ +  + ]:    1151929 :         if (clone_flags & CLONE_THREAD)
    1045                 :            :                 return 0;
    1046                 :            : 
    1047                 :    1149601 :         sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
    1048                 :    1149598 :         tsk->signal = sig;
    1049         [ +  + ]:    1149598 :         if (!sig)
    1050                 :            :                 return -ENOMEM;
    1051                 :            : 
    1052                 :    1149596 :         sig->nr_threads = 1;
    1053                 :    1149596 :         atomic_set(&sig->live, 1);
    1054                 :    1149596 :         atomic_set(&sig->sigcnt, 1);
    1055                 :    1149596 :         init_waitqueue_head(&sig->wait_chldexit);
    1056                 :    1149591 :         sig->curr_target = tsk;
    1057                 :            :         init_sigpending(&sig->shared_pending);
    1058                 :    1149591 :         INIT_LIST_HEAD(&sig->posix_timers);
    1059                 :            : 
    1060                 :    1149591 :         hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    1061                 :    1149554 :         sig->real_timer.function = it_real_fn;
    1062                 :            : 
    1063                 :    1149554 :         task_lock(current->group_leader);
    1064                 :    2299198 :         memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
    1065                 :    1149599 :         task_unlock(current->group_leader);
    1066                 :            : 
    1067                 :            :         posix_cpu_timers_init_group(sig);
    1068                 :            : 
    1069                 :    1149612 :         tty_audit_fork(sig);
    1070                 :            :         sched_autogroup_fork(sig);
    1071                 :            : 
    1072                 :            : #ifdef CONFIG_CGROUPS
    1073                 :    1149562 :         init_rwsem(&sig->group_rwsem);
    1074                 :            : #endif
    1075                 :            : 
    1076                 :    1149560 :         sig->oom_score_adj = current->signal->oom_score_adj;
    1077                 :    1149560 :         sig->oom_score_adj_min = current->signal->oom_score_adj_min;
    1078                 :            : 
    1079                 :    1149560 :         sig->has_child_subreaper = current->signal->has_child_subreaper ||
    1080                 :            :                                    current->signal->is_child_subreaper;
    1081                 :            : 
    1082                 :    1149560 :         mutex_init(&sig->cred_guard_mutex);
    1083                 :            : 
    1084                 :    1149563 :         return 0;
    1085                 :            : }
    1086                 :            : 
    1087                 :            : static void copy_flags(unsigned long clone_flags, struct task_struct *p)
    1088                 :            : {
    1089                 :    1151894 :         unsigned long new_flags = p->flags;
    1090                 :            : 
    1091                 :    1151894 :         new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
    1092                 :    1151894 :         new_flags |= PF_FORKNOEXEC;
    1093                 :    1151894 :         p->flags = new_flags;
    1094                 :            : }
    1095                 :            : 
    1096                 :          0 : SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
    1097                 :            : {
    1098                 :       8464 :         current->clear_child_tid = tidptr;
    1099                 :            : 
    1100                 :       8464 :         return task_pid_vnr(current);
    1101                 :            : }
    1102                 :            : 
    1103                 :            : static void rt_mutex_init_task(struct task_struct *p)
    1104                 :            : {
    1105                 :    1151911 :         raw_spin_lock_init(&p->pi_lock);
    1106                 :            : #ifdef CONFIG_RT_MUTEXES
    1107                 :            :         plist_head_init(&p->pi_waiters);
    1108                 :    1151911 :         p->pi_blocked_on = NULL;
    1109                 :            : #endif
    1110                 :            : }
    1111                 :            : 
    1112                 :            : #ifdef CONFIG_MM_OWNER
    1113                 :            : void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
    1114                 :            : {
    1115                 :            :         mm->owner = p;
    1116                 :            : }
    1117                 :            : #endif /* CONFIG_MM_OWNER */
    1118                 :            : 
    1119                 :            : /*
    1120                 :            :  * Initialize POSIX timer handling for a single task.
    1121                 :            :  */
    1122                 :            : static void posix_cpu_timers_init(struct task_struct *tsk)
    1123                 :            : {
    1124                 :    1151898 :         tsk->cputime_expires.prof_exp = 0;
    1125                 :    1151898 :         tsk->cputime_expires.virt_exp = 0;
    1126                 :    1151898 :         tsk->cputime_expires.sched_exp = 0;
    1127                 :    1151898 :         INIT_LIST_HEAD(&tsk->cpu_timers[0]);
    1128                 :    1151898 :         INIT_LIST_HEAD(&tsk->cpu_timers[1]);
    1129                 :    1151898 :         INIT_LIST_HEAD(&tsk->cpu_timers[2]);
    1130                 :            : }
    1131                 :            : 
    1132                 :            : static inline void
    1133                 :            : init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
    1134                 :            : {
    1135                 :    3451140 :          task->pids[type].pid = pid;
    1136                 :            : }
    1137                 :            : 
    1138                 :            : /*
    1139                 :            :  * This creates a new process as a copy of the old one,
    1140                 :            :  * but does not actually start it yet.
    1141                 :            :  *
    1142                 :            :  * It copies the registers, and all the appropriate
    1143                 :            :  * parts of the process environment (as per the clone
    1144                 :            :  * flags). The actual kick-off is left to the caller.
    1145                 :            :  */
    1146                 :          0 : static struct task_struct *copy_process(unsigned long clone_flags,
    1147                 :            :                                         unsigned long stack_start,
    1148                 :            :                                         unsigned long stack_size,
    1149                 :            :                                         int __user *child_tidptr,
    1150                 :            :                                         struct pid *pid,
    1151                 :            :                                         int trace)
    1152                 :            : {
    1153                 :            :         int retval;
    1154                 :            :         struct task_struct *p;
    1155                 :            : 
    1156            [ + ]:    1151921 :         if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
    1157                 :            :                 return ERR_PTR(-EINVAL);
    1158                 :            : 
    1159         [ +  - ]:    1151922 :         if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
    1160                 :            :                 return ERR_PTR(-EINVAL);
    1161                 :            : 
    1162                 :            :         /*
    1163                 :            :          * Thread groups must share signals as well, and detached threads
    1164                 :            :          * can only be started up within the thread group.
    1165                 :            :          */
    1166         [ +  + ]:    1151922 :         if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
    1167                 :            :                 return ERR_PTR(-EINVAL);
    1168                 :            : 
    1169                 :            :         /*
    1170                 :            :          * Shared signal handlers imply shared VM. By way of the above,
    1171                 :            :          * thread groups also imply shared VM. Blocking this case allows
    1172                 :            :          * for various simplifications in other code.
    1173                 :            :          */
    1174            [ + ]:    1151921 :         if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
    1175                 :            :                 return ERR_PTR(-EINVAL);
    1176                 :            : 
    1177                 :            :         /*
    1178                 :            :          * Siblings of global init remain as zombies on exit since they are
    1179                 :            :          * not reaped by their parent (swapper). To solve this and to avoid
    1180                 :            :          * multi-rooted process trees, prevent global and container-inits
    1181                 :            :          * from creating siblings.
    1182                 :            :          */
    1183    [ +  + ][ + ]:    1151925 :         if ((clone_flags & CLONE_PARENT) &&
    1184                 :          1 :                                 current->signal->flags & SIGNAL_UNKILLABLE)
    1185                 :            :                 return ERR_PTR(-EINVAL);
    1186                 :            : 
    1187                 :            :         /*
    1188                 :            :          * If the new process will be in a different pid or user namespace
    1189                 :            :          * do not allow it to share a thread group or signal handlers or
    1190                 :            :          * parent with the forking task.
    1191                 :            :          */
    1192         [ +  + ]:    1151918 :         if (clone_flags & CLONE_SIGHAND) {
    1193   [ +  -  +  - ]:       4634 :                 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
    1194                 :       2317 :                     (task_active_pid_ns(current) !=
    1195                 :       2317 :                                 current->nsproxy->pid_ns_for_children))
    1196                 :            :                         return ERR_PTR(-EINVAL);
    1197                 :            :         }
    1198                 :            : 
    1199                 :    1151918 :         retval = security_task_create(clone_flags);
    1200            [ + ]:    1151918 :         if (retval)
    1201                 :            :                 goto fork_out;
    1202                 :            : 
    1203                 :            :         retval = -ENOMEM;
    1204                 :    1151929 :         p = dup_task_struct(current);
    1205         [ +  - ]:    1151916 :         if (!p)
    1206                 :            :                 goto fork_out;
    1207                 :            : 
    1208                 :            :         ftrace_graph_init_task(p);
    1209                 :    1151916 :         get_seccomp_filter(p);
    1210                 :            : 
    1211                 :            :         rt_mutex_init_task(p);
    1212                 :            : 
    1213                 :            : #ifdef CONFIG_PROVE_LOCKING
    1214                 :            :         DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
    1215                 :            :         DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
    1216                 :            : #endif
    1217                 :            :         retval = -EAGAIN;
    1218         [ +  + ]:    1151911 :         if (atomic_read(&p->real_cred->user->processes) >=
    1219                 :            :                         task_rlimit(p, RLIMIT_NPROC)) {
    1220   [ -  +  #  # ]:         20 :                 if (p->real_cred->user != INIT_USER &&
    1221         [ #  # ]:          0 :                     !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
    1222                 :            :                         goto bad_fork_free;
    1223                 :            :         }
    1224                 :    1151911 :         current->flags &= ~PF_NPROC_EXCEEDED;
    1225                 :            : 
    1226                 :    1151911 :         retval = copy_creds(p, clone_flags);
    1227         [ +  + ]:    1151928 :         if (retval < 0)
    1228                 :            :                 goto bad_fork_free;
    1229                 :            : 
    1230                 :            :         /*
    1231                 :            :          * If multiple threads are within copy_process(), then this check
    1232                 :            :          * triggers too late. This doesn't hurt, the check is only there
    1233                 :            :          * to stop root fork bombs.
    1234                 :            :          */
    1235                 :            :         retval = -EAGAIN;
    1236            [ + ]:    1151920 :         if (nr_threads >= max_threads)
    1237                 :            :                 goto bad_fork_cleanup_count;
    1238                 :            : 
    1239         [ +  + ]:    1151921 :         if (!try_module_get(task_thread_info(p)->exec_domain->module))
    1240                 :            :                 goto bad_fork_cleanup_count;
    1241                 :            : 
    1242                 :    1151894 :         p->did_exec = 0;
    1243                 :            :         delayacct_tsk_init(p);  /* Must remain after dup_task_struct() */
    1244                 :            :         copy_flags(clone_flags, p);
    1245                 :    1151894 :         INIT_LIST_HEAD(&p->children);
    1246                 :    1151894 :         INIT_LIST_HEAD(&p->sibling);
    1247                 :            :         rcu_copy_process(p);
    1248                 :    1151894 :         p->vfork_done = NULL;
    1249                 :    1151894 :         spin_lock_init(&p->alloc_lock);
    1250                 :            : 
    1251                 :            :         init_sigpending(&p->pending);
    1252                 :            : 
    1253                 :    1151894 :         p->utime = p->stime = p->gtime = 0;
    1254                 :    1151894 :         p->utimescaled = p->stimescaled = 0;
    1255                 :            : #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
    1256                 :    1151894 :         p->prev_cputime.utime = p->prev_cputime.stime = 0;
    1257                 :            : #endif
    1258                 :            : #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
    1259                 :            :         seqlock_init(&p->vtime_seqlock);
    1260                 :            :         p->vtime_snap = 0;
    1261                 :            :         p->vtime_snap_whence = VTIME_SLEEPING;
    1262                 :            : #endif
    1263                 :            : 
    1264                 :            : #if defined(SPLIT_RSS_COUNTING)
    1265                 :    1151894 :         memset(&p->rss_stat, 0, sizeof(p->rss_stat));
    1266                 :            : #endif
    1267                 :            : 
    1268                 :    1151898 :         p->default_timer_slack_ns = current->timer_slack_ns;
    1269                 :            : 
    1270                 :            :         task_io_accounting_init(&p->ioac);
    1271                 :            :         acct_clear_integrals(p);
    1272                 :            : 
    1273                 :            :         posix_cpu_timers_init(p);
    1274                 :            : 
    1275                 :    1151898 :         do_posix_clock_monotonic_gettime(&p->start_time);
    1276                 :    1151929 :         p->real_start_time = p->start_time;
    1277                 :    1151929 :         monotonic_to_bootbased(&p->real_start_time);
    1278                 :    1151875 :         p->io_context = NULL;
    1279                 :    1151875 :         p->audit_context = NULL;
    1280         [ +  + ]:    1151875 :         if (clone_flags & CLONE_THREAD)
    1281                 :       2316 :                 threadgroup_change_begin(current);
    1282                 :    1151875 :         cgroup_fork(p);
    1283                 :            : #ifdef CONFIG_NUMA
    1284                 :            :         p->mempolicy = mpol_dup(p->mempolicy);
    1285                 :            :         if (IS_ERR(p->mempolicy)) {
    1286                 :            :                 retval = PTR_ERR(p->mempolicy);
    1287                 :            :                 p->mempolicy = NULL;
    1288                 :            :                 goto bad_fork_cleanup_cgroup;
    1289                 :            :         }
    1290                 :            :         mpol_fix_fork_child_flag(p);
    1291                 :            : #endif
    1292                 :            : #ifdef CONFIG_CPUSETS
    1293                 :            :         p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
    1294                 :            :         p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
    1295                 :            :         seqcount_init(&p->mems_allowed_seq);
    1296                 :            : #endif
    1297                 :            : #ifdef CONFIG_TRACE_IRQFLAGS
    1298                 :            :         p->irq_events = 0;
    1299                 :            :         p->hardirqs_enabled = 0;
    1300                 :            :         p->hardirq_enable_ip = 0;
    1301                 :            :         p->hardirq_enable_event = 0;
    1302                 :            :         p->hardirq_disable_ip = _THIS_IP_;
    1303                 :            :         p->hardirq_disable_event = 0;
    1304                 :            :         p->softirqs_enabled = 1;
    1305                 :            :         p->softirq_enable_ip = _THIS_IP_;
    1306                 :            :         p->softirq_enable_event = 0;
    1307                 :            :         p->softirq_disable_ip = 0;
    1308                 :            :         p->softirq_disable_event = 0;
    1309                 :            :         p->hardirq_context = 0;
    1310                 :            :         p->softirq_context = 0;
    1311                 :            : #endif
    1312                 :            : #ifdef CONFIG_LOCKDEP
    1313                 :            :         p->lockdep_depth = 0; /* no locks held yet */
    1314                 :            :         p->curr_chain_key = 0;
    1315                 :            :         p->lockdep_recursion = 0;
    1316                 :            : #endif
    1317                 :            : 
    1318                 :            : #ifdef CONFIG_DEBUG_MUTEXES
    1319                 :            :         p->blocked_on = NULL; /* not blocked yet */
    1320                 :            : #endif
    1321                 :            : #ifdef CONFIG_MEMCG
    1322                 :            :         p->memcg_batch.do_batch = 0;
    1323                 :            :         p->memcg_batch.memcg = NULL;
    1324                 :            : #endif
    1325                 :            : #ifdef CONFIG_BCACHE
    1326                 :            :         p->sequential_io     = 0;
    1327                 :            :         p->sequential_io_avg = 0;
    1328                 :            : #endif
    1329                 :            : 
    1330                 :            :         /* Perform scheduler related setup. Assign this task to a CPU. */
    1331                 :    1151898 :         sched_fork(clone_flags, p);
    1332                 :            : 
    1333                 :    1151923 :         retval = perf_event_init_task(p);
    1334            [ + ]:    1151872 :         if (retval)
    1335                 :            :                 goto bad_fork_cleanup_policy;
    1336                 :    1151873 :         retval = audit_alloc(p);
    1337         [ +  + ]:    1151928 :         if (retval)
    1338                 :            :                 goto bad_fork_cleanup_policy;
    1339                 :            :         /* copy all the process information */
    1340                 :    1151915 :         retval = copy_semundo(clone_flags, p);
    1341         [ +  + ]:    1151887 :         if (retval)
    1342                 :            :                 goto bad_fork_cleanup_audit;
    1343                 :    1151882 :         retval = copy_files(clone_flags, p);
    1344         [ +  - ]:    1151921 :         if (retval)
    1345                 :            :                 goto bad_fork_cleanup_semundo;
    1346                 :    1151921 :         retval = copy_fs(clone_flags, p);
    1347         [ +  - ]:    1151926 :         if (retval)
    1348                 :            :                 goto bad_fork_cleanup_files;
    1349                 :    1151926 :         retval = copy_sighand(clone_flags, p);
    1350            [ + ]:    1151906 :         if (retval)
    1351                 :            :                 goto bad_fork_cleanup_fs;
    1352                 :    1151907 :         retval = copy_signal(clone_flags, p);
    1353         [ +  - ]:    1151884 :         if (retval)
    1354                 :            :                 goto bad_fork_cleanup_sighand;
    1355                 :    1151884 :         retval = copy_mm(clone_flags, p);
    1356         [ +  - ]:    1151929 :         if (retval)
    1357                 :            :                 goto bad_fork_cleanup_signal;
    1358                 :    1151929 :         retval = copy_namespaces(clone_flags, p);
    1359         [ +  + ]:    1151927 :         if (retval)
    1360                 :            :                 goto bad_fork_cleanup_mm;
    1361                 :    1151924 :         retval = copy_io(clone_flags, p);
    1362         [ +  - ]:    1151924 :         if (retval)
    1363                 :            :                 goto bad_fork_cleanup_namespaces;
    1364                 :    1151924 :         retval = copy_thread(clone_flags, stack_start, stack_size, p);
    1365         [ +  - ]:    1151925 :         if (retval)
    1366                 :            :                 goto bad_fork_cleanup_io;
    1367                 :            : 
    1368         [ +  - ]:    1151925 :         if (pid != &init_struct_pid) {
    1369                 :            :                 retval = -ENOMEM;
    1370                 :    1151925 :                 pid = alloc_pid(p->nsproxy->pid_ns_for_children);
    1371         [ +  - ]:    1151925 :                 if (!pid)
    1372                 :            :                         goto bad_fork_cleanup_io;
    1373                 :            :         }
    1374                 :            : 
    1375         [ +  + ]:    1151925 :         p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
    1376                 :            :         /*
    1377                 :            :          * Clear TID on mm_release()?
    1378                 :            :          */
    1379         [ +  + ]:    1151925 :         p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
    1380                 :            : #ifdef CONFIG_BLOCK
    1381                 :    1151925 :         p->plug = NULL;
    1382                 :            : #endif
    1383                 :            : #ifdef CONFIG_FUTEX
    1384                 :    1151925 :         p->robust_list = NULL;
    1385                 :            : #ifdef CONFIG_COMPAT
    1386                 :            :         p->compat_robust_list = NULL;
    1387                 :            : #endif
    1388                 :    1151925 :         INIT_LIST_HEAD(&p->pi_state_list);
    1389                 :    1151925 :         p->pi_state_cache = NULL;
    1390                 :            : #endif
    1391                 :            :         /*
    1392                 :            :          * sigaltstack should be cleared when sharing the same VM
    1393                 :            :          */
    1394         [ +  + ]:    1151925 :         if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
    1395                 :       2711 :                 p->sas_ss_sp = p->sas_ss_size = 0;
    1396                 :            : 
    1397                 :            :         /*
    1398                 :            :          * Syscall tracing and stepping should be turned off in the
    1399                 :            :          * child regardless of CLONE_PTRACE.
    1400                 :            :          */
    1401                 :            :         user_disable_single_step(p);
    1402                 :            :         clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
    1403                 :            : #ifdef TIF_SYSCALL_EMU
    1404                 :            :         clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
    1405                 :            : #endif
    1406                 :            :         clear_all_latency_tracing(p);
    1407                 :            : 
    1408                 :            :         /* ok, now we should be set up.. */
    1409                 :    1151925 :         p->pid = pid_nr(pid);
    1410         [ +  + ]:    1151925 :         if (clone_flags & CLONE_THREAD) {
    1411                 :       2316 :                 p->exit_signal = -1;
    1412                 :       2316 :                 p->group_leader = current->group_leader;
    1413                 :       2316 :                 p->tgid = current->tgid;
    1414                 :            :         } else {
    1415         [ +  + ]:    1149609 :                 if (clone_flags & CLONE_PARENT)
    1416                 :          1 :                         p->exit_signal = current->group_leader->exit_signal;
    1417                 :            :                 else
    1418                 :    1149608 :                         p->exit_signal = (clone_flags & CSIGNAL);
    1419                 :    1149609 :                 p->group_leader = p;
    1420                 :    1149609 :                 p->tgid = p->pid;
    1421                 :            :         }
    1422                 :            : 
    1423                 :    1151925 :         p->pdeath_signal = 0;
    1424                 :    1151925 :         p->exit_state = 0;
    1425                 :            : 
    1426                 :    1151925 :         p->nr_dirtied = 0;
    1427                 :    1151925 :         p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
    1428                 :    1151925 :         p->dirty_paused_when = 0;
    1429                 :            : 
    1430                 :    1151925 :         INIT_LIST_HEAD(&p->thread_group);
    1431                 :    1151925 :         p->task_works = NULL;
    1432                 :            : 
    1433                 :            :         /*
    1434                 :            :          * Make it visible to the rest of the system, but dont wake it up yet.
    1435                 :            :          * Need tasklist lock for parent etc handling!
    1436                 :            :          */
    1437                 :    1151925 :         write_lock_irq(&tasklist_lock);
    1438                 :            : 
    1439                 :            :         /* CLONE_PARENT re-uses the old parent */
    1440         [ +  + ]:    1151925 :         if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
    1441                 :       2317 :                 p->real_parent = current->real_parent;
    1442                 :       2317 :                 p->parent_exec_id = current->parent_exec_id;
    1443                 :            :         } else {
    1444                 :    1149608 :                 p->real_parent = current;
    1445                 :    1149608 :                 p->parent_exec_id = current->self_exec_id;
    1446                 :            :         }
    1447                 :            : 
    1448                 :    1151925 :         spin_lock(&current->sighand->siglock);
    1449                 :            : 
    1450                 :            :         /*
    1451                 :            :          * Process group and session signals need to be delivered to just the
    1452                 :            :          * parent before the fork or both the parent and the child after the
    1453                 :            :          * fork. Restart if a signal comes in before we add the new process to
    1454                 :            :          * it's process group.
    1455                 :            :          * A fatal signal pending means that current will exit, so the new
    1456                 :            :          * thread can't slip out of an OOM kill (or normal SIGKILL).
    1457                 :            :         */
    1458                 :    1151925 :         recalc_sigpending();
    1459         [ +  + ]:    1151925 :         if (signal_pending(current)) {
    1460                 :          1 :                 spin_unlock(&current->sighand->siglock);
    1461                 :            :                 write_unlock_irq(&tasklist_lock);
    1462                 :            :                 retval = -ERESTARTNOINTR;
    1463                 :            :                 goto bad_fork_free_pid;
    1464                 :            :         }
    1465                 :            : 
    1466         [ +  - ]:    1151924 :         if (likely(p->pid)) {
    1467 [ +  - ][ +  + ]:    1151924 :                 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
    1468                 :            : 
    1469                 :            :                 init_task_pid(p, PIDTYPE_PID, pid);
    1470         [ +  + ]:    1151924 :                 if (thread_group_leader(p)) {
    1471                 :    1149608 :                         init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
    1472                 :    1149608 :                         init_task_pid(p, PIDTYPE_SID, task_session(current));
    1473                 :            : 
    1474         [ -  + ]:    1149608 :                         if (is_child_reaper(pid)) {
    1475                 :          0 :                                 ns_of_pid(pid)->child_reaper = p;
    1476                 :          0 :                                 p->signal->flags |= SIGNAL_UNKILLABLE;
    1477                 :            :                         }
    1478                 :            : 
    1479                 :    1149608 :                         p->signal->leader_pid = pid;
    1480                 :    3448824 :                         p->signal->tty = tty_kref_get(current->signal->tty);
    1481                 :    1149608 :                         list_add_tail(&p->sibling, &p->real_parent->children);
    1482                 :    1149608 :                         list_add_tail_rcu(&p->tasks, &init_task.tasks);
    1483                 :    1149608 :                         attach_pid(p, PIDTYPE_PGID);
    1484                 :    1149608 :                         attach_pid(p, PIDTYPE_SID);
    1485                 :    1149608 :                         __this_cpu_inc(process_counts);
    1486                 :            :                 } else {
    1487                 :       2316 :                         current->signal->nr_threads++;
    1488                 :       2316 :                         atomic_inc(&current->signal->live);
    1489                 :       2316 :                         atomic_inc(&current->signal->sigcnt);
    1490                 :       2316 :                         list_add_tail_rcu(&p->thread_group,
    1491                 :       2316 :                                           &p->group_leader->thread_group);
    1492                 :            :                 }
    1493                 :    1151924 :                 attach_pid(p, PIDTYPE_PID);
    1494                 :    1151924 :                 nr_threads++;
    1495                 :            :         }
    1496                 :            : 
    1497                 :    1151924 :         total_forks++;
    1498                 :    1151924 :         spin_unlock(&current->sighand->siglock);
    1499                 :            :         write_unlock_irq(&tasklist_lock);
    1500                 :    1151924 :         proc_fork_connector(p);
    1501                 :    1151924 :         cgroup_post_fork(p);
    1502         [ +  + ]:    1151924 :         if (clone_flags & CLONE_THREAD)
    1503                 :       2316 :                 threadgroup_change_end(current);
    1504                 :    1151924 :         perf_event_fork(p);
    1505                 :            : 
    1506                 :            :         trace_task_newtask(p, clone_flags);
    1507                 :            :         uprobe_copy_process(p, clone_flags);
    1508                 :            : 
    1509                 :    1151924 :         return p;
    1510                 :            : 
    1511                 :            : bad_fork_free_pid:
    1512         [ +  - ]:          1 :         if (pid != &init_struct_pid)
    1513                 :          1 :                 free_pid(pid);
    1514                 :            : bad_fork_cleanup_io:
    1515         [ -  + ]:          1 :         if (p->io_context)
    1516                 :          0 :                 exit_io_context(p);
    1517                 :            : bad_fork_cleanup_namespaces:
    1518                 :          1 :         exit_task_namespaces(p);
    1519                 :            : bad_fork_cleanup_mm:
    1520         [ +  - ]:          5 :         if (p->mm)
    1521                 :          5 :                 mmput(p->mm);
    1522                 :            : bad_fork_cleanup_signal:
    1523         [ +  - ]:          5 :         if (!(clone_flags & CLONE_THREAD))
    1524                 :          5 :                 free_signal_struct(p->signal);
    1525                 :            : bad_fork_cleanup_sighand:
    1526                 :          5 :         __cleanup_sighand(p->sighand);
    1527                 :            : bad_fork_cleanup_fs:
    1528                 :          4 :         exit_fs(p); /* blocking */
    1529                 :            : bad_fork_cleanup_files:
    1530                 :          5 :         exit_files(p); /* blocking */
    1531                 :            : bad_fork_cleanup_semundo:
    1532                 :          5 :         exit_sem(p);
    1533                 :            : bad_fork_cleanup_audit:
    1534                 :            :         audit_free(p);
    1535                 :            : bad_fork_cleanup_policy:
    1536                 :         17 :         perf_event_free_task(p);
    1537                 :            : #ifdef CONFIG_NUMA
    1538                 :            :         mpol_put(p->mempolicy);
    1539                 :            : bad_fork_cleanup_cgroup:
    1540                 :            : #endif
    1541         [ -  + ]:          5 :         if (clone_flags & CLONE_THREAD)
    1542                 :          0 :                 threadgroup_change_end(current);
    1543                 :          5 :         cgroup_exit(p, 0);
    1544                 :            :         delayacct_tsk_free(p);
    1545                 :          5 :         module_put(task_thread_info(p)->exec_domain->module);
    1546                 :            : bad_fork_cleanup_count:
    1547                 :         12 :         atomic_dec(&p->cred->user->processes);
    1548                 :          5 :         exit_creds(p);
    1549                 :            : bad_fork_free:
    1550                 :         13 :         free_task(p);
    1551                 :            : fork_out:
    1552                 :          5 :         return ERR_PTR(retval);
    1553                 :            : }
    1554                 :            : 
    1555                 :            : static inline void init_idle_pids(struct pid_link *links)
    1556                 :            : {
    1557                 :            :         enum pid_type type;
    1558                 :            : 
    1559         [ #  # ]:          0 :         for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
    1560                 :          0 :                 INIT_HLIST_NODE(&links[type].node); /* not really needed */
    1561                 :          0 :                 links[type].pid = &init_struct_pid;
    1562                 :            :         }
    1563                 :            : }
    1564                 :            : 
    1565                 :          0 : struct task_struct *fork_idle(int cpu)
    1566                 :            : {
    1567                 :            :         struct task_struct *task;
    1568                 :          0 :         task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0);
    1569         [ #  # ]:          0 :         if (!IS_ERR(task)) {
    1570                 :          0 :                 init_idle_pids(task->pids);
    1571                 :          0 :                 init_idle(task, cpu);
    1572                 :            :         }
    1573                 :            : 
    1574                 :          0 :         return task;
    1575                 :            : }
    1576                 :            : 
    1577                 :            : /*
    1578                 :            :  *  Ok, this is the main fork-routine.
    1579                 :            :  *
    1580                 :            :  * It copies the process, and if successful kick-starts
    1581                 :            :  * it and waits for it to finish using the VM if required.
    1582                 :            :  */
    1583                 :          0 : long do_fork(unsigned long clone_flags,
    1584                 :            :               unsigned long stack_start,
    1585                 :            :               unsigned long stack_size,
    1586                 :            :               int __user *parent_tidptr,
    1587                 :            :               int __user *child_tidptr)
    1588                 :            : {
    1589                 :            :         struct task_struct *p;
    1590                 :            :         int trace = 0;
    1591                 :            :         long nr;
    1592                 :            : 
    1593                 :            :         /*
    1594                 :            :          * Determine whether and which event to report to ptracer.  When
    1595                 :            :          * called from kernel_thread or CLONE_UNTRACED is explicitly
    1596                 :            :          * requested, no event is reported; otherwise, report if the event
    1597                 :            :          * for the type of forking is enabled.
    1598                 :            :          */
    1599         [ +  + ]:    1151929 :         if (!(clone_flags & CLONE_UNTRACED)) {
    1600         [ +  + ]:    1151422 :                 if (clone_flags & CLONE_VFORK)
    1601                 :            :                         trace = PTRACE_EVENT_VFORK;
    1602         [ +  + ]:    1139791 :                 else if ((clone_flags & CSIGNAL) != SIGCHLD)
    1603                 :            :                         trace = PTRACE_EVENT_CLONE;
    1604                 :            :                 else
    1605                 :            :                         trace = PTRACE_EVENT_FORK;
    1606                 :            : 
    1607         [ +  + ]:    1151422 :                 if (likely(!ptrace_event_enabled(current, trace)))
    1608                 :            :                         trace = 0;
    1609                 :            :         }
    1610                 :            : 
    1611                 :    1151929 :         p = copy_process(clone_flags, stack_start, stack_size,
    1612                 :            :                          child_tidptr, NULL, trace);
    1613                 :            :         /*
    1614                 :            :          * Do this prior waking up the new thread - the thread pointer
    1615                 :            :          * might get invalid after that point, if the thread exits quickly.
    1616                 :            :          */
    1617         [ +  + ]:    1151928 :         if (!IS_ERR(p)) {
    1618                 :            :                 struct completion vfork;
    1619                 :            : 
    1620                 :    1151923 :                 trace_sched_process_fork(current, p);
    1621                 :            : 
    1622                 :            :                 nr = task_pid_vnr(p);
    1623                 :            : 
    1624         [ +  + ]:    1151924 :                 if (clone_flags & CLONE_PARENT_SETTID)
    1625                 :       2685 :                         put_user(nr, parent_tidptr);
    1626                 :            : 
    1627         [ +  + ]:    1151924 :                 if (clone_flags & CLONE_VFORK) {
    1628                 :      11743 :                         p->vfork_done = &vfork;
    1629                 :            :                         init_completion(&vfork);
    1630                 :      11743 :                         get_task_struct(p);
    1631                 :            :                 }
    1632                 :            : 
    1633                 :    1151924 :                 wake_up_new_task(p);
    1634                 :            : 
    1635                 :            :                 /* forking complete and child started to run, tell ptracer */
    1636         [ +  + ]:    1151924 :                 if (unlikely(trace))
    1637                 :         32 :                         ptrace_event(trace, nr);
    1638                 :            : 
    1639         [ +  + ]:    1151924 :                 if (clone_flags & CLONE_VFORK) {
    1640         [ +  - ]:      11743 :                         if (!wait_for_vfork_done(p, &vfork))
    1641                 :      11743 :                                 ptrace_event(PTRACE_EVENT_VFORK_DONE, nr);
    1642                 :            :                 }
    1643                 :            :         } else {
    1644                 :            :                 nr = PTR_ERR(p);
    1645                 :            :         }
    1646                 :    1151929 :         return nr;
    1647                 :            : }
    1648                 :            : 
    1649                 :            : /*
    1650                 :            :  * Create a kernel thread.
    1651                 :            :  */
    1652                 :          0 : pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
    1653                 :            : {
    1654                 :        505 :         return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
    1655                 :            :                 (unsigned long)arg, NULL, NULL);
    1656                 :            : }
    1657                 :            : 
    1658                 :            : #ifdef __ARCH_WANT_SYS_FORK
    1659                 :          0 : SYSCALL_DEFINE0(fork)
    1660                 :            : {
    1661                 :            : #ifdef CONFIG_MMU
    1662                 :          0 :         return do_fork(SIGCHLD, 0, 0, NULL, NULL);
    1663                 :            : #else
    1664                 :            :         /* can not support in nommu mode */
    1665                 :            :         return(-EINVAL);
    1666                 :            : #endif
    1667                 :            : }
    1668                 :            : #endif
    1669                 :            : 
    1670                 :            : #ifdef __ARCH_WANT_SYS_VFORK
    1671                 :          0 : SYSCALL_DEFINE0(vfork)
    1672                 :            : {
    1673                 :      11630 :         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 
    1674                 :            :                         0, NULL, NULL);
    1675                 :            : }
    1676                 :            : #endif
    1677                 :            : 
    1678                 :            : #ifdef __ARCH_WANT_SYS_CLONE
    1679                 :            : #ifdef CONFIG_CLONE_BACKWARDS
    1680                 :          0 : SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
    1681                 :            :                  int __user *, parent_tidptr,
    1682                 :            :                  int, tls_val,
    1683                 :            :                  int __user *, child_tidptr)
    1684                 :            : #elif defined(CONFIG_CLONE_BACKWARDS2)
    1685                 :            : SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
    1686                 :            :                  int __user *, parent_tidptr,
    1687                 :            :                  int __user *, child_tidptr,
    1688                 :            :                  int, tls_val)
    1689                 :            : #elif defined(CONFIG_CLONE_BACKWARDS3)
    1690                 :            : SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
    1691                 :            :                 int, stack_size,
    1692                 :            :                 int __user *, parent_tidptr,
    1693                 :            :                 int __user *, child_tidptr,
    1694                 :            :                 int, tls_val)
    1695                 :            : #else
    1696                 :            : SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
    1697                 :            :                  int __user *, parent_tidptr,
    1698                 :            :                  int __user *, child_tidptr,
    1699                 :            :                  int, tls_val)
    1700                 :            : #endif
    1701                 :            : {
    1702                 :    1139794 :         return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr);
    1703                 :            : }
    1704                 :            : #endif
    1705                 :            : 
    1706                 :            : #ifndef ARCH_MIN_MMSTRUCT_ALIGN
    1707                 :            : #define ARCH_MIN_MMSTRUCT_ALIGN 0
    1708                 :            : #endif
    1709                 :            : 
    1710                 :          0 : static void sighand_ctor(void *data)
    1711                 :            : {
    1712                 :            :         struct sighand_struct *sighand = data;
    1713                 :            : 
    1714                 :       4590 :         spin_lock_init(&sighand->siglock);
    1715                 :       4590 :         init_waitqueue_head(&sighand->signalfd_wqh);
    1716                 :       4590 : }
    1717                 :            : 
    1718                 :          0 : void __init proc_caches_init(void)
    1719                 :            : {
    1720                 :          0 :         sighand_cachep = kmem_cache_create("sighand_cache",
    1721                 :            :                         sizeof(struct sighand_struct), 0,
    1722                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
    1723                 :            :                         SLAB_NOTRACK, sighand_ctor);
    1724                 :          0 :         signal_cachep = kmem_cache_create("signal_cache",
    1725                 :            :                         sizeof(struct signal_struct), 0,
    1726                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
    1727                 :          0 :         files_cachep = kmem_cache_create("files_cache",
    1728                 :            :                         sizeof(struct files_struct), 0,
    1729                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
    1730                 :          0 :         fs_cachep = kmem_cache_create("fs_cache",
    1731                 :            :                         sizeof(struct fs_struct), 0,
    1732                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
    1733                 :            :         /*
    1734                 :            :          * FIXME! The "sizeof(struct mm_struct)" currently includes the
    1735                 :            :          * whole struct cpumask for the OFFSTACK case. We could change
    1736                 :            :          * this to *only* allocate as much of it as required by the
    1737                 :            :          * maximum number of CPU's we can ever have.  The cpumask_allocation
    1738                 :            :          * is at the end of the structure, exactly for that reason.
    1739                 :            :          */
    1740                 :          0 :         mm_cachep = kmem_cache_create("mm_struct",
    1741                 :            :                         sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
    1742                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
    1743                 :          0 :         vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
    1744                 :          0 :         mmap_init();
    1745                 :          0 :         nsproxy_cache_init();
    1746                 :          0 : }
    1747                 :            : 
    1748                 :            : /*
    1749                 :            :  * Check constraints on flags passed to the unshare system call.
    1750                 :            :  */
    1751                 :          0 : static int check_unshare_flags(unsigned long unshare_flags)
    1752                 :            : {
    1753         [ +  + ]:          7 :         if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
    1754                 :            :                                 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
    1755                 :            :                                 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
    1756                 :            :                                 CLONE_NEWUSER|CLONE_NEWPID))
    1757                 :            :                 return -EINVAL;
    1758                 :            :         /*
    1759                 :            :          * Not implemented, but pretend it works if there is nothing to
    1760                 :            :          * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
    1761                 :            :          * needs to unshare vm.
    1762                 :            :          */
    1763         [ -  + ]:          5 :         if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
    1764                 :            :                 /* FIXME: get_task_mm() increments ->mm_users */
    1765         [ #  # ]:          0 :                 if (atomic_read(&current->mm->mm_users) > 1)
    1766                 :            :                         return -EINVAL;
    1767                 :            :         }
    1768                 :            : 
    1769                 :          5 :         return 0;
    1770                 :            : }
    1771                 :            : 
    1772                 :            : /*
    1773                 :            :  * Unshare the filesystem structure if it is being shared
    1774                 :            :  */
    1775                 :          0 : static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
    1776                 :            : {
    1777                 :          5 :         struct fs_struct *fs = current->fs;
    1778                 :            : 
    1779 [ +  + ][ +  - ]:          5 :         if (!(unshare_flags & CLONE_FS) || !fs)
    1780                 :            :                 return 0;
    1781                 :            : 
    1782                 :            :         /* don't need lock here; in the worst case we'll do useless copy */
    1783         [ -  + ]:          2 :         if (fs->users == 1)
    1784                 :            :                 return 0;
    1785                 :            : 
    1786                 :          0 :         *new_fsp = copy_fs_struct(fs);
    1787         [ #  # ]:          0 :         if (!*new_fsp)
    1788                 :            :                 return -ENOMEM;
    1789                 :            : 
    1790                 :          0 :         return 0;
    1791                 :            : }
    1792                 :            : 
    1793                 :            : /*
    1794                 :            :  * Unshare file descriptor table if it is being shared
    1795                 :            :  */
    1796                 :          0 : static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
    1797                 :            : {
    1798                 :      98497 :         struct files_struct *fd = current->files;
    1799                 :      98497 :         int error = 0;
    1800                 :            : 
    1801 [ +  + ][ +  - ]:      98497 :         if ((unshare_flags & CLONE_FILES) &&
    1802         [ -  + ]:      98493 :             (fd && atomic_read(&fd->count) > 1)) {
    1803                 :          0 :                 *new_fdp = dup_fd(fd, &error);
    1804         [ #  # ]:          0 :                 if (!*new_fdp)
    1805                 :          0 :                         return error;
    1806                 :            :         }
    1807                 :            : 
    1808                 :            :         return 0;
    1809                 :            : }
    1810                 :            : 
    1811                 :            : /*
    1812                 :            :  * unshare allows a process to 'unshare' part of the process
    1813                 :            :  * context which was originally shared using clone.  copy_*
    1814                 :            :  * functions used by do_fork() cannot be used here directly
    1815                 :            :  * because they modify an inactive task_struct that is being
    1816                 :            :  * constructed. Here we are modifying the current, active,
    1817                 :            :  * task_struct.
    1818                 :            :  */
    1819                 :          0 : SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
    1820                 :            : {
    1821                 :          7 :         struct fs_struct *fs, *new_fs = NULL;
    1822                 :          7 :         struct files_struct *fd, *new_fd = NULL;
    1823                 :            :         struct cred *new_cred = NULL;
    1824                 :          7 :         struct nsproxy *new_nsproxy = NULL;
    1825                 :            :         int do_sysvsem = 0;
    1826                 :            :         int err;
    1827                 :            : 
    1828                 :            :         /*
    1829                 :            :          * If unsharing a user namespace must also unshare the thread.
    1830                 :            :          */
    1831         [ +  + ]:          7 :         if (unshare_flags & CLONE_NEWUSER)
    1832                 :          2 :                 unshare_flags |= CLONE_THREAD | CLONE_FS;
    1833                 :            :         /*
    1834                 :            :          * If unsharing a thread from a thread group, must also unshare vm.
    1835                 :            :          */
    1836         [ +  + ]:          7 :         if (unshare_flags & CLONE_THREAD)
    1837                 :          2 :                 unshare_flags |= CLONE_VM;
    1838                 :            :         /*
    1839                 :            :          * If unsharing vm, must also unshare signal handlers.
    1840                 :            :          */
    1841         [ +  + ]:          7 :         if (unshare_flags & CLONE_VM)
    1842                 :          2 :                 unshare_flags |= CLONE_SIGHAND;
    1843                 :            :         /*
    1844                 :            :          * If unsharing namespace, must also unshare filesystem information.
    1845                 :            :          */
    1846         [ +  + ]:          7 :         if (unshare_flags & CLONE_NEWNS)
    1847                 :          3 :                 unshare_flags |= CLONE_FS;
    1848                 :            : 
    1849                 :          7 :         err = check_unshare_flags(unshare_flags);
    1850         [ +  + ]:          7 :         if (err)
    1851                 :            :                 goto bad_unshare_out;
    1852                 :            :         /*
    1853                 :            :          * CLONE_NEWIPC must also detach from the undolist: after switching
    1854                 :            :          * to a new ipc namespace, the semaphore arrays from the old
    1855                 :            :          * namespace are unreachable.
    1856                 :            :          */
    1857         [ -  + ]:          5 :         if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
    1858                 :            :                 do_sysvsem = 1;
    1859                 :          5 :         err = unshare_fs(unshare_flags, &new_fs);
    1860         [ +  - ]:          5 :         if (err)
    1861                 :            :                 goto bad_unshare_out;
    1862                 :          5 :         err = unshare_fd(unshare_flags, &new_fd);
    1863         [ +  - ]:          5 :         if (err)
    1864                 :            :                 goto bad_unshare_cleanup_fs;
    1865                 :            :         err = unshare_userns(unshare_flags, &new_cred);
    1866         [ +  - ]:          5 :         if (err)
    1867                 :            :                 goto bad_unshare_cleanup_fd;
    1868                 :          5 :         err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
    1869                 :            :                                          new_cred, new_fs);
    1870         [ +  - ]:          5 :         if (err)
    1871                 :            :                 goto bad_unshare_cleanup_cred;
    1872                 :            : 
    1873 [ +  - ][ +  - ]:          5 :         if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
         [ +  - ][ +  + ]
    1874         [ -  + ]:          1 :                 if (do_sysvsem) {
    1875                 :            :                         /*
    1876                 :            :                          * CLONE_SYSVSEM is equivalent to sys_exit().
    1877                 :            :                          */
    1878                 :          0 :                         exit_sem(current);
    1879                 :            :                 }
    1880                 :            : 
    1881         [ +  - ]:          1 :                 if (new_nsproxy)
    1882                 :          1 :                         switch_task_namespaces(current, new_nsproxy);
    1883                 :            : 
    1884                 :          1 :                 task_lock(current);
    1885                 :            : 
    1886         [ -  + ]:          1 :                 if (new_fs) {
    1887                 :          0 :                         fs = current->fs;
    1888                 :            :                         spin_lock(&fs->lock);
    1889                 :          0 :                         current->fs = new_fs;
    1890         [ #  # ]:          0 :                         if (--fs->users)
    1891                 :          0 :                                 new_fs = NULL;
    1892                 :            :                         else
    1893                 :          0 :                                 new_fs = fs;
    1894                 :            :                         spin_unlock(&fs->lock);
    1895                 :            :                 }
    1896                 :            : 
    1897         [ -  + ]:          1 :                 if (new_fd) {
    1898                 :          0 :                         fd = current->files;
    1899                 :          0 :                         current->files = new_fd;
    1900                 :          0 :                         new_fd = fd;
    1901                 :            :                 }
    1902                 :            : 
    1903                 :          1 :                 task_unlock(current);
    1904                 :            : 
    1905                 :            :                 if (new_cred) {
    1906                 :            :                         /* Install the new user namespace */
    1907                 :            :                         commit_creds(new_cred);
    1908                 :            :                         new_cred = NULL;
    1909                 :            :                 }
    1910                 :            :         }
    1911                 :            : 
    1912                 :            : bad_unshare_cleanup_cred:
    1913                 :            :         if (new_cred)
    1914                 :            :                 put_cred(new_cred);
    1915                 :            : bad_unshare_cleanup_fd:
    1916         [ -  + ]:          5 :         if (new_fd)
    1917                 :          0 :                 put_files_struct(new_fd);
    1918                 :            : 
    1919                 :            : bad_unshare_cleanup_fs:
    1920         [ -  + ]:          5 :         if (new_fs)
    1921                 :          0 :                 free_fs_struct(new_fs);
    1922                 :            : 
    1923                 :            : bad_unshare_out:
    1924                 :            :         return err;
    1925                 :            : }
    1926                 :            : 
    1927                 :            : /*
    1928                 :            :  *      Helper to unshare the files of the current task.
    1929                 :            :  *      We don't want to expose copy_files internals to
    1930                 :            :  *      the exec layer of the kernel.
    1931                 :            :  */
    1932                 :            : 
    1933                 :          0 : int unshare_files(struct files_struct **displaced)
    1934                 :            : {
    1935                 :      98492 :         struct task_struct *task = current;
    1936                 :      98492 :         struct files_struct *copy = NULL;
    1937                 :            :         int error;
    1938                 :            : 
    1939                 :      98492 :         error = unshare_fd(CLONE_FILES, &copy);
    1940 [ +  - ][ +  - ]:     196983 :         if (error || !copy) {
    1941                 :      98491 :                 *displaced = NULL;
    1942                 :      98491 :                 return error;
    1943                 :            :         }
    1944                 :          0 :         *displaced = task->files;
    1945                 :            :         task_lock(task);
    1946                 :          0 :         task->files = copy;
    1947                 :            :         task_unlock(task);
    1948                 :          0 :         return 0;
    1949                 :            : }

Generated by: LCOV version 1.9