csky: Add setup_initrd check code
authorGuo Ren <guoren@linux.alibaba.com>
Tue, 8 Oct 2019 06:25:13 +0000 (14:25 +0800)
committerGuo Ren <guoren@linux.alibaba.com>
Fri, 21 Feb 2020 07:43:25 +0000 (15:43 +0800)
We should give some necessary check for initrd just like other
architectures and it seems that setup_initrd() could be a common
code for all architectures.

Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
arch/csky/kernel/setup.c
arch/csky/mm/init.c

index 4fc7b16b77d249e422ec590b8d312468113ccc53..3821e55742f46f0070688f1e81161aefb8760f5c 100644 (file)
@@ -47,9 +47,6 @@ static void __init csky_memblock_init(void)
        signed long size;
 
        memblock_reserve(__pa(_stext), _end - _stext);
-#ifdef CONFIG_BLK_DEV_INITRD
-       memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
-#endif
 
        early_init_fdt_reserve_self();
        early_init_fdt_scan_reserved_mem();
index dbb4b2dfe4b7b50a5ab33a8a95be61b4db174821..cb64d8647a78b3eb9529b962e6267361e3c15897 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/swap.h>
 #include <linux/proc_fs.h>
 #include <linux/pfn.h>
+#include <linux/initrd.h>
 
 #include <asm/setup.h>
 #include <asm/cachectl.h>
@@ -36,6 +37,45 @@ unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
                                                __page_aligned_bss;
 EXPORT_SYMBOL(empty_zero_page);
 
+#ifdef CONFIG_BLK_DEV_INITRD
+static void __init setup_initrd(void)
+{
+       unsigned long size;
+
+       if (initrd_start >= initrd_end) {
+               pr_err("initrd not found or empty");
+               goto disable;
+       }
+
+       if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
+               pr_err("initrd extends beyond end of memory");
+               goto disable;
+       }
+
+       size = initrd_end - initrd_start;
+
+       if (memblock_is_region_reserved(__pa(initrd_start), size)) {
+               pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region",
+                      __pa(initrd_start), size);
+               goto disable;
+       }
+
+       memblock_reserve(__pa(initrd_start), size);
+
+       pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
+               (void *)(initrd_start), size);
+
+       initrd_below_start_ok = 1;
+
+       return;
+
+disable:
+       initrd_start = initrd_end = 0;
+
+       pr_err(" - disabling initrd\n");
+}
+#endif
+
 void __init mem_init(void)
 {
 #ifdef CONFIG_HIGHMEM
@@ -47,6 +87,10 @@ void __init mem_init(void)
 #endif
        high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
+#ifdef CONFIG_BLK_DEV_INITRD
+       setup_initrd();
+#endif
+
        memblock_free_all();
 
 #ifdef CONFIG_HIGHMEM