1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kexec_handover.c - kexec handover metadata processing 4 * Copyright (C) 2023 Alexander Graf <graf@amazon.com> 5 * Copyright (C) 2025 Microsoft Corporation, Mike Rapoport <rppt@kernel.org> 6 * Copyright (C) 2025 Google LLC, Changyuan Lyu <changyuanl@google.com> 7 */ 8 9 #define pr_fmt(fmt) "KHO: " fmt 10 11 #include <linux/cleanup.h> 12 #include <linux/cma.h> 13 #include <linux/count_zeros.h> 14 #include <linux/debugfs.h> 15 #include <linux/kexec.h> 16 #include <linux/kexec_handover.h> 17 #include <linux/libfdt.h> 18 #include <linux/list.h> 19 #include <linux/memblock.h> 20 #include <linux/notifier.h> 21 #include <linux/page-isolation.h> 22 #include <linux/vmalloc.h> 23 24 #include <asm/early_ioremap.h> 25 26 #include "kexec_handover_internal.h" 27 /* 28 * KHO is tightly coupled with mm init and needs access to some of mm 29 * internal APIs. 30 */ 31 #include "../mm/internal.h" 32 #include "kexec_internal.h" 33 34 #define KHO_FDT_COMPATIBLE "kho-v1" 35 #define PROP_PRESERVED_MEMORY_MAP "preserved-memory-map" 36 #define PROP_SUB_FDT "fdt" 37 38 #define KHO_PAGE_MAGIC 0x4b484f50U /* ASCII for 'KHOP' */ 39 40 /* 41 * KHO uses page->private, which is an unsigned long, to store page metadata. 42 * Use it to store both the magic and the order. 43 */ 44 union kho_page_info { 45 unsigned long page_private; 46 struct { 47 unsigned int order; 48 unsigned int magic; 49 }; 50 }; 51 52 static_assert(sizeof(union kho_page_info) == sizeof(((struct page *)0)->private)); 53 54 static bool kho_enable __ro_after_init; 55 56 bool kho_is_enabled(void) 57 { 58 return kho_enable; 59 } 60 EXPORT_SYMBOL_GPL(kho_is_enabled); 61 62 static int __init kho_parse_enable(char *p) 63 { 64 return kstrtobool(p, &kho_enable); 65 } 66 early_param("kho", kho_parse_enable); 67 68 /* 69 * Keep track of memory that is to be preserved across KHO. 70 * 71 * The serializing side uses two levels of xarrays to manage chunks of per-order 72 * PAGE_SIZE byte bitmaps. For instance if PAGE_SIZE = 4096, the entire 1G order 73 * of a 8TB system would fit inside a single 4096 byte bitmap. For order 0 74 * allocations each bitmap will cover 128M of address space. Thus, for 16G of 75 * memory at most 512K of bitmap memory will be needed for order 0. 76 * 77 * This approach is fully incremental, as the serialization progresses folios 78 * can continue be aggregated to the tracker. The final step, immediately prior 79 * to kexec would serialize the xarray information into a linked list for the 80 * successor kernel to parse. 81 */ 82 83 #define PRESERVE_BITS (PAGE_SIZE * 8) 84 85 struct kho_mem_phys_bits { 86 DECLARE_BITMAP(preserve, PRESERVE_BITS); 87 }; 88 89 static_assert(sizeof(struct kho_mem_phys_bits) == PAGE_SIZE); 90 91 struct kho_mem_phys { 92 /* 93 * Points to kho_mem_phys_bits, a sparse bitmap array. Each bit is sized 94 * to order. 95 */ 96 struct xarray phys_bits; 97 }; 98 99 struct kho_mem_track { 100 /* Points to kho_mem_phys, each order gets its own bitmap tree */ 101 struct xarray orders; 102 }; 103 104 struct khoser_mem_chunk; 105 106 struct kho_serialization { 107 struct page *fdt; 108 struct list_head fdt_list; 109 struct dentry *sub_fdt_dir; 110 struct kho_mem_track track; 111 /* First chunk of serialized preserved memory map */ 112 struct khoser_mem_chunk *preserved_mem_map; 113 }; 114 115 struct kho_out { 116 struct blocking_notifier_head chain_head; 117 118 struct dentry *dir; 119 120 struct mutex lock; /* protects KHO FDT finalization */ 121 122 struct kho_serialization ser; 123 bool finalized; 124 }; 125 126 static struct kho_out kho_out = { 127 .chain_head = BLOCKING_NOTIFIER_INIT(kho_out.chain_head), 128 .lock = __MUTEX_INITIALIZER(kho_out.lock), 129 .ser = { 130 .fdt_list = LIST_HEAD_INIT(kho_out.ser.fdt_list), 131 .track = { 132 .orders = XARRAY_INIT(kho_out.ser.track.orders, 0), 133 }, 134 }, 135 .finalized = false, 136 }; 137 138 static void *xa_load_or_alloc(struct xarray *xa, unsigned long index) 139 { 140 void *res = xa_load(xa, index); 141 142 if (res) 143 return res; 144 145 void *elm __free(free_page) = (void *)get_zeroed_page(GFP_KERNEL); 146 147 if (!elm) 148 return ERR_PTR(-ENOMEM); 149 150 if (WARN_ON(kho_scratch_overlap(virt_to_phys(elm), PAGE_SIZE))) 151 return ERR_PTR(-EINVAL); 152 153 res = xa_cmpxchg(xa, index, NULL, elm, GFP_KERNEL); 154 if (xa_is_err(res)) 155 return ERR_PTR(xa_err(res)); 156 else if (res) 157 return res; 158 159 return no_free_ptr(elm); 160 } 161 162 static void __kho_unpreserve(struct kho_mem_track *track, unsigned long pfn, 163 unsigned long end_pfn) 164 { 165 struct kho_mem_phys_bits *bits; 166 struct kho_mem_phys *physxa; 167 168 while (pfn < end_pfn) { 169 const unsigned int order = 170 min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); 171 const unsigned long pfn_high = pfn >> order; 172 173 physxa = xa_load(&track->orders, order); 174 if (!physxa) 175 continue; 176 177 bits = xa_load(&physxa->phys_bits, pfn_high / PRESERVE_BITS); 178 if (!bits) 179 continue; 180 181 clear_bit(pfn_high % PRESERVE_BITS, bits->preserve); 182 183 pfn += 1 << order; 184 } 185 } 186 187 static int __kho_preserve_order(struct kho_mem_track *track, unsigned long pfn, 188 unsigned int order) 189 { 190 struct kho_mem_phys_bits *bits; 191 struct kho_mem_phys *physxa, *new_physxa; 192 const unsigned long pfn_high = pfn >> order; 193 194 might_sleep(); 195 196 if (kho_out.finalized) 197 return -EBUSY; 198 199 physxa = xa_load(&track->orders, order); 200 if (!physxa) { 201 int err; 202 203 new_physxa = kzalloc(sizeof(*physxa), GFP_KERNEL); 204 if (!new_physxa) 205 return -ENOMEM; 206 207 xa_init(&new_physxa->phys_bits); 208 physxa = xa_cmpxchg(&track->orders, order, NULL, new_physxa, 209 GFP_KERNEL); 210 211 err = xa_err(physxa); 212 if (err || physxa) { 213 xa_destroy(&new_physxa->phys_bits); 214 kfree(new_physxa); 215 216 if (err) 217 return err; 218 } else { 219 physxa = new_physxa; 220 } 221 } 222 223 bits = xa_load_or_alloc(&physxa->phys_bits, pfn_high / PRESERVE_BITS); 224 if (IS_ERR(bits)) 225 return PTR_ERR(bits); 226 227 set_bit(pfn_high % PRESERVE_BITS, bits->preserve); 228 229 return 0; 230 } 231 232 static struct page *kho_restore_page(phys_addr_t phys) 233 { 234 struct page *page = pfn_to_online_page(PHYS_PFN(phys)); 235 union kho_page_info info; 236 unsigned int nr_pages; 237 238 if (!page) 239 return NULL; 240 241 info.page_private = page->private; 242 /* 243 * deserialize_bitmap() only sets the magic on the head page. This magic 244 * check also implicitly makes sure phys is order-aligned since for 245 * non-order-aligned phys addresses, magic will never be set. 246 */ 247 if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) 248 return NULL; 249 nr_pages = (1 << info.order); 250 251 /* Clear private to make sure later restores on this page error out. */ 252 page->private = 0; 253 /* Head page gets refcount of 1. */ 254 set_page_count(page, 1); 255 256 /* For higher order folios, tail pages get a page count of zero. */ 257 for (unsigned int i = 1; i < nr_pages; i++) 258 set_page_count(page + i, 0); 259 260 if (info.order > 0) 261 prep_compound_page(page, info.order); 262 263 adjust_managed_page_count(page, nr_pages); 264 return page; 265 } 266 267 /** 268 * kho_restore_folio - recreates the folio from the preserved memory. 269 * @phys: physical address of the folio. 270 * 271 * Return: pointer to the struct folio on success, NULL on failure. 272 */ 273 struct folio *kho_restore_folio(phys_addr_t phys) 274 { 275 struct page *page = kho_restore_page(phys); 276 277 return page ? page_folio(page) : NULL; 278 } 279 EXPORT_SYMBOL_GPL(kho_restore_folio); 280 281 /** 282 * kho_restore_pages - restore list of contiguous order 0 pages. 283 * @phys: physical address of the first page. 284 * @nr_pages: number of pages. 285 * 286 * Restore a contiguous list of order 0 pages that was preserved with 287 * kho_preserve_pages(). 288 * 289 * Return: 0 on success, error code on failure 290 */ 291 struct page *kho_restore_pages(phys_addr_t phys, unsigned int nr_pages) 292 { 293 const unsigned long start_pfn = PHYS_PFN(phys); 294 const unsigned long end_pfn = start_pfn + nr_pages; 295 unsigned long pfn = start_pfn; 296 297 while (pfn < end_pfn) { 298 const unsigned int order = 299 min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); 300 struct page *page = kho_restore_page(PFN_PHYS(pfn)); 301 302 if (!page) 303 return NULL; 304 split_page(page, order); 305 pfn += 1 << order; 306 } 307 308 return pfn_to_page(start_pfn); 309 } 310 EXPORT_SYMBOL_GPL(kho_restore_pages); 311 312 /* Serialize and deserialize struct kho_mem_phys across kexec 313 * 314 * Record all the bitmaps in a linked list of pages for the next kernel to 315 * process. Each chunk holds bitmaps of the same order and each block of bitmaps 316 * starts at a given physical address. This allows the bitmaps to be sparse. The 317 * xarray is used to store them in a tree while building up the data structure, 318 * but the KHO successor kernel only needs to process them once in order. 319 * 320 * All of this memory is normal kmalloc() memory and is not marked for 321 * preservation. The successor kernel will remain isolated to the scratch space 322 * until it completes processing this list. Once processed all the memory 323 * storing these ranges will be marked as free. 324 */ 325 326 struct khoser_mem_bitmap_ptr { 327 phys_addr_t phys_start; 328 DECLARE_KHOSER_PTR(bitmap, struct kho_mem_phys_bits *); 329 }; 330 331 struct khoser_mem_chunk_hdr { 332 DECLARE_KHOSER_PTR(next, struct khoser_mem_chunk *); 333 unsigned int order; 334 unsigned int num_elms; 335 }; 336 337 #define KHOSER_BITMAP_SIZE \ 338 ((PAGE_SIZE - sizeof(struct khoser_mem_chunk_hdr)) / \ 339 sizeof(struct khoser_mem_bitmap_ptr)) 340 341 struct khoser_mem_chunk { 342 struct khoser_mem_chunk_hdr hdr; 343 struct khoser_mem_bitmap_ptr bitmaps[KHOSER_BITMAP_SIZE]; 344 }; 345 346 static_assert(sizeof(struct khoser_mem_chunk) == PAGE_SIZE); 347 348 static struct khoser_mem_chunk *new_chunk(struct khoser_mem_chunk *cur_chunk, 349 unsigned long order) 350 { 351 struct khoser_mem_chunk *chunk __free(free_page) = NULL; 352 353 chunk = (void *)get_zeroed_page(GFP_KERNEL); 354 if (!chunk) 355 return ERR_PTR(-ENOMEM); 356 357 if (WARN_ON(kho_scratch_overlap(virt_to_phys(chunk), PAGE_SIZE))) 358 return ERR_PTR(-EINVAL); 359 360 chunk->hdr.order = order; 361 if (cur_chunk) 362 KHOSER_STORE_PTR(cur_chunk->hdr.next, chunk); 363 return no_free_ptr(chunk); 364 } 365 366 static void kho_mem_ser_free(struct khoser_mem_chunk *first_chunk) 367 { 368 struct khoser_mem_chunk *chunk = first_chunk; 369 370 while (chunk) { 371 struct khoser_mem_chunk *tmp = chunk; 372 373 chunk = KHOSER_LOAD_PTR(chunk->hdr.next); 374 kfree(tmp); 375 } 376 } 377 378 static int kho_mem_serialize(struct kho_serialization *ser) 379 { 380 struct khoser_mem_chunk *first_chunk = NULL; 381 struct khoser_mem_chunk *chunk = NULL; 382 struct kho_mem_phys *physxa; 383 unsigned long order; 384 int err = -ENOMEM; 385 386 xa_for_each(&ser->track.orders, order, physxa) { 387 struct kho_mem_phys_bits *bits; 388 unsigned long phys; 389 390 chunk = new_chunk(chunk, order); 391 if (IS_ERR(chunk)) { 392 err = PTR_ERR(chunk); 393 goto err_free; 394 } 395 396 if (!first_chunk) 397 first_chunk = chunk; 398 399 xa_for_each(&physxa->phys_bits, phys, bits) { 400 struct khoser_mem_bitmap_ptr *elm; 401 402 if (chunk->hdr.num_elms == ARRAY_SIZE(chunk->bitmaps)) { 403 chunk = new_chunk(chunk, order); 404 if (IS_ERR(chunk)) { 405 err = PTR_ERR(chunk); 406 goto err_free; 407 } 408 } 409 410 elm = &chunk->bitmaps[chunk->hdr.num_elms]; 411 chunk->hdr.num_elms++; 412 elm->phys_start = (phys * PRESERVE_BITS) 413 << (order + PAGE_SHIFT); 414 KHOSER_STORE_PTR(elm->bitmap, bits); 415 } 416 } 417 418 ser->preserved_mem_map = first_chunk; 419 420 return 0; 421 422 err_free: 423 kho_mem_ser_free(first_chunk); 424 return err; 425 } 426 427 static void __init deserialize_bitmap(unsigned int order, 428 struct khoser_mem_bitmap_ptr *elm) 429 { 430 struct kho_mem_phys_bits *bitmap = KHOSER_LOAD_PTR(elm->bitmap); 431 unsigned long bit; 432 433 for_each_set_bit(bit, bitmap->preserve, PRESERVE_BITS) { 434 int sz = 1 << (order + PAGE_SHIFT); 435 phys_addr_t phys = 436 elm->phys_start + (bit << (order + PAGE_SHIFT)); 437 struct page *page = phys_to_page(phys); 438 union kho_page_info info; 439 440 memblock_reserve(phys, sz); 441 memblock_reserved_mark_noinit(phys, sz); 442 info.magic = KHO_PAGE_MAGIC; 443 info.order = order; 444 page->private = info.page_private; 445 } 446 } 447 448 static void __init kho_mem_deserialize(const void *fdt) 449 { 450 struct khoser_mem_chunk *chunk; 451 const phys_addr_t *mem; 452 int len; 453 454 mem = fdt_getprop(fdt, 0, PROP_PRESERVED_MEMORY_MAP, &len); 455 456 if (!mem || len != sizeof(*mem)) { 457 pr_err("failed to get preserved memory bitmaps\n"); 458 return; 459 } 460 461 chunk = *mem ? phys_to_virt(*mem) : NULL; 462 while (chunk) { 463 unsigned int i; 464 465 for (i = 0; i != chunk->hdr.num_elms; i++) 466 deserialize_bitmap(chunk->hdr.order, 467 &chunk->bitmaps[i]); 468 chunk = KHOSER_LOAD_PTR(chunk->hdr.next); 469 } 470 } 471 472 /* 473 * With KHO enabled, memory can become fragmented because KHO regions may 474 * be anywhere in physical address space. The scratch regions give us a 475 * safe zones that we will never see KHO allocations from. This is where we 476 * can later safely load our new kexec images into and then use the scratch 477 * area for early allocations that happen before page allocator is 478 * initialized. 479 */ 480 struct kho_scratch *kho_scratch; 481 unsigned int kho_scratch_cnt; 482 483 /* 484 * The scratch areas are scaled by default as percent of memory allocated from 485 * memblock. A user can override the scale with command line parameter: 486 * 487 * kho_scratch=N% 488 * 489 * It is also possible to explicitly define size for a lowmem, a global and 490 * per-node scratch areas: 491 * 492 * kho_scratch=l[KMG],n[KMG],m[KMG] 493 * 494 * The explicit size definition takes precedence over scale definition. 495 */ 496 static unsigned int scratch_scale __initdata = 200; 497 static phys_addr_t scratch_size_global __initdata; 498 static phys_addr_t scratch_size_pernode __initdata; 499 static phys_addr_t scratch_size_lowmem __initdata; 500 501 static int __init kho_parse_scratch_size(char *p) 502 { 503 size_t len; 504 unsigned long sizes[3]; 505 size_t total_size = 0; 506 int i; 507 508 if (!p) 509 return -EINVAL; 510 511 len = strlen(p); 512 if (!len) 513 return -EINVAL; 514 515 /* parse nn% */ 516 if (p[len - 1] == '%') { 517 /* unsigned int max is 4,294,967,295, 10 chars */ 518 char s_scale[11] = {}; 519 int ret = 0; 520 521 if (len > ARRAY_SIZE(s_scale)) 522 return -EINVAL; 523 524 memcpy(s_scale, p, len - 1); 525 ret = kstrtouint(s_scale, 10, &scratch_scale); 526 if (!ret) 527 pr_notice("scratch scale is %d%%\n", scratch_scale); 528 return ret; 529 } 530 531 /* parse ll[KMG],mm[KMG],nn[KMG] */ 532 for (i = 0; i < ARRAY_SIZE(sizes); i++) { 533 char *endp = p; 534 535 if (i > 0) { 536 if (*p != ',') 537 return -EINVAL; 538 p += 1; 539 } 540 541 sizes[i] = memparse(p, &endp); 542 if (endp == p) 543 return -EINVAL; 544 p = endp; 545 total_size += sizes[i]; 546 } 547 548 if (!total_size) 549 return -EINVAL; 550 551 /* The string should be fully consumed by now. */ 552 if (*p) 553 return -EINVAL; 554 555 scratch_size_lowmem = sizes[0]; 556 scratch_size_global = sizes[1]; 557 scratch_size_pernode = sizes[2]; 558 scratch_scale = 0; 559 560 pr_notice("scratch areas: lowmem: %lluMiB global: %lluMiB pernode: %lldMiB\n", 561 (u64)(scratch_size_lowmem >> 20), 562 (u64)(scratch_size_global >> 20), 563 (u64)(scratch_size_pernode >> 20)); 564 565 return 0; 566 } 567 early_param("kho_scratch", kho_parse_scratch_size); 568 569 static void __init scratch_size_update(void) 570 { 571 phys_addr_t size; 572 573 if (!scratch_scale) 574 return; 575 576 size = memblock_reserved_kern_size(ARCH_LOW_ADDRESS_LIMIT, 577 NUMA_NO_NODE); 578 size = size * scratch_scale / 100; 579 scratch_size_lowmem = round_up(size, CMA_MIN_ALIGNMENT_BYTES); 580 581 size = memblock_reserved_kern_size(MEMBLOCK_ALLOC_ANYWHERE, 582 NUMA_NO_NODE); 583 size = size * scratch_scale / 100 - scratch_size_lowmem; 584 scratch_size_global = round_up(size, CMA_MIN_ALIGNMENT_BYTES); 585 } 586 587 static phys_addr_t __init scratch_size_node(int nid) 588 { 589 phys_addr_t size; 590 591 if (scratch_scale) { 592 size = memblock_reserved_kern_size(MEMBLOCK_ALLOC_ANYWHERE, 593 nid); 594 size = size * scratch_scale / 100; 595 } else { 596 size = scratch_size_pernode; 597 } 598 599 return round_up(size, CMA_MIN_ALIGNMENT_BYTES); 600 } 601 602 /** 603 * kho_reserve_scratch - Reserve a contiguous chunk of memory for kexec 604 * 605 * With KHO we can preserve arbitrary pages in the system. To ensure we still 606 * have a large contiguous region of memory when we search the physical address 607 * space for target memory, let's make sure we always have a large CMA region 608 * active. This CMA region will only be used for movable pages which are not a 609 * problem for us during KHO because we can just move them somewhere else. 610 */ 611 static void __init kho_reserve_scratch(void) 612 { 613 phys_addr_t addr, size; 614 int nid, i = 0; 615 616 if (!kho_enable) 617 return; 618 619 scratch_size_update(); 620 621 /* FIXME: deal with node hot-plug/remove */ 622 kho_scratch_cnt = num_online_nodes() + 2; 623 size = kho_scratch_cnt * sizeof(*kho_scratch); 624 kho_scratch = memblock_alloc(size, PAGE_SIZE); 625 if (!kho_scratch) 626 goto err_disable_kho; 627 628 /* 629 * reserve scratch area in low memory for lowmem allocations in the 630 * next kernel 631 */ 632 size = scratch_size_lowmem; 633 addr = memblock_phys_alloc_range(size, CMA_MIN_ALIGNMENT_BYTES, 0, 634 ARCH_LOW_ADDRESS_LIMIT); 635 if (!addr) 636 goto err_free_scratch_desc; 637 638 kho_scratch[i].addr = addr; 639 kho_scratch[i].size = size; 640 i++; 641 642 /* reserve large contiguous area for allocations without nid */ 643 size = scratch_size_global; 644 addr = memblock_phys_alloc(size, CMA_MIN_ALIGNMENT_BYTES); 645 if (!addr) 646 goto err_free_scratch_areas; 647 648 kho_scratch[i].addr = addr; 649 kho_scratch[i].size = size; 650 i++; 651 652 for_each_online_node(nid) { 653 size = scratch_size_node(nid); 654 addr = memblock_alloc_range_nid(size, CMA_MIN_ALIGNMENT_BYTES, 655 0, MEMBLOCK_ALLOC_ACCESSIBLE, 656 nid, true); 657 if (!addr) 658 goto err_free_scratch_areas; 659 660 kho_scratch[i].addr = addr; 661 kho_scratch[i].size = size; 662 i++; 663 } 664 665 return; 666 667 err_free_scratch_areas: 668 for (i--; i >= 0; i--) 669 memblock_phys_free(kho_scratch[i].addr, kho_scratch[i].size); 670 err_free_scratch_desc: 671 memblock_free(kho_scratch, kho_scratch_cnt * sizeof(*kho_scratch)); 672 err_disable_kho: 673 pr_warn("Failed to reserve scratch area, disabling kexec handover\n"); 674 kho_enable = false; 675 } 676 677 struct fdt_debugfs { 678 struct list_head list; 679 struct debugfs_blob_wrapper wrapper; 680 struct dentry *file; 681 }; 682 683 static int kho_debugfs_fdt_add(struct list_head *list, struct dentry *dir, 684 const char *name, const void *fdt) 685 { 686 struct fdt_debugfs *f; 687 struct dentry *file; 688 689 f = kmalloc(sizeof(*f), GFP_KERNEL); 690 if (!f) 691 return -ENOMEM; 692 693 f->wrapper.data = (void *)fdt; 694 f->wrapper.size = fdt_totalsize(fdt); 695 696 file = debugfs_create_blob(name, 0400, dir, &f->wrapper); 697 if (IS_ERR(file)) { 698 kfree(f); 699 return PTR_ERR(file); 700 } 701 702 f->file = file; 703 list_add(&f->list, list); 704 705 return 0; 706 } 707 708 /** 709 * kho_add_subtree - record the physical address of a sub FDT in KHO root tree. 710 * @ser: serialization control object passed by KHO notifiers. 711 * @name: name of the sub tree. 712 * @fdt: the sub tree blob. 713 * 714 * Creates a new child node named @name in KHO root FDT and records 715 * the physical address of @fdt. The pages of @fdt must also be preserved 716 * by KHO for the new kernel to retrieve it after kexec. 717 * 718 * A debugfs blob entry is also created at 719 * ``/sys/kernel/debug/kho/out/sub_fdts/@name``. 720 * 721 * Return: 0 on success, error code on failure 722 */ 723 int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt) 724 { 725 int err = 0; 726 u64 phys = (u64)virt_to_phys(fdt); 727 void *root = page_to_virt(ser->fdt); 728 729 err |= fdt_begin_node(root, name); 730 err |= fdt_property(root, PROP_SUB_FDT, &phys, sizeof(phys)); 731 err |= fdt_end_node(root); 732 733 if (err) 734 return err; 735 736 return kho_debugfs_fdt_add(&ser->fdt_list, ser->sub_fdt_dir, name, fdt); 737 } 738 EXPORT_SYMBOL_GPL(kho_add_subtree); 739 740 int register_kho_notifier(struct notifier_block *nb) 741 { 742 return blocking_notifier_chain_register(&kho_out.chain_head, nb); 743 } 744 EXPORT_SYMBOL_GPL(register_kho_notifier); 745 746 int unregister_kho_notifier(struct notifier_block *nb) 747 { 748 return blocking_notifier_chain_unregister(&kho_out.chain_head, nb); 749 } 750 EXPORT_SYMBOL_GPL(unregister_kho_notifier); 751 752 /** 753 * kho_preserve_folio - preserve a folio across kexec. 754 * @folio: folio to preserve. 755 * 756 * Instructs KHO to preserve the whole folio across kexec. The order 757 * will be preserved as well. 758 * 759 * Return: 0 on success, error code on failure 760 */ 761 int kho_preserve_folio(struct folio *folio) 762 { 763 const unsigned long pfn = folio_pfn(folio); 764 const unsigned int order = folio_order(folio); 765 struct kho_mem_track *track = &kho_out.ser.track; 766 767 if (WARN_ON(kho_scratch_overlap(pfn << PAGE_SHIFT, PAGE_SIZE << order))) 768 return -EINVAL; 769 770 return __kho_preserve_order(track, pfn, order); 771 } 772 EXPORT_SYMBOL_GPL(kho_preserve_folio); 773 774 /** 775 * kho_preserve_pages - preserve contiguous pages across kexec 776 * @page: first page in the list. 777 * @nr_pages: number of pages. 778 * 779 * Preserve a contiguous list of order 0 pages. Must be restored using 780 * kho_restore_pages() to ensure the pages are restored properly as order 0. 781 * 782 * Return: 0 on success, error code on failure 783 */ 784 int kho_preserve_pages(struct page *page, unsigned int nr_pages) 785 { 786 struct kho_mem_track *track = &kho_out.ser.track; 787 const unsigned long start_pfn = page_to_pfn(page); 788 const unsigned long end_pfn = start_pfn + nr_pages; 789 unsigned long pfn = start_pfn; 790 unsigned long failed_pfn = 0; 791 int err = 0; 792 793 if (WARN_ON(kho_scratch_overlap(start_pfn << PAGE_SHIFT, 794 nr_pages << PAGE_SHIFT))) { 795 return -EINVAL; 796 } 797 798 while (pfn < end_pfn) { 799 const unsigned int order = 800 min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); 801 802 err = __kho_preserve_order(track, pfn, order); 803 if (err) { 804 failed_pfn = pfn; 805 break; 806 } 807 808 pfn += 1 << order; 809 } 810 811 if (err) 812 __kho_unpreserve(track, start_pfn, failed_pfn); 813 814 return err; 815 } 816 EXPORT_SYMBOL_GPL(kho_preserve_pages); 817 818 struct kho_vmalloc_hdr { 819 DECLARE_KHOSER_PTR(next, struct kho_vmalloc_chunk *); 820 }; 821 822 #define KHO_VMALLOC_SIZE \ 823 ((PAGE_SIZE - sizeof(struct kho_vmalloc_hdr)) / \ 824 sizeof(phys_addr_t)) 825 826 struct kho_vmalloc_chunk { 827 struct kho_vmalloc_hdr hdr; 828 phys_addr_t phys[KHO_VMALLOC_SIZE]; 829 }; 830 831 static_assert(sizeof(struct kho_vmalloc_chunk) == PAGE_SIZE); 832 833 /* vmalloc flags KHO supports */ 834 #define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP) 835 836 /* KHO internal flags for vmalloc preservations */ 837 #define KHO_VMALLOC_ALLOC 0x0001 838 #define KHO_VMALLOC_HUGE_VMAP 0x0002 839 840 static unsigned short vmalloc_flags_to_kho(unsigned int vm_flags) 841 { 842 unsigned short kho_flags = 0; 843 844 if (vm_flags & VM_ALLOC) 845 kho_flags |= KHO_VMALLOC_ALLOC; 846 if (vm_flags & VM_ALLOW_HUGE_VMAP) 847 kho_flags |= KHO_VMALLOC_HUGE_VMAP; 848 849 return kho_flags; 850 } 851 852 static unsigned int kho_flags_to_vmalloc(unsigned short kho_flags) 853 { 854 unsigned int vm_flags = 0; 855 856 if (kho_flags & KHO_VMALLOC_ALLOC) 857 vm_flags |= VM_ALLOC; 858 if (kho_flags & KHO_VMALLOC_HUGE_VMAP) 859 vm_flags |= VM_ALLOW_HUGE_VMAP; 860 861 return vm_flags; 862 } 863 864 static struct kho_vmalloc_chunk *new_vmalloc_chunk(struct kho_vmalloc_chunk *cur) 865 { 866 struct kho_vmalloc_chunk *chunk; 867 int err; 868 869 chunk = (struct kho_vmalloc_chunk *)get_zeroed_page(GFP_KERNEL); 870 if (!chunk) 871 return NULL; 872 873 err = kho_preserve_pages(virt_to_page(chunk), 1); 874 if (err) 875 goto err_free; 876 if (cur) 877 KHOSER_STORE_PTR(cur->hdr.next, chunk); 878 return chunk; 879 880 err_free: 881 free_page((unsigned long)chunk); 882 return NULL; 883 } 884 885 static void kho_vmalloc_unpreserve_chunk(struct kho_vmalloc_chunk *chunk) 886 { 887 struct kho_mem_track *track = &kho_out.ser.track; 888 unsigned long pfn = PHYS_PFN(virt_to_phys(chunk)); 889 890 __kho_unpreserve(track, pfn, pfn + 1); 891 892 for (int i = 0; chunk->phys[i]; i++) { 893 pfn = PHYS_PFN(chunk->phys[i]); 894 __kho_unpreserve(track, pfn, pfn + 1); 895 } 896 } 897 898 static void kho_vmalloc_free_chunks(struct kho_vmalloc *kho_vmalloc) 899 { 900 struct kho_vmalloc_chunk *chunk = KHOSER_LOAD_PTR(kho_vmalloc->first); 901 902 while (chunk) { 903 struct kho_vmalloc_chunk *tmp = chunk; 904 905 kho_vmalloc_unpreserve_chunk(chunk); 906 907 chunk = KHOSER_LOAD_PTR(chunk->hdr.next); 908 free_page((unsigned long)tmp); 909 } 910 } 911 912 /** 913 * kho_preserve_vmalloc - preserve memory allocated with vmalloc() across kexec 914 * @ptr: pointer to the area in vmalloc address space 915 * @preservation: placeholder for preservation metadata 916 * 917 * Instructs KHO to preserve the area in vmalloc address space at @ptr. The 918 * physical pages mapped at @ptr will be preserved and on successful return 919 * @preservation will hold the physical address of a structure that describes 920 * the preservation. 921 * 922 * NOTE: The memory allocated with vmalloc_node() variants cannot be reliably 923 * restored on the same node 924 * 925 * Return: 0 on success, error code on failure 926 */ 927 int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation) 928 { 929 struct kho_vmalloc_chunk *chunk; 930 struct vm_struct *vm = find_vm_area(ptr); 931 unsigned int order, flags, nr_contig_pages; 932 unsigned int idx = 0; 933 int err; 934 935 if (!vm) 936 return -EINVAL; 937 938 if (vm->flags & ~KHO_VMALLOC_SUPPORTED_FLAGS) 939 return -EOPNOTSUPP; 940 941 flags = vmalloc_flags_to_kho(vm->flags); 942 order = get_vm_area_page_order(vm); 943 944 chunk = new_vmalloc_chunk(NULL); 945 if (!chunk) 946 return -ENOMEM; 947 KHOSER_STORE_PTR(preservation->first, chunk); 948 949 nr_contig_pages = (1 << order); 950 for (int i = 0; i < vm->nr_pages; i += nr_contig_pages) { 951 phys_addr_t phys = page_to_phys(vm->pages[i]); 952 953 err = kho_preserve_pages(vm->pages[i], nr_contig_pages); 954 if (err) 955 goto err_free; 956 957 chunk->phys[idx++] = phys; 958 if (idx == ARRAY_SIZE(chunk->phys)) { 959 chunk = new_vmalloc_chunk(chunk); 960 if (!chunk) 961 goto err_free; 962 idx = 0; 963 } 964 } 965 966 preservation->total_pages = vm->nr_pages; 967 preservation->flags = flags; 968 preservation->order = order; 969 970 return 0; 971 972 err_free: 973 kho_vmalloc_free_chunks(preservation); 974 return err; 975 } 976 EXPORT_SYMBOL_GPL(kho_preserve_vmalloc); 977 978 /** 979 * kho_restore_vmalloc - recreates and populates an area in vmalloc address 980 * space from the preserved memory. 981 * @preservation: preservation metadata. 982 * 983 * Recreates an area in vmalloc address space and populates it with memory that 984 * was preserved using kho_preserve_vmalloc(). 985 * 986 * Return: pointer to the area in the vmalloc address space, NULL on failure. 987 */ 988 void *kho_restore_vmalloc(const struct kho_vmalloc *preservation) 989 { 990 struct kho_vmalloc_chunk *chunk = KHOSER_LOAD_PTR(preservation->first); 991 unsigned int align, order, shift, vm_flags; 992 unsigned long total_pages, contig_pages; 993 unsigned long addr, size; 994 struct vm_struct *area; 995 struct page **pages; 996 unsigned int idx = 0; 997 int err; 998 999 vm_flags = kho_flags_to_vmalloc(preservation->flags); 1000 if (vm_flags & ~KHO_VMALLOC_SUPPORTED_FLAGS) 1001 return NULL; 1002 1003 total_pages = preservation->total_pages; 1004 pages = kvmalloc_array(total_pages, sizeof(*pages), GFP_KERNEL); 1005 if (!pages) 1006 return NULL; 1007 order = preservation->order; 1008 contig_pages = (1 << order); 1009 shift = PAGE_SHIFT + order; 1010 align = 1 << shift; 1011 1012 while (chunk) { 1013 struct page *page; 1014 1015 for (int i = 0; chunk->phys[i]; i++) { 1016 phys_addr_t phys = chunk->phys[i]; 1017 1018 if (idx + contig_pages > total_pages) 1019 goto err_free_pages_array; 1020 1021 page = kho_restore_pages(phys, contig_pages); 1022 if (!page) 1023 goto err_free_pages_array; 1024 1025 for (int j = 0; j < contig_pages; j++) 1026 pages[idx++] = page; 1027 1028 phys += contig_pages * PAGE_SIZE; 1029 } 1030 1031 page = kho_restore_pages(virt_to_phys(chunk), 1); 1032 if (!page) 1033 goto err_free_pages_array; 1034 chunk = KHOSER_LOAD_PTR(chunk->hdr.next); 1035 __free_page(page); 1036 } 1037 1038 if (idx != total_pages) 1039 goto err_free_pages_array; 1040 1041 area = __get_vm_area_node(total_pages * PAGE_SIZE, align, shift, 1042 vm_flags, VMALLOC_START, VMALLOC_END, 1043 NUMA_NO_NODE, GFP_KERNEL, 1044 __builtin_return_address(0)); 1045 if (!area) 1046 goto err_free_pages_array; 1047 1048 addr = (unsigned long)area->addr; 1049 size = get_vm_area_size(area); 1050 err = vmap_pages_range(addr, addr + size, PAGE_KERNEL, pages, shift); 1051 if (err) 1052 goto err_free_vm_area; 1053 1054 area->nr_pages = total_pages; 1055 area->pages = pages; 1056 1057 return area->addr; 1058 1059 err_free_vm_area: 1060 free_vm_area(area); 1061 err_free_pages_array: 1062 kvfree(pages); 1063 return NULL; 1064 } 1065 EXPORT_SYMBOL_GPL(kho_restore_vmalloc); 1066 1067 /* Handling for debug/kho/out */ 1068 1069 static struct dentry *debugfs_root; 1070 1071 static int kho_out_update_debugfs_fdt(void) 1072 { 1073 int err = 0; 1074 struct fdt_debugfs *ff, *tmp; 1075 1076 if (kho_out.finalized) { 1077 err = kho_debugfs_fdt_add(&kho_out.ser.fdt_list, kho_out.dir, 1078 "fdt", page_to_virt(kho_out.ser.fdt)); 1079 } else { 1080 list_for_each_entry_safe(ff, tmp, &kho_out.ser.fdt_list, list) { 1081 debugfs_remove(ff->file); 1082 list_del(&ff->list); 1083 kfree(ff); 1084 } 1085 } 1086 1087 return err; 1088 } 1089 1090 static int kho_abort(void) 1091 { 1092 int err; 1093 unsigned long order; 1094 struct kho_mem_phys *physxa; 1095 1096 xa_for_each(&kho_out.ser.track.orders, order, physxa) { 1097 struct kho_mem_phys_bits *bits; 1098 unsigned long phys; 1099 1100 xa_for_each(&physxa->phys_bits, phys, bits) 1101 kfree(bits); 1102 1103 xa_destroy(&physxa->phys_bits); 1104 kfree(physxa); 1105 } 1106 xa_destroy(&kho_out.ser.track.orders); 1107 1108 if (kho_out.ser.preserved_mem_map) { 1109 kho_mem_ser_free(kho_out.ser.preserved_mem_map); 1110 kho_out.ser.preserved_mem_map = NULL; 1111 } 1112 1113 err = blocking_notifier_call_chain(&kho_out.chain_head, KEXEC_KHO_ABORT, 1114 NULL); 1115 err = notifier_to_errno(err); 1116 1117 if (err) 1118 pr_err("Failed to abort KHO finalization: %d\n", err); 1119 1120 return err; 1121 } 1122 1123 static int kho_finalize(void) 1124 { 1125 int err = 0; 1126 u64 *preserved_mem_map; 1127 void *fdt = page_to_virt(kho_out.ser.fdt); 1128 1129 err |= fdt_create(fdt, PAGE_SIZE); 1130 err |= fdt_finish_reservemap(fdt); 1131 err |= fdt_begin_node(fdt, ""); 1132 err |= fdt_property_string(fdt, "compatible", KHO_FDT_COMPATIBLE); 1133 /** 1134 * Reserve the preserved-memory-map property in the root FDT, so 1135 * that all property definitions will precede subnodes created by 1136 * KHO callers. 1137 */ 1138 err |= fdt_property_placeholder(fdt, PROP_PRESERVED_MEMORY_MAP, 1139 sizeof(*preserved_mem_map), 1140 (void **)&preserved_mem_map); 1141 if (err) 1142 goto abort; 1143 1144 err = kho_preserve_folio(page_folio(kho_out.ser.fdt)); 1145 if (err) 1146 goto abort; 1147 1148 err = blocking_notifier_call_chain(&kho_out.chain_head, 1149 KEXEC_KHO_FINALIZE, &kho_out.ser); 1150 err = notifier_to_errno(err); 1151 if (err) 1152 goto abort; 1153 1154 err = kho_mem_serialize(&kho_out.ser); 1155 if (err) 1156 goto abort; 1157 1158 *preserved_mem_map = (u64)virt_to_phys(kho_out.ser.preserved_mem_map); 1159 1160 err |= fdt_end_node(fdt); 1161 err |= fdt_finish(fdt); 1162 1163 abort: 1164 if (err) { 1165 pr_err("Failed to convert KHO state tree: %d\n", err); 1166 kho_abort(); 1167 } 1168 1169 return err; 1170 } 1171 1172 static int kho_out_finalize_get(void *data, u64 *val) 1173 { 1174 mutex_lock(&kho_out.lock); 1175 *val = kho_out.finalized; 1176 mutex_unlock(&kho_out.lock); 1177 1178 return 0; 1179 } 1180 1181 static int kho_out_finalize_set(void *data, u64 _val) 1182 { 1183 int ret = 0; 1184 bool val = !!_val; 1185 1186 mutex_lock(&kho_out.lock); 1187 1188 if (val == kho_out.finalized) { 1189 if (kho_out.finalized) 1190 ret = -EEXIST; 1191 else 1192 ret = -ENOENT; 1193 goto unlock; 1194 } 1195 1196 if (val) 1197 ret = kho_finalize(); 1198 else 1199 ret = kho_abort(); 1200 1201 if (ret) 1202 goto unlock; 1203 1204 kho_out.finalized = val; 1205 ret = kho_out_update_debugfs_fdt(); 1206 1207 unlock: 1208 mutex_unlock(&kho_out.lock); 1209 return ret; 1210 } 1211 1212 DEFINE_DEBUGFS_ATTRIBUTE(fops_kho_out_finalize, kho_out_finalize_get, 1213 kho_out_finalize_set, "%llu\n"); 1214 1215 static int scratch_phys_show(struct seq_file *m, void *v) 1216 { 1217 for (int i = 0; i < kho_scratch_cnt; i++) 1218 seq_printf(m, "0x%llx\n", kho_scratch[i].addr); 1219 1220 return 0; 1221 } 1222 DEFINE_SHOW_ATTRIBUTE(scratch_phys); 1223 1224 static int scratch_len_show(struct seq_file *m, void *v) 1225 { 1226 for (int i = 0; i < kho_scratch_cnt; i++) 1227 seq_printf(m, "0x%llx\n", kho_scratch[i].size); 1228 1229 return 0; 1230 } 1231 DEFINE_SHOW_ATTRIBUTE(scratch_len); 1232 1233 static __init int kho_out_debugfs_init(void) 1234 { 1235 struct dentry *dir, *f, *sub_fdt_dir; 1236 1237 dir = debugfs_create_dir("out", debugfs_root); 1238 if (IS_ERR(dir)) 1239 return -ENOMEM; 1240 1241 sub_fdt_dir = debugfs_create_dir("sub_fdts", dir); 1242 if (IS_ERR(sub_fdt_dir)) 1243 goto err_rmdir; 1244 1245 f = debugfs_create_file("scratch_phys", 0400, dir, NULL, 1246 &scratch_phys_fops); 1247 if (IS_ERR(f)) 1248 goto err_rmdir; 1249 1250 f = debugfs_create_file("scratch_len", 0400, dir, NULL, 1251 &scratch_len_fops); 1252 if (IS_ERR(f)) 1253 goto err_rmdir; 1254 1255 f = debugfs_create_file("finalize", 0600, dir, NULL, 1256 &fops_kho_out_finalize); 1257 if (IS_ERR(f)) 1258 goto err_rmdir; 1259 1260 kho_out.dir = dir; 1261 kho_out.ser.sub_fdt_dir = sub_fdt_dir; 1262 return 0; 1263 1264 err_rmdir: 1265 debugfs_remove_recursive(dir); 1266 return -ENOENT; 1267 } 1268 1269 struct kho_in { 1270 struct dentry *dir; 1271 phys_addr_t fdt_phys; 1272 phys_addr_t scratch_phys; 1273 struct list_head fdt_list; 1274 }; 1275 1276 static struct kho_in kho_in = { 1277 .fdt_list = LIST_HEAD_INIT(kho_in.fdt_list), 1278 }; 1279 1280 static const void *kho_get_fdt(void) 1281 { 1282 return kho_in.fdt_phys ? phys_to_virt(kho_in.fdt_phys) : NULL; 1283 } 1284 1285 /** 1286 * is_kho_boot - check if current kernel was booted via KHO-enabled 1287 * kexec 1288 * 1289 * This function checks if the current kernel was loaded through a kexec 1290 * operation with KHO enabled, by verifying that a valid KHO FDT 1291 * was passed. 1292 * 1293 * Note: This function returns reliable results only after 1294 * kho_populate() has been called during early boot. Before that, 1295 * it may return false even if KHO data is present. 1296 * 1297 * Return: true if booted via KHO-enabled kexec, false otherwise 1298 */ 1299 bool is_kho_boot(void) 1300 { 1301 return !!kho_get_fdt(); 1302 } 1303 EXPORT_SYMBOL_GPL(is_kho_boot); 1304 1305 /** 1306 * kho_retrieve_subtree - retrieve a preserved sub FDT by its name. 1307 * @name: the name of the sub FDT passed to kho_add_subtree(). 1308 * @phys: if found, the physical address of the sub FDT is stored in @phys. 1309 * 1310 * Retrieve a preserved sub FDT named @name and store its physical 1311 * address in @phys. 1312 * 1313 * Return: 0 on success, error code on failure 1314 */ 1315 int kho_retrieve_subtree(const char *name, phys_addr_t *phys) 1316 { 1317 const void *fdt = kho_get_fdt(); 1318 const u64 *val; 1319 int offset, len; 1320 1321 if (!fdt) 1322 return -ENOENT; 1323 1324 if (!phys) 1325 return -EINVAL; 1326 1327 offset = fdt_subnode_offset(fdt, 0, name); 1328 if (offset < 0) 1329 return -ENOENT; 1330 1331 val = fdt_getprop(fdt, offset, PROP_SUB_FDT, &len); 1332 if (!val || len != sizeof(*val)) 1333 return -EINVAL; 1334 1335 *phys = (phys_addr_t)*val; 1336 1337 return 0; 1338 } 1339 EXPORT_SYMBOL_GPL(kho_retrieve_subtree); 1340 1341 /* Handling for debugfs/kho/in */ 1342 1343 static __init int kho_in_debugfs_init(const void *fdt) 1344 { 1345 struct dentry *sub_fdt_dir; 1346 int err, child; 1347 1348 kho_in.dir = debugfs_create_dir("in", debugfs_root); 1349 if (IS_ERR(kho_in.dir)) 1350 return PTR_ERR(kho_in.dir); 1351 1352 sub_fdt_dir = debugfs_create_dir("sub_fdts", kho_in.dir); 1353 if (IS_ERR(sub_fdt_dir)) { 1354 err = PTR_ERR(sub_fdt_dir); 1355 goto err_rmdir; 1356 } 1357 1358 err = kho_debugfs_fdt_add(&kho_in.fdt_list, kho_in.dir, "fdt", fdt); 1359 if (err) 1360 goto err_rmdir; 1361 1362 fdt_for_each_subnode(child, fdt, 0) { 1363 int len = 0; 1364 const char *name = fdt_get_name(fdt, child, NULL); 1365 const u64 *fdt_phys; 1366 1367 fdt_phys = fdt_getprop(fdt, child, "fdt", &len); 1368 if (!fdt_phys) 1369 continue; 1370 if (len != sizeof(*fdt_phys)) { 1371 pr_warn("node `%s`'s prop `fdt` has invalid length: %d\n", 1372 name, len); 1373 continue; 1374 } 1375 err = kho_debugfs_fdt_add(&kho_in.fdt_list, sub_fdt_dir, name, 1376 phys_to_virt(*fdt_phys)); 1377 if (err) { 1378 pr_warn("failed to add fdt `%s` to debugfs: %d\n", name, 1379 err); 1380 continue; 1381 } 1382 } 1383 1384 return 0; 1385 1386 err_rmdir: 1387 debugfs_remove_recursive(kho_in.dir); 1388 return err; 1389 } 1390 1391 static __init int kho_init(void) 1392 { 1393 int err = 0; 1394 const void *fdt = kho_get_fdt(); 1395 1396 if (!kho_enable) 1397 return 0; 1398 1399 kho_out.ser.fdt = alloc_page(GFP_KERNEL); 1400 if (!kho_out.ser.fdt) { 1401 err = -ENOMEM; 1402 goto err_free_scratch; 1403 } 1404 1405 debugfs_root = debugfs_create_dir("kho", NULL); 1406 if (IS_ERR(debugfs_root)) { 1407 err = -ENOENT; 1408 goto err_free_fdt; 1409 } 1410 1411 err = kho_out_debugfs_init(); 1412 if (err) 1413 goto err_free_fdt; 1414 1415 if (fdt) { 1416 err = kho_in_debugfs_init(fdt); 1417 /* 1418 * Failure to create /sys/kernel/debug/kho/in does not prevent 1419 * reviving state from KHO and setting up KHO for the next 1420 * kexec. 1421 */ 1422 if (err) 1423 pr_err("failed exposing handover FDT in debugfs: %d\n", 1424 err); 1425 1426 return 0; 1427 } 1428 1429 for (int i = 0; i < kho_scratch_cnt; i++) { 1430 unsigned long base_pfn = PHYS_PFN(kho_scratch[i].addr); 1431 unsigned long count = kho_scratch[i].size >> PAGE_SHIFT; 1432 unsigned long pfn; 1433 1434 for (pfn = base_pfn; pfn < base_pfn + count; 1435 pfn += pageblock_nr_pages) 1436 init_cma_reserved_pageblock(pfn_to_page(pfn)); 1437 } 1438 1439 return 0; 1440 1441 err_free_fdt: 1442 put_page(kho_out.ser.fdt); 1443 kho_out.ser.fdt = NULL; 1444 err_free_scratch: 1445 for (int i = 0; i < kho_scratch_cnt; i++) { 1446 void *start = __va(kho_scratch[i].addr); 1447 void *end = start + kho_scratch[i].size; 1448 1449 free_reserved_area(start, end, -1, ""); 1450 } 1451 kho_enable = false; 1452 return err; 1453 } 1454 late_initcall(kho_init); 1455 1456 static void __init kho_release_scratch(void) 1457 { 1458 phys_addr_t start, end; 1459 u64 i; 1460 1461 memmap_init_kho_scratch_pages(); 1462 1463 /* 1464 * Mark scratch mem as CMA before we return it. That way we 1465 * ensure that no kernel allocations happen on it. That means 1466 * we can reuse it as scratch memory again later. 1467 */ 1468 __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, 1469 MEMBLOCK_KHO_SCRATCH, &start, &end, NULL) { 1470 ulong start_pfn = pageblock_start_pfn(PFN_DOWN(start)); 1471 ulong end_pfn = pageblock_align(PFN_UP(end)); 1472 ulong pfn; 1473 1474 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) 1475 init_pageblock_migratetype(pfn_to_page(pfn), 1476 MIGRATE_CMA, false); 1477 } 1478 } 1479 1480 void __init kho_memory_init(void) 1481 { 1482 struct folio *folio; 1483 1484 if (kho_in.scratch_phys) { 1485 kho_scratch = phys_to_virt(kho_in.scratch_phys); 1486 kho_release_scratch(); 1487 1488 kho_mem_deserialize(kho_get_fdt()); 1489 folio = kho_restore_folio(kho_in.fdt_phys); 1490 if (!folio) 1491 pr_warn("failed to restore folio for KHO fdt\n"); 1492 } else { 1493 kho_reserve_scratch(); 1494 } 1495 } 1496 1497 void __init kho_populate(phys_addr_t fdt_phys, u64 fdt_len, 1498 phys_addr_t scratch_phys, u64 scratch_len) 1499 { 1500 void *fdt = NULL; 1501 struct kho_scratch *scratch = NULL; 1502 int err = 0; 1503 unsigned int scratch_cnt = scratch_len / sizeof(*kho_scratch); 1504 1505 /* Validate the input FDT */ 1506 fdt = early_memremap(fdt_phys, fdt_len); 1507 if (!fdt) { 1508 pr_warn("setup: failed to memremap FDT (0x%llx)\n", fdt_phys); 1509 err = -EFAULT; 1510 goto out; 1511 } 1512 err = fdt_check_header(fdt); 1513 if (err) { 1514 pr_warn("setup: handover FDT (0x%llx) is invalid: %d\n", 1515 fdt_phys, err); 1516 err = -EINVAL; 1517 goto out; 1518 } 1519 err = fdt_node_check_compatible(fdt, 0, KHO_FDT_COMPATIBLE); 1520 if (err) { 1521 pr_warn("setup: handover FDT (0x%llx) is incompatible with '%s': %d\n", 1522 fdt_phys, KHO_FDT_COMPATIBLE, err); 1523 err = -EINVAL; 1524 goto out; 1525 } 1526 1527 scratch = early_memremap(scratch_phys, scratch_len); 1528 if (!scratch) { 1529 pr_warn("setup: failed to memremap scratch (phys=0x%llx, len=%lld)\n", 1530 scratch_phys, scratch_len); 1531 err = -EFAULT; 1532 goto out; 1533 } 1534 1535 /* 1536 * We pass a safe contiguous blocks of memory to use for early boot 1537 * purporses from the previous kernel so that we can resize the 1538 * memblock array as needed. 1539 */ 1540 for (int i = 0; i < scratch_cnt; i++) { 1541 struct kho_scratch *area = &scratch[i]; 1542 u64 size = area->size; 1543 1544 memblock_add(area->addr, size); 1545 err = memblock_mark_kho_scratch(area->addr, size); 1546 if (WARN_ON(err)) { 1547 pr_warn("failed to mark the scratch region 0x%pa+0x%pa: %d", 1548 &area->addr, &size, err); 1549 goto out; 1550 } 1551 pr_debug("Marked 0x%pa+0x%pa as scratch", &area->addr, &size); 1552 } 1553 1554 memblock_reserve(scratch_phys, scratch_len); 1555 1556 /* 1557 * Now that we have a viable region of scratch memory, let's tell 1558 * the memblocks allocator to only use that for any allocations. 1559 * That way we ensure that nothing scribbles over in use data while 1560 * we initialize the page tables which we will need to ingest all 1561 * memory reservations from the previous kernel. 1562 */ 1563 memblock_set_kho_scratch_only(); 1564 1565 kho_in.fdt_phys = fdt_phys; 1566 kho_in.scratch_phys = scratch_phys; 1567 kho_scratch_cnt = scratch_cnt; 1568 pr_info("found kexec handover data. Will skip init for some devices\n"); 1569 1570 out: 1571 if (fdt) 1572 early_memunmap(fdt, fdt_len); 1573 if (scratch) 1574 early_memunmap(scratch, scratch_len); 1575 if (err) 1576 pr_warn("disabling KHO revival: %d\n", err); 1577 } 1578 1579 /* Helper functions for kexec_file_load */ 1580 1581 int kho_fill_kimage(struct kimage *image) 1582 { 1583 ssize_t scratch_size; 1584 int err = 0; 1585 struct kexec_buf scratch; 1586 1587 if (!kho_out.finalized) 1588 return 0; 1589 1590 image->kho.fdt = page_to_phys(kho_out.ser.fdt); 1591 1592 scratch_size = sizeof(*kho_scratch) * kho_scratch_cnt; 1593 scratch = (struct kexec_buf){ 1594 .image = image, 1595 .buffer = kho_scratch, 1596 .bufsz = scratch_size, 1597 .mem = KEXEC_BUF_MEM_UNKNOWN, 1598 .memsz = scratch_size, 1599 .buf_align = SZ_64K, /* Makes it easier to map */ 1600 .buf_max = ULONG_MAX, 1601 .top_down = true, 1602 }; 1603 err = kexec_add_buffer(&scratch); 1604 if (err) 1605 return err; 1606 image->kho.scratch = &image->segment[image->nr_segments - 1]; 1607 1608 return 0; 1609 } 1610 1611 static int kho_walk_scratch(struct kexec_buf *kbuf, 1612 int (*func)(struct resource *, void *)) 1613 { 1614 int ret = 0; 1615 int i; 1616 1617 for (i = 0; i < kho_scratch_cnt; i++) { 1618 struct resource res = { 1619 .start = kho_scratch[i].addr, 1620 .end = kho_scratch[i].addr + kho_scratch[i].size - 1, 1621 }; 1622 1623 /* Try to fit the kimage into our KHO scratch region */ 1624 ret = func(&res, kbuf); 1625 if (ret) 1626 break; 1627 } 1628 1629 return ret; 1630 } 1631 1632 int kho_locate_mem_hole(struct kexec_buf *kbuf, 1633 int (*func)(struct resource *, void *)) 1634 { 1635 int ret; 1636 1637 if (!kho_enable || kbuf->image->type == KEXEC_TYPE_CRASH) 1638 return 1; 1639 1640 ret = kho_walk_scratch(kbuf, func); 1641 1642 return ret == 1 ? 0 : -EADDRNOTAVAIL; 1643 } 1644