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