]> asedeno.scripts.mit.edu Git - linux.git/blob - mm/sparse.c
mm/sparsemem: add helpers track active portions of a section at boot
[linux.git] / mm / sparse.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sparse memory mappings.
4  */
5 #include <linux/mm.h>
6 #include <linux/slab.h>
7 #include <linux/mmzone.h>
8 #include <linux/memblock.h>
9 #include <linux/compiler.h>
10 #include <linux/highmem.h>
11 #include <linux/export.h>
12 #include <linux/spinlock.h>
13 #include <linux/vmalloc.h>
14
15 #include "internal.h"
16 #include <asm/dma.h>
17 #include <asm/pgalloc.h>
18 #include <asm/pgtable.h>
19
20 /*
21  * Permanent SPARSEMEM data:
22  *
23  * 1) mem_section       - memory sections, mem_map's for valid memory
24  */
25 #ifdef CONFIG_SPARSEMEM_EXTREME
26 struct mem_section **mem_section;
27 #else
28 struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
29         ____cacheline_internodealigned_in_smp;
30 #endif
31 EXPORT_SYMBOL(mem_section);
32
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
40 static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
41 #else
42 static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
43 #endif
44
45 int page_to_nid(const struct page *page)
46 {
47         return section_to_node_table[page_to_section(page)];
48 }
49 EXPORT_SYMBOL(page_to_nid);
50
51 static 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 */
56 static inline void set_section_nid(unsigned long section_nr, int nid)
57 {
58 }
59 #endif
60
61 #ifdef CONFIG_SPARSEMEM_EXTREME
62 static noinline struct mem_section __ref *sparse_index_alloc(int nid)
63 {
64         struct mem_section *section = NULL;
65         unsigned long array_size = SECTIONS_PER_ROOT *
66                                    sizeof(struct mem_section);
67
68         if (slab_is_available()) {
69                 section = kzalloc_node(array_size, GFP_KERNEL, nid);
70         } else {
71                 section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
72                                               nid);
73                 if (!section)
74                         panic("%s: Failed to allocate %lu bytes nid=%d\n",
75                               __func__, array_size, nid);
76         }
77
78         return section;
79 }
80
81 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
82 {
83         unsigned long root = SECTION_NR_TO_ROOT(section_nr);
84         struct mem_section *section;
85
86         if (mem_section[root])
87                 return -EEXIST;
88
89         section = sparse_index_alloc(nid);
90         if (!section)
91                 return -ENOMEM;
92
93         mem_section[root] = section;
94
95         return 0;
96 }
97 #else /* !SPARSEMEM_EXTREME */
98 static inline int sparse_index_init(unsigned long section_nr, int nid)
99 {
100         return 0;
101 }
102 #endif
103
104 #ifdef CONFIG_SPARSEMEM_EXTREME
105 unsigned long __section_nr(struct mem_section *ms)
106 {
107         unsigned long root_nr;
108         struct mem_section *root = NULL;
109
110         for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
111                 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
112                 if (!root)
113                         continue;
114
115                 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
116                      break;
117         }
118
119         VM_BUG_ON(!root);
120
121         return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
122 }
123 #else
124 unsigned long __section_nr(struct mem_section *ms)
125 {
126         return (unsigned long)(ms - mem_section[0]);
127 }
128 #endif
129
130 /*
131  * During early boot, before section_mem_map is used for an actual
132  * mem_map, we use section_mem_map to store the section's NUMA
133  * node.  This keeps us from having to use another data structure.  The
134  * node information is cleared just before we store the real mem_map.
135  */
136 static inline unsigned long sparse_encode_early_nid(int nid)
137 {
138         return (nid << SECTION_NID_SHIFT);
139 }
140
141 static inline int sparse_early_nid(struct mem_section *section)
142 {
143         return (section->section_mem_map >> SECTION_NID_SHIFT);
144 }
145
146 /* Validate the physical addressing limitations of the model */
147 void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
148                                                 unsigned long *end_pfn)
149 {
150         unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
151
152         /*
153          * Sanity checks - do not allow an architecture to pass
154          * in larger pfns than the maximum scope of sparsemem:
155          */
156         if (*start_pfn > max_sparsemem_pfn) {
157                 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
158                         "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
159                         *start_pfn, *end_pfn, max_sparsemem_pfn);
160                 WARN_ON_ONCE(1);
161                 *start_pfn = max_sparsemem_pfn;
162                 *end_pfn = max_sparsemem_pfn;
163         } else if (*end_pfn > max_sparsemem_pfn) {
164                 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
165                         "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
166                         *start_pfn, *end_pfn, max_sparsemem_pfn);
167                 WARN_ON_ONCE(1);
168                 *end_pfn = max_sparsemem_pfn;
169         }
170 }
171
172 /*
173  * There are a number of times that we loop over NR_MEM_SECTIONS,
174  * looking for section_present() on each.  But, when we have very
175  * large physical address spaces, NR_MEM_SECTIONS can also be
176  * very large which makes the loops quite long.
177  *
178  * Keeping track of this gives us an easy way to break out of
179  * those loops early.
180  */
181 unsigned long __highest_present_section_nr;
182 static void section_mark_present(struct mem_section *ms)
183 {
184         unsigned long section_nr = __section_nr(ms);
185
186         if (section_nr > __highest_present_section_nr)
187                 __highest_present_section_nr = section_nr;
188
189         ms->section_mem_map |= SECTION_MARKED_PRESENT;
190 }
191
192 static inline unsigned long next_present_section_nr(unsigned long section_nr)
193 {
194         do {
195                 section_nr++;
196                 if (present_section_nr(section_nr))
197                         return section_nr;
198         } while ((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 != -1) &&                             \
205               (section_nr <= __highest_present_section_nr));    \
206              section_nr = next_present_section_nr(section_nr))
207
208 static inline unsigned long first_present_section_nr(void)
209 {
210         return next_present_section_nr(-1);
211 }
212
213 void subsection_mask_set(unsigned long *map, unsigned long pfn,
214                 unsigned long nr_pages)
215 {
216         int idx = subsection_map_index(pfn);
217         int end = subsection_map_index(pfn + nr_pages - 1);
218
219         bitmap_set(map, idx, end - idx + 1);
220 }
221
222 void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
223 {
224         int end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
225         int i, start_sec = pfn_to_section_nr(pfn);
226
227         if (!nr_pages)
228                 return;
229
230         for (i = start_sec; i <= end_sec; i++) {
231                 struct mem_section *ms;
232                 unsigned long pfns;
233
234                 pfns = min(nr_pages, PAGES_PER_SECTION
235                                 - (pfn & ~PAGE_SECTION_MASK));
236                 ms = __nr_to_section(i);
237                 subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
238
239                 pr_debug("%s: sec: %d pfns: %ld set(%d, %d)\n", __func__, i,
240                                 pfns, subsection_map_index(pfn),
241                                 subsection_map_index(pfn + pfns - 1));
242
243                 pfn += pfns;
244                 nr_pages -= pfns;
245         }
246 }
247
248 /* Record a memory area against a node. */
249 void __init memory_present(int nid, unsigned long start, unsigned long end)
250 {
251         unsigned long pfn;
252
253 #ifdef CONFIG_SPARSEMEM_EXTREME
254         if (unlikely(!mem_section)) {
255                 unsigned long size, align;
256
257                 size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
258                 align = 1 << (INTERNODE_CACHE_SHIFT);
259                 mem_section = memblock_alloc(size, align);
260                 if (!mem_section)
261                         panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
262                               __func__, size, align);
263         }
264 #endif
265
266         start &= PAGE_SECTION_MASK;
267         mminit_validate_memmodel_limits(&start, &end);
268         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
269                 unsigned long section = pfn_to_section_nr(pfn);
270                 struct mem_section *ms;
271
272                 sparse_index_init(section, nid);
273                 set_section_nid(section, nid);
274
275                 ms = __nr_to_section(section);
276                 if (!ms->section_mem_map) {
277                         ms->section_mem_map = sparse_encode_early_nid(nid) |
278                                                         SECTION_IS_ONLINE;
279                         section_mark_present(ms);
280                 }
281         }
282 }
283
284 /*
285  * Mark all memblocks as present using memory_present(). This is a
286  * convienence function that is useful for a number of arches
287  * to mark all of the systems memory as present during initialization.
288  */
289 void __init memblocks_present(void)
290 {
291         struct memblock_region *reg;
292
293         for_each_memblock(memory, reg) {
294                 memory_present(memblock_get_region_node(reg),
295                                memblock_region_memory_base_pfn(reg),
296                                memblock_region_memory_end_pfn(reg));
297         }
298 }
299
300 /*
301  * Subtle, we encode the real pfn into the mem_map such that
302  * the identity pfn - section_mem_map will return the actual
303  * physical page frame number.
304  */
305 static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
306 {
307         unsigned long coded_mem_map =
308                 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
309         BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
310         BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
311         return coded_mem_map;
312 }
313
314 /*
315  * Decode mem_map from the coded memmap
316  */
317 struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
318 {
319         /* mask off the extra low bits of information */
320         coded_mem_map &= SECTION_MAP_MASK;
321         return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
322 }
323
324 static void __meminit sparse_init_one_section(struct mem_section *ms,
325                 unsigned long pnum, struct page *mem_map,
326                 struct mem_section_usage *usage, unsigned long flags)
327 {
328         ms->section_mem_map &= ~SECTION_MAP_MASK;
329         ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum)
330                 | SECTION_HAS_MEM_MAP | flags;
331         ms->usage = usage;
332 }
333
334 static unsigned long usemap_size(void)
335 {
336         return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
337 }
338
339 size_t mem_section_usage_size(void)
340 {
341         return sizeof(struct mem_section_usage) + usemap_size();
342 }
343
344 #ifdef CONFIG_MEMORY_HOTREMOVE
345 static struct mem_section_usage * __init
346 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
347                                          unsigned long size)
348 {
349         struct mem_section_usage *usage;
350         unsigned long goal, limit;
351         int nid;
352         /*
353          * A page may contain usemaps for other sections preventing the
354          * page being freed and making a section unremovable while
355          * other sections referencing the usemap remain active. Similarly,
356          * a pgdat can prevent a section being removed. If section A
357          * contains a pgdat and section B contains the usemap, both
358          * sections become inter-dependent. This allocates usemaps
359          * from the same section as the pgdat where possible to avoid
360          * this problem.
361          */
362         goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
363         limit = goal + (1UL << PA_SECTION_SHIFT);
364         nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
365 again:
366         usage = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid);
367         if (!usage && limit) {
368                 limit = 0;
369                 goto again;
370         }
371         return usage;
372 }
373
374 static void __init check_usemap_section_nr(int nid,
375                 struct mem_section_usage *usage)
376 {
377         unsigned long usemap_snr, pgdat_snr;
378         static unsigned long old_usemap_snr;
379         static unsigned long old_pgdat_snr;
380         struct pglist_data *pgdat = NODE_DATA(nid);
381         int usemap_nid;
382
383         /* First call */
384         if (!old_usemap_snr) {
385                 old_usemap_snr = NR_MEM_SECTIONS;
386                 old_pgdat_snr = NR_MEM_SECTIONS;
387         }
388
389         usemap_snr = pfn_to_section_nr(__pa(usage) >> PAGE_SHIFT);
390         pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
391         if (usemap_snr == pgdat_snr)
392                 return;
393
394         if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
395                 /* skip redundant message */
396                 return;
397
398         old_usemap_snr = usemap_snr;
399         old_pgdat_snr = pgdat_snr;
400
401         usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
402         if (usemap_nid != nid) {
403                 pr_info("node %d must be removed before remove section %ld\n",
404                         nid, usemap_snr);
405                 return;
406         }
407         /*
408          * There is a circular dependency.
409          * Some platforms allow un-removable section because they will just
410          * gather other removable sections for dynamic partitioning.
411          * Just notify un-removable section's number here.
412          */
413         pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
414                 usemap_snr, pgdat_snr, nid);
415 }
416 #else
417 static struct mem_section_usage * __init
418 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
419                                          unsigned long size)
420 {
421         return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id);
422 }
423
424 static void __init check_usemap_section_nr(int nid,
425                 struct mem_section_usage *usage)
426 {
427 }
428 #endif /* CONFIG_MEMORY_HOTREMOVE */
429
430 #ifdef CONFIG_SPARSEMEM_VMEMMAP
431 static unsigned long __init section_map_size(void)
432 {
433         return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
434 }
435
436 #else
437 static unsigned long __init section_map_size(void)
438 {
439         return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
440 }
441
442 struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
443                 struct vmem_altmap *altmap)
444 {
445         unsigned long size = section_map_size();
446         struct page *map = sparse_buffer_alloc(size);
447         phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
448
449         if (map)
450                 return map;
451
452         map = memblock_alloc_try_nid(size,
453                                           PAGE_SIZE, addr,
454                                           MEMBLOCK_ALLOC_ACCESSIBLE, nid);
455         if (!map)
456                 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
457                       __func__, size, PAGE_SIZE, nid, &addr);
458
459         return map;
460 }
461 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
462
463 static void *sparsemap_buf __meminitdata;
464 static void *sparsemap_buf_end __meminitdata;
465
466 static void __init sparse_buffer_init(unsigned long size, int nid)
467 {
468         phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
469         WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
470         sparsemap_buf =
471                 memblock_alloc_try_nid_raw(size, PAGE_SIZE,
472                                                 addr,
473                                                 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
474         sparsemap_buf_end = sparsemap_buf + size;
475 }
476
477 static void __init sparse_buffer_fini(void)
478 {
479         unsigned long size = sparsemap_buf_end - sparsemap_buf;
480
481         if (sparsemap_buf && size > 0)
482                 memblock_free_early(__pa(sparsemap_buf), size);
483         sparsemap_buf = NULL;
484 }
485
486 void * __meminit sparse_buffer_alloc(unsigned long size)
487 {
488         void *ptr = NULL;
489
490         if (sparsemap_buf) {
491                 ptr = PTR_ALIGN(sparsemap_buf, size);
492                 if (ptr + size > sparsemap_buf_end)
493                         ptr = NULL;
494                 else
495                         sparsemap_buf = ptr + size;
496         }
497         return ptr;
498 }
499
500 void __weak __meminit vmemmap_populate_print_last(void)
501 {
502 }
503
504 /*
505  * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
506  * And number of present sections in this node is map_count.
507  */
508 static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
509                                    unsigned long pnum_end,
510                                    unsigned long map_count)
511 {
512         struct mem_section_usage *usage;
513         unsigned long pnum;
514         struct page *map;
515
516         usage = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
517                         mem_section_usage_size() * map_count);
518         if (!usage) {
519                 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
520                 goto failed;
521         }
522         sparse_buffer_init(map_count * section_map_size(), nid);
523         for_each_present_section_nr(pnum_begin, pnum) {
524                 if (pnum >= pnum_end)
525                         break;
526
527                 map = sparse_mem_map_populate(pnum, nid, NULL);
528                 if (!map) {
529                         pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
530                                __func__, nid);
531                         pnum_begin = pnum;
532                         goto failed;
533                 }
534                 check_usemap_section_nr(nid, usage);
535                 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
536                                 SECTION_IS_EARLY);
537                 usage = (void *) usage + mem_section_usage_size();
538         }
539         sparse_buffer_fini();
540         return;
541 failed:
542         /* We failed to allocate, mark all the following pnums as not present */
543         for_each_present_section_nr(pnum_begin, pnum) {
544                 struct mem_section *ms;
545
546                 if (pnum >= pnum_end)
547                         break;
548                 ms = __nr_to_section(pnum);
549                 ms->section_mem_map = 0;
550         }
551 }
552
553 /*
554  * Allocate the accumulated non-linear sections, allocate a mem_map
555  * for each and record the physical to section mapping.
556  */
557 void __init sparse_init(void)
558 {
559         unsigned long pnum_begin = first_present_section_nr();
560         int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
561         unsigned long pnum_end, map_count = 1;
562
563         /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
564         set_pageblock_order();
565
566         for_each_present_section_nr(pnum_begin + 1, pnum_end) {
567                 int nid = sparse_early_nid(__nr_to_section(pnum_end));
568
569                 if (nid == nid_begin) {
570                         map_count++;
571                         continue;
572                 }
573                 /* Init node with sections in range [pnum_begin, pnum_end) */
574                 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
575                 nid_begin = nid;
576                 pnum_begin = pnum_end;
577                 map_count = 1;
578         }
579         /* cover the last node */
580         sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
581         vmemmap_populate_print_last();
582 }
583
584 #ifdef CONFIG_MEMORY_HOTPLUG
585
586 /* Mark all memory sections within the pfn range as online */
587 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
588 {
589         unsigned long pfn;
590
591         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
592                 unsigned long section_nr = pfn_to_section_nr(pfn);
593                 struct mem_section *ms;
594
595                 /* onlining code should never touch invalid ranges */
596                 if (WARN_ON(!valid_section_nr(section_nr)))
597                         continue;
598
599                 ms = __nr_to_section(section_nr);
600                 ms->section_mem_map |= SECTION_IS_ONLINE;
601         }
602 }
603
604 #ifdef CONFIG_MEMORY_HOTREMOVE
605 /* Mark all memory sections within the pfn range as offline */
606 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
607 {
608         unsigned long pfn;
609
610         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
611                 unsigned long section_nr = pfn_to_section_nr(pfn);
612                 struct mem_section *ms;
613
614                 /*
615                  * TODO this needs some double checking. Offlining code makes
616                  * sure to check pfn_valid but those checks might be just bogus
617                  */
618                 if (WARN_ON(!valid_section_nr(section_nr)))
619                         continue;
620
621                 ms = __nr_to_section(section_nr);
622                 ms->section_mem_map &= ~SECTION_IS_ONLINE;
623         }
624 }
625 #endif
626
627 #ifdef CONFIG_SPARSEMEM_VMEMMAP
628 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
629                 struct vmem_altmap *altmap)
630 {
631         /* This will make the necessary allocations eventually. */
632         return sparse_mem_map_populate(pnum, nid, altmap);
633 }
634 static void __kfree_section_memmap(struct page *memmap,
635                 struct vmem_altmap *altmap)
636 {
637         unsigned long start = (unsigned long)memmap;
638         unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
639
640         vmemmap_free(start, end, altmap);
641 }
642 static void free_map_bootmem(struct page *memmap)
643 {
644         unsigned long start = (unsigned long)memmap;
645         unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
646
647         vmemmap_free(start, end, NULL);
648 }
649 #else
650 static struct page *__kmalloc_section_memmap(void)
651 {
652         struct page *page, *ret;
653         unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
654
655         page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
656         if (page)
657                 goto got_map_page;
658
659         ret = vmalloc(memmap_size);
660         if (ret)
661                 goto got_map_ptr;
662
663         return NULL;
664 got_map_page:
665         ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
666 got_map_ptr:
667
668         return ret;
669 }
670
671 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
672                 struct vmem_altmap *altmap)
673 {
674         return __kmalloc_section_memmap();
675 }
676
677 static void __kfree_section_memmap(struct page *memmap,
678                 struct vmem_altmap *altmap)
679 {
680         if (is_vmalloc_addr(memmap))
681                 vfree(memmap);
682         else
683                 free_pages((unsigned long)memmap,
684                            get_order(sizeof(struct page) * PAGES_PER_SECTION));
685 }
686
687 static void free_map_bootmem(struct page *memmap)
688 {
689         unsigned long maps_section_nr, removing_section_nr, i;
690         unsigned long magic, nr_pages;
691         struct page *page = virt_to_page(memmap);
692
693         nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
694                 >> PAGE_SHIFT;
695
696         for (i = 0; i < nr_pages; i++, page++) {
697                 magic = (unsigned long) page->freelist;
698
699                 BUG_ON(magic == NODE_INFO);
700
701                 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
702                 removing_section_nr = page_private(page);
703
704                 /*
705                  * When this function is called, the removing section is
706                  * logical offlined state. This means all pages are isolated
707                  * from page allocator. If removing section's memmap is placed
708                  * on the same section, it must not be freed.
709                  * If it is freed, page allocator may allocate it which will
710                  * be removed physically soon.
711                  */
712                 if (maps_section_nr != removing_section_nr)
713                         put_page_bootmem(page);
714         }
715 }
716 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
717
718 /**
719  * sparse_add_one_section - add a memory section
720  * @nid: The node to add section on
721  * @start_pfn: start pfn of the memory range
722  * @altmap: device page map
723  *
724  * This is only intended for hotplug.
725  *
726  * Return:
727  * * 0          - On success.
728  * * -EEXIST    - Section has been present.
729  * * -ENOMEM    - Out of memory.
730  */
731 int __meminit sparse_add_one_section(int nid, unsigned long start_pfn,
732                                      struct vmem_altmap *altmap)
733 {
734         unsigned long section_nr = pfn_to_section_nr(start_pfn);
735         struct mem_section_usage *usage;
736         struct mem_section *ms;
737         struct page *memmap;
738         int ret;
739
740         /*
741          * no locking for this, because it does its own
742          * plus, it does a kmalloc
743          */
744         ret = sparse_index_init(section_nr, nid);
745         if (ret < 0 && ret != -EEXIST)
746                 return ret;
747         ret = 0;
748         memmap = kmalloc_section_memmap(section_nr, nid, altmap);
749         if (!memmap)
750                 return -ENOMEM;
751         usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
752         if (!usage) {
753                 __kfree_section_memmap(memmap, altmap);
754                 return -ENOMEM;
755         }
756
757         ms = __pfn_to_section(start_pfn);
758         if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
759                 ret = -EEXIST;
760                 goto out;
761         }
762
763         /*
764          * Poison uninitialized struct pages in order to catch invalid flags
765          * combinations.
766          */
767         page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
768
769         set_section_nid(section_nr, nid);
770         section_mark_present(ms);
771         sparse_init_one_section(ms, section_nr, memmap, usage, 0);
772
773 out:
774         if (ret < 0) {
775                 kfree(usage);
776                 __kfree_section_memmap(memmap, altmap);
777         }
778         return ret;
779 }
780
781 #ifdef CONFIG_MEMORY_FAILURE
782 static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
783 {
784         int i;
785
786         if (!memmap)
787                 return;
788
789         /*
790          * A further optimization is to have per section refcounted
791          * num_poisoned_pages.  But that would need more space per memmap, so
792          * for now just do a quick global check to speed up this routine in the
793          * absence of bad pages.
794          */
795         if (atomic_long_read(&num_poisoned_pages) == 0)
796                 return;
797
798         for (i = 0; i < nr_pages; i++) {
799                 if (PageHWPoison(&memmap[i])) {
800                         atomic_long_sub(1, &num_poisoned_pages);
801                         ClearPageHWPoison(&memmap[i]);
802                 }
803         }
804 }
805 #else
806 static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
807 {
808 }
809 #endif
810
811 static void free_section_usage(struct mem_section *ms, struct page *memmap,
812                 struct mem_section_usage *usage, struct vmem_altmap *altmap)
813 {
814         if (!usage)
815                 return;
816
817         /*
818          * Check to see if allocation came from hot-plug-add
819          */
820         if (!early_section(ms)) {
821                 kfree(usage);
822                 if (memmap)
823                         __kfree_section_memmap(memmap, altmap);
824                 return;
825         }
826
827         /*
828          * The usemap came from bootmem. This is packed with other usemaps
829          * on the section which has pgdat at boot time. Just keep it as is now.
830          */
831
832         if (memmap)
833                 free_map_bootmem(memmap);
834 }
835
836 void sparse_remove_one_section(struct mem_section *ms, unsigned long map_offset,
837                                struct vmem_altmap *altmap)
838 {
839         struct page *memmap = NULL;
840         struct mem_section_usage *usage = NULL;
841
842         if (ms->section_mem_map) {
843                 usage = ms->usage;
844                 memmap = sparse_decode_mem_map(ms->section_mem_map,
845                                                 __section_nr(ms));
846                 ms->section_mem_map = 0;
847                 ms->usage = NULL;
848         }
849
850         clear_hwpoisoned_pages(memmap + map_offset,
851                         PAGES_PER_SECTION - map_offset);
852         free_section_usage(ms, memmap, usage, altmap);
853 }
854 #endif /* CONFIG_MEMORY_HOTPLUG */