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