]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memory_hotplug.c
hugetlb: prevent deadlock in __unmap_hugepage_range() when alloc_huge_page() fails
[net-next-2.6.git] / mm / memory_hotplug.c
CommitLineData
3947be19
DH
1/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
3947be19
DH
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>
2d1d43f6 16#include <linux/writeback.h>
3947be19
DH
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>
0a547039 24#include <linux/ioport.h>
0c0e6195
KH
25#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
71088785 28#include <linux/pfn.h>
6ad696d2 29#include <linux/suspend.h>
6d9c285a 30#include <linux/mm_inline.h>
3947be19
DH
31
32#include <asm/tlbflush.h>
33
1e5ad9a3
AB
34#include "internal.h"
35
45e0b78b
KM
36/* add this memory to iomem resource */
37static 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;
887c3cb1 46 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
45e0b78b
KM
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
56static void release_memory_resource(struct resource *res)
57{
58 if (!res)
59 return;
60 release_resource(res);
61 kfree(res);
62 return;
63}
64
53947027 65#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
04753278 66#ifndef CONFIG_SPARSEMEM_VMEMMAP
af370fb8 67static void get_page_bootmem(unsigned long info, struct page *page, int type)
04753278 68{
af370fb8 69 atomic_set(&page->_mapcount, type);
04753278
YG
70 SetPagePrivate(page);
71 set_page_private(page, info);
72 atomic_inc(&page->_count);
73}
74
75void put_page_bootmem(struct page *page)
76{
af370fb8 77 int type;
04753278 78
af370fb8
YG
79 type = atomic_read(&page->_mapcount);
80 BUG_ON(type >= -1);
04753278
YG
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
d92bc318 91static void register_page_bootmem_info_section(unsigned long start_pfn)
04753278
YG
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++)
af370fb8 124 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
04753278
YG
125
126}
127
128void 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
76cdd58e
HC
164static 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
181static 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
31168481 194static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
3947be19
DH
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;
76cdd58e 200 unsigned long flags;
3947be19
DH
201
202 zone_type = zone - pgdat->node_zones;
76cdd58e
HC
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);
a2f3aa02
DH
216 memmap_init_zone(nr_pages, nid, zone_type,
217 phys_start_pfn, MEMMAP_HOTPLUG);
718127cc 218 return 0;
3947be19
DH
219}
220
c04fc586
GH
221static int __meminit __add_section(int nid, struct zone *zone,
222 unsigned long phys_start_pfn)
3947be19 223{
3947be19 224 int nr_pages = PAGES_PER_SECTION;
3947be19
DH
225 int ret;
226
ebd15302
KH
227 if (pfn_valid(phys_start_pfn))
228 return -EEXIST;
229
0b0acbec 230 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
3947be19
DH
231
232 if (ret < 0)
233 return ret;
234
718127cc
YG
235 ret = __add_zone(zone, phys_start_pfn);
236
237 if (ret < 0)
238 return ret;
239
c04fc586 240 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
3947be19
DH
241}
242
0c0a4a51
YG
243#ifdef CONFIG_SPARSEMEM_VMEMMAP
244static 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
ea01ea93
BP
253static 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}
0c0a4a51 271#endif
ea01ea93 272
3947be19
DH
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 */
c04fc586
GH
279int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
280 unsigned long nr_pages)
3947be19
DH
281{
282 unsigned long i;
283 int err = 0;
6f712711
KH
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);
3947be19 288
6f712711 289 for (i = start_sec; i <= end_sec; i++) {
c04fc586 290 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
3947be19 291
6f712711 292 /*
183ff22b 293 * EEXIST is finally dealt with by ioresource collision
6f712711
KH
294 * check. see add_memory() => register_memory_resource()
295 * Warning will be printed if there is collision.
bed120c6
JS
296 */
297 if (err && (err != -EEXIST))
3947be19 298 break;
6f712711 299 err = 0;
3947be19
DH
300 }
301
302 return err;
303}
bed120c6 304EXPORT_SYMBOL_GPL(__add_pages);
3947be19 305
ea01ea93
BP
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 */
317int __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
ea01ea93
BP
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;
de7f0cba
NF
332 release_mem_region(pfn << PAGE_SHIFT,
333 PAGES_PER_SECTION << PAGE_SHIFT);
ea01ea93
BP
334 ret = __remove_section(zone, __pfn_to_section(pfn));
335 if (ret)
336 break;
337 }
338 return ret;
339}
340EXPORT_SYMBOL_GPL(__remove_pages);
341
180c06ef
JF
342void online_page(struct page *page)
343{
4738e1b9
JB
344 unsigned long pfn = page_to_pfn(page);
345
180c06ef 346 totalram_pages++;
4738e1b9
JB
347 if (pfn >= num_physpages)
348 num_physpages = pfn + 1;
180c06ef
JF
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
75884fb1
KH
364static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
365 void *arg)
3947be19
DH
366{
367 unsigned long i;
75884fb1
KH
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
381int online_pages(unsigned long pfn, unsigned long nr_pages)
382{
3947be19
DH
383 unsigned long onlined_pages = 0;
384 struct zone *zone;
6811378e 385 int need_zonelists_rebuild = 0;
7b78d335
YG
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;
3947be19 397
7b78d335
YG
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 }
3947be19
DH
404 /*
405 * This doesn't need a lock to do pfn_to_page().
406 * The section can't be removed here because of the
da19cbcf 407 * memory_block->state_mutex.
3947be19
DH
408 */
409 zone = page_zone(pfn_to_page(pfn));
6811378e
YG
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
908eedc6 418 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
75884fb1 419 online_pages_range);
fd8a4221
GL
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
3947be19 427 zone->present_pages += onlined_pages;
f2937be5 428 zone->zone_pgdat->node_present_pages += onlined_pages;
3947be19 429
112067f0 430 zone_pcp_update(zone);
bc75d33f 431 setup_per_zone_wmarks();
bce7394a 432 calculate_zone_inactive_ratio(zone);
7ea1530a
CL
433 if (onlined_pages) {
434 kswapd_run(zone_to_nid(zone));
435 node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
436 }
61b13993 437
6811378e
YG
438 if (need_zonelists_rebuild)
439 build_all_zonelists();
2f7f24ec
KL
440 else
441 vm_total_pages = nr_free_pagecache_pages();
442
2d1d43f6 443 writeback_set_ratelimit();
7b78d335
YG
444
445 if (onlined_pages)
446 memory_notify(MEM_ONLINE, &arg);
447
3947be19
DH
448 return 0;
449}
53947027 450#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
bc02af93 451
e1319331
HS
452/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
453static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9af3c2de
YG
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.*/
9109fb7b 469 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
9af3c2de
YG
470
471 return pgdat;
472}
473
474static 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
0a547039 481
31168481
AV
482/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
483int __ref add_memory(int nid, u64 start, u64 size)
bc02af93 484{
9af3c2de
YG
485 pg_data_t *pgdat = NULL;
486 int new_pgdat = 0;
ebd15302 487 struct resource *res;
bc02af93
YG
488 int ret;
489
6ad696d2
AK
490 lock_system_sleep();
491
ebd15302 492 res = register_memory_resource(start, size);
6ad696d2 493 ret = -EEXIST;
ebd15302 494 if (!res)
6ad696d2 495 goto out;
ebd15302 496
9af3c2de
YG
497 if (!node_online(nid)) {
498 pgdat = hotadd_new_pgdat(nid, start);
6ad696d2 499 ret = -ENOMEM;
9af3c2de 500 if (!pgdat)
6ad696d2 501 goto out;
9af3c2de 502 new_pgdat = 1;
9af3c2de
YG
503 }
504
bc02af93
YG
505 /* call arch's memory hotadd */
506 ret = arch_add_memory(nid, start, size);
507
9af3c2de
YG
508 if (ret < 0)
509 goto error;
510
0fc44159 511 /* we online node here. we can't roll back from here. */
9af3c2de
YG
512 node_set_online(nid);
513
0fc44159
YG
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
6ad696d2
AK
524 goto out;
525
9af3c2de
YG
526error:
527 /* rollback pgdat allocation and others */
528 if (new_pgdat)
529 rollback_node_hotadd(nid, pgdat);
ebd15302
KH
530 if (res)
531 release_memory_resource(res);
9af3c2de 532
6ad696d2
AK
533out:
534 unlock_system_sleep();
bc02af93
YG
535 return ret;
536}
537EXPORT_SYMBOL_GPL(add_memory);
0c0e6195
KH
538
539#ifdef CONFIG_MEMORY_HOTREMOVE
5c755e9f
BP
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 */
547static 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 */
553static 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. */
571int 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
0c0e6195
KH
600/*
601 * Confirm all pages in a range [start, end) is belongs to the same zone.
602 */
603static 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 */
630int 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
644static struct page *
3c1d4378 645hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
0c0e6195 646{
3c1d4378
HD
647 /* This should be improooooved!! */
648 return alloc_page(GFP_HIGHUSER_MOVABLE);
0c0e6195
KH
649}
650
0c0e6195
KH
651#define NR_OFFLINE_AT_ONCE_PAGES (256)
652static int
653do_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 */
62695a84 672 ret = isolate_lru_page(page);
0c0e6195 673 if (!ret) { /* Success */
62695a84 674 list_add_tail(&page->lru, &source);
0c0e6195 675 move_pages--;
6d9c285a
KM
676 inc_zone_page_state(page, NR_ISOLATED_ANON +
677 page_is_file_cache(page));
678
0c0e6195
KH
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 */
62b61f61 701 ret = migrate_pages(&source, hotremove_migrate_alloc, 0, 1);
0c0e6195
KH
702
703out:
704 return ret;
705}
706
707/*
708 * remove from free_area[] and mark all as Reserved.
709 */
710static int
711offline_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
718static void
719offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
720{
908eedc6 721 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
0c0e6195
KH
722 offline_isolated_pages_cb);
723}
724
725/*
726 * Check all pages in range, recoreded as memory resource, are isolated.
727 */
728static int
729check_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
741static long
742check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
743{
744 long offlined = 0;
745 int ret;
746
908eedc6 747 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
0c0e6195
KH
748 check_pages_isolated_cb);
749 if (ret < 0)
750 offlined = (long)ret;
751 return offlined;
752}
753
b4e655a4 754static int offline_pages(unsigned long start_pfn,
0c0e6195
KH
755 unsigned long end_pfn, unsigned long timeout)
756{
757 unsigned long pfn, nr_pages, expire;
758 long offlined_pages;
7b78d335 759 int ret, drain, retry_max, node;
0c0e6195 760 struct zone *zone;
7b78d335 761 struct memory_notify arg;
0c0e6195
KH
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;
7b78d335 773
6ad696d2
AK
774 lock_system_sleep();
775
7b78d335
YG
776 zone = page_zone(pfn_to_page(start_pfn));
777 node = zone_to_nid(zone);
778 nr_pages = end_pfn - start_pfn;
779
0c0e6195
KH
780 /* set above range as isolated */
781 ret = start_isolate_page_range(start_pfn, end_pfn);
782 if (ret)
6ad696d2 783 goto out;
7b78d335
YG
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
0c0e6195
KH
796 pfn = start_pfn;
797 expire = jiffies + timeout;
798 drain = 0;
799 retry_max = 5;
800repeat:
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();
9f8f2172 813 drain_all_pages();
0c0e6195
KH
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. */
9f8f2172 836 drain_all_pages();
0c0e6195
KH
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);
dbc0e4ce
KH
847 /* reset pagetype flags and makes migrate type to be MOVABLE */
848 undo_isolate_page_range(start_pfn, end_pfn);
0c0e6195 849 /* removal success */
0c0e6195
KH
850 zone->present_pages -= offlined_pages;
851 zone->zone_pgdat->node_present_pages -= offlined_pages;
852 totalram_pages -= offlined_pages;
7b78d335 853
bce7394a
MK
854 setup_per_zone_wmarks();
855 calculate_zone_inactive_ratio(zone);
8fe23e05
DR
856 if (!node_present_pages(node)) {
857 node_clear_state(node, N_HIGH_MEMORY);
858 kswapd_stop(node);
859 }
bce7394a 860
0c0e6195
KH
861 vm_total_pages = nr_free_pagecache_pages();
862 writeback_set_ratelimit();
7b78d335
YG
863
864 memory_notify(MEM_OFFLINE, &arg);
6ad696d2 865 unlock_system_sleep();
0c0e6195
KH
866 return 0;
867
868failed_removal:
869 printk(KERN_INFO "memory offlining %lx to %lx failed\n",
870 start_pfn, end_pfn);
7b78d335 871 memory_notify(MEM_CANCEL_OFFLINE, &arg);
0c0e6195
KH
872 /* pushback to free area */
873 undo_isolate_page_range(start_pfn, end_pfn);
7b78d335 874
6ad696d2
AK
875out:
876 unlock_system_sleep();
0c0e6195
KH
877 return ret;
878}
71088785
BP
879
880int 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}
48e94196
KH
888#else
889int remove_memory(u64 start, u64 size)
890{
891 return -EINVAL;
892}
0c0e6195 893#endif /* CONFIG_MEMORY_HOTREMOVE */
71088785 894EXPORT_SYMBOL_GPL(remove_memory);