[PATCH] __unmap_hugepage_range(): add comment
[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
3e347261 27#ifdef CONFIG_SPARSEMEM_EXTREME
28ae55c9
DH
28static struct mem_section *sparse_index_alloc(int nid)
29{
30 struct mem_section *section = NULL;
31 unsigned long array_size = SECTIONS_PER_ROOT *
32 sizeof(struct mem_section);
33
39d24e64 34 if (slab_is_available())
46a66eec
MK
35 section = kmalloc_node(array_size, GFP_KERNEL, nid);
36 else
37 section = alloc_bootmem_node(NODE_DATA(nid), array_size);
28ae55c9
DH
38
39 if (section)
40 memset(section, 0, array_size);
41
42 return section;
3e347261 43}
802f192e 44
28ae55c9 45static int sparse_index_init(unsigned long section_nr, int nid)
802f192e 46{
34af946a 47 static DEFINE_SPINLOCK(index_init_lock);
28ae55c9
DH
48 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
49 struct mem_section *section;
50 int ret = 0;
802f192e
BP
51
52 if (mem_section[root])
28ae55c9 53 return -EEXIST;
3e347261 54
28ae55c9
DH
55 section = sparse_index_alloc(nid);
56 /*
57 * This lock keeps two different sections from
58 * reallocating for the same index
59 */
60 spin_lock(&index_init_lock);
3e347261 61
28ae55c9
DH
62 if (mem_section[root]) {
63 ret = -EEXIST;
64 goto out;
65 }
66
67 mem_section[root] = section;
68out:
69 spin_unlock(&index_init_lock);
70 return ret;
71}
72#else /* !SPARSEMEM_EXTREME */
73static inline int sparse_index_init(unsigned long section_nr, int nid)
74{
75 return 0;
802f192e 76}
28ae55c9
DH
77#endif
78
4ca644d9
DH
79/*
80 * Although written for the SPARSEMEM_EXTREME case, this happens
81 * to also work for the flat array case becase
82 * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
83 */
84int __section_nr(struct mem_section* ms)
85{
86 unsigned long root_nr;
87 struct mem_section* root;
88
12783b00
MK
89 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
90 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
4ca644d9
DH
91 if (!root)
92 continue;
93
94 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
95 break;
96 }
97
98 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
99}
100
30c253e6
AW
101/*
102 * During early boot, before section_mem_map is used for an actual
103 * mem_map, we use section_mem_map to store the section's NUMA
104 * node. This keeps us from having to use another data structure. The
105 * node information is cleared just before we store the real mem_map.
106 */
107static inline unsigned long sparse_encode_early_nid(int nid)
108{
109 return (nid << SECTION_NID_SHIFT);
110}
111
112static inline int sparse_early_nid(struct mem_section *section)
113{
114 return (section->section_mem_map >> SECTION_NID_SHIFT);
115}
116
d41dee36
AW
117/* Record a memory area against a node. */
118void memory_present(int nid, unsigned long start, unsigned long end)
119{
120 unsigned long pfn;
121
122 start &= PAGE_SECTION_MASK;
123 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
124 unsigned long section = pfn_to_section_nr(pfn);
802f192e
BP
125 struct mem_section *ms;
126
127 sparse_index_init(section, nid);
128
129 ms = __nr_to_section(section);
130 if (!ms->section_mem_map)
30c253e6
AW
131 ms->section_mem_map = sparse_encode_early_nid(nid) |
132 SECTION_MARKED_PRESENT;
d41dee36
AW
133 }
134}
135
136/*
137 * Only used by the i386 NUMA architecures, but relatively
138 * generic code.
139 */
140unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
141 unsigned long end_pfn)
142{
143 unsigned long pfn;
144 unsigned long nr_pages = 0;
145
146 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
147 if (nid != early_pfn_to_nid(pfn))
148 continue;
149
150 if (pfn_valid(pfn))
151 nr_pages += PAGES_PER_SECTION;
152 }
153
154 return nr_pages * sizeof(struct page);
155}
156
29751f69
AW
157/*
158 * Subtle, we encode the real pfn into the mem_map such that
159 * the identity pfn - section_mem_map will return the actual
160 * physical page frame number.
161 */
162static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
163{
164 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
165}
166
167/*
168 * We need this if we ever free the mem_maps. While not implemented yet,
169 * this function is included for parity with its sibling.
170 */
171static __attribute((unused))
172struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
173{
174 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
175}
176
177static int sparse_init_one_section(struct mem_section *ms,
178 unsigned long pnum, struct page *mem_map)
179{
180 if (!valid_section(ms))
181 return -EINVAL;
182
30c253e6 183 ms->section_mem_map &= ~SECTION_MAP_MASK;
29751f69
AW
184 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
185
186 return 1;
187}
188
189static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
190{
191 struct page *map;
802f192e 192 struct mem_section *ms = __nr_to_section(pnum);
30c253e6 193 int nid = sparse_early_nid(ms);
29751f69
AW
194
195 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
196 if (map)
197 return map;
198
199 map = alloc_bootmem_node(NODE_DATA(nid),
200 sizeof(struct page) * PAGES_PER_SECTION);
201 if (map)
202 return map;
203
204 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
802f192e 205 ms->section_mem_map = 0;
29751f69
AW
206 return NULL;
207}
208
0b0acbec
DH
209static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
210{
211 struct page *page, *ret;
212 unsigned long memmap_size = sizeof(struct page) * nr_pages;
213
f2d0aa5b 214 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
0b0acbec
DH
215 if (page)
216 goto got_map_page;
217
218 ret = vmalloc(memmap_size);
219 if (ret)
220 goto got_map_ptr;
221
222 return NULL;
223got_map_page:
224 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
225got_map_ptr:
226 memset(ret, 0, memmap_size);
227
228 return ret;
229}
230
231static int vaddr_in_vmalloc_area(void *addr)
232{
233 if (addr >= (void *)VMALLOC_START &&
234 addr < (void *)VMALLOC_END)
235 return 1;
236 return 0;
237}
238
239static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
240{
241 if (vaddr_in_vmalloc_area(memmap))
242 vfree(memmap);
243 else
244 free_pages((unsigned long)memmap,
245 get_order(sizeof(struct page) * nr_pages));
246}
247
d41dee36
AW
248/*
249 * Allocate the accumulated non-linear sections, allocate a mem_map
250 * for each and record the physical to section mapping.
251 */
252void sparse_init(void)
253{
254 unsigned long pnum;
255 struct page *map;
d41dee36
AW
256
257 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
29751f69 258 if (!valid_section_nr(pnum))
d41dee36
AW
259 continue;
260
29751f69 261 map = sparse_early_mem_map_alloc(pnum);
802f192e
BP
262 if (!map)
263 continue;
264 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
d41dee36
AW
265 }
266}
29751f69
AW
267
268/*
269 * returns the number of sections whose mem_maps were properly
270 * set. If this is <=0, then that means that the passed-in
271 * map was not consumed and must be freed.
272 */
0b0acbec
DH
273int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
274 int nr_pages)
29751f69 275{
0b0acbec
DH
276 unsigned long section_nr = pfn_to_section_nr(start_pfn);
277 struct pglist_data *pgdat = zone->zone_pgdat;
278 struct mem_section *ms;
279 struct page *memmap;
280 unsigned long flags;
281 int ret;
29751f69 282
0b0acbec
DH
283 /*
284 * no locking for this, because it does its own
285 * plus, it does a kmalloc
286 */
287 sparse_index_init(section_nr, pgdat->node_id);
288 memmap = __kmalloc_section_memmap(nr_pages);
289
290 pgdat_resize_lock(pgdat, &flags);
29751f69 291
0b0acbec
DH
292 ms = __pfn_to_section(start_pfn);
293 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
294 ret = -EEXIST;
295 goto out;
296 }
29751f69
AW
297 ms->section_mem_map |= SECTION_MARKED_PRESENT;
298
0b0acbec
DH
299 ret = sparse_init_one_section(ms, section_nr, memmap);
300
0b0acbec
DH
301out:
302 pgdat_resize_unlock(pgdat, &flags);
46a66eec
MK
303 if (ret <= 0)
304 __kfree_section_memmap(memmap, nr_pages);
0b0acbec 305 return ret;
29751f69 306}