Linux Audio

Check our new training course

Loading...
v6.2
  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/crash_core.h>
  9#include <linux/init.h>
 10#include <linux/utsname.h>
 11#include <linux/vmalloc.h>
 12#include <linux/sizes.h>
 
 
 
 
 
 
 
 
 
 
 13
 14#include <asm/page.h>
 15#include <asm/sections.h>
 16
 17#include <crypto/sha1.h>
 18
 19#include "kallsyms_internal.h"
 
 20
 21/* vmcoreinfo stuff */
 22unsigned char *vmcoreinfo_data;
 23size_t vmcoreinfo_size;
 24u32 *vmcoreinfo_note;
 25
 26/* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
 27static unsigned char *vmcoreinfo_data_safecopy;
 28
 29/*
 30 * parsing the "crashkernel" commandline
 31 *
 32 * this code is intended to be called from architecture specific code
 33 */
 34
 35
 36/*
 37 * This function parses command lines in the format
 38 *
 39 *   crashkernel=ramsize-range:size[,...][@offset]
 40 *
 41 * The function returns 0 on success and -EINVAL on failure.
 42 */
 43static int __init parse_crashkernel_mem(char *cmdline,
 44					unsigned long long system_ram,
 45					unsigned long long *crash_size,
 46					unsigned long long *crash_base)
 47{
 48	char *cur = cmdline, *tmp;
 49	unsigned long long total_mem = system_ram;
 50
 51	/*
 52	 * Firmware sometimes reserves some memory regions for its own use,
 53	 * so the system memory size is less than the actual physical memory
 54	 * size. Work around this by rounding up the total size to 128M,
 55	 * which is enough for most test cases.
 56	 */
 57	total_mem = roundup(total_mem, SZ_128M);
 58
 59	/* for each entry of the comma-separated list */
 60	do {
 61		unsigned long long start, end = ULLONG_MAX, size;
 62
 63		/* get the start of the range */
 64		start = memparse(cur, &tmp);
 65		if (cur == tmp) {
 66			pr_warn("crashkernel: Memory value expected\n");
 67			return -EINVAL;
 68		}
 69		cur = tmp;
 70		if (*cur != '-') {
 71			pr_warn("crashkernel: '-' expected\n");
 72			return -EINVAL;
 73		}
 74		cur++;
 75
 76		/* if no ':' is here, than we read the end */
 77		if (*cur != ':') {
 78			end = memparse(cur, &tmp);
 79			if (cur == tmp) {
 80				pr_warn("crashkernel: Memory value expected\n");
 81				return -EINVAL;
 82			}
 83			cur = tmp;
 84			if (end <= start) {
 85				pr_warn("crashkernel: end <= start\n");
 86				return -EINVAL;
 87			}
 88		}
 89
 90		if (*cur != ':') {
 91			pr_warn("crashkernel: ':' expected\n");
 92			return -EINVAL;
 93		}
 94		cur++;
 95
 96		size = memparse(cur, &tmp);
 97		if (cur == tmp) {
 98			pr_warn("Memory value expected\n");
 99			return -EINVAL;
100		}
101		cur = tmp;
102		if (size >= total_mem) {
103			pr_warn("crashkernel: invalid size\n");
104			return -EINVAL;
105		}
106
107		/* match ? */
108		if (total_mem >= start && total_mem < end) {
109			*crash_size = size;
110			break;
111		}
112	} while (*cur++ == ',');
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114	if (*crash_size > 0) {
115		while (*cur && *cur != ' ' && *cur != '@')
116			cur++;
117		if (*cur == '@') {
118			cur++;
119			*crash_base = memparse(cur, &tmp);
120			if (cur == tmp) {
121				pr_warn("Memory value expected after '@'\n");
122				return -EINVAL;
123			}
124		}
125	} else
126		pr_info("crashkernel size resulted in zero bytes\n");
127
128	return 0;
129}
130
131/*
132 * That function parses "simple" (old) crashkernel command lines like
133 *
134 *	crashkernel=size[@offset]
135 *
136 * It returns 0 on success and -EINVAL on failure.
137 */
138static int __init parse_crashkernel_simple(char *cmdline,
139					   unsigned long long *crash_size,
140					   unsigned long long *crash_base)
141{
142	char *cur = cmdline;
143
144	*crash_size = memparse(cmdline, &cur);
145	if (cmdline == cur) {
146		pr_warn("crashkernel: memory value expected\n");
147		return -EINVAL;
148	}
149
150	if (*cur == '@')
151		*crash_base = memparse(cur+1, &cur);
152	else if (*cur != ' ' && *cur != '\0') {
153		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
154		return -EINVAL;
155	}
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157	return 0;
158}
159
160#define SUFFIX_HIGH 0
161#define SUFFIX_LOW  1
162#define SUFFIX_NULL 2
163static __initdata char *suffix_tbl[] = {
164	[SUFFIX_HIGH] = ",high",
165	[SUFFIX_LOW]  = ",low",
166	[SUFFIX_NULL] = NULL,
167};
168
169/*
170 * That function parses "suffix"  crashkernel command lines like
171 *
172 *	crashkernel=size,[high|low]
173 *
174 * It returns 0 on success and -EINVAL on failure.
175 */
176static int __init parse_crashkernel_suffix(char *cmdline,
177					   unsigned long long	*crash_size,
178					   const char *suffix)
179{
180	char *cur = cmdline;
181
182	*crash_size = memparse(cmdline, &cur);
183	if (cmdline == cur) {
184		pr_warn("crashkernel: memory value expected\n");
185		return -EINVAL;
186	}
187
188	/* check with suffix */
189	if (strncmp(cur, suffix, strlen(suffix))) {
190		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
191		return -EINVAL;
192	}
193	cur += strlen(suffix);
194	if (*cur != ' ' && *cur != '\0') {
195		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
196		return -EINVAL;
197	}
198
199	return 0;
200}
 
