1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/mm/memory_hotplug.c
8 #include <linux/stddef.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>
40 #include <asm/tlbflush.h>
46 * online_page_callback contains pointer to current page onlining function.
47 * Initially it is generic_online_page(). If it is required it could be
48 * changed by calling set_online_page_callback() for callback registration
49 * and restore_online_page_callback() for generic callback restore.
52 static online_page_callback_t online_page_callback
= generic_online_page
;
53 static DEFINE_MUTEX(online_page_callback_lock
);
55 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock
);
57 static int default_kernel_zone
= ZONE_NORMAL
;
59 void get_online_mems(void)
61 percpu_down_read(&mem_hotplug_lock
);
64 void put_online_mems(void)
66 percpu_up_read(&mem_hotplug_lock
);
69 bool movable_node_enabled
= false;
71 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
72 int memhp_default_online_type
= MMOP_OFFLINE
;
74 int memhp_default_online_type
= MMOP_ONLINE
;
77 static int __init
setup_memhp_default_state(char *str
)
79 const int online_type
= memhp_online_type_from_str(str
);
82 memhp_default_online_type
= online_type
;
86 __setup("memhp_default_state=", setup_memhp_default_state
);
88 void mem_hotplug_begin(void)
91 percpu_down_write(&mem_hotplug_lock
);
94 void mem_hotplug_done(void)
96 percpu_up_write(&mem_hotplug_lock
);
100 u64 max_mem_size
= U64_MAX
;
102 /* add this memory to iomem resource */
103 static struct resource
*register_memory_resource(u64 start
, u64 size
,
104 const char *resource_name
)
106 struct resource
*res
;
107 unsigned long flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
109 if (strcmp(resource_name
, "System RAM"))
110 flags
|= IORESOURCE_SYSRAM_DRIVER_MANAGED
;
113 * Make sure value parsed from 'mem=' only restricts memory adding
114 * while booting, so that memory hotplug won't be impacted. Please
115 * refer to document of 'mem=' in kernel-parameters.txt for more
118 if (start
+ size
> max_mem_size
&& system_state
< SYSTEM_RUNNING
)
119 return ERR_PTR(-E2BIG
);
122 * Request ownership of the new memory range. This might be
123 * a child of an existing resource that was present but
124 * not marked as busy.
126 res
= __request_region(&iomem_resource
, start
, size
,
127 resource_name
, flags
);
130 pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
131 start
, start
+ size
);
132 return ERR_PTR(-EEXIST
);
137 static void release_memory_resource(struct resource
*res
)
141 release_resource(res
);
145 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
146 void get_page_bootmem(unsigned long info
, struct page
*page
,
149 page
->freelist
= (void *)type
;
150 SetPagePrivate(page
);
151 set_page_private(page
, info
);
155 void put_page_bootmem(struct page
*page
)
159 type
= (unsigned long) page
->freelist
;
160 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
161 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
163 if (page_ref_dec_return(page
) == 1) {
164 page
->freelist
= NULL
;
165 ClearPagePrivate(page
);
166 set_page_private(page
, 0);
167 INIT_LIST_HEAD(&page
->lru
);
168 free_reserved_page(page
);
172 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
173 #ifndef CONFIG_SPARSEMEM_VMEMMAP
174 static void register_page_bootmem_info_section(unsigned long start_pfn
)
176 unsigned long mapsize
, section_nr
, i
;
177 struct mem_section
*ms
;
178 struct page
*page
, *memmap
;
179 struct mem_section_usage
*usage
;
181 section_nr
= pfn_to_section_nr(start_pfn
);
182 ms
= __nr_to_section(section_nr
);
184 /* Get section's memmap address */
185 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
188 * Get page for the memmap's phys address
189 * XXX: need more consideration for sparse_vmemmap...
191 page
= virt_to_page(memmap
);
192 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
193 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
195 /* remember memmap's page */
196 for (i
= 0; i
< mapsize
; i
++, page
++)
197 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
200 page
= virt_to_page(usage
);
202 mapsize
= PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT
;
204 for (i
= 0; i
< mapsize
; i
++, page
++)
205 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
208 #else /* CONFIG_SPARSEMEM_VMEMMAP */
209 static void register_page_bootmem_info_section(unsigned long start_pfn
)
211 unsigned long mapsize
, section_nr
, i
;
212 struct mem_section
*ms
;
213 struct page
*page
, *memmap
;
214 struct mem_section_usage
*usage
;
216 section_nr
= pfn_to_section_nr(start_pfn
);
217 ms
= __nr_to_section(section_nr
);
219 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
221 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
224 page
= virt_to_page(usage
);
226 mapsize
= PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT
;
228 for (i
= 0; i
< mapsize
; i
++, page
++)
229 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
231 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
233 void __init
register_page_bootmem_info_node(struct pglist_data
*pgdat
)
235 unsigned long i
, pfn
, end_pfn
, nr_pages
;
236 int node
= pgdat
->node_id
;
239 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
240 page
= virt_to_page(pgdat
);
242 for (i
= 0; i
< nr_pages
; i
++, page
++)
243 get_page_bootmem(node
, page
, NODE_INFO
);
245 pfn
= pgdat
->node_start_pfn
;
246 end_pfn
= pgdat_end_pfn(pgdat
);
248 /* register section info */
249 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
251 * Some platforms can assign the same pfn to multiple nodes - on
252 * node0 as well as nodeN. To avoid registering a pfn against
253 * multiple nodes we check that this pfn does not already
254 * reside in some other nodes.
256 if (pfn_valid(pfn
) && (early_pfn_to_nid(pfn
) == node
))
257 register_page_bootmem_info_section(pfn
);
260 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
262 static int check_pfn_span(unsigned long pfn
, unsigned long nr_pages
,
266 * Disallow all operations smaller than a sub-section and only
267 * allow operations smaller than a section for
268 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
269 * enforces a larger memory_block_size_bytes() granularity for
270 * memory that will be marked online, so this check should only
271 * fire for direct arch_{add,remove}_memory() users outside of
272 * add_memory_resource().
274 unsigned long min_align
;
276 if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP
))
277 min_align
= PAGES_PER_SUBSECTION
;
279 min_align
= PAGES_PER_SECTION
;
280 if (!IS_ALIGNED(pfn
, min_align
)
281 || !IS_ALIGNED(nr_pages
, min_align
)) {
282 WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n",
283 reason
, pfn
, pfn
+ nr_pages
- 1);
289 static int check_hotplug_memory_addressable(unsigned long pfn
,
290 unsigned long nr_pages
)
292 const u64 max_addr
= PFN_PHYS(pfn
+ nr_pages
) - 1;
294 if (max_addr
>> MAX_PHYSMEM_BITS
) {
295 const u64 max_allowed
= (1ull << (MAX_PHYSMEM_BITS
+ 1)) - 1;
297 "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
298 (u64
)PFN_PHYS(pfn
), max_addr
, max_allowed
);
306 * Reasonably generic function for adding memory. It is
307 * expected that archs that support memory hotplug will
308 * call this function after deciding the zone to which to
311 int __ref
__add_pages(int nid
, unsigned long pfn
, unsigned long nr_pages
,
312 struct mhp_params
*params
)
314 const unsigned long end_pfn
= pfn
+ nr_pages
;
315 unsigned long cur_nr_pages
;
317 struct vmem_altmap
*altmap
= params
->altmap
;
319 if (WARN_ON_ONCE(!params
->pgprot
.pgprot
))
322 err
= check_hotplug_memory_addressable(pfn
, nr_pages
);
328 * Validate altmap is within bounds of the total request
330 if (altmap
->base_pfn
!= pfn
331 || vmem_altmap_offset(altmap
) > nr_pages
) {
332 pr_warn_once("memory add fail, invalid altmap\n");
338 err
= check_pfn_span(pfn
, nr_pages
, "add");
342 for (; pfn
< end_pfn
; pfn
+= cur_nr_pages
) {
343 /* Select all remaining pages up to the next section boundary */
344 cur_nr_pages
= min(end_pfn
- pfn
,
345 SECTION_ALIGN_UP(pfn
+ 1) - pfn
);
346 err
= sparse_add_section(nid
, pfn
, cur_nr_pages
, altmap
);
351 vmemmap_populate_print_last();
355 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
356 static unsigned long find_smallest_section_pfn(int nid
, struct zone
*zone
,
357 unsigned long start_pfn
,
358 unsigned long end_pfn
)
360 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SUBSECTION
) {
361 if (unlikely(!pfn_to_online_page(start_pfn
)))
364 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
367 if (zone
!= page_zone(pfn_to_page(start_pfn
)))
376 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
377 static unsigned long find_biggest_section_pfn(int nid
, struct zone
*zone
,
378 unsigned long start_pfn
,
379 unsigned long end_pfn
)
383 /* pfn is the end pfn of a memory section. */
385 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SUBSECTION
) {
386 if (unlikely(!pfn_to_online_page(pfn
)))
389 if (unlikely(pfn_to_nid(pfn
) != nid
))
392 if (zone
!= page_zone(pfn_to_page(pfn
)))
401 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
402 unsigned long end_pfn
)
405 int nid
= zone_to_nid(zone
);
407 zone_span_writelock(zone
);
408 if (zone
->zone_start_pfn
== start_pfn
) {
410 * If the section is smallest section in the zone, it need
411 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
412 * In this case, we find second smallest valid mem_section
413 * for shrinking zone.
415 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
418 zone
->spanned_pages
= zone_end_pfn(zone
) - pfn
;
419 zone
->zone_start_pfn
= pfn
;
421 zone
->zone_start_pfn
= 0;
422 zone
->spanned_pages
= 0;
424 } else if (zone_end_pfn(zone
) == end_pfn
) {
426 * If the section is biggest section in the zone, it need
427 * shrink zone->spanned_pages.
428 * In this case, we find second biggest valid mem_section for
431 pfn
= find_biggest_section_pfn(nid
, zone
, zone
->zone_start_pfn
,
434 zone
->spanned_pages
= pfn
- zone
->zone_start_pfn
+ 1;
436 zone
->zone_start_pfn
= 0;
437 zone
->spanned_pages
= 0;
440 zone_span_writeunlock(zone
);
443 static void update_pgdat_span(struct pglist_data
*pgdat
)
445 unsigned long node_start_pfn
= 0, node_end_pfn
= 0;
448 for (zone
= pgdat
->node_zones
;
449 zone
< pgdat
->node_zones
+ MAX_NR_ZONES
; zone
++) {
450 unsigned long zone_end_pfn
= zone
->zone_start_pfn
+
453 /* No need to lock the zones, they can't change. */
454 if (!zone
->spanned_pages
)
457 node_start_pfn
= zone
->zone_start_pfn
;
458 node_end_pfn
= zone_end_pfn
;
462 if (zone_end_pfn
> node_end_pfn
)
463 node_end_pfn
= zone_end_pfn
;
464 if (zone
->zone_start_pfn
< node_start_pfn
)
465 node_start_pfn
= zone
->zone_start_pfn
;
468 pgdat
->node_start_pfn
= node_start_pfn
;
469 pgdat
->node_spanned_pages
= node_end_pfn
- node_start_pfn
;
472 void __ref
remove_pfn_range_from_zone(struct zone
*zone
,
473 unsigned long start_pfn
,
474 unsigned long nr_pages
)
476 const unsigned long end_pfn
= start_pfn
+ nr_pages
;
477 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
478 unsigned long pfn
, cur_nr_pages
, flags
;
480 /* Poison struct pages because they are now uninitialized again. */
481 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= cur_nr_pages
) {
484 /* Select all remaining pages up to the next section boundary */
486 min(end_pfn
- pfn
, SECTION_ALIGN_UP(pfn
+ 1) - pfn
);
487 page_init_poison(pfn_to_page(pfn
),
488 sizeof(struct page
) * cur_nr_pages
);
491 #ifdef CONFIG_ZONE_DEVICE
493 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
494 * we will not try to shrink the zones - which is okay as
495 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
497 if (zone_idx(zone
) == ZONE_DEVICE
)
501 clear_zone_contiguous(zone
);
503 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
504 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
505 update_pgdat_span(pgdat
);
506 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
508 set_zone_contiguous(zone
);
511 static void __remove_section(unsigned long pfn
, unsigned long nr_pages
,
512 unsigned long map_offset
,
513 struct vmem_altmap
*altmap
)
515 struct mem_section
*ms
= __pfn_to_section(pfn
);
517 if (WARN_ON_ONCE(!valid_section(ms
)))
520 sparse_remove_section(ms
, pfn
, nr_pages
, map_offset
, altmap
);
524 * __remove_pages() - remove sections of pages
525 * @pfn: starting pageframe (must be aligned to start of a section)
526 * @nr_pages: number of pages to remove (must be multiple of section size)
527 * @altmap: alternative device page map or %NULL if default memmap is used
529 * Generic helper function to remove section mappings and sysfs entries
530 * for the section of the memory we are removing. Caller needs to make
531 * sure that pages are marked reserved and zones are adjust properly by
532 * calling offline_pages().
534 void __remove_pages(unsigned long pfn
, unsigned long nr_pages
,
535 struct vmem_altmap
*altmap
)
537 const unsigned long end_pfn
= pfn
+ nr_pages
;
538 unsigned long cur_nr_pages
;
539 unsigned long map_offset
= 0;
541 map_offset
= vmem_altmap_offset(altmap
);
543 if (check_pfn_span(pfn
, nr_pages
, "remove"))
546 for (; pfn
< end_pfn
; pfn
+= cur_nr_pages
) {
548 /* Select all remaining pages up to the next section boundary */
549 cur_nr_pages
= min(end_pfn
- pfn
,
550 SECTION_ALIGN_UP(pfn
+ 1) - pfn
);
551 __remove_section(pfn
, cur_nr_pages
, map_offset
, altmap
);
556 int set_online_page_callback(online_page_callback_t callback
)
561 mutex_lock(&online_page_callback_lock
);
563 if (online_page_callback
== generic_online_page
) {
564 online_page_callback
= callback
;
568 mutex_unlock(&online_page_callback_lock
);
573 EXPORT_SYMBOL_GPL(set_online_page_callback
);
575 int restore_online_page_callback(online_page_callback_t callback
)
580 mutex_lock(&online_page_callback_lock
);
582 if (online_page_callback
== callback
) {
583 online_page_callback
= generic_online_page
;
587 mutex_unlock(&online_page_callback_lock
);
592 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
594 void generic_online_page(struct page
*page
, unsigned int order
)
597 * Freeing the page with debug_pagealloc enabled will try to unmap it,
598 * so we should map it first. This is better than introducing a special
599 * case in page freeing fast path.
601 debug_pagealloc_map_pages(page
, 1 << order
);
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
);
609 EXPORT_SYMBOL_GPL(generic_online_page
);
611 static void online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
)
613 const unsigned long end_pfn
= start_pfn
+ nr_pages
;
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
622 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= MAX_ORDER_NR_PAGES
)
623 (*online_page_callback
)(pfn_to_page(pfn
), MAX_ORDER
- 1);
625 /* mark all involved sections as online */
626 online_mem_sections(start_pfn
, end_pfn
);
629 /* check which state of node_states will be changed when online memory */
630 static void node_states_check_changes_online(unsigned long nr_pages
,
631 struct zone
*zone
, struct memory_notify
*arg
)
633 int nid
= zone_to_nid(zone
);
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
;
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
;
649 static void node_states_set_node(int node
, struct memory_notify
*arg
)
651 if (arg
->status_change_nid_normal
>= 0)
652 node_set_state(node
, N_NORMAL_MEMORY
);
654 if (arg
->status_change_nid_high
>= 0)
655 node_set_state(node
, N_HIGH_MEMORY
);
657 if (arg
->status_change_nid
>= 0)
658 node_set_state(node
, N_MEMORY
);
661 static void __meminit
resize_zone_range(struct zone
*zone
, unsigned long start_pfn
,
662 unsigned long nr_pages
)
664 unsigned long old_end_pfn
= zone_end_pfn(zone
);
666 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
667 zone
->zone_start_pfn
= start_pfn
;
669 zone
->spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - zone
->zone_start_pfn
;
672 static void __meminit
resize_pgdat_range(struct pglist_data
*pgdat
, unsigned long start_pfn
,
673 unsigned long nr_pages
)
675 unsigned long old_end_pfn
= pgdat_end_pfn(pgdat
);
677 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
678 pgdat
->node_start_pfn
= start_pfn
;
680 pgdat
->node_spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - pgdat
->node_start_pfn
;
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.
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.
692 void __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
)
696 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
697 int nid
= pgdat
->node_id
;
700 clear_zone_contiguous(zone
);
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
);
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
718 memmap_init_zone(nr_pages
, nid
, zone_idx(zone
), start_pfn
, 0,
719 MEMINIT_HOTPLUG
, altmap
, migratetype
);
721 set_zone_contiguous(zone
);
724 void set_default_mem_hotplug_zone(enum zone_type zone
)
726 default_kernel_zone
= zone
;
729 #ifdef CONFIG_HIGHMEM
730 #define MAX_KERNEL_ZONE ZONE_HIGHMEM
732 #define MAX_KERNEL_ZONE ZONE_NORMAL
736 * Returns a default kernel memory zone for the given pfn range.
737 * If no kernel zone covers this pfn range it will automatically go
738 * to the MAX_KERNEL_ZONE.
740 static struct zone
*default_kernel_zone_for_pfn(int nid
, unsigned long start_pfn
,
741 unsigned long nr_pages
)
743 struct pglist_data
*pgdat
= NODE_DATA(nid
);
746 for (zid
= 0; zid
<= MAX_KERNEL_ZONE
; zid
++) {
747 struct zone
*zone
= &pgdat
->node_zones
[zid
];
749 if (zone_intersects(zone
, start_pfn
, nr_pages
))
753 return &pgdat
->node_zones
[default_kernel_zone
];
756 static inline struct zone
*default_zone_for_pfn(int nid
, unsigned long start_pfn
,
757 unsigned long nr_pages
)
759 struct zone
*kernel_zone
= default_kernel_zone_for_pfn(nid
, start_pfn
,
761 struct zone
*movable_zone
= &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
762 bool in_kernel
= zone_intersects(kernel_zone
, start_pfn
, nr_pages
);
763 bool in_movable
= zone_intersects(movable_zone
, start_pfn
, nr_pages
);
766 * We inherit the existing zone in a simple case where zones do not
767 * overlap in the given range
769 if (in_kernel
^ in_movable
)
770 return (in_kernel
) ? kernel_zone
: movable_zone
;
773 * If the range doesn't belong to any zone or two zones overlap in the
774 * given range then we use movable zone only if movable_node is
775 * enabled because we always online to a kernel zone by default.
777 return movable_node_enabled
? movable_zone
: kernel_zone
;
780 struct zone
* zone_for_pfn_range(int online_type
, int nid
, unsigned start_pfn
,
781 unsigned long nr_pages
)
783 if (online_type
== MMOP_ONLINE_KERNEL
)
784 return default_kernel_zone_for_pfn(nid
, start_pfn
, nr_pages
);
786 if (online_type
== MMOP_ONLINE_MOVABLE
)
787 return &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
789 return default_zone_for_pfn(nid
, start_pfn
, nr_pages
);
792 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
,
793 int online_type
, int nid
)
797 int need_zonelists_rebuild
= 0;
799 struct memory_notify arg
;
801 /* We can only online full sections (e.g., SECTION_IS_ONLINE) */
802 if (WARN_ON_ONCE(!nr_pages
||
803 !IS_ALIGNED(pfn
| nr_pages
, PAGES_PER_SECTION
)))
808 /* associate pfn range with the zone */
809 zone
= zone_for_pfn_range(online_type
, nid
, pfn
, nr_pages
);
810 move_pfn_range_to_zone(zone
, pfn
, nr_pages
, NULL
, MIGRATE_ISOLATE
);
813 arg
.nr_pages
= nr_pages
;
814 node_states_check_changes_online(nr_pages
, zone
, &arg
);
816 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
817 ret
= notifier_to_errno(ret
);
819 goto failed_addition
;
822 * Fixup the number of isolated pageblocks before marking the sections
823 * onlining, such that undo_isolate_page_range() works correctly.
825 spin_lock_irqsave(&zone
->lock
, flags
);
826 zone
->nr_isolate_pageblock
+= nr_pages
/ pageblock_nr_pages
;
827 spin_unlock_irqrestore(&zone
->lock
, flags
);
830 * If this zone is not populated, then it is not in zonelist.
831 * This means the page allocator ignores this zone.
832 * So, zonelist must be updated after online.
834 if (!populated_zone(zone
)) {
835 need_zonelists_rebuild
= 1;
836 setup_zone_pageset(zone
);
839 online_pages_range(pfn
, nr_pages
);
840 zone
->present_pages
+= nr_pages
;
842 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
843 zone
->zone_pgdat
->node_present_pages
+= nr_pages
;
844 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
846 node_states_set_node(nid
, &arg
);
847 if (need_zonelists_rebuild
)
848 build_all_zonelists(NULL
);
849 zone_pcp_update(zone
);
851 /* Basic onlining is complete, allow allocation of onlined pages. */
852 undo_isolate_page_range(pfn
, pfn
+ nr_pages
, MIGRATE_MOVABLE
);
855 * Freshly onlined pages aren't shuffled (e.g., all pages are placed to
856 * the tail of the freelist when undoing isolation). Shuffle the whole
857 * zone to make sure the just onlined pages are properly distributed
858 * across the whole freelist - to create an initial shuffle.
862 init_per_zone_wmark_min();
867 writeback_set_ratelimit();
869 memory_notify(MEM_ONLINE
, &arg
);
874 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
875 (unsigned long long) pfn
<< PAGE_SHIFT
,
876 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
877 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
878 remove_pfn_range_from_zone(zone
, pfn
, nr_pages
);
882 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
884 static void reset_node_present_pages(pg_data_t
*pgdat
)
888 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
889 z
->present_pages
= 0;
891 pgdat
->node_present_pages
= 0;
894 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
895 static pg_data_t __ref
*hotadd_new_pgdat(int nid
)
897 struct pglist_data
*pgdat
;
899 pgdat
= NODE_DATA(nid
);
901 pgdat
= arch_alloc_nodedata(nid
);
905 pgdat
->per_cpu_nodestats
=
906 alloc_percpu(struct per_cpu_nodestat
);
907 arch_refresh_nodedata(nid
, pgdat
);
911 * Reset the nr_zones, order and highest_zoneidx before reuse.
912 * Note that kswapd will init kswapd_highest_zoneidx properly
913 * when it starts in the near future.
916 pgdat
->kswapd_order
= 0;
917 pgdat
->kswapd_highest_zoneidx
= 0;
918 for_each_online_cpu(cpu
) {
919 struct per_cpu_nodestat
*p
;
921 p
= per_cpu_ptr(pgdat
->per_cpu_nodestats
, cpu
);
922 memset(p
, 0, sizeof(*p
));
926 /* we can use NODE_DATA(nid) from here */
927 pgdat
->node_id
= nid
;
928 pgdat
->node_start_pfn
= 0;
930 /* init node's zones as empty zones, we don't have any present pages.*/
931 free_area_init_core_hotplug(nid
);
934 * The node we allocated has no zone fallback lists. For avoiding
935 * to access not-initialized zonelist, build here.
937 build_all_zonelists(pgdat
);
940 * When memory is hot-added, all the memory is in offline state. So
941 * clear all zones' present_pages because they will be updated in
942 * online_pages() and offline_pages().
944 reset_node_managed_pages(pgdat
);
945 reset_node_present_pages(pgdat
);
950 static void rollback_node_hotadd(int nid
)
952 pg_data_t
*pgdat
= NODE_DATA(nid
);
954 arch_refresh_nodedata(nid
, NULL
);
955 free_percpu(pgdat
->per_cpu_nodestats
);
956 arch_free_nodedata(pgdat
);
961 * try_online_node - online a node if offlined
963 * @set_node_online: Whether we want to online the node
964 * called by cpu_up() to online a node without onlined memory.
967 * 1 -> a new node has been allocated
968 * 0 -> the node is already online
969 * -ENOMEM -> the node could not be allocated
971 static int __try_online_node(int nid
, bool set_node_online
)
976 if (node_online(nid
))
979 pgdat
= hotadd_new_pgdat(nid
);
981 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
986 if (set_node_online
) {
987 node_set_online(nid
);
988 ret
= register_one_node(nid
);
996 * Users of this function always want to online/register the node
998 int try_online_node(int nid
)
1002 mem_hotplug_begin();
1003 ret
= __try_online_node(nid
, true);
1008 static int check_hotplug_memory_range(u64 start
, u64 size
)
1010 /* memory range must be block size aligned */
1011 if (!size
|| !IS_ALIGNED(start
, memory_block_size_bytes()) ||
1012 !IS_ALIGNED(size
, memory_block_size_bytes())) {
1013 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1014 memory_block_size_bytes(), start
, size
);
1021 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1023 mem
->online_type
= memhp_default_online_type
;
1024 return device_online(&mem
->dev
);
1028 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1029 * and online/offline operations (triggered e.g. by sysfs).
1031 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1033 int __ref
add_memory_resource(int nid
, struct resource
*res
, mhp_t mhp_flags
)
1035 struct mhp_params params
= { .pgprot
= pgprot_mhp(PAGE_KERNEL
) };
1037 bool new_node
= false;
1041 size
= resource_size(res
);
1043 ret
= check_hotplug_memory_range(start
, size
);
1047 if (!node_possible(nid
)) {
1048 WARN(1, "node %d was absent from the node_possible_map\n", nid
);
1052 mem_hotplug_begin();
1054 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK
))
1055 memblock_add_node(start
, size
, nid
);
1057 ret
= __try_online_node(nid
, false);
1062 /* call arch's memory hotadd */
1063 ret
= arch_add_memory(nid
, start
, size
, ¶ms
);
1067 /* create memory block devices after memory was added */
1068 ret
= create_memory_block_devices(start
, size
);
1070 arch_remove_memory(nid
, start
, size
, NULL
);
1075 /* If sysfs file of new node can't be created, cpu on the node
1076 * can't be hot-added. There is no rollback way now.
1077 * So, check by BUG_ON() to catch it reluctantly..
1078 * We online node here. We can't roll back from here.
1080 node_set_online(nid
);
1081 ret
= __register_one_node(nid
);
1085 /* link memory sections under this node.*/
1086 link_mem_sections(nid
, PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1089 /* create new memmap entry */
1090 if (!strcmp(res
->name
, "System RAM"))
1091 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1093 /* device_online() will take the lock when calling online_pages() */
1097 * In case we're allowed to merge the resource, flag it and trigger
1098 * merging now that adding succeeded.
1100 if (mhp_flags
& MEMHP_MERGE_RESOURCE
)
1101 merge_system_ram_resource(res
);
1103 /* online pages if requested */
1104 if (memhp_default_online_type
!= MMOP_OFFLINE
)
1105 walk_memory_blocks(start
, size
, NULL
, online_memory_block
);
1109 /* rollback pgdat allocation and others */
1111 rollback_node_hotadd(nid
);
1112 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK
))
1113 memblock_remove(start
, size
);
1118 /* requires device_hotplug_lock, see add_memory_resource() */
1119 int __ref
__add_memory(int nid
, u64 start
, u64 size
, mhp_t mhp_flags
)
1121 struct resource
*res
;
1124 res
= register_memory_resource(start
, size
, "System RAM");
1126 return PTR_ERR(res
);
1128 ret
= add_memory_resource(nid
, res
, mhp_flags
);
1130 release_memory_resource(res
);
1134 int add_memory(int nid
, u64 start
, u64 size
, mhp_t mhp_flags
)
1138 lock_device_hotplug();
1139 rc
= __add_memory(nid
, start
, size
, mhp_flags
);
1140 unlock_device_hotplug();
1144 EXPORT_SYMBOL_GPL(add_memory
);
1147 * Add special, driver-managed memory to the system as system RAM. Such
1148 * memory is not exposed via the raw firmware-provided memmap as system
1149 * RAM, instead, it is detected and added by a driver - during cold boot,
1150 * after a reboot, and after kexec.
1152 * Reasons why this memory should not be used for the initial memmap of a
1153 * kexec kernel or for placing kexec images:
1154 * - The booting kernel is in charge of determining how this memory will be
1155 * used (e.g., use persistent memory as system RAM)
1156 * - Coordination with a hypervisor is required before this memory
1157 * can be used (e.g., inaccessible parts).
1159 * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided
1160 * memory map") are created. Also, the created memory resource is flagged
1161 * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case
1162 * this memory as well (esp., not place kexec images onto it).
1164 * The resource_name (visible via /proc/iomem) has to have the format
1165 * "System RAM ($DRIVER)".
1167 int add_memory_driver_managed(int nid
, u64 start
, u64 size
,
1168 const char *resource_name
, mhp_t mhp_flags
)
1170 struct resource
*res
;
1173 if (!resource_name
||
1174 strstr(resource_name
, "System RAM (") != resource_name
||
1175 resource_name
[strlen(resource_name
) - 1] != ')')
1178 lock_device_hotplug();
1180 res
= register_memory_resource(start
, size
, resource_name
);
1186 rc
= add_memory_resource(nid
, res
, mhp_flags
);
1188 release_memory_resource(res
);
1191 unlock_device_hotplug();
1194 EXPORT_SYMBOL_GPL(add_memory_driver_managed
);
1196 #ifdef CONFIG_MEMORY_HOTREMOVE
1198 * Confirm all pages in a range [start, end) belong to the same zone (skipping
1199 * memory holes). When true, return the zone.
1201 struct zone
*test_pages_in_a_zone(unsigned long start_pfn
,
1202 unsigned long end_pfn
)
1204 unsigned long pfn
, sec_end_pfn
;
1205 struct zone
*zone
= NULL
;
1208 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1210 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1211 /* Make sure the memory section is present first */
1212 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1214 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1215 pfn
+= MAX_ORDER_NR_PAGES
) {
1217 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1218 while ((i
< MAX_ORDER_NR_PAGES
) &&
1219 !pfn_valid_within(pfn
+ i
))
1221 if (i
== MAX_ORDER_NR_PAGES
|| pfn
+ i
>= end_pfn
)
1223 /* Check if we got outside of the zone */
1224 if (zone
&& !zone_spans_pfn(zone
, pfn
+ i
))
1226 page
= pfn_to_page(pfn
+ i
);
1227 if (zone
&& page_zone(page
) != zone
)
1229 zone
= page_zone(page
);
1237 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1238 * non-lru movable pages and hugepages). Will skip over most unmovable
1239 * pages (esp., pages that can be skipped when offlining), but bail out on
1240 * definitely unmovable pages.
1243 * 0 in case a movable page is found and movable_pfn was updated.
1244 * -ENOENT in case no movable page was found.
1245 * -EBUSY in case a definitely unmovable page was found.
1247 static int scan_movable_pages(unsigned long start
, unsigned long end
,
1248 unsigned long *movable_pfn
)
1252 for (pfn
= start
; pfn
< end
; pfn
++) {
1253 struct page
*page
, *head
;
1256 if (!pfn_valid(pfn
))
1258 page
= pfn_to_page(pfn
);
1261 if (__PageMovable(page
))
1265 * PageOffline() pages that are not marked __PageMovable() and
1266 * have a reference count > 0 (after MEM_GOING_OFFLINE) are
1267 * definitely unmovable. If their reference count would be 0,
1268 * they could at least be skipped when offlining memory.
1270 if (PageOffline(page
) && page_count(page
))
1273 if (!PageHuge(page
))
1275 head
= compound_head(page
);
1276 if (page_huge_active(head
))
1278 skip
= compound_nr(head
) - (page
- head
);
1288 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1291 struct page
*page
, *head
;
1295 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++) {
1296 if (!pfn_valid(pfn
))
1298 page
= pfn_to_page(pfn
);
1299 head
= compound_head(page
);
1301 if (PageHuge(page
)) {
1302 pfn
= page_to_pfn(head
) + compound_nr(head
) - 1;
1303 isolate_huge_page(head
, &source
);
1305 } else if (PageTransHuge(page
))
1306 pfn
= page_to_pfn(head
) + thp_nr_pages(page
) - 1;
1309 * HWPoison pages have elevated reference counts so the migration would
1310 * fail on them. It also doesn't make any sense to migrate them in the
1311 * first place. Still try to unmap such a page in case it is still mapped
1312 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1313 * the unmap as the catch all safety net).
1315 if (PageHWPoison(page
)) {
1316 if (WARN_ON(PageLRU(page
)))
1317 isolate_lru_page(page
);
1318 if (page_mapped(page
))
1319 try_to_unmap(page
, TTU_IGNORE_MLOCK
);
1323 if (!get_page_unless_zero(page
))
1326 * We can skip free pages. And we can deal with pages on
1327 * LRU and non-lru movable pages.
1330 ret
= isolate_lru_page(page
);
1332 ret
= isolate_movable_page(page
, ISOLATE_UNEVICTABLE
);
1333 if (!ret
) { /* Success */
1334 list_add_tail(&page
->lru
, &source
);
1335 if (!__PageMovable(page
))
1336 inc_node_page_state(page
, NR_ISOLATED_ANON
+
1337 page_is_file_lru(page
));
1340 pr_warn("failed to isolate pfn %lx\n", pfn
);
1341 dump_page(page
, "isolation failed");
1345 if (!list_empty(&source
)) {
1346 nodemask_t nmask
= node_states
[N_MEMORY
];
1347 struct migration_target_control mtc
= {
1349 .gfp_mask
= GFP_USER
| __GFP_MOVABLE
| __GFP_RETRY_MAYFAIL
,
1353 * We have checked that migration range is on a single zone so
1354 * we can use the nid of the first page to all the others.
1356 mtc
.nid
= page_to_nid(list_first_entry(&source
, struct page
, lru
));
1359 * try to allocate from a different node but reuse this node
1360 * if there are no other online nodes to be used (e.g. we are
1361 * offlining a part of the only existing node)
1363 node_clear(mtc
.nid
, nmask
);
1364 if (nodes_empty(nmask
))
1365 node_set(mtc
.nid
, nmask
);
1366 ret
= migrate_pages(&source
, alloc_migration_target
, NULL
,
1367 (unsigned long)&mtc
, MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1369 list_for_each_entry(page
, &source
, lru
) {
1370 pr_warn("migrating pfn %lx failed ret:%d ",
1371 page_to_pfn(page
), ret
);
1372 dump_page(page
, "migration failure");
1374 putback_movable_pages(&source
);
1381 static int __init
cmdline_parse_movable_node(char *p
)
1383 movable_node_enabled
= true;
1386 early_param("movable_node", cmdline_parse_movable_node
);
1388 /* check which state of node_states will be changed when offline memory */
1389 static void node_states_check_changes_offline(unsigned long nr_pages
,
1390 struct zone
*zone
, struct memory_notify
*arg
)
1392 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1393 unsigned long present_pages
= 0;
1396 arg
->status_change_nid
= NUMA_NO_NODE
;
1397 arg
->status_change_nid_normal
= NUMA_NO_NODE
;
1398 arg
->status_change_nid_high
= NUMA_NO_NODE
;
1401 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
1402 * If the memory to be offline is within the range
1403 * [0..ZONE_NORMAL], and it is the last present memory there,
1404 * the zones in that range will become empty after the offlining,
1405 * thus we can determine that we need to clear the node from
1406 * node_states[N_NORMAL_MEMORY].
1408 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1409 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1410 if (zone_idx(zone
) <= ZONE_NORMAL
&& nr_pages
>= present_pages
)
1411 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1413 #ifdef CONFIG_HIGHMEM
1415 * node_states[N_HIGH_MEMORY] contains nodes which
1416 * have normal memory or high memory.
1417 * Here we add the present_pages belonging to ZONE_HIGHMEM.
1418 * If the zone is within the range of [0..ZONE_HIGHMEM), and
1419 * we determine that the zones in that range become empty,
1420 * we need to clear the node for N_HIGH_MEMORY.
1422 present_pages
+= pgdat
->node_zones
[ZONE_HIGHMEM
].present_pages
;
1423 if (zone_idx(zone
) <= ZONE_HIGHMEM
&& nr_pages
>= present_pages
)
1424 arg
->status_change_nid_high
= zone_to_nid(zone
);
1428 * We have accounted the pages from [0..ZONE_NORMAL), and
1429 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
1431 * Here we count the possible pages from ZONE_MOVABLE.
1432 * If after having accounted all the pages, we see that the nr_pages
1433 * to be offlined is over or equal to the accounted pages,
1434 * we know that the node will become empty, and so, we can clear
1435 * it for N_MEMORY as well.
1437 present_pages
+= pgdat
->node_zones
[ZONE_MOVABLE
].present_pages
;
1439 if (nr_pages
>= present_pages
)
1440 arg
->status_change_nid
= zone_to_nid(zone
);
1443 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1445 if (arg
->status_change_nid_normal
>= 0)
1446 node_clear_state(node
, N_NORMAL_MEMORY
);
1448 if (arg
->status_change_nid_high
>= 0)
1449 node_clear_state(node
, N_HIGH_MEMORY
);
1451 if (arg
->status_change_nid
>= 0)
1452 node_clear_state(node
, N_MEMORY
);
1455 static int count_system_ram_pages_cb(unsigned long start_pfn
,
1456 unsigned long nr_pages
, void *data
)
1458 unsigned long *nr_system_ram_pages
= data
;
1460 *nr_system_ram_pages
+= nr_pages
;
1464 int __ref
offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1466 const unsigned long end_pfn
= start_pfn
+ nr_pages
;
1467 unsigned long pfn
, system_ram_pages
= 0;
1468 unsigned long flags
;
1470 struct memory_notify arg
;
1474 /* We can only offline full sections (e.g., SECTION_IS_ONLINE) */
1475 if (WARN_ON_ONCE(!nr_pages
||
1476 !IS_ALIGNED(start_pfn
| nr_pages
, PAGES_PER_SECTION
)))
1479 mem_hotplug_begin();
1482 * Don't allow to offline memory blocks that contain holes.
1483 * Consequently, memory blocks with holes can never get onlined
1484 * via the hotplug path - online_pages() - as hotplugged memory has
1485 * no holes. This way, we e.g., don't have to worry about marking
1486 * memory holes PG_reserved, don't need pfn_valid() checks, and can
1487 * avoid using walk_system_ram_range() later.
1489 walk_system_ram_range(start_pfn
, nr_pages
, &system_ram_pages
,
1490 count_system_ram_pages_cb
);
1491 if (system_ram_pages
!= nr_pages
) {
1493 reason
= "memory holes";
1494 goto failed_removal
;
1497 /* This makes hotplug much easier...and readable.
1498 we assume this for now. .*/
1499 zone
= test_pages_in_a_zone(start_pfn
, end_pfn
);
1502 reason
= "multizone range";
1503 goto failed_removal
;
1505 node
= zone_to_nid(zone
);
1508 * Disable pcplists so that page isolation cannot race with freeing
1509 * in a way that pages from isolated pageblock are left on pcplists.
1511 zone_pcp_disable(zone
);
1513 /* set above range as isolated */
1514 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1516 MEMORY_OFFLINE
| REPORT_FAILURE
);
1518 reason
= "failure to isolate range";
1519 goto failed_removal_pcplists_disabled
;
1522 arg
.start_pfn
= start_pfn
;
1523 arg
.nr_pages
= nr_pages
;
1524 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1526 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1527 ret
= notifier_to_errno(ret
);
1529 reason
= "notifier failure";
1530 goto failed_removal_isolated
;
1536 if (signal_pending(current
)) {
1538 reason
= "signal backoff";
1539 goto failed_removal_isolated
;
1543 lru_add_drain_all();
1545 ret
= scan_movable_pages(pfn
, end_pfn
, &pfn
);
1548 * TODO: fatal migration failures should bail
1551 do_migrate_range(pfn
, end_pfn
);
1555 if (ret
!= -ENOENT
) {
1556 reason
= "unmovable page";
1557 goto failed_removal_isolated
;
1561 * Dissolve free hugepages in the memory block before doing
1562 * offlining actually in order to make hugetlbfs's object
1563 * counting consistent.
1565 ret
= dissolve_free_huge_pages(start_pfn
, end_pfn
);
1567 reason
= "failure to dissolve huge pages";
1568 goto failed_removal_isolated
;
1571 ret
= test_pages_isolated(start_pfn
, end_pfn
, MEMORY_OFFLINE
);
1575 /* Mark all sections offline and remove free pages from the buddy. */
1576 __offline_isolated_pages(start_pfn
, end_pfn
);
1577 pr_debug("Offlined Pages %ld\n", nr_pages
);
1580 * The memory sections are marked offline, and the pageblock flags
1581 * effectively stale; nobody should be touching them. Fixup the number
1582 * of isolated pageblocks, memory onlining will properly revert this.
1584 spin_lock_irqsave(&zone
->lock
, flags
);
1585 zone
->nr_isolate_pageblock
-= nr_pages
/ pageblock_nr_pages
;
1586 spin_unlock_irqrestore(&zone
->lock
, flags
);
1588 zone_pcp_enable(zone
);
1590 /* removal success */
1591 adjust_managed_page_count(pfn_to_page(start_pfn
), -nr_pages
);
1592 zone
->present_pages
-= nr_pages
;
1594 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1595 zone
->zone_pgdat
->node_present_pages
-= nr_pages
;
1596 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1598 init_per_zone_wmark_min();
1600 if (!populated_zone(zone
)) {
1601 zone_pcp_reset(zone
);
1602 build_all_zonelists(NULL
);
1604 zone_pcp_update(zone
);
1606 node_states_clear_node(node
, &arg
);
1607 if (arg
.status_change_nid
>= 0) {
1609 kcompactd_stop(node
);
1612 writeback_set_ratelimit();
1614 memory_notify(MEM_OFFLINE
, &arg
);
1615 remove_pfn_range_from_zone(zone
, start_pfn
, nr_pages
);
1619 failed_removal_isolated
:
1620 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1621 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1622 failed_removal_pcplists_disabled
:
1623 zone_pcp_enable(zone
);
1625 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1626 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1627 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1,
1629 /* pushback to free area */
1634 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1636 int ret
= !is_memblock_offlined(mem
);
1638 if (unlikely(ret
)) {
1639 phys_addr_t beginpa
, endpa
;
1641 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1642 endpa
= beginpa
+ memory_block_size_bytes() - 1;
1643 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1651 static int check_cpu_on_node(pg_data_t
*pgdat
)
1655 for_each_present_cpu(cpu
) {
1656 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1658 * the cpu on this node isn't removed, and we can't
1659 * offline this node.
1667 static int check_no_memblock_for_node_cb(struct memory_block
*mem
, void *arg
)
1669 int nid
= *(int *)arg
;
1672 * If a memory block belongs to multiple nodes, the stored nid is not
1673 * reliable. However, such blocks are always online (e.g., cannot get
1674 * offlined) and, therefore, are still spanned by the node.
1676 return mem
->nid
== nid
? -EEXIST
: 0;
1683 * Offline a node if all memory sections and cpus of the node are removed.
1685 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1686 * and online/offline operations before this call.
1688 void try_offline_node(int nid
)
1690 pg_data_t
*pgdat
= NODE_DATA(nid
);
1694 * If the node still spans pages (especially ZONE_DEVICE), don't
1695 * offline it. A node spans memory after move_pfn_range_to_zone(),
1696 * e.g., after the memory block was onlined.
1698 if (pgdat
->node_spanned_pages
)
1702 * Especially offline memory blocks might not be spanned by the
1703 * node. They will get spanned by the node once they get onlined.
1704 * However, they link to the node in sysfs and can get onlined later.
1706 rc
= for_each_memory_block(&nid
, check_no_memblock_for_node_cb
);
1710 if (check_cpu_on_node(pgdat
))
1714 * all memory/cpu of this node are removed, we can offline this
1717 node_set_offline(nid
);
1718 unregister_one_node(nid
);
1720 EXPORT_SYMBOL(try_offline_node
);
1722 static int __ref
try_remove_memory(int nid
, u64 start
, u64 size
)
1726 BUG_ON(check_hotplug_memory_range(start
, size
));
1729 * All memory blocks must be offlined before removing memory. Check
1730 * whether all memory blocks in question are offline and return error
1731 * if this is not the case.
1733 rc
= walk_memory_blocks(start
, size
, NULL
, check_memblock_offlined_cb
);
1737 /* remove memmap entry */
1738 firmware_map_remove(start
, start
+ size
, "System RAM");
1741 * Memory block device removal under the device_hotplug_lock is
1742 * a barrier against racing online attempts.
1744 remove_memory_block_devices(start
, size
);
1746 mem_hotplug_begin();
1748 arch_remove_memory(nid
, start
, size
, NULL
);
1750 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK
)) {
1751 memblock_free(start
, size
);
1752 memblock_remove(start
, size
);
1755 release_mem_region_adjustable(start
, size
);
1757 try_offline_node(nid
);
1766 * @start: physical address of the region to remove
1767 * @size: size of the region to remove
1769 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1770 * and online/offline operations before this call, as required by
1771 * try_offline_node().
1773 void __remove_memory(int nid
, u64 start
, u64 size
)
1777 * trigger BUG() if some memory is not offlined prior to calling this
1780 if (try_remove_memory(nid
, start
, size
))
1785 * Remove memory if every memory block is offline, otherwise return -EBUSY is
1786 * some memory is not offline
1788 int remove_memory(int nid
, u64 start
, u64 size
)
1792 lock_device_hotplug();
1793 rc
= try_remove_memory(nid
, start
, size
);
1794 unlock_device_hotplug();
1798 EXPORT_SYMBOL_GPL(remove_memory
);
1800 static int try_offline_memory_block(struct memory_block
*mem
, void *arg
)
1802 uint8_t online_type
= MMOP_ONLINE_KERNEL
;
1803 uint8_t **online_types
= arg
;
1808 * Sense the online_type via the zone of the memory block. Offlining
1809 * with multiple zones within one memory block will be rejected
1810 * by offlining code ... so we don't care about that.
1812 page
= pfn_to_online_page(section_nr_to_pfn(mem
->start_section_nr
));
1813 if (page
&& zone_idx(page_zone(page
)) == ZONE_MOVABLE
)
1814 online_type
= MMOP_ONLINE_MOVABLE
;
1816 rc
= device_offline(&mem
->dev
);
1818 * Default is MMOP_OFFLINE - change it only if offlining succeeded,
1819 * so try_reonline_memory_block() can do the right thing.
1822 **online_types
= online_type
;
1825 /* Ignore if already offline. */
1826 return rc
< 0 ? rc
: 0;
1829 static int try_reonline_memory_block(struct memory_block
*mem
, void *arg
)
1831 uint8_t **online_types
= arg
;
1834 if (**online_types
!= MMOP_OFFLINE
) {
1835 mem
->online_type
= **online_types
;
1836 rc
= device_online(&mem
->dev
);
1838 pr_warn("%s: Failed to re-online memory: %d",
1842 /* Continue processing all remaining memory blocks. */
1848 * Try to offline and remove memory. Might take a long time to finish in case
1849 * memory is still in use. Primarily useful for memory devices that logically
1850 * unplugged all memory (so it's no longer in use) and want to offline + remove
1853 int offline_and_remove_memory(int nid
, u64 start
, u64 size
)
1855 const unsigned long mb_count
= size
/ memory_block_size_bytes();
1856 uint8_t *online_types
, *tmp
;
1859 if (!IS_ALIGNED(start
, memory_block_size_bytes()) ||
1860 !IS_ALIGNED(size
, memory_block_size_bytes()) || !size
)
1864 * We'll remember the old online type of each memory block, so we can
1865 * try to revert whatever we did when offlining one memory block fails
1866 * after offlining some others succeeded.
1868 online_types
= kmalloc_array(mb_count
, sizeof(*online_types
),
1873 * Initialize all states to MMOP_OFFLINE, so when we abort processing in
1874 * try_offline_memory_block(), we'll skip all unprocessed blocks in
1875 * try_reonline_memory_block().
1877 memset(online_types
, MMOP_OFFLINE
, mb_count
);
1879 lock_device_hotplug();
1882 rc
= walk_memory_blocks(start
, size
, &tmp
, try_offline_memory_block
);
1885 * In case we succeeded to offline all memory, remove it.
1886 * This cannot fail as it cannot get onlined in the meantime.
1889 rc
= try_remove_memory(nid
, start
, size
);
1891 pr_err("%s: Failed to remove memory: %d", __func__
, rc
);
1895 * Rollback what we did. While memory onlining might theoretically fail
1896 * (nacked by a notifier), it barely ever happens.
1900 walk_memory_blocks(start
, size
, &tmp
,
1901 try_reonline_memory_block
);
1903 unlock_device_hotplug();
1905 kfree(online_types
);
1908 EXPORT_SYMBOL_GPL(offline_and_remove_memory
);
1909 #endif /* CONFIG_MEMORY_HOTREMOVE */