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