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