2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.h>
9 #include <linux/swap.h>
10 #include <linux/interrupt.h>
11 #include <linux/pagemap.h>
12 #include <linux/compiler.h>
13 #include <linux/export.h>
14 #include <linux/pagevec.h>
15 #include <linux/writeback.h>
16 #include <linux/slab.h>
17 #include <linux/sysctl.h>
18 #include <linux/cpu.h>
19 #include <linux/memory.h>
20 #include <linux/memory_hotplug.h>
21 #include <linux/highmem.h>
22 #include <linux/vmalloc.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
25 #include <linux/migrate.h>
26 #include <linux/page-isolation.h>
27 #include <linux/pfn.h>
28 #include <linux/suspend.h>
29 #include <linux/mm_inline.h>
30 #include <linux/firmware-map.h>
31 #include <linux/stop_machine.h>
32 #include <linux/hugetlb.h>
33 #include <linux/memblock.h>
34 #include <linux/bootmem.h>
36 #include <asm/tlbflush.h>
41 * online_page_callback contains pointer to current page onlining function.
42 * Initially it is generic_online_page(). If it is required it could be
43 * changed by calling set_online_page_callback() for callback registration
44 * and restore_online_page_callback() for generic callback restore.
47 static void generic_online_page(struct page
*page
);
49 static online_page_callback_t online_page_callback
= generic_online_page
;
50 static DEFINE_MUTEX(online_page_callback_lock
);
52 /* The same as the cpu_hotplug lock, but for memory hotplug. */
54 struct task_struct
*active_writer
;
55 struct mutex lock
; /* Synchronizes accesses to refcount, */
57 * Also blocks the new readers during
58 * an ongoing mem hotplug operation.
62 #ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map
;
66 .active_writer
= NULL
,
67 .lock
= __MUTEX_INITIALIZER(mem_hotplug
.lock
),
69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map
= {.name
= "mem_hotplug.lock" },
74 /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
75 #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
76 #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
77 #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
79 void get_online_mems(void)
82 if (mem_hotplug
.active_writer
== current
)
84 memhp_lock_acquire_read();
85 mutex_lock(&mem_hotplug
.lock
);
86 mem_hotplug
.refcount
++;
87 mutex_unlock(&mem_hotplug
.lock
);
91 void put_online_mems(void)
93 if (mem_hotplug
.active_writer
== current
)
95 mutex_lock(&mem_hotplug
.lock
);
97 if (WARN_ON(!mem_hotplug
.refcount
))
98 mem_hotplug
.refcount
++; /* try to fix things up */
100 if (!--mem_hotplug
.refcount
&& unlikely(mem_hotplug
.active_writer
))
101 wake_up_process(mem_hotplug
.active_writer
);
102 mutex_unlock(&mem_hotplug
.lock
);
103 memhp_lock_release();
107 void mem_hotplug_begin(void)
109 mem_hotplug
.active_writer
= current
;
111 memhp_lock_acquire();
113 mutex_lock(&mem_hotplug
.lock
);
114 if (likely(!mem_hotplug
.refcount
))
116 __set_current_state(TASK_UNINTERRUPTIBLE
);
117 mutex_unlock(&mem_hotplug
.lock
);
122 void mem_hotplug_done(void)
124 mem_hotplug
.active_writer
= NULL
;
125 mutex_unlock(&mem_hotplug
.lock
);
126 memhp_lock_release();
129 /* add this memory to iomem resource */
130 static struct resource
*register_memory_resource(u64 start
, u64 size
)
132 struct resource
*res
;
133 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
136 res
->name
= "System RAM";
138 res
->end
= start
+ size
- 1;
139 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
140 if (request_resource(&iomem_resource
, res
) < 0) {
141 pr_debug("System RAM resource %pR cannot be added\n", res
);
148 static void release_memory_resource(struct resource
*res
)
152 release_resource(res
);
157 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
158 void get_page_bootmem(unsigned long info
, struct page
*page
,
161 page
->lru
.next
= (struct list_head
*) type
;
162 SetPagePrivate(page
);
163 set_page_private(page
, info
);
164 atomic_inc(&page
->_count
);
167 void put_page_bootmem(struct page
*page
)
171 type
= (unsigned long) page
->lru
.next
;
172 BUG_ON(type
< MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE
||
173 type
> MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE
);
175 if (atomic_dec_return(&page
->_count
) == 1) {
176 ClearPagePrivate(page
);
177 set_page_private(page
, 0);
178 INIT_LIST_HEAD(&page
->lru
);
179 free_reserved_page(page
);
183 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
184 #ifndef CONFIG_SPARSEMEM_VMEMMAP
185 static void register_page_bootmem_info_section(unsigned long start_pfn
)
187 unsigned long *usemap
, mapsize
, section_nr
, i
;
188 struct mem_section
*ms
;
189 struct page
*page
, *memmap
;
191 section_nr
= pfn_to_section_nr(start_pfn
);
192 ms
= __nr_to_section(section_nr
);
194 /* Get section's memmap address */
195 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
198 * Get page for the memmap's phys address
199 * XXX: need more consideration for sparse_vmemmap...
201 page
= virt_to_page(memmap
);
202 mapsize
= sizeof(struct page
) * PAGES_PER_SECTION
;
203 mapsize
= PAGE_ALIGN(mapsize
) >> PAGE_SHIFT
;
205 /* remember memmap's page */
206 for (i
= 0; i
< mapsize
; i
++, page
++)
207 get_page_bootmem(section_nr
, page
, SECTION_INFO
);
209 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
210 page
= virt_to_page(usemap
);
212 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
214 for (i
= 0; i
< mapsize
; i
++, page
++)
215 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
218 #else /* CONFIG_SPARSEMEM_VMEMMAP */
219 static void register_page_bootmem_info_section(unsigned long start_pfn
)
221 unsigned long *usemap
, mapsize
, section_nr
, i
;
222 struct mem_section
*ms
;
223 struct page
*page
, *memmap
;
225 if (!pfn_valid(start_pfn
))
228 section_nr
= pfn_to_section_nr(start_pfn
);
229 ms
= __nr_to_section(section_nr
);
231 memmap
= sparse_decode_mem_map(ms
->section_mem_map
, section_nr
);
233 register_page_bootmem_memmap(section_nr
, memmap
, PAGES_PER_SECTION
);
235 usemap
= __nr_to_section(section_nr
)->pageblock_flags
;
236 page
= virt_to_page(usemap
);
238 mapsize
= PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT
;
240 for (i
= 0; i
< mapsize
; i
++, page
++)
241 get_page_bootmem(section_nr
, page
, MIX_SECTION_INFO
);
243 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
245 void register_page_bootmem_info_node(struct pglist_data
*pgdat
)
247 unsigned long i
, pfn
, end_pfn
, nr_pages
;
248 int node
= pgdat
->node_id
;
252 nr_pages
= PAGE_ALIGN(sizeof(struct pglist_data
)) >> PAGE_SHIFT
;
253 page
= virt_to_page(pgdat
);
255 for (i
= 0; i
< nr_pages
; i
++, page
++)
256 get_page_bootmem(node
, page
, NODE_INFO
);
258 zone
= &pgdat
->node_zones
[0];
259 for (; zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1; zone
++) {
260 if (zone_is_initialized(zone
)) {
261 nr_pages
= zone
->wait_table_hash_nr_entries
262 * sizeof(wait_queue_head_t
);
263 nr_pages
= PAGE_ALIGN(nr_pages
) >> PAGE_SHIFT
;
264 page
= virt_to_page(zone
->wait_table
);
266 for (i
= 0; i
< nr_pages
; i
++, page
++)
267 get_page_bootmem(node
, page
, NODE_INFO
);
271 pfn
= pgdat
->node_start_pfn
;
272 end_pfn
= pgdat_end_pfn(pgdat
);
274 /* register section info */
275 for (; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
277 * Some platforms can assign the same pfn to multiple nodes - on
278 * node0 as well as nodeN. To avoid registering a pfn against
279 * multiple nodes we check that this pfn does not already
280 * reside in some other nodes.
282 if (pfn_valid(pfn
) && (pfn_to_nid(pfn
) == node
))
283 register_page_bootmem_info_section(pfn
);
286 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
288 static void __meminit
grow_zone_span(struct zone
*zone
, unsigned long start_pfn
,
289 unsigned long end_pfn
)
291 unsigned long old_zone_end_pfn
;
293 zone_span_writelock(zone
);
295 old_zone_end_pfn
= zone_end_pfn(zone
);
296 if (zone_is_empty(zone
) || start_pfn
< zone
->zone_start_pfn
)
297 zone
->zone_start_pfn
= start_pfn
;
299 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
300 zone
->zone_start_pfn
;
302 zone_span_writeunlock(zone
);
305 static void resize_zone(struct zone
*zone
, unsigned long start_pfn
,
306 unsigned long end_pfn
)
308 zone_span_writelock(zone
);
310 if (end_pfn
- start_pfn
) {
311 zone
->zone_start_pfn
= start_pfn
;
312 zone
->spanned_pages
= end_pfn
- start_pfn
;
315 * make it consist as free_area_init_core(),
316 * if spanned_pages = 0, then keep start_pfn = 0
318 zone
->zone_start_pfn
= 0;
319 zone
->spanned_pages
= 0;
322 zone_span_writeunlock(zone
);
325 static void fix_zone_id(struct zone
*zone
, unsigned long start_pfn
,
326 unsigned long end_pfn
)
328 enum zone_type zid
= zone_idx(zone
);
329 int nid
= zone
->zone_pgdat
->node_id
;
332 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
333 set_page_links(pfn_to_page(pfn
), zid
, nid
, pfn
);
336 /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
337 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
338 static int __ref
ensure_zone_is_initialized(struct zone
*zone
,
339 unsigned long start_pfn
, unsigned long num_pages
)
341 if (!zone_is_initialized(zone
))
342 return init_currently_empty_zone(zone
, start_pfn
, num_pages
,
347 static int __meminit
move_pfn_range_left(struct zone
*z1
, struct zone
*z2
,
348 unsigned long start_pfn
, unsigned long end_pfn
)
352 unsigned long z1_start_pfn
;
354 ret
= ensure_zone_is_initialized(z1
, start_pfn
, end_pfn
- start_pfn
);
358 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
360 /* can't move pfns which are higher than @z2 */
361 if (end_pfn
> zone_end_pfn(z2
))
363 /* the move out part must be at the left most of @z2 */
364 if (start_pfn
> z2
->zone_start_pfn
)
366 /* must included/overlap */
367 if (end_pfn
<= z2
->zone_start_pfn
)
370 /* use start_pfn for z1's start_pfn if z1 is empty */
371 if (!zone_is_empty(z1
))
372 z1_start_pfn
= z1
->zone_start_pfn
;
374 z1_start_pfn
= start_pfn
;
376 resize_zone(z1
, z1_start_pfn
, end_pfn
);
377 resize_zone(z2
, end_pfn
, zone_end_pfn(z2
));
379 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
381 fix_zone_id(z1
, start_pfn
, end_pfn
);
385 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
389 static int __meminit
move_pfn_range_right(struct zone
*z1
, struct zone
*z2
,
390 unsigned long start_pfn
, unsigned long end_pfn
)
394 unsigned long z2_end_pfn
;
396 ret
= ensure_zone_is_initialized(z2
, start_pfn
, end_pfn
- start_pfn
);
400 pgdat_resize_lock(z1
->zone_pgdat
, &flags
);
402 /* can't move pfns which are lower than @z1 */
403 if (z1
->zone_start_pfn
> start_pfn
)
405 /* the move out part mast at the right most of @z1 */
406 if (zone_end_pfn(z1
) > end_pfn
)
408 /* must included/overlap */
409 if (start_pfn
>= zone_end_pfn(z1
))
412 /* use end_pfn for z2's end_pfn if z2 is empty */
413 if (!zone_is_empty(z2
))
414 z2_end_pfn
= zone_end_pfn(z2
);
416 z2_end_pfn
= end_pfn
;
418 resize_zone(z1
, z1
->zone_start_pfn
, start_pfn
);
419 resize_zone(z2
, start_pfn
, z2_end_pfn
);
421 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
423 fix_zone_id(z2
, start_pfn
, end_pfn
);
427 pgdat_resize_unlock(z1
->zone_pgdat
, &flags
);
431 static void __meminit
grow_pgdat_span(struct pglist_data
*pgdat
, unsigned long start_pfn
,
432 unsigned long end_pfn
)
434 unsigned long old_pgdat_end_pfn
= pgdat_end_pfn(pgdat
);
436 if (!pgdat
->node_spanned_pages
|| start_pfn
< pgdat
->node_start_pfn
)
437 pgdat
->node_start_pfn
= start_pfn
;
439 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
440 pgdat
->node_start_pfn
;
443 static int __meminit
__add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
445 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
446 int nr_pages
= PAGES_PER_SECTION
;
447 int nid
= pgdat
->node_id
;
449 unsigned long flags
, pfn
;
452 zone_type
= zone
- pgdat
->node_zones
;
453 ret
= ensure_zone_is_initialized(zone
, phys_start_pfn
, nr_pages
);
457 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
458 grow_zone_span(zone
, phys_start_pfn
, phys_start_pfn
+ nr_pages
);
459 grow_pgdat_span(zone
->zone_pgdat
, phys_start_pfn
,
460 phys_start_pfn
+ nr_pages
);
461 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
462 memmap_init_zone(nr_pages
, nid
, zone_type
,
463 phys_start_pfn
, MEMMAP_HOTPLUG
);
465 /* online_page_range is called later and expects pages reserved */
466 for (pfn
= phys_start_pfn
; pfn
< phys_start_pfn
+ nr_pages
; pfn
++) {
470 SetPageReserved(pfn_to_page(pfn
));
475 static int __meminit
__add_section(int nid
, struct zone
*zone
,
476 unsigned long phys_start_pfn
)
480 if (pfn_valid(phys_start_pfn
))
483 ret
= sparse_add_one_section(zone
, phys_start_pfn
);
488 ret
= __add_zone(zone
, phys_start_pfn
);
493 return register_new_memory(nid
, __pfn_to_section(phys_start_pfn
));
497 * Reasonably generic function for adding memory. It is
498 * expected that archs that support memory hotplug will
499 * call this function after deciding the zone to which to
502 int __ref
__add_pages(int nid
, struct zone
*zone
, unsigned long phys_start_pfn
,
503 unsigned long nr_pages
)
507 int start_sec
, end_sec
;
508 /* during initialize mem_map, align hot-added range to section */
509 start_sec
= pfn_to_section_nr(phys_start_pfn
);
510 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
512 for (i
= start_sec
; i
<= end_sec
; i
++) {
513 err
= __add_section(nid
, zone
, section_nr_to_pfn(i
));
516 * EEXIST is finally dealt with by ioresource collision
517 * check. see add_memory() => register_memory_resource()
518 * Warning will be printed if there is collision.
520 if (err
&& (err
!= -EEXIST
))
524 vmemmap_populate_print_last();
528 EXPORT_SYMBOL_GPL(__add_pages
);
530 #ifdef CONFIG_MEMORY_HOTREMOVE
531 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
532 static int find_smallest_section_pfn(int nid
, struct zone
*zone
,
533 unsigned long start_pfn
,
534 unsigned long end_pfn
)
536 struct mem_section
*ms
;
538 for (; start_pfn
< end_pfn
; start_pfn
+= PAGES_PER_SECTION
) {
539 ms
= __pfn_to_section(start_pfn
);
541 if (unlikely(!valid_section(ms
)))
544 if (unlikely(pfn_to_nid(start_pfn
) != nid
))
547 if (zone
&& zone
!= page_zone(pfn_to_page(start_pfn
)))
556 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
557 static int find_biggest_section_pfn(int nid
, struct zone
*zone
,
558 unsigned long start_pfn
,
559 unsigned long end_pfn
)
561 struct mem_section
*ms
;
564 /* pfn is the end pfn of a memory section. */
566 for (; pfn
>= start_pfn
; pfn
-= PAGES_PER_SECTION
) {
567 ms
= __pfn_to_section(pfn
);
569 if (unlikely(!valid_section(ms
)))
572 if (unlikely(pfn_to_nid(pfn
) != nid
))
575 if (zone
&& zone
!= page_zone(pfn_to_page(pfn
)))
584 static void shrink_zone_span(struct zone
*zone
, unsigned long start_pfn
,
585 unsigned long end_pfn
)
587 unsigned long zone_start_pfn
= zone
->zone_start_pfn
;
588 unsigned long z
= zone_end_pfn(zone
); /* zone_end_pfn namespace clash */
589 unsigned long zone_end_pfn
= z
;
591 struct mem_section
*ms
;
592 int nid
= zone_to_nid(zone
);
594 zone_span_writelock(zone
);
595 if (zone_start_pfn
== start_pfn
) {
597 * If the section is smallest section in the zone, it need
598 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
599 * In this case, we find second smallest valid mem_section
600 * for shrinking zone.
602 pfn
= find_smallest_section_pfn(nid
, zone
, end_pfn
,
605 zone
->zone_start_pfn
= pfn
;
606 zone
->spanned_pages
= zone_end_pfn
- pfn
;
608 } else if (zone_end_pfn
== end_pfn
) {
610 * If the section is biggest section in the zone, it need
611 * shrink zone->spanned_pages.
612 * In this case, we find second biggest valid mem_section for
615 pfn
= find_biggest_section_pfn(nid
, zone
, zone_start_pfn
,
618 zone
->spanned_pages
= pfn
- zone_start_pfn
+ 1;
622 * The section is not biggest or smallest mem_section in the zone, it
623 * only creates a hole in the zone. So in this case, we need not
624 * change the zone. But perhaps, the zone has only hole data. Thus
625 * it check the zone has only hole or not.
627 pfn
= zone_start_pfn
;
628 for (; pfn
< zone_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
629 ms
= __pfn_to_section(pfn
);
631 if (unlikely(!valid_section(ms
)))
634 if (page_zone(pfn_to_page(pfn
)) != zone
)
637 /* If the section is current section, it continues the loop */
638 if (start_pfn
== pfn
)
641 /* If we find valid section, we have nothing to do */
642 zone_span_writeunlock(zone
);
646 /* The zone has no valid section */
647 zone
->zone_start_pfn
= 0;
648 zone
->spanned_pages
= 0;
649 zone_span_writeunlock(zone
);
652 static void shrink_pgdat_span(struct pglist_data
*pgdat
,
653 unsigned long start_pfn
, unsigned long end_pfn
)
655 unsigned long pgdat_start_pfn
= pgdat
->node_start_pfn
;
656 unsigned long p
= pgdat_end_pfn(pgdat
); /* pgdat_end_pfn namespace clash */
657 unsigned long pgdat_end_pfn
= p
;
659 struct mem_section
*ms
;
660 int nid
= pgdat
->node_id
;
662 if (pgdat_start_pfn
== start_pfn
) {
664 * If the section is smallest section in the pgdat, it need
665 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
666 * In this case, we find second smallest valid mem_section
667 * for shrinking zone.
669 pfn
= find_smallest_section_pfn(nid
, NULL
, end_pfn
,
672 pgdat
->node_start_pfn
= pfn
;
673 pgdat
->node_spanned_pages
= pgdat_end_pfn
- pfn
;
675 } else if (pgdat_end_pfn
== end_pfn
) {
677 * If the section is biggest section in the pgdat, it need
678 * shrink pgdat->node_spanned_pages.
679 * In this case, we find second biggest valid mem_section for
682 pfn
= find_biggest_section_pfn(nid
, NULL
, pgdat_start_pfn
,
685 pgdat
->node_spanned_pages
= pfn
- pgdat_start_pfn
+ 1;
689 * If the section is not biggest or smallest mem_section in the pgdat,
690 * it only creates a hole in the pgdat. So in this case, we need not
692 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
693 * has only hole or not.
695 pfn
= pgdat_start_pfn
;
696 for (; pfn
< pgdat_end_pfn
; pfn
+= PAGES_PER_SECTION
) {
697 ms
= __pfn_to_section(pfn
);
699 if (unlikely(!valid_section(ms
)))
702 if (pfn_to_nid(pfn
) != nid
)
705 /* If the section is current section, it continues the loop */
706 if (start_pfn
== pfn
)
709 /* If we find valid section, we have nothing to do */
713 /* The pgdat has no valid section */
714 pgdat
->node_start_pfn
= 0;
715 pgdat
->node_spanned_pages
= 0;
718 static void __remove_zone(struct zone
*zone
, unsigned long start_pfn
)
720 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
721 int nr_pages
= PAGES_PER_SECTION
;
725 zone_type
= zone
- pgdat
->node_zones
;
727 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
728 shrink_zone_span(zone
, start_pfn
, start_pfn
+ nr_pages
);
729 shrink_pgdat_span(pgdat
, start_pfn
, start_pfn
+ nr_pages
);
730 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
733 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
)
735 unsigned long start_pfn
;
739 if (!valid_section(ms
))
742 ret
= unregister_memory_section(ms
);
746 scn_nr
= __section_nr(ms
);
747 start_pfn
= section_nr_to_pfn(scn_nr
);
748 __remove_zone(zone
, start_pfn
);
750 sparse_remove_one_section(zone
, ms
);
755 * __remove_pages() - remove sections of pages from a zone
756 * @zone: zone from which pages need to be removed
757 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
758 * @nr_pages: number of pages to remove (must be multiple of section size)
760 * Generic helper function to remove section mappings and sysfs entries
761 * for the section of the memory we are removing. Caller needs to make
762 * sure that pages are marked reserved and zones are adjust properly by
763 * calling offline_pages().
765 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
766 unsigned long nr_pages
)
769 int sections_to_remove
;
770 resource_size_t start
, size
;
774 * We can only remove entire sections
776 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
777 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
779 start
= phys_start_pfn
<< PAGE_SHIFT
;
780 size
= nr_pages
* PAGE_SIZE
;
781 ret
= release_mem_region_adjustable(&iomem_resource
, start
, size
);
783 resource_size_t endres
= start
+ size
- 1;
785 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
786 &start
, &endres
, ret
);
789 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
790 for (i
= 0; i
< sections_to_remove
; i
++) {
791 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
792 ret
= __remove_section(zone
, __pfn_to_section(pfn
));
798 EXPORT_SYMBOL_GPL(__remove_pages
);
799 #endif /* CONFIG_MEMORY_HOTREMOVE */
801 int set_online_page_callback(online_page_callback_t callback
)
806 mutex_lock(&online_page_callback_lock
);
808 if (online_page_callback
== generic_online_page
) {
809 online_page_callback
= callback
;
813 mutex_unlock(&online_page_callback_lock
);
818 EXPORT_SYMBOL_GPL(set_online_page_callback
);
820 int restore_online_page_callback(online_page_callback_t callback
)
825 mutex_lock(&online_page_callback_lock
);
827 if (online_page_callback
== callback
) {
828 online_page_callback
= generic_online_page
;
832 mutex_unlock(&online_page_callback_lock
);
837 EXPORT_SYMBOL_GPL(restore_online_page_callback
);
839 void __online_page_set_limits(struct page
*page
)
842 EXPORT_SYMBOL_GPL(__online_page_set_limits
);
844 void __online_page_increment_counters(struct page
*page
)
846 adjust_managed_page_count(page
, 1);
848 EXPORT_SYMBOL_GPL(__online_page_increment_counters
);
850 void __online_page_free(struct page
*page
)
852 __free_reserved_page(page
);
854 EXPORT_SYMBOL_GPL(__online_page_free
);
856 static void generic_online_page(struct page
*page
)
858 __online_page_set_limits(page
);
859 __online_page_increment_counters(page
);
860 __online_page_free(page
);
863 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
867 unsigned long onlined_pages
= *(unsigned long *)arg
;
869 if (PageReserved(pfn_to_page(start_pfn
)))
870 for (i
= 0; i
< nr_pages
; i
++) {
871 page
= pfn_to_page(start_pfn
+ i
);
872 (*online_page_callback
)(page
);
875 *(unsigned long *)arg
= onlined_pages
;
879 #ifdef CONFIG_MOVABLE_NODE
881 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
884 static bool can_online_high_movable(struct zone
*zone
)
888 #else /* CONFIG_MOVABLE_NODE */
889 /* ensure every online node has NORMAL memory */
890 static bool can_online_high_movable(struct zone
*zone
)
892 return node_state(zone_to_nid(zone
), N_NORMAL_MEMORY
);
894 #endif /* CONFIG_MOVABLE_NODE */
896 /* check which state of node_states will be changed when online memory */
897 static void node_states_check_changes_online(unsigned long nr_pages
,
898 struct zone
*zone
, struct memory_notify
*arg
)
900 int nid
= zone_to_nid(zone
);
901 enum zone_type zone_last
= ZONE_NORMAL
;
904 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
905 * contains nodes which have zones of 0...ZONE_NORMAL,
906 * set zone_last to ZONE_NORMAL.
908 * If we don't have HIGHMEM nor movable node,
909 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
910 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
912 if (N_MEMORY
== N_NORMAL_MEMORY
)
913 zone_last
= ZONE_MOVABLE
;
916 * if the memory to be online is in a zone of 0...zone_last, and
917 * the zones of 0...zone_last don't have memory before online, we will
918 * need to set the node to node_states[N_NORMAL_MEMORY] after
919 * the memory is online.
921 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_NORMAL_MEMORY
))
922 arg
->status_change_nid_normal
= nid
;
924 arg
->status_change_nid_normal
= -1;
926 #ifdef CONFIG_HIGHMEM
928 * If we have movable node, node_states[N_HIGH_MEMORY]
929 * contains nodes which have zones of 0...ZONE_HIGHMEM,
930 * set zone_last to ZONE_HIGHMEM.
932 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
933 * contains nodes which have zones of 0...ZONE_MOVABLE,
934 * set zone_last to ZONE_MOVABLE.
936 zone_last
= ZONE_HIGHMEM
;
937 if (N_MEMORY
== N_HIGH_MEMORY
)
938 zone_last
= ZONE_MOVABLE
;
940 if (zone_idx(zone
) <= zone_last
&& !node_state(nid
, N_HIGH_MEMORY
))
941 arg
->status_change_nid_high
= nid
;
943 arg
->status_change_nid_high
= -1;
945 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
949 * if the node don't have memory befor online, we will need to
950 * set the node to node_states[N_MEMORY] after the memory
953 if (!node_state(nid
, N_MEMORY
))
954 arg
->status_change_nid
= nid
;
956 arg
->status_change_nid
= -1;
959 static void node_states_set_node(int node
, struct memory_notify
*arg
)
961 if (arg
->status_change_nid_normal
>= 0)
962 node_set_state(node
, N_NORMAL_MEMORY
);
964 if (arg
->status_change_nid_high
>= 0)
965 node_set_state(node
, N_HIGH_MEMORY
);
967 node_set_state(node
, N_MEMORY
);
971 /* Must be protected by mem_hotplug_begin() */
972 int __ref
online_pages(unsigned long pfn
, unsigned long nr_pages
, int online_type
)
975 unsigned long onlined_pages
= 0;
977 int need_zonelists_rebuild
= 0;
980 struct memory_notify arg
;
983 * This doesn't need a lock to do pfn_to_page().
984 * The section can't be removed here because of the
985 * memory_block->state_mutex.
987 zone
= page_zone(pfn_to_page(pfn
));
989 if ((zone_idx(zone
) > ZONE_NORMAL
||
990 online_type
== MMOP_ONLINE_MOVABLE
) &&
991 !can_online_high_movable(zone
))
994 if (online_type
== MMOP_ONLINE_KERNEL
&&
995 zone_idx(zone
) == ZONE_MOVABLE
) {
996 if (move_pfn_range_left(zone
- 1, zone
, pfn
, pfn
+ nr_pages
))
999 if (online_type
== MMOP_ONLINE_MOVABLE
&&
1000 zone_idx(zone
) == ZONE_MOVABLE
- 1) {
1001 if (move_pfn_range_right(zone
, zone
+ 1, pfn
, pfn
+ nr_pages
))
1005 /* Previous code may changed the zone of the pfn range */
1006 zone
= page_zone(pfn_to_page(pfn
));
1008 arg
.start_pfn
= pfn
;
1009 arg
.nr_pages
= nr_pages
;
1010 node_states_check_changes_online(nr_pages
, zone
, &arg
);
1012 nid
= pfn_to_nid(pfn
);
1014 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
1015 ret
= notifier_to_errno(ret
);
1017 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1021 * If this zone is not populated, then it is not in zonelist.
1022 * This means the page allocator ignores this zone.
1023 * So, zonelist must be updated after online.
1025 mutex_lock(&zonelists_mutex
);
1026 if (!populated_zone(zone
)) {
1027 need_zonelists_rebuild
= 1;
1028 build_all_zonelists(NULL
, zone
);
1031 ret
= walk_system_ram_range(pfn
, nr_pages
, &onlined_pages
,
1032 online_pages_range
);
1034 if (need_zonelists_rebuild
)
1035 zone_pcp_reset(zone
);
1036 mutex_unlock(&zonelists_mutex
);
1037 printk(KERN_DEBUG
"online_pages [mem %#010llx-%#010llx] failed\n",
1038 (unsigned long long) pfn
<< PAGE_SHIFT
,
1039 (((unsigned long long) pfn
+ nr_pages
)
1040 << PAGE_SHIFT
) - 1);
1041 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
1045 zone
->present_pages
+= onlined_pages
;
1047 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1048 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
1049 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1051 if (onlined_pages
) {
1052 node_states_set_node(zone_to_nid(zone
), &arg
);
1053 if (need_zonelists_rebuild
)
1054 build_all_zonelists(NULL
, NULL
);
1056 zone_pcp_update(zone
);
1059 mutex_unlock(&zonelists_mutex
);
1061 init_per_zone_wmark_min();
1064 kswapd_run(zone_to_nid(zone
));
1066 vm_total_pages
= nr_free_pagecache_pages();
1068 writeback_set_ratelimit();
1071 memory_notify(MEM_ONLINE
, &arg
);
1074 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1076 static void reset_node_present_pages(pg_data_t
*pgdat
)
1080 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
1081 z
->present_pages
= 0;
1083 pgdat
->node_present_pages
= 0;
1086 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1087 static pg_data_t __ref
*hotadd_new_pgdat(int nid
, u64 start
)
1089 struct pglist_data
*pgdat
;
1090 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
1091 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
1092 unsigned long start_pfn
= PFN_DOWN(start
);
1094 pgdat
= NODE_DATA(nid
);
1096 pgdat
= arch_alloc_nodedata(nid
);
1100 arch_refresh_nodedata(nid
, pgdat
);
1102 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1103 pgdat
->nr_zones
= 0;
1104 pgdat
->classzone_idx
= 0;
1107 /* we can use NODE_DATA(nid) from here */
1109 /* init node's zones as empty zones, we don't have any present pages.*/
1110 free_area_init_node(nid
, zones_size
, start_pfn
, zholes_size
);
1113 * The node we allocated has no zone fallback lists. For avoiding
1114 * to access not-initialized zonelist, build here.
1116 mutex_lock(&zonelists_mutex
);
1117 build_all_zonelists(pgdat
, NULL
);
1118 mutex_unlock(&zonelists_mutex
);
1121 * zone->managed_pages is set to an approximate value in
1122 * free_area_init_core(), which will cause
1123 * /sys/device/system/node/nodeX/meminfo has wrong data.
1124 * So reset it to 0 before any memory is onlined.
1126 reset_node_managed_pages(pgdat
);
1129 * When memory is hot-added, all the memory is in offline state. So
1130 * clear all zones' present_pages because they will be updated in
1131 * online_pages() and offline_pages().
1133 reset_node_present_pages(pgdat
);
1138 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
1140 arch_refresh_nodedata(nid
, NULL
);
1141 arch_free_nodedata(pgdat
);
1147 * try_online_node - online a node if offlined
1149 * called by cpu_up() to online a node without onlined memory.
1151 int try_online_node(int nid
)
1156 if (node_online(nid
))
1159 mem_hotplug_begin();
1160 pgdat
= hotadd_new_pgdat(nid
, 0);
1162 pr_err("Cannot online node %d due to NULL pgdat\n", nid
);
1166 node_set_online(nid
);
1167 ret
= register_one_node(nid
);
1170 if (pgdat
->node_zonelists
->_zonerefs
->zone
== NULL
) {
1171 mutex_lock(&zonelists_mutex
);
1172 build_all_zonelists(NULL
, NULL
);
1173 mutex_unlock(&zonelists_mutex
);
1181 static int check_hotplug_memory_range(u64 start
, u64 size
)
1183 u64 start_pfn
= PFN_DOWN(start
);
1184 u64 nr_pages
= size
>> PAGE_SHIFT
;
1186 /* Memory range must be aligned with section */
1187 if ((start_pfn
& ~PAGE_SECTION_MASK
) ||
1188 (nr_pages
% PAGES_PER_SECTION
) || (!nr_pages
)) {
1189 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1190 (unsigned long long)start
,
1191 (unsigned long long)size
);
1199 * If movable zone has already been setup, newly added memory should be check.
1200 * If its address is higher than movable zone, it should be added as movable.
1201 * Without this check, movable zone may overlap with other zone.
1203 static int should_add_memory_movable(int nid
, u64 start
, u64 size
)
1205 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1206 pg_data_t
*pgdat
= NODE_DATA(nid
);
1207 struct zone
*movable_zone
= pgdat
->node_zones
+ ZONE_MOVABLE
;
1209 if (zone_is_empty(movable_zone
))
1212 if (movable_zone
->zone_start_pfn
<= start_pfn
)
1218 int zone_for_memory(int nid
, u64 start
, u64 size
, int zone_default
)
1220 if (should_add_memory_movable(nid
, start
, size
))
1221 return ZONE_MOVABLE
;
1223 return zone_default
;
1226 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1227 int __ref
add_memory(int nid
, u64 start
, u64 size
)
1229 pg_data_t
*pgdat
= NULL
;
1232 struct resource
*res
;
1235 ret
= check_hotplug_memory_range(start
, size
);
1239 res
= register_memory_resource(start
, size
);
1244 { /* Stupid hack to suppress address-never-null warning */
1245 void *p
= NODE_DATA(nid
);
1249 mem_hotplug_begin();
1251 new_node
= !node_online(nid
);
1253 pgdat
= hotadd_new_pgdat(nid
, start
);
1259 /* call arch's memory hotadd */
1260 ret
= arch_add_memory(nid
, start
, size
);
1265 /* we online node here. we can't roll back from here. */
1266 node_set_online(nid
);
1269 ret
= register_one_node(nid
);
1271 * If sysfs file of new node can't create, cpu on the node
1272 * can't be hot-added. There is no rollback way now.
1273 * So, check by BUG_ON() to catch it reluctantly..
1278 /* create new memmap entry */
1279 firmware_map_add_hotplug(start
, start
+ size
, "System RAM");
1280 memblock_add_node(start
, size
, nid
);
1285 /* rollback pgdat allocation and others */
1287 rollback_node_hotadd(nid
, pgdat
);
1288 release_memory_resource(res
);
1294 EXPORT_SYMBOL_GPL(add_memory
);
1296 #ifdef CONFIG_MEMORY_HOTREMOVE
1298 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1299 * set and the size of the free page is given by page_order(). Using this,
1300 * the function determines if the pageblock contains only free pages.
1301 * Due to buddy contraints, a free page at least the size of a pageblock will
1302 * be located at the start of the pageblock
1304 static inline int pageblock_free(struct page
*page
)
1306 return PageBuddy(page
) && page_order(page
) >= pageblock_order
;
1309 /* Return the start of the next active pageblock after a given page */
1310 static struct page
*next_active_pageblock(struct page
*page
)
1312 /* Ensure the starting page is pageblock-aligned */
1313 BUG_ON(page_to_pfn(page
) & (pageblock_nr_pages
- 1));
1315 /* If the entire pageblock is free, move to the end of free page */
1316 if (pageblock_free(page
)) {
1318 /* be careful. we don't have locks, page_order can be changed.*/
1319 order
= page_order(page
);
1320 if ((order
< MAX_ORDER
) && (order
>= pageblock_order
))
1321 return page
+ (1 << order
);
1324 return page
+ pageblock_nr_pages
;
1327 /* Checks if this range of memory is likely to be hot-removable. */
1328 int is_mem_section_removable(unsigned long start_pfn
, unsigned long nr_pages
)
1330 struct page
*page
= pfn_to_page(start_pfn
);
1331 struct page
*end_page
= page
+ nr_pages
;
1333 /* Check the starting page of each pageblock within the range */
1334 for (; page
< end_page
; page
= next_active_pageblock(page
)) {
1335 if (!is_pageblock_removable_nolock(page
))
1340 /* All pageblocks in the memory block are likely to be hot-removable */
1345 * Confirm all pages in a range [start, end) is belongs to the same zone.
1347 int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
)
1350 struct zone
*zone
= NULL
;
1353 for (pfn
= start_pfn
;
1355 pfn
+= MAX_ORDER_NR_PAGES
) {
1357 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1358 while ((i
< MAX_ORDER_NR_PAGES
) && !pfn_valid_within(pfn
+ i
))
1360 if (i
== MAX_ORDER_NR_PAGES
)
1362 page
= pfn_to_page(pfn
+ i
);
1363 if (zone
&& page_zone(page
) != zone
)
1365 zone
= page_zone(page
);
1371 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1372 * and hugepages). We scan pfn because it's much easier than scanning over
1373 * linked list. This function returns the pfn of the first found movable
1374 * page if it's found, otherwise 0.
1376 static unsigned long scan_movable_pages(unsigned long start
, unsigned long end
)
1380 for (pfn
= start
; pfn
< end
; pfn
++) {
1381 if (pfn_valid(pfn
)) {
1382 page
= pfn_to_page(pfn
);
1385 if (PageHuge(page
)) {
1386 if (page_huge_active(page
))
1389 pfn
= round_up(pfn
+ 1,
1390 1 << compound_order(page
)) - 1;
1397 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1399 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
1403 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
1404 int not_managed
= 0;
1408 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
1409 if (!pfn_valid(pfn
))
1411 page
= pfn_to_page(pfn
);
1413 if (PageHuge(page
)) {
1414 struct page
*head
= compound_head(page
);
1415 pfn
= page_to_pfn(head
) + (1<<compound_order(head
)) - 1;
1416 if (compound_order(head
) > PFN_SECTION_SHIFT
) {
1420 if (isolate_huge_page(page
, &source
))
1421 move_pages
-= 1 << compound_order(head
);
1425 if (!get_page_unless_zero(page
))
1428 * We can skip free pages. And we can only deal with pages on
1431 ret
= isolate_lru_page(page
);
1432 if (!ret
) { /* Success */
1434 list_add_tail(&page
->lru
, &source
);
1436 inc_zone_page_state(page
, NR_ISOLATED_ANON
+
1437 page_is_file_cache(page
));
1440 #ifdef CONFIG_DEBUG_VM
1441 printk(KERN_ALERT
"removing pfn %lx from LRU failed\n",
1443 dump_page(page
, "failed to remove from LRU");
1446 /* Because we don't have big zone->lock. we should
1447 check this again here. */
1448 if (page_count(page
)) {
1455 if (!list_empty(&source
)) {
1457 putback_movable_pages(&source
);
1462 * alloc_migrate_target should be improooooved!!
1463 * migrate_pages returns # of failed pages.
1465 ret
= migrate_pages(&source
, alloc_migrate_target
, NULL
, 0,
1466 MIGRATE_SYNC
, MR_MEMORY_HOTPLUG
);
1468 putback_movable_pages(&source
);
1475 * remove from free_area[] and mark all as Reserved.
1478 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
1481 __offline_isolated_pages(start
, start
+ nr_pages
);
1486 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
1488 walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, NULL
,
1489 offline_isolated_pages_cb
);
1493 * Check all pages in range, recoreded as memory resource, are isolated.
1496 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
1500 long offlined
= *(long *)data
;
1501 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
, true);
1502 offlined
= nr_pages
;
1504 *(long *)data
+= offlined
;
1509 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
1514 ret
= walk_system_ram_range(start_pfn
, end_pfn
- start_pfn
, &offlined
,
1515 check_pages_isolated_cb
);
1517 offlined
= (long)ret
;
1521 #ifdef CONFIG_MOVABLE_NODE
1523 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1526 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1530 #else /* CONFIG_MOVABLE_NODE */
1531 /* ensure the node has NORMAL memory if it is still online */
1532 static bool can_offline_normal(struct zone
*zone
, unsigned long nr_pages
)
1534 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1535 unsigned long present_pages
= 0;
1538 for (zt
= 0; zt
<= ZONE_NORMAL
; zt
++)
1539 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1541 if (present_pages
> nr_pages
)
1545 for (; zt
<= ZONE_MOVABLE
; zt
++)
1546 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1549 * we can't offline the last normal memory until all
1550 * higher memory is offlined.
1552 return present_pages
== 0;
1554 #endif /* CONFIG_MOVABLE_NODE */
1556 static int __init
cmdline_parse_movable_node(char *p
)
1558 #ifdef CONFIG_MOVABLE_NODE
1560 * Memory used by the kernel cannot be hot-removed because Linux
1561 * cannot migrate the kernel pages. When memory hotplug is
1562 * enabled, we should prevent memblock from allocating memory
1565 * ACPI SRAT records all hotpluggable memory ranges. But before
1566 * SRAT is parsed, we don't know about it.
1568 * The kernel image is loaded into memory at very early time. We
1569 * cannot prevent this anyway. So on NUMA system, we set any
1570 * node the kernel resides in as un-hotpluggable.
1572 * Since on modern servers, one node could have double-digit
1573 * gigabytes memory, we can assume the memory around the kernel
1574 * image is also un-hotpluggable. So before SRAT is parsed, just
1575 * allocate memory near the kernel image to try the best to keep
1576 * the kernel away from hotpluggable memory.
1578 memblock_set_bottom_up(true);
1579 movable_node_enabled
= true;
1581 pr_warn("movable_node option not supported\n");
1585 early_param("movable_node", cmdline_parse_movable_node
);
1587 /* check which state of node_states will be changed when offline memory */
1588 static void node_states_check_changes_offline(unsigned long nr_pages
,
1589 struct zone
*zone
, struct memory_notify
*arg
)
1591 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
1592 unsigned long present_pages
= 0;
1593 enum zone_type zt
, zone_last
= ZONE_NORMAL
;
1596 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1597 * contains nodes which have zones of 0...ZONE_NORMAL,
1598 * set zone_last to ZONE_NORMAL.
1600 * If we don't have HIGHMEM nor movable node,
1601 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1602 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1604 if (N_MEMORY
== N_NORMAL_MEMORY
)
1605 zone_last
= ZONE_MOVABLE
;
1608 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1609 * If the memory to be offline is in a zone of 0...zone_last,
1610 * and it is the last present memory, 0...zone_last will
1611 * become empty after offline , thus we can determind we will
1612 * need to clear the node from node_states[N_NORMAL_MEMORY].
1614 for (zt
= 0; zt
<= zone_last
; zt
++)
1615 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1616 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1617 arg
->status_change_nid_normal
= zone_to_nid(zone
);
1619 arg
->status_change_nid_normal
= -1;
1621 #ifdef CONFIG_HIGHMEM
1623 * If we have movable node, node_states[N_HIGH_MEMORY]
1624 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1625 * set zone_last to ZONE_HIGHMEM.
1627 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1628 * contains nodes which have zones of 0...ZONE_MOVABLE,
1629 * set zone_last to ZONE_MOVABLE.
1631 zone_last
= ZONE_HIGHMEM
;
1632 if (N_MEMORY
== N_HIGH_MEMORY
)
1633 zone_last
= ZONE_MOVABLE
;
1635 for (; zt
<= zone_last
; zt
++)
1636 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1637 if (zone_idx(zone
) <= zone_last
&& nr_pages
>= present_pages
)
1638 arg
->status_change_nid_high
= zone_to_nid(zone
);
1640 arg
->status_change_nid_high
= -1;
1642 arg
->status_change_nid_high
= arg
->status_change_nid_normal
;
1646 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1648 zone_last
= ZONE_MOVABLE
;
1651 * check whether node_states[N_HIGH_MEMORY] will be changed
1652 * If we try to offline the last present @nr_pages from the node,
1653 * we can determind we will need to clear the node from
1654 * node_states[N_HIGH_MEMORY].
1656 for (; zt
<= zone_last
; zt
++)
1657 present_pages
+= pgdat
->node_zones
[zt
].present_pages
;
1658 if (nr_pages
>= present_pages
)
1659 arg
->status_change_nid
= zone_to_nid(zone
);
1661 arg
->status_change_nid
= -1;
1664 static void node_states_clear_node(int node
, struct memory_notify
*arg
)
1666 if (arg
->status_change_nid_normal
>= 0)
1667 node_clear_state(node
, N_NORMAL_MEMORY
);
1669 if ((N_MEMORY
!= N_NORMAL_MEMORY
) &&
1670 (arg
->status_change_nid_high
>= 0))
1671 node_clear_state(node
, N_HIGH_MEMORY
);
1673 if ((N_MEMORY
!= N_HIGH_MEMORY
) &&
1674 (arg
->status_change_nid
>= 0))
1675 node_clear_state(node
, N_MEMORY
);
1678 static int __ref
__offline_pages(unsigned long start_pfn
,
1679 unsigned long end_pfn
, unsigned long timeout
)
1681 unsigned long pfn
, nr_pages
, expire
;
1682 long offlined_pages
;
1683 int ret
, drain
, retry_max
, node
;
1684 unsigned long flags
;
1686 struct memory_notify arg
;
1688 /* at least, alignment against pageblock is necessary */
1689 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
1691 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
1693 /* This makes hotplug much easier...and readable.
1694 we assume this for now. .*/
1695 if (!test_pages_in_a_zone(start_pfn
, end_pfn
))
1698 zone
= page_zone(pfn_to_page(start_pfn
));
1699 node
= zone_to_nid(zone
);
1700 nr_pages
= end_pfn
- start_pfn
;
1702 if (zone_idx(zone
) <= ZONE_NORMAL
&& !can_offline_normal(zone
, nr_pages
))
1705 /* set above range as isolated */
1706 ret
= start_isolate_page_range(start_pfn
, end_pfn
,
1707 MIGRATE_MOVABLE
, true);
1711 arg
.start_pfn
= start_pfn
;
1712 arg
.nr_pages
= nr_pages
;
1713 node_states_check_changes_offline(nr_pages
, zone
, &arg
);
1715 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
1716 ret
= notifier_to_errno(ret
);
1718 goto failed_removal
;
1721 expire
= jiffies
+ timeout
;
1725 /* start memory hot removal */
1727 if (time_after(jiffies
, expire
))
1728 goto failed_removal
;
1730 if (signal_pending(current
))
1731 goto failed_removal
;
1734 lru_add_drain_all();
1736 drain_all_pages(zone
);
1739 pfn
= scan_movable_pages(start_pfn
, end_pfn
);
1740 if (pfn
) { /* We have movable pages */
1741 ret
= do_migrate_range(pfn
, end_pfn
);
1747 if (--retry_max
== 0)
1748 goto failed_removal
;
1754 /* drain all zone's lru pagevec, this is asynchronous... */
1755 lru_add_drain_all();
1757 /* drain pcp pages, this is synchronous. */
1758 drain_all_pages(zone
);
1760 * dissolve free hugepages in the memory block before doing offlining
1761 * actually in order to make hugetlbfs's object counting consistent.
1763 dissolve_free_huge_pages(start_pfn
, end_pfn
);
1765 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
1766 if (offlined_pages
< 0) {
1768 goto failed_removal
;
1770 printk(KERN_INFO
"Offlined Pages %ld\n", offlined_pages
);
1771 /* Ok, all of our target is isolated.
1772 We cannot do rollback at this point. */
1773 offline_isolated_pages(start_pfn
, end_pfn
);
1774 /* reset pagetype flags and makes migrate type to be MOVABLE */
1775 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1776 /* removal success */
1777 adjust_managed_page_count(pfn_to_page(start_pfn
), -offlined_pages
);
1778 zone
->present_pages
-= offlined_pages
;
1780 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
1781 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
1782 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
1784 init_per_zone_wmark_min();
1786 if (!populated_zone(zone
)) {
1787 zone_pcp_reset(zone
);
1788 mutex_lock(&zonelists_mutex
);
1789 build_all_zonelists(NULL
, NULL
);
1790 mutex_unlock(&zonelists_mutex
);
1792 zone_pcp_update(zone
);
1794 node_states_clear_node(node
, &arg
);
1795 if (arg
.status_change_nid
>= 0)
1798 vm_total_pages
= nr_free_pagecache_pages();
1799 writeback_set_ratelimit();
1801 memory_notify(MEM_OFFLINE
, &arg
);
1805 printk(KERN_INFO
"memory offlining [mem %#010llx-%#010llx] failed\n",
1806 (unsigned long long) start_pfn
<< PAGE_SHIFT
,
1807 ((unsigned long long) end_pfn
<< PAGE_SHIFT
) - 1);
1808 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
1809 /* pushback to free area */
1810 undo_isolate_page_range(start_pfn
, end_pfn
, MIGRATE_MOVABLE
);
1814 /* Must be protected by mem_hotplug_begin() */
1815 int offline_pages(unsigned long start_pfn
, unsigned long nr_pages
)
1817 return __offline_pages(start_pfn
, start_pfn
+ nr_pages
, 120 * HZ
);
1819 #endif /* CONFIG_MEMORY_HOTREMOVE */
1822 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1823 * @start_pfn: start pfn of the memory range
1824 * @end_pfn: end pfn of the memory range
1825 * @arg: argument passed to func
1826 * @func: callback for each memory section walked
1828 * This function walks through all present mem sections in range
1829 * [start_pfn, end_pfn) and call func on each mem section.
1831 * Returns the return value of func.
1833 int walk_memory_range(unsigned long start_pfn
, unsigned long end_pfn
,
1834 void *arg
, int (*func
)(struct memory_block
*, void *))
1836 struct memory_block
*mem
= NULL
;
1837 struct mem_section
*section
;
1838 unsigned long pfn
, section_nr
;
1841 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1842 section_nr
= pfn_to_section_nr(pfn
);
1843 if (!present_section_nr(section_nr
))
1846 section
= __nr_to_section(section_nr
);
1847 /* same memblock? */
1849 if ((section_nr
>= mem
->start_section_nr
) &&
1850 (section_nr
<= mem
->end_section_nr
))
1853 mem
= find_memory_block_hinted(section
, mem
);
1857 ret
= func(mem
, arg
);
1859 kobject_put(&mem
->dev
.kobj
);
1865 kobject_put(&mem
->dev
.kobj
);
1870 #ifdef CONFIG_MEMORY_HOTREMOVE
1871 static int check_memblock_offlined_cb(struct memory_block
*mem
, void *arg
)
1873 int ret
= !is_memblock_offlined(mem
);
1875 if (unlikely(ret
)) {
1876 phys_addr_t beginpa
, endpa
;
1878 beginpa
= PFN_PHYS(section_nr_to_pfn(mem
->start_section_nr
));
1879 endpa
= PFN_PHYS(section_nr_to_pfn(mem
->end_section_nr
+ 1))-1;
1880 pr_warn("removing memory fails, because memory "
1881 "[%pa-%pa] is onlined\n",
1888 static int check_cpu_on_node(pg_data_t
*pgdat
)
1892 for_each_present_cpu(cpu
) {
1893 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1895 * the cpu on this node isn't removed, and we can't
1896 * offline this node.
1904 static void unmap_cpu_on_node(pg_data_t
*pgdat
)
1906 #ifdef CONFIG_ACPI_NUMA
1909 for_each_possible_cpu(cpu
)
1910 if (cpu_to_node(cpu
) == pgdat
->node_id
)
1911 numa_clear_node(cpu
);
1915 static int check_and_unmap_cpu_on_node(pg_data_t
*pgdat
)
1919 ret
= check_cpu_on_node(pgdat
);
1924 * the node will be offlined when we come here, so we can clear
1925 * the cpu_to_node() now.
1928 unmap_cpu_on_node(pgdat
);
1935 * Offline a node if all memory sections and cpus of the node are removed.
1937 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1938 * and online/offline operations before this call.
1940 void try_offline_node(int nid
)
1942 pg_data_t
*pgdat
= NODE_DATA(nid
);
1943 unsigned long start_pfn
= pgdat
->node_start_pfn
;
1944 unsigned long end_pfn
= start_pfn
+ pgdat
->node_spanned_pages
;
1948 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
1949 unsigned long section_nr
= pfn_to_section_nr(pfn
);
1951 if (!present_section_nr(section_nr
))
1954 if (pfn_to_nid(pfn
) != nid
)
1958 * some memory sections of this node are not removed, and we
1959 * can't offline node now.
1964 if (check_and_unmap_cpu_on_node(pgdat
))
1968 * all memory/cpu of this node are removed, we can offline this
1971 node_set_offline(nid
);
1972 unregister_one_node(nid
);
1974 /* free waittable in each zone */
1975 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
1976 struct zone
*zone
= pgdat
->node_zones
+ i
;
1979 * wait_table may be allocated from boot memory,
1980 * here only free if it's allocated by vmalloc.
1982 if (is_vmalloc_addr(zone
->wait_table
)) {
1983 vfree(zone
->wait_table
);
1984 zone
->wait_table
= NULL
;
1988 EXPORT_SYMBOL(try_offline_node
);
1993 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1994 * and online/offline operations before this call, as required by
1995 * try_offline_node().
1997 void __ref
remove_memory(int nid
, u64 start
, u64 size
)
2001 BUG_ON(check_hotplug_memory_range(start
, size
));
2003 mem_hotplug_begin();
2006 * All memory blocks must be offlined before removing memory. Check
2007 * whether all memory blocks in question are offline and trigger a BUG()
2008 * if this is not the case.
2010 ret
= walk_memory_range(PFN_DOWN(start
), PFN_UP(start
+ size
- 1), NULL
,
2011 check_memblock_offlined_cb
);
2015 /* remove memmap entry */
2016 firmware_map_remove(start
, start
+ size
, "System RAM");
2017 memblock_free(start
, size
);
2018 memblock_remove(start
, size
);
2020 arch_remove_memory(start
, size
);
2022 try_offline_node(nid
);
2026 EXPORT_SYMBOL_GPL(remove_memory
);
2027 #endif /* CONFIG_MEMORY_HOTREMOVE */