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