cma.c 15 KB

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