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