2 * linux/mm/memory_hotplug.c
7 #include <linux/config.h>
8 #include <linux/stddef.h>
10 #include <linux/swap.h>
11 #include <linux/interrupt.h>
12 #include <linux/pagemap.h>
13 #include <linux/bootmem.h>
14 #include <linux/compiler.h>
15 #include <linux/module.h>
16 #include <linux/pagevec.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <linux/cpu.h>
20 #include <linux/memory.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/highmem.h>
23 #include <linux/vmalloc.h>
25 #include <asm/tlbflush.h>
27 static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
29 struct page *page, *ret;
30 unsigned long memmap_size = sizeof(struct page) * nr_pages;
32 page = alloc_pages(GFP_KERNEL, get_order(memmap_size));
36 ret = vmalloc(memmap_size);
42 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
44 memset(ret, 0, memmap_size);
49 extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
51 static void __add_zone(struct zone *zone, unsigned long phys_start_pfn)
53 struct pglist_data *pgdat = zone->zone_pgdat;
54 int nr_pages = PAGES_PER_SECTION;
55 int nid = pgdat->node_id;
58 zone_type = zone - pgdat->node_zones;
59 memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn);
60 zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages);
63 extern int sparse_add_one_section(struct zone *, unsigned long,
64 struct page *mem_map);
65 static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
67 struct pglist_data *pgdat = zone->zone_pgdat;
68 int nr_pages = PAGES_PER_SECTION;
73 * This can potentially allocate memory, and does its own
76 sparse_index_init(pfn_to_section_nr(phys_start_pfn), pgdat->node_id);
78 pgdat_resize_lock(pgdat, &flags);
79 memmap = __kmalloc_section_memmap(nr_pages);
80 ret = sparse_add_one_section(zone, phys_start_pfn, memmap);
81 pgdat_resize_unlock(pgdat, &flags);
84 /* the mem_map didn't get used */
85 if (memmap >= (struct page *)VMALLOC_START &&
86 memmap < (struct page *)VMALLOC_END)
89 free_pages((unsigned long)memmap,
90 get_order(sizeof(struct page) * nr_pages));
96 __add_zone(zone, phys_start_pfn);
97 return register_new_memory(__pfn_to_section(phys_start_pfn));
101 * Reasonably generic function for adding memory. It is
102 * expected that archs that support memory hotplug will
103 * call this function after deciding the zone to which to
106 int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
107 unsigned long nr_pages)
112 for (i = 0; i < nr_pages; i += PAGES_PER_SECTION) {
113 err = __add_section(zone, phys_start_pfn + i);
122 static void grow_zone_span(struct zone *zone,
123 unsigned long start_pfn, unsigned long end_pfn)
125 unsigned long old_zone_end_pfn;
127 zone_span_writelock(zone);
129 old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
130 if (start_pfn < zone->zone_start_pfn)
131 zone->zone_start_pfn = start_pfn;
133 if (end_pfn > old_zone_end_pfn)
134 zone->spanned_pages = end_pfn - zone->zone_start_pfn;
136 zone_span_writeunlock(zone);
139 static void grow_pgdat_span(struct pglist_data *pgdat,
140 unsigned long start_pfn, unsigned long end_pfn)
142 unsigned long old_pgdat_end_pfn =
143 pgdat->node_start_pfn + pgdat->node_spanned_pages;
145 if (start_pfn < pgdat->node_start_pfn)
146 pgdat->node_start_pfn = start_pfn;
148 if (end_pfn > old_pgdat_end_pfn)
149 pgdat->node_spanned_pages = end_pfn - pgdat->node_spanned_pages;
152 int online_pages(unsigned long pfn, unsigned long nr_pages)
156 unsigned long onlined_pages = 0;
160 * This doesn't need a lock to do pfn_to_page().
161 * The section can't be removed here because of the
162 * memory_block->state_sem.
164 zone = page_zone(pfn_to_page(pfn));
165 pgdat_resize_lock(zone->zone_pgdat, &flags);
166 grow_zone_span(zone, pfn, pfn + nr_pages);
167 grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
168 pgdat_resize_unlock(zone->zone_pgdat, &flags);
170 for (i = 0; i < nr_pages; i++) {
171 struct page *page = pfn_to_page(pfn + i);
175 zone->present_pages += onlined_pages;