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/memory_hotplug.h>
21 #include <linux/highmem.h>
22 #include <linux/vmalloc.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
25 #include <linux/migrate.h>
26 #include <linux/page-isolation.h>
27 #include <linux/pfn.h>
28 #include <linux/suspend.h>
29 #include <linux/mm_inline.h>
30 #include <linux/firmware-map.h>
31 #include <linux/stop_machine.h>
32 #include <linux/hugetlb.h>
33 #include <linux/memblock.h>
34 #include <linux/bootmem.h>
35 #include <linux/compaction.h>
36 #include <linux/rmap.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 int 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 void get_online_mems(void)
84 if (mem_hotplug.active_writer == current)
86 memhp_lock_acquire_read();
87 mutex_lock(&mem_hotplug.lock);
88 mem_hotplug.refcount++;
89 mutex_unlock(&mem_hotplug.lock);
93 void put_online_mems(void)
95 if (mem_hotplug.active_writer == current)
97 mutex_lock(&mem_hotplug.lock);
99 if (WARN_ON(!mem_hotplug.refcount))
100 mem_hotplug.refcount++; /* try to fix things up */
102 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
103 wake_up_process(mem_hotplug.active_writer);
104 mutex_unlock(&mem_hotplug.lock);
105 memhp_lock_release();
109 void mem_hotplug_begin(void)
111 mem_hotplug.active_writer = current;
113 memhp_lock_acquire();
115 mutex_lock(&mem_hotplug.lock);
116 if (likely(!mem_hotplug.refcount))
118 __set_current_state(TASK_UNINTERRUPTIBLE);
119 mutex_unlock(&mem_hotplug.lock);
124 void mem_hotplug_done(void)
126 mem_hotplug.active_writer = NULL;
127 mutex_unlock(&mem_hotplug.lock);
128 memhp_lock_release();
131 /* add this memory to iomem resource */
132 static struct resource *register_memory_resource(u64 start, u64 size)
134 struct resource *res;
135 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
138 res->name = "System RAM";
140 res->end = start + size - 1;
141 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
142 if (request_resource(&iomem_resource, res) < 0) {
143 pr_debug("System RAM resource %pR cannot be added\n", res);
150 static void release_memory_resource(struct resource *res)
154 release_resource(res);
159 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
160 void get_page_bootmem(unsigned long info, struct page *page,
163 page->lru.next = (struct list_head *) type;
164 SetPagePrivate(page);
165 set_page_private(page, info);
166 atomic_inc(&page->_count);
169 void put_page_bootmem(struct page *page)
173 type = (unsigned long) page->lru.next;
174 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
175 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
177 if (atomic_dec_return(&page->_count) == 1) {
178 ClearPagePrivate(page);
179 set_page_private(page, 0);
180 INIT_LIST_HEAD(&page->lru);
181 free_reserved_page(page);
185 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
186 #ifndef CONFIG_SPARSEMEM_VMEMMAP
187 static void register_page_bootmem_info_section(unsigned long start_pfn)
189 unsigned long *usemap, mapsize, section_nr, i;
190 struct mem_section *ms;
191 struct page *page, *memmap;
193 section_nr = pfn_to_section_nr(start_pfn);
194 ms = __nr_to_section(section_nr);
196 /* Get section's memmap address */
197 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
200 * Get page for the memmap's phys address
201 * XXX: need more consideration for sparse_vmemmap...
203 page = virt_to_page(memmap);
204 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
205 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
207 /* remember memmap's page */
208 for (i = 0; i < mapsize; i++, page++)
209 get_page_bootmem(section_nr, page, SECTION_INFO);
211 usemap = __nr_to_section(section_nr)->pageblock_flags;
212 page = virt_to_page(usemap);
214 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
216 for (i = 0; i < mapsize; i++, page++)
217 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
220 #else /* CONFIG_SPARSEMEM_VMEMMAP */
221 static void register_page_bootmem_info_section(unsigned long start_pfn)
223 unsigned long *usemap, mapsize, section_nr, i;
224 struct mem_section *ms;
225 struct page *page, *memmap;
227 if (!pfn_valid(start_pfn))
230 section_nr = pfn_to_section_nr(start_pfn);
231 ms = __nr_to_section(section_nr);
233 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
235 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
237 usemap = __nr_to_section(section_nr)->pageblock_flags;
238 page = virt_to_page(usemap);
240 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
242 for (i = 0; i < mapsize; i++, page++)
243 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
245 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
247 void register_page_bootmem_info_node(struct pglist_data *pgdat)
249 unsigned long i, pfn, end_pfn, nr_pages;
250 int node = pgdat->node_id;
254 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
255 page = virt_to_page(pgdat);
257 for (i = 0; i < nr_pages; i++, page++)
258 get_page_bootmem(node, page, NODE_INFO);
260 zone = &pgdat->node_zones[0];
261 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
262 if (zone_is_initialized(zone)) {
263 nr_pages = zone->wait_table_hash_nr_entries
264 * sizeof(wait_queue_head_t);
265 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
266 page = virt_to_page(zone->wait_table);
268 for (i = 0; i < nr_pages; i++, page++)
269 get_page_bootmem(node, page, NODE_INFO);
273 pfn = pgdat->node_start_pfn;
274 end_pfn = pgdat_end_pfn(pgdat);
276 /* register section info */
277 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
279 * Some platforms can assign the same pfn to multiple nodes - on
280 * node0 as well as nodeN. To avoid registering a pfn against
281 * multiple nodes we check that this pfn does not already
282 * reside in some other nodes.
284 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
285 register_page_bootmem_info_section(pfn);
288 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
290 static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
291 unsigned long end_pfn)
293 unsigned long old_zone_end_pfn;
295 zone_span_writelock(zone);
297 old_zone_end_pfn = zone_end_pfn(zone);
298 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
299 zone->zone_start_pfn = start_pfn;
301 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
302 zone->zone_start_pfn;
304 zone_span_writeunlock(zone);
307 static void resize_zone(struct zone *zone, unsigned long start_pfn,
308 unsigned long end_pfn)
310 zone_span_writelock(zone);
312 if (end_pfn - start_pfn) {
313 zone->zone_start_pfn = start_pfn;
314 zone->spanned_pages = end_pfn - start_pfn;
317 * make it consist as free_area_init_core(),
318 * if spanned_pages = 0, then keep start_pfn = 0
320 zone->zone_start_pfn = 0;
321 zone->spanned_pages = 0;
324 zone_span_writeunlock(zone);
327 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
328 unsigned long end_pfn)
330 enum zone_type zid = zone_idx(zone);
331 int nid = zone->zone_pgdat->node_id;
334 for (pfn = start_pfn; pfn < end_pfn; pfn++)
335 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
338 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
339 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
340 static int __ref ensure_zone_is_initialized(struct zone *zone,
341 unsigned long start_pfn, unsigned long num_pages)
343 if (!zone_is_initialized(zone))
344 return init_currently_empty_zone(zone, start_pfn, num_pages);
349 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
350 unsigned long start_pfn, unsigned long end_pfn)
354 unsigned long z1_start_pfn;
356 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
360 pgdat_resize_lock(z1->zone_pgdat, &flags);
362 /* can't move pfns which are higher than @z2 */
363 if (end_pfn > zone_end_pfn(z2))
365 /* the move out part must be at the left most of @z2 */
366 if (start_pfn > z2->zone_start_pfn)
368 /* must included/overlap */
369 if (end_pfn <= z2->zone_start_pfn)
372 /* use start_pfn for z1's start_pfn if z1 is empty */
373 if (!zone_is_empty(z1))
374 z1_start_pfn = z1->zone_start_pfn;
376 z1_start_pfn = start_pfn;
378 resize_zone(z1, z1_start_pfn, end_pfn);
379 resize_zone(z2, end_pfn, zone_end_pfn(z2));
381 pgdat_resize_unlock(z1->zone_pgdat, &flags);
383 fix_zone_id(z1, start_pfn, end_pfn);
387 pgdat_resize_unlock(z1->zone_pgdat, &flags);
391 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
392 unsigned long start_pfn, unsigned long end_pfn)
396 unsigned long z2_end_pfn;
398 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
402 pgdat_resize_lock(z1->zone_pgdat, &flags);
404 /* can't move pfns which are lower than @z1 */
405 if (z1->zone_start_pfn > start_pfn)
407 /* the move out part mast at the right most of @z1 */
408 if (zone_end_pfn(z1) > end_pfn)
410 /* must included/overlap */
411 if (start_pfn >= zone_end_pfn(z1))
414 /* use end_pfn for z2's end_pfn if z2 is empty */
415 if (!zone_is_empty(z2))
416 z2_end_pfn = zone_end_pfn(z2);
418 z2_end_pfn = end_pfn;
420 resize_zone(z1, z1->zone_start_pfn, start_pfn);
421 resize_zone(z2, start_pfn, z2_end_pfn);
423 pgdat_resize_unlock(z1->zone_pgdat, &flags);
425 fix_zone_id(z2, start_pfn, end_pfn);
429 pgdat_resize_unlock(z1->zone_pgdat, &flags);
433 static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
434 unsigned long end_pfn)
436 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
438 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
439 pgdat->node_start_pfn = start_pfn;
441 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
442 pgdat->node_start_pfn;
445 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
447 struct pglist_data *pgdat = zone->zone_pgdat;
448 int nr_pages = PAGES_PER_SECTION;
449 int nid = pgdat->node_id;
451 unsigned long flags, pfn;
454 zone_type = zone - pgdat->node_zones;
455 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
459 pgdat_resize_lock(zone->zone_pgdat, &flags);
460 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
461 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
462 phys_start_pfn + nr_pages);
463 pgdat_resize_unlock(zone->zone_pgdat, &flags);
464 memmap_init_zone(nr_pages, nid, zone_type,
465 phys_start_pfn, MEMMAP_HOTPLUG);
467 /* online_page_range is called later and expects pages reserved */
468 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
472 SetPageReserved(pfn_to_page(pfn));
477 static int __meminit __add_section(int nid, struct zone *zone,
478 unsigned long phys_start_pfn)
482 if (pfn_valid(phys_start_pfn))
485 ret = sparse_add_one_section(zone, phys_start_pfn);
490 ret = __add_zone(zone, phys_start_pfn);
495 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
499 * Reasonably generic function for adding memory. It is
500 * expected that archs that support memory hotplug will
501 * call this function after deciding the zone to which to
504 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
505 unsigned long nr_pages)
509 int start_sec, end_sec;
510 /* during initialize mem_map, align hot-added range to section */
511 start_sec = pfn_to_section_nr(phys_start_pfn);
512 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
514 for (i = start_sec; i <= end_sec; i++) {
515 err = __add_section(nid, zone, section_nr_to_pfn(i));
518 * EEXIST is finally dealt with by ioresource collision
519 * check. see add_memory() => register_memory_resource()
520 * Warning will be printed if there is collision.
522 if (err && (err != -EEXIST))
526 vmemmap_populate_print_last();
530 EXPORT_SYMBOL_GPL(__add_pages);
532 #ifdef CONFIG_MEMORY_HOTREMOVE
533 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
534 static int find_smallest_section_pfn(int nid, struct zone *zone,
535 unsigned long start_pfn,
536 unsigned long end_pfn)
538 struct mem_section *ms;
540 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
541 ms = __pfn_to_section(start_pfn);
543 if (unlikely(!valid_section(ms)))
546 if (unlikely(pfn_to_nid(start_pfn) != nid))
549 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
558 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
559 static int find_biggest_section_pfn(int nid, struct zone *zone,
560 unsigned long start_pfn,
561 unsigned long end_pfn)
563 struct mem_section *ms;
566 /* pfn is the end pfn of a memory section. */
568 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
569 ms = __pfn_to_section(pfn);
571 if (unlikely(!valid_section(ms)))
574 if (unlikely(pfn_to_nid(pfn) != nid))
577 if (zone && zone != page_zone(pfn_to_page(pfn)))
586 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
587 unsigned long end_pfn)
589 unsigned long zone_start_pfn = zone->zone_start_pfn;
590 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
591 unsigned long zone_end_pfn = z;
593 struct mem_section *ms;
594 int nid = zone_to_nid(zone);
596 zone_span_writelock(zone);
597 if (zone_start_pfn == start_pfn) {
599 * If the section is smallest section in the zone, it need
600 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
601 * In this case, we find second smallest valid mem_section
602 * for shrinking zone.
604 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
607 zone->zone_start_pfn = pfn;
608 zone->spanned_pages = zone_end_pfn - pfn;
610 } else if (zone_end_pfn == end_pfn) {
612 * If the section is biggest section in the zone, it need
613 * shrink zone->spanned_pages.
614 * In this case, we find second biggest valid mem_section for
617 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
620 zone->spanned_pages = pfn - zone_start_pfn + 1;
624 * The section is not biggest or smallest mem_section in the zone, it
625 * only creates a hole in the zone. So in this case, we need not
626 * change the zone. But perhaps, the zone has only hole data. Thus
627 * it check the zone has only hole or not.
629 pfn = zone_start_pfn;
630 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
631 ms = __pfn_to_section(pfn);
633 if (unlikely(!valid_section(ms)))
636 if (page_zone(pfn_to_page(pfn)) != zone)
639 /* If the section is current section, it continues the loop */
640 if (start_pfn == pfn)
643 /* If we find valid section, we have nothing to do */
644 zone_span_writeunlock(zone);
648 /* The zone has no valid section */
649 zone->zone_start_pfn = 0;
650 zone->spanned_pages = 0;
651 zone_span_writeunlock(zone);
654 static void shrink_pgdat_span(struct pglist_data *pgdat,
655 unsigned long start_pfn, unsigned long end_pfn)
657 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
658 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
659 unsigned long pgdat_end_pfn = p;
661 struct mem_section *ms;
662 int nid = pgdat->node_id;
664 if (pgdat_start_pfn == start_pfn) {
666 * If the section is smallest section in the pgdat, it need
667 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
668 * In this case, we find second smallest valid mem_section
669 * for shrinking zone.
671 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
674 pgdat->node_start_pfn = pfn;
675 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
677 } else if (pgdat_end_pfn == end_pfn) {
679 * If the section is biggest section in the pgdat, it need
680 * shrink pgdat->node_spanned_pages.
681 * In this case, we find second biggest valid mem_section for
684 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
687 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
691 * If the section is not biggest or smallest mem_section in the pgdat,
692 * it only creates a hole in the pgdat. So in this case, we need not
694 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
695 * has only hole or not.
697 pfn = pgdat_start_pfn;
698 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
699 ms = __pfn_to_section(pfn);
701 if (unlikely(!valid_section(ms)))
704 if (pfn_to_nid(pfn) != nid)
707 /* If the section is current section, it continues the loop */
708 if (start_pfn == pfn)
711 /* If we find valid section, we have nothing to do */
715 /* The pgdat has no valid section */
716 pgdat->node_start_pfn = 0;
717 pgdat->node_spanned_pages = 0;
720 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
722 struct pglist_data *pgdat = zone->zone_pgdat;
723 int nr_pages = PAGES_PER_SECTION;
727 zone_type = zone - pgdat->node_zones;
729 pgdat_resize_lock(zone->zone_pgdat, &flags);
730 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
731 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
732 pgdat_resize_unlock(zone->zone_pgdat, &flags);
735 static int __remove_section(struct zone *zone, struct mem_section *ms)
737 unsigned long start_pfn;
741 if (!valid_section(ms))
744 ret = unregister_memory_section(ms);
748 scn_nr = __section_nr(ms);
749 start_pfn = section_nr_to_pfn(scn_nr);
750 __remove_zone(zone, start_pfn);
752 sparse_remove_one_section(zone, ms);
757 * __remove_pages() - remove sections of pages from a zone
758 * @zone: zone from which pages need to be removed
759 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
760 * @nr_pages: number of pages to remove (must be multiple of section size)
762 * Generic helper function to remove section mappings and sysfs entries
763 * for the section of the memory we are removing. Caller needs to make
764 * sure that pages are marked reserved and zones are adjust properly by
765 * calling offline_pages().
767 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
768 unsigned long nr_pages)
771 int sections_to_remove;
772 resource_size_t start, size;
776 * We can only remove entire sections
778 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
779 BUG_ON(nr_pages % PAGES_PER_SECTION);
781 start = phys_start_pfn << PAGE_SHIFT;
782 size = nr_pages * PAGE_SIZE;
784 /* in the ZONE_DEVICE case device driver owns the memory region */
785 if (!is_dev_zone(zone))
786 ret = release_mem_region_adjustable(&iomem_resource, start, size);
788 resource_size_t endres = start + size - 1;
790 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
791 &start, &endres, ret);
794 sections_to_remove = nr_pages / PAGES_PER_SECTION;
795 for (i = 0; i < sections_to_remove; i++) {
796 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
797 ret = __remove_section(zone, __pfn_to_section(pfn));
803 EXPORT_SYMBOL_GPL(__remove_pages);
804 #endif /* CONFIG_MEMORY_HOTREMOVE */
806 int set_online_page_callback(online_page_callback_t callback)
811 mutex_lock(&online_page_callback_lock);
813 if (online_page_callback == generic_online_page) {
814 online_page_callback = callback;
818 mutex_unlock(&online_page_callback_lock);
823 EXPORT_SYMBOL_GPL(set_online_page_callback);
825 int restore_online_page_callback(online_page_callback_t callback)
830 mutex_lock(&online_page_callback_lock);
832 if (online_page_callback == callback) {
833 online_page_callback = generic_online_page;
837 mutex_unlock(&online_page_callback_lock);
842 EXPORT_SYMBOL_GPL(restore_online_page_callback);
844 void __online_page_set_limits(struct page *page)
847 EXPORT_SYMBOL_GPL(__online_page_set_limits);
849 void __online_page_increment_counters(struct page *page)
851 adjust_managed_page_count(page, 1);
853 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
855 void __online_page_free(struct page *page)
857 __free_reserved_page(page);
859 EXPORT_SYMBOL_GPL(__online_page_free);
861 static int generic_online_page(struct page *page)
863 __online_page_set_limits(page);
864 __online_page_increment_counters(page);
865 __online_page_free(page);
869 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
873 unsigned long onlined_pages = *(unsigned long *)arg;
876 if (PageReserved(pfn_to_page(start_pfn)))
877 for (i = 0; i < nr_pages; i++) {
878 page = pfn_to_page(start_pfn + i);
879 ret = (*online_page_callback)(page);
883 *(unsigned long *)arg = onlined_pages;
887 #ifdef CONFIG_MOVABLE_NODE
889 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
892 static bool can_online_high_movable(struct zone *zone)
896 #else /* CONFIG_MOVABLE_NODE */
897 /* ensure every online node has NORMAL memory */
898 static bool can_online_high_movable(struct zone *zone)
900 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
902 #endif /* CONFIG_MOVABLE_NODE */
904 /* check which state of node_states will be changed when online memory */
905 static void node_states_check_changes_online(unsigned long nr_pages,
906 struct zone *zone, struct memory_notify *arg)
908 int nid = zone_to_nid(zone);
909 enum zone_type zone_last = ZONE_NORMAL;
912 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
913 * contains nodes which have zones of 0...ZONE_NORMAL,
914 * set zone_last to ZONE_NORMAL.
916 * If we don't have HIGHMEM nor movable node,
917 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
918 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
920 if (N_MEMORY == N_NORMAL_MEMORY)
921 zone_last = ZONE_MOVABLE;
924 * if the memory to be online is in a zone of 0...zone_last, and
925 * the zones of 0...zone_last don't have memory before online, we will
926 * need to set the node to node_states[N_NORMAL_MEMORY] after
927 * the memory is online.
929 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
930 arg->status_change_nid_normal = nid;
932 arg->status_change_nid_normal = -1;
934 #ifdef CONFIG_HIGHMEM
936 * If we have movable node, node_states[N_HIGH_MEMORY]
937 * contains nodes which have zones of 0...ZONE_HIGHMEM,
938 * set zone_last to ZONE_HIGHMEM.
940 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
941 * contains nodes which have zones of 0...ZONE_MOVABLE,
942 * set zone_last to ZONE_MOVABLE.
944 zone_last = ZONE_HIGHMEM;
945 if (N_MEMORY == N_HIGH_MEMORY)
946 zone_last = ZONE_MOVABLE;
948 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
949 arg->status_change_nid_high = nid;
951 arg->status_change_nid_high = -1;
953 arg->status_change_nid_high = arg->status_change_nid_normal;
957 * if the node don't have memory befor online, we will need to
958 * set the node to node_states[N_MEMORY] after the memory
961 if (!node_state(nid, N_MEMORY))
962 arg->status_change_nid = nid;
964 arg->status_change_nid = -1;
967 static void node_states_set_node(int node, struct memory_notify *arg)
969 if (arg->status_change_nid_normal >= 0)
970 node_set_state(node, N_NORMAL_MEMORY);
972 if (arg->status_change_nid_high >= 0)
973 node_set_state(node, N_HIGH_MEMORY);
975 node_set_state(node, N_MEMORY);
979 /* Must be protected by mem_hotplug_begin() */
980 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
983 unsigned long onlined_pages = 0;
985 int need_zonelists_rebuild = 0;
988 struct memory_notify arg;
991 * This doesn't need a lock to do pfn_to_page().
992 * The section can't be removed here because of the
993 * memory_block->state_mutex.
995 zone = page_zone(pfn_to_page(pfn));
997 if ((zone_idx(zone) > ZONE_NORMAL ||
998 online_type == MMOP_ONLINE_MOVABLE) &&
999 !can_online_high_movable(zone))
1002 if (online_type == MMOP_ONLINE_KERNEL &&
1003 zone_idx(zone) == ZONE_MOVABLE) {
1004 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
1007 if (online_type == MMOP_ONLINE_MOVABLE &&
1008 zone_idx(zone) == ZONE_MOVABLE - 1) {
1009 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
1013 /* Previous code may changed the zone of the pfn range */
1014 zone = page_zone(pfn_to_page(pfn));
1016 arg.start_pfn = pfn;
1017 arg.nr_pages = nr_pages;
1018 node_states_check_changes_online(nr_pages, zone, &arg);
1020 nid = zone_to_nid(zone);
1022 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1023 ret = notifier_to_errno(ret);
1025 memory_notify(MEM_CANCEL_ONLINE, &arg);
1029 * If this zone is not populated, then it is not in zonelist.
1030 * This means the page allocator ignores this zone.
1031 * So, zonelist must be updated after online.
1033 mutex_lock(&zonelists_mutex);
1034 if (!populated_zone(zone)) {
1035 need_zonelists_rebuild = 1;
1036 build_all_zonelists(NULL, zone);
1039 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1040 online_pages_range);
1042 if (need_zonelists_rebuild)
1043 zone_pcp_reset(zone);
1044 mutex_unlock(&zonelists_mutex);
1045 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1046 (unsigned long long) pfn << PAGE_SHIFT,
1047 (((unsigned long long) pfn + nr_pages)
1048 << PAGE_SHIFT) - 1);
1049 memory_notify(MEM_CANCEL_ONLINE, &arg);
1053 zone->present_pages += onlined_pages;
1055 pgdat_resize_lock(zone->zone_pgdat, &flags);
1056 zone->zone_pgdat->node_present_pages += onlined_pages;
1057 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1059 if (onlined_pages) {
1060 node_states_set_node(nid, &arg);
1061 if (need_zonelists_rebuild)
1062 build_all_zonelists(NULL, NULL);
1064 zone_pcp_update(zone);
1067 mutex_unlock(&zonelists_mutex);
1069 init_per_zone_wmark_min();
1071 if (onlined_pages) {
1076 vm_total_pages = nr_free_pagecache_pages();
1078 writeback_set_ratelimit();
1081 memory_notify(MEM_ONLINE, &arg);
1084 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1086 static void reset_node_present_pages(pg_data_t *pgdat)
1090 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1091 z->present_pages = 0;
1093 pgdat->node_present_pages = 0;
1096 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1097 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1099 struct pglist_data *pgdat;
1100 unsigned long zones_size[MAX_NR_ZONES] = {0};
1101 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1102 unsigned long start_pfn = PFN_DOWN(start);
1104 pgdat = NODE_DATA(nid);
1106 pgdat = arch_alloc_nodedata(nid);
1110 arch_refresh_nodedata(nid, pgdat);
1112 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1113 pgdat->nr_zones = 0;
1114 pgdat->classzone_idx = 0;
1117 /* we can use NODE_DATA(nid) from here */
1119 /* init node's zones as empty zones, we don't have any present pages.*/
1120 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1123 * The node we allocated has no zone fallback lists. For avoiding
1124 * to access not-initialized zonelist, build here.
1126 mutex_lock(&zonelists_mutex);
1127 build_all_zonelists(pgdat, NULL);
1128 mutex_unlock(&zonelists_mutex);
1131 * zone->managed_pages is set to an approximate value in
1132 * free_area_init_core(), which will cause
1133 * /sys/device/system/node/nodeX/meminfo has wrong data.
1134 * So reset it to 0 before any memory is onlined.
1136 reset_node_managed_pages(pgdat);
1139 * When memory is hot-added, all the memory is in offline state. So
1140 * clear all zones' present_pages because they will be updated in
1141 * online_pages() and offline_pages().
1143 reset_node_present_pages(pgdat);
1148 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1150 arch_refresh_nodedata(nid, NULL);
1151 arch_free_nodedata(pgdat);
1157 * try_online_node - online a node if offlined
1159 * called by cpu_up() to online a node without onlined memory.
1161 int try_online_node(int nid)
1166 if (node_online(nid))
1169 mem_hotplug_begin();
1170 pgdat = hotadd_new_pgdat(nid, 0);
1172 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1176 node_set_online(nid);
1177 ret = register_one_node(nid);
1180 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1181 mutex_lock(&zonelists_mutex);
1182 build_all_zonelists(NULL, NULL);
1183 mutex_unlock(&zonelists_mutex);
1191 static int check_hotplug_memory_range(u64 start, u64 size)
1193 u64 start_pfn = PFN_DOWN(start);
1194 u64 nr_pages = size >> PAGE_SHIFT;
1196 /* Memory range must be aligned with section */
1197 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1198 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1199 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1200 (unsigned long long)start,
1201 (unsigned long long)size);
1209 * If movable zone has already been setup, newly added memory should be check.
1210 * If its address is higher than movable zone, it should be added as movable.
1211 * Without this check, movable zone may overlap with other zone.
1213 static int should_add_memory_movable(int nid, u64 start, u64 size)
1215 unsigned long start_pfn = start >> PAGE_SHIFT;
1216 pg_data_t *pgdat = NODE_DATA(nid);
1217 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1219 if (zone_is_empty(movable_zone))
1222 if (movable_zone->zone_start_pfn <= start_pfn)
1228 int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1231 #ifdef CONFIG_ZONE_DEVICE
1235 if (should_add_memory_movable(nid, start, size))
1236 return ZONE_MOVABLE;
1238 return zone_default;
1241 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1242 int __ref add_memory_resource(int nid, struct resource *res)
1245 pg_data_t *pgdat = NULL;
1251 size = resource_size(res);
1253 ret = check_hotplug_memory_range(start, size);
1257 { /* Stupid hack to suppress address-never-null warning */
1258 void *p = NODE_DATA(nid);
1262 mem_hotplug_begin();
1265 * Add new range to memblock so that when hotadd_new_pgdat() is called
1266 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1267 * this new range and calculate total pages correctly. The range will
1268 * be removed at hot-remove time.
1270 memblock_add_node(start, size, nid);
1272 new_node = !node_online(nid);
1274 pgdat = hotadd_new_pgdat(nid, start);
1280 /* call arch's memory hotadd */
1281 ret = arch_add_memory(nid, start, size, false);
1286 /* we online node here. we can't roll back from here. */
1287 node_set_online(nid);
1290 ret = register_one_node(nid);
1292 * If sysfs file of new node can't create, cpu on the node
1293 * can't be hot-added. There is no rollback way now.
1294 * So, check by BUG_ON() to catch it reluctantly..
1299 /* create new memmap entry */
1300 firmware_map_add_hotplug(start, start + size, "System RAM");
1305 /* rollback pgdat allocation and others */
1307 rollback_node_hotadd(nid, pgdat);
1308 memblock_remove(start, size);
1314 EXPORT_SYMBOL_GPL(add_memory_resource);
1316 int __ref add_memory(int nid, u64 start, u64 size)
1318 struct resource *res;
1321 res = register_memory_resource(start, size);
1325 ret = add_memory_resource(nid, res);
1327 release_memory_resource(res);
1330 EXPORT_SYMBOL_GPL(add_memory);
1332 #ifdef CONFIG_MEMORY_HOTREMOVE
1334 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1335 * set and the size of the free page is given by page_order(). Using this,
1336 * the function determines if the pageblock contains only free pages.
1337 * Due to buddy contraints, a free page at least the size of a pageblock will
1338 * be located at the start of the pageblock
1340 static inline int pageblock_free(struct page *page)
1342 return PageBuddy(page) && page_order(page) >= pageblock_order;
1345 /* Return the start of the next active pageblock after a given page */
1346 static struct page *next_active_pageblock(struct page *page)
1348 /* Ensure the starting page is pageblock-aligned */
1349 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1351 /* If the entire pageblock is free, move to the end of free page */
1352 if (pageblock_free(page)) {
1354 /* be careful. we don't have locks, page_order can be changed.*/
1355 order = page_order(page);
1356 if ((order < MAX_ORDER) && (order >= pageblock_order))
1357 return page + (1 << order);
1360 return page + pageblock_nr_pages;
1363 /* Checks if this range of memory is likely to be hot-removable. */
1364 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1366 struct page *page = pfn_to_page(start_pfn);
1367 unsigned long end_pfn = min(start_pfn + nr_pages, zone_end_pfn(page_zone(page)));
1368 struct page *end_page = pfn_to_page(end_pfn);
1370 /* Check the starting page of each pageblock within the range */
1371 for (; page < end_page; page = next_active_pageblock(page)) {
1372 if (!is_pageblock_removable_nolock(page))
1377 /* All pageblocks in the memory block are likely to be hot-removable */
1382 * Confirm all pages in a range [start, end) belong to the same zone.
1383 * When true, return its valid [start, end).
1385 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1386 unsigned long *valid_start, unsigned long *valid_end)
1388 unsigned long pfn, sec_end_pfn;
1389 unsigned long start, end;
1390 struct zone *zone = NULL;
1393 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1395 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1396 /* Make sure the memory section is present first */
1397 if (!present_section_nr(pfn_to_section_nr(pfn)))
1399 for (; pfn < sec_end_pfn && pfn < end_pfn;
1400 pfn += MAX_ORDER_NR_PAGES) {
1402 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1403 while ((i < MAX_ORDER_NR_PAGES) &&
1404 !pfn_valid_within(pfn + i))
1406 if (i == MAX_ORDER_NR_PAGES)
1408 /* Check if we got outside of the zone */
1409 if (zone && !zone_spans_pfn(zone, pfn + i))
1411 page = pfn_to_page(pfn + i);
1412 if (zone && page_zone(page) != zone)
1416 zone = page_zone(page);
1417 end = pfn + MAX_ORDER_NR_PAGES;
1422 *valid_start = start;
1431 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1432 * non-lru movable pages and hugepages). We scan pfn because it's much
1433 * easier than scanning over linked list. This function returns the pfn
1434 * of the first found movable page if it's found, otherwise 0.
1436 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1440 for (pfn = start; pfn < end; pfn++) {
1441 if (pfn_valid(pfn)) {
1442 page = pfn_to_page(pfn);
1445 if (__PageMovable(page))
1447 if (PageHuge(page)) {
1448 if (page_huge_active(page))
1451 pfn = round_up(pfn + 1,
1452 1 << compound_order(page)) - 1;
1459 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1461 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1465 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1466 int not_managed = 0;
1470 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1471 if (!pfn_valid(pfn))
1473 page = pfn_to_page(pfn);
1475 if (PageHuge(page)) {
1476 struct page *head = compound_head(page);
1477 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1478 if (compound_order(head) > PFN_SECTION_SHIFT) {
1482 if (isolate_huge_page(page, &source))
1483 move_pages -= 1 << compound_order(head);
1488 * HWPoison pages have elevated reference counts so the migration would
1489 * fail on them. It also doesn't make any sense to migrate them in the
1490 * first place. Still try to unmap such a page in case it is still mapped
1491 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1492 * the unmap as the catch all safety net).
1494 if (PageHWPoison(page)) {
1495 if (WARN_ON(PageLRU(page)))
1496 isolate_lru_page(page);
1497 if (page_mapped(page))
1498 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS, NULL);
1502 if (!get_page_unless_zero(page))
1505 * We can skip free pages. And we can deal with pages on
1506 * LRU and non-lru movable pages.
1509 ret = isolate_lru_page(page);
1511 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1512 if (!ret) { /* Success */
1514 list_add_tail(&page->lru, &source);
1516 if (!__PageMovable(page))
1517 inc_zone_page_state(page, NR_ISOLATED_ANON +
1518 page_is_file_cache(page));
1520 #ifdef CONFIG_DEBUG_VM
1521 pr_alert("failed to isolate pfn %lx\n", pfn);
1522 dump_page(page, "isolation failed");
1525 /* Because we don't have big zone->lock. we should
1526 check this again here. */
1527 if (page_count(page)) {
1534 if (!list_empty(&source)) {
1536 putback_movable_pages(&source);
1541 * alloc_migrate_target should be improooooved!!
1542 * migrate_pages returns # of failed pages.
1544 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
1545 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1547 putback_movable_pages(&source);
1554 * remove from free_area[] and mark all as Reserved.
1557 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1560 __offline_isolated_pages(start, start + nr_pages);
1565 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1567 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1568 offline_isolated_pages_cb);
1572 * Check all pages in range, recoreded as memory resource, are isolated.
1575 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1579 long offlined = *(long *)data;
1580 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1581 offlined = nr_pages;
1583 *(long *)data += offlined;
1588 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1593 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1594 check_pages_isolated_cb);
1596 offlined = (long)ret;
1600 #ifdef CONFIG_MOVABLE_NODE
1602 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1605 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1609 #else /* CONFIG_MOVABLE_NODE */
1610 /* ensure the node has NORMAL memory if it is still online */
1611 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1613 struct pglist_data *pgdat = zone->zone_pgdat;
1614 unsigned long present_pages = 0;
1617 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1618 present_pages += pgdat->node_zones[zt].present_pages;
1620 if (present_pages > nr_pages)
1624 for (; zt <= ZONE_MOVABLE; zt++)
1625 present_pages += pgdat->node_zones[zt].present_pages;
1628 * we can't offline the last normal memory until all
1629 * higher memory is offlined.
1631 return present_pages == 0;
1633 #endif /* CONFIG_MOVABLE_NODE */
1635 static int __init cmdline_parse_movable_node(char *p)
1637 #ifdef CONFIG_MOVABLE_NODE
1639 * Memory used by the kernel cannot be hot-removed because Linux
1640 * cannot migrate the kernel pages. When memory hotplug is
1641 * enabled, we should prevent memblock from allocating memory
1644 * ACPI SRAT records all hotpluggable memory ranges. But before
1645 * SRAT is parsed, we don't know about it.
1647 * The kernel image is loaded into memory at very early time. We
1648 * cannot prevent this anyway. So on NUMA system, we set any
1649 * node the kernel resides in as un-hotpluggable.
1651 * Since on modern servers, one node could have double-digit
1652 * gigabytes memory, we can assume the memory around the kernel
1653 * image is also un-hotpluggable. So before SRAT is parsed, just
1654 * allocate memory near the kernel image to try the best to keep
1655 * the kernel away from hotpluggable memory.
1657 memblock_set_bottom_up(true);
1658 movable_node_enabled = true;
1660 pr_warn("movable_node option not supported\n");
1664 early_param("movable_node", cmdline_parse_movable_node);
1666 /* check which state of node_states will be changed when offline memory */
1667 static void node_states_check_changes_offline(unsigned long nr_pages,
1668 struct zone *zone, struct memory_notify *arg)
1670 struct pglist_data *pgdat = zone->zone_pgdat;
1671 unsigned long present_pages = 0;
1672 enum zone_type zt, zone_last = ZONE_NORMAL;
1675 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1676 * contains nodes which have zones of 0...ZONE_NORMAL,
1677 * set zone_last to ZONE_NORMAL.
1679 * If we don't have HIGHMEM nor movable node,
1680 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1681 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1683 if (N_MEMORY == N_NORMAL_MEMORY)
1684 zone_last = ZONE_MOVABLE;
1687 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1688 * If the memory to be offline is in a zone of 0...zone_last,
1689 * and it is the last present memory, 0...zone_last will
1690 * become empty after offline , thus we can determind we will
1691 * need to clear the node from node_states[N_NORMAL_MEMORY].
1693 for (zt = 0; zt <= zone_last; zt++)
1694 present_pages += pgdat->node_zones[zt].present_pages;
1695 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1696 arg->status_change_nid_normal = zone_to_nid(zone);
1698 arg->status_change_nid_normal = -1;
1700 #ifdef CONFIG_HIGHMEM
1702 * If we have movable node, node_states[N_HIGH_MEMORY]
1703 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1704 * set zone_last to ZONE_HIGHMEM.
1706 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1707 * contains nodes which have zones of 0...ZONE_MOVABLE,
1708 * set zone_last to ZONE_MOVABLE.
1710 zone_last = ZONE_HIGHMEM;
1711 if (N_MEMORY == N_HIGH_MEMORY)
1712 zone_last = ZONE_MOVABLE;
1714 for (; zt <= zone_last; zt++)
1715 present_pages += pgdat->node_zones[zt].present_pages;
1716 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1717 arg->status_change_nid_high = zone_to_nid(zone);
1719 arg->status_change_nid_high = -1;
1721 arg->status_change_nid_high = arg->status_change_nid_normal;
1725 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1727 zone_last = ZONE_MOVABLE;
1730 * check whether node_states[N_HIGH_MEMORY] will be changed
1731 * If we try to offline the last present @nr_pages from the node,
1732 * we can determind we will need to clear the node from
1733 * node_states[N_HIGH_MEMORY].
1735 for (; zt <= zone_last; zt++)
1736 present_pages += pgdat->node_zones[zt].present_pages;
1737 if (nr_pages >= present_pages)
1738 arg->status_change_nid = zone_to_nid(zone);
1740 arg->status_change_nid = -1;
1743 static void node_states_clear_node(int node, struct memory_notify *arg)
1745 if (arg->status_change_nid_normal >= 0)
1746 node_clear_state(node, N_NORMAL_MEMORY);
1748 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1749 (arg->status_change_nid_high >= 0))
1750 node_clear_state(node, N_HIGH_MEMORY);
1752 if ((N_MEMORY != N_HIGH_MEMORY) &&
1753 (arg->status_change_nid >= 0))
1754 node_clear_state(node, N_MEMORY);
1757 static int __ref __offline_pages(unsigned long start_pfn,
1758 unsigned long end_pfn, unsigned long timeout)
1760 unsigned long pfn, nr_pages, expire;
1761 long offlined_pages;
1762 int ret, drain, retry_max, node;
1763 unsigned long flags;
1764 unsigned long valid_start, valid_end;
1766 struct memory_notify arg;
1768 /* at least, alignment against pageblock is necessary */
1769 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1771 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1773 /* This makes hotplug much easier...and readable.
1774 we assume this for now. .*/
1775 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1778 zone = page_zone(pfn_to_page(valid_start));
1779 node = zone_to_nid(zone);
1780 nr_pages = end_pfn - start_pfn;
1782 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1785 /* set above range as isolated */
1786 ret = start_isolate_page_range(start_pfn, end_pfn,
1787 MIGRATE_MOVABLE, true);
1791 arg.start_pfn = start_pfn;
1792 arg.nr_pages = nr_pages;
1793 node_states_check_changes_offline(nr_pages, zone, &arg);
1795 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1796 ret = notifier_to_errno(ret);
1798 goto failed_removal;
1801 expire = jiffies + timeout;
1805 /* start memory hot removal */
1807 if (time_after(jiffies, expire))
1808 goto failed_removal;
1810 if (signal_pending(current))
1811 goto failed_removal;
1814 lru_add_drain_all();
1816 drain_all_pages(zone);
1819 pfn = scan_movable_pages(start_pfn, end_pfn);
1820 if (pfn) { /* We have movable pages */
1821 ret = do_migrate_range(pfn, end_pfn);
1827 if (--retry_max == 0)
1828 goto failed_removal;
1834 /* drain all zone's lru pagevec, this is asynchronous... */
1835 lru_add_drain_all();
1837 /* drain pcp pages, this is synchronous. */
1838 drain_all_pages(zone);
1840 * dissolve free hugepages in the memory block before doing offlining
1841 * actually in order to make hugetlbfs's object counting consistent.
1843 dissolve_free_huge_pages(start_pfn, end_pfn);
1845 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1846 if (offlined_pages < 0) {
1848 goto failed_removal;
1850 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1851 /* Ok, all of our target is isolated.
1852 We cannot do rollback at this point. */
1853 offline_isolated_pages(start_pfn, end_pfn);
1854 /* reset pagetype flags and makes migrate type to be MOVABLE */
1855 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1856 /* removal success */
1857 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1858 zone->present_pages -= offlined_pages;
1860 pgdat_resize_lock(zone->zone_pgdat, &flags);
1861 zone->zone_pgdat->node_present_pages -= offlined_pages;
1862 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1864 init_per_zone_wmark_min();
1866 if (!populated_zone(zone)) {
1867 zone_pcp_reset(zone);
1868 mutex_lock(&zonelists_mutex);
1869 build_all_zonelists(NULL, NULL);
1870 mutex_unlock(&zonelists_mutex);
1872 zone_pcp_update(zone);
1874 node_states_clear_node(node, &arg);
1875 if (arg.status_change_nid >= 0) {
1877 kcompactd_stop(node);
1880 vm_total_pages = nr_free_pagecache_pages();
1881 writeback_set_ratelimit();
1883 memory_notify(MEM_OFFLINE, &arg);
1887 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1888 (unsigned long long) start_pfn << PAGE_SHIFT,
1889 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1890 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1891 /* pushback to free area */
1892 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1896 /* Must be protected by mem_hotplug_begin() */
1897 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1899 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1901 #endif /* CONFIG_MEMORY_HOTREMOVE */
1904 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1905 * @start_pfn: start pfn of the memory range
1906 * @end_pfn: end pfn of the memory range
1907 * @arg: argument passed to func
1908 * @func: callback for each memory section walked
1910 * This function walks through all present mem sections in range
1911 * [start_pfn, end_pfn) and call func on each mem section.
1913 * Returns the return value of func.
1915 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1916 void *arg, int (*func)(struct memory_block *, void *))
1918 struct memory_block *mem = NULL;
1919 struct mem_section *section;
1920 unsigned long pfn, section_nr;
1923 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1924 section_nr = pfn_to_section_nr(pfn);
1925 if (!present_section_nr(section_nr))
1928 section = __nr_to_section(section_nr);
1929 /* same memblock? */
1931 if ((section_nr >= mem->start_section_nr) &&
1932 (section_nr <= mem->end_section_nr))
1935 mem = find_memory_block_hinted(section, mem);
1939 ret = func(mem, arg);
1941 kobject_put(&mem->dev.kobj);
1947 kobject_put(&mem->dev.kobj);
1952 #ifdef CONFIG_MEMORY_HOTREMOVE
1953 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1955 int ret = !is_memblock_offlined(mem);
1957 if (unlikely(ret)) {
1958 phys_addr_t beginpa, endpa;
1960 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1961 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1962 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1969 static int check_cpu_on_node(pg_data_t *pgdat)
1973 for_each_present_cpu(cpu) {
1974 if (cpu_to_node(cpu) == pgdat->node_id)
1976 * the cpu on this node isn't removed, and we can't
1977 * offline this node.
1985 static void unmap_cpu_on_node(pg_data_t *pgdat)
1987 #ifdef CONFIG_ACPI_NUMA
1990 for_each_possible_cpu(cpu)
1991 if (cpu_to_node(cpu) == pgdat->node_id)
1992 numa_clear_node(cpu);
1996 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
2000 ret = check_cpu_on_node(pgdat);
2005 * the node will be offlined when we come here, so we can clear
2006 * the cpu_to_node() now.
2009 unmap_cpu_on_node(pgdat);
2016 * Offline a node if all memory sections and cpus of the node are removed.
2018 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2019 * and online/offline operations before this call.
2021 void try_offline_node(int nid)
2023 pg_data_t *pgdat = NODE_DATA(nid);
2024 unsigned long start_pfn = pgdat->node_start_pfn;
2025 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
2029 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2030 unsigned long section_nr = pfn_to_section_nr(pfn);
2032 if (!present_section_nr(section_nr))
2035 if (pfn_to_nid(pfn) != nid)
2039 * some memory sections of this node are not removed, and we
2040 * can't offline node now.
2045 if (check_and_unmap_cpu_on_node(pgdat))
2049 * all memory/cpu of this node are removed, we can offline this
2052 node_set_offline(nid);
2053 unregister_one_node(nid);
2055 /* free waittable in each zone */
2056 for (i = 0; i < MAX_NR_ZONES; i++) {
2057 struct zone *zone = pgdat->node_zones + i;
2060 * wait_table may be allocated from boot memory,
2061 * here only free if it's allocated by vmalloc.
2063 if (is_vmalloc_addr(zone->wait_table)) {
2064 vfree(zone->wait_table);
2065 zone->wait_table = NULL;
2069 EXPORT_SYMBOL(try_offline_node);
2074 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2075 * and online/offline operations before this call, as required by
2076 * try_offline_node().
2078 void __ref remove_memory(int nid, u64 start, u64 size)
2082 BUG_ON(check_hotplug_memory_range(start, size));
2084 mem_hotplug_begin();
2087 * All memory blocks must be offlined before removing memory. Check
2088 * whether all memory blocks in question are offline and trigger a BUG()
2089 * if this is not the case.
2091 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2092 check_memblock_offlined_cb);
2096 /* remove memmap entry */
2097 firmware_map_remove(start, start + size, "System RAM");
2098 memblock_free(start, size);
2099 memblock_remove(start, size);
2101 arch_remove_memory(start, size);
2103 try_offline_node(nid);
2107 EXPORT_SYMBOL_GPL(remove_memory);
2108 #endif /* CONFIG_MEMORY_HOTREMOVE */