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 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
57 void get_online_mems(void)
59 percpu_down_read(&mem_hotplug_lock);
62 void put_online_mems(void)
64 percpu_up_read(&mem_hotplug_lock);
67 bool movable_node_enabled = false;
69 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
70 bool memhp_auto_online;
72 bool memhp_auto_online = true;
74 EXPORT_SYMBOL_GPL(memhp_auto_online);
76 static int __init setup_memhp_default_state(char *str)
78 if (!strcmp(str, "online"))
79 memhp_auto_online = true;
80 else if (!strcmp(str, "offline"))
81 memhp_auto_online = false;
85 __setup("memhp_default_state=", setup_memhp_default_state);
87 void mem_hotplug_begin(void)
90 percpu_down_write(&mem_hotplug_lock);
93 void mem_hotplug_done(void)
95 percpu_up_write(&mem_hotplug_lock);
99 /* add this memory to iomem resource */
100 static struct resource *register_memory_resource(u64 start, u64 size)
102 struct resource *res, *conflict;
103 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
105 return ERR_PTR(-ENOMEM);
107 res->name = "System RAM";
109 res->end = start + size - 1;
110 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
111 conflict = request_resource_conflict(&iomem_resource, res);
113 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
114 pr_debug("Device unaddressable memory block "
115 "memory hotplug at %#010llx !\n",
116 (unsigned long long)start);
118 pr_debug("System RAM resource %pR cannot be added\n", res);
120 return ERR_PTR(-EEXIST);
125 static void release_memory_resource(struct resource *res)
129 release_resource(res);
134 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
135 void get_page_bootmem(unsigned long info, struct page *page,
138 page->freelist = (void *)type;
139 SetPagePrivate(page);
140 set_page_private(page, info);
144 void put_page_bootmem(struct page *page)
148 type = (unsigned long) page->freelist;
149 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
150 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
152 if (page_ref_dec_return(page) == 1) {
153 page->freelist = NULL;
154 ClearPagePrivate(page);
155 set_page_private(page, 0);
156 INIT_LIST_HEAD(&page->lru);
157 free_reserved_page(page);
161 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
162 #ifndef CONFIG_SPARSEMEM_VMEMMAP
163 static void register_page_bootmem_info_section(unsigned long start_pfn)
165 unsigned long *usemap, mapsize, section_nr, i;
166 struct mem_section *ms;
167 struct page *page, *memmap;
169 section_nr = pfn_to_section_nr(start_pfn);
170 ms = __nr_to_section(section_nr);
172 /* Get section's memmap address */
173 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
176 * Get page for the memmap's phys address
177 * XXX: need more consideration for sparse_vmemmap...
179 page = virt_to_page(memmap);
180 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
181 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
183 /* remember memmap's page */
184 for (i = 0; i < mapsize; i++, page++)
185 get_page_bootmem(section_nr, page, SECTION_INFO);
187 usemap = ms->pageblock_flags;
188 page = virt_to_page(usemap);
190 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
192 for (i = 0; i < mapsize; i++, page++)
193 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
196 #else /* CONFIG_SPARSEMEM_VMEMMAP */
197 static void register_page_bootmem_info_section(unsigned long start_pfn)
199 unsigned long *usemap, mapsize, section_nr, i;
200 struct mem_section *ms;
201 struct page *page, *memmap;
203 section_nr = pfn_to_section_nr(start_pfn);
204 ms = __nr_to_section(section_nr);
206 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
208 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
210 usemap = ms->pageblock_flags;
211 page = virt_to_page(usemap);
213 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
215 for (i = 0; i < mapsize; i++, page++)
216 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
218 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
220 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
222 unsigned long i, pfn, end_pfn, nr_pages;
223 int node = pgdat->node_id;
226 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
227 page = virt_to_page(pgdat);
229 for (i = 0; i < nr_pages; i++, page++)
230 get_page_bootmem(node, page, NODE_INFO);
232 pfn = pgdat->node_start_pfn;
233 end_pfn = pgdat_end_pfn(pgdat);
235 /* register section info */
236 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
238 * Some platforms can assign the same pfn to multiple nodes - on
239 * node0 as well as nodeN. To avoid registering a pfn against
240 * multiple nodes we check that this pfn does not already
241 * reside in some other nodes.
243 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
244 register_page_bootmem_info_section(pfn);
247 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
249 static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
250 struct vmem_altmap *altmap, bool want_memblock)
255 if (pfn_valid(phys_start_pfn))
258 ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn, altmap);
263 * Make all the pages reserved so that nobody will stumble over half
265 * FIXME: We also have to associate it with a node because page_to_nid
266 * relies on having page with the proper node.
268 for (i = 0; i < PAGES_PER_SECTION; i++) {
269 unsigned long pfn = phys_start_pfn + i;
274 page = pfn_to_page(pfn);
275 set_page_node(page, nid);
276 SetPageReserved(page);
282 return hotplug_memory_register(nid, __pfn_to_section(phys_start_pfn));
286 * Reasonably generic function for adding memory. It is
287 * expected that archs that support memory hotplug will
288 * call this function after deciding the zone to which to
291 int __ref __add_pages(int nid, unsigned long phys_start_pfn,
292 unsigned long nr_pages, struct vmem_altmap *altmap,
297 int start_sec, end_sec;
299 /* during initialize mem_map, align hot-added range to section */
300 start_sec = pfn_to_section_nr(phys_start_pfn);
301 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
305 * Validate altmap is within bounds of the total request
307 if (altmap->base_pfn != phys_start_pfn
308 || vmem_altmap_offset(altmap) > nr_pages) {
309 pr_warn_once("memory add fail, invalid altmap\n");
316 for (i = start_sec; i <= end_sec; i++) {
317 err = __add_section(nid, section_nr_to_pfn(i), altmap,
321 * EEXIST is finally dealt with by ioresource collision
322 * check. see add_memory() => register_memory_resource()
323 * Warning will be printed if there is collision.
325 if (err && (err != -EEXIST))
330 vmemmap_populate_print_last();
335 #ifdef CONFIG_MEMORY_HOTREMOVE
336 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
337 static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
338 unsigned long start_pfn,
339 unsigned long end_pfn)
341 struct mem_section *ms;
343 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
344 ms = __pfn_to_section(start_pfn);
346 if (unlikely(!valid_section(ms)))
349 if (unlikely(pfn_to_nid(start_pfn) != nid))
352 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
361 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
362 static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
363 unsigned long start_pfn,
364 unsigned long end_pfn)
366 struct mem_section *ms;
369 /* pfn is the end pfn of a memory section. */
371 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
372 ms = __pfn_to_section(pfn);
374 if (unlikely(!valid_section(ms)))
377 if (unlikely(pfn_to_nid(pfn) != nid))
380 if (zone && zone != page_zone(pfn_to_page(pfn)))
389 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
390 unsigned long end_pfn)
392 unsigned long zone_start_pfn = zone->zone_start_pfn;
393 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
394 unsigned long zone_end_pfn = z;
396 struct mem_section *ms;
397 int nid = zone_to_nid(zone);
399 zone_span_writelock(zone);
400 if (zone_start_pfn == start_pfn) {
402 * If the section is smallest section in the zone, it need
403 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
404 * In this case, we find second smallest valid mem_section
405 * for shrinking zone.
407 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
410 zone->zone_start_pfn = pfn;
411 zone->spanned_pages = zone_end_pfn - pfn;
413 } else if (zone_end_pfn == end_pfn) {
415 * If the section is biggest section in the zone, it need
416 * shrink zone->spanned_pages.
417 * In this case, we find second biggest valid mem_section for
420 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
423 zone->spanned_pages = pfn - zone_start_pfn + 1;
427 * The section is not biggest or smallest mem_section in the zone, it
428 * only creates a hole in the zone. So in this case, we need not
429 * change the zone. But perhaps, the zone has only hole data. Thus
430 * it check the zone has only hole or not.
432 pfn = zone_start_pfn;
433 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
434 ms = __pfn_to_section(pfn);
436 if (unlikely(!valid_section(ms)))
439 if (page_zone(pfn_to_page(pfn)) != zone)
442 /* If the section is current section, it continues the loop */
443 if (start_pfn == pfn)
446 /* If we find valid section, we have nothing to do */
447 zone_span_writeunlock(zone);
451 /* The zone has no valid section */
452 zone->zone_start_pfn = 0;
453 zone->spanned_pages = 0;
454 zone_span_writeunlock(zone);
457 static void shrink_pgdat_span(struct pglist_data *pgdat,
458 unsigned long start_pfn, unsigned long end_pfn)
460 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
461 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
462 unsigned long pgdat_end_pfn = p;
464 struct mem_section *ms;
465 int nid = pgdat->node_id;
467 if (pgdat_start_pfn == start_pfn) {
469 * If the section is smallest section in the pgdat, it need
470 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
471 * In this case, we find second smallest valid mem_section
472 * for shrinking zone.
474 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
477 pgdat->node_start_pfn = pfn;
478 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
480 } else if (pgdat_end_pfn == end_pfn) {
482 * If the section is biggest section in the pgdat, it need
483 * shrink pgdat->node_spanned_pages.
484 * In this case, we find second biggest valid mem_section for
487 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
490 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
494 * If the section is not biggest or smallest mem_section in the pgdat,
495 * it only creates a hole in the pgdat. So in this case, we need not
497 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
498 * has only hole or not.
500 pfn = pgdat_start_pfn;
501 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
502 ms = __pfn_to_section(pfn);
504 if (unlikely(!valid_section(ms)))
507 if (pfn_to_nid(pfn) != nid)
510 /* If the section is current section, it continues the loop */
511 if (start_pfn == pfn)
514 /* If we find valid section, we have nothing to do */
518 /* The pgdat has no valid section */
519 pgdat->node_start_pfn = 0;
520 pgdat->node_spanned_pages = 0;
523 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
525 struct pglist_data *pgdat = zone->zone_pgdat;
526 int nr_pages = PAGES_PER_SECTION;
529 pgdat_resize_lock(zone->zone_pgdat, &flags);
530 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
531 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
532 pgdat_resize_unlock(zone->zone_pgdat, &flags);
535 static int __remove_section(struct zone *zone, struct mem_section *ms,
536 unsigned long map_offset, struct vmem_altmap *altmap)
538 unsigned long start_pfn;
542 if (!valid_section(ms))
545 ret = unregister_memory_section(ms);
549 scn_nr = __section_nr(ms);
550 start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
551 __remove_zone(zone, start_pfn);
553 sparse_remove_one_section(zone, ms, map_offset, altmap);
558 * __remove_pages() - remove sections of pages from a zone
559 * @zone: zone from which pages need to be removed
560 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
561 * @nr_pages: number of pages to remove (must be multiple of section size)
563 * Generic helper function to remove section mappings and sysfs entries
564 * for the section of the memory we are removing. Caller needs to make
565 * sure that pages are marked reserved and zones are adjust properly by
566 * calling offline_pages().
568 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
569 unsigned long nr_pages, struct vmem_altmap *altmap)
572 unsigned long map_offset = 0;
573 int sections_to_remove, ret = 0;
575 /* In the ZONE_DEVICE case device driver owns the memory region */
576 if (is_dev_zone(zone)) {
578 map_offset = vmem_altmap_offset(altmap);
580 resource_size_t start, size;
582 start = phys_start_pfn << PAGE_SHIFT;
583 size = nr_pages * PAGE_SIZE;
585 ret = release_mem_region_adjustable(&iomem_resource, start,
588 resource_size_t endres = start + size - 1;
590 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
591 &start, &endres, ret);
595 clear_zone_contiguous(zone);
598 * We can only remove entire sections
600 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
601 BUG_ON(nr_pages % PAGES_PER_SECTION);
603 sections_to_remove = nr_pages / PAGES_PER_SECTION;
604 for (i = 0; i < sections_to_remove; i++) {
605 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
607 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
614 set_zone_contiguous(zone);
618 #endif /* CONFIG_MEMORY_HOTREMOVE */
620 int set_online_page_callback(online_page_callback_t callback)
625 mutex_lock(&online_page_callback_lock);
627 if (online_page_callback == generic_online_page) {
628 online_page_callback = callback;
632 mutex_unlock(&online_page_callback_lock);
637 EXPORT_SYMBOL_GPL(set_online_page_callback);
639 int restore_online_page_callback(online_page_callback_t callback)
644 mutex_lock(&online_page_callback_lock);
646 if (online_page_callback == callback) {
647 online_page_callback = generic_online_page;
651 mutex_unlock(&online_page_callback_lock);
656 EXPORT_SYMBOL_GPL(restore_online_page_callback);
658 void __online_page_set_limits(struct page *page)
661 EXPORT_SYMBOL_GPL(__online_page_set_limits);
663 void __online_page_increment_counters(struct page *page)
665 adjust_managed_page_count(page, 1);
667 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
669 void __online_page_free(struct page *page)
671 __free_reserved_page(page);
673 EXPORT_SYMBOL_GPL(__online_page_free);
675 static void generic_online_page(struct page *page)
677 __online_page_set_limits(page);
678 __online_page_increment_counters(page);
679 __online_page_free(page);
682 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
686 unsigned long onlined_pages = *(unsigned long *)arg;
689 if (PageReserved(pfn_to_page(start_pfn)))
690 for (i = 0; i < nr_pages; i++) {
691 page = pfn_to_page(start_pfn + i);
692 (*online_page_callback)(page);
696 online_mem_sections(start_pfn, start_pfn + nr_pages);
698 *(unsigned long *)arg = onlined_pages;
702 /* check which state of node_states will be changed when online memory */
703 static void node_states_check_changes_online(unsigned long nr_pages,
704 struct zone *zone, struct memory_notify *arg)
706 int nid = zone_to_nid(zone);
707 enum zone_type zone_last = ZONE_NORMAL;
710 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
711 * contains nodes which have zones of 0...ZONE_NORMAL,
712 * set zone_last to ZONE_NORMAL.
714 * If we don't have HIGHMEM nor movable node,
715 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
716 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
718 if (N_MEMORY == N_NORMAL_MEMORY)
719 zone_last = ZONE_MOVABLE;
722 * if the memory to be online is in a zone of 0...zone_last, and
723 * the zones of 0...zone_last don't have memory before online, we will
724 * need to set the node to node_states[N_NORMAL_MEMORY] after
725 * the memory is online.
727 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
728 arg->status_change_nid_normal = nid;
730 arg->status_change_nid_normal = -1;
732 #ifdef CONFIG_HIGHMEM
734 * If we have movable node, node_states[N_HIGH_MEMORY]
735 * contains nodes which have zones of 0...ZONE_HIGHMEM,
736 * set zone_last to ZONE_HIGHMEM.
738 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
739 * contains nodes which have zones of 0...ZONE_MOVABLE,
740 * set zone_last to ZONE_MOVABLE.
742 zone_last = ZONE_HIGHMEM;
743 if (N_MEMORY == N_HIGH_MEMORY)
744 zone_last = ZONE_MOVABLE;
746 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
747 arg->status_change_nid_high = nid;
749 arg->status_change_nid_high = -1;
751 arg->status_change_nid_high = arg->status_change_nid_normal;
755 * if the node don't have memory befor online, we will need to
756 * set the node to node_states[N_MEMORY] after the memory
759 if (!node_state(nid, N_MEMORY))
760 arg->status_change_nid = nid;
762 arg->status_change_nid = -1;
765 static void node_states_set_node(int node, struct memory_notify *arg)
767 if (arg->status_change_nid_normal >= 0)
768 node_set_state(node, N_NORMAL_MEMORY);
770 if (arg->status_change_nid_high >= 0)
771 node_set_state(node, N_HIGH_MEMORY);
773 node_set_state(node, N_MEMORY);
776 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
777 unsigned long nr_pages)
779 unsigned long old_end_pfn = zone_end_pfn(zone);
781 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
782 zone->zone_start_pfn = start_pfn;
784 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
787 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
788 unsigned long nr_pages)
790 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
792 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
793 pgdat->node_start_pfn = start_pfn;
795 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
798 void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
799 unsigned long nr_pages, struct vmem_altmap *altmap)
801 struct pglist_data *pgdat = zone->zone_pgdat;
802 int nid = pgdat->node_id;
805 if (zone_is_empty(zone))
806 init_currently_empty_zone(zone, start_pfn, nr_pages);
808 clear_zone_contiguous(zone);
810 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
811 pgdat_resize_lock(pgdat, &flags);
812 zone_span_writelock(zone);
813 resize_zone_range(zone, start_pfn, nr_pages);
814 zone_span_writeunlock(zone);
815 resize_pgdat_range(pgdat, start_pfn, nr_pages);
816 pgdat_resize_unlock(pgdat, &flags);
819 * TODO now we have a visible range of pages which are not associated
820 * with their zone properly. Not nice but set_pfnblock_flags_mask
821 * expects the zone spans the pfn range. All the pages in the range
822 * are reserved so nobody should be touching them so we should be safe
824 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
825 MEMMAP_HOTPLUG, altmap);
827 set_zone_contiguous(zone);
831 * Returns a default kernel memory zone for the given pfn range.
832 * If no kernel zone covers this pfn range it will automatically go
833 * to the ZONE_NORMAL.
835 static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
836 unsigned long nr_pages)
838 struct pglist_data *pgdat = NODE_DATA(nid);
841 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
842 struct zone *zone = &pgdat->node_zones[zid];
844 if (zone_intersects(zone, start_pfn, nr_pages))
848 return &pgdat->node_zones[ZONE_NORMAL];
851 static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
852 unsigned long nr_pages)
854 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
856 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
857 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
858 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
861 * We inherit the existing zone in a simple case where zones do not
862 * overlap in the given range
864 if (in_kernel ^ in_movable)
865 return (in_kernel) ? kernel_zone : movable_zone;
868 * If the range doesn't belong to any zone or two zones overlap in the
869 * given range then we use movable zone only if movable_node is
870 * enabled because we always online to a kernel zone by default.
872 return movable_node_enabled ? movable_zone : kernel_zone;
875 struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
876 unsigned long nr_pages)
878 if (online_type == MMOP_ONLINE_KERNEL)
879 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
881 if (online_type == MMOP_ONLINE_MOVABLE)
882 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
884 return default_zone_for_pfn(nid, start_pfn, nr_pages);
888 * Associates the given pfn range with the given node and the zone appropriate
889 * for the given online type.
891 static struct zone * __meminit move_pfn_range(int online_type, int nid,
892 unsigned long start_pfn, unsigned long nr_pages)
896 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
897 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
901 /* Must be protected by mem_hotplug_begin() or a device_lock */
902 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
905 unsigned long onlined_pages = 0;
907 int need_zonelists_rebuild = 0;
910 struct memory_notify arg;
912 nid = pfn_to_nid(pfn);
913 /* associate pfn range with the zone */
914 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
917 arg.nr_pages = nr_pages;
918 node_states_check_changes_online(nr_pages, zone, &arg);
920 ret = memory_notify(MEM_GOING_ONLINE, &arg);
921 ret = notifier_to_errno(ret);
923 goto failed_addition;
926 * If this zone is not populated, then it is not in zonelist.
927 * This means the page allocator ignores this zone.
928 * So, zonelist must be updated after online.
930 if (!populated_zone(zone)) {
931 need_zonelists_rebuild = 1;
932 setup_zone_pageset(zone);
935 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
938 if (need_zonelists_rebuild)
939 zone_pcp_reset(zone);
940 goto failed_addition;
943 zone->present_pages += onlined_pages;
945 pgdat_resize_lock(zone->zone_pgdat, &flags);
946 zone->zone_pgdat->node_present_pages += onlined_pages;
947 pgdat_resize_unlock(zone->zone_pgdat, &flags);
950 node_states_set_node(nid, &arg);
951 if (need_zonelists_rebuild)
952 build_all_zonelists(NULL);
954 zone_pcp_update(zone);
957 init_per_zone_wmark_min();
964 vm_total_pages = nr_free_pagecache_pages();
966 writeback_set_ratelimit();
969 memory_notify(MEM_ONLINE, &arg);
973 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
974 (unsigned long long) pfn << PAGE_SHIFT,
975 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
976 memory_notify(MEM_CANCEL_ONLINE, &arg);
979 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
981 static void reset_node_present_pages(pg_data_t *pgdat)
985 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
986 z->present_pages = 0;
988 pgdat->node_present_pages = 0;
991 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
992 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
994 struct pglist_data *pgdat;
995 unsigned long zones_size[MAX_NR_ZONES] = {0};
996 unsigned long zholes_size[MAX_NR_ZONES] = {0};
997 unsigned long start_pfn = PFN_DOWN(start);
999 pgdat = NODE_DATA(nid);
1001 pgdat = arch_alloc_nodedata(nid);
1005 arch_refresh_nodedata(nid, pgdat);
1008 * Reset the nr_zones, order and classzone_idx before reuse.
1009 * Note that kswapd will init kswapd_classzone_idx properly
1010 * when it starts in the near future.
1012 pgdat->nr_zones = 0;
1013 pgdat->kswapd_order = 0;
1014 pgdat->kswapd_classzone_idx = 0;
1017 /* we can use NODE_DATA(nid) from here */
1019 /* init node's zones as empty zones, we don't have any present pages.*/
1020 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1021 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
1024 * The node we allocated has no zone fallback lists. For avoiding
1025 * to access not-initialized zonelist, build here.
1027 build_all_zonelists(pgdat);
1030 * zone->managed_pages is set to an approximate value in
1031 * free_area_init_core(), which will cause
1032 * /sys/device/system/node/nodeX/meminfo has wrong data.
1033 * So reset it to 0 before any memory is onlined.
1035 reset_node_managed_pages(pgdat);
1038 * When memory is hot-added, all the memory is in offline state. So
1039 * clear all zones' present_pages because they will be updated in
1040 * online_pages() and offline_pages().
1042 reset_node_present_pages(pgdat);
1047 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1049 arch_refresh_nodedata(nid, NULL);
1050 free_percpu(pgdat->per_cpu_nodestats);
1051 arch_free_nodedata(pgdat);
1057 * try_online_node - online a node if offlined
1059 * called by cpu_up() to online a node without onlined memory.
1061 int try_online_node(int nid)
1066 if (node_online(nid))
1069 mem_hotplug_begin();
1070 pgdat = hotadd_new_pgdat(nid, 0);
1072 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1076 node_set_online(nid);
1077 ret = register_one_node(nid);
1084 static int check_hotplug_memory_range(u64 start, u64 size)
1086 unsigned long block_sz = memory_block_size_bytes();
1087 u64 block_nr_pages = block_sz >> PAGE_SHIFT;
1088 u64 nr_pages = size >> PAGE_SHIFT;
1089 u64 start_pfn = PFN_DOWN(start);
1091 /* memory range must be block size aligned */
1092 if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) ||
1093 !IS_ALIGNED(nr_pages, block_nr_pages)) {
1094 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1095 block_sz, start, size);
1102 static int online_memory_block(struct memory_block *mem, void *arg)
1104 return device_online(&mem->dev);
1107 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1108 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1111 pg_data_t *pgdat = NULL;
1117 size = resource_size(res);
1119 ret = check_hotplug_memory_range(start, size);
1123 { /* Stupid hack to suppress address-never-null warning */
1124 void *p = NODE_DATA(nid);
1128 mem_hotplug_begin();
1131 * Add new range to memblock so that when hotadd_new_pgdat() is called
1132 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1133 * this new range and calculate total pages correctly. The range will
1134 * be removed at hot-remove time.
1136 memblock_add_node(start, size, nid);
1138 new_node = !node_online(nid);
1140 pgdat = hotadd_new_pgdat(nid, start);
1146 /* call arch's memory hotadd */
1147 ret = arch_add_memory(nid, start, size, NULL, true);
1152 /* we online node here. we can't roll back from here. */
1153 node_set_online(nid);
1156 unsigned long start_pfn = start >> PAGE_SHIFT;
1157 unsigned long nr_pages = size >> PAGE_SHIFT;
1159 ret = __register_one_node(nid);
1164 * link memory sections under this node. This is already
1165 * done when creatig memory section in register_new_memory
1166 * but that depends to have the node registered so offline
1167 * nodes have to go through register_node.
1168 * TODO clean up this mess.
1170 ret = link_mem_sections(nid, start_pfn, nr_pages);
1173 * If sysfs file of new node can't create, cpu on the node
1174 * can't be hot-added. There is no rollback way now.
1175 * So, check by BUG_ON() to catch it reluctantly..
1180 /* create new memmap entry */
1181 firmware_map_add_hotplug(start, start + size, "System RAM");
1183 /* online pages if requested */
1185 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1186 NULL, online_memory_block);
1191 /* rollback pgdat allocation and others */
1192 if (new_pgdat && pgdat)
1193 rollback_node_hotadd(nid, pgdat);
1194 memblock_remove(start, size);
1200 EXPORT_SYMBOL_GPL(add_memory_resource);
1202 int __ref add_memory(int nid, u64 start, u64 size)
1204 struct resource *res;
1207 res = register_memory_resource(start, size);
1209 return PTR_ERR(res);
1211 ret = add_memory_resource(nid, res, memhp_auto_online);
1213 release_memory_resource(res);
1216 EXPORT_SYMBOL_GPL(add_memory);
1218 #ifdef CONFIG_MEMORY_HOTREMOVE
1220 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1221 * set and the size of the free page is given by page_order(). Using this,
1222 * the function determines if the pageblock contains only free pages.
1223 * Due to buddy contraints, a free page at least the size of a pageblock will
1224 * be located at the start of the pageblock
1226 static inline int pageblock_free(struct page *page)
1228 return PageBuddy(page) && page_order(page) >= pageblock_order;
1231 /* Return the start of the next active pageblock after a given page */
1232 static struct page *next_active_pageblock(struct page *page)
1234 /* Ensure the starting page is pageblock-aligned */
1235 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1237 /* If the entire pageblock is free, move to the end of free page */
1238 if (pageblock_free(page)) {
1240 /* be careful. we don't have locks, page_order can be changed.*/
1241 order = page_order(page);
1242 if ((order < MAX_ORDER) && (order >= pageblock_order))
1243 return page + (1 << order);
1246 return page + pageblock_nr_pages;
1249 /* Checks if this range of memory is likely to be hot-removable. */
1250 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1252 struct page *page = pfn_to_page(start_pfn);
1253 struct page *end_page = page + nr_pages;
1255 /* Check the starting page of each pageblock within the range */
1256 for (; page < end_page; page = next_active_pageblock(page)) {
1257 if (!is_pageblock_removable_nolock(page))
1262 /* All pageblocks in the memory block are likely to be hot-removable */
1267 * Confirm all pages in a range [start, end) belong to the same zone.
1268 * When true, return its valid [start, end).
1270 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1271 unsigned long *valid_start, unsigned long *valid_end)
1273 unsigned long pfn, sec_end_pfn;
1274 unsigned long start, end;
1275 struct zone *zone = NULL;
1278 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1280 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1281 /* Make sure the memory section is present first */
1282 if (!present_section_nr(pfn_to_section_nr(pfn)))
1284 for (; pfn < sec_end_pfn && pfn < end_pfn;
1285 pfn += MAX_ORDER_NR_PAGES) {
1287 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1288 while ((i < MAX_ORDER_NR_PAGES) &&
1289 !pfn_valid_within(pfn + i))
1291 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1293 page = pfn_to_page(pfn + i);
1294 if (zone && page_zone(page) != zone)
1298 zone = page_zone(page);
1299 end = pfn + MAX_ORDER_NR_PAGES;
1304 *valid_start = start;
1305 *valid_end = min(end, end_pfn);
1313 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1314 * non-lru movable pages and hugepages). We scan pfn because it's much
1315 * easier than scanning over linked list. This function returns the pfn
1316 * of the first found movable page if it's found, otherwise 0.
1318 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1322 for (pfn = start; pfn < end; pfn++) {
1323 if (pfn_valid(pfn)) {
1324 page = pfn_to_page(pfn);
1327 if (__PageMovable(page))
1329 if (PageHuge(page)) {
1330 if (page_huge_active(page))
1333 pfn = round_up(pfn + 1,
1334 1 << compound_order(page)) - 1;
1341 static struct page *new_node_page(struct page *page, unsigned long private,
1344 int nid = page_to_nid(page);
1345 nodemask_t nmask = node_states[N_MEMORY];
1348 * try to allocate from a different node but reuse this node if there
1349 * are no other online nodes to be used (e.g. we are offlining a part
1350 * of the only existing node)
1352 node_clear(nid, nmask);
1353 if (nodes_empty(nmask))
1354 node_set(nid, nmask);
1356 return new_page_nodemask(page, nid, &nmask);
1359 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1361 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1365 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1366 int not_managed = 0;
1370 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1371 if (!pfn_valid(pfn))
1373 page = pfn_to_page(pfn);
1375 if (PageHuge(page)) {
1376 struct page *head = compound_head(page);
1377 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1378 if (compound_order(head) > PFN_SECTION_SHIFT) {
1382 if (isolate_huge_page(page, &source))
1383 move_pages -= 1 << compound_order(head);
1385 } else if (thp_migration_supported() && PageTransHuge(page))
1386 pfn = page_to_pfn(compound_head(page))
1387 + hpage_nr_pages(page) - 1;
1389 if (!get_page_unless_zero(page))
1392 * We can skip free pages. And we can deal with pages on
1393 * LRU and non-lru movable pages.
1396 ret = isolate_lru_page(page);
1398 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1399 if (!ret) { /* Success */
1401 list_add_tail(&page->lru, &source);
1403 if (!__PageMovable(page))
1404 inc_node_page_state(page, NR_ISOLATED_ANON +
1405 page_is_file_cache(page));
1408 #ifdef CONFIG_DEBUG_VM
1409 pr_alert("failed to isolate pfn %lx\n", pfn);
1410 dump_page(page, "isolation failed");
1413 /* Because we don't have big zone->lock. we should
1414 check this again here. */
1415 if (page_count(page)) {
1422 if (!list_empty(&source)) {
1424 putback_movable_pages(&source);
1428 /* Allocate a new page from the nearest neighbor node */
1429 ret = migrate_pages(&source, new_node_page, NULL, 0,
1430 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1432 putback_movable_pages(&source);
1439 * remove from free_area[] and mark all as Reserved.
1442 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1445 __offline_isolated_pages(start, start + nr_pages);
1450 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1452 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1453 offline_isolated_pages_cb);
1457 * Check all pages in range, recoreded as memory resource, are isolated.
1460 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1464 long offlined = *(long *)data;
1465 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1466 offlined = nr_pages;
1468 *(long *)data += offlined;
1473 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1478 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1479 check_pages_isolated_cb);
1481 offlined = (long)ret;
1485 static int __init cmdline_parse_movable_node(char *p)
1487 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1488 movable_node_enabled = true;
1490 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1494 early_param("movable_node", cmdline_parse_movable_node);
1496 /* check which state of node_states will be changed when offline memory */
1497 static void node_states_check_changes_offline(unsigned long nr_pages,
1498 struct zone *zone, struct memory_notify *arg)
1500 struct pglist_data *pgdat = zone->zone_pgdat;
1501 unsigned long present_pages = 0;
1502 enum zone_type zt, zone_last = ZONE_NORMAL;
1505 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1506 * contains nodes which have zones of 0...ZONE_NORMAL,
1507 * set zone_last to ZONE_NORMAL.
1509 * If we don't have HIGHMEM nor movable node,
1510 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1511 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1513 if (N_MEMORY == N_NORMAL_MEMORY)
1514 zone_last = ZONE_MOVABLE;
1517 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1518 * If the memory to be offline is in a zone of 0...zone_last,
1519 * and it is the last present memory, 0...zone_last will
1520 * become empty after offline , thus we can determind we will
1521 * need to clear the node from node_states[N_NORMAL_MEMORY].
1523 for (zt = 0; zt <= zone_last; zt++)
1524 present_pages += pgdat->node_zones[zt].present_pages;
1525 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1526 arg->status_change_nid_normal = zone_to_nid(zone);
1528 arg->status_change_nid_normal = -1;
1530 #ifdef CONFIG_HIGHMEM
1532 * If we have movable node, node_states[N_HIGH_MEMORY]
1533 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1534 * set zone_last to ZONE_HIGHMEM.
1536 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1537 * contains nodes which have zones of 0...ZONE_MOVABLE,
1538 * set zone_last to ZONE_MOVABLE.
1540 zone_last = ZONE_HIGHMEM;
1541 if (N_MEMORY == N_HIGH_MEMORY)
1542 zone_last = ZONE_MOVABLE;
1544 for (; zt <= zone_last; zt++)
1545 present_pages += pgdat->node_zones[zt].present_pages;
1546 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1547 arg->status_change_nid_high = zone_to_nid(zone);
1549 arg->status_change_nid_high = -1;
1551 arg->status_change_nid_high = arg->status_change_nid_normal;
1555 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1557 zone_last = ZONE_MOVABLE;
1560 * check whether node_states[N_HIGH_MEMORY] will be changed
1561 * If we try to offline the last present @nr_pages from the node,
1562 * we can determind we will need to clear the node from
1563 * node_states[N_HIGH_MEMORY].
1565 for (; zt <= zone_last; zt++)
1566 present_pages += pgdat->node_zones[zt].present_pages;
1567 if (nr_pages >= present_pages)
1568 arg->status_change_nid = zone_to_nid(zone);
1570 arg->status_change_nid = -1;
1573 static void node_states_clear_node(int node, struct memory_notify *arg)
1575 if (arg->status_change_nid_normal >= 0)
1576 node_clear_state(node, N_NORMAL_MEMORY);
1578 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1579 (arg->status_change_nid_high >= 0))
1580 node_clear_state(node, N_HIGH_MEMORY);
1582 if ((N_MEMORY != N_HIGH_MEMORY) &&
1583 (arg->status_change_nid >= 0))
1584 node_clear_state(node, N_MEMORY);
1587 static int __ref __offline_pages(unsigned long start_pfn,
1588 unsigned long end_pfn)
1590 unsigned long pfn, nr_pages;
1591 long offlined_pages;
1593 unsigned long flags;
1594 unsigned long valid_start, valid_end;
1596 struct memory_notify arg;
1598 /* at least, alignment against pageblock is necessary */
1599 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1601 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1603 /* This makes hotplug much easier...and readable.
1604 we assume this for now. .*/
1605 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1608 zone = page_zone(pfn_to_page(valid_start));
1609 node = zone_to_nid(zone);
1610 nr_pages = end_pfn - start_pfn;
1612 /* set above range as isolated */
1613 ret = start_isolate_page_range(start_pfn, end_pfn,
1614 MIGRATE_MOVABLE, true);
1618 arg.start_pfn = start_pfn;
1619 arg.nr_pages = nr_pages;
1620 node_states_check_changes_offline(nr_pages, zone, &arg);
1622 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1623 ret = notifier_to_errno(ret);
1625 goto failed_removal;
1629 /* start memory hot removal */
1631 if (signal_pending(current))
1632 goto failed_removal;
1635 lru_add_drain_all();
1636 drain_all_pages(zone);
1638 pfn = scan_movable_pages(start_pfn, end_pfn);
1639 if (pfn) { /* We have movable pages */
1640 ret = do_migrate_range(pfn, end_pfn);
1645 * dissolve free hugepages in the memory block before doing offlining
1646 * actually in order to make hugetlbfs's object counting consistent.
1648 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1650 goto failed_removal;
1652 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1653 if (offlined_pages < 0)
1655 pr_info("Offlined Pages %ld\n", offlined_pages);
1656 /* Ok, all of our target is isolated.
1657 We cannot do rollback at this point. */
1658 offline_isolated_pages(start_pfn, end_pfn);
1659 /* reset pagetype flags and makes migrate type to be MOVABLE */
1660 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1661 /* removal success */
1662 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1663 zone->present_pages -= offlined_pages;
1665 pgdat_resize_lock(zone->zone_pgdat, &flags);
1666 zone->zone_pgdat->node_present_pages -= offlined_pages;
1667 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1669 init_per_zone_wmark_min();
1671 if (!populated_zone(zone)) {
1672 zone_pcp_reset(zone);
1673 build_all_zonelists(NULL);
1675 zone_pcp_update(zone);
1677 node_states_clear_node(node, &arg);
1678 if (arg.status_change_nid >= 0) {
1680 kcompactd_stop(node);
1683 vm_total_pages = nr_free_pagecache_pages();
1684 writeback_set_ratelimit();
1686 memory_notify(MEM_OFFLINE, &arg);
1690 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1691 (unsigned long long) start_pfn << PAGE_SHIFT,
1692 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1693 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1694 /* pushback to free area */
1695 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1699 /* Must be protected by mem_hotplug_begin() or a device_lock */
1700 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1702 return __offline_pages(start_pfn, start_pfn + nr_pages);
1704 #endif /* CONFIG_MEMORY_HOTREMOVE */
1707 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1708 * @start_pfn: start pfn of the memory range
1709 * @end_pfn: end pfn of the memory range
1710 * @arg: argument passed to func
1711 * @func: callback for each memory section walked
1713 * This function walks through all present mem sections in range
1714 * [start_pfn, end_pfn) and call func on each mem section.
1716 * Returns the return value of func.
1718 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1719 void *arg, int (*func)(struct memory_block *, void *))
1721 struct memory_block *mem = NULL;
1722 struct mem_section *section;
1723 unsigned long pfn, section_nr;
1726 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1727 section_nr = pfn_to_section_nr(pfn);
1728 if (!present_section_nr(section_nr))
1731 section = __nr_to_section(section_nr);
1732 /* same memblock? */
1734 if ((section_nr >= mem->start_section_nr) &&
1735 (section_nr <= mem->end_section_nr))
1738 mem = find_memory_block_hinted(section, mem);
1742 ret = func(mem, arg);
1744 kobject_put(&mem->dev.kobj);
1750 kobject_put(&mem->dev.kobj);
1755 #ifdef CONFIG_MEMORY_HOTREMOVE
1756 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1758 int ret = !is_memblock_offlined(mem);
1760 if (unlikely(ret)) {
1761 phys_addr_t beginpa, endpa;
1763 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1764 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1765 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1772 static int check_cpu_on_node(pg_data_t *pgdat)
1776 for_each_present_cpu(cpu) {
1777 if (cpu_to_node(cpu) == pgdat->node_id)
1779 * the cpu on this node isn't removed, and we can't
1780 * offline this node.
1788 static void unmap_cpu_on_node(pg_data_t *pgdat)
1790 #ifdef CONFIG_ACPI_NUMA
1793 for_each_possible_cpu(cpu)
1794 if (cpu_to_node(cpu) == pgdat->node_id)
1795 numa_clear_node(cpu);
1799 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1803 ret = check_cpu_on_node(pgdat);
1808 * the node will be offlined when we come here, so we can clear
1809 * the cpu_to_node() now.
1812 unmap_cpu_on_node(pgdat);
1819 * Offline a node if all memory sections and cpus of the node are removed.
1821 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1822 * and online/offline operations before this call.
1824 void try_offline_node(int nid)
1826 pg_data_t *pgdat = NODE_DATA(nid);
1827 unsigned long start_pfn = pgdat->node_start_pfn;
1828 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
1831 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1832 unsigned long section_nr = pfn_to_section_nr(pfn);
1834 if (!present_section_nr(section_nr))
1837 if (pfn_to_nid(pfn) != nid)
1841 * some memory sections of this node are not removed, and we
1842 * can't offline node now.
1847 if (check_and_unmap_cpu_on_node(pgdat))
1851 * all memory/cpu of this node are removed, we can offline this
1854 node_set_offline(nid);
1855 unregister_one_node(nid);
1857 EXPORT_SYMBOL(try_offline_node);
1862 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1863 * and online/offline operations before this call, as required by
1864 * try_offline_node().
1866 void __ref remove_memory(int nid, u64 start, u64 size)
1870 BUG_ON(check_hotplug_memory_range(start, size));
1872 mem_hotplug_begin();
1875 * All memory blocks must be offlined before removing memory. Check
1876 * whether all memory blocks in question are offline and trigger a BUG()
1877 * if this is not the case.
1879 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1880 check_memblock_offlined_cb);
1884 /* remove memmap entry */
1885 firmware_map_remove(start, start + size, "System RAM");
1886 memblock_free(start, size);
1887 memblock_remove(start, size);
1889 arch_remove_memory(start, size, NULL);
1891 try_offline_node(nid);
1895 EXPORT_SYMBOL_GPL(remove_memory);
1896 #endif /* CONFIG_MEMORY_HOTREMOVE */