Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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