d4d0cd4740874d40227e8fa9b32266a3321284a2
[linux-2.6-block.git] / mm / nobootmem.c
1 // SPDX-License-Identifier: GPL-2.0
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>
15 #include <linux/export.h>
16 #include <linux/kmemleak.h>
17 #include <linux/range.h>
18 #include <linux/memblock.h>
19 #include <linux/bootmem.h>
20
21 #include <asm/bug.h>
22 #include <asm/io.h>
23
24 #include "internal.h"
25
26 #ifndef CONFIG_NEED_MULTIPLE_NODES
27 struct pglist_data __refdata contig_page_data;
28 EXPORT_SYMBOL(contig_page_data);
29 #endif
30
31 unsigned long max_low_pfn;
32 unsigned long min_low_pfn;
33 unsigned long max_pfn;
34 unsigned long long max_possible_pfn;
35
36 static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
37                                         u64 goal, u64 limit)
38 {
39         void *ptr;
40         u64 addr;
41         enum memblock_flags flags = choose_memblock_flags();
42
43         if (limit > memblock.current_limit)
44                 limit = memblock.current_limit;
45
46 again:
47         addr = memblock_find_in_range_node(size, align, goal, limit, nid,
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         }
55         if (!addr)
56                 return NULL;
57
58         if (memblock_reserve(addr, size))
59                 return NULL;
60
61         ptr = phys_to_virt(addr);
62         memset(ptr, 0, size);
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
71 /**
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  */
80 void __init free_bootmem_late(unsigned long addr, unsigned long size)
81 {
82         unsigned long cursor, end;
83
84         kmemleak_free_part_phys(addr, size);
85
86         cursor = PFN_UP(addr);
87         end = PFN_DOWN(addr + size);
88
89         for (; cursor < end; cursor++) {
90                 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
91                 totalram_pages++;
92         }
93 }
94
95 static void __init __free_pages_memory(unsigned long start, unsigned long end)
96 {
97         int order;
98
99         while (start < end) {
100                 order = min(MAX_ORDER - 1UL, __ffs(start));
101
102                 while (start + (1UL << order) > end)
103                         order--;
104
105                 __free_pages_bootmem(pfn_to_page(start), start, order);
106
107                 start += (1UL << order);
108         }
109 }
110
111 static 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
118         if (start_pfn >= end_pfn)
119                 return 0;
120
121         __free_pages_memory(start_pfn, end_pfn);
122
123         return end_pfn - start_pfn;
124 }
125
126 static unsigned long __init free_low_memory_core_early(void)
127 {
128         unsigned long count = 0;
129         phys_addr_t start, end;
130         u64 i;
131
132         memblock_clear_hotplug(0, -1);
133
134         for_each_reserved_mem_region(i, &start, &end)
135                 reserve_bootmem_region(start, end);
136
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          */
142         for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
143                                 NULL)
144                 count += __free_memory_core(start, end);
145
146         return count;
147 }
148
149 static int reset_managed_pages_done __initdata;
150
151 void reset_node_managed_pages(pg_data_t *pgdat)
152 {
153         struct zone *z;
154
155         for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
156                 z->managed_pages = 0;
157 }
158
159 void __init reset_all_zones_managed_pages(void)
160 {
161         struct pglist_data *pgdat;
162
163         if (reset_managed_pages_done)
164                 return;
165
166         for_each_online_pgdat(pgdat)
167                 reset_node_managed_pages(pgdat);
168
169         reset_managed_pages_done = 1;
170 }
171
172 /**
173  * free_all_bootmem - release free pages to the buddy allocator
174  *
175  * Return: the number of pages actually released.
176  */
177 unsigned long __init free_all_bootmem(void)
178 {
179         unsigned long pages;
180
181         reset_all_zones_managed_pages();
182
183         pages = free_low_memory_core_early();
184         totalram_pages += pages;
185
186         return pages;
187 }
188
189 /**
190  * free_bootmem_node - mark a page range as usable
191  * @pgdat: node the range resides on
192  * @physaddr: starting physical address of the range
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  */
199 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
200                               unsigned long size)
201 {
202         memblock_free(physaddr, size);
203 }
204
205 /**
206  * free_bootmem - mark a page range as usable
207  * @addr: starting physical address of the range
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  */
214 void __init free_bootmem(unsigned long addr, unsigned long size)
215 {
216         memblock_free(addr, size);
217 }
218
219 static 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
229 restart:
230
231         ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
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  *
255  * Return: address of the allocated region or %NULL on failure.
256  */
257 void * __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
265 static 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          */
275         pr_alert("bootmem alloc of %lu bytes failed!\n", size);
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.
292  *
293  * Return: address of the allocated region.
294  */
295 void * __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
303 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
304                                                    unsigned long size,
305                                                    unsigned long align,
306                                                    unsigned long goal,
307                                                    unsigned long limit)
308 {
309         void *ptr;
310
311 again:
312         ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
313                                         goal, limit);
314         if (ptr)
315                 return ptr;
316
317         ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
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
330 void * __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
339 static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
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
349         pr_alert("bootmem alloc of %lu bytes failed!\n", size);
350         panic("Out of memory");
351         return NULL;
352 }
353
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.
368  *
369  * Return: address of the allocated region.
370  */
371 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
372                                    unsigned long align, unsigned long goal)
373 {
374         if (WARN_ON_ONCE(slab_is_available()))
375                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
376
377         return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
378 }
379
380 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
381                                    unsigned long align, unsigned long goal)
382 {
383         return __alloc_bootmem_node(pgdat, size, align, goal);
384 }
385
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.
399  *
400  * Return: address of the allocated region.
401  */
402 void * __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 }
407
408 void * __init __alloc_bootmem_low_nopanic(unsigned long size,
409                                           unsigned long align,
410                                           unsigned long goal)
411 {
412         return ___alloc_bootmem_nopanic(size, align, goal,
413                                         ARCH_LOW_ADDRESS_LIMIT);
414 }
415
416 /**
417  * __alloc_bootmem_low_node - allocate low boot memory from a specific node
418  * @pgdat: node to allocate from
419  * @size: size of the request in bytes
420  * @align: alignment of the region
421  * @goal: preferred starting address of the region
422  *
423  * The goal is dropped if it can not be satisfied and the allocation will
424  * fall back to memory below @goal.
425  *
426  * Allocation may fall back to any node in the system if the specified node
427  * can not hold the requested memory.
428  *
429  * The function panics if the request can not be satisfied.
430  *
431  * Return: address of the allocated region.
432  */
433 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
434                                        unsigned long align, unsigned long goal)
435 {
436         if (WARN_ON_ONCE(slab_is_available()))
437                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
438
439         return ___alloc_bootmem_node(pgdat, size, align, goal,
440                                      ARCH_LOW_ADDRESS_LIMIT);
441 }