blob: e8292ee5fe1ff749695c491b5413a5e7778f8297 [file] [log] [blame]
Joonsoo Kima2541292014-08-06 16:05:25 -07001/*
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
19#define pr_fmt(fmt) "cma: " fmt
20
21#ifdef CONFIG_CMA_DEBUG
22#ifndef DEBUG
23# define DEBUG
24#endif
25#endif
Stefan Strogin99e8ea62015-04-15 16:14:50 -070026#define CREATE_TRACE_POINTS
Joonsoo Kima2541292014-08-06 16:05:25 -070027
28#include <linux/memblock.h>
29#include <linux/err.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/sizes.h>
33#include <linux/slab.h>
34#include <linux/log2.h>
35#include <linux/cma.h>
Marek Szyprowskif7426b92014-10-09 15:26:47 -070036#include <linux/highmem.h>
Thierry Reding620951e2014-12-12 16:58:31 -080037#include <linux/io.h>
Liam Mark51934022015-10-23 12:14:11 -070038#include <linux/delay.h>
Prakash Guptaa0d60fa2017-08-02 12:30:55 +053039#include <linux/show_mem_notifier.h>
Stefan Strogin99e8ea62015-04-15 16:14:50 -070040#include <trace/events/cma.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070041
Sasha Levin28b24c12015-04-14 15:44:57 -070042#include "cma.h"
Joonsoo Kima2541292014-08-06 16:05:25 -070043
Sasha Levin28b24c12015-04-14 15:44:57 -070044struct cma cma_areas[MAX_CMA_AREAS];
45unsigned cma_area_count;
Joonsoo Kima2541292014-08-06 16:05:25 -070046static DEFINE_MUTEX(cma_mutex);
47
Sasha Levinac173822015-04-14 15:47:04 -070048phys_addr_t cma_get_base(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070049{
50 return PFN_PHYS(cma->base_pfn);
51}
Asish Bhattacharyadc753712019-11-12 21:49:58 +053052EXPORT_SYMBOL(cma_get_base);
Joonsoo Kima2541292014-08-06 16:05:25 -070053
Sasha Levinac173822015-04-14 15:47:04 -070054unsigned long cma_get_size(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070055{
56 return cma->count << PAGE_SHIFT;
57}
Asish Bhattacharyadc753712019-11-12 21:49:58 +053058EXPORT_SYMBOL(cma_get_size);
Joonsoo Kima2541292014-08-06 16:05:25 -070059
Laura Abbott77dbb1d2017-04-18 11:27:03 -070060const char *cma_get_name(const struct cma *cma)
61{
62 return cma->name ? cma->name : "(undefined)";
63}
64
Sasha Levinac173822015-04-14 15:47:04 -070065static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
Doug Berger714c19e2017-07-10 15:49:44 -070066 unsigned int align_order)
Joonsoo Kima2541292014-08-06 16:05:25 -070067{
Weijie Yang68faed62014-10-13 15:51:03 -070068 if (align_order <= cma->order_per_bit)
69 return 0;
70 return (1UL << (align_order - cma->order_per_bit)) - 1;
Joonsoo Kima2541292014-08-06 16:05:25 -070071}
72
Danesh Petigara850fc432015-03-12 16:25:57 -070073/*
Doug Berger714c19e2017-07-10 15:49:44 -070074 * Find the offset of the base PFN from the specified align_order.
75 * The value returned is represented in order_per_bits.
Danesh Petigara850fc432015-03-12 16:25:57 -070076 */
Sasha Levinac173822015-04-14 15:47:04 -070077static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
Doug Berger714c19e2017-07-10 15:49:44 -070078 unsigned int align_order)
Gregory Fongb5be83e2014-12-12 16:54:48 -080079{
Doug Berger714c19e2017-07-10 15:49:44 -070080 return (cma->base_pfn & ((1UL << align_order) - 1))
81 >> cma->order_per_bit;
Gregory Fongb5be83e2014-12-12 16:54:48 -080082}
83
Sasha Levinac173822015-04-14 15:47:04 -070084static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
85 unsigned long pages)
Joonsoo Kima2541292014-08-06 16:05:25 -070086{
87 return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
88}
89
Sasha Levinac173822015-04-14 15:47:04 -070090static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
91 unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -070092{
93 unsigned long bitmap_no, bitmap_count;
94
95 bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
96 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
97
98 mutex_lock(&cma->lock);
99 bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
100 mutex_unlock(&cma->lock);
101}
102
Prakash Guptaa0d60fa2017-08-02 12:30:55 +0530103static int cma_showmem_notifier(struct notifier_block *nb,
104 unsigned long action, void *data)
105{
106 int i;
107 unsigned long used;
108 struct cma *cma;
109
110 for (i = 0; i < cma_area_count; i++) {
111 cma = &cma_areas[i];
112 used = bitmap_weight(cma->bitmap,
113 (int)cma_bitmap_maxno(cma));
114 used <<= cma->order_per_bit;
115 pr_info("cma-%d pages: => %lu used of %lu total pages\n",
116 i, used, cma->count);
117 }
118
119 return 0;
120}
121
122static struct notifier_block cma_nb = {
123 .notifier_call = cma_showmem_notifier,
124};
125
Joonsoo Kima2541292014-08-06 16:05:25 -0700126static int __init cma_activate_area(struct cma *cma)
127{
128 int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
129 unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
130 unsigned i = cma->count >> pageblock_order;
131 struct zone *zone;
132
133 cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
134
Yue Hu8f759452019-05-13 17:18:14 -0700135 if (!cma->bitmap) {
136 cma->count = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700137 return -ENOMEM;
Yue Hu8f759452019-05-13 17:18:14 -0700138 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700139
140 WARN_ON_ONCE(!pfn_valid(pfn));
141 zone = page_zone(pfn_to_page(pfn));
142
143 do {
144 unsigned j;
145
146 base_pfn = pfn;
147 for (j = pageblock_nr_pages; j; --j, pfn++) {
148 WARN_ON_ONCE(!pfn_valid(pfn));
149 /*
150 * alloc_contig_range requires the pfn range
151 * specified to be in the same zone. Make this
152 * simple by forcing the entire CMA resv range
153 * to be in the same zone.
154 */
155 if (page_zone(pfn_to_page(pfn)) != zone)
156 goto err;
157 }
158 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
159 } while (--i);
160
161 mutex_init(&cma->lock);
Sasha Levin26b02a12015-04-14 15:44:59 -0700162
163#ifdef CONFIG_CMA_DEBUGFS
164 INIT_HLIST_HEAD(&cma->mem_head);
165 spin_lock_init(&cma->mem_head_lock);
166#endif
167
Shiraz Hashima24e0232016-02-15 16:26:37 +0530168 if (!PageHighMem(pfn_to_page(cma->base_pfn)))
169 kmemleak_free_part(__va(cma->base_pfn << PAGE_SHIFT),
170 cma->count << PAGE_SHIFT);
171
Joonsoo Kima2541292014-08-06 16:05:25 -0700172 return 0;
173
174err:
175 kfree(cma->bitmap);
Laurent Pinchartf022d8c2014-10-24 13:18:39 +0300176 cma->count = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700177 return -EINVAL;
178}
179
180static int __init cma_init_reserved_areas(void)
181{
182 int i;
183
184 for (i = 0; i < cma_area_count; i++) {
185 int ret = cma_activate_area(&cma_areas[i]);
186
187 if (ret)
188 return ret;
189 }
190
Prakash Guptaa0d60fa2017-08-02 12:30:55 +0530191 show_mem_notifier_register(&cma_nb);
192
Joonsoo Kima2541292014-08-06 16:05:25 -0700193 return 0;
194}
195core_initcall(cma_init_reserved_areas);
196
197/**
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700198 * cma_init_reserved_mem() - create custom contiguous area from reserved memory
199 * @base: Base address of the reserved area
200 * @size: Size of the reserved area (in bytes),
201 * @order_per_bit: Order of pages represented by one bit on bitmap.
202 * @res_cma: Pointer to store the created cma region.
203 *
204 * This function creates custom contiguous area from already reserved memory.
205 */
206int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Sasha Levinac173822015-04-14 15:47:04 -0700207 unsigned int order_per_bit,
Laura Abbott77dbb1d2017-04-18 11:27:03 -0700208 const char *name,
Sasha Levinac173822015-04-14 15:47:04 -0700209 struct cma **res_cma)
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700210{
211 struct cma *cma;
212 phys_addr_t alignment;
213
214 /* Sanity checks */
215 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
216 pr_err("Not enough slots for CMA reserved regions!\n");
217 return -ENOSPC;
218 }
219
220 if (!size || !memblock_is_region_reserved(base, size))
221 return -EINVAL;
222
Shailendra Verma0f96ae22015-06-24 16:58:03 -0700223 /* ensure minimal alignment required by mm core */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700224 alignment = PAGE_SIZE <<
225 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700226
227 /* alignment should be aligned with order_per_bit */
228 if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
229 return -EINVAL;
230
231 if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
232 return -EINVAL;
233
234 /*
235 * Each reserved area must be initialised later, when more kernel
236 * subsystems (like slab allocator) are available.
237 */
238 cma = &cma_areas[cma_area_count];
Laura Abbott77dbb1d2017-04-18 11:27:03 -0700239 if (name) {
240 cma->name = name;
241 } else {
242 cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count);
243 if (!cma->name)
244 return -ENOMEM;
245 }
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700246 cma->base_pfn = PFN_DOWN(base);
247 cma->count = size >> PAGE_SHIFT;
248 cma->order_per_bit = order_per_bit;
249 *res_cma = cma;
250 cma_area_count++;
George G. Davis94737a82015-02-11 15:26:27 -0800251 totalcma_pages += (size / PAGE_SIZE);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700252
253 return 0;
254}
255
256/**
Joonsoo Kima2541292014-08-06 16:05:25 -0700257 * cma_declare_contiguous() - reserve custom contiguous area
Joonsoo Kima2541292014-08-06 16:05:25 -0700258 * @base: Base address of the reserved area optional, use 0 for any
Joonsoo Kimc1f733a2014-08-06 16:05:32 -0700259 * @size: Size of the reserved area (in bytes),
Joonsoo Kima2541292014-08-06 16:05:25 -0700260 * @limit: End address of the reserved memory (optional, 0 for any).
261 * @alignment: Alignment for the CMA area, should be power of 2 or zero
262 * @order_per_bit: Order of pages represented by one bit on bitmap.
Joonsoo Kima2541292014-08-06 16:05:25 -0700263 * @fixed: hint about where to place the reserved area
Joonsoo Kimc1f733a2014-08-06 16:05:32 -0700264 * @res_cma: Pointer to store the created cma region.
Joonsoo Kima2541292014-08-06 16:05:25 -0700265 *
266 * This function reserves memory from early allocator. It should be
267 * called by arch specific code once the early allocator (memblock or bootmem)
268 * has been activated and all other subsystems have already allocated/reserved
269 * memory. This function allows to create custom reserved areas.
270 *
271 * If @fixed is true, reserve contiguous area at exactly @base. If false,
272 * reserve in range from @base to @limit.
273 */
Joonsoo Kimc1f733a2014-08-06 16:05:32 -0700274int __init cma_declare_contiguous(phys_addr_t base,
275 phys_addr_t size, phys_addr_t limit,
Joonsoo Kima2541292014-08-06 16:05:25 -0700276 phys_addr_t alignment, unsigned int order_per_bit,
Laura Abbott77dbb1d2017-04-18 11:27:03 -0700277 bool fixed, const char *name, struct cma **res_cma)
Joonsoo Kima2541292014-08-06 16:05:25 -0700278{
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700279 phys_addr_t memblock_end = memblock_end_of_DRAM();
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800280 phys_addr_t highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700281 int ret = 0;
282
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800283#ifdef CONFIG_X86
284 /*
285 * high_memory isn't direct mapped memory so retrieving its physical
286 * address isn't appropriate. But it would be useful to check the
Shailendra Verma0f96ae22015-06-24 16:58:03 -0700287 * physical address of the highmem boundary so it's justifiable to get
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800288 * the physical address from it. On x86 there is a validation check for
289 * this case, so the following workaround is needed to avoid it.
290 */
291 highmem_start = __pa_nodebug(high_memory);
292#else
293 highmem_start = __pa(high_memory);
294#endif
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300295 pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
296 __func__, &size, &base, &limit, &alignment);
Joonsoo Kima2541292014-08-06 16:05:25 -0700297
298 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
299 pr_err("Not enough slots for CMA reserved regions!\n");
300 return -ENOSPC;
301 }
302
303 if (!size)
304 return -EINVAL;
305
306 if (alignment && !is_power_of_2(alignment))
307 return -EINVAL;
308
309 /*
310 * Sanitise input arguments.
311 * Pages both ends in CMA area could be merged into adjacent unmovable
312 * migratetype page by page allocator's buddy algorithm. In the case,
313 * you couldn't get a contiguous memory, which is not what we want.
314 */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700315 alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
316 max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
Doug Bergerad204112019-07-16 16:26:24 -0700317 if (fixed && base & (alignment - 1)) {
318 ret = -EINVAL;
319 pr_err("Region at %pa must be aligned to %pa bytes\n",
320 &base, &alignment);
321 goto err;
322 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700323 base = ALIGN(base, alignment);
324 size = ALIGN(size, alignment);
325 limit &= ~(alignment - 1);
326
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300327 if (!base)
328 fixed = false;
329
Joonsoo Kima2541292014-08-06 16:05:25 -0700330 /* size should be aligned with order_per_bit */
331 if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
332 return -EINVAL;
333
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700334 /*
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300335 * If allocating at a fixed base the request region must not cross the
336 * low/high memory boundary.
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700337 */
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300338 if (fixed && base < highmem_start && base + size > highmem_start) {
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700339 ret = -EINVAL;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300340 pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
341 &base, &highmem_start);
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700342 goto err;
343 }
344
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300345 /*
346 * If the limit is unspecified or above the memblock end, its effective
347 * value will be the memblock end. Set it explicitly to simplify further
348 * checks.
349 */
350 if (limit == 0 || limit > memblock_end)
351 limit = memblock_end;
352
Doug Bergerad204112019-07-16 16:26:24 -0700353 if (base + size > limit) {
354 ret = -EINVAL;
355 pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n",
356 &size, &base, &limit);
357 goto err;
358 }
359
Joonsoo Kima2541292014-08-06 16:05:25 -0700360 /* Reserve memory */
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300361 if (fixed) {
Joonsoo Kima2541292014-08-06 16:05:25 -0700362 if (memblock_is_region_reserved(base, size) ||
363 memblock_reserve(base, size) < 0) {
364 ret = -EBUSY;
365 goto err;
366 }
367 } else {
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300368 phys_addr_t addr = 0;
369
370 /*
371 * All pages in the reserved area must come from the same zone.
372 * If the requested region crosses the low/high memory boundary,
373 * try allocating from high memory first and fall back to low
374 * memory in case of failure.
375 */
376 if (base < highmem_start && limit > highmem_start) {
377 addr = memblock_alloc_range(size, alignment,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700378 highmem_start, limit,
379 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300380 limit = highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700381 }
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300382
383 if (!addr) {
384 addr = memblock_alloc_range(size, alignment, base,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700385 limit,
386 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300387 if (!addr) {
388 ret = -ENOMEM;
389 goto err;
390 }
391 }
392
Thierry Reding620951e2014-12-12 16:58:31 -0800393 /*
394 * kmemleak scans/reads tracked objects for pointers to other
395 * objects but this address isn't mapped and accessible
396 */
Catalin Marinas9099dae2016-10-11 13:55:11 -0700397 kmemleak_ignore_phys(addr);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300398 base = addr;
Joonsoo Kima2541292014-08-06 16:05:25 -0700399 }
400
Laura Abbott77dbb1d2017-04-18 11:27:03 -0700401 ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700402 if (ret)
Peng Fandcd85a72019-03-05 15:49:50 -0800403 goto free_mem;
Joonsoo Kima2541292014-08-06 16:05:25 -0700404
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300405 pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
406 &base);
Joonsoo Kima2541292014-08-06 16:05:25 -0700407 return 0;
408
Peng Fandcd85a72019-03-05 15:49:50 -0800409free_mem:
410 memblock_free(base, size);
Joonsoo Kima2541292014-08-06 16:05:25 -0700411err:
Joonsoo Kim0de9d2e2014-08-06 16:05:34 -0700412 pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Joonsoo Kima2541292014-08-06 16:05:25 -0700413 return ret;
414}
415
Jaewon Kimefb354d2017-02-24 14:58:50 -0800416#ifdef CONFIG_CMA_DEBUG
417static void cma_debug_show_areas(struct cma *cma)
418{
419 unsigned long next_zero_bit, next_set_bit;
420 unsigned long start = 0;
421 unsigned int nr_zero, nr_total = 0;
422
423 mutex_lock(&cma->lock);
424 pr_info("number of available pages: ");
425 for (;;) {
426 next_zero_bit = find_next_zero_bit(cma->bitmap, cma->count, start);
427 if (next_zero_bit >= cma->count)
428 break;
429 next_set_bit = find_next_bit(cma->bitmap, cma->count, next_zero_bit);
430 nr_zero = next_set_bit - next_zero_bit;
431 pr_cont("%s%u@%lu", nr_total ? "+" : "", nr_zero, next_zero_bit);
432 nr_total += nr_zero;
433 start = next_zero_bit + nr_zero;
434 }
435 pr_cont("=> %u free of %lu total pages\n", nr_total, cma->count);
436 mutex_unlock(&cma->lock);
437}
438#else
439static inline void cma_debug_show_areas(struct cma *cma) { }
440#endif
441
Joonsoo Kima2541292014-08-06 16:05:25 -0700442/**
443 * cma_alloc() - allocate pages from contiguous area
444 * @cma: Contiguous memory region for which the allocation is performed.
445 * @count: Requested number of pages.
446 * @align: Requested alignment of pages (in PAGE_SIZE order).
447 *
448 * This function allocates part of contiguous memory on specific
449 * contiguous memory area.
450 */
Rohit Vaswani67a2e2132015-10-22 13:32:11 -0700451struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align)
Joonsoo Kima2541292014-08-06 16:05:25 -0700452{
Andrew Morton3acaea62015-11-05 18:50:08 -0800453 unsigned long mask, offset;
454 unsigned long pfn = -1;
455 unsigned long start = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700456 unsigned long bitmap_maxno, bitmap_no, bitmap_count;
457 struct page *page = NULL;
Liam Mark51934022015-10-23 12:14:11 -0700458 int retry_after_sleep = 0;
Jaewon Kimefb354d2017-02-24 14:58:50 -0800459 int ret = -ENOMEM;
Shiraz Hashimb35a5582018-05-01 17:58:51 +0530460 int max_retries = 2;
461 int available_regions = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700462
463 if (!cma || !cma->count)
464 return NULL;
465
Rohit Vaswani67a2e2132015-10-22 13:32:11 -0700466 pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
Joonsoo Kima2541292014-08-06 16:05:25 -0700467 count, align);
468
469 if (!count)
470 return NULL;
471
Liam Mark9645c9e2015-11-25 14:37:54 -0800472 trace_cma_alloc_start(count, align);
473
Joonsoo Kima2541292014-08-06 16:05:25 -0700474 mask = cma_bitmap_aligned_mask(cma, align);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800475 offset = cma_bitmap_aligned_offset(cma, align);
Joonsoo Kima2541292014-08-06 16:05:25 -0700476 bitmap_maxno = cma_bitmap_maxno(cma);
477 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
478
Shiraz Hashim6b36ba52016-11-10 10:46:16 -0800479 if (bitmap_count > bitmap_maxno)
480 return NULL;
481
Joonsoo Kima2541292014-08-06 16:05:25 -0700482 for (;;) {
483 mutex_lock(&cma->lock);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800484 bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
485 bitmap_maxno, start, bitmap_count, mask,
486 offset);
Joonsoo Kima2541292014-08-06 16:05:25 -0700487 if (bitmap_no >= bitmap_maxno) {
Vinayak Menon852548e2018-08-08 13:23:23 +0530488 if ((retry_after_sleep < max_retries) &&
489 (ret == -EBUSY)) {
Liam Mark51934022015-10-23 12:14:11 -0700490 start = 0;
491 /*
Shiraz Hashimb35a5582018-05-01 17:58:51 +0530492 * update max retries if available free regions
493 * are less.
494 */
495 if (available_regions < 3)
496 max_retries = 5;
497 available_regions = 0;
498 /*
Liam Mark51934022015-10-23 12:14:11 -0700499 * Page may be momentarily pinned by some other
500 * process which has been scheduled out, eg.
501 * in exit path, during unmap call, or process
502 * fork and so cannot be freed there. Sleep
503 * for 100ms and retry twice to see if it has
504 * been freed later.
505 */
506 mutex_unlock(&cma->lock);
507 msleep(100);
508 retry_after_sleep++;
509 continue;
510 } else {
511 mutex_unlock(&cma->lock);
512 break;
513 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700514 }
Shiraz Hashimb35a5582018-05-01 17:58:51 +0530515
516 available_regions++;
Joonsoo Kima2541292014-08-06 16:05:25 -0700517 bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
518 /*
519 * It's safe to drop the lock here. We've marked this region for
520 * our exclusive use. If the migration fails we will take the
521 * lock again and unmark it.
522 */
523 mutex_unlock(&cma->lock);
524
525 pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
526 mutex_lock(&cma_mutex);
527 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
528 mutex_unlock(&cma_mutex);
529 if (ret == 0) {
530 page = pfn_to_page(pfn);
531 break;
Joonsoo Kima2541292014-08-06 16:05:25 -0700532 }
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700533
Joonsoo Kima2541292014-08-06 16:05:25 -0700534 cma_clear_bitmap(cma, pfn, count);
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700535 if (ret != -EBUSY)
536 break;
537
Joonsoo Kima2541292014-08-06 16:05:25 -0700538 pr_debug("%s(): memory range at %p is busy, retrying\n",
539 __func__, pfn_to_page(pfn));
Liam Mark9645c9e2015-11-25 14:37:54 -0800540
541 trace_cma_alloc_busy_retry(pfn, pfn_to_page(pfn), count, align);
Joonsoo Kima2541292014-08-06 16:05:25 -0700542 /* try again with a bit different memory target */
543 start = bitmap_no + mask + 1;
544 }
545
Andrew Morton3acaea62015-11-05 18:50:08 -0800546 trace_cma_alloc(pfn, page, count, align);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700547
Jaewon Kimefb354d2017-02-24 14:58:50 -0800548 if (ret) {
549 pr_info("%s: alloc failed, req-size: %zu pages, ret: %d\n",
550 __func__, count, ret);
551 cma_debug_show_areas(cma);
552 }
553
Joonsoo Kima2541292014-08-06 16:05:25 -0700554 pr_debug("%s(): returned %p\n", __func__, page);
555 return page;
556}
557
558/**
559 * cma_release() - release allocated pages
560 * @cma: Contiguous memory region for which the allocation is performed.
561 * @pages: Allocated pages.
562 * @count: Number of allocated pages.
563 *
564 * This function releases memory allocated by alloc_cma().
565 * It returns false when provided pages do not belong to contiguous area and
566 * true otherwise.
567 */
Sasha Levinac173822015-04-14 15:47:04 -0700568bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -0700569{
570 unsigned long pfn;
571
572 if (!cma || !pages)
573 return false;
574
575 pr_debug("%s(page %p)\n", __func__, (void *)pages);
576
577 pfn = page_to_pfn(pages);
578
579 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
580 return false;
581
582 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
583
584 free_contig_range(pfn, count);
585 cma_clear_bitmap(cma, pfn, count);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700586 trace_cma_release(pfn, pages, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700587
588 return true;
589}