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