mm: nobootmem: remove dead code
[linux-2.6-block.git] / mm / nobootmem.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
09325873
YL
2/*
3 * bootmem - A boot-time physical memory allocator and configurator
4 *
5 * Copyright (C) 1999 Ingo Molnar
6 * 1999 Kanoj Sarcar, SGI
7 * 2008 Johannes Weiner
8 *
9 * Access to this subsystem has to be serialized externally (which is true
10 * for the boot process anyway).
11 */
12#include <linux/init.h>
13#include <linux/pfn.h>
14#include <linux/slab.h>
b95f1b31 15#include <linux/export.h>
09325873
YL
16#include <linux/kmemleak.h>
17#include <linux/range.h>
18#include <linux/memblock.h>
2382705f 19#include <linux/bootmem.h>
09325873
YL
20
21#include <asm/bug.h>
22#include <asm/io.h>
09325873
YL
23
24#include "internal.h"
25
e782ab42
YL
26#ifndef CONFIG_NEED_MULTIPLE_NODES
27struct pglist_data __refdata contig_page_data;
28EXPORT_SYMBOL(contig_page_data);
29#endif
30
09325873
YL
31unsigned long max_low_pfn;
32unsigned long min_low_pfn;
33unsigned long max_pfn;
8dd33030 34unsigned long long max_possible_pfn;
09325873 35
8bc1f91e
YL
36static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
37 u64 goal, u64 limit)
38{
39 void *ptr;
40 u64 addr;
e1720fee 41 enum memblock_flags flags = choose_memblock_flags();
8bc1f91e
YL
42
43 if (limit > memblock.current_limit)
44 limit = memblock.current_limit;
45
a3f5bafc 46again:
fc6daaf9 47 addr = memblock_find_in_range_node(size, align, goal, limit, nid,
a3f5bafc
TL
48 flags);
49 if (!addr && (flags & MEMBLOCK_MIRROR)) {
50 flags &= ~MEMBLOCK_MIRROR;
51 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
52 &size);
53 goto again;
54 }
1f5026a7 55 if (!addr)
8bc1f91e
YL
56 return NULL;
57
87379ec8
PH
58 if (memblock_reserve(addr, size))
59 return NULL;
60
8bc1f91e
YL
61 ptr = phys_to_virt(addr);
62 memset(ptr, 0, size);
8bc1f91e
YL
63 /*
64 * The min_count is set to 0 so that bootmem allocated blocks
65 * are never reported as leaks.
66 */
67 kmemleak_alloc(ptr, size, 0, 0);
68 return ptr;
69}
70
8108ad51 71/**
09325873
YL
72 * free_bootmem_late - free bootmem pages directly to page allocator
73 * @addr: starting address of the range
74 * @size: size of the range in bytes
75 *
76 * This is only useful when the bootmem allocator has already been torn
77 * down, but we are still initializing the system. Pages are given directly
78 * to the page allocator, no bootmem metadata is updated because it is gone.
79 */
80void __init free_bootmem_late(unsigned long addr, unsigned long size)
81{
82 unsigned long cursor, end;
83
9099daed 84 kmemleak_free_part_phys(addr, size);
09325873
YL
85
86 cursor = PFN_UP(addr);
87 end = PFN_DOWN(addr + size);
88
89 for (; cursor < end; cursor++) {
d70ddd7a 90 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
09325873
YL
91 totalram_pages++;
92 }
93}
94
95static void __init __free_pages_memory(unsigned long start, unsigned long end)
96{
309d0b39 97 int order;
09325873 98
309d0b39
RH
99 while (start < end) {
100 order = min(MAX_ORDER - 1UL, __ffs(start));
09325873 101
309d0b39
RH
102 while (start + (1UL << order) > end)
103 order--;
09325873 104
d70ddd7a 105 __free_pages_bootmem(pfn_to_page(start), start, order);
09325873 106
309d0b39
RH
107 start += (1UL << order);
108 }
09325873
YL
109}
110
29f67386
YL
111static unsigned long __init __free_memory_core(phys_addr_t start,
112 phys_addr_t end)
113{
114 unsigned long start_pfn = PFN_UP(start);
115 unsigned long end_pfn = min_t(unsigned long,
116 PFN_DOWN(end), max_low_pfn);
117
172ffeb9 118 if (start_pfn >= end_pfn)
29f67386
YL
119 return 0;
120
121 __free_pages_memory(start_pfn, end_pfn);
122
123 return end_pfn - start_pfn;
124}
125
b4def350 126static unsigned long __init free_low_memory_core_early(void)
09325873 127{
09325873 128 unsigned long count = 0;
354f17e1 129 phys_addr_t start, end;
8a9ca34c
TH
130 u64 i;
131
0a313a99
XQ
132 memblock_clear_hotplug(0, -1);
133
92923ca3
NZ
134 for_each_reserved_mem_region(i, &start, &end)
135 reserve_bootmem_region(start, end);
136
914a0516
WG
137 /*
138 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
139 * because in some case like Node0 doesn't have RAM installed
140 * low ram will be on Node1
141 */
fc6daaf9
TL
142 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
143 NULL)
29f67386
YL
144 count += __free_memory_core(start, end);
145
09325873
YL
146 return count;
147}
148
7b4b2a0d
JL
149static int reset_managed_pages_done __initdata;
150
f784a3f1 151void reset_node_managed_pages(pg_data_t *pgdat)
9feedc9d
JL
152{
153 struct zone *z;
154
9feedc9d 155 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
7b4b2a0d
JL
156 z->managed_pages = 0;
157}
158
159void __init reset_all_zones_managed_pages(void)
160{
161 struct pglist_data *pgdat;
162
f784a3f1
TC
163 if (reset_managed_pages_done)
164 return;
165
7b4b2a0d
JL
166 for_each_online_pgdat(pgdat)
167 reset_node_managed_pages(pgdat);
f784a3f1 168
7b4b2a0d 169 reset_managed_pages_done = 1;
9feedc9d
JL
170}
171
09325873
YL
172/**
173 * free_all_bootmem - release free pages to the buddy allocator
174 *
8108ad51 175 * Return: the number of pages actually released.
09325873
YL
176 */
177unsigned long __init free_all_bootmem(void)
178{
0c988534
JL
179 unsigned long pages;
180
7b4b2a0d 181 reset_all_zones_managed_pages();
9feedc9d 182
0c988534
JL
183 pages = free_low_memory_core_early();
184 totalram_pages += pages;
185
186 return pages;
09325873
YL
187}
188
189/**
190 * free_bootmem_node - mark a page range as usable
191 * @pgdat: node the range resides on
8108ad51 192 * @physaddr: starting physical address of the range
09325873
YL
193 * @size: size of the range in bytes
194 *
195 * Partial pages will be considered reserved and left as they are.
196 *
197 * The range must reside completely on the specified node.
198 */
199void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
200 unsigned long size)
201{
24aa0788 202 memblock_free(physaddr, size);
09325873
YL
203}
204
205/**
206 * free_bootmem - mark a page range as usable
8108ad51 207 * @addr: starting physical address of the range
09325873
YL
208 * @size: size of the range in bytes
209 *
210 * Partial pages will be considered reserved and left as they are.
211 *
212 * The range must be contiguous but may span node boundaries.
213 */
214void __init free_bootmem(unsigned long addr, unsigned long size)
215{
24aa0788 216 memblock_free(addr, size);
09325873
YL
217}
218
219static void * __init ___alloc_bootmem_nopanic(unsigned long size,
220 unsigned long align,
221 unsigned long goal,
222 unsigned long limit)
223{
224 void *ptr;
225
226 if (WARN_ON_ONCE(slab_is_available()))
227 return kzalloc(size, GFP_NOWAIT);
228
229restart:
230
b1154233 231 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
09325873
YL
232
233 if (ptr)
234 return ptr;
235
236 if (goal != 0) {
237 goal = 0;
238 goto restart;
239 }
240
241 return NULL;
242}
243
244/**
245 * __alloc_bootmem_nopanic - allocate boot memory without panicking
246 * @size: size of the request in bytes
247 * @align: alignment of the region
248 * @goal: preferred starting address of the region
249 *
250 * The goal is dropped if it can not be satisfied and the allocation will
251 * fall back to memory below @goal.
252 *
253 * Allocation may happen on any node in the system.
254 *
8108ad51 255 * Return: address of the allocated region or %NULL on failure.
09325873
YL
256 */
257void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
258 unsigned long goal)
259{
260 unsigned long limit = -1UL;
261
262 return ___alloc_bootmem_nopanic(size, align, goal, limit);
263}
264
265static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
266 unsigned long goal, unsigned long limit)
267{
268 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
269
270 if (mem)
271 return mem;
272 /*
273 * Whoops, we cannot satisfy the allocation request.
274 */
1170532b 275 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
09325873
YL
276 panic("Out of memory");
277 return NULL;
278}
279
280/**
281 * __alloc_bootmem - allocate boot memory
282 * @size: size of the request in bytes
283 * @align: alignment of the region
284 * @goal: preferred starting address of the region
285 *
286 * The goal is dropped if it can not be satisfied and the allocation will
287 * fall back to memory below @goal.
288 *
289 * Allocation may happen on any node in the system.
290 *
291 * The function panics if the request can not be satisfied.
8108ad51
MR
292 *
293 * Return: address of the allocated region.
09325873
YL
294 */
295void * __init __alloc_bootmem(unsigned long size, unsigned long align,
296 unsigned long goal)
297{
298 unsigned long limit = -1UL;
299
300 return ___alloc_bootmem(size, align, goal, limit);
301}
302
99ab7b19 303void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
ba539868
JW
304 unsigned long size,
305 unsigned long align,
306 unsigned long goal,
307 unsigned long limit)
308{
309 void *ptr;
310
311again:
312 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
313 goal, limit);
314 if (ptr)
315 return ptr;
316
b1154233 317 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
ba539868
JW
318 goal, limit);
319 if (ptr)
320 return ptr;
321
322 if (goal) {
323 goal = 0;
324 goto again;
325 }
326
327 return NULL;
328}
329
330void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
331 unsigned long align, unsigned long goal)
332{
333 if (WARN_ON_ONCE(slab_is_available()))
334 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
335
336 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
337}
338
de498507 339static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
ba539868
JW
340 unsigned long align, unsigned long goal,
341 unsigned long limit)
342{
343 void *ptr;
344
345 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
346 if (ptr)
347 return ptr;
348
1170532b 349 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
ba539868
JW
350 panic("Out of memory");
351 return NULL;
352}
353
09325873
YL
354/**
355 * __alloc_bootmem_node - allocate boot memory from a specific node
356 * @pgdat: node to allocate from
357 * @size: size of the request in bytes
358 * @align: alignment of the region
359 * @goal: preferred starting address of the region
360 *
361 * The goal is dropped if it can not be satisfied and the allocation will
362 * fall back to memory below @goal.
363 *
364 * Allocation may fall back to any node in the system if the specified node
365 * can not hold the requested memory.
366 *
367 * The function panics if the request can not be satisfied.
8108ad51
MR
368 *
369 * Return: address of the allocated region.
09325873
YL
370 */
371void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
372 unsigned long align, unsigned long goal)
373{
09325873
YL
374 if (WARN_ON_ONCE(slab_is_available()))
375 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
376
ba539868 377 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
09325873
YL
378}
379
380void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
381 unsigned long align, unsigned long goal)
382{
09325873 383 return __alloc_bootmem_node(pgdat, size, align, goal);
09325873
YL
384}
385
09325873
YL
386
387/**
388 * __alloc_bootmem_low - allocate low boot memory
389 * @size: size of the request in bytes
390 * @align: alignment of the region
391 * @goal: preferred starting address of the region
392 *
393 * The goal is dropped if it can not be satisfied and the allocation will
394 * fall back to memory below @goal.
395 *
396 * Allocation may happen on any node in the system.
397 *
398 * The function panics if the request can not be satisfied.
8108ad51
MR
399 *
400 * Return: address of the allocated region.
09325873
YL
401 */
402void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
403 unsigned long goal)
404{
405 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
406}