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