xref: /linux/kernel/crash_core.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/buildid.h>
10 #include <linux/init.h>
11 #include <linux/utsname.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sizes.h>
14 #include <linux/kexec.h>
15 #include <linux/memory.h>
16 #include <linux/mm.h>
17 #include <linux/cpuhotplug.h>
18 #include <linux/memblock.h>
19 #include <linux/kmemleak.h>
20 #include <linux/crash_core.h>
21 #include <linux/reboot.h>
22 #include <linux/btf.h>
23 #include <linux/objtool.h>
24 #include <linux/delay.h>
25 #include <linux/panic.h>
26 
27 #include <asm/page.h>
28 #include <asm/sections.h>
29 
30 #include "kallsyms_internal.h"
31 #include "kexec_internal.h"
32 
33 /* Per cpu memory for storing cpu states in case of system crash. */
34 note_buf_t __percpu *crash_notes;
35 
36 /* time to wait for possible DMA to finish before starting the kdump kernel
37  * when a CMA reservation is used
38  */
39 #define CMA_DMA_TIMEOUT_SEC 10
40 
41 #ifdef CONFIG_CRASH_DUMP
42 
43 int kimage_crash_copy_vmcoreinfo(struct kimage *image)
44 {
45 	struct page *vmcoreinfo_base;
46 	struct page *vmcoreinfo_pages[DIV_ROUND_UP(VMCOREINFO_BYTES, PAGE_SIZE)];
47 	unsigned int order, nr_pages;
48 	int i;
49 	void *safecopy;
50 
51 	nr_pages = DIV_ROUND_UP(VMCOREINFO_BYTES, PAGE_SIZE);
52 	order = get_order(VMCOREINFO_BYTES);
53 
54 	if (!IS_ENABLED(CONFIG_CRASH_DUMP))
55 		return 0;
56 	if (image->type != KEXEC_TYPE_CRASH)
57 		return 0;
58 
59 	/*
60 	 * For kdump, allocate one vmcoreinfo safe copy from the
61 	 * crash memory. as we have arch_kexec_protect_crashkres()
62 	 * after kexec syscall, we naturally protect it from write
63 	 * (even read) access under kernel direct mapping. But on
64 	 * the other hand, we still need to operate it when crash
65 	 * happens to generate vmcoreinfo note, hereby we rely on
66 	 * vmap for this purpose.
67 	 */
68 	vmcoreinfo_base = kimage_alloc_control_pages(image, order);
69 	if (!vmcoreinfo_base) {
70 		pr_warn("Could not allocate vmcoreinfo buffer\n");
71 		return -ENOMEM;
72 	}
73 	for (i = 0; i < nr_pages; i++)
74 		vmcoreinfo_pages[i] = vmcoreinfo_base + i;
75 
76 	safecopy = vmap(vmcoreinfo_pages, nr_pages, VM_MAP, PAGE_KERNEL);
77 	if (!safecopy) {
78 		pr_warn("Could not vmap vmcoreinfo buffer\n");
79 		return -ENOMEM;
80 	}
81 
82 	image->vmcoreinfo_data_copy = safecopy;
83 	crash_update_vmcoreinfo_safecopy(safecopy);
84 
85 	return 0;
86 }
87 
88 
89 
90 int kexec_should_crash(struct task_struct *p)
91 {
92 	/*
93 	 * If crash_kexec_post_notifiers is enabled, don't run
94 	 * crash_kexec() here yet, which must be run after panic
95 	 * notifiers in panic().
96 	 */
97 	if (crash_kexec_post_notifiers)
98 		return 0;
99 	/*
100 	 * There are 4 panic() calls in make_task_dead() path, each of which
101 	 * corresponds to each of these 4 conditions.
102 	 */
103 	if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
104 		return 1;
105 	return 0;
106 }
107 
108 int kexec_crash_loaded(void)
109 {
110 	return !!kexec_crash_image;
111 }
112 EXPORT_SYMBOL_GPL(kexec_crash_loaded);
113 
114 static void crash_cma_clear_pending_dma(void)
115 {
116 	if (!crashk_cma_cnt)
117 		return;
118 
119 	mdelay(CMA_DMA_TIMEOUT_SEC * 1000);
120 }
121 
122 /*
123  * No panic_cpu check version of crash_kexec().  This function is called
124  * only when panic_cpu holds the current CPU number; this is the only CPU
125  * which processes crash_kexec routines.
126  */
127 void __noclone __crash_kexec(struct pt_regs *regs)
128 {
129 	/* Take the kexec_lock here to prevent sys_kexec_load
130 	 * running on one cpu from replacing the crash kernel
131 	 * we are using after a panic on a different cpu.
132 	 *
133 	 * If the crash kernel was not located in a fixed area
134 	 * of memory the xchg(&kexec_crash_image) would be
135 	 * sufficient.  But since I reuse the memory...
136 	 */
137 	if (kexec_trylock()) {
138 		if (kexec_crash_image) {
139 			struct pt_regs fixed_regs;
140 
141 			crash_setup_regs(&fixed_regs, regs);
142 			crash_save_vmcoreinfo();
143 			machine_crash_shutdown(&fixed_regs);
144 			crash_cma_clear_pending_dma();
145 			machine_kexec(kexec_crash_image);
146 		}
147 		kexec_unlock();
148 	}
149 }
150 STACK_FRAME_NON_STANDARD(__crash_kexec);
151 
152 __bpf_kfunc void crash_kexec(struct pt_regs *regs)
153 {
154 	if (panic_try_start()) {
155 		/* This is the 1st CPU which comes here, so go ahead. */
156 		__crash_kexec(regs);
157 
158 		/*
159 		 * Reset panic_cpu to allow another panic()/crash_kexec()
160 		 * call.
161 		 */
162 		panic_reset();
163 	}
164 }
165 
166 static inline resource_size_t crash_resource_size(const struct resource *res)
167 {
168 	return !res->end ? 0 : resource_size(res);
169 }
170 
171 
172 
173 
174 int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
175 			  void **addr, unsigned long *sz)
176 {
177 	Elf64_Ehdr *ehdr;
178 	Elf64_Phdr *phdr;
179 	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
180 	unsigned char *buf;
181 	unsigned int cpu, i;
182 	unsigned long long notes_addr;
183 	unsigned long mstart, mend;
184 
185 	/* extra phdr for vmcoreinfo ELF note */
186 	nr_phdr = nr_cpus + 1;
187 	nr_phdr += mem->nr_ranges;
188 
189 	/*
190 	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
191 	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
192 	 * I think this is required by tools like gdb. So same physical
193 	 * memory will be mapped in two ELF headers. One will contain kernel
194 	 * text virtual addresses and other will have __va(physical) addresses.
195 	 */
196 
197 	nr_phdr++;
198 	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
199 	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
200 
201 	buf = vzalloc(elf_sz);
202 	if (!buf)
203 		return -ENOMEM;
204 
205 	ehdr = (Elf64_Ehdr *)buf;
206 	phdr = (Elf64_Phdr *)(ehdr + 1);
207 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
208 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
209 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
210 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
211 	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
212 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
213 	ehdr->e_type = ET_CORE;
214 	ehdr->e_machine = ELF_ARCH;
215 	ehdr->e_version = EV_CURRENT;
216 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
217 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
218 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
219 
220 	/* Prepare one phdr of type PT_NOTE for each possible CPU */
221 	for_each_possible_cpu(cpu) {
222 		phdr->p_type = PT_NOTE;
223 		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
224 		phdr->p_offset = phdr->p_paddr = notes_addr;
225 		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
226 		(ehdr->e_phnum)++;
227 		phdr++;
228 	}
229 
230 	/* Prepare one PT_NOTE header for vmcoreinfo */
231 	phdr->p_type = PT_NOTE;
232 	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
233 	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
234 	(ehdr->e_phnum)++;
235 	phdr++;
236 
237 	/* Prepare PT_LOAD type program header for kernel text region */
238 	if (need_kernel_map) {
239 		phdr->p_type = PT_LOAD;
240 		phdr->p_flags = PF_R|PF_W|PF_X;
241 		phdr->p_vaddr = (unsigned long) _text;
242 		phdr->p_filesz = phdr->p_memsz = _end - _text;
243 		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
244 		ehdr->e_phnum++;
245 		phdr++;
246 	}
247 
248 	/* Go through all the ranges in mem->ranges[] and prepare phdr */
249 	for (i = 0; i < mem->nr_ranges; i++) {
250 		mstart = mem->ranges[i].start;
251 		mend = mem->ranges[i].end;
252 
253 		phdr->p_type = PT_LOAD;
254 		phdr->p_flags = PF_R|PF_W|PF_X;
255 		phdr->p_offset  = mstart;
256 
257 		phdr->p_paddr = mstart;
258 		phdr->p_vaddr = (unsigned long) __va(mstart);
259 		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
260 		phdr->p_align = 0;
261 		ehdr->e_phnum++;
262 #ifdef CONFIG_KEXEC_FILE
263 		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",
264 			      phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
265 			      ehdr->e_phnum, phdr->p_offset);
266 #endif
267 		phdr++;
268 	}
269 
270 	*addr = buf;
271 	*sz = elf_sz;
272 	return 0;
273 }
274 
275 /**
276  * crash_exclude_mem_range - exclude a mem range for existing ranges
277  * @mem: mem->range contains an array of ranges sorted in ascending order
278  * @mstart: the start of to-be-excluded range
279  * @mend: the start of to-be-excluded range
280  *
281  * If you are unsure if a range split will happen, to avoid function call
282  * failure because of -ENOMEM, always make sure
283  *    mem->max_nr_ranges == mem->nr_ranges + 1
284  * before calling the function each time.
285  *
286  * returns 0 if a memory range is excluded successfully
287  * return -ENOMEM if mem->ranges doesn't have space to hold split ranges
288  */
289 int crash_exclude_mem_range(struct crash_mem *mem,
290 			    unsigned long long mstart, unsigned long long mend)
291 {
292 	int i;
293 	unsigned long long start, end, p_start, p_end;
294 
295 	for (i = 0; i < mem->nr_ranges; i++) {
296 		start = mem->ranges[i].start;
297 		end = mem->ranges[i].end;
298 		p_start = mstart;
299 		p_end = mend;
300 
301 		if (p_start > end)
302 			continue;
303 
304 		/*
305 		 * Because the memory ranges in mem->ranges are stored in
306 		 * ascending order, when we detect `p_end < start`, we can
307 		 * immediately exit the for loop, as the subsequent memory
308 		 * ranges will definitely be outside the range we are looking
309 		 * for.
310 		 */
311 		if (p_end < start)
312 			break;
313 
314 		/* Truncate any area outside of range */
315 		if (p_start < start)
316 			p_start = start;
317 		if (p_end > end)
318 			p_end = end;
319 
320 		/* Found completely overlapping range */
321 		if (p_start == start && p_end == end) {
322 			memmove(&mem->ranges[i], &mem->ranges[i + 1],
323 				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
324 			i--;
325 			mem->nr_ranges--;
326 		} else if (p_start > start && p_end < end) {
327 			/* Split original range */
328 			if (mem->nr_ranges >= mem->max_nr_ranges)
329 				return -ENOMEM;
330 
331 			memmove(&mem->ranges[i + 2], &mem->ranges[i + 1],
332 				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
333 
334 			mem->ranges[i].end = p_start - 1;
335 			mem->ranges[i + 1].start = p_end + 1;
336 			mem->ranges[i + 1].end = end;
337 
338 			i++;
339 			mem->nr_ranges++;
340 		} else if (p_start != start)
341 			mem->ranges[i].end = p_start - 1;
342 		else
343 			mem->ranges[i].start = p_end + 1;
344 	}
345 
346 	return 0;
347 }
348 EXPORT_SYMBOL_GPL(crash_exclude_mem_range);
349 
350 ssize_t crash_get_memory_size(void)
351 {
352 	ssize_t size = 0;
353 
354 	if (!kexec_trylock())
355 		return -EBUSY;
356 
357 	size += crash_resource_size(&crashk_res);
358 	size += crash_resource_size(&crashk_low_res);
359 
360 	kexec_unlock();
361 	return size;
362 }
363 
364 static int __crash_shrink_memory(struct resource *old_res,
365 				 unsigned long new_size)
366 {
367 	struct resource *ram_res;
368 
369 	ram_res = kzalloc_obj(*ram_res);
370 	if (!ram_res)
371 		return -ENOMEM;
372 
373 	ram_res->start = old_res->start + new_size;
374 	ram_res->end   = old_res->end;
375 	ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
376 	ram_res->name  = "System RAM";
377 
378 	if (!new_size) {
379 		release_resource(old_res);
380 		old_res->start = 0;
381 		old_res->end   = 0;
382 	} else {
383 		old_res->end = ram_res->start - 1;
384 	}
385 
386 	crash_free_reserved_phys_range(ram_res->start, ram_res->end);
387 	insert_resource(&iomem_resource, ram_res);
388 
389 	return 0;
390 }
391 
392 int crash_shrink_memory(unsigned long new_size)
393 {
394 	int ret = 0;
395 	unsigned long old_size, low_size;
396 
397 	if (!kexec_trylock())
398 		return -EBUSY;
399 
400 	if (kexec_crash_image) {
401 		ret = -ENOENT;
402 		goto unlock;
403 	}
404 
405 	low_size = crash_resource_size(&crashk_low_res);
406 	old_size = crash_resource_size(&crashk_res) + low_size;
407 	new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
408 	if (new_size >= old_size) {
409 		ret = (new_size == old_size) ? 0 : -EINVAL;
410 		goto unlock;
411 	}
412 
413 	/*
414 	 * (low_size > new_size) implies that low_size is greater than zero.
415 	 * This also means that if low_size is zero, the else branch is taken.
416 	 *
417 	 * If low_size is greater than 0, (low_size > new_size) indicates that
418 	 * crashk_low_res also needs to be shrunken. Otherwise, only crashk_res
419 	 * needs to be shrunken.
420 	 */
421 	if (low_size > new_size) {
422 		ret = __crash_shrink_memory(&crashk_res, 0);
423 		if (ret)
424 			goto unlock;
425 
426 		ret = __crash_shrink_memory(&crashk_low_res, new_size);
427 	} else {
428 		ret = __crash_shrink_memory(&crashk_res, new_size - low_size);
429 	}
430 
431 	/* Swap crashk_res and crashk_low_res if needed */
432 	if (!crashk_res.end && crashk_low_res.end) {
433 		crashk_res.start = crashk_low_res.start;
434 		crashk_res.end   = crashk_low_res.end;
435 		release_resource(&crashk_low_res);
436 		crashk_low_res.start = 0;
437 		crashk_low_res.end   = 0;
438 		insert_resource(&iomem_resource, &crashk_res);
439 	}
440 
441 unlock:
442 	kexec_unlock();
443 	return ret;
444 }
445 
446 void crash_save_cpu(struct pt_regs *regs, int cpu)
447 {
448 	struct elf_prstatus prstatus;
449 	u32 *buf;
450 
451 	if ((cpu < 0) || (cpu >= nr_cpu_ids))
452 		return;
453 
454 	/* Using ELF notes here is opportunistic.
455 	 * I need a well defined structure format
456 	 * for the data I pass, and I need tags
457 	 * on the data to indicate what information I have
458 	 * squirrelled away.  ELF notes happen to provide
459 	 * all of that, so there is no need to invent something new.
460 	 */
461 	buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
462 	if (!buf)
463 		return;
464 	memset(&prstatus, 0, sizeof(prstatus));
465 	prstatus.common.pr_pid = current->pid;
466 	elf_core_copy_regs(&prstatus.pr_reg, regs);
467 	buf = append_elf_note(buf, NN_PRSTATUS, NT_PRSTATUS,
468 			      &prstatus, sizeof(prstatus));
469 	final_note(buf);
470 }
471 
472 
473 
474 static int __init crash_notes_memory_init(void)
475 {
476 	/* Allocate memory for saving cpu registers. */
477 	size_t size, align;
478 
479 	/*
480 	 * crash_notes could be allocated across 2 vmalloc pages when percpu
481 	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
482 	 * pages are also on 2 continuous physical pages. In this case the
483 	 * 2nd part of crash_notes in 2nd page could be lost since only the
484 	 * starting address and size of crash_notes are exported through sysfs.
485 	 * Here round up the size of crash_notes to the nearest power of two
486 	 * and pass it to __alloc_percpu as align value. This can make sure
487 	 * crash_notes is allocated inside one physical page.
488 	 */
489 	size = sizeof(note_buf_t);
490 	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
491 
492 	/*
493 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
494 	 * definitely will be in 2 pages with that.
495 	 */
496 	BUILD_BUG_ON(size > PAGE_SIZE);
497 
498 	crash_notes = __alloc_percpu(size, align);
499 	if (!crash_notes) {
500 		pr_warn("Memory allocation for saving cpu register states failed\n");
501 		return -ENOMEM;
502 	}
503 	return 0;
504 }
505 subsys_initcall(crash_notes_memory_init);
506 
507 #endif /*CONFIG_CRASH_DUMP*/
508 
509 #ifdef CONFIG_CRASH_HOTPLUG
510 #undef pr_fmt
511 #define pr_fmt(fmt) "crash hp: " fmt
512 
513 /*
514  * Different than kexec/kdump loading/unloading/jumping/shrinking which
515  * usually rarely happen, there will be many crash hotplug events notified
516  * during one short period, e.g one memory board is hot added and memory
517  * regions are online. So mutex lock  __crash_hotplug_lock is used to
518  * serialize the crash hotplug handling specifically.
519  */
520 static DEFINE_MUTEX(__crash_hotplug_lock);
521 #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock)
522 #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock)
523 
524 /*
525  * This routine utilized when the crash_hotplug sysfs node is read.
526  * It reflects the kernel's ability/permission to update the kdump
527  * image directly.
528  */
529 int crash_check_hotplug_support(void)
530 {
531 	int rc = 0;
532 
533 	crash_hotplug_lock();
534 	/* Obtain lock while reading crash information */
535 	if (!kexec_trylock()) {
536 		if (!kexec_in_progress)
537 			pr_info("kexec_trylock() failed, kdump image may be inaccurate\n");
538 		crash_hotplug_unlock();
539 		return 0;
540 	}
541 	if (kexec_crash_image) {
542 		rc = kexec_crash_image->hotplug_support;
543 	}
544 	/* Release lock now that update complete */
545 	kexec_unlock();
546 	crash_hotplug_unlock();
547 
548 	return rc;
549 }
550 
551 /*
552  * To accurately reflect hot un/plug changes of CPU and Memory resources
553  * (including onling and offlining of those resources), the relevant
554  * kexec segments must be updated with latest CPU and Memory resources.
555  *
556  * Architectures must ensure two things for all segments that need
557  * updating during hotplug events:
558  *
559  * 1. Segments must be large enough to accommodate a growing number of
560  *    resources.
561  * 2. Exclude the segments from SHA verification.
562  *
563  * For example, on most architectures, the elfcorehdr (which is passed
564  * to the crash kernel via the elfcorehdr= parameter) must include the
565  * new list of CPUs and memory. To make changes to the elfcorehdr, it
566  * should be large enough to permit a growing number of CPU and Memory
567  * resources. One can estimate the elfcorehdr memory size based on
568  * NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. The elfcorehdr is
569  * excluded from SHA verification by default if the architecture
570  * supports crash hotplug.
571  */
572 static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu, void *arg)
573 {
574 	struct kimage *image;
575 
576 	crash_hotplug_lock();
577 	/* Obtain lock while changing crash information */
578 	if (!kexec_trylock()) {
579 		if (!kexec_in_progress)
580 			pr_info("kexec_trylock() failed, kdump image may be inaccurate\n");
581 		crash_hotplug_unlock();
582 		return;
583 	}
584 
585 	/* Check kdump is not loaded */
586 	if (!kexec_crash_image)
587 		goto out;
588 
589 	image = kexec_crash_image;
590 
591 	/* Check that kexec segments update is permitted */
592 	if (!image->hotplug_support)
593 		goto out;
594 
595 	if (hp_action == KEXEC_CRASH_HP_ADD_CPU ||
596 		hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
597 		pr_debug("hp_action %u, cpu %u\n", hp_action, cpu);
598 	else
599 		pr_debug("hp_action %u\n", hp_action);
600 
601 	/*
602 	 * The elfcorehdr_index is set to -1 when the struct kimage
603 	 * is allocated. Find the segment containing the elfcorehdr,
604 	 * if not already found.
605 	 */
606 	if (image->elfcorehdr_index < 0) {
607 		unsigned long mem;
608 		unsigned char *ptr;
609 		unsigned int n;
610 
611 		for (n = 0; n < image->nr_segments; n++) {
612 			mem = image->segment[n].mem;
613 			ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
614 			if (ptr) {
615 				/* The segment containing elfcorehdr */
616 				if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
617 					image->elfcorehdr_index = (int)n;
618 				kunmap_local(ptr);
619 			}
620 		}
621 	}
622 
623 	if (image->elfcorehdr_index < 0) {
624 		pr_err("unable to locate elfcorehdr segment");
625 		goto out;
626 	}
627 
628 	/* Needed in order for the segments to be updated */
629 	arch_kexec_unprotect_crashkres();
630 
631 	/* Differentiate between normal load and hotplug update */
632 	image->hp_action = hp_action;
633 
634 	/* Now invoke arch-specific update handler */
635 	arch_crash_handle_hotplug_event(image, arg);
636 
637 	/* No longer handling a hotplug event */
638 	image->hp_action = KEXEC_CRASH_HP_NONE;
639 	image->elfcorehdr_updated = true;
640 
641 	/* Change back to read-only */
642 	arch_kexec_protect_crashkres();
643 
644 	/* Errors in the callback is not a reason to rollback state */
645 out:
646 	/* Release lock now that update complete */
647 	kexec_unlock();
648 	crash_hotplug_unlock();
649 }
650 
651 static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *arg)
652 {
653 	switch (val) {
654 	case MEM_ONLINE:
655 		crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY,
656 			KEXEC_CRASH_HP_INVALID_CPU, arg);
657 		break;
658 
659 	case MEM_OFFLINE:
660 		crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY,
661 			KEXEC_CRASH_HP_INVALID_CPU, arg);
662 		break;
663 	}
664 	return NOTIFY_OK;
665 }
666 
667 static struct notifier_block crash_memhp_nb = {
668 	.notifier_call = crash_memhp_notifier,
669 	.priority = 0
670 };
671 
672 static int crash_cpuhp_online(unsigned int cpu)
673 {
674 	crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu, NULL);
675 	return 0;
676 }
677 
678 static int crash_cpuhp_offline(unsigned int cpu)
679 {
680 	crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu, NULL);
681 	return 0;
682 }
683 
684 static int __init crash_hotplug_init(void)
685 {
686 	int result = 0;
687 
688 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
689 		register_memory_notifier(&crash_memhp_nb);
690 
691 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
692 		result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
693 			"crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline);
694 	}
695 
696 	return result;
697 }
698 
699 subsys_initcall(crash_hotplug_init);
700 #endif
701