]> asedeno.scripts.mit.edu Git - linux.git/blob - mm/nobootmem.c
mm: nobootmem: remove bootmem allocation APIs
[linux.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 /**
37  * free_bootmem_late - free bootmem pages directly to page allocator
38  * @addr: starting address of the range
39  * @size: size of the range in bytes
40  *
41  * This is only useful when the bootmem allocator has already been torn
42  * down, but we are still initializing the system.  Pages are given directly
43  * to the page allocator, no bootmem metadata is updated because it is gone.
44  */
45 void __init free_bootmem_late(unsigned long addr, unsigned long size)
46 {
47         unsigned long cursor, end;
48
49         kmemleak_free_part_phys(addr, size);
50
51         cursor = PFN_UP(addr);
52         end = PFN_DOWN(addr + size);
53
54         for (; cursor < end; cursor++) {
55                 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
56                 totalram_pages++;
57         }
58 }
59
60 static void __init __free_pages_memory(unsigned long start, unsigned long end)
61 {
62         int order;
63
64         while (start < end) {
65                 order = min(MAX_ORDER - 1UL, __ffs(start));
66
67                 while (start + (1UL << order) > end)
68                         order--;
69
70                 __free_pages_bootmem(pfn_to_page(start), start, order);
71
72                 start += (1UL << order);
73         }
74 }
75
76 static unsigned long __init __free_memory_core(phys_addr_t start,
77                                  phys_addr_t end)
78 {
79         unsigned long start_pfn = PFN_UP(start);
80         unsigned long end_pfn = min_t(unsigned long,
81                                       PFN_DOWN(end), max_low_pfn);
82
83         if (start_pfn >= end_pfn)
84                 return 0;
85
86         __free_pages_memory(start_pfn, end_pfn);
87
88         return end_pfn - start_pfn;
89 }
90
91 static unsigned long __init free_low_memory_core_early(void)
92 {
93         unsigned long count = 0;
94         phys_addr_t start, end;
95         u64 i;
96
97         memblock_clear_hotplug(0, -1);
98
99         for_each_reserved_mem_region(i, &start, &end)
100                 reserve_bootmem_region(start, end);
101
102         /*
103          * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
104          *  because in some case like Node0 doesn't have RAM installed
105          *  low ram will be on Node1
106          */
107         for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
108                                 NULL)
109                 count += __free_memory_core(start, end);
110
111         return count;
112 }
113
114 static int reset_managed_pages_done __initdata;
115
116 void reset_node_managed_pages(pg_data_t *pgdat)
117 {
118         struct zone *z;
119
120         for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
121                 z->managed_pages = 0;
122 }
123
124 void __init reset_all_zones_managed_pages(void)
125 {
126         struct pglist_data *pgdat;
127
128         if (reset_managed_pages_done)
129                 return;
130
131         for_each_online_pgdat(pgdat)
132                 reset_node_managed_pages(pgdat);
133
134         reset_managed_pages_done = 1;
135 }
136
137 /**
138  * free_all_bootmem - release free pages to the buddy allocator
139  *
140  * Return: the number of pages actually released.
141  */
142 unsigned long __init free_all_bootmem(void)
143 {
144         unsigned long pages;
145
146         reset_all_zones_managed_pages();
147
148         pages = free_low_memory_core_early();
149         totalram_pages += pages;
150
151         return pages;
152 }
153
154 /**
155  * free_bootmem_node - mark a page range as usable
156  * @pgdat: node the range resides on
157  * @physaddr: starting physical address of the range
158  * @size: size of the range in bytes
159  *
160  * Partial pages will be considered reserved and left as they are.
161  *
162  * The range must reside completely on the specified node.
163  */
164 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
165                               unsigned long size)
166 {
167         memblock_free(physaddr, size);
168 }
169
170 /**
171  * free_bootmem - mark a page range as usable
172  * @addr: starting physical address of the range
173  * @size: size of the range in bytes
174  *
175  * Partial pages will be considered reserved and left as they are.
176  *
177  * The range must be contiguous but may span node boundaries.
178  */
179 void __init free_bootmem(unsigned long addr, unsigned long size)
180 {
181         memblock_free(addr, size);
182 }