201
202static __init char *get_last_crashkernel(char *cmdline,
203			     const char *name,
204			     const char *suffix)
205{
206	char *p = cmdline, *ck_cmdline = NULL;
207
208	/* find crashkernel and use the last one if there are more */
209	p = strstr(p, name);
210	while (p) {
211		char *end_p = strchr(p, ' ');
212		char *q;
213
214		if (!end_p)
215			end_p = p + strlen(p);
216
217		if (!suffix) {
218			int i;
219
220			/* skip the one with any known suffix */
221			for (i = 0; suffix_tbl[i]; i++) {
222				q = end_p - strlen(suffix_tbl[i]);
223				if (!strncmp(q, suffix_tbl[i],
224					     strlen(suffix_tbl[i])))
225					goto next;
226			}
227			ck_cmdline = p;
228		} else {
229			q = end_p - strlen(suffix);
230			if (!strncmp(q, suffix, strlen(suffix)))
231				ck_cmdline = p;
232		}
233next:
234		p = strstr(p+1, name);
235	}
236
237	return ck_cmdline;
 
 
 
 
 
 
 
 
 
238}
239
240static int __init __parse_crashkernel(char *cmdline,
241			     unsigned long long system_ram,
242			     unsigned long long *crash_size,
243			     unsigned long long *crash_base,
244			     const char *name,
245			     const char *suffix)
246{
247	char	*first_colon, *first_space;
248	char	*ck_cmdline;
 
249
250	BUG_ON(!crash_size || !crash_base);
251	*crash_size = 0;
252	*crash_base = 0;
253
254	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
255	if (!ck_cmdline)
256		return -ENOENT;
257
258	ck_cmdline += strlen(name);
 
 
 
 
 
 
 
 
 
 
 
 
 
259
260	if (suffix)
261		return parse_crashkernel_suffix(ck_cmdline, crash_size,
262				suffix);
263	/*
264	 * if the commandline contains a ':', then that's the extended
265	 * syntax -- if not, it must be the classic syntax
 
 
 
266	 */
267	first_colon = strchr(ck_cmdline, ':');
268	first_space = strchr(ck_cmdline, ' ');
269	if (first_colon && (!first_space || first_colon < first_space))
270		return parse_crashkernel_mem(ck_cmdline, system_ram,
271				crash_size, crash_base);
272
273	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
274}
 
