cma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. struct cma {
  34. unsigned long base_pfn;
  35. unsigned long count;
  36. unsigned long *bitmap;
  37. unsigned int order_per_bit; /* Order of pages represented by one bit */
  38. struct mutex lock;
  39. };
  40. static struct cma cma_areas[MAX_CMA_AREAS];
  41. static unsigned cma_area_count;
  42. static DEFINE_MUTEX(cma_mutex);
  43. phys_addr_t cma_get_base(struct cma *cma)
  44. {
  45. return PFN_PHYS(cma->base_pfn);
  46. }
  47. unsigned long cma_get_size(struct cma *cma)
  48. {
  49. return cma->count << PAGE_SHIFT;
  50. }
  51. static unsigned long cma_bitmap_aligned_mask(struct cma *cma, int align_order)
  52. {
  53. if (align_order <= cma->order_per_bit)
  54. return 0;
  55. return (1UL << (align_order - cma->order_per_bit)) - 1;
  56. }
  57. static unsigned long cma_bitmap_maxno(struct cma *cma)
  58. {
  59. return cma->count >> cma->order_per_bit;
  60. }
  61. static unsigned long cma_bitmap_pages_to_bits(struct cma *cma,
  62. unsigned long pages)
  63. {
  64. return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
  65. }
  66. static void cma_clear_bitmap(struct cma *cma, unsigned long pfn, int count)
  67. {
  68. unsigned long bitmap_no, bitmap_count;
  69. bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
  70. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  71. mutex_lock(&cma->lock);
  72. bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
  73. mutex_unlock(&cma->lock);
  74. }
  75. static int __init cma_activate_area(struct cma *cma)
  76. {
  77. int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
  78. unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
  79. unsigned i = cma->count >> pageblock_order;
  80. struct zone *zone;
  81. cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  82. if (!cma->bitmap)
  83. return -ENOMEM;
  84. WARN_ON_ONCE(!pfn_valid(pfn));
  85. zone = page_zone(pfn_to_page(pfn));
  86. do {
  87. unsigned j;
  88. base_pfn = pfn;
  89. for (j = pageblock_nr_pages; j; --j, pfn++) {
  90. WARN_ON_ONCE(!pfn_valid(pfn));
  91. /*
  92. * alloc_contig_range requires the pfn range
  93. * specified to be in the same zone. Make this
  94. * simple by forcing the entire CMA resv range
  95. * to be in the same zone.
  96. */
  97. if (page_zone(pfn_to_page(pfn)) != zone)
  98. goto err;
  99. }
  100. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  101. } while (--i);
  102. mutex_init(&cma->lock);
  103. return 0;
  104. err:
  105. kfree(cma->bitmap);
  106. return -EINVAL;
  107. }
  108. static int __init cma_init_reserved_areas(void)
  109. {
  110. int i;
  111. for (i = 0; i < cma_area_count; i++) {
  112. int ret = cma_activate_area(&cma_areas[i]);
  113. if (ret)
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. core_initcall(cma_init_reserved_areas);
  119. /**
  120. * cma_init_reserved_mem() - create custom contiguous area from reserved memory
  121. * @base: Base address of the reserved area
  122. * @size: Size of the reserved area (in bytes),
  123. * @order_per_bit: Order of pages represented by one bit on bitmap.
  124. * @res_cma: Pointer to store the created cma region.
  125. *
  126. * This function creates custom contiguous area from already reserved memory.
  127. */
  128. int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
  129. int order_per_bit, struct cma **res_cma)
  130. {
  131. struct cma *cma;
  132. phys_addr_t alignment;
  133. /* Sanity checks */
  134. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  135. pr_err("Not enough slots for CMA reserved regions!\n");
  136. return -ENOSPC;
  137. }
  138. if (!size || !memblock_is_region_reserved(base, size))
  139. return -EINVAL;
  140. /* ensure minimal alignment requied by mm core */
  141. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  142. /* alignment should be aligned with order_per_bit */
  143. if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
  144. return -EINVAL;
  145. if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
  146. return -EINVAL;
  147. /*
  148. * Each reserved area must be initialised later, when more kernel
  149. * subsystems (like slab allocator) are available.
  150. */
  151. cma = &cma_areas[cma_area_count];
  152. cma->base_pfn = PFN_DOWN(base);
  153. cma->count = size >> PAGE_SHIFT;
  154. cma->order_per_bit = order_per_bit;
  155. *res_cma = cma;
  156. cma_area_count++;
  157. return 0;
  158. }
  159. /**
  160. * cma_declare_contiguous() - reserve custom contiguous area
  161. * @base: Base address of the reserved area optional, use 0 for any
  162. * @size: Size of the reserved area (in bytes),
  163. * @limit: End address of the reserved memory (optional, 0 for any).
  164. * @alignment: Alignment for the CMA area, should be power of 2 or zero
  165. * @order_per_bit: Order of pages represented by one bit on bitmap.
  166. * @fixed: hint about where to place the reserved area
  167. * @res_cma: Pointer to store the created cma region.
  168. *
  169. * This function reserves memory from early allocator. It should be
  170. * called by arch specific code once the early allocator (memblock or bootmem)
  171. * has been activated and all other subsystems have already allocated/reserved
  172. * memory. This function allows to create custom reserved areas.
  173. *
  174. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  175. * reserve in range from @base to @limit.
  176. */
  177. int __init cma_declare_contiguous(phys_addr_t base,
  178. phys_addr_t size, phys_addr_t limit,
  179. phys_addr_t alignment, unsigned int order_per_bit,
  180. bool fixed, struct cma **res_cma)
  181. {
  182. phys_addr_t memblock_end = memblock_end_of_DRAM();
  183. phys_addr_t highmem_start = __pa(high_memory);
  184. int ret = 0;
  185. pr_debug("%s(size %lx, base %08lx, limit %08lx alignment %08lx)\n",
  186. __func__, (unsigned long)size, (unsigned long)base,
  187. (unsigned long)limit, (unsigned long)alignment);
  188. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  189. pr_err("Not enough slots for CMA reserved regions!\n");
  190. return -ENOSPC;
  191. }
  192. if (!size)
  193. return -EINVAL;
  194. if (alignment && !is_power_of_2(alignment))
  195. return -EINVAL;
  196. /*
  197. * Sanitise input arguments.
  198. * Pages both ends in CMA area could be merged into adjacent unmovable
  199. * migratetype page by page allocator's buddy algorithm. In the case,
  200. * you couldn't get a contiguous memory, which is not what we want.
  201. */
  202. alignment = max(alignment,
  203. (phys_addr_t)PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order));
  204. base = ALIGN(base, alignment);
  205. size = ALIGN(size, alignment);
  206. limit &= ~(alignment - 1);
  207. /* size should be aligned with order_per_bit */
  208. if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
  209. return -EINVAL;
  210. /*
  211. * adjust limit to avoid crossing low/high memory boundary for
  212. * automatically allocated regions
  213. */
  214. if (((limit == 0 || limit > memblock_end) &&
  215. (memblock_end - size < highmem_start &&
  216. memblock_end > highmem_start)) ||
  217. (!fixed && limit > highmem_start && limit - size < highmem_start)) {
  218. limit = highmem_start;
  219. }
  220. if (fixed && base < highmem_start && base+size > highmem_start) {
  221. ret = -EINVAL;
  222. pr_err("Region at %08lx defined on low/high memory boundary (%08lx)\n",
  223. (unsigned long)base, (unsigned long)highmem_start);
  224. goto err;
  225. }
  226. /* Reserve memory */
  227. if (base && fixed) {
  228. if (memblock_is_region_reserved(base, size) ||
  229. memblock_reserve(base, size) < 0) {
  230. ret = -EBUSY;
  231. goto err;
  232. }
  233. } else {
  234. phys_addr_t addr = memblock_alloc_range(size, alignment, base,
  235. limit);
  236. if (!addr) {
  237. ret = -ENOMEM;
  238. goto err;
  239. } else {
  240. base = addr;
  241. }
  242. }
  243. ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
  244. if (ret)
  245. goto err;
  246. pr_info("Reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
  247. (unsigned long)base);
  248. return 0;
  249. err:
  250. pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  251. return ret;
  252. }
  253. /**
  254. * cma_alloc() - allocate pages from contiguous area
  255. * @cma: Contiguous memory region for which the allocation is performed.
  256. * @count: Requested number of pages.
  257. * @align: Requested alignment of pages (in PAGE_SIZE order).
  258. *
  259. * This function allocates part of contiguous memory on specific
  260. * contiguous memory area.
  261. */
  262. struct page *cma_alloc(struct cma *cma, int count, unsigned int align)
  263. {
  264. unsigned long mask, pfn, start = 0;
  265. unsigned long bitmap_maxno, bitmap_no, bitmap_count;
  266. struct page *page = NULL;
  267. int ret;
  268. if (!cma || !cma->count)
  269. return NULL;
  270. pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
  271. count, align);
  272. if (!count)
  273. return NULL;
  274. mask = cma_bitmap_aligned_mask(cma, align);
  275. bitmap_maxno = cma_bitmap_maxno(cma);
  276. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  277. for (;;) {
  278. mutex_lock(&cma->lock);
  279. bitmap_no = bitmap_find_next_zero_area(cma->bitmap,
  280. bitmap_maxno, start, bitmap_count, mask);
  281. if (bitmap_no >= bitmap_maxno) {
  282. mutex_unlock(&cma->lock);
  283. break;
  284. }
  285. bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
  286. /*
  287. * It's safe to drop the lock here. We've marked this region for
  288. * our exclusive use. If the migration fails we will take the
  289. * lock again and unmark it.
  290. */
  291. mutex_unlock(&cma->lock);
  292. pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
  293. mutex_lock(&cma_mutex);
  294. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  295. mutex_unlock(&cma_mutex);
  296. if (ret == 0) {
  297. page = pfn_to_page(pfn);
  298. break;
  299. }
  300. cma_clear_bitmap(cma, pfn, count);
  301. if (ret != -EBUSY)
  302. break;
  303. pr_debug("%s(): memory range at %p is busy, retrying\n",
  304. __func__, pfn_to_page(pfn));
  305. /* try again with a bit different memory target */
  306. start = bitmap_no + mask + 1;
  307. }
  308. pr_debug("%s(): returned %p\n", __func__, page);
  309. return page;
  310. }
  311. /**
  312. * cma_release() - release allocated pages
  313. * @cma: Contiguous memory region for which the allocation is performed.
  314. * @pages: Allocated pages.
  315. * @count: Number of allocated pages.
  316. *
  317. * This function releases memory allocated by alloc_cma().
  318. * It returns false when provided pages do not belong to contiguous area and
  319. * true otherwise.
  320. */
  321. bool cma_release(struct cma *cma, struct page *pages, int count)
  322. {
  323. unsigned long pfn;
  324. if (!cma || !pages)
  325. return false;
  326. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  327. pfn = page_to_pfn(pages);
  328. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  329. return false;
  330. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  331. free_contig_range(pfn, count);
  332. cma_clear_bitmap(cma, pfn, count);
  333. return true;
  334. }