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