]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - mm/memory_hotplug.c
ath10k: drop fragments with multicast DA for PCIe
[mirror_ubuntu-hirsute-kernel.git] / mm / memory_hotplug.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
3947be19
DH
2/*
3 * linux/mm/memory_hotplug.c
4 *
5 * Copyright (C)
6 */
7
3947be19
DH
8#include <linux/stddef.h>
9#include <linux/mm.h>
174cd4b1 10#include <linux/sched/signal.h>
3947be19
DH
11#include <linux/swap.h>
12#include <linux/interrupt.h>
13#include <linux/pagemap.h>
3947be19 14#include <linux/compiler.h>
b95f1b31 15#include <linux/export.h>
3947be19 16#include <linux/pagevec.h>
2d1d43f6 17#include <linux/writeback.h>
3947be19
DH
18#include <linux/slab.h>
19#include <linux/sysctl.h>
20#include <linux/cpu.h>
21#include <linux/memory.h>
4b94ffdc 22#include <linux/memremap.h>
3947be19
DH
23#include <linux/memory_hotplug.h>
24#include <linux/highmem.h>
25#include <linux/vmalloc.h>
0a547039 26#include <linux/ioport.h>
0c0e6195
KH
27#include <linux/delay.h>
28#include <linux/migrate.h>
29#include <linux/page-isolation.h>
71088785 30#include <linux/pfn.h>
6ad696d2 31#include <linux/suspend.h>
6d9c285a 32#include <linux/mm_inline.h>
d96ae530 33#include <linux/firmware-map.h>
60a5a19e 34#include <linux/stop_machine.h>
c8721bbb 35#include <linux/hugetlb.h>
c5320926 36#include <linux/memblock.h>
698b1b30 37#include <linux/compaction.h>
b15c8726 38#include <linux/rmap.h>
3947be19
DH
39
40#include <asm/tlbflush.h>
41
1e5ad9a3 42#include "internal.h"
e900a918 43#include "shuffle.h"
1e5ad9a3 44
9d0ad8ca
DK
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
9d0ad8ca 52static online_page_callback_t online_page_callback = generic_online_page;
bfc8c901 53static DEFINE_MUTEX(online_page_callback_lock);
9d0ad8ca 54
3f906ba2 55DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
bfc8c901 56
accfb3e0
SF
57static int default_kernel_zone = ZONE_NORMAL;
58
3f906ba2
TG
59void get_online_mems(void)
60{
61 percpu_down_read(&mem_hotplug_lock);
62}
bfc8c901 63
3f906ba2
TG
64void put_online_mems(void)
65{
66 percpu_up_read(&mem_hotplug_lock);
67}
bfc8c901 68
4932381e
MH
69bool movable_node_enabled = false;
70
8604d9e5 71#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
862919e5 72int memhp_default_online_type = MMOP_OFFLINE;
8604d9e5 73#else
862919e5 74int memhp_default_online_type = MMOP_ONLINE;
8604d9e5 75#endif
31bc3858 76
86dd995d
VK
77static int __init setup_memhp_default_state(char *str)
78{
5f47adf7
DH
79 const int online_type = memhp_online_type_from_str(str);
80
81 if (online_type >= 0)
82 memhp_default_online_type = online_type;
86dd995d
VK
83
84 return 1;
85}
86__setup("memhp_default_state=", setup_memhp_default_state);
87
30467e0b 88void mem_hotplug_begin(void)
20d6c96b 89{
3f906ba2
TG
90 cpus_read_lock();
91 percpu_down_write(&mem_hotplug_lock);
20d6c96b
KM
92}
93
30467e0b 94void mem_hotplug_done(void)
bfc8c901 95{
3f906ba2
TG
96 percpu_up_write(&mem_hotplug_lock);
97 cpus_read_unlock();
bfc8c901 98}
20d6c96b 99
357b4da5
JG
100u64 max_mem_size = U64_MAX;
101
45e0b78b 102/* add this memory to iomem resource */
7b7b2721
DH
103static struct resource *register_memory_resource(u64 start, u64 size,
104 const char *resource_name)
45e0b78b 105{
2794129e
DH
106 struct resource *res;
107 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
7b7b2721
DH
108
109 if (strcmp(resource_name, "System RAM"))
7cf603d1 110 flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED;
357b4da5 111
f3cd4c86
BH
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)
357b4da5
JG
119 return ERR_PTR(-E2BIG);
120
2794129e
DH
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);
6f754ba4 132 return ERR_PTR(-EEXIST);
45e0b78b
KM
133 }
134 return res;
135}
136
137static void release_memory_resource(struct resource *res)
138{
139 if (!res)
140 return;
141 release_resource(res);
142 kfree(res);
45e0b78b
KM
143}
144
53947027 145#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
46723bfa
YI
146void get_page_bootmem(unsigned long info, struct page *page,
147 unsigned long type)
04753278 148{
ddffe98d 149 page->freelist = (void *)type;
04753278
YG
150 SetPagePrivate(page);
151 set_page_private(page, info);
fe896d18 152 page_ref_inc(page);
04753278
YG
153}
154
170a5a7e 155void put_page_bootmem(struct page *page)
04753278 156{
5f24ce5f 157 unsigned long type;
04753278 158
ddffe98d 159 type = (unsigned long) page->freelist;
5f24ce5f
AA
160 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
161 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
04753278 162
fe896d18 163 if (page_ref_dec_return(page) == 1) {
ddffe98d 164 page->freelist = NULL;
04753278
YG
165 ClearPagePrivate(page);
166 set_page_private(page, 0);
5f24ce5f 167 INIT_LIST_HEAD(&page->lru);
170a5a7e 168 free_reserved_page(page);
04753278 169 }
04753278
YG
170}
171
46723bfa
YI
172#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
173#ifndef CONFIG_SPARSEMEM_VMEMMAP
d92bc318 174static void register_page_bootmem_info_section(unsigned long start_pfn)
04753278 175{
f1eca35a 176 unsigned long mapsize, section_nr, i;
04753278
YG
177 struct mem_section *ms;
178 struct page *page, *memmap;
f1eca35a 179 struct mem_section_usage *usage;
04753278 180
04753278
YG
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
f1eca35a
DW
199 usage = ms->usage;
200 page = virt_to_page(usage);
04753278 201
f1eca35a 202 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
04753278
YG
203
204 for (i = 0; i < mapsize; i++, page++)
af370fb8 205 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
04753278
YG
206
207}
46723bfa
YI
208#else /* CONFIG_SPARSEMEM_VMEMMAP */
209static void register_page_bootmem_info_section(unsigned long start_pfn)
210{
f1eca35a 211 unsigned long mapsize, section_nr, i;
46723bfa
YI
212 struct mem_section *ms;
213 struct page *page, *memmap;
f1eca35a 214 struct mem_section_usage *usage;
46723bfa 215
46723bfa
YI
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
f1eca35a
DW
223 usage = ms->usage;
224 page = virt_to_page(usage);
46723bfa 225
f1eca35a 226 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
46723bfa
YI
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 */
04753278 232
7ded384a 233void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
04753278
YG
234{
235 unsigned long i, pfn, end_pfn, nr_pages;
236 int node = pgdat->node_id;
237 struct page *page;
04753278
YG
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
04753278 245 pfn = pgdat->node_start_pfn;
c1f19495 246 end_pfn = pgdat_end_pfn(pgdat);
04753278 247
7e9f5eb0 248 /* register section info */
f14851af 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
7e9f5eb0 254 * reside in some other nodes.
f14851af 255 */
f65e91df 256 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
f14851af 257 register_page_bootmem_info_section(pfn);
258 }
04753278 259}
46723bfa 260#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
04753278 261
7ea62160
DW
262static 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
dca4436d
AS
289static 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
4edd7cef
DR
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 */
7ea62160 311int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
f5637d3b 312 struct mhp_params *params)
4edd7cef 313{
6cdd0b30
DH
314 const unsigned long end_pfn = pfn + nr_pages;
315 unsigned long cur_nr_pages;
9a845030 316 int err;
f5637d3b 317 struct vmem_altmap *altmap = params->altmap;
4b94ffdc 318
bfeb022f
LG
319 if (WARN_ON_ONCE(!params->pgprot.pgprot))
320 return -EINVAL;
321
dca4436d
AS
322 err = check_hotplug_memory_addressable(pfn, nr_pages);
323 if (err)
324 return err;
325
4b94ffdc
DW
326 if (altmap) {
327 /*
328 * Validate altmap is within bounds of the total request
329 */
7ea62160 330 if (altmap->base_pfn != pfn
4b94ffdc
DW
331 || vmem_altmap_offset(altmap) > nr_pages) {
332 pr_warn_once("memory add fail, invalid altmap\n");
7ea62160 333 return -EINVAL;
4b94ffdc
DW
334 }
335 altmap->alloc = 0;
336 }
337
7ea62160
DW
338 err = check_pfn_span(pfn, nr_pages, "add");
339 if (err)
340 return err;
341
6cdd0b30
DH
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);
ba72b4c8
DW
347 if (err)
348 break;
f64ac5e6 349 cond_resched();
4edd7cef 350 }
c435a390 351 vmemmap_populate_print_last();
4edd7cef
DR
352 return err;
353}
4edd7cef 354
815121d2 355/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
d09b0137 356static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
815121d2
YI
357 unsigned long start_pfn,
358 unsigned long end_pfn)
359{
49ba3c6b 360 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
7ce700bf 361 if (unlikely(!pfn_to_online_page(start_pfn)))
815121d2
YI
362 continue;
363
364 if (unlikely(pfn_to_nid(start_pfn) != nid))
365 continue;
366
9b05158f 367 if (zone != page_zone(pfn_to_page(start_pfn)))
815121d2
YI
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). */
d09b0137 377static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
815121d2
YI
378 unsigned long start_pfn,
379 unsigned long end_pfn)
380{
815121d2
YI
381 unsigned long pfn;
382
383 /* pfn is the end pfn of a memory section. */
384 pfn = end_pfn - 1;
49ba3c6b 385 for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
7ce700bf 386 if (unlikely(!pfn_to_online_page(pfn)))
815121d2
YI
387 continue;
388
389 if (unlikely(pfn_to_nid(pfn) != nid))
390 continue;
391
9b05158f 392 if (zone != page_zone(pfn_to_page(pfn)))
815121d2
YI
393 continue;
394
395 return pfn;
396 }
397
398 return 0;
399}
400
401static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
402 unsigned long end_pfn)
403{
815121d2 404 unsigned long pfn;
815121d2
YI
405 int nid = zone_to_nid(zone);
406
407 zone_span_writelock(zone);
5d12071c 408 if (zone->zone_start_pfn == start_pfn) {
815121d2
YI
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,
5d12071c 416 zone_end_pfn(zone));
815121d2 417 if (pfn) {
5d12071c 418 zone->spanned_pages = zone_end_pfn(zone) - pfn;
815121d2 419 zone->zone_start_pfn = pfn;
950b68d9
DH
420 } else {
421 zone->zone_start_pfn = 0;
422 zone->spanned_pages = 0;
815121d2 423 }
5d12071c 424 } else if (zone_end_pfn(zone) == end_pfn) {
815121d2
YI
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 */
5d12071c 431 pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn,
815121d2
YI
432 start_pfn);
433 if (pfn)
5d12071c 434 zone->spanned_pages = pfn - zone->zone_start_pfn + 1;
950b68d9
DH
435 else {
436 zone->zone_start_pfn = 0;
437 zone->spanned_pages = 0;
438 }
815121d2 439 }
815121d2
YI
440 zone_span_writeunlock(zone);
441}
442
00d6c019 443static void update_pgdat_span(struct pglist_data *pgdat)
815121d2 444{
00d6c019
DH
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. */
656d5711
DH
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
00d6c019
DH
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;
815121d2
YI
466 }
467
00d6c019
DH
468 pgdat->node_start_pfn = node_start_pfn;
469 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
815121d2
YI
470}
471
feee6b29
DH
472void __ref remove_pfn_range_from_zone(struct zone *zone,
473 unsigned long start_pfn,
474 unsigned long nr_pages)
815121d2 475{
b7e3debd 476 const unsigned long end_pfn = start_pfn + nr_pages;
815121d2 477 struct pglist_data *pgdat = zone->zone_pgdat;
b7e3debd 478 unsigned long pfn, cur_nr_pages, flags;
815121d2 479
d33695b1 480 /* Poison struct pages because they are now uninitialized again. */
b7e3debd
BW
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 }
d33695b1 490
7ce700bf
DH
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
feee6b29
DH
501 clear_zone_contiguous(zone);
502
815121d2
YI
503 pgdat_resize_lock(zone->zone_pgdat, &flags);
504 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
00d6c019 505 update_pgdat_span(pgdat);
815121d2 506 pgdat_resize_unlock(zone->zone_pgdat, &flags);
feee6b29
DH
507
508 set_zone_contiguous(zone);
815121d2
YI
509}
510
feee6b29
DH
511static void __remove_section(unsigned long pfn, unsigned long nr_pages,
512 unsigned long map_offset,
513 struct vmem_altmap *altmap)
ea01ea93 514{
10404901 515 struct mem_section *ms = __pfn_to_section(pfn);
ea01ea93 516
9d1d887d
DH
517 if (WARN_ON_ONCE(!valid_section(ms)))
518 return;
ea01ea93 519
ba72b4c8 520 sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
ea01ea93
BP
521}
522
ea01ea93 523/**
feee6b29 524 * __remove_pages() - remove sections of pages
7ea62160 525 * @pfn: starting pageframe (must be aligned to start of a section)
ea01ea93 526 * @nr_pages: number of pages to remove (must be multiple of section size)
e8b098fc 527 * @altmap: alternative device page map or %NULL if default memmap is used
ea01ea93
BP
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 */
feee6b29
DH
534void __remove_pages(unsigned long pfn, unsigned long nr_pages,
535 struct vmem_altmap *altmap)
ea01ea93 536{
52fb87c8
DH
537 const unsigned long end_pfn = pfn + nr_pages;
538 unsigned long cur_nr_pages;
4b94ffdc 539 unsigned long map_offset = 0;
4b94ffdc 540
96da4350 541 map_offset = vmem_altmap_offset(altmap);
ea01ea93 542
7ea62160
DW
543 if (check_pfn_span(pfn, nr_pages, "remove"))
544 return;
ea01ea93 545
52fb87c8 546 for (; pfn < end_pfn; pfn += cur_nr_pages) {
dd33ad7b 547 cond_resched();
52fb87c8 548 /* Select all remaining pages up to the next section boundary */
a11b9419
DH
549 cur_nr_pages = min(end_pfn - pfn,
550 SECTION_ALIGN_UP(pfn + 1) - pfn);
52fb87c8 551 __remove_section(pfn, cur_nr_pages, map_offset, altmap);
4b94ffdc 552 map_offset = 0;
ea01ea93 553 }
ea01ea93 554}
ea01ea93 555
9d0ad8ca
DK
556int set_online_page_callback(online_page_callback_t callback)
557{
558 int rc = -EINVAL;
559
bfc8c901
VD
560 get_online_mems();
561 mutex_lock(&online_page_callback_lock);
9d0ad8ca
DK
562
563 if (online_page_callback == generic_online_page) {
564 online_page_callback = callback;
565 rc = 0;
566 }
567
bfc8c901
VD
568 mutex_unlock(&online_page_callback_lock);
569 put_online_mems();
9d0ad8ca
DK
570
571 return rc;
572}
573EXPORT_SYMBOL_GPL(set_online_page_callback);
574
575int restore_online_page_callback(online_page_callback_t callback)
576{
577 int rc = -EINVAL;
578
bfc8c901
VD
579 get_online_mems();
580 mutex_lock(&online_page_callback_lock);
9d0ad8ca
DK
581
582 if (online_page_callback == callback) {
583 online_page_callback = generic_online_page;
584 rc = 0;
585 }
586
bfc8c901
VD
587 mutex_unlock(&online_page_callback_lock);
588 put_online_mems();
9d0ad8ca
DK
589
590 return rc;
591}
592EXPORT_SYMBOL_GPL(restore_online_page_callback);
593
18db1491 594void generic_online_page(struct page *page, unsigned int order)
9d0ad8ca 595{
c87cbc1f
VB
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 */
77bc7fd6 601 debug_pagealloc_map_pages(page, 1 << order);
a9cd410a
AK
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}
18db1491 609EXPORT_SYMBOL_GPL(generic_online_page);
a9cd410a 610
aac65321 611static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages)
3947be19 612{
b2c2ab20
DH
613 const unsigned long end_pfn = start_pfn + nr_pages;
614 unsigned long pfn;
b2c2ab20
DH
615
616 /*
aac65321
DH
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").
b2c2ab20 621 */
aac65321
DH
622 for (pfn = start_pfn; pfn < end_pfn; pfn += MAX_ORDER_NR_PAGES)
623 (*online_page_callback)(pfn_to_page(pfn), MAX_ORDER - 1);
2d070eab 624
b2c2ab20
DH
625 /* mark all involved sections as online */
626 online_mem_sections(start_pfn, end_pfn);
75884fb1
KH
627}
628
d9713679
LJ
629/* check which state of node_states will be changed when online memory */
630static 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);
d9713679 634
98fa15f3
AK
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;
d9713679 638
8efe33f4
OS
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))
d9713679 642 arg->status_change_nid_normal = nid;
6715ddf9 643#ifdef CONFIG_HIGHMEM
d3ba3ae1 644 if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
6715ddf9 645 arg->status_change_nid_high = nid;
6715ddf9 646#endif
d9713679
LJ
647}
648
649static 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
6715ddf9
LJ
654 if (arg->status_change_nid_high >= 0)
655 node_set_state(node, N_HIGH_MEMORY);
656
83d83612
OS
657 if (arg->status_change_nid >= 0)
658 node_set_state(node, N_MEMORY);
d9713679
LJ
659}
660
f1dd2cd1
MH
661static 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
672static 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;
f1dd2cd1 681
3fccb74c
DH
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.
d882c006
DH
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.
3fccb74c 691 */
a99583e7 692void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
d882c006
DH
693 unsigned long nr_pages,
694 struct vmem_altmap *altmap, int migratetype)
f1dd2cd1
MH
695{
696 struct pglist_data *pgdat = zone->zone_pgdat;
697 int nid = pgdat->node_id;
698 unsigned long flags;
df429ac0 699
f1dd2cd1
MH
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);
fa004ab7
WY
705 if (zone_is_empty(zone))
706 init_currently_empty_zone(zone, start_pfn, nr_pages);
f1dd2cd1
MH
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 */
dc2da7b4 718 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, 0,
d882c006 719 MEMINIT_HOTPLUG, altmap, migratetype);
f1dd2cd1
MH
720
721 set_zone_contiguous(zone);
722}
723
accfb3e0
SF
724void 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
c246a213
MH
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
accfb3e0 738 * to the MAX_KERNEL_ZONE.
c246a213 739 */
c6f03e29 740static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
c246a213
MH
741 unsigned long nr_pages)
742{
743 struct pglist_data *pgdat = NODE_DATA(nid);
744 int zid;
745
accfb3e0 746 for (zid = 0; zid <= MAX_KERNEL_ZONE; zid++) {
c246a213
MH
747 struct zone *zone = &pgdat->node_zones[zid];
748
749 if (zone_intersects(zone, start_pfn, nr_pages))
750 return zone;
751 }
752
accfb3e0 753 return &pgdat->node_zones[default_kernel_zone];
c246a213
MH
754}
755
c6f03e29
MH
756static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
757 unsigned long nr_pages)
e5e68930 758{
c6f03e29
MH
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);
e5e68930
MH
764
765 /*
c6f03e29
MH
766 * We inherit the existing zone in a simple case where zones do not
767 * overlap in the given range
e5e68930 768 */
c6f03e29
MH
769 if (in_kernel ^ in_movable)
770 return (in_kernel) ? kernel_zone : movable_zone;
9f123ab5 771
c6f03e29
MH
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;
9f123ab5
MH
778}
779
e5e68930
MH
780struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
781 unsigned long nr_pages)
f1dd2cd1 782{
c6f03e29
MH
783 if (online_type == MMOP_ONLINE_KERNEL)
784 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
f1dd2cd1 785
c6f03e29
MH
786 if (online_type == MMOP_ONLINE_MOVABLE)
787 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
df429ac0 788
c6f03e29 789 return default_zone_for_pfn(nid, start_pfn, nr_pages);
e5e68930
MH
790}
791
bd5c2344
DH
792int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
793 int online_type, int nid)
75884fb1 794{
aa47228a 795 unsigned long flags;
3947be19 796 struct zone *zone;
6811378e 797 int need_zonelists_rebuild = 0;
7b78d335
YG
798 int ret;
799 struct memory_notify arg;
d0dc12e8 800
4986fac1
DH
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
381eab4a
DH
806 mem_hotplug_begin();
807
f1dd2cd1 808 /* associate pfn range with the zone */
3fccb74c 809 zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages);
b30c5927 810 move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE);
f1dd2cd1 811
7b78d335
YG
812 arg.start_pfn = pfn;
813 arg.nr_pages = nr_pages;
d9713679 814 node_states_check_changes_online(nr_pages, zone, &arg);
7b78d335 815
7b78d335
YG
816 ret = memory_notify(MEM_GOING_ONLINE, &arg);
817 ret = notifier_to_errno(ret);
e33e33b4
CY
818 if (ret)
819 goto failed_addition;
820
b30c5927
DH
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
6811378e
YG
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 */
6dcd73d7 834 if (!populated_zone(zone)) {
6811378e 835 need_zonelists_rebuild = 1;
72675e13 836 setup_zone_pageset(zone);
6dcd73d7 837 }
6811378e 838
aac65321
DH
839 online_pages_range(pfn, nr_pages);
840 zone->present_pages += nr_pages;
aa47228a
CS
841
842 pgdat_resize_lock(zone->zone_pgdat, &flags);
aac65321 843 zone->zone_pgdat->node_present_pages += nr_pages;
aa47228a
CS
844 pgdat_resize_unlock(zone->zone_pgdat, &flags);
845
b30c5927
DH
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
93146d98 854 /*
b86c5fc4
DH
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.
93146d98 859 */
e900a918
DW
860 shuffle_zone(zone);
861
1b79acc9
KM
862 init_per_zone_wmark_min();
863
ca9a46f8
DH
864 kswapd_run(nid);
865 kcompactd_run(nid);
61b13993 866
2d1d43f6 867 writeback_set_ratelimit();
7b78d335 868
ca9a46f8 869 memory_notify(MEM_ONLINE, &arg);
381eab4a 870 mem_hotplug_done();
30467e0b 871 return 0;
e33e33b4
CY
872
873failed_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);
feee6b29 878 remove_pfn_range_from_zone(zone, pfn, nr_pages);
381eab4a 879 mem_hotplug_done();
e33e33b4 880 return ret;
3947be19 881}
53947027 882#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
bc02af93 883
0bd85420
TC
884static 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
e1319331 894/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
c68ab18c 895static pg_data_t __ref *hotadd_new_pgdat(int nid)
9af3c2de
YG
896{
897 struct pglist_data *pgdat;
9af3c2de 898
a1e565aa
TC
899 pgdat = NODE_DATA(nid);
900 if (!pgdat) {
901 pgdat = arch_alloc_nodedata(nid);
902 if (!pgdat)
903 return NULL;
9af3c2de 904
33fce011
WY
905 pgdat->per_cpu_nodestats =
906 alloc_percpu(struct per_cpu_nodestat);
a1e565aa 907 arch_refresh_nodedata(nid, pgdat);
b0dc3a34 908 } else {
33fce011 909 int cpu;
e716f2eb 910 /*
97a225e6
JK
911 * Reset the nr_zones, order and highest_zoneidx before reuse.
912 * Note that kswapd will init kswapd_highest_zoneidx properly
e716f2eb
MG
913 * when it starts in the near future.
914 */
b0dc3a34 915 pgdat->nr_zones = 0;
38087d9b 916 pgdat->kswapd_order = 0;
97a225e6 917 pgdat->kswapd_highest_zoneidx = 0;
33fce011
WY
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 }
a1e565aa 924 }
9af3c2de
YG
925
926 /* we can use NODE_DATA(nid) from here */
03e85f9d 927 pgdat->node_id = nid;
c68ab18c 928 pgdat->node_start_pfn = 0;
03e85f9d 929
9af3c2de 930 /* init node's zones as empty zones, we don't have any present pages.*/
03e85f9d 931 free_area_init_core_hotplug(nid);
9af3c2de 932
959ecc48
KH
933 /*
934 * The node we allocated has no zone fallback lists. For avoiding
935 * to access not-initialized zonelist, build here.
936 */
72675e13 937 build_all_zonelists(pgdat);
959ecc48 938
0bd85420
TC
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 */
03e85f9d 944 reset_node_managed_pages(pgdat);
0bd85420
TC
945 reset_node_present_pages(pgdat);
946
9af3c2de
YG
947 return pgdat;
948}
949
b9ff0360 950static void rollback_node_hotadd(int nid)
9af3c2de 951{
b9ff0360
OS
952 pg_data_t *pgdat = NODE_DATA(nid);
953
9af3c2de 954 arch_refresh_nodedata(nid, NULL);
5830169f 955 free_percpu(pgdat->per_cpu_nodestats);
9af3c2de 956 arch_free_nodedata(pgdat);
9af3c2de
YG
957}
958
0a547039 959
01b0f197
TK
960/**
961 * try_online_node - online a node if offlined
e8b098fc 962 * @nid: the node ID
b9ff0360 963 * @set_node_online: Whether we want to online the node
cf23422b 964 * called by cpu_up() to online a node without onlined memory.
b9ff0360
OS
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
cf23422b 970 */
c68ab18c 971static int __try_online_node(int nid, bool set_node_online)
cf23422b 972{
b9ff0360
OS
973 pg_data_t *pgdat;
974 int ret = 1;
cf23422b 975
01b0f197
TK
976 if (node_online(nid))
977 return 0;
978
c68ab18c 979 pgdat = hotadd_new_pgdat(nid);
7553e8f2 980 if (!pgdat) {
01b0f197 981 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
cf23422b 982 ret = -ENOMEM;
983 goto out;
984 }
b9ff0360
OS
985
986 if (set_node_online) {
987 node_set_online(nid);
988 ret = register_one_node(nid);
989 BUG_ON(ret);
990 }
cf23422b 991out:
b9ff0360
OS
992 return ret;
993}
994
995/*
996 * Users of this function always want to online/register the node
997 */
998int try_online_node(int nid)
999{
1000 int ret;
1001
1002 mem_hotplug_begin();
c68ab18c 1003 ret = __try_online_node(nid, true);
bfc8c901 1004 mem_hotplug_done();
cf23422b 1005 return ret;
1006}
1007
27356f54
TK
1008static int check_hotplug_memory_range(u64 start, u64 size)
1009{
ba325585 1010 /* memory range must be block size aligned */
cec3ebd0
DH
1011 if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
1012 !IS_ALIGNED(size, memory_block_size_bytes())) {
ba325585 1013 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
cec3ebd0 1014 memory_block_size_bytes(), start, size);
27356f54
TK
1015 return -EINVAL;
1016 }
1017
1018 return 0;
1019}
1020
31bc3858
VK
1021static int online_memory_block(struct memory_block *mem, void *arg)
1022{
862919e5 1023 mem->online_type = memhp_default_online_type;
dc18d706 1024 return device_online(&mem->dev);
31bc3858
VK
1025}
1026
8df1d0e4
DH
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 */
b6117199 1033int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
bc02af93 1034{
302cba40 1035 struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
62cedb9f 1036 u64 start, size;
b9ff0360 1037 bool new_node = false;
bc02af93
YG
1038 int ret;
1039
62cedb9f
DV
1040 start = res->start;
1041 size = resource_size(res);
1042
27356f54
TK
1043 ret = check_hotplug_memory_range(start, size);
1044 if (ret)
1045 return ret;
1046
fa6d9ec7
VV
1047 if (!node_possible(nid)) {
1048 WARN(1, "node %d was absent from the node_possible_map\n", nid);
1049 return -EINVAL;
1050 }
1051
bfc8c901 1052 mem_hotplug_begin();
ac13c462 1053
52219aea
DH
1054 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
1055 memblock_add_node(start, size, nid);
7f36e3e5 1056
c68ab18c 1057 ret = __try_online_node(nid, false);
b9ff0360
OS
1058 if (ret < 0)
1059 goto error;
1060 new_node = ret;
9af3c2de 1061
bc02af93 1062 /* call arch's memory hotadd */
f5637d3b 1063 ret = arch_add_memory(nid, start, size, &params);
9af3c2de
YG
1064 if (ret < 0)
1065 goto error;
1066
db051a0d
DH
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
a1e565aa 1074 if (new_node) {
d5b6f6a3 1075 /* If sysfs file of new node can't be created, cpu on the node
0fc44159
YG
1076 * can't be hot-added. There is no rollback way now.
1077 * So, check by BUG_ON() to catch it reluctantly..
d5b6f6a3 1078 * We online node here. We can't roll back from here.
0fc44159 1079 */
d5b6f6a3
OS
1080 node_set_online(nid);
1081 ret = __register_one_node(nid);
0fc44159
YG
1082 BUG_ON(ret);
1083 }
1084
d5b6f6a3 1085 /* link memory sections under this node.*/
90c7eaeb
LD
1086 link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1),
1087 MEMINIT_HOTPLUG);
d5b6f6a3 1088
d96ae530 1089 /* create new memmap entry */
7b7b2721
DH
1090 if (!strcmp(res->name, "System RAM"))
1091 firmware_map_add_hotplug(start, start + size, "System RAM");
d96ae530 1092
381eab4a
DH
1093 /* device_online() will take the lock when calling online_pages() */
1094 mem_hotplug_done();
1095
9ca6551e
DH
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
31bc3858 1103 /* online pages if requested */
862919e5 1104 if (memhp_default_online_type != MMOP_OFFLINE)
fbcf73ce 1105 walk_memory_blocks(start, size, NULL, online_memory_block);
31bc3858 1106
381eab4a 1107 return ret;
9af3c2de
YG
1108error:
1109 /* rollback pgdat allocation and others */
b9ff0360
OS
1110 if (new_node)
1111 rollback_node_hotadd(nid);
52219aea
DH
1112 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
1113 memblock_remove(start, size);
bfc8c901 1114 mem_hotplug_done();
bc02af93
YG
1115 return ret;
1116}
62cedb9f 1117
8df1d0e4 1118/* requires device_hotplug_lock, see add_memory_resource() */
b6117199 1119int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
62cedb9f
DV
1120{
1121 struct resource *res;
1122 int ret;
1123
7b7b2721 1124 res = register_memory_resource(start, size, "System RAM");
6f754ba4
VK
1125 if (IS_ERR(res))
1126 return PTR_ERR(res);
62cedb9f 1127
b6117199 1128 ret = add_memory_resource(nid, res, mhp_flags);
62cedb9f
DV
1129 if (ret < 0)
1130 release_memory_resource(res);
1131 return ret;
1132}
8df1d0e4 1133
b6117199 1134int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
8df1d0e4
DH
1135{
1136 int rc;
1137
1138 lock_device_hotplug();
b6117199 1139 rc = __add_memory(nid, start, size, mhp_flags);
8df1d0e4
DH
1140 unlock_device_hotplug();
1141
1142 return rc;
1143}
bc02af93 1144EXPORT_SYMBOL_GPL(add_memory);
0c0e6195 1145
7b7b2721
DH
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
7cf603d1 1161 * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case
7b7b2721
DH
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 */
1167int add_memory_driver_managed(int nid, u64 start, u64 size,
b6117199 1168 const char *resource_name, mhp_t mhp_flags)
7b7b2721
DH
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
b6117199 1186 rc = add_memory_resource(nid, res, mhp_flags);
7b7b2721
DH
1187 if (rc < 0)
1188 release_memory_resource(res);
1189
1190out_unlock:
1191 unlock_device_hotplug();
1192 return rc;
1193}
1194EXPORT_SYMBOL_GPL(add_memory_driver_managed);
1195
0c0e6195
KH
1196#ifdef CONFIG_MEMORY_HOTREMOVE
1197/*
92917998
DH
1198 * Confirm all pages in a range [start, end) belong to the same zone (skipping
1199 * memory holes). When true, return the zone.
0c0e6195 1200 */
92917998
DH
1201struct zone *test_pages_in_a_zone(unsigned long start_pfn,
1202 unsigned long end_pfn)
0c0e6195 1203{
5f0f2887 1204 unsigned long pfn, sec_end_pfn;
0c0e6195
KH
1205 struct zone *zone = NULL;
1206 struct page *page;
1207 int i;
deb88a2a 1208 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
0c0e6195 1209 pfn < end_pfn;
deb88a2a 1210 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
5f0f2887
AB
1211 /* Make sure the memory section is present first */
1212 if (!present_section_nr(pfn_to_section_nr(pfn)))
0c0e6195 1213 continue;
5f0f2887
AB
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++;
d6d8c8a4 1221 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
5f0f2887 1222 continue;
24feb47c
MZ
1223 /* Check if we got outside of the zone */
1224 if (zone && !zone_spans_pfn(zone, pfn + i))
92917998 1225 return NULL;
5f0f2887
AB
1226 page = pfn_to_page(pfn + i);
1227 if (zone && page_zone(page) != zone)
92917998 1228 return NULL;
5f0f2887
AB
1229 zone = page_zone(page);
1230 }
0c0e6195 1231 }
deb88a2a 1232
92917998 1233 return zone;
0c0e6195
KH
1234}
1235
1236/*
0efadf48 1237 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
aa218795
DH
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.
0c0e6195 1246 */
aa218795
DH
1247static int scan_movable_pages(unsigned long start, unsigned long end,
1248 unsigned long *movable_pfn)
0c0e6195
KH
1249{
1250 unsigned long pfn;
eeb0efd0 1251
0c0e6195 1252 for (pfn = start; pfn < end; pfn++) {
eeb0efd0
OS
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))
aa218795 1260 goto found;
eeb0efd0 1261 if (__PageMovable(page))
aa218795
DH
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;
eeb0efd0
OS
1272
1273 if (!PageHuge(page))
1274 continue;
1275 head = compound_head(page);
39186cbe 1276 if (page_huge_active(head))
aa218795 1277 goto found;
d8c6546b 1278 skip = compound_nr(head) - (page - head);
eeb0efd0 1279 pfn += skip - 1;
0c0e6195 1280 }
aa218795
DH
1281 return -ENOENT;
1282found:
1283 *movable_pfn = pfn;
0c0e6195
KH
1284 return 0;
1285}
1286
0c0e6195
KH
1287static int
1288do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1289{
1290 unsigned long pfn;
6c357848 1291 struct page *page, *head;
0c0e6195
KH
1292 int ret = 0;
1293 LIST_HEAD(source);
1294
a85009c3 1295 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
0c0e6195
KH
1296 if (!pfn_valid(pfn))
1297 continue;
1298 page = pfn_to_page(pfn);
6c357848 1299 head = compound_head(page);
c8721bbb
NH
1300
1301 if (PageHuge(page)) {
d8c6546b 1302 pfn = page_to_pfn(head) + compound_nr(head) - 1;
daf3538a 1303 isolate_huge_page(head, &source);
c8721bbb 1304 continue;
94723aaf 1305 } else if (PageTransHuge(page))
6c357848 1306 pfn = page_to_pfn(head) + thp_nr_pages(page) - 1;
c8721bbb 1307
b15c8726
MH
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))
013339df 1319 try_to_unmap(page, TTU_IGNORE_MLOCK);
b15c8726
MH
1320 continue;
1321 }
1322
700c2a46 1323 if (!get_page_unless_zero(page))
0c0e6195
KH
1324 continue;
1325 /*
0efadf48
YX
1326 * We can skip free pages. And we can deal with pages on
1327 * LRU and non-lru movable pages.
0c0e6195 1328 */
0efadf48
YX
1329 if (PageLRU(page))
1330 ret = isolate_lru_page(page);
1331 else
1332 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
0c0e6195 1333 if (!ret) { /* Success */
62695a84 1334 list_add_tail(&page->lru, &source);
0efadf48
YX
1335 if (!__PageMovable(page))
1336 inc_node_page_state(page, NR_ISOLATED_ANON +
9de4f22a 1337 page_is_file_lru(page));
6d9c285a 1338
0c0e6195 1339 } else {
2932c8b0 1340 pr_warn("failed to isolate pfn %lx\n", pfn);
0efadf48 1341 dump_page(page, "isolation failed");
0c0e6195 1342 }
1723058e 1343 put_page(page);
0c0e6195 1344 }
f3ab2636 1345 if (!list_empty(&source)) {
203e6e5c
JK
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);
2932c8b0
MH
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 }
c8721bbb 1374 putback_movable_pages(&source);
2932c8b0 1375 }
0c0e6195 1376 }
1723058e 1377
0c0e6195
KH
1378 return ret;
1379}
1380
c5320926
TC
1381static int __init cmdline_parse_movable_node(char *p)
1382{
55ac590c 1383 movable_node_enabled = true;
c5320926
TC
1384 return 0;
1385}
1386early_param("movable_node", cmdline_parse_movable_node);
1387
d9713679
LJ
1388/* check which state of node_states will be changed when offline memory */
1389static 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;
86b27bea 1394 enum zone_type zt;
d9713679 1395
98fa15f3
AK
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;
d9713679
LJ
1399
1400 /*
86b27bea
OS
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].
d9713679 1407 */
86b27bea 1408 for (zt = 0; zt <= ZONE_NORMAL; zt++)
d9713679 1409 present_pages += pgdat->node_zones[zt].present_pages;
86b27bea 1410 if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
d9713679 1411 arg->status_change_nid_normal = zone_to_nid(zone);
d9713679 1412
6715ddf9
LJ
1413#ifdef CONFIG_HIGHMEM
1414 /*
86b27bea
OS
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.
6715ddf9 1421 */
86b27bea
OS
1422 present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1423 if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
6715ddf9 1424 arg->status_change_nid_high = zone_to_nid(zone);
6715ddf9
LJ
1425#endif
1426
d9713679 1427 /*
86b27bea
OS
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.
d9713679 1436 */
86b27bea 1437 present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
d9713679 1438
d9713679
LJ
1439 if (nr_pages >= present_pages)
1440 arg->status_change_nid = zone_to_nid(zone);
d9713679
LJ
1441}
1442
1443static 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
cf01f6f5 1448 if (arg->status_change_nid_high >= 0)
d9713679 1449 node_clear_state(node, N_HIGH_MEMORY);
6715ddf9 1450
cf01f6f5 1451 if (arg->status_change_nid >= 0)
6715ddf9 1452 node_clear_state(node, N_MEMORY);
d9713679
LJ
1453}
1454
c5e79ef5
DH
1455static 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
73a11c96 1464int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages)
0c0e6195 1465{
73a11c96 1466 const unsigned long end_pfn = start_pfn + nr_pages;
0a1a9a00 1467 unsigned long pfn, system_ram_pages = 0;
d702909f 1468 unsigned long flags;
0c0e6195 1469 struct zone *zone;
7b78d335 1470 struct memory_notify arg;
ea15153c 1471 int ret, node;
79605093 1472 char *reason;
0c0e6195 1473
4986fac1
DH
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
381eab4a
DH
1479 mem_hotplug_begin();
1480
c5e79ef5
DH
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 */
73a11c96 1489 walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages,
c5e79ef5 1490 count_system_ram_pages_cb);
73a11c96 1491 if (system_ram_pages != nr_pages) {
c5e79ef5
DH
1492 ret = -EINVAL;
1493 reason = "memory holes";
1494 goto failed_removal;
1495 }
1496
0c0e6195
KH
1497 /* This makes hotplug much easier...and readable.
1498 we assume this for now. .*/
92917998
DH
1499 zone = test_pages_in_a_zone(start_pfn, end_pfn);
1500 if (!zone) {
79605093
MH
1501 ret = -EINVAL;
1502 reason = "multizone range";
1503 goto failed_removal;
381eab4a 1504 }
7b78d335 1505 node = zone_to_nid(zone);
7b78d335 1506
ec6e8c7e
VB
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
0c0e6195 1513 /* set above range as isolated */
b023f468 1514 ret = start_isolate_page_range(start_pfn, end_pfn,
d381c547 1515 MIGRATE_MOVABLE,
756d25be 1516 MEMORY_OFFLINE | REPORT_FAILURE);
3fa0c7c7 1517 if (ret) {
79605093 1518 reason = "failure to isolate range";
ec6e8c7e 1519 goto failed_removal_pcplists_disabled;
381eab4a 1520 }
7b78d335
YG
1521
1522 arg.start_pfn = start_pfn;
1523 arg.nr_pages = nr_pages;
d9713679 1524 node_states_check_changes_offline(nr_pages, zone, &arg);
7b78d335
YG
1525
1526 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1527 ret = notifier_to_errno(ret);
79605093
MH
1528 if (ret) {
1529 reason = "notifier failure";
1530 goto failed_removal_isolated;
1531 }
7b78d335 1532
bb8965bd 1533 do {
aa218795
DH
1534 pfn = start_pfn;
1535 do {
bb8965bd
MH
1536 if (signal_pending(current)) {
1537 ret = -EINTR;
1538 reason = "signal backoff";
1539 goto failed_removal_isolated;
1540 }
72b39cfc 1541
bb8965bd
MH
1542 cond_resched();
1543 lru_add_drain_all();
bb8965bd 1544
aa218795
DH
1545 ret = scan_movable_pages(pfn, end_pfn, &pfn);
1546 if (!ret) {
bb8965bd
MH
1547 /*
1548 * TODO: fatal migration failures should bail
1549 * out
1550 */
1551 do_migrate_range(pfn, end_pfn);
1552 }
aa218795
DH
1553 } while (!ret);
1554
1555 if (ret != -ENOENT) {
1556 reason = "unmovable page";
1557 goto failed_removal_isolated;
bb8965bd 1558 }
0c0e6195 1559
bb8965bd
MH
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 }
0a1a9a00 1570
0a1a9a00 1571 ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE);
ec6e8c7e 1572
5557c766 1573 } while (ret);
72b39cfc 1574
0a1a9a00
DH
1575 /* Mark all sections offline and remove free pages from the buddy. */
1576 __offline_isolated_pages(start_pfn, end_pfn);
7c33023a 1577 pr_debug("Offlined Pages %ld\n", nr_pages);
0a1a9a00 1578
9b7ea46a 1579 /*
b30c5927
DH
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.
9b7ea46a
QC
1583 */
1584 spin_lock_irqsave(&zone->lock, flags);
ea15153c 1585 zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;
9b7ea46a
QC
1586 spin_unlock_irqrestore(&zone->lock, flags);
1587
ec6e8c7e
VB
1588 zone_pcp_enable(zone);
1589
0c0e6195 1590 /* removal success */
0a1a9a00
DH
1591 adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages);
1592 zone->present_pages -= nr_pages;
d702909f
CS
1593
1594 pgdat_resize_lock(zone->zone_pgdat, &flags);
0a1a9a00 1595 zone->zone_pgdat->node_present_pages -= nr_pages;
d702909f 1596 pgdat_resize_unlock(zone->zone_pgdat, &flags);
7b78d335 1597
1b79acc9
KM
1598 init_per_zone_wmark_min();
1599
1e8537ba 1600 if (!populated_zone(zone)) {
340175b7 1601 zone_pcp_reset(zone);
72675e13 1602 build_all_zonelists(NULL);
1e8537ba
XQ
1603 } else
1604 zone_pcp_update(zone);
340175b7 1605
d9713679 1606 node_states_clear_node(node, &arg);
698b1b30 1607 if (arg.status_change_nid >= 0) {
8fe23e05 1608 kswapd_stop(node);
698b1b30
VB
1609 kcompactd_stop(node);
1610 }
bce7394a 1611
0c0e6195 1612 writeback_set_ratelimit();
7b78d335
YG
1613
1614 memory_notify(MEM_OFFLINE, &arg);
feee6b29 1615 remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
381eab4a 1616 mem_hotplug_done();
0c0e6195
KH
1617 return 0;
1618
79605093
MH
1619failed_removal_isolated:
1620 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
c4efe484 1621 memory_notify(MEM_CANCEL_OFFLINE, &arg);
ec6e8c7e
VB
1622failed_removal_pcplists_disabled:
1623 zone_pcp_enable(zone);
0c0e6195 1624failed_removal:
79605093 1625 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
e33e33b4 1626 (unsigned long long) start_pfn << PAGE_SHIFT,
79605093
MH
1627 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
1628 reason);
0c0e6195 1629 /* pushback to free area */
381eab4a 1630 mem_hotplug_done();
0c0e6195
KH
1631 return ret;
1632}
71088785 1633
d6de9d53 1634static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
bbc76be6
WC
1635{
1636 int ret = !is_memblock_offlined(mem);
1637
349daa0f
RD
1638 if (unlikely(ret)) {
1639 phys_addr_t beginpa, endpa;
1640
1641 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
b6c88d3b 1642 endpa = beginpa + memory_block_size_bytes() - 1;
756a025f 1643 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
349daa0f 1644 &beginpa, &endpa);
bbc76be6 1645
eca499ab
PT
1646 return -EBUSY;
1647 }
1648 return 0;
bbc76be6
WC
1649}
1650
0f1cfe9d 1651static int check_cpu_on_node(pg_data_t *pgdat)
60a5a19e 1652{
60a5a19e
TC
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
2c91f8fc
DH
1667static 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
0f1cfe9d
TK
1679/**
1680 * try_offline_node
e8b098fc 1681 * @nid: the node ID
0f1cfe9d
TK
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 */
90b30cdc 1688void try_offline_node(int nid)
60a5a19e 1689{
d822b86a 1690 pg_data_t *pgdat = NODE_DATA(nid);
2c91f8fc 1691 int rc;
60a5a19e 1692
2c91f8fc
DH
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;
60a5a19e 1700
2c91f8fc
DH
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)
60a5a19e 1708 return;
60a5a19e 1709
46a3679b 1710 if (check_cpu_on_node(pgdat))
60a5a19e
TC
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}
90b30cdc 1720EXPORT_SYMBOL(try_offline_node);
60a5a19e 1721
eca499ab 1722static int __ref try_remove_memory(int nid, u64 start, u64 size)
bbc76be6 1723{
eca499ab 1724 int rc = 0;
993c1aad 1725
27356f54
TK
1726 BUG_ON(check_hotplug_memory_range(start, size));
1727
6677e3ea 1728 /*
242831eb 1729 * All memory blocks must be offlined before removing memory. Check
eca499ab 1730 * whether all memory blocks in question are offline and return error
242831eb 1731 * if this is not the case.
6677e3ea 1732 */
fbcf73ce 1733 rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb);
eca499ab 1734 if (rc)
b4223a51 1735 return rc;
6677e3ea 1736
46c66c4b
YI
1737 /* remove memmap entry */
1738 firmware_map_remove(start, start + size, "System RAM");
4c4b7f9b 1739
f1037ec0
DW
1740 /*
1741 * Memory block device removal under the device_hotplug_lock is
1742 * a barrier against racing online attempts.
1743 */
4c4b7f9b 1744 remove_memory_block_devices(start, size);
46c66c4b 1745
f1037ec0
DW
1746 mem_hotplug_begin();
1747
2c2a5af6 1748 arch_remove_memory(nid, start, size, NULL);
52219aea
DH
1749
1750 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) {
1751 memblock_free(start, size);
1752 memblock_remove(start, size);
1753 }
1754
cb8e3c8b 1755 release_mem_region_adjustable(start, size);
24d335ca 1756
60a5a19e
TC
1757 try_offline_node(nid);
1758
bfc8c901 1759 mem_hotplug_done();
b4223a51 1760 return 0;
71088785 1761}
d15e5926 1762
eca499ab
PT
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 */
1773void __remove_memory(int nid, u64 start, u64 size)
1774{
1775
1776 /*
29a90db9 1777 * trigger BUG() if some memory is not offlined prior to calling this
eca499ab
PT
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 */
1788int remove_memory(int nid, u64 start, u64 size)
d15e5926 1789{
eca499ab
PT
1790 int rc;
1791
d15e5926 1792 lock_device_hotplug();
eca499ab 1793 rc = try_remove_memory(nid, start, size);
d15e5926 1794 unlock_device_hotplug();
eca499ab
PT
1795
1796 return rc;
d15e5926 1797}
71088785 1798EXPORT_SYMBOL_GPL(remove_memory);
08b3acd7 1799
8dc4bb58
DH
1800static 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
1829static 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
08b3acd7 1847/*
8dc4bb58
DH
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.
08b3acd7
DH
1852 */
1853int offline_and_remove_memory(int nid, u64 start, u64 size)
1854{
8dc4bb58
DH
1855 const unsigned long mb_count = size / memory_block_size_bytes();
1856 uint8_t *online_types, *tmp;
1857 int rc;
08b3acd7
DH
1858
1859 if (!IS_ALIGNED(start, memory_block_size_bytes()) ||
8dc4bb58
DH
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);
08b3acd7
DH
1878
1879 lock_device_hotplug();
8dc4bb58
DH
1880
1881 tmp = online_types;
1882 rc = walk_memory_blocks(start, size, &tmp, try_offline_memory_block);
08b3acd7
DH
1883
1884 /*
8dc4bb58 1885 * In case we succeeded to offline all memory, remove it.
08b3acd7
DH
1886 * This cannot fail as it cannot get onlined in the meantime.
1887 */
1888 if (!rc) {
1889 rc = try_remove_memory(nid, start, size);
8dc4bb58
DH
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);
08b3acd7
DH
1902 }
1903 unlock_device_hotplug();
1904
8dc4bb58 1905 kfree(online_types);
08b3acd7
DH
1906 return rc;
1907}
1908EXPORT_SYMBOL_GPL(offline_and_remove_memory);
aba6efc4 1909#endif /* CONFIG_MEMORY_HOTREMOVE */