xref: /linux/kernel/fork.c (revision acc53a0b4c156877773da6e9eea4113dc7e770ae)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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 
15 #include <linux/anon_inodes.h>
16 #include <linux/slab.h>
17 #include <linux/sched/autogroup.h>
18 #include <linux/sched/mm.h>
19 #include <linux/sched/user.h>
20 #include <linux/sched/numa_balancing.h>
21 #include <linux/sched/stat.h>
22 #include <linux/sched/task.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/sched/cputime.h>
25 #include <linux/sched/ext.h>
26 #include <linux/seq_file.h>
27 #include <linux/rtmutex.h>
28 #include <linux/init.h>
29 #include <linux/unistd.h>
30 #include <linux/module.h>
31 #include <linux/vmalloc.h>
32 #include <linux/completion.h>
33 #include <linux/personality.h>
34 #include <linux/mempolicy.h>
35 #include <linux/sem.h>
36 #include <linux/file.h>
37 #include <linux/fdtable.h>
38 #include <linux/iocontext.h>
39 #include <linux/key.h>
40 #include <linux/kmsan.h>
41 #include <linux/binfmts.h>
42 #include <linux/mman.h>
43 #include <linux/mmu_notifier.h>
44 #include <linux/fs.h>
45 #include <linux/mm.h>
46 #include <linux/mm_inline.h>
47 #include <linux/memblock.h>
48 #include <linux/nsproxy.h>
49 #include <linux/capability.h>
50 #include <linux/cpu.h>
51 #include <linux/cgroup.h>
52 #include <linux/security.h>
53 #include <linux/hugetlb.h>
54 #include <linux/seccomp.h>
55 #include <linux/swap.h>
56 #include <linux/syscalls.h>
57 #include <linux/syscall_user_dispatch.h>
58 #include <linux/jiffies.h>
59 #include <linux/futex.h>
60 #include <linux/compat.h>
61 #include <linux/kthread.h>
62 #include <linux/task_io_accounting_ops.h>
63 #include <linux/rcupdate.h>
64 #include <linux/ptrace.h>
65 #include <linux/mount.h>
66 #include <linux/audit.h>
67 #include <linux/memcontrol.h>
68 #include <linux/ftrace.h>
69 #include <linux/proc_fs.h>
70 #include <linux/profile.h>
71 #include <linux/rmap.h>
72 #include <linux/ksm.h>
73 #include <linux/acct.h>
74 #include <linux/userfaultfd_k.h>
75 #include <linux/tsacct_kern.h>
76 #include <linux/cn_proc.h>
77 #include <linux/freezer.h>
78 #include <linux/delayacct.h>
79 #include <linux/taskstats_kern.h>
80 #include <linux/tty.h>
81 #include <linux/fs_struct.h>
82 #include <linux/magic.h>
83 #include <linux/perf_event.h>
84 #include <linux/posix-timers.h>
85 #include <linux/user-return-notifier.h>
86 #include <linux/oom.h>
87 #include <linux/khugepaged.h>
88 #include <linux/signalfd.h>
89 #include <linux/uprobes.h>
90 #include <linux/aio.h>
91 #include <linux/compiler.h>
92 #include <linux/sysctl.h>
93 #include <linux/kcov.h>
94 #include <linux/livepatch.h>
95 #include <linux/thread_info.h>
96 #include <linux/stackleak.h>
97 #include <linux/kasan.h>
98 #include <linux/scs.h>
99 #include <linux/io_uring.h>
100 #include <linux/bpf.h>
101 #include <linux/stackprotector.h>
102 #include <linux/user_events.h>
103 #include <linux/iommu.h>
104 #include <linux/rseq.h>
105 #include <uapi/linux/pidfd.h>
106 #include <linux/pidfs.h>
107 #include <linux/tick.h>
108 
109 #include <asm/pgalloc.h>
110 #include <linux/uaccess.h>
111 #include <asm/mmu_context.h>
112 #include <asm/cacheflush.h>
113 #include <asm/tlbflush.h>
114 
115 /* For dup_mmap(). */
116 #include "../mm/internal.h"
117 
118 #include <trace/events/sched.h>
119 
120 #define CREATE_TRACE_POINTS
121 #include <trace/events/task.h>
122 
123 #include <kunit/visibility.h>
124 
125 /*
126  * Minimum number of threads to boot the kernel
127  */
128 #define MIN_THREADS 20
129 
130 /*
131  * Maximum number of threads
132  */
133 #define MAX_THREADS FUTEX_TID_MASK
134 
135 /*
136  * Protected counters by write_lock_irq(&tasklist_lock)
137  */
138 unsigned long total_forks;	/* Handle normal Linux uptimes. */
139 int nr_threads;			/* The idle threads do not count.. */
140 
141 static int max_threads;		/* tunable limit on nr_threads */
142 
143 #define NAMED_ARRAY_INDEX(x)	[x] = __stringify(x)
144 
145 static const char * const resident_page_types[] = {
146 	NAMED_ARRAY_INDEX(MM_FILEPAGES),
147 	NAMED_ARRAY_INDEX(MM_ANONPAGES),
148 	NAMED_ARRAY_INDEX(MM_SWAPENTS),
149 	NAMED_ARRAY_INDEX(MM_SHMEMPAGES),
150 };
151 
152 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
153 
154 __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
155 
156 #ifdef CONFIG_PROVE_RCU
157 int lockdep_tasklist_lock_is_held(void)
158 {
159 	return lockdep_is_held(&tasklist_lock);
160 }
161 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
162 #endif /* #ifdef CONFIG_PROVE_RCU */
163 
164 int nr_processes(void)
165 {
166 	int cpu;
167 	int total = 0;
168 
169 	for_each_possible_cpu(cpu)
170 		total += per_cpu(process_counts, cpu);
171 
172 	return total;
173 }
174 
175 void __weak arch_release_task_struct(struct task_struct *tsk)
176 {
177 }
178 
179 static struct kmem_cache *task_struct_cachep;
180 
181 static inline struct task_struct *alloc_task_struct_node(int node)
182 {
183 	return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
184 }
185 
186 static inline void free_task_struct(struct task_struct *tsk)
187 {
188 	kmem_cache_free(task_struct_cachep, tsk);
189 }
190 
191 /*
192  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
193  * kmemcache based allocator.
194  */
195 # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
196 
197 #  ifdef CONFIG_VMAP_STACK
198 /*
199  * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB
200  * flush.  Try to minimize the number of calls by caching stacks.
201  */
202 #define NR_CACHED_STACKS 2
203 static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]);
204 
205 struct vm_stack {
206 	struct rcu_head rcu;
207 	struct vm_struct *stack_vm_area;
208 };
209 
210 static bool try_release_thread_stack_to_cache(struct vm_struct *vm)
211 {
212 	unsigned int i;
213 
214 	for (i = 0; i < NR_CACHED_STACKS; i++) {
215 		struct vm_struct *tmp = NULL;
216 
217 		if (this_cpu_try_cmpxchg(cached_stacks[i], &tmp, vm))
218 			return true;
219 	}
220 	return false;
221 }
222 
223 static void thread_stack_free_rcu(struct rcu_head *rh)
224 {
225 	struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu);
226 
227 	if (try_release_thread_stack_to_cache(vm_stack->stack_vm_area))
228 		return;
229 
230 	vfree(vm_stack);
231 }
232 
233 static void thread_stack_delayed_free(struct task_struct *tsk)
234 {
235 	struct vm_stack *vm_stack = tsk->stack;
236 
237 	vm_stack->stack_vm_area = tsk->stack_vm_area;
238 	call_rcu(&vm_stack->rcu, thread_stack_free_rcu);
239 }
240 
241 static int free_vm_stack_cache(unsigned int cpu)
242 {
243 	struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu);
244 	int i;
245 
246 	for (i = 0; i < NR_CACHED_STACKS; i++) {
247 		struct vm_struct *vm_stack = cached_vm_stacks[i];
248 
249 		if (!vm_stack)
250 			continue;
251 
252 		vfree(vm_stack->addr);
253 		cached_vm_stacks[i] = NULL;
254 	}
255 
256 	return 0;
257 }
258 
259 static int memcg_charge_kernel_stack(struct vm_struct *vm)
260 {
261 	int i;
262 	int ret;
263 	int nr_charged = 0;
264 
265 	BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
266 
267 	for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
268 		ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 0);
269 		if (ret)
270 			goto err;
271 		nr_charged++;
272 	}
273 	return 0;
274 err:
275 	for (i = 0; i < nr_charged; i++)
276 		memcg_kmem_uncharge_page(vm->pages[i], 0);
277 	return ret;
278 }
279 
280 static int alloc_thread_stack_node(struct task_struct *tsk, int node)
281 {
282 	struct vm_struct *vm;
283 	void *stack;
284 	int i;
285 
286 	for (i = 0; i < NR_CACHED_STACKS; i++) {
287 		struct vm_struct *s;
288 
289 		s = this_cpu_xchg(cached_stacks[i], NULL);
290 
291 		if (!s)
292 			continue;
293 
294 		/* Reset stack metadata. */
295 		kasan_unpoison_range(s->addr, THREAD_SIZE);
296 
297 		stack = kasan_reset_tag(s->addr);
298 
299 		/* Clear stale pointers from reused stack. */
300 		memset(stack, 0, THREAD_SIZE);
301 
302 		if (memcg_charge_kernel_stack(s)) {
303 			vfree(s->addr);
304 			return -ENOMEM;
305 		}
306 
307 		tsk->stack_vm_area = s;
308 		tsk->stack = stack;
309 		return 0;
310 	}
311 
312 	/*
313 	 * Allocated stacks are cached and later reused by new threads,
314 	 * so memcg accounting is performed manually on assigning/releasing
315 	 * stacks to tasks. Drop __GFP_ACCOUNT.
316 	 */
317 	stack = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN,
318 				     THREADINFO_GFP & ~__GFP_ACCOUNT,
319 				     node, __builtin_return_address(0));
320 	if (!stack)
321 		return -ENOMEM;
322 
323 	vm = find_vm_area(stack);
324 	if (memcg_charge_kernel_stack(vm)) {
325 		vfree(stack);
326 		return -ENOMEM;
327 	}
328 	/*
329 	 * We can't call find_vm_area() in interrupt context, and
330 	 * free_thread_stack() can be called in interrupt context,
331 	 * so cache the vm_struct.
332 	 */
333 	tsk->stack_vm_area = vm;
334 	stack = kasan_reset_tag(stack);
335 	tsk->stack = stack;
336 	return 0;
337 }
338 
339 static void free_thread_stack(struct task_struct *tsk)
340 {
341 	if (!try_release_thread_stack_to_cache(tsk->stack_vm_area))
342 		thread_stack_delayed_free(tsk);
343 
344 	tsk->stack = NULL;
345 	tsk->stack_vm_area = NULL;
346 }
347 
348 #  else /* !CONFIG_VMAP_STACK */
349 
350 static void thread_stack_free_rcu(struct rcu_head *rh)
351 {
352 	__free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
353 }
354 
355 static void thread_stack_delayed_free(struct task_struct *tsk)
356 {
357 	struct rcu_head *rh = tsk->stack;
358 
359 	call_rcu(rh, thread_stack_free_rcu);
360 }
361 
362 static int alloc_thread_stack_node(struct task_struct *tsk, int node)
363 {
364 	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
365 					     THREAD_SIZE_ORDER);
366 
367 	if (likely(page)) {
368 		tsk->stack = kasan_reset_tag(page_address(page));
369 		return 0;
370 	}
371 	return -ENOMEM;
372 }
373 
374 static void free_thread_stack(struct task_struct *tsk)
375 {
376 	thread_stack_delayed_free(tsk);
377 	tsk->stack = NULL;
378 }
379 
380 #  endif /* CONFIG_VMAP_STACK */
381 # else /* !(THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)) */
382 
383 static struct kmem_cache *thread_stack_cache;
384 
385 static void thread_stack_free_rcu(struct rcu_head *rh)
386 {
387 	kmem_cache_free(thread_stack_cache, rh);
388 }
389 
390 static void thread_stack_delayed_free(struct task_struct *tsk)
391 {
392 	struct rcu_head *rh = tsk->stack;
393 
394 	call_rcu(rh, thread_stack_free_rcu);
395 }
396 
397 static int alloc_thread_stack_node(struct task_struct *tsk, int node)
398 {
399 	unsigned long *stack;
400 	stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
401 	stack = kasan_reset_tag(stack);
402 	tsk->stack = stack;
403 	return stack ? 0 : -ENOMEM;
404 }
405 
406 static void free_thread_stack(struct task_struct *tsk)
407 {
408 	thread_stack_delayed_free(tsk);
409 	tsk->stack = NULL;
410 }
411 
412 void thread_stack_cache_init(void)
413 {
414 	thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
415 					THREAD_SIZE, THREAD_SIZE, 0, 0,
416 					THREAD_SIZE, NULL);
417 	BUG_ON(thread_stack_cache == NULL);
418 }
419 
420 # endif /* THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) */
421 
422 /* SLAB cache for signal_struct structures (tsk->signal) */
423 static struct kmem_cache *signal_cachep;
424 
425 /* SLAB cache for sighand_struct structures (tsk->sighand) */
426 struct kmem_cache *sighand_cachep;
427 
428 /* SLAB cache for files_struct structures (tsk->files) */
429 struct kmem_cache *files_cachep;
430 
431 /* SLAB cache for fs_struct structures (tsk->fs) */
432 struct kmem_cache *fs_cachep;
433 
434 /* SLAB cache for mm_struct structures (tsk->mm) */
435 static struct kmem_cache *mm_cachep;
436 
437 static void account_kernel_stack(struct task_struct *tsk, int account)
438 {
439 	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
440 		struct vm_struct *vm = task_stack_vm_area(tsk);
441 		int i;
442 
443 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
444 			mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB,
445 					      account * (PAGE_SIZE / 1024));
446 	} else {
447 		void *stack = task_stack_page(tsk);
448 
449 		/* All stack pages are in the same node. */
450 		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
451 				      account * (THREAD_SIZE / 1024));
452 	}
453 }
454 
455 void exit_task_stack_account(struct task_struct *tsk)
456 {
457 	account_kernel_stack(tsk, -1);
458 
459 	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
460 		struct vm_struct *vm;
461 		int i;
462 
463 		vm = task_stack_vm_area(tsk);
464 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
465 			memcg_kmem_uncharge_page(vm->pages[i], 0);
466 	}
467 }
468 
469 static void release_task_stack(struct task_struct *tsk)
470 {
471 	if (WARN_ON(READ_ONCE(tsk->__state) != TASK_DEAD))
472 		return;  /* Better to leak the stack than to free prematurely */
473 
474 	free_thread_stack(tsk);
475 }
476 
477 #ifdef CONFIG_THREAD_INFO_IN_TASK
478 void put_task_stack(struct task_struct *tsk)
479 {
480 	if (refcount_dec_and_test(&tsk->stack_refcount))
481 		release_task_stack(tsk);
482 }
483 #endif
484 
485 void free_task(struct task_struct *tsk)
486 {
487 #ifdef CONFIG_SECCOMP
488 	WARN_ON_ONCE(tsk->seccomp.filter);
489 #endif
490 	release_user_cpus_ptr(tsk);
491 	scs_release(tsk);
492 
493 #ifndef CONFIG_THREAD_INFO_IN_TASK
494 	/*
495 	 * The task is finally done with both the stack and thread_info,
496 	 * so free both.
497 	 */
498 	release_task_stack(tsk);
499 #else
500 	/*
501 	 * If the task had a separate stack allocation, it should be gone
502 	 * by now.
503 	 */
504 	WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0);
505 #endif
506 	rt_mutex_debug_task_free(tsk);
507 	ftrace_graph_exit_task(tsk);
508 	arch_release_task_struct(tsk);
509 	if (tsk->flags & PF_KTHREAD)
510 		free_kthread_struct(tsk);
511 	bpf_task_storage_free(tsk);
512 	free_task_struct(tsk);
513 }
514 EXPORT_SYMBOL(free_task);
515 
516 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm)
517 {
518 	struct file *exe_file;
519 
520 	exe_file = get_mm_exe_file(oldmm);
521 	RCU_INIT_POINTER(mm->exe_file, exe_file);
522 	/*
523 	 * We depend on the oldmm having properly denied write access to the
524 	 * exe_file already.
525 	 */
526 	if (exe_file && exe_file_deny_write_access(exe_file))
527 		pr_warn_once("exe_file_deny_write_access() failed in %s\n", __func__);
528 }
529 
530 #ifdef CONFIG_MMU
531 static inline int mm_alloc_pgd(struct mm_struct *mm)
532 {
533 	mm->pgd = pgd_alloc(mm);
534 	if (unlikely(!mm->pgd))
535 		return -ENOMEM;
536 	return 0;
537 }
538 
539 static inline void mm_free_pgd(struct mm_struct *mm)
540 {
541 	pgd_free(mm, mm->pgd);
542 }
543 #else
544 #define mm_alloc_pgd(mm)	(0)
545 #define mm_free_pgd(mm)
546 #endif /* CONFIG_MMU */
547 
548 #ifdef CONFIG_MM_ID
549 static DEFINE_IDA(mm_ida);
550 
551 static inline int mm_alloc_id(struct mm_struct *mm)
552 {
553 	int ret;
554 
555 	ret = ida_alloc_range(&mm_ida, MM_ID_MIN, MM_ID_MAX, GFP_KERNEL);
556 	if (ret < 0)
557 		return ret;
558 	mm->mm_id = ret;
559 	return 0;
560 }
561 
562 static inline void mm_free_id(struct mm_struct *mm)
563 {
564 	const mm_id_t id = mm->mm_id;
565 
566 	mm->mm_id = MM_ID_DUMMY;
567 	if (id == MM_ID_DUMMY)
568 		return;
569 	if (WARN_ON_ONCE(id < MM_ID_MIN || id > MM_ID_MAX))
570 		return;
571 	ida_free(&mm_ida, id);
572 }
573 #else /* !CONFIG_MM_ID */
574 static inline int mm_alloc_id(struct mm_struct *mm) { return 0; }
575 static inline void mm_free_id(struct mm_struct *mm) {}
576 #endif /* CONFIG_MM_ID */
577 
578 static void check_mm(struct mm_struct *mm)
579 {
580 	int i;
581 
582 	BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS,
583 			 "Please make sure 'struct resident_page_types[]' is updated as well");
584 
585 	for (i = 0; i < NR_MM_COUNTERS; i++) {
586 		long x = percpu_counter_sum(&mm->rss_stat[i]);
587 
588 		if (unlikely(x))
589 			pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n",
590 				 mm, resident_page_types[i], x);
591 	}
592 
593 	if (mm_pgtables_bytes(mm))
594 		pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
595 				mm_pgtables_bytes(mm));
596 
597 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
598 	VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
599 #endif
600 }
601 
602 #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
603 #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
604 
605 static void do_check_lazy_tlb(void *arg)
606 {
607 	struct mm_struct *mm = arg;
608 
609 	WARN_ON_ONCE(current->active_mm == mm);
610 }
611 
612 static void do_shoot_lazy_tlb(void *arg)
613 {
614 	struct mm_struct *mm = arg;
615 
616 	if (current->active_mm == mm) {
617 		WARN_ON_ONCE(current->mm);
618 		current->active_mm = &init_mm;
619 		switch_mm(mm, &init_mm, current);
620 	}
621 }
622 
623 static void cleanup_lazy_tlbs(struct mm_struct *mm)
624 {
625 	if (!IS_ENABLED(CONFIG_MMU_LAZY_TLB_SHOOTDOWN)) {
626 		/*
627 		 * In this case, lazy tlb mms are refounted and would not reach
628 		 * __mmdrop until all CPUs have switched away and mmdrop()ed.
629 		 */
630 		return;
631 	}
632 
633 	/*
634 	 * Lazy mm shootdown does not refcount "lazy tlb mm" usage, rather it
635 	 * requires lazy mm users to switch to another mm when the refcount
636 	 * drops to zero, before the mm is freed. This requires IPIs here to
637 	 * switch kernel threads to init_mm.
638 	 *
639 	 * archs that use IPIs to flush TLBs can piggy-back that lazy tlb mm
640 	 * switch with the final userspace teardown TLB flush which leaves the
641 	 * mm lazy on this CPU but no others, reducing the need for additional
642 	 * IPIs here. There are cases where a final IPI is still required here,
643 	 * such as the final mmdrop being performed on a different CPU than the
644 	 * one exiting, or kernel threads using the mm when userspace exits.
645 	 *
646 	 * IPI overheads have not found to be expensive, but they could be
647 	 * reduced in a number of possible ways, for example (roughly
648 	 * increasing order of complexity):
649 	 * - The last lazy reference created by exit_mm() could instead switch
650 	 *   to init_mm, however it's probable this will run on the same CPU
651 	 *   immediately afterwards, so this may not reduce IPIs much.
652 	 * - A batch of mms requiring IPIs could be gathered and freed at once.
653 	 * - CPUs store active_mm where it can be remotely checked without a
654 	 *   lock, to filter out false-positives in the cpumask.
655 	 * - After mm_users or mm_count reaches zero, switching away from the
656 	 *   mm could clear mm_cpumask to reduce some IPIs, perhaps together
657 	 *   with some batching or delaying of the final IPIs.
658 	 * - A delayed freeing and RCU-like quiescing sequence based on mm
659 	 *   switching to avoid IPIs completely.
660 	 */
661 	on_each_cpu_mask(mm_cpumask(mm), do_shoot_lazy_tlb, (void *)mm, 1);
662 	if (IS_ENABLED(CONFIG_DEBUG_VM_SHOOT_LAZIES))
663 		on_each_cpu(do_check_lazy_tlb, (void *)mm, 1);
664 }
665 
666 /*
667  * Called when the last reference to the mm
668  * is dropped: either by a lazy thread or by
669  * mmput. Free the page directory and the mm.
670  */
671 void __mmdrop(struct mm_struct *mm)
672 {
673 	BUG_ON(mm == &init_mm);
674 	WARN_ON_ONCE(mm == current->mm);
675 
676 	/* Ensure no CPUs are using this as their lazy tlb mm */
677 	cleanup_lazy_tlbs(mm);
678 
679 	WARN_ON_ONCE(mm == current->active_mm);
680 	mm_free_pgd(mm);
681 	mm_free_id(mm);
682 	destroy_context(mm);
683 	mmu_notifier_subscriptions_destroy(mm);
684 	check_mm(mm);
685 	put_user_ns(mm->user_ns);
686 	mm_pasid_drop(mm);
687 	mm_destroy_cid(mm);
688 	percpu_counter_destroy_many(mm->rss_stat, NR_MM_COUNTERS);
689 
690 	free_mm(mm);
691 }
692 EXPORT_SYMBOL_GPL(__mmdrop);
693 
694 static void mmdrop_async_fn(struct work_struct *work)
695 {
696 	struct mm_struct *mm;
697 
698 	mm = container_of(work, struct mm_struct, async_put_work);
699 	__mmdrop(mm);
700 }
701 
702 static void mmdrop_async(struct mm_struct *mm)
703 {
704 	if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
705 		INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
706 		schedule_work(&mm->async_put_work);
707 	}
708 }
709 
710 static inline void free_signal_struct(struct signal_struct *sig)
711 {
712 	taskstats_tgid_free(sig);
713 	sched_autogroup_exit(sig);
714 	/*
715 	 * __mmdrop is not safe to call from softirq context on x86 due to
716 	 * pgd_dtor so postpone it to the async context
717 	 */
718 	if (sig->oom_mm)
719 		mmdrop_async(sig->oom_mm);
720 	kmem_cache_free(signal_cachep, sig);
721 }
722 
723 static inline void put_signal_struct(struct signal_struct *sig)
724 {
725 	if (refcount_dec_and_test(&sig->sigcnt))
726 		free_signal_struct(sig);
727 }
728 
729 void __put_task_struct(struct task_struct *tsk)
730 {
731 	WARN_ON(!tsk->exit_state);
732 	WARN_ON(refcount_read(&tsk->usage));
733 	WARN_ON(tsk == current);
734 
735 	sched_ext_free(tsk);
736 	io_uring_free(tsk);
737 	cgroup_free(tsk);
738 	task_numa_free(tsk, true);
739 	security_task_free(tsk);
740 	exit_creds(tsk);
741 	delayacct_tsk_free(tsk);
742 	put_signal_struct(tsk->signal);
743 	sched_core_free(tsk);
744 	free_task(tsk);
745 }
746 EXPORT_SYMBOL_GPL(__put_task_struct);
747 
748 void __put_task_struct_rcu_cb(struct rcu_head *rhp)
749 {
750 	struct task_struct *task = container_of(rhp, struct task_struct, rcu);
751 
752 	__put_task_struct(task);
753 }
754 EXPORT_SYMBOL_GPL(__put_task_struct_rcu_cb);
755 
756 void __init __weak arch_task_cache_init(void) { }
757 
758 /*
759  * set_max_threads
760  */
761 static void __init set_max_threads(unsigned int max_threads_suggested)
762 {
763 	u64 threads;
764 	unsigned long nr_pages = memblock_estimated_nr_free_pages();
765 
766 	/*
767 	 * The number of threads shall be limited such that the thread
768 	 * structures may only consume a small part of the available memory.
769 	 */
770 	if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64)
771 		threads = MAX_THREADS;
772 	else
773 		threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE,
774 				    (u64) THREAD_SIZE * 8UL);
775 
776 	if (threads > max_threads_suggested)
777 		threads = max_threads_suggested;
778 
779 	max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
780 }
781 
782 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
783 /* Initialized by the architecture: */
784 int arch_task_struct_size __read_mostly;
785 #endif
786 
787 static void __init task_struct_whitelist(unsigned long *offset, unsigned long *size)
788 {
789 	/* Fetch thread_struct whitelist for the architecture. */
790 	arch_thread_struct_whitelist(offset, size);
791 
792 	/*
793 	 * Handle zero-sized whitelist or empty thread_struct, otherwise
794 	 * adjust offset to position of thread_struct in task_struct.
795 	 */
796 	if (unlikely(*size == 0))
797 		*offset = 0;
798 	else
799 		*offset += offsetof(struct task_struct, thread);
800 }
801 
802 void __init fork_init(void)
803 {
804 	int i;
805 #ifndef ARCH_MIN_TASKALIGN
806 #define ARCH_MIN_TASKALIGN	0
807 #endif
808 	int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN);
809 	unsigned long useroffset, usersize;
810 
811 	/* create a slab on which task_structs can be allocated */
812 	task_struct_whitelist(&useroffset, &usersize);
813 	task_struct_cachep = kmem_cache_create_usercopy("task_struct",
814 			arch_task_struct_size, align,
815 			SLAB_PANIC|SLAB_ACCOUNT,
816 			useroffset, usersize, NULL);
817 
818 	/* do the arch specific task caches init */
819 	arch_task_cache_init();
820 
821 	set_max_threads(MAX_THREADS);
822 
823 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
824 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
825 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
826 		init_task.signal->rlim[RLIMIT_NPROC];
827 
828 	for (i = 0; i < UCOUNT_COUNTS; i++)
829 		init_user_ns.ucount_max[i] = max_threads/2;
830 
831 	set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_NPROC,      RLIM_INFINITY);
832 	set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MSGQUEUE,   RLIM_INFINITY);
833 	set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_SIGPENDING, RLIM_INFINITY);
834 	set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MEMLOCK,    RLIM_INFINITY);
835 
836 #ifdef CONFIG_VMAP_STACK
837 	cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
838 			  NULL, free_vm_stack_cache);
839 #endif
840 
841 	scs_init();
842 
843 	lockdep_init_task(&init_task);
844 	uprobes_init();
845 }
846 
847 int __weak arch_dup_task_struct(struct task_struct *dst,
848 					       struct task_struct *src)
849 {
850 	*dst = *src;
851 	return 0;
852 }
853 
854 void set_task_stack_end_magic(struct task_struct *tsk)
855 {
856 	unsigned long *stackend;
857 
858 	stackend = end_of_stack(tsk);
859 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
860 }
861 
862 static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
863 {
864 	struct task_struct *tsk;
865 	int err;
866 
867 	if (node == NUMA_NO_NODE)
868 		node = tsk_fork_get_node(orig);
869 	tsk = alloc_task_struct_node(node);
870 	if (!tsk)
871 		return NULL;
872 
873 	err = arch_dup_task_struct(tsk, orig);
874 	if (err)
875 		goto free_tsk;
876 
877 	err = alloc_thread_stack_node(tsk, node);
878 	if (err)
879 		goto free_tsk;
880 
881 #ifdef CONFIG_THREAD_INFO_IN_TASK
882 	refcount_set(&tsk->stack_refcount, 1);
883 #endif
884 	account_kernel_stack(tsk, 1);
885 
886 	err = scs_prepare(tsk, node);
887 	if (err)
888 		goto free_stack;
889 
890 #ifdef CONFIG_SECCOMP
891 	/*
892 	 * We must handle setting up seccomp filters once we're under
893 	 * the sighand lock in case orig has changed between now and
894 	 * then. Until then, filter must be NULL to avoid messing up
895 	 * the usage counts on the error path calling free_task.
896 	 */
897 	tsk->seccomp.filter = NULL;
898 #endif
899 
900 	setup_thread_stack(tsk, orig);
901 	clear_user_return_notifier(tsk);
902 	clear_tsk_need_resched(tsk);
903 	set_task_stack_end_magic(tsk);
904 	clear_syscall_work_syscall_user_dispatch(tsk);
905 
906 #ifdef CONFIG_STACKPROTECTOR
907 	tsk->stack_canary = get_random_canary();
908 #endif
909 	if (orig->cpus_ptr == &orig->cpus_mask)
910 		tsk->cpus_ptr = &tsk->cpus_mask;
911 	dup_user_cpus_ptr(tsk, orig, node);
912 
913 	/*
914 	 * One for the user space visible state that goes away when reaped.
915 	 * One for the scheduler.
916 	 */
917 	refcount_set(&tsk->rcu_users, 2);
918 	/* One for the rcu users */
919 	refcount_set(&tsk->usage, 1);
920 #ifdef CONFIG_BLK_DEV_IO_TRACE
921 	tsk->btrace_seq = 0;
922 #endif
923 	tsk->splice_pipe = NULL;
924 	tsk->task_frag.page = NULL;
925 	tsk->wake_q.next = NULL;
926 	tsk->worker_private = NULL;
927 
928 	kcov_task_init(tsk);
929 	kmsan_task_create(tsk);
930 	kmap_local_fork(tsk);
931 
932 #ifdef CONFIG_FAULT_INJECTION
933 	tsk->fail_nth = 0;
934 #endif
935 
936 #ifdef CONFIG_BLK_CGROUP
937 	tsk->throttle_disk = NULL;
938 	tsk->use_memdelay = 0;
939 #endif
940 
941 #ifdef CONFIG_ARCH_HAS_CPU_PASID
942 	tsk->pasid_activated = 0;
943 #endif
944 
945 #ifdef CONFIG_MEMCG
946 	tsk->active_memcg = NULL;
947 #endif
948 
949 #ifdef CONFIG_X86_BUS_LOCK_DETECT
950 	tsk->reported_split_lock = 0;
951 #endif
952 
953 #ifdef CONFIG_SCHED_MM_CID
954 	tsk->mm_cid = -1;
955 	tsk->last_mm_cid = -1;
956 	tsk->mm_cid_active = 0;
957 	tsk->migrate_from_cpu = -1;
958 #endif
959 	return tsk;
960 
961 free_stack:
962 	exit_task_stack_account(tsk);
963 	free_thread_stack(tsk);
964 free_tsk:
965 	free_task_struct(tsk);
966 	return NULL;
967 }
968 
969 __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
970 
971 static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
972 
973 static int __init coredump_filter_setup(char *s)
974 {
975 	default_dump_filter =
976 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
977 		MMF_DUMP_FILTER_MASK;
978 	return 1;
979 }
980 
981 __setup("coredump_filter=", coredump_filter_setup);
982 
983 #include <linux/init_task.h>
984 
985 static void mm_init_aio(struct mm_struct *mm)
986 {
987 #ifdef CONFIG_AIO
988 	spin_lock_init(&mm->ioctx_lock);
989 	mm->ioctx_table = NULL;
990 #endif
991 }
992 
993 static __always_inline void mm_clear_owner(struct mm_struct *mm,
994 					   struct task_struct *p)
995 {
996 #ifdef CONFIG_MEMCG
997 	if (mm->owner == p)
998 		WRITE_ONCE(mm->owner, NULL);
999 #endif
1000 }
1001 
1002 static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
1003 {
1004 #ifdef CONFIG_MEMCG
1005 	mm->owner = p;
1006 #endif
1007 }
1008 
1009 static void mm_init_uprobes_state(struct mm_struct *mm)
1010 {
1011 #ifdef CONFIG_UPROBES
1012 	mm->uprobes_state.xol_area = NULL;
1013 #endif
1014 }
1015 
1016 static void mmap_init_lock(struct mm_struct *mm)
1017 {
1018 	init_rwsem(&mm->mmap_lock);
1019 	mm_lock_seqcount_init(mm);
1020 #ifdef CONFIG_PER_VMA_LOCK
1021 	rcuwait_init(&mm->vma_writer_wait);
1022 #endif
1023 }
1024 
1025 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
1026 	struct user_namespace *user_ns)
1027 {
1028 	mt_init_flags(&mm->mm_mt, MM_MT_FLAGS);
1029 	mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock);
1030 	atomic_set(&mm->mm_users, 1);
1031 	atomic_set(&mm->mm_count, 1);
1032 	seqcount_init(&mm->write_protect_seq);
1033 	mmap_init_lock(mm);
1034 	INIT_LIST_HEAD(&mm->mmlist);
1035 	mm_pgtables_bytes_init(mm);
1036 	mm->map_count = 0;
1037 	mm->locked_vm = 0;
1038 	atomic64_set(&mm->pinned_vm, 0);
1039 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
1040 	spin_lock_init(&mm->page_table_lock);
1041 	spin_lock_init(&mm->arg_lock);
1042 	mm_init_cpumask(mm);
1043 	mm_init_aio(mm);
1044 	mm_init_owner(mm, p);
1045 	mm_pasid_init(mm);
1046 	RCU_INIT_POINTER(mm->exe_file, NULL);
1047 	mmu_notifier_subscriptions_init(mm);
1048 	init_tlb_flush_pending(mm);
1049 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
1050 	mm->pmd_huge_pte = NULL;
1051 #endif
1052 	mm_init_uprobes_state(mm);
1053 	hugetlb_count_init(mm);
1054 
1055 	if (current->mm) {
1056 		mm->flags = mmf_init_flags(current->mm->flags);
1057 		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
1058 	} else {
1059 		mm->flags = default_dump_filter;
1060 		mm->def_flags = 0;
1061 	}
1062 
1063 	if (mm_alloc_pgd(mm))
1064 		goto fail_nopgd;
1065 
1066 	if (mm_alloc_id(mm))
1067 		goto fail_noid;
1068 
1069 	if (init_new_context(p, mm))
1070 		goto fail_nocontext;
1071 
1072 	if (mm_alloc_cid(mm, p))
1073 		goto fail_cid;
1074 
1075 	if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT,
1076 				     NR_MM_COUNTERS))
1077 		goto fail_pcpu;
1078 
1079 	mm->user_ns = get_user_ns(user_ns);
1080 	lru_gen_init_mm(mm);
1081 	return mm;
1082 
1083 fail_pcpu:
1084 	mm_destroy_cid(mm);
1085 fail_cid:
1086 	destroy_context(mm);
1087 fail_nocontext:
1088 	mm_free_id(mm);
1089 fail_noid:
1090 	mm_free_pgd(mm);
1091 fail_nopgd:
1092 	free_mm(mm);
1093 	return NULL;
1094 }
1095 
1096 /*
1097  * Allocate and initialize an mm_struct.
1098  */
1099 struct mm_struct *mm_alloc(void)
1100 {
1101 	struct mm_struct *mm;
1102 
1103 	mm = allocate_mm();
1104 	if (!mm)
1105 		return NULL;
1106 
1107 	memset(mm, 0, sizeof(*mm));
1108 	return mm_init(mm, current, current_user_ns());
1109 }
1110 EXPORT_SYMBOL_IF_KUNIT(mm_alloc);
1111 
1112 static inline void __mmput(struct mm_struct *mm)
1113 {
1114 	VM_BUG_ON(atomic_read(&mm->mm_users));
1115 
1116 	uprobe_clear_state(mm);
1117 	exit_aio(mm);
1118 	ksm_exit(mm);
1119 	khugepaged_exit(mm); /* must run before exit_mmap */
1120 	exit_mmap(mm);
1121 	mm_put_huge_zero_folio(mm);
1122 	set_mm_exe_file(mm, NULL);
1123 	if (!list_empty(&mm->mmlist)) {
1124 		spin_lock(&mmlist_lock);
1125 		list_del(&mm->mmlist);
1126 		spin_unlock(&mmlist_lock);
1127 	}
1128 	if (mm->binfmt)
1129 		module_put(mm->binfmt->module);
1130 	lru_gen_del_mm(mm);
1131 	mmdrop(mm);
1132 }
1133 
1134 /*
1135  * Decrement the use count and release all resources for an mm.
1136  */
1137 void mmput(struct mm_struct *mm)
1138 {
1139 	might_sleep();
1140 
1141 	if (atomic_dec_and_test(&mm->mm_users))
1142 		__mmput(mm);
1143 }
1144 EXPORT_SYMBOL_GPL(mmput);
1145 
1146 #ifdef CONFIG_MMU
1147 static void mmput_async_fn(struct work_struct *work)
1148 {
1149 	struct mm_struct *mm = container_of(work, struct mm_struct,
1150 					    async_put_work);
1151 
1152 	__mmput(mm);
1153 }
1154 
1155 void mmput_async(struct mm_struct *mm)
1156 {
1157 	if (atomic_dec_and_test(&mm->mm_users)) {
1158 		INIT_WORK(&mm->async_put_work, mmput_async_fn);
1159 		schedule_work(&mm->async_put_work);
1160 	}
1161 }
1162 EXPORT_SYMBOL_GPL(mmput_async);
1163 #endif
1164 
1165 /**
1166  * set_mm_exe_file - change a reference to the mm's executable file
1167  * @mm: The mm to change.
1168  * @new_exe_file: The new file to use.
1169  *
1170  * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
1171  *
1172  * Main users are mmput() and sys_execve(). Callers prevent concurrent
1173  * invocations: in mmput() nobody alive left, in execve it happens before
1174  * the new mm is made visible to anyone.
1175  *
1176  * Can only fail if new_exe_file != NULL.
1177  */
1178 int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1179 {
1180 	struct file *old_exe_file;
1181 
1182 	/*
1183 	 * It is safe to dereference the exe_file without RCU as
1184 	 * this function is only called if nobody else can access
1185 	 * this mm -- see comment above for justification.
1186 	 */
1187 	old_exe_file = rcu_dereference_raw(mm->exe_file);
1188 
1189 	if (new_exe_file) {
1190 		/*
1191 		 * We expect the caller (i.e., sys_execve) to already denied
1192 		 * write access, so this is unlikely to fail.
1193 		 */
1194 		if (unlikely(exe_file_deny_write_access(new_exe_file)))
1195 			return -EACCES;
1196 		get_file(new_exe_file);
1197 	}
1198 	rcu_assign_pointer(mm->exe_file, new_exe_file);
1199 	if (old_exe_file) {
1200 		exe_file_allow_write_access(old_exe_file);
1201 		fput(old_exe_file);
1202 	}
1203 	return 0;
1204 }
1205 
1206 /**
1207  * replace_mm_exe_file - replace a reference to the mm's executable file
1208  * @mm: The mm to change.
1209  * @new_exe_file: The new file to use.
1210  *
1211  * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
1212  *
1213  * Main user is sys_prctl(PR_SET_MM_MAP/EXE_FILE).
1214  */
1215 int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1216 {
1217 	struct vm_area_struct *vma;
1218 	struct file *old_exe_file;
1219 	int ret = 0;
1220 
1221 	/* Forbid mm->exe_file change if old file still mapped. */
1222 	old_exe_file = get_mm_exe_file(mm);
1223 	if (old_exe_file) {
1224 		VMA_ITERATOR(vmi, mm, 0);
1225 		mmap_read_lock(mm);
1226 		for_each_vma(vmi, vma) {
1227 			if (!vma->vm_file)
1228 				continue;
1229 			if (path_equal(&vma->vm_file->f_path,
1230 				       &old_exe_file->f_path)) {
1231 				ret = -EBUSY;
1232 				break;
1233 			}
1234 		}
1235 		mmap_read_unlock(mm);
1236 		fput(old_exe_file);
1237 		if (ret)
1238 			return ret;
1239 	}
1240 
1241 	ret = exe_file_deny_write_access(new_exe_file);
1242 	if (ret)
1243 		return -EACCES;
1244 	get_file(new_exe_file);
1245 
1246 	/* set the new file */
1247 	mmap_write_lock(mm);
1248 	old_exe_file = rcu_dereference_raw(mm->exe_file);
1249 	rcu_assign_pointer(mm->exe_file, new_exe_file);
1250 	mmap_write_unlock(mm);
1251 
1252 	if (old_exe_file) {
1253 		exe_file_allow_write_access(old_exe_file);
1254 		fput(old_exe_file);
1255 	}
1256 	return 0;
1257 }
1258 
1259 /**
1260  * get_mm_exe_file - acquire a reference to the mm's executable file
1261  * @mm: The mm of interest.
1262  *
1263  * Returns %NULL if mm has no associated executable file.
1264  * User must release file via fput().
1265  */
1266 struct file *get_mm_exe_file(struct mm_struct *mm)
1267 {
1268 	struct file *exe_file;
1269 
1270 	rcu_read_lock();
1271 	exe_file = get_file_rcu(&mm->exe_file);
1272 	rcu_read_unlock();
1273 	return exe_file;
1274 }
1275 
1276 /**
1277  * get_task_exe_file - acquire a reference to the task's executable file
1278  * @task: The task.
1279  *
1280  * Returns %NULL if task's mm (if any) has no associated executable file or
1281  * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
1282  * User must release file via fput().
1283  */
1284 struct file *get_task_exe_file(struct task_struct *task)
1285 {
1286 	struct file *exe_file = NULL;
1287 	struct mm_struct *mm;
1288 
1289 	if (task->flags & PF_KTHREAD)
1290 		return NULL;
1291 
1292 	task_lock(task);
1293 	mm = task->mm;
1294 	if (mm)
1295 		exe_file = get_mm_exe_file(mm);
1296 	task_unlock(task);
1297 	return exe_file;
1298 }
1299 
1300 /**
1301  * get_task_mm - acquire a reference to the task's mm
1302  * @task: The task.
1303  *
1304  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
1305  * this kernel workthread has transiently adopted a user mm with use_mm,
1306  * to do its AIO) is not set and if so returns a reference to it, after
1307  * bumping up the use count.  User must release the mm via mmput()
1308  * after use.  Typically used by /proc and ptrace.
1309  */
1310 struct mm_struct *get_task_mm(struct task_struct *task)
1311 {
1312 	struct mm_struct *mm;
1313 
1314 	if (task->flags & PF_KTHREAD)
1315 		return NULL;
1316 
1317 	task_lock(task);
1318 	mm = task->mm;
1319 	if (mm)
1320 		mmget(mm);
1321 	task_unlock(task);
1322 	return mm;
1323 }
1324 EXPORT_SYMBOL_GPL(get_task_mm);
1325 
1326 static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode)
1327 {
1328 	if (mm == current->mm)
1329 		return true;
1330 	if (ptrace_may_access(task, mode))
1331 		return true;
1332 	if ((mode & PTRACE_MODE_READ) && perfmon_capable())
1333 		return true;
1334 	return false;
1335 }
1336 
1337 struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
1338 {
1339 	struct mm_struct *mm;
1340 	int err;
1341 
1342 	err =  down_read_killable(&task->signal->exec_update_lock);
1343 	if (err)
1344 		return ERR_PTR(err);
1345 
1346 	mm = get_task_mm(task);
1347 	if (!mm) {
1348 		mm = ERR_PTR(-ESRCH);
1349 	} else if (!may_access_mm(mm, task, mode)) {
1350 		mmput(mm);
1351 		mm = ERR_PTR(-EACCES);
1352 	}
1353 	up_read(&task->signal->exec_update_lock);
1354 
1355 	return mm;
1356 }
1357 
1358 static void complete_vfork_done(struct task_struct *tsk)
1359 {
1360 	struct completion *vfork;
1361 
1362 	task_lock(tsk);
1363 	vfork = tsk->vfork_done;
1364 	if (likely(vfork)) {
1365 		tsk->vfork_done = NULL;
1366 		complete(vfork);
1367 	}
1368 	task_unlock(tsk);
1369 }
1370 
1371 static int wait_for_vfork_done(struct task_struct *child,
1372 				struct completion *vfork)
1373 {
1374 	unsigned int state = TASK_KILLABLE|TASK_FREEZABLE;
1375 	int killed;
1376 
1377 	cgroup_enter_frozen();
1378 	killed = wait_for_completion_state(vfork, state);
1379 	cgroup_leave_frozen(false);
1380 
1381 	if (killed) {
1382 		task_lock(child);
1383 		child->vfork_done = NULL;
1384 		task_unlock(child);
1385 	}
1386 
1387 	put_task_struct(child);
1388 	return killed;
1389 }
1390 
1391 /* Please note the differences between mmput and mm_release.
1392  * mmput is called whenever we stop holding onto a mm_struct,
1393  * error success whatever.
1394  *
1395  * mm_release is called after a mm_struct has been removed
1396  * from the current process.
1397  *
1398  * This difference is important for error handling, when we
1399  * only half set up a mm_struct for a new process and need to restore
1400  * the old one.  Because we mmput the new mm_struct before
1401  * restoring the old one. . .
1402  * Eric Biederman 10 January 1998
1403  */
1404 static void mm_release(struct task_struct *tsk, struct mm_struct *mm)
1405 {
1406 	uprobe_free_utask(tsk);
1407 
1408 	/* Get rid of any cached register state */
1409 	deactivate_mm(tsk, mm);
1410 
1411 	/*
1412 	 * Signal userspace if we're not exiting with a core dump
1413 	 * because we want to leave the value intact for debugging
1414 	 * purposes.
1415 	 */
1416 	if (tsk->clear_child_tid) {
1417 		if (atomic_read(&mm->mm_users) > 1) {
1418 			/*
1419 			 * We don't check the error code - if userspace has
1420 			 * not set up a proper pointer then tough luck.
1421 			 */
1422 			put_user(0, tsk->clear_child_tid);
1423 			do_futex(tsk->clear_child_tid, FUTEX_WAKE,
1424 					1, NULL, NULL, 0, 0);
1425 		}
1426 		tsk->clear_child_tid = NULL;
1427 	}
1428 
1429 	/*
1430 	 * All done, finally we can wake up parent and return this mm to him.
1431 	 * Also kthread_stop() uses this completion for synchronization.
1432 	 */
1433 	if (tsk->vfork_done)
1434 		complete_vfork_done(tsk);
1435 }
1436 
1437 void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm)
1438 {
1439 	futex_exit_release(tsk);
1440 	mm_release(tsk, mm);
1441 }
1442 
1443 void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm)
1444 {
1445 	futex_exec_release(tsk);
1446 	mm_release(tsk, mm);
1447 }
1448 
1449 /**
1450  * dup_mm() - duplicates an existing mm structure
1451  * @tsk: the task_struct with which the new mm will be associated.
1452  * @oldmm: the mm to duplicate.
1453  *
1454  * Allocates a new mm structure and duplicates the provided @oldmm structure
1455  * content into it.
1456  *
1457  * Return: the duplicated mm or NULL on failure.
1458  */
1459 static struct mm_struct *dup_mm(struct task_struct *tsk,
1460 				struct mm_struct *oldmm)
1461 {
1462 	struct mm_struct *mm;
1463 	int err;
1464 
1465 	mm = allocate_mm();
1466 	if (!mm)
1467 		goto fail_nomem;
1468 
1469 	memcpy(mm, oldmm, sizeof(*mm));
1470 
1471 	if (!mm_init(mm, tsk, mm->user_ns))
1472 		goto fail_nomem;
1473 
1474 	uprobe_start_dup_mmap();
1475 	err = dup_mmap(mm, oldmm);
1476 	if (err)
1477 		goto free_pt;
1478 	uprobe_end_dup_mmap();
1479 
1480 	mm->hiwater_rss = get_mm_rss(mm);
1481 	mm->hiwater_vm = mm->total_vm;
1482 
1483 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
1484 		goto free_pt;
1485 
1486 	return mm;
1487 
1488 free_pt:
1489 	/* don't put binfmt in mmput, we haven't got module yet */
1490 	mm->binfmt = NULL;
1491 	mm_init_owner(mm, NULL);
1492 	mmput(mm);
1493 	if (err)
1494 		uprobe_end_dup_mmap();
1495 
1496 fail_nomem:
1497 	return NULL;
1498 }
1499 
1500 static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
1501 {
1502 	struct mm_struct *mm, *oldmm;
1503 
1504 	tsk->min_flt = tsk->maj_flt = 0;
1505 	tsk->nvcsw = tsk->nivcsw = 0;
1506 #ifdef CONFIG_DETECT_HUNG_TASK
1507 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
1508 	tsk->last_switch_time = 0;
1509 #endif
1510 
1511 	tsk->mm = NULL;
1512 	tsk->active_mm = NULL;
1513 
1514 	/*
1515 	 * Are we cloning a kernel thread?
1516 	 *
1517 	 * We need to steal a active VM for that..
1518 	 */
1519 	oldmm = current->mm;
1520 	if (!oldmm)
1521 		return 0;
1522 
1523 	if (clone_flags & CLONE_VM) {
1524 		mmget(oldmm);
1525 		mm = oldmm;
1526 	} else {
1527 		mm = dup_mm(tsk, current->mm);
1528 		if (!mm)
1529 			return -ENOMEM;
1530 	}
1531 
1532 	tsk->mm = mm;
1533 	tsk->active_mm = mm;
1534 	sched_mm_cid_fork(tsk);
1535 	return 0;
1536 }
1537 
1538 static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
1539 {
1540 	struct fs_struct *fs = current->fs;
1541 	if (clone_flags & CLONE_FS) {
1542 		/* tsk->fs is already what we want */
1543 		spin_lock(&fs->lock);
1544 		/* "users" and "in_exec" locked for check_unsafe_exec() */
1545 		if (fs->in_exec) {
1546 			spin_unlock(&fs->lock);
1547 			return -EAGAIN;
1548 		}
1549 		fs->users++;
1550 		spin_unlock(&fs->lock);
1551 		return 0;
1552 	}
1553 	tsk->fs = copy_fs_struct(fs);
1554 	if (!tsk->fs)
1555 		return -ENOMEM;
1556 	return 0;
1557 }
1558 
1559 static int copy_files(unsigned long clone_flags, struct task_struct *tsk,
1560 		      int no_files)
1561 {
1562 	struct files_struct *oldf, *newf;
1563 
1564 	/*
1565 	 * A background process may not have any files ...
1566 	 */
1567 	oldf = current->files;
1568 	if (!oldf)
1569 		return 0;
1570 
1571 	if (no_files) {
1572 		tsk->files = NULL;
1573 		return 0;
1574 	}
1575 
1576 	if (clone_flags & CLONE_FILES) {
1577 		atomic_inc(&oldf->count);
1578 		return 0;
1579 	}
1580 
1581 	newf = dup_fd(oldf, NULL);
1582 	if (IS_ERR(newf))
1583 		return PTR_ERR(newf);
1584 
1585 	tsk->files = newf;
1586 	return 0;
1587 }
1588 
1589 static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
1590 {
1591 	struct sighand_struct *sig;
1592 
1593 	if (clone_flags & CLONE_SIGHAND) {
1594 		refcount_inc(&current->sighand->count);
1595 		return 0;
1596 	}
1597 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1598 	RCU_INIT_POINTER(tsk->sighand, sig);
1599 	if (!sig)
1600 		return -ENOMEM;
1601 
1602 	refcount_set(&sig->count, 1);
1603 	spin_lock_irq(&current->sighand->siglock);
1604 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
1605 	spin_unlock_irq(&current->sighand->siglock);
1606 
1607 	/* Reset all signal handler not set to SIG_IGN to SIG_DFL. */
1608 	if (clone_flags & CLONE_CLEAR_SIGHAND)
1609 		flush_signal_handlers(tsk, 0);
1610 
1611 	return 0;
1612 }
1613 
1614 void __cleanup_sighand(struct sighand_struct *sighand)
1615 {
1616 	if (refcount_dec_and_test(&sighand->count)) {
1617 		signalfd_cleanup(sighand);
1618 		/*
1619 		 * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it
1620 		 * without an RCU grace period, see __lock_task_sighand().
1621 		 */
1622 		kmem_cache_free(sighand_cachep, sighand);
1623 	}
1624 }
1625 
1626 /*
1627  * Initialize POSIX timer handling for a thread group.
1628  */
1629 static void posix_cpu_timers_init_group(struct signal_struct *sig)
1630 {
1631 	struct posix_cputimers *pct = &sig->posix_cputimers;
1632 	unsigned long cpu_limit;
1633 
1634 	cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
1635 	posix_cputimers_group_init(pct, cpu_limit);
1636 }
1637 
1638 static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
1639 {
1640 	struct signal_struct *sig;
1641 
1642 	if (clone_flags & CLONE_THREAD)
1643 		return 0;
1644 
1645 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
1646 	tsk->signal = sig;
1647 	if (!sig)
1648 		return -ENOMEM;
1649 
1650 	sig->nr_threads = 1;
1651 	sig->quick_threads = 1;
1652 	atomic_set(&sig->live, 1);
1653 	refcount_set(&sig->sigcnt, 1);
1654 
1655 	/* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
1656 	sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
1657 	tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
1658 
1659 	init_waitqueue_head(&sig->wait_chldexit);
1660 	sig->curr_target = tsk;
1661 	init_sigpending(&sig->shared_pending);
1662 	INIT_HLIST_HEAD(&sig->multiprocess);
1663 	seqlock_init(&sig->stats_lock);
1664 	prev_cputime_init(&sig->prev_cputime);
1665 
1666 #ifdef CONFIG_POSIX_TIMERS
1667 	INIT_HLIST_HEAD(&sig->posix_timers);
1668 	INIT_HLIST_HEAD(&sig->ignored_posix_timers);
1669 	hrtimer_setup(&sig->real_timer, it_real_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1670 #endif
1671 
1672 	task_lock(current->group_leader);
1673 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
1674 	task_unlock(current->group_leader);
1675 
1676 	posix_cpu_timers_init_group(sig);
1677 
1678 	tty_audit_fork(sig);
1679 	sched_autogroup_fork(sig);
1680 
1681 	sig->oom_score_adj = current->signal->oom_score_adj;
1682 	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
1683 
1684 	mutex_init(&sig->cred_guard_mutex);
1685 	init_rwsem(&sig->exec_update_lock);
1686 
1687 	return 0;
1688 }
1689 
1690 static void copy_seccomp(struct task_struct *p)
1691 {
1692 #ifdef CONFIG_SECCOMP
1693 	/*
1694 	 * Must be called with sighand->lock held, which is common to
1695 	 * all threads in the group. Holding cred_guard_mutex is not
1696 	 * needed because this new task is not yet running and cannot
1697 	 * be racing exec.
1698 	 */
1699 	assert_spin_locked(&current->sighand->siglock);
1700 
1701 	/* Ref-count the new filter user, and assign it. */
1702 	get_seccomp_filter(current);
1703 	p->seccomp = current->seccomp;
1704 
1705 	/*
1706 	 * Explicitly enable no_new_privs here in case it got set
1707 	 * between the task_struct being duplicated and holding the
1708 	 * sighand lock. The seccomp state and nnp must be in sync.
1709 	 */
1710 	if (task_no_new_privs(current))
1711 		task_set_no_new_privs(p);
1712 
1713 	/*
1714 	 * If the parent gained a seccomp mode after copying thread
1715 	 * flags and between before we held the sighand lock, we have
1716 	 * to manually enable the seccomp thread flag here.
1717 	 */
1718 	if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
1719 		set_task_syscall_work(p, SECCOMP);
1720 #endif
1721 }
1722 
1723 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
1724 {
1725 	current->clear_child_tid = tidptr;
1726 
1727 	return task_pid_vnr(current);
1728 }
1729 
1730 static void rt_mutex_init_task(struct task_struct *p)
1731 {
1732 	raw_spin_lock_init(&p->pi_lock);
1733 #ifdef CONFIG_RT_MUTEXES
1734 	p->pi_waiters = RB_ROOT_CACHED;
1735 	p->pi_top_task = NULL;
1736 	p->pi_blocked_on = NULL;
1737 #endif
1738 }
1739 
1740 static inline void init_task_pid_links(struct task_struct *task)
1741 {
1742 	enum pid_type type;
1743 
1744 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type)
1745 		INIT_HLIST_NODE(&task->pid_links[type]);
1746 }
1747 
1748 static inline void
1749 init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
1750 {
1751 	if (type == PIDTYPE_PID)
1752 		task->thread_pid = pid;
1753 	else
1754 		task->signal->pids[type] = pid;
1755 }
1756 
1757 static inline void rcu_copy_process(struct task_struct *p)
1758 {
1759 #ifdef CONFIG_PREEMPT_RCU
1760 	p->rcu_read_lock_nesting = 0;
1761 	p->rcu_read_unlock_special.s = 0;
1762 	p->rcu_blocked_node = NULL;
1763 	INIT_LIST_HEAD(&p->rcu_node_entry);
1764 #endif /* #ifdef CONFIG_PREEMPT_RCU */
1765 #ifdef CONFIG_TASKS_RCU
1766 	p->rcu_tasks_holdout = false;
1767 	INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
1768 	p->rcu_tasks_idle_cpu = -1;
1769 	INIT_LIST_HEAD(&p->rcu_tasks_exit_list);
1770 #endif /* #ifdef CONFIG_TASKS_RCU */
1771 #ifdef CONFIG_TASKS_TRACE_RCU
1772 	p->trc_reader_nesting = 0;
1773 	p->trc_reader_special.s = 0;
1774 	INIT_LIST_HEAD(&p->trc_holdout_list);
1775 	INIT_LIST_HEAD(&p->trc_blkd_node);
1776 #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
1777 }
1778 
1779 /**
1780  * __pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
1781  * @pid:   the struct pid for which to create a pidfd
1782  * @flags: flags of the new @pidfd
1783  * @ret: Where to return the file for the pidfd.
1784  *
1785  * Allocate a new file that stashes @pid and reserve a new pidfd number in the
1786  * caller's file descriptor table. The pidfd is reserved but not installed yet.
1787  *
1788  * The helper doesn't perform checks on @pid which makes it useful for pidfds
1789  * created via CLONE_PIDFD where @pid has no task attached when the pidfd and
1790  * pidfd file are prepared.
1791  *
1792  * If this function returns successfully the caller is responsible to either
1793  * call fd_install() passing the returned pidfd and pidfd file as arguments in
1794  * order to install the pidfd into its file descriptor table or they must use
1795  * put_unused_fd() and fput() on the returned pidfd and pidfd file
1796  * respectively.
1797  *
1798  * This function is useful when a pidfd must already be reserved but there
1799  * might still be points of failure afterwards and the caller wants to ensure
1800  * that no pidfd is leaked into its file descriptor table.
1801  *
1802  * Return: On success, a reserved pidfd is returned from the function and a new
1803  *         pidfd file is returned in the last argument to the function. On
1804  *         error, a negative error code is returned from the function and the
1805  *         last argument remains unchanged.
1806  */
1807 static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
1808 {
1809 	struct file *pidfd_file;
1810 
1811 	CLASS(get_unused_fd, pidfd)(O_CLOEXEC);
1812 	if (pidfd < 0)
1813 		return pidfd;
1814 
1815 	pidfd_file = pidfs_alloc_file(pid, flags | O_RDWR);
1816 	if (IS_ERR(pidfd_file))
1817 		return PTR_ERR(pidfd_file);
1818 
1819 	*ret = pidfd_file;
1820 	return take_fd(pidfd);
1821 }
1822 
1823 /**
1824  * pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
1825  * @pid:   the struct pid for which to create a pidfd
1826  * @flags: flags of the new @pidfd
1827  * @ret: Where to return the pidfd.
1828  *
1829  * Allocate a new file that stashes @pid and reserve a new pidfd number in the
1830  * caller's file descriptor table. The pidfd is reserved but not installed yet.
1831  *
1832  * The helper verifies that @pid is still in use, without PIDFD_THREAD the
1833  * task identified by @pid must be a thread-group leader.
1834  *
1835  * If this function returns successfully the caller is responsible to either
1836  * call fd_install() passing the returned pidfd and pidfd file as arguments in
1837  * order to install the pidfd into its file descriptor table or they must use
1838  * put_unused_fd() and fput() on the returned pidfd and pidfd file
1839  * respectively.
1840  *
1841  * This function is useful when a pidfd must already be reserved but there
1842  * might still be points of failure afterwards and the caller wants to ensure
1843  * that no pidfd is leaked into its file descriptor table.
1844  *
1845  * Return: On success, a reserved pidfd is returned from the function and a new
1846  *         pidfd file is returned in the last argument to the function. On
1847  *         error, a negative error code is returned from the function and the
1848  *         last argument remains unchanged.
1849  */
1850 int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
1851 {
1852 	bool thread = flags & PIDFD_THREAD;
1853 
1854 	if (!pid || !pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
1855 		return -EINVAL;
1856 
1857 	return __pidfd_prepare(pid, flags, ret);
1858 }
1859 
1860 static void __delayed_free_task(struct rcu_head *rhp)
1861 {
1862 	struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
1863 
1864 	free_task(tsk);
1865 }
1866 
1867 static __always_inline void delayed_free_task(struct task_struct *tsk)
1868 {
1869 	if (IS_ENABLED(CONFIG_MEMCG))
1870 		call_rcu(&tsk->rcu, __delayed_free_task);
1871 	else
1872 		free_task(tsk);
1873 }
1874 
1875 static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk)
1876 {
1877 	/* Skip if kernel thread */
1878 	if (!tsk->mm)
1879 		return;
1880 
1881 	/* Skip if spawning a thread or using vfork */
1882 	if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM)
1883 		return;
1884 
1885 	/* We need to synchronize with __set_oom_adj */
1886 	mutex_lock(&oom_adj_mutex);
1887 	set_bit(MMF_MULTIPROCESS, &tsk->mm->flags);
1888 	/* Update the values in case they were changed after copy_signal */
1889 	tsk->signal->oom_score_adj = current->signal->oom_score_adj;
1890 	tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min;
1891 	mutex_unlock(&oom_adj_mutex);
1892 }
1893 
1894 #ifdef CONFIG_RV
1895 static void rv_task_fork(struct task_struct *p)
1896 {
1897 	int i;
1898 
1899 	for (i = 0; i < RV_PER_TASK_MONITORS; i++)
1900 		p->rv[i].da_mon.monitoring = false;
1901 }
1902 #else
1903 #define rv_task_fork(p) do {} while (0)
1904 #endif
1905 
1906 /*
1907  * This creates a new process as a copy of the old one,
1908  * but does not actually start it yet.
1909  *
1910  * It copies the registers, and all the appropriate
1911  * parts of the process environment (as per the clone
1912  * flags). The actual kick-off is left to the caller.
1913  */
1914 __latent_entropy struct task_struct *copy_process(
1915 					struct pid *pid,
1916 					int trace,
1917 					int node,
1918 					struct kernel_clone_args *args)
1919 {
1920 	int pidfd = -1, retval;
1921 	struct task_struct *p;
1922 	struct multiprocess_signals delayed;
1923 	struct file *pidfile = NULL;
1924 	const u64 clone_flags = args->flags;
1925 	struct nsproxy *nsp = current->nsproxy;
1926 
1927 	/*
1928 	 * Don't allow sharing the root directory with processes in a different
1929 	 * namespace
1930 	 */
1931 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
1932 		return ERR_PTR(-EINVAL);
1933 
1934 	if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
1935 		return ERR_PTR(-EINVAL);
1936 
1937 	/*
1938 	 * Thread groups must share signals as well, and detached threads
1939 	 * can only be started up within the thread group.
1940 	 */
1941 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
1942 		return ERR_PTR(-EINVAL);
1943 
1944 	/*
1945 	 * Shared signal handlers imply shared VM. By way of the above,
1946 	 * thread groups also imply shared VM. Blocking this case allows
1947 	 * for various simplifications in other code.
1948 	 */
1949 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
1950 		return ERR_PTR(-EINVAL);
1951 
1952 	/*
1953 	 * Siblings of global init remain as zombies on exit since they are
1954 	 * not reaped by their parent (swapper). To solve this and to avoid
1955 	 * multi-rooted process trees, prevent global and container-inits
1956 	 * from creating siblings.
1957 	 */
1958 	if ((clone_flags & CLONE_PARENT) &&
1959 				current->signal->flags & SIGNAL_UNKILLABLE)
1960 		return ERR_PTR(-EINVAL);
1961 
1962 	/*
1963 	 * If the new process will be in a different pid or user namespace
1964 	 * do not allow it to share a thread group with the forking task.
1965 	 */
1966 	if (clone_flags & CLONE_THREAD) {
1967 		if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
1968 		    (task_active_pid_ns(current) != nsp->pid_ns_for_children))
1969 			return ERR_PTR(-EINVAL);
1970 	}
1971 
1972 	if (clone_flags & CLONE_PIDFD) {
1973 		/*
1974 		 * - CLONE_DETACHED is blocked so that we can potentially
1975 		 *   reuse it later for CLONE_PIDFD.
1976 		 */
1977 		if (clone_flags & CLONE_DETACHED)
1978 			return ERR_PTR(-EINVAL);
1979 	}
1980 
1981 	/*
1982 	 * Force any signals received before this point to be delivered
1983 	 * before the fork happens.  Collect up signals sent to multiple
1984 	 * processes that happen during the fork and delay them so that
1985 	 * they appear to happen after the fork.
1986 	 */
1987 	sigemptyset(&delayed.signal);
1988 	INIT_HLIST_NODE(&delayed.node);
1989 
1990 	spin_lock_irq(&current->sighand->siglock);
1991 	if (!(clone_flags & CLONE_THREAD))
1992 		hlist_add_head(&delayed.node, &current->signal->multiprocess);
1993 	recalc_sigpending();
1994 	spin_unlock_irq(&current->sighand->siglock);
1995 	retval = -ERESTARTNOINTR;
1996 	if (task_sigpending(current))
1997 		goto fork_out;
1998 
1999 	retval = -ENOMEM;
2000 	p = dup_task_struct(current, node);
2001 	if (!p)
2002 		goto fork_out;
2003 	p->flags &= ~PF_KTHREAD;
2004 	if (args->kthread)
2005 		p->flags |= PF_KTHREAD;
2006 	if (args->user_worker) {
2007 		/*
2008 		 * Mark us a user worker, and block any signal that isn't
2009 		 * fatal or STOP
2010 		 */
2011 		p->flags |= PF_USER_WORKER;
2012 		siginitsetinv(&p->blocked, sigmask(SIGKILL)|sigmask(SIGSTOP));
2013 	}
2014 	if (args->io_thread)
2015 		p->flags |= PF_IO_WORKER;
2016 
2017 	if (args->name)
2018 		strscpy_pad(p->comm, args->name, sizeof(p->comm));
2019 
2020 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL;
2021 	/*
2022 	 * Clear TID on mm_release()?
2023 	 */
2024 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL;
2025 
2026 	ftrace_graph_init_task(p);
2027 
2028 	rt_mutex_init_task(p);
2029 
2030 	lockdep_assert_irqs_enabled();
2031 #ifdef CONFIG_PROVE_LOCKING
2032 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
2033 #endif
2034 	retval = copy_creds(p, clone_flags);
2035 	if (retval < 0)
2036 		goto bad_fork_free;
2037 
2038 	retval = -EAGAIN;
2039 	if (is_rlimit_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {
2040 		if (p->real_cred->user != INIT_USER &&
2041 		    !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
2042 			goto bad_fork_cleanup_count;
2043 	}
2044 	current->flags &= ~PF_NPROC_EXCEEDED;
2045 
2046 	/*
2047 	 * If multiple threads are within copy_process(), then this check
2048 	 * triggers too late. This doesn't hurt, the check is only there
2049 	 * to stop root fork bombs.
2050 	 */
2051 	retval = -EAGAIN;
2052 	if (data_race(nr_threads >= max_threads))
2053 		goto bad_fork_cleanup_count;
2054 
2055 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
2056 	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY);
2057 	p->flags |= PF_FORKNOEXEC;
2058 	INIT_LIST_HEAD(&p->children);
2059 	INIT_LIST_HEAD(&p->sibling);
2060 	rcu_copy_process(p);
2061 	p->vfork_done = NULL;
2062 	spin_lock_init(&p->alloc_lock);
2063 
2064 	init_sigpending(&p->pending);
2065 
2066 	p->utime = p->stime = p->gtime = 0;
2067 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
2068 	p->utimescaled = p->stimescaled = 0;
2069 #endif
2070 	prev_cputime_init(&p->prev_cputime);
2071 
2072 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
2073 	seqcount_init(&p->vtime.seqcount);
2074 	p->vtime.starttime = 0;
2075 	p->vtime.state = VTIME_INACTIVE;
2076 #endif
2077 
2078 #ifdef CONFIG_IO_URING
2079 	p->io_uring = NULL;
2080 #endif
2081 
2082 	p->default_timer_slack_ns = current->timer_slack_ns;
2083 
2084 #ifdef CONFIG_PSI
2085 	p->psi_flags = 0;
2086 #endif
2087 
2088 	task_io_accounting_init(&p->ioac);
2089 	acct_clear_integrals(p);
2090 
2091 	posix_cputimers_init(&p->posix_cputimers);
2092 	tick_dep_init_task(p);
2093 
2094 	p->io_context = NULL;
2095 	audit_set_context(p, NULL);
2096 	cgroup_fork(p);
2097 	if (args->kthread) {
2098 		if (!set_kthread_struct(p))
2099 			goto bad_fork_cleanup_delayacct;
2100 	}
2101 #ifdef CONFIG_NUMA
2102 	p->mempolicy = mpol_dup(p->mempolicy);
2103 	if (IS_ERR(p->mempolicy)) {
2104 		retval = PTR_ERR(p->mempolicy);
2105 		p->mempolicy = NULL;
2106 		goto bad_fork_cleanup_delayacct;
2107 	}
2108 #endif
2109 #ifdef CONFIG_CPUSETS
2110 	p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
2111 	seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock);
2112 #endif
2113 #ifdef CONFIG_TRACE_IRQFLAGS
2114 	memset(&p->irqtrace, 0, sizeof(p->irqtrace));
2115 	p->irqtrace.hardirq_disable_ip	= _THIS_IP_;
2116 	p->irqtrace.softirq_enable_ip	= _THIS_IP_;
2117 	p->softirqs_enabled		= 1;
2118 	p->softirq_context		= 0;
2119 #endif
2120 
2121 	p->pagefault_disabled = 0;
2122 
2123 #ifdef CONFIG_LOCKDEP
2124 	lockdep_init_task(p);
2125 #endif
2126 
2127 #ifdef CONFIG_DEBUG_MUTEXES
2128 	p->blocked_on = NULL; /* not blocked yet */
2129 #endif
2130 #ifdef CONFIG_BCACHE
2131 	p->sequential_io	= 0;
2132 	p->sequential_io_avg	= 0;
2133 #endif
2134 #ifdef CONFIG_BPF_SYSCALL
2135 	RCU_INIT_POINTER(p->bpf_storage, NULL);
2136 	p->bpf_ctx = NULL;
2137 #endif
2138 
2139 	/* Perform scheduler related setup. Assign this task to a CPU. */
2140 	retval = sched_fork(clone_flags, p);
2141 	if (retval)
2142 		goto bad_fork_cleanup_policy;
2143 
2144 	retval = perf_event_init_task(p, clone_flags);
2145 	if (retval)
2146 		goto bad_fork_sched_cancel_fork;
2147 	retval = audit_alloc(p);
2148 	if (retval)
2149 		goto bad_fork_cleanup_perf;
2150 	/* copy all the process information */
2151 	shm_init_task(p);
2152 	retval = security_task_alloc(p, clone_flags);
2153 	if (retval)
2154 		goto bad_fork_cleanup_audit;
2155 	retval = copy_semundo(clone_flags, p);
2156 	if (retval)
2157 		goto bad_fork_cleanup_security;
2158 	retval = copy_files(clone_flags, p, args->no_files);
2159 	if (retval)
2160 		goto bad_fork_cleanup_semundo;
2161 	retval = copy_fs(clone_flags, p);
2162 	if (retval)
2163 		goto bad_fork_cleanup_files;
2164 	retval = copy_sighand(clone_flags, p);
2165 	if (retval)
2166 		goto bad_fork_cleanup_fs;
2167 	retval = copy_signal(clone_flags, p);
2168 	if (retval)
2169 		goto bad_fork_cleanup_sighand;
2170 	retval = copy_mm(clone_flags, p);
2171 	if (retval)
2172 		goto bad_fork_cleanup_signal;
2173 	retval = copy_namespaces(clone_flags, p);
2174 	if (retval)
2175 		goto bad_fork_cleanup_mm;
2176 	retval = copy_io(clone_flags, p);
2177 	if (retval)
2178 		goto bad_fork_cleanup_namespaces;
2179 	retval = copy_thread(p, args);
2180 	if (retval)
2181 		goto bad_fork_cleanup_io;
2182 
2183 	stackleak_task_init(p);
2184 
2185 	if (pid != &init_struct_pid) {
2186 		pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid,
2187 				args->set_tid_size);
2188 		if (IS_ERR(pid)) {
2189 			retval = PTR_ERR(pid);
2190 			goto bad_fork_cleanup_thread;
2191 		}
2192 	}
2193 
2194 	/*
2195 	 * This has to happen after we've potentially unshared the file
2196 	 * descriptor table (so that the pidfd doesn't leak into the child
2197 	 * if the fd table isn't shared).
2198 	 */
2199 	if (clone_flags & CLONE_PIDFD) {
2200 		int flags = (clone_flags & CLONE_THREAD) ? PIDFD_THREAD : 0;
2201 
2202 		/*
2203 		 * Note that no task has been attached to @pid yet indicate
2204 		 * that via CLONE_PIDFD.
2205 		 */
2206 		retval = __pidfd_prepare(pid, flags | PIDFD_CLONE, &pidfile);
2207 		if (retval < 0)
2208 			goto bad_fork_free_pid;
2209 		pidfd = retval;
2210 
2211 		retval = put_user(pidfd, args->pidfd);
2212 		if (retval)
2213 			goto bad_fork_put_pidfd;
2214 	}
2215 
2216 #ifdef CONFIG_BLOCK
2217 	p->plug = NULL;
2218 #endif
2219 	futex_init_task(p);
2220 
2221 	/*
2222 	 * sigaltstack should be cleared when sharing the same VM
2223 	 */
2224 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
2225 		sas_ss_reset(p);
2226 
2227 	/*
2228 	 * Syscall tracing and stepping should be turned off in the
2229 	 * child regardless of CLONE_PTRACE.
2230 	 */
2231 	user_disable_single_step(p);
2232 	clear_task_syscall_work(p, SYSCALL_TRACE);
2233 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU)
2234 	clear_task_syscall_work(p, SYSCALL_EMU);
2235 #endif
2236 	clear_tsk_latency_tracing(p);
2237 
2238 	/* ok, now we should be set up.. */
2239 	p->pid = pid_nr(pid);
2240 	if (clone_flags & CLONE_THREAD) {
2241 		p->group_leader = current->group_leader;
2242 		p->tgid = current->tgid;
2243 	} else {
2244 		p->group_leader = p;
2245 		p->tgid = p->pid;
2246 	}
2247 
2248 	p->nr_dirtied = 0;
2249 	p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
2250 	p->dirty_paused_when = 0;
2251 
2252 	p->pdeath_signal = 0;
2253 	p->task_works = NULL;
2254 	clear_posix_cputimers_work(p);
2255 
2256 #ifdef CONFIG_KRETPROBES
2257 	p->kretprobe_instances.first = NULL;
2258 #endif
2259 #ifdef CONFIG_RETHOOK
2260 	p->rethooks.first = NULL;
2261 #endif
2262 
2263 	/*
2264 	 * Ensure that the cgroup subsystem policies allow the new process to be
2265 	 * forked. It should be noted that the new process's css_set can be changed
2266 	 * between here and cgroup_post_fork() if an organisation operation is in
2267 	 * progress.
2268 	 */
2269 	retval = cgroup_can_fork(p, args);
2270 	if (retval)
2271 		goto bad_fork_put_pidfd;
2272 
2273 	/*
2274 	 * Now that the cgroups are pinned, re-clone the parent cgroup and put
2275 	 * the new task on the correct runqueue. All this *before* the task
2276 	 * becomes visible.
2277 	 *
2278 	 * This isn't part of ->can_fork() because while the re-cloning is
2279 	 * cgroup specific, it unconditionally needs to place the task on a
2280 	 * runqueue.
2281 	 */
2282 	retval = sched_cgroup_fork(p, args);
2283 	if (retval)
2284 		goto bad_fork_cancel_cgroup;
2285 
2286 	/*
2287 	 * From this point on we must avoid any synchronous user-space
2288 	 * communication until we take the tasklist-lock. In particular, we do
2289 	 * not want user-space to be able to predict the process start-time by
2290 	 * stalling fork(2) after we recorded the start_time but before it is
2291 	 * visible to the system.
2292 	 */
2293 
2294 	p->start_time = ktime_get_ns();
2295 	p->start_boottime = ktime_get_boottime_ns();
2296 
2297 	/*
2298 	 * Make it visible to the rest of the system, but dont wake it up yet.
2299 	 * Need tasklist lock for parent etc handling!
2300 	 */
2301 	write_lock_irq(&tasklist_lock);
2302 
2303 	/* CLONE_PARENT re-uses the old parent */
2304 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
2305 		p->real_parent = current->real_parent;
2306 		p->parent_exec_id = current->parent_exec_id;
2307 		if (clone_flags & CLONE_THREAD)
2308 			p->exit_signal = -1;
2309 		else
2310 			p->exit_signal = current->group_leader->exit_signal;
2311 	} else {
2312 		p->real_parent = current;
2313 		p->parent_exec_id = current->self_exec_id;
2314 		p->exit_signal = args->exit_signal;
2315 	}
2316 
2317 	klp_copy_process(p);
2318 
2319 	sched_core_fork(p);
2320 
2321 	spin_lock(&current->sighand->siglock);
2322 
2323 	rv_task_fork(p);
2324 
2325 	rseq_fork(p, clone_flags);
2326 
2327 	/* Don't start children in a dying pid namespace */
2328 	if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
2329 		retval = -ENOMEM;
2330 		goto bad_fork_core_free;
2331 	}
2332 
2333 	/* Let kill terminate clone/fork in the middle */
2334 	if (fatal_signal_pending(current)) {
2335 		retval = -EINTR;
2336 		goto bad_fork_core_free;
2337 	}
2338 
2339 	/* No more failure paths after this point. */
2340 
2341 	/*
2342 	 * Copy seccomp details explicitly here, in case they were changed
2343 	 * before holding sighand lock.
2344 	 */
2345 	copy_seccomp(p);
2346 
2347 	init_task_pid_links(p);
2348 	if (likely(p->pid)) {
2349 		ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
2350 
2351 		init_task_pid(p, PIDTYPE_PID, pid);
2352 		if (thread_group_leader(p)) {
2353 			init_task_pid(p, PIDTYPE_TGID, pid);
2354 			init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
2355 			init_task_pid(p, PIDTYPE_SID, task_session(current));
2356 
2357 			if (is_child_reaper(pid)) {
2358 				ns_of_pid(pid)->child_reaper = p;
2359 				p->signal->flags |= SIGNAL_UNKILLABLE;
2360 			}
2361 			p->signal->shared_pending.signal = delayed.signal;
2362 			p->signal->tty = tty_kref_get(current->signal->tty);
2363 			/*
2364 			 * Inherit has_child_subreaper flag under the same
2365 			 * tasklist_lock with adding child to the process tree
2366 			 * for propagate_has_child_subreaper optimization.
2367 			 */
2368 			p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper ||
2369 							 p->real_parent->signal->is_child_subreaper;
2370 			list_add_tail(&p->sibling, &p->real_parent->children);
2371 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
2372 			attach_pid(p, PIDTYPE_TGID);
2373 			attach_pid(p, PIDTYPE_PGID);
2374 			attach_pid(p, PIDTYPE_SID);
2375 			__this_cpu_inc(process_counts);
2376 		} else {
2377 			current->signal->nr_threads++;
2378 			current->signal->quick_threads++;
2379 			atomic_inc(&current->signal->live);
2380 			refcount_inc(&current->signal->sigcnt);
2381 			task_join_group_stop(p);
2382 			list_add_tail_rcu(&p->thread_node,
2383 					  &p->signal->thread_head);
2384 		}
2385 		attach_pid(p, PIDTYPE_PID);
2386 		nr_threads++;
2387 	}
2388 	total_forks++;
2389 	hlist_del_init(&delayed.node);
2390 	spin_unlock(&current->sighand->siglock);
2391 	syscall_tracepoint_update(p);
2392 	write_unlock_irq(&tasklist_lock);
2393 
2394 	if (pidfile)
2395 		fd_install(pidfd, pidfile);
2396 
2397 	proc_fork_connector(p);
2398 	sched_post_fork(p);
2399 	cgroup_post_fork(p, args);
2400 	perf_event_fork(p);
2401 
2402 	trace_task_newtask(p, clone_flags);
2403 	uprobe_copy_process(p, clone_flags);
2404 	user_events_fork(p, clone_flags);
2405 
2406 	copy_oom_score_adj(clone_flags, p);
2407 
2408 	return p;
2409 
2410 bad_fork_core_free:
2411 	sched_core_free(p);
2412 	spin_unlock(&current->sighand->siglock);
2413 	write_unlock_irq(&tasklist_lock);
2414 bad_fork_cancel_cgroup:
2415 	cgroup_cancel_fork(p, args);
2416 bad_fork_put_pidfd:
2417 	if (clone_flags & CLONE_PIDFD) {
2418 		fput(pidfile);
2419 		put_unused_fd(pidfd);
2420 	}
2421 bad_fork_free_pid:
2422 	if (pid != &init_struct_pid)
2423 		free_pid(pid);
2424 bad_fork_cleanup_thread:
2425 	exit_thread(p);
2426 bad_fork_cleanup_io:
2427 	if (p->io_context)
2428 		exit_io_context(p);
2429 bad_fork_cleanup_namespaces:
2430 	exit_task_namespaces(p);
2431 bad_fork_cleanup_mm:
2432 	if (p->mm) {
2433 		mm_clear_owner(p->mm, p);
2434 		mmput(p->mm);
2435 	}
2436 bad_fork_cleanup_signal:
2437 	if (!(clone_flags & CLONE_THREAD))
2438 		free_signal_struct(p->signal);
2439 bad_fork_cleanup_sighand:
2440 	__cleanup_sighand(p->sighand);
2441 bad_fork_cleanup_fs:
2442 	exit_fs(p); /* blocking */
2443 bad_fork_cleanup_files:
2444 	exit_files(p); /* blocking */
2445 bad_fork_cleanup_semundo:
2446 	exit_sem(p);
2447 bad_fork_cleanup_security:
2448 	security_task_free(p);
2449 bad_fork_cleanup_audit:
2450 	audit_free(p);
2451 bad_fork_cleanup_perf:
2452 	perf_event_free_task(p);
2453 bad_fork_sched_cancel_fork:
2454 	sched_cancel_fork(p);
2455 bad_fork_cleanup_policy:
2456 	lockdep_free_task(p);
2457 #ifdef CONFIG_NUMA
2458 	mpol_put(p->mempolicy);
2459 #endif
2460 bad_fork_cleanup_delayacct:
2461 	delayacct_tsk_free(p);
2462 bad_fork_cleanup_count:
2463 	dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
2464 	exit_creds(p);
2465 bad_fork_free:
2466 	WRITE_ONCE(p->__state, TASK_DEAD);
2467 	exit_task_stack_account(p);
2468 	put_task_stack(p);
2469 	delayed_free_task(p);
2470 fork_out:
2471 	spin_lock_irq(&current->sighand->siglock);
2472 	hlist_del_init(&delayed.node);
2473 	spin_unlock_irq(&current->sighand->siglock);
2474 	return ERR_PTR(retval);
2475 }
2476 
2477 static inline void init_idle_pids(struct task_struct *idle)
2478 {
2479 	enum pid_type type;
2480 
2481 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
2482 		INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */
2483 		init_task_pid(idle, type, &init_struct_pid);
2484 	}
2485 }
2486 
2487 static int idle_dummy(void *dummy)
2488 {
2489 	/* This function is never called */
2490 	return 0;
2491 }
2492 
2493 struct task_struct * __init fork_idle(int cpu)
2494 {
2495 	struct task_struct *task;
2496 	struct kernel_clone_args args = {
2497 		.flags		= CLONE_VM,
2498 		.fn		= &idle_dummy,
2499 		.fn_arg		= NULL,
2500 		.kthread	= 1,
2501 		.idle		= 1,
2502 	};
2503 
2504 	task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args);
2505 	if (!IS_ERR(task)) {
2506 		init_idle_pids(task);
2507 		init_idle(task, cpu);
2508 	}
2509 
2510 	return task;
2511 }
2512 
2513 /*
2514  * This is like kernel_clone(), but shaved down and tailored to just
2515  * creating io_uring workers. It returns a created task, or an error pointer.
2516  * The returned task is inactive, and the caller must fire it up through
2517  * wake_up_new_task(p). All signals are blocked in the created task.
2518  */
2519 struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node)
2520 {
2521 	unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
2522 				CLONE_IO;
2523 	struct kernel_clone_args args = {
2524 		.flags		= ((lower_32_bits(flags) | CLONE_VM |
2525 				    CLONE_UNTRACED) & ~CSIGNAL),
2526 		.exit_signal	= (lower_32_bits(flags) & CSIGNAL),
2527 		.fn		= fn,
2528 		.fn_arg		= arg,
2529 		.io_thread	= 1,
2530 		.user_worker	= 1,
2531 	};
2532 
2533 	return copy_process(NULL, 0, node, &args);
2534 }
2535 
2536 /*
2537  *  Ok, this is the main fork-routine.
2538  *
2539  * It copies the process, and if successful kick-starts
2540  * it and waits for it to finish using the VM if required.
2541  *
2542  * args->exit_signal is expected to be checked for sanity by the caller.
2543  */
2544 pid_t kernel_clone(struct kernel_clone_args *args)
2545 {
2546 	u64 clone_flags = args->flags;
2547 	struct completion vfork;
2548 	struct pid *pid;
2549 	struct task_struct *p;
2550 	int trace = 0;
2551 	pid_t nr;
2552 
2553 	/*
2554 	 * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument
2555 	 * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are
2556 	 * mutually exclusive. With clone3() CLONE_PIDFD has grown a separate
2557 	 * field in struct clone_args and it still doesn't make sense to have
2558 	 * them both point at the same memory location. Performing this check
2559 	 * here has the advantage that we don't need to have a separate helper
2560 	 * to check for legacy clone().
2561 	 */
2562 	if ((clone_flags & CLONE_PIDFD) &&
2563 	    (clone_flags & CLONE_PARENT_SETTID) &&
2564 	    (args->pidfd == args->parent_tid))
2565 		return -EINVAL;
2566 
2567 	/*
2568 	 * Determine whether and which event to report to ptracer.  When
2569 	 * called from kernel_thread or CLONE_UNTRACED is explicitly
2570 	 * requested, no event is reported; otherwise, report if the event
2571 	 * for the type of forking is enabled.
2572 	 */
2573 	if (!(clone_flags & CLONE_UNTRACED)) {
2574 		if (clone_flags & CLONE_VFORK)
2575 			trace = PTRACE_EVENT_VFORK;
2576 		else if (args->exit_signal != SIGCHLD)
2577 			trace = PTRACE_EVENT_CLONE;
2578 		else
2579 			trace = PTRACE_EVENT_FORK;
2580 
2581 		if (likely(!ptrace_event_enabled(current, trace)))
2582 			trace = 0;
2583 	}
2584 
2585 	p = copy_process(NULL, trace, NUMA_NO_NODE, args);
2586 	add_latent_entropy();
2587 
2588 	if (IS_ERR(p))
2589 		return PTR_ERR(p);
2590 
2591 	/*
2592 	 * Do this prior waking up the new thread - the thread pointer
2593 	 * might get invalid after that point, if the thread exits quickly.
2594 	 */
2595 	trace_sched_process_fork(current, p);
2596 
2597 	pid = get_task_pid(p, PIDTYPE_PID);
2598 	nr = pid_vnr(pid);
2599 
2600 	if (clone_flags & CLONE_PARENT_SETTID)
2601 		put_user(nr, args->parent_tid);
2602 
2603 	if (clone_flags & CLONE_VFORK) {
2604 		p->vfork_done = &vfork;
2605 		init_completion(&vfork);
2606 		get_task_struct(p);
2607 	}
2608 
2609 	if (IS_ENABLED(CONFIG_LRU_GEN_WALKS_MMU) && !(clone_flags & CLONE_VM)) {
2610 		/* lock the task to synchronize with memcg migration */
2611 		task_lock(p);
2612 		lru_gen_add_mm(p->mm);
2613 		task_unlock(p);
2614 	}
2615 
2616 	wake_up_new_task(p);
2617 
2618 	/* forking complete and child started to run, tell ptracer */
2619 	if (unlikely(trace))
2620 		ptrace_event_pid(trace, pid);
2621 
2622 	if (clone_flags & CLONE_VFORK) {
2623 		if (!wait_for_vfork_done(p, &vfork))
2624 			ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
2625 	}
2626 
2627 	put_pid(pid);
2628 	return nr;
2629 }
2630 
2631 /*
2632  * Create a kernel thread.
2633  */
2634 pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name,
2635 		    unsigned long flags)
2636 {
2637 	struct kernel_clone_args args = {
2638 		.flags		= ((lower_32_bits(flags) | CLONE_VM |
2639 				    CLONE_UNTRACED) & ~CSIGNAL),
2640 		.exit_signal	= (lower_32_bits(flags) & CSIGNAL),
2641 		.fn		= fn,
2642 		.fn_arg		= arg,
2643 		.name		= name,
2644 		.kthread	= 1,
2645 	};
2646 
2647 	return kernel_clone(&args);
2648 }
2649 
2650 /*
2651  * Create a user mode thread.
2652  */
2653 pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags)
2654 {
2655 	struct kernel_clone_args args = {
2656 		.flags		= ((lower_32_bits(flags) | CLONE_VM |
2657 				    CLONE_UNTRACED) & ~CSIGNAL),
2658 		.exit_signal	= (lower_32_bits(flags) & CSIGNAL),
2659 		.fn		= fn,
2660 		.fn_arg		= arg,
2661 	};
2662 
2663 	return kernel_clone(&args);
2664 }
2665 
2666 #ifdef __ARCH_WANT_SYS_FORK
2667 SYSCALL_DEFINE0(fork)
2668 {
2669 #ifdef CONFIG_MMU
2670 	struct kernel_clone_args args = {
2671 		.exit_signal = SIGCHLD,
2672 	};
2673 
2674 	return kernel_clone(&args);
2675 #else
2676 	/* can not support in nommu mode */
2677 	return -EINVAL;
2678 #endif
2679 }
2680 #endif
2681 
2682 #ifdef __ARCH_WANT_SYS_VFORK
2683 SYSCALL_DEFINE0(vfork)
2684 {
2685 	struct kernel_clone_args args = {
2686 		.flags		= CLONE_VFORK | CLONE_VM,
2687 		.exit_signal	= SIGCHLD,
2688 	};
2689 
2690 	return kernel_clone(&args);
2691 }
2692 #endif
2693 
2694 #ifdef __ARCH_WANT_SYS_CLONE
2695 #ifdef CONFIG_CLONE_BACKWARDS
2696 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
2697 		 int __user *, parent_tidptr,
2698 		 unsigned long, tls,
2699 		 int __user *, child_tidptr)
2700 #elif defined(CONFIG_CLONE_BACKWARDS2)
2701 SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
2702 		 int __user *, parent_tidptr,
2703 		 int __user *, child_tidptr,
2704 		 unsigned long, tls)
2705 #elif defined(CONFIG_CLONE_BACKWARDS3)
2706 SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
2707 		int, stack_size,
2708 		int __user *, parent_tidptr,
2709 		int __user *, child_tidptr,
2710 		unsigned long, tls)
2711 #else
2712 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
2713 		 int __user *, parent_tidptr,
2714 		 int __user *, child_tidptr,
2715 		 unsigned long, tls)
2716 #endif
2717 {
2718 	struct kernel_clone_args args = {
2719 		.flags		= (lower_32_bits(clone_flags) & ~CSIGNAL),
2720 		.pidfd		= parent_tidptr,
2721 		.child_tid	= child_tidptr,
2722 		.parent_tid	= parent_tidptr,
2723 		.exit_signal	= (lower_32_bits(clone_flags) & CSIGNAL),
2724 		.stack		= newsp,
2725 		.tls		= tls,
2726 	};
2727 
2728 	return kernel_clone(&args);
2729 }
2730 #endif
2731 
2732 noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
2733 					      struct clone_args __user *uargs,
2734 					      size_t usize)
2735 {
2736 	int err;
2737 	struct clone_args args;
2738 	pid_t *kset_tid = kargs->set_tid;
2739 
2740 	BUILD_BUG_ON(offsetofend(struct clone_args, tls) !=
2741 		     CLONE_ARGS_SIZE_VER0);
2742 	BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) !=
2743 		     CLONE_ARGS_SIZE_VER1);
2744 	BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) !=
2745 		     CLONE_ARGS_SIZE_VER2);
2746 	BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2);
2747 
2748 	if (unlikely(usize > PAGE_SIZE))
2749 		return -E2BIG;
2750 	if (unlikely(usize < CLONE_ARGS_SIZE_VER0))
2751 		return -EINVAL;
2752 
2753 	err = copy_struct_from_user(&args, sizeof(args), uargs, usize);
2754 	if (err)
2755 		return err;
2756 
2757 	if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL))
2758 		return -EINVAL;
2759 
2760 	if (unlikely(!args.set_tid && args.set_tid_size > 0))
2761 		return -EINVAL;
2762 
2763 	if (unlikely(args.set_tid && args.set_tid_size == 0))
2764 		return -EINVAL;
2765 
2766 	/*
2767 	 * Verify that higher 32bits of exit_signal are unset and that
2768 	 * it is a valid signal
2769 	 */
2770 	if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) ||
2771 		     !valid_signal(args.exit_signal)))
2772 		return -EINVAL;
2773 
2774 	if ((args.flags & CLONE_INTO_CGROUP) &&
2775 	    (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2))
2776 		return -EINVAL;
2777 
2778 	*kargs = (struct kernel_clone_args){
2779 		.flags		= args.flags,
2780 		.pidfd		= u64_to_user_ptr(args.pidfd),
2781 		.child_tid	= u64_to_user_ptr(args.child_tid),
2782 		.parent_tid	= u64_to_user_ptr(args.parent_tid),
2783 		.exit_signal	= args.exit_signal,
2784 		.stack		= args.stack,
2785 		.stack_size	= args.stack_size,
2786 		.tls		= args.tls,
2787 		.set_tid_size	= args.set_tid_size,
2788 		.cgroup		= args.cgroup,
2789 	};
2790 
2791 	if (args.set_tid &&
2792 		copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid),
2793 			(kargs->set_tid_size * sizeof(pid_t))))
2794 		return -EFAULT;
2795 
2796 	kargs->set_tid = kset_tid;
2797 
2798 	return 0;
2799 }
2800 
2801 /**
2802  * clone3_stack_valid - check and prepare stack
2803  * @kargs: kernel clone args
2804  *
2805  * Verify that the stack arguments userspace gave us are sane.
2806  * In addition, set the stack direction for userspace since it's easy for us to
2807  * determine.
2808  */
2809 static inline bool clone3_stack_valid(struct kernel_clone_args *kargs)
2810 {
2811 	if (kargs->stack == 0) {
2812 		if (kargs->stack_size > 0)
2813 			return false;
2814 	} else {
2815 		if (kargs->stack_size == 0)
2816 			return false;
2817 
2818 		if (!access_ok((void __user *)kargs->stack, kargs->stack_size))
2819 			return false;
2820 
2821 #if !defined(CONFIG_STACK_GROWSUP)
2822 		kargs->stack += kargs->stack_size;
2823 #endif
2824 	}
2825 
2826 	return true;
2827 }
2828 
2829 static bool clone3_args_valid(struct kernel_clone_args *kargs)
2830 {
2831 	/* Verify that no unknown flags are passed along. */
2832 	if (kargs->flags &
2833 	    ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP))
2834 		return false;
2835 
2836 	/*
2837 	 * - make the CLONE_DETACHED bit reusable for clone3
2838 	 * - make the CSIGNAL bits reusable for clone3
2839 	 */
2840 	if (kargs->flags & (CLONE_DETACHED | (CSIGNAL & (~CLONE_NEWTIME))))
2841 		return false;
2842 
2843 	if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) ==
2844 	    (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND))
2845 		return false;
2846 
2847 	if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) &&
2848 	    kargs->exit_signal)
2849 		return false;
2850 
2851 	if (!clone3_stack_valid(kargs))
2852 		return false;
2853 
2854 	return true;
2855 }
2856 
2857 /**
2858  * sys_clone3 - create a new process with specific properties
2859  * @uargs: argument structure
2860  * @size:  size of @uargs
2861  *
2862  * clone3() is the extensible successor to clone()/clone2().
2863  * It takes a struct as argument that is versioned by its size.
2864  *
2865  * Return: On success, a positive PID for the child process.
2866  *         On error, a negative errno number.
2867  */
2868 SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size)
2869 {
2870 	int err;
2871 
2872 	struct kernel_clone_args kargs;
2873 	pid_t set_tid[MAX_PID_NS_LEVEL];
2874 
2875 #ifdef __ARCH_BROKEN_SYS_CLONE3
2876 #warning clone3() entry point is missing, please fix
2877 	return -ENOSYS;
2878 #endif
2879 
2880 	kargs.set_tid = set_tid;
2881 
2882 	err = copy_clone_args_from_user(&kargs, uargs, size);
2883 	if (err)
2884 		return err;
2885 
2886 	if (!clone3_args_valid(&kargs))
2887 		return -EINVAL;
2888 
2889 	return kernel_clone(&kargs);
2890 }
2891 
2892 void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data)
2893 {
2894 	struct task_struct *leader, *parent, *child;
2895 	int res;
2896 
2897 	read_lock(&tasklist_lock);
2898 	leader = top = top->group_leader;
2899 down:
2900 	for_each_thread(leader, parent) {
2901 		list_for_each_entry(child, &parent->children, sibling) {
2902 			res = visitor(child, data);
2903 			if (res) {
2904 				if (res < 0)
2905 					goto out;
2906 				leader = child;
2907 				goto down;
2908 			}
2909 up:
2910 			;
2911 		}
2912 	}
2913 
2914 	if (leader != top) {
2915 		child = leader;
2916 		parent = child->real_parent;
2917 		leader = parent->group_leader;
2918 		goto up;
2919 	}
2920 out:
2921 	read_unlock(&tasklist_lock);
2922 }
2923 
2924 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
2925 #define ARCH_MIN_MMSTRUCT_ALIGN 0
2926 #endif
2927 
2928 static void sighand_ctor(void *data)
2929 {
2930 	struct sighand_struct *sighand = data;
2931 
2932 	spin_lock_init(&sighand->siglock);
2933 	init_waitqueue_head(&sighand->signalfd_wqh);
2934 }
2935 
2936 void __init mm_cache_init(void)
2937 {
2938 	unsigned int mm_size;
2939 
2940 	/*
2941 	 * The mm_cpumask is located at the end of mm_struct, and is
2942 	 * dynamically sized based on the maximum CPU number this system
2943 	 * can have, taking hotplug into account (nr_cpu_ids).
2944 	 */
2945 	mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size();
2946 
2947 	mm_cachep = kmem_cache_create_usercopy("mm_struct",
2948 			mm_size, ARCH_MIN_MMSTRUCT_ALIGN,
2949 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
2950 			offsetof(struct mm_struct, saved_auxv),
2951 			sizeof_field(struct mm_struct, saved_auxv),
2952 			NULL);
2953 }
2954 
2955 void __init proc_caches_init(void)
2956 {
2957 	sighand_cachep = kmem_cache_create("sighand_cache",
2958 			sizeof(struct sighand_struct), 0,
2959 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
2960 			SLAB_ACCOUNT, sighand_ctor);
2961 	signal_cachep = kmem_cache_create("signal_cache",
2962 			sizeof(struct signal_struct), 0,
2963 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
2964 			NULL);
2965 	files_cachep = kmem_cache_create("files_cache",
2966 			sizeof(struct files_struct), 0,
2967 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
2968 			NULL);
2969 	fs_cachep = kmem_cache_create("fs_cache",
2970 			sizeof(struct fs_struct), 0,
2971 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
2972 			NULL);
2973 	mmap_init();
2974 	nsproxy_cache_init();
2975 }
2976 
2977 /*
2978  * Check constraints on flags passed to the unshare system call.
2979  */
2980 static int check_unshare_flags(unsigned long unshare_flags)
2981 {
2982 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
2983 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
2984 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
2985 				CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP|
2986 				CLONE_NEWTIME))
2987 		return -EINVAL;
2988 	/*
2989 	 * Not implemented, but pretend it works if there is nothing
2990 	 * to unshare.  Note that unsharing the address space or the
2991 	 * signal handlers also need to unshare the signal queues (aka
2992 	 * CLONE_THREAD).
2993 	 */
2994 	if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
2995 		if (!thread_group_empty(current))
2996 			return -EINVAL;
2997 	}
2998 	if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
2999 		if (refcount_read(&current->sighand->count) > 1)
3000 			return -EINVAL;
3001 	}
3002 	if (unshare_flags & CLONE_VM) {
3003 		if (!current_is_single_threaded())
3004 			return -EINVAL;
3005 	}
3006 
3007 	return 0;
3008 }
3009 
3010 /*
3011  * Unshare the filesystem structure if it is being shared
3012  */
3013 static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
3014 {
3015 	struct fs_struct *fs = current->fs;
3016 
3017 	if (!(unshare_flags & CLONE_FS) || !fs)
3018 		return 0;
3019 
3020 	/* don't need lock here; in the worst case we'll do useless copy */
3021 	if (fs->users == 1)
3022 		return 0;
3023 
3024 	*new_fsp = copy_fs_struct(fs);
3025 	if (!*new_fsp)
3026 		return -ENOMEM;
3027 
3028 	return 0;
3029 }
3030 
3031 /*
3032  * Unshare file descriptor table if it is being shared
3033  */
3034 static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
3035 {
3036 	struct files_struct *fd = current->files;
3037 
3038 	if ((unshare_flags & CLONE_FILES) &&
3039 	    (fd && atomic_read(&fd->count) > 1)) {
3040 		fd = dup_fd(fd, NULL);
3041 		if (IS_ERR(fd))
3042 			return PTR_ERR(fd);
3043 		*new_fdp = fd;
3044 	}
3045 
3046 	return 0;
3047 }
3048 
3049 /*
3050  * unshare allows a process to 'unshare' part of the process
3051  * context which was originally shared using clone.  copy_*
3052  * functions used by kernel_clone() cannot be used here directly
3053  * because they modify an inactive task_struct that is being
3054  * constructed. Here we are modifying the current, active,
3055  * task_struct.
3056  */
3057 int ksys_unshare(unsigned long unshare_flags)
3058 {
3059 	struct fs_struct *fs, *new_fs = NULL;
3060 	struct files_struct *new_fd = NULL;
3061 	struct cred *new_cred = NULL;
3062 	struct nsproxy *new_nsproxy = NULL;
3063 	int do_sysvsem = 0;
3064 	int err;
3065 
3066 	/*
3067 	 * If unsharing a user namespace must also unshare the thread group
3068 	 * and unshare the filesystem root and working directories.
3069 	 */
3070 	if (unshare_flags & CLONE_NEWUSER)
3071 		unshare_flags |= CLONE_THREAD | CLONE_FS;
3072 	/*
3073 	 * If unsharing vm, must also unshare signal handlers.
3074 	 */
3075 	if (unshare_flags & CLONE_VM)
3076 		unshare_flags |= CLONE_SIGHAND;
3077 	/*
3078 	 * If unsharing a signal handlers, must also unshare the signal queues.
3079 	 */
3080 	if (unshare_flags & CLONE_SIGHAND)
3081 		unshare_flags |= CLONE_THREAD;
3082 	/*
3083 	 * If unsharing namespace, must also unshare filesystem information.
3084 	 */
3085 	if (unshare_flags & CLONE_NEWNS)
3086 		unshare_flags |= CLONE_FS;
3087 
3088 	err = check_unshare_flags(unshare_flags);
3089 	if (err)
3090 		goto bad_unshare_out;
3091 	/*
3092 	 * CLONE_NEWIPC must also detach from the undolist: after switching
3093 	 * to a new ipc namespace, the semaphore arrays from the old
3094 	 * namespace are unreachable.
3095 	 */
3096 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
3097 		do_sysvsem = 1;
3098 	err = unshare_fs(unshare_flags, &new_fs);
3099 	if (err)
3100 		goto bad_unshare_out;
3101 	err = unshare_fd(unshare_flags, &new_fd);
3102 	if (err)
3103 		goto bad_unshare_cleanup_fs;
3104 	err = unshare_userns(unshare_flags, &new_cred);
3105 	if (err)
3106 		goto bad_unshare_cleanup_fd;
3107 	err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
3108 					 new_cred, new_fs);
3109 	if (err)
3110 		goto bad_unshare_cleanup_cred;
3111 
3112 	if (new_cred) {
3113 		err = set_cred_ucounts(new_cred);
3114 		if (err)
3115 			goto bad_unshare_cleanup_cred;
3116 	}
3117 
3118 	if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
3119 		if (do_sysvsem) {
3120 			/*
3121 			 * CLONE_SYSVSEM is equivalent to sys_exit().
3122 			 */
3123 			exit_sem(current);
3124 		}
3125 		if (unshare_flags & CLONE_NEWIPC) {
3126 			/* Orphan segments in old ns (see sem above). */
3127 			exit_shm(current);
3128 			shm_init_task(current);
3129 		}
3130 
3131 		if (new_nsproxy)
3132 			switch_task_namespaces(current, new_nsproxy);
3133 
3134 		task_lock(current);
3135 
3136 		if (new_fs) {
3137 			fs = current->fs;
3138 			spin_lock(&fs->lock);
3139 			current->fs = new_fs;
3140 			if (--fs->users)
3141 				new_fs = NULL;
3142 			else
3143 				new_fs = fs;
3144 			spin_unlock(&fs->lock);
3145 		}
3146 
3147 		if (new_fd)
3148 			swap(current->files, new_fd);
3149 
3150 		task_unlock(current);
3151 
3152 		if (new_cred) {
3153 			/* Install the new user namespace */
3154 			commit_creds(new_cred);
3155 			new_cred = NULL;
3156 		}
3157 	}
3158 
3159 	perf_event_namespaces(current);
3160 
3161 bad_unshare_cleanup_cred:
3162 	if (new_cred)
3163 		put_cred(new_cred);
3164 bad_unshare_cleanup_fd:
3165 	if (new_fd)
3166 		put_files_struct(new_fd);
3167 
3168 bad_unshare_cleanup_fs:
3169 	if (new_fs)
3170 		free_fs_struct(new_fs);
3171 
3172 bad_unshare_out:
3173 	return err;
3174 }
3175 
3176 SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
3177 {
3178 	return ksys_unshare(unshare_flags);
3179 }
3180 
3181 /*
3182  *	Helper to unshare the files of the current task.
3183  *	We don't want to expose copy_files internals to
3184  *	the exec layer of the kernel.
3185  */
3186 
3187 int unshare_files(void)
3188 {
3189 	struct task_struct *task = current;
3190 	struct files_struct *old, *copy = NULL;
3191 	int error;
3192 
3193 	error = unshare_fd(CLONE_FILES, &copy);
3194 	if (error || !copy)
3195 		return error;
3196 
3197 	old = task->files;
3198 	task_lock(task);
3199 	task->files = copy;
3200 	task_unlock(task);
3201 	put_files_struct(old);
3202 	return 0;
3203 }
3204 
3205 int sysctl_max_threads(const struct ctl_table *table, int write,
3206 		       void *buffer, size_t *lenp, loff_t *ppos)
3207 {
3208 	struct ctl_table t;
3209 	int ret;
3210 	int threads = max_threads;
3211 	int min = 1;
3212 	int max = MAX_THREADS;
3213 
3214 	t = *table;
3215 	t.data = &threads;
3216 	t.extra1 = &min;
3217 	t.extra2 = &max;
3218 
3219 	ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
3220 	if (ret || !write)
3221 		return ret;
3222 
3223 	max_threads = threads;
3224 
3225 	return 0;
3226 }
3227