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	= HLIST_HEAD_INIT,
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_list.node	= LIST_HEAD_INIT(init_task.scx.dsq_list.node),
106 		.sticky_cpu	= -1,
107 		.holding_cpu	= -1,
108 		.runnable_node	= LIST_HEAD_INIT(init_task.scx.runnable_node),
109 		.runnable_at	= INITIAL_JIFFIES,
110 		.ddsp_dsq_id	= SCX_DSQ_INVALID,
111 		.slice		= SCX_SLICE_DFL,
112 	},
113 #endif
114 	.ptraced	= LIST_HEAD_INIT(init_task.ptraced),
115 	.ptrace_entry	= LIST_HEAD_INIT(init_task.ptrace_entry),
116 	.real_parent	= &init_task,
117 	.parent		= &init_task,
118 	.children	= LIST_HEAD_INIT(init_task.children),
119 	.sibling	= LIST_HEAD_INIT(init_task.sibling),
120 	.group_leader	= &init_task,
121 	RCU_POINTER_INITIALIZER(real_cred, &init_cred),
122 	RCU_POINTER_INITIALIZER(cred, &init_cred),
123 	.comm		= INIT_TASK_COMM,
124 	.thread		= INIT_THREAD,
125 	.fs		= &init_fs,
126 	.files		= &init_files,
127 #ifdef CONFIG_IO_URING
128 	.io_uring	= NULL,
129 #endif
130 	.signal		= &init_signals,
131 	.sighand	= &init_sighand,
132 	.nsproxy	= &init_nsproxy,
133 	.pending	= {
134 		.list = LIST_HEAD_INIT(init_task.pending.list),
135 		.signal = {{0}}
136 	},
137 	.blocked	= {{0}},
138 	.alloc_lock	= __SPIN_LOCK_UNLOCKED(init_task.alloc_lock),
139 	.journal_info	= NULL,
140 	INIT_CPU_TIMERS(init_task)
141 	.pi_lock	= __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock),
142 	.timer_slack_ns = 50000, /* 50 usec default slack */
143 	.thread_pid	= &init_struct_pid,
144 	.thread_node	= LIST_HEAD_INIT(init_signals.thread_head),
145 #ifdef CONFIG_AUDIT
146 	.loginuid	= INVALID_UID,
147 	.sessionid	= AUDIT_SID_UNSET,
148 #endif
149 #ifdef CONFIG_PERF_EVENTS
150 	.perf_event_mutex = __MUTEX_INITIALIZER(init_task.perf_event_mutex),
151 	.perf_event_list = LIST_HEAD_INIT(init_task.perf_event_list),
152 #endif
153 #ifdef CONFIG_PREEMPT_RCU
154 	.rcu_read_lock_nesting = 0,
155 	.rcu_read_unlock_special.s = 0,
156 	.rcu_node_entry = LIST_HEAD_INIT(init_task.rcu_node_entry),
157 	.rcu_blocked_node = NULL,
158 #endif
159 #ifdef CONFIG_TASKS_RCU
160 	.rcu_tasks_holdout = false,
161 	.rcu_tasks_holdout_list = LIST_HEAD_INIT(init_task.rcu_tasks_holdout_list),
162 	.rcu_tasks_idle_cpu = -1,
163 	.rcu_tasks_exit_list = LIST_HEAD_INIT(init_task.rcu_tasks_exit_list),
164 #endif
165 #ifdef CONFIG_TASKS_TRACE_RCU
166 	.trc_reader_nesting = 0,
167 	.trc_reader_special.s = 0,
168 	.trc_holdout_list = LIST_HEAD_INIT(init_task.trc_holdout_list),
169 	.trc_blkd_node = LIST_HEAD_INIT(init_task.trc_blkd_node),
170 #endif
171 #ifdef CONFIG_CPUSETS
172 	.mems_allowed_seq = SEQCNT_SPINLOCK_ZERO(init_task.mems_allowed_seq,
173 						 &init_task.alloc_lock),
174 #endif
175 #ifdef CONFIG_RT_MUTEXES
176 	.pi_waiters	= RB_ROOT_CACHED,
177 	.pi_top_task	= NULL,
178 #endif
179 	INIT_PREV_CPUTIME(init_task)
180 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
181 	.vtime.seqcount	= SEQCNT_ZERO(init_task.vtime_seqcount),
182 	.vtime.starttime = 0,
183 	.vtime.state	= VTIME_SYS,
184 #endif
185 #ifdef CONFIG_NUMA_BALANCING
186 	.numa_preferred_nid = NUMA_NO_NODE,
187 	.numa_group	= NULL,
188 	.numa_faults	= NULL,
189 #endif
190 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
191 	.kasan_depth	= 1,
192 #endif
193 #ifdef CONFIG_KCSAN
194 	.kcsan_ctx = {
195 		.scoped_accesses	= {LIST_POISON1, NULL},
196 	},
197 #endif
198 #ifdef CONFIG_TRACE_IRQFLAGS
199 	.softirqs_enabled = 1,
200 #endif
201 #ifdef CONFIG_LOCKDEP
202 	.lockdep_depth = 0, /* no locks held yet */
203 	.curr_chain_key = INITIAL_CHAIN_KEY,
204 	.lockdep_recursion = 0,
205 #endif
206 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
207 	.ret_stack		= NULL,
208 	.tracing_graph_pause	= ATOMIC_INIT(0),
209 #endif
210 #if defined(CONFIG_TRACING) && defined(CONFIG_PREEMPTION)
211 	.trace_recursion = 0,
212 #endif
213 #ifdef CONFIG_LIVEPATCH
214 	.patch_state	= KLP_TRANSITION_IDLE,
215 #endif
216 #ifdef CONFIG_SECURITY
217 	.security	= NULL,
218 #endif
219 #ifdef CONFIG_SECCOMP_FILTER
220 	.seccomp	= { .filter_count = ATOMIC_INIT(0) },
221 #endif
222 };
223 EXPORT_SYMBOL(init_task);
224 
225 /*
226  * Initial thread structure. Alignment of this is handled by a special
227  * linker map entry.
228  */
229 #ifndef CONFIG_THREAD_INFO_IN_TASK
230 struct thread_info init_thread_info __init_thread_info = INIT_THREAD_INFO(init_task);
231 #endif
232