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