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