275
276/*
277 * That function is the entry point for command line parsing and should be
278 * called from the arch-specific code.
279 */
280int __init parse_crashkernel(char *cmdline,
281			     unsigned long long system_ram,
282			     unsigned long long *crash_size,
283			     unsigned long long *crash_base)
284{
285	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
286					"crashkernel=", NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287}
288
289int __init parse_crashkernel_high(char *cmdline,
290			     unsigned long long system_ram,
291			     unsigned long long *crash_size,
292			     unsigned long long *crash_base)
293{
294	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
295				"crashkernel=", suffix_tbl[SUFFIX_HIGH]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296}
297
298int __init parse_crashkernel_low(char *cmdline,
299			     unsigned long long system_ram,
300			     unsigned long long *crash_size,
301			     unsigned long long *crash_base)
302{
303	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
304				"crashkernel=", suffix_tbl[SUFFIX_LOW]);
 
 
 
 
 
 
 
 
305}
306
307/*
308 * Add a dummy early_param handler to mark crashkernel= as a known command line
309 * parameter and suppress incorrect warnings in init/main.c.
310 */
311static int __init parse_crashkernel_dummy(char *arg)
312{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313	return 0;
314}
315early_param("crashkernel", parse_crashkernel_dummy);
316
317Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
318			  void *data, size_t data_len)
319{
320	struct elf_note *note = (struct elf_note *)buf;
 
321
322	note->n_namesz = strlen(name) + 1;
323	note->n_descsz = data_len;
324	note->n_type   = type;
325	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
326	memcpy(buf, name, note->n_namesz);
327	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
328	memcpy(buf, data, data_len);
329	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
330
331	return buf;
332}
 
 
333
334void final_note(Elf_Word *buf)
335{
336	memset(buf, 0, sizeof(struct elf_note));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337}
338
339static void update_vmcoreinfo_note(void)
340{
341	u32 *buf = vmcoreinfo_note;
 
342
343	if (!vmcoreinfo_size)
344		return;
345	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
346			      vmcoreinfo_size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347	final_note(buf);
348}
349
350void crash_update_vmcoreinfo_safecopy(void *ptr)
351{
352	if (ptr)
353		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
354
355	vmcoreinfo_data_safecopy = ptr;
356}
357
358void crash_save_vmcoreinfo(void)
359{
360	if (!vmcoreinfo_note)
361		return;
362
363	/* Use the safe copy to generate vmcoreinfo note if have */
364	if (vmcoreinfo_data_safecopy)
365		vmcoreinfo_data = vmcoreinfo_data_safecopy;
 
 
 
 
 
 
 
 
 
366
367	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
368	update_vmcoreinfo_note();
 
 
 
 
 
 
 
 
 
 
369}
 
