Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kexec: split crashkernel reservation code out from crash_core.c

Patch series "Split crash out from kexec and clean up related config
items", v3.

Motivation:
=============
Previously, LKP reported a building error. When investigating, it can't
be resolved reasonablly with the present messy kdump config items.

https://lore.kernel.org/oe-kbuild-all/202312182200.Ka7MzifQ-lkp@intel.com/

The kdump (crash dumping) related config items could causes confusions:

Firstly,

CRASH_CORE enables codes including
- crashkernel reservation;
- elfcorehdr updating;
- vmcoreinfo exporting;
- crash hotplug handling;

Now fadump of powerpc, kcore dynamic debugging and kdump all selects
CRASH_CORE, while fadump
- fadump needs crashkernel parsing, vmcoreinfo exporting, and accessing
global variable 'elfcorehdr_addr';
- kcore only needs vmcoreinfo exporting;
- kdump needs all of the current kernel/crash_core.c.

So only enabling PROC_CORE or FA_DUMP will enable CRASH_CORE, this
mislead people that we enable crash dumping, actual it's not.

Secondly,

It's not reasonable to allow KEXEC_CORE select CRASH_CORE.

Because KEXEC_CORE enables codes which allocate control pages, copy
kexec/kdump segments, and prepare for switching. These codes are
shared by both kexec reboot and kdump. We could want kexec reboot,
but disable kdump. In that case, CRASH_CORE should not be selected.

--------------------
CONFIG_CRASH_CORE=y
CONFIG_KEXEC_CORE=y
CONFIG_KEXEC=y
CONFIG_KEXEC_FILE=y
---------------------

Thirdly,

It's not reasonable to allow CRASH_DUMP select KEXEC_CORE.

That could make KEXEC_CORE, CRASH_DUMP are enabled independently from
KEXEC or KEXEC_FILE. However, w/o KEXEC or KEXEC_FILE, the KEXEC_CORE
code built in doesn't make any sense because no kernel loading or
switching will happen to utilize the KEXEC_CORE code.
---------------------
CONFIG_CRASH_CORE=y
CONFIG_KEXEC_CORE=y
CONFIG_CRASH_DUMP=y
---------------------

In this case, what is worse, on arch sh and arm, KEXEC relies on MMU,
while CRASH_DUMP can still be enabled when !MMU, then compiling error is
seen as the lkp test robot reported in above link.

------arch/sh/Kconfig------
config ARCH_SUPPORTS_KEXEC
def_bool MMU

config ARCH_SUPPORTS_CRASH_DUMP
def_bool BROKEN_ON_SMP
---------------------------

Changes:
===========
1, split out crash_reserve.c from crash_core.c;
2, split out vmcore_infoc. from crash_core.c;
3, move crash related codes in kexec_core.c into crash_core.c;
4, remove dependency of FA_DUMP on CRASH_DUMP;
5, clean up kdump related config items;
6, wrap up crash codes in crash related ifdefs on all 8 arch-es
which support crash dumping, except of ppc;

Achievement:
===========
With above changes, I can rearrange the config item logic as below (the right
item depends on or is selected by the left item):

PROC_KCORE -----------> VMCORE_INFO

|----------> VMCORE_INFO
FA_DUMP----|
|----------> CRASH_RESERVE

---->VMCORE_INFO
/
|---->CRASH_RESERVE
KEXEC --| /|
|--> KEXEC_CORE--> CRASH_DUMP-->/-|---->PROC_VMCORE
KEXEC_FILE --| \ |
\---->CRASH_HOTPLUG


KEXEC --|
|--> KEXEC_CORE (for kexec reboot only)
KEXEC_FILE --|

Test
========
On all 8 architectures, including x86_64, arm64, s390x, sh, arm, mips,
riscv, loongarch, I did below three cases of config item setting and
building all passed. Take configs on x86_64 as exampmle here:

(1) Both CONFIG_KEXEC and KEXEC_FILE is unset, then all kexec/kdump
items are unset automatically:
# Kexec and crash features
# CONFIG_KEXEC is not set
# CONFIG_KEXEC_FILE is not set
# end of Kexec and crash features

(2) set CONFIG_KEXEC_FILE and 'make olddefconfig':
---------------
# Kexec and crash features
CONFIG_CRASH_RESERVE=y
CONFIG_VMCORE_INFO=y
CONFIG_KEXEC_CORE=y
CONFIG_KEXEC_FILE=y
CONFIG_CRASH_DUMP=y
CONFIG_CRASH_HOTPLUG=y
CONFIG_CRASH_MAX_MEMORY_RANGES=8192
# end of Kexec and crash features
---------------

(3) unset CONFIG_CRASH_DUMP in case 2 and execute 'make olddefconfig':
------------------------
# Kexec and crash features
CONFIG_KEXEC_CORE=y
CONFIG_KEXEC_FILE=y
# end of Kexec and crash features
------------------------

Note:
For ppc, it needs investigation to make clear how to split out crash
code in arch folder. Hope Hari and Pingfan can help have a look, see if
it's doable. Now, I make it either have both kexec and crash enabled, or
disable both of them altogether.


This patch (of 14):

Both kdump and fa_dump of ppc rely on crashkernel reservation. Move the
relevant codes into separate files: crash_reserve.c,
include/linux/crash_reserve.h.

And also add config item CRASH_RESERVE to control its enabling of the
codes. And update config items which has relationship with crashkernel
reservation.

And also change ifdeffery from CONFIG_CRASH_CORE to CONFIG_CRASH_RESERVE
when those scopes are only crashkernel reservation related.

And also rename arch/XXX/include/asm/{crash_core.h => crash_reserve.h} on
arm64, x86 and risc-v because those architectures' crash_core.h is only
related to crashkernel reservation.

