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