370
371void vmcoreinfo_append_str(const char *fmt, ...)
372{
373	va_list args;
374	char buf[0x50];
375	size_t r;
376
377	va_start(args, fmt);
378	r = vscnprintf(buf, sizeof(buf), fmt, args);
379	va_end(args);
380
381	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
 
 
 
 
 
 
 
 
 
382
383	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
 
 
 
 
 
 
 
384
385	vmcoreinfo_size += r;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
387	WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES,
388		  "vmcoreinfo data exceeds allocated size, truncating");
389}
390
391/*
392 * provide an empty default implementation here -- architecture
393 * code may override this
 
 
 
 
 
 
 
 
 
 
394 */
395void __weak arch_crash_save_vmcoreinfo(void)
396{}
397
398phys_addr_t __weak paddr_vmcoreinfo_note(void)
399{
400	return __pa(vmcoreinfo_note);
401}
402EXPORT_SYMBOL(paddr_vmcoreinfo_note);
403
404static int __init crash_save_vmcoreinfo_init(void)
405{
406	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
407	if (!vmcoreinfo_data) {
408		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
409		return -ENOMEM;
410	}
411
412	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
413						GFP_KERNEL | __GFP_ZERO);
414	if (!vmcoreinfo_note) {
415		free_page((unsigned long)vmcoreinfo_data);
416		vmcoreinfo_data = NULL;
417		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
418		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419	}
420
421	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
422	VMCOREINFO_BUILD_ID();
423	VMCOREINFO_PAGESIZE(PAGE_SIZE);
424
425	VMCOREINFO_SYMBOL(init_uts_ns);
426	VMCOREINFO_OFFSET(uts_namespace, name);
427	VMCOREINFO_SYMBOL(node_online_map);
428#ifdef CONFIG_MMU
429	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
430#endif
431	VMCOREINFO_SYMBOL(_stext);
432	VMCOREINFO_SYMBOL(vmap_area_list);
433
434#ifndef CONFIG_NUMA
435	VMCOREINFO_SYMBOL(mem_map);
436	VMCOREINFO_SYMBOL(contig_page_data);
437#endif
438#ifdef CONFIG_SPARSEMEM
439	VMCOREINFO_SYMBOL_ARRAY(mem_section);
440	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
441	VMCOREINFO_STRUCT_SIZE(mem_section);
442	VMCOREINFO_OFFSET(mem_section, section_mem_map);
443	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
444	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
445#endif
446	VMCOREINFO_STRUCT_SIZE(page);
447	VMCOREINFO_STRUCT_SIZE(pglist_data);
448	VMCOREINFO_STRUCT_SIZE(zone);
449	VMCOREINFO_STRUCT_SIZE(free_area);
450	VMCOREINFO_STRUCT_SIZE(list_head);
451	VMCOREINFO_SIZE(nodemask_t);
452	VMCOREINFO_OFFSET(page, flags);
453	VMCOREINFO_OFFSET(page, _refcount);
454	VMCOREINFO_OFFSET(page, mapping);
455	VMCOREINFO_OFFSET(page, lru);
456	VMCOREINFO_OFFSET(page, _mapcount);
457	VMCOREINFO_OFFSET(page, private);
458	VMCOREINFO_OFFSET(page, compound_dtor);
459	VMCOREINFO_OFFSET(page, compound_order);
460	VMCOREINFO_OFFSET(page, compound_head);
461	VMCOREINFO_OFFSET(pglist_data, node_zones);
462	VMCOREINFO_OFFSET(pglist_data, nr_zones);
463#ifdef CONFIG_FLATMEM
464	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
465#endif
466	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
467	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
468	VMCOREINFO_OFFSET(pglist_data, node_id);
469	VMCOREINFO_OFFSET(zone, free_area);
470	VMCOREINFO_OFFSET(zone, vm_stat);
471	VMCOREINFO_OFFSET(zone, spanned_pages);
472	VMCOREINFO_OFFSET(free_area, free_list);
473	VMCOREINFO_OFFSET(list_head, next);
474	VMCOREINFO_OFFSET(list_head, prev);
475	VMCOREINFO_OFFSET(vmap_area, va_start);
476	VMCOREINFO_OFFSET(vmap_area, list);
477	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
478	log_buf_vmcoreinfo_setup();
479	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
480	VMCOREINFO_NUMBER(NR_FREE_PAGES);
481	VMCOREINFO_NUMBER(PG_lru);
482	VMCOREINFO_NUMBER(PG_private);
483	VMCOREINFO_NUMBER(PG_swapcache);
484	VMCOREINFO_NUMBER(PG_swapbacked);
485	VMCOREINFO_NUMBER(PG_slab);
486#ifdef CONFIG_MEMORY_FAILURE
487	VMCOREINFO_NUMBER(PG_hwpoison);
488#endif
489	VMCOREINFO_NUMBER(PG_head_mask);
490#define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
491	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
492#ifdef CONFIG_HUGETLB_PAGE
493	VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
494#define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
495	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
496#endif
497
498#ifdef CONFIG_KALLSYMS
499	VMCOREINFO_SYMBOL(kallsyms_names);
500	VMCOREINFO_SYMBOL(kallsyms_num_syms);
501	VMCOREINFO_SYMBOL(kallsyms_token_table);
502	VMCOREINFO_SYMBOL(kallsyms_token_index);
503#ifdef CONFIG_KALLSYMS_BASE_RELATIVE
504	VMCOREINFO_SYMBOL(kallsyms_offsets);
505	VMCOREINFO_SYMBOL(kallsyms_relative_base);
506#else
507	VMCOREINFO_SYMBOL(kallsyms_addresses);
508#endif /* CONFIG_KALLSYMS_BASE_RELATIVE */
509#endif /* CONFIG_KALLSYMS */
510
511	arch_crash_save_vmcoreinfo();
512	update_vmcoreinfo_note();
 
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514	return 0;
515}
516
517subsys_initcall(crash_save_vmcoreinfo_init);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v6.9.4
  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/mm.h>
 15#include <linux/cpuhotplug.h>
 16#include <linux/memblock.h>
 17#include <linux/kmemleak.h>
 18#include <linux/crash_core.h>
 19#include <linux/reboot.h>
 20#include <linux/btf.h>
 21#include <linux/objtool.h>
 22
 23#include <asm/page.h>
 24#include <asm/sections.h>
 25
 26#include <crypto/sha1.h>
 27
 28#include "kallsyms_internal.h"
 29#include "kexec_internal.h"
 30
 31/* Per cpu memory for storing cpu states in case of system crash. */
 32note_buf_t __percpu *crash_notes;
 
 
 
 
 
 33
 34#ifdef CONFIG_CRASH_DUMP
 
 
 
 
 35
 36int kimage_crash_copy_vmcoreinfo(struct kimage *image)
 
 
 
 
 
 
 
 
 
 
 
 37{
 38	struct page *vmcoreinfo_page;
 39	void *safecopy;
 40
 41	if (!IS_ENABLED(CONFIG_CRASH_DUMP))
 42		return 0;
 43	if (image->type != KEXEC_TYPE_CRASH)
 44		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45
 46	/*
 47	 * For kdump, allocate one vmcoreinfo safe copy from the
 48	 * crash memory. as we have arch_kexec_protect_crashkres()
 49	 * after kexec syscall, we naturally protect it from write
 50	 * (even read) access under kernel direct mapping. But on
 51	 * the other hand, we still need to operate it when crash
 52	 * happens to generate vmcoreinfo note, hereby we rely on
 53	 * vmap for this purpose.
 54	 */
 55	vmcoreinfo_page = kimage_alloc_control_pages(image, 0);
 56	if (!vmcoreinfo_page) {
 57		pr_warn("Could not allocate vmcoreinfo buffer\n");
 58		return -ENOMEM;
 59	}
 60	safecopy = vmap(&vmcoreinfo_page, 1, VM_MAP, PAGE_KERNEL);
 61	if (!safecopy) {
 62		pr_warn("Could not vmap vmcoreinfo buffer\n");
 63		return -ENOMEM;
 64	}
 65
 66	image->vmcoreinfo_data_copy = safecopy;
 67	crash_update_vmcoreinfo_safecopy(safecopy);
 
 
 
 
 
 
 
 
 
 
 
 68
 69	return 0;
 70}
 71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72
 73
 74int kexec_should_crash(struct task_struct *p)
 75{
 76	/*
 77	 * If crash_kexec_post_notifiers is enabled, don't run
 78	 * crash_kexec() here yet, which must be run after panic
 79	 * notifiers in panic().
 80	 */
 81	if (crash_kexec_post_notifiers)
 82		return 0;
 83	/*
 84	 * There are 4 panic() calls in make_task_dead() path, each of which
 85	 * corresponds to each of these 4 conditions.
 86	 */
 87	if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
 88		return 1;
 89	return 0;
 90}
 91
 92int kexec_crash_loaded(void)
 93{
 94	return !!kexec_crash_image;
 95}
 96EXPORT_SYMBOL_GPL(kexec_crash_loaded);
 
 
 
 97
 98/*
 99 * No panic_cpu check version of crash_kexec().  This function is called
100 * only when panic_cpu holds the current CPU number; this is the only CPU
101 * which processes crash_kexec routines.
 
 
102 */
103void __noclone __crash_kexec(struct pt_regs *regs)
104{
105	/* Take the kexec_lock here to prevent sys_kexec_load
106	 * running on one cpu from replacing the crash kernel
107	 * we are using after a panic on a different cpu.
108	 *
109	 * If the crash kernel was not located in a fixed area
110	 * of memory the xchg(&kexec_crash_image) would be
111	 * sufficient.  But since I reuse the memory...
112	 */
113	if (kexec_trylock()) {
114		if (kexec_crash_image) {
115			struct pt_regs fixed_regs;
116
117			crash_setup_regs(&fixed_regs, regs);
118			crash_save_vmcoreinfo();
119			machine_crash_shutdown(&fixed_regs);
120			machine_kexec(kexec_crash_image);
121		}
122		kexec_unlock();
 
123	}
 
 
124}
125STACK_FRAME_NON_STANDARD(__crash_kexec);
126
127__bpf_kfunc void crash_kexec(struct pt_regs *regs)
128{
129	int old_cpu, this_cpu;
130
131	/*
132	 * Only one CPU is allowed to execute the crash_kexec() code as with
133	 * panic().  Otherwise parallel calls of panic() and crash_kexec()
134	 * may stop each other.  To exclude them, we use panic_cpu here too.
135	 */
136	old_cpu = PANIC_CPU_INVALID;
137	this_cpu = raw_smp_processor_id();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
139	if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) {
140		/* This is the 1st CPU which comes here, so go ahead. */
141		__crash_kexec(regs);
142
143		/*
144		 * Reset panic_cpu to allow another panic()/crash_kexec()
145		 * call.
146		 */
147		atomic_set(&panic_cpu, PANIC_CPU_INVALID);
148	}
149}
150
151static inline resource_size_t crash_resource_size(const struct resource *res)
 
 
 
 
 
