cma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. cma->count = 0;
  107. return -EINVAL;
  108. }
  109. static int __init cma_init_reserved_areas(void)
  110. {
  111. int i;
  112. for (i = 0; i < cma_area_count; i++) {
  113. int ret = cma_activate_area(&cma_areas[i]);
  114. if (ret)
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. core_initcall(cma_init_reserved_areas);
  120. /**
  121. * cma_init_reserved_mem() - create custom contiguous area from reserved memory
  122. * @base: Base address of the reserved area
  123. * @size: Size of the reserved area (in bytes),
  124. * @order_per_bit: Order of pages represented by one bit on bitmap.
  125. * @res_cma: Pointer to store the created cma region.
  126. *
  127. * This function creates custom contiguous area from already reserved memory.
  128. */
  129. int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
  130. int order_per_bit, struct cma **res_cma)
  131. {
  132. struct cma *cma;
  133. phys_addr_t alignment;
  134. /* Sanity checks */
  135. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  136. pr_err("Not enough slots for CMA reserved regions!\n");
  137. return -ENOSPC;
  138. }
  139. if (!size || !memblock_is_region_reserved(base, size))
  140. return -EINVAL;
  141. /* ensure minimal alignment requied by mm core */
  142. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  143. /* alignment should be aligned with order_per_bit */
  144. if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
  145. return -EINVAL;
  146. if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
  147. return -EINVAL;
  148. /*
  149. * Each reserved area must be initialised later, when more kernel
  150. * subsystems (like slab allocator) are available.
  151. */
  152. cma = &cma_areas[cma_area_count];
  153. cma->base_pfn = PFN_DOWN(base);
  154. cma->count = size >> PAGE_SHIFT;
  155. cma->order_per_bit = order_per_bit;
  156. *res_cma = cma;
  157. cma_area_count++;
  158. return 0;
  159. }
  160. /**
  161. * cma_declare_contiguous() - reserve custom contiguous area
  162. * @base: Base address of the reserved area optional, use 0 for any
  163. * @size: Size of the reserved area (in bytes),
  164. * @limit: End address of the reserved memory (optional, 0 for any).
  165. * @alignment: Alignment for the CMA area, should be power of 2 or zero
  166. * @order_per_bit: Order of pages represented by one bit on bitmap.
  167. * @fixed: hint about where to place the reserved area
  168. * @res_cma: Pointer to store the created cma region.
  169. *
  170. * This function reserves memory from early allocator. It should be
  171. * called by arch specific code once the early allocator (memblock or bootmem)
  172. * has been activated and all other subsystems have already allocated/reserved
  173. * memory. This function allows to create custom reserved areas.
  174. *
  175. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  176. * reserve in range from @base to @limit.
  177. */
  178. int __init cma_declare_contiguous(phys_addr_t base,
  179. phys_addr_t size, phys_addr_t limit,
  180. phys_addr_t alignment, unsigned int order_per_bit,
  181. bool fixed, struct cma **res_cma)
  182. {
  183. phys_addr_t memblock_end = memblock_end_of_DRAM();
  184. phys_addr_t highmem_start = __pa(high_memory);
  185. int ret = 0;
  186. pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
  187. __func__, &size, &base, &limit, &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. if (!base)
  208. fixed = false;
  209. /* size should be aligned with order_per_bit */
  210. if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
  211. return -EINVAL;
  212. /*
  213. * If allocating at a fixed base the request region must not cross the
  214. * low/high memory boundary.
  215. */
  216. if (fixed && base < highmem_start && base + size > highmem_start) {
  217. ret = -EINVAL;
  218. pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
  219. &base, &highmem_start);
  220. goto err;
  221. }
  222. /*
  223. * If the limit is unspecified or above the memblock end, its effective
  224. * value will be the memblock end. Set it explicitly to simplify further
  225. * checks.
  226. */
  227. if (limit == 0 || limit > memblock_end)
  228. limit = memblock_end;
  229. /* Reserve memory */
  230. if (fixed) {
  231. if (memblock_is_region_reserved(base, size) ||
  232. memblock_reserve(base, size) < 0) {
  233. ret = -EBUSY;
  234. goto err;
  235. }
  236. } else {
  237. phys_addr_t addr = 0;
  238. /*
  239. * All pages in the reserved area must come from the same zone.
  240. * If the requested region crosses the low/high memory boundary,
  241. * try allocating from high memory first and fall back to low
  242. * memory in case of failure.
  243. */
  244. if (base < highmem_start && limit > highmem_start) {
  245. addr = memblock_alloc_range(size, alignment,
  246. highmem_start, limit);
  247. limit = highmem_start;
  248. }
  249. if (!addr) {
  250. addr = memblock_alloc_range(size, alignment, base,
  251. limit);
  252. if (!addr) {
  253. ret = -ENOMEM;
  254. goto err;
  255. }
  256. }
  257. base = addr;
  258. }
  259. ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
  260. if (ret)
  261. goto err;
  262. pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
  263. &base);
  264. return 0;
  265. err:
  266. pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  267. return ret;
  268. }
  269. /**
  270. * cma_alloc() - allocate pages from contiguous area
  271. * @cma: Contiguous memory region for which the allocation is performed.
  272. * @count: Requested number of pages.
  273. * @align: Requested alignment of pages (in PAGE_SIZE order).
  274. *
  275. * This function allocates part of contiguous memory on specific
  276. * contiguous memory area.
  277. */
  278. struct page *cma_alloc(struct cma *cma, int count, unsigned int align)
  279. {
  280. unsigned long mask, pfn, start = 0;
  281. unsigned long bitmap_maxno, bitmap_no, bitmap_count;
  282. struct page *page = NULL;
  283. int ret;
  284. if (!cma || !cma->count)
  285. return NULL;
  286. pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
  287. count, align);
  288. if (!count)
  289. return NULL;
  290. mask = cma_bitmap_aligned_mask(cma, align);
  291. bitmap_maxno = cma_bitmap_maxno(cma);
  292. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  293. for (;;) {
  294. mutex_lock(&cma->lock);
  295. bitmap_no = bitmap_find_next_zero_area(cma->bitmap,
  296. bitmap_maxno, start, bitmap_count, mask);
  297. if (bitmap_no >= bitmap_maxno) {
  298. mutex_unlock(&cma->lock);
  299. break;
  300. }
  301. bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
  302. /*
  303. * It's safe to drop the lock here. We've marked this region for
  304. * our exclusive use. If the migration fails we will take the
  305. * lock again and unmark it.
  306. */
  307. mutex_unlock(&cma->lock);
  308. pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
  309. mutex_lock(&cma_mutex);
  310. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  311. mutex_unlock(&cma_mutex);
  312. if (ret == 0) {
  313. page = pfn_to_page(pfn);
  314. break;
  315. }
  316. cma_clear_bitmap(cma, pfn, count);
  317. if (ret != -EBUSY)
  318. break;
  319. pr_debug("%s(): memory range at %p is busy, retrying\n",
  320. __func__, pfn_to_page(pfn));
  321. /* try again with a bit different memory target */
  322. start = bitmap_no + mask + 1;
  323. }
  324. pr_debug("%s(): returned %p\n", __func__, page);
  325. return page;
  326. }
  327. /**
  328. * cma_release() - release allocated pages
  329. * @cma: Contiguous memory region for which the allocation is performed.
  330. * @pages: Allocated pages.
  331. * @count: Number of allocated pages.
  332. *
  333. * This function releases memory allocated by alloc_cma().
  334. * It returns false when provided pages do not belong to contiguous area and
  335. * true otherwise.
  336. */
  337. bool cma_release(struct cma *cma, struct page *pages, int count)
  338. {
  339. unsigned long pfn;
  340. if (!cma || !pages)
  341. return false;
  342. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  343. pfn = page_to_pfn(pages);
  344. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  345. return false;
  346. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  347. free_contig_range(pfn, count);
  348. cma_clear_bitmap(cma, pfn, count);
  349. return true;
  350. }