2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/sched/signal.h>
10 #include <linux/swap.h>
11 #include <linux/interrupt.h>
12 #include <linux/pagemap.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
15 #include <linux/pagevec.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/highmem.h>
24 #include <linux/vmalloc.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/migrate.h>
28 #include <linux/page-isolation.h>
29 #include <linux/pfn.h>
30 #include <linux/suspend.h>
31 #include <linux/mm_inline.h>
32 #include <linux/firmware-map.h>
33 #include <linux/stop_machine.h>
34 #include <linux/hugetlb.h>
35 #include <linux/memblock.h>
36 #include <linux/bootmem.h>
37 #include <linux/compaction.h>
39 #include <asm/tlbflush.h>
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
50 static void generic_online_page(struct page *page);
52 static online_page_callback_t online_page_callback = generic_online_page;
53 static DEFINE_MUTEX(online_page_callback_lock);
55 /* The same as the cpu_hotplug lock, but for memory hotplug. */
57 struct task_struct *active_writer;
58 struct mutex lock; /* Synchronizes accesses to refcount, */
60 * Also blocks the new readers during
61 * an ongoing mem hotplug operation.
65 #ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map;
69 .active_writer = NULL,
70 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
72 #ifdef CONFIG_DEBUG_LOCK_ALLOC
73 .dep_map = {.name = "mem_hotplug.lock" },
77 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
78 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
79 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
80 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
82 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
83 bool memhp_auto_online;
85 bool memhp_auto_online = true;
87 EXPORT_SYMBOL_GPL(memhp_auto_online);
89 static int __init setup_memhp_default_state(char *str)
91 if (!strcmp(str, "online"))
92 memhp_auto_online = true;
93 else if (!strcmp(str, "offline"))
94 memhp_auto_online = false;
98 __setup("memhp_default_state=", setup_memhp_default_state);
100 void get_online_mems(void)
103 if (mem_hotplug.active_writer == current)
105 memhp_lock_acquire_read();
106 mutex_lock(&mem_hotplug.lock);
107 mem_hotplug.refcount++;
108 mutex_unlock(&mem_hotplug.lock);
112 void put_online_mems(void)
114 if (mem_hotplug.active_writer == current)
116 mutex_lock(&mem_hotplug.lock);
118 if (WARN_ON(!mem_hotplug.refcount))
119 mem_hotplug.refcount++; /* try to fix things up */
121 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
122 wake_up_process(mem_hotplug.active_writer);
123 mutex_unlock(&mem_hotplug.lock);
124 memhp_lock_release();
128 /* Serializes write accesses to mem_hotplug.active_writer. */
129 static DEFINE_MUTEX(memory_add_remove_lock);
131 void mem_hotplug_begin(void)
133 mutex_lock(&memory_add_remove_lock);
135 mem_hotplug.active_writer = current;
137 memhp_lock_acquire();
139 mutex_lock(&mem_hotplug.lock);
140 if (likely(!mem_hotplug.refcount))
142 __set_current_state(TASK_UNINTERRUPTIBLE);
143 mutex_unlock(&mem_hotplug.lock);
148 void mem_hotplug_done(void)
150 mem_hotplug.active_writer = NULL;
151 mutex_unlock(&mem_hotplug.lock);
152 memhp_lock_release();
153 mutex_unlock(&memory_add_remove_lock);
156 /* add this memory to iomem resource */
157 static struct resource *register_memory_resource(u64 start, u64 size)
159 struct resource *res;
160 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
162 return ERR_PTR(-ENOMEM);
164 res->name = "System RAM";
166 res->end = start + size - 1;
167 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
168 if (request_resource(&iomem_resource, res) < 0) {
169 pr_debug("System RAM resource %pR cannot be added\n", res);
171 return ERR_PTR(-EEXIST);
176 static void release_memory_resource(struct resource *res)
180 release_resource(res);
185 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
186 void get_page_bootmem(unsigned long info, struct page *page,
189 page->freelist = (void *)type;
190 SetPagePrivate(page);
191 set_page_private(page, info);
195 void put_page_bootmem(struct page *page)
199 type = (unsigned long) page->freelist;
200 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
201 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
203 if (page_ref_dec_return(page) == 1) {
204 page->freelist = NULL;
205 ClearPagePrivate(page);
206 set_page_private(page, 0);
207 INIT_LIST_HEAD(&page->lru);
208 free_reserved_page(page);
212 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
213 #ifndef CONFIG_SPARSEMEM_VMEMMAP
214 static void register_page_bootmem_info_section(unsigned long start_pfn)
216 unsigned long *usemap, mapsize, section_nr, i;
217 struct mem_section *ms;
218 struct page *page, *memmap;
220 section_nr = pfn_to_section_nr(start_pfn);
221 ms = __nr_to_section(section_nr);
223 /* Get section's memmap address */
224 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
227 * Get page for the memmap's phys address
228 * XXX: need more consideration for sparse_vmemmap...
230 page = virt_to_page(memmap);
231 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
232 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
234 /* remember memmap's page */
235 for (i = 0; i < mapsize; i++, page++)
236 get_page_bootmem(section_nr, page, SECTION_INFO);
238 usemap = __nr_to_section(section_nr)->pageblock_flags;
239 page = virt_to_page(usemap);
241 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
243 for (i = 0; i < mapsize; i++, page++)
244 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
247 #else /* CONFIG_SPARSEMEM_VMEMMAP */
248 static void register_page_bootmem_info_section(unsigned long start_pfn)
250 unsigned long *usemap, mapsize, section_nr, i;
251 struct mem_section *ms;
252 struct page *page, *memmap;
254 if (!pfn_valid(start_pfn))
257 section_nr = pfn_to_section_nr(start_pfn);
258 ms = __nr_to_section(section_nr);
260 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
262 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
264 usemap = __nr_to_section(section_nr)->pageblock_flags;
265 page = virt_to_page(usemap);
267 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
269 for (i = 0; i < mapsize; i++, page++)
270 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
272 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
274 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
276 unsigned long i, pfn, end_pfn, nr_pages;
277 int node = pgdat->node_id;
280 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
281 page = virt_to_page(pgdat);
283 for (i = 0; i < nr_pages; i++, page++)
284 get_page_bootmem(node, page, NODE_INFO);
286 pfn = pgdat->node_start_pfn;
287 end_pfn = pgdat_end_pfn(pgdat);
289 /* register section info */
290 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
292 * Some platforms can assign the same pfn to multiple nodes - on
293 * node0 as well as nodeN. To avoid registering a pfn against
294 * multiple nodes we check that this pfn does not already
295 * reside in some other nodes.
297 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
298 register_page_bootmem_info_section(pfn);
301 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
303 static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
309 if (pfn_valid(phys_start_pfn))
312 ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn);
317 * Make all the pages reserved so that nobody will stumble over half
319 * FIXME: We also have to associate it with a node because pfn_to_node
320 * relies on having page with the proper node.
322 for (i = 0; i < PAGES_PER_SECTION; i++) {
323 unsigned long pfn = phys_start_pfn + i;
328 page = pfn_to_page(pfn);
329 set_page_node(page, nid);
330 SetPageReserved(page);
336 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
340 * Reasonably generic function for adding memory. It is
341 * expected that archs that support memory hotplug will
342 * call this function after deciding the zone to which to
345 int __ref __add_pages(int nid, unsigned long phys_start_pfn,
346 unsigned long nr_pages, bool want_memblock)
350 int start_sec, end_sec;
351 struct vmem_altmap *altmap;
353 /* during initialize mem_map, align hot-added range to section */
354 start_sec = pfn_to_section_nr(phys_start_pfn);
355 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
357 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
360 * Validate altmap is within bounds of the total request
362 if (altmap->base_pfn != phys_start_pfn
363 || vmem_altmap_offset(altmap) > nr_pages) {
364 pr_warn_once("memory add fail, invalid altmap\n");
371 for (i = start_sec; i <= end_sec; i++) {
372 err = __add_section(nid, section_nr_to_pfn(i), want_memblock);
375 * EEXIST is finally dealt with by ioresource collision
376 * check. see add_memory() => register_memory_resource()
377 * Warning will be printed if there is collision.
379 if (err && (err != -EEXIST))
383 vmemmap_populate_print_last();
387 EXPORT_SYMBOL_GPL(__add_pages);
389 #ifdef CONFIG_MEMORY_HOTREMOVE
390 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
391 static int find_smallest_section_pfn(int nid, struct zone *zone,
392 unsigned long start_pfn,
393 unsigned long end_pfn)
395 struct mem_section *ms;
397 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
398 ms = __pfn_to_section(start_pfn);
400 if (unlikely(!valid_section(ms)))
403 if (unlikely(pfn_to_nid(start_pfn) != nid))
406 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
415 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
416 static int find_biggest_section_pfn(int nid, struct zone *zone,
417 unsigned long start_pfn,
418 unsigned long end_pfn)
420 struct mem_section *ms;
423 /* pfn is the end pfn of a memory section. */
425 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
426 ms = __pfn_to_section(pfn);
428 if (unlikely(!valid_section(ms)))
431 if (unlikely(pfn_to_nid(pfn) != nid))
434 if (zone && zone != page_zone(pfn_to_page(pfn)))
443 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
444 unsigned long end_pfn)
446 unsigned long zone_start_pfn = zone->zone_start_pfn;
447 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
448 unsigned long zone_end_pfn = z;
450 struct mem_section *ms;
451 int nid = zone_to_nid(zone);
453 zone_span_writelock(zone);
454 if (zone_start_pfn == start_pfn) {
456 * If the section is smallest section in the zone, it need
457 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
458 * In this case, we find second smallest valid mem_section
459 * for shrinking zone.
461 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
464 zone->zone_start_pfn = pfn;
465 zone->spanned_pages = zone_end_pfn - pfn;
467 } else if (zone_end_pfn == end_pfn) {
469 * If the section is biggest section in the zone, it need
470 * shrink zone->spanned_pages.
471 * In this case, we find second biggest valid mem_section for
474 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
477 zone->spanned_pages = pfn - zone_start_pfn + 1;
481 * The section is not biggest or smallest mem_section in the zone, it
482 * only creates a hole in the zone. So in this case, we need not
483 * change the zone. But perhaps, the zone has only hole data. Thus
484 * it check the zone has only hole or not.
486 pfn = zone_start_pfn;
487 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
488 ms = __pfn_to_section(pfn);
490 if (unlikely(!valid_section(ms)))
493 if (page_zone(pfn_to_page(pfn)) != zone)
496 /* If the section is current section, it continues the loop */
497 if (start_pfn == pfn)
500 /* If we find valid section, we have nothing to do */
501 zone_span_writeunlock(zone);
505 /* The zone has no valid section */
506 zone->zone_start_pfn = 0;
507 zone->spanned_pages = 0;
508 zone_span_writeunlock(zone);
511 static void shrink_pgdat_span(struct pglist_data *pgdat,
512 unsigned long start_pfn, unsigned long end_pfn)
514 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
515 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
516 unsigned long pgdat_end_pfn = p;
518 struct mem_section *ms;
519 int nid = pgdat->node_id;
521 if (pgdat_start_pfn == start_pfn) {
523 * If the section is smallest section in the pgdat, it need
524 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
525 * In this case, we find second smallest valid mem_section
526 * for shrinking zone.
528 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
531 pgdat->node_start_pfn = pfn;
532 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
534 } else if (pgdat_end_pfn == end_pfn) {
536 * If the section is biggest section in the pgdat, it need
537 * shrink pgdat->node_spanned_pages.
538 * In this case, we find second biggest valid mem_section for
541 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
544 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
548 * If the section is not biggest or smallest mem_section in the pgdat,
549 * it only creates a hole in the pgdat. So in this case, we need not
551 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
552 * has only hole or not.
554 pfn = pgdat_start_pfn;
555 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
556 ms = __pfn_to_section(pfn);
558 if (unlikely(!valid_section(ms)))
561 if (pfn_to_nid(pfn) != nid)
564 /* If the section is current section, it continues the loop */
565 if (start_pfn == pfn)
568 /* If we find valid section, we have nothing to do */
572 /* The pgdat has no valid section */
573 pgdat->node_start_pfn = 0;
574 pgdat->node_spanned_pages = 0;
577 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
579 struct pglist_data *pgdat = zone->zone_pgdat;
580 int nr_pages = PAGES_PER_SECTION;
584 zone_type = zone - pgdat->node_zones;
586 pgdat_resize_lock(zone->zone_pgdat, &flags);
587 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
588 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
589 pgdat_resize_unlock(zone->zone_pgdat, &flags);
592 static int __remove_section(struct zone *zone, struct mem_section *ms,
593 unsigned long map_offset)
595 unsigned long start_pfn;
599 if (!valid_section(ms))
602 ret = unregister_memory_section(ms);
606 scn_nr = __section_nr(ms);
607 start_pfn = section_nr_to_pfn(scn_nr);
608 __remove_zone(zone, start_pfn);
610 sparse_remove_one_section(zone, ms, map_offset);
615 * __remove_pages() - remove sections of pages from a zone
616 * @zone: zone from which pages need to be removed
617 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
618 * @nr_pages: number of pages to remove (must be multiple of section size)
620 * Generic helper function to remove section mappings and sysfs entries
621 * for the section of the memory we are removing. Caller needs to make
622 * sure that pages are marked reserved and zones are adjust properly by
623 * calling offline_pages().
625 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
626 unsigned long nr_pages)
629 unsigned long map_offset = 0;
630 int sections_to_remove, ret = 0;
632 /* In the ZONE_DEVICE case device driver owns the memory region */
633 if (is_dev_zone(zone)) {
634 struct page *page = pfn_to_page(phys_start_pfn);
635 struct vmem_altmap *altmap;
637 altmap = to_vmem_altmap((unsigned long) page);
639 map_offset = vmem_altmap_offset(altmap);
641 resource_size_t start, size;
643 start = phys_start_pfn << PAGE_SHIFT;
644 size = nr_pages * PAGE_SIZE;
646 ret = release_mem_region_adjustable(&iomem_resource, start,
649 resource_size_t endres = start + size - 1;
651 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
652 &start, &endres, ret);
656 clear_zone_contiguous(zone);
659 * We can only remove entire sections
661 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
662 BUG_ON(nr_pages % PAGES_PER_SECTION);
664 sections_to_remove = nr_pages / PAGES_PER_SECTION;
665 for (i = 0; i < sections_to_remove; i++) {
666 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
668 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
674 set_zone_contiguous(zone);
678 #endif /* CONFIG_MEMORY_HOTREMOVE */
680 int set_online_page_callback(online_page_callback_t callback)
685 mutex_lock(&online_page_callback_lock);
687 if (online_page_callback == generic_online_page) {
688 online_page_callback = callback;
692 mutex_unlock(&online_page_callback_lock);
697 EXPORT_SYMBOL_GPL(set_online_page_callback);
699 int restore_online_page_callback(online_page_callback_t callback)
704 mutex_lock(&online_page_callback_lock);
706 if (online_page_callback == callback) {
707 online_page_callback = generic_online_page;
711 mutex_unlock(&online_page_callback_lock);
716 EXPORT_SYMBOL_GPL(restore_online_page_callback);
718 void __online_page_set_limits(struct page *page)
721 EXPORT_SYMBOL_GPL(__online_page_set_limits);
723 void __online_page_increment_counters(struct page *page)
725 adjust_managed_page_count(page, 1);
727 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
729 void __online_page_free(struct page *page)
731 __free_reserved_page(page);
733 EXPORT_SYMBOL_GPL(__online_page_free);
735 static void generic_online_page(struct page *page)
737 __online_page_set_limits(page);
738 __online_page_increment_counters(page);
739 __online_page_free(page);
742 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
746 unsigned long onlined_pages = *(unsigned long *)arg;
749 if (PageReserved(pfn_to_page(start_pfn)))
750 for (i = 0; i < nr_pages; i++) {
751 page = pfn_to_page(start_pfn + i);
752 (*online_page_callback)(page);
756 online_mem_sections(start_pfn, start_pfn + nr_pages);
758 *(unsigned long *)arg = onlined_pages;
762 #ifdef CONFIG_MOVABLE_NODE
764 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
767 static bool can_online_high_movable(int nid)
771 #else /* CONFIG_MOVABLE_NODE */
772 /* ensure every online node has NORMAL memory */
773 static bool can_online_high_movable(int nid)
775 return node_state(nid, N_NORMAL_MEMORY);
777 #endif /* CONFIG_MOVABLE_NODE */
779 /* check which state of node_states will be changed when online memory */
780 static void node_states_check_changes_online(unsigned long nr_pages,
781 struct zone *zone, struct memory_notify *arg)
783 int nid = zone_to_nid(zone);
784 enum zone_type zone_last = ZONE_NORMAL;
787 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
788 * contains nodes which have zones of 0...ZONE_NORMAL,
789 * set zone_last to ZONE_NORMAL.
791 * If we don't have HIGHMEM nor movable node,
792 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
793 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
795 if (N_MEMORY == N_NORMAL_MEMORY)
796 zone_last = ZONE_MOVABLE;
799 * if the memory to be online is in a zone of 0...zone_last, and
800 * the zones of 0...zone_last don't have memory before online, we will
801 * need to set the node to node_states[N_NORMAL_MEMORY] after
802 * the memory is online.
804 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
805 arg->status_change_nid_normal = nid;
807 arg->status_change_nid_normal = -1;
809 #ifdef CONFIG_HIGHMEM
811 * If we have movable node, node_states[N_HIGH_MEMORY]
812 * contains nodes which have zones of 0...ZONE_HIGHMEM,
813 * set zone_last to ZONE_HIGHMEM.
815 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
816 * contains nodes which have zones of 0...ZONE_MOVABLE,
817 * set zone_last to ZONE_MOVABLE.
819 zone_last = ZONE_HIGHMEM;
820 if (N_MEMORY == N_HIGH_MEMORY)
821 zone_last = ZONE_MOVABLE;
823 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
824 arg->status_change_nid_high = nid;
826 arg->status_change_nid_high = -1;
828 arg->status_change_nid_high = arg->status_change_nid_normal;
832 * if the node don't have memory befor online, we will need to
833 * set the node to node_states[N_MEMORY] after the memory
836 if (!node_state(nid, N_MEMORY))
837 arg->status_change_nid = nid;
839 arg->status_change_nid = -1;
842 static void node_states_set_node(int node, struct memory_notify *arg)
844 if (arg->status_change_nid_normal >= 0)
845 node_set_state(node, N_NORMAL_MEMORY);
847 if (arg->status_change_nid_high >= 0)
848 node_set_state(node, N_HIGH_MEMORY);
850 node_set_state(node, N_MEMORY);
853 bool allow_online_pfn_range(int nid, unsigned long pfn, unsigned long nr_pages, int online_type)
855 struct pglist_data *pgdat = NODE_DATA(nid);
856 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
857 struct zone *default_zone = default_zone_for_pfn(nid, pfn, nr_pages);
860 * TODO there shouldn't be any inherent reason to have ZONE_NORMAL
861 * physically before ZONE_MOVABLE. All we need is they do not
862 * overlap. Historically we didn't allow ZONE_NORMAL after ZONE_MOVABLE
863 * though so let's stick with it for simplicity for now.
864 * TODO make sure we do not overlap with ZONE_DEVICE
866 if (online_type == MMOP_ONLINE_KERNEL) {
867 if (zone_is_empty(movable_zone))
869 return movable_zone->zone_start_pfn >= pfn + nr_pages;
870 } else if (online_type == MMOP_ONLINE_MOVABLE) {
871 return zone_end_pfn(default_zone) <= pfn;
874 /* MMOP_ONLINE_KEEP will always succeed and inherits the current zone */
875 return online_type == MMOP_ONLINE_KEEP;
878 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
879 unsigned long nr_pages)
881 unsigned long old_end_pfn = zone_end_pfn(zone);
883 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
884 zone->zone_start_pfn = start_pfn;
886 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
889 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
890 unsigned long nr_pages)
892 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
894 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
895 pgdat->node_start_pfn = start_pfn;
897 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
900 void __ref move_pfn_range_to_zone(struct zone *zone,
901 unsigned long start_pfn, unsigned long nr_pages)
903 struct pglist_data *pgdat = zone->zone_pgdat;
904 int nid = pgdat->node_id;
907 if (zone_is_empty(zone))
908 init_currently_empty_zone(zone, start_pfn, nr_pages);
910 clear_zone_contiguous(zone);
912 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
913 pgdat_resize_lock(pgdat, &flags);
914 zone_span_writelock(zone);
915 resize_zone_range(zone, start_pfn, nr_pages);
916 zone_span_writeunlock(zone);
917 resize_pgdat_range(pgdat, start_pfn, nr_pages);
918 pgdat_resize_unlock(pgdat, &flags);
921 * TODO now we have a visible range of pages which are not associated
922 * with their zone properly. Not nice but set_pfnblock_flags_mask
923 * expects the zone spans the pfn range. All the pages in the range
924 * are reserved so nobody should be touching them so we should be safe
926 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, MEMMAP_HOTPLUG);
928 set_zone_contiguous(zone);
932 * Returns a default kernel memory zone for the given pfn range.
933 * If no kernel zone covers this pfn range it will automatically go
934 * to the ZONE_NORMAL.
936 struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
937 unsigned long nr_pages)
939 struct pglist_data *pgdat = NODE_DATA(nid);
942 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
943 struct zone *zone = &pgdat->node_zones[zid];
945 if (zone_intersects(zone, start_pfn, nr_pages))
949 return &pgdat->node_zones[ZONE_NORMAL];
953 * Associates the given pfn range with the given node and the zone appropriate
954 * for the given online type.
956 static struct zone * __meminit move_pfn_range(int online_type, int nid,
957 unsigned long start_pfn, unsigned long nr_pages)
959 struct pglist_data *pgdat = NODE_DATA(nid);
960 struct zone *zone = default_zone_for_pfn(nid, start_pfn, nr_pages);
962 if (online_type == MMOP_ONLINE_KEEP) {
963 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
965 * MMOP_ONLINE_KEEP defaults to MMOP_ONLINE_KERNEL but use
966 * movable zone if that is not possible (e.g. we are within
967 * or past the existing movable zone)
969 if (!allow_online_pfn_range(nid, start_pfn, nr_pages,
972 } else if (online_type == MMOP_ONLINE_MOVABLE) {
973 zone = &pgdat->node_zones[ZONE_MOVABLE];
976 move_pfn_range_to_zone(zone, start_pfn, nr_pages);
980 /* Must be protected by mem_hotplug_begin() */
981 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
984 unsigned long onlined_pages = 0;
986 int need_zonelists_rebuild = 0;
989 struct memory_notify arg;
991 nid = pfn_to_nid(pfn);
992 if (!allow_online_pfn_range(nid, pfn, nr_pages, online_type))
995 if (online_type == MMOP_ONLINE_MOVABLE && !can_online_high_movable(nid))
998 /* associate pfn range with the zone */
999 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
1001 arg.start_pfn = pfn;
1002 arg.nr_pages = nr_pages;
1003 node_states_check_changes_online(nr_pages, zone, &arg);
1005 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1006 ret = notifier_to_errno(ret);
1008 goto failed_addition;
1011 * If this zone is not populated, then it is not in zonelist.
1012 * This means the page allocator ignores this zone.
1013 * So, zonelist must be updated after online.
1015 mutex_lock(&zonelists_mutex);
1016 if (!populated_zone(zone)) {
1017 need_zonelists_rebuild = 1;
1018 build_all_zonelists(NULL, zone);
1021 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1022 online_pages_range);
1024 if (need_zonelists_rebuild)
1025 zone_pcp_reset(zone);
1026 mutex_unlock(&zonelists_mutex);
1027 goto failed_addition;
1030 zone->present_pages += onlined_pages;
1032 pgdat_resize_lock(zone->zone_pgdat, &flags);
1033 zone->zone_pgdat->node_present_pages += onlined_pages;
1034 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1036 if (onlined_pages) {
1037 node_states_set_node(nid, &arg);
1038 if (need_zonelists_rebuild)
1039 build_all_zonelists(NULL, NULL);
1041 zone_pcp_update(zone);
1044 mutex_unlock(&zonelists_mutex);
1046 init_per_zone_wmark_min();
1048 if (onlined_pages) {
1053 vm_total_pages = nr_free_pagecache_pages();
1055 writeback_set_ratelimit();
1058 memory_notify(MEM_ONLINE, &arg);
1062 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1063 (unsigned long long) pfn << PAGE_SHIFT,
1064 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1065 memory_notify(MEM_CANCEL_ONLINE, &arg);
1068 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1070 static void reset_node_present_pages(pg_data_t *pgdat)
1074 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1075 z->present_pages = 0;
1077 pgdat->node_present_pages = 0;
1080 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1081 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1083 struct pglist_data *pgdat;
1084 unsigned long zones_size[MAX_NR_ZONES] = {0};
1085 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1086 unsigned long start_pfn = PFN_DOWN(start);
1088 pgdat = NODE_DATA(nid);
1090 pgdat = arch_alloc_nodedata(nid);
1094 arch_refresh_nodedata(nid, pgdat);
1097 * Reset the nr_zones, order and classzone_idx before reuse.
1098 * Note that kswapd will init kswapd_classzone_idx properly
1099 * when it starts in the near future.
1101 pgdat->nr_zones = 0;
1102 pgdat->kswapd_order = 0;
1103 pgdat->kswapd_classzone_idx = 0;
1106 /* we can use NODE_DATA(nid) from here */
1108 /* init node's zones as empty zones, we don't have any present pages.*/
1109 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1110 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
1113 * The node we allocated has no zone fallback lists. For avoiding
1114 * to access not-initialized zonelist, build here.
1116 mutex_lock(&zonelists_mutex);
1117 build_all_zonelists(pgdat, NULL);
1118 mutex_unlock(&zonelists_mutex);
1121 * zone->managed_pages is set to an approximate value in
1122 * free_area_init_core(), which will cause
1123 * /sys/device/system/node/nodeX/meminfo has wrong data.
1124 * So reset it to 0 before any memory is onlined.
1126 reset_node_managed_pages(pgdat);
1129 * When memory is hot-added, all the memory is in offline state. So
1130 * clear all zones' present_pages because they will be updated in
1131 * online_pages() and offline_pages().
1133 reset_node_present_pages(pgdat);
1138 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1140 arch_refresh_nodedata(nid, NULL);
1141 free_percpu(pgdat->per_cpu_nodestats);
1142 arch_free_nodedata(pgdat);
1148 * try_online_node - online a node if offlined
1150 * called by cpu_up() to online a node without onlined memory.
1152 int try_online_node(int nid)
1157 if (node_online(nid))
1160 mem_hotplug_begin();
1161 pgdat = hotadd_new_pgdat(nid, 0);
1163 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1167 node_set_online(nid);
1168 ret = register_one_node(nid);
1171 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1172 mutex_lock(&zonelists_mutex);
1173 build_all_zonelists(NULL, NULL);
1174 mutex_unlock(&zonelists_mutex);
1182 static int check_hotplug_memory_range(u64 start, u64 size)
1184 u64 start_pfn = PFN_DOWN(start);
1185 u64 nr_pages = size >> PAGE_SHIFT;
1187 /* Memory range must be aligned with section */
1188 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1189 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1190 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1191 (unsigned long long)start,
1192 (unsigned long long)size);
1199 static int online_memory_block(struct memory_block *mem, void *arg)
1201 return device_online(&mem->dev);
1204 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1205 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1208 pg_data_t *pgdat = NULL;
1214 size = resource_size(res);
1216 ret = check_hotplug_memory_range(start, size);
1220 { /* Stupid hack to suppress address-never-null warning */
1221 void *p = NODE_DATA(nid);
1225 mem_hotplug_begin();
1228 * Add new range to memblock so that when hotadd_new_pgdat() is called
1229 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1230 * this new range and calculate total pages correctly. The range will
1231 * be removed at hot-remove time.
1233 memblock_add_node(start, size, nid);
1235 new_node = !node_online(nid);
1237 pgdat = hotadd_new_pgdat(nid, start);
1243 /* call arch's memory hotadd */
1244 ret = arch_add_memory(nid, start, size, true);
1249 /* we online node here. we can't roll back from here. */
1250 node_set_online(nid);
1253 unsigned long start_pfn = start >> PAGE_SHIFT;
1254 unsigned long nr_pages = size >> PAGE_SHIFT;
1256 ret = __register_one_node(nid);
1261 * link memory sections under this node. This is already
1262 * done when creatig memory section in register_new_memory
1263 * but that depends to have the node registered so offline
1264 * nodes have to go through register_node.
1265 * TODO clean up this mess.
1267 ret = link_mem_sections(nid, start_pfn, nr_pages);
1270 * If sysfs file of new node can't create, cpu on the node
1271 * can't be hot-added. There is no rollback way now.
1272 * So, check by BUG_ON() to catch it reluctantly..
1277 /* create new memmap entry */
1278 firmware_map_add_hotplug(start, start + size, "System RAM");
1280 /* online pages if requested */
1282 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1283 NULL, online_memory_block);
1288 /* rollback pgdat allocation and others */
1290 rollback_node_hotadd(nid, pgdat);
1291 memblock_remove(start, size);
1297 EXPORT_SYMBOL_GPL(add_memory_resource);
1299 int __ref add_memory(int nid, u64 start, u64 size)
1301 struct resource *res;
1304 res = register_memory_resource(start, size);
1306 return PTR_ERR(res);
1308 ret = add_memory_resource(nid, res, memhp_auto_online);
1310 release_memory_resource(res);
1313 EXPORT_SYMBOL_GPL(add_memory);
1315 #ifdef CONFIG_MEMORY_HOTREMOVE
1317 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1318 * set and the size of the free page is given by page_order(). Using this,
1319 * the function determines if the pageblock contains only free pages.
1320 * Due to buddy contraints, a free page at least the size of a pageblock will
1321 * be located at the start of the pageblock
1323 static inline int pageblock_free(struct page *page)
1325 return PageBuddy(page) && page_order(page) >= pageblock_order;
1328 /* Return the start of the next active pageblock after a given page */
1329 static struct page *next_active_pageblock(struct page *page)
1331 /* Ensure the starting page is pageblock-aligned */
1332 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1334 /* If the entire pageblock is free, move to the end of free page */
1335 if (pageblock_free(page)) {
1337 /* be careful. we don't have locks, page_order can be changed.*/
1338 order = page_order(page);
1339 if ((order < MAX_ORDER) && (order >= pageblock_order))
1340 return page + (1 << order);
1343 return page + pageblock_nr_pages;
1346 /* Checks if this range of memory is likely to be hot-removable. */
1347 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1349 struct page *page = pfn_to_page(start_pfn);
1350 struct page *end_page = page + nr_pages;
1352 /* Check the starting page of each pageblock within the range */
1353 for (; page < end_page; page = next_active_pageblock(page)) {
1354 if (!is_pageblock_removable_nolock(page))
1359 /* All pageblocks in the memory block are likely to be hot-removable */
1364 * Confirm all pages in a range [start, end) belong to the same zone.
1365 * When true, return its valid [start, end).
1367 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1368 unsigned long *valid_start, unsigned long *valid_end)
1370 unsigned long pfn, sec_end_pfn;
1371 unsigned long start, end;
1372 struct zone *zone = NULL;
1375 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1377 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1378 /* Make sure the memory section is present first */
1379 if (!present_section_nr(pfn_to_section_nr(pfn)))
1381 for (; pfn < sec_end_pfn && pfn < end_pfn;
1382 pfn += MAX_ORDER_NR_PAGES) {
1384 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1385 while ((i < MAX_ORDER_NR_PAGES) &&
1386 !pfn_valid_within(pfn + i))
1388 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1390 page = pfn_to_page(pfn + i);
1391 if (zone && page_zone(page) != zone)
1395 zone = page_zone(page);
1396 end = pfn + MAX_ORDER_NR_PAGES;
1401 *valid_start = start;
1402 *valid_end = min(end, end_pfn);
1410 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1411 * non-lru movable pages and hugepages). We scan pfn because it's much
1412 * easier than scanning over linked list. This function returns the pfn
1413 * of the first found movable page if it's found, otherwise 0.
1415 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1419 for (pfn = start; pfn < end; pfn++) {
1420 if (pfn_valid(pfn)) {
1421 page = pfn_to_page(pfn);
1424 if (__PageMovable(page))
1426 if (PageHuge(page)) {
1427 if (page_huge_active(page))
1430 pfn = round_up(pfn + 1,
1431 1 << compound_order(page)) - 1;
1438 static struct page *new_node_page(struct page *page, unsigned long private,
1441 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1442 int nid = page_to_nid(page);
1443 nodemask_t nmask = node_states[N_MEMORY];
1444 struct page *new_page = NULL;
1447 * TODO: allocate a destination hugepage from a nearest neighbor node,
1448 * accordance with memory policy of the user process if possible. For
1449 * now as a simple work-around, we use the next node for destination.
1452 return alloc_huge_page_node(page_hstate(compound_head(page)),
1453 next_node_in(nid, nmask));
1455 node_clear(nid, nmask);
1457 if (PageHighMem(page)
1458 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1459 gfp_mask |= __GFP_HIGHMEM;
1461 if (!nodes_empty(nmask))
1462 new_page = __alloc_pages_nodemask(gfp_mask, 0,
1463 node_zonelist(nid, gfp_mask), &nmask);
1465 new_page = __alloc_pages(gfp_mask, 0,
1466 node_zonelist(nid, gfp_mask));
1471 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1473 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1477 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1478 int not_managed = 0;
1482 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1483 if (!pfn_valid(pfn))
1485 page = pfn_to_page(pfn);
1487 if (PageHuge(page)) {
1488 struct page *head = compound_head(page);
1489 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1490 if (compound_order(head) > PFN_SECTION_SHIFT) {
1494 if (isolate_huge_page(page, &source))
1495 move_pages -= 1 << compound_order(head);
1499 if (!get_page_unless_zero(page))
1502 * We can skip free pages. And we can deal with pages on
1503 * LRU and non-lru movable pages.
1506 ret = isolate_lru_page(page);
1508 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1509 if (!ret) { /* Success */
1511 list_add_tail(&page->lru, &source);
1513 if (!__PageMovable(page))
1514 inc_node_page_state(page, NR_ISOLATED_ANON +
1515 page_is_file_cache(page));
1518 #ifdef CONFIG_DEBUG_VM
1519 pr_alert("failed to isolate pfn %lx\n", pfn);
1520 dump_page(page, "isolation failed");
1523 /* Because we don't have big zone->lock. we should
1524 check this again here. */
1525 if (page_count(page)) {
1532 if (!list_empty(&source)) {
1534 putback_movable_pages(&source);
1538 /* Allocate a new page from the nearest neighbor node */
1539 ret = migrate_pages(&source, new_node_page, NULL, 0,
1540 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1542 putback_movable_pages(&source);
1549 * remove from free_area[] and mark all as Reserved.
1552 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1555 __offline_isolated_pages(start, start + nr_pages);
1560 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1562 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1563 offline_isolated_pages_cb);
1567 * Check all pages in range, recoreded as memory resource, are isolated.
1570 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1574 long offlined = *(long *)data;
1575 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1576 offlined = nr_pages;
1578 *(long *)data += offlined;
1583 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1588 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1589 check_pages_isolated_cb);
1591 offlined = (long)ret;
1595 #ifdef CONFIG_MOVABLE_NODE
1597 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1600 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1604 #else /* CONFIG_MOVABLE_NODE */
1605 /* ensure the node has NORMAL memory if it is still online */
1606 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1608 struct pglist_data *pgdat = zone->zone_pgdat;
1609 unsigned long present_pages = 0;
1612 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1613 present_pages += pgdat->node_zones[zt].present_pages;
1615 if (present_pages > nr_pages)
1619 for (; zt <= ZONE_MOVABLE; zt++)
1620 present_pages += pgdat->node_zones[zt].present_pages;
1623 * we can't offline the last normal memory until all
1624 * higher memory is offlined.
1626 return present_pages == 0;
1628 #endif /* CONFIG_MOVABLE_NODE */
1630 static int __init cmdline_parse_movable_node(char *p)
1632 #ifdef CONFIG_MOVABLE_NODE
1633 movable_node_enabled = true;
1635 pr_warn("movable_node option not supported\n");
1639 early_param("movable_node", cmdline_parse_movable_node);
1641 /* check which state of node_states will be changed when offline memory */
1642 static void node_states_check_changes_offline(unsigned long nr_pages,
1643 struct zone *zone, struct memory_notify *arg)
1645 struct pglist_data *pgdat = zone->zone_pgdat;
1646 unsigned long present_pages = 0;
1647 enum zone_type zt, zone_last = ZONE_NORMAL;
1650 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1651 * contains nodes which have zones of 0...ZONE_NORMAL,
1652 * set zone_last to ZONE_NORMAL.
1654 * If we don't have HIGHMEM nor movable node,
1655 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1656 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1658 if (N_MEMORY == N_NORMAL_MEMORY)
1659 zone_last = ZONE_MOVABLE;
1662 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1663 * If the memory to be offline is in a zone of 0...zone_last,
1664 * and it is the last present memory, 0...zone_last will
1665 * become empty after offline , thus we can determind we will
1666 * need to clear the node from node_states[N_NORMAL_MEMORY].
1668 for (zt = 0; zt <= zone_last; zt++)
1669 present_pages += pgdat->node_zones[zt].present_pages;
1670 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1671 arg->status_change_nid_normal = zone_to_nid(zone);
1673 arg->status_change_nid_normal = -1;
1675 #ifdef CONFIG_HIGHMEM
1677 * If we have movable node, node_states[N_HIGH_MEMORY]
1678 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1679 * set zone_last to ZONE_HIGHMEM.
1681 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1682 * contains nodes which have zones of 0...ZONE_MOVABLE,
1683 * set zone_last to ZONE_MOVABLE.
1685 zone_last = ZONE_HIGHMEM;
1686 if (N_MEMORY == N_HIGH_MEMORY)
1687 zone_last = ZONE_MOVABLE;
1689 for (; zt <= zone_last; zt++)
1690 present_pages += pgdat->node_zones[zt].present_pages;
1691 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1692 arg->status_change_nid_high = zone_to_nid(zone);
1694 arg->status_change_nid_high = -1;
1696 arg->status_change_nid_high = arg->status_change_nid_normal;
1700 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1702 zone_last = ZONE_MOVABLE;
1705 * check whether node_states[N_HIGH_MEMORY] will be changed
1706 * If we try to offline the last present @nr_pages from the node,
1707 * we can determind we will need to clear the node from
1708 * node_states[N_HIGH_MEMORY].
1710 for (; zt <= zone_last; zt++)
1711 present_pages += pgdat->node_zones[zt].present_pages;
1712 if (nr_pages >= present_pages)
1713 arg->status_change_nid = zone_to_nid(zone);
1715 arg->status_change_nid = -1;
1718 static void node_states_clear_node(int node, struct memory_notify *arg)
1720 if (arg->status_change_nid_normal >= 0)
1721 node_clear_state(node, N_NORMAL_MEMORY);
1723 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1724 (arg->status_change_nid_high >= 0))
1725 node_clear_state(node, N_HIGH_MEMORY);
1727 if ((N_MEMORY != N_HIGH_MEMORY) &&
1728 (arg->status_change_nid >= 0))
1729 node_clear_state(node, N_MEMORY);
1732 static int __ref __offline_pages(unsigned long start_pfn,
1733 unsigned long end_pfn, unsigned long timeout)
1735 unsigned long pfn, nr_pages, expire;
1736 long offlined_pages;
1737 int ret, drain, retry_max, node;
1738 unsigned long flags;
1739 unsigned long valid_start, valid_end;
1741 struct memory_notify arg;
1743 /* at least, alignment against pageblock is necessary */
1744 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1746 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1748 /* This makes hotplug much easier...and readable.
1749 we assume this for now. .*/
1750 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1753 zone = page_zone(pfn_to_page(valid_start));
1754 node = zone_to_nid(zone);
1755 nr_pages = end_pfn - start_pfn;
1757 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1760 /* set above range as isolated */
1761 ret = start_isolate_page_range(start_pfn, end_pfn,
1762 MIGRATE_MOVABLE, true);
1766 arg.start_pfn = start_pfn;
1767 arg.nr_pages = nr_pages;
1768 node_states_check_changes_offline(nr_pages, zone, &arg);
1770 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1771 ret = notifier_to_errno(ret);
1773 goto failed_removal;
1776 expire = jiffies + timeout;
1780 /* start memory hot removal */
1782 if (time_after(jiffies, expire))
1783 goto failed_removal;
1785 if (signal_pending(current))
1786 goto failed_removal;
1789 lru_add_drain_all();
1791 drain_all_pages(zone);
1794 pfn = scan_movable_pages(start_pfn, end_pfn);
1795 if (pfn) { /* We have movable pages */
1796 ret = do_migrate_range(pfn, end_pfn);
1802 if (--retry_max == 0)
1803 goto failed_removal;
1809 /* drain all zone's lru pagevec, this is asynchronous... */
1810 lru_add_drain_all();
1812 /* drain pcp pages, this is synchronous. */
1813 drain_all_pages(zone);
1815 * dissolve free hugepages in the memory block before doing offlining
1816 * actually in order to make hugetlbfs's object counting consistent.
1818 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1820 goto failed_removal;
1822 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1823 if (offlined_pages < 0) {
1825 goto failed_removal;
1827 pr_info("Offlined Pages %ld\n", offlined_pages);
1828 /* Ok, all of our target is isolated.
1829 We cannot do rollback at this point. */
1830 offline_isolated_pages(start_pfn, end_pfn);
1831 /* reset pagetype flags and makes migrate type to be MOVABLE */
1832 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1833 /* removal success */
1834 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1835 zone->present_pages -= offlined_pages;
1837 pgdat_resize_lock(zone->zone_pgdat, &flags);
1838 zone->zone_pgdat->node_present_pages -= offlined_pages;
1839 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1841 init_per_zone_wmark_min();
1843 if (!populated_zone(zone)) {
1844 zone_pcp_reset(zone);
1845 mutex_lock(&zonelists_mutex);
1846 build_all_zonelists(NULL, NULL);
1847 mutex_unlock(&zonelists_mutex);
1849 zone_pcp_update(zone);
1851 node_states_clear_node(node, &arg);
1852 if (arg.status_change_nid >= 0) {
1854 kcompactd_stop(node);
1857 vm_total_pages = nr_free_pagecache_pages();
1858 writeback_set_ratelimit();
1860 memory_notify(MEM_OFFLINE, &arg);
1864 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1865 (unsigned long long) start_pfn << PAGE_SHIFT,
1866 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1867 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1868 /* pushback to free area */
1869 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1873 /* Must be protected by mem_hotplug_begin() */
1874 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1876 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1878 #endif /* CONFIG_MEMORY_HOTREMOVE */
1881 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1882 * @start_pfn: start pfn of the memory range
1883 * @end_pfn: end pfn of the memory range
1884 * @arg: argument passed to func
1885 * @func: callback for each memory section walked
1887 * This function walks through all present mem sections in range
1888 * [start_pfn, end_pfn) and call func on each mem section.
1890 * Returns the return value of func.
1892 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1893 void *arg, int (*func)(struct memory_block *, void *))
1895 struct memory_block *mem = NULL;
1896 struct mem_section *section;
1897 unsigned long pfn, section_nr;
1900 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1901 section_nr = pfn_to_section_nr(pfn);
1902 if (!present_section_nr(section_nr))
1905 section = __nr_to_section(section_nr);
1906 /* same memblock? */
1908 if ((section_nr >= mem->start_section_nr) &&
1909 (section_nr <= mem->end_section_nr))
1912 mem = find_memory_block_hinted(section, mem);
1916 ret = func(mem, arg);
1918 kobject_put(&mem->dev.kobj);
1924 kobject_put(&mem->dev.kobj);
1929 #ifdef CONFIG_MEMORY_HOTREMOVE
1930 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1932 int ret = !is_memblock_offlined(mem);
1934 if (unlikely(ret)) {
1935 phys_addr_t beginpa, endpa;
1937 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1938 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1939 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1946 static int check_cpu_on_node(pg_data_t *pgdat)
1950 for_each_present_cpu(cpu) {
1951 if (cpu_to_node(cpu) == pgdat->node_id)
1953 * the cpu on this node isn't removed, and we can't
1954 * offline this node.
1962 static void unmap_cpu_on_node(pg_data_t *pgdat)
1964 #ifdef CONFIG_ACPI_NUMA
1967 for_each_possible_cpu(cpu)
1968 if (cpu_to_node(cpu) == pgdat->node_id)
1969 numa_clear_node(cpu);
1973 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1977 ret = check_cpu_on_node(pgdat);
1982 * the node will be offlined when we come here, so we can clear
1983 * the cpu_to_node() now.
1986 unmap_cpu_on_node(pgdat);
1993 * Offline a node if all memory sections and cpus of the node are removed.
1995 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1996 * and online/offline operations before this call.
1998 void try_offline_node(int nid)
2000 pg_data_t *pgdat = NODE_DATA(nid);
2001 unsigned long start_pfn = pgdat->node_start_pfn;
2002 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
2005 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2006 unsigned long section_nr = pfn_to_section_nr(pfn);
2008 if (!present_section_nr(section_nr))
2011 if (pfn_to_nid(pfn) != nid)
2015 * some memory sections of this node are not removed, and we
2016 * can't offline node now.
2021 if (check_and_unmap_cpu_on_node(pgdat))
2025 * all memory/cpu of this node are removed, we can offline this
2028 node_set_offline(nid);
2029 unregister_one_node(nid);
2031 EXPORT_SYMBOL(try_offline_node);
2036 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2037 * and online/offline operations before this call, as required by
2038 * try_offline_node().
2040 void __ref remove_memory(int nid, u64 start, u64 size)
2044 BUG_ON(check_hotplug_memory_range(start, size));
2046 mem_hotplug_begin();
2049 * All memory blocks must be offlined before removing memory. Check
2050 * whether all memory blocks in question are offline and trigger a BUG()
2051 * if this is not the case.
2053 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2054 check_memblock_offlined_cb);
2058 /* remove memmap entry */
2059 firmware_map_remove(start, start + size, "System RAM");
2060 memblock_free(start, size);
2061 memblock_remove(start, size);
2063 arch_remove_memory(start, size);
2065 try_offline_node(nid);
2069 EXPORT_SYMBOL_GPL(remove_memory);
2070 #endif /* CONFIG_MEMORY_HOTREMOVE */