152{
153	return !res->end ? 0 : resource_size(res);
154}
155
156
 
 
 
157
 
 
 
158
159int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
160			  void **addr, unsigned long *sz)
161{
162	Elf64_Ehdr *ehdr;
163	Elf64_Phdr *phdr;
164	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
165	unsigned char *buf;
166	unsigned int cpu, i;
167	unsigned long long notes_addr;
168	unsigned long mstart, mend;
169
170	/* extra phdr for vmcoreinfo ELF note */
171	nr_phdr = nr_cpus + 1;
172	nr_phdr += mem->nr_ranges;
173
 
 
 
174	/*
175	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
176	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
177	 * I think this is required by tools like gdb. So same physical
178	 * memory will be mapped in two ELF headers. One will contain kernel
179	 * text virtual addresses and other will have __va(physical) addresses.
180	 */
 
 
 
 
 
181
182	nr_phdr++;
183	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
184	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
185
186	buf = vzalloc(elf_sz);
187	if (!buf)
188		return -ENOMEM;
189
190	ehdr = (Elf64_Ehdr *)buf;
191	phdr = (Elf64_Phdr *)(ehdr + 1);
192	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
193	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
194	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
195	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
196	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
197	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
198	ehdr->e_type = ET_CORE;
199	ehdr->e_machine = ELF_ARCH;
200	ehdr->e_version = EV_CURRENT;
201	ehdr->e_phoff = sizeof(Elf64_Ehdr);
202	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
203	ehdr->e_phentsize = sizeof(Elf64_Phdr);
204
205	/* Prepare one phdr of type PT_NOTE for each possible CPU */
206	for_each_possible_cpu(cpu) {
207		phdr->p_type = PT_NOTE;
208		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
209		phdr->p_offset = phdr->p_paddr = notes_addr;
210		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
211		(ehdr->e_phnum)++;
212		phdr++;
213	}
214
215	/* Prepare one PT_NOTE header for vmcoreinfo */
216	phdr->p_type = PT_NOTE;
217	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
218	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
219	(ehdr->e_phnum)++;
220	phdr++;
221
222	/* Prepare PT_LOAD type program header for kernel text region */
223	if (need_kernel_map) {
224		phdr->p_type = PT_LOAD;
225		phdr->p_flags = PF_R|PF_W|PF_X;
226		phdr->p_vaddr = (unsigned long) _text;
227		phdr->p_filesz = phdr->p_memsz = _end - _text;
228		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
229		ehdr->e_phnum++;
230		phdr++;
231	}
232
233	/* Go through all the ranges in mem->ranges[] and prepare phdr */
234	for (i = 0; i < mem->nr_ranges; i++) {
235		mstart = mem->ranges[i].start;
236		mend = mem->ranges[i].end;
237
238		phdr->p_type = PT_LOAD;
239		phdr->p_flags = PF_R|PF_W|PF_X;
240		phdr->p_offset  = mstart;
241
242		phdr->p_paddr = mstart;
243		phdr->p_vaddr = (unsigned long) __va(mstart);
244		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
245		phdr->p_align = 0;
246		ehdr->e_phnum++;
247#ifdef CONFIG_KEXEC_FILE
248		kexec_dprintk("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
249			      phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
250			      ehdr->e_phnum, phdr->p_offset);
251#endif
252		phdr++;
253	}
254
255	*addr = buf;
256	*sz = elf_sz;
257	return 0;
258}
259
260int crash_exclude_mem_range(struct crash_mem *mem,
261			    unsigned long long mstart, unsigned long long mend)
 
 
262{
263	int i;
264	unsigned long long start, end, p_start, p_end;
265
266	for (i = 0; i < mem->nr_ranges; i++) {
267		start = mem->ranges[i].start;
268		end = mem->ranges[i].end;
269		p_start = mstart;
270		p_end = mend;
271
272		if (p_start > end)
273			continue;
274
275		/*
276		 * Because the memory ranges in mem->ranges are stored in
277		 * ascending order, when we detect `p_end < start`, we can
278		 * immediately exit the for loop, as the subsequent memory
279		 * ranges will definitely be outside the range we are looking
280		 * for.
281		 */
282		if (p_end < start)
283			break;
284
285		/* Truncate any area outside of range */
286		if (p_start < start)
287			p_start = start;
288		if (p_end > end)
289			p_end = end;
290
291		/* Found completely overlapping range */
292		if (p_start == start && p_end == end) {
293			memmove(&mem->ranges[i], &mem->ranges[i + 1],
294				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
295			i--;
296			mem->nr_ranges--;
297		} else if (p_start > start && p_end < end) {
298			/* Split original range */
299			if (mem->nr_ranges >= mem->max_nr_ranges)
300				return -ENOMEM;
301
302			memmove(&mem->ranges[i + 2], &mem->ranges[i + 1],
303				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
304
305			mem->ranges[i].end = p_start - 1;
306			mem->ranges[i + 1].start = p_end + 1;
307			mem->ranges[i + 1].end = end;
308
309			i++;
310			mem->nr_ranges++;
311		} else if (p_start != start)
312			mem->ranges[i].end = p_start - 1;
313		else
314			mem->ranges[i].start = p_end + 1;
315	}
316
317	return 0;
318}
319
320ssize_t crash_get_memory_size(void)
 
 
 
321{
322	ssize_t size = 0;
323
324	if (!kexec_trylock())
325		return -EBUSY;
326
327	size += crash_resource_size(&crashk_res);
328	size += crash_resource_size(&crashk_low_res);
329
330	kexec_unlock();
331	return size;
332}
333
334static int __crash_shrink_memory(struct resource *old_res,
335				 unsigned long new_size)
 
 
 
336{
337	struct resource *ram_res;
338
339	ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
340	if (!ram_res)
341		return -ENOMEM;
342
343	ram_res->start = old_res->start + new_size;
344	ram_res->end   = old_res->end;
345	ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
346	ram_res->name  = "System RAM";
347
348	if (!new_size) {
349		release_resource(old_res);
350		old_res->start = 0;
351		old_res->end   = 0;
352	} else {
353		crashk_res.end = ram_res->start - 1;
354	}
355
356	crash_free_reserved_phys_range(ram_res->start, ram_res->end);
357	insert_resource(&iomem_resource, ram_res);
358
359	return 0;
360}
 
361
362int crash_shrink_memory(unsigned long new_size)
 
363{
364	int ret = 0;
365	unsigned long old_size, low_size;
366
367	if (!kexec_trylock())
368		return -EBUSY;
 
 
 
 
 
 
369
370	if (kexec_crash_image) {
371		ret = -ENOENT;
372		goto unlock;
373	}
374
375	low_size = crash_resource_size(&crashk_low_res);
376	old_size = crash_resource_size(&crashk_res) + low_size;
377	new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
378	if (new_size >= old_size) {
379		ret = (new_size == old_size) ? 0 : -EINVAL;
380		goto unlock;
381	}
382
383	/*
384	 * (low_size > new_size) implies that low_size is greater than zero.
385	 * This also means that if low_size is zero, the else branch is taken.
386	 *
387	 * If low_size is greater than 0, (low_size > new_size) indicates that
388	 * crashk_low_res also needs to be shrunken. Otherwise, only crashk_res
389	 * needs to be shrunken.
390	 */
391	if (low_size > new_size) {
392		ret = __crash_shrink_memory(&crashk_res, 0);
393		if (ret)
394			goto unlock;
395
396		ret = __crash_shrink_memory(&crashk_low_res, new_size);
397	} else {
398		ret = __crash_shrink_memory(&crashk_res, new_size - low_size);
399	}
400
401	/* Swap crashk_res and crashk_low_res if needed */
402	if (!crashk_res.end && crashk_low_res.end) {
403		crashk_res.start = crashk_low_res.start;
404		crashk_res.end   = crashk_low_res.end;
405		release_resource(&crashk_low_res);
406		crashk_low_res.start = 0;
407		crashk_low_res.end   = 0;
408		insert_resource(&iomem_resource, &crashk_res);
409	}
410
411unlock:
412	kexec_unlock();
413	return ret;
414}
415
416void crash_save_cpu(struct pt_regs *regs, int cpu)
417{
418	struct elf_prstatus prstatus;
419	u32 *buf;
420
421	if ((cpu < 0) || (cpu >= nr_cpu_ids))
422		return;
423
424	/* Using ELF notes here is opportunistic.
425	 * I need a well defined structure format
426	 * for the data I pass, and I need tags
427	 * on the data to indicate what information I have
428	 * squirrelled away.  ELF notes happen to provide
429	 * all of that, so there is no need to invent something new.
430	 */
431	buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
432	if (!buf)
433		return;
434	memset(&prstatus, 0, sizeof(prstatus));
435	prstatus.common.pr_pid = current->pid;
436	elf_core_copy_regs(&prstatus.pr_reg, regs);
437	buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
438			      &prstatus, sizeof(prstatus));
439	final_note(buf);
440}
441
 
 
 
 
442
 
 
443
444static int __init crash_notes_memory_init(void)
445{
446	/* Allocate memory for saving cpu registers. */
447	size_t size, align;
448
449	/*
450	 * crash_notes could be allocated across 2 vmalloc pages when percpu
451	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
452	 * pages are also on 2 continuous physical pages. In this case the
453	 * 2nd part of crash_notes in 2nd page could be lost since only the
454	 * starting address and size of crash_notes are exported through sysfs.
455	 * Here round up the size of crash_notes to the nearest power of two
456	 * and pass it to __alloc_percpu as align value. This can make sure
457	 * crash_notes is allocated inside one physical page.
458	 */
459	size = sizeof(note_buf_t);
460	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
461
462	/*
463	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
464	 * definitely will be in 2 pages with that.
465	 */
466	BUILD_BUG_ON(size > PAGE_SIZE);
467
468	crash_notes = __alloc_percpu(size, align);
469	if (!crash_notes) {
470		pr_warn("Memory allocation for saving cpu register states failed\n");
471		return -ENOMEM;
472	}
473	return 0;
474}
475subsys_initcall(crash_notes_memory_init);
476
477#endif /*CONFIG_CRASH_DUMP*/
 
 
 
 
478
479#ifdef CONFIG_CRASH_HOTPLUG
480#undef pr_fmt
481#define pr_fmt(fmt) "crash hp: " fmt
482
483/*
484 * Different than kexec/kdump loading/unloading/jumping/shrinking which
485 * usually rarely happen, there will be many crash hotplug events notified
486 * during one short period, e.g one memory board is hot added and memory
487 * regions are online. So mutex lock  __crash_hotplug_lock is used to
488 * serialize the crash hotplug handling specifically.
489 */
490static DEFINE_MUTEX(__crash_hotplug_lock);
491#define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock)
492#define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock)
493
494/*
495 * This routine utilized when the crash_hotplug sysfs node is read.
496 * It reflects the kernel's ability/permission to update the crash
497 * elfcorehdr directly.
498 */
499int crash_check_update_elfcorehdr(void)
500{
501	int rc = 0;
502
503	crash_hotplug_lock();
504	/* Obtain lock while reading crash information */
505	if (!kexec_trylock()) {
506		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
507		crash_hotplug_unlock();
508		return 0;
509	}
510	if (kexec_crash_image) {
511		if (kexec_crash_image->file_mode)
512			rc = 1;
513		else
514			rc = kexec_crash_image->update_elfcorehdr;
515	}
516	/* Release lock now that update complete */
517	kexec_unlock();
518	crash_hotplug_unlock();
519
520	return rc;
 
521}
522
523/*
524 * To accurately reflect hot un/plug changes of cpu and memory resources
525 * (including onling and offlining of those resources), the elfcorehdr
526 * (which is passed to the crash kernel via the elfcorehdr= parameter)
527 * must be updated with the new list of CPUs and memories.
528 *
529 * In order to make changes to elfcorehdr, two conditions are needed:
530 * First, the segment containing the elfcorehdr must be large enough
531 * to permit a growing number of resources; the elfcorehdr memory size
532 * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES.
533 * Second, purgatory must explicitly exclude the elfcorehdr from the
534 * list of segments it checks (since the elfcorehdr changes and thus
535 * would require an update to purgatory itself to update the digest).
536 */
537static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu)
 
 
 
