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 /* check which state of node_states will be changed when online memory */
763 static void node_states_check_changes_online(unsigned long nr_pages,
764 struct zone *zone, struct memory_notify *arg)
766 int nid = zone_to_nid(zone);
767 enum zone_type zone_last = ZONE_NORMAL;
770 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
771 * contains nodes which have zones of 0...ZONE_NORMAL,
772 * set zone_last to ZONE_NORMAL.
774 * If we don't have HIGHMEM nor movable node,
775 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
776 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
778 if (N_MEMORY == N_NORMAL_MEMORY)
779 zone_last = ZONE_MOVABLE;
782 * if the memory to be online is in a zone of 0...zone_last, and
783 * the zones of 0...zone_last don't have memory before online, we will
784 * need to set the node to node_states[N_NORMAL_MEMORY] after
785 * the memory is online.
787 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
788 arg->status_change_nid_normal = nid;
790 arg->status_change_nid_normal = -1;
792 #ifdef CONFIG_HIGHMEM
794 * If we have movable node, node_states[N_HIGH_MEMORY]
795 * contains nodes which have zones of 0...ZONE_HIGHMEM,
796 * set zone_last to ZONE_HIGHMEM.
798 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
799 * contains nodes which have zones of 0...ZONE_MOVABLE,
800 * set zone_last to ZONE_MOVABLE.
802 zone_last = ZONE_HIGHMEM;
803 if (N_MEMORY == N_HIGH_MEMORY)
804 zone_last = ZONE_MOVABLE;
806 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
807 arg->status_change_nid_high = nid;
809 arg->status_change_nid_high = -1;
811 arg->status_change_nid_high = arg->status_change_nid_normal;
815 * if the node don't have memory befor online, we will need to
816 * set the node to node_states[N_MEMORY] after the memory
819 if (!node_state(nid, N_MEMORY))
820 arg->status_change_nid = nid;
822 arg->status_change_nid = -1;
825 static void node_states_set_node(int node, struct memory_notify *arg)
827 if (arg->status_change_nid_normal >= 0)
828 node_set_state(node, N_NORMAL_MEMORY);
830 if (arg->status_change_nid_high >= 0)
831 node_set_state(node, N_HIGH_MEMORY);
833 node_set_state(node, N_MEMORY);
836 bool allow_online_pfn_range(int nid, unsigned long pfn, unsigned long nr_pages, int online_type)
838 struct pglist_data *pgdat = NODE_DATA(nid);
839 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
840 struct zone *default_zone = default_zone_for_pfn(nid, pfn, nr_pages);
843 * TODO there shouldn't be any inherent reason to have ZONE_NORMAL
844 * physically before ZONE_MOVABLE. All we need is they do not
845 * overlap. Historically we didn't allow ZONE_NORMAL after ZONE_MOVABLE
846 * though so let's stick with it for simplicity for now.
847 * TODO make sure we do not overlap with ZONE_DEVICE
849 if (online_type == MMOP_ONLINE_KERNEL) {
850 if (zone_is_empty(movable_zone))
852 return movable_zone->zone_start_pfn >= pfn + nr_pages;
853 } else if (online_type == MMOP_ONLINE_MOVABLE) {
854 return zone_end_pfn(default_zone) <= pfn;
857 /* MMOP_ONLINE_KEEP will always succeed and inherits the current zone */
858 return online_type == MMOP_ONLINE_KEEP;
861 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
862 unsigned long nr_pages)
864 unsigned long old_end_pfn = zone_end_pfn(zone);
866 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
867 zone->zone_start_pfn = start_pfn;
869 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
872 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
873 unsigned long nr_pages)
875 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
877 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
878 pgdat->node_start_pfn = start_pfn;
880 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
883 void __ref move_pfn_range_to_zone(struct zone *zone,
884 unsigned long start_pfn, unsigned long nr_pages)
886 struct pglist_data *pgdat = zone->zone_pgdat;
887 int nid = pgdat->node_id;
890 if (zone_is_empty(zone))
891 init_currently_empty_zone(zone, start_pfn, nr_pages);
893 clear_zone_contiguous(zone);
895 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
896 pgdat_resize_lock(pgdat, &flags);
897 zone_span_writelock(zone);
898 resize_zone_range(zone, start_pfn, nr_pages);
899 zone_span_writeunlock(zone);
900 resize_pgdat_range(pgdat, start_pfn, nr_pages);
901 pgdat_resize_unlock(pgdat, &flags);
904 * TODO now we have a visible range of pages which are not associated
905 * with their zone properly. Not nice but set_pfnblock_flags_mask
906 * expects the zone spans the pfn range. All the pages in the range
907 * are reserved so nobody should be touching them so we should be safe
909 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, MEMMAP_HOTPLUG);
911 set_zone_contiguous(zone);
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 struct zone *default_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 * Associates the given pfn range with the given node and the zone appropriate
937 * for the given online type.
939 static struct zone * __meminit move_pfn_range(int online_type, int nid,
940 unsigned long start_pfn, unsigned long nr_pages)
942 struct pglist_data *pgdat = NODE_DATA(nid);
943 struct zone *zone = default_zone_for_pfn(nid, start_pfn, nr_pages);
945 if (online_type == MMOP_ONLINE_KEEP) {
946 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
948 * MMOP_ONLINE_KEEP defaults to MMOP_ONLINE_KERNEL but use
949 * movable zone if that is not possible (e.g. we are within
950 * or past the existing movable zone)
952 if (!allow_online_pfn_range(nid, start_pfn, nr_pages,
955 } else if (online_type == MMOP_ONLINE_MOVABLE) {
956 zone = &pgdat->node_zones[ZONE_MOVABLE];
959 move_pfn_range_to_zone(zone, start_pfn, nr_pages);
963 /* Must be protected by mem_hotplug_begin() */
964 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
967 unsigned long onlined_pages = 0;
969 int need_zonelists_rebuild = 0;
972 struct memory_notify arg;
974 nid = pfn_to_nid(pfn);
975 if (!allow_online_pfn_range(nid, pfn, nr_pages, online_type))
978 /* associate pfn range with the zone */
979 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
982 arg.nr_pages = nr_pages;
983 node_states_check_changes_online(nr_pages, zone, &arg);
985 ret = memory_notify(MEM_GOING_ONLINE, &arg);
986 ret = notifier_to_errno(ret);
988 goto failed_addition;
991 * If this zone is not populated, then it is not in zonelist.
992 * This means the page allocator ignores this zone.
993 * So, zonelist must be updated after online.
995 mutex_lock(&zonelists_mutex);
996 if (!populated_zone(zone)) {
997 need_zonelists_rebuild = 1;
998 build_all_zonelists(NULL, zone);
1001 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1002 online_pages_range);
1004 if (need_zonelists_rebuild)
1005 zone_pcp_reset(zone);
1006 mutex_unlock(&zonelists_mutex);
1007 goto failed_addition;
1010 zone->present_pages += onlined_pages;
1012 pgdat_resize_lock(zone->zone_pgdat, &flags);
1013 zone->zone_pgdat->node_present_pages += onlined_pages;
1014 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1016 if (onlined_pages) {
1017 node_states_set_node(nid, &arg);
1018 if (need_zonelists_rebuild)
1019 build_all_zonelists(NULL, NULL);
1021 zone_pcp_update(zone);
1024 mutex_unlock(&zonelists_mutex);
1026 init_per_zone_wmark_min();
1028 if (onlined_pages) {
1033 vm_total_pages = nr_free_pagecache_pages();
1035 writeback_set_ratelimit();
1038 memory_notify(MEM_ONLINE, &arg);
1042 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1043 (unsigned long long) pfn << PAGE_SHIFT,
1044 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1045 memory_notify(MEM_CANCEL_ONLINE, &arg);
1048 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1050 static void reset_node_present_pages(pg_data_t *pgdat)
1054 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1055 z->present_pages = 0;
1057 pgdat->node_present_pages = 0;
1060 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1061 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1063 struct pglist_data *pgdat;
1064 unsigned long zones_size[MAX_NR_ZONES] = {0};
1065 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1066 unsigned long start_pfn = PFN_DOWN(start);
1068 pgdat = NODE_DATA(nid);
1070 pgdat = arch_alloc_nodedata(nid);
1074 arch_refresh_nodedata(nid, pgdat);
1077 * Reset the nr_zones, order and classzone_idx before reuse.
1078 * Note that kswapd will init kswapd_classzone_idx properly
1079 * when it starts in the near future.
1081 pgdat->nr_zones = 0;
1082 pgdat->kswapd_order = 0;
1083 pgdat->kswapd_classzone_idx = 0;
1086 /* we can use NODE_DATA(nid) from here */
1088 /* init node's zones as empty zones, we don't have any present pages.*/
1089 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1090 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
1093 * The node we allocated has no zone fallback lists. For avoiding
1094 * to access not-initialized zonelist, build here.
1096 mutex_lock(&zonelists_mutex);
1097 build_all_zonelists(pgdat, NULL);
1098 mutex_unlock(&zonelists_mutex);
1101 * zone->managed_pages is set to an approximate value in
1102 * free_area_init_core(), which will cause
1103 * /sys/device/system/node/nodeX/meminfo has wrong data.
1104 * So reset it to 0 before any memory is onlined.
1106 reset_node_managed_pages(pgdat);
1109 * When memory is hot-added, all the memory is in offline state. So
1110 * clear all zones' present_pages because they will be updated in
1111 * online_pages() and offline_pages().
1113 reset_node_present_pages(pgdat);
1118 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1120 arch_refresh_nodedata(nid, NULL);
1121 free_percpu(pgdat->per_cpu_nodestats);
1122 arch_free_nodedata(pgdat);
1128 * try_online_node - online a node if offlined
1130 * called by cpu_up() to online a node without onlined memory.
1132 int try_online_node(int nid)
1137 if (node_online(nid))
1140 mem_hotplug_begin();
1141 pgdat = hotadd_new_pgdat(nid, 0);
1143 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1147 node_set_online(nid);
1148 ret = register_one_node(nid);
1151 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1152 mutex_lock(&zonelists_mutex);
1153 build_all_zonelists(NULL, NULL);
1154 mutex_unlock(&zonelists_mutex);
1162 static int check_hotplug_memory_range(u64 start, u64 size)
1164 u64 start_pfn = PFN_DOWN(start);
1165 u64 nr_pages = size >> PAGE_SHIFT;
1167 /* Memory range must be aligned with section */
1168 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1169 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1170 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1171 (unsigned long long)start,
1172 (unsigned long long)size);
1179 static int online_memory_block(struct memory_block *mem, void *arg)
1181 return device_online(&mem->dev);
1184 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1185 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1188 pg_data_t *pgdat = NULL;
1194 size = resource_size(res);
1196 ret = check_hotplug_memory_range(start, size);
1200 { /* Stupid hack to suppress address-never-null warning */
1201 void *p = NODE_DATA(nid);
1205 mem_hotplug_begin();
1208 * Add new range to memblock so that when hotadd_new_pgdat() is called
1209 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1210 * this new range and calculate total pages correctly. The range will
1211 * be removed at hot-remove time.
1213 memblock_add_node(start, size, nid);
1215 new_node = !node_online(nid);
1217 pgdat = hotadd_new_pgdat(nid, start);
1223 /* call arch's memory hotadd */
1224 ret = arch_add_memory(nid, start, size, true);
1229 /* we online node here. we can't roll back from here. */
1230 node_set_online(nid);
1233 unsigned long start_pfn = start >> PAGE_SHIFT;
1234 unsigned long nr_pages = size >> PAGE_SHIFT;
1236 ret = __register_one_node(nid);
1241 * link memory sections under this node. This is already
1242 * done when creatig memory section in register_new_memory
1243 * but that depends to have the node registered so offline
1244 * nodes have to go through register_node.
1245 * TODO clean up this mess.
1247 ret = link_mem_sections(nid, start_pfn, nr_pages);
1250 * If sysfs file of new node can't create, cpu on the node
1251 * can't be hot-added. There is no rollback way now.
1252 * So, check by BUG_ON() to catch it reluctantly..
1257 /* create new memmap entry */
1258 firmware_map_add_hotplug(start, start + size, "System RAM");
1260 /* online pages if requested */
1262 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1263 NULL, online_memory_block);
1268 /* rollback pgdat allocation and others */
1270 rollback_node_hotadd(nid, pgdat);
1271 memblock_remove(start, size);
1277 EXPORT_SYMBOL_GPL(add_memory_resource);
1279 int __ref add_memory(int nid, u64 start, u64 size)
1281 struct resource *res;
1284 res = register_memory_resource(start, size);
1286 return PTR_ERR(res);
1288 ret = add_memory_resource(nid, res, memhp_auto_online);
1290 release_memory_resource(res);
1293 EXPORT_SYMBOL_GPL(add_memory);
1295 #ifdef CONFIG_MEMORY_HOTREMOVE
1297 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1298 * set and the size of the free page is given by page_order(). Using this,
1299 * the function determines if the pageblock contains only free pages.
1300 * Due to buddy contraints, a free page at least the size of a pageblock will
1301 * be located at the start of the pageblock
1303 static inline int pageblock_free(struct page *page)
1305 return PageBuddy(page) && page_order(page) >= pageblock_order;
1308 /* Return the start of the next active pageblock after a given page */
1309 static struct page *next_active_pageblock(struct page *page)
1311 /* Ensure the starting page is pageblock-aligned */
1312 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1314 /* If the entire pageblock is free, move to the end of free page */
1315 if (pageblock_free(page)) {
1317 /* be careful. we don't have locks, page_order can be changed.*/
1318 order = page_order(page);
1319 if ((order < MAX_ORDER) && (order >= pageblock_order))
1320 return page + (1 << order);
1323 return page + pageblock_nr_pages;
1326 /* Checks if this range of memory is likely to be hot-removable. */
1327 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1329 struct page *page = pfn_to_page(start_pfn);
1330 struct page *end_page = page + nr_pages;
1332 /* Check the starting page of each pageblock within the range */
1333 for (; page < end_page; page = next_active_pageblock(page)) {
1334 if (!is_pageblock_removable_nolock(page))
1339 /* All pageblocks in the memory block are likely to be hot-removable */
1344 * Confirm all pages in a range [start, end) belong to the same zone.
1345 * When true, return its valid [start, end).
1347 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1348 unsigned long *valid_start, unsigned long *valid_end)
1350 unsigned long pfn, sec_end_pfn;
1351 unsigned long start, end;
1352 struct zone *zone = NULL;
1355 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1357 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1358 /* Make sure the memory section is present first */
1359 if (!present_section_nr(pfn_to_section_nr(pfn)))
1361 for (; pfn < sec_end_pfn && pfn < end_pfn;
1362 pfn += MAX_ORDER_NR_PAGES) {
1364 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1365 while ((i < MAX_ORDER_NR_PAGES) &&
1366 !pfn_valid_within(pfn + i))
1368 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1370 page = pfn_to_page(pfn + i);
1371 if (zone && page_zone(page) != zone)
1375 zone = page_zone(page);
1376 end = pfn + MAX_ORDER_NR_PAGES;
1381 *valid_start = start;
1382 *valid_end = min(end, end_pfn);
1390 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1391 * non-lru movable pages and hugepages). We scan pfn because it's much
1392 * easier than scanning over linked list. This function returns the pfn
1393 * of the first found movable page if it's found, otherwise 0.
1395 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1399 for (pfn = start; pfn < end; pfn++) {
1400 if (pfn_valid(pfn)) {
1401 page = pfn_to_page(pfn);
1404 if (__PageMovable(page))
1406 if (PageHuge(page)) {
1407 if (page_huge_active(page))
1410 pfn = round_up(pfn + 1,
1411 1 << compound_order(page)) - 1;
1418 static struct page *new_node_page(struct page *page, unsigned long private,
1421 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1422 int nid = page_to_nid(page);
1423 nodemask_t nmask = node_states[N_MEMORY];
1424 struct page *new_page = NULL;
1427 * TODO: allocate a destination hugepage from a nearest neighbor node,
1428 * accordance with memory policy of the user process if possible. For
1429 * now as a simple work-around, we use the next node for destination.
1432 return alloc_huge_page_node(page_hstate(compound_head(page)),
1433 next_node_in(nid, nmask));
1435 node_clear(nid, nmask);
1437 if (PageHighMem(page)
1438 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1439 gfp_mask |= __GFP_HIGHMEM;
1441 if (!nodes_empty(nmask))
1442 new_page = __alloc_pages_nodemask(gfp_mask, 0, nid, &nmask);
1444 new_page = __alloc_pages(gfp_mask, 0, nid);
1449 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1451 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1455 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1456 int not_managed = 0;
1460 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1461 if (!pfn_valid(pfn))
1463 page = pfn_to_page(pfn);
1465 if (PageHuge(page)) {
1466 struct page *head = compound_head(page);
1467 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1468 if (compound_order(head) > PFN_SECTION_SHIFT) {
1472 if (isolate_huge_page(page, &source))
1473 move_pages -= 1 << compound_order(head);
1477 if (!get_page_unless_zero(page))
1480 * We can skip free pages. And we can deal with pages on
1481 * LRU and non-lru movable pages.
1484 ret = isolate_lru_page(page);
1486 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1487 if (!ret) { /* Success */
1489 list_add_tail(&page->lru, &source);
1491 if (!__PageMovable(page))
1492 inc_node_page_state(page, NR_ISOLATED_ANON +
1493 page_is_file_cache(page));
1496 #ifdef CONFIG_DEBUG_VM
1497 pr_alert("failed to isolate pfn %lx\n", pfn);
1498 dump_page(page, "isolation failed");
1501 /* Because we don't have big zone->lock. we should
1502 check this again here. */
1503 if (page_count(page)) {
1510 if (!list_empty(&source)) {
1512 putback_movable_pages(&source);
1516 /* Allocate a new page from the nearest neighbor node */
1517 ret = migrate_pages(&source, new_node_page, NULL, 0,
1518 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1520 putback_movable_pages(&source);
1527 * remove from free_area[] and mark all as Reserved.
1530 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1533 __offline_isolated_pages(start, start + nr_pages);
1538 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1540 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1541 offline_isolated_pages_cb);
1545 * Check all pages in range, recoreded as memory resource, are isolated.
1548 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1552 long offlined = *(long *)data;
1553 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1554 offlined = nr_pages;
1556 *(long *)data += offlined;
1561 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1566 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1567 check_pages_isolated_cb);
1569 offlined = (long)ret;
1573 static int __init cmdline_parse_movable_node(char *p)
1575 #ifdef CONFIG_MOVABLE_NODE
1576 movable_node_enabled = true;
1578 pr_warn("movable_node option not supported\n");
1582 early_param("movable_node", cmdline_parse_movable_node);
1584 /* check which state of node_states will be changed when offline memory */
1585 static void node_states_check_changes_offline(unsigned long nr_pages,
1586 struct zone *zone, struct memory_notify *arg)
1588 struct pglist_data *pgdat = zone->zone_pgdat;
1589 unsigned long present_pages = 0;
1590 enum zone_type zt, zone_last = ZONE_NORMAL;
1593 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1594 * contains nodes which have zones of 0...ZONE_NORMAL,
1595 * set zone_last to ZONE_NORMAL.
1597 * If we don't have HIGHMEM nor movable node,
1598 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1599 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1601 if (N_MEMORY == N_NORMAL_MEMORY)
1602 zone_last = ZONE_MOVABLE;
1605 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1606 * If the memory to be offline is in a zone of 0...zone_last,
1607 * and it is the last present memory, 0...zone_last will
1608 * become empty after offline , thus we can determind we will
1609 * need to clear the node from node_states[N_NORMAL_MEMORY].
1611 for (zt = 0; zt <= zone_last; zt++)
1612 present_pages += pgdat->node_zones[zt].present_pages;
1613 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1614 arg->status_change_nid_normal = zone_to_nid(zone);
1616 arg->status_change_nid_normal = -1;
1618 #ifdef CONFIG_HIGHMEM
1620 * If we have movable node, node_states[N_HIGH_MEMORY]
1621 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1622 * set zone_last to ZONE_HIGHMEM.
1624 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1625 * contains nodes which have zones of 0...ZONE_MOVABLE,
1626 * set zone_last to ZONE_MOVABLE.
1628 zone_last = ZONE_HIGHMEM;
1629 if (N_MEMORY == N_HIGH_MEMORY)
1630 zone_last = ZONE_MOVABLE;
1632 for (; zt <= zone_last; zt++)
1633 present_pages += pgdat->node_zones[zt].present_pages;
1634 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1635 arg->status_change_nid_high = zone_to_nid(zone);
1637 arg->status_change_nid_high = -1;
1639 arg->status_change_nid_high = arg->status_change_nid_normal;
1643 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1645 zone_last = ZONE_MOVABLE;
1648 * check whether node_states[N_HIGH_MEMORY] will be changed
1649 * If we try to offline the last present @nr_pages from the node,
1650 * we can determind we will need to clear the node from
1651 * node_states[N_HIGH_MEMORY].
1653 for (; zt <= zone_last; zt++)
1654 present_pages += pgdat->node_zones[zt].present_pages;
1655 if (nr_pages >= present_pages)
1656 arg->status_change_nid = zone_to_nid(zone);
1658 arg->status_change_nid = -1;
1661 static void node_states_clear_node(int node, struct memory_notify *arg)
1663 if (arg->status_change_nid_normal >= 0)
1664 node_clear_state(node, N_NORMAL_MEMORY);
1666 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1667 (arg->status_change_nid_high >= 0))
1668 node_clear_state(node, N_HIGH_MEMORY);
1670 if ((N_MEMORY != N_HIGH_MEMORY) &&
1671 (arg->status_change_nid >= 0))
1672 node_clear_state(node, N_MEMORY);
1675 static int __ref __offline_pages(unsigned long start_pfn,
1676 unsigned long end_pfn, unsigned long timeout)
1678 unsigned long pfn, nr_pages, expire;
1679 long offlined_pages;
1680 int ret, drain, retry_max, node;
1681 unsigned long flags;
1682 unsigned long valid_start, valid_end;
1684 struct memory_notify arg;
1686 /* at least, alignment against pageblock is necessary */
1687 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1689 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1691 /* This makes hotplug much easier...and readable.
1692 we assume this for now. .*/
1693 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1696 zone = page_zone(pfn_to_page(valid_start));
1697 node = zone_to_nid(zone);
1698 nr_pages = end_pfn - start_pfn;
1700 /* set above range as isolated */
1701 ret = start_isolate_page_range(start_pfn, end_pfn,
1702 MIGRATE_MOVABLE, true);
1706 arg.start_pfn = start_pfn;
1707 arg.nr_pages = nr_pages;
1708 node_states_check_changes_offline(nr_pages, zone, &arg);
1710 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1711 ret = notifier_to_errno(ret);
1713 goto failed_removal;
1716 expire = jiffies + timeout;
1720 /* start memory hot removal */
1722 if (time_after(jiffies, expire))
1723 goto failed_removal;
1725 if (signal_pending(current))
1726 goto failed_removal;
1729 lru_add_drain_all();
1731 drain_all_pages(zone);
1734 pfn = scan_movable_pages(start_pfn, end_pfn);
1735 if (pfn) { /* We have movable pages */
1736 ret = do_migrate_range(pfn, end_pfn);
1742 if (--retry_max == 0)
1743 goto failed_removal;
1749 /* drain all zone's lru pagevec, this is asynchronous... */
1750 lru_add_drain_all();
1752 /* drain pcp pages, this is synchronous. */
1753 drain_all_pages(zone);
1755 * dissolve free hugepages in the memory block before doing offlining
1756 * actually in order to make hugetlbfs's object counting consistent.
1758 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1760 goto failed_removal;
1762 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1763 if (offlined_pages < 0) {
1765 goto failed_removal;
1767 pr_info("Offlined Pages %ld\n", offlined_pages);
1768 /* Ok, all of our target is isolated.
1769 We cannot do rollback at this point. */
1770 offline_isolated_pages(start_pfn, end_pfn);
1771 /* reset pagetype flags and makes migrate type to be MOVABLE */
1772 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1773 /* removal success */
1774 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1775 zone->present_pages -= offlined_pages;
1777 pgdat_resize_lock(zone->zone_pgdat, &flags);
1778 zone->zone_pgdat->node_present_pages -= offlined_pages;
1779 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1781 init_per_zone_wmark_min();
1783 if (!populated_zone(zone)) {
1784 zone_pcp_reset(zone);
1785 mutex_lock(&zonelists_mutex);
1786 build_all_zonelists(NULL, NULL);
1787 mutex_unlock(&zonelists_mutex);
1789 zone_pcp_update(zone);
1791 node_states_clear_node(node, &arg);
1792 if (arg.status_change_nid >= 0) {
1794 kcompactd_stop(node);
1797 vm_total_pages = nr_free_pagecache_pages();
1798 writeback_set_ratelimit();
1800 memory_notify(MEM_OFFLINE, &arg);
1804 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1805 (unsigned long long) start_pfn << PAGE_SHIFT,
1806 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1807 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1808 /* pushback to free area */
1809 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1813 /* Must be protected by mem_hotplug_begin() */
1814 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1816 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1818 #endif /* CONFIG_MEMORY_HOTREMOVE */
1821 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1822 * @start_pfn: start pfn of the memory range
1823 * @end_pfn: end pfn of the memory range
1824 * @arg: argument passed to func
1825 * @func: callback for each memory section walked
1827 * This function walks through all present mem sections in range
1828 * [start_pfn, end_pfn) and call func on each mem section.
1830 * Returns the return value of func.
1832 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1833 void *arg, int (*func)(struct memory_block *, void *))
1835 struct memory_block *mem = NULL;
1836 struct mem_section *section;
1837 unsigned long pfn, section_nr;
1840 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1841 section_nr = pfn_to_section_nr(pfn);
1842 if (!present_section_nr(section_nr))
1845 section = __nr_to_section(section_nr);
1846 /* same memblock? */
1848 if ((section_nr >= mem->start_section_nr) &&
1849 (section_nr <= mem->end_section_nr))
1852 mem = find_memory_block_hinted(section, mem);
1856 ret = func(mem, arg);
1858 kobject_put(&mem->dev.kobj);
1864 kobject_put(&mem->dev.kobj);
1869 #ifdef CONFIG_MEMORY_HOTREMOVE
1870 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1872 int ret = !is_memblock_offlined(mem);
1874 if (unlikely(ret)) {
1875 phys_addr_t beginpa, endpa;
1877 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1878 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1879 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1886 static int check_cpu_on_node(pg_data_t *pgdat)
1890 for_each_present_cpu(cpu) {
1891 if (cpu_to_node(cpu) == pgdat->node_id)
1893 * the cpu on this node isn't removed, and we can't
1894 * offline this node.
1902 static void unmap_cpu_on_node(pg_data_t *pgdat)
1904 #ifdef CONFIG_ACPI_NUMA
1907 for_each_possible_cpu(cpu)
1908 if (cpu_to_node(cpu) == pgdat->node_id)
1909 numa_clear_node(cpu);
1913 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1917 ret = check_cpu_on_node(pgdat);
1922 * the node will be offlined when we come here, so we can clear
1923 * the cpu_to_node() now.
1926 unmap_cpu_on_node(pgdat);
1933 * Offline a node if all memory sections and cpus of the node are removed.
1935 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1936 * and online/offline operations before this call.
1938 void try_offline_node(int nid)
1940 pg_data_t *pgdat = NODE_DATA(nid);
1941 unsigned long start_pfn = pgdat->node_start_pfn;
1942 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
1945 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1946 unsigned long section_nr = pfn_to_section_nr(pfn);
1948 if (!present_section_nr(section_nr))
1951 if (pfn_to_nid(pfn) != nid)
1955 * some memory sections of this node are not removed, and we
1956 * can't offline node now.
1961 if (check_and_unmap_cpu_on_node(pgdat))
1965 * all memory/cpu of this node are removed, we can offline this
1968 node_set_offline(nid);
1969 unregister_one_node(nid);
1971 EXPORT_SYMBOL(try_offline_node);
1976 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1977 * and online/offline operations before this call, as required by
1978 * try_offline_node().
1980 void __ref remove_memory(int nid, u64 start, u64 size)
1984 BUG_ON(check_hotplug_memory_range(start, size));
1986 mem_hotplug_begin();
1989 * All memory blocks must be offlined before removing memory. Check
1990 * whether all memory blocks in question are offline and trigger a BUG()
1991 * if this is not the case.
1993 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1994 check_memblock_offlined_cb);
1998 /* remove memmap entry */
1999 firmware_map_remove(start, start + size, "System RAM");
2000 memblock_free(start, size);
2001 memblock_remove(start, size);
2003 arch_remove_memory(start, size);
2005 try_offline_node(nid);
2009 EXPORT_SYMBOL_GPL(remove_memory);
2010 #endif /* CONFIG_MEMORY_HOTREMOVE */