1 // SPDX-License-Identifier: GPL-2.0-only
3 * crash.c - kernel crash support code.
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/kmemleak.h>
19 #include <asm/sections.h>
21 #include <crypto/sha1.h>
23 #include "kallsyms_internal.h"
24 #include "kexec_internal.h"
26 /* Location of the reserved area for the crash kernel */
27 struct resource crashk_res = {
28 .name = "Crash kernel",
31 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
32 .desc = IORES_DESC_CRASH_KERNEL
34 struct resource crashk_low_res = {
35 .name = "Crash kernel",
38 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
39 .desc = IORES_DESC_CRASH_KERNEL
43 * parsing the "crashkernel" commandline
45 * this code is intended to be called from architecture specific code
50 * This function parses command lines in the format
52 * crashkernel=ramsize-range:size[,...][@offset]
54 * The function returns 0 on success and -EINVAL on failure.
56 static int __init parse_crashkernel_mem(char *cmdline,
57 unsigned long long system_ram,
58 unsigned long long *crash_size,
59 unsigned long long *crash_base)
61 char *cur = cmdline, *tmp;
62 unsigned long long total_mem = system_ram;
65 * Firmware sometimes reserves some memory regions for its own use,
66 * so the system memory size is less than the actual physical memory
67 * size. Work around this by rounding up the total size to 128M,
68 * which is enough for most test cases.
70 total_mem = roundup(total_mem, SZ_128M);
72 /* for each entry of the comma-separated list */
74 unsigned long long start, end = ULLONG_MAX, size;
76 /* get the start of the range */
77 start = memparse(cur, &tmp);
79 pr_warn("crashkernel: Memory value expected\n");
84 pr_warn("crashkernel: '-' expected\n");
89 /* if no ':' is here, than we read the end */
91 end = memparse(cur, &tmp);
93 pr_warn("crashkernel: Memory value expected\n");
98 pr_warn("crashkernel: end <= start\n");
104 pr_warn("crashkernel: ':' expected\n");
109 size = memparse(cur, &tmp);
111 pr_warn("crashkernel: Memory value expected\n");
115 if (size >= total_mem) {
116 pr_warn("crashkernel: invalid size\n");
121 if (total_mem >= start && total_mem < end) {
125 } while (*cur++ == ',');
127 if (*crash_size > 0) {
128 while (*cur && *cur != ' ' && *cur != '@')
132 *crash_base = memparse(cur, &tmp);
134 pr_warn("crahskernel: Memory value expected after '@'\n");
139 pr_info("crashkernel size resulted in zero bytes\n");
145 * That function parses "simple" (old) crashkernel command lines like
147 * crashkernel=size[@offset]
149 * It returns 0 on success and -EINVAL on failure.
151 static int __init parse_crashkernel_simple(char *cmdline,
152 unsigned long long *crash_size,
153 unsigned long long *crash_base)
157 *crash_size = memparse(cmdline, &cur);
158 if (cmdline == cur) {
159 pr_warn("crashkernel: memory value expected\n");
164 *crash_base = memparse(cur+1, &cur);
165 else if (*cur != ' ' && *cur != '\0') {
166 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
173 #define SUFFIX_HIGH 0
175 #define SUFFIX_NULL 2
176 static __initdata char *suffix_tbl[] = {
177 [SUFFIX_HIGH] = ",high",
178 [SUFFIX_LOW] = ",low",
179 [SUFFIX_NULL] = NULL,
183 * That function parses "suffix" crashkernel command lines like
185 * crashkernel=size,[high|low]
187 * It returns 0 on success and -EINVAL on failure.
189 static int __init parse_crashkernel_suffix(char *cmdline,
190 unsigned long long *crash_size,
195 *crash_size = memparse(cmdline, &cur);
196 if (cmdline == cur) {
197 pr_warn("crashkernel: memory value expected\n");
201 /* check with suffix */
202 if (strncmp(cur, suffix, strlen(suffix))) {
203 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
206 cur += strlen(suffix);
207 if (*cur != ' ' && *cur != '\0') {
208 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
215 static __init char *get_last_crashkernel(char *cmdline,
219 char *p = cmdline, *ck_cmdline = NULL;
221 /* find crashkernel and use the last one if there are more */
224 char *end_p = strchr(p, ' ');
228 end_p = p + strlen(p);
233 /* skip the one with any known suffix */
234 for (i = 0; suffix_tbl[i]; i++) {
235 q = end_p - strlen(suffix_tbl[i]);
236 if (!strncmp(q, suffix_tbl[i],
237 strlen(suffix_tbl[i])))
242 q = end_p - strlen(suffix);
243 if (!strncmp(q, suffix, strlen(suffix)))
247 p = strstr(p+1, name);
253 static int __init __parse_crashkernel(char *cmdline,
254 unsigned long long system_ram,
255 unsigned long long *crash_size,
256 unsigned long long *crash_base,
259 char *first_colon, *first_space;
261 char *name = "crashkernel=";
263 BUG_ON(!crash_size || !crash_base);
267 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
271 ck_cmdline += strlen(name);
274 return parse_crashkernel_suffix(ck_cmdline, crash_size,
277 * if the commandline contains a ':', then that's the extended
278 * syntax -- if not, it must be the classic syntax
280 first_colon = strchr(ck_cmdline, ':');
281 first_space = strchr(ck_cmdline, ' ');
282 if (first_colon && (!first_space || first_colon < first_space))
283 return parse_crashkernel_mem(ck_cmdline, system_ram,
284 crash_size, crash_base);
286 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
290 * That function is the entry point for command line parsing and should be
291 * called from the arch-specific code.
293 * If crashkernel=,high|low is supported on architecture, non-NULL values
294 * should be passed to parameters 'low_size' and 'high'.
296 int __init parse_crashkernel(char *cmdline,
297 unsigned long long system_ram,
298 unsigned long long *crash_size,
299 unsigned long long *crash_base,
300 unsigned long long *low_size,
305 /* crashkernel=X[@offset] */
306 ret = __parse_crashkernel(cmdline, system_ram, crash_size,
308 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
310 * If non-NULL 'high' passed in and no normal crashkernel
311 * setting detected, try parsing crashkernel=,high|low.
313 if (high && ret == -ENOENT) {
314 ret = __parse_crashkernel(cmdline, 0, crash_size,
315 crash_base, suffix_tbl[SUFFIX_HIGH]);
316 if (ret || !*crash_size)
320 * crashkernel=Y,low can be specified or not, but invalid value
323 ret = __parse_crashkernel(cmdline, 0, low_size,
324 crash_base, suffix_tbl[SUFFIX_LOW]);
325 if (ret == -ENOENT) {
326 *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
338 if (*crash_size >= system_ram)
345 * Add a dummy early_param handler to mark crashkernel= as a known command line
346 * parameter and suppress incorrect warnings in init/main.c.
348 static int __init parse_crashkernel_dummy(char *arg)
352 early_param("crashkernel", parse_crashkernel_dummy);
354 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
355 static int __init reserve_crashkernel_low(unsigned long long low_size)
358 unsigned long long low_base;
360 low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
362 pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
366 pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
367 low_base, low_base + low_size, low_size >> 20);
369 crashk_low_res.start = low_base;
370 crashk_low_res.end = low_base + low_size - 1;
371 #ifdef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
372 insert_resource(&iomem_resource, &crashk_low_res);
378 void __init reserve_crashkernel_generic(char *cmdline,
379 unsigned long long crash_size,
380 unsigned long long crash_base,
381 unsigned long long crash_low_size,
384 unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
385 bool fixed_base = false;
387 /* User specifies base address explicitly. */
390 search_base = crash_base;
391 search_end = crash_base + crash_size;
393 search_base = CRASH_ADDR_LOW_MAX;
394 search_end = CRASH_ADDR_HIGH_MAX;
398 crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
399 search_base, search_end);
402 * For crashkernel=size[KMG]@offset[KMG], print out failure
403 * message if can't reserve the specified region.
406 pr_warn("crashkernel reservation failed - memory is in use.\n");
411 * For crashkernel=size[KMG], if the first attempt was for
412 * low memory, fall back to high memory, the minimum required
413 * low memory will be reserved later.
415 if (!high && search_end == CRASH_ADDR_LOW_MAX) {
416 search_end = CRASH_ADDR_HIGH_MAX;
417 search_base = CRASH_ADDR_LOW_MAX;
418 crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
423 * For crashkernel=size[KMG],high, if the first attempt was
424 * for high memory, fall back to low memory.
426 if (high && search_end == CRASH_ADDR_HIGH_MAX) {
427 search_end = CRASH_ADDR_LOW_MAX;
429 if (search_end != CRASH_ADDR_HIGH_MAX)
432 pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
437 if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
438 crash_low_size && reserve_crashkernel_low(crash_low_size)) {
439 memblock_phys_free(crash_base, crash_size);
443 pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
444 crash_base, crash_base + crash_size, crash_size >> 20);
447 * The crashkernel memory will be removed from the kernel linear
448 * map. Inform kmemleak so that it won't try to access it.
450 kmemleak_ignore_phys(crash_base);
451 if (crashk_low_res.end)
452 kmemleak_ignore_phys(crashk_low_res.start);
454 crashk_res.start = crash_base;
455 crashk_res.end = crash_base + crash_size - 1;
456 #ifdef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
457 insert_resource(&iomem_resource, &crashk_res);
461 #ifndef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
462 static __init int insert_crashkernel_resources(void)
464 if (crashk_res.start < crashk_res.end)
465 insert_resource(&iomem_resource, &crashk_res);
467 if (crashk_low_res.start < crashk_low_res.end)
468 insert_resource(&iomem_resource, &crashk_low_res);
472 early_initcall(insert_crashkernel_resources);