[akpm@linux-foundation.org: s/CRASH_RESEERVE/CRASH_RESERVE/, per Klara Modin]
Link: https://lkml.kernel.org/r/20240124051254.67105-1-bhe@redhat.com
Link: https://lkml.kernel.org/r/20240124051254.67105-2-bhe@redhat.com
Signed-off-by: Baoquan He <bhe@redhat.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Pingfan Liu <piliu@redhat.com>
Cc: Klara Modin <klarasmodin@gmail.com>
Cc: Michael Kelley <mhklinux@outlook.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Yang Li <yang.lee@linux.alibaba.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Baoquan He and committed by
Andrew Morton
85fcde40 8be4d46e

+531 -491
+1 -1
arch/arm64/Kconfig
··· 1519 1519 def_bool y 1520 1520 1521 1521 config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 1522 - def_bool CRASH_CORE 1522 + def_bool CRASH_RESERVE 1523 1523 1524 1524 config TRANS_TABLE 1525 1525 def_bool y
+2 -2
arch/arm64/include/asm/crash_core.h arch/arm64/include/asm/crash_reserve.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 - #ifndef _ARM64_CRASH_CORE_H 3 - #define _ARM64_CRASH_CORE_H 2 + #ifndef _ARM64_CRASH_RESERVE_H 3 + #define _ARM64_CRASH_RESERVE_H 4 4 5 5 /* Current arm64 boot protocol requires 2MB alignment */ 6 6 #define CRASH_ALIGN SZ_2M
+1
arch/powerpc/Kconfig
··· 691 691 bool "Firmware-assisted dump" 692 692 depends on PPC64 && (PPC_RTAS || PPC_POWERNV) 693 693 select CRASH_CORE 694 + select CRASH_RESERVE 694 695 select CRASH_DUMP 695 696 help 696 697 A robust mechanism to get reliable kernel crash dump with
+2 -2
arch/powerpc/mm/nohash/kaslr_booke.c
··· 13 13 #include <linux/delay.h> 14 14 #include <linux/memblock.h> 15 15 #include <linux/libfdt.h> 16 - #include <linux/crash_core.h> 16 + #include <linux/crash_reserve.h> 17 17 #include <linux/of.h> 18 18 #include <linux/of_fdt.h> 19 19 #include <asm/cacheflush.h> ··· 173 173 174 174 static void __init get_crash_kernel(void *fdt, unsigned long size) 175 175 { 176 - #ifdef CONFIG_CRASH_CORE 176 + #ifdef CONFIG_CRASH_RESERVE 177 177 unsigned long long crash_size, crash_base; 178 178 int ret; 179 179
+1 -1
arch/riscv/Kconfig
··· 767 767 def_bool y 768 768 769 769 config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 770 - def_bool CRASH_CORE 770 + def_bool CRASH_RESERVE 771 771 772 772 config COMPAT 773 773 bool "Kernel support for 32-bit U-mode"
+2 -2
arch/riscv/include/asm/crash_core.h arch/riscv/include/asm/crash_reserve.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 - #ifndef _RISCV_CRASH_CORE_H 3 - #define _RISCV_CRASH_CORE_H 2 + #ifndef _RISCV_CRASH_RESERVE_H 3 + #define _RISCV_CRASH_RESERVE_H 4 4 5 5 #define CRASH_ALIGN PMD_SIZE 6 6
+1 -1
arch/x86/Kconfig
··· 2106 2106 def_bool y 2107 2107 2108 2108 config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 2109 - def_bool CRASH_CORE 2109 + def_bool CRASH_RESERVE 2110 2110 2111 2111 config PHYSICAL_START 2112 2112 hex "Physical address where the kernel is loaded" if (EXPERT || CRASH_DUMP)
+3 -3
arch/x86/include/asm/crash_core.h arch/x86/include/asm/crash_reserve.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _X86_CRASH_CORE_H 3 - #define _X86_CRASH_CORE_H 2 + #ifndef _X86_CRASH_RESERVE_H 3 + #define _X86_CRASH_RESERVE_H 4 4 5 5 /* 16M alignment for crash kernel regions */ 6 6 #define CRASH_ALIGN SZ_16M ··· 39 39 #endif 40 40 } 41 41 42 - #endif /* _X86_CRASH_CORE_H */ 42 + #endif /* _X86_CRASH_RESERVE_H */
-40
include/linux/crash_core.h
··· 5 5 #include <linux/linkage.h> 6 6 #include <linux/elfcore.h> 7 7 #include <linux/elf.h> 8 - #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 9 - #include <asm/crash_core.h> 10 - #endif 11 - 12 - /* Location of a reserved region to hold the crash kernel. 13 - */ 14 - extern struct resource crashk_res; 15 - extern struct resource crashk_low_res; 16 8 17 9 #define CRASH_CORE_NOTE_NAME "CORE" 18 10 #define CRASH_CORE_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4) ··· 78 86 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 79 87 void *data, size_t data_len); 80 88 void final_note(Elf_Word *buf); 81 - 82 - int __init parse_crashkernel(char *cmdline, unsigned long long system_ram, 83 - unsigned long long *crash_size, unsigned long long *crash_base, 84 - unsigned long long *low_size, bool *high); 85 - 86 - #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 87 - #ifndef DEFAULT_CRASH_KERNEL_LOW_SIZE 88 - #define DEFAULT_CRASH_KERNEL_LOW_SIZE (128UL << 20) 89 - #endif 90 - #ifndef CRASH_ALIGN 91 - #define CRASH_ALIGN SZ_2M 92 - #endif 93 - #ifndef CRASH_ADDR_LOW_MAX 94 - #define CRASH_ADDR_LOW_MAX SZ_4G 95 - #endif 96 - #ifndef CRASH_ADDR_HIGH_MAX 97 - #define CRASH_ADDR_HIGH_MAX memblock_end_of_DRAM() 98 - #endif 99 - 100 - void __init reserve_crashkernel_generic(char *cmdline, 101 - unsigned long long crash_size, 102 - unsigned long long crash_base, 103 - unsigned long long crash_low_size, 104 - bool high); 105 - #else 106 - static inline void __init reserve_crashkernel_generic(char *cmdline, 107 - unsigned long long crash_size, 108 - unsigned long long crash_base, 109 - unsigned long long crash_low_size, 110 - bool high) 111 - {} 112 - #endif 113 89 114 90 /* Alignment required for elf header segment */ 115 91 #define ELF_CORE_HEADER_ALIGN 4096
+48
include/linux/crash_reserve.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef LINUX_CRASH_RESERVE_H 3 + #define LINUX_CRASH_RESERVE_H 4 + 5 + #include <linux/linkage.h> 6 + #include <linux/elfcore.h> 7 + #include <linux/elf.h> 8 + #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 9 + #include <asm/crash_reserve.h> 10 + #endif 11 + 12 + /* Location of a reserved region to hold the crash kernel. 13 + */ 14 + extern struct resource crashk_res; 15 + extern struct resource crashk_low_res; 16 + 17 + int __init parse_crashkernel(char *cmdline, unsigned long long system_ram, 18 + unsigned long long *crash_size, unsigned long long *crash_base, 19 + unsigned long long *low_size, bool *high); 20 + 21 + #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 22 + #ifndef DEFAULT_CRASH_KERNEL_LOW_SIZE 23 + #define DEFAULT_CRASH_KERNEL_LOW_SIZE (128UL << 20) 24 + #endif 25 + #ifndef CRASH_ALIGN 26 + #define CRASH_ALIGN SZ_2M 27 + #endif 28 + #ifndef CRASH_ADDR_LOW_MAX 29 + #define CRASH_ADDR_LOW_MAX SZ_4G 30 + #endif 31 + #ifndef CRASH_ADDR_HIGH_MAX 32 + #define CRASH_ADDR_HIGH_MAX memblock_end_of_DRAM() 33 + #endif 34 + 35 + void __init reserve_crashkernel_generic(char *cmdline, 36 + unsigned long long crash_size, 37 + unsigned long long crash_base, 38 + unsigned long long crash_low_size, 39 + bool high); 40 + #else 41 + static inline void __init reserve_crashkernel_generic(char *cmdline, 42 + unsigned long long crash_size, 43 + unsigned long long crash_base, 44 + unsigned long long crash_low_size, 45 + bool high) 46 + {} 47 + #endif 48 + #endif /* LINUX_CRASH_RESERVE_H */
+1
include/linux/kexec.h
··· 16 16 #if !defined(__ASSEMBLY__) 17 17 18 18 #include <linux/crash_core.h> 19 + #include <linux/crash_reserve.h> 19 20 #include <asm/io.h> 20 21 #include <linux/range.h> 21 22
+4 -1
kernel/Kconfig.kexec
··· 2 2 3 3 menu "Kexec and crash features" 4 4 5 + config CRASH_RESERVE 6 + bool 7 + 5 8 config CRASH_CORE 6 9 bool 7 10 8 11 config KEXEC_CORE 9 12 select CRASH_CORE 13 + select CRASH_RESERVE 10 14 bool 11 15 12 16 config KEXEC_ELF ··· 100 96 config CRASH_DUMP 101 97 bool "kernel crash dumps" 102 98 depends on ARCH_SUPPORTS_CRASH_DUMP 103 - select CRASH_CORE 104 99 select KEXEC_CORE 105 100 help 106 101 Generate crash dump after being started by kexec.
+1
kernel/Makefile
··· 69 69 obj-$(CONFIG_KALLSYMS_SELFTEST) += kallsyms_selftest.o 70 70 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o 71 71 obj-$(CONFIG_CRASH_CORE) += crash_core.o 72 + obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o 72 73 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o 73 74 obj-$(CONFIG_KEXEC) += kexec.o 74 75 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
-438
kernel/crash_core.c
··· 34 34 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */ 35 35 static unsigned char *vmcoreinfo_data_safecopy; 36 36 37 - /* Location of the reserved area for the crash kernel */ 38 - struct resource crashk_res = { 39 - .name = "Crash kernel", 40 - .start = 0, 41 - .end = 0, 42 - .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 43 - .desc = IORES_DESC_CRASH_KERNEL 44 - }; 45 - struct resource crashk_low_res = { 46 - .name = "Crash kernel", 47 - .start = 0, 48 - .end = 0, 49 - .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 50 - .desc = IORES_DESC_CRASH_KERNEL 51 - }; 52 - 53 - /* 54 - * parsing the "crashkernel" commandline 55 - * 56 - * this code is intended to be called from architecture specific code 57 - */ 58 - 59 - 60 - /* 61 - * This function parses command lines in the format 62 - * 63 - * crashkernel=ramsize-range:size[,...][@offset] 64 - * 65 - * The function returns 0 on success and -EINVAL on failure. 66 - */ 67 - static int __init parse_crashkernel_mem(char *cmdline, 68 - unsigned long long system_ram, 69 - unsigned long long *crash_size, 70 - unsigned long long *crash_base) 71 - { 72 - char *cur = cmdline, *tmp; 73 - unsigned long long total_mem = system_ram; 74 - 75 - /* 76 - * Firmware sometimes reserves some memory regions for its own use, 77 - * so the system memory size is less than the actual physical memory 78 - * size. Work around this by rounding up the total size to 128M, 79 - * which is enough for most test cases. 80 - */ 81 - total_mem = roundup(total_mem, SZ_128M); 82 - 83 - /* for each entry of the comma-separated list */ 84 - do { 85 - unsigned long long start, end = ULLONG_MAX, size; 86 - 87 - /* get the start of the range */ 88 - start = memparse(cur, &tmp); 89 - if (cur == tmp) { 90 - pr_warn("crashkernel: Memory value expected\n"); 91 - return -EINVAL; 92 - } 93 - cur = tmp; 94 - if (*cur != '-') { 95 - pr_warn("crashkernel: '-' expected\n"); 96 - return -EINVAL; 97 - } 98 - cur++; 99 - 100 - /* if no ':' is here, than we read the end */ 101 - if (*cur != ':') { 102 - end = memparse(cur, &tmp); 103 - if (cur == tmp) { 104 - pr_warn("crashkernel: Memory value expected\n"); 105 - return -EINVAL; 106 - } 107 - cur = tmp; 108 - if (end <= start) { 109 - pr_warn("crashkernel: end <= start\n"); 110 - return -EINVAL; 111 - } 112 - } 113 - 114 - if (*cur != ':') { 115 - pr_warn("crashkernel: ':' expected\n"); 116 - return -EINVAL; 117 - } 118 - cur++; 119 - 120 - size = memparse(cur, &tmp); 121 - if (cur == tmp) { 122 - pr_warn("Memory value expected\n"); 123 - return -EINVAL; 124 - } 125 - cur = tmp; 126 - if (size >= total_mem) { 127 - pr_warn("crashkernel: invalid size\n"); 128 - return -EINVAL; 129 - } 130 - 131 - /* match ? */ 132 - if (total_mem >= start && total_mem < end) { 133 - *crash_size = size; 134 - break; 135 - } 136 - } while (*cur++ == ','); 137 - 138 - if (*crash_size > 0) { 139 - while (*cur && *cur != ' ' && *cur != '@') 140 - cur++; 141 - if (*cur == '@') { 142 - cur++; 143 - *crash_base = memparse(cur, &tmp); 144 - if (cur == tmp) { 145 - pr_warn("Memory value expected after '@'\n"); 146 - return -EINVAL; 147 - } 148 - } 149 - } else 150 - pr_info("crashkernel size resulted in zero bytes\n"); 151 - 152 - return 0; 153 - } 154 - 155 - /* 156 - * That function parses "simple" (old) crashkernel command lines like 157 - * 158 - * crashkernel=size[@offset] 159 - * 160 - * It returns 0 on success and -EINVAL on failure. 161 - */ 162 - static int __init parse_crashkernel_simple(char *cmdline, 163 - unsigned long long *crash_size, 164 - unsigned long long *crash_base) 165 - { 166 - char *cur = cmdline; 167 - 168 - *crash_size = memparse(cmdline, &cur); 169 - if (cmdline == cur) { 170 - pr_warn("crashkernel: memory value expected\n"); 171 - return -EINVAL; 172 - } 173 - 174 - if (*cur == '@') 175 - *crash_base = memparse(cur+1, &cur); 176 - else if (*cur != ' ' && *cur != '\0') { 177 - pr_warn("crashkernel: unrecognized char: %c\n", *cur); 178 - return -EINVAL; 179 - } 180 - 181 - return 0; 182 - } 183 - 184 - #define SUFFIX_HIGH 0 185 - #define SUFFIX_LOW 1 186 - #define SUFFIX_NULL 2 187 - static __initdata char *suffix_tbl[] = { 188 - [SUFFIX_HIGH] = ",high", 189 - [SUFFIX_LOW] = ",low", 190 - [SUFFIX_NULL] = NULL, 191 - }; 192 - 193 - /* 194 - * That function parses "suffix" crashkernel command lines like 195 - * 196 - * crashkernel=size,[high|low] 197 - * 198 - * It returns 0 on success and -EINVAL on failure. 199 - */ 200 - static int __init parse_crashkernel_suffix(char *cmdline, 201 - unsigned long long *crash_size, 202 - const char *suffix) 203 - { 204 - char *cur = cmdline; 205 - 206 - *crash_size = memparse(cmdline, &cur); 207 - if (cmdline == cur) { 208 - pr_warn("crashkernel: memory value expected\n"); 209 - return -EINVAL; 210 - } 211 - 212 - /* check with suffix */ 213 - if (strncmp(cur, suffix, strlen(suffix))) { 214 - pr_warn("crashkernel: unrecognized char: %c\n", *cur); 215 - return -EINVAL; 216 - } 217 - cur += strlen(suffix); 218 - if (*cur != ' ' && *cur != '\0') { 219 - pr_warn("crashkernel: unrecognized char: %c\n", *cur); 220 - return -EINVAL; 221 - } 222 - 223 - return 0; 224 - } 225 - 226 - static __init char *get_last_crashkernel(char *cmdline, 227 - const char *name, 228 - const char *suffix) 229 - { 230 - char *p = cmdline, *ck_cmdline = NULL; 231 - 232 - /* find crashkernel and use the last one if there are more */ 233 - p = strstr(p, name); 234 - while (p) { 235 - char *end_p = strchr(p, ' '); 236 - char *q; 237 - 238 - if (!end_p) 239 - end_p = p + strlen(p); 240 - 241 - if (!suffix) { 242 - int i; 243 - 244 - /* skip the one with any known suffix */ 245 - for (i = 0; suffix_tbl[i]; i++) { 246 - q = end_p - strlen(suffix_tbl[i]); 247 - if (!strncmp(q, suffix_tbl[i], 248 - strlen(suffix_tbl[i]))) 249 - goto next; 250 - } 251 - ck_cmdline = p; 252 - } else { 253 - q = end_p - strlen(suffix); 254 - if (!strncmp(q, suffix, strlen(suffix))) 255 - ck_cmdline = p; 256 - } 257 - next: 258 - p = strstr(p+1, name); 259 - } 260 - 261 - return ck_cmdline; 262 - } 263 - 264 - static int __init __parse_crashkernel(char *cmdline, 265 - unsigned long long system_ram, 266 - unsigned long long *crash_size, 267 - unsigned long long *crash_base, 268 - const char *suffix) 269 - { 270 - char *first_colon, *first_space; 271 - char *ck_cmdline; 272 - char *name = "crashkernel="; 273 - 274 - BUG_ON(!crash_size || !crash_base); 275 - *crash_size = 0; 276 - *crash_base = 0; 277 - 278 - ck_cmdline = get_last_crashkernel(cmdline, name, suffix); 279 - if (!ck_cmdline) 280 - return -ENOENT; 281 - 282 - ck_cmdline += strlen(name); 283 - 284 - if (suffix) 285 - return parse_crashkernel_suffix(ck_cmdline, crash_size, 286 - suffix); 287 - /* 288 - * if the commandline contains a ':', then that's the extended 289 - * syntax -- if not, it must be the classic syntax 290 - */ 291 - first_colon = strchr(ck_cmdline, ':'); 292 - first_space = strchr(ck_cmdline, ' '); 293 - if (first_colon && (!first_space || first_colon < first_space)) 294 - return parse_crashkernel_mem(ck_cmdline, system_ram, 295 - crash_size, crash_base); 296 - 297 - return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); 298 - } 299 - 300 - /* 301 - * That function is the entry point for command line parsing and should be 302 - * called from the arch-specific code. 303 - * 304 - * If crashkernel=,high|low is supported on architecture, non-NULL values 305 - * should be passed to parameters 'low_size' and 'high'. 306 - */ 307 - int __init parse_crashkernel(char *cmdline, 308 - unsigned long long system_ram, 309 - unsigned long long *crash_size, 310 - unsigned long long *crash_base, 311 - unsigned long long *low_size, 312 - bool *high) 313 - { 314 - int ret; 315 - 316 - /* crashkernel=X[@offset] */ 317 - ret = __parse_crashkernel(cmdline, system_ram, crash_size, 318 - crash_base, NULL); 319 - #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 320 - /* 321 - * If non-NULL 'high' passed in and no normal crashkernel 322 - * setting detected, try parsing crashkernel=,high|low. 323 - */ 324 - if (high && ret == -ENOENT) { 325 - ret = __parse_crashkernel(cmdline, 0, crash_size, 326 - crash_base, suffix_tbl[SUFFIX_HIGH]); 327 - if (ret || !*crash_size) 328 - return -EINVAL; 329 - 330 - /* 331 - * crashkernel=Y,low can be specified or not, but invalid value 332 - * is not allowed. 333 - */ 334 - ret = __parse_crashkernel(cmdline, 0, low_size, 335 - crash_base, suffix_tbl[SUFFIX_LOW]); 336 - if (ret == -ENOENT) { 337 - *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 338 - ret = 0; 339 - } else if (ret) { 340 - return ret; 341 - } 342 - 343 - *high = true; 344 - } 345 - #endif 346 - if (!*crash_size) 347 - ret = -EINVAL; 348 - 349 - return ret; 350 - } 351 - 352 - /* 353 - * Add a dummy early_param handler to mark crashkernel= as a known command line 354 - * parameter and suppress incorrect warnings in init/main.c. 355 - */ 356 - static int __init parse_crashkernel_dummy(char *arg) 357 - { 358 - return 0; 359 - } 360 - early_param("crashkernel", parse_crashkernel_dummy); 361 - 362 - #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 363 - static int __init reserve_crashkernel_low(unsigned long long low_size) 364 - { 365 - #ifdef CONFIG_64BIT 366 - unsigned long long low_base; 367 - 368 - low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX); 369 - if (!low_base) { 370 - pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size); 371 - return -ENOMEM; 372 - } 373 - 374 - pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n", 375 - low_base, low_base + low_size, low_size >> 20); 376 - 377 - crashk_low_res.start = low_base; 378 - crashk_low_res.end = low_base + low_size - 1; 379 - #endif 380 - return 0; 381 - } 382 - 383 - void __init reserve_crashkernel_generic(char *cmdline, 384 - unsigned long long crash_size, 385 - unsigned long long crash_base, 386 - unsigned long long crash_low_size, 387 - bool high) 388 - { 389 - unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0; 390 - bool fixed_base = false; 391 - 392 - /* User specifies base address explicitly. */ 393 - if (crash_base) { 394 - fixed_base = true; 395 - search_base = crash_base; 396 - search_end = crash_base + crash_size; 397 - } else if (high) { 398 - search_base = CRASH_ADDR_LOW_MAX; 399 - search_end = CRASH_ADDR_HIGH_MAX; 400 - } 401 - 402 - retry: 403 - crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, 404 - search_base, search_end); 405 - if (!crash_base) { 406 - /* 407 - * For crashkernel=size[KMG]@offset[KMG], print out failure 408 - * message if can't reserve the specified region. 409 - */ 410 - if (fixed_base) { 411 - pr_warn("crashkernel reservation failed - memory is in use.\n"); 412 - return; 413 - } 414 - 415 - /* 416 - * For crashkernel=size[KMG], if the first attempt was for 417 - * low memory, fall back to high memory, the minimum required 418 - * low memory will be reserved later. 419 - */ 420 - if (!high && search_end == CRASH_ADDR_LOW_MAX) { 421 - search_end = CRASH_ADDR_HIGH_MAX; 422 - search_base = CRASH_ADDR_LOW_MAX; 423 - crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 424 - goto retry; 425 - } 426 - 427 - /* 428 - * For crashkernel=size[KMG],high, if the first attempt was 429 - * for high memory, fall back to low memory. 430 - */ 431 - if (high && search_end == CRASH_ADDR_HIGH_MAX) { 432 - search_end = CRASH_ADDR_LOW_MAX; 433 - search_base = 0; 434 - goto retry; 435 - } 436 - pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 437 - crash_size); 438 - return; 439 - } 440 - 441 - if ((crash_base >= CRASH_ADDR_LOW_MAX) && 442 - crash_low_size && reserve_crashkernel_low(crash_low_size)) { 443 - memblock_phys_free(crash_base, crash_size); 444 - return; 445 - } 446 - 447 - pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 448 - crash_base, crash_base + crash_size, crash_size >> 20); 449 - 450 - /* 451 - * The crashkernel memory will be removed from the kernel linear 452 - * map. Inform kmemleak so that it won't try to access it. 453 - */ 454 - kmemleak_ignore_phys(crash_base); 455 - if (crashk_low_res.end) 456 - kmemleak_ignore_phys(crashk_low_res.start); 457 - 458 - crashk_res.start = crash_base; 459 - crashk_res.end = crash_base + crash_size - 1; 460 - } 461 - 462 - static __init int insert_crashkernel_resources(void) 463 - { 464 - if (crashk_res.start < crashk_res.end) 465 - insert_resource(&iomem_resource, &crashk_res); 466 - 467 - if (crashk_low_res.start < crashk_low_res.end) 468 - insert_resource(&iomem_resource, &crashk_low_res); 469 - 470 - return 0; 471 - } 472 - early_initcall(insert_crashkernel_resources); 473 - #endif 474 - 475 37 int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, 476 38 void **addr, unsigned long *sz) 477 39 {
+464
kernel/crash_reserve.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * crash.c - kernel crash support code. 4 + * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com> 5 + */ 6 + 7 + #include <linux/buildid.h> 8 + #include <linux/init.h> 9 + #include <linux/utsname.h> 10 + #include <linux/vmalloc.h> 11 + #include <linux/sizes.h> 12 + #include <linux/kexec.h> 13 + #include <linux/memory.h> 14 + #include <linux/cpuhotplug.h> 15 + #include <linux/memblock.h> 16 + #include <linux/kexec.h> 17 + #include <linux/kmemleak.h> 18 + 19 + #include <asm/page.h> 20 + #include <asm/sections.h> 21 + 22 + #include <crypto/sha1.h> 23 + 24 + #include "kallsyms_internal.h" 25 + #include "kexec_internal.h" 26 + 27 + /* Location of the reserved area for the crash kernel */ 28 + struct resource crashk_res = { 29 + .name = "Crash kernel", 30 + .start = 0, 31 + .end = 0, 32 + .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 33 + .desc = IORES_DESC_CRASH_KERNEL 34 + }; 35 + struct resource crashk_low_res = { 36 + .name = "Crash kernel", 37 + .start = 0, 38 + .end = 0, 39 + .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 40 + .desc = IORES_DESC_CRASH_KERNEL 41 + }; 42 + 43 + /* 44 + * parsing the "crashkernel" commandline 45 + * 46 + * this code is intended to be called from architecture specific code 47 + */ 48 + 49 + 50 + /* 51 + * This function parses command lines in the format 52 + * 53 + * crashkernel=ramsize-range:size[,...][@offset] 54 + * 55 + * The function returns 0 on success and -EINVAL on failure. 56 + */ 57 + static int __init parse_crashkernel_mem(char *cmdline, 58 + unsigned long long system_ram, 59 + unsigned long long *crash_size, 60 + unsigned long long *crash_base) 61 + { 62 + char *cur = cmdline, *tmp; 63 + unsigned long long total_mem = system_ram; 64 + 65 + /* 66 + * Firmware sometimes reserves some memory regions for its own use, 67 + * so the system memory size is less than the actual physical memory 68 + * size. Work around this by rounding up the total size to 128M, 69 + * which is enough for most test cases. 70 + */ 71 + total_mem = roundup(total_mem, SZ_128M); 72 + 73 + /* for each entry of the comma-separated list */ 74 + do { 75 + unsigned long long start, end = ULLONG_MAX, size; 76 + 77 + /* get the start of the range */ 78 + start = memparse(cur, &tmp); 79 + if (cur == tmp) { 80 + pr_warn("crashkernel: Memory value expected\n"); 81 + return -EINVAL; 82 + } 83 + cur = tmp; 84 + if (*cur != '-') { 85 + pr_warn("crashkernel: '-' expected\n"); 86 + return -EINVAL; 87 + } 88 + cur++; 89 + 90 + /* if no ':' is here, than we read the end */ 91 + if (*cur != ':') { 92 + end = memparse(cur, &tmp); 93 + if (cur == tmp) { 94 + pr_warn("crashkernel: Memory value expected\n"); 95 + return -EINVAL; 96 + } 97 + cur = tmp; 98 + if (end <= start) { 99 + pr_warn("crashkernel: end <= start\n"); 100 + return -EINVAL; 101 + } 102 + } 103 + 104 + if (*cur != ':') { 105 + pr_warn("crashkernel: ':' expected\n"); 106 + return -EINVAL; 107 + } 108 + cur++; 109 + 110 + size = memparse(cur, &tmp); 111 + if (cur == tmp) { 112 + pr_warn("Memory value expected\n"); 113 + return -EINVAL; 114 + } 115 + cur = tmp; 116 + if (size >= total_mem) { 117 + pr_warn("crashkernel: invalid size\n"); 118 + return -EINVAL; 119 + } 120 + 121 + /* match ? */ 122 + if (total_mem >= start && total_mem < end) { 123 + *crash_size = size; 124 + break; 125 + } 126 + } while (*cur++ == ','); 127 + 128 + if (*crash_size > 0) { 129 + while (*cur && *cur != ' ' && *cur != '@') 130 + cur++; 131 + if (*cur == '@') { 132 + cur++; 133 + *crash_base = memparse(cur, &tmp); 134 + if (cur == tmp) { 135 + pr_warn("Memory value expected after '@'\n"); 136 + return -EINVAL; 137 + } 138 + } 139 + } else 140 + pr_info("crashkernel size resulted in zero bytes\n"); 141 + 142 + return 0; 143 + } 144 + 145 + /* 146 + * That function parses "simple" (old) crashkernel command lines like 147 + * 148 + * crashkernel=size[@offset] 149 + * 150 + * It returns 0 on success and -EINVAL on failure. 151 + */ 152 + static int __init parse_crashkernel_simple(char *cmdline, 153 + unsigned long long *crash_size, 154 + unsigned long long *crash_base) 155 + { 156 + char *cur = cmdline; 157 + 158 + *crash_size = memparse(cmdline, &cur); 159 + if (cmdline == cur) { 160 + pr_warn("crashkernel: memory value expected\n"); 161 + return -EINVAL; 162 + } 163 + 164 + if (*cur == '@') 165 + *crash_base = memparse(cur+1, &cur); 166 + else if (*cur != ' ' && *cur != '\0') { 167 + pr_warn("crashkernel: unrecognized char: %c\n", *cur); 168 + return -EINVAL; 169 + } 170 + 171 + return 0; 172 + } 173 + 174 + #define SUFFIX_HIGH 0 175 + #define SUFFIX_LOW 1 176 + #define SUFFIX_NULL 2 177 + static __initdata char *suffix_tbl[] = { 178 + [SUFFIX_HIGH] = ",high", 179 + [SUFFIX_LOW] = ",low", 180 + [SUFFIX_NULL] = NULL, 181 + }; 182 + 183 + /* 184 + * That function parses "suffix" crashkernel command lines like 185 + * 186 + * crashkernel=size,[high|low] 187 + * 188 + * It returns 0 on success and -EINVAL on failure. 189 + */ 190 + static int __init parse_crashkernel_suffix(char *cmdline, 191 + unsigned long long *crash_size, 192 + const char *suffix) 193 + { 194 + char *cur = cmdline; 195 + 196 + *crash_size = memparse(cmdline, &cur); 197 + if (cmdline == cur) { 198 + pr_warn("crashkernel: memory value expected\n"); 199 + return -EINVAL; 200 + } 201 + 202 + /* check with suffix */ 203 + if (strncmp(cur, suffix, strlen(suffix))) { 204 + pr_warn("crashkernel: unrecognized char: %c\n", *cur); 205 + return -EINVAL; 206 + } 207 + cur += strlen(suffix); 208 + if (*cur != ' ' && *cur != '\0') { 209 + pr_warn("crashkernel: unrecognized char: %c\n", *cur); 210 + return -EINVAL; 211 + } 212 + 213 + return 0; 214 + } 215 + 216 + static __init char *get_last_crashkernel(char *cmdline, 217 + const char *name, 218 + const char *suffix) 219 + { 220 + char *p = cmdline, *ck_cmdline = NULL; 221 + 222 + /* find crashkernel and use the last one if there are more */ 223 + p = strstr(p, name); 224 + while (p) { 225 + char *end_p = strchr(p, ' '); 226 + char *q; 227 + 228 + if (!end_p) 229 + end_p = p + strlen(p); 230 + 231 + if (!suffix) { 232 + int i; 233 + 234 + /* skip the one with any known suffix */ 235 + for (i = 0; suffix_tbl[i]; i++) { 236 + q = end_p - strlen(suffix_tbl[i]); 237 + if (!strncmp(q, suffix_tbl[i], 238 + strlen(suffix_tbl[i]))) 239 + goto next; 240 + } 241 + ck_cmdline = p; 242 + } else { 243 + q = end_p - strlen(suffix); 244 + if (!strncmp(q, suffix, strlen(suffix))) 245 + ck_cmdline = p; 246 + } 247 + next: 248 + p = strstr(p+1, name); 249 + } 250 + 251 + return ck_cmdline; 252 + } 253 + 254 + static int __init __parse_crashkernel(char *cmdline, 255 + unsigned long long system_ram, 256 + unsigned long long *crash_size, 257 + unsigned long long *crash_base, 258 + const char *suffix) 259 + { 260 + char *first_colon, *first_space; 261 + char *ck_cmdline; 262 + char *name = "crashkernel="; 263 + 264 + BUG_ON(!crash_size || !crash_base); 265 + *crash_size = 0; 266 + *crash_base = 0; 267 + 268 + ck_cmdline = get_last_crashkernel(cmdline, name, suffix); 269 + if (!ck_cmdline) 270 + return -ENOENT; 271 + 272 + ck_cmdline += strlen(name); 273 + 274 + if (suffix) 275 + return parse_crashkernel_suffix(ck_cmdline, crash_size, 276 + suffix); 277 + /* 278 + * if the commandline contains a ':', then that's the extended 279 + * syntax -- if not, it must be the classic syntax 280 + */ 281 + first_colon = strchr(ck_cmdline, ':'); 282 + first_space = strchr(ck_cmdline, ' '); 283 + if (first_colon && (!first_space || first_colon < first_space)) 284 + return parse_crashkernel_mem(ck_cmdline, system_ram, 285 + crash_size, crash_base); 286 + 287 + return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); 288 + } 289 + 290 + /* 291 + * That function is the entry point for command line parsing and should be 292 + * called from the arch-specific code. 293 + * 294 + * If crashkernel=,high|low is supported on architecture, non-NULL values 295 + * should be passed to parameters 'low_size' and 'high'. 296 + */ 297 + int __init parse_crashkernel(char *cmdline, 298 + unsigned long long system_ram, 299 + unsigned long long *crash_size, 300 + unsigned long long *crash_base, 301 + unsigned long long *low_size, 302 + bool *high) 303 + { 304 + int ret; 305 + 306 + /* crashkernel=X[@offset] */ 307 + ret = __parse_crashkernel(cmdline, system_ram, crash_size, 308 + crash_base, NULL); 309 + #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 310 + /* 311 + * If non-NULL 'high' passed in and no normal crashkernel 312 + * setting detected, try parsing crashkernel=,high|low. 313 + */ 314 + if (high && ret == -ENOENT) { 315 + ret = __parse_crashkernel(cmdline, 0, crash_size, 316 + crash_base, suffix_tbl[SUFFIX_HIGH]); 317 + if (ret || !*crash_size) 318 + return -EINVAL; 319 + 320 + /* 321 + * crashkernel=Y,low can be specified or not, but invalid value 322 + * is not allowed. 323 + */ 324 + ret = __parse_crashkernel(cmdline, 0, low_size, 325 + crash_base, suffix_tbl[SUFFIX_LOW]); 326 + if (ret == -ENOENT) { 327 + *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 328 + ret = 0; 329 + } else if (ret) { 330 + return ret; 331 + } 332 + 333 + *high = true; 334 + } 335 + #endif 336 + if (!*crash_size) 337 + ret = -EINVAL; 338 + 339 + return ret; 340 + } 341 + 342 + /* 343 + * Add a dummy early_param handler to mark crashkernel= as a known command line 344 + * parameter and suppress incorrect warnings in init/main.c. 345 + */ 346 + static int __init parse_crashkernel_dummy(char *arg) 347 + { 348 + return 0; 349 + } 350 + early_param("crashkernel", parse_crashkernel_dummy); 351 + 352 + #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 353 + static int __init reserve_crashkernel_low(unsigned long long low_size) 354 + { 355 + #ifdef CONFIG_64BIT 356 + unsigned long long low_base; 357 + 358 + low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX); 359 + if (!low_base) { 360 + pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size); 361 + return -ENOMEM; 362 + } 363 + 364 + pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n", 365 + low_base, low_base + low_size, low_size >> 20); 366 + 367 + crashk_low_res.start = low_base; 368 + crashk_low_res.end = low_base + low_size - 1; 369 + insert_resource(&iomem_resource, &crashk_low_res); 370 + #endif 371 + return 0; 372 + } 373 + 374 + void __init reserve_crashkernel_generic(char *cmdline, 375 + unsigned long long crash_size, 376 + unsigned long long crash_base, 377 + unsigned long long crash_low_size, 378 + bool high) 379 + { 380 + unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0; 381 + bool fixed_base = false; 382 + 383 + /* User specifies base address explicitly. */ 384 + if (crash_base) { 385 + fixed_base = true; 386 + search_base = crash_base; 387 + search_end = crash_base + crash_size; 388 + } else if (high) { 389 + search_base = CRASH_ADDR_LOW_MAX; 390 + search_end = CRASH_ADDR_HIGH_MAX; 391 + } 392 + 393 + retry: 394 + crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, 395 + search_base, search_end); 396 + if (!crash_base) { 397 + /* 398 + * For crashkernel=size[KMG]@offset[KMG], print out failure 399 + * message if can't reserve the specified region. 400 + */ 401 + if (fixed_base) { 402 + pr_warn("crashkernel reservation failed - memory is in use.\n"); 403 + return; 404 + } 405 + 406 + /* 407 + * For crashkernel=size[KMG], if the first attempt was for 408 + * low memory, fall back to high memory, the minimum required 409 + * low memory will be reserved later. 410 + */ 411 + if (!high && search_end == CRASH_ADDR_LOW_MAX) { 412 + search_end = CRASH_ADDR_HIGH_MAX; 413 + search_base = CRASH_ADDR_LOW_MAX; 414 + crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 415 + goto retry; 416 + } 417 + 418 + /* 419 + * For crashkernel=size[KMG],high, if the first attempt was 420 + * for high memory, fall back to low memory. 421 + */ 422 + if (high && search_end == CRASH_ADDR_HIGH_MAX) { 423 + search_end = CRASH_ADDR_LOW_MAX; 424 + search_base = 0; 425 + goto retry; 426 + } 427 + pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 428 + crash_size); 429 + return; 430 + } 431 + 432 + if ((crash_base >= CRASH_ADDR_LOW_MAX) && 433 + crash_low_size && reserve_crashkernel_low(crash_low_size)) { 434 + memblock_phys_free(crash_base, crash_size); 435 + return; 436 + } 437 + 438 + pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 439 + crash_base, crash_base + crash_size, crash_size >> 20); 440 + 441 + /* 442 + * The crashkernel memory will be removed from the kernel linear 443 + * map. Inform kmemleak so that it won't try to access it. 444 + */ 445 + kmemleak_ignore_phys(crash_base); 446 + if (crashk_low_res.end) 447 + kmemleak_ignore_phys(crashk_low_res.start); 448 + 449 + crashk_res.start = crash_base; 450 + crashk_res.end = crash_base + crash_size - 1; 451 + } 452 + 453 + static __init int insert_crashkernel_resources(void) 454 + { 455 + if (crashk_res.start < crashk_res.end) 456 + insert_resource(&iomem_resource, &crashk_res); 457 + 458 + if (crashk_low_res.start < crashk_low_res.end) 459 + insert_resource(&iomem_resource, &crashk_low_res); 460 + 461 + return 0; 462 + } 463 + early_initcall(insert_crashkernel_resources); 464 + #endif