blob: bba10fbcdce743c2ed025918e0cf6917e8daac5b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/fork.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*
9 * 'fork.c' contains the help-routines for the 'fork' system call
10 * (see also entry.S and others).
11 * Fork is rather simple, once you get the hang of it, but the memory
12 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
13 */
14
Christian Braunerb3e583822019-03-27 13:04:15 +010015#include <linux/anon_inodes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010017#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010018#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010019#include <linux/sched/coredump.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010020#include <linux/sched/user.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010021#include <linux/sched/numa_balancing.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010022#include <linux/sched/stat.h>
Ingo Molnar29930022017-02-08 18:51:36 +010023#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010024#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010025#include <linux/sched/cputime.h>
Christian Braunerb3e583822019-03-27 13:04:15 +010026#include <linux/seq_file.h>
Ingo Molnar037741a2017-02-03 10:08:30 +010027#include <linux/rtmutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/vmalloc.h>
32#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/personality.h>
34#include <linux/mempolicy.h>
35#include <linux/sem.h>
36#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040037#include <linux/fdtable.h>
Jens Axboeda9cbc82008-06-30 20:42:08 +020038#include <linux/iocontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/key.h>
40#include <linux/binfmts.h>
41#include <linux/mman.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070042#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/fs.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070044#include <linux/mm.h>
45#include <linux/vmacache.h>
Serge E. Hallynab516012006-10-02 02:18:06 -070046#include <linux/nsproxy.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080047#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/cpu.h>
Paul Menageb4f48b62007-10-18 23:39:33 -070049#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/security.h>
Mel Gormana1e78772008-07-23 21:27:23 -070051#include <linux/hugetlb.h>
Will Drewrye2cfabdf2012-04-12 16:47:57 -050052#include <linux/seccomp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/swap.h>
54#include <linux/syscalls.h>
55#include <linux/jiffies.h>
56#include <linux/futex.h>
Linus Torvalds8141c7f2008-11-15 10:20:36 -080057#include <linux/compat.h>
Eric Dumazet207205a2011-03-22 16:30:44 -070058#include <linux/kthread.h>
Andrew Morton7c3ab7382006-12-10 02:19:19 -080059#include <linux/task_io_accounting_ops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070060#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ptrace.h>
62#include <linux/mount.h>
63#include <linux/audit.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080064#include <linux/memcontrol.h>
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +010065#include <linux/ftrace.h>
Mike Galbraith5e2bf012012-05-10 13:01:45 -070066#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/profile.h>
68#include <linux/rmap.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070069#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/acct.h>
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080071#include <linux/userfaultfd_k.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070072#include <linux/tsacct_kern.h>
Matt Helsley9f460802005-11-07 00:59:16 -080073#include <linux/cn_proc.h>
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070074#include <linux/freezer.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070075#include <linux/delayacct.h>
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -070076#include <linux/taskstats_kern.h>
Arjan van de Ven0a4254052006-09-26 10:52:38 +020077#include <linux/random.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070078#include <linux/tty.h>
Jens Axboefd0928d2008-01-24 08:52:45 +010079#include <linux/blkdev.h>
Al Viro5ad4e532009-03-29 19:50:06 -040080#include <linux/fs_struct.h>
Eric Sandeen7c9f8862008-04-22 16:38:23 -050081#include <linux/magic.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020082#include <linux/perf_event.h>
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020083#include <linux/posix-timers.h>
Avi Kivity8e7cac72009-11-29 16:34:48 +020084#include <linux/user-return-notifier.h>
Ying Han3d5992d2010-10-26 14:21:23 -070085#include <linux/oom.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080086#include <linux/khugepaged.h>
Oleg Nesterovd80e7312012-02-24 20:07:11 +010087#include <linux/signalfd.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053088#include <linux/uprobes.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070089#include <linux/aio.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070090#include <linux/compiler.h>
Heinrich Schuchardt16db3d32015-04-16 12:47:50 -070091#include <linux/sysctl.h>
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070092#include <linux/kcov.h>
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060093#include <linux/livepatch.h>
Mark Rutland48ac3c12017-07-14 12:23:09 +010094#include <linux/thread_info.h>
Alexander Popovafaef012018-08-17 01:16:58 +030095#include <linux/stackleak.h>
Daniel Axtenseafb1492019-11-30 17:54:57 -080096#include <linux/kasan.h>
Sami Tolvanend08b9f02020-04-27 09:00:07 -070097#include <linux/scs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800100#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <asm/mmu_context.h>
102#include <asm/cacheflush.h>
103#include <asm/tlbflush.h>
104
Steven Rostedtad8d75f2009-04-14 19:39:12 -0400105#include <trace/events/sched.h>
106
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800107#define CREATE_TRACE_POINTS
108#include <trace/events/task.h>
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
Heinrich Schuchardtac1b3982015-04-16 12:47:47 -0700111 * Minimum number of threads to boot the kernel
112 */
113#define MIN_THREADS 20
114
115/*
116 * Maximum number of threads
117 */
118#define MAX_THREADS FUTEX_TID_MASK
119
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * Protected counters by write_lock_irq(&tasklist_lock)
122 */
123unsigned long total_forks; /* Handle normal Linux uptimes. */
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -0700124int nr_threads; /* The idle threads do not count.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Kefeng Wang8856ae42019-05-31 22:30:12 -0700126static int max_threads; /* tunable limit on nr_threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Sai Praneeth Prakhya8495f7e2019-09-25 16:47:27 -0700128#define NAMED_ARRAY_INDEX(x) [x] = __stringify(x)
129
130static const char * const resident_page_types[] = {
131 NAMED_ARRAY_INDEX(MM_FILEPAGES),
132 NAMED_ARRAY_INDEX(MM_ANONPAGES),
133 NAMED_ARRAY_INDEX(MM_SWAPENTS),
134 NAMED_ARRAY_INDEX(MM_SHMEMPAGES),
135};
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137DEFINE_PER_CPU(unsigned long, process_counts) = 0;
138
Christoph Hellwigc59923a2006-07-10 04:45:40 -0700139__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
Paul E. McKenneydb1466b2010-03-03 07:46:56 -0800140
141#ifdef CONFIG_PROVE_RCU
142int lockdep_tasklist_lock_is_held(void)
143{
144 return lockdep_is_held(&tasklist_lock);
145}
146EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
147#endif /* #ifdef CONFIG_PROVE_RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149int nr_processes(void)
150{
151 int cpu;
152 int total = 0;
153
Ian Campbell1d510752009-11-03 10:11:14 +0000154 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 total += per_cpu(process_counts, cpu);
156
157 return total;
158}
159
Akinobu Mitaf19b9f72012-07-30 14:42:33 -0700160void __weak arch_release_task_struct(struct task_struct *tsk)
161{
162}
163
Thomas Gleixnerf5e10282012-05-05 15:05:48 +0000164#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
Christoph Lametere18b8902006-12-06 20:33:20 -0800165static struct kmem_cache *task_struct_cachep;
Thomas Gleixner41101802012-05-05 15:05:41 +0000166
167static inline struct task_struct *alloc_task_struct_node(int node)
168{
169 return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
170}
171
Thomas Gleixner41101802012-05-05 15:05:41 +0000172static inline void free_task_struct(struct task_struct *tsk)
173{
Thomas Gleixner41101802012-05-05 15:05:41 +0000174 kmem_cache_free(task_struct_cachep, tsk);
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#endif
177
Linus Torvaldsb235bee2016-06-24 15:09:37 -0700178#ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
Thomas Gleixner41101802012-05-05 15:05:41 +0000179
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000180/*
181 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
182 * kmemcache based allocator.
183 */
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700184# if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700185
186#ifdef CONFIG_VMAP_STACK
187/*
188 * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB
189 * flush. Try to minimize the number of calls by caching stacks.
190 */
191#define NR_CACHED_STACKS 2
192static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]);
Hoeun Ryu19659c52017-05-08 15:56:11 -0700193
194static int free_vm_stack_cache(unsigned int cpu)
195{
196 struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu);
197 int i;
198
199 for (i = 0; i < NR_CACHED_STACKS; i++) {
200 struct vm_struct *vm_stack = cached_vm_stacks[i];
201
202 if (!vm_stack)
203 continue;
204
205 vfree(vm_stack->addr);
206 cached_vm_stacks[i] = NULL;
207 }
208
209 return 0;
210}
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700211#endif
212
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700213static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
FUJITA Tomonorib69c49b2008-07-25 01:45:40 -0700214{
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700215#ifdef CONFIG_VMAP_STACK
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700216 void *stack;
217 int i;
218
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700219 for (i = 0; i < NR_CACHED_STACKS; i++) {
Christoph Lameter112166f2017-07-12 14:33:11 -0700220 struct vm_struct *s;
221
222 s = this_cpu_xchg(cached_stacks[i], NULL);
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700223
224 if (!s)
225 continue;
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700226
Daniel Axtenseafb1492019-11-30 17:54:57 -0800227 /* Clear the KASAN shadow of the stack. */
228 kasan_unpoison_shadow(s->addr, THREAD_SIZE);
229
Konstantin Khlebnikovca182552017-10-13 15:58:22 -0700230 /* Clear stale pointers from reused stack. */
231 memset(s->addr, 0, THREAD_SIZE);
Kees Cooke01e8062018-04-20 14:55:31 -0700232
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700233 tsk->stack_vm_area = s;
Shakeel Buttba4a4572019-01-08 15:22:57 -0800234 tsk->stack = s->addr;
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700235 return s->addr;
236 }
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700237
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700238 /*
239 * Allocated stacks are cached and later reused by new threads,
240 * so memcg accounting is performed manually on assigning/releasing
241 * stacks to tasks. Drop __GFP_ACCOUNT.
242 */
Mark Rutland48ac3c12017-07-14 12:23:09 +0100243 stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN,
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700244 VMALLOC_START, VMALLOC_END,
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700245 THREADINFO_GFP & ~__GFP_ACCOUNT,
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700246 PAGE_KERNEL,
247 0, node, __builtin_return_address(0));
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700248
249 /*
250 * We can't call find_vm_area() in interrupt context, and
251 * free_thread_stack() can be called in interrupt context,
252 * so cache the vm_struct.
253 */
Rik van Riel5eed6f12018-12-21 14:30:54 -0800254 if (stack) {
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700255 tsk->stack_vm_area = find_vm_area(stack);
Rik van Riel5eed6f12018-12-21 14:30:54 -0800256 tsk->stack = stack;
257 }
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700258 return stack;
259#else
Vladimir Davydov49491482016-07-26 15:24:24 -0700260 struct page *page = alloc_pages_node(node, THREADINFO_GFP,
261 THREAD_SIZE_ORDER);
Eric Dumazetb6a84012011-03-22 16:30:42 -0700262
Andrea Arcangeli1bf45802019-06-28 12:07:14 -0700263 if (likely(page)) {
264 tsk->stack = page_address(page);
265 return tsk->stack;
266 }
267 return NULL;
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700268#endif
FUJITA Tomonorib69c49b2008-07-25 01:45:40 -0700269}
270
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700271static inline void free_thread_stack(struct task_struct *tsk)
FUJITA Tomonorib69c49b2008-07-25 01:45:40 -0700272{
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700273#ifdef CONFIG_VMAP_STACK
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700274 struct vm_struct *vm = task_stack_vm_area(tsk);
275
276 if (vm) {
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700277 int i;
278
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700279 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
280 mod_memcg_page_state(vm->pages[i],
281 MEMCG_KERNEL_STACK_KB,
282 -(int)(PAGE_SIZE / 1024));
283
Roman Gushchinf4b00ea2020-04-01 21:06:46 -0700284 memcg_kmem_uncharge_page(vm->pages[i], 0);
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700285 }
286
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700287 for (i = 0; i < NR_CACHED_STACKS; i++) {
Christoph Lameter112166f2017-07-12 14:33:11 -0700288 if (this_cpu_cmpxchg(cached_stacks[i],
289 NULL, tsk->stack_vm_area) != NULL)
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700290 continue;
291
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700292 return;
293 }
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700294
Andrey Ryabinin0f110a92016-12-12 16:44:14 -0800295 vfree_atomic(tsk->stack);
Andy Lutomirskiac496bf2016-09-15 22:45:49 -0700296 return;
297 }
298#endif
299
300 __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER);
FUJITA Tomonorib69c49b2008-07-25 01:45:40 -0700301}
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000302# else
Linus Torvaldsb235bee2016-06-24 15:09:37 -0700303static struct kmem_cache *thread_stack_cache;
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000304
Michael Ellerman9521d392016-06-25 21:53:30 +1000305static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000306 int node)
307{
Rik van Riel5eed6f12018-12-21 14:30:54 -0800308 unsigned long *stack;
309 stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
310 tsk->stack = stack;
311 return stack;
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000312}
313
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700314static void free_thread_stack(struct task_struct *tsk)
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000315{
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700316 kmem_cache_free(thread_stack_cache, tsk->stack);
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000317}
318
Linus Torvaldsb235bee2016-06-24 15:09:37 -0700319void thread_stack_cache_init(void)
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000320{
David Windsorf9d299462017-06-10 22:50:41 -0400321 thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
322 THREAD_SIZE, THREAD_SIZE, 0, 0,
323 THREAD_SIZE, NULL);
Linus Torvaldsb235bee2016-06-24 15:09:37 -0700324 BUG_ON(thread_stack_cache == NULL);
Thomas Gleixner0d15d742012-05-05 15:05:41 +0000325}
326# endif
FUJITA Tomonorib69c49b2008-07-25 01:45:40 -0700327#endif
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* SLAB cache for signal_struct structures (tsk->signal) */
Christoph Lametere18b8902006-12-06 20:33:20 -0800330static struct kmem_cache *signal_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/* SLAB cache for sighand_struct structures (tsk->sighand) */
Christoph Lametere18b8902006-12-06 20:33:20 -0800333struct kmem_cache *sighand_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/* SLAB cache for files_struct structures (tsk->files) */
Christoph Lametere18b8902006-12-06 20:33:20 -0800336struct kmem_cache *files_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/* SLAB cache for fs_struct structures (tsk->fs) */
Christoph Lametere18b8902006-12-06 20:33:20 -0800339struct kmem_cache *fs_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/* SLAB cache for vm_area_struct structures */
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700342static struct kmem_cache *vm_area_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/* SLAB cache for mm_struct structures (tsk->mm) */
Christoph Lametere18b8902006-12-06 20:33:20 -0800345static struct kmem_cache *mm_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds490fc052018-07-21 15:24:03 -0700347struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700348{
Andrew Mortona6704682018-08-21 21:53:06 -0700349 struct vm_area_struct *vma;
Linus Torvalds490fc052018-07-21 15:24:03 -0700350
Andrew Mortona6704682018-08-21 21:53:06 -0700351 vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Kirill A. Shutemov027232d2018-07-26 16:37:25 -0700352 if (vma)
353 vma_init(vma, mm);
Linus Torvalds490fc052018-07-21 15:24:03 -0700354 return vma;
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700355}
356
357struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
358{
Linus Torvalds95faf692018-07-21 14:48:45 -0700359 struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
360
361 if (new) {
Qian Caicda099b2020-02-19 11:00:54 -0800362 ASSERT_EXCLUSIVE_WRITER(orig->vm_flags);
363 ASSERT_EXCLUSIVE_WRITER(orig->vm_file);
364 /*
365 * orig->shared.rb may be modified concurrently, but the clone
366 * will be reinitialized.
367 */
368 *new = data_race(*orig);
Linus Torvalds95faf692018-07-21 14:48:45 -0700369 INIT_LIST_HEAD(&new->anon_vma_chain);
Li Xinhaie39a4b32020-04-06 20:03:39 -0700370 new->vm_next = new->vm_prev = NULL;
Linus Torvalds95faf692018-07-21 14:48:45 -0700371 }
372 return new;
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700373}
374
375void vm_area_free(struct vm_area_struct *vma)
376{
377 kmem_cache_free(vm_area_cachep, vma);
378}
379
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700380static void account_kernel_stack(struct task_struct *tsk, int account)
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700381{
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700382 void *stack = task_stack_page(tsk);
383 struct vm_struct *vm = task_stack_vm_area(tsk);
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700384
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700385 BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
Andy Lutomirskiefdc9492016-07-28 15:48:17 -0700386
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700387 if (vm) {
388 int i;
389
390 BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
391
392 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
393 mod_zone_page_state(page_zone(vm->pages[i]),
394 NR_KERNEL_STACK_KB,
395 PAGE_SIZE / 1024 * account);
396 }
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700397 } else {
398 /*
399 * All stack pages are in the same zone and belong to the
400 * same memcg.
401 */
402 struct page *first_page = virt_to_page(stack);
403
404 mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
405 THREAD_SIZE / 1024 * account);
406
Roman Gushchin8380ce42020-03-28 19:17:25 -0700407 mod_memcg_obj_state(stack, MEMCG_KERNEL_STACK_KB,
408 account * (THREAD_SIZE / 1024));
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700409 }
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700410}
411
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700412static int memcg_charge_kernel_stack(struct task_struct *tsk)
413{
414#ifdef CONFIG_VMAP_STACK
415 struct vm_struct *vm = task_stack_vm_area(tsk);
416 int ret;
417
418 if (vm) {
419 int i;
420
421 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
422 /*
Roman Gushchinf4b00ea2020-04-01 21:06:46 -0700423 * If memcg_kmem_charge_page() fails, page->mem_cgroup
424 * pointer is NULL, and both memcg_kmem_uncharge_page()
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700425 * and mod_memcg_page_state() in free_thread_stack()
426 * will ignore this page. So it's safe.
427 */
Roman Gushchinf4b00ea2020-04-01 21:06:46 -0700428 ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL,
429 0);
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700430 if (ret)
431 return ret;
432
433 mod_memcg_page_state(vm->pages[i],
434 MEMCG_KERNEL_STACK_KB,
435 PAGE_SIZE / 1024);
436 }
437 }
438#endif
439 return 0;
440}
441
Andy Lutomirski68f24b082016-09-15 22:45:48 -0700442static void release_task_stack(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Andy Lutomirski405c0752016-10-31 08:11:43 -0700444 if (WARN_ON(tsk->state != TASK_DEAD))
445 return; /* Better to leak the stack than to free prematurely */
446
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700447 account_kernel_stack(tsk, -1);
Andy Lutomirskiba14a192016-08-11 02:35:21 -0700448 free_thread_stack(tsk);
Andy Lutomirski68f24b082016-09-15 22:45:48 -0700449 tsk->stack = NULL;
450#ifdef CONFIG_VMAP_STACK
451 tsk->stack_vm_area = NULL;
452#endif
453}
454
455#ifdef CONFIG_THREAD_INFO_IN_TASK
456void put_task_stack(struct task_struct *tsk)
457{
Elena Reshetovaf0b89d32019-01-18 14:27:30 +0200458 if (refcount_dec_and_test(&tsk->stack_refcount))
Andy Lutomirski68f24b082016-09-15 22:45:48 -0700459 release_task_stack(tsk);
460}
461#endif
462
463void free_task(struct task_struct *tsk)
464{
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700465 scs_release(tsk);
466
Andy Lutomirski68f24b082016-09-15 22:45:48 -0700467#ifndef CONFIG_THREAD_INFO_IN_TASK
468 /*
469 * The task is finally done with both the stack and thread_info,
470 * so free both.
471 */
472 release_task_stack(tsk);
473#else
474 /*
475 * If the task had a separate stack allocation, it should be gone
476 * by now.
477 */
Elena Reshetovaf0b89d32019-01-18 14:27:30 +0200478 WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0);
Andy Lutomirski68f24b082016-09-15 22:45:48 -0700479#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700480 rt_mutex_debug_task_free(tsk);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100481 ftrace_graph_exit_task(tsk);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500482 put_seccomp_filter(tsk);
Akinobu Mitaf19b9f72012-07-30 14:42:33 -0700483 arch_release_task_struct(tsk);
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100484 if (tsk->flags & PF_KTHREAD)
485 free_kthread_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 free_task_struct(tsk);
487}
488EXPORT_SYMBOL(free_task);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#ifdef CONFIG_MMU
Emese Revfy0766f782016-06-20 20:42:34 +0200491static __latent_entropy int dup_mmap(struct mm_struct *mm,
492 struct mm_struct *oldmm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700494 struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct rb_node **rb_link, *rb_parent;
496 int retval;
497 unsigned long charge;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800498 LIST_HEAD(uf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100500 uprobe_start_dup_mmap();
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700501 if (mmap_write_lock_killable(oldmm)) {
Michal Hocko7c051262016-05-23 16:25:48 -0700502 retval = -EINTR;
503 goto fail_uprobe_end;
504 }
Ralf Baechleec8c0442006-12-12 17:14:57 +0000505 flush_cache_dup_mm(oldmm);
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200506 uprobe_dup_mmap(oldmm, mm);
Ingo Molnarad339452006-07-03 00:25:15 -0700507 /*
508 * Not linked in yet - no deadlock potential:
509 */
Michel Lespinasseaaa2cc52020-06-08 21:33:33 -0700510 mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING);
Hugh Dickins7ee78232005-10-29 18:16:08 -0700511
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -0700512 /* No ordering required: file already has been exposed. */
513 RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
514
Vladimir Davydov4f7d4612014-08-08 14:22:01 -0700515 mm->total_vm = oldmm->total_vm;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800516 mm->data_vm = oldmm->data_vm;
Vladimir Davydov4f7d4612014-08-08 14:22:01 -0700517 mm->exec_vm = oldmm->exec_vm;
518 mm->stack_vm = oldmm->stack_vm;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 rb_link = &mm->mm_rb.rb_node;
521 rb_parent = NULL;
522 pprev = &mm->mmap;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700523 retval = ksm_fork(mm, oldmm);
524 if (retval)
525 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800526 retval = khugepaged_fork(mm, oldmm);
527 if (retval)
528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700530 prev = NULL;
Hugh Dickinsfd3e42f2005-10-29 18:16:06 -0700531 for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct file *file;
533
534 if (mpnt->vm_flags & VM_DONTCOPY) {
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800535 vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 continue;
537 }
538 charge = 0;
Tetsuo Handa655c79b2018-06-14 15:26:34 -0700539 /*
540 * Don't duplicate many vmas if we've been oom-killed (for
541 * example)
542 */
543 if (fatal_signal_pending(current)) {
544 retval = -EINTR;
545 goto out;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (mpnt->vm_flags & VM_ACCOUNT) {
Huang Shijieb2412b72012-07-30 14:42:30 -0700548 unsigned long len = vma_pages(mpnt);
549
Al Viro191c5422012-02-13 03:58:52 +0000550 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto fail_nomem;
552 charge = len;
553 }
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700554 tmp = vm_area_dup(mpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!tmp)
556 goto fail_nomem;
Oleg Nesterovef0855d2013-09-11 14:20:14 -0700557 retval = vma_dup_policy(mpnt, tmp);
558 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto fail_nomem_policy;
Andrea Arcangelia247c3a2010-09-22 13:05:12 -0700560 tmp->vm_mm = mm;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800561 retval = dup_userfaultfd(tmp, &uf);
562 if (retval)
563 goto fail_nomem_anon_vma_fork;
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700564 if (tmp->vm_flags & VM_WIPEONFORK) {
Li Xinhai93949bb2020-04-06 20:03:33 -0700565 /*
566 * VM_WIPEONFORK gets a clean slate in the child.
567 * Don't prepare anon_vma until fault since we don't
568 * copy page for current vma.
569 */
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700570 tmp->anon_vma = NULL;
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700571 } else if (anon_vma_fork(tmp, mpnt))
Rik van Riel5beb4932010-03-05 13:42:07 -0800572 goto fail_nomem_anon_vma_fork;
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800573 tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 file = tmp->vm_file;
575 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500576 struct inode *inode = file_inode(file);
Hugh Dickinsb88ed202008-12-10 20:48:52 +0000577 struct address_space *mapping = file->f_mapping;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 get_file(file);
580 if (tmp->vm_flags & VM_DENYWRITE)
581 atomic_dec(&inode->i_writecount);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800582 i_mmap_lock_write(mapping);
Hugh Dickinsb88ed202008-12-10 20:48:52 +0000583 if (tmp->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700584 atomic_inc(&mapping->i_mmap_writable);
Hugh Dickinsb88ed202008-12-10 20:48:52 +0000585 flush_dcache_mmap_lock(mapping);
586 /* insert tmp into the share list, just after mpnt */
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800587 vma_interval_tree_insert_after(tmp, mpnt,
588 &mapping->i_mmap);
Hugh Dickinsb88ed202008-12-10 20:48:52 +0000589 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800590 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 /*
Mel Gormana1e78772008-07-23 21:27:23 -0700594 * Clear hugetlb-related page reserves for children. This only
595 * affects MAP_PRIVATE mappings. Faults generated by the child
596 * are not guaranteed to succeed, even if read-only
597 */
598 if (is_vm_hugetlb_page(tmp))
599 reset_vma_resv_huge_pages(tmp);
600
601 /*
Hugh Dickins7ee78232005-10-29 18:16:08 -0700602 * Link in the new vma and copy the page table entries.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *pprev = tmp;
605 pprev = &tmp->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700606 tmp->vm_prev = prev;
607 prev = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 __vma_link_rb(mm, tmp, rb_link, rb_parent);
610 rb_link = &tmp->vm_rb.rb_right;
611 rb_parent = &tmp->vm_rb;
612
613 mm->map_count++;
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700614 if (!(tmp->vm_flags & VM_WIPEONFORK))
615 retval = copy_page_range(mm, oldmm, mpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (tmp->vm_ops && tmp->vm_ops->open)
618 tmp->vm_ops->open(tmp);
619
620 if (retval)
621 goto out;
622 }
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200623 /* a new mm has just been created */
Nadav Amit1ed0cc52018-09-04 15:45:41 -0700624 retval = arch_dup_mmap(oldmm, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700626 mmap_write_unlock(mm);
Hugh Dickinsfd3e42f2005-10-29 18:16:06 -0700627 flush_tlb_mm(oldmm);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700628 mmap_write_unlock(oldmm);
Pavel Emelyanov893e26e2017-02-22 15:42:27 -0800629 dup_userfaultfd_complete(&uf);
Michal Hocko7c051262016-05-23 16:25:48 -0700630fail_uprobe_end:
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100631 uprobe_end_dup_mmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return retval;
Rik van Riel5beb4932010-03-05 13:42:07 -0800633fail_nomem_anon_vma_fork:
Oleg Nesterovef0855d2013-09-11 14:20:14 -0700634 mpol_put(vma_policy(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635fail_nomem_policy:
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700636 vm_area_free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637fail_nomem:
638 retval = -ENOMEM;
639 vm_unacct_memory(charge);
640 goto out;
641}
642
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -0700643static inline int mm_alloc_pgd(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 mm->pgd = pgd_alloc(mm);
646 if (unlikely(!mm->pgd))
647 return -ENOMEM;
648 return 0;
649}
650
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -0700651static inline void mm_free_pgd(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800653 pgd_free(mm, mm->pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655#else
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -0700656static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
657{
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700658 mmap_write_lock(oldmm);
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -0700659 RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700660 mmap_write_unlock(oldmm);
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -0700661 return 0;
662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#define mm_alloc_pgd(mm) (0)
664#define mm_free_pgd(mm)
665#endif /* CONFIG_MMU */
666
Andrew Mortond70f2a12018-01-31 16:15:51 -0800667static void check_mm(struct mm_struct *mm)
668{
669 int i;
670
Sai Praneeth Prakhya8495f7e2019-09-25 16:47:27 -0700671 BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS,
672 "Please make sure 'struct resident_page_types[]' is updated as well");
673
Andrew Mortond70f2a12018-01-31 16:15:51 -0800674 for (i = 0; i < NR_MM_COUNTERS; i++) {
675 long x = atomic_long_read(&mm->rss_stat.count[i]);
676
677 if (unlikely(x))
Sai Praneeth Prakhya8495f7e2019-09-25 16:47:27 -0700678 pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n",
679 mm, resident_page_types[i], x);
Andrew Mortond70f2a12018-01-31 16:15:51 -0800680 }
681
682 if (mm_pgtables_bytes(mm))
683 pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
684 mm_pgtables_bytes(mm));
685
686#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
687 VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
688#endif
689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Christoph Lametere94b1762006-12-06 20:33:17 -0800691#define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
693
Andrew Mortond70f2a12018-01-31 16:15:51 -0800694/*
695 * Called when the last reference to the mm
696 * is dropped: either by a lazy thread or by
697 * mmput. Free the page directory and the mm.
698 */
Andrew Mortond34bc482018-02-21 14:45:17 -0800699void __mmdrop(struct mm_struct *mm)
Andrew Mortond70f2a12018-01-31 16:15:51 -0800700{
701 BUG_ON(mm == &init_mm);
Mark Rutland3eda69c2018-04-05 16:25:12 -0700702 WARN_ON_ONCE(mm == current->mm);
703 WARN_ON_ONCE(mm == current->active_mm);
Andrew Mortond70f2a12018-01-31 16:15:51 -0800704 mm_free_pgd(mm);
705 destroy_context(mm);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -0400706 mmu_notifier_subscriptions_destroy(mm);
Andrew Mortond70f2a12018-01-31 16:15:51 -0800707 check_mm(mm);
708 put_user_ns(mm->user_ns);
709 free_mm(mm);
710}
Andrew Mortond34bc482018-02-21 14:45:17 -0800711EXPORT_SYMBOL_GPL(__mmdrop);
Andrew Mortond70f2a12018-01-31 16:15:51 -0800712
713static void mmdrop_async_fn(struct work_struct *work)
714{
715 struct mm_struct *mm;
716
717 mm = container_of(work, struct mm_struct, async_put_work);
718 __mmdrop(mm);
719}
720
721static void mmdrop_async(struct mm_struct *mm)
722{
723 if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
724 INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
725 schedule_work(&mm->async_put_work);
726 }
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static inline void free_signal_struct(struct signal_struct *sig)
730{
731 taskstats_tgid_free(sig);
732 sched_autogroup_exit(sig);
733 /*
734 * __mmdrop is not safe to call from softirq context on x86 due to
735 * pgd_dtor so postpone it to the async context
736 */
737 if (sig->oom_mm)
738 mmdrop_async(sig->oom_mm);
739 kmem_cache_free(signal_cachep, sig);
740}
741
742static inline void put_signal_struct(struct signal_struct *sig)
743{
Elena Reshetova60d4de32019-01-18 14:27:27 +0200744 if (refcount_dec_and_test(&sig->sigcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 free_signal_struct(sig);
746}
747
748void __put_task_struct(struct task_struct *tsk)
749{
750 WARN_ON(!tsk->exit_state);
Elena Reshetovaec1d2812019-01-18 14:27:29 +0200751 WARN_ON(refcount_read(&tsk->usage));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 WARN_ON(tsk == current);
753
754 cgroup_free(tsk);
Jann Horn16d51a52019-07-16 17:20:45 +0200755 task_numa_free(tsk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 security_task_free(tsk);
757 exit_creds(tsk);
758 delayacct_tsk_free(tsk);
759 put_signal_struct(tsk->signal);
760
761 if (!profile_handoff_task(tsk))
762 free_task(tsk);
763}
764EXPORT_SYMBOL_GPL(__put_task_struct);
765
766void __init __weak arch_task_cache_init(void) { }
767
768/*
769 * set_max_threads
770 */
771static void set_max_threads(unsigned int max_threads_suggested)
772{
773 u64 threads;
Arun KSca79b0c2018-12-28 00:34:29 -0800774 unsigned long nr_pages = totalram_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /*
777 * The number of threads shall be limited such that the thread
778 * structures may only consume a small part of the available memory.
779 */
Arun KS3d6357d2018-12-28 00:34:20 -0800780 if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 threads = MAX_THREADS;
782 else
Arun KS3d6357d2018-12-28 00:34:20 -0800783 threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 (u64) THREAD_SIZE * 8UL);
785
786 if (threads > max_threads_suggested)
787 threads = max_threads_suggested;
788
789 max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
790}
791
792#ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
793/* Initialized by the architecture: */
794int arch_task_struct_size __read_mostly;
795#endif
796
Christoph Hellwig4189ff22019-08-12 08:55:24 +0200797#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
Kees Cook59054292017-08-16 13:00:58 -0700798static void task_struct_whitelist(unsigned long *offset, unsigned long *size)
799{
800 /* Fetch thread_struct whitelist for the architecture. */
801 arch_thread_struct_whitelist(offset, size);
802
803 /*
804 * Handle zero-sized whitelist or empty thread_struct, otherwise
805 * adjust offset to position of thread_struct in task_struct.
806 */
807 if (unlikely(*size == 0))
808 *offset = 0;
809 else
810 *offset += offsetof(struct task_struct, thread);
811}
Christoph Hellwig4189ff22019-08-12 08:55:24 +0200812#endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */
Kees Cook59054292017-08-16 13:00:58 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814void __init fork_init(void)
815{
816 int i;
817#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
818#ifndef ARCH_MIN_TASKALIGN
819#define ARCH_MIN_TASKALIGN 0
820#endif
821 int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN);
Kees Cook59054292017-08-16 13:00:58 -0700822 unsigned long useroffset, usersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* create a slab on which task_structs can be allocated */
Kees Cook59054292017-08-16 13:00:58 -0700825 task_struct_whitelist(&useroffset, &usersize);
826 task_struct_cachep = kmem_cache_create_usercopy("task_struct",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 arch_task_struct_size, align,
Kees Cook59054292017-08-16 13:00:58 -0700828 SLAB_PANIC|SLAB_ACCOUNT,
829 useroffset, usersize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#endif
831
832 /* do the arch specific task caches init */
833 arch_task_cache_init();
834
835 set_max_threads(MAX_THREADS);
836
837 init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
838 init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
839 init_task.signal->rlim[RLIMIT_SIGPENDING] =
840 init_task.signal->rlim[RLIMIT_NPROC];
841
842 for (i = 0; i < UCOUNT_COUNTS; i++) {
843 init_user_ns.ucount_max[i] = max_threads/2;
844 }
845
846#ifdef CONFIG_VMAP_STACK
847 cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
848 NULL, free_vm_stack_cache);
849#endif
850
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700851 scs_init();
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 lockdep_init_task(&init_task);
Nadav Amitaad42dd2019-04-26 16:22:44 -0700854 uprobes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857int __weak arch_dup_task_struct(struct task_struct *dst,
858 struct task_struct *src)
859{
860 *dst = *src;
861 return 0;
862}
863
864void set_task_stack_end_magic(struct task_struct *tsk)
865{
866 unsigned long *stackend;
867
868 stackend = end_of_stack(tsk);
869 *stackend = STACK_END_MAGIC; /* for overflow detection */
870}
871
872static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
873{
874 struct task_struct *tsk;
875 unsigned long *stack;
YueHaibing0f4991e2018-12-28 00:40:00 -0800876 struct vm_struct *stack_vm_area __maybe_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int err;
878
879 if (node == NUMA_NO_NODE)
880 node = tsk_fork_get_node(orig);
881 tsk = alloc_task_struct_node(node);
882 if (!tsk)
883 return NULL;
884
885 stack = alloc_thread_stack_node(tsk, node);
886 if (!stack)
887 goto free_tsk;
888
Roman Gushchin9b6f7e12018-10-26 15:03:19 -0700889 if (memcg_charge_kernel_stack(tsk))
890 goto free_stack;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 stack_vm_area = task_stack_vm_area(tsk);
893
894 err = arch_dup_task_struct(tsk, orig);
895
896 /*
897 * arch_dup_task_struct() clobbers the stack-related fields. Make
898 * sure they're properly initialized before using any stack-related
899 * functions again.
900 */
901 tsk->stack = stack;
902#ifdef CONFIG_VMAP_STACK
903 tsk->stack_vm_area = stack_vm_area;
904#endif
905#ifdef CONFIG_THREAD_INFO_IN_TASK
Elena Reshetovaf0b89d32019-01-18 14:27:30 +0200906 refcount_set(&tsk->stack_refcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#endif
908
909 if (err)
910 goto free_stack;
911
Sami Tolvanend08b9f02020-04-27 09:00:07 -0700912 err = scs_prepare(tsk, node);
913 if (err)
914 goto free_stack;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#ifdef CONFIG_SECCOMP
917 /*
918 * We must handle setting up seccomp filters once we're under
919 * the sighand lock in case orig has changed between now and
920 * then. Until then, filter must be NULL to avoid messing up
921 * the usage counts on the error path calling free_task.
922 */
923 tsk->seccomp.filter = NULL;
924#endif
925
926 setup_thread_stack(tsk, orig);
927 clear_user_return_notifier(tsk);
928 clear_tsk_need_resched(tsk);
929 set_task_stack_end_magic(tsk);
930
Linus Torvalds050e9ba2018-06-14 12:21:18 +0900931#ifdef CONFIG_STACKPROTECTOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 tsk->stack_canary = get_random_canary();
933#endif
Sebastian Andrzej Siewior3bd370622019-04-23 16:26:36 +0200934 if (orig->cpus_ptr == &orig->cpus_mask)
935 tsk->cpus_ptr = &tsk->cpus_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /*
Eric W. Biederman0ff7b2c2019-09-14 07:33:58 -0500938 * One for the user space visible state that goes away when reaped.
939 * One for the scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Eric W. Biederman0ff7b2c2019-09-14 07:33:58 -0500941 refcount_set(&tsk->rcu_users, 2);
942 /* One for the rcu users */
943 refcount_set(&tsk->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#ifdef CONFIG_BLK_DEV_IO_TRACE
945 tsk->btrace_seq = 0;
946#endif
947 tsk->splice_pipe = NULL;
948 tsk->task_frag.page = NULL;
949 tsk->wake_q.next = NULL;
950
951 account_kernel_stack(tsk, 1);
952
953 kcov_task_init(tsk);
954
955#ifdef CONFIG_FAULT_INJECTION
956 tsk->fail_nth = 0;
957#endif
958
Josef Bacik2c323012018-07-31 12:39:04 -0400959#ifdef CONFIG_BLK_CGROUP
960 tsk->throttle_queue = NULL;
961 tsk->use_memdelay = 0;
962#endif
963
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700964#ifdef CONFIG_MEMCG
965 tsk->active_memcg = NULL;
966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return tsk;
968
969free_stack:
970 free_thread_stack(tsk);
971free_tsk:
972 free_task_struct(tsk);
973 return NULL;
974}
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976__cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
977
Hidehiro Kawai4cb0e112009-01-06 14:42:47 -0800978static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
979
980static int __init coredump_filter_setup(char *s)
981{
982 default_dump_filter =
983 (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
984 MMF_DUMP_FILTER_MASK;
985 return 1;
986}
987
988__setup("coredump_filter=", coredump_filter_setup);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#include <linux/init_task.h>
991
Alexey Dobriyan858f0992009-09-23 15:57:32 -0700992static void mm_init_aio(struct mm_struct *mm)
993{
994#ifdef CONFIG_AIO
995 spin_lock_init(&mm->ioctx_lock);
Benjamin LaHaisedb446a02013-07-30 12:54:40 -0400996 mm->ioctx_table = NULL;
Alexey Dobriyan858f0992009-09-23 15:57:32 -0700997#endif
998}
999
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07001000static __always_inline void mm_clear_owner(struct mm_struct *mm,
1001 struct task_struct *p)
1002{
1003#ifdef CONFIG_MEMCG
1004 if (mm->owner == p)
1005 WRITE_ONCE(mm->owner, NULL);
1006#endif
1007}
1008
Vladimir Davydov33144e82014-08-08 14:22:03 -07001009static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
1010{
1011#ifdef CONFIG_MEMCG
1012 mm->owner = p;
1013#endif
1014}
1015
Eric Biggers355627f2017-08-31 16:15:26 -07001016static void mm_init_uprobes_state(struct mm_struct *mm)
1017{
1018#ifdef CONFIG_UPROBES
1019 mm->uprobes_state.xol_area = NULL;
1020#endif
1021}
1022
Eric W. Biedermanbfedb582016-10-13 21:23:16 -05001023static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
1024 struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001026 mm->mmap = NULL;
1027 mm->mm_rb = RB_ROOT;
1028 mm->vmacache_seqnum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 atomic_set(&mm->mm_users, 1);
1030 atomic_set(&mm->mm_count, 1);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001031 mmap_init_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 INIT_LIST_HEAD(&mm->mmlist);
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001033 mm->core_state = NULL;
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08001034 mm_pgtables_bytes_init(mm);
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001035 mm->map_count = 0;
1036 mm->locked_vm = 0;
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08001037 atomic64_set(&mm->pinned_vm, 0);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001038 memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_lock_init(&mm->page_table_lock);
Yang Shi88aa7cc2018-06-07 17:05:28 -07001040 spin_lock_init(&mm->arg_lock);
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001041 mm_init_cpumask(mm);
Alexey Dobriyan858f0992009-09-23 15:57:32 -07001042 mm_init_aio(mm);
Balbir Singhcf475ad22008-04-29 01:00:16 -07001043 mm_init_owner(mm, p);
Eric Biggers2b7e8662017-08-25 15:55:43 -07001044 RCU_INIT_POINTER(mm->exe_file, NULL);
Jason Gunthorpe984cfe42019-12-18 13:40:35 -04001045 mmu_notifier_subscriptions_init(mm);
Nadav Amit16af97d2017-08-10 15:23:56 -07001046 init_tlb_flush_pending(mm);
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001047#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
1048 mm->pmd_huge_pte = NULL;
1049#endif
Eric Biggers355627f2017-08-31 16:15:26 -07001050 mm_init_uprobes_state(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Alex Thorltona0715cc2014-04-07 15:37:10 -07001052 if (current->mm) {
1053 mm->flags = current->mm->flags & MMF_INIT_MASK;
1054 mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
1055 } else {
1056 mm->flags = default_dump_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 mm->def_flags = 0;
Alex Thorltona0715cc2014-04-07 15:37:10 -07001058 }
1059
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001060 if (mm_alloc_pgd(mm))
1061 goto fail_nopgd;
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001062
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001063 if (init_new_context(p, mm))
1064 goto fail_nocontext;
1065
Eric W. Biedermanbfedb582016-10-13 21:23:16 -05001066 mm->user_ns = get_user_ns(user_ns);
Vladimir Davydov41f727f2014-08-08 14:21:56 -07001067 return mm;
1068
1069fail_nocontext:
1070 mm_free_pgd(mm);
1071fail_nopgd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 free_mm(mm);
1073 return NULL;
1074}
1075
1076/*
1077 * Allocate and initialize an mm_struct.
1078 */
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07001079struct mm_struct *mm_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07001081 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 mm = allocate_mm();
KOSAKI Motohirode03c722011-05-24 17:12:15 -07001084 if (!mm)
1085 return NULL;
1086
1087 memset(mm, 0, sizeof(*mm));
Eric W. Biedermanbfedb582016-10-13 21:23:16 -05001088 return mm_init(mm, current, current_user_ns());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Michal Hockoec8d7c12016-05-20 16:57:21 -07001091static inline void __mmput(struct mm_struct *mm)
1092{
1093 VM_BUG_ON(atomic_read(&mm->mm_users));
1094
1095 uprobe_clear_state(mm);
1096 exit_aio(mm);
1097 ksm_exit(mm);
1098 khugepaged_exit(mm); /* must run before exit_mmap */
1099 exit_mmap(mm);
Aaron Lu6fcb52a2016-10-07 17:00:08 -07001100 mm_put_huge_zero_page(mm);
Michal Hockoec8d7c12016-05-20 16:57:21 -07001101 set_mm_exe_file(mm, NULL);
1102 if (!list_empty(&mm->mmlist)) {
1103 spin_lock(&mmlist_lock);
1104 list_del(&mm->mmlist);
1105 spin_unlock(&mmlist_lock);
1106 }
1107 if (mm->binfmt)
1108 module_put(mm->binfmt->module);
1109 mmdrop(mm);
1110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*
1113 * Decrement the use count and release all resources for an mm.
1114 */
1115void mmput(struct mm_struct *mm)
1116{
Andrew Morton0ae26f12006-06-23 02:05:15 -07001117 might_sleep();
1118
Michal Hockoec8d7c12016-05-20 16:57:21 -07001119 if (atomic_dec_and_test(&mm->mm_users))
1120 __mmput(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122EXPORT_SYMBOL_GPL(mmput);
1123
Sherry Yanga1b22892017-10-03 16:15:00 -07001124#ifdef CONFIG_MMU
1125static void mmput_async_fn(struct work_struct *work)
1126{
1127 struct mm_struct *mm = container_of(work, struct mm_struct,
1128 async_put_work);
1129
1130 __mmput(mm);
1131}
1132
1133void mmput_async(struct mm_struct *mm)
1134{
1135 if (atomic_dec_and_test(&mm->mm_users)) {
1136 INIT_WORK(&mm->async_put_work, mmput_async_fn);
1137 schedule_work(&mm->async_put_work);
1138 }
1139}
1140#endif
1141
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001142/**
1143 * set_mm_exe_file - change a reference to the mm's executable file
1144 *
1145 * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
1146 *
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001147 * Main users are mmput() and sys_execve(). Callers prevent concurrent
1148 * invocations: in mmput() nobody alive left, in execve task is single
1149 * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
1150 * mm->exe_file, but does so without using set_mm_exe_file() in order
1151 * to do avoid the need for any locks.
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001152 */
Jiri Slaby38646012011-05-26 16:25:46 -07001153void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1154{
Davidlohr Bueso6e399cd2015-04-16 12:47:59 -07001155 struct file *old_exe_file;
1156
1157 /*
1158 * It is safe to dereference the exe_file without RCU as
1159 * this function is only called if nobody else can access
1160 * this mm -- see comment above for justification.
1161 */
1162 old_exe_file = rcu_dereference_raw(mm->exe_file);
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001163
Jiri Slaby38646012011-05-26 16:25:46 -07001164 if (new_exe_file)
1165 get_file(new_exe_file);
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001166 rcu_assign_pointer(mm->exe_file, new_exe_file);
1167 if (old_exe_file)
1168 fput(old_exe_file);
Jiri Slaby38646012011-05-26 16:25:46 -07001169}
1170
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001171/**
1172 * get_mm_exe_file - acquire a reference to the mm's executable file
1173 *
1174 * Returns %NULL if mm has no associated executable file.
1175 * User must release file via fput().
1176 */
Jiri Slaby38646012011-05-26 16:25:46 -07001177struct file *get_mm_exe_file(struct mm_struct *mm)
1178{
1179 struct file *exe_file;
1180
Konstantin Khlebnikov90f31d02015-04-16 12:47:56 -07001181 rcu_read_lock();
1182 exe_file = rcu_dereference(mm->exe_file);
1183 if (exe_file && !get_file_rcu(exe_file))
1184 exe_file = NULL;
1185 rcu_read_unlock();
Jiri Slaby38646012011-05-26 16:25:46 -07001186 return exe_file;
1187}
Davidlohr Bueso11163342015-04-16 12:49:12 -07001188EXPORT_SYMBOL(get_mm_exe_file);
Jiri Slaby38646012011-05-26 16:25:46 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/**
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001191 * get_task_exe_file - acquire a reference to the task's executable file
1192 *
1193 * Returns %NULL if task's mm (if any) has no associated executable file or
1194 * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
1195 * User must release file via fput().
1196 */
1197struct file *get_task_exe_file(struct task_struct *task)
1198{
1199 struct file *exe_file = NULL;
1200 struct mm_struct *mm;
1201
1202 task_lock(task);
1203 mm = task->mm;
1204 if (mm) {
1205 if (!(task->flags & PF_KTHREAD))
1206 exe_file = get_mm_exe_file(mm);
1207 }
1208 task_unlock(task);
1209 return exe_file;
1210}
1211EXPORT_SYMBOL(get_task_exe_file);
1212
1213/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * get_task_mm - acquire a reference to the task's mm
1215 *
Oleg Nesterov246bb0b2008-07-25 01:47:38 -07001216 * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * this kernel workthread has transiently adopted a user mm with use_mm,
1218 * to do its AIO) is not set and if so returns a reference to it, after
1219 * bumping up the use count. User must release the mm via mmput()
1220 * after use. Typically used by /proc and ptrace.
1221 */
1222struct mm_struct *get_task_mm(struct task_struct *task)
1223{
1224 struct mm_struct *mm;
1225
1226 task_lock(task);
1227 mm = task->mm;
1228 if (mm) {
Oleg Nesterov246bb0b2008-07-25 01:47:38 -07001229 if (task->flags & PF_KTHREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 mm = NULL;
1231 else
Vegard Nossum3fce3712017-02-27 14:30:10 -08001232 mmget(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 task_unlock(task);
1235 return mm;
1236}
1237EXPORT_SYMBOL_GPL(get_task_mm);
1238
Christopher Yeoh8cdb8782012-02-02 11:34:09 +10301239struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
1240{
1241 struct mm_struct *mm;
1242 int err;
1243
Bernd Edlinger3e74fab2020-03-20 21:26:04 +01001244 err = mutex_lock_killable(&task->signal->exec_update_mutex);
Christopher Yeoh8cdb8782012-02-02 11:34:09 +10301245 if (err)
1246 return ERR_PTR(err);
1247
1248 mm = get_task_mm(task);
1249 if (mm && mm != current->mm &&
1250 !ptrace_may_access(task, mode)) {
1251 mmput(mm);
1252 mm = ERR_PTR(-EACCES);
1253 }
Bernd Edlinger3e74fab2020-03-20 21:26:04 +01001254 mutex_unlock(&task->signal->exec_update_mutex);
Christopher Yeoh8cdb8782012-02-02 11:34:09 +10301255
1256 return mm;
1257}
1258
Oleg Nesterov57b59c42012-03-05 14:59:13 -08001259static void complete_vfork_done(struct task_struct *tsk)
Oleg Nesterovc415c3b2012-03-05 14:59:13 -08001260{
Oleg Nesterovd68b46f2012-03-05 14:59:13 -08001261 struct completion *vfork;
Oleg Nesterovc415c3b2012-03-05 14:59:13 -08001262
Oleg Nesterovd68b46f2012-03-05 14:59:13 -08001263 task_lock(tsk);
1264 vfork = tsk->vfork_done;
1265 if (likely(vfork)) {
1266 tsk->vfork_done = NULL;
1267 complete(vfork);
1268 }
1269 task_unlock(tsk);
1270}
1271
1272static int wait_for_vfork_done(struct task_struct *child,
1273 struct completion *vfork)
1274{
1275 int killed;
1276
1277 freezer_do_not_count();
Roman Gushchin76f969e2019-04-19 10:03:04 -07001278 cgroup_enter_frozen();
Oleg Nesterovd68b46f2012-03-05 14:59:13 -08001279 killed = wait_for_completion_killable(vfork);
Roman Gushchin76f969e2019-04-19 10:03:04 -07001280 cgroup_leave_frozen(false);
Oleg Nesterovd68b46f2012-03-05 14:59:13 -08001281 freezer_count();
1282
1283 if (killed) {
1284 task_lock(child);
1285 child->vfork_done = NULL;
1286 task_unlock(child);
1287 }
1288
1289 put_task_struct(child);
1290 return killed;
Oleg Nesterovc415c3b2012-03-05 14:59:13 -08001291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/* Please note the differences between mmput and mm_release.
1294 * mmput is called whenever we stop holding onto a mm_struct,
1295 * error success whatever.
1296 *
1297 * mm_release is called after a mm_struct has been removed
1298 * from the current process.
1299 *
1300 * This difference is important for error handling, when we
1301 * only half set up a mm_struct for a new process and need to restore
1302 * the old one. Because we mmput the new mm_struct before
1303 * restoring the old one. . .
1304 * Eric Biederman 10 January 1998
1305 */
Thomas Gleixner4610ba72019-11-06 22:55:38 +01001306static void mm_release(struct task_struct *tsk, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301308 uprobe_free_utask(tsk);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /* Get rid of any cached register state */
1311 deactivate_mm(tsk, mm);
1312
Roland McGrathfec1d012006-12-06 20:36:34 -08001313 /*
Michal Hocko735f2772016-09-01 16:15:13 -07001314 * Signal userspace if we're not exiting with a core dump
1315 * because we want to leave the value intact for debugging
1316 * purposes.
Roland McGrathfec1d012006-12-06 20:36:34 -08001317 */
Eric Dumazet9c8a8222009-08-06 15:09:28 -07001318 if (tsk->clear_child_tid) {
Michal Hocko735f2772016-09-01 16:15:13 -07001319 if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) &&
Eric Dumazet9c8a8222009-08-06 15:09:28 -07001320 atomic_read(&mm->mm_users) > 1) {
1321 /*
1322 * We don't check the error code - if userspace has
1323 * not set up a proper pointer then tough luck.
1324 */
1325 put_user(0, tsk->clear_child_tid);
Dominik Brodowski2de0db92018-03-11 11:34:26 +01001326 do_futex(tsk->clear_child_tid, FUTEX_WAKE,
1327 1, NULL, NULL, 0, 0);
Eric Dumazet9c8a8222009-08-06 15:09:28 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 tsk->clear_child_tid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Konstantin Khlebnikovf7505d642012-05-31 16:26:21 -07001331
1332 /*
1333 * All done, finally we can wake up parent and return this mm to him.
1334 * Also kthread_stop() uses this completion for synchronization.
1335 */
1336 if (tsk->vfork_done)
1337 complete_vfork_done(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Thomas Gleixner4610ba72019-11-06 22:55:38 +01001340void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm)
1341{
Thomas Gleixner150d7152019-11-06 22:55:39 +01001342 futex_exit_release(tsk);
Thomas Gleixner4610ba72019-11-06 22:55:38 +01001343 mm_release(tsk, mm);
1344}
1345
1346void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm)
1347{
Thomas Gleixner150d7152019-11-06 22:55:39 +01001348 futex_exec_release(tsk);
Thomas Gleixner4610ba72019-11-06 22:55:38 +01001349 mm_release(tsk, mm);
1350}
1351
Nadav Amit13585fa2019-04-25 17:11:25 -07001352/**
1353 * dup_mm() - duplicates an existing mm structure
1354 * @tsk: the task_struct with which the new mm will be associated.
1355 * @oldmm: the mm to duplicate.
1356 *
1357 * Allocates a new mm structure and duplicates the provided @oldmm structure
1358 * content into it.
1359 *
1360 * Return: the duplicated mm or NULL on failure.
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001361 */
Nadav Amit13585fa2019-04-25 17:11:25 -07001362static struct mm_struct *dup_mm(struct task_struct *tsk,
1363 struct mm_struct *oldmm)
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001364{
Nadav Amit13585fa2019-04-25 17:11:25 -07001365 struct mm_struct *mm;
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001366 int err;
1367
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001368 mm = allocate_mm();
1369 if (!mm)
1370 goto fail_nomem;
1371
1372 memcpy(mm, oldmm, sizeof(*mm));
1373
Eric W. Biedermanbfedb582016-10-13 21:23:16 -05001374 if (!mm_init(mm, tsk, mm->user_ns))
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001375 goto fail_nomem;
1376
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001377 err = dup_mmap(mm, oldmm);
1378 if (err)
1379 goto free_pt;
1380
1381 mm->hiwater_rss = get_mm_rss(mm);
1382 mm->hiwater_vm = mm->total_vm;
1383
Hiroshi Shimamoto801460d2009-09-23 15:57:41 -07001384 if (mm->binfmt && !try_module_get(mm->binfmt->module))
1385 goto free_pt;
1386
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001387 return mm;
1388
1389free_pt:
Hiroshi Shimamoto801460d2009-09-23 15:57:41 -07001390 /* don't put binfmt in mmput, we haven't got module yet */
1391 mm->binfmt = NULL;
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07001392 mm_init_owner(mm, NULL);
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001393 mmput(mm);
1394
1395fail_nomem:
1396 return NULL;
JANAK DESAIa0a7ec32006-02-07 12:59:01 -08001397}
1398
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07001399static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07001401 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int retval;
1403
1404 tsk->min_flt = tsk->maj_flt = 0;
1405 tsk->nvcsw = tsk->nivcsw = 0;
Mandeep Singh Baines17406b82009-02-06 15:37:47 -08001406#ifdef CONFIG_DETECT_HUNG_TASK
1407 tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
Dmitry Vyukova2e51442018-08-21 21:55:52 -07001408 tsk->last_switch_time = 0;
Mandeep Singh Baines17406b82009-02-06 15:37:47 -08001409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 tsk->mm = NULL;
1412 tsk->active_mm = NULL;
1413
1414 /*
1415 * Are we cloning a kernel thread?
1416 *
1417 * We need to steal a active VM for that..
1418 */
1419 oldmm = current->mm;
1420 if (!oldmm)
1421 return 0;
1422
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001423 /* initialize the new vmacache entries */
1424 vmacache_flush(tsk);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (clone_flags & CLONE_VM) {
Vegard Nossum3fce3712017-02-27 14:30:10 -08001427 mmget(oldmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 mm = oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto good_mm;
1430 }
1431
1432 retval = -ENOMEM;
Nadav Amit13585fa2019-04-25 17:11:25 -07001433 mm = dup_mm(tsk, current->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!mm)
1435 goto fail_nomem;
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437good_mm:
1438 tsk->mm = mm;
1439 tsk->active_mm = mm;
1440 return 0;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442fail_nomem:
1443 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Alexey Dobriyana39bc512007-10-18 23:41:10 -07001446static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Al Viro498052b2009-03-30 07:20:30 -04001448 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (clone_flags & CLONE_FS) {
Al Viro498052b2009-03-30 07:20:30 -04001450 /* tsk->fs is already what we want */
Nick Piggin2a4419b2010-08-18 04:37:33 +10001451 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -04001452 if (fs->in_exec) {
Nick Piggin2a4419b2010-08-18 04:37:33 +10001453 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -04001454 return -EAGAIN;
1455 }
1456 fs->users++;
Nick Piggin2a4419b2010-08-18 04:37:33 +10001457 spin_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return 0;
1459 }
Al Viro498052b2009-03-30 07:20:30 -04001460 tsk->fs = copy_fs_struct(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!tsk->fs)
1462 return -ENOMEM;
1463 return 0;
1464}
1465
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07001466static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
JANAK DESAIa016f332006-02-07 12:59:02 -08001467{
1468 struct files_struct *oldf, *newf;
1469 int error = 0;
1470
1471 /*
1472 * A background process may not have any files ...
1473 */
1474 oldf = current->files;
1475 if (!oldf)
1476 goto out;
1477
1478 if (clone_flags & CLONE_FILES) {
1479 atomic_inc(&oldf->count);
1480 goto out;
1481 }
1482
JANAK DESAIa016f332006-02-07 12:59:02 -08001483 newf = dup_fd(oldf, &error);
1484 if (!newf)
1485 goto out;
1486
1487 tsk->files = newf;
1488 error = 0;
1489out:
1490 return error;
1491}
1492
Jens Axboefadad8782008-01-24 08:54:47 +01001493static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
Jens Axboefd0928d2008-01-24 08:52:45 +01001494{
1495#ifdef CONFIG_BLOCK
1496 struct io_context *ioc = current->io_context;
Tejun Heo6e736be2011-12-14 00:33:38 +01001497 struct io_context *new_ioc;
Jens Axboefd0928d2008-01-24 08:52:45 +01001498
1499 if (!ioc)
1500 return 0;
Jens Axboefadad8782008-01-24 08:54:47 +01001501 /*
1502 * Share io context with parent, if CLONE_IO is set
1503 */
1504 if (clone_flags & CLONE_IO) {
Tejun Heo3d487492012-03-05 13:15:25 -08001505 ioc_task_link(ioc);
1506 tsk->io_context = ioc;
Jens Axboefadad8782008-01-24 08:54:47 +01001507 } else if (ioprio_valid(ioc->ioprio)) {
Tejun Heo6e736be2011-12-14 00:33:38 +01001508 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
1509 if (unlikely(!new_ioc))
Jens Axboefd0928d2008-01-24 08:52:45 +01001510 return -ENOMEM;
1511
Tejun Heo6e736be2011-12-14 00:33:38 +01001512 new_ioc->ioprio = ioc->ioprio;
Tejun Heo11a31222012-02-07 07:51:30 +01001513 put_io_context(new_ioc);
Jens Axboefd0928d2008-01-24 08:52:45 +01001514 }
1515#endif
1516 return 0;
1517}
1518
Alexey Dobriyana39bc512007-10-18 23:41:10 -07001519static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct sighand_struct *sig;
1522
Zhaolei60348802009-01-06 14:40:46 -08001523 if (clone_flags & CLONE_SIGHAND) {
Elena Reshetovad036bda2019-01-18 14:27:26 +02001524 refcount_inc(&current->sighand->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
1526 }
1527 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
Madhuparna Bhowmik0c282b02020-01-27 23:28:21 +05301528 RCU_INIT_POINTER(tsk->sighand, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (!sig)
1530 return -ENOMEM;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +02001531
Elena Reshetovad036bda2019-01-18 14:27:26 +02001532 refcount_set(&sig->count, 1);
Jann Horn06e62a42018-08-21 22:00:58 -07001533 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
Jann Horn06e62a42018-08-21 22:00:58 -07001535 spin_unlock_irq(&current->sighand->siglock);
Christian Braunerb612e5d2019-10-14 12:45:37 +02001536
1537 /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */
1538 if (clone_flags & CLONE_CLEAR_SIGHAND)
1539 flush_signal_handlers(tsk, 0);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542}
1543
Oleg Nesterova7e53282006-03-28 16:11:27 -08001544void __cleanup_sighand(struct sighand_struct *sighand)
Oleg Nesterovc81addc2006-03-28 16:11:17 -08001545{
Elena Reshetovad036bda2019-01-18 14:27:26 +02001546 if (refcount_dec_and_test(&sighand->count)) {
Oleg Nesterovd80e7312012-02-24 20:07:11 +01001547 signalfd_cleanup(sighand);
Oleg Nesterov392809b2014-09-28 23:44:18 +02001548 /*
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08001549 * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it
Oleg Nesterov392809b2014-09-28 23:44:18 +02001550 * without an RCU grace period, see __lock_task_sighand().
1551 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -08001552 kmem_cache_free(sighand_cachep, sighand);
Oleg Nesterovd80e7312012-02-24 20:07:11 +01001553 }
Oleg Nesterovc81addc2006-03-28 16:11:17 -08001554}
1555
Frank Mayharf06febc2008-09-12 09:54:39 -07001556/*
1557 * Initialize POSIX timer handling for a thread group.
1558 */
1559static void posix_cpu_timers_init_group(struct signal_struct *sig)
1560{
Thomas Gleixner2b699422019-08-21 21:09:04 +02001561 struct posix_cputimers *pct = &sig->posix_cputimers;
Jiri Slaby78d7d402010-03-05 13:42:54 -08001562 unsigned long cpu_limit;
1563
Jason Low316c1608d2015-04-28 13:00:20 -07001564 cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Thomas Gleixner3a245c02019-08-21 21:09:06 +02001565 posix_cputimers_group_init(pct, cpu_limit);
Frank Mayharf06febc2008-09-12 09:54:39 -07001566}
1567
Alexey Dobriyana39bc512007-10-18 23:41:10 -07001568static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct signal_struct *sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Oleg Nesterov4ab6c082009-08-26 14:29:24 -07001572 if (clone_flags & CLONE_THREAD)
Peter Zijlstra490dea42008-11-24 17:06:57 +01001573 return 0;
Oleg Nesterov6279a7512009-03-27 01:06:07 +01001574
Veaceslav Falicoa56704e2010-03-10 15:23:01 -08001575 sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 tsk->signal = sig;
1577 if (!sig)
1578 return -ENOMEM;
1579
Oleg Nesterovb3ac0222010-05-26 14:43:24 -07001580 sig->nr_threads = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 atomic_set(&sig->live, 1);
Elena Reshetova60d4de32019-01-18 14:27:27 +02001582 refcount_set(&sig->sigcnt, 1);
Oleg Nesterov0c740d02014-01-21 15:49:56 -08001583
1584 /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
1585 sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
1586 tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 init_waitqueue_head(&sig->wait_chldexit);
Oleg Nesterovdb51aec2008-04-30 00:52:52 -07001589 sig->curr_target = tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 init_sigpending(&sig->shared_pending);
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05001591 INIT_HLIST_HEAD(&sig->multiprocess);
Rik van Riele78c3492014-08-16 13:40:10 -04001592 seqlock_init(&sig->stats_lock);
Peter Zijlstra9d7fb042015-06-30 11:30:54 +02001593 prev_cputime_init(&sig->prev_cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001595#ifdef CONFIG_POSIX_TIMERS
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05001596 INIT_LIST_HEAD(&sig->posix_timers);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001597 hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 sig->real_timer.function = it_real_fn;
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05001599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 task_lock(current->group_leader);
1602 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
1603 task_unlock(current->group_leader);
1604
Oleg Nesterov6279a7512009-03-27 01:06:07 +01001605 posix_cpu_timers_init_group(sig);
1606
Miloslav Trmac522ed772007-07-15 23:40:56 -07001607 tty_audit_fork(sig);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001608 sched_autogroup_fork(sig);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001609
David Rientjesa63d83f2010-08-09 17:19:46 -07001610 sig->oom_score_adj = current->signal->oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001611 sig->oom_score_adj_min = current->signal->oom_score_adj_min;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001612
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07001613 mutex_init(&sig->cred_guard_mutex);
Eric W. Biedermaneea96732020-03-25 10:03:36 -05001614 mutex_init(&sig->exec_update_mutex);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617}
1618
Kees Cookdbd952122014-06-27 15:18:48 -07001619static void copy_seccomp(struct task_struct *p)
1620{
1621#ifdef CONFIG_SECCOMP
1622 /*
1623 * Must be called with sighand->lock held, which is common to
1624 * all threads in the group. Holding cred_guard_mutex is not
1625 * needed because this new task is not yet running and cannot
1626 * be racing exec.
1627 */
Guenter Roeck69f6a342014-08-10 20:50:30 -07001628 assert_spin_locked(&current->sighand->siglock);
Kees Cookdbd952122014-06-27 15:18:48 -07001629
1630 /* Ref-count the new filter user, and assign it. */
1631 get_seccomp_filter(current);
1632 p->seccomp = current->seccomp;
1633
1634 /*
1635 * Explicitly enable no_new_privs here in case it got set
1636 * between the task_struct being duplicated and holding the
1637 * sighand lock. The seccomp state and nnp must be in sync.
1638 */
1639 if (task_no_new_privs(current))
1640 task_set_no_new_privs(p);
1641
1642 /*
1643 * If the parent gained a seccomp mode after copying thread
1644 * flags and between before we held the sighand lock, we have
1645 * to manually enable the seccomp thread flag here.
1646 */
1647 if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
1648 set_tsk_thread_flag(p, TIF_SECCOMP);
1649#endif
1650}
1651
Heiko Carstens17da2bd2009-01-14 14:14:10 +01001652SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 current->clear_child_tid = tidptr;
1655
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001656 return task_pid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Alexey Dobriyana39bc512007-10-18 23:41:10 -07001659static void rt_mutex_init_task(struct task_struct *p)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001660{
Thomas Gleixner1d615482009-11-17 14:54:03 +01001661 raw_spin_lock_init(&p->pi_lock);
Zilvinas Valinskase29e1752007-03-16 13:38:34 -08001662#ifdef CONFIG_RT_MUTEXES
Davidlohr Buesoa23ba902017-09-08 16:15:01 -07001663 p->pi_waiters = RB_ROOT_CACHED;
Xunlei Pange96a77052017-03-23 15:56:08 +01001664 p->pi_top_task = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001665 p->pi_blocked_on = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001666#endif
1667}
1668
Eric W. Biederman2c470472017-09-26 13:06:43 -05001669static inline void init_task_pid_links(struct task_struct *task)
1670{
1671 enum pid_type type;
1672
1673 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1674 INIT_HLIST_NODE(&task->pid_links[type]);
1675 }
1676}
1677
Oleg Nesterov81907732013-07-03 15:08:31 -07001678static inline void
1679init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
1680{
Eric W. Biederman2c470472017-09-26 13:06:43 -05001681 if (type == PIDTYPE_PID)
1682 task->thread_pid = pid;
1683 else
1684 task->signal->pids[type] = pid;
Oleg Nesterov81907732013-07-03 15:08:31 -07001685}
1686
Ingo Molnar6bfbaa52017-02-03 21:37:49 +01001687static inline void rcu_copy_process(struct task_struct *p)
1688{
1689#ifdef CONFIG_PREEMPT_RCU
1690 p->rcu_read_lock_nesting = 0;
1691 p->rcu_read_unlock_special.s = 0;
1692 p->rcu_blocked_node = NULL;
1693 INIT_LIST_HEAD(&p->rcu_node_entry);
1694#endif /* #ifdef CONFIG_PREEMPT_RCU */
1695#ifdef CONFIG_TASKS_RCU
1696 p->rcu_tasks_holdout = false;
1697 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
1698 p->rcu_tasks_idle_cpu = -1;
1699#endif /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001700#ifdef CONFIG_TASKS_TRACE_RCU
1701 p->trc_reader_nesting = 0;
Paul E. McKenney276c4102020-03-17 16:02:06 -07001702 p->trc_reader_special.s = 0;
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001703 INIT_LIST_HEAD(&p->trc_holdout_list);
1704#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
Ingo Molnar6bfbaa52017-02-03 21:37:49 +01001705}
1706
Christian Brauner3695eae2019-07-28 00:22:29 +02001707struct pid *pidfd_pid(const struct file *file)
1708{
1709 if (file->f_op == &pidfd_fops)
1710 return file->private_data;
1711
1712 return ERR_PTR(-EBADF);
1713}
1714
Christian Braunerb3e583822019-03-27 13:04:15 +01001715static int pidfd_release(struct inode *inode, struct file *file)
1716{
1717 struct pid *pid = file->private_data;
1718
1719 file->private_data = NULL;
1720 put_pid(pid);
1721 return 0;
1722}
1723
1724#ifdef CONFIG_PROC_FS
Christian Kellner15d42eb2019-10-14 18:20:32 +02001725/**
1726 * pidfd_show_fdinfo - print information about a pidfd
1727 * @m: proc fdinfo file
1728 * @f: file referencing a pidfd
1729 *
1730 * Pid:
1731 * This function will print the pid that a given pidfd refers to in the
1732 * pid namespace of the procfs instance.
1733 * If the pid namespace of the process is not a descendant of the pid
1734 * namespace of the procfs instance 0 will be shown as its pid. This is
1735 * similar to calling getppid() on a process whose parent is outside of
1736 * its pid namespace.
1737 *
1738 * NSpid:
1739 * If pid namespaces are supported then this function will also print
1740 * the pid of a given pidfd refers to for all descendant pid namespaces
1741 * starting from the current pid namespace of the instance, i.e. the
1742 * Pid field and the first entry in the NSpid field will be identical.
1743 * If the pid namespace of the process is not a descendant of the pid
1744 * namespace of the procfs instance 0 will be shown as its first NSpid
1745 * entry and no others will be shown.
1746 * Note that this differs from the Pid and NSpid fields in
1747 * /proc/<pid>/status where Pid and NSpid are always shown relative to
1748 * the pid namespace of the procfs instance. The difference becomes
1749 * obvious when sending around a pidfd between pid namespaces from a
1750 * different branch of the tree, i.e. where no ancestoral relation is
1751 * present between the pid namespaces:
1752 * - create two new pid namespaces ns1 and ns2 in the initial pid
1753 * namespace (also take care to create new mount namespaces in the
1754 * new pid namespace and mount procfs)
1755 * - create a process with a pidfd in ns1
1756 * - send pidfd from ns1 to ns2
1757 * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid
1758 * have exactly one entry, which is 0
1759 */
Christian Braunerb3e583822019-03-27 13:04:15 +01001760static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
1761{
Christian Braunerb3e583822019-03-27 13:04:15 +01001762 struct pid *pid = f->private_data;
Christian Brauner3d6d8da2019-10-17 12:18:28 +02001763 struct pid_namespace *ns;
1764 pid_t nr = -1;
Christian Braunerb3e583822019-03-27 13:04:15 +01001765
Christian Brauner3d6d8da2019-10-17 12:18:28 +02001766 if (likely(pid_has_task(pid, PIDTYPE_PID))) {
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02001767 ns = proc_pid_ns(file_inode(m->file)->i_sb);
Christian Brauner3d6d8da2019-10-17 12:18:28 +02001768 nr = pid_nr_ns(pid, ns);
1769 }
1770
1771 seq_put_decimal_ll(m, "Pid:\t", nr);
Christian Kellner15d42eb2019-10-14 18:20:32 +02001772
1773#ifdef CONFIG_PID_NS
Christian Brauner3d6d8da2019-10-17 12:18:28 +02001774 seq_put_decimal_ll(m, "\nNSpid:\t", nr);
1775 if (nr > 0) {
Christian Kellner15d42eb2019-10-14 18:20:32 +02001776 int i;
1777
1778 /* If nr is non-zero it means that 'pid' is valid and that
1779 * ns, i.e. the pid namespace associated with the procfs
1780 * instance, is in the pid namespace hierarchy of pid.
1781 * Start at one below the already printed level.
1782 */
1783 for (i = ns->level + 1; i <= pid->level; i++)
Christian Brauner3d6d8da2019-10-17 12:18:28 +02001784 seq_put_decimal_ll(m, "\t", pid->numbers[i].nr);
Christian Kellner15d42eb2019-10-14 18:20:32 +02001785 }
1786#endif
Christian Braunerb3e583822019-03-27 13:04:15 +01001787 seq_putc(m, '\n');
1788}
1789#endif
1790
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04001791/*
1792 * Poll support for process exit notification.
1793 */
Luc Van Oostenryck9e777162019-11-20 01:33:20 +01001794static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04001795{
1796 struct task_struct *task;
1797 struct pid *pid = file->private_data;
Luc Van Oostenryck9e777162019-11-20 01:33:20 +01001798 __poll_t poll_flags = 0;
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04001799
1800 poll_wait(file, &pid->wait_pidfd, pts);
1801
1802 rcu_read_lock();
1803 task = pid_task(pid, PIDTYPE_PID);
1804 /*
1805 * Inform pollers only when the whole thread group exits.
1806 * If the thread group leader exits before all other threads in the
1807 * group, then poll(2) should block, similar to the wait(2) family.
1808 */
1809 if (!task || (task->exit_state && thread_group_empty(task)))
Luc Van Oostenryck9e777162019-11-20 01:33:20 +01001810 poll_flags = EPOLLIN | EPOLLRDNORM;
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04001811 rcu_read_unlock();
1812
1813 return poll_flags;
1814}
1815
Christian Braunerb3e583822019-03-27 13:04:15 +01001816const struct file_operations pidfd_fops = {
1817 .release = pidfd_release,
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04001818 .poll = pidfd_poll,
Christian Braunerb3e583822019-03-27 13:04:15 +01001819#ifdef CONFIG_PROC_FS
1820 .show_fdinfo = pidfd_show_fdinfo,
1821#endif
1822};
1823
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07001824static void __delayed_free_task(struct rcu_head *rhp)
1825{
1826 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
1827
1828 free_task(tsk);
1829}
1830
1831static __always_inline void delayed_free_task(struct task_struct *tsk)
1832{
1833 if (IS_ENABLED(CONFIG_MEMCG))
1834 call_rcu(&tsk->rcu, __delayed_free_task);
1835 else
1836 free_task(tsk);
1837}
1838
Frank Mayharf06febc2008-09-12 09:54:39 -07001839/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 * This creates a new process as a copy of the old one,
1841 * but does not actually start it yet.
1842 *
1843 * It copies the registers, and all the appropriate
1844 * parts of the process environment (as per the clone
1845 * flags). The actual kick-off is left to the caller.
1846 */
Emese Revfy0766f782016-06-20 20:42:34 +02001847static __latent_entropy struct task_struct *copy_process(
Roland McGrath09a05392008-07-25 19:45:47 -07001848 struct pid *pid,
Josh Triplett3033f14a2015-06-25 15:01:19 -07001849 int trace,
Christian Brauner7f192e32019-05-25 11:36:41 +02001850 int node,
1851 struct kernel_clone_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Christian Braunerb3e583822019-03-27 13:04:15 +01001853 int pidfd = -1, retval;
Mariusz Kozlowskia24efe62007-10-18 23:41:09 -07001854 struct task_struct *p;
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05001855 struct multiprocess_signals delayed;
Al Viro6fd2fe42019-06-26 22:22:09 -04001856 struct file *pidfile = NULL;
Christian Brauner7f192e32019-05-25 11:36:41 +02001857 u64 clone_flags = args->flags;
Andrei Vagin769071a2019-11-12 01:26:52 +00001858 struct nsproxy *nsp = current->nsproxy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Marcos Paulo de Souza667b6092018-02-06 15:39:34 -08001860 /*
1861 * Don't allow sharing the root directory with processes in a different
1862 * namespace
1863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
1865 return ERR_PTR(-EINVAL);
1866
Eric W. Biedermane66eded2013-03-13 11:51:49 -07001867 if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
1868 return ERR_PTR(-EINVAL);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Thread groups must share signals as well, and detached threads
1872 * can only be started up within the thread group.
1873 */
1874 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
1875 return ERR_PTR(-EINVAL);
1876
1877 /*
1878 * Shared signal handlers imply shared VM. By way of the above,
1879 * thread groups also imply shared VM. Blocking this case allows
1880 * for various simplifications in other code.
1881 */
1882 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
1883 return ERR_PTR(-EINVAL);
1884
Sukadev Bhattiprolu123be072009-09-23 15:57:20 -07001885 /*
1886 * Siblings of global init remain as zombies on exit since they are
1887 * not reaped by their parent (swapper). To solve this and to avoid
1888 * multi-rooted process trees, prevent global and container-inits
1889 * from creating siblings.
1890 */
1891 if ((clone_flags & CLONE_PARENT) &&
1892 current->signal->flags & SIGNAL_UNKILLABLE)
1893 return ERR_PTR(-EINVAL);
1894
Eric W. Biederman8382fca2012-12-20 19:26:06 -08001895 /*
Oleg Nesterov40a0d322013-09-11 14:19:41 -07001896 * If the new process will be in a different pid or user namespace
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05001897 * do not allow it to share a thread group with the forking task.
Eric W. Biederman8382fca2012-12-20 19:26:06 -08001898 */
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05001899 if (clone_flags & CLONE_THREAD) {
Oleg Nesterov40a0d322013-09-11 14:19:41 -07001900 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
Andrei Vagin769071a2019-11-12 01:26:52 +00001901 (task_active_pid_ns(current) != nsp->pid_ns_for_children))
1902 return ERR_PTR(-EINVAL);
1903 }
1904
1905 /*
1906 * If the new process will be in a different time namespace
1907 * do not allow it to share VM or a thread group with the forking task.
1908 */
1909 if (clone_flags & (CLONE_THREAD | CLONE_VM)) {
1910 if (nsp->time_ns != nsp->time_ns_for_children)
Oleg Nesterov40a0d322013-09-11 14:19:41 -07001911 return ERR_PTR(-EINVAL);
1912 }
Eric W. Biederman8382fca2012-12-20 19:26:06 -08001913
Christian Braunerb3e583822019-03-27 13:04:15 +01001914 if (clone_flags & CLONE_PIDFD) {
Christian Braunerb3e583822019-03-27 13:04:15 +01001915 /*
Christian Braunerb3e583822019-03-27 13:04:15 +01001916 * - CLONE_DETACHED is blocked so that we can potentially
1917 * reuse it later for CLONE_PIDFD.
1918 * - CLONE_THREAD is blocked until someone really needs it.
1919 */
Christian Brauner7f192e32019-05-25 11:36:41 +02001920 if (clone_flags & (CLONE_DETACHED | CLONE_THREAD))
Christian Braunerb3e583822019-03-27 13:04:15 +01001921 return ERR_PTR(-EINVAL);
Christian Braunerb3e583822019-03-27 13:04:15 +01001922 }
1923
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05001924 /*
1925 * Force any signals received before this point to be delivered
1926 * before the fork happens. Collect up signals sent to multiple
1927 * processes that happen during the fork and delay them so that
1928 * they appear to happen after the fork.
1929 */
1930 sigemptyset(&delayed.signal);
1931 INIT_HLIST_NODE(&delayed.node);
1932
1933 spin_lock_irq(&current->sighand->siglock);
1934 if (!(clone_flags & CLONE_THREAD))
1935 hlist_add_head(&delayed.node, &current->signal->multiprocess);
1936 recalc_sigpending();
1937 spin_unlock_irq(&current->sighand->siglock);
1938 retval = -ERESTARTNOINTR;
1939 if (signal_pending(current))
1940 goto fork_out;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 retval = -ENOMEM;
Andi Kleen725fc622016-05-23 16:24:05 -07001943 p = dup_task_struct(current, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!p)
1945 goto fork_out;
1946
Vegard Nossum4d6501dc2017-05-09 09:39:59 +02001947 /*
1948 * This _must_ happen before we call free_task(), i.e. before we jump
1949 * to any of the bad_fork_* labels. This is to avoid freeing
1950 * p->set_child_tid which is (ab)used as a kthread's data pointer for
1951 * kernel threads (PF_KTHREAD).
1952 */
Christian Brauner7f192e32019-05-25 11:36:41 +02001953 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL;
Vegard Nossum4d6501dc2017-05-09 09:39:59 +02001954 /*
1955 * Clear TID on mm_release()?
1956 */
Christian Brauner7f192e32019-05-25 11:36:41 +02001957 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL;
Vegard Nossum4d6501dc2017-05-09 09:39:59 +02001958
Steven Rostedtf7e8b612009-06-02 16:39:48 -04001959 ftrace_graph_init_task(p);
1960
Peter Zijlstrabea493a2006-10-17 00:10:33 -07001961 rt_mutex_init_task(p);
1962
Ingo Molnard12c1a32008-07-14 12:09:28 +02001963#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001964 DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1965 DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 retval = -EAGAIN;
David Howells3b11a1d2008-11-14 10:39:26 +11001968 if (atomic_read(&p->real_cred->user->processes) >=
Jiri Slaby78d7d402010-03-05 13:42:54 -08001969 task_rlimit(p, RLIMIT_NPROC)) {
Eric Parisb57922b2013-07-03 15:08:29 -07001970 if (p->real_cred->user != INIT_USER &&
1971 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto bad_fork_free;
1973 }
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +04001974 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
David Howellsf1752ee2008-11-14 10:39:17 +11001976 retval = copy_creds(p, clone_flags);
1977 if (retval < 0)
1978 goto bad_fork_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
1981 * If multiple threads are within copy_process(), then this check
1982 * triggers too late. This doesn't hurt, the check is only there
1983 * to stop root fork bombs.
1984 */
Li Zefan04ec93f2009-02-06 08:17:19 +00001985 retval = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (nr_threads >= max_threads)
1987 goto bad_fork_cleanup_count;
1988
Shailabh Nagarca74e922006-07-14 00:24:36 -07001989 delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
Peter Zijlstrac1de45c2016-11-28 23:03:05 -08001990 p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE);
David Rientjes514ddb42014-04-07 15:37:27 -07001991 p->flags |= PF_FORKNOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 INIT_LIST_HEAD(&p->children);
1993 INIT_LIST_HEAD(&p->sibling);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001994 rcu_copy_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 p->vfork_done = NULL;
1996 spin_lock_init(&p->alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 init_sigpending(&p->pending);
1999
Martin Schwidefsky64861632011-12-15 14:56:09 +01002000 p->utime = p->stime = p->gtime = 0;
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01002001#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Martin Schwidefsky64861632011-12-15 14:56:09 +01002002 p->utimescaled = p->stimescaled = 0;
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01002003#endif
Peter Zijlstra9d7fb042015-06-30 11:30:54 +02002004 prev_cputime_init(&p->prev_cputime);
2005
Frederic Weisbecker6a616712012-12-16 20:00:34 +01002006#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbeckerbac5b6b2017-06-29 19:15:10 +02002007 seqcount_init(&p->vtime.seqcount);
2008 p->vtime.starttime = 0;
2009 p->vtime.state = VTIME_INACTIVE;
Frederic Weisbecker6a616712012-12-16 20:00:34 +01002010#endif
2011
KAMEZAWA Hiroyukia3a2e762010-04-06 14:34:42 -07002012#if defined(SPLIT_RSS_COUNTING)
2013 memset(&p->rss_stat, 0, sizeof(p->rss_stat));
2014#endif
Balbir Singh172ba842007-07-09 18:52:00 +02002015
Arjan van de Ven69766752008-09-01 15:52:40 -07002016 p->default_timer_slack_ns = current->timer_slack_ns;
2017
Johannes Weinereb414682018-10-26 15:06:27 -07002018#ifdef CONFIG_PSI
2019 p->psi_flags = 0;
2020#endif
2021
Andrea Righi59954772008-07-27 17:29:15 +02002022 task_io_accounting_init(&p->ioac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 acct_clear_integrals(p);
2024
Thomas Gleixner3a245c02019-08-21 21:09:06 +02002025 posix_cputimers_init(&p->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 p->io_context = NULL;
Richard Guy Briggsc0b0ae82018-05-12 21:58:21 -04002028 audit_set_context(p, NULL);
Paul Menageb4f48b62007-10-18 23:39:33 -07002029 cgroup_fork(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030#ifdef CONFIG_NUMA
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002031 p->mempolicy = mpol_dup(p->mempolicy);
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002032 if (IS_ERR(p->mempolicy)) {
2033 retval = PTR_ERR(p->mempolicy);
2034 p->mempolicy = NULL;
Li Zefane8604cb2014-03-28 15:18:27 +08002035 goto bad_fork_cleanup_threadgroup_lock;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037#endif
Michal Hocko778d3b02011-07-26 16:08:30 -07002038#ifdef CONFIG_CPUSETS
2039 p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
2040 p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002041 seqcount_init(&p->mems_allowed_seq);
Michal Hocko778d3b02011-07-26 16:08:30 -07002042#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -07002043#ifdef CONFIG_TRACE_IRQFLAGS
2044 p->irq_events = 0;
2045 p->hardirqs_enabled = 0;
2046 p->hardirq_enable_ip = 0;
2047 p->hardirq_enable_event = 0;
2048 p->hardirq_disable_ip = _THIS_IP_;
2049 p->hardirq_disable_event = 0;
2050 p->softirqs_enabled = 1;
2051 p->softirq_enable_ip = _THIS_IP_;
2052 p->softirq_enable_event = 0;
2053 p->softirq_disable_ip = 0;
2054 p->softirq_disable_event = 0;
2055 p->hardirq_context = 0;
2056 p->softirq_context = 0;
2057#endif
David Hildenbrand8bcbde52015-05-11 17:52:06 +02002058
2059 p->pagefault_disabled = 0;
2060
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002061#ifdef CONFIG_LOCKDEP
Byungchul Parkb09be672017-08-07 16:12:52 +09002062 lockdep_init_task(p);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002063#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Ingo Molnar408894e2006-01-09 15:59:20 -08002065#ifdef CONFIG_DEBUG_MUTEXES
2066 p->blocked_on = NULL; /* not blocked yet */
2067#endif
Kent Overstreetcafe5632013-03-23 16:11:31 -07002068#ifdef CONFIG_BCACHE
2069 p->sequential_io = 0;
2070 p->sequential_io_avg = 0;
2071#endif
Markus Metzger0f481402009-04-03 16:43:48 +02002072
Srivatsa Vaddagiri3c90e6e2007-11-09 22:39:39 +01002073 /* Perform scheduler related setup. Assign this task to a CPU. */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002074 retval = sched_fork(clone_flags, p);
2075 if (retval)
2076 goto bad_fork_cleanup_policy;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02002077
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078 retval = perf_event_init_task(p);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02002079 if (retval)
2080 goto bad_fork_cleanup_policy;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002081 retval = audit_alloc(p);
2082 if (retval)
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07002083 goto bad_fork_cleanup_perf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /* copy all the process information */
Jack Millerab602f72014-08-08 14:23:19 -07002085 shm_init_task(p);
Tetsuo Handae4e55b42017-03-24 20:46:33 +09002086 retval = security_task_alloc(p, clone_flags);
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002087 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto bad_fork_cleanup_audit;
Tetsuo Handae4e55b42017-03-24 20:46:33 +09002089 retval = copy_semundo(clone_flags, p);
2090 if (retval)
2091 goto bad_fork_cleanup_security;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002092 retval = copy_files(clone_flags, p);
2093 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 goto bad_fork_cleanup_semundo;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002095 retval = copy_fs(clone_flags, p);
2096 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto bad_fork_cleanup_files;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002098 retval = copy_sighand(clone_flags, p);
2099 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto bad_fork_cleanup_fs;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002101 retval = copy_signal(clone_flags, p);
2102 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto bad_fork_cleanup_sighand;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002104 retval = copy_mm(clone_flags, p);
2105 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 goto bad_fork_cleanup_signal;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002107 retval = copy_namespaces(clone_flags, p);
2108 if (retval)
David Howellsd84f4f92008-11-14 10:39:23 +11002109 goto bad_fork_cleanup_mm;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002110 retval = copy_io(clone_flags, p);
2111 if (retval)
Jens Axboefd0928d2008-01-24 08:52:45 +01002112 goto bad_fork_cleanup_namespaces;
Christian Brauner7f192e32019-05-25 11:36:41 +02002113 retval = copy_thread_tls(clone_flags, args->stack, args->stack_size, p,
2114 args->tls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (retval)
Jens Axboefd0928d2008-01-24 08:52:45 +01002116 goto bad_fork_cleanup_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Alexander Popovafaef012018-08-17 01:16:58 +03002118 stackleak_task_init(p);
2119
Pavel Emelyanov425fb2b2007-10-18 23:40:07 -07002120 if (pid != &init_struct_pid) {
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002121 pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid,
2122 args->set_tid_size);
Michal Hocko35f71bc2015-04-16 12:47:38 -07002123 if (IS_ERR(pid)) {
2124 retval = PTR_ERR(pid);
Jiri Slaby0740aa52016-05-20 17:00:25 -07002125 goto bad_fork_cleanup_thread;
Michal Hocko35f71bc2015-04-16 12:47:38 -07002126 }
Pavel Emelyanov425fb2b2007-10-18 23:40:07 -07002127 }
2128
Christian Braunerb3e583822019-03-27 13:04:15 +01002129 /*
2130 * This has to happen after we've potentially unshared the file
2131 * descriptor table (so that the pidfd doesn't leak into the child
2132 * if the fd table isn't shared).
2133 */
2134 if (clone_flags & CLONE_PIDFD) {
Al Viro6fd2fe42019-06-26 22:22:09 -04002135 retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
Christian Braunerb3e583822019-03-27 13:04:15 +01002136 if (retval < 0)
2137 goto bad_fork_free_pid;
2138
2139 pidfd = retval;
Al Viro6fd2fe42019-06-26 22:22:09 -04002140
2141 pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid,
2142 O_RDWR | O_CLOEXEC);
2143 if (IS_ERR(pidfile)) {
2144 put_unused_fd(pidfd);
Christian Brauner28dd29c2019-07-01 16:01:46 +02002145 retval = PTR_ERR(pidfile);
Al Viro6fd2fe42019-06-26 22:22:09 -04002146 goto bad_fork_free_pid;
2147 }
2148 get_pid(pid); /* held by pidfile now */
2149
Christian Brauner7f192e32019-05-25 11:36:41 +02002150 retval = put_user(pidfd, args->pidfd);
Christian Braunerb3e583822019-03-27 13:04:15 +01002151 if (retval)
2152 goto bad_fork_put_pidfd;
2153 }
2154
Jens Axboe73c10102011-03-08 13:19:51 +01002155#ifdef CONFIG_BLOCK
2156 p->plug = NULL;
2157#endif
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01002158 futex_init_task(p);
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
GOTO Masanorif9a38792006-03-13 21:20:44 -08002161 * sigaltstack should be cleared when sharing the same VM
2162 */
2163 if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
Stas Sergeev2a742132016-04-14 23:20:04 +03002164 sas_ss_reset(p);
GOTO Masanorif9a38792006-03-13 21:20:44 -08002165
2166 /*
Oleg Nesterov65808072009-12-15 16:47:16 -08002167 * Syscall tracing and stepping should be turned off in the
2168 * child regardless of CLONE_PTRACE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
Oleg Nesterov65808072009-12-15 16:47:16 -08002170 user_disable_single_step(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
Laurent Viviered75e8d2005-09-03 15:57:18 -07002172#ifdef TIF_SYSCALL_EMU
2173 clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
2174#endif
Lin Fenge02c9b02019-05-14 15:42:34 -07002175 clear_tsk_latency_tracing(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* ok, now we should be set up.. */
Oleg Nesterov18c830d2013-07-03 15:08:32 -07002178 p->pid = pid_nr(pid);
2179 if (clone_flags & CLONE_THREAD) {
Oleg Nesterov5f8aadd2012-03-14 19:55:38 +01002180 p->exit_signal = -1;
Oleg Nesterov18c830d2013-07-03 15:08:32 -07002181 p->group_leader = current->group_leader;
2182 p->tgid = current->tgid;
2183 } else {
2184 if (clone_flags & CLONE_PARENT)
2185 p->exit_signal = current->group_leader->exit_signal;
2186 else
Christian Brauner7f192e32019-05-25 11:36:41 +02002187 p->exit_signal = args->exit_signal;
Oleg Nesterov18c830d2013-07-03 15:08:32 -07002188 p->group_leader = p;
2189 p->tgid = p->pid;
2190 }
Oleg Nesterov5f8aadd2012-03-14 19:55:38 +01002191
Wu Fengguang9d823e82011-06-11 18:10:12 -06002192 p->nr_dirtied = 0;
2193 p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
Wu Fengguang83712352011-06-11 19:25:42 -06002194 p->dirty_paused_when = 0;
Wu Fengguang9d823e82011-06-11 18:10:12 -06002195
Oleg Nesterovbb8cbbf2013-11-13 15:36:12 +01002196 p->pdeath_signal = 0;
Oleg Nesterov47e65322006-03-28 16:11:25 -08002197 INIT_LIST_HEAD(&p->thread_group);
Al Viro158e1642012-06-27 09:24:13 +04002198 p->task_works = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Oleg Nesterov18c830d2013-07-03 15:08:32 -07002200 /*
Aleksa Sarai7e476822015-06-09 21:32:09 +10002201 * Ensure that the cgroup subsystem policies allow the new process to be
2202 * forked. It should be noted the the new process's css_set can be changed
2203 * between here and cgroup_post_fork() if an organisation operation is in
2204 * progress.
2205 */
Christian Brauneref2c41c2020-02-05 14:26:22 +01002206 retval = cgroup_can_fork(p, args);
Aleksa Sarai7e476822015-06-09 21:32:09 +10002207 if (retval)
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01002208 goto bad_fork_put_pidfd;
Aleksa Sarai7e476822015-06-09 21:32:09 +10002209
2210 /*
David Herrmann7b558512019-01-08 13:58:52 +01002211 * From this point on we must avoid any synchronous user-space
2212 * communication until we take the tasklist-lock. In particular, we do
2213 * not want user-space to be able to predict the process start-time by
2214 * stalling fork(2) after we recorded the start_time but before it is
2215 * visible to the system.
2216 */
2217
2218 p->start_time = ktime_get_ns();
Peter Zijlstracf25e242019-11-07 11:07:58 +01002219 p->start_boottime = ktime_get_boottime_ns();
David Herrmann7b558512019-01-08 13:58:52 +01002220
2221 /*
Oleg Nesterov18c830d2013-07-03 15:08:32 -07002222 * Make it visible to the rest of the system, but dont wake it up yet.
2223 * Need tasklist lock for parent etc handling!
2224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 write_lock_irq(&tasklist_lock);
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* CLONE_PARENT re-uses the old parent */
Oleg Nesterov2d5516c2009-03-02 22:58:45 +01002228 if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 p->real_parent = current->real_parent;
Oleg Nesterov2d5516c2009-03-02 22:58:45 +01002230 p->parent_exec_id = current->parent_exec_id;
2231 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 p->real_parent = current;
Oleg Nesterov2d5516c2009-03-02 22:58:45 +01002233 p->parent_exec_id = current->self_exec_id;
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -06002236 klp_copy_process(p);
2237
Oleg Nesterov3f17da62006-02-15 22:13:24 +03002238 spin_lock(&current->sighand->siglock);
Oleg Nesterov4a2c7a72006-03-28 16:11:26 -08002239
2240 /*
Kees Cookdbd952122014-06-27 15:18:48 -07002241 * Copy seccomp details explicitly here, in case they were changed
2242 * before holding sighand lock.
2243 */
2244 copy_seccomp(p);
2245
Mathieu Desnoyersd7822b12018-06-02 08:43:54 -04002246 rseq_fork(p, clone_flags);
2247
Eric W. Biederman4ca1d3e2018-07-13 15:30:33 -05002248 /* Don't start children in a dying pid namespace */
Gargi Sharmae8cfbc22017-11-17 15:30:34 -08002249 if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
Kirill Tkhai3fd37222017-05-12 19:11:31 +03002250 retval = -ENOMEM;
2251 goto bad_fork_cancel_cgroup;
2252 }
Oleg Nesterov4a2c7a72006-03-28 16:11:26 -08002253
Eric W. Biederman7673bf52018-07-23 08:01:10 -05002254 /* Let kill terminate clone/fork in the middle */
2255 if (fatal_signal_pending(current)) {
2256 retval = -EINTR;
2257 goto bad_fork_cancel_cgroup;
2258 }
2259
Al Viro6fd2fe42019-06-26 22:22:09 -04002260 /* past the last point of failure */
2261 if (pidfile)
2262 fd_install(pidfd, pidfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Eric W. Biederman2c470472017-09-26 13:06:43 -05002264 init_task_pid_links(p);
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -08002265 if (likely(p->pid)) {
Tejun Heo4b9d33e2011-06-17 16:50:38 +02002266 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Oleg Nesterov81907732013-07-03 15:08:31 -07002268 init_task_pid(p, PIDTYPE_PID, pid);
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -08002269 if (thread_group_leader(p)) {
Eric W. Biederman6883f812017-06-04 04:32:13 -05002270 init_task_pid(p, PIDTYPE_TGID, pid);
Oleg Nesterov81907732013-07-03 15:08:31 -07002271 init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
2272 init_task_pid(p, PIDTYPE_SID, task_session(current));
2273
Eric W. Biederman1c4042c2010-07-12 17:10:36 -07002274 if (is_child_reaper(pid)) {
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002275 ns_of_pid(pid)->child_reaper = p;
Eric W. Biederman1c4042c2010-07-12 17:10:36 -07002276 p->signal->flags |= SIGNAL_UNKILLABLE;
2277 }
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05002278 p->signal->shared_pending.signal = delayed.signal;
Alan Cox9c9f4de2008-10-13 10:37:26 +01002279 p->signal->tty = tty_kref_get(current->signal->tty);
Pavel Tikhomirov749860c2017-01-30 18:06:12 +03002280 /*
2281 * Inherit has_child_subreaper flag under the same
2282 * tasklist_lock with adding child to the process tree
2283 * for propagate_has_child_subreaper optimization.
2284 */
2285 p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper ||
2286 p->real_parent->signal->is_child_subreaper;
Oleg Nesterov9cd80bb2009-12-17 15:27:15 -08002287 list_add_tail(&p->sibling, &p->real_parent->children);
Eric W. Biederman5e85d4a2006-04-18 22:20:16 -07002288 list_add_tail_rcu(&p->tasks, &init_task.tasks);
Eric W. Biederman6883f812017-06-04 04:32:13 -05002289 attach_pid(p, PIDTYPE_TGID);
Oleg Nesterov81907732013-07-03 15:08:31 -07002290 attach_pid(p, PIDTYPE_PGID);
2291 attach_pid(p, PIDTYPE_SID);
Christoph Lameter909ea962010-12-08 16:22:55 +01002292 __this_cpu_inc(process_counts);
Oleg Nesterov80628ca2013-07-03 15:08:30 -07002293 } else {
2294 current->signal->nr_threads++;
2295 atomic_inc(&current->signal->live);
Elena Reshetova60d4de32019-01-18 14:27:27 +02002296 refcount_inc(&current->signal->sigcnt);
Eric W. Biederman924de3b2018-07-23 13:38:00 -05002297 task_join_group_stop(p);
Oleg Nesterov80628ca2013-07-03 15:08:30 -07002298 list_add_tail_rcu(&p->thread_group,
2299 &p->group_leader->thread_group);
Oleg Nesterov0c740d02014-01-21 15:49:56 -08002300 list_add_tail_rcu(&p->thread_node,
2301 &p->signal->thread_head);
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -08002302 }
Oleg Nesterov81907732013-07-03 15:08:31 -07002303 attach_pid(p, PIDTYPE_PID);
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -08002304 nr_threads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 total_forks++;
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05002307 hlist_del_init(&delayed.node);
Oleg Nesterov3f17da62006-02-15 22:13:24 +03002308 spin_unlock(&current->sighand->siglock);
Oleg Nesterov4af42062014-04-13 20:58:54 +02002309 syscall_tracepoint_update(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 write_unlock_irq(&tasklist_lock);
Oleg Nesterov4af42062014-04-13 20:58:54 +02002311
Andrew Mortonc13cf852005-11-28 13:43:48 -08002312 proc_fork_connector(p);
Christian Brauneref2c41c2020-02-05 14:26:22 +01002313 cgroup_post_fork(p, args);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314 perf_event_fork(p);
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08002315
2316 trace_task_newtask(p, clone_flags);
Oleg Nesterov3ab67962013-10-16 19:39:37 +02002317 uprobe_copy_process(p, clone_flags);
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return p;
2320
Aleksa Sarai7e476822015-06-09 21:32:09 +10002321bad_fork_cancel_cgroup:
Kirill Tkhai3fd37222017-05-12 19:11:31 +03002322 spin_unlock(&current->sighand->siglock);
2323 write_unlock_irq(&tasklist_lock);
Christian Brauneref2c41c2020-02-05 14:26:22 +01002324 cgroup_cancel_fork(p, args);
Christian Braunerb3e583822019-03-27 13:04:15 +01002325bad_fork_put_pidfd:
Al Viro6fd2fe42019-06-26 22:22:09 -04002326 if (clone_flags & CLONE_PIDFD) {
2327 fput(pidfile);
2328 put_unused_fd(pidfd);
2329 }
Pavel Emelyanov425fb2b2007-10-18 23:40:07 -07002330bad_fork_free_pid:
2331 if (pid != &init_struct_pid)
2332 free_pid(pid);
Jiri Slaby0740aa52016-05-20 17:00:25 -07002333bad_fork_cleanup_thread:
2334 exit_thread(p);
Jens Axboefd0928d2008-01-24 08:52:45 +01002335bad_fork_cleanup_io:
Louis Rillingb69f2292009-12-04 14:52:42 +01002336 if (p->io_context)
2337 exit_io_context(p);
Serge E. Hallynab516012006-10-02 02:18:06 -07002338bad_fork_cleanup_namespaces:
Linus Torvalds444f3782007-01-30 13:35:18 -08002339 exit_task_namespaces(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340bad_fork_cleanup_mm:
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07002341 if (p->mm) {
2342 mm_clear_owner(p->mm, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mmput(p->mm);
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07002344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345bad_fork_cleanup_signal:
Oleg Nesterov4ab6c082009-08-26 14:29:24 -07002346 if (!(clone_flags & CLONE_THREAD))
Mike Galbraith1c5354d2011-01-05 11:16:04 +01002347 free_signal_struct(p->signal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348bad_fork_cleanup_sighand:
Oleg Nesterova7e53282006-03-28 16:11:27 -08002349 __cleanup_sighand(p->sighand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350bad_fork_cleanup_fs:
2351 exit_fs(p); /* blocking */
2352bad_fork_cleanup_files:
2353 exit_files(p); /* blocking */
2354bad_fork_cleanup_semundo:
2355 exit_sem(p);
Tetsuo Handae4e55b42017-03-24 20:46:33 +09002356bad_fork_cleanup_security:
2357 security_task_free(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358bad_fork_cleanup_audit:
2359 audit_free(p);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07002360bad_fork_cleanup_perf:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 perf_event_free_task(p);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07002362bad_fork_cleanup_policy:
Byungchul Parkb09be672017-08-07 16:12:52 +09002363 lockdep_free_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364#ifdef CONFIG_NUMA
Lee Schermerhornf0be3d32008-04-28 02:13:08 -07002365 mpol_put(p->mempolicy);
Li Zefane8604cb2014-03-28 15:18:27 +08002366bad_fork_cleanup_threadgroup_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367#endif
Shailabh Nagar35df17c2006-08-31 21:27:38 -07002368 delayacct_tsk_free(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369bad_fork_cleanup_count:
David Howellsd84f4f92008-11-14 10:39:23 +11002370 atomic_dec(&p->cred->user->processes);
David Howellse0e81732009-09-02 09:13:40 +01002371 exit_creds(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372bad_fork_free:
Andy Lutomirski405c0752016-10-31 08:11:43 -07002373 p->state = TASK_DEAD;
Andy Lutomirski68f24b082016-09-15 22:45:48 -07002374 put_task_stack(p);
Andrea Arcangelic3f3ce02019-05-14 15:40:46 -07002375 delayed_free_task(p);
Oleg Nesterovfe7d37d2006-01-08 01:04:02 -08002376fork_out:
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05002377 spin_lock_irq(&current->sighand->siglock);
2378 hlist_del_init(&delayed.node);
2379 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovfe7d37d2006-01-08 01:04:02 -08002380 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Eric W. Biederman2c470472017-09-26 13:06:43 -05002383static inline void init_idle_pids(struct task_struct *idle)
Oleg Nesterovf106eee2010-05-26 14:44:11 -07002384{
2385 enum pid_type type;
2386
2387 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
Eric W. Biederman2c470472017-09-26 13:06:43 -05002388 INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */
2389 init_task_pid(idle, type, &init_struct_pid);
Oleg Nesterovf106eee2010-05-26 14:44:11 -07002390 }
2391}
2392
Paul Gortmaker0db06282013-06-19 14:53:51 -04002393struct task_struct *fork_idle(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Ingo Molnar36c8b582006-07-03 00:25:41 -07002395 struct task_struct *task;
Christian Brauner7f192e32019-05-25 11:36:41 +02002396 struct kernel_clone_args args = {
2397 .flags = CLONE_VM,
2398 };
2399
2400 task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args);
Oleg Nesterovf106eee2010-05-26 14:44:11 -07002401 if (!IS_ERR(task)) {
Eric W. Biederman2c470472017-09-26 13:06:43 -05002402 init_idle_pids(task);
Akinobu Mita753ca4f2006-11-25 11:09:34 -08002403 init_idle(task, cpu);
Oleg Nesterovf106eee2010-05-26 14:44:11 -07002404 }
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -08002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return task;
2407}
2408
Nadav Amit13585fa2019-04-25 17:11:25 -07002409struct mm_struct *copy_init_mm(void)
2410{
2411 return dup_mm(NULL, &init_mm);
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/*
2415 * Ok, this is the main fork-routine.
2416 *
2417 * It copies the process, and if successful kick-starts
2418 * it and waits for it to finish using the VM if required.
Eugene Syromiatnikova0eb9ab2019-09-11 18:45:40 +01002419 *
2420 * args->exit_signal is expected to be checked for sanity by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
Christian Brauner7f192e32019-05-25 11:36:41 +02002422long _do_fork(struct kernel_clone_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Christian Brauner7f192e32019-05-25 11:36:41 +02002424 u64 clone_flags = args->flags;
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002425 struct completion vfork;
2426 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct task_struct *p;
2428 int trace = 0;
Eric W. Biederman92476d72006-03-31 02:31:42 -08002429 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Andrew Mortonbdff7462008-02-04 22:27:22 -08002431 /*
Tejun Heo4b9d33e2011-06-17 16:50:38 +02002432 * Determine whether and which event to report to ptracer. When
2433 * called from kernel_thread or CLONE_UNTRACED is explicitly
2434 * requested, no event is reported; otherwise, report if the event
2435 * for the type of forking is enabled.
Roland McGrath09a05392008-07-25 19:45:47 -07002436 */
Al Viroe80d6662012-10-22 23:10:08 -04002437 if (!(clone_flags & CLONE_UNTRACED)) {
Tejun Heo4b9d33e2011-06-17 16:50:38 +02002438 if (clone_flags & CLONE_VFORK)
2439 trace = PTRACE_EVENT_VFORK;
Christian Brauner7f192e32019-05-25 11:36:41 +02002440 else if (args->exit_signal != SIGCHLD)
Tejun Heo4b9d33e2011-06-17 16:50:38 +02002441 trace = PTRACE_EVENT_CLONE;
2442 else
2443 trace = PTRACE_EVENT_FORK;
2444
2445 if (likely(!ptrace_event_enabled(current, trace)))
2446 trace = 0;
2447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Christian Brauner7f192e32019-05-25 11:36:41 +02002449 p = copy_process(NULL, trace, NUMA_NO_NODE, args);
Emese Revfy38addce2016-06-20 20:41:19 +02002450 add_latent_entropy();
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002451
2452 if (IS_ERR(p))
2453 return PTR_ERR(p);
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /*
2456 * Do this prior waking up the new thread - the thread pointer
2457 * might get invalid after that point, if the thread exits quickly.
2458 */
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002459 trace_sched_process_fork(current, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002461 pid = get_task_pid(p, PIDTYPE_PID);
2462 nr = pid_vnr(pid);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002463
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002464 if (clone_flags & CLONE_PARENT_SETTID)
Christian Brauner7f192e32019-05-25 11:36:41 +02002465 put_user(nr, args->parent_tid);
Pavel Emelyanov30e49c22007-10-18 23:40:10 -07002466
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002467 if (clone_flags & CLONE_VFORK) {
2468 p->vfork_done = &vfork;
2469 init_completion(&vfork);
2470 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
Marcos Paulo de Souza9f5325a2018-02-06 15:39:30 -08002472
2473 wake_up_new_task(p);
2474
2475 /* forking complete and child started to run, tell ptracer */
2476 if (unlikely(trace))
2477 ptrace_event_pid(trace, pid);
2478
2479 if (clone_flags & CLONE_VFORK) {
2480 if (!wait_for_vfork_done(p, &vfork))
2481 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
2482 }
2483
2484 put_pid(pid);
Eric W. Biederman92476d72006-03-31 02:31:42 -08002485 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Dmitry V. Levin028b6e8a2019-07-14 19:20:47 +03002488bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)
2489{
2490 /* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
2491 if ((kargs->flags & CLONE_PIDFD) &&
2492 (kargs->flags & CLONE_PARENT_SETTID))
2493 return false;
2494
2495 return true;
2496}
2497
Josh Triplett3033f14a2015-06-25 15:01:19 -07002498#ifndef CONFIG_HAVE_COPY_THREAD_TLS
2499/* For compatibility with architectures that call do_fork directly rather than
2500 * using the syscall entry points below. */
2501long do_fork(unsigned long clone_flags,
2502 unsigned long stack_start,
2503 unsigned long stack_size,
2504 int __user *parent_tidptr,
2505 int __user *child_tidptr)
2506{
Christian Brauner7f192e32019-05-25 11:36:41 +02002507 struct kernel_clone_args args = {
Christian Brauner3f2c7882020-05-07 12:32:14 +02002508 .flags = (lower_32_bits(clone_flags) & ~CSIGNAL),
Dmitry V. Levin028b6e8a2019-07-14 19:20:47 +03002509 .pidfd = parent_tidptr,
Christian Brauner7f192e32019-05-25 11:36:41 +02002510 .child_tid = child_tidptr,
2511 .parent_tid = parent_tidptr,
Christian Brauner3f2c7882020-05-07 12:32:14 +02002512 .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL),
Christian Brauner7f192e32019-05-25 11:36:41 +02002513 .stack = stack_start,
2514 .stack_size = stack_size,
2515 };
2516
Dmitry V. Levin028b6e8a2019-07-14 19:20:47 +03002517 if (!legacy_clone_args_valid(&args))
2518 return -EINVAL;
2519
Christian Brauner7f192e32019-05-25 11:36:41 +02002520 return _do_fork(&args);
Josh Triplett3033f14a2015-06-25 15:01:19 -07002521}
2522#endif
2523
Al Viro2aa3a7f2012-09-21 19:55:31 -04002524/*
2525 * Create a kernel thread.
2526 */
2527pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
2528{
Christian Brauner7f192e32019-05-25 11:36:41 +02002529 struct kernel_clone_args args = {
Christian Brauner3f2c7882020-05-07 12:32:14 +02002530 .flags = ((lower_32_bits(flags) | CLONE_VM |
2531 CLONE_UNTRACED) & ~CSIGNAL),
2532 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
Christian Brauner7f192e32019-05-25 11:36:41 +02002533 .stack = (unsigned long)fn,
2534 .stack_size = (unsigned long)arg,
2535 };
2536
2537 return _do_fork(&args);
Al Viro2aa3a7f2012-09-21 19:55:31 -04002538}
Al Viro2aa3a7f2012-09-21 19:55:31 -04002539
Al Virod2125042012-10-23 13:17:59 -04002540#ifdef __ARCH_WANT_SYS_FORK
2541SYSCALL_DEFINE0(fork)
2542{
2543#ifdef CONFIG_MMU
Christian Brauner7f192e32019-05-25 11:36:41 +02002544 struct kernel_clone_args args = {
2545 .exit_signal = SIGCHLD,
2546 };
2547
2548 return _do_fork(&args);
Al Virod2125042012-10-23 13:17:59 -04002549#else
2550 /* can not support in nommu mode */
Daeseok Youn5d59e182014-01-23 15:55:47 -08002551 return -EINVAL;
Al Virod2125042012-10-23 13:17:59 -04002552#endif
2553}
2554#endif
2555
2556#ifdef __ARCH_WANT_SYS_VFORK
2557SYSCALL_DEFINE0(vfork)
2558{
Christian Brauner7f192e32019-05-25 11:36:41 +02002559 struct kernel_clone_args args = {
2560 .flags = CLONE_VFORK | CLONE_VM,
2561 .exit_signal = SIGCHLD,
2562 };
2563
2564 return _do_fork(&args);
Al Virod2125042012-10-23 13:17:59 -04002565}
2566#endif
2567
2568#ifdef __ARCH_WANT_SYS_CLONE
2569#ifdef CONFIG_CLONE_BACKWARDS
2570SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
2571 int __user *, parent_tidptr,
Josh Triplett3033f14a2015-06-25 15:01:19 -07002572 unsigned long, tls,
Al Virod2125042012-10-23 13:17:59 -04002573 int __user *, child_tidptr)
2574#elif defined(CONFIG_CLONE_BACKWARDS2)
2575SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
2576 int __user *, parent_tidptr,
2577 int __user *, child_tidptr,
Josh Triplett3033f14a2015-06-25 15:01:19 -07002578 unsigned long, tls)
Michal Simekdfa97712013-08-13 16:00:53 -07002579#elif defined(CONFIG_CLONE_BACKWARDS3)
2580SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
2581 int, stack_size,
2582 int __user *, parent_tidptr,
2583 int __user *, child_tidptr,
Josh Triplett3033f14a2015-06-25 15:01:19 -07002584 unsigned long, tls)
Al Virod2125042012-10-23 13:17:59 -04002585#else
2586SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
2587 int __user *, parent_tidptr,
2588 int __user *, child_tidptr,
Josh Triplett3033f14a2015-06-25 15:01:19 -07002589 unsigned long, tls)
Al Virod2125042012-10-23 13:17:59 -04002590#endif
2591{
Christian Brauner7f192e32019-05-25 11:36:41 +02002592 struct kernel_clone_args args = {
Christian Brauner3f2c7882020-05-07 12:32:14 +02002593 .flags = (lower_32_bits(clone_flags) & ~CSIGNAL),
Christian Brauner7f192e32019-05-25 11:36:41 +02002594 .pidfd = parent_tidptr,
2595 .child_tid = child_tidptr,
2596 .parent_tid = parent_tidptr,
Christian Brauner3f2c7882020-05-07 12:32:14 +02002597 .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL),
Christian Brauner7f192e32019-05-25 11:36:41 +02002598 .stack = newsp,
2599 .tls = tls,
2600 };
2601
Dmitry V. Levin028b6e8a2019-07-14 19:20:47 +03002602 if (!legacy_clone_args_valid(&args))
Christian Brauner7f192e32019-05-25 11:36:41 +02002603 return -EINVAL;
2604
2605 return _do_fork(&args);
2606}
Christian Braunerd68dbb02019-06-21 01:26:35 +02002607#endif
Christian Brauner7f192e32019-05-25 11:36:41 +02002608
Christian Braunerd68dbb02019-06-21 01:26:35 +02002609#ifdef __ARCH_WANT_SYS_CLONE3
Amanieu d'Antrasdd499f72020-01-02 18:24:13 +01002610
2611/*
2612 * copy_thread implementations handle CLONE_SETTLS by reading the TLS value from
2613 * the registers containing the syscall arguments for clone. This doesn't work
2614 * with clone3 since the TLS value is passed in clone_args instead.
2615 */
2616#ifndef CONFIG_HAVE_COPY_THREAD_TLS
2617#error clone3 requires copy_thread_tls support in arch
2618#endif
2619
Christian Brauner7f192e32019-05-25 11:36:41 +02002620noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
2621 struct clone_args __user *uargs,
Aleksa Saraif14c2342019-10-01 11:10:53 +10002622 size_t usize)
Christian Brauner7f192e32019-05-25 11:36:41 +02002623{
Aleksa Saraif14c2342019-10-01 11:10:53 +10002624 int err;
Christian Brauner7f192e32019-05-25 11:36:41 +02002625 struct clone_args args;
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002626 pid_t *kset_tid = kargs->set_tid;
Christian Brauner7f192e32019-05-25 11:36:41 +02002627
Eugene Syromiatnikova966dcf2020-04-12 22:26:58 +02002628 BUILD_BUG_ON(offsetofend(struct clone_args, tls) !=
2629 CLONE_ARGS_SIZE_VER0);
2630 BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) !=
2631 CLONE_ARGS_SIZE_VER1);
2632 BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) !=
2633 CLONE_ARGS_SIZE_VER2);
2634 BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2);
2635
Aleksa Saraif14c2342019-10-01 11:10:53 +10002636 if (unlikely(usize > PAGE_SIZE))
Christian Brauner7f192e32019-05-25 11:36:41 +02002637 return -E2BIG;
Aleksa Saraif14c2342019-10-01 11:10:53 +10002638 if (unlikely(usize < CLONE_ARGS_SIZE_VER0))
Christian Brauner7f192e32019-05-25 11:36:41 +02002639 return -EINVAL;
2640
Aleksa Saraif14c2342019-10-01 11:10:53 +10002641 err = copy_struct_from_user(&args, sizeof(args), uargs, usize);
2642 if (err)
2643 return err;
Christian Brauner7f192e32019-05-25 11:36:41 +02002644
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002645 if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL))
2646 return -EINVAL;
2647
2648 if (unlikely(!args.set_tid && args.set_tid_size > 0))
2649 return -EINVAL;
2650
2651 if (unlikely(args.set_tid && args.set_tid_size == 0))
2652 return -EINVAL;
2653
Eugene Syromiatnikova0eb9ab2019-09-11 18:45:40 +01002654 /*
2655 * Verify that higher 32bits of exit_signal are unset and that
2656 * it is a valid signal
2657 */
2658 if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) ||
2659 !valid_signal(args.exit_signal)))
2660 return -EINVAL;
2661
Eugene Syromiatnikov62173872020-04-12 22:31:23 +02002662 if ((args.flags & CLONE_INTO_CGROUP) &&
2663 (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2))
Christian Brauneref2c41c2020-02-05 14:26:22 +01002664 return -EINVAL;
2665
Christian Brauner7f192e32019-05-25 11:36:41 +02002666 *kargs = (struct kernel_clone_args){
2667 .flags = args.flags,
2668 .pidfd = u64_to_user_ptr(args.pidfd),
2669 .child_tid = u64_to_user_ptr(args.child_tid),
2670 .parent_tid = u64_to_user_ptr(args.parent_tid),
2671 .exit_signal = args.exit_signal,
2672 .stack = args.stack,
2673 .stack_size = args.stack_size,
2674 .tls = args.tls,
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002675 .set_tid_size = args.set_tid_size,
Christian Brauneref2c41c2020-02-05 14:26:22 +01002676 .cgroup = args.cgroup,
Christian Brauner7f192e32019-05-25 11:36:41 +02002677 };
2678
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002679 if (args.set_tid &&
2680 copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid),
2681 (kargs->set_tid_size * sizeof(pid_t))))
2682 return -EFAULT;
2683
2684 kargs->set_tid = kset_tid;
2685
Christian Brauner7f192e32019-05-25 11:36:41 +02002686 return 0;
2687}
2688
Christian Braunerfa729c42019-10-31 12:36:08 +01002689/**
2690 * clone3_stack_valid - check and prepare stack
2691 * @kargs: kernel clone args
2692 *
2693 * Verify that the stack arguments userspace gave us are sane.
2694 * In addition, set the stack direction for userspace since it's easy for us to
2695 * determine.
2696 */
2697static inline bool clone3_stack_valid(struct kernel_clone_args *kargs)
2698{
2699 if (kargs->stack == 0) {
2700 if (kargs->stack_size > 0)
2701 return false;
2702 } else {
2703 if (kargs->stack_size == 0)
2704 return false;
2705
2706 if (!access_ok((void __user *)kargs->stack, kargs->stack_size))
2707 return false;
2708
2709#if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64)
2710 kargs->stack += kargs->stack_size;
2711#endif
2712 }
2713
2714 return true;
2715}
2716
2717static bool clone3_args_valid(struct kernel_clone_args *kargs)
Christian Brauner7f192e32019-05-25 11:36:41 +02002718{
Christian Braunerb612e5d2019-10-14 12:45:37 +02002719 /* Verify that no unknown flags are passed along. */
Christian Brauneref2c41c2020-02-05 14:26:22 +01002720 if (kargs->flags &
2721 ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP))
Christian Brauner7f192e32019-05-25 11:36:41 +02002722 return false;
2723
2724 /*
2725 * - make the CLONE_DETACHED bit reuseable for clone3
2726 * - make the CSIGNAL bits reuseable for clone3
2727 */
2728 if (kargs->flags & (CLONE_DETACHED | CSIGNAL))
2729 return false;
2730
Christian Braunerb612e5d2019-10-14 12:45:37 +02002731 if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) ==
2732 (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND))
2733 return false;
2734
Christian Brauner7f192e32019-05-25 11:36:41 +02002735 if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) &&
2736 kargs->exit_signal)
2737 return false;
2738
Christian Braunerfa729c42019-10-31 12:36:08 +01002739 if (!clone3_stack_valid(kargs))
2740 return false;
2741
Christian Brauner7f192e32019-05-25 11:36:41 +02002742 return true;
2743}
2744
Christian Brauner501bd012019-09-27 17:28:42 +02002745/**
2746 * clone3 - create a new process with specific properties
2747 * @uargs: argument structure
2748 * @size: size of @uargs
2749 *
2750 * clone3() is the extensible successor to clone()/clone2().
2751 * It takes a struct as argument that is versioned by its size.
2752 *
2753 * Return: On success, a positive PID for the child process.
2754 * On error, a negative errno number.
2755 */
Christian Brauner7f192e32019-05-25 11:36:41 +02002756SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size)
2757{
2758 int err;
2759
2760 struct kernel_clone_args kargs;
Adrian Reber49cb2fc2019-11-15 13:36:20 +01002761 pid_t set_tid[MAX_PID_NS_LEVEL];
2762
2763 kargs.set_tid = set_tid;
Christian Brauner7f192e32019-05-25 11:36:41 +02002764
2765 err = copy_clone_args_from_user(&kargs, uargs, size);
2766 if (err)
2767 return err;
2768
2769 if (!clone3_args_valid(&kargs))
2770 return -EINVAL;
2771
2772 return _do_fork(&kargs);
Al Virod2125042012-10-23 13:17:59 -04002773}
2774#endif
2775
Oleg Nesterov0f1b92c2017-01-30 18:06:11 +03002776void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data)
2777{
2778 struct task_struct *leader, *parent, *child;
2779 int res;
2780
2781 read_lock(&tasklist_lock);
2782 leader = top = top->group_leader;
2783down:
2784 for_each_thread(leader, parent) {
2785 list_for_each_entry(child, &parent->children, sibling) {
2786 res = visitor(child, data);
2787 if (res) {
2788 if (res < 0)
2789 goto out;
2790 leader = child;
2791 goto down;
2792 }
2793up:
2794 ;
2795 }
2796 }
2797
2798 if (leader != top) {
2799 child = leader;
2800 parent = child->real_parent;
2801 leader = parent->group_leader;
2802 goto up;
2803 }
2804out:
2805 read_unlock(&tasklist_lock);
2806}
2807
Ravikiran G Thirumalai5fd63b32006-01-11 22:46:15 +01002808#ifndef ARCH_MIN_MMSTRUCT_ALIGN
2809#define ARCH_MIN_MMSTRUCT_ALIGN 0
2810#endif
2811
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002812static void sighand_ctor(void *data)
Oleg Nesterovaa1757f2006-03-28 16:11:12 -08002813{
2814 struct sighand_struct *sighand = data;
2815
Christoph Lametera35afb82007-05-16 22:10:57 -07002816 spin_lock_init(&sighand->siglock);
Davide Libenzib8fceee2007-09-20 12:40:16 -07002817 init_waitqueue_head(&sighand->signalfd_wqh);
Oleg Nesterovaa1757f2006-03-28 16:11:12 -08002818}
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820void __init proc_caches_init(void)
2821{
Rik van Rielc1a2f7f2018-07-16 15:03:31 -04002822 unsigned int mm_size;
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 sighand_cachep = kmem_cache_create("sighand_cache",
2825 sizeof(struct sighand_struct), 0,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08002826 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08002827 SLAB_ACCOUNT, sighand_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 signal_cachep = kmem_cache_create("signal_cache",
2829 sizeof(struct signal_struct), 0,
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08002830 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
Vladimir Davydov5d097052016-01-14 15:18:21 -08002831 NULL);
Paul Mundt20c2df82007-07-20 10:11:58 +09002832 files_cachep = kmem_cache_create("files_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 sizeof(struct files_struct), 0,
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08002834 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
Vladimir Davydov5d097052016-01-14 15:18:21 -08002835 NULL);
Paul Mundt20c2df82007-07-20 10:11:58 +09002836 fs_cachep = kmem_cache_create("fs_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 sizeof(struct fs_struct), 0,
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08002838 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
Vladimir Davydov5d097052016-01-14 15:18:21 -08002839 NULL);
Rik van Rielc1a2f7f2018-07-16 15:03:31 -04002840
Linus Torvalds6345d242011-05-29 11:32:28 -07002841 /*
Rik van Rielc1a2f7f2018-07-16 15:03:31 -04002842 * The mm_cpumask is located at the end of mm_struct, and is
2843 * dynamically sized based on the maximum CPU number this system
2844 * can have, taking hotplug into account (nr_cpu_ids).
Linus Torvalds6345d242011-05-29 11:32:28 -07002845 */
Rik van Rielc1a2f7f2018-07-16 15:03:31 -04002846 mm_size = sizeof(struct mm_struct) + cpumask_size();
2847
David Windsor07dcd7f2017-08-15 16:45:00 -07002848 mm_cachep = kmem_cache_create_usercopy("mm_struct",
Rik van Rielc1a2f7f2018-07-16 15:03:31 -04002849 mm_size, ARCH_MIN_MMSTRUCT_ALIGN,
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08002850 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
David Windsor07dcd7f2017-08-15 16:45:00 -07002851 offsetof(struct mm_struct, saved_auxv),
2852 sizeof_field(struct mm_struct, saved_auxv),
Vladimir Davydov5d097052016-01-14 15:18:21 -08002853 NULL);
2854 vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT);
David Howells8feae132009-01-08 12:04:47 +00002855 mmap_init();
Al Viro66577192011-06-28 15:41:10 -04002856 nsproxy_cache_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002858
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002859/*
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002860 * Check constraints on flags passed to the unshare system call.
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002861 */
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002862static int check_unshare_flags(unsigned long unshare_flags)
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002863{
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002864 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
2865 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
Eric W. Biederman50804fe2010-03-02 15:41:50 -08002866 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
Andrei Vagin769071a2019-11-12 01:26:52 +00002867 CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP|
2868 CLONE_NEWTIME))
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002869 return -EINVAL;
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002870 /*
Eric W. Biederman12c641a2015-08-10 17:35:07 -05002871 * Not implemented, but pretend it works if there is nothing
2872 * to unshare. Note that unsharing the address space or the
2873 * signal handlers also need to unshare the signal queues (aka
2874 * CLONE_THREAD).
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002875 */
2876 if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
Eric W. Biederman12c641a2015-08-10 17:35:07 -05002877 if (!thread_group_empty(current))
2878 return -EINVAL;
2879 }
2880 if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
Elena Reshetovad036bda2019-01-18 14:27:26 +02002881 if (refcount_read(&current->sighand->count) > 1)
Eric W. Biederman12c641a2015-08-10 17:35:07 -05002882 return -EINVAL;
2883 }
2884 if (unshare_flags & CLONE_VM) {
2885 if (!current_is_single_threaded())
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002886 return -EINVAL;
2887 }
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002888
2889 return 0;
2890}
2891
2892/*
JANAK DESAI99d14192006-02-07 12:58:59 -08002893 * Unshare the filesystem structure if it is being shared
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002894 */
2895static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
2896{
2897 struct fs_struct *fs = current->fs;
2898
Al Viro498052b2009-03-30 07:20:30 -04002899 if (!(unshare_flags & CLONE_FS) || !fs)
2900 return 0;
2901
2902 /* don't need lock here; in the worst case we'll do useless copy */
2903 if (fs->users == 1)
2904 return 0;
2905
2906 *new_fsp = copy_fs_struct(fs);
2907 if (!*new_fsp)
2908 return -ENOMEM;
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002909
2910 return 0;
2911}
2912
2913/*
JANAK DESAIa016f332006-02-07 12:59:02 -08002914 * Unshare file descriptor table if it is being shared
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002915 */
2916static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
2917{
2918 struct files_struct *fd = current->files;
JANAK DESAIa016f332006-02-07 12:59:02 -08002919 int error = 0;
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002920
2921 if ((unshare_flags & CLONE_FILES) &&
JANAK DESAIa016f332006-02-07 12:59:02 -08002922 (fd && atomic_read(&fd->count) > 1)) {
2923 *new_fdp = dup_fd(fd, &error);
2924 if (!*new_fdp)
2925 return error;
2926 }
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002927
2928 return 0;
2929}
2930
2931/*
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002932 * unshare allows a process to 'unshare' part of the process
2933 * context which was originally shared using clone. copy_*
2934 * functions used by do_fork() cannot be used here directly
2935 * because they modify an inactive task_struct that is being
2936 * constructed. Here we are modifying the current, active,
2937 * task_struct.
2938 */
Dominik Brodowski9b321052018-03-11 11:34:42 +01002939int ksys_unshare(unsigned long unshare_flags)
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002940{
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002941 struct fs_struct *fs, *new_fs = NULL;
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002942 struct files_struct *fd, *new_fd = NULL;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002943 struct cred *new_cred = NULL;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -07002944 struct nsproxy *new_nsproxy = NULL;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002945 int do_sysvsem = 0;
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002946 int err;
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002947
Eric W. Biederman50804fe2010-03-02 15:41:50 -08002948 /*
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05002949 * If unsharing a user namespace must also unshare the thread group
2950 * and unshare the filesystem root and working directories.
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002951 */
2952 if (unshare_flags & CLONE_NEWUSER)
Eric W. Biedermane66eded2013-03-13 11:51:49 -07002953 unshare_flags |= CLONE_THREAD | CLONE_FS;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002954 /*
Eric W. Biederman50804fe2010-03-02 15:41:50 -08002955 * If unsharing vm, must also unshare signal handlers.
2956 */
2957 if (unshare_flags & CLONE_VM)
2958 unshare_flags |= CLONE_SIGHAND;
Manfred Spraul6013f672008-04-29 01:00:59 -07002959 /*
Eric W. Biederman12c641a2015-08-10 17:35:07 -05002960 * If unsharing a signal handlers, must also unshare the signal queues.
2961 */
2962 if (unshare_flags & CLONE_SIGHAND)
2963 unshare_flags |= CLONE_THREAD;
2964 /*
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002965 * If unsharing namespace, must also unshare filesystem information.
2966 */
2967 if (unshare_flags & CLONE_NEWNS)
2968 unshare_flags |= CLONE_FS;
Eric W. Biederman50804fe2010-03-02 15:41:50 -08002969
2970 err = check_unshare_flags(unshare_flags);
2971 if (err)
2972 goto bad_unshare_out;
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002973 /*
Manfred Spraul6013f672008-04-29 01:00:59 -07002974 * CLONE_NEWIPC must also detach from the undolist: after switching
2975 * to a new ipc namespace, the semaphore arrays from the old
2976 * namespace are unreachable.
2977 */
2978 if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002979 do_sysvsem = 1;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002980 err = unshare_fs(unshare_flags, &new_fs);
2981 if (err)
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002982 goto bad_unshare_out;
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002983 err = unshare_fd(unshare_flags, &new_fd);
2984 if (err)
Oleg Nesterov9bfb23f2011-03-22 16:34:09 -07002985 goto bad_unshare_cleanup_fs;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002986 err = unshare_userns(unshare_flags, &new_cred);
Daniel Rebelo de Oliveirafb0a6852011-07-26 16:08:39 -07002987 if (err)
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002988 goto bad_unshare_cleanup_fd;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002989 err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
2990 new_cred, new_fs);
2991 if (err)
2992 goto bad_unshare_cleanup_cred;
JANAK DESAIcf2e3402006-02-07 12:58:58 -08002993
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07002994 if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002995 if (do_sysvsem) {
2996 /*
2997 * CLONE_SYSVSEM is equivalent to sys_exit().
2998 */
2999 exit_sem(current);
3000 }
Jack Millerab602f72014-08-08 14:23:19 -07003001 if (unshare_flags & CLONE_NEWIPC) {
3002 /* Orphan segments in old ns (see sem above). */
3003 exit_shm(current);
3004 shm_init_task(current);
3005 }
Serge E. Hallynab516012006-10-02 02:18:06 -07003006
Alan Cox6f977e62013-02-27 17:03:23 -08003007 if (new_nsproxy)
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -07003008 switch_task_namespaces(current, new_nsproxy);
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003009
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -07003010 task_lock(current);
3011
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003012 if (new_fs) {
3013 fs = current->fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +10003014 spin_lock(&fs->lock);
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003015 current->fs = new_fs;
Al Viro498052b2009-03-30 07:20:30 -04003016 if (--fs->users)
3017 new_fs = NULL;
3018 else
3019 new_fs = fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +10003020 spin_unlock(&fs->lock);
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003021 }
3022
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003023 if (new_fd) {
3024 fd = current->files;
3025 current->files = new_fd;
3026 new_fd = fd;
3027 }
3028
3029 task_unlock(current);
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07003030
3031 if (new_cred) {
3032 /* Install the new user namespace */
3033 commit_creds(new_cred);
3034 new_cred = NULL;
3035 }
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003036 }
3037
Hari Bathinie4222672017-03-08 02:11:36 +05303038 perf_event_namespaces(current);
3039
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -07003040bad_unshare_cleanup_cred:
3041 if (new_cred)
3042 put_cred(new_cred);
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003043bad_unshare_cleanup_fd:
3044 if (new_fd)
3045 put_files_struct(new_fd);
3046
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003047bad_unshare_cleanup_fs:
3048 if (new_fs)
Al Viro498052b2009-03-30 07:20:30 -04003049 free_fs_struct(new_fs);
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003050
JANAK DESAIcf2e3402006-02-07 12:58:58 -08003051bad_unshare_out:
3052 return err;
3053}
Al Viro3b125382008-04-22 05:31:30 -04003054
Dominik Brodowski9b321052018-03-11 11:34:42 +01003055SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
3056{
3057 return ksys_unshare(unshare_flags);
3058}
3059
Al Viro3b125382008-04-22 05:31:30 -04003060/*
3061 * Helper to unshare the files of the current task.
3062 * We don't want to expose copy_files internals to
3063 * the exec layer of the kernel.
3064 */
3065
3066int unshare_files(struct files_struct **displaced)
3067{
3068 struct task_struct *task = current;
Al Viro50704512008-04-26 05:25:00 +01003069 struct files_struct *copy = NULL;
Al Viro3b125382008-04-22 05:31:30 -04003070 int error;
3071
3072 error = unshare_fd(CLONE_FILES, &copy);
3073 if (error || !copy) {
3074 *displaced = NULL;
3075 return error;
3076 }
3077 *displaced = task->files;
3078 task_lock(task);
3079 task->files = copy;
3080 task_unlock(task);
3081 return 0;
3082}
Heinrich Schuchardt16db3d32015-04-16 12:47:50 -07003083
3084int sysctl_max_threads(struct ctl_table *table, int write,
3085 void __user *buffer, size_t *lenp, loff_t *ppos)
3086{
3087 struct ctl_table t;
3088 int ret;
3089 int threads = max_threads;
Michal Hockob0f53db2019-10-06 17:58:19 -07003090 int min = 1;
Heinrich Schuchardt16db3d32015-04-16 12:47:50 -07003091 int max = MAX_THREADS;
3092
3093 t = *table;
3094 t.data = &threads;
3095 t.extra1 = &min;
3096 t.extra2 = &max;
3097
3098 ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
3099 if (ret || !write)
3100 return ret;
3101
Michal Hockob0f53db2019-10-06 17:58:19 -07003102 max_threads = threads;
Heinrich Schuchardt16db3d32015-04-16 12:47:50 -07003103
3104 return 0;
3105}