2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/sched/signal.h>
10 #include <linux/swap.h>
11 #include <linux/interrupt.h>
12 #include <linux/pagemap.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/memremap.h>
22 #include <linux/memory_hotplug.h>
23 #include <linux/highmem.h>
24 #include <linux/vmalloc.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/migrate.h>
28 #include <linux/page-isolation.h>
29 #include <linux/pfn.h>
30 #include <linux/suspend.h>
31 #include <linux/mm_inline.h>
32 #include <linux/firmware-map.h>
33 #include <linux/stop_machine.h>
34 #include <linux/hugetlb.h>
35 #include <linux/memblock.h>
36 #include <linux/bootmem.h>
37 #include <linux/compaction.h>
38 #include <linux/rmap.h>
40 #include <asm/tlbflush.h>
45 * online_page_callback contains pointer to current page onlining function.
46 * Initially it is generic_online_page(). If it is required it could be
47 * changed by calling set_online_page_callback() for callback registration
48 * and restore_online_page_callback() for generic callback restore.
51 static void generic_online_page(struct page
*page
);
53 static online_page_callback_t online_page_callback
= generic_online_page
;
54 static DEFINE_MUTEX(online_page_callback_lock
);
56 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock
);
58 static int default_kernel_zone
= ZONE_NORMAL
;
60 void get_online_mems(void)
62 percpu_down_read(&mem_hotplug_lock
);
65 void put_online_mems(void)
67 percpu_up_read(&mem_hotplug_lock
);
70 bool movable_node_enabled
= false;
72 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
73 bool memhp_auto_online
;
75 bool memhp_auto_online
= true;
77 EXPORT_SYMBOL_GPL(memhp_auto_online
);
79 static int __init
setup_memhp_default_state(char *str
)
81 if (!strcmp(str
, "online"))
82 memhp_auto_online
= true;
83 else if (!strcmp(str
, "offline"))
84 memhp_auto_online
= false;
88 __setup("memhp_default_state=", setup_memhp_default_state
);
90 void mem_hotplug_begin(void)
93 percpu_down_write(&mem_hotplug_lock
);
96 void mem_hotplug_done(void)
98 percpu_up_write(&mem_hotplug_lock
);
102 /* add this memory to iomem resource */
103 static struct resource
*register_memory_resource(u64 start
, u64 size
)
105 struct resource
*res
, *conflict
;
106 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
108 return ERR_PTR(-ENOMEM
);
110 res
->name
= "System RAM";
112 res
->end
= start
+ size
- 1;
113 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
114 conflict
= request_resource_conflict(&iomem_resource
, res
);
116 if (conflict
->desc
== IORES_DESC_DEVICE_PRIVATE_MEMORY
) {
117 pr_debug("Device unaddressable memory block "
118 "memory hotplug at %#010llx !\n",
119 (unsigned long long)start
);
121 pr_debug("System RAM resource %pR cannot be added\n", res
);
123 return ERR_PTR(-EEXIST
);
128 static void release_memory_resource(struct resource
*res
)
132 release_resource(res
);
137 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
138 void get_page_bootmem(unsigned long info
, struct page
*page
,
141 page
->freelist
= (void *)type
;
142 SetPagePrivate(page
);
143 set_page_private(page
, info
);
147 void put_page_bootmem(struct page
*page
)
151 type
= (unsigned long) page
->freelist
;
152 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
153 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
155 if (page_ref_dec_return(page
) == 1) {
156 page
->freelist
= NULL
;
157 ClearPagePrivate(page
);
158 set_page_private(page
, 0);
159 INIT_LIST_HEAD(&page
->lru
);
160 free_reserved_page(page
);
164 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
165 #ifndef 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 section_nr
= pfn_to_section_nr(start_pfn
);
173 ms
= __nr_to_section(section_nr
);
175 /* Get section's memmap address */
176 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
179 * Get page for the memmap's phys address
180 * XXX: need more consideration for sparse_vmemmap...
182 page
= virt_to_page(memmap
);
183 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
184 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
186 /* remember memmap's page */
187 for (i
= 0; i
< mapsize
; i
++, page
++)
188 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
190 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
191 page
= virt_to_page(usemap
);
193 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
195 for (i
= 0; i
< mapsize
; i
++, page
++)
196 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
199 #else /* CONFIG_SPARSEMEM_VMEMMAP */
200 static void register_page_bootmem_info_section(unsigned long start_pfn
)
202 unsigned long *usemap
, mapsize
, section_nr
, i
;
203 struct mem_section
*ms
;
204 struct page
*page
, *memmap
;
206 if (!pfn_valid(start_pfn
))
209 section_nr
= pfn_to_section_nr(start_pfn
);
210 ms
= __nr_to_section(section_nr
);
212 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
214 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
216 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
217 page
= virt_to_page(usemap
);
219 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
221 for (i
= 0; i
< mapsize
; i
++, page
++)
222 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
224 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
226 void __init
register_page_bootmem_info_node(struct pglist_data
*pgdat
)
228 unsigned long i
, pfn
, end_pfn
, nr_pages
;
229 int node
= pgdat
->node_id
;
232 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
233 page
= virt_to_page(pgdat
);
235 for (i
= 0; i
< nr_pages
; i
++, page
++)
236 get_page_bootmem(node
, page
, NODE_INFO
);
238 pfn
= pgdat
->node_start_pfn
;
239 end_pfn
= pgdat_end_pfn(pgdat
);
241 /* register section info */
242 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
244 * Some platforms can assign the same pfn to multiple nodes - on
245 * node0 as well as nodeN. To avoid registering a pfn against
246 * multiple nodes we check that this pfn does not already
247 * reside in some other nodes.
249 if (pfn_valid(pfn
) && (early_pfn_to_nid(pfn
) == node
))
250 register_page_bootmem_info_section(pfn
);
253 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
255 static int __meminit
__add_section(int nid
, unsigned long phys_start_pfn
,
261 if (pfn_valid(phys_start_pfn
))
264 ret
= sparse_add_one_section(NODE_DATA(nid
), phys_start_pfn
);
269 * Make all the pages reserved so that nobody will stumble over half
271 * FIXME: We also have to associate it with a node because page_to_nid
272 * relies on having page with the proper node.
274 for (i
= 0; i
< PAGES_PER_SECTION
; i
++) {
275 unsigned long pfn
= phys_start_pfn
+ i
;
280 page
= pfn_to_page(pfn
);
281 set_page_node(page
, nid
);
282 SetPageReserved(page
);
288 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
292 * Reasonably generic function for adding memory. It is
293 * expected that archs that support memory hotplug will
294 * call this function after deciding the zone to which to
297 int __ref
__add_pages(int nid
, unsigned long phys_start_pfn
,
298 unsigned long nr_pages
, bool want_memblock
)
302 int start_sec
, end_sec
;
303 struct vmem_altmap
*altmap
;
305 /* during initialize mem_map, align hot-added range to section */
306 start_sec
= pfn_to_section_nr(phys_start_pfn
);
307 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
309 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
312 * Validate altmap is within bounds of the total request
314 if (altmap
->base_pfn
!= phys_start_pfn
315 || vmem_altmap_offset(altmap
) > nr_pages
) {
316 pr_warn_once("memory add fail, invalid altmap\n");
323 for (i
= start_sec
; i
<= end_sec
; i
++) {
324 err
= __add_section(nid
, section_nr_to_pfn(i
), want_memblock
);
327 * EEXIST is finally dealt with by ioresource collision
328 * check. see add_memory() => register_memory_resource()
329 * Warning will be printed if there is collision.
331 if (err
&& (err
!= -EEXIST
))
336 vmemmap_populate_print_last();
340 EXPORT_SYMBOL_GPL(__add_pages
);
342 #ifdef CONFIG_MEMORY_HOTREMOVE
343 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
344 static unsigned long find_smallest_section_pfn(int nid
, struct zone
*zone
,
345 unsigned long start_pfn
,
346 unsigned long end_pfn
)
348 struct mem_section
*ms
;
350 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
351 ms
= __pfn_to_section(start_pfn
);
353 if (unlikely(!valid_section(ms
)))
356 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
359 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
368 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
369 static unsigned long find_biggest_section_pfn(int nid
, struct zone
*zone
,
370 unsigned long start_pfn
,
371 unsigned long end_pfn
)
373 struct mem_section
*ms
;
376 /* pfn is the end pfn of a memory section. */
378 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
379 ms
= __pfn_to_section(pfn
);
381 if (unlikely(!valid_section(ms
)))
384 if (unlikely(pfn_to_nid(pfn
) != nid
))
387 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
396 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
397 unsigned long end_pfn
)
399 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
400 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
401 unsigned long zone_end_pfn
= z
;
403 struct mem_section
*ms
;
404 int nid
= zone_to_nid(zone
);
406 zone_span_writelock(zone
);
407 if (zone_start_pfn
== start_pfn
) {
409 * If the section is smallest section in the zone, it need
410 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
411 * In this case, we find second smallest valid mem_section
412 * for shrinking zone.
414 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
417 zone
->zone_start_pfn
= pfn
;
418 zone
->spanned_pages
= zone_end_pfn
- pfn
;
420 } else if (zone_end_pfn
== end_pfn
) {
422 * If the section is biggest section in the zone, it need
423 * shrink zone->spanned_pages.
424 * In this case, we find second biggest valid mem_section for
427 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
430 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
434 * The section is not biggest or smallest mem_section in the zone, it
435 * only creates a hole in the zone. So in this case, we need not
436 * change the zone. But perhaps, the zone has only hole data. Thus
437 * it check the zone has only hole or not.
439 pfn
= zone_start_pfn
;
440 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
441 ms
= __pfn_to_section(pfn
);
443 if (unlikely(!valid_section(ms
)))
446 if (page_zone(pfn_to_page(pfn
)) != zone
)
449 /* If the section is current section, it continues the loop */
450 if (start_pfn
== pfn
)
453 /* If we find valid section, we have nothing to do */
454 zone_span_writeunlock(zone
);
458 /* The zone has no valid section */
459 zone
->zone_start_pfn
= 0;
460 zone
->spanned_pages
= 0;
461 zone_span_writeunlock(zone
);
464 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
465 unsigned long start_pfn
, unsigned long end_pfn
)
467 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
468 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
469 unsigned long pgdat_end_pfn
= p
;
471 struct mem_section
*ms
;
472 int nid
= pgdat
->node_id
;
474 if (pgdat_start_pfn
== start_pfn
) {
476 * If the section is smallest section in the pgdat, it need
477 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
478 * In this case, we find second smallest valid mem_section
479 * for shrinking zone.
481 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
484 pgdat
->node_start_pfn
= pfn
;
485 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
487 } else if (pgdat_end_pfn
== end_pfn
) {
489 * If the section is biggest section in the pgdat, it need
490 * shrink pgdat->node_spanned_pages.
491 * In this case, we find second biggest valid mem_section for
494 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
497 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
501 * If the section is not biggest or smallest mem_section in the pgdat,
502 * it only creates a hole in the pgdat. So in this case, we need not
504 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
505 * has only hole or not.
507 pfn
= pgdat_start_pfn
;
508 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
509 ms
= __pfn_to_section(pfn
);
511 if (unlikely(!valid_section(ms
)))
514 if (pfn_to_nid(pfn
) != nid
)
517 /* If the section is current section, it continues the loop */
518 if (start_pfn
== pfn
)
521 /* If we find valid section, we have nothing to do */
525 /* The pgdat has no valid section */
526 pgdat
->node_start_pfn
= 0;
527 pgdat
->node_spanned_pages
= 0;
530 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
532 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
533 int nr_pages
= PAGES_PER_SECTION
;
536 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
537 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
538 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
539 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
542 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
543 unsigned long map_offset
)
545 unsigned long start_pfn
;
549 if (!valid_section(ms
))
552 ret
= unregister_memory_section(ms
);
556 scn_nr
= __section_nr(ms
);
557 start_pfn
= section_nr_to_pfn((unsigned long)scn_nr
);
558 __remove_zone(zone
, start_pfn
);
560 sparse_remove_one_section(zone
, ms
, map_offset
);
565 * __remove_pages() - remove sections of pages from a zone
566 * @zone: zone from which pages need to be removed
567 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
568 * @nr_pages: number of pages to remove (must be multiple of section size)
570 * Generic helper function to remove section mappings and sysfs entries
571 * for the section of the memory we are removing. Caller needs to make
572 * sure that pages are marked reserved and zones are adjust properly by
573 * calling offline_pages().
575 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
576 unsigned long nr_pages
)
579 unsigned long map_offset
= 0;
580 int sections_to_remove
, ret
= 0;
582 /* In the ZONE_DEVICE case device driver owns the memory region */
583 if (is_dev_zone(zone
)) {
584 struct page
*page
= pfn_to_page(phys_start_pfn
);
585 struct vmem_altmap
*altmap
;
587 altmap
= to_vmem_altmap((unsigned long) page
);
589 map_offset
= vmem_altmap_offset(altmap
);
591 resource_size_t start
, size
;
593 start
= phys_start_pfn
<< PAGE_SHIFT
;
594 size
= nr_pages
* PAGE_SIZE
;
596 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
599 resource_size_t endres
= start
+ size
- 1;
601 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
602 &start
, &endres
, ret
);
606 clear_zone_contiguous(zone
);
609 * We can only remove entire sections
611 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
612 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
614 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
615 for (i
= 0; i
< sections_to_remove
; i
++) {
616 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
618 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
624 set_zone_contiguous(zone
);
628 #endif /* CONFIG_MEMORY_HOTREMOVE */
630 int set_online_page_callback(online_page_callback_t callback
)
635 mutex_lock(&online_page_callback_lock
);
637 if (online_page_callback
== generic_online_page
) {
638 online_page_callback
= callback
;
642 mutex_unlock(&online_page_callback_lock
);
647 EXPORT_SYMBOL_GPL(set_online_page_callback
);
649 int restore_online_page_callback(online_page_callback_t callback
)
654 mutex_lock(&online_page_callback_lock
);
656 if (online_page_callback
== callback
) {
657 online_page_callback
= generic_online_page
;
661 mutex_unlock(&online_page_callback_lock
);
666 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
668 void __online_page_set_limits(struct page
*page
)
671 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
673 void __online_page_increment_counters(struct page
*page
)
675 adjust_managed_page_count(page
, 1);
677 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
679 void __online_page_free(struct page
*page
)
681 __free_reserved_page(page
);
683 EXPORT_SYMBOL_GPL(__online_page_free
);
685 static void generic_online_page(struct page
*page
)
687 __online_page_set_limits(page
);
688 __online_page_increment_counters(page
);
689 __online_page_free(page
);
692 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
696 unsigned long onlined_pages
= *(unsigned long *)arg
;
699 if (PageReserved(pfn_to_page(start_pfn
)))
700 for (i
= 0; i
< nr_pages
; i
++) {
701 page
= pfn_to_page(start_pfn
+ i
);
702 (*online_page_callback
)(page
);
706 online_mem_sections(start_pfn
, start_pfn
+ nr_pages
);
708 *(unsigned long *)arg
= onlined_pages
;
712 /* check which state of node_states will be changed when online memory */
713 static void node_states_check_changes_online(unsigned long nr_pages
,
714 struct zone
*zone
, struct memory_notify
*arg
)
716 int nid
= zone_to_nid(zone
);
717 enum zone_type zone_last
= ZONE_NORMAL
;
720 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
721 * contains nodes which have zones of 0...ZONE_NORMAL,
722 * set zone_last to ZONE_NORMAL.
724 * If we don't have HIGHMEM nor movable node,
725 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
726 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
728 if (N_MEMORY
== N_NORMAL_MEMORY
)
729 zone_last
= ZONE_MOVABLE
;
732 * if the memory to be online is in a zone of 0...zone_last, and
733 * the zones of 0...zone_last don't have memory before online, we will
734 * need to set the node to node_states[N_NORMAL_MEMORY] after
735 * the memory is online.
737 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
738 arg
->status_change_nid_normal
= nid
;
740 arg
->status_change_nid_normal
= -1;
742 #ifdef CONFIG_HIGHMEM
744 * If we have movable node, node_states[N_HIGH_MEMORY]
745 * contains nodes which have zones of 0...ZONE_HIGHMEM,
746 * set zone_last to ZONE_HIGHMEM.
748 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
749 * contains nodes which have zones of 0...ZONE_MOVABLE,
750 * set zone_last to ZONE_MOVABLE.
752 zone_last
= ZONE_HIGHMEM
;
753 if (N_MEMORY
== N_HIGH_MEMORY
)
754 zone_last
= ZONE_MOVABLE
;
756 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
757 arg
->status_change_nid_high
= nid
;
759 arg
->status_change_nid_high
= -1;
761 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
765 * if the node don't have memory befor online, we will need to
766 * set the node to node_states[N_MEMORY] after the memory
769 if (!node_state(nid
, N_MEMORY
))
770 arg
->status_change_nid
= nid
;
772 arg
->status_change_nid
= -1;
775 static void node_states_set_node(int node
, struct memory_notify
*arg
)
777 if (arg
->status_change_nid_normal
>= 0)
778 node_set_state(node
, N_NORMAL_MEMORY
);
780 if (arg
->status_change_nid_high
>= 0)
781 node_set_state(node
, N_HIGH_MEMORY
);
783 node_set_state(node
, N_MEMORY
);
786 static void __meminit
resize_zone_range(struct zone
*zone
, unsigned long start_pfn
,
787 unsigned long nr_pages
)
789 unsigned long old_end_pfn
= zone_end_pfn(zone
);
791 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
792 zone
->zone_start_pfn
= start_pfn
;
794 zone
->spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - zone
->zone_start_pfn
;
797 static void __meminit
resize_pgdat_range(struct pglist_data
*pgdat
, unsigned long start_pfn
,
798 unsigned long nr_pages
)
800 unsigned long old_end_pfn
= pgdat_end_pfn(pgdat
);
802 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
803 pgdat
->node_start_pfn
= start_pfn
;
805 pgdat
->node_spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - pgdat
->node_start_pfn
;
808 void __ref
move_pfn_range_to_zone(struct zone
*zone
,
809 unsigned long start_pfn
, unsigned long nr_pages
)
811 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
812 int nid
= pgdat
->node_id
;
815 if (zone_is_empty(zone
))
816 init_currently_empty_zone(zone
, start_pfn
, nr_pages
);
818 clear_zone_contiguous(zone
);
820 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
821 pgdat_resize_lock(pgdat
, &flags
);
822 zone_span_writelock(zone
);
823 resize_zone_range(zone
, start_pfn
, nr_pages
);
824 zone_span_writeunlock(zone
);
825 resize_pgdat_range(pgdat
, start_pfn
, nr_pages
);
826 pgdat_resize_unlock(pgdat
, &flags
);
829 * TODO now we have a visible range of pages which are not associated
830 * with their zone properly. Not nice but set_pfnblock_flags_mask
831 * expects the zone spans the pfn range. All the pages in the range
832 * are reserved so nobody should be touching them so we should be safe
834 memmap_init_zone(nr_pages
, nid
, zone_idx(zone
), start_pfn
, MEMMAP_HOTPLUG
);
836 set_zone_contiguous(zone
);
839 void set_default_mem_hotplug_zone(enum zone_type zone
)
841 default_kernel_zone
= zone
;
844 #ifdef CONFIG_HIGHMEM
845 #define MAX_KERNEL_ZONE ZONE_HIGHMEM
847 #define MAX_KERNEL_ZONE ZONE_NORMAL
851 * Returns a default kernel memory zone for the given pfn range.
852 * If no kernel zone covers this pfn range it will automatically go
853 * to the MAX_KERNEL_ZONE.
855 static struct zone
*default_kernel_zone_for_pfn(int nid
, unsigned long start_pfn
,
856 unsigned long nr_pages
)
858 struct pglist_data
*pgdat
= NODE_DATA(nid
);
861 for (zid
= 0; zid
<= MAX_KERNEL_ZONE
; zid
++) {
862 struct zone
*zone
= &pgdat
->node_zones
[zid
];
864 if (zone_intersects(zone
, start_pfn
, nr_pages
))
868 return &pgdat
->node_zones
[default_kernel_zone
];
871 static inline struct zone
*default_zone_for_pfn(int nid
, unsigned long start_pfn
,
872 unsigned long nr_pages
)
874 struct zone
*kernel_zone
= default_kernel_zone_for_pfn(nid
, start_pfn
,
876 struct zone
*movable_zone
= &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
877 bool in_kernel
= zone_intersects(kernel_zone
, start_pfn
, nr_pages
);
878 bool in_movable
= zone_intersects(movable_zone
, start_pfn
, nr_pages
);
881 * We inherit the existing zone in a simple case where zones do not
882 * overlap in the given range
884 if (in_kernel
^ in_movable
)
885 return (in_kernel
) ? kernel_zone
: movable_zone
;
888 * If the range doesn't belong to any zone or two zones overlap in the
889 * given range then we use movable zone only if movable_node is
890 * enabled because we always online to a kernel zone by default.
892 return movable_node_enabled
? movable_zone
: kernel_zone
;
895 struct zone
* zone_for_pfn_range(int online_type
, int nid
, unsigned start_pfn
,
896 unsigned long nr_pages
)
898 if (online_type
== MMOP_ONLINE_KERNEL
)
899 return default_kernel_zone_for_pfn(nid
, start_pfn
, nr_pages
);
901 if (online_type
== MMOP_ONLINE_MOVABLE
)
902 return &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
904 return default_zone_for_pfn(nid
, start_pfn
, nr_pages
);
908 * Associates the given pfn range with the given node and the zone appropriate
909 * for the given online type.
911 static struct zone
* __meminit
move_pfn_range(int online_type
, int nid
,
912 unsigned long start_pfn
, unsigned long nr_pages
)
916 zone
= zone_for_pfn_range(online_type
, nid
, start_pfn
, nr_pages
);
917 move_pfn_range_to_zone(zone
, start_pfn
, nr_pages
);
921 /* Must be protected by mem_hotplug_begin() or a device_lock */
922 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
925 unsigned long onlined_pages
= 0;
927 int need_zonelists_rebuild
= 0;
930 struct memory_notify arg
;
932 nid
= pfn_to_nid(pfn
);
933 /* associate pfn range with the zone */
934 zone
= move_pfn_range(online_type
, nid
, pfn
, nr_pages
);
937 arg
.nr_pages
= nr_pages
;
938 node_states_check_changes_online(nr_pages
, zone
, &arg
);
940 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
941 ret
= notifier_to_errno(ret
);
943 goto failed_addition
;
946 * If this zone is not populated, then it is not in zonelist.
947 * This means the page allocator ignores this zone.
948 * So, zonelist must be updated after online.
950 if (!populated_zone(zone
)) {
951 need_zonelists_rebuild
= 1;
952 setup_zone_pageset(zone
);
955 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
958 if (need_zonelists_rebuild
)
959 zone_pcp_reset(zone
);
960 goto failed_addition
;
963 zone
->present_pages
+= onlined_pages
;
965 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
966 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
967 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
970 node_states_set_node(nid
, &arg
);
971 if (need_zonelists_rebuild
)
972 build_all_zonelists(NULL
);
974 zone_pcp_update(zone
);
977 init_per_zone_wmark_min();
984 vm_total_pages
= nr_free_pagecache_pages();
986 writeback_set_ratelimit();
989 memory_notify(MEM_ONLINE
, &arg
);
993 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
994 (unsigned long long) pfn
<< PAGE_SHIFT
,
995 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
996 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
999 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1001 static void reset_node_present_pages(pg_data_t
*pgdat
)
1005 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1006 z
->present_pages
= 0;
1008 pgdat
->node_present_pages
= 0;
1011 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1012 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1014 struct pglist_data
*pgdat
;
1015 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1016 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1017 unsigned long start_pfn
= PFN_DOWN(start
);
1019 pgdat
= NODE_DATA(nid
);
1021 pgdat
= arch_alloc_nodedata(nid
);
1025 arch_refresh_nodedata(nid
, pgdat
);
1028 * Reset the nr_zones, order and classzone_idx before reuse.
1029 * Note that kswapd will init kswapd_classzone_idx properly
1030 * when it starts in the near future.
1032 pgdat
->nr_zones
= 0;
1033 pgdat
->kswapd_order
= 0;
1034 pgdat
->kswapd_classzone_idx
= 0;
1037 /* we can use NODE_DATA(nid) from here */
1039 /* init node's zones as empty zones, we don't have any present pages.*/
1040 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1041 pgdat
->per_cpu_nodestats
= alloc_percpu(struct per_cpu_nodestat
);
1044 * The node we allocated has no zone fallback lists. For avoiding
1045 * to access not-initialized zonelist, build here.
1047 build_all_zonelists(pgdat
);
1050 * zone->managed_pages is set to an approximate value in
1051 * free_area_init_core(), which will cause
1052 * /sys/device/system/node/nodeX/meminfo has wrong data.
1053 * So reset it to 0 before any memory is onlined.
1055 reset_node_managed_pages(pgdat
);
1058 * When memory is hot-added, all the memory is in offline state. So
1059 * clear all zones' present_pages because they will be updated in
1060 * online_pages() and offline_pages().
1062 reset_node_present_pages(pgdat
);
1067 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1069 arch_refresh_nodedata(nid
, NULL
);
1070 free_percpu(pgdat
->per_cpu_nodestats
);
1071 arch_free_nodedata(pgdat
);
1077 * try_online_node - online a node if offlined
1079 * called by cpu_up() to online a node without onlined memory.
1081 int try_online_node(int nid
)
1086 if (node_online(nid
))
1089 mem_hotplug_begin();
1090 pgdat
= hotadd_new_pgdat(nid
, 0);
1092 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1096 node_set_online(nid
);
1097 ret
= register_one_node(nid
);
1104 static int check_hotplug_memory_range(u64 start
, u64 size
)
1106 u64 start_pfn
= PFN_DOWN(start
);
1107 u64 nr_pages
= size
>> PAGE_SHIFT
;
1109 /* Memory range must be aligned with section */
1110 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1111 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1112 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1113 (unsigned long long)start
,
1114 (unsigned long long)size
);
1121 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1123 return device_online(&mem
->dev
);
1126 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1127 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1130 pg_data_t
*pgdat
= NULL
;
1136 size
= resource_size(res
);
1138 ret
= check_hotplug_memory_range(start
, size
);
1142 { /* Stupid hack to suppress address-never-null warning */
1143 void *p
= NODE_DATA(nid
);
1147 mem_hotplug_begin();
1150 * Add new range to memblock so that when hotadd_new_pgdat() is called
1151 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1152 * this new range and calculate total pages correctly. The range will
1153 * be removed at hot-remove time.
1155 memblock_add_node(start
, size
, nid
);
1157 new_node
= !node_online(nid
);
1159 pgdat
= hotadd_new_pgdat(nid
, start
);
1165 /* call arch's memory hotadd */
1166 ret
= arch_add_memory(nid
, start
, size
, true);
1171 /* we online node here. we can't roll back from here. */
1172 node_set_online(nid
);
1175 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1176 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1178 ret
= __register_one_node(nid
);
1183 * link memory sections under this node. This is already
1184 * done when creatig memory section in register_new_memory
1185 * but that depends to have the node registered so offline
1186 * nodes have to go through register_node.
1187 * TODO clean up this mess.
1189 ret
= link_mem_sections(nid
, start_pfn
, nr_pages
);
1192 * If sysfs file of new node can't create, cpu on the node
1193 * can't be hot-added. There is no rollback way now.
1194 * So, check by BUG_ON() to catch it reluctantly..
1199 /* create new memmap entry */
1200 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1202 /* online pages if requested */
1204 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1205 NULL
, online_memory_block
);
1210 /* rollback pgdat allocation and others */
1211 if (new_pgdat
&& pgdat
)
1212 rollback_node_hotadd(nid
, pgdat
);
1213 memblock_remove(start
, size
);
1219 EXPORT_SYMBOL_GPL(add_memory_resource
);
1221 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1223 struct resource
*res
;
1226 res
= register_memory_resource(start
, size
);
1228 return PTR_ERR(res
);
1230 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1232 release_memory_resource(res
);
1235 EXPORT_SYMBOL_GPL(add_memory
);
1237 #ifdef CONFIG_MEMORY_HOTREMOVE
1239 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1240 * set and the size of the free page is given by page_order(). Using this,
1241 * the function determines if the pageblock contains only free pages.
1242 * Due to buddy contraints, a free page at least the size of a pageblock will
1243 * be located at the start of the pageblock
1245 static inline int pageblock_free(struct page
*page
)
1247 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1250 /* Return the start of the next active pageblock after a given page */
1251 static struct page
*next_active_pageblock(struct page
*page
)
1253 /* Ensure the starting page is pageblock-aligned */
1254 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1256 /* If the entire pageblock is free, move to the end of free page */
1257 if (pageblock_free(page
)) {
1259 /* be careful. we don't have locks, page_order can be changed.*/
1260 order
= page_order(page
);
1261 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1262 return page
+ (1 << order
);
1265 return page
+ pageblock_nr_pages
;
1268 /* Checks if this range of memory is likely to be hot-removable. */
1269 bool is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1271 struct page
*page
= pfn_to_page(start_pfn
);
1272 unsigned long end_pfn
= min(start_pfn
+ nr_pages
, zone_end_pfn(page_zone(page
)));
1273 struct page
*end_page
= pfn_to_page(end_pfn
);
1275 /* Check the starting page of each pageblock within the range */
1276 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1277 if (!is_pageblock_removable_nolock(page
))
1282 /* All pageblocks in the memory block are likely to be hot-removable */
1287 * Confirm all pages in a range [start, end) belong to the same zone.
1288 * When true, return its valid [start, end).
1290 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
,
1291 unsigned long *valid_start
, unsigned long *valid_end
)
1293 unsigned long pfn
, sec_end_pfn
;
1294 unsigned long start
, end
;
1295 struct zone
*zone
= NULL
;
1298 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1300 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1301 /* Make sure the memory section is present first */
1302 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1304 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1305 pfn
+= MAX_ORDER_NR_PAGES
) {
1307 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1308 while ((i
< MAX_ORDER_NR_PAGES
) &&
1309 !pfn_valid_within(pfn
+ i
))
1311 if (i
== MAX_ORDER_NR_PAGES
|| pfn
+ i
>= end_pfn
)
1313 /* Check if we got outside of the zone */
1314 if (zone
&& !zone_spans_pfn(zone
, pfn
+ i
))
1316 page
= pfn_to_page(pfn
+ i
);
1317 if (zone
&& page_zone(page
) != zone
)
1321 zone
= page_zone(page
);
1322 end
= pfn
+ MAX_ORDER_NR_PAGES
;
1327 *valid_start
= start
;
1328 *valid_end
= min(end
, end_pfn
);
1336 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1337 * non-lru movable pages and hugepages). We scan pfn because it's much
1338 * easier than scanning over linked list. This function returns the pfn
1339 * of the first found movable page if it's found, otherwise 0.
1341 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1345 for (pfn
= start
; pfn
< end
; pfn
++) {
1346 struct page
*page
, *head
;
1349 if (!pfn_valid(pfn
))
1351 page
= pfn_to_page(pfn
);
1354 if (__PageMovable(page
))
1357 if (!PageHuge(page
))
1359 head
= compound_head(page
);
1360 if (page_huge_active(head
))
1362 skip
= (1 << compound_order(head
)) - (page
- head
);
1368 static struct page
*new_node_page(struct page
*page
, unsigned long private,
1371 int nid
= page_to_nid(page
);
1372 nodemask_t nmask
= node_states
[N_MEMORY
];
1375 * try to allocate from a different node but reuse this node if there
1376 * are no other online nodes to be used (e.g. we are offlining a part
1377 * of the only existing node)
1379 node_clear(nid
, nmask
);
1380 if (nodes_empty(nmask
))
1381 node_set(nid
, nmask
);
1383 return new_page_nodemask(page
, nid
, &nmask
);
1386 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1388 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1392 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1393 int not_managed
= 0;
1397 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1398 if (!pfn_valid(pfn
))
1400 page
= pfn_to_page(pfn
);
1402 if (PageHuge(page
)) {
1403 struct page
*head
= compound_head(page
);
1404 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1405 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1409 if (isolate_huge_page(page
, &source
))
1410 move_pages
-= 1 << compound_order(head
);
1412 } else if (thp_migration_supported() && PageTransHuge(page
))
1413 pfn
= page_to_pfn(compound_head(page
))
1414 + hpage_nr_pages(page
) - 1;
1417 * HWPoison pages have elevated reference counts so the migration would
1418 * fail on them. It also doesn't make any sense to migrate them in the
1419 * first place. Still try to unmap such a page in case it is still mapped
1420 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1421 * the unmap as the catch all safety net).
1423 if (PageHWPoison(page
)) {
1424 if (WARN_ON(PageLRU(page
)))
1425 isolate_lru_page(page
);
1426 if (page_mapped(page
))
1427 try_to_unmap(page
, TTU_IGNORE_MLOCK
| TTU_IGNORE_ACCESS
);
1431 if (!get_page_unless_zero(page
))
1434 * We can skip free pages. And we can deal with pages on
1435 * LRU and non-lru movable pages.
1438 ret
= isolate_lru_page(page
);
1440 ret
= isolate_movable_page(page
, ISOLATE_UNEVICTABLE
);
1441 if (!ret
) { /* Success */
1443 list_add_tail(&page
->lru
, &source
);
1445 if (!__PageMovable(page
))
1446 inc_node_page_state(page
, NR_ISOLATED_ANON
+
1447 page_is_file_cache(page
));
1450 #ifdef CONFIG_DEBUG_VM
1451 pr_alert("failed to isolate pfn %lx\n", pfn
);
1452 dump_page(page
, "isolation failed");
1455 /* Because we don't have big zone->lock. we should
1456 check this again here. */
1457 if (page_count(page
)) {
1464 if (!list_empty(&source
)) {
1466 putback_movable_pages(&source
);
1470 /* Allocate a new page from the nearest neighbor node */
1471 ret
= migrate_pages(&source
, new_node_page
, NULL
, 0,
1472 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1474 putback_movable_pages(&source
);
1481 * remove from free_area[] and mark all as Reserved.
1484 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1487 __offline_isolated_pages(start
, start
+ nr_pages
);
1492 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1494 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1495 offline_isolated_pages_cb
);
1499 * Check all pages in range, recoreded as memory resource, are isolated.
1502 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1506 long offlined
= *(long *)data
;
1507 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1508 offlined
= nr_pages
;
1510 *(long *)data
+= offlined
;
1515 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1520 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1521 check_pages_isolated_cb
);
1523 offlined
= (long)ret
;
1527 static int __init
cmdline_parse_movable_node(char *p
)
1529 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1530 movable_node_enabled
= true;
1532 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1536 early_param("movable_node", cmdline_parse_movable_node
);
1538 /* check which state of node_states will be changed when offline memory */
1539 static void node_states_check_changes_offline(unsigned long nr_pages
,
1540 struct zone
*zone
, struct memory_notify
*arg
)
1542 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1543 unsigned long present_pages
= 0;
1544 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1547 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1548 * contains nodes which have zones of 0...ZONE_NORMAL,
1549 * set zone_last to ZONE_NORMAL.
1551 * If we don't have HIGHMEM nor movable node,
1552 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1553 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1555 if (N_MEMORY
== N_NORMAL_MEMORY
)
1556 zone_last
= ZONE_MOVABLE
;
1559 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1560 * If the memory to be offline is in a zone of 0...zone_last,
1561 * and it is the last present memory, 0...zone_last will
1562 * become empty after offline , thus we can determind we will
1563 * need to clear the node from node_states[N_NORMAL_MEMORY].
1565 for (zt
= 0; zt
<= zone_last
; zt
++)
1566 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1567 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1568 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1570 arg
->status_change_nid_normal
= -1;
1572 #ifdef CONFIG_HIGHMEM
1574 * If we have movable node, node_states[N_HIGH_MEMORY]
1575 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1576 * set zone_last to ZONE_HIGHMEM.
1578 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1579 * contains nodes which have zones of 0...ZONE_MOVABLE,
1580 * set zone_last to ZONE_MOVABLE.
1582 zone_last
= ZONE_HIGHMEM
;
1583 if (N_MEMORY
== N_HIGH_MEMORY
)
1584 zone_last
= ZONE_MOVABLE
;
1586 for (; zt
<= zone_last
; zt
++)
1587 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1588 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1589 arg
->status_change_nid_high
= zone_to_nid(zone
);
1591 arg
->status_change_nid_high
= -1;
1593 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1597 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1599 zone_last
= ZONE_MOVABLE
;
1602 * check whether node_states[N_HIGH_MEMORY] will be changed
1603 * If we try to offline the last present @nr_pages from the node,
1604 * we can determind we will need to clear the node from
1605 * node_states[N_HIGH_MEMORY].
1607 for (; zt
<= zone_last
; zt
++)
1608 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1609 if (nr_pages
>= present_pages
)
1610 arg
->status_change_nid
= zone_to_nid(zone
);
1612 arg
->status_change_nid
= -1;
1615 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1617 if (arg
->status_change_nid_normal
>= 0)
1618 node_clear_state(node
, N_NORMAL_MEMORY
);
1620 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1621 (arg
->status_change_nid_high
>= 0))
1622 node_clear_state(node
, N_HIGH_MEMORY
);
1624 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1625 (arg
->status_change_nid
>= 0))
1626 node_clear_state(node
, N_MEMORY
);
1629 static int __ref
__offline_pages(unsigned long start_pfn
,
1630 unsigned long end_pfn
)
1632 unsigned long pfn
, nr_pages
;
1633 long offlined_pages
;
1635 unsigned long flags
;
1636 unsigned long valid_start
, valid_end
;
1638 struct memory_notify arg
;
1640 /* at least, alignment against pageblock is necessary */
1641 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1643 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1645 /* This makes hotplug much easier...and readable.
1646 we assume this for now. .*/
1647 if (!test_pages_in_a_zone(start_pfn
, end_pfn
, &valid_start
, &valid_end
))
1650 zone
= page_zone(pfn_to_page(valid_start
));
1651 node
= zone_to_nid(zone
);
1652 nr_pages
= end_pfn
- start_pfn
;
1654 /* set above range as isolated */
1655 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1656 MIGRATE_MOVABLE
, true);
1660 arg
.start_pfn
= start_pfn
;
1661 arg
.nr_pages
= nr_pages
;
1662 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1664 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1665 ret
= notifier_to_errno(ret
);
1667 goto failed_removal
;
1671 /* start memory hot removal */
1673 if (signal_pending(current
))
1674 goto failed_removal
;
1677 lru_add_drain_all_cpuslocked();
1678 drain_all_pages(zone
);
1680 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1681 if (pfn
) { /* We have movable pages */
1682 ret
= do_migrate_range(pfn
, end_pfn
);
1687 * dissolve free hugepages in the memory block before doing offlining
1688 * actually in order to make hugetlbfs's object counting consistent.
1690 ret
= dissolve_free_huge_pages(start_pfn
, end_pfn
);
1692 goto failed_removal
;
1694 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1695 if (offlined_pages
< 0)
1697 pr_info("Offlined Pages %ld\n", offlined_pages
);
1698 /* Ok, all of our target is isolated.
1699 We cannot do rollback at this point. */
1700 offline_isolated_pages(start_pfn
, end_pfn
);
1701 /* reset pagetype flags and makes migrate type to be MOVABLE */
1702 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1703 /* removal success */
1704 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1705 zone
->present_pages
-= offlined_pages
;
1707 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1708 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1709 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1711 init_per_zone_wmark_min();
1713 if (!populated_zone(zone
)) {
1714 zone_pcp_reset(zone
);
1715 build_all_zonelists(NULL
);
1717 zone_pcp_update(zone
);
1719 node_states_clear_node(node
, &arg
);
1720 if (arg
.status_change_nid
>= 0) {
1722 kcompactd_stop(node
);
1725 vm_total_pages
= nr_free_pagecache_pages();
1726 writeback_set_ratelimit();
1728 memory_notify(MEM_OFFLINE
, &arg
);
1732 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1733 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1734 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1735 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1736 /* pushback to free area */
1737 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1741 /* Must be protected by mem_hotplug_begin() or a device_lock */
1742 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1744 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
);
1746 #endif /* CONFIG_MEMORY_HOTREMOVE */
1749 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1750 * @start_pfn: start pfn of the memory range
1751 * @end_pfn: end pfn of the memory range
1752 * @arg: argument passed to func
1753 * @func: callback for each memory section walked
1755 * This function walks through all present mem sections in range
1756 * [start_pfn, end_pfn) and call func on each mem section.
1758 * Returns the return value of func.
1760 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1761 void *arg
, int (*func
)(struct memory_block
*, void *))
1763 struct memory_block
*mem
= NULL
;
1764 struct mem_section
*section
;
1765 unsigned long pfn
, section_nr
;
1768 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1769 section_nr
= pfn_to_section_nr(pfn
);
1770 if (!present_section_nr(section_nr
))
1773 section
= __nr_to_section(section_nr
);
1774 /* same memblock? */
1776 if ((section_nr
>= mem
->start_section_nr
) &&
1777 (section_nr
<= mem
->end_section_nr
))
1780 mem
= find_memory_block_hinted(section
, mem
);
1784 ret
= func(mem
, arg
);
1786 kobject_put(&mem
->dev
.kobj
);
1792 kobject_put(&mem
->dev
.kobj
);
1797 #ifdef CONFIG_MEMORY_HOTREMOVE
1798 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1800 int ret
= !is_memblock_offlined(mem
);
1802 if (unlikely(ret
)) {
1803 phys_addr_t beginpa
, endpa
;
1805 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1806 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1807 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1814 static int check_cpu_on_node(pg_data_t
*pgdat
)
1818 for_each_present_cpu(cpu
) {
1819 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1821 * the cpu on this node isn't removed, and we can't
1822 * offline this node.
1830 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1832 #ifdef CONFIG_ACPI_NUMA
1835 for_each_possible_cpu(cpu
)
1836 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1837 numa_clear_node(cpu
);
1841 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
1845 ret
= check_cpu_on_node(pgdat
);
1850 * the node will be offlined when we come here, so we can clear
1851 * the cpu_to_node() now.
1854 unmap_cpu_on_node(pgdat
);
1861 * Offline a node if all memory sections and cpus of the node are removed.
1863 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1864 * and online/offline operations before this call.
1866 void try_offline_node(int nid
)
1868 pg_data_t
*pgdat
= NODE_DATA(nid
);
1869 unsigned long start_pfn
= pgdat
->node_start_pfn
;
1870 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
1873 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1874 unsigned long section_nr
= pfn_to_section_nr(pfn
);
1876 if (!present_section_nr(section_nr
))
1879 if (pfn_to_nid(pfn
) != nid
)
1883 * some memory sections of this node are not removed, and we
1884 * can't offline node now.
1889 if (check_and_unmap_cpu_on_node(pgdat
))
1893 * all memory/cpu of this node are removed, we can offline this
1896 node_set_offline(nid
);
1897 unregister_one_node(nid
);
1899 EXPORT_SYMBOL(try_offline_node
);
1904 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1905 * and online/offline operations before this call, as required by
1906 * try_offline_node().
1908 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
1912 BUG_ON(check_hotplug_memory_range(start
, size
));
1914 mem_hotplug_begin();
1917 * All memory blocks must be offlined before removing memory. Check
1918 * whether all memory blocks in question are offline and trigger a BUG()
1919 * if this is not the case.
1921 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
1922 check_memblock_offlined_cb
);
1926 /* remove memmap entry */
1927 firmware_map_remove(start
, start
+ size
, "System RAM");
1928 memblock_free(start
, size
);
1929 memblock_remove(start
, size
);
1931 arch_remove_memory(start
, size
);
1933 try_offline_node(nid
);
1937 EXPORT_SYMBOL_GPL(remove_memory
);
1938 #endif /* CONFIG_MEMORY_HOTREMOVE */