jme: Fix device PM wakeup API usage
[linux/fpc-iii.git] / mm / memory_hotplug.c
blob4ec1d4d7521ad343426af86c424545257037beab
1 /*
2 * linux/mm/memory_hotplug.c
4 * Copyright (C)
5 */
7 #include <linux/stddef.h>
8 #include <linux/mm.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>
38 #include "internal.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. */
53 static struct {
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.
60 int refcount;
62 #ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map;
64 #endif
65 } mem_hotplug = {
66 .active_writer = NULL,
67 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
68 .refcount = 0,
69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map = {.name = "mem_hotplug.lock" },
71 #endif
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)
81 might_sleep();
82 if (mem_hotplug.active_writer == current)
83 return;
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)
94 return;
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 static void mem_hotplug_begin(void)
109 mem_hotplug.active_writer = current;
111 memhp_lock_acquire();
112 for (;;) {
113 mutex_lock(&mem_hotplug.lock);
114 if (likely(!mem_hotplug.refcount))
115 break;
116 __set_current_state(TASK_UNINTERRUPTIBLE);
117 mutex_unlock(&mem_hotplug.lock);
118 schedule();
122 static 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);
134 BUG_ON(!res);
136 res->name = "System RAM";
137 res->start = start;
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);
142 kfree(res);
143 res = NULL;
145 return res;
148 static void release_memory_resource(struct resource *res)
150 if (!res)
151 return;
152 release_resource(res);
153 kfree(res);
154 return;
157 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
158 void get_page_bootmem(unsigned long info, struct page *page,
159 unsigned long type)
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)
169 unsigned long type;
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))
226 return;
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;
249 struct page *page;
250 struct zone *zone;
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 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;
313 } else {
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;
330 unsigned long pfn;
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,
343 MEMMAP_HOTPLUG);
344 return 0;
347 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
348 unsigned long start_pfn, unsigned long end_pfn)
350 int ret;
351 unsigned long flags;
352 unsigned long z1_start_pfn;
354 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
355 if (ret)
356 return ret;
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))
362 goto out_fail;
363 /* the move out part must be at the left most of @z2 */
364 if (start_pfn > z2->zone_start_pfn)
365 goto out_fail;
366 /* must included/overlap */
367 if (end_pfn <= z2->zone_start_pfn)
368 goto out_fail;
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;
373 else
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);
383 return 0;
384 out_fail:
385 pgdat_resize_unlock(z1->zone_pgdat, &flags);
386 return -1;
389 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
390 unsigned long start_pfn, unsigned long end_pfn)
392 int ret;
393 unsigned long flags;
394 unsigned long z2_end_pfn;
396 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
397 if (ret)
398 return ret;
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)
404 goto out_fail;
405 /* the move out part mast at the right most of @z1 */
406 if (zone_end_pfn(z1) > end_pfn)
407 goto out_fail;
408 /* must included/overlap */
409 if (start_pfn >= zone_end_pfn(z1))
410 goto out_fail;
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);
415 else
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);
425 return 0;
426 out_fail:
427 pgdat_resize_unlock(z1->zone_pgdat, &flags);
428 return -1;
431 static void 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;
448 int zone_type;
449 unsigned long flags;
450 int ret;
452 zone_type = zone - pgdat->node_zones;
453 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
454 if (ret)
455 return ret;
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);
464 return 0;
467 static int __meminit __add_section(int nid, struct zone *zone,
468 unsigned long phys_start_pfn)
470 int ret;
472 if (pfn_valid(phys_start_pfn))
473 return -EEXIST;
475 ret = sparse_add_one_section(zone, phys_start_pfn);
477 if (ret < 0)
478 return ret;
480 ret = __add_zone(zone, phys_start_pfn);
482 if (ret < 0)
483 return ret;
485 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
489 * Reasonably generic function for adding memory. It is
490 * expected that archs that support memory hotplug will
491 * call this function after deciding the zone to which to
492 * add the new pages.
494 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
495 unsigned long nr_pages)
497 unsigned long i;
498 int err = 0;
499 int start_sec, end_sec;
500 /* during initialize mem_map, align hot-added range to section */
501 start_sec = pfn_to_section_nr(phys_start_pfn);
502 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
504 for (i = start_sec; i <= end_sec; i++) {
505 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
508 * EEXIST is finally dealt with by ioresource collision
509 * check. see add_memory() => register_memory_resource()
510 * Warning will be printed if there is collision.
512 if (err && (err != -EEXIST))
513 break;
514 err = 0;
517 return err;
519 EXPORT_SYMBOL_GPL(__add_pages);
521 #ifdef CONFIG_MEMORY_HOTREMOVE
522 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
523 static int find_smallest_section_pfn(int nid, struct zone *zone,
524 unsigned long start_pfn,
525 unsigned long end_pfn)
527 struct mem_section *ms;
529 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
530 ms = __pfn_to_section(start_pfn);
532 if (unlikely(!valid_section(ms)))
533 continue;
535 if (unlikely(pfn_to_nid(start_pfn) != nid))
536 continue;
538 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
539 continue;
541 return start_pfn;
544 return 0;
547 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
548 static int find_biggest_section_pfn(int nid, struct zone *zone,
549 unsigned long start_pfn,
550 unsigned long end_pfn)
552 struct mem_section *ms;
553 unsigned long pfn;
555 /* pfn is the end pfn of a memory section. */
556 pfn = end_pfn - 1;
557 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
558 ms = __pfn_to_section(pfn);
560 if (unlikely(!valid_section(ms)))
561 continue;
563 if (unlikely(pfn_to_nid(pfn) != nid))
564 continue;
566 if (zone && zone != page_zone(pfn_to_page(pfn)))
567 continue;
569 return pfn;
572 return 0;
575 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
576 unsigned long end_pfn)
578 unsigned long zone_start_pfn = zone->zone_start_pfn;
579 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
580 unsigned long zone_end_pfn = z;
581 unsigned long pfn;
582 struct mem_section *ms;
583 int nid = zone_to_nid(zone);
585 zone_span_writelock(zone);
586 if (zone_start_pfn == start_pfn) {
588 * If the section is smallest section in the zone, it need
589 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
590 * In this case, we find second smallest valid mem_section
591 * for shrinking zone.
593 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
594 zone_end_pfn);
595 if (pfn) {
596 zone->zone_start_pfn = pfn;
597 zone->spanned_pages = zone_end_pfn - pfn;
599 } else if (zone_end_pfn == end_pfn) {
601 * If the section is biggest section in the zone, it need
602 * shrink zone->spanned_pages.
603 * In this case, we find second biggest valid mem_section for
604 * shrinking zone.
606 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
607 start_pfn);
608 if (pfn)
609 zone->spanned_pages = pfn - zone_start_pfn + 1;
613 * The section is not biggest or smallest mem_section in the zone, it
614 * only creates a hole in the zone. So in this case, we need not
615 * change the zone. But perhaps, the zone has only hole data. Thus
616 * it check the zone has only hole or not.
618 pfn = zone_start_pfn;
619 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
620 ms = __pfn_to_section(pfn);
622 if (unlikely(!valid_section(ms)))
623 continue;
625 if (page_zone(pfn_to_page(pfn)) != zone)
626 continue;
628 /* If the section is current section, it continues the loop */
629 if (start_pfn == pfn)
630 continue;
632 /* If we find valid section, we have nothing to do */
633 zone_span_writeunlock(zone);
634 return;
637 /* The zone has no valid section */
638 zone->zone_start_pfn = 0;
639 zone->spanned_pages = 0;
640 zone_span_writeunlock(zone);
643 static void shrink_pgdat_span(struct pglist_data *pgdat,
644 unsigned long start_pfn, unsigned long end_pfn)
646 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
647 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
648 unsigned long pgdat_end_pfn = p;
649 unsigned long pfn;
650 struct mem_section *ms;
651 int nid = pgdat->node_id;
653 if (pgdat_start_pfn == start_pfn) {
655 * If the section is smallest section in the pgdat, it need
656 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
657 * In this case, we find second smallest valid mem_section
658 * for shrinking zone.
660 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
661 pgdat_end_pfn);
662 if (pfn) {
663 pgdat->node_start_pfn = pfn;
664 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
666 } else if (pgdat_end_pfn == end_pfn) {
668 * If the section is biggest section in the pgdat, it need
669 * shrink pgdat->node_spanned_pages.
670 * In this case, we find second biggest valid mem_section for
671 * shrinking zone.
673 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
674 start_pfn);
675 if (pfn)
676 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
680 * If the section is not biggest or smallest mem_section in the pgdat,
681 * it only creates a hole in the pgdat. So in this case, we need not
682 * change the pgdat.
683 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
684 * has only hole or not.
686 pfn = pgdat_start_pfn;
687 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
688 ms = __pfn_to_section(pfn);
690 if (unlikely(!valid_section(ms)))
691 continue;
693 if (pfn_to_nid(pfn) != nid)
694 continue;
696 /* If the section is current section, it continues the loop */
697 if (start_pfn == pfn)
698 continue;
700 /* If we find valid section, we have nothing to do */
701 return;
704 /* The pgdat has no valid section */
705 pgdat->node_start_pfn = 0;
706 pgdat->node_spanned_pages = 0;
709 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
711 struct pglist_data *pgdat = zone->zone_pgdat;
712 int nr_pages = PAGES_PER_SECTION;
713 int zone_type;
714 unsigned long flags;
716 zone_type = zone - pgdat->node_zones;
718 pgdat_resize_lock(zone->zone_pgdat, &flags);
719 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
720 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
721 pgdat_resize_unlock(zone->zone_pgdat, &flags);
724 static int __remove_section(struct zone *zone, struct mem_section *ms)
726 unsigned long start_pfn;
727 int scn_nr;
728 int ret = -EINVAL;
730 if (!valid_section(ms))
731 return ret;
733 ret = unregister_memory_section(ms);
734 if (ret)
735 return ret;
737 scn_nr = __section_nr(ms);
738 start_pfn = section_nr_to_pfn(scn_nr);
739 __remove_zone(zone, start_pfn);
741 sparse_remove_one_section(zone, ms);
742 return 0;
746 * __remove_pages() - remove sections of pages from a zone
747 * @zone: zone from which pages need to be removed
748 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
749 * @nr_pages: number of pages to remove (must be multiple of section size)
751 * Generic helper function to remove section mappings and sysfs entries
752 * for the section of the memory we are removing. Caller needs to make
753 * sure that pages are marked reserved and zones are adjust properly by
754 * calling offline_pages().
756 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
757 unsigned long nr_pages)
759 unsigned long i;
760 int sections_to_remove;
761 resource_size_t start, size;
762 int ret = 0;
765 * We can only remove entire sections
767 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
768 BUG_ON(nr_pages % PAGES_PER_SECTION);
770 start = phys_start_pfn << PAGE_SHIFT;
771 size = nr_pages * PAGE_SIZE;
772 ret = release_mem_region_adjustable(&iomem_resource, start, size);
773 if (ret) {
774 resource_size_t endres = start + size - 1;
776 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
777 &start, &endres, ret);
780 sections_to_remove = nr_pages / PAGES_PER_SECTION;
781 for (i = 0; i < sections_to_remove; i++) {
782 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
783 ret = __remove_section(zone, __pfn_to_section(pfn));
784 if (ret)
785 break;
787 return ret;
789 EXPORT_SYMBOL_GPL(__remove_pages);
790 #endif /* CONFIG_MEMORY_HOTREMOVE */
792 int set_online_page_callback(online_page_callback_t callback)
794 int rc = -EINVAL;
796 get_online_mems();
797 mutex_lock(&online_page_callback_lock);
799 if (online_page_callback == generic_online_page) {
800 online_page_callback = callback;
801 rc = 0;
804 mutex_unlock(&online_page_callback_lock);
805 put_online_mems();
807 return rc;
809 EXPORT_SYMBOL_GPL(set_online_page_callback);
811 int restore_online_page_callback(online_page_callback_t callback)
813 int rc = -EINVAL;
815 get_online_mems();
816 mutex_lock(&online_page_callback_lock);
818 if (online_page_callback == callback) {
819 online_page_callback = generic_online_page;
820 rc = 0;
823 mutex_unlock(&online_page_callback_lock);
824 put_online_mems();
826 return rc;
828 EXPORT_SYMBOL_GPL(restore_online_page_callback);
830 void __online_page_set_limits(struct page *page)
833 EXPORT_SYMBOL_GPL(__online_page_set_limits);
835 void __online_page_increment_counters(struct page *page)
837 adjust_managed_page_count(page, 1);
839 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
841 void __online_page_free(struct page *page)
843 __free_reserved_page(page);
845 EXPORT_SYMBOL_GPL(__online_page_free);
847 static void generic_online_page(struct page *page)
849 __online_page_set_limits(page);
850 __online_page_increment_counters(page);
851 __online_page_free(page);
854 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
855 void *arg)
857 unsigned long i;
858 unsigned long onlined_pages = *(unsigned long *)arg;
859 struct page *page;
860 if (PageReserved(pfn_to_page(start_pfn)))
861 for (i = 0; i < nr_pages; i++) {
862 page = pfn_to_page(start_pfn + i);
863 (*online_page_callback)(page);
864 onlined_pages++;
866 *(unsigned long *)arg = onlined_pages;
867 return 0;
870 #ifdef CONFIG_MOVABLE_NODE
872 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
873 * normal memory.
875 static bool can_online_high_movable(struct zone *zone)
877 return true;
879 #else /* CONFIG_MOVABLE_NODE */
880 /* ensure every online node has NORMAL memory */
881 static bool can_online_high_movable(struct zone *zone)
883 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
885 #endif /* CONFIG_MOVABLE_NODE */
887 /* check which state of node_states will be changed when online memory */
888 static void node_states_check_changes_online(unsigned long nr_pages,
889 struct zone *zone, struct memory_notify *arg)
891 int nid = zone_to_nid(zone);
892 enum zone_type zone_last = ZONE_NORMAL;
895 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
896 * contains nodes which have zones of 0...ZONE_NORMAL,
897 * set zone_last to ZONE_NORMAL.
899 * If we don't have HIGHMEM nor movable node,
900 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
901 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
903 if (N_MEMORY == N_NORMAL_MEMORY)
904 zone_last = ZONE_MOVABLE;
907 * if the memory to be online is in a zone of 0...zone_last, and
908 * the zones of 0...zone_last don't have memory before online, we will
909 * need to set the node to node_states[N_NORMAL_MEMORY] after
910 * the memory is online.
912 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
913 arg->status_change_nid_normal = nid;
914 else
915 arg->status_change_nid_normal = -1;
917 #ifdef CONFIG_HIGHMEM
919 * If we have movable node, node_states[N_HIGH_MEMORY]
920 * contains nodes which have zones of 0...ZONE_HIGHMEM,
921 * set zone_last to ZONE_HIGHMEM.
923 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
924 * contains nodes which have zones of 0...ZONE_MOVABLE,
925 * set zone_last to ZONE_MOVABLE.
927 zone_last = ZONE_HIGHMEM;
928 if (N_MEMORY == N_HIGH_MEMORY)
929 zone_last = ZONE_MOVABLE;
931 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
932 arg->status_change_nid_high = nid;
933 else
934 arg->status_change_nid_high = -1;
935 #else
936 arg->status_change_nid_high = arg->status_change_nid_normal;
937 #endif
940 * if the node don't have memory befor online, we will need to
941 * set the node to node_states[N_MEMORY] after the memory
942 * is online.
944 if (!node_state(nid, N_MEMORY))
945 arg->status_change_nid = nid;
946 else
947 arg->status_change_nid = -1;
950 static void node_states_set_node(int node, struct memory_notify *arg)
952 if (arg->status_change_nid_normal >= 0)
953 node_set_state(node, N_NORMAL_MEMORY);
955 if (arg->status_change_nid_high >= 0)
956 node_set_state(node, N_HIGH_MEMORY);
958 node_set_state(node, N_MEMORY);
962 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
964 unsigned long flags;
965 unsigned long onlined_pages = 0;
966 struct zone *zone;
967 int need_zonelists_rebuild = 0;
968 int nid;
969 int ret;
970 struct memory_notify arg;
972 mem_hotplug_begin();
974 * This doesn't need a lock to do pfn_to_page().
975 * The section can't be removed here because of the
976 * memory_block->state_mutex.
978 zone = page_zone(pfn_to_page(pfn));
980 ret = -EINVAL;
981 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
982 !can_online_high_movable(zone))
983 goto out;
985 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
986 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
987 goto out;
989 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
990 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
991 goto out;
994 /* Previous code may changed the zone of the pfn range */
995 zone = page_zone(pfn_to_page(pfn));
997 arg.start_pfn = pfn;
998 arg.nr_pages = nr_pages;
999 node_states_check_changes_online(nr_pages, zone, &arg);
1001 nid = pfn_to_nid(pfn);
1003 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1004 ret = notifier_to_errno(ret);
1005 if (ret) {
1006 memory_notify(MEM_CANCEL_ONLINE, &arg);
1007 goto out;
1010 * If this zone is not populated, then it is not in zonelist.
1011 * This means the page allocator ignores this zone.
1012 * So, zonelist must be updated after online.
1014 mutex_lock(&zonelists_mutex);
1015 if (!populated_zone(zone)) {
1016 need_zonelists_rebuild = 1;
1017 build_all_zonelists(NULL, zone);
1020 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1021 online_pages_range);
1022 if (ret) {
1023 if (need_zonelists_rebuild)
1024 zone_pcp_reset(zone);
1025 mutex_unlock(&zonelists_mutex);
1026 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1027 (unsigned long long) pfn << PAGE_SHIFT,
1028 (((unsigned long long) pfn + nr_pages)
1029 << PAGE_SHIFT) - 1);
1030 memory_notify(MEM_CANCEL_ONLINE, &arg);
1031 goto out;
1034 zone->present_pages += onlined_pages;
1036 pgdat_resize_lock(zone->zone_pgdat, &flags);
1037 zone->zone_pgdat->node_present_pages += onlined_pages;
1038 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1040 if (onlined_pages) {
1041 node_states_set_node(zone_to_nid(zone), &arg);
1042 if (need_zonelists_rebuild)
1043 build_all_zonelists(NULL, NULL);
1044 else
1045 zone_pcp_update(zone);
1048 mutex_unlock(&zonelists_mutex);
1050 init_per_zone_wmark_min();
1052 if (onlined_pages)
1053 kswapd_run(zone_to_nid(zone));
1055 vm_total_pages = nr_free_pagecache_pages();
1057 writeback_set_ratelimit();
1059 if (onlined_pages)
1060 memory_notify(MEM_ONLINE, &arg);
1061 out:
1062 mem_hotplug_done();
1063 return ret;
1065 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1067 static void reset_node_present_pages(pg_data_t *pgdat)
1069 struct zone *z;
1071 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1072 z->present_pages = 0;
1074 pgdat->node_present_pages = 0;
1077 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1078 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1080 struct pglist_data *pgdat;
1081 unsigned long zones_size[MAX_NR_ZONES] = {0};
1082 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1083 unsigned long start_pfn = PFN_DOWN(start);
1085 pgdat = NODE_DATA(nid);
1086 if (!pgdat) {
1087 pgdat = arch_alloc_nodedata(nid);
1088 if (!pgdat)
1089 return NULL;
1091 arch_refresh_nodedata(nid, pgdat);
1092 } else {
1093 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1094 pgdat->nr_zones = 0;
1095 pgdat->classzone_idx = 0;
1098 /* we can use NODE_DATA(nid) from here */
1100 /* init node's zones as empty zones, we don't have any present pages.*/
1101 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1104 * The node we allocated has no zone fallback lists. For avoiding
1105 * to access not-initialized zonelist, build here.
1107 mutex_lock(&zonelists_mutex);
1108 build_all_zonelists(pgdat, NULL);
1109 mutex_unlock(&zonelists_mutex);
1112 * zone->managed_pages is set to an approximate value in
1113 * free_area_init_core(), which will cause
1114 * /sys/device/system/node/nodeX/meminfo has wrong data.
1115 * So reset it to 0 before any memory is onlined.
1117 reset_node_managed_pages(pgdat);
1120 * When memory is hot-added, all the memory is in offline state. So
1121 * clear all zones' present_pages because they will be updated in
1122 * online_pages() and offline_pages().
1124 reset_node_present_pages(pgdat);
1126 return pgdat;
1129 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1131 arch_refresh_nodedata(nid, NULL);
1132 arch_free_nodedata(pgdat);
1133 return;
1138 * try_online_node - online a node if offlined
1140 * called by cpu_up() to online a node without onlined memory.
1142 int try_online_node(int nid)
1144 pg_data_t *pgdat;
1145 int ret;
1147 if (node_online(nid))
1148 return 0;
1150 mem_hotplug_begin();
1151 pgdat = hotadd_new_pgdat(nid, 0);
1152 if (!pgdat) {
1153 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1154 ret = -ENOMEM;
1155 goto out;
1157 node_set_online(nid);
1158 ret = register_one_node(nid);
1159 BUG_ON(ret);
1161 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1162 mutex_lock(&zonelists_mutex);
1163 build_all_zonelists(NULL, NULL);
1164 mutex_unlock(&zonelists_mutex);
1167 out:
1168 mem_hotplug_done();
1169 return ret;
1172 static int check_hotplug_memory_range(u64 start, u64 size)
1174 u64 start_pfn = PFN_DOWN(start);
1175 u64 nr_pages = size >> PAGE_SHIFT;
1177 /* Memory range must be aligned with section */
1178 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1179 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1180 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1181 (unsigned long long)start,
1182 (unsigned long long)size);
1183 return -EINVAL;
1186 return 0;
1189 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1190 int __ref add_memory(int nid, u64 start, u64 size)
1192 pg_data_t *pgdat = NULL;
1193 bool new_pgdat;
1194 bool new_node;
1195 struct resource *res;
1196 int ret;
1198 ret = check_hotplug_memory_range(start, size);
1199 if (ret)
1200 return ret;
1202 res = register_memory_resource(start, size);
1203 ret = -EEXIST;
1204 if (!res)
1205 return ret;
1207 { /* Stupid hack to suppress address-never-null warning */
1208 void *p = NODE_DATA(nid);
1209 new_pgdat = !p;
1212 mem_hotplug_begin();
1214 new_node = !node_online(nid);
1215 if (new_node) {
1216 pgdat = hotadd_new_pgdat(nid, start);
1217 ret = -ENOMEM;
1218 if (!pgdat)
1219 goto error;
1222 /* call arch's memory hotadd */
1223 ret = arch_add_memory(nid, start, size);
1225 if (ret < 0)
1226 goto error;
1228 /* we online node here. we can't roll back from here. */
1229 node_set_online(nid);
1231 if (new_node) {
1232 ret = register_one_node(nid);
1234 * If sysfs file of new node can't create, cpu on the node
1235 * can't be hot-added. There is no rollback way now.
1236 * So, check by BUG_ON() to catch it reluctantly..
1238 BUG_ON(ret);
1241 /* create new memmap entry */
1242 firmware_map_add_hotplug(start, start + size, "System RAM");
1244 goto out;
1246 error:
1247 /* rollback pgdat allocation and others */
1248 if (new_pgdat)
1249 rollback_node_hotadd(nid, pgdat);
1250 release_memory_resource(res);
1252 out:
1253 mem_hotplug_done();
1254 return ret;
1256 EXPORT_SYMBOL_GPL(add_memory);
1258 #ifdef CONFIG_MEMORY_HOTREMOVE
1260 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1261 * set and the size of the free page is given by page_order(). Using this,
1262 * the function determines if the pageblock contains only free pages.
1263 * Due to buddy contraints, a free page at least the size of a pageblock will
1264 * be located at the start of the pageblock
1266 static inline int pageblock_free(struct page *page)
1268 return PageBuddy(page) && page_order(page) >= pageblock_order;
1271 /* Return the start of the next active pageblock after a given page */
1272 static struct page *next_active_pageblock(struct page *page)
1274 /* Ensure the starting page is pageblock-aligned */
1275 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1277 /* If the entire pageblock is free, move to the end of free page */
1278 if (pageblock_free(page)) {
1279 int order;
1280 /* be careful. we don't have locks, page_order can be changed.*/
1281 order = page_order(page);
1282 if ((order < MAX_ORDER) && (order >= pageblock_order))
1283 return page + (1 << order);
1286 return page + pageblock_nr_pages;
1289 /* Checks if this range of memory is likely to be hot-removable. */
1290 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1292 struct page *page = pfn_to_page(start_pfn);
1293 struct page *end_page = page + nr_pages;
1295 /* Check the starting page of each pageblock within the range */
1296 for (; page < end_page; page = next_active_pageblock(page)) {
1297 if (!is_pageblock_removable_nolock(page))
1298 return 0;
1299 cond_resched();
1302 /* All pageblocks in the memory block are likely to be hot-removable */
1303 return 1;
1307 * Confirm all pages in a range [start, end) is belongs to the same zone.
1309 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1311 unsigned long pfn, sec_end_pfn;
1312 struct zone *zone = NULL;
1313 struct page *page;
1314 int i;
1315 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
1316 pfn < end_pfn;
1317 pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1318 /* Make sure the memory section is present first */
1319 if (!present_section_nr(pfn_to_section_nr(pfn)))
1320 continue;
1321 for (; pfn < sec_end_pfn && pfn < end_pfn;
1322 pfn += MAX_ORDER_NR_PAGES) {
1323 i = 0;
1324 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1325 while ((i < MAX_ORDER_NR_PAGES) &&
1326 !pfn_valid_within(pfn + i))
1327 i++;
1328 if (i == MAX_ORDER_NR_PAGES)
1329 continue;
1330 page = pfn_to_page(pfn + i);
1331 if (zone && page_zone(page) != zone)
1332 return 0;
1333 zone = page_zone(page);
1336 return 1;
1340 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1341 * and hugepages). We scan pfn because it's much easier than scanning over
1342 * linked list. This function returns the pfn of the first found movable
1343 * page if it's found, otherwise 0.
1345 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1347 unsigned long pfn;
1348 struct page *page;
1349 for (pfn = start; pfn < end; pfn++) {
1350 if (pfn_valid(pfn)) {
1351 page = pfn_to_page(pfn);
1352 if (PageLRU(page))
1353 return pfn;
1354 if (PageHuge(page)) {
1355 if (is_hugepage_active(page))
1356 return pfn;
1357 else
1358 pfn = round_up(pfn + 1,
1359 1 << compound_order(page)) - 1;
1363 return 0;
1366 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1367 static int
1368 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1370 unsigned long pfn;
1371 struct page *page;
1372 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1373 int not_managed = 0;
1374 int ret = 0;
1375 LIST_HEAD(source);
1377 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1378 if (!pfn_valid(pfn))
1379 continue;
1380 page = pfn_to_page(pfn);
1382 if (PageHuge(page)) {
1383 struct page *head = compound_head(page);
1384 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1385 if (compound_order(head) > PFN_SECTION_SHIFT) {
1386 ret = -EBUSY;
1387 break;
1389 if (isolate_huge_page(page, &source))
1390 move_pages -= 1 << compound_order(head);
1391 continue;
1394 if (!get_page_unless_zero(page))
1395 continue;
1397 * We can skip free pages. And we can only deal with pages on
1398 * LRU.
1400 ret = isolate_lru_page(page);
1401 if (!ret) { /* Success */
1402 put_page(page);
1403 list_add_tail(&page->lru, &source);
1404 move_pages--;
1405 inc_zone_page_state(page, NR_ISOLATED_ANON +
1406 page_is_file_cache(page));
1408 } else {
1409 #ifdef CONFIG_DEBUG_VM
1410 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1411 pfn);
1412 dump_page(page, "failed to remove from LRU");
1413 #endif
1414 put_page(page);
1415 /* Because we don't have big zone->lock. we should
1416 check this again here. */
1417 if (page_count(page)) {
1418 not_managed++;
1419 ret = -EBUSY;
1420 break;
1424 if (!list_empty(&source)) {
1425 if (not_managed) {
1426 putback_movable_pages(&source);
1427 goto out;
1431 * alloc_migrate_target should be improooooved!!
1432 * migrate_pages returns # of failed pages.
1434 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
1435 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1436 if (ret)
1437 putback_movable_pages(&source);
1439 out:
1440 return ret;
1444 * remove from free_area[] and mark all as Reserved.
1446 static int
1447 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1448 void *data)
1450 __offline_isolated_pages(start, start + nr_pages);
1451 return 0;
1454 static void
1455 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1457 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1458 offline_isolated_pages_cb);
1462 * Check all pages in range, recoreded as memory resource, are isolated.
1464 static int
1465 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1466 void *data)
1468 int ret;
1469 long offlined = *(long *)data;
1470 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1471 offlined = nr_pages;
1472 if (!ret)
1473 *(long *)data += offlined;
1474 return ret;
1477 static long
1478 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1480 long offlined = 0;
1481 int ret;
1483 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1484 check_pages_isolated_cb);
1485 if (ret < 0)
1486 offlined = (long)ret;
1487 return offlined;
1490 #ifdef CONFIG_MOVABLE_NODE
1492 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1493 * normal memory.
1495 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1497 return true;
1499 #else /* CONFIG_MOVABLE_NODE */
1500 /* ensure the node has NORMAL memory if it is still online */
1501 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1503 struct pglist_data *pgdat = zone->zone_pgdat;
1504 unsigned long present_pages = 0;
1505 enum zone_type zt;
1507 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1508 present_pages += pgdat->node_zones[zt].present_pages;
1510 if (present_pages > nr_pages)
1511 return true;
1513 present_pages = 0;
1514 for (; zt <= ZONE_MOVABLE; zt++)
1515 present_pages += pgdat->node_zones[zt].present_pages;
1518 * we can't offline the last normal memory until all
1519 * higher memory is offlined.
1521 return present_pages == 0;
1523 #endif /* CONFIG_MOVABLE_NODE */
1525 static int __init cmdline_parse_movable_node(char *p)
1527 #ifdef CONFIG_MOVABLE_NODE
1529 * Memory used by the kernel cannot be hot-removed because Linux
1530 * cannot migrate the kernel pages. When memory hotplug is
1531 * enabled, we should prevent memblock from allocating memory
1532 * for the kernel.
1534 * ACPI SRAT records all hotpluggable memory ranges. But before
1535 * SRAT is parsed, we don't know about it.
1537 * The kernel image is loaded into memory at very early time. We
1538 * cannot prevent this anyway. So on NUMA system, we set any
1539 * node the kernel resides in as un-hotpluggable.
1541 * Since on modern servers, one node could have double-digit
1542 * gigabytes memory, we can assume the memory around the kernel
1543 * image is also un-hotpluggable. So before SRAT is parsed, just
1544 * allocate memory near the kernel image to try the best to keep
1545 * the kernel away from hotpluggable memory.
1547 memblock_set_bottom_up(true);
1548 movable_node_enabled = true;
1549 #else
1550 pr_warn("movable_node option not supported\n");
1551 #endif
1552 return 0;
1554 early_param("movable_node", cmdline_parse_movable_node);
1556 /* check which state of node_states will be changed when offline memory */
1557 static void node_states_check_changes_offline(unsigned long nr_pages,
1558 struct zone *zone, struct memory_notify *arg)
1560 struct pglist_data *pgdat = zone->zone_pgdat;
1561 unsigned long present_pages = 0;
1562 enum zone_type zt, zone_last = ZONE_NORMAL;
1565 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1566 * contains nodes which have zones of 0...ZONE_NORMAL,
1567 * set zone_last to ZONE_NORMAL.
1569 * If we don't have HIGHMEM nor movable node,
1570 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1571 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1573 if (N_MEMORY == N_NORMAL_MEMORY)
1574 zone_last = ZONE_MOVABLE;
1577 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1578 * If the memory to be offline is in a zone of 0...zone_last,
1579 * and it is the last present memory, 0...zone_last will
1580 * become empty after offline , thus we can determind we will
1581 * need to clear the node from node_states[N_NORMAL_MEMORY].
1583 for (zt = 0; zt <= zone_last; zt++)
1584 present_pages += pgdat->node_zones[zt].present_pages;
1585 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1586 arg->status_change_nid_normal = zone_to_nid(zone);
1587 else
1588 arg->status_change_nid_normal = -1;
1590 #ifdef CONFIG_HIGHMEM
1592 * If we have movable node, node_states[N_HIGH_MEMORY]
1593 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1594 * set zone_last to ZONE_HIGHMEM.
1596 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1597 * contains nodes which have zones of 0...ZONE_MOVABLE,
1598 * set zone_last to ZONE_MOVABLE.
1600 zone_last = ZONE_HIGHMEM;
1601 if (N_MEMORY == N_HIGH_MEMORY)
1602 zone_last = ZONE_MOVABLE;
1604 for (; zt <= zone_last; zt++)
1605 present_pages += pgdat->node_zones[zt].present_pages;
1606 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1607 arg->status_change_nid_high = zone_to_nid(zone);
1608 else
1609 arg->status_change_nid_high = -1;
1610 #else
1611 arg->status_change_nid_high = arg->status_change_nid_normal;
1612 #endif
1615 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1617 zone_last = ZONE_MOVABLE;
1620 * check whether node_states[N_HIGH_MEMORY] will be changed
1621 * If we try to offline the last present @nr_pages from the node,
1622 * we can determind we will need to clear the node from
1623 * node_states[N_HIGH_MEMORY].
1625 for (; zt <= zone_last; zt++)
1626 present_pages += pgdat->node_zones[zt].present_pages;
1627 if (nr_pages >= present_pages)
1628 arg->status_change_nid = zone_to_nid(zone);
1629 else
1630 arg->status_change_nid = -1;
1633 static void node_states_clear_node(int node, struct memory_notify *arg)
1635 if (arg->status_change_nid_normal >= 0)
1636 node_clear_state(node, N_NORMAL_MEMORY);
1638 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1639 (arg->status_change_nid_high >= 0))
1640 node_clear_state(node, N_HIGH_MEMORY);
1642 if ((N_MEMORY != N_HIGH_MEMORY) &&
1643 (arg->status_change_nid >= 0))
1644 node_clear_state(node, N_MEMORY);
1647 static int __ref __offline_pages(unsigned long start_pfn,
1648 unsigned long end_pfn, unsigned long timeout)
1650 unsigned long pfn, nr_pages, expire;
1651 long offlined_pages;
1652 int ret, drain, retry_max, node;
1653 unsigned long flags;
1654 struct zone *zone;
1655 struct memory_notify arg;
1657 /* at least, alignment against pageblock is necessary */
1658 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1659 return -EINVAL;
1660 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1661 return -EINVAL;
1662 /* This makes hotplug much easier...and readable.
1663 we assume this for now. .*/
1664 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1665 return -EINVAL;
1667 mem_hotplug_begin();
1669 zone = page_zone(pfn_to_page(start_pfn));
1670 node = zone_to_nid(zone);
1671 nr_pages = end_pfn - start_pfn;
1673 ret = -EINVAL;
1674 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1675 goto out;
1677 /* set above range as isolated */
1678 ret = start_isolate_page_range(start_pfn, end_pfn,
1679 MIGRATE_MOVABLE, true);
1680 if (ret)
1681 goto out;
1683 arg.start_pfn = start_pfn;
1684 arg.nr_pages = nr_pages;
1685 node_states_check_changes_offline(nr_pages, zone, &arg);
1687 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1688 ret = notifier_to_errno(ret);
1689 if (ret)
1690 goto failed_removal;
1692 pfn = start_pfn;
1693 expire = jiffies + timeout;
1694 drain = 0;
1695 retry_max = 5;
1696 repeat:
1697 /* start memory hot removal */
1698 ret = -EAGAIN;
1699 if (time_after(jiffies, expire))
1700 goto failed_removal;
1701 ret = -EINTR;
1702 if (signal_pending(current))
1703 goto failed_removal;
1704 ret = 0;
1705 if (drain) {
1706 lru_add_drain_all();
1707 cond_resched();
1708 drain_all_pages();
1711 pfn = scan_movable_pages(start_pfn, end_pfn);
1712 if (pfn) { /* We have movable pages */
1713 ret = do_migrate_range(pfn, end_pfn);
1714 if (!ret) {
1715 drain = 1;
1716 goto repeat;
1717 } else {
1718 if (ret < 0)
1719 if (--retry_max == 0)
1720 goto failed_removal;
1721 yield();
1722 drain = 1;
1723 goto repeat;
1726 /* drain all zone's lru pagevec, this is asynchronous... */
1727 lru_add_drain_all();
1728 yield();
1729 /* drain pcp pages, this is synchronous. */
1730 drain_all_pages();
1732 * dissolve free hugepages in the memory block before doing offlining
1733 * actually in order to make hugetlbfs's object counting consistent.
1735 dissolve_free_huge_pages(start_pfn, end_pfn);
1736 /* check again */
1737 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1738 if (offlined_pages < 0) {
1739 ret = -EBUSY;
1740 goto failed_removal;
1742 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1743 /* Ok, all of our target is isolated.
1744 We cannot do rollback at this point. */
1745 offline_isolated_pages(start_pfn, end_pfn);
1746 /* reset pagetype flags and makes migrate type to be MOVABLE */
1747 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1748 /* removal success */
1749 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1750 zone->present_pages -= offlined_pages;
1752 pgdat_resize_lock(zone->zone_pgdat, &flags);
1753 zone->zone_pgdat->node_present_pages -= offlined_pages;
1754 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1756 init_per_zone_wmark_min();
1758 if (!populated_zone(zone)) {
1759 zone_pcp_reset(zone);
1760 mutex_lock(&zonelists_mutex);
1761 build_all_zonelists(NULL, NULL);
1762 mutex_unlock(&zonelists_mutex);
1763 } else
1764 zone_pcp_update(zone);
1766 node_states_clear_node(node, &arg);
1767 if (arg.status_change_nid >= 0)
1768 kswapd_stop(node);
1770 vm_total_pages = nr_free_pagecache_pages();
1771 writeback_set_ratelimit();
1773 memory_notify(MEM_OFFLINE, &arg);
1774 mem_hotplug_done();
1775 return 0;
1777 failed_removal:
1778 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1779 (unsigned long long) start_pfn << PAGE_SHIFT,
1780 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1781 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1782 /* pushback to free area */
1783 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1785 out:
1786 mem_hotplug_done();
1787 return ret;
1790 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1792 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1794 #endif /* CONFIG_MEMORY_HOTREMOVE */
1797 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1798 * @start_pfn: start pfn of the memory range
1799 * @end_pfn: end pfn of the memory range
1800 * @arg: argument passed to func
1801 * @func: callback for each memory section walked
1803 * This function walks through all present mem sections in range
1804 * [start_pfn, end_pfn) and call func on each mem section.
1806 * Returns the return value of func.
1808 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1809 void *arg, int (*func)(struct memory_block *, void *))
1811 struct memory_block *mem = NULL;
1812 struct mem_section *section;
1813 unsigned long pfn, section_nr;
1814 int ret;
1816 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1817 section_nr = pfn_to_section_nr(pfn);
1818 if (!present_section_nr(section_nr))
1819 continue;
1821 section = __nr_to_section(section_nr);
1822 /* same memblock? */
1823 if (mem)
1824 if ((section_nr >= mem->start_section_nr) &&
1825 (section_nr <= mem->end_section_nr))
1826 continue;
1828 mem = find_memory_block_hinted(section, mem);
1829 if (!mem)
1830 continue;
1832 ret = func(mem, arg);
1833 if (ret) {
1834 kobject_put(&mem->dev.kobj);
1835 return ret;
1839 if (mem)
1840 kobject_put(&mem->dev.kobj);
1842 return 0;
1845 #ifdef CONFIG_MEMORY_HOTREMOVE
1846 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1848 int ret = !is_memblock_offlined(mem);
1850 if (unlikely(ret)) {
1851 phys_addr_t beginpa, endpa;
1853 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1854 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1855 pr_warn("removing memory fails, because memory "
1856 "[%pa-%pa] is onlined\n",
1857 &beginpa, &endpa);
1860 return ret;
1863 static int check_cpu_on_node(pg_data_t *pgdat)
1865 int cpu;
1867 for_each_present_cpu(cpu) {
1868 if (cpu_to_node(cpu) == pgdat->node_id)
1870 * the cpu on this node isn't removed, and we can't
1871 * offline this node.
1873 return -EBUSY;
1876 return 0;
1879 static void unmap_cpu_on_node(pg_data_t *pgdat)
1881 #ifdef CONFIG_ACPI_NUMA
1882 int cpu;
1884 for_each_possible_cpu(cpu)
1885 if (cpu_to_node(cpu) == pgdat->node_id)
1886 numa_clear_node(cpu);
1887 #endif
1890 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1892 int ret;
1894 ret = check_cpu_on_node(pgdat);
1895 if (ret)
1896 return ret;
1899 * the node will be offlined when we come here, so we can clear
1900 * the cpu_to_node() now.
1903 unmap_cpu_on_node(pgdat);
1904 return 0;
1908 * try_offline_node
1910 * Offline a node if all memory sections and cpus of the node are removed.
1912 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1913 * and online/offline operations before this call.
1915 void try_offline_node(int nid)
1917 pg_data_t *pgdat = NODE_DATA(nid);
1918 unsigned long start_pfn = pgdat->node_start_pfn;
1919 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
1920 unsigned long pfn;
1921 struct page *pgdat_page = virt_to_page(pgdat);
1922 int i;
1924 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1925 unsigned long section_nr = pfn_to_section_nr(pfn);
1927 if (!present_section_nr(section_nr))
1928 continue;
1930 if (pfn_to_nid(pfn) != nid)
1931 continue;
1934 * some memory sections of this node are not removed, and we
1935 * can't offline node now.
1937 return;
1940 if (check_and_unmap_cpu_on_node(pgdat))
1941 return;
1944 * all memory/cpu of this node are removed, we can offline this
1945 * node now.
1947 node_set_offline(nid);
1948 unregister_one_node(nid);
1950 if (!PageSlab(pgdat_page) && !PageCompound(pgdat_page))
1951 /* node data is allocated from boot memory */
1952 return;
1954 /* free waittable in each zone */
1955 for (i = 0; i < MAX_NR_ZONES; i++) {
1956 struct zone *zone = pgdat->node_zones + i;
1959 * wait_table may be allocated from boot memory,
1960 * here only free if it's allocated by vmalloc.
1962 if (is_vmalloc_addr(zone->wait_table)) {
1963 vfree(zone->wait_table);
1964 zone->wait_table = NULL;
1968 EXPORT_SYMBOL(try_offline_node);
1971 * remove_memory
1973 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1974 * and online/offline operations before this call, as required by
1975 * try_offline_node().
1977 void __ref remove_memory(int nid, u64 start, u64 size)
1979 int ret;
1981 BUG_ON(check_hotplug_memory_range(start, size));
1983 mem_hotplug_begin();
1986 * All memory blocks must be offlined before removing memory. Check
1987 * whether all memory blocks in question are offline and trigger a BUG()
1988 * if this is not the case.
1990 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1991 check_memblock_offlined_cb);
1992 if (ret)
1993 BUG();
1995 /* remove memmap entry */
1996 firmware_map_remove(start, start + size, "System RAM");
1998 arch_remove_memory(start, size);
2000 try_offline_node(nid);
2002 mem_hotplug_done();
2004 EXPORT_SYMBOL_GPL(remove_memory);
2005 #endif /* CONFIG_MEMORY_HOTREMOVE */