220459c9ff1ce44db6b7124752f30ddfd8b798a0
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / memory_hotplug.c
1 /*
2  *  linux/mm/memory_hotplug.c
3  *
4  *  Copyright (C)
5  */
6
7 #include <linux/stddef.h>
8 #include <linux/mm.h>
9 #include <linux/swap.h>
10 #include <linux/interrupt.h>
11 #include <linux/pagemap.h>
12 #include <linux/bootmem.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
15 #include <linux/pagevec.h>
16 #include <linux/writeback.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/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
33 #include <asm/tlbflush.h>
34
35 #include "internal.h"
36
37 /*
38  * online_page_callback contains pointer to current page onlining function.
39  * Initially it is generic_online_page(). If it is required it could be
40  * changed by calling set_online_page_callback() for callback registration
41  * and restore_online_page_callback() for generic callback restore.
42  */
43
44 static void generic_online_page(struct page *page);
45
46 static online_page_callback_t online_page_callback = generic_online_page;
47
48 DEFINE_MUTEX(mem_hotplug_mutex);
49
50 void lock_memory_hotplug(void)
51 {
52         mutex_lock(&mem_hotplug_mutex);
53
54         /* for exclusive hibernation if CONFIG_HIBERNATION=y */
55         lock_system_sleep();
56 }
57
58 void unlock_memory_hotplug(void)
59 {
60         unlock_system_sleep();
61         mutex_unlock(&mem_hotplug_mutex);
62 }
63
64
65 /* add this memory to iomem resource */
66 static struct resource *register_memory_resource(u64 start, u64 size)
67 {
68         struct resource *res;
69         res = kzalloc(sizeof(struct resource), GFP_KERNEL);
70         BUG_ON(!res);
71
72         res->name = "System RAM";
73         res->start = start;
74         res->end = start + size - 1;
75         res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
76         if (request_resource(&iomem_resource, res) < 0) {
77                 printk("System RAM resource %pR cannot be added\n", res);
78                 kfree(res);
79                 res = NULL;
80         }
81         return res;
82 }
83
84 static void release_memory_resource(struct resource *res)
85 {
86         if (!res)
87                 return;
88         release_resource(res);
89         kfree(res);
90         return;
91 }
92
93 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
94 void get_page_bootmem(unsigned long info,  struct page *page,
95                       unsigned long type)
96 {
97         page->lru.next = (struct list_head *) type;
98         SetPagePrivate(page);
99         set_page_private(page, info);
100         atomic_inc(&page->_count);
101 }
102
103 /* reference to __meminit __free_pages_bootmem is valid
104  * so use __ref to tell modpost not to generate a warning */
105 void __ref put_page_bootmem(struct page *page)
106 {
107         unsigned long type;
108         static DEFINE_MUTEX(ppb_lock);
109
110         type = (unsigned long) page->lru.next;
111         BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112                type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
113
114         if (atomic_dec_return(&page->_count) == 1) {
115                 ClearPagePrivate(page);
116                 set_page_private(page, 0);
117                 INIT_LIST_HEAD(&page->lru);
118
119                 /*
120                  * Please refer to comment for __free_pages_bootmem()
121                  * for why we serialize here.
122                  */
123                 mutex_lock(&ppb_lock);
124                 __free_pages_bootmem(page, 0);
125                 mutex_unlock(&ppb_lock);
126         }
127
128 }
129
130 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
131 #ifndef CONFIG_SPARSEMEM_VMEMMAP
132 static void register_page_bootmem_info_section(unsigned long start_pfn)
133 {
134         unsigned long *usemap, mapsize, section_nr, i;
135         struct mem_section *ms;
136         struct page *page, *memmap;
137
138         section_nr = pfn_to_section_nr(start_pfn);
139         ms = __nr_to_section(section_nr);
140
141         /* Get section's memmap address */
142         memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
143
144         /*
145          * Get page for the memmap's phys address
146          * XXX: need more consideration for sparse_vmemmap...
147          */
148         page = virt_to_page(memmap);
149         mapsize = sizeof(struct page) * PAGES_PER_SECTION;
150         mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
151
152         /* remember memmap's page */
153         for (i = 0; i < mapsize; i++, page++)
154                 get_page_bootmem(section_nr, page, SECTION_INFO);
155
156         usemap = __nr_to_section(section_nr)->pageblock_flags;
157         page = virt_to_page(usemap);
158
159         mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
160
161         for (i = 0; i < mapsize; i++, page++)
162                 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
163
164 }
165 #else /* CONFIG_SPARSEMEM_VMEMMAP */
166 static void register_page_bootmem_info_section(unsigned long start_pfn)
167 {
168         unsigned long *usemap, mapsize, section_nr, i;
169         struct mem_section *ms;
170         struct page *page, *memmap;
171
172         if (!pfn_valid(start_pfn))
173                 return;
174
175         section_nr = pfn_to_section_nr(start_pfn);
176         ms = __nr_to_section(section_nr);
177
178         memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
179
180         register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
181
182         usemap = __nr_to_section(section_nr)->pageblock_flags;
183         page = virt_to_page(usemap);
184
185         mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
186
187         for (i = 0; i < mapsize; i++, page++)
188                 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
189 }
190 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
191
192 void register_page_bootmem_info_node(struct pglist_data *pgdat)
193 {
194         unsigned long i, pfn, end_pfn, nr_pages;
195         int node = pgdat->node_id;
196         struct page *page;
197         struct zone *zone;
198
199         nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
200         page = virt_to_page(pgdat);
201
202         for (i = 0; i < nr_pages; i++, page++)
203                 get_page_bootmem(node, page, NODE_INFO);
204
205         zone = &pgdat->node_zones[0];
206         for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
207                 if (zone->wait_table) {
208                         nr_pages = zone->wait_table_hash_nr_entries
209                                 * sizeof(wait_queue_head_t);
210                         nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
211                         page = virt_to_page(zone->wait_table);
212
213                         for (i = 0; i < nr_pages; i++, page++)
214                                 get_page_bootmem(node, page, NODE_INFO);
215                 }
216         }
217
218         pfn = pgdat->node_start_pfn;
219         end_pfn = pfn + pgdat->node_spanned_pages;
220
221         /* register_section info */
222         for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
223                 /*
224                  * Some platforms can assign the same pfn to multiple nodes - on
225                  * node0 as well as nodeN.  To avoid registering a pfn against
226                  * multiple nodes we check that this pfn does not already
227                  * reside in some other node.
228                  */
229                 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
230                         register_page_bootmem_info_section(pfn);
231         }
232 }
233 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
234
235 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
236                            unsigned long end_pfn)
237 {
238         unsigned long old_zone_end_pfn;
239
240         zone_span_writelock(zone);
241
242         old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
243         if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
244                 zone->zone_start_pfn = start_pfn;
245
246         zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
247                                 zone->zone_start_pfn;
248
249         zone_span_writeunlock(zone);
250 }
251
252 static void resize_zone(struct zone *zone, unsigned long start_pfn,
253                 unsigned long end_pfn)
254 {
255         zone_span_writelock(zone);
256
257         if (end_pfn - start_pfn) {
258                 zone->zone_start_pfn = start_pfn;
259                 zone->spanned_pages = end_pfn - start_pfn;
260         } else {
261                 /*
262                  * make it consist as free_area_init_core(),
263                  * if spanned_pages = 0, then keep start_pfn = 0
264                  */
265                 zone->zone_start_pfn = 0;
266                 zone->spanned_pages = 0;
267         }
268
269         zone_span_writeunlock(zone);
270 }
271
272 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
273                 unsigned long end_pfn)
274 {
275         enum zone_type zid = zone_idx(zone);
276         int nid = zone->zone_pgdat->node_id;
277         unsigned long pfn;
278
279         for (pfn = start_pfn; pfn < end_pfn; pfn++)
280                 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
281 }
282
283 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
284                 unsigned long start_pfn, unsigned long end_pfn)
285 {
286         int ret;
287         unsigned long flags;
288         unsigned long z1_start_pfn;
289
290         if (!z1->wait_table) {
291                 ret = init_currently_empty_zone(z1, start_pfn,
292                         end_pfn - start_pfn, MEMMAP_HOTPLUG);
293                 if (ret)
294                         return ret;
295         }
296
297         pgdat_resize_lock(z1->zone_pgdat, &flags);
298
299         /* can't move pfns which are higher than @z2 */
300         if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
301                 goto out_fail;
302         /* the move out part mast at the left most of @z2 */
303         if (start_pfn > z2->zone_start_pfn)
304                 goto out_fail;
305         /* must included/overlap */
306         if (end_pfn <= z2->zone_start_pfn)
307                 goto out_fail;
308
309         /* use start_pfn for z1's start_pfn if z1 is empty */
310         if (z1->spanned_pages)
311                 z1_start_pfn = z1->zone_start_pfn;
312         else
313                 z1_start_pfn = start_pfn;
314
315         resize_zone(z1, z1_start_pfn, end_pfn);
316         resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
317
318         pgdat_resize_unlock(z1->zone_pgdat, &flags);
319
320         fix_zone_id(z1, start_pfn, end_pfn);
321
322         return 0;
323 out_fail:
324         pgdat_resize_unlock(z1->zone_pgdat, &flags);
325         return -1;
326 }
327
328 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
329                 unsigned long start_pfn, unsigned long end_pfn)
330 {
331         int ret;
332         unsigned long flags;
333         unsigned long z2_end_pfn;
334
335         if (!z2->wait_table) {
336                 ret = init_currently_empty_zone(z2, start_pfn,
337                         end_pfn - start_pfn, MEMMAP_HOTPLUG);
338                 if (ret)
339                         return ret;
340         }
341
342         pgdat_resize_lock(z1->zone_pgdat, &flags);
343
344         /* can't move pfns which are lower than @z1 */
345         if (z1->zone_start_pfn > start_pfn)
346                 goto out_fail;
347         /* the move out part mast at the right most of @z1 */
348         if (z1->zone_start_pfn + z1->spanned_pages >  end_pfn)
349                 goto out_fail;
350         /* must included/overlap */
351         if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
352                 goto out_fail;
353
354         /* use end_pfn for z2's end_pfn if z2 is empty */
355         if (z2->spanned_pages)
356                 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
357         else
358                 z2_end_pfn = end_pfn;
359
360         resize_zone(z1, z1->zone_start_pfn, start_pfn);
361         resize_zone(z2, start_pfn, z2_end_pfn);
362
363         pgdat_resize_unlock(z1->zone_pgdat, &flags);
364
365         fix_zone_id(z2, start_pfn, end_pfn);
366
367         return 0;
368 out_fail:
369         pgdat_resize_unlock(z1->zone_pgdat, &flags);
370         return -1;
371 }
372
373 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
374                             unsigned long end_pfn)
375 {
376         unsigned long old_pgdat_end_pfn =
377                 pgdat->node_start_pfn + pgdat->node_spanned_pages;
378
379         if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
380                 pgdat->node_start_pfn = start_pfn;
381
382         pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
383                                         pgdat->node_start_pfn;
384 }
385
386 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
387 {
388         struct pglist_data *pgdat = zone->zone_pgdat;
389         int nr_pages = PAGES_PER_SECTION;
390         int nid = pgdat->node_id;
391         int zone_type;
392         unsigned long flags;
393
394         zone_type = zone - pgdat->node_zones;
395         if (!zone->wait_table) {
396                 int ret;
397
398                 ret = init_currently_empty_zone(zone, phys_start_pfn,
399                                                 nr_pages, MEMMAP_HOTPLUG);
400                 if (ret)
401                         return ret;
402         }
403         pgdat_resize_lock(zone->zone_pgdat, &flags);
404         grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
405         grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
406                         phys_start_pfn + nr_pages);
407         pgdat_resize_unlock(zone->zone_pgdat, &flags);
408         memmap_init_zone(nr_pages, nid, zone_type,
409                          phys_start_pfn, MEMMAP_HOTPLUG);
410         return 0;
411 }
412
413 static int __meminit __add_section(int nid, struct zone *zone,
414                                         unsigned long phys_start_pfn)
415 {
416         int nr_pages = PAGES_PER_SECTION;
417         int ret;
418
419         if (pfn_valid(phys_start_pfn))
420                 return -EEXIST;
421
422         ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
423
424         if (ret < 0)
425                 return ret;
426
427         ret = __add_zone(zone, phys_start_pfn);
428
429         if (ret < 0)
430                 return ret;
431
432         return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
433 }
434
435 #ifdef CONFIG_SPARSEMEM_VMEMMAP
436 static int __remove_section(struct zone *zone, struct mem_section *ms)
437 {
438         /*
439          * XXX: Freeing memmap with vmemmap is not implement yet.
440          *      This should be removed later.
441          */
442         return -EBUSY;
443 }
444 #else
445 static int __remove_section(struct zone *zone, struct mem_section *ms)
446 {
447         int ret = -EINVAL;
448
449         if (!valid_section(ms))
450                 return ret;
451
452         ret = unregister_memory_section(ms);
453         if (ret)
454                 return ret;
455
456         sparse_remove_one_section(zone, ms);
457         return 0;
458 }
459 #endif
460
461 /*
462  * Reasonably generic function for adding memory.  It is
463  * expected that archs that support memory hotplug will
464  * call this function after deciding the zone to which to
465  * add the new pages.
466  */
467 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
468                         unsigned long nr_pages)
469 {
470         unsigned long i;
471         int err = 0;
472         int start_sec, end_sec;
473         /* during initialize mem_map, align hot-added range to section */
474         start_sec = pfn_to_section_nr(phys_start_pfn);
475         end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
476
477         for (i = start_sec; i <= end_sec; i++) {
478                 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
479
480                 /*
481                  * EEXIST is finally dealt with by ioresource collision
482                  * check. see add_memory() => register_memory_resource()
483                  * Warning will be printed if there is collision.
484                  */
485                 if (err && (err != -EEXIST))
486                         break;
487                 err = 0;
488         }
489
490         return err;
491 }
492 EXPORT_SYMBOL_GPL(__add_pages);
493
494 /**
495  * __remove_pages() - remove sections of pages from a zone
496  * @zone: zone from which pages need to be removed
497  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
498  * @nr_pages: number of pages to remove (must be multiple of section size)
499  *
500  * Generic helper function to remove section mappings and sysfs entries
501  * for the section of the memory we are removing. Caller needs to make
502  * sure that pages are marked reserved and zones are adjust properly by
503  * calling offline_pages().
504  */
505 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
506                  unsigned long nr_pages)
507 {
508         unsigned long i, ret = 0;
509         int sections_to_remove;
510
511         /*
512          * We can only remove entire sections
513          */
514         BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
515         BUG_ON(nr_pages % PAGES_PER_SECTION);
516
517         release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
518
519         sections_to_remove = nr_pages / PAGES_PER_SECTION;
520         for (i = 0; i < sections_to_remove; i++) {
521                 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
522                 ret = __remove_section(zone, __pfn_to_section(pfn));
523                 if (ret)
524                         break;
525         }
526         return ret;
527 }
528 EXPORT_SYMBOL_GPL(__remove_pages);
529
530 int set_online_page_callback(online_page_callback_t callback)
531 {
532         int rc = -EINVAL;
533
534         lock_memory_hotplug();
535
536         if (online_page_callback == generic_online_page) {
537                 online_page_callback = callback;
538                 rc = 0;
539         }
540
541         unlock_memory_hotplug();
542
543         return rc;
544 }
545 EXPORT_SYMBOL_GPL(set_online_page_callback);
546
547 int restore_online_page_callback(online_page_callback_t callback)
548 {
549         int rc = -EINVAL;
550
551         lock_memory_hotplug();
552
553         if (online_page_callback == callback) {
554                 online_page_callback = generic_online_page;
555                 rc = 0;
556         }
557
558         unlock_memory_hotplug();
559
560         return rc;
561 }
562 EXPORT_SYMBOL_GPL(restore_online_page_callback);
563
564 void __online_page_set_limits(struct page *page)
565 {
566         unsigned long pfn = page_to_pfn(page);
567
568         if (pfn >= num_physpages)
569                 num_physpages = pfn + 1;
570 }
571 EXPORT_SYMBOL_GPL(__online_page_set_limits);
572
573 void __online_page_increment_counters(struct page *page)
574 {
575         totalram_pages++;
576
577 #ifdef CONFIG_HIGHMEM
578         if (PageHighMem(page))
579                 totalhigh_pages++;
580 #endif
581 }
582 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
583
584 void __online_page_free(struct page *page)
585 {
586         ClearPageReserved(page);
587         init_page_count(page);
588         __free_page(page);
589 }
590 EXPORT_SYMBOL_GPL(__online_page_free);
591
592 static void generic_online_page(struct page *page)
593 {
594         __online_page_set_limits(page);
595         __online_page_increment_counters(page);
596         __online_page_free(page);
597 }
598
599 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
600                         void *arg)
601 {
602         unsigned long i;
603         unsigned long onlined_pages = *(unsigned long *)arg;
604         struct page *page;
605         if (PageReserved(pfn_to_page(start_pfn)))
606                 for (i = 0; i < nr_pages; i++) {
607                         page = pfn_to_page(start_pfn + i);
608                         (*online_page_callback)(page);
609                         onlined_pages++;
610                 }
611         *(unsigned long *)arg = onlined_pages;
612         return 0;
613 }
614
615 #ifdef CONFIG_MOVABLE_NODE
616 /*
617  * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
618  * normal memory.
619  */
620 static bool can_online_high_movable(struct zone *zone)
621 {
622         return true;
623 }
624 #else /* CONFIG_MOVABLE_NODE */
625 /* ensure every online node has NORMAL memory */
626 static bool can_online_high_movable(struct zone *zone)
627 {
628         return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
629 }
630 #endif /* CONFIG_MOVABLE_NODE */
631
632 /* check which state of node_states will be changed when online memory */
633 static void node_states_check_changes_online(unsigned long nr_pages,
634         struct zone *zone, struct memory_notify *arg)
635 {
636         int nid = zone_to_nid(zone);
637         enum zone_type zone_last = ZONE_NORMAL;
638
639         /*
640          * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
641          * contains nodes which have zones of 0...ZONE_NORMAL,
642          * set zone_last to ZONE_NORMAL.
643          *
644          * If we don't have HIGHMEM nor movable node,
645          * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
646          * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
647          */
648         if (N_MEMORY == N_NORMAL_MEMORY)
649                 zone_last = ZONE_MOVABLE;
650
651         /*
652          * if the memory to be online is in a zone of 0...zone_last, and
653          * the zones of 0...zone_last don't have memory before online, we will
654          * need to set the node to node_states[N_NORMAL_MEMORY] after
655          * the memory is online.
656          */
657         if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
658                 arg->status_change_nid_normal = nid;
659         else
660                 arg->status_change_nid_normal = -1;
661
662 #ifdef CONFIG_HIGHMEM
663         /*
664          * If we have movable node, node_states[N_HIGH_MEMORY]
665          * contains nodes which have zones of 0...ZONE_HIGHMEM,
666          * set zone_last to ZONE_HIGHMEM.
667          *
668          * If we don't have movable node, node_states[N_NORMAL_MEMORY]
669          * contains nodes which have zones of 0...ZONE_MOVABLE,
670          * set zone_last to ZONE_MOVABLE.
671          */
672         zone_last = ZONE_HIGHMEM;
673         if (N_MEMORY == N_HIGH_MEMORY)
674                 zone_last = ZONE_MOVABLE;
675
676         if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
677                 arg->status_change_nid_high = nid;
678         else
679                 arg->status_change_nid_high = -1;
680 #else
681         arg->status_change_nid_high = arg->status_change_nid_normal;
682 #endif
683
684         /*
685          * if the node don't have memory befor online, we will need to
686          * set the node to node_states[N_MEMORY] after the memory
687          * is online.
688          */
689         if (!node_state(nid, N_MEMORY))
690                 arg->status_change_nid = nid;
691         else
692                 arg->status_change_nid = -1;
693 }
694
695 static void node_states_set_node(int node, struct memory_notify *arg)
696 {
697         if (arg->status_change_nid_normal >= 0)
698                 node_set_state(node, N_NORMAL_MEMORY);
699
700         if (arg->status_change_nid_high >= 0)
701                 node_set_state(node, N_HIGH_MEMORY);
702
703         node_set_state(node, N_MEMORY);
704 }
705
706
707 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
708 {
709         unsigned long onlined_pages = 0;
710         struct zone *zone;
711         int need_zonelists_rebuild = 0;
712         int nid;
713         int ret;
714         struct memory_notify arg;
715
716         lock_memory_hotplug();
717         /*
718          * This doesn't need a lock to do pfn_to_page().
719          * The section can't be removed here because of the
720          * memory_block->state_mutex.
721          */
722         zone = page_zone(pfn_to_page(pfn));
723
724         if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
725             !can_online_high_movable(zone)) {
726                 unlock_memory_hotplug();
727                 return -1;
728         }
729
730         if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
731                 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
732                         unlock_memory_hotplug();
733                         return -1;
734                 }
735         }
736         if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
737                 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
738                         unlock_memory_hotplug();
739                         return -1;
740                 }
741         }
742
743         /* Previous code may changed the zone of the pfn range */
744         zone = page_zone(pfn_to_page(pfn));
745
746         arg.start_pfn = pfn;
747         arg.nr_pages = nr_pages;
748         node_states_check_changes_online(nr_pages, zone, &arg);
749
750         nid = page_to_nid(pfn_to_page(pfn));
751
752         ret = memory_notify(MEM_GOING_ONLINE, &arg);
753         ret = notifier_to_errno(ret);
754         if (ret) {
755                 memory_notify(MEM_CANCEL_ONLINE, &arg);
756                 unlock_memory_hotplug();
757                 return ret;
758         }
759         /*
760          * If this zone is not populated, then it is not in zonelist.
761          * This means the page allocator ignores this zone.
762          * So, zonelist must be updated after online.
763          */
764         mutex_lock(&zonelists_mutex);
765         if (!populated_zone(zone)) {
766                 need_zonelists_rebuild = 1;
767                 build_all_zonelists(NULL, zone);
768         }
769
770         ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
771                 online_pages_range);
772         if (ret) {
773                 if (need_zonelists_rebuild)
774                         zone_pcp_reset(zone);
775                 mutex_unlock(&zonelists_mutex);
776                 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
777                        (unsigned long long) pfn << PAGE_SHIFT,
778                        (((unsigned long long) pfn + nr_pages)
779                             << PAGE_SHIFT) - 1);
780                 memory_notify(MEM_CANCEL_ONLINE, &arg);
781                 unlock_memory_hotplug();
782                 return ret;
783         }
784
785         zone->managed_pages += onlined_pages;
786         zone->present_pages += onlined_pages;
787         zone->zone_pgdat->node_present_pages += onlined_pages;
788         if (onlined_pages) {
789                 node_states_set_node(zone_to_nid(zone), &arg);
790                 if (need_zonelists_rebuild)
791                         build_all_zonelists(NULL, NULL);
792                 else
793                         zone_pcp_update(zone);
794         }
795
796         mutex_unlock(&zonelists_mutex);
797
798         init_per_zone_wmark_min();
799
800         if (onlined_pages)
801                 kswapd_run(zone_to_nid(zone));
802
803         vm_total_pages = nr_free_pagecache_pages();
804
805         writeback_set_ratelimit();
806
807         if (onlined_pages)
808                 memory_notify(MEM_ONLINE, &arg);
809         unlock_memory_hotplug();
810
811         return 0;
812 }
813 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
814
815 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
816 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
817 {
818         struct pglist_data *pgdat;
819         unsigned long zones_size[MAX_NR_ZONES] = {0};
820         unsigned long zholes_size[MAX_NR_ZONES] = {0};
821         unsigned long start_pfn = start >> PAGE_SHIFT;
822
823         pgdat = arch_alloc_nodedata(nid);
824         if (!pgdat)
825                 return NULL;
826
827         arch_refresh_nodedata(nid, pgdat);
828
829         /* we can use NODE_DATA(nid) from here */
830
831         /* init node's zones as empty zones, we don't have any present pages.*/
832         free_area_init_node(nid, zones_size, start_pfn, zholes_size);
833
834         /*
835          * The node we allocated has no zone fallback lists. For avoiding
836          * to access not-initialized zonelist, build here.
837          */
838         mutex_lock(&zonelists_mutex);
839         build_all_zonelists(pgdat, NULL);
840         mutex_unlock(&zonelists_mutex);
841
842         return pgdat;
843 }
844
845 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
846 {
847         arch_refresh_nodedata(nid, NULL);
848         arch_free_nodedata(pgdat);
849         return;
850 }
851
852
853 /*
854  * called by cpu_up() to online a node without onlined memory.
855  */
856 int mem_online_node(int nid)
857 {
858         pg_data_t       *pgdat;
859         int     ret;
860
861         lock_memory_hotplug();
862         pgdat = hotadd_new_pgdat(nid, 0);
863         if (!pgdat) {
864                 ret = -ENOMEM;
865                 goto out;
866         }
867         node_set_online(nid);
868         ret = register_one_node(nid);
869         BUG_ON(ret);
870
871 out:
872         unlock_memory_hotplug();
873         return ret;
874 }
875
876 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
877 int __ref add_memory(int nid, u64 start, u64 size)
878 {
879         pg_data_t *pgdat = NULL;
880         int new_pgdat = 0;
881         struct resource *res;
882         int ret;
883
884         lock_memory_hotplug();
885
886         res = register_memory_resource(start, size);
887         ret = -EEXIST;
888         if (!res)
889                 goto out;
890
891         if (!node_online(nid)) {
892                 pgdat = hotadd_new_pgdat(nid, start);
893                 ret = -ENOMEM;
894                 if (!pgdat)
895                         goto error;
896                 new_pgdat = 1;
897         }
898
899         /* call arch's memory hotadd */
900         ret = arch_add_memory(nid, start, size);
901
902         if (ret < 0)
903                 goto error;
904
905         /* we online node here. we can't roll back from here. */
906         node_set_online(nid);
907
908         if (new_pgdat) {
909                 ret = register_one_node(nid);
910                 /*
911                  * If sysfs file of new node can't create, cpu on the node
912                  * can't be hot-added. There is no rollback way now.
913                  * So, check by BUG_ON() to catch it reluctantly..
914                  */
915                 BUG_ON(ret);
916         }
917
918         /* create new memmap entry */
919         firmware_map_add_hotplug(start, start + size, "System RAM");
920
921         goto out;
922
923 error:
924         /* rollback pgdat allocation and others */
925         if (new_pgdat)
926                 rollback_node_hotadd(nid, pgdat);
927         release_memory_resource(res);
928
929 out:
930         unlock_memory_hotplug();
931         return ret;
932 }
933 EXPORT_SYMBOL_GPL(add_memory);
934
935 #ifdef CONFIG_MEMORY_HOTREMOVE
936 /*
937  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
938  * set and the size of the free page is given by page_order(). Using this,
939  * the function determines if the pageblock contains only free pages.
940  * Due to buddy contraints, a free page at least the size of a pageblock will
941  * be located at the start of the pageblock
942  */
943 static inline int pageblock_free(struct page *page)
944 {
945         return PageBuddy(page) && page_order(page) >= pageblock_order;
946 }
947
948 /* Return the start of the next active pageblock after a given page */
949 static struct page *next_active_pageblock(struct page *page)
950 {
951         /* Ensure the starting page is pageblock-aligned */
952         BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
953
954         /* If the entire pageblock is free, move to the end of free page */
955         if (pageblock_free(page)) {
956                 int order;
957                 /* be careful. we don't have locks, page_order can be changed.*/
958                 order = page_order(page);
959                 if ((order < MAX_ORDER) && (order >= pageblock_order))
960                         return page + (1 << order);
961         }
962
963         return page + pageblock_nr_pages;
964 }
965
966 /* Checks if this range of memory is likely to be hot-removable. */
967 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
968 {
969         struct page *page = pfn_to_page(start_pfn);
970         struct page *end_page = page + nr_pages;
971
972         /* Check the starting page of each pageblock within the range */
973         for (; page < end_page; page = next_active_pageblock(page)) {
974                 if (!is_pageblock_removable_nolock(page))
975                         return 0;
976                 cond_resched();
977         }
978
979         /* All pageblocks in the memory block are likely to be hot-removable */
980         return 1;
981 }
982
983 /*
984  * Confirm all pages in a range [start, end) is belongs to the same zone.
985  */
986 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
987 {
988         unsigned long pfn;
989         struct zone *zone = NULL;
990         struct page *page;
991         int i;
992         for (pfn = start_pfn;
993              pfn < end_pfn;
994              pfn += MAX_ORDER_NR_PAGES) {
995                 i = 0;
996                 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
997                 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
998                         i++;
999                 if (i == MAX_ORDER_NR_PAGES)
1000                         continue;
1001                 page = pfn_to_page(pfn + i);
1002                 if (zone && page_zone(page) != zone)
1003                         return 0;
1004                 zone = page_zone(page);
1005         }
1006         return 1;
1007 }
1008
1009 /*
1010  * Scanning pfn is much easier than scanning lru list.
1011  * Scan pfn from start to end and Find LRU page.
1012  */
1013 static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
1014 {
1015         unsigned long pfn;
1016         struct page *page;
1017         for (pfn = start; pfn < end; pfn++) {
1018                 if (pfn_valid(pfn)) {
1019                         page = pfn_to_page(pfn);
1020                         if (PageLRU(page))
1021                                 return pfn;
1022                 }
1023         }
1024         return 0;
1025 }
1026
1027 #define NR_OFFLINE_AT_ONCE_PAGES        (256)
1028 static int
1029 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1030 {
1031         unsigned long pfn;
1032         struct page *page;
1033         int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1034         int not_managed = 0;
1035         int ret = 0;
1036         LIST_HEAD(source);
1037
1038         for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1039                 if (!pfn_valid(pfn))
1040                         continue;
1041                 page = pfn_to_page(pfn);
1042                 if (!get_page_unless_zero(page))
1043                         continue;
1044                 /*
1045                  * We can skip free pages. And we can only deal with pages on
1046                  * LRU.
1047                  */
1048                 ret = isolate_lru_page(page);
1049                 if (!ret) { /* Success */
1050                         put_page(page);
1051                         list_add_tail(&page->lru, &source);
1052                         move_pages--;
1053                         inc_zone_page_state(page, NR_ISOLATED_ANON +
1054                                             page_is_file_cache(page));
1055
1056                 } else {
1057 #ifdef CONFIG_DEBUG_VM
1058                         printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1059                                pfn);
1060                         dump_page(page);
1061 #endif
1062                         put_page(page);
1063                         /* Because we don't have big zone->lock. we should
1064                            check this again here. */
1065                         if (page_count(page)) {
1066                                 not_managed++;
1067                                 ret = -EBUSY;
1068                                 break;
1069                         }
1070                 }
1071         }
1072         if (!list_empty(&source)) {
1073                 if (not_managed) {
1074                         putback_lru_pages(&source);
1075                         goto out;
1076                 }
1077
1078                 /*
1079                  * alloc_migrate_target should be improooooved!!
1080                  * migrate_pages returns # of failed pages.
1081                  */
1082                 ret = migrate_pages(&source, alloc_migrate_target, 0,
1083                                                         true, MIGRATE_SYNC,
1084                                                         MR_MEMORY_HOTPLUG);
1085                 if (ret)
1086                         putback_lru_pages(&source);
1087         }
1088 out:
1089         return ret;
1090 }
1091
1092 /*
1093  * remove from free_area[] and mark all as Reserved.
1094  */
1095 static int
1096 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1097                         void *data)
1098 {
1099         __offline_isolated_pages(start, start + nr_pages);
1100         return 0;
1101 }
1102
1103 static void
1104 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1105 {
1106         walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1107                                 offline_isolated_pages_cb);
1108 }
1109
1110 /*
1111  * Check all pages in range, recoreded as memory resource, are isolated.
1112  */
1113 static int
1114 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1115                         void *data)
1116 {
1117         int ret;
1118         long offlined = *(long *)data;
1119         ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1120         offlined = nr_pages;
1121         if (!ret)
1122                 *(long *)data += offlined;
1123         return ret;
1124 }
1125
1126 static long
1127 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1128 {
1129         long offlined = 0;
1130         int ret;
1131
1132         ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1133                         check_pages_isolated_cb);
1134         if (ret < 0)
1135                 offlined = (long)ret;
1136         return offlined;
1137 }
1138
1139 #ifdef CONFIG_MOVABLE_NODE
1140 /*
1141  * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1142  * normal memory.
1143  */
1144 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1145 {
1146         return true;
1147 }
1148 #else /* CONFIG_MOVABLE_NODE */
1149 /* ensure the node has NORMAL memory if it is still online */
1150 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1151 {
1152         struct pglist_data *pgdat = zone->zone_pgdat;
1153         unsigned long present_pages = 0;
1154         enum zone_type zt;
1155
1156         for (zt = 0; zt <= ZONE_NORMAL; zt++)
1157                 present_pages += pgdat->node_zones[zt].present_pages;
1158
1159         if (present_pages > nr_pages)
1160                 return true;
1161
1162         present_pages = 0;
1163         for (; zt <= ZONE_MOVABLE; zt++)
1164                 present_pages += pgdat->node_zones[zt].present_pages;
1165
1166         /*
1167          * we can't offline the last normal memory until all
1168          * higher memory is offlined.
1169          */
1170         return present_pages == 0;
1171 }
1172 #endif /* CONFIG_MOVABLE_NODE */
1173
1174 /* check which state of node_states will be changed when offline memory */
1175 static void node_states_check_changes_offline(unsigned long nr_pages,
1176                 struct zone *zone, struct memory_notify *arg)
1177 {
1178         struct pglist_data *pgdat = zone->zone_pgdat;
1179         unsigned long present_pages = 0;
1180         enum zone_type zt, zone_last = ZONE_NORMAL;
1181
1182         /*
1183          * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1184          * contains nodes which have zones of 0...ZONE_NORMAL,
1185          * set zone_last to ZONE_NORMAL.
1186          *
1187          * If we don't have HIGHMEM nor movable node,
1188          * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1189          * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1190          */
1191         if (N_MEMORY == N_NORMAL_MEMORY)
1192                 zone_last = ZONE_MOVABLE;
1193
1194         /*
1195          * check whether node_states[N_NORMAL_MEMORY] will be changed.
1196          * If the memory to be offline is in a zone of 0...zone_last,
1197          * and it is the last present memory, 0...zone_last will
1198          * become empty after offline , thus we can determind we will
1199          * need to clear the node from node_states[N_NORMAL_MEMORY].
1200          */
1201         for (zt = 0; zt <= zone_last; zt++)
1202                 present_pages += pgdat->node_zones[zt].present_pages;
1203         if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1204                 arg->status_change_nid_normal = zone_to_nid(zone);
1205         else
1206                 arg->status_change_nid_normal = -1;
1207
1208 #ifdef CONFIG_HIGHMEM
1209         /*
1210          * If we have movable node, node_states[N_HIGH_MEMORY]
1211          * contains nodes which have zones of 0...ZONE_HIGHMEM,
1212          * set zone_last to ZONE_HIGHMEM.
1213          *
1214          * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1215          * contains nodes which have zones of 0...ZONE_MOVABLE,
1216          * set zone_last to ZONE_MOVABLE.
1217          */
1218         zone_last = ZONE_HIGHMEM;
1219         if (N_MEMORY == N_HIGH_MEMORY)
1220                 zone_last = ZONE_MOVABLE;
1221
1222         for (; zt <= zone_last; zt++)
1223                 present_pages += pgdat->node_zones[zt].present_pages;
1224         if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1225                 arg->status_change_nid_high = zone_to_nid(zone);
1226         else
1227                 arg->status_change_nid_high = -1;
1228 #else
1229         arg->status_change_nid_high = arg->status_change_nid_normal;
1230 #endif
1231
1232         /*
1233          * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1234          */
1235         zone_last = ZONE_MOVABLE;
1236
1237         /*
1238          * check whether node_states[N_HIGH_MEMORY] will be changed
1239          * If we try to offline the last present @nr_pages from the node,
1240          * we can determind we will need to clear the node from
1241          * node_states[N_HIGH_MEMORY].
1242          */
1243         for (; zt <= zone_last; zt++)
1244                 present_pages += pgdat->node_zones[zt].present_pages;
1245         if (nr_pages >= present_pages)
1246                 arg->status_change_nid = zone_to_nid(zone);
1247         else
1248                 arg->status_change_nid = -1;
1249 }
1250
1251 static void node_states_clear_node(int node, struct memory_notify *arg)
1252 {
1253         if (arg->status_change_nid_normal >= 0)
1254                 node_clear_state(node, N_NORMAL_MEMORY);
1255
1256         if ((N_MEMORY != N_NORMAL_MEMORY) &&
1257             (arg->status_change_nid_high >= 0))
1258                 node_clear_state(node, N_HIGH_MEMORY);
1259
1260         if ((N_MEMORY != N_HIGH_MEMORY) &&
1261             (arg->status_change_nid >= 0))
1262                 node_clear_state(node, N_MEMORY);
1263 }
1264
1265 static int __ref __offline_pages(unsigned long start_pfn,
1266                   unsigned long end_pfn, unsigned long timeout)
1267 {
1268         unsigned long pfn, nr_pages, expire;
1269         long offlined_pages;
1270         int ret, drain, retry_max, node;
1271         struct zone *zone;
1272         struct memory_notify arg;
1273
1274         BUG_ON(start_pfn >= end_pfn);
1275         /* at least, alignment against pageblock is necessary */
1276         if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1277                 return -EINVAL;
1278         if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1279                 return -EINVAL;
1280         /* This makes hotplug much easier...and readable.
1281            we assume this for now. .*/
1282         if (!test_pages_in_a_zone(start_pfn, end_pfn))
1283                 return -EINVAL;
1284
1285         lock_memory_hotplug();
1286
1287         zone = page_zone(pfn_to_page(start_pfn));
1288         node = zone_to_nid(zone);
1289         nr_pages = end_pfn - start_pfn;
1290
1291         ret = -EINVAL;
1292         if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1293                 goto out;
1294
1295         /* set above range as isolated */
1296         ret = start_isolate_page_range(start_pfn, end_pfn,
1297                                        MIGRATE_MOVABLE, true);
1298         if (ret)
1299                 goto out;
1300
1301         arg.start_pfn = start_pfn;
1302         arg.nr_pages = nr_pages;
1303         node_states_check_changes_offline(nr_pages, zone, &arg);
1304
1305         ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1306         ret = notifier_to_errno(ret);
1307         if (ret)
1308                 goto failed_removal;
1309
1310         pfn = start_pfn;
1311         expire = jiffies + timeout;
1312         drain = 0;
1313         retry_max = 5;
1314 repeat:
1315         /* start memory hot removal */
1316         ret = -EAGAIN;
1317         if (time_after(jiffies, expire))
1318                 goto failed_removal;
1319         ret = -EINTR;
1320         if (signal_pending(current))
1321                 goto failed_removal;
1322         ret = 0;
1323         if (drain) {
1324                 lru_add_drain_all();
1325                 cond_resched();
1326                 drain_all_pages();
1327         }
1328
1329         pfn = scan_lru_pages(start_pfn, end_pfn);
1330         if (pfn) { /* We have page on LRU */
1331                 ret = do_migrate_range(pfn, end_pfn);
1332                 if (!ret) {
1333                         drain = 1;
1334                         goto repeat;
1335                 } else {
1336                         if (ret < 0)
1337                                 if (--retry_max == 0)
1338                                         goto failed_removal;
1339                         yield();
1340                         drain = 1;
1341                         goto repeat;
1342                 }
1343         }
1344         /* drain all zone's lru pagevec, this is asynchronous... */
1345         lru_add_drain_all();
1346         yield();
1347         /* drain pcp pages, this is synchronous. */
1348         drain_all_pages();
1349         /* check again */
1350         offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1351         if (offlined_pages < 0) {
1352                 ret = -EBUSY;
1353                 goto failed_removal;
1354         }
1355         printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1356         /* Ok, all of our target is isolated.
1357            We cannot do rollback at this point. */
1358         offline_isolated_pages(start_pfn, end_pfn);
1359         /* reset pagetype flags and makes migrate type to be MOVABLE */
1360         undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1361         /* removal success */
1362         zone->managed_pages -= offlined_pages;
1363         zone->present_pages -= offlined_pages;
1364         zone->zone_pgdat->node_present_pages -= offlined_pages;
1365         totalram_pages -= offlined_pages;
1366
1367         init_per_zone_wmark_min();
1368
1369         if (!populated_zone(zone)) {
1370                 zone_pcp_reset(zone);
1371                 mutex_lock(&zonelists_mutex);
1372                 build_all_zonelists(NULL, NULL);
1373                 mutex_unlock(&zonelists_mutex);
1374         } else
1375                 zone_pcp_update(zone);
1376
1377         node_states_clear_node(node, &arg);
1378         if (arg.status_change_nid >= 0)
1379                 kswapd_stop(node);
1380
1381         vm_total_pages = nr_free_pagecache_pages();
1382         writeback_set_ratelimit();
1383
1384         memory_notify(MEM_OFFLINE, &arg);
1385         unlock_memory_hotplug();
1386         return 0;
1387
1388 failed_removal:
1389         printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1390                (unsigned long long) start_pfn << PAGE_SHIFT,
1391                ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1392         memory_notify(MEM_CANCEL_OFFLINE, &arg);
1393         /* pushback to free area */
1394         undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1395
1396 out:
1397         unlock_memory_hotplug();
1398         return ret;
1399 }
1400
1401 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1402 {
1403         return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1404 }
1405
1406 /**
1407  * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1408  * @start_pfn: start pfn of the memory range
1409  * @end_pfn: end pft of the memory range
1410  * @arg: argument passed to func
1411  * @func: callback for each memory section walked
1412  *
1413  * This function walks through all present mem sections in range
1414  * [start_pfn, end_pfn) and call func on each mem section.
1415  *
1416  * Returns the return value of func.
1417  */
1418 static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1419                 void *arg, int (*func)(struct memory_block *, void *))
1420 {
1421         struct memory_block *mem = NULL;
1422         struct mem_section *section;
1423         unsigned long pfn, section_nr;
1424         int ret;
1425
1426         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1427                 section_nr = pfn_to_section_nr(pfn);
1428                 if (!present_section_nr(section_nr))
1429                         continue;
1430
1431                 section = __nr_to_section(section_nr);
1432                 /* same memblock? */
1433                 if (mem)
1434                         if ((section_nr >= mem->start_section_nr) &&
1435                             (section_nr <= mem->end_section_nr))
1436                                 continue;
1437
1438                 mem = find_memory_block_hinted(section, mem);
1439                 if (!mem)
1440                         continue;
1441
1442                 ret = func(mem, arg);
1443                 if (ret) {
1444                         kobject_put(&mem->dev.kobj);
1445                         return ret;
1446                 }
1447         }
1448
1449         if (mem)
1450                 kobject_put(&mem->dev.kobj);
1451
1452         return 0;
1453 }
1454
1455 /**
1456  * offline_memory_block_cb - callback function for offlining memory block
1457  * @mem: the memory block to be offlined
1458  * @arg: buffer to hold error msg
1459  *
1460  * Always return 0, and put the error msg in arg if any.
1461  */
1462 static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1463 {
1464         int *ret = arg;
1465         int error = offline_memory_block(mem);
1466
1467         if (error != 0 && *ret == 0)
1468                 *ret = error;
1469
1470         return 0;
1471 }
1472
1473 static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1474 {
1475         int ret = !is_memblock_offlined(mem);
1476
1477         if (unlikely(ret))
1478                 pr_warn("removing memory fails, because memory "
1479                         "[%#010llx-%#010llx] is onlined\n",
1480                         PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1481                         PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1482
1483         return ret;
1484 }
1485
1486 int __ref remove_memory(u64 start, u64 size)
1487 {
1488         unsigned long start_pfn, end_pfn;
1489         int ret = 0;
1490         int retry = 1;
1491
1492         start_pfn = PFN_DOWN(start);
1493         end_pfn = start_pfn + PFN_DOWN(size);
1494
1495         /*
1496          * When CONFIG_MEMCG is on, one memory block may be used by other
1497          * blocks to store page cgroup when onlining pages. But we don't know
1498          * in what order pages are onlined. So we iterate twice to offline
1499          * memory:
1500          * 1st iterate: offline every non primary memory block.
1501          * 2nd iterate: offline primary (i.e. first added) memory block.
1502          */
1503 repeat:
1504         walk_memory_range(start_pfn, end_pfn, &ret,
1505                           offline_memory_block_cb);
1506         if (ret) {
1507                 if (!retry)
1508                         return ret;
1509
1510                 retry = 0;
1511                 ret = 0;
1512                 goto repeat;
1513         }
1514
1515         lock_memory_hotplug();
1516
1517         /*
1518          * we have offlined all memory blocks like this:
1519          *   1. lock memory hotplug
1520          *   2. offline a memory block
1521          *   3. unlock memory hotplug
1522          *
1523          * repeat step1-3 to offline the memory block. All memory blocks
1524          * must be offlined before removing memory. But we don't hold the
1525          * lock in the whole operation. So we should check whether all
1526          * memory blocks are offlined.
1527          */
1528
1529         ret = walk_memory_range(start_pfn, end_pfn, NULL,
1530                                 is_memblock_offlined_cb);
1531         if (ret) {
1532                 unlock_memory_hotplug();
1533                 return ret;
1534         }
1535
1536         /* remove memmap entry */
1537         firmware_map_remove(start, start + size, "System RAM");
1538
1539         arch_remove_memory(start, size);
1540
1541         unlock_memory_hotplug();
1542
1543         return 0;
1544 }
1545 #else
1546 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1547 {
1548         return -EINVAL;
1549 }
1550 int remove_memory(u64 start, u64 size)
1551 {
1552         return -EINVAL;
1553 }
1554 #endif /* CONFIG_MEMORY_HOTREMOVE */
1555 EXPORT_SYMBOL_GPL(remove_memory);