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