cma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <linux/kmemleak.h>
  36. #include <trace/events/cma.h>
  37. #include "cma.h"
  38. struct cma cma_areas[MAX_CMA_AREAS];
  39. unsigned cma_area_count;
  40. static DEFINE_MUTEX(cma_mutex);
  41. phys_addr_t cma_get_base(const struct cma *cma)
  42. {
  43. return PFN_PHYS(cma->base_pfn);
  44. }
  45. unsigned long cma_get_size(const struct cma *cma)
  46. {
  47. return cma->count << PAGE_SHIFT;
  48. }
  49. const char *cma_get_name(const struct cma *cma)
  50. {
  51. return cma->name ? cma->name : "(undefined)";
  52. }
  53. static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
  54. unsigned int align_order)
  55. {
  56. if (align_order <= cma->order_per_bit)
  57. return 0;
  58. return (1UL << (align_order - cma->order_per_bit)) - 1;
  59. }
  60. /*
  61. * Find the offset of the base PFN from the specified align_order.
  62. * The value returned is represented in order_per_bits.
  63. */
  64. static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
  65. unsigned int align_order)
  66. {
  67. return (cma->base_pfn & ((1UL << align_order) - 1))
  68. >> cma->order_per_bit;
  69. }
  70. static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
  71. unsigned long pages)
  72. {
  73. return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
  74. }
  75. static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
  76. unsigned 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. cma->count = 0;
  94. return -ENOMEM;
  95. }
  96. WARN_ON_ONCE(!pfn_valid(pfn));
  97. zone = page_zone(pfn_to_page(pfn));
  98. do {
  99. unsigned j;
  100. base_pfn = pfn;
  101. for (j = pageblock_nr_pages; j; --j, pfn++) {
  102. WARN_ON_ONCE(!pfn_valid(pfn));
  103. /*
  104. * alloc_contig_range requires the pfn range
  105. * specified to be in the same zone. Make this
  106. * simple by forcing the entire CMA resv range
  107. * to be in the same zone.
  108. */
  109. if (page_zone(pfn_to_page(pfn)) != zone)
  110. goto not_in_zone;
  111. }
  112. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  113. } while (--i);
  114. mutex_init(&cma->lock);
  115. #ifdef CONFIG_CMA_DEBUGFS
  116. INIT_HLIST_HEAD(&cma->mem_head);
  117. spin_lock_init(&cma->mem_head_lock);
  118. #endif
  119. return 0;
  120. not_in_zone:
  121. pr_err("CMA area %s could not be activated\n", cma->name);
  122. kfree(cma->bitmap);
  123. cma->count = 0;
  124. return -EINVAL;
  125. }
  126. static int __init cma_init_reserved_areas(void)
  127. {
  128. int i;
  129. for (i = 0; i < cma_area_count; i++) {
  130. int ret = cma_activate_area(&cma_areas[i]);
  131. if (ret)
  132. return ret;
  133. }
  134. return 0;
  135. }
  136. core_initcall(cma_init_reserved_areas);
  137. /**
  138. * cma_init_reserved_mem() - create custom contiguous area from reserved memory
  139. * @base: Base address of the reserved area
  140. * @size: Size of the reserved area (in bytes),
  141. * @order_per_bit: Order of pages represented by one bit on bitmap.
  142. * @name: The name of the area. If this parameter is NULL, the name of
  143. * the area will be set to "cmaN", where N is a running counter of
  144. * used areas.
  145. * @res_cma: Pointer to store the created cma region.
  146. *
  147. * This function creates custom contiguous area from already reserved memory.
  148. */
  149. int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
  150. unsigned int order_per_bit,
  151. const char *name,
  152. struct cma **res_cma)
  153. {
  154. struct cma *cma;
  155. phys_addr_t alignment;
  156. /* Sanity checks */
  157. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  158. pr_err("Not enough slots for CMA reserved regions!\n");
  159. return -ENOSPC;
  160. }
  161. if (!size || !memblock_is_region_reserved(base, size))
  162. return -EINVAL;
  163. /* ensure minimal alignment required by mm core */
  164. alignment = PAGE_SIZE <<
  165. max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
  166. /* alignment should be aligned with order_per_bit */
  167. if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
  168. return -EINVAL;
  169. if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
  170. return -EINVAL;
  171. /*
  172. * Each reserved area must be initialised later, when more kernel
  173. * subsystems (like slab allocator) are available.
  174. */
  175. cma = &cma_areas[cma_area_count];
  176. if (name) {
  177. cma->name = name;
  178. } else {
  179. cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count);
  180. if (!cma->name)
  181. return -ENOMEM;
  182. }
  183. cma->base_pfn = PFN_DOWN(base);
  184. cma->count = size >> PAGE_SHIFT;
  185. cma->order_per_bit = order_per_bit;
  186. *res_cma = cma;
  187. cma_area_count++;
  188. totalcma_pages += (size / PAGE_SIZE);
  189. return 0;
  190. }
  191. /**
  192. * cma_declare_contiguous() - reserve custom contiguous area
  193. * @base: Base address of the reserved area optional, use 0 for any
  194. * @size: Size of the reserved area (in bytes),
  195. * @limit: End address of the reserved memory (optional, 0 for any).
  196. * @alignment: Alignment for the CMA area, should be power of 2 or zero
  197. * @order_per_bit: Order of pages represented by one bit on bitmap.
  198. * @fixed: hint about where to place the reserved area
  199. * @name: The name of the area. See function cma_init_reserved_mem()
  200. * @res_cma: Pointer to store the created cma region.
  201. *
  202. * This function reserves memory from early allocator. It should be
  203. * called by arch specific code once the early allocator (memblock or bootmem)
  204. * has been activated and all other subsystems have already allocated/reserved
  205. * memory. This function allows to create custom reserved areas.
  206. *
  207. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  208. * reserve in range from @base to @limit.
  209. */
  210. int __init cma_declare_contiguous(phys_addr_t base,
  211. phys_addr_t size, phys_addr_t limit,
  212. phys_addr_t alignment, unsigned int order_per_bit,
  213. bool fixed, const char *name, struct cma **res_cma)
  214. {
  215. phys_addr_t memblock_end = memblock_end_of_DRAM();
  216. phys_addr_t highmem_start;
  217. int ret = 0;
  218. /*
  219. * We can't use __pa(high_memory) directly, since high_memory
  220. * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
  221. * complain. Find the boundary by adding one to the last valid
  222. * address.
  223. */
  224. highmem_start = __pa(high_memory - 1) + 1;
  225. pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
  226. __func__, &size, &base, &limit, &alignment);
  227. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  228. pr_err("Not enough slots for CMA reserved regions!\n");
  229. return -ENOSPC;
  230. }
  231. if (!size)
  232. return -EINVAL;
  233. if (alignment && !is_power_of_2(alignment))
  234. return -EINVAL;
  235. /*
  236. * Sanitise input arguments.
  237. * Pages both ends in CMA area could be merged into adjacent unmovable
  238. * migratetype page by page allocator's buddy algorithm. In the case,
  239. * you couldn't get a contiguous memory, which is not what we want.
  240. */
  241. alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
  242. max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
  243. if (fixed && base & (alignment - 1)) {
  244. ret = -EINVAL;
  245. pr_err("Region at %pa must be aligned to %pa bytes\n",
  246. &base, &alignment);
  247. goto err;
  248. }
  249. base = ALIGN(base, alignment);
  250. size = ALIGN(size, alignment);
  251. limit &= ~(alignment - 1);
  252. if (!base)
  253. fixed = false;
  254. /* size should be aligned with order_per_bit */
  255. if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
  256. return -EINVAL;
  257. /*
  258. * If allocating at a fixed base the request region must not cross the
  259. * low/high memory boundary.
  260. */
  261. if (fixed && base < highmem_start && base + size > highmem_start) {
  262. ret = -EINVAL;
  263. pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
  264. &base, &highmem_start);
  265. goto err;
  266. }
  267. /*
  268. * If the limit is unspecified or above the memblock end, its effective
  269. * value will be the memblock end. Set it explicitly to simplify further
  270. * checks.
  271. */
  272. if (limit == 0 || limit > memblock_end)
  273. limit = memblock_end;
  274. if (base + size > limit) {
  275. ret = -EINVAL;
  276. pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n",
  277. &size, &base, &limit);
  278. goto err;
  279. }
  280. /* Reserve memory */
  281. if (fixed) {
  282. if (memblock_is_region_reserved(base, size) ||
  283. memblock_reserve(base, size) < 0) {
  284. ret = -EBUSY;
  285. goto err;
  286. }
  287. } else {
  288. phys_addr_t addr = 0;
  289. /*
  290. * All pages in the reserved area must come from the same zone.
  291. * If the requested region crosses the low/high memory boundary,
  292. * try allocating from high memory first and fall back to low
  293. * memory in case of failure.
  294. */
  295. if (base < highmem_start && limit > highmem_start) {
  296. addr = memblock_alloc_range(size, alignment,
  297. highmem_start, limit,
  298. MEMBLOCK_NONE);
  299. limit = highmem_start;
  300. }
  301. if (!addr) {
  302. addr = memblock_alloc_range(size, alignment, base,
  303. limit,
  304. MEMBLOCK_NONE);
  305. if (!addr) {
  306. ret = -ENOMEM;
  307. goto err;
  308. }
  309. }
  310. /*
  311. * kmemleak scans/reads tracked objects for pointers to other
  312. * objects but this address isn't mapped and accessible
  313. */
  314. kmemleak_ignore_phys(addr);
  315. base = addr;
  316. }
  317. ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
  318. if (ret)
  319. goto free_mem;
  320. pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
  321. &base);
  322. return 0;
  323. free_mem:
  324. memblock_free(base, size);
  325. err:
  326. pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  327. return ret;
  328. }
  329. #ifdef CONFIG_CMA_DEBUG
  330. static void cma_debug_show_areas(struct cma *cma)
  331. {
  332. unsigned long next_zero_bit, next_set_bit, nr_zero;
  333. unsigned long start = 0;
  334. unsigned long nr_part, nr_total = 0;
  335. unsigned long nbits = cma_bitmap_maxno(cma);
  336. mutex_lock(&cma->lock);
  337. pr_info("number of available pages: ");
  338. for (;;) {
  339. next_zero_bit = find_next_zero_bit(cma->bitmap, nbits, start);
  340. if (next_zero_bit >= nbits)
  341. break;
  342. next_set_bit = find_next_bit(cma->bitmap, nbits, next_zero_bit);
  343. nr_zero = next_set_bit - next_zero_bit;
  344. nr_part = nr_zero << cma->order_per_bit;
  345. pr_cont("%s%lu@%lu", nr_total ? "+" : "", nr_part,
  346. next_zero_bit);
  347. nr_total += nr_part;
  348. start = next_zero_bit + nr_zero;
  349. }
  350. pr_cont("=> %lu free of %lu total pages\n", nr_total, cma->count);
  351. mutex_unlock(&cma->lock);
  352. }
  353. #else
  354. static inline void cma_debug_show_areas(struct cma *cma) { }
  355. #endif
  356. /**
  357. * cma_alloc() - allocate pages from contiguous area
  358. * @cma: Contiguous memory region for which the allocation is performed.
  359. * @count: Requested number of pages.
  360. * @align: Requested alignment of pages (in PAGE_SIZE order).
  361. * @no_warn: Avoid printing message about failed allocation
  362. *
  363. * This function allocates part of contiguous memory on specific
  364. * contiguous memory area.
  365. */
  366. struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
  367. bool no_warn)
  368. {
  369. unsigned long mask, offset;
  370. unsigned long pfn = -1;
  371. unsigned long start = 0;
  372. unsigned long bitmap_maxno, bitmap_no, bitmap_count;
  373. struct page *page = NULL;
  374. int ret = -ENOMEM;
  375. if (!cma || !cma->count)
  376. return NULL;
  377. pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
  378. count, align);
  379. if (!count)
  380. return NULL;
  381. mask = cma_bitmap_aligned_mask(cma, align);
  382. offset = cma_bitmap_aligned_offset(cma, align);
  383. bitmap_maxno = cma_bitmap_maxno(cma);
  384. bitmap_count = cma_bitmap_pages_to_bits(cma, count);
  385. if (bitmap_count > bitmap_maxno)
  386. return NULL;
  387. for (;;) {
  388. mutex_lock(&cma->lock);
  389. bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
  390. bitmap_maxno, start, bitmap_count, mask,
  391. offset);
  392. if (bitmap_no >= bitmap_maxno) {
  393. mutex_unlock(&cma->lock);
  394. break;
  395. }
  396. bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
  397. /*
  398. * It's safe to drop the lock here. We've marked this region for
  399. * our exclusive use. If the migration fails we will take the
  400. * lock again and unmark it.
  401. */
  402. mutex_unlock(&cma->lock);
  403. pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
  404. mutex_lock(&cma_mutex);
  405. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
  406. GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
  407. mutex_unlock(&cma_mutex);
  408. if (ret == 0) {
  409. page = pfn_to_page(pfn);
  410. break;
  411. }
  412. cma_clear_bitmap(cma, pfn, count);
  413. if (ret != -EBUSY)
  414. break;
  415. pr_debug("%s(): memory range at %p is busy, retrying\n",
  416. __func__, pfn_to_page(pfn));
  417. /* try again with a bit different memory target */
  418. start = bitmap_no + mask + 1;
  419. }
  420. trace_cma_alloc(pfn, page, count, align);
  421. if (ret && !no_warn) {
  422. pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n",
  423. __func__, count, ret);
  424. cma_debug_show_areas(cma);
  425. }
  426. pr_debug("%s(): returned %p\n", __func__, page);
  427. return page;
  428. }
  429. /**
  430. * cma_release() - release allocated pages
  431. * @cma: Contiguous memory region for which the allocation is performed.
  432. * @pages: Allocated pages.
  433. * @count: Number of allocated pages.
  434. *
  435. * This function releases memory allocated by alloc_cma().
  436. * It returns false when provided pages do not belong to contiguous area and
  437. * true otherwise.
  438. */
  439. bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
  440. {
  441. unsigned long pfn;
  442. if (!cma || !pages)
  443. return false;
  444. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  445. pfn = page_to_pfn(pages);
  446. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  447. return false;
  448. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  449. free_contig_range(pfn, count);
  450. cma_clear_bitmap(cma, pfn, count);
  451. trace_cma_release(pfn, pages, count);
  452. return true;
  453. }
  454. int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
  455. {
  456. int i;
  457. for (i = 0; i < cma_area_count; i++) {
  458. int ret = it(&cma_areas[i], data);
  459. if (ret)
  460. return ret;
  461. }
  462. return 0;
  463. }