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
;
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
));
105 * Reasonably generic function for adding memory. It is
106 * expected that archs that support memory hotplug will
107 * call this function after deciding the zone to which to
110 int __add_pages(struct zone
*zone
, unsigned long phys_start_pfn
,
111 unsigned long nr_pages
)
115 int start_sec
, end_sec
;
116 /* during initialize mem_map, align hot-added range to section */
117 start_sec
= pfn_to_section_nr(phys_start_pfn
);
118 end_sec
= pfn_to_section_nr(phys_start_pfn
+ nr_pages
- 1);
120 for (i
= start_sec
; i
<= end_sec
; i
++) {
121 err
= __add_section(zone
, i
<< PFN_SECTION_SHIFT
);
124 * EEXIST is finally dealed with by ioresource collision
125 * check. see add_memory() => register_memory_resource()
126 * Warning will be printed if there is collision.
128 if (err
&& (err
!= -EEXIST
))
135 EXPORT_SYMBOL_GPL(__add_pages
);
137 static void grow_zone_span(struct zone
*zone
,
138 unsigned long start_pfn
, unsigned long end_pfn
)
140 unsigned long old_zone_end_pfn
;
142 zone_span_writelock(zone
);
144 old_zone_end_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
145 if (start_pfn
< zone
->zone_start_pfn
)
146 zone
->zone_start_pfn
= start_pfn
;
148 zone
->spanned_pages
= max(old_zone_end_pfn
, end_pfn
) -
149 zone
->zone_start_pfn
;
151 zone_span_writeunlock(zone
);
154 static void grow_pgdat_span(struct pglist_data
*pgdat
,
155 unsigned long start_pfn
, unsigned long end_pfn
)
157 unsigned long old_pgdat_end_pfn
=
158 pgdat
->node_start_pfn
+ pgdat
->node_spanned_pages
;
160 if (start_pfn
< pgdat
->node_start_pfn
)
161 pgdat
->node_start_pfn
= start_pfn
;
163 pgdat
->node_spanned_pages
= max(old_pgdat_end_pfn
, end_pfn
) -
164 pgdat
->node_start_pfn
;
167 static int online_pages_range(unsigned long start_pfn
, unsigned long nr_pages
,
171 unsigned long onlined_pages
= *(unsigned long *)arg
;
173 if (PageReserved(pfn_to_page(start_pfn
)))
174 for (i
= 0; i
< nr_pages
; i
++) {
175 page
= pfn_to_page(start_pfn
+ i
);
179 *(unsigned long *)arg
= onlined_pages
;
184 int online_pages(unsigned long pfn
, unsigned long nr_pages
)
187 unsigned long onlined_pages
= 0;
189 int need_zonelists_rebuild
= 0;
192 * This doesn't need a lock to do pfn_to_page().
193 * The section can't be removed here because of the
194 * memory_block->state_sem.
196 zone
= page_zone(pfn_to_page(pfn
));
197 pgdat_resize_lock(zone
->zone_pgdat
, &flags
);
198 grow_zone_span(zone
, pfn
, pfn
+ nr_pages
);
199 grow_pgdat_span(zone
->zone_pgdat
, pfn
, pfn
+ nr_pages
);
200 pgdat_resize_unlock(zone
->zone_pgdat
, &flags
);
203 * If this zone is not populated, then it is not in zonelist.
204 * This means the page allocator ignores this zone.
205 * So, zonelist must be updated after online.
207 if (!populated_zone(zone
))
208 need_zonelists_rebuild
= 1;
210 walk_memory_resource(pfn
, nr_pages
, &onlined_pages
,
212 zone
->present_pages
+= onlined_pages
;
213 zone
->zone_pgdat
->node_present_pages
+= onlined_pages
;
215 setup_per_zone_pages_min();
217 kswapd_run(zone_to_nid(zone
));
218 node_set_state(zone_to_nid(zone
), N_HIGH_MEMORY
);
221 if (need_zonelists_rebuild
)
222 build_all_zonelists();
223 vm_total_pages
= nr_free_pagecache_pages();
224 writeback_set_ratelimit();
227 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
229 static pg_data_t
*hotadd_new_pgdat(int nid
, u64 start
)
231 struct pglist_data
*pgdat
;
232 unsigned long zones_size
[MAX_NR_ZONES
] = {0};
233 unsigned long zholes_size
[MAX_NR_ZONES
] = {0};
234 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
236 pgdat
= arch_alloc_nodedata(nid
);
240 arch_refresh_nodedata(nid
, pgdat
);
242 /* we can use NODE_DATA(nid) from here */
244 /* init node's zones as empty zones, we don't have any present pages.*/
245 free_area_init_node(nid
, pgdat
, zones_size
, start_pfn
, zholes_size
);
250 static void rollback_node_hotadd(int nid
, pg_data_t
*pgdat
)
252 arch_refresh_nodedata(nid
, NULL
);
253 arch_free_nodedata(pgdat
);
258 int add_memory(int nid
, u64 start
, u64 size
)
260 pg_data_t
*pgdat
= NULL
;
262 struct resource
*res
;
265 res
= register_memory_resource(start
, size
);
269 if (!node_online(nid
)) {
270 pgdat
= hotadd_new_pgdat(nid
, start
);
276 /* call arch's memory hotadd */
277 ret
= arch_add_memory(nid
, start
, size
);
282 /* we online node here. we can't roll back from here. */
283 node_set_online(nid
);
285 cpuset_track_online_nodes();
288 ret
= register_one_node(nid
);
290 * If sysfs file of new node can't create, cpu on the node
291 * can't be hot-added. There is no rollback way now.
292 * So, check by BUG_ON() to catch it reluctantly..
299 /* rollback pgdat allocation and others */
301 rollback_node_hotadd(nid
, pgdat
);
303 release_memory_resource(res
);
307 EXPORT_SYMBOL_GPL(add_memory
);
309 #ifdef CONFIG_MEMORY_HOTREMOVE
311 * Confirm all pages in a range [start, end) is belongs to the same zone.
313 static int test_pages_in_a_zone(unsigned long start_pfn
, unsigned long end_pfn
)
316 struct zone
*zone
= NULL
;
319 for (pfn
= start_pfn
;
321 pfn
+= MAX_ORDER_NR_PAGES
) {
323 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
324 while ((i
< MAX_ORDER_NR_PAGES
) && !pfn_valid_within(pfn
+ i
))
326 if (i
== MAX_ORDER_NR_PAGES
)
328 page
= pfn_to_page(pfn
+ i
);
329 if (zone
&& page_zone(page
) != zone
)
331 zone
= page_zone(page
);
337 * Scanning pfn is much easier than scanning lru list.
338 * Scan pfn from start to end and Find LRU page.
340 int scan_lru_pages(unsigned long start
, unsigned long end
)
344 for (pfn
= start
; pfn
< end
; pfn
++) {
345 if (pfn_valid(pfn
)) {
346 page
= pfn_to_page(pfn
);
355 hotremove_migrate_alloc(struct page
*page
,
356 unsigned long private,
359 /* This should be improoooooved!! */
360 return alloc_page(GFP_HIGHUSER_PAGECACHE
);
364 #define NR_OFFLINE_AT_ONCE_PAGES (256)
366 do_migrate_range(unsigned long start_pfn
, unsigned long end_pfn
)
370 int move_pages
= NR_OFFLINE_AT_ONCE_PAGES
;
375 for (pfn
= start_pfn
; pfn
< end_pfn
&& move_pages
> 0; pfn
++) {
378 page
= pfn_to_page(pfn
);
379 if (!page_count(page
))
382 * We can skip free pages. And we can only deal with pages on
385 ret
= isolate_lru_page(page
, &source
);
386 if (!ret
) { /* Success */
389 /* Becasue we don't have big zone->lock. we should
390 check this again here. */
391 if (page_count(page
))
393 #ifdef CONFIG_DEBUG_VM
394 printk(KERN_INFO
"removing from LRU failed"
396 pfn
, page_count(page
), page
->flags
);
402 if (!list_empty(&source
))
403 putback_lru_pages(&source
);
407 if (list_empty(&source
))
409 /* this function returns # of failed pages */
410 ret
= migrate_pages(&source
, hotremove_migrate_alloc
, 0);
417 * remove from free_area[] and mark all as Reserved.
420 offline_isolated_pages_cb(unsigned long start
, unsigned long nr_pages
,
423 __offline_isolated_pages(start
, start
+ nr_pages
);
428 offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
430 walk_memory_resource(start_pfn
, end_pfn
- start_pfn
, NULL
,
431 offline_isolated_pages_cb
);
435 * Check all pages in range, recoreded as memory resource, are isolated.
438 check_pages_isolated_cb(unsigned long start_pfn
, unsigned long nr_pages
,
442 long offlined
= *(long *)data
;
443 ret
= test_pages_isolated(start_pfn
, start_pfn
+ nr_pages
);
446 *(long *)data
+= offlined
;
451 check_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
)
456 ret
= walk_memory_resource(start_pfn
, end_pfn
- start_pfn
, &offlined
,
457 check_pages_isolated_cb
);
459 offlined
= (long)ret
;
463 extern void drain_all_local_pages(void);
465 int offline_pages(unsigned long start_pfn
,
466 unsigned long end_pfn
, unsigned long timeout
)
468 unsigned long pfn
, nr_pages
, expire
;
470 int ret
, drain
, retry_max
;
473 BUG_ON(start_pfn
>= end_pfn
);
474 /* at least, alignment against pageblock is necessary */
475 if (!IS_ALIGNED(start_pfn
, pageblock_nr_pages
))
477 if (!IS_ALIGNED(end_pfn
, pageblock_nr_pages
))
479 /* This makes hotplug much easier...and readable.
480 we assume this for now. .*/
481 if (!test_pages_in_a_zone(start_pfn
, end_pfn
))
483 /* set above range as isolated */
484 ret
= start_isolate_page_range(start_pfn
, end_pfn
);
487 nr_pages
= end_pfn
- start_pfn
;
489 expire
= jiffies
+ timeout
;
493 /* start memory hot removal */
495 if (time_after(jiffies
, expire
))
498 if (signal_pending(current
))
503 flush_scheduled_work();
505 drain_all_local_pages();
508 pfn
= scan_lru_pages(start_pfn
, end_pfn
);
509 if (pfn
) { /* We have page on LRU */
510 ret
= do_migrate_range(pfn
, end_pfn
);
516 if (--retry_max
== 0)
523 /* drain all zone's lru pagevec, this is asyncronous... */
525 flush_scheduled_work();
527 /* drain pcp pages , this is synchrouns. */
528 drain_all_local_pages();
530 offlined_pages
= check_pages_isolated(start_pfn
, end_pfn
);
531 if (offlined_pages
< 0) {
535 printk(KERN_INFO
"Offlined Pages %ld\n", offlined_pages
);
536 /* Ok, all of our target is islaoted.
537 We cannot do rollback at this point. */
538 offline_isolated_pages(start_pfn
, end_pfn
);
539 /* reset pagetype flags */
540 start_isolate_page_range(start_pfn
, end_pfn
);
541 /* removal success */
542 zone
= page_zone(pfn_to_page(start_pfn
));
543 zone
->present_pages
-= offlined_pages
;
544 zone
->zone_pgdat
->node_present_pages
-= offlined_pages
;
545 totalram_pages
-= offlined_pages
;
546 num_physpages
-= offlined_pages
;
547 vm_total_pages
= nr_free_pagecache_pages();
548 writeback_set_ratelimit();
552 printk(KERN_INFO
"memory offlining %lx to %lx failed\n",
554 /* pushback to free area */
555 undo_isolate_page_range(start_pfn
, end_pfn
);
559 int remove_memory(u64 start
, u64 size
)
563 EXPORT_SYMBOL_GPL(remove_memory
);
564 #endif /* CONFIG_MEMORY_HOTREMOVE */