]> git.ipfire.org Git - people/arne_f/kernel.git/blame - mm/memory_hotplug.c
memory-hotplug: integrated __remove_section() of CONFIG_SPARSEMEM_VMEMMAP.
[people/arne_f/kernel.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>
b95f1b31 14#include <linux/export.h>
3947be19 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>
d96ae530 31#include <linux/firmware-map.h>
3947be19
DH
32
33#include <asm/tlbflush.h>
34
1e5ad9a3
AB
35#include "internal.h"
36
9d0ad8ca
DK
37/*
38 * online_page_callback contains pointer to current page onlining function.
39 * Initially it is generic_online_page(). If it is required it could be
40 * changed by calling set_online_page_callback() for callback registration
41 * and restore_online_page_callback() for generic callback restore.
42 */
43
44static void generic_online_page(struct page *page);
45
46static online_page_callback_t online_page_callback = generic_online_page;
47
20d6c96b
KM
48DEFINE_MUTEX(mem_hotplug_mutex);
49
50void lock_memory_hotplug(void)
51{
52 mutex_lock(&mem_hotplug_mutex);
53
54 /* for exclusive hibernation if CONFIG_HIBERNATION=y */
55 lock_system_sleep();
56}
57
58void unlock_memory_hotplug(void)
59{
60 unlock_system_sleep();
61 mutex_unlock(&mem_hotplug_mutex);
62}
63
64
45e0b78b
KM
65/* add this memory to iomem resource */
66static struct resource *register_memory_resource(u64 start, u64 size)
67{
68 struct resource *res;
69 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
70 BUG_ON(!res);
71
72 res->name = "System RAM";
73 res->start = start;
74 res->end = start + size - 1;
887c3cb1 75 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
45e0b78b 76 if (request_resource(&iomem_resource, res) < 0) {
a62e2f4f 77 printk("System RAM resource %pR cannot be added\n", res);
45e0b78b
KM
78 kfree(res);
79 res = NULL;
80 }
81 return res;
82}
83
84static void release_memory_resource(struct resource *res)
85{
86 if (!res)
87 return;
88 release_resource(res);
89 kfree(res);
90 return;
91}
92
53947027 93#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
46723bfa
YI
94void get_page_bootmem(unsigned long info, struct page *page,
95 unsigned long type)
04753278 96{
5f24ce5f 97 page->lru.next = (struct list_head *) type;
04753278
YG
98 SetPagePrivate(page);
99 set_page_private(page, info);
100 atomic_inc(&page->_count);
101}
102
23ce932a
RM
103/* reference to __meminit __free_pages_bootmem is valid
104 * so use __ref to tell modpost not to generate a warning */
105void __ref put_page_bootmem(struct page *page)
04753278 106{
5f24ce5f 107 unsigned long type;
9feedc9d 108 static DEFINE_MUTEX(ppb_lock);
04753278 109
5f24ce5f
AA
110 type = (unsigned long) page->lru.next;
111 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
04753278
YG
113
114 if (atomic_dec_return(&page->_count) == 1) {
115 ClearPagePrivate(page);
116 set_page_private(page, 0);
5f24ce5f 117 INIT_LIST_HEAD(&page->lru);
9feedc9d
JL
118
119 /*
120 * Please refer to comment for __free_pages_bootmem()
121 * for why we serialize here.
122 */
123 mutex_lock(&ppb_lock);
04753278 124 __free_pages_bootmem(page, 0);
9feedc9d 125 mutex_unlock(&ppb_lock);
04753278
YG
126 }
127
128}
129
46723bfa
YI
130#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
131#ifndef CONFIG_SPARSEMEM_VMEMMAP
d92bc318 132static void register_page_bootmem_info_section(unsigned long start_pfn)
04753278
YG
133{
134 unsigned long *usemap, mapsize, section_nr, i;
135 struct mem_section *ms;
136 struct page *page, *memmap;
137
04753278
YG
138 section_nr = pfn_to_section_nr(start_pfn);
139 ms = __nr_to_section(section_nr);
140
141 /* Get section's memmap address */
142 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
143
144 /*
145 * Get page for the memmap's phys address
146 * XXX: need more consideration for sparse_vmemmap...
147 */
148 page = virt_to_page(memmap);
149 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
150 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
151
152 /* remember memmap's page */
153 for (i = 0; i < mapsize; i++, page++)
154 get_page_bootmem(section_nr, page, SECTION_INFO);
155
156 usemap = __nr_to_section(section_nr)->pageblock_flags;
157 page = virt_to_page(usemap);
158
159 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
160
161 for (i = 0; i < mapsize; i++, page++)
af370fb8 162 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
04753278
YG
163
164}
46723bfa
YI
165#else /* CONFIG_SPARSEMEM_VMEMMAP */
166static void register_page_bootmem_info_section(unsigned long start_pfn)
167{
168 unsigned long *usemap, mapsize, section_nr, i;
169 struct mem_section *ms;
170 struct page *page, *memmap;
171
172 if (!pfn_valid(start_pfn))
173 return;
174
175 section_nr = pfn_to_section_nr(start_pfn);
176 ms = __nr_to_section(section_nr);
177
178 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
179
180 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
181
182 usemap = __nr_to_section(section_nr)->pageblock_flags;
183 page = virt_to_page(usemap);
184
185 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
186
187 for (i = 0; i < mapsize; i++, page++)
188 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
189}
190#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
04753278
YG
191
192void register_page_bootmem_info_node(struct pglist_data *pgdat)
193{
194 unsigned long i, pfn, end_pfn, nr_pages;
195 int node = pgdat->node_id;
196 struct page *page;
197 struct zone *zone;
198
199 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
200 page = virt_to_page(pgdat);
201
202 for (i = 0; i < nr_pages; i++, page++)
203 get_page_bootmem(node, page, NODE_INFO);
204
205 zone = &pgdat->node_zones[0];
206 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
207 if (zone->wait_table) {
208 nr_pages = zone->wait_table_hash_nr_entries
209 * sizeof(wait_queue_head_t);
210 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
211 page = virt_to_page(zone->wait_table);
212
213 for (i = 0; i < nr_pages; i++, page++)
214 get_page_bootmem(node, page, NODE_INFO);
215 }
216 }
217
218 pfn = pgdat->node_start_pfn;
219 end_pfn = pfn + pgdat->node_spanned_pages;
220
221 /* register_section info */
f14851af 222 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
223 /*
224 * Some platforms can assign the same pfn to multiple nodes - on
225 * node0 as well as nodeN. To avoid registering a pfn against
226 * multiple nodes we check that this pfn does not already
227 * reside in some other node.
228 */
229 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
230 register_page_bootmem_info_section(pfn);
231 }
04753278 232}
46723bfa 233#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
04753278 234
76cdd58e
HC
235static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
236 unsigned long end_pfn)
237{
238 unsigned long old_zone_end_pfn;
239
240 zone_span_writelock(zone);
241
242 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
712cd386 243 if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
76cdd58e
HC
244 zone->zone_start_pfn = start_pfn;
245
246 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
247 zone->zone_start_pfn;
248
249 zone_span_writeunlock(zone);
250}
251
511c2aba
LJ
252static void resize_zone(struct zone *zone, unsigned long start_pfn,
253 unsigned long end_pfn)
254{
255 zone_span_writelock(zone);
256
e455a9b9
LJ
257 if (end_pfn - start_pfn) {
258 zone->zone_start_pfn = start_pfn;
259 zone->spanned_pages = end_pfn - start_pfn;
260 } else {
261 /*
262 * make it consist as free_area_init_core(),
263 * if spanned_pages = 0, then keep start_pfn = 0
264 */
265 zone->zone_start_pfn = 0;
266 zone->spanned_pages = 0;
267 }
511c2aba
LJ
268
269 zone_span_writeunlock(zone);
270}
271
272static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
273 unsigned long end_pfn)
274{
275 enum zone_type zid = zone_idx(zone);
276 int nid = zone->zone_pgdat->node_id;
277 unsigned long pfn;
278
279 for (pfn = start_pfn; pfn < end_pfn; pfn++)
280 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
281}
282
e455a9b9 283static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
511c2aba
LJ
284 unsigned long start_pfn, unsigned long end_pfn)
285{
e455a9b9 286 int ret;
511c2aba 287 unsigned long flags;
e455a9b9
LJ
288 unsigned long z1_start_pfn;
289
290 if (!z1->wait_table) {
291 ret = init_currently_empty_zone(z1, start_pfn,
292 end_pfn - start_pfn, MEMMAP_HOTPLUG);
293 if (ret)
294 return ret;
295 }
511c2aba
LJ
296
297 pgdat_resize_lock(z1->zone_pgdat, &flags);
298
299 /* can't move pfns which are higher than @z2 */
300 if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
301 goto out_fail;
302 /* the move out part mast at the left most of @z2 */
303 if (start_pfn > z2->zone_start_pfn)
304 goto out_fail;
305 /* must included/overlap */
306 if (end_pfn <= z2->zone_start_pfn)
307 goto out_fail;
308
e455a9b9
LJ
309 /* use start_pfn for z1's start_pfn if z1 is empty */
310 if (z1->spanned_pages)
311 z1_start_pfn = z1->zone_start_pfn;
312 else
313 z1_start_pfn = start_pfn;
314
315 resize_zone(z1, z1_start_pfn, end_pfn);
511c2aba
LJ
316 resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
317
318 pgdat_resize_unlock(z1->zone_pgdat, &flags);
319
320 fix_zone_id(z1, start_pfn, end_pfn);
321
322 return 0;
323out_fail:
324 pgdat_resize_unlock(z1->zone_pgdat, &flags);
325 return -1;
326}
327
e455a9b9 328static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
511c2aba
LJ
329 unsigned long start_pfn, unsigned long end_pfn)
330{
e455a9b9 331 int ret;
511c2aba 332 unsigned long flags;
e455a9b9
LJ
333 unsigned long z2_end_pfn;
334
335 if (!z2->wait_table) {
336 ret = init_currently_empty_zone(z2, start_pfn,
337 end_pfn - start_pfn, MEMMAP_HOTPLUG);
338 if (ret)
339 return ret;
340 }
511c2aba
LJ
341
342 pgdat_resize_lock(z1->zone_pgdat, &flags);
343
344 /* can't move pfns which are lower than @z1 */
345 if (z1->zone_start_pfn > start_pfn)
346 goto out_fail;
347 /* the move out part mast at the right most of @z1 */
348 if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
349 goto out_fail;
350 /* must included/overlap */
351 if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
352 goto out_fail;
353
e455a9b9
LJ
354 /* use end_pfn for z2's end_pfn if z2 is empty */
355 if (z2->spanned_pages)
356 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
357 else
358 z2_end_pfn = end_pfn;
359
511c2aba 360 resize_zone(z1, z1->zone_start_pfn, start_pfn);
e455a9b9 361 resize_zone(z2, start_pfn, z2_end_pfn);
511c2aba
LJ
362
363 pgdat_resize_unlock(z1->zone_pgdat, &flags);
364
365 fix_zone_id(z2, start_pfn, end_pfn);
366
367 return 0;
368out_fail:
369 pgdat_resize_unlock(z1->zone_pgdat, &flags);
370 return -1;
371}
372
76cdd58e
HC
373static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
374 unsigned long end_pfn)
375{
376 unsigned long old_pgdat_end_pfn =
377 pgdat->node_start_pfn + pgdat->node_spanned_pages;
378
712cd386 379 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
76cdd58e
HC
380 pgdat->node_start_pfn = start_pfn;
381
382 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
383 pgdat->node_start_pfn;
384}
385
31168481 386static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
3947be19
DH
387{
388 struct pglist_data *pgdat = zone->zone_pgdat;
389 int nr_pages = PAGES_PER_SECTION;
390 int nid = pgdat->node_id;
391 int zone_type;
76cdd58e 392 unsigned long flags;
3947be19
DH
393
394 zone_type = zone - pgdat->node_zones;
76cdd58e
HC
395 if (!zone->wait_table) {
396 int ret;
397
398 ret = init_currently_empty_zone(zone, phys_start_pfn,
399 nr_pages, MEMMAP_HOTPLUG);
400 if (ret)
401 return ret;
402 }
403 pgdat_resize_lock(zone->zone_pgdat, &flags);
404 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
405 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
406 phys_start_pfn + nr_pages);
407 pgdat_resize_unlock(zone->zone_pgdat, &flags);
a2f3aa02
DH
408 memmap_init_zone(nr_pages, nid, zone_type,
409 phys_start_pfn, MEMMAP_HOTPLUG);
718127cc 410 return 0;
3947be19
DH
411}
412
c04fc586
GH
413static int __meminit __add_section(int nid, struct zone *zone,
414 unsigned long phys_start_pfn)
3947be19 415{
3947be19 416 int nr_pages = PAGES_PER_SECTION;
3947be19
DH
417 int ret;
418
ebd15302
KH
419 if (pfn_valid(phys_start_pfn))
420 return -EEXIST;
421
0b0acbec 422 ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
3947be19
DH
423
424 if (ret < 0)
425 return ret;
426
718127cc
YG
427 ret = __add_zone(zone, phys_start_pfn);
428
429 if (ret < 0)
430 return ret;
431
c04fc586 432 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
3947be19
DH
433}
434
ea01ea93
BP
435static int __remove_section(struct zone *zone, struct mem_section *ms)
436{
ea01ea93
BP
437 int ret = -EINVAL;
438
439 if (!valid_section(ms))
440 return ret;
441
442 ret = unregister_memory_section(ms);
443 if (ret)
444 return ret;
445
ea01ea93 446 sparse_remove_one_section(zone, ms);
ea01ea93
BP
447 return 0;
448}
449
3947be19
DH
450/*
451 * Reasonably generic function for adding memory. It is
452 * expected that archs that support memory hotplug will
453 * call this function after deciding the zone to which to
454 * add the new pages.
455 */
c04fc586
GH
456int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
457 unsigned long nr_pages)
3947be19
DH
458{
459 unsigned long i;
460 int err = 0;
6f712711
KH
461 int start_sec, end_sec;
462 /* during initialize mem_map, align hot-added range to section */
463 start_sec = pfn_to_section_nr(phys_start_pfn);
464 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
3947be19 465
6f712711 466 for (i = start_sec; i <= end_sec; i++) {
c04fc586 467 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
3947be19 468
6f712711 469 /*
183ff22b 470 * EEXIST is finally dealt with by ioresource collision
6f712711
KH
471 * check. see add_memory() => register_memory_resource()
472 * Warning will be printed if there is collision.
bed120c6
JS
473 */
474 if (err && (err != -EEXIST))
3947be19 475 break;
6f712711 476 err = 0;
3947be19
DH
477 }
478
479 return err;
480}
bed120c6 481EXPORT_SYMBOL_GPL(__add_pages);
3947be19 482
ea01ea93
BP
483/**
484 * __remove_pages() - remove sections of pages from a zone
485 * @zone: zone from which pages need to be removed
486 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
487 * @nr_pages: number of pages to remove (must be multiple of section size)
488 *
489 * Generic helper function to remove section mappings and sysfs entries
490 * for the section of the memory we are removing. Caller needs to make
491 * sure that pages are marked reserved and zones are adjust properly by
492 * calling offline_pages().
493 */
494int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
495 unsigned long nr_pages)
496{
497 unsigned long i, ret = 0;
498 int sections_to_remove;
499
500 /*
501 * We can only remove entire sections
502 */
503 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
504 BUG_ON(nr_pages % PAGES_PER_SECTION);
505
d760afd4
YI
506 release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
507
ea01ea93
BP
508 sections_to_remove = nr_pages / PAGES_PER_SECTION;
509 for (i = 0; i < sections_to_remove; i++) {
510 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
511 ret = __remove_section(zone, __pfn_to_section(pfn));
512 if (ret)
513 break;
514 }
515 return ret;
516}
517EXPORT_SYMBOL_GPL(__remove_pages);
518
9d0ad8ca
DK
519int set_online_page_callback(online_page_callback_t callback)
520{
521 int rc = -EINVAL;
522
523 lock_memory_hotplug();
524
525 if (online_page_callback == generic_online_page) {
526 online_page_callback = callback;
527 rc = 0;
528 }
529
530 unlock_memory_hotplug();
531
532 return rc;
533}
534EXPORT_SYMBOL_GPL(set_online_page_callback);
535
536int restore_online_page_callback(online_page_callback_t callback)
537{
538 int rc = -EINVAL;
539
540 lock_memory_hotplug();
541
542 if (online_page_callback == callback) {
543 online_page_callback = generic_online_page;
544 rc = 0;
545 }
546
547 unlock_memory_hotplug();
548
549 return rc;
550}
551EXPORT_SYMBOL_GPL(restore_online_page_callback);
552
553void __online_page_set_limits(struct page *page)
180c06ef 554{
4738e1b9
JB
555 unsigned long pfn = page_to_pfn(page);
556
4738e1b9
JB
557 if (pfn >= num_physpages)
558 num_physpages = pfn + 1;
9d0ad8ca
DK
559}
560EXPORT_SYMBOL_GPL(__online_page_set_limits);
561
562void __online_page_increment_counters(struct page *page)
563{
564 totalram_pages++;
180c06ef
JF
565
566#ifdef CONFIG_HIGHMEM
567 if (PageHighMem(page))
568 totalhigh_pages++;
569#endif
9d0ad8ca
DK
570}
571EXPORT_SYMBOL_GPL(__online_page_increment_counters);
180c06ef 572
9d0ad8ca
DK
573void __online_page_free(struct page *page)
574{
180c06ef
JF
575 ClearPageReserved(page);
576 init_page_count(page);
577 __free_page(page);
578}
9d0ad8ca
DK
579EXPORT_SYMBOL_GPL(__online_page_free);
580
581static void generic_online_page(struct page *page)
582{
583 __online_page_set_limits(page);
584 __online_page_increment_counters(page);
585 __online_page_free(page);
586}
180c06ef 587
75884fb1
KH
588static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
589 void *arg)
3947be19
DH
590{
591 unsigned long i;
75884fb1
KH
592 unsigned long onlined_pages = *(unsigned long *)arg;
593 struct page *page;
594 if (PageReserved(pfn_to_page(start_pfn)))
595 for (i = 0; i < nr_pages; i++) {
596 page = pfn_to_page(start_pfn + i);
9d0ad8ca 597 (*online_page_callback)(page);
75884fb1
KH
598 onlined_pages++;
599 }
600 *(unsigned long *)arg = onlined_pages;
601 return 0;
602}
603
09285af7 604#ifdef CONFIG_MOVABLE_NODE
79a4dcef
TC
605/*
606 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
607 * normal memory.
608 */
09285af7
LJ
609static bool can_online_high_movable(struct zone *zone)
610{
611 return true;
612}
79a4dcef 613#else /* CONFIG_MOVABLE_NODE */
74d42d8f
LJ
614/* ensure every online node has NORMAL memory */
615static bool can_online_high_movable(struct zone *zone)
616{
617 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
618}
79a4dcef 619#endif /* CONFIG_MOVABLE_NODE */
74d42d8f 620
d9713679
LJ
621/* check which state of node_states will be changed when online memory */
622static void node_states_check_changes_online(unsigned long nr_pages,
623 struct zone *zone, struct memory_notify *arg)
624{
625 int nid = zone_to_nid(zone);
626 enum zone_type zone_last = ZONE_NORMAL;
627
628 /*
6715ddf9
LJ
629 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
630 * contains nodes which have zones of 0...ZONE_NORMAL,
631 * set zone_last to ZONE_NORMAL.
d9713679 632 *
6715ddf9
LJ
633 * If we don't have HIGHMEM nor movable node,
634 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
635 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
d9713679 636 */
6715ddf9 637 if (N_MEMORY == N_NORMAL_MEMORY)
d9713679
LJ
638 zone_last = ZONE_MOVABLE;
639
640 /*
641 * if the memory to be online is in a zone of 0...zone_last, and
642 * the zones of 0...zone_last don't have memory before online, we will
643 * need to set the node to node_states[N_NORMAL_MEMORY] after
644 * the memory is online.
645 */
646 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
647 arg->status_change_nid_normal = nid;
648 else
649 arg->status_change_nid_normal = -1;
650
6715ddf9
LJ
651#ifdef CONFIG_HIGHMEM
652 /*
653 * If we have movable node, node_states[N_HIGH_MEMORY]
654 * contains nodes which have zones of 0...ZONE_HIGHMEM,
655 * set zone_last to ZONE_HIGHMEM.
656 *
657 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
658 * contains nodes which have zones of 0...ZONE_MOVABLE,
659 * set zone_last to ZONE_MOVABLE.
660 */
661 zone_last = ZONE_HIGHMEM;
662 if (N_MEMORY == N_HIGH_MEMORY)
663 zone_last = ZONE_MOVABLE;
664
665 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
666 arg->status_change_nid_high = nid;
667 else
668 arg->status_change_nid_high = -1;
669#else
670 arg->status_change_nid_high = arg->status_change_nid_normal;
671#endif
672
d9713679
LJ
673 /*
674 * if the node don't have memory befor online, we will need to
6715ddf9 675 * set the node to node_states[N_MEMORY] after the memory
d9713679
LJ
676 * is online.
677 */
6715ddf9 678 if (!node_state(nid, N_MEMORY))
d9713679
LJ
679 arg->status_change_nid = nid;
680 else
681 arg->status_change_nid = -1;
682}
683
684static void node_states_set_node(int node, struct memory_notify *arg)
685{
686 if (arg->status_change_nid_normal >= 0)
687 node_set_state(node, N_NORMAL_MEMORY);
688
6715ddf9
LJ
689 if (arg->status_change_nid_high >= 0)
690 node_set_state(node, N_HIGH_MEMORY);
691
692 node_set_state(node, N_MEMORY);
d9713679
LJ
693}
694
75884fb1 695
511c2aba 696int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
75884fb1 697{
3947be19
DH
698 unsigned long onlined_pages = 0;
699 struct zone *zone;
6811378e 700 int need_zonelists_rebuild = 0;
7b78d335
YG
701 int nid;
702 int ret;
703 struct memory_notify arg;
704
925268a0 705 lock_memory_hotplug();
d9713679
LJ
706 /*
707 * This doesn't need a lock to do pfn_to_page().
708 * The section can't be removed here because of the
709 * memory_block->state_mutex.
710 */
711 zone = page_zone(pfn_to_page(pfn));
712
74d42d8f
LJ
713 if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
714 !can_online_high_movable(zone)) {
715 unlock_memory_hotplug();
716 return -1;
717 }
718
511c2aba
LJ
719 if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
720 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
721 unlock_memory_hotplug();
722 return -1;
723 }
724 }
725 if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
726 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
727 unlock_memory_hotplug();
728 return -1;
729 }
730 }
731
732 /* Previous code may changed the zone of the pfn range */
733 zone = page_zone(pfn_to_page(pfn));
734
7b78d335
YG
735 arg.start_pfn = pfn;
736 arg.nr_pages = nr_pages;
d9713679 737 node_states_check_changes_online(nr_pages, zone, &arg);
7b78d335
YG
738
739 nid = page_to_nid(pfn_to_page(pfn));
3947be19 740
7b78d335
YG
741 ret = memory_notify(MEM_GOING_ONLINE, &arg);
742 ret = notifier_to_errno(ret);
743 if (ret) {
744 memory_notify(MEM_CANCEL_ONLINE, &arg);
925268a0 745 unlock_memory_hotplug();
7b78d335
YG
746 return ret;
747 }
6811378e
YG
748 /*
749 * If this zone is not populated, then it is not in zonelist.
750 * This means the page allocator ignores this zone.
751 * So, zonelist must be updated after online.
752 */
4eaf3f64 753 mutex_lock(&zonelists_mutex);
6dcd73d7 754 if (!populated_zone(zone)) {
6811378e 755 need_zonelists_rebuild = 1;
6dcd73d7
WC
756 build_all_zonelists(NULL, zone);
757 }
6811378e 758
908eedc6 759 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
75884fb1 760 online_pages_range);
fd8a4221 761 if (ret) {
6dcd73d7
WC
762 if (need_zonelists_rebuild)
763 zone_pcp_reset(zone);
4eaf3f64 764 mutex_unlock(&zonelists_mutex);
a62e2f4f
BH
765 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
766 (unsigned long long) pfn << PAGE_SHIFT,
767 (((unsigned long long) pfn + nr_pages)
768 << PAGE_SHIFT) - 1);
fd8a4221 769 memory_notify(MEM_CANCEL_ONLINE, &arg);
925268a0 770 unlock_memory_hotplug();
fd8a4221
GL
771 return ret;
772 }
773
9feedc9d 774 zone->managed_pages += onlined_pages;
3947be19 775 zone->present_pages += onlined_pages;
f2937be5 776 zone->zone_pgdat->node_present_pages += onlined_pages;
08dff7b7 777 if (onlined_pages) {
d9713679 778 node_states_set_node(zone_to_nid(zone), &arg);
08dff7b7 779 if (need_zonelists_rebuild)
6dcd73d7 780 build_all_zonelists(NULL, NULL);
08dff7b7
JL
781 else
782 zone_pcp_update(zone);
783 }
3947be19 784
4eaf3f64 785 mutex_unlock(&zonelists_mutex);
1b79acc9
KM
786
787 init_per_zone_wmark_min();
788
08dff7b7 789 if (onlined_pages)
7ea1530a 790 kswapd_run(zone_to_nid(zone));
61b13993 791
1f522509 792 vm_total_pages = nr_free_pagecache_pages();
2f7f24ec 793
2d1d43f6 794 writeback_set_ratelimit();
7b78d335
YG
795
796 if (onlined_pages)
797 memory_notify(MEM_ONLINE, &arg);
925268a0 798 unlock_memory_hotplug();
7b78d335 799
3947be19
DH
800 return 0;
801}
53947027 802#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
bc02af93 803
e1319331
HS
804/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
805static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9af3c2de
YG
806{
807 struct pglist_data *pgdat;
808 unsigned long zones_size[MAX_NR_ZONES] = {0};
809 unsigned long zholes_size[MAX_NR_ZONES] = {0};
810 unsigned long start_pfn = start >> PAGE_SHIFT;
811
812 pgdat = arch_alloc_nodedata(nid);
813 if (!pgdat)
814 return NULL;
815
816 arch_refresh_nodedata(nid, pgdat);
817
818 /* we can use NODE_DATA(nid) from here */
819
820 /* init node's zones as empty zones, we don't have any present pages.*/
9109fb7b 821 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
9af3c2de 822
959ecc48
KH
823 /*
824 * The node we allocated has no zone fallback lists. For avoiding
825 * to access not-initialized zonelist, build here.
826 */
f957db4f 827 mutex_lock(&zonelists_mutex);
9adb62a5 828 build_all_zonelists(pgdat, NULL);
f957db4f 829 mutex_unlock(&zonelists_mutex);
959ecc48 830
9af3c2de
YG
831 return pgdat;
832}
833
834static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
835{
836 arch_refresh_nodedata(nid, NULL);
837 arch_free_nodedata(pgdat);
838 return;
839}
840
0a547039 841
cf23422b 842/*
843 * called by cpu_up() to online a node without onlined memory.
844 */
845int mem_online_node(int nid)
846{
847 pg_data_t *pgdat;
848 int ret;
849
20d6c96b 850 lock_memory_hotplug();
cf23422b 851 pgdat = hotadd_new_pgdat(nid, 0);
7553e8f2 852 if (!pgdat) {
cf23422b 853 ret = -ENOMEM;
854 goto out;
855 }
856 node_set_online(nid);
857 ret = register_one_node(nid);
858 BUG_ON(ret);
859
860out:
20d6c96b 861 unlock_memory_hotplug();
cf23422b 862 return ret;
863}
864
31168481
AV
865/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
866int __ref add_memory(int nid, u64 start, u64 size)
bc02af93 867{
9af3c2de
YG
868 pg_data_t *pgdat = NULL;
869 int new_pgdat = 0;
ebd15302 870 struct resource *res;
bc02af93
YG
871 int ret;
872
20d6c96b 873 lock_memory_hotplug();
6ad696d2 874
ebd15302 875 res = register_memory_resource(start, size);
6ad696d2 876 ret = -EEXIST;
ebd15302 877 if (!res)
6ad696d2 878 goto out;
ebd15302 879
9af3c2de
YG
880 if (!node_online(nid)) {
881 pgdat = hotadd_new_pgdat(nid, start);
6ad696d2 882 ret = -ENOMEM;
9af3c2de 883 if (!pgdat)
41b9e2d7 884 goto error;
9af3c2de 885 new_pgdat = 1;
9af3c2de
YG
886 }
887
bc02af93
YG
888 /* call arch's memory hotadd */
889 ret = arch_add_memory(nid, start, size);
890
9af3c2de
YG
891 if (ret < 0)
892 goto error;
893
0fc44159 894 /* we online node here. we can't roll back from here. */
9af3c2de
YG
895 node_set_online(nid);
896
0fc44159
YG
897 if (new_pgdat) {
898 ret = register_one_node(nid);
899 /*
900 * If sysfs file of new node can't create, cpu on the node
901 * can't be hot-added. There is no rollback way now.
902 * So, check by BUG_ON() to catch it reluctantly..
903 */
904 BUG_ON(ret);
905 }
906
d96ae530 907 /* create new memmap entry */
908 firmware_map_add_hotplug(start, start + size, "System RAM");
909
6ad696d2
AK
910 goto out;
911
9af3c2de
YG
912error:
913 /* rollback pgdat allocation and others */
914 if (new_pgdat)
915 rollback_node_hotadd(nid, pgdat);
a864b9d0 916 release_memory_resource(res);
9af3c2de 917
6ad696d2 918out:
20d6c96b 919 unlock_memory_hotplug();
bc02af93
YG
920 return ret;
921}
922EXPORT_SYMBOL_GPL(add_memory);
0c0e6195
KH
923
924#ifdef CONFIG_MEMORY_HOTREMOVE
5c755e9f
BP
925/*
926 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
927 * set and the size of the free page is given by page_order(). Using this,
928 * the function determines if the pageblock contains only free pages.
929 * Due to buddy contraints, a free page at least the size of a pageblock will
930 * be located at the start of the pageblock
931 */
932static inline int pageblock_free(struct page *page)
933{
934 return PageBuddy(page) && page_order(page) >= pageblock_order;
935}
936
937/* Return the start of the next active pageblock after a given page */
938static struct page *next_active_pageblock(struct page *page)
939{
5c755e9f
BP
940 /* Ensure the starting page is pageblock-aligned */
941 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
942
5c755e9f 943 /* If the entire pageblock is free, move to the end of free page */
0dcc48c1
KH
944 if (pageblock_free(page)) {
945 int order;
946 /* be careful. we don't have locks, page_order can be changed.*/
947 order = page_order(page);
948 if ((order < MAX_ORDER) && (order >= pageblock_order))
949 return page + (1 << order);
950 }
5c755e9f 951
0dcc48c1 952 return page + pageblock_nr_pages;
5c755e9f
BP
953}
954
955/* Checks if this range of memory is likely to be hot-removable. */
956int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
957{
5c755e9f
BP
958 struct page *page = pfn_to_page(start_pfn);
959 struct page *end_page = page + nr_pages;
960
961 /* Check the starting page of each pageblock within the range */
962 for (; page < end_page; page = next_active_pageblock(page)) {
49ac8255 963 if (!is_pageblock_removable_nolock(page))
5c755e9f 964 return 0;
49ac8255 965 cond_resched();
5c755e9f
BP
966 }
967
968 /* All pageblocks in the memory block are likely to be hot-removable */
969 return 1;
970}
971
0c0e6195
KH
972/*
973 * Confirm all pages in a range [start, end) is belongs to the same zone.
974 */
975static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
976{
977 unsigned long pfn;
978 struct zone *zone = NULL;
979 struct page *page;
980 int i;
981 for (pfn = start_pfn;
982 pfn < end_pfn;
983 pfn += MAX_ORDER_NR_PAGES) {
984 i = 0;
985 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
986 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
987 i++;
988 if (i == MAX_ORDER_NR_PAGES)
989 continue;
990 page = pfn_to_page(pfn + i);
991 if (zone && page_zone(page) != zone)
992 return 0;
993 zone = page_zone(page);
994 }
995 return 1;
996}
997
998/*
999 * Scanning pfn is much easier than scanning lru list.
1000 * Scan pfn from start to end and Find LRU page.
1001 */
7bbc0905 1002static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
0c0e6195
KH
1003{
1004 unsigned long pfn;
1005 struct page *page;
1006 for (pfn = start; pfn < end; pfn++) {
1007 if (pfn_valid(pfn)) {
1008 page = pfn_to_page(pfn);
1009 if (PageLRU(page))
1010 return pfn;
1011 }
1012 }
1013 return 0;
1014}
1015
0c0e6195
KH
1016#define NR_OFFLINE_AT_ONCE_PAGES (256)
1017static int
1018do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1019{
1020 unsigned long pfn;
1021 struct page *page;
1022 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1023 int not_managed = 0;
1024 int ret = 0;
1025 LIST_HEAD(source);
1026
1027 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1028 if (!pfn_valid(pfn))
1029 continue;
1030 page = pfn_to_page(pfn);
700c2a46 1031 if (!get_page_unless_zero(page))
0c0e6195
KH
1032 continue;
1033 /*
1034 * We can skip free pages. And we can only deal with pages on
1035 * LRU.
1036 */
62695a84 1037 ret = isolate_lru_page(page);
0c0e6195 1038 if (!ret) { /* Success */
700c2a46 1039 put_page(page);
62695a84 1040 list_add_tail(&page->lru, &source);
0c0e6195 1041 move_pages--;
6d9c285a
KM
1042 inc_zone_page_state(page, NR_ISOLATED_ANON +
1043 page_is_file_cache(page));
1044
0c0e6195 1045 } else {
0c0e6195 1046#ifdef CONFIG_DEBUG_VM
718a3821
WF
1047 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1048 pfn);
1049 dump_page(page);
0c0e6195 1050#endif
700c2a46 1051 put_page(page);
25985edc 1052 /* Because we don't have big zone->lock. we should
809c4449
BL
1053 check this again here. */
1054 if (page_count(page)) {
1055 not_managed++;
f3ab2636 1056 ret = -EBUSY;
809c4449
BL
1057 break;
1058 }
0c0e6195
KH
1059 }
1060 }
f3ab2636
BL
1061 if (!list_empty(&source)) {
1062 if (not_managed) {
1063 putback_lru_pages(&source);
1064 goto out;
1065 }
74c08f98
MK
1066
1067 /*
1068 * alloc_migrate_target should be improooooved!!
1069 * migrate_pages returns # of failed pages.
1070 */
1071 ret = migrate_pages(&source, alloc_migrate_target, 0,
7b2a2d4a
MG
1072 true, MIGRATE_SYNC,
1073 MR_MEMORY_HOTPLUG);
f3ab2636 1074 if (ret)
0c0e6195 1075 putback_lru_pages(&source);
0c0e6195 1076 }
0c0e6195
KH
1077out:
1078 return ret;
1079}
1080
1081/*
1082 * remove from free_area[] and mark all as Reserved.
1083 */
1084static int
1085offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1086 void *data)
1087{
1088 __offline_isolated_pages(start, start + nr_pages);
1089 return 0;
1090}
1091
1092static void
1093offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1094{
908eedc6 1095 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
0c0e6195
KH
1096 offline_isolated_pages_cb);
1097}
1098
1099/*
1100 * Check all pages in range, recoreded as memory resource, are isolated.
1101 */
1102static int
1103check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1104 void *data)
1105{
1106 int ret;
1107 long offlined = *(long *)data;
b023f468 1108 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
0c0e6195
KH
1109 offlined = nr_pages;
1110 if (!ret)
1111 *(long *)data += offlined;
1112 return ret;
1113}
1114
1115static long
1116check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1117{
1118 long offlined = 0;
1119 int ret;
1120
908eedc6 1121 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
0c0e6195
KH
1122 check_pages_isolated_cb);
1123 if (ret < 0)
1124 offlined = (long)ret;
1125 return offlined;
1126}
1127
09285af7 1128#ifdef CONFIG_MOVABLE_NODE
79a4dcef
TC
1129/*
1130 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1131 * normal memory.
1132 */
09285af7
LJ
1133static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1134{
1135 return true;
1136}
79a4dcef 1137#else /* CONFIG_MOVABLE_NODE */
74d42d8f
LJ
1138/* ensure the node has NORMAL memory if it is still online */
1139static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1140{
1141 struct pglist_data *pgdat = zone->zone_pgdat;
1142 unsigned long present_pages = 0;
1143 enum zone_type zt;
1144
1145 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1146 present_pages += pgdat->node_zones[zt].present_pages;
1147
1148 if (present_pages > nr_pages)
1149 return true;
1150
1151 present_pages = 0;
1152 for (; zt <= ZONE_MOVABLE; zt++)
1153 present_pages += pgdat->node_zones[zt].present_pages;
1154
1155 /*
1156 * we can't offline the last normal memory until all
1157 * higher memory is offlined.
1158 */
1159 return present_pages == 0;
1160}
79a4dcef 1161#endif /* CONFIG_MOVABLE_NODE */
74d42d8f 1162
d9713679
LJ
1163/* check which state of node_states will be changed when offline memory */
1164static void node_states_check_changes_offline(unsigned long nr_pages,
1165 struct zone *zone, struct memory_notify *arg)
1166{
1167 struct pglist_data *pgdat = zone->zone_pgdat;
1168 unsigned long present_pages = 0;
1169 enum zone_type zt, zone_last = ZONE_NORMAL;
1170
1171 /*
6715ddf9
LJ
1172 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1173 * contains nodes which have zones of 0...ZONE_NORMAL,
1174 * set zone_last to ZONE_NORMAL.
d9713679 1175 *
6715ddf9
LJ
1176 * If we don't have HIGHMEM nor movable node,
1177 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1178 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
d9713679 1179 */
6715ddf9 1180 if (N_MEMORY == N_NORMAL_MEMORY)
d9713679
LJ
1181 zone_last = ZONE_MOVABLE;
1182
1183 /*
1184 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1185 * If the memory to be offline is in a zone of 0...zone_last,
1186 * and it is the last present memory, 0...zone_last will
1187 * become empty after offline , thus we can determind we will
1188 * need to clear the node from node_states[N_NORMAL_MEMORY].
1189 */
1190 for (zt = 0; zt <= zone_last; zt++)
1191 present_pages += pgdat->node_zones[zt].present_pages;
1192 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1193 arg->status_change_nid_normal = zone_to_nid(zone);
1194 else
1195 arg->status_change_nid_normal = -1;
1196
6715ddf9
LJ
1197#ifdef CONFIG_HIGHMEM
1198 /*
1199 * If we have movable node, node_states[N_HIGH_MEMORY]
1200 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1201 * set zone_last to ZONE_HIGHMEM.
1202 *
1203 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1204 * contains nodes which have zones of 0...ZONE_MOVABLE,
1205 * set zone_last to ZONE_MOVABLE.
1206 */
1207 zone_last = ZONE_HIGHMEM;
1208 if (N_MEMORY == N_HIGH_MEMORY)
1209 zone_last = ZONE_MOVABLE;
1210
1211 for (; zt <= zone_last; zt++)
1212 present_pages += pgdat->node_zones[zt].present_pages;
1213 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1214 arg->status_change_nid_high = zone_to_nid(zone);
1215 else
1216 arg->status_change_nid_high = -1;
1217#else
1218 arg->status_change_nid_high = arg->status_change_nid_normal;
1219#endif
1220
d9713679
LJ
1221 /*
1222 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1223 */
1224 zone_last = ZONE_MOVABLE;
1225
1226 /*
1227 * check whether node_states[N_HIGH_MEMORY] will be changed
1228 * If we try to offline the last present @nr_pages from the node,
1229 * we can determind we will need to clear the node from
1230 * node_states[N_HIGH_MEMORY].
1231 */
1232 for (; zt <= zone_last; zt++)
1233 present_pages += pgdat->node_zones[zt].present_pages;
1234 if (nr_pages >= present_pages)
1235 arg->status_change_nid = zone_to_nid(zone);
1236 else
1237 arg->status_change_nid = -1;
1238}
1239
1240static void node_states_clear_node(int node, struct memory_notify *arg)
1241{
1242 if (arg->status_change_nid_normal >= 0)
1243 node_clear_state(node, N_NORMAL_MEMORY);
1244
6715ddf9
LJ
1245 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1246 (arg->status_change_nid_high >= 0))
d9713679 1247 node_clear_state(node, N_HIGH_MEMORY);
6715ddf9
LJ
1248
1249 if ((N_MEMORY != N_HIGH_MEMORY) &&
1250 (arg->status_change_nid >= 0))
1251 node_clear_state(node, N_MEMORY);
d9713679
LJ
1252}
1253
a16cee10 1254static int __ref __offline_pages(unsigned long start_pfn,
0c0e6195
KH
1255 unsigned long end_pfn, unsigned long timeout)
1256{
1257 unsigned long pfn, nr_pages, expire;
1258 long offlined_pages;
7b78d335 1259 int ret, drain, retry_max, node;
0c0e6195 1260 struct zone *zone;
7b78d335 1261 struct memory_notify arg;
0c0e6195
KH
1262
1263 BUG_ON(start_pfn >= end_pfn);
1264 /* at least, alignment against pageblock is necessary */
1265 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1266 return -EINVAL;
1267 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1268 return -EINVAL;
1269 /* This makes hotplug much easier...and readable.
1270 we assume this for now. .*/
1271 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1272 return -EINVAL;
7b78d335 1273
20d6c96b 1274 lock_memory_hotplug();
6ad696d2 1275
7b78d335
YG
1276 zone = page_zone(pfn_to_page(start_pfn));
1277 node = zone_to_nid(zone);
1278 nr_pages = end_pfn - start_pfn;
1279
74d42d8f
LJ
1280 ret = -EINVAL;
1281 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1282 goto out;
1283
0c0e6195 1284 /* set above range as isolated */
b023f468
WC
1285 ret = start_isolate_page_range(start_pfn, end_pfn,
1286 MIGRATE_MOVABLE, true);
0c0e6195 1287 if (ret)
6ad696d2 1288 goto out;
7b78d335
YG
1289
1290 arg.start_pfn = start_pfn;
1291 arg.nr_pages = nr_pages;
d9713679 1292 node_states_check_changes_offline(nr_pages, zone, &arg);
7b78d335
YG
1293
1294 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1295 ret = notifier_to_errno(ret);
1296 if (ret)
1297 goto failed_removal;
1298
0c0e6195
KH
1299 pfn = start_pfn;
1300 expire = jiffies + timeout;
1301 drain = 0;
1302 retry_max = 5;
1303repeat:
1304 /* start memory hot removal */
1305 ret = -EAGAIN;
1306 if (time_after(jiffies, expire))
1307 goto failed_removal;
1308 ret = -EINTR;
1309 if (signal_pending(current))
1310 goto failed_removal;
1311 ret = 0;
1312 if (drain) {
1313 lru_add_drain_all();
0c0e6195 1314 cond_resched();
9f8f2172 1315 drain_all_pages();
0c0e6195
KH
1316 }
1317
1318 pfn = scan_lru_pages(start_pfn, end_pfn);
1319 if (pfn) { /* We have page on LRU */
1320 ret = do_migrate_range(pfn, end_pfn);
1321 if (!ret) {
1322 drain = 1;
1323 goto repeat;
1324 } else {
1325 if (ret < 0)
1326 if (--retry_max == 0)
1327 goto failed_removal;
1328 yield();
1329 drain = 1;
1330 goto repeat;
1331 }
1332 }
b3834be5 1333 /* drain all zone's lru pagevec, this is asynchronous... */
0c0e6195 1334 lru_add_drain_all();
0c0e6195 1335 yield();
b3834be5 1336 /* drain pcp pages, this is synchronous. */
9f8f2172 1337 drain_all_pages();
0c0e6195
KH
1338 /* check again */
1339 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1340 if (offlined_pages < 0) {
1341 ret = -EBUSY;
1342 goto failed_removal;
1343 }
1344 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
b3834be5 1345 /* Ok, all of our target is isolated.
0c0e6195
KH
1346 We cannot do rollback at this point. */
1347 offline_isolated_pages(start_pfn, end_pfn);
dbc0e4ce 1348 /* reset pagetype flags and makes migrate type to be MOVABLE */
0815f3d8 1349 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
0c0e6195 1350 /* removal success */
9feedc9d 1351 zone->managed_pages -= offlined_pages;
0c0e6195
KH
1352 zone->present_pages -= offlined_pages;
1353 zone->zone_pgdat->node_present_pages -= offlined_pages;
1354 totalram_pages -= offlined_pages;
7b78d335 1355
1b79acc9
KM
1356 init_per_zone_wmark_min();
1357
1e8537ba 1358 if (!populated_zone(zone)) {
340175b7 1359 zone_pcp_reset(zone);
1e8537ba
XQ
1360 mutex_lock(&zonelists_mutex);
1361 build_all_zonelists(NULL, NULL);
1362 mutex_unlock(&zonelists_mutex);
1363 } else
1364 zone_pcp_update(zone);
340175b7 1365
d9713679
LJ
1366 node_states_clear_node(node, &arg);
1367 if (arg.status_change_nid >= 0)
8fe23e05 1368 kswapd_stop(node);
bce7394a 1369
0c0e6195
KH
1370 vm_total_pages = nr_free_pagecache_pages();
1371 writeback_set_ratelimit();
7b78d335
YG
1372
1373 memory_notify(MEM_OFFLINE, &arg);
20d6c96b 1374 unlock_memory_hotplug();
0c0e6195
KH
1375 return 0;
1376
1377failed_removal:
a62e2f4f
BH
1378 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1379 (unsigned long long) start_pfn << PAGE_SHIFT,
1380 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
7b78d335 1381 memory_notify(MEM_CANCEL_OFFLINE, &arg);
0c0e6195 1382 /* pushback to free area */
0815f3d8 1383 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
7b78d335 1384
6ad696d2 1385out:
20d6c96b 1386 unlock_memory_hotplug();
0c0e6195
KH
1387 return ret;
1388}
71088785 1389
a16cee10
WC
1390int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1391{
1392 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1393}
1394
bbc76be6
WC
1395/**
1396 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1397 * @start_pfn: start pfn of the memory range
1398 * @end_pfn: end pft of the memory range
1399 * @arg: argument passed to func
1400 * @func: callback for each memory section walked
1401 *
1402 * This function walks through all present mem sections in range
1403 * [start_pfn, end_pfn) and call func on each mem section.
1404 *
1405 * Returns the return value of func.
1406 */
1407static int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1408 void *arg, int (*func)(struct memory_block *, void *))
71088785 1409{
e90bdb7f
WC
1410 struct memory_block *mem = NULL;
1411 struct mem_section *section;
e90bdb7f
WC
1412 unsigned long pfn, section_nr;
1413 int ret;
e90bdb7f
WC
1414
1415 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1416 section_nr = pfn_to_section_nr(pfn);
1417 if (!present_section_nr(section_nr))
1418 continue;
1419
1420 section = __nr_to_section(section_nr);
1421 /* same memblock? */
1422 if (mem)
1423 if ((section_nr >= mem->start_section_nr) &&
1424 (section_nr <= mem->end_section_nr))
1425 continue;
1426
1427 mem = find_memory_block_hinted(section, mem);
1428 if (!mem)
1429 continue;
1430
bbc76be6 1431 ret = func(mem, arg);
e90bdb7f 1432 if (ret) {
bbc76be6
WC
1433 kobject_put(&mem->dev.kobj);
1434 return ret;
e90bdb7f
WC
1435 }
1436 }
1437
1438 if (mem)
1439 kobject_put(&mem->dev.kobj);
1440
bbc76be6
WC
1441 return 0;
1442}
1443
1444/**
1445 * offline_memory_block_cb - callback function for offlining memory block
1446 * @mem: the memory block to be offlined
1447 * @arg: buffer to hold error msg
1448 *
1449 * Always return 0, and put the error msg in arg if any.
1450 */
1451static int offline_memory_block_cb(struct memory_block *mem, void *arg)
1452{
1453 int *ret = arg;
1454 int error = offline_memory_block(mem);
1455
1456 if (error != 0 && *ret == 0)
1457 *ret = error;
1458
1459 return 0;
1460}
1461
1462static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)
1463{
1464 int ret = !is_memblock_offlined(mem);
1465
1466 if (unlikely(ret))
1467 pr_warn("removing memory fails, because memory "
1468 "[%#010llx-%#010llx] is onlined\n",
1469 PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),
1470 PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1);
1471
1472 return ret;
1473}
1474
46c66c4b 1475int __ref remove_memory(u64 start, u64 size)
bbc76be6
WC
1476{
1477 unsigned long start_pfn, end_pfn;
1478 int ret = 0;
1479 int retry = 1;
1480
1481 start_pfn = PFN_DOWN(start);
1482 end_pfn = start_pfn + PFN_DOWN(size);
1483
1484 /*
1485 * When CONFIG_MEMCG is on, one memory block may be used by other
1486 * blocks to store page cgroup when onlining pages. But we don't know
1487 * in what order pages are onlined. So we iterate twice to offline
1488 * memory:
1489 * 1st iterate: offline every non primary memory block.
1490 * 2nd iterate: offline primary (i.e. first added) memory block.
1491 */
1492repeat:
1493 walk_memory_range(start_pfn, end_pfn, &ret,
1494 offline_memory_block_cb);
1495 if (ret) {
1496 if (!retry)
1497 return ret;
1498
1499 retry = 0;
1500 ret = 0;
993c1aad
WC
1501 goto repeat;
1502 }
1503
6677e3ea
YI
1504 lock_memory_hotplug();
1505
1506 /*
1507 * we have offlined all memory blocks like this:
1508 * 1. lock memory hotplug
1509 * 2. offline a memory block
1510 * 3. unlock memory hotplug
1511 *
1512 * repeat step1-3 to offline the memory block. All memory blocks
1513 * must be offlined before removing memory. But we don't hold the
1514 * lock in the whole operation. So we should check whether all
1515 * memory blocks are offlined.
1516 */
1517
bbc76be6
WC
1518 ret = walk_memory_range(start_pfn, end_pfn, NULL,
1519 is_memblock_offlined_cb);
1520 if (ret) {
1521 unlock_memory_hotplug();
1522 return ret;
6677e3ea
YI
1523 }
1524
46c66c4b
YI
1525 /* remove memmap entry */
1526 firmware_map_remove(start, start + size, "System RAM");
1527
24d335ca
WC
1528 arch_remove_memory(start, size);
1529
6677e3ea
YI
1530 unlock_memory_hotplug();
1531
e90bdb7f 1532 return 0;
71088785 1533}
48e94196 1534#else
a16cee10
WC
1535int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1536{
1537 return -EINVAL;
1538}
48e94196
KH
1539int remove_memory(u64 start, u64 size)
1540{
1541 return -EINVAL;
1542}
0c0e6195 1543#endif /* CONFIG_MEMORY_HOTREMOVE */
71088785 1544EXPORT_SYMBOL_GPL(remove_memory);