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