bootmem.c 12 KB

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