OSDN Git Service

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