Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 2 | /* |
| 3 | * sparse memory mappings. |
| 4 | */ |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 5 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 7 | #include <linux/mmzone.h> |
Mike Rapoport | 97ad108 | 2018-10-30 15:09:44 -0700 | [diff] [blame] | 8 | #include <linux/memblock.h> |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 10 | #include <linux/highmem.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 14 | |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 15 | #include "internal.h" |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 16 | #include <asm/dma.h> |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 17 | #include <asm/pgalloc.h> |
| 18 | #include <asm/pgtable.h> |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Permanent SPARSEMEM data: |
| 22 | * |
| 23 | * 1) mem_section - memory sections, mem_map's for valid memory |
| 24 | */ |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_SPARSEMEM_EXTREME |
Kirill A. Shutemov | 83e3c487 | 2017-09-29 17:08:16 +0300 | [diff] [blame] | 26 | struct mem_section **mem_section; |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 27 | #else |
| 28 | struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT] |
Ravikiran G Thirumalai | 22fc6ec | 2006-01-08 01:01:27 -0800 | [diff] [blame] | 29 | ____cacheline_internodealigned_in_smp; |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 30 | #endif |
| 31 | EXPORT_SYMBOL(mem_section); |
| 32 | |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 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 | |
Ian Campbell | 33dd4e0 | 2011-07-25 17:11:51 -0700 | [diff] [blame] | 45 | int page_to_nid(const struct page *page) |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 46 | { |
| 47 | return section_to_node_table[page_to_section(page)]; |
| 48 | } |
| 49 | EXPORT_SYMBOL(page_to_nid); |
Andy Whitcroft | 85770ff | 2007-08-22 14:01:03 -0700 | [diff] [blame] | 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 | } |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 61 | #ifdef CONFIG_SPARSEMEM_EXTREME |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 62 | static noinline struct mem_section __ref *sparse_index_alloc(int nid) |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 63 | { |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 64 | struct mem_section *section = NULL; |
| 65 | unsigned long array_size = SECTIONS_PER_ROOT * |
| 66 | sizeof(struct mem_section); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 67 | |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 68 | if (slab_is_available()) { |
Michal Hocko | b95046b | 2017-09-06 16:20:41 -0700 | [diff] [blame] | 69 | section = kzalloc_node(array_size, GFP_KERNEL, nid); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 70 | } else { |
Mike Rapoport | 7e1c4e2 | 2018-10-30 15:09:57 -0700 | [diff] [blame] | 71 | section = memblock_alloc_node(array_size, SMP_CACHE_BYTES, |
| 72 | nid); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 73 | if (!section) |
| 74 | panic("%s: Failed to allocate %lu bytes nid=%d\n", |
| 75 | __func__, array_size, nid); |
| 76 | } |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 77 | |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 78 | return section; |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 79 | } |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 80 | |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 81 | static int __meminit sparse_index_init(unsigned long section_nr, int nid) |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 82 | { |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 83 | unsigned long root = SECTION_NR_TO_ROOT(section_nr); |
| 84 | struct mem_section *section; |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 85 | |
| 86 | if (mem_section[root]) |
| 87 | return -EEXIST; |
| 88 | |
| 89 | section = sparse_index_alloc(nid); |
WANG Cong | af0cd5a | 2007-12-17 16:19:58 -0800 | [diff] [blame] | 90 | if (!section) |
| 91 | return -ENOMEM; |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 92 | |
| 93 | mem_section[root] = section; |
Gavin Shan | c1c9518 | 2012-07-31 16:46:06 -0700 | [diff] [blame] | 94 | |
Zhang Yanfei | 9d1936c | 2013-05-17 22:10:38 +0800 | [diff] [blame] | 95 | return 0; |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 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 | |
Zhou Chengming | 91fd8b9 | 2016-07-28 15:48:35 -0700 | [diff] [blame] | 104 | #ifdef CONFIG_SPARSEMEM_EXTREME |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 105 | int __section_nr(struct mem_section* ms) |
| 106 | { |
| 107 | unsigned long root_nr; |
Kirill A. Shutemov | 83e3c487 | 2017-09-29 17:08:16 +0300 | [diff] [blame] | 108 | struct mem_section *root = NULL; |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 109 | |
Mike Kravetz | 12783b0 | 2006-05-20 15:00:05 -0700 | [diff] [blame] | 110 | for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) { |
| 111 | root = __nr_to_section(root_nr * SECTIONS_PER_ROOT); |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 112 | if (!root) |
| 113 | continue; |
| 114 | |
| 115 | if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT))) |
| 116 | break; |
| 117 | } |
| 118 | |
Kirill A. Shutemov | 83e3c487 | 2017-09-29 17:08:16 +0300 | [diff] [blame] | 119 | VM_BUG_ON(!root); |
Gavin Shan | db36a46 | 2012-07-31 16:46:04 -0700 | [diff] [blame] | 120 | |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 121 | return (root_nr * SECTIONS_PER_ROOT) + (ms - root); |
| 122 | } |
Zhou Chengming | 91fd8b9 | 2016-07-28 15:48:35 -0700 | [diff] [blame] | 123 | #else |
| 124 | int __section_nr(struct mem_section* ms) |
| 125 | { |
| 126 | return (int)(ms - mem_section[0]); |
| 127 | } |
| 128 | #endif |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 129 | |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 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 | |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 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) |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 149 | { |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 150 | unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT); |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 151 | |
Ingo Molnar | bead9a3 | 2008-04-16 01:40:00 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Sanity checks - do not allow an architecture to pass |
| 154 | * in larger pfns than the maximum scope of sparsemem: |
| 155 | */ |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 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; |
Cyrill Gorcunov | ef161a9 | 2009-03-31 15:19:25 -0700 | [diff] [blame] | 163 | } else if (*end_pfn > max_sparsemem_pfn) { |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 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 | |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 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; |
Wei Yang | d538c16 | 2018-06-07 17:06:39 -0700 | [diff] [blame] | 198 | } while ((section_nr <= __highest_present_section_nr)); |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 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); \ |
Qian Cai | d778015 | 2019-03-05 15:50:11 -0800 | [diff] [blame] | 204 | ((section_nr != -1) && \ |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 205 | (section_nr <= __highest_present_section_nr)); \ |
| 206 | section_nr = next_present_section_nr(section_nr)) |
| 207 | |
Pavel Tatashin | 85c77f7 | 2018-08-17 15:49:33 -0700 | [diff] [blame] | 208 | static inline unsigned long first_present_section_nr(void) |
| 209 | { |
| 210 | return next_present_section_nr(-1); |
| 211 | } |
| 212 | |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 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; |
Ingo Molnar | bead9a3 | 2008-04-16 01:40:00 +0200 | [diff] [blame] | 217 | |
Kirill A. Shutemov | 629a359 | 2017-11-07 11:33:37 +0300 | [diff] [blame] | 218 | #ifdef CONFIG_SPARSEMEM_EXTREME |
| 219 | if (unlikely(!mem_section)) { |
| 220 | unsigned long size, align; |
| 221 | |
Baoquan He | d09cfbb | 2018-01-04 16:18:06 -0800 | [diff] [blame] | 222 | size = sizeof(struct mem_section*) * NR_SECTION_ROOTS; |
Kirill A. Shutemov | 629a359 | 2017-11-07 11:33:37 +0300 | [diff] [blame] | 223 | align = 1 << (INTERNODE_CACHE_SHIFT); |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 224 | mem_section = memblock_alloc(size, align); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 225 | if (!mem_section) |
| 226 | panic("%s: Failed to allocate %lu bytes align=0x%lx\n", |
| 227 | __func__, size, align); |
Kirill A. Shutemov | 629a359 | 2017-11-07 11:33:37 +0300 | [diff] [blame] | 228 | } |
| 229 | #endif |
| 230 | |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 231 | start &= PAGE_SECTION_MASK; |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 232 | mminit_validate_memmodel_limits(&start, &end); |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 233 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) { |
| 234 | unsigned long section = pfn_to_section_nr(pfn); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 235 | struct mem_section *ms; |
| 236 | |
| 237 | sparse_index_init(section, nid); |
Andy Whitcroft | 85770ff | 2007-08-22 14:01:03 -0700 | [diff] [blame] | 238 | set_section_nid(section, nid); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 239 | |
| 240 | ms = __nr_to_section(section); |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 241 | if (!ms->section_mem_map) { |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 242 | ms->section_mem_map = sparse_encode_early_nid(nid) | |
| 243 | SECTION_IS_ONLINE; |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 244 | section_mark_present(ms); |
| 245 | } |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
Logan Gunthorpe | 9def36e | 2018-12-14 14:16:57 -0800 | [diff] [blame] | 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 | /* |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 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 | { |
Petr Tesarik | def9b71 | 2018-01-31 16:20:26 -0800 | [diff] [blame] | 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; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 280 | * Decode mem_map from the coded memmap |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 281 | */ |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 282 | struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum) |
| 283 | { |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 284 | /* mask off the extra low bits of information */ |
| 285 | coded_mem_map &= SECTION_MAP_MASK; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 286 | return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum); |
| 287 | } |
| 288 | |
Oscar Salvador | 4e40987 | 2018-08-17 15:47:14 -0700 | [diff] [blame] | 289 | static void __meminit sparse_init_one_section(struct mem_section *ms, |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 290 | unsigned long pnum, struct page *mem_map, |
| 291 | unsigned long *pageblock_bitmap) |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 292 | { |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 293 | ms->section_mem_map &= ~SECTION_MAP_MASK; |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 294 | ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) | |
| 295 | SECTION_HAS_MEM_MAP; |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 296 | ms->pageblock_flags = pageblock_bitmap; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 299 | unsigned long usemap_size(void) |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 300 | { |
Wei Yang | 60a7a88 | 2017-05-03 14:53:51 -0700 | [diff] [blame] | 301 | return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long); |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 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 | |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 311 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 312 | static unsigned long * __init |
Yinghai Lu | a4322e1b | 2010-02-10 01:20:21 -0800 | [diff] [blame] | 313 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
Johannes Weiner | 238305b | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 314 | unsigned long size) |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 315 | { |
Yinghai Lu | 99ab7b1 | 2012-07-11 14:02:53 -0700 | [diff] [blame] | 316 | unsigned long goal, limit; |
| 317 | unsigned long *p; |
| 318 | int nid; |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 319 | /* |
| 320 | * A page may contain usemaps for other sections preventing the |
| 321 | * page being freed and making a section unremovable while |
Li Zhong | c800bcd | 2014-03-31 16:41:58 +0800 | [diff] [blame] | 322 | * other sections referencing the usemap remain active. Similarly, |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 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 | */ |
Yinghai Lu | 07b4e2b | 2012-07-11 14:02:51 -0700 | [diff] [blame] | 329 | goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT); |
Yinghai Lu | 99ab7b1 | 2012-07-11 14:02:53 -0700 | [diff] [blame] | 330 | limit = goal + (1UL << PA_SECTION_SHIFT); |
| 331 | nid = early_pfn_to_nid(goal >> PAGE_SHIFT); |
| 332 | again: |
Mike Rapoport | 26fb3da | 2019-03-11 23:30:42 -0700 | [diff] [blame] | 333 | p = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid); |
Yinghai Lu | 99ab7b1 | 2012-07-11 14:02:53 -0700 | [diff] [blame] | 334 | if (!p && limit) { |
| 335 | limit = 0; |
| 336 | goto again; |
| 337 | } |
| 338 | return p; |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) |
| 342 | { |
| 343 | unsigned long usemap_snr, pgdat_snr; |
Kirill A. Shutemov | 83e3c487 | 2017-09-29 17:08:16 +0300 | [diff] [blame] | 344 | static unsigned long old_usemap_snr; |
| 345 | static unsigned long old_pgdat_snr; |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 346 | struct pglist_data *pgdat = NODE_DATA(nid); |
| 347 | int usemap_nid; |
| 348 | |
Kirill A. Shutemov | 83e3c487 | 2017-09-29 17:08:16 +0300 | [diff] [blame] | 349 | /* First call */ |
| 350 | if (!old_usemap_snr) { |
| 351 | old_usemap_snr = NR_MEM_SECTIONS; |
| 352 | old_pgdat_snr = NR_MEM_SECTIONS; |
| 353 | } |
| 354 | |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 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 | /* skip redundant message */ |
| 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) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 369 | pr_info("node %d must be removed before remove section %ld\n", |
| 370 | nid, usemap_snr); |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | /* |
| 374 | * There is a circular dependency. |
| 375 | * Some platforms allow un-removable section because they will just |
| 376 | * gather other removable sections for dynamic partitioning. |
| 377 | * Just notify un-removable section's number here. |
| 378 | */ |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 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); |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 381 | } |
| 382 | #else |
| 383 | static unsigned long * __init |
Yinghai Lu | a4322e1b | 2010-02-10 01:20:21 -0800 | [diff] [blame] | 384 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
Johannes Weiner | 238305b | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 385 | unsigned long size) |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 386 | { |
Mike Rapoport | 26fb3da | 2019-03-11 23:30:42 -0700 | [diff] [blame] | 387 | return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id); |
Yasunori Goto | 48c9068 | 2008-07-23 21:28:15 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) |
| 391 | { |
| 392 | } |
| 393 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
| 394 | |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 395 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Pavel Tatashin | afda57b | 2018-08-17 15:49:30 -0700 | [diff] [blame] | 396 | static unsigned long __init section_map_size(void) |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 397 | { |
| 398 | return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE); |
| 399 | } |
| 400 | |
| 401 | #else |
Pavel Tatashin | afda57b | 2018-08-17 15:49:30 -0700 | [diff] [blame] | 402 | static unsigned long __init section_map_size(void) |
Pavel Tatashin | e131c06 | 2018-08-17 15:49:26 -0700 | [diff] [blame] | 403 | { |
| 404 | return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION); |
| 405 | } |
| 406 | |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 407 | struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid, |
| 408 | struct vmem_altmap *altmap) |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 409 | { |
Pavel Tatashin | e131c06 | 2018-08-17 15:49:26 -0700 | [diff] [blame] | 410 | unsigned long size = section_map_size(); |
| 411 | struct page *map = sparse_buffer_alloc(size); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 412 | phys_addr_t addr = __pa(MAX_DMA_ADDRESS); |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 413 | |
Pavel Tatashin | e131c06 | 2018-08-17 15:49:26 -0700 | [diff] [blame] | 414 | if (map) |
| 415 | return map; |
| 416 | |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 417 | map = memblock_alloc_try_nid(size, |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 418 | PAGE_SIZE, addr, |
Mike Rapoport | 97ad108 | 2018-10-30 15:09:44 -0700 | [diff] [blame] | 419 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 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 | |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 424 | return map; |
| 425 | } |
| 426 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
| 427 | |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 428 | static void *sparsemap_buf __meminitdata; |
| 429 | static void *sparsemap_buf_end __meminitdata; |
| 430 | |
Pavel Tatashin | afda57b | 2018-08-17 15:49:30 -0700 | [diff] [blame] | 431 | static void __init sparse_buffer_init(unsigned long size, int nid) |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 432 | { |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 433 | phys_addr_t addr = __pa(MAX_DMA_ADDRESS); |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 434 | WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */ |
| 435 | sparsemap_buf = |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 436 | memblock_alloc_try_nid_raw(size, PAGE_SIZE, |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 437 | addr, |
Mike Rapoport | 97ad108 | 2018-10-30 15:09:44 -0700 | [diff] [blame] | 438 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 439 | sparsemap_buf_end = sparsemap_buf + size; |
| 440 | } |
| 441 | |
Pavel Tatashin | afda57b | 2018-08-17 15:49:30 -0700 | [diff] [blame] | 442 | static void __init sparse_buffer_fini(void) |
Pavel Tatashin | 35fd1eb | 2018-08-17 15:49:21 -0700 | [diff] [blame] | 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 | |
| 451 | void * __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 | |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 465 | void __weak __meminit vmemmap_populate_print_last(void) |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 466 | { |
| 467 | } |
Yinghai Lu | a4322e1b | 2010-02-10 01:20:21 -0800 | [diff] [blame] | 468 | |
Pavel Tatashin | 85c77f7 | 2018-08-17 15:49:33 -0700 | [diff] [blame] | 469 | /* |
| 470 | * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end) |
| 471 | * And number of present sections in this node is map_count. |
| 472 | */ |
| 473 | static 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; |
| 506 | failed: |
| 507 | /* We failed to allocate, mark all the following pnums as not present */ |
| 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 | * Allocate the accumulated non-linear sections, allocate a mem_map |
| 520 | * for each and record the physical to section mapping. |
| 521 | */ |
Pavel Tatashin | 2a3cb8b | 2018-08-17 15:49:37 -0700 | [diff] [blame] | 522 | void __init sparse_init(void) |
Pavel Tatashin | 85c77f7 | 2018-08-17 15:49:33 -0700 | [diff] [blame] | 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 | /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */ |
| 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 | /* Init node with sections in range [pnum_begin, pnum_end) */ |
| 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 | /* cover the last node */ |
| 545 | sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count); |
| 546 | vmemmap_populate_print_last(); |
| 547 | } |
| 548 | |
Stephen Rothwell | 193faea | 2007-06-08 13:46:51 -0700 | [diff] [blame] | 549 | #ifdef CONFIG_MEMORY_HOTPLUG |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 550 | |
| 551 | /* Mark all memory sections within the pfn range as online */ |
| 552 | void 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) { |
Michal Hocko | b4ccec4 | 2017-09-08 16:13:15 -0700 | [diff] [blame] | 557 | unsigned long section_nr = pfn_to_section_nr(pfn); |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 558 | struct mem_section *ms; |
| 559 | |
| 560 | /* onlining code should never touch invalid ranges */ |
| 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 |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 570 | /* Mark all memory sections within the pfn range as offline */ |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 571 | void 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) { |
Pavel Tatashin | 27227c7 | 2018-05-11 16:01:50 -0700 | [diff] [blame] | 576 | unsigned long section_nr = pfn_to_section_nr(pfn); |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 577 | struct mem_section *ms; |
| 578 | |
| 579 | /* |
| 580 | * TODO this needs some double checking. Offlining code makes |
| 581 | * sure to check pfn_valid but those checks might be just bogus |
| 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 | |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 592 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 593 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid, |
| 594 | struct vmem_altmap *altmap) |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 595 | { |
| 596 | /* This will make the necessary allocations eventually. */ |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 597 | return sparse_mem_map_populate(pnum, nid, altmap); |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 598 | } |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 599 | static void __kfree_section_memmap(struct page *memmap, |
| 600 | struct vmem_altmap *altmap) |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 601 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 602 | unsigned long start = (unsigned long)memmap; |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 603 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 604 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 605 | vmemmap_free(start, end, altmap); |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 606 | } |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 607 | static void free_map_bootmem(struct page *memmap) |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 608 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 609 | unsigned long start = (unsigned long)memmap; |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 610 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 611 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 612 | vmemmap_free(start, end, NULL); |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 613 | } |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 614 | #else |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 615 | static struct page *__kmalloc_section_memmap(void) |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 616 | { |
| 617 | struct page *page, *ret; |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 618 | unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION; |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 619 | |
Yasunori Goto | f2d0aa5 | 2006-10-28 10:38:32 -0700 | [diff] [blame] | 620 | page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size)); |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 621 | if (page) |
| 622 | goto got_map_page; |
| 623 | |
| 624 | ret = vmalloc(memmap_size); |
| 625 | if (ret) |
| 626 | goto got_map_ptr; |
| 627 | |
| 628 | return NULL; |
| 629 | got_map_page: |
| 630 | ret = (struct page *)pfn_to_kaddr(page_to_pfn(page)); |
| 631 | got_map_ptr: |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 636 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid, |
| 637 | struct vmem_altmap *altmap) |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 638 | { |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 639 | return __kmalloc_section_memmap(); |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 642 | static void __kfree_section_memmap(struct page *memmap, |
| 643 | struct vmem_altmap *altmap) |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 644 | { |
Christoph Lameter | 9e2779f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 645 | if (is_vmalloc_addr(memmap)) |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 646 | vfree(memmap); |
| 647 | else |
| 648 | free_pages((unsigned long)memmap, |
Zhang Yanfei | 85b35fe | 2013-11-12 15:07:42 -0800 | [diff] [blame] | 649 | get_order(sizeof(struct page) * PAGES_PER_SECTION)); |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 650 | } |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 651 | |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 652 | static void free_map_bootmem(struct page *memmap) |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 653 | { |
| 654 | unsigned long maps_section_nr, removing_section_nr, i; |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 655 | unsigned long magic, nr_pages; |
Jianguo Wu | ae64ffc | 2012-11-29 13:54:21 -0800 | [diff] [blame] | 656 | struct page *page = virt_to_page(memmap); |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 657 | |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 658 | nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page)) |
| 659 | >> PAGE_SHIFT; |
| 660 | |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 661 | for (i = 0; i < nr_pages; i++, page++) { |
Yasuaki Ishimatsu | ddffe98 | 2017-02-22 15:45:13 -0800 | [diff] [blame] | 662 | magic = (unsigned long) page->freelist; |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 663 | |
| 664 | BUG_ON(magic == NODE_INFO); |
| 665 | |
| 666 | maps_section_nr = pfn_to_section_nr(page_to_pfn(page)); |
Yasuaki Ishimatsu | 857e522 | 2017-02-22 15:45:10 -0800 | [diff] [blame] | 667 | removing_section_nr = page_private(page); |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * When this function is called, the removing section is |
| 671 | * logical offlined state. This means all pages are isolated |
| 672 | * from page allocator. If removing section's memmap is placed |
| 673 | * on the same section, it must not be freed. |
| 674 | * If it is freed, page allocator may allocate it which will |
| 675 | * be removed physically soon. |
| 676 | */ |
| 677 | if (maps_section_nr != removing_section_nr) |
| 678 | put_page_bootmem(page); |
| 679 | } |
| 680 | } |
Yasunori Goto | 98f3cfc | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 681 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 682 | |
Baoquan He | 7567cfc | 2019-05-13 17:19:32 -0700 | [diff] [blame] | 683 | /** |
| 684 | * sparse_add_one_section - add a memory section |
| 685 | * @nid: The node to add section on |
| 686 | * @start_pfn: start pfn of the memory range |
| 687 | * @altmap: device page map |
| 688 | * |
| 689 | * This is only intended for hotplug. |
| 690 | * |
| 691 | * Return: |
| 692 | * * 0 - On success. |
| 693 | * * -EEXIST - Section has been present. |
| 694 | * * -ENOMEM - Out of memory. |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 695 | */ |
Wei Yang | 4e0d2e7 | 2018-12-28 00:37:06 -0800 | [diff] [blame] | 696 | int __meminit sparse_add_one_section(int nid, unsigned long start_pfn, |
| 697 | struct vmem_altmap *altmap) |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 698 | { |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 699 | unsigned long section_nr = pfn_to_section_nr(start_pfn); |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 700 | struct mem_section *ms; |
| 701 | struct page *memmap; |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 702 | unsigned long *usemap; |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 703 | int ret; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 704 | |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 705 | /* |
| 706 | * no locking for this, because it does its own |
| 707 | * plus, it does a kmalloc |
| 708 | */ |
Wei Yang | 4e0d2e7 | 2018-12-28 00:37:06 -0800 | [diff] [blame] | 709 | ret = sparse_index_init(section_nr, nid); |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 710 | if (ret < 0 && ret != -EEXIST) |
| 711 | return ret; |
Oscar Salvador | 4e40987 | 2018-08-17 15:47:14 -0700 | [diff] [blame] | 712 | ret = 0; |
Wei Yang | 4e0d2e7 | 2018-12-28 00:37:06 -0800 | [diff] [blame] | 713 | memmap = kmalloc_section_memmap(section_nr, nid, altmap); |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 714 | if (!memmap) |
| 715 | return -ENOMEM; |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 716 | usemap = __kmalloc_section_usemap(); |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 717 | if (!usemap) { |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 718 | __kfree_section_memmap(memmap, altmap); |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 719 | return -ENOMEM; |
| 720 | } |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 721 | |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 722 | ms = __pfn_to_section(start_pfn); |
| 723 | if (ms->section_mem_map & SECTION_MARKED_PRESENT) { |
| 724 | ret = -EEXIST; |
| 725 | goto out; |
| 726 | } |
Mel Gorman | 5c0e306 | 2007-10-16 01:25:56 -0700 | [diff] [blame] | 727 | |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Poison uninitialized struct pages in order to catch invalid flags |
| 730 | * combinations. |
| 731 | */ |
Alexander Duyck | f682a97 | 2018-10-26 15:07:45 -0700 | [diff] [blame] | 732 | page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION); |
Wen Congyang | 3ac19f8 | 2012-12-11 16:00:59 -0800 | [diff] [blame] | 733 | |
Wei Yang | 26f26be | 2019-07-18 15:57:21 -0700 | [diff] [blame^] | 734 | set_section_nid(section_nr, nid); |
Dave Hansen | c4e1be9 | 2017-07-06 15:36:44 -0700 | [diff] [blame] | 735 | section_mark_present(ms); |
Oscar Salvador | 4e40987 | 2018-08-17 15:47:14 -0700 | [diff] [blame] | 736 | sparse_init_one_section(ms, section_nr, memmap, usemap); |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 737 | |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 738 | out: |
Oscar Salvador | 4e40987 | 2018-08-17 15:47:14 -0700 | [diff] [blame] | 739 | if (ret < 0) { |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 740 | kfree(usemap); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 741 | __kfree_section_memmap(memmap, altmap); |
WANG Cong | bbd0682 | 2007-12-17 16:19:59 -0800 | [diff] [blame] | 742 | } |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 743 | return ret; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 744 | } |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 745 | |
Wen Congyang | 95a4774 | 2012-12-11 16:00:47 -0800 | [diff] [blame] | 746 | #ifdef CONFIG_MEMORY_FAILURE |
| 747 | static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | if (!memmap) |
| 752 | return; |
| 753 | |
Balbir Singh | 5eb570a | 2018-12-28 00:33:24 -0800 | [diff] [blame] | 754 | /* |
| 755 | * A further optimization is to have per section refcounted |
| 756 | * num_poisoned_pages. But that would need more space per memmap, so |
| 757 | * for now just do a quick global check to speed up this routine in the |
| 758 | * absence of bad pages. |
| 759 | */ |
| 760 | if (atomic_long_read(&num_poisoned_pages) == 0) |
| 761 | return; |
| 762 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 763 | for (i = 0; i < nr_pages; i++) { |
Wen Congyang | 95a4774 | 2012-12-11 16:00:47 -0800 | [diff] [blame] | 764 | if (PageHWPoison(&memmap[i])) { |
Xishi Qiu | 293c07e | 2013-02-22 16:34:02 -0800 | [diff] [blame] | 765 | atomic_long_sub(1, &num_poisoned_pages); |
Wen Congyang | 95a4774 | 2012-12-11 16:00:47 -0800 | [diff] [blame] | 766 | ClearPageHWPoison(&memmap[i]); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | #else |
| 771 | static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) |
| 772 | { |
| 773 | } |
| 774 | #endif |
| 775 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 776 | static void free_section_usemap(struct page *memmap, unsigned long *usemap, |
| 777 | struct vmem_altmap *altmap) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 778 | { |
| 779 | struct page *usemap_page; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 780 | |
| 781 | if (!usemap) |
| 782 | return; |
| 783 | |
| 784 | usemap_page = virt_to_page(usemap); |
| 785 | /* |
| 786 | * Check to see if allocation came from hot-plug-add |
| 787 | */ |
| 788 | if (PageSlab(usemap_page) || PageCompound(usemap_page)) { |
| 789 | kfree(usemap); |
| 790 | if (memmap) |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 791 | __kfree_section_memmap(memmap, altmap); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 792 | return; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * The usemap came from bootmem. This is packed with other usemaps |
| 797 | * on the section which has pgdat at boot time. Just keep it as is now. |
| 798 | */ |
| 799 | |
Zhang Yanfei | 81556b0 | 2013-11-12 15:07:43 -0800 | [diff] [blame] | 800 | if (memmap) |
| 801 | free_map_bootmem(memmap); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 802 | } |
| 803 | |
David Hildenbrand | b9bf8d3 | 2019-07-18 15:57:17 -0700 | [diff] [blame] | 804 | void sparse_remove_one_section(struct mem_section *ms, unsigned long map_offset, |
| 805 | struct vmem_altmap *altmap) |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 806 | { |
| 807 | struct page *memmap = NULL; |
Wei Yang | 83af658 | 2018-12-28 00:37:02 -0800 | [diff] [blame] | 808 | unsigned long *usemap = NULL; |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 809 | |
| 810 | if (ms->section_mem_map) { |
| 811 | usemap = ms->pageblock_flags; |
| 812 | memmap = sparse_decode_mem_map(ms->section_mem_map, |
| 813 | __section_nr(ms)); |
| 814 | ms->section_mem_map = 0; |
| 815 | ms->pageblock_flags = NULL; |
| 816 | } |
| 817 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 818 | clear_hwpoisoned_pages(memmap + map_offset, |
| 819 | PAGES_PER_SECTION - map_offset); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 820 | free_section_usemap(memmap, usemap, altmap); |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 821 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 822 | #endif /* CONFIG_MEMORY_HOTPLUG */ |