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/bootmem.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
15 #include <linux/pagevec.h>
16 #include <linux/writeback.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/highmem.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
33 #include <asm/tlbflush.h>
38 * online_page_callback contains pointer to current page onlining function.
39 * Initially it is generic_online_page(). If it is required it could be
40 * changed by calling set_online_page_callback() for callback registration
41 * and restore_online_page_callback() for generic callback restore.
44 static void generic_online_page(struct page *page);
46 static online_page_callback_t online_page_callback = generic_online_page;
48 DEFINE_MUTEX(mem_hotplug_mutex);
50 void lock_memory_hotplug(void)
52 mutex_lock(&mem_hotplug_mutex);
54 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
58 void unlock_memory_hotplug(void)
60 unlock_system_sleep();
61 mutex_unlock(&mem_hotplug_mutex);
65 /* add this memory to iomem resource */
66 static struct resource *register_memory_resource(u64 start, u64 size)
69 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
72 res->name = "System RAM";
74 res->end = start + size - 1;
75 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
76 if (request_resource(&iomem_resource, res) < 0) {
77 printk("System RAM resource %pR cannot be added\n", res);
84 static void release_memory_resource(struct resource *res)
88 release_resource(res);
93 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
94 void get_page_bootmem(unsigned long info, struct page *page,
97 page->lru.next = (struct list_head *) type;
99 set_page_private(page, info);
100 atomic_inc(&page->_count);
103 /* reference to __meminit __free_pages_bootmem is valid
104 * so use __ref to tell modpost not to generate a warning */
105 void __ref put_page_bootmem(struct page *page)
108 static DEFINE_MUTEX(ppb_lock);
110 type = (unsigned long) page->lru.next;
111 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
114 if (atomic_dec_return(&page->_count) == 1) {
115 ClearPagePrivate(page);
116 set_page_private(page, 0);
117 INIT_LIST_HEAD(&page->lru);
120 * Please refer to comment for __free_pages_bootmem()
121 * for why we serialize here.
123 mutex_lock(&ppb_lock);
124 __free_pages_bootmem(page, 0);
125 mutex_unlock(&ppb_lock);
130 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
131 #ifndef CONFIG_SPARSEMEM_VMEMMAP
132 static void register_page_bootmem_info_section(unsigned long start_pfn)
134 unsigned long *usemap, mapsize, section_nr, i;
135 struct mem_section *ms;
136 struct page *page, *memmap;
138 section_nr = pfn_to_section_nr(start_pfn);
139 ms = __nr_to_section(section_nr);
141 /* Get section's memmap address */
142 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
145 * Get page for the memmap's phys address
146 * XXX: need more consideration for sparse_vmemmap...
148 page = virt_to_page(memmap);
149 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
150 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
152 /* remember memmap's page */
153 for (i = 0; i < mapsize; i++, page++)
154 get_page_bootmem(section_nr, page, SECTION_INFO);
156 usemap = __nr_to_section(section_nr)->pageblock_flags;
157 page = virt_to_page(usemap);
159 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
161 for (i = 0; i < mapsize; i++, page++)
162 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
165 #else /* CONFIG_SPARSEMEM_VMEMMAP */
166 static void register_page_bootmem_info_section(unsigned long start_pfn)
168 unsigned long *usemap, mapsize, section_nr, i;
169 struct mem_section *ms;
170 struct page *page, *memmap;
172 if (!pfn_valid(start_pfn))
175 section_nr = pfn_to_section_nr(start_pfn);
176 ms = __nr_to_section(section_nr);
178 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
180 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
182 usemap = __nr_to_section(section_nr)->pageblock_flags;
183 page = virt_to_page(usemap);
185 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
187 for (i = 0; i < mapsize; i++, page++)
188 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
190 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
192 void register_page_bootmem_info_node(struct pglist_data *pgdat)
194 unsigned long i, pfn, end_pfn, nr_pages;
195 int node = pgdat->node_id;
199 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
200 page = virt_to_page(pgdat);
202 for (i = 0; i < nr_pages; i++, page++)
203 get_page_bootmem(node, page, NODE_INFO);
205 zone = &pgdat->node_zones[0];
206 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
207 if (zone->wait_table) {
208 nr_pages = zone->wait_table_hash_nr_entries
209 * sizeof(wait_queue_head_t);
210 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
211 page = virt_to_page(zone->wait_table);
213 for (i = 0; i < nr_pages; i++, page++)
214 get_page_bootmem(node, page, NODE_INFO);
218 pfn = pgdat->node_start_pfn;
219 end_pfn = pfn + pgdat->node_spanned_pages;
221 /* register_section info */
222 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
224 * Some platforms can assign the same pfn to multiple nodes - on
225 * node0 as well as nodeN. To avoid registering a pfn against
226 * multiple nodes we check that this pfn does not already
227 * reside in some other node.
229 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
230 register_page_bootmem_info_section(pfn);
233 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
235 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
236 unsigned long end_pfn)
238 unsigned long old_zone_end_pfn;
240 zone_span_writelock(zone);
242 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
243 if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
244 zone->zone_start_pfn = start_pfn;
246 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
247 zone->zone_start_pfn;
249 zone_span_writeunlock(zone);
252 static void resize_zone(struct zone *zone, unsigned long start_pfn,
253 unsigned long end_pfn)
255 zone_span_writelock(zone);
257 if (end_pfn - start_pfn) {
258 zone->zone_start_pfn = start_pfn;
259 zone->spanned_pages = end_pfn - start_pfn;
262 * make it consist as free_area_init_core(),
263 * if spanned_pages = 0, then keep start_pfn = 0
265 zone->zone_start_pfn = 0;
266 zone->spanned_pages = 0;
269 zone_span_writeunlock(zone);
272 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
273 unsigned long end_pfn)
275 enum zone_type zid = zone_idx(zone);
276 int nid = zone->zone_pgdat->node_id;
279 for (pfn = start_pfn; pfn < end_pfn; pfn++)
280 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
283 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
284 unsigned long start_pfn, unsigned long end_pfn)
288 unsigned long z1_start_pfn;
290 if (!z1->wait_table) {
291 ret = init_currently_empty_zone(z1, start_pfn,
292 end_pfn - start_pfn, MEMMAP_HOTPLUG);
297 pgdat_resize_lock(z1->zone_pgdat, &flags);
299 /* can't move pfns which are higher than @z2 */
300 if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
302 /* the move out part mast at the left most of @z2 */
303 if (start_pfn > z2->zone_start_pfn)
305 /* must included/overlap */
306 if (end_pfn <= z2->zone_start_pfn)
309 /* use start_pfn for z1's start_pfn if z1 is empty */
310 if (z1->spanned_pages)
311 z1_start_pfn = z1->zone_start_pfn;
313 z1_start_pfn = start_pfn;
315 resize_zone(z1, z1_start_pfn, end_pfn);
316 resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
318 pgdat_resize_unlock(z1->zone_pgdat, &flags);
320 fix_zone_id(z1, start_pfn, end_pfn);
324 pgdat_resize_unlock(z1->zone_pgdat, &flags);
328 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
329 unsigned long start_pfn, unsigned long end_pfn)
333 unsigned long z2_end_pfn;
335 if (!z2->wait_table) {
336 ret = init_currently_empty_zone(z2, start_pfn,
337 end_pfn - start_pfn, MEMMAP_HOTPLUG);
342 pgdat_resize_lock(z1->zone_pgdat, &flags);
344 /* can't move pfns which are lower than @z1 */
345 if (z1->zone_start_pfn > start_pfn)
347 /* the move out part mast at the right most of @z1 */
348 if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
350 /* must included/overlap */
351 if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
354 /* use end_pfn for z2's end_pfn if z2 is empty */
355 if (z2->spanned_pages)
356 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
358 z2_end_pfn = end_pfn;
360 resize_zone(z1, z1->zone_start_pfn, start_pfn);
361 resize_zone(z2, start_pfn, z2_end_pfn);
363 pgdat_resize_unlock(z1->zone_pgdat, &flags);
365 fix_zone_id(z2, start_pfn, end_pfn);
369 pgdat_resize_unlock(z1->zone_pgdat, &flags);
373 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
374 unsigned long end_pfn)
376 unsigned long old_pgdat_end_pfn =
377 pgdat->node_start_pfn + pgdat->node_spanned_pages;
379 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
380 pgdat->node_start_pfn = start_pfn;
382 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
383 pgdat->node_start_pfn;
386 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
388 struct pglist_data *pgdat = zone->zone_pgdat;
389 int nr_pages = PAGES_PER_SECTION;
390 int nid = pgdat->node_id;
394 zone_type = zone - pgdat->node_zones;
395 if (!zone->wait_table) {
398 ret = init_currently_empty_zone(zone, phys_start_pfn,
399 nr_pages, MEMMAP_HOTPLUG);
403 pgdat_resize_lock(zone->zone_pgdat, &flags);
404 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
405 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
406 phys_start_pfn + nr_pages);
407 pgdat_resize_unlock(zone->zone_pgdat, &flags);
408 memmap_init_zone(nr_pages, nid, zone_type,
409 phys_start_pfn, MEMMAP_HOTPLUG);
413 static int __meminit __add_section(int nid, struct zone *zone,
414 unsigned long phys_start_pfn)
416 int nr_pages = PAGES_PER_SECTION;
419 if (pfn_valid(phys_start_pfn))
422 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
427 ret = __add_zone(zone, phys_start_pfn);
432 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
435 #ifdef CONFIG_SPARSEMEM_VMEMMAP
436 static int __remove_section(struct zone *zone, struct mem_section *ms)
439 * XXX: Freeing memmap with vmemmap is not implement yet.
440 * This should be removed later.
445 static int __remove_section(struct zone *zone, struct mem_section *ms)
448 struct pglist_data *pgdat = zone->zone_pgdat;
451 if (!valid_section(ms))
454 ret = unregister_memory_section(ms);
458 pgdat_resize_lock(pgdat, &flags);
459 sparse_remove_one_section(zone, ms);
460 pgdat_resize_unlock(pgdat, &flags);
466 * Reasonably generic function for adding memory. It is
467 * expected that archs that support memory hotplug will
468 * call this function after deciding the zone to which to
471 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
472 unsigned long nr_pages)
476 int start_sec, end_sec;
477 /* during initialize mem_map, align hot-added range to section */
478 start_sec = pfn_to_section_nr(phys_start_pfn);
479 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
481 for (i = start_sec; i <= end_sec; i++) {
482 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
485 * EEXIST is finally dealt with by ioresource collision
486 * check. see add_memory() => register_memory_resource()
487 * Warning will be printed if there is collision.
489 if (err && (err != -EEXIST))
496 EXPORT_SYMBOL_GPL(__add_pages);
499 * __remove_pages() - remove sections of pages from a zone
500 * @zone: zone from which pages need to be removed
501 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
502 * @nr_pages: number of pages to remove (must be multiple of section size)
504 * Generic helper function to remove section mappings and sysfs entries
505 * for the section of the memory we are removing. Caller needs to make
506 * sure that pages are marked reserved and zones are adjust properly by
507 * calling offline_pages().
509 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
510 unsigned long nr_pages)
512 unsigned long i, ret = 0;
513 int sections_to_remove;
516 * We can only remove entire sections
518 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
519 BUG_ON(nr_pages % PAGES_PER_SECTION);
521 release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
523 sections_to_remove = nr_pages / PAGES_PER_SECTION;
524 for (i = 0; i < sections_to_remove; i++) {
525 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
526 ret = __remove_section(zone, __pfn_to_section(pfn));
532 EXPORT_SYMBOL_GPL(__remove_pages);
534 int set_online_page_callback(online_page_callback_t callback)
538 lock_memory_hotplug();
540 if (online_page_callback == generic_online_page) {
541 online_page_callback = callback;
545 unlock_memory_hotplug();
549 EXPORT_SYMBOL_GPL(set_online_page_callback);
551 int restore_online_page_callback(online_page_callback_t callback)
555 lock_memory_hotplug();
557 if (online_page_callback == callback) {
558 online_page_callback = generic_online_page;
562 unlock_memory_hotplug();
566 EXPORT_SYMBOL_GPL(restore_online_page_callback);
568 void __online_page_set_limits(struct page *page)
570 unsigned long pfn = page_to_pfn(page);
572 if (pfn >= num_physpages)
573 num_physpages = pfn + 1;
575 EXPORT_SYMBOL_GPL(__online_page_set_limits);
577 void __online_page_increment_counters(struct page *page)
581 #ifdef CONFIG_HIGHMEM
582 if (PageHighMem(page))
586 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
588 void __online_page_free(struct page *page)
590 ClearPageReserved(page);
591 init_page_count(page);
594 EXPORT_SYMBOL_GPL(__online_page_free);
596 static void generic_online_page(struct page *page)
598 __online_page_set_limits(page);
599 __online_page_increment_counters(page);
600 __online_page_free(page);
603 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
607 unsigned long onlined_pages = *(unsigned long *)arg;
609 if (PageReserved(pfn_to_page(start_pfn)))
610 for (i = 0; i < nr_pages; i++) {
611 page = pfn_to_page(start_pfn + i);
612 (*online_page_callback)(page);
615 *(unsigned long *)arg = onlined_pages;
619 #ifdef CONFIG_MOVABLE_NODE
621 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
624 static bool can_online_high_movable(struct zone *zone)
628 #else /* CONFIG_MOVABLE_NODE */
629 /* ensure every online node has NORMAL memory */
630 static bool can_online_high_movable(struct zone *zone)
632 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
634 #endif /* CONFIG_MOVABLE_NODE */
636 /* check which state of node_states will be changed when online memory */
637 static void node_states_check_changes_online(unsigned long nr_pages,
638 struct zone *zone, struct memory_notify *arg)
640 int nid = zone_to_nid(zone);
641 enum zone_type zone_last = ZONE_NORMAL;
644 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
645 * contains nodes which have zones of 0...ZONE_NORMAL,
646 * set zone_last to ZONE_NORMAL.
648 * If we don't have HIGHMEM nor movable node,
649 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
650 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
652 if (N_MEMORY == N_NORMAL_MEMORY)
653 zone_last = ZONE_MOVABLE;
656 * if the memory to be online is in a zone of 0...zone_last, and
657 * the zones of 0...zone_last don't have memory before online, we will
658 * need to set the node to node_states[N_NORMAL_MEMORY] after
659 * the memory is online.
661 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
662 arg->status_change_nid_normal = nid;
664 arg->status_change_nid_normal = -1;
666 #ifdef CONFIG_HIGHMEM
668 * If we have movable node, node_states[N_HIGH_MEMORY]
669 * contains nodes which have zones of 0...ZONE_HIGHMEM,
670 * set zone_last to ZONE_HIGHMEM.
672 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
673 * contains nodes which have zones of 0...ZONE_MOVABLE,
674 * set zone_last to ZONE_MOVABLE.
676 zone_last = ZONE_HIGHMEM;
677 if (N_MEMORY == N_HIGH_MEMORY)
678 zone_last = ZONE_MOVABLE;
680 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
681 arg->status_change_nid_high = nid;
683 arg->status_change_nid_high = -1;
685 arg->status_change_nid_high = arg->status_change_nid_normal;
689 * if the node don't have memory befor online, we will need to
690 * set the node to node_states[N_MEMORY] after the memory
693 if (!node_state(nid, N_MEMORY))
694 arg->status_change_nid = nid;
696 arg->status_change_nid = -1;
699 static void node_states_set_node(int node, struct memory_notify *arg)
701 if (arg->status_change_nid_normal >= 0)
702 node_set_state(node, N_NORMAL_MEMORY);
704 if (arg->status_change_nid_high >= 0)
705 node_set_state(node, N_HIGH_MEMORY);
707 node_set_state(node, N_MEMORY);
711 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
713 unsigned long onlined_pages = 0;
715 int need_zonelists_rebuild = 0;
718 struct memory_notify arg;
720 lock_memory_hotplug();
722 * This doesn't need a lock to do pfn_to_page().
723 * The section can't be removed here because of the
724 * memory_block->state_mutex.
726 zone = page_zone(pfn_to_page(pfn));
728 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
729 !can_online_high_movable(zone)) {
730 unlock_memory_hotplug();
734 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
735 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
736 unlock_memory_hotplug();
740 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
741 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
742 unlock_memory_hotplug();
747 /* Previous code may changed the zone of the pfn range */
748 zone = page_zone(pfn_to_page(pfn));
751 arg.nr_pages = nr_pages;
752 node_states_check_changes_online(nr_pages, zone, &arg);
754 nid = page_to_nid(pfn_to_page(pfn));
756 ret = memory_notify(MEM_GOING_ONLINE, &arg);
757 ret = notifier_to_errno(ret);
759 memory_notify(MEM_CANCEL_ONLINE, &arg);
760 unlock_memory_hotplug();
764 * If this zone is not populated, then it is not in zonelist.
765 * This means the page allocator ignores this zone.
766 * So, zonelist must be updated after online.
768 mutex_lock(&zonelists_mutex);
769 if (!populated_zone(zone)) {
770 need_zonelists_rebuild = 1;
771 build_all_zonelists(NULL, zone);
774 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
777 if (need_zonelists_rebuild)
778 zone_pcp_reset(zone);
779 mutex_unlock(&zonelists_mutex);
780 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
781 (unsigned long long) pfn << PAGE_SHIFT,
782 (((unsigned long long) pfn + nr_pages)
784 memory_notify(MEM_CANCEL_ONLINE, &arg);
785 unlock_memory_hotplug();
789 zone->managed_pages += onlined_pages;
790 zone->present_pages += onlined_pages;
791 zone->zone_pgdat->node_present_pages += onlined_pages;
793 node_states_set_node(zone_to_nid(zone), &arg);
794 if (need_zonelists_rebuild)
795 build_all_zonelists(NULL, NULL);
797 zone_pcp_update(zone);
800 mutex_unlock(&zonelists_mutex);
802 init_per_zone_wmark_min();
805 kswapd_run(zone_to_nid(zone));
807 vm_total_pages = nr_free_pagecache_pages();
809 writeback_set_ratelimit();
812 memory_notify(MEM_ONLINE, &arg);
813 unlock_memory_hotplug();
817 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
819 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
820 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
822 struct pglist_data *pgdat;
823 unsigned long zones_size[MAX_NR_ZONES] = {0};
824 unsigned long zholes_size[MAX_NR_ZONES] = {0};
825 unsigned long start_pfn = start >> PAGE_SHIFT;
827 pgdat = arch_alloc_nodedata(nid);
831 arch_refresh_nodedata(nid, pgdat);
833 /* we can use NODE_DATA(nid) from here */
835 /* init node's zones as empty zones, we don't have any present pages.*/
836 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
839 * The node we allocated has no zone fallback lists. For avoiding
840 * to access not-initialized zonelist, build here.
842 mutex_lock(&zonelists_mutex);
843 build_all_zonelists(pgdat, NULL);
844 mutex_unlock(&zonelists_mutex);
849 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
851 arch_refresh_nodedata(nid, NULL);
852 arch_free_nodedata(pgdat);
858 * called by cpu_up() to online a node without onlined memory.
860 int mem_online_node(int nid)
865 lock_memory_hotplug();
866 pgdat = hotadd_new_pgdat(nid, 0);
871 node_set_online(nid);
872 ret = register_one_node(nid);
876 unlock_memory_hotplug();
880 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
881 int __ref add_memory(int nid, u64 start, u64 size)
883 pg_data_t *pgdat = NULL;
885 struct resource *res;
888 lock_memory_hotplug();
890 res = register_memory_resource(start, size);
895 if (!node_online(nid)) {
896 pgdat = hotadd_new_pgdat(nid, start);
903 /* call arch's memory hotadd */
904 ret = arch_add_memory(nid, start, size);
909 /* we online node here. we can't roll back from here. */
910 node_set_online(nid);
913 ret = register_one_node(nid);
915 * If sysfs file of new node can't create, cpu on the node
916 * can't be hot-added. There is no rollback way now.
917 * So, check by BUG_ON() to catch it reluctantly..
922 /* create new memmap entry */
923 firmware_map_add_hotplug(start, start + size, "System RAM");
928 /* rollback pgdat allocation and others */
930 rollback_node_hotadd(nid, pgdat);
931 release_memory_resource(res);
934 unlock_memory_hotplug();
937 EXPORT_SYMBOL_GPL(add_memory);
939 #ifdef CONFIG_MEMORY_HOTREMOVE
941 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
942 * set and the size of the free page is given by page_order(). Using this,
943 * the function determines if the pageblock contains only free pages.
944 * Due to buddy contraints, a free page at least the size of a pageblock will
945 * be located at the start of the pageblock
947 static inline int pageblock_free(struct page *page)
949 return PageBuddy(page) && page_order(page) >= pageblock_order;
952 /* Return the start of the next active pageblock after a given page */
953 static struct page *next_active_pageblock(struct page *page)
955 /* Ensure the starting page is pageblock-aligned */
956 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
958 /* If the entire pageblock is free, move to the end of free page */
959 if (pageblock_free(page)) {
961 /* be careful. we don't have locks, page_order can be changed.*/
962 order = page_order(page);
963 if ((order < MAX_ORDER) && (order >= pageblock_order))
964 return page + (1 << order);
967 return page + pageblock_nr_pages;
970 /* Checks if this range of memory is likely to be hot-removable. */
971 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
973 struct page *page = pfn_to_page(start_pfn);
974 struct page *end_page = page + nr_pages;
976 /* Check the starting page of each pageblock within the range */
977 for (; page < end_page; page = next_active_pageblock(page)) {
978 if (!is_pageblock_removable_nolock(page))
983 /* All pageblocks in the memory block are likely to be hot-removable */
988 * Confirm all pages in a range [start, end) is belongs to the same zone.
990 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
993 struct zone *zone = NULL;
996 for (pfn = start_pfn;
998 pfn += MAX_ORDER_NR_PAGES) {
1000 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1001 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1003 if (i == MAX_ORDER_NR_PAGES)
1005 page = pfn_to_page(pfn + i);
1006 if (zone && page_zone(page) != zone)
1008 zone = page_zone(page);
1014 * Scanning pfn is much easier than scanning lru list.
1015 * Scan pfn from start to end and Find LRU page.
1017 static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
1021 for (pfn = start; pfn < end; pfn++) {
1022 if (pfn_valid(pfn)) {
1023 page = pfn_to_page(pfn);
1031 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1033 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1037 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1038 int not_managed = 0;
1042 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1043 if (!pfn_valid(pfn))
1045 page = pfn_to_page(pfn);
1046 if (!get_page_unless_zero(page))
1049 * We can skip free pages. And we can only deal with pages on
1052 ret = isolate_lru_page(page);
1053 if (!ret) { /* Success */
1055 list_add_tail(&page->lru, &source);
1057 inc_zone_page_state(page, NR_ISOLATED_ANON +
1058 page_is_file_cache(page));
1061 #ifdef CONFIG_DEBUG_VM
1062 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1067 /* Because we don't have big zone->lock. we should
1068 check this again here. */
1069 if (page_count(page)) {
1076 if (!list_empty(&source)) {
1078 putback_lru_pages(&source);
1083 * alloc_migrate_target should be improooooved!!
1084 * migrate_pages returns # of failed pages.
1086 ret = migrate_pages(&source, alloc_migrate_target, 0,
1090 putback_lru_pages(&source);
1097 * remove from free_area[] and mark all as Reserved.
1100 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1103 __offline_isolated_pages(start, start + nr_pages);
1108 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1110 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1111 offline_isolated_pages_cb);
1115 * Check all pages in range, recoreded as memory resource, are isolated.
1118 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1122 long offlined = *(long *)data;
1123 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1124 offlined = nr_pages;
1126 *(long *)data += offlined;
1131 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1136 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1137 check_pages_isolated_cb);
1139 offlined = (long)ret;
1143 #ifdef CONFIG_MOVABLE_NODE
1145 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1148 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1152 #else /* CONFIG_MOVABLE_NODE */
1153 /* ensure the node has NORMAL memory if it is still online */
1154 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1156 struct pglist_data *pgdat = zone->zone_pgdat;
1157 unsigned long present_pages = 0;
1160 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1161 present_pages += pgdat->node_zones[zt].present_pages;
1163 if (present_pages > nr_pages)
1167 for (; zt <= ZONE_MOVABLE; zt++)
1168 present_pages += pgdat->node_zones[zt].present_pages;
1171 * we can't offline the last normal memory until all
1172 * higher memory is offlined.
1174 return present_pages == 0;
1176 #endif /* CONFIG_MOVABLE_NODE */
1178 /* check which state of node_states will be changed when offline memory */
1179 static void node_states_check_changes_offline(unsigned long nr_pages,
1180 struct zone *zone, struct memory_notify *arg)
1182 struct pglist_data *pgdat = zone->zone_pgdat;
1183 unsigned long present_pages = 0;
1184 enum zone_type zt, zone_last = ZONE_NORMAL;
1187 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1188 * contains nodes which have zones of 0...ZONE_NORMAL,
1189 * set zone_last to ZONE_NORMAL.
1191 * If we don't have HIGHMEM nor movable node,
1192 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1193 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1195 if (N_MEMORY == N_NORMAL_MEMORY)
1196 zone_last = ZONE_MOVABLE;
1199 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1200 * If the memory to be offline is in a zone of 0...zone_last,
1201 * and it is the last present memory, 0...zone_last will
1202 * become empty after offline , thus we can determind we will
1203 * need to clear the node from node_states[N_NORMAL_MEMORY].
1205 for (zt = 0; zt <= zone_last; zt++)
1206 present_pages += pgdat->node_zones[zt].present_pages;
1207 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1208 arg->status_change_nid_normal = zone_to_nid(zone);
1210 arg->status_change_nid_normal = -1;
1212 #ifdef CONFIG_HIGHMEM
1214 * If we have movable node, node_states[N_HIGH_MEMORY]
1215 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1216 * set zone_last to ZONE_HIGHMEM.
1218 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1219 * contains nodes which have zones of 0...ZONE_MOVABLE,
1220 * set zone_last to ZONE_MOVABLE.
1222 zone_last = ZONE_HIGHMEM;
1223 if (N_MEMORY == N_HIGH_MEMORY)
1224 zone_last = ZONE_MOVABLE;
1226 for (; zt <= zone_last; zt++)
1227 present_pages += pgdat->node_zones[zt].present_pages;
1228 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1229 arg->status_change_nid_high = zone_to_nid(zone);
1231 arg->status_change_nid_high = -1;
1233 arg->status_change_nid_high = arg->status_change_nid_normal;
1237 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1239 zone_last = ZONE_MOVABLE;
1242 * check whether node_states[N_HIGH_MEMORY] will be changed
1243 * If we try to offline the last present @nr_pages from the node,
1244 * we can determind we will need to clear the node from
1245 * node_states[N_HIGH_MEMORY].
1247 for (; zt <= zone_last; zt++)
1248 present_pages += pgdat->node_zones[zt].present_pages;
1249 if (nr_pages >= present_pages)
1250 arg->status_change_nid = zone_to_nid(zone);
1252 arg->status_change_nid = -1;
1255 static void node_states_clear_node(int node, struct memory_notify *arg)
1257 if (arg->status_change_nid_normal >= 0)
1258 node_clear_state(node, N_NORMAL_MEMORY);
1260 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1261 (arg->status_change_nid_high >= 0))
1262 node_clear_state(node, N_HIGH_MEMORY);
1264 if ((N_MEMORY != N_HIGH_MEMORY) &&
1265 (arg->status_change_nid >= 0))
1266 node_clear_state(node, N_MEMORY);
1269 static int __ref __offline_pages(unsigned long start_pfn,
1270 unsigned long end_pfn, unsigned long timeout)
1272 unsigned long pfn, nr_pages, expire;
1273 long offlined_pages;
1274 int ret, drain, retry_max, node;
1276 struct memory_notify arg;
1278 BUG_ON(start_pfn >= end_pfn);
1279 /* at least, alignment against pageblock is necessary */
1280 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1282 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1284 /* This makes hotplug much easier...and readable.
1285 we assume this for now. .*/
1286 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1289 lock_memory_hotplug();
1291 zone = page_zone(pfn_to_page(start_pfn));
1292 node = zone_to_nid(zone);
1293 nr_pages = end_pfn - start_pfn;
1296 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1299 /* set above range as isolated */
1300 ret = start_isolate_page_range(start_pfn, end_pfn,
1301 MIGRATE_MOVABLE, true);
1305 arg.start_pfn = start_pfn;
1306 arg.nr_pages = nr_pages;
1307 node_states_check_changes_offline(nr_pages, zone, &arg);
1309 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1310 ret = notifier_to_errno(ret);
1312 goto failed_removal;
1315 expire = jiffies + timeout;
1319 /* start memory hot removal */
1321 if (time_after(jiffies, expire))
1322 goto failed_removal;
1324 if (signal_pending(current))
1325 goto failed_removal;
1328 lru_add_drain_all();
1333 pfn = scan_lru_pages(start_pfn, end_pfn);
1334 if (pfn) { /* We have page on LRU */
1335 ret = do_migrate_range(pfn, end_pfn);
1341 if (--retry_max == 0)
1342 goto failed_removal;
1348 /* drain all zone's lru pagevec, this is asynchronous... */
1349 lru_add_drain_all();
1351 /* drain pcp pages, this is synchronous. */
1354 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1355 if (offlined_pages < 0) {
1357 goto failed_removal;
1359 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1360 /* Ok, all of our target is isolated.
1361 We cannot do rollback at this point. */
1362 offline_isolated_pages(start_pfn, end_pfn);
1363 /* reset pagetype flags and makes migrate type to be MOVABLE */
1364 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1365 /* removal success */
1366 zone->managed_pages -= offlined_pages;
1367 zone->present_pages -= offlined_pages;
1368 zone->zone_pgdat->node_present_pages -= offlined_pages;
1369 totalram_pages -= offlined_pages;
1371 init_per_zone_wmark_min();
1373 if (!populated_zone(zone)) {
1374 zone_pcp_reset(zone);
1375 mutex_lock(&zonelists_mutex);
1376 build_all_zonelists(NULL, NULL);
1377 mutex_unlock(&zonelists_mutex);
1379 zone_pcp_update(zone);
1381 node_states_clear_node(node, &arg);
1382 if (arg.status_change_nid >= 0)
1385 vm_total_pages = nr_free_pagecache_pages();
1386 writeback_set_ratelimit();
1388 memory_notify(MEM_OFFLINE, &arg);
1389 unlock_memory_hotplug();
1393 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1394 (unsigned long long) start_pfn << PAGE_SHIFT,
1395 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1396 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1397 /* pushback to free area */
1398 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1401 unlock_memory_hotplug();
1405 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1407 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1411 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1412 * @start_pfn: start pfn of the memory range
1413 * @end_pfn: end pft of the memory range
1414 * @arg: argument passed to func
1415 * @func: callback for each memory section walked
1417 * This function walks through all present mem sections in range
1418 * [start_pfn, end_pfn) and call func on each mem section.
1420 * Returns the return value of func.
1422 static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1423 void *arg, int (*func)(struct memory_block *, void *))
1425 struct memory_block *mem = NULL;
1426 struct mem_section *section;
1427 unsigned long pfn, section_nr;
1430 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1431 section_nr = pfn_to_section_nr(pfn);
1432 if (!present_section_nr(section_nr))
1435 section = __nr_to_section(section_nr);
1436 /* same memblock? */
1438 if ((section_nr >= mem->start_section_nr) &&
1439 (section_nr <= mem->end_section_nr))
1442 mem = find_memory_block_hinted(section, mem);
1446 ret = func(mem, arg);
1448 kobject_put(&mem->dev.kobj);
1454 kobject_put(&mem->dev.kobj);
1460 * offline_memory_block_cb - callback function for offlining memory block
1461 * @mem: the memory block to be offlined
1462 * @arg: buffer to hold error msg
1464 * Always return 0, and put the error msg in arg if any.
1466 static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1469 int error = offline_memory_block(mem);
1471 if (error != 0 && *ret == 0)
1477 static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1479 int ret = !is_memblock_offlined(mem);
1482 pr_warn("removing memory fails, because memory "
1483 "[%#010llx-%#010llx] is onlined\n",
1484 PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1485 PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1490 int __ref remove_memory(u64 start, u64 size)
1492 unsigned long start_pfn, end_pfn;
1496 start_pfn = PFN_DOWN(start);
1497 end_pfn = start_pfn + PFN_DOWN(size);
1500 * When CONFIG_MEMCG is on, one memory block may be used by other
1501 * blocks to store page cgroup when onlining pages. But we don't know
1502 * in what order pages are onlined. So we iterate twice to offline
1504 * 1st iterate: offline every non primary memory block.
1505 * 2nd iterate: offline primary (i.e. first added) memory block.
1508 walk_memory_range(start_pfn, end_pfn, &ret,
1509 offline_memory_block_cb);
1519 lock_memory_hotplug();
1522 * we have offlined all memory blocks like this:
1523 * 1. lock memory hotplug
1524 * 2. offline a memory block
1525 * 3. unlock memory hotplug
1527 * repeat step1-3 to offline the memory block. All memory blocks
1528 * must be offlined before removing memory. But we don't hold the
1529 * lock in the whole operation. So we should check whether all
1530 * memory blocks are offlined.
1533 ret = walk_memory_range(start_pfn, end_pfn, NULL,
1534 is_memblock_offlined_cb);
1536 unlock_memory_hotplug();
1540 /* remove memmap entry */
1541 firmware_map_remove(start, start + size, "System RAM");
1543 arch_remove_memory(start, size);
1545 unlock_memory_hotplug();
1550 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1554 int remove_memory(u64 start, u64 size)
1558 #endif /* CONFIG_MEMORY_HOTREMOVE */
1559 EXPORT_SYMBOL_GPL(remove_memory);