1
2
3
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
22
23
24
25#ifdef CONFIG_SPARSEMEM_EXTREME
26struct mem_section **mem_section;
27#else
28struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
29 ____cacheline_internodealigned_in_smp;
30#endif
31EXPORT_SYMBOL(mem_section);
32
33#ifdef NODE_NOT_IN_PAGE_FLAGS
34
35
36
37
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
45int page_to_nid(const struct page *page)
46{
47 return section_to_node_table[page_to_section(page)];
48}
49EXPORT_SYMBOL(page_to_nid);
50
51static void set_section_nid(unsigned long section_nr, int nid)
52{
53 section_to_node_table[section_nr] = nid;
54}
55#else
56static inline void set_section_nid(unsigned long section_nr, int nid)
57{
58}
59#endif
60
61#ifdef CONFIG_SPARSEMEM_EXTREME
62static 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
81static 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
98static inline int sparse_index_init(unsigned long section_nr, int nid)
99{
100 return 0;
101}
102#endif
103
104#ifdef CONFIG_SPARSEMEM_EXTREME
105int __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
124int __section_nr(struct mem_section* ms)
125{
126 return (int)(ms - mem_section[0]);
127}
128#endif
129
130
131
132
133
134
135
136static inline unsigned long sparse_encode_early_nid(int nid)
137{
138 return (nid << SECTION_NID_SHIFT);
139}
140
141static inline int sparse_early_nid(struct mem_section *section)
142{
143 return (section->section_mem_map >> SECTION_NID_SHIFT);
144}
145
146
147void __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
154
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
174
175
176
177
178
179
180
181int __highest_present_section_nr;
182static 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
192static 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
208static inline unsigned long first_present_section_nr(void)
209{
210 return next_present_section_nr(-1);
211}
212
213
214void __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
251
252
253
254void __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
267
268
269
270static 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
281
282struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
283{
284
285 coded_mem_map &= SECTION_MAP_MASK;
286 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
287}
288
289static 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
299unsigned long usemap_size(void)
300{
301 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
302}
303
304#ifdef CONFIG_MEMORY_HOTPLUG
305static unsigned long *__kmalloc_section_usemap(void)
306{
307 return kmalloc(usemap_size(), GFP_KERNEL);
308}
309#endif
310
311#ifdef CONFIG_MEMORY_HOTREMOVE
312static unsigned long * __init
313sparse_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
321
322
323
324
325
326
327
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);
332again:
333 p = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid);
334 if (!p && limit) {
335 limit = 0;
336 goto again;
337 }
338 return p;
339}
340
341static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
342{
343 unsigned long usemap_snr, pgdat_snr;
344 static unsigned long old_usemap_snr;
345 static unsigned long old_pgdat_snr;
346 struct pglist_data *pgdat = NODE_DATA(nid);
347 int usemap_nid;
348
349
350 if (!old_usemap_snr) {
351 old_usemap_snr = NR_MEM_SECTIONS;
352 old_pgdat_snr = NR_MEM_SECTIONS;
353 }
354
355 usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
356 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
357 if (usemap_snr == pgdat_snr)
358 return;
359
360 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
361
362 return;
363
364 old_usemap_snr = usemap_snr;
365 old_pgdat_snr = pgdat_snr;
366
367 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
368 if (usemap_nid != nid) {
369 pr_info("node %d must be removed before remove section %ld\n",
370 nid, usemap_snr);
371 return;
372 }
373
374
375
376
377
378
379 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
380 usemap_snr, pgdat_snr, nid);
381}
382#else
383static unsigned long * __init
384sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
385 unsigned long size)
386{
387 return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id);
388}
389
390static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
391{
392}
393#endif
394
395#ifdef CONFIG_SPARSEMEM_VMEMMAP
396static unsigned long __init section_map_size(void)
397{
398 return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
399}
400
401#else
402static unsigned long __init section_map_size(void)
403{
404 return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
405}
406
407struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
408 struct vmem_altmap *altmap)
409{
410 unsigned long size = section_map_size();
411 struct page *map = sparse_buffer_alloc(size);
412 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
413
414 if (map)
415 return map;
416
417 map = memblock_alloc_try_nid(size,
418 PAGE_SIZE, addr,
419 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
420 if (!map)
421 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
422 __func__, size, PAGE_SIZE, nid, &addr);
423
424 return map;
425}
426#endif
427
428static void *sparsemap_buf __meminitdata;
429static void *sparsemap_buf_end __meminitdata;
430
431static void __init sparse_buffer_init(unsigned long size, int nid)
432{
433 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
434 WARN_ON(sparsemap_buf);
435 sparsemap_buf =
436 memblock_alloc_try_nid_raw(size, PAGE_SIZE,
437 addr,
438 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
439 sparsemap_buf_end = sparsemap_buf + size;
440}
441
442static void __init sparse_buffer_fini(void)
443{
444 unsigned long size = sparsemap_buf_end - sparsemap_buf;
445
446 if (sparsemap_buf && size > 0)
447 memblock_free_early(__pa(sparsemap_buf), size);
448 sparsemap_buf = NULL;
449}
450
451void * __meminit sparse_buffer_alloc(unsigned long size)
452{
453 void *ptr = NULL;
454
455 if (sparsemap_buf) {
456 ptr = PTR_ALIGN(sparsemap_buf, size);
457 if (ptr + size > sparsemap_buf_end)
458 ptr = NULL;
459 else
460 sparsemap_buf = ptr + size;
461 }
462 return ptr;
463}
464
465void __weak __meminit vmemmap_populate_print_last(void)
466{
467}
468
469
470
471
472
473static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
474 unsigned long pnum_end,
475 unsigned long map_count)
476{
477 unsigned long pnum, usemap_longs, *usemap;
478 struct page *map;
479
480 usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS);
481 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
482 usemap_size() *
483 map_count);
484 if (!usemap) {
485 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
486 goto failed;
487 }
488 sparse_buffer_init(map_count * section_map_size(), nid);
489 for_each_present_section_nr(pnum_begin, pnum) {
490 if (pnum >= pnum_end)
491 break;
492
493 map = sparse_mem_map_populate(pnum, nid, NULL);
494 if (!map) {
495 pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
496 __func__, nid);
497 pnum_begin = pnum;
498 goto failed;
499 }
500 check_usemap_section_nr(nid, usemap);
501 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap);
502 usemap += usemap_longs;
503 }
504 sparse_buffer_fini();
505 return;
506failed:
507
508 for_each_present_section_nr(pnum_begin, pnum) {
509 struct mem_section *ms;
510
511 if (pnum >= pnum_end)
512 break;
513 ms = __nr_to_section(pnum);
514 ms->section_mem_map = 0;
515 }
516}
517
518
519
520
521
522void __init sparse_init(void)
523{
524 unsigned long pnum_begin = first_present_section_nr();
525 int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
526 unsigned long pnum_end, map_count = 1;
527
528
529 set_pageblock_order();
530
531 for_each_present_section_nr(pnum_begin + 1, pnum_end) {
532 int nid = sparse_early_nid(__nr_to_section(pnum_end));
533
534 if (nid == nid_begin) {
535 map_count++;
536 continue;
537 }
538
539 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
540 nid_begin = nid;
541 pnum_begin = pnum_end;
542 map_count = 1;
543 }
544
545 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
546 vmemmap_populate_print_last();
547}
548
549#ifdef CONFIG_MEMORY_HOTPLUG
550
551
552void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
553{
554 unsigned long pfn;
555
556 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
557 unsigned long section_nr = pfn_to_section_nr(pfn);
558 struct mem_section *ms;
559
560
561 if (WARN_ON(!valid_section_nr(section_nr)))
562 continue;
563
564 ms = __nr_to_section(section_nr);
565 ms->section_mem_map |= SECTION_IS_ONLINE;
566 }
567}
568
569#ifdef CONFIG_MEMORY_HOTREMOVE
570
571void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
572{
573 unsigned long pfn;
574
575 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
576 unsigned long section_nr = pfn_to_section_nr(pfn);
577 struct mem_section *ms;
578
579
580
581
582
583 if (WARN_ON(!valid_section_nr(section_nr)))
584 continue;
585
586 ms = __nr_to_section(section_nr);
587 ms->section_mem_map &= ~SECTION_IS_ONLINE;
588 }
589}
590#endif
591
592#ifdef CONFIG_SPARSEMEM_VMEMMAP
593static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
594 struct vmem_altmap *altmap)
595{
596
597 return sparse_mem_map_populate(pnum, nid, altmap);
598}
599static void __kfree_section_memmap(struct page *memmap,
600 struct vmem_altmap *altmap)
601{
602 unsigned long start = (unsigned long)memmap;
603 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
604
605 vmemmap_free(start, end, altmap);
606}
607#ifdef CONFIG_MEMORY_HOTREMOVE
608static void free_map_bootmem(struct page *memmap)
609{
610 unsigned long start = (unsigned long)memmap;
611 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
612
613 vmemmap_free(start, end, NULL);
614}
615#endif
616#else
617static struct page *__kmalloc_section_memmap(void)
618{
619 struct page *page, *ret;
620 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
621
622 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
623 if (page)
624 goto got_map_page;
625
626 ret = vmalloc(memmap_size);
627 if (ret)
628 goto got_map_ptr;
629
630 return NULL;
631got_map_page:
632 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
633got_map_ptr:
634
635 return ret;
636}
637
638static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
639 struct vmem_altmap *altmap)
640{
641 return __kmalloc_section_memmap();
642}
643
644static void __kfree_section_memmap(struct page *memmap,
645 struct vmem_altmap *altmap)
646{
647 if (is_vmalloc_addr(memmap))
648 vfree(memmap);
649 else
650 free_pages((unsigned long)memmap,
651 get_order(sizeof(struct page) * PAGES_PER_SECTION));
652}
653
654#ifdef CONFIG_MEMORY_HOTREMOVE
655static void free_map_bootmem(struct page *memmap)
656{
657 unsigned long maps_section_nr, removing_section_nr, i;
658 unsigned long magic, nr_pages;
659 struct page *page = virt_to_page(memmap);
660
661 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
662 >> PAGE_SHIFT;
663
664 for (i = 0; i < nr_pages; i++, page++) {
665 magic = (unsigned long) page->freelist;
666
667 BUG_ON(magic == NODE_INFO);
668
669 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
670 removing_section_nr = page_private(page);
671
672
673
674
675
676
677
678
679
680 if (maps_section_nr != removing_section_nr)
681 put_page_bootmem(page);
682 }
683}
684#endif
685#endif
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700int __meminit sparse_add_one_section(int nid, unsigned long start_pfn,
701 struct vmem_altmap *altmap)
702{
703 unsigned long section_nr = pfn_to_section_nr(start_pfn);
704 struct mem_section *ms;
705 struct page *memmap;
706 unsigned long *usemap;
707 int ret;
708
709
710
711
712
713 ret = sparse_index_init(section_nr, nid);
714 if (ret < 0 && ret != -EEXIST)
715 return ret;
716 ret = 0;
717 memmap = kmalloc_section_memmap(section_nr, nid, altmap);
718 if (!memmap)
719 return -ENOMEM;
720 usemap = __kmalloc_section_usemap();
721 if (!usemap) {
722 __kfree_section_memmap(memmap, altmap);
723 return -ENOMEM;
724 }
725
726 ms = __pfn_to_section(start_pfn);
727 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
728 ret = -EEXIST;
729 goto out;
730 }
731
732
733
734
735
736 page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
737
738 section_mark_present(ms);
739 sparse_init_one_section(ms, section_nr, memmap, usemap);
740
741out:
742 if (ret < 0) {
743 kfree(usemap);
744 __kfree_section_memmap(memmap, altmap);
745 }
746 return ret;
747}
748
749#ifdef CONFIG_MEMORY_HOTREMOVE
750#ifdef CONFIG_MEMORY_FAILURE
751static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
752{
753 int i;
754
755 if (!memmap)
756 return;
757
758
759
760
761
762
763
764 if (atomic_long_read(&num_poisoned_pages) == 0)
765 return;
766
767 for (i = 0; i < nr_pages; i++) {
768 if (PageHWPoison(&memmap[i])) {
769 atomic_long_sub(1, &num_poisoned_pages);
770 ClearPageHWPoison(&memmap[i]);
771 }
772 }
773}
774#else
775static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
776{
777}
778#endif
779
780static void free_section_usemap(struct page *memmap, unsigned long *usemap,
781 struct vmem_altmap *altmap)
782{
783 struct page *usemap_page;
784
785 if (!usemap)
786 return;
787
788 usemap_page = virt_to_page(usemap);
789
790
791
792 if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
793 kfree(usemap);
794 if (memmap)
795 __kfree_section_memmap(memmap, altmap);
796 return;
797 }
798
799
800
801
802
803
804 if (memmap)
805 free_map_bootmem(memmap);
806}
807
808void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
809 unsigned long map_offset, struct vmem_altmap *altmap)
810{
811 struct page *memmap = NULL;
812 unsigned long *usemap = NULL;
813
814 if (ms->section_mem_map) {
815 usemap = ms->pageblock_flags;
816 memmap = sparse_decode_mem_map(ms->section_mem_map,
817 __section_nr(ms));
818 ms->section_mem_map = 0;
819 ms->pageblock_flags = NULL;
820 }
821
822 clear_hwpoisoned_pages(memmap + map_offset,
823 PAGES_PER_SECTION - map_offset);
824 free_section_usemap(memmap, usemap, altmap);
825}
826#endif
827#endif
828