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