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