~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/fork.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp