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