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 /* The same as the cpu_hotplug lock, but for memory hotplug. */
57 struct task_struct
*active_writer
;
58 struct mutex lock
; /* Synchronizes accesses to refcount, */
60 * Also blocks the new readers during
61 * an ongoing mem hotplug operation.
65 #ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map
;
69 .active_writer
= NULL
,
70 .lock
= __MUTEX_INITIALIZER(mem_hotplug
.lock
),
72 #ifdef CONFIG_DEBUG_LOCK_ALLOC
73 .dep_map
= {.name
= "mem_hotplug.lock" },
77 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
78 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
79 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
80 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
82 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
83 bool memhp_auto_online
;
85 bool memhp_auto_online
= true;
87 EXPORT_SYMBOL_GPL(memhp_auto_online
);
89 static int __init
setup_memhp_default_state(char *str
)
91 if (!strcmp(str
, "online"))
92 memhp_auto_online
= true;
93 else if (!strcmp(str
, "offline"))
94 memhp_auto_online
= false;
98 __setup("memhp_default_state=", setup_memhp_default_state
);
100 void get_online_mems(void)
103 if (mem_hotplug
.active_writer
== current
)
105 memhp_lock_acquire_read();
106 mutex_lock(&mem_hotplug
.lock
);
107 mem_hotplug
.refcount
++;
108 mutex_unlock(&mem_hotplug
.lock
);
112 void put_online_mems(void)
114 if (mem_hotplug
.active_writer
== current
)
116 mutex_lock(&mem_hotplug
.lock
);
118 if (WARN_ON(!mem_hotplug
.refcount
))
119 mem_hotplug
.refcount
++; /* try to fix things up */
121 if (!--mem_hotplug
.refcount
&& unlikely(mem_hotplug
.active_writer
))
122 wake_up_process(mem_hotplug
.active_writer
);
123 mutex_unlock(&mem_hotplug
.lock
);
124 memhp_lock_release();
128 /* Serializes write accesses to mem_hotplug.active_writer. */
129 static DEFINE_MUTEX(memory_add_remove_lock
);
131 void mem_hotplug_begin(void)
133 mutex_lock(&memory_add_remove_lock
);
135 mem_hotplug
.active_writer
= current
;
137 memhp_lock_acquire();
139 mutex_lock(&mem_hotplug
.lock
);
140 if (likely(!mem_hotplug
.refcount
))
142 __set_current_state(TASK_UNINTERRUPTIBLE
);
143 mutex_unlock(&mem_hotplug
.lock
);
148 void mem_hotplug_done(void)
150 mem_hotplug
.active_writer
= NULL
;
151 mutex_unlock(&mem_hotplug
.lock
);
152 memhp_lock_release();
153 mutex_unlock(&memory_add_remove_lock
);
156 /* add this memory to iomem resource */
157 static struct resource
*register_memory_resource(u64 start
, u64 size
)
159 struct resource
*res
;
160 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
162 return ERR_PTR(-ENOMEM
);
164 res
->name
= "System RAM";
166 res
->end
= start
+ size
- 1;
167 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
168 if (request_resource(&iomem_resource
, res
) < 0) {
169 pr_debug("System RAM resource %pR cannot be added\n", res
);
171 return ERR_PTR(-EEXIST
);
176 static void release_memory_resource(struct resource
*res
)
180 release_resource(res
);
185 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
186 void get_page_bootmem(unsigned long info
, struct page
*page
,
189 page
->freelist
= (void *)type
;
190 SetPagePrivate(page
);
191 set_page_private(page
, info
);
195 void put_page_bootmem(struct page
*page
)
199 type
= (unsigned long) page
->freelist
;
200 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
201 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
203 if (page_ref_dec_return(page
) == 1) {
204 page
->freelist
= NULL
;
205 ClearPagePrivate(page
);
206 set_page_private(page
, 0);
207 INIT_LIST_HEAD(&page
->lru
);
208 free_reserved_page(page
);
212 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
213 #ifndef CONFIG_SPARSEMEM_VMEMMAP
214 static void register_page_bootmem_info_section(unsigned long start_pfn
)
216 unsigned long *usemap
, mapsize
, section_nr
, i
;
217 struct mem_section
*ms
;
218 struct page
*page
, *memmap
;
220 section_nr
= pfn_to_section_nr(start_pfn
);
221 ms
= __nr_to_section(section_nr
);
223 /* Get section's memmap address */
224 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
227 * Get page for the memmap's phys address
228 * XXX: need more consideration for sparse_vmemmap...
230 page
= virt_to_page(memmap
);
231 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
232 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
234 /* remember memmap's page */
235 for (i
= 0; i
< mapsize
; i
++, page
++)
236 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
238 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
239 page
= virt_to_page(usemap
);
241 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
243 for (i
= 0; i
< mapsize
; i
++, page
++)
244 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
247 #else /* CONFIG_SPARSEMEM_VMEMMAP */
248 static void register_page_bootmem_info_section(unsigned long start_pfn
)
250 unsigned long *usemap
, mapsize
, section_nr
, i
;
251 struct mem_section
*ms
;
252 struct page
*page
, *memmap
;
254 if (!pfn_valid(start_pfn
))
257 section_nr
= pfn_to_section_nr(start_pfn
);
258 ms
= __nr_to_section(section_nr
);
260 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
262 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
264 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
265 page
= virt_to_page(usemap
);
267 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
269 for (i
= 0; i
< mapsize
; i
++, page
++)
270 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
272 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
274 void __init
register_page_bootmem_info_node(struct pglist_data
*pgdat
)
276 unsigned long i
, pfn
, end_pfn
, nr_pages
;
277 int node
= pgdat
->node_id
;
280 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
281 page
= virt_to_page(pgdat
);
283 for (i
= 0; i
< nr_pages
; i
++, page
++)
284 get_page_bootmem(node
, page
, NODE_INFO
);
286 pfn
= pgdat
->node_start_pfn
;
287 end_pfn
= pgdat_end_pfn(pgdat
);
289 /* register section info */
290 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
292 * Some platforms can assign the same pfn to multiple nodes - on
293 * node0 as well as nodeN. To avoid registering a pfn against
294 * multiple nodes we check that this pfn does not already
295 * reside in some other nodes.
297 if (pfn_valid(pfn
) && (early_pfn_to_nid(pfn
) == node
))
298 register_page_bootmem_info_section(pfn
);
301 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
303 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
304 unsigned long end_pfn
)
306 unsigned long old_zone_end_pfn
;
308 zone_span_writelock(zone
);
310 old_zone_end_pfn
= zone_end_pfn(zone
);
311 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
312 zone
->zone_start_pfn
= start_pfn
;
314 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
315 zone
->zone_start_pfn
;
317 zone_span_writeunlock(zone
);
320 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
321 unsigned long end_pfn
)
323 zone_span_writelock(zone
);
325 if (end_pfn
- start_pfn
) {
326 zone
->zone_start_pfn
= start_pfn
;
327 zone
->spanned_pages
= end_pfn
- start_pfn
;
330 * make it consist as free_area_init_core(),
331 * if spanned_pages = 0, then keep start_pfn = 0
333 zone
->zone_start_pfn
= 0;
334 zone
->spanned_pages
= 0;
337 zone_span_writeunlock(zone
);
340 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
341 unsigned long end_pfn
)
343 enum zone_type zid
= zone_idx(zone
);
344 int nid
= zone
->zone_pgdat
->node_id
;
347 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
348 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
351 static void __ref
ensure_zone_is_initialized(struct zone
*zone
,
352 unsigned long start_pfn
, unsigned long num_pages
)
354 if (!zone_is_initialized(zone
))
355 init_currently_empty_zone(zone
, start_pfn
, num_pages
);
358 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
359 unsigned long start_pfn
, unsigned long end_pfn
)
362 unsigned long z1_start_pfn
;
364 ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
366 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
368 /* can't move pfns which are higher than @z2 */
369 if (end_pfn
> zone_end_pfn(z2
))
371 /* the move out part must be at the left most of @z2 */
372 if (start_pfn
> z2
->zone_start_pfn
)
374 /* must included/overlap */
375 if (end_pfn
<= z2
->zone_start_pfn
)
378 /* use start_pfn for z1's start_pfn if z1 is empty */
379 if (!zone_is_empty(z1
))
380 z1_start_pfn
= z1
->zone_start_pfn
;
382 z1_start_pfn
= start_pfn
;
384 resize_zone(z1
, z1_start_pfn
, end_pfn
);
385 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
387 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
389 fix_zone_id(z1
, start_pfn
, end_pfn
);
393 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
397 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
398 unsigned long start_pfn
, unsigned long end_pfn
)
401 unsigned long z2_end_pfn
;
403 ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
405 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
407 /* can't move pfns which are lower than @z1 */
408 if (z1
->zone_start_pfn
> start_pfn
)
410 /* the move out part mast at the right most of @z1 */
411 if (zone_end_pfn(z1
) > end_pfn
)
413 /* must included/overlap */
414 if (start_pfn
>= zone_end_pfn(z1
))
417 /* use end_pfn for z2's end_pfn if z2 is empty */
418 if (!zone_is_empty(z2
))
419 z2_end_pfn
= zone_end_pfn(z2
);
421 z2_end_pfn
= end_pfn
;
423 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
424 resize_zone(z2
, start_pfn
, z2_end_pfn
);
426 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
428 fix_zone_id(z2
, start_pfn
, end_pfn
);
432 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
436 static struct zone
* __meminit
move_pfn_range(int zone_shift
,
437 unsigned long start_pfn
, unsigned long end_pfn
)
439 struct zone
*zone
= page_zone(pfn_to_page(start_pfn
));
443 ret
= move_pfn_range_left(zone
+ zone_shift
, zone
,
446 ret
= move_pfn_range_right(zone
, zone
+ zone_shift
,
452 return zone
+ zone_shift
;
455 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
456 unsigned long end_pfn
)
458 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
460 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
461 pgdat
->node_start_pfn
= start_pfn
;
463 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
464 pgdat
->node_start_pfn
;
467 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
469 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
470 int nr_pages
= PAGES_PER_SECTION
;
471 int nid
= pgdat
->node_id
;
473 unsigned long flags
, pfn
;
475 zone_type
= zone
- pgdat
->node_zones
;
476 ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
478 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
479 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
480 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
481 phys_start_pfn
+ nr_pages
);
482 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
483 memmap_init_zone(nr_pages
, nid
, zone_type
,
484 phys_start_pfn
, MEMMAP_HOTPLUG
);
486 /* online_page_range is called later and expects pages reserved */
487 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
491 SetPageReserved(pfn_to_page(pfn
));
496 static int __meminit
__add_section(int nid
, struct zone
*zone
,
497 unsigned long phys_start_pfn
)
501 if (pfn_valid(phys_start_pfn
))
504 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
509 ret
= __add_zone(zone
, phys_start_pfn
);
514 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
518 * Reasonably generic function for adding memory. It is
519 * expected that archs that support memory hotplug will
520 * call this function after deciding the zone to which to
523 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
524 unsigned long nr_pages
)
528 int start_sec
, end_sec
;
529 struct vmem_altmap
*altmap
;
531 clear_zone_contiguous(zone
);
533 /* during initialize mem_map, align hot-added range to section */
534 start_sec
= pfn_to_section_nr(phys_start_pfn
);
535 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
537 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
540 * Validate altmap is within bounds of the total request
542 if (altmap
->base_pfn
!= phys_start_pfn
543 || vmem_altmap_offset(altmap
) > nr_pages
) {
544 pr_warn_once("memory add fail, invalid altmap\n");
551 for (i
= start_sec
; i
<= end_sec
; i
++) {
552 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
));
555 * EEXIST is finally dealt with by ioresource collision
556 * check. see add_memory() => register_memory_resource()
557 * Warning will be printed if there is collision.
559 if (err
&& (err
!= -EEXIST
))
563 vmemmap_populate_print_last();
565 set_zone_contiguous(zone
);
568 EXPORT_SYMBOL_GPL(__add_pages
);
570 #ifdef CONFIG_MEMORY_HOTREMOVE
571 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
572 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
573 unsigned long start_pfn
,
574 unsigned long end_pfn
)
576 struct mem_section
*ms
;
578 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
579 ms
= __pfn_to_section(start_pfn
);
581 if (unlikely(!valid_section(ms
)))
584 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
587 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
596 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
597 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
598 unsigned long start_pfn
,
599 unsigned long end_pfn
)
601 struct mem_section
*ms
;
604 /* pfn is the end pfn of a memory section. */
606 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
607 ms
= __pfn_to_section(pfn
);
609 if (unlikely(!valid_section(ms
)))
612 if (unlikely(pfn_to_nid(pfn
) != nid
))
615 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
624 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
625 unsigned long end_pfn
)
627 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
628 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
629 unsigned long zone_end_pfn
= z
;
631 struct mem_section
*ms
;
632 int nid
= zone_to_nid(zone
);
634 zone_span_writelock(zone
);
635 if (zone_start_pfn
== start_pfn
) {
637 * If the section is smallest section in the zone, it need
638 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
639 * In this case, we find second smallest valid mem_section
640 * for shrinking zone.
642 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
645 zone
->zone_start_pfn
= pfn
;
646 zone
->spanned_pages
= zone_end_pfn
- pfn
;
648 } else if (zone_end_pfn
== end_pfn
) {
650 * If the section is biggest section in the zone, it need
651 * shrink zone->spanned_pages.
652 * In this case, we find second biggest valid mem_section for
655 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
658 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
662 * The section is not biggest or smallest mem_section in the zone, it
663 * only creates a hole in the zone. So in this case, we need not
664 * change the zone. But perhaps, the zone has only hole data. Thus
665 * it check the zone has only hole or not.
667 pfn
= zone_start_pfn
;
668 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
669 ms
= __pfn_to_section(pfn
);
671 if (unlikely(!valid_section(ms
)))
674 if (page_zone(pfn_to_page(pfn
)) != zone
)
677 /* If the section is current section, it continues the loop */
678 if (start_pfn
== pfn
)
681 /* If we find valid section, we have nothing to do */
682 zone_span_writeunlock(zone
);
686 /* The zone has no valid section */
687 zone
->zone_start_pfn
= 0;
688 zone
->spanned_pages
= 0;
689 zone_span_writeunlock(zone
);
692 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
693 unsigned long start_pfn
, unsigned long end_pfn
)
695 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
696 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
697 unsigned long pgdat_end_pfn
= p
;
699 struct mem_section
*ms
;
700 int nid
= pgdat
->node_id
;
702 if (pgdat_start_pfn
== start_pfn
) {
704 * If the section is smallest section in the pgdat, it need
705 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
706 * In this case, we find second smallest valid mem_section
707 * for shrinking zone.
709 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
712 pgdat
->node_start_pfn
= pfn
;
713 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
715 } else if (pgdat_end_pfn
== end_pfn
) {
717 * If the section is biggest section in the pgdat, it need
718 * shrink pgdat->node_spanned_pages.
719 * In this case, we find second biggest valid mem_section for
722 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
725 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
729 * If the section is not biggest or smallest mem_section in the pgdat,
730 * it only creates a hole in the pgdat. So in this case, we need not
732 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
733 * has only hole or not.
735 pfn
= pgdat_start_pfn
;
736 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
737 ms
= __pfn_to_section(pfn
);
739 if (unlikely(!valid_section(ms
)))
742 if (pfn_to_nid(pfn
) != nid
)
745 /* If the section is current section, it continues the loop */
746 if (start_pfn
== pfn
)
749 /* If we find valid section, we have nothing to do */
753 /* The pgdat has no valid section */
754 pgdat
->node_start_pfn
= 0;
755 pgdat
->node_spanned_pages
= 0;
758 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
760 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
761 int nr_pages
= PAGES_PER_SECTION
;
765 zone_type
= zone
- pgdat
->node_zones
;
767 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
768 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
769 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
770 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
773 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
774 unsigned long map_offset
)
776 unsigned long start_pfn
;
780 if (!valid_section(ms
))
783 ret
= unregister_memory_section(ms
);
787 scn_nr
= __section_nr(ms
);
788 start_pfn
= section_nr_to_pfn(scn_nr
);
789 __remove_zone(zone
, start_pfn
);
791 sparse_remove_one_section(zone
, ms
, map_offset
);
796 * __remove_pages() - remove sections of pages from a zone
797 * @zone: zone from which pages need to be removed
798 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
799 * @nr_pages: number of pages to remove (must be multiple of section size)
801 * Generic helper function to remove section mappings and sysfs entries
802 * for the section of the memory we are removing. Caller needs to make
803 * sure that pages are marked reserved and zones are adjust properly by
804 * calling offline_pages().
806 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
807 unsigned long nr_pages
)
810 unsigned long map_offset
= 0;
811 int sections_to_remove
, ret
= 0;
813 /* In the ZONE_DEVICE case device driver owns the memory region */
814 if (is_dev_zone(zone
)) {
815 struct page
*page
= pfn_to_page(phys_start_pfn
);
816 struct vmem_altmap
*altmap
;
818 altmap
= to_vmem_altmap((unsigned long) page
);
820 map_offset
= vmem_altmap_offset(altmap
);
822 resource_size_t start
, size
;
824 start
= phys_start_pfn
<< PAGE_SHIFT
;
825 size
= nr_pages
* PAGE_SIZE
;
827 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
830 resource_size_t endres
= start
+ size
- 1;
832 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
833 &start
, &endres
, ret
);
837 clear_zone_contiguous(zone
);
840 * We can only remove entire sections
842 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
843 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
845 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
846 for (i
= 0; i
< sections_to_remove
; i
++) {
847 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
849 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
855 set_zone_contiguous(zone
);
859 #endif /* CONFIG_MEMORY_HOTREMOVE */
861 int set_online_page_callback(online_page_callback_t callback
)
866 mutex_lock(&online_page_callback_lock
);
868 if (online_page_callback
== generic_online_page
) {
869 online_page_callback
= callback
;
873 mutex_unlock(&online_page_callback_lock
);
878 EXPORT_SYMBOL_GPL(set_online_page_callback
);
880 int restore_online_page_callback(online_page_callback_t callback
)
885 mutex_lock(&online_page_callback_lock
);
887 if (online_page_callback
== callback
) {
888 online_page_callback
= generic_online_page
;
892 mutex_unlock(&online_page_callback_lock
);
897 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
899 void __online_page_set_limits(struct page
*page
)
902 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
904 void __online_page_increment_counters(struct page
*page
)
906 adjust_managed_page_count(page
, 1);
908 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
910 void __online_page_free(struct page
*page
)
912 __free_reserved_page(page
);
914 EXPORT_SYMBOL_GPL(__online_page_free
);
916 static void generic_online_page(struct page
*page
)
918 __online_page_set_limits(page
);
919 __online_page_increment_counters(page
);
920 __online_page_free(page
);
923 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
927 unsigned long onlined_pages
= *(unsigned long *)arg
;
929 if (PageReserved(pfn_to_page(start_pfn
)))
930 for (i
= 0; i
< nr_pages
; i
++) {
931 page
= pfn_to_page(start_pfn
+ i
);
932 (*online_page_callback
)(page
);
935 *(unsigned long *)arg
= onlined_pages
;
939 #ifdef CONFIG_MOVABLE_NODE
941 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
944 static bool can_online_high_movable(int nid
)
948 #else /* CONFIG_MOVABLE_NODE */
949 /* ensure every online node has NORMAL memory */
950 static bool can_online_high_movable(int nid
)
952 return node_state(nid
, N_NORMAL_MEMORY
);
954 #endif /* CONFIG_MOVABLE_NODE */
956 /* check which state of node_states will be changed when online memory */
957 static void node_states_check_changes_online(unsigned long nr_pages
,
958 struct zone
*zone
, struct memory_notify
*arg
)
960 int nid
= zone_to_nid(zone
);
961 enum zone_type zone_last
= ZONE_NORMAL
;
964 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
965 * contains nodes which have zones of 0...ZONE_NORMAL,
966 * set zone_last to ZONE_NORMAL.
968 * If we don't have HIGHMEM nor movable node,
969 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
970 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
972 if (N_MEMORY
== N_NORMAL_MEMORY
)
973 zone_last
= ZONE_MOVABLE
;
976 * if the memory to be online is in a zone of 0...zone_last, and
977 * the zones of 0...zone_last don't have memory before online, we will
978 * need to set the node to node_states[N_NORMAL_MEMORY] after
979 * the memory is online.
981 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
982 arg
->status_change_nid_normal
= nid
;
984 arg
->status_change_nid_normal
= -1;
986 #ifdef CONFIG_HIGHMEM
988 * If we have movable node, node_states[N_HIGH_MEMORY]
989 * contains nodes which have zones of 0...ZONE_HIGHMEM,
990 * set zone_last to ZONE_HIGHMEM.
992 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
993 * contains nodes which have zones of 0...ZONE_MOVABLE,
994 * set zone_last to ZONE_MOVABLE.
996 zone_last
= ZONE_HIGHMEM
;
997 if (N_MEMORY
== N_HIGH_MEMORY
)
998 zone_last
= ZONE_MOVABLE
;
1000 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
1001 arg
->status_change_nid_high
= nid
;
1003 arg
->status_change_nid_high
= -1;
1005 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1009 * if the node don't have memory befor online, we will need to
1010 * set the node to node_states[N_MEMORY] after the memory
1013 if (!node_state(nid
, N_MEMORY
))
1014 arg
->status_change_nid
= nid
;
1016 arg
->status_change_nid
= -1;
1019 static void node_states_set_node(int node
, struct memory_notify
*arg
)
1021 if (arg
->status_change_nid_normal
>= 0)
1022 node_set_state(node
, N_NORMAL_MEMORY
);
1024 if (arg
->status_change_nid_high
>= 0)
1025 node_set_state(node
, N_HIGH_MEMORY
);
1027 node_set_state(node
, N_MEMORY
);
1030 bool zone_can_shift(unsigned long pfn
, unsigned long nr_pages
,
1031 enum zone_type target
, int *zone_shift
)
1033 struct zone
*zone
= page_zone(pfn_to_page(pfn
));
1034 enum zone_type idx
= zone_idx(zone
);
1040 /* pages must be at end of current zone */
1041 if (pfn
+ nr_pages
!= zone_end_pfn(zone
))
1044 /* no zones in use between current zone and target */
1045 for (i
= idx
+ 1; i
< target
; i
++)
1046 if (zone_is_initialized(zone
- idx
+ i
))
1051 /* pages must be at beginning of current zone */
1052 if (pfn
!= zone
->zone_start_pfn
)
1055 /* no zones in use between current zone and target */
1056 for (i
= target
+ 1; i
< idx
; i
++)
1057 if (zone_is_initialized(zone
- idx
+ i
))
1061 *zone_shift
= target
- idx
;
1065 /* Must be protected by mem_hotplug_begin() */
1066 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
1068 unsigned long flags
;
1069 unsigned long onlined_pages
= 0;
1071 int need_zonelists_rebuild
= 0;
1074 struct memory_notify arg
;
1078 * This doesn't need a lock to do pfn_to_page().
1079 * The section can't be removed here because of the
1080 * memory_block->state_mutex.
1082 zone
= page_zone(pfn_to_page(pfn
));
1084 if ((zone_idx(zone
) > ZONE_NORMAL
||
1085 online_type
== MMOP_ONLINE_MOVABLE
) &&
1086 !can_online_high_movable(pfn_to_nid(pfn
)))
1089 if (online_type
== MMOP_ONLINE_KERNEL
) {
1090 if (!zone_can_shift(pfn
, nr_pages
, ZONE_NORMAL
, &zone_shift
))
1092 } else if (online_type
== MMOP_ONLINE_MOVABLE
) {
1093 if (!zone_can_shift(pfn
, nr_pages
, ZONE_MOVABLE
, &zone_shift
))
1097 zone
= move_pfn_range(zone_shift
, pfn
, pfn
+ nr_pages
);
1101 arg
.start_pfn
= pfn
;
1102 arg
.nr_pages
= nr_pages
;
1103 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1105 nid
= zone_to_nid(zone
);
1107 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1108 ret
= notifier_to_errno(ret
);
1110 goto failed_addition
;
1113 * If this zone is not populated, then it is not in zonelist.
1114 * This means the page allocator ignores this zone.
1115 * So, zonelist must be updated after online.
1117 mutex_lock(&zonelists_mutex
);
1118 if (!populated_zone(zone
)) {
1119 need_zonelists_rebuild
= 1;
1120 build_all_zonelists(NULL
, zone
);
1123 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1124 online_pages_range
);
1126 if (need_zonelists_rebuild
)
1127 zone_pcp_reset(zone
);
1128 mutex_unlock(&zonelists_mutex
);
1129 goto failed_addition
;
1132 zone
->present_pages
+= onlined_pages
;
1134 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1135 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1136 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1138 if (onlined_pages
) {
1139 node_states_set_node(nid
, &arg
);
1140 if (need_zonelists_rebuild
)
1141 build_all_zonelists(NULL
, NULL
);
1143 zone_pcp_update(zone
);
1146 mutex_unlock(&zonelists_mutex
);
1148 init_per_zone_wmark_min();
1150 if (onlined_pages
) {
1155 vm_total_pages
= nr_free_pagecache_pages();
1157 writeback_set_ratelimit();
1160 memory_notify(MEM_ONLINE
, &arg
);
1164 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1165 (unsigned long long) pfn
<< PAGE_SHIFT
,
1166 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
1167 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1170 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1172 static void reset_node_present_pages(pg_data_t
*pgdat
)
1176 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1177 z
->present_pages
= 0;
1179 pgdat
->node_present_pages
= 0;
1182 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1183 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1185 struct pglist_data
*pgdat
;
1186 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1187 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1188 unsigned long start_pfn
= PFN_DOWN(start
);
1190 pgdat
= NODE_DATA(nid
);
1192 pgdat
= arch_alloc_nodedata(nid
);
1196 arch_refresh_nodedata(nid
, pgdat
);
1199 * Reset the nr_zones, order and classzone_idx before reuse.
1200 * Note that kswapd will init kswapd_classzone_idx properly
1201 * when it starts in the near future.
1203 pgdat
->nr_zones
= 0;
1204 pgdat
->kswapd_order
= 0;
1205 pgdat
->kswapd_classzone_idx
= 0;
1208 /* we can use NODE_DATA(nid) from here */
1210 /* init node's zones as empty zones, we don't have any present pages.*/
1211 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1212 pgdat
->per_cpu_nodestats
= alloc_percpu(struct per_cpu_nodestat
);
1215 * The node we allocated has no zone fallback lists. For avoiding
1216 * to access not-initialized zonelist, build here.
1218 mutex_lock(&zonelists_mutex
);
1219 build_all_zonelists(pgdat
, NULL
);
1220 mutex_unlock(&zonelists_mutex
);
1223 * zone->managed_pages is set to an approximate value in
1224 * free_area_init_core(), which will cause
1225 * /sys/device/system/node/nodeX/meminfo has wrong data.
1226 * So reset it to 0 before any memory is onlined.
1228 reset_node_managed_pages(pgdat
);
1231 * When memory is hot-added, all the memory is in offline state. So
1232 * clear all zones' present_pages because they will be updated in
1233 * online_pages() and offline_pages().
1235 reset_node_present_pages(pgdat
);
1240 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1242 arch_refresh_nodedata(nid
, NULL
);
1243 free_percpu(pgdat
->per_cpu_nodestats
);
1244 arch_free_nodedata(pgdat
);
1250 * try_online_node - online a node if offlined
1252 * called by cpu_up() to online a node without onlined memory.
1254 int try_online_node(int nid
)
1259 if (node_online(nid
))
1262 mem_hotplug_begin();
1263 pgdat
= hotadd_new_pgdat(nid
, 0);
1265 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1269 node_set_online(nid
);
1270 ret
= register_one_node(nid
);
1273 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1274 mutex_lock(&zonelists_mutex
);
1275 build_all_zonelists(NULL
, NULL
);
1276 mutex_unlock(&zonelists_mutex
);
1284 static int check_hotplug_memory_range(u64 start
, u64 size
)
1286 u64 start_pfn
= PFN_DOWN(start
);
1287 u64 nr_pages
= size
>> PAGE_SHIFT
;
1289 /* Memory range must be aligned with section */
1290 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1291 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1292 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1293 (unsigned long long)start
,
1294 (unsigned long long)size
);
1302 * If movable zone has already been setup, newly added memory should be check.
1303 * If its address is higher than movable zone, it should be added as movable.
1304 * Without this check, movable zone may overlap with other zone.
1306 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1308 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1309 pg_data_t
*pgdat
= NODE_DATA(nid
);
1310 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1312 if (zone_is_empty(movable_zone
))
1315 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1321 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
,
1324 #ifdef CONFIG_ZONE_DEVICE
1328 if (should_add_memory_movable(nid
, start
, size
))
1329 return ZONE_MOVABLE
;
1331 return zone_default
;
1334 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1336 return device_online(&mem
->dev
);
1339 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1340 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1343 pg_data_t
*pgdat
= NULL
;
1349 size
= resource_size(res
);
1351 ret
= check_hotplug_memory_range(start
, size
);
1355 { /* Stupid hack to suppress address-never-null warning */
1356 void *p
= NODE_DATA(nid
);
1360 mem_hotplug_begin();
1363 * Add new range to memblock so that when hotadd_new_pgdat() is called
1364 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1365 * this new range and calculate total pages correctly. The range will
1366 * be removed at hot-remove time.
1368 memblock_add_node(start
, size
, nid
);
1370 new_node
= !node_online(nid
);
1372 pgdat
= hotadd_new_pgdat(nid
, start
);
1378 /* call arch's memory hotadd */
1379 ret
= arch_add_memory(nid
, start
, size
, false);
1384 /* we online node here. we can't roll back from here. */
1385 node_set_online(nid
);
1388 ret
= register_one_node(nid
);
1390 * If sysfs file of new node can't create, cpu on the node
1391 * can't be hot-added. There is no rollback way now.
1392 * So, check by BUG_ON() to catch it reluctantly..
1397 /* create new memmap entry */
1398 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1400 /* online pages if requested */
1402 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1403 NULL
, online_memory_block
);
1408 /* rollback pgdat allocation and others */
1410 rollback_node_hotadd(nid
, pgdat
);
1411 memblock_remove(start
, size
);
1417 EXPORT_SYMBOL_GPL(add_memory_resource
);
1419 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1421 struct resource
*res
;
1424 res
= register_memory_resource(start
, size
);
1426 return PTR_ERR(res
);
1428 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1430 release_memory_resource(res
);
1433 EXPORT_SYMBOL_GPL(add_memory
);
1435 #ifdef CONFIG_MEMORY_HOTREMOVE
1437 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1438 * set and the size of the free page is given by page_order(). Using this,
1439 * the function determines if the pageblock contains only free pages.
1440 * Due to buddy contraints, a free page at least the size of a pageblock will
1441 * be located at the start of the pageblock
1443 static inline int pageblock_free(struct page
*page
)
1445 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1448 /* Return the start of the next active pageblock after a given page */
1449 static struct page
*next_active_pageblock(struct page
*page
)
1451 /* Ensure the starting page is pageblock-aligned */
1452 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1454 /* If the entire pageblock is free, move to the end of free page */
1455 if (pageblock_free(page
)) {
1457 /* be careful. we don't have locks, page_order can be changed.*/
1458 order
= page_order(page
);
1459 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1460 return page
+ (1 << order
);
1463 return page
+ pageblock_nr_pages
;
1466 /* Checks if this range of memory is likely to be hot-removable. */
1467 bool is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1469 struct page
*page
= pfn_to_page(start_pfn
);
1470 struct page
*end_page
= page
+ nr_pages
;
1472 /* Check the starting page of each pageblock within the range */
1473 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1474 if (!is_pageblock_removable_nolock(page
))
1479 /* All pageblocks in the memory block are likely to be hot-removable */
1484 * Confirm all pages in a range [start, end) belong to the same zone.
1485 * When true, return its valid [start, end).
1487 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
,
1488 unsigned long *valid_start
, unsigned long *valid_end
)
1490 unsigned long pfn
, sec_end_pfn
;
1491 unsigned long start
, end
;
1492 struct zone
*zone
= NULL
;
1495 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1497 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1498 /* Make sure the memory section is present first */
1499 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1501 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1502 pfn
+= MAX_ORDER_NR_PAGES
) {
1504 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1505 while ((i
< MAX_ORDER_NR_PAGES
) &&
1506 !pfn_valid_within(pfn
+ i
))
1508 if (i
== MAX_ORDER_NR_PAGES
|| pfn
+ i
>= end_pfn
)
1510 page
= pfn_to_page(pfn
+ i
);
1511 if (zone
&& page_zone(page
) != zone
)
1515 zone
= page_zone(page
);
1516 end
= pfn
+ MAX_ORDER_NR_PAGES
;
1521 *valid_start
= start
;
1522 *valid_end
= min(end
, end_pfn
);
1530 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1531 * non-lru movable pages and hugepages). We scan pfn because it's much
1532 * easier than scanning over linked list. This function returns the pfn
1533 * of the first found movable page if it's found, otherwise 0.
1535 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1539 for (pfn
= start
; pfn
< end
; pfn
++) {
1540 if (pfn_valid(pfn
)) {
1541 page
= pfn_to_page(pfn
);
1544 if (__PageMovable(page
))
1546 if (PageHuge(page
)) {
1547 if (page_huge_active(page
))
1550 pfn
= round_up(pfn
+ 1,
1551 1 << compound_order(page
)) - 1;
1558 static struct page
*new_node_page(struct page
*page
, unsigned long private,
1561 gfp_t gfp_mask
= GFP_USER
| __GFP_MOVABLE
;
1562 int nid
= page_to_nid(page
);
1563 nodemask_t nmask
= node_states
[N_MEMORY
];
1564 struct page
*new_page
= NULL
;
1567 * TODO: allocate a destination hugepage from a nearest neighbor node,
1568 * accordance with memory policy of the user process if possible. For
1569 * now as a simple work-around, we use the next node for destination.
1572 return alloc_huge_page_node(page_hstate(compound_head(page
)),
1573 next_node_in(nid
, nmask
));
1575 node_clear(nid
, nmask
);
1577 if (PageHighMem(page
)
1578 || (zone_idx(page_zone(page
)) == ZONE_MOVABLE
))
1579 gfp_mask
|= __GFP_HIGHMEM
;
1581 if (!nodes_empty(nmask
))
1582 new_page
= __alloc_pages_nodemask(gfp_mask
, 0,
1583 node_zonelist(nid
, gfp_mask
), &nmask
);
1585 new_page
= __alloc_pages(gfp_mask
, 0,
1586 node_zonelist(nid
, gfp_mask
));
1591 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1593 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1597 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1598 int not_managed
= 0;
1602 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1603 if (!pfn_valid(pfn
))
1605 page
= pfn_to_page(pfn
);
1607 if (PageHuge(page
)) {
1608 struct page
*head
= compound_head(page
);
1609 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1610 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1614 if (isolate_huge_page(page
, &source
))
1615 move_pages
-= 1 << compound_order(head
);
1619 if (!get_page_unless_zero(page
))
1622 * We can skip free pages. And we can deal with pages on
1623 * LRU and non-lru movable pages.
1626 ret
= isolate_lru_page(page
);
1628 ret
= isolate_movable_page(page
, ISOLATE_UNEVICTABLE
);
1629 if (!ret
) { /* Success */
1631 list_add_tail(&page
->lru
, &source
);
1633 if (!__PageMovable(page
))
1634 inc_node_page_state(page
, NR_ISOLATED_ANON
+
1635 page_is_file_cache(page
));
1638 #ifdef CONFIG_DEBUG_VM
1639 pr_alert("failed to isolate pfn %lx\n", pfn
);
1640 dump_page(page
, "isolation failed");
1643 /* Because we don't have big zone->lock. we should
1644 check this again here. */
1645 if (page_count(page
)) {
1652 if (!list_empty(&source
)) {
1654 putback_movable_pages(&source
);
1658 /* Allocate a new page from the nearest neighbor node */
1659 ret
= migrate_pages(&source
, new_node_page
, NULL
, 0,
1660 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1662 putback_movable_pages(&source
);
1669 * remove from free_area[] and mark all as Reserved.
1672 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1675 __offline_isolated_pages(start
, start
+ nr_pages
);
1680 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1682 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1683 offline_isolated_pages_cb
);
1687 * Check all pages in range, recoreded as memory resource, are isolated.
1690 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1694 long offlined
= *(long *)data
;
1695 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1696 offlined
= nr_pages
;
1698 *(long *)data
+= offlined
;
1703 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1708 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1709 check_pages_isolated_cb
);
1711 offlined
= (long)ret
;
1715 #ifdef CONFIG_MOVABLE_NODE
1717 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1720 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1724 #else /* CONFIG_MOVABLE_NODE */
1725 /* ensure the node has NORMAL memory if it is still online */
1726 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1728 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1729 unsigned long present_pages
= 0;
1732 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1733 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1735 if (present_pages
> nr_pages
)
1739 for (; zt
<= ZONE_MOVABLE
; zt
++)
1740 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1743 * we can't offline the last normal memory until all
1744 * higher memory is offlined.
1746 return present_pages
== 0;
1748 #endif /* CONFIG_MOVABLE_NODE */
1750 static int __init
cmdline_parse_movable_node(char *p
)
1752 #ifdef CONFIG_MOVABLE_NODE
1753 movable_node_enabled
= true;
1755 pr_warn("movable_node option not supported\n");
1759 early_param("movable_node", cmdline_parse_movable_node
);
1761 /* check which state of node_states will be changed when offline memory */
1762 static void node_states_check_changes_offline(unsigned long nr_pages
,
1763 struct zone
*zone
, struct memory_notify
*arg
)
1765 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1766 unsigned long present_pages
= 0;
1767 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1770 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1771 * contains nodes which have zones of 0...ZONE_NORMAL,
1772 * set zone_last to ZONE_NORMAL.
1774 * If we don't have HIGHMEM nor movable node,
1775 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1776 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1778 if (N_MEMORY
== N_NORMAL_MEMORY
)
1779 zone_last
= ZONE_MOVABLE
;
1782 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1783 * If the memory to be offline is in a zone of 0...zone_last,
1784 * and it is the last present memory, 0...zone_last will
1785 * become empty after offline , thus we can determind we will
1786 * need to clear the node from node_states[N_NORMAL_MEMORY].
1788 for (zt
= 0; zt
<= zone_last
; zt
++)
1789 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1790 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1791 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1793 arg
->status_change_nid_normal
= -1;
1795 #ifdef CONFIG_HIGHMEM
1797 * If we have movable node, node_states[N_HIGH_MEMORY]
1798 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1799 * set zone_last to ZONE_HIGHMEM.
1801 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1802 * contains nodes which have zones of 0...ZONE_MOVABLE,
1803 * set zone_last to ZONE_MOVABLE.
1805 zone_last
= ZONE_HIGHMEM
;
1806 if (N_MEMORY
== N_HIGH_MEMORY
)
1807 zone_last
= ZONE_MOVABLE
;
1809 for (; zt
<= zone_last
; zt
++)
1810 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1811 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1812 arg
->status_change_nid_high
= zone_to_nid(zone
);
1814 arg
->status_change_nid_high
= -1;
1816 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1820 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1822 zone_last
= ZONE_MOVABLE
;
1825 * check whether node_states[N_HIGH_MEMORY] will be changed
1826 * If we try to offline the last present @nr_pages from the node,
1827 * we can determind we will need to clear the node from
1828 * node_states[N_HIGH_MEMORY].
1830 for (; zt
<= zone_last
; zt
++)
1831 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1832 if (nr_pages
>= present_pages
)
1833 arg
->status_change_nid
= zone_to_nid(zone
);
1835 arg
->status_change_nid
= -1;
1838 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1840 if (arg
->status_change_nid_normal
>= 0)
1841 node_clear_state(node
, N_NORMAL_MEMORY
);
1843 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1844 (arg
->status_change_nid_high
>= 0))
1845 node_clear_state(node
, N_HIGH_MEMORY
);
1847 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1848 (arg
->status_change_nid
>= 0))
1849 node_clear_state(node
, N_MEMORY
);
1852 static int __ref
__offline_pages(unsigned long start_pfn
,
1853 unsigned long end_pfn
, unsigned long timeout
)
1855 unsigned long pfn
, nr_pages
, expire
;
1856 long offlined_pages
;
1857 int ret
, drain
, retry_max
, node
;
1858 unsigned long flags
;
1859 unsigned long valid_start
, valid_end
;
1861 struct memory_notify arg
;
1863 /* at least, alignment against pageblock is necessary */
1864 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1866 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1868 /* This makes hotplug much easier...and readable.
1869 we assume this for now. .*/
1870 if (!test_pages_in_a_zone(start_pfn
, end_pfn
, &valid_start
, &valid_end
))
1873 zone
= page_zone(pfn_to_page(valid_start
));
1874 node
= zone_to_nid(zone
);
1875 nr_pages
= end_pfn
- start_pfn
;
1877 if (zone_idx(zone
) <= ZONE_NORMAL
&& !can_offline_normal(zone
, nr_pages
))
1880 /* set above range as isolated */
1881 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1882 MIGRATE_MOVABLE
, true);
1886 arg
.start_pfn
= start_pfn
;
1887 arg
.nr_pages
= nr_pages
;
1888 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1890 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1891 ret
= notifier_to_errno(ret
);
1893 goto failed_removal
;
1896 expire
= jiffies
+ timeout
;
1900 /* start memory hot removal */
1902 if (time_after(jiffies
, expire
))
1903 goto failed_removal
;
1905 if (signal_pending(current
))
1906 goto failed_removal
;
1909 lru_add_drain_all();
1911 drain_all_pages(zone
);
1914 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1915 if (pfn
) { /* We have movable pages */
1916 ret
= do_migrate_range(pfn
, end_pfn
);
1922 if (--retry_max
== 0)
1923 goto failed_removal
;
1929 /* drain all zone's lru pagevec, this is asynchronous... */
1930 lru_add_drain_all();
1932 /* drain pcp pages, this is synchronous. */
1933 drain_all_pages(zone
);
1935 * dissolve free hugepages in the memory block before doing offlining
1936 * actually in order to make hugetlbfs's object counting consistent.
1938 ret
= dissolve_free_huge_pages(start_pfn
, end_pfn
);
1940 goto failed_removal
;
1942 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1943 if (offlined_pages
< 0) {
1945 goto failed_removal
;
1947 pr_info("Offlined Pages %ld\n", offlined_pages
);
1948 /* Ok, all of our target is isolated.
1949 We cannot do rollback at this point. */
1950 offline_isolated_pages(start_pfn
, end_pfn
);
1951 /* reset pagetype flags and makes migrate type to be MOVABLE */
1952 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1953 /* removal success */
1954 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1955 zone
->present_pages
-= offlined_pages
;
1957 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1958 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1959 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1961 init_per_zone_wmark_min();
1963 if (!populated_zone(zone
)) {
1964 zone_pcp_reset(zone
);
1965 mutex_lock(&zonelists_mutex
);
1966 build_all_zonelists(NULL
, NULL
);
1967 mutex_unlock(&zonelists_mutex
);
1969 zone_pcp_update(zone
);
1971 node_states_clear_node(node
, &arg
);
1972 if (arg
.status_change_nid
>= 0) {
1974 kcompactd_stop(node
);
1977 vm_total_pages
= nr_free_pagecache_pages();
1978 writeback_set_ratelimit();
1980 memory_notify(MEM_OFFLINE
, &arg
);
1984 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1985 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1986 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1987 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1988 /* pushback to free area */
1989 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1993 /* Must be protected by mem_hotplug_begin() */
1994 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1996 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
, 120 * HZ
);
1998 #endif /* CONFIG_MEMORY_HOTREMOVE */
2001 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
2002 * @start_pfn: start pfn of the memory range
2003 * @end_pfn: end pfn of the memory range
2004 * @arg: argument passed to func
2005 * @func: callback for each memory section walked
2007 * This function walks through all present mem sections in range
2008 * [start_pfn, end_pfn) and call func on each mem section.
2010 * Returns the return value of func.
2012 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
2013 void *arg
, int (*func
)(struct memory_block
*, void *))
2015 struct memory_block
*mem
= NULL
;
2016 struct mem_section
*section
;
2017 unsigned long pfn
, section_nr
;
2020 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2021 section_nr
= pfn_to_section_nr(pfn
);
2022 if (!present_section_nr(section_nr
))
2025 section
= __nr_to_section(section_nr
);
2026 /* same memblock? */
2028 if ((section_nr
>= mem
->start_section_nr
) &&
2029 (section_nr
<= mem
->end_section_nr
))
2032 mem
= find_memory_block_hinted(section
, mem
);
2036 ret
= func(mem
, arg
);
2038 kobject_put(&mem
->dev
.kobj
);
2044 kobject_put(&mem
->dev
.kobj
);
2049 #ifdef CONFIG_MEMORY_HOTREMOVE
2050 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
2052 int ret
= !is_memblock_offlined(mem
);
2054 if (unlikely(ret
)) {
2055 phys_addr_t beginpa
, endpa
;
2057 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
2058 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
2059 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
2066 static int check_cpu_on_node(pg_data_t
*pgdat
)
2070 for_each_present_cpu(cpu
) {
2071 if (cpu_to_node(cpu
) == pgdat
->node_id
)
2073 * the cpu on this node isn't removed, and we can't
2074 * offline this node.
2082 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
2084 #ifdef CONFIG_ACPI_NUMA
2087 for_each_possible_cpu(cpu
)
2088 if (cpu_to_node(cpu
) == pgdat
->node_id
)
2089 numa_clear_node(cpu
);
2093 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
2097 ret
= check_cpu_on_node(pgdat
);
2102 * the node will be offlined when we come here, so we can clear
2103 * the cpu_to_node() now.
2106 unmap_cpu_on_node(pgdat
);
2113 * Offline a node if all memory sections and cpus of the node are removed.
2115 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2116 * and online/offline operations before this call.
2118 void try_offline_node(int nid
)
2120 pg_data_t
*pgdat
= NODE_DATA(nid
);
2121 unsigned long start_pfn
= pgdat
->node_start_pfn
;
2122 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
2125 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2126 unsigned long section_nr
= pfn_to_section_nr(pfn
);
2128 if (!present_section_nr(section_nr
))
2131 if (pfn_to_nid(pfn
) != nid
)
2135 * some memory sections of this node are not removed, and we
2136 * can't offline node now.
2141 if (check_and_unmap_cpu_on_node(pgdat
))
2145 * all memory/cpu of this node are removed, we can offline this
2148 node_set_offline(nid
);
2149 unregister_one_node(nid
);
2151 EXPORT_SYMBOL(try_offline_node
);
2156 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2157 * and online/offline operations before this call, as required by
2158 * try_offline_node().
2160 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2164 BUG_ON(check_hotplug_memory_range(start
, size
));
2166 mem_hotplug_begin();
2169 * All memory blocks must be offlined before removing memory. Check
2170 * whether all memory blocks in question are offline and trigger a BUG()
2171 * if this is not the case.
2173 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2174 check_memblock_offlined_cb
);
2178 /* remove memmap entry */
2179 firmware_map_remove(start
, start
+ size
, "System RAM");
2180 memblock_free(start
, size
);
2181 memblock_remove(start
, size
);
2183 arch_remove_memory(start
, size
);
2185 try_offline_node(nid
);
2189 EXPORT_SYMBOL_GPL(remove_memory
);
2190 #endif /* CONFIG_MEMORY_HOTREMOVE */