OSDN Git Service

7397fb4e78b4e09de3dcd00de9eb752577ff2241
[uclinux-h8/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 int __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 int __section_nr(struct mem_section* ms)
125 {
126         return (int)(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 int __highest_present_section_nr;
182 static void section_mark_present(struct mem_section *ms)
183 {
184         int 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 int next_present_section_nr(int 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 /* Record a memory area against a node. */
214 void __init memory_present(int nid, unsigned long start, unsigned long end)
215 {
216         unsigned long pfn;
217
218 #ifdef CONFIG_SPARSEMEM_EXTREME
219         if (unlikely(!mem_section)) {
220                 unsigned long size, align;
221
222                 size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
223                 align = 1 << (INTERNODE_CACHE_SHIFT);
224                 mem_section = memblock_alloc(size, align);
225                 if (!mem_section)
226                         panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
227                               __func__, size, align);
228         }
229 #endif
230
231         start &= PAGE_SECTION_MASK;
232         mminit_validate_memmodel_limits(&start, &end);
233         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
234                 unsigned long section = pfn_to_section_nr(pfn);
235                 struct mem_section *ms;
236
237                 sparse_index_init(section, nid);
238                 set_section_nid(section, nid);
239
240                 ms = __nr_to_section(section);
241                 if (!ms->section_mem_map) {
242                         ms->section_mem_map = sparse_encode_early_nid(nid) |
243                                                         SECTION_IS_ONLINE;
244                         section_mark_present(ms);
245                 }
246         }
247 }
248
249 /*
250  * Mark all memblocks as present using memory_present(). This is a
251  * convienence function that is useful for a number of arches
252  * to mark all of the systems memory as present during initialization.
253  */
254 void __init memblocks_present(void)
255 {
256         struct memblock_region *reg;
257
258         for_each_memblock(memory, reg) {
259                 memory_present(memblock_get_region_node(reg),
260                                memblock_region_memory_base_pfn(reg),
261                                memblock_region_memory_end_pfn(reg));
262         }
263 }
264
265 /*
266  * Subtle, we encode the real pfn into the mem_map such that
267  * the identity pfn - section_mem_map will return the actual
268  * physical page frame number.
269  */
270 static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
271 {
272         unsigned long coded_mem_map =
273                 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
274         BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
275         BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
276         return coded_mem_map;
277 }
278
279 /*
280  * Decode mem_map from the coded memmap
281  */
282 struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
283 {
284         /* mask off the extra low bits of information */
285         coded_mem_map &= SECTION_MAP_MASK;
286         return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
287 }
288
289 static void __meminit sparse_init_one_section(struct mem_section *ms,
290                 unsigned long pnum, struct page *mem_map,
291                 unsigned long *pageblock_bitmap)
292 {
293         ms->section_mem_map &= ~SECTION_MAP_MASK;
294         ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
295                                                         SECTION_HAS_MEM_MAP;
296         ms->pageblock_flags = pageblock_bitmap;
297 }
298
299 unsigned long usemap_size(void)
300 {
301         return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
302 }
303
304 #ifdef CONFIG_MEMORY_HOTPLUG
305 static unsigned long *__kmalloc_section_usemap(void)
306 {
307         return kmalloc(usemap_size(), GFP_KERNEL);
308 }
309 #endif /* CONFIG_MEMORY_HOTPLUG */
310
311 #ifdef CONFIG_MEMORY_HOTREMOVE
312 static unsigned long * __init
313 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
314                                          unsigned long size)
315 {
316         unsigned long goal, limit;
317         unsigned long *p;
318         int nid;
319         /*
320          * A page may contain usemaps for other sections preventing the
321          * page being freed and making a section unremovable while
322          * other sections referencing the usemap remain active. Similarly,
323          * a pgdat can prevent a section being removed. If section A
324          * contains a pgdat and section B contains the usemap, both
325          * sections become inter-dependent. This allocates usemaps
326          * from the same section as the pgdat where possible to avoid
327          * this problem.
328          */
329         goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
330         limit = goal + (1UL << PA_SECTION_SHIFT);
331         nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
332 again:
333         p = memblock_alloc_try_nid_nopanic(size,
334                                                 SMP_CACHE_BYTES, goal, limit,
335                                                 nid);
336         if (!p && limit) {
337                 limit = 0;
338                 goto again;
339         }
340         return p;
341 }
342
343 static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
344 {
345         unsigned long usemap_snr, pgdat_snr;
346         static unsigned long old_usemap_snr;
347         static unsigned long old_pgdat_snr;
348         struct pglist_data *pgdat = NODE_DATA(nid);
349         int usemap_nid;
350
351         /* First call */
352         if (!old_usemap_snr) {
353                 old_usemap_snr = NR_MEM_SECTIONS;
354                 old_pgdat_snr = NR_MEM_SECTIONS;
355         }
356
357         usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
358         pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
359         if (usemap_snr == pgdat_snr)
360                 return;
361
362         if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
363                 /* skip redundant message */
364                 return;
365
366         old_usemap_snr = usemap_snr;
367         old_pgdat_snr = pgdat_snr;
368
369         usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
370         if (usemap_nid != nid) {
371                 pr_info("node %d must be removed before remove section %ld\n",
372                         nid, usemap_snr);
373                 return;
374         }
375         /*
376          * There is a circular dependency.
377          * Some platforms allow un-removable section because they will just
378          * gather other removable sections for dynamic partitioning.
379          * Just notify un-removable section's number here.
380          */
381         pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
382                 usemap_snr, pgdat_snr, nid);
383 }
384 #else
385 static unsigned long * __init
386 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
387                                          unsigned long size)
388 {
389         return memblock_alloc_node_nopanic(size, pgdat->node_id);
390 }
391
392 static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
393 {
394 }
395 #endif /* CONFIG_MEMORY_HOTREMOVE */
396
397 #ifdef CONFIG_SPARSEMEM_VMEMMAP
398 static unsigned long __init section_map_size(void)
399 {
400         return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
401 }
402
403 #else
404 static unsigned long __init section_map_size(void)
405 {
406         return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
407 }
408
409 struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
410                 struct vmem_altmap *altmap)
411 {
412         unsigned long size = section_map_size();
413         struct page *map = sparse_buffer_alloc(size);
414         phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
415
416         if (map)
417                 return map;
418
419         map = memblock_alloc_try_nid(size,
420                                           PAGE_SIZE, addr,
421                                           MEMBLOCK_ALLOC_ACCESSIBLE, nid);
422         if (!map)
423                 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
424                       __func__, size, PAGE_SIZE, nid, &addr);
425
426         return map;
427 }
428 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
429
430 static void *sparsemap_buf __meminitdata;
431 static void *sparsemap_buf_end __meminitdata;
432
433 static void __init sparse_buffer_init(unsigned long size, int nid)
434 {
435         phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
436         WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
437         sparsemap_buf =
438                 memblock_alloc_try_nid_raw(size, PAGE_SIZE,
439                                                 addr,
440                                                 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
441         sparsemap_buf_end = sparsemap_buf + size;
442 }
443
444 static void __init sparse_buffer_fini(void)
445 {
446         unsigned long size = sparsemap_buf_end - sparsemap_buf;
447
448         if (sparsemap_buf && size > 0)
449                 memblock_free_early(__pa(sparsemap_buf), size);
450         sparsemap_buf = NULL;
451 }
452
453 void * __meminit sparse_buffer_alloc(unsigned long size)
454 {
455         void *ptr = NULL;
456
457         if (sparsemap_buf) {
458                 ptr = PTR_ALIGN(sparsemap_buf, size);
459                 if (ptr + size > sparsemap_buf_end)
460                         ptr = NULL;
461                 else
462                         sparsemap_buf = ptr + size;
463         }
464         return ptr;
465 }
466
467 void __weak __meminit vmemmap_populate_print_last(void)
468 {
469 }
470
471 /*
472  * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
473  * And number of present sections in this node is map_count.
474  */
475 static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
476                                    unsigned long pnum_end,
477                                    unsigned long map_count)
478 {
479         unsigned long pnum, usemap_longs, *usemap;
480         struct page *map;
481
482         usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS);
483         usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
484                                                           usemap_size() *
485                                                           map_count);
486         if (!usemap) {
487                 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
488                 goto failed;
489         }
490         sparse_buffer_init(map_count * section_map_size(), nid);
491         for_each_present_section_nr(pnum_begin, pnum) {
492                 if (pnum >= pnum_end)
493                         break;
494
495                 map = sparse_mem_map_populate(pnum, nid, NULL);
496                 if (!map) {
497                         pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
498                                __func__, nid);
499                         pnum_begin = pnum;
500                         goto failed;
501                 }
502                 check_usemap_section_nr(nid, usemap);
503                 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap);
504                 usemap += usemap_longs;
505         }
506         sparse_buffer_fini();
507         return;
508 failed:
509         /* We failed to allocate, mark all the following pnums as not present */
510         for_each_present_section_nr(pnum_begin, pnum) {
511                 struct mem_section *ms;
512
513                 if (pnum >= pnum_end)
514                         break;
515                 ms = __nr_to_section(pnum);
516                 ms->section_mem_map = 0;
517         }
518 }
519
520 /*
521  * Allocate the accumulated non-linear sections, allocate a mem_map
522  * for each and record the physical to section mapping.
523  */
524 void __init sparse_init(void)
525 {
526         unsigned long pnum_begin = first_present_section_nr();
527         int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
528         unsigned long pnum_end, map_count = 1;
529
530         /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
531         set_pageblock_order();
532
533         for_each_present_section_nr(pnum_begin + 1, pnum_end) {
534                 int nid = sparse_early_nid(__nr_to_section(pnum_end));
535
536                 if (nid == nid_begin) {
537                         map_count++;
538                         continue;
539                 }
540                 /* Init node with sections in range [pnum_begin, pnum_end) */
541                 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
542                 nid_begin = nid;
543                 pnum_begin = pnum_end;
544                 map_count = 1;
545         }
546         /* cover the last node */
547         sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
548         vmemmap_populate_print_last();
549 }
550
551 #ifdef CONFIG_MEMORY_HOTPLUG
552
553 /* Mark all memory sections within the pfn range as online */
554 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
555 {
556         unsigned long pfn;
557
558         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
559                 unsigned long section_nr = pfn_to_section_nr(pfn);
560                 struct mem_section *ms;
561
562                 /* onlining code should never touch invalid ranges */
563                 if (WARN_ON(!valid_section_nr(section_nr)))
564                         continue;
565
566                 ms = __nr_to_section(section_nr);
567                 ms->section_mem_map |= SECTION_IS_ONLINE;
568         }
569 }
570
571 #ifdef CONFIG_MEMORY_HOTREMOVE
572 /* Mark all memory sections within the pfn range as online */
573 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
574 {
575         unsigned long pfn;
576
577         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
578                 unsigned long section_nr = pfn_to_section_nr(pfn);
579                 struct mem_section *ms;
580
581                 /*
582                  * TODO this needs some double checking. Offlining code makes
583                  * sure to check pfn_valid but those checks might be just bogus
584                  */
585                 if (WARN_ON(!valid_section_nr(section_nr)))
586                         continue;
587
588                 ms = __nr_to_section(section_nr);
589                 ms->section_mem_map &= ~SECTION_IS_ONLINE;
590         }
591 }
592 #endif
593
594 #ifdef CONFIG_SPARSEMEM_VMEMMAP
595 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
596                 struct vmem_altmap *altmap)
597 {
598         /* This will make the necessary allocations eventually. */
599         return sparse_mem_map_populate(pnum, nid, altmap);
600 }
601 static void __kfree_section_memmap(struct page *memmap,
602                 struct vmem_altmap *altmap)
603 {
604         unsigned long start = (unsigned long)memmap;
605         unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
606
607         vmemmap_free(start, end, altmap);
608 }
609 #ifdef CONFIG_MEMORY_HOTREMOVE
610 static void free_map_bootmem(struct page *memmap)
611 {
612         unsigned long start = (unsigned long)memmap;
613         unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
614
615         vmemmap_free(start, end, NULL);
616 }
617 #endif /* CONFIG_MEMORY_HOTREMOVE */
618 #else
619 static struct page *__kmalloc_section_memmap(void)
620 {
621         struct page *page, *ret;
622         unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
623
624         page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
625         if (page)
626                 goto got_map_page;
627
628         ret = vmalloc(memmap_size);
629         if (ret)
630                 goto got_map_ptr;
631
632         return NULL;
633 got_map_page:
634         ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
635 got_map_ptr:
636
637         return ret;
638 }
639
640 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
641                 struct vmem_altmap *altmap)
642 {
643         return __kmalloc_section_memmap();
644 }
645
646 static void __kfree_section_memmap(struct page *memmap,
647                 struct vmem_altmap *altmap)
648 {
649         if (is_vmalloc_addr(memmap))
650                 vfree(memmap);
651         else
652                 free_pages((unsigned long)memmap,
653                            get_order(sizeof(struct page) * PAGES_PER_SECTION));
654 }
655
656 #ifdef CONFIG_MEMORY_HOTREMOVE
657 static void free_map_bootmem(struct page *memmap)
658 {
659         unsigned long maps_section_nr, removing_section_nr, i;
660         unsigned long magic, nr_pages;
661         struct page *page = virt_to_page(memmap);
662
663         nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
664                 >> PAGE_SHIFT;
665
666         for (i = 0; i < nr_pages; i++, page++) {
667                 magic = (unsigned long) page->freelist;
668
669                 BUG_ON(magic == NODE_INFO);
670
671                 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
672                 removing_section_nr = page_private(page);
673
674                 /*
675                  * When this function is called, the removing section is
676                  * logical offlined state. This means all pages are isolated
677                  * from page allocator. If removing section's memmap is placed
678                  * on the same section, it must not be freed.
679                  * If it is freed, page allocator may allocate it which will
680                  * be removed physically soon.
681                  */
682                 if (maps_section_nr != removing_section_nr)
683                         put_page_bootmem(page);
684         }
685 }
686 #endif /* CONFIG_MEMORY_HOTREMOVE */
687 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
688
689 /*
690  * returns the number of sections whose mem_maps were properly
691  * set.  If this is <=0, then that means that the passed-in
692  * map was not consumed and must be freed.
693  */
694 int __meminit sparse_add_one_section(int nid, unsigned long start_pfn,
695                                      struct vmem_altmap *altmap)
696 {
697         unsigned long section_nr = pfn_to_section_nr(start_pfn);
698         struct mem_section *ms;
699         struct page *memmap;
700         unsigned long *usemap;
701         int ret;
702
703         /*
704          * no locking for this, because it does its own
705          * plus, it does a kmalloc
706          */
707         ret = sparse_index_init(section_nr, nid);
708         if (ret < 0 && ret != -EEXIST)
709                 return ret;
710         ret = 0;
711         memmap = kmalloc_section_memmap(section_nr, nid, altmap);
712         if (!memmap)
713                 return -ENOMEM;
714         usemap = __kmalloc_section_usemap();
715         if (!usemap) {
716                 __kfree_section_memmap(memmap, altmap);
717                 return -ENOMEM;
718         }
719
720         ms = __pfn_to_section(start_pfn);
721         if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
722                 ret = -EEXIST;
723                 goto out;
724         }
725
726         /*
727          * Poison uninitialized struct pages in order to catch invalid flags
728          * combinations.
729          */
730         page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
731
732         section_mark_present(ms);
733         sparse_init_one_section(ms, section_nr, memmap, usemap);
734
735 out:
736         if (ret < 0) {
737                 kfree(usemap);
738                 __kfree_section_memmap(memmap, altmap);
739         }
740         return ret;
741 }
742
743 #ifdef CONFIG_MEMORY_HOTREMOVE
744 #ifdef CONFIG_MEMORY_FAILURE
745 static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
746 {
747         int i;
748
749         if (!memmap)
750                 return;
751
752         /*
753          * A further optimization is to have per section refcounted
754          * num_poisoned_pages.  But that would need more space per memmap, so
755          * for now just do a quick global check to speed up this routine in the
756          * absence of bad pages.
757          */
758         if (atomic_long_read(&num_poisoned_pages) == 0)
759                 return;
760
761         for (i = 0; i < nr_pages; i++) {
762                 if (PageHWPoison(&memmap[i])) {
763                         atomic_long_sub(1, &num_poisoned_pages);
764                         ClearPageHWPoison(&memmap[i]);
765                 }
766         }
767 }
768 #else
769 static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
770 {
771 }
772 #endif
773
774 static void free_section_usemap(struct page *memmap, unsigned long *usemap,
775                 struct vmem_altmap *altmap)
776 {
777         struct page *usemap_page;
778
779         if (!usemap)
780                 return;
781
782         usemap_page = virt_to_page(usemap);
783         /*
784          * Check to see if allocation came from hot-plug-add
785          */
786         if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
787                 kfree(usemap);
788                 if (memmap)
789                         __kfree_section_memmap(memmap, altmap);
790                 return;
791         }
792
793         /*
794          * The usemap came from bootmem. This is packed with other usemaps
795          * on the section which has pgdat at boot time. Just keep it as is now.
796          */
797
798         if (memmap)
799                 free_map_bootmem(memmap);
800 }
801
802 void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
803                 unsigned long map_offset, struct vmem_altmap *altmap)
804 {
805         struct page *memmap = NULL;
806         unsigned long *usemap = NULL;
807
808         if (ms->section_mem_map) {
809                 usemap = ms->pageblock_flags;
810                 memmap = sparse_decode_mem_map(ms->section_mem_map,
811                                                 __section_nr(ms));
812                 ms->section_mem_map = 0;
813                 ms->pageblock_flags = NULL;
814         }
815
816         clear_hwpoisoned_pages(memmap + map_offset,
817                         PAGES_PER_SECTION - map_offset);
818         free_section_usemap(memmap, usemap, altmap);
819 }
820 #endif /* CONFIG_MEMORY_HOTREMOVE */
821 #endif /* CONFIG_MEMORY_HOTPLUG */