0074-mm-sparsemem-Allocate-mem_section-at-runtime-for-CON.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. From cc87e9d44044fb3ae4145d6ad9574697439b03bf Mon Sep 17 00:00:00 2001
  2. From: "Kirill A. Shutemov" <[email protected]>
  3. Date: Fri, 29 Sep 2017 17:08:16 +0300
  4. Subject: [PATCH 074/242] mm/sparsemem: Allocate mem_section at runtime for
  5. CONFIG_SPARSEMEM_EXTREME=y
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. Size of the mem_section[] array depends on the size of the physical address space.
  11. In preparation for boot-time switching between paging modes on x86-64
  12. we need to make the allocation of mem_section[] dynamic, because otherwise
  13. we waste a lot of RAM: with CONFIG_NODE_SHIFT=10, mem_section[] size is 32kB
  14. for 4-level paging and 2MB for 5-level paging mode.
  15. The patch allocates the array on the first call to sparse_memory_present_with_active_regions().
  16. Signed-off-by: Kirill A. Shutemov <[email protected]>
  17. Cc: Andrew Morton <[email protected]>
  18. Cc: Andy Lutomirski <[email protected]>
  19. Cc: Borislav Petkov <[email protected]>
  20. Cc: Cyrill Gorcunov <[email protected]>
  21. Cc: Linus Torvalds <[email protected]>
  22. Cc: Peter Zijlstra <[email protected]>
  23. Cc: Thomas Gleixner <[email protected]>
  24. Cc: [email protected]
  25. Link: http://lkml.kernel.org/r/[email protected]
  26. Signed-off-by: Ingo Molnar <[email protected]>
  27. (cherry picked from commit 83e3c48729d9ebb7af5a31a504f3fd6aff0348c4)
  28. Signed-off-by: Andy Whitcroft <[email protected]>
  29. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  30. (cherry picked from commit c70f71e01a0ae5d884abae0424618abe90b82011)
  31. Signed-off-by: Fabian Grünbichler <[email protected]>
  32. ---
  33. include/linux/mmzone.h | 6 +++++-
  34. mm/page_alloc.c | 10 ++++++++++
  35. mm/sparse.c | 17 +++++++++++------
  36. 3 files changed, 26 insertions(+), 7 deletions(-)
  37. diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
  38. index fc14b8b3f6ce..9c6c001a8c6c 100644
  39. --- a/include/linux/mmzone.h
  40. +++ b/include/linux/mmzone.h
  41. @@ -1137,13 +1137,17 @@ struct mem_section {
  42. #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
  43. #ifdef CONFIG_SPARSEMEM_EXTREME
  44. -extern struct mem_section *mem_section[NR_SECTION_ROOTS];
  45. +extern struct mem_section **mem_section;
  46. #else
  47. extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
  48. #endif
  49. static inline struct mem_section *__nr_to_section(unsigned long nr)
  50. {
  51. +#ifdef CONFIG_SPARSEMEM_EXTREME
  52. + if (!mem_section)
  53. + return NULL;
  54. +#endif
  55. if (!mem_section[SECTION_NR_TO_ROOT(nr)])
  56. return NULL;
  57. return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
  58. diff --git a/mm/page_alloc.c b/mm/page_alloc.c
  59. index 1423da8dd16f..66eb23ab658d 100644
  60. --- a/mm/page_alloc.c
  61. +++ b/mm/page_alloc.c
  62. @@ -5707,6 +5707,16 @@ void __init sparse_memory_present_with_active_regions(int nid)
  63. unsigned long start_pfn, end_pfn;
  64. int i, this_nid;
  65. +#ifdef CONFIG_SPARSEMEM_EXTREME
  66. + if (!mem_section) {
  67. + unsigned long size, align;
  68. +
  69. + size = sizeof(struct mem_section) * NR_SECTION_ROOTS;
  70. + align = 1 << (INTERNODE_CACHE_SHIFT);
  71. + mem_section = memblock_virt_alloc(size, align);
  72. + }
  73. +#endif
  74. +
  75. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
  76. memory_present(this_nid, start_pfn, end_pfn);
  77. }
  78. diff --git a/mm/sparse.c b/mm/sparse.c
  79. index cdce7a7bb3f3..308a0789d1bb 100644
  80. --- a/mm/sparse.c
  81. +++ b/mm/sparse.c
  82. @@ -22,8 +22,7 @@
  83. * 1) mem_section - memory sections, mem_map's for valid memory
  84. */
  85. #ifdef CONFIG_SPARSEMEM_EXTREME
  86. -struct mem_section *mem_section[NR_SECTION_ROOTS]
  87. - ____cacheline_internodealigned_in_smp;
  88. +struct mem_section **mem_section;
  89. #else
  90. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  91. ____cacheline_internodealigned_in_smp;
  92. @@ -104,7 +103,7 @@ static inline int sparse_index_init(unsigned long section_nr, int nid)
  93. int __section_nr(struct mem_section* ms)
  94. {
  95. unsigned long root_nr;
  96. - struct mem_section* root;
  97. + struct mem_section *root = NULL;
  98. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  99. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  100. @@ -115,7 +114,7 @@ int __section_nr(struct mem_section* ms)
  101. break;
  102. }
  103. - VM_BUG_ON(root_nr == NR_SECTION_ROOTS);
  104. + VM_BUG_ON(!root);
  105. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  106. }
  107. @@ -333,11 +332,17 @@ sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  108. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  109. {
  110. unsigned long usemap_snr, pgdat_snr;
  111. - static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
  112. - static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
  113. + static unsigned long old_usemap_snr;
  114. + static unsigned long old_pgdat_snr;
  115. struct pglist_data *pgdat = NODE_DATA(nid);
  116. int usemap_nid;
  117. + /* First call */
  118. + if (!old_usemap_snr) {
  119. + old_usemap_snr = NR_MEM_SECTIONS;
  120. + old_pgdat_snr = NR_MEM_SECTIONS;
  121. + }
  122. +
  123. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  124. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  125. if (usemap_snr == pgdat_snr)
  126. --
  127. 2.14.2