GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / mm / memory_hotplug.c
blob9ba02d740f0d3dd4be4b28685d1067c9f587b27f
1 /*
2 * linux/mm/memory_hotplug.c
4 * Copyright (C)
5 */
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>
33 #include <asm/tlbflush.h>
35 #include "internal.h"
37 /* add this memory to iomem resource */
38 static struct resource *register_memory_resource(u64 start, u64 size)
40 struct resource *res;
41 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
42 BUG_ON(!res);
44 res->name = "System RAM";
45 res->start = start;
46 res->end = start + size - 1;
47 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
48 if (request_resource(&iomem_resource, res) < 0) {
49 printk("System RAM resource %llx - %llx cannot be added\n",
50 (unsigned long long)res->start, (unsigned long long)res->end);
51 kfree(res);
52 res = NULL;
54 return res;
57 static void release_memory_resource(struct resource *res)
59 if (!res)
60 return;
61 release_resource(res);
62 kfree(res);
63 return;
66 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
67 #ifndef CONFIG_SPARSEMEM_VMEMMAP
68 static void get_page_bootmem(unsigned long info, struct page *page, int type)
70 atomic_set(&page->_mapcount, type);
71 SetPagePrivate(page);
72 set_page_private(page, info);
73 atomic_inc(&page->_count);
76 /* reference to __meminit __free_pages_bootmem is valid
77 * so use __ref to tell modpost not to generate a warning */
78 void __ref put_page_bootmem(struct page *page)
80 int type;
82 type = atomic_read(&page->_mapcount);
83 BUG_ON(type >= -1);
85 if (atomic_dec_return(&page->_count) == 1) {
86 ClearPagePrivate(page);
87 set_page_private(page, 0);
88 reset_page_mapcount(page);
89 __free_pages_bootmem(page, 0);
94 static void register_page_bootmem_info_section(unsigned long start_pfn)
96 unsigned long *usemap, mapsize, section_nr, i;
97 struct mem_section *ms;
98 struct page *page, *memmap;
100 if (!pfn_valid(start_pfn))
101 return;
103 section_nr = pfn_to_section_nr(start_pfn);
104 ms = __nr_to_section(section_nr);
106 /* Get section's memmap address */
107 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
109 page = virt_to_page(memmap);
110 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
111 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
113 /* remember memmap's page */
114 for (i = 0; i < mapsize; i++, page++)
115 get_page_bootmem(section_nr, page, SECTION_INFO);
117 usemap = __nr_to_section(section_nr)->pageblock_flags;
118 page = virt_to_page(usemap);
120 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
122 for (i = 0; i < mapsize; i++, page++)
123 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
127 void register_page_bootmem_info_node(struct pglist_data *pgdat)
129 unsigned long i, pfn, end_pfn, nr_pages;
130 int node = pgdat->node_id;
131 struct page *page;
132 struct zone *zone;
134 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
135 page = virt_to_page(pgdat);
137 for (i = 0; i < nr_pages; i++, page++)
138 get_page_bootmem(node, page, NODE_INFO);
140 zone = &pgdat->node_zones[0];
141 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
142 if (zone->wait_table) {
143 nr_pages = zone->wait_table_hash_nr_entries
144 * sizeof(wait_queue_head_t);
145 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
146 page = virt_to_page(zone->wait_table);
148 for (i = 0; i < nr_pages; i++, page++)
149 get_page_bootmem(node, page, NODE_INFO);
153 pfn = pgdat->node_start_pfn;
154 end_pfn = pfn + pgdat->node_spanned_pages;
156 /* register_section info */
157 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
158 register_page_bootmem_info_section(pfn);
161 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
163 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
164 unsigned long end_pfn)
166 unsigned long old_zone_end_pfn;
168 zone_span_writelock(zone);
170 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
171 if (start_pfn < zone->zone_start_pfn)
172 zone->zone_start_pfn = start_pfn;
174 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
175 zone->zone_start_pfn;
177 zone_span_writeunlock(zone);
180 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
181 unsigned long end_pfn)
183 unsigned long old_pgdat_end_pfn =
184 pgdat->node_start_pfn + pgdat->node_spanned_pages;
186 if (start_pfn < pgdat->node_start_pfn)
187 pgdat->node_start_pfn = start_pfn;
189 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
190 pgdat->node_start_pfn;
193 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
195 struct pglist_data *pgdat = zone->zone_pgdat;
196 int nr_pages = PAGES_PER_SECTION;
197 int nid = pgdat->node_id;
198 int zone_type;
199 unsigned long flags;
201 zone_type = zone - pgdat->node_zones;
202 if (!zone->wait_table) {
203 int ret;
205 ret = init_currently_empty_zone(zone, phys_start_pfn,
206 nr_pages, MEMMAP_HOTPLUG);
207 if (ret)
208 return ret;
210 pgdat_resize_lock(zone->zone_pgdat, &flags);
211 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
212 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
213 phys_start_pfn + nr_pages);
214 pgdat_resize_unlock(zone->zone_pgdat, &flags);
215 memmap_init_zone(nr_pages, nid, zone_type,
216 phys_start_pfn, MEMMAP_HOTPLUG);
217 return 0;
220 static int __meminit __add_section(int nid, struct zone *zone,
221 unsigned long phys_start_pfn)
223 int nr_pages = PAGES_PER_SECTION;
224 int ret;
226 if (pfn_valid(phys_start_pfn))
227 return -EEXIST;
229 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
231 if (ret < 0)
232 return ret;
234 ret = __add_zone(zone, phys_start_pfn);
236 if (ret < 0)
237 return ret;
239 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
242 #ifdef CONFIG_SPARSEMEM_VMEMMAP
243 static int __remove_section(struct zone *zone, struct mem_section *ms)
245 return -EBUSY;
247 #else
248 static int __remove_section(struct zone *zone, struct mem_section *ms)
250 unsigned long flags;
251 struct pglist_data *pgdat = zone->zone_pgdat;
252 int ret = -EINVAL;
254 if (!valid_section(ms))
255 return ret;
257 ret = unregister_memory_section(ms);
258 if (ret)
259 return ret;
261 pgdat_resize_lock(pgdat, &flags);
262 sparse_remove_one_section(zone, ms);
263 pgdat_resize_unlock(pgdat, &flags);
264 return 0;
266 #endif
269 * Reasonably generic function for adding memory. It is
270 * expected that archs that support memory hotplug will
271 * call this function after deciding the zone to which to
272 * add the new pages.
274 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
275 unsigned long nr_pages)
277 unsigned long i;
278 int err = 0;
279 int start_sec, end_sec;
280 /* during initialize mem_map, align hot-added range to section */
281 start_sec = pfn_to_section_nr(phys_start_pfn);
282 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
284 for (i = start_sec; i <= end_sec; i++) {
285 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
288 * EEXIST is finally dealt with by ioresource collision
289 * check. see add_memory() => register_memory_resource()
290 * Warning will be printed if there is collision.
292 if (err && (err != -EEXIST))
293 break;
294 err = 0;
297 return err;
299 EXPORT_SYMBOL_GPL(__add_pages);
302 * __remove_pages() - remove sections of pages from a zone
303 * @zone: zone from which pages need to be removed
304 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
305 * @nr_pages: number of pages to remove (must be multiple of section size)
307 * Generic helper function to remove section mappings and sysfs entries
308 * for the section of the memory we are removing. Caller needs to make
309 * sure that pages are marked reserved and zones are adjust properly by
310 * calling offline_pages().
312 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
313 unsigned long nr_pages)
315 unsigned long i, ret = 0;
316 int sections_to_remove;
319 * We can only remove entire sections
321 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
322 BUG_ON(nr_pages % PAGES_PER_SECTION);
324 sections_to_remove = nr_pages / PAGES_PER_SECTION;
325 for (i = 0; i < sections_to_remove; i++) {
326 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
327 release_mem_region(pfn << PAGE_SHIFT,
328 PAGES_PER_SECTION << PAGE_SHIFT);
329 ret = __remove_section(zone, __pfn_to_section(pfn));
330 if (ret)
331 break;
333 return ret;
335 EXPORT_SYMBOL_GPL(__remove_pages);
337 void online_page(struct page *page)
339 unsigned long pfn = page_to_pfn(page);
341 totalram_pages++;
342 if (pfn >= num_physpages)
343 num_physpages = pfn + 1;
345 #ifdef CONFIG_HIGHMEM
346 if (PageHighMem(page))
347 totalhigh_pages++;
348 #endif
350 #ifdef CONFIG_FLATMEM
351 max_mapnr = max(page_to_pfn(page), max_mapnr);
352 #endif
354 ClearPageReserved(page);
355 init_page_count(page);
356 __free_page(page);
359 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
360 void *arg)
362 unsigned long i;
363 unsigned long onlined_pages = *(unsigned long *)arg;
364 struct page *page;
365 if (PageReserved(pfn_to_page(start_pfn)))
366 for (i = 0; i < nr_pages; i++) {
367 page = pfn_to_page(start_pfn + i);
368 online_page(page);
369 onlined_pages++;
371 *(unsigned long *)arg = onlined_pages;
372 return 0;
376 int online_pages(unsigned long pfn, unsigned long nr_pages)
378 unsigned long onlined_pages = 0;
379 struct zone *zone;
380 int need_zonelists_rebuild = 0;
381 int nid;
382 int ret;
383 struct memory_notify arg;
385 arg.start_pfn = pfn;
386 arg.nr_pages = nr_pages;
387 arg.status_change_nid = -1;
389 nid = page_to_nid(pfn_to_page(pfn));
390 if (node_present_pages(nid) == 0)
391 arg.status_change_nid = nid;
393 ret = memory_notify(MEM_GOING_ONLINE, &arg);
394 ret = notifier_to_errno(ret);
395 if (ret) {
396 memory_notify(MEM_CANCEL_ONLINE, &arg);
397 return ret;
400 * This doesn't need a lock to do pfn_to_page().
401 * The section can't be removed here because of the
402 * memory_block->state_mutex.
404 zone = page_zone(pfn_to_page(pfn));
406 * If this zone is not populated, then it is not in zonelist.
407 * This means the page allocator ignores this zone.
408 * So, zonelist must be updated after online.
410 mutex_lock(&zonelists_mutex);
411 if (!populated_zone(zone))
412 need_zonelists_rebuild = 1;
414 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
415 online_pages_range);
416 if (ret) {
417 mutex_unlock(&zonelists_mutex);
418 printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
419 nr_pages, pfn);
420 memory_notify(MEM_CANCEL_ONLINE, &arg);
421 return ret;
424 zone->present_pages += onlined_pages;
425 zone->zone_pgdat->node_present_pages += onlined_pages;
426 if (need_zonelists_rebuild)
427 build_all_zonelists(zone);
428 else
429 zone_pcp_update(zone);
431 mutex_unlock(&zonelists_mutex);
432 setup_per_zone_wmarks();
433 calculate_zone_inactive_ratio(zone);
434 if (onlined_pages) {
435 kswapd_run(zone_to_nid(zone));
436 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
439 vm_total_pages = nr_free_pagecache_pages();
441 writeback_set_ratelimit();
443 if (onlined_pages)
444 memory_notify(MEM_ONLINE, &arg);
446 return 0;
448 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
450 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
451 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
453 struct pglist_data *pgdat;
454 unsigned long zones_size[MAX_NR_ZONES] = {0};
455 unsigned long zholes_size[MAX_NR_ZONES] = {0};
456 unsigned long start_pfn = start >> PAGE_SHIFT;
458 pgdat = arch_alloc_nodedata(nid);
459 if (!pgdat)
460 return NULL;
462 arch_refresh_nodedata(nid, pgdat);
464 /* we can use NODE_DATA(nid) from here */
466 /* init node's zones as empty zones, we don't have any present pages.*/
467 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
469 return pgdat;
472 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
474 arch_refresh_nodedata(nid, NULL);
475 arch_free_nodedata(pgdat);
476 return;
481 * called by cpu_up() to online a node without onlined memory.
483 int mem_online_node(int nid)
485 pg_data_t *pgdat;
486 int ret;
488 lock_system_sleep();
489 pgdat = hotadd_new_pgdat(nid, 0);
490 if (pgdat) {
491 ret = -ENOMEM;
492 goto out;
494 node_set_online(nid);
495 ret = register_one_node(nid);
496 BUG_ON(ret);
498 out:
499 unlock_system_sleep();
500 return ret;
503 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
504 int __ref add_memory(int nid, u64 start, u64 size)
506 pg_data_t *pgdat = NULL;
507 int new_pgdat = 0;
508 struct resource *res;
509 int ret;
511 lock_system_sleep();
513 res = register_memory_resource(start, size);
514 ret = -EEXIST;
515 if (!res)
516 goto out;
518 if (!node_online(nid)) {
519 pgdat = hotadd_new_pgdat(nid, start);
520 ret = -ENOMEM;
521 if (!pgdat)
522 goto out;
523 new_pgdat = 1;
526 /* call arch's memory hotadd */
527 ret = arch_add_memory(nid, start, size);
529 if (ret < 0)
530 goto error;
532 /* we online node here. we can't roll back from here. */
533 node_set_online(nid);
535 if (new_pgdat) {
536 ret = register_one_node(nid);
538 * If sysfs file of new node can't create, cpu on the node
539 * can't be hot-added. There is no rollback way now.
540 * So, check by BUG_ON() to catch it reluctantly..
542 BUG_ON(ret);
545 /* create new memmap entry */
546 firmware_map_add_hotplug(start, start + size, "System RAM");
548 goto out;
550 error:
551 /* rollback pgdat allocation and others */
552 if (new_pgdat)
553 rollback_node_hotadd(nid, pgdat);
554 if (res)
555 release_memory_resource(res);
557 out:
558 unlock_system_sleep();
559 return ret;
561 EXPORT_SYMBOL_GPL(add_memory);
563 #ifdef CONFIG_MEMORY_HOTREMOVE
565 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
566 * set and the size of the free page is given by page_order(). Using this,
567 * the function determines if the pageblock contains only free pages.
568 * Due to buddy contraints, a free page at least the size of a pageblock will
569 * be located at the start of the pageblock
571 static inline int pageblock_free(struct page *page)
573 return PageBuddy(page) && page_order(page) >= pageblock_order;
576 /* Return the start of the next active pageblock after a given page */
577 static struct page *next_active_pageblock(struct page *page)
579 /* Ensure the starting page is pageblock-aligned */
580 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
582 /* If the entire pageblock is free, move to the end of free page */
583 if (pageblock_free(page)) {
584 int order;
585 /* be careful. we don't have locks, page_order can be changed.*/
586 order = page_order(page);
587 if ((order < MAX_ORDER) && (order >= pageblock_order))
588 return page + (1 << order);
591 return page + pageblock_nr_pages;
594 /* Checks if this range of memory is likely to be hot-removable. */
595 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
597 int type;
598 struct page *page = pfn_to_page(start_pfn);
599 struct page *end_page = page + nr_pages;
601 /* Check the starting page of each pageblock within the range */
602 for (; page < end_page; page = next_active_pageblock(page)) {
603 type = get_pageblock_migratetype(page);
606 * A pageblock containing MOVABLE or free pages is considered
607 * removable
609 if (type != MIGRATE_MOVABLE && !pageblock_free(page))
610 return 0;
613 * A pageblock starting with a PageReserved page is not
614 * considered removable.
616 if (PageReserved(page))
617 return 0;
620 /* All pageblocks in the memory block are likely to be hot-removable */
621 return 1;
625 * Confirm all pages in a range [start, end) is belongs to the same zone.
627 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
629 unsigned long pfn;
630 struct zone *zone = NULL;
631 struct page *page;
632 int i;
633 for (pfn = start_pfn;
634 pfn < end_pfn;
635 pfn += MAX_ORDER_NR_PAGES) {
636 i = 0;
637 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
638 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
639 i++;
640 if (i == MAX_ORDER_NR_PAGES)
641 continue;
642 page = pfn_to_page(pfn + i);
643 if (zone && page_zone(page) != zone)
644 return 0;
645 zone = page_zone(page);
647 return 1;
651 * Scanning pfn is much easier than scanning lru list.
652 * Scan pfn from start to end and Find LRU page.
654 unsigned long scan_lru_pages(unsigned long start, unsigned long end)
656 unsigned long pfn;
657 struct page *page;
658 for (pfn = start; pfn < end; pfn++) {
659 if (pfn_valid(pfn)) {
660 page = pfn_to_page(pfn);
661 if (PageLRU(page))
662 return pfn;
665 return 0;
668 static struct page *
669 hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
671 /* This should be improooooved!! */
672 return alloc_page(GFP_HIGHUSER_MOVABLE);
675 #define NR_OFFLINE_AT_ONCE_PAGES (256)
676 static int
677 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
679 unsigned long pfn;
680 struct page *page;
681 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
682 int not_managed = 0;
683 int ret = 0;
684 LIST_HEAD(source);
686 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
687 if (!pfn_valid(pfn))
688 continue;
689 page = pfn_to_page(pfn);
690 if (!page_count(page))
691 continue;
693 * We can skip free pages. And we can only deal with pages on
694 * LRU.
696 ret = isolate_lru_page(page);
697 if (!ret) { /* Success */
698 list_add_tail(&page->lru, &source);
699 move_pages--;
700 inc_zone_page_state(page, NR_ISOLATED_ANON +
701 page_is_file_cache(page));
703 } else {
704 /* Becasue we don't have big zone->lock. we should
705 check this again here. */
706 if (page_count(page))
707 not_managed++;
708 #ifdef CONFIG_DEBUG_VM
709 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
710 pfn);
711 dump_page(page);
712 #endif
715 ret = -EBUSY;
716 if (not_managed) {
717 if (!list_empty(&source))
718 putback_lru_pages(&source);
719 goto out;
721 ret = 0;
722 if (list_empty(&source))
723 goto out;
724 /* this function returns # of failed pages */
725 ret = migrate_pages(&source, hotremove_migrate_alloc, 0, 1);
727 out:
728 return ret;
732 * remove from free_area[] and mark all as Reserved.
734 static int
735 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
736 void *data)
738 __offline_isolated_pages(start, start + nr_pages);
739 return 0;
742 static void
743 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
745 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
746 offline_isolated_pages_cb);
750 * Check all pages in range, recoreded as memory resource, are isolated.
752 static int
753 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
754 void *data)
756 int ret;
757 long offlined = *(long *)data;
758 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
759 offlined = nr_pages;
760 if (!ret)
761 *(long *)data += offlined;
762 return ret;
765 static long
766 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
768 long offlined = 0;
769 int ret;
771 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
772 check_pages_isolated_cb);
773 if (ret < 0)
774 offlined = (long)ret;
775 return offlined;
778 static int offline_pages(unsigned long start_pfn,
779 unsigned long end_pfn, unsigned long timeout)
781 unsigned long pfn, nr_pages, expire;
782 long offlined_pages;
783 int ret, drain, retry_max, node;
784 struct zone *zone;
785 struct memory_notify arg;
787 BUG_ON(start_pfn >= end_pfn);
788 /* at least, alignment against pageblock is necessary */
789 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
790 return -EINVAL;
791 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
792 return -EINVAL;
793 /* This makes hotplug much easier...and readable.
794 we assume this for now. .*/
795 if (!test_pages_in_a_zone(start_pfn, end_pfn))
796 return -EINVAL;
798 lock_system_sleep();
800 zone = page_zone(pfn_to_page(start_pfn));
801 node = zone_to_nid(zone);
802 nr_pages = end_pfn - start_pfn;
804 /* set above range as isolated */
805 ret = start_isolate_page_range(start_pfn, end_pfn);
806 if (ret)
807 goto out;
809 arg.start_pfn = start_pfn;
810 arg.nr_pages = nr_pages;
811 arg.status_change_nid = -1;
812 if (nr_pages >= node_present_pages(node))
813 arg.status_change_nid = node;
815 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
816 ret = notifier_to_errno(ret);
817 if (ret)
818 goto failed_removal;
820 pfn = start_pfn;
821 expire = jiffies + timeout;
822 drain = 0;
823 retry_max = 5;
824 repeat:
825 /* start memory hot removal */
826 ret = -EAGAIN;
827 if (time_after(jiffies, expire))
828 goto failed_removal;
829 ret = -EINTR;
830 if (signal_pending(current))
831 goto failed_removal;
832 ret = 0;
833 if (drain) {
834 lru_add_drain_all();
835 flush_scheduled_work();
836 cond_resched();
837 drain_all_pages();
840 pfn = scan_lru_pages(start_pfn, end_pfn);
841 if (pfn) { /* We have page on LRU */
842 ret = do_migrate_range(pfn, end_pfn);
843 if (!ret) {
844 drain = 1;
845 goto repeat;
846 } else {
847 if (ret < 0)
848 if (--retry_max == 0)
849 goto failed_removal;
850 yield();
851 drain = 1;
852 goto repeat;
855 /* drain all zone's lru pagevec, this is asyncronous... */
856 lru_add_drain_all();
857 flush_scheduled_work();
858 yield();
859 /* drain pcp pages , this is synchrouns. */
860 drain_all_pages();
861 /* check again */
862 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
863 if (offlined_pages < 0) {
864 ret = -EBUSY;
865 goto failed_removal;
867 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
868 /* Ok, all of our target is islaoted.
869 We cannot do rollback at this point. */
870 offline_isolated_pages(start_pfn, end_pfn);
871 /* reset pagetype flags and makes migrate type to be MOVABLE */
872 undo_isolate_page_range(start_pfn, end_pfn);
873 /* removal success */
874 zone->present_pages -= offlined_pages;
875 zone->zone_pgdat->node_present_pages -= offlined_pages;
876 totalram_pages -= offlined_pages;
878 setup_per_zone_wmarks();
879 calculate_zone_inactive_ratio(zone);
880 if (!node_present_pages(node)) {
881 node_clear_state(node, N_HIGH_MEMORY);
882 kswapd_stop(node);
885 vm_total_pages = nr_free_pagecache_pages();
886 writeback_set_ratelimit();
888 memory_notify(MEM_OFFLINE, &arg);
889 unlock_system_sleep();
890 return 0;
892 failed_removal:
893 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
894 start_pfn, end_pfn);
895 memory_notify(MEM_CANCEL_OFFLINE, &arg);
896 /* pushback to free area */
897 undo_isolate_page_range(start_pfn, end_pfn);
899 out:
900 unlock_system_sleep();
901 return ret;
904 int remove_memory(u64 start, u64 size)
906 unsigned long start_pfn, end_pfn;
908 start_pfn = PFN_DOWN(start);
909 end_pfn = start_pfn + PFN_DOWN(size);
910 return offline_pages(start_pfn, end_pfn, 120 * HZ);
912 #else
913 int remove_memory(u64 start, u64 size)
915 return -EINVAL;
917 #endif /* CONFIG_MEMORY_HOTREMOVE */
918 EXPORT_SYMBOL_GPL(remove_memory);