2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/swap.h>
10 #include <linux/interrupt.h>
11 #include <linux/pagemap.h>
12 #include <linux/compiler.h>
13 #include <linux/export.h>
14 #include <linux/pagevec.h>
15 #include <linux/writeback.h>
16 #include <linux/slab.h>
17 #include <linux/sysctl.h>
18 #include <linux/cpu.h>
19 #include <linux/memory.h>
20 #include <linux/memremap.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/highmem.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
32 #include <linux/stop_machine.h>
33 #include <linux/hugetlb.h>
34 #include <linux/memblock.h>
35 #include <linux/bootmem.h>
36 #include <linux/compaction.h>
38 #include <asm/tlbflush.h>
43 * online_page_callback contains pointer to current page onlining function.
44 * Initially it is generic_online_page(). If it is required it could be
45 * changed by calling set_online_page_callback() for callback registration
46 * and restore_online_page_callback() for generic callback restore.
49 static void generic_online_page(struct page
*page
);
51 static online_page_callback_t online_page_callback
= generic_online_page
;
52 static DEFINE_MUTEX(online_page_callback_lock
);
54 /* The same as the cpu_hotplug lock, but for memory hotplug. */
56 struct task_struct
*active_writer
;
57 struct mutex lock
; /* Synchronizes accesses to refcount, */
59 * Also blocks the new readers during
60 * an ongoing mem hotplug operation.
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map
;
68 .active_writer
= NULL
,
69 .lock
= __MUTEX_INITIALIZER(mem_hotplug
.lock
),
71 #ifdef CONFIG_DEBUG_LOCK_ALLOC
72 .dep_map
= {.name
= "mem_hotplug.lock" },
76 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
77 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
78 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
79 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
81 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
82 bool memhp_auto_online
;
84 bool memhp_auto_online
= true;
86 EXPORT_SYMBOL_GPL(memhp_auto_online
);
88 void get_online_mems(void)
91 if (mem_hotplug
.active_writer
== current
)
93 memhp_lock_acquire_read();
94 mutex_lock(&mem_hotplug
.lock
);
95 mem_hotplug
.refcount
++;
96 mutex_unlock(&mem_hotplug
.lock
);
100 void put_online_mems(void)
102 if (mem_hotplug
.active_writer
== current
)
104 mutex_lock(&mem_hotplug
.lock
);
106 if (WARN_ON(!mem_hotplug
.refcount
))
107 mem_hotplug
.refcount
++; /* try to fix things up */
109 if (!--mem_hotplug
.refcount
&& unlikely(mem_hotplug
.active_writer
))
110 wake_up_process(mem_hotplug
.active_writer
);
111 mutex_unlock(&mem_hotplug
.lock
);
112 memhp_lock_release();
116 void mem_hotplug_begin(void)
118 mem_hotplug
.active_writer
= current
;
120 memhp_lock_acquire();
122 mutex_lock(&mem_hotplug
.lock
);
123 if (likely(!mem_hotplug
.refcount
))
125 __set_current_state(TASK_UNINTERRUPTIBLE
);
126 mutex_unlock(&mem_hotplug
.lock
);
131 void mem_hotplug_done(void)
133 mem_hotplug
.active_writer
= NULL
;
134 mutex_unlock(&mem_hotplug
.lock
);
135 memhp_lock_release();
138 /* add this memory to iomem resource */
139 static struct resource
*register_memory_resource(u64 start
, u64 size
)
141 struct resource
*res
;
142 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
144 return ERR_PTR(-ENOMEM
);
146 res
->name
= "System RAM";
148 res
->end
= start
+ size
- 1;
149 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
150 if (request_resource(&iomem_resource
, res
) < 0) {
151 pr_debug("System RAM resource %pR cannot be added\n", res
);
153 return ERR_PTR(-EEXIST
);
158 static void release_memory_resource(struct resource
*res
)
162 release_resource(res
);
167 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
168 void get_page_bootmem(unsigned long info
, struct page
*page
,
171 page
->lru
.next
= (struct list_head
*) type
;
172 SetPagePrivate(page
);
173 set_page_private(page
, info
);
177 void put_page_bootmem(struct page
*page
)
181 type
= (unsigned long) page
->lru
.next
;
182 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
183 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
185 if (page_ref_dec_return(page
) == 1) {
186 ClearPagePrivate(page
);
187 set_page_private(page
, 0);
188 INIT_LIST_HEAD(&page
->lru
);
189 free_reserved_page(page
);
193 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
194 #ifndef CONFIG_SPARSEMEM_VMEMMAP
195 static void register_page_bootmem_info_section(unsigned long start_pfn
)
197 unsigned long *usemap
, mapsize
, section_nr
, i
;
198 struct mem_section
*ms
;
199 struct page
*page
, *memmap
;
201 section_nr
= pfn_to_section_nr(start_pfn
);
202 ms
= __nr_to_section(section_nr
);
204 /* Get section's memmap address */
205 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
208 * Get page for the memmap's phys address
209 * XXX: need more consideration for sparse_vmemmap...
211 page
= virt_to_page(memmap
);
212 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
213 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
215 /* remember memmap's page */
216 for (i
= 0; i
< mapsize
; i
++, page
++)
217 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
219 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
220 page
= virt_to_page(usemap
);
222 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
224 for (i
= 0; i
< mapsize
; i
++, page
++)
225 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
228 #else /* CONFIG_SPARSEMEM_VMEMMAP */
229 static void register_page_bootmem_info_section(unsigned long start_pfn
)
231 unsigned long *usemap
, mapsize
, section_nr
, i
;
232 struct mem_section
*ms
;
233 struct page
*page
, *memmap
;
235 if (!pfn_valid(start_pfn
))
238 section_nr
= pfn_to_section_nr(start_pfn
);
239 ms
= __nr_to_section(section_nr
);
241 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
243 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
245 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
246 page
= virt_to_page(usemap
);
248 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
250 for (i
= 0; i
< mapsize
; i
++, page
++)
251 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
253 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
255 void register_page_bootmem_info_node(struct pglist_data
*pgdat
)
257 unsigned long i
, pfn
, end_pfn
, nr_pages
;
258 int node
= pgdat
->node_id
;
262 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
263 page
= virt_to_page(pgdat
);
265 for (i
= 0; i
< nr_pages
; i
++, page
++)
266 get_page_bootmem(node
, page
, NODE_INFO
);
268 zone
= &pgdat
->node_zones
[0];
269 for (; zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1; zone
++) {
270 if (zone_is_initialized(zone
)) {
271 nr_pages
= zone
->wait_table_hash_nr_entries
272 * sizeof(wait_queue_head_t
);
273 nr_pages
= PAGE_ALIGN(nr_pages
) >> PAGE_SHIFT
;
274 page
= virt_to_page(zone
->wait_table
);
276 for (i
= 0; i
< nr_pages
; i
++, page
++)
277 get_page_bootmem(node
, page
, NODE_INFO
);
281 pfn
= pgdat
->node_start_pfn
;
282 end_pfn
= pgdat_end_pfn(pgdat
);
284 /* register section info */
285 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
287 * Some platforms can assign the same pfn to multiple nodes - on
288 * node0 as well as nodeN. To avoid registering a pfn against
289 * multiple nodes we check that this pfn does not already
290 * reside in some other nodes.
292 if (pfn_valid(pfn
) && (pfn_to_nid(pfn
) == node
))
293 register_page_bootmem_info_section(pfn
);
296 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
298 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
299 unsigned long end_pfn
)
301 unsigned long old_zone_end_pfn
;
303 zone_span_writelock(zone
);
305 old_zone_end_pfn
= zone_end_pfn(zone
);
306 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
307 zone
->zone_start_pfn
= start_pfn
;
309 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
310 zone
->zone_start_pfn
;
312 zone_span_writeunlock(zone
);
315 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
316 unsigned long end_pfn
)
318 zone_span_writelock(zone
);
320 if (end_pfn
- start_pfn
) {
321 zone
->zone_start_pfn
= start_pfn
;
322 zone
->spanned_pages
= end_pfn
- start_pfn
;
325 * make it consist as free_area_init_core(),
326 * if spanned_pages = 0, then keep start_pfn = 0
328 zone
->zone_start_pfn
= 0;
329 zone
->spanned_pages
= 0;
332 zone_span_writeunlock(zone
);
335 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
336 unsigned long end_pfn
)
338 enum zone_type zid
= zone_idx(zone
);
339 int nid
= zone
->zone_pgdat
->node_id
;
342 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
343 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
346 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
347 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
348 static int __ref
ensure_zone_is_initialized(struct zone
*zone
,
349 unsigned long start_pfn
, unsigned long num_pages
)
351 if (!zone_is_initialized(zone
))
352 return init_currently_empty_zone(zone
, start_pfn
, num_pages
);
357 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
358 unsigned long start_pfn
, unsigned long end_pfn
)
362 unsigned long z1_start_pfn
;
364 ret
= ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
368 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
370 /* can't move pfns which are higher than @z2 */
371 if (end_pfn
> zone_end_pfn(z2
))
373 /* the move out part must be at the left most of @z2 */
374 if (start_pfn
> z2
->zone_start_pfn
)
376 /* must included/overlap */
377 if (end_pfn
<= z2
->zone_start_pfn
)
380 /* use start_pfn for z1's start_pfn if z1 is empty */
381 if (!zone_is_empty(z1
))
382 z1_start_pfn
= z1
->zone_start_pfn
;
384 z1_start_pfn
= start_pfn
;
386 resize_zone(z1
, z1_start_pfn
, end_pfn
);
387 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
389 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
391 fix_zone_id(z1
, start_pfn
, end_pfn
);
395 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
399 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
400 unsigned long start_pfn
, unsigned long end_pfn
)
404 unsigned long z2_end_pfn
;
406 ret
= ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
410 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
412 /* can't move pfns which are lower than @z1 */
413 if (z1
->zone_start_pfn
> start_pfn
)
415 /* the move out part mast at the right most of @z1 */
416 if (zone_end_pfn(z1
) > end_pfn
)
418 /* must included/overlap */
419 if (start_pfn
>= zone_end_pfn(z1
))
422 /* use end_pfn for z2's end_pfn if z2 is empty */
423 if (!zone_is_empty(z2
))
424 z2_end_pfn
= zone_end_pfn(z2
);
426 z2_end_pfn
= end_pfn
;
428 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
429 resize_zone(z2
, start_pfn
, z2_end_pfn
);
431 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
433 fix_zone_id(z2
, start_pfn
, end_pfn
);
437 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
441 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
442 unsigned long end_pfn
)
444 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
446 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
447 pgdat
->node_start_pfn
= start_pfn
;
449 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
450 pgdat
->node_start_pfn
;
453 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
455 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
456 int nr_pages
= PAGES_PER_SECTION
;
457 int nid
= pgdat
->node_id
;
459 unsigned long flags
, pfn
;
462 zone_type
= zone
- pgdat
->node_zones
;
463 ret
= ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
467 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
468 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
469 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
470 phys_start_pfn
+ nr_pages
);
471 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
472 memmap_init_zone(nr_pages
, nid
, zone_type
,
473 phys_start_pfn
, MEMMAP_HOTPLUG
);
475 /* online_page_range is called later and expects pages reserved */
476 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
480 SetPageReserved(pfn_to_page(pfn
));
485 static int __meminit
__add_section(int nid
, struct zone
*zone
,
486 unsigned long phys_start_pfn
)
490 if (pfn_valid(phys_start_pfn
))
493 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
498 ret
= __add_zone(zone
, phys_start_pfn
);
503 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
507 * Reasonably generic function for adding memory. It is
508 * expected that archs that support memory hotplug will
509 * call this function after deciding the zone to which to
512 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
513 unsigned long nr_pages
)
517 int start_sec
, end_sec
;
518 struct vmem_altmap
*altmap
;
520 clear_zone_contiguous(zone
);
522 /* during initialize mem_map, align hot-added range to section */
523 start_sec
= pfn_to_section_nr(phys_start_pfn
);
524 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
526 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
529 * Validate altmap is within bounds of the total request
531 if (altmap
->base_pfn
!= phys_start_pfn
532 || vmem_altmap_offset(altmap
) > nr_pages
) {
533 pr_warn_once("memory add fail, invalid altmap\n");
540 for (i
= start_sec
; i
<= end_sec
; i
++) {
541 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
));
544 * EEXIST is finally dealt with by ioresource collision
545 * check. see add_memory() => register_memory_resource()
546 * Warning will be printed if there is collision.
548 if (err
&& (err
!= -EEXIST
))
552 vmemmap_populate_print_last();
554 set_zone_contiguous(zone
);
557 EXPORT_SYMBOL_GPL(__add_pages
);
559 #ifdef CONFIG_MEMORY_HOTREMOVE
560 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
561 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
562 unsigned long start_pfn
,
563 unsigned long end_pfn
)
565 struct mem_section
*ms
;
567 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
568 ms
= __pfn_to_section(start_pfn
);
570 if (unlikely(!valid_section(ms
)))
573 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
576 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
585 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
586 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
587 unsigned long start_pfn
,
588 unsigned long end_pfn
)
590 struct mem_section
*ms
;
593 /* pfn is the end pfn of a memory section. */
595 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
596 ms
= __pfn_to_section(pfn
);
598 if (unlikely(!valid_section(ms
)))
601 if (unlikely(pfn_to_nid(pfn
) != nid
))
604 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
613 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
614 unsigned long end_pfn
)
616 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
617 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
618 unsigned long zone_end_pfn
= z
;
620 struct mem_section
*ms
;
621 int nid
= zone_to_nid(zone
);
623 zone_span_writelock(zone
);
624 if (zone_start_pfn
== start_pfn
) {
626 * If the section is smallest section in the zone, it need
627 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
628 * In this case, we find second smallest valid mem_section
629 * for shrinking zone.
631 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
634 zone
->zone_start_pfn
= pfn
;
635 zone
->spanned_pages
= zone_end_pfn
- pfn
;
637 } else if (zone_end_pfn
== end_pfn
) {
639 * If the section is biggest section in the zone, it need
640 * shrink zone->spanned_pages.
641 * In this case, we find second biggest valid mem_section for
644 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
647 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
651 * The section is not biggest or smallest mem_section in the zone, it
652 * only creates a hole in the zone. So in this case, we need not
653 * change the zone. But perhaps, the zone has only hole data. Thus
654 * it check the zone has only hole or not.
656 pfn
= zone_start_pfn
;
657 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
658 ms
= __pfn_to_section(pfn
);
660 if (unlikely(!valid_section(ms
)))
663 if (page_zone(pfn_to_page(pfn
)) != zone
)
666 /* If the section is current section, it continues the loop */
667 if (start_pfn
== pfn
)
670 /* If we find valid section, we have nothing to do */
671 zone_span_writeunlock(zone
);
675 /* The zone has no valid section */
676 zone
->zone_start_pfn
= 0;
677 zone
->spanned_pages
= 0;
678 zone_span_writeunlock(zone
);
681 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
682 unsigned long start_pfn
, unsigned long end_pfn
)
684 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
685 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
686 unsigned long pgdat_end_pfn
= p
;
688 struct mem_section
*ms
;
689 int nid
= pgdat
->node_id
;
691 if (pgdat_start_pfn
== start_pfn
) {
693 * If the section is smallest section in the pgdat, it need
694 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
695 * In this case, we find second smallest valid mem_section
696 * for shrinking zone.
698 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
701 pgdat
->node_start_pfn
= pfn
;
702 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
704 } else if (pgdat_end_pfn
== end_pfn
) {
706 * If the section is biggest section in the pgdat, it need
707 * shrink pgdat->node_spanned_pages.
708 * In this case, we find second biggest valid mem_section for
711 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
714 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
718 * If the section is not biggest or smallest mem_section in the pgdat,
719 * it only creates a hole in the pgdat. So in this case, we need not
721 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
722 * has only hole or not.
724 pfn
= pgdat_start_pfn
;
725 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
726 ms
= __pfn_to_section(pfn
);
728 if (unlikely(!valid_section(ms
)))
731 if (pfn_to_nid(pfn
) != nid
)
734 /* If the section is current section, it continues the loop */
735 if (start_pfn
== pfn
)
738 /* If we find valid section, we have nothing to do */
742 /* The pgdat has no valid section */
743 pgdat
->node_start_pfn
= 0;
744 pgdat
->node_spanned_pages
= 0;
747 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
749 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
750 int nr_pages
= PAGES_PER_SECTION
;
754 zone_type
= zone
- pgdat
->node_zones
;
756 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
757 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
758 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
759 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
762 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
763 unsigned long map_offset
)
765 unsigned long start_pfn
;
769 if (!valid_section(ms
))
772 ret
= unregister_memory_section(ms
);
776 scn_nr
= __section_nr(ms
);
777 start_pfn
= section_nr_to_pfn(scn_nr
);
778 __remove_zone(zone
, start_pfn
);
780 sparse_remove_one_section(zone
, ms
, map_offset
);
785 * __remove_pages() - remove sections of pages from a zone
786 * @zone: zone from which pages need to be removed
787 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
788 * @nr_pages: number of pages to remove (must be multiple of section size)
790 * Generic helper function to remove section mappings and sysfs entries
791 * for the section of the memory we are removing. Caller needs to make
792 * sure that pages are marked reserved and zones are adjust properly by
793 * calling offline_pages().
795 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
796 unsigned long nr_pages
)
799 unsigned long map_offset
= 0;
800 int sections_to_remove
, ret
= 0;
802 /* In the ZONE_DEVICE case device driver owns the memory region */
803 if (is_dev_zone(zone
)) {
804 struct page
*page
= pfn_to_page(phys_start_pfn
);
805 struct vmem_altmap
*altmap
;
807 altmap
= to_vmem_altmap((unsigned long) page
);
809 map_offset
= vmem_altmap_offset(altmap
);
811 resource_size_t start
, size
;
813 start
= phys_start_pfn
<< PAGE_SHIFT
;
814 size
= nr_pages
* PAGE_SIZE
;
816 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
819 resource_size_t endres
= start
+ size
- 1;
821 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
822 &start
, &endres
, ret
);
826 clear_zone_contiguous(zone
);
829 * We can only remove entire sections
831 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
832 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
834 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
835 for (i
= 0; i
< sections_to_remove
; i
++) {
836 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
838 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
844 set_zone_contiguous(zone
);
848 EXPORT_SYMBOL_GPL(__remove_pages
);
849 #endif /* CONFIG_MEMORY_HOTREMOVE */
851 int set_online_page_callback(online_page_callback_t callback
)
856 mutex_lock(&online_page_callback_lock
);
858 if (online_page_callback
== generic_online_page
) {
859 online_page_callback
= callback
;
863 mutex_unlock(&online_page_callback_lock
);
868 EXPORT_SYMBOL_GPL(set_online_page_callback
);
870 int restore_online_page_callback(online_page_callback_t callback
)
875 mutex_lock(&online_page_callback_lock
);
877 if (online_page_callback
== callback
) {
878 online_page_callback
= generic_online_page
;
882 mutex_unlock(&online_page_callback_lock
);
887 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
889 void __online_page_set_limits(struct page
*page
)
892 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
894 void __online_page_increment_counters(struct page
*page
)
896 adjust_managed_page_count(page
, 1);
898 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
900 void __online_page_free(struct page
*page
)
902 __free_reserved_page(page
);
904 EXPORT_SYMBOL_GPL(__online_page_free
);
906 static void generic_online_page(struct page
*page
)
908 __online_page_set_limits(page
);
909 __online_page_increment_counters(page
);
910 __online_page_free(page
);
913 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
917 unsigned long onlined_pages
= *(unsigned long *)arg
;
919 if (PageReserved(pfn_to_page(start_pfn
)))
920 for (i
= 0; i
< nr_pages
; i
++) {
921 page
= pfn_to_page(start_pfn
+ i
);
922 (*online_page_callback
)(page
);
925 *(unsigned long *)arg
= onlined_pages
;
929 #ifdef CONFIG_MOVABLE_NODE
931 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
934 static bool can_online_high_movable(struct zone
*zone
)
938 #else /* CONFIG_MOVABLE_NODE */
939 /* ensure every online node has NORMAL memory */
940 static bool can_online_high_movable(struct zone
*zone
)
942 return node_state(zone_to_nid(zone
), N_NORMAL_MEMORY
);
944 #endif /* CONFIG_MOVABLE_NODE */
946 /* check which state of node_states will be changed when online memory */
947 static void node_states_check_changes_online(unsigned long nr_pages
,
948 struct zone
*zone
, struct memory_notify
*arg
)
950 int nid
= zone_to_nid(zone
);
951 enum zone_type zone_last
= ZONE_NORMAL
;
954 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
955 * contains nodes which have zones of 0...ZONE_NORMAL,
956 * set zone_last to ZONE_NORMAL.
958 * If we don't have HIGHMEM nor movable node,
959 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
960 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
962 if (N_MEMORY
== N_NORMAL_MEMORY
)
963 zone_last
= ZONE_MOVABLE
;
966 * if the memory to be online is in a zone of 0...zone_last, and
967 * the zones of 0...zone_last don't have memory before online, we will
968 * need to set the node to node_states[N_NORMAL_MEMORY] after
969 * the memory is online.
971 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
972 arg
->status_change_nid_normal
= nid
;
974 arg
->status_change_nid_normal
= -1;
976 #ifdef CONFIG_HIGHMEM
978 * If we have movable node, node_states[N_HIGH_MEMORY]
979 * contains nodes which have zones of 0...ZONE_HIGHMEM,
980 * set zone_last to ZONE_HIGHMEM.
982 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
983 * contains nodes which have zones of 0...ZONE_MOVABLE,
984 * set zone_last to ZONE_MOVABLE.
986 zone_last
= ZONE_HIGHMEM
;
987 if (N_MEMORY
== N_HIGH_MEMORY
)
988 zone_last
= ZONE_MOVABLE
;
990 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
991 arg
->status_change_nid_high
= nid
;
993 arg
->status_change_nid_high
= -1;
995 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
999 * if the node don't have memory befor online, we will need to
1000 * set the node to node_states[N_MEMORY] after the memory
1003 if (!node_state(nid
, N_MEMORY
))
1004 arg
->status_change_nid
= nid
;
1006 arg
->status_change_nid
= -1;
1009 static void node_states_set_node(int node
, struct memory_notify
*arg
)
1011 if (arg
->status_change_nid_normal
>= 0)
1012 node_set_state(node
, N_NORMAL_MEMORY
);
1014 if (arg
->status_change_nid_high
>= 0)
1015 node_set_state(node
, N_HIGH_MEMORY
);
1017 node_set_state(node
, N_MEMORY
);
1021 /* Must be protected by mem_hotplug_begin() */
1022 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
1024 unsigned long flags
;
1025 unsigned long onlined_pages
= 0;
1027 int need_zonelists_rebuild
= 0;
1030 struct memory_notify arg
;
1033 * This doesn't need a lock to do pfn_to_page().
1034 * The section can't be removed here because of the
1035 * memory_block->state_mutex.
1037 zone
= page_zone(pfn_to_page(pfn
));
1039 if ((zone_idx(zone
) > ZONE_NORMAL
||
1040 online_type
== MMOP_ONLINE_MOVABLE
) &&
1041 !can_online_high_movable(zone
))
1044 if (online_type
== MMOP_ONLINE_KERNEL
&&
1045 zone_idx(zone
) == ZONE_MOVABLE
) {
1046 if (move_pfn_range_left(zone
- 1, zone
, pfn
, pfn
+ nr_pages
))
1049 if (online_type
== MMOP_ONLINE_MOVABLE
&&
1050 zone_idx(zone
) == ZONE_MOVABLE
- 1) {
1051 if (move_pfn_range_right(zone
, zone
+ 1, pfn
, pfn
+ nr_pages
))
1055 /* Previous code may changed the zone of the pfn range */
1056 zone
= page_zone(pfn_to_page(pfn
));
1058 arg
.start_pfn
= pfn
;
1059 arg
.nr_pages
= nr_pages
;
1060 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1062 nid
= zone_to_nid(zone
);
1064 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1065 ret
= notifier_to_errno(ret
);
1067 goto failed_addition
;
1070 * If this zone is not populated, then it is not in zonelist.
1071 * This means the page allocator ignores this zone.
1072 * So, zonelist must be updated after online.
1074 mutex_lock(&zonelists_mutex
);
1075 if (!populated_zone(zone
)) {
1076 need_zonelists_rebuild
= 1;
1077 build_all_zonelists(NULL
, zone
);
1080 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1081 online_pages_range
);
1083 if (need_zonelists_rebuild
)
1084 zone_pcp_reset(zone
);
1085 mutex_unlock(&zonelists_mutex
);
1086 goto failed_addition
;
1089 zone
->present_pages
+= onlined_pages
;
1091 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1092 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1093 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1095 if (onlined_pages
) {
1096 node_states_set_node(nid
, &arg
);
1097 if (need_zonelists_rebuild
)
1098 build_all_zonelists(NULL
, NULL
);
1100 zone_pcp_update(zone
);
1103 mutex_unlock(&zonelists_mutex
);
1105 init_per_zone_wmark_min();
1107 if (onlined_pages
) {
1112 vm_total_pages
= nr_free_pagecache_pages();
1114 writeback_set_ratelimit();
1117 memory_notify(MEM_ONLINE
, &arg
);
1121 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1122 (unsigned long long) pfn
<< PAGE_SHIFT
,
1123 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
1124 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1127 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1129 static void reset_node_present_pages(pg_data_t
*pgdat
)
1133 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1134 z
->present_pages
= 0;
1136 pgdat
->node_present_pages
= 0;
1139 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1140 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1142 struct pglist_data
*pgdat
;
1143 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1144 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1145 unsigned long start_pfn
= PFN_DOWN(start
);
1147 pgdat
= NODE_DATA(nid
);
1149 pgdat
= arch_alloc_nodedata(nid
);
1153 arch_refresh_nodedata(nid
, pgdat
);
1155 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1156 pgdat
->nr_zones
= 0;
1157 pgdat
->classzone_idx
= 0;
1160 /* we can use NODE_DATA(nid) from here */
1162 /* init node's zones as empty zones, we don't have any present pages.*/
1163 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1166 * The node we allocated has no zone fallback lists. For avoiding
1167 * to access not-initialized zonelist, build here.
1169 mutex_lock(&zonelists_mutex
);
1170 build_all_zonelists(pgdat
, NULL
);
1171 mutex_unlock(&zonelists_mutex
);
1174 * zone->managed_pages is set to an approximate value in
1175 * free_area_init_core(), which will cause
1176 * /sys/device/system/node/nodeX/meminfo has wrong data.
1177 * So reset it to 0 before any memory is onlined.
1179 reset_node_managed_pages(pgdat
);
1182 * When memory is hot-added, all the memory is in offline state. So
1183 * clear all zones' present_pages because they will be updated in
1184 * online_pages() and offline_pages().
1186 reset_node_present_pages(pgdat
);
1191 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1193 arch_refresh_nodedata(nid
, NULL
);
1194 arch_free_nodedata(pgdat
);
1200 * try_online_node - online a node if offlined
1202 * called by cpu_up() to online a node without onlined memory.
1204 int try_online_node(int nid
)
1209 if (node_online(nid
))
1212 mem_hotplug_begin();
1213 pgdat
= hotadd_new_pgdat(nid
, 0);
1215 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1219 node_set_online(nid
);
1220 ret
= register_one_node(nid
);
1223 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1224 mutex_lock(&zonelists_mutex
);
1225 build_all_zonelists(NULL
, NULL
);
1226 mutex_unlock(&zonelists_mutex
);
1234 static int check_hotplug_memory_range(u64 start
, u64 size
)
1236 u64 start_pfn
= PFN_DOWN(start
);
1237 u64 nr_pages
= size
>> PAGE_SHIFT
;
1239 /* Memory range must be aligned with section */
1240 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1241 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1242 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1243 (unsigned long long)start
,
1244 (unsigned long long)size
);
1252 * If movable zone has already been setup, newly added memory should be check.
1253 * If its address is higher than movable zone, it should be added as movable.
1254 * Without this check, movable zone may overlap with other zone.
1256 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1258 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1259 pg_data_t
*pgdat
= NODE_DATA(nid
);
1260 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1262 if (zone_is_empty(movable_zone
))
1265 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1271 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
,
1274 #ifdef CONFIG_ZONE_DEVICE
1278 if (should_add_memory_movable(nid
, start
, size
))
1279 return ZONE_MOVABLE
;
1281 return zone_default
;
1284 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1286 return memory_block_change_state(mem
, MEM_ONLINE
, MEM_OFFLINE
);
1289 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1290 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1293 pg_data_t
*pgdat
= NULL
;
1299 size
= resource_size(res
);
1301 ret
= check_hotplug_memory_range(start
, size
);
1305 { /* Stupid hack to suppress address-never-null warning */
1306 void *p
= NODE_DATA(nid
);
1310 mem_hotplug_begin();
1313 * Add new range to memblock so that when hotadd_new_pgdat() is called
1314 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1315 * this new range and calculate total pages correctly. The range will
1316 * be removed at hot-remove time.
1318 memblock_add_node(start
, size
, nid
);
1320 new_node
= !node_online(nid
);
1322 pgdat
= hotadd_new_pgdat(nid
, start
);
1328 /* call arch's memory hotadd */
1329 ret
= arch_add_memory(nid
, start
, size
, false);
1334 /* we online node here. we can't roll back from here. */
1335 node_set_online(nid
);
1338 ret
= register_one_node(nid
);
1340 * If sysfs file of new node can't create, cpu on the node
1341 * can't be hot-added. There is no rollback way now.
1342 * So, check by BUG_ON() to catch it reluctantly..
1347 /* create new memmap entry */
1348 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1350 /* online pages if requested */
1352 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1353 NULL
, online_memory_block
);
1358 /* rollback pgdat allocation and others */
1360 rollback_node_hotadd(nid
, pgdat
);
1361 memblock_remove(start
, size
);
1367 EXPORT_SYMBOL_GPL(add_memory_resource
);
1369 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1371 struct resource
*res
;
1374 res
= register_memory_resource(start
, size
);
1376 return PTR_ERR(res
);
1378 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1380 release_memory_resource(res
);
1383 EXPORT_SYMBOL_GPL(add_memory
);
1385 #ifdef CONFIG_MEMORY_HOTREMOVE
1387 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1388 * set and the size of the free page is given by page_order(). Using this,
1389 * the function determines if the pageblock contains only free pages.
1390 * Due to buddy contraints, a free page at least the size of a pageblock will
1391 * be located at the start of the pageblock
1393 static inline int pageblock_free(struct page
*page
)
1395 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1398 /* Return the start of the next active pageblock after a given page */
1399 static struct page
*next_active_pageblock(struct page
*page
)
1401 /* Ensure the starting page is pageblock-aligned */
1402 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1404 /* If the entire pageblock is free, move to the end of free page */
1405 if (pageblock_free(page
)) {
1407 /* be careful. we don't have locks, page_order can be changed.*/
1408 order
= page_order(page
);
1409 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1410 return page
+ (1 << order
);
1413 return page
+ pageblock_nr_pages
;
1416 /* Checks if this range of memory is likely to be hot-removable. */
1417 bool is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1419 struct page
*page
= pfn_to_page(start_pfn
);
1420 struct page
*end_page
= page
+ nr_pages
;
1422 /* Check the starting page of each pageblock within the range */
1423 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1424 if (!is_pageblock_removable_nolock(page
))
1429 /* All pageblocks in the memory block are likely to be hot-removable */
1434 * Confirm all pages in a range [start, end) is belongs to the same zone.
1436 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
)
1438 unsigned long pfn
, sec_end_pfn
;
1439 struct zone
*zone
= NULL
;
1442 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
);
1444 pfn
= sec_end_pfn
+ 1, sec_end_pfn
+= PAGES_PER_SECTION
) {
1445 /* Make sure the memory section is present first */
1446 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1448 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1449 pfn
+= MAX_ORDER_NR_PAGES
) {
1451 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1452 while ((i
< MAX_ORDER_NR_PAGES
) &&
1453 !pfn_valid_within(pfn
+ i
))
1455 if (i
== MAX_ORDER_NR_PAGES
)
1457 page
= pfn_to_page(pfn
+ i
);
1458 if (zone
&& page_zone(page
) != zone
)
1460 zone
= page_zone(page
);
1467 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1468 * and hugepages). We scan pfn because it's much easier than scanning over
1469 * linked list. This function returns the pfn of the first found movable
1470 * page if it's found, otherwise 0.
1472 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1476 for (pfn
= start
; pfn
< end
; pfn
++) {
1477 if (pfn_valid(pfn
)) {
1478 page
= pfn_to_page(pfn
);
1481 if (PageHuge(page
)) {
1482 if (page_huge_active(page
))
1485 pfn
= round_up(pfn
+ 1,
1486 1 << compound_order(page
)) - 1;
1493 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1495 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1499 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1500 int not_managed
= 0;
1504 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1505 if (!pfn_valid(pfn
))
1507 page
= pfn_to_page(pfn
);
1509 if (PageHuge(page
)) {
1510 struct page
*head
= compound_head(page
);
1511 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1512 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1516 if (isolate_huge_page(page
, &source
))
1517 move_pages
-= 1 << compound_order(head
);
1521 if (!get_page_unless_zero(page
))
1524 * We can skip free pages. And we can only deal with pages on
1527 ret
= isolate_lru_page(page
);
1528 if (!ret
) { /* Success */
1530 list_add_tail(&page
->lru
, &source
);
1532 inc_zone_page_state(page
, NR_ISOLATED_ANON
+
1533 page_is_file_cache(page
));
1536 #ifdef CONFIG_DEBUG_VM
1537 pr_alert("removing pfn %lx from LRU failed\n", pfn
);
1538 dump_page(page
, "failed to remove from LRU");
1541 /* Because we don't have big zone->lock. we should
1542 check this again here. */
1543 if (page_count(page
)) {
1550 if (!list_empty(&source
)) {
1552 putback_movable_pages(&source
);
1557 * alloc_migrate_target should be improooooved!!
1558 * migrate_pages returns # of failed pages.
1560 ret
= migrate_pages(&source
, alloc_migrate_target
, NULL
, 0,
1561 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1563 putback_movable_pages(&source
);
1570 * remove from free_area[] and mark all as Reserved.
1573 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1576 __offline_isolated_pages(start
, start
+ nr_pages
);
1581 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1583 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1584 offline_isolated_pages_cb
);
1588 * Check all pages in range, recoreded as memory resource, are isolated.
1591 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1595 long offlined
= *(long *)data
;
1596 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1597 offlined
= nr_pages
;
1599 *(long *)data
+= offlined
;
1604 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1609 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1610 check_pages_isolated_cb
);
1612 offlined
= (long)ret
;
1616 #ifdef CONFIG_MOVABLE_NODE
1618 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1621 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1625 #else /* CONFIG_MOVABLE_NODE */
1626 /* ensure the node has NORMAL memory if it is still online */
1627 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1629 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1630 unsigned long present_pages
= 0;
1633 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1634 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1636 if (present_pages
> nr_pages
)
1640 for (; zt
<= ZONE_MOVABLE
; zt
++)
1641 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1644 * we can't offline the last normal memory until all
1645 * higher memory is offlined.
1647 return present_pages
== 0;
1649 #endif /* CONFIG_MOVABLE_NODE */
1651 static int __init
cmdline_parse_movable_node(char *p
)
1653 #ifdef CONFIG_MOVABLE_NODE
1655 * Memory used by the kernel cannot be hot-removed because Linux
1656 * cannot migrate the kernel pages. When memory hotplug is
1657 * enabled, we should prevent memblock from allocating memory
1660 * ACPI SRAT records all hotpluggable memory ranges. But before
1661 * SRAT is parsed, we don't know about it.
1663 * The kernel image is loaded into memory at very early time. We
1664 * cannot prevent this anyway. So on NUMA system, we set any
1665 * node the kernel resides in as un-hotpluggable.
1667 * Since on modern servers, one node could have double-digit
1668 * gigabytes memory, we can assume the memory around the kernel
1669 * image is also un-hotpluggable. So before SRAT is parsed, just
1670 * allocate memory near the kernel image to try the best to keep
1671 * the kernel away from hotpluggable memory.
1673 memblock_set_bottom_up(true);
1674 movable_node_enabled
= true;
1676 pr_warn("movable_node option not supported\n");
1680 early_param("movable_node", cmdline_parse_movable_node
);
1682 /* check which state of node_states will be changed when offline memory */
1683 static void node_states_check_changes_offline(unsigned long nr_pages
,
1684 struct zone
*zone
, struct memory_notify
*arg
)
1686 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1687 unsigned long present_pages
= 0;
1688 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1691 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1692 * contains nodes which have zones of 0...ZONE_NORMAL,
1693 * set zone_last to ZONE_NORMAL.
1695 * If we don't have HIGHMEM nor movable node,
1696 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1697 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1699 if (N_MEMORY
== N_NORMAL_MEMORY
)
1700 zone_last
= ZONE_MOVABLE
;
1703 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1704 * If the memory to be offline is in a zone of 0...zone_last,
1705 * and it is the last present memory, 0...zone_last will
1706 * become empty after offline , thus we can determind we will
1707 * need to clear the node from node_states[N_NORMAL_MEMORY].
1709 for (zt
= 0; zt
<= zone_last
; zt
++)
1710 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1711 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1712 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1714 arg
->status_change_nid_normal
= -1;
1716 #ifdef CONFIG_HIGHMEM
1718 * If we have movable node, node_states[N_HIGH_MEMORY]
1719 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1720 * set zone_last to ZONE_HIGHMEM.
1722 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1723 * contains nodes which have zones of 0...ZONE_MOVABLE,
1724 * set zone_last to ZONE_MOVABLE.
1726 zone_last
= ZONE_HIGHMEM
;
1727 if (N_MEMORY
== N_HIGH_MEMORY
)
1728 zone_last
= ZONE_MOVABLE
;
1730 for (; zt
<= zone_last
; zt
++)
1731 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1732 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1733 arg
->status_change_nid_high
= zone_to_nid(zone
);
1735 arg
->status_change_nid_high
= -1;
1737 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1741 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1743 zone_last
= ZONE_MOVABLE
;
1746 * check whether node_states[N_HIGH_MEMORY] will be changed
1747 * If we try to offline the last present @nr_pages from the node,
1748 * we can determind we will need to clear the node from
1749 * node_states[N_HIGH_MEMORY].
1751 for (; zt
<= zone_last
; zt
++)
1752 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1753 if (nr_pages
>= present_pages
)
1754 arg
->status_change_nid
= zone_to_nid(zone
);
1756 arg
->status_change_nid
= -1;
1759 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1761 if (arg
->status_change_nid_normal
>= 0)
1762 node_clear_state(node
, N_NORMAL_MEMORY
);
1764 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1765 (arg
->status_change_nid_high
>= 0))
1766 node_clear_state(node
, N_HIGH_MEMORY
);
1768 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1769 (arg
->status_change_nid
>= 0))
1770 node_clear_state(node
, N_MEMORY
);
1773 static int __ref
__offline_pages(unsigned long start_pfn
,
1774 unsigned long end_pfn
, unsigned long timeout
)
1776 unsigned long pfn
, nr_pages
, expire
;
1777 long offlined_pages
;
1778 int ret
, drain
, retry_max
, node
;
1779 unsigned long flags
;
1781 struct memory_notify arg
;
1783 /* at least, alignment against pageblock is necessary */
1784 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1786 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1788 /* This makes hotplug much easier...and readable.
1789 we assume this for now. .*/
1790 if (!test_pages_in_a_zone(start_pfn
, end_pfn
))
1793 zone
= page_zone(pfn_to_page(start_pfn
));
1794 node
= zone_to_nid(zone
);
1795 nr_pages
= end_pfn
- start_pfn
;
1797 if (zone_idx(zone
) <= ZONE_NORMAL
&& !can_offline_normal(zone
, nr_pages
))
1800 /* set above range as isolated */
1801 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1802 MIGRATE_MOVABLE
, true);
1806 arg
.start_pfn
= start_pfn
;
1807 arg
.nr_pages
= nr_pages
;
1808 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1810 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1811 ret
= notifier_to_errno(ret
);
1813 goto failed_removal
;
1816 expire
= jiffies
+ timeout
;
1820 /* start memory hot removal */
1822 if (time_after(jiffies
, expire
))
1823 goto failed_removal
;
1825 if (signal_pending(current
))
1826 goto failed_removal
;
1829 lru_add_drain_all();
1831 drain_all_pages(zone
);
1834 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1835 if (pfn
) { /* We have movable pages */
1836 ret
= do_migrate_range(pfn
, end_pfn
);
1842 if (--retry_max
== 0)
1843 goto failed_removal
;
1849 /* drain all zone's lru pagevec, this is asynchronous... */
1850 lru_add_drain_all();
1852 /* drain pcp pages, this is synchronous. */
1853 drain_all_pages(zone
);
1855 * dissolve free hugepages in the memory block before doing offlining
1856 * actually in order to make hugetlbfs's object counting consistent.
1858 dissolve_free_huge_pages(start_pfn
, end_pfn
);
1860 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1861 if (offlined_pages
< 0) {
1863 goto failed_removal
;
1865 pr_info("Offlined Pages %ld\n", offlined_pages
);
1866 /* Ok, all of our target is isolated.
1867 We cannot do rollback at this point. */
1868 offline_isolated_pages(start_pfn
, end_pfn
);
1869 /* reset pagetype flags and makes migrate type to be MOVABLE */
1870 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1871 /* removal success */
1872 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1873 zone
->present_pages
-= offlined_pages
;
1875 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1876 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1877 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1879 init_per_zone_wmark_min();
1881 if (!populated_zone(zone
)) {
1882 zone_pcp_reset(zone
);
1883 mutex_lock(&zonelists_mutex
);
1884 build_all_zonelists(NULL
, NULL
);
1885 mutex_unlock(&zonelists_mutex
);
1887 zone_pcp_update(zone
);
1889 node_states_clear_node(node
, &arg
);
1890 if (arg
.status_change_nid
>= 0) {
1892 kcompactd_stop(node
);
1895 vm_total_pages
= nr_free_pagecache_pages();
1896 writeback_set_ratelimit();
1898 memory_notify(MEM_OFFLINE
, &arg
);
1902 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1903 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1904 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1905 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1906 /* pushback to free area */
1907 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1911 /* Must be protected by mem_hotplug_begin() */
1912 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1914 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
, 120 * HZ
);
1916 #endif /* CONFIG_MEMORY_HOTREMOVE */
1919 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1920 * @start_pfn: start pfn of the memory range
1921 * @end_pfn: end pfn of the memory range
1922 * @arg: argument passed to func
1923 * @func: callback for each memory section walked
1925 * This function walks through all present mem sections in range
1926 * [start_pfn, end_pfn) and call func on each mem section.
1928 * Returns the return value of func.
1930 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1931 void *arg
, int (*func
)(struct memory_block
*, void *))
1933 struct memory_block
*mem
= NULL
;
1934 struct mem_section
*section
;
1935 unsigned long pfn
, section_nr
;
1938 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1939 section_nr
= pfn_to_section_nr(pfn
);
1940 if (!present_section_nr(section_nr
))
1943 section
= __nr_to_section(section_nr
);
1944 /* same memblock? */
1946 if ((section_nr
>= mem
->start_section_nr
) &&
1947 (section_nr
<= mem
->end_section_nr
))
1950 mem
= find_memory_block_hinted(section
, mem
);
1954 ret
= func(mem
, arg
);
1956 kobject_put(&mem
->dev
.kobj
);
1962 kobject_put(&mem
->dev
.kobj
);
1967 #ifdef CONFIG_MEMORY_HOTREMOVE
1968 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1970 int ret
= !is_memblock_offlined(mem
);
1972 if (unlikely(ret
)) {
1973 phys_addr_t beginpa
, endpa
;
1975 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1976 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1977 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1984 static int check_cpu_on_node(pg_data_t
*pgdat
)
1988 for_each_present_cpu(cpu
) {
1989 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1991 * the cpu on this node isn't removed, and we can't
1992 * offline this node.
2000 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
2002 #ifdef CONFIG_ACPI_NUMA
2005 for_each_possible_cpu(cpu
)
2006 if (cpu_to_node(cpu
) == pgdat
->node_id
)
2007 numa_clear_node(cpu
);
2011 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
2015 ret
= check_cpu_on_node(pgdat
);
2020 * the node will be offlined when we come here, so we can clear
2021 * the cpu_to_node() now.
2024 unmap_cpu_on_node(pgdat
);
2031 * Offline a node if all memory sections and cpus of the node are removed.
2033 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2034 * and online/offline operations before this call.
2036 void try_offline_node(int nid
)
2038 pg_data_t
*pgdat
= NODE_DATA(nid
);
2039 unsigned long start_pfn
= pgdat
->node_start_pfn
;
2040 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
2044 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2045 unsigned long section_nr
= pfn_to_section_nr(pfn
);
2047 if (!present_section_nr(section_nr
))
2050 if (pfn_to_nid(pfn
) != nid
)
2054 * some memory sections of this node are not removed, and we
2055 * can't offline node now.
2060 if (check_and_unmap_cpu_on_node(pgdat
))
2064 * all memory/cpu of this node are removed, we can offline this
2067 node_set_offline(nid
);
2068 unregister_one_node(nid
);
2070 /* free waittable in each zone */
2071 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
2072 struct zone
*zone
= pgdat
->node_zones
+ i
;
2075 * wait_table may be allocated from boot memory,
2076 * here only free if it's allocated by vmalloc.
2078 if (is_vmalloc_addr(zone
->wait_table
)) {
2079 vfree(zone
->wait_table
);
2080 zone
->wait_table
= NULL
;
2084 EXPORT_SYMBOL(try_offline_node
);
2089 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2090 * and online/offline operations before this call, as required by
2091 * try_offline_node().
2093 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2097 BUG_ON(check_hotplug_memory_range(start
, size
));
2099 mem_hotplug_begin();
2102 * All memory blocks must be offlined before removing memory. Check
2103 * whether all memory blocks in question are offline and trigger a BUG()
2104 * if this is not the case.
2106 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2107 check_memblock_offlined_cb
);
2111 /* remove memmap entry */
2112 firmware_map_remove(start
, start
+ size
, "System RAM");
2113 memblock_free(start
, size
);
2114 memblock_remove(start
, size
);
2116 arch_remove_memory(start
, size
);
2118 try_offline_node(nid
);
2122 EXPORT_SYMBOL_GPL(remove_memory
);
2123 #endif /* CONFIG_MEMORY_HOTREMOVE */