bootmem.c 12 KB

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