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