Skip to contentSkip to conversation

kernel/crash: make parse_crashkernel()'s return value more indicant

PATCH Superseded Pingfan Liu 2 participants 3 messages Apr 19, 2019

Subsystems (8)
Arm (32-bit) x86 PowerPC MIPS +4 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/crash: make parse_crashkernel()'s return value more indicant

At present, both return and crash_size should be checked to guarantee the success of parse_crashkernel().
Simplify the way by returning negative if fail, positive if success. In case of failure, -EINVAL for bad syntax, -1 for the parsing results in crash_size=0.

Cc Russell King <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Tony Luck <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Paul Burton <[email protected]>
Cc James Hogan <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Yoshinori Sato <[email protected]>
Cc Rich Felker <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Julien Thierry <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Logan Gunthorpe <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Greg Hackmann <[email protected]>
Cc Stefan Agner <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Yangtao Li <[email protected]>
Cc Dave Young <[email protected]>
Cc Baoquan He <[email protected]>
---
 arch/arm/kernel/setup.c             |  2 +-
 arch/arm64/mm/init.c                |  2 +-
 arch/ia64/kernel/setup.c            |  2 +-
 arch/mips/kernel/setup.c            |  2 +-
 arch/powerpc/kernel/fadump.c        |  2 +-
 arch/powerpc/kernel/machine_kexec.c |  2 +-
 arch/s390/kernel/setup.c            |  2 +-
 arch/sh/kernel/machine_kexec.c      |  2 +-
 arch/x86/kernel/setup.c             |  4 ++--
 kernel/crash_core.c                 | 12 ++++++++++--
 10 files changed, 20 insertions(+), 12 deletions(-)
arch/arm/kernel/setup.c +1 -1
index 5d78b6a..2feab13 100644
@@ -997,7 +997,7 @@ static void __init reserve_crashkernel(void)
 	total_mem = get_total_mem();
 	ret = parse_crashkernel(boot_command_line, total_mem,
 				&crash_size, &crash_base);
-	if (ret)
+	if (ret < 0)
 		return;
 
 	if (crash_base <= 0) {
arch/arm64/mm/init.c +1 -1
index 6bc1350..240918c 100644
@@ -79,7 +79,7 @@ static void __init reserve_crashkernel(void)
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 				&crash_size, &crash_base);
 	/* no crashkernel= or invalid value specified */
-	if (ret || !crash_size)
+	if (ret < 0)
 		return;
 
 	crash_size = PAGE_ALIGN(crash_size);
arch/ia64/kernel/setup.c +1 -1
index 583a374..99cae33 100644
@@ -277,7 +277,7 @@ static void __init setup_crashkernel(unsigned long total, int *n)
 
 	ret = parse_crashkernel(boot_command_line, total,
 			&size, &base);
