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