diff mbox series

[2/3] MIPS: Return -EINVAL if mem parameter is invalid in early_parse_mem()

Message ID 1647615920-23103-3-git-send-email-yangtiezhu@loongson.cn (mailing list archive)
State Superseded
Headers show
Series MIPS: Modify early_parse_mem() | expand

Commit Message

Tiezhu Yang March 18, 2022, 3:05 p.m. UTC
In the current code, the users usually need to make sure the value
of mem parameter is correct, but it is better to do some check to
avoid potential boot hangs.

This commit checks whether the first mem parameter is invalid, if
yes, return -EINVAL before call memblock_remove() and memblock_add().

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 arch/mips/kernel/setup.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 14aa8bd..c8c8f60 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -343,12 +343,19 @@  static int usermem __initdata;
 static int __init early_parse_mem(char *p)
 {
 	phys_addr_t start, size;
+	phys_addr_t pa_start, pa_end;
+	u64 idx;
 
 	if (!p) {
 		pr_err("mem parameter is empty, do nothing\n");
 		return -EINVAL;
 	}
 
+	start = 0;
+	size = memparse(p, &p);
+	if (*p == '@')
+		start = memparse(p + 1, &p);
+
 	/*
 	 * If a user specifies memory size, we
 	 * blow away any automatically generated
@@ -356,13 +363,20 @@  static int __init early_parse_mem(char *p)
 	 */
 	if (usermem == 0) {
 		usermem = 1;
+		for_each_mem_range(idx, &pa_start, &pa_end) {
+			if (start >= pa_start && size <= pa_end - pa_start)
+				break;
+
+			if (idx < memblock.memory.cnt)
+				continue;
+
+			usermem = -1;
+			pr_err("mem parameter is invalid, do nothing\n");
+			return -EINVAL;
+		}
 		memblock_remove(memblock_start_of_DRAM(),
 			memblock_end_of_DRAM() - memblock_start_of_DRAM());
 	}
-	start = 0;
-	size = memparse(p, &p);
-	if (*p == '@')
-		start = memparse(p + 1, &p);
 
 	memblock_add(start, size);
 
@@ -638,7 +652,7 @@  static void __init arch_mem_init(char **cmdline_p)
 
 	parse_early_param();
 
-	if (usermem)
+	if (usermem == 1)
 		pr_info("User-defined physical RAM map overwrite\n");
 
 	check_kernel_sections_mem();