blob: aa40e6c7b042e95f5fb30a24cb05014c55ce9152 [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>
Randy Dunlap514c6032018-04-05 16:25:34 -070038#include <linux/kmemleak.h>
Stefan Strogin99e8ea62015-04-15 16:14:50 -070039#include <trace/events/cma.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070040
Sasha Levin28b24c12015-04-14 15:44:57 -070041#include "cma.h"
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -070042#include "internal.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}
52
Sasha Levinac173822015-04-14 15:47:04 -070053unsigned long cma_get_size(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070054{
55 return cma->count << PAGE_SHIFT;
56}
57
Laura Abbottf318dd02017-04-18 11:27:03 -070058const char *cma_get_name(const struct cma *cma)
59{
60 return cma->name ? cma->name : "(undefined)";
61}
62
Sasha Levinac173822015-04-14 15:47:04 -070063static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070064 unsigned int align_order)
Joonsoo Kima2541292014-08-06 16:05:25 -070065{
Weijie Yang68faed62014-10-13 15:51:03 -070066 if (align_order <= cma->order_per_bit)
67 return 0;
68 return (1UL << (align_order - cma->order_per_bit)) - 1;
Joonsoo Kima2541292014-08-06 16:05:25 -070069}
70
Danesh Petigara850fc432015-03-12 16:25:57 -070071/*
Doug Bergere048cb32017-07-10 15:49:44 -070072 * Find the offset of the base PFN from the specified align_order.
73 * The value returned is represented in order_per_bits.
Danesh Petigara850fc432015-03-12 16:25:57 -070074 */
Sasha Levinac173822015-04-14 15:47:04 -070075static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070076 unsigned int align_order)
Gregory Fongb5be83e2014-12-12 16:54:48 -080077{
Doug Bergere048cb32017-07-10 15:49:44 -070078 return (cma->base_pfn & ((1UL << align_order) - 1))
79 >> cma->order_per_bit;
Gregory Fongb5be83e2014-12-12 16:54:48 -080080}
81
Sasha Levinac173822015-04-14 15:47:04 -070082static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
83 unsigned long pages)
Joonsoo Kima2541292014-08-06 16:05:25 -070084{
85 return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
86}
87
Sasha Levinac173822015-04-14 15:47:04 -070088static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
89 unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -070090{
91 unsigned long bitmap_no, bitmap_count;
92
93 bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
94 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
95
96 mutex_lock(&cma->lock);
97 bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
98 mutex_unlock(&cma->lock);
99}
100
101static int __init cma_activate_area(struct cma *cma)
102{
103 int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
104 unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
105 unsigned i = cma->count >> pageblock_order;
106 struct zone *zone;
107
108 cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
109
110 if (!cma->bitmap)
111 return -ENOMEM;
112
Joonsoo Kima2541292014-08-06 16:05:25 -0700113 do {
114 unsigned j;
115
116 base_pfn = pfn;
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700117 if (!pfn_valid(base_pfn))
118 goto err;
119
120 zone = page_zone(pfn_to_page(base_pfn));
Joonsoo Kima2541292014-08-06 16:05:25 -0700121 for (j = pageblock_nr_pages; j; --j, pfn++) {
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700122 if (!pfn_valid(pfn))
123 goto err;
124
Joonsoo Kima2541292014-08-06 16:05:25 -0700125 /*
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700126 * In init_cma_reserved_pageblock(), present_pages
127 * is adjusted with assumption that all pages in
128 * the pageblock come from a single zone.
Joonsoo Kima2541292014-08-06 16:05:25 -0700129 */
130 if (page_zone(pfn_to_page(pfn)) != zone)
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700131 goto err;
Joonsoo Kima2541292014-08-06 16:05:25 -0700132 }
133 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
134 } while (--i);
135
136 mutex_init(&cma->lock);
Sasha Levin26b02a12015-04-14 15:44:59 -0700137
138#ifdef CONFIG_CMA_DEBUGFS
139 INIT_HLIST_HEAD(&cma->mem_head);
140 spin_lock_init(&cma->mem_head_lock);
141#endif
142
Joonsoo Kima2541292014-08-06 16:05:25 -0700143 return 0;
144
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700145err:
Anshuman Khanduale35ef632017-07-10 15:48:12 -0700146 pr_err("CMA area %s could not be activated\n", cma->name);
Joonsoo Kima2541292014-08-06 16:05:25 -0700147 kfree(cma->bitmap);
Laurent Pinchartf022d8c2014-10-24 13:18:39 +0300148 cma->count = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700149 return -EINVAL;
150}
151
152static int __init cma_init_reserved_areas(void)
153{
154 int i;
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700155 struct zone *zone;
156 pg_data_t *pgdat;
157
158 if (!cma_area_count)
159 return 0;
160
161 for_each_online_pgdat(pgdat) {
162 unsigned long start_pfn = UINT_MAX, end_pfn = 0;
163
164 zone = &pgdat->node_zones[ZONE_MOVABLE];
165
166 /*
167 * In this case, we cannot adjust the zone range
168 * since it is now maximum node span and we don't
169 * know original zone range.
170 */
171 if (populated_zone(zone))
172 continue;
173
174 for (i = 0; i < cma_area_count; i++) {
175 if (pfn_to_nid(cma_areas[i].base_pfn) !=
176 pgdat->node_id)
177 continue;
178
179 start_pfn = min(start_pfn, cma_areas[i].base_pfn);
180 end_pfn = max(end_pfn, cma_areas[i].base_pfn +
181 cma_areas[i].count);
182 }
183
184 if (!end_pfn)
185 continue;
186
187 zone->zone_start_pfn = start_pfn;
188 zone->spanned_pages = end_pfn - start_pfn;
189 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700190
191 for (i = 0; i < cma_area_count; i++) {
192 int ret = cma_activate_area(&cma_areas[i]);
193
194 if (ret)
195 return ret;
196 }
197
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700198 /*
199 * Reserved pages for ZONE_MOVABLE are now activated and
200 * this would change ZONE_MOVABLE's managed page counter and
201 * the other zones' present counter. We need to re-calculate
202 * various zone information that depends on this initialization.
203 */
204 build_all_zonelists(NULL);
205 for_each_populated_zone(zone) {
206 if (zone_idx(zone) == ZONE_MOVABLE) {
207 zone_pcp_reset(zone);
208 setup_zone_pageset(zone);
209 } else
210 zone_pcp_update(zone);
211
212 set_zone_contiguous(zone);
213 }
214
215 /*
216 * We need to re-init per zone wmark by calling
217 * init_per_zone_wmark_min() but doesn't call here because it is
218 * registered on core_initcall and it will be called later than us.
219 */
220
Joonsoo Kima2541292014-08-06 16:05:25 -0700221 return 0;
222}
Joonsoo Kimbad8c6c2018-04-10 16:30:15 -0700223pure_initcall(cma_init_reserved_areas);
Joonsoo Kima2541292014-08-06 16:05:25 -0700224
225/**
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700226 * cma_init_reserved_mem() - create custom contiguous area from reserved memory
227 * @base: Base address of the reserved area
228 * @size: Size of the reserved area (in bytes),
229 * @order_per_bit: Order of pages represented by one bit on bitmap.
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700230 * @name: The name of the area. If this parameter is NULL, the name of
231 * the area will be set to "cmaN", where N is a running counter of
232 * used areas.
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700233 * @res_cma: Pointer to store the created cma region.
234 *
235 * This function creates custom contiguous area from already reserved memory.
236 */
237int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Sasha Levinac173822015-04-14 15:47:04 -0700238 unsigned int order_per_bit,
Laura Abbottf318dd02017-04-18 11:27:03 -0700239 const char *name,
Sasha Levinac173822015-04-14 15:47:04 -0700240 struct cma **res_cma)
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700241{
242 struct cma *cma;
243 phys_addr_t alignment;
244
245 /* Sanity checks */
246 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
247 pr_err("Not enough slots for CMA reserved regions!\n");
248 return -ENOSPC;
249 }
250
251 if (!size || !memblock_is_region_reserved(base, size))
252 return -EINVAL;
253
Shailendra Verma0f96ae22015-06-24 16:58:03 -0700254 /* ensure minimal alignment required by mm core */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700255 alignment = PAGE_SIZE <<
256 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700257
258 /* alignment should be aligned with order_per_bit */
259 if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
260 return -EINVAL;
261
262 if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
263 return -EINVAL;
264
265 /*
266 * Each reserved area must be initialised later, when more kernel
267 * subsystems (like slab allocator) are available.
268 */
269 cma = &cma_areas[cma_area_count];
Laura Abbottf318dd02017-04-18 11:27:03 -0700270 if (name) {
271 cma->name = name;
272 } else {
273 cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count);
274 if (!cma->name)
275 return -ENOMEM;
276 }
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700277 cma->base_pfn = PFN_DOWN(base);
278 cma->count = size >> PAGE_SHIFT;
279 cma->order_per_bit = order_per_bit;
280 *res_cma = cma;
281 cma_area_count++;
George G. Davis94737a82015-02-11 15:26:27 -0800282 totalcma_pages += (size / PAGE_SIZE);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700283
284 return 0;
285}
286
287/**
Joonsoo Kima2541292014-08-06 16:05:25 -0700288 * cma_declare_contiguous() - reserve custom contiguous area
Joonsoo Kima2541292014-08-06 16:05:25 -0700289 * @base: Base address of the reserved area optional, use 0 for any
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700290 * @size: Size of the reserved area (in bytes),
Joonsoo Kima2541292014-08-06 16:05:25 -0700291 * @limit: End address of the reserved memory (optional, 0 for any).
292 * @alignment: Alignment for the CMA area, should be power of 2 or zero
293 * @order_per_bit: Order of pages represented by one bit on bitmap.
Joonsoo Kima2541292014-08-06 16:05:25 -0700294 * @fixed: hint about where to place the reserved area
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700295 * @name: The name of the area. See function cma_init_reserved_mem()
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700296 * @res_cma: Pointer to store the created cma region.
Joonsoo Kima2541292014-08-06 16:05:25 -0700297 *
298 * This function reserves memory from early allocator. It should be
299 * called by arch specific code once the early allocator (memblock or bootmem)
300 * has been activated and all other subsystems have already allocated/reserved
301 * memory. This function allows to create custom reserved areas.
302 *
303 * If @fixed is true, reserve contiguous area at exactly @base. If false,
304 * reserve in range from @base to @limit.
305 */
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700306int __init cma_declare_contiguous(phys_addr_t base,
307 phys_addr_t size, phys_addr_t limit,
Joonsoo Kima2541292014-08-06 16:05:25 -0700308 phys_addr_t alignment, unsigned int order_per_bit,
Laura Abbottf318dd02017-04-18 11:27:03 -0700309 bool fixed, const char *name, struct cma **res_cma)
Joonsoo Kima2541292014-08-06 16:05:25 -0700310{
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700311 phys_addr_t memblock_end = memblock_end_of_DRAM();
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800312 phys_addr_t highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700313 int ret = 0;
314
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800315 /*
Laura Abbott2dece442017-01-10 13:35:41 -0800316 * We can't use __pa(high_memory) directly, since high_memory
317 * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
318 * complain. Find the boundary by adding one to the last valid
319 * address.
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800320 */
Laura Abbott2dece442017-01-10 13:35:41 -0800321 highmem_start = __pa(high_memory - 1) + 1;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300322 pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
323 __func__, &size, &base, &limit, &alignment);
Joonsoo Kima2541292014-08-06 16:05:25 -0700324
325 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
326 pr_err("Not enough slots for CMA reserved regions!\n");
327 return -ENOSPC;
328 }
329
330 if (!size)
331 return -EINVAL;
332
333 if (alignment && !is_power_of_2(alignment))
334 return -EINVAL;
335
336 /*
337 * Sanitise input arguments.
338 * Pages both ends in CMA area could be merged into adjacent unmovable
339 * migratetype page by page allocator's buddy algorithm. In the case,
340 * you couldn't get a contiguous memory, which is not what we want.
341 */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700342 alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
343 max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
Joonsoo Kima2541292014-08-06 16:05:25 -0700344 base = ALIGN(base, alignment);
345 size = ALIGN(size, alignment);
346 limit &= ~(alignment - 1);
347
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300348 if (!base)
349 fixed = false;
350
Joonsoo Kima2541292014-08-06 16:05:25 -0700351 /* size should be aligned with order_per_bit */
352 if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
353 return -EINVAL;
354
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700355 /*
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300356 * If allocating at a fixed base the request region must not cross the
357 * low/high memory boundary.
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700358 */
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300359 if (fixed && base < highmem_start && base + size > highmem_start) {
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700360 ret = -EINVAL;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300361 pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
362 &base, &highmem_start);
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700363 goto err;
364 }
365
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300366 /*
367 * If the limit is unspecified or above the memblock end, its effective
368 * value will be the memblock end. Set it explicitly to simplify further
369 * checks.
370 */
371 if (limit == 0 || limit > memblock_end)
372 limit = memblock_end;
373
Joonsoo Kima2541292014-08-06 16:05:25 -0700374 /* Reserve memory */
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300375 if (fixed) {
Joonsoo Kima2541292014-08-06 16:05:25 -0700376 if (memblock_is_region_reserved(base, size) ||
377 memblock_reserve(base, size) < 0) {
378 ret = -EBUSY;
379 goto err;
380 }
381 } else {
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300382 phys_addr_t addr = 0;
383
384 /*
385 * All pages in the reserved area must come from the same zone.
386 * If the requested region crosses the low/high memory boundary,
387 * try allocating from high memory first and fall back to low
388 * memory in case of failure.
389 */
390 if (base < highmem_start && limit > highmem_start) {
391 addr = memblock_alloc_range(size, alignment,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700392 highmem_start, limit,
393 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300394 limit = highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700395 }
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300396
397 if (!addr) {
398 addr = memblock_alloc_range(size, alignment, base,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700399 limit,
400 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300401 if (!addr) {
402 ret = -ENOMEM;
403 goto err;
404 }
405 }
406
Thierry Reding620951e2014-12-12 16:58:31 -0800407 /*
408 * kmemleak scans/reads tracked objects for pointers to other
409 * objects but this address isn't mapped and accessible
410 */
Catalin Marinas9099dae2016-10-11 13:55:11 -0700411 kmemleak_ignore_phys(addr);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300412 base = addr;
Joonsoo Kima2541292014-08-06 16:05:25 -0700413 }
414
Laura Abbottf318dd02017-04-18 11:27:03 -0700415 ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700416 if (ret)
417 goto err;
Joonsoo Kima2541292014-08-06 16:05:25 -0700418
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300419 pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
420 &base);
Joonsoo Kima2541292014-08-06 16:05:25 -0700421 return 0;
422
423err:
Joonsoo Kim0de9d2e2014-08-06 16:05:34 -0700424 pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Joonsoo Kima2541292014-08-06 16:05:25 -0700425 return ret;
426}
427
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800428#ifdef CONFIG_CMA_DEBUG
429static void cma_debug_show_areas(struct cma *cma)
430{
431 unsigned long next_zero_bit, next_set_bit;
432 unsigned long start = 0;
433 unsigned int nr_zero, nr_total = 0;
434
435 mutex_lock(&cma->lock);
436 pr_info("number of available pages: ");
437 for (;;) {
438 next_zero_bit = find_next_zero_bit(cma->bitmap, cma->count, start);
439 if (next_zero_bit >= cma->count)
440 break;
441 next_set_bit = find_next_bit(cma->bitmap, cma->count, next_zero_bit);
442 nr_zero = next_set_bit - next_zero_bit;
443 pr_cont("%s%u@%lu", nr_total ? "+" : "", nr_zero, next_zero_bit);
444 nr_total += nr_zero;
445 start = next_zero_bit + nr_zero;
446 }
447 pr_cont("=> %u free of %lu total pages\n", nr_total, cma->count);
448 mutex_unlock(&cma->lock);
449}
450#else
451static inline void cma_debug_show_areas(struct cma *cma) { }
452#endif
453
Joonsoo Kima2541292014-08-06 16:05:25 -0700454/**
455 * cma_alloc() - allocate pages from contiguous area
456 * @cma: Contiguous memory region for which the allocation is performed.
457 * @count: Requested number of pages.
458 * @align: Requested alignment of pages (in PAGE_SIZE order).
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700459 * @gfp_mask: GFP mask to use during compaction
Joonsoo Kima2541292014-08-06 16:05:25 -0700460 *
461 * This function allocates part of contiguous memory on specific
462 * contiguous memory area.
463 */
Lucas Stache2f466e2017-02-24 14:58:41 -0800464struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
465 gfp_t gfp_mask)
Joonsoo Kima2541292014-08-06 16:05:25 -0700466{
Andrew Morton3acaea62015-11-05 18:50:08 -0800467 unsigned long mask, offset;
468 unsigned long pfn = -1;
469 unsigned long start = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700470 unsigned long bitmap_maxno, bitmap_no, bitmap_count;
471 struct page *page = NULL;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800472 int ret = -ENOMEM;
Joonsoo Kima2541292014-08-06 16:05:25 -0700473
474 if (!cma || !cma->count)
475 return NULL;
476
Rohit Vaswani67a2e2132015-10-22 13:32:11 -0700477 pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
Joonsoo Kima2541292014-08-06 16:05:25 -0700478 count, align);
479
480 if (!count)
481 return NULL;
482
483 mask = cma_bitmap_aligned_mask(cma, align);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800484 offset = cma_bitmap_aligned_offset(cma, align);
Joonsoo Kima2541292014-08-06 16:05:25 -0700485 bitmap_maxno = cma_bitmap_maxno(cma);
486 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
487
Shiraz Hashim6b36ba52016-11-10 10:46:16 -0800488 if (bitmap_count > bitmap_maxno)
489 return NULL;
490
Joonsoo Kima2541292014-08-06 16:05:25 -0700491 for (;;) {
492 mutex_lock(&cma->lock);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800493 bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
494 bitmap_maxno, start, bitmap_count, mask,
495 offset);
Joonsoo Kima2541292014-08-06 16:05:25 -0700496 if (bitmap_no >= bitmap_maxno) {
497 mutex_unlock(&cma->lock);
498 break;
499 }
500 bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
501 /*
502 * It's safe to drop the lock here. We've marked this region for
503 * our exclusive use. If the migration fails we will take the
504 * lock again and unmark it.
505 */
506 mutex_unlock(&cma->lock);
507
508 pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
509 mutex_lock(&cma_mutex);
Lucas Stachca96b622017-02-24 14:58:37 -0800510 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
Lucas Stache2f466e2017-02-24 14:58:41 -0800511 gfp_mask);
Joonsoo Kima2541292014-08-06 16:05:25 -0700512 mutex_unlock(&cma_mutex);
513 if (ret == 0) {
514 page = pfn_to_page(pfn);
515 break;
Joonsoo Kima2541292014-08-06 16:05:25 -0700516 }
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700517
Joonsoo Kima2541292014-08-06 16:05:25 -0700518 cma_clear_bitmap(cma, pfn, count);
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700519 if (ret != -EBUSY)
520 break;
521
Joonsoo Kima2541292014-08-06 16:05:25 -0700522 pr_debug("%s(): memory range at %p is busy, retrying\n",
523 __func__, pfn_to_page(pfn));
524 /* try again with a bit different memory target */
525 start = bitmap_no + mask + 1;
526 }
527
Andrew Morton3acaea62015-11-05 18:50:08 -0800528 trace_cma_alloc(pfn, page, count, align);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700529
Boris Brezillonef465012017-10-13 15:58:01 -0700530 if (ret && !(gfp_mask & __GFP_NOWARN)) {
Pintu Agarwal5984af1082017-11-15 17:34:26 -0800531 pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n",
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800532 __func__, count, ret);
533 cma_debug_show_areas(cma);
534 }
535
Joonsoo Kima2541292014-08-06 16:05:25 -0700536 pr_debug("%s(): returned %p\n", __func__, page);
537 return page;
538}
539
540/**
541 * cma_release() - release allocated pages
542 * @cma: Contiguous memory region for which the allocation is performed.
543 * @pages: Allocated pages.
544 * @count: Number of allocated pages.
545 *
546 * This function releases memory allocated by alloc_cma().
547 * It returns false when provided pages do not belong to contiguous area and
548 * true otherwise.
549 */
Sasha Levinac173822015-04-14 15:47:04 -0700550bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -0700551{
552 unsigned long pfn;
553
554 if (!cma || !pages)
555 return false;
556
557 pr_debug("%s(page %p)\n", __func__, (void *)pages);
558
559 pfn = page_to_pfn(pages);
560
561 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
562 return false;
563
564 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
565
566 free_contig_range(pfn, count);
567 cma_clear_bitmap(cma, pfn, count);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700568 trace_cma_release(pfn, pages, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700569
570 return true;
571}
Laura Abbotte4231bc2017-04-18 11:27:04 -0700572
573int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
574{
575 int i;
576
577 for (i = 0; i < cma_area_count; i++) {
578 int ret = it(&cma_areas[i], data);
579
580 if (ret)
581 return ret;
582 }
583
584 return 0;
585}