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