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