2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/sched/signal.h>
10 #include <linux/swap.h>
11 #include <linux/interrupt.h>
12 #include <linux/pagemap.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
15 #include <linux/pagevec.h>
16 #include <linux/writeback.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/memremap.h>
22 #include <linux/memory_hotplug.h>
23 #include <linux/highmem.h>
24 #include <linux/vmalloc.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/migrate.h>
28 #include <linux/page-isolation.h>
29 #include <linux/pfn.h>
30 #include <linux/suspend.h>
31 #include <linux/mm_inline.h>
32 #include <linux/firmware-map.h>
33 #include <linux/stop_machine.h>
34 #include <linux/hugetlb.h>
35 #include <linux/memblock.h>
36 #include <linux/bootmem.h>
37 #include <linux/compaction.h>
39 #include <asm/tlbflush.h>
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
50 static void generic_online_page(struct page
*page
);
52 static online_page_callback_t online_page_callback
= generic_online_page
;
53 static DEFINE_MUTEX(online_page_callback_lock
);
55 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock
);
57 void get_online_mems(void)
59 percpu_down_read(&mem_hotplug_lock
);
62 void put_online_mems(void)
64 percpu_up_read(&mem_hotplug_lock
);
67 bool movable_node_enabled
= false;
69 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
70 bool memhp_auto_online
;
72 bool memhp_auto_online
= true;
74 EXPORT_SYMBOL_GPL(memhp_auto_online
);
76 static int __init
setup_memhp_default_state(char *str
)
78 if (!strcmp(str
, "online"))
79 memhp_auto_online
= true;
80 else if (!strcmp(str
, "offline"))
81 memhp_auto_online
= false;
85 __setup("memhp_default_state=", setup_memhp_default_state
);
87 void mem_hotplug_begin(void)
90 percpu_down_write(&mem_hotplug_lock
);
93 void mem_hotplug_done(void)
95 percpu_up_write(&mem_hotplug_lock
);
99 /* add this memory to iomem resource */
100 static struct resource
*register_memory_resource(u64 start
, u64 size
)
102 struct resource
*res
;
103 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
105 return ERR_PTR(-ENOMEM
);
107 res
->name
= "System RAM";
109 res
->end
= start
+ size
- 1;
110 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
111 if (request_resource(&iomem_resource
, res
) < 0) {
112 pr_debug("System RAM resource %pR cannot be added\n", res
);
114 return ERR_PTR(-EEXIST
);
119 static void release_memory_resource(struct resource
*res
)
123 release_resource(res
);
128 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
129 void get_page_bootmem(unsigned long info
, struct page
*page
,
132 page
->freelist
= (void *)type
;
133 SetPagePrivate(page
);
134 set_page_private(page
, info
);
138 void put_page_bootmem(struct page
*page
)
142 type
= (unsigned long) page
->freelist
;
143 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
144 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
146 if (page_ref_dec_return(page
) == 1) {
147 page
->freelist
= NULL
;
148 ClearPagePrivate(page
);
149 set_page_private(page
, 0);
150 INIT_LIST_HEAD(&page
->lru
);
151 free_reserved_page(page
);
155 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
156 #ifndef CONFIG_SPARSEMEM_VMEMMAP
157 static void register_page_bootmem_info_section(unsigned long start_pfn
)
159 unsigned long *usemap
, mapsize
, section_nr
, i
;
160 struct mem_section
*ms
;
161 struct page
*page
, *memmap
;
163 section_nr
= pfn_to_section_nr(start_pfn
);
164 ms
= __nr_to_section(section_nr
);
166 /* Get section's memmap address */
167 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
170 * Get page for the memmap's phys address
171 * XXX: need more consideration for sparse_vmemmap...
173 page
= virt_to_page(memmap
);
174 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
175 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
177 /* remember memmap's page */
178 for (i
= 0; i
< mapsize
; i
++, page
++)
179 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
181 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
182 page
= virt_to_page(usemap
);
184 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
186 for (i
= 0; i
< mapsize
; i
++, page
++)
187 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
190 #else /* CONFIG_SPARSEMEM_VMEMMAP */
191 static void register_page_bootmem_info_section(unsigned long start_pfn
)
193 unsigned long *usemap
, mapsize
, section_nr
, i
;
194 struct mem_section
*ms
;
195 struct page
*page
, *memmap
;
197 if (!pfn_valid(start_pfn
))
200 section_nr
= pfn_to_section_nr(start_pfn
);
201 ms
= __nr_to_section(section_nr
);
203 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
205 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
207 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
208 page
= virt_to_page(usemap
);
210 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
212 for (i
= 0; i
< mapsize
; i
++, page
++)
213 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
215 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
217 void __init
register_page_bootmem_info_node(struct pglist_data
*pgdat
)
219 unsigned long i
, pfn
, end_pfn
, nr_pages
;
220 int node
= pgdat
->node_id
;
223 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
224 page
= virt_to_page(pgdat
);
226 for (i
= 0; i
< nr_pages
; i
++, page
++)
227 get_page_bootmem(node
, page
, NODE_INFO
);
229 pfn
= pgdat
->node_start_pfn
;
230 end_pfn
= pgdat_end_pfn(pgdat
);
232 /* register section info */
233 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
235 * Some platforms can assign the same pfn to multiple nodes - on
236 * node0 as well as nodeN. To avoid registering a pfn against
237 * multiple nodes we check that this pfn does not already
238 * reside in some other nodes.
240 if (pfn_valid(pfn
) && (early_pfn_to_nid(pfn
) == node
))
241 register_page_bootmem_info_section(pfn
);
244 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
246 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
247 unsigned long end_pfn
)
249 unsigned long old_zone_end_pfn
;
251 zone_span_writelock(zone
);
253 old_zone_end_pfn
= zone_end_pfn(zone
);
254 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
255 zone
->zone_start_pfn
= start_pfn
;
257 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
258 zone
->zone_start_pfn
;
260 zone_span_writeunlock(zone
);
263 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
264 unsigned long end_pfn
)
266 zone_span_writelock(zone
);
268 if (end_pfn
- start_pfn
) {
269 zone
->zone_start_pfn
= start_pfn
;
270 zone
->spanned_pages
= end_pfn
- start_pfn
;
273 * make it consist as free_area_init_core(),
274 * if spanned_pages = 0, then keep start_pfn = 0
276 zone
->zone_start_pfn
= 0;
277 zone
->spanned_pages
= 0;
280 zone_span_writeunlock(zone
);
283 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
284 unsigned long end_pfn
)
286 enum zone_type zid
= zone_idx(zone
);
287 int nid
= zone
->zone_pgdat
->node_id
;
290 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
291 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
294 static void __ref
ensure_zone_is_initialized(struct zone
*zone
,
295 unsigned long start_pfn
, unsigned long num_pages
)
297 if (!zone_is_initialized(zone
))
298 init_currently_empty_zone(zone
, start_pfn
, num_pages
);
301 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
302 unsigned long start_pfn
, unsigned long end_pfn
)
305 unsigned long z1_start_pfn
;
307 ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
309 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
311 /* can't move pfns which are higher than @z2 */
312 if (end_pfn
> zone_end_pfn(z2
))
314 /* the move out part must be at the left most of @z2 */
315 if (start_pfn
> z2
->zone_start_pfn
)
317 /* must included/overlap */
318 if (end_pfn
<= z2
->zone_start_pfn
)
321 /* use start_pfn for z1's start_pfn if z1 is empty */
322 if (!zone_is_empty(z1
))
323 z1_start_pfn
= z1
->zone_start_pfn
;
325 z1_start_pfn
= start_pfn
;
327 resize_zone(z1
, z1_start_pfn
, end_pfn
);
328 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
330 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
332 fix_zone_id(z1
, start_pfn
, end_pfn
);
336 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
340 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
341 unsigned long start_pfn
, unsigned long end_pfn
)
344 unsigned long z2_end_pfn
;
346 ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
348 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
350 /* can't move pfns which are lower than @z1 */
351 if (z1
->zone_start_pfn
> start_pfn
)
353 /* the move out part mast at the right most of @z1 */
354 if (zone_end_pfn(z1
) > end_pfn
)
356 /* must included/overlap */
357 if (start_pfn
>= zone_end_pfn(z1
))
360 /* use end_pfn for z2's end_pfn if z2 is empty */
361 if (!zone_is_empty(z2
))
362 z2_end_pfn
= zone_end_pfn(z2
);
364 z2_end_pfn
= end_pfn
;
366 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
367 resize_zone(z2
, start_pfn
, z2_end_pfn
);
369 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
371 fix_zone_id(z2
, start_pfn
, end_pfn
);
375 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
379 static struct zone
* __meminit
move_pfn_range(int zone_shift
,
380 unsigned long start_pfn
, unsigned long end_pfn
)
382 struct zone
*zone
= page_zone(pfn_to_page(start_pfn
));
386 ret
= move_pfn_range_left(zone
+ zone_shift
, zone
,
389 ret
= move_pfn_range_right(zone
, zone
+ zone_shift
,
395 return zone
+ zone_shift
;
398 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
399 unsigned long end_pfn
)
401 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
403 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
404 pgdat
->node_start_pfn
= start_pfn
;
406 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
407 pgdat
->node_start_pfn
;
410 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
412 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
413 int nr_pages
= PAGES_PER_SECTION
;
414 int nid
= pgdat
->node_id
;
416 unsigned long flags
, pfn
;
418 zone_type
= zone
- pgdat
->node_zones
;
419 ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
421 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
422 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
423 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
424 phys_start_pfn
+ nr_pages
);
425 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
426 memmap_init_zone(nr_pages
, nid
, zone_type
,
427 phys_start_pfn
, MEMMAP_HOTPLUG
);
429 /* online_page_range is called later and expects pages reserved */
430 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
434 SetPageReserved(pfn_to_page(pfn
));
439 static int __meminit
__add_section(int nid
, struct zone
*zone
,
440 unsigned long phys_start_pfn
, bool want_memblock
)
444 if (pfn_valid(phys_start_pfn
))
447 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
452 ret
= __add_zone(zone
, phys_start_pfn
);
460 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
464 * Reasonably generic function for adding memory. It is
465 * expected that archs that support memory hotplug will
466 * call this function after deciding the zone to which to
469 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
470 unsigned long nr_pages
, bool want_memblock
)
474 int start_sec
, end_sec
;
475 struct vmem_altmap
*altmap
;
477 clear_zone_contiguous(zone
);
479 /* during initialize mem_map, align hot-added range to section */
480 start_sec
= pfn_to_section_nr(phys_start_pfn
);
481 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
483 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
486 * Validate altmap is within bounds of the total request
488 if (altmap
->base_pfn
!= phys_start_pfn
489 || vmem_altmap_offset(altmap
) > nr_pages
) {
490 pr_warn_once("memory add fail, invalid altmap\n");
497 for (i
= start_sec
; i
<= end_sec
; i
++) {
498 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
), want_memblock
);
501 * EEXIST is finally dealt with by ioresource collision
502 * check. see add_memory() => register_memory_resource()
503 * Warning will be printed if there is collision.
505 if (err
&& (err
!= -EEXIST
))
509 vmemmap_populate_print_last();
511 set_zone_contiguous(zone
);
514 EXPORT_SYMBOL_GPL(__add_pages
);
516 #ifdef CONFIG_MEMORY_HOTREMOVE
517 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
518 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
519 unsigned long start_pfn
,
520 unsigned long end_pfn
)
522 struct mem_section
*ms
;
524 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
525 ms
= __pfn_to_section(start_pfn
);
527 if (unlikely(!valid_section(ms
)))
530 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
533 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
542 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
543 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
544 unsigned long start_pfn
,
545 unsigned long end_pfn
)
547 struct mem_section
*ms
;
550 /* pfn is the end pfn of a memory section. */
552 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
553 ms
= __pfn_to_section(pfn
);
555 if (unlikely(!valid_section(ms
)))
558 if (unlikely(pfn_to_nid(pfn
) != nid
))
561 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
570 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
571 unsigned long end_pfn
)
573 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
574 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
575 unsigned long zone_end_pfn
= z
;
577 struct mem_section
*ms
;
578 int nid
= zone_to_nid(zone
);
580 zone_span_writelock(zone
);
581 if (zone_start_pfn
== start_pfn
) {
583 * If the section is smallest section in the zone, it need
584 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
585 * In this case, we find second smallest valid mem_section
586 * for shrinking zone.
588 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
591 zone
->zone_start_pfn
= pfn
;
592 zone
->spanned_pages
= zone_end_pfn
- pfn
;
594 } else if (zone_end_pfn
== end_pfn
) {
596 * If the section is biggest section in the zone, it need
597 * shrink zone->spanned_pages.
598 * In this case, we find second biggest valid mem_section for
601 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
604 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
608 * The section is not biggest or smallest mem_section in the zone, it
609 * only creates a hole in the zone. So in this case, we need not
610 * change the zone. But perhaps, the zone has only hole data. Thus
611 * it check the zone has only hole or not.
613 pfn
= zone_start_pfn
;
614 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
615 ms
= __pfn_to_section(pfn
);
617 if (unlikely(!valid_section(ms
)))
620 if (page_zone(pfn_to_page(pfn
)) != zone
)
623 /* If the section is current section, it continues the loop */
624 if (start_pfn
== pfn
)
627 /* If we find valid section, we have nothing to do */
628 zone_span_writeunlock(zone
);
632 /* The zone has no valid section */
633 zone
->zone_start_pfn
= 0;
634 zone
->spanned_pages
= 0;
635 zone_span_writeunlock(zone
);
638 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
639 unsigned long start_pfn
, unsigned long end_pfn
)
641 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
642 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
643 unsigned long pgdat_end_pfn
= p
;
645 struct mem_section
*ms
;
646 int nid
= pgdat
->node_id
;
648 if (pgdat_start_pfn
== start_pfn
) {
650 * If the section is smallest section in the pgdat, it need
651 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
652 * In this case, we find second smallest valid mem_section
653 * for shrinking zone.
655 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
658 pgdat
->node_start_pfn
= pfn
;
659 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
661 } else if (pgdat_end_pfn
== end_pfn
) {
663 * If the section is biggest section in the pgdat, it need
664 * shrink pgdat->node_spanned_pages.
665 * In this case, we find second biggest valid mem_section for
668 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
671 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
675 * If the section is not biggest or smallest mem_section in the pgdat,
676 * it only creates a hole in the pgdat. So in this case, we need not
678 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
679 * has only hole or not.
681 pfn
= pgdat_start_pfn
;
682 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
683 ms
= __pfn_to_section(pfn
);
685 if (unlikely(!valid_section(ms
)))
688 if (pfn_to_nid(pfn
) != nid
)
691 /* If the section is current section, it continues the loop */
692 if (start_pfn
== pfn
)
695 /* If we find valid section, we have nothing to do */
699 /* The pgdat has no valid section */
700 pgdat
->node_start_pfn
= 0;
701 pgdat
->node_spanned_pages
= 0;
704 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
706 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
707 int nr_pages
= PAGES_PER_SECTION
;
710 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
711 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
712 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
713 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
716 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
717 unsigned long map_offset
)
719 unsigned long start_pfn
;
723 if (!valid_section(ms
))
726 ret
= unregister_memory_section(ms
);
730 scn_nr
= __section_nr(ms
);
731 start_pfn
= section_nr_to_pfn(scn_nr
);
732 __remove_zone(zone
, start_pfn
);
734 sparse_remove_one_section(zone
, ms
, map_offset
);
739 * __remove_pages() - remove sections of pages from a zone
740 * @zone: zone from which pages need to be removed
741 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
742 * @nr_pages: number of pages to remove (must be multiple of section size)
744 * Generic helper function to remove section mappings and sysfs entries
745 * for the section of the memory we are removing. Caller needs to make
746 * sure that pages are marked reserved and zones are adjust properly by
747 * calling offline_pages().
749 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
750 unsigned long nr_pages
)
753 unsigned long map_offset
= 0;
754 int sections_to_remove
, ret
= 0;
756 /* In the ZONE_DEVICE case device driver owns the memory region */
757 if (is_dev_zone(zone
)) {
758 struct page
*page
= pfn_to_page(phys_start_pfn
);
759 struct vmem_altmap
*altmap
;
761 altmap
= to_vmem_altmap((unsigned long) page
);
763 map_offset
= vmem_altmap_offset(altmap
);
765 resource_size_t start
, size
;
767 start
= phys_start_pfn
<< PAGE_SHIFT
;
768 size
= nr_pages
* PAGE_SIZE
;
770 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
773 resource_size_t endres
= start
+ size
- 1;
775 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
776 &start
, &endres
, ret
);
780 clear_zone_contiguous(zone
);
783 * We can only remove entire sections
785 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
786 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
788 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
789 for (i
= 0; i
< sections_to_remove
; i
++) {
790 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
792 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
798 set_zone_contiguous(zone
);
802 #endif /* CONFIG_MEMORY_HOTREMOVE */
804 int set_online_page_callback(online_page_callback_t callback
)
809 mutex_lock(&online_page_callback_lock
);
811 if (online_page_callback
== generic_online_page
) {
812 online_page_callback
= callback
;
816 mutex_unlock(&online_page_callback_lock
);
821 EXPORT_SYMBOL_GPL(set_online_page_callback
);
823 int restore_online_page_callback(online_page_callback_t callback
)
828 mutex_lock(&online_page_callback_lock
);
830 if (online_page_callback
== callback
) {
831 online_page_callback
= generic_online_page
;
835 mutex_unlock(&online_page_callback_lock
);
840 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
842 void __online_page_set_limits(struct page
*page
)
845 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
847 void __online_page_increment_counters(struct page
*page
)
849 adjust_managed_page_count(page
, 1);
851 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
853 void __online_page_free(struct page
*page
)
855 __free_reserved_page(page
);
857 EXPORT_SYMBOL_GPL(__online_page_free
);
859 static void generic_online_page(struct page
*page
)
861 __online_page_set_limits(page
);
862 __online_page_increment_counters(page
);
863 __online_page_free(page
);
866 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
870 unsigned long onlined_pages
= *(unsigned long *)arg
;
873 if (PageReserved(pfn_to_page(start_pfn
)))
874 for (i
= 0; i
< nr_pages
; i
++) {
875 page
= pfn_to_page(start_pfn
+ i
);
876 (*online_page_callback
)(page
);
880 online_mem_sections(start_pfn
, start_pfn
+ nr_pages
);
882 *(unsigned long *)arg
= onlined_pages
;
886 #ifdef CONFIG_MOVABLE_NODE
888 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
891 static bool can_online_high_movable(int nid
)
895 #else /* CONFIG_MOVABLE_NODE */
896 /* ensure every online node has NORMAL memory */
897 static bool can_online_high_movable(int nid
)
899 return node_state(nid
, N_NORMAL_MEMORY
);
901 #endif /* CONFIG_MOVABLE_NODE */
903 /* check which state of node_states will be changed when online memory */
904 static void node_states_check_changes_online(unsigned long nr_pages
,
905 struct zone
*zone
, struct memory_notify
*arg
)
907 int nid
= zone_to_nid(zone
);
908 enum zone_type zone_last
= ZONE_NORMAL
;
911 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
912 * contains nodes which have zones of 0...ZONE_NORMAL,
913 * set zone_last to ZONE_NORMAL.
915 * If we don't have HIGHMEM nor movable node,
916 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
917 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
919 if (N_MEMORY
== N_NORMAL_MEMORY
)
920 zone_last
= ZONE_MOVABLE
;
923 * if the memory to be online is in a zone of 0...zone_last, and
924 * the zones of 0...zone_last don't have memory before online, we will
925 * need to set the node to node_states[N_NORMAL_MEMORY] after
926 * the memory is online.
928 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
929 arg
->status_change_nid_normal
= nid
;
931 arg
->status_change_nid_normal
= -1;
933 #ifdef CONFIG_HIGHMEM
935 * If we have movable node, node_states[N_HIGH_MEMORY]
936 * contains nodes which have zones of 0...ZONE_HIGHMEM,
937 * set zone_last to ZONE_HIGHMEM.
939 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
940 * contains nodes which have zones of 0...ZONE_MOVABLE,
941 * set zone_last to ZONE_MOVABLE.
943 zone_last
= ZONE_HIGHMEM
;
944 if (N_MEMORY
== N_HIGH_MEMORY
)
945 zone_last
= ZONE_MOVABLE
;
947 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
948 arg
->status_change_nid_high
= nid
;
950 arg
->status_change_nid_high
= -1;
952 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
956 * if the node don't have memory befor online, we will need to
957 * set the node to node_states[N_MEMORY] after the memory
960 if (!node_state(nid
, N_MEMORY
))
961 arg
->status_change_nid
= nid
;
963 arg
->status_change_nid
= -1;
966 static void node_states_set_node(int node
, struct memory_notify
*arg
)
968 if (arg
->status_change_nid_normal
>= 0)
969 node_set_state(node
, N_NORMAL_MEMORY
);
971 if (arg
->status_change_nid_high
>= 0)
972 node_set_state(node
, N_HIGH_MEMORY
);
974 node_set_state(node
, N_MEMORY
);
977 bool zone_can_shift(unsigned long pfn
, unsigned long nr_pages
,
978 enum zone_type target
, int *zone_shift
)
980 struct zone
*zone
= page_zone(pfn_to_page(pfn
));
981 enum zone_type idx
= zone_idx(zone
);
987 /* pages must be at end of current zone */
988 if (pfn
+ nr_pages
!= zone_end_pfn(zone
))
991 /* no zones in use between current zone and target */
992 for (i
= idx
+ 1; i
< target
; i
++)
993 if (zone_is_initialized(zone
- idx
+ i
))
998 /* pages must be at beginning of current zone */
999 if (pfn
!= zone
->zone_start_pfn
)
1002 /* no zones in use between current zone and target */
1003 for (i
= target
+ 1; i
< idx
; i
++)
1004 if (zone_is_initialized(zone
- idx
+ i
))
1008 *zone_shift
= target
- idx
;
1012 /* Must be protected by mem_hotplug_begin() */
1013 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
1015 unsigned long flags
;
1016 unsigned long onlined_pages
= 0;
1018 int need_zonelists_rebuild
= 0;
1021 struct memory_notify arg
;
1025 * This doesn't need a lock to do pfn_to_page().
1026 * The section can't be removed here because of the
1027 * memory_block->state_mutex.
1029 zone
= page_zone(pfn_to_page(pfn
));
1031 if ((zone_idx(zone
) > ZONE_NORMAL
||
1032 online_type
== MMOP_ONLINE_MOVABLE
) &&
1033 !can_online_high_movable(pfn_to_nid(pfn
)))
1036 if (online_type
== MMOP_ONLINE_KERNEL
) {
1037 if (!zone_can_shift(pfn
, nr_pages
, ZONE_NORMAL
, &zone_shift
))
1039 } else if (online_type
== MMOP_ONLINE_MOVABLE
) {
1040 if (!zone_can_shift(pfn
, nr_pages
, ZONE_MOVABLE
, &zone_shift
))
1044 zone
= move_pfn_range(zone_shift
, pfn
, pfn
+ nr_pages
);
1048 arg
.start_pfn
= pfn
;
1049 arg
.nr_pages
= nr_pages
;
1050 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1052 nid
= zone_to_nid(zone
);
1054 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1055 ret
= notifier_to_errno(ret
);
1057 goto failed_addition
;
1060 * If this zone is not populated, then it is not in zonelist.
1061 * This means the page allocator ignores this zone.
1062 * So, zonelist must be updated after online.
1064 mutex_lock(&zonelists_mutex
);
1065 if (!populated_zone(zone
)) {
1066 need_zonelists_rebuild
= 1;
1067 build_all_zonelists(NULL
, zone
);
1070 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1071 online_pages_range
);
1073 if (need_zonelists_rebuild
)
1074 zone_pcp_reset(zone
);
1075 mutex_unlock(&zonelists_mutex
);
1076 goto failed_addition
;
1079 zone
->present_pages
+= onlined_pages
;
1081 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1082 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1083 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1085 if (onlined_pages
) {
1086 node_states_set_node(nid
, &arg
);
1087 if (need_zonelists_rebuild
)
1088 build_all_zonelists(NULL
, NULL
);
1090 zone_pcp_update(zone
);
1093 mutex_unlock(&zonelists_mutex
);
1095 init_per_zone_wmark_min();
1097 if (onlined_pages
) {
1102 vm_total_pages
= nr_free_pagecache_pages();
1104 writeback_set_ratelimit();
1107 memory_notify(MEM_ONLINE
, &arg
);
1111 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1112 (unsigned long long) pfn
<< PAGE_SHIFT
,
1113 (((unsigned long long) pfn
+ nr_pages
) << PAGE_SHIFT
) - 1);
1114 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1117 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1119 static void reset_node_present_pages(pg_data_t
*pgdat
)
1123 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1124 z
->present_pages
= 0;
1126 pgdat
->node_present_pages
= 0;
1129 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1130 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1132 struct pglist_data
*pgdat
;
1133 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1134 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1135 unsigned long start_pfn
= PFN_DOWN(start
);
1137 pgdat
= NODE_DATA(nid
);
1139 pgdat
= arch_alloc_nodedata(nid
);
1143 arch_refresh_nodedata(nid
, pgdat
);
1146 * Reset the nr_zones, order and classzone_idx before reuse.
1147 * Note that kswapd will init kswapd_classzone_idx properly
1148 * when it starts in the near future.
1150 pgdat
->nr_zones
= 0;
1151 pgdat
->kswapd_order
= 0;
1152 pgdat
->kswapd_classzone_idx
= 0;
1155 /* we can use NODE_DATA(nid) from here */
1157 /* init node's zones as empty zones, we don't have any present pages.*/
1158 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1159 pgdat
->per_cpu_nodestats
= alloc_percpu(struct per_cpu_nodestat
);
1162 * The node we allocated has no zone fallback lists. For avoiding
1163 * to access not-initialized zonelist, build here.
1165 mutex_lock(&zonelists_mutex
);
1166 build_all_zonelists(pgdat
, NULL
);
1167 mutex_unlock(&zonelists_mutex
);
1170 * zone->managed_pages is set to an approximate value in
1171 * free_area_init_core(), which will cause
1172 * /sys/device/system/node/nodeX/meminfo has wrong data.
1173 * So reset it to 0 before any memory is onlined.
1175 reset_node_managed_pages(pgdat
);
1178 * When memory is hot-added, all the memory is in offline state. So
1179 * clear all zones' present_pages because they will be updated in
1180 * online_pages() and offline_pages().
1182 reset_node_present_pages(pgdat
);
1187 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1189 arch_refresh_nodedata(nid
, NULL
);
1190 free_percpu(pgdat
->per_cpu_nodestats
);
1191 arch_free_nodedata(pgdat
);
1197 * try_online_node - online a node if offlined
1199 * called by cpu_up() to online a node without onlined memory.
1201 int try_online_node(int nid
)
1206 if (node_online(nid
))
1209 mem_hotplug_begin();
1210 pgdat
= hotadd_new_pgdat(nid
, 0);
1212 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1216 node_set_online(nid
);
1217 ret
= register_one_node(nid
);
1220 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1221 mutex_lock(&zonelists_mutex
);
1222 build_all_zonelists(NULL
, NULL
);
1223 mutex_unlock(&zonelists_mutex
);
1231 static int check_hotplug_memory_range(u64 start
, u64 size
)
1233 u64 start_pfn
= PFN_DOWN(start
);
1234 u64 nr_pages
= size
>> PAGE_SHIFT
;
1236 /* Memory range must be aligned with section */
1237 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1238 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1239 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1240 (unsigned long long)start
,
1241 (unsigned long long)size
);
1249 * If movable zone has already been setup, newly added memory should be check.
1250 * If its address is higher than movable zone, it should be added as movable.
1251 * Without this check, movable zone may overlap with other zone.
1253 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1255 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1256 pg_data_t
*pgdat
= NODE_DATA(nid
);
1257 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1259 if (zone_is_empty(movable_zone
))
1262 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1268 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
,
1271 #ifdef CONFIG_ZONE_DEVICE
1275 if (should_add_memory_movable(nid
, start
, size
))
1276 return ZONE_MOVABLE
;
1278 return zone_default
;
1281 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1283 return device_online(&mem
->dev
);
1286 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1287 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1290 pg_data_t
*pgdat
= NULL
;
1296 size
= resource_size(res
);
1298 ret
= check_hotplug_memory_range(start
, size
);
1302 { /* Stupid hack to suppress address-never-null warning */
1303 void *p
= NODE_DATA(nid
);
1307 mem_hotplug_begin();
1310 * Add new range to memblock so that when hotadd_new_pgdat() is called
1311 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1312 * this new range and calculate total pages correctly. The range will
1313 * be removed at hot-remove time.
1315 memblock_add_node(start
, size
, nid
);
1317 new_node
= !node_online(nid
);
1319 pgdat
= hotadd_new_pgdat(nid
, start
);
1325 /* call arch's memory hotadd */
1326 ret
= arch_add_memory(nid
, start
, size
, true);
1331 /* we online node here. we can't roll back from here. */
1332 node_set_online(nid
);
1335 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1336 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1338 ret
= __register_one_node(nid
);
1343 * link memory sections under this node. This is already
1344 * done when creatig memory section in register_new_memory
1345 * but that depends to have the node registered so offline
1346 * nodes have to go through register_node.
1347 * TODO clean up this mess.
1349 ret
= link_mem_sections(nid
, start_pfn
, nr_pages
);
1352 * If sysfs file of new node can't create, cpu on the node
1353 * can't be hot-added. There is no rollback way now.
1354 * So, check by BUG_ON() to catch it reluctantly..
1359 /* create new memmap entry */
1360 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1362 /* online pages if requested */
1364 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1365 NULL
, online_memory_block
);
1370 /* rollback pgdat allocation and others */
1371 if (new_pgdat
&& pgdat
)
1372 rollback_node_hotadd(nid
, pgdat
);
1373 memblock_remove(start
, size
);
1379 EXPORT_SYMBOL_GPL(add_memory_resource
);
1381 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1383 struct resource
*res
;
1386 res
= register_memory_resource(start
, size
);
1388 return PTR_ERR(res
);
1390 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1392 release_memory_resource(res
);
1395 EXPORT_SYMBOL_GPL(add_memory
);
1397 #ifdef CONFIG_MEMORY_HOTREMOVE
1399 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1400 * set and the size of the free page is given by page_order(). Using this,
1401 * the function determines if the pageblock contains only free pages.
1402 * Due to buddy contraints, a free page at least the size of a pageblock will
1403 * be located at the start of the pageblock
1405 static inline int pageblock_free(struct page
*page
)
1407 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1410 /* Return the start of the next active pageblock after a given page */
1411 static struct page
*next_active_pageblock(struct page
*page
)
1413 /* Ensure the starting page is pageblock-aligned */
1414 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1416 /* If the entire pageblock is free, move to the end of free page */
1417 if (pageblock_free(page
)) {
1419 /* be careful. we don't have locks, page_order can be changed.*/
1420 order
= page_order(page
);
1421 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1422 return page
+ (1 << order
);
1425 return page
+ pageblock_nr_pages
;
1428 /* Checks if this range of memory is likely to be hot-removable. */
1429 bool is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1431 struct page
*page
= pfn_to_page(start_pfn
);
1432 struct page
*end_page
= page
+ nr_pages
;
1434 /* Check the starting page of each pageblock within the range */
1435 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1436 if (!is_pageblock_removable_nolock(page
))
1441 /* All pageblocks in the memory block are likely to be hot-removable */
1446 * Confirm all pages in a range [start, end) belong to the same zone.
1447 * When true, return its valid [start, end).
1449 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
,
1450 unsigned long *valid_start
, unsigned long *valid_end
)
1452 unsigned long pfn
, sec_end_pfn
;
1453 unsigned long start
, end
;
1454 struct zone
*zone
= NULL
;
1457 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
+ 1);
1459 pfn
= sec_end_pfn
, sec_end_pfn
+= PAGES_PER_SECTION
) {
1460 /* Make sure the memory section is present first */
1461 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1463 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1464 pfn
+= MAX_ORDER_NR_PAGES
) {
1466 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1467 while ((i
< MAX_ORDER_NR_PAGES
) &&
1468 !pfn_valid_within(pfn
+ i
))
1470 if (i
== MAX_ORDER_NR_PAGES
|| pfn
+ i
>= end_pfn
)
1472 page
= pfn_to_page(pfn
+ i
);
1473 if (zone
&& page_zone(page
) != zone
)
1477 zone
= page_zone(page
);
1478 end
= pfn
+ MAX_ORDER_NR_PAGES
;
1483 *valid_start
= start
;
1484 *valid_end
= min(end
, end_pfn
);
1492 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1493 * non-lru movable pages and hugepages). We scan pfn because it's much
1494 * easier than scanning over linked list. This function returns the pfn
1495 * of the first found movable page if it's found, otherwise 0.
1497 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1501 for (pfn
= start
; pfn
< end
; pfn
++) {
1502 if (pfn_valid(pfn
)) {
1503 page
= pfn_to_page(pfn
);
1506 if (__PageMovable(page
))
1508 if (PageHuge(page
)) {
1509 if (page_huge_active(page
))
1512 pfn
= round_up(pfn
+ 1,
1513 1 << compound_order(page
)) - 1;
1520 static struct page
*new_node_page(struct page
*page
, unsigned long private,
1523 int nid
= page_to_nid(page
);
1524 nodemask_t nmask
= node_states
[N_MEMORY
];
1527 * try to allocate from a different node but reuse this node if there
1528 * are no other online nodes to be used (e.g. we are offlining a part
1529 * of the only existing node)
1531 node_clear(nid
, nmask
);
1532 if (nodes_empty(nmask
))
1533 node_set(nid
, nmask
);
1535 return new_page_nodemask(page
, nid
, &nmask
);
1538 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1540 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1544 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1545 int not_managed
= 0;
1549 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1550 if (!pfn_valid(pfn
))
1552 page
= pfn_to_page(pfn
);
1554 if (PageHuge(page
)) {
1555 struct page
*head
= compound_head(page
);
1556 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1557 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1561 if (isolate_huge_page(page
, &source
))
1562 move_pages
-= 1 << compound_order(head
);
1566 if (!get_page_unless_zero(page
))
1569 * We can skip free pages. And we can deal with pages on
1570 * LRU and non-lru movable pages.
1573 ret
= isolate_lru_page(page
);
1575 ret
= isolate_movable_page(page
, ISOLATE_UNEVICTABLE
);
1576 if (!ret
) { /* Success */
1578 list_add_tail(&page
->lru
, &source
);
1580 if (!__PageMovable(page
))
1581 inc_node_page_state(page
, NR_ISOLATED_ANON
+
1582 page_is_file_cache(page
));
1585 #ifdef CONFIG_DEBUG_VM
1586 pr_alert("failed to isolate pfn %lx\n", pfn
);
1587 dump_page(page
, "isolation failed");
1590 /* Because we don't have big zone->lock. we should
1591 check this again here. */
1592 if (page_count(page
)) {
1599 if (!list_empty(&source
)) {
1601 putback_movable_pages(&source
);
1605 /* Allocate a new page from the nearest neighbor node */
1606 ret
= migrate_pages(&source
, new_node_page
, NULL
, 0,
1607 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1609 putback_movable_pages(&source
);
1616 * remove from free_area[] and mark all as Reserved.
1619 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1622 __offline_isolated_pages(start
, start
+ nr_pages
);
1627 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1629 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1630 offline_isolated_pages_cb
);
1634 * Check all pages in range, recoreded as memory resource, are isolated.
1637 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1641 long offlined
= *(long *)data
;
1642 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1643 offlined
= nr_pages
;
1645 *(long *)data
+= offlined
;
1650 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1655 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1656 check_pages_isolated_cb
);
1658 offlined
= (long)ret
;
1662 static int __init
cmdline_parse_movable_node(char *p
)
1664 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1665 movable_node_enabled
= true;
1667 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1671 early_param("movable_node", cmdline_parse_movable_node
);
1673 /* check which state of node_states will be changed when offline memory */
1674 static void node_states_check_changes_offline(unsigned long nr_pages
,
1675 struct zone
*zone
, struct memory_notify
*arg
)
1677 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1678 unsigned long present_pages
= 0;
1679 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1682 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1683 * contains nodes which have zones of 0...ZONE_NORMAL,
1684 * set zone_last to ZONE_NORMAL.
1686 * If we don't have HIGHMEM nor movable node,
1687 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1688 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1690 if (N_MEMORY
== N_NORMAL_MEMORY
)
1691 zone_last
= ZONE_MOVABLE
;
1694 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1695 * If the memory to be offline is in a zone of 0...zone_last,
1696 * and it is the last present memory, 0...zone_last will
1697 * become empty after offline , thus we can determind we will
1698 * need to clear the node from node_states[N_NORMAL_MEMORY].
1700 for (zt
= 0; zt
<= zone_last
; zt
++)
1701 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1702 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1703 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1705 arg
->status_change_nid_normal
= -1;
1707 #ifdef CONFIG_HIGHMEM
1709 * If we have movable node, node_states[N_HIGH_MEMORY]
1710 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1711 * set zone_last to ZONE_HIGHMEM.
1713 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1714 * contains nodes which have zones of 0...ZONE_MOVABLE,
1715 * set zone_last to ZONE_MOVABLE.
1717 zone_last
= ZONE_HIGHMEM
;
1718 if (N_MEMORY
== N_HIGH_MEMORY
)
1719 zone_last
= ZONE_MOVABLE
;
1721 for (; zt
<= zone_last
; zt
++)
1722 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1723 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1724 arg
->status_change_nid_high
= zone_to_nid(zone
);
1726 arg
->status_change_nid_high
= -1;
1728 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1732 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1734 zone_last
= ZONE_MOVABLE
;
1737 * check whether node_states[N_HIGH_MEMORY] will be changed
1738 * If we try to offline the last present @nr_pages from the node,
1739 * we can determind we will need to clear the node from
1740 * node_states[N_HIGH_MEMORY].
1742 for (; zt
<= zone_last
; zt
++)
1743 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1744 if (nr_pages
>= present_pages
)
1745 arg
->status_change_nid
= zone_to_nid(zone
);
1747 arg
->status_change_nid
= -1;
1750 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1752 if (arg
->status_change_nid_normal
>= 0)
1753 node_clear_state(node
, N_NORMAL_MEMORY
);
1755 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1756 (arg
->status_change_nid_high
>= 0))
1757 node_clear_state(node
, N_HIGH_MEMORY
);
1759 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1760 (arg
->status_change_nid
>= 0))
1761 node_clear_state(node
, N_MEMORY
);
1764 static int __ref
__offline_pages(unsigned long start_pfn
,
1765 unsigned long end_pfn
, unsigned long timeout
)
1767 unsigned long pfn
, nr_pages
, expire
;
1768 long offlined_pages
;
1769 int ret
, drain
, retry_max
, node
;
1770 unsigned long flags
;
1771 unsigned long valid_start
, valid_end
;
1773 struct memory_notify arg
;
1775 /* at least, alignment against pageblock is necessary */
1776 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1778 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1780 /* This makes hotplug much easier...and readable.
1781 we assume this for now. .*/
1782 if (!test_pages_in_a_zone(start_pfn
, end_pfn
, &valid_start
, &valid_end
))
1785 zone
= page_zone(pfn_to_page(valid_start
));
1786 node
= zone_to_nid(zone
);
1787 nr_pages
= end_pfn
- start_pfn
;
1789 /* set above range as isolated */
1790 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1791 MIGRATE_MOVABLE
, true);
1795 arg
.start_pfn
= start_pfn
;
1796 arg
.nr_pages
= nr_pages
;
1797 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1799 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1800 ret
= notifier_to_errno(ret
);
1802 goto failed_removal
;
1805 expire
= jiffies
+ timeout
;
1809 /* start memory hot removal */
1811 if (time_after(jiffies
, expire
))
1812 goto failed_removal
;
1814 if (signal_pending(current
))
1815 goto failed_removal
;
1818 lru_add_drain_all_cpuslocked();
1820 drain_all_pages(zone
);
1823 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1824 if (pfn
) { /* We have movable pages */
1825 ret
= do_migrate_range(pfn
, end_pfn
);
1831 if (--retry_max
== 0)
1832 goto failed_removal
;
1838 /* drain all zone's lru pagevec, this is asynchronous... */
1839 lru_add_drain_all_cpuslocked();
1841 /* drain pcp pages, this is synchronous. */
1842 drain_all_pages(zone
);
1844 * dissolve free hugepages in the memory block before doing offlining
1845 * actually in order to make hugetlbfs's object counting consistent.
1847 ret
= dissolve_free_huge_pages(start_pfn
, end_pfn
);
1849 goto failed_removal
;
1851 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1852 if (offlined_pages
< 0) {
1854 goto failed_removal
;
1856 pr_info("Offlined Pages %ld\n", offlined_pages
);
1857 /* Ok, all of our target is isolated.
1858 We cannot do rollback at this point. */
1859 offline_isolated_pages(start_pfn
, end_pfn
);
1860 /* reset pagetype flags and makes migrate type to be MOVABLE */
1861 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1862 /* removal success */
1863 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1864 zone
->present_pages
-= offlined_pages
;
1866 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1867 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1868 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1870 init_per_zone_wmark_min();
1872 if (!populated_zone(zone
)) {
1873 zone_pcp_reset(zone
);
1874 mutex_lock(&zonelists_mutex
);
1875 build_all_zonelists(NULL
, NULL
);
1876 mutex_unlock(&zonelists_mutex
);
1878 zone_pcp_update(zone
);
1880 node_states_clear_node(node
, &arg
);
1881 if (arg
.status_change_nid
>= 0) {
1883 kcompactd_stop(node
);
1886 vm_total_pages
= nr_free_pagecache_pages();
1887 writeback_set_ratelimit();
1889 memory_notify(MEM_OFFLINE
, &arg
);
1893 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1894 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1895 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1896 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1897 /* pushback to free area */
1898 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1902 /* Must be protected by mem_hotplug_begin() */
1903 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1905 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
, 120 * HZ
);
1907 #endif /* CONFIG_MEMORY_HOTREMOVE */
1910 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1911 * @start_pfn: start pfn of the memory range
1912 * @end_pfn: end pfn of the memory range
1913 * @arg: argument passed to func
1914 * @func: callback for each memory section walked
1916 * This function walks through all present mem sections in range
1917 * [start_pfn, end_pfn) and call func on each mem section.
1919 * Returns the return value of func.
1921 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1922 void *arg
, int (*func
)(struct memory_block
*, void *))
1924 struct memory_block
*mem
= NULL
;
1925 struct mem_section
*section
;
1926 unsigned long pfn
, section_nr
;
1929 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1930 section_nr
= pfn_to_section_nr(pfn
);
1931 if (!present_section_nr(section_nr
))
1934 section
= __nr_to_section(section_nr
);
1935 /* same memblock? */
1937 if ((section_nr
>= mem
->start_section_nr
) &&
1938 (section_nr
<= mem
->end_section_nr
))
1941 mem
= find_memory_block_hinted(section
, mem
);
1945 ret
= func(mem
, arg
);
1947 kobject_put(&mem
->dev
.kobj
);
1953 kobject_put(&mem
->dev
.kobj
);
1958 #ifdef CONFIG_MEMORY_HOTREMOVE
1959 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1961 int ret
= !is_memblock_offlined(mem
);
1963 if (unlikely(ret
)) {
1964 phys_addr_t beginpa
, endpa
;
1966 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1967 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1968 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1975 static int check_cpu_on_node(pg_data_t
*pgdat
)
1979 for_each_present_cpu(cpu
) {
1980 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1982 * the cpu on this node isn't removed, and we can't
1983 * offline this node.
1991 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1993 #ifdef CONFIG_ACPI_NUMA
1996 for_each_possible_cpu(cpu
)
1997 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1998 numa_clear_node(cpu
);
2002 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
2006 ret
= check_cpu_on_node(pgdat
);
2011 * the node will be offlined when we come here, so we can clear
2012 * the cpu_to_node() now.
2015 unmap_cpu_on_node(pgdat
);
2022 * Offline a node if all memory sections and cpus of the node are removed.
2024 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2025 * and online/offline operations before this call.
2027 void try_offline_node(int nid
)
2029 pg_data_t
*pgdat
= NODE_DATA(nid
);
2030 unsigned long start_pfn
= pgdat
->node_start_pfn
;
2031 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
2034 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2035 unsigned long section_nr
= pfn_to_section_nr(pfn
);
2037 if (!present_section_nr(section_nr
))
2040 if (pfn_to_nid(pfn
) != nid
)
2044 * some memory sections of this node are not removed, and we
2045 * can't offline node now.
2050 if (check_and_unmap_cpu_on_node(pgdat
))
2054 * all memory/cpu of this node are removed, we can offline this
2057 node_set_offline(nid
);
2058 unregister_one_node(nid
);
2060 EXPORT_SYMBOL(try_offline_node
);
2065 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2066 * and online/offline operations before this call, as required by
2067 * try_offline_node().
2069 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2073 BUG_ON(check_hotplug_memory_range(start
, size
));
2075 mem_hotplug_begin();
2078 * All memory blocks must be offlined before removing memory. Check
2079 * whether all memory blocks in question are offline and trigger a BUG()
2080 * if this is not the case.
2082 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2083 check_memblock_offlined_cb
);
2087 /* remove memmap entry */
2088 firmware_map_remove(start
, start
+ size
, "System RAM");
2089 memblock_free(start
, size
);
2090 memblock_remove(start
, size
);
2092 arch_remove_memory(start
, size
);
2094 try_offline_node(nid
);
2098 EXPORT_SYMBOL_GPL(remove_memory
);
2099 #endif /* CONFIG_MEMORY_HOTREMOVE */