blob: 6b01022e23a99e5d8ed9925caf9bbf5b46fadb8e [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 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. Shutemov83e3c4872017-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
Mike Rapoport8a7f97b2019-03-11 23:30:31 -070068 if (slab_is_available()) {
Michal Hockob95046b2017-09-06 16:20:41 -070069 section = kzalloc_node(array_size, GFP_KERNEL, nid);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -070070 } else {
Mike Rapoport7e1c4e22018-10-30 15:09:57 -070071 section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
72 nid);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -070073 if (!section)
74 panic("%s: Failed to allocate %lu bytes nid=%d\n",
75 __func__, array_size, nid);
76 }
Bob Picco3e347262005-09-03 15:54:28 -070077
Dave Hansen28ae55c2005-09-03 15:54:29 -070078 return section;
Bob Picco802f1922005-09-03 15:54:26 -070079}
Dave Hansen28ae55c2005-09-03 15:54:29 -070080
Yasunori Gotoa3142c82007-05-08 00:23:07 -070081static int __meminit sparse_index_init(unsigned long section_nr, int nid)
Dave Hansen28ae55c2005-09-03 15:54:29 -070082{
Dave Hansen28ae55c2005-09-03 15:54:29 -070083 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
84 struct mem_section *section;
Dave Hansen28ae55c2005-09-03 15:54:29 -070085
86 if (mem_section[root])
87 return -EEXIST;
88
89 section = sparse_index_alloc(nid);
WANG Congaf0cd5a2007-12-17 16:19:58 -080090 if (!section)
91 return -ENOMEM;
Dave Hansen28ae55c2005-09-03 15:54:29 -070092
93 mem_section[root] = section;
Gavin Shanc1c95182012-07-31 16:46:06 -070094
Zhang Yanfei9d1936c2013-05-17 22:10:38 +080095 return 0;
Dave Hansen28ae55c2005-09-03 15:54:29 -070096}
97#else /* !SPARSEMEM_EXTREME */
98static inline int sparse_index_init(unsigned long section_nr, int nid)
99{
100 return 0;
101}
102#endif
103
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700104#ifdef CONFIG_SPARSEMEM_EXTREME
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700105unsigned long __section_nr(struct mem_section *ms)
Dave Hansen4ca644d2005-10-29 18:16:51 -0700106{
107 unsigned long root_nr;
Kirill A. Shutemov83e3c4872017-09-29 17:08:16 +0300108 struct mem_section *root = NULL;
Dave Hansen4ca644d2005-10-29 18:16:51 -0700109
Mike Kravetz12783b02006-05-20 15:00:05 -0700110 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
111 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
Dave Hansen4ca644d2005-10-29 18:16:51 -0700112 if (!root)
113 continue;
114
115 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
116 break;
117 }
118
Kirill A. Shutemov83e3c4872017-09-29 17:08:16 +0300119 VM_BUG_ON(!root);
Gavin Shandb36a462012-07-31 16:46:04 -0700120
Dave Hansen4ca644d2005-10-29 18:16:51 -0700121 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
122}
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700123#else
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700124unsigned long __section_nr(struct mem_section *ms)
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700125{
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700126 return (unsigned long)(ms - mem_section[0]);
Zhou Chengming91fd8b92016-07-28 15:48:35 -0700127}
128#endif
Dave Hansen4ca644d2005-10-29 18:16:51 -0700129
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700130/*
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 */
136static inline unsigned long sparse_encode_early_nid(int nid)
137{
138 return (nid << SECTION_NID_SHIFT);
139}
140
141static inline int sparse_early_nid(struct mem_section *section)
142{
143 return (section->section_mem_map >> SECTION_NID_SHIFT);
144}
145
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700146/* Validate the physical addressing limitations of the model */
147void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
148 unsigned long *end_pfn)
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700149{
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700150 unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700151
Ingo Molnarbead9a32008-04-16 01:40:00 +0200152 /*
153 * Sanity checks - do not allow an architecture to pass
154 * in larger pfns than the maximum scope of sparsemem:
155 */
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700156 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 Gorcunovef161a92009-03-31 15:19:25 -0700163 } else if (*end_pfn > max_sparsemem_pfn) {
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700164 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 Hansenc4e1be92017-07-06 15:36:44 -0700172/*
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 */
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700181unsigned long __highest_present_section_nr;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700182static void section_mark_present(struct mem_section *ms)
183{
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700184 unsigned long section_nr = __section_nr(ms);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700185
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
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700192static inline unsigned long next_present_section_nr(unsigned long section_nr)
Dave Hansenc4e1be92017-07-06 15:36:44 -0700193{
194 do {
195 section_nr++;
196 if (present_section_nr(section_nr))
197 return section_nr;
Wei Yangd538c162018-06-07 17:06:39 -0700198 } while ((section_nr <= __highest_present_section_nr));
Dave Hansenc4e1be92017-07-06 15:36:44 -0700199
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 Caid7780152019-03-05 15:50:11 -0800204 ((section_nr != -1) && \
Dave Hansenc4e1be92017-07-06 15:36:44 -0700205 (section_nr <= __highest_present_section_nr)); \
206 section_nr = next_present_section_nr(section_nr))
207
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700208static inline unsigned long first_present_section_nr(void)
209{
210 return next_present_section_nr(-1);
211}
212
Dan Williamsf46edbd2019-07-18 15:58:04 -0700213void subsection_mask_set(unsigned long *map, unsigned long pfn,
214 unsigned long nr_pages)
215{
216 int idx = subsection_map_index(pfn);
217 int end = subsection_map_index(pfn + nr_pages - 1);
218
219 bitmap_set(map, idx, end - idx + 1);
220}
221
222void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
223{
224 int end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
225 int i, start_sec = pfn_to_section_nr(pfn);
226
227 if (!nr_pages)
228 return;
229
230 for (i = start_sec; i <= end_sec; i++) {
231 struct mem_section *ms;
232 unsigned long pfns;
233
234 pfns = min(nr_pages, PAGES_PER_SECTION
235 - (pfn & ~PAGE_SECTION_MASK));
236 ms = __nr_to_section(i);
237 subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
238
239 pr_debug("%s: sec: %d pfns: %ld set(%d, %d)\n", __func__, i,
240 pfns, subsection_map_index(pfn),
241 subsection_map_index(pfn + pfns - 1));
242
243 pfn += pfns;
244 nr_pages -= pfns;
245 }
246}
247
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700248/* Record a memory area against a node. */
249void __init memory_present(int nid, unsigned long start, unsigned long end)
250{
251 unsigned long pfn;
Ingo Molnarbead9a32008-04-16 01:40:00 +0200252
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300253#ifdef CONFIG_SPARSEMEM_EXTREME
254 if (unlikely(!mem_section)) {
255 unsigned long size, align;
256
Baoquan Hed09cfbb2018-01-04 16:18:06 -0800257 size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300258 align = 1 << (INTERNODE_CACHE_SHIFT);
Mike Rapoporteb31d552018-10-30 15:08:04 -0700259 mem_section = memblock_alloc(size, align);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700260 if (!mem_section)
261 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
262 __func__, size, align);
Kirill A. Shutemov629a3592017-11-07 11:33:37 +0300263 }
264#endif
265
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700266 start &= PAGE_SECTION_MASK;
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700267 mminit_validate_memmodel_limits(&start, &end);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700268 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
269 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700270 struct mem_section *ms;
271
272 sparse_index_init(section, nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -0700273 set_section_nid(section, nid);
Bob Picco802f1922005-09-03 15:54:26 -0700274
275 ms = __nr_to_section(section);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700276 if (!ms->section_mem_map) {
Michal Hocko2d070ea2017-07-06 15:37:56 -0700277 ms->section_mem_map = sparse_encode_early_nid(nid) |
278 SECTION_IS_ONLINE;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700279 section_mark_present(ms);
280 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700281 }
282}
283
284/*
Logan Gunthorpe9def36e2018-12-14 14:16:57 -0800285 * Mark all memblocks as present using memory_present(). This is a
286 * convienence function that is useful for a number of arches
287 * to mark all of the systems memory as present during initialization.
288 */
289void __init memblocks_present(void)
290{
291 struct memblock_region *reg;
292
293 for_each_memblock(memory, reg) {
294 memory_present(memblock_get_region_node(reg),
295 memblock_region_memory_base_pfn(reg),
296 memblock_region_memory_end_pfn(reg));
297 }
298}
299
300/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700301 * Subtle, we encode the real pfn into the mem_map such that
302 * the identity pfn - section_mem_map will return the actual
303 * physical page frame number.
304 */
305static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
306{
Petr Tesarikdef9b712018-01-31 16:20:26 -0800307 unsigned long coded_mem_map =
308 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
309 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
310 BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
311 return coded_mem_map;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700312}
313
314/*
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700315 * Decode mem_map from the coded memmap
Andy Whitcroft29751f62005-06-23 00:08:00 -0700316 */
Andy Whitcroft29751f62005-06-23 00:08:00 -0700317struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
318{
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700319 /* mask off the extra low bits of information */
320 coded_mem_map &= SECTION_MAP_MASK;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700321 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
322}
323
Oscar Salvador4e409872018-08-17 15:47:14 -0700324static void __meminit sparse_init_one_section(struct mem_section *ms,
Mel Gorman5c0e3062007-10-16 01:25:56 -0700325 unsigned long pnum, struct page *mem_map,
Dan Williams326e1b82019-07-18 15:58:00 -0700326 struct mem_section_usage *usage, unsigned long flags)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700327{
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700328 ms->section_mem_map &= ~SECTION_MAP_MASK;
Dan Williams326e1b82019-07-18 15:58:00 -0700329 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum)
330 | SECTION_HAS_MEM_MAP | flags;
Dan Williamsf1eca352019-07-18 15:57:57 -0700331 ms->usage = usage;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700332}
333
Dan Williamsf1eca352019-07-18 15:57:57 -0700334static unsigned long usemap_size(void)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700335{
Wei Yang60a7a882017-05-03 14:53:51 -0700336 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
Mel Gorman5c0e3062007-10-16 01:25:56 -0700337}
338
Dan Williamsf1eca352019-07-18 15:57:57 -0700339size_t mem_section_usage_size(void)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700340{
Dan Williamsf1eca352019-07-18 15:57:57 -0700341 return sizeof(struct mem_section_usage) + usemap_size();
Mel Gorman5c0e3062007-10-16 01:25:56 -0700342}
Mel Gorman5c0e3062007-10-16 01:25:56 -0700343
Yasunori Goto48c90682008-07-23 21:28:15 -0700344#ifdef CONFIG_MEMORY_HOTREMOVE
Dan Williamsf1eca352019-07-18 15:57:57 -0700345static struct mem_section_usage * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800346sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700347 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700348{
Dan Williamsf1eca352019-07-18 15:57:57 -0700349 struct mem_section_usage *usage;
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700350 unsigned long goal, limit;
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700351 int nid;
Yasunori Goto48c90682008-07-23 21:28:15 -0700352 /*
353 * A page may contain usemaps for other sections preventing the
354 * page being freed and making a section unremovable while
Li Zhongc800bcd2014-03-31 16:41:58 +0800355 * other sections referencing the usemap remain active. Similarly,
Yasunori Goto48c90682008-07-23 21:28:15 -0700356 * a pgdat can prevent a section being removed. If section A
357 * contains a pgdat and section B contains the usemap, both
358 * sections become inter-dependent. This allocates usemaps
359 * from the same section as the pgdat where possible to avoid
360 * this problem.
361 */
Yinghai Lu07b4e2b2012-07-11 14:02:51 -0700362 goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700363 limit = goal + (1UL << PA_SECTION_SHIFT);
364 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
365again:
Dan Williamsf1eca352019-07-18 15:57:57 -0700366 usage = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid);
367 if (!usage && limit) {
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700368 limit = 0;
369 goto again;
370 }
Dan Williamsf1eca352019-07-18 15:57:57 -0700371 return usage;
Yasunori Goto48c90682008-07-23 21:28:15 -0700372}
373
Dan Williamsf1eca352019-07-18 15:57:57 -0700374static void __init check_usemap_section_nr(int nid,
375 struct mem_section_usage *usage)
Yasunori Goto48c90682008-07-23 21:28:15 -0700376{
377 unsigned long usemap_snr, pgdat_snr;
Kirill A. Shutemov83e3c4872017-09-29 17:08:16 +0300378 static unsigned long old_usemap_snr;
379 static unsigned long old_pgdat_snr;
Yasunori Goto48c90682008-07-23 21:28:15 -0700380 struct pglist_data *pgdat = NODE_DATA(nid);
381 int usemap_nid;
382
Kirill A. Shutemov83e3c4872017-09-29 17:08:16 +0300383 /* First call */
384 if (!old_usemap_snr) {
385 old_usemap_snr = NR_MEM_SECTIONS;
386 old_pgdat_snr = NR_MEM_SECTIONS;
387 }
388
Dan Williamsf1eca352019-07-18 15:57:57 -0700389 usemap_snr = pfn_to_section_nr(__pa(usage) >> PAGE_SHIFT);
Yasunori Goto48c90682008-07-23 21:28:15 -0700390 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
391 if (usemap_snr == pgdat_snr)
392 return;
393
394 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
395 /* skip redundant message */
396 return;
397
398 old_usemap_snr = usemap_snr;
399 old_pgdat_snr = pgdat_snr;
400
401 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
402 if (usemap_nid != nid) {
Joe Perches11705322016-03-17 14:19:50 -0700403 pr_info("node %d must be removed before remove section %ld\n",
404 nid, usemap_snr);
Yasunori Goto48c90682008-07-23 21:28:15 -0700405 return;
406 }
407 /*
408 * There is a circular dependency.
409 * Some platforms allow un-removable section because they will just
410 * gather other removable sections for dynamic partitioning.
411 * Just notify un-removable section's number here.
412 */
Joe Perches11705322016-03-17 14:19:50 -0700413 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
414 usemap_snr, pgdat_snr, nid);
Yasunori Goto48c90682008-07-23 21:28:15 -0700415}
416#else
Dan Williamsf1eca352019-07-18 15:57:57 -0700417static struct mem_section_usage * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800418sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700419 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700420{
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700421 return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id);
Yasunori Goto48c90682008-07-23 21:28:15 -0700422}
423
Dan Williamsf1eca352019-07-18 15:57:57 -0700424static void __init check_usemap_section_nr(int nid,
425 struct mem_section_usage *usage)
Yasunori Goto48c90682008-07-23 21:28:15 -0700426{
427}
428#endif /* CONFIG_MEMORY_HOTREMOVE */
429
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700430#ifdef CONFIG_SPARSEMEM_VMEMMAP
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700431static unsigned long __init section_map_size(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700432{
433 return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
434}
435
436#else
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700437static unsigned long __init section_map_size(void)
Pavel Tatashine131c062018-08-17 15:49:26 -0700438{
439 return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
440}
441
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700442struct page __init *__populate_section_memmap(unsigned long pfn,
443 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700444{
Pavel Tatashine131c062018-08-17 15:49:26 -0700445 unsigned long size = section_map_size();
446 struct page *map = sparse_buffer_alloc(size);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700447 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700448
Pavel Tatashine131c062018-08-17 15:49:26 -0700449 if (map)
450 return map;
451
Mike Rapoporteb31d552018-10-30 15:08:04 -0700452 map = memblock_alloc_try_nid(size,
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700453 PAGE_SIZE, addr,
Mike Rapoport97ad1082018-10-30 15:09:44 -0700454 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700455 if (!map)
456 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
457 __func__, size, PAGE_SIZE, nid, &addr);
458
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700459 return map;
460}
461#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
462
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700463static void *sparsemap_buf __meminitdata;
464static void *sparsemap_buf_end __meminitdata;
465
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700466static void __init sparse_buffer_init(unsigned long size, int nid)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700467{
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700468 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700469 WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
470 sparsemap_buf =
Mike Rapoporteb31d552018-10-30 15:08:04 -0700471 memblock_alloc_try_nid_raw(size, PAGE_SIZE,
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700472 addr,
Mike Rapoport97ad1082018-10-30 15:09:44 -0700473 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700474 sparsemap_buf_end = sparsemap_buf + size;
475}
476
Pavel Tatashinafda57b2018-08-17 15:49:30 -0700477static void __init sparse_buffer_fini(void)
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -0700478{
479 unsigned long size = sparsemap_buf_end - sparsemap_buf;
480
481 if (sparsemap_buf && size > 0)
482 memblock_free_early(__pa(sparsemap_buf), size);
483 sparsemap_buf = NULL;
484}
485
486void * __meminit sparse_buffer_alloc(unsigned long size)
487{
488 void *ptr = NULL;
489
490 if (sparsemap_buf) {
491 ptr = PTR_ALIGN(sparsemap_buf, size);
492 if (ptr + size > sparsemap_buf_end)
493 ptr = NULL;
494 else
495 sparsemap_buf = ptr + size;
496 }
497 return ptr;
498}
499
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700500void __weak __meminit vmemmap_populate_print_last(void)
Yinghai Luc2b91e22008-04-12 01:19:24 -0700501{
502}
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800503
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700504/*
505 * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
506 * And number of present sections in this node is map_count.
507 */
508static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
509 unsigned long pnum_end,
510 unsigned long map_count)
511{
Dan Williamsf1eca352019-07-18 15:57:57 -0700512 struct mem_section_usage *usage;
513 unsigned long pnum;
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700514 struct page *map;
515
Dan Williamsf1eca352019-07-18 15:57:57 -0700516 usage = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
517 mem_section_usage_size() * map_count);
518 if (!usage) {
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700519 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
520 goto failed;
521 }
522 sparse_buffer_init(map_count * section_map_size(), nid);
523 for_each_present_section_nr(pnum_begin, pnum) {
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700524 unsigned long pfn = section_nr_to_pfn(pnum);
525
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700526 if (pnum >= pnum_end)
527 break;
528
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700529 map = __populate_section_memmap(pfn, PAGES_PER_SECTION,
530 nid, NULL);
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700531 if (!map) {
532 pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
533 __func__, nid);
534 pnum_begin = pnum;
535 goto failed;
536 }
Dan Williamsf1eca352019-07-18 15:57:57 -0700537 check_usemap_section_nr(nid, usage);
Dan Williams326e1b82019-07-18 15:58:00 -0700538 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
539 SECTION_IS_EARLY);
Dan Williamsf1eca352019-07-18 15:57:57 -0700540 usage = (void *) usage + mem_section_usage_size();
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700541 }
542 sparse_buffer_fini();
543 return;
544failed:
545 /* We failed to allocate, mark all the following pnums as not present */
546 for_each_present_section_nr(pnum_begin, pnum) {
547 struct mem_section *ms;
548
549 if (pnum >= pnum_end)
550 break;
551 ms = __nr_to_section(pnum);
552 ms->section_mem_map = 0;
553 }
554}
555
556/*
557 * Allocate the accumulated non-linear sections, allocate a mem_map
558 * for each and record the physical to section mapping.
559 */
Pavel Tatashin2a3cb8b2018-08-17 15:49:37 -0700560void __init sparse_init(void)
Pavel Tatashin85c77f72018-08-17 15:49:33 -0700561{
562 unsigned long pnum_begin = first_present_section_nr();
563 int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
564 unsigned long pnum_end, map_count = 1;
565
566 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
567 set_pageblock_order();
568
569 for_each_present_section_nr(pnum_begin + 1, pnum_end) {
570 int nid = sparse_early_nid(__nr_to_section(pnum_end));
571
572 if (nid == nid_begin) {
573 map_count++;
574 continue;
575 }
576 /* Init node with sections in range [pnum_begin, pnum_end) */
577 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
578 nid_begin = nid;
579 pnum_begin = pnum_end;
580 map_count = 1;
581 }
582 /* cover the last node */
583 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
584 vmemmap_populate_print_last();
585}
586
Stephen Rothwell193faea2007-06-08 13:46:51 -0700587#ifdef CONFIG_MEMORY_HOTPLUG
Michal Hocko2d070ea2017-07-06 15:37:56 -0700588
589/* Mark all memory sections within the pfn range as online */
590void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
591{
592 unsigned long pfn;
593
594 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Michal Hockob4ccec42017-09-08 16:13:15 -0700595 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700596 struct mem_section *ms;
597
598 /* onlining code should never touch invalid ranges */
599 if (WARN_ON(!valid_section_nr(section_nr)))
600 continue;
601
602 ms = __nr_to_section(section_nr);
603 ms->section_mem_map |= SECTION_IS_ONLINE;
604 }
605}
606
607#ifdef CONFIG_MEMORY_HOTREMOVE
Qian Cai9b7ea462019-03-28 20:43:34 -0700608/* Mark all memory sections within the pfn range as offline */
Michal Hocko2d070ea2017-07-06 15:37:56 -0700609void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
610{
611 unsigned long pfn;
612
613 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Pavel Tatashin27227c72018-05-11 16:01:50 -0700614 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700615 struct mem_section *ms;
616
617 /*
618 * TODO this needs some double checking. Offlining code makes
619 * sure to check pfn_valid but those checks might be just bogus
620 */
621 if (WARN_ON(!valid_section_nr(section_nr)))
622 continue;
623
624 ms = __nr_to_section(section_nr);
625 ms->section_mem_map &= ~SECTION_IS_ONLINE;
626 }
627}
628#endif
629
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700630#ifdef CONFIG_SPARSEMEM_VMEMMAP
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700631static struct page *populate_section_memmap(unsigned long pfn,
632 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700633{
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700634 return __populate_section_memmap(pfn, nr_pages, nid, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700635}
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700636
637static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100638 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700639{
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700640 unsigned long start = (unsigned long) pfn_to_page(pfn);
641 unsigned long end = start + nr_pages * sizeof(struct page);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700642
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100643 vmemmap_free(start, end, altmap);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700644}
Zhang Yanfei81556b02013-11-12 15:07:43 -0800645static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700646{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700647 unsigned long start = (unsigned long)memmap;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800648 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700649
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100650 vmemmap_free(start, end, NULL);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700651}
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700652#else
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700653struct page *populate_section_memmap(unsigned long pfn,
654 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700655{
656 struct page *page, *ret;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800657 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700658
Yasunori Gotof2d0aa52006-10-28 10:38:32 -0700659 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700660 if (page)
661 goto got_map_page;
662
663 ret = vmalloc(memmap_size);
664 if (ret)
665 goto got_map_ptr;
666
667 return NULL;
668got_map_page:
669 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
670got_map_ptr:
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700671
672 return ret;
673}
674
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700675static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100676 struct vmem_altmap *altmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700677{
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700678 struct page *memmap = pfn_to_page(pfn);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700679
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800680 if (is_vmalloc_addr(memmap))
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700681 vfree(memmap);
682 else
683 free_pages((unsigned long)memmap,
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800684 get_order(sizeof(struct page) * PAGES_PER_SECTION));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700685}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700686
Zhang Yanfei81556b02013-11-12 15:07:43 -0800687static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700688{
689 unsigned long maps_section_nr, removing_section_nr, i;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800690 unsigned long magic, nr_pages;
Jianguo Wuae64ffc2012-11-29 13:54:21 -0800691 struct page *page = virt_to_page(memmap);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700692
Zhang Yanfei81556b02013-11-12 15:07:43 -0800693 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
694 >> PAGE_SHIFT;
695
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700696 for (i = 0; i < nr_pages; i++, page++) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800697 magic = (unsigned long) page->freelist;
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700698
699 BUG_ON(magic == NODE_INFO);
700
701 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
Yasuaki Ishimatsu857e5222017-02-22 15:45:10 -0800702 removing_section_nr = page_private(page);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700703
704 /*
705 * When this function is called, the removing section is
706 * logical offlined state. This means all pages are isolated
707 * from page allocator. If removing section's memmap is placed
708 * on the same section, it must not be freed.
709 * If it is freed, page allocator may allocate it which will
710 * be removed physically soon.
711 */
712 if (maps_section_nr != removing_section_nr)
713 put_page_bootmem(page);
714 }
715}
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700716#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700717
Baoquan He7567cfc2019-05-13 17:19:32 -0700718/**
719 * sparse_add_one_section - add a memory section
720 * @nid: The node to add section on
721 * @start_pfn: start pfn of the memory range
722 * @altmap: device page map
723 *
724 * This is only intended for hotplug.
725 *
726 * Return:
727 * * 0 - On success.
728 * * -EEXIST - Section has been present.
729 * * -ENOMEM - Out of memory.
Andy Whitcroft29751f62005-06-23 00:08:00 -0700730 */
Wei Yang4e0d2e72018-12-28 00:37:06 -0800731int __meminit sparse_add_one_section(int nid, unsigned long start_pfn,
732 struct vmem_altmap *altmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700733{
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700734 unsigned long section_nr = pfn_to_section_nr(start_pfn);
Dan Williamsf1eca352019-07-18 15:57:57 -0700735 struct mem_section_usage *usage;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700736 struct mem_section *ms;
737 struct page *memmap;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700738 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700739
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700740 /*
741 * no locking for this, because it does its own
742 * plus, it does a kmalloc
743 */
Wei Yang4e0d2e72018-12-28 00:37:06 -0800744 ret = sparse_index_init(section_nr, nid);
WANG Congbbd06822007-12-17 16:19:59 -0800745 if (ret < 0 && ret != -EEXIST)
746 return ret;
Oscar Salvador4e409872018-08-17 15:47:14 -0700747 ret = 0;
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700748 memmap = populate_section_memmap(start_pfn, PAGES_PER_SECTION, nid,
749 altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800750 if (!memmap)
751 return -ENOMEM;
Dan Williamsf1eca352019-07-18 15:57:57 -0700752 usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
753 if (!usage) {
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700754 depopulate_section_memmap(start_pfn, PAGES_PER_SECTION, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800755 return -ENOMEM;
756 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700757
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700758 ms = __pfn_to_section(start_pfn);
759 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
760 ret = -EEXIST;
761 goto out;
762 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700763
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700764 /*
765 * Poison uninitialized struct pages in order to catch invalid flags
766 * combinations.
767 */
Alexander Duyckf682a972018-10-26 15:07:45 -0700768 page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
Wen Congyang3ac19f82012-12-11 16:00:59 -0800769
Wei Yang26f26be2019-07-18 15:57:21 -0700770 set_section_nid(section_nr, nid);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700771 section_mark_present(ms);
Dan Williams326e1b82019-07-18 15:58:00 -0700772 sparse_init_one_section(ms, section_nr, memmap, usage, 0);
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700773
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700774out:
Oscar Salvador4e409872018-08-17 15:47:14 -0700775 if (ret < 0) {
Dan Williamsf1eca352019-07-18 15:57:57 -0700776 kfree(usage);
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700777 depopulate_section_memmap(start_pfn, PAGES_PER_SECTION, altmap);
WANG Congbbd06822007-12-17 16:19:59 -0800778 }
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700779 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700780}
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700781
Wen Congyang95a47742012-12-11 16:00:47 -0800782#ifdef CONFIG_MEMORY_FAILURE
783static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
784{
785 int i;
786
787 if (!memmap)
788 return;
789
Balbir Singh5eb570a2018-12-28 00:33:24 -0800790 /*
791 * A further optimization is to have per section refcounted
792 * num_poisoned_pages. But that would need more space per memmap, so
793 * for now just do a quick global check to speed up this routine in the
794 * absence of bad pages.
795 */
796 if (atomic_long_read(&num_poisoned_pages) == 0)
797 return;
798
Dan Williams4b94ffd2016-01-15 16:56:22 -0800799 for (i = 0; i < nr_pages; i++) {
Wen Congyang95a47742012-12-11 16:00:47 -0800800 if (PageHWPoison(&memmap[i])) {
Xishi Qiu293c07e2013-02-22 16:34:02 -0800801 atomic_long_sub(1, &num_poisoned_pages);
Wen Congyang95a47742012-12-11 16:00:47 -0800802 ClearPageHWPoison(&memmap[i]);
803 }
804 }
805}
806#else
807static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
808{
809}
810#endif
811
Dan Williams326e1b82019-07-18 15:58:00 -0700812static void free_section_usage(struct mem_section *ms, struct page *memmap,
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700813 struct mem_section_usage *usage, unsigned long pfn,
814 unsigned long nr_pages, struct vmem_altmap *altmap)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700815{
Dan Williamsf1eca352019-07-18 15:57:57 -0700816 if (!usage)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700817 return;
818
David Rientjes4edd7ce2013-04-29 15:08:22 -0700819 /*
820 * Check to see if allocation came from hot-plug-add
821 */
Dan Williams326e1b82019-07-18 15:58:00 -0700822 if (!early_section(ms)) {
Dan Williamsf1eca352019-07-18 15:57:57 -0700823 kfree(usage);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700824 if (memmap)
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700825 depopulate_section_memmap(pfn, nr_pages, altmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700826 return;
827 }
828
829 /*
830 * The usemap came from bootmem. This is packed with other usemaps
831 * on the section which has pgdat at boot time. Just keep it as is now.
832 */
833
Zhang Yanfei81556b02013-11-12 15:07:43 -0800834 if (memmap)
835 free_map_bootmem(memmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700836}
837
David Hildenbrandb9bf8d32019-07-18 15:57:17 -0700838void sparse_remove_one_section(struct mem_section *ms, unsigned long map_offset,
839 struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700840{
841 struct page *memmap = NULL;
Dan Williamsf1eca352019-07-18 15:57:57 -0700842 struct mem_section_usage *usage = NULL;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700843
844 if (ms->section_mem_map) {
Dan Williamsf1eca352019-07-18 15:57:57 -0700845 usage = ms->usage;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700846 memmap = sparse_decode_mem_map(ms->section_mem_map,
847 __section_nr(ms));
848 ms->section_mem_map = 0;
Dan Williamsf1eca352019-07-18 15:57:57 -0700849 ms->usage = NULL;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700850 }
851
Dan Williams4b94ffd2016-01-15 16:56:22 -0800852 clear_hwpoisoned_pages(memmap + map_offset,
853 PAGES_PER_SECTION - map_offset);
Dan Williamse9c0a3f02019-07-18 15:58:11 -0700854 free_section_usage(ms, memmap, usage,
855 section_nr_to_pfn(__section_nr(ms)),
856 PAGES_PER_SECTION, altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700857}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700858#endif /* CONFIG_MEMORY_HOTPLUG */