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>
39 #include <asm/tlbflush.h>
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
50 static void generic_online_page(struct page
*page
);
52 static online_page_callback_t online_page_callback
= generic_online_page
;
53 static DEFINE_MUTEX(online_page_callback_lock
);
55 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock
);
57 static int default_kernel_zone
= ZONE_NORMAL
;
59 void get_online_mems(void)
61 percpu_down_read(&mem_hotplug_lock
);
64 void put_online_mems(void)
66 percpu_up_read(&mem_hotplug_lock
);
69 bool movable_node_enabled
= false;
71 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
72 bool memhp_auto_online
;
74 bool memhp_auto_online
= true;
76 EXPORT_SYMBOL_GPL(memhp_auto_online
);
78 static int __init
setup_memhp_default_state(char *str
)
80 if (!strcmp(str
, "online"))
81 memhp_auto_online
= true;
82 else if (!strcmp(str
, "offline"))
83 memhp_auto_online
= false;
87 __setup("memhp_default_state=", setup_memhp_default_state
);
89 void mem_hotplug_begin(void)
92 percpu_down_write(&mem_hotplug_lock
);
95 void mem_hotplug_done(void)
97 percpu_up_write(&mem_hotplug_lock
);
101 /* add this memory to iomem resource */
102 static struct resource
*register_memory_resource(u64 start
, u64 size
)
104 struct resource
*res
, *conflict
;
105 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
107 return ERR_PTR(-ENOMEM
);
109 res
->name
= "System RAM";
111 res
->end
= start
+ size
- 1;
112 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
113 conflict
= request_resource_conflict(&iomem_resource
, res
);
115 if (conflict
->desc
== IORES_DESC_DEVICE_PRIVATE_MEMORY
) {
116 pr_debug("Device unaddressable memory block "
117 "memory hotplug at %#010llx !\n",
118 (unsigned long long)start
);
120 pr_debug("System RAM resource %pR cannot be added\n", res
);
122 return ERR_PTR(-EEXIST
);
127 static void release_memory_resource(struct resource
*res
)
131 release_resource(res
);
136 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
137 void get_page_bootmem(unsigned long info
, struct page
*page
,
140 page
->freelist
= (void *)type
;
141 SetPagePrivate(page
);
142 set_page_private(page
, info
);
146 void put_page_bootmem(struct page
*page
)
150 type
= (unsigned long) page
->freelist
;
151 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
152 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
154 if (page_ref_dec_return(page
) == 1) {
155 page
->freelist
= NULL
;
156 ClearPagePrivate(page
);
157 set_page_private(page
, 0);
158 INIT_LIST_HEAD(&page
->lru
);
159 free_reserved_page(page
);
163 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
164 #ifndef CONFIG_SPARSEMEM_VMEMMAP
165 static void register_page_bootmem_info_section(unsigned long start_pfn
)
167 unsigned long *usemap
, mapsize
, section_nr
, i
;
168 struct mem_section
*ms
;
169 struct page
*page
, *memmap
;
171 section_nr
= pfn_to_section_nr(start_pfn
);
172 ms
= __nr_to_section(section_nr
);
174 /* Get section's memmap address */
175 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
178 * Get page for the memmap's phys address
179 * XXX: need more consideration for sparse_vmemmap...
181 page
= virt_to_page(memmap
);
182 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
183 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
185 /* remember memmap's page */
186 for (i
= 0; i
< mapsize
; i
++, page
++)
187 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
189 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
190 page
= virt_to_page(usemap
);
192 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
194 for (i
= 0; i
< mapsize
; i
++, page
++)
195 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
198 #else /* CONFIG_SPARSEMEM_VMEMMAP */
199 static void register_page_bootmem_info_section(unsigned long start_pfn
)
201 unsigned long *usemap
, mapsize
, section_nr
, i
;
202 struct mem_section
*ms
;
203 struct page
*page
, *memmap
;
205 if (!pfn_valid(start_pfn
))
208 section_nr
= pfn_to_section_nr(start_pfn
);
209 ms
= __nr_to_section(section_nr
);
211 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
213 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
215 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
216 page
= virt_to_page(usemap
);
218 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
220 for (i
= 0; i
< mapsize
; i
++, page
++)
221 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
223 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
225 void __init
register_page_bootmem_info_node(struct pglist_data
*pgdat
)
227 unsigned long i
, pfn
, end_pfn
, nr_pages
;
228 int node
= pgdat
->node_id
;
231 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
232 page
= virt_to_page(pgdat
);
234 for (i
= 0; i
< nr_pages
; i
++, page
++)
235 get_page_bootmem(node
, page
, NODE_INFO
);
237 pfn
= pgdat
->node_start_pfn
;
238 end_pfn
= pgdat_end_pfn(pgdat
);
240 /* register section info */
241 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
243 * Some platforms can assign the same pfn to multiple nodes - on
244 * node0 as well as nodeN. To avoid registering a pfn against
245 * multiple nodes we check that this pfn does not already
246 * reside in some other nodes.
248 if (pfn_valid(pfn
) && (early_pfn_to_nid(pfn
) == node
))
249 register_page_bootmem_info_section(pfn
);
252 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
254 static int __meminit
__add_section(int nid
, unsigned long phys_start_pfn
,
260 if (pfn_valid(phys_start_pfn
))
263 ret
= sparse_add_one_section(NODE_DATA(nid
), phys_start_pfn
);
268 * Make all the pages reserved so that nobody will stumble over half
270 * FIXME: We also have to associate it with a node because page_to_nid
271 * relies on having page with the proper node.
273 for (i
= 0; i
< PAGES_PER_SECTION
; i
++) {
274 unsigned long pfn
= phys_start_pfn
+ i
;
279 page
= pfn_to_page(pfn
);
280 set_page_node(page
, nid
);
281 SetPageReserved(page
);
287 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
291 * Reasonably generic function for adding memory. It is
292 * expected that archs that support memory hotplug will
293 * call this function after deciding the zone to which to
296 int __ref
__add_pages(int nid
, unsigned long phys_start_pfn
,
297 unsigned long nr_pages
, bool want_memblock
)
301 int start_sec
, end_sec
;
302 struct vmem_altmap
*altmap
;
304 /* during initialize mem_map, align hot-added range to section */
305 start_sec
= pfn_to_section_nr(phys_start_pfn
);
306 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
308 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
311 * Validate altmap is within bounds of the total request
313 if (altmap
->base_pfn
!= phys_start_pfn
314 || vmem_altmap_offset(altmap
) > nr_pages
) {
315 pr_warn_once("memory add fail, invalid altmap\n");
322 for (i
= start_sec
; i
<= end_sec
; i
++) {
323 err
= __add_section(nid
, section_nr_to_pfn(i
), want_memblock
);
326 * EEXIST is finally dealt with by ioresource collision
327 * check. see add_memory() => register_memory_resource()
328 * Warning will be printed if there is collision.
330 if (err
&& (err
!= -EEXIST
))
335 vmemmap_populate_print_last();
339 EXPORT_SYMBOL_GPL(__add_pages
);
341 #ifdef CONFIG_MEMORY_HOTREMOVE
342 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
343 static unsigned long find_smallest_section_pfn(int nid
, struct zone
*zone
,
344 unsigned long start_pfn
,
345 unsigned long end_pfn
)
347 struct mem_section
*ms
;
349 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
350 ms
= __pfn_to_section(start_pfn
);
352 if (unlikely(!valid_section(ms
)))
355 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
358 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
367 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
368 static unsigned long find_biggest_section_pfn(int nid
, struct zone
*zone
,
369 unsigned long start_pfn
,
370 unsigned long end_pfn
)
372 struct mem_section
*ms
;
375 /* pfn is the end pfn of a memory section. */
377 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
378 ms
= __pfn_to_section(pfn
);
380 if (unlikely(!valid_section(ms
)))
383 if (unlikely(pfn_to_nid(pfn
) != nid
))
386 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
395 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
396 unsigned long end_pfn
)
398 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
399 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
400 unsigned long zone_end_pfn
= z
;
402 struct mem_section
*ms
;
403 int nid
= zone_to_nid(zone
);
405 zone_span_writelock(zone
);
406 if (zone_start_pfn
== start_pfn
) {
408 * If the section is smallest section in the zone, it need
409 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
410 * In this case, we find second smallest valid mem_section
411 * for shrinking zone.
413 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
416 zone
->zone_start_pfn
= pfn
;
417 zone
->spanned_pages
= zone_end_pfn
- pfn
;
419 } else if (zone_end_pfn
== end_pfn
) {
421 * If the section is biggest section in the zone, it need
422 * shrink zone->spanned_pages.
423 * In this case, we find second biggest valid mem_section for
426 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
429 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
433 * The section is not biggest or smallest mem_section in the zone, it
434 * only creates a hole in the zone. So in this case, we need not
435 * change the zone. But perhaps, the zone has only hole data. Thus
436 * it check the zone has only hole or not.
438 pfn
= zone_start_pfn
;
439 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
440 ms
= __pfn_to_section(pfn
);
442 if (unlikely(!valid_section(ms
)))
445 if (page_zone(pfn_to_page(pfn
)) != zone
)
448 /* If the section is current section, it continues the loop */
449 if (start_pfn
== pfn
)
452 /* If we find valid section, we have nothing to do */
453 zone_span_writeunlock(zone
);
457 /* The zone has no valid section */
458 zone
->zone_start_pfn
= 0;
459 zone
->spanned_pages
= 0;
460 zone_span_writeunlock(zone
);
463 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
464 unsigned long start_pfn
, unsigned long end_pfn
)
466 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
467 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
468 unsigned long pgdat_end_pfn
= p
;
470 struct mem_section
*ms
;
471 int nid
= pgdat
->node_id
;
473 if (pgdat_start_pfn
== start_pfn
) {
475 * If the section is smallest section in the pgdat, it need
476 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
477 * In this case, we find second smallest valid mem_section
478 * for shrinking zone.
480 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
483 pgdat
->node_start_pfn
= pfn
;
484 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
486 } else if (pgdat_end_pfn
== end_pfn
) {
488 * If the section is biggest section in the pgdat, it need
489 * shrink pgdat->node_spanned_pages.
490 * In this case, we find second biggest valid mem_section for
493 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
496 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
500 * If the section is not biggest or smallest mem_section in the pgdat,
501 * it only creates a hole in the pgdat. So in this case, we need not
503 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
504 * has only hole or not.
506 pfn
= pgdat_start_pfn
;
507 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
508 ms
= __pfn_to_section(pfn
);
510 if (unlikely(!valid_section(ms
)))
513 if (pfn_to_nid(pfn
) != nid
)
516 /* If the section is current section, it continues the loop */
517 if (start_pfn
== pfn
)
520 /* If we find valid section, we have nothing to do */
524 /* The pgdat has no valid section */
525 pgdat
->node_start_pfn
= 0;
526 pgdat
->node_spanned_pages
= 0;
529 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
531 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
532 int nr_pages
= PAGES_PER_SECTION
;
535 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
536 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
537 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
538 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
541 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
542 unsigned long map_offset
)
544 unsigned long start_pfn
;
548 if (!valid_section(ms
))
551 ret
= unregister_memory_section(ms
);
555 scn_nr
= __section_nr(ms
);
556 start_pfn
= section_nr_to_pfn((unsigned long)scn_nr
);
557 __remove_zone(zone
, start_pfn
);
559 sparse_remove_one_section(zone
, ms
, map_offset
);
564 * __remove_pages() - remove sections of pages from a zone
565 * @zone: zone from which pages need to be removed
566 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
567 * @nr_pages: number of pages to remove (must be multiple of section size)
569 * Generic helper function to remove section mappings and sysfs entries
570 * for the section of the memory we are removing. Caller needs to make
571 * sure that pages are marked reserved and zones are adjust properly by
572 * calling offline_pages().
574 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
575 unsigned long nr_pages
)
578 unsigned long map_offset
= 0;
579 int sections_to_remove
, ret
= 0;
581 /* In the ZONE_DEVICE case device driver owns the memory region */
582 if (is_dev_zone(zone
)) {
583 struct page
*page
= pfn_to_page(phys_start_pfn
);
584 struct vmem_altmap
*altmap
;
586 altmap
= to_vmem_altmap((unsigned long) page
);
588 map_offset
= vmem_altmap_offset(altmap
);
590 resource_size_t start
, size
;
592 start
= phys_start_pfn
<< PAGE_SHIFT
;
593 size
= nr_pages
* PAGE_SIZE
;
595 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
598 resource_size_t endres
= start
+ size
- 1;
600 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
601 &start
, &endres
, ret
);
605 clear_zone_contiguous(zone
);
608 * We can only remove entire sections
610 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
611 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
613 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
614 for (i
= 0; i
< sections_to_remove
; i
++) {
615 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
617 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
623 set_zone_contiguous(zone
);
627 #endif /* CONFIG_MEMORY_HOTREMOVE */
629 int set_online_page_callback(online_page_callback_t callback
)
634 mutex_lock(&online_page_callback_lock
);
636 if (online_page_callback
== generic_online_page
) {
637 online_page_callback
= callback
;
641 mutex_unlock(&online_page_callback_lock
);
646 EXPORT_SYMBOL_GPL(set_online_page_callback
);
648 int restore_online_page_callback(online_page_callback_t callback
)
653 mutex_lock(&online_page_callback_lock
);
655 if (online_page_callback
== callback
) {
656 online_page_callback
= generic_online_page
;
660 mutex_unlock(&online_page_callback_lock
);
665 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
667 void __online_page_set_limits(struct page
*page
)
670 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
672 void __online_page_increment_counters(struct page
*page
)
674 adjust_managed_page_count(page
, 1);
676 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
678 void __online_page_free(struct page
*page
)
680 __free_reserved_page(page
);
682 EXPORT_SYMBOL_GPL(__online_page_free
);
684 static void generic_online_page(struct page
*page
)
686 __online_page_set_limits(page
);
687 __online_page_increment_counters(page
);
688 __online_page_free(page
);
691 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
695 unsigned long onlined_pages
= *(unsigned long *)arg
;
698 if (PageReserved(pfn_to_page(start_pfn
)))
699 for (i
= 0; i
< nr_pages
; i
++) {
700 page
= pfn_to_page(start_pfn
+ i
);
701 (*online_page_callback
)(page
);
705 online_mem_sections(start_pfn
, start_pfn
+ nr_pages
);
707 *(unsigned long *)arg
= onlined_pages
;
711 /* check which state of node_states will be changed when online memory */
712 static void node_states_check_changes_online(unsigned long nr_pages
,
713 struct zone
*zone
, struct memory_notify
*arg
)
715 int nid
= zone_to_nid(zone
);
716 enum zone_type zone_last
= ZONE_NORMAL
;
719 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
720 * contains nodes which have zones of 0...ZONE_NORMAL,
721 * set zone_last to ZONE_NORMAL.
723 * If we don't have HIGHMEM nor movable node,
724 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
725 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
727 if (N_MEMORY
== N_NORMAL_MEMORY
)
728 zone_last
= ZONE_MOVABLE
;
731 * if the memory to be online is in a zone of 0...zone_last, and
732 * the zones of 0...zone_last don't have memory before online, we will
733 * need to set the node to node_states[N_NORMAL_MEMORY] after
734 * the memory is online.
736 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
737 arg
->status_change_nid_normal
= nid
;
739 arg
->status_change_nid_normal
= -1;
741 #ifdef CONFIG_HIGHMEM
743 * If we have movable node, node_states[N_HIGH_MEMORY]
744 * contains nodes which have zones of 0...ZONE_HIGHMEM,
745 * set zone_last to ZONE_HIGHMEM.
747 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
748 * contains nodes which have zones of 0...ZONE_MOVABLE,
749 * set zone_last to ZONE_MOVABLE.
751 zone_last
= ZONE_HIGHMEM
;
752 if (N_MEMORY
== N_HIGH_MEMORY
)
753 zone_last
= ZONE_MOVABLE
;
755 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
756 arg
->status_change_nid_high
= nid
;
758 arg
->status_change_nid_high
= -1;
760 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
764 * if the node don't have memory befor online, we will need to
765 * set the node to node_states[N_MEMORY] after the memory
768 if (!node_state(nid
, N_MEMORY
))
769 arg
->status_change_nid
= nid
;
771 arg
->status_change_nid
= -1;
774 static void node_states_set_node(int node
, struct memory_notify
*arg
)
776 if (arg
->status_change_nid_normal
>= 0)
777 node_set_state(node
, N_NORMAL_MEMORY
);
779 if (arg
->status_change_nid_high
>= 0)
780 node_set_state(node
, N_HIGH_MEMORY
);
782 node_set_state(node
, N_MEMORY
);
785 static void __meminit
resize_zone_range(struct zone
*zone
, unsigned long start_pfn
,
786 unsigned long nr_pages
)
788 unsigned long old_end_pfn
= zone_end_pfn(zone
);
790 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
791 zone
->zone_start_pfn
= start_pfn
;
793 zone
->spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - zone
->zone_start_pfn
;
796 static void __meminit
resize_pgdat_range(struct pglist_data
*pgdat
, unsigned long start_pfn
,
797 unsigned long nr_pages
)
799 unsigned long old_end_pfn
= pgdat_end_pfn(pgdat
);
801 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
802 pgdat
->node_start_pfn
= start_pfn
;
804 pgdat
->node_spanned_pages
= max(start_pfn
+ nr_pages
, old_end_pfn
) - pgdat
->node_start_pfn
;
807 void __ref
move_pfn_range_to_zone(struct zone
*zone
,
808 unsigned long start_pfn
, unsigned long nr_pages
)
810 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
811 int nid
= pgdat
->node_id
;
814 if (zone_is_empty(zone
))
815 init_currently_empty_zone(zone
, start_pfn
, nr_pages
);
817 clear_zone_contiguous(zone
);
819 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
820 pgdat_resize_lock(pgdat
, &flags
);
821 zone_span_writelock(zone
);
822 resize_zone_range(zone
, start_pfn
, nr_pages
);
823 zone_span_writeunlock(zone
);
824 resize_pgdat_range(pgdat
, start_pfn
, nr_pages
);
825 pgdat_resize_unlock(pgdat
, &flags
);
828 * TODO now we have a visible range of pages which are not associated
829 * with their zone properly. Not nice but set_pfnblock_flags_mask
830 * expects the zone spans the pfn range. All the pages in the range
831 * are reserved so nobody should be touching them so we should be safe
833 memmap_init_zone(nr_pages
, nid
, zone_idx(zone
), start_pfn
, MEMMAP_HOTPLUG
);
835 set_zone_contiguous(zone
);
838 void set_default_mem_hotplug_zone(enum zone_type zone
)
840 default_kernel_zone
= zone
;
843 #ifdef CONFIG_HIGHMEM
844 #define MAX_KERNEL_ZONE ZONE_HIGHMEM
846 #define MAX_KERNEL_ZONE ZONE_NORMAL
850 * Returns a default kernel memory zone for the given pfn range.
851 * If no kernel zone covers this pfn range it will automatically go
852 * to the MAX_KERNEL_ZONE.
854 static struct zone
*default_kernel_zone_for_pfn(int nid
, unsigned long start_pfn
,
855 unsigned long nr_pages
)
857 struct pglist_data
*pgdat
= NODE_DATA(nid
);
860 for (zid
= 0; zid
<= MAX_KERNEL_ZONE
; zid
++) {
861 struct zone
*zone
= &pgdat
->node_zones
[zid
];
863 if (zone_intersects(zone
, start_pfn
, nr_pages
))
867 return &pgdat
->node_zones
[default_kernel_zone
];
870 static inline struct zone
*default_zone_for_pfn(int nid
, unsigned long start_pfn
,
871 unsigned long nr_pages
)
873 struct zone
*kernel_zone
= default_kernel_zone_for_pfn(nid
, start_pfn
,
875 struct zone
*movable_zone
= &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
876 bool in_kernel
= zone_intersects(kernel_zone
, start_pfn
, nr_pages
);
877 bool in_movable
= zone_intersects(movable_zone
, start_pfn
, nr_pages
);
880 * We inherit the existing zone in a simple case where zones do not
881 * overlap in the given range
883 if (in_kernel
^ in_movable
)
884 return (in_kernel
) ? kernel_zone
: movable_zone
;
887 * If the range doesn't belong to any zone or two zones overlap in the
888 * given range then we use movable zone only if movable_node is
889 * enabled because we always online to a kernel zone by default.
891 return movable_node_enabled
? movable_zone
: kernel_zone
;
894 struct zone
* zone_for_pfn_range(int online_type
, int nid
, unsigned start_pfn
,
895 unsigned long nr_pages
)
897 if (online_type
== MMOP_ONLINE_KERNEL
)
898 return default_kernel_zone_for_pfn(nid
, start_pfn
, nr_pages
);
900 if (online_type
== MMOP_ONLINE_MOVABLE
)
901 return &NODE_DATA(nid
)->node_zones
[ZONE_MOVABLE
];
903 return default_zone_for_pfn(nid
, start_pfn
, nr_pages
);
907 * Associates the given pfn range with the given node and the zone appropriate
908 * for the given online type.
910 static struct zone
* __meminit
move_pfn_range(int online_type
, int nid
,
911 unsigned long start_pfn
, unsigned long nr_pages
)
915 zone
= zone_for_pfn_range(online_type
, nid
, start_pfn
, nr_pages
);
916 move_pfn_range_to_zone(zone
, start_pfn
, nr_pages
);
920 /* Must be protected by mem_hotplug_begin() or a device_lock */
921 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
924 unsigned long onlined_pages
= 0;
926 int need_zonelists_rebuild
= 0;
929 struct memory_notify arg
;
931 nid
= pfn_to_nid(pfn
);
932 /* associate pfn range with the zone */
933 zone
= move_pfn_range(online_type
, nid
, pfn
, nr_pages
);
936 arg
.nr_pages
= nr_pages
;
937 node_states_check_changes_online(nr_pages
, zone
, &arg
);
939 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
940 ret
= notifier_to_errno(ret
);
942 goto failed_addition
;
945 * If this zone is not populated, then it is not in zonelist.
946 * This means the page allocator ignores this zone.
947 * So, zonelist must be updated after online.
949 if (!populated_zone(zone
)) {
950 need_zonelists_rebuild
= 1;
951 setup_zone_pageset(zone
);
954 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
957 if (need_zonelists_rebuild
)
958 zone_pcp_reset(zone
);
959 goto failed_addition
;
962 zone
->present_pages
+= onlined_pages
;
964 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
965 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
966 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
969 node_states_set_node(nid
, &arg
);
970 if (need_zonelists_rebuild
)
971 build_all_zonelists(NULL
);
973 zone_pcp_update(zone
);
976 init_per_zone_wmark_min();
983 vm_total_pages
= nr_free_pagecache_pages();
985 writeback_set_ratelimit();
988 memory_notify(MEM_ONLINE
, &arg
);
992 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
993 (unsigned long long) pfn
<< PAGE_SHIFT
,
994 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
995 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
998 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1000 static void reset_node_present_pages(pg_data_t
*pgdat
)
1004 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1005 z
->present_pages
= 0;
1007 pgdat
->node_present_pages
= 0;
1010 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1011 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1013 struct pglist_data
*pgdat
;
1014 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1015 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1016 unsigned long start_pfn
= PFN_DOWN(start
);
1018 pgdat
= NODE_DATA(nid
);
1020 pgdat
= arch_alloc_nodedata(nid
);
1024 arch_refresh_nodedata(nid
, pgdat
);
1027 * Reset the nr_zones, order and classzone_idx before reuse.
1028 * Note that kswapd will init kswapd_classzone_idx properly
1029 * when it starts in the near future.
1031 pgdat
->nr_zones
= 0;
1032 pgdat
->kswapd_order
= 0;
1033 pgdat
->kswapd_classzone_idx
= 0;
1036 /* we can use NODE_DATA(nid) from here */
1038 /* init node's zones as empty zones, we don't have any present pages.*/
1039 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1040 pgdat
->per_cpu_nodestats
= alloc_percpu(struct per_cpu_nodestat
);
1043 * The node we allocated has no zone fallback lists. For avoiding
1044 * to access not-initialized zonelist, build here.
1046 build_all_zonelists(pgdat
);
1049 * zone->managed_pages is set to an approximate value in
1050 * free_area_init_core(), which will cause
1051 * /sys/device/system/node/nodeX/meminfo has wrong data.
1052 * So reset it to 0 before any memory is onlined.
1054 reset_node_managed_pages(pgdat
);
1057 * When memory is hot-added, all the memory is in offline state. So
1058 * clear all zones' present_pages because they will be updated in
1059 * online_pages() and offline_pages().
1061 reset_node_present_pages(pgdat
);
1066 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1068 arch_refresh_nodedata(nid
, NULL
);
1069 free_percpu(pgdat
->per_cpu_nodestats
);
1070 arch_free_nodedata(pgdat
);
1076 * try_online_node - online a node if offlined
1078 * called by cpu_up() to online a node without onlined memory.
1080 int try_online_node(int nid
)
1085 if (node_online(nid
))
1088 mem_hotplug_begin();
1089 pgdat
= hotadd_new_pgdat(nid
, 0);
1091 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1095 node_set_online(nid
);
1096 ret
= register_one_node(nid
);
1103 static int check_hotplug_memory_range(u64 start
, u64 size
)
1105 u64 start_pfn
= PFN_DOWN(start
);
1106 u64 nr_pages
= size
>> PAGE_SHIFT
;
1108 /* Memory range must be aligned with section */
1109 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1110 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1111 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1112 (unsigned long long)start
,
1113 (unsigned long long)size
);
1120 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1122 return device_online(&mem
->dev
);
1125 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1126 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1129 pg_data_t
*pgdat
= NULL
;
1135 size
= resource_size(res
);
1137 ret
= check_hotplug_memory_range(start
, size
);
1141 { /* Stupid hack to suppress address-never-null warning */
1142 void *p
= NODE_DATA(nid
);
1146 mem_hotplug_begin();
1149 * Add new range to memblock so that when hotadd_new_pgdat() is called
1150 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1151 * this new range and calculate total pages correctly. The range will
1152 * be removed at hot-remove time.
1154 memblock_add_node(start
, size
, nid
);
1156 new_node
= !node_online(nid
);
1158 pgdat
= hotadd_new_pgdat(nid
, start
);
1164 /* call arch's memory hotadd */
1165 ret
= arch_add_memory(nid
, start
, size
, true);
1170 /* we online node here. we can't roll back from here. */
1171 node_set_online(nid
);
1174 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1175 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1177 ret
= __register_one_node(nid
);
1182 * link memory sections under this node. This is already
1183 * done when creatig memory section in register_new_memory
1184 * but that depends to have the node registered so offline
1185 * nodes have to go through register_node.
1186 * TODO clean up this mess.
1188 ret
= link_mem_sections(nid
, start_pfn
, nr_pages
);
1191 * If sysfs file of new node can't create, cpu on the node
1192 * can't be hot-added. There is no rollback way now.
1193 * So, check by BUG_ON() to catch it reluctantly..
1198 /* create new memmap entry */
1199 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1201 /* online pages if requested */
1203 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1204 NULL
, online_memory_block
);
1209 /* rollback pgdat allocation and others */
1210 if (new_pgdat
&& pgdat
)
1211 rollback_node_hotadd(nid
, pgdat
);
1212 memblock_remove(start
, size
);
1218 EXPORT_SYMBOL_GPL(add_memory_resource
);
1220 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1222 struct resource
*res
;
1225 res
= register_memory_resource(start
, size
);
1227 return PTR_ERR(res
);
1229 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1231 release_memory_resource(res
);
1234 EXPORT_SYMBOL_GPL(add_memory
);
1236 #ifdef CONFIG_MEMORY_HOTREMOVE
1238 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1239 * set and the size of the free page is given by page_order(). Using this,
1240 * the function determines if the pageblock contains only free pages.
1241 * Due to buddy contraints, a free page at least the size of a pageblock will
1242 * be located at the start of the pageblock
1244 static inline int pageblock_free(struct page
*page
)
1246 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1249 /* Return the start of the next active pageblock after a given page */
1250 static struct page
*next_active_pageblock(struct page
*page
)
1252 /* Ensure the starting page is pageblock-aligned */
1253 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1255 /* If the entire pageblock is free, move to the end of free page */
1256 if (pageblock_free(page
)) {
1258 /* be careful. we don't have locks, page_order can be changed.*/
1259 order
= page_order(page
);
1260 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1261 return page
+ (1 << order
);
1264 return page
+ pageblock_nr_pages
;
1267 /* Checks if this range of memory is likely to be hot-removable. */
1268 bool is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1270 struct page
*page
= pfn_to_page(start_pfn
);
1271 struct page
*end_page
= page
+ nr_pages
;
1273 /* Check the starting page of each pageblock within the range */
1274 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1275 if (!is_pageblock_removable_nolock(page
))
1280 /* All pageblocks in the memory block are likely to be hot-removable */
1285 * Confirm all pages in a range [start, end) belong to the same zone.
1286 * When true, return its valid [start, end).
1288 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
,
1289 unsigned long *valid_start
, unsigned long *valid_end
)
1291 unsigned long pfn
, sec_end_pfn
;
1292 unsigned long start
, end
;
1293 struct zone
*zone
= NULL
;
1296 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1298 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1299 /* Make sure the memory section is present first */
1300 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1302 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1303 pfn
+= MAX_ORDER_NR_PAGES
) {
1305 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1306 while ((i
< MAX_ORDER_NR_PAGES
) &&
1307 !pfn_valid_within(pfn
+ i
))
1309 if (i
== MAX_ORDER_NR_PAGES
|| pfn
+ i
>= end_pfn
)
1311 page
= pfn_to_page(pfn
+ i
);
1312 if (zone
&& page_zone(page
) != zone
)
1316 zone
= page_zone(page
);
1317 end
= pfn
+ MAX_ORDER_NR_PAGES
;
1322 *valid_start
= start
;
1323 *valid_end
= min(end
, end_pfn
);
1331 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1332 * non-lru movable pages and hugepages). We scan pfn because it's much
1333 * easier than scanning over linked list. This function returns the pfn
1334 * of the first found movable page if it's found, otherwise 0.
1336 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1340 for (pfn
= start
; pfn
< end
; pfn
++) {
1341 if (pfn_valid(pfn
)) {
1342 page
= pfn_to_page(pfn
);
1345 if (__PageMovable(page
))
1347 if (PageHuge(page
)) {
1348 if (page_huge_active(page
))
1351 pfn
= round_up(pfn
+ 1,
1352 1 << compound_order(page
)) - 1;
1359 static struct page
*new_node_page(struct page
*page
, unsigned long private,
1362 int nid
= page_to_nid(page
);
1363 nodemask_t nmask
= node_states
[N_MEMORY
];
1366 * try to allocate from a different node but reuse this node if there
1367 * are no other online nodes to be used (e.g. we are offlining a part
1368 * of the only existing node)
1370 node_clear(nid
, nmask
);
1371 if (nodes_empty(nmask
))
1372 node_set(nid
, nmask
);
1374 return new_page_nodemask(page
, nid
, &nmask
);
1377 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1379 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1383 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1384 int not_managed
= 0;
1388 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1389 if (!pfn_valid(pfn
))
1391 page
= pfn_to_page(pfn
);
1393 if (PageHuge(page
)) {
1394 struct page
*head
= compound_head(page
);
1395 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1396 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1400 if (isolate_huge_page(page
, &source
))
1401 move_pages
-= 1 << compound_order(head
);
1403 } else if (thp_migration_supported() && PageTransHuge(page
))
1404 pfn
= page_to_pfn(compound_head(page
))
1405 + hpage_nr_pages(page
) - 1;
1407 if (!get_page_unless_zero(page
))
1410 * We can skip free pages. And we can deal with pages on
1411 * LRU and non-lru movable pages.
1414 ret
= isolate_lru_page(page
);
1416 ret
= isolate_movable_page(page
, ISOLATE_UNEVICTABLE
);
1417 if (!ret
) { /* Success */
1419 list_add_tail(&page
->lru
, &source
);
1421 if (!__PageMovable(page
))
1422 inc_node_page_state(page
, NR_ISOLATED_ANON
+
1423 page_is_file_cache(page
));
1426 #ifdef CONFIG_DEBUG_VM
1427 pr_alert("failed to isolate pfn %lx\n", pfn
);
1428 dump_page(page
, "isolation failed");
1431 /* Because we don't have big zone->lock. we should
1432 check this again here. */
1433 if (page_count(page
)) {
1440 if (!list_empty(&source
)) {
1442 putback_movable_pages(&source
);
1446 /* Allocate a new page from the nearest neighbor node */
1447 ret
= migrate_pages(&source
, new_node_page
, NULL
, 0,
1448 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1450 putback_movable_pages(&source
);
1457 * remove from free_area[] and mark all as Reserved.
1460 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1463 __offline_isolated_pages(start
, start
+ nr_pages
);
1468 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1470 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1471 offline_isolated_pages_cb
);
1475 * Check all pages in range, recoreded as memory resource, are isolated.
1478 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1482 long offlined
= *(long *)data
;
1483 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1484 offlined
= nr_pages
;
1486 *(long *)data
+= offlined
;
1491 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1496 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1497 check_pages_isolated_cb
);
1499 offlined
= (long)ret
;
1503 static int __init
cmdline_parse_movable_node(char *p
)
1505 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1506 movable_node_enabled
= true;
1508 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1512 early_param("movable_node", cmdline_parse_movable_node
);
1514 /* check which state of node_states will be changed when offline memory */
1515 static void node_states_check_changes_offline(unsigned long nr_pages
,
1516 struct zone
*zone
, struct memory_notify
*arg
)
1518 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1519 unsigned long present_pages
= 0;
1520 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1523 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1524 * contains nodes which have zones of 0...ZONE_NORMAL,
1525 * set zone_last to ZONE_NORMAL.
1527 * If we don't have HIGHMEM nor movable node,
1528 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1529 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1531 if (N_MEMORY
== N_NORMAL_MEMORY
)
1532 zone_last
= ZONE_MOVABLE
;
1535 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1536 * If the memory to be offline is in a zone of 0...zone_last,
1537 * and it is the last present memory, 0...zone_last will
1538 * become empty after offline , thus we can determind we will
1539 * need to clear the node from node_states[N_NORMAL_MEMORY].
1541 for (zt
= 0; zt
<= zone_last
; zt
++)
1542 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1543 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1544 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1546 arg
->status_change_nid_normal
= -1;
1548 #ifdef CONFIG_HIGHMEM
1550 * If we have movable node, node_states[N_HIGH_MEMORY]
1551 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1552 * set zone_last to ZONE_HIGHMEM.
1554 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1555 * contains nodes which have zones of 0...ZONE_MOVABLE,
1556 * set zone_last to ZONE_MOVABLE.
1558 zone_last
= ZONE_HIGHMEM
;
1559 if (N_MEMORY
== N_HIGH_MEMORY
)
1560 zone_last
= ZONE_MOVABLE
;
1562 for (; zt
<= zone_last
; zt
++)
1563 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1564 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1565 arg
->status_change_nid_high
= zone_to_nid(zone
);
1567 arg
->status_change_nid_high
= -1;
1569 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1573 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1575 zone_last
= ZONE_MOVABLE
;
1578 * check whether node_states[N_HIGH_MEMORY] will be changed
1579 * If we try to offline the last present @nr_pages from the node,
1580 * we can determind we will need to clear the node from
1581 * node_states[N_HIGH_MEMORY].
1583 for (; zt
<= zone_last
; zt
++)
1584 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1585 if (nr_pages
>= present_pages
)
1586 arg
->status_change_nid
= zone_to_nid(zone
);
1588 arg
->status_change_nid
= -1;
1591 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1593 if (arg
->status_change_nid_normal
>= 0)
1594 node_clear_state(node
, N_NORMAL_MEMORY
);
1596 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1597 (arg
->status_change_nid_high
>= 0))
1598 node_clear_state(node
, N_HIGH_MEMORY
);
1600 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1601 (arg
->status_change_nid
>= 0))
1602 node_clear_state(node
, N_MEMORY
);
1605 static int __ref
__offline_pages(unsigned long start_pfn
,
1606 unsigned long end_pfn
)
1608 unsigned long pfn
, nr_pages
;
1609 long offlined_pages
;
1611 unsigned long flags
;
1612 unsigned long valid_start
, valid_end
;
1614 struct memory_notify arg
;
1616 /* at least, alignment against pageblock is necessary */
1617 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1619 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1621 /* This makes hotplug much easier...and readable.
1622 we assume this for now. .*/
1623 if (!test_pages_in_a_zone(start_pfn
, end_pfn
, &valid_start
, &valid_end
))
1626 zone
= page_zone(pfn_to_page(valid_start
));
1627 node
= zone_to_nid(zone
);
1628 nr_pages
= end_pfn
- start_pfn
;
1630 /* set above range as isolated */
1631 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1632 MIGRATE_MOVABLE
, true);
1636 arg
.start_pfn
= start_pfn
;
1637 arg
.nr_pages
= nr_pages
;
1638 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1640 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1641 ret
= notifier_to_errno(ret
);
1643 goto failed_removal
;
1647 /* start memory hot removal */
1649 if (signal_pending(current
))
1650 goto failed_removal
;
1653 lru_add_drain_all_cpuslocked();
1654 drain_all_pages(zone
);
1656 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1657 if (pfn
) { /* We have movable pages */
1658 ret
= do_migrate_range(pfn
, end_pfn
);
1663 * dissolve free hugepages in the memory block before doing offlining
1664 * actually in order to make hugetlbfs's object counting consistent.
1666 ret
= dissolve_free_huge_pages(start_pfn
, end_pfn
);
1668 goto failed_removal
;
1670 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1671 if (offlined_pages
< 0)
1673 pr_info("Offlined Pages %ld\n", offlined_pages
);
1674 /* Ok, all of our target is isolated.
1675 We cannot do rollback at this point. */
1676 offline_isolated_pages(start_pfn
, end_pfn
);
1677 /* reset pagetype flags and makes migrate type to be MOVABLE */
1678 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1679 /* removal success */
1680 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1681 zone
->present_pages
-= offlined_pages
;
1683 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1684 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1685 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1687 init_per_zone_wmark_min();
1689 if (!populated_zone(zone
)) {
1690 zone_pcp_reset(zone
);
1691 build_all_zonelists(NULL
);
1693 zone_pcp_update(zone
);
1695 node_states_clear_node(node
, &arg
);
1696 if (arg
.status_change_nid
>= 0) {
1698 kcompactd_stop(node
);
1701 vm_total_pages
= nr_free_pagecache_pages();
1702 writeback_set_ratelimit();
1704 memory_notify(MEM_OFFLINE
, &arg
);
1708 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1709 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1710 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1711 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1712 /* pushback to free area */
1713 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1717 /* Must be protected by mem_hotplug_begin() or a device_lock */
1718 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1720 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
);
1722 #endif /* CONFIG_MEMORY_HOTREMOVE */
1725 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1726 * @start_pfn: start pfn of the memory range
1727 * @end_pfn: end pfn of the memory range
1728 * @arg: argument passed to func
1729 * @func: callback for each memory section walked
1731 * This function walks through all present mem sections in range
1732 * [start_pfn, end_pfn) and call func on each mem section.
1734 * Returns the return value of func.
1736 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1737 void *arg
, int (*func
)(struct memory_block
*, void *))
1739 struct memory_block
*mem
= NULL
;
1740 struct mem_section
*section
;
1741 unsigned long pfn
, section_nr
;
1744 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1745 section_nr
= pfn_to_section_nr(pfn
);
1746 if (!present_section_nr(section_nr
))
1749 section
= __nr_to_section(section_nr
);
1750 /* same memblock? */
1752 if ((section_nr
>= mem
->start_section_nr
) &&
1753 (section_nr
<= mem
->end_section_nr
))
1756 mem
= find_memory_block_hinted(section
, mem
);
1760 ret
= func(mem
, arg
);
1762 kobject_put(&mem
->dev
.kobj
);
1768 kobject_put(&mem
->dev
.kobj
);
1773 #ifdef CONFIG_MEMORY_HOTREMOVE
1774 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1776 int ret
= !is_memblock_offlined(mem
);
1778 if (unlikely(ret
)) {
1779 phys_addr_t beginpa
, endpa
;
1781 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1782 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1783 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1790 static int check_cpu_on_node(pg_data_t
*pgdat
)
1794 for_each_present_cpu(cpu
) {
1795 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1797 * the cpu on this node isn't removed, and we can't
1798 * offline this node.
1806 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1808 #ifdef CONFIG_ACPI_NUMA
1811 for_each_possible_cpu(cpu
)
1812 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1813 numa_clear_node(cpu
);
1817 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
1821 ret
= check_cpu_on_node(pgdat
);
1826 * the node will be offlined when we come here, so we can clear
1827 * the cpu_to_node() now.
1830 unmap_cpu_on_node(pgdat
);
1837 * Offline a node if all memory sections and cpus of the node are removed.
1839 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1840 * and online/offline operations before this call.
1842 void try_offline_node(int nid
)
1844 pg_data_t
*pgdat
= NODE_DATA(nid
);
1845 unsigned long start_pfn
= pgdat
->node_start_pfn
;
1846 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
1849 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1850 unsigned long section_nr
= pfn_to_section_nr(pfn
);
1852 if (!present_section_nr(section_nr
))
1855 if (pfn_to_nid(pfn
) != nid
)
1859 * some memory sections of this node are not removed, and we
1860 * can't offline node now.
1865 if (check_and_unmap_cpu_on_node(pgdat
))
1869 * all memory/cpu of this node are removed, we can offline this
1872 node_set_offline(nid
);
1873 unregister_one_node(nid
);
1875 EXPORT_SYMBOL(try_offline_node
);
1880 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1881 * and online/offline operations before this call, as required by
1882 * try_offline_node().
1884 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
1888 BUG_ON(check_hotplug_memory_range(start
, size
));
1890 mem_hotplug_begin();
1893 * All memory blocks must be offlined before removing memory. Check
1894 * whether all memory blocks in question are offline and trigger a BUG()
1895 * if this is not the case.
1897 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
1898 check_memblock_offlined_cb
);
1902 /* remove memmap entry */
1903 firmware_map_remove(start
, start
+ size
, "System RAM");
1904 memblock_free(start
, size
);
1905 memblock_remove(start
, size
);
1907 arch_remove_memory(start
, size
);
1909 try_offline_node(nid
);
1913 EXPORT_SYMBOL_GPL(remove_memory
);
1914 #endif /* CONFIG_MEMORY_HOTREMOVE */