xref: /linux/init/init_task.c (revision 056e065a6b6e01ab54bb9770c0d5a15350e571e2)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init_task.h>
3 #include <linux/export.h>
4 #include <linux/mqueue.h>
5 #include <linux/sched.h>
6 #include <linux/sched/sysctl.h>
7 #include <linux/sched/rt.h>
8 #include <linux/sched/task.h>
9 #include <linux/sched/ext.h>
10 #include <linux/sched/exec_state.h>
11 #include <linux/user_namespace.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/mm.h>
15 #include <linux/audit.h>
16 #include <linux/numa.h>
17 #include <linux/scs.h>
18 #include <linux/plist.h>
19 
20 #include <linux/uaccess.h>
21 
22 static struct signal_struct init_signals = {
23 	.nr_threads	= 1,
24 	.thread_head	= LIST_HEAD_INIT(init_task.thread_node),
25 	.wait_chldexit	= __WAIT_QUEUE_HEAD_INITIALIZER(init_signals.wait_chldexit),
26 	.shared_pending	= {
27 		.list = LIST_HEAD_INIT(init_signals.shared_pending.list),
28 		.signal =  {{0}}
29 	},
30 	.multiprocess	= HLIST_HEAD_INIT,
31 	.rlim		= INIT_RLIMITS,
32 #ifdef CONFIG_CGROUPS
33 	.cgroup_threadgroup_rwsem	= __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem),
34 #endif
35 	.cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex),
36 	.exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock),
37 #ifdef CONFIG_POSIX_TIMERS
38 	.posix_timers		= HLIST_HEAD_INIT,
39 	.ignored_posix_timers	= HLIST_HEAD_INIT,
40 	.cputimer		= {
41 		.cputime_atomic	= INIT_CPUTIME_ATOMIC,
42 	},
43 #endif
44 	INIT_CPU_TIMERS(init_signals)
45 	.pids = {
46 		[PIDTYPE_PID]	= &init_struct_pid,
47 		[PIDTYPE_TGID]	= &init_struct_pid,
48 		[PIDTYPE_PGID]	= &init_struct_pid,
49 		[PIDTYPE_SID]	= &init_struct_pid,
50 	},
51 	INIT_PREV_CPUTIME(init_signals)
52 };
53 
54 static struct sighand_struct init_sighand = {
55 	.count		= REFCOUNT_INIT(1),
56 	.action		= { { { .sa_handler = SIG_DFL, } }, },
57 	.siglock	= __SPIN_LOCK_UNLOCKED(init_sighand.siglock),
58 	.signalfd_wqh	= __WAIT_QUEUE_HEAD_INITIALIZER(init_sighand.signalfd_wqh),
59 };
60 
61 /* init to 2 - one for init_task, one to ensure it is never freed */
62 struct task_exec_state init_task_exec_state = {
63 	.count		= REFCOUNT_INIT(2),
64 	.dumpable	= TASK_DUMPABLE_OWNER,
65 	.user_ns	= &init_user_ns,
66 };
67 
68 #ifdef CONFIG_SHADOW_CALL_STACK
69 unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)] = {
70 	[(SCS_SIZE / sizeof(long)) - 1] = SCS_END_MAGIC
71 };
72 #endif
73 
74 /* init to 2 - one for init_task, one to ensure it is never freed */
75 static struct group_info init_groups = { .usage = REFCOUNT_INIT(2) };
76 
77 /*
78  * The initial credentials for the initial task
79  */
80 static struct cred init_cred = {
81 	.usage			= ATOMIC_INIT(4),
82 	.uid			= GLOBAL_ROOT_UID,
83 	.gid			= GLOBAL_ROOT_GID,
84 	.suid			= GLOBAL_ROOT_UID,
85 	.sgid			= GLOBAL_ROOT_GID,
86 	.euid			= GLOBAL_ROOT_UID,
87 	.egid			= GLOBAL_ROOT_GID,
88 	.fsuid			= GLOBAL_ROOT_UID,
89 	.fsgid			= GLOBAL_ROOT_GID,
90 	.securebits		= SECUREBITS_DEFAULT,
91 	.cap_inheritable	= CAP_EMPTY_SET,
92 	.cap_permitted		= CAP_FULL_SET,
93 	.cap_effective		= CAP_FULL_SET,
94 	.cap_bset		= CAP_FULL_SET,
95 	.user			= INIT_USER,
96 	.user_ns		= &init_user_ns,
97 	.group_info		= &init_groups,
98 	.ucounts		= &init_ucounts,
99 };
100 
101 /*
102  * Set up the first task table, touch at your own risk!. Base=0,
103  * limit=0x1fffff (=2MB)
104  */
105 struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
106 #ifdef CONFIG_THREAD_INFO_IN_TASK
107 	.thread_info	= INIT_THREAD_INFO(init_task),
108 	.stack_refcount	= REFCOUNT_INIT(1),
109 #endif
110 	.__state	= 0,
111 	.stack		= init_stack,
112 	.usage		= REFCOUNT_INIT(2),
113 	.flags		= PF_KTHREAD,
114 	.prio		= MAX_PRIO - 20,
115 	.static_prio	= MAX_PRIO - 20,
116 	.normal_prio	= MAX_PRIO - 20,
117 	.policy		= SCHED_NORMAL,
118 	.cpus_ptr	= &init_task.cpus_mask,
119 	.user_cpus_ptr	= NULL,
120 	.cpus_mask	= CPU_MASK_ALL,
121 	.max_allowed_capacity	= SCHED_CAPACITY_SCALE,
122 	.nr_cpus_allowed= NR_CPUS,
123 	.mm		= NULL,
124 	.active_mm	= &init_mm,
125 	.exec_state	= &init_task_exec_state,
126 	.restart_block	= {
127 		.fn = do_no_restart_syscall,
128 	},
129 	.se		= {
130 		.group_node 	= LIST_HEAD_INIT(init_task.se.group_node),
131 	},
132 	.rt		= {
133 		.run_list	= LIST_HEAD_INIT(init_task.rt.run_list),
134 		.time_slice	= RR_TIMESLICE,
135 	},
136 	.tasks		= LIST_HEAD_INIT(init_task.tasks),
137 #ifdef CONFIG_SMP
138 	.pushable_tasks	= PLIST_NODE_INIT(init_task.pushable_tasks, MAX_PRIO),
139 #endif
140 #ifdef CONFIG_CGROUP_SCHED
141 	.sched_task_group = &root_task_group,
142 #endif
143 #ifdef CONFIG_SCHED_CLASS_EXT
144 	.scx		= {
145 		.dsq_list.node	= LIST_HEAD_INIT(init_task.scx.dsq_list.node),
146 		.sticky_cpu	= -1,
147 		.holding_cpu	= -1,
148 		.runnable_node	= LIST_HEAD_INIT(init_task.scx.runnable_node),
149 		.runnable_at	= INITIAL_JIFFIES,
150 		.ddsp_dsq_id	= SCX_DSQ_INVALID,
151 		.slice		= SCX_SLICE_DFL,
152 	},
153 #endif
154 	.ptraced	= LIST_HEAD_INIT(init_task.ptraced),
155 	.ptrace_entry	= LIST_HEAD_INIT(init_task.ptrace_entry),
156 	.real_parent	= &init_task,
157 	.parent		= &init_task,
158 	.children	= LIST_HEAD_INIT(init_task.children),
159 	.sibling	= LIST_HEAD_INIT(init_task.sibling),
160 	.group_leader	= &init_task,
161 	RCU_POINTER_INITIALIZER(real_cred, &init_cred),
162 	RCU_POINTER_INITIALIZER(cred, &init_cred),
163 	.comm		= INIT_TASK_COMM,
164 	.thread		= INIT_THREAD,
165 	.fs		= &init_fs,
166 	.files		= &init_files,
167 #ifdef CONFIG_IO_URING
168 	.io_uring	= NULL,
169 #endif
170 	.signal		= &init_signals,
171 	.sighand	= &init_sighand,
172 	.nsproxy	= &init_nsproxy,
173 	.pending	= {
174 		.list = LIST_HEAD_INIT(init_task.pending.list),
175 		.signal = {{0}}
176 	},
177 	.blocked	= {{0}},
178 	.alloc_lock	= __SPIN_LOCK_UNLOCKED(init_task.alloc_lock),
179 	.journal_info	= NULL,
180 	INIT_CPU_TIMERS(init_task)
181 	.pi_lock	= __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock),
182 	.blocked_lock	= __RAW_SPIN_LOCK_UNLOCKED(init_task.blocked_lock),
183 	.timer_slack_ns = 50000, /* 50 usec default slack */
184 	.thread_pid	= &init_struct_pid,
185 	.thread_node	= LIST_HEAD_INIT(init_signals.thread_head),
186 #ifdef CONFIG_AUDIT
187 	.loginuid	= INVALID_UID,
188 	.sessionid	= AUDIT_SID_UNSET,
189 #endif
190 #ifdef CONFIG_PERF_EVENTS
191 	.perf_event_mutex = __MUTEX_INITIALIZER(init_task.perf_event_mutex),
192 	.perf_event_list = LIST_HEAD_INIT(init_task.perf_event_list),
193 #endif
194 #ifdef CONFIG_PREEMPT_RCU
195 	.rcu_read_lock_nesting = 0,
196 	.rcu_read_unlock_special.s = 0,
197 	.rcu_node_entry = LIST_HEAD_INIT(init_task.rcu_node_entry),
198 	.rcu_blocked_node = NULL,
199 #endif
200 #ifdef CONFIG_TASKS_RCU
201 	.rcu_tasks_holdout = false,
202 	.rcu_tasks_holdout_list = LIST_HEAD_INIT(init_task.rcu_tasks_holdout_list),
203 	.rcu_tasks_idle_cpu = -1,
204 	.rcu_tasks_exit_list = LIST_HEAD_INIT(init_task.rcu_tasks_exit_list),
205 #endif
206 #ifdef CONFIG_TASKS_TRACE_RCU
207 	.trc_reader_nesting = 0,
208 #endif
209 #ifdef CONFIG_CPUSETS
210 	.mems_allowed_seq = SEQCNT_SPINLOCK_ZERO(init_task.mems_allowed_seq,
211 						 &init_task.alloc_lock),
212 #endif
213 	.blocked_donor = NULL,
214 #ifdef CONFIG_RT_MUTEXES
215 	.pi_waiters	= RB_ROOT_CACHED,
216 	.pi_top_task	= NULL,
217 #endif
218 	INIT_PREV_CPUTIME(init_task)
219 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
220 	.vtime.seqcount	= SEQCNT_ZERO(init_task.vtime_seqcount),
221 	.vtime.starttime = 0,
222 	.vtime.state	= VTIME_SYS,
223 #endif
224 #ifdef CONFIG_NUMA_BALANCING
225 	.numa_preferred_nid = NUMA_NO_NODE,
226 	.numa_group	= NULL,
227 	.numa_faults	= NULL,
228 #endif
229 #ifdef CONFIG_SCHED_CACHE
230 	.preferred_llc  = -1,
231 	.pref_llc_queued  = 0,
232 #endif
233 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
234 	.kasan_depth	= 1,
235 #endif
236 #ifdef CONFIG_KCSAN
237 	.kcsan_ctx = {
238 		.scoped_accesses	= {LIST_POISON1, NULL},
239 	},
240 #endif
241 #ifdef CONFIG_TRACE_IRQFLAGS
242 	.softirqs_enabled = 1,
243 #endif
244 #ifdef CONFIG_LOCKDEP
245 	.lockdep_depth = 0, /* no locks held yet */
246 	.curr_chain_key = INITIAL_CHAIN_KEY,
247 	.lockdep_recursion = 0,
248 #endif
249 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
250 	.ret_stack		= NULL,
251 	.tracing_graph_pause	= ATOMIC_INIT(0),
252 #endif
253 #if defined(CONFIG_TRACING) && defined(CONFIG_PREEMPTION)
254 	.trace_recursion = 0,
255 #endif
256 #ifdef CONFIG_LIVEPATCH
257 	.patch_state	= KLP_TRANSITION_IDLE,
258 #endif
259 #ifdef CONFIG_SECURITY
260 	.security	= NULL,
261 #endif
262 #ifdef CONFIG_SECCOMP_FILTER
263 	.seccomp	= { .filter_count = ATOMIC_INIT(0) },
264 #endif
265 #ifdef CONFIG_SCHED_MM_CID
266 	.mm_cid		= { .cid = MM_CID_UNSET, },
267 #endif
268 };
269 EXPORT_SYMBOL(init_task);
270 
271 /*
272  * Initial thread structure. Alignment of this is handled by a special
273  * linker map entry.
274  */
275 #ifndef CONFIG_THREAD_INFO_IN_TASK
276 struct thread_info init_thread_info __init_thread_info = INIT_THREAD_INFO(init_task);
277 #endif
278