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 [ + + ]: 14 : for_each_possible_cpu(cpu)
113 : 10 : total += per_cpu(process_counts, cpu);
114 : :
115 : 2 : return total;
116 : : }
117 : :
118 : 0 : void __weak arch_release_task_struct(struct task_struct *tsk)
119 : : {
120 : 1122666 : }
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 : 1122977 : 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 : 1122278 : 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 : 1122161 : }
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 : 1122985 : 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 [ + - ]: 1122984 : return page ? page_address(page) : NULL;
154 : : }
155 : :
156 : : static inline void free_thread_info(struct thread_info *ti)
157 : : {
158 : 1122182 : 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 : 6733482 : struct zone *zone = page_zone(virt_to_page(ti));
207 : :
208 : 2244494 : mod_zone_page_state(zone, NR_KERNEL_STACK, account);
209 : 2245228 : }
210 : :
211 : 0 : void free_task(struct task_struct *tsk)
212 : : {
213 : 1122279 : account_kernel_stack(tsk->stack, -1);
214 : 1122223 : arch_release_thread_info(tsk->stack);
215 : 1122182 : free_thread_info(tsk->stack);
216 : : rt_mutex_debug_task_free(tsk);
217 : : ftrace_graph_exit_task(tsk);
218 : 1122980 : put_seccomp_filter(tsk);
219 : 1122738 : arch_release_task_struct(tsk);
220 : : free_task_struct(tsk);
221 : 1121440 : }
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 : 1120006 : kmem_cache_free(signal_cachep, sig);
229 : : }
230 : :
231 : : static inline void put_signal_struct(struct signal_struct *sig)
232 : : {
233 [ + + ]: 1122401 : 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 [ - + ]: 1121857 : WARN_ON(!tsk->exit_state);
252 [ - + ]: 1121881 : WARN_ON(atomic_read(&tsk->usage));
253 [ - + ]: 1121881 : WARN_ON(tsk == current);
254 : :
255 : 1121881 : security_task_free(tsk);
256 : 1121853 : exit_creds(tsk);
257 : : delayacct_tsk_free(tsk);
258 : 1120948 : put_signal_struct(tsk->signal);
259 : :
260 : 1122611 : atomic_notifier_call_chain(&task_free_notifier, 0, tsk);
261 [ + ]: 1122397 : if (!profile_handoff_task(tsk))
262 : 1122194 : free_task(tsk);
263 : 1121629 : }
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 : 1122984 : *dst = *src;
306 : 1122984 : 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 : 1122979 : int node = tsk_fork_get_node(orig);
315 : : int err;
316 : :
317 : : tsk = alloc_task_struct_node(node);
318 [ + + ]: 1122986 : if (!tsk)
319 : : return NULL;
320 : :
321 : 1122984 : ti = alloc_thread_info_node(tsk, node);
322 [ + ]: 1122984 : if (!ti)
323 : : goto free_tsk;
324 : :
325 : 1122985 : err = arch_dup_task_struct(tsk, orig);
326 [ + - ]: 1122986 : if (err)
327 : : goto free_ti;
328 : :
329 : 1122986 : 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 : 1122987 : *stackend = STACK_END_MAGIC; /* for overflow detection */
336 : :
337 : : #ifdef CONFIG_CC_STACKPROTECTOR
338 : 1122987 : 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 : 1122968 : atomic_set(&tsk->usage, 2);
346 : : #ifdef CONFIG_BLK_DEV_IO_TRACE
347 : : tsk->btrace_seq = 0;
348 : : #endif
349 : 1122968 : tsk->splice_pipe = NULL;
350 : 1122968 : tsk->task_frag.page = NULL;
351 : :
352 : 1122968 : account_kernel_stack(ti, 1);
353 : :
354 : 1122983 : 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 : 12466579 : 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 : 1115019 : down_write(&oldmm->mmap_sem);
373 : 1115026 : flush_cache_dup_mm(oldmm);
374 : : uprobe_dup_mmap(oldmm, mm);
375 : : /*
376 : : * Not linked in yet - no deadlock potential:
377 : : */
378 : 1115020 : down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
379 : :
380 : 1115024 : mm->locked_vm = 0;
381 : 1115024 : mm->mmap = NULL;
382 : 1115024 : mm->mmap_cache = NULL;
383 : 1115024 : mm->map_count = 0;
384 : : cpumask_clear(mm_cpumask(mm));
385 : 1115024 : mm->mm_rb = RB_ROOT;
386 : 1115024 : rb_link = &mm->mm_rb.rb_node;
387 : : rb_parent = NULL;
388 : 1115024 : 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 [ + + ]: 19602807 : for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
398 : 11804901 : struct file *file;
399 : :
400 [ + + ]: 18487844 : if (mpnt->vm_flags & VM_DONTCOPY) {
401 : 297 : vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
402 : 297 : -vma_pages(mpnt));
403 : 0 : continue;
404 : : }
405 : : charge = 0;
406 [ + + ]: 18487547 : if (mpnt->vm_flags & VM_ACCOUNT) {
407 : : unsigned long len = vma_pages(mpnt);
408 : :
409 [ + ]: 12466282 : if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
410 : : goto fail_nomem;
411 : : charge = len;
412 : : }
413 : 18487565 : tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
414 [ + ]: 18487446 : if (!tmp)
415 : : goto fail_nomem;
416 : 18487479 : *tmp = *mpnt;
417 : 18487479 : INIT_LIST_HEAD(&tmp->anon_vma_chain);
418 : : retval = vma_dup_policy(mpnt, tmp);
419 : : if (retval)
420 : : goto fail_nomem_policy;
421 : 18487479 : tmp->vm_mm = mm;
422 [ + - ]: 18487479 : if (anon_vma_fork(tmp, mpnt))
423 : : goto fail_nomem_anon_vma_fork;
424 : 18487674 : tmp->vm_flags &= ~VM_LOCKED;
425 : 18487674 : tmp->vm_next = tmp->vm_prev = NULL;
426 : 18487674 : file = tmp->vm_file;
427 [ + + ]: 18487674 : if (file) {
428 : : struct inode *inode = file_inode(file);
429 : 11804901 : struct address_space *mapping = file->f_mapping;
430 : :
431 : : get_file(file);
432 [ + + ]: 11804972 : if (tmp->vm_flags & VM_DENYWRITE)
433 : 6690139 : atomic_dec(&inode->i_writecount);
434 : 11804965 : mutex_lock(&mapping->i_mmap_mutex);
435 [ + + ]: 11805039 : if (tmp->vm_flags & VM_SHARED)
436 : 10286 : mapping->i_mmap_writable++;
437 : : flush_dcache_mmap_lock(mapping);
438 : : /* insert tmp into the share list, just after mpnt */
439 [ - + ]: 11805038 : if (unlikely(tmp->vm_flags & VM_NONLINEAR))
440 : 0 : vma_nonlinear_insert(tmp,
441 : : &mapping->i_mmap_nonlinear);
442 : : else
443 : 11805038 : vma_interval_tree_insert_after(tmp, mpnt,
444 : : &mapping->i_mmap);
445 : : flush_dcache_mmap_unlock(mapping);
446 : 11805022 : 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 : 18487874 : *pprev = tmp;
461 : 18487874 : pprev = &tmp->vm_next;
462 : 18487874 : tmp->vm_prev = prev;
463 : : prev = tmp;
464 : :
465 : 18487874 : __vma_link_rb(mm, tmp, rb_link, rb_parent);
466 : 18487925 : rb_link = &tmp->vm_rb.rb_right;
467 : 18487925 : rb_parent = &tmp->vm_rb;
468 : :
469 : 18487925 : mm->map_count++;
470 : 18487925 : retval = copy_page_range(mm, oldmm, mpnt);
471 : :
472 [ + + ][ + + ]: 18487676 : if (tmp->vm_ops && tmp->vm_ops->open)
473 : 8 : tmp->vm_ops->open(tmp);
474 : :
475 [ + - ]: 18487783 : 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 : 1114963 : up_write(&mm->mmap_sem);
483 : 1115026 : flush_tlb_mm(oldmm);
484 : 1115026 : up_write(&oldmm->mmap_sem);
485 : : uprobe_end_dup_mmap();
486 : 1115026 : 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 : 1156127 : mm->pgd = pgd_alloc(mm);
500 [ + ]: 2312258 : 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 : 1156129 : 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 : 1156126 : spin_lock_init(&mm->ioctx_lock);
538 : 1156126 : 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 : 1156127 : atomic_set(&mm->mm_users, 1);
545 : 1156127 : atomic_set(&mm->mm_count, 1);
546 : 1156127 : init_rwsem(&mm->mmap_sem);
547 : 1156126 : INIT_LIST_HEAD(&mm->mmlist);
548 : 2312252 : mm->flags = (current->mm) ?
549 [ + + ]: 1156126 : (current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
550 : 1156126 : mm->core_state = NULL;
551 : : atomic_long_set(&mm->nr_ptes, 0);
552 : 1156126 : memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
553 : 1156126 : 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 [ + - ]: 1156131 : if (likely(!mm_alloc_pgd(mm))) {
559 : 1156131 : 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 [ + + ]: 4624536 : for (i = 0; i < NR_MM_COUNTERS; i++) {
573 : 3468402 : long x = atomic_long_read(&mm->rss_stat.count[i]);
574 : :
575 [ - + ]: 3468402 : 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 : 1156134 : }
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 : 41111 : mm = allocate_mm();
593 [ + - ]: 41112 : if (!mm)
594 : : return NULL;
595 : :
596 : 41112 : memset(mm, 0, sizeof(*mm));
597 : : mm_init_cpumask(mm);
598 : 41113 : 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 [ - + ]: 1156129 : BUG_ON(mm == &init_mm);
609 : : mm_free_pgd(mm);
610 : : destroy_context(mm);
611 : : mmu_notifier_mm_destroy(mm);
612 : 1156134 : check_mm(mm);
613 : 1156134 : free_mm(mm);
614 : 1156129 : }
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 [ + + ]: 1387258 : if (atomic_dec_and_test(&mm->mm_users)) {
625 : : uprobe_clear_state(mm);
626 : 1156125 : exit_aio(mm);
627 : : ksm_exit(mm);
628 : : khugepaged_exit(mm); /* must run before exit_mmap */
629 : 1156108 : exit_mmap(mm);
630 : 1156131 : set_mm_exe_file(mm, NULL);
631 [ - + ]: 1156131 : if (!list_empty(&mm->mmlist)) {
632 : : spin_lock(&mmlist_lock);
633 : : list_del(&mm->mmlist);
634 : : spin_unlock(&mmlist_lock);
635 : : }
636 [ + + ]: 1156129 : if (mm->binfmt)
637 : 1156121 : module_put(mm->binfmt->module);
638 : : mmdrop(mm);
639 : : }
640 : 2 : }
641 : : EXPORT_SYMBOL_GPL(mmput);
642 : :
643 : 0 : void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
644 : : {
645 [ + + ]: 1197229 : if (new_exe_file)
646 : : get_file(new_exe_file);
647 [ + + ]: 1197227 : if (mm->exe_file)
648 : 1156122 : fput(mm->exe_file);
649 : 1197231 : mm->exe_file = new_exe_file;
650 : 1197231 : }
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 : 1117609 : down_read(&mm->mmap_sem);
658 : 1117609 : exe_file = mm->exe_file;
659 [ + + ]: 1117609 : if (exe_file)
660 : : get_file(exe_file);
661 : 1117607 : up_read(&mm->mmap_sem);
662 : 1117607 : 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 : 1115023 : 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 : 257266 : mm = task->mm;
687 [ + + ]: 257266 : if (mm) {
688 [ + - ]: 223942 : if (task->flags & PF_KTHREAD)
689 : : mm = NULL;
690 : : else
691 : 223942 : atomic_inc(&mm->mm_users);
692 : : }
693 : : task_unlock(task);
694 : 257266 : 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 : 71662 : err = mutex_lock_killable(&task->signal->cred_guard_mutex);
704 [ - + ]: 71662 : if (err)
705 : 0 : return ERR_PTR(err);
706 : :
707 : 71662 : mm = get_task_mm(task);
708 [ + - ]: 130648 : 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 : 71662 : mutex_unlock(&task->signal->cred_guard_mutex);
715 : :
716 : 71662 : 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 : 5656 : vfork = tsk->vfork_done;
725 [ + - ]: 5656 : if (likely(vfork)) {
726 : 5656 : tsk->vfork_done = NULL;
727 : 5656 : complete(vfork);
728 : : }
729 : : task_unlock(tsk);
730 : 5656 : }
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 : 5160 : killed = wait_for_completion_killable(vfork);
739 : : freezer_count();
740 : :
741 [ - + ]: 5160 : if (killed) {
742 : : task_lock(child);
743 : 0 : child->vfork_done = NULL;
744 : : task_unlock(child);
745 : : }
746 : :
747 : : put_task_struct(child);
748 : 5160 : 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 [ + + ]: 1164062 : if (unlikely(tsk->robust_list)) {
769 : 14210 : exit_robust_list(tsk);
770 : 14209 : 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 [ - + ]: 1164061 : 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 [ + + ]: 2328127 : if (tsk->clear_child_tid) {
795 [ + + ][ + + ]: 1122356 : if (!(tsk->flags & PF_SIGNALED) &&
796 : 1122060 : atomic_read(&mm->mm_users) > 1) {
797 : : /*
798 : : * We don't check the error code - if userspace has
799 : : * not set up a proper pointer then tough luck.
800 : : */
801 : 2201 : put_user(0, tsk->clear_child_tid);
802 : 2200 : sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
803 : : 1, NULL, NULL, 0);
804 : : }
805 : 1122350 : 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 [ + + ]: 1164059 : if (tsk->vfork_done)
813 : 5656 : complete_vfork_done(tsk);
814 : 1164059 : }
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 : 1115018 : struct mm_struct *mm, *oldmm = current->mm;
823 : : int err;
824 : :
825 [ + ]: 1115018 : if (!oldmm)
826 : : return NULL;
827 : :
828 : 1115019 : mm = allocate_mm();
829 [ + - ]: 1115023 : if (!mm)
830 : : goto fail_nomem;
831 : :
832 : 1115023 : 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 [ + + ]: 1115023 : if (!mm_init(mm, tsk))
839 : : goto fail_nomem;
840 : :
841 : 1115020 : if (init_new_context(tsk, mm))
842 : : goto fail_nocontext;
843 : :
844 : : dup_mm_exe_file(oldmm, mm);
845 : :
846 : 1115017 : err = dup_mmap(mm, oldmm);
847 [ + - ]: 1115026 : if (err)
848 : : goto free_pt;
849 : :
850 : 1115026 : mm->hiwater_rss = get_mm_rss(mm);
851 : 1115026 : mm->hiwater_vm = mm->total_vm;
852 : :
853 [ + ][ + ]: 1115026 : if (mm->binfmt && !try_module_get(mm->binfmt->module))
854 : : goto free_pt;
855 : :
856 : 1115025 : 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 : 1122978 : tsk->min_flt = tsk->maj_flt = 0;
882 : 1122978 : tsk->nvcsw = tsk->nivcsw = 0;
883 : : #ifdef CONFIG_DETECT_HUNG_TASK
884 : : tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
885 : : #endif
886 : :
887 : 1122978 : tsk->mm = NULL;
888 : 1122978 : 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 : 1122978 : oldmm = current->mm;
896 [ + + ]: 1122978 : if (!oldmm)
897 : : return 0;
898 : :
899 [ + + ]: 1122163 : if (clone_flags & CLONE_VM) {
900 : 7151 : atomic_inc(&oldmm->mm_users);
901 : : mm = oldmm;
902 : 7151 : goto good_mm;
903 : : }
904 : :
905 : : retval = -ENOMEM;
906 : 1115012 : mm = dup_mm(tsk);
907 [ + + ]: 1115026 : if (!mm)
908 : : goto fail_nomem;
909 : :
910 : : good_mm:
911 : 1122175 : tsk->mm = mm;
912 : 1122175 : tsk->active_mm = mm;
913 : 1122175 : 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 : 1122978 : struct fs_struct *fs = current->fs;
922 [ + + ]: 1122978 : if (clone_flags & CLONE_FS) {
923 : : /* tsk->fs is already what we want */
924 : : spin_lock(&fs->lock);
925 [ - + ]: 2776 : if (fs->in_exec) {
926 : : spin_unlock(&fs->lock);
927 : 0 : return -EAGAIN;
928 : : }
929 : 2776 : fs->users++;
930 : : spin_unlock(&fs->lock);
931 : 2776 : return 0;
932 : : }
933 : 1120203 : tsk->fs = copy_fs_struct(fs);
934 [ + - ]: 1120212 : if (!tsk->fs)
935 : : return -ENOMEM;
936 : 1120212 : 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 : 1122967 : int error = 0;
943 : :
944 : : /*
945 : : * A background process may not have any files ...
946 : : */
947 : 1122967 : oldf = current->files;
948 [ + + ]: 1122967 : if (!oldf)
949 : : goto out;
950 : :
951 [ + + ]: 1122966 : if (clone_flags & CLONE_FILES) {
952 : 2776 : atomic_inc(&oldf->count);
953 : : goto out;
954 : : }
955 : :
956 : 1120190 : newf = dup_fd(oldf, &error);
957 [ + + ]: 1120208 : if (!newf)
958 : : goto out;
959 : :
960 : 1120206 : tsk->files = newf;
961 : 1120206 : error = 0;
962 : : out:
963 : 18 : return error;
964 : : }
965 : :
966 : 0 : static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
967 : : {
968 : : #ifdef CONFIG_BLOCK
969 : 1122983 : struct io_context *ioc = current->io_context;
970 : : struct io_context *new_ioc;
971 : :
972 [ + + ]: 1122983 : if (!ioc)
973 : : return 0;
974 : : /*
975 : : * Share io context with parent, if CLONE_IO is set
976 : : */
977 [ - + ]: 1069811 : if (clone_flags & CLONE_IO) {
978 : : ioc_task_link(ioc);
979 : 0 : tsk->io_context = ioc;
980 [ - + ]: 1069811 : } 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 [ + + ]: 1122987 : if (clone_flags & CLONE_SIGHAND) {
997 : 2258 : atomic_inc(¤t->sighand->count);
998 : 2258 : return 0;
999 : : }
1000 : 1120729 : sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1001 : 1120729 : rcu_assign_pointer(tsk->sighand, sig);
1002 [ + ]: 1120715 : if (!sig)
1003 : : return -ENOMEM;
1004 : 1120725 : atomic_set(&sig->count, 1);
1005 : 2241450 : memcpy(sig->action, current->sighand->action, sizeof(sig->action));
1006 : 1120725 : return 0;
1007 : : }
1008 : :
1009 : 0 : void __cleanup_sighand(struct sighand_struct *sighand)
1010 : : {
1011 [ + + ]: 1122985 : if (atomic_dec_and_test(&sighand->count)) {
1012 : 1120727 : signalfd_cleanup(sighand);
1013 : 1120727 : 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 : 1120727 : cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
1029 [ + + ]: 1120727 : if (cpu_limit != RLIM_INFINITY) {
1030 : 358 : sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
1031 : 358 : sig->cputimer.running = 1;
1032 : : }
1033 : :
1034 : : /* The timer lists. */
1035 : 1120727 : INIT_LIST_HEAD(&sig->cpu_timers[0]);
1036 : 1120727 : INIT_LIST_HEAD(&sig->cpu_timers[1]);
1037 : 1120727 : 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 [ + + ]: 1122970 : if (clone_flags & CLONE_THREAD)
1045 : : return 0;
1046 : :
1047 : 1120710 : sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
1048 : 1120725 : tsk->signal = sig;
1049 [ + + ]: 1120725 : if (!sig)
1050 : : return -ENOMEM;
1051 : :
1052 : 1120719 : sig->nr_threads = 1;
1053 : 1120719 : atomic_set(&sig->live, 1);
1054 : 1120719 : atomic_set(&sig->sigcnt, 1);
1055 : 1120719 : init_waitqueue_head(&sig->wait_chldexit);
1056 : 1120719 : sig->curr_target = tsk;
1057 : : init_sigpending(&sig->shared_pending);
1058 : 1120719 : INIT_LIST_HEAD(&sig->posix_timers);
1059 : :
1060 : 1120719 : hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1061 : 1120714 : sig->real_timer.function = it_real_fn;
1062 : :
1063 : 1120714 : task_lock(current->group_leader);
1064 : 2241444 : memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
1065 : 1120722 : task_unlock(current->group_leader);
1066 : :
1067 : : posix_cpu_timers_init_group(sig);
1068 : :
1069 : 1120727 : tty_audit_fork(sig);
1070 : : sched_autogroup_fork(sig);
1071 : :
1072 : : #ifdef CONFIG_CGROUPS
1073 : 1120723 : init_rwsem(&sig->group_rwsem);
1074 : : #endif
1075 : :
1076 : 1120706 : sig->oom_score_adj = current->signal->oom_score_adj;
1077 : 1120706 : sig->oom_score_adj_min = current->signal->oom_score_adj_min;
1078 : :
1079 : 1120706 : sig->has_child_subreaper = current->signal->has_child_subreaper ||
1080 : : current->signal->is_child_subreaper;
1081 : :
1082 : 1120706 : mutex_init(&sig->cred_guard_mutex);
1083 : :
1084 : 1120720 : return 0;
1085 : : }
1086 : :
1087 : : static void copy_flags(unsigned long clone_flags, struct task_struct *p)
1088 : : {
1089 : 1122965 : unsigned long new_flags = p->flags;
1090 : :
1091 : 1122965 : new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
1092 : 1122965 : new_flags |= PF_FORKNOEXEC;
1093 : 1122965 : p->flags = new_flags;
1094 : : }
1095 : :
1096 : 0 : SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
1097 : : {
1098 : 5776 : current->clear_child_tid = tidptr;
1099 : :
1100 : 5776 : return task_pid_vnr(current);
1101 : : }
1102 : :
1103 : : static void rt_mutex_init_task(struct task_struct *p)
1104 : : {
1105 : 1122980 : raw_spin_lock_init(&p->pi_lock);
1106 : : #ifdef CONFIG_RT_MUTEXES
1107 : : plist_head_init(&p->pi_waiters);
1108 : 1122980 : 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 : 1122971 : tsk->cputime_expires.prof_exp = 0;
1125 : 1122971 : tsk->cputime_expires.virt_exp = 0;
1126 : 1122971 : tsk->cputime_expires.sched_exp = 0;
1127 : 1122971 : INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1128 : 1122971 : INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1129 : 1122971 : 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 : 3364402 : 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 [ + + ]: 1122985 : if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
1157 : : return ERR_PTR(-EINVAL);
1158 : :
1159 [ + + ]: 1122984 : 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 [ + ]: 1122983 : 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 [ + - ]: 1122985 : 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 [ + + ][ + - ]: 1122985 : 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 [ + + ]: 1122985 : if (clone_flags & CLONE_SIGHAND) {
1193 [ + - + ]: 4513 : if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
1194 : 2257 : (task_active_pid_ns(current) !=
1195 : 2256 : current->nsproxy->pid_ns_for_children))
1196 : : return ERR_PTR(-EINVAL);
1197 : : }
1198 : :
1199 : 1122985 : retval = security_task_create(clone_flags);
1200 [ + - ]: 1122982 : if (retval)
1201 : : goto fork_out;
1202 : :
1203 : : retval = -ENOMEM;
1204 : 1122982 : p = dup_task_struct(current);
1205 [ + + ]: 1122984 : if (!p)
1206 : : goto fork_out;
1207 : :
1208 : : ftrace_graph_init_task(p);
1209 : 1122979 : 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 [ + + ]: 1122980 : 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 : 1122980 : current->flags &= ~PF_NPROC_EXCEEDED;
1225 : :
1226 : 1122980 : retval = copy_creds(p, clone_flags);
1227 [ + + ]: 1122981 : 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 [ + ]: 1122973 : if (nr_threads >= max_threads)
1237 : : goto bad_fork_cleanup_count;
1238 : :
1239 [ + + ]: 1122982 : if (!try_module_get(task_thread_info(p)->exec_domain->module))
1240 : : goto bad_fork_cleanup_count;
1241 : :
1242 : 1122965 : p->did_exec = 0;
1243 : : delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
1244 : : copy_flags(clone_flags, p);
1245 : 1122965 : INIT_LIST_HEAD(&p->children);
1246 : 1122965 : INIT_LIST_HEAD(&p->sibling);
1247 : : rcu_copy_process(p);
1248 : 1122965 : p->vfork_done = NULL;
1249 : 1122965 : spin_lock_init(&p->alloc_lock);
1250 : :
1251 : : init_sigpending(&p->pending);
1252 : :
1253 : 1122965 : p->utime = p->stime = p->gtime = 0;
1254 : 1122965 : p->utimescaled = p->stimescaled = 0;
1255 : : #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
1256 : 1122965 : 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 : 1122965 : memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1266 : : #endif
1267 : :
1268 : 1122971 : 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 : 1122971 : do_posix_clock_monotonic_gettime(&p->start_time);
1276 : 1122973 : p->real_start_time = p->start_time;
1277 : 1122973 : monotonic_to_bootbased(&p->real_start_time);
1278 : 1122954 : p->io_context = NULL;
1279 : 1122954 : p->audit_context = NULL;
1280 [ + + ]: 1122954 : if (clone_flags & CLONE_THREAD)
1281 : 2257 : threadgroup_change_begin(current);
1282 : 1122954 : 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 : 1122985 : sched_fork(clone_flags, p);
1332 : :
1333 : 1122980 : retval = perf_event_init_task(p);
1334 [ + ]: 1122967 : if (retval)
1335 : : goto bad_fork_cleanup_policy;
1336 : 1122976 : retval = audit_alloc(p);
1337 [ + ]: 1122968 : if (retval)
1338 : : goto bad_fork_cleanup_policy;
1339 : : /* copy all the process information */
1340 : 1122986 : retval = copy_semundo(clone_flags, p);
1341 [ + + ]: 1122959 : if (retval)
1342 : : goto bad_fork_cleanup_audit;
1343 : 1122948 : retval = copy_files(clone_flags, p);
1344 [ + - ]: 1122987 : if (retval)
1345 : : goto bad_fork_cleanup_semundo;
1346 : 1122987 : retval = copy_fs(clone_flags, p);
1347 [ + ]: 1122985 : if (retval)
1348 : : goto bad_fork_cleanup_files;
1349 : 1122987 : retval = copy_sighand(clone_flags, p);
1350 [ + ]: 1122962 : if (retval)
1351 : : goto bad_fork_cleanup_fs;
1352 : 1122980 : retval = copy_signal(clone_flags, p);
1353 [ + - ]: 1122978 : if (retval)
1354 : : goto bad_fork_cleanup_sighand;
1355 : 1122978 : retval = copy_mm(clone_flags, p);
1356 [ + - ]: 1122987 : if (retval)
1357 : : goto bad_fork_cleanup_signal;
1358 : 1122987 : retval = copy_namespaces(clone_flags, p);
1359 [ + + ]: 1122988 : if (retval)
1360 : : goto bad_fork_cleanup_mm;
1361 : 1122983 : retval = copy_io(clone_flags, p);
1362 [ + - ]: 1122983 : if (retval)
1363 : : goto bad_fork_cleanup_namespaces;
1364 : 1122983 : retval = copy_thread(clone_flags, stack_start, stack_size, p);
1365 [ + - ]: 1122982 : if (retval)
1366 : : goto bad_fork_cleanup_io;
1367 : :
1368 [ + ]: 1122982 : if (pid != &init_struct_pid) {
1369 : : retval = -ENOMEM;
1370 : 1122983 : pid = alloc_pid(p->nsproxy->pid_ns_for_children);
1371 [ + - ]: 1122984 : if (!pid)
1372 : : goto bad_fork_cleanup_io;
1373 : : }
1374 : :
1375 [ + + ]: 1122983 : p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1376 : : /*
1377 : : * Clear TID on mm_release()?
1378 : : */
1379 [ + + ]: 1122983 : p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
1380 : : #ifdef CONFIG_BLOCK
1381 : 1122983 : p->plug = NULL;
1382 : : #endif
1383 : : #ifdef CONFIG_FUTEX
1384 : 1122983 : p->robust_list = NULL;
1385 : : #ifdef CONFIG_COMPAT
1386 : : p->compat_robust_list = NULL;
1387 : : #endif
1388 : 1122983 : INIT_LIST_HEAD(&p->pi_state_list);
1389 : 1122983 : p->pi_state_cache = NULL;
1390 : : #endif
1391 : : /*
1392 : : * sigaltstack should be cleared when sharing the same VM
1393 : : */
1394 [ + + ]: 1122983 : if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1395 : 2802 : 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 : 1122984 : p->pid = pid_nr(pid);
1410 [ + + ]: 1122984 : if (clone_flags & CLONE_THREAD) {
1411 : 2257 : p->exit_signal = -1;
1412 : 2257 : p->group_leader = current->group_leader;
1413 : 2257 : p->tgid = current->tgid;
1414 : : } else {
1415 [ + + ]: 1120727 : if (clone_flags & CLONE_PARENT)
1416 : 1 : p->exit_signal = current->group_leader->exit_signal;
1417 : : else
1418 : 1120726 : p->exit_signal = (clone_flags & CSIGNAL);
1419 : 1120727 : p->group_leader = p;
1420 : 1120727 : p->tgid = p->pid;
1421 : : }
1422 : :
1423 : 1122984 : p->pdeath_signal = 0;
1424 : 1122984 : p->exit_state = 0;
1425 : :
1426 : 1122984 : p->nr_dirtied = 0;
1427 : 1122984 : p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
1428 : 1122984 : p->dirty_paused_when = 0;
1429 : :
1430 : 1122984 : INIT_LIST_HEAD(&p->thread_group);
1431 : 1122984 : 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 : 1122984 : write_lock_irq(&tasklist_lock);
1438 : :
1439 : : /* CLONE_PARENT re-uses the old parent */
1440 [ + + ]: 1122984 : if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
1441 : 2258 : p->real_parent = current->real_parent;
1442 : 2258 : p->parent_exec_id = current->parent_exec_id;
1443 : : } else {
1444 : 1120726 : p->real_parent = current;
1445 : 1120726 : p->parent_exec_id = current->self_exec_id;
1446 : : }
1447 : :
1448 : 1122984 : spin_lock(¤t->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 : 1122984 : recalc_sigpending();
1459 [ + + ]: 1122984 : if (signal_pending(current)) {
1460 : 12 : spin_unlock(¤t->sighand->siglock);
1461 : : write_unlock_irq(&tasklist_lock);
1462 : : retval = -ERESTARTNOINTR;
1463 : : goto bad_fork_free_pid;
1464 : : }
1465 : :
1466 [ + - ]: 1122972 : if (likely(p->pid)) {
1467 [ + - ][ + + ]: 1122972 : ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
1468 : :
1469 : : init_task_pid(p, PIDTYPE_PID, pid);
1470 [ + + ]: 1122972 : if (thread_group_leader(p)) {
1471 : 1120715 : init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
1472 : 1120715 : init_task_pid(p, PIDTYPE_SID, task_session(current));
1473 : :
1474 [ - + ]: 1120715 : if (is_child_reaper(pid)) {
1475 : 0 : ns_of_pid(pid)->child_reaper = p;
1476 : 0 : p->signal->flags |= SIGNAL_UNKILLABLE;
1477 : : }
1478 : :
1479 : 1120715 : p->signal->leader_pid = pid;
1480 : 3362145 : p->signal->tty = tty_kref_get(current->signal->tty);
1481 : 1120715 : list_add_tail(&p->sibling, &p->real_parent->children);
1482 : 1120715 : list_add_tail_rcu(&p->tasks, &init_task.tasks);
1483 : 1120715 : attach_pid(p, PIDTYPE_PGID);
1484 : 1120715 : attach_pid(p, PIDTYPE_SID);
1485 : 1120715 : __this_cpu_inc(process_counts);
1486 : : } else {
1487 : 2257 : current->signal->nr_threads++;
1488 : 2257 : atomic_inc(¤t->signal->live);
1489 : 2257 : atomic_inc(¤t->signal->sigcnt);
1490 : 2257 : list_add_tail_rcu(&p->thread_group,
1491 : 2257 : &p->group_leader->thread_group);
1492 : : }
1493 : 1122972 : attach_pid(p, PIDTYPE_PID);
1494 : 1122972 : nr_threads++;
1495 : : }
1496 : :
1497 : 1122972 : total_forks++;
1498 : 1122972 : spin_unlock(¤t->sighand->siglock);
1499 : : write_unlock_irq(&tasklist_lock);
1500 : 1122972 : proc_fork_connector(p);
1501 : 1122972 : cgroup_post_fork(p);
1502 [ + + ]: 1122972 : if (clone_flags & CLONE_THREAD)
1503 : 2257 : threadgroup_change_end(current);
1504 : 1122972 : perf_event_fork(p);
1505 : :
1506 : : trace_task_newtask(p, clone_flags);
1507 : : uprobe_copy_process(p, clone_flags);
1508 : :
1509 : 1122972 : return p;
1510 : :
1511 : : bad_fork_free_pid:
1512 [ + - ]: 12 : if (pid != &init_struct_pid)
1513 : 12 : free_pid(pid);
1514 : : bad_fork_cleanup_io:
1515 [ - + ]: 12 : if (p->io_context)
1516 : 0 : exit_io_context(p);
1517 : : bad_fork_cleanup_namespaces:
1518 : 12 : exit_task_namespaces(p);
1519 : : bad_fork_cleanup_mm:
1520 [ + - ]: 16 : if (p->mm)
1521 : 16 : mmput(p->mm);
1522 : : bad_fork_cleanup_signal:
1523 [ + - ]: 16 : if (!(clone_flags & CLONE_THREAD))
1524 : 16 : free_signal_struct(p->signal);
1525 : : bad_fork_cleanup_sighand:
1526 : 16 : __cleanup_sighand(p->sighand);
1527 : : bad_fork_cleanup_fs:
1528 : 0 : exit_fs(p); /* blocking */
1529 : : bad_fork_cleanup_files:
1530 : 14 : exit_files(p); /* blocking */
1531 : : bad_fork_cleanup_semundo:
1532 : 16 : exit_sem(p);
1533 : : bad_fork_cleanup_audit:
1534 : : audit_free(p);
1535 : : bad_fork_cleanup_policy:
1536 : 0 : perf_event_free_task(p);
1537 : : #ifdef CONFIG_NUMA
1538 : : mpol_put(p->mempolicy);
1539 : : bad_fork_cleanup_cgroup:
1540 : : #endif
1541 [ - + ]: 16 : if (clone_flags & CLONE_THREAD)
1542 : 0 : threadgroup_change_end(current);
1543 : 16 : cgroup_exit(p, 0);
1544 : : delayacct_tsk_free(p);
1545 : 16 : module_put(task_thread_info(p)->exec_domain->module);
1546 : : bad_fork_cleanup_count:
1547 : 9 : atomic_dec(&p->cred->user->processes);
1548 : 16 : exit_creds(p);
1549 : : bad_fork_free:
1550 : 24 : free_task(p);
1551 : : fork_out:
1552 : 16 : 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 [ + + ]: 1122982 : if (!(clone_flags & CLONE_UNTRACED)) {
1600 [ + + ]: 1122172 : if (clone_flags & CLONE_VFORK)
1601 : : trace = PTRACE_EVENT_VFORK;
1602 [ + + ]: 1117282 : else if ((clone_flags & CSIGNAL) != SIGCHLD)
1603 : : trace = PTRACE_EVENT_CLONE;
1604 : : else
1605 : : trace = PTRACE_EVENT_FORK;
1606 : :
1607 [ + + ]: 1122172 : if (likely(!ptrace_event_enabled(current, trace)))
1608 : : trace = 0;
1609 : : }
1610 : :
1611 : 1122982 : 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 [ + + ]: 1122988 : if (!IS_ERR(p)) {
1618 : : struct completion vfork;
1619 : :
1620 : 1122972 : trace_sched_process_fork(current, p);
1621 : :
1622 : : nr = task_pid_vnr(p);
1623 : :
1624 [ + + ]: 1122972 : if (clone_flags & CLONE_PARENT_SETTID)
1625 : 2927 : put_user(nr, parent_tidptr);
1626 : :
1627 [ + + ]: 1122972 : if (clone_flags & CLONE_VFORK) {
1628 : 5160 : p->vfork_done = &vfork;
1629 : : init_completion(&vfork);
1630 : 5160 : get_task_struct(p);
1631 : : }
1632 : :
1633 : 1122972 : wake_up_new_task(p);
1634 : :
1635 : : /* forking complete and child started to run, tell ptracer */
1636 [ + + ]: 1122971 : if (unlikely(trace))
1637 : 32 : ptrace_event(trace, nr);
1638 : :
1639 [ + + ]: 1122972 : if (clone_flags & CLONE_VFORK) {
1640 [ + - ]: 5160 : if (!wait_for_vfork_done(p, &vfork))
1641 : 5160 : ptrace_event(PTRACE_EVENT_VFORK_DONE, nr);
1642 : : }
1643 : : } else {
1644 : : nr = PTR_ERR(p);
1645 : : }
1646 : 1122988 : 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 : 811 : 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 : 4891 : 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 : 1117283 : 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 : 4842 : spin_lock_init(&sighand->siglock);
1715 : 4842 : init_waitqueue_head(&sighand->signalfd_wqh);
1716 : 4842 : }
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(¤t->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 : 70171 : struct files_struct *fd = current->files;
1799 : 70171 : int error = 0;
1800 : :
1801 [ + + ][ + - ]: 70171 : if ((unshare_flags & CLONE_FILES) &&
1802 [ - + ]: 70167 : (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 : 70166 : struct task_struct *task = current;
1936 : 70166 : struct files_struct *copy = NULL;
1937 : : int error;
1938 : :
1939 : 70166 : error = unshare_fd(CLONE_FILES, ©);
1940 [ + ][ + - ]: 140331 : if (error || !copy) {
1941 : 70165 : *displaced = NULL;
1942 : 70165 : 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 : : }
|