blob: 248d5d7bbf55b26a064159833ad90b04cbf250bc [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>
8#include <linux/bootmem.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07009#include <linux/compiler.h>
Dave Hansen0b0acbec2005-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 Hansen0b0acbec2005-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
Santosh Shilimkarbb016b82014-01-21 15:50:34 -080071 section = memblock_virt_alloc_node(array_size, nid);
Bob Picco3e347262005-09-03 15:54:28 -070072
Dave Hansen28ae55c2005-09-03 15:54:29 -070073 return section;
Bob Picco802f1922005-09-03 15:54:26 -070074}
Dave Hansen28ae55c2005-09-03 15:54:29 -070075
Yasunori Gotoa3142c82007-05-08 00:23:07 -070076static int __meminit sparse_index_init(unsigned long section_nr, int nid)
Dave Hansen28ae55c2005-09-03 15:54:29 -070077{
Dave Hansen28ae55c2005-09-03 15:54:29 -070078 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
79 struct mem_section *section;
Dave Hansen28ae55c2005-09-03 15:54:29 -070080
81 if (mem_section[root])
82 return -EEXIST;
83
84 section = sparse_index_alloc(nid);
WANG Congaf0cd5a2007-12-17 16:19:58 -080085 if (!section)
86 return -ENOMEM;
Dave Hansen28ae55c2005-09-03 15:54:29 -070087
88 mem_section[root] = section;
Gavin Shanc1c95182012-07-31 16:46:06 -070089
Zhang Yanfei9d1936c2013-05-17 22:10:38 +080090 return 0;
Dave Hansen28ae55c2005-09-03 15:54:29 -070091}
92#else /* !SPARSEMEM_EXTREME */
93static inline int sparse_index_init(unsigned long section_nr, int nid)
94{
95 return 0;
96}
97#endif
98
Zhou Chengming91fd8b92016-07-28 15:48:35 -070099#ifdef CONFIG_SPARSEMEM_EXTREME
Dave Hansen4ca644d2005-10-29 18:16:51 -0700100int __section_nr(struct mem_section* ms)
101{
102 unsigned long root_nr;
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300103 struct mem_section *root = NULL;
Dave Hansen4ca644d2005-10-29 18:16:51 -0700104
Mike Kravetz12783b02006-05-20 15:00:05 -0700105 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
106 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
Dave Hansen4ca644d2005-10-29 18:16:51 -0700107 if (!root)
108 continue;
109
110 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
111 break;
112 }
113
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300114 VM_BUG_ON(!root);
Gavin Shandb36a462012-07-31 16:46:04 -0700115
Dave Hansen4ca644d2005-10-29 18:16:51 -0700116 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
117}
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700118#else
119int __section_nr(struct mem_section* ms)
120{
121 return (int)(ms - mem_section[0]);
122}
123#endif
Dave Hansen4ca644d2005-10-29 18:16:51 -0700124
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700125/*
126 * During early boot, before section_mem_map is used for an actual
127 * mem_map, we use section_mem_map to store the section's NUMA
128 * node. This keeps us from having to use another data structure. The
129 * node information is cleared just before we store the real mem_map.
130 */
131static inline unsigned long sparse_encode_early_nid(int nid)
132{
133 return (nid << SECTION_NID_SHIFT);
134}
135
136static inline int sparse_early_nid(struct mem_section *section)
137{
138 return (section->section_mem_map >> SECTION_NID_SHIFT);
139}
140
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700141/* Validate the physical addressing limitations of the model */
142void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
143 unsigned long *end_pfn)
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700144{
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700145 unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700146
Ingo Molnarbead9a32008-04-16 01:40:00 +0200147 /*
148 * Sanity checks - do not allow an architecture to pass
149 * in larger pfns than the maximum scope of sparsemem:
150 */
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700151 if (*start_pfn > max_sparsemem_pfn) {
152 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
153 "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
154 *start_pfn, *end_pfn, max_sparsemem_pfn);
155 WARN_ON_ONCE(1);
156 *start_pfn = max_sparsemem_pfn;
157 *end_pfn = max_sparsemem_pfn;
Cyrill Gorcunovef161a92009-03-31 15:19:25 -0700158 } else if (*end_pfn > max_sparsemem_pfn) {
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700159 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
160 "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
161 *start_pfn, *end_pfn, max_sparsemem_pfn);
162 WARN_ON_ONCE(1);
163 *end_pfn = max_sparsemem_pfn;
164 }
165}
166
Dave Hansenc4e1be92017-07-06 15:36:44 -0700167/*
168 * There are a number of times that we loop over NR_MEM_SECTIONS,
169 * looking for section_present() on each. But, when we have very
170 * large physical address spaces, NR_MEM_SECTIONS can also be
171 * very large which makes the loops quite long.
172 *
173 * Keeping track of this gives us an easy way to break out of
174 * those loops early.
175 */
176int __highest_present_section_nr;
177static void section_mark_present(struct mem_section *ms)
178{
179 int section_nr = __section_nr(ms);
180
181 if (section_nr > __highest_present_section_nr)
182 __highest_present_section_nr = section_nr;
183
184 ms->section_mem_map |= SECTION_MARKED_PRESENT;
185}
186
187static inline int next_present_section_nr(int section_nr)
188{
189 do {
190 section_nr++;
191 if (present_section_nr(section_nr))
192 return section_nr;
Wei Yangd538c162018-06-07 17:06:39 -0700193 } while ((section_nr <= __highest_present_section_nr));
Dave Hansenc4e1be92017-07-06 15:36:44 -0700194
195 return -1;
196}
197#define for_each_present_section_nr(start, section_nr) \
198 for (section_nr = next_present_section_nr(start-1); \
199 ((section_nr >= 0) && \
Dave Hansenc4e1be92017-07-06 15:36:44 -0700200 (section_nr <= __highest_present_section_nr)); \
201 section_nr = next_present_section_nr(section_nr))
202
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700203static inline unsigned long first_present_section_nr(void)
204{
205 return next_present_section_nr(-1);
206}
207
Baoquan Hef2fc10e2018-08-17 15:48:38 -0700208/*
209 * Record how many memory sections are marked as present
210 * during system bootup.
211 */
212static int __initdata nr_present_sections;
213
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700214/* Record a memory area against a node. */
215void __init memory_present(int nid, unsigned long start, unsigned long end)
216{
217 unsigned long pfn;
Ingo Molnarbead9a32008-04-16 01:40:00 +0200218
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300219#ifdef CONFIG_SPARSEMEM_EXTREME
220 if (unlikely(!mem_section)) {
221 unsigned long size, align;
222
Baoquan Hed09cfbb2018-01-04 16:18:06 -0800223 size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300224 align = 1 << (INTERNODE_CACHE_SHIFT);
225 mem_section = memblock_virt_alloc(size, align);
226 }
227#endif
228
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700229 start &= PAGE_SECTION_MASK;
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700230 mminit_validate_memmodel_limits(&start, &end);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700231 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
232 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700233 struct mem_section *ms;
234
235 sparse_index_init(section, nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -0700236 set_section_nid(section, nid);
Bob Picco802f1922005-09-03 15:54:26 -0700237
238 ms = __nr_to_section(section);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700239 if (!ms->section_mem_map) {
Michal Hocko2d070ea2017-07-06 15:37:56 -0700240 ms->section_mem_map = sparse_encode_early_nid(nid) |
241 SECTION_IS_ONLINE;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700242 section_mark_present(ms);
Baoquan Hef2fc10e2018-08-17 15:48:38 -0700243 nr_present_sections++;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700244 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700245 }
246}
247
248/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700249 * Subtle, we encode the real pfn into the mem_map such that
250 * the identity pfn - section_mem_map will return the actual
251 * physical page frame number.
252 */
253static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
254{
Petr Tesarikdef9b712018-01-31 16:20:26 -0800255 unsigned long coded_mem_map =
256 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
257 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
258 BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
259 return coded_mem_map;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700260}
261
262/*
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700263 * Decode mem_map from the coded memmap
Andy Whitcroft29751f62005-06-23 00:08:00 -0700264 */
Andy Whitcroft29751f62005-06-23 00:08:00 -0700265struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
266{
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700267 /* mask off the extra low bits of information */
268 coded_mem_map &= SECTION_MAP_MASK;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700269 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
270}
271
Oscar Salvador4e409872018-08-17 15:47:14 -0700272static void __meminit sparse_init_one_section(struct mem_section *ms,
Mel Gorman5c0e3062007-10-16 01:25:56 -0700273 unsigned long pnum, struct page *mem_map,
274 unsigned long *pageblock_bitmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700275{
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700276 ms->section_mem_map &= ~SECTION_MAP_MASK;
Andy Whitcroft540557b2007-10-16 01:24:11 -0700277 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
278 SECTION_HAS_MEM_MAP;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700279 ms->pageblock_flags = pageblock_bitmap;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700280}
281
Yasunori Goto04753272008-04-28 02:13:31 -0700282unsigned long usemap_size(void)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700283{
Wei Yang60a7a882017-05-03 14:53:51 -0700284 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
Mel Gorman5c0e3062007-10-16 01:25:56 -0700285}
286
287#ifdef CONFIG_MEMORY_HOTPLUG
288static unsigned long *__kmalloc_section_usemap(void)
289{
290 return kmalloc(usemap_size(), GFP_KERNEL);
291}
292#endif /* CONFIG_MEMORY_HOTPLUG */
293
Yasunori Goto48c90682008-07-23 21:28:15 -0700294#ifdef CONFIG_MEMORY_HOTREMOVE
295static unsigned long * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800296sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700297 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700298{
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700299 unsigned long goal, limit;
300 unsigned long *p;
301 int nid;
Yasunori Goto48c90682008-07-23 21:28:15 -0700302 /*
303 * A page may contain usemaps for other sections preventing the
304 * page being freed and making a section unremovable while
Li Zhongc800bcd2014-03-31 16:41:58 +0800305 * other sections referencing the usemap remain active. Similarly,
Yasunori Goto48c90682008-07-23 21:28:15 -0700306 * a pgdat can prevent a section being removed. If section A
307 * contains a pgdat and section B contains the usemap, both
308 * sections become inter-dependent. This allocates usemaps
309 * from the same section as the pgdat where possible to avoid
310 * this problem.
311 */
Yinghai Lu07b4e2b2012-07-11 14:02:51 -0700312 goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700313 limit = goal + (1UL << PA_SECTION_SHIFT);
314 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
315again:
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800316 p = memblock_virt_alloc_try_nid_nopanic(size,
317 SMP_CACHE_BYTES, goal, limit,
318 nid);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700319 if (!p && limit) {
320 limit = 0;
321 goto again;
322 }
323 return p;
Yasunori Goto48c90682008-07-23 21:28:15 -0700324}
325
326static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
327{
328 unsigned long usemap_snr, pgdat_snr;
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300329 static unsigned long old_usemap_snr;
330 static unsigned long old_pgdat_snr;
Yasunori Goto48c90682008-07-23 21:28:15 -0700331 struct pglist_data *pgdat = NODE_DATA(nid);
332 int usemap_nid;
333
Kirill A. Shutemov83e3c482017-09-29 17:08:16 +0300334 /* First call */
335 if (!old_usemap_snr) {
336 old_usemap_snr = NR_MEM_SECTIONS;
337 old_pgdat_snr = NR_MEM_SECTIONS;
338 }
339
Yasunori Goto48c90682008-07-23 21:28:15 -0700340 usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
341 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
342 if (usemap_snr == pgdat_snr)
343 return;
344
345 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
346 /* skip redundant message */
347 return;
348
349 old_usemap_snr = usemap_snr;
350 old_pgdat_snr = pgdat_snr;
351
352 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
353 if (usemap_nid != nid) {
Joe Perches11705322016-03-17 14:19:50 -0700354 pr_info("node %d must be removed before remove section %ld\n",
355 nid, usemap_snr);
Yasunori Goto48c90682008-07-23 21:28:15 -0700356 return;
357 }
358 /*
359 * There is a circular dependency.
360 * Some platforms allow un-removable section because they will just
361 * gather other removable sections for dynamic partitioning.
362 * Just notify un-removable section's number here.
363 */
Joe Perches11705322016-03-17 14:19:50 -0700364 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
365 usemap_snr, pgdat_snr, nid);
Yasunori Goto48c90682008-07-23 21:28:15 -0700366}
367#else
368static unsigned long * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800369sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700370 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700371{
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800372 return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
Yasunori Goto48c90682008-07-23 21:28:15 -0700373}
374
375static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
376{
377}
378#endif /* CONFIG_MEMORY_HOTREMOVE */
379
Wanpeng Li18732092013-09-11 14:22:38 -0700380static void __init sparse_early_usemaps_alloc_node(void *data,
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800381 unsigned long pnum_begin,
382 unsigned long pnum_end,
383 unsigned long usemap_count, int nodeid)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700384{
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800385 void *usemap;
386 unsigned long pnum;
Wanpeng Li18732092013-09-11 14:22:38 -0700387 unsigned long **usemap_map = (unsigned long **)data;
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800388 int size = usemap_size();
Baoquan Hec98aff62018-08-17 15:48:49 -0700389 int nr_consumed_maps = 0;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700390
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800391 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
Johannes Weiner238305b2012-05-29 15:06:36 -0700392 size * usemap_count);
Nishanth Aravamudanf5bf18f2012-03-21 16:34:07 -0700393 if (!usemap) {
Joe Perches11705322016-03-17 14:19:50 -0700394 pr_warn("%s: allocation failed\n", __func__);
Johannes Weiner238305b2012-05-29 15:06:36 -0700395 return;
Yasunori Goto48c90682008-07-23 21:28:15 -0700396 }
397
Nishanth Aravamudanf5bf18f2012-03-21 16:34:07 -0700398 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
399 if (!present_section_nr(pnum))
400 continue;
Baoquan Hec98aff62018-08-17 15:48:49 -0700401 usemap_map[nr_consumed_maps] = usemap;
Nishanth Aravamudanf5bf18f2012-03-21 16:34:07 -0700402 usemap += size;
Baoquan Hec98aff62018-08-17 15:48:49 -0700403 check_usemap_section_nr(nodeid, usemap_map[nr_consumed_maps]);
404 nr_consumed_maps++;
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800405 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700406}
407
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700408#ifdef CONFIG_SPARSEMEM_VMEMMAP
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700409static unsigned long __init section_map_size(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700410
411{
412 return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
413}
414
415#else
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700416static unsigned long __init section_map_size(void)
Pavel Tatashine131c062018-08-17 15:49:26 -0700417{
418 return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
419}
420
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100421struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
422 struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700423{
Pavel Tatashine131c062018-08-17 15:49:26 -0700424 unsigned long size = section_map_size();
425 struct page *map = sparse_buffer_alloc(size);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700426
Pavel Tatashine131c062018-08-17 15:49:26 -0700427 if (map)
428 return map;
429
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800430 map = memblock_virt_alloc_try_nid(size,
431 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
432 BOOTMEM_ALLOC_ACCESSIBLE, nid);
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700433 return map;
434}
Yinghai Lu9bdac912010-02-10 01:20:22 -0800435void __init sparse_mem_maps_populate_node(struct page **map_map,
436 unsigned long pnum_begin,
437 unsigned long pnum_end,
438 unsigned long map_count, int nodeid)
439{
Yinghai Lu9bdac912010-02-10 01:20:22 -0800440 unsigned long pnum;
Pavel Tatashine131c062018-08-17 15:49:26 -0700441 int nr_consumed_maps = 0;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800442
Yinghai Lu9bdac912010-02-10 01:20:22 -0800443 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
Yinghai Lu9bdac912010-02-10 01:20:22 -0800444 if (!present_section_nr(pnum))
445 continue;
Baoquan Hec98aff62018-08-17 15:48:49 -0700446 map_map[nr_consumed_maps] =
447 sparse_mem_map_populate(pnum, nodeid, NULL);
448 if (map_map[nr_consumed_maps++])
Yinghai Lu9bdac912010-02-10 01:20:22 -0800449 continue;
Joe Perches11705322016-03-17 14:19:50 -0700450 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
Joe Perches756a0252016-03-17 14:19:47 -0700451 __func__);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800452 }
453}
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700454#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
455
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700456static void *sparsemap_buf __meminitdata;
457static void *sparsemap_buf_end __meminitdata;
458
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700459static void __init sparse_buffer_init(unsigned long size, int nid)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700460{
461 WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
462 sparsemap_buf =
463 memblock_virt_alloc_try_nid_raw(size, PAGE_SIZE,
464 __pa(MAX_DMA_ADDRESS),
465 BOOTMEM_ALLOC_ACCESSIBLE, nid);
466 sparsemap_buf_end = sparsemap_buf + size;
467}
468
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700469static void __init sparse_buffer_fini(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700470{
471 unsigned long size = sparsemap_buf_end - sparsemap_buf;
472
473 if (sparsemap_buf && size > 0)
474 memblock_free_early(__pa(sparsemap_buf), size);
475 sparsemap_buf = NULL;
476}
477
478void * __meminit sparse_buffer_alloc(unsigned long size)
479{
480 void *ptr = NULL;
481
482 if (sparsemap_buf) {
483 ptr = PTR_ALIGN(sparsemap_buf, size);
484 if (ptr + size > sparsemap_buf_end)
485 ptr = NULL;
486 else
487 sparsemap_buf = ptr + size;
488 }
489 return ptr;
490}
491
Yinghai Lu81d0d952010-02-27 09:29:38 -0800492#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Wanpeng Li18732092013-09-11 14:22:38 -0700493static void __init sparse_early_mem_maps_alloc_node(void *data,
Yinghai Lu9bdac912010-02-10 01:20:22 -0800494 unsigned long pnum_begin,
495 unsigned long pnum_end,
496 unsigned long map_count, int nodeid)
497{
Wanpeng Li18732092013-09-11 14:22:38 -0700498 struct page **map_map = (struct page **)data;
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700499
500 sparse_buffer_init(section_map_size() * map_count, nodeid);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800501 sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
502 map_count, nodeid);
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700503 sparse_buffer_fini();
Yinghai Lu9bdac912010-02-10 01:20:22 -0800504}
Yinghai Lu81d0d952010-02-27 09:29:38 -0800505#else
Adrian Bunk9e5c6da2008-07-25 19:46:22 -0700506static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700507{
508 struct page *map;
509 struct mem_section *ms = __nr_to_section(pnum);
510 int nid = sparse_early_nid(ms);
511
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100512 map = sparse_mem_map_populate(pnum, nid, NULL);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700513 if (map)
514 return map;
515
Joe Perches11705322016-03-17 14:19:50 -0700516 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
Joe Perches756a0252016-03-17 14:19:47 -0700517 __func__);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700518 return NULL;
519}
Yinghai Lu9bdac912010-02-10 01:20:22 -0800520#endif
Andy Whitcroft29751f62005-06-23 00:08:00 -0700521
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700522void __weak __meminit vmemmap_populate_print_last(void)
Yinghai Luc2b91e22008-04-12 01:19:24 -0700523{
524}
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800525
Wanpeng Li18732092013-09-11 14:22:38 -0700526/**
527 * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
528 * @map: usemap_map for pageblock flags or mmap_map for vmemmap
Baoquan He92586312018-08-17 15:48:45 -0700529 * @unit_size: size of map unit
Stephen Rothwell193faea2007-06-08 13:46:51 -0700530 */
Wanpeng Li18732092013-09-11 14:22:38 -0700531static void __init alloc_usemap_and_memmap(void (*alloc_func)
532 (void *, unsigned long, unsigned long,
Baoquan He92586312018-08-17 15:48:45 -0700533 unsigned long, int), void *data,
534 int data_unit_size)
Stephen Rothwell193faea2007-06-08 13:46:51 -0700535{
536 unsigned long pnum;
Wanpeng Li18732092013-09-11 14:22:38 -0700537 unsigned long map_count;
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800538 int nodeid_begin = 0;
539 unsigned long pnum_begin = 0;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800540
Dave Hansenc4e1be92017-07-06 15:36:44 -0700541 for_each_present_section_nr(0, pnum) {
Yinghai Lu9bdac912010-02-10 01:20:22 -0800542 struct mem_section *ms;
543
Yinghai Lu9bdac912010-02-10 01:20:22 -0800544 ms = __nr_to_section(pnum);
545 nodeid_begin = sparse_early_nid(ms);
546 pnum_begin = pnum;
547 break;
548 }
549 map_count = 1;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700550 for_each_present_section_nr(pnum_begin + 1, pnum) {
Yinghai Lu9bdac912010-02-10 01:20:22 -0800551 struct mem_section *ms;
552 int nodeid;
553
Yinghai Lu9bdac912010-02-10 01:20:22 -0800554 ms = __nr_to_section(pnum);
555 nodeid = sparse_early_nid(ms);
556 if (nodeid == nodeid_begin) {
557 map_count++;
558 continue;
559 }
560 /* ok, we need to take cake of from pnum_begin to pnum - 1*/
Wanpeng Li18732092013-09-11 14:22:38 -0700561 alloc_func(data, pnum_begin, pnum,
562 map_count, nodeid_begin);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800563 /* new start, update count etc*/
564 nodeid_begin = nodeid;
565 pnum_begin = pnum;
Baoquan Hec98aff62018-08-17 15:48:49 -0700566 data += map_count * data_unit_size;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800567 map_count = 1;
568 }
569 /* ok, last chunk */
Wei Yang08994b22018-06-07 17:06:43 -0700570 alloc_func(data, pnum_begin, __highest_present_section_nr+1,
Wanpeng Li18732092013-09-11 14:22:38 -0700571 map_count, nodeid_begin);
572}
573
574/*
575 * Allocate the accumulated non-linear sections, allocate a mem_map
576 * for each and record the physical to section mapping.
577 */
578void __init sparse_init(void)
579{
580 unsigned long pnum;
581 struct page *map;
582 unsigned long *usemap;
583 unsigned long **usemap_map;
584 int size;
Baoquan Hec98aff62018-08-17 15:48:49 -0700585 int nr_consumed_maps = 0;
Wanpeng Li18732092013-09-11 14:22:38 -0700586#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
587 int size2;
588 struct page **map_map;
589#endif
590
591 /* see include/linux/mmzone.h 'struct mem_section' definition */
592 BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
593
594 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
595 set_pageblock_order();
596
597 /*
598 * map is using big page (aka 2M in x86 64 bit)
599 * usemap is less one page (aka 24 bytes)
600 * so alloc 2M (with 2M align) and 24 bytes in turn will
601 * make next 2M slip to one more 2M later.
602 * then in big system, the memory will have a lot of holes...
603 * here try to allocate 2M pages continuously.
604 *
605 * powerpc need to call sparse_init_one_section right after each
606 * sparse_early_mem_map_alloc, so allocate usemap_map at first.
607 */
Baoquan Hec98aff62018-08-17 15:48:49 -0700608 size = sizeof(unsigned long *) * nr_present_sections;
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800609 usemap_map = memblock_virt_alloc(size, 0);
Wanpeng Li18732092013-09-11 14:22:38 -0700610 if (!usemap_map)
611 panic("can not allocate usemap_map\n");
612 alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
Baoquan He92586312018-08-17 15:48:45 -0700613 (void *)usemap_map,
614 sizeof(usemap_map[0]));
Wanpeng Li18732092013-09-11 14:22:38 -0700615
616#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Baoquan Hec98aff62018-08-17 15:48:49 -0700617 size2 = sizeof(struct page *) * nr_present_sections;
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800618 map_map = memblock_virt_alloc(size2, 0);
Wanpeng Li18732092013-09-11 14:22:38 -0700619 if (!map_map)
620 panic("can not allocate map_map\n");
621 alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
Baoquan He92586312018-08-17 15:48:45 -0700622 (void *)map_map,
623 sizeof(map_map[0]));
Yinghai Lu9bdac912010-02-10 01:20:22 -0800624#endif
625
Baoquan Hec98aff62018-08-17 15:48:49 -0700626 /*
627 * The number of present sections stored in nr_present_sections
628 * are kept the same since mem sections are marked as present in
629 * memory_present(). In this for loop, we need check which sections
630 * failed to allocate memmap or usemap, then clear its
631 * ->section_mem_map accordingly. During this process, we need
632 * increase 'nr_consumed_maps' whether its allocation of memmap
633 * or usemap failed or not, so that after we handle the i-th
634 * memory section, can get memmap and usemap of (i+1)-th section
635 * correctly.
636 */
Dave Hansenc4e1be92017-07-06 15:36:44 -0700637 for_each_present_section_nr(0, pnum) {
Baoquan He07a34a82018-08-17 15:48:42 -0700638 struct mem_section *ms;
Baoquan Hec98aff62018-08-17 15:48:49 -0700639
640 if (nr_consumed_maps >= nr_present_sections) {
641 pr_err("nr_consumed_maps goes beyond nr_present_sections\n");
642 break;
643 }
Baoquan He07a34a82018-08-17 15:48:42 -0700644 ms = __nr_to_section(pnum);
Baoquan Hec98aff62018-08-17 15:48:49 -0700645 usemap = usemap_map[nr_consumed_maps];
Baoquan He07a34a82018-08-17 15:48:42 -0700646 if (!usemap) {
647 ms->section_mem_map = 0;
Baoquan Hec98aff62018-08-17 15:48:49 -0700648 nr_consumed_maps++;
Yinghai Lue123dd3f2008-04-13 11:51:06 -0700649 continue;
Baoquan He07a34a82018-08-17 15:48:42 -0700650 }
Stephen Rothwell193faea2007-06-08 13:46:51 -0700651
Yinghai Lu9bdac912010-02-10 01:20:22 -0800652#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Baoquan Hec98aff62018-08-17 15:48:49 -0700653 map = map_map[nr_consumed_maps];
Yinghai Lu9bdac912010-02-10 01:20:22 -0800654#else
Stephen Rothwell193faea2007-06-08 13:46:51 -0700655 map = sparse_early_mem_map_alloc(pnum);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800656#endif
Baoquan He07a34a82018-08-17 15:48:42 -0700657 if (!map) {
658 ms->section_mem_map = 0;
Baoquan Hec98aff62018-08-17 15:48:49 -0700659 nr_consumed_maps++;
Stephen Rothwell193faea2007-06-08 13:46:51 -0700660 continue;
Baoquan He07a34a82018-08-17 15:48:42 -0700661 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700662
Mel Gorman5c0e3062007-10-16 01:25:56 -0700663 sparse_init_one_section(__nr_to_section(pnum), pnum, map,
664 usemap);
Baoquan Hec98aff62018-08-17 15:48:49 -0700665 nr_consumed_maps++;
Stephen Rothwell193faea2007-06-08 13:46:51 -0700666 }
Yinghai Lue123dd3f2008-04-13 11:51:06 -0700667
Yinghai Luc2b91e22008-04-12 01:19:24 -0700668 vmemmap_populate_print_last();
669
Yinghai Lu9bdac912010-02-10 01:20:22 -0800670#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800671 memblock_free_early(__pa(map_map), size2);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800672#endif
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800673 memblock_free_early(__pa(usemap_map), size);
Stephen Rothwell193faea2007-06-08 13:46:51 -0700674}
675
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700676/*
677 * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
678 * And number of present sections in this node is map_count.
679 */
680static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
681 unsigned long pnum_end,
682 unsigned long map_count)
683{
684 unsigned long pnum, usemap_longs, *usemap;
685 struct page *map;
686
687 usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS);
688 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
689 usemap_size() *
690 map_count);
691 if (!usemap) {
692 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
693 goto failed;
694 }
695 sparse_buffer_init(map_count * section_map_size(), nid);
696 for_each_present_section_nr(pnum_begin, pnum) {
697 if (pnum >= pnum_end)
698 break;
699
700 map = sparse_mem_map_populate(pnum, nid, NULL);
701 if (!map) {
702 pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
703 __func__, nid);
704 pnum_begin = pnum;
705 goto failed;
706 }
707 check_usemap_section_nr(nid, usemap);
708 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap);
709 usemap += usemap_longs;
710 }
711 sparse_buffer_fini();
712 return;
713failed:
714 /* We failed to allocate, mark all the following pnums as not present */
715 for_each_present_section_nr(pnum_begin, pnum) {
716 struct mem_section *ms;
717
718 if (pnum >= pnum_end)
719 break;
720 ms = __nr_to_section(pnum);
721 ms->section_mem_map = 0;
722 }
723}
724
725/*
726 * Allocate the accumulated non-linear sections, allocate a mem_map
727 * for each and record the physical to section mapping.
728 */
729void __init new_sparse_init(void)
730{
731 unsigned long pnum_begin = first_present_section_nr();
732 int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
733 unsigned long pnum_end, map_count = 1;
734
735 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
736 set_pageblock_order();
737
738 for_each_present_section_nr(pnum_begin + 1, pnum_end) {
739 int nid = sparse_early_nid(__nr_to_section(pnum_end));
740
741 if (nid == nid_begin) {
742 map_count++;
743 continue;
744 }
745 /* Init node with sections in range [pnum_begin, pnum_end) */
746 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
747 nid_begin = nid;
748 pnum_begin = pnum_end;
749 map_count = 1;
750 }
751 /* cover the last node */
752 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
753 vmemmap_populate_print_last();
754}
755
Stephen Rothwell193faea2007-06-08 13:46:51 -0700756#ifdef CONFIG_MEMORY_HOTPLUG
Michal Hocko2d070ea2017-07-06 15:37:56 -0700757
758/* Mark all memory sections within the pfn range as online */
759void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
760{
761 unsigned long pfn;
762
763 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Michal Hockob4ccec42017-09-08 16:13:15 -0700764 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700765 struct mem_section *ms;
766
767 /* onlining code should never touch invalid ranges */
768 if (WARN_ON(!valid_section_nr(section_nr)))
769 continue;
770
771 ms = __nr_to_section(section_nr);
772 ms->section_mem_map |= SECTION_IS_ONLINE;
773 }
774}
775
776#ifdef CONFIG_MEMORY_HOTREMOVE
777/* Mark all memory sections within the pfn range as online */
778void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
779{
780 unsigned long pfn;
781
782 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Pavel Tatashin27227c72018-05-11 16:01:50 -0700783 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700784 struct mem_section *ms;
785
786 /*
787 * TODO this needs some double checking. Offlining code makes
788 * sure to check pfn_valid but those checks might be just bogus
789 */
790 if (WARN_ON(!valid_section_nr(section_nr)))
791 continue;
792
793 ms = __nr_to_section(section_nr);
794 ms->section_mem_map &= ~SECTION_IS_ONLINE;
795 }
796}
797#endif
798
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700799#ifdef CONFIG_SPARSEMEM_VMEMMAP
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100800static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
801 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700802{
803 /* This will make the necessary allocations eventually. */
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100804 return sparse_mem_map_populate(pnum, nid, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700805}
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100806static void __kfree_section_memmap(struct page *memmap,
807 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700808{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700809 unsigned long start = (unsigned long)memmap;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800810 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700811
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100812 vmemmap_free(start, end, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700813}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700814#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800815static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700816{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700817 unsigned long start = (unsigned long)memmap;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800818 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700819
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100820 vmemmap_free(start, end, NULL);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700821}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700822#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700823#else
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800824static struct page *__kmalloc_section_memmap(void)
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700825{
826 struct page *page, *ret;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800827 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700828
Yasunori Gotof2d0aa52006-10-28 10:38:32 -0700829 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700830 if (page)
831 goto got_map_page;
832
833 ret = vmalloc(memmap_size);
834 if (ret)
835 goto got_map_ptr;
836
837 return NULL;
838got_map_page:
839 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
840got_map_ptr:
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700841
842 return ret;
843}
844
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100845static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
846 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700847{
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800848 return __kmalloc_section_memmap();
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700849}
850
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100851static void __kfree_section_memmap(struct page *memmap,
852 struct vmem_altmap *altmap)
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700853{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800854 if (is_vmalloc_addr(memmap))
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700855 vfree(memmap);
856 else
857 free_pages((unsigned long)memmap,
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800858 get_order(sizeof(struct page) * PAGES_PER_SECTION));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700859}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700860
David Rientjes4edd7ce2013-04-29 15:08:22 -0700861#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800862static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700863{
864 unsigned long maps_section_nr, removing_section_nr, i;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800865 unsigned long magic, nr_pages;
Jianguo Wuae64ffc2012-11-29 13:54:21 -0800866 struct page *page = virt_to_page(memmap);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700867
Zhang Yanfei81556b02013-11-12 15:07:43 -0800868 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
869 >> PAGE_SHIFT;
870
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700871 for (i = 0; i < nr_pages; i++, page++) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800872 magic = (unsigned long) page->freelist;
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700873
874 BUG_ON(magic == NODE_INFO);
875
876 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
Yasuaki Ishimatsu857e5222017-02-22 15:45:10 -0800877 removing_section_nr = page_private(page);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700878
879 /*
880 * When this function is called, the removing section is
881 * logical offlined state. This means all pages are isolated
882 * from page allocator. If removing section's memmap is placed
883 * on the same section, it must not be freed.
884 * If it is freed, page allocator may allocate it which will
885 * be removed physically soon.
886 */
887 if (maps_section_nr != removing_section_nr)
888 put_page_bootmem(page);
889 }
890}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700891#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700892#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700893
Andy Whitcroft29751f62005-06-23 00:08:00 -0700894/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700895 * returns the number of sections whose mem_maps were properly
896 * set. If this is <=0, then that means that the passed-in
897 * map was not consumed and must be freed.
898 */
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100899int __meminit sparse_add_one_section(struct pglist_data *pgdat,
900 unsigned long start_pfn, struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700901{
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700902 unsigned long section_nr = pfn_to_section_nr(start_pfn);
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700903 struct mem_section *ms;
904 struct page *memmap;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700905 unsigned long *usemap;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700906 unsigned long flags;
907 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700908
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700909 /*
910 * no locking for this, because it does its own
911 * plus, it does a kmalloc
912 */
WANG Congbbd06822007-12-17 16:19:59 -0800913 ret = sparse_index_init(section_nr, pgdat->node_id);
914 if (ret < 0 && ret != -EEXIST)
915 return ret;
Oscar Salvador4e409872018-08-17 15:47:14 -0700916 ret = 0;
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100917 memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800918 if (!memmap)
919 return -ENOMEM;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700920 usemap = __kmalloc_section_usemap();
WANG Congbbd06822007-12-17 16:19:59 -0800921 if (!usemap) {
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100922 __kfree_section_memmap(memmap, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800923 return -ENOMEM;
924 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700925
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700926 pgdat_resize_lock(pgdat, &flags);
927
928 ms = __pfn_to_section(start_pfn);
929 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
930 ret = -EEXIST;
931 goto out;
932 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700933
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700934#ifdef CONFIG_DEBUG_VM
935 /*
936 * Poison uninitialized struct pages in order to catch invalid flags
937 * combinations.
938 */
939 memset(memmap, PAGE_POISON_PATTERN, sizeof(struct page) * PAGES_PER_SECTION);
940#endif
Wen Congyang3ac19f82012-12-11 16:00:59 -0800941
Dave Hansenc4e1be92017-07-06 15:36:44 -0700942 section_mark_present(ms);
Oscar Salvador4e409872018-08-17 15:47:14 -0700943 sparse_init_one_section(ms, section_nr, memmap, usemap);
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700944
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700945out:
946 pgdat_resize_unlock(pgdat, &flags);
Oscar Salvador4e409872018-08-17 15:47:14 -0700947 if (ret < 0) {
WANG Congbbd06822007-12-17 16:19:59 -0800948 kfree(usemap);
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100949 __kfree_section_memmap(memmap, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800950 }
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700951 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700952}
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700953
Zhang Yanfeif3deb682013-07-08 16:00:10 -0700954#ifdef CONFIG_MEMORY_HOTREMOVE
Wen Congyang95a47742012-12-11 16:00:47 -0800955#ifdef CONFIG_MEMORY_FAILURE
956static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
957{
958 int i;
959
960 if (!memmap)
961 return;
962
Dan Williams4b94ffd2016-01-15 16:56:22 -0800963 for (i = 0; i < nr_pages; i++) {
Wen Congyang95a47742012-12-11 16:00:47 -0800964 if (PageHWPoison(&memmap[i])) {
Xishi Qiu293c07e2013-02-22 16:34:02 -0800965 atomic_long_sub(1, &num_poisoned_pages);
Wen Congyang95a47742012-12-11 16:00:47 -0800966 ClearPageHWPoison(&memmap[i]);
967 }
968 }
969}
970#else
971static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
972{
973}
974#endif
975
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100976static void free_section_usemap(struct page *memmap, unsigned long *usemap,
977 struct vmem_altmap *altmap)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700978{
979 struct page *usemap_page;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700980
981 if (!usemap)
982 return;
983
984 usemap_page = virt_to_page(usemap);
985 /*
986 * Check to see if allocation came from hot-plug-add
987 */
988 if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
989 kfree(usemap);
990 if (memmap)
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100991 __kfree_section_memmap(memmap, altmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700992 return;
993 }
994
995 /*
996 * The usemap came from bootmem. This is packed with other usemaps
997 * on the section which has pgdat at boot time. Just keep it as is now.
998 */
999
Zhang Yanfei81556b02013-11-12 15:07:43 -08001000 if (memmap)
1001 free_map_bootmem(memmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -07001002}
1003
Dan Williams4b94ffd2016-01-15 16:56:22 -08001004void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001005 unsigned long map_offset, struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -07001006{
1007 struct page *memmap = NULL;
Tang Chencd099682013-02-22 16:33:02 -08001008 unsigned long *usemap = NULL, flags;
1009 struct pglist_data *pgdat = zone->zone_pgdat;
Badari Pulavartyea01ea92008-04-28 02:12:01 -07001010
Tang Chencd099682013-02-22 16:33:02 -08001011 pgdat_resize_lock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -07001012 if (ms->section_mem_map) {
1013 usemap = ms->pageblock_flags;
1014 memmap = sparse_decode_mem_map(ms->section_mem_map,
1015 __section_nr(ms));
1016 ms->section_mem_map = 0;
1017 ms->pageblock_flags = NULL;
1018 }
Tang Chencd099682013-02-22 16:33:02 -08001019 pgdat_resize_unlock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -07001020
Dan Williams4b94ffd2016-01-15 16:56:22 -08001021 clear_hwpoisoned_pages(memmap + map_offset,
1022 PAGES_PER_SECTION - map_offset);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001023 free_section_usemap(memmap, usemap, altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -07001024}
David Rientjes4edd7ce2013-04-29 15:08:22 -07001025#endif /* CONFIG_MEMORY_HOTREMOVE */
1026#endif /* CONFIG_MEMORY_HOTPLUG */