blob: 4900707ae146cf73c1c9ca4d01bd63a9dfc27c8b [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
Bob Picco802f1922005-09-03 15:54:26 -070026struct mem_section *mem_section[NR_SECTION_ROOTS]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080027 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070028#else
29struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080030 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070031#endif
32EXPORT_SYMBOL(mem_section);
33
Christoph Lameter89689ae2006-12-06 20:31:45 -080034#ifdef NODE_NOT_IN_PAGE_FLAGS
35/*
36 * If we did not store the node number in the page then we have to
37 * do a lookup in the section_to_node_table in order to find which
38 * node the page belongs to.
39 */
40#if MAX_NUMNODES <= 256
41static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
42#else
43static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
44#endif
45
Ian Campbell33dd4e02011-07-25 17:11:51 -070046int page_to_nid(const struct page *page)
Christoph Lameter89689ae2006-12-06 20:31:45 -080047{
48 return section_to_node_table[page_to_section(page)];
49}
50EXPORT_SYMBOL(page_to_nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -070051
52static void set_section_nid(unsigned long section_nr, int nid)
53{
54 section_to_node_table[section_nr] = nid;
55}
56#else /* !NODE_NOT_IN_PAGE_FLAGS */
57static inline void set_section_nid(unsigned long section_nr, int nid)
58{
59}
Christoph Lameter89689ae2006-12-06 20:31:45 -080060#endif
61
Bob Picco3e347262005-09-03 15:54:28 -070062#ifdef CONFIG_SPARSEMEM_EXTREME
Fabian Frederickbd721ea2016-08-02 14:03:33 -070063static noinline struct mem_section __ref *sparse_index_alloc(int nid)
Bob Picco802f1922005-09-03 15:54:26 -070064{
Dave Hansen28ae55c2005-09-03 15:54:29 -070065 struct mem_section *section = NULL;
66 unsigned long array_size = SECTIONS_PER_ROOT *
67 sizeof(struct mem_section);
Bob Picco802f1922005-09-03 15:54:26 -070068
Michal Hockob95046b2017-09-06 16:20:41 -070069 if (slab_is_available())
70 section = kzalloc_node(array_size, GFP_KERNEL, nid);
71 else
Santosh Shilimkarbb016b82014-01-21 15:50:34 -080072 section = memblock_virt_alloc_node(array_size, 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;
104 struct mem_section* root;
105
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
Gavin Shandb36a462012-07-31 16:46:04 -0700115 VM_BUG_ON(root_nr == NR_SECTION_ROOTS);
116
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;
194 } while ((section_nr < NR_MEM_SECTIONS) &&
195 (section_nr <= __highest_present_section_nr));
196
197 return -1;
198}
199#define for_each_present_section_nr(start, section_nr) \
200 for (section_nr = next_present_section_nr(start-1); \
201 ((section_nr >= 0) && \
202 (section_nr < NR_MEM_SECTIONS) && \
203 (section_nr <= __highest_present_section_nr)); \
204 section_nr = next_present_section_nr(section_nr))
205
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700206/* Record a memory area against a node. */
207void __init memory_present(int nid, unsigned long start, unsigned long end)
208{
209 unsigned long pfn;
Ingo Molnarbead9a32008-04-16 01:40:00 +0200210
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700211 start &= PAGE_SECTION_MASK;
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700212 mminit_validate_memmodel_limits(&start, &end);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700213 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
214 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700215 struct mem_section *ms;
216
217 sparse_index_init(section, nid);
Andy Whitcroft85770ff2007-08-22 14:01:03 -0700218 set_section_nid(section, nid);
Bob Picco802f1922005-09-03 15:54:26 -0700219
220 ms = __nr_to_section(section);
Dave Hansenc4e1be92017-07-06 15:36:44 -0700221 if (!ms->section_mem_map) {
Michal Hocko2d070ea2017-07-06 15:37:56 -0700222 ms->section_mem_map = sparse_encode_early_nid(nid) |
223 SECTION_IS_ONLINE;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700224 section_mark_present(ms);
225 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700226 }
227}
228
229/*
230 * Only used by the i386 NUMA architecures, but relatively
231 * generic code.
232 */
233unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
234 unsigned long end_pfn)
235{
236 unsigned long pfn;
237 unsigned long nr_pages = 0;
238
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700239 mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700240 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
241 if (nid != early_pfn_to_nid(pfn))
242 continue;
243
Andy Whitcroft540557b2007-10-16 01:24:11 -0700244 if (pfn_present(pfn))
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700245 nr_pages += PAGES_PER_SECTION;
246 }
247
248 return nr_pages * sizeof(struct page);
249}
250
251/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700252 * Subtle, we encode the real pfn into the mem_map such that
253 * the identity pfn - section_mem_map will return the actual
254 * physical page frame number.
255 */
256static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
257{
258 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
259}
260
261/*
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700262 * Decode mem_map from the coded memmap
Andy Whitcroft29751f62005-06-23 00:08:00 -0700263 */
Andy Whitcroft29751f62005-06-23 00:08:00 -0700264struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
265{
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700266 /* mask off the extra low bits of information */
267 coded_mem_map &= SECTION_MAP_MASK;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700268 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
269}
270
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700271static int __meminit sparse_init_one_section(struct mem_section *ms,
Mel Gorman5c0e3062007-10-16 01:25:56 -0700272 unsigned long pnum, struct page *mem_map,
273 unsigned long *pageblock_bitmap)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700274{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700275 if (!present_section(ms))
Andy Whitcroft29751f62005-06-23 00:08:00 -0700276 return -EINVAL;
277
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700278 ms->section_mem_map &= ~SECTION_MAP_MASK;
Andy Whitcroft540557b2007-10-16 01:24:11 -0700279 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
280 SECTION_HAS_MEM_MAP;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700281 ms->pageblock_flags = pageblock_bitmap;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700282
283 return 1;
284}
285
Yasunori Goto04753272008-04-28 02:13:31 -0700286unsigned long usemap_size(void)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700287{
Wei Yang60a7a882017-05-03 14:53:51 -0700288 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
Mel Gorman5c0e3062007-10-16 01:25:56 -0700289}
290
291#ifdef CONFIG_MEMORY_HOTPLUG
292static unsigned long *__kmalloc_section_usemap(void)
293{
294 return kmalloc(usemap_size(), GFP_KERNEL);
295}
296#endif /* CONFIG_MEMORY_HOTPLUG */
297
Yasunori Goto48c90682008-07-23 21:28:15 -0700298#ifdef CONFIG_MEMORY_HOTREMOVE
299static unsigned long * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800300sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700301 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700302{
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700303 unsigned long goal, limit;
304 unsigned long *p;
305 int nid;
Yasunori Goto48c90682008-07-23 21:28:15 -0700306 /*
307 * A page may contain usemaps for other sections preventing the
308 * page being freed and making a section unremovable while
Li Zhongc800bcd2014-03-31 16:41:58 +0800309 * other sections referencing the usemap remain active. Similarly,
Yasunori Goto48c90682008-07-23 21:28:15 -0700310 * a pgdat can prevent a section being removed. If section A
311 * contains a pgdat and section B contains the usemap, both
312 * sections become inter-dependent. This allocates usemaps
313 * from the same section as the pgdat where possible to avoid
314 * this problem.
315 */
Yinghai Lu07b4e2b2012-07-11 14:02:51 -0700316 goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700317 limit = goal + (1UL << PA_SECTION_SHIFT);
318 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
319again:
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800320 p = memblock_virt_alloc_try_nid_nopanic(size,
321 SMP_CACHE_BYTES, goal, limit,
322 nid);
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700323 if (!p && limit) {
324 limit = 0;
325 goto again;
326 }
327 return p;
Yasunori Goto48c90682008-07-23 21:28:15 -0700328}
329
330static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
331{
332 unsigned long usemap_snr, pgdat_snr;
333 static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
334 static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
335 struct pglist_data *pgdat = NODE_DATA(nid);
336 int usemap_nid;
337
338 usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
339 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
340 if (usemap_snr == pgdat_snr)
341 return;
342
343 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
344 /* skip redundant message */
345 return;
346
347 old_usemap_snr = usemap_snr;
348 old_pgdat_snr = pgdat_snr;
349
350 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
351 if (usemap_nid != nid) {
Joe Perches11705322016-03-17 14:19:50 -0700352 pr_info("node %d must be removed before remove section %ld\n",
353 nid, usemap_snr);
Yasunori Goto48c90682008-07-23 21:28:15 -0700354 return;
355 }
356 /*
357 * There is a circular dependency.
358 * Some platforms allow un-removable section because they will just
359 * gather other removable sections for dynamic partitioning.
360 * Just notify un-removable section's number here.
361 */
Joe Perches11705322016-03-17 14:19:50 -0700362 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
363 usemap_snr, pgdat_snr, nid);
Yasunori Goto48c90682008-07-23 21:28:15 -0700364}
365#else
366static unsigned long * __init
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800367sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
Johannes Weiner238305b2012-05-29 15:06:36 -0700368 unsigned long size)
Yasunori Goto48c90682008-07-23 21:28:15 -0700369{
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800370 return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
Yasunori Goto48c90682008-07-23 21:28:15 -0700371}
372
373static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
374{
375}
376#endif /* CONFIG_MEMORY_HOTREMOVE */
377
Wanpeng Li18732092013-09-11 14:22:38 -0700378static void __init sparse_early_usemaps_alloc_node(void *data,
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800379 unsigned long pnum_begin,
380 unsigned long pnum_end,
381 unsigned long usemap_count, int nodeid)
Mel Gorman5c0e3062007-10-16 01:25:56 -0700382{
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800383 void *usemap;
384 unsigned long pnum;
Wanpeng Li18732092013-09-11 14:22:38 -0700385 unsigned long **usemap_map = (unsigned long **)data;
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800386 int size = usemap_size();
Mel Gorman5c0e3062007-10-16 01:25:56 -0700387
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800388 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
Johannes Weiner238305b2012-05-29 15:06:36 -0700389 size * usemap_count);
Nishanth Aravamudanf5bf18f2012-03-21 16:34:07 -0700390 if (!usemap) {
Joe Perches11705322016-03-17 14:19:50 -0700391 pr_warn("%s: allocation failed\n", __func__);
Johannes Weiner238305b2012-05-29 15:06:36 -0700392 return;
Yasunori Goto48c90682008-07-23 21:28:15 -0700393 }
394
Nishanth Aravamudanf5bf18f2012-03-21 16:34:07 -0700395 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
396 if (!present_section_nr(pnum))
397 continue;
398 usemap_map[pnum] = usemap;
399 usemap += size;
400 check_usemap_section_nr(nodeid, usemap_map[pnum]);
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800401 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700402}
403
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700404#ifndef CONFIG_SPARSEMEM_VMEMMAP
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700405struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700406{
407 struct page *map;
Yinghai Lue48e67e2010-05-24 14:31:57 -0700408 unsigned long size;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700409
410 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
411 if (map)
412 return map;
413
Yinghai Lue48e67e2010-05-24 14:31:57 -0700414 size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800415 map = memblock_virt_alloc_try_nid(size,
416 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
417 BOOTMEM_ALLOC_ACCESSIBLE, nid);
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700418 return map;
419}
Yinghai Lu9bdac912010-02-10 01:20:22 -0800420void __init sparse_mem_maps_populate_node(struct page **map_map,
421 unsigned long pnum_begin,
422 unsigned long pnum_end,
423 unsigned long map_count, int nodeid)
424{
425 void *map;
426 unsigned long pnum;
427 unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
428
429 map = alloc_remap(nodeid, size * map_count);
430 if (map) {
431 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
432 if (!present_section_nr(pnum))
433 continue;
434 map_map[pnum] = map;
435 map += size;
436 }
437 return;
438 }
439
440 size = PAGE_ALIGN(size);
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800441 map = memblock_virt_alloc_try_nid(size * map_count,
442 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
443 BOOTMEM_ALLOC_ACCESSIBLE, nodeid);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800444 if (map) {
445 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
446 if (!present_section_nr(pnum))
447 continue;
448 map_map[pnum] = map;
449 map += size;
450 }
451 return;
452 }
453
454 /* fallback */
455 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
456 struct mem_section *ms;
457
458 if (!present_section_nr(pnum))
459 continue;
460 map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
461 if (map_map[pnum])
462 continue;
463 ms = __nr_to_section(pnum);
Joe Perches11705322016-03-17 14:19:50 -0700464 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
Joe Perches756a0252016-03-17 14:19:47 -0700465 __func__);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800466 ms->section_mem_map = 0;
467 }
468}
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700469#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
470
Yinghai Lu81d0d952010-02-27 09:29:38 -0800471#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Wanpeng Li18732092013-09-11 14:22:38 -0700472static void __init sparse_early_mem_maps_alloc_node(void *data,
Yinghai Lu9bdac912010-02-10 01:20:22 -0800473 unsigned long pnum_begin,
474 unsigned long pnum_end,
475 unsigned long map_count, int nodeid)
476{
Wanpeng Li18732092013-09-11 14:22:38 -0700477 struct page **map_map = (struct page **)data;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800478 sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
479 map_count, nodeid);
480}
Yinghai Lu81d0d952010-02-27 09:29:38 -0800481#else
Adrian Bunk9e5c6da2008-07-25 19:46:22 -0700482static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700483{
484 struct page *map;
485 struct mem_section *ms = __nr_to_section(pnum);
486 int nid = sparse_early_nid(ms);
487
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700488 map = sparse_mem_map_populate(pnum, nid);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700489 if (map)
490 return map;
491
Joe Perches11705322016-03-17 14:19:50 -0700492 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
Joe Perches756a0252016-03-17 14:19:47 -0700493 __func__);
Bob Picco802f1922005-09-03 15:54:26 -0700494 ms->section_mem_map = 0;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700495 return NULL;
496}
Yinghai Lu9bdac912010-02-10 01:20:22 -0800497#endif
Andy Whitcroft29751f62005-06-23 00:08:00 -0700498
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700499void __weak __meminit vmemmap_populate_print_last(void)
Yinghai Luc2b91e22008-04-12 01:19:24 -0700500{
501}
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800502
Wanpeng Li18732092013-09-11 14:22:38 -0700503/**
504 * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
505 * @map: usemap_map for pageblock flags or mmap_map for vmemmap
Stephen Rothwell193faea2007-06-08 13:46:51 -0700506 */
Wanpeng Li18732092013-09-11 14:22:38 -0700507static void __init alloc_usemap_and_memmap(void (*alloc_func)
508 (void *, unsigned long, unsigned long,
509 unsigned long, int), void *data)
Stephen Rothwell193faea2007-06-08 13:46:51 -0700510{
511 unsigned long pnum;
Wanpeng Li18732092013-09-11 14:22:38 -0700512 unsigned long map_count;
Yinghai Lua4322e1b2010-02-10 01:20:21 -0800513 int nodeid_begin = 0;
514 unsigned long pnum_begin = 0;
Yinghai Lu9bdac912010-02-10 01:20:22 -0800515
Dave Hansenc4e1be92017-07-06 15:36:44 -0700516 for_each_present_section_nr(0, pnum) {
Yinghai Lu9bdac912010-02-10 01:20:22 -0800517 struct mem_section *ms;
518
Yinghai Lu9bdac912010-02-10 01:20:22 -0800519 ms = __nr_to_section(pnum);
520 nodeid_begin = sparse_early_nid(ms);
521 pnum_begin = pnum;
522 break;
523 }
524 map_count = 1;
Dave Hansenc4e1be92017-07-06 15:36:44 -0700525 for_each_present_section_nr(pnum_begin + 1, pnum) {
Yinghai Lu9bdac912010-02-10 01:20:22 -0800526 struct mem_section *ms;
527 int nodeid;
528
Yinghai Lu9bdac912010-02-10 01:20:22 -0800529 ms = __nr_to_section(pnum);
530 nodeid = sparse_early_nid(ms);
531 if (nodeid == nodeid_begin) {
532 map_count++;
533 continue;
534 }
535 /* ok, we need to take cake of from pnum_begin to pnum - 1*/
Wanpeng Li18732092013-09-11 14:22:38 -0700536 alloc_func(data, pnum_begin, pnum,
537 map_count, nodeid_begin);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800538 /* new start, update count etc*/
539 nodeid_begin = nodeid;
540 pnum_begin = pnum;
541 map_count = 1;
542 }
543 /* ok, last chunk */
Wanpeng Li18732092013-09-11 14:22:38 -0700544 alloc_func(data, pnum_begin, NR_MEM_SECTIONS,
545 map_count, nodeid_begin);
546}
547
548/*
549 * Allocate the accumulated non-linear sections, allocate a mem_map
550 * for each and record the physical to section mapping.
551 */
552void __init sparse_init(void)
553{
554 unsigned long pnum;
555 struct page *map;
556 unsigned long *usemap;
557 unsigned long **usemap_map;
558 int size;
559#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
560 int size2;
561 struct page **map_map;
562#endif
563
564 /* see include/linux/mmzone.h 'struct mem_section' definition */
565 BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
566
567 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
568 set_pageblock_order();
569
570 /*
571 * map is using big page (aka 2M in x86 64 bit)
572 * usemap is less one page (aka 24 bytes)
573 * so alloc 2M (with 2M align) and 24 bytes in turn will
574 * make next 2M slip to one more 2M later.
575 * then in big system, the memory will have a lot of holes...
576 * here try to allocate 2M pages continuously.
577 *
578 * powerpc need to call sparse_init_one_section right after each
579 * sparse_early_mem_map_alloc, so allocate usemap_map at first.
580 */
581 size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800582 usemap_map = memblock_virt_alloc(size, 0);
Wanpeng Li18732092013-09-11 14:22:38 -0700583 if (!usemap_map)
584 panic("can not allocate usemap_map\n");
585 alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
586 (void *)usemap_map);
587
588#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
589 size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800590 map_map = memblock_virt_alloc(size2, 0);
Wanpeng Li18732092013-09-11 14:22:38 -0700591 if (!map_map)
592 panic("can not allocate map_map\n");
593 alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
594 (void *)map_map);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800595#endif
596
Dave Hansenc4e1be92017-07-06 15:36:44 -0700597 for_each_present_section_nr(0, pnum) {
Yinghai Lue123dd32008-04-13 11:51:06 -0700598 usemap = usemap_map[pnum];
599 if (!usemap)
600 continue;
Stephen Rothwell193faea2007-06-08 13:46:51 -0700601
Yinghai Lu9bdac912010-02-10 01:20:22 -0800602#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
603 map = map_map[pnum];
604#else
Stephen Rothwell193faea2007-06-08 13:46:51 -0700605 map = sparse_early_mem_map_alloc(pnum);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800606#endif
Stephen Rothwell193faea2007-06-08 13:46:51 -0700607 if (!map)
608 continue;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700609
Mel Gorman5c0e3062007-10-16 01:25:56 -0700610 sparse_init_one_section(__nr_to_section(pnum), pnum, map,
611 usemap);
Stephen Rothwell193faea2007-06-08 13:46:51 -0700612 }
Yinghai Lue123dd32008-04-13 11:51:06 -0700613
Yinghai Luc2b91e22008-04-12 01:19:24 -0700614 vmemmap_populate_print_last();
615
Yinghai Lu9bdac912010-02-10 01:20:22 -0800616#ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800617 memblock_free_early(__pa(map_map), size2);
Yinghai Lu9bdac912010-02-10 01:20:22 -0800618#endif
Santosh Shilimkarbb016b82014-01-21 15:50:34 -0800619 memblock_free_early(__pa(usemap_map), size);
Stephen Rothwell193faea2007-06-08 13:46:51 -0700620}
621
622#ifdef CONFIG_MEMORY_HOTPLUG
Michal Hocko2d070ea2017-07-06 15:37:56 -0700623
624/* Mark all memory sections within the pfn range as online */
625void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
626{
627 unsigned long pfn;
628
629 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
Michal Hockob4ccec42017-09-08 16:13:15 -0700630 unsigned long section_nr = pfn_to_section_nr(pfn);
Michal Hocko2d070ea2017-07-06 15:37:56 -0700631 struct mem_section *ms;
632
633 /* onlining code should never touch invalid ranges */
634 if (WARN_ON(!valid_section_nr(section_nr)))
635 continue;
636
637 ms = __nr_to_section(section_nr);
638 ms->section_mem_map |= SECTION_IS_ONLINE;
639 }
640}
641
642#ifdef CONFIG_MEMORY_HOTREMOVE
643/* Mark all memory sections within the pfn range as online */
644void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
645{
646 unsigned long pfn;
647
648 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
649 unsigned long section_nr = pfn_to_section_nr(start_pfn);
650 struct mem_section *ms;
651
652 /*
653 * TODO this needs some double checking. Offlining code makes
654 * sure to check pfn_valid but those checks might be just bogus
655 */
656 if (WARN_ON(!valid_section_nr(section_nr)))
657 continue;
658
659 ms = __nr_to_section(section_nr);
660 ms->section_mem_map &= ~SECTION_IS_ONLINE;
661 }
662}
663#endif
664
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700665#ifdef CONFIG_SPARSEMEM_VMEMMAP
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800666static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700667{
668 /* This will make the necessary allocations eventually. */
669 return sparse_mem_map_populate(pnum, nid);
670}
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800671static void __kfree_section_memmap(struct page *memmap)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700672{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700673 unsigned long start = (unsigned long)memmap;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800674 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700675
676 vmemmap_free(start, end);
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700677}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700678#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800679static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700680{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700681 unsigned long start = (unsigned long)memmap;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800682 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
Johannes Weiner0aad8182013-04-29 15:07:50 -0700683
684 vmemmap_free(start, end);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700685}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700686#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700687#else
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800688static struct page *__kmalloc_section_memmap(void)
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700689{
690 struct page *page, *ret;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800691 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700692
Yasunori Gotof2d0aa52006-10-28 10:38:32 -0700693 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700694 if (page)
695 goto got_map_page;
696
697 ret = vmalloc(memmap_size);
698 if (ret)
699 goto got_map_ptr;
700
701 return NULL;
702got_map_page:
703 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
704got_map_ptr:
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700705
706 return ret;
707}
708
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800709static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700710{
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800711 return __kmalloc_section_memmap();
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700712}
713
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800714static void __kfree_section_memmap(struct page *memmap)
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700715{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800716 if (is_vmalloc_addr(memmap))
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700717 vfree(memmap);
718 else
719 free_pages((unsigned long)memmap,
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800720 get_order(sizeof(struct page) * PAGES_PER_SECTION));
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700721}
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700722
David Rientjes4edd7ce2013-04-29 15:08:22 -0700723#ifdef CONFIG_MEMORY_HOTREMOVE
Zhang Yanfei81556b02013-11-12 15:07:43 -0800724static void free_map_bootmem(struct page *memmap)
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700725{
726 unsigned long maps_section_nr, removing_section_nr, i;
Zhang Yanfei81556b02013-11-12 15:07:43 -0800727 unsigned long magic, nr_pages;
Jianguo Wuae64ffc2012-11-29 13:54:21 -0800728 struct page *page = virt_to_page(memmap);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700729
Zhang Yanfei81556b02013-11-12 15:07:43 -0800730 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
731 >> PAGE_SHIFT;
732
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700733 for (i = 0; i < nr_pages; i++, page++) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800734 magic = (unsigned long) page->freelist;
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700735
736 BUG_ON(magic == NODE_INFO);
737
738 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
Yasuaki Ishimatsu857e5222017-02-22 15:45:10 -0800739 removing_section_nr = page_private(page);
Yasunori Goto0c0a4a52008-04-28 02:13:34 -0700740
741 /*
742 * When this function is called, the removing section is
743 * logical offlined state. This means all pages are isolated
744 * from page allocator. If removing section's memmap is placed
745 * on the same section, it must not be freed.
746 * If it is freed, page allocator may allocate it which will
747 * be removed physically soon.
748 */
749 if (maps_section_nr != removing_section_nr)
750 put_page_bootmem(page);
751 }
752}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700753#endif /* CONFIG_MEMORY_HOTREMOVE */
Yasunori Goto98f3cfc2007-10-16 01:26:14 -0700754#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700755
Andy Whitcroft29751f62005-06-23 00:08:00 -0700756/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700757 * returns the number of sections whose mem_maps were properly
758 * set. If this is <=0, then that means that the passed-in
759 * map was not consumed and must be freed.
760 */
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700761int __meminit sparse_add_one_section(struct pglist_data *pgdat, unsigned long start_pfn)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700762{
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700763 unsigned long section_nr = pfn_to_section_nr(start_pfn);
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700764 struct mem_section *ms;
765 struct page *memmap;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700766 unsigned long *usemap;
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700767 unsigned long flags;
768 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700769
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700770 /*
771 * no locking for this, because it does its own
772 * plus, it does a kmalloc
773 */
WANG Congbbd06822007-12-17 16:19:59 -0800774 ret = sparse_index_init(section_nr, pgdat->node_id);
775 if (ret < 0 && ret != -EEXIST)
776 return ret;
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800777 memmap = kmalloc_section_memmap(section_nr, pgdat->node_id);
WANG Congbbd06822007-12-17 16:19:59 -0800778 if (!memmap)
779 return -ENOMEM;
Mel Gorman5c0e3062007-10-16 01:25:56 -0700780 usemap = __kmalloc_section_usemap();
WANG Congbbd06822007-12-17 16:19:59 -0800781 if (!usemap) {
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800782 __kfree_section_memmap(memmap);
WANG Congbbd06822007-12-17 16:19:59 -0800783 return -ENOMEM;
784 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700785
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700786 pgdat_resize_lock(pgdat, &flags);
787
788 ms = __pfn_to_section(start_pfn);
789 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
790 ret = -EEXIST;
791 goto out;
792 }
Mel Gorman5c0e3062007-10-16 01:25:56 -0700793
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800794 memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION);
Wen Congyang3ac19f82012-12-11 16:00:59 -0800795
Dave Hansenc4e1be92017-07-06 15:36:44 -0700796 section_mark_present(ms);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700797
Mel Gorman5c0e3062007-10-16 01:25:56 -0700798 ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700799
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700800out:
801 pgdat_resize_unlock(pgdat, &flags);
WANG Congbbd06822007-12-17 16:19:59 -0800802 if (ret <= 0) {
803 kfree(usemap);
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800804 __kfree_section_memmap(memmap);
WANG Congbbd06822007-12-17 16:19:59 -0800805 }
Dave Hansen0b0acbec2005-10-29 18:16:55 -0700806 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700807}
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700808
Zhang Yanfeif3deb682013-07-08 16:00:10 -0700809#ifdef CONFIG_MEMORY_HOTREMOVE
Wen Congyang95a47742012-12-11 16:00:47 -0800810#ifdef CONFIG_MEMORY_FAILURE
811static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
812{
813 int i;
814
815 if (!memmap)
816 return;
817
Dan Williams4b94ffd2016-01-15 16:56:22 -0800818 for (i = 0; i < nr_pages; i++) {
Wen Congyang95a47742012-12-11 16:00:47 -0800819 if (PageHWPoison(&memmap[i])) {
Xishi Qiu293c07e2013-02-22 16:34:02 -0800820 atomic_long_sub(1, &num_poisoned_pages);
Wen Congyang95a47742012-12-11 16:00:47 -0800821 ClearPageHWPoison(&memmap[i]);
822 }
823 }
824}
825#else
826static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
827{
828}
829#endif
830
David Rientjes4edd7ce2013-04-29 15:08:22 -0700831static void free_section_usemap(struct page *memmap, unsigned long *usemap)
832{
833 struct page *usemap_page;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700834
835 if (!usemap)
836 return;
837
838 usemap_page = virt_to_page(usemap);
839 /*
840 * Check to see if allocation came from hot-plug-add
841 */
842 if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
843 kfree(usemap);
844 if (memmap)
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800845 __kfree_section_memmap(memmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700846 return;
847 }
848
849 /*
850 * The usemap came from bootmem. This is packed with other usemaps
851 * on the section which has pgdat at boot time. Just keep it as is now.
852 */
853
Zhang Yanfei81556b02013-11-12 15:07:43 -0800854 if (memmap)
855 free_map_bootmem(memmap);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700856}
857
Dan Williams4b94ffd2016-01-15 16:56:22 -0800858void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
859 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700860{
861 struct page *memmap = NULL;
Tang Chencd099682013-02-22 16:33:02 -0800862 unsigned long *usemap = NULL, flags;
863 struct pglist_data *pgdat = zone->zone_pgdat;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700864
Tang Chencd099682013-02-22 16:33:02 -0800865 pgdat_resize_lock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700866 if (ms->section_mem_map) {
867 usemap = ms->pageblock_flags;
868 memmap = sparse_decode_mem_map(ms->section_mem_map,
869 __section_nr(ms));
870 ms->section_mem_map = 0;
871 ms->pageblock_flags = NULL;
872 }
Tang Chencd099682013-02-22 16:33:02 -0800873 pgdat_resize_unlock(pgdat, &flags);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700874
Dan Williams4b94ffd2016-01-15 16:56:22 -0800875 clear_hwpoisoned_pages(memmap + map_offset,
876 PAGES_PER_SECTION - map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700877 free_section_usemap(memmap, usemap);
878}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700879#endif /* CONFIG_MEMORY_HOTREMOVE */
880#endif /* CONFIG_MEMORY_HOTPLUG */