memory hotplug: one more lock on memory hotplug
[pandora-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/module.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 DEFINE_MUTEX(mem_hotplug_mutex);
38
39 void lock_memory_hotplug(void)
40 {
41         mutex_lock(&mem_hotplug_mutex);
42
43         /* for exclusive hibernation if CONFIG_HIBERNATION=y */
44         lock_system_sleep();
45 }
46
47 void unlock_memory_hotplug(void)
48 {
49         unlock_system_sleep();
50         mutex_unlock(&mem_hotplug_mutex);
51 }
52
53
54 /* add this memory to iomem resource */
55 static struct resource *register_memory_resource(u64 start, u64 size)
56 {
57         struct resource *res;
58         res = kzalloc(sizeof(struct resource), GFP_KERNEL);
59         BUG_ON(!res);
60
61         res->name = "System RAM";
62         res->start = start;
63         res->end = start + size - 1;
64         res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
65         if (request_resource(&iomem_resource, res) < 0) {
66                 printk("System RAM resource %llx - %llx cannot be added\n",
67                 (unsigned long long)res->start, (unsigned long long)res->end);
68                 kfree(res);
69                 res = NULL;
70         }
71         return res;
72 }
73
74 static void release_memory_resource(struct resource *res)
75 {
76         if (!res)
77                 return;
78         release_resource(res);
79         kfree(res);
80         return;
81 }
82
83 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
84 #ifndef CONFIG_SPARSEMEM_VMEMMAP
85 static void get_page_bootmem(unsigned long info,  struct page *page, int type)
86 {
87         atomic_set(&page->_mapcount, type);
88         SetPagePrivate(page);
89         set_page_private(page, info);
90         atomic_inc(&page->_count);
91 }
92
93 /* reference to __meminit __free_pages_bootmem is valid
94  * so use __ref to tell modpost not to generate a warning */
95 void __ref put_page_bootmem(struct page *page)
96 {
97         int type;
98
99         type = atomic_read(&page->_mapcount);
100         BUG_ON(type >= -1);
101
102         if (atomic_dec_return(&page->_count) == 1) {
103                 ClearPagePrivate(page);
104                 set_page_private(page, 0);
105                 reset_page_mapcount(page);
106                 __free_pages_bootmem(page, 0);
107         }
108
109 }
110
111 static void register_page_bootmem_info_section(unsigned long start_pfn)
112 {
113         unsigned long *usemap, mapsize, section_nr, i;
114         struct mem_section *ms;
115         struct page *page, *memmap;
116
117         if (!pfn_valid(start_pfn))
118                 return;
119
120         section_nr = pfn_to_section_nr(start_pfn);
121         ms = __nr_to_section(section_nr);
122
123         /* Get section's memmap address */
124         memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
125
126         /*
127          * Get page for the memmap's phys address
128          * XXX: need more consideration for sparse_vmemmap...
129          */
130         page = virt_to_page(memmap);
131         mapsize = sizeof(struct page) * PAGES_PER_SECTION;
132         mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
133
134         /* remember memmap's page */
135         for (i = 0; i < mapsize; i++, page++)
136                 get_page_bootmem(section_nr, page, SECTION_INFO);
137
138         usemap = __nr_to_section(section_nr)->pageblock_flags;
139         page = virt_to_page(usemap);
140
141         mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
142
143         for (i = 0; i < mapsize; i++, page++)
144                 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
145
146 }
147
148 void register_page_bootmem_info_node(struct pglist_data *pgdat)
149 {
150         unsigned long i, pfn, end_pfn, nr_pages;
151         int node = pgdat->node_id;
152         struct page *page;
153         struct zone *zone;
154
155         nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
156         page = virt_to_page(pgdat);
157
158         for (i = 0; i < nr_pages; i++, page++)
159                 get_page_bootmem(node, page, NODE_INFO);
160
161         zone = &pgdat->node_zones[0];
162         for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
163                 if (zone->wait_table) {
164                         nr_pages = zone->wait_table_hash_nr_entries
165                                 * sizeof(wait_queue_head_t);
166                         nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
167                         page = virt_to_page(zone->wait_table);
168
169                         for (i = 0; i < nr_pages; i++, page++)
170                                 get_page_bootmem(node, page, NODE_INFO);
171                 }
172         }
173
174         pfn = pgdat->node_start_pfn;
175         end_pfn = pfn + pgdat->node_spanned_pages;
176
177         /* register_section info */
178         for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
179                 register_page_bootmem_info_section(pfn);
180
181 }
182 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
183
184 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
185                            unsigned long end_pfn)
186 {
187         unsigned long old_zone_end_pfn;
188
189         zone_span_writelock(zone);
190
191         old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
192         if (start_pfn < zone->zone_start_pfn)
193                 zone->zone_start_pfn = start_pfn;
194
195         zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
196                                 zone->zone_start_pfn;
197
198         zone_span_writeunlock(zone);
199 }
200
201 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
202                             unsigned long end_pfn)
203 {
204         unsigned long old_pgdat_end_pfn =
205                 pgdat->node_start_pfn + pgdat->node_spanned_pages;
206
207         if (start_pfn < pgdat->node_start_pfn)
208                 pgdat->node_start_pfn = start_pfn;
209
210         pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
211                                         pgdat->node_start_pfn;
212 }
213
214 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
215 {
216         struct pglist_data *pgdat = zone->zone_pgdat;
217         int nr_pages = PAGES_PER_SECTION;
218         int nid = pgdat->node_id;
219         int zone_type;
220         unsigned long flags;
221
222         zone_type = zone - pgdat->node_zones;
223         if (!zone->wait_table) {
224                 int ret;
225
226                 ret = init_currently_empty_zone(zone, phys_start_pfn,
227                                                 nr_pages, MEMMAP_HOTPLUG);
228                 if (ret)
229                         return ret;
230         }
231         pgdat_resize_lock(zone->zone_pgdat, &flags);
232         grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
233         grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
234                         phys_start_pfn + nr_pages);
235         pgdat_resize_unlock(zone->zone_pgdat, &flags);
236         memmap_init_zone(nr_pages, nid, zone_type,
237                          phys_start_pfn, MEMMAP_HOTPLUG);
238         return 0;
239 }
240
241 static int __meminit __add_section(int nid, struct zone *zone,
242                                         unsigned long phys_start_pfn)
243 {
244         int nr_pages = PAGES_PER_SECTION;
245         int ret;
246
247         if (pfn_valid(phys_start_pfn))
248                 return -EEXIST;
249
250         ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
251
252         if (ret < 0)
253                 return ret;
254
255         ret = __add_zone(zone, phys_start_pfn);
256
257         if (ret < 0)
258                 return ret;
259
260         return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
261 }
262
263 #ifdef CONFIG_SPARSEMEM_VMEMMAP
264 static int __remove_section(struct zone *zone, struct mem_section *ms)
265 {
266         /*
267          * XXX: Freeing memmap with vmemmap is not implement yet.
268          *      This should be removed later.
269          */
270         return -EBUSY;
271 }
272 #else
273 static int __remove_section(struct zone *zone, struct mem_section *ms)
274 {
275         unsigned long flags;
276         struct pglist_data *pgdat = zone->zone_pgdat;
277         int ret = -EINVAL;
278
279         if (!valid_section(ms))
280                 return ret;
281
282         ret = unregister_memory_section(ms);
283         if (ret)
284                 return ret;
285
286         pgdat_resize_lock(pgdat, &flags);
287         sparse_remove_one_section(zone, ms);
288         pgdat_resize_unlock(pgdat, &flags);
289         return 0;
290 }
291 #endif
292
293 /*
294  * Reasonably generic function for adding memory.  It is
295  * expected that archs that support memory hotplug will
296  * call this function after deciding the zone to which to
297  * add the new pages.
298  */
299 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
300                         unsigned long nr_pages)
301 {
302         unsigned long i;
303         int err = 0;
304         int start_sec, end_sec;
305         /* during initialize mem_map, align hot-added range to section */
306         start_sec = pfn_to_section_nr(phys_start_pfn);
307         end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
308
309         for (i = start_sec; i <= end_sec; i++) {
310                 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
311
312                 /*
313                  * EEXIST is finally dealt with by ioresource collision
314                  * check. see add_memory() => register_memory_resource()
315                  * Warning will be printed if there is collision.
316                  */
317                 if (err && (err != -EEXIST))
318                         break;
319                 err = 0;
320         }
321
322         return err;
323 }
324 EXPORT_SYMBOL_GPL(__add_pages);
325
326 /**
327  * __remove_pages() - remove sections of pages from a zone
328  * @zone: zone from which pages need to be removed
329  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
330  * @nr_pages: number of pages to remove (must be multiple of section size)
331  *
332  * Generic helper function to remove section mappings and sysfs entries
333  * for the section of the memory we are removing. Caller needs to make
334  * sure that pages are marked reserved and zones are adjust properly by
335  * calling offline_pages().
336  */
337 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
338                  unsigned long nr_pages)
339 {
340         unsigned long i, ret = 0;
341         int sections_to_remove;
342
343         /*
344          * We can only remove entire sections
345          */
346         BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
347         BUG_ON(nr_pages % PAGES_PER_SECTION);
348
349         sections_to_remove = nr_pages / PAGES_PER_SECTION;
350         for (i = 0; i < sections_to_remove; i++) {
351                 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
352                 release_mem_region(pfn << PAGE_SHIFT,
353                                    PAGES_PER_SECTION << PAGE_SHIFT);
354                 ret = __remove_section(zone, __pfn_to_section(pfn));
355                 if (ret)
356                         break;
357         }
358         return ret;
359 }
360 EXPORT_SYMBOL_GPL(__remove_pages);
361
362 void online_page(struct page *page)
363 {
364         unsigned long pfn = page_to_pfn(page);
365
366         totalram_pages++;
367         if (pfn >= num_physpages)
368                 num_physpages = pfn + 1;
369
370 #ifdef CONFIG_HIGHMEM
371         if (PageHighMem(page))
372                 totalhigh_pages++;
373 #endif
374
375 #ifdef CONFIG_FLATMEM
376         max_mapnr = max(page_to_pfn(page), max_mapnr);
377 #endif
378
379         ClearPageReserved(page);
380         init_page_count(page);
381         __free_page(page);
382 }
383
384 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
385                         void *arg)
386 {
387         unsigned long i;
388         unsigned long onlined_pages = *(unsigned long *)arg;
389         struct page *page;
390         if (PageReserved(pfn_to_page(start_pfn)))
391                 for (i = 0; i < nr_pages; i++) {
392                         page = pfn_to_page(start_pfn + i);
393                         online_page(page);
394                         onlined_pages++;
395                 }
396         *(unsigned long *)arg = onlined_pages;
397         return 0;
398 }
399
400
401 int online_pages(unsigned long pfn, unsigned long nr_pages)
402 {
403         unsigned long onlined_pages = 0;
404         struct zone *zone;
405         int need_zonelists_rebuild = 0;
406         int nid;
407         int ret;
408         struct memory_notify arg;
409
410         lock_memory_hotplug();
411         arg.start_pfn = pfn;
412         arg.nr_pages = nr_pages;
413         arg.status_change_nid = -1;
414
415         nid = page_to_nid(pfn_to_page(pfn));
416         if (node_present_pages(nid) == 0)
417                 arg.status_change_nid = nid;
418
419         ret = memory_notify(MEM_GOING_ONLINE, &arg);
420         ret = notifier_to_errno(ret);
421         if (ret) {
422                 memory_notify(MEM_CANCEL_ONLINE, &arg);
423                 unlock_memory_hotplug();
424                 return ret;
425         }
426         /*
427          * This doesn't need a lock to do pfn_to_page().
428          * The section can't be removed here because of the
429          * memory_block->state_mutex.
430          */
431         zone = page_zone(pfn_to_page(pfn));
432         /*
433          * If this zone is not populated, then it is not in zonelist.
434          * This means the page allocator ignores this zone.
435          * So, zonelist must be updated after online.
436          */
437         mutex_lock(&zonelists_mutex);
438         if (!populated_zone(zone))
439                 need_zonelists_rebuild = 1;
440
441         ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
442                 online_pages_range);
443         if (ret) {
444                 mutex_unlock(&zonelists_mutex);
445                 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
446                         nr_pages, pfn);
447                 memory_notify(MEM_CANCEL_ONLINE, &arg);
448                 unlock_memory_hotplug();
449                 return ret;
450         }
451
452         zone->present_pages += onlined_pages;
453         zone->zone_pgdat->node_present_pages += onlined_pages;
454         if (need_zonelists_rebuild)
455                 build_all_zonelists(zone);
456         else
457                 zone_pcp_update(zone);
458
459         mutex_unlock(&zonelists_mutex);
460         setup_per_zone_wmarks();
461         calculate_zone_inactive_ratio(zone);
462         if (onlined_pages) {
463                 kswapd_run(zone_to_nid(zone));
464                 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
465         }
466
467         vm_total_pages = nr_free_pagecache_pages();
468
469         writeback_set_ratelimit();
470
471         if (onlined_pages)
472                 memory_notify(MEM_ONLINE, &arg);
473         unlock_memory_hotplug();
474
475         return 0;
476 }
477 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
478
479 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
480 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
481 {
482         struct pglist_data *pgdat;
483         unsigned long zones_size[MAX_NR_ZONES] = {0};
484         unsigned long zholes_size[MAX_NR_ZONES] = {0};
485         unsigned long start_pfn = start >> PAGE_SHIFT;
486
487         pgdat = arch_alloc_nodedata(nid);
488         if (!pgdat)
489                 return NULL;
490
491         arch_refresh_nodedata(nid, pgdat);
492
493         /* we can use NODE_DATA(nid) from here */
494
495         /* init node's zones as empty zones, we don't have any present pages.*/
496         free_area_init_node(nid, zones_size, start_pfn, zholes_size);
497
498         return pgdat;
499 }
500
501 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
502 {
503         arch_refresh_nodedata(nid, NULL);
504         arch_free_nodedata(pgdat);
505         return;
506 }
507
508
509 /*
510  * called by cpu_up() to online a node without onlined memory.
511  */
512 int mem_online_node(int nid)
513 {
514         pg_data_t       *pgdat;
515         int     ret;
516
517         lock_memory_hotplug();
518         pgdat = hotadd_new_pgdat(nid, 0);
519         if (pgdat) {
520                 ret = -ENOMEM;
521                 goto out;
522         }
523         node_set_online(nid);
524         ret = register_one_node(nid);
525         BUG_ON(ret);
526
527 out:
528         unlock_memory_hotplug();
529         return ret;
530 }
531
532 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
533 int __ref add_memory(int nid, u64 start, u64 size)
534 {
535         pg_data_t *pgdat = NULL;
536         int new_pgdat = 0;
537         struct resource *res;
538         int ret;
539
540         lock_memory_hotplug();
541
542         res = register_memory_resource(start, size);
543         ret = -EEXIST;
544         if (!res)
545                 goto out;
546
547         if (!node_online(nid)) {
548                 pgdat = hotadd_new_pgdat(nid, start);
549                 ret = -ENOMEM;
550                 if (!pgdat)
551                         goto out;
552                 new_pgdat = 1;
553         }
554
555         /* call arch's memory hotadd */
556         ret = arch_add_memory(nid, start, size);
557
558         if (ret < 0)
559                 goto error;
560
561         /* we online node here. we can't roll back from here. */
562         node_set_online(nid);
563
564         if (new_pgdat) {
565                 ret = register_one_node(nid);
566                 /*
567                  * If sysfs file of new node can't create, cpu on the node
568                  * can't be hot-added. There is no rollback way now.
569                  * So, check by BUG_ON() to catch it reluctantly..
570                  */
571                 BUG_ON(ret);
572         }
573
574         /* create new memmap entry */
575         firmware_map_add_hotplug(start, start + size, "System RAM");
576
577         goto out;
578
579 error:
580         /* rollback pgdat allocation and others */
581         if (new_pgdat)
582                 rollback_node_hotadd(nid, pgdat);
583         if (res)
584                 release_memory_resource(res);
585
586 out:
587         unlock_memory_hotplug();
588         return ret;
589 }
590 EXPORT_SYMBOL_GPL(add_memory);
591
592 #ifdef CONFIG_MEMORY_HOTREMOVE
593 /*
594  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
595  * set and the size of the free page is given by page_order(). Using this,
596  * the function determines if the pageblock contains only free pages.
597  * Due to buddy contraints, a free page at least the size of a pageblock will
598  * be located at the start of the pageblock
599  */
600 static inline int pageblock_free(struct page *page)
601 {
602         return PageBuddy(page) && page_order(page) >= pageblock_order;
603 }
604
605 /* Return the start of the next active pageblock after a given page */
606 static struct page *next_active_pageblock(struct page *page)
607 {
608         /* Ensure the starting page is pageblock-aligned */
609         BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
610
611         /* If the entire pageblock is free, move to the end of free page */
612         if (pageblock_free(page)) {
613                 int order;
614                 /* be careful. we don't have locks, page_order can be changed.*/
615                 order = page_order(page);
616                 if ((order < MAX_ORDER) && (order >= pageblock_order))
617                         return page + (1 << order);
618         }
619
620         return page + pageblock_nr_pages;
621 }
622
623 /* Checks if this range of memory is likely to be hot-removable. */
624 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
625 {
626         struct page *page = pfn_to_page(start_pfn);
627         struct page *end_page = page + nr_pages;
628
629         /* Check the starting page of each pageblock within the range */
630         for (; page < end_page; page = next_active_pageblock(page)) {
631                 if (!is_pageblock_removable_nolock(page))
632                         return 0;
633                 cond_resched();
634         }
635
636         /* All pageblocks in the memory block are likely to be hot-removable */
637         return 1;
638 }
639
640 /*
641  * Confirm all pages in a range [start, end) is belongs to the same zone.
642  */
643 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
644 {
645         unsigned long pfn;
646         struct zone *zone = NULL;
647         struct page *page;
648         int i;
649         for (pfn = start_pfn;
650              pfn < end_pfn;
651              pfn += MAX_ORDER_NR_PAGES) {
652                 i = 0;
653                 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
654                 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
655                         i++;
656                 if (i == MAX_ORDER_NR_PAGES)
657                         continue;
658                 page = pfn_to_page(pfn + i);
659                 if (zone && page_zone(page) != zone)
660                         return 0;
661                 zone = page_zone(page);
662         }
663         return 1;
664 }
665
666 /*
667  * Scanning pfn is much easier than scanning lru list.
668  * Scan pfn from start to end and Find LRU page.
669  */
670 static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
671 {
672         unsigned long pfn;
673         struct page *page;
674         for (pfn = start; pfn < end; pfn++) {
675                 if (pfn_valid(pfn)) {
676                         page = pfn_to_page(pfn);
677                         if (PageLRU(page))
678                                 return pfn;
679                 }
680         }
681         return 0;
682 }
683
684 static struct page *
685 hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
686 {
687         /* This should be improooooved!! */
688         return alloc_page(GFP_HIGHUSER_MOVABLE);
689 }
690
691 #define NR_OFFLINE_AT_ONCE_PAGES        (256)
692 static int
693 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
694 {
695         unsigned long pfn;
696         struct page *page;
697         int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
698         int not_managed = 0;
699         int ret = 0;
700         LIST_HEAD(source);
701
702         for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
703                 if (!pfn_valid(pfn))
704                         continue;
705                 page = pfn_to_page(pfn);
706                 if (!page_count(page))
707                         continue;
708                 /*
709                  * We can skip free pages. And we can only deal with pages on
710                  * LRU.
711                  */
712                 ret = isolate_lru_page(page);
713                 if (!ret) { /* Success */
714                         list_add_tail(&page->lru, &source);
715                         move_pages--;
716                         inc_zone_page_state(page, NR_ISOLATED_ANON +
717                                             page_is_file_cache(page));
718
719                 } else {
720 #ifdef CONFIG_DEBUG_VM
721                         printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
722                                pfn);
723                         dump_page(page);
724 #endif
725                         /* Becasue we don't have big zone->lock. we should
726                            check this again here. */
727                         if (page_count(page)) {
728                                 not_managed++;
729                                 ret = -EBUSY;
730                                 break;
731                         }
732                 }
733         }
734         if (!list_empty(&source)) {
735                 if (not_managed) {
736                         putback_lru_pages(&source);
737                         goto out;
738                 }
739                 /* this function returns # of failed pages */
740                 ret = migrate_pages(&source, hotremove_migrate_alloc, 0, 1);
741                 if (ret)
742                         putback_lru_pages(&source);
743         }
744 out:
745         return ret;
746 }
747
748 /*
749  * remove from free_area[] and mark all as Reserved.
750  */
751 static int
752 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
753                         void *data)
754 {
755         __offline_isolated_pages(start, start + nr_pages);
756         return 0;
757 }
758
759 static void
760 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
761 {
762         walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
763                                 offline_isolated_pages_cb);
764 }
765
766 /*
767  * Check all pages in range, recoreded as memory resource, are isolated.
768  */
769 static int
770 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
771                         void *data)
772 {
773         int ret;
774         long offlined = *(long *)data;
775         ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
776         offlined = nr_pages;
777         if (!ret)
778                 *(long *)data += offlined;
779         return ret;
780 }
781
782 static long
783 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
784 {
785         long offlined = 0;
786         int ret;
787
788         ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
789                         check_pages_isolated_cb);
790         if (ret < 0)
791                 offlined = (long)ret;
792         return offlined;
793 }
794
795 static int offline_pages(unsigned long start_pfn,
796                   unsigned long end_pfn, unsigned long timeout)
797 {
798         unsigned long pfn, nr_pages, expire;
799         long offlined_pages;
800         int ret, drain, retry_max, node;
801         struct zone *zone;
802         struct memory_notify arg;
803
804         BUG_ON(start_pfn >= end_pfn);
805         /* at least, alignment against pageblock is necessary */
806         if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
807                 return -EINVAL;
808         if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
809                 return -EINVAL;
810         /* This makes hotplug much easier...and readable.
811            we assume this for now. .*/
812         if (!test_pages_in_a_zone(start_pfn, end_pfn))
813                 return -EINVAL;
814
815         lock_memory_hotplug();
816
817         zone = page_zone(pfn_to_page(start_pfn));
818         node = zone_to_nid(zone);
819         nr_pages = end_pfn - start_pfn;
820
821         /* set above range as isolated */
822         ret = start_isolate_page_range(start_pfn, end_pfn);
823         if (ret)
824                 goto out;
825
826         arg.start_pfn = start_pfn;
827         arg.nr_pages = nr_pages;
828         arg.status_change_nid = -1;
829         if (nr_pages >= node_present_pages(node))
830                 arg.status_change_nid = node;
831
832         ret = memory_notify(MEM_GOING_OFFLINE, &arg);
833         ret = notifier_to_errno(ret);
834         if (ret)
835                 goto failed_removal;
836
837         pfn = start_pfn;
838         expire = jiffies + timeout;
839         drain = 0;
840         retry_max = 5;
841 repeat:
842         /* start memory hot removal */
843         ret = -EAGAIN;
844         if (time_after(jiffies, expire))
845                 goto failed_removal;
846         ret = -EINTR;
847         if (signal_pending(current))
848                 goto failed_removal;
849         ret = 0;
850         if (drain) {
851                 lru_add_drain_all();
852                 cond_resched();
853                 drain_all_pages();
854         }
855
856         pfn = scan_lru_pages(start_pfn, end_pfn);
857         if (pfn) { /* We have page on LRU */
858                 ret = do_migrate_range(pfn, end_pfn);
859                 if (!ret) {
860                         drain = 1;
861                         goto repeat;
862                 } else {
863                         if (ret < 0)
864                                 if (--retry_max == 0)
865                                         goto failed_removal;
866                         yield();
867                         drain = 1;
868                         goto repeat;
869                 }
870         }
871         /* drain all zone's lru pagevec, this is asyncronous... */
872         lru_add_drain_all();
873         yield();
874         /* drain pcp pages , this is synchrouns. */
875         drain_all_pages();
876         /* check again */
877         offlined_pages = check_pages_isolated(start_pfn, end_pfn);
878         if (offlined_pages < 0) {
879                 ret = -EBUSY;
880                 goto failed_removal;
881         }
882         printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
883         /* Ok, all of our target is islaoted.
884            We cannot do rollback at this point. */
885         offline_isolated_pages(start_pfn, end_pfn);
886         /* reset pagetype flags and makes migrate type to be MOVABLE */
887         undo_isolate_page_range(start_pfn, end_pfn);
888         /* removal success */
889         zone->present_pages -= offlined_pages;
890         zone->zone_pgdat->node_present_pages -= offlined_pages;
891         totalram_pages -= offlined_pages;
892
893         setup_per_zone_wmarks();
894         calculate_zone_inactive_ratio(zone);
895         if (!node_present_pages(node)) {
896                 node_clear_state(node, N_HIGH_MEMORY);
897                 kswapd_stop(node);
898         }
899
900         vm_total_pages = nr_free_pagecache_pages();
901         writeback_set_ratelimit();
902
903         memory_notify(MEM_OFFLINE, &arg);
904         unlock_memory_hotplug();
905         return 0;
906
907 failed_removal:
908         printk(KERN_INFO "memory offlining %lx to %lx failed\n",
909                 start_pfn, end_pfn);
910         memory_notify(MEM_CANCEL_OFFLINE, &arg);
911         /* pushback to free area */
912         undo_isolate_page_range(start_pfn, end_pfn);
913
914 out:
915         unlock_memory_hotplug();
916         return ret;
917 }
918
919 int remove_memory(u64 start, u64 size)
920 {
921         unsigned long start_pfn, end_pfn;
922
923         start_pfn = PFN_DOWN(start);
924         end_pfn = start_pfn + PFN_DOWN(size);
925         return offline_pages(start_pfn, end_pfn, 120 * HZ);
926 }
927 #else
928 int remove_memory(u64 start, u64 size)
929 {
930         return -EINVAL;
931 }
932 #endif /* CONFIG_MEMORY_HOTREMOVE */
933 EXPORT_SYMBOL_GPL(remove_memory);