-	if (ret = 0 && size > 0) {
+	if (ret >= 0) {
 		if (!base) {
 			sort_regions(rsvd_region, *n);
 			*n = merge_regions(rsvd_region, *n);
arch/mips/kernel/setup.c +1 -1
index 8d1dc6c..168571b 100644
@@ -715,7 +715,7 @@ static void __init mips_parse_crashkernel(void)
 	total_mem = get_total_mem();
 	ret = parse_crashkernel(boot_command_line, total_mem,
 				&crash_size, &crash_base);
-	if (ret != 0 || crash_size <= 0)
+	if (ret < 0)
 		return;
 
 	if (!memory_region_available(crash_base, crash_size)) {
arch/powerpc/kernel/fadump.c +1 -1
index 45a8d0b..0b626e2 100644
@@ -376,7 +376,7 @@ static inline unsigned long fadump_calculate_reserve_size(void)
 	 */
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 				&size, &base);
-	if (ret = 0 && size > 0) {
+	if (ret >= 0) {
 		unsigned long max_size;
 
 		if (fw_dump.reserve_bootvar)
arch/powerpc/kernel/machine_kexec.c +1 -1
index 63f5a93..9f3e61a 100644
@@ -122,7 +122,7 @@ void __init reserve_crashkernel(void)
 	/* use common parsing */
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 			&crash_size, &crash_base);
-	if (ret = 0 && crash_size > 0) {
+	if (ret >= 0) {
 		crashk_res.start = crash_base;
 		crashk_res.end = crash_base + crash_size - 1;
 	}
arch/s390/kernel/setup.c +1 -1
index 2c642af..d4bd61b 100644
@@ -671,7 +671,7 @@ static void __init reserve_crashkernel(void)
 
 	crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
 	crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
-	if (rc || crash_size = 0)
+	if (rc < 0)
 		return;
 
 	if (memblock.memory.regions[0].size < crash_size) {
arch/sh/kernel/machine_kexec.c +1 -1
index 63d63a3..612b21e 100644
@@ -157,7 +157,7 @@ void __init reserve_crashkernel(void)
 
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 			&crash_size, &crash_base);
-	if (ret = 0 && crash_size > 0) {
+	if (ret >= 0) {
 		crashk_res.start = crash_base;
 		crashk_res.end = crash_base + crash_size - 1;
 	}
arch/x86/kernel/setup.c +2 -2
index 3d872a5..62d07d4 100644
@@ -526,11 +526,11 @@ static void __init reserve_crashkernel(void)
 
 	/* crashkernel=XM */
 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
-	if (ret != 0 || crash_size <= 0) {
+	if (ret = -EINVAL) {
 		/* crashkernel=X,high */
 		ret = parse_crashkernel_high(boot_command_line, total_mem,
 					     &crash_size, &crash_base);
-		if (ret != 0 || crash_size <= 0)
+		if (ret < 0)
 			return;
 		high = true;
 	}
kernel/crash_core.c +10 -2
index 093c9f9..563d86d 100644
@@ -87,7 +87,7 @@ static int __init parse_crashkernel_mem(char *cmdline,
 		cur = tmp;
 		if (size >= system_ram) {
 			pr_warn("crashkernel: invalid size\n");
-			return -EINVAL;
+			return -1;
 		}
 
 		/* match ? */
@@ -108,8 +108,10 @@ static int __init parse_crashkernel_mem(char *cmdline,
 				return -EINVAL;
 			}
 		}
-	} else
+	} else {
 		pr_info("crashkernel size resulted in zero bytes\n");
+		return -1;
+	}
 
 	return 0;
 }
@@ -139,6 +141,8 @@ static int __init parse_crashkernel_simple(char *cmdline,
 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
 		return -EINVAL;
 	}
+	if (*crash_size = 0)
+		return -1;
 
 	return 0;
 }
@@ -181,6 +185,8 @@ static int __init parse_crashkernel_suffix(char *cmdline,
 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
 		return -EINVAL;
 	}
+	if (*crash_size = 0)
+		return -1;
 
 	return 0;
 }
@@ -266,6 +272,8 @@ static int __init __parse_crashkernel(char *cmdline,
 /*
  * That function is the entry point for command line parsing and should be
  * called from the arch-specific code.
+ * On success 0 or 1 if @offset is given. On error -EINVAL if bad syntax,
+ * or -1 if the parsing results in crash_size=0.
  */
 int __init parse_crashkernel(char *cmdline,
 			     unsigned long long system_ram,
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · Pingfan Liu

At present, both return and crash_size should be checked to guarantee the success of parse_crashkernel().
Simplify the way by returning negative if fail, positive if success. In case of failure, -EINVAL for bad syntax, -1 for the parsing results in crash_size=0.

I'm not entirely sure what you are trying to say here, but '-1' is not an improvement at all. We surely are not short of proper error codes, right?

Also I don't see any positive return value > 0. So what is this about:

arch/ia64/kernel/setup.c static void __init setup_crashkernel(unsigned long total, int *n) · line 280

Quoted 8 lines
arch/ia64/kernel/setup.c +1 -1
@@ -277,7 +277,7 @@ static void __init setup_crashkernel(unsigned long total, int *n)
 
 	ret = parse_crashkernel(boot_command_line, total,
 			&size, &base);
-	if (ret = 0 && size > 0) {
+	if (ret >= 0) {

^^^^^^^^^^^^^^^^^^^^^^^^^^^ ????

arch/powerpc/kernel/fadump.c static inline unsigned long fadump_calculate_reserve_size(void) · line 379

Quoted 11 lines
arch/ia64/kernel/setup.c +0 -0
 	if (!memory_region_available(crash_base, crash_size)) {
arch/powerpc/kernel/fadump.c +1 -1
index 45a8d0b..0b626e2 100644
@@ -376,7 +376,7 @@ static inline unsigned long fadump_calculate_reserve_size(void)
 	 */
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 				&size, &base);
-	if (ret = 0 && size > 0) {
+	if (ret >= 0) {

and this ?

arch/powerpc/kernel/machine_kexec.c void __init reserve_crashkernel(void) · line 125

Quoted 13 lines
arch/powerpc/kernel/fadump.c +0 -0
 		unsigned long max_size;
 
 		if (fw_dump.reserve_bootvar)
arch/powerpc/kernel/machine_kexec.c +1 -1
index 63f5a93..9f3e61a 100644
@@ -122,7 +122,7 @@ void __init reserve_crashkernel(void)
 	/* use common parsing */
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 			&crash_size, &crash_base);
-	if (ret = 0 && crash_size > 0) {
+	if (ret >= 0) {

Again.

arch/sh/kernel/machine_kexec.c void __init reserve_crashkernel(void) · line 160

Quoted 11 lines
arch/powerpc/kernel/machine_kexec.c +0 -0
 		crashk_res.start = crash_base;
 		crashk_res.end = crash_base + crash_size - 1;
 	}
arch/sh/kernel/machine_kexec.c +1 -1
@@ -157,7 +157,7 @@ void __init reserve_crashkernel(void)
 
 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
 			&crash_size, &crash_base);
-	if (ret = 0 && crash_size > 0) {
+	if (ret >= 0) {

And some more.

arch/x86/kernel/setup.c static void __init reserve_crashkernel(void) · line 529

Quoted 13 lines
arch/sh/kernel/machine_kexec.c +0 -0
 		crashk_res.start = crash_base;
 		crashk_res.end = crash_base + crash_size - 1;
 	}
arch/x86/kernel/setup.c +1 -1
index 3d872a5..62d07d4 100644
@@ -526,11 +526,11 @@ static void __init reserve_crashkernel(void)
 
 	/* crashkernel=XM */
 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
-	if (ret != 0 || crash_size <= 0) {
+	if (ret = -EINVAL) {

Without an explanation why this proceedes on error codes other than EINVAL this is uncomprehensible. Comments exist for a reason.

Quoted 9 lines
arch/x86/kernel/setup.c +1 -1
 		/* crashkernel=X,high */
 		ret = parse_crashkernel_high(boot_command_line, total_mem,
 					     &crash_size, &crash_base);
-		if (ret != 0 || crash_size <= 0)
+		if (ret < 0)
 			return;
 		high = true;
 

arch/x86/kernel/setup.c static int __init parse_crashkernel_mem(char *cmdline, · line 90

Quoted 5 lines
arch/x86/kernel/setup.c +1 -1
@@ -87,7 +87,7 @@ static int __init parse_crashkernel_mem(char *cmdline,
 		cur = tmp;
 		if (size >= system_ram) {
 			pr_warn("crashkernel: invalid size\n");
-			return -EINVAL;
+			return -1;

Well, this is incomprehensible as well. The pr_warn() says invalid and then you change the error code to something magic.

Thanks,

tglx

#2 lore raw
Quoted 12 lines · Thomas Gleixner
Quoted 5 lines · Pingfan Liu

At present, both return and crash_size should be checked to guarantee the success of parse_crashkernel().
Simplify the way by returning negative if fail, positive if success. In case of failure, -EINVAL for bad syntax, -1 for the parsing results in crash_size=0.

I'm not entirely sure what you are trying to say here, but '-1' is not an improvement at all. We surely are not short of proper error codes, right?

The different negative return values are only used by x86. The option "crashkernel=X,high", which is only used on x86, causes parse_kernel() to return -EINVAL, then let parse_crashkernel_high() have a try.

When parsing crashkernel=size@offset and crashkernel=range1:size1, there are other cases of failure, which is not worth to call parse_crashkernel_high() to have a try. That is "-1" aiming for.

First, in parse_crashkernel_mem(), if demanded size is bigger than system ram, this one looks like -ENOMEM, but -ENOMEM normally is used for allocation. Second, in parse_crashkernel_mem(), if system ram is not inside the range listed by "crashkernel=". Third, crashkernel=0MB is given in the option (not in practice, but can not forbid user to do so).

All of these cases can be treated as -EINVAL, but hard to define the error codes.

Quoted 2 lines

Also I don't see any positive return value > 0. So what is this about:

Yes. 0 is enough for success. I had thought about returning 1 if @offset is specified in crashkernel. But at present, no use case for it.

Quoted 64 lines

arch/ia64/kernel/setup.c static void __init setup_crashkernel(unsigned long total, int *n) · line 280

Quoted 8 lines
arch/ia64/kernel/setup.c +1 -1
@@ -277,7 +277,7 @@ static void __init setup_crashkernel(unsigned long total, int *n)
 
      ret = parse_crashkernel(boot_command_line, total,
                      &size, &base);
-     if (ret = 0 && size > 0) {
+     if (ret >= 0) {

^^^^^^^^^^^^^^^^^^^^^^^^^^^ ????

arch/powerpc/kernel/fadump.c static inline unsigned long fadump_calculate_reserve_size(void) · line 379

Quoted 11 lines
arch/ia64/kernel/setup.c +0 -0
      if (!memory_region_available(crash_base, crash_size)) {
arch/powerpc/kernel/fadump.c +1 -1
index 45a8d0b..0b626e2 100644
@@ -376,7 +376,7 @@ static inline unsigned long fadump_calculate_reserve_size(void)
       */
      ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
                              &size, &base);
-     if (ret = 0 && size > 0) {
+     if (ret >= 0) {

and this ?

arch/powerpc/kernel/machine_kexec.c void __init reserve_crashkernel(void) · line 125

Quoted 13 lines
arch/powerpc/kernel/fadump.c +0 -0
              unsigned long max_size;
 
              if (fw_dump.reserve_bootvar)
arch/powerpc/kernel/machine_kexec.c +1 -1
index 63f5a93..9f3e61a 100644
@@ -122,7 +122,7 @@ void __init reserve_crashkernel(void)
      /* use common parsing */
      ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
                      &crash_size, &crash_base);
-     if (ret = 0 && crash_size > 0) {
+     if (ret >= 0) {

Again.

arch/sh/kernel/machine_kexec.c void __init reserve_crashkernel(void) · line 160

Quoted 11 lines
arch/powerpc/kernel/machine_kexec.c +0 -0
              crashk_res.start = crash_base;
              crashk_res.end = crash_base + crash_size - 1;
      }
arch/sh/kernel/machine_kexec.c +1 -1
@@ -157,7 +157,7 @@ void __init reserve_crashkernel(void)
 
      ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
                      &crash_size, &crash_base);
-     if (ret = 0 && crash_size > 0) {
+     if (ret >= 0) {

And some more.

Quoted 9 lines
arch/sh/kernel/machine_kexec.c +0 -0
              crashk_res.start = crash_base;
              crashk_res.end = crash_base + crash_size - 1;
      }
arch/x86/kernel/setup.c +0 -0
index 3d872a5..62d07d4 100644
@@ -526,11 +526,11 @@ static void __init reserve_crashkernel(void)
 
Quoted 8 lines

arch/x86/kernel/setup.c static void __init reserve_crashkernel(void) · line 529

Quoted 4 lines
arch/x86/kernel/setup.c +1 -1
      /* crashkernel=XM */
      ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
-     if (ret != 0 || crash_size <= 0) {
+     if (ret = -EINVAL) {

Without an explanation why this proceedes on error codes other than EINVAL this is uncomprehensible. Comments exist for a reason.

As explained above, deciding whether to let parse_crashkernel_high() try.

Quoted 8 lines
Quoted 7 lines
arch/x86/kernel/setup.c +1 -1
              /* crashkernel=X,high */
              ret = parse_crashkernel_high(boot_command_line, total_mem,
                                           &crash_size, &crash_base);
-             if (ret != 0 || crash_size <= 0)
+             if (ret < 0)
                      return;
              high = true;
Quoted 10 lines

arch/x86/kernel/setup.c static int __init parse_crashkernel_mem(char *cmdline, · line 90

Quoted 6 lines
arch/x86/kernel/setup.c +1 -1
@@ -87,7 +87,7 @@ static int __init parse_crashkernel_mem(char *cmdline,
              cur = tmp;
              if (size >= system_ram) {
                      pr_warn("crashkernel: invalid size\n");
-                     return -EINVAL;
+                     return -1;

Well, this is incomprehensible as well. The pr_warn() says invalid and then you change the error code to something magic.

As explained above, want to know whether worth to let parse_crashkernel_high() try.

What about the following alternative method? Treating crash_size=0 as -EINVAL. Then on x86, just call parse_crashkernel_high() blindly to have a try. Thanks for your review.

Regards,
Pingfan