cma.c 8.7 KB

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