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