cma.c 12 KB

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