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> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
1da177e4 | 14 | #include <linux/bootmem.h> |
1da177e4 | 15 | #include <linux/module.h> |
ec3a354b | 16 | #include <linux/kmemleak.h> |
08677214 | 17 | #include <linux/range.h> |
72d7c3b3 | 18 | #include <linux/memblock.h> |
e786e86a FBH |
19 | |
20 | #include <asm/bug.h> | |
1da177e4 | 21 | #include <asm/io.h> |
dfd54cbc | 22 | #include <asm/processor.h> |
e786e86a | 23 | |
1da177e4 LT |
24 | #include "internal.h" |
25 | ||
1da177e4 LT |
26 | unsigned long max_low_pfn; |
27 | unsigned long min_low_pfn; | |
28 | unsigned long max_pfn; | |
29 | ||
92aa63a5 VG |
30 | #ifdef CONFIG_CRASH_DUMP |
31 | /* | |
32 | * If we have booted due to a crash, max_pfn will be a very low value. We need | |
33 | * to know the amount of memory that the previous kernel used. | |
34 | */ | |
35 | unsigned long saved_max_pfn; | |
36 | #endif | |
37 | ||
b61bfa3c JW |
38 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
39 | ||
636cc40c JW |
40 | static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list); |
41 | ||
2e5237da JW |
42 | static int bootmem_debug; |
43 | ||
44 | static int __init bootmem_debug_setup(char *buf) | |
45 | { | |
46 | bootmem_debug = 1; | |
47 | return 0; | |
48 | } | |
49 | early_param("bootmem_debug", bootmem_debug_setup); | |
50 | ||
51 | #define bdebug(fmt, args...) ({ \ | |
52 | if (unlikely(bootmem_debug)) \ | |
53 | printk(KERN_INFO \ | |
54 | "bootmem::%s " fmt, \ | |
80a914dc | 55 | __func__, ## args); \ |
2e5237da JW |
56 | }) |
57 | ||
df049a5f | 58 | static unsigned long __init bootmap_bytes(unsigned long pages) |
223e8dc9 | 59 | { |
df049a5f | 60 | unsigned long bytes = (pages + 7) / 8; |
223e8dc9 | 61 | |
df049a5f | 62 | return ALIGN(bytes, sizeof(long)); |
223e8dc9 JW |
63 | } |
64 | ||
a66fd7da JW |
65 | /** |
66 | * bootmem_bootmap_pages - calculate bitmap size in pages | |
67 | * @pages: number of pages the bitmap has to represent | |
68 | */ | |
f71bf0ca | 69 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
1da177e4 | 70 | { |
df049a5f | 71 | unsigned long bytes = bootmap_bytes(pages); |
1da177e4 | 72 | |
df049a5f | 73 | return PAGE_ALIGN(bytes) >> PAGE_SHIFT; |
1da177e4 | 74 | } |
f71bf0ca | 75 | |
679bc9fb KH |
76 | /* |
77 | * link bdata in order | |
78 | */ | |
69d49e68 | 79 | static void __init link_bootmem(bootmem_data_t *bdata) |
679bc9fb | 80 | { |
636cc40c | 81 | struct list_head *iter; |
f71bf0ca | 82 | |
636cc40c JW |
83 | list_for_each(iter, &bdata_list) { |
84 | bootmem_data_t *ent; | |
85 | ||
86 | ent = list_entry(iter, bootmem_data_t, list); | |
3560e249 | 87 | if (bdata->node_min_pfn < ent->node_min_pfn) |
636cc40c | 88 | break; |
679bc9fb | 89 | } |
636cc40c | 90 | list_add_tail(&bdata->list, iter); |
679bc9fb KH |
91 | } |
92 | ||
1da177e4 LT |
93 | /* |
94 | * Called once to set up the allocator itself. | |
95 | */ | |
8ae04463 | 96 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
1da177e4 LT |
97 | unsigned long mapstart, unsigned long start, unsigned long end) |
98 | { | |
bbc7b92e | 99 | unsigned long mapsize; |
1da177e4 | 100 | |
2dbb51c4 | 101 | mminit_validate_memmodel_limits(&start, &end); |
bbc7b92e | 102 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
3560e249 | 103 | bdata->node_min_pfn = start; |
1da177e4 | 104 | bdata->node_low_pfn = end; |
679bc9fb | 105 | link_bootmem(bdata); |
1da177e4 LT |
106 | |
107 | /* | |
108 | * Initially all pages are reserved - setup_arch() has to | |
109 | * register free RAM areas explicitly. | |
110 | */ | |
df049a5f | 111 | mapsize = bootmap_bytes(end - start); |
1da177e4 LT |
112 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
113 | ||
2e5237da JW |
114 | bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", |
115 | bdata - bootmem_node_data, start, mapstart, end, mapsize); | |
116 | ||
1da177e4 LT |
117 | return mapsize; |
118 | } | |
119 | ||
a66fd7da JW |
120 | /** |
121 | * init_bootmem_node - register a node as boot memory | |
122 | * @pgdat: node to register | |
123 | * @freepfn: pfn where the bitmap for this node is to be placed | |
124 | * @startpfn: first pfn on the node | |
125 | * @endpfn: first pfn after the node | |
126 | * | |
127 | * Returns the number of bytes needed to hold the bitmap for this node. | |
128 | */ | |
223e8dc9 JW |
129 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
130 | unsigned long startpfn, unsigned long endpfn) | |
131 | { | |
132 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); | |
133 | } | |
134 | ||
a66fd7da JW |
135 | /** |
136 | * init_bootmem - register boot memory | |
137 | * @start: pfn where the bitmap is to be placed | |
138 | * @pages: number of available physical pages | |
139 | * | |
140 | * Returns the number of bytes needed to hold the bitmap. | |
141 | */ | |
223e8dc9 JW |
142 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
143 | { | |
144 | max_low_pfn = pages; | |
145 | min_low_pfn = start; | |
146 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); | |
147 | } | |
09325873 | 148 | |
9f993ac3 FT |
149 | /* |
150 | * free_bootmem_late - free bootmem pages directly to page allocator | |
151 | * @addr: starting address of the range | |
152 | * @size: size of the range in bytes | |
153 | * | |
154 | * This is only useful when the bootmem allocator has already been torn | |
155 | * down, but we are still initializing the system. Pages are given directly | |
156 | * to the page allocator, no bootmem metadata is updated because it is gone. | |
157 | */ | |
158 | void __init free_bootmem_late(unsigned long addr, unsigned long size) | |
159 | { | |
160 | unsigned long cursor, end; | |
161 | ||
162 | kmemleak_free_part(__va(addr), size); | |
163 | ||
164 | cursor = PFN_UP(addr); | |
165 | end = PFN_DOWN(addr + size); | |
166 | ||
167 | for (; cursor < end; cursor++) { | |
168 | __free_pages_bootmem(pfn_to_page(cursor), 0); | |
169 | totalram_pages++; | |
170 | } | |
171 | } | |
172 | ||
223e8dc9 JW |
173 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) |
174 | { | |
41546c17 | 175 | int aligned; |
223e8dc9 | 176 | struct page *page; |
41546c17 JW |
177 | unsigned long start, end, pages, count = 0; |
178 | ||
179 | if (!bdata->node_bootmem_map) | |
180 | return 0; | |
181 | ||
3560e249 | 182 | start = bdata->node_min_pfn; |
41546c17 JW |
183 | end = bdata->node_low_pfn; |
184 | ||
223e8dc9 | 185 | /* |
41546c17 JW |
186 | * If the start is aligned to the machines wordsize, we might |
187 | * be able to free pages in bulks of that order. | |
223e8dc9 | 188 | */ |
41546c17 | 189 | aligned = !(start & (BITS_PER_LONG - 1)); |
223e8dc9 | 190 | |
41546c17 JW |
191 | bdebug("nid=%td start=%lx end=%lx aligned=%d\n", |
192 | bdata - bootmem_node_data, start, end, aligned); | |
223e8dc9 | 193 | |
41546c17 JW |
194 | while (start < end) { |
195 | unsigned long *map, idx, vec; | |
223e8dc9 | 196 | |
41546c17 | 197 | map = bdata->node_bootmem_map; |
3560e249 | 198 | idx = start - bdata->node_min_pfn; |
41546c17 JW |
199 | vec = ~map[idx / BITS_PER_LONG]; |
200 | ||
201 | if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) { | |
202 | int order = ilog2(BITS_PER_LONG); | |
203 | ||
204 | __free_pages_bootmem(pfn_to_page(start), order); | |
223e8dc9 | 205 | count += BITS_PER_LONG; |
41546c17 JW |
206 | } else { |
207 | unsigned long off = 0; | |
208 | ||
209 | while (vec && off < BITS_PER_LONG) { | |
210 | if (vec & 1) { | |
211 | page = pfn_to_page(start + off); | |
223e8dc9 | 212 | __free_pages_bootmem(page, 0); |
41546c17 | 213 | count++; |
223e8dc9 | 214 | } |
41546c17 JW |
215 | vec >>= 1; |
216 | off++; | |
223e8dc9 | 217 | } |
223e8dc9 | 218 | } |
41546c17 | 219 | start += BITS_PER_LONG; |
223e8dc9 JW |
220 | } |
221 | ||
223e8dc9 | 222 | page = virt_to_page(bdata->node_bootmem_map); |
3560e249 | 223 | pages = bdata->node_low_pfn - bdata->node_min_pfn; |
41546c17 JW |
224 | pages = bootmem_bootmap_pages(pages); |
225 | count += pages; | |
226 | while (pages--) | |
227 | __free_pages_bootmem(page++, 0); | |
223e8dc9 | 228 | |
2e5237da JW |
229 | bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count); |
230 | ||
223e8dc9 JW |
231 | return count; |
232 | } | |
233 | ||
a66fd7da JW |
234 | /** |
235 | * free_all_bootmem_node - release a node's free pages to the buddy allocator | |
236 | * @pgdat: node to be released | |
237 | * | |
238 | * Returns the number of pages actually released. | |
239 | */ | |
223e8dc9 JW |
240 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
241 | { | |
242 | register_page_bootmem_info_node(pgdat); | |
243 | return free_all_bootmem_core(pgdat->bdata); | |
244 | } | |
245 | ||
a66fd7da JW |
246 | /** |
247 | * free_all_bootmem - release free pages to the buddy allocator | |
248 | * | |
249 | * Returns the number of pages actually released. | |
250 | */ | |
223e8dc9 JW |
251 | unsigned long __init free_all_bootmem(void) |
252 | { | |
aa235fc7 YL |
253 | unsigned long total_pages = 0; |
254 | bootmem_data_t *bdata; | |
255 | ||
256 | list_for_each_entry(bdata, &bdata_list, list) | |
257 | total_pages += free_all_bootmem_core(bdata); | |
258 | ||
259 | return total_pages; | |
223e8dc9 JW |
260 | } |
261 | ||
d747fa4b JW |
262 | static void __init __free(bootmem_data_t *bdata, |
263 | unsigned long sidx, unsigned long eidx) | |
264 | { | |
265 | unsigned long idx; | |
266 | ||
267 | bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data, | |
3560e249 JW |
268 | sidx + bdata->node_min_pfn, |
269 | eidx + bdata->node_min_pfn); | |
d747fa4b | 270 | |
e2bf3cae JW |
271 | if (bdata->hint_idx > sidx) |
272 | bdata->hint_idx = sidx; | |
273 | ||
d747fa4b JW |
274 | for (idx = sidx; idx < eidx; idx++) |
275 | if (!test_and_clear_bit(idx, bdata->node_bootmem_map)) | |
276 | BUG(); | |
277 | } | |
278 | ||
279 | static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx, | |
280 | unsigned long eidx, int flags) | |
281 | { | |
282 | unsigned long idx; | |
283 | int exclusive = flags & BOOTMEM_EXCLUSIVE; | |
284 | ||
285 | bdebug("nid=%td start=%lx end=%lx flags=%x\n", | |
286 | bdata - bootmem_node_data, | |
3560e249 JW |
287 | sidx + bdata->node_min_pfn, |
288 | eidx + bdata->node_min_pfn, | |
d747fa4b JW |
289 | flags); |
290 | ||
291 | for (idx = sidx; idx < eidx; idx++) | |
292 | if (test_and_set_bit(idx, bdata->node_bootmem_map)) { | |
293 | if (exclusive) { | |
294 | __free(bdata, sidx, idx); | |
295 | return -EBUSY; | |
296 | } | |
297 | bdebug("silent double reserve of PFN %lx\n", | |
3560e249 | 298 | idx + bdata->node_min_pfn); |
d747fa4b JW |
299 | } |
300 | return 0; | |
301 | } | |
302 | ||
e2bf3cae JW |
303 | static int __init mark_bootmem_node(bootmem_data_t *bdata, |
304 | unsigned long start, unsigned long end, | |
305 | int reserve, int flags) | |
223e8dc9 JW |
306 | { |
307 | unsigned long sidx, eidx; | |
223e8dc9 | 308 | |
e2bf3cae JW |
309 | bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n", |
310 | bdata - bootmem_node_data, start, end, reserve, flags); | |
223e8dc9 | 311 | |
3560e249 | 312 | BUG_ON(start < bdata->node_min_pfn); |
e2bf3cae | 313 | BUG_ON(end > bdata->node_low_pfn); |
223e8dc9 | 314 | |
3560e249 JW |
315 | sidx = start - bdata->node_min_pfn; |
316 | eidx = end - bdata->node_min_pfn; | |
223e8dc9 | 317 | |
e2bf3cae JW |
318 | if (reserve) |
319 | return __reserve(bdata, sidx, eidx, flags); | |
223e8dc9 | 320 | else |
e2bf3cae JW |
321 | __free(bdata, sidx, eidx); |
322 | return 0; | |
323 | } | |
324 | ||
325 | static int __init mark_bootmem(unsigned long start, unsigned long end, | |
326 | int reserve, int flags) | |
327 | { | |
328 | unsigned long pos; | |
329 | bootmem_data_t *bdata; | |
330 | ||
331 | pos = start; | |
332 | list_for_each_entry(bdata, &bdata_list, list) { | |
333 | int err; | |
334 | unsigned long max; | |
335 | ||
3560e249 JW |
336 | if (pos < bdata->node_min_pfn || |
337 | pos >= bdata->node_low_pfn) { | |
e2bf3cae JW |
338 | BUG_ON(pos != start); |
339 | continue; | |
340 | } | |
341 | ||
342 | max = min(bdata->node_low_pfn, end); | |
223e8dc9 | 343 | |
e2bf3cae JW |
344 | err = mark_bootmem_node(bdata, pos, max, reserve, flags); |
345 | if (reserve && err) { | |
346 | mark_bootmem(start, pos, 0, 0); | |
347 | return err; | |
348 | } | |
223e8dc9 | 349 | |
e2bf3cae JW |
350 | if (max == end) |
351 | return 0; | |
352 | pos = bdata->node_low_pfn; | |
353 | } | |
354 | BUG(); | |
223e8dc9 JW |
355 | } |
356 | ||
a66fd7da JW |
357 | /** |
358 | * free_bootmem_node - mark a page range as usable | |
359 | * @pgdat: node the range resides on | |
360 | * @physaddr: starting address of the range | |
361 | * @size: size of the range in bytes | |
362 | * | |
363 | * Partial pages will be considered reserved and left as they are. | |
364 | * | |
e2bf3cae | 365 | * The range must reside completely on the specified node. |
a66fd7da | 366 | */ |
223e8dc9 JW |
367 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
368 | unsigned long size) | |
369 | { | |
e2bf3cae JW |
370 | unsigned long start, end; |
371 | ||
ec3a354b CM |
372 | kmemleak_free_part(__va(physaddr), size); |
373 | ||
e2bf3cae JW |
374 | start = PFN_UP(physaddr); |
375 | end = PFN_DOWN(physaddr + size); | |
376 | ||
377 | mark_bootmem_node(pgdat->bdata, start, end, 0, 0); | |
223e8dc9 JW |
378 | } |
379 | ||
a66fd7da JW |
380 | /** |
381 | * free_bootmem - mark a page range as usable | |
382 | * @addr: starting address of the range | |
383 | * @size: size of the range in bytes | |
384 | * | |
385 | * Partial pages will be considered reserved and left as they are. | |
386 | * | |
e2bf3cae | 387 | * The range must be contiguous but may span node boundaries. |
a66fd7da | 388 | */ |
223e8dc9 JW |
389 | void __init free_bootmem(unsigned long addr, unsigned long size) |
390 | { | |
e2bf3cae | 391 | unsigned long start, end; |
a5645a61 | 392 | |
ec3a354b CM |
393 | kmemleak_free_part(__va(addr), size); |
394 | ||
e2bf3cae JW |
395 | start = PFN_UP(addr); |
396 | end = PFN_DOWN(addr + size); | |
1da177e4 | 397 | |
e2bf3cae | 398 | mark_bootmem(start, end, 0, 0); |
1da177e4 LT |
399 | } |
400 | ||
a66fd7da JW |
401 | /** |
402 | * reserve_bootmem_node - mark a page range as reserved | |
403 | * @pgdat: node the range resides on | |
404 | * @physaddr: starting address of the range | |
405 | * @size: size of the range in bytes | |
406 | * @flags: reservation flags (see linux/bootmem.h) | |
407 | * | |
408 | * Partial pages will be reserved. | |
409 | * | |
e2bf3cae | 410 | * The range must reside completely on the specified node. |
a66fd7da | 411 | */ |
223e8dc9 JW |
412 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
413 | unsigned long size, int flags) | |
1da177e4 | 414 | { |
e2bf3cae | 415 | unsigned long start, end; |
1da177e4 | 416 | |
e2bf3cae JW |
417 | start = PFN_DOWN(physaddr); |
418 | end = PFN_UP(physaddr + size); | |
419 | ||
420 | return mark_bootmem_node(pgdat->bdata, start, end, 1, flags); | |
223e8dc9 | 421 | } |
5a982cbc | 422 | |
a66fd7da JW |
423 | /** |
424 | * reserve_bootmem - mark a page range as usable | |
425 | * @addr: starting address of the range | |
426 | * @size: size of the range in bytes | |
427 | * @flags: reservation flags (see linux/bootmem.h) | |
428 | * | |
429 | * Partial pages will be reserved. | |
430 | * | |
e2bf3cae | 431 | * The range must be contiguous but may span node boundaries. |
a66fd7da | 432 | */ |
223e8dc9 JW |
433 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
434 | int flags) | |
435 | { | |
e2bf3cae | 436 | unsigned long start, end; |
1da177e4 | 437 | |
e2bf3cae JW |
438 | start = PFN_DOWN(addr); |
439 | end = PFN_UP(addr + size); | |
223e8dc9 | 440 | |
e2bf3cae | 441 | return mark_bootmem(start, end, 1, flags); |
1da177e4 LT |
442 | } |
443 | ||
f88eff74 YL |
444 | int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len, |
445 | int flags) | |
446 | { | |
447 | return reserve_bootmem(phys, len, flags); | |
448 | } | |
449 | ||
8aa043d7 JB |
450 | static unsigned long __init align_idx(struct bootmem_data *bdata, |
451 | unsigned long idx, unsigned long step) | |
481ebd0d JW |
452 | { |
453 | unsigned long base = bdata->node_min_pfn; | |
454 | ||
455 | /* | |
456 | * Align the index with respect to the node start so that the | |
457 | * combination of both satisfies the requested alignment. | |
458 | */ | |
459 | ||
460 | return ALIGN(base + idx, step) - base; | |
461 | } | |
462 | ||
8aa043d7 JB |
463 | static unsigned long __init align_off(struct bootmem_data *bdata, |
464 | unsigned long off, unsigned long align) | |
481ebd0d JW |
465 | { |
466 | unsigned long base = PFN_PHYS(bdata->node_min_pfn); | |
467 | ||
468 | /* Same as align_idx for byte offsets */ | |
469 | ||
470 | return ALIGN(base + off, align) - base; | |
471 | } | |
472 | ||
d0c4f570 TH |
473 | static void * __init alloc_bootmem_core(struct bootmem_data *bdata, |
474 | unsigned long size, unsigned long align, | |
475 | unsigned long goal, unsigned long limit) | |
1da177e4 | 476 | { |
0f3caba2 | 477 | unsigned long fallback = 0; |
5f2809e6 JW |
478 | unsigned long min, max, start, sidx, midx, step; |
479 | ||
594fe1a0 JW |
480 | bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n", |
481 | bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT, | |
482 | align, goal, limit); | |
483 | ||
5f2809e6 JW |
484 | BUG_ON(!size); |
485 | BUG_ON(align & (align - 1)); | |
486 | BUG_ON(limit && goal + size > limit); | |
1da177e4 | 487 | |
7c309a64 CK |
488 | if (!bdata->node_bootmem_map) |
489 | return NULL; | |
490 | ||
3560e249 | 491 | min = bdata->node_min_pfn; |
5f2809e6 | 492 | max = bdata->node_low_pfn; |
9a2dc04c | 493 | |
5f2809e6 JW |
494 | goal >>= PAGE_SHIFT; |
495 | limit >>= PAGE_SHIFT; | |
496 | ||
497 | if (limit && max > limit) | |
498 | max = limit; | |
499 | if (max <= min) | |
9a2dc04c YL |
500 | return NULL; |
501 | ||
5f2809e6 | 502 | step = max(align >> PAGE_SHIFT, 1UL); |
281dd25c | 503 | |
5f2809e6 JW |
504 | if (goal && min < goal && goal < max) |
505 | start = ALIGN(goal, step); | |
506 | else | |
507 | start = ALIGN(min, step); | |
1da177e4 | 508 | |
481ebd0d | 509 | sidx = start - bdata->node_min_pfn; |
3560e249 | 510 | midx = max - bdata->node_min_pfn; |
1da177e4 | 511 | |
5f2809e6 | 512 | if (bdata->hint_idx > sidx) { |
0f3caba2 JW |
513 | /* |
514 | * Handle the valid case of sidx being zero and still | |
515 | * catch the fallback below. | |
516 | */ | |
517 | fallback = sidx + 1; | |
481ebd0d | 518 | sidx = align_idx(bdata, bdata->hint_idx, step); |
5f2809e6 | 519 | } |
1da177e4 | 520 | |
5f2809e6 JW |
521 | while (1) { |
522 | int merge; | |
523 | void *region; | |
524 | unsigned long eidx, i, start_off, end_off; | |
525 | find_block: | |
526 | sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx); | |
481ebd0d | 527 | sidx = align_idx(bdata, sidx, step); |
5f2809e6 | 528 | eidx = sidx + PFN_UP(size); |
ad09315c | 529 | |
5f2809e6 | 530 | if (sidx >= midx || eidx > midx) |
66d43e98 | 531 | break; |
1da177e4 | 532 | |
5f2809e6 JW |
533 | for (i = sidx; i < eidx; i++) |
534 | if (test_bit(i, bdata->node_bootmem_map)) { | |
481ebd0d | 535 | sidx = align_idx(bdata, i, step); |
5f2809e6 JW |
536 | if (sidx == i) |
537 | sidx += step; | |
538 | goto find_block; | |
539 | } | |
1da177e4 | 540 | |
627240aa | 541 | if (bdata->last_end_off & (PAGE_SIZE - 1) && |
5f2809e6 | 542 | PFN_DOWN(bdata->last_end_off) + 1 == sidx) |
481ebd0d | 543 | start_off = align_off(bdata, bdata->last_end_off, align); |
5f2809e6 JW |
544 | else |
545 | start_off = PFN_PHYS(sidx); | |
546 | ||
547 | merge = PFN_DOWN(start_off) < sidx; | |
548 | end_off = start_off + size; | |
549 | ||
550 | bdata->last_end_off = end_off; | |
551 | bdata->hint_idx = PFN_UP(end_off); | |
552 | ||
553 | /* | |
554 | * Reserve the area now: | |
555 | */ | |
d747fa4b JW |
556 | if (__reserve(bdata, PFN_DOWN(start_off) + merge, |
557 | PFN_UP(end_off), BOOTMEM_EXCLUSIVE)) | |
558 | BUG(); | |
5f2809e6 | 559 | |
3560e249 JW |
560 | region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) + |
561 | start_off); | |
5f2809e6 | 562 | memset(region, 0, size); |
008139d9 CM |
563 | /* |
564 | * The min_count is set to 0 so that bootmem allocated blocks | |
565 | * are never reported as leaks. | |
566 | */ | |
567 | kmemleak_alloc(region, size, 0, 0); | |
5f2809e6 | 568 | return region; |
1da177e4 LT |
569 | } |
570 | ||
0f3caba2 | 571 | if (fallback) { |
481ebd0d | 572 | sidx = align_idx(bdata, fallback - 1, step); |
0f3caba2 JW |
573 | fallback = 0; |
574 | goto find_block; | |
575 | } | |
576 | ||
577 | return NULL; | |
578 | } | |
579 | ||
d0c4f570 TH |
580 | static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata, |
581 | unsigned long size, unsigned long align, | |
582 | unsigned long goal, unsigned long limit) | |
583 | { | |
441c7e0a PE |
584 | if (WARN_ON_ONCE(slab_is_available())) |
585 | return kzalloc(size, GFP_NOWAIT); | |
586 | ||
d0c4f570 | 587 | #ifdef CONFIG_HAVE_ARCH_BOOTMEM |
433f13a7 JP |
588 | { |
589 | bootmem_data_t *p_bdata; | |
590 | ||
591 | p_bdata = bootmem_arch_preferred_node(bdata, size, align, | |
592 | goal, limit); | |
593 | if (p_bdata) | |
594 | return alloc_bootmem_core(p_bdata, size, align, | |
595 | goal, limit); | |
596 | } | |
d0c4f570 TH |
597 | #endif |
598 | return NULL; | |
599 | } | |
600 | ||
0f3caba2 JW |
601 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, |
602 | unsigned long align, | |
603 | unsigned long goal, | |
604 | unsigned long limit) | |
605 | { | |
606 | bootmem_data_t *bdata; | |
d0c4f570 | 607 | void *region; |
0f3caba2 JW |
608 | |
609 | restart: | |
d0c4f570 TH |
610 | region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit); |
611 | if (region) | |
612 | return region; | |
0f3caba2 | 613 | |
d0c4f570 | 614 | list_for_each_entry(bdata, &bdata_list, list) { |
0f3caba2 JW |
615 | if (goal && bdata->node_low_pfn <= PFN_DOWN(goal)) |
616 | continue; | |
3560e249 | 617 | if (limit && bdata->node_min_pfn >= PFN_DOWN(limit)) |
0f3caba2 JW |
618 | break; |
619 | ||
620 | region = alloc_bootmem_core(bdata, size, align, goal, limit); | |
621 | if (region) | |
622 | return region; | |
623 | } | |
624 | ||
5f2809e6 JW |
625 | if (goal) { |
626 | goal = 0; | |
0f3caba2 | 627 | goto restart; |
5f2809e6 | 628 | } |
2e5237da | 629 | |
5f2809e6 | 630 | return NULL; |
1da177e4 LT |
631 | } |
632 | ||
a66fd7da JW |
633 | /** |
634 | * __alloc_bootmem_nopanic - allocate boot memory without panicking | |
635 | * @size: size of the request in bytes | |
636 | * @align: alignment of the region | |
637 | * @goal: preferred starting address of the region | |
638 | * | |
639 | * The goal is dropped if it can not be satisfied and the allocation will | |
640 | * fall back to memory below @goal. | |
641 | * | |
642 | * Allocation may happen on any node in the system. | |
643 | * | |
644 | * Returns NULL on failure. | |
645 | */ | |
bb0923a6 | 646 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
0f3caba2 | 647 | unsigned long goal) |
1da177e4 | 648 | { |
08677214 YL |
649 | unsigned long limit = 0; |
650 | ||
08677214 | 651 | return ___alloc_bootmem_nopanic(size, align, goal, limit); |
0f3caba2 | 652 | } |
1da177e4 | 653 | |
0f3caba2 JW |
654 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, |
655 | unsigned long goal, unsigned long limit) | |
656 | { | |
657 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); | |
658 | ||
659 | if (mem) | |
660 | return mem; | |
661 | /* | |
662 | * Whoops, we cannot satisfy the allocation request. | |
663 | */ | |
664 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
665 | panic("Out of memory"); | |
a8062231 AK |
666 | return NULL; |
667 | } | |
1da177e4 | 668 | |
a66fd7da JW |
669 | /** |
670 | * __alloc_bootmem - allocate boot memory | |
671 | * @size: size of the request in bytes | |
672 | * @align: alignment of the region | |
673 | * @goal: preferred starting address of the region | |
674 | * | |
675 | * The goal is dropped if it can not be satisfied and the allocation will | |
676 | * fall back to memory below @goal. | |
677 | * | |
678 | * Allocation may happen on any node in the system. | |
679 | * | |
680 | * The function panics if the request can not be satisfied. | |
681 | */ | |
bb0923a6 FBH |
682 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
683 | unsigned long goal) | |
a8062231 | 684 | { |
08677214 YL |
685 | unsigned long limit = 0; |
686 | ||
08677214 | 687 | return ___alloc_bootmem(size, align, goal, limit); |
1da177e4 LT |
688 | } |
689 | ||
4cc278b7 JW |
690 | static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata, |
691 | unsigned long size, unsigned long align, | |
692 | unsigned long goal, unsigned long limit) | |
693 | { | |
694 | void *ptr; | |
695 | ||
d0c4f570 TH |
696 | ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit); |
697 | if (ptr) | |
698 | return ptr; | |
699 | ||
4cc278b7 JW |
700 | ptr = alloc_bootmem_core(bdata, size, align, goal, limit); |
701 | if (ptr) | |
702 | return ptr; | |
703 | ||
704 | return ___alloc_bootmem(size, align, goal, limit); | |
705 | } | |
706 | ||
a66fd7da JW |
707 | /** |
708 | * __alloc_bootmem_node - allocate boot memory from a specific node | |
709 | * @pgdat: node to allocate from | |
710 | * @size: size of the request in bytes | |
711 | * @align: alignment of the region | |
712 | * @goal: preferred starting address of the region | |
713 | * | |
714 | * The goal is dropped if it can not be satisfied and the allocation will | |
715 | * fall back to memory below @goal. | |
716 | * | |
717 | * Allocation may fall back to any node in the system if the specified node | |
718 | * can not hold the requested memory. | |
719 | * | |
720 | * The function panics if the request can not be satisfied. | |
721 | */ | |
bb0923a6 FBH |
722 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
723 | unsigned long align, unsigned long goal) | |
1da177e4 | 724 | { |
c91c4773 PE |
725 | if (WARN_ON_ONCE(slab_is_available())) |
726 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
727 | ||
09325873 | 728 | return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0); |
08677214 YL |
729 | } |
730 | ||
731 | void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size, | |
732 | unsigned long align, unsigned long goal) | |
733 | { | |
734 | #ifdef MAX_DMA32_PFN | |
735 | unsigned long end_pfn; | |
736 | ||
737 | if (WARN_ON_ONCE(slab_is_available())) | |
738 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
739 | ||
740 | /* update goal according ...MAX_DMA32_PFN */ | |
741 | end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages; | |
742 | ||
743 | if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) && | |
744 | (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) { | |
745 | void *ptr; | |
746 | unsigned long new_goal; | |
747 | ||
748 | new_goal = MAX_DMA32_PFN << PAGE_SHIFT; | |
08677214 YL |
749 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, |
750 | new_goal, 0); | |
08677214 YL |
751 | if (ptr) |
752 | return ptr; | |
753 | } | |
754 | #endif | |
755 | ||
756 | return __alloc_bootmem_node(pgdat, size, align, goal); | |
757 | ||
1da177e4 LT |
758 | } |
759 | ||
e70260aa | 760 | #ifdef CONFIG_SPARSEMEM |
a66fd7da JW |
761 | /** |
762 | * alloc_bootmem_section - allocate boot memory from a specific section | |
763 | * @size: size of the request in bytes | |
764 | * @section_nr: sparse map section to allocate from | |
765 | * | |
766 | * Return NULL on failure. | |
767 | */ | |
e70260aa YG |
768 | void * __init alloc_bootmem_section(unsigned long size, |
769 | unsigned long section_nr) | |
770 | { | |
75a56cfe JW |
771 | bootmem_data_t *bdata; |
772 | unsigned long pfn, goal, limit; | |
e70260aa YG |
773 | |
774 | pfn = section_nr_to_pfn(section_nr); | |
75a56cfe JW |
775 | goal = pfn << PAGE_SHIFT; |
776 | limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT; | |
777 | bdata = &bootmem_node_data[early_pfn_to_nid(pfn)]; | |
e70260aa | 778 | |
75a56cfe | 779 | return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit); |
e70260aa YG |
780 | } |
781 | #endif | |
782 | ||
b54bbf7b AK |
783 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
784 | unsigned long align, unsigned long goal) | |
785 | { | |
786 | void *ptr; | |
787 | ||
c91c4773 PE |
788 | if (WARN_ON_ONCE(slab_is_available())) |
789 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
790 | ||
d0c4f570 TH |
791 | ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0); |
792 | if (ptr) | |
793 | return ptr; | |
794 | ||
b54bbf7b AK |
795 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
796 | if (ptr) | |
797 | return ptr; | |
798 | ||
799 | return __alloc_bootmem_nopanic(size, align, goal); | |
800 | } | |
801 | ||
dfd54cbc HC |
802 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
803 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
804 | #endif | |
008857c1 | 805 | |
a66fd7da JW |
806 | /** |
807 | * __alloc_bootmem_low - allocate low boot memory | |
808 | * @size: size of the request in bytes | |
809 | * @align: alignment of the region | |
810 | * @goal: preferred starting address of the region | |
811 | * | |
812 | * The goal is dropped if it can not be satisfied and the allocation will | |
813 | * fall back to memory below @goal. | |
814 | * | |
815 | * Allocation may happen on any node in the system. | |
816 | * | |
817 | * The function panics if the request can not be satisfied. | |
818 | */ | |
bb0923a6 FBH |
819 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
820 | unsigned long goal) | |
008857c1 | 821 | { |
0f3caba2 | 822 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); |
008857c1 RT |
823 | } |
824 | ||
a66fd7da JW |
825 | /** |
826 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node | |
827 | * @pgdat: node to allocate from | |
828 | * @size: size of the request in bytes | |
829 | * @align: alignment of the region | |
830 | * @goal: preferred starting address of the region | |
831 | * | |
832 | * The goal is dropped if it can not be satisfied and the allocation will | |
833 | * fall back to memory below @goal. | |
834 | * | |
835 | * Allocation may fall back to any node in the system if the specified node | |
836 | * can not hold the requested memory. | |
837 | * | |
838 | * The function panics if the request can not be satisfied. | |
839 | */ | |
008857c1 RT |
840 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
841 | unsigned long align, unsigned long goal) | |
842 | { | |
c91c4773 PE |
843 | if (WARN_ON_ONCE(slab_is_available())) |
844 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
845 | ||
09325873 | 846 | return ___alloc_bootmem_node(pgdat->bdata, size, align, |
b8ab9f82 | 847 | goal, ARCH_LOW_ADDRESS_LIMIT); |
008857c1 | 848 | } |