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/memory_hotplug.h>
21 #include <linux/highmem.h>
22 #include <linux/vmalloc.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
25 #include <linux/migrate.h>
26 #include <linux/page-isolation.h>
27 #include <linux/pfn.h>
28 #include <linux/suspend.h>
29 #include <linux/mm_inline.h>
30 #include <linux/firmware-map.h>
31 #include <linux/stop_machine.h>
32 #include <linux/hugetlb.h>
33 #include <linux/memblock.h>
34 #include <linux/bootmem.h>
36 #include <asm/tlbflush.h>
41 * online_page_callback contains pointer to current page onlining function.
42 * Initially it is generic_online_page(). If it is required it could be
43 * changed by calling set_online_page_callback() for callback registration
44 * and restore_online_page_callback() for generic callback restore.
47 static void generic_online_page(struct page
*page
);
49 static online_page_callback_t online_page_callback
= generic_online_page
;
50 static DEFINE_MUTEX(online_page_callback_lock
);
52 /* The same as the cpu_hotplug lock, but for memory hotplug. */
54 struct task_struct
*active_writer
;
55 struct mutex lock
; /* Synchronizes accesses to refcount, */
57 * Also blocks the new readers during
58 * an ongoing mem hotplug operation.
62 #ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map
;
66 .active_writer
= NULL
,
67 .lock
= __MUTEX_INITIALIZER(mem_hotplug
.lock
),
69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map
= {.name
= "mem_hotplug.lock" },
74 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
75 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
76 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
77 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
79 bool memhp_auto_online
;
80 EXPORT_SYMBOL_GPL(memhp_auto_online
);
82 void get_online_mems(void)
85 if (mem_hotplug
.active_writer
== current
)
87 memhp_lock_acquire_read();
88 mutex_lock(&mem_hotplug
.lock
);
89 mem_hotplug
.refcount
++;
90 mutex_unlock(&mem_hotplug
.lock
);
94 void put_online_mems(void)
96 if (mem_hotplug
.active_writer
== current
)
98 mutex_lock(&mem_hotplug
.lock
);
100 if (WARN_ON(!mem_hotplug
.refcount
))
101 mem_hotplug
.refcount
++; /* try to fix things up */
103 if (!--mem_hotplug
.refcount
&& unlikely(mem_hotplug
.active_writer
))
104 wake_up_process(mem_hotplug
.active_writer
);
105 mutex_unlock(&mem_hotplug
.lock
);
106 memhp_lock_release();
110 void mem_hotplug_begin(void)
112 mem_hotplug
.active_writer
= current
;
114 memhp_lock_acquire();
116 mutex_lock(&mem_hotplug
.lock
);
117 if (likely(!mem_hotplug
.refcount
))
119 __set_current_state(TASK_UNINTERRUPTIBLE
);
120 mutex_unlock(&mem_hotplug
.lock
);
125 void mem_hotplug_done(void)
127 mem_hotplug
.active_writer
= NULL
;
128 mutex_unlock(&mem_hotplug
.lock
);
129 memhp_lock_release();
132 /* add this memory to iomem resource */
133 static struct resource
*register_memory_resource(u64 start
, u64 size
)
135 struct resource
*res
;
136 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
139 res
->name
= "System RAM";
141 res
->end
= start
+ size
- 1;
142 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
143 if (request_resource(&iomem_resource
, res
) < 0) {
144 pr_debug("System RAM resource %pR cannot be added\n", res
);
151 static void release_memory_resource(struct resource
*res
)
155 release_resource(res
);
160 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
161 void get_page_bootmem(unsigned long info
, struct page
*page
,
164 page
->lru
.next
= (struct list_head
*) type
;
165 SetPagePrivate(page
);
166 set_page_private(page
, info
);
167 atomic_inc(&page
->_count
);
170 void put_page_bootmem(struct page
*page
)
174 type
= (unsigned long) page
->lru
.next
;
175 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
176 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
178 if (atomic_dec_return(&page
->_count
) == 1) {
179 ClearPagePrivate(page
);
180 set_page_private(page
, 0);
181 INIT_LIST_HEAD(&page
->lru
);
182 free_reserved_page(page
);
186 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
187 #ifndef CONFIG_SPARSEMEM_VMEMMAP
188 static void register_page_bootmem_info_section(unsigned long start_pfn
)
190 unsigned long *usemap
, mapsize
, section_nr
, i
;
191 struct mem_section
*ms
;
192 struct page
*page
, *memmap
;
194 section_nr
= pfn_to_section_nr(start_pfn
);
195 ms
= __nr_to_section(section_nr
);
197 /* Get section's memmap address */
198 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
201 * Get page for the memmap's phys address
202 * XXX: need more consideration for sparse_vmemmap...
204 page
= virt_to_page(memmap
);
205 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
206 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
208 /* remember memmap's page */
209 for (i
= 0; i
< mapsize
; i
++, page
++)
210 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
212 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
213 page
= virt_to_page(usemap
);
215 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
217 for (i
= 0; i
< mapsize
; i
++, page
++)
218 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
221 #else /* CONFIG_SPARSEMEM_VMEMMAP */
222 static void register_page_bootmem_info_section(unsigned long start_pfn
)
224 unsigned long *usemap
, mapsize
, section_nr
, i
;
225 struct mem_section
*ms
;
226 struct page
*page
, *memmap
;
228 if (!pfn_valid(start_pfn
))
231 section_nr
= pfn_to_section_nr(start_pfn
);
232 ms
= __nr_to_section(section_nr
);
234 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
236 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
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
);
246 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
248 void register_page_bootmem_info_node(struct pglist_data
*pgdat
)
250 unsigned long i
, pfn
, end_pfn
, nr_pages
;
251 int node
= pgdat
->node_id
;
255 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
256 page
= virt_to_page(pgdat
);
258 for (i
= 0; i
< nr_pages
; i
++, page
++)
259 get_page_bootmem(node
, page
, NODE_INFO
);
261 zone
= &pgdat
->node_zones
[0];
262 for (; zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1; zone
++) {
263 if (zone_is_initialized(zone
)) {
264 nr_pages
= zone
->wait_table_hash_nr_entries
265 * sizeof(wait_queue_head_t
);
266 nr_pages
= PAGE_ALIGN(nr_pages
) >> PAGE_SHIFT
;
267 page
= virt_to_page(zone
->wait_table
);
269 for (i
= 0; i
< nr_pages
; i
++, page
++)
270 get_page_bootmem(node
, page
, NODE_INFO
);
274 pfn
= pgdat
->node_start_pfn
;
275 end_pfn
= pgdat_end_pfn(pgdat
);
277 /* register section info */
278 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
280 * Some platforms can assign the same pfn to multiple nodes - on
281 * node0 as well as nodeN. To avoid registering a pfn against
282 * multiple nodes we check that this pfn does not already
283 * reside in some other nodes.
285 if (pfn_valid(pfn
) && (pfn_to_nid(pfn
) == node
))
286 register_page_bootmem_info_section(pfn
);
289 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
291 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
292 unsigned long end_pfn
)
294 unsigned long old_zone_end_pfn
;
296 zone_span_writelock(zone
);
298 old_zone_end_pfn
= zone_end_pfn(zone
);
299 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
300 zone
->zone_start_pfn
= start_pfn
;
302 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
303 zone
->zone_start_pfn
;
305 zone_span_writeunlock(zone
);
308 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
309 unsigned long end_pfn
)
311 zone_span_writelock(zone
);
313 if (end_pfn
- start_pfn
) {
314 zone
->zone_start_pfn
= start_pfn
;
315 zone
->spanned_pages
= end_pfn
- start_pfn
;
318 * make it consist as free_area_init_core(),
319 * if spanned_pages = 0, then keep start_pfn = 0
321 zone
->zone_start_pfn
= 0;
322 zone
->spanned_pages
= 0;
325 zone_span_writeunlock(zone
);
328 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
329 unsigned long end_pfn
)
331 enum zone_type zid
= zone_idx(zone
);
332 int nid
= zone
->zone_pgdat
->node_id
;
335 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
336 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
339 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
340 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
341 static int __ref
ensure_zone_is_initialized(struct zone
*zone
,
342 unsigned long start_pfn
, unsigned long num_pages
)
344 if (!zone_is_initialized(zone
))
345 return init_currently_empty_zone(zone
, start_pfn
, num_pages
);
350 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
351 unsigned long start_pfn
, unsigned long end_pfn
)
355 unsigned long z1_start_pfn
;
357 ret
= ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
361 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
363 /* can't move pfns which are higher than @z2 */
364 if (end_pfn
> zone_end_pfn(z2
))
366 /* the move out part must be at the left most of @z2 */
367 if (start_pfn
> z2
->zone_start_pfn
)
369 /* must included/overlap */
370 if (end_pfn
<= z2
->zone_start_pfn
)
373 /* use start_pfn for z1's start_pfn if z1 is empty */
374 if (!zone_is_empty(z1
))
375 z1_start_pfn
= z1
->zone_start_pfn
;
377 z1_start_pfn
= start_pfn
;
379 resize_zone(z1
, z1_start_pfn
, end_pfn
);
380 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
382 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
384 fix_zone_id(z1
, start_pfn
, end_pfn
);
388 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
392 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
393 unsigned long start_pfn
, unsigned long end_pfn
)
397 unsigned long z2_end_pfn
;
399 ret
= ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
403 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
405 /* can't move pfns which are lower than @z1 */
406 if (z1
->zone_start_pfn
> start_pfn
)
408 /* the move out part mast at the right most of @z1 */
409 if (zone_end_pfn(z1
) > end_pfn
)
411 /* must included/overlap */
412 if (start_pfn
>= zone_end_pfn(z1
))
415 /* use end_pfn for z2's end_pfn if z2 is empty */
416 if (!zone_is_empty(z2
))
417 z2_end_pfn
= zone_end_pfn(z2
);
419 z2_end_pfn
= end_pfn
;
421 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
422 resize_zone(z2
, start_pfn
, z2_end_pfn
);
424 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
426 fix_zone_id(z2
, start_pfn
, end_pfn
);
430 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
434 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
435 unsigned long end_pfn
)
437 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
439 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
440 pgdat
->node_start_pfn
= start_pfn
;
442 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
443 pgdat
->node_start_pfn
;
446 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
448 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
449 int nr_pages
= PAGES_PER_SECTION
;
450 int nid
= pgdat
->node_id
;
452 unsigned long flags
, pfn
;
455 zone_type
= zone
- pgdat
->node_zones
;
456 ret
= ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
460 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
461 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
462 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
463 phys_start_pfn
+ nr_pages
);
464 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
465 memmap_init_zone(nr_pages
, nid
, zone_type
,
466 phys_start_pfn
, MEMMAP_HOTPLUG
);
468 /* online_page_range is called later and expects pages reserved */
469 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
473 SetPageReserved(pfn_to_page(pfn
));
478 static int __meminit
__add_section(int nid
, struct zone
*zone
,
479 unsigned long phys_start_pfn
)
483 if (pfn_valid(phys_start_pfn
))
486 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
491 ret
= __add_zone(zone
, phys_start_pfn
);
496 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
500 * Reasonably generic function for adding memory. It is
501 * expected that archs that support memory hotplug will
502 * call this function after deciding the zone to which to
505 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
506 unsigned long nr_pages
)
510 int start_sec
, end_sec
;
511 /* during initialize mem_map, align hot-added range to section */
512 start_sec
= pfn_to_section_nr(phys_start_pfn
);
513 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
515 for (i
= start_sec
; i
<= end_sec
; i
++) {
516 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
));
519 * EEXIST is finally dealt with by ioresource collision
520 * check. see add_memory() => register_memory_resource()
521 * Warning will be printed if there is collision.
523 if (err
&& (err
!= -EEXIST
))
527 vmemmap_populate_print_last();
531 EXPORT_SYMBOL_GPL(__add_pages
);
533 #ifdef CONFIG_MEMORY_HOTREMOVE
534 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
535 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
536 unsigned long start_pfn
,
537 unsigned long end_pfn
)
539 struct mem_section
*ms
;
541 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
542 ms
= __pfn_to_section(start_pfn
);
544 if (unlikely(!valid_section(ms
)))
547 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
550 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
559 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
560 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
561 unsigned long start_pfn
,
562 unsigned long end_pfn
)
564 struct mem_section
*ms
;
567 /* pfn is the end pfn of a memory section. */
569 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
570 ms
= __pfn_to_section(pfn
);
572 if (unlikely(!valid_section(ms
)))
575 if (unlikely(pfn_to_nid(pfn
) != nid
))
578 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
587 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
588 unsigned long end_pfn
)
590 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
591 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
592 unsigned long zone_end_pfn
= z
;
594 struct mem_section
*ms
;
595 int nid
= zone_to_nid(zone
);
597 zone_span_writelock(zone
);
598 if (zone_start_pfn
== start_pfn
) {
600 * If the section is smallest section in the zone, it need
601 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
602 * In this case, we find second smallest valid mem_section
603 * for shrinking zone.
605 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
608 zone
->zone_start_pfn
= pfn
;
609 zone
->spanned_pages
= zone_end_pfn
- pfn
;
611 } else if (zone_end_pfn
== end_pfn
) {
613 * If the section is biggest section in the zone, it need
614 * shrink zone->spanned_pages.
615 * In this case, we find second biggest valid mem_section for
618 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
621 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
625 * The section is not biggest or smallest mem_section in the zone, it
626 * only creates a hole in the zone. So in this case, we need not
627 * change the zone. But perhaps, the zone has only hole data. Thus
628 * it check the zone has only hole or not.
630 pfn
= zone_start_pfn
;
631 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
632 ms
= __pfn_to_section(pfn
);
634 if (unlikely(!valid_section(ms
)))
637 if (page_zone(pfn_to_page(pfn
)) != zone
)
640 /* If the section is current section, it continues the loop */
641 if (start_pfn
== pfn
)
644 /* If we find valid section, we have nothing to do */
645 zone_span_writeunlock(zone
);
649 /* The zone has no valid section */
650 zone
->zone_start_pfn
= 0;
651 zone
->spanned_pages
= 0;
652 zone_span_writeunlock(zone
);
655 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
656 unsigned long start_pfn
, unsigned long end_pfn
)
658 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
659 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
660 unsigned long pgdat_end_pfn
= p
;
662 struct mem_section
*ms
;
663 int nid
= pgdat
->node_id
;
665 if (pgdat_start_pfn
== start_pfn
) {
667 * If the section is smallest section in the pgdat, it need
668 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
669 * In this case, we find second smallest valid mem_section
670 * for shrinking zone.
672 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
675 pgdat
->node_start_pfn
= pfn
;
676 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
678 } else if (pgdat_end_pfn
== end_pfn
) {
680 * If the section is biggest section in the pgdat, it need
681 * shrink pgdat->node_spanned_pages.
682 * In this case, we find second biggest valid mem_section for
685 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
688 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
692 * If the section is not biggest or smallest mem_section in the pgdat,
693 * it only creates a hole in the pgdat. So in this case, we need not
695 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
696 * has only hole or not.
698 pfn
= pgdat_start_pfn
;
699 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
700 ms
= __pfn_to_section(pfn
);
702 if (unlikely(!valid_section(ms
)))
705 if (pfn_to_nid(pfn
) != nid
)
708 /* If the section is current section, it continues the loop */
709 if (start_pfn
== pfn
)
712 /* If we find valid section, we have nothing to do */
716 /* The pgdat has no valid section */
717 pgdat
->node_start_pfn
= 0;
718 pgdat
->node_spanned_pages
= 0;
721 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
723 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
724 int nr_pages
= PAGES_PER_SECTION
;
728 zone_type
= zone
- pgdat
->node_zones
;
730 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
731 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
732 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
733 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
736 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
)
738 unsigned long start_pfn
;
742 if (!valid_section(ms
))
745 ret
= unregister_memory_section(ms
);
749 scn_nr
= __section_nr(ms
);
750 start_pfn
= section_nr_to_pfn(scn_nr
);
751 __remove_zone(zone
, start_pfn
);
753 sparse_remove_one_section(zone
, ms
);
758 * __remove_pages() - remove sections of pages from a zone
759 * @zone: zone from which pages need to be removed
760 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
761 * @nr_pages: number of pages to remove (must be multiple of section size)
763 * Generic helper function to remove section mappings and sysfs entries
764 * for the section of the memory we are removing. Caller needs to make
765 * sure that pages are marked reserved and zones are adjust properly by
766 * calling offline_pages().
768 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
769 unsigned long nr_pages
)
772 int sections_to_remove
;
773 resource_size_t start
, size
;
777 * We can only remove entire sections
779 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
780 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
782 start
= phys_start_pfn
<< PAGE_SHIFT
;
783 size
= nr_pages
* PAGE_SIZE
;
785 /* in the ZONE_DEVICE case device driver owns the memory region */
786 if (!is_dev_zone(zone
))
787 ret
= release_mem_region_adjustable(&iomem_resource
, start
, size
);
789 resource_size_t endres
= start
+ size
- 1;
791 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
792 &start
, &endres
, ret
);
795 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
796 for (i
= 0; i
< sections_to_remove
; i
++) {
797 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
798 ret
= __remove_section(zone
, __pfn_to_section(pfn
));
804 EXPORT_SYMBOL_GPL(__remove_pages
);
805 #endif /* CONFIG_MEMORY_HOTREMOVE */
807 int set_online_page_callback(online_page_callback_t callback
)
812 mutex_lock(&online_page_callback_lock
);
814 if (online_page_callback
== generic_online_page
) {
815 online_page_callback
= callback
;
819 mutex_unlock(&online_page_callback_lock
);
824 EXPORT_SYMBOL_GPL(set_online_page_callback
);
826 int restore_online_page_callback(online_page_callback_t callback
)
831 mutex_lock(&online_page_callback_lock
);
833 if (online_page_callback
== callback
) {
834 online_page_callback
= generic_online_page
;
838 mutex_unlock(&online_page_callback_lock
);
843 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
845 void __online_page_set_limits(struct page
*page
)
848 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
850 void __online_page_increment_counters(struct page
*page
)
852 adjust_managed_page_count(page
, 1);
854 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
856 void __online_page_free(struct page
*page
)
858 __free_reserved_page(page
);
860 EXPORT_SYMBOL_GPL(__online_page_free
);
862 static void generic_online_page(struct page
*page
)
864 __online_page_set_limits(page
);
865 __online_page_increment_counters(page
);
866 __online_page_free(page
);
869 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
873 unsigned long onlined_pages
= *(unsigned long *)arg
;
875 if (PageReserved(pfn_to_page(start_pfn
)))
876 for (i
= 0; i
< nr_pages
; i
++) {
877 page
= pfn_to_page(start_pfn
+ i
);
878 (*online_page_callback
)(page
);
881 *(unsigned long *)arg
= onlined_pages
;
885 #ifdef CONFIG_MOVABLE_NODE
887 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
890 static bool can_online_high_movable(struct zone
*zone
)
894 #else /* CONFIG_MOVABLE_NODE */
895 /* ensure every online node has NORMAL memory */
896 static bool can_online_high_movable(struct zone
*zone
)
898 return node_state(zone_to_nid(zone
), N_NORMAL_MEMORY
);
900 #endif /* CONFIG_MOVABLE_NODE */
902 /* check which state of node_states will be changed when online memory */
903 static void node_states_check_changes_online(unsigned long nr_pages
,
904 struct zone
*zone
, struct memory_notify
*arg
)
906 int nid
= zone_to_nid(zone
);
907 enum zone_type zone_last
= ZONE_NORMAL
;
910 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
911 * contains nodes which have zones of 0...ZONE_NORMAL,
912 * set zone_last to ZONE_NORMAL.
914 * If we don't have HIGHMEM nor movable node,
915 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
916 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
918 if (N_MEMORY
== N_NORMAL_MEMORY
)
919 zone_last
= ZONE_MOVABLE
;
922 * if the memory to be online is in a zone of 0...zone_last, and
923 * the zones of 0...zone_last don't have memory before online, we will
924 * need to set the node to node_states[N_NORMAL_MEMORY] after
925 * the memory is online.
927 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
928 arg
->status_change_nid_normal
= nid
;
930 arg
->status_change_nid_normal
= -1;
932 #ifdef CONFIG_HIGHMEM
934 * If we have movable node, node_states[N_HIGH_MEMORY]
935 * contains nodes which have zones of 0...ZONE_HIGHMEM,
936 * set zone_last to ZONE_HIGHMEM.
938 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
939 * contains nodes which have zones of 0...ZONE_MOVABLE,
940 * set zone_last to ZONE_MOVABLE.
942 zone_last
= ZONE_HIGHMEM
;
943 if (N_MEMORY
== N_HIGH_MEMORY
)
944 zone_last
= ZONE_MOVABLE
;
946 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
947 arg
->status_change_nid_high
= nid
;
949 arg
->status_change_nid_high
= -1;
951 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
955 * if the node don't have memory befor online, we will need to
956 * set the node to node_states[N_MEMORY] after the memory
959 if (!node_state(nid
, N_MEMORY
))
960 arg
->status_change_nid
= nid
;
962 arg
->status_change_nid
= -1;
965 static void node_states_set_node(int node
, struct memory_notify
*arg
)
967 if (arg
->status_change_nid_normal
>= 0)
968 node_set_state(node
, N_NORMAL_MEMORY
);
970 if (arg
->status_change_nid_high
>= 0)
971 node_set_state(node
, N_HIGH_MEMORY
);
973 node_set_state(node
, N_MEMORY
);
977 /* Must be protected by mem_hotplug_begin() */
978 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
981 unsigned long onlined_pages
= 0;
983 int need_zonelists_rebuild
= 0;
986 struct memory_notify arg
;
989 * This doesn't need a lock to do pfn_to_page().
990 * The section can't be removed here because of the
991 * memory_block->state_mutex.
993 zone
= page_zone(pfn_to_page(pfn
));
995 if ((zone_idx(zone
) > ZONE_NORMAL
||
996 online_type
== MMOP_ONLINE_MOVABLE
) &&
997 !can_online_high_movable(zone
))
1000 if (online_type
== MMOP_ONLINE_KERNEL
&&
1001 zone_idx(zone
) == ZONE_MOVABLE
) {
1002 if (move_pfn_range_left(zone
- 1, zone
, pfn
, pfn
+ nr_pages
))
1005 if (online_type
== MMOP_ONLINE_MOVABLE
&&
1006 zone_idx(zone
) == ZONE_MOVABLE
- 1) {
1007 if (move_pfn_range_right(zone
, zone
+ 1, pfn
, pfn
+ nr_pages
))
1011 /* Previous code may changed the zone of the pfn range */
1012 zone
= page_zone(pfn_to_page(pfn
));
1014 arg
.start_pfn
= pfn
;
1015 arg
.nr_pages
= nr_pages
;
1016 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1018 nid
= pfn_to_nid(pfn
);
1020 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1021 ret
= notifier_to_errno(ret
);
1023 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1027 * If this zone is not populated, then it is not in zonelist.
1028 * This means the page allocator ignores this zone.
1029 * So, zonelist must be updated after online.
1031 mutex_lock(&zonelists_mutex
);
1032 if (!populated_zone(zone
)) {
1033 need_zonelists_rebuild
= 1;
1034 build_all_zonelists(NULL
, zone
);
1037 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1038 online_pages_range
);
1040 if (need_zonelists_rebuild
)
1041 zone_pcp_reset(zone
);
1042 mutex_unlock(&zonelists_mutex
);
1043 printk(KERN_DEBUG
"online_pages [mem %#010llx-%#010llx] failed\n",
1044 (unsigned long long) pfn
<< PAGE_SHIFT
,
1045 (((unsigned long long) pfn
+ nr_pages
)
1046 << PAGE_SHIFT
) - 1);
1047 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1051 zone
->present_pages
+= onlined_pages
;
1053 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1054 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1055 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1057 if (onlined_pages
) {
1058 node_states_set_node(zone_to_nid(zone
), &arg
);
1059 if (need_zonelists_rebuild
)
1060 build_all_zonelists(NULL
, NULL
);
1062 zone_pcp_update(zone
);
1065 mutex_unlock(&zonelists_mutex
);
1067 init_per_zone_wmark_min();
1070 kswapd_run(zone_to_nid(zone
));
1072 vm_total_pages
= nr_free_pagecache_pages();
1074 writeback_set_ratelimit();
1077 memory_notify(MEM_ONLINE
, &arg
);
1080 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1082 static void reset_node_present_pages(pg_data_t
*pgdat
)
1086 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1087 z
->present_pages
= 0;
1089 pgdat
->node_present_pages
= 0;
1092 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1093 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1095 struct pglist_data
*pgdat
;
1096 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1097 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1098 unsigned long start_pfn
= PFN_DOWN(start
);
1100 pgdat
= NODE_DATA(nid
);
1102 pgdat
= arch_alloc_nodedata(nid
);
1106 arch_refresh_nodedata(nid
, pgdat
);
1108 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1109 pgdat
->nr_zones
= 0;
1110 pgdat
->classzone_idx
= 0;
1113 /* we can use NODE_DATA(nid) from here */
1115 /* init node's zones as empty zones, we don't have any present pages.*/
1116 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1119 * The node we allocated has no zone fallback lists. For avoiding
1120 * to access not-initialized zonelist, build here.
1122 mutex_lock(&zonelists_mutex
);
1123 build_all_zonelists(pgdat
, NULL
);
1124 mutex_unlock(&zonelists_mutex
);
1127 * zone->managed_pages is set to an approximate value in
1128 * free_area_init_core(), which will cause
1129 * /sys/device/system/node/nodeX/meminfo has wrong data.
1130 * So reset it to 0 before any memory is onlined.
1132 reset_node_managed_pages(pgdat
);
1135 * When memory is hot-added, all the memory is in offline state. So
1136 * clear all zones' present_pages because they will be updated in
1137 * online_pages() and offline_pages().
1139 reset_node_present_pages(pgdat
);
1144 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1146 arch_refresh_nodedata(nid
, NULL
);
1147 arch_free_nodedata(pgdat
);
1153 * try_online_node - online a node if offlined
1155 * called by cpu_up() to online a node without onlined memory.
1157 int try_online_node(int nid
)
1162 if (node_online(nid
))
1165 mem_hotplug_begin();
1166 pgdat
= hotadd_new_pgdat(nid
, 0);
1168 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1172 node_set_online(nid
);
1173 ret
= register_one_node(nid
);
1176 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1177 mutex_lock(&zonelists_mutex
);
1178 build_all_zonelists(NULL
, NULL
);
1179 mutex_unlock(&zonelists_mutex
);
1187 static int check_hotplug_memory_range(u64 start
, u64 size
)
1189 u64 start_pfn
= PFN_DOWN(start
);
1190 u64 nr_pages
= size
>> PAGE_SHIFT
;
1192 /* Memory range must be aligned with section */
1193 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1194 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1195 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1196 (unsigned long long)start
,
1197 (unsigned long long)size
);
1205 * If movable zone has already been setup, newly added memory should be check.
1206 * If its address is higher than movable zone, it should be added as movable.
1207 * Without this check, movable zone may overlap with other zone.
1209 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1211 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1212 pg_data_t
*pgdat
= NODE_DATA(nid
);
1213 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1215 if (zone_is_empty(movable_zone
))
1218 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1224 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
,
1227 #ifdef CONFIG_ZONE_DEVICE
1231 if (should_add_memory_movable(nid
, start
, size
))
1232 return ZONE_MOVABLE
;
1234 return zone_default
;
1237 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1239 return memory_block_change_state(mem
, MEM_ONLINE
, MEM_OFFLINE
);
1242 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1243 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1246 pg_data_t
*pgdat
= NULL
;
1252 size
= resource_size(res
);
1254 ret
= check_hotplug_memory_range(start
, size
);
1258 { /* Stupid hack to suppress address-never-null warning */
1259 void *p
= NODE_DATA(nid
);
1263 mem_hotplug_begin();
1266 * Add new range to memblock so that when hotadd_new_pgdat() is called
1267 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1268 * this new range and calculate total pages correctly. The range will
1269 * be removed at hot-remove time.
1271 memblock_add_node(start
, size
, nid
);
1273 new_node
= !node_online(nid
);
1275 pgdat
= hotadd_new_pgdat(nid
, start
);
1281 /* call arch's memory hotadd */
1282 ret
= arch_add_memory(nid
, start
, size
, false);
1287 /* we online node here. we can't roll back from here. */
1288 node_set_online(nid
);
1291 ret
= register_one_node(nid
);
1293 * If sysfs file of new node can't create, cpu on the node
1294 * can't be hot-added. There is no rollback way now.
1295 * So, check by BUG_ON() to catch it reluctantly..
1300 /* create new memmap entry */
1301 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1303 /* online pages if requested */
1305 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1306 NULL
, online_memory_block
);
1311 /* rollback pgdat allocation and others */
1313 rollback_node_hotadd(nid
, pgdat
);
1314 memblock_remove(start
, size
);
1320 EXPORT_SYMBOL_GPL(add_memory_resource
);
1322 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1324 struct resource
*res
;
1327 res
= register_memory_resource(start
, size
);
1331 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1333 release_memory_resource(res
);
1336 EXPORT_SYMBOL_GPL(add_memory
);
1338 #ifdef CONFIG_MEMORY_HOTREMOVE
1340 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1341 * set and the size of the free page is given by page_order(). Using this,
1342 * the function determines if the pageblock contains only free pages.
1343 * Due to buddy contraints, a free page at least the size of a pageblock will
1344 * be located at the start of the pageblock
1346 static inline int pageblock_free(struct page
*page
)
1348 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1351 /* Return the start of the next active pageblock after a given page */
1352 static struct page
*next_active_pageblock(struct page
*page
)
1354 /* Ensure the starting page is pageblock-aligned */
1355 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1357 /* If the entire pageblock is free, move to the end of free page */
1358 if (pageblock_free(page
)) {
1360 /* be careful. we don't have locks, page_order can be changed.*/
1361 order
= page_order(page
);
1362 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1363 return page
+ (1 << order
);
1366 return page
+ pageblock_nr_pages
;
1369 /* Checks if this range of memory is likely to be hot-removable. */
1370 int is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1372 struct page
*page
= pfn_to_page(start_pfn
);
1373 struct page
*end_page
= page
+ nr_pages
;
1375 /* Check the starting page of each pageblock within the range */
1376 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1377 if (!is_pageblock_removable_nolock(page
))
1382 /* All pageblocks in the memory block are likely to be hot-removable */
1387 * Confirm all pages in a range [start, end) belong to the same zone.
1388 * When true, return its valid [start, end).
1390 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
,
1391 unsigned long *valid_start
, unsigned long *valid_end
)
1393 unsigned long pfn
, sec_end_pfn
;
1394 unsigned long start
, end
;
1395 struct zone
*zone
= NULL
;
1398 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1400 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1401 /* Make sure the memory section is present first */
1402 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1404 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1405 pfn
+= MAX_ORDER_NR_PAGES
) {
1407 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1408 while ((i
< MAX_ORDER_NR_PAGES
) &&
1409 !pfn_valid_within(pfn
+ i
))
1411 if (i
== MAX_ORDER_NR_PAGES
)
1413 page
= pfn_to_page(pfn
+ i
);
1414 if (zone
&& page_zone(page
) != zone
)
1418 zone
= page_zone(page
);
1419 end
= pfn
+ MAX_ORDER_NR_PAGES
;
1424 *valid_start
= start
;
1433 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1434 * and hugepages). We scan pfn because it's much easier than scanning over
1435 * linked list. This function returns the pfn of the first found movable
1436 * page if it's found, otherwise 0.
1438 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1442 for (pfn
= start
; pfn
< end
; pfn
++) {
1443 if (pfn_valid(pfn
)) {
1444 page
= pfn_to_page(pfn
);
1447 if (PageHuge(page
)) {
1448 if (page_huge_active(page
))
1451 pfn
= round_up(pfn
+ 1,
1452 1 << compound_order(page
)) - 1;
1459 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1461 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1465 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1466 int not_managed
= 0;
1470 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1471 if (!pfn_valid(pfn
))
1473 page
= pfn_to_page(pfn
);
1475 if (PageHuge(page
)) {
1476 struct page
*head
= compound_head(page
);
1477 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1478 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1482 if (isolate_huge_page(page
, &source
))
1483 move_pages
-= 1 << compound_order(head
);
1487 if (!get_page_unless_zero(page
))
1490 * We can skip free pages. And we can only deal with pages on
1493 ret
= isolate_lru_page(page
);
1494 if (!ret
) { /* Success */
1496 list_add_tail(&page
->lru
, &source
);
1498 inc_zone_page_state(page
, NR_ISOLATED_ANON
+
1499 page_is_file_cache(page
));
1502 #ifdef CONFIG_DEBUG_VM
1503 printk(KERN_ALERT
"removing pfn %lx from LRU failed\n",
1505 dump_page(page
, "failed to remove from LRU");
1508 /* Because we don't have big zone->lock. we should
1509 check this again here. */
1510 if (page_count(page
)) {
1517 if (!list_empty(&source
)) {
1519 putback_movable_pages(&source
);
1524 * alloc_migrate_target should be improooooved!!
1525 * migrate_pages returns # of failed pages.
1527 ret
= migrate_pages(&source
, alloc_migrate_target
, NULL
, 0,
1528 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1530 putback_movable_pages(&source
);
1537 * remove from free_area[] and mark all as Reserved.
1540 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1543 __offline_isolated_pages(start
, start
+ nr_pages
);
1548 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1550 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1551 offline_isolated_pages_cb
);
1555 * Check all pages in range, recoreded as memory resource, are isolated.
1558 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1562 long offlined
= *(long *)data
;
1563 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1564 offlined
= nr_pages
;
1566 *(long *)data
+= offlined
;
1571 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1576 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1577 check_pages_isolated_cb
);
1579 offlined
= (long)ret
;
1583 #ifdef CONFIG_MOVABLE_NODE
1585 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1588 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1592 #else /* CONFIG_MOVABLE_NODE */
1593 /* ensure the node has NORMAL memory if it is still online */
1594 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1596 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1597 unsigned long present_pages
= 0;
1600 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1601 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1603 if (present_pages
> nr_pages
)
1607 for (; zt
<= ZONE_MOVABLE
; zt
++)
1608 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1611 * we can't offline the last normal memory until all
1612 * higher memory is offlined.
1614 return present_pages
== 0;
1616 #endif /* CONFIG_MOVABLE_NODE */
1618 static int __init
cmdline_parse_movable_node(char *p
)
1620 #ifdef CONFIG_MOVABLE_NODE
1622 * Memory used by the kernel cannot be hot-removed because Linux
1623 * cannot migrate the kernel pages. When memory hotplug is
1624 * enabled, we should prevent memblock from allocating memory
1627 * ACPI SRAT records all hotpluggable memory ranges. But before
1628 * SRAT is parsed, we don't know about it.
1630 * The kernel image is loaded into memory at very early time. We
1631 * cannot prevent this anyway. So on NUMA system, we set any
1632 * node the kernel resides in as un-hotpluggable.
1634 * Since on modern servers, one node could have double-digit
1635 * gigabytes memory, we can assume the memory around the kernel
1636 * image is also un-hotpluggable. So before SRAT is parsed, just
1637 * allocate memory near the kernel image to try the best to keep
1638 * the kernel away from hotpluggable memory.
1640 memblock_set_bottom_up(true);
1641 movable_node_enabled
= true;
1643 pr_warn("movable_node option not supported\n");
1647 early_param("movable_node", cmdline_parse_movable_node
);
1649 /* check which state of node_states will be changed when offline memory */
1650 static void node_states_check_changes_offline(unsigned long nr_pages
,
1651 struct zone
*zone
, struct memory_notify
*arg
)
1653 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1654 unsigned long present_pages
= 0;
1655 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1658 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1659 * contains nodes which have zones of 0...ZONE_NORMAL,
1660 * set zone_last to ZONE_NORMAL.
1662 * If we don't have HIGHMEM nor movable node,
1663 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1664 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1666 if (N_MEMORY
== N_NORMAL_MEMORY
)
1667 zone_last
= ZONE_MOVABLE
;
1670 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1671 * If the memory to be offline is in a zone of 0...zone_last,
1672 * and it is the last present memory, 0...zone_last will
1673 * become empty after offline , thus we can determind we will
1674 * need to clear the node from node_states[N_NORMAL_MEMORY].
1676 for (zt
= 0; zt
<= zone_last
; zt
++)
1677 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1678 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1679 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1681 arg
->status_change_nid_normal
= -1;
1683 #ifdef CONFIG_HIGHMEM
1685 * If we have movable node, node_states[N_HIGH_MEMORY]
1686 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1687 * set zone_last to ZONE_HIGHMEM.
1689 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1690 * contains nodes which have zones of 0...ZONE_MOVABLE,
1691 * set zone_last to ZONE_MOVABLE.
1693 zone_last
= ZONE_HIGHMEM
;
1694 if (N_MEMORY
== N_HIGH_MEMORY
)
1695 zone_last
= ZONE_MOVABLE
;
1697 for (; zt
<= zone_last
; zt
++)
1698 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1699 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1700 arg
->status_change_nid_high
= zone_to_nid(zone
);
1702 arg
->status_change_nid_high
= -1;
1704 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1708 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1710 zone_last
= ZONE_MOVABLE
;
1713 * check whether node_states[N_HIGH_MEMORY] will be changed
1714 * If we try to offline the last present @nr_pages from the node,
1715 * we can determind we will need to clear the node from
1716 * node_states[N_HIGH_MEMORY].
1718 for (; zt
<= zone_last
; zt
++)
1719 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1720 if (nr_pages
>= present_pages
)
1721 arg
->status_change_nid
= zone_to_nid(zone
);
1723 arg
->status_change_nid
= -1;
1726 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1728 if (arg
->status_change_nid_normal
>= 0)
1729 node_clear_state(node
, N_NORMAL_MEMORY
);
1731 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1732 (arg
->status_change_nid_high
>= 0))
1733 node_clear_state(node
, N_HIGH_MEMORY
);
1735 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1736 (arg
->status_change_nid
>= 0))
1737 node_clear_state(node
, N_MEMORY
);
1740 static int __ref
__offline_pages(unsigned long start_pfn
,
1741 unsigned long end_pfn
, unsigned long timeout
)
1743 unsigned long pfn
, nr_pages
, expire
;
1744 long offlined_pages
;
1745 int ret
, drain
, retry_max
, node
;
1746 unsigned long flags
;
1747 unsigned long valid_start
, valid_end
;
1749 struct memory_notify arg
;
1751 /* at least, alignment against pageblock is necessary */
1752 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1754 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1756 /* This makes hotplug much easier...and readable.
1757 we assume this for now. .*/
1758 if (!test_pages_in_a_zone(start_pfn
, end_pfn
, &valid_start
, &valid_end
))
1761 zone
= page_zone(pfn_to_page(valid_start
));
1762 node
= zone_to_nid(zone
);
1763 nr_pages
= end_pfn
- start_pfn
;
1765 if (zone_idx(zone
) <= ZONE_NORMAL
&& !can_offline_normal(zone
, nr_pages
))
1768 /* set above range as isolated */
1769 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1770 MIGRATE_MOVABLE
, true);
1774 arg
.start_pfn
= start_pfn
;
1775 arg
.nr_pages
= nr_pages
;
1776 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1778 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1779 ret
= notifier_to_errno(ret
);
1781 goto failed_removal
;
1784 expire
= jiffies
+ timeout
;
1788 /* start memory hot removal */
1790 if (time_after(jiffies
, expire
))
1791 goto failed_removal
;
1793 if (signal_pending(current
))
1794 goto failed_removal
;
1797 lru_add_drain_all();
1799 drain_all_pages(zone
);
1802 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1803 if (pfn
) { /* We have movable pages */
1804 ret
= do_migrate_range(pfn
, end_pfn
);
1810 if (--retry_max
== 0)
1811 goto failed_removal
;
1817 /* drain all zone's lru pagevec, this is asynchronous... */
1818 lru_add_drain_all();
1820 /* drain pcp pages, this is synchronous. */
1821 drain_all_pages(zone
);
1823 * dissolve free hugepages in the memory block before doing offlining
1824 * actually in order to make hugetlbfs's object counting consistent.
1826 dissolve_free_huge_pages(start_pfn
, end_pfn
);
1828 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1829 if (offlined_pages
< 0) {
1831 goto failed_removal
;
1833 printk(KERN_INFO
"Offlined Pages %ld\n", offlined_pages
);
1834 /* Ok, all of our target is isolated.
1835 We cannot do rollback at this point. */
1836 offline_isolated_pages(start_pfn
, end_pfn
);
1837 /* reset pagetype flags and makes migrate type to be MOVABLE */
1838 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1839 /* removal success */
1840 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1841 zone
->present_pages
-= offlined_pages
;
1843 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1844 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1845 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1847 init_per_zone_wmark_min();
1849 if (!populated_zone(zone
)) {
1850 zone_pcp_reset(zone
);
1851 mutex_lock(&zonelists_mutex
);
1852 build_all_zonelists(NULL
, NULL
);
1853 mutex_unlock(&zonelists_mutex
);
1855 zone_pcp_update(zone
);
1857 node_states_clear_node(node
, &arg
);
1858 if (arg
.status_change_nid
>= 0)
1861 vm_total_pages
= nr_free_pagecache_pages();
1862 writeback_set_ratelimit();
1864 memory_notify(MEM_OFFLINE
, &arg
);
1868 printk(KERN_INFO
"memory offlining [mem %#010llx-%#010llx] failed\n",
1869 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1870 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1871 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1872 /* pushback to free area */
1873 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1877 /* Must be protected by mem_hotplug_begin() */
1878 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1880 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
, 120 * HZ
);
1882 #endif /* CONFIG_MEMORY_HOTREMOVE */
1885 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1886 * @start_pfn: start pfn of the memory range
1887 * @end_pfn: end pfn of the memory range
1888 * @arg: argument passed to func
1889 * @func: callback for each memory section walked
1891 * This function walks through all present mem sections in range
1892 * [start_pfn, end_pfn) and call func on each mem section.
1894 * Returns the return value of func.
1896 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1897 void *arg
, int (*func
)(struct memory_block
*, void *))
1899 struct memory_block
*mem
= NULL
;
1900 struct mem_section
*section
;
1901 unsigned long pfn
, section_nr
;
1904 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1905 section_nr
= pfn_to_section_nr(pfn
);
1906 if (!present_section_nr(section_nr
))
1909 section
= __nr_to_section(section_nr
);
1910 /* same memblock? */
1912 if ((section_nr
>= mem
->start_section_nr
) &&
1913 (section_nr
<= mem
->end_section_nr
))
1916 mem
= find_memory_block_hinted(section
, mem
);
1920 ret
= func(mem
, arg
);
1922 kobject_put(&mem
->dev
.kobj
);
1928 kobject_put(&mem
->dev
.kobj
);
1933 #ifdef CONFIG_MEMORY_HOTREMOVE
1934 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1936 int ret
= !is_memblock_offlined(mem
);
1938 if (unlikely(ret
)) {
1939 phys_addr_t beginpa
, endpa
;
1941 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1942 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1943 pr_warn("removing memory fails, because memory "
1944 "[%pa-%pa] is onlined\n",
1951 static int check_cpu_on_node(pg_data_t
*pgdat
)
1955 for_each_present_cpu(cpu
) {
1956 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1958 * the cpu on this node isn't removed, and we can't
1959 * offline this node.
1967 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1969 #ifdef CONFIG_ACPI_NUMA
1972 for_each_possible_cpu(cpu
)
1973 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1974 numa_clear_node(cpu
);
1978 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
1982 ret
= check_cpu_on_node(pgdat
);
1987 * the node will be offlined when we come here, so we can clear
1988 * the cpu_to_node() now.
1991 unmap_cpu_on_node(pgdat
);
1998 * Offline a node if all memory sections and cpus of the node are removed.
2000 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2001 * and online/offline operations before this call.
2003 void try_offline_node(int nid
)
2005 pg_data_t
*pgdat
= NODE_DATA(nid
);
2006 unsigned long start_pfn
= pgdat
->node_start_pfn
;
2007 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
2011 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2012 unsigned long section_nr
= pfn_to_section_nr(pfn
);
2014 if (!present_section_nr(section_nr
))
2017 if (pfn_to_nid(pfn
) != nid
)
2021 * some memory sections of this node are not removed, and we
2022 * can't offline node now.
2027 if (check_and_unmap_cpu_on_node(pgdat
))
2031 * all memory/cpu of this node are removed, we can offline this
2034 node_set_offline(nid
);
2035 unregister_one_node(nid
);
2037 /* free waittable in each zone */
2038 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
2039 struct zone
*zone
= pgdat
->node_zones
+ i
;
2042 * wait_table may be allocated from boot memory,
2043 * here only free if it's allocated by vmalloc.
2045 if (is_vmalloc_addr(zone
->wait_table
)) {
2046 vfree(zone
->wait_table
);
2047 zone
->wait_table
= NULL
;
2051 EXPORT_SYMBOL(try_offline_node
);
2056 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2057 * and online/offline operations before this call, as required by
2058 * try_offline_node().
2060 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2064 BUG_ON(check_hotplug_memory_range(start
, size
));
2066 mem_hotplug_begin();
2069 * All memory blocks must be offlined before removing memory. Check
2070 * whether all memory blocks in question are offline and trigger a BUG()
2071 * if this is not the case.
2073 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2074 check_memblock_offlined_cb
);
2078 /* remove memmap entry */
2079 firmware_map_remove(start
, start
+ size
, "System RAM");
2080 memblock_free(start
, size
);
2081 memblock_remove(start
, size
);
2083 arch_remove_memory(start
, size
);
2085 try_offline_node(nid
);
2089 EXPORT_SYMBOL_GPL(remove_memory
);
2090 #endif /* CONFIG_MEMORY_HOTREMOVE */