]>
Commit | Line | Data |
---|---|---|
d41dee36 AW |
1 | /* |
2 | * sparse memory mappings. | |
3 | */ | |
d41dee36 | 4 | #include <linux/mm.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
d41dee36 AW |
6 | #include <linux/mmzone.h> |
7 | #include <linux/bootmem.h> | |
3b32123d | 8 | #include <linux/compiler.h> |
0b0acbec | 9 | #include <linux/highmem.h> |
b95f1b31 | 10 | #include <linux/export.h> |
28ae55c9 | 11 | #include <linux/spinlock.h> |
0b0acbec | 12 | #include <linux/vmalloc.h> |
3b32123d | 13 | |
0c0a4a51 | 14 | #include "internal.h" |
d41dee36 | 15 | #include <asm/dma.h> |
8f6aac41 CL |
16 | #include <asm/pgalloc.h> |
17 | #include <asm/pgtable.h> | |
d41dee36 AW |
18 | |
19 | /* | |
20 | * Permanent SPARSEMEM data: | |
21 | * | |
22 | * 1) mem_section - memory sections, mem_map's for valid memory | |
23 | */ | |
3e347261 | 24 | #ifdef CONFIG_SPARSEMEM_EXTREME |
c70f71e0 | 25 | struct mem_section **mem_section; |
3e347261 BP |
26 | #else |
27 | struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT] | |
22fc6ecc | 28 | ____cacheline_internodealigned_in_smp; |
3e347261 BP |
29 | #endif |
30 | EXPORT_SYMBOL(mem_section); | |
31 | ||
89689ae7 CL |
32 | #ifdef NODE_NOT_IN_PAGE_FLAGS |
33 | /* | |
34 | * If we did not store the node number in the page then we have to | |
35 | * do a lookup in the section_to_node_table in order to find which | |
36 | * node the page belongs to. | |
37 | */ | |
38 | #if MAX_NUMNODES <= 256 | |
39 | static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; | |
40 | #else | |
41 | static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; | |
42 | #endif | |
43 | ||
33dd4e0e | 44 | int page_to_nid(const struct page *page) |
89689ae7 CL |
45 | { |
46 | return section_to_node_table[page_to_section(page)]; | |
47 | } | |
48 | EXPORT_SYMBOL(page_to_nid); | |
85770ffe AW |
49 | |
50 | static void set_section_nid(unsigned long section_nr, int nid) | |
51 | { | |
52 | section_to_node_table[section_nr] = nid; | |
53 | } | |
54 | #else /* !NODE_NOT_IN_PAGE_FLAGS */ | |
55 | static inline void set_section_nid(unsigned long section_nr, int nid) | |
56 | { | |
57 | } | |
89689ae7 CL |
58 | #endif |
59 | ||
3e347261 | 60 | #ifdef CONFIG_SPARSEMEM_EXTREME |
bd721ea7 | 61 | static noinline struct mem_section __ref *sparse_index_alloc(int nid) |
28ae55c9 DH |
62 | { |
63 | struct mem_section *section = NULL; | |
64 | unsigned long array_size = SECTIONS_PER_ROOT * | |
65 | sizeof(struct mem_section); | |
66 | ||
f52407ce SL |
67 | if (slab_is_available()) { |
68 | if (node_state(nid, N_HIGH_MEMORY)) | |
5b760e64 | 69 | section = kzalloc_node(array_size, GFP_KERNEL, nid); |
f52407ce | 70 | else |
5b760e64 GS |
71 | section = kzalloc(array_size, GFP_KERNEL); |
72 | } else { | |
bb016b84 | 73 | section = memblock_virt_alloc_node(array_size, nid); |
5b760e64 | 74 | } |
28ae55c9 DH |
75 | |
76 | return section; | |
3e347261 | 77 | } |
802f192e | 78 | |
a3142c8e | 79 | static int __meminit sparse_index_init(unsigned long section_nr, int nid) |
802f192e | 80 | { |
28ae55c9 DH |
81 | unsigned long root = SECTION_NR_TO_ROOT(section_nr); |
82 | struct mem_section *section; | |
802f192e BP |
83 | |
84 | if (mem_section[root]) | |
28ae55c9 | 85 | return -EEXIST; |
3e347261 | 86 | |
28ae55c9 | 87 | section = sparse_index_alloc(nid); |
af0cd5a7 WC |
88 | if (!section) |
89 | return -ENOMEM; | |
28ae55c9 DH |
90 | |
91 | mem_section[root] = section; | |
c1c95183 | 92 | |
9d1936cf | 93 | return 0; |
28ae55c9 DH |
94 | } |
95 | #else /* !SPARSEMEM_EXTREME */ | |
96 | static inline int sparse_index_init(unsigned long section_nr, int nid) | |
97 | { | |
98 | return 0; | |
802f192e | 99 | } |
28ae55c9 DH |
100 | #endif |
101 | ||
91fd8b95 | 102 | #ifdef CONFIG_SPARSEMEM_EXTREME |
4ca644d9 DH |
103 | int __section_nr(struct mem_section* ms) |
104 | { | |
105 | unsigned long root_nr; | |
c70f71e0 | 106 | struct mem_section *root = NULL; |
4ca644d9 | 107 | |
12783b00 MK |
108 | for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) { |
109 | root = __nr_to_section(root_nr * SECTIONS_PER_ROOT); | |
4ca644d9 DH |
110 | if (!root) |
111 | continue; | |
112 | ||
113 | if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT))) | |
114 | break; | |
115 | } | |
116 | ||
c70f71e0 | 117 | VM_BUG_ON(!root); |
db36a461 | 118 | |
4ca644d9 DH |
119 | return (root_nr * SECTIONS_PER_ROOT) + (ms - root); |
120 | } | |
91fd8b95 ZC |
121 | #else |
122 | int __section_nr(struct mem_section* ms) | |
123 | { | |
124 | return (int)(ms - mem_section[0]); | |
125 | } | |
126 | #endif | |
4ca644d9 | 127 | |
30c253e6 AW |
128 | /* |
129 | * During early boot, before section_mem_map is used for an actual | |
130 | * mem_map, we use section_mem_map to store the section's NUMA | |
131 | * node. This keeps us from having to use another data structure. The | |
132 | * node information is cleared just before we store the real mem_map. | |
133 | */ | |
134 | static inline unsigned long sparse_encode_early_nid(int nid) | |
135 | { | |
136 | return (nid << SECTION_NID_SHIFT); | |
137 | } | |
138 | ||
139 | static inline int sparse_early_nid(struct mem_section *section) | |
140 | { | |
141 | return (section->section_mem_map >> SECTION_NID_SHIFT); | |
142 | } | |
143 | ||
2dbb51c4 MG |
144 | /* Validate the physical addressing limitations of the model */ |
145 | void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn, | |
146 | unsigned long *end_pfn) | |
d41dee36 | 147 | { |
2dbb51c4 | 148 | unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT); |
d41dee36 | 149 | |
bead9a3a IM |
150 | /* |
151 | * Sanity checks - do not allow an architecture to pass | |
152 | * in larger pfns than the maximum scope of sparsemem: | |
153 | */ | |
2dbb51c4 MG |
154 | if (*start_pfn > max_sparsemem_pfn) { |
155 | mminit_dprintk(MMINIT_WARNING, "pfnvalidation", | |
156 | "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n", | |
157 | *start_pfn, *end_pfn, max_sparsemem_pfn); | |
158 | WARN_ON_ONCE(1); | |
159 | *start_pfn = max_sparsemem_pfn; | |
160 | *end_pfn = max_sparsemem_pfn; | |
ef161a98 | 161 | } else if (*end_pfn > max_sparsemem_pfn) { |
2dbb51c4 MG |
162 | mminit_dprintk(MMINIT_WARNING, "pfnvalidation", |
163 | "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n", | |
164 | *start_pfn, *end_pfn, max_sparsemem_pfn); | |
165 | WARN_ON_ONCE(1); | |
166 | *end_pfn = max_sparsemem_pfn; | |
167 | } | |
168 | } | |
169 | ||
c4e1be9e DH |
170 | /* |
171 | * There are a number of times that we loop over NR_MEM_SECTIONS, | |
172 | * looking for section_present() on each. But, when we have very | |
173 | * large physical address spaces, NR_MEM_SECTIONS can also be | |
174 | * very large which makes the loops quite long. | |
175 | * | |
176 | * Keeping track of this gives us an easy way to break out of | |
177 | * those loops early. | |
178 | */ | |
179 | int __highest_present_section_nr; | |
180 | static void section_mark_present(struct mem_section *ms) | |
181 | { | |
182 | int section_nr = __section_nr(ms); | |
183 | ||
184 | if (section_nr > __highest_present_section_nr) | |
185 | __highest_present_section_nr = section_nr; | |
186 | ||
187 | ms->section_mem_map |= SECTION_MARKED_PRESENT; | |
188 | } | |
189 | ||
190 | static inline int next_present_section_nr(int section_nr) | |
191 | { | |
192 | do { | |
193 | section_nr++; | |
194 | if (present_section_nr(section_nr)) | |
195 | return section_nr; | |
196 | } while ((section_nr < NR_MEM_SECTIONS) && | |
197 | (section_nr <= __highest_present_section_nr)); | |
198 | ||
199 | return -1; | |
200 | } | |
201 | #define for_each_present_section_nr(start, section_nr) \ | |
202 | for (section_nr = next_present_section_nr(start-1); \ | |
203 | ((section_nr >= 0) && \ | |
204 | (section_nr < NR_MEM_SECTIONS) && \ | |
205 | (section_nr <= __highest_present_section_nr)); \ | |
206 | section_nr = next_present_section_nr(section_nr)) | |
207 | ||
2dbb51c4 MG |
208 | /* Record a memory area against a node. */ |
209 | void __init memory_present(int nid, unsigned long start, unsigned long end) | |
210 | { | |
211 | unsigned long pfn; | |
bead9a3a | 212 | |
fbc3acbf KS |
213 | #ifdef CONFIG_SPARSEMEM_EXTREME |
214 | if (unlikely(!mem_section)) { | |
215 | unsigned long size, align; | |
216 | ||
217 | size = sizeof(struct mem_section) * NR_SECTION_ROOTS; | |
218 | align = 1 << (INTERNODE_CACHE_SHIFT); | |
219 | mem_section = memblock_virt_alloc(size, align); | |
220 | } | |
221 | #endif | |
222 | ||
d41dee36 | 223 | start &= PAGE_SECTION_MASK; |
2dbb51c4 | 224 | mminit_validate_memmodel_limits(&start, &end); |
d41dee36 AW |
225 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) { |
226 | unsigned long section = pfn_to_section_nr(pfn); | |
802f192e BP |
227 | struct mem_section *ms; |
228 | ||
229 | sparse_index_init(section, nid); | |
85770ffe | 230 | set_section_nid(section, nid); |
802f192e BP |
231 | |
232 | ms = __nr_to_section(section); | |
c4e1be9e | 233 | if (!ms->section_mem_map) { |
2d070eab MH |
234 | ms->section_mem_map = sparse_encode_early_nid(nid) | |
235 | SECTION_IS_ONLINE; | |
c4e1be9e DH |
236 | section_mark_present(ms); |
237 | } | |
d41dee36 AW |
238 | } |
239 | } | |
240 | ||
241 | /* | |
242 | * Only used by the i386 NUMA architecures, but relatively | |
243 | * generic code. | |
244 | */ | |
245 | unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn, | |
246 | unsigned long end_pfn) | |
247 | { | |
248 | unsigned long pfn; | |
249 | unsigned long nr_pages = 0; | |
250 | ||
2dbb51c4 | 251 | mminit_validate_memmodel_limits(&start_pfn, &end_pfn); |
d41dee36 AW |
252 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
253 | if (nid != early_pfn_to_nid(pfn)) | |
254 | continue; | |
255 | ||
540557b9 | 256 | if (pfn_present(pfn)) |
d41dee36 AW |
257 | nr_pages += PAGES_PER_SECTION; |
258 | } | |
259 | ||
260 | return nr_pages * sizeof(struct page); | |
261 | } | |
262 | ||
29751f69 AW |
263 | /* |
264 | * Subtle, we encode the real pfn into the mem_map such that | |
265 | * the identity pfn - section_mem_map will return the actual | |
266 | * physical page frame number. | |
267 | */ | |
268 | static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum) | |
269 | { | |
270 | return (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); | |
271 | } | |
272 | ||
273 | /* | |
ea01ea93 | 274 | * Decode mem_map from the coded memmap |
29751f69 | 275 | */ |
29751f69 AW |
276 | struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum) |
277 | { | |
ea01ea93 BP |
278 | /* mask off the extra low bits of information */ |
279 | coded_mem_map &= SECTION_MAP_MASK; | |
29751f69 AW |
280 | return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum); |
281 | } | |
282 | ||
a3142c8e | 283 | static int __meminit sparse_init_one_section(struct mem_section *ms, |
5c0e3066 MG |
284 | unsigned long pnum, struct page *mem_map, |
285 | unsigned long *pageblock_bitmap) | |
29751f69 | 286 | { |
540557b9 | 287 | if (!present_section(ms)) |
29751f69 AW |
288 | return -EINVAL; |
289 | ||
30c253e6 | 290 | ms->section_mem_map &= ~SECTION_MAP_MASK; |
540557b9 AW |
291 | ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) | |
292 | SECTION_HAS_MEM_MAP; | |
5c0e3066 | 293 | ms->pageblock_flags = pageblock_bitmap; |
29751f69 AW |
294 | |
295 | return 1; | |
296 | } | |
297 | ||
04753278 | 298 | unsigned long usemap_size(void) |
5c0e3066 | 299 | { |
60a7a88d | 300 | return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long); |
5c0e3066 MG |
301 | } |
302 | ||
303 | #ifdef CONFIG_MEMORY_HOTPLUG | |
304 | static unsigned long *__kmalloc_section_usemap(void) | |
305 | { | |
306 | return kmalloc(usemap_size(), GFP_KERNEL); | |
307 | } | |
308 | #endif /* CONFIG_MEMORY_HOTPLUG */ | |
309 | ||
48c90682 YG |
310 | #ifdef CONFIG_MEMORY_HOTREMOVE |
311 | static unsigned long * __init | |
a4322e1b | 312 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
238305bb | 313 | unsigned long size) |
48c90682 | 314 | { |
99ab7b19 YL |
315 | unsigned long goal, limit; |
316 | unsigned long *p; | |
317 | int nid; | |
48c90682 YG |
318 | /* |
319 | * A page may contain usemaps for other sections preventing the | |
320 | * page being freed and making a section unremovable while | |
c800bcd5 | 321 | * other sections referencing the usemap remain active. Similarly, |
48c90682 YG |
322 | * a pgdat can prevent a section being removed. If section A |
323 | * contains a pgdat and section B contains the usemap, both | |
324 | * sections become inter-dependent. This allocates usemaps | |
325 | * from the same section as the pgdat where possible to avoid | |
326 | * this problem. | |
327 | */ | |
07b4e2bc | 328 | goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT); |
99ab7b19 YL |
329 | limit = goal + (1UL << PA_SECTION_SHIFT); |
330 | nid = early_pfn_to_nid(goal >> PAGE_SHIFT); | |
331 | again: | |
bb016b84 SS |
332 | p = memblock_virt_alloc_try_nid_nopanic(size, |
333 | SMP_CACHE_BYTES, goal, limit, | |
334 | nid); | |
99ab7b19 YL |
335 | if (!p && limit) { |
336 | limit = 0; | |
337 | goto again; | |
338 | } | |
339 | return p; | |
48c90682 YG |
340 | } |
341 | ||
342 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) | |
343 | { | |
344 | unsigned long usemap_snr, pgdat_snr; | |
c70f71e0 KS |
345 | static unsigned long old_usemap_snr; |
346 | static unsigned long old_pgdat_snr; | |
48c90682 YG |
347 | struct pglist_data *pgdat = NODE_DATA(nid); |
348 | int usemap_nid; | |
349 | ||
c70f71e0 KS |
350 | /* First call */ |
351 | if (!old_usemap_snr) { | |
352 | old_usemap_snr = NR_MEM_SECTIONS; | |
353 | old_pgdat_snr = NR_MEM_SECTIONS; | |
354 | } | |
355 | ||
48c90682 YG |
356 | usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT); |
357 | pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT); | |
358 | if (usemap_snr == pgdat_snr) | |
359 | return; | |
360 | ||
361 | if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr) | |
362 | /* skip redundant message */ | |
363 | return; | |
364 | ||
365 | old_usemap_snr = usemap_snr; | |
366 | old_pgdat_snr = pgdat_snr; | |
367 | ||
368 | usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr)); | |
369 | if (usemap_nid != nid) { | |
1170532b JP |
370 | pr_info("node %d must be removed before remove section %ld\n", |
371 | nid, usemap_snr); | |
48c90682 YG |
372 | return; |
373 | } | |
374 | /* | |
375 | * There is a circular dependency. | |
376 | * Some platforms allow un-removable section because they will just | |
377 | * gather other removable sections for dynamic partitioning. | |
378 | * Just notify un-removable section's number here. | |
379 | */ | |
1170532b JP |
380 | pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n", |
381 | usemap_snr, pgdat_snr, nid); | |
48c90682 YG |
382 | } |
383 | #else | |
384 | static unsigned long * __init | |
a4322e1b | 385 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
238305bb | 386 | unsigned long size) |
48c90682 | 387 | { |
bb016b84 | 388 | return memblock_virt_alloc_node_nopanic(size, pgdat->node_id); |
48c90682 YG |
389 | } |
390 | ||
391 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) | |
392 | { | |
393 | } | |
394 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | |
395 | ||
18732093 | 396 | static void __init sparse_early_usemaps_alloc_node(void *data, |
a4322e1b YL |
397 | unsigned long pnum_begin, |
398 | unsigned long pnum_end, | |
399 | unsigned long usemap_count, int nodeid) | |
5c0e3066 | 400 | { |
a4322e1b YL |
401 | void *usemap; |
402 | unsigned long pnum; | |
18732093 | 403 | unsigned long **usemap_map = (unsigned long **)data; |
a4322e1b | 404 | int size = usemap_size(); |
5c0e3066 | 405 | |
a4322e1b | 406 | usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid), |
238305bb | 407 | size * usemap_count); |
f5bf18fa | 408 | if (!usemap) { |
1170532b | 409 | pr_warn("%s: allocation failed\n", __func__); |
238305bb | 410 | return; |
48c90682 YG |
411 | } |
412 | ||
f5bf18fa NA |
413 | for (pnum = pnum_begin; pnum < pnum_end; pnum++) { |
414 | if (!present_section_nr(pnum)) | |
415 | continue; | |
416 | usemap_map[pnum] = usemap; | |
417 | usemap += size; | |
418 | check_usemap_section_nr(nodeid, usemap_map[pnum]); | |
a4322e1b | 419 | } |
5c0e3066 MG |
420 | } |
421 | ||
8f6aac41 | 422 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
98f3cfc1 | 423 | struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid) |
29751f69 AW |
424 | { |
425 | struct page *map; | |
e48e67e0 | 426 | unsigned long size; |
29751f69 AW |
427 | |
428 | map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION); | |
429 | if (map) | |
430 | return map; | |
431 | ||
e48e67e0 | 432 | size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION); |
bb016b84 SS |
433 | map = memblock_virt_alloc_try_nid(size, |
434 | PAGE_SIZE, __pa(MAX_DMA_ADDRESS), | |
435 | BOOTMEM_ALLOC_ACCESSIBLE, nid); | |
8f6aac41 CL |
436 | return map; |
437 | } | |
9bdac914 YL |
438 | void __init sparse_mem_maps_populate_node(struct page **map_map, |
439 | unsigned long pnum_begin, | |
440 | unsigned long pnum_end, | |
441 | unsigned long map_count, int nodeid) | |
442 | { | |
443 | void *map; | |
444 | unsigned long pnum; | |
445 | unsigned long size = sizeof(struct page) * PAGES_PER_SECTION; | |
446 | ||
447 | map = alloc_remap(nodeid, size * map_count); | |
448 | if (map) { | |
449 | for (pnum = pnum_begin; pnum < pnum_end; pnum++) { | |
450 | if (!present_section_nr(pnum)) | |
451 | continue; | |
452 | map_map[pnum] = map; | |
453 | map += size; | |
454 | } | |
455 | return; | |
456 | } | |
457 | ||
458 | size = PAGE_ALIGN(size); | |
bb016b84 SS |
459 | map = memblock_virt_alloc_try_nid(size * map_count, |
460 | PAGE_SIZE, __pa(MAX_DMA_ADDRESS), | |
461 | BOOTMEM_ALLOC_ACCESSIBLE, nodeid); | |
9bdac914 YL |
462 | if (map) { |
463 | for (pnum = pnum_begin; pnum < pnum_end; pnum++) { | |
464 | if (!present_section_nr(pnum)) | |
465 | continue; | |
466 | map_map[pnum] = map; | |
467 | map += size; | |
468 | } | |
469 | return; | |
470 | } | |
471 | ||
472 | /* fallback */ | |
473 | for (pnum = pnum_begin; pnum < pnum_end; pnum++) { | |
474 | struct mem_section *ms; | |
475 | ||
476 | if (!present_section_nr(pnum)) | |
477 | continue; | |
478 | map_map[pnum] = sparse_mem_map_populate(pnum, nodeid); | |
479 | if (map_map[pnum]) | |
480 | continue; | |
481 | ms = __nr_to_section(pnum); | |
1170532b | 482 | pr_err("%s: sparsemem memory map backing failed some memory will not be available\n", |
756a025f | 483 | __func__); |
9bdac914 YL |
484 | ms->section_mem_map = 0; |
485 | } | |
486 | } | |
8f6aac41 CL |
487 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
488 | ||
81d0d950 | 489 | #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER |
18732093 | 490 | static void __init sparse_early_mem_maps_alloc_node(void *data, |
9bdac914 YL |
491 | unsigned long pnum_begin, |
492 | unsigned long pnum_end, | |
493 | unsigned long map_count, int nodeid) | |
494 | { | |
18732093 | 495 | struct page **map_map = (struct page **)data; |
9bdac914 YL |
496 | sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end, |
497 | map_count, nodeid); | |
498 | } | |
81d0d950 | 499 | #else |
9e5c6da7 | 500 | static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum) |
8f6aac41 CL |
501 | { |
502 | struct page *map; | |
503 | struct mem_section *ms = __nr_to_section(pnum); | |
504 | int nid = sparse_early_nid(ms); | |
505 | ||
98f3cfc1 | 506 | map = sparse_mem_map_populate(pnum, nid); |
29751f69 AW |
507 | if (map) |
508 | return map; | |
509 | ||
1170532b | 510 | pr_err("%s: sparsemem memory map backing failed some memory will not be available\n", |
756a025f | 511 | __func__); |
802f192e | 512 | ms->section_mem_map = 0; |
29751f69 AW |
513 | return NULL; |
514 | } | |
9bdac914 | 515 | #endif |
29751f69 | 516 | |
3b32123d | 517 | void __weak __meminit vmemmap_populate_print_last(void) |
c2b91e2e YL |
518 | { |
519 | } | |
a4322e1b | 520 | |
18732093 WL |
521 | /** |
522 | * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap | |
523 | * @map: usemap_map for pageblock flags or mmap_map for vmemmap | |
524 | */ | |
525 | static void __init alloc_usemap_and_memmap(void (*alloc_func) | |
526 | (void *, unsigned long, unsigned long, | |
527 | unsigned long, int), void *data) | |
528 | { | |
529 | unsigned long pnum; | |
530 | unsigned long map_count; | |
531 | int nodeid_begin = 0; | |
532 | unsigned long pnum_begin = 0; | |
533 | ||
c4e1be9e | 534 | for_each_present_section_nr(0, pnum) { |
18732093 WL |
535 | struct mem_section *ms; |
536 | ||
18732093 WL |
537 | ms = __nr_to_section(pnum); |
538 | nodeid_begin = sparse_early_nid(ms); | |
539 | pnum_begin = pnum; | |
540 | break; | |
541 | } | |
542 | map_count = 1; | |
c4e1be9e | 543 | for_each_present_section_nr(pnum_begin + 1, pnum) { |
18732093 WL |
544 | struct mem_section *ms; |
545 | int nodeid; | |
546 | ||
18732093 WL |
547 | ms = __nr_to_section(pnum); |
548 | nodeid = sparse_early_nid(ms); | |
549 | if (nodeid == nodeid_begin) { | |
550 | map_count++; | |
551 | continue; | |
552 | } | |
553 | /* ok, we need to take cake of from pnum_begin to pnum - 1*/ | |
554 | alloc_func(data, pnum_begin, pnum, | |
555 | map_count, nodeid_begin); | |
556 | /* new start, update count etc*/ | |
557 | nodeid_begin = nodeid; | |
558 | pnum_begin = pnum; | |
559 | map_count = 1; | |
560 | } | |
561 | /* ok, last chunk */ | |
562 | alloc_func(data, pnum_begin, NR_MEM_SECTIONS, | |
563 | map_count, nodeid_begin); | |
564 | } | |
565 | ||
193faea9 SR |
566 | /* |
567 | * Allocate the accumulated non-linear sections, allocate a mem_map | |
568 | * for each and record the physical to section mapping. | |
569 | */ | |
570 | void __init sparse_init(void) | |
571 | { | |
572 | unsigned long pnum; | |
573 | struct page *map; | |
5c0e3066 | 574 | unsigned long *usemap; |
e123dd3f | 575 | unsigned long **usemap_map; |
81d0d950 | 576 | int size; |
81d0d950 | 577 | #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER |
81d0d950 YL |
578 | int size2; |
579 | struct page **map_map; | |
580 | #endif | |
e123dd3f | 581 | |
55878e88 CS |
582 | /* see include/linux/mmzone.h 'struct mem_section' definition */ |
583 | BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section))); | |
584 | ||
ca57df79 XQ |
585 | /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */ |
586 | set_pageblock_order(); | |
587 | ||
e123dd3f YL |
588 | /* |
589 | * map is using big page (aka 2M in x86 64 bit) | |
590 | * usemap is less one page (aka 24 bytes) | |
591 | * so alloc 2M (with 2M align) and 24 bytes in turn will | |
592 | * make next 2M slip to one more 2M later. | |
593 | * then in big system, the memory will have a lot of holes... | |
25985edc | 594 | * here try to allocate 2M pages continuously. |
e123dd3f YL |
595 | * |
596 | * powerpc need to call sparse_init_one_section right after each | |
597 | * sparse_early_mem_map_alloc, so allocate usemap_map at first. | |
598 | */ | |
599 | size = sizeof(unsigned long *) * NR_MEM_SECTIONS; | |
bb016b84 | 600 | usemap_map = memblock_virt_alloc(size, 0); |
e123dd3f YL |
601 | if (!usemap_map) |
602 | panic("can not allocate usemap_map\n"); | |
18732093 WL |
603 | alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node, |
604 | (void *)usemap_map); | |
193faea9 | 605 | |
9bdac914 YL |
606 | #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER |
607 | size2 = sizeof(struct page *) * NR_MEM_SECTIONS; | |
bb016b84 | 608 | map_map = memblock_virt_alloc(size2, 0); |
9bdac914 YL |
609 | if (!map_map) |
610 | panic("can not allocate map_map\n"); | |
18732093 WL |
611 | alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node, |
612 | (void *)map_map); | |
9bdac914 YL |
613 | #endif |
614 | ||
c4e1be9e | 615 | for_each_present_section_nr(0, pnum) { |
e123dd3f | 616 | usemap = usemap_map[pnum]; |
5c0e3066 MG |
617 | if (!usemap) |
618 | continue; | |
619 | ||
9bdac914 YL |
620 | #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER |
621 | map = map_map[pnum]; | |
622 | #else | |
e123dd3f | 623 | map = sparse_early_mem_map_alloc(pnum); |
9bdac914 | 624 | #endif |
e123dd3f YL |
625 | if (!map) |
626 | continue; | |
627 | ||
5c0e3066 MG |
628 | sparse_init_one_section(__nr_to_section(pnum), pnum, map, |
629 | usemap); | |
193faea9 | 630 | } |
e123dd3f | 631 | |
c2b91e2e YL |
632 | vmemmap_populate_print_last(); |
633 | ||
9bdac914 | 634 | #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER |
bb016b84 | 635 | memblock_free_early(__pa(map_map), size2); |
9bdac914 | 636 | #endif |
bb016b84 | 637 | memblock_free_early(__pa(usemap_map), size); |
193faea9 SR |
638 | } |
639 | ||
640 | #ifdef CONFIG_MEMORY_HOTPLUG | |
2d070eab MH |
641 | |
642 | /* Mark all memory sections within the pfn range as online */ | |
643 | void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn) | |
644 | { | |
645 | unsigned long pfn; | |
646 | ||
647 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { | |
6c7287cd | 648 | unsigned long section_nr = pfn_to_section_nr(pfn); |
2d070eab MH |
649 | struct mem_section *ms; |
650 | ||
651 | /* onlining code should never touch invalid ranges */ | |
652 | if (WARN_ON(!valid_section_nr(section_nr))) | |
653 | continue; | |
654 | ||
655 | ms = __nr_to_section(section_nr); | |
656 | ms->section_mem_map |= SECTION_IS_ONLINE; | |
657 | } | |
658 | } | |
659 | ||
660 | #ifdef CONFIG_MEMORY_HOTREMOVE | |
661 | /* Mark all memory sections within the pfn range as online */ | |
662 | void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn) | |
663 | { | |
664 | unsigned long pfn; | |
665 | ||
666 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { | |
667 | unsigned long section_nr = pfn_to_section_nr(start_pfn); | |
668 | struct mem_section *ms; | |
669 | ||
670 | /* | |
671 | * TODO this needs some double checking. Offlining code makes | |
672 | * sure to check pfn_valid but those checks might be just bogus | |
673 | */ | |
674 | if (WARN_ON(!valid_section_nr(section_nr))) | |
675 | continue; | |
676 | ||
677 | ms = __nr_to_section(section_nr); | |
678 | ms->section_mem_map &= ~SECTION_IS_ONLINE; | |
679 | } | |
680 | } | |
681 | #endif | |
682 | ||
98f3cfc1 | 683 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
85b35fea | 684 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid) |
98f3cfc1 YG |
685 | { |
686 | /* This will make the necessary allocations eventually. */ | |
687 | return sparse_mem_map_populate(pnum, nid); | |
688 | } | |
85b35fea | 689 | static void __kfree_section_memmap(struct page *memmap) |
98f3cfc1 | 690 | { |
0aad818b | 691 | unsigned long start = (unsigned long)memmap; |
85b35fea | 692 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
0aad818b JW |
693 | |
694 | vmemmap_free(start, end); | |
98f3cfc1 | 695 | } |
4edd7cef | 696 | #ifdef CONFIG_MEMORY_HOTREMOVE |
81556b02 | 697 | static void free_map_bootmem(struct page *memmap) |
0c0a4a51 | 698 | { |
0aad818b | 699 | unsigned long start = (unsigned long)memmap; |
81556b02 | 700 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
0aad818b JW |
701 | |
702 | vmemmap_free(start, end); | |
0c0a4a51 | 703 | } |
4edd7cef | 704 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
98f3cfc1 | 705 | #else |
85b35fea | 706 | static struct page *__kmalloc_section_memmap(void) |
0b0acbec DH |
707 | { |
708 | struct page *page, *ret; | |
85b35fea | 709 | unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION; |
0b0acbec | 710 | |
f2d0aa5b | 711 | page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size)); |
0b0acbec DH |
712 | if (page) |
713 | goto got_map_page; | |
714 | ||
715 | ret = vmalloc(memmap_size); | |
716 | if (ret) | |
717 | goto got_map_ptr; | |
718 | ||
719 | return NULL; | |
720 | got_map_page: | |
721 | ret = (struct page *)pfn_to_kaddr(page_to_pfn(page)); | |
722 | got_map_ptr: | |
0b0acbec DH |
723 | |
724 | return ret; | |
725 | } | |
726 | ||
85b35fea | 727 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid) |
98f3cfc1 | 728 | { |
85b35fea | 729 | return __kmalloc_section_memmap(); |
98f3cfc1 YG |
730 | } |
731 | ||
85b35fea | 732 | static void __kfree_section_memmap(struct page *memmap) |
0b0acbec | 733 | { |
9e2779fa | 734 | if (is_vmalloc_addr(memmap)) |
0b0acbec DH |
735 | vfree(memmap); |
736 | else | |
737 | free_pages((unsigned long)memmap, | |
85b35fea | 738 | get_order(sizeof(struct page) * PAGES_PER_SECTION)); |
0b0acbec | 739 | } |
0c0a4a51 | 740 | |
4edd7cef | 741 | #ifdef CONFIG_MEMORY_HOTREMOVE |
81556b02 | 742 | static void free_map_bootmem(struct page *memmap) |
0c0a4a51 YG |
743 | { |
744 | unsigned long maps_section_nr, removing_section_nr, i; | |
81556b02 | 745 | unsigned long magic, nr_pages; |
ae64ffca | 746 | struct page *page = virt_to_page(memmap); |
0c0a4a51 | 747 | |
81556b02 ZY |
748 | nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page)) |
749 | >> PAGE_SHIFT; | |
750 | ||
0c0a4a51 | 751 | for (i = 0; i < nr_pages; i++, page++) { |
ddffe98d | 752 | magic = (unsigned long) page->freelist; |
0c0a4a51 YG |
753 | |
754 | BUG_ON(magic == NODE_INFO); | |
755 | ||
756 | maps_section_nr = pfn_to_section_nr(page_to_pfn(page)); | |
857e522a | 757 | removing_section_nr = page_private(page); |
0c0a4a51 YG |
758 | |
759 | /* | |
760 | * When this function is called, the removing section is | |
761 | * logical offlined state. This means all pages are isolated | |
762 | * from page allocator. If removing section's memmap is placed | |
763 | * on the same section, it must not be freed. | |
764 | * If it is freed, page allocator may allocate it which will | |
765 | * be removed physically soon. | |
766 | */ | |
767 | if (maps_section_nr != removing_section_nr) | |
768 | put_page_bootmem(page); | |
769 | } | |
770 | } | |
4edd7cef | 771 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
98f3cfc1 | 772 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
0b0acbec | 773 | |
29751f69 AW |
774 | /* |
775 | * returns the number of sections whose mem_maps were properly | |
776 | * set. If this is <=0, then that means that the passed-in | |
777 | * map was not consumed and must be freed. | |
778 | */ | |
4fe85d5a | 779 | int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn) |
29751f69 | 780 | { |
0b0acbec | 781 | unsigned long section_nr = pfn_to_section_nr(start_pfn); |
4fe85d5a | 782 | struct pglist_data *pgdat = zone->zone_pgdat; |
0b0acbec DH |
783 | struct mem_section *ms; |
784 | struct page *memmap; | |
5c0e3066 | 785 | unsigned long *usemap; |
0b0acbec DH |
786 | unsigned long flags; |
787 | int ret; | |
29751f69 | 788 | |
0b0acbec DH |
789 | /* |
790 | * no locking for this, because it does its own | |
791 | * plus, it does a kmalloc | |
792 | */ | |
bbd06825 WC |
793 | ret = sparse_index_init(section_nr, pgdat->node_id); |
794 | if (ret < 0 && ret != -EEXIST) | |
795 | return ret; | |
85b35fea | 796 | memmap = kmalloc_section_memmap(section_nr, pgdat->node_id); |
bbd06825 WC |
797 | if (!memmap) |
798 | return -ENOMEM; | |
5c0e3066 | 799 | usemap = __kmalloc_section_usemap(); |
bbd06825 | 800 | if (!usemap) { |
85b35fea | 801 | __kfree_section_memmap(memmap); |
bbd06825 WC |
802 | return -ENOMEM; |
803 | } | |
0b0acbec DH |
804 | |
805 | pgdat_resize_lock(pgdat, &flags); | |
29751f69 | 806 | |
0b0acbec DH |
807 | ms = __pfn_to_section(start_pfn); |
808 | if (ms->section_mem_map & SECTION_MARKED_PRESENT) { | |
809 | ret = -EEXIST; | |
810 | goto out; | |
811 | } | |
5c0e3066 | 812 | |
85b35fea | 813 | memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION); |
3ac19f8e | 814 | |
c4e1be9e | 815 | section_mark_present(ms); |
29751f69 | 816 | |
5c0e3066 | 817 | ret = sparse_init_one_section(ms, section_nr, memmap, usemap); |
0b0acbec | 818 | |
0b0acbec DH |
819 | out: |
820 | pgdat_resize_unlock(pgdat, &flags); | |
bbd06825 WC |
821 | if (ret <= 0) { |
822 | kfree(usemap); | |
85b35fea | 823 | __kfree_section_memmap(memmap); |
bbd06825 | 824 | } |
0b0acbec | 825 | return ret; |
29751f69 | 826 | } |
ea01ea93 | 827 | |
f3deb687 | 828 | #ifdef CONFIG_MEMORY_HOTREMOVE |
95a4774d WC |
829 | #ifdef CONFIG_MEMORY_FAILURE |
830 | static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) | |
831 | { | |
832 | int i; | |
833 | ||
834 | if (!memmap) | |
835 | return; | |
836 | ||
4b94ffdc | 837 | for (i = 0; i < nr_pages; i++) { |
95a4774d | 838 | if (PageHWPoison(&memmap[i])) { |
293c07e3 | 839 | atomic_long_sub(1, &num_poisoned_pages); |
95a4774d WC |
840 | ClearPageHWPoison(&memmap[i]); |
841 | } | |
842 | } | |
843 | } | |
844 | #else | |
845 | static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) | |
846 | { | |
847 | } | |
848 | #endif | |
849 | ||
4edd7cef DR |
850 | static void free_section_usemap(struct page *memmap, unsigned long *usemap) |
851 | { | |
852 | struct page *usemap_page; | |
4edd7cef DR |
853 | |
854 | if (!usemap) | |
855 | return; | |
856 | ||
857 | usemap_page = virt_to_page(usemap); | |
858 | /* | |
859 | * Check to see if allocation came from hot-plug-add | |
860 | */ | |
861 | if (PageSlab(usemap_page) || PageCompound(usemap_page)) { | |
862 | kfree(usemap); | |
863 | if (memmap) | |
85b35fea | 864 | __kfree_section_memmap(memmap); |
4edd7cef DR |
865 | return; |
866 | } | |
867 | ||
868 | /* | |
869 | * The usemap came from bootmem. This is packed with other usemaps | |
870 | * on the section which has pgdat at boot time. Just keep it as is now. | |
871 | */ | |
872 | ||
81556b02 ZY |
873 | if (memmap) |
874 | free_map_bootmem(memmap); | |
4edd7cef DR |
875 | } |
876 | ||
4b94ffdc DW |
877 | void sparse_remove_one_section(struct zone *zone, struct mem_section *ms, |
878 | unsigned long map_offset) | |
ea01ea93 BP |
879 | { |
880 | struct page *memmap = NULL; | |
cd099682 TC |
881 | unsigned long *usemap = NULL, flags; |
882 | struct pglist_data *pgdat = zone->zone_pgdat; | |
ea01ea93 | 883 | |
cd099682 | 884 | pgdat_resize_lock(pgdat, &flags); |
ea01ea93 BP |
885 | if (ms->section_mem_map) { |
886 | usemap = ms->pageblock_flags; | |
887 | memmap = sparse_decode_mem_map(ms->section_mem_map, | |
888 | __section_nr(ms)); | |
889 | ms->section_mem_map = 0; | |
890 | ms->pageblock_flags = NULL; | |
891 | } | |
cd099682 | 892 | pgdat_resize_unlock(pgdat, &flags); |
ea01ea93 | 893 | |
4b94ffdc DW |
894 | clear_hwpoisoned_pages(memmap + map_offset, |
895 | PAGES_PER_SECTION - map_offset); | |
ea01ea93 BP |
896 | free_section_usemap(memmap, usemap); |
897 | } | |
4edd7cef DR |
898 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
899 | #endif /* CONFIG_MEMORY_HOTPLUG */ |