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>
37 #include <asm/tlbflush.h>
42 * online_page_callback contains pointer to current page onlining function.
43 * Initially it is generic_online_page(). If it is required it could be
44 * changed by calling set_online_page_callback() for callback registration
45 * and restore_online_page_callback() for generic callback restore.
48 static void generic_online_page(struct page *page);
50 static online_page_callback_t online_page_callback = generic_online_page;
51 static DEFINE_MUTEX(online_page_callback_lock);
53 /* The same as the cpu_hotplug lock, but for memory hotplug. */
55 struct task_struct *active_writer;
56 struct mutex lock; /* Synchronizes accesses to refcount, */
58 * Also blocks the new readers during
59 * an ongoing mem hotplug operation.
63 #ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
67 .active_writer = NULL,
68 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
70 #ifdef CONFIG_DEBUG_LOCK_ALLOC
71 .dep_map = {.name = "mem_hotplug.lock" },
75 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
76 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
77 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
78 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
80 void get_online_mems(void)
83 if (mem_hotplug.active_writer == current)
85 memhp_lock_acquire_read();
86 mutex_lock(&mem_hotplug.lock);
87 mem_hotplug.refcount++;
88 mutex_unlock(&mem_hotplug.lock);
92 void put_online_mems(void)
94 if (mem_hotplug.active_writer == current)
96 mutex_lock(&mem_hotplug.lock);
98 if (WARN_ON(!mem_hotplug.refcount))
99 mem_hotplug.refcount++; /* try to fix things up */
101 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
102 wake_up_process(mem_hotplug.active_writer);
103 mutex_unlock(&mem_hotplug.lock);
104 memhp_lock_release();
108 void mem_hotplug_begin(void)
110 mem_hotplug.active_writer = current;
112 memhp_lock_acquire();
114 mutex_lock(&mem_hotplug.lock);
115 if (likely(!mem_hotplug.refcount))
117 __set_current_state(TASK_UNINTERRUPTIBLE);
118 mutex_unlock(&mem_hotplug.lock);
123 void mem_hotplug_done(void)
125 mem_hotplug.active_writer = NULL;
126 mutex_unlock(&mem_hotplug.lock);
127 memhp_lock_release();
130 /* add this memory to iomem resource */
131 static struct resource *register_memory_resource(u64 start, u64 size)
133 struct resource *res;
134 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
137 res->name = "System RAM";
139 res->end = start + size - 1;
140 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
141 if (request_resource(&iomem_resource, res) < 0) {
142 pr_debug("System RAM resource %pR cannot be added\n", res);
149 static void release_memory_resource(struct resource *res)
153 release_resource(res);
158 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
159 void get_page_bootmem(unsigned long info, struct page *page,
162 page->lru.next = (struct list_head *) type;
163 SetPagePrivate(page);
164 set_page_private(page, info);
165 atomic_inc(&page->_count);
168 void put_page_bootmem(struct page *page)
172 type = (unsigned long) page->lru.next;
173 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
174 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
176 if (atomic_dec_return(&page->_count) == 1) {
177 ClearPagePrivate(page);
178 set_page_private(page, 0);
179 INIT_LIST_HEAD(&page->lru);
180 free_reserved_page(page);
184 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
185 #ifndef CONFIG_SPARSEMEM_VMEMMAP
186 static void register_page_bootmem_info_section(unsigned long start_pfn)
188 unsigned long *usemap, mapsize, section_nr, i;
189 struct mem_section *ms;
190 struct page *page, *memmap;
192 section_nr = pfn_to_section_nr(start_pfn);
193 ms = __nr_to_section(section_nr);
195 /* Get section's memmap address */
196 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
199 * Get page for the memmap's phys address
200 * XXX: need more consideration for sparse_vmemmap...
202 page = virt_to_page(memmap);
203 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
204 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
206 /* remember memmap's page */
207 for (i = 0; i < mapsize; i++, page++)
208 get_page_bootmem(section_nr, page, SECTION_INFO);
210 usemap = __nr_to_section(section_nr)->pageblock_flags;
211 page = virt_to_page(usemap);
213 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
215 for (i = 0; i < mapsize; i++, page++)
216 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
219 #else /* CONFIG_SPARSEMEM_VMEMMAP */
220 static void register_page_bootmem_info_section(unsigned long start_pfn)
222 unsigned long *usemap, mapsize, section_nr, i;
223 struct mem_section *ms;
224 struct page *page, *memmap;
226 if (!pfn_valid(start_pfn))
229 section_nr = pfn_to_section_nr(start_pfn);
230 ms = __nr_to_section(section_nr);
232 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
234 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
236 usemap = __nr_to_section(section_nr)->pageblock_flags;
237 page = virt_to_page(usemap);
239 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
241 for (i = 0; i < mapsize; i++, page++)
242 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
244 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
246 void register_page_bootmem_info_node(struct pglist_data *pgdat)
248 unsigned long i, pfn, end_pfn, nr_pages;
249 int node = pgdat->node_id;
253 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
254 page = virt_to_page(pgdat);
256 for (i = 0; i < nr_pages; i++, page++)
257 get_page_bootmem(node, page, NODE_INFO);
259 zone = &pgdat->node_zones[0];
260 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
261 if (zone_is_initialized(zone)) {
262 nr_pages = zone->wait_table_hash_nr_entries
263 * sizeof(wait_queue_head_t);
264 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
265 page = virt_to_page(zone->wait_table);
267 for (i = 0; i < nr_pages; i++, page++)
268 get_page_bootmem(node, page, NODE_INFO);
272 pfn = pgdat->node_start_pfn;
273 end_pfn = pgdat_end_pfn(pgdat);
275 /* register section info */
276 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
278 * Some platforms can assign the same pfn to multiple nodes - on
279 * node0 as well as nodeN. To avoid registering a pfn against
280 * multiple nodes we check that this pfn does not already
281 * reside in some other nodes.
283 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
284 register_page_bootmem_info_section(pfn);
287 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
289 static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
290 unsigned long end_pfn)
292 unsigned long old_zone_end_pfn;
294 zone_span_writelock(zone);
296 old_zone_end_pfn = zone_end_pfn(zone);
297 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
298 zone->zone_start_pfn = start_pfn;
300 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
301 zone->zone_start_pfn;
303 zone_span_writeunlock(zone);
306 static void resize_zone(struct zone *zone, unsigned long start_pfn,
307 unsigned long end_pfn)
309 zone_span_writelock(zone);
311 if (end_pfn - start_pfn) {
312 zone->zone_start_pfn = start_pfn;
313 zone->spanned_pages = end_pfn - start_pfn;
316 * make it consist as free_area_init_core(),
317 * if spanned_pages = 0, then keep start_pfn = 0
319 zone->zone_start_pfn = 0;
320 zone->spanned_pages = 0;
323 zone_span_writeunlock(zone);
326 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
327 unsigned long end_pfn)
329 enum zone_type zid = zone_idx(zone);
330 int nid = zone->zone_pgdat->node_id;
333 for (pfn = start_pfn; pfn < end_pfn; pfn++)
334 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
337 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
338 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
339 static int __ref ensure_zone_is_initialized(struct zone *zone,
340 unsigned long start_pfn, unsigned long num_pages)
342 if (!zone_is_initialized(zone))
343 return init_currently_empty_zone(zone, start_pfn, num_pages);
348 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
349 unsigned long start_pfn, unsigned long end_pfn)
353 unsigned long z1_start_pfn;
355 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
359 pgdat_resize_lock(z1->zone_pgdat, &flags);
361 /* can't move pfns which are higher than @z2 */
362 if (end_pfn > zone_end_pfn(z2))
364 /* the move out part must be at the left most of @z2 */
365 if (start_pfn > z2->zone_start_pfn)
367 /* must included/overlap */
368 if (end_pfn <= z2->zone_start_pfn)
371 /* use start_pfn for z1's start_pfn if z1 is empty */
372 if (!zone_is_empty(z1))
373 z1_start_pfn = z1->zone_start_pfn;
375 z1_start_pfn = start_pfn;
377 resize_zone(z1, z1_start_pfn, end_pfn);
378 resize_zone(z2, end_pfn, zone_end_pfn(z2));
380 pgdat_resize_unlock(z1->zone_pgdat, &flags);
382 fix_zone_id(z1, start_pfn, end_pfn);
386 pgdat_resize_unlock(z1->zone_pgdat, &flags);
390 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
391 unsigned long start_pfn, unsigned long end_pfn)
395 unsigned long z2_end_pfn;
397 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
401 pgdat_resize_lock(z1->zone_pgdat, &flags);
403 /* can't move pfns which are lower than @z1 */
404 if (z1->zone_start_pfn > start_pfn)
406 /* the move out part mast at the right most of @z1 */
407 if (zone_end_pfn(z1) > end_pfn)
409 /* must included/overlap */
410 if (start_pfn >= zone_end_pfn(z1))
413 /* use end_pfn for z2's end_pfn if z2 is empty */
414 if (!zone_is_empty(z2))
415 z2_end_pfn = zone_end_pfn(z2);
417 z2_end_pfn = end_pfn;
419 resize_zone(z1, z1->zone_start_pfn, start_pfn);
420 resize_zone(z2, start_pfn, z2_end_pfn);
422 pgdat_resize_unlock(z1->zone_pgdat, &flags);
424 fix_zone_id(z2, start_pfn, end_pfn);
428 pgdat_resize_unlock(z1->zone_pgdat, &flags);
432 static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
433 unsigned long end_pfn)
435 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
437 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
438 pgdat->node_start_pfn = start_pfn;
440 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
441 pgdat->node_start_pfn;
444 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
446 struct pglist_data *pgdat = zone->zone_pgdat;
447 int nr_pages = PAGES_PER_SECTION;
448 int nid = pgdat->node_id;
450 unsigned long flags, pfn;
453 zone_type = zone - pgdat->node_zones;
454 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
458 pgdat_resize_lock(zone->zone_pgdat, &flags);
459 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
460 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
461 phys_start_pfn + nr_pages);
462 pgdat_resize_unlock(zone->zone_pgdat, &flags);
463 memmap_init_zone(nr_pages, nid, zone_type,
464 phys_start_pfn, MEMMAP_HOTPLUG);
466 /* online_page_range is called later and expects pages reserved */
467 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
471 SetPageReserved(pfn_to_page(pfn));
476 static int __meminit __add_section(int nid, struct zone *zone,
477 unsigned long phys_start_pfn)
481 if (pfn_valid(phys_start_pfn))
484 ret = sparse_add_one_section(zone, phys_start_pfn);
489 ret = __add_zone(zone, phys_start_pfn);
494 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
498 * Reasonably generic function for adding memory. It is
499 * expected that archs that support memory hotplug will
500 * call this function after deciding the zone to which to
503 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
504 unsigned long nr_pages)
508 int start_sec, end_sec;
509 /* during initialize mem_map, align hot-added range to section */
510 start_sec = pfn_to_section_nr(phys_start_pfn);
511 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
513 for (i = start_sec; i <= end_sec; i++) {
514 err = __add_section(nid, zone, section_nr_to_pfn(i));
517 * EEXIST is finally dealt with by ioresource collision
518 * check. see add_memory() => register_memory_resource()
519 * Warning will be printed if there is collision.
521 if (err && (err != -EEXIST))
525 vmemmap_populate_print_last();
529 EXPORT_SYMBOL_GPL(__add_pages);
531 #ifdef CONFIG_MEMORY_HOTREMOVE
532 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
533 static int find_smallest_section_pfn(int nid, struct zone *zone,
534 unsigned long start_pfn,
535 unsigned long end_pfn)
537 struct mem_section *ms;
539 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
540 ms = __pfn_to_section(start_pfn);
542 if (unlikely(!valid_section(ms)))
545 if (unlikely(pfn_to_nid(start_pfn) != nid))
548 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
557 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
558 static int find_biggest_section_pfn(int nid, struct zone *zone,
559 unsigned long start_pfn,
560 unsigned long end_pfn)
562 struct mem_section *ms;
565 /* pfn is the end pfn of a memory section. */
567 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
568 ms = __pfn_to_section(pfn);
570 if (unlikely(!valid_section(ms)))
573 if (unlikely(pfn_to_nid(pfn) != nid))
576 if (zone && zone != page_zone(pfn_to_page(pfn)))
585 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
586 unsigned long end_pfn)
588 unsigned long zone_start_pfn = zone->zone_start_pfn;
589 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
590 unsigned long zone_end_pfn = z;
592 struct mem_section *ms;
593 int nid = zone_to_nid(zone);
595 zone_span_writelock(zone);
596 if (zone_start_pfn == start_pfn) {
598 * If the section is smallest section in the zone, it need
599 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
600 * In this case, we find second smallest valid mem_section
601 * for shrinking zone.
603 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
606 zone->zone_start_pfn = pfn;
607 zone->spanned_pages = zone_end_pfn - pfn;
609 } else if (zone_end_pfn == end_pfn) {
611 * If the section is biggest section in the zone, it need
612 * shrink zone->spanned_pages.
613 * In this case, we find second biggest valid mem_section for
616 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
619 zone->spanned_pages = pfn - zone_start_pfn + 1;
623 * The section is not biggest or smallest mem_section in the zone, it
624 * only creates a hole in the zone. So in this case, we need not
625 * change the zone. But perhaps, the zone has only hole data. Thus
626 * it check the zone has only hole or not.
628 pfn = zone_start_pfn;
629 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
630 ms = __pfn_to_section(pfn);
632 if (unlikely(!valid_section(ms)))
635 if (page_zone(pfn_to_page(pfn)) != zone)
638 /* If the section is current section, it continues the loop */
639 if (start_pfn == pfn)
642 /* If we find valid section, we have nothing to do */
643 zone_span_writeunlock(zone);
647 /* The zone has no valid section */
648 zone->zone_start_pfn = 0;
649 zone->spanned_pages = 0;
650 zone_span_writeunlock(zone);
653 static void shrink_pgdat_span(struct pglist_data *pgdat,
654 unsigned long start_pfn, unsigned long end_pfn)
656 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
657 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
658 unsigned long pgdat_end_pfn = p;
660 struct mem_section *ms;
661 int nid = pgdat->node_id;
663 if (pgdat_start_pfn == start_pfn) {
665 * If the section is smallest section in the pgdat, it need
666 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
667 * In this case, we find second smallest valid mem_section
668 * for shrinking zone.
670 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
673 pgdat->node_start_pfn = pfn;
674 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
676 } else if (pgdat_end_pfn == end_pfn) {
678 * If the section is biggest section in the pgdat, it need
679 * shrink pgdat->node_spanned_pages.
680 * In this case, we find second biggest valid mem_section for
683 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
686 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
690 * If the section is not biggest or smallest mem_section in the pgdat,
691 * it only creates a hole in the pgdat. So in this case, we need not
693 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
694 * has only hole or not.
696 pfn = pgdat_start_pfn;
697 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
698 ms = __pfn_to_section(pfn);
700 if (unlikely(!valid_section(ms)))
703 if (pfn_to_nid(pfn) != nid)
706 /* If the section is current section, it continues the loop */
707 if (start_pfn == pfn)
710 /* If we find valid section, we have nothing to do */
714 /* The pgdat has no valid section */
715 pgdat->node_start_pfn = 0;
716 pgdat->node_spanned_pages = 0;
719 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
721 struct pglist_data *pgdat = zone->zone_pgdat;
722 int nr_pages = PAGES_PER_SECTION;
726 zone_type = zone - pgdat->node_zones;
728 pgdat_resize_lock(zone->zone_pgdat, &flags);
729 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
730 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
731 pgdat_resize_unlock(zone->zone_pgdat, &flags);
734 static int __remove_section(struct zone *zone, struct mem_section *ms)
736 unsigned long start_pfn;
740 if (!valid_section(ms))
743 ret = unregister_memory_section(ms);
747 scn_nr = __section_nr(ms);
748 start_pfn = section_nr_to_pfn(scn_nr);
749 __remove_zone(zone, start_pfn);
751 sparse_remove_one_section(zone, ms);
756 * __remove_pages() - remove sections of pages from a zone
757 * @zone: zone from which pages need to be removed
758 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
759 * @nr_pages: number of pages to remove (must be multiple of section size)
761 * Generic helper function to remove section mappings and sysfs entries
762 * for the section of the memory we are removing. Caller needs to make
763 * sure that pages are marked reserved and zones are adjust properly by
764 * calling offline_pages().
766 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
767 unsigned long nr_pages)
770 int sections_to_remove;
771 resource_size_t start, size;
775 * We can only remove entire sections
777 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
778 BUG_ON(nr_pages % PAGES_PER_SECTION);
780 start = phys_start_pfn << PAGE_SHIFT;
781 size = nr_pages * PAGE_SIZE;
783 /* in the ZONE_DEVICE case device driver owns the memory region */
784 if (!is_dev_zone(zone))
785 ret = release_mem_region_adjustable(&iomem_resource, start, size);
787 resource_size_t endres = start + size - 1;
789 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
790 &start, &endres, ret);
793 sections_to_remove = nr_pages / PAGES_PER_SECTION;
794 for (i = 0; i < sections_to_remove; i++) {
795 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
796 ret = __remove_section(zone, __pfn_to_section(pfn));
802 EXPORT_SYMBOL_GPL(__remove_pages);
803 #endif /* CONFIG_MEMORY_HOTREMOVE */
805 int set_online_page_callback(online_page_callback_t callback)
810 mutex_lock(&online_page_callback_lock);
812 if (online_page_callback == generic_online_page) {
813 online_page_callback = callback;
817 mutex_unlock(&online_page_callback_lock);
822 EXPORT_SYMBOL_GPL(set_online_page_callback);
824 int restore_online_page_callback(online_page_callback_t callback)
829 mutex_lock(&online_page_callback_lock);
831 if (online_page_callback == callback) {
832 online_page_callback = generic_online_page;
836 mutex_unlock(&online_page_callback_lock);
841 EXPORT_SYMBOL_GPL(restore_online_page_callback);
843 void __online_page_set_limits(struct page *page)
846 EXPORT_SYMBOL_GPL(__online_page_set_limits);
848 void __online_page_increment_counters(struct page *page)
850 adjust_managed_page_count(page, 1);
852 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
854 void __online_page_free(struct page *page)
856 __free_reserved_page(page);
858 EXPORT_SYMBOL_GPL(__online_page_free);
860 static void generic_online_page(struct page *page)
862 __online_page_set_limits(page);
863 __online_page_increment_counters(page);
864 __online_page_free(page);
867 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
871 unsigned long onlined_pages = *(unsigned long *)arg;
873 if (PageReserved(pfn_to_page(start_pfn)))
874 for (i = 0; i < nr_pages; i++) {
875 page = pfn_to_page(start_pfn + i);
876 (*online_page_callback)(page);
879 *(unsigned long *)arg = onlined_pages;
883 #ifdef CONFIG_MOVABLE_NODE
885 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
888 static bool can_online_high_movable(struct zone *zone)
892 #else /* CONFIG_MOVABLE_NODE */
893 /* ensure every online node has NORMAL memory */
894 static bool can_online_high_movable(struct zone *zone)
896 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
898 #endif /* CONFIG_MOVABLE_NODE */
900 /* check which state of node_states will be changed when online memory */
901 static void node_states_check_changes_online(unsigned long nr_pages,
902 struct zone *zone, struct memory_notify *arg)
904 int nid = zone_to_nid(zone);
905 enum zone_type zone_last = ZONE_NORMAL;
908 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
909 * contains nodes which have zones of 0...ZONE_NORMAL,
910 * set zone_last to ZONE_NORMAL.
912 * If we don't have HIGHMEM nor movable node,
913 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
914 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
916 if (N_MEMORY == N_NORMAL_MEMORY)
917 zone_last = ZONE_MOVABLE;
920 * if the memory to be online is in a zone of 0...zone_last, and
921 * the zones of 0...zone_last don't have memory before online, we will
922 * need to set the node to node_states[N_NORMAL_MEMORY] after
923 * the memory is online.
925 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
926 arg->status_change_nid_normal = nid;
928 arg->status_change_nid_normal = -1;
930 #ifdef CONFIG_HIGHMEM
932 * If we have movable node, node_states[N_HIGH_MEMORY]
933 * contains nodes which have zones of 0...ZONE_HIGHMEM,
934 * set zone_last to ZONE_HIGHMEM.
936 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
937 * contains nodes which have zones of 0...ZONE_MOVABLE,
938 * set zone_last to ZONE_MOVABLE.
940 zone_last = ZONE_HIGHMEM;
941 if (N_MEMORY == N_HIGH_MEMORY)
942 zone_last = ZONE_MOVABLE;
944 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
945 arg->status_change_nid_high = nid;
947 arg->status_change_nid_high = -1;
949 arg->status_change_nid_high = arg->status_change_nid_normal;
953 * if the node don't have memory befor online, we will need to
954 * set the node to node_states[N_MEMORY] after the memory
957 if (!node_state(nid, N_MEMORY))
958 arg->status_change_nid = nid;
960 arg->status_change_nid = -1;
963 static void node_states_set_node(int node, struct memory_notify *arg)
965 if (arg->status_change_nid_normal >= 0)
966 node_set_state(node, N_NORMAL_MEMORY);
968 if (arg->status_change_nid_high >= 0)
969 node_set_state(node, N_HIGH_MEMORY);
971 node_set_state(node, N_MEMORY);
975 /* Must be protected by mem_hotplug_begin() */
976 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
979 unsigned long onlined_pages = 0;
981 int need_zonelists_rebuild = 0;
984 struct memory_notify arg;
987 * This doesn't need a lock to do pfn_to_page().
988 * The section can't be removed here because of the
989 * memory_block->state_mutex.
991 zone = page_zone(pfn_to_page(pfn));
993 if ((zone_idx(zone) > ZONE_NORMAL ||
994 online_type == MMOP_ONLINE_MOVABLE) &&
995 !can_online_high_movable(zone))
998 if (online_type == MMOP_ONLINE_KERNEL &&
999 zone_idx(zone) == ZONE_MOVABLE) {
1000 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
1003 if (online_type == MMOP_ONLINE_MOVABLE &&
1004 zone_idx(zone) == ZONE_MOVABLE - 1) {
1005 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
1009 /* Previous code may changed the zone of the pfn range */
1010 zone = page_zone(pfn_to_page(pfn));
1012 arg.start_pfn = pfn;
1013 arg.nr_pages = nr_pages;
1014 node_states_check_changes_online(nr_pages, zone, &arg);
1016 nid = zone_to_nid(zone);
1018 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1019 ret = notifier_to_errno(ret);
1021 memory_notify(MEM_CANCEL_ONLINE, &arg);
1025 * If this zone is not populated, then it is not in zonelist.
1026 * This means the page allocator ignores this zone.
1027 * So, zonelist must be updated after online.
1029 mutex_lock(&zonelists_mutex);
1030 if (!populated_zone(zone)) {
1031 need_zonelists_rebuild = 1;
1032 build_all_zonelists(NULL, zone);
1035 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1036 online_pages_range);
1038 if (need_zonelists_rebuild)
1039 zone_pcp_reset(zone);
1040 mutex_unlock(&zonelists_mutex);
1041 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1042 (unsigned long long) pfn << PAGE_SHIFT,
1043 (((unsigned long long) pfn + nr_pages)
1044 << PAGE_SHIFT) - 1);
1045 memory_notify(MEM_CANCEL_ONLINE, &arg);
1049 zone->present_pages += onlined_pages;
1051 pgdat_resize_lock(zone->zone_pgdat, &flags);
1052 zone->zone_pgdat->node_present_pages += onlined_pages;
1053 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1055 if (onlined_pages) {
1056 node_states_set_node(nid, &arg);
1057 if (need_zonelists_rebuild)
1058 build_all_zonelists(NULL, NULL);
1060 zone_pcp_update(zone);
1063 mutex_unlock(&zonelists_mutex);
1065 init_per_zone_wmark_min();
1067 if (onlined_pages) {
1072 vm_total_pages = nr_free_pagecache_pages();
1074 writeback_set_ratelimit();
1077 memory_notify(MEM_ONLINE, &arg);
1080 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1082 static void reset_node_present_pages(pg_data_t *pgdat)
1086 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1087 z->present_pages = 0;
1089 pgdat->node_present_pages = 0;
1092 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1093 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1095 struct pglist_data *pgdat;
1096 unsigned long zones_size[MAX_NR_ZONES] = {0};
1097 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1098 unsigned long start_pfn = PFN_DOWN(start);
1100 pgdat = NODE_DATA(nid);
1102 pgdat = arch_alloc_nodedata(nid);
1106 arch_refresh_nodedata(nid, pgdat);
1108 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1109 pgdat->nr_zones = 0;
1110 pgdat->classzone_idx = 0;
1113 /* we can use NODE_DATA(nid) from here */
1115 /* init node's zones as empty zones, we don't have any present pages.*/
1116 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1119 * The node we allocated has no zone fallback lists. For avoiding
1120 * to access not-initialized zonelist, build here.
1122 mutex_lock(&zonelists_mutex);
1123 build_all_zonelists(pgdat, NULL);
1124 mutex_unlock(&zonelists_mutex);
1127 * zone->managed_pages is set to an approximate value in
1128 * free_area_init_core(), which will cause
1129 * /sys/device/system/node/nodeX/meminfo has wrong data.
1130 * So reset it to 0 before any memory is onlined.
1132 reset_node_managed_pages(pgdat);
1135 * When memory is hot-added, all the memory is in offline state. So
1136 * clear all zones' present_pages because they will be updated in
1137 * online_pages() and offline_pages().
1139 reset_node_present_pages(pgdat);
1144 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1146 arch_refresh_nodedata(nid, NULL);
1147 arch_free_nodedata(pgdat);
1153 * try_online_node - online a node if offlined
1155 * called by cpu_up() to online a node without onlined memory.
1157 int try_online_node(int nid)
1162 if (node_online(nid))
1165 mem_hotplug_begin();
1166 pgdat = hotadd_new_pgdat(nid, 0);
1168 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1172 node_set_online(nid);
1173 ret = register_one_node(nid);
1176 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1177 mutex_lock(&zonelists_mutex);
1178 build_all_zonelists(NULL, NULL);
1179 mutex_unlock(&zonelists_mutex);
1187 static int check_hotplug_memory_range(u64 start, u64 size)
1189 u64 start_pfn = PFN_DOWN(start);
1190 u64 nr_pages = size >> PAGE_SHIFT;
1192 /* Memory range must be aligned with section */
1193 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1194 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1195 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1196 (unsigned long long)start,
1197 (unsigned long long)size);
1205 * If movable zone has already been setup, newly added memory should be check.
1206 * If its address is higher than movable zone, it should be added as movable.
1207 * Without this check, movable zone may overlap with other zone.
1209 static int should_add_memory_movable(int nid, u64 start, u64 size)
1211 unsigned long start_pfn = start >> PAGE_SHIFT;
1212 pg_data_t *pgdat = NODE_DATA(nid);
1213 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1215 if (zone_is_empty(movable_zone))
1218 if (movable_zone->zone_start_pfn <= start_pfn)
1224 int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1227 #ifdef CONFIG_ZONE_DEVICE
1231 if (should_add_memory_movable(nid, start, size))
1232 return ZONE_MOVABLE;
1234 return zone_default;
1237 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1238 int __ref add_memory_resource(int nid, struct resource *res)
1241 pg_data_t *pgdat = NULL;
1247 size = resource_size(res);
1249 ret = check_hotplug_memory_range(start, size);
1253 { /* Stupid hack to suppress address-never-null warning */
1254 void *p = NODE_DATA(nid);
1258 mem_hotplug_begin();
1261 * Add new range to memblock so that when hotadd_new_pgdat() is called
1262 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1263 * this new range and calculate total pages correctly. The range will
1264 * be removed at hot-remove time.
1266 memblock_add_node(start, size, nid);
1268 new_node = !node_online(nid);
1270 pgdat = hotadd_new_pgdat(nid, start);
1276 /* call arch's memory hotadd */
1277 ret = arch_add_memory(nid, start, size, false);
1282 /* we online node here. we can't roll back from here. */
1283 node_set_online(nid);
1286 ret = register_one_node(nid);
1288 * If sysfs file of new node can't create, cpu on the node
1289 * can't be hot-added. There is no rollback way now.
1290 * So, check by BUG_ON() to catch it reluctantly..
1295 /* create new memmap entry */
1296 firmware_map_add_hotplug(start, start + size, "System RAM");
1301 /* rollback pgdat allocation and others */
1303 rollback_node_hotadd(nid, pgdat);
1304 memblock_remove(start, size);
1310 EXPORT_SYMBOL_GPL(add_memory_resource);
1312 int __ref add_memory(int nid, u64 start, u64 size)
1314 struct resource *res;
1317 res = register_memory_resource(start, size);
1321 ret = add_memory_resource(nid, res);
1323 release_memory_resource(res);
1326 EXPORT_SYMBOL_GPL(add_memory);
1328 #ifdef CONFIG_MEMORY_HOTREMOVE
1330 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1331 * set and the size of the free page is given by page_order(). Using this,
1332 * the function determines if the pageblock contains only free pages.
1333 * Due to buddy contraints, a free page at least the size of a pageblock will
1334 * be located at the start of the pageblock
1336 static inline int pageblock_free(struct page *page)
1338 return PageBuddy(page) && page_order(page) >= pageblock_order;
1341 /* Return the start of the next active pageblock after a given page */
1342 static struct page *next_active_pageblock(struct page *page)
1344 /* Ensure the starting page is pageblock-aligned */
1345 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1347 /* If the entire pageblock is free, move to the end of free page */
1348 if (pageblock_free(page)) {
1350 /* be careful. we don't have locks, page_order can be changed.*/
1351 order = page_order(page);
1352 if ((order < MAX_ORDER) && (order >= pageblock_order))
1353 return page + (1 << order);
1356 return page + pageblock_nr_pages;
1359 /* Checks if this range of memory is likely to be hot-removable. */
1360 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1362 struct page *page = pfn_to_page(start_pfn);
1363 struct page *end_page = page + nr_pages;
1365 /* Check the starting page of each pageblock within the range */
1366 for (; page < end_page; page = next_active_pageblock(page)) {
1367 if (!is_pageblock_removable_nolock(page))
1372 /* All pageblocks in the memory block are likely to be hot-removable */
1377 * Confirm all pages in a range [start, end) belong to the same zone.
1378 * When true, return its valid [start, end).
1380 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1381 unsigned long *valid_start, unsigned long *valid_end)
1383 unsigned long pfn, sec_end_pfn;
1384 unsigned long start, end;
1385 struct zone *zone = NULL;
1388 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1390 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1391 /* Make sure the memory section is present first */
1392 if (!present_section_nr(pfn_to_section_nr(pfn)))
1394 for (; pfn < sec_end_pfn && pfn < end_pfn;
1395 pfn += MAX_ORDER_NR_PAGES) {
1397 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1398 while ((i < MAX_ORDER_NR_PAGES) &&
1399 !pfn_valid_within(pfn + i))
1401 if (i == MAX_ORDER_NR_PAGES)
1403 page = pfn_to_page(pfn + i);
1404 if (zone && page_zone(page) != zone)
1408 zone = page_zone(page);
1409 end = pfn + MAX_ORDER_NR_PAGES;
1414 *valid_start = start;
1423 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1424 * and hugepages). We scan pfn because it's much easier than scanning over
1425 * linked list. This function returns the pfn of the first found movable
1426 * page if it's found, otherwise 0.
1428 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1432 for (pfn = start; pfn < end; pfn++) {
1433 if (pfn_valid(pfn)) {
1434 page = pfn_to_page(pfn);
1437 if (PageHuge(page)) {
1438 if (page_huge_active(page))
1441 pfn = round_up(pfn + 1,
1442 1 << compound_order(page)) - 1;
1449 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1451 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1455 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1456 int not_managed = 0;
1460 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1461 if (!pfn_valid(pfn))
1463 page = pfn_to_page(pfn);
1465 if (PageHuge(page)) {
1466 struct page *head = compound_head(page);
1467 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1468 if (compound_order(head) > PFN_SECTION_SHIFT) {
1472 if (isolate_huge_page(page, &source))
1473 move_pages -= 1 << compound_order(head);
1477 if (!get_page_unless_zero(page))
1480 * We can skip free pages. And we can only deal with pages on
1483 ret = isolate_lru_page(page);
1484 if (!ret) { /* Success */
1486 list_add_tail(&page->lru, &source);
1488 inc_zone_page_state(page, NR_ISOLATED_ANON +
1489 page_is_file_cache(page));
1492 #ifdef CONFIG_DEBUG_VM
1493 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1495 dump_page(page, "failed to remove from LRU");
1498 /* Because we don't have big zone->lock. we should
1499 check this again here. */
1500 if (page_count(page)) {
1507 if (!list_empty(&source)) {
1509 putback_movable_pages(&source);
1514 * alloc_migrate_target should be improooooved!!
1515 * migrate_pages returns # of failed pages.
1517 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
1518 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1520 putback_movable_pages(&source);
1527 * remove from free_area[] and mark all as Reserved.
1530 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1533 __offline_isolated_pages(start, start + nr_pages);
1538 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1540 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1541 offline_isolated_pages_cb);
1545 * Check all pages in range, recoreded as memory resource, are isolated.
1548 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1552 long offlined = *(long *)data;
1553 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1554 offlined = nr_pages;
1556 *(long *)data += offlined;
1561 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1566 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1567 check_pages_isolated_cb);
1569 offlined = (long)ret;
1573 #ifdef CONFIG_MOVABLE_NODE
1575 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1578 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1582 #else /* CONFIG_MOVABLE_NODE */
1583 /* ensure the node has NORMAL memory if it is still online */
1584 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1586 struct pglist_data *pgdat = zone->zone_pgdat;
1587 unsigned long present_pages = 0;
1590 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1591 present_pages += pgdat->node_zones[zt].present_pages;
1593 if (present_pages > nr_pages)
1597 for (; zt <= ZONE_MOVABLE; zt++)
1598 present_pages += pgdat->node_zones[zt].present_pages;
1601 * we can't offline the last normal memory until all
1602 * higher memory is offlined.
1604 return present_pages == 0;
1606 #endif /* CONFIG_MOVABLE_NODE */
1608 static int __init cmdline_parse_movable_node(char *p)
1610 #ifdef CONFIG_MOVABLE_NODE
1612 * Memory used by the kernel cannot be hot-removed because Linux
1613 * cannot migrate the kernel pages. When memory hotplug is
1614 * enabled, we should prevent memblock from allocating memory
1617 * ACPI SRAT records all hotpluggable memory ranges. But before
1618 * SRAT is parsed, we don't know about it.
1620 * The kernel image is loaded into memory at very early time. We
1621 * cannot prevent this anyway. So on NUMA system, we set any
1622 * node the kernel resides in as un-hotpluggable.
1624 * Since on modern servers, one node could have double-digit
1625 * gigabytes memory, we can assume the memory around the kernel
1626 * image is also un-hotpluggable. So before SRAT is parsed, just
1627 * allocate memory near the kernel image to try the best to keep
1628 * the kernel away from hotpluggable memory.
1630 memblock_set_bottom_up(true);
1631 movable_node_enabled = true;
1633 pr_warn("movable_node option not supported\n");
1637 early_param("movable_node", cmdline_parse_movable_node);
1639 /* check which state of node_states will be changed when offline memory */
1640 static void node_states_check_changes_offline(unsigned long nr_pages,
1641 struct zone *zone, struct memory_notify *arg)
1643 struct pglist_data *pgdat = zone->zone_pgdat;
1644 unsigned long present_pages = 0;
1645 enum zone_type zt, zone_last = ZONE_NORMAL;
1648 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1649 * contains nodes which have zones of 0...ZONE_NORMAL,
1650 * set zone_last to ZONE_NORMAL.
1652 * If we don't have HIGHMEM nor movable node,
1653 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1654 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1656 if (N_MEMORY == N_NORMAL_MEMORY)
1657 zone_last = ZONE_MOVABLE;
1660 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1661 * If the memory to be offline is in a zone of 0...zone_last,
1662 * and it is the last present memory, 0...zone_last will
1663 * become empty after offline , thus we can determind we will
1664 * need to clear the node from node_states[N_NORMAL_MEMORY].
1666 for (zt = 0; zt <= zone_last; zt++)
1667 present_pages += pgdat->node_zones[zt].present_pages;
1668 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1669 arg->status_change_nid_normal = zone_to_nid(zone);
1671 arg->status_change_nid_normal = -1;
1673 #ifdef CONFIG_HIGHMEM
1675 * If we have movable node, node_states[N_HIGH_MEMORY]
1676 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1677 * set zone_last to ZONE_HIGHMEM.
1679 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1680 * contains nodes which have zones of 0...ZONE_MOVABLE,
1681 * set zone_last to ZONE_MOVABLE.
1683 zone_last = ZONE_HIGHMEM;
1684 if (N_MEMORY == N_HIGH_MEMORY)
1685 zone_last = ZONE_MOVABLE;
1687 for (; zt <= zone_last; zt++)
1688 present_pages += pgdat->node_zones[zt].present_pages;
1689 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1690 arg->status_change_nid_high = zone_to_nid(zone);
1692 arg->status_change_nid_high = -1;
1694 arg->status_change_nid_high = arg->status_change_nid_normal;
1698 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1700 zone_last = ZONE_MOVABLE;
1703 * check whether node_states[N_HIGH_MEMORY] will be changed
1704 * If we try to offline the last present @nr_pages from the node,
1705 * we can determind we will need to clear the node from
1706 * node_states[N_HIGH_MEMORY].
1708 for (; zt <= zone_last; zt++)
1709 present_pages += pgdat->node_zones[zt].present_pages;
1710 if (nr_pages >= present_pages)
1711 arg->status_change_nid = zone_to_nid(zone);
1713 arg->status_change_nid = -1;
1716 static void node_states_clear_node(int node, struct memory_notify *arg)
1718 if (arg->status_change_nid_normal >= 0)
1719 node_clear_state(node, N_NORMAL_MEMORY);
1721 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1722 (arg->status_change_nid_high >= 0))
1723 node_clear_state(node, N_HIGH_MEMORY);
1725 if ((N_MEMORY != N_HIGH_MEMORY) &&
1726 (arg->status_change_nid >= 0))
1727 node_clear_state(node, N_MEMORY);
1730 static int __ref __offline_pages(unsigned long start_pfn,
1731 unsigned long end_pfn, unsigned long timeout)
1733 unsigned long pfn, nr_pages, expire;
1734 long offlined_pages;
1735 int ret, drain, retry_max, node;
1736 unsigned long flags;
1737 unsigned long valid_start, valid_end;
1739 struct memory_notify arg;
1741 /* at least, alignment against pageblock is necessary */
1742 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1744 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1746 /* This makes hotplug much easier...and readable.
1747 we assume this for now. .*/
1748 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1751 zone = page_zone(pfn_to_page(valid_start));
1752 node = zone_to_nid(zone);
1753 nr_pages = end_pfn - start_pfn;
1755 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1758 /* set above range as isolated */
1759 ret = start_isolate_page_range(start_pfn, end_pfn,
1760 MIGRATE_MOVABLE, true);
1764 arg.start_pfn = start_pfn;
1765 arg.nr_pages = nr_pages;
1766 node_states_check_changes_offline(nr_pages, zone, &arg);
1768 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1769 ret = notifier_to_errno(ret);
1771 goto failed_removal;
1774 expire = jiffies + timeout;
1778 /* start memory hot removal */
1780 if (time_after(jiffies, expire))
1781 goto failed_removal;
1783 if (signal_pending(current))
1784 goto failed_removal;
1787 lru_add_drain_all();
1789 drain_all_pages(zone);
1792 pfn = scan_movable_pages(start_pfn, end_pfn);
1793 if (pfn) { /* We have movable pages */
1794 ret = do_migrate_range(pfn, end_pfn);
1800 if (--retry_max == 0)
1801 goto failed_removal;
1807 /* drain all zone's lru pagevec, this is asynchronous... */
1808 lru_add_drain_all();
1810 /* drain pcp pages, this is synchronous. */
1811 drain_all_pages(zone);
1813 * dissolve free hugepages in the memory block before doing offlining
1814 * actually in order to make hugetlbfs's object counting consistent.
1816 dissolve_free_huge_pages(start_pfn, end_pfn);
1818 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1819 if (offlined_pages < 0) {
1821 goto failed_removal;
1823 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1824 /* Ok, all of our target is isolated.
1825 We cannot do rollback at this point. */
1826 offline_isolated_pages(start_pfn, end_pfn);
1827 /* reset pagetype flags and makes migrate type to be MOVABLE */
1828 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1829 /* removal success */
1830 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1831 zone->present_pages -= offlined_pages;
1833 pgdat_resize_lock(zone->zone_pgdat, &flags);
1834 zone->zone_pgdat->node_present_pages -= offlined_pages;
1835 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1837 init_per_zone_wmark_min();
1839 if (!populated_zone(zone)) {
1840 zone_pcp_reset(zone);
1841 mutex_lock(&zonelists_mutex);
1842 build_all_zonelists(NULL, NULL);
1843 mutex_unlock(&zonelists_mutex);
1845 zone_pcp_update(zone);
1847 node_states_clear_node(node, &arg);
1848 if (arg.status_change_nid >= 0) {
1850 kcompactd_stop(node);
1853 vm_total_pages = nr_free_pagecache_pages();
1854 writeback_set_ratelimit();
1856 memory_notify(MEM_OFFLINE, &arg);
1860 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1861 (unsigned long long) start_pfn << PAGE_SHIFT,
1862 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1863 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1864 /* pushback to free area */
1865 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1869 /* Must be protected by mem_hotplug_begin() */
1870 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1872 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1874 #endif /* CONFIG_MEMORY_HOTREMOVE */
1877 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1878 * @start_pfn: start pfn of the memory range
1879 * @end_pfn: end pfn of the memory range
1880 * @arg: argument passed to func
1881 * @func: callback for each memory section walked
1883 * This function walks through all present mem sections in range
1884 * [start_pfn, end_pfn) and call func on each mem section.
1886 * Returns the return value of func.
1888 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1889 void *arg, int (*func)(struct memory_block *, void *))
1891 struct memory_block *mem = NULL;
1892 struct mem_section *section;
1893 unsigned long pfn, section_nr;
1896 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1897 section_nr = pfn_to_section_nr(pfn);
1898 if (!present_section_nr(section_nr))
1901 section = __nr_to_section(section_nr);
1902 /* same memblock? */
1904 if ((section_nr >= mem->start_section_nr) &&
1905 (section_nr <= mem->end_section_nr))
1908 mem = find_memory_block_hinted(section, mem);
1912 ret = func(mem, arg);
1914 kobject_put(&mem->dev.kobj);
1920 kobject_put(&mem->dev.kobj);
1925 #ifdef CONFIG_MEMORY_HOTREMOVE
1926 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1928 int ret = !is_memblock_offlined(mem);
1930 if (unlikely(ret)) {
1931 phys_addr_t beginpa, endpa;
1933 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1934 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1935 pr_warn("removing memory fails, because memory "
1936 "[%pa-%pa] is onlined\n",
1943 static int check_cpu_on_node(pg_data_t *pgdat)
1947 for_each_present_cpu(cpu) {
1948 if (cpu_to_node(cpu) == pgdat->node_id)
1950 * the cpu on this node isn't removed, and we can't
1951 * offline this node.
1959 static void unmap_cpu_on_node(pg_data_t *pgdat)
1961 #ifdef CONFIG_ACPI_NUMA
1964 for_each_possible_cpu(cpu)
1965 if (cpu_to_node(cpu) == pgdat->node_id)
1966 numa_clear_node(cpu);
1970 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1974 ret = check_cpu_on_node(pgdat);
1979 * the node will be offlined when we come here, so we can clear
1980 * the cpu_to_node() now.
1983 unmap_cpu_on_node(pgdat);
1990 * Offline a node if all memory sections and cpus of the node are removed.
1992 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1993 * and online/offline operations before this call.
1995 void try_offline_node(int nid)
1997 pg_data_t *pgdat = NODE_DATA(nid);
1998 unsigned long start_pfn = pgdat->node_start_pfn;
1999 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
2003 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2004 unsigned long section_nr = pfn_to_section_nr(pfn);
2006 if (!present_section_nr(section_nr))
2009 if (pfn_to_nid(pfn) != nid)
2013 * some memory sections of this node are not removed, and we
2014 * can't offline node now.
2019 if (check_and_unmap_cpu_on_node(pgdat))
2023 * all memory/cpu of this node are removed, we can offline this
2026 node_set_offline(nid);
2027 unregister_one_node(nid);
2029 /* free waittable in each zone */
2030 for (i = 0; i < MAX_NR_ZONES; i++) {
2031 struct zone *zone = pgdat->node_zones + i;
2034 * wait_table may be allocated from boot memory,
2035 * here only free if it's allocated by vmalloc.
2037 if (is_vmalloc_addr(zone->wait_table)) {
2038 vfree(zone->wait_table);
2039 zone->wait_table = NULL;
2043 EXPORT_SYMBOL(try_offline_node);
2048 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2049 * and online/offline operations before this call, as required by
2050 * try_offline_node().
2052 void __ref remove_memory(int nid, u64 start, u64 size)
2056 BUG_ON(check_hotplug_memory_range(start, size));
2058 mem_hotplug_begin();
2061 * All memory blocks must be offlined before removing memory. Check
2062 * whether all memory blocks in question are offline and trigger a BUG()
2063 * if this is not the case.
2065 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2066 check_memblock_offlined_cb);
2070 /* remove memmap entry */
2071 firmware_map_remove(start, start + size, "System RAM");
2072 memblock_free(start, size);
2073 memblock_remove(start, size);
2075 arch_remove_memory(start, size);
2077 try_offline_node(nid);
2081 EXPORT_SYMBOL_GPL(remove_memory);
2082 #endif /* CONFIG_MEMORY_HOTREMOVE */