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/writeback.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/memremap.h>
22 #include <linux/memory_hotplug.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
32 #include <linux/stop_machine.h>
33 #include <linux/hugetlb.h>
34 #include <linux/memblock.h>
35 #include <linux/compaction.h>
36 #include <linux/rmap.h>
37 #include <linux/module.h>
39 #include <asm/tlbflush.h>
45 MEMMAP_ON_MEMORY_DISABLE = 0,
46 MEMMAP_ON_MEMORY_ENABLE,
47 MEMMAP_ON_MEMORY_FORCE,
50 static int memmap_mode __read_mostly = MEMMAP_ON_MEMORY_DISABLE;
52 static inline unsigned long memory_block_memmap_size(void)
54 return PHYS_PFN(memory_block_size_bytes()) * sizeof(struct page);
57 static inline unsigned long memory_block_memmap_on_memory_pages(void)
59 unsigned long nr_pages = PFN_UP(memory_block_memmap_size());
62 * In "forced" memmap_on_memory mode, we add extra pages to align the
63 * vmemmap size to cover full pageblocks. That way, we can add memory
64 * even if the vmemmap size is not properly aligned, however, we might waste
67 if (memmap_mode == MEMMAP_ON_MEMORY_FORCE)
68 return pageblock_align(nr_pages);
72 #ifdef CONFIG_MHP_MEMMAP_ON_MEMORY
74 * memory_hotplug.memmap_on_memory parameter
76 static int set_memmap_mode(const char *val, const struct kernel_param *kp)
81 if (sysfs_streq(val, "force") || sysfs_streq(val, "FORCE")) {
82 mode = MEMMAP_ON_MEMORY_FORCE;
84 ret = kstrtobool(val, &enabled);
88 mode = MEMMAP_ON_MEMORY_ENABLE;
90 mode = MEMMAP_ON_MEMORY_DISABLE;
92 *((int *)kp->arg) = mode;
93 if (mode == MEMMAP_ON_MEMORY_FORCE) {
94 unsigned long memmap_pages = memory_block_memmap_on_memory_pages();
96 pr_info_once("Memory hotplug will waste %ld pages in each memory block\n",
97 memmap_pages - PFN_UP(memory_block_memmap_size()));
102 static int get_memmap_mode(char *buffer, const struct kernel_param *kp)
104 int mode = *((int *)kp->arg);
106 if (mode == MEMMAP_ON_MEMORY_FORCE)
107 return sprintf(buffer, "force\n");
108 return sprintf(buffer, "%c\n", mode ? 'Y' : 'N');
111 static const struct kernel_param_ops memmap_mode_ops = {
112 .set = set_memmap_mode,
113 .get = get_memmap_mode,
115 module_param_cb(memmap_on_memory, &memmap_mode_ops, &memmap_mode, 0444);
116 MODULE_PARM_DESC(memmap_on_memory, "Enable memmap on memory for memory hotplug\n"
117 "With value \"force\" it could result in memory wastage due "
118 "to memmap size limitations (Y/N/force)");
120 static inline bool mhp_memmap_on_memory(void)
122 return memmap_mode != MEMMAP_ON_MEMORY_DISABLE;
125 static inline bool mhp_memmap_on_memory(void)
132 ONLINE_POLICY_CONTIG_ZONES = 0,
133 ONLINE_POLICY_AUTO_MOVABLE,
136 static const char * const online_policy_to_str[] = {
137 [ONLINE_POLICY_CONTIG_ZONES] = "contig-zones",
138 [ONLINE_POLICY_AUTO_MOVABLE] = "auto-movable",
141 static int set_online_policy(const char *val, const struct kernel_param *kp)
143 int ret = sysfs_match_string(online_policy_to_str, val);
147 *((int *)kp->arg) = ret;
151 static int get_online_policy(char *buffer, const struct kernel_param *kp)
153 return sprintf(buffer, "%s\n", online_policy_to_str[*((int *)kp->arg)]);
157 * memory_hotplug.online_policy: configure online behavior when onlining without
158 * specifying a zone (MMOP_ONLINE)
160 * "contig-zones": keep zone contiguous
161 * "auto-movable": online memory to ZONE_MOVABLE if the configuration
162 * (auto_movable_ratio, auto_movable_numa_aware) allows for it
164 static int online_policy __read_mostly = ONLINE_POLICY_CONTIG_ZONES;
165 static const struct kernel_param_ops online_policy_ops = {
166 .set = set_online_policy,
167 .get = get_online_policy,
169 module_param_cb(online_policy, &online_policy_ops, &online_policy, 0644);
170 MODULE_PARM_DESC(online_policy,
171 "Set the online policy (\"contig-zones\", \"auto-movable\") "
172 "Default: \"contig-zones\"");
175 * memory_hotplug.auto_movable_ratio: specify maximum MOVABLE:KERNEL ratio
177 * The ratio represent an upper limit and the kernel might decide to not
178 * online some memory to ZONE_MOVABLE -- e.g., because hotplugged KERNEL memory
179 * doesn't allow for more MOVABLE memory.
181 static unsigned int auto_movable_ratio __read_mostly = 301;
182 module_param(auto_movable_ratio, uint, 0644);
183 MODULE_PARM_DESC(auto_movable_ratio,
184 "Set the maximum ratio of MOVABLE:KERNEL memory in the system "
185 "in percent for \"auto-movable\" online policy. Default: 301");
188 * memory_hotplug.auto_movable_numa_aware: consider numa node stats
191 static bool auto_movable_numa_aware __read_mostly = true;
192 module_param(auto_movable_numa_aware, bool, 0644);
193 MODULE_PARM_DESC(auto_movable_numa_aware,
194 "Consider numa node stats in addition to global stats in "
195 "\"auto-movable\" online policy. Default: true");
196 #endif /* CONFIG_NUMA */
199 * online_page_callback contains pointer to current page onlining function.
200 * Initially it is generic_online_page(). If it is required it could be
201 * changed by calling set_online_page_callback() for callback registration
202 * and restore_online_page_callback() for generic callback restore.
205 static online_page_callback_t online_page_callback = generic_online_page;
206 static DEFINE_MUTEX(online_page_callback_lock);
208 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
210 void get_online_mems(void)
212 percpu_down_read(&mem_hotplug_lock);
215 void put_online_mems(void)
217 percpu_up_read(&mem_hotplug_lock);
220 bool movable_node_enabled = false;
222 static int mhp_default_online_type = -1;
223 int mhp_get_default_online_type(void)
225 if (mhp_default_online_type >= 0)
226 return mhp_default_online_type;
228 if (IS_ENABLED(CONFIG_MHP_DEFAULT_ONLINE_TYPE_OFFLINE))
229 mhp_default_online_type = MMOP_OFFLINE;
230 else if (IS_ENABLED(CONFIG_MHP_DEFAULT_ONLINE_TYPE_ONLINE_AUTO))
231 mhp_default_online_type = MMOP_ONLINE;
232 else if (IS_ENABLED(CONFIG_MHP_DEFAULT_ONLINE_TYPE_ONLINE_KERNEL))
233 mhp_default_online_type = MMOP_ONLINE_KERNEL;
234 else if (IS_ENABLED(CONFIG_MHP_DEFAULT_ONLINE_TYPE_ONLINE_MOVABLE))
235 mhp_default_online_type = MMOP_ONLINE_MOVABLE;
237 mhp_default_online_type = MMOP_OFFLINE;
239 return mhp_default_online_type;
242 void mhp_set_default_online_type(int online_type)
244 mhp_default_online_type = online_type;
247 static int __init setup_memhp_default_state(char *str)
249 const int online_type = mhp_online_type_from_str(str);
251 if (online_type >= 0)
252 mhp_default_online_type = online_type;
256 __setup("memhp_default_state=", setup_memhp_default_state);
258 void mem_hotplug_begin(void)
261 percpu_down_write(&mem_hotplug_lock);
264 void mem_hotplug_done(void)
266 percpu_up_write(&mem_hotplug_lock);
270 u64 max_mem_size = U64_MAX;
272 /* add this memory to iomem resource */
273 static struct resource *register_memory_resource(u64 start, u64 size,
274 const char *resource_name)
276 struct resource *res;
277 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
279 if (strcmp(resource_name, "System RAM"))
280 flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED;
282 if (!mhp_range_allowed(start, size, true))
283 return ERR_PTR(-E2BIG);
286 * Make sure value parsed from 'mem=' only restricts memory adding
287 * while booting, so that memory hotplug won't be impacted. Please
288 * refer to document of 'mem=' in kernel-parameters.txt for more
291 if (start + size > max_mem_size && system_state < SYSTEM_RUNNING)
292 return ERR_PTR(-E2BIG);
295 * Request ownership of the new memory range. This might be
296 * a child of an existing resource that was present but
297 * not marked as busy.
299 res = __request_region(&iomem_resource, start, size,
300 resource_name, flags);
303 pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
304 start, start + size);
305 return ERR_PTR(-EEXIST);
310 static void release_memory_resource(struct resource *res)
314 release_resource(res);
318 static int check_pfn_span(unsigned long pfn, unsigned long nr_pages)
321 * Disallow all operations smaller than a sub-section and only
322 * allow operations smaller than a section for
323 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
324 * enforces a larger memory_block_size_bytes() granularity for
325 * memory that will be marked online, so this check should only
326 * fire for direct arch_{add,remove}_memory() users outside of
327 * add_memory_resource().
329 unsigned long min_align;
331 if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
332 min_align = PAGES_PER_SUBSECTION;
334 min_align = PAGES_PER_SECTION;
335 if (!IS_ALIGNED(pfn | nr_pages, min_align))
341 * Return page for the valid pfn only if the page is online. All pfn
342 * walkers which rely on the fully initialized page->flags and others
343 * should use this rather than pfn_valid && pfn_to_page
345 struct page *pfn_to_online_page(unsigned long pfn)
347 unsigned long nr = pfn_to_section_nr(pfn);
348 struct dev_pagemap *pgmap;
349 struct mem_section *ms;
351 if (nr >= NR_MEM_SECTIONS)
354 ms = __nr_to_section(nr);
355 if (!online_section(ms))
359 * Save some code text when online_section() +
360 * pfn_section_valid() are sufficient.
362 if (IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) && !pfn_valid(pfn))
365 if (!pfn_section_valid(ms, pfn))
368 if (!online_device_section(ms))
369 return pfn_to_page(pfn);
372 * Slowpath: when ZONE_DEVICE collides with
373 * ZONE_{NORMAL,MOVABLE} within the same section some pfns in
374 * the section may be 'offline' but 'valid'. Only
375 * get_dev_pagemap() can determine sub-section online status.
377 pgmap = get_dev_pagemap(pfn, NULL);
378 put_dev_pagemap(pgmap);
380 /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */
384 return pfn_to_page(pfn);
386 EXPORT_SYMBOL_GPL(pfn_to_online_page);
388 int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
389 struct mhp_params *params)
391 const unsigned long end_pfn = pfn + nr_pages;
392 unsigned long cur_nr_pages;
394 struct vmem_altmap *altmap = params->altmap;
396 if (WARN_ON_ONCE(!pgprot_val(params->pgprot)))
399 VM_BUG_ON(!mhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false));
403 * Validate altmap is within bounds of the total request
405 if (altmap->base_pfn != pfn
406 || vmem_altmap_offset(altmap) > nr_pages) {
407 pr_warn_once("memory add fail, invalid altmap\n");
413 if (check_pfn_span(pfn, nr_pages)) {
414 WARN(1, "Misaligned %s start: %#lx end: %#lx\n", __func__, pfn, pfn + nr_pages - 1);
418 for (; pfn < end_pfn; pfn += cur_nr_pages) {
419 /* Select all remaining pages up to the next section boundary */
420 cur_nr_pages = min(end_pfn - pfn,
421 SECTION_ALIGN_UP(pfn + 1) - pfn);
422 err = sparse_add_section(nid, pfn, cur_nr_pages, altmap,
428 vmemmap_populate_print_last();
432 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
433 static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
434 unsigned long start_pfn,
435 unsigned long end_pfn)
437 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
438 if (unlikely(!pfn_to_online_page(start_pfn)))
441 if (unlikely(pfn_to_nid(start_pfn) != nid))
444 if (zone != page_zone(pfn_to_page(start_pfn)))
453 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
454 static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
455 unsigned long start_pfn,
456 unsigned long end_pfn)
460 /* pfn is the end pfn of a memory section. */
462 for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
463 if (unlikely(!pfn_to_online_page(pfn)))
466 if (unlikely(pfn_to_nid(pfn) != nid))
469 if (zone != page_zone(pfn_to_page(pfn)))
478 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
479 unsigned long end_pfn)
482 int nid = zone_to_nid(zone);
484 if (zone->zone_start_pfn == start_pfn) {
486 * If the section is smallest section in the zone, it need
487 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
488 * In this case, we find second smallest valid mem_section
489 * for shrinking zone.
491 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
494 zone->spanned_pages = zone_end_pfn(zone) - pfn;
495 zone->zone_start_pfn = pfn;
497 zone->zone_start_pfn = 0;
498 zone->spanned_pages = 0;
500 } else if (zone_end_pfn(zone) == end_pfn) {
502 * If the section is biggest section in the zone, it need
503 * shrink zone->spanned_pages.
504 * In this case, we find second biggest valid mem_section for
507 pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn,
510 zone->spanned_pages = pfn - zone->zone_start_pfn + 1;
512 zone->zone_start_pfn = 0;
513 zone->spanned_pages = 0;
518 static void update_pgdat_span(struct pglist_data *pgdat)
520 unsigned long node_start_pfn = 0, node_end_pfn = 0;
523 for (zone = pgdat->node_zones;
524 zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
525 unsigned long end_pfn = zone_end_pfn(zone);
527 /* No need to lock the zones, they can't change. */
528 if (!zone->spanned_pages)
531 node_start_pfn = zone->zone_start_pfn;
532 node_end_pfn = end_pfn;
536 if (end_pfn > node_end_pfn)
537 node_end_pfn = end_pfn;
538 if (zone->zone_start_pfn < node_start_pfn)
539 node_start_pfn = zone->zone_start_pfn;
542 pgdat->node_start_pfn = node_start_pfn;
543 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
546 void remove_pfn_range_from_zone(struct zone *zone,
547 unsigned long start_pfn,
548 unsigned long nr_pages)
550 const unsigned long end_pfn = start_pfn + nr_pages;
551 struct pglist_data *pgdat = zone->zone_pgdat;
552 unsigned long pfn, cur_nr_pages;
554 /* Poison struct pages because they are now uninitialized again. */
555 for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {
558 /* Select all remaining pages up to the next section boundary */
560 min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
561 page_init_poison(pfn_to_page(pfn),
562 sizeof(struct page) * cur_nr_pages);
566 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
567 * we will not try to shrink the zones - which is okay as
568 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
570 if (zone_is_zone_device(zone))
573 clear_zone_contiguous(zone);
575 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
576 update_pgdat_span(pgdat);
578 set_zone_contiguous(zone);
582 * __remove_pages() - remove sections of pages
583 * @pfn: starting pageframe (must be aligned to start of a section)
584 * @nr_pages: number of pages to remove (must be multiple of section size)
585 * @altmap: alternative device page map or %NULL if default memmap is used
587 * Generic helper function to remove section mappings and sysfs entries
588 * for the section of the memory we are removing. Caller needs to make
589 * sure that pages are marked reserved and zones are adjust properly by
590 * calling offline_pages().
592 void __remove_pages(unsigned long pfn, unsigned long nr_pages,
593 struct vmem_altmap *altmap)
595 const unsigned long end_pfn = pfn + nr_pages;
596 unsigned long cur_nr_pages;
598 if (check_pfn_span(pfn, nr_pages)) {
599 WARN(1, "Misaligned %s start: %#lx end: %#lx\n", __func__, pfn, pfn + nr_pages - 1);
603 for (; pfn < end_pfn; pfn += cur_nr_pages) {
605 /* Select all remaining pages up to the next section boundary */
606 cur_nr_pages = min(end_pfn - pfn,
607 SECTION_ALIGN_UP(pfn + 1) - pfn);
608 sparse_remove_section(pfn, cur_nr_pages, altmap);
612 int set_online_page_callback(online_page_callback_t callback)
617 mutex_lock(&online_page_callback_lock);
619 if (online_page_callback == generic_online_page) {
620 online_page_callback = callback;
624 mutex_unlock(&online_page_callback_lock);
629 EXPORT_SYMBOL_GPL(set_online_page_callback);
631 int restore_online_page_callback(online_page_callback_t callback)
636 mutex_lock(&online_page_callback_lock);
638 if (online_page_callback == callback) {
639 online_page_callback = generic_online_page;
643 mutex_unlock(&online_page_callback_lock);
648 EXPORT_SYMBOL_GPL(restore_online_page_callback);
650 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
651 void generic_online_page(struct page *page, unsigned int order)
653 __free_pages_core(page, order, MEMINIT_HOTPLUG);
655 EXPORT_SYMBOL_GPL(generic_online_page);
657 static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages)
659 const unsigned long end_pfn = start_pfn + nr_pages;
663 * Online the pages in MAX_PAGE_ORDER aligned chunks. The callback might
664 * decide to not expose all pages to the buddy (e.g., expose them
665 * later). We account all pages as being online and belonging to this
667 * When using memmap_on_memory, the range might not be aligned to
668 * MAX_ORDER_NR_PAGES - 1, but pageblock aligned. __ffs() will detect
669 * this and the first chunk to online will be pageblock_nr_pages.
671 for (pfn = start_pfn; pfn < end_pfn;) {
672 struct page *page = pfn_to_page(pfn);
676 * Free to online pages in the largest chunks alignment allows.
678 * __ffs() behaviour is undefined for 0. start == 0 is
679 * MAX_PAGE_ORDER-aligned, Set order to MAX_PAGE_ORDER for
683 order = min_t(int, MAX_PAGE_ORDER, __ffs(pfn));
685 order = MAX_PAGE_ORDER;
688 * Exposing the page to the buddy by freeing can cause
689 * issues with debug_pagealloc enabled: some archs don't
690 * like double-unmappings. So treat them like any pages that
691 * were allocated from the buddy.
693 debug_pagealloc_map_pages(page, 1 << order);
694 (*online_page_callback)(page, order);
695 pfn += (1UL << order);
698 /* mark all involved sections as online */
699 online_mem_sections(start_pfn, end_pfn);
702 /* check which state of node_states will be changed when online memory */
703 static void node_states_check_changes_online(unsigned long nr_pages,
704 struct zone *zone, struct memory_notify *arg)
706 int nid = zone_to_nid(zone);
708 arg->status_change_nid = NUMA_NO_NODE;
709 arg->status_change_nid_normal = NUMA_NO_NODE;
711 if (!node_state(nid, N_MEMORY))
712 arg->status_change_nid = nid;
713 if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
714 arg->status_change_nid_normal = nid;
717 static void node_states_set_node(int node, struct memory_notify *arg)
719 if (arg->status_change_nid_normal >= 0)
720 node_set_state(node, N_NORMAL_MEMORY);
722 if (arg->status_change_nid >= 0)
723 node_set_state(node, N_MEMORY);
726 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
727 unsigned long nr_pages)
729 unsigned long old_end_pfn = zone_end_pfn(zone);
731 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
732 zone->zone_start_pfn = start_pfn;
734 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
737 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
738 unsigned long nr_pages)
740 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
742 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
743 pgdat->node_start_pfn = start_pfn;
745 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
749 #ifdef CONFIG_ZONE_DEVICE
750 static void section_taint_zone_device(unsigned long pfn)
752 struct mem_section *ms = __pfn_to_section(pfn);
754 ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE;
757 static inline void section_taint_zone_device(unsigned long pfn)
763 * Associate the pfn range with the given zone, initializing the memmaps
764 * and resizing the pgdat/zone data to span the added pages. After this
765 * call, all affected pages are PageOffline().
767 * All aligned pageblocks are initialized to the specified migratetype
768 * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related
769 * zone stats (e.g., nr_isolate_pageblock) are touched.
771 void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
772 unsigned long nr_pages,
773 struct vmem_altmap *altmap, int migratetype)
775 struct pglist_data *pgdat = zone->zone_pgdat;
776 int nid = pgdat->node_id;
778 clear_zone_contiguous(zone);
780 if (zone_is_empty(zone))
781 init_currently_empty_zone(zone, start_pfn, nr_pages);
782 resize_zone_range(zone, start_pfn, nr_pages);
783 resize_pgdat_range(pgdat, start_pfn, nr_pages);
786 * Subsection population requires care in pfn_to_online_page().
787 * Set the taint to enable the slow path detection of
788 * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE}
791 if (zone_is_zone_device(zone)) {
792 if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION))
793 section_taint_zone_device(start_pfn);
794 if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION))
795 section_taint_zone_device(start_pfn + nr_pages);
799 * TODO now we have a visible range of pages which are not associated
800 * with their zone properly. Not nice but set_pfnblock_flags_mask
801 * expects the zone spans the pfn range. All the pages in the range
802 * are reserved so nobody should be touching them so we should be safe
804 memmap_init_range(nr_pages, nid, zone_idx(zone), start_pfn, 0,
805 MEMINIT_HOTPLUG, altmap, migratetype);
807 set_zone_contiguous(zone);
810 struct auto_movable_stats {
811 unsigned long kernel_early_pages;
812 unsigned long movable_pages;
815 static void auto_movable_stats_account_zone(struct auto_movable_stats *stats,
818 if (zone_idx(zone) == ZONE_MOVABLE) {
819 stats->movable_pages += zone->present_pages;
821 stats->kernel_early_pages += zone->present_early_pages;
824 * CMA pages (never on hotplugged memory) behave like
827 stats->movable_pages += zone->cma_pages;
828 stats->kernel_early_pages -= zone->cma_pages;
829 #endif /* CONFIG_CMA */
832 struct auto_movable_group_stats {
833 unsigned long movable_pages;
834 unsigned long req_kernel_early_pages;
837 static int auto_movable_stats_account_group(struct memory_group *group,
840 const int ratio = READ_ONCE(auto_movable_ratio);
841 struct auto_movable_group_stats *stats = arg;
845 * We don't support modifying the config while the auto-movable online
846 * policy is already enabled. Just avoid the division by zero below.
852 * Calculate how many early kernel pages this group requires to
853 * satisfy the configured zone ratio.
855 pages = group->present_movable_pages * 100 / ratio;
856 pages -= group->present_kernel_pages;
859 stats->req_kernel_early_pages += pages;
860 stats->movable_pages += group->present_movable_pages;
864 static bool auto_movable_can_online_movable(int nid, struct memory_group *group,
865 unsigned long nr_pages)
867 unsigned long kernel_early_pages, movable_pages;
868 struct auto_movable_group_stats group_stats = {};
869 struct auto_movable_stats stats = {};
873 /* Walk all relevant zones and collect MOVABLE vs. KERNEL stats. */
874 if (nid == NUMA_NO_NODE) {
875 /* TODO: cache values */
876 for_each_populated_zone(zone)
877 auto_movable_stats_account_zone(&stats, zone);
879 for (i = 0; i < MAX_NR_ZONES; i++) {
880 pg_data_t *pgdat = NODE_DATA(nid);
882 zone = pgdat->node_zones + i;
883 if (populated_zone(zone))
884 auto_movable_stats_account_zone(&stats, zone);
888 kernel_early_pages = stats.kernel_early_pages;
889 movable_pages = stats.movable_pages;
892 * Kernel memory inside dynamic memory group allows for more MOVABLE
893 * memory within the same group. Remove the effect of all but the
894 * current group from the stats.
896 walk_dynamic_memory_groups(nid, auto_movable_stats_account_group,
897 group, &group_stats);
898 if (kernel_early_pages <= group_stats.req_kernel_early_pages)
900 kernel_early_pages -= group_stats.req_kernel_early_pages;
901 movable_pages -= group_stats.movable_pages;
903 if (group && group->is_dynamic)
904 kernel_early_pages += group->present_kernel_pages;
907 * Test if we could online the given number of pages to ZONE_MOVABLE
908 * and still stay in the configured ratio.
910 movable_pages += nr_pages;
911 return movable_pages <= (auto_movable_ratio * kernel_early_pages) / 100;
915 * Returns a default kernel memory zone for the given pfn range.
916 * If no kernel zone covers this pfn range it will automatically go
917 * to the ZONE_NORMAL.
919 static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
920 unsigned long nr_pages)
922 struct pglist_data *pgdat = NODE_DATA(nid);
925 for (zid = 0; zid < ZONE_NORMAL; zid++) {
926 struct zone *zone = &pgdat->node_zones[zid];
928 if (zone_intersects(zone, start_pfn, nr_pages))
932 return &pgdat->node_zones[ZONE_NORMAL];
936 * Determine to which zone to online memory dynamically based on user
937 * configuration and system stats. We care about the following ratio:
941 * Whereby MOVABLE is memory in ZONE_MOVABLE and KERNEL is memory in
942 * one of the kernel zones. CMA pages inside one of the kernel zones really
943 * behaves like ZONE_MOVABLE, so we treat them accordingly.
945 * We don't allow for hotplugged memory in a KERNEL zone to increase the
946 * amount of MOVABLE memory we can have, so we end up with:
948 * MOVABLE : KERNEL_EARLY
950 * Whereby KERNEL_EARLY is memory in one of the kernel zones, available sinze
951 * boot. We base our calculation on KERNEL_EARLY internally, because:
953 * a) Hotplugged memory in one of the kernel zones can sometimes still get
954 * hotunplugged, especially when hot(un)plugging individual memory blocks.
955 * There is no coordination across memory devices, therefore "automatic"
956 * hotunplugging, as implemented in hypervisors, could result in zone
958 * b) Early/boot memory in one of the kernel zones can usually not get
959 * hotunplugged again (e.g., no firmware interface to unplug, fragmented
960 * with unmovable allocations). While there are corner cases where it might
961 * still work, it is barely relevant in practice.
963 * Exceptions are dynamic memory groups, which allow for more MOVABLE
964 * memory within the same memory group -- because in that case, there is
965 * coordination within the single memory device managed by a single driver.
967 * We rely on "present pages" instead of "managed pages", as the latter is
968 * highly unreliable and dynamic in virtualized environments, and does not
969 * consider boot time allocations. For example, memory ballooning adjusts the
970 * managed pages when inflating/deflating the balloon, and balloon compaction
971 * can even migrate inflated pages between zones.
973 * Using "present pages" is better but some things to keep in mind are:
975 * a) Some memblock allocations, such as for the crashkernel area, are
976 * effectively unused by the kernel, yet they account to "present pages".
977 * Fortunately, these allocations are comparatively small in relevant setups
978 * (e.g., fraction of system memory).
979 * b) Some hotplugged memory blocks in virtualized environments, esecially
980 * hotplugged by virtio-mem, look like they are completely present, however,
981 * only parts of the memory block are actually currently usable.
982 * "present pages" is an upper limit that can get reached at runtime. As
983 * we base our calculations on KERNEL_EARLY, this is not an issue.
985 static struct zone *auto_movable_zone_for_pfn(int nid,
986 struct memory_group *group,
988 unsigned long nr_pages)
990 unsigned long online_pages = 0, max_pages, end_pfn;
993 if (!auto_movable_ratio)
996 if (group && !group->is_dynamic) {
997 max_pages = group->s.max_pages;
998 online_pages = group->present_movable_pages;
1000 /* If anything is !MOVABLE online the rest !MOVABLE. */
1001 if (group->present_kernel_pages)
1003 } else if (!group || group->d.unit_pages == nr_pages) {
1004 max_pages = nr_pages;
1006 max_pages = group->d.unit_pages;
1008 * Take a look at all online sections in the current unit.
1009 * We can safely assume that all pages within a section belong
1010 * to the same zone, because dynamic memory groups only deal
1011 * with hotplugged memory.
1013 pfn = ALIGN_DOWN(pfn, group->d.unit_pages);
1014 end_pfn = pfn + group->d.unit_pages;
1015 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1016 page = pfn_to_online_page(pfn);
1019 /* If anything is !MOVABLE online the rest !MOVABLE. */
1020 if (!is_zone_movable_page(page))
1022 online_pages += PAGES_PER_SECTION;
1027 * Online MOVABLE if we could *currently* online all remaining parts
1028 * MOVABLE. We expect to (add+) online them immediately next, so if
1029 * nobody interferes, all will be MOVABLE if possible.
1031 nr_pages = max_pages - online_pages;
1032 if (!auto_movable_can_online_movable(NUMA_NO_NODE, group, nr_pages))
1036 if (auto_movable_numa_aware &&
1037 !auto_movable_can_online_movable(nid, group, nr_pages))
1039 #endif /* CONFIG_NUMA */
1041 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
1043 return default_kernel_zone_for_pfn(nid, pfn, nr_pages);
1046 static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
1047 unsigned long nr_pages)
1049 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
1051 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
1052 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
1053 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
1056 * We inherit the existing zone in a simple case where zones do not
1057 * overlap in the given range
1059 if (in_kernel ^ in_movable)
1060 return (in_kernel) ? kernel_zone : movable_zone;
1063 * If the range doesn't belong to any zone or two zones overlap in the
1064 * given range then we use movable zone only if movable_node is
1065 * enabled because we always online to a kernel zone by default.
1067 return movable_node_enabled ? movable_zone : kernel_zone;
1070 struct zone *zone_for_pfn_range(int online_type, int nid,
1071 struct memory_group *group, unsigned long start_pfn,
1072 unsigned long nr_pages)
1074 if (online_type == MMOP_ONLINE_KERNEL)
1075 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
1077 if (online_type == MMOP_ONLINE_MOVABLE)
1078 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
1080 if (online_policy == ONLINE_POLICY_AUTO_MOVABLE)
1081 return auto_movable_zone_for_pfn(nid, group, start_pfn, nr_pages);
1083 return default_zone_for_pfn(nid, start_pfn, nr_pages);
1087 * This function should only be called by memory_block_{online,offline},
1088 * and {online,offline}_pages.
1090 void adjust_present_page_count(struct page *page, struct memory_group *group,
1093 struct zone *zone = page_zone(page);
1094 const bool movable = zone_idx(zone) == ZONE_MOVABLE;
1097 * We only support onlining/offlining/adding/removing of complete
1098 * memory blocks; therefore, either all is either early or hotplugged.
1100 if (early_section(__pfn_to_section(page_to_pfn(page))))
1101 zone->present_early_pages += nr_pages;
1102 zone->present_pages += nr_pages;
1103 zone->zone_pgdat->node_present_pages += nr_pages;
1105 if (group && movable)
1106 group->present_movable_pages += nr_pages;
1107 else if (group && !movable)
1108 group->present_kernel_pages += nr_pages;
1111 int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
1112 struct zone *zone, bool mhp_off_inaccessible)
1114 unsigned long end_pfn = pfn + nr_pages;
1117 ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
1122 * Memory block is accessible at this stage and hence poison the struct
1123 * pages now. If the memory block is accessible during memory hotplug
1124 * addition phase, then page poisining is already performed in
1125 * sparse_add_section().
1127 if (mhp_off_inaccessible)
1128 page_init_poison(pfn_to_page(pfn), sizeof(struct page) * nr_pages);
1130 move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_UNMOVABLE);
1132 for (i = 0; i < nr_pages; i++) {
1133 struct page *page = pfn_to_page(pfn + i);
1135 __ClearPageOffline(page);
1136 SetPageVmemmapSelfHosted(page);
1140 * It might be that the vmemmap_pages fully span sections. If that is
1141 * the case, mark those sections online here as otherwise they will be
1144 if (nr_pages >= PAGES_PER_SECTION)
1145 online_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION));
1150 void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages)
1152 unsigned long end_pfn = pfn + nr_pages;
1155 * It might be that the vmemmap_pages fully span sections. If that is
1156 * the case, mark those sections offline here as otherwise they will be
1159 if (nr_pages >= PAGES_PER_SECTION)
1160 offline_mem_sections(pfn, ALIGN_DOWN(end_pfn, PAGES_PER_SECTION));
1163 * The pages associated with this vmemmap have been offlined, so
1164 * we can reset its state here.
1166 remove_pfn_range_from_zone(page_zone(pfn_to_page(pfn)), pfn, nr_pages);
1167 kasan_remove_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
1171 * Must be called with mem_hotplug_lock in write mode.
1173 int online_pages(unsigned long pfn, unsigned long nr_pages,
1174 struct zone *zone, struct memory_group *group)
1176 unsigned long flags;
1177 int need_zonelists_rebuild = 0;
1178 const int nid = zone_to_nid(zone);
1180 struct memory_notify arg;
1183 * {on,off}lining is constrained to full memory sections (or more
1184 * precisely to memory blocks from the user space POV).
1185 * memmap_on_memory is an exception because it reserves initial part
1186 * of the physical memory space for vmemmaps. That space is pageblock
1189 if (WARN_ON_ONCE(!nr_pages || !pageblock_aligned(pfn) ||
1190 !IS_ALIGNED(pfn + nr_pages, PAGES_PER_SECTION)))
1194 /* associate pfn range with the zone */
1195 move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE);
1197 arg.start_pfn = pfn;
1198 arg.nr_pages = nr_pages;
1199 node_states_check_changes_online(nr_pages, zone, &arg);
1201 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1202 ret = notifier_to_errno(ret);
1204 goto failed_addition;
1207 * Fixup the number of isolated pageblocks before marking the sections
1208 * onlining, such that undo_isolate_page_range() works correctly.
1210 spin_lock_irqsave(&zone->lock, flags);
1211 zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages;
1212 spin_unlock_irqrestore(&zone->lock, flags);
1215 * If this zone is not populated, then it is not in zonelist.
1216 * This means the page allocator ignores this zone.
1217 * So, zonelist must be updated after online.
1219 if (!populated_zone(zone)) {
1220 need_zonelists_rebuild = 1;
1221 setup_zone_pageset(zone);
1224 online_pages_range(pfn, nr_pages);
1225 adjust_present_page_count(pfn_to_page(pfn), group, nr_pages);
1227 node_states_set_node(nid, &arg);
1228 if (need_zonelists_rebuild)
1229 build_all_zonelists(NULL);
1231 /* Basic onlining is complete, allow allocation of onlined pages. */
1232 undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE);
1235 * Freshly onlined pages aren't shuffled (e.g., all pages are placed to
1236 * the tail of the freelist when undoing isolation). Shuffle the whole
1237 * zone to make sure the just onlined pages are properly distributed
1238 * across the whole freelist - to create an initial shuffle.
1242 /* reinitialise watermarks and update pcp limits */
1243 init_per_zone_wmark_min();
1248 writeback_set_ratelimit();
1250 memory_notify(MEM_ONLINE, &arg);
1254 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1255 (unsigned long long) pfn << PAGE_SHIFT,
1256 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1257 memory_notify(MEM_CANCEL_ONLINE, &arg);
1258 remove_pfn_range_from_zone(zone, pfn, nr_pages);
1262 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1263 static pg_data_t *hotadd_init_pgdat(int nid)
1265 struct pglist_data *pgdat;
1268 * NODE_DATA is preallocated (free_area_init) but its internal
1269 * state is not allocated completely. Add missing pieces.
1270 * Completely offline nodes stay around and they just need
1273 pgdat = NODE_DATA(nid);
1275 /* init node's zones as empty zones, we don't have any present pages.*/
1276 free_area_init_core_hotplug(pgdat);
1279 * The node we allocated has no zone fallback lists. For avoiding
1280 * to access not-initialized zonelist, build here.
1282 build_all_zonelists(pgdat);
1288 * __try_online_node - online a node if offlined
1290 * @set_node_online: Whether we want to online the node
1291 * called by cpu_up() to online a node without onlined memory.
1294 * 1 -> a new node has been allocated
1295 * 0 -> the node is already online
1296 * -ENOMEM -> the node could not be allocated
1298 static int __try_online_node(int nid, bool set_node_online)
1303 if (node_online(nid))
1306 pgdat = hotadd_init_pgdat(nid);
1308 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1313 if (set_node_online) {
1314 node_set_online(nid);
1315 ret = register_one_node(nid);
1323 * Users of this function always want to online/register the node
1325 int try_online_node(int nid)
1329 mem_hotplug_begin();
1330 ret = __try_online_node(nid, true);
1335 static int check_hotplug_memory_range(u64 start, u64 size)
1337 /* memory range must be block size aligned */
1338 if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
1339 !IS_ALIGNED(size, memory_block_size_bytes())) {
1340 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1341 memory_block_size_bytes(), start, size);
1348 static int online_memory_block(struct memory_block *mem, void *arg)
1350 mem->online_type = mhp_get_default_online_type();
1351 return device_online(&mem->dev);
1354 #ifndef arch_supports_memmap_on_memory
1355 static inline bool arch_supports_memmap_on_memory(unsigned long vmemmap_size)
1358 * As default, we want the vmemmap to span a complete PMD such that we
1359 * can map the vmemmap using a single PMD if supported by the
1362 return IS_ALIGNED(vmemmap_size, PMD_SIZE);
1366 bool mhp_supports_memmap_on_memory(void)
1368 unsigned long vmemmap_size = memory_block_memmap_size();
1369 unsigned long memmap_pages = memory_block_memmap_on_memory_pages();
1372 * Besides having arch support and the feature enabled at runtime, we
1373 * need a few more assumptions to hold true:
1375 * a) The vmemmap pages span complete PMDs: We don't want vmemmap code
1376 * to populate memory from the altmap for unrelated parts (i.e.,
1377 * other memory blocks)
1379 * b) The vmemmap pages (and thereby the pages that will be exposed to
1380 * the buddy) have to cover full pageblocks: memory onlining/offlining
1381 * code requires applicable ranges to be page-aligned, for example, to
1382 * set the migratetypes properly.
1384 * TODO: Although we have a check here to make sure that vmemmap pages
1385 * fully populate a PMD, it is not the right place to check for
1386 * this. A much better solution involves improving vmemmap code
1387 * to fallback to base pages when trying to populate vmemmap using
1388 * altmap as an alternative source of memory, and we do not exactly
1389 * populate a single PMD.
1391 if (!mhp_memmap_on_memory())
1395 * Make sure the vmemmap allocation is fully contained
1396 * so that we always allocate vmemmap memory from altmap area.
1398 if (!IS_ALIGNED(vmemmap_size, PAGE_SIZE))
1402 * start pfn should be pageblock_nr_pages aligned for correctly
1403 * setting migrate types
1405 if (!pageblock_aligned(memmap_pages))
1408 if (memmap_pages == PHYS_PFN(memory_block_size_bytes()))
1409 /* No effective hotplugged memory doesn't make sense. */
1412 return arch_supports_memmap_on_memory(vmemmap_size);
1414 EXPORT_SYMBOL_GPL(mhp_supports_memmap_on_memory);
1416 static void remove_memory_blocks_and_altmaps(u64 start, u64 size)
1418 unsigned long memblock_size = memory_block_size_bytes();
1422 * For memmap_on_memory, the altmaps were added on a per-memblock
1423 * basis; we have to process each individual memory block.
1425 for (cur_start = start; cur_start < start + size;
1426 cur_start += memblock_size) {
1427 struct vmem_altmap *altmap = NULL;
1428 struct memory_block *mem;
1430 mem = find_memory_block(pfn_to_section_nr(PFN_DOWN(cur_start)));
1431 if (WARN_ON_ONCE(!mem))
1434 altmap = mem->altmap;
1437 remove_memory_block_devices(cur_start, memblock_size);
1439 arch_remove_memory(cur_start, memblock_size, altmap);
1441 /* Verify that all vmemmap pages have actually been freed. */
1442 WARN(altmap->alloc, "Altmap not fully unmapped");
1447 static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,
1448 u64 start, u64 size, mhp_t mhp_flags)
1450 unsigned long memblock_size = memory_block_size_bytes();
1454 for (cur_start = start; cur_start < start + size;
1455 cur_start += memblock_size) {
1456 struct mhp_params params = { .pgprot =
1457 pgprot_mhp(PAGE_KERNEL) };
1458 struct vmem_altmap mhp_altmap = {
1459 .base_pfn = PHYS_PFN(cur_start),
1460 .end_pfn = PHYS_PFN(cur_start + memblock_size - 1),
1463 mhp_altmap.free = memory_block_memmap_on_memory_pages();
1464 if (mhp_flags & MHP_OFFLINE_INACCESSIBLE)
1465 mhp_altmap.inaccessible = true;
1466 params.altmap = kmemdup(&mhp_altmap, sizeof(struct vmem_altmap),
1468 if (!params.altmap) {
1473 /* call arch's memory hotadd */
1474 ret = arch_add_memory(nid, cur_start, memblock_size, ¶ms);
1476 kfree(params.altmap);
1480 /* create memory block devices after memory was added */
1481 ret = create_memory_block_devices(cur_start, memblock_size,
1482 params.altmap, group);
1484 arch_remove_memory(cur_start, memblock_size, NULL);
1485 kfree(params.altmap);
1492 if (ret && cur_start != start)
1493 remove_memory_blocks_and_altmaps(start, cur_start - start);
1498 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1499 * and online/offline operations (triggered e.g. by sysfs).
1501 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1503 int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
1505 struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
1506 enum memblock_flags memblock_flags = MEMBLOCK_NONE;
1507 struct memory_group *group = NULL;
1509 bool new_node = false;
1513 size = resource_size(res);
1515 ret = check_hotplug_memory_range(start, size);
1519 if (mhp_flags & MHP_NID_IS_MGID) {
1520 group = memory_group_find_by_id(nid);
1526 if (!node_possible(nid)) {
1527 WARN(1, "node %d was absent from the node_possible_map\n", nid);
1531 mem_hotplug_begin();
1533 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) {
1534 if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
1535 memblock_flags = MEMBLOCK_DRIVER_MANAGED;
1536 ret = memblock_add_node(start, size, nid, memblock_flags);
1538 goto error_mem_hotplug_end;
1541 ret = __try_online_node(nid, false);
1547 * Self hosted memmap array
1549 if ((mhp_flags & MHP_MEMMAP_ON_MEMORY) &&
1550 mhp_supports_memmap_on_memory()) {
1551 ret = create_altmaps_and_memory_blocks(nid, group, start, size, mhp_flags);
1555 ret = arch_add_memory(nid, start, size, ¶ms);
1559 /* create memory block devices after memory was added */
1560 ret = create_memory_block_devices(start, size, NULL, group);
1562 arch_remove_memory(start, size, params.altmap);
1568 /* If sysfs file of new node can't be created, cpu on the node
1569 * can't be hot-added. There is no rollback way now.
1570 * So, check by BUG_ON() to catch it reluctantly..
1571 * We online node here. We can't roll back from here.
1573 node_set_online(nid);
1574 ret = __register_one_node(nid);
1578 register_memory_blocks_under_node(nid, PFN_DOWN(start),
1579 PFN_UP(start + size - 1),
1582 /* create new memmap entry */
1583 if (!strcmp(res->name, "System RAM"))
1584 firmware_map_add_hotplug(start, start + size, "System RAM");
1586 /* device_online() will take the lock when calling online_pages() */
1590 * In case we're allowed to merge the resource, flag it and trigger
1591 * merging now that adding succeeded.
1593 if (mhp_flags & MHP_MERGE_RESOURCE)
1594 merge_system_ram_resource(res);
1596 /* online pages if requested */
1597 if (mhp_get_default_online_type() != MMOP_OFFLINE)
1598 walk_memory_blocks(start, size, NULL, online_memory_block);
1602 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
1603 memblock_remove(start, size);
1604 error_mem_hotplug_end:
1609 /* requires device_hotplug_lock, see add_memory_resource() */
1610 int __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
1612 struct resource *res;
1615 res = register_memory_resource(start, size, "System RAM");
1617 return PTR_ERR(res);
1619 ret = add_memory_resource(nid, res, mhp_flags);
1621 release_memory_resource(res);
1625 int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
1629 lock_device_hotplug();
1630 rc = __add_memory(nid, start, size, mhp_flags);
1631 unlock_device_hotplug();
1635 EXPORT_SYMBOL_GPL(add_memory);
1638 * Add special, driver-managed memory to the system as system RAM. Such
1639 * memory is not exposed via the raw firmware-provided memmap as system
1640 * RAM, instead, it is detected and added by a driver - during cold boot,
1641 * after a reboot, and after kexec.
1643 * Reasons why this memory should not be used for the initial memmap of a
1644 * kexec kernel or for placing kexec images:
1645 * - The booting kernel is in charge of determining how this memory will be
1646 * used (e.g., use persistent memory as system RAM)
1647 * - Coordination with a hypervisor is required before this memory
1648 * can be used (e.g., inaccessible parts).
1650 * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided
1651 * memory map") are created. Also, the created memory resource is flagged
1652 * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case
1653 * this memory as well (esp., not place kexec images onto it).
1655 * The resource_name (visible via /proc/iomem) has to have the format
1656 * "System RAM ($DRIVER)".
1658 int add_memory_driver_managed(int nid, u64 start, u64 size,
1659 const char *resource_name, mhp_t mhp_flags)
1661 struct resource *res;
1664 if (!resource_name ||
1665 strstr(resource_name, "System RAM (") != resource_name ||
1666 resource_name[strlen(resource_name) - 1] != ')')
1669 lock_device_hotplug();
1671 res = register_memory_resource(start, size, resource_name);
1677 rc = add_memory_resource(nid, res, mhp_flags);
1679 release_memory_resource(res);
1682 unlock_device_hotplug();
1685 EXPORT_SYMBOL_GPL(add_memory_driver_managed);
1688 * Platforms should define arch_get_mappable_range() that provides
1689 * maximum possible addressable physical memory range for which the
1690 * linear mapping could be created. The platform returned address
1691 * range must adhere to these following semantics.
1693 * - range.start <= range.end
1694 * - Range includes both end points [range.start..range.end]
1696 * There is also a fallback definition provided here, allowing the
1697 * entire possible physical address range in case any platform does
1698 * not define arch_get_mappable_range().
1700 struct range __weak arch_get_mappable_range(void)
1702 struct range mhp_range = {
1709 struct range mhp_get_pluggable_range(bool need_mapping)
1711 const u64 max_phys = DIRECT_MAP_PHYSMEM_END;
1712 struct range mhp_range;
1715 mhp_range = arch_get_mappable_range();
1716 if (mhp_range.start > max_phys) {
1717 mhp_range.start = 0;
1720 mhp_range.end = min_t(u64, mhp_range.end, max_phys);
1722 mhp_range.start = 0;
1723 mhp_range.end = max_phys;
1727 EXPORT_SYMBOL_GPL(mhp_get_pluggable_range);
1729 bool mhp_range_allowed(u64 start, u64 size, bool need_mapping)
1731 struct range mhp_range = mhp_get_pluggable_range(need_mapping);
1732 u64 end = start + size;
1734 if (start < end && start >= mhp_range.start && (end - 1) <= mhp_range.end)
1737 pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n",
1738 start, end, mhp_range.start, mhp_range.end);
1742 #ifdef CONFIG_MEMORY_HOTREMOVE
1744 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1745 * non-lru movable pages and hugepages). Will skip over most unmovable
1746 * pages (esp., pages that can be skipped when offlining), but bail out on
1747 * definitely unmovable pages.
1750 * 0 in case a movable page is found and movable_pfn was updated.
1751 * -ENOENT in case no movable page was found.
1752 * -EBUSY in case a definitely unmovable page was found.
1754 static int scan_movable_pages(unsigned long start, unsigned long end,
1755 unsigned long *movable_pfn)
1759 for_each_valid_pfn(pfn, start, end) {
1761 struct folio *folio;
1763 page = pfn_to_page(pfn);
1766 if (__PageMovable(page))
1770 * PageOffline() pages that are not marked __PageMovable() and
1771 * have a reference count > 0 (after MEM_GOING_OFFLINE) are
1772 * definitely unmovable. If their reference count would be 0,
1773 * they could at least be skipped when offlining memory.
1775 if (PageOffline(page) && page_count(page))
1778 if (!PageHuge(page))
1780 folio = page_folio(page);
1782 * This test is racy as we hold no reference or lock. The
1783 * hugetlb page could have been free'ed and head is no longer
1784 * a hugetlb page before the following check. In such unlikely
1785 * cases false positives and negatives are possible. Calling
1786 * code must deal with these scenarios.
1788 if (folio_test_hugetlb_migratable(folio))
1790 pfn |= folio_nr_pages(folio) - 1;
1798 static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1800 struct folio *folio;
1803 static DEFINE_RATELIMIT_STATE(migrate_rs, DEFAULT_RATELIMIT_INTERVAL,
1804 DEFAULT_RATELIMIT_BURST);
1806 for_each_valid_pfn(pfn, start_pfn, end_pfn) {
1809 page = pfn_to_page(pfn);
1810 folio = page_folio(page);
1812 if (!folio_try_get(folio))
1815 if (unlikely(page_folio(page) != folio))
1818 if (folio_test_large(folio))
1819 pfn = folio_pfn(folio) + folio_nr_pages(folio) - 1;
1821 if (folio_contain_hwpoisoned_page(folio)) {
1822 if (WARN_ON(folio_test_lru(folio)))
1823 folio_isolate_lru(folio);
1824 if (folio_mapped(folio)) {
1826 unmap_poisoned_folio(folio, pfn, false);
1827 folio_unlock(folio);
1833 if (!isolate_folio_to_list(folio, &source)) {
1834 if (__ratelimit(&migrate_rs)) {
1835 pr_warn("failed to isolate pfn %lx\n",
1837 dump_page(page, "isolation failed");
1843 if (!list_empty(&source)) {
1844 nodemask_t nmask = node_states[N_MEMORY];
1845 struct migration_target_control mtc = {
1847 .gfp_mask = GFP_KERNEL | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL,
1848 .reason = MR_MEMORY_HOTPLUG,
1853 * We have checked that migration range is on a single zone so
1854 * we can use the nid of the first page to all the others.
1856 mtc.nid = folio_nid(list_first_entry(&source, struct folio, lru));
1859 * try to allocate from a different node but reuse this node
1860 * if there are no other online nodes to be used (e.g. we are
1861 * offlining a part of the only existing node)
1863 node_clear(mtc.nid, nmask);
1864 if (nodes_empty(nmask))
1865 node_set(mtc.nid, nmask);
1866 ret = migrate_pages(&source, alloc_migration_target, NULL,
1867 (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG, NULL);
1869 list_for_each_entry(folio, &source, lru) {
1870 if (__ratelimit(&migrate_rs)) {
1871 pr_warn("migrating pfn %lx failed ret:%d\n",
1872 folio_pfn(folio), ret);
1873 dump_page(&folio->page,
1874 "migration failure");
1877 putback_movable_pages(&source);
1882 static int __init cmdline_parse_movable_node(char *p)
1884 movable_node_enabled = true;
1887 early_param("movable_node", cmdline_parse_movable_node);
1889 /* check which state of node_states will be changed when offline memory */
1890 static void node_states_check_changes_offline(unsigned long nr_pages,
1891 struct zone *zone, struct memory_notify *arg)
1893 struct pglist_data *pgdat = zone->zone_pgdat;
1894 unsigned long present_pages = 0;
1897 arg->status_change_nid = NUMA_NO_NODE;
1898 arg->status_change_nid_normal = NUMA_NO_NODE;
1901 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
1902 * If the memory to be offline is within the range
1903 * [0..ZONE_NORMAL], and it is the last present memory there,
1904 * the zones in that range will become empty after the offlining,
1905 * thus we can determine that we need to clear the node from
1906 * node_states[N_NORMAL_MEMORY].
1908 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1909 present_pages += pgdat->node_zones[zt].present_pages;
1910 if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
1911 arg->status_change_nid_normal = zone_to_nid(zone);
1914 * We have accounted the pages from [0..ZONE_NORMAL); ZONE_HIGHMEM
1915 * does not apply as we don't support 32bit.
1916 * Here we count the possible pages from ZONE_MOVABLE.
1917 * If after having accounted all the pages, we see that the nr_pages
1918 * to be offlined is over or equal to the accounted pages,
1919 * we know that the node will become empty, and so, we can clear
1920 * it for N_MEMORY as well.
1922 present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1924 if (nr_pages >= present_pages)
1925 arg->status_change_nid = zone_to_nid(zone);
1928 static void node_states_clear_node(int node, struct memory_notify *arg)
1930 if (arg->status_change_nid_normal >= 0)
1931 node_clear_state(node, N_NORMAL_MEMORY);
1933 if (arg->status_change_nid >= 0)
1934 node_clear_state(node, N_MEMORY);
1937 static int count_system_ram_pages_cb(unsigned long start_pfn,
1938 unsigned long nr_pages, void *data)
1940 unsigned long *nr_system_ram_pages = data;
1942 *nr_system_ram_pages += nr_pages;
1947 * Must be called with mem_hotplug_lock in write mode.
1949 int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
1950 struct zone *zone, struct memory_group *group)
1952 const unsigned long end_pfn = start_pfn + nr_pages;
1953 unsigned long pfn, managed_pages, system_ram_pages = 0;
1954 const int node = zone_to_nid(zone);
1955 unsigned long flags;
1956 struct memory_notify arg;
1961 * {on,off}lining is constrained to full memory sections (or more
1962 * precisely to memory blocks from the user space POV).
1963 * memmap_on_memory is an exception because it reserves initial part
1964 * of the physical memory space for vmemmaps. That space is pageblock
1967 if (WARN_ON_ONCE(!nr_pages || !pageblock_aligned(start_pfn) ||
1968 !IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)))
1972 * Don't allow to offline memory blocks that contain holes.
1973 * Consequently, memory blocks with holes can never get onlined
1974 * via the hotplug path - online_pages() - as hotplugged memory has
1975 * no holes. This way, we don't have to worry about memory holes,
1976 * don't need pfn_valid() checks, and can avoid using
1977 * walk_system_ram_range() later.
1979 walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages,
1980 count_system_ram_pages_cb);
1981 if (system_ram_pages != nr_pages) {
1983 reason = "memory holes";
1984 goto failed_removal;
1988 * We only support offlining of memory blocks managed by a single zone,
1989 * checked by calling code. This is just a sanity check that we might
1990 * want to remove in the future.
1992 if (WARN_ON_ONCE(page_zone(pfn_to_page(start_pfn)) != zone ||
1993 page_zone(pfn_to_page(end_pfn - 1)) != zone)) {
1995 reason = "multizone range";
1996 goto failed_removal;
2000 * Disable pcplists so that page isolation cannot race with freeing
2001 * in a way that pages from isolated pageblock are left on pcplists.
2003 zone_pcp_disable(zone);
2004 lru_cache_disable();
2006 /* set above range as isolated */
2007 ret = start_isolate_page_range(start_pfn, end_pfn,
2009 MEMORY_OFFLINE | REPORT_FAILURE);
2011 reason = "failure to isolate range";
2012 goto failed_removal_pcplists_disabled;
2015 arg.start_pfn = start_pfn;
2016 arg.nr_pages = nr_pages;
2017 node_states_check_changes_offline(nr_pages, zone, &arg);
2019 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
2020 ret = notifier_to_errno(ret);
2022 reason = "notifier failure";
2023 goto failed_removal_isolated;
2030 * Historically we always checked for any signal and
2031 * can't limit it to fatal signals without eventually
2032 * breaking user space.
2034 if (signal_pending(current)) {
2036 reason = "signal backoff";
2037 goto failed_removal_isolated;
2042 ret = scan_movable_pages(pfn, end_pfn, &pfn);
2045 * TODO: fatal migration failures should bail
2048 do_migrate_range(pfn, end_pfn);
2052 if (ret != -ENOENT) {
2053 reason = "unmovable page";
2054 goto failed_removal_isolated;
2058 * Dissolve free hugetlb folios in the memory block before doing
2059 * offlining actually in order to make hugetlbfs's object
2060 * counting consistent.
2062 ret = dissolve_free_hugetlb_folios(start_pfn, end_pfn);
2064 reason = "failure to dissolve huge pages";
2065 goto failed_removal_isolated;
2068 ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE);
2072 /* Mark all sections offline and remove free pages from the buddy. */
2073 managed_pages = __offline_isolated_pages(start_pfn, end_pfn);
2074 pr_debug("Offlined Pages %ld\n", nr_pages);
2077 * The memory sections are marked offline, and the pageblock flags
2078 * effectively stale; nobody should be touching them. Fixup the number
2079 * of isolated pageblocks, memory onlining will properly revert this.
2081 spin_lock_irqsave(&zone->lock, flags);
2082 zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;
2083 spin_unlock_irqrestore(&zone->lock, flags);
2086 zone_pcp_enable(zone);
2088 /* removal success */
2089 adjust_managed_page_count(pfn_to_page(start_pfn), -managed_pages);
2090 adjust_present_page_count(pfn_to_page(start_pfn), group, -nr_pages);
2092 /* reinitialise watermarks and update pcp limits */
2093 init_per_zone_wmark_min();
2096 * Make sure to mark the node as memory-less before rebuilding the zone
2097 * list. Otherwise this node would still appear in the fallback lists.
2099 node_states_clear_node(node, &arg);
2100 if (!populated_zone(zone)) {
2101 zone_pcp_reset(zone);
2102 build_all_zonelists(NULL);
2105 if (arg.status_change_nid >= 0) {
2106 kcompactd_stop(node);
2110 writeback_set_ratelimit();
2112 memory_notify(MEM_OFFLINE, &arg);
2113 remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
2116 failed_removal_isolated:
2117 /* pushback to free area */
2118 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
2119 memory_notify(MEM_CANCEL_OFFLINE, &arg);
2120 failed_removal_pcplists_disabled:
2122 zone_pcp_enable(zone);
2124 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
2125 (unsigned long long) start_pfn << PAGE_SHIFT,
2126 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
2131 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
2136 if (unlikely(mem->state != MEM_OFFLINE)) {
2137 phys_addr_t beginpa, endpa;
2139 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2140 endpa = beginpa + memory_block_size_bytes() - 1;
2141 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
2149 static int count_memory_range_altmaps_cb(struct memory_block *mem, void *arg)
2151 u64 *num_altmaps = (u64 *)arg;
2159 static int check_cpu_on_node(int nid)
2163 for_each_present_cpu(cpu) {
2164 if (cpu_to_node(cpu) == nid)
2166 * the cpu on this node isn't removed, and we can't
2167 * offline this node.
2175 static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
2177 int nid = *(int *)arg;
2180 * If a memory block belongs to multiple nodes, the stored nid is not
2181 * reliable. However, such blocks are always online (e.g., cannot get
2182 * offlined) and, therefore, are still spanned by the node.
2184 return mem->nid == nid ? -EEXIST : 0;
2191 * Offline a node if all memory sections and cpus of the node are removed.
2193 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2194 * and online/offline operations before this call.
2196 void try_offline_node(int nid)
2201 * If the node still spans pages (especially ZONE_DEVICE), don't
2202 * offline it. A node spans memory after move_pfn_range_to_zone(),
2203 * e.g., after the memory block was onlined.
2205 if (node_spanned_pages(nid))
2209 * Especially offline memory blocks might not be spanned by the
2210 * node. They will get spanned by the node once they get onlined.
2211 * However, they link to the node in sysfs and can get onlined later.
2213 rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
2217 if (check_cpu_on_node(nid))
2221 * all memory/cpu of this node are removed, we can offline this
2224 node_set_offline(nid);
2225 unregister_one_node(nid);
2227 EXPORT_SYMBOL(try_offline_node);
2229 static int memory_blocks_have_altmaps(u64 start, u64 size)
2231 u64 num_memblocks = size / memory_block_size_bytes();
2232 u64 num_altmaps = 0;
2234 if (!mhp_memmap_on_memory())
2237 walk_memory_blocks(start, size, &num_altmaps,
2238 count_memory_range_altmaps_cb);
2240 if (num_altmaps == 0)
2243 if (WARN_ON_ONCE(num_memblocks != num_altmaps))
2249 static int try_remove_memory(u64 start, u64 size)
2251 int rc, nid = NUMA_NO_NODE;
2253 BUG_ON(check_hotplug_memory_range(start, size));
2256 * All memory blocks must be offlined before removing memory. Check
2257 * whether all memory blocks in question are offline and return error
2258 * if this is not the case.
2260 * While at it, determine the nid. Note that if we'd have mixed nodes,
2261 * we'd only try to offline the last determined one -- which is good
2262 * enough for the cases we care about.
2264 rc = walk_memory_blocks(start, size, &nid, check_memblock_offlined_cb);
2268 /* remove memmap entry */
2269 firmware_map_remove(start, start + size, "System RAM");
2271 mem_hotplug_begin();
2273 rc = memory_blocks_have_altmaps(start, size);
2279 * Memory block device removal under the device_hotplug_lock is
2280 * a barrier against racing online attempts.
2281 * No altmaps present, do the removal directly
2283 remove_memory_block_devices(start, size);
2284 arch_remove_memory(start, size, NULL);
2286 /* all memblocks in the range have altmaps */
2287 remove_memory_blocks_and_altmaps(start, size);
2290 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
2291 memblock_remove(start, size);
2293 release_mem_region_adjustable(start, size);
2295 if (nid != NUMA_NO_NODE)
2296 try_offline_node(nid);
2303 * __remove_memory - Remove memory if every memory block is offline
2304 * @start: physical address of the region to remove
2305 * @size: size of the region to remove
2307 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2308 * and online/offline operations before this call, as required by
2309 * try_offline_node().
2311 void __remove_memory(u64 start, u64 size)
2315 * trigger BUG() if some memory is not offlined prior to calling this
2318 if (try_remove_memory(start, size))
2323 * Remove memory if every memory block is offline, otherwise return -EBUSY is
2324 * some memory is not offline
2326 int remove_memory(u64 start, u64 size)
2330 lock_device_hotplug();
2331 rc = try_remove_memory(start, size);
2332 unlock_device_hotplug();
2336 EXPORT_SYMBOL_GPL(remove_memory);
2338 static int try_offline_memory_block(struct memory_block *mem, void *arg)
2340 uint8_t online_type = MMOP_ONLINE_KERNEL;
2341 uint8_t **online_types = arg;
2346 * Sense the online_type via the zone of the memory block. Offlining
2347 * with multiple zones within one memory block will be rejected
2348 * by offlining code ... so we don't care about that.
2350 page = pfn_to_online_page(section_nr_to_pfn(mem->start_section_nr));
2351 if (page && zone_idx(page_zone(page)) == ZONE_MOVABLE)
2352 online_type = MMOP_ONLINE_MOVABLE;
2354 rc = device_offline(&mem->dev);
2356 * Default is MMOP_OFFLINE - change it only if offlining succeeded,
2357 * so try_reonline_memory_block() can do the right thing.
2360 **online_types = online_type;
2363 /* Ignore if already offline. */
2364 return rc < 0 ? rc : 0;
2367 static int try_reonline_memory_block(struct memory_block *mem, void *arg)
2369 uint8_t **online_types = arg;
2372 if (**online_types != MMOP_OFFLINE) {
2373 mem->online_type = **online_types;
2374 rc = device_online(&mem->dev);
2376 pr_warn("%s: Failed to re-online memory: %d",
2380 /* Continue processing all remaining memory blocks. */
2386 * Try to offline and remove memory. Might take a long time to finish in case
2387 * memory is still in use. Primarily useful for memory devices that logically
2388 * unplugged all memory (so it's no longer in use) and want to offline + remove
2391 int offline_and_remove_memory(u64 start, u64 size)
2393 const unsigned long mb_count = size / memory_block_size_bytes();
2394 uint8_t *online_types, *tmp;
2397 if (!IS_ALIGNED(start, memory_block_size_bytes()) ||
2398 !IS_ALIGNED(size, memory_block_size_bytes()) || !size)
2402 * We'll remember the old online type of each memory block, so we can
2403 * try to revert whatever we did when offlining one memory block fails
2404 * after offlining some others succeeded.
2406 online_types = kmalloc_array(mb_count, sizeof(*online_types),
2411 * Initialize all states to MMOP_OFFLINE, so when we abort processing in
2412 * try_offline_memory_block(), we'll skip all unprocessed blocks in
2413 * try_reonline_memory_block().
2415 memset(online_types, MMOP_OFFLINE, mb_count);
2417 lock_device_hotplug();
2420 rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block);
2423 * In case we succeeded to offline all memory, remove it.
2424 * This cannot fail as it cannot get onlined in the meantime.
2427 rc = try_remove_memory(start, size);
2429 pr_err("%s: Failed to remove memory: %d", __func__, rc);
2433 * Rollback what we did. While memory onlining might theoretically fail
2434 * (nacked by a notifier), it barely ever happens.
2438 walk_memory_blocks(start, size, &tmp,
2439 try_reonline_memory_block);
2441 unlock_device_hotplug();
2443 kfree(online_types);
2446 EXPORT_SYMBOL_GPL(offline_and_remove_memory);
2447 #endif /* CONFIG_MEMORY_HOTREMOVE */