1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * linux/mm/memory_hotplug.c 4 * 5 * Copyright (C) 6 */ 7 8#include <linux/stddef.h> 9#include <linux/mm.h> 10#include <linux/sched/signal.h> 11#include <linux/swap.h> 12#include <linux/interrupt.h> 13#include <linux/pagemap.h> 14#include <linux/compiler.h> 15#include <linux/export.h> 16#include <linux/pagevec.h> 17#include <linux/writeback.h> 18#include <linux/slab.h> 19#include <linux/sysctl.h> 20#include <linux/cpu.h> 21#include <linux/memory.h> 22#include <linux/memremap.h> 23#include <linux/memory_hotplug.h> 24#include <linux/highmem.h> 25#include <linux/vmalloc.h> 26#include <linux/ioport.h> 27#include <linux/delay.h> 28#include <linux/migrate.h> 29#include <linux/page-isolation.h> 30#include <linux/pfn.h> 31#include <linux/suspend.h> 32#include <linux/mm_inline.h> 33#include <linux/firmware-map.h> 34#include <linux/stop_machine.h> 35#include <linux/hugetlb.h> 36#include <linux/memblock.h> 37#include <linux/compaction.h> 38#include <linux/rmap.h> 39#include <linux/zswapd.h> 40 41#include <asm/tlbflush.h> 42 43#include "internal.h" 44#include "shuffle.h" 45 46/* 47 * online_page_callback contains pointer to current page onlining function. 48 * Initially it is generic_online_page(). If it is required it could be 49 * changed by calling set_online_page_callback() for callback registration 50 * and restore_online_page_callback() for generic callback restore. 51 */ 52 53static online_page_callback_t online_page_callback = generic_online_page; 54static DEFINE_MUTEX(online_page_callback_lock); 55 56DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); 57 58void get_online_mems(void) 59{ 60 percpu_down_read(&mem_hotplug_lock); 61} 62 63void put_online_mems(void) 64{ 65 percpu_up_read(&mem_hotplug_lock); 66} 67 68bool movable_node_enabled = false; 69 70#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE 71int memhp_default_online_type = MMOP_OFFLINE; 72#else 73int memhp_default_online_type = MMOP_ONLINE; 74#endif 75 76static int __init setup_memhp_default_state(char *str) 77{ 78 const int online_type = memhp_online_type_from_str(str); 79 80 if (online_type >= 0) 81 memhp_default_online_type = online_type; 82 83 return 1; 84} 85__setup("memhp_default_state=", setup_memhp_default_state); 86 87void mem_hotplug_begin(void) 88{ 89 cpus_read_lock(); 90 percpu_down_write(&mem_hotplug_lock); 91} 92 93void mem_hotplug_done(void) 94{ 95 percpu_up_write(&mem_hotplug_lock); 96 cpus_read_unlock(); 97} 98 99u64 max_mem_size = U64_MAX; 100 101/* add this memory to iomem resource */ 102static struct resource *register_memory_resource(u64 start, u64 size, 103 const char *resource_name) 104{ 105 struct resource *res; 106 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 107 108 if (strcmp(resource_name, "System RAM")) 109 flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED; 110 111 /* 112 * Make sure value parsed from 'mem=' only restricts memory adding 113 * while booting, so that memory hotplug won't be impacted. Please 114 * refer to document of 'mem=' in kernel-parameters.txt for more 115 * details. 116 */ 117 if (start + size > max_mem_size && system_state < SYSTEM_RUNNING) 118 return ERR_PTR(-E2BIG); 119 120 /* 121 * Request ownership of the new memory range. This might be 122 * a child of an existing resource that was present but 123 * not marked as busy. 124 */ 125 res = __request_region(&iomem_resource, start, size, 126 resource_name, flags); 127 128 if (!res) { 129 pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", 130 start, start + size); 131 return ERR_PTR(-EEXIST); 132 } 133 return res; 134} 135 136static void release_memory_resource(struct resource *res) 137{ 138 if (!res) 139 return; 140 release_resource(res); 141 kfree(res); 142} 143 144#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 145void get_page_bootmem(unsigned long info, struct page *page, 146 unsigned long type) 147{ 148 page->freelist = (void *)type; 149 SetPagePrivate(page); 150 set_page_private(page, info); 151 page_ref_inc(page); 152} 153 154void put_page_bootmem(struct page *page) 155{ 156 unsigned long type; 157 158 type = (unsigned long) page->freelist; 159 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 160 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 161 162 if (page_ref_dec_return(page) == 1) { 163 page->freelist = NULL; 164 ClearPagePrivate(page); 165 set_page_private(page, 0); 166 INIT_LIST_HEAD(&page->lru); 167 free_reserved_page(page); 168 } 169} 170 171#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 172#ifndef CONFIG_SPARSEMEM_VMEMMAP 173static void register_page_bootmem_info_section(unsigned long start_pfn) 174{ 175 unsigned long mapsize, section_nr, i; 176 struct mem_section *ms; 177 struct page *page, *memmap; 178 struct mem_section_usage *usage; 179 180 section_nr = pfn_to_section_nr(start_pfn); 181 ms = __nr_to_section(section_nr); 182 183 /* Get section's memmap address */ 184 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 185 186 /* 187 * Get page for the memmap's phys address 188 * XXX: need more consideration for sparse_vmemmap... 189 */ 190 page = virt_to_page(memmap); 191 mapsize = sizeof(struct page) * PAGES_PER_SECTION; 192 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; 193 194 /* remember memmap's page */ 195 for (i = 0; i < mapsize; i++, page++) 196 get_page_bootmem(section_nr, page, SECTION_INFO); 197 198 usage = ms->usage; 199 page = virt_to_page(usage); 200 201 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; 202 203 for (i = 0; i < mapsize; i++, page++) 204 get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 205 206} 207#else /* CONFIG_SPARSEMEM_VMEMMAP */ 208static void register_page_bootmem_info_section(unsigned long start_pfn) 209{ 210 unsigned long mapsize, section_nr, i; 211 struct mem_section *ms; 212 struct page *page, *memmap; 213 struct mem_section_usage *usage; 214 215 section_nr = pfn_to_section_nr(start_pfn); 216 ms = __nr_to_section(section_nr); 217 218 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); 219 220 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); 221 222 usage = ms->usage; 223 page = virt_to_page(usage); 224 225 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; 226 227 for (i = 0; i < mapsize; i++, page++) 228 get_page_bootmem(section_nr, page, MIX_SECTION_INFO); 229} 230#endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 231 232void __init register_page_bootmem_info_node(struct pglist_data *pgdat) 233{ 234 unsigned long i, pfn, end_pfn, nr_pages; 235 int node = pgdat->node_id; 236 struct page *page; 237 238 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; 239 page = virt_to_page(pgdat); 240 241 for (i = 0; i < nr_pages; i++, page++) 242 get_page_bootmem(node, page, NODE_INFO); 243 244 pfn = pgdat->node_start_pfn; 245 end_pfn = pgdat_end_pfn(pgdat); 246 247 /* register section info */ 248 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 249 /* 250 * Some platforms can assign the same pfn to multiple nodes - on 251 * node0 as well as nodeN. To avoid registering a pfn against 252 * multiple nodes we check that this pfn does not already 253 * reside in some other nodes. 254 */ 255 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) 256 register_page_bootmem_info_section(pfn); 257 } 258} 259#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ 260 261static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, 262 const char *reason) 263{ 264 /* 265 * Disallow all operations smaller than a sub-section and only 266 * allow operations smaller than a section for 267 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range() 268 * enforces a larger memory_block_size_bytes() granularity for 269 * memory that will be marked online, so this check should only 270 * fire for direct arch_{add,remove}_memory() users outside of 271 * add_memory_resource(). 272 */ 273 unsigned long min_align; 274 275 if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 276 min_align = PAGES_PER_SUBSECTION; 277 else 278 min_align = PAGES_PER_SECTION; 279 if (!IS_ALIGNED(pfn, min_align) 280 || !IS_ALIGNED(nr_pages, min_align)) { 281 WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n", 282 reason, pfn, pfn + nr_pages - 1); 283 return -EINVAL; 284 } 285 return 0; 286} 287 288static int check_hotplug_memory_addressable(unsigned long pfn, 289 unsigned long nr_pages) 290{ 291 const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1; 292 293 if (max_addr >> MAX_PHYSMEM_BITS) { 294 const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1; 295 WARN(1, 296 "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n", 297 (u64)PFN_PHYS(pfn), max_addr, max_allowed); 298 return -E2BIG; 299 } 300 301 return 0; 302} 303 304/* 305 * Reasonably generic function for adding memory. It is 306 * expected that archs that support memory hotplug will 307 * call this function after deciding the zone to which to 308 * add the new pages. 309 */ 310int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, 311 struct mhp_params *params) 312{ 313 const unsigned long end_pfn = pfn + nr_pages; 314 unsigned long cur_nr_pages; 315 int err; 316 struct vmem_altmap *altmap = params->altmap; 317 318 if (WARN_ON_ONCE(!params->pgprot.pgprot)) 319 return -EINVAL; 320 321 err = check_hotplug_memory_addressable(pfn, nr_pages); 322 if (err) 323 return err; 324 325 if (altmap) { 326 /* 327 * Validate altmap is within bounds of the total request 328 */ 329 if (altmap->base_pfn != pfn 330 || vmem_altmap_offset(altmap) > nr_pages) { 331 pr_warn_once("memory add fail, invalid altmap\n"); 332 return -EINVAL; 333 } 334 altmap->alloc = 0; 335 } 336 337 err = check_pfn_span(pfn, nr_pages, "add"); 338 if (err) 339 return err; 340 341 for (; pfn < end_pfn; pfn += cur_nr_pages) { 342 /* Select all remaining pages up to the next section boundary */ 343 cur_nr_pages = min(end_pfn - pfn, 344 SECTION_ALIGN_UP(pfn + 1) - pfn); 345 err = sparse_add_section(nid, pfn, cur_nr_pages, altmap); 346 if (err) 347 break; 348 cond_resched(); 349 } 350 vmemmap_populate_print_last(); 351 return err; 352} 353 354/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ 355static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, 356 unsigned long start_pfn, 357 unsigned long end_pfn) 358{ 359 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) { 360 if (unlikely(!pfn_to_online_page(start_pfn))) 361 continue; 362 363 if (unlikely(pfn_to_nid(start_pfn) != nid)) 364 continue; 365 366 if (zone != page_zone(pfn_to_page(start_pfn))) 367 continue; 368 369 return start_pfn; 370 } 371 372 return 0; 373} 374 375/* find the biggest valid pfn in the range [start_pfn, end_pfn). */ 376static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, 377 unsigned long start_pfn, 378 unsigned long end_pfn) 379{ 380 unsigned long pfn; 381 382 /* pfn is the end pfn of a memory section. */ 383 pfn = end_pfn - 1; 384 for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) { 385 if (unlikely(!pfn_to_online_page(pfn))) 386 continue; 387 388 if (unlikely(pfn_to_nid(pfn) != nid)) 389 continue; 390 391 if (zone != page_zone(pfn_to_page(pfn))) 392 continue; 393 394 return pfn; 395 } 396 397 return 0; 398} 399 400static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, 401 unsigned long end_pfn) 402{ 403 unsigned long pfn; 404 int nid = zone_to_nid(zone); 405 406 zone_span_writelock(zone); 407 if (zone->zone_start_pfn == start_pfn) { 408 /* 409 * If the section is smallest section in the zone, it need 410 * shrink zone->zone_start_pfn and zone->zone_spanned_pages. 411 * In this case, we find second smallest valid mem_section 412 * for shrinking zone. 413 */ 414 pfn = find_smallest_section_pfn(nid, zone, end_pfn, 415 zone_end_pfn(zone)); 416 if (pfn) { 417 zone->spanned_pages = zone_end_pfn(zone) - pfn; 418 zone->zone_start_pfn = pfn; 419 } else { 420 zone->zone_start_pfn = 0; 421 zone->spanned_pages = 0; 422 } 423 } else if (zone_end_pfn(zone) == end_pfn) { 424 /* 425 * If the section is biggest section in the zone, it need 426 * shrink zone->spanned_pages. 427 * In this case, we find second biggest valid mem_section for 428 * shrinking zone. 429 */ 430 pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn, 431 start_pfn); 432 if (pfn) 433 zone->spanned_pages = pfn - zone->zone_start_pfn + 1; 434 else { 435 zone->zone_start_pfn = 0; 436 zone->spanned_pages = 0; 437 } 438 } 439 zone_span_writeunlock(zone); 440} 441 442static void update_pgdat_span(struct pglist_data *pgdat) 443{ 444 unsigned long node_start_pfn = 0, node_end_pfn = 0; 445 struct zone *zone; 446 447 for (zone = pgdat->node_zones; 448 zone < pgdat->node_zones + MAX_NR_ZONES; zone++) { 449 unsigned long zone_end_pfn = zone->zone_start_pfn + 450 zone->spanned_pages; 451 452 /* No need to lock the zones, they can't change. */ 453 if (!zone->spanned_pages) 454 continue; 455 if (!node_end_pfn) { 456 node_start_pfn = zone->zone_start_pfn; 457 node_end_pfn = zone_end_pfn; 458 continue; 459 } 460 461 if (zone_end_pfn > node_end_pfn) 462 node_end_pfn = zone_end_pfn; 463 if (zone->zone_start_pfn < node_start_pfn) 464 node_start_pfn = zone->zone_start_pfn; 465 } 466 467 pgdat->node_start_pfn = node_start_pfn; 468 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn; 469} 470 471void __ref remove_pfn_range_from_zone(struct zone *zone, 472 unsigned long start_pfn, 473 unsigned long nr_pages) 474{ 475 const unsigned long end_pfn = start_pfn + nr_pages; 476 struct pglist_data *pgdat = zone->zone_pgdat; 477 unsigned long pfn, cur_nr_pages, flags; 478 479 /* Poison struct pages because they are now uninitialized again. */ 480 for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) { 481 cond_resched(); 482 483 /* Select all remaining pages up to the next section boundary */ 484 cur_nr_pages = 485 min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn); 486 page_init_poison(pfn_to_page(pfn), 487 sizeof(struct page) * cur_nr_pages); 488 } 489 490#ifdef CONFIG_ZONE_DEVICE 491 /* 492 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So 493 * we will not try to shrink the zones - which is okay as 494 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way. 495 */ 496 if (zone_idx(zone) == ZONE_DEVICE) 497 return; 498#endif 499 500 clear_zone_contiguous(zone); 501 502 pgdat_resize_lock(zone->zone_pgdat, &flags); 503 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); 504 update_pgdat_span(pgdat); 505 pgdat_resize_unlock(zone->zone_pgdat, &flags); 506 507 set_zone_contiguous(zone); 508} 509 510static void __remove_section(unsigned long pfn, unsigned long nr_pages, 511 unsigned long map_offset, 512 struct vmem_altmap *altmap) 513{ 514 struct mem_section *ms = __pfn_to_section(pfn); 515 516 if (WARN_ON_ONCE(!valid_section(ms))) 517 return; 518 519 sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap); 520} 521 522/** 523 * __remove_pages() - remove sections of pages 524 * @pfn: starting pageframe (must be aligned to start of a section) 525 * @nr_pages: number of pages to remove (must be multiple of section size) 526 * @altmap: alternative device page map or %NULL if default memmap is used 527 * 528 * Generic helper function to remove section mappings and sysfs entries 529 * for the section of the memory we are removing. Caller needs to make 530 * sure that pages are marked reserved and zones are adjust properly by 531 * calling offline_pages(). 532 */ 533void __remove_pages(unsigned long pfn, unsigned long nr_pages, 534 struct vmem_altmap *altmap) 535{ 536 const unsigned long end_pfn = pfn + nr_pages; 537 unsigned long cur_nr_pages; 538 unsigned long map_offset = 0; 539 540 map_offset = vmem_altmap_offset(altmap); 541 542 if (check_pfn_span(pfn, nr_pages, "remove")) 543 return; 544 545 for (; pfn < end_pfn; pfn += cur_nr_pages) { 546 cond_resched(); 547 /* Select all remaining pages up to the next section boundary */ 548 cur_nr_pages = min(end_pfn - pfn, 549 SECTION_ALIGN_UP(pfn + 1) - pfn); 550 __remove_section(pfn, cur_nr_pages, map_offset, altmap); 551 map_offset = 0; 552 } 553} 554 555int set_online_page_callback(online_page_callback_t callback) 556{ 557 int rc = -EINVAL; 558 559 get_online_mems(); 560 mutex_lock(&online_page_callback_lock); 561 562 if (online_page_callback == generic_online_page) { 563 online_page_callback = callback; 564 rc = 0; 565 } 566 567 mutex_unlock(&online_page_callback_lock); 568 put_online_mems(); 569 570 return rc; 571} 572EXPORT_SYMBOL_GPL(set_online_page_callback); 573 574int restore_online_page_callback(online_page_callback_t callback) 575{ 576 int rc = -EINVAL; 577 578 get_online_mems(); 579 mutex_lock(&online_page_callback_lock); 580 581 if (online_page_callback == callback) { 582 online_page_callback = generic_online_page; 583 rc = 0; 584 } 585 586 mutex_unlock(&online_page_callback_lock); 587 put_online_mems(); 588 589 return rc; 590} 591EXPORT_SYMBOL_GPL(restore_online_page_callback); 592 593void generic_online_page(struct page *page, unsigned int order) 594{ 595 /* 596 * Freeing the page with debug_pagealloc enabled will try to unmap it, 597 * so we should map it first. This is better than introducing a special 598 * case in page freeing fast path. 599 */ 600 if (debug_pagealloc_enabled_static()) 601 kernel_map_pages(page, 1 << order, 1); 602 __free_pages_core(page, order); 603 totalram_pages_add(1UL << order); 604#ifdef CONFIG_HIGHMEM 605 if (PageHighMem(page)) 606 totalhigh_pages_add(1UL << order); 607#endif 608} 609EXPORT_SYMBOL_GPL(generic_online_page); 610 611static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages) 612{ 613 const unsigned long end_pfn = start_pfn + nr_pages; 614 unsigned long pfn; 615 616 /* 617 * Online the pages in MAX_ORDER - 1 aligned chunks. The callback might 618 * decide to not expose all pages to the buddy (e.g., expose them 619 * later). We account all pages as being online and belonging to this 620 * zone ("present"). 621 */ 622 for (pfn = start_pfn; pfn < end_pfn; pfn += MAX_ORDER_NR_PAGES) 623 (*online_page_callback)(pfn_to_page(pfn), MAX_ORDER - 1); 624 625 /* mark all involved sections as online */ 626 online_mem_sections(start_pfn, end_pfn); 627} 628 629/* check which state of node_states will be changed when online memory */ 630static void node_states_check_changes_online(unsigned long nr_pages, 631 struct zone *zone, struct memory_notify *arg) 632{ 633 int nid = zone_to_nid(zone); 634 635 arg->status_change_nid = NUMA_NO_NODE; 636 arg->status_change_nid_normal = NUMA_NO_NODE; 637 arg->status_change_nid_high = NUMA_NO_NODE; 638 639 if (!node_state(nid, N_MEMORY)) 640 arg->status_change_nid = nid; 641 if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) 642 arg->status_change_nid_normal = nid; 643#ifdef CONFIG_HIGHMEM 644 if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY)) 645 arg->status_change_nid_high = nid; 646#endif 647} 648 649static void node_states_set_node(int node, struct memory_notify *arg) 650{ 651 if (arg->status_change_nid_normal >= 0) 652 node_set_state(node, N_NORMAL_MEMORY); 653 654 if (arg->status_change_nid_high >= 0) 655 node_set_state(node, N_HIGH_MEMORY); 656 657 if (arg->status_change_nid >= 0) 658 node_set_state(node, N_MEMORY); 659} 660 661static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, 662 unsigned long nr_pages) 663{ 664 unsigned long old_end_pfn = zone_end_pfn(zone); 665 666 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) 667 zone->zone_start_pfn = start_pfn; 668 669 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; 670} 671 672static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, 673 unsigned long nr_pages) 674{ 675 unsigned long old_end_pfn = pgdat_end_pfn(pgdat); 676 677 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) 678 pgdat->node_start_pfn = start_pfn; 679 680 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; 681 682} 683/* 684 * Associate the pfn range with the given zone, initializing the memmaps 685 * and resizing the pgdat/zone data to span the added pages. After this 686 * call, all affected pages are PG_reserved. 687 * 688 * All aligned pageblocks are initialized to the specified migratetype 689 * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related 690 * zone stats (e.g., nr_isolate_pageblock) are touched. 691 */ 692void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, 693 unsigned long nr_pages, 694 struct vmem_altmap *altmap, int migratetype) 695{ 696 struct pglist_data *pgdat = zone->zone_pgdat; 697 int nid = pgdat->node_id; 698 unsigned long flags; 699 700 clear_zone_contiguous(zone); 701 702 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ 703 pgdat_resize_lock(pgdat, &flags); 704 zone_span_writelock(zone); 705 if (zone_is_empty(zone)) 706 init_currently_empty_zone(zone, start_pfn, nr_pages); 707 resize_zone_range(zone, start_pfn, nr_pages); 708 zone_span_writeunlock(zone); 709 resize_pgdat_range(pgdat, start_pfn, nr_pages); 710 pgdat_resize_unlock(pgdat, &flags); 711 712 /* 713 * TODO now we have a visible range of pages which are not associated 714 * with their zone properly. Not nice but set_pfnblock_flags_mask 715 * expects the zone spans the pfn range. All the pages in the range 716 * are reserved so nobody should be touching them so we should be safe 717 */ 718 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, 0, 719 MEMINIT_HOTPLUG, altmap, migratetype); 720 721 set_zone_contiguous(zone); 722} 723 724/* 725 * Returns a default kernel memory zone for the given pfn range. 726 * If no kernel zone covers this pfn range it will automatically go 727 * to the ZONE_NORMAL. 728 */ 729static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, 730 unsigned long nr_pages) 731{ 732 struct pglist_data *pgdat = NODE_DATA(nid); 733 int zid; 734 735 for (zid = 0; zid <= ZONE_NORMAL; zid++) { 736 struct zone *zone = &pgdat->node_zones[zid]; 737 738 if (zone_intersects(zone, start_pfn, nr_pages)) 739 return zone; 740 } 741 742 return &pgdat->node_zones[ZONE_NORMAL]; 743} 744 745static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, 746 unsigned long nr_pages) 747{ 748 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, 749 nr_pages); 750 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 751 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); 752 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); 753 754 /* 755 * We inherit the existing zone in a simple case where zones do not 756 * overlap in the given range 757 */ 758 if (in_kernel ^ in_movable) 759 return (in_kernel) ? kernel_zone : movable_zone; 760 761 /* 762 * If the range doesn't belong to any zone or two zones overlap in the 763 * given range then we use movable zone only if movable_node is 764 * enabled because we always online to a kernel zone by default. 765 */ 766 return movable_node_enabled ? movable_zone : kernel_zone; 767} 768 769struct zone *zone_for_pfn_range(int online_type, int nid, 770 unsigned long start_pfn, unsigned long nr_pages) 771{ 772 if (online_type == MMOP_ONLINE_KERNEL) 773 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); 774 775 if (online_type == MMOP_ONLINE_MOVABLE) 776 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; 777 778 return default_zone_for_pfn(nid, start_pfn, nr_pages); 779} 780 781int __ref online_pages(unsigned long pfn, unsigned long nr_pages, 782 int online_type, int nid) 783{ 784 unsigned long flags; 785 struct zone *zone; 786 int need_zonelists_rebuild = 0; 787 int ret; 788 struct memory_notify arg; 789 790 /* We can only online full sections (e.g., SECTION_IS_ONLINE) */ 791 if (WARN_ON_ONCE(!nr_pages || 792 !IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))) 793 return -EINVAL; 794 795 mem_hotplug_begin(); 796 797 /* associate pfn range with the zone */ 798 zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages); 799 move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE); 800 801 arg.start_pfn = pfn; 802 arg.nr_pages = nr_pages; 803 node_states_check_changes_online(nr_pages, zone, &arg); 804 805 ret = memory_notify(MEM_GOING_ONLINE, &arg); 806 ret = notifier_to_errno(ret); 807 if (ret) 808 goto failed_addition; 809 810 /* 811 * Fixup the number of isolated pageblocks before marking the sections 812 * onlining, such that undo_isolate_page_range() works correctly. 813 */ 814 spin_lock_irqsave(&zone->lock, flags); 815 zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages; 816 spin_unlock_irqrestore(&zone->lock, flags); 817 818 /* 819 * If this zone is not populated, then it is not in zonelist. 820 * This means the page allocator ignores this zone. 821 * So, zonelist must be updated after online. 822 */ 823 if (!populated_zone(zone)) { 824 need_zonelists_rebuild = 1; 825 setup_zone_pageset(zone); 826 } 827 828 online_pages_range(pfn, nr_pages); 829 zone->present_pages += nr_pages; 830 831 pgdat_resize_lock(zone->zone_pgdat, &flags); 832 zone->zone_pgdat->node_present_pages += nr_pages; 833 pgdat_resize_unlock(zone->zone_pgdat, &flags); 834 835 node_states_set_node(nid, &arg); 836 if (need_zonelists_rebuild) 837 build_all_zonelists(NULL); 838 zone_pcp_update(zone); 839 840 /* Basic onlining is complete, allow allocation of onlined pages. */ 841 undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE); 842 843 /* 844 * Freshly onlined pages aren't shuffled (e.g., all pages are placed to 845 * the tail of the freelist when undoing isolation). Shuffle the whole 846 * zone to make sure the just onlined pages are properly distributed 847 * across the whole freelist - to create an initial shuffle. 848 */ 849 shuffle_zone(zone); 850 851 init_per_zone_wmark_min(); 852 853 kswapd_run(nid); 854 kcompactd_run(nid); 855#ifdef CONFIG_HYPERHOLD_ZSWAPD 856 zswapd_run(nid); 857#endif 858 859 writeback_set_ratelimit(); 860 861 memory_notify(MEM_ONLINE, &arg); 862 mem_hotplug_done(); 863 return 0; 864 865failed_addition: 866 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", 867 (unsigned long long) pfn << PAGE_SHIFT, 868 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); 869 memory_notify(MEM_CANCEL_ONLINE, &arg); 870 remove_pfn_range_from_zone(zone, pfn, nr_pages); 871 mem_hotplug_done(); 872 return ret; 873} 874#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ 875 876static void reset_node_present_pages(pg_data_t *pgdat) 877{ 878 struct zone *z; 879 880 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 881 z->present_pages = 0; 882 883 pgdat->node_present_pages = 0; 884} 885 886/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ 887static pg_data_t __ref *hotadd_new_pgdat(int nid) 888{ 889 struct pglist_data *pgdat; 890 891 pgdat = NODE_DATA(nid); 892 if (!pgdat) { 893 pgdat = arch_alloc_nodedata(nid); 894 if (!pgdat) 895 return NULL; 896 897 pgdat->per_cpu_nodestats = 898 alloc_percpu(struct per_cpu_nodestat); 899 arch_refresh_nodedata(nid, pgdat); 900 } else { 901 int cpu; 902 /* 903 * Reset the nr_zones, order and highest_zoneidx before reuse. 904 * Note that kswapd will init kswapd_highest_zoneidx properly 905 * when it starts in the near future. 906 */ 907 pgdat->nr_zones = 0; 908 pgdat->kswapd_order = 0; 909 pgdat->kswapd_highest_zoneidx = 0; 910 for_each_online_cpu(cpu) { 911 struct per_cpu_nodestat *p; 912 913 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); 914 memset(p, 0, sizeof(*p)); 915 } 916 } 917 918 /* we can use NODE_DATA(nid) from here */ 919 pgdat->node_id = nid; 920 pgdat->node_start_pfn = 0; 921 922 /* init node's zones as empty zones, we don't have any present pages.*/ 923 free_area_init_core_hotplug(nid); 924 925 /* 926 * The node we allocated has no zone fallback lists. For avoiding 927 * to access not-initialized zonelist, build here. 928 */ 929 build_all_zonelists(pgdat); 930 931 /* 932 * When memory is hot-added, all the memory is in offline state. So 933 * clear all zones' present_pages because they will be updated in 934 * online_pages() and offline_pages(). 935 */ 936 reset_node_managed_pages(pgdat); 937 reset_node_present_pages(pgdat); 938 939 return pgdat; 940} 941 942static void rollback_node_hotadd(int nid) 943{ 944 pg_data_t *pgdat = NODE_DATA(nid); 945 946 arch_refresh_nodedata(nid, NULL); 947 free_percpu(pgdat->per_cpu_nodestats); 948 arch_free_nodedata(pgdat); 949} 950 951 952/** 953 * try_online_node - online a node if offlined 954 * @nid: the node ID 955 * @set_node_online: Whether we want to online the node 956 * called by cpu_up() to online a node without onlined memory. 957 * 958 * Returns: 959 * 1 -> a new node has been allocated 960 * 0 -> the node is already online 961 * -ENOMEM -> the node could not be allocated 962 */ 963static int __try_online_node(int nid, bool set_node_online) 964{ 965 pg_data_t *pgdat; 966 int ret = 1; 967 968 if (node_online(nid)) 969 return 0; 970 971 pgdat = hotadd_new_pgdat(nid); 972 if (!pgdat) { 973 pr_err("Cannot online node %d due to NULL pgdat\n", nid); 974 ret = -ENOMEM; 975 goto out; 976 } 977 978 if (set_node_online) { 979 node_set_online(nid); 980 ret = register_one_node(nid); 981 BUG_ON(ret); 982 } 983out: 984 return ret; 985} 986 987/* 988 * Users of this function always want to online/register the node 989 */ 990int try_online_node(int nid) 991{ 992 int ret; 993 994 mem_hotplug_begin(); 995 ret = __try_online_node(nid, true); 996 mem_hotplug_done(); 997 return ret; 998} 999 1000static int check_hotplug_memory_range(u64 start, u64 size) 1001{ 1002 /* memory range must be block size aligned */ 1003 if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || 1004 !IS_ALIGNED(size, memory_block_size_bytes())) { 1005 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", 1006 memory_block_size_bytes(), start, size); 1007 return -EINVAL; 1008 } 1009 1010 return 0; 1011} 1012 1013static int online_memory_block(struct memory_block *mem, void *arg) 1014{ 1015 mem->online_type = memhp_default_online_type; 1016 return device_online(&mem->dev); 1017} 1018 1019/* 1020 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 1021 * and online/offline operations (triggered e.g. by sysfs). 1022 * 1023 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG 1024 */ 1025int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) 1026{ 1027 struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) }; 1028 u64 start, size; 1029 bool new_node = false; 1030 int ret; 1031 1032 start = res->start; 1033 size = resource_size(res); 1034 1035 ret = check_hotplug_memory_range(start, size); 1036 if (ret) 1037 return ret; 1038 1039 if (!node_possible(nid)) { 1040 WARN(1, "node %d was absent from the node_possible_map\n", nid); 1041 return -EINVAL; 1042 } 1043 1044 mem_hotplug_begin(); 1045 1046 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 1047 memblock_add_node(start, size, nid); 1048 1049 ret = __try_online_node(nid, false); 1050 if (ret < 0) 1051 goto error; 1052 new_node = ret; 1053 1054 /* call arch's memory hotadd */ 1055 ret = arch_add_memory(nid, start, size, ¶ms); 1056 if (ret < 0) 1057 goto error; 1058 1059 /* create memory block devices after memory was added */ 1060 ret = create_memory_block_devices(start, size); 1061 if (ret) { 1062 arch_remove_memory(nid, start, size, NULL); 1063 goto error; 1064 } 1065 1066 if (new_node) { 1067 /* If sysfs file of new node can't be created, cpu on the node 1068 * can't be hot-added. There is no rollback way now. 1069 * So, check by BUG_ON() to catch it reluctantly.. 1070 * We online node here. We can't roll back from here. 1071 */ 1072 node_set_online(nid); 1073 ret = __register_one_node(nid); 1074 BUG_ON(ret); 1075 } 1076 1077 /* link memory sections under this node.*/ 1078 link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1), 1079 MEMINIT_HOTPLUG); 1080 1081 /* create new memmap entry */ 1082 if (!strcmp(res->name, "System RAM")) 1083 firmware_map_add_hotplug(start, start + size, "System RAM"); 1084 1085 /* device_online() will take the lock when calling online_pages() */ 1086 mem_hotplug_done(); 1087 1088 /* 1089 * In case we're allowed to merge the resource, flag it and trigger 1090 * merging now that adding succeeded. 1091 */ 1092 if (mhp_flags & MEMHP_MERGE_RESOURCE) 1093 merge_system_ram_resource(res); 1094 1095 /* online pages if requested */ 1096 if (memhp_default_online_type != MMOP_OFFLINE) 1097 walk_memory_blocks(start, size, NULL, online_memory_block); 1098 1099 return ret; 1100error: 1101 /* rollback pgdat allocation and others */ 1102 if (new_node) 1103 rollback_node_hotadd(nid); 1104 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 1105 memblock_remove(start, size); 1106 mem_hotplug_done(); 1107 return ret; 1108} 1109 1110/* requires device_hotplug_lock, see add_memory_resource() */ 1111int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 1112{ 1113 struct resource *res; 1114 int ret; 1115 1116 res = register_memory_resource(start, size, "System RAM"); 1117 if (IS_ERR(res)) 1118 return PTR_ERR(res); 1119 1120 ret = add_memory_resource(nid, res, mhp_flags); 1121 if (ret < 0) 1122 release_memory_resource(res); 1123 return ret; 1124} 1125 1126int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags) 1127{ 1128 int rc; 1129 1130 lock_device_hotplug(); 1131 rc = __add_memory(nid, start, size, mhp_flags); 1132 unlock_device_hotplug(); 1133 1134 return rc; 1135} 1136EXPORT_SYMBOL_GPL(add_memory); 1137 1138/* 1139 * Add special, driver-managed memory to the system as system RAM. Such 1140 * memory is not exposed via the raw firmware-provided memmap as system 1141 * RAM, instead, it is detected and added by a driver - during cold boot, 1142 * after a reboot, and after kexec. 1143 * 1144 * Reasons why this memory should not be used for the initial memmap of a 1145 * kexec kernel or for placing kexec images: 1146 * - The booting kernel is in charge of determining how this memory will be 1147 * used (e.g., use persistent memory as system RAM) 1148 * - Coordination with a hypervisor is required before this memory 1149 * can be used (e.g., inaccessible parts). 1150 * 1151 * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided 1152 * memory map") are created. Also, the created memory resource is flagged 1153 * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case 1154 * this memory as well (esp., not place kexec images onto it). 1155 * 1156 * The resource_name (visible via /proc/iomem) has to have the format 1157 * "System RAM ($DRIVER)". 1158 */ 1159int add_memory_driver_managed(int nid, u64 start, u64 size, 1160 const char *resource_name, mhp_t mhp_flags) 1161{ 1162 struct resource *res; 1163 int rc; 1164 1165 if (!resource_name || 1166 strstr(resource_name, "System RAM (") != resource_name || 1167 resource_name[strlen(resource_name) - 1] != ')') 1168 return -EINVAL; 1169 1170 lock_device_hotplug(); 1171 1172 res = register_memory_resource(start, size, resource_name); 1173 if (IS_ERR(res)) { 1174 rc = PTR_ERR(res); 1175 goto out_unlock; 1176 } 1177 1178 rc = add_memory_resource(nid, res, mhp_flags); 1179 if (rc < 0) 1180 release_memory_resource(res); 1181 1182out_unlock: 1183 unlock_device_hotplug(); 1184 return rc; 1185} 1186EXPORT_SYMBOL_GPL(add_memory_driver_managed); 1187 1188#ifdef CONFIG_MEMORY_HOTREMOVE 1189/* 1190 * Confirm all pages in a range [start, end) belong to the same zone (skipping 1191 * memory holes). When true, return the zone. 1192 */ 1193struct zone *test_pages_in_a_zone(unsigned long start_pfn, 1194 unsigned long end_pfn) 1195{ 1196 unsigned long pfn, sec_end_pfn; 1197 struct zone *zone = NULL; 1198 struct page *page; 1199 int i; 1200 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1); 1201 pfn < end_pfn; 1202 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) { 1203 /* Make sure the memory section is present first */ 1204 if (!present_section_nr(pfn_to_section_nr(pfn))) 1205 continue; 1206 for (; pfn < sec_end_pfn && pfn < end_pfn; 1207 pfn += MAX_ORDER_NR_PAGES) { 1208 i = 0; 1209 /* This is just a CONFIG_HOLES_IN_ZONE check.*/ 1210 while ((i < MAX_ORDER_NR_PAGES) && 1211 !pfn_valid_within(pfn + i)) 1212 i++; 1213 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn) 1214 continue; 1215 /* Check if we got outside of the zone */ 1216 if (zone && !zone_spans_pfn(zone, pfn + i)) 1217 return NULL; 1218 page = pfn_to_page(pfn + i); 1219 if (zone && page_zone(page) != zone) 1220 return NULL; 1221 zone = page_zone(page); 1222 } 1223 } 1224 1225 return zone; 1226} 1227 1228/* 1229 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, 1230 * non-lru movable pages and hugepages). Will skip over most unmovable 1231 * pages (esp., pages that can be skipped when offlining), but bail out on 1232 * definitely unmovable pages. 1233 * 1234 * Returns: 1235 * 0 in case a movable page is found and movable_pfn was updated. 1236 * -ENOENT in case no movable page was found. 1237 * -EBUSY in case a definitely unmovable page was found. 1238 */ 1239static int scan_movable_pages(unsigned long start, unsigned long end, 1240 unsigned long *movable_pfn) 1241{ 1242 unsigned long pfn; 1243 1244 for (pfn = start; pfn < end; pfn++) { 1245 struct page *page, *head; 1246 unsigned long skip; 1247 1248 if (!pfn_valid(pfn)) 1249 continue; 1250 page = pfn_to_page(pfn); 1251 if (PageLRU(page)) 1252 goto found; 1253 if (__PageMovable(page)) 1254 goto found; 1255 1256 /* 1257 * PageOffline() pages that are not marked __PageMovable() and 1258 * have a reference count > 0 (after MEM_GOING_OFFLINE) are 1259 * definitely unmovable. If their reference count would be 0, 1260 * they could at least be skipped when offlining memory. 1261 */ 1262 if (PageOffline(page) && page_count(page)) 1263 return -EBUSY; 1264 1265 if (!PageHuge(page)) 1266 continue; 1267 head = compound_head(page); 1268 if (page_huge_active(head)) 1269 goto found; 1270 skip = compound_nr(head) - (pfn - page_to_pfn(head)); 1271 pfn += skip - 1; 1272 } 1273 return -ENOENT; 1274found: 1275 *movable_pfn = pfn; 1276 return 0; 1277} 1278 1279static int 1280do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) 1281{ 1282 unsigned long pfn; 1283 struct page *page, *head; 1284 int ret = 0; 1285 LIST_HEAD(source); 1286 static DEFINE_RATELIMIT_STATE(migrate_rs, DEFAULT_RATELIMIT_INTERVAL, 1287 DEFAULT_RATELIMIT_BURST); 1288 1289 for (pfn = start_pfn; pfn < end_pfn; pfn++) { 1290 if (!pfn_valid(pfn)) 1291 continue; 1292 page = pfn_to_page(pfn); 1293 head = compound_head(page); 1294 1295 if (PageHuge(page)) { 1296 pfn = page_to_pfn(head) + compound_nr(head) - 1; 1297 isolate_hugetlb(head, &source); 1298 continue; 1299 } else if (PageTransHuge(page)) 1300 pfn = page_to_pfn(head) + thp_nr_pages(page) - 1; 1301 1302 /* 1303 * HWPoison pages have elevated reference counts so the migration would 1304 * fail on them. It also doesn't make any sense to migrate them in the 1305 * first place. Still try to unmap such a page in case it is still mapped 1306 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep 1307 * the unmap as the catch all safety net). 1308 */ 1309 if (PageHWPoison(page)) { 1310 if (WARN_ON(PageLRU(page))) 1311 isolate_lru_page(page); 1312 if (page_mapped(page)) 1313 try_to_unmap(page, TTU_IGNORE_MLOCK); 1314 continue; 1315 } 1316 1317 if (!get_page_unless_zero(page)) 1318 continue; 1319 /* 1320 * We can skip free pages. And we can deal with pages on 1321 * LRU and non-lru movable pages. 1322 */ 1323 if (PageLRU(page)) 1324 ret = isolate_lru_page(page); 1325 else 1326 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); 1327 if (!ret) { /* Success */ 1328 list_add_tail(&page->lru, &source); 1329 if (!__PageMovable(page)) 1330 inc_node_page_state(page, NR_ISOLATED_ANON + 1331 page_is_file_lru(page)); 1332 1333 } else { 1334 if (__ratelimit(&migrate_rs)) { 1335 pr_warn("failed to isolate pfn %lx\n", pfn); 1336 dump_page(page, "isolation failed"); 1337 } 1338 } 1339 put_page(page); 1340 } 1341 if (!list_empty(&source)) { 1342 nodemask_t nmask = node_states[N_MEMORY]; 1343 struct migration_target_control mtc = { 1344 .nmask = &nmask, 1345 .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, 1346 }; 1347 1348 /* 1349 * We have checked that migration range is on a single zone so 1350 * we can use the nid of the first page to all the others. 1351 */ 1352 mtc.nid = page_to_nid(list_first_entry(&source, struct page, lru)); 1353 1354 /* 1355 * try to allocate from a different node but reuse this node 1356 * if there are no other online nodes to be used (e.g. we are 1357 * offlining a part of the only existing node) 1358 */ 1359 node_clear(mtc.nid, nmask); 1360 if (nodes_empty(nmask)) 1361 node_set(mtc.nid, nmask); 1362 ret = migrate_pages(&source, alloc_migration_target, NULL, 1363 (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG); 1364 if (ret) { 1365 list_for_each_entry(page, &source, lru) { 1366 if (__ratelimit(&migrate_rs)) { 1367 pr_warn("migrating pfn %lx failed ret:%d\n", 1368 page_to_pfn(page), ret); 1369 dump_page(page, "migration failure"); 1370 } 1371 } 1372 putback_movable_pages(&source); 1373 } 1374 } 1375 1376 return ret; 1377} 1378 1379static int __init cmdline_parse_movable_node(char *p) 1380{ 1381 movable_node_enabled = true; 1382 return 0; 1383} 1384early_param("movable_node", cmdline_parse_movable_node); 1385 1386/* check which state of node_states will be changed when offline memory */ 1387static void node_states_check_changes_offline(unsigned long nr_pages, 1388 struct zone *zone, struct memory_notify *arg) 1389{ 1390 struct pglist_data *pgdat = zone->zone_pgdat; 1391 unsigned long present_pages = 0; 1392 enum zone_type zt; 1393 1394 arg->status_change_nid = NUMA_NO_NODE; 1395 arg->status_change_nid_normal = NUMA_NO_NODE; 1396 arg->status_change_nid_high = NUMA_NO_NODE; 1397 1398 /* 1399 * Check whether node_states[N_NORMAL_MEMORY] will be changed. 1400 * If the memory to be offline is within the range 1401 * [0..ZONE_NORMAL], and it is the last present memory there, 1402 * the zones in that range will become empty after the offlining, 1403 * thus we can determine that we need to clear the node from 1404 * node_states[N_NORMAL_MEMORY]. 1405 */ 1406 for (zt = 0; zt <= ZONE_NORMAL; zt++) 1407 present_pages += pgdat->node_zones[zt].present_pages; 1408 if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) 1409 arg->status_change_nid_normal = zone_to_nid(zone); 1410 1411#ifdef CONFIG_HIGHMEM 1412 /* 1413 * node_states[N_HIGH_MEMORY] contains nodes which 1414 * have normal memory or high memory. 1415 * Here we add the present_pages belonging to ZONE_HIGHMEM. 1416 * If the zone is within the range of [0..ZONE_HIGHMEM), and 1417 * we determine that the zones in that range become empty, 1418 * we need to clear the node for N_HIGH_MEMORY. 1419 */ 1420 present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages; 1421 if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages) 1422 arg->status_change_nid_high = zone_to_nid(zone); 1423#endif 1424 1425 /* 1426 * We have accounted the pages from [0..ZONE_NORMAL), and 1427 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM 1428 * as well. 1429 * Here we count the possible pages from ZONE_MOVABLE. 1430 * If after having accounted all the pages, we see that the nr_pages 1431 * to be offlined is over or equal to the accounted pages, 1432 * we know that the node will become empty, and so, we can clear 1433 * it for N_MEMORY as well. 1434 */ 1435 present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; 1436 1437 if (nr_pages >= present_pages) 1438 arg->status_change_nid = zone_to_nid(zone); 1439} 1440 1441static void node_states_clear_node(int node, struct memory_notify *arg) 1442{ 1443 if (arg->status_change_nid_normal >= 0) 1444 node_clear_state(node, N_NORMAL_MEMORY); 1445 1446 if (arg->status_change_nid_high >= 0) 1447 node_clear_state(node, N_HIGH_MEMORY); 1448 1449 if (arg->status_change_nid >= 0) 1450 node_clear_state(node, N_MEMORY); 1451} 1452 1453static int count_system_ram_pages_cb(unsigned long start_pfn, 1454 unsigned long nr_pages, void *data) 1455{ 1456 unsigned long *nr_system_ram_pages = data; 1457 1458 *nr_system_ram_pages += nr_pages; 1459 return 0; 1460} 1461 1462int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages) 1463{ 1464 const unsigned long end_pfn = start_pfn + nr_pages; 1465 unsigned long pfn, system_ram_pages = 0; 1466 unsigned long flags; 1467 struct zone *zone; 1468 struct memory_notify arg; 1469 int ret, node; 1470 char *reason; 1471 1472 /* We can only offline full sections (e.g., SECTION_IS_ONLINE) */ 1473 if (WARN_ON_ONCE(!nr_pages || 1474 !IS_ALIGNED(start_pfn | nr_pages, PAGES_PER_SECTION))) 1475 return -EINVAL; 1476 1477 mem_hotplug_begin(); 1478 1479 /* 1480 * Don't allow to offline memory blocks that contain holes. 1481 * Consequently, memory blocks with holes can never get onlined 1482 * via the hotplug path - online_pages() - as hotplugged memory has 1483 * no holes. This way, we e.g., don't have to worry about marking 1484 * memory holes PG_reserved, don't need pfn_valid() checks, and can 1485 * avoid using walk_system_ram_range() later. 1486 */ 1487 walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages, 1488 count_system_ram_pages_cb); 1489 if (system_ram_pages != nr_pages) { 1490 ret = -EINVAL; 1491 reason = "memory holes"; 1492 goto failed_removal; 1493 } 1494 1495 /* This makes hotplug much easier...and readable. 1496 we assume this for now. .*/ 1497 zone = test_pages_in_a_zone(start_pfn, end_pfn); 1498 if (!zone) { 1499 ret = -EINVAL; 1500 reason = "multizone range"; 1501 goto failed_removal; 1502 } 1503 node = zone_to_nid(zone); 1504 1505 /* set above range as isolated */ 1506 ret = start_isolate_page_range(start_pfn, end_pfn, 1507 MIGRATE_MOVABLE, 1508 MEMORY_OFFLINE | REPORT_FAILURE); 1509 if (ret) { 1510 reason = "failure to isolate range"; 1511 goto failed_removal; 1512 } 1513 1514 arg.start_pfn = start_pfn; 1515 arg.nr_pages = nr_pages; 1516 node_states_check_changes_offline(nr_pages, zone, &arg); 1517 1518 ret = memory_notify(MEM_GOING_OFFLINE, &arg); 1519 ret = notifier_to_errno(ret); 1520 if (ret) { 1521 reason = "notifier failure"; 1522 goto failed_removal_isolated; 1523 } 1524 1525 do { 1526 pfn = start_pfn; 1527 do { 1528 if (signal_pending(current)) { 1529 ret = -EINTR; 1530 reason = "signal backoff"; 1531 goto failed_removal_isolated; 1532 } 1533 1534 cond_resched(); 1535 lru_add_drain_all(); 1536 1537 ret = scan_movable_pages(pfn, end_pfn, &pfn); 1538 if (!ret) { 1539 /* 1540 * TODO: fatal migration failures should bail 1541 * out 1542 */ 1543 do_migrate_range(pfn, end_pfn); 1544 } 1545 } while (!ret); 1546 1547 if (ret != -ENOENT) { 1548 reason = "unmovable page"; 1549 goto failed_removal_isolated; 1550 } 1551 1552 /* 1553 * Dissolve free hugepages in the memory block before doing 1554 * offlining actually in order to make hugetlbfs's object 1555 * counting consistent. 1556 */ 1557 ret = dissolve_free_huge_pages(start_pfn, end_pfn); 1558 if (ret) { 1559 reason = "failure to dissolve huge pages"; 1560 goto failed_removal_isolated; 1561 } 1562 1563 /* 1564 * per-cpu pages are drained in start_isolate_page_range, but if 1565 * there are still pages that are not free, make sure that we 1566 * drain again, because when we isolated range we might 1567 * have raced with another thread that was adding pages to pcp 1568 * list. 1569 * 1570 * Forward progress should be still guaranteed because 1571 * pages on the pcp list can only belong to MOVABLE_ZONE 1572 * because has_unmovable_pages explicitly checks for 1573 * PageBuddy on freed pages on other zones. 1574 */ 1575 ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE); 1576 if (ret) 1577 drain_all_pages(zone); 1578 } while (ret); 1579 1580 /* Mark all sections offline and remove free pages from the buddy. */ 1581 __offline_isolated_pages(start_pfn, end_pfn); 1582 pr_info("Offlined Pages %ld\n", nr_pages); 1583 1584 /* 1585 * The memory sections are marked offline, and the pageblock flags 1586 * effectively stale; nobody should be touching them. Fixup the number 1587 * of isolated pageblocks, memory onlining will properly revert this. 1588 */ 1589 spin_lock_irqsave(&zone->lock, flags); 1590 zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages; 1591 spin_unlock_irqrestore(&zone->lock, flags); 1592 1593 /* removal success */ 1594 adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages); 1595 zone->present_pages -= nr_pages; 1596 1597 pgdat_resize_lock(zone->zone_pgdat, &flags); 1598 zone->zone_pgdat->node_present_pages -= nr_pages; 1599 pgdat_resize_unlock(zone->zone_pgdat, &flags); 1600 1601 init_per_zone_wmark_min(); 1602 1603 if (!populated_zone(zone)) { 1604 zone_pcp_reset(zone); 1605 build_all_zonelists(NULL); 1606 } else 1607 zone_pcp_update(zone); 1608 1609 node_states_clear_node(node, &arg); 1610 if (arg.status_change_nid >= 0) { 1611 kswapd_stop(node); 1612 kcompactd_stop(node); 1613#ifdef CONFIG_HYPERHOLD_ZSWAPD 1614 zswapd_stop(node); 1615#endif 1616 } 1617 1618 writeback_set_ratelimit(); 1619 1620 memory_notify(MEM_OFFLINE, &arg); 1621 remove_pfn_range_from_zone(zone, start_pfn, nr_pages); 1622 mem_hotplug_done(); 1623 return 0; 1624 1625failed_removal_isolated: 1626 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); 1627 memory_notify(MEM_CANCEL_OFFLINE, &arg); 1628failed_removal: 1629 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", 1630 (unsigned long long) start_pfn << PAGE_SHIFT, 1631 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, 1632 reason); 1633 /* pushback to free area */ 1634 mem_hotplug_done(); 1635 return ret; 1636} 1637 1638static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) 1639{ 1640 int ret = !is_memblock_offlined(mem); 1641 1642 if (unlikely(ret)) { 1643 phys_addr_t beginpa, endpa; 1644 1645 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); 1646 endpa = beginpa + memory_block_size_bytes() - 1; 1647 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", 1648 &beginpa, &endpa); 1649 1650 return -EBUSY; 1651 } 1652 return 0; 1653} 1654 1655static int check_cpu_on_node(pg_data_t *pgdat) 1656{ 1657 int cpu; 1658 1659 for_each_present_cpu(cpu) { 1660 if (cpu_to_node(cpu) == pgdat->node_id) 1661 /* 1662 * the cpu on this node isn't removed, and we can't 1663 * offline this node. 1664 */ 1665 return -EBUSY; 1666 } 1667 1668 return 0; 1669} 1670 1671static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg) 1672{ 1673 int nid = *(int *)arg; 1674 1675 /* 1676 * If a memory block belongs to multiple nodes, the stored nid is not 1677 * reliable. However, such blocks are always online (e.g., cannot get 1678 * offlined) and, therefore, are still spanned by the node. 1679 */ 1680 return mem->nid == nid ? -EEXIST : 0; 1681} 1682 1683/** 1684 * try_offline_node 1685 * @nid: the node ID 1686 * 1687 * Offline a node if all memory sections and cpus of the node are removed. 1688 * 1689 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 1690 * and online/offline operations before this call. 1691 */ 1692void try_offline_node(int nid) 1693{ 1694 pg_data_t *pgdat = NODE_DATA(nid); 1695 int rc; 1696 1697 /* 1698 * If the node still spans pages (especially ZONE_DEVICE), don't 1699 * offline it. A node spans memory after move_pfn_range_to_zone(), 1700 * e.g., after the memory block was onlined. 1701 */ 1702 if (pgdat->node_spanned_pages) 1703 return; 1704 1705 /* 1706 * Especially offline memory blocks might not be spanned by the 1707 * node. They will get spanned by the node once they get onlined. 1708 * However, they link to the node in sysfs and can get onlined later. 1709 */ 1710 rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb); 1711 if (rc) 1712 return; 1713 1714 if (check_cpu_on_node(pgdat)) 1715 return; 1716 1717 /* 1718 * all memory/cpu of this node are removed, we can offline this 1719 * node now. 1720 */ 1721 node_set_offline(nid); 1722 unregister_one_node(nid); 1723} 1724EXPORT_SYMBOL(try_offline_node); 1725 1726static int __ref try_remove_memory(int nid, u64 start, u64 size) 1727{ 1728 int rc = 0; 1729 1730 BUG_ON(check_hotplug_memory_range(start, size)); 1731 1732 /* 1733 * All memory blocks must be offlined before removing memory. Check 1734 * whether all memory blocks in question are offline and return error 1735 * if this is not the case. 1736 */ 1737 rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb); 1738 if (rc) 1739 return rc; 1740 1741 /* remove memmap entry */ 1742 firmware_map_remove(start, start + size, "System RAM"); 1743 1744 /* 1745 * Memory block device removal under the device_hotplug_lock is 1746 * a barrier against racing online attempts. 1747 */ 1748 remove_memory_block_devices(start, size); 1749 1750 mem_hotplug_begin(); 1751 1752 arch_remove_memory(nid, start, size, NULL); 1753 1754 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) { 1755 memblock_free(start, size); 1756 memblock_remove(start, size); 1757 } 1758 1759 release_mem_region_adjustable(start, size); 1760 1761 try_offline_node(nid); 1762 1763 mem_hotplug_done(); 1764 return 0; 1765} 1766 1767/** 1768 * remove_memory 1769 * @nid: the node ID 1770 * @start: physical address of the region to remove 1771 * @size: size of the region to remove 1772 * 1773 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug 1774 * and online/offline operations before this call, as required by 1775 * try_offline_node(). 1776 */ 1777void __remove_memory(int nid, u64 start, u64 size) 1778{ 1779 1780 /* 1781 * trigger BUG() if some memory is not offlined prior to calling this 1782 * function 1783 */ 1784 if (try_remove_memory(nid, start, size)) 1785 BUG(); 1786} 1787 1788/* 1789 * Remove memory if every memory block is offline, otherwise return -EBUSY is 1790 * some memory is not offline 1791 */ 1792int remove_memory(int nid, u64 start, u64 size) 1793{ 1794 int rc; 1795 1796 lock_device_hotplug(); 1797 rc = try_remove_memory(nid, start, size); 1798 unlock_device_hotplug(); 1799 1800 return rc; 1801} 1802EXPORT_SYMBOL_GPL(remove_memory); 1803 1804static int try_offline_memory_block(struct memory_block *mem, void *arg) 1805{ 1806 uint8_t online_type = MMOP_ONLINE_KERNEL; 1807 uint8_t **online_types = arg; 1808 struct page *page; 1809 int rc; 1810 1811 /* 1812 * Sense the online_type via the zone of the memory block. Offlining 1813 * with multiple zones within one memory block will be rejected 1814 * by offlining code ... so we don't care about that. 1815 */ 1816 page = pfn_to_online_page(section_nr_to_pfn(mem->start_section_nr)); 1817 if (page && zone_idx(page_zone(page)) == ZONE_MOVABLE) 1818 online_type = MMOP_ONLINE_MOVABLE; 1819 1820 rc = device_offline(&mem->dev); 1821 /* 1822 * Default is MMOP_OFFLINE - change it only if offlining succeeded, 1823 * so try_reonline_memory_block() can do the right thing. 1824 */ 1825 if (!rc) 1826 **online_types = online_type; 1827 1828 (*online_types)++; 1829 /* Ignore if already offline. */ 1830 return rc < 0 ? rc : 0; 1831} 1832 1833static int try_reonline_memory_block(struct memory_block *mem, void *arg) 1834{ 1835 uint8_t **online_types = arg; 1836 int rc; 1837 1838 if (**online_types != MMOP_OFFLINE) { 1839 mem->online_type = **online_types; 1840 rc = device_online(&mem->dev); 1841 if (rc < 0) 1842 pr_warn("%s: Failed to re-online memory: %d", 1843 __func__, rc); 1844 } 1845 1846 /* Continue processing all remaining memory blocks. */ 1847 (*online_types)++; 1848 return 0; 1849} 1850 1851/* 1852 * Try to offline and remove memory. Might take a long time to finish in case 1853 * memory is still in use. Primarily useful for memory devices that logically 1854 * unplugged all memory (so it's no longer in use) and want to offline + remove 1855 * that memory. 1856 */ 1857int offline_and_remove_memory(int nid, u64 start, u64 size) 1858{ 1859 const unsigned long mb_count = size / memory_block_size_bytes(); 1860 uint8_t *online_types, *tmp; 1861 int rc; 1862 1863 if (!IS_ALIGNED(start, memory_block_size_bytes()) || 1864 !IS_ALIGNED(size, memory_block_size_bytes()) || !size) 1865 return -EINVAL; 1866 1867 /* 1868 * We'll remember the old online type of each memory block, so we can 1869 * try to revert whatever we did when offlining one memory block fails 1870 * after offlining some others succeeded. 1871 */ 1872 online_types = kmalloc_array(mb_count, sizeof(*online_types), 1873 GFP_KERNEL); 1874 if (!online_types) 1875 return -ENOMEM; 1876 /* 1877 * Initialize all states to MMOP_OFFLINE, so when we abort processing in 1878 * try_offline_memory_block(), we'll skip all unprocessed blocks in 1879 * try_reonline_memory_block(). 1880 */ 1881 memset(online_types, MMOP_OFFLINE, mb_count); 1882 1883 lock_device_hotplug(); 1884 1885 tmp = online_types; 1886 rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block); 1887 1888 /* 1889 * In case we succeeded to offline all memory, remove it. 1890 * This cannot fail as it cannot get onlined in the meantime. 1891 */ 1892 if (!rc) { 1893 rc = try_remove_memory(nid, start, size); 1894 if (rc) 1895 pr_err("%s: Failed to remove memory: %d", __func__, rc); 1896 } 1897 1898 /* 1899 * Rollback what we did. While memory onlining might theoretically fail 1900 * (nacked by a notifier), it barely ever happens. 1901 */ 1902 if (rc) { 1903 tmp = online_types; 1904 walk_memory_blocks(start, size, &tmp, 1905 try_reonline_memory_block); 1906 } 1907 unlock_device_hotplug(); 1908 1909 kfree(online_types); 1910 return rc; 1911} 1912EXPORT_SYMBOL_GPL(offline_and_remove_memory); 1913#endif /* CONFIG_MEMORY_HOTREMOVE */ 1914