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,12 +863,12 @@
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();
583
- softirq_early_init();
584872 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
585873 boot_cpu_hotplug_init();
586874
....@@ -589,10 +877,10 @@
589877
590878 #ifdef CONFIG_ARCH_ROCKCHIP
591879 {
592
- const char *s = boot_command_line;
593
- 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)];
594882 int n =
595
- pr_notice("Kernel command line: %s\n", boot_command_line);
883
+ pr_notice("Kernel command line: %s\n", saved_command_line);
596884 n -= strlen("Kernel command line: ");
597885 s += n;
598886 /* command line maybe too long to print one time */
....@@ -602,7 +890,7 @@
602890 }
603891 }
604892 #else
605
- pr_notice("Kernel command line: %s\n", boot_command_line);
893
+ pr_notice("Kernel command line: %s\n", saved_command_line);
606894 #endif
607895 /* parameters may set static keys */
608896 jump_label_init();
....@@ -614,6 +902,9 @@
614902 if (!IS_ERR_OR_NULL(after_dashes))
615903 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
616904 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);
617908
618909 /*
619910 * These use large bootmem allocations and must precede
....@@ -636,11 +927,7 @@
636927 * time - but meanwhile we still have a functioning scheduler.
637928 */
638929 sched_init();
639
- /*
640
- * Disable preemption - early bootup scheduling is extremely
641
- * fragile until we cpu_idle() for the first time.
642
- */
643
- preempt_disable();
930
+
644931 if (WARN(!irqs_disabled(),
645932 "Interrupts were enabled *very* early, fixing it\n"))
646933 local_irq_disable();
....@@ -677,21 +964,19 @@
677964 hrtimers_init();
678965 softirq_init();
679966 timekeeping_init();
967
+ kfence_init();
968
+ time_init();
680969
681970 /*
682971 * For best initial stack canary entropy, prepare it after:
683972 * - setup_arch() for any UEFI RNG entropy and boot cmdline access
684
- * - timekeeping_init() for ktime entropy used in rand_initialize()
685
- * - rand_initialize() to get any arch-specific entropy like RDRAND
686
- * - add_latent_entropy() to get any latent entropy
687
- * - 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
688976 */
689
- rand_initialize();
690
- add_latent_entropy();
691
- add_device_randomness(command_line, strlen(command_line));
977
+ random_init(command_line);
692978 boot_init_stack_canary();
693979
694
- time_init();
695980 perf_event_init();
696981 profile_init();
697982 call_function_init();
....@@ -738,8 +1023,6 @@
7381023 initrd_start = 0;
7391024 }
7401025 #endif
741
- kmemleak_init();
742
- debug_objects_mem_init();
7431026 setup_per_cpu_pageset();
7441027 numa_policy_init();
7451028 acpi_early_init();
....@@ -773,18 +1056,16 @@
7731056 taskstats_init_early();
7741057 delayacct_init();
7751058
1059
+ poking_init();
7761060 check_bugs();
7771061
7781062 acpi_subsystem_init();
7791063 arch_post_acpi_subsys_init();
7801064 sfi_init_late();
781
-
782
- if (efi_enabled(EFI_RUNTIME_SERVICES)) {
783
- efi_free_boot_services();
784
- }
1065
+ kcsan_init();
7851066
7861067 /* Do the rest non-__init'ed, we're now alive */
787
- rest_init();
1068
+ arch_call_rest_init();
7881069
7891070 prevent_tail_call_optimization();
7901071 }
....@@ -818,14 +1099,22 @@
8181099 str_entry = strsep(&str, ",");
8191100 if (str_entry) {
8201101 pr_debug("blacklisting initcall %s\n", str_entry);
821
- entry = alloc_bootmem(sizeof(*entry));
822
- 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);
8231112 strcpy(entry->buf, str_entry);
8241113 list_add(&entry->next, &blacklisted_initcalls);
8251114 }
8261115 } while (str_entry);
8271116
828
- return 0;
1117
+ return 1;
8291118 }
8301119
8311120 static bool __init_or_module initcall_blacklisted(initcall_t fn)
....@@ -874,7 +1163,7 @@
8741163 {
8751164 ktime_t *calltime = (ktime_t *)data;
8761165
877
- 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));
8781167 *calltime = ktime_get();
8791168 }
8801169
....@@ -888,7 +1177,7 @@
8881177 rettime = ktime_get();
8891178 delta = ktime_sub(rettime, *calltime);
8901179 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
891
- printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
1180
+ printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
8921181 fn, ret, duration);
8931182 }
8941183
....@@ -945,7 +1234,7 @@
9451234 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
9461235 local_irq_enable();
9471236 }
948
- WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
1237
+ WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
9491238
9501239 add_latent_entropy();
9511240 return ret;
....@@ -976,7 +1265,7 @@
9761265 };
9771266
9781267 /* Keep these in sync with initcalls in include/linux/init.h */
979
-static char *initcall_level_names[] __initdata = {
1268
+static const char *initcall_level_names[] __initdata = {
9801269 "pure",
9811270 "core",
9821271 "postcore",
....@@ -986,6 +1275,12 @@
9861275 "device",
9871276 "late",
9881277 };
1278
+
1279
+static int __init ignore_unknown_bootoption(char *param, char *val,
1280
+ const char *unused, void *arg)
1281
+{
1282
+ return 0;
1283
+}
9891284
9901285 #ifdef CONFIG_INITCALL_ASYNC
9911286 extern initcall_entry_t __initcall0s_start[];
....@@ -1164,16 +1459,15 @@
11641459 }
11651460 #endif /* CONFIG_INITCALL_ASYNC */
11661461
1167
-static void __init do_initcall_level(int level)
1462
+static void __init do_initcall_level(int level, char *command_line)
11681463 {
11691464 initcall_entry_t *fn;
11701465
1171
- strcpy(initcall_command_line, saved_command_line);
11721466 parse_args(initcall_level_names[level],
1173
- initcall_command_line, __start___param,
1467
+ command_line, __start___param,
11741468 __stop___param - __start___param,
11751469 level, level,
1176
- NULL, &repair_env_string);
1470
+ NULL, ignore_unknown_bootoption);
11771471
11781472 trace_initcall_level(initcall_level_names[level]);
11791473
....@@ -1190,13 +1484,24 @@
11901484 static void __init do_initcalls(void)
11911485 {
11921486 int level;
1487
+ size_t len = strlen(saved_command_line) + 1;
1488
+ char *command_line;
11931489
11941490 #ifdef CONFIG_INITCALL_ASYNC
11951491 initcall_init_workers();
11961492 #endif
11971493
1198
- for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
1199
- 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);
12001505
12011506 #ifdef CONFIG_INITCALL_ASYNC
12021507 initcall_free_works();
....@@ -1213,7 +1518,6 @@
12131518 static void __init do_basic_setup(void)
12141519 {
12151520 cpuset_init_smp();
1216
- shmem_init();
12171521 driver_init();
12181522 init_irq_proc();
12191523 do_ctors();
....@@ -1230,24 +1534,19 @@
12301534 do_one_initcall(initcall_from_entry(fn));
12311535 }
12321536
1233
-/*
1234
- * This function requests modules which should be loaded by default and is
1235
- * called twice right after initrd is mounted and right before init is
1236
- * exec'd. If such modules are on either initrd or rootfs, they will be
1237
- * loaded before control is passed to userland.
1238
- */
1239
-void __init load_default_modules(void)
1240
-{
1241
- load_default_elevator_module();
1242
-}
1243
-
12441537 static int run_init_process(const char *init_filename)
12451538 {
1539
+ const char *const *p;
1540
+
12461541 argv_init[0] = init_filename;
12471542 pr_info("Run %s as init process\n", init_filename);
1248
- return do_execve(getname_kernel(init_filename),
1249
- (const char __user *const __user *)argv_init,
1250
- (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);
12511550 }
12521551
12531552 static int try_to_run_init_process(const char *init_filename)
....@@ -1270,7 +1569,9 @@
12701569 bool rodata_enabled __ro_after_init = true;
12711570 static int __init set_debug_rodata(char *str)
12721571 {
1273
- 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;
12741575 }
12751576 __setup("rodata=", set_debug_rodata);
12761577 #endif
....@@ -1280,16 +1581,21 @@
12801581 {
12811582 if (rodata_enabled) {
12821583 /*
1283
- * load_module() results in W+X mappings, which are cleaned up
1284
- * 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
12851586 * flushed so that we don't hit false positives looking for
12861587 * insecure pages which are W+X.
12871588 */
1288
- rcu_barrier_sched();
1589
+ rcu_barrier();
12891590 mark_rodata_ro();
12901591 rodata_test();
12911592 } else
12921593 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");
12931599 }
12941600 #else
12951601 static inline void mark_readonly(void)
....@@ -1298,6 +1604,11 @@
12981604 }
12991605 #endif
13001606
1607
+void __weak free_initmem(void)
1608
+{
1609
+ free_initmem_default(POISON_FREE_INITMEM);
1610
+}
1611
+
13011612 static int __ref kernel_init(void *unused)
13021613 {
13031614 int ret;
....@@ -1305,8 +1616,9 @@
13051616 kernel_init_freeable();
13061617 /* need to finish all async __init code before freeing the memory */
13071618 async_synchronize_full();
1619
+ kprobe_free_init_mem();
13081620 ftrace_free_init_mem();
1309
- jump_label_invalidate_initmem();
1621
+ kgdb_free_init_mem();
13101622 free_initmem();
13111623 mark_readonly();
13121624
....@@ -1320,6 +1632,8 @@
13201632 numa_default_policy();
13211633
13221634 rcu_end_inkernel_boot();
1635
+
1636
+ do_sysctl_args();
13231637
13241638 if (ramdisk_execute_command) {
13251639 ret = run_init_process(ramdisk_execute_command);
....@@ -1342,6 +1656,16 @@
13421656 panic("Requested init %s failed (error %d).",
13431657 execute_command, ret);
13441658 }
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
+
13451669 if (!try_to_run_init_process("/sbin/init") ||
13461670 !try_to_run_init_process("/etc/init") ||
13471671 !try_to_run_init_process("/bin/init") ||
....@@ -1350,6 +1674,21 @@
13501674
13511675 panic("No working init found. Try passing init= option to kernel. "
13521676 "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);
13531692 }
13541693
13551694 static noinline void __init kernel_init_freeable(void)
....@@ -1375,42 +1714,37 @@
13751714
13761715 init_mm_internals();
13771716
1717
+ rcu_init_tasks_generic();
13781718 do_pre_smp_initcalls();
13791719 lockup_detector_init();
13801720
13811721 smp_init();
13821722 sched_init_smp();
13831723
1384
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
1385
- 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");
13861726 #endif
13871727
1728
+ padata_init();
13881729 page_alloc_init_late();
13891730 /* Initialize page ext after all struct pages are initialized. */
13901731 page_ext_init();
13911732
13921733 do_basic_setup();
13931734
1735
+ kunit_run_all_tests();
1736
+
13941737 #if IS_BUILTIN(CONFIG_INITRD_ASYNC)
13951738 async_synchronize_full();
13961739 #endif
13971740
1398
- /* Open the /dev/console on the rootfs, this should never fail */
1399
- if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1400
- pr_err("Warning: unable to open an initial console.\n");
1741
+ console_on_rootfs();
14011742
1402
- (void) ksys_dup(0);
1403
- (void) ksys_dup(0);
14041743 /*
14051744 * check if there is an early userspace init. If yes, let it do all
14061745 * the work
14071746 */
1408
-
1409
- if (!ramdisk_execute_command)
1410
- ramdisk_execute_command = "/init";
1411
-
1412
- if (ksys_access((const char __user *)
1413
- ramdisk_execute_command, 0) != 0) {
1747
+ if (init_eaccess(ramdisk_execute_command) != 0) {
14141748 ramdisk_execute_command = NULL;
14151749 prepare_namespace();
14161750 }
....@@ -1425,5 +1759,4 @@
14251759 */
14261760
14271761 integrity_load_keys();
1428
- load_default_modules();
14291762 }