]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - mm/memory_hotplug.c
UBUNTU: upstream stable to v4.19.98, v5.4.14
[mirror_ubuntu-eoan-kernel.git] / mm / memory_hotplug.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/mm/memory_hotplug.c
4 *
5 * Copyright (C)
6 */
7
8 #include <linux/stddef.h>
9 #include <linux/mm.h>
10 #include <linux/sched/signal.h>
11 #include <linux/swap.h>
12 #include <linux/interrupt.h>
13 #include <linux/pagemap.h>
14 #include <linux/compiler.h>
15 #include <linux/export.h>
16 #include <linux/pagevec.h>
17 #include <linux/writeback.h>
18 #include <linux/slab.h>
19 #include <linux/sysctl.h>
20 #include <linux/cpu.h>
21 #include <linux/memory.h>
22 #include <linux/memremap.h>
23 #include <linux/memory_hotplug.h>
24 #include <linux/highmem.h>
25 #include <linux/vmalloc.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/migrate.h>
29 #include <linux/page-isolation.h>
30 #include <linux/pfn.h>
31 #include <linux/suspend.h>
32 #include <linux/mm_inline.h>
33 #include <linux/firmware-map.h>
34 #include <linux/stop_machine.h>
35 #include <linux/hugetlb.h>
36 #include <linux/memblock.h>
37 #include <linux/compaction.h>
38 #include <linux/rmap.h>
39
40 #include <asm/tlbflush.h>
41
42 #include "internal.h"
43 #include "shuffle.h"
44
45 /*
46 * online_page_callback contains pointer to current page onlining function.
47 * Initially it is generic_online_page(). If it is required it could be
48 * changed by calling set_online_page_callback() for callback registration
49 * and restore_online_page_callback() for generic callback restore.
50 */
51
52 static void generic_online_page(struct page *page, unsigned int order);
53
54 static online_page_callback_t online_page_callback = generic_online_page;
55 static DEFINE_MUTEX(online_page_callback_lock);
56
57 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
58
59 static int default_kernel_zone = ZONE_NORMAL;
60
61 void get_online_mems(void)
62 {
63 percpu_down_read(&mem_hotplug_lock);
64 }
65
66 void put_online_mems(void)
67 {
68 percpu_up_read(&mem_hotplug_lock);
69 }
70
71 bool movable_node_enabled = false;
72
73 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
74 bool memhp_auto_online;
75 #else
76 bool memhp_auto_online = true;
77 #endif
78 EXPORT_SYMBOL_GPL(memhp_auto_online);
79
80 static int __init setup_memhp_default_state(char *str)
81 {
82 if (!strcmp(str, "online"))
83 memhp_auto_online = true;
84 else if (!strcmp(str, "offline"))
85 memhp_auto_online = false;
86
87 return 1;
88 }
89 __setup("memhp_default_state=", setup_memhp_default_state);
90
91 void mem_hotplug_begin(void)
92 {
93 cpus_read_lock();
94 percpu_down_write(&mem_hotplug_lock);
95 }
96
97 void mem_hotplug_done(void)
98 {
99 percpu_up_write(&mem_hotplug_lock);
100 cpus_read_unlock();
101 }
102
103 u64 max_mem_size = U64_MAX;
104
105 /* add this memory to iomem resource */
106 static struct resource *register_memory_resource(u64 start, u64 size)
107 {
108 struct resource *res;
109 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
110 char *resource_name = "System RAM";
111
112 if (start + size > max_mem_size)
113 return ERR_PTR(-E2BIG);
114
115 /*
116 * Request ownership of the new memory range. This might be
117 * a child of an existing resource that was present but
118 * not marked as busy.
119 */
120 res = __request_region(&iomem_resource, start, size,
121 resource_name, flags);
122
123 if (!res) {
124 pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
125 start, start + size);
126 return ERR_PTR(-EEXIST);
127 }
128 return res;
129 }
130
131 static void release_memory_resource(struct resource *res)
132 {
133 if (!res)
134 return;
135 release_resource(res);
136 kfree(res);
137 }
138
139 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
140 void get_page_bootmem(unsigned long info, struct page *page,
141 unsigned long type)
142 {
143 page->freelist = (void *)type;
144 SetPagePrivate(page);
145 set_page_private(page, info);
146 page_ref_inc(page);
147 }
148
149 void put_page_bootmem(struct page *page)
150 {
151 unsigned long type;
152
153 type = (unsigned long) page->freelist;
154 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
155 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
156
157 if (page_ref_dec_return(page) == 1) {
158 page->freelist = NULL;
159 ClearPagePrivate(page);
160 set_page_private(page, 0);
161 INIT_LIST_HEAD(&page->lru);
162 free_reserved_page(page);
163 }
164 }
165
166 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
167 #ifndef CONFIG_SPARSEMEM_VMEMMAP
168 static void register_page_bootmem_info_section(unsigned long start_pfn)
169 {
170 unsigned long mapsize, section_nr, i;
171 struct mem_section *ms;
172 struct page *page, *memmap;
173 struct mem_section_usage *usage;
174
175 section_nr = pfn_to_section_nr(start_pfn);
176 ms = __nr_to_section(section_nr);
177
178 /* Get section's memmap address */
179 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
180
181 /*
182 * Get page for the memmap's phys address
183 * XXX: need more consideration for sparse_vmemmap...
184 */
185 page = virt_to_page(memmap);
186 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
187 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
188
189 /* remember memmap's page */
190 for (i = 0; i < mapsize; i++, page++)
191 get_page_bootmem(section_nr, page, SECTION_INFO);
192
193 usage = ms->usage;
194 page = virt_to_page(usage);
195
196 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
197
198 for (i = 0; i < mapsize; i++, page++)
199 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
200
201 }
202 #else /* CONFIG_SPARSEMEM_VMEMMAP */
203 static void register_page_bootmem_info_section(unsigned long start_pfn)
204 {
205 unsigned long mapsize, section_nr, i;
206 struct mem_section *ms;
207 struct page *page, *memmap;
208 struct mem_section_usage *usage;
209
210 section_nr = pfn_to_section_nr(start_pfn);
211 ms = __nr_to_section(section_nr);
212
213 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
214
215 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
216
217 usage = ms->usage;
218 page = virt_to_page(usage);
219
220 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
221
222 for (i = 0; i < mapsize; i++, page++)
223 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
224 }
225 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
226
227 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
228 {
229 unsigned long i, pfn, end_pfn, nr_pages;
230 int node = pgdat->node_id;
231 struct page *page;
232
233 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
234 page = virt_to_page(pgdat);
235
236 for (i = 0; i < nr_pages; i++, page++)
237 get_page_bootmem(node, page, NODE_INFO);
238
239 pfn = pgdat->node_start_pfn;
240 end_pfn = pgdat_end_pfn(pgdat);
241
242 /* register section info */
243 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
244 /*
245 * Some platforms can assign the same pfn to multiple nodes - on
246 * node0 as well as nodeN. To avoid registering a pfn against
247 * multiple nodes we check that this pfn does not already
248 * reside in some other nodes.
249 */
250 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
251 register_page_bootmem_info_section(pfn);
252 }
253 }
254 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
255
256 static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
257 const char *reason)
258 {
259 /*
260 * Disallow all operations smaller than a sub-section and only
261 * allow operations smaller than a section for
262 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
263 * enforces a larger memory_block_size_bytes() granularity for
264 * memory that will be marked online, so this check should only
265 * fire for direct arch_{add,remove}_memory() users outside of
266 * add_memory_resource().
267 */
268 unsigned long min_align;
269
270 if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
271 min_align = PAGES_PER_SUBSECTION;
272 else
273 min_align = PAGES_PER_SECTION;
274 if (!IS_ALIGNED(pfn, min_align)
275 || !IS_ALIGNED(nr_pages, min_align)) {
276 WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n",
277 reason, pfn, pfn + nr_pages - 1);
278 return -EINVAL;
279 }
280 return 0;
281 }
282
283 /*
284 * Reasonably generic function for adding memory. It is
285 * expected that archs that support memory hotplug will
286 * call this function after deciding the zone to which to
287 * add the new pages.
288 */
289 int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
290 struct mhp_restrictions *restrictions)
291 {
292 int err;
293 unsigned long nr, start_sec, end_sec;
294 struct vmem_altmap *altmap = restrictions->altmap;
295
296 if (altmap) {
297 /*
298 * Validate altmap is within bounds of the total request
299 */
300 if (altmap->base_pfn != pfn
301 || vmem_altmap_offset(altmap) > nr_pages) {
302 pr_warn_once("memory add fail, invalid altmap\n");
303 return -EINVAL;
304 }
305 altmap->alloc = 0;
306 }
307
308 err = check_pfn_span(pfn, nr_pages, "add");
309 if (err)
310 return err;
311
312 start_sec = pfn_to_section_nr(pfn);
313 end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
314 for (nr = start_sec; nr <= end_sec; nr++) {
315 unsigned long pfns;
316
317 pfns = min(nr_pages, PAGES_PER_SECTION
318 - (pfn & ~PAGE_SECTION_MASK));
319 err = sparse_add_section(nid, pfn, pfns, altmap);
320 if (err)
321 break;
322 pfn += pfns;
323 nr_pages -= pfns;
324 cond_resched();
325 }
326 vmemmap_populate_print_last();
327 return err;
328 }
329
330 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
331 static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
332 unsigned long start_pfn,
333 unsigned long end_pfn)
334 {
335 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
336 if (unlikely(!pfn_to_online_page(start_pfn)))
337 continue;
338
339 if (unlikely(pfn_to_nid(start_pfn) != nid))
340 continue;
341
342 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
343 continue;
344
345 return start_pfn;
346 }
347
348 return 0;
349 }
350
351 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
352 static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
353 unsigned long start_pfn,
354 unsigned long end_pfn)
355 {
356 unsigned long pfn;
357
358 /* pfn is the end pfn of a memory section. */
359 pfn = end_pfn - 1;
360 for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
361 if (unlikely(!pfn_to_online_page(pfn)))
362 continue;
363
364 if (unlikely(pfn_to_nid(pfn) != nid))
365 continue;
366
367 if (zone && zone != page_zone(pfn_to_page(pfn)))
368 continue;
369
370 return pfn;
371 }
372
373 return 0;
374 }
375
376 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
377 unsigned long end_pfn)
378 {
379 unsigned long zone_start_pfn = zone->zone_start_pfn;
380 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
381 unsigned long zone_end_pfn = z;
382 unsigned long pfn;
383 int nid = zone_to_nid(zone);
384
385 zone_span_writelock(zone);
386 if (zone_start_pfn == start_pfn) {
387 /*
388 * If the section is smallest section in the zone, it need
389 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
390 * In this case, we find second smallest valid mem_section
391 * for shrinking zone.
392 */
393 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
394 zone_end_pfn);
395 if (pfn) {
396 zone->zone_start_pfn = pfn;
397 zone->spanned_pages = zone_end_pfn - pfn;
398 }
399 } else if (zone_end_pfn == end_pfn) {
400 /*
401 * If the section is biggest section in the zone, it need
402 * shrink zone->spanned_pages.
403 * In this case, we find second biggest valid mem_section for
404 * shrinking zone.
405 */
406 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
407 start_pfn);
408 if (pfn)
409 zone->spanned_pages = pfn - zone_start_pfn + 1;
410 }
411
412 /*
413 * The section is not biggest or smallest mem_section in the zone, it
414 * only creates a hole in the zone. So in this case, we need not
415 * change the zone. But perhaps, the zone has only hole data. Thus
416 * it check the zone has only hole or not.
417 */
418 pfn = zone_start_pfn;
419 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SUBSECTION) {
420 if (unlikely(!pfn_to_online_page(pfn)))
421 continue;
422
423 if (page_zone(pfn_to_page(pfn)) != zone)
424 continue;
425
426 /* Skip range to be removed */
427 if (pfn >= start_pfn && pfn < end_pfn)
428 continue;
429
430 /* If we find valid section, we have nothing to do */
431 zone_span_writeunlock(zone);
432 return;
433 }
434
435 /* The zone has no valid section */
436 zone->zone_start_pfn = 0;
437 zone->spanned_pages = 0;
438 zone_span_writeunlock(zone);
439 }
440
441 static void update_pgdat_span(struct pglist_data *pgdat)
442 {
443 unsigned long node_start_pfn = 0, node_end_pfn = 0;
444 struct zone *zone;
445
446 for (zone = pgdat->node_zones;
447 zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
448 unsigned long zone_end_pfn = zone->zone_start_pfn +
449 zone->spanned_pages;
450
451 /* No need to lock the zones, they can't change. */
452 if (!zone->spanned_pages)
453 continue;
454 if (!node_end_pfn) {
455 node_start_pfn = zone->zone_start_pfn;
456 node_end_pfn = zone_end_pfn;
457 continue;
458 }
459
460 if (zone_end_pfn > node_end_pfn)
461 node_end_pfn = zone_end_pfn;
462 if (zone->zone_start_pfn < node_start_pfn)
463 node_start_pfn = zone->zone_start_pfn;
464 }
465
466 pgdat->node_start_pfn = node_start_pfn;
467 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
468 }
469
470 void __ref remove_pfn_range_from_zone(struct zone *zone,
471 unsigned long start_pfn,
472 unsigned long nr_pages)
473 {
474 struct pglist_data *pgdat = zone->zone_pgdat;
475 unsigned long flags;
476
477 #ifdef CONFIG_ZONE_DEVICE
478 /*
479 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
480 * we will not try to shrink the zones - which is okay as
481 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
482 */
483 if (zone_idx(zone) == ZONE_DEVICE)
484 return;
485 #endif
486
487 clear_zone_contiguous(zone);
488
489 pgdat_resize_lock(zone->zone_pgdat, &flags);
490 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
491 update_pgdat_span(pgdat);
492 pgdat_resize_unlock(zone->zone_pgdat, &flags);
493
494 set_zone_contiguous(zone);
495 }
496
497 static void __remove_section(unsigned long pfn, unsigned long nr_pages,
498 unsigned long map_offset,
499 struct vmem_altmap *altmap)
500 {
501 struct mem_section *ms = __nr_to_section(pfn_to_section_nr(pfn));
502
503 if (WARN_ON_ONCE(!valid_section(ms)))
504 return;
505
506 sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
507 }
508
509 /**
510 * __remove_pages() - remove sections of pages
511 * @pfn: starting pageframe (must be aligned to start of a section)
512 * @nr_pages: number of pages to remove (must be multiple of section size)
513 * @altmap: alternative device page map or %NULL if default memmap is used
514 *
515 * Generic helper function to remove section mappings and sysfs entries
516 * for the section of the memory we are removing. Caller needs to make
517 * sure that pages are marked reserved and zones are adjust properly by
518 * calling offline_pages().
519 */
520 void __remove_pages(unsigned long pfn, unsigned long nr_pages,
521 struct vmem_altmap *altmap)
522 {
523 unsigned long map_offset = 0;
524 unsigned long nr, start_sec, end_sec;
525
526 map_offset = vmem_altmap_offset(altmap);
527
528 if (check_pfn_span(pfn, nr_pages, "remove"))
529 return;
530
531 start_sec = pfn_to_section_nr(pfn);
532 end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
533 for (nr = start_sec; nr <= end_sec; nr++) {
534 unsigned long pfns;
535
536 cond_resched();
537 pfns = min(nr_pages, PAGES_PER_SECTION
538 - (pfn & ~PAGE_SECTION_MASK));
539 __remove_section(pfn, pfns, map_offset, altmap);
540 pfn += pfns;
541 nr_pages -= pfns;
542 map_offset = 0;
543 }
544 }
545
546 int set_online_page_callback(online_page_callback_t callback)
547 {
548 int rc = -EINVAL;
549
550 get_online_mems();
551 mutex_lock(&online_page_callback_lock);
552
553 if (online_page_callback == generic_online_page) {
554 online_page_callback = callback;
555 rc = 0;
556 }
557
558 mutex_unlock(&online_page_callback_lock);
559 put_online_mems();
560
561 return rc;
562 }
563 EXPORT_SYMBOL_GPL(set_online_page_callback);
564
565 int restore_online_page_callback(online_page_callback_t callback)
566 {
567 int rc = -EINVAL;
568
569 get_online_mems();
570 mutex_lock(&online_page_callback_lock);
571
572 if (online_page_callback == callback) {
573 online_page_callback = generic_online_page;
574 rc = 0;
575 }
576
577 mutex_unlock(&online_page_callback_lock);
578 put_online_mems();
579
580 return rc;
581 }
582 EXPORT_SYMBOL_GPL(restore_online_page_callback);
583
584 void __online_page_set_limits(struct page *page)
585 {
586 }
587 EXPORT_SYMBOL_GPL(__online_page_set_limits);
588
589 void __online_page_increment_counters(struct page *page)
590 {
591 adjust_managed_page_count(page, 1);
592 }
593 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
594
595 void __online_page_free(struct page *page)
596 {
597 __free_reserved_page(page);
598 }
599 EXPORT_SYMBOL_GPL(__online_page_free);
600
601 static void generic_online_page(struct page *page, unsigned int order)
602 {
603 kernel_map_pages(page, 1 << order, 1);
604 __free_pages_core(page, order);
605 totalram_pages_add(1UL << order);
606 #ifdef CONFIG_HIGHMEM
607 if (PageHighMem(page))
608 totalhigh_pages_add(1UL << order);
609 #endif
610 }
611
612 static int online_pages_blocks(unsigned long start, unsigned long nr_pages)
613 {
614 unsigned long end = start + nr_pages;
615 int order, onlined_pages = 0;
616
617 while (start < end) {
618 order = min(MAX_ORDER - 1,
619 get_order(PFN_PHYS(end) - PFN_PHYS(start)));
620 (*online_page_callback)(pfn_to_page(start), order);
621
622 onlined_pages += (1UL << order);
623 start += (1UL << order);
624 }
625 return onlined_pages;
626 }
627
628 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
629 void *arg)
630 {
631 unsigned long onlined_pages = *(unsigned long *)arg;
632
633 if (PageReserved(pfn_to_page(start_pfn)))
634 onlined_pages += online_pages_blocks(start_pfn, nr_pages);
635
636 online_mem_sections(start_pfn, start_pfn + nr_pages);
637
638 *(unsigned long *)arg = onlined_pages;
639 return 0;
640 }
641
642 /* check which state of node_states will be changed when online memory */
643 static void node_states_check_changes_online(unsigned long nr_pages,
644 struct zone *zone, struct memory_notify *arg)
645 {
646 int nid = zone_to_nid(zone);
647
648 arg->status_change_nid = NUMA_NO_NODE;
649 arg->status_change_nid_normal = NUMA_NO_NODE;
650 arg->status_change_nid_high = NUMA_NO_NODE;
651
652 if (!node_state(nid, N_MEMORY))
653 arg->status_change_nid = nid;
654 if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
655 arg->status_change_nid_normal = nid;
656 #ifdef CONFIG_HIGHMEM
657 if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
658 arg->status_change_nid_high = nid;
659 #endif
660 }
661
662 static void node_states_set_node(int node, struct memory_notify *arg)
663 {
664 if (arg->status_change_nid_normal >= 0)
665 node_set_state(node, N_NORMAL_MEMORY);
666
667 if (arg->status_change_nid_high >= 0)
668 node_set_state(node, N_HIGH_MEMORY);
669
670 if (arg->status_change_nid >= 0)
671 node_set_state(node, N_MEMORY);
672 }
673
674 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
675 unsigned long nr_pages)
676 {
677 unsigned long old_end_pfn = zone_end_pfn(zone);
678
679 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
680 zone->zone_start_pfn = start_pfn;
681
682 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
683 }
684
685 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
686 unsigned long nr_pages)
687 {
688 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
689
690 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
691 pgdat->node_start_pfn = start_pfn;
692
693 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
694 }
695
696 void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
697 unsigned long nr_pages, struct vmem_altmap *altmap)
698 {
699 struct pglist_data *pgdat = zone->zone_pgdat;
700 int nid = pgdat->node_id;
701 unsigned long flags;
702
703 clear_zone_contiguous(zone);
704
705 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
706 pgdat_resize_lock(pgdat, &flags);
707 zone_span_writelock(zone);
708 if (zone_is_empty(zone))
709 init_currently_empty_zone(zone, start_pfn, nr_pages);
710 resize_zone_range(zone, start_pfn, nr_pages);
711 zone_span_writeunlock(zone);
712 resize_pgdat_range(pgdat, start_pfn, nr_pages);
713 pgdat_resize_unlock(pgdat, &flags);
714
715 /*
716 * TODO now we have a visible range of pages which are not associated
717 * with their zone properly. Not nice but set_pfnblock_flags_mask
718 * expects the zone spans the pfn range. All the pages in the range
719 * are reserved so nobody should be touching them so we should be safe
720 */
721 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
722 MEMMAP_HOTPLUG, altmap);
723
724 set_zone_contiguous(zone);
725 }
726
727 void set_default_mem_hotplug_zone(enum zone_type zone)
728 {
729 default_kernel_zone = zone;
730 }
731
732 #ifdef CONFIG_HIGHMEM
733 #define MAX_KERNEL_ZONE ZONE_HIGHMEM
734 #else
735 #define MAX_KERNEL_ZONE ZONE_NORMAL
736 #endif
737
738 /*
739 * Returns a default kernel memory zone for the given pfn range.
740 * If no kernel zone covers this pfn range it will automatically go
741 * to the MAX_KERNEL_ZONE.
742 */
743 static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
744 unsigned long nr_pages)
745 {
746 struct pglist_data *pgdat = NODE_DATA(nid);
747 int zid;
748
749 for (zid = 0; zid <= MAX_KERNEL_ZONE; zid++) {
750 struct zone *zone = &pgdat->node_zones[zid];
751
752 if (zone_intersects(zone, start_pfn, nr_pages))
753 return zone;
754 }
755
756 return &pgdat->node_zones[default_kernel_zone];
757 }
758
759 static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
760 unsigned long nr_pages)
761 {
762 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
763 nr_pages);
764 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
765 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
766 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
767
768 /*
769 * We inherit the existing zone in a simple case where zones do not
770 * overlap in the given range
771 */
772 if (in_kernel ^ in_movable)
773 return (in_kernel) ? kernel_zone : movable_zone;
774
775 /*
776 * If the range doesn't belong to any zone or two zones overlap in the
777 * given range then we use movable zone only if movable_node is
778 * enabled because we always online to a kernel zone by default.
779 */
780 return movable_node_enabled ? movable_zone : kernel_zone;
781 }
782
783 struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
784 unsigned long nr_pages)
785 {
786 if (online_type == MMOP_ONLINE_KERNEL)
787 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
788
789 if (online_type == MMOP_ONLINE_MOVABLE)
790 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
791
792 return default_zone_for_pfn(nid, start_pfn, nr_pages);
793 }
794
795 /*
796 * Associates the given pfn range with the given node and the zone appropriate
797 * for the given online type.
798 */
799 static struct zone * __meminit move_pfn_range(int online_type, int nid,
800 unsigned long start_pfn, unsigned long nr_pages)
801 {
802 struct zone *zone;
803
804 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
805 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
806 return zone;
807 }
808
809 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
810 {
811 unsigned long flags;
812 unsigned long onlined_pages = 0;
813 struct zone *zone;
814 int need_zonelists_rebuild = 0;
815 int nid;
816 int ret;
817 struct memory_notify arg;
818 struct memory_block *mem;
819
820 mem_hotplug_begin();
821
822 /*
823 * We can't use pfn_to_nid() because nid might be stored in struct page
824 * which is not yet initialized. Instead, we find nid from memory block.
825 */
826 mem = find_memory_block(__pfn_to_section(pfn));
827 nid = mem->nid;
828 put_device(&mem->dev);
829
830 /* associate pfn range with the zone */
831 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
832
833 arg.start_pfn = pfn;
834 arg.nr_pages = nr_pages;
835 node_states_check_changes_online(nr_pages, zone, &arg);
836
837 ret = memory_notify(MEM_GOING_ONLINE, &arg);
838 ret = notifier_to_errno(ret);
839 if (ret)
840 goto failed_addition;
841
842 /*
843 * If this zone is not populated, then it is not in zonelist.
844 * This means the page allocator ignores this zone.
845 * So, zonelist must be updated after online.
846 */
847 if (!populated_zone(zone)) {
848 need_zonelists_rebuild = 1;
849 setup_zone_pageset(zone);
850 }
851
852 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
853 online_pages_range);
854 if (ret) {
855 if (need_zonelists_rebuild)
856 zone_pcp_reset(zone);
857 goto failed_addition;
858 }
859
860 zone->present_pages += onlined_pages;
861
862 pgdat_resize_lock(zone->zone_pgdat, &flags);
863 zone->zone_pgdat->node_present_pages += onlined_pages;
864 pgdat_resize_unlock(zone->zone_pgdat, &flags);
865
866 shuffle_zone(zone);
867
868 if (onlined_pages) {
869 node_states_set_node(nid, &arg);
870 if (need_zonelists_rebuild)
871 build_all_zonelists(NULL);
872 else
873 zone_pcp_update(zone);
874 }
875
876 init_per_zone_wmark_min();
877
878 if (onlined_pages) {
879 kswapd_run(nid);
880 kcompactd_run(nid);
881 }
882
883 vm_total_pages = nr_free_pagecache_pages();
884
885 writeback_set_ratelimit();
886
887 if (onlined_pages)
888 memory_notify(MEM_ONLINE, &arg);
889 mem_hotplug_done();
890 return 0;
891
892 failed_addition:
893 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
894 (unsigned long long) pfn << PAGE_SHIFT,
895 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
896 memory_notify(MEM_CANCEL_ONLINE, &arg);
897 remove_pfn_range_from_zone(zone, pfn, nr_pages);
898 mem_hotplug_done();
899 return ret;
900 }
901 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
902
903 static void reset_node_present_pages(pg_data_t *pgdat)
904 {
905 struct zone *z;
906
907 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
908 z->present_pages = 0;
909
910 pgdat->node_present_pages = 0;
911 }
912
913 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
914 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
915 {
916 struct pglist_data *pgdat;
917 unsigned long start_pfn = PFN_DOWN(start);
918
919 pgdat = NODE_DATA(nid);
920 if (!pgdat) {
921 pgdat = arch_alloc_nodedata(nid);
922 if (!pgdat)
923 return NULL;
924
925 arch_refresh_nodedata(nid, pgdat);
926 } else {
927 /*
928 * Reset the nr_zones, order and classzone_idx before reuse.
929 * Note that kswapd will init kswapd_classzone_idx properly
930 * when it starts in the near future.
931 */
932 pgdat->nr_zones = 0;
933 pgdat->kswapd_order = 0;
934 pgdat->kswapd_classzone_idx = 0;
935 }
936
937 /* we can use NODE_DATA(nid) from here */
938
939 pgdat->node_id = nid;
940 pgdat->node_start_pfn = start_pfn;
941
942 /* init node's zones as empty zones, we don't have any present pages.*/
943 free_area_init_core_hotplug(nid);
944 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
945
946 /*
947 * The node we allocated has no zone fallback lists. For avoiding
948 * to access not-initialized zonelist, build here.
949 */
950 build_all_zonelists(pgdat);
951
952 /*
953 * When memory is hot-added, all the memory is in offline state. So
954 * clear all zones' present_pages because they will be updated in
955 * online_pages() and offline_pages().
956 */
957 reset_node_managed_pages(pgdat);
958 reset_node_present_pages(pgdat);
959
960 return pgdat;
961 }
962
963 static void rollback_node_hotadd(int nid)
964 {
965 pg_data_t *pgdat = NODE_DATA(nid);
966
967 arch_refresh_nodedata(nid, NULL);
968 free_percpu(pgdat->per_cpu_nodestats);
969 arch_free_nodedata(pgdat);
970 }
971
972
973 /**
974 * try_online_node - online a node if offlined
975 * @nid: the node ID
976 * @start: start addr of the node
977 * @set_node_online: Whether we want to online the node
978 * called by cpu_up() to online a node without onlined memory.
979 *
980 * Returns:
981 * 1 -> a new node has been allocated
982 * 0 -> the node is already online
983 * -ENOMEM -> the node could not be allocated
984 */
985 static int __try_online_node(int nid, u64 start, bool set_node_online)
986 {
987 pg_data_t *pgdat;
988 int ret = 1;
989
990 if (node_online(nid))
991 return 0;
992
993 pgdat = hotadd_new_pgdat(nid, start);
994 if (!pgdat) {
995 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
996 ret = -ENOMEM;
997 goto out;
998 }
999
1000 if (set_node_online) {
1001 node_set_online(nid);
1002 ret = register_one_node(nid);
1003 BUG_ON(ret);
1004 }
1005 out:
1006 return ret;
1007 }
1008
1009 /*
1010 * Users of this function always want to online/register the node
1011 */
1012 int try_online_node(int nid)
1013 {
1014 int ret;
1015
1016 mem_hotplug_begin();
1017 ret = __try_online_node(nid, 0, true);
1018 mem_hotplug_done();
1019 return ret;
1020 }
1021
1022 static int check_hotplug_memory_range(u64 start, u64 size)
1023 {
1024 /* memory range must be block size aligned */
1025 if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
1026 !IS_ALIGNED(size, memory_block_size_bytes())) {
1027 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1028 memory_block_size_bytes(), start, size);
1029 return -EINVAL;
1030 }
1031
1032 return 0;
1033 }
1034
1035 static int online_memory_block(struct memory_block *mem, void *arg)
1036 {
1037 return device_online(&mem->dev);
1038 }
1039
1040 /*
1041 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1042 * and online/offline operations (triggered e.g. by sysfs).
1043 *
1044 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1045 */
1046 int __ref add_memory_resource(int nid, struct resource *res)
1047 {
1048 struct mhp_restrictions restrictions = {};
1049 u64 start, size;
1050 bool new_node = false;
1051 int ret;
1052
1053 start = res->start;
1054 size = resource_size(res);
1055
1056 ret = check_hotplug_memory_range(start, size);
1057 if (ret)
1058 return ret;
1059
1060 mem_hotplug_begin();
1061
1062 /*
1063 * Add new range to memblock so that when hotadd_new_pgdat() is called
1064 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1065 * this new range and calculate total pages correctly. The range will
1066 * be removed at hot-remove time.
1067 */
1068 memblock_add_node(start, size, nid);
1069
1070 ret = __try_online_node(nid, start, false);
1071 if (ret < 0)
1072 goto error;
1073 new_node = ret;
1074
1075 /* call arch's memory hotadd */
1076 ret = arch_add_memory(nid, start, size, &restrictions);
1077 if (ret < 0)
1078 goto error;
1079
1080 /* create memory block devices after memory was added */
1081 ret = create_memory_block_devices(start, size);
1082 if (ret) {
1083 arch_remove_memory(nid, start, size, NULL);
1084 goto error;
1085 }
1086
1087 if (new_node) {
1088 /* If sysfs file of new node can't be created, cpu on the node
1089 * can't be hot-added. There is no rollback way now.
1090 * So, check by BUG_ON() to catch it reluctantly..
1091 * We online node here. We can't roll back from here.
1092 */
1093 node_set_online(nid);
1094 ret = __register_one_node(nid);
1095 BUG_ON(ret);
1096 }
1097
1098 /* link memory sections under this node.*/
1099 ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
1100 BUG_ON(ret);
1101
1102 /* create new memmap entry */
1103 firmware_map_add_hotplug(start, start + size, "System RAM");
1104
1105 /* device_online() will take the lock when calling online_pages() */
1106 mem_hotplug_done();
1107
1108 /* online pages if requested */
1109 if (memhp_auto_online)
1110 walk_memory_blocks(start, size, NULL, online_memory_block);
1111
1112 return ret;
1113 error:
1114 /* rollback pgdat allocation and others */
1115 if (new_node)
1116 rollback_node_hotadd(nid);
1117 memblock_remove(start, size);
1118 mem_hotplug_done();
1119 return ret;
1120 }
1121
1122 /* requires device_hotplug_lock, see add_memory_resource() */
1123 int __ref __add_memory(int nid, u64 start, u64 size)
1124 {
1125 struct resource *res;
1126 int ret;
1127
1128 res = register_memory_resource(start, size);
1129 if (IS_ERR(res))
1130 return PTR_ERR(res);
1131
1132 ret = add_memory_resource(nid, res);
1133 if (ret < 0)
1134 release_memory_resource(res);
1135 return ret;
1136 }
1137
1138 int add_memory(int nid, u64 start, u64 size)
1139 {
1140 int rc;
1141
1142 lock_device_hotplug();
1143 rc = __add_memory(nid, start, size);
1144 unlock_device_hotplug();
1145
1146 return rc;
1147 }
1148 EXPORT_SYMBOL_GPL(add_memory);
1149
1150 #ifdef CONFIG_MEMORY_HOTREMOVE
1151 /*
1152 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1153 * set and the size of the free page is given by page_order(). Using this,
1154 * the function determines if the pageblock contains only free pages.
1155 * Due to buddy contraints, a free page at least the size of a pageblock will
1156 * be located at the start of the pageblock
1157 */
1158 static inline int pageblock_free(struct page *page)
1159 {
1160 return PageBuddy(page) && page_order(page) >= pageblock_order;
1161 }
1162
1163 /* Return the pfn of the start of the next active pageblock after a given pfn */
1164 static unsigned long next_active_pageblock(unsigned long pfn)
1165 {
1166 struct page *page = pfn_to_page(pfn);
1167
1168 /* Ensure the starting page is pageblock-aligned */
1169 BUG_ON(pfn & (pageblock_nr_pages - 1));
1170
1171 /* If the entire pageblock is free, move to the end of free page */
1172 if (pageblock_free(page)) {
1173 int order;
1174 /* be careful. we don't have locks, page_order can be changed.*/
1175 order = page_order(page);
1176 if ((order < MAX_ORDER) && (order >= pageblock_order))
1177 return pfn + (1 << order);
1178 }
1179
1180 return pfn + pageblock_nr_pages;
1181 }
1182
1183 static bool is_pageblock_removable_nolock(unsigned long pfn)
1184 {
1185 struct page *page = pfn_to_page(pfn);
1186 struct zone *zone;
1187
1188 /*
1189 * We have to be careful here because we are iterating over memory
1190 * sections which are not zone aware so we might end up outside of
1191 * the zone but still within the section.
1192 * We have to take care about the node as well. If the node is offline
1193 * its NODE_DATA will be NULL - see page_zone.
1194 */
1195 if (!node_online(page_to_nid(page)))
1196 return false;
1197
1198 zone = page_zone(page);
1199 pfn = page_to_pfn(page);
1200 if (!zone_spans_pfn(zone, pfn))
1201 return false;
1202
1203 return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON);
1204 }
1205
1206 /* Checks if this range of memory is likely to be hot-removable. */
1207 bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1208 {
1209 unsigned long end_pfn, pfn;
1210
1211 end_pfn = min(start_pfn + nr_pages,
1212 zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
1213
1214 /* Check the starting page of each pageblock within the range */
1215 for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1216 if (!is_pageblock_removable_nolock(pfn))
1217 return false;
1218 cond_resched();
1219 }
1220
1221 /* All pageblocks in the memory block are likely to be hot-removable */
1222 return true;
1223 }
1224
1225 /*
1226 * Confirm all pages in a range [start, end) belong to the same zone.
1227 * When true, return its valid [start, end).
1228 */
1229 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1230 unsigned long *valid_start, unsigned long *valid_end)
1231 {
1232 unsigned long pfn, sec_end_pfn;
1233 unsigned long start, end;
1234 struct zone *zone = NULL;
1235 struct page *page;
1236 int i;
1237 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1238 pfn < end_pfn;
1239 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1240 /* Make sure the memory section is present first */
1241 if (!present_section_nr(pfn_to_section_nr(pfn)))
1242 continue;
1243 for (; pfn < sec_end_pfn && pfn < end_pfn;
1244 pfn += MAX_ORDER_NR_PAGES) {
1245 i = 0;
1246 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1247 while ((i < MAX_ORDER_NR_PAGES) &&
1248 !pfn_valid_within(pfn + i))
1249 i++;
1250 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1251 continue;
1252 /* Check if we got outside of the zone */
1253 if (zone && !zone_spans_pfn(zone, pfn + i))
1254 return 0;
1255 page = pfn_to_page(pfn + i);
1256 if (zone && page_zone(page) != zone)
1257 return 0;
1258 if (!zone)
1259 start = pfn + i;
1260 zone = page_zone(page);
1261 end = pfn + MAX_ORDER_NR_PAGES;
1262 }
1263 }
1264
1265 if (zone) {
1266 *valid_start = start;
1267 *valid_end = min(end, end_pfn);
1268 return 1;
1269 } else {
1270 return 0;
1271 }
1272 }
1273
1274 /*
1275 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1276 * non-lru movable pages and hugepages). We scan pfn because it's much
1277 * easier than scanning over linked list. This function returns the pfn
1278 * of the first found movable page if it's found, otherwise 0.
1279 */
1280 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1281 {
1282 unsigned long pfn;
1283
1284 for (pfn = start; pfn < end; pfn++) {
1285 struct page *page, *head;
1286 unsigned long skip;
1287
1288 if (!pfn_valid(pfn))
1289 continue;
1290 page = pfn_to_page(pfn);
1291 if (PageLRU(page))
1292 return pfn;
1293 if (__PageMovable(page))
1294 return pfn;
1295
1296 if (!PageHuge(page))
1297 continue;
1298 head = compound_head(page);
1299 if (page_huge_active(head))
1300 return pfn;
1301 skip = (1 << compound_order(head)) - (page - head);
1302 pfn += skip - 1;
1303 }
1304 return 0;
1305 }
1306
1307 static struct page *new_node_page(struct page *page, unsigned long private)
1308 {
1309 int nid = page_to_nid(page);
1310 nodemask_t nmask = node_states[N_MEMORY];
1311
1312 /*
1313 * try to allocate from a different node but reuse this node if there
1314 * are no other online nodes to be used (e.g. we are offlining a part
1315 * of the only existing node)
1316 */
1317 node_clear(nid, nmask);
1318 if (nodes_empty(nmask))
1319 node_set(nid, nmask);
1320
1321 return new_page_nodemask(page, nid, &nmask);
1322 }
1323
1324 static int
1325 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1326 {
1327 unsigned long pfn;
1328 struct page *page;
1329 int ret = 0;
1330 LIST_HEAD(source);
1331
1332 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1333 if (!pfn_valid(pfn))
1334 continue;
1335 page = pfn_to_page(pfn);
1336
1337 if (PageHuge(page)) {
1338 struct page *head = compound_head(page);
1339 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1340 isolate_huge_page(head, &source);
1341 continue;
1342 } else if (PageTransHuge(page))
1343 pfn = page_to_pfn(compound_head(page))
1344 + hpage_nr_pages(page) - 1;
1345
1346 /*
1347 * HWPoison pages have elevated reference counts so the migration would
1348 * fail on them. It also doesn't make any sense to migrate them in the
1349 * first place. Still try to unmap such a page in case it is still mapped
1350 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1351 * the unmap as the catch all safety net).
1352 */
1353 if (PageHWPoison(page)) {
1354 if (WARN_ON(PageLRU(page)))
1355 isolate_lru_page(page);
1356 if (page_mapped(page))
1357 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1358 continue;
1359 }
1360
1361 if (!get_page_unless_zero(page))
1362 continue;
1363 /*
1364 * We can skip free pages. And we can deal with pages on
1365 * LRU and non-lru movable pages.
1366 */
1367 if (PageLRU(page))
1368 ret = isolate_lru_page(page);
1369 else
1370 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1371 if (!ret) { /* Success */
1372 list_add_tail(&page->lru, &source);
1373 if (!__PageMovable(page))
1374 inc_node_page_state(page, NR_ISOLATED_ANON +
1375 page_is_file_cache(page));
1376
1377 } else {
1378 pr_warn("failed to isolate pfn %lx\n", pfn);
1379 dump_page(page, "isolation failed");
1380 }
1381 put_page(page);
1382 }
1383 if (!list_empty(&source)) {
1384 /* Allocate a new page from the nearest neighbor node */
1385 ret = migrate_pages(&source, new_node_page, NULL, 0,
1386 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1387 if (ret) {
1388 list_for_each_entry(page, &source, lru) {
1389 pr_warn("migrating pfn %lx failed ret:%d ",
1390 page_to_pfn(page), ret);
1391 dump_page(page, "migration failure");
1392 }
1393 putback_movable_pages(&source);
1394 }
1395 }
1396
1397 return ret;
1398 }
1399
1400 /*
1401 * remove from free_area[] and mark all as Reserved.
1402 */
1403 static int
1404 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1405 void *data)
1406 {
1407 unsigned long *offlined_pages = (unsigned long *)data;
1408
1409 *offlined_pages += __offline_isolated_pages(start, start + nr_pages);
1410 return 0;
1411 }
1412
1413 /*
1414 * Check all pages in range, recoreded as memory resource, are isolated.
1415 */
1416 static int
1417 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1418 void *data)
1419 {
1420 return test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1421 }
1422
1423 static int __init cmdline_parse_movable_node(char *p)
1424 {
1425 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1426 movable_node_enabled = true;
1427 #else
1428 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1429 #endif
1430 return 0;
1431 }
1432 early_param("movable_node", cmdline_parse_movable_node);
1433
1434 /* check which state of node_states will be changed when offline memory */
1435 static void node_states_check_changes_offline(unsigned long nr_pages,
1436 struct zone *zone, struct memory_notify *arg)
1437 {
1438 struct pglist_data *pgdat = zone->zone_pgdat;
1439 unsigned long present_pages = 0;
1440 enum zone_type zt;
1441
1442 arg->status_change_nid = NUMA_NO_NODE;
1443 arg->status_change_nid_normal = NUMA_NO_NODE;
1444 arg->status_change_nid_high = NUMA_NO_NODE;
1445
1446 /*
1447 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
1448 * If the memory to be offline is within the range
1449 * [0..ZONE_NORMAL], and it is the last present memory there,
1450 * the zones in that range will become empty after the offlining,
1451 * thus we can determine that we need to clear the node from
1452 * node_states[N_NORMAL_MEMORY].
1453 */
1454 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1455 present_pages += pgdat->node_zones[zt].present_pages;
1456 if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
1457 arg->status_change_nid_normal = zone_to_nid(zone);
1458
1459 #ifdef CONFIG_HIGHMEM
1460 /*
1461 * node_states[N_HIGH_MEMORY] contains nodes which
1462 * have normal memory or high memory.
1463 * Here we add the present_pages belonging to ZONE_HIGHMEM.
1464 * If the zone is within the range of [0..ZONE_HIGHMEM), and
1465 * we determine that the zones in that range become empty,
1466 * we need to clear the node for N_HIGH_MEMORY.
1467 */
1468 present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1469 if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
1470 arg->status_change_nid_high = zone_to_nid(zone);
1471 #endif
1472
1473 /*
1474 * We have accounted the pages from [0..ZONE_NORMAL), and
1475 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
1476 * as well.
1477 * Here we count the possible pages from ZONE_MOVABLE.
1478 * If after having accounted all the pages, we see that the nr_pages
1479 * to be offlined is over or equal to the accounted pages,
1480 * we know that the node will become empty, and so, we can clear
1481 * it for N_MEMORY as well.
1482 */
1483 present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1484
1485 if (nr_pages >= present_pages)
1486 arg->status_change_nid = zone_to_nid(zone);
1487 }
1488
1489 static void node_states_clear_node(int node, struct memory_notify *arg)
1490 {
1491 if (arg->status_change_nid_normal >= 0)
1492 node_clear_state(node, N_NORMAL_MEMORY);
1493
1494 if (arg->status_change_nid_high >= 0)
1495 node_clear_state(node, N_HIGH_MEMORY);
1496
1497 if (arg->status_change_nid >= 0)
1498 node_clear_state(node, N_MEMORY);
1499 }
1500
1501 static int __ref __offline_pages(unsigned long start_pfn,
1502 unsigned long end_pfn)
1503 {
1504 unsigned long pfn, nr_pages;
1505 unsigned long offlined_pages = 0;
1506 int ret, node, nr_isolate_pageblock;
1507 unsigned long flags;
1508 unsigned long valid_start, valid_end;
1509 struct zone *zone;
1510 struct memory_notify arg;
1511 char *reason;
1512
1513 mem_hotplug_begin();
1514
1515 /* This makes hotplug much easier...and readable.
1516 we assume this for now. .*/
1517 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1518 &valid_end)) {
1519 ret = -EINVAL;
1520 reason = "multizone range";
1521 goto failed_removal;
1522 }
1523
1524 zone = page_zone(pfn_to_page(valid_start));
1525 node = zone_to_nid(zone);
1526 nr_pages = end_pfn - start_pfn;
1527
1528 /* set above range as isolated */
1529 ret = start_isolate_page_range(start_pfn, end_pfn,
1530 MIGRATE_MOVABLE,
1531 SKIP_HWPOISON | REPORT_FAILURE);
1532 if (ret < 0) {
1533 reason = "failure to isolate range";
1534 goto failed_removal;
1535 }
1536 nr_isolate_pageblock = ret;
1537
1538 arg.start_pfn = start_pfn;
1539 arg.nr_pages = nr_pages;
1540 node_states_check_changes_offline(nr_pages, zone, &arg);
1541
1542 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1543 ret = notifier_to_errno(ret);
1544 if (ret) {
1545 reason = "notifier failure";
1546 goto failed_removal_isolated;
1547 }
1548
1549 do {
1550 for (pfn = start_pfn; pfn;) {
1551 if (signal_pending(current)) {
1552 ret = -EINTR;
1553 reason = "signal backoff";
1554 goto failed_removal_isolated;
1555 }
1556
1557 cond_resched();
1558 lru_add_drain_all();
1559
1560 pfn = scan_movable_pages(pfn, end_pfn);
1561 if (pfn) {
1562 /*
1563 * TODO: fatal migration failures should bail
1564 * out
1565 */
1566 do_migrate_range(pfn, end_pfn);
1567 }
1568 }
1569
1570 /*
1571 * Dissolve free hugepages in the memory block before doing
1572 * offlining actually in order to make hugetlbfs's object
1573 * counting consistent.
1574 */
1575 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1576 if (ret) {
1577 reason = "failure to dissolve huge pages";
1578 goto failed_removal_isolated;
1579 }
1580 /* check again */
1581 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
1582 NULL, check_pages_isolated_cb);
1583 } while (ret);
1584
1585 /* Ok, all of our target is isolated.
1586 We cannot do rollback at this point. */
1587 walk_system_ram_range(start_pfn, end_pfn - start_pfn,
1588 &offlined_pages, offline_isolated_pages_cb);
1589 pr_info("Offlined Pages %ld\n", offlined_pages);
1590 /*
1591 * Onlining will reset pagetype flags and makes migrate type
1592 * MOVABLE, so just need to decrease the number of isolated
1593 * pageblocks zone counter here.
1594 */
1595 spin_lock_irqsave(&zone->lock, flags);
1596 zone->nr_isolate_pageblock -= nr_isolate_pageblock;
1597 spin_unlock_irqrestore(&zone->lock, flags);
1598
1599 /* removal success */
1600 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1601 zone->present_pages -= offlined_pages;
1602
1603 pgdat_resize_lock(zone->zone_pgdat, &flags);
1604 zone->zone_pgdat->node_present_pages -= offlined_pages;
1605 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1606
1607 init_per_zone_wmark_min();
1608
1609 if (!populated_zone(zone)) {
1610 zone_pcp_reset(zone);
1611 build_all_zonelists(NULL);
1612 } else
1613 zone_pcp_update(zone);
1614
1615 node_states_clear_node(node, &arg);
1616 if (arg.status_change_nid >= 0) {
1617 kswapd_stop(node);
1618 kcompactd_stop(node);
1619 }
1620
1621 vm_total_pages = nr_free_pagecache_pages();
1622 writeback_set_ratelimit();
1623
1624 memory_notify(MEM_OFFLINE, &arg);
1625 remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
1626 mem_hotplug_done();
1627 return 0;
1628
1629 failed_removal_isolated:
1630 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1631 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1632 failed_removal:
1633 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1634 (unsigned long long) start_pfn << PAGE_SHIFT,
1635 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
1636 reason);
1637 /* pushback to free area */
1638 mem_hotplug_done();
1639 return ret;
1640 }
1641
1642 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1643 {
1644 return __offline_pages(start_pfn, start_pfn + nr_pages);
1645 }
1646
1647 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1648 {
1649 int ret = !is_memblock_offlined(mem);
1650
1651 if (unlikely(ret)) {
1652 phys_addr_t beginpa, endpa;
1653
1654 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1655 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1656 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1657 &beginpa, &endpa);
1658
1659 return -EBUSY;
1660 }
1661 return 0;
1662 }
1663
1664 static int check_cpu_on_node(pg_data_t *pgdat)
1665 {
1666 int cpu;
1667
1668 for_each_present_cpu(cpu) {
1669 if (cpu_to_node(cpu) == pgdat->node_id)
1670 /*
1671 * the cpu on this node isn't removed, and we can't
1672 * offline this node.
1673 */
1674 return -EBUSY;
1675 }
1676
1677 return 0;
1678 }
1679
1680 static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
1681 {
1682 int nid = *(int *)arg;
1683
1684 /*
1685 * If a memory block belongs to multiple nodes, the stored nid is not
1686 * reliable. However, such blocks are always online (e.g., cannot get
1687 * offlined) and, therefore, are still spanned by the node.
1688 */
1689 return mem->nid == nid ? -EEXIST : 0;
1690 }
1691
1692 /**
1693 * try_offline_node
1694 * @nid: the node ID
1695 *
1696 * Offline a node if all memory sections and cpus of the node are removed.
1697 *
1698 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1699 * and online/offline operations before this call.
1700 */
1701 void try_offline_node(int nid)
1702 {
1703 pg_data_t *pgdat = NODE_DATA(nid);
1704 int rc;
1705
1706 /*
1707 * If the node still spans pages (especially ZONE_DEVICE), don't
1708 * offline it. A node spans memory after move_pfn_range_to_zone(),
1709 * e.g., after the memory block was onlined.
1710 */
1711 if (pgdat->node_spanned_pages)
1712 return;
1713
1714 /*
1715 * Especially offline memory blocks might not be spanned by the
1716 * node. They will get spanned by the node once they get onlined.
1717 * However, they link to the node in sysfs and can get onlined later.
1718 */
1719 rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
1720 if (rc)
1721 return;
1722
1723 if (check_cpu_on_node(pgdat))
1724 return;
1725
1726 /*
1727 * all memory/cpu of this node are removed, we can offline this
1728 * node now.
1729 */
1730 node_set_offline(nid);
1731 unregister_one_node(nid);
1732 }
1733 EXPORT_SYMBOL(try_offline_node);
1734
1735 static void __release_memory_resource(resource_size_t start,
1736 resource_size_t size)
1737 {
1738 int ret;
1739
1740 /*
1741 * When removing memory in the same granularity as it was added,
1742 * this function never fails. It might only fail if resources
1743 * have to be adjusted or split. We'll ignore the error, as
1744 * removing of memory cannot fail.
1745 */
1746 ret = release_mem_region_adjustable(&iomem_resource, start, size);
1747 if (ret) {
1748 resource_size_t endres = start + size - 1;
1749
1750 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1751 &start, &endres, ret);
1752 }
1753 }
1754
1755 static int __ref try_remove_memory(int nid, u64 start, u64 size)
1756 {
1757 int rc = 0;
1758
1759 BUG_ON(check_hotplug_memory_range(start, size));
1760
1761 mem_hotplug_begin();
1762
1763 /*
1764 * All memory blocks must be offlined before removing memory. Check
1765 * whether all memory blocks in question are offline and return error
1766 * if this is not the case.
1767 */
1768 rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb);
1769 if (rc)
1770 goto done;
1771
1772 /* remove memmap entry */
1773 firmware_map_remove(start, start + size, "System RAM");
1774 memblock_free(start, size);
1775 memblock_remove(start, size);
1776
1777 /* remove memory block devices before removing memory */
1778 remove_memory_block_devices(start, size);
1779
1780 arch_remove_memory(nid, start, size, NULL);
1781 __release_memory_resource(start, size);
1782
1783 try_offline_node(nid);
1784
1785 done:
1786 mem_hotplug_done();
1787 return rc;
1788 }
1789
1790 /**
1791 * remove_memory
1792 * @nid: the node ID
1793 * @start: physical address of the region to remove
1794 * @size: size of the region to remove
1795 *
1796 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1797 * and online/offline operations before this call, as required by
1798 * try_offline_node().
1799 */
1800 void __remove_memory(int nid, u64 start, u64 size)
1801 {
1802
1803 /*
1804 * trigger BUG() is some memory is not offlined prior to calling this
1805 * function
1806 */
1807 if (try_remove_memory(nid, start, size))
1808 BUG();
1809 }
1810
1811 /*
1812 * Remove memory if every memory block is offline, otherwise return -EBUSY is
1813 * some memory is not offline
1814 */
1815 int remove_memory(int nid, u64 start, u64 size)
1816 {
1817 int rc;
1818
1819 lock_device_hotplug();
1820 rc = try_remove_memory(nid, start, size);
1821 unlock_device_hotplug();
1822
1823 return rc;
1824 }
1825 EXPORT_SYMBOL_GPL(remove_memory);
1826 #endif /* CONFIG_MEMORY_HOTREMOVE */