7 #include <linux/stddef.h>
13 #include <linux/compiler.h>
14 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
33 #include <asm/tlbflush.h>
44 static void generic_online_page(
struct page *
page);
46 static online_page_callback_t online_page_callback = generic_online_page;
60 unlock_system_sleep();
72 res->
name =
"System RAM";
74 res->
end = start + size - 1;
77 printk(
"System RAM resource %pR cannot be added\n", res);
84 static void release_memory_resource(
struct resource *res)
93 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
94 #ifndef CONFIG_SPARSEMEM_VMEMMAP
95 static void get_page_bootmem(
unsigned long info,
struct page *
page,
100 set_page_private(page, info);
110 type = (
unsigned long) page->
lru.next;
111 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
115 ClearPagePrivate(page);
116 set_page_private(page, 0);
117 INIT_LIST_HEAD(&page->
lru);
123 static void register_page_bootmem_info_section(
unsigned long start_pfn)
125 unsigned long *usemap, mapsize, section_nr,
i;
126 struct mem_section *
ms;
129 section_nr = pfn_to_section_nr(start_pfn);
130 ms = __nr_to_section(section_nr);
140 mapsize =
sizeof(
struct page) * PAGES_PER_SECTION;
144 for (i = 0; i < mapsize; i++, page++)
145 get_page_bootmem(section_nr, page, SECTION_INFO);
147 usemap = __nr_to_section(section_nr)->pageblock_flags;
152 for (i = 0; i < mapsize; i++, page++)
153 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
157 void register_page_bootmem_info_node(
struct pglist_data *pgdat)
159 unsigned long i, pfn, end_pfn, nr_pages;
167 for (i = 0; i < nr_pages; i++, page++)
168 get_page_bootmem(node, page, NODE_INFO);
171 for (; zone < pgdat->
node_zones + MAX_NR_ZONES - 1; zone++) {
178 for (i = 0; i < nr_pages; i++, page++)
179 get_page_bootmem(node, page, NODE_INFO);
187 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
194 if (
pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
195 register_page_bootmem_info_section(pfn);
200 static void grow_zone_span(
struct zone *zone,
unsigned long start_pfn,
201 unsigned long end_pfn)
203 unsigned long old_zone_end_pfn;
205 zone_span_writelock(zone);
214 zone_span_writeunlock(zone);
217 static void grow_pgdat_span(
struct pglist_data *pgdat,
unsigned long start_pfn,
218 unsigned long end_pfn)
220 unsigned long old_pgdat_end_pfn =
230 static int __meminit __add_zone(
struct zone *zone,
unsigned long phys_start_pfn)
233 int nr_pages = PAGES_PER_SECTION;
248 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
249 grow_pgdat_span(zone->
zone_pgdat, phys_start_pfn,
250 phys_start_pfn + nr_pages);
251 pgdat_resize_unlock(zone->
zone_pgdat, &flags);
257 static int __meminit __add_section(
int nid,
struct zone *zone,
258 unsigned long phys_start_pfn)
260 int nr_pages = PAGES_PER_SECTION;
271 ret = __add_zone(zone, phys_start_pfn);
279 #ifdef CONFIG_SPARSEMEM_VMEMMAP
280 static int __remove_section(
struct zone *zone,
struct mem_section *ms)
289 static int __remove_section(
struct zone *zone,
struct mem_section *ms)
295 if (!valid_section(ms))
302 pgdat_resize_lock(pgdat, &flags);
304 pgdat_resize_unlock(pgdat, &flags);
315 int __ref __add_pages(
int nid,
struct zone *zone,
unsigned long phys_start_pfn,
316 unsigned long nr_pages)
320 int start_sec, end_sec;
322 start_sec = pfn_to_section_nr(phys_start_pfn);
323 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
325 for (i = start_sec; i <= end_sec; i++) {
326 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
333 if (err && (err != -
EEXIST))
353 int __remove_pages(
struct zone *zone,
unsigned long phys_start_pfn,
354 unsigned long nr_pages)
356 unsigned long i, ret = 0;
357 int sections_to_remove;
362 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
363 BUG_ON(nr_pages % PAGES_PER_SECTION);
367 sections_to_remove = nr_pages / PAGES_PER_SECTION;
368 for (i = 0; i < sections_to_remove; i++) {
369 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
370 ret = __remove_section(zone, __pfn_to_section(pfn));
378 int set_online_page_callback(online_page_callback_t
callback)
384 if (online_page_callback == generic_online_page) {
395 int restore_online_page_callback(online_page_callback_t callback)
401 if (online_page_callback == callback) {
402 online_page_callback = generic_online_page;
412 void __online_page_set_limits(
struct page *page)
421 void __online_page_increment_counters(
struct page *page)
425 #ifdef CONFIG_HIGHMEM
426 if (PageHighMem(page))
432 void __online_page_free(
struct page *page)
434 ClearPageReserved(page);
435 init_page_count(page);
440 static void generic_online_page(
struct page *page)
442 __online_page_set_limits(page);
443 __online_page_increment_counters(page);
444 __online_page_free(page);
447 static int online_pages_range(
unsigned long start_pfn,
unsigned long nr_pages,
451 unsigned long onlined_pages = *(
unsigned long *)arg;
454 for (i = 0; i < nr_pages; i++) {
456 (*online_page_callback)(
page);
459 *(
unsigned long *)arg = onlined_pages;
464 int __ref online_pages(
unsigned long pfn,
unsigned long nr_pages)
466 unsigned long onlined_pages = 0;
468 int need_zonelists_rebuild = 0;
476 arg.status_change_nid = -1;
480 arg.status_change_nid = nid;
483 ret = notifier_to_errno(ret);
501 if (!populated_zone(zone))
502 need_zonelists_rebuild = 1;
510 (((
unsigned long long) pfn + nr_pages)
518 zone->
zone_pgdat->node_present_pages += onlined_pages;
521 if (need_zonelists_rebuild)
524 zone_pcp_update(zone);
550 unsigned long zones_size[MAX_NR_ZONES] = {0};
551 unsigned long zholes_size[MAX_NR_ZONES] = {0};
552 unsigned long start_pfn = start >>
PAGE_SHIFT;
554 pgdat = arch_alloc_nodedata(nid);
558 arch_refresh_nodedata(nid, pgdat);
576 static void rollback_node_hotadd(
int nid,
pg_data_t *pgdat)
578 arch_refresh_nodedata(nid,
NULL);
579 arch_free_nodedata(pgdat);
593 pgdat = hotadd_new_pgdat(nid, 0);
617 res = register_memory_resource(start, size);
623 pgdat = hotadd_new_pgdat(nid, start);
657 rollback_node_hotadd(nid, pgdat);
659 release_memory_resource(res);
667 #ifdef CONFIG_MEMORY_HOTREMOVE
675 static inline int pageblock_free(
struct page *page)
681 static struct page *next_active_pageblock(
struct page *page)
687 if (pageblock_free(page)) {
690 order = page_order(page);
692 return page + (1 <<
order);
699 int is_mem_section_removable(
unsigned long start_pfn,
unsigned long nr_pages)
702 struct page *end_page = page + nr_pages;
705 for (; page < end_page; page = next_active_pageblock(page)) {
718 static int test_pages_in_a_zone(
unsigned long start_pfn,
unsigned long end_pfn)
721 struct zone *zone =
NULL;
724 for (pfn = start_pfn;
734 if (zone && page_zone(page) != zone)
736 zone = page_zone(page);
745 static unsigned long scan_lru_pages(
unsigned long start,
unsigned long end)
749 for (pfn = start; pfn <
end; pfn++) {
759 #define NR_OFFLINE_AT_ONCE_PAGES (256)
761 do_migrate_range(
unsigned long start_pfn,
unsigned long end_pfn)
765 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
770 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
774 if (!get_page_unless_zero(page))
786 page_is_file_cache(page));
789 #ifdef CONFIG_DEBUG_VM
797 if (page_count(page)) {
804 if (!list_empty(&
source)) {
827 offline_isolated_pages_cb(
unsigned long start,
unsigned long nr_pages,
830 __offline_isolated_pages(start, start + nr_pages);
835 offline_isolated_pages(
unsigned long start_pfn,
unsigned long end_pfn)
838 offline_isolated_pages_cb);
845 check_pages_isolated_cb(
unsigned long start_pfn,
unsigned long nr_pages,
849 long offlined = *(
long *)data;
853 *(
long *)data += offlined;
858 check_pages_isolated(
unsigned long start_pfn,
unsigned long end_pfn)
864 check_pages_isolated_cb);
866 offlined = (
long)ret;
870 static int __ref __offline_pages(
unsigned long start_pfn,
871 unsigned long end_pfn,
unsigned long timeout)
873 unsigned long pfn, nr_pages, expire;
875 int ret, drain, retry_max,
node;
879 BUG_ON(start_pfn >= end_pfn);
887 if (!test_pages_in_a_zone(start_pfn, end_pfn))
893 node = zone_to_nid(zone);
903 arg.status_change_nid = -1;
905 arg.status_change_nid =
node;
908 ret = notifier_to_errno(ret);
931 pfn = scan_lru_pages(start_pfn, end_pfn);
933 ret = do_migrate_range(pfn, end_pfn);
939 if (--retry_max == 0)
952 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
953 if (offlined_pages < 0) {
960 offline_isolated_pages(start_pfn, end_pfn);
965 zone->
zone_pgdat->node_present_pages -= offlined_pages;
966 totalram_pages -= offlined_pages;
970 if (!populated_zone(zone)) {
971 zone_pcp_reset(zone);
976 zone_pcp_update(zone);
992 (
unsigned long long) start_pfn << PAGE_SHIFT,
993 ((
unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1003 int offline_pages(
unsigned long start_pfn,
unsigned long nr_pages)
1005 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 *
HZ);
1012 unsigned long start_pfn, end_pfn;
1013 unsigned long pfn, section_nr;
1017 end_pfn = start_pfn +
PFN_DOWN(size);
1019 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1020 section_nr = pfn_to_section_nr(pfn);
1021 if (!present_section_nr(section_nr))
1024 section = __nr_to_section(section_nr);
1028 (section_nr <= mem->end_section_nr))