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