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