2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/swap.h>
10 #include <linux/interrupt.h>
11 #include <linux/pagemap.h>
12 #include <linux/compiler.h>
13 #include <linux/export.h>
14 #include <linux/pagevec.h>
15 #include <linux/writeback.h>
16 #include <linux/slab.h>
17 #include <linux/sysctl.h>
18 #include <linux/cpu.h>
19 #include <linux/memory.h>
20 #include <linux/memremap.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/highmem.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
32 #include <linux/stop_machine.h>
33 #include <linux/hugetlb.h>
34 #include <linux/memblock.h>
35 #include <linux/bootmem.h>
36 #include <linux/compaction.h>
38 #include <asm/tlbflush.h>
43 * online_page_callback contains pointer to current page onlining function.
44 * Initially it is generic_online_page(). If it is required it could be
45 * changed by calling set_online_page_callback() for callback registration
46 * and restore_online_page_callback() for generic callback restore.
49 static void generic_online_page(struct page *page);
51 static online_page_callback_t online_page_callback = generic_online_page;
52 static DEFINE_MUTEX(online_page_callback_lock);
54 /* The same as the cpu_hotplug lock, but for memory hotplug. */
56 struct task_struct *active_writer;
57 struct mutex lock; /* Synchronizes accesses to refcount, */
59 * Also blocks the new readers during
60 * an ongoing mem hotplug operation.
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
68 .active_writer = NULL,
69 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
71 #ifdef CONFIG_DEBUG_LOCK_ALLOC
72 .dep_map = {.name = "mem_hotplug.lock" },
76 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
77 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
78 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
79 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
81 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
82 bool memhp_auto_online;
84 bool memhp_auto_online = true;
86 EXPORT_SYMBOL_GPL(memhp_auto_online);
88 static int __init setup_memhp_default_state(char *str)
90 if (!strcmp(str, "online"))
91 memhp_auto_online = true;
92 else if (!strcmp(str, "offline"))
93 memhp_auto_online = false;
97 __setup("memhp_default_state=", setup_memhp_default_state);
99 void get_online_mems(void)
102 if (mem_hotplug.active_writer == current)
104 memhp_lock_acquire_read();
105 mutex_lock(&mem_hotplug.lock);
106 mem_hotplug.refcount++;
107 mutex_unlock(&mem_hotplug.lock);
111 void put_online_mems(void)
113 if (mem_hotplug.active_writer == current)
115 mutex_lock(&mem_hotplug.lock);
117 if (WARN_ON(!mem_hotplug.refcount))
118 mem_hotplug.refcount++; /* try to fix things up */
120 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
121 wake_up_process(mem_hotplug.active_writer);
122 mutex_unlock(&mem_hotplug.lock);
123 memhp_lock_release();
127 void mem_hotplug_begin(void)
129 assert_held_device_hotplug();
131 mem_hotplug.active_writer = current;
133 memhp_lock_acquire();
135 mutex_lock(&mem_hotplug.lock);
136 if (likely(!mem_hotplug.refcount))
138 __set_current_state(TASK_UNINTERRUPTIBLE);
139 mutex_unlock(&mem_hotplug.lock);
144 void mem_hotplug_done(void)
146 mem_hotplug.active_writer = NULL;
147 mutex_unlock(&mem_hotplug.lock);
148 memhp_lock_release();
151 /* add this memory to iomem resource */
152 static struct resource *register_memory_resource(u64 start, u64 size)
154 struct resource *res;
155 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
157 return ERR_PTR(-ENOMEM);
159 res->name = "System RAM";
161 res->end = start + size - 1;
162 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
163 if (request_resource(&iomem_resource, res) < 0) {
164 pr_debug("System RAM resource %pR cannot be added\n", res);
166 return ERR_PTR(-EEXIST);
171 static void release_memory_resource(struct resource *res)
175 release_resource(res);
180 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
181 void get_page_bootmem(unsigned long info, struct page *page,
184 page->freelist = (void *)type;
185 SetPagePrivate(page);
186 set_page_private(page, info);
190 void put_page_bootmem(struct page *page)
194 type = (unsigned long) page->freelist;
195 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
196 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
198 if (page_ref_dec_return(page) == 1) {
199 page->freelist = NULL;
200 ClearPagePrivate(page);
201 set_page_private(page, 0);
202 INIT_LIST_HEAD(&page->lru);
203 free_reserved_page(page);
207 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
208 #ifndef CONFIG_SPARSEMEM_VMEMMAP
209 static void register_page_bootmem_info_section(unsigned long start_pfn)
211 unsigned long *usemap, mapsize, section_nr, i;
212 struct mem_section *ms;
213 struct page *page, *memmap;
215 section_nr = pfn_to_section_nr(start_pfn);
216 ms = __nr_to_section(section_nr);
218 /* Get section's memmap address */
219 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
222 * Get page for the memmap's phys address
223 * XXX: need more consideration for sparse_vmemmap...
225 page = virt_to_page(memmap);
226 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
227 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
229 /* remember memmap's page */
230 for (i = 0; i < mapsize; i++, page++)
231 get_page_bootmem(section_nr, page, SECTION_INFO);
233 usemap = __nr_to_section(section_nr)->pageblock_flags;
234 page = virt_to_page(usemap);
236 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
238 for (i = 0; i < mapsize; i++, page++)
239 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
242 #else /* CONFIG_SPARSEMEM_VMEMMAP */
243 static void register_page_bootmem_info_section(unsigned long start_pfn)
245 unsigned long *usemap, mapsize, section_nr, i;
246 struct mem_section *ms;
247 struct page *page, *memmap;
249 if (!pfn_valid(start_pfn))
252 section_nr = pfn_to_section_nr(start_pfn);
253 ms = __nr_to_section(section_nr);
255 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
257 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
259 usemap = __nr_to_section(section_nr)->pageblock_flags;
260 page = virt_to_page(usemap);
262 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
264 for (i = 0; i < mapsize; i++, page++)
265 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
267 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
269 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
271 unsigned long i, pfn, end_pfn, nr_pages;
272 int node = pgdat->node_id;
275 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
276 page = virt_to_page(pgdat);
278 for (i = 0; i < nr_pages; i++, page++)
279 get_page_bootmem(node, page, NODE_INFO);
281 pfn = pgdat->node_start_pfn;
282 end_pfn = pgdat_end_pfn(pgdat);
284 /* register section info */
285 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
287 * Some platforms can assign the same pfn to multiple nodes - on
288 * node0 as well as nodeN. To avoid registering a pfn against
289 * multiple nodes we check that this pfn does not already
290 * reside in some other nodes.
292 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
293 register_page_bootmem_info_section(pfn);
296 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
298 static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
299 unsigned long end_pfn)
301 unsigned long old_zone_end_pfn;
303 zone_span_writelock(zone);
305 old_zone_end_pfn = zone_end_pfn(zone);
306 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
307 zone->zone_start_pfn = start_pfn;
309 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
310 zone->zone_start_pfn;
312 zone_span_writeunlock(zone);
315 static void resize_zone(struct zone *zone, unsigned long start_pfn,
316 unsigned long end_pfn)
318 zone_span_writelock(zone);
320 if (end_pfn - start_pfn) {
321 zone->zone_start_pfn = start_pfn;
322 zone->spanned_pages = end_pfn - start_pfn;
325 * make it consist as free_area_init_core(),
326 * if spanned_pages = 0, then keep start_pfn = 0
328 zone->zone_start_pfn = 0;
329 zone->spanned_pages = 0;
332 zone_span_writeunlock(zone);
335 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
336 unsigned long end_pfn)
338 enum zone_type zid = zone_idx(zone);
339 int nid = zone->zone_pgdat->node_id;
342 for (pfn = start_pfn; pfn < end_pfn; pfn++)
343 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
346 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
347 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
348 static int __ref ensure_zone_is_initialized(struct zone *zone,
349 unsigned long start_pfn, unsigned long num_pages)
351 if (!zone_is_initialized(zone))
352 return init_currently_empty_zone(zone, start_pfn, num_pages);
357 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
358 unsigned long start_pfn, unsigned long end_pfn)
362 unsigned long z1_start_pfn;
364 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
368 pgdat_resize_lock(z1->zone_pgdat, &flags);
370 /* can't move pfns which are higher than @z2 */
371 if (end_pfn > zone_end_pfn(z2))
373 /* the move out part must be at the left most of @z2 */
374 if (start_pfn > z2->zone_start_pfn)
376 /* must included/overlap */
377 if (end_pfn <= z2->zone_start_pfn)
380 /* use start_pfn for z1's start_pfn if z1 is empty */
381 if (!zone_is_empty(z1))
382 z1_start_pfn = z1->zone_start_pfn;
384 z1_start_pfn = start_pfn;
386 resize_zone(z1, z1_start_pfn, end_pfn);
387 resize_zone(z2, end_pfn, zone_end_pfn(z2));
389 pgdat_resize_unlock(z1->zone_pgdat, &flags);
391 fix_zone_id(z1, start_pfn, end_pfn);
395 pgdat_resize_unlock(z1->zone_pgdat, &flags);
399 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
400 unsigned long start_pfn, unsigned long end_pfn)
404 unsigned long z2_end_pfn;
406 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
410 pgdat_resize_lock(z1->zone_pgdat, &flags);
412 /* can't move pfns which are lower than @z1 */
413 if (z1->zone_start_pfn > start_pfn)
415 /* the move out part mast at the right most of @z1 */
416 if (zone_end_pfn(z1) > end_pfn)
418 /* must included/overlap */
419 if (start_pfn >= zone_end_pfn(z1))
422 /* use end_pfn for z2's end_pfn if z2 is empty */
423 if (!zone_is_empty(z2))
424 z2_end_pfn = zone_end_pfn(z2);
426 z2_end_pfn = end_pfn;
428 resize_zone(z1, z1->zone_start_pfn, start_pfn);
429 resize_zone(z2, start_pfn, z2_end_pfn);
431 pgdat_resize_unlock(z1->zone_pgdat, &flags);
433 fix_zone_id(z2, start_pfn, end_pfn);
437 pgdat_resize_unlock(z1->zone_pgdat, &flags);
441 static struct zone * __meminit move_pfn_range(int zone_shift,
442 unsigned long start_pfn, unsigned long end_pfn)
444 struct zone *zone = page_zone(pfn_to_page(start_pfn));
448 ret = move_pfn_range_left(zone + zone_shift, zone,
451 ret = move_pfn_range_right(zone, zone + zone_shift,
457 return zone + zone_shift;
460 static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
461 unsigned long end_pfn)
463 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
465 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
466 pgdat->node_start_pfn = start_pfn;
468 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
469 pgdat->node_start_pfn;
472 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
474 struct pglist_data *pgdat = zone->zone_pgdat;
475 int nr_pages = PAGES_PER_SECTION;
476 int nid = pgdat->node_id;
478 unsigned long flags, pfn;
481 zone_type = zone - pgdat->node_zones;
482 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
486 pgdat_resize_lock(zone->zone_pgdat, &flags);
487 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
488 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
489 phys_start_pfn + nr_pages);
490 pgdat_resize_unlock(zone->zone_pgdat, &flags);
491 memmap_init_zone(nr_pages, nid, zone_type,
492 phys_start_pfn, MEMMAP_HOTPLUG);
494 /* online_page_range is called later and expects pages reserved */
495 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
499 SetPageReserved(pfn_to_page(pfn));
504 static int __meminit __add_section(int nid, struct zone *zone,
505 unsigned long phys_start_pfn)
509 if (pfn_valid(phys_start_pfn))
512 ret = sparse_add_one_section(zone, phys_start_pfn);
517 ret = __add_zone(zone, phys_start_pfn);
522 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
526 * Reasonably generic function for adding memory. It is
527 * expected that archs that support memory hotplug will
528 * call this function after deciding the zone to which to
531 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
532 unsigned long nr_pages)
536 int start_sec, end_sec;
537 struct vmem_altmap *altmap;
539 clear_zone_contiguous(zone);
541 /* during initialize mem_map, align hot-added range to section */
542 start_sec = pfn_to_section_nr(phys_start_pfn);
543 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
545 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
548 * Validate altmap is within bounds of the total request
550 if (altmap->base_pfn != phys_start_pfn
551 || vmem_altmap_offset(altmap) > nr_pages) {
552 pr_warn_once("memory add fail, invalid altmap\n");
559 for (i = start_sec; i <= end_sec; i++) {
560 err = __add_section(nid, zone, section_nr_to_pfn(i));
563 * EEXIST is finally dealt with by ioresource collision
564 * check. see add_memory() => register_memory_resource()
565 * Warning will be printed if there is collision.
567 if (err && (err != -EEXIST))
571 vmemmap_populate_print_last();
573 set_zone_contiguous(zone);
576 EXPORT_SYMBOL_GPL(__add_pages);
578 #ifdef CONFIG_MEMORY_HOTREMOVE
579 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
580 static int find_smallest_section_pfn(int nid, struct zone *zone,
581 unsigned long start_pfn,
582 unsigned long end_pfn)
584 struct mem_section *ms;
586 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
587 ms = __pfn_to_section(start_pfn);
589 if (unlikely(!valid_section(ms)))
592 if (unlikely(pfn_to_nid(start_pfn) != nid))
595 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
604 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
605 static int find_biggest_section_pfn(int nid, struct zone *zone,
606 unsigned long start_pfn,
607 unsigned long end_pfn)
609 struct mem_section *ms;
612 /* pfn is the end pfn of a memory section. */
614 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
615 ms = __pfn_to_section(pfn);
617 if (unlikely(!valid_section(ms)))
620 if (unlikely(pfn_to_nid(pfn) != nid))
623 if (zone && zone != page_zone(pfn_to_page(pfn)))
632 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
633 unsigned long end_pfn)
635 unsigned long zone_start_pfn = zone->zone_start_pfn;
636 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
637 unsigned long zone_end_pfn = z;
639 struct mem_section *ms;
640 int nid = zone_to_nid(zone);
642 zone_span_writelock(zone);
643 if (zone_start_pfn == start_pfn) {
645 * If the section is smallest section in the zone, it need
646 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
647 * In this case, we find second smallest valid mem_section
648 * for shrinking zone.
650 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
653 zone->zone_start_pfn = pfn;
654 zone->spanned_pages = zone_end_pfn - pfn;
656 } else if (zone_end_pfn == end_pfn) {
658 * If the section is biggest section in the zone, it need
659 * shrink zone->spanned_pages.
660 * In this case, we find second biggest valid mem_section for
663 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
666 zone->spanned_pages = pfn - zone_start_pfn + 1;
670 * The section is not biggest or smallest mem_section in the zone, it
671 * only creates a hole in the zone. So in this case, we need not
672 * change the zone. But perhaps, the zone has only hole data. Thus
673 * it check the zone has only hole or not.
675 pfn = zone_start_pfn;
676 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
677 ms = __pfn_to_section(pfn);
679 if (unlikely(!valid_section(ms)))
682 if (page_zone(pfn_to_page(pfn)) != zone)
685 /* If the section is current section, it continues the loop */
686 if (start_pfn == pfn)
689 /* If we find valid section, we have nothing to do */
690 zone_span_writeunlock(zone);
694 /* The zone has no valid section */
695 zone->zone_start_pfn = 0;
696 zone->spanned_pages = 0;
697 zone_span_writeunlock(zone);
700 static void shrink_pgdat_span(struct pglist_data *pgdat,
701 unsigned long start_pfn, unsigned long end_pfn)
703 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
704 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
705 unsigned long pgdat_end_pfn = p;
707 struct mem_section *ms;
708 int nid = pgdat->node_id;
710 if (pgdat_start_pfn == start_pfn) {
712 * If the section is smallest section in the pgdat, it need
713 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
714 * In this case, we find second smallest valid mem_section
715 * for shrinking zone.
717 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
720 pgdat->node_start_pfn = pfn;
721 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
723 } else if (pgdat_end_pfn == end_pfn) {
725 * If the section is biggest section in the pgdat, it need
726 * shrink pgdat->node_spanned_pages.
727 * In this case, we find second biggest valid mem_section for
730 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
733 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
737 * If the section is not biggest or smallest mem_section in the pgdat,
738 * it only creates a hole in the pgdat. So in this case, we need not
740 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
741 * has only hole or not.
743 pfn = pgdat_start_pfn;
744 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
745 ms = __pfn_to_section(pfn);
747 if (unlikely(!valid_section(ms)))
750 if (pfn_to_nid(pfn) != nid)
753 /* If the section is current section, it continues the loop */
754 if (start_pfn == pfn)
757 /* If we find valid section, we have nothing to do */
761 /* The pgdat has no valid section */
762 pgdat->node_start_pfn = 0;
763 pgdat->node_spanned_pages = 0;
766 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
768 struct pglist_data *pgdat = zone->zone_pgdat;
769 int nr_pages = PAGES_PER_SECTION;
773 zone_type = zone - pgdat->node_zones;
775 pgdat_resize_lock(zone->zone_pgdat, &flags);
776 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
777 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
778 pgdat_resize_unlock(zone->zone_pgdat, &flags);
781 static int __remove_section(struct zone *zone, struct mem_section *ms,
782 unsigned long map_offset)
784 unsigned long start_pfn;
788 if (!valid_section(ms))
791 ret = unregister_memory_section(ms);
795 scn_nr = __section_nr(ms);
796 start_pfn = section_nr_to_pfn(scn_nr);
797 __remove_zone(zone, start_pfn);
799 sparse_remove_one_section(zone, ms, map_offset);
804 * __remove_pages() - remove sections of pages from a zone
805 * @zone: zone from which pages need to be removed
806 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
807 * @nr_pages: number of pages to remove (must be multiple of section size)
809 * Generic helper function to remove section mappings and sysfs entries
810 * for the section of the memory we are removing. Caller needs to make
811 * sure that pages are marked reserved and zones are adjust properly by
812 * calling offline_pages().
814 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
815 unsigned long nr_pages)
818 unsigned long map_offset = 0;
819 int sections_to_remove, ret = 0;
821 /* In the ZONE_DEVICE case device driver owns the memory region */
822 if (is_dev_zone(zone)) {
823 struct page *page = pfn_to_page(phys_start_pfn);
824 struct vmem_altmap *altmap;
826 altmap = to_vmem_altmap((unsigned long) page);
828 map_offset = vmem_altmap_offset(altmap);
830 resource_size_t start, size;
832 start = phys_start_pfn << PAGE_SHIFT;
833 size = nr_pages * PAGE_SIZE;
835 ret = release_mem_region_adjustable(&iomem_resource, start,
838 resource_size_t endres = start + size - 1;
840 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
841 &start, &endres, ret);
845 clear_zone_contiguous(zone);
848 * We can only remove entire sections
850 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
851 BUG_ON(nr_pages % PAGES_PER_SECTION);
853 sections_to_remove = nr_pages / PAGES_PER_SECTION;
854 for (i = 0; i < sections_to_remove; i++) {
855 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
857 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
863 set_zone_contiguous(zone);
867 #endif /* CONFIG_MEMORY_HOTREMOVE */
869 int set_online_page_callback(online_page_callback_t callback)
874 mutex_lock(&online_page_callback_lock);
876 if (online_page_callback == generic_online_page) {
877 online_page_callback = callback;
881 mutex_unlock(&online_page_callback_lock);
886 EXPORT_SYMBOL_GPL(set_online_page_callback);
888 int restore_online_page_callback(online_page_callback_t callback)
893 mutex_lock(&online_page_callback_lock);
895 if (online_page_callback == callback) {
896 online_page_callback = generic_online_page;
900 mutex_unlock(&online_page_callback_lock);
905 EXPORT_SYMBOL_GPL(restore_online_page_callback);
907 void __online_page_set_limits(struct page *page)
910 EXPORT_SYMBOL_GPL(__online_page_set_limits);
912 void __online_page_increment_counters(struct page *page)
914 adjust_managed_page_count(page, 1);
916 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
918 void __online_page_free(struct page *page)
920 __free_reserved_page(page);
922 EXPORT_SYMBOL_GPL(__online_page_free);
924 static void generic_online_page(struct page *page)
926 __online_page_set_limits(page);
927 __online_page_increment_counters(page);
928 __online_page_free(page);
931 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
935 unsigned long onlined_pages = *(unsigned long *)arg;
937 if (PageReserved(pfn_to_page(start_pfn)))
938 for (i = 0; i < nr_pages; i++) {
939 page = pfn_to_page(start_pfn + i);
940 (*online_page_callback)(page);
943 *(unsigned long *)arg = onlined_pages;
947 #ifdef CONFIG_MOVABLE_NODE
949 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
952 static bool can_online_high_movable(struct zone *zone)
956 #else /* CONFIG_MOVABLE_NODE */
957 /* ensure every online node has NORMAL memory */
958 static bool can_online_high_movable(struct zone *zone)
960 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
962 #endif /* CONFIG_MOVABLE_NODE */
964 /* check which state of node_states will be changed when online memory */
965 static void node_states_check_changes_online(unsigned long nr_pages,
966 struct zone *zone, struct memory_notify *arg)
968 int nid = zone_to_nid(zone);
969 enum zone_type zone_last = ZONE_NORMAL;
972 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
973 * contains nodes which have zones of 0...ZONE_NORMAL,
974 * set zone_last to ZONE_NORMAL.
976 * If we don't have HIGHMEM nor movable node,
977 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
978 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
980 if (N_MEMORY == N_NORMAL_MEMORY)
981 zone_last = ZONE_MOVABLE;
984 * if the memory to be online is in a zone of 0...zone_last, and
985 * the zones of 0...zone_last don't have memory before online, we will
986 * need to set the node to node_states[N_NORMAL_MEMORY] after
987 * the memory is online.
989 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
990 arg->status_change_nid_normal = nid;
992 arg->status_change_nid_normal = -1;
994 #ifdef CONFIG_HIGHMEM
996 * If we have movable node, node_states[N_HIGH_MEMORY]
997 * contains nodes which have zones of 0...ZONE_HIGHMEM,
998 * set zone_last to ZONE_HIGHMEM.
1000 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1001 * contains nodes which have zones of 0...ZONE_MOVABLE,
1002 * set zone_last to ZONE_MOVABLE.
1004 zone_last = ZONE_HIGHMEM;
1005 if (N_MEMORY == N_HIGH_MEMORY)
1006 zone_last = ZONE_MOVABLE;
1008 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
1009 arg->status_change_nid_high = nid;
1011 arg->status_change_nid_high = -1;
1013 arg->status_change_nid_high = arg->status_change_nid_normal;
1017 * if the node don't have memory befor online, we will need to
1018 * set the node to node_states[N_MEMORY] after the memory
1021 if (!node_state(nid, N_MEMORY))
1022 arg->status_change_nid = nid;
1024 arg->status_change_nid = -1;
1027 static void node_states_set_node(int node, struct memory_notify *arg)
1029 if (arg->status_change_nid_normal >= 0)
1030 node_set_state(node, N_NORMAL_MEMORY);
1032 if (arg->status_change_nid_high >= 0)
1033 node_set_state(node, N_HIGH_MEMORY);
1035 node_set_state(node, N_MEMORY);
1038 bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
1039 enum zone_type target, int *zone_shift)
1041 struct zone *zone = page_zone(pfn_to_page(pfn));
1042 enum zone_type idx = zone_idx(zone);
1048 /* pages must be at end of current zone */
1049 if (pfn + nr_pages != zone_end_pfn(zone))
1052 /* no zones in use between current zone and target */
1053 for (i = idx + 1; i < target; i++)
1054 if (zone_is_initialized(zone - idx + i))
1059 /* pages must be at beginning of current zone */
1060 if (pfn != zone->zone_start_pfn)
1063 /* no zones in use between current zone and target */
1064 for (i = target + 1; i < idx; i++)
1065 if (zone_is_initialized(zone - idx + i))
1069 *zone_shift = target - idx;
1073 /* Must be protected by mem_hotplug_begin() */
1074 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
1076 unsigned long flags;
1077 unsigned long onlined_pages = 0;
1079 int need_zonelists_rebuild = 0;
1082 struct memory_notify arg;
1086 * This doesn't need a lock to do pfn_to_page().
1087 * The section can't be removed here because of the
1088 * memory_block->state_mutex.
1090 zone = page_zone(pfn_to_page(pfn));
1092 if ((zone_idx(zone) > ZONE_NORMAL ||
1093 online_type == MMOP_ONLINE_MOVABLE) &&
1094 !can_online_high_movable(zone))
1097 if (online_type == MMOP_ONLINE_KERNEL) {
1098 if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
1100 } else if (online_type == MMOP_ONLINE_MOVABLE) {
1101 if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
1105 zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
1109 arg.start_pfn = pfn;
1110 arg.nr_pages = nr_pages;
1111 node_states_check_changes_online(nr_pages, zone, &arg);
1113 nid = zone_to_nid(zone);
1115 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1116 ret = notifier_to_errno(ret);
1118 goto failed_addition;
1121 * If this zone is not populated, then it is not in zonelist.
1122 * This means the page allocator ignores this zone.
1123 * So, zonelist must be updated after online.
1125 mutex_lock(&zonelists_mutex);
1126 if (!populated_zone(zone)) {
1127 need_zonelists_rebuild = 1;
1128 build_all_zonelists(NULL, zone);
1131 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1132 online_pages_range);
1134 if (need_zonelists_rebuild)
1135 zone_pcp_reset(zone);
1136 mutex_unlock(&zonelists_mutex);
1137 goto failed_addition;
1140 zone->present_pages += onlined_pages;
1142 pgdat_resize_lock(zone->zone_pgdat, &flags);
1143 zone->zone_pgdat->node_present_pages += onlined_pages;
1144 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1146 if (onlined_pages) {
1147 node_states_set_node(nid, &arg);
1148 if (need_zonelists_rebuild)
1149 build_all_zonelists(NULL, NULL);
1151 zone_pcp_update(zone);
1154 mutex_unlock(&zonelists_mutex);
1156 init_per_zone_wmark_min();
1158 if (onlined_pages) {
1163 vm_total_pages = nr_free_pagecache_pages();
1165 writeback_set_ratelimit();
1168 memory_notify(MEM_ONLINE, &arg);
1172 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1173 (unsigned long long) pfn << PAGE_SHIFT,
1174 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1175 memory_notify(MEM_CANCEL_ONLINE, &arg);
1178 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1180 static void reset_node_present_pages(pg_data_t *pgdat)
1184 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1185 z->present_pages = 0;
1187 pgdat->node_present_pages = 0;
1190 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1191 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1193 struct pglist_data *pgdat;
1194 unsigned long zones_size[MAX_NR_ZONES] = {0};
1195 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1196 unsigned long start_pfn = PFN_DOWN(start);
1198 pgdat = NODE_DATA(nid);
1200 pgdat = arch_alloc_nodedata(nid);
1204 arch_refresh_nodedata(nid, pgdat);
1206 /* Reset the nr_zones, order and classzone_idx before reuse */
1207 pgdat->nr_zones = 0;
1208 pgdat->kswapd_order = 0;
1209 pgdat->kswapd_classzone_idx = 0;
1212 /* we can use NODE_DATA(nid) from here */
1214 /* init node's zones as empty zones, we don't have any present pages.*/
1215 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1216 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
1219 * The node we allocated has no zone fallback lists. For avoiding
1220 * to access not-initialized zonelist, build here.
1222 mutex_lock(&zonelists_mutex);
1223 build_all_zonelists(pgdat, NULL);
1224 mutex_unlock(&zonelists_mutex);
1227 * zone->managed_pages is set to an approximate value in
1228 * free_area_init_core(), which will cause
1229 * /sys/device/system/node/nodeX/meminfo has wrong data.
1230 * So reset it to 0 before any memory is onlined.
1232 reset_node_managed_pages(pgdat);
1235 * When memory is hot-added, all the memory is in offline state. So
1236 * clear all zones' present_pages because they will be updated in
1237 * online_pages() and offline_pages().
1239 reset_node_present_pages(pgdat);
1244 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1246 arch_refresh_nodedata(nid, NULL);
1247 free_percpu(pgdat->per_cpu_nodestats);
1248 arch_free_nodedata(pgdat);
1254 * try_online_node - online a node if offlined
1256 * called by cpu_up() to online a node without onlined memory.
1258 int try_online_node(int nid)
1263 if (node_online(nid))
1266 mem_hotplug_begin();
1267 pgdat = hotadd_new_pgdat(nid, 0);
1269 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1273 node_set_online(nid);
1274 ret = register_one_node(nid);
1277 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1278 mutex_lock(&zonelists_mutex);
1279 build_all_zonelists(NULL, NULL);
1280 mutex_unlock(&zonelists_mutex);
1288 static int check_hotplug_memory_range(u64 start, u64 size)
1290 u64 start_pfn = PFN_DOWN(start);
1291 u64 nr_pages = size >> PAGE_SHIFT;
1293 /* Memory range must be aligned with section */
1294 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1295 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1296 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1297 (unsigned long long)start,
1298 (unsigned long long)size);
1306 * If movable zone has already been setup, newly added memory should be check.
1307 * If its address is higher than movable zone, it should be added as movable.
1308 * Without this check, movable zone may overlap with other zone.
1310 static int should_add_memory_movable(int nid, u64 start, u64 size)
1312 unsigned long start_pfn = start >> PAGE_SHIFT;
1313 pg_data_t *pgdat = NODE_DATA(nid);
1314 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1316 if (zone_is_empty(movable_zone))
1319 if (movable_zone->zone_start_pfn <= start_pfn)
1325 int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1328 #ifdef CONFIG_ZONE_DEVICE
1332 if (should_add_memory_movable(nid, start, size))
1333 return ZONE_MOVABLE;
1335 return zone_default;
1338 static int online_memory_block(struct memory_block *mem, void *arg)
1340 return device_online(&mem->dev);
1343 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1344 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1347 pg_data_t *pgdat = NULL;
1353 size = resource_size(res);
1355 ret = check_hotplug_memory_range(start, size);
1359 { /* Stupid hack to suppress address-never-null warning */
1360 void *p = NODE_DATA(nid);
1364 mem_hotplug_begin();
1367 * Add new range to memblock so that when hotadd_new_pgdat() is called
1368 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1369 * this new range and calculate total pages correctly. The range will
1370 * be removed at hot-remove time.
1372 memblock_add_node(start, size, nid);
1374 new_node = !node_online(nid);
1376 pgdat = hotadd_new_pgdat(nid, start);
1382 /* call arch's memory hotadd */
1383 ret = arch_add_memory(nid, start, size, false);
1388 /* we online node here. we can't roll back from here. */
1389 node_set_online(nid);
1392 ret = register_one_node(nid);
1394 * If sysfs file of new node can't create, cpu on the node
1395 * can't be hot-added. There is no rollback way now.
1396 * So, check by BUG_ON() to catch it reluctantly..
1401 /* create new memmap entry */
1402 firmware_map_add_hotplug(start, start + size, "System RAM");
1404 /* online pages if requested */
1406 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1407 NULL, online_memory_block);
1412 /* rollback pgdat allocation and others */
1414 rollback_node_hotadd(nid, pgdat);
1415 memblock_remove(start, size);
1421 EXPORT_SYMBOL_GPL(add_memory_resource);
1423 int __ref add_memory(int nid, u64 start, u64 size)
1425 struct resource *res;
1428 res = register_memory_resource(start, size);
1430 return PTR_ERR(res);
1432 ret = add_memory_resource(nid, res, memhp_auto_online);
1434 release_memory_resource(res);
1437 EXPORT_SYMBOL_GPL(add_memory);
1439 #ifdef CONFIG_MEMORY_HOTREMOVE
1441 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1442 * set and the size of the free page is given by page_order(). Using this,
1443 * the function determines if the pageblock contains only free pages.
1444 * Due to buddy contraints, a free page at least the size of a pageblock will
1445 * be located at the start of the pageblock
1447 static inline int pageblock_free(struct page *page)
1449 return PageBuddy(page) && page_order(page) >= pageblock_order;
1452 /* Return the start of the next active pageblock after a given page */
1453 static struct page *next_active_pageblock(struct page *page)
1455 /* Ensure the starting page is pageblock-aligned */
1456 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1458 /* If the entire pageblock is free, move to the end of free page */
1459 if (pageblock_free(page)) {
1461 /* be careful. we don't have locks, page_order can be changed.*/
1462 order = page_order(page);
1463 if ((order < MAX_ORDER) && (order >= pageblock_order))
1464 return page + (1 << order);
1467 return page + pageblock_nr_pages;
1470 /* Checks if this range of memory is likely to be hot-removable. */
1471 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1473 struct page *page = pfn_to_page(start_pfn);
1474 struct page *end_page = page + nr_pages;
1476 /* Check the starting page of each pageblock within the range */
1477 for (; page < end_page; page = next_active_pageblock(page)) {
1478 if (!is_pageblock_removable_nolock(page))
1483 /* All pageblocks in the memory block are likely to be hot-removable */
1488 * Confirm all pages in a range [start, end) belong to the same zone.
1489 * When true, return its valid [start, end).
1491 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1492 unsigned long *valid_start, unsigned long *valid_end)
1494 unsigned long pfn, sec_end_pfn;
1495 unsigned long start, end;
1496 struct zone *zone = NULL;
1499 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1501 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1502 /* Make sure the memory section is present first */
1503 if (!present_section_nr(pfn_to_section_nr(pfn)))
1505 for (; pfn < sec_end_pfn && pfn < end_pfn;
1506 pfn += MAX_ORDER_NR_PAGES) {
1508 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1509 while ((i < MAX_ORDER_NR_PAGES) &&
1510 !pfn_valid_within(pfn + i))
1512 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1514 page = pfn_to_page(pfn + i);
1515 if (zone && page_zone(page) != zone)
1519 zone = page_zone(page);
1520 end = pfn + MAX_ORDER_NR_PAGES;
1525 *valid_start = start;
1526 *valid_end = min(end, end_pfn);
1534 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1535 * non-lru movable pages and hugepages). We scan pfn because it's much
1536 * easier than scanning over linked list. This function returns the pfn
1537 * of the first found movable page if it's found, otherwise 0.
1539 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1543 for (pfn = start; pfn < end; pfn++) {
1544 if (pfn_valid(pfn)) {
1545 page = pfn_to_page(pfn);
1548 if (__PageMovable(page))
1550 if (PageHuge(page)) {
1551 if (page_huge_active(page))
1554 pfn = round_up(pfn + 1,
1555 1 << compound_order(page)) - 1;
1562 static struct page *new_node_page(struct page *page, unsigned long private,
1565 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1566 int nid = page_to_nid(page);
1567 nodemask_t nmask = node_states[N_MEMORY];
1568 struct page *new_page = NULL;
1571 * TODO: allocate a destination hugepage from a nearest neighbor node,
1572 * accordance with memory policy of the user process if possible. For
1573 * now as a simple work-around, we use the next node for destination.
1576 return alloc_huge_page_node(page_hstate(compound_head(page)),
1577 next_node_in(nid, nmask));
1579 node_clear(nid, nmask);
1581 if (PageHighMem(page)
1582 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1583 gfp_mask |= __GFP_HIGHMEM;
1585 if (!nodes_empty(nmask))
1586 new_page = __alloc_pages_nodemask(gfp_mask, 0,
1587 node_zonelist(nid, gfp_mask), &nmask);
1589 new_page = __alloc_pages(gfp_mask, 0,
1590 node_zonelist(nid, gfp_mask));
1595 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1597 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1601 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1602 int not_managed = 0;
1606 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1607 if (!pfn_valid(pfn))
1609 page = pfn_to_page(pfn);
1611 if (PageHuge(page)) {
1612 struct page *head = compound_head(page);
1613 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1614 if (compound_order(head) > PFN_SECTION_SHIFT) {
1618 if (isolate_huge_page(page, &source))
1619 move_pages -= 1 << compound_order(head);
1623 if (!get_page_unless_zero(page))
1626 * We can skip free pages. And we can deal with pages on
1627 * LRU and non-lru movable pages.
1630 ret = isolate_lru_page(page);
1632 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1633 if (!ret) { /* Success */
1635 list_add_tail(&page->lru, &source);
1637 if (!__PageMovable(page))
1638 inc_node_page_state(page, NR_ISOLATED_ANON +
1639 page_is_file_cache(page));
1642 #ifdef CONFIG_DEBUG_VM
1643 pr_alert("failed to isolate pfn %lx\n", pfn);
1644 dump_page(page, "isolation failed");
1647 /* Because we don't have big zone->lock. we should
1648 check this again here. */
1649 if (page_count(page)) {
1656 if (!list_empty(&source)) {
1658 putback_movable_pages(&source);
1662 /* Allocate a new page from the nearest neighbor node */
1663 ret = migrate_pages(&source, new_node_page, NULL, 0,
1664 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1666 putback_movable_pages(&source);
1673 * remove from free_area[] and mark all as Reserved.
1676 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1679 __offline_isolated_pages(start, start + nr_pages);
1684 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1686 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1687 offline_isolated_pages_cb);
1691 * Check all pages in range, recoreded as memory resource, are isolated.
1694 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1698 long offlined = *(long *)data;
1699 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1700 offlined = nr_pages;
1702 *(long *)data += offlined;
1707 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1712 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1713 check_pages_isolated_cb);
1715 offlined = (long)ret;
1719 #ifdef CONFIG_MOVABLE_NODE
1721 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1724 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1728 #else /* CONFIG_MOVABLE_NODE */
1729 /* ensure the node has NORMAL memory if it is still online */
1730 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1732 struct pglist_data *pgdat = zone->zone_pgdat;
1733 unsigned long present_pages = 0;
1736 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1737 present_pages += pgdat->node_zones[zt].present_pages;
1739 if (present_pages > nr_pages)
1743 for (; zt <= ZONE_MOVABLE; zt++)
1744 present_pages += pgdat->node_zones[zt].present_pages;
1747 * we can't offline the last normal memory until all
1748 * higher memory is offlined.
1750 return present_pages == 0;
1752 #endif /* CONFIG_MOVABLE_NODE */
1754 static int __init cmdline_parse_movable_node(char *p)
1756 #ifdef CONFIG_MOVABLE_NODE
1757 movable_node_enabled = true;
1759 pr_warn("movable_node option not supported\n");
1763 early_param("movable_node", cmdline_parse_movable_node);
1765 /* check which state of node_states will be changed when offline memory */
1766 static void node_states_check_changes_offline(unsigned long nr_pages,
1767 struct zone *zone, struct memory_notify *arg)
1769 struct pglist_data *pgdat = zone->zone_pgdat;
1770 unsigned long present_pages = 0;
1771 enum zone_type zt, zone_last = ZONE_NORMAL;
1774 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1775 * contains nodes which have zones of 0...ZONE_NORMAL,
1776 * set zone_last to ZONE_NORMAL.
1778 * If we don't have HIGHMEM nor movable node,
1779 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1780 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1782 if (N_MEMORY == N_NORMAL_MEMORY)
1783 zone_last = ZONE_MOVABLE;
1786 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1787 * If the memory to be offline is in a zone of 0...zone_last,
1788 * and it is the last present memory, 0...zone_last will
1789 * become empty after offline , thus we can determind we will
1790 * need to clear the node from node_states[N_NORMAL_MEMORY].
1792 for (zt = 0; zt <= zone_last; zt++)
1793 present_pages += pgdat->node_zones[zt].present_pages;
1794 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1795 arg->status_change_nid_normal = zone_to_nid(zone);
1797 arg->status_change_nid_normal = -1;
1799 #ifdef CONFIG_HIGHMEM
1801 * If we have movable node, node_states[N_HIGH_MEMORY]
1802 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1803 * set zone_last to ZONE_HIGHMEM.
1805 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1806 * contains nodes which have zones of 0...ZONE_MOVABLE,
1807 * set zone_last to ZONE_MOVABLE.
1809 zone_last = ZONE_HIGHMEM;
1810 if (N_MEMORY == N_HIGH_MEMORY)
1811 zone_last = ZONE_MOVABLE;
1813 for (; zt <= zone_last; zt++)
1814 present_pages += pgdat->node_zones[zt].present_pages;
1815 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1816 arg->status_change_nid_high = zone_to_nid(zone);
1818 arg->status_change_nid_high = -1;
1820 arg->status_change_nid_high = arg->status_change_nid_normal;
1824 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1826 zone_last = ZONE_MOVABLE;
1829 * check whether node_states[N_HIGH_MEMORY] will be changed
1830 * If we try to offline the last present @nr_pages from the node,
1831 * we can determind we will need to clear the node from
1832 * node_states[N_HIGH_MEMORY].
1834 for (; zt <= zone_last; zt++)
1835 present_pages += pgdat->node_zones[zt].present_pages;
1836 if (nr_pages >= present_pages)
1837 arg->status_change_nid = zone_to_nid(zone);
1839 arg->status_change_nid = -1;
1842 static void node_states_clear_node(int node, struct memory_notify *arg)
1844 if (arg->status_change_nid_normal >= 0)
1845 node_clear_state(node, N_NORMAL_MEMORY);
1847 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1848 (arg->status_change_nid_high >= 0))
1849 node_clear_state(node, N_HIGH_MEMORY);
1851 if ((N_MEMORY != N_HIGH_MEMORY) &&
1852 (arg->status_change_nid >= 0))
1853 node_clear_state(node, N_MEMORY);
1856 static int __ref __offline_pages(unsigned long start_pfn,
1857 unsigned long end_pfn, unsigned long timeout)
1859 unsigned long pfn, nr_pages, expire;
1860 long offlined_pages;
1861 int ret, drain, retry_max, node;
1862 unsigned long flags;
1863 unsigned long valid_start, valid_end;
1865 struct memory_notify arg;
1867 /* at least, alignment against pageblock is necessary */
1868 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1870 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1872 /* This makes hotplug much easier...and readable.
1873 we assume this for now. .*/
1874 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1877 zone = page_zone(pfn_to_page(valid_start));
1878 node = zone_to_nid(zone);
1879 nr_pages = end_pfn - start_pfn;
1881 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1884 /* set above range as isolated */
1885 ret = start_isolate_page_range(start_pfn, end_pfn,
1886 MIGRATE_MOVABLE, true);
1890 arg.start_pfn = start_pfn;
1891 arg.nr_pages = nr_pages;
1892 node_states_check_changes_offline(nr_pages, zone, &arg);
1894 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1895 ret = notifier_to_errno(ret);
1897 goto failed_removal;
1900 expire = jiffies + timeout;
1904 /* start memory hot removal */
1906 if (time_after(jiffies, expire))
1907 goto failed_removal;
1909 if (signal_pending(current))
1910 goto failed_removal;
1913 lru_add_drain_all();
1915 drain_all_pages(zone);
1918 pfn = scan_movable_pages(start_pfn, end_pfn);
1919 if (pfn) { /* We have movable pages */
1920 ret = do_migrate_range(pfn, end_pfn);
1926 if (--retry_max == 0)
1927 goto failed_removal;
1933 /* drain all zone's lru pagevec, this is asynchronous... */
1934 lru_add_drain_all();
1936 /* drain pcp pages, this is synchronous. */
1937 drain_all_pages(zone);
1939 * dissolve free hugepages in the memory block before doing offlining
1940 * actually in order to make hugetlbfs's object counting consistent.
1942 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1944 goto failed_removal;
1946 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1947 if (offlined_pages < 0) {
1949 goto failed_removal;
1951 pr_info("Offlined Pages %ld\n", offlined_pages);
1952 /* Ok, all of our target is isolated.
1953 We cannot do rollback at this point. */
1954 offline_isolated_pages(start_pfn, end_pfn);
1955 /* reset pagetype flags and makes migrate type to be MOVABLE */
1956 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1957 /* removal success */
1958 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1959 zone->present_pages -= offlined_pages;
1961 pgdat_resize_lock(zone->zone_pgdat, &flags);
1962 zone->zone_pgdat->node_present_pages -= offlined_pages;
1963 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1965 init_per_zone_wmark_min();
1967 if (!populated_zone(zone)) {
1968 zone_pcp_reset(zone);
1969 mutex_lock(&zonelists_mutex);
1970 build_all_zonelists(NULL, NULL);
1971 mutex_unlock(&zonelists_mutex);
1973 zone_pcp_update(zone);
1975 node_states_clear_node(node, &arg);
1976 if (arg.status_change_nid >= 0) {
1978 kcompactd_stop(node);
1981 vm_total_pages = nr_free_pagecache_pages();
1982 writeback_set_ratelimit();
1984 memory_notify(MEM_OFFLINE, &arg);
1988 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1989 (unsigned long long) start_pfn << PAGE_SHIFT,
1990 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1991 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1992 /* pushback to free area */
1993 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1997 /* Must be protected by mem_hotplug_begin() */
1998 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
2000 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
2002 #endif /* CONFIG_MEMORY_HOTREMOVE */
2005 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2006 * @start_pfn: start pfn of the memory range
2007 * @end_pfn: end pfn of the memory range
2008 * @arg: argument passed to func
2009 * @func: callback for each memory section walked
2011 * This function walks through all present mem sections in range
2012 * [start_pfn, end_pfn) and call func on each mem section.
2014 * Returns the return value of func.
2016 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
2017 void *arg, int (*func)(struct memory_block *, void *))
2019 struct memory_block *mem = NULL;
2020 struct mem_section *section;
2021 unsigned long pfn, section_nr;
2024 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2025 section_nr = pfn_to_section_nr(pfn);
2026 if (!present_section_nr(section_nr))
2029 section = __nr_to_section(section_nr);
2030 /* same memblock? */
2032 if ((section_nr >= mem->start_section_nr) &&
2033 (section_nr <= mem->end_section_nr))
2036 mem = find_memory_block_hinted(section, mem);
2040 ret = func(mem, arg);
2042 kobject_put(&mem->dev.kobj);
2048 kobject_put(&mem->dev.kobj);
2053 #ifdef CONFIG_MEMORY_HOTREMOVE
2054 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
2056 int ret = !is_memblock_offlined(mem);
2058 if (unlikely(ret)) {
2059 phys_addr_t beginpa, endpa;
2061 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
2062 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
2063 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
2070 static int check_cpu_on_node(pg_data_t *pgdat)
2074 for_each_present_cpu(cpu) {
2075 if (cpu_to_node(cpu) == pgdat->node_id)
2077 * the cpu on this node isn't removed, and we can't
2078 * offline this node.
2086 static void unmap_cpu_on_node(pg_data_t *pgdat)
2088 #ifdef CONFIG_ACPI_NUMA
2091 for_each_possible_cpu(cpu)
2092 if (cpu_to_node(cpu) == pgdat->node_id)
2093 numa_clear_node(cpu);
2097 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
2101 ret = check_cpu_on_node(pgdat);
2106 * the node will be offlined when we come here, so we can clear
2107 * the cpu_to_node() now.
2110 unmap_cpu_on_node(pgdat);
2117 * Offline a node if all memory sections and cpus of the node are removed.
2119 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2120 * and online/offline operations before this call.
2122 void try_offline_node(int nid)
2124 pg_data_t *pgdat = NODE_DATA(nid);
2125 unsigned long start_pfn = pgdat->node_start_pfn;
2126 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
2129 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2130 unsigned long section_nr = pfn_to_section_nr(pfn);
2132 if (!present_section_nr(section_nr))
2135 if (pfn_to_nid(pfn) != nid)
2139 * some memory sections of this node are not removed, and we
2140 * can't offline node now.
2145 if (check_and_unmap_cpu_on_node(pgdat))
2149 * all memory/cpu of this node are removed, we can offline this
2152 node_set_offline(nid);
2153 unregister_one_node(nid);
2155 EXPORT_SYMBOL(try_offline_node);
2160 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2161 * and online/offline operations before this call, as required by
2162 * try_offline_node().
2164 void __ref remove_memory(int nid, u64 start, u64 size)
2168 BUG_ON(check_hotplug_memory_range(start, size));
2170 mem_hotplug_begin();
2173 * All memory blocks must be offlined before removing memory. Check
2174 * whether all memory blocks in question are offline and trigger a BUG()
2175 * if this is not the case.
2177 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2178 check_memblock_offlined_cb);
2182 /* remove memmap entry */
2183 firmware_map_remove(start, start + size, "System RAM");
2184 memblock_free(start, size);
2185 memblock_remove(start, size);
2187 arch_remove_memory(start, size);
2189 try_offline_node(nid);
2193 EXPORT_SYMBOL_GPL(remove_memory);
2194 #endif /* CONFIG_MEMORY_HOTREMOVE */