net: dsa: mt7530: set CPU port to fallback mode
[linux/fpc-iii.git] / mm / memory_hotplug.c
blobaae7ff4856711a59aaa51c0d5260c8ea511325ea
1 /*
2 * linux/mm/memory_hotplug.c
4 * Copyright (C)
5 */
7 #include <linux/stddef.h>
8 #include <linux/mm.h>
9 #include <linux/sched/signal.h>
10 #include <linux/swap.h>
11 #include <linux/interrupt.h>
12 #include <linux/pagemap.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
15 #include <linux/pagevec.h>
16 #include <linux/writeback.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/memremap.h>
22 #include <linux/memory_hotplug.h>
23 #include <linux/highmem.h>
24 #include <linux/vmalloc.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/migrate.h>
28 #include <linux/page-isolation.h>
29 #include <linux/pfn.h>
30 #include <linux/suspend.h>
31 #include <linux/mm_inline.h>
32 #include <linux/firmware-map.h>
33 #include <linux/stop_machine.h>
34 #include <linux/hugetlb.h>
35 #include <linux/memblock.h>
36 #include <linux/bootmem.h>
37 #include <linux/compaction.h>
38 #include <linux/rmap.h>
40 #include <asm/tlbflush.h>
42 #include "internal.h"
45 * online_page_callback contains pointer to current page onlining function.
46 * Initially it is generic_online_page(). If it is required it could be
47 * changed by calling set_online_page_callback() for callback registration
48 * and restore_online_page_callback() for generic callback restore.
51 static void generic_online_page(struct page *page);
53 static online_page_callback_t online_page_callback = generic_online_page;
54 static DEFINE_MUTEX(online_page_callback_lock);
56 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
58 void get_online_mems(void)
60 percpu_down_read(&mem_hotplug_lock);
63 void put_online_mems(void)
65 percpu_up_read(&mem_hotplug_lock);
68 bool movable_node_enabled = false;
70 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
71 bool memhp_auto_online;
72 #else
73 bool memhp_auto_online = true;
74 #endif
75 EXPORT_SYMBOL_GPL(memhp_auto_online);
77 static int __init setup_memhp_default_state(char *str)
79 if (!strcmp(str, "online"))
80 memhp_auto_online = true;
81 else if (!strcmp(str, "offline"))
82 memhp_auto_online = false;
84 return 1;
86 __setup("memhp_default_state=", setup_memhp_default_state);
88 void mem_hotplug_begin(void)
90 cpus_read_lock();
91 percpu_down_write(&mem_hotplug_lock);
94 void mem_hotplug_done(void)
96 percpu_up_write(&mem_hotplug_lock);
97 cpus_read_unlock();
100 /* add this memory to iomem resource */
101 static struct resource *register_memory_resource(u64 start, u64 size)
103 struct resource *res, *conflict;
104 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
105 if (!res)
106 return ERR_PTR(-ENOMEM);
108 res->name = "System RAM";
109 res->start = start;
110 res->end = start + size - 1;
111 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
112 conflict = request_resource_conflict(&iomem_resource, res);
113 if (conflict) {
114 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
115 pr_debug("Device unaddressable memory block "
116 "memory hotplug at %#010llx !\n",
117 (unsigned long long)start);
119 pr_debug("System RAM resource %pR cannot be added\n", res);
120 kfree(res);
121 return ERR_PTR(-EEXIST);
123 return res;
126 static void release_memory_resource(struct resource *res)
128 if (!res)
129 return;
130 release_resource(res);
131 kfree(res);
132 return;
135 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
136 void get_page_bootmem(unsigned long info, struct page *page,
137 unsigned long type)
139 page->freelist = (void *)type;
140 SetPagePrivate(page);
141 set_page_private(page, info);
142 page_ref_inc(page);
145 void put_page_bootmem(struct page *page)
147 unsigned long type;
149 type = (unsigned long) page->freelist;
150 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
151 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
153 if (page_ref_dec_return(page) == 1) {
154 page->freelist = NULL;
155 ClearPagePrivate(page);
156 set_page_private(page, 0);
157 INIT_LIST_HEAD(&page->lru);
158 free_reserved_page(page);
162 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
163 #ifndef CONFIG_SPARSEMEM_VMEMMAP
164 static void register_page_bootmem_info_section(unsigned long start_pfn)
166 unsigned long *usemap, mapsize, section_nr, i;
167 struct mem_section *ms;
168 struct page *page, *memmap;
170 section_nr = pfn_to_section_nr(start_pfn);
171 ms = __nr_to_section(section_nr);
173 /* Get section's memmap address */
174 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
177 * Get page for the memmap's phys address
178 * XXX: need more consideration for sparse_vmemmap...
180 page = virt_to_page(memmap);
181 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
182 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
184 /* remember memmap's page */
185 for (i = 0; i < mapsize; i++, page++)
186 get_page_bootmem(section_nr, page, SECTION_INFO);
188 usemap = ms->pageblock_flags;
189 page = virt_to_page(usemap);
191 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
193 for (i = 0; i < mapsize; i++, page++)
194 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
197 #else /* CONFIG_SPARSEMEM_VMEMMAP */
198 static void register_page_bootmem_info_section(unsigned long start_pfn)
200 unsigned long *usemap, mapsize, section_nr, i;
201 struct mem_section *ms;
202 struct page *page, *memmap;
204 section_nr = pfn_to_section_nr(start_pfn);
205 ms = __nr_to_section(section_nr);
207 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
209 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
211 usemap = ms->pageblock_flags;
212 page = virt_to_page(usemap);
214 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
216 for (i = 0; i < mapsize; i++, page++)
217 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
219 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
221 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
223 unsigned long i, pfn, end_pfn, nr_pages;
224 int node = pgdat->node_id;
225 struct page *page;
227 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
228 page = virt_to_page(pgdat);
230 for (i = 0; i < nr_pages; i++, page++)
231 get_page_bootmem(node, page, NODE_INFO);
233 pfn = pgdat->node_start_pfn;
234 end_pfn = pgdat_end_pfn(pgdat);
236 /* register section info */
237 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
239 * Some platforms can assign the same pfn to multiple nodes - on
240 * node0 as well as nodeN. To avoid registering a pfn against
241 * multiple nodes we check that this pfn does not already
242 * reside in some other nodes.
244 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
245 register_page_bootmem_info_section(pfn);
248 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
250 static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
251 struct vmem_altmap *altmap, bool want_memblock)
253 int ret;
255 if (pfn_valid(phys_start_pfn))
256 return -EEXIST;
258 ret = sparse_add_one_section(nid, phys_start_pfn, altmap);
259 return ret < 0 ? ret : 0;
263 * Reasonably generic function for adding memory. It is
264 * expected that archs that support memory hotplug will
265 * call this function after deciding the zone to which to
266 * add the new pages.
268 int __ref __add_pages(int nid, unsigned long phys_start_pfn,
269 unsigned long nr_pages, struct vmem_altmap *altmap,
270 bool want_memblock)
272 unsigned long i;
273 int err = 0;
274 int start_sec, end_sec;
276 /* during initialize mem_map, align hot-added range to section */
277 start_sec = pfn_to_section_nr(phys_start_pfn);
278 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
280 if (altmap) {
282 * Validate altmap is within bounds of the total request
284 if (altmap->base_pfn != phys_start_pfn
285 || vmem_altmap_offset(altmap) > nr_pages) {
286 pr_warn_once("memory add fail, invalid altmap\n");
287 err = -EINVAL;
288 goto out;
290 altmap->alloc = 0;
293 for (i = start_sec; i <= end_sec; i++) {
294 err = __add_section(nid, section_nr_to_pfn(i), altmap,
295 want_memblock);
298 * EEXIST is finally dealt with by ioresource collision
299 * check. see add_memory() => register_memory_resource()
300 * Warning will be printed if there is collision.
302 if (err && (err != -EEXIST))
303 break;
304 err = 0;
305 cond_resched();
307 vmemmap_populate_print_last();
308 out:
309 return err;
312 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
313 static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
314 unsigned long start_pfn,
315 unsigned long end_pfn)
317 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
318 if (unlikely(!pfn_to_online_page(start_pfn)))
319 continue;
321 if (unlikely(pfn_to_nid(start_pfn) != nid))
322 continue;
324 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
325 continue;
327 return start_pfn;
330 return 0;
333 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
334 static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
335 unsigned long start_pfn,
336 unsigned long end_pfn)
338 unsigned long pfn;
340 /* pfn is the end pfn of a memory section. */
341 pfn = end_pfn - 1;
342 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
343 if (unlikely(!pfn_to_online_page(pfn)))
344 continue;
346 if (unlikely(pfn_to_nid(pfn) != nid))
347 continue;
349 if (zone && zone != page_zone(pfn_to_page(pfn)))
350 continue;
352 return pfn;
355 return 0;
358 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
359 unsigned long end_pfn)
361 unsigned long zone_start_pfn = zone->zone_start_pfn;
362 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
363 unsigned long zone_end_pfn = z;
364 unsigned long pfn;
365 int nid = zone_to_nid(zone);
367 zone_span_writelock(zone);
368 if (zone_start_pfn == start_pfn) {
370 * If the section is smallest section in the zone, it need
371 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
372 * In this case, we find second smallest valid mem_section
373 * for shrinking zone.
375 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
376 zone_end_pfn);
377 if (pfn) {
378 zone->zone_start_pfn = pfn;
379 zone->spanned_pages = zone_end_pfn - pfn;
381 } else if (zone_end_pfn == end_pfn) {
383 * If the section is biggest section in the zone, it need
384 * shrink zone->spanned_pages.
385 * In this case, we find second biggest valid mem_section for
386 * shrinking zone.
388 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
389 start_pfn);
390 if (pfn)
391 zone->spanned_pages = pfn - zone_start_pfn + 1;
395 * The section is not biggest or smallest mem_section in the zone, it
396 * only creates a hole in the zone. So in this case, we need not
397 * change the zone. But perhaps, the zone has only hole data. Thus
398 * it check the zone has only hole or not.
400 pfn = zone_start_pfn;
401 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
402 if (unlikely(!pfn_to_online_page(pfn)))
403 continue;
405 if (page_zone(pfn_to_page(pfn)) != zone)
406 continue;
408 /* If the section is current section, it continues the loop */
409 if (start_pfn == pfn)
410 continue;
412 /* If we find valid section, we have nothing to do */
413 zone_span_writeunlock(zone);
414 return;
417 /* The zone has no valid section */
418 zone->zone_start_pfn = 0;
419 zone->spanned_pages = 0;
420 zone_span_writeunlock(zone);
423 static void update_pgdat_span(struct pglist_data *pgdat)
425 unsigned long node_start_pfn = 0, node_end_pfn = 0;
426 struct zone *zone;
428 for (zone = pgdat->node_zones;
429 zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
430 unsigned long zone_end_pfn = zone->zone_start_pfn +
431 zone->spanned_pages;
433 /* No need to lock the zones, they can't change. */
434 if (!zone->spanned_pages)
435 continue;
436 if (!node_end_pfn) {
437 node_start_pfn = zone->zone_start_pfn;
438 node_end_pfn = zone_end_pfn;
439 continue;
442 if (zone_end_pfn > node_end_pfn)
443 node_end_pfn = zone_end_pfn;
444 if (zone->zone_start_pfn < node_start_pfn)
445 node_start_pfn = zone->zone_start_pfn;
448 pgdat->node_start_pfn = node_start_pfn;
449 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
452 void __ref remove_pfn_range_from_zone(struct zone *zone,
453 unsigned long start_pfn,
454 unsigned long nr_pages)
456 struct pglist_data *pgdat = zone->zone_pgdat;
457 unsigned long flags;
459 #ifdef CONFIG_ZONE_DEVICE
461 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
462 * we will not try to shrink the zones - which is okay as
463 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
465 if (zone_idx(zone) == ZONE_DEVICE)
466 return;
467 #endif
469 clear_zone_contiguous(zone);
471 pgdat_resize_lock(zone->zone_pgdat, &flags);
472 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
473 update_pgdat_span(pgdat);
474 pgdat_resize_unlock(zone->zone_pgdat, &flags);
476 set_zone_contiguous(zone);
479 static void __remove_section(struct mem_section *ms, unsigned long map_offset,
480 struct vmem_altmap *altmap)
482 unsigned long start_pfn;
483 int scn_nr;
485 if (WARN_ON_ONCE(!valid_section(ms)))
486 return;
488 scn_nr = __section_nr(ms);
489 start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
491 sparse_remove_one_section(ms, map_offset, altmap);
495 * __remove_pages() - remove sections of pages
496 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
497 * @nr_pages: number of pages to remove (must be multiple of section size)
498 * @altmap: alternative device page map or %NULL if default memmap is used
500 * Generic helper function to remove section mappings and sysfs entries
501 * for the section of the memory we are removing. Caller needs to make
502 * sure that pages are marked reserved and zones are adjust properly by
503 * calling offline_pages().
505 void __remove_pages(unsigned long phys_start_pfn, unsigned long nr_pages,
506 struct vmem_altmap *altmap)
508 unsigned long i;
509 unsigned long map_offset = 0;
510 int sections_to_remove;
512 if (altmap)
513 map_offset = vmem_altmap_offset(altmap);
516 * We can only remove entire sections
518 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
519 BUG_ON(nr_pages % PAGES_PER_SECTION);
521 sections_to_remove = nr_pages / PAGES_PER_SECTION;
522 for (i = 0; i < sections_to_remove; i++) {
523 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
525 cond_resched();
526 __remove_section(__pfn_to_section(pfn), map_offset, altmap);
527 map_offset = 0;
531 int set_online_page_callback(online_page_callback_t callback)
533 int rc = -EINVAL;
535 get_online_mems();
536 mutex_lock(&online_page_callback_lock);
538 if (online_page_callback == generic_online_page) {
539 online_page_callback = callback;
540 rc = 0;
543 mutex_unlock(&online_page_callback_lock);
544 put_online_mems();
546 return rc;
548 EXPORT_SYMBOL_GPL(set_online_page_callback);
550 int restore_online_page_callback(online_page_callback_t callback)
552 int rc = -EINVAL;
554 get_online_mems();
555 mutex_lock(&online_page_callback_lock);
557 if (online_page_callback == callback) {
558 online_page_callback = generic_online_page;
559 rc = 0;
562 mutex_unlock(&online_page_callback_lock);
563 put_online_mems();
565 return rc;
567 EXPORT_SYMBOL_GPL(restore_online_page_callback);
569 void __online_page_set_limits(struct page *page)
572 EXPORT_SYMBOL_GPL(__online_page_set_limits);
574 void __online_page_increment_counters(struct page *page)
576 adjust_managed_page_count(page, 1);
578 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
580 void __online_page_free(struct page *page)
582 __free_reserved_page(page);
584 EXPORT_SYMBOL_GPL(__online_page_free);
586 static void generic_online_page(struct page *page)
588 __online_page_set_limits(page);
589 __online_page_increment_counters(page);
590 __online_page_free(page);
593 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
594 void *arg)
596 unsigned long i;
597 unsigned long onlined_pages = *(unsigned long *)arg;
598 struct page *page;
600 if (PageReserved(pfn_to_page(start_pfn)))
601 for (i = 0; i < nr_pages; i++) {
602 page = pfn_to_page(start_pfn + i);
603 (*online_page_callback)(page);
604 onlined_pages++;
607 online_mem_sections(start_pfn, start_pfn + nr_pages);
609 *(unsigned long *)arg = onlined_pages;
610 return 0;
613 /* check which state of node_states will be changed when online memory */
614 static void node_states_check_changes_online(unsigned long nr_pages,
615 struct zone *zone, struct memory_notify *arg)
617 int nid = zone_to_nid(zone);
618 enum zone_type zone_last = ZONE_NORMAL;
621 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
622 * contains nodes which have zones of 0...ZONE_NORMAL,
623 * set zone_last to ZONE_NORMAL.
625 * If we don't have HIGHMEM nor movable node,
626 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
627 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
629 if (N_MEMORY == N_NORMAL_MEMORY)
630 zone_last = ZONE_MOVABLE;
633 * if the memory to be online is in a zone of 0...zone_last, and
634 * the zones of 0...zone_last don't have memory before online, we will
635 * need to set the node to node_states[N_NORMAL_MEMORY] after
636 * the memory is online.
638 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
639 arg->status_change_nid_normal = nid;
640 else
641 arg->status_change_nid_normal = -1;
643 #ifdef CONFIG_HIGHMEM
645 * If we have movable node, node_states[N_HIGH_MEMORY]
646 * contains nodes which have zones of 0...ZONE_HIGHMEM,
647 * set zone_last to ZONE_HIGHMEM.
649 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
650 * contains nodes which have zones of 0...ZONE_MOVABLE,
651 * set zone_last to ZONE_MOVABLE.
653 zone_last = ZONE_HIGHMEM;
654 if (N_MEMORY == N_HIGH_MEMORY)
655 zone_last = ZONE_MOVABLE;
657 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
658 arg->status_change_nid_high = nid;
659 else
660 arg->status_change_nid_high = -1;
661 #else
662 arg->status_change_nid_high = arg->status_change_nid_normal;
663 #endif
666 * if the node don't have memory befor online, we will need to
667 * set the node to node_states[N_MEMORY] after the memory
668 * is online.
670 if (!node_state(nid, N_MEMORY))
671 arg->status_change_nid = nid;
672 else
673 arg->status_change_nid = -1;
676 static void node_states_set_node(int node, struct memory_notify *arg)
678 if (arg->status_change_nid_normal >= 0)
679 node_set_state(node, N_NORMAL_MEMORY);
681 if (arg->status_change_nid_high >= 0)
682 node_set_state(node, N_HIGH_MEMORY);
684 node_set_state(node, N_MEMORY);
687 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
688 unsigned long nr_pages)
690 unsigned long old_end_pfn = zone_end_pfn(zone);
692 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
693 zone->zone_start_pfn = start_pfn;
695 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
698 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
699 unsigned long nr_pages)
701 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
703 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
704 pgdat->node_start_pfn = start_pfn;
706 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
709 void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
710 unsigned long nr_pages, struct vmem_altmap *altmap)
712 struct pglist_data *pgdat = zone->zone_pgdat;
713 int nid = pgdat->node_id;
714 unsigned long flags;
716 if (zone_is_empty(zone))
717 init_currently_empty_zone(zone, start_pfn, nr_pages);
719 clear_zone_contiguous(zone);
721 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
722 pgdat_resize_lock(pgdat, &flags);
723 zone_span_writelock(zone);
724 resize_zone_range(zone, start_pfn, nr_pages);
725 zone_span_writeunlock(zone);
726 resize_pgdat_range(pgdat, start_pfn, nr_pages);
727 pgdat_resize_unlock(pgdat, &flags);
730 * TODO now we have a visible range of pages which are not associated
731 * with their zone properly. Not nice but set_pfnblock_flags_mask
732 * expects the zone spans the pfn range. All the pages in the range
733 * are reserved so nobody should be touching them so we should be safe
735 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
736 MEMMAP_HOTPLUG, altmap);
738 set_zone_contiguous(zone);
742 * Returns a default kernel memory zone for the given pfn range.
743 * If no kernel zone covers this pfn range it will automatically go
744 * to the ZONE_NORMAL.
746 static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
747 unsigned long nr_pages)
749 struct pglist_data *pgdat = NODE_DATA(nid);
750 int zid;
752 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
753 struct zone *zone = &pgdat->node_zones[zid];
755 if (zone_intersects(zone, start_pfn, nr_pages))
756 return zone;
759 return &pgdat->node_zones[ZONE_NORMAL];
762 static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
763 unsigned long nr_pages)
765 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
766 nr_pages);
767 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
768 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
769 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
772 * We inherit the existing zone in a simple case where zones do not
773 * overlap in the given range
775 if (in_kernel ^ in_movable)
776 return (in_kernel) ? kernel_zone : movable_zone;
779 * If the range doesn't belong to any zone or two zones overlap in the
780 * given range then we use movable zone only if movable_node is
781 * enabled because we always online to a kernel zone by default.
783 return movable_node_enabled ? movable_zone : kernel_zone;
786 struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
787 unsigned long nr_pages)
789 if (online_type == MMOP_ONLINE_KERNEL)
790 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
792 if (online_type == MMOP_ONLINE_MOVABLE)
793 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
795 return default_zone_for_pfn(nid, start_pfn, nr_pages);
799 * Associates the given pfn range with the given node and the zone appropriate
800 * for the given online type.
802 static struct zone * __meminit move_pfn_range(int online_type, int nid,
803 unsigned long start_pfn, unsigned long nr_pages)
805 struct zone *zone;
807 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
808 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
809 return zone;
812 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
814 unsigned long flags;
815 unsigned long onlined_pages = 0;
816 struct zone *zone;
817 int need_zonelists_rebuild = 0;
818 int nid;
819 int ret;
820 struct memory_notify arg;
821 struct memory_block *mem;
823 mem_hotplug_begin();
826 * We can't use pfn_to_nid() because nid might be stored in struct page
827 * which is not yet initialized. Instead, we find nid from memory block.
829 mem = find_memory_block(__pfn_to_section(pfn));
830 nid = mem->nid;
831 put_device(&mem->dev);
833 /* associate pfn range with the zone */
834 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
836 arg.start_pfn = pfn;
837 arg.nr_pages = nr_pages;
838 node_states_check_changes_online(nr_pages, zone, &arg);
840 ret = memory_notify(MEM_GOING_ONLINE, &arg);
841 ret = notifier_to_errno(ret);
842 if (ret)
843 goto failed_addition;
846 * If this zone is not populated, then it is not in zonelist.
847 * This means the page allocator ignores this zone.
848 * So, zonelist must be updated after online.
850 if (!populated_zone(zone)) {
851 need_zonelists_rebuild = 1;
852 setup_zone_pageset(zone);
855 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
856 online_pages_range);
857 if (ret) {
858 if (need_zonelists_rebuild)
859 zone_pcp_reset(zone);
860 goto failed_addition;
863 zone->present_pages += onlined_pages;
865 pgdat_resize_lock(zone->zone_pgdat, &flags);
866 zone->zone_pgdat->node_present_pages += onlined_pages;
867 pgdat_resize_unlock(zone->zone_pgdat, &flags);
869 if (onlined_pages) {
870 node_states_set_node(nid, &arg);
871 if (need_zonelists_rebuild)
872 build_all_zonelists(NULL);
873 else
874 zone_pcp_update(zone);
877 init_per_zone_wmark_min();
879 if (onlined_pages) {
880 kswapd_run(nid);
881 kcompactd_run(nid);
884 vm_total_pages = nr_free_pagecache_pages();
886 writeback_set_ratelimit();
888 if (onlined_pages)
889 memory_notify(MEM_ONLINE, &arg);
890 mem_hotplug_done();
891 return 0;
893 failed_addition:
894 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
895 (unsigned long long) pfn << PAGE_SHIFT,
896 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
897 memory_notify(MEM_CANCEL_ONLINE, &arg);
898 remove_pfn_range_from_zone(zone, pfn, nr_pages);
899 mem_hotplug_done();
900 return ret;
902 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
904 static void reset_node_present_pages(pg_data_t *pgdat)
906 struct zone *z;
908 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
909 z->present_pages = 0;
911 pgdat->node_present_pages = 0;
914 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
915 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
917 struct pglist_data *pgdat;
918 unsigned long start_pfn = PFN_DOWN(start);
920 pgdat = NODE_DATA(nid);
921 if (!pgdat) {
922 pgdat = arch_alloc_nodedata(nid);
923 if (!pgdat)
924 return NULL;
926 arch_refresh_nodedata(nid, pgdat);
927 } else {
929 * Reset the nr_zones, order and classzone_idx before reuse.
930 * Note that kswapd will init kswapd_classzone_idx properly
931 * when it starts in the near future.
933 pgdat->nr_zones = 0;
934 pgdat->kswapd_order = 0;
935 pgdat->kswapd_classzone_idx = 0;
938 /* we can use NODE_DATA(nid) from here */
940 pgdat->node_id = nid;
941 pgdat->node_start_pfn = start_pfn;
943 /* init node's zones as empty zones, we don't have any present pages.*/
944 free_area_init_core_hotplug(nid);
945 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
948 * The node we allocated has no zone fallback lists. For avoiding
949 * to access not-initialized zonelist, build here.
951 build_all_zonelists(pgdat);
954 * When memory is hot-added, all the memory is in offline state. So
955 * clear all zones' present_pages because they will be updated in
956 * online_pages() and offline_pages().
958 reset_node_managed_pages(pgdat);
959 reset_node_present_pages(pgdat);
961 return pgdat;
964 static void rollback_node_hotadd(int nid)
966 pg_data_t *pgdat = NODE_DATA(nid);
968 arch_refresh_nodedata(nid, NULL);
969 free_percpu(pgdat->per_cpu_nodestats);
970 arch_free_nodedata(pgdat);
971 return;
976 * try_online_node - online a node if offlined
977 * @nid: the node ID
978 * @start: start addr of the node
979 * @set_node_online: Whether we want to online the node
980 * called by cpu_up() to online a node without onlined memory.
982 * Returns:
983 * 1 -> a new node has been allocated
984 * 0 -> the node is already online
985 * -ENOMEM -> the node could not be allocated
987 static int __try_online_node(int nid, u64 start, bool set_node_online)
989 pg_data_t *pgdat;
990 int ret = 1;
992 if (node_online(nid))
993 return 0;
995 pgdat = hotadd_new_pgdat(nid, start);
996 if (!pgdat) {
997 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
998 ret = -ENOMEM;
999 goto out;
1002 if (set_node_online) {
1003 node_set_online(nid);
1004 ret = register_one_node(nid);
1005 BUG_ON(ret);
1007 out:
1008 return ret;
1012 * Users of this function always want to online/register the node
1014 int try_online_node(int nid)
1016 int ret;
1018 mem_hotplug_begin();
1019 ret = __try_online_node(nid, 0, true);
1020 mem_hotplug_done();
1021 return ret;
1024 static int check_hotplug_memory_range(u64 start, u64 size)
1026 unsigned long block_sz = memory_block_size_bytes();
1027 u64 block_nr_pages = block_sz >> PAGE_SHIFT;
1028 u64 nr_pages = size >> PAGE_SHIFT;
1029 u64 start_pfn = PFN_DOWN(start);
1031 /* memory range must be block size aligned */
1032 if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) ||
1033 !IS_ALIGNED(nr_pages, block_nr_pages)) {
1034 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1035 block_sz, start, size);
1036 return -EINVAL;
1039 return 0;
1042 static int online_memory_block(struct memory_block *mem, void *arg)
1044 return device_online(&mem->dev);
1048 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1049 * and online/offline operations (triggered e.g. by sysfs).
1051 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1053 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1055 u64 start, size;
1056 bool new_node = false;
1057 int ret;
1059 start = res->start;
1060 size = resource_size(res);
1062 ret = check_hotplug_memory_range(start, size);
1063 if (ret)
1064 return ret;
1066 mem_hotplug_begin();
1069 * Add new range to memblock so that when hotadd_new_pgdat() is called
1070 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1071 * this new range and calculate total pages correctly. The range will
1072 * be removed at hot-remove time.
1074 memblock_add_node(start, size, nid);
1076 ret = __try_online_node(nid, start, false);
1077 if (ret < 0)
1078 goto error;
1079 new_node = ret;
1081 /* call arch's memory hotadd */
1082 ret = arch_add_memory(nid, start, size, NULL, true);
1083 if (ret < 0)
1084 goto error;
1086 /* create memory block devices after memory was added */
1087 ret = create_memory_block_devices(start, size);
1088 if (ret) {
1089 arch_remove_memory(nid, start, size, NULL);
1090 goto error;
1093 if (new_node) {
1094 /* If sysfs file of new node can't be created, cpu on the node
1095 * can't be hot-added. There is no rollback way now.
1096 * So, check by BUG_ON() to catch it reluctantly..
1097 * We online node here. We can't roll back from here.
1099 node_set_online(nid);
1100 ret = __register_one_node(nid);
1101 BUG_ON(ret);
1104 /* link memory sections under this node.*/
1105 ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
1106 BUG_ON(ret);
1108 /* create new memmap entry */
1109 firmware_map_add_hotplug(start, start + size, "System RAM");
1111 /* device_online() will take the lock when calling online_pages() */
1112 mem_hotplug_done();
1114 /* online pages if requested */
1115 if (online)
1116 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1117 NULL, online_memory_block);
1119 return ret;
1120 error:
1121 /* rollback pgdat allocation and others */
1122 if (new_node)
1123 rollback_node_hotadd(nid);
1124 memblock_remove(start, size);
1125 mem_hotplug_done();
1126 return ret;
1129 /* requires device_hotplug_lock, see add_memory_resource() */
1130 int __ref __add_memory(int nid, u64 start, u64 size)
1132 struct resource *res;
1133 int ret;
1135 res = register_memory_resource(start, size);
1136 if (IS_ERR(res))
1137 return PTR_ERR(res);
1139 ret = add_memory_resource(nid, res, memhp_auto_online);
1140 if (ret < 0)
1141 release_memory_resource(res);
1142 return ret;
1145 int add_memory(int nid, u64 start, u64 size)
1147 int rc;
1149 lock_device_hotplug();
1150 rc = __add_memory(nid, start, size);
1151 unlock_device_hotplug();
1153 return rc;
1155 EXPORT_SYMBOL_GPL(add_memory);
1157 #ifdef CONFIG_MEMORY_HOTREMOVE
1159 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1160 * set and the size of the free page is given by page_order(). Using this,
1161 * the function determines if the pageblock contains only free pages.
1162 * Due to buddy contraints, a free page at least the size of a pageblock will
1163 * be located at the start of the pageblock
1165 static inline int pageblock_free(struct page *page)
1167 return PageBuddy(page) && page_order(page) >= pageblock_order;
1170 /* Return the pfn of the start of the next active pageblock after a given pfn */
1171 static unsigned long next_active_pageblock(unsigned long pfn)
1173 struct page *page = pfn_to_page(pfn);
1175 /* Ensure the starting page is pageblock-aligned */
1176 BUG_ON(pfn & (pageblock_nr_pages - 1));
1178 /* If the entire pageblock is free, move to the end of free page */
1179 if (pageblock_free(page)) {
1180 int order;
1181 /* be careful. we don't have locks, page_order can be changed.*/
1182 order = page_order(page);
1183 if ((order < MAX_ORDER) && (order >= pageblock_order))
1184 return pfn + (1 << order);
1187 return pfn + pageblock_nr_pages;
1190 static bool is_pageblock_removable_nolock(unsigned long pfn)
1192 struct page *page = pfn_to_page(pfn);
1193 struct zone *zone;
1196 * We have to be careful here because we are iterating over memory
1197 * sections which are not zone aware so we might end up outside of
1198 * the zone but still within the section.
1199 * We have to take care about the node as well. If the node is offline
1200 * its NODE_DATA will be NULL - see page_zone.
1202 if (!node_online(page_to_nid(page)))
1203 return false;
1205 zone = page_zone(page);
1206 pfn = page_to_pfn(page);
1207 if (!zone_spans_pfn(zone, pfn))
1208 return false;
1210 return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, true);
1213 /* Checks if this range of memory is likely to be hot-removable. */
1214 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1216 unsigned long end_pfn, pfn;
1218 end_pfn = min(start_pfn + nr_pages,
1219 zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
1221 /* Check the starting page of each pageblock within the range */
1222 for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1223 if (!is_pageblock_removable_nolock(pfn))
1224 return false;
1225 cond_resched();
1228 /* All pageblocks in the memory block are likely to be hot-removable */
1229 return true;
1233 * Confirm all pages in a range [start, end) belong to the same zone.
1234 * When true, return its valid [start, end).
1236 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1237 unsigned long *valid_start, unsigned long *valid_end)
1239 unsigned long pfn, sec_end_pfn;
1240 unsigned long start, end;
1241 struct zone *zone = NULL;
1242 struct page *page;
1243 int i;
1244 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1245 pfn < end_pfn;
1246 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1247 /* Make sure the memory section is present first */
1248 if (!present_section_nr(pfn_to_section_nr(pfn)))
1249 continue;
1250 for (; pfn < sec_end_pfn && pfn < end_pfn;
1251 pfn += MAX_ORDER_NR_PAGES) {
1252 i = 0;
1253 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1254 while ((i < MAX_ORDER_NR_PAGES) &&
1255 !pfn_valid_within(pfn + i))
1256 i++;
1257 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1258 continue;
1259 /* Check if we got outside of the zone */
1260 if (zone && !zone_spans_pfn(zone, pfn + i))
1261 return 0;
1262 page = pfn_to_page(pfn + i);
1263 if (zone && page_zone(page) != zone)
1264 return 0;
1265 if (!zone)
1266 start = pfn + i;
1267 zone = page_zone(page);
1268 end = pfn + MAX_ORDER_NR_PAGES;
1272 if (zone) {
1273 *valid_start = start;
1274 *valid_end = min(end, end_pfn);
1275 return 1;
1276 } else {
1277 return 0;
1282 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1283 * non-lru movable pages and hugepages). We scan pfn because it's much
1284 * easier than scanning over linked list. This function returns the pfn
1285 * of the first found movable page if it's found, otherwise 0.
1287 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1289 unsigned long pfn;
1291 for (pfn = start; pfn < end; pfn++) {
1292 struct page *page, *head;
1293 unsigned long skip;
1295 if (!pfn_valid(pfn))
1296 continue;
1297 page = pfn_to_page(pfn);
1298 if (PageLRU(page))
1299 return pfn;
1300 if (__PageMovable(page))
1301 return pfn;
1303 if (!PageHuge(page))
1304 continue;
1305 head = compound_head(page);
1306 if (hugepage_migration_supported(page_hstate(head)) &&
1307 page_huge_active(head))
1308 return pfn;
1309 skip = (1 << compound_order(head)) - (page - head);
1310 pfn += skip - 1;
1312 return 0;
1315 static struct page *new_node_page(struct page *page, unsigned long private)
1317 int nid = page_to_nid(page);
1318 nodemask_t nmask = node_states[N_MEMORY];
1321 * try to allocate from a different node but reuse this node if there
1322 * are no other online nodes to be used (e.g. we are offlining a part
1323 * of the only existing node)
1325 node_clear(nid, nmask);
1326 if (nodes_empty(nmask))
1327 node_set(nid, nmask);
1329 return new_page_nodemask(page, nid, &nmask);
1332 #define NR_OFFLINE_AT_ONCE_PAGES (256)
1333 static int
1334 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1336 unsigned long pfn;
1337 struct page *page;
1338 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1339 int not_managed = 0;
1340 int ret = 0;
1341 LIST_HEAD(source);
1343 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1344 if (!pfn_valid(pfn))
1345 continue;
1346 page = pfn_to_page(pfn);
1348 if (PageHuge(page)) {
1349 struct page *head = compound_head(page);
1350 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1351 if (compound_order(head) > PFN_SECTION_SHIFT) {
1352 ret = -EBUSY;
1353 break;
1355 if (isolate_huge_page(page, &source))
1356 move_pages -= 1 << compound_order(head);
1357 continue;
1358 } else if (PageTransHuge(page))
1359 pfn = page_to_pfn(compound_head(page))
1360 + hpage_nr_pages(page) - 1;
1363 * HWPoison pages have elevated reference counts so the migration would
1364 * fail on them. It also doesn't make any sense to migrate them in the
1365 * first place. Still try to unmap such a page in case it is still mapped
1366 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1367 * the unmap as the catch all safety net).
1369 if (PageHWPoison(page)) {
1370 if (WARN_ON(PageLRU(page)))
1371 isolate_lru_page(page);
1372 if (page_mapped(page))
1373 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1374 continue;
1377 if (!get_page_unless_zero(page))
1378 continue;
1380 * We can skip free pages. And we can deal with pages on
1381 * LRU and non-lru movable pages.
1383 if (PageLRU(page))
1384 ret = isolate_lru_page(page);
1385 else
1386 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1387 if (!ret) { /* Success */
1388 put_page(page);
1389 list_add_tail(&page->lru, &source);
1390 move_pages--;
1391 if (!__PageMovable(page))
1392 inc_node_page_state(page, NR_ISOLATED_ANON +
1393 page_is_file_cache(page));
1395 } else {
1396 #ifdef CONFIG_DEBUG_VM
1397 pr_alert("failed to isolate pfn %lx\n", pfn);
1398 dump_page(page, "isolation failed");
1399 #endif
1400 put_page(page);
1401 /* Because we don't have big zone->lock. we should
1402 check this again here. */
1403 if (page_count(page)) {
1404 not_managed++;
1405 ret = -EBUSY;
1406 break;
1410 if (!list_empty(&source)) {
1411 if (not_managed) {
1412 putback_movable_pages(&source);
1413 goto out;
1416 /* Allocate a new page from the nearest neighbor node */
1417 ret = migrate_pages(&source, new_node_page, NULL, 0,
1418 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1419 if (ret)
1420 putback_movable_pages(&source);
1422 out:
1423 return ret;
1427 * remove from free_area[] and mark all as Reserved.
1429 static int
1430 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1431 void *data)
1433 __offline_isolated_pages(start, start + nr_pages);
1434 return 0;
1437 static void
1438 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1440 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1441 offline_isolated_pages_cb);
1445 * Check all pages in range, recoreded as memory resource, are isolated.
1447 static int
1448 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1449 void *data)
1451 int ret;
1452 long offlined = *(long *)data;
1453 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1454 offlined = nr_pages;
1455 if (!ret)
1456 *(long *)data += offlined;
1457 return ret;
1460 static long
1461 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1463 long offlined = 0;
1464 int ret;
1466 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1467 check_pages_isolated_cb);
1468 if (ret < 0)
1469 offlined = (long)ret;
1470 return offlined;
1473 static int __init cmdline_parse_movable_node(char *p)
1475 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1476 movable_node_enabled = true;
1477 #else
1478 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1479 #endif
1480 return 0;
1482 early_param("movable_node", cmdline_parse_movable_node);
1484 /* check which state of node_states will be changed when offline memory */
1485 static void node_states_check_changes_offline(unsigned long nr_pages,
1486 struct zone *zone, struct memory_notify *arg)
1488 struct pglist_data *pgdat = zone->zone_pgdat;
1489 unsigned long present_pages = 0;
1490 enum zone_type zt, zone_last = ZONE_NORMAL;
1493 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1494 * contains nodes which have zones of 0...ZONE_NORMAL,
1495 * set zone_last to ZONE_NORMAL.
1497 * If we don't have HIGHMEM nor movable node,
1498 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1499 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1501 if (N_MEMORY == N_NORMAL_MEMORY)
1502 zone_last = ZONE_MOVABLE;
1505 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1506 * If the memory to be offline is in a zone of 0...zone_last,
1507 * and it is the last present memory, 0...zone_last will
1508 * become empty after offline , thus we can determind we will
1509 * need to clear the node from node_states[N_NORMAL_MEMORY].
1511 for (zt = 0; zt <= zone_last; zt++)
1512 present_pages += pgdat->node_zones[zt].present_pages;
1513 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1514 arg->status_change_nid_normal = zone_to_nid(zone);
1515 else
1516 arg->status_change_nid_normal = -1;
1518 #ifdef CONFIG_HIGHMEM
1520 * If we have movable node, node_states[N_HIGH_MEMORY]
1521 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1522 * set zone_last to ZONE_HIGHMEM.
1524 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1525 * contains nodes which have zones of 0...ZONE_MOVABLE,
1526 * set zone_last to ZONE_MOVABLE.
1528 zone_last = ZONE_HIGHMEM;
1529 if (N_MEMORY == N_HIGH_MEMORY)
1530 zone_last = ZONE_MOVABLE;
1532 for (; zt <= zone_last; zt++)
1533 present_pages += pgdat->node_zones[zt].present_pages;
1534 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1535 arg->status_change_nid_high = zone_to_nid(zone);
1536 else
1537 arg->status_change_nid_high = -1;
1538 #else
1539 arg->status_change_nid_high = arg->status_change_nid_normal;
1540 #endif
1543 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1545 zone_last = ZONE_MOVABLE;
1548 * check whether node_states[N_HIGH_MEMORY] will be changed
1549 * If we try to offline the last present @nr_pages from the node,
1550 * we can determind we will need to clear the node from
1551 * node_states[N_HIGH_MEMORY].
1553 for (; zt <= zone_last; zt++)
1554 present_pages += pgdat->node_zones[zt].present_pages;
1555 if (nr_pages >= present_pages)
1556 arg->status_change_nid = zone_to_nid(zone);
1557 else
1558 arg->status_change_nid = -1;
1561 static void node_states_clear_node(int node, struct memory_notify *arg)
1563 if (arg->status_change_nid_normal >= 0)
1564 node_clear_state(node, N_NORMAL_MEMORY);
1566 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1567 (arg->status_change_nid_high >= 0))
1568 node_clear_state(node, N_HIGH_MEMORY);
1570 if ((N_MEMORY != N_HIGH_MEMORY) &&
1571 (arg->status_change_nid >= 0))
1572 node_clear_state(node, N_MEMORY);
1575 static int __ref __offline_pages(unsigned long start_pfn,
1576 unsigned long end_pfn)
1578 unsigned long pfn, nr_pages;
1579 long offlined_pages;
1580 int ret, node;
1581 unsigned long flags;
1582 unsigned long valid_start, valid_end;
1583 struct zone *zone;
1584 struct memory_notify arg;
1586 /* at least, alignment against pageblock is necessary */
1587 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1588 return -EINVAL;
1589 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1590 return -EINVAL;
1592 mem_hotplug_begin();
1594 /* This makes hotplug much easier...and readable.
1595 we assume this for now. .*/
1596 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1597 &valid_end)) {
1598 mem_hotplug_done();
1599 return -EINVAL;
1602 zone = page_zone(pfn_to_page(valid_start));
1603 node = zone_to_nid(zone);
1604 nr_pages = end_pfn - start_pfn;
1606 /* set above range as isolated */
1607 ret = start_isolate_page_range(start_pfn, end_pfn,
1608 MIGRATE_MOVABLE, true);
1609 if (ret) {
1610 mem_hotplug_done();
1611 return ret;
1614 arg.start_pfn = start_pfn;
1615 arg.nr_pages = nr_pages;
1616 node_states_check_changes_offline(nr_pages, zone, &arg);
1618 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1619 ret = notifier_to_errno(ret);
1620 if (ret)
1621 goto failed_removal;
1623 pfn = start_pfn;
1624 repeat:
1625 /* start memory hot removal */
1626 ret = -EINTR;
1627 if (signal_pending(current))
1628 goto failed_removal;
1630 cond_resched();
1631 lru_add_drain_all();
1632 drain_all_pages(zone);
1634 pfn = scan_movable_pages(start_pfn, end_pfn);
1635 if (pfn) { /* We have movable pages */
1636 ret = do_migrate_range(pfn, end_pfn);
1637 goto repeat;
1641 * dissolve free hugepages in the memory block before doing offlining
1642 * actually in order to make hugetlbfs's object counting consistent.
1644 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1645 if (ret)
1646 goto failed_removal;
1647 /* check again */
1648 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1649 if (offlined_pages < 0)
1650 goto repeat;
1651 pr_info("Offlined Pages %ld\n", offlined_pages);
1652 /* Ok, all of our target is isolated.
1653 We cannot do rollback at this point. */
1654 offline_isolated_pages(start_pfn, end_pfn);
1655 /* reset pagetype flags and makes migrate type to be MOVABLE */
1656 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1657 /* removal success */
1658 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1659 zone->present_pages -= offlined_pages;
1661 pgdat_resize_lock(zone->zone_pgdat, &flags);
1662 zone->zone_pgdat->node_present_pages -= offlined_pages;
1663 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1665 init_per_zone_wmark_min();
1667 if (!populated_zone(zone)) {
1668 zone_pcp_reset(zone);
1669 build_all_zonelists(NULL);
1670 } else
1671 zone_pcp_update(zone);
1673 node_states_clear_node(node, &arg);
1674 if (arg.status_change_nid >= 0) {
1675 kswapd_stop(node);
1676 kcompactd_stop(node);
1679 vm_total_pages = nr_free_pagecache_pages();
1680 writeback_set_ratelimit();
1682 memory_notify(MEM_OFFLINE, &arg);
1683 remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
1684 mem_hotplug_done();
1685 return 0;
1687 failed_removal:
1688 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1689 (unsigned long long) start_pfn << PAGE_SHIFT,
1690 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1691 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1692 /* pushback to free area */
1693 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1694 mem_hotplug_done();
1695 return ret;
1698 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1700 return __offline_pages(start_pfn, start_pfn + nr_pages);
1702 #endif /* CONFIG_MEMORY_HOTREMOVE */
1705 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1706 * @start_pfn: start pfn of the memory range
1707 * @end_pfn: end pfn of the memory range
1708 * @arg: argument passed to func
1709 * @func: callback for each memory section walked
1711 * This function walks through all present mem sections in range
1712 * [start_pfn, end_pfn) and call func on each mem section.
1714 * Returns the return value of func.
1716 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1717 void *arg, int (*func)(struct memory_block *, void *))
1719 struct memory_block *mem = NULL;
1720 struct mem_section *section;
1721 unsigned long pfn, section_nr;
1722 int ret;
1724 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1725 section_nr = pfn_to_section_nr(pfn);
1726 if (!present_section_nr(section_nr))
1727 continue;
1729 section = __nr_to_section(section_nr);
1730 /* same memblock? */
1731 if (mem)
1732 if ((section_nr >= mem->start_section_nr) &&
1733 (section_nr <= mem->end_section_nr))
1734 continue;
1736 mem = find_memory_block_hinted(section, mem);
1737 if (!mem)
1738 continue;
1740 ret = func(mem, arg);
1741 if (ret) {
1742 kobject_put(&mem->dev.kobj);
1743 return ret;
1747 if (mem)
1748 kobject_put(&mem->dev.kobj);
1750 return 0;
1753 #ifdef CONFIG_MEMORY_HOTREMOVE
1754 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1756 int ret = !is_memblock_offlined(mem);
1758 if (unlikely(ret)) {
1759 phys_addr_t beginpa, endpa;
1761 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1762 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1763 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1764 &beginpa, &endpa);
1767 return ret;
1770 static int check_cpu_on_node(pg_data_t *pgdat)
1772 int cpu;
1774 for_each_present_cpu(cpu) {
1775 if (cpu_to_node(cpu) == pgdat->node_id)
1777 * the cpu on this node isn't removed, and we can't
1778 * offline this node.
1780 return -EBUSY;
1783 return 0;
1786 static void unmap_cpu_on_node(pg_data_t *pgdat)
1788 #ifdef CONFIG_ACPI_NUMA
1789 int cpu;
1791 for_each_possible_cpu(cpu)
1792 if (cpu_to_node(cpu) == pgdat->node_id)
1793 numa_clear_node(cpu);
1794 #endif
1797 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1799 int ret;
1801 ret = check_cpu_on_node(pgdat);
1802 if (ret)
1803 return ret;
1806 * the node will be offlined when we come here, so we can clear
1807 * the cpu_to_node() now.
1810 unmap_cpu_on_node(pgdat);
1811 return 0;
1814 static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
1816 int nid = *(int *)arg;
1819 * If a memory block belongs to multiple nodes, the stored nid is not
1820 * reliable. However, such blocks are always online (e.g., cannot get
1821 * offlined) and, therefore, are still spanned by the node.
1823 return mem->nid == nid ? -EEXIST : 0;
1827 * try_offline_node
1828 * @nid: the node ID
1830 * Offline a node if all memory sections and cpus of the node are removed.
1832 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1833 * and online/offline operations before this call.
1835 void try_offline_node(int nid)
1837 pg_data_t *pgdat = NODE_DATA(nid);
1838 int rc;
1841 * If the node still spans pages (especially ZONE_DEVICE), don't
1842 * offline it. A node spans memory after move_pfn_range_to_zone(),
1843 * e.g., after the memory block was onlined.
1845 if (pgdat->node_spanned_pages)
1846 return;
1849 * Especially offline memory blocks might not be spanned by the
1850 * node. They will get spanned by the node once they get onlined.
1851 * However, they link to the node in sysfs and can get onlined later.
1853 rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
1854 if (rc)
1855 return;
1857 if (check_and_unmap_cpu_on_node(pgdat))
1858 return;
1861 * all memory/cpu of this node are removed, we can offline this
1862 * node now.
1864 node_set_offline(nid);
1865 unregister_one_node(nid);
1867 EXPORT_SYMBOL(try_offline_node);
1869 static void __release_memory_resource(resource_size_t start,
1870 resource_size_t size)
1872 int ret;
1875 * When removing memory in the same granularity as it was added,
1876 * this function never fails. It might only fail if resources
1877 * have to be adjusted or split. We'll ignore the error, as
1878 * removing of memory cannot fail.
1880 ret = release_mem_region_adjustable(&iomem_resource, start, size);
1881 if (ret) {
1882 resource_size_t endres = start + size - 1;
1884 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1885 &start, &endres, ret);
1890 * remove_memory
1891 * @nid: the node ID
1892 * @start: physical address of the region to remove
1893 * @size: size of the region to remove
1895 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1896 * and online/offline operations before this call, as required by
1897 * try_offline_node().
1899 void __ref __remove_memory(int nid, u64 start, u64 size)
1901 int ret;
1903 BUG_ON(check_hotplug_memory_range(start, size));
1906 * All memory blocks must be offlined before removing memory. Check
1907 * whether all memory blocks in question are offline and trigger a BUG()
1908 * if this is not the case.
1910 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
1911 check_memblock_offlined_cb);
1912 if (ret)
1913 BUG();
1915 /* remove memmap entry */
1916 firmware_map_remove(start, start + size, "System RAM");
1917 memblock_free(start, size);
1918 memblock_remove(start, size);
1921 * Memory block device removal under the device_hotplug_lock is
1922 * a barrier against racing online attempts.
1924 remove_memory_block_devices(start, size);
1926 mem_hotplug_begin();
1928 arch_remove_memory(nid, start, size, NULL);
1929 __release_memory_resource(start, size);
1931 try_offline_node(nid);
1933 mem_hotplug_done();
1936 void remove_memory(int nid, u64 start, u64 size)
1938 lock_device_hotplug();
1939 __remove_memory(nid, start, size);
1940 unlock_device_hotplug();
1942 EXPORT_SYMBOL_GPL(remove_memory);
1943 #endif /* CONFIG_MEMORY_HOTREMOVE */