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