2 * linux/mm/memory_hotplug.c
7 #include <linux/stddef.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/cpuset.h>
26 #include <linux/delay.h>
27 #include <linux/migrate.h>
28 #include <linux/page-isolation.h>
30 #include <asm/tlbflush.h>
32 /* add this memory to iomem resource */
33 static struct resource
*register_memory_resource(u64 start
, u64 size
)
36 res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
39 res
->name
= "System RAM";
41 res
->end
= start
+ size
- 1;
42 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
43 if (request_resource(&iomem_resource
, res
) < 0) {
44 printk("System RAM resource %llx - %llx cannot be added\n",
45 (unsigned long long)res
->start
, (unsigned long long)res
->end
);
52 static void release_memory_resource(struct resource
*res
)
56 release_resource(res
);
62 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
63 static int __add_zone(struct zone
*zone
, unsigned long phys_start_pfn
)
65 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
66 int nr_pages
= PAGES_PER_SECTION
;
67 int nid
= pgdat
->node_id
;
70 zone_type
= zone
- pgdat
->node_zones
;
71 if (!zone
->wait_table
) {
73 ret
= init_currently_empty_zone(zone
, phys_start_pfn
,
74 nr_pages
, MEMMAP_HOTPLUG
);
78 memmap_init_zone(nr_pages
, nid
, zone_type
,
79 phys_start_pfn
, MEMMAP_HOTPLUG
);
83 static int __add_section(struct zone
*zone
, unsigned long phys_start_pfn
)
85 int nr_pages
= PAGES_PER_SECTION
;
88 if (pfn_valid(phys_start_pfn
))
91 ret
= sparse_add_one_section(zone
, phys_start_pfn
, nr_pages
);
96 ret
= __add_zone(zone
, phys_start_pfn
);
101 return register_new_memory(__pfn_to_section(phys_start_pfn
));
104 static int __remove_section(struct zone
*zone
, struct mem_section
*ms
)
107 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
110 if (!valid_section(ms
))
113 ret
= unregister_memory_section(ms
);
117 pgdat_resize_lock(pgdat
, &flags
);
118 sparse_remove_one_section(zone
, ms
);
119 pgdat_resize_unlock(pgdat
, &flags
);
124 * Reasonably generic function for adding memory. It is
125 * expected that archs that support memory hotplug will
126 * call this function after deciding the zone to which to
129 int __add_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
130 unsigned long nr_pages
)
134 int start_sec
, end_sec
;
135 /* during initialize mem_map, align hot-added range to section */
136 start_sec
= pfn_to_section_nr(phys_start_pfn
);
137 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
139 for (i
= start_sec
; i
<= end_sec
; i
++) {
140 err
= __add_section(zone
, i
<< PFN_SECTION_SHIFT
);
143 * EEXIST is finally dealt with by ioresource collision
144 * check. see add_memory() => register_memory_resource()
145 * Warning will be printed if there is collision.
147 if (err
&& (err
!= -EEXIST
))
154 EXPORT_SYMBOL_GPL(__add_pages
);
157 * __remove_pages() - remove sections of pages from a zone
158 * @zone: zone from which pages need to be removed
159 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
160 * @nr_pages: number of pages to remove (must be multiple of section size)
162 * Generic helper function to remove section mappings and sysfs entries
163 * for the section of the memory we are removing. Caller needs to make
164 * sure that pages are marked reserved and zones are adjust properly by
165 * calling offline_pages().
167 int __remove_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
168 unsigned long nr_pages
)
170 unsigned long i
, ret
= 0;
171 int sections_to_remove
;
174 * We can only remove entire sections
176 BUG_ON(phys_start_pfn
& ~PAGE_SECTION_MASK
);
177 BUG_ON(nr_pages
% PAGES_PER_SECTION
);
179 release_mem_region(phys_start_pfn
<< PAGE_SHIFT
, nr_pages
* PAGE_SIZE
);
181 sections_to_remove
= nr_pages
/ PAGES_PER_SECTION
;
182 for (i
= 0; i
< sections_to_remove
; i
++) {
183 unsigned long pfn
= phys_start_pfn
+ i
*PAGES_PER_SECTION
;
184 ret
= __remove_section(zone
, __pfn_to_section(pfn
));
190 EXPORT_SYMBOL_GPL(__remove_pages
);
192 static void grow_zone_span(struct zone
*zone
,
193 unsigned long start_pfn
, unsigned long end_pfn
)
195 unsigned long old_zone_end_pfn
;
197 zone_span_writelock(zone
);
199 old_zone_end_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
200 if (start_pfn
< zone
->zone_start_pfn
)
201 zone
->zone_start_pfn
= start_pfn
;
203 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
204 zone
->zone_start_pfn
;
206 zone_span_writeunlock(zone
);
209 static void grow_pgdat_span(struct pglist_data
*pgdat
,
210 unsigned long start_pfn
, unsigned long end_pfn
)
212 unsigned long old_pgdat_end_pfn
=
213 pgdat
->node_start_pfn
+ pgdat
->node_spanned_pages
;
215 if (start_pfn
< pgdat
->node_start_pfn
)
216 pgdat
->node_start_pfn
= start_pfn
;
218 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
219 pgdat
->node_start_pfn
;
222 void online_page(struct page
*page
)
227 #ifdef CONFIG_HIGHMEM
228 if (PageHighMem(page
))
232 #ifdef CONFIG_FLATMEM
233 max_mapnr
= max(page_to_pfn(page
), max_mapnr
);
236 ClearPageReserved(page
);
237 init_page_count(page
);
241 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
245 unsigned long onlined_pages
= *(unsigned long *)arg
;
247 if (PageReserved(pfn_to_page(start_pfn
)))
248 for (i
= 0; i
< nr_pages
; i
++) {
249 page
= pfn_to_page(start_pfn
+ i
);
253 *(unsigned long *)arg
= onlined_pages
;
258 int online_pages(unsigned long pfn
, unsigned long nr_pages
)
261 unsigned long onlined_pages
= 0;
263 int need_zonelists_rebuild
= 0;
266 struct memory_notify arg
;
269 arg
.nr_pages
= nr_pages
;
270 arg
.status_change_nid
= -1;
272 nid
= page_to_nid(pfn_to_page(pfn
));
273 if (node_present_pages(nid
) == 0)
274 arg
.status_change_nid
= nid
;
276 ret
= memory_notify(MEM_GOING_ONLINE
, &arg
);
277 ret
= notifier_to_errno(ret
);
279 memory_notify(MEM_CANCEL_ONLINE
, &arg
);
283 * This doesn't need a lock to do pfn_to_page().
284 * The section can't be removed here because of the
285 * memory_block->state_mutex.
287 zone
= page_zone(pfn_to_page(pfn
));
288 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
289 grow_zone_span(zone
, pfn
, pfn
+ nr_pages
);
290 grow_pgdat_span(zone
->zone_pgdat
, pfn
, pfn
+ nr_pages
);
291 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
294 * If this zone is not populated, then it is not in zonelist.
295 * This means the page allocator ignores this zone.
296 * So, zonelist must be updated after online.
298 if (!populated_zone(zone
))
299 need_zonelists_rebuild
= 1;
301 walk_memory_resource(pfn
, nr_pages
, &onlined_pages
,
303 zone
->present_pages
+= onlined_pages
;
304 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
306 setup_per_zone_pages_min();
308 kswapd_run(zone_to_nid(zone
));
309 node_set_state(zone_to_nid(zone
), N_HIGH_MEMORY
);
312 if (need_zonelists_rebuild
)
313 build_all_zonelists();
314 vm_total_pages
= nr_free_pagecache_pages();
315 writeback_set_ratelimit();
318 memory_notify(MEM_ONLINE
, &arg
);
322 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
324 static pg_data_t
*hotadd_new_pgdat(int nid
, u64 start
)
326 struct pglist_data
*pgdat
;
327 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
328 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
329 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
331 pgdat
= arch_alloc_nodedata(nid
);
335 arch_refresh_nodedata(nid
, pgdat
);
337 /* we can use NODE_DATA(nid) from here */
339 /* init node's zones as empty zones, we don't have any present pages.*/
340 free_area_init_node(nid
, pgdat
, zones_size
, start_pfn
, zholes_size
);
345 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
347 arch_refresh_nodedata(nid
, NULL
);
348 arch_free_nodedata(pgdat
);
353 int add_memory(int nid
, u64 start
, u64 size
)
355 pg_data_t
*pgdat
= NULL
;
357 struct resource
*res
;
360 res
= register_memory_resource(start
, size
);
364 if (!node_online(nid
)) {
365 pgdat
= hotadd_new_pgdat(nid
, start
);
371 /* call arch's memory hotadd */
372 ret
= arch_add_memory(nid
, start
, size
);
377 /* we online node here. we can't roll back from here. */
378 node_set_online(nid
);
380 cpuset_track_online_nodes();
383 ret
= register_one_node(nid
);
385 * If sysfs file of new node can't create, cpu on the node
386 * can't be hot-added. There is no rollback way now.
387 * So, check by BUG_ON() to catch it reluctantly..
394 /* rollback pgdat allocation and others */
396 rollback_node_hotadd(nid
, pgdat
);
398 release_memory_resource(res
);
402 EXPORT_SYMBOL_GPL(add_memory
);
404 #ifdef CONFIG_MEMORY_HOTREMOVE
406 * Confirm all pages in a range [start, end) is belongs to the same zone.
408 static int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
)
411 struct zone
*zone
= NULL
;
414 for (pfn
= start_pfn
;
416 pfn
+= MAX_ORDER_NR_PAGES
) {
418 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
419 while ((i
< MAX_ORDER_NR_PAGES
) && !pfn_valid_within(pfn
+ i
))
421 if (i
== MAX_ORDER_NR_PAGES
)
423 page
= pfn_to_page(pfn
+ i
);
424 if (zone
&& page_zone(page
) != zone
)
426 zone
= page_zone(page
);
432 * Scanning pfn is much easier than scanning lru list.
433 * Scan pfn from start to end and Find LRU page.
435 int scan_lru_pages(unsigned long start
, unsigned long end
)
439 for (pfn
= start
; pfn
< end
; pfn
++) {
440 if (pfn_valid(pfn
)) {
441 page
= pfn_to_page(pfn
);
450 hotremove_migrate_alloc(struct page
*page
,
451 unsigned long private,
454 /* This should be improoooooved!! */
455 return alloc_page(GFP_HIGHUSER_PAGECACHE
);
459 #define NR_OFFLINE_AT_ONCE_PAGES (256)
461 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
465 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
470 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
473 page
= pfn_to_page(pfn
);
474 if (!page_count(page
))
477 * We can skip free pages. And we can only deal with pages on
480 ret
= isolate_lru_page(page
, &source
);
481 if (!ret
) { /* Success */
484 /* Becasue we don't have big zone->lock. we should
485 check this again here. */
486 if (page_count(page
))
488 #ifdef CONFIG_DEBUG_VM
489 printk(KERN_INFO
"removing from LRU failed"
491 pfn
, page_count(page
), page
->flags
);
497 if (!list_empty(&source
))
498 putback_lru_pages(&source
);
502 if (list_empty(&source
))
504 /* this function returns # of failed pages */
505 ret
= migrate_pages(&source
, hotremove_migrate_alloc
, 0);
512 * remove from free_area[] and mark all as Reserved.
515 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
518 __offline_isolated_pages(start
, start
+ nr_pages
);
523 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
525 walk_memory_resource(start_pfn
, end_pfn
- start_pfn
, NULL
,
526 offline_isolated_pages_cb
);
530 * Check all pages in range, recoreded as memory resource, are isolated.
533 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
537 long offlined
= *(long *)data
;
538 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
);
541 *(long *)data
+= offlined
;
546 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
551 ret
= walk_memory_resource(start_pfn
, end_pfn
- start_pfn
, &offlined
,
552 check_pages_isolated_cb
);
554 offlined
= (long)ret
;
558 int offline_pages(unsigned long start_pfn
,
559 unsigned long end_pfn
, unsigned long timeout
)
561 unsigned long pfn
, nr_pages
, expire
;
563 int ret
, drain
, retry_max
, node
;
565 struct memory_notify arg
;
567 BUG_ON(start_pfn
>= end_pfn
);
568 /* at least, alignment against pageblock is necessary */
569 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
571 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
573 /* This makes hotplug much easier...and readable.
574 we assume this for now. .*/
575 if (!test_pages_in_a_zone(start_pfn
, end_pfn
))
578 zone
= page_zone(pfn_to_page(start_pfn
));
579 node
= zone_to_nid(zone
);
580 nr_pages
= end_pfn
- start_pfn
;
582 /* set above range as isolated */
583 ret
= start_isolate_page_range(start_pfn
, end_pfn
);
587 arg
.start_pfn
= start_pfn
;
588 arg
.nr_pages
= nr_pages
;
589 arg
.status_change_nid
= -1;
590 if (nr_pages
>= node_present_pages(node
))
591 arg
.status_change_nid
= node
;
593 ret
= memory_notify(MEM_GOING_OFFLINE
, &arg
);
594 ret
= notifier_to_errno(ret
);
599 expire
= jiffies
+ timeout
;
603 /* start memory hot removal */
605 if (time_after(jiffies
, expire
))
608 if (signal_pending(current
))
613 flush_scheduled_work();
618 pfn
= scan_lru_pages(start_pfn
, end_pfn
);
619 if (pfn
) { /* We have page on LRU */
620 ret
= do_migrate_range(pfn
, end_pfn
);
626 if (--retry_max
== 0)
633 /* drain all zone's lru pagevec, this is asyncronous... */
635 flush_scheduled_work();
637 /* drain pcp pages , this is synchrouns. */
640 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
641 if (offlined_pages
< 0) {
645 printk(KERN_INFO
"Offlined Pages %ld\n", offlined_pages
);
646 /* Ok, all of our target is islaoted.
647 We cannot do rollback at this point. */
648 offline_isolated_pages(start_pfn
, end_pfn
);
649 /* reset pagetype flags and makes migrate type to be MOVABLE */
650 undo_isolate_page_range(start_pfn
, end_pfn
);
651 /* removal success */
652 zone
->present_pages
-= offlined_pages
;
653 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
654 totalram_pages
-= offlined_pages
;
655 num_physpages
-= offlined_pages
;
657 vm_total_pages
= nr_free_pagecache_pages();
658 writeback_set_ratelimit();
660 memory_notify(MEM_OFFLINE
, &arg
);
664 printk(KERN_INFO
"memory offlining %lx to %lx failed\n",
666 memory_notify(MEM_CANCEL_OFFLINE
, &arg
);
667 /* pushback to free area */
668 undo_isolate_page_range(start_pfn
, end_pfn
);
673 int remove_memory(u64 start
, u64 size
)
677 EXPORT_SYMBOL_GPL(remove_memory
);
678 #endif /* CONFIG_MEMORY_HOTREMOVE */