1
2
3
4
5
6
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/compiler.h>
13#include <linux/export.h>
14#include <linux/pagevec.h>
15#include <linux/writeback.h>
16#include <linux/slab.h>
17#include <linux/sysctl.h>
18#include <linux/cpu.h>
19#include <linux/memory.h>
20#include <linux/memory_hotplug.h>
21#include <linux/highmem.h>
22#include <linux/vmalloc.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
25#include <linux/migrate.h>
26#include <linux/page-isolation.h>
27#include <linux/pfn.h>
28#include <linux/suspend.h>
29#include <linux/mm_inline.h>
30#include <linux/firmware-map.h>
31#include <linux/stop_machine.h>
32#include <linux/hugetlb.h>
33#include <linux/memblock.h>
34#include <linux/bootmem.h>
35
36#include <asm/tlbflush.h>
37
38#include "internal.h"
39
40
41
42
43
44
45
46
47static void generic_online_page(struct page *page);
48
49static online_page_callback_t online_page_callback = generic_online_page;
50static DEFINE_MUTEX(online_page_callback_lock);
51
52
53static struct {
54 struct task_struct *active_writer;
55 struct mutex lock;
56
57
58
59
60 int refcount;
61
62#ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map;
64#endif
65} mem_hotplug = {
66 .active_writer = NULL,
67 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
68 .refcount = 0,
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map = {.name = "mem_hotplug.lock" },
71#endif
72};
73
74
75#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
76#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
77#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
78
79void get_online_mems(void)
80{
81 might_sleep();
82 if (mem_hotplug.active_writer == current)
83 return;
84 memhp_lock_acquire_read();
85 mutex_lock(&mem_hotplug.lock);
86 mem_hotplug.refcount++;
87 mutex_unlock(&mem_hotplug.lock);
88
89}
90
91void put_online_mems(void)
92{
93 if (mem_hotplug.active_writer == current)
94 return;
95 mutex_lock(&mem_hotplug.lock);
96
97 if (WARN_ON(!mem_hotplug.refcount))
98 mem_hotplug.refcount++;
99
100 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
101 wake_up_process(mem_hotplug.active_writer);
102 mutex_unlock(&mem_hotplug.lock);
103 memhp_lock_release();
104
105}
106
107static void mem_hotplug_begin(void)
108{
109 mem_hotplug.active_writer = current;
110
111 memhp_lock_acquire();
112 for (;;) {
113 mutex_lock(&mem_hotplug.lock);
114 if (likely(!mem_hotplug.refcount))
115 break;
116 __set_current_state(TASK_UNINTERRUPTIBLE);
117 mutex_unlock(&mem_hotplug.lock);
118 schedule();
119 }
120}
121
122static void mem_hotplug_done(void)
123{
124 mem_hotplug.active_writer = NULL;
125 mutex_unlock(&mem_hotplug.lock);
126 memhp_lock_release();
127}
128
129
130static struct resource *register_memory_resource(u64 start, u64 size)
131{
132 struct resource *res;
133 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
134 BUG_ON(!res);
135
136 res->name = "System RAM";
137 res->start = start;
138 res->end = start + size - 1;
139 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
140 if (request_resource(&iomem_resource, res) < 0) {
141 pr_debug("System RAM resource %pR cannot be added\n", res);
142 kfree(res);
143 res = NULL;
144 }
145 return res;
146}
147
148static void release_memory_resource(struct resource *res)
149{
150 if (!res)
151 return;
152 release_resource(res);
153 kfree(res);
154 return;
155}
156
157#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
158void get_page_bootmem(unsigned long info, struct page *page,
159 unsigned long type)
160{
161 page->lru.next = (struct list_head *) type;
162 SetPagePrivate(page);
163 set_page_private(page, info);
164 atomic_inc(&page->_count);
165}
166
167void put_page_bootmem(struct page *page)
168{
169 unsigned long type;
170
171 type = (unsigned long) page->lru.next;
172 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
173 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
174
175 if (atomic_dec_return(&page->_count) == 1) {
176 ClearPagePrivate(page);
177 set_page_private(page, 0);
178 INIT_LIST_HEAD(&page->lru);
179 free_reserved_page(page);
180 }
181}
182
183#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
184#ifndef CONFIG_SPARSEMEM_VMEMMAP
185static void register_page_bootmem_info_section(unsigned long start_pfn)
186{
187 unsigned long *usemap, mapsize, section_nr, i;
188 struct mem_section *ms;
189 struct page *page, *memmap;
190
191 section_nr = pfn_to_section_nr(start_pfn);
192 ms = __nr_to_section(section_nr);
193
194
195 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
196
197
198
199
200
201 page = virt_to_page(memmap);
202 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
203 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
204
205
206 for (i = 0; i < mapsize; i++, page++)
207 get_page_bootmem(section_nr, page, SECTION_INFO);
208
209 usemap = __nr_to_section(section_nr)->pageblock_flags;
210 page = virt_to_page(usemap);
211
212 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
213
214 for (i = 0; i < mapsize; i++, page++)
215 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
216
217}
218#else
219static void register_page_bootmem_info_section(unsigned long start_pfn)
220{
221 unsigned long *usemap, mapsize, section_nr, i;
222 struct mem_section *ms;
223 struct page *page, *memmap;
224
225 if (!pfn_valid(start_pfn))
226 return;
227
228 section_nr = pfn_to_section_nr(start_pfn);
229 ms = __nr_to_section(section_nr);
230
231 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
232
233 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
234
235 usemap = __nr_to_section(section_nr)->pageblock_flags;
236 page = virt_to_page(usemap);
237
238 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
239
240 for (i = 0; i < mapsize; i++, page++)
241 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
242}
243#endif
244
245void register_page_bootmem_info_node(struct pglist_data *pgdat)
246{
247 unsigned long i, pfn, end_pfn, nr_pages;
248 int node = pgdat->node_id;
249 struct page *page;
250 struct zone *zone;
251
252 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
253 page = virt_to_page(pgdat);
254
255 for (i = 0; i < nr_pages; i++, page++)
256 get_page_bootmem(node, page, NODE_INFO);
257
258 zone = &pgdat->node_zones[0];
259 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
260 if (zone_is_initialized(zone)) {
261 nr_pages = zone->wait_table_hash_nr_entries
262 * sizeof(wait_queue_head_t);
263 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
264 page = virt_to_page(zone->wait_table);
265
266 for (i = 0; i < nr_pages; i++, page++)
267 get_page_bootmem(node, page, NODE_INFO);
268 }
269 }
270
271 pfn = pgdat->node_start_pfn;
272 end_pfn = pgdat_end_pfn(pgdat);
273
274
275 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
276
277
278
279
280
281
282 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
283 register_page_bootmem_info_section(pfn);
284 }
285}
286#endif
287
288static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
289 unsigned long end_pfn)
290{
291 unsigned long old_zone_end_pfn;
292
293 zone_span_writelock(zone);
294
295 old_zone_end_pfn = zone_end_pfn(zone);
296 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
297 zone->zone_start_pfn = start_pfn;
298
299 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
300 zone->zone_start_pfn;
301
302 zone_span_writeunlock(zone);
303}
304
305static void resize_zone(struct zone *zone, unsigned long start_pfn,
306 unsigned long end_pfn)
307{
308 zone_span_writelock(zone);
309
310 if (end_pfn - start_pfn) {
311 zone->zone_start_pfn = start_pfn;
312 zone->spanned_pages = end_pfn - start_pfn;
313 } else {
314
315
316
317
318 zone->zone_start_pfn = 0;
319 zone->spanned_pages = 0;
320 }
321
322 zone_span_writeunlock(zone);
323}
324
325static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
326 unsigned long end_pfn)
327{
328 enum zone_type zid = zone_idx(zone);
329 int nid = zone->zone_pgdat->node_id;
330 unsigned long pfn;
331
332 for (pfn = start_pfn; pfn < end_pfn; pfn++)
333 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
334}
335
336
337
338static int __ref ensure_zone_is_initialized(struct zone *zone,
339 unsigned long start_pfn, unsigned long num_pages)
340{
341 if (!zone_is_initialized(zone))
342 return init_currently_empty_zone(zone, start_pfn, num_pages,
343 MEMMAP_HOTPLUG);
344 return 0;
345}
346
347static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
348 unsigned long start_pfn, unsigned long end_pfn)
349{
350 int ret;
351 unsigned long flags;
352 unsigned long z1_start_pfn;
353
354 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
355 if (ret)
356 return ret;
357
358 pgdat_resize_lock(z1->zone_pgdat, &flags);
359
360
361 if (end_pfn > zone_end_pfn(z2))
362 goto out_fail;
363
364 if (start_pfn > z2->zone_start_pfn)
365 goto out_fail;
366
367 if (end_pfn <= z2->zone_start_pfn)
368 goto out_fail;
369
370
371 if (!zone_is_empty(z1))
372 z1_start_pfn = z1->zone_start_pfn;
373 else
374 z1_start_pfn = start_pfn;
375
376 resize_zone(z1, z1_start_pfn, end_pfn);
377 resize_zone(z2, end_pfn, zone_end_pfn(z2));
378
379 pgdat_resize_unlock(z1->zone_pgdat, &flags);
380
381 fix_zone_id(z1, start_pfn, end_pfn);
382
383 return 0;
384out_fail:
385 pgdat_resize_unlock(z1->zone_pgdat, &flags);
386 return -1;
387}
388
389static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
390 unsigned long start_pfn, unsigned long end_pfn)
391{
392 int ret;
393 unsigned long flags;
394 unsigned long z2_end_pfn;
395
396 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
397 if (ret)
398 return ret;
399
400 pgdat_resize_lock(z1->zone_pgdat, &flags);
401
402
403 if (z1->zone_start_pfn > start_pfn)
404 goto out_fail;
405
406 if (zone_end_pfn(z1) > end_pfn)
407 goto out_fail;
408
409 if (start_pfn >= zone_end_pfn(z1))
410 goto out_fail;
411
412
413 if (!zone_is_empty(z2))
414 z2_end_pfn = zone_end_pfn(z2);
415 else
416 z2_end_pfn = end_pfn;
417
418 resize_zone(z1, z1->zone_start_pfn, start_pfn);
419 resize_zone(z2, start_pfn, z2_end_pfn);
420
421 pgdat_resize_unlock(z1->zone_pgdat, &flags);
422
423 fix_zone_id(z2, start_pfn, end_pfn);
424
425 return 0;
426out_fail:
427 pgdat_resize_unlock(z1->zone_pgdat, &flags);
428 return -1;
429}
430
431static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
432 unsigned long end_pfn)
433{
434 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
435
436 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
437 pgdat->node_start_pfn = start_pfn;
438
439 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
440 pgdat->node_start_pfn;
441}
442
443static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
444{
445 struct pglist_data *pgdat = zone->zone_pgdat;
446 int nr_pages = PAGES_PER_SECTION;
447 int nid = pgdat->node_id;
448 int zone_type;
449 unsigned long flags;
450 int ret;
451
452 zone_type = zone - pgdat->node_zones;
453 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
454 if (ret)
455 return ret;
456
457 pgdat_resize_lock(zone->zone_pgdat, &flags);
458 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
459 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
460 phys_start_pfn + nr_pages);
461 pgdat_resize_unlock(zone->zone_pgdat, &flags);
462 memmap_init_zone(nr_pages, nid, zone_type,
463 phys_start_pfn, MEMMAP_HOTPLUG);
464 return 0;
465}
466
467static int __meminit __add_section(int nid, struct zone *zone,
468 unsigned long phys_start_pfn)
469{
470 int ret;
471
472 if (pfn_valid(phys_start_pfn))
473 return -EEXIST;
474
475 ret = sparse_add_one_section(zone, phys_start_pfn);
476
477 if (ret < 0)
478 return ret;
479
480 ret = __add_zone(zone, phys_start_pfn);
481
482 if (ret < 0)
483 return ret;
484
485 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
486}
487
488
489
490
491
492
493
494int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
495 unsigned long nr_pages)
496{
497 unsigned long i;
498 int err = 0;
499 int start_sec, end_sec;
500
501 start_sec = pfn_to_section_nr(phys_start_pfn);
502 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
503
504 for (i = start_sec; i <= end_sec; i++) {
505 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
506
507
508
509
510
511
512 if (err && (err != -EEXIST))
513 break;
514 err = 0;
515 }
516
517 return err;
518}
519EXPORT_SYMBOL_GPL(__add_pages);
520
521#ifdef CONFIG_MEMORY_HOTREMOVE
522
523static int find_smallest_section_pfn(int nid, struct zone *zone,
524 unsigned long start_pfn,
525 unsigned long end_pfn)
526{
527 struct mem_section *ms;
528
529 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
530 ms = __pfn_to_section(start_pfn);
531
532 if (unlikely(!valid_section(ms)))
533 continue;
534
535 if (unlikely(pfn_to_nid(start_pfn) != nid))
536 continue;
537
538 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
539 continue;
540
541 return start_pfn;
542 }
543
544 return 0;
545}
546
547
548static int find_biggest_section_pfn(int nid, struct zone *zone,
549 unsigned long start_pfn,
550 unsigned long end_pfn)
551{
552 struct mem_section *ms;
553 unsigned long pfn;
554
555
556 pfn = end_pfn - 1;
557 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
558 ms = __pfn_to_section(pfn);
559
560 if (unlikely(!valid_section(ms)))
561 continue;
562
563 if (unlikely(pfn_to_nid(pfn) != nid))
564 continue;
565
566 if (zone && zone != page_zone(pfn_to_page(pfn)))
567 continue;
568
569 return pfn;
570 }
571
572 return 0;
573}
574
575static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
576 unsigned long end_pfn)
577{
578 unsigned long zone_start_pfn = zone->zone_start_pfn;
579 unsigned long z = zone_end_pfn(zone);
580 unsigned long zone_end_pfn = z;
581 unsigned long pfn;
582 struct mem_section *ms;
583 int nid = zone_to_nid(zone);
584
585 zone_span_writelock(zone);
586 if (zone_start_pfn == start_pfn) {
587
588
589
590
591
592
593 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
594 zone_end_pfn);
595 if (pfn) {
596 zone->zone_start_pfn = pfn;
597 zone->spanned_pages = zone_end_pfn - pfn;
598 }
599 } else if (zone_end_pfn == end_pfn) {
600
601
602
603
604
605
606 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
607 start_pfn);
608 if (pfn)
609 zone->spanned_pages = pfn - zone_start_pfn + 1;
610 }
611
612
613
614
615
616
617
618 pfn = zone_start_pfn;
619 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
620 ms = __pfn_to_section(pfn);
621
622 if (unlikely(!valid_section(ms)))
623 continue;
624
625 if (page_zone(pfn_to_page(pfn)) != zone)
626 continue;
627
628
629 if (start_pfn == pfn)
630 continue;
631
632
633 zone_span_writeunlock(zone);
634 return;
635 }
636
637
638 zone->zone_start_pfn = 0;
639 zone->spanned_pages = 0;
640 zone_span_writeunlock(zone);
641}
642
643static void shrink_pgdat_span(struct pglist_data *pgdat,
644 unsigned long start_pfn, unsigned long end_pfn)
645{
646 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
647 unsigned long p = pgdat_end_pfn(pgdat);
648 unsigned long pgdat_end_pfn = p;
649 unsigned long pfn;
650 struct mem_section *ms;
651 int nid = pgdat->node_id;
652
653 if (pgdat_start_pfn == start_pfn) {
654
655
656
657
658
659
660 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
661 pgdat_end_pfn);
662 if (pfn) {
663 pgdat->node_start_pfn = pfn;
664 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
665 }
666 } else if (pgdat_end_pfn == end_pfn) {
667
668
669
670
671
672
673 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
674 start_pfn);
675 if (pfn)
676 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
677 }
678
679
680
681
682
683
684
685
686 pfn = pgdat_start_pfn;
687 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
688 ms = __pfn_to_section(pfn);
689
690 if (unlikely(!valid_section(ms)))
691 continue;
692
693 if (pfn_to_nid(pfn) != nid)
694 continue;
695
696
697 if (start_pfn == pfn)
698 continue;
699
700
701 return;
702 }
703
704
705 pgdat->node_start_pfn = 0;
706 pgdat->node_spanned_pages = 0;
707}
708
709static void __remove_zone(struct zone *zone, unsigned long start_pfn)
710{
711 struct pglist_data *pgdat = zone->zone_pgdat;
712 int nr_pages = PAGES_PER_SECTION;
713 int zone_type;
714 unsigned long flags;
715
716 zone_type = zone - pgdat->node_zones;
717
718 pgdat_resize_lock(zone->zone_pgdat, &flags);
719 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
720 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
721 pgdat_resize_unlock(zone->zone_pgdat, &flags);
722}
723
724static int __remove_section(struct zone *zone, struct mem_section *ms)
725{
726 unsigned long start_pfn;
727 int scn_nr;
728 int ret = -EINVAL;
729
730 if (!valid_section(ms))
731 return ret;
732
733 ret = unregister_memory_section(ms);
734 if (ret)
735 return ret;
736
737 scn_nr = __section_nr(ms);
738 start_pfn = section_nr_to_pfn(scn_nr);
739 __remove_zone(zone, start_pfn);
740
741 sparse_remove_one_section(zone, ms);
742 return 0;
743}
744
745
746
747
748
749
750
751
752
753
754
755
756int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
757 unsigned long nr_pages)
758{
759 unsigned long i;
760 int sections_to_remove;
761 resource_size_t start, size;
762 int ret = 0;
763
764
765
766
767 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
768 BUG_ON(nr_pages % PAGES_PER_SECTION);
769
770 start = phys_start_pfn << PAGE_SHIFT;
771 size = nr_pages * PAGE_SIZE;
772 ret = release_mem_region_adjustable(&iomem_resource, start, size);
773 if (ret) {
774 resource_size_t endres = start + size - 1;
775
776 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
777 &start, &endres, ret);
778 }
779
780 sections_to_remove = nr_pages / PAGES_PER_SECTION;
781 for (i = 0; i < sections_to_remove; i++) {
782 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
783 ret = __remove_section(zone, __pfn_to_section(pfn));
784 if (ret)
785 break;
786 }
787 return ret;
788}
789EXPORT_SYMBOL_GPL(__remove_pages);
790#endif
791
792int set_online_page_callback(online_page_callback_t callback)
793{
794 int rc = -EINVAL;
795
796 get_online_mems();
797 mutex_lock(&online_page_callback_lock);
798
799 if (online_page_callback == generic_online_page) {
800 online_page_callback = callback;
801 rc = 0;
802 }
803
804 mutex_unlock(&online_page_callback_lock);
805 put_online_mems();
806
807 return rc;
808}
809EXPORT_SYMBOL_GPL(set_online_page_callback);
810
811int restore_online_page_callback(online_page_callback_t callback)
812{
813 int rc = -EINVAL;
814
815 get_online_mems();
816 mutex_lock(&online_page_callback_lock);
817
818 if (online_page_callback == callback) {
819 online_page_callback = generic_online_page;
820 rc = 0;
821 }
822
823 mutex_unlock(&online_page_callback_lock);
824 put_online_mems();
825
826 return rc;
827}
828EXPORT_SYMBOL_GPL(restore_online_page_callback);
829
830void __online_page_set_limits(struct page *page)
831{
832}
833EXPORT_SYMBOL_GPL(__online_page_set_limits);
834
835void __online_page_increment_counters(struct page *page)
836{
837 adjust_managed_page_count(page, 1);
838}
839EXPORT_SYMBOL_GPL(__online_page_increment_counters);
840
841void __online_page_free(struct page *page)
842{
843 __free_reserved_page(page);
844}
845EXPORT_SYMBOL_GPL(__online_page_free);
846
847static void generic_online_page(struct page *page)
848{
849 __online_page_set_limits(page);
850 __online_page_increment_counters(page);
851 __online_page_free(page);
852}
853
854static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
855 void *arg)
856{
857 unsigned long i;
858 unsigned long onlined_pages = *(unsigned long *)arg;
859 struct page *page;
860 if (PageReserved(pfn_to_page(start_pfn)))
861 for (i = 0; i < nr_pages; i++) {
862 page = pfn_to_page(start_pfn + i);
863 (*online_page_callback)(page);
864 onlined_pages++;
865 }
866 *(unsigned long *)arg = onlined_pages;
867 return 0;
868}
869
870#ifdef CONFIG_MOVABLE_NODE
871
872
873
874
875static bool can_online_high_movable(struct zone *zone)
876{
877 return true;
878}
879#else
880
881static bool can_online_high_movable(struct zone *zone)
882{
883 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
884}
885#endif
886
887
888static void node_states_check_changes_online(unsigned long nr_pages,
889 struct zone *zone, struct memory_notify *arg)
890{
891 int nid = zone_to_nid(zone);
892 enum zone_type zone_last = ZONE_NORMAL;
893
894
895
896
897
898
899
900
901
902
903 if (N_MEMORY == N_NORMAL_MEMORY)
904 zone_last = ZONE_MOVABLE;
905
906
907
908
909
910
911
912 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
913 arg->status_change_nid_normal = nid;
914 else
915 arg->status_change_nid_normal = -1;
916
917#ifdef CONFIG_HIGHMEM
918
919
920
921
922
923
924
925
926
927 zone_last = ZONE_HIGHMEM;
928 if (N_MEMORY == N_HIGH_MEMORY)
929 zone_last = ZONE_MOVABLE;
930
931 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
932 arg->status_change_nid_high = nid;
933 else
934 arg->status_change_nid_high = -1;
935#else
936 arg->status_change_nid_high = arg->status_change_nid_normal;
937#endif
938
939
940
941
942
943
944 if (!node_state(nid, N_MEMORY))
945 arg->status_change_nid = nid;
946 else
947 arg->status_change_nid = -1;
948}
949
950static void node_states_set_node(int node, struct memory_notify *arg)
951{
952 if (arg->status_change_nid_normal >= 0)
953 node_set_state(node, N_NORMAL_MEMORY);
954
955 if (arg->status_change_nid_high >= 0)
956 node_set_state(node, N_HIGH_MEMORY);
957
958 node_set_state(node, N_MEMORY);
959}
960
961
962int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
963{
964 unsigned long flags;
965 unsigned long onlined_pages = 0;
966 struct zone *zone;
967 int need_zonelists_rebuild = 0;
968 int nid;
969 int ret;
970 struct memory_notify arg;
971
972 mem_hotplug_begin();
973
974
975
976
977
978 zone = page_zone(pfn_to_page(pfn));
979
980 ret = -EINVAL;
981 if ((zone_idx(zone) > ZONE_NORMAL ||
982 online_type == MMOP_ONLINE_MOVABLE) &&
983 !can_online_high_movable(zone))
984 goto out;
985
986 if (online_type == MMOP_ONLINE_KERNEL &&
987 zone_idx(zone) == ZONE_MOVABLE) {
988 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
989 goto out;
990 }
991 if (online_type == MMOP_ONLINE_MOVABLE &&
992 zone_idx(zone) == ZONE_MOVABLE - 1) {
993 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
994 goto out;
995 }
996
997
998 zone = page_zone(pfn_to_page(pfn));
999
1000 arg.start_pfn = pfn;
1001 arg.nr_pages = nr_pages;
1002 node_states_check_changes_online(nr_pages, zone, &arg);
1003
1004 nid = pfn_to_nid(pfn);
1005
1006 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1007 ret = notifier_to_errno(ret);
1008 if (ret) {
1009 memory_notify(MEM_CANCEL_ONLINE, &arg);
1010 goto out;
1011 }
1012
1013
1014
1015
1016
1017 mutex_lock(&zonelists_mutex);
1018 if (!populated_zone(zone)) {
1019 need_zonelists_rebuild = 1;
1020 build_all_zonelists(NULL, zone);
1021 }
1022
1023 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1024 online_pages_range);
1025 if (ret) {
1026 if (need_zonelists_rebuild)
1027 zone_pcp_reset(zone);
1028 mutex_unlock(&zonelists_mutex);
1029 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1030 (unsigned long long) pfn << PAGE_SHIFT,
1031 (((unsigned long long) pfn + nr_pages)
1032 << PAGE_SHIFT) - 1);
1033 memory_notify(MEM_CANCEL_ONLINE, &arg);
1034 goto out;
1035 }
1036
1037 zone->present_pages += onlined_pages;
1038
1039 pgdat_resize_lock(zone->zone_pgdat, &flags);
1040 zone->zone_pgdat->node_present_pages += onlined_pages;
1041 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1042
1043 if (onlined_pages) {
1044 node_states_set_node(zone_to_nid(zone), &arg);
1045 if (need_zonelists_rebuild)
1046 build_all_zonelists(NULL, NULL);
1047 else
1048 zone_pcp_update(zone);
1049 }
1050
1051 mutex_unlock(&zonelists_mutex);
1052
1053 init_per_zone_wmark_min();
1054
1055 if (onlined_pages)
1056 kswapd_run(zone_to_nid(zone));
1057
1058 vm_total_pages = nr_free_pagecache_pages();
1059
1060 writeback_set_ratelimit();
1061
1062 if (onlined_pages)
1063 memory_notify(MEM_ONLINE, &arg);
1064out:
1065 mem_hotplug_done();
1066 return ret;
1067}
1068#endif
1069
1070static void reset_node_present_pages(pg_data_t *pgdat)
1071{
1072 struct zone *z;
1073
1074 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1075 z->present_pages = 0;
1076
1077 pgdat->node_present_pages = 0;
1078}
1079
1080
1081static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1082{
1083 struct pglist_data *pgdat;
1084 unsigned long zones_size[MAX_NR_ZONES] = {0};
1085 unsigned long zholes_size[MAX_NR_ZONES] = {0};
1086 unsigned long start_pfn = PFN_DOWN(start);
1087
1088 pgdat = NODE_DATA(nid);
1089 if (!pgdat) {
1090 pgdat = arch_alloc_nodedata(nid);
1091 if (!pgdat)
1092 return NULL;
1093
1094 arch_refresh_nodedata(nid, pgdat);
1095 }
1096
1097
1098
1099
1100 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1101
1102
1103
1104
1105
1106 mutex_lock(&zonelists_mutex);
1107 build_all_zonelists(pgdat, NULL);
1108 mutex_unlock(&zonelists_mutex);
1109
1110
1111
1112
1113
1114
1115
1116 reset_node_managed_pages(pgdat);
1117
1118
1119
1120
1121
1122
1123 reset_node_present_pages(pgdat);
1124
1125 return pgdat;
1126}
1127
1128static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1129{
1130 arch_refresh_nodedata(nid, NULL);
1131 arch_free_nodedata(pgdat);
1132 return;
1133}
1134
1135
1136
1137
1138
1139
1140
1141int try_online_node(int nid)
1142{
1143 pg_data_t *pgdat;
1144 int ret;
1145
1146 if (node_online(nid))
1147 return 0;
1148
1149 mem_hotplug_begin();
1150 pgdat = hotadd_new_pgdat(nid, 0);
1151 if (!pgdat) {
1152 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1153 ret = -ENOMEM;
1154 goto out;
1155 }
1156 node_set_online(nid);
1157 ret = register_one_node(nid);
1158 BUG_ON(ret);
1159
1160 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1161 mutex_lock(&zonelists_mutex);
1162 build_all_zonelists(NULL, NULL);
1163 mutex_unlock(&zonelists_mutex);
1164 }
1165
1166out:
1167 mem_hotplug_done();
1168 return ret;
1169}
1170
1171static int check_hotplug_memory_range(u64 start, u64 size)
1172{
1173 u64 start_pfn = PFN_DOWN(start);
1174 u64 nr_pages = size >> PAGE_SHIFT;
1175
1176
1177 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1178 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1179 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1180 (unsigned long long)start,
1181 (unsigned long long)size);
1182 return -EINVAL;
1183 }
1184
1185 return 0;
1186}
1187
1188
1189
1190
1191
1192
1193static int should_add_memory_movable(int nid, u64 start, u64 size)
1194{
1195 unsigned long start_pfn = start >> PAGE_SHIFT;
1196 pg_data_t *pgdat = NODE_DATA(nid);
1197 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1198
1199 if (zone_is_empty(movable_zone))
1200 return 0;
1201
1202 if (movable_zone->zone_start_pfn <= start_pfn)
1203 return 1;
1204
1205 return 0;
1206}
1207
1208int zone_for_memory(int nid, u64 start, u64 size, int zone_default)
1209{
1210 if (should_add_memory_movable(nid, start, size))
1211 return ZONE_MOVABLE;
1212
1213 return zone_default;
1214}
1215
1216
1217int __ref add_memory(int nid, u64 start, u64 size)
1218{
1219 pg_data_t *pgdat = NULL;
1220 bool new_pgdat;
1221 bool new_node;
1222 struct resource *res;
1223 int ret;
1224
1225 ret = check_hotplug_memory_range(start, size);
1226 if (ret)
1227 return ret;
1228
1229 res = register_memory_resource(start, size);
1230 ret = -EEXIST;
1231 if (!res)
1232 return ret;
1233
1234 {
1235 void *p = NODE_DATA(nid);
1236 new_pgdat = !p;
1237 }
1238
1239 mem_hotplug_begin();
1240
1241 new_node = !node_online(nid);
1242 if (new_node) {
1243 pgdat = hotadd_new_pgdat(nid, start);
1244 ret = -ENOMEM;
1245 if (!pgdat)
1246 goto error;
1247 }
1248
1249
1250 ret = arch_add_memory(nid, start, size);
1251
1252 if (ret < 0)
1253 goto error;
1254
1255
1256 node_set_online(nid);
1257
1258 if (new_node) {
1259 ret = register_one_node(nid);
1260
1261
1262
1263
1264
1265 BUG_ON(ret);
1266 }
1267
1268
1269 firmware_map_add_hotplug(start, start + size, "System RAM");
1270
1271 goto out;
1272
1273error:
1274
1275 if (new_pgdat)
1276 rollback_node_hotadd(nid, pgdat);
1277 release_memory_resource(res);
1278
1279out:
1280 mem_hotplug_done();
1281 return ret;
1282}
1283EXPORT_SYMBOL_GPL(add_memory);
1284
1285#ifdef CONFIG_MEMORY_HOTREMOVE
1286
1287
1288
1289
1290
1291
1292
1293static inline int pageblock_free(struct page *page)
1294{
1295 return PageBuddy(page) && page_order(page) >= pageblock_order;
1296}
1297
1298
1299static struct page *next_active_pageblock(struct page *page)
1300{
1301
1302 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1303
1304
1305 if (pageblock_free(page)) {
1306 int order;
1307
1308 order = page_order(page);
1309 if ((order < MAX_ORDER) && (order >= pageblock_order))
1310 return page + (1 << order);
1311 }
1312
1313 return page + pageblock_nr_pages;
1314}
1315
1316
1317int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1318{
1319 struct page *page = pfn_to_page(start_pfn);
1320 struct page *end_page = page + nr_pages;
1321
1322
1323 for (; page < end_page; page = next_active_pageblock(page)) {
1324 if (!is_pageblock_removable_nolock(page))
1325 return 0;
1326 cond_resched();
1327 }
1328
1329
1330 return 1;
1331}
1332
1333
1334
1335
1336int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1337{
1338 unsigned long pfn;
1339 struct zone *zone = NULL;
1340 struct page *page;
1341 int i;
1342 for (pfn = start_pfn;
1343 pfn < end_pfn;
1344 pfn += MAX_ORDER_NR_PAGES) {
1345 i = 0;
1346
1347 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1348 i++;
1349 if (i == MAX_ORDER_NR_PAGES)
1350 continue;
1351 page = pfn_to_page(pfn + i);
1352 if (zone && page_zone(page) != zone)
1353 return 0;
1354 zone = page_zone(page);
1355 }
1356 return 1;
1357}
1358
1359
1360
1361
1362
1363
1364
1365static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1366{
1367 unsigned long pfn;
1368 struct page *page;
1369 for (pfn = start; pfn < end; pfn++) {
1370 if (pfn_valid(pfn)) {
1371 page = pfn_to_page(pfn);
1372 if (PageLRU(page))
1373 return pfn;
1374 if (PageHuge(page)) {
1375 if (is_hugepage_active(page))
1376 return pfn;
1377 else
1378 pfn = round_up(pfn + 1,
1379 1 << compound_order(page)) - 1;
1380 }
1381 }
1382 }
1383 return 0;
1384}
1385
1386#define NR_OFFLINE_AT_ONCE_PAGES (256)
1387static int
1388do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1389{
1390 unsigned long pfn;
1391 struct page *page;
1392 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1393 int not_managed = 0;
1394 int ret = 0;
1395 LIST_HEAD(source);
1396
1397 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1398 if (!pfn_valid(pfn))
1399 continue;
1400 page = pfn_to_page(pfn);
1401
1402 if (PageHuge(page)) {
1403 struct page *head = compound_head(page);
1404 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1405 if (compound_order(head) > PFN_SECTION_SHIFT) {
1406 ret = -EBUSY;
1407 break;
1408 }
1409 if (isolate_huge_page(page, &source))
1410 move_pages -= 1 << compound_order(head);
1411 continue;
1412 }
1413
1414 if (!get_page_unless_zero(page))
1415 continue;
1416
1417
1418
1419
1420 ret = isolate_lru_page(page);
1421 if (!ret) {
1422 put_page(page);
1423 list_add_tail(&page->lru, &source);
1424 move_pages--;
1425 inc_zone_page_state(page, NR_ISOLATED_ANON +
1426 page_is_file_cache(page));
1427
1428 } else {
1429#ifdef CONFIG_DEBUG_VM
1430 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1431 pfn);
1432 dump_page(page, "failed to remove from LRU");
1433#endif
1434 put_page(page);
1435
1436
1437 if (page_count(page)) {
1438 not_managed++;
1439 ret = -EBUSY;
1440 break;
1441 }
1442 }
1443 }
1444 if (!list_empty(&source)) {
1445 if (not_managed) {
1446 putback_movable_pages(&source);
1447 goto out;
1448 }
1449
1450
1451
1452
1453
1454 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
1455 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1456 if (ret)
1457 putback_movable_pages(&source);
1458 }
1459out:
1460 return ret;
1461}
1462
1463
1464
1465
1466static int
1467offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1468 void *data)
1469{
1470 __offline_isolated_pages(start, start + nr_pages);
1471 return 0;
1472}
1473
1474static void
1475offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1476{
1477 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1478 offline_isolated_pages_cb);
1479}
1480
1481
1482
1483
1484static int
1485check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1486 void *data)
1487{
1488 int ret;
1489 long offlined = *(long *)data;
1490 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1491 offlined = nr_pages;
1492 if (!ret)
1493 *(long *)data += offlined;
1494 return ret;
1495}
1496
1497static long
1498check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1499{
1500 long offlined = 0;
1501 int ret;
1502
1503 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1504 check_pages_isolated_cb);
1505 if (ret < 0)
1506 offlined = (long)ret;
1507 return offlined;
1508}
1509
1510#ifdef CONFIG_MOVABLE_NODE
1511
1512
1513
1514
1515static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1516{
1517 return true;
1518}
1519#else
1520
1521static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1522{
1523 struct pglist_data *pgdat = zone->zone_pgdat;
1524 unsigned long present_pages = 0;
1525 enum zone_type zt;
1526
1527 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1528 present_pages += pgdat->node_zones[zt].present_pages;
1529
1530 if (present_pages > nr_pages)
1531 return true;
1532
1533 present_pages = 0;
1534 for (; zt <= ZONE_MOVABLE; zt++)
1535 present_pages += pgdat->node_zones[zt].present_pages;
1536
1537
1538
1539
1540
1541 return present_pages == 0;
1542}
1543#endif
1544
1545static int __init cmdline_parse_movable_node(char *p)
1546{
1547#ifdef CONFIG_MOVABLE_NODE
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567 memblock_set_bottom_up(true);
1568 movable_node_enabled = true;
1569#else
1570 pr_warn("movable_node option not supported\n");
1571#endif
1572 return 0;
1573}
1574early_param("movable_node", cmdline_parse_movable_node);
1575
1576
1577static void node_states_check_changes_offline(unsigned long nr_pages,
1578 struct zone *zone, struct memory_notify *arg)
1579{
1580 struct pglist_data *pgdat = zone->zone_pgdat;
1581 unsigned long present_pages = 0;
1582 enum zone_type zt, zone_last = ZONE_NORMAL;
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593 if (N_MEMORY == N_NORMAL_MEMORY)
1594 zone_last = ZONE_MOVABLE;
1595
1596
1597
1598
1599
1600
1601
1602
1603 for (zt = 0; zt <= zone_last; zt++)
1604 present_pages += pgdat->node_zones[zt].present_pages;
1605 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1606 arg->status_change_nid_normal = zone_to_nid(zone);
1607 else
1608 arg->status_change_nid_normal = -1;
1609
1610#ifdef CONFIG_HIGHMEM
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620 zone_last = ZONE_HIGHMEM;
1621 if (N_MEMORY == N_HIGH_MEMORY)
1622 zone_last = ZONE_MOVABLE;
1623
1624 for (; zt <= zone_last; zt++)
1625 present_pages += pgdat->node_zones[zt].present_pages;
1626 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1627 arg->status_change_nid_high = zone_to_nid(zone);
1628 else
1629 arg->status_change_nid_high = -1;
1630#else
1631 arg->status_change_nid_high = arg->status_change_nid_normal;
1632#endif
1633
1634
1635
1636
1637 zone_last = ZONE_MOVABLE;
1638
1639
1640
1641
1642
1643
1644
1645 for (; zt <= zone_last; zt++)
1646 present_pages += pgdat->node_zones[zt].present_pages;
1647 if (nr_pages >= present_pages)
1648 arg->status_change_nid = zone_to_nid(zone);
1649 else
1650 arg->status_change_nid = -1;
1651}
1652
1653static void node_states_clear_node(int node, struct memory_notify *arg)
1654{
1655 if (arg->status_change_nid_normal >= 0)
1656 node_clear_state(node, N_NORMAL_MEMORY);
1657
1658 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1659 (arg->status_change_nid_high >= 0))
1660 node_clear_state(node, N_HIGH_MEMORY);
1661
1662 if ((N_MEMORY != N_HIGH_MEMORY) &&
1663 (arg->status_change_nid >= 0))
1664 node_clear_state(node, N_MEMORY);
1665}
1666
1667static int __ref __offline_pages(unsigned long start_pfn,
1668 unsigned long end_pfn, unsigned long timeout)
1669{
1670 unsigned long pfn, nr_pages, expire;
1671 long offlined_pages;
1672 int ret, drain, retry_max, node;
1673 unsigned long flags;
1674 struct zone *zone;
1675 struct memory_notify arg;
1676
1677
1678 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1679 return -EINVAL;
1680 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1681 return -EINVAL;
1682
1683
1684 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1685 return -EINVAL;
1686
1687 mem_hotplug_begin();
1688
1689 zone = page_zone(pfn_to_page(start_pfn));
1690 node = zone_to_nid(zone);
1691 nr_pages = end_pfn - start_pfn;
1692
1693 ret = -EINVAL;
1694 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1695 goto out;
1696
1697
1698 ret = start_isolate_page_range(start_pfn, end_pfn,
1699 MIGRATE_MOVABLE, true);
1700 if (ret)
1701 goto out;
1702
1703 arg.start_pfn = start_pfn;
1704 arg.nr_pages = nr_pages;
1705 node_states_check_changes_offline(nr_pages, zone, &arg);
1706
1707 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1708 ret = notifier_to_errno(ret);
1709 if (ret)
1710 goto failed_removal;
1711
1712 pfn = start_pfn;
1713 expire = jiffies + timeout;
1714 drain = 0;
1715 retry_max = 5;
1716repeat:
1717
1718 ret = -EAGAIN;
1719 if (time_after(jiffies, expire))
1720 goto failed_removal;
1721 ret = -EINTR;
1722 if (signal_pending(current))
1723 goto failed_removal;
1724 ret = 0;
1725 if (drain) {
1726 lru_add_drain_all();
1727 cond_resched();
1728 drain_all_pages();
1729 }
1730
1731 pfn = scan_movable_pages(start_pfn, end_pfn);
1732 if (pfn) {
1733 ret = do_migrate_range(pfn, end_pfn);
1734 if (!ret) {
1735 drain = 1;
1736 goto repeat;
1737 } else {
1738 if (ret < 0)
1739 if (--retry_max == 0)
1740 goto failed_removal;
1741 yield();
1742 drain = 1;
1743 goto repeat;
1744 }
1745 }
1746
1747 lru_add_drain_all();
1748 yield();
1749
1750 drain_all_pages();
1751
1752
1753
1754
1755 dissolve_free_huge_pages(start_pfn, end_pfn);
1756
1757 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1758 if (offlined_pages < 0) {
1759 ret = -EBUSY;
1760 goto failed_removal;
1761 }
1762 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1763
1764
1765 offline_isolated_pages(start_pfn, end_pfn);
1766
1767 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1768
1769 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1770 zone->present_pages -= offlined_pages;
1771
1772 pgdat_resize_lock(zone->zone_pgdat, &flags);
1773 zone->zone_pgdat->node_present_pages -= offlined_pages;
1774 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1775
1776 init_per_zone_wmark_min();
1777
1778 if (!populated_zone(zone)) {
1779 zone_pcp_reset(zone);
1780 mutex_lock(&zonelists_mutex);
1781 build_all_zonelists(NULL, NULL);
1782 mutex_unlock(&zonelists_mutex);
1783 } else
1784 zone_pcp_update(zone);
1785
1786 node_states_clear_node(node, &arg);
1787 if (arg.status_change_nid >= 0)
1788 kswapd_stop(node);
1789
1790 vm_total_pages = nr_free_pagecache_pages();
1791 writeback_set_ratelimit();
1792
1793 memory_notify(MEM_OFFLINE, &arg);
1794 mem_hotplug_done();
1795 return 0;
1796
1797failed_removal:
1798 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1799 (unsigned long long) start_pfn << PAGE_SHIFT,
1800 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1801 memory_notify(MEM_CANCEL_OFFLINE, &arg);
1802
1803 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1804
1805out:
1806 mem_hotplug_done();
1807 return ret;
1808}
1809
1810int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1811{
1812 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1813}
1814#endif
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1829 void *arg, int (*func)(struct memory_block *, void *))
1830{
1831 struct memory_block *mem = NULL;
1832 struct mem_section *section;
1833 unsigned long pfn, section_nr;
1834 int ret;
1835
1836 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1837 section_nr = pfn_to_section_nr(pfn);
1838 if (!present_section_nr(section_nr))
1839 continue;
1840
1841 section = __nr_to_section(section_nr);
1842
1843 if (mem)
1844 if ((section_nr >= mem->start_section_nr) &&
1845 (section_nr <= mem->end_section_nr))
1846 continue;
1847
1848 mem = find_memory_block_hinted(section, mem);
1849 if (!mem)
1850 continue;
1851
1852 ret = func(mem, arg);
1853 if (ret) {
1854 kobject_put(&mem->dev.kobj);
1855 return ret;
1856 }
1857 }
1858
1859 if (mem)
1860 kobject_put(&mem->dev.kobj);
1861
1862 return 0;
1863}
1864
1865#ifdef CONFIG_MEMORY_HOTREMOVE
1866static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1867{
1868 int ret = !is_memblock_offlined(mem);
1869
1870 if (unlikely(ret)) {
1871 phys_addr_t beginpa, endpa;
1872
1873 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1874 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1875 pr_warn("removing memory fails, because memory "
1876 "[%pa-%pa] is onlined\n",
1877 &beginpa, &endpa);
1878 }
1879
1880 return ret;
1881}
1882
1883static int check_cpu_on_node(pg_data_t *pgdat)
1884{
1885 int cpu;
1886
1887 for_each_present_cpu(cpu) {
1888 if (cpu_to_node(cpu) == pgdat->node_id)
1889
1890
1891
1892
1893 return -EBUSY;
1894 }
1895
1896 return 0;
1897}
1898
1899static void unmap_cpu_on_node(pg_data_t *pgdat)
1900{
1901#ifdef CONFIG_ACPI_NUMA
1902 int cpu;
1903
1904 for_each_possible_cpu(cpu)
1905 if (cpu_to_node(cpu) == pgdat->node_id)
1906 numa_clear_node(cpu);
1907#endif
1908}
1909
1910static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1911{
1912 int ret;
1913
1914 ret = check_cpu_on_node(pgdat);
1915 if (ret)
1916 return ret;
1917
1918
1919
1920
1921
1922
1923 unmap_cpu_on_node(pgdat);
1924 return 0;
1925}
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935void try_offline_node(int nid)
1936{
1937 pg_data_t *pgdat = NODE_DATA(nid);
1938 unsigned long start_pfn = pgdat->node_start_pfn;
1939 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
1940 unsigned long pfn;
1941 int i;
1942
1943 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1944 unsigned long section_nr = pfn_to_section_nr(pfn);
1945
1946 if (!present_section_nr(section_nr))
1947 continue;
1948
1949 if (pfn_to_nid(pfn) != nid)
1950 continue;
1951
1952
1953
1954
1955
1956 return;
1957 }
1958
1959 if (check_and_unmap_cpu_on_node(pgdat))
1960 return;
1961
1962
1963
1964
1965
1966 node_set_offline(nid);
1967 unregister_one_node(nid);
1968
1969
1970 for (i = 0; i < MAX_NR_ZONES; i++) {
1971 struct zone *zone = pgdat->node_zones + i;
1972
1973
1974
1975
1976
1977 if (is_vmalloc_addr(zone->wait_table))
1978 vfree(zone->wait_table);
1979 }
1980
1981
1982
1983
1984
1985
1986
1987
1988 memset(pgdat, 0, sizeof(*pgdat));
1989}
1990EXPORT_SYMBOL(try_offline_node);
1991
1992
1993
1994
1995
1996
1997
1998
1999void __ref remove_memory(int nid, u64 start, u64 size)
2000{
2001 int ret;
2002
2003 BUG_ON(check_hotplug_memory_range(start, size));
2004
2005 mem_hotplug_begin();
2006
2007
2008
2009
2010
2011
2012 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2013 check_memblock_offlined_cb);
2014 if (ret)
2015 BUG();
2016
2017
2018 firmware_map_remove(start, start + size, "System RAM");
2019
2020 arch_remove_memory(start, size);
2021
2022 try_offline_node(nid);
2023
2024 mem_hotplug_done();
2025}
2026EXPORT_SYMBOL_GPL(remove_memory);
2027#endif
2028