]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/bootmem.c
[PATCH] oom: more printk
[net-next-2.6.git] / mm / bootmem.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/bootmem.c
3 *
4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
6 *
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.
10 */
1da177e4 11#include <linux/init.h>
bbc7b92e 12#include <linux/pfn.h>
1da177e4 13#include <linux/bootmem.h>
1da177e4 14#include <linux/module.h>
e786e86a
FBH
15
16#include <asm/bug.h>
1da177e4 17#include <asm/io.h>
e786e86a 18
1da177e4
LT
19#include "internal.h"
20
21/*
22 * Access to this subsystem has to be serialized externally. (this is
23 * true for the boot process anyway)
24 */
25unsigned long max_low_pfn;
26unsigned long min_low_pfn;
27unsigned long max_pfn;
28
6d46cc6b 29EXPORT_UNUSED_SYMBOL(max_pfn); /* June 2006 */
1da177e4 30
679bc9fb 31static LIST_HEAD(bdata_list);
92aa63a5
VG
32#ifdef CONFIG_CRASH_DUMP
33/*
34 * If we have booted due to a crash, max_pfn will be a very low value. We need
35 * to know the amount of memory that the previous kernel used.
36 */
37unsigned long saved_max_pfn;
38#endif
39
1da177e4 40/* return the number of _pages_ that will be allocated for the boot bitmap */
f71bf0ca 41unsigned long __init bootmem_bootmap_pages(unsigned long pages)
1da177e4
LT
42{
43 unsigned long mapsize;
44
45 mapsize = (pages+7)/8;
46 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
47 mapsize >>= PAGE_SHIFT;
48
49 return mapsize;
50}
f71bf0ca 51
679bc9fb
KH
52/*
53 * link bdata in order
54 */
69d49e68 55static void __init link_bootmem(bootmem_data_t *bdata)
679bc9fb
KH
56{
57 bootmem_data_t *ent;
f71bf0ca 58
679bc9fb
KH
59 if (list_empty(&bdata_list)) {
60 list_add(&bdata->list, &bdata_list);
61 return;
62 }
63 /* insert in order */
64 list_for_each_entry(ent, &bdata_list, list) {
65 if (bdata->node_boot_start < ent->node_boot_start) {
66 list_add_tail(&bdata->list, &ent->list);
67 return;
68 }
69 }
70 list_add_tail(&bdata->list, &bdata_list);
679bc9fb
KH
71}
72
bbc7b92e
FBH
73/*
74 * Given an initialised bdata, it returns the size of the boot bitmap
75 */
76static unsigned long __init get_mapsize(bootmem_data_t *bdata)
77{
78 unsigned long mapsize;
79 unsigned long start = PFN_DOWN(bdata->node_boot_start);
80 unsigned long end = bdata->node_low_pfn;
81
82 mapsize = ((end - start) + 7) / 8;
83 return ALIGN(mapsize, sizeof(long));
84}
1da177e4
LT
85
86/*
87 * Called once to set up the allocator itself.
88 */
f71bf0ca 89static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
1da177e4
LT
90 unsigned long mapstart, unsigned long start, unsigned long end)
91{
92 bootmem_data_t *bdata = pgdat->bdata;
bbc7b92e 93 unsigned long mapsize;
1da177e4 94
bbc7b92e
FBH
95 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
96 bdata->node_boot_start = PFN_PHYS(start);
1da177e4 97 bdata->node_low_pfn = end;
679bc9fb 98 link_bootmem(bdata);
1da177e4
LT
99
100 /*
101 * Initially all pages are reserved - setup_arch() has to
102 * register free RAM areas explicitly.
103 */
bbc7b92e 104 mapsize = get_mapsize(bdata);
1da177e4
LT
105 memset(bdata->node_bootmem_map, 0xff, mapsize);
106
107 return mapsize;
108}
109
110/*
111 * Marks a particular physical memory range as unallocatable. Usable RAM
112 * might be used for boot-time allocations - or it might get added
113 * to the free page pool later on.
114 */
bb0923a6
FBH
115static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
116 unsigned long size)
1da177e4 117{
bbc7b92e 118 unsigned long sidx, eidx;
1da177e4 119 unsigned long i;
bbc7b92e 120
1da177e4
LT
121 /*
122 * round up, partially reserved pages are considered
123 * fully reserved.
124 */
1da177e4 125 BUG_ON(!size);
bbc7b92e
FBH
126 BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn);
127 BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn);
128
129 sidx = PFN_DOWN(addr - bdata->node_boot_start);
130 eidx = PFN_UP(addr + size - bdata->node_boot_start);
1da177e4
LT
131
132 for (i = sidx; i < eidx; i++)
133 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
134#ifdef CONFIG_DEBUG_BOOTMEM
135 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
136#endif
137 }
138}
139
bb0923a6
FBH
140static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
141 unsigned long size)
1da177e4 142{
bbc7b92e 143 unsigned long sidx, eidx;
1da177e4 144 unsigned long i;
bbc7b92e 145
1da177e4
LT
146 /*
147 * round down end of usable mem, partially free pages are
148 * considered reserved.
149 */
1da177e4 150 BUG_ON(!size);
bbc7b92e 151 BUG_ON(PFN_DOWN(addr + size) > bdata->node_low_pfn);
1da177e4
LT
152
153 if (addr < bdata->last_success)
154 bdata->last_success = addr;
155
156 /*
157 * Round up the beginning of the address.
158 */
bbc7b92e
FBH
159 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
160 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
1da177e4
LT
161
162 for (i = sidx; i < eidx; i++) {
163 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
164 BUG();
165 }
166}
167
168/*
169 * We 'merge' subsequent allocations to save space. We might 'lose'
170 * some fraction of a page if allocations cannot be satisfied due to
171 * size constraints on boxes where there is physical RAM space
172 * fragmentation - in these cases (mostly large memory boxes) this
173 * is not a problem.
174 *
175 * On low memory boxes we get it right in 100% of the cases.
176 *
177 * alignment has to be a power of 2 value.
178 *
179 * NOTE: This function is _not_ reentrant.
180 */
267b4801 181void * __init
1da177e4 182__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
281dd25c 183 unsigned long align, unsigned long goal, unsigned long limit)
1da177e4
LT
184{
185 unsigned long offset, remaining_size, areasize, preferred;
bbc7b92e 186 unsigned long i, start = 0, incr, eidx, end_pfn;
1da177e4
LT
187 void *ret;
188
f71bf0ca 189 if (!size) {
1da177e4
LT
190 printk("__alloc_bootmem_core(): zero-sized request\n");
191 BUG();
192 }
193 BUG_ON(align & (align-1));
194
281dd25c
YG
195 if (limit && bdata->node_boot_start >= limit)
196 return NULL;
197
bbc7b92e
FBH
198 end_pfn = bdata->node_low_pfn;
199 limit = PFN_DOWN(limit);
281dd25c
YG
200 if (limit && end_pfn > limit)
201 end_pfn = limit;
202
bbc7b92e 203 eidx = end_pfn - PFN_DOWN(bdata->node_boot_start);
1da177e4 204 offset = 0;
bbc7b92e
FBH
205 if (align && (bdata->node_boot_start & (align - 1UL)) != 0)
206 offset = align - (bdata->node_boot_start & (align - 1UL));
207 offset = PFN_DOWN(offset);
1da177e4
LT
208
209 /*
210 * We try to allocate bootmem pages above 'goal'
211 * first, then we try to allocate lower pages.
212 */
bbc7b92e 213 if (goal && goal >= bdata->node_boot_start && PFN_DOWN(goal) < end_pfn) {
1da177e4
LT
214 preferred = goal - bdata->node_boot_start;
215
216 if (bdata->last_success >= preferred)
281dd25c
YG
217 if (!limit || (limit && limit > bdata->last_success))
218 preferred = bdata->last_success;
1da177e4
LT
219 } else
220 preferred = 0;
221
bbc7b92e
FBH
222 preferred = PFN_DOWN(ALIGN(preferred, align)) + offset;
223 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
1da177e4
LT
224 incr = align >> PAGE_SHIFT ? : 1;
225
226restart_scan:
227 for (i = preferred; i < eidx; i += incr) {
228 unsigned long j;
229 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
230 i = ALIGN(i, incr);
66d43e98
HM
231 if (i >= eidx)
232 break;
1da177e4
LT
233 if (test_bit(i, bdata->node_bootmem_map))
234 continue;
235 for (j = i + 1; j < i + areasize; ++j) {
236 if (j >= eidx)
237 goto fail_block;
f71bf0ca 238 if (test_bit(j, bdata->node_bootmem_map))
1da177e4
LT
239 goto fail_block;
240 }
241 start = i;
242 goto found;
243 fail_block:
244 i = ALIGN(j, incr);
245 }
246
247 if (preferred > offset) {
248 preferred = offset;
249 goto restart_scan;
250 }
251 return NULL;
252
253found:
bbc7b92e 254 bdata->last_success = PFN_PHYS(start);
1da177e4
LT
255 BUG_ON(start >= eidx);
256
257 /*
258 * Is the next page of the previous allocation-end the start
259 * of this allocation's buffer? If yes then we can 'merge'
260 * the previous partial page with this allocation.
261 */
262 if (align < PAGE_SIZE &&
263 bdata->last_offset && bdata->last_pos+1 == start) {
8c0e33c1 264 offset = ALIGN(bdata->last_offset, align);
1da177e4 265 BUG_ON(offset > PAGE_SIZE);
f71bf0ca 266 remaining_size = PAGE_SIZE - offset;
1da177e4
LT
267 if (size < remaining_size) {
268 areasize = 0;
269 /* last_pos unchanged */
f71bf0ca
FBH
270 bdata->last_offset = offset + size;
271 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
272 offset +
273 bdata->node_boot_start);
1da177e4
LT
274 } else {
275 remaining_size = size - remaining_size;
f71bf0ca
FBH
276 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
277 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
278 offset +
279 bdata->node_boot_start);
280 bdata->last_pos = start + areasize - 1;
1da177e4
LT
281 bdata->last_offset = remaining_size;
282 }
283 bdata->last_offset &= ~PAGE_MASK;
284 } else {
285 bdata->last_pos = start + areasize - 1;
286 bdata->last_offset = size & ~PAGE_MASK;
287 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
288 }
289
290 /*
291 * Reserve the area now:
292 */
f71bf0ca 293 for (i = start; i < start + areasize; i++)
1da177e4
LT
294 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
295 BUG();
296 memset(ret, 0, size);
297 return ret;
298}
299
300static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
301{
302 struct page *page;
d41dee36 303 unsigned long pfn;
1da177e4
LT
304 bootmem_data_t *bdata = pgdat->bdata;
305 unsigned long i, count, total = 0;
306 unsigned long idx;
307 unsigned long *map;
308 int gofast = 0;
309
310 BUG_ON(!bdata->node_bootmem_map);
311
312 count = 0;
313 /* first extant page of the node */
bbc7b92e
FBH
314 pfn = PFN_DOWN(bdata->node_boot_start);
315 idx = bdata->node_low_pfn - pfn;
1da177e4
LT
316 map = bdata->node_bootmem_map;
317 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
318 if (bdata->node_boot_start == 0 ||
319 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
320 gofast = 1;
321 for (i = 0; i < idx; ) {
322 unsigned long v = ~map[i / BITS_PER_LONG];
d41dee36 323
1da177e4 324 if (gofast && v == ~0UL) {
a226f6c8 325 int order;
1da177e4 326
d41dee36 327 page = pfn_to_page(pfn);
1da177e4 328 count += BITS_PER_LONG;
1da177e4 329 order = ffs(BITS_PER_LONG) - 1;
a226f6c8 330 __free_pages_bootmem(page, order);
1da177e4
LT
331 i += BITS_PER_LONG;
332 page += BITS_PER_LONG;
333 } else if (v) {
334 unsigned long m;
d41dee36
AW
335
336 page = pfn_to_page(pfn);
1da177e4
LT
337 for (m = 1; m && i < idx; m<<=1, page++, i++) {
338 if (v & m) {
339 count++;
a226f6c8 340 __free_pages_bootmem(page, 0);
1da177e4
LT
341 }
342 }
343 } else {
f71bf0ca 344 i += BITS_PER_LONG;
1da177e4 345 }
d41dee36 346 pfn += BITS_PER_LONG;
1da177e4
LT
347 }
348 total += count;
349
350 /*
351 * Now free the allocator bitmap itself, it's not
352 * needed anymore:
353 */
354 page = virt_to_page(bdata->node_bootmem_map);
355 count = 0;
bbc7b92e
FBH
356 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
357 for (i = 0; i < idx; i++, page++) {
a226f6c8 358 __free_pages_bootmem(page, 0);
bbc7b92e 359 count++;
1da177e4
LT
360 }
361 total += count;
362 bdata->node_bootmem_map = NULL;
363
364 return total;
365}
366
f71bf0ca 367unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
bb0923a6 368 unsigned long startpfn, unsigned long endpfn)
1da177e4 369{
f71bf0ca 370 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
1da177e4
LT
371}
372
f71bf0ca
FBH
373void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
374 unsigned long size)
1da177e4
LT
375{
376 reserve_bootmem_core(pgdat->bdata, physaddr, size);
377}
378
f71bf0ca
FBH
379void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
380 unsigned long size)
1da177e4
LT
381{
382 free_bootmem_core(pgdat->bdata, physaddr, size);
383}
384
f71bf0ca 385unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
1da177e4 386{
f71bf0ca 387 return free_all_bootmem_core(pgdat);
1da177e4
LT
388}
389
f71bf0ca 390unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
1da177e4
LT
391{
392 max_low_pfn = pages;
393 min_low_pfn = start;
f71bf0ca 394 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
1da177e4
LT
395}
396
397#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
f71bf0ca 398void __init reserve_bootmem(unsigned long addr, unsigned long size)
1da177e4
LT
399{
400 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
401}
402#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
403
f71bf0ca 404void __init free_bootmem(unsigned long addr, unsigned long size)
1da177e4
LT
405{
406 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
407}
408
f71bf0ca 409unsigned long __init free_all_bootmem(void)
1da177e4 410{
f71bf0ca 411 return free_all_bootmem_core(NODE_DATA(0));
1da177e4
LT
412}
413
bb0923a6
FBH
414void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
415 unsigned long goal)
1da177e4 416{
679bc9fb 417 bootmem_data_t *bdata;
1da177e4
LT
418 void *ptr;
419
f71bf0ca
FBH
420 list_for_each_entry(bdata, &bdata_list, list) {
421 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
422 if (ptr)
423 return ptr;
424 }
a8062231
AK
425 return NULL;
426}
1da177e4 427
bb0923a6
FBH
428void * __init __alloc_bootmem(unsigned long size, unsigned long align,
429 unsigned long goal)
a8062231
AK
430{
431 void *mem = __alloc_bootmem_nopanic(size,align,goal);
f71bf0ca 432
a8062231
AK
433 if (mem)
434 return mem;
1da177e4
LT
435 /*
436 * Whoops, we cannot satisfy the allocation request.
437 */
438 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
439 panic("Out of memory");
440 return NULL;
441}
442
281dd25c 443
bb0923a6
FBH
444void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
445 unsigned long align, unsigned long goal)
1da177e4
LT
446{
447 void *ptr;
448
008857c1 449 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
1da177e4 450 if (ptr)
f71bf0ca 451 return ptr;
1da177e4 452
008857c1 453 return __alloc_bootmem(size, align, goal);
1da177e4
LT
454}
455
008857c1
RT
456#define LOW32LIMIT 0xffffffff
457
bb0923a6
FBH
458void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
459 unsigned long goal)
008857c1 460{
679bc9fb 461 bootmem_data_t *bdata;
008857c1
RT
462 void *ptr;
463
f71bf0ca
FBH
464 list_for_each_entry(bdata, &bdata_list, list) {
465 ptr = __alloc_bootmem_core(bdata, size, align, goal, LOW32LIMIT);
466 if (ptr)
467 return ptr;
468 }
008857c1
RT
469
470 /*
471 * Whoops, we cannot satisfy the allocation request.
472 */
473 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
474 panic("Out of low memory");
475 return NULL;
476}
477
478void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
479 unsigned long align, unsigned long goal)
480{
481 return __alloc_bootmem_core(pgdat->bdata, size, align, goal, LOW32LIMIT);
482}