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