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 #ifndef CONFIG_SPARSEMEM_VMEMMAP
95 static void get_page_bootmem(unsigned long info, struct page *page,
98 page->lru.next = (struct list_head *) type;
100 set_page_private(page, info);
101 atomic_inc(&page->_count);
104 /* reference to __meminit __free_pages_bootmem is valid
105 * so use __ref to tell modpost not to generate a warning */
106 void __ref put_page_bootmem(struct page *page)
109 static DEFINE_MUTEX(ppb_lock);
111 type = (unsigned long) page->lru.next;
112 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
113 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
115 if (atomic_dec_return(&page->_count) == 1) {
116 ClearPagePrivate(page);
117 set_page_private(page, 0);
118 INIT_LIST_HEAD(&page->lru);
121 * Please refer to comment for __free_pages_bootmem()
122 * for why we serialize here.
124 mutex_lock(&ppb_lock);
125 __free_pages_bootmem(page, 0);
126 mutex_unlock(&ppb_lock);
131 static void register_page_bootmem_info_section(unsigned long start_pfn)
133 unsigned long *usemap, mapsize, section_nr, i;
134 struct mem_section *ms;
135 struct page *page, *memmap;
137 section_nr = pfn_to_section_nr(start_pfn);
138 ms = __nr_to_section(section_nr);
140 /* Get section's memmap address */
141 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
144 * Get page for the memmap's phys address
145 * XXX: need more consideration for sparse_vmemmap...
147 page = virt_to_page(memmap);
148 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
149 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
151 /* remember memmap's page */
152 for (i = 0; i < mapsize; i++, page++)
153 get_page_bootmem(section_nr, page, SECTION_INFO);
155 usemap = __nr_to_section(section_nr)->pageblock_flags;
156 page = virt_to_page(usemap);
158 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
160 for (i = 0; i < mapsize; i++, page++)
161 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
165 void register_page_bootmem_info_node(struct pglist_data *pgdat)
167 unsigned long i, pfn, end_pfn, nr_pages;
168 int node = pgdat->node_id;
172 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
173 page = virt_to_page(pgdat);
175 for (i = 0; i < nr_pages; i++, page++)
176 get_page_bootmem(node, page, NODE_INFO);
178 zone = &pgdat->node_zones[0];
179 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
180 if (zone->wait_table) {
181 nr_pages = zone->wait_table_hash_nr_entries
182 * sizeof(wait_queue_head_t);
183 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
184 page = virt_to_page(zone->wait_table);
186 for (i = 0; i < nr_pages; i++, page++)
187 get_page_bootmem(node, page, NODE_INFO);
191 pfn = pgdat->node_start_pfn;
192 end_pfn = pfn + pgdat->node_spanned_pages;
194 /* register_section info */
195 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
197 * Some platforms can assign the same pfn to multiple nodes - on
198 * node0 as well as nodeN. To avoid registering a pfn against
199 * multiple nodes we check that this pfn does not already
200 * reside in some other node.
202 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
203 register_page_bootmem_info_section(pfn);
206 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
208 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
209 unsigned long end_pfn)
211 unsigned long old_zone_end_pfn;
213 zone_span_writelock(zone);
215 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
216 if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
217 zone->zone_start_pfn = start_pfn;
219 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
220 zone->zone_start_pfn;
222 zone_span_writeunlock(zone);
225 static void resize_zone(struct zone *zone, unsigned long start_pfn,
226 unsigned long end_pfn)
228 zone_span_writelock(zone);
230 if (end_pfn - start_pfn) {
231 zone->zone_start_pfn = start_pfn;
232 zone->spanned_pages = end_pfn - start_pfn;
235 * make it consist as free_area_init_core(),
236 * if spanned_pages = 0, then keep start_pfn = 0
238 zone->zone_start_pfn = 0;
239 zone->spanned_pages = 0;
242 zone_span_writeunlock(zone);
245 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
246 unsigned long end_pfn)
248 enum zone_type zid = zone_idx(zone);
249 int nid = zone->zone_pgdat->node_id;
252 for (pfn = start_pfn; pfn < end_pfn; pfn++)
253 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
256 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
257 unsigned long start_pfn, unsigned long end_pfn)
261 unsigned long z1_start_pfn;
263 if (!z1->wait_table) {
264 ret = init_currently_empty_zone(z1, start_pfn,
265 end_pfn - start_pfn, MEMMAP_HOTPLUG);
270 pgdat_resize_lock(z1->zone_pgdat, &flags);
272 /* can't move pfns which are higher than @z2 */
273 if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
275 /* the move out part mast at the left most of @z2 */
276 if (start_pfn > z2->zone_start_pfn)
278 /* must included/overlap */
279 if (end_pfn <= z2->zone_start_pfn)
282 /* use start_pfn for z1's start_pfn if z1 is empty */
283 if (z1->spanned_pages)
284 z1_start_pfn = z1->zone_start_pfn;
286 z1_start_pfn = start_pfn;
288 resize_zone(z1, z1_start_pfn, end_pfn);
289 resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
291 pgdat_resize_unlock(z1->zone_pgdat, &flags);
293 fix_zone_id(z1, start_pfn, end_pfn);
297 pgdat_resize_unlock(z1->zone_pgdat, &flags);
301 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
302 unsigned long start_pfn, unsigned long end_pfn)
306 unsigned long z2_end_pfn;
308 if (!z2->wait_table) {
309 ret = init_currently_empty_zone(z2, start_pfn,
310 end_pfn - start_pfn, MEMMAP_HOTPLUG);
315 pgdat_resize_lock(z1->zone_pgdat, &flags);
317 /* can't move pfns which are lower than @z1 */
318 if (z1->zone_start_pfn > start_pfn)
320 /* the move out part mast at the right most of @z1 */
321 if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
323 /* must included/overlap */
324 if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
327 /* use end_pfn for z2's end_pfn if z2 is empty */
328 if (z2->spanned_pages)
329 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
331 z2_end_pfn = end_pfn;
333 resize_zone(z1, z1->zone_start_pfn, start_pfn);
334 resize_zone(z2, start_pfn, z2_end_pfn);
336 pgdat_resize_unlock(z1->zone_pgdat, &flags);
338 fix_zone_id(z2, start_pfn, end_pfn);
342 pgdat_resize_unlock(z1->zone_pgdat, &flags);
346 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
347 unsigned long end_pfn)
349 unsigned long old_pgdat_end_pfn =
350 pgdat->node_start_pfn + pgdat->node_spanned_pages;
352 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
353 pgdat->node_start_pfn = start_pfn;
355 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
356 pgdat->node_start_pfn;
359 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
361 struct pglist_data *pgdat = zone->zone_pgdat;
362 int nr_pages = PAGES_PER_SECTION;
363 int nid = pgdat->node_id;
367 zone_type = zone - pgdat->node_zones;
368 if (!zone->wait_table) {
371 ret = init_currently_empty_zone(zone, phys_start_pfn,
372 nr_pages, MEMMAP_HOTPLUG);
376 pgdat_resize_lock(zone->zone_pgdat, &flags);
377 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
378 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
379 phys_start_pfn + nr_pages);
380 pgdat_resize_unlock(zone->zone_pgdat, &flags);
381 memmap_init_zone(nr_pages, nid, zone_type,
382 phys_start_pfn, MEMMAP_HOTPLUG);
386 static int __meminit __add_section(int nid, struct zone *zone,
387 unsigned long phys_start_pfn)
389 int nr_pages = PAGES_PER_SECTION;
392 if (pfn_valid(phys_start_pfn))
395 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
400 ret = __add_zone(zone, phys_start_pfn);
405 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
408 #ifdef CONFIG_SPARSEMEM_VMEMMAP
409 static int __remove_section(struct zone *zone, struct mem_section *ms)
412 * XXX: Freeing memmap with vmemmap is not implement yet.
413 * This should be removed later.
418 static int __remove_section(struct zone *zone, struct mem_section *ms)
421 struct pglist_data *pgdat = zone->zone_pgdat;
424 if (!valid_section(ms))
427 ret = unregister_memory_section(ms);
431 pgdat_resize_lock(pgdat, &flags);
432 sparse_remove_one_section(zone, ms);
433 pgdat_resize_unlock(pgdat, &flags);
439 * Reasonably generic function for adding memory. It is
440 * expected that archs that support memory hotplug will
441 * call this function after deciding the zone to which to
444 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
445 unsigned long nr_pages)
449 int start_sec, end_sec;
450 /* during initialize mem_map, align hot-added range to section */
451 start_sec = pfn_to_section_nr(phys_start_pfn);
452 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
454 for (i = start_sec; i <= end_sec; i++) {
455 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
458 * EEXIST is finally dealt with by ioresource collision
459 * check. see add_memory() => register_memory_resource()
460 * Warning will be printed if there is collision.
462 if (err && (err != -EEXIST))
469 EXPORT_SYMBOL_GPL(__add_pages);
472 * __remove_pages() - remove sections of pages from a zone
473 * @zone: zone from which pages need to be removed
474 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
475 * @nr_pages: number of pages to remove (must be multiple of section size)
477 * Generic helper function to remove section mappings and sysfs entries
478 * for the section of the memory we are removing. Caller needs to make
479 * sure that pages are marked reserved and zones are adjust properly by
480 * calling offline_pages().
482 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
483 unsigned long nr_pages)
485 unsigned long i, ret = 0;
486 int sections_to_remove;
489 * We can only remove entire sections
491 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
492 BUG_ON(nr_pages % PAGES_PER_SECTION);
494 release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
496 sections_to_remove = nr_pages / PAGES_PER_SECTION;
497 for (i = 0; i < sections_to_remove; i++) {
498 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
499 ret = __remove_section(zone, __pfn_to_section(pfn));
505 EXPORT_SYMBOL_GPL(__remove_pages);
507 int set_online_page_callback(online_page_callback_t callback)
511 lock_memory_hotplug();
513 if (online_page_callback == generic_online_page) {
514 online_page_callback = callback;
518 unlock_memory_hotplug();
522 EXPORT_SYMBOL_GPL(set_online_page_callback);
524 int restore_online_page_callback(online_page_callback_t callback)
528 lock_memory_hotplug();
530 if (online_page_callback == callback) {
531 online_page_callback = generic_online_page;
535 unlock_memory_hotplug();
539 EXPORT_SYMBOL_GPL(restore_online_page_callback);
541 void __online_page_set_limits(struct page *page)
543 unsigned long pfn = page_to_pfn(page);
545 if (pfn >= num_physpages)
546 num_physpages = pfn + 1;
548 EXPORT_SYMBOL_GPL(__online_page_set_limits);
550 void __online_page_increment_counters(struct page *page)
554 #ifdef CONFIG_HIGHMEM
555 if (PageHighMem(page))
559 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
561 void __online_page_free(struct page *page)
563 ClearPageReserved(page);
564 init_page_count(page);
567 EXPORT_SYMBOL_GPL(__online_page_free);
569 static void generic_online_page(struct page *page)
571 __online_page_set_limits(page);
572 __online_page_increment_counters(page);
573 __online_page_free(page);
576 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
580 unsigned long onlined_pages = *(unsigned long *)arg;
582 if (PageReserved(pfn_to_page(start_pfn)))
583 for (i = 0; i < nr_pages; i++) {
584 page = pfn_to_page(start_pfn + i);
585 (*online_page_callback)(page);
588 *(unsigned long *)arg = onlined_pages;
592 #ifdef CONFIG_MOVABLE_NODE
594 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
597 static bool can_online_high_movable(struct zone *zone)
601 #else /* CONFIG_MOVABLE_NODE */
602 /* ensure every online node has NORMAL memory */
603 static bool can_online_high_movable(struct zone *zone)
605 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
607 #endif /* CONFIG_MOVABLE_NODE */
609 /* check which state of node_states will be changed when online memory */
610 static void node_states_check_changes_online(unsigned long nr_pages,
611 struct zone *zone, struct memory_notify *arg)
613 int nid = zone_to_nid(zone);
614 enum zone_type zone_last = ZONE_NORMAL;
617 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
618 * contains nodes which have zones of 0...ZONE_NORMAL,
619 * set zone_last to ZONE_NORMAL.
621 * If we don't have HIGHMEM nor movable node,
622 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
623 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
625 if (N_MEMORY == N_NORMAL_MEMORY)
626 zone_last = ZONE_MOVABLE;
629 * if the memory to be online is in a zone of 0...zone_last, and
630 * the zones of 0...zone_last don't have memory before online, we will
631 * need to set the node to node_states[N_NORMAL_MEMORY] after
632 * the memory is online.
634 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
635 arg->status_change_nid_normal = nid;
637 arg->status_change_nid_normal = -1;
639 #ifdef CONFIG_HIGHMEM
641 * If we have movable node, node_states[N_HIGH_MEMORY]
642 * contains nodes which have zones of 0...ZONE_HIGHMEM,
643 * set zone_last to ZONE_HIGHMEM.
645 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
646 * contains nodes which have zones of 0...ZONE_MOVABLE,
647 * set zone_last to ZONE_MOVABLE.
649 zone_last = ZONE_HIGHMEM;
650 if (N_MEMORY == N_HIGH_MEMORY)
651 zone_last = ZONE_MOVABLE;
653 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
654 arg->status_change_nid_high = nid;
656 arg->status_change_nid_high = -1;
658 arg->status_change_nid_high = arg->status_change_nid_normal;
662 * if the node don't have memory befor online, we will need to
663 * set the node to node_states[N_MEMORY] after the memory
666 if (!node_state(nid, N_MEMORY))
667 arg->status_change_nid = nid;
669 arg->status_change_nid = -1;
672 static void node_states_set_node(int node, struct memory_notify *arg)
674 if (arg->status_change_nid_normal >= 0)
675 node_set_state(node, N_NORMAL_MEMORY);
677 if (arg->status_change_nid_high >= 0)
678 node_set_state(node, N_HIGH_MEMORY);
680 node_set_state(node, N_MEMORY);
684 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
686 unsigned long onlined_pages = 0;
688 int need_zonelists_rebuild = 0;
691 struct memory_notify arg;
693 lock_memory_hotplug();
695 * This doesn't need a lock to do pfn_to_page().
696 * The section can't be removed here because of the
697 * memory_block->state_mutex.
699 zone = page_zone(pfn_to_page(pfn));
701 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
702 !can_online_high_movable(zone)) {
703 unlock_memory_hotplug();
707 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
708 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
709 unlock_memory_hotplug();
713 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
714 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
715 unlock_memory_hotplug();
720 /* Previous code may changed the zone of the pfn range */
721 zone = page_zone(pfn_to_page(pfn));
724 arg.nr_pages = nr_pages;
725 node_states_check_changes_online(nr_pages, zone, &arg);
727 nid = page_to_nid(pfn_to_page(pfn));
729 ret = memory_notify(MEM_GOING_ONLINE, &arg);
730 ret = notifier_to_errno(ret);
732 memory_notify(MEM_CANCEL_ONLINE, &arg);
733 unlock_memory_hotplug();
737 * If this zone is not populated, then it is not in zonelist.
738 * This means the page allocator ignores this zone.
739 * So, zonelist must be updated after online.
741 mutex_lock(&zonelists_mutex);
742 if (!populated_zone(zone)) {
743 need_zonelists_rebuild = 1;
744 build_all_zonelists(NULL, zone);
747 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
750 if (need_zonelists_rebuild)
751 zone_pcp_reset(zone);
752 mutex_unlock(&zonelists_mutex);
753 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
754 (unsigned long long) pfn << PAGE_SHIFT,
755 (((unsigned long long) pfn + nr_pages)
757 memory_notify(MEM_CANCEL_ONLINE, &arg);
758 unlock_memory_hotplug();
762 zone->managed_pages += onlined_pages;
763 zone->present_pages += onlined_pages;
764 zone->zone_pgdat->node_present_pages += onlined_pages;
766 node_states_set_node(zone_to_nid(zone), &arg);
767 if (need_zonelists_rebuild)
768 build_all_zonelists(NULL, NULL);
770 zone_pcp_update(zone);
773 mutex_unlock(&zonelists_mutex);
775 init_per_zone_wmark_min();
778 kswapd_run(zone_to_nid(zone));
780 vm_total_pages = nr_free_pagecache_pages();
782 writeback_set_ratelimit();
785 memory_notify(MEM_ONLINE, &arg);
786 unlock_memory_hotplug();
790 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
792 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
793 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
795 struct pglist_data *pgdat;
796 unsigned long zones_size[MAX_NR_ZONES] = {0};
797 unsigned long zholes_size[MAX_NR_ZONES] = {0};
798 unsigned long start_pfn = start >> PAGE_SHIFT;
800 pgdat = arch_alloc_nodedata(nid);
804 arch_refresh_nodedata(nid, pgdat);
806 /* we can use NODE_DATA(nid) from here */
808 /* init node's zones as empty zones, we don't have any present pages.*/
809 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
812 * The node we allocated has no zone fallback lists. For avoiding
813 * to access not-initialized zonelist, build here.
815 mutex_lock(&zonelists_mutex);
816 build_all_zonelists(pgdat, NULL);
817 mutex_unlock(&zonelists_mutex);
822 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
824 arch_refresh_nodedata(nid, NULL);
825 arch_free_nodedata(pgdat);
831 * called by cpu_up() to online a node without onlined memory.
833 int mem_online_node(int nid)
838 lock_memory_hotplug();
839 pgdat = hotadd_new_pgdat(nid, 0);
844 node_set_online(nid);
845 ret = register_one_node(nid);
849 unlock_memory_hotplug();
853 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
854 int __ref add_memory(int nid, u64 start, u64 size)
856 pg_data_t *pgdat = NULL;
858 struct resource *res;
861 lock_memory_hotplug();
863 res = register_memory_resource(start, size);
868 if (!node_online(nid)) {
869 pgdat = hotadd_new_pgdat(nid, start);
876 /* call arch's memory hotadd */
877 ret = arch_add_memory(nid, start, size);
882 /* we online node here. we can't roll back from here. */
883 node_set_online(nid);
886 ret = register_one_node(nid);
888 * If sysfs file of new node can't create, cpu on the node
889 * can't be hot-added. There is no rollback way now.
890 * So, check by BUG_ON() to catch it reluctantly..
895 /* create new memmap entry */
896 firmware_map_add_hotplug(start, start + size, "System RAM");
901 /* rollback pgdat allocation and others */
903 rollback_node_hotadd(nid, pgdat);
904 release_memory_resource(res);
907 unlock_memory_hotplug();
910 EXPORT_SYMBOL_GPL(add_memory);
912 #ifdef CONFIG_MEMORY_HOTREMOVE
914 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
915 * set and the size of the free page is given by page_order(). Using this,
916 * the function determines if the pageblock contains only free pages.
917 * Due to buddy contraints, a free page at least the size of a pageblock will
918 * be located at the start of the pageblock
920 static inline int pageblock_free(struct page *page)
922 return PageBuddy(page) && page_order(page) >= pageblock_order;
925 /* Return the start of the next active pageblock after a given page */
926 static struct page *next_active_pageblock(struct page *page)
928 /* Ensure the starting page is pageblock-aligned */
929 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
931 /* If the entire pageblock is free, move to the end of free page */
932 if (pageblock_free(page)) {
934 /* be careful. we don't have locks, page_order can be changed.*/
935 order = page_order(page);
936 if ((order < MAX_ORDER) && (order >= pageblock_order))
937 return page + (1 << order);
940 return page + pageblock_nr_pages;
943 /* Checks if this range of memory is likely to be hot-removable. */
944 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
946 struct page *page = pfn_to_page(start_pfn);
947 struct page *end_page = page + nr_pages;
949 /* Check the starting page of each pageblock within the range */
950 for (; page < end_page; page = next_active_pageblock(page)) {
951 if (!is_pageblock_removable_nolock(page))
956 /* All pageblocks in the memory block are likely to be hot-removable */
961 * Confirm all pages in a range [start, end) is belongs to the same zone.
963 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
966 struct zone *zone = NULL;
969 for (pfn = start_pfn;
971 pfn += MAX_ORDER_NR_PAGES) {
973 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
974 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
976 if (i == MAX_ORDER_NR_PAGES)
978 page = pfn_to_page(pfn + i);
979 if (zone && page_zone(page) != zone)
981 zone = page_zone(page);
987 * Scanning pfn is much easier than scanning lru list.
988 * Scan pfn from start to end and Find LRU page.
990 static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
994 for (pfn = start; pfn < end; pfn++) {
995 if (pfn_valid(pfn)) {
996 page = pfn_to_page(pfn);
1004 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1006 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1010 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1011 int not_managed = 0;
1015 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1016 if (!pfn_valid(pfn))
1018 page = pfn_to_page(pfn);
1019 if (!get_page_unless_zero(page))
1022 * We can skip free pages. And we can only deal with pages on
1025 ret = isolate_lru_page(page);
1026 if (!ret) { /* Success */
1028 list_add_tail(&page->lru, &source);
1030 inc_zone_page_state(page, NR_ISOLATED_ANON +
1031 page_is_file_cache(page));
1034 #ifdef CONFIG_DEBUG_VM
1035 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1040 /* Because we don't have big zone->lock. we should
1041 check this again here. */
1042 if (page_count(page)) {
1049 if (!list_empty(&source)) {
1051 putback_lru_pages(&source);
1056 * alloc_migrate_target should be improooooved!!
1057 * migrate_pages returns # of failed pages.
1059 ret = migrate_pages(&source, alloc_migrate_target, 0,
1063 putback_lru_pages(&source);
1070 * remove from free_area[] and mark all as Reserved.
1073 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1076 __offline_isolated_pages(start, start + nr_pages);
1081 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1083 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1084 offline_isolated_pages_cb);
1088 * Check all pages in range, recoreded as memory resource, are isolated.
1091 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1095 long offlined = *(long *)data;
1096 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1097 offlined = nr_pages;
1099 *(long *)data += offlined;
1104 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1109 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1110 check_pages_isolated_cb);
1112 offlined = (long)ret;
1116 #ifdef CONFIG_MOVABLE_NODE
1118 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1121 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1125 #else /* CONFIG_MOVABLE_NODE */
1126 /* ensure the node has NORMAL memory if it is still online */
1127 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1129 struct pglist_data *pgdat = zone->zone_pgdat;
1130 unsigned long present_pages = 0;
1133 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1134 present_pages += pgdat->node_zones[zt].present_pages;
1136 if (present_pages > nr_pages)
1140 for (; zt <= ZONE_MOVABLE; zt++)
1141 present_pages += pgdat->node_zones[zt].present_pages;
1144 * we can't offline the last normal memory until all
1145 * higher memory is offlined.
1147 return present_pages == 0;
1149 #endif /* CONFIG_MOVABLE_NODE */
1151 /* check which state of node_states will be changed when offline memory */
1152 static void node_states_check_changes_offline(unsigned long nr_pages,
1153 struct zone *zone, struct memory_notify *arg)
1155 struct pglist_data *pgdat = zone->zone_pgdat;
1156 unsigned long present_pages = 0;
1157 enum zone_type zt, zone_last = ZONE_NORMAL;
1160 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1161 * contains nodes which have zones of 0...ZONE_NORMAL,
1162 * set zone_last to ZONE_NORMAL.
1164 * If we don't have HIGHMEM nor movable node,
1165 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1166 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1168 if (N_MEMORY == N_NORMAL_MEMORY)
1169 zone_last = ZONE_MOVABLE;
1172 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1173 * If the memory to be offline is in a zone of 0...zone_last,
1174 * and it is the last present memory, 0...zone_last will
1175 * become empty after offline , thus we can determind we will
1176 * need to clear the node from node_states[N_NORMAL_MEMORY].
1178 for (zt = 0; zt <= zone_last; zt++)
1179 present_pages += pgdat->node_zones[zt].present_pages;
1180 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1181 arg->status_change_nid_normal = zone_to_nid(zone);
1183 arg->status_change_nid_normal = -1;
1185 #ifdef CONFIG_HIGHMEM
1187 * If we have movable node, node_states[N_HIGH_MEMORY]
1188 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1189 * set zone_last to ZONE_HIGHMEM.
1191 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1192 * contains nodes which have zones of 0...ZONE_MOVABLE,
1193 * set zone_last to ZONE_MOVABLE.
1195 zone_last = ZONE_HIGHMEM;
1196 if (N_MEMORY == N_HIGH_MEMORY)
1197 zone_last = ZONE_MOVABLE;
1199 for (; zt <= zone_last; zt++)
1200 present_pages += pgdat->node_zones[zt].present_pages;
1201 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1202 arg->status_change_nid_high = zone_to_nid(zone);
1204 arg->status_change_nid_high = -1;
1206 arg->status_change_nid_high = arg->status_change_nid_normal;
1210 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1212 zone_last = ZONE_MOVABLE;
1215 * check whether node_states[N_HIGH_MEMORY] will be changed
1216 * If we try to offline the last present @nr_pages from the node,
1217 * we can determind we will need to clear the node from
1218 * node_states[N_HIGH_MEMORY].
1220 for (; zt <= zone_last; zt++)
1221 present_pages += pgdat->node_zones[zt].present_pages;
1222 if (nr_pages >= present_pages)
1223 arg->status_change_nid = zone_to_nid(zone);
1225 arg->status_change_nid = -1;
1228 static void node_states_clear_node(int node, struct memory_notify *arg)
1230 if (arg->status_change_nid_normal >= 0)
1231 node_clear_state(node, N_NORMAL_MEMORY);
1233 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1234 (arg->status_change_nid_high >= 0))
1235 node_clear_state(node, N_HIGH_MEMORY);
1237 if ((N_MEMORY != N_HIGH_MEMORY) &&
1238 (arg->status_change_nid >= 0))
1239 node_clear_state(node, N_MEMORY);
1242 static int __ref __offline_pages(unsigned long start_pfn,
1243 unsigned long end_pfn, unsigned long timeout)
1245 unsigned long pfn, nr_pages, expire;
1246 long offlined_pages;
1247 int ret, drain, retry_max, node;
1249 struct memory_notify arg;
1251 BUG_ON(start_pfn >= end_pfn);
1252 /* at least, alignment against pageblock is necessary */
1253 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1255 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1257 /* This makes hotplug much easier...and readable.
1258 we assume this for now. .*/
1259 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1262 lock_memory_hotplug();
1264 zone = page_zone(pfn_to_page(start_pfn));
1265 node = zone_to_nid(zone);
1266 nr_pages = end_pfn - start_pfn;
1269 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1272 /* set above range as isolated */
1273 ret = start_isolate_page_range(start_pfn, end_pfn,
1274 MIGRATE_MOVABLE, true);
1278 arg.start_pfn = start_pfn;
1279 arg.nr_pages = nr_pages;
1280 node_states_check_changes_offline(nr_pages, zone, &arg);
1282 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1283 ret = notifier_to_errno(ret);
1285 goto failed_removal;
1288 expire = jiffies + timeout;
1292 /* start memory hot removal */
1294 if (time_after(jiffies, expire))
1295 goto failed_removal;
1297 if (signal_pending(current))
1298 goto failed_removal;
1301 lru_add_drain_all();
1306 pfn = scan_lru_pages(start_pfn, end_pfn);
1307 if (pfn) { /* We have page on LRU */
1308 ret = do_migrate_range(pfn, end_pfn);
1314 if (--retry_max == 0)
1315 goto failed_removal;
1321 /* drain all zone's lru pagevec, this is asynchronous... */
1322 lru_add_drain_all();
1324 /* drain pcp pages, this is synchronous. */
1327 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1328 if (offlined_pages < 0) {
1330 goto failed_removal;
1332 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1333 /* Ok, all of our target is isolated.
1334 We cannot do rollback at this point. */
1335 offline_isolated_pages(start_pfn, end_pfn);
1336 /* reset pagetype flags and makes migrate type to be MOVABLE */
1337 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1338 /* removal success */
1339 zone->managed_pages -= offlined_pages;
1340 zone->present_pages -= offlined_pages;
1341 zone->zone_pgdat->node_present_pages -= offlined_pages;
1342 totalram_pages -= offlined_pages;
1344 init_per_zone_wmark_min();
1346 if (!populated_zone(zone)) {
1347 zone_pcp_reset(zone);
1348 mutex_lock(&zonelists_mutex);
1349 build_all_zonelists(NULL, NULL);
1350 mutex_unlock(&zonelists_mutex);
1352 zone_pcp_update(zone);
1354 node_states_clear_node(node, &arg);
1355 if (arg.status_change_nid >= 0)
1358 vm_total_pages = nr_free_pagecache_pages();
1359 writeback_set_ratelimit();
1361 memory_notify(MEM_OFFLINE, &arg);
1362 unlock_memory_hotplug();
1366 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1367 (unsigned long long) start_pfn << PAGE_SHIFT,
1368 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1369 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1370 /* pushback to free area */
1371 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1374 unlock_memory_hotplug();
1378 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1380 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1384 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1385 * @start_pfn: start pfn of the memory range
1386 * @end_pfn: end pft of the memory range
1387 * @arg: argument passed to func
1388 * @func: callback for each memory section walked
1390 * This function walks through all present mem sections in range
1391 * [start_pfn, end_pfn) and call func on each mem section.
1393 * Returns the return value of func.
1395 static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1396 void *arg, int (*func)(struct memory_block *, void *))
1398 struct memory_block *mem = NULL;
1399 struct mem_section *section;
1400 unsigned long pfn, section_nr;
1403 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1404 section_nr = pfn_to_section_nr(pfn);
1405 if (!present_section_nr(section_nr))
1408 section = __nr_to_section(section_nr);
1409 /* same memblock? */
1411 if ((section_nr >= mem->start_section_nr) &&
1412 (section_nr <= mem->end_section_nr))
1415 mem = find_memory_block_hinted(section, mem);
1419 ret = func(mem, arg);
1421 kobject_put(&mem->dev.kobj);
1427 kobject_put(&mem->dev.kobj);
1433 * offline_memory_block_cb - callback function for offlining memory block
1434 * @mem: the memory block to be offlined
1435 * @arg: buffer to hold error msg
1437 * Always return 0, and put the error msg in arg if any.
1439 static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1442 int error = offline_memory_block(mem);
1444 if (error != 0 && *ret == 0)
1450 static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1452 int ret = !is_memblock_offlined(mem);
1455 pr_warn("removing memory fails, because memory "
1456 "[%#010llx-%#010llx] is onlined\n",
1457 PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1458 PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1463 int __ref remove_memory(u64 start, u64 size)
1465 unsigned long start_pfn, end_pfn;
1469 start_pfn = PFN_DOWN(start);
1470 end_pfn = start_pfn + PFN_DOWN(size);
1473 * When CONFIG_MEMCG is on, one memory block may be used by other
1474 * blocks to store page cgroup when onlining pages. But we don't know
1475 * in what order pages are onlined. So we iterate twice to offline
1477 * 1st iterate: offline every non primary memory block.
1478 * 2nd iterate: offline primary (i.e. first added) memory block.
1481 walk_memory_range(start_pfn, end_pfn, &ret,
1482 offline_memory_block_cb);
1492 lock_memory_hotplug();
1495 * we have offlined all memory blocks like this:
1496 * 1. lock memory hotplug
1497 * 2. offline a memory block
1498 * 3. unlock memory hotplug
1500 * repeat step1-3 to offline the memory block. All memory blocks
1501 * must be offlined before removing memory. But we don't hold the
1502 * lock in the whole operation. So we should check whether all
1503 * memory blocks are offlined.
1506 ret = walk_memory_range(start_pfn, end_pfn, NULL,
1507 is_memblock_offlined_cb);
1509 unlock_memory_hotplug();
1513 /* remove memmap entry */
1514 firmware_map_remove(start, start + size, "System RAM");
1516 unlock_memory_hotplug();
1521 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1525 int remove_memory(u64 start, u64 size)
1529 #endif /* CONFIG_MEMORY_HOTREMOVE */
1530 EXPORT_SYMBOL_GPL(remove_memory);