sparsemem: record when a section has a valid mem_map
[linux-2.6-block.git] / mm / sparse.c
CommitLineData
d41dee36
AW
1/*
2 * sparse memory mappings.
3 */
d41dee36
AW
4#include <linux/mm.h>
5#include <linux/mmzone.h>
6#include <linux/bootmem.h>
0b0acbec 7#include <linux/highmem.h>
d41dee36 8#include <linux/module.h>
28ae55c9 9#include <linux/spinlock.h>
0b0acbec 10#include <linux/vmalloc.h>
d41dee36
AW
11#include <asm/dma.h>
12
13/*
14 * Permanent SPARSEMEM data:
15 *
16 * 1) mem_section - memory sections, mem_map's for valid memory
17 */
3e347261 18#ifdef CONFIG_SPARSEMEM_EXTREME
802f192e 19struct mem_section *mem_section[NR_SECTION_ROOTS]
22fc6ecc 20 ____cacheline_internodealigned_in_smp;
3e347261
BP
21#else
22struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
22fc6ecc 23 ____cacheline_internodealigned_in_smp;
3e347261
BP
24#endif
25EXPORT_SYMBOL(mem_section);
26
89689ae7
CL
27#ifdef NODE_NOT_IN_PAGE_FLAGS
28/*
29 * If we did not store the node number in the page then we have to
30 * do a lookup in the section_to_node_table in order to find which
31 * node the page belongs to.
32 */
33#if MAX_NUMNODES <= 256
34static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
35#else
36static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
37#endif
38
25ba77c1 39int page_to_nid(struct page *page)
89689ae7
CL
40{
41 return section_to_node_table[page_to_section(page)];
42}
43EXPORT_SYMBOL(page_to_nid);
85770ffe
AW
44
45static void set_section_nid(unsigned long section_nr, int nid)
46{
47 section_to_node_table[section_nr] = nid;
48}
49#else /* !NODE_NOT_IN_PAGE_FLAGS */
50static inline void set_section_nid(unsigned long section_nr, int nid)
51{
52}
89689ae7
CL
53#endif
54
3e347261 55#ifdef CONFIG_SPARSEMEM_EXTREME
577a32f6 56static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
28ae55c9
DH
57{
58 struct mem_section *section = NULL;
59 unsigned long array_size = SECTIONS_PER_ROOT *
60 sizeof(struct mem_section);
61
39d24e64 62 if (slab_is_available())
46a66eec
MK
63 section = kmalloc_node(array_size, GFP_KERNEL, nid);
64 else
65 section = alloc_bootmem_node(NODE_DATA(nid), array_size);
28ae55c9
DH
66
67 if (section)
68 memset(section, 0, array_size);
69
70 return section;
3e347261 71}
802f192e 72
a3142c8e 73static int __meminit sparse_index_init(unsigned long section_nr, int nid)
802f192e 74{
34af946a 75 static DEFINE_SPINLOCK(index_init_lock);
28ae55c9
DH
76 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
77 struct mem_section *section;
78 int ret = 0;
802f192e
BP
79
80 if (mem_section[root])
28ae55c9 81 return -EEXIST;
3e347261 82
28ae55c9
DH
83 section = sparse_index_alloc(nid);
84 /*
85 * This lock keeps two different sections from
86 * reallocating for the same index
87 */
88 spin_lock(&index_init_lock);
3e347261 89
28ae55c9
DH
90 if (mem_section[root]) {
91 ret = -EEXIST;
92 goto out;
93 }
94
95 mem_section[root] = section;
96out:
97 spin_unlock(&index_init_lock);
98 return ret;
99}
100#else /* !SPARSEMEM_EXTREME */
101static inline int sparse_index_init(unsigned long section_nr, int nid)
102{
103 return 0;
802f192e 104}
28ae55c9
DH
105#endif
106
4ca644d9
DH
107/*
108 * Although written for the SPARSEMEM_EXTREME case, this happens
cd881a6b 109 * to also work for the flat array case because
4ca644d9
DH
110 * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
111 */
112int __section_nr(struct mem_section* ms)
113{
114 unsigned long root_nr;
115 struct mem_section* root;
116
12783b00
MK
117 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
118 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
4ca644d9
DH
119 if (!root)
120 continue;
121
122 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
123 break;
124 }
125
126 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
127}
128
30c253e6
AW
129/*
130 * During early boot, before section_mem_map is used for an actual
131 * mem_map, we use section_mem_map to store the section's NUMA
132 * node. This keeps us from having to use another data structure. The
133 * node information is cleared just before we store the real mem_map.
134 */
135static inline unsigned long sparse_encode_early_nid(int nid)
136{
137 return (nid << SECTION_NID_SHIFT);
138}
139
140static inline int sparse_early_nid(struct mem_section *section)
141{
142 return (section->section_mem_map >> SECTION_NID_SHIFT);
143}
144
d41dee36 145/* Record a memory area against a node. */
a3142c8e 146void __init memory_present(int nid, unsigned long start, unsigned long end)
d41dee36
AW
147{
148 unsigned long pfn;
149
150 start &= PAGE_SECTION_MASK;
151 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
152 unsigned long section = pfn_to_section_nr(pfn);
802f192e
BP
153 struct mem_section *ms;
154
155 sparse_index_init(section, nid);
85770ffe 156 set_section_nid(section, nid);
802f192e
BP
157
158 ms = __nr_to_section(section);
159 if (!ms->section_mem_map)
30c253e6
AW
160 ms->section_mem_map = sparse_encode_early_nid(nid) |
161 SECTION_MARKED_PRESENT;
d41dee36
AW
162 }
163}
164
165/*
166 * Only used by the i386 NUMA architecures, but relatively
167 * generic code.
168 */
169unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
170 unsigned long end_pfn)
171{
172 unsigned long pfn;
173 unsigned long nr_pages = 0;
174
175 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
176 if (nid != early_pfn_to_nid(pfn))
177 continue;
178
540557b9 179 if (pfn_present(pfn))
d41dee36
AW
180 nr_pages += PAGES_PER_SECTION;
181 }
182
183 return nr_pages * sizeof(struct page);
184}
185
29751f69
AW
186/*
187 * Subtle, we encode the real pfn into the mem_map such that
188 * the identity pfn - section_mem_map will return the actual
189 * physical page frame number.
190 */
191static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
192{
193 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
194}
195
196/*
197 * We need this if we ever free the mem_maps. While not implemented yet,
198 * this function is included for parity with its sibling.
199 */
200static __attribute((unused))
201struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
202{
203 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
204}
205
a3142c8e 206static int __meminit sparse_init_one_section(struct mem_section *ms,
29751f69
AW
207 unsigned long pnum, struct page *mem_map)
208{
540557b9 209 if (!present_section(ms))
29751f69
AW
210 return -EINVAL;
211
30c253e6 212 ms->section_mem_map &= ~SECTION_MAP_MASK;
540557b9
AW
213 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
214 SECTION_HAS_MEM_MAP;
29751f69
AW
215
216 return 1;
217}
218
dec2e6b7 219__attribute__((weak)) __init
2e1c49db
ZN
220void *alloc_bootmem_high_node(pg_data_t *pgdat, unsigned long size)
221{
222 return NULL;
223}
224
a3142c8e 225static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
29751f69
AW
226{
227 struct page *map;
802f192e 228 struct mem_section *ms = __nr_to_section(pnum);
30c253e6 229 int nid = sparse_early_nid(ms);
29751f69
AW
230
231 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
232 if (map)
233 return map;
234
2e1c49db
ZN
235 map = alloc_bootmem_high_node(NODE_DATA(nid),
236 sizeof(struct page) * PAGES_PER_SECTION);
237 if (map)
238 return map;
239
29751f69
AW
240 map = alloc_bootmem_node(NODE_DATA(nid),
241 sizeof(struct page) * PAGES_PER_SECTION);
242 if (map)
243 return map;
244
245 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
802f192e 246 ms->section_mem_map = 0;
29751f69
AW
247 return NULL;
248}
249
193faea9
SR
250/*
251 * Allocate the accumulated non-linear sections, allocate a mem_map
252 * for each and record the physical to section mapping.
253 */
254void __init sparse_init(void)
255{
256 unsigned long pnum;
257 struct page *map;
258
259 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
540557b9 260 if (!present_section_nr(pnum))
193faea9
SR
261 continue;
262
263 map = sparse_early_mem_map_alloc(pnum);
264 if (!map)
265 continue;
266 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
267 }
268}
269
270#ifdef CONFIG_MEMORY_HOTPLUG
0b0acbec
DH
271static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
272{
273 struct page *page, *ret;
274 unsigned long memmap_size = sizeof(struct page) * nr_pages;
275
f2d0aa5b 276 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
0b0acbec
DH
277 if (page)
278 goto got_map_page;
279
280 ret = vmalloc(memmap_size);
281 if (ret)
282 goto got_map_ptr;
283
284 return NULL;
285got_map_page:
286 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
287got_map_ptr:
288 memset(ret, 0, memmap_size);
289
290 return ret;
291}
292
293static int vaddr_in_vmalloc_area(void *addr)
294{
295 if (addr >= (void *)VMALLOC_START &&
296 addr < (void *)VMALLOC_END)
297 return 1;
298 return 0;
299}
300
301static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
302{
303 if (vaddr_in_vmalloc_area(memmap))
304 vfree(memmap);
305 else
306 free_pages((unsigned long)memmap,
307 get_order(sizeof(struct page) * nr_pages));
308}
309
29751f69
AW
310/*
311 * returns the number of sections whose mem_maps were properly
312 * set. If this is <=0, then that means that the passed-in
313 * map was not consumed and must be freed.
314 */
0b0acbec
DH
315int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
316 int nr_pages)
29751f69 317{
0b0acbec
DH
318 unsigned long section_nr = pfn_to_section_nr(start_pfn);
319 struct pglist_data *pgdat = zone->zone_pgdat;
320 struct mem_section *ms;
321 struct page *memmap;
322 unsigned long flags;
323 int ret;
29751f69 324
0b0acbec
DH
325 /*
326 * no locking for this, because it does its own
327 * plus, it does a kmalloc
328 */
329 sparse_index_init(section_nr, pgdat->node_id);
330 memmap = __kmalloc_section_memmap(nr_pages);
331
332 pgdat_resize_lock(pgdat, &flags);
29751f69 333
0b0acbec
DH
334 ms = __pfn_to_section(start_pfn);
335 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
336 ret = -EEXIST;
337 goto out;
338 }
29751f69
AW
339 ms->section_mem_map |= SECTION_MARKED_PRESENT;
340
0b0acbec
DH
341 ret = sparse_init_one_section(ms, section_nr, memmap);
342
0b0acbec
DH
343out:
344 pgdat_resize_unlock(pgdat, &flags);
46a66eec
MK
345 if (ret <= 0)
346 __kfree_section_memmap(memmap, nr_pages);
0b0acbec 347 return ret;
29751f69 348}
a3142c8e 349#endif