blob: 33307fc05c4d3372d5e4746116330532765e61d7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002/*
3 * sparse memory mappings.
4 */
Andy Whitcroftd41dee32005-06-23 00:07:54 -07005#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -07007#include <linux/mmzone.h>
Mike Rapoport97ad1082018-10-30 15:09:44 -07008#include <linux/memblock.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07009#include <linux/compiler.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -070010#include <linux/highmem.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040011#include <linux/export.h>
Dave Hansen28ae55c2005-09-03 15:54:29 -070012#include <linux/spinlock.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -070013#include <linux/vmalloc.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070014
Yasunori Goto0c0a4a52008-04-28 02:13:34 -070015#include "internal.h"
Andy Whitcroftd41dee32005-06-23 00:07:54 -070016#include <asm/dma.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070017#include <asm/pgalloc.h>
18#include <asm/pgtable.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -070019
20/*
21 * Permanent SPARSEMEM data:
22 *
23 * 1) mem_section - memory sections, mem_map's for valid memory
24 */
Bob Picco3e347262005-09-03 15:54:28 -070025#ifdef CONFIG_SPARSEMEM_EXTREME
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +030026struct mem_section **mem_section;
Bob Picco3e347262005-09-03 15:54:28 -070027#else
28struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080029 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070030#endif
31EXPORT_SYMBOL(mem_section);
32
Christoph Lameter89689ae2006-12-06 20:31:45 -080033#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
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
Ian Campbell33dd4e02011-07-25 17:11:51 -070045int page_to_nid(const struct page *page)
Christoph Lameter89689ae2006-12-06 20:31:45 -080046{
47 return section_to_node_table[page_to_section(page)];
48}
49EXPORT_SYMBOL(page_to_nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -070050
51static 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 */
56static inline void set_section_nid(unsigned long section_nr, int nid)
57{
58}
Christoph Lameter89689ae2006-12-06 20:31:45 -080059#endif
60
Bob Picco3e347262005-09-03 15:54:28 -070061#ifdef CONFIG_SPARSEMEM_EXTREME
Fabian Frederickbd721ea2016-08-02 14:03:33 -070062static noinline struct mem_section __ref *sparse_index_alloc(int nid)
Bob Picco802f1922005-09-03 15:54:26 -070063{
Dave Hansen28ae55c2005-09-03 15:54:29 -070064 struct mem_section *section = NULL;
65 unsigned long array_size = SECTIONS_PER_ROOT *
66 sizeof(struct mem_section);
Bob Picco802f1922005-09-03 15:54:26 -070067
Michal Hockob95046b2017-09-06 16:20:41 -070068 if (slab_is_available())
69 section = kzalloc_node(array_size, GFP_KERNEL, nid);
70 else
Mike Rapoport7e1c4e22018-10-30 15:09:57 -070071 section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
72 nid);
Bob Picco3e347262005-09-03 15:54:28 -070073
Dave Hansen28ae55c2005-09-03 15:54:29 -070074 return section;
Bob Picco802f1922005-09-03 15:54:26 -070075}
Dave Hansen28ae55c2005-09-03 15:54:29 -070076
Yasunori Gotoa3142c82007-05-08 00:23:07 -070077static int __meminit sparse_index_init(unsigned long section_nr, int nid)
Dave Hansen28ae55c2005-09-03 15:54:29 -070078{
Dave Hansen28ae55c2005-09-03 15:54:29 -070079 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
80 struct mem_section *section;
Dave Hansen28ae55c2005-09-03 15:54:29 -070081
82 if (mem_section[root])
83 return -EEXIST;
84
85 section = sparse_index_alloc(nid);
WANG Congaf0cd5a2007-12-17 16:19:58 -080086 if (!section)
87 return -ENOMEM;
Dave Hansen28ae55c2005-09-03 15:54:29 -070088
89 mem_section[root] = section;
Gavin Shanc1c95182012-07-31 16:46:06 -070090
Zhang Yanfei9d1936c2013-05-17 22:10:38 +080091 return 0;
Dave Hansen28ae55c2005-09-03 15:54:29 -070092}
93#else /* !SPARSEMEM_EXTREME */
94static inline int sparse_index_init(unsigned long section_nr, int nid)
95{
96 return 0;
97}
98#endif
99
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700100#ifdef CONFIG_SPARSEMEM_EXTREME
Dave Hansen4ca644d2005-10-29 18:16:51 -0700101int __section_nr(struct mem_section* ms)
102{
103 unsigned long root_nr;
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300104 struct mem_section *root = NULL;
Dave Hansen4ca644d2005-10-29 18:16:51 -0700105
Mike Kravetz12783b02006-05-20 15:00:05 -0700106 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
107 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
Dave Hansen4ca644d2005-10-29 18:16:51 -0700108 if (!root)
109 continue;
110
111 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
112 break;
113 }
114
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300115 VM_BUG_ON(!root);
Gavin Shandb36a462012-07-31 16:46:04 -0700116
Dave Hansen4ca644d2005-10-29 18:16:51 -0700117 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
118}
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700119#else
120int __section_nr(struct mem_section* ms)
121{
122 return (int)(ms - mem_section[0]);
123}
124#endif
Dave Hansen4ca644d2005-10-29 18:16:51 -0700125
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700126/*
127 * During early boot, before section_mem_map is used for an actual
128 * mem_map, we use section_mem_map to store the section's NUMA
129 * node. This keeps us from having to use another data structure. The
130 * node information is cleared just before we store the real mem_map.
131 */
132static inline unsigned long sparse_encode_early_nid(int nid)
133{
134 return (nid << SECTION_NID_SHIFT);
135}
136
137static inline int sparse_early_nid(struct mem_section *section)
138{
139 return (section->section_mem_map >> SECTION_NID_SHIFT);
140}
141
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700142/* Validate the physical addressing limitations of the model */
143void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
144 unsigned long *end_pfn)
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700145{
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700146 unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700147
Ingo Molnarbead9a32008-04-16 01:40:00 +0200148 /*
149 * Sanity checks - do not allow an architecture to pass
150 * in larger pfns than the maximum scope of sparsemem:
151 */
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700152 if (*start_pfn > max_sparsemem_pfn) {
153 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
154 "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
155 *start_pfn, *end_pfn, max_sparsemem_pfn);
156 WARN_ON_ONCE(1);
157 *start_pfn = max_sparsemem_pfn;
158 *end_pfn = max_sparsemem_pfn;
Cyrill Gorcunovef161a92009-03-31 15:19:25 -0700159 } else if (*end_pfn > max_sparsemem_pfn) {
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700160 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
161 "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
162 *start_pfn, *end_pfn, max_sparsemem_pfn);
163 WARN_ON_ONCE(1);
164 *end_pfn = max_sparsemem_pfn;
165 }
166}
167
Dave Hansenc4e1be92017-07-06 15:36:44 -0700168/*
169 * There are a number of times that we loop over NR_MEM_SECTIONS,
170 * looking for section_present() on each. But, when we have very
171 * large physical address spaces, NR_MEM_SECTIONS can also be
172 * very large which makes the loops quite long.
173 *
174 * Keeping track of this gives us an easy way to break out of
175 * those loops early.
176 */
177int __highest_present_section_nr;
178static void section_mark_present(struct mem_section *ms)
179{
180 int section_nr = __section_nr(ms);
181
182 if (section_nr > __highest_present_section_nr)
183 __highest_present_section_nr = section_nr;
184
185 ms->section_mem_map |= SECTION_MARKED_PRESENT;
186}
187
188static inline int next_present_section_nr(int section_nr)
189{
190 do {
191 section_nr++;
192 if (present_section_nr(section_nr))
193 return section_nr;
Wei Yangd538c162018-06-07 17:06:39 -0700194 } while ((section_nr <= __highest_present_section_nr));
Dave Hansenc4e1be92017-07-06 15:36:44 -0700195
196 return -1;
197}
198#define for_each_present_section_nr(start, section_nr) \
199 for (section_nr = next_present_section_nr(start-1); \
200 ((section_nr >= 0) && \
Dave Hansenc4e1be92017-07-06 15:36:44 -0700201 (section_nr <= __highest_present_section_nr)); \
202 section_nr = next_present_section_nr(section_nr))
203
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700204static inline unsigned long first_present_section_nr(void)
205{
206 return next_present_section_nr(-1);
207}
208
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700209/* Record a memory area against a node. */
210void __init memory_present(int nid, unsigned long start, unsigned long end)
211{
212 unsigned long pfn;
Ingo Molnarbead9a32008-04-16 01:40:00 +0200213
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300214#ifdef CONFIG_SPARSEMEM_EXTREME
215 if (unlikely(!mem_section)) {
216 unsigned long size, align;
217
Baoquan Hed09cfbb2018-01-04 16:18:06 -0800218 size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300219 align = 1 << (INTERNODE_CACHE_SHIFT);
Mike Rapoporteb31d552018-10-30 15:08:04 -0700220 mem_section = memblock_alloc(size, align);
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300221 }
222#endif
223
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700224 start &= PAGE_SECTION_MASK;
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700225 mminit_validate_memmodel_limits(&start, &end);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700226 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
227 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700228 struct mem_section *ms;
229
230 sparse_index_init(section, nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -0700231 set_section_nid(section, nid);
Bob Picco802f1922005-09-03 15:54:26 -0700232
233 ms = __nr_to_section(section);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700234 if (!ms->section_mem_map) {
Michal Hocko2d070ea2017-07-06 15:37:56 -0700235 ms->section_mem_map = sparse_encode_early_nid(nid) |
236 SECTION_IS_ONLINE;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700237 section_mark_present(ms);
238 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700239 }
240}
241
242/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700243 * Subtle, we encode the real pfn into the mem_map such that
244 * the identity pfn - section_mem_map will return the actual
245 * physical page frame number.
246 */
247static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
248{
Petr Tesarikdef9b712018-01-31 16:20:26 -0800249 unsigned long coded_mem_map =
250 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
251 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
252 BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
253 return coded_mem_map;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700254}
255
256/*
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700257 * Decode mem_map from the coded memmap
Andy Whitcroft29751f62005-06-23 00:08:00 -0700258 */
Andy Whitcroft29751f62005-06-23 00:08:00 -0700259struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
260{
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700261 /* mask off the extra low bits of information */
262 coded_mem_map &= SECTION_MAP_MASK;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700263 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
264}
265
Oscar Salvador4e409872018-08-17 15:47:14 -0700266static void __meminit sparse_init_one_section(struct mem_section *ms,
Mel Gorman5c0e3062007-10-16 01:25:56 -0700267 unsigned long pnum, struct page *mem_map,
268 unsigned long *pageblock_bitmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700269{
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700270 ms->section_mem_map &= ~SECTION_MAP_MASK;
Andy Whitcroft540557b2007-10-16 01:24:11 -0700271 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
272 SECTION_HAS_MEM_MAP;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700273 ms->pageblock_flags = pageblock_bitmap;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700274}
275
Yasunori Goto04753272008-04-28 02:13:31 -0700276unsigned long usemap_size(void)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700277{
Wei Yang60a7a882017-05-03 14:53:51 -0700278 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
Mel Gorman5c0e3062007-10-16 01:25:56 -0700279}
280
281#ifdef CONFIG_MEMORY_HOTPLUG
282static unsigned long *__kmalloc_section_usemap(void)
283{
284 return kmalloc(usemap_size(), GFP_KERNEL);
285}
286#endif /* CONFIG_MEMORY_HOTPLUG */
287
Yasunori Goto48c90682008-07-23 21:28:15 -0700288#ifdef CONFIG_MEMORY_HOTREMOVE
289static unsigned long * __init
Yinghai Lua4322e12010-02-10 01:20:21 -0800290sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700291 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700292{
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700293 unsigned long goal, limit;
294 unsigned long *p;
295 int nid;
Yasunori Goto48c90682008-07-23 21:28:15 -0700296 /*
297 * A page may contain usemaps for other sections preventing the
298 * page being freed and making a section unremovable while
Li Zhongc800bcd2014-03-31 16:41:58 +0800299 * other sections referencing the usemap remain active. Similarly,
Yasunori Goto48c90682008-07-23 21:28:15 -0700300 * a pgdat can prevent a section being removed. If section A
301 * contains a pgdat and section B contains the usemap, both
302 * sections become inter-dependent. This allocates usemaps
303 * from the same section as the pgdat where possible to avoid
304 * this problem.
305 */
Yinghai Lu07b4e2b2012-07-11 14:02:51 -0700306 goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700307 limit = goal + (1UL << PA_SECTION_SHIFT);
308 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
309again:
Mike Rapoporteb31d552018-10-30 15:08:04 -0700310 p = memblock_alloc_try_nid_nopanic(size,
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800311 SMP_CACHE_BYTES, goal, limit,
312 nid);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700313 if (!p && limit) {
314 limit = 0;
315 goto again;
316 }
317 return p;
Yasunori Goto48c90682008-07-23 21:28:15 -0700318}
319
320static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
321{
322 unsigned long usemap_snr, pgdat_snr;
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300323 static unsigned long old_usemap_snr;
324 static unsigned long old_pgdat_snr;
Yasunori Goto48c90682008-07-23 21:28:15 -0700325 struct pglist_data *pgdat = NODE_DATA(nid);
326 int usemap_nid;
327
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300328 /* First call */
329 if (!old_usemap_snr) {
330 old_usemap_snr = NR_MEM_SECTIONS;
331 old_pgdat_snr = NR_MEM_SECTIONS;
332 }
333
Yasunori Goto48c90682008-07-23 21:28:15 -0700334 usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
335 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
336 if (usemap_snr == pgdat_snr)
337 return;
338
339 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
340 /* skip redundant message */
341 return;
342
343 old_usemap_snr = usemap_snr;
344 old_pgdat_snr = pgdat_snr;
345
346 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
347 if (usemap_nid != nid) {
Joe Perches11705322016-03-17 14:19:50 -0700348 pr_info("node %d must be removed before remove section %ld\n",
349 nid, usemap_snr);
Yasunori Goto48c90682008-07-23 21:28:15 -0700350 return;
351 }
352 /*
353 * There is a circular dependency.
354 * Some platforms allow un-removable section because they will just
355 * gather other removable sections for dynamic partitioning.
356 * Just notify un-removable section's number here.
357 */
Joe Perches11705322016-03-17 14:19:50 -0700358 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
359 usemap_snr, pgdat_snr, nid);
Yasunori Goto48c90682008-07-23 21:28:15 -0700360}
361#else
362static unsigned long * __init
Yinghai Lua4322e12010-02-10 01:20:21 -0800363sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700364 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700365{
Mike Rapoporteb31d552018-10-30 15:08:04 -0700366 return memblock_alloc_node_nopanic(size, pgdat->node_id);
Yasunori Goto48c90682008-07-23 21:28:15 -0700367}
368
369static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
370{
371}
372#endif /* CONFIG_MEMORY_HOTREMOVE */
373
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700374#ifdef CONFIG_SPARSEMEM_VMEMMAP
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700375static unsigned long __init section_map_size(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700376{
377 return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
378}
379
380#else
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700381static unsigned long __init section_map_size(void)
Pavel Tatashine131c062018-08-17 15:49:26 -0700382{
383 return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
384}
385
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100386struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
387 struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700388{
Pavel Tatashine131c062018-08-17 15:49:26 -0700389 unsigned long size = section_map_size();
390 struct page *map = sparse_buffer_alloc(size);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700391
Pavel Tatashine131c062018-08-17 15:49:26 -0700392 if (map)
393 return map;
394
Mike Rapoporteb31d552018-10-30 15:08:04 -0700395 map = memblock_alloc_try_nid(size,
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800396 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700397 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700398 return map;
399}
400#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
401
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700402static void *sparsemap_buf __meminitdata;
403static void *sparsemap_buf_end __meminitdata;
404
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700405static void __init sparse_buffer_init(unsigned long size, int nid)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700406{
407 WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
408 sparsemap_buf =
Mike Rapoporteb31d552018-10-30 15:08:04 -0700409 memblock_alloc_try_nid_raw(size, PAGE_SIZE,
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700410 __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700411 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700412 sparsemap_buf_end = sparsemap_buf + size;
413}
414
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700415static void __init sparse_buffer_fini(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700416{
417 unsigned long size = sparsemap_buf_end - sparsemap_buf;
418
419 if (sparsemap_buf && size > 0)
420 memblock_free_early(__pa(sparsemap_buf), size);
421 sparsemap_buf = NULL;
422}
423
424void * __meminit sparse_buffer_alloc(unsigned long size)
425{
426 void *ptr = NULL;
427
428 if (sparsemap_buf) {
429 ptr = PTR_ALIGN(sparsemap_buf, size);
430 if (ptr + size > sparsemap_buf_end)
431 ptr = NULL;
432 else
433 sparsemap_buf = ptr + size;
434 }
435 return ptr;
436}
437
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700438void __weak __meminit vmemmap_populate_print_last(void)
Yinghai Luc2b91e22008-04-12 01:19:24 -0700439{
440}
Yinghai Lua4322e12010-02-10 01:20:21 -0800441
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700442/*
443 * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
444 * And number of present sections in this node is map_count.
445 */
446static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
447 unsigned long pnum_end,
448 unsigned long map_count)
449{
450 unsigned long pnum, usemap_longs, *usemap;
451 struct page *map;
452
453 usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS);
454 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
455 usemap_size() *
456 map_count);
457 if (!usemap) {
458 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
459 goto failed;
460 }
461 sparse_buffer_init(map_count * section_map_size(), nid);
462 for_each_present_section_nr(pnum_begin, pnum) {
463 if (pnum >= pnum_end)
464 break;
465
466 map = sparse_mem_map_populate(pnum, nid, NULL);
467 if (!map) {
468 pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
469 __func__, nid);
470 pnum_begin = pnum;
471 goto failed;
472 }
473 check_usemap_section_nr(nid, usemap);
474 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap);
475 usemap += usemap_longs;
476 }
477 sparse_buffer_fini();
478 return;
479failed:
480 /* We failed to allocate, mark all the following pnums as not present */
481 for_each_present_section_nr(pnum_begin, pnum) {
482 struct mem_section *ms;
483
484 if (pnum >= pnum_end)
485 break;
486 ms = __nr_to_section(pnum);
487 ms->section_mem_map = 0;
488 }
489}
490
491/*
492 * Allocate the accumulated non-linear sections, allocate a mem_map
493 * for each and record the physical to section mapping.
494 */
Pavel Tatashin2a3cb8b2018-08-17 15:49:37 -0700495void __init sparse_init(void)
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700496{
497 unsigned long pnum_begin = first_present_section_nr();
498 int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
499 unsigned long pnum_end, map_count = 1;
500
501 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
502 set_pageblock_order();
503
504 for_each_present_section_nr(pnum_begin + 1, pnum_end) {
505 int nid = sparse_early_nid(__nr_to_section(pnum_end));
506
507 if (nid == nid_begin) {
508 map_count++;
509 continue;
510 }
511 /* Init node with sections in range [pnum_begin, pnum_end) */
512 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
513 nid_begin = nid;
514 pnum_begin = pnum_end;
515 map_count = 1;
516 }
517 /* cover the last node */
518 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
519 vmemmap_populate_print_last();
520}
521
Stephen Rothwell193faea2007-06-08 13:46:51 -0700522#ifdef CONFIG_MEMORY_HOTPLUG
Michal Hocko2d070ea2017-07-06 15:37:56 -0700523
524/* Mark all memory sections within the pfn range as online */
525void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
526{
527 unsigned long pfn;
528
529 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Michal Hockob4ccec42017-09-08 16:13:15 -0700530 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700531 struct mem_section *ms;
532
533 /* onlining code should never touch invalid ranges */
534 if (WARN_ON(!valid_section_nr(section_nr)))
535 continue;
536
537 ms = __nr_to_section(section_nr);
538 ms->section_mem_map |= SECTION_IS_ONLINE;
539 }
540}
541
542#ifdef CONFIG_MEMORY_HOTREMOVE
543/* Mark all memory sections within the pfn range as online */
544void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
545{
546 unsigned long pfn;
547
548 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Pavel Tatashin27227c72018-05-11 16:01:50 -0700549 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700550 struct mem_section *ms;
551
552 /*
553 * TODO this needs some double checking. Offlining code makes
554 * sure to check pfn_valid but those checks might be just bogus
555 */
556 if (WARN_ON(!valid_section_nr(section_nr)))
557 continue;
558
559 ms = __nr_to_section(section_nr);
560 ms->section_mem_map &= ~SECTION_IS_ONLINE;
561 }
562}
563#endif
564
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700565#ifdef CONFIG_SPARSEMEM_VMEMMAP
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100566static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
567 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700568{
569 /* This will make the necessary allocations eventually. */
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100570 return sparse_mem_map_populate(pnum, nid, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700571}
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100572static void __kfree_section_memmap(struct page *memmap,
573 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700574{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700575 unsigned long start = (unsigned long)memmap;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800576 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700577
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100578 vmemmap_free(start, end, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700579}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700580#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800581static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700582{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700583 unsigned long start = (unsigned long)memmap;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800584 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700585
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100586 vmemmap_free(start, end, NULL);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700587}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700588#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700589#else
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800590static struct page *__kmalloc_section_memmap(void)
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700591{
592 struct page *page, *ret;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800593 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700594
Yasunori Gotof2d0aa52006-10-28 10:38:32 -0700595 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700596 if (page)
597 goto got_map_page;
598
599 ret = vmalloc(memmap_size);
600 if (ret)
601 goto got_map_ptr;
602
603 return NULL;
604got_map_page:
605 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
606got_map_ptr:
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700607
608 return ret;
609}
610
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100611static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
612 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700613{
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800614 return __kmalloc_section_memmap();
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700615}
616
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100617static void __kfree_section_memmap(struct page *memmap,
618 struct vmem_altmap *altmap)
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700619{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800620 if (is_vmalloc_addr(memmap))
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700621 vfree(memmap);
622 else
623 free_pages((unsigned long)memmap,
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800624 get_order(sizeof(struct page) * PAGES_PER_SECTION));
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700625}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700626
David Rientjes4edd7ce2013-04-29 15:08:22 -0700627#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800628static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700629{
630 unsigned long maps_section_nr, removing_section_nr, i;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800631 unsigned long magic, nr_pages;
Jianguo Wuae64ffc2012-11-29 13:54:21 -0800632 struct page *page = virt_to_page(memmap);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700633
Zhang Yanfei81556b02013-11-12 15:07:43 -0800634 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
635 >> PAGE_SHIFT;
636
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700637 for (i = 0; i < nr_pages; i++, page++) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800638 magic = (unsigned long) page->freelist;
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700639
640 BUG_ON(magic == NODE_INFO);
641
642 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
Yasuaki Ishimatsu857e5222017-02-22 15:45:10 -0800643 removing_section_nr = page_private(page);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700644
645 /*
646 * When this function is called, the removing section is
647 * logical offlined state. This means all pages are isolated
648 * from page allocator. If removing section's memmap is placed
649 * on the same section, it must not be freed.
650 * If it is freed, page allocator may allocate it which will
651 * be removed physically soon.
652 */
653 if (maps_section_nr != removing_section_nr)
654 put_page_bootmem(page);
655 }
656}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700657#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700658#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700659
Andy Whitcroft29751f62005-06-23 00:08:00 -0700660/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700661 * returns the number of sections whose mem_maps were properly
662 * set. If this is <=0, then that means that the passed-in
663 * map was not consumed and must be freed.
664 */
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100665int __meminit sparse_add_one_section(struct pglist_data *pgdat,
666 unsigned long start_pfn, struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700667{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700668 unsigned long section_nr = pfn_to_section_nr(start_pfn);
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700669 struct mem_section *ms;
670 struct page *memmap;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700671 unsigned long *usemap;
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700672 unsigned long flags;
673 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700674
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700675 /*
676 * no locking for this, because it does its own
677 * plus, it does a kmalloc
678 */
WANG Congbbd06822007-12-17 16:19:59 -0800679 ret = sparse_index_init(section_nr, pgdat->node_id);
680 if (ret < 0 && ret != -EEXIST)
681 return ret;
Oscar Salvador4e409872018-08-17 15:47:14 -0700682 ret = 0;
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100683 memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800684 if (!memmap)
685 return -ENOMEM;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700686 usemap = __kmalloc_section_usemap();
WANG Congbbd06822007-12-17 16:19:59 -0800687 if (!usemap) {
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100688 __kfree_section_memmap(memmap, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800689 return -ENOMEM;
690 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700691
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700692 pgdat_resize_lock(pgdat, &flags);
693
694 ms = __pfn_to_section(start_pfn);
695 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
696 ret = -EEXIST;
697 goto out;
698 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700699
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700700 /*
701 * Poison uninitialized struct pages in order to catch invalid flags
702 * combinations.
703 */
Alexander Duyckf682a972018-10-26 15:07:45 -0700704 page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
Wen Congyang3ac19f82012-12-11 16:00:59 -0800705
Dave Hansenc4e1be92017-07-06 15:36:44 -0700706 section_mark_present(ms);
Oscar Salvador4e409872018-08-17 15:47:14 -0700707 sparse_init_one_section(ms, section_nr, memmap, usemap);
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700708
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700709out:
710 pgdat_resize_unlock(pgdat, &flags);
Oscar Salvador4e409872018-08-17 15:47:14 -0700711 if (ret < 0) {
WANG Congbbd06822007-12-17 16:19:59 -0800712 kfree(usemap);
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100713 __kfree_section_memmap(memmap, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800714 }
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700715 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700716}
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700717
Zhang Yanfeif3deb682013-07-08 16:00:10 -0700718#ifdef CONFIG_MEMORY_HOTREMOVE
Wen Congyang95a47742012-12-11 16:00:47 -0800719#ifdef CONFIG_MEMORY_FAILURE
720static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
721{
722 int i;
723
724 if (!memmap)
725 return;
726
Dan Williams4b94ffd2016-01-15 16:56:22 -0800727 for (i = 0; i < nr_pages; i++) {
Wen Congyang95a47742012-12-11 16:00:47 -0800728 if (PageHWPoison(&memmap[i])) {
Xishi Qiu293c07e2013-02-22 16:34:02 -0800729 atomic_long_sub(1, &num_poisoned_pages);
Wen Congyang95a47742012-12-11 16:00:47 -0800730 ClearPageHWPoison(&memmap[i]);
731 }
732 }
733}
734#else
735static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
736{
737}
738#endif
739
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100740static void free_section_usemap(struct page *memmap, unsigned long *usemap,
741 struct vmem_altmap *altmap)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700742{
743 struct page *usemap_page;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700744
745 if (!usemap)
746 return;
747
748 usemap_page = virt_to_page(usemap);
749 /*
750 * Check to see if allocation came from hot-plug-add
751 */
752 if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
753 kfree(usemap);
754 if (memmap)
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100755 __kfree_section_memmap(memmap, altmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700756 return;
757 }
758
759 /*
760 * The usemap came from bootmem. This is packed with other usemaps
761 * on the section which has pgdat at boot time. Just keep it as is now.
762 */
763
Zhang Yanfei81556b02013-11-12 15:07:43 -0800764 if (memmap)
765 free_map_bootmem(memmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700766}
767
Dan Williams4b94ffd2016-01-15 16:56:22 -0800768void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100769 unsigned long map_offset, struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700770{
771 struct page *memmap = NULL;
Tang Chencd099682013-02-22 16:33:02 -0800772 unsigned long *usemap = NULL, flags;
773 struct pglist_data *pgdat = zone->zone_pgdat;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700774
Tang Chencd099682013-02-22 16:33:02 -0800775 pgdat_resize_lock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700776 if (ms->section_mem_map) {
777 usemap = ms->pageblock_flags;
778 memmap = sparse_decode_mem_map(ms->section_mem_map,
779 __section_nr(ms));
780 ms->section_mem_map = 0;
781 ms->pageblock_flags = NULL;
782 }
Tang Chencd099682013-02-22 16:33:02 -0800783 pgdat_resize_unlock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700784
Dan Williams4b94ffd2016-01-15 16:56:22 -0800785 clear_hwpoisoned_pages(memmap + map_offset,
786 PAGES_PER_SECTION - map_offset);
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100787 free_section_usemap(memmap, usemap, altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700788}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700789#endif /* CONFIG_MEMORY_HOTREMOVE */
790#endif /* CONFIG_MEMORY_HOTPLUG */