2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/swap.h>
10 #include <linux/interrupt.h>
11 #include <linux/pagemap.h>
12 #include <linux/compiler.h>
13 #include <linux/export.h>
14 #include <linux/pagevec.h>
15 #include <linux/writeback.h>
16 #include <linux/slab.h>
17 #include <linux/sysctl.h>
18 #include <linux/cpu.h>
19 #include <linux/memory.h>
20 #include <linux/memremap.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/highmem.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
32 #include <linux/stop_machine.h>
33 #include <linux/hugetlb.h>
34 #include <linux/memblock.h>
35 #include <linux/bootmem.h>
37 #include <asm/tlbflush.h>
42 * online_page_callback contains pointer to current page onlining function.
43 * Initially it is generic_online_page(). If it is required it could be
44 * changed by calling set_online_page_callback() for callback registration
45 * and restore_online_page_callback() for generic callback restore.
48 static void generic_online_page(struct page
*page
);
50 static online_page_callback_t online_page_callback
= generic_online_page
;
51 static DEFINE_MUTEX(online_page_callback_lock
);
53 /* The same as the cpu_hotplug lock, but for memory hotplug. */
55 struct task_struct
*active_writer
;
56 struct mutex lock
; /* Synchronizes accesses to refcount, */
58 * Also blocks the new readers during
59 * an ongoing mem hotplug operation.
63 #ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map
;
67 .active_writer
= NULL
,
68 .lock
= __MUTEX_INITIALIZER(mem_hotplug
.lock
),
70 #ifdef CONFIG_DEBUG_LOCK_ALLOC
71 .dep_map
= {.name
= "mem_hotplug.lock" },
75 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
76 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
77 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
78 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
80 bool memhp_auto_online
;
81 EXPORT_SYMBOL_GPL(memhp_auto_online
);
83 void get_online_mems(void)
86 if (mem_hotplug
.active_writer
== current
)
88 memhp_lock_acquire_read();
89 mutex_lock(&mem_hotplug
.lock
);
90 mem_hotplug
.refcount
++;
91 mutex_unlock(&mem_hotplug
.lock
);
95 void put_online_mems(void)
97 if (mem_hotplug
.active_writer
== current
)
99 mutex_lock(&mem_hotplug
.lock
);
101 if (WARN_ON(!mem_hotplug
.refcount
))
102 mem_hotplug
.refcount
++; /* try to fix things up */
104 if (!--mem_hotplug
.refcount
&& unlikely(mem_hotplug
.active_writer
))
105 wake_up_process(mem_hotplug
.active_writer
);
106 mutex_unlock(&mem_hotplug
.lock
);
107 memhp_lock_release();
111 void mem_hotplug_begin(void)
113 mem_hotplug
.active_writer
= current
;
115 memhp_lock_acquire();
117 mutex_lock(&mem_hotplug
.lock
);
118 if (likely(!mem_hotplug
.refcount
))
120 __set_current_state(TASK_UNINTERRUPTIBLE
);
121 mutex_unlock(&mem_hotplug
.lock
);
126 void mem_hotplug_done(void)
128 mem_hotplug
.active_writer
= NULL
;
129 mutex_unlock(&mem_hotplug
.lock
);
130 memhp_lock_release();
133 /* add this memory to iomem resource */
134 static struct resource
*register_memory_resource(u64 start
, u64 size
)
136 struct resource
*res
;
137 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
139 return ERR_PTR(-ENOMEM
);
141 res
->name
= "System RAM";
143 res
->end
= start
+ size
- 1;
144 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
145 if (request_resource(&iomem_resource
, res
) < 0) {
146 pr_debug("System RAM resource %pR cannot be added\n", res
);
148 return ERR_PTR(-EEXIST
);
153 static void release_memory_resource(struct resource
*res
)
157 release_resource(res
);
162 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
163 void get_page_bootmem(unsigned long info
, struct page
*page
,
166 page
->lru
.next
= (struct list_head
*) type
;
167 SetPagePrivate(page
);
168 set_page_private(page
, info
);
169 atomic_inc(&page
->_count
);
172 void put_page_bootmem(struct page
*page
)
176 type
= (unsigned long) page
->lru
.next
;
177 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
178 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
180 if (atomic_dec_return(&page
->_count
) == 1) {
181 ClearPagePrivate(page
);
182 set_page_private(page
, 0);
183 INIT_LIST_HEAD(&page
->lru
);
184 free_reserved_page(page
);
188 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
189 #ifndef CONFIG_SPARSEMEM_VMEMMAP
190 static void register_page_bootmem_info_section(unsigned long start_pfn
)
192 unsigned long *usemap
, mapsize
, section_nr
, i
;
193 struct mem_section
*ms
;
194 struct page
*page
, *memmap
;
196 section_nr
= pfn_to_section_nr(start_pfn
);
197 ms
= __nr_to_section(section_nr
);
199 /* Get section's memmap address */
200 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
203 * Get page for the memmap's phys address
204 * XXX: need more consideration for sparse_vmemmap...
206 page
= virt_to_page(memmap
);
207 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
208 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
210 /* remember memmap's page */
211 for (i
= 0; i
< mapsize
; i
++, page
++)
212 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
214 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
215 page
= virt_to_page(usemap
);
217 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
219 for (i
= 0; i
< mapsize
; i
++, page
++)
220 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
223 #else /* CONFIG_SPARSEMEM_VMEMMAP */
224 static void register_page_bootmem_info_section(unsigned long start_pfn
)
226 unsigned long *usemap
, mapsize
, section_nr
, i
;
227 struct mem_section
*ms
;
228 struct page
*page
, *memmap
;
230 if (!pfn_valid(start_pfn
))
233 section_nr
= pfn_to_section_nr(start_pfn
);
234 ms
= __nr_to_section(section_nr
);
236 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
238 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
240 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
241 page
= virt_to_page(usemap
);
243 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
245 for (i
= 0; i
< mapsize
; i
++, page
++)
246 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
248 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
250 void register_page_bootmem_info_node(struct pglist_data
*pgdat
)
252 unsigned long i
, pfn
, end_pfn
, nr_pages
;
253 int node
= pgdat
->node_id
;
257 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
258 page
= virt_to_page(pgdat
);
260 for (i
= 0; i
< nr_pages
; i
++, page
++)
261 get_page_bootmem(node
, page
, NODE_INFO
);
263 zone
= &pgdat
->node_zones
[0];
264 for (; zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1; zone
++) {
265 if (zone_is_initialized(zone
)) {
266 nr_pages
= zone
->wait_table_hash_nr_entries
267 * sizeof(wait_queue_head_t
);
268 nr_pages
= PAGE_ALIGN(nr_pages
) >> PAGE_SHIFT
;
269 page
= virt_to_page(zone
->wait_table
);
271 for (i
= 0; i
< nr_pages
; i
++, page
++)
272 get_page_bootmem(node
, page
, NODE_INFO
);
276 pfn
= pgdat
->node_start_pfn
;
277 end_pfn
= pgdat_end_pfn(pgdat
);
279 /* register section info */
280 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
282 * Some platforms can assign the same pfn to multiple nodes - on
283 * node0 as well as nodeN. To avoid registering a pfn against
284 * multiple nodes we check that this pfn does not already
285 * reside in some other nodes.
287 if (pfn_valid(pfn
) && (pfn_to_nid(pfn
) == node
))
288 register_page_bootmem_info_section(pfn
);
291 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
293 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
294 unsigned long end_pfn
)
296 unsigned long old_zone_end_pfn
;
298 zone_span_writelock(zone
);
300 old_zone_end_pfn
= zone_end_pfn(zone
);
301 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
302 zone
->zone_start_pfn
= start_pfn
;
304 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
305 zone
->zone_start_pfn
;
307 zone_span_writeunlock(zone
);
310 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
311 unsigned long end_pfn
)
313 zone_span_writelock(zone
);
315 if (end_pfn
- start_pfn
) {
316 zone
->zone_start_pfn
= start_pfn
;
317 zone
->spanned_pages
= end_pfn
- start_pfn
;
320 * make it consist as free_area_init_core(),
321 * if spanned_pages = 0, then keep start_pfn = 0
323 zone
->zone_start_pfn
= 0;
324 zone
->spanned_pages
= 0;
327 zone_span_writeunlock(zone
);
330 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
331 unsigned long end_pfn
)
333 enum zone_type zid
= zone_idx(zone
);
334 int nid
= zone
->zone_pgdat
->node_id
;
337 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
338 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
341 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
342 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
343 static int __ref
ensure_zone_is_initialized(struct zone
*zone
,
344 unsigned long start_pfn
, unsigned long num_pages
)
346 if (!zone_is_initialized(zone
))
347 return init_currently_empty_zone(zone
, start_pfn
, num_pages
);
352 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
353 unsigned long start_pfn
, unsigned long end_pfn
)
357 unsigned long z1_start_pfn
;
359 ret
= ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
363 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
365 /* can't move pfns which are higher than @z2 */
366 if (end_pfn
> zone_end_pfn(z2
))
368 /* the move out part must be at the left most of @z2 */
369 if (start_pfn
> z2
->zone_start_pfn
)
371 /* must included/overlap */
372 if (end_pfn
<= z2
->zone_start_pfn
)
375 /* use start_pfn for z1's start_pfn if z1 is empty */
376 if (!zone_is_empty(z1
))
377 z1_start_pfn
= z1
->zone_start_pfn
;
379 z1_start_pfn
= start_pfn
;
381 resize_zone(z1
, z1_start_pfn
, end_pfn
);
382 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
384 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
386 fix_zone_id(z1
, start_pfn
, end_pfn
);
390 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
394 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
395 unsigned long start_pfn
, unsigned long end_pfn
)
399 unsigned long z2_end_pfn
;
401 ret
= ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
405 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
407 /* can't move pfns which are lower than @z1 */
408 if (z1
->zone_start_pfn
> start_pfn
)
410 /* the move out part mast at the right most of @z1 */
411 if (zone_end_pfn(z1
) > end_pfn
)
413 /* must included/overlap */
414 if (start_pfn
>= zone_end_pfn(z1
))
417 /* use end_pfn for z2's end_pfn if z2 is empty */
418 if (!zone_is_empty(z2
))
419 z2_end_pfn
= zone_end_pfn(z2
);
421 z2_end_pfn
= end_pfn
;
423 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
424 resize_zone(z2
, start_pfn
, z2_end_pfn
);
426 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
428 fix_zone_id(z2
, start_pfn
, end_pfn
);
432 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
436 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
437 unsigned long end_pfn
)
439 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
441 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
442 pgdat
->node_start_pfn
= start_pfn
;
444 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
445 pgdat
->node_start_pfn
;
448 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
450 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
451 int nr_pages
= PAGES_PER_SECTION
;
452 int nid
= pgdat
->node_id
;
454 unsigned long flags
, pfn
;
457 zone_type
= zone
- pgdat
->node_zones
;
458 ret
= ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
462 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
463 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
464 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
465 phys_start_pfn
+ nr_pages
);
466 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
467 memmap_init_zone(nr_pages
, nid
, zone_type
,
468 phys_start_pfn
, MEMMAP_HOTPLUG
);
470 /* online_page_range is called later and expects pages reserved */
471 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
475 SetPageReserved(pfn_to_page(pfn
));
480 static int __meminit
__add_section(int nid
, struct zone
*zone
,
481 unsigned long phys_start_pfn
)
485 if (pfn_valid(phys_start_pfn
))
488 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
493 ret
= __add_zone(zone
, phys_start_pfn
);
498 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
502 * Reasonably generic function for adding memory. It is
503 * expected that archs that support memory hotplug will
504 * call this function after deciding the zone to which to
507 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
508 unsigned long nr_pages
)
512 int start_sec
, end_sec
;
513 struct vmem_altmap
*altmap
;
515 clear_zone_contiguous(zone
);
517 /* during initialize mem_map, align hot-added range to section */
518 start_sec
= pfn_to_section_nr(phys_start_pfn
);
519 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
521 altmap
= to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn
));
524 * Validate altmap is within bounds of the total request
526 if (altmap
->base_pfn
!= phys_start_pfn
527 || vmem_altmap_offset(altmap
) > nr_pages
) {
528 pr_warn_once("memory add fail, invalid altmap\n");
535 for (i
= start_sec
; i
<= end_sec
; i
++) {
536 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
));
539 * EEXIST is finally dealt with by ioresource collision
540 * check. see add_memory() => register_memory_resource()
541 * Warning will be printed if there is collision.
543 if (err
&& (err
!= -EEXIST
))
547 vmemmap_populate_print_last();
549 set_zone_contiguous(zone
);
552 EXPORT_SYMBOL_GPL(__add_pages
);
554 #ifdef CONFIG_MEMORY_HOTREMOVE
555 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
556 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
557 unsigned long start_pfn
,
558 unsigned long end_pfn
)
560 struct mem_section
*ms
;
562 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
563 ms
= __pfn_to_section(start_pfn
);
565 if (unlikely(!valid_section(ms
)))
568 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
571 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
580 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
581 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
582 unsigned long start_pfn
,
583 unsigned long end_pfn
)
585 struct mem_section
*ms
;
588 /* pfn is the end pfn of a memory section. */
590 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
591 ms
= __pfn_to_section(pfn
);
593 if (unlikely(!valid_section(ms
)))
596 if (unlikely(pfn_to_nid(pfn
) != nid
))
599 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
608 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
609 unsigned long end_pfn
)
611 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
612 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
613 unsigned long zone_end_pfn
= z
;
615 struct mem_section
*ms
;
616 int nid
= zone_to_nid(zone
);
618 zone_span_writelock(zone
);
619 if (zone_start_pfn
== start_pfn
) {
621 * If the section is smallest section in the zone, it need
622 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
623 * In this case, we find second smallest valid mem_section
624 * for shrinking zone.
626 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
629 zone
->zone_start_pfn
= pfn
;
630 zone
->spanned_pages
= zone_end_pfn
- pfn
;
632 } else if (zone_end_pfn
== end_pfn
) {
634 * If the section is biggest section in the zone, it need
635 * shrink zone->spanned_pages.
636 * In this case, we find second biggest valid mem_section for
639 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
642 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
646 * The section is not biggest or smallest mem_section in the zone, it
647 * only creates a hole in the zone. So in this case, we need not
648 * change the zone. But perhaps, the zone has only hole data. Thus
649 * it check the zone has only hole or not.
651 pfn
= zone_start_pfn
;
652 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
653 ms
= __pfn_to_section(pfn
);
655 if (unlikely(!valid_section(ms
)))
658 if (page_zone(pfn_to_page(pfn
)) != zone
)
661 /* If the section is current section, it continues the loop */
662 if (start_pfn
== pfn
)
665 /* If we find valid section, we have nothing to do */
666 zone_span_writeunlock(zone
);
670 /* The zone has no valid section */
671 zone
->zone_start_pfn
= 0;
672 zone
->spanned_pages
= 0;
673 zone_span_writeunlock(zone
);
676 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
677 unsigned long start_pfn
, unsigned long end_pfn
)
679 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
680 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
681 unsigned long pgdat_end_pfn
= p
;
683 struct mem_section
*ms
;
684 int nid
= pgdat
->node_id
;
686 if (pgdat_start_pfn
== start_pfn
) {
688 * If the section is smallest section in the pgdat, it need
689 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
690 * In this case, we find second smallest valid mem_section
691 * for shrinking zone.
693 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
696 pgdat
->node_start_pfn
= pfn
;
697 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
699 } else if (pgdat_end_pfn
== end_pfn
) {
701 * If the section is biggest section in the pgdat, it need
702 * shrink pgdat->node_spanned_pages.
703 * In this case, we find second biggest valid mem_section for
706 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
709 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
713 * If the section is not biggest or smallest mem_section in the pgdat,
714 * it only creates a hole in the pgdat. So in this case, we need not
716 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
717 * has only hole or not.
719 pfn
= pgdat_start_pfn
;
720 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
721 ms
= __pfn_to_section(pfn
);
723 if (unlikely(!valid_section(ms
)))
726 if (pfn_to_nid(pfn
) != nid
)
729 /* If the section is current section, it continues the loop */
730 if (start_pfn
== pfn
)
733 /* If we find valid section, we have nothing to do */
737 /* The pgdat has no valid section */
738 pgdat
->node_start_pfn
= 0;
739 pgdat
->node_spanned_pages
= 0;
742 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
744 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
745 int nr_pages
= PAGES_PER_SECTION
;
749 zone_type
= zone
- pgdat
->node_zones
;
751 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
752 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
753 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
754 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
757 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
,
758 unsigned long map_offset
)
760 unsigned long start_pfn
;
764 if (!valid_section(ms
))
767 ret
= unregister_memory_section(ms
);
771 scn_nr
= __section_nr(ms
);
772 start_pfn
= section_nr_to_pfn(scn_nr
);
773 __remove_zone(zone
, start_pfn
);
775 sparse_remove_one_section(zone
, ms
, map_offset
);
780 * __remove_pages() - remove sections of pages from a zone
781 * @zone: zone from which pages need to be removed
782 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
783 * @nr_pages: number of pages to remove (must be multiple of section size)
785 * Generic helper function to remove section mappings and sysfs entries
786 * for the section of the memory we are removing. Caller needs to make
787 * sure that pages are marked reserved and zones are adjust properly by
788 * calling offline_pages().
790 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
791 unsigned long nr_pages
)
794 unsigned long map_offset
= 0;
795 int sections_to_remove
, ret
= 0;
797 /* In the ZONE_DEVICE case device driver owns the memory region */
798 if (is_dev_zone(zone
)) {
799 struct page
*page
= pfn_to_page(phys_start_pfn
);
800 struct vmem_altmap
*altmap
;
802 altmap
= to_vmem_altmap((unsigned long) page
);
804 map_offset
= vmem_altmap_offset(altmap
);
806 resource_size_t start
, size
;
808 start
= phys_start_pfn
<< PAGE_SHIFT
;
809 size
= nr_pages
* PAGE_SIZE
;
811 ret
= release_mem_region_adjustable(&iomem_resource
, start
,
814 resource_size_t endres
= start
+ size
- 1;
816 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
817 &start
, &endres
, ret
);
821 clear_zone_contiguous(zone
);
824 * We can only remove entire sections
826 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
827 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
829 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
830 for (i
= 0; i
< sections_to_remove
; i
++) {
831 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
833 ret
= __remove_section(zone
, __pfn_to_section(pfn
), map_offset
);
839 set_zone_contiguous(zone
);
843 EXPORT_SYMBOL_GPL(__remove_pages
);
844 #endif /* CONFIG_MEMORY_HOTREMOVE */
846 int set_online_page_callback(online_page_callback_t callback
)
851 mutex_lock(&online_page_callback_lock
);
853 if (online_page_callback
== generic_online_page
) {
854 online_page_callback
= callback
;
858 mutex_unlock(&online_page_callback_lock
);
863 EXPORT_SYMBOL_GPL(set_online_page_callback
);
865 int restore_online_page_callback(online_page_callback_t callback
)
870 mutex_lock(&online_page_callback_lock
);
872 if (online_page_callback
== callback
) {
873 online_page_callback
= generic_online_page
;
877 mutex_unlock(&online_page_callback_lock
);
882 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
884 void __online_page_set_limits(struct page
*page
)
887 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
889 void __online_page_increment_counters(struct page
*page
)
891 adjust_managed_page_count(page
, 1);
893 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
895 void __online_page_free(struct page
*page
)
897 __free_reserved_page(page
);
899 EXPORT_SYMBOL_GPL(__online_page_free
);
901 static void generic_online_page(struct page
*page
)
903 __online_page_set_limits(page
);
904 __online_page_increment_counters(page
);
905 __online_page_free(page
);
908 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
912 unsigned long onlined_pages
= *(unsigned long *)arg
;
914 if (PageReserved(pfn_to_page(start_pfn
)))
915 for (i
= 0; i
< nr_pages
; i
++) {
916 page
= pfn_to_page(start_pfn
+ i
);
917 (*online_page_callback
)(page
);
920 *(unsigned long *)arg
= onlined_pages
;
924 #ifdef CONFIG_MOVABLE_NODE
926 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
929 static bool can_online_high_movable(struct zone
*zone
)
933 #else /* CONFIG_MOVABLE_NODE */
934 /* ensure every online node has NORMAL memory */
935 static bool can_online_high_movable(struct zone
*zone
)
937 return node_state(zone_to_nid(zone
), N_NORMAL_MEMORY
);
939 #endif /* CONFIG_MOVABLE_NODE */
941 /* check which state of node_states will be changed when online memory */
942 static void node_states_check_changes_online(unsigned long nr_pages
,
943 struct zone
*zone
, struct memory_notify
*arg
)
945 int nid
= zone_to_nid(zone
);
946 enum zone_type zone_last
= ZONE_NORMAL
;
949 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
950 * contains nodes which have zones of 0...ZONE_NORMAL,
951 * set zone_last to ZONE_NORMAL.
953 * If we don't have HIGHMEM nor movable node,
954 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
955 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
957 if (N_MEMORY
== N_NORMAL_MEMORY
)
958 zone_last
= ZONE_MOVABLE
;
961 * if the memory to be online is in a zone of 0...zone_last, and
962 * the zones of 0...zone_last don't have memory before online, we will
963 * need to set the node to node_states[N_NORMAL_MEMORY] after
964 * the memory is online.
966 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
967 arg
->status_change_nid_normal
= nid
;
969 arg
->status_change_nid_normal
= -1;
971 #ifdef CONFIG_HIGHMEM
973 * If we have movable node, node_states[N_HIGH_MEMORY]
974 * contains nodes which have zones of 0...ZONE_HIGHMEM,
975 * set zone_last to ZONE_HIGHMEM.
977 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
978 * contains nodes which have zones of 0...ZONE_MOVABLE,
979 * set zone_last to ZONE_MOVABLE.
981 zone_last
= ZONE_HIGHMEM
;
982 if (N_MEMORY
== N_HIGH_MEMORY
)
983 zone_last
= ZONE_MOVABLE
;
985 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
986 arg
->status_change_nid_high
= nid
;
988 arg
->status_change_nid_high
= -1;
990 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
994 * if the node don't have memory befor online, we will need to
995 * set the node to node_states[N_MEMORY] after the memory
998 if (!node_state(nid
, N_MEMORY
))
999 arg
->status_change_nid
= nid
;
1001 arg
->status_change_nid
= -1;
1004 static void node_states_set_node(int node
, struct memory_notify
*arg
)
1006 if (arg
->status_change_nid_normal
>= 0)
1007 node_set_state(node
, N_NORMAL_MEMORY
);
1009 if (arg
->status_change_nid_high
>= 0)
1010 node_set_state(node
, N_HIGH_MEMORY
);
1012 node_set_state(node
, N_MEMORY
);
1016 /* Must be protected by mem_hotplug_begin() */
1017 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
1019 unsigned long flags
;
1020 unsigned long onlined_pages
= 0;
1022 int need_zonelists_rebuild
= 0;
1025 struct memory_notify arg
;
1028 * This doesn't need a lock to do pfn_to_page().
1029 * The section can't be removed here because of the
1030 * memory_block->state_mutex.
1032 zone
= page_zone(pfn_to_page(pfn
));
1034 if ((zone_idx(zone
) > ZONE_NORMAL
||
1035 online_type
== MMOP_ONLINE_MOVABLE
) &&
1036 !can_online_high_movable(zone
))
1039 if (online_type
== MMOP_ONLINE_KERNEL
&&
1040 zone_idx(zone
) == ZONE_MOVABLE
) {
1041 if (move_pfn_range_left(zone
- 1, zone
, pfn
, pfn
+ nr_pages
))
1044 if (online_type
== MMOP_ONLINE_MOVABLE
&&
1045 zone_idx(zone
) == ZONE_MOVABLE
- 1) {
1046 if (move_pfn_range_right(zone
, zone
+ 1, pfn
, pfn
+ nr_pages
))
1050 /* Previous code may changed the zone of the pfn range */
1051 zone
= page_zone(pfn_to_page(pfn
));
1053 arg
.start_pfn
= pfn
;
1054 arg
.nr_pages
= nr_pages
;
1055 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1057 nid
= pfn_to_nid(pfn
);
1059 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1060 ret
= notifier_to_errno(ret
);
1062 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1066 * If this zone is not populated, then it is not in zonelist.
1067 * This means the page allocator ignores this zone.
1068 * So, zonelist must be updated after online.
1070 mutex_lock(&zonelists_mutex
);
1071 if (!populated_zone(zone
)) {
1072 need_zonelists_rebuild
= 1;
1073 build_all_zonelists(NULL
, zone
);
1076 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1077 online_pages_range
);
1079 if (need_zonelists_rebuild
)
1080 zone_pcp_reset(zone
);
1081 mutex_unlock(&zonelists_mutex
);
1082 printk(KERN_DEBUG
"online_pages [mem %#010llx-%#010llx] failed\n",
1083 (unsigned long long) pfn
<< PAGE_SHIFT
,
1084 (((unsigned long long) pfn
+ nr_pages
)
1085 << PAGE_SHIFT
) - 1);
1086 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1090 zone
->present_pages
+= onlined_pages
;
1092 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1093 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1094 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1096 if (onlined_pages
) {
1097 node_states_set_node(zone_to_nid(zone
), &arg
);
1098 if (need_zonelists_rebuild
)
1099 build_all_zonelists(NULL
, NULL
);
1101 zone_pcp_update(zone
);
1104 mutex_unlock(&zonelists_mutex
);
1106 init_per_zone_wmark_min();
1109 kswapd_run(zone_to_nid(zone
));
1111 vm_total_pages
= nr_free_pagecache_pages();
1113 writeback_set_ratelimit();
1116 memory_notify(MEM_ONLINE
, &arg
);
1119 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1121 static void reset_node_present_pages(pg_data_t
*pgdat
)
1125 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1126 z
->present_pages
= 0;
1128 pgdat
->node_present_pages
= 0;
1131 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1132 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1134 struct pglist_data
*pgdat
;
1135 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1136 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1137 unsigned long start_pfn
= PFN_DOWN(start
);
1139 pgdat
= NODE_DATA(nid
);
1141 pgdat
= arch_alloc_nodedata(nid
);
1145 arch_refresh_nodedata(nid
, pgdat
);
1147 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1148 pgdat
->nr_zones
= 0;
1149 pgdat
->classzone_idx
= 0;
1152 /* we can use NODE_DATA(nid) from here */
1154 /* init node's zones as empty zones, we don't have any present pages.*/
1155 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1158 * The node we allocated has no zone fallback lists. For avoiding
1159 * to access not-initialized zonelist, build here.
1161 mutex_lock(&zonelists_mutex
);
1162 build_all_zonelists(pgdat
, NULL
);
1163 mutex_unlock(&zonelists_mutex
);
1166 * zone->managed_pages is set to an approximate value in
1167 * free_area_init_core(), which will cause
1168 * /sys/device/system/node/nodeX/meminfo has wrong data.
1169 * So reset it to 0 before any memory is onlined.
1171 reset_node_managed_pages(pgdat
);
1174 * When memory is hot-added, all the memory is in offline state. So
1175 * clear all zones' present_pages because they will be updated in
1176 * online_pages() and offline_pages().
1178 reset_node_present_pages(pgdat
);
1183 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1185 arch_refresh_nodedata(nid
, NULL
);
1186 arch_free_nodedata(pgdat
);
1192 * try_online_node - online a node if offlined
1194 * called by cpu_up() to online a node without onlined memory.
1196 int try_online_node(int nid
)
1201 if (node_online(nid
))
1204 mem_hotplug_begin();
1205 pgdat
= hotadd_new_pgdat(nid
, 0);
1207 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1211 node_set_online(nid
);
1212 ret
= register_one_node(nid
);
1215 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1216 mutex_lock(&zonelists_mutex
);
1217 build_all_zonelists(NULL
, NULL
);
1218 mutex_unlock(&zonelists_mutex
);
1226 static int check_hotplug_memory_range(u64 start
, u64 size
)
1228 u64 start_pfn
= PFN_DOWN(start
);
1229 u64 nr_pages
= size
>> PAGE_SHIFT
;
1231 /* Memory range must be aligned with section */
1232 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1233 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1234 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1235 (unsigned long long)start
,
1236 (unsigned long long)size
);
1244 * If movable zone has already been setup, newly added memory should be check.
1245 * If its address is higher than movable zone, it should be added as movable.
1246 * Without this check, movable zone may overlap with other zone.
1248 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1250 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1251 pg_data_t
*pgdat
= NODE_DATA(nid
);
1252 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1254 if (zone_is_empty(movable_zone
))
1257 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1263 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
,
1266 #ifdef CONFIG_ZONE_DEVICE
1270 if (should_add_memory_movable(nid
, start
, size
))
1271 return ZONE_MOVABLE
;
1273 return zone_default
;
1276 static int online_memory_block(struct memory_block
*mem
, void *arg
)
1278 return memory_block_change_state(mem
, MEM_ONLINE
, MEM_OFFLINE
);
1281 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1282 int __ref
add_memory_resource(int nid
, struct resource
*res
, bool online
)
1285 pg_data_t
*pgdat
= NULL
;
1291 size
= resource_size(res
);
1293 ret
= check_hotplug_memory_range(start
, size
);
1297 { /* Stupid hack to suppress address-never-null warning */
1298 void *p
= NODE_DATA(nid
);
1302 mem_hotplug_begin();
1305 * Add new range to memblock so that when hotadd_new_pgdat() is called
1306 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1307 * this new range and calculate total pages correctly. The range will
1308 * be removed at hot-remove time.
1310 memblock_add_node(start
, size
, nid
);
1312 new_node
= !node_online(nid
);
1314 pgdat
= hotadd_new_pgdat(nid
, start
);
1320 /* call arch's memory hotadd */
1321 ret
= arch_add_memory(nid
, start
, size
, false);
1326 /* we online node here. we can't roll back from here. */
1327 node_set_online(nid
);
1330 ret
= register_one_node(nid
);
1332 * If sysfs file of new node can't create, cpu on the node
1333 * can't be hot-added. There is no rollback way now.
1334 * So, check by BUG_ON() to catch it reluctantly..
1339 /* create new memmap entry */
1340 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1342 /* online pages if requested */
1344 walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1),
1345 NULL
, online_memory_block
);
1350 /* rollback pgdat allocation and others */
1352 rollback_node_hotadd(nid
, pgdat
);
1353 memblock_remove(start
, size
);
1359 EXPORT_SYMBOL_GPL(add_memory_resource
);
1361 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1363 struct resource
*res
;
1366 res
= register_memory_resource(start
, size
);
1368 return PTR_ERR(res
);
1370 ret
= add_memory_resource(nid
, res
, memhp_auto_online
);
1372 release_memory_resource(res
);
1375 EXPORT_SYMBOL_GPL(add_memory
);
1377 #ifdef CONFIG_MEMORY_HOTREMOVE
1379 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1380 * set and the size of the free page is given by page_order(). Using this,
1381 * the function determines if the pageblock contains only free pages.
1382 * Due to buddy contraints, a free page at least the size of a pageblock will
1383 * be located at the start of the pageblock
1385 static inline int pageblock_free(struct page
*page
)
1387 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1390 /* Return the start of the next active pageblock after a given page */
1391 static struct page
*next_active_pageblock(struct page
*page
)
1393 /* Ensure the starting page is pageblock-aligned */
1394 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1396 /* If the entire pageblock is free, move to the end of free page */
1397 if (pageblock_free(page
)) {
1399 /* be careful. we don't have locks, page_order can be changed.*/
1400 order
= page_order(page
);
1401 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1402 return page
+ (1 << order
);
1405 return page
+ pageblock_nr_pages
;
1408 /* Checks if this range of memory is likely to be hot-removable. */
1409 int is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1411 struct page
*page
= pfn_to_page(start_pfn
);
1412 struct page
*end_page
= page
+ nr_pages
;
1414 /* Check the starting page of each pageblock within the range */
1415 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1416 if (!is_pageblock_removable_nolock(page
))
1421 /* All pageblocks in the memory block are likely to be hot-removable */
1426 * Confirm all pages in a range [start, end) is belongs to the same zone.
1428 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
)
1430 unsigned long pfn
, sec_end_pfn
;
1431 struct zone
*zone
= NULL
;
1434 for (pfn
= start_pfn
, sec_end_pfn
= SECTION_ALIGN_UP(start_pfn
);
1436 pfn
= sec_end_pfn
+ 1, sec_end_pfn
+= PAGES_PER_SECTION
) {
1437 /* Make sure the memory section is present first */
1438 if (!present_section_nr(pfn_to_section_nr(pfn
)))
1440 for (; pfn
< sec_end_pfn
&& pfn
< end_pfn
;
1441 pfn
+= MAX_ORDER_NR_PAGES
) {
1443 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1444 while ((i
< MAX_ORDER_NR_PAGES
) &&
1445 !pfn_valid_within(pfn
+ i
))
1447 if (i
== MAX_ORDER_NR_PAGES
)
1449 page
= pfn_to_page(pfn
+ i
);
1450 if (zone
&& page_zone(page
) != zone
)
1452 zone
= page_zone(page
);
1459 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1460 * and hugepages). We scan pfn because it's much easier than scanning over
1461 * linked list. This function returns the pfn of the first found movable
1462 * page if it's found, otherwise 0.
1464 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1468 for (pfn
= start
; pfn
< end
; pfn
++) {
1469 if (pfn_valid(pfn
)) {
1470 page
= pfn_to_page(pfn
);
1473 if (PageHuge(page
)) {
1474 if (page_huge_active(page
))
1477 pfn
= round_up(pfn
+ 1,
1478 1 << compound_order(page
)) - 1;
1485 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1487 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1491 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1492 int not_managed
= 0;
1496 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1497 if (!pfn_valid(pfn
))
1499 page
= pfn_to_page(pfn
);
1501 if (PageHuge(page
)) {
1502 struct page
*head
= compound_head(page
);
1503 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1504 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1508 if (isolate_huge_page(page
, &source
))
1509 move_pages
-= 1 << compound_order(head
);
1513 if (!get_page_unless_zero(page
))
1516 * We can skip free pages. And we can only deal with pages on
1519 ret
= isolate_lru_page(page
);
1520 if (!ret
) { /* Success */
1522 list_add_tail(&page
->lru
, &source
);
1524 inc_zone_page_state(page
, NR_ISOLATED_ANON
+
1525 page_is_file_cache(page
));
1528 #ifdef CONFIG_DEBUG_VM
1529 printk(KERN_ALERT
"removing pfn %lx from LRU failed\n",
1531 dump_page(page
, "failed to remove from LRU");
1534 /* Because we don't have big zone->lock. we should
1535 check this again here. */
1536 if (page_count(page
)) {
1543 if (!list_empty(&source
)) {
1545 putback_movable_pages(&source
);
1550 * alloc_migrate_target should be improooooved!!
1551 * migrate_pages returns # of failed pages.
1553 ret
= migrate_pages(&source
, alloc_migrate_target
, NULL
, 0,
1554 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1556 putback_movable_pages(&source
);
1563 * remove from free_area[] and mark all as Reserved.
1566 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1569 __offline_isolated_pages(start
, start
+ nr_pages
);
1574 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1576 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1577 offline_isolated_pages_cb
);
1581 * Check all pages in range, recoreded as memory resource, are isolated.
1584 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1588 long offlined
= *(long *)data
;
1589 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1590 offlined
= nr_pages
;
1592 *(long *)data
+= offlined
;
1597 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1602 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1603 check_pages_isolated_cb
);
1605 offlined
= (long)ret
;
1609 #ifdef CONFIG_MOVABLE_NODE
1611 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1614 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1618 #else /* CONFIG_MOVABLE_NODE */
1619 /* ensure the node has NORMAL memory if it is still online */
1620 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1622 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1623 unsigned long present_pages
= 0;
1626 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1627 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1629 if (present_pages
> nr_pages
)
1633 for (; zt
<= ZONE_MOVABLE
; zt
++)
1634 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1637 * we can't offline the last normal memory until all
1638 * higher memory is offlined.
1640 return present_pages
== 0;
1642 #endif /* CONFIG_MOVABLE_NODE */
1644 static int __init
cmdline_parse_movable_node(char *p
)
1646 #ifdef CONFIG_MOVABLE_NODE
1648 * Memory used by the kernel cannot be hot-removed because Linux
1649 * cannot migrate the kernel pages. When memory hotplug is
1650 * enabled, we should prevent memblock from allocating memory
1653 * ACPI SRAT records all hotpluggable memory ranges. But before
1654 * SRAT is parsed, we don't know about it.
1656 * The kernel image is loaded into memory at very early time. We
1657 * cannot prevent this anyway. So on NUMA system, we set any
1658 * node the kernel resides in as un-hotpluggable.
1660 * Since on modern servers, one node could have double-digit
1661 * gigabytes memory, we can assume the memory around the kernel
1662 * image is also un-hotpluggable. So before SRAT is parsed, just
1663 * allocate memory near the kernel image to try the best to keep
1664 * the kernel away from hotpluggable memory.
1666 memblock_set_bottom_up(true);
1667 movable_node_enabled
= true;
1669 pr_warn("movable_node option not supported\n");
1673 early_param("movable_node", cmdline_parse_movable_node
);
1675 /* check which state of node_states will be changed when offline memory */
1676 static void node_states_check_changes_offline(unsigned long nr_pages
,
1677 struct zone
*zone
, struct memory_notify
*arg
)
1679 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1680 unsigned long present_pages
= 0;
1681 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1684 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1685 * contains nodes which have zones of 0...ZONE_NORMAL,
1686 * set zone_last to ZONE_NORMAL.
1688 * If we don't have HIGHMEM nor movable node,
1689 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1690 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1692 if (N_MEMORY
== N_NORMAL_MEMORY
)
1693 zone_last
= ZONE_MOVABLE
;
1696 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1697 * If the memory to be offline is in a zone of 0...zone_last,
1698 * and it is the last present memory, 0...zone_last will
1699 * become empty after offline , thus we can determind we will
1700 * need to clear the node from node_states[N_NORMAL_MEMORY].
1702 for (zt
= 0; zt
<= zone_last
; zt
++)
1703 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1704 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1705 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1707 arg
->status_change_nid_normal
= -1;
1709 #ifdef CONFIG_HIGHMEM
1711 * If we have movable node, node_states[N_HIGH_MEMORY]
1712 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1713 * set zone_last to ZONE_HIGHMEM.
1715 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1716 * contains nodes which have zones of 0...ZONE_MOVABLE,
1717 * set zone_last to ZONE_MOVABLE.
1719 zone_last
= ZONE_HIGHMEM
;
1720 if (N_MEMORY
== N_HIGH_MEMORY
)
1721 zone_last
= ZONE_MOVABLE
;
1723 for (; zt
<= zone_last
; zt
++)
1724 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1725 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1726 arg
->status_change_nid_high
= zone_to_nid(zone
);
1728 arg
->status_change_nid_high
= -1;
1730 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1734 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1736 zone_last
= ZONE_MOVABLE
;
1739 * check whether node_states[N_HIGH_MEMORY] will be changed
1740 * If we try to offline the last present @nr_pages from the node,
1741 * we can determind we will need to clear the node from
1742 * node_states[N_HIGH_MEMORY].
1744 for (; zt
<= zone_last
; zt
++)
1745 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1746 if (nr_pages
>= present_pages
)
1747 arg
->status_change_nid
= zone_to_nid(zone
);
1749 arg
->status_change_nid
= -1;
1752 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1754 if (arg
->status_change_nid_normal
>= 0)
1755 node_clear_state(node
, N_NORMAL_MEMORY
);
1757 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1758 (arg
->status_change_nid_high
>= 0))
1759 node_clear_state(node
, N_HIGH_MEMORY
);
1761 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1762 (arg
->status_change_nid
>= 0))
1763 node_clear_state(node
, N_MEMORY
);
1766 static int __ref
__offline_pages(unsigned long start_pfn
,
1767 unsigned long end_pfn
, unsigned long timeout
)
1769 unsigned long pfn
, nr_pages
, expire
;
1770 long offlined_pages
;
1771 int ret
, drain
, retry_max
, node
;
1772 unsigned long flags
;
1774 struct memory_notify arg
;
1776 /* at least, alignment against pageblock is necessary */
1777 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1779 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1781 /* This makes hotplug much easier...and readable.
1782 we assume this for now. .*/
1783 if (!test_pages_in_a_zone(start_pfn
, end_pfn
))
1786 zone
= page_zone(pfn_to_page(start_pfn
));
1787 node
= zone_to_nid(zone
);
1788 nr_pages
= end_pfn
- start_pfn
;
1790 if (zone_idx(zone
) <= ZONE_NORMAL
&& !can_offline_normal(zone
, nr_pages
))
1793 /* set above range as isolated */
1794 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1795 MIGRATE_MOVABLE
, true);
1799 arg
.start_pfn
= start_pfn
;
1800 arg
.nr_pages
= nr_pages
;
1801 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1803 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1804 ret
= notifier_to_errno(ret
);
1806 goto failed_removal
;
1809 expire
= jiffies
+ timeout
;
1813 /* start memory hot removal */
1815 if (time_after(jiffies
, expire
))
1816 goto failed_removal
;
1818 if (signal_pending(current
))
1819 goto failed_removal
;
1822 lru_add_drain_all();
1824 drain_all_pages(zone
);
1827 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1828 if (pfn
) { /* We have movable pages */
1829 ret
= do_migrate_range(pfn
, end_pfn
);
1835 if (--retry_max
== 0)
1836 goto failed_removal
;
1842 /* drain all zone's lru pagevec, this is asynchronous... */
1843 lru_add_drain_all();
1845 /* drain pcp pages, this is synchronous. */
1846 drain_all_pages(zone
);
1848 * dissolve free hugepages in the memory block before doing offlining
1849 * actually in order to make hugetlbfs's object counting consistent.
1851 dissolve_free_huge_pages(start_pfn
, end_pfn
);
1853 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1854 if (offlined_pages
< 0) {
1856 goto failed_removal
;
1858 printk(KERN_INFO
"Offlined Pages %ld\n", offlined_pages
);
1859 /* Ok, all of our target is isolated.
1860 We cannot do rollback at this point. */
1861 offline_isolated_pages(start_pfn
, end_pfn
);
1862 /* reset pagetype flags and makes migrate type to be MOVABLE */
1863 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1864 /* removal success */
1865 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1866 zone
->present_pages
-= offlined_pages
;
1868 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1869 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1870 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1872 init_per_zone_wmark_min();
1874 if (!populated_zone(zone
)) {
1875 zone_pcp_reset(zone
);
1876 mutex_lock(&zonelists_mutex
);
1877 build_all_zonelists(NULL
, NULL
);
1878 mutex_unlock(&zonelists_mutex
);
1880 zone_pcp_update(zone
);
1882 node_states_clear_node(node
, &arg
);
1883 if (arg
.status_change_nid
>= 0)
1886 vm_total_pages
= nr_free_pagecache_pages();
1887 writeback_set_ratelimit();
1889 memory_notify(MEM_OFFLINE
, &arg
);
1893 printk(KERN_INFO
"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 "
1969 "[%pa-%pa] is onlined\n",
1976 static int check_cpu_on_node(pg_data_t
*pgdat
)
1980 for_each_present_cpu(cpu
) {
1981 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1983 * the cpu on this node isn't removed, and we can't
1984 * offline this node.
1992 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1994 #ifdef CONFIG_ACPI_NUMA
1997 for_each_possible_cpu(cpu
)
1998 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1999 numa_clear_node(cpu
);
2003 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
2007 ret
= check_cpu_on_node(pgdat
);
2012 * the node will be offlined when we come here, so we can clear
2013 * the cpu_to_node() now.
2016 unmap_cpu_on_node(pgdat
);
2023 * Offline a node if all memory sections and cpus of the node are removed.
2025 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2026 * and online/offline operations before this call.
2028 void try_offline_node(int nid
)
2030 pg_data_t
*pgdat
= NODE_DATA(nid
);
2031 unsigned long start_pfn
= pgdat
->node_start_pfn
;
2032 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
2036 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
2037 unsigned long section_nr
= pfn_to_section_nr(pfn
);
2039 if (!present_section_nr(section_nr
))
2042 if (pfn_to_nid(pfn
) != nid
)
2046 * some memory sections of this node are not removed, and we
2047 * can't offline node now.
2052 if (check_and_unmap_cpu_on_node(pgdat
))
2056 * all memory/cpu of this node are removed, we can offline this
2059 node_set_offline(nid
);
2060 unregister_one_node(nid
);
2062 /* free waittable in each zone */
2063 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
2064 struct zone
*zone
= pgdat
->node_zones
+ i
;
2067 * wait_table may be allocated from boot memory,
2068 * here only free if it's allocated by vmalloc.
2070 if (is_vmalloc_addr(zone
->wait_table
)) {
2071 vfree(zone
->wait_table
);
2072 zone
->wait_table
= NULL
;
2076 EXPORT_SYMBOL(try_offline_node
);
2081 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2082 * and online/offline operations before this call, as required by
2083 * try_offline_node().
2085 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2089 BUG_ON(check_hotplug_memory_range(start
, size
));
2091 mem_hotplug_begin();
2094 * All memory blocks must be offlined before removing memory. Check
2095 * whether all memory blocks in question are offline and trigger a BUG()
2096 * if this is not the case.
2098 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2099 check_memblock_offlined_cb
);
2103 /* remove memmap entry */
2104 firmware_map_remove(start
, start
+ size
, "System RAM");
2105 memblock_free(start
, size
);
2106 memblock_remove(start
, size
);
2108 arch_remove_memory(start
, size
);
2110 try_offline_node(nid
);
2114 EXPORT_SYMBOL_GPL(remove_memory
);
2115 #endif /* CONFIG_MEMORY_HOTREMOVE */