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