xref: /linux/kernel/crash_reserve.c (revision 231bb8c1be040044f14ed74b30e4e1a26db550f8)
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/kmemleak.h>
17 #include <linux/cma.h>
18 #include <linux/crash_reserve.h>
19 
20 #include <asm/page.h>
21 #include <asm/sections.h>
22 
23 #include "kallsyms_internal.h"
24 #include "kexec_internal.h"
25 
26 /* Location of the reserved area for the crash kernel */
27 struct resource crashk_res = {
28 	.name  = "Crash kernel",
29 	.start = 0,
30 	.end   = 0,
31 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
32 	.desc  = IORES_DESC_CRASH_KERNEL
33 };
34 struct resource crashk_low_res = {
35 	.name  = "Crash kernel",
36 	.start = 0,
37 	.end   = 0,
38 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
39 	.desc  = IORES_DESC_CRASH_KERNEL
40 };
41 
42 /*
43  * parsing the "crashkernel" commandline
44  *
45  * this code is intended to be called from architecture specific code
46  */
47 
48 
49 /*
50  * This function parses command lines in the format
51  *
52  *   crashkernel=ramsize-range:size[,...][@offset]
53  *
54  * The function returns 0 on success and -EINVAL on failure.
55  */
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)
60 {
61 	char *cur = cmdline, *tmp;
62 	unsigned long long total_mem = system_ram;
63 
64 	/*
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.
69 	 */
70 	total_mem = roundup(total_mem, SZ_128M);
71 
72 	/* for each entry of the comma-separated list */
73 	do {
74 		unsigned long long start, end = ULLONG_MAX, size;
75 
76 		/* get the start of the range */
77 		start = memparse(cur, &tmp);
78 		if (cur == tmp) {
79 			pr_warn("crashkernel: Memory value expected\n");
80 			return -EINVAL;
81 		}
82 		cur = tmp;
83 		if (*cur != '-') {
84 			pr_warn("crashkernel: '-' expected\n");
85 			return -EINVAL;
86 		}
87 		cur++;
88 
89 		/* if no ':' is here, than we read the end */
90 		if (*cur != ':') {
91 			end = memparse(cur, &tmp);
92 			if (cur == tmp) {
93 				pr_warn("crashkernel: Memory value expected\n");
94 				return -EINVAL;
95 			}
96 			cur = tmp;
97 			if (end <= start) {
98 				pr_warn("crashkernel: end <= start\n");
99 				return -EINVAL;
100 			}
101 		}
102 
103 		if (*cur != ':') {
104 			pr_warn("crashkernel: ':' expected\n");
105 			return -EINVAL;
106 		}
107 		cur++;
108 
109 		size = memparse(cur, &tmp);
110 		if (cur == tmp) {
111 			pr_warn("crashkernel: Memory value expected\n");
112 			return -EINVAL;
113 		}
114 		cur = tmp;
115 		if (size >= total_mem) {
116 			pr_warn("crashkernel: invalid size\n");
117 			return -EINVAL;
118 		}
119 
120 		/* match ? */
121 		if (total_mem >= start && total_mem < end) {
122 			*crash_size = size;
123 			break;
124 		}
125 	} while (*cur++ == ',');
126 
127 	if (*crash_size > 0) {
128 		while (*cur && *cur != ' ' && *cur != '@')
129 			cur++;
130 		if (*cur == '@') {
131 			cur++;
132 			*crash_base = memparse(cur, &tmp);
133 			if (cur == tmp) {
134 				pr_warn("crashkernel: Memory value expected after '@'\n");
135 				return -EINVAL;
136 			}
137 		}
138 	} else
139 		pr_info("crashkernel size resulted in zero bytes\n");
140 
141 	return 0;
142 }
143 
144 /*
145  * That function parses "simple" (old) crashkernel command lines like
146  *
147  *	crashkernel=size[@offset]
148  *
149  * It returns 0 on success and -EINVAL on failure.
150  */
151 static int __init parse_crashkernel_simple(char *cmdline,
152 					   unsigned long long *crash_size,
153 					   unsigned long long *crash_base)
154 {
155 	char *cur = cmdline;
156 
157 	*crash_size = memparse(cmdline, &cur);
158 	if (cmdline == cur) {
159 		pr_warn("crashkernel: memory value expected\n");
160 		return -EINVAL;
161 	}
162 
163 	if (*cur == '@')
164 		*crash_base = memparse(cur+1, &cur);
165 	else if (*cur != ' ' && *cur != '\0') {
166 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
167 		return -EINVAL;
168 	}
169 
170 	return 0;
171 }
172 
173 #define SUFFIX_HIGH 0
174 #define SUFFIX_LOW  1
175 #define SUFFIX_CMA  2
176 #define SUFFIX_NULL 3
177 static __initdata char *suffix_tbl[] = {
178 	[SUFFIX_HIGH] = ",high",
179 	[SUFFIX_LOW]  = ",low",
180 	[SUFFIX_CMA]  = ",cma",
181 	[SUFFIX_NULL] = NULL,
182 };
183 
184 /*
185  * That function parses "suffix"  crashkernel command lines like
186  *
187  *	crashkernel=size,[high|low|cma]
188  *
189  * It returns 0 on success and -EINVAL on failure.
190  */
191 static int __init parse_crashkernel_suffix(char *cmdline,
192 					   unsigned long long *crash_size,
193 					   const char *suffix)
194 {
195 	char *cur = cmdline;
196 
197 	*crash_size = memparse(cmdline, &cur);
198 	if (cmdline == cur) {
199 		pr_warn("crashkernel: memory value expected\n");
200 		return -EINVAL;
201 	}
202 
203 	/* check with suffix */
204 	if (strncmp(cur, suffix, strlen(suffix))) {
205 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
206 		return -EINVAL;
207 	}
208 	cur += strlen(suffix);
209 	if (*cur != ' ' && *cur != '\0') {
210 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
211 		return -EINVAL;
212 	}
213 
214 	return 0;
215 }
216 
217 static __init char *get_last_crashkernel(char *cmdline,
218 			     const char *name,
219 			     const char *suffix)
220 {
221 	char *p = cmdline, *ck_cmdline = NULL;
222 
223 	/* find crashkernel and use the last one if there are more */
224 	p = strstr(p, name);
225 	while (p) {
226 		char *end_p = strchr(p, ' ');
227 		char *q;
228 
229 		if (!end_p)
230 			end_p = p + strlen(p);
231 
232 		if (!suffix) {
233 			int i;
234 
235 			/* skip the one with any known suffix */
236 			for (i = 0; suffix_tbl[i]; i++) {
237 				q = end_p - strlen(suffix_tbl[i]);
238 				if (!strncmp(q, suffix_tbl[i],
239 					     strlen(suffix_tbl[i])))
240 					goto next;
241 			}
242 			ck_cmdline = p;
243 		} else {
244 			q = end_p - strlen(suffix);
245 			if (!strncmp(q, suffix, strlen(suffix)))
246 				ck_cmdline = p;
247 		}
248 next:
249 		p = strstr(p+1, name);
250 	}
251 
252 	return ck_cmdline;
253 }
254 
255 static int __init __parse_crashkernel(char *cmdline,
256 			     unsigned long long system_ram,
257 			     unsigned long long *crash_size,
258 			     unsigned long long *crash_base,
259 			     const char *suffix)
260 {
261 	char *first_colon, *first_space;
262 	char *ck_cmdline;
263 	char *name = "crashkernel=";
264 
265 	BUG_ON(!crash_size || !crash_base);
266 	*crash_size = 0;
267 	*crash_base = 0;
268 
269 	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
270 	if (!ck_cmdline)
271 		return -ENOENT;
272 
273 	ck_cmdline += strlen(name);
274 
275 	if (suffix)
276 		return parse_crashkernel_suffix(ck_cmdline, crash_size,
277 				suffix);
278 	/*
279 	 * if the commandline contains a ':', then that's the extended
280 	 * syntax -- if not, it must be the classic syntax
281 	 */
282 	first_colon = strchr(ck_cmdline, ':');
283 	first_space = strchr(ck_cmdline, ' ');
284 	if (first_colon && (!first_space || first_colon < first_space))
285 		return parse_crashkernel_mem(ck_cmdline, system_ram,
286 				crash_size, crash_base);
287 
288 	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
289 }
290 
291 /*
292  * That function is the entry point for command line parsing and should be
293  * called from the arch-specific code.
294  *
295  * If crashkernel=,high|low is supported on architecture, non-NULL values
296  * should be passed to parameters 'low_size' and 'high'.
297  */
298 int __init parse_crashkernel(char *cmdline,
299 			     unsigned long long system_ram,
300 			     unsigned long long *crash_size,
301 			     unsigned long long *crash_base,
302 			     unsigned long long *low_size,
303 			     unsigned long long *cma_size,
304 			     bool *high)
305 {
306 	int ret;
307 	unsigned long long __always_unused cma_base;
308 
309 	/* crashkernel=X[@offset] */
310 	ret = __parse_crashkernel(cmdline, system_ram, crash_size,
311 				crash_base, NULL);
312 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
313 	/*
314 	 * If non-NULL 'high' passed in and no normal crashkernel
315 	 * setting detected, try parsing crashkernel=,high|low.
316 	 */
317 	if (high && ret == -ENOENT) {
318 		ret = __parse_crashkernel(cmdline, 0, crash_size,
319 				crash_base, suffix_tbl[SUFFIX_HIGH]);
320 		if (ret || !*crash_size)
321 			return -EINVAL;
322 
323 		/*
324 		 * crashkernel=Y,low can be specified or not, but invalid value
325 		 * is not allowed.
326 		 */
327 		ret = __parse_crashkernel(cmdline, 0, low_size,
328 				crash_base, suffix_tbl[SUFFIX_LOW]);
329 		if (ret == -ENOENT) {
330 			*low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
331 			ret = 0;
332 		} else if (ret) {
333 			return ret;
334 		}
335 
336 		*high = true;
337 	}
338 
339 	/*
340 	 * optional CMA reservation
341 	 * cma_base is ignored
342 	 */
343 	if (cma_size)
344 		__parse_crashkernel(cmdline, 0, cma_size,
345 			&cma_base, suffix_tbl[SUFFIX_CMA]);
346 #endif
347 	if (!*crash_size)
348 		ret = -EINVAL;
349 
350 	if (*crash_size >= system_ram)
351 		ret = -EINVAL;
352 
353 	return ret;
354 }
355 
356 /*
357  * Add a dummy early_param handler to mark crashkernel= as a known command line
358  * parameter and suppress incorrect warnings in init/main.c.
359  */
360 static int __init parse_crashkernel_dummy(char *arg)
361 {
362 	return 0;
363 }
364 early_param("crashkernel", parse_crashkernel_dummy);
365 
366 #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
367 static int __init reserve_crashkernel_low(unsigned long long low_size)
368 {
369 #ifdef CONFIG_64BIT
370 	unsigned long long low_base;
371 
372 	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
373 	if (!low_base) {
374 		pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
375 		return -ENOMEM;
376 	}
377 
378 	pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
379 		low_base, low_base + low_size, low_size >> 20);
380 
381 	crashk_low_res.start = low_base;
382 	crashk_low_res.end   = low_base + low_size - 1;
383 #ifdef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
384 	insert_resource(&iomem_resource, &crashk_low_res);
385 #endif
386 #endif
387 	return 0;
388 }
389 
390 void __init reserve_crashkernel_generic(unsigned long long crash_size,
391 					unsigned long long crash_base,
392 					unsigned long long crash_low_size,
393 					bool high)
394 {
395 	unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
396 	bool fixed_base = false;
397 
398 	/* User specifies base address explicitly. */
399 	if (crash_base) {
400 		fixed_base = true;
401 		search_base = crash_base;
402 		search_end = crash_base + crash_size;
403 	} else if (high) {
404 		search_base = CRASH_ADDR_LOW_MAX;
405 		search_end = CRASH_ADDR_HIGH_MAX;
406 	}
407 
408 retry:
409 	crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
410 					       search_base, search_end);
411 	if (!crash_base) {
412 		/*
413 		 * For crashkernel=size[KMG]@offset[KMG], print out failure
414 		 * message if can't reserve the specified region.
415 		 */
416 		if (fixed_base) {
417 			pr_warn("crashkernel reservation failed - memory is in use.\n");
418 			return;
419 		}
420 
421 		/*
422 		 * For crashkernel=size[KMG], if the first attempt was for
423 		 * low memory, fall back to high memory, the minimum required
424 		 * low memory will be reserved later.
425 		 */
426 		if (!high && search_end == CRASH_ADDR_LOW_MAX) {
427 			search_end = CRASH_ADDR_HIGH_MAX;
428 			search_base = CRASH_ADDR_LOW_MAX;
429 			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
430 			goto retry;
431 		}
432 
433 		/*
434 		 * For crashkernel=size[KMG],high, if the first attempt was
435 		 * for high memory, fall back to low memory.
436 		 */
437 		if (high && search_end == CRASH_ADDR_HIGH_MAX) {
438 			search_end = CRASH_ADDR_LOW_MAX;
439 			search_base = 0;
440 			if (search_end != CRASH_ADDR_HIGH_MAX)
441 				goto retry;
442 		}
443 		pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
444 			crash_size);
445 		return;
446 	}
447 
448 	if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
449 	     crash_low_size && reserve_crashkernel_low(crash_low_size)) {
450 		memblock_phys_free(crash_base, crash_size);
451 		return;
452 	}
453 
454 	pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
455 		crash_base, crash_base + crash_size, crash_size >> 20);
456 
457 	/*
458 	 * The crashkernel memory will be removed from the kernel linear
459 	 * map. Inform kmemleak so that it won't try to access it.
460 	 */
461 	kmemleak_ignore_phys(crash_base);
462 	if (crashk_low_res.end)
463 		kmemleak_ignore_phys(crashk_low_res.start);
464 
465 	crashk_res.start = crash_base;
466 	crashk_res.end = crash_base + crash_size - 1;
467 #ifdef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
468 	insert_resource(&iomem_resource, &crashk_res);
469 #endif
470 }
471 
472 struct range crashk_cma_ranges[CRASHKERNEL_CMA_RANGES_MAX];
473 #ifdef CRASHKERNEL_CMA
474 int crashk_cma_cnt;
475 void __init reserve_crashkernel_cma(unsigned long long cma_size)
476 {
477 	unsigned long long request_size = roundup(cma_size, PAGE_SIZE);
478 	unsigned long long reserved_size = 0;
479 
480 	if (!cma_size)
481 		return;
482 
483 	while (cma_size > reserved_size &&
484 	       crashk_cma_cnt < CRASHKERNEL_CMA_RANGES_MAX) {
485 
486 		struct cma *res;
487 
488 		if (cma_declare_contiguous(0, request_size, 0, 0, 0, false,
489 				       "crashkernel", &res)) {
490 			/* reservation failed, try half-sized blocks */
491 			if (request_size <= PAGE_SIZE)
492 				break;
493 
494 			request_size = roundup(request_size / 2, PAGE_SIZE);
495 			continue;
496 		}
497 
498 		crashk_cma_ranges[crashk_cma_cnt].start = cma_get_base(res);
499 		crashk_cma_ranges[crashk_cma_cnt].end =
500 			crashk_cma_ranges[crashk_cma_cnt].start +
501 			cma_get_size(res) - 1;
502 		++crashk_cma_cnt;
503 		reserved_size += request_size;
504 	}
505 
506 	if (cma_size > reserved_size)
507 		pr_warn("crashkernel CMA reservation failed: %lld MB requested, %lld MB reserved in %d ranges\n",
508 			cma_size >> 20, reserved_size >> 20, crashk_cma_cnt);
509 	else
510 		pr_info("crashkernel CMA reserved: %lld MB in %d ranges\n",
511 			reserved_size >> 20, crashk_cma_cnt);
512 }
513 
514 #else /* CRASHKERNEL_CMA */
515 void __init reserve_crashkernel_cma(unsigned long long cma_size)
516 {
517 	if (cma_size)
518 		pr_warn("crashkernel CMA reservation not supported\n");
519 }
520 #endif
521 
522 #ifndef HAVE_ARCH_ADD_CRASH_RES_TO_IOMEM_EARLY
523 static __init int insert_crashkernel_resources(void)
524 {
525 	if (!arch_add_crash_res_to_iomem())
526 		return 0;
527 
528 	if (crashk_res.start < crashk_res.end)
529 		insert_resource(&iomem_resource, &crashk_res);
530 
531 	if (crashk_low_res.start < crashk_low_res.end)
532 		insert_resource(&iomem_resource, &crashk_low_res);
533 
534 	return 0;
535 }
536 early_initcall(insert_crashkernel_resources);
537 #endif
538 #endif
539