1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kexec.c - kexec system call core 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/btf.h> 10 #include <linux/capability.h> 11 #include <linux/mm.h> 12 #include <linux/file.h> 13 #include <linux/slab.h> 14 #include <linux/fs.h> 15 #include <linux/kexec.h> 16 #include <linux/mutex.h> 17 #include <linux/list.h> 18 #include <linux/highmem.h> 19 #include <linux/syscalls.h> 20 #include <linux/reboot.h> 21 #include <linux/ioport.h> 22 #include <linux/hardirq.h> 23 #include <linux/elf.h> 24 #include <linux/elfcore.h> 25 #include <linux/utsname.h> 26 #include <linux/numa.h> 27 #include <linux/suspend.h> 28 #include <linux/device.h> 29 #include <linux/freezer.h> 30 #include <linux/panic_notifier.h> 31 #include <linux/pm.h> 32 #include <linux/cpu.h> 33 #include <linux/uaccess.h> 34 #include <linux/io.h> 35 #include <linux/console.h> 36 #include <linux/vmalloc.h> 37 #include <linux/swap.h> 38 #include <linux/syscore_ops.h> 39 #include <linux/compiler.h> 40 #include <linux/hugetlb.h> 41 #include <linux/objtool.h> 42 #include <linux/kmsg_dump.h> 43 #include <linux/dma-map-ops.h> 44 45 #include <asm/page.h> 46 #include <asm/sections.h> 47 48 #include <crypto/hash.h> 49 #include "kexec_internal.h" 50 51 atomic_t __kexec_lock = ATOMIC_INIT(0); 52 53 /* Flag to indicate we are going to kexec a new kernel */ 54 bool kexec_in_progress = false; 55 56 bool kexec_file_dbg_print; 57 58 /* 59 * When kexec transitions to the new kernel there is a one-to-one 60 * mapping between physical and virtual addresses. On processors 61 * where you can disable the MMU this is trivial, and easy. For 62 * others it is still a simple predictable page table to setup. 63 * 64 * In that environment kexec copies the new kernel to its final 65 * resting place. This means I can only support memory whose 66 * physical address can fit in an unsigned long. In particular 67 * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled. 68 * If the assembly stub has more restrictive requirements 69 * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be 70 * defined more restrictively in <asm/kexec.h>. 71 * 72 * The code for the transition from the current kernel to the 73 * new kernel is placed in the control_code_buffer, whose size 74 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single 75 * page of memory is necessary, but some architectures require more. 76 * Because this memory must be identity mapped in the transition from 77 * virtual to physical addresses it must live in the range 78 * 0 - TASK_SIZE, as only the user space mappings are arbitrarily 79 * modifiable. 80 * 81 * The assembly stub in the control code buffer is passed a linked list 82 * of descriptor pages detailing the source pages of the new kernel, 83 * and the destination addresses of those source pages. As this data 84 * structure is not used in the context of the current OS, it must 85 * be self-contained. 86 * 87 * The code has been made to work with highmem pages and will use a 88 * destination page in its final resting place (if it happens 89 * to allocate it). The end product of this is that most of the 90 * physical address space, and most of RAM can be used. 91 * 92 * Future directions include: 93 * - allocating a page table with the control code buffer identity 94 * mapped, to simplify machine_kexec and make kexec_on_panic more 95 * reliable. 96 */ 97 98 /* 99 * KIMAGE_NO_DEST is an impossible destination address..., for 100 * allocating pages whose destination address we do not care about. 101 */ 102 #define KIMAGE_NO_DEST (-1UL) 103 #define PAGE_COUNT(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT) 104 105 static struct page *kimage_alloc_page(struct kimage *image, 106 gfp_t gfp_mask, 107 unsigned long dest); 108 109 int sanity_check_segment_list(struct kimage *image) 110 { 111 int i; 112 unsigned long nr_segments = image->nr_segments; 113 unsigned long total_pages = 0; 114 unsigned long nr_pages = totalram_pages(); 115 116 /* 117 * Verify we have good destination addresses. The caller is 118 * responsible for making certain we don't attempt to load 119 * the new image into invalid or reserved areas of RAM. This 120 * just verifies it is an address we can use. 121 * 122 * Since the kernel does everything in page size chunks ensure 123 * the destination addresses are page aligned. Too many 124 * special cases crop of when we don't do this. The most 125 * insidious is getting overlapping destination addresses 126 * simply because addresses are changed to page size 127 * granularity. 128 */ 129 for (i = 0; i < nr_segments; i++) { 130 unsigned long mstart, mend; 131 132 mstart = image->segment[i].mem; 133 mend = mstart + image->segment[i].memsz; 134 if (mstart > mend) 135 return -EADDRNOTAVAIL; 136 if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK)) 137 return -EADDRNOTAVAIL; 138 if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT) 139 return -EADDRNOTAVAIL; 140 } 141 142 /* Verify our destination addresses do not overlap. 143 * If we alloed overlapping destination addresses 144 * through very weird things can happen with no 145 * easy explanation as one segment stops on another. 146 */ 147 for (i = 0; i < nr_segments; i++) { 148 unsigned long mstart, mend; 149 unsigned long j; 150 151 mstart = image->segment[i].mem; 152 mend = mstart + image->segment[i].memsz; 153 for (j = 0; j < i; j++) { 154 unsigned long pstart, pend; 155 156 pstart = image->segment[j].mem; 157 pend = pstart + image->segment[j].memsz; 158 /* Do the segments overlap ? */ 159 if ((mend > pstart) && (mstart < pend)) 160 return -EINVAL; 161 } 162 } 163 164 /* Ensure our buffer sizes are strictly less than 165 * our memory sizes. This should always be the case, 166 * and it is easier to check up front than to be surprised 167 * later on. 168 */ 169 for (i = 0; i < nr_segments; i++) { 170 if (image->segment[i].bufsz > image->segment[i].memsz) 171 return -EINVAL; 172 } 173 174 /* 175 * Verify that no more than half of memory will be consumed. If the 176 * request from userspace is too large, a large amount of time will be 177 * wasted allocating pages, which can cause a soft lockup. 178 */ 179 for (i = 0; i < nr_segments; i++) { 180 if (PAGE_COUNT(image->segment[i].memsz) > nr_pages / 2) 181 return -EINVAL; 182 183 total_pages += PAGE_COUNT(image->segment[i].memsz); 184 } 185 186 if (total_pages > nr_pages / 2) 187 return -EINVAL; 188 189 #ifdef CONFIG_CRASH_DUMP 190 /* 191 * Verify we have good destination addresses. Normally 192 * the caller is responsible for making certain we don't 193 * attempt to load the new image into invalid or reserved 194 * areas of RAM. But crash kernels are preloaded into a 195 * reserved area of ram. We must ensure the addresses 196 * are in the reserved area otherwise preloading the 197 * kernel could corrupt things. 198 */ 199 200 if (image->type == KEXEC_TYPE_CRASH) { 201 for (i = 0; i < nr_segments; i++) { 202 unsigned long mstart, mend; 203 204 mstart = image->segment[i].mem; 205 mend = mstart + image->segment[i].memsz - 1; 206 /* Ensure we are within the crash kernel limits */ 207 if ((mstart < phys_to_boot_phys(crashk_res.start)) || 208 (mend > phys_to_boot_phys(crashk_res.end))) 209 return -EADDRNOTAVAIL; 210 } 211 } 212 #endif 213 214 /* 215 * The destination addresses are searched from system RAM rather than 216 * being allocated from the buddy allocator, so they are not guaranteed 217 * to be accepted by the current kernel. Accept the destination 218 * addresses before kexec swaps their content with the segments' source 219 * pages to avoid accessing memory before it is accepted. 220 */ 221 for (i = 0; i < nr_segments; i++) 222 accept_memory(image->segment[i].mem, image->segment[i].memsz); 223 224 return 0; 225 } 226 227 struct kimage *do_kimage_alloc_init(void) 228 { 229 struct kimage *image; 230 231 /* Allocate a controlling structure */ 232 image = kzalloc(sizeof(*image), GFP_KERNEL); 233 if (!image) 234 return NULL; 235 236 image->entry = &image->head; 237 image->last_entry = &image->head; 238 image->control_page = ~0; /* By default this does not apply */ 239 image->type = KEXEC_TYPE_DEFAULT; 240 241 /* Initialize the list of control pages */ 242 INIT_LIST_HEAD(&image->control_pages); 243 244 /* Initialize the list of destination pages */ 245 INIT_LIST_HEAD(&image->dest_pages); 246 247 /* Initialize the list of unusable pages */ 248 INIT_LIST_HEAD(&image->unusable_pages); 249 250 #ifdef CONFIG_CRASH_HOTPLUG 251 image->hp_action = KEXEC_CRASH_HP_NONE; 252 image->elfcorehdr_index = -1; 253 image->elfcorehdr_updated = false; 254 #endif 255 256 return image; 257 } 258 259 int kimage_is_destination_range(struct kimage *image, 260 unsigned long start, 261 unsigned long end) 262 { 263 unsigned long i; 264 265 for (i = 0; i < image->nr_segments; i++) { 266 unsigned long mstart, mend; 267 268 mstart = image->segment[i].mem; 269 mend = mstart + image->segment[i].memsz - 1; 270 if ((end >= mstart) && (start <= mend)) 271 return 1; 272 } 273 274 return 0; 275 } 276 277 static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order) 278 { 279 struct page *pages; 280 281 if (fatal_signal_pending(current)) 282 return NULL; 283 pages = alloc_pages(gfp_mask & ~__GFP_ZERO, order); 284 if (pages) { 285 unsigned int count, i; 286 287 pages->mapping = NULL; 288 set_page_private(pages, order); 289 count = 1 << order; 290 for (i = 0; i < count; i++) 291 SetPageReserved(pages + i); 292 293 arch_kexec_post_alloc_pages(page_address(pages), count, 294 gfp_mask); 295 296 if (gfp_mask & __GFP_ZERO) 297 for (i = 0; i < count; i++) 298 clear_highpage(pages + i); 299 } 300 301 return pages; 302 } 303 304 static void kimage_free_pages(struct page *page) 305 { 306 unsigned int order, count, i; 307 308 order = page_private(page); 309 count = 1 << order; 310 311 arch_kexec_pre_free_pages(page_address(page), count); 312 313 for (i = 0; i < count; i++) 314 ClearPageReserved(page + i); 315 __free_pages(page, order); 316 } 317 318 void kimage_free_page_list(struct list_head *list) 319 { 320 struct page *page, *next; 321 322 list_for_each_entry_safe(page, next, list, lru) { 323 list_del(&page->lru); 324 kimage_free_pages(page); 325 } 326 } 327 328 static struct page *kimage_alloc_normal_control_pages(struct kimage *image, 329 unsigned int order) 330 { 331 /* Control pages are special, they are the intermediaries 332 * that are needed while we copy the rest of the pages 333 * to their final resting place. As such they must 334 * not conflict with either the destination addresses 335 * or memory the kernel is already using. 336 * 337 * The only case where we really need more than one of 338 * these are for architectures where we cannot disable 339 * the MMU and must instead generate an identity mapped 340 * page table for all of the memory. 341 * 342 * At worst this runs in O(N) of the image size. 343 */ 344 struct list_head extra_pages; 345 struct page *pages; 346 unsigned int count; 347 348 count = 1 << order; 349 INIT_LIST_HEAD(&extra_pages); 350 351 /* Loop while I can allocate a page and the page allocated 352 * is a destination page. 353 */ 354 do { 355 unsigned long pfn, epfn, addr, eaddr; 356 357 pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order); 358 if (!pages) 359 break; 360 pfn = page_to_boot_pfn(pages); 361 epfn = pfn + count; 362 addr = pfn << PAGE_SHIFT; 363 eaddr = (epfn << PAGE_SHIFT) - 1; 364 if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) || 365 kimage_is_destination_range(image, addr, eaddr)) { 366 list_add(&pages->lru, &extra_pages); 367 pages = NULL; 368 } 369 } while (!pages); 370 371 if (pages) { 372 /* Remember the allocated page... */ 373 list_add(&pages->lru, &image->control_pages); 374 375 /* Because the page is already in it's destination 376 * location we will never allocate another page at 377 * that address. Therefore kimage_alloc_pages 378 * will not return it (again) and we don't need 379 * to give it an entry in image->segment[]. 380 */ 381 } 382 /* Deal with the destination pages I have inadvertently allocated. 383 * 384 * Ideally I would convert multi-page allocations into single 385 * page allocations, and add everything to image->dest_pages. 386 * 387 * For now it is simpler to just free the pages. 388 */ 389 kimage_free_page_list(&extra_pages); 390 391 return pages; 392 } 393 394 #ifdef CONFIG_CRASH_DUMP 395 static struct page *kimage_alloc_crash_control_pages(struct kimage *image, 396 unsigned int order) 397 { 398 /* Control pages are special, they are the intermediaries 399 * that are needed while we copy the rest of the pages 400 * to their final resting place. As such they must 401 * not conflict with either the destination addresses 402 * or memory the kernel is already using. 403 * 404 * Control pages are also the only pags we must allocate 405 * when loading a crash kernel. All of the other pages 406 * are specified by the segments and we just memcpy 407 * into them directly. 408 * 409 * The only case where we really need more than one of 410 * these are for architectures where we cannot disable 411 * the MMU and must instead generate an identity mapped 412 * page table for all of the memory. 413 * 414 * Given the low demand this implements a very simple 415 * allocator that finds the first hole of the appropriate 416 * size in the reserved memory region, and allocates all 417 * of the memory up to and including the hole. 418 */ 419 unsigned long hole_start, hole_end, size; 420 struct page *pages; 421 422 pages = NULL; 423 size = (1 << order) << PAGE_SHIFT; 424 hole_start = ALIGN(image->control_page, size); 425 hole_end = hole_start + size - 1; 426 while (hole_end <= crashk_res.end) { 427 unsigned long i; 428 429 cond_resched(); 430 431 if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT) 432 break; 433 /* See if I overlap any of the segments */ 434 for (i = 0; i < image->nr_segments; i++) { 435 unsigned long mstart, mend; 436 437 mstart = image->segment[i].mem; 438 mend = mstart + image->segment[i].memsz - 1; 439 if ((hole_end >= mstart) && (hole_start <= mend)) { 440 /* Advance the hole to the end of the segment */ 441 hole_start = ALIGN(mend, size); 442 hole_end = hole_start + size - 1; 443 break; 444 } 445 } 446 /* If I don't overlap any segments I have found my hole! */ 447 if (i == image->nr_segments) { 448 pages = pfn_to_page(hole_start >> PAGE_SHIFT); 449 image->control_page = hole_end + 1; 450 break; 451 } 452 } 453 454 /* Ensure that these pages are decrypted if SME is enabled. */ 455 if (pages) 456 arch_kexec_post_alloc_pages(page_address(pages), 1 << order, 0); 457 458 return pages; 459 } 460 #endif 461 462 463 struct page *kimage_alloc_control_pages(struct kimage *image, 464 unsigned int order) 465 { 466 struct page *pages = NULL; 467 468 switch (image->type) { 469 case KEXEC_TYPE_DEFAULT: 470 pages = kimage_alloc_normal_control_pages(image, order); 471 break; 472 #ifdef CONFIG_CRASH_DUMP 473 case KEXEC_TYPE_CRASH: 474 pages = kimage_alloc_crash_control_pages(image, order); 475 break; 476 #endif 477 } 478 479 return pages; 480 } 481 482 static int kimage_add_entry(struct kimage *image, kimage_entry_t entry) 483 { 484 if (*image->entry != 0) 485 image->entry++; 486 487 if (image->entry == image->last_entry) { 488 kimage_entry_t *ind_page; 489 struct page *page; 490 491 page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST); 492 if (!page) 493 return -ENOMEM; 494 495 ind_page = page_address(page); 496 *image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION; 497 image->entry = ind_page; 498 image->last_entry = ind_page + 499 ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1); 500 } 501 *image->entry = entry; 502 image->entry++; 503 *image->entry = 0; 504 505 return 0; 506 } 507 508 static int kimage_set_destination(struct kimage *image, 509 unsigned long destination) 510 { 511 destination &= PAGE_MASK; 512 513 return kimage_add_entry(image, destination | IND_DESTINATION); 514 } 515 516 517 static int kimage_add_page(struct kimage *image, unsigned long page) 518 { 519 page &= PAGE_MASK; 520 521 return kimage_add_entry(image, page | IND_SOURCE); 522 } 523 524 525 static void kimage_free_extra_pages(struct kimage *image) 526 { 527 /* Walk through and free any extra destination pages I may have */ 528 kimage_free_page_list(&image->dest_pages); 529 530 /* Walk through and free any unusable pages I have cached */ 531 kimage_free_page_list(&image->unusable_pages); 532 533 } 534 535 void kimage_terminate(struct kimage *image) 536 { 537 if (*image->entry != 0) 538 image->entry++; 539 540 *image->entry = IND_DONE; 541 } 542 543 #define for_each_kimage_entry(image, ptr, entry) \ 544 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \ 545 ptr = (entry & IND_INDIRECTION) ? \ 546 boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1) 547 548 static void kimage_free_entry(kimage_entry_t entry) 549 { 550 struct page *page; 551 552 page = boot_pfn_to_page(entry >> PAGE_SHIFT); 553 kimage_free_pages(page); 554 } 555 556 static void kimage_free_cma(struct kimage *image) 557 { 558 unsigned long i; 559 560 for (i = 0; i < image->nr_segments; i++) { 561 struct page *cma = image->segment_cma[i]; 562 u32 nr_pages = image->segment[i].memsz >> PAGE_SHIFT; 563 564 if (!cma) 565 continue; 566 567 arch_kexec_pre_free_pages(page_address(cma), nr_pages); 568 dma_release_from_contiguous(NULL, cma, nr_pages); 569 image->segment_cma[i] = NULL; 570 } 571 572 } 573 574 void kimage_free(struct kimage *image) 575 { 576 kimage_entry_t *ptr, entry; 577 kimage_entry_t ind = 0; 578 579 if (!image) 580 return; 581 582 #ifdef CONFIG_CRASH_DUMP 583 if (image->vmcoreinfo_data_copy) { 584 crash_update_vmcoreinfo_safecopy(NULL); 585 vunmap(image->vmcoreinfo_data_copy); 586 } 587 #endif 588 589 kimage_free_extra_pages(image); 590 for_each_kimage_entry(image, ptr, entry) { 591 if (entry & IND_INDIRECTION) { 592 /* Free the previous indirection page */ 593 if (ind & IND_INDIRECTION) 594 kimage_free_entry(ind); 595 /* Save this indirection page until we are 596 * done with it. 597 */ 598 ind = entry; 599 } else if (entry & IND_SOURCE) 600 kimage_free_entry(entry); 601 } 602 /* Free the final indirection page */ 603 if (ind & IND_INDIRECTION) 604 kimage_free_entry(ind); 605 606 /* Handle any machine specific cleanup */ 607 machine_kexec_cleanup(image); 608 609 /* Free the kexec control pages... */ 610 kimage_free_page_list(&image->control_pages); 611 612 /* Free CMA allocations */ 613 kimage_free_cma(image); 614 615 /* 616 * Free up any temporary buffers allocated. This might hit if 617 * error occurred much later after buffer allocation. 618 */ 619 if (image->file_mode) 620 kimage_file_post_load_cleanup(image); 621 622 kfree(image); 623 } 624 625 static kimage_entry_t *kimage_dst_used(struct kimage *image, 626 unsigned long page) 627 { 628 kimage_entry_t *ptr, entry; 629 unsigned long destination = 0; 630 631 for_each_kimage_entry(image, ptr, entry) { 632 if (entry & IND_DESTINATION) 633 destination = entry & PAGE_MASK; 634 else if (entry & IND_SOURCE) { 635 if (page == destination) 636 return ptr; 637 destination += PAGE_SIZE; 638 } 639 } 640 641 return NULL; 642 } 643 644 static struct page *kimage_alloc_page(struct kimage *image, 645 gfp_t gfp_mask, 646 unsigned long destination) 647 { 648 /* 649 * Here we implement safeguards to ensure that a source page 650 * is not copied to its destination page before the data on 651 * the destination page is no longer useful. 652 * 653 * To do this we maintain the invariant that a source page is 654 * either its own destination page, or it is not a 655 * destination page at all. 656 * 657 * That is slightly stronger than required, but the proof 658 * that no problems will not occur is trivial, and the 659 * implementation is simply to verify. 660 * 661 * When allocating all pages normally this algorithm will run 662 * in O(N) time, but in the worst case it will run in O(N^2) 663 * time. If the runtime is a problem the data structures can 664 * be fixed. 665 */ 666 struct page *page; 667 unsigned long addr; 668 669 /* 670 * Walk through the list of destination pages, and see if I 671 * have a match. 672 */ 673 list_for_each_entry(page, &image->dest_pages, lru) { 674 addr = page_to_boot_pfn(page) << PAGE_SHIFT; 675 if (addr == destination) { 676 list_del(&page->lru); 677 return page; 678 } 679 } 680 page = NULL; 681 while (1) { 682 kimage_entry_t *old; 683 684 /* Allocate a page, if we run out of memory give up */ 685 page = kimage_alloc_pages(gfp_mask, 0); 686 if (!page) 687 return NULL; 688 /* If the page cannot be used file it away */ 689 if (page_to_boot_pfn(page) > 690 (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) { 691 list_add(&page->lru, &image->unusable_pages); 692 continue; 693 } 694 addr = page_to_boot_pfn(page) << PAGE_SHIFT; 695 696 /* If it is the destination page we want use it */ 697 if (addr == destination) 698 break; 699 700 /* If the page is not a destination page use it */ 701 if (!kimage_is_destination_range(image, addr, 702 addr + PAGE_SIZE - 1)) 703 break; 704 705 /* 706 * I know that the page is someones destination page. 707 * See if there is already a source page for this 708 * destination page. And if so swap the source pages. 709 */ 710 old = kimage_dst_used(image, addr); 711 if (old) { 712 /* If so move it */ 713 unsigned long old_addr; 714 struct page *old_page; 715 716 old_addr = *old & PAGE_MASK; 717 old_page = boot_pfn_to_page(old_addr >> PAGE_SHIFT); 718 copy_highpage(page, old_page); 719 *old = addr | (*old & ~PAGE_MASK); 720 721 /* The old page I have found cannot be a 722 * destination page, so return it if it's 723 * gfp_flags honor the ones passed in. 724 */ 725 if (!(gfp_mask & __GFP_HIGHMEM) && 726 PageHighMem(old_page)) { 727 kimage_free_pages(old_page); 728 continue; 729 } 730 page = old_page; 731 break; 732 } 733 /* Place the page on the destination list, to be used later */ 734 list_add(&page->lru, &image->dest_pages); 735 } 736 737 return page; 738 } 739 740 static int kimage_load_cma_segment(struct kimage *image, int idx) 741 { 742 struct kexec_segment *segment = &image->segment[idx]; 743 struct page *cma = image->segment_cma[idx]; 744 char *ptr = page_address(cma); 745 size_t ubytes, mbytes; 746 int result = 0; 747 unsigned char __user *buf = NULL; 748 unsigned char *kbuf = NULL; 749 750 if (image->file_mode) 751 kbuf = segment->kbuf; 752 else 753 buf = segment->buf; 754 ubytes = segment->bufsz; 755 mbytes = segment->memsz; 756 757 /* Then copy from source buffer to the CMA one */ 758 while (mbytes) { 759 size_t uchunk, mchunk; 760 761 mchunk = min_t(size_t, mbytes, PAGE_SIZE); 762 uchunk = min(ubytes, mchunk); 763 764 if (uchunk) { 765 /* For file based kexec, source pages are in kernel memory */ 766 if (image->file_mode) 767 memcpy(ptr, kbuf, uchunk); 768 else 769 result = copy_from_user(ptr, buf, uchunk); 770 ubytes -= uchunk; 771 if (image->file_mode) 772 kbuf += uchunk; 773 else 774 buf += uchunk; 775 } 776 777 if (result) { 778 result = -EFAULT; 779 goto out; 780 } 781 782 ptr += mchunk; 783 mbytes -= mchunk; 784 785 cond_resched(); 786 } 787 788 /* Clear any remainder */ 789 memset(ptr, 0, mbytes); 790 791 out: 792 return result; 793 } 794 795 static int kimage_load_normal_segment(struct kimage *image, int idx) 796 { 797 struct kexec_segment *segment = &image->segment[idx]; 798 unsigned long maddr; 799 size_t ubytes, mbytes; 800 int result; 801 unsigned char __user *buf = NULL; 802 unsigned char *kbuf = NULL; 803 804 if (image->file_mode) 805 kbuf = segment->kbuf; 806 else 807 buf = segment->buf; 808 ubytes = segment->bufsz; 809 mbytes = segment->memsz; 810 maddr = segment->mem; 811 812 if (image->segment_cma[idx]) 813 return kimage_load_cma_segment(image, idx); 814 815 result = kimage_set_destination(image, maddr); 816 if (result < 0) 817 goto out; 818 819 while (mbytes) { 820 struct page *page; 821 char *ptr; 822 size_t uchunk, mchunk; 823 824 page = kimage_alloc_page(image, GFP_HIGHUSER, maddr); 825 if (!page) { 826 result = -ENOMEM; 827 goto out; 828 } 829 result = kimage_add_page(image, page_to_boot_pfn(page) 830 << PAGE_SHIFT); 831 if (result < 0) 832 goto out; 833 834 ptr = kmap_local_page(page); 835 /* Start with a clear page */ 836 clear_page(ptr); 837 mchunk = min_t(size_t, mbytes, PAGE_SIZE); 838 uchunk = min(ubytes, mchunk); 839 840 if (uchunk) { 841 /* For file based kexec, source pages are in kernel memory */ 842 if (image->file_mode) 843 memcpy(ptr, kbuf, uchunk); 844 else 845 result = copy_from_user(ptr, buf, uchunk); 846 ubytes -= uchunk; 847 if (image->file_mode) 848 kbuf += uchunk; 849 else 850 buf += uchunk; 851 } 852 kunmap_local(ptr); 853 if (result) { 854 result = -EFAULT; 855 goto out; 856 } 857 maddr += mchunk; 858 mbytes -= mchunk; 859 860 cond_resched(); 861 } 862 out: 863 return result; 864 } 865 866 #ifdef CONFIG_CRASH_DUMP 867 static int kimage_load_crash_segment(struct kimage *image, int idx) 868 { 869 /* For crash dumps kernels we simply copy the data from 870 * user space to it's destination. 871 * We do things a page at a time for the sake of kmap. 872 */ 873 struct kexec_segment *segment = &image->segment[idx]; 874 unsigned long maddr; 875 size_t ubytes, mbytes; 876 int result; 877 unsigned char __user *buf = NULL; 878 unsigned char *kbuf = NULL; 879 880 result = 0; 881 if (image->file_mode) 882 kbuf = segment->kbuf; 883 else 884 buf = segment->buf; 885 ubytes = segment->bufsz; 886 mbytes = segment->memsz; 887 maddr = segment->mem; 888 while (mbytes) { 889 struct page *page; 890 char *ptr; 891 size_t uchunk, mchunk; 892 893 page = boot_pfn_to_page(maddr >> PAGE_SHIFT); 894 if (!page) { 895 result = -ENOMEM; 896 goto out; 897 } 898 arch_kexec_post_alloc_pages(page_address(page), 1, 0); 899 ptr = kmap_local_page(page); 900 mchunk = min_t(size_t, mbytes, PAGE_SIZE); 901 uchunk = min(ubytes, mchunk); 902 if (mchunk > uchunk) { 903 /* Zero the trailing part of the page */ 904 memset(ptr + uchunk, 0, mchunk - uchunk); 905 } 906 907 if (uchunk) { 908 /* For file based kexec, source pages are in kernel memory */ 909 if (image->file_mode) 910 memcpy(ptr, kbuf, uchunk); 911 else 912 result = copy_from_user(ptr, buf, uchunk); 913 ubytes -= uchunk; 914 if (image->file_mode) 915 kbuf += uchunk; 916 else 917 buf += uchunk; 918 } 919 kexec_flush_icache_page(page); 920 kunmap_local(ptr); 921 arch_kexec_pre_free_pages(page_address(page), 1); 922 if (result) { 923 result = -EFAULT; 924 goto out; 925 } 926 maddr += mchunk; 927 mbytes -= mchunk; 928 929 cond_resched(); 930 } 931 out: 932 return result; 933 } 934 #endif 935 936 int kimage_load_segment(struct kimage *image, int idx) 937 { 938 int result = -ENOMEM; 939 940 switch (image->type) { 941 case KEXEC_TYPE_DEFAULT: 942 result = kimage_load_normal_segment(image, idx); 943 break; 944 #ifdef CONFIG_CRASH_DUMP 945 case KEXEC_TYPE_CRASH: 946 result = kimage_load_crash_segment(image, idx); 947 break; 948 #endif 949 } 950 951 return result; 952 } 953 954 void *kimage_map_segment(struct kimage *image, 955 unsigned long addr, unsigned long size) 956 { 957 unsigned long src_page_addr, dest_page_addr = 0; 958 unsigned long eaddr = addr + size; 959 kimage_entry_t *ptr, entry; 960 struct page **src_pages; 961 unsigned int npages; 962 void *vaddr = NULL; 963 int i; 964 965 /* 966 * Collect the source pages and map them in a contiguous VA range. 967 */ 968 npages = PFN_UP(eaddr) - PFN_DOWN(addr); 969 src_pages = kmalloc_array(npages, sizeof(*src_pages), GFP_KERNEL); 970 if (!src_pages) { 971 pr_err("Could not allocate ima pages array.\n"); 972 return NULL; 973 } 974 975 i = 0; 976 for_each_kimage_entry(image, ptr, entry) { 977 if (entry & IND_DESTINATION) { 978 dest_page_addr = entry & PAGE_MASK; 979 } else if (entry & IND_SOURCE) { 980 if (dest_page_addr >= addr && dest_page_addr < eaddr) { 981 src_page_addr = entry & PAGE_MASK; 982 src_pages[i++] = 983 virt_to_page(__va(src_page_addr)); 984 if (i == npages) 985 break; 986 dest_page_addr += PAGE_SIZE; 987 } 988 } 989 } 990 991 /* Sanity check. */ 992 WARN_ON(i < npages); 993 994 vaddr = vmap(src_pages, npages, VM_MAP, PAGE_KERNEL); 995 kfree(src_pages); 996 997 if (!vaddr) 998 pr_err("Could not map ima buffer.\n"); 999 1000 return vaddr; 1001 } 1002 1003 void kimage_unmap_segment(void *segment_buffer) 1004 { 1005 vunmap(segment_buffer); 1006 } 1007 1008 struct kexec_load_limit { 1009 /* Mutex protects the limit count. */ 1010 struct mutex mutex; 1011 int limit; 1012 }; 1013 1014 static struct kexec_load_limit load_limit_reboot = { 1015 .mutex = __MUTEX_INITIALIZER(load_limit_reboot.mutex), 1016 .limit = -1, 1017 }; 1018 1019 static struct kexec_load_limit load_limit_panic = { 1020 .mutex = __MUTEX_INITIALIZER(load_limit_panic.mutex), 1021 .limit = -1, 1022 }; 1023 1024 struct kimage *kexec_image; 1025 struct kimage *kexec_crash_image; 1026 static int kexec_load_disabled; 1027 1028 #ifdef CONFIG_SYSCTL 1029 static int kexec_limit_handler(const struct ctl_table *table, int write, 1030 void *buffer, size_t *lenp, loff_t *ppos) 1031 { 1032 struct kexec_load_limit *limit = table->data; 1033 int val; 1034 struct ctl_table tmp = { 1035 .data = &val, 1036 .maxlen = sizeof(val), 1037 .mode = table->mode, 1038 }; 1039 int ret; 1040 1041 if (write) { 1042 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos); 1043 if (ret) 1044 return ret; 1045 1046 if (val < 0) 1047 return -EINVAL; 1048 1049 mutex_lock(&limit->mutex); 1050 if (limit->limit != -1 && val >= limit->limit) 1051 ret = -EINVAL; 1052 else 1053 limit->limit = val; 1054 mutex_unlock(&limit->mutex); 1055 1056 return ret; 1057 } 1058 1059 mutex_lock(&limit->mutex); 1060 val = limit->limit; 1061 mutex_unlock(&limit->mutex); 1062 1063 return proc_dointvec(&tmp, write, buffer, lenp, ppos); 1064 } 1065 1066 static const struct ctl_table kexec_core_sysctls[] = { 1067 { 1068 .procname = "kexec_load_disabled", 1069 .data = &kexec_load_disabled, 1070 .maxlen = sizeof(int), 1071 .mode = 0644, 1072 /* only handle a transition from default "0" to "1" */ 1073 .proc_handler = proc_dointvec_minmax, 1074 .extra1 = SYSCTL_ONE, 1075 .extra2 = SYSCTL_ONE, 1076 }, 1077 { 1078 .procname = "kexec_load_limit_panic", 1079 .data = &load_limit_panic, 1080 .mode = 0644, 1081 .proc_handler = kexec_limit_handler, 1082 }, 1083 { 1084 .procname = "kexec_load_limit_reboot", 1085 .data = &load_limit_reboot, 1086 .mode = 0644, 1087 .proc_handler = kexec_limit_handler, 1088 }, 1089 }; 1090 1091 static int __init kexec_core_sysctl_init(void) 1092 { 1093 register_sysctl_init("kernel", kexec_core_sysctls); 1094 return 0; 1095 } 1096 late_initcall(kexec_core_sysctl_init); 1097 #endif 1098 1099 bool kexec_load_permitted(int kexec_image_type) 1100 { 1101 struct kexec_load_limit *limit; 1102 1103 /* 1104 * Only the superuser can use the kexec syscall and if it has not 1105 * been disabled. 1106 */ 1107 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) 1108 return false; 1109 1110 /* Check limit counter and decrease it.*/ 1111 limit = (kexec_image_type == KEXEC_TYPE_CRASH) ? 1112 &load_limit_panic : &load_limit_reboot; 1113 mutex_lock(&limit->mutex); 1114 if (!limit->limit) { 1115 mutex_unlock(&limit->mutex); 1116 return false; 1117 } 1118 if (limit->limit != -1) 1119 limit->limit--; 1120 mutex_unlock(&limit->mutex); 1121 1122 return true; 1123 } 1124 1125 /* 1126 * Move into place and start executing a preloaded standalone 1127 * executable. If nothing was preloaded return an error. 1128 */ 1129 int kernel_kexec(void) 1130 { 1131 int error = 0; 1132 1133 if (!kexec_trylock()) 1134 return -EBUSY; 1135 if (!kexec_image) { 1136 error = -EINVAL; 1137 goto Unlock; 1138 } 1139 1140 #ifdef CONFIG_KEXEC_JUMP 1141 if (kexec_image->preserve_context) { 1142 /* 1143 * This flow is analogous to hibernation flows that occur 1144 * before creating an image and before jumping from the 1145 * restore kernel to the image one, so it uses the same 1146 * device callbacks as those two flows. 1147 */ 1148 pm_prepare_console(); 1149 error = freeze_processes(); 1150 if (error) { 1151 error = -EBUSY; 1152 goto Restore_console; 1153 } 1154 console_suspend_all(); 1155 error = dpm_suspend_start(PMSG_FREEZE); 1156 if (error) 1157 goto Resume_devices; 1158 /* 1159 * dpm_suspend_end() must be called after dpm_suspend_start() 1160 * to complete the transition, like in the hibernation flows 1161 * mentioned above. 1162 */ 1163 error = dpm_suspend_end(PMSG_FREEZE); 1164 if (error) 1165 goto Resume_devices; 1166 error = suspend_disable_secondary_cpus(); 1167 if (error) 1168 goto Enable_cpus; 1169 local_irq_disable(); 1170 error = syscore_suspend(); 1171 if (error) 1172 goto Enable_irqs; 1173 } else 1174 #endif 1175 { 1176 kexec_in_progress = true; 1177 kernel_restart_prepare("kexec reboot"); 1178 migrate_to_reboot_cpu(); 1179 syscore_shutdown(); 1180 1181 /* 1182 * migrate_to_reboot_cpu() disables CPU hotplug assuming that 1183 * no further code needs to use CPU hotplug (which is true in 1184 * the reboot case). However, the kexec path depends on using 1185 * CPU hotplug again; so re-enable it here. 1186 */ 1187 cpu_hotplug_enable(); 1188 pr_notice("Starting new kernel\n"); 1189 machine_shutdown(); 1190 } 1191 1192 kmsg_dump(KMSG_DUMP_SHUTDOWN); 1193 machine_kexec(kexec_image); 1194 1195 #ifdef CONFIG_KEXEC_JUMP 1196 if (kexec_image->preserve_context) { 1197 /* 1198 * This flow is analogous to hibernation flows that occur after 1199 * creating an image and after the image kernel has got control 1200 * back, and in case the devices have been reset or otherwise 1201 * manipulated in the meantime, it uses the device callbacks 1202 * used by the latter. 1203 */ 1204 syscore_resume(); 1205 Enable_irqs: 1206 local_irq_enable(); 1207 Enable_cpus: 1208 suspend_enable_secondary_cpus(); 1209 dpm_resume_start(PMSG_RESTORE); 1210 Resume_devices: 1211 dpm_resume_end(PMSG_RESTORE); 1212 console_resume_all(); 1213 thaw_processes(); 1214 Restore_console: 1215 pm_restore_console(); 1216 } 1217 #endif 1218 1219 Unlock: 1220 kexec_unlock(); 1221 return error; 1222 } 1223