]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - mm/bootmem.c
4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
7 * simple boot-time physical memory area allocator and
8 * free memory collector. It's used to deal with reserved
9 * system memory and memory holes as well.
11 #include <linux/init.h>
12 #include <linux/pfn.h>
13 #include <linux/bootmem.h>
14 #include <linux/module.h>
18 #include <asm/processor.h>
23 * Access to this subsystem has to be serialized externally. (this is
24 * true for the boot process anyway)
26 unsigned long max_low_pfn
;
27 unsigned long min_low_pfn
;
28 unsigned long max_pfn
;
30 static LIST_HEAD(bdata_list
);
31 #ifdef CONFIG_CRASH_DUMP
33 * If we have booted due to a crash, max_pfn will be a very low value. We need
34 * to know the amount of memory that the previous kernel used.
36 unsigned long saved_max_pfn
;
39 /* return the number of _pages_ that will be allocated for the boot bitmap */
40 unsigned long __init
bootmem_bootmap_pages(unsigned long pages
)
42 unsigned long mapsize
;
44 mapsize
= (pages
+7)/8;
45 mapsize
= (mapsize
+ ~PAGE_MASK
) & PAGE_MASK
;
46 mapsize
>>= PAGE_SHIFT
;
54 static void __init
link_bootmem(bootmem_data_t
*bdata
)
58 if (list_empty(&bdata_list
)) {
59 list_add(&bdata
->list
, &bdata_list
);
63 list_for_each_entry(ent
, &bdata_list
, list
) {
64 if (bdata
->node_boot_start
< ent
->node_boot_start
) {
65 list_add_tail(&bdata
->list
, &ent
->list
);
69 list_add_tail(&bdata
->list
, &bdata_list
);
73 * Given an initialised bdata, it returns the size of the boot bitmap
75 static unsigned long __init
get_mapsize(bootmem_data_t
*bdata
)
77 unsigned long mapsize
;
78 unsigned long start
= PFN_DOWN(bdata
->node_boot_start
);
79 unsigned long end
= bdata
->node_low_pfn
;
81 mapsize
= ((end
- start
) + 7) / 8;
82 return ALIGN(mapsize
, sizeof(long));
86 * Called once to set up the allocator itself.
88 static unsigned long __init
init_bootmem_core(pg_data_t
*pgdat
,
89 unsigned long mapstart
, unsigned long start
, unsigned long end
)
91 bootmem_data_t
*bdata
= pgdat
->bdata
;
92 unsigned long mapsize
;
94 bdata
->node_bootmem_map
= phys_to_virt(PFN_PHYS(mapstart
));
95 bdata
->node_boot_start
= PFN_PHYS(start
);
96 bdata
->node_low_pfn
= end
;
100 * Initially all pages are reserved - setup_arch() has to
101 * register free RAM areas explicitly.
103 mapsize
= get_mapsize(bdata
);
104 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
110 * Marks a particular physical memory range as unallocatable. Usable RAM
111 * might be used for boot-time allocations - or it might get added
112 * to the free page pool later on.
114 static void __init
reserve_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
,
117 unsigned long sidx
, eidx
;
121 * round up, partially reserved pages are considered
125 BUG_ON(PFN_DOWN(addr
) >= bdata
->node_low_pfn
);
126 BUG_ON(PFN_UP(addr
+ size
) > bdata
->node_low_pfn
);
128 sidx
= PFN_DOWN(addr
- bdata
->node_boot_start
);
129 eidx
= PFN_UP(addr
+ size
- bdata
->node_boot_start
);
131 for (i
= sidx
; i
< eidx
; i
++)
132 if (test_and_set_bit(i
, bdata
->node_bootmem_map
)) {
133 #ifdef CONFIG_DEBUG_BOOTMEM
134 printk("hm, page %08lx reserved twice.\n", i
*PAGE_SIZE
);
139 static void __init
free_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
,
142 unsigned long sidx
, eidx
;
146 * round down end of usable mem, partially free pages are
147 * considered reserved.
150 BUG_ON(PFN_DOWN(addr
+ size
) > bdata
->node_low_pfn
);
152 if (addr
< bdata
->last_success
)
153 bdata
->last_success
= addr
;
156 * Round up the beginning of the address.
158 sidx
= PFN_UP(addr
) - PFN_DOWN(bdata
->node_boot_start
);
159 eidx
= PFN_DOWN(addr
+ size
- bdata
->node_boot_start
);
161 for (i
= sidx
; i
< eidx
; i
++) {
162 if (unlikely(!test_and_clear_bit(i
, bdata
->node_bootmem_map
)))
168 * We 'merge' subsequent allocations to save space. We might 'lose'
169 * some fraction of a page if allocations cannot be satisfied due to
170 * size constraints on boxes where there is physical RAM space
171 * fragmentation - in these cases (mostly large memory boxes) this
174 * On low memory boxes we get it right in 100% of the cases.
176 * alignment has to be a power of 2 value.
178 * NOTE: This function is _not_ reentrant.
181 __alloc_bootmem_core(struct bootmem_data
*bdata
, unsigned long size
,
182 unsigned long align
, unsigned long goal
, unsigned long limit
)
184 unsigned long offset
, remaining_size
, areasize
, preferred
;
185 unsigned long i
, start
= 0, incr
, eidx
, end_pfn
;
189 printk("__alloc_bootmem_core(): zero-sized request\n");
192 BUG_ON(align
& (align
-1));
194 if (limit
&& bdata
->node_boot_start
>= limit
)
197 /* on nodes without memory - bootmem_map is NULL */
198 if (!bdata
->node_bootmem_map
)
201 end_pfn
= bdata
->node_low_pfn
;
202 limit
= PFN_DOWN(limit
);
203 if (limit
&& end_pfn
> limit
)
206 eidx
= end_pfn
- PFN_DOWN(bdata
->node_boot_start
);
208 if (align
&& (bdata
->node_boot_start
& (align
- 1UL)) != 0)
209 offset
= align
- (bdata
->node_boot_start
& (align
- 1UL));
210 offset
= PFN_DOWN(offset
);
213 * We try to allocate bootmem pages above 'goal'
214 * first, then we try to allocate lower pages.
216 if (goal
&& goal
>= bdata
->node_boot_start
&& PFN_DOWN(goal
) < end_pfn
) {
217 preferred
= goal
- bdata
->node_boot_start
;
219 if (bdata
->last_success
>= preferred
)
220 if (!limit
|| (limit
&& limit
> bdata
->last_success
))
221 preferred
= bdata
->last_success
;
225 preferred
= PFN_DOWN(ALIGN(preferred
, align
)) + offset
;
226 areasize
= (size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
227 incr
= align
>> PAGE_SHIFT
? : 1;
230 for (i
= preferred
; i
< eidx
; i
+= incr
) {
232 i
= find_next_zero_bit(bdata
->node_bootmem_map
, eidx
, i
);
236 if (test_bit(i
, bdata
->node_bootmem_map
))
238 for (j
= i
+ 1; j
< i
+ areasize
; ++j
) {
241 if (test_bit(j
, bdata
->node_bootmem_map
))
250 if (preferred
> offset
) {
257 bdata
->last_success
= PFN_PHYS(start
);
258 BUG_ON(start
>= eidx
);
261 * Is the next page of the previous allocation-end the start
262 * of this allocation's buffer? If yes then we can 'merge'
263 * the previous partial page with this allocation.
265 if (align
< PAGE_SIZE
&&
266 bdata
->last_offset
&& bdata
->last_pos
+1 == start
) {
267 offset
= ALIGN(bdata
->last_offset
, align
);
268 BUG_ON(offset
> PAGE_SIZE
);
269 remaining_size
= PAGE_SIZE
- offset
;
270 if (size
< remaining_size
) {
272 /* last_pos unchanged */
273 bdata
->last_offset
= offset
+ size
;
274 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
276 bdata
->node_boot_start
);
278 remaining_size
= size
- remaining_size
;
279 areasize
= (remaining_size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
280 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
282 bdata
->node_boot_start
);
283 bdata
->last_pos
= start
+ areasize
- 1;
284 bdata
->last_offset
= remaining_size
;
286 bdata
->last_offset
&= ~PAGE_MASK
;
288 bdata
->last_pos
= start
+ areasize
- 1;
289 bdata
->last_offset
= size
& ~PAGE_MASK
;
290 ret
= phys_to_virt(start
* PAGE_SIZE
+ bdata
->node_boot_start
);
294 * Reserve the area now:
296 for (i
= start
; i
< start
+ areasize
; i
++)
297 if (unlikely(test_and_set_bit(i
, bdata
->node_bootmem_map
)))
299 memset(ret
, 0, size
);
303 static unsigned long __init
free_all_bootmem_core(pg_data_t
*pgdat
)
307 bootmem_data_t
*bdata
= pgdat
->bdata
;
308 unsigned long i
, count
, total
= 0;
313 BUG_ON(!bdata
->node_bootmem_map
);
316 /* first extant page of the node */
317 pfn
= PFN_DOWN(bdata
->node_boot_start
);
318 idx
= bdata
->node_low_pfn
- pfn
;
319 map
= bdata
->node_bootmem_map
;
320 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
321 if (bdata
->node_boot_start
== 0 ||
322 ffs(bdata
->node_boot_start
) - PAGE_SHIFT
> ffs(BITS_PER_LONG
))
324 for (i
= 0; i
< idx
; ) {
325 unsigned long v
= ~map
[i
/ BITS_PER_LONG
];
327 if (gofast
&& v
== ~0UL) {
330 page
= pfn_to_page(pfn
);
331 count
+= BITS_PER_LONG
;
332 order
= ffs(BITS_PER_LONG
) - 1;
333 __free_pages_bootmem(page
, order
);
335 page
+= BITS_PER_LONG
;
339 page
= pfn_to_page(pfn
);
340 for (m
= 1; m
&& i
< idx
; m
<<=1, page
++, i
++) {
343 __free_pages_bootmem(page
, 0);
349 pfn
+= BITS_PER_LONG
;
354 * Now free the allocator bitmap itself, it's not
357 page
= virt_to_page(bdata
->node_bootmem_map
);
359 idx
= (get_mapsize(bdata
) + PAGE_SIZE
-1) >> PAGE_SHIFT
;
360 for (i
= 0; i
< idx
; i
++, page
++) {
361 __free_pages_bootmem(page
, 0);
365 bdata
->node_bootmem_map
= NULL
;
370 unsigned long __init
init_bootmem_node(pg_data_t
*pgdat
, unsigned long freepfn
,
371 unsigned long startpfn
, unsigned long endpfn
)
373 return init_bootmem_core(pgdat
, freepfn
, startpfn
, endpfn
);
376 void __init
reserve_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
379 reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
);
382 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
385 free_bootmem_core(pgdat
->bdata
, physaddr
, size
);
388 unsigned long __init
free_all_bootmem_node(pg_data_t
*pgdat
)
390 return free_all_bootmem_core(pgdat
);
393 unsigned long __init
init_bootmem(unsigned long start
, unsigned long pages
)
397 return init_bootmem_core(NODE_DATA(0), start
, 0, pages
);
400 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
401 void __init
reserve_bootmem(unsigned long addr
, unsigned long size
)
403 reserve_bootmem_core(NODE_DATA(0)->bdata
, addr
, size
);
405 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
407 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
409 free_bootmem_core(NODE_DATA(0)->bdata
, addr
, size
);
412 unsigned long __init
free_all_bootmem(void)
414 return free_all_bootmem_core(NODE_DATA(0));
417 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
420 bootmem_data_t
*bdata
;
423 list_for_each_entry(bdata
, &bdata_list
, list
) {
424 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
, 0);
431 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
434 void *mem
= __alloc_bootmem_nopanic(size
,align
,goal
);
439 * Whoops, we cannot satisfy the allocation request.
441 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
442 panic("Out of memory");
447 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
448 unsigned long align
, unsigned long goal
)
452 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
, 0);
456 return __alloc_bootmem(size
, align
, goal
);
459 #ifndef ARCH_LOW_ADDRESS_LIMIT
460 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
463 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
466 bootmem_data_t
*bdata
;
469 list_for_each_entry(bdata
, &bdata_list
, list
) {
470 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
,
471 ARCH_LOW_ADDRESS_LIMIT
);
477 * Whoops, we cannot satisfy the allocation request.
479 printk(KERN_ALERT
"low bootmem alloc of %lu bytes failed!\n", size
);
480 panic("Out of low memory");
484 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
485 unsigned long align
, unsigned long goal
)
487 return __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
,
488 ARCH_LOW_ADDRESS_LIMIT
);