blob: 7af197cdabc0bfecf31395f365167d322f688e22 [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
Stefan Strogin99e8ea62015-04-15 16:14:50 -070021#define CREATE_TRACE_POINTS
Joonsoo Kima2541292014-08-06 16:05:25 -070022
23#include <linux/memblock.h>
24#include <linux/err.h>
25#include <linux/mm.h>
26#include <linux/mutex.h>
27#include <linux/sizes.h>
28#include <linux/slab.h>
29#include <linux/log2.h>
30#include <linux/cma.h>
Marek Szyprowskif7426b92014-10-09 15:26:47 -070031#include <linux/highmem.h>
Thierry Reding620951e2014-12-12 16:58:31 -080032#include <linux/io.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070033#include <linux/kmemleak.h>
Swathi Sridharbbbc80b2018-07-13 10:02:08 -070034#include <linux/delay.h>
Prakash Gupta883e44f2017-08-02 12:30:55 +053035#include <linux/show_mem_notifier.h>
Stefan Strogin99e8ea62015-04-15 16:14:50 -070036#include <trace/events/cma.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070037
Sasha Levin28b24c12015-04-14 15:44:57 -070038#include "cma.h"
Joonsoo Kima2541292014-08-06 16:05:25 -070039
Sasha Levin28b24c12015-04-14 15:44:57 -070040struct cma cma_areas[MAX_CMA_AREAS];
41unsigned cma_area_count;
Joonsoo Kima2541292014-08-06 16:05:25 -070042static DEFINE_MUTEX(cma_mutex);
43
Sasha Levinac173822015-04-14 15:47:04 -070044phys_addr_t cma_get_base(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070045{
46 return PFN_PHYS(cma->base_pfn);
47}
48
Sasha Levinac173822015-04-14 15:47:04 -070049unsigned long cma_get_size(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070050{
51 return cma->count << PAGE_SHIFT;
52}
53
Laura Abbottf318dd02017-04-18 11:27:03 -070054const char *cma_get_name(const struct cma *cma)
55{
56 return cma->name ? cma->name : "(undefined)";
57}
58
Sasha Levinac173822015-04-14 15:47:04 -070059static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070060 unsigned int align_order)
Joonsoo Kima2541292014-08-06 16:05:25 -070061{
Weijie Yang68faed62014-10-13 15:51:03 -070062 if (align_order <= cma->order_per_bit)
63 return 0;
64 return (1UL << (align_order - cma->order_per_bit)) - 1;
Joonsoo Kima2541292014-08-06 16:05:25 -070065}
66
Danesh Petigara850fc432015-03-12 16:25:57 -070067/*
Doug Bergere048cb32017-07-10 15:49:44 -070068 * Find the offset of the base PFN from the specified align_order.
69 * The value returned is represented in order_per_bits.
Danesh Petigara850fc432015-03-12 16:25:57 -070070 */
Sasha Levinac173822015-04-14 15:47:04 -070071static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070072 unsigned int align_order)
Gregory Fongb5be83e2014-12-12 16:54:48 -080073{
Doug Bergere048cb32017-07-10 15:49:44 -070074 return (cma->base_pfn & ((1UL << align_order) - 1))
75 >> cma->order_per_bit;
Gregory Fongb5be83e2014-12-12 16:54:48 -080076}
77
Sasha Levinac173822015-04-14 15:47:04 -070078static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
79 unsigned long pages)
Joonsoo Kima2541292014-08-06 16:05:25 -070080{
81 return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
82}
83
Sasha Levinac173822015-04-14 15:47:04 -070084static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
85 unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -070086{
87 unsigned long bitmap_no, bitmap_count;
88
89 bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
90 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
91
92 mutex_lock(&cma->lock);
93 bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
94 mutex_unlock(&cma->lock);
95}
96
Prakash Gupta883e44f2017-08-02 12:30:55 +053097static int cma_showmem_notifier(struct notifier_block *nb,
98 unsigned long action, void *data)
99{
100 int i;
101 unsigned long used;
102 struct cma *cma;
103
104 for (i = 0; i < cma_area_count; i++) {
105 cma = &cma_areas[i];
106 used = bitmap_weight(cma->bitmap,
107 (int)cma_bitmap_maxno(cma));
108 used <<= cma->order_per_bit;
109 pr_info("cma-%d pages: => %lu used of %lu total pages\n",
110 i, used, cma->count);
111 }
112
113 return 0;
114}
115
116static struct notifier_block cma_nb = {
117 .notifier_call = cma_showmem_notifier,
118};
119
Joonsoo Kima2541292014-08-06 16:05:25 -0700120static int __init cma_activate_area(struct cma *cma)
121{
122 int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
123 unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
124 unsigned i = cma->count >> pageblock_order;
125 struct zone *zone;
126
127 cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
128
Yue Hue5f88572019-05-13 17:18:14 -0700129 if (!cma->bitmap) {
130 cma->count = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700131 return -ENOMEM;
Yue Hue5f88572019-05-13 17:18:14 -0700132 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700133
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900134 WARN_ON_ONCE(!pfn_valid(pfn));
135 zone = page_zone(pfn_to_page(pfn));
136
Joonsoo Kima2541292014-08-06 16:05:25 -0700137 do {
138 unsigned j;
139
140 base_pfn = pfn;
141 for (j = pageblock_nr_pages; j; --j, pfn++) {
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900142 WARN_ON_ONCE(!pfn_valid(pfn));
Joonsoo Kima2541292014-08-06 16:05:25 -0700143 /*
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900144 * alloc_contig_range requires the pfn range
145 * specified to be in the same zone. Make this
146 * simple by forcing the entire CMA resv range
147 * to be in the same zone.
Joonsoo Kima2541292014-08-06 16:05:25 -0700148 */
149 if (page_zone(pfn_to_page(pfn)) != zone)
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900150 goto not_in_zone;
Joonsoo Kima2541292014-08-06 16:05:25 -0700151 }
152 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
153 } while (--i);
154
155 mutex_init(&cma->lock);
Sasha Levin26b02a12015-04-14 15:44:59 -0700156
157#ifdef CONFIG_CMA_DEBUGFS
158 INIT_HLIST_HEAD(&cma->mem_head);
159 spin_lock_init(&cma->mem_head_lock);
160#endif
161
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700162 if (!PageHighMem(pfn_to_page(cma->base_pfn)))
163 kmemleak_free_part(__va(cma->base_pfn << PAGE_SHIFT),
164 cma->count << PAGE_SHIFT);
165
Joonsoo Kima2541292014-08-06 16:05:25 -0700166 return 0;
167
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900168not_in_zone:
Anshuman Khanduale35ef632017-07-10 15:48:12 -0700169 pr_err("CMA area %s could not be activated\n", cma->name);
Joonsoo Kima2541292014-08-06 16:05:25 -0700170 kfree(cma->bitmap);
Laurent Pinchartf022d8c2014-10-24 13:18:39 +0300171 cma->count = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700172 return -EINVAL;
173}
174
175static int __init cma_init_reserved_areas(void)
176{
177 int i;
178
179 for (i = 0; i < cma_area_count; i++) {
180 int ret = cma_activate_area(&cma_areas[i]);
181
182 if (ret)
183 return ret;
184 }
185
Prakash Gupta883e44f2017-08-02 12:30:55 +0530186 show_mem_notifier_register(&cma_nb);
187
Joonsoo Kima2541292014-08-06 16:05:25 -0700188 return 0;
189}
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900190core_initcall(cma_init_reserved_areas);
Joonsoo Kima2541292014-08-06 16:05:25 -0700191
192/**
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700193 * cma_init_reserved_mem() - create custom contiguous area from reserved memory
194 * @base: Base address of the reserved area
195 * @size: Size of the reserved area (in bytes),
196 * @order_per_bit: Order of pages represented by one bit on bitmap.
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700197 * @name: The name of the area. If this parameter is NULL, the name of
198 * the area will be set to "cmaN", where N is a running counter of
199 * used areas.
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700200 * @res_cma: Pointer to store the created cma region.
201 *
202 * This function creates custom contiguous area from already reserved memory.
203 */
204int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Sasha Levinac173822015-04-14 15:47:04 -0700205 unsigned int order_per_bit,
Laura Abbottf318dd02017-04-18 11:27:03 -0700206 const char *name,
Sasha Levinac173822015-04-14 15:47:04 -0700207 struct cma **res_cma)
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700208{
209 struct cma *cma;
210 phys_addr_t alignment;
211
212 /* Sanity checks */
213 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
214 pr_err("Not enough slots for CMA reserved regions!\n");
215 return -ENOSPC;
216 }
217
218 if (!size || !memblock_is_region_reserved(base, size))
219 return -EINVAL;
220
Shailendra Verma0f96ae22015-06-24 16:58:03 -0700221 /* ensure minimal alignment required by mm core */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700222 alignment = PAGE_SIZE <<
223 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700224
225 /* alignment should be aligned with order_per_bit */
226 if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
227 return -EINVAL;
228
229 if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
230 return -EINVAL;
231
232 /*
233 * Each reserved area must be initialised later, when more kernel
234 * subsystems (like slab allocator) are available.
235 */
236 cma = &cma_areas[cma_area_count];
Laura Abbottf318dd02017-04-18 11:27:03 -0700237 if (name) {
238 cma->name = name;
239 } else {
240 cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count);
241 if (!cma->name)
242 return -ENOMEM;
243 }
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700244 cma->base_pfn = PFN_DOWN(base);
245 cma->count = size >> PAGE_SHIFT;
246 cma->order_per_bit = order_per_bit;
247 *res_cma = cma;
248 cma_area_count++;
George G. Davis94737a82015-02-11 15:26:27 -0800249 totalcma_pages += (size / PAGE_SIZE);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700250
251 return 0;
252}
253
254/**
Joonsoo Kima2541292014-08-06 16:05:25 -0700255 * cma_declare_contiguous() - reserve custom contiguous area
Joonsoo Kima2541292014-08-06 16:05:25 -0700256 * @base: Base address of the reserved area optional, use 0 for any
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700257 * @size: Size of the reserved area (in bytes),
Joonsoo Kima2541292014-08-06 16:05:25 -0700258 * @limit: End address of the reserved memory (optional, 0 for any).
259 * @alignment: Alignment for the CMA area, should be power of 2 or zero
260 * @order_per_bit: Order of pages represented by one bit on bitmap.
Joonsoo Kima2541292014-08-06 16:05:25 -0700261 * @fixed: hint about where to place the reserved area
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700262 * @name: The name of the area. See function cma_init_reserved_mem()
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700263 * @res_cma: Pointer to store the created cma region.
Joonsoo Kima2541292014-08-06 16:05:25 -0700264 *
265 * This function reserves memory from early allocator. It should be
266 * called by arch specific code once the early allocator (memblock or bootmem)
267 * has been activated and all other subsystems have already allocated/reserved
268 * memory. This function allows to create custom reserved areas.
269 *
270 * If @fixed is true, reserve contiguous area at exactly @base. If false,
271 * reserve in range from @base to @limit.
272 */
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700273int __init cma_declare_contiguous(phys_addr_t base,
274 phys_addr_t size, phys_addr_t limit,
Joonsoo Kima2541292014-08-06 16:05:25 -0700275 phys_addr_t alignment, unsigned int order_per_bit,
Laura Abbottf318dd02017-04-18 11:27:03 -0700276 bool fixed, const char *name, struct cma **res_cma)
Joonsoo Kima2541292014-08-06 16:05:25 -0700277{
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700278 phys_addr_t memblock_end = memblock_end_of_DRAM();
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800279 phys_addr_t highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700280 int ret = 0;
281
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800282 /*
Laura Abbott2dece442017-01-10 13:35:41 -0800283 * We can't use __pa(high_memory) directly, since high_memory
284 * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
285 * complain. Find the boundary by adding one to the last valid
286 * address.
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800287 */
Laura Abbott2dece442017-01-10 13:35:41 -0800288 highmem_start = __pa(high_memory - 1) + 1;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300289 pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
290 __func__, &size, &base, &limit, &alignment);
Joonsoo Kima2541292014-08-06 16:05:25 -0700291
292 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
293 pr_err("Not enough slots for CMA reserved regions!\n");
294 return -ENOSPC;
295 }
296
297 if (!size)
298 return -EINVAL;
299
300 if (alignment && !is_power_of_2(alignment))
301 return -EINVAL;
302
303 /*
304 * Sanitise input arguments.
305 * Pages both ends in CMA area could be merged into adjacent unmovable
306 * migratetype page by page allocator's buddy algorithm. In the case,
307 * you couldn't get a contiguous memory, which is not what we want.
308 */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700309 alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
310 max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
Doug Berger439c79e2019-07-16 16:26:24 -0700311 if (fixed && base & (alignment - 1)) {
312 ret = -EINVAL;
313 pr_err("Region at %pa must be aligned to %pa bytes\n",
314 &base, &alignment);
315 goto err;
316 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700317 base = ALIGN(base, alignment);
318 size = ALIGN(size, alignment);
319 limit &= ~(alignment - 1);
320
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300321 if (!base)
322 fixed = false;
323
Joonsoo Kima2541292014-08-06 16:05:25 -0700324 /* size should be aligned with order_per_bit */
325 if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
326 return -EINVAL;
327
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700328 /*
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300329 * If allocating at a fixed base the request region must not cross the
330 * low/high memory boundary.
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700331 */
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300332 if (fixed && base < highmem_start && base + size > highmem_start) {
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700333 ret = -EINVAL;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300334 pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
335 &base, &highmem_start);
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700336 goto err;
337 }
338
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300339 /*
340 * If the limit is unspecified or above the memblock end, its effective
341 * value will be the memblock end. Set it explicitly to simplify further
342 * checks.
343 */
344 if (limit == 0 || limit > memblock_end)
345 limit = memblock_end;
346
Doug Berger439c79e2019-07-16 16:26:24 -0700347 if (base + size > limit) {
348 ret = -EINVAL;
349 pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n",
350 &size, &base, &limit);
351 goto err;
352 }
353
Joonsoo Kima2541292014-08-06 16:05:25 -0700354 /* Reserve memory */
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300355 if (fixed) {
Joonsoo Kima2541292014-08-06 16:05:25 -0700356 if (memblock_is_region_reserved(base, size) ||
357 memblock_reserve(base, size) < 0) {
358 ret = -EBUSY;
359 goto err;
360 }
361 } else {
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300362 phys_addr_t addr = 0;
363
364 /*
365 * All pages in the reserved area must come from the same zone.
366 * If the requested region crosses the low/high memory boundary,
367 * try allocating from high memory first and fall back to low
368 * memory in case of failure.
369 */
370 if (base < highmem_start && limit > highmem_start) {
371 addr = memblock_alloc_range(size, alignment,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700372 highmem_start, limit,
373 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300374 limit = highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700375 }
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300376
377 if (!addr) {
378 addr = memblock_alloc_range(size, alignment, base,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700379 limit,
380 MEMBLOCK_NONE);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300381 if (!addr) {
382 ret = -ENOMEM;
383 goto err;
384 }
385 }
386
Thierry Reding620951e2014-12-12 16:58:31 -0800387 /*
388 * kmemleak scans/reads tracked objects for pointers to other
389 * objects but this address isn't mapped and accessible
390 */
Catalin Marinas9099dae2016-10-11 13:55:11 -0700391 kmemleak_ignore_phys(addr);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300392 base = addr;
Joonsoo Kima2541292014-08-06 16:05:25 -0700393 }
394
Laura Abbottf318dd02017-04-18 11:27:03 -0700395 ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700396 if (ret)
Peng Fanf555b002019-03-05 15:49:50 -0800397 goto free_mem;
Joonsoo Kima2541292014-08-06 16:05:25 -0700398
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300399 pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
400 &base);
Joonsoo Kima2541292014-08-06 16:05:25 -0700401 return 0;
402
Peng Fanf555b002019-03-05 15:49:50 -0800403free_mem:
404 memblock_free(base, size);
Joonsoo Kima2541292014-08-06 16:05:25 -0700405err:
Joonsoo Kim0de9d2e2014-08-06 16:05:34 -0700406 pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Joonsoo Kima2541292014-08-06 16:05:25 -0700407 return ret;
408}
409
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800410#ifdef CONFIG_CMA_DEBUG
411static void cma_debug_show_areas(struct cma *cma)
412{
Yue Hu77a01e32019-05-13 17:17:41 -0700413 unsigned long next_zero_bit, next_set_bit, nr_zero;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800414 unsigned long start = 0;
Yue Hu77a01e32019-05-13 17:17:41 -0700415 unsigned long nr_part, nr_total = 0;
416 unsigned long nbits = cma_bitmap_maxno(cma);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800417
418 mutex_lock(&cma->lock);
419 pr_info("number of available pages: ");
420 for (;;) {
Yue Hu77a01e32019-05-13 17:17:41 -0700421 next_zero_bit = find_next_zero_bit(cma->bitmap, nbits, start);
422 if (next_zero_bit >= nbits)
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800423 break;
Yue Hu77a01e32019-05-13 17:17:41 -0700424 next_set_bit = find_next_bit(cma->bitmap, nbits, next_zero_bit);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800425 nr_zero = next_set_bit - next_zero_bit;
Yue Hu77a01e32019-05-13 17:17:41 -0700426 nr_part = nr_zero << cma->order_per_bit;
427 pr_cont("%s%lu@%lu", nr_total ? "+" : "", nr_part,
428 next_zero_bit);
429 nr_total += nr_part;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800430 start = next_zero_bit + nr_zero;
431 }
Yue Hu77a01e32019-05-13 17:17:41 -0700432 pr_cont("=> %lu free of %lu total pages\n", nr_total, cma->count);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800433 mutex_unlock(&cma->lock);
434}
435#else
436static inline void cma_debug_show_areas(struct cma *cma) { }
437#endif
438
Joonsoo Kima2541292014-08-06 16:05:25 -0700439/**
440 * cma_alloc() - allocate pages from contiguous area
441 * @cma: Contiguous memory region for which the allocation is performed.
442 * @count: Requested number of pages.
443 * @align: Requested alignment of pages (in PAGE_SIZE order).
Marek Szyprowski65182022018-08-17 15:48:57 -0700444 * @no_warn: Avoid printing message about failed allocation
Joonsoo Kima2541292014-08-06 16:05:25 -0700445 *
446 * This function allocates part of contiguous memory on specific
447 * contiguous memory area.
448 */
Lucas Stache2f466e2017-02-24 14:58:41 -0800449struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
Marek Szyprowski65182022018-08-17 15:48:57 -0700450 bool no_warn)
Joonsoo Kima2541292014-08-06 16:05:25 -0700451{
Andrew Morton3acaea62015-11-05 18:50:08 -0800452 unsigned long mask, offset;
453 unsigned long pfn = -1;
454 unsigned long start = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700455 unsigned long bitmap_maxno, bitmap_no, bitmap_count;
Andrey Konovalovbe21fa32018-12-28 00:30:57 -0800456 size_t i;
Joonsoo Kima2541292014-08-06 16:05:25 -0700457 struct page *page = NULL;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800458 int ret = -ENOMEM;
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700459 int retry_after_sleep = 0;
Patrick Daly91fa7cf2019-11-14 18:11:30 -0800460 int max_retries = 20;
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700461 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 Mark97a2d062015-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 Menon098250a2018-08-08 13:23:23 +0530488 if ((retry_after_sleep < max_retries) &&
489 (ret == -EBUSY)) {
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700490 start = 0;
491 /*
492 * update max retries if available free regions
493 * are less.
494 */
495 if (available_regions < 3)
Patrick Daly91fa7cf2019-11-14 18:11:30 -0800496 max_retries = 25;
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700497 available_regions = 0;
498 /*
499 * 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 }
Swathi Sridharbbbc80b2018-07-13 10:02:08 -0700515
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);
Lucas Stachca96b622017-02-24 14:58:37 -0800527 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
Marek Szyprowski65182022018-08-17 15:48:57 -0700528 GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
Joonsoo Kima2541292014-08-06 16:05:25 -0700529 mutex_unlock(&cma_mutex);
530 if (ret == 0) {
531 page = pfn_to_page(pfn);
532 break;
Joonsoo Kima2541292014-08-06 16:05:25 -0700533 }
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700534
Joonsoo Kima2541292014-08-06 16:05:25 -0700535 cma_clear_bitmap(cma, pfn, count);
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700536 if (ret != -EBUSY)
537 break;
538
Joonsoo Kima2541292014-08-06 16:05:25 -0700539 pr_debug("%s(): memory range at %p is busy, retrying\n",
540 __func__, pfn_to_page(pfn));
Liam Mark97a2d062015-11-25 14:37:54 -0800541
542 trace_cma_alloc_busy_retry(pfn, pfn_to_page(pfn), count, align);
Joonsoo Kima2541292014-08-06 16:05:25 -0700543 /* try again with a bit different memory target */
544 start = bitmap_no + mask + 1;
545 }
546
Andrew Morton3acaea62015-11-05 18:50:08 -0800547 trace_cma_alloc(pfn, page, count, align);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700548
Andrey Konovalovbe21fa32018-12-28 00:30:57 -0800549 /*
550 * CMA can allocate multiple page blocks, which results in different
551 * blocks being marked with different tags. Reset the tags to ignore
552 * those page blocks.
553 */
554 if (page) {
555 for (i = 0; i < count; i++)
556 page_kasan_tag_reset(page + i);
557 }
558
Marek Szyprowski65182022018-08-17 15:48:57 -0700559 if (ret && !no_warn) {
Patrick Daly3df983b2019-07-19 17:38:57 -0700560 pr_err("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n",
561 __func__, cma->name, cma->count, ret);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800562 cma_debug_show_areas(cma);
563 }
564
Joonsoo Kima2541292014-08-06 16:05:25 -0700565 pr_debug("%s(): returned %p\n", __func__, page);
566 return page;
567}
568
569/**
570 * cma_release() - release allocated pages
571 * @cma: Contiguous memory region for which the allocation is performed.
572 * @pages: Allocated pages.
573 * @count: Number of allocated pages.
574 *
575 * This function releases memory allocated by alloc_cma().
576 * It returns false when provided pages do not belong to contiguous area and
577 * true otherwise.
578 */
Sasha Levinac173822015-04-14 15:47:04 -0700579bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -0700580{
581 unsigned long pfn;
582
583 if (!cma || !pages)
584 return false;
585
586 pr_debug("%s(page %p)\n", __func__, (void *)pages);
587
588 pfn = page_to_pfn(pages);
589
590 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
591 return false;
592
593 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
594
595 free_contig_range(pfn, count);
596 cma_clear_bitmap(cma, pfn, count);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700597 trace_cma_release(pfn, pages, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700598
599 return true;
600}
Laura Abbotte4231bc2017-04-18 11:27:04 -0700601
602int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
603{
604 int i;
605
606 for (i = 0; i < cma_area_count; i++) {
607 int ret = it(&cma_areas[i], data);
608
609 if (ret)
610 return ret;
611 }
612
613 return 0;
614}