init_task.c 7.3 KB

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