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