538{
539	struct kimage *image;
 
 
540
541	crash_hotplug_lock();
542	/* Obtain lock while changing crash information */
543	if (!kexec_trylock()) {
544		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
545		crash_hotplug_unlock();
546		return;
547	}
548
549	/* Check kdump is not loaded */
550	if (!kexec_crash_image)
551		goto out;
552
553	image = kexec_crash_image;
554
555	/* Check that updating elfcorehdr is permitted */
556	if (!(image->file_mode || image->update_elfcorehdr))
557		goto out;
558
559	if (hp_action == KEXEC_CRASH_HP_ADD_CPU ||
560		hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
561		pr_debug("hp_action %u, cpu %u\n", hp_action, cpu);
562	else
563		pr_debug("hp_action %u\n", hp_action);
564
565	/*
566	 * The elfcorehdr_index is set to -1 when the struct kimage
567	 * is allocated. Find the segment containing the elfcorehdr,
568	 * if not already found.
569	 */
570	if (image->elfcorehdr_index < 0) {
571		unsigned long mem;
572		unsigned char *ptr;
573		unsigned int n;
574
575		for (n = 0; n < image->nr_segments; n++) {
576			mem = image->segment[n].mem;
577			ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
578			if (ptr) {
579				/* The segment containing elfcorehdr */
580				if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
581					image->elfcorehdr_index = (int)n;
582				kunmap_local(ptr);
583			}
584		}
585	}
586
587	if (image->elfcorehdr_index < 0) {
588		pr_err("unable to locate elfcorehdr segment");
589		goto out;
590	}
 
 
 
 
 
 
 
 
591
592	/* Needed in order for the segments to be updated */
593	arch_kexec_unprotect_crashkres();
594
595	/* Differentiate between normal load and hotplug update */
596	image->hp_action = hp_action;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
598	/* Now invoke arch-specific update handler */
599	arch_crash_handle_hotplug_event(image);
 
 
 
 
 
 
 
 
 
 
600
601	/* No longer handling a hotplug event */
602	image->hp_action = KEXEC_CRASH_HP_NONE;
603	image->elfcorehdr_updated = true;
604
605	/* Change back to read-only */
606	arch_kexec_protect_crashkres();
607
608	/* Errors in the callback is not a reason to rollback state */
609out:
610	/* Release lock now that update complete */
611	kexec_unlock();
612	crash_hotplug_unlock();
613}
614
615static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v)
616{
617	switch (val) {
618	case MEM_ONLINE:
619		crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY,
620			KEXEC_CRASH_HP_INVALID_CPU);
621		break;
622
623	case MEM_OFFLINE:
624		crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY,
625			KEXEC_CRASH_HP_INVALID_CPU);
626		break;
627	}
628	return NOTIFY_OK;
629}
630
631static struct notifier_block crash_memhp_nb = {
632	.notifier_call = crash_memhp_notifier,
633	.priority = 0
634};
635
636static int crash_cpuhp_online(unsigned int cpu)
637{
638	crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu);
639	return 0;
640}
641
642static int crash_cpuhp_offline(unsigned int cpu)
643{
644	crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu);
645	return 0;
646}
647
648static int __init crash_hotplug_init(void)
649{
650	int result = 0;
651
652	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
653		register_memory_notifier(&crash_memhp_nb);
654
655	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
656		result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
657			"crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline);
658	}
659
660	return result;
661}
662
663subsys_initcall(crash_hotplug_init);
664#endif