hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/init/main.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/init/main.c
34 *
....@@ -25,18 +26,21 @@
2526 #include <linux/ioport.h>
2627 #include <linux/init.h>
2728 #include <linux/initrd.h>
28
-#include <linux/bootmem.h>
29
+#include <linux/memblock.h>
2930 #include <linux/acpi.h>
31
+#include <linux/bootconfig.h>
3032 #include <linux/console.h>
3133 #include <linux/nmi.h>
3234 #include <linux/percpu.h>
3335 #include <linux/kmod.h>
36
+#include <linux/kprobes.h>
3437 #include <linux/vmalloc.h>
3538 #include <linux/kernel_stat.h>
3639 #include <linux/start_kernel.h>
3740 #include <linux/security.h>
3841 #include <linux/smp.h>
3942 #include <linux/profile.h>
43
+#include <linux/kfence.h>
4044 #include <linux/rcupdate.h>
4145 #include <linux/moduleparam.h>
4246 #include <linux/kallsyms.h>
....@@ -61,8 +65,9 @@
6165 #include <linux/debugobjects.h>
6266 #include <linux/lockdep.h>
6367 #include <linux/kmemleak.h>
68
+#include <linux/padata.h>
6469 #include <linux/pid_namespace.h>
65
-#include <linux/device.h>
70
+#include <linux/device/driver.h>
6671 #include <linux/kthread.h>
6772 #include <linux/sched.h>
6873 #include <linux/sched/init.h>
....@@ -92,6 +97,9 @@
9297 #include <linux/rodata_test.h>
9398 #include <linux/jump_label.h>
9499 #include <linux/mem_encrypt.h>
100
+#include <linux/kcsan.h>
101
+#include <linux/init_syscalls.h>
102
+#include <linux/stackdepot.h>
95103
96104 #include <asm/io.h>
97105 #include <asm/bugs.h>
....@@ -101,6 +109,8 @@
101109
102110 #define CREATE_TRACE_POINTS
103111 #include <trace/events/initcall.h>
112
+
113
+#include <kunit/test.h>
104114
105115 static int kernel_init(void *);
106116
....@@ -133,14 +143,24 @@
133143 char __initdata boot_command_line[COMMAND_LINE_SIZE];
134144 /* Untouched saved command line (eg. for /proc) */
135145 char *saved_command_line;
136
-EXPORT_SYMBOL_GPL(saved_command_line);
137146 /* Command line for parameter parsing */
138147 static char *static_command_line;
139
-/* Command line for per-initcall parameter parsing */
140
-static char *initcall_command_line;
148
+/* Untouched extra command line */
149
+static char *extra_command_line;
150
+/* Extra init arguments */
151
+static char *extra_init_args;
152
+
153
+#ifdef CONFIG_BOOT_CONFIG
154
+/* Is bootconfig on command line? */
155
+static bool bootconfig_found;
156
+static bool initargs_found;
157
+#else
158
+# define bootconfig_found false
159
+# define initargs_found false
160
+#endif
141161
142162 static char *execute_command;
143
-static char *ramdisk_execute_command;
163
+static char *ramdisk_execute_command = "/init";
144164
145165 /*
146166 * Used to generate warnings if static_key manipulation functions are used
....@@ -245,9 +265,223 @@
245265
246266 early_param("loglevel", loglevel);
247267
248
-/* Change NUL term back to "=", to make "param" the whole string. */
249
-static int __init repair_env_string(char *param, char *val,
268
+#ifdef CONFIG_BLK_DEV_INITRD
269
+static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum)
270
+{
271
+ u32 size, csum;
272
+ char *data;
273
+ u32 *hdr;
274
+ int i;
275
+
276
+ if (!initrd_end)
277
+ return NULL;
278
+
279
+ data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
280
+ /*
281
+ * Since Grub may align the size of initrd to 4, we must
282
+ * check the preceding 3 bytes as well.
283
+ */
284
+ for (i = 0; i < 4; i++) {
285
+ if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
286
+ goto found;
287
+ data--;
288
+ }
289
+ return NULL;
290
+
291
+found:
292
+ hdr = (u32 *)(data - 8);
293
+ size = le32_to_cpu(hdr[0]);
294
+ csum = le32_to_cpu(hdr[1]);
295
+
296
+ data = ((void *)hdr) - size;
297
+ if ((unsigned long)data < initrd_start) {
298
+ pr_err("bootconfig size %d is greater than initrd size %ld\n",
299
+ size, initrd_end - initrd_start);
300
+ return NULL;
301
+ }
302
+
303
+ /* Remove bootconfig from initramfs/initrd */
304
+ initrd_end = (unsigned long)data;
305
+ if (_size)
306
+ *_size = size;
307
+ if (_csum)
308
+ *_csum = csum;
309
+
310
+ return data;
311
+}
312
+#else
313
+static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum)
314
+{
315
+ return NULL;
316
+}
317
+#endif
318
+
319
+#ifdef CONFIG_BOOT_CONFIG
320
+
321
+static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
322
+
323
+#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
324
+
325
+static int __init xbc_snprint_cmdline(char *buf, size_t size,
326
+ struct xbc_node *root)
327
+{
328
+ struct xbc_node *knode, *vnode;
329
+ char *end = buf + size;
330
+ const char *val;
331
+ int ret;
332
+
333
+ xbc_node_for_each_key_value(root, knode, val) {
334
+ ret = xbc_node_compose_key_after(root, knode,
335
+ xbc_namebuf, XBC_KEYLEN_MAX);
336
+ if (ret < 0)
337
+ return ret;
338
+
339
+ vnode = xbc_node_get_child(knode);
340
+ if (!vnode) {
341
+ ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
342
+ if (ret < 0)
343
+ return ret;
344
+ buf += ret;
345
+ continue;
346
+ }
347
+ xbc_array_for_each_value(vnode, val) {
348
+ ret = snprintf(buf, rest(buf, end), "%s=\"%s\" ",
349
+ xbc_namebuf, val);
350
+ if (ret < 0)
351
+ return ret;
352
+ buf += ret;
353
+ }
354
+ }
355
+
356
+ return buf - (end - size);
357
+}
358
+#undef rest
359
+
360
+/* Make an extra command line under given key word */
361
+static char * __init xbc_make_cmdline(const char *key)
362
+{
363
+ struct xbc_node *root;
364
+ char *new_cmdline;
365
+ int ret, len = 0;
366
+
367
+ root = xbc_find_node(key);
368
+ if (!root)
369
+ return NULL;
370
+
371
+ /* Count required buffer size */
372
+ len = xbc_snprint_cmdline(NULL, 0, root);
373
+ if (len <= 0)
374
+ return NULL;
375
+
376
+ new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
377
+ if (!new_cmdline) {
378
+ pr_err("Failed to allocate memory for extra kernel cmdline.\n");
379
+ return NULL;
380
+ }
381
+
382
+ ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
383
+ if (ret < 0 || ret > len) {
384
+ pr_err("Failed to print extra kernel cmdline.\n");
385
+ memblock_free(__pa(new_cmdline), len + 1);
386
+ return NULL;
387
+ }
388
+
389
+ return new_cmdline;
390
+}
391
+
392
+static int __init bootconfig_params(char *param, char *val,
250393 const char *unused, void *arg)
394
+{
395
+ if (strcmp(param, "bootconfig") == 0) {
396
+ bootconfig_found = true;
397
+ }
398
+ return 0;
399
+}
400
+
401
+static void __init setup_boot_config(const char *cmdline)
402
+{
403
+ static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
404
+ const char *msg;
405
+ int pos;
406
+ u32 size, csum;
407
+ char *data, *copy, *err;
408
+ int ret;
409
+
410
+ /* Cut out the bootconfig data even if we have no bootconfig option */
411
+ data = get_boot_config_from_initrd(&size, &csum);
412
+
413
+ strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
414
+ err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL,
415
+ bootconfig_params);
416
+
417
+ if (IS_ERR(err) || !bootconfig_found)
418
+ return;
419
+
420
+ /* parse_args() stops at '--' and returns an address */
421
+ if (err)
422
+ initargs_found = true;
423
+
424
+ if (!data) {
425
+ pr_err("'bootconfig' found on command line, but no bootconfig found\n");
426
+ return;
427
+ }
428
+
429
+ if (size >= XBC_DATA_MAX) {
430
+ pr_err("bootconfig size %d greater than max size %d\n",
431
+ size, XBC_DATA_MAX);
432
+ return;
433
+ }
434
+
435
+ if (xbc_calc_checksum(data, size) != csum) {
436
+ pr_err("bootconfig checksum failed\n");
437
+ return;
438
+ }
439
+
440
+ copy = memblock_alloc(size + 1, SMP_CACHE_BYTES);
441
+ if (!copy) {
442
+ pr_err("Failed to allocate memory for bootconfig\n");
443
+ return;
444
+ }
445
+
446
+ memcpy(copy, data, size);
447
+ copy[size] = '\0';
448
+
449
+ ret = xbc_init(copy, &msg, &pos);
450
+ if (ret < 0) {
451
+ if (pos < 0)
452
+ pr_err("Failed to init bootconfig: %s.\n", msg);
453
+ else
454
+ pr_err("Failed to parse bootconfig: %s at %d.\n",
455
+ msg, pos);
456
+ } else {
457
+ pr_info("Load bootconfig: %d bytes %d nodes\n", size, ret);
458
+ /* keys starting with "kernel." are passed via cmdline */
459
+ extra_command_line = xbc_make_cmdline("kernel");
460
+ /* Also, "init." keys are init arguments */
461
+ extra_init_args = xbc_make_cmdline("init");
462
+ }
463
+ return;
464
+}
465
+
466
+#else
467
+
468
+static void __init setup_boot_config(const char *cmdline)
469
+{
470
+ /* Remove bootconfig data from initrd */
471
+ get_boot_config_from_initrd(NULL, NULL);
472
+}
473
+
474
+static int __init warn_bootconfig(char *str)
475
+{
476
+ pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n");
477
+ return 0;
478
+}
479
+early_param("bootconfig", warn_bootconfig);
480
+
481
+#endif
482
+
483
+/* Change NUL term back to "=", to make "param" the whole string. */
484
+static void __init repair_env_string(char *param, char *val)
251485 {
252486 if (val) {
253487 /* param=val or param="val"? */
....@@ -256,11 +490,9 @@
256490 else if (val == param+strlen(param)+2) {
257491 val[-2] = '=';
258492 memmove(val-1, val, strlen(val)+1);
259
- val--;
260493 } else
261494 BUG();
262495 }
263
- return 0;
264496 }
265497
266498 /* Anything after -- gets handed straight to init. */
....@@ -272,7 +504,7 @@
272504 if (panic_later)
273505 return 0;
274506
275
- repair_env_string(param, val, unused, NULL);
507
+ repair_env_string(param, val);
276508
277509 for (i = 0; argv_init[i]; i++) {
278510 if (i == MAX_INIT_ARGS) {
....@@ -292,14 +524,16 @@
292524 static int __init unknown_bootoption(char *param, char *val,
293525 const char *unused, void *arg)
294526 {
295
- repair_env_string(param, val, unused, NULL);
527
+ size_t len = strlen(param);
528
+
529
+ repair_env_string(param, val);
296530
297531 /* Handle obsolete-style parameters */
298532 if (obsolete_checksetup(param))
299533 return 0;
300534
301535 /* Unused module parameter. */
302
- if (strchr(param, '.') && (!val || strchr(param, '.') < val))
536
+ if (strnchr(param, len, '.'))
303537 return 0;
304538
305539 if (panic_later)
....@@ -313,7 +547,7 @@
313547 panic_later = "env";
314548 panic_param = param;
315549 }
316
- if (!strncmp(param, envp_init[i], val - param))
550
+ if (!strncmp(param, envp_init[i], len+1))
317551 break;
318552 }
319553 envp_init[i] = param;
....@@ -374,13 +608,51 @@
374608 */
375609 static void __init setup_command_line(char *command_line)
376610 {
377
- saved_command_line =
378
- memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
379
- initcall_command_line =
380
- memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
381
- static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
382
- strcpy(saved_command_line, boot_command_line);
383
- strcpy(static_command_line, command_line);
611
+ size_t len, xlen = 0, ilen = 0;
612
+
613
+ if (extra_command_line)
614
+ xlen = strlen(extra_command_line);
615
+ if (extra_init_args)
616
+ ilen = strlen(extra_init_args) + 4; /* for " -- " */
617
+
618
+ len = xlen + strlen(boot_command_line) + 1;
619
+
620
+ saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
621
+ if (!saved_command_line)
622
+ panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
623
+
624
+ static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
625
+ if (!static_command_line)
626
+ panic("%s: Failed to allocate %zu bytes\n", __func__, len);
627
+
628
+ if (xlen) {
629
+ /*
630
+ * We have to put extra_command_line before boot command
631
+ * lines because there could be dashes (separator of init
632
+ * command line) in the command lines.
633
+ */
634
+ strcpy(saved_command_line, extra_command_line);
635
+ strcpy(static_command_line, extra_command_line);
636
+ }
637
+ strcpy(saved_command_line + xlen, boot_command_line);
638
+ strcpy(static_command_line + xlen, command_line);
639
+
640
+ if (ilen) {
641
+ /*
642
+ * Append supplemental init boot args to saved_command_line
643
+ * so that user can check what command line options passed
644
+ * to init.
645
+ */
646
+ len = strlen(saved_command_line);
647
+ if (initargs_found) {
648
+ saved_command_line[len++] = ' ';
649
+ } else {
650
+ strcpy(saved_command_line + len, " -- ");
651
+ len += 4;
652
+ }
653
+
654
+ strcpy(saved_command_line + len, extra_init_args);
655
+ }
384656 }
385657
386658 /*
....@@ -394,7 +666,7 @@
394666
395667 static __initdata DECLARE_COMPLETION(kthreadd_done);
396668
397
-static noinline void __ref rest_init(void)
669
+noinline void __ref rest_init(void)
398670 {
399671 struct task_struct *tsk;
400672 int pid;
....@@ -424,7 +696,7 @@
424696
425697 /*
426698 * Enable might_sleep() and smp_processor_id() checks.
427
- * They cannot be enabled earlier because with CONFIG_PREEMPT=y
699
+ * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
428700 * kernel_thread() would trigger might_sleep() splats. With
429701 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
430702 * already, but it's stuck on the kthreadd_done completion.
....@@ -496,6 +768,10 @@
496768
497769 void __init __weak mem_encrypt_init(void) { }
498770
771
+void __init __weak poking_init(void) { }
772
+
773
+void __init __weak pgtable_cache_init(void) { }
774
+
499775 bool initcall_debug;
500776 core_param(initcall_debug, initcall_debug, bool, 0644);
501777
....@@ -542,10 +818,17 @@
542818 * bigger than MAX_ORDER unless SPARSEMEM.
543819 */
544820 page_ext_init_flatmem();
821
+ init_mem_debugging_and_hardening();
822
+ kfence_alloc_pool();
545823 report_meminit();
824
+ stack_depot_init();
546825 mem_init();
826
+ /* page_owner must be initialized after buddy is ready */
827
+ page_ext_init_flatmem_late();
547828 kmem_cache_init();
829
+ kmemleak_init();
548830 pgtable_init();
831
+ debug_objects_mem_init();
549832 vmalloc_init();
550833 ioremap_huge_init();
551834 /* Should be run before the first non-init thread is created */
....@@ -554,7 +837,12 @@
554837 pti_init();
555838 }
556839
557
-asmlinkage __visible void __init start_kernel(void)
840
+void __init __weak arch_call_rest_init(void)
841
+{
842
+ rest_init();
843
+}
844
+
845
+asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
558846 {
559847 char *command_line;
560848 char *after_dashes;
....@@ -575,8 +863,9 @@
575863 boot_cpu_init();
576864 page_address_init();
577865 pr_notice("%s", linux_banner);
866
+ early_security_init();
578867 setup_arch(&command_line);
579
- mm_init_cpumask(&init_mm);
868
+ setup_boot_config(command_line);
580869 setup_command_line(command_line);
581870 setup_nr_cpu_ids();
582871 setup_per_cpu_areas();
....@@ -588,10 +877,10 @@
588877
589878 #ifdef CONFIG_ARCH_ROCKCHIP
590879 {
591
- const char *s = boot_command_line;
592
- const char *e = &boot_command_line[strlen(boot_command_line)];
880
+ const char *s = saved_command_line;
881
+ const char *e = &saved_command_line[strlen(saved_command_line)];
593882 int n =
594
- pr_notice("Kernel command line: %s\n", boot_command_line);
883
+ pr_notice("Kernel command line: %s\n", saved_command_line);
595884 n -= strlen("Kernel command line: ");
596885 s += n;
597886 /* command line maybe too long to print one time */
....@@ -601,7 +890,7 @@
601890 }
602891 }
603892 #else
604
- pr_notice("Kernel command line: %s\n", boot_command_line);
893
+ pr_notice("Kernel command line: %s\n", saved_command_line);
605894 #endif
606895 /* parameters may set static keys */
607896 jump_label_init();
....@@ -613,6 +902,9 @@
613902 if (!IS_ERR_OR_NULL(after_dashes))
614903 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
615904 NULL, set_init_arg);
905
+ if (extra_init_args)
906
+ parse_args("Setting extra init args", extra_init_args,
907
+ NULL, 0, -1, -1, NULL, set_init_arg);
616908
617909 /*
618910 * These use large bootmem allocations and must precede
....@@ -635,11 +927,7 @@
635927 * time - but meanwhile we still have a functioning scheduler.
636928 */
637929 sched_init();
638
- /*
639
- * Disable preemption - early bootup scheduling is extremely
640
- * fragile until we cpu_idle() for the first time.
641
- */
642
- preempt_disable();
930
+
643931 if (WARN(!irqs_disabled(),
644932 "Interrupts were enabled *very* early, fixing it\n"))
645933 local_irq_disable();
....@@ -676,21 +964,19 @@
676964 hrtimers_init();
677965 softirq_init();
678966 timekeeping_init();
967
+ kfence_init();
968
+ time_init();
679969
680970 /*
681971 * For best initial stack canary entropy, prepare it after:
682972 * - setup_arch() for any UEFI RNG entropy and boot cmdline access
683
- * - timekeeping_init() for ktime entropy used in rand_initialize()
684
- * - rand_initialize() to get any arch-specific entropy like RDRAND
685
- * - add_latent_entropy() to get any latent entropy
686
- * - adding command line entropy
973
+ * - timekeeping_init() for ktime entropy used in random_init()
974
+ * - time_init() for making random_get_entropy() work on some platforms
975
+ * - random_init() to initialize the RNG from from early entropy sources
687976 */
688
- rand_initialize();
689
- add_latent_entropy();
690
- add_device_randomness(command_line, strlen(command_line));
977
+ random_init(command_line);
691978 boot_init_stack_canary();
692979
693
- time_init();
694980 perf_event_init();
695981 profile_init();
696982 call_function_init();
....@@ -737,8 +1023,6 @@
7371023 initrd_start = 0;
7381024 }
7391025 #endif
740
- kmemleak_init();
741
- debug_objects_mem_init();
7421026 setup_per_cpu_pageset();
7431027 numa_policy_init();
7441028 acpi_early_init();
....@@ -772,18 +1056,16 @@
7721056 taskstats_init_early();
7731057 delayacct_init();
7741058
1059
+ poking_init();
7751060 check_bugs();
7761061
7771062 acpi_subsystem_init();
7781063 arch_post_acpi_subsys_init();
7791064 sfi_init_late();
780
-
781
- if (efi_enabled(EFI_RUNTIME_SERVICES)) {
782
- efi_free_boot_services();
783
- }
1065
+ kcsan_init();
7841066
7851067 /* Do the rest non-__init'ed, we're now alive */
786
- rest_init();
1068
+ arch_call_rest_init();
7871069
7881070 prevent_tail_call_optimization();
7891071 }
....@@ -817,14 +1099,22 @@
8171099 str_entry = strsep(&str, ",");
8181100 if (str_entry) {
8191101 pr_debug("blacklisting initcall %s\n", str_entry);
820
- entry = alloc_bootmem(sizeof(*entry));
821
- entry->buf = alloc_bootmem(strlen(str_entry) + 1);
1102
+ entry = memblock_alloc(sizeof(*entry),
1103
+ SMP_CACHE_BYTES);
1104
+ if (!entry)
1105
+ panic("%s: Failed to allocate %zu bytes\n",
1106
+ __func__, sizeof(*entry));
1107
+ entry->buf = memblock_alloc(strlen(str_entry) + 1,
1108
+ SMP_CACHE_BYTES);
1109
+ if (!entry->buf)
1110
+ panic("%s: Failed to allocate %zu bytes\n",
1111
+ __func__, strlen(str_entry) + 1);
8221112 strcpy(entry->buf, str_entry);
8231113 list_add(&entry->next, &blacklisted_initcalls);
8241114 }
8251115 } while (str_entry);
8261116
827
- return 0;
1117
+ return 1;
8281118 }
8291119
8301120 static bool __init_or_module initcall_blacklisted(initcall_t fn)
....@@ -873,7 +1163,7 @@
8731163 {
8741164 ktime_t *calltime = (ktime_t *)data;
8751165
876
- printk(KERN_DEBUG "calling %pF @ %i\n", fn, task_pid_nr(current));
1166
+ printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current));
8771167 *calltime = ktime_get();
8781168 }
8791169
....@@ -887,7 +1177,7 @@
8871177 rettime = ktime_get();
8881178 delta = ktime_sub(rettime, *calltime);
8891179 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
890
- printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
1180
+ printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
8911181 fn, ret, duration);
8921182 }
8931183
....@@ -944,7 +1234,7 @@
9441234 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
9451235 local_irq_enable();
9461236 }
947
- WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
1237
+ WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
9481238
9491239 add_latent_entropy();
9501240 return ret;
....@@ -975,7 +1265,7 @@
9751265 };
9761266
9771267 /* Keep these in sync with initcalls in include/linux/init.h */
978
-static char *initcall_level_names[] __initdata = {
1268
+static const char *initcall_level_names[] __initdata = {
9791269 "pure",
9801270 "core",
9811271 "postcore",
....@@ -985,6 +1275,12 @@
9851275 "device",
9861276 "late",
9871277 };
1278
+
1279
+static int __init ignore_unknown_bootoption(char *param, char *val,
1280
+ const char *unused, void *arg)
1281
+{
1282
+ return 0;
1283
+}
9881284
9891285 #ifdef CONFIG_INITCALL_ASYNC
9901286 extern initcall_entry_t __initcall0s_start[];
....@@ -1163,16 +1459,15 @@
11631459 }
11641460 #endif /* CONFIG_INITCALL_ASYNC */
11651461
1166
-static void __init do_initcall_level(int level)
1462
+static void __init do_initcall_level(int level, char *command_line)
11671463 {
11681464 initcall_entry_t *fn;
11691465
1170
- strcpy(initcall_command_line, saved_command_line);
11711466 parse_args(initcall_level_names[level],
1172
- initcall_command_line, __start___param,
1467
+ command_line, __start___param,
11731468 __stop___param - __start___param,
11741469 level, level,
1175
- NULL, &repair_env_string);
1470
+ NULL, ignore_unknown_bootoption);
11761471
11771472 trace_initcall_level(initcall_level_names[level]);
11781473
....@@ -1189,13 +1484,24 @@
11891484 static void __init do_initcalls(void)
11901485 {
11911486 int level;
1487
+ size_t len = strlen(saved_command_line) + 1;
1488
+ char *command_line;
11921489
11931490 #ifdef CONFIG_INITCALL_ASYNC
11941491 initcall_init_workers();
11951492 #endif
11961493
1197
- for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
1198
- do_initcall_level(level);
1494
+ command_line = kzalloc(len, GFP_KERNEL);
1495
+ if (!command_line)
1496
+ panic("%s: Failed to allocate %zu bytes\n", __func__, len);
1497
+
1498
+ for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
1499
+ /* Parser modifies command_line, restore it each time */
1500
+ strcpy(command_line, saved_command_line);
1501
+ do_initcall_level(level, command_line);
1502
+ }
1503
+
1504
+ kfree(command_line);
11991505
12001506 #ifdef CONFIG_INITCALL_ASYNC
12011507 initcall_free_works();
....@@ -1212,7 +1518,6 @@
12121518 static void __init do_basic_setup(void)
12131519 {
12141520 cpuset_init_smp();
1215
- shmem_init();
12161521 driver_init();
12171522 init_irq_proc();
12181523 do_ctors();
....@@ -1229,24 +1534,19 @@
12291534 do_one_initcall(initcall_from_entry(fn));
12301535 }
12311536
1232
-/*
1233
- * This function requests modules which should be loaded by default and is
1234
- * called twice right after initrd is mounted and right before init is
1235
- * exec'd. If such modules are on either initrd or rootfs, they will be
1236
- * loaded before control is passed to userland.
1237
- */
1238
-void __init load_default_modules(void)
1239
-{
1240
- load_default_elevator_module();
1241
-}
1242
-
12431537 static int run_init_process(const char *init_filename)
12441538 {
1539
+ const char *const *p;
1540
+
12451541 argv_init[0] = init_filename;
12461542 pr_info("Run %s as init process\n", init_filename);
1247
- return do_execve(getname_kernel(init_filename),
1248
- (const char __user *const __user *)argv_init,
1249
- (const char __user *const __user *)envp_init);
1543
+ pr_debug(" with arguments:\n");
1544
+ for (p = argv_init; *p; p++)
1545
+ pr_debug(" %s\n", *p);
1546
+ pr_debug(" with environment:\n");
1547
+ for (p = envp_init; *p; p++)
1548
+ pr_debug(" %s\n", *p);
1549
+ return kernel_execve(init_filename, argv_init, envp_init);
12501550 }
12511551
12521552 static int try_to_run_init_process(const char *init_filename)
....@@ -1269,7 +1569,9 @@
12691569 bool rodata_enabled __ro_after_init = true;
12701570 static int __init set_debug_rodata(char *str)
12711571 {
1272
- return strtobool(str, &rodata_enabled);
1572
+ if (strtobool(str, &rodata_enabled))
1573
+ pr_warn("Invalid option string for rodata: '%s'\n", str);
1574
+ return 1;
12731575 }
12741576 __setup("rodata=", set_debug_rodata);
12751577 #endif
....@@ -1279,16 +1581,21 @@
12791581 {
12801582 if (rodata_enabled) {
12811583 /*
1282
- * load_module() results in W+X mappings, which are cleaned up
1283
- * with call_rcu_sched(). Let's make sure that queued work is
1584
+ * load_module() results in W+X mappings, which are cleaned
1585
+ * up with call_rcu(). Let's make sure that queued work is
12841586 * flushed so that we don't hit false positives looking for
12851587 * insecure pages which are W+X.
12861588 */
1287
- rcu_barrier_sched();
1589
+ rcu_barrier();
12881590 mark_rodata_ro();
12891591 rodata_test();
12901592 } else
12911593 pr_info("Kernel memory protection disabled.\n");
1594
+}
1595
+#elif defined(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)
1596
+static inline void mark_readonly(void)
1597
+{
1598
+ pr_warn("Kernel memory protection not selected by kernel config.\n");
12921599 }
12931600 #else
12941601 static inline void mark_readonly(void)
....@@ -1297,6 +1604,11 @@
12971604 }
12981605 #endif
12991606
1607
+void __weak free_initmem(void)
1608
+{
1609
+ free_initmem_default(POISON_FREE_INITMEM);
1610
+}
1611
+
13001612 static int __ref kernel_init(void *unused)
13011613 {
13021614 int ret;
....@@ -1304,8 +1616,9 @@
13041616 kernel_init_freeable();
13051617 /* need to finish all async __init code before freeing the memory */
13061618 async_synchronize_full();
1619
+ kprobe_free_init_mem();
13071620 ftrace_free_init_mem();
1308
- jump_label_invalidate_initmem();
1621
+ kgdb_free_init_mem();
13091622 free_initmem();
13101623 mark_readonly();
13111624
....@@ -1319,6 +1632,8 @@
13191632 numa_default_policy();
13201633
13211634 rcu_end_inkernel_boot();
1635
+
1636
+ do_sysctl_args();
13221637
13231638 if (ramdisk_execute_command) {
13241639 ret = run_init_process(ramdisk_execute_command);
....@@ -1341,6 +1656,16 @@
13411656 panic("Requested init %s failed (error %d).",
13421657 execute_command, ret);
13431658 }
1659
+
1660
+ if (CONFIG_DEFAULT_INIT[0] != '\0') {
1661
+ ret = run_init_process(CONFIG_DEFAULT_INIT);
1662
+ if (ret)
1663
+ pr_err("Default init %s failed (error %d)\n",
1664
+ CONFIG_DEFAULT_INIT, ret);
1665
+ else
1666
+ return 0;
1667
+ }
1668
+
13441669 if (!try_to_run_init_process("/sbin/init") ||
13451670 !try_to_run_init_process("/etc/init") ||
13461671 !try_to_run_init_process("/bin/init") ||
....@@ -1349,6 +1674,21 @@
13491674
13501675 panic("No working init found. Try passing init= option to kernel. "
13511676 "See Linux Documentation/admin-guide/init.rst for guidance.");
1677
+}
1678
+
1679
+/* Open /dev/console, for stdin/stdout/stderr, this should never fail */
1680
+void __init console_on_rootfs(void)
1681
+{
1682
+ struct file *file = filp_open("/dev/console", O_RDWR, 0);
1683
+
1684
+ if (IS_ERR(file)) {
1685
+ pr_err("Warning: unable to open an initial console.\n");
1686
+ return;
1687
+ }
1688
+ init_dup(file);
1689
+ init_dup(file);
1690
+ init_dup(file);
1691
+ fput(file);
13521692 }
13531693
13541694 static noinline void __init kernel_init_freeable(void)
....@@ -1374,42 +1714,37 @@
13741714
13751715 init_mm_internals();
13761716
1717
+ rcu_init_tasks_generic();
13771718 do_pre_smp_initcalls();
13781719 lockup_detector_init();
13791720
13801721 smp_init();
13811722 sched_init_smp();
13821723
1383
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
1384
- kthread_run(defer_free_bootmem, NULL, "defer_mem");
1724
+#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
1725
+ kthread_run(defer_free_memblock, NULL, "defer_mem");
13851726 #endif
13861727
1728
+ padata_init();
13871729 page_alloc_init_late();
13881730 /* Initialize page ext after all struct pages are initialized. */
13891731 page_ext_init();
13901732
13911733 do_basic_setup();
13921734
1735
+ kunit_run_all_tests();
1736
+
13931737 #if IS_BUILTIN(CONFIG_INITRD_ASYNC)
13941738 async_synchronize_full();
13951739 #endif
13961740
1397
- /* Open the /dev/console on the rootfs, this should never fail */
1398
- if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1399
- pr_err("Warning: unable to open an initial console.\n");
1741
+ console_on_rootfs();
14001742
1401
- (void) ksys_dup(0);
1402
- (void) ksys_dup(0);
14031743 /*
14041744 * check if there is an early userspace init. If yes, let it do all
14051745 * the work
14061746 */
1407
-
1408
- if (!ramdisk_execute_command)
1409
- ramdisk_execute_command = "/init";
1410
-
1411
- if (ksys_access((const char __user *)
1412
- ramdisk_execute_command, 0) != 0) {
1747
+ if (init_eaccess(ramdisk_execute_command) != 0) {
14131748 ramdisk_execute_command = NULL;
14141749 prepare_namespace();
14151750 }
....@@ -1424,5 +1759,4 @@
14241759 */
14251760
14261761 integrity_load_keys();
1427
- load_default_modules();
14281762 }