cma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Contiguous Memory Allocator
  3. *
  4. * Copyright (c) 2010-2011 by Samsung Electronics.
  5. * Copyright IBM Corporation, 2013
  6. * Copyright LG Electronics Inc., 2014
  7. * Written by:
  8. * Marek Szyprowski <m.szyprowski@samsung.com>
  9. * Michal Nazarewicz <mina86@mina86.com>
  10. * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  11. * Joonsoo Kim <iamjoonsoo.kim@lge.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of the
  16. * License or (at your optional) any later version of the license.
  17. */
  18. #define pr_fmt(fmt) "cma: " fmt
  19. #ifdef CONFIG_CMA_DEBUG
  20. #ifndef DEBUG
  21. # define DEBUG
  22. #endif
  23. #endif
  24. #include <linux/memblock.h>
  25. #include <linux/err.h>
  26. #include <linux/mm.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sizes.h>
  29. #include <linux/slab.h>
  30. #include <linux/log2.h>
  31. #include <linux/cma.h>
  32. #include <linux/highmem.h>
  33. #include <linux/io.h>
  34. struct cma {
  35. unsigned long base_pfn;
  36. unsigned long count;
  37. unsigned long *bitmap;
  38. unsigned int order_per_bit; /* Order of pages represented by one bit */
  39. struct mutex lock;
  40. };
  41. static struct cma cma_areas[MAX_CMA_AREAS];
  42. static unsigned cma_area_count;
  43. static DEFINE_MUTEX(cma_mutex);
  44. phys_addr_t cma_get_base(struct cma *cma)
  45. {
  46. return PFN_PHYS(cma->base_pfn);
  47. }
  48. unsigned long cma_get_size(struct cma *cma)
  49. {
  50. return cma->count << PAGE_SHIFT;
  51. }
  52. static unsigned long cma_bitmap_aligned_mask(struct cma *cma, int align_order)
  53. {
  54. if (align_order <= cma->order_per_bit)
  55. return 0;
  56. return (1UL << (align_order - cma->order_per_bit)) - 1;
  57. }
  58. static unsigned long cma_bitmap_aligned_offset(struct cma *cma, int align_order)
  59. {
  60. unsigned int alignment;
  61. if (align_order <= cma->order_per_bit)
  62. return 0;
  63. alignment = 1UL << (align_order - cma->order_per_bit);
  64. return ALIGN(cma->base_pfn, alignment) -
  65. (cma->base_pfn >> cma->order_per_bit);
  66. }
  67. static unsigned long cma_bitmap_maxno(struct cma *cma)
  68. {
  69. return cma->count >> cma->order_per_bit;
  70. }
  71. static unsigned long cma_bitmap_pages_to_bits(struct cma *cma,
  72. unsigned long pages)
  73. {
  74. return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
  75. }
  76. static void cma_clear_bitmap(struct cma *cma, unsigned long pfn, int count)
  77. {
  78. unsigned long bitmap_no, bitmap_count;
  79. bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
  80. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  81. mutex_lock(&cma->lock);
  82. bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
  83. mutex_unlock(&cma->lock);
  84. }
  85. static int __init cma_activate_area(struct cma *cma)
  86. {
  87. int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
  88. unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
  89. unsigned i = cma->count >> pageblock_order;
  90. struct zone *zone;
  91. cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  92. if (!cma->bitmap)
  93. return -ENOMEM;
  94. WARN_ON_ONCE(!pfn_valid(pfn));
  95. zone = page_zone(pfn_to_page(pfn));
  96. do {
  97. unsigned j;
  98. base_pfn = pfn;
  99. for (j = pageblock_nr_pages; j; --j, pfn++) {
  100. WARN_ON_ONCE(!pfn_valid(pfn));
  101. /*
  102. * alloc_contig_range requires the pfn range
  103. * specified to be in the same zone. Make this
  104. * simple by forcing the entire CMA resv range
  105. * to be in the same zone.
  106. */
  107. if (page_zone(pfn_to_page(pfn)) != zone)
  108. goto err;
  109. }
  110. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  111. } while (--i);
  112. mutex_init(&cma->lock);
  113. return 0;
  114. err:
  115. kfree(cma->bitmap);
  116. cma->count = 0;
  117. return -EINVAL;
  118. }
  119. static int __init cma_init_reserved_areas(void)
  120. {
  121. int i;
  122. for (i = 0; i < cma_area_count; i++) {
  123. int ret = cma_activate_area(&cma_areas[i]);
  124. if (ret)
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. core_initcall(cma_init_reserved_areas);
  130. /**
  131. * cma_init_reserved_mem() - create custom contiguous area from reserved memory
  132. * @base: Base address of the reserved area
  133. * @size: Size of the reserved area (in bytes),
  134. * @order_per_bit: Order of pages represented by one bit on bitmap.
  135. * @res_cma: Pointer to store the created cma region.
  136. *
  137. * This function creates custom contiguous area from already reserved memory.
  138. */
  139. int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
  140. int order_per_bit, struct cma **res_cma)
  141. {
  142. struct cma *cma;
  143. phys_addr_t alignment;
  144. /* Sanity checks */
  145. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  146. pr_err("Not enough slots for CMA reserved regions!\n");
  147. return -ENOSPC;
  148. }
  149. if (!size || !memblock_is_region_reserved(base, size))
  150. return -EINVAL;
  151. /* ensure minimal alignment requied by mm core */
  152. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  153. /* alignment should be aligned with order_per_bit */
  154. if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
  155. return -EINVAL;
  156. if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
  157. return -EINVAL;
  158. /*
  159. * Each reserved area must be initialised later, when more kernel
  160. * subsystems (like slab allocator) are available.
  161. */
  162. cma = &cma_areas[cma_area_count];
  163. cma->base_pfn = PFN_DOWN(base);
  164. cma->count = size >> PAGE_SHIFT;
  165. cma->order_per_bit = order_per_bit;
  166. *res_cma = cma;
  167. cma_area_count++;
  168. return 0;
  169. }
  170. /**
  171. * cma_declare_contiguous() - reserve custom contiguous area
  172. * @base: Base address of the reserved area optional, use 0 for any
  173. * @size: Size of the reserved area (in bytes),
  174. * @limit: End address of the reserved memory (optional, 0 for any).
  175. * @alignment: Alignment for the CMA area, should be power of 2 or zero
  176. * @order_per_bit: Order of pages represented by one bit on bitmap.
  177. * @fixed: hint about where to place the reserved area
  178. * @res_cma: Pointer to store the created cma region.
  179. *
  180. * This function reserves memory from early allocator. It should be
  181. * called by arch specific code once the early allocator (memblock or bootmem)
  182. * has been activated and all other subsystems have already allocated/reserved
  183. * memory. This function allows to create custom reserved areas.
  184. *
  185. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  186. * reserve in range from @base to @limit.
  187. */
  188. int __init cma_declare_contiguous(phys_addr_t base,
  189. phys_addr_t size, phys_addr_t limit,
  190. phys_addr_t alignment, unsigned int order_per_bit,
  191. bool fixed, struct cma **res_cma)
  192. {
  193. phys_addr_t memblock_end = memblock_end_of_DRAM();
  194. phys_addr_t highmem_start;
  195. int ret = 0;
  196. #ifdef CONFIG_X86
  197. /*
  198. * high_memory isn't direct mapped memory so retrieving its physical
  199. * address isn't appropriate. But it would be useful to check the
  200. * physical address of the highmem boundary so it's justfiable to get
  201. * the physical address from it. On x86 there is a validation check for
  202. * this case, so the following workaround is needed to avoid it.
  203. */
  204. highmem_start = __pa_nodebug(high_memory);
  205. #else
  206. highmem_start = __pa(high_memory);
  207. #endif
  208. pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
  209. __func__, &size, &base, &limit, &alignment);
  210. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  211. pr_err("Not enough slots for CMA reserved regions!\n");
  212. return -ENOSPC;
  213. }
  214. if (!size)
  215. return -EINVAL;
  216. if (alignment && !is_power_of_2(alignment))
  217. return -EINVAL;
  218. /*
  219. * Sanitise input arguments.
  220. * Pages both ends in CMA area could be merged into adjacent unmovable
  221. * migratetype page by page allocator's buddy algorithm. In the case,
  222. * you couldn't get a contiguous memory, which is not what we want.
  223. */
  224. alignment = max(alignment,
  225. (phys_addr_t)PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order));
  226. base = ALIGN(base, alignment);
  227. size = ALIGN(size, alignment);
  228. limit &= ~(alignment - 1);
  229. if (!base)
  230. fixed = false;
  231. /* size should be aligned with order_per_bit */
  232. if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
  233. return -EINVAL;
  234. /*
  235. * If allocating at a fixed base the request region must not cross the
  236. * low/high memory boundary.
  237. */
  238. if (fixed && base < highmem_start && base + size > highmem_start) {
  239. ret = -EINVAL;
  240. pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
  241. &base, &highmem_start);
  242. goto err;
  243. }
  244. /*
  245. * If the limit is unspecified or above the memblock end, its effective
  246. * value will be the memblock end. Set it explicitly to simplify further
  247. * checks.
  248. */
  249. if (limit == 0 || limit > memblock_end)
  250. limit = memblock_end;
  251. /* Reserve memory */
  252. if (fixed) {
  253. if (memblock_is_region_reserved(base, size) ||
  254. memblock_reserve(base, size) < 0) {
  255. ret = -EBUSY;
  256. goto err;
  257. }
  258. } else {
  259. phys_addr_t addr = 0;
  260. /*
  261. * All pages in the reserved area must come from the same zone.
  262. * If the requested region crosses the low/high memory boundary,
  263. * try allocating from high memory first and fall back to low
  264. * memory in case of failure.
  265. */
  266. if (base < highmem_start && limit > highmem_start) {
  267. addr = memblock_alloc_range(size, alignment,
  268. highmem_start, limit);
  269. limit = highmem_start;
  270. }
  271. if (!addr) {
  272. addr = memblock_alloc_range(size, alignment, base,
  273. limit);
  274. if (!addr) {
  275. ret = -ENOMEM;
  276. goto err;
  277. }
  278. }
  279. /*
  280. * kmemleak scans/reads tracked objects for pointers to other
  281. * objects but this address isn't mapped and accessible
  282. */
  283. kmemleak_ignore(phys_to_virt(addr));
  284. base = addr;
  285. }
  286. ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
  287. if (ret)
  288. goto err;
  289. totalcma_pages += (size / PAGE_SIZE);
  290. pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
  291. &base);
  292. return 0;
  293. err:
  294. pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  295. return ret;
  296. }
  297. /**
  298. * cma_alloc() - allocate pages from contiguous area
  299. * @cma: Contiguous memory region for which the allocation is performed.
  300. * @count: Requested number of pages.
  301. * @align: Requested alignment of pages (in PAGE_SIZE order).
  302. *
  303. * This function allocates part of contiguous memory on specific
  304. * contiguous memory area.
  305. */
  306. struct page *cma_alloc(struct cma *cma, int count, unsigned int align)
  307. {
  308. unsigned long mask, offset, pfn, start = 0;
  309. unsigned long bitmap_maxno, bitmap_no, bitmap_count;
  310. struct page *page = NULL;
  311. int ret;
  312. if (!cma || !cma->count)
  313. return NULL;
  314. pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
  315. count, align);
  316. if (!count)
  317. return NULL;
  318. mask = cma_bitmap_aligned_mask(cma, align);
  319. offset = cma_bitmap_aligned_offset(cma, align);
  320. bitmap_maxno = cma_bitmap_maxno(cma);
  321. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  322. for (;;) {
  323. mutex_lock(&cma->lock);
  324. bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
  325. bitmap_maxno, start, bitmap_count, mask,
  326. offset);
  327. if (bitmap_no >= bitmap_maxno) {
  328. mutex_unlock(&cma->lock);
  329. break;
  330. }
  331. bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
  332. /*
  333. * It's safe to drop the lock here. We've marked this region for
  334. * our exclusive use. If the migration fails we will take the
  335. * lock again and unmark it.
  336. */
  337. mutex_unlock(&cma->lock);
  338. pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
  339. mutex_lock(&cma_mutex);
  340. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  341. mutex_unlock(&cma_mutex);
  342. if (ret == 0) {
  343. page = pfn_to_page(pfn);
  344. break;
  345. }
  346. cma_clear_bitmap(cma, pfn, count);
  347. if (ret != -EBUSY)
  348. break;
  349. pr_debug("%s(): memory range at %p is busy, retrying\n",
  350. __func__, pfn_to_page(pfn));
  351. /* try again with a bit different memory target */
  352. start = bitmap_no + mask + 1;
  353. }
  354. pr_debug("%s(): returned %p\n", __func__, page);
  355. return page;
  356. }
  357. /**
  358. * cma_release() - release allocated pages
  359. * @cma: Contiguous memory region for which the allocation is performed.
  360. * @pages: Allocated pages.
  361. * @count: Number of allocated pages.
  362. *
  363. * This function releases memory allocated by alloc_cma().
  364. * It returns false when provided pages do not belong to contiguous area and
  365. * true otherwise.
  366. */
  367. bool cma_release(struct cma *cma, struct page *pages, int count)
  368. {
  369. unsigned long pfn;
  370. if (!cma || !pages)
  371. return false;
  372. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  373. pfn = page_to_pfn(pages);
  374. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  375. return false;
  376. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  377. free_contig_range(pfn, count);
  378. cma_clear_bitmap(cma, pfn, count);
  379. return true;
  380. }