]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
57cfc29e | 2 | * bootmem - A boot-time physical memory allocator and configurator |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 1999 Ingo Molnar | |
57cfc29e JW |
5 | * 1999 Kanoj Sarcar, SGI |
6 | * 2008 Johannes Weiner | |
1da177e4 | 7 | * |
57cfc29e JW |
8 | * Access to this subsystem has to be serialized externally (which is true |
9 | * for the boot process anyway). | |
1da177e4 | 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> |
dfd54cbc | 18 | #include <asm/processor.h> |
e786e86a | 19 | |
1da177e4 LT |
20 | #include "internal.h" |
21 | ||
1da177e4 LT |
22 | unsigned long max_low_pfn; |
23 | unsigned long min_low_pfn; | |
24 | unsigned long max_pfn; | |
25 | ||
679bc9fb | 26 | static LIST_HEAD(bdata_list); |
92aa63a5 VG |
27 | #ifdef CONFIG_CRASH_DUMP |
28 | /* | |
29 | * If we have booted due to a crash, max_pfn will be a very low value. We need | |
30 | * to know the amount of memory that the previous kernel used. | |
31 | */ | |
32 | unsigned long saved_max_pfn; | |
33 | #endif | |
34 | ||
b61bfa3c JW |
35 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
36 | ||
2e5237da JW |
37 | static int bootmem_debug; |
38 | ||
39 | static int __init bootmem_debug_setup(char *buf) | |
40 | { | |
41 | bootmem_debug = 1; | |
42 | return 0; | |
43 | } | |
44 | early_param("bootmem_debug", bootmem_debug_setup); | |
45 | ||
46 | #define bdebug(fmt, args...) ({ \ | |
47 | if (unlikely(bootmem_debug)) \ | |
48 | printk(KERN_INFO \ | |
49 | "bootmem::%s " fmt, \ | |
50 | __FUNCTION__, ## args); \ | |
51 | }) | |
52 | ||
df049a5f | 53 | static unsigned long __init bootmap_bytes(unsigned long pages) |
223e8dc9 | 54 | { |
df049a5f | 55 | unsigned long bytes = (pages + 7) / 8; |
223e8dc9 | 56 | |
df049a5f | 57 | return ALIGN(bytes, sizeof(long)); |
223e8dc9 JW |
58 | } |
59 | ||
a66fd7da JW |
60 | /** |
61 | * bootmem_bootmap_pages - calculate bitmap size in pages | |
62 | * @pages: number of pages the bitmap has to represent | |
63 | */ | |
f71bf0ca | 64 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
1da177e4 | 65 | { |
df049a5f | 66 | unsigned long bytes = bootmap_bytes(pages); |
1da177e4 | 67 | |
df049a5f | 68 | return PAGE_ALIGN(bytes) >> PAGE_SHIFT; |
1da177e4 | 69 | } |
f71bf0ca | 70 | |
679bc9fb KH |
71 | /* |
72 | * link bdata in order | |
73 | */ | |
69d49e68 | 74 | static void __init link_bootmem(bootmem_data_t *bdata) |
679bc9fb KH |
75 | { |
76 | bootmem_data_t *ent; | |
f71bf0ca | 77 | |
679bc9fb KH |
78 | if (list_empty(&bdata_list)) { |
79 | list_add(&bdata->list, &bdata_list); | |
80 | return; | |
81 | } | |
82 | /* insert in order */ | |
83 | list_for_each_entry(ent, &bdata_list, list) { | |
84 | if (bdata->node_boot_start < ent->node_boot_start) { | |
85 | list_add_tail(&bdata->list, &ent->list); | |
86 | return; | |
87 | } | |
88 | } | |
89 | list_add_tail(&bdata->list, &bdata_list); | |
679bc9fb KH |
90 | } |
91 | ||
1da177e4 LT |
92 | /* |
93 | * Called once to set up the allocator itself. | |
94 | */ | |
8ae04463 | 95 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
1da177e4 LT |
96 | unsigned long mapstart, unsigned long start, unsigned long end) |
97 | { | |
bbc7b92e | 98 | unsigned long mapsize; |
1da177e4 | 99 | |
2dbb51c4 | 100 | mminit_validate_memmodel_limits(&start, &end); |
bbc7b92e FBH |
101 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
102 | bdata->node_boot_start = PFN_PHYS(start); | |
1da177e4 | 103 | bdata->node_low_pfn = end; |
679bc9fb | 104 | link_bootmem(bdata); |
1da177e4 LT |
105 | |
106 | /* | |
107 | * Initially all pages are reserved - setup_arch() has to | |
108 | * register free RAM areas explicitly. | |
109 | */ | |
df049a5f | 110 | mapsize = bootmap_bytes(end - start); |
1da177e4 LT |
111 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
112 | ||
2e5237da JW |
113 | bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", |
114 | bdata - bootmem_node_data, start, mapstart, end, mapsize); | |
115 | ||
1da177e4 LT |
116 | return mapsize; |
117 | } | |
118 | ||
a66fd7da JW |
119 | /** |
120 | * init_bootmem_node - register a node as boot memory | |
121 | * @pgdat: node to register | |
122 | * @freepfn: pfn where the bitmap for this node is to be placed | |
123 | * @startpfn: first pfn on the node | |
124 | * @endpfn: first pfn after the node | |
125 | * | |
126 | * Returns the number of bytes needed to hold the bitmap for this node. | |
127 | */ | |
223e8dc9 JW |
128 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
129 | unsigned long startpfn, unsigned long endpfn) | |
130 | { | |
131 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); | |
132 | } | |
133 | ||
a66fd7da JW |
134 | /** |
135 | * init_bootmem - register boot memory | |
136 | * @start: pfn where the bitmap is to be placed | |
137 | * @pages: number of available physical pages | |
138 | * | |
139 | * Returns the number of bytes needed to hold the bitmap. | |
140 | */ | |
223e8dc9 JW |
141 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
142 | { | |
143 | max_low_pfn = pages; | |
144 | min_low_pfn = start; | |
145 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); | |
146 | } | |
147 | ||
148 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) | |
149 | { | |
150 | struct page *page; | |
151 | unsigned long pfn; | |
152 | unsigned long i, count; | |
df049a5f | 153 | unsigned long idx, pages; |
223e8dc9 JW |
154 | unsigned long *map; |
155 | int gofast = 0; | |
156 | ||
157 | BUG_ON(!bdata->node_bootmem_map); | |
158 | ||
159 | count = 0; | |
160 | /* first extant page of the node */ | |
161 | pfn = PFN_DOWN(bdata->node_boot_start); | |
162 | idx = bdata->node_low_pfn - pfn; | |
163 | map = bdata->node_bootmem_map; | |
164 | /* | |
165 | * Check if we are aligned to BITS_PER_LONG pages. If so, we might | |
166 | * be able to free page orders of that size at once. | |
167 | */ | |
168 | if (!(pfn & (BITS_PER_LONG-1))) | |
169 | gofast = 1; | |
170 | ||
171 | for (i = 0; i < idx; ) { | |
172 | unsigned long v = ~map[i / BITS_PER_LONG]; | |
173 | ||
174 | if (gofast && v == ~0UL) { | |
175 | int order; | |
176 | ||
177 | page = pfn_to_page(pfn); | |
178 | count += BITS_PER_LONG; | |
179 | order = ffs(BITS_PER_LONG) - 1; | |
180 | __free_pages_bootmem(page, order); | |
181 | i += BITS_PER_LONG; | |
182 | page += BITS_PER_LONG; | |
183 | } else if (v) { | |
184 | unsigned long m; | |
185 | ||
186 | page = pfn_to_page(pfn); | |
187 | for (m = 1; m && i < idx; m<<=1, page++, i++) { | |
188 | if (v & m) { | |
189 | count++; | |
190 | __free_pages_bootmem(page, 0); | |
191 | } | |
192 | } | |
193 | } else { | |
194 | i += BITS_PER_LONG; | |
195 | } | |
196 | pfn += BITS_PER_LONG; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Now free the allocator bitmap itself, it's not | |
201 | * needed anymore: | |
202 | */ | |
203 | page = virt_to_page(bdata->node_bootmem_map); | |
df049a5f JW |
204 | pages = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
205 | idx = bootmem_bootmap_pages(pages); | |
223e8dc9 JW |
206 | for (i = 0; i < idx; i++, page++) |
207 | __free_pages_bootmem(page, 0); | |
208 | count += i; | |
209 | bdata->node_bootmem_map = NULL; | |
210 | ||
2e5237da JW |
211 | bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count); |
212 | ||
223e8dc9 JW |
213 | return count; |
214 | } | |
215 | ||
a66fd7da JW |
216 | /** |
217 | * free_all_bootmem_node - release a node's free pages to the buddy allocator | |
218 | * @pgdat: node to be released | |
219 | * | |
220 | * Returns the number of pages actually released. | |
221 | */ | |
223e8dc9 JW |
222 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
223 | { | |
224 | register_page_bootmem_info_node(pgdat); | |
225 | return free_all_bootmem_core(pgdat->bdata); | |
226 | } | |
227 | ||
a66fd7da JW |
228 | /** |
229 | * free_all_bootmem - release free pages to the buddy allocator | |
230 | * | |
231 | * Returns the number of pages actually released. | |
232 | */ | |
223e8dc9 JW |
233 | unsigned long __init free_all_bootmem(void) |
234 | { | |
235 | return free_all_bootmem_core(NODE_DATA(0)->bdata); | |
236 | } | |
237 | ||
238 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, | |
239 | unsigned long size) | |
240 | { | |
241 | unsigned long sidx, eidx; | |
242 | unsigned long i; | |
243 | ||
244 | BUG_ON(!size); | |
245 | ||
246 | /* out range */ | |
247 | if (addr + size < bdata->node_boot_start || | |
248 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
249 | return; | |
250 | /* | |
251 | * round down end of usable mem, partially free pages are | |
252 | * considered reserved. | |
253 | */ | |
254 | ||
255 | if (addr >= bdata->node_boot_start && addr < bdata->last_success) | |
256 | bdata->last_success = addr; | |
257 | ||
258 | /* | |
259 | * Round up to index to the range. | |
260 | */ | |
261 | if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start)) | |
262 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); | |
263 | else | |
264 | sidx = 0; | |
265 | ||
266 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); | |
267 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) | |
268 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
269 | ||
2e5237da JW |
270 | bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data, |
271 | sidx + PFN_DOWN(bdata->node_boot_start), | |
272 | eidx + PFN_DOWN(bdata->node_boot_start)); | |
273 | ||
223e8dc9 JW |
274 | for (i = sidx; i < eidx; i++) { |
275 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) | |
276 | BUG(); | |
277 | } | |
278 | } | |
279 | ||
a66fd7da JW |
280 | /** |
281 | * free_bootmem_node - mark a page range as usable | |
282 | * @pgdat: node the range resides on | |
283 | * @physaddr: starting address of the range | |
284 | * @size: size of the range in bytes | |
285 | * | |
286 | * Partial pages will be considered reserved and left as they are. | |
287 | * | |
288 | * Only physical pages that actually reside on @pgdat are marked. | |
289 | */ | |
223e8dc9 JW |
290 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
291 | unsigned long size) | |
292 | { | |
293 | free_bootmem_core(pgdat->bdata, physaddr, size); | |
294 | } | |
295 | ||
a66fd7da JW |
296 | /** |
297 | * free_bootmem - mark a page range as usable | |
298 | * @addr: starting address of the range | |
299 | * @size: size of the range in bytes | |
300 | * | |
301 | * Partial pages will be considered reserved and left as they are. | |
302 | * | |
303 | * All physical pages within the range are marked, no matter what | |
304 | * node they reside on. | |
305 | */ | |
223e8dc9 JW |
306 | void __init free_bootmem(unsigned long addr, unsigned long size) |
307 | { | |
308 | bootmem_data_t *bdata; | |
309 | list_for_each_entry(bdata, &bdata_list, list) | |
310 | free_bootmem_core(bdata, addr, size); | |
311 | } | |
312 | ||
1da177e4 LT |
313 | /* |
314 | * Marks a particular physical memory range as unallocatable. Usable RAM | |
315 | * might be used for boot-time allocations - or it might get added | |
316 | * to the free page pool later on. | |
317 | */ | |
a5645a61 | 318 | static int __init can_reserve_bootmem_core(bootmem_data_t *bdata, |
72a7fe39 | 319 | unsigned long addr, unsigned long size, int flags) |
1da177e4 | 320 | { |
bbc7b92e | 321 | unsigned long sidx, eidx; |
1da177e4 | 322 | unsigned long i; |
a5645a61 YL |
323 | |
324 | BUG_ON(!size); | |
325 | ||
326 | /* out of range, don't hold other */ | |
327 | if (addr + size < bdata->node_boot_start || | |
328 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
329 | return 0; | |
bbc7b92e | 330 | |
1da177e4 | 331 | /* |
a5645a61 | 332 | * Round up to index to the range. |
1da177e4 | 333 | */ |
a5645a61 YL |
334 | if (addr > bdata->node_boot_start) |
335 | sidx= PFN_DOWN(addr - bdata->node_boot_start); | |
336 | else | |
337 | sidx = 0; | |
338 | ||
339 | eidx = PFN_UP(addr + size - bdata->node_boot_start); | |
340 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) | |
341 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
342 | ||
343 | for (i = sidx; i < eidx; i++) { | |
344 | if (test_bit(i, bdata->node_bootmem_map)) { | |
345 | if (flags & BOOTMEM_EXCLUSIVE) | |
346 | return -EBUSY; | |
347 | } | |
348 | } | |
349 | ||
350 | return 0; | |
351 | ||
352 | } | |
353 | ||
354 | static void __init reserve_bootmem_core(bootmem_data_t *bdata, | |
355 | unsigned long addr, unsigned long size, int flags) | |
356 | { | |
357 | unsigned long sidx, eidx; | |
358 | unsigned long i; | |
359 | ||
1da177e4 | 360 | BUG_ON(!size); |
bbc7b92e | 361 | |
a5645a61 YL |
362 | /* out of range */ |
363 | if (addr + size < bdata->node_boot_start || | |
364 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
365 | return; | |
366 | ||
367 | /* | |
368 | * Round up to index to the range. | |
369 | */ | |
370 | if (addr > bdata->node_boot_start) | |
371 | sidx= PFN_DOWN(addr - bdata->node_boot_start); | |
372 | else | |
373 | sidx = 0; | |
374 | ||
bbc7b92e | 375 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
a5645a61 YL |
376 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
377 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
1da177e4 | 378 | |
2e5237da JW |
379 | bdebug("nid=%td start=%lx end=%lx flags=%x\n", |
380 | bdata - bootmem_node_data, | |
381 | sidx + PFN_DOWN(bdata->node_boot_start), | |
382 | eidx + PFN_DOWN(bdata->node_boot_start), | |
383 | flags); | |
384 | ||
385 | for (i = sidx; i < eidx; i++) | |
386 | if (test_and_set_bit(i, bdata->node_bootmem_map)) | |
387 | bdebug("hm, page %lx reserved twice.\n", | |
388 | PFN_DOWN(bdata->node_boot_start) + i); | |
1da177e4 LT |
389 | } |
390 | ||
a66fd7da JW |
391 | /** |
392 | * reserve_bootmem_node - mark a page range as reserved | |
393 | * @pgdat: node the range resides on | |
394 | * @physaddr: starting address of the range | |
395 | * @size: size of the range in bytes | |
396 | * @flags: reservation flags (see linux/bootmem.h) | |
397 | * | |
398 | * Partial pages will be reserved. | |
399 | * | |
400 | * Only physical pages that actually reside on @pgdat are marked. | |
401 | */ | |
223e8dc9 JW |
402 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
403 | unsigned long size, int flags) | |
1da177e4 | 404 | { |
223e8dc9 | 405 | int ret; |
1da177e4 | 406 | |
223e8dc9 JW |
407 | ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
408 | if (ret < 0) | |
409 | return -ENOMEM; | |
410 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); | |
411 | return 0; | |
412 | } | |
5a982cbc | 413 | |
223e8dc9 | 414 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE |
a66fd7da JW |
415 | /** |
416 | * reserve_bootmem - mark a page range as usable | |
417 | * @addr: starting address of the range | |
418 | * @size: size of the range in bytes | |
419 | * @flags: reservation flags (see linux/bootmem.h) | |
420 | * | |
421 | * Partial pages will be reserved. | |
422 | * | |
423 | * All physical pages within the range are marked, no matter what | |
424 | * node they reside on. | |
425 | */ | |
223e8dc9 JW |
426 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
427 | int flags) | |
428 | { | |
429 | bootmem_data_t *bdata; | |
430 | int ret; | |
1da177e4 | 431 | |
223e8dc9 JW |
432 | list_for_each_entry(bdata, &bdata_list, list) { |
433 | ret = can_reserve_bootmem_core(bdata, addr, size, flags); | |
434 | if (ret < 0) | |
435 | return ret; | |
1da177e4 | 436 | } |
223e8dc9 JW |
437 | list_for_each_entry(bdata, &bdata_list, list) |
438 | reserve_bootmem_core(bdata, addr, size, flags); | |
439 | ||
440 | return 0; | |
1da177e4 | 441 | } |
223e8dc9 | 442 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ |
1da177e4 LT |
443 | |
444 | /* | |
445 | * We 'merge' subsequent allocations to save space. We might 'lose' | |
446 | * some fraction of a page if allocations cannot be satisfied due to | |
447 | * size constraints on boxes where there is physical RAM space | |
448 | * fragmentation - in these cases (mostly large memory boxes) this | |
449 | * is not a problem. | |
450 | * | |
451 | * On low memory boxes we get it right in 100% of the cases. | |
452 | * | |
453 | * alignment has to be a power of 2 value. | |
454 | * | |
455 | * NOTE: This function is _not_ reentrant. | |
456 | */ | |
ffc6421f JW |
457 | static void * __init |
458 | alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, | |
459 | unsigned long align, unsigned long goal, unsigned long limit) | |
1da177e4 | 460 | { |
9a2dc04c | 461 | unsigned long areasize, preferred; |
bbc7b92e | 462 | unsigned long i, start = 0, incr, eidx, end_pfn; |
1da177e4 | 463 | void *ret; |
9a2dc04c YL |
464 | unsigned long node_boot_start; |
465 | void *node_bootmem_map; | |
1da177e4 | 466 | |
f71bf0ca | 467 | if (!size) { |
ffc6421f | 468 | printk("alloc_bootmem_core(): zero-sized request\n"); |
1da177e4 LT |
469 | BUG(); |
470 | } | |
471 | BUG_ON(align & (align-1)); | |
472 | ||
7c309a64 CK |
473 | /* on nodes without memory - bootmem_map is NULL */ |
474 | if (!bdata->node_bootmem_map) | |
475 | return NULL; | |
476 | ||
2e5237da JW |
477 | bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n", |
478 | bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT, | |
479 | align, goal, limit); | |
480 | ||
9a2dc04c YL |
481 | /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */ |
482 | node_boot_start = bdata->node_boot_start; | |
483 | node_bootmem_map = bdata->node_bootmem_map; | |
484 | if (align) { | |
485 | node_boot_start = ALIGN(bdata->node_boot_start, align); | |
486 | if (node_boot_start > bdata->node_boot_start) | |
487 | node_bootmem_map = (unsigned long *)bdata->node_bootmem_map + | |
488 | PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG; | |
489 | } | |
490 | ||
491 | if (limit && node_boot_start >= limit) | |
492 | return NULL; | |
493 | ||
bbc7b92e FBH |
494 | end_pfn = bdata->node_low_pfn; |
495 | limit = PFN_DOWN(limit); | |
281dd25c YG |
496 | if (limit && end_pfn > limit) |
497 | end_pfn = limit; | |
498 | ||
9a2dc04c | 499 | eidx = end_pfn - PFN_DOWN(node_boot_start); |
1da177e4 LT |
500 | |
501 | /* | |
502 | * We try to allocate bootmem pages above 'goal' | |
503 | * first, then we try to allocate lower pages. | |
504 | */ | |
ad09315c YL |
505 | preferred = 0; |
506 | if (goal && PFN_DOWN(goal) < end_pfn) { | |
9a2dc04c YL |
507 | if (goal > node_boot_start) |
508 | preferred = goal - node_boot_start; | |
1da177e4 | 509 | |
9a2dc04c YL |
510 | if (bdata->last_success > node_boot_start && |
511 | bdata->last_success - node_boot_start >= preferred) | |
281dd25c | 512 | if (!limit || (limit && limit > bdata->last_success)) |
9a2dc04c | 513 | preferred = bdata->last_success - node_boot_start; |
ad09315c | 514 | } |
1da177e4 | 515 | |
9a2dc04c | 516 | preferred = PFN_DOWN(ALIGN(preferred, align)); |
bbc7b92e | 517 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; |
1da177e4 LT |
518 | incr = align >> PAGE_SHIFT ? : 1; |
519 | ||
520 | restart_scan: | |
ad09315c | 521 | for (i = preferred; i < eidx;) { |
1da177e4 | 522 | unsigned long j; |
ad09315c | 523 | |
9a2dc04c | 524 | i = find_next_zero_bit(node_bootmem_map, eidx, i); |
1da177e4 | 525 | i = ALIGN(i, incr); |
66d43e98 HM |
526 | if (i >= eidx) |
527 | break; | |
9a2dc04c | 528 | if (test_bit(i, node_bootmem_map)) { |
ad09315c | 529 | i += incr; |
1da177e4 | 530 | continue; |
ad09315c | 531 | } |
1da177e4 LT |
532 | for (j = i + 1; j < i + areasize; ++j) { |
533 | if (j >= eidx) | |
534 | goto fail_block; | |
9a2dc04c | 535 | if (test_bit(j, node_bootmem_map)) |
1da177e4 LT |
536 | goto fail_block; |
537 | } | |
538 | start = i; | |
539 | goto found; | |
540 | fail_block: | |
541 | i = ALIGN(j, incr); | |
ad09315c YL |
542 | if (i == j) |
543 | i += incr; | |
1da177e4 LT |
544 | } |
545 | ||
9a2dc04c YL |
546 | if (preferred > 0) { |
547 | preferred = 0; | |
1da177e4 LT |
548 | goto restart_scan; |
549 | } | |
550 | return NULL; | |
551 | ||
552 | found: | |
9a2dc04c | 553 | bdata->last_success = PFN_PHYS(start) + node_boot_start; |
1da177e4 LT |
554 | BUG_ON(start >= eidx); |
555 | ||
556 | /* | |
557 | * Is the next page of the previous allocation-end the start | |
558 | * of this allocation's buffer? If yes then we can 'merge' | |
559 | * the previous partial page with this allocation. | |
560 | */ | |
561 | if (align < PAGE_SIZE && | |
562 | bdata->last_offset && bdata->last_pos+1 == start) { | |
9a2dc04c | 563 | unsigned long offset, remaining_size; |
8c0e33c1 | 564 | offset = ALIGN(bdata->last_offset, align); |
1da177e4 | 565 | BUG_ON(offset > PAGE_SIZE); |
f71bf0ca | 566 | remaining_size = PAGE_SIZE - offset; |
1da177e4 LT |
567 | if (size < remaining_size) { |
568 | areasize = 0; | |
569 | /* last_pos unchanged */ | |
f71bf0ca FBH |
570 | bdata->last_offset = offset + size; |
571 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
9a2dc04c | 572 | offset + node_boot_start); |
1da177e4 LT |
573 | } else { |
574 | remaining_size = size - remaining_size; | |
f71bf0ca FBH |
575 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
576 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
9a2dc04c | 577 | offset + node_boot_start); |
f71bf0ca | 578 | bdata->last_pos = start + areasize - 1; |
1da177e4 LT |
579 | bdata->last_offset = remaining_size; |
580 | } | |
581 | bdata->last_offset &= ~PAGE_MASK; | |
582 | } else { | |
583 | bdata->last_pos = start + areasize - 1; | |
584 | bdata->last_offset = size & ~PAGE_MASK; | |
9a2dc04c | 585 | ret = phys_to_virt(start * PAGE_SIZE + node_boot_start); |
1da177e4 LT |
586 | } |
587 | ||
2e5237da JW |
588 | bdebug("nid=%td start=%lx end=%lx\n", |
589 | bdata - bootmem_node_data, | |
590 | start + PFN_DOWN(bdata->node_boot_start), | |
591 | start + areasize + PFN_DOWN(bdata->node_boot_start)); | |
592 | ||
1da177e4 LT |
593 | /* |
594 | * Reserve the area now: | |
595 | */ | |
f71bf0ca | 596 | for (i = start; i < start + areasize; i++) |
9a2dc04c | 597 | if (unlikely(test_and_set_bit(i, node_bootmem_map))) |
1da177e4 LT |
598 | BUG(); |
599 | memset(ret, 0, size); | |
600 | return ret; | |
601 | } | |
602 | ||
a66fd7da JW |
603 | /** |
604 | * __alloc_bootmem_nopanic - allocate boot memory without panicking | |
605 | * @size: size of the request in bytes | |
606 | * @align: alignment of the region | |
607 | * @goal: preferred starting address of the region | |
608 | * | |
609 | * The goal is dropped if it can not be satisfied and the allocation will | |
610 | * fall back to memory below @goal. | |
611 | * | |
612 | * Allocation may happen on any node in the system. | |
613 | * | |
614 | * Returns NULL on failure. | |
615 | */ | |
bb0923a6 FBH |
616 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
617 | unsigned long goal) | |
1da177e4 | 618 | { |
679bc9fb | 619 | bootmem_data_t *bdata; |
1da177e4 LT |
620 | void *ptr; |
621 | ||
f71bf0ca | 622 | list_for_each_entry(bdata, &bdata_list, list) { |
ffc6421f | 623 | ptr = alloc_bootmem_core(bdata, size, align, goal, 0); |
f71bf0ca FBH |
624 | if (ptr) |
625 | return ptr; | |
626 | } | |
a8062231 AK |
627 | return NULL; |
628 | } | |
1da177e4 | 629 | |
a66fd7da JW |
630 | /** |
631 | * __alloc_bootmem - allocate boot memory | |
632 | * @size: size of the request in bytes | |
633 | * @align: alignment of the region | |
634 | * @goal: preferred starting address of the region | |
635 | * | |
636 | * The goal is dropped if it can not be satisfied and the allocation will | |
637 | * fall back to memory below @goal. | |
638 | * | |
639 | * Allocation may happen on any node in the system. | |
640 | * | |
641 | * The function panics if the request can not be satisfied. | |
642 | */ | |
bb0923a6 FBH |
643 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
644 | unsigned long goal) | |
a8062231 AK |
645 | { |
646 | void *mem = __alloc_bootmem_nopanic(size,align,goal); | |
f71bf0ca | 647 | |
a8062231 AK |
648 | if (mem) |
649 | return mem; | |
1da177e4 LT |
650 | /* |
651 | * Whoops, we cannot satisfy the allocation request. | |
652 | */ | |
653 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
654 | panic("Out of memory"); | |
655 | return NULL; | |
656 | } | |
657 | ||
a66fd7da JW |
658 | /** |
659 | * __alloc_bootmem_node - allocate boot memory from a specific node | |
660 | * @pgdat: node to allocate from | |
661 | * @size: size of the request in bytes | |
662 | * @align: alignment of the region | |
663 | * @goal: preferred starting address of the region | |
664 | * | |
665 | * The goal is dropped if it can not be satisfied and the allocation will | |
666 | * fall back to memory below @goal. | |
667 | * | |
668 | * Allocation may fall back to any node in the system if the specified node | |
669 | * can not hold the requested memory. | |
670 | * | |
671 | * The function panics if the request can not be satisfied. | |
672 | */ | |
bb0923a6 FBH |
673 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
674 | unsigned long align, unsigned long goal) | |
1da177e4 LT |
675 | { |
676 | void *ptr; | |
677 | ||
ffc6421f | 678 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
1da177e4 | 679 | if (ptr) |
f71bf0ca | 680 | return ptr; |
1da177e4 | 681 | |
008857c1 | 682 | return __alloc_bootmem(size, align, goal); |
1da177e4 LT |
683 | } |
684 | ||
e70260aa | 685 | #ifdef CONFIG_SPARSEMEM |
a66fd7da JW |
686 | /** |
687 | * alloc_bootmem_section - allocate boot memory from a specific section | |
688 | * @size: size of the request in bytes | |
689 | * @section_nr: sparse map section to allocate from | |
690 | * | |
691 | * Return NULL on failure. | |
692 | */ | |
e70260aa YG |
693 | void * __init alloc_bootmem_section(unsigned long size, |
694 | unsigned long section_nr) | |
695 | { | |
696 | void *ptr; | |
697 | unsigned long limit, goal, start_nr, end_nr, pfn; | |
698 | struct pglist_data *pgdat; | |
699 | ||
700 | pfn = section_nr_to_pfn(section_nr); | |
701 | goal = PFN_PHYS(pfn); | |
702 | limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1; | |
703 | pgdat = NODE_DATA(early_pfn_to_nid(pfn)); | |
ffc6421f JW |
704 | ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal, |
705 | limit); | |
e70260aa YG |
706 | |
707 | if (!ptr) | |
708 | return NULL; | |
709 | ||
710 | start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr))); | |
711 | end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size)); | |
712 | if (start_nr != section_nr || end_nr != section_nr) { | |
713 | printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n", | |
714 | section_nr); | |
715 | free_bootmem_core(pgdat->bdata, __pa(ptr), size); | |
716 | ptr = NULL; | |
717 | } | |
718 | ||
719 | return ptr; | |
720 | } | |
721 | #endif | |
722 | ||
b54bbf7b AK |
723 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
724 | unsigned long align, unsigned long goal) | |
725 | { | |
726 | void *ptr; | |
727 | ||
728 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); | |
729 | if (ptr) | |
730 | return ptr; | |
731 | ||
732 | return __alloc_bootmem_nopanic(size, align, goal); | |
733 | } | |
734 | ||
dfd54cbc HC |
735 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
736 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
737 | #endif | |
008857c1 | 738 | |
a66fd7da JW |
739 | /** |
740 | * __alloc_bootmem_low - allocate low boot memory | |
741 | * @size: size of the request in bytes | |
742 | * @align: alignment of the region | |
743 | * @goal: preferred starting address of the region | |
744 | * | |
745 | * The goal is dropped if it can not be satisfied and the allocation will | |
746 | * fall back to memory below @goal. | |
747 | * | |
748 | * Allocation may happen on any node in the system. | |
749 | * | |
750 | * The function panics if the request can not be satisfied. | |
751 | */ | |
bb0923a6 FBH |
752 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
753 | unsigned long goal) | |
008857c1 | 754 | { |
679bc9fb | 755 | bootmem_data_t *bdata; |
008857c1 RT |
756 | void *ptr; |
757 | ||
f71bf0ca | 758 | list_for_each_entry(bdata, &bdata_list, list) { |
ffc6421f JW |
759 | ptr = alloc_bootmem_core(bdata, size, align, goal, |
760 | ARCH_LOW_ADDRESS_LIMIT); | |
f71bf0ca FBH |
761 | if (ptr) |
762 | return ptr; | |
763 | } | |
008857c1 RT |
764 | |
765 | /* | |
766 | * Whoops, we cannot satisfy the allocation request. | |
767 | */ | |
768 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); | |
769 | panic("Out of low memory"); | |
770 | return NULL; | |
771 | } | |
772 | ||
a66fd7da JW |
773 | /** |
774 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node | |
775 | * @pgdat: node to allocate from | |
776 | * @size: size of the request in bytes | |
777 | * @align: alignment of the region | |
778 | * @goal: preferred starting address of the region | |
779 | * | |
780 | * The goal is dropped if it can not be satisfied and the allocation will | |
781 | * fall back to memory below @goal. | |
782 | * | |
783 | * Allocation may fall back to any node in the system if the specified node | |
784 | * can not hold the requested memory. | |
785 | * | |
786 | * The function panics if the request can not be satisfied. | |
787 | */ | |
008857c1 RT |
788 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
789 | unsigned long align, unsigned long goal) | |
790 | { | |
ffc6421f JW |
791 | return alloc_bootmem_core(pgdat->bdata, size, align, goal, |
792 | ARCH_LOW_ADDRESS_LIMIT); | |
008857c1 | 793 | } |