xref: /linux/init/main.c (revision 136114e0abf03005e182d75761ab694648e6d388)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/init/main.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  *
7  *  GK 2/5/95  -  Changed to support mounting root fs via NFS
8  *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
9  *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
10  *  Simplified starting of init:  Michael A. Griffith <grif@acm.org>
11  */
12 
13 #define DEBUG		/* Enable initcall_debug */
14 
15 #include <linux/types.h>
16 #include <linux/export.h>
17 #include <linux/extable.h>
18 #include <linux/module.h>
19 #include <linux/proc_fs.h>
20 #include <linux/binfmts.h>
21 #include <linux/kernel.h>
22 #include <linux/syscalls.h>
23 #include <linux/stackprotector.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/delay.h>
27 #include <linux/ioport.h>
28 #include <linux/init.h>
29 #include <linux/initrd.h>
30 #include <linux/memblock.h>
31 #include <linux/acpi.h>
32 #include <linux/bootconfig.h>
33 #include <linux/console.h>
34 #include <linux/nmi.h>
35 #include <linux/percpu.h>
36 #include <linux/kmod.h>
37 #include <linux/kprobes.h>
38 #include <linux/kmsan.h>
39 #include <linux/vmalloc.h>
40 #include <linux/kernel_stat.h>
41 #include <linux/start_kernel.h>
42 #include <linux/security.h>
43 #include <linux/smp.h>
44 #include <linux/profile.h>
45 #include <linux/kfence.h>
46 #include <linux/rcupdate.h>
47 #include <linux/srcu.h>
48 #include <linux/moduleparam.h>
49 #include <linux/kallsyms.h>
50 #include <linux/buildid.h>
51 #include <linux/writeback.h>
52 #include <linux/cpu.h>
53 #include <linux/cpuset.h>
54 #include <linux/memcontrol.h>
55 #include <linux/cgroup.h>
56 #include <linux/tick.h>
57 #include <linux/sched/isolation.h>
58 #include <linux/interrupt.h>
59 #include <linux/taskstats_kern.h>
60 #include <linux/delayacct.h>
61 #include <linux/unistd.h>
62 #include <linux/utsname.h>
63 #include <linux/rmap.h>
64 #include <linux/mempolicy.h>
65 #include <linux/key.h>
66 #include <linux/debug_locks.h>
67 #include <linux/debugobjects.h>
68 #include <linux/lockdep.h>
69 #include <linux/kmemleak.h>
70 #include <linux/padata.h>
71 #include <linux/pid_namespace.h>
72 #include <linux/device/driver.h>
73 #include <linux/kthread.h>
74 #include <linux/sched.h>
75 #include <linux/sched/init.h>
76 #include <linux/signal.h>
77 #include <linux/idr.h>
78 #include <linux/kgdb.h>
79 #include <linux/ftrace.h>
80 #include <linux/async.h>
81 #include <linux/shmem_fs.h>
82 #include <linux/slab.h>
83 #include <linux/perf_event.h>
84 #include <linux/ptrace.h>
85 #include <linux/pti.h>
86 #include <linux/blkdev.h>
87 #include <linux/sched/clock.h>
88 #include <linux/sched/task.h>
89 #include <linux/sched/task_stack.h>
90 #include <linux/context_tracking.h>
91 #include <linux/random.h>
92 #include <linux/moduleloader.h>
93 #include <linux/list.h>
94 #include <linux/integrity.h>
95 #include <linux/proc_ns.h>
96 #include <linux/io.h>
97 #include <linux/cache.h>
98 #include <linux/rodata_test.h>
99 #include <linux/jump_label.h>
100 #include <linux/kcsan.h>
101 #include <linux/init_syscalls.h>
102 #include <linux/stackdepot.h>
103 #include <linux/randomize_kstack.h>
104 #include <linux/pidfs.h>
105 #include <linux/ptdump.h>
106 #include <linux/time_namespace.h>
107 #include <linux/unaligned.h>
108 #include <net/net_namespace.h>
109 
110 #include <asm/io.h>
111 #include <asm/setup.h>
112 #include <asm/sections.h>
113 #include <asm/cacheflush.h>
114 
115 #define CREATE_TRACE_POINTS
116 #include <trace/events/initcall.h>
117 
118 #include <kunit/test.h>
119 
120 static int kernel_init(void *);
121 
122 /*
123  * Debug helper: via this flag we know that we are in 'early bootup code'
124  * where only the boot processor is running with IRQ disabled.  This means
125  * two things - IRQ must not be enabled before the flag is cleared and some
126  * operations which are not allowed with IRQ disabled are allowed while the
127  * flag is set.
128  */
129 bool early_boot_irqs_disabled __read_mostly;
130 
131 enum system_states system_state __read_mostly;
132 EXPORT_SYMBOL(system_state);
133 
134 /*
135  * Boot command-line arguments
136  */
137 #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
138 #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
139 
140 /* Default late time init is NULL. archs can override this later. */
141 void (*__initdata late_time_init)(void);
142 
143 /* Untouched command line saved by arch-specific code. */
144 char __initdata boot_command_line[COMMAND_LINE_SIZE];
145 /* Untouched saved command line (eg. for /proc) */
146 char *saved_command_line __ro_after_init;
147 unsigned int saved_command_line_len __ro_after_init;
148 /* Command line for parameter parsing */
149 static char *static_command_line;
150 /* Untouched extra command line */
151 static char *extra_command_line;
152 /* Extra init arguments */
153 static char *extra_init_args;
154 
155 #ifdef CONFIG_BOOT_CONFIG
156 /* Is bootconfig on command line? */
157 static bool bootconfig_found;
158 static size_t initargs_offs;
159 #else
160 # define bootconfig_found false
161 # define initargs_offs 0
162 #endif
163 
164 static char *execute_command;
165 static char *ramdisk_execute_command = "/init";
166 static bool __initdata ramdisk_execute_command_set;
167 
168 /*
169  * Used to generate warnings if static_key manipulation functions are used
170  * before jump_label_init is called.
171  */
172 bool static_key_initialized __read_mostly;
173 EXPORT_SYMBOL_GPL(static_key_initialized);
174 
175 /*
176  * If set, this is an indication to the drivers that reset the underlying
177  * device before going ahead with the initialization otherwise driver might
178  * rely on the BIOS and skip the reset operation.
179  *
180  * This is useful if kernel is booting in an unreliable environment.
181  * For ex. kdump situation where previous kernel has crashed, BIOS has been
182  * skipped and devices will be in unknown state.
183  */
184 unsigned int reset_devices;
185 EXPORT_SYMBOL(reset_devices);
186 
187 static int __init set_reset_devices(char *str)
188 {
189 	reset_devices = 1;
190 	return 1;
191 }
192 
193 __setup("reset_devices", set_reset_devices);
194 
195 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
196 const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
197 static const char *panic_later, *panic_param;
198 
199 static bool __init obsolete_checksetup(char *line)
200 {
201 	const struct obs_kernel_param *p;
202 	bool had_early_param = false;
203 
204 	p = __setup_start;
205 	do {
206 		int n = strlen(p->str);
207 		if (parameqn(line, p->str, n)) {
208 			if (p->early) {
209 				/* Already done in parse_early_param?
210 				 * (Needs exact match on param part).
211 				 * Keep iterating, as we can have early
212 				 * params and __setups of same names 8( */
213 				if (line[n] == '\0' || line[n] == '=')
214 					had_early_param = true;
215 			} else if (!p->setup_func) {
216 				pr_warn("Parameter %s is obsolete, ignored\n",
217 					p->str);
218 				return true;
219 			} else if (p->setup_func(line + n))
220 				return true;
221 		}
222 		p++;
223 	} while (p < __setup_end);
224 
225 	return had_early_param;
226 }
227 
228 /*
229  * This should be approx 2 Bo*oMips to start (note initial shift), and will
230  * still work even if initially too large, it will just take slightly longer
231  */
232 unsigned long loops_per_jiffy = (1<<12);
233 EXPORT_SYMBOL(loops_per_jiffy);
234 
235 static int __init debug_kernel(char *str)
236 {
237 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
238 	return 0;
239 }
240 
241 static int __init quiet_kernel(char *str)
242 {
243 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
244 	return 0;
245 }
246 
247 early_param("debug", debug_kernel);
248 early_param("quiet", quiet_kernel);
249 
250 static int __init loglevel(char *str)
251 {
252 	int newlevel;
253 
254 	/*
255 	 * Only update loglevel value when a correct setting was passed,
256 	 * to prevent blind crashes (when loglevel being set to 0) that
257 	 * are quite hard to debug
258 	 */
259 	if (get_option(&str, &newlevel)) {
260 		console_loglevel = newlevel;
261 		return 0;
262 	}
263 
264 	return -EINVAL;
265 }
266 
267 early_param("loglevel", loglevel);
268 
269 #ifdef CONFIG_BLK_DEV_INITRD
270 static void * __init get_boot_config_from_initrd(size_t *_size)
271 {
272 	u32 size, csum;
273 	char *data;
274 	u8 *hdr;
275 	int i;
276 
277 	if (!initrd_end)
278 		return NULL;
279 
280 	data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
281 	/*
282 	 * Since Grub may align the size of initrd to 4, we must
283 	 * check the preceding 3 bytes as well.
284 	 */
285 	for (i = 0; i < 4; i++) {
286 		if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
287 			goto found;
288 		data--;
289 	}
290 	return NULL;
291 
292 found:
293 	hdr = (u8 *)(data - 8);
294 	size = get_unaligned_le32(hdr);
295 	csum = get_unaligned_le32(hdr + 4);
296 
297 	data = ((void *)hdr) - size;
298 	if ((unsigned long)data < initrd_start) {
299 		pr_err("bootconfig size %d is greater than initrd size %ld\n",
300 			size, initrd_end - initrd_start);
301 		return NULL;
302 	}
303 
304 	if (xbc_calc_checksum(data, size) != csum) {
305 		pr_err("bootconfig checksum failed\n");
306 		return NULL;
307 	}
308 
309 	/* Remove bootconfig from initramfs/initrd */
310 	initrd_end = (unsigned long)data;
311 	if (_size)
312 		*_size = size;
313 
314 	return data;
315 }
316 #else
317 static void * __init get_boot_config_from_initrd(size_t *_size)
318 {
319 	return NULL;
320 }
321 #endif
322 
323 #ifdef CONFIG_BOOT_CONFIG
324 
325 static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
326 
327 #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
328 
329 static int __init xbc_snprint_cmdline(char *buf, size_t size,
330 				      struct xbc_node *root)
331 {
332 	struct xbc_node *knode, *vnode;
333 	char *end = buf + size;
334 	const char *val, *q;
335 	int ret;
336 
337 	xbc_node_for_each_key_value(root, knode, val) {
338 		ret = xbc_node_compose_key_after(root, knode,
339 					xbc_namebuf, XBC_KEYLEN_MAX);
340 		if (ret < 0)
341 			return ret;
342 
343 		vnode = xbc_node_get_child(knode);
344 		if (!vnode) {
345 			ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
346 			if (ret < 0)
347 				return ret;
348 			buf += ret;
349 			continue;
350 		}
351 		xbc_array_for_each_value(vnode, val) {
352 			/*
353 			 * For prettier and more readable /proc/cmdline, only
354 			 * quote the value when necessary, i.e. when it contains
355 			 * whitespace.
356 			 */
357 			q = strpbrk(val, " \t\r\n") ? "\"" : "";
358 			ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
359 				       xbc_namebuf, q, val, q);
360 			if (ret < 0)
361 				return ret;
362 			buf += ret;
363 		}
364 	}
365 
366 	return buf - (end - size);
367 }
368 #undef rest
369 
370 /* Make an extra command line under given key word */
371 static char * __init xbc_make_cmdline(const char *key)
372 {
373 	struct xbc_node *root;
374 	char *new_cmdline;
375 	int ret, len = 0;
376 
377 	root = xbc_find_node(key);
378 	if (!root)
379 		return NULL;
380 
381 	/* Count required buffer size */
382 	len = xbc_snprint_cmdline(NULL, 0, root);
383 	if (len <= 0)
384 		return NULL;
385 
386 	new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
387 	if (!new_cmdline) {
388 		pr_err("Failed to allocate memory for extra kernel cmdline.\n");
389 		return NULL;
390 	}
391 
392 	ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
393 	if (ret < 0 || ret > len) {
394 		pr_err("Failed to print extra kernel cmdline.\n");
395 		memblock_free(new_cmdline, len + 1);
396 		return NULL;
397 	}
398 
399 	return new_cmdline;
400 }
401 
402 static int __init bootconfig_params(char *param, char *val,
403 				    const char *unused, void *arg)
404 {
405 	if (strcmp(param, "bootconfig") == 0) {
406 		bootconfig_found = true;
407 	}
408 	return 0;
409 }
410 
411 static int __init warn_bootconfig(char *str)
412 {
413 	/* The 'bootconfig' has been handled by bootconfig_params(). */
414 	return 0;
415 }
416 
417 static void __init setup_boot_config(void)
418 {
419 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
420 	const char *msg, *data;
421 	int pos, ret;
422 	size_t size;
423 	char *err;
424 
425 	/* Cut out the bootconfig data even if we have no bootconfig option */
426 	data = get_boot_config_from_initrd(&size);
427 	/* If there is no bootconfig in initrd, try embedded one. */
428 	if (!data)
429 		data = xbc_get_embedded_bootconfig(&size);
430 
431 	strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
432 	err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL,
433 			 bootconfig_params);
434 
435 	if (IS_ERR(err) || !(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE)))
436 		return;
437 
438 	/* parse_args() stops at the next param of '--' and returns an address */
439 	if (err)
440 		initargs_offs = err - tmp_cmdline;
441 
442 	if (!data) {
443 		/* If user intended to use bootconfig, show an error level message */
444 		if (bootconfig_found)
445 			pr_err("'bootconfig' found on command line, but no bootconfig found\n");
446 		else
447 			pr_info("No bootconfig data provided, so skipping bootconfig");
448 		return;
449 	}
450 
451 	if (size >= XBC_DATA_MAX) {
452 		pr_err("bootconfig size %ld greater than max size %d\n",
453 			(long)size, XBC_DATA_MAX);
454 		return;
455 	}
456 
457 	ret = xbc_init(data, size, &msg, &pos);
458 	if (ret < 0) {
459 		if (pos < 0)
460 			pr_err("Failed to init bootconfig: %s.\n", msg);
461 		else
462 			pr_err("Failed to parse bootconfig: %s at %d.\n",
463 				msg, pos);
464 	} else {
465 		xbc_get_info(&ret, NULL);
466 		pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret);
467 		/* keys starting with "kernel." are passed via cmdline */
468 		extra_command_line = xbc_make_cmdline("kernel");
469 		/* Also, "init." keys are init arguments */
470 		extra_init_args = xbc_make_cmdline("init");
471 	}
472 	return;
473 }
474 
475 static void __init exit_boot_config(void)
476 {
477 	xbc_exit();
478 }
479 
480 #else	/* !CONFIG_BOOT_CONFIG */
481 
482 static void __init setup_boot_config(void)
483 {
484 	/* Remove bootconfig data from initrd */
485 	get_boot_config_from_initrd(NULL);
486 }
487 
488 static int __init warn_bootconfig(char *str)
489 {
490 	pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n");
491 	return 0;
492 }
493 
494 #define exit_boot_config()	do {} while (0)
495 
496 #endif	/* CONFIG_BOOT_CONFIG */
497 
498 early_param("bootconfig", warn_bootconfig);
499 
500 bool __init cmdline_has_extra_options(void)
501 {
502 	return extra_command_line || extra_init_args;
503 }
504 
505 /* Change NUL term back to "=", to make "param" the whole string. */
506 static void __init repair_env_string(char *param, char *val)
507 {
508 	if (val) {
509 		/* param=val or param="val"? */
510 		if (val == param+strlen(param)+1)
511 			val[-1] = '=';
512 		else if (val == param+strlen(param)+2) {
513 			val[-2] = '=';
514 			memmove(val-1, val, strlen(val)+1);
515 		} else
516 			BUG();
517 	}
518 }
519 
520 /* Anything after -- gets handed straight to init. */
521 static int __init set_init_arg(char *param, char *val,
522 			       const char *unused, void *arg)
523 {
524 	unsigned int i;
525 
526 	if (panic_later)
527 		return 0;
528 
529 	repair_env_string(param, val);
530 
531 	for (i = 0; argv_init[i]; i++) {
532 		if (i == MAX_INIT_ARGS) {
533 			panic_later = "init";
534 			panic_param = param;
535 			return 0;
536 		}
537 	}
538 	argv_init[i] = param;
539 	return 0;
540 }
541 
542 /*
543  * Unknown boot options get handed to init, unless they look like
544  * unused parameters (modprobe will find them in /proc/cmdline).
545  */
546 static int __init unknown_bootoption(char *param, char *val,
547 				     const char *unused, void *arg)
548 {
549 	size_t len = strlen(param);
550 	/*
551 	 * Well-known bootloader identifiers:
552 	 * 1. LILO/Grub pass "BOOT_IMAGE=...";
553 	 * 2. kexec/kdump (kexec-tools) pass "kexec".
554 	 */
555 	const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
556 
557 	/* Handle params aliased to sysctls */
558 	if (sysctl_is_alias(param))
559 		return 0;
560 
561 	repair_env_string(param, val);
562 
563 	/* Handle bootloader identifier */
564 	for (int i = 0; bootloader[i]; i++) {
565 		if (strstarts(param, bootloader[i]))
566 			return 0;
567 	}
568 
569 	/* Handle obsolete-style parameters */
570 	if (obsolete_checksetup(param))
571 		return 0;
572 
573 	/* Unused module parameter. */
574 	if (strnchr(param, len, '.'))
575 		return 0;
576 
577 	if (panic_later)
578 		return 0;
579 
580 	if (val) {
581 		/* Environment option */
582 		unsigned int i;
583 		for (i = 0; envp_init[i]; i++) {
584 			if (i == MAX_INIT_ENVS) {
585 				panic_later = "env";
586 				panic_param = param;
587 			}
588 			if (!strncmp(param, envp_init[i], len+1))
589 				break;
590 		}
591 		envp_init[i] = param;
592 	} else {
593 		/* Command line option */
594 		unsigned int i;
595 		for (i = 0; argv_init[i]; i++) {
596 			if (i == MAX_INIT_ARGS) {
597 				panic_later = "init";
598 				panic_param = param;
599 			}
600 		}
601 		argv_init[i] = param;
602 	}
603 	return 0;
604 }
605 
606 static int __init init_setup(char *str)
607 {
608 	unsigned int i;
609 
610 	execute_command = str;
611 	/*
612 	 * In case LILO is going to boot us with default command line,
613 	 * it prepends "auto" before the whole cmdline which makes
614 	 * the shell think it should execute a script with such name.
615 	 * So we ignore all arguments entered _before_ init=... [MJ]
616 	 */
617 	for (i = 1; i < MAX_INIT_ARGS; i++)
618 		argv_init[i] = NULL;
619 	return 1;
620 }
621 __setup("init=", init_setup);
622 
623 static int __init rdinit_setup(char *str)
624 {
625 	unsigned int i;
626 
627 	ramdisk_execute_command = str;
628 	ramdisk_execute_command_set = true;
629 	/* See "auto" comment in init_setup */
630 	for (i = 1; i < MAX_INIT_ARGS; i++)
631 		argv_init[i] = NULL;
632 	return 1;
633 }
634 __setup("rdinit=", rdinit_setup);
635 
636 #ifndef CONFIG_SMP
637 static inline void setup_nr_cpu_ids(void) { }
638 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
639 #endif
640 
641 /*
642  * We need to store the untouched command line for future reference.
643  * We also need to store the touched command line since the parameter
644  * parsing is performed in place, and we should allow a component to
645  * store reference of name/value for future reference.
646  */
647 static void __init setup_command_line(char *command_line)
648 {
649 	size_t len, xlen = 0, ilen = 0;
650 
651 	if (extra_command_line)
652 		xlen = strlen(extra_command_line);
653 	if (extra_init_args) {
654 		extra_init_args = strim(extra_init_args); /* remove trailing space */
655 		ilen = strlen(extra_init_args) + 4; /* for " -- " */
656 	}
657 
658 	len = xlen + strlen(boot_command_line) + ilen + 1;
659 
660 	saved_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
661 
662 	len = xlen + strlen(command_line) + 1;
663 
664 	static_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
665 
666 	if (xlen) {
667 		/*
668 		 * We have to put extra_command_line before boot command
669 		 * lines because there could be dashes (separator of init
670 		 * command line) in the command lines.
671 		 */
672 		strcpy(saved_command_line, extra_command_line);
673 		strcpy(static_command_line, extra_command_line);
674 	}
675 	strcpy(saved_command_line + xlen, boot_command_line);
676 	strcpy(static_command_line + xlen, command_line);
677 
678 	if (ilen) {
679 		/*
680 		 * Append supplemental init boot args to saved_command_line
681 		 * so that user can check what command line options passed
682 		 * to init.
683 		 * The order should always be
684 		 * " -- "[bootconfig init-param][cmdline init-param]
685 		 */
686 		if (initargs_offs) {
687 			len = xlen + initargs_offs;
688 			strcpy(saved_command_line + len, extra_init_args);
689 			len += ilen - 4;	/* strlen(extra_init_args) */
690 			strcpy(saved_command_line + len,
691 				boot_command_line + initargs_offs - 1);
692 		} else {
693 			len = strlen(saved_command_line);
694 			strcpy(saved_command_line + len, " -- ");
695 			len += 4;
696 			strcpy(saved_command_line + len, extra_init_args);
697 		}
698 	}
699 
700 	saved_command_line_len = strlen(saved_command_line);
701 }
702 
703 /*
704  * We need to finalize in a non-__init function or else race conditions
705  * between the root thread and the init thread may cause start_kernel to
706  * be reaped by free_initmem before the root thread has proceeded to
707  * cpu_idle.
708  *
709  * gcc-3.4 accidentally inlines this function, so use noinline.
710  */
711 
712 static __initdata DECLARE_COMPLETION(kthreadd_done);
713 
714 static noinline void __ref __noreturn rest_init(void)
715 {
716 	struct task_struct *tsk;
717 	int pid;
718 
719 	rcu_scheduler_starting();
720 	/*
721 	 * We need to spawn init first so that it obtains pid 1, however
722 	 * the init task will end up wanting to create kthreads, which, if
723 	 * we schedule it before we create kthreadd, will OOPS.
724 	 */
725 	pid = user_mode_thread(kernel_init, NULL, CLONE_FS);
726 	/*
727 	 * Pin init on the boot CPU. Task migration is not properly working
728 	 * until sched_init_smp() has been run. It will set the allowed
729 	 * CPUs for init to the non isolated CPUs.
730 	 */
731 	rcu_read_lock();
732 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
733 	tsk->flags |= PF_NO_SETAFFINITY;
734 	set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
735 	rcu_read_unlock();
736 
737 	numa_default_policy();
738 	pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES);
739 	rcu_read_lock();
740 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
741 	rcu_read_unlock();
742 
743 	/*
744 	 * Enable might_sleep() and smp_processor_id() checks.
745 	 * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
746 	 * kernel_thread() would trigger might_sleep() splats. With
747 	 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
748 	 * already, but it's stuck on the kthreadd_done completion.
749 	 */
750 	system_state = SYSTEM_SCHEDULING;
751 
752 	complete(&kthreadd_done);
753 
754 	/*
755 	 * The boot idle thread must execute schedule()
756 	 * at least once to get things moving:
757 	 */
758 	schedule_preempt_disabled();
759 	/* Call into cpu_idle with preempt disabled */
760 	cpu_startup_entry(CPUHP_ONLINE);
761 }
762 
763 /* Check for early params. */
764 static int __init do_early_param(char *param, char *val,
765 				 const char *unused, void *arg)
766 {
767 	const struct obs_kernel_param *p;
768 
769 	for (p = __setup_start; p < __setup_end; p++) {
770 		if (p->early && parameq(param, p->str)) {
771 			if (p->setup_func(val) != 0)
772 				pr_warn("Malformed early option '%s'\n", param);
773 		}
774 	}
775 	/* We accept everything at this stage. */
776 	return 0;
777 }
778 
779 void __init parse_early_options(char *cmdline)
780 {
781 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
782 		   do_early_param);
783 }
784 
785 /* Arch code calls this early on, or if not, just before other parsing. */
786 void __init parse_early_param(void)
787 {
788 	static int done __initdata;
789 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
790 
791 	if (done)
792 		return;
793 
794 	/* All fall through to do_early_param. */
795 	strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
796 	parse_early_options(tmp_cmdline);
797 	done = 1;
798 }
799 
800 void __init __weak arch_post_acpi_subsys_init(void) { }
801 
802 void __init __weak smp_setup_processor_id(void)
803 {
804 }
805 
806 void __init __weak smp_prepare_boot_cpu(void)
807 {
808 }
809 
810 # if THREAD_SIZE >= PAGE_SIZE
811 void __init __weak thread_stack_cache_init(void)
812 {
813 }
814 #endif
815 
816 void __init __weak poking_init(void) { }
817 
818 void __init __weak pgtable_cache_init(void) { }
819 
820 void __init __weak trap_init(void) { }
821 
822 bool initcall_debug;
823 core_param(initcall_debug, initcall_debug, bool, 0644);
824 
825 #ifdef TRACEPOINTS_ENABLED
826 static void __init initcall_debug_enable(void);
827 #else
828 static inline void initcall_debug_enable(void)
829 {
830 }
831 #endif
832 
833 #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
834 DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
835 			   randomize_kstack_offset);
836 DEFINE_PER_CPU(u32, kstack_offset);
837 
838 static int __init early_randomize_kstack_offset(char *buf)
839 {
840 	int ret;
841 	bool bool_result;
842 
843 	ret = kstrtobool(buf, &bool_result);
844 	if (ret)
845 		return ret;
846 
847 	if (bool_result)
848 		static_branch_enable(&randomize_kstack_offset);
849 	else
850 		static_branch_disable(&randomize_kstack_offset);
851 	return 0;
852 }
853 early_param("randomize_kstack_offset", early_randomize_kstack_offset);
854 #endif
855 
856 static void __init print_unknown_bootoptions(void)
857 {
858 	char *unknown_options;
859 	char *end;
860 	const char *const *p;
861 	size_t len;
862 
863 	if (panic_later || (!argv_init[1] && !envp_init[2]))
864 		return;
865 
866 	/*
867 	 * Determine how many options we have to print out, plus a space
868 	 * before each
869 	 */
870 	len = 1; /* null terminator */
871 	for (p = &argv_init[1]; *p; p++) {
872 		len++;
873 		len += strlen(*p);
874 	}
875 	for (p = &envp_init[2]; *p; p++) {
876 		len++;
877 		len += strlen(*p);
878 	}
879 
880 	unknown_options = memblock_alloc(len, SMP_CACHE_BYTES);
881 	if (!unknown_options) {
882 		pr_err("%s: Failed to allocate %zu bytes\n",
883 			__func__, len);
884 		return;
885 	}
886 	end = unknown_options;
887 
888 	for (p = &argv_init[1]; *p; p++)
889 		end += sprintf(end, " %s", *p);
890 	for (p = &envp_init[2]; *p; p++)
891 		end += sprintf(end, " %s", *p);
892 
893 	/* Start at unknown_options[1] to skip the initial space */
894 	pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n",
895 		&unknown_options[1]);
896 	memblock_free(unknown_options, len);
897 }
898 
899 static void __init early_numa_node_init(void)
900 {
901 #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
902 #ifndef cpu_to_node
903 	int cpu;
904 
905 	/* The early_cpu_to_node() should be ready here. */
906 	for_each_possible_cpu(cpu)
907 		set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
908 #endif
909 #endif
910 }
911 
912 #define KERNEL_CMDLINE_PREFIX		"Kernel command line: "
913 #define KERNEL_CMDLINE_PREFIX_LEN	(sizeof(KERNEL_CMDLINE_PREFIX) - 1)
914 #define KERNEL_CMDLINE_CONTINUATION	" \\"
915 #define KERNEL_CMDLINE_CONTINUATION_LEN	(sizeof(KERNEL_CMDLINE_CONTINUATION) - 1)
916 
917 #define MIN_CMDLINE_LOG_WRAP_IDEAL_LEN	(KERNEL_CMDLINE_PREFIX_LEN + \
918 					 KERNEL_CMDLINE_CONTINUATION_LEN)
919 #define CMDLINE_LOG_WRAP_IDEAL_LEN	(CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN > \
920 					 MIN_CMDLINE_LOG_WRAP_IDEAL_LEN ? \
921 					 CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN : \
922 					 MIN_CMDLINE_LOG_WRAP_IDEAL_LEN)
923 
924 #define IDEAL_CMDLINE_LEN		(CMDLINE_LOG_WRAP_IDEAL_LEN - KERNEL_CMDLINE_PREFIX_LEN)
925 #define IDEAL_CMDLINE_SPLIT_LEN		(IDEAL_CMDLINE_LEN - KERNEL_CMDLINE_CONTINUATION_LEN)
926 
927 /**
928  * print_kernel_cmdline() - Print the kernel cmdline with wrapping.
929  * @cmdline: The cmdline to print.
930  *
931  * Print the kernel command line, trying to wrap based on the Kconfig knob
932  * CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN.
933  *
934  * Wrapping is based on spaces, ignoring quotes. All lines are prefixed
935  * with "Kernel command line: " and lines that are not the last line have
936  * a " \" suffix added to them. The prefix and suffix count towards the
937  * line length for wrapping purposes. The ideal length will be exceeded
938  * if no appropriate place to wrap is found.
939  *
940  * Example output if CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN is 40:
941  *   Kernel command line: loglevel=7 \
942  *   Kernel command line: init=/sbin/init \
943  *   Kernel command line: root=PARTUUID=8c3efc1a-768b-6642-8d0c-89eb782f19f0/PARTNROFF=1 \
944  *   Kernel command line: rootwait ro \
945  *   Kernel command line: my_quoted_arg="The \
946  *   Kernel command line: quick brown fox \
947  *   Kernel command line: jumps over the \
948  *   Kernel command line: lazy dog."
949  */
950 static void __init print_kernel_cmdline(const char *cmdline)
951 {
952 	size_t len;
953 
954 	/* Config option of 0 or anything longer than the max disables wrapping */
955 	if (CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN == 0 ||
956 	    IDEAL_CMDLINE_LEN >= COMMAND_LINE_SIZE - 1) {
957 		pr_notice("%s%s\n", KERNEL_CMDLINE_PREFIX, cmdline);
958 		return;
959 	}
960 
961 	len = strlen(cmdline);
962 	while (len > IDEAL_CMDLINE_LEN) {
963 		const char *first_space;
964 		const char *prev_cutoff;
965 		const char *cutoff;
966 		int to_print;
967 		size_t used;
968 
969 		/* Find the last ' ' that wouldn't make the line too long */
970 		prev_cutoff = NULL;
971 		cutoff = cmdline;
972 		while (true) {
973 			cutoff = strchr(cutoff + 1, ' ');
974 			if (!cutoff || cutoff - cmdline > IDEAL_CMDLINE_SPLIT_LEN)
975 				break;
976 			prev_cutoff = cutoff;
977 		}
978 		if (prev_cutoff)
979 			cutoff = prev_cutoff;
980 		else if (!cutoff)
981 			break;
982 
983 		/* Find the beginning and end of the string of spaces */
984 		first_space = cutoff;
985 		while (first_space > cmdline && first_space[-1] == ' ')
986 			first_space--;
987 		to_print = first_space - cmdline;
988 		while (*cutoff == ' ')
989 			cutoff++;
990 		used = cutoff - cmdline;
991 
992 		/* If the whole string is used, break and do the final printout */
993 		if (len == used)
994 			break;
995 
996 		if (to_print)
997 			pr_notice("%s%.*s%s\n", KERNEL_CMDLINE_PREFIX,
998 				  to_print, cmdline, KERNEL_CMDLINE_CONTINUATION);
999 
1000 		len -= used;
1001 		cmdline += used;
1002 	}
1003 	if (len)
1004 		pr_notice("%s%s\n", KERNEL_CMDLINE_PREFIX, cmdline);
1005 }
1006 
1007 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
1008 void start_kernel(void)
1009 {
1010 	char *command_line;
1011 	char *after_dashes;
1012 
1013 	set_task_stack_end_magic(&init_task);
1014 	smp_setup_processor_id();
1015 	debug_objects_early_init();
1016 	init_vmlinux_build_id();
1017 
1018 	cgroup_init_early();
1019 
1020 	local_irq_disable();
1021 	early_boot_irqs_disabled = true;
1022 
1023 	/*
1024 	 * Interrupts are still disabled. Do necessary setups, then
1025 	 * enable them.
1026 	 */
1027 	boot_cpu_init();
1028 	page_address_init();
1029 	pr_notice("%s", linux_banner);
1030 	setup_arch(&command_line);
1031 	mm_core_init_early();
1032 	/* Static keys and static calls are needed by LSMs */
1033 	jump_label_init();
1034 	static_call_init();
1035 	early_security_init();
1036 	setup_boot_config();
1037 	setup_command_line(command_line);
1038 	setup_nr_cpu_ids();
1039 	setup_per_cpu_areas();
1040 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
1041 	early_numa_node_init();
1042 	boot_cpu_hotplug_init();
1043 
1044 	print_kernel_cmdline(saved_command_line);
1045 	/* parameters may set static keys */
1046 	parse_early_param();
1047 	after_dashes = parse_args("Booting kernel",
1048 				  static_command_line, __start___param,
1049 				  __stop___param - __start___param,
1050 				  -1, -1, NULL, &unknown_bootoption);
1051 	print_unknown_bootoptions();
1052 	if (!IS_ERR_OR_NULL(after_dashes))
1053 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
1054 			   NULL, set_init_arg);
1055 	if (extra_init_args)
1056 		parse_args("Setting extra init args", extra_init_args,
1057 			   NULL, 0, -1, -1, NULL, set_init_arg);
1058 
1059 	/* Architectural and non-timekeeping rng init, before allocator init */
1060 	random_init_early(command_line);
1061 
1062 	/*
1063 	 * These use large bootmem allocations and must precede
1064 	 * initalization of page allocator
1065 	 */
1066 	setup_log_buf(0);
1067 	vfs_caches_init_early();
1068 	sort_main_extable();
1069 	trap_init();
1070 	mm_core_init();
1071 	maple_tree_init();
1072 	poking_init();
1073 	ftrace_init();
1074 
1075 	/* trace_printk can be enabled here */
1076 	early_trace_init();
1077 
1078 	/*
1079 	 * Set up the scheduler prior starting any interrupts (such as the
1080 	 * timer interrupt). Full topology setup happens at smp_init()
1081 	 * time - but meanwhile we still have a functioning scheduler.
1082 	 */
1083 	sched_init();
1084 
1085 	if (WARN(!irqs_disabled(),
1086 		 "Interrupts were enabled *very* early, fixing it\n"))
1087 		local_irq_disable();
1088 	radix_tree_init();
1089 
1090 	/*
1091 	 * Set up housekeeping before setting up workqueues to allow the unbound
1092 	 * workqueue to take non-housekeeping into account.
1093 	 */
1094 	housekeeping_init();
1095 
1096 	/*
1097 	 * Allow workqueue creation and work item queueing/cancelling
1098 	 * early.  Work item execution depends on kthreads and starts after
1099 	 * workqueue_init().
1100 	 */
1101 	workqueue_init_early();
1102 
1103 	rcu_init();
1104 	kvfree_rcu_init();
1105 
1106 	/* Trace events are available after this */
1107 	trace_init();
1108 
1109 	if (initcall_debug)
1110 		initcall_debug_enable();
1111 
1112 	context_tracking_init();
1113 	/* init some links before init_ISA_irqs() */
1114 	early_irq_init();
1115 	init_IRQ();
1116 	tick_init();
1117 	rcu_init_nohz();
1118 	timers_init();
1119 	srcu_init();
1120 	hrtimers_init();
1121 	softirq_init();
1122 	timekeeping_init();
1123 	time_init();
1124 
1125 	/* This must be after timekeeping is initialized */
1126 	random_init();
1127 
1128 	/* These make use of the fully initialized rng */
1129 	kfence_init();
1130 	boot_init_stack_canary();
1131 
1132 	perf_event_init();
1133 	profile_init();
1134 	call_function_init();
1135 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
1136 
1137 	early_boot_irqs_disabled = false;
1138 	local_irq_enable();
1139 
1140 	kmem_cache_init_late();
1141 
1142 	/*
1143 	 * HACK ALERT! This is early. We're enabling the console before
1144 	 * we've done PCI setups etc, and console_init() must be aware of
1145 	 * this. But we do want output early, in case something goes wrong.
1146 	 */
1147 	console_init();
1148 	if (panic_later)
1149 		panic("Too many boot %s vars at `%s'", panic_later,
1150 		      panic_param);
1151 
1152 	lockdep_init();
1153 
1154 	/*
1155 	 * Need to run this when irqs are enabled, because it wants
1156 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
1157 	 * too:
1158 	 */
1159 	locking_selftest();
1160 
1161 #ifdef CONFIG_BLK_DEV_INITRD
1162 	if (initrd_start && !initrd_below_start_ok &&
1163 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
1164 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
1165 		    page_to_pfn(virt_to_page((void *)initrd_start)),
1166 		    min_low_pfn);
1167 		initrd_start = 0;
1168 	}
1169 #endif
1170 	setup_per_cpu_pageset();
1171 	numa_policy_init();
1172 	acpi_early_init();
1173 	if (late_time_init)
1174 		late_time_init();
1175 	sched_clock_init();
1176 	calibrate_delay();
1177 
1178 	arch_cpu_finalize_init();
1179 
1180 	pid_idr_init();
1181 	anon_vma_init();
1182 	thread_stack_cache_init();
1183 	cred_init();
1184 	fork_init();
1185 	proc_caches_init();
1186 	uts_ns_init();
1187 	time_ns_init();
1188 	key_init();
1189 	security_init();
1190 	dbg_late_init();
1191 	net_ns_init();
1192 	vfs_caches_init();
1193 	pagecache_init();
1194 	signals_init();
1195 	seq_file_init();
1196 	proc_root_init();
1197 	nsfs_init();
1198 	pidfs_init();
1199 	cpuset_init();
1200 	mem_cgroup_init();
1201 	cgroup_init();
1202 	taskstats_init_early();
1203 	delayacct_init();
1204 
1205 	acpi_subsystem_init();
1206 	arch_post_acpi_subsys_init();
1207 	kcsan_init();
1208 
1209 	/* Do the rest non-__init'ed, we're now alive */
1210 	rest_init();
1211 
1212 	/*
1213 	 * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10
1214 	 * and older.
1215 	 */
1216 #if !__has_attribute(__no_stack_protector__)
1217 	prevent_tail_call_optimization();
1218 #endif
1219 }
1220 
1221 /* Call all constructor functions linked into the kernel. */
1222 static void __init do_ctors(void)
1223 {
1224 /*
1225  * For UML, the constructors have already been called by the
1226  * normal setup code as it's just a normal ELF binary, so we
1227  * cannot do it again - but we do need CONFIG_CONSTRUCTORS
1228  * even on UML for modules.
1229  */
1230 #if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML)
1231 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
1232 
1233 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
1234 		(*fn)();
1235 #endif
1236 }
1237 
1238 #ifdef CONFIG_KALLSYMS
1239 struct blacklist_entry {
1240 	struct list_head next;
1241 	char *buf;
1242 };
1243 
1244 static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
1245 
1246 static int __init initcall_blacklist(char *str)
1247 {
1248 	char *str_entry;
1249 	struct blacklist_entry *entry;
1250 
1251 	/* str argument is a comma-separated list of functions */
1252 	do {
1253 		str_entry = strsep(&str, ",");
1254 		if (str_entry) {
1255 			pr_debug("blacklisting initcall %s\n", str_entry);
1256 			entry = memblock_alloc_or_panic(sizeof(*entry),
1257 					       SMP_CACHE_BYTES);
1258 			entry->buf = memblock_alloc_or_panic(strlen(str_entry) + 1,
1259 						    SMP_CACHE_BYTES);
1260 			strcpy(entry->buf, str_entry);
1261 			list_add(&entry->next, &blacklisted_initcalls);
1262 		}
1263 	} while (str_entry);
1264 
1265 	return 1;
1266 }
1267 
1268 static bool __init_or_module initcall_blacklisted(initcall_t fn)
1269 {
1270 	struct blacklist_entry *entry;
1271 	char fn_name[KSYM_SYMBOL_LEN];
1272 	unsigned long addr;
1273 
1274 	if (list_empty(&blacklisted_initcalls))
1275 		return false;
1276 
1277 	addr = (unsigned long) dereference_function_descriptor(fn);
1278 	sprint_symbol_no_offset(fn_name, addr);
1279 
1280 	/*
1281 	 * fn will be "function_name [module_name]" where [module_name] is not
1282 	 * displayed for built-in init functions.  Strip off the [module_name].
1283 	 */
1284 	strreplace(fn_name, ' ', '\0');
1285 
1286 	list_for_each_entry(entry, &blacklisted_initcalls, next) {
1287 		if (!strcmp(fn_name, entry->buf)) {
1288 			pr_debug("initcall %s blacklisted\n", fn_name);
1289 			return true;
1290 		}
1291 	}
1292 
1293 	return false;
1294 }
1295 #else
1296 static int __init initcall_blacklist(char *str)
1297 {
1298 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
1299 	return 0;
1300 }
1301 
1302 static bool __init_or_module initcall_blacklisted(initcall_t fn)
1303 {
1304 	return false;
1305 }
1306 #endif
1307 __setup("initcall_blacklist=", initcall_blacklist);
1308 
1309 static __init_or_module void
1310 trace_initcall_start_cb(void *data, initcall_t fn)
1311 {
1312 	ktime_t *calltime = data;
1313 
1314 	printk(KERN_DEBUG "calling  %pS @ %i\n", fn, task_pid_nr(current));
1315 	*calltime = ktime_get();
1316 }
1317 
1318 static __init_or_module void
1319 trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
1320 {
1321 	ktime_t rettime, *calltime = data;
1322 
1323 	rettime = ktime_get();
1324 	printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
1325 		 fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime));
1326 }
1327 
1328 static __init_or_module void
1329 trace_initcall_level_cb(void *data, const char *level)
1330 {
1331 	printk(KERN_DEBUG "entering initcall level: %s\n", level);
1332 }
1333 
1334 static ktime_t initcall_calltime;
1335 
1336 #ifdef TRACEPOINTS_ENABLED
1337 static void __init initcall_debug_enable(void)
1338 {
1339 	int ret;
1340 
1341 	ret = register_trace_initcall_start(trace_initcall_start_cb,
1342 					    &initcall_calltime);
1343 	ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
1344 					      &initcall_calltime);
1345 	ret |= register_trace_initcall_level(trace_initcall_level_cb, NULL);
1346 	WARN(ret, "Failed to register initcall tracepoints\n");
1347 }
1348 # define do_trace_initcall_start	trace_initcall_start
1349 # define do_trace_initcall_finish	trace_initcall_finish
1350 # define do_trace_initcall_level	trace_initcall_level
1351 #else
1352 static inline void do_trace_initcall_start(initcall_t fn)
1353 {
1354 	if (!initcall_debug)
1355 		return;
1356 	trace_initcall_start_cb(&initcall_calltime, fn);
1357 }
1358 static inline void do_trace_initcall_finish(initcall_t fn, int ret)
1359 {
1360 	if (!initcall_debug)
1361 		return;
1362 	trace_initcall_finish_cb(&initcall_calltime, fn, ret);
1363 }
1364 static inline void do_trace_initcall_level(const char *level)
1365 {
1366 	if (!initcall_debug)
1367 		return;
1368 	trace_initcall_level_cb(NULL, level);
1369 }
1370 #endif /* !TRACEPOINTS_ENABLED */
1371 
1372 int __init_or_module do_one_initcall(initcall_t fn)
1373 {
1374 	int count = preempt_count();
1375 	char msgbuf[64];
1376 	int ret;
1377 
1378 	if (initcall_blacklisted(fn))
1379 		return -EPERM;
1380 
1381 	do_trace_initcall_start(fn);
1382 	ret = fn();
1383 	do_trace_initcall_finish(fn, ret);
1384 
1385 	msgbuf[0] = 0;
1386 
1387 	if (preempt_count() != count) {
1388 		sprintf(msgbuf, "preemption imbalance ");
1389 		preempt_count_set(count);
1390 	}
1391 	if (irqs_disabled()) {
1392 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
1393 		local_irq_enable();
1394 	}
1395 	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
1396 
1397 	add_latent_entropy();
1398 	return ret;
1399 }
1400 
1401 
1402 static initcall_entry_t *initcall_levels[] __initdata = {
1403 	__initcall0_start,
1404 	__initcall1_start,
1405 	__initcall2_start,
1406 	__initcall3_start,
1407 	__initcall4_start,
1408 	__initcall5_start,
1409 	__initcall6_start,
1410 	__initcall7_start,
1411 	__initcall_end,
1412 };
1413 
1414 /* Keep these in sync with initcalls in include/linux/init.h */
1415 static const char *initcall_level_names[] __initdata = {
1416 	"pure",
1417 	"core",
1418 	"postcore",
1419 	"arch",
1420 	"subsys",
1421 	"fs",
1422 	"device",
1423 	"late",
1424 };
1425 
1426 static int __init ignore_unknown_bootoption(char *param, char *val,
1427 			       const char *unused, void *arg)
1428 {
1429 	return 0;
1430 }
1431 
1432 static void __init do_initcall_level(int level, char *command_line)
1433 {
1434 	initcall_entry_t *fn;
1435 
1436 	parse_args(initcall_level_names[level],
1437 		   command_line, __start___param,
1438 		   __stop___param - __start___param,
1439 		   level, level,
1440 		   NULL, ignore_unknown_bootoption);
1441 
1442 	do_trace_initcall_level(initcall_level_names[level]);
1443 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
1444 		do_one_initcall(initcall_from_entry(fn));
1445 }
1446 
1447 static void __init do_initcalls(void)
1448 {
1449 	int level;
1450 	size_t len = saved_command_line_len + 1;
1451 	char *command_line;
1452 
1453 	command_line = kzalloc(len, GFP_KERNEL);
1454 	if (!command_line)
1455 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
1456 
1457 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
1458 		/* Parser modifies command_line, restore it each time */
1459 		strcpy(command_line, saved_command_line);
1460 		do_initcall_level(level, command_line);
1461 	}
1462 
1463 	kfree(command_line);
1464 }
1465 
1466 /*
1467  * Ok, the machine is now initialized. None of the devices
1468  * have been touched yet, but the CPU subsystem is up and
1469  * running, and memory and process management works.
1470  *
1471  * Now we can finally start doing some real work..
1472  */
1473 static void __init do_basic_setup(void)
1474 {
1475 	cpuset_init_smp();
1476 	driver_init();
1477 	init_irq_proc();
1478 	do_ctors();
1479 	do_initcalls();
1480 }
1481 
1482 static void __init do_pre_smp_initcalls(void)
1483 {
1484 	initcall_entry_t *fn;
1485 
1486 	do_trace_initcall_level("early");
1487 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
1488 		do_one_initcall(initcall_from_entry(fn));
1489 }
1490 
1491 static int run_init_process(const char *init_filename)
1492 {
1493 	const char *const *p;
1494 
1495 	argv_init[0] = init_filename;
1496 	pr_info("Run %s as init process\n", init_filename);
1497 	pr_debug("  with arguments:\n");
1498 	for (p = argv_init; *p; p++)
1499 		pr_debug("    %s\n", *p);
1500 	pr_debug("  with environment:\n");
1501 	for (p = envp_init; *p; p++)
1502 		pr_debug("    %s\n", *p);
1503 	return kernel_execve(init_filename, argv_init, envp_init);
1504 }
1505 
1506 static int try_to_run_init_process(const char *init_filename)
1507 {
1508 	int ret;
1509 
1510 	ret = run_init_process(init_filename);
1511 
1512 	if (ret && ret != -ENOENT) {
1513 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1514 		       init_filename, ret);
1515 	}
1516 
1517 	return ret;
1518 }
1519 
1520 static noinline void __init kernel_init_freeable(void);
1521 
1522 #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
1523 bool rodata_enabled __ro_after_init = true;
1524 
1525 #ifndef arch_parse_debug_rodata
1526 static inline bool arch_parse_debug_rodata(char *str) { return false; }
1527 #endif
1528 
1529 static int __init set_debug_rodata(char *str)
1530 {
1531 	if (arch_parse_debug_rodata(str))
1532 		return 0;
1533 
1534 	if (str && !strcmp(str, "on"))
1535 		rodata_enabled = true;
1536 	else if (str && !strcmp(str, "off"))
1537 		rodata_enabled = false;
1538 	else
1539 		pr_warn("Invalid option string for rodata: '%s'\n", str);
1540 	return 0;
1541 }
1542 early_param("rodata", set_debug_rodata);
1543 #endif
1544 
1545 static void mark_readonly(void)
1546 {
1547 	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
1548 		/*
1549 		 * load_module() results in W+X mappings, which are cleaned
1550 		 * up with init_free_wq. Let's make sure that queued work is
1551 		 * flushed so that we don't hit false positives looking for
1552 		 * insecure pages which are W+X.
1553 		 */
1554 		flush_module_init_free_work();
1555 		jump_label_init_ro();
1556 		mark_rodata_ro();
1557 		debug_checkwx();
1558 		rodata_test();
1559 	} else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
1560 		pr_info("Kernel memory protection disabled.\n");
1561 	} else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
1562 		pr_warn("Kernel memory protection not selected by kernel config.\n");
1563 	} else {
1564 		pr_warn("This architecture does not have kernel memory protection.\n");
1565 	}
1566 }
1567 
1568 void __weak free_initmem(void)
1569 {
1570 	free_initmem_default(POISON_FREE_INITMEM);
1571 }
1572 
1573 static int __ref kernel_init(void *unused)
1574 {
1575 	int ret;
1576 
1577 	/*
1578 	 * Wait until kthreadd is all set-up.
1579 	 */
1580 	wait_for_completion(&kthreadd_done);
1581 
1582 	kernel_init_freeable();
1583 	/* need to finish all async __init code before freeing the memory */
1584 	async_synchronize_full();
1585 
1586 	system_state = SYSTEM_FREEING_INITMEM;
1587 	kprobe_free_init_mem();
1588 	ftrace_free_init_mem();
1589 	kgdb_free_init_mem();
1590 	exit_boot_config();
1591 	free_initmem();
1592 	mark_readonly();
1593 
1594 	/*
1595 	 * Kernel mappings are now finalized - update the userspace page-table
1596 	 * to finalize PTI.
1597 	 */
1598 	pti_finalize();
1599 
1600 	system_state = SYSTEM_RUNNING;
1601 	numa_default_policy();
1602 
1603 	rcu_end_inkernel_boot();
1604 
1605 	do_sysctl_args();
1606 
1607 	if (ramdisk_execute_command) {
1608 		ret = run_init_process(ramdisk_execute_command);
1609 		if (!ret)
1610 			return 0;
1611 		pr_err("Failed to execute %s (error %d)\n",
1612 		       ramdisk_execute_command, ret);
1613 	}
1614 
1615 	/*
1616 	 * We try each of these until one succeeds.
1617 	 *
1618 	 * The Bourne shell can be used instead of init if we are
1619 	 * trying to recover a really broken machine.
1620 	 */
1621 	if (execute_command) {
1622 		ret = run_init_process(execute_command);
1623 		if (!ret)
1624 			return 0;
1625 		panic("Requested init %s failed (error %d).",
1626 		      execute_command, ret);
1627 	}
1628 
1629 	if (CONFIG_DEFAULT_INIT[0] != '\0') {
1630 		ret = run_init_process(CONFIG_DEFAULT_INIT);
1631 		if (ret)
1632 			pr_err("Default init %s failed (error %d)\n",
1633 			       CONFIG_DEFAULT_INIT, ret);
1634 		else
1635 			return 0;
1636 	}
1637 
1638 	if (!try_to_run_init_process("/sbin/init") ||
1639 	    !try_to_run_init_process("/etc/init") ||
1640 	    !try_to_run_init_process("/bin/init") ||
1641 	    !try_to_run_init_process("/bin/sh"))
1642 		return 0;
1643 
1644 	panic("No working init found.  Try passing init= option to kernel. "
1645 	      "See Linux Documentation/admin-guide/init.rst for guidance.");
1646 }
1647 
1648 /* Open /dev/console, for stdin/stdout/stderr, this should never fail */
1649 void __init console_on_rootfs(void)
1650 {
1651 	struct file *file = filp_open("/dev/console", O_RDWR, 0);
1652 
1653 	if (IS_ERR(file)) {
1654 		pr_err("Warning: unable to open an initial console.\n");
1655 		return;
1656 	}
1657 	init_dup(file);
1658 	init_dup(file);
1659 	init_dup(file);
1660 	fput(file);
1661 }
1662 
1663 static noinline void __init kernel_init_freeable(void)
1664 {
1665 	/* Now the scheduler is fully set up and can do blocking allocations */
1666 	gfp_allowed_mask = __GFP_BITS_MASK;
1667 
1668 	/*
1669 	 * init can allocate pages on any node
1670 	 */
1671 	set_mems_allowed(node_states[N_MEMORY]);
1672 
1673 	cad_pid = get_pid(task_pid(current));
1674 
1675 	smp_prepare_cpus(setup_max_cpus);
1676 
1677 	workqueue_init();
1678 
1679 	init_mm_internals();
1680 
1681 	do_pre_smp_initcalls();
1682 	lockup_detector_init();
1683 
1684 	smp_init();
1685 	sched_init_smp();
1686 
1687 	workqueue_init_topology();
1688 	async_init();
1689 	padata_init();
1690 	page_alloc_init_late();
1691 
1692 	do_basic_setup();
1693 
1694 	kunit_run_all_tests();
1695 
1696 	wait_for_initramfs();
1697 	console_on_rootfs();
1698 
1699 	/*
1700 	 * check if there is an early userspace init.  If yes, let it do all
1701 	 * the work
1702 	 */
1703 	int ramdisk_command_access;
1704 	ramdisk_command_access = init_eaccess(ramdisk_execute_command);
1705 	if (ramdisk_command_access != 0) {
1706 		if (ramdisk_execute_command_set)
1707 			pr_warn("check access for rdinit=%s failed: %i, ignoring\n",
1708 				ramdisk_execute_command, ramdisk_command_access);
1709 		ramdisk_execute_command = NULL;
1710 		prepare_namespace();
1711 	}
1712 
1713 	/*
1714 	 * Ok, we have completed the initial bootup, and
1715 	 * we're essentially up and running. Get rid of the
1716 	 * initmem segments and start the user-mode stuff..
1717 	 *
1718 	 * rootfs is available now, try loading the public keys
1719 	 * and default modules
1720 	 */
1721 
1722 	integrity_load_keys();
1723 }
1724