blob: c8b32947318a5e5d144c4e02957d5d95963fe802 [file] [log] [blame]
Thomas Gleixner8607a962019-05-22 09:51:44 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joonsoo Kima2541292014-08-06 16:05:25 -07002/*
3 * Contiguous Memory Allocator
4 *
5 * Copyright (c) 2010-2011 by Samsung Electronics.
6 * Copyright IBM Corporation, 2013
7 * Copyright LG Electronics Inc., 2014
8 * Written by:
9 * Marek Szyprowski <m.szyprowski@samsung.com>
10 * Michal Nazarewicz <mina86@mina86.com>
11 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
12 * Joonsoo Kim <iamjoonsoo.kim@lge.com>
Joonsoo Kima2541292014-08-06 16:05:25 -070013 */
14
15#define pr_fmt(fmt) "cma: " fmt
16
17#ifdef CONFIG_CMA_DEBUG
18#ifndef DEBUG
19# define DEBUG
20#endif
21#endif
Stefan Strogin99e8ea62015-04-15 16:14:50 -070022#define CREATE_TRACE_POINTS
Joonsoo Kima2541292014-08-06 16:05:25 -070023
24#include <linux/memblock.h>
25#include <linux/err.h>
26#include <linux/mm.h>
Sandeep Patil716306e2019-09-13 14:50:38 -070027#include <linux/module.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070028#include <linux/mutex.h>
29#include <linux/sizes.h>
30#include <linux/slab.h>
31#include <linux/log2.h>
32#include <linux/cma.h>
Marek Szyprowskif7426b92014-10-09 15:26:47 -070033#include <linux/highmem.h>
Thierry Reding620951e2014-12-12 16:58:31 -080034#include <linux/io.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070035#include <linux/kmemleak.h>
Chris Goldsworthy73eda8e2020-09-18 09:19:53 -070036#include <linux/sched.h>
37#include <linux/jiffies.h>
Stefan Strogin99e8ea62015-04-15 16:14:50 -070038#include <trace/events/cma.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070039
Minchan Kimc6e85ea2021-04-01 18:25:57 -070040#undef CREATE_TRACE_POINTS
41#include <trace/hooks/mm.h>
42
Sasha Levin28b24c12015-04-14 15:44:57 -070043#include "cma.h"
Joonsoo Kima2541292014-08-06 16:05:25 -070044
Minchan Kim74f247e2021-10-18 09:11:11 -070045extern void lru_cache_disable(void);
46extern void lru_cache_enable(void);
47
Sasha Levin28b24c12015-04-14 15:44:57 -070048struct cma cma_areas[MAX_CMA_AREAS];
49unsigned cma_area_count;
Joonsoo Kima2541292014-08-06 16:05:25 -070050
Sasha Levinac173822015-04-14 15:47:04 -070051phys_addr_t cma_get_base(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070052{
53 return PFN_PHYS(cma->base_pfn);
54}
55
Sasha Levinac173822015-04-14 15:47:04 -070056unsigned long cma_get_size(const struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -070057{
58 return cma->count << PAGE_SHIFT;
59}
60
Laura Abbottf318dd02017-04-18 11:27:03 -070061const char *cma_get_name(const struct cma *cma)
62{
Barry Song18e98e52020-08-11 18:31:57 -070063 return cma->name;
Laura Abbottf318dd02017-04-18 11:27:03 -070064}
Sandeep Patil716306e2019-09-13 14:50:38 -070065EXPORT_SYMBOL_GPL(cma_get_name);
Laura Abbottf318dd02017-04-18 11:27:03 -070066
Sasha Levinac173822015-04-14 15:47:04 -070067static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070068 unsigned int align_order)
Joonsoo Kima2541292014-08-06 16:05:25 -070069{
Weijie Yang68faed62014-10-13 15:51:03 -070070 if (align_order <= cma->order_per_bit)
71 return 0;
72 return (1UL << (align_order - cma->order_per_bit)) - 1;
Joonsoo Kima2541292014-08-06 16:05:25 -070073}
74
Danesh Petigara850fc432015-03-12 16:25:57 -070075/*
Doug Bergere048cb32017-07-10 15:49:44 -070076 * Find the offset of the base PFN from the specified align_order.
77 * The value returned is represented in order_per_bits.
Danesh Petigara850fc432015-03-12 16:25:57 -070078 */
Sasha Levinac173822015-04-14 15:47:04 -070079static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
Doug Bergere048cb32017-07-10 15:49:44 -070080 unsigned int align_order)
Gregory Fongb5be83e2014-12-12 16:54:48 -080081{
Doug Bergere048cb32017-07-10 15:49:44 -070082 return (cma->base_pfn & ((1UL << align_order) - 1))
83 >> cma->order_per_bit;
Gregory Fongb5be83e2014-12-12 16:54:48 -080084}
85
Sasha Levinac173822015-04-14 15:47:04 -070086static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
87 unsigned long pages)
Joonsoo Kima2541292014-08-06 16:05:25 -070088{
89 return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
90}
91
Sasha Levinac173822015-04-14 15:47:04 -070092static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
93 unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -070094{
95 unsigned long bitmap_no, bitmap_count;
96
97 bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
98 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
99
100 mutex_lock(&cma->lock);
101 bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
102 mutex_unlock(&cma->lock);
103}
104
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700105static void __init cma_activate_area(struct cma *cma)
Joonsoo Kima2541292014-08-06 16:05:25 -0700106{
David Hildenbrand06080c42021-02-25 17:16:37 -0800107 unsigned long base_pfn = cma->base_pfn, pfn;
Joonsoo Kima2541292014-08-06 16:05:25 -0700108 struct zone *zone;
109
Yunfeng Ye2184f992019-11-30 17:57:22 -0800110 cma->bitmap = bitmap_zalloc(cma_bitmap_maxno(cma), GFP_KERNEL);
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700111 if (!cma->bitmap)
112 goto out_error;
Joonsoo Kima2541292014-08-06 16:05:25 -0700113
Jianqun Xu87e83912022-03-24 17:49:08 +0800114 if (IS_ENABLED(CONFIG_CMA_INACTIVE))
115 goto out;
David Hildenbrand06080c42021-02-25 17:16:37 -0800116 /*
117 * alloc_contig_range() requires the pfn range specified to be in the
118 * same zone. Simplify by forcing the entire CMA resv range to be in the
119 * same zone.
120 */
121 WARN_ON_ONCE(!pfn_valid(base_pfn));
122 zone = page_zone(pfn_to_page(base_pfn));
123 for (pfn = base_pfn + 1; pfn < base_pfn + cma->count; pfn++) {
124 WARN_ON_ONCE(!pfn_valid(pfn));
125 if (page_zone(pfn_to_page(pfn)) != zone)
126 goto not_in_zone;
127 }
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900128
David Hildenbrand06080c42021-02-25 17:16:37 -0800129 for (pfn = base_pfn; pfn < base_pfn + cma->count;
130 pfn += pageblock_nr_pages)
131 init_cma_reserved_pageblock(pfn_to_page(pfn));
Joonsoo Kima2541292014-08-06 16:05:25 -0700132
Jianqun Xu87e83912022-03-24 17:49:08 +0800133out:
Joonsoo Kima2541292014-08-06 16:05:25 -0700134 mutex_init(&cma->lock);
Sasha Levin26b02a12015-04-14 15:44:59 -0700135
136#ifdef CONFIG_CMA_DEBUGFS
137 INIT_HLIST_HEAD(&cma->mem_head);
138 spin_lock_init(&cma->mem_head_lock);
139#endif
140
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700141 return;
Joonsoo Kima2541292014-08-06 16:05:25 -0700142
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900143not_in_zone:
Yunfeng Ye2184f992019-11-30 17:57:22 -0800144 bitmap_free(cma->bitmap);
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700145out_error:
David Hildenbrand06080c42021-02-25 17:16:37 -0800146 /* Expose all pages to the buddy, they are useless for CMA. */
147 for (pfn = base_pfn; pfn < base_pfn + cma->count; pfn++)
148 free_reserved_page(pfn_to_page(pfn));
149 totalcma_pages -= cma->count;
Laurent Pinchartf022d8c2014-10-24 13:18:39 +0300150 cma->count = 0;
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700151 pr_err("CMA area %s could not be activated\n", cma->name);
152 return;
Joonsoo Kima2541292014-08-06 16:05:25 -0700153}
154
155static int __init cma_init_reserved_areas(void)
156{
157 int i;
158
Mike Kravetz3a5139f2020-08-11 18:32:03 -0700159 for (i = 0; i < cma_area_count; i++)
160 cma_activate_area(&cma_areas[i]);
Joonsoo Kima2541292014-08-06 16:05:25 -0700161
162 return 0;
163}
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900164core_initcall(cma_init_reserved_areas);
Joonsoo Kima2541292014-08-06 16:05:25 -0700165
166/**
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700167 * cma_init_reserved_mem() - create custom contiguous area from reserved memory
168 * @base: Base address of the reserved area
169 * @size: Size of the reserved area (in bytes),
170 * @order_per_bit: Order of pages represented by one bit on bitmap.
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700171 * @name: The name of the area. If this parameter is NULL, the name of
172 * the area will be set to "cmaN", where N is a running counter of
173 * used areas.
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700174 * @res_cma: Pointer to store the created cma region.
175 *
176 * This function creates custom contiguous area from already reserved memory.
177 */
178int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Sasha Levinac173822015-04-14 15:47:04 -0700179 unsigned int order_per_bit,
Laura Abbottf318dd02017-04-18 11:27:03 -0700180 const char *name,
Sasha Levinac173822015-04-14 15:47:04 -0700181 struct cma **res_cma)
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700182{
183 struct cma *cma;
184 phys_addr_t alignment;
185
186 /* Sanity checks */
187 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
188 pr_err("Not enough slots for CMA reserved regions!\n");
189 return -ENOSPC;
190 }
191
192 if (!size || !memblock_is_region_reserved(base, size))
193 return -EINVAL;
194
Shailendra Verma0f96ae22015-06-24 16:58:03 -0700195 /* ensure minimal alignment required by mm core */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700196 alignment = PAGE_SIZE <<
197 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700198
199 /* alignment should be aligned with order_per_bit */
200 if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
201 return -EINVAL;
202
203 if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
204 return -EINVAL;
205
206 /*
207 * Each reserved area must be initialised later, when more kernel
208 * subsystems (like slab allocator) are available.
209 */
210 cma = &cma_areas[cma_area_count];
Barry Song18e98e52020-08-11 18:31:57 -0700211
212 if (name)
213 snprintf(cma->name, CMA_MAX_NAME, name);
214 else
215 snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count);
216
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700217 cma->base_pfn = PFN_DOWN(base);
218 cma->count = size >> PAGE_SHIFT;
219 cma->order_per_bit = order_per_bit;
220 *res_cma = cma;
221 cma_area_count++;
George G. Davis94737a82015-02-11 15:26:27 -0800222 totalcma_pages += (size / PAGE_SIZE);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700223
224 return 0;
225}
226
227/**
Aslan Bakirov8676af12020-04-10 14:32:42 -0700228 * cma_declare_contiguous_nid() - reserve custom contiguous area
Joonsoo Kima2541292014-08-06 16:05:25 -0700229 * @base: Base address of the reserved area optional, use 0 for any
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700230 * @size: Size of the reserved area (in bytes),
Joonsoo Kima2541292014-08-06 16:05:25 -0700231 * @limit: End address of the reserved memory (optional, 0 for any).
232 * @alignment: Alignment for the CMA area, should be power of 2 or zero
233 * @order_per_bit: Order of pages represented by one bit on bitmap.
Joonsoo Kima2541292014-08-06 16:05:25 -0700234 * @fixed: hint about where to place the reserved area
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700235 * @name: The name of the area. See function cma_init_reserved_mem()
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700236 * @res_cma: Pointer to store the created cma region.
Aslan Bakirov8676af12020-04-10 14:32:42 -0700237 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Joonsoo Kima2541292014-08-06 16:05:25 -0700238 *
239 * This function reserves memory from early allocator. It should be
240 * called by arch specific code once the early allocator (memblock or bootmem)
241 * has been activated and all other subsystems have already allocated/reserved
242 * memory. This function allows to create custom reserved areas.
243 *
244 * If @fixed is true, reserve contiguous area at exactly @base. If false,
245 * reserve in range from @base to @limit.
246 */
Aslan Bakirov8676af12020-04-10 14:32:42 -0700247int __init cma_declare_contiguous_nid(phys_addr_t base,
Joonsoo Kimc1f733aa2014-08-06 16:05:32 -0700248 phys_addr_t size, phys_addr_t limit,
Joonsoo Kima2541292014-08-06 16:05:25 -0700249 phys_addr_t alignment, unsigned int order_per_bit,
Aslan Bakirov8676af12020-04-10 14:32:42 -0700250 bool fixed, const char *name, struct cma **res_cma,
251 int nid)
Joonsoo Kima2541292014-08-06 16:05:25 -0700252{
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700253 phys_addr_t memblock_end = memblock_end_of_DRAM();
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800254 phys_addr_t highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700255 int ret = 0;
256
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800257 /*
Laura Abbott2dece442017-01-10 13:35:41 -0800258 * We can't use __pa(high_memory) directly, since high_memory
259 * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
260 * complain. Find the boundary by adding one to the last valid
261 * address.
Joonsoo Kim6b101e22014-12-10 15:41:12 -0800262 */
Laura Abbott2dece442017-01-10 13:35:41 -0800263 highmem_start = __pa(high_memory - 1) + 1;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300264 pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
265 __func__, &size, &base, &limit, &alignment);
Joonsoo Kima2541292014-08-06 16:05:25 -0700266
267 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
268 pr_err("Not enough slots for CMA reserved regions!\n");
269 return -ENOSPC;
270 }
271
272 if (!size)
273 return -EINVAL;
274
275 if (alignment && !is_power_of_2(alignment))
276 return -EINVAL;
277
278 /*
279 * Sanitise input arguments.
280 * Pages both ends in CMA area could be merged into adjacent unmovable
281 * migratetype page by page allocator's buddy algorithm. In the case,
282 * you couldn't get a contiguous memory, which is not what we want.
283 */
Stephen Rothwellbadbda52016-05-27 14:27:41 -0700284 alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
285 max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
Doug Bergerc6333242019-07-16 16:26:24 -0700286 if (fixed && base & (alignment - 1)) {
287 ret = -EINVAL;
288 pr_err("Region at %pa must be aligned to %pa bytes\n",
289 &base, &alignment);
290 goto err;
291 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700292 base = ALIGN(base, alignment);
293 size = ALIGN(size, alignment);
294 limit &= ~(alignment - 1);
295
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300296 if (!base)
297 fixed = false;
298
Joonsoo Kima2541292014-08-06 16:05:25 -0700299 /* size should be aligned with order_per_bit */
300 if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
301 return -EINVAL;
302
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700303 /*
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300304 * If allocating at a fixed base the request region must not cross the
305 * low/high memory boundary.
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700306 */
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300307 if (fixed && base < highmem_start && base + size > highmem_start) {
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700308 ret = -EINVAL;
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300309 pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
310 &base, &highmem_start);
Marek Szyprowskif7426b92014-10-09 15:26:47 -0700311 goto err;
312 }
313
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300314 /*
315 * If the limit is unspecified or above the memblock end, its effective
316 * value will be the memblock end. Set it explicitly to simplify further
317 * checks.
318 */
319 if (limit == 0 || limit > memblock_end)
320 limit = memblock_end;
321
Doug Bergerc6333242019-07-16 16:26:24 -0700322 if (base + size > limit) {
323 ret = -EINVAL;
324 pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n",
325 &size, &base, &limit);
326 goto err;
327 }
328
Joonsoo Kima2541292014-08-06 16:05:25 -0700329 /* Reserve memory */
Laurent Pinchart800a85d2014-10-24 13:18:40 +0300330 if (fixed) {
Joonsoo Kima2541292014-08-06 16:05:25 -0700331 if (memblock_is_region_reserved(base, size) ||
332 memblock_reserve(base, size) < 0) {
333 ret = -EBUSY;
334 goto err;
335 }
336 } else {
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300337 phys_addr_t addr = 0;
338
339 /*
340 * All pages in the reserved area must come from the same zone.
341 * If the requested region crosses the low/high memory boundary,
342 * try allocating from high memory first and fall back to low
343 * memory in case of failure.
344 */
345 if (base < highmem_start && limit > highmem_start) {
Aslan Bakirov8676af12020-04-10 14:32:42 -0700346 addr = memblock_alloc_range_nid(size, alignment,
Barry Song40366bd2020-07-03 15:15:24 -0700347 highmem_start, limit, nid, true);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300348 limit = highmem_start;
Joonsoo Kima2541292014-08-06 16:05:25 -0700349 }
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300350
Roman Gushchin0e0bfc42021-02-25 17:16:33 -0800351 /*
352 * If there is enough memory, try a bottom-up allocation first.
353 * It will place the new cma area close to the start of the node
354 * and guarantee that the compaction is moving pages out of the
355 * cma area and not into it.
356 * Avoid using first 4GB to not interfere with constrained zones
357 * like DMA/DMA32.
358 */
359#ifdef CONFIG_PHYS_ADDR_T_64BIT
360 if (!memblock_bottom_up() && memblock_end >= SZ_4G + size) {
361 memblock_set_bottom_up(true);
362 addr = memblock_alloc_range_nid(size, alignment, SZ_4G,
363 limit, nid, true);
364 memblock_set_bottom_up(false);
365 }
366#endif
367
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300368 if (!addr) {
Aslan Bakirov8676af12020-04-10 14:32:42 -0700369 addr = memblock_alloc_range_nid(size, alignment, base,
Barry Song40366bd2020-07-03 15:15:24 -0700370 limit, nid, true);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300371 if (!addr) {
372 ret = -ENOMEM;
373 goto err;
374 }
375 }
376
Thierry Reding620951e2014-12-12 16:58:31 -0800377 /*
378 * kmemleak scans/reads tracked objects for pointers to other
379 * objects but this address isn't mapped and accessible
380 */
Catalin Marinas9099dae2016-10-11 13:55:11 -0700381 kmemleak_ignore_phys(addr);
Laurent Pinchart16195dd2014-10-24 13:18:41 +0300382 base = addr;
Joonsoo Kima2541292014-08-06 16:05:25 -0700383 }
384
Laura Abbottf318dd02017-04-18 11:27:03 -0700385 ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
Marek Szyprowskide9e14e2014-10-13 15:51:09 -0700386 if (ret)
Peng Fan0d3bd182019-03-05 15:49:50 -0800387 goto free_mem;
Joonsoo Kima2541292014-08-06 16:05:25 -0700388
Laurent Pinchart56fa4f62014-10-24 13:18:42 +0300389 pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
390 &base);
Joonsoo Kima2541292014-08-06 16:05:25 -0700391 return 0;
392
Peng Fan0d3bd182019-03-05 15:49:50 -0800393free_mem:
394 memblock_free(base, size);
Joonsoo Kima2541292014-08-06 16:05:25 -0700395err:
Joonsoo Kim0de9d2e2014-08-06 16:05:34 -0700396 pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Joonsoo Kima2541292014-08-06 16:05:25 -0700397 return ret;
398}
399
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800400#ifdef CONFIG_CMA_DEBUG
401static void cma_debug_show_areas(struct cma *cma)
402{
Yue Hu2b59e012019-05-13 17:17:41 -0700403 unsigned long next_zero_bit, next_set_bit, nr_zero;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800404 unsigned long start = 0;
Yue Hu2b59e012019-05-13 17:17:41 -0700405 unsigned long nr_part, nr_total = 0;
406 unsigned long nbits = cma_bitmap_maxno(cma);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800407
408 mutex_lock(&cma->lock);
409 pr_info("number of available pages: ");
410 for (;;) {
Yue Hu2b59e012019-05-13 17:17:41 -0700411 next_zero_bit = find_next_zero_bit(cma->bitmap, nbits, start);
412 if (next_zero_bit >= nbits)
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800413 break;
Yue Hu2b59e012019-05-13 17:17:41 -0700414 next_set_bit = find_next_bit(cma->bitmap, nbits, next_zero_bit);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800415 nr_zero = next_set_bit - next_zero_bit;
Yue Hu2b59e012019-05-13 17:17:41 -0700416 nr_part = nr_zero << cma->order_per_bit;
417 pr_cont("%s%lu@%lu", nr_total ? "+" : "", nr_part,
418 next_zero_bit);
419 nr_total += nr_part;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800420 start = next_zero_bit + nr_zero;
421 }
Yue Hu2b59e012019-05-13 17:17:41 -0700422 pr_cont("=> %lu free of %lu total pages\n", nr_total, cma->count);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800423 mutex_unlock(&cma->lock);
424}
425#else
426static inline void cma_debug_show_areas(struct cma *cma) { }
427#endif
428
Joonsoo Kima2541292014-08-06 16:05:25 -0700429/**
430 * cma_alloc() - allocate pages from contiguous area
431 * @cma: Contiguous memory region for which the allocation is performed.
432 * @count: Requested number of pages.
433 * @align: Requested alignment of pages (in PAGE_SIZE order).
Minchan Kim23ba9902021-01-21 12:09:34 -0800434 * @gfp_mask: GFP mask to use during the cma allocation.
Joonsoo Kima2541292014-08-06 16:05:25 -0700435 *
436 * This function allocates part of contiguous memory on specific
437 * contiguous memory area.
438 */
Lucas Stache2f466e2017-02-24 14:58:41 -0800439struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
Minchan Kim23ba9902021-01-21 12:09:34 -0800440 gfp_t gfp_mask)
Joonsoo Kima2541292014-08-06 16:05:25 -0700441{
Andrew Morton3acaea62015-11-05 18:50:08 -0800442 unsigned long mask, offset;
443 unsigned long pfn = -1;
444 unsigned long start = 0;
Joonsoo Kima2541292014-08-06 16:05:25 -0700445 unsigned long bitmap_maxno, bitmap_no, bitmap_count;
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800446 size_t i;
Joonsoo Kima2541292014-08-06 16:05:25 -0700447 struct page *page = NULL;
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800448 int ret = -ENOMEM;
Chris Goldsworthy73eda8e2020-09-18 09:19:53 -0700449 int num_attempts = 0;
450 int max_retries = 5;
Minchan Kimc6e85ea2021-04-01 18:25:57 -0700451 s64 ts;
Minchan Kim675e5042021-06-29 11:28:13 -0700452 struct cma_alloc_info cma_info = {0};
Minchan Kimc6e85ea2021-04-01 18:25:57 -0700453
454 trace_android_vh_cma_alloc_start(&ts);
Joonsoo Kima2541292014-08-06 16:05:25 -0700455
Jianqun Xu835832b2020-08-11 18:31:54 -0700456 if (!cma || !cma->count || !cma->bitmap)
Minchan Kim0fb39172021-03-04 12:30:30 -0800457 goto out;
Joonsoo Kima2541292014-08-06 16:05:25 -0700458
Minchan Kim23ba9902021-01-21 12:09:34 -0800459 pr_debug("%s(cma %p, count %zu, align %d gfp_mask 0x%x)\n", __func__,
460 (void *)cma, count, align, gfp_mask);
Joonsoo Kima2541292014-08-06 16:05:25 -0700461
462 if (!count)
Minchan Kim0fb39172021-03-04 12:30:30 -0800463 goto out;
Joonsoo Kima2541292014-08-06 16:05:25 -0700464
Georgi Djakov1cfa9bc2021-04-12 00:02:38 -0400465 trace_cma_alloc_start(cma->name, count, align);
Liam Markc4536432021-04-12 00:02:38 -0400466
Joonsoo Kima2541292014-08-06 16:05:25 -0700467 mask = cma_bitmap_aligned_mask(cma, align);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800468 offset = cma_bitmap_aligned_offset(cma, align);
Joonsoo Kima2541292014-08-06 16:05:25 -0700469 bitmap_maxno = cma_bitmap_maxno(cma);
470 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
471
Shiraz Hashim6b36ba52016-11-10 10:46:16 -0800472 if (bitmap_count > bitmap_maxno)
Minchan Kim0fb39172021-03-04 12:30:30 -0800473 goto out;
Shiraz Hashim6b36ba52016-11-10 10:46:16 -0800474
Minchan Kim74f247e2021-10-18 09:11:11 -0700475 lru_cache_disable();
Joonsoo Kima2541292014-08-06 16:05:25 -0700476 for (;;) {
Minchan Kim675e5042021-06-29 11:28:13 -0700477 struct acr_info info = {0};
478
Joonsoo Kima2541292014-08-06 16:05:25 -0700479 mutex_lock(&cma->lock);
Gregory Fongb5be83e2014-12-12 16:54:48 -0800480 bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
481 bitmap_maxno, start, bitmap_count, mask,
482 offset);
Joonsoo Kima2541292014-08-06 16:05:25 -0700483 if (bitmap_no >= bitmap_maxno) {
Chris Goldsworthy73eda8e2020-09-18 09:19:53 -0700484 if ((num_attempts < max_retries) && (ret == -EBUSY)) {
485 mutex_unlock(&cma->lock);
486
Minchan Kim12f48602021-07-13 08:56:06 -0700487 if (fatal_signal_pending(current) ||
488 (gfp_mask & __GFP_NORETRY))
Chris Goldsworthy73eda8e2020-09-18 09:19:53 -0700489 break;
490
491 /*
492 * Page may be momentarily pinned by some other
493 * process which has been scheduled out, e.g.
494 * in exit path, during unmap call, or process
495 * fork and so cannot be freed there. Sleep
496 * for 100ms and retry the allocation.
497 */
498 start = 0;
499 ret = -ENOMEM;
500 schedule_timeout_killable(msecs_to_jiffies(100));
501 num_attempts++;
502 continue;
503 } else {
504 mutex_unlock(&cma->lock);
505 break;
506 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700507 }
508 bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
509 /*
510 * It's safe to drop the lock here. We've marked this region for
511 * our exclusive use. If the migration fails we will take the
512 * lock again and unmark it.
513 */
514 mutex_unlock(&cma->lock);
515
516 pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
Jianqun Xu87e83912022-03-24 17:49:08 +0800517 if (IS_ENABLED(CONFIG_CMA_INACTIVE)) {
518 page = pfn_to_page(pfn);
519 lru_cache_enable();
520 goto out;
521 }
Minchan Kim675e5042021-06-29 11:28:13 -0700522 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp_mask, &info);
523 cma_info.nr_migrated += info.nr_migrated;
524 cma_info.nr_reclaimed += info.nr_reclaimed;
525 cma_info.nr_mapped += info.nr_mapped;
526 if (info.err) {
527 if (info.err & ACR_ERR_ISOLATE)
528 cma_info.nr_isolate_fail++;
529 if (info.err & ACR_ERR_MIGRATE)
530 cma_info.nr_migrate_fail++;
531 if (info.err & ACR_ERR_TEST)
532 cma_info.nr_test_fail++;
533 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700534 if (ret == 0) {
535 page = pfn_to_page(pfn);
536 break;
Joonsoo Kima2541292014-08-06 16:05:25 -0700537 }
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700538
Joonsoo Kima2541292014-08-06 16:05:25 -0700539 cma_clear_bitmap(cma, pfn, count);
Joonsoo Kimb7155e72014-08-06 16:05:30 -0700540 if (ret != -EBUSY)
541 break;
542
Joonsoo Kima2541292014-08-06 16:05:25 -0700543 pr_debug("%s(): memory range at %p is busy, retrying\n",
544 __func__, pfn_to_page(pfn));
Liam Markc4536432021-04-12 00:02:38 -0400545
Minchan Kim76782b52021-04-12 00:02:38 -0400546 trace_cma_alloc_busy_retry(cma->name, pfn, pfn_to_page(pfn),
547 count, align);
Minchan Kim0e688e92021-06-29 12:08:44 -0700548
549 if (info.failed_pfn && gfp_mask & __GFP_NORETRY) {
550 /* try again from following failed page */
551 start = (pfn_max_align_up(info.failed_pfn + 1) -
552 cma->base_pfn) >> cma->order_per_bit;
553
554 } else {
555 /* try again with a bit different memory target */
556 start = bitmap_no + mask + 1;
557 }
Joonsoo Kima2541292014-08-06 16:05:25 -0700558 }
559
Minchan Kim74f247e2021-10-18 09:11:11 -0700560 lru_cache_enable();
Minchan Kim76782b52021-04-12 00:02:38 -0400561 trace_cma_alloc_finish(cma->name, pfn, page, count, align);
Minchan Kim675e5042021-06-29 11:28:13 -0700562 trace_cma_alloc_info(cma->name, page, count, align, &cma_info);
Stefan Strogin99e8ea62015-04-15 16:14:50 -0700563
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800564 /*
565 * CMA can allocate multiple page blocks, which results in different
566 * blocks being marked with different tags. Reset the tags to ignore
567 * those page blocks.
568 */
569 if (page) {
570 for (i = 0; i < count; i++)
571 page_kasan_tag_reset(page + i);
572 }
573
Minchan Kim23ba9902021-01-21 12:09:34 -0800574 if (ret && !(gfp_mask & __GFP_NOWARN)) {
Patrick Daly0567ea32021-02-25 17:16:44 -0800575 pr_err("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n",
576 __func__, cma->name, count, ret);
Jaewon Kimdbe43d42017-02-24 14:58:50 -0800577 cma_debug_show_areas(cma);
578 }
579
Joonsoo Kima2541292014-08-06 16:05:25 -0700580 pr_debug("%s(): returned %p\n", __func__, page);
Minchan Kim0fb39172021-03-04 12:30:30 -0800581out:
Minchan Kimc6e85ea2021-04-01 18:25:57 -0700582 trace_android_vh_cma_alloc_finish(cma, page, count, align, gfp_mask, ts);
Minchan Kima5903592021-03-24 16:10:53 -0700583 if (page) {
Minchan Kim0fb39172021-03-04 12:30:30 -0800584 count_vm_event(CMA_ALLOC_SUCCESS);
Minchan Kima5903592021-03-24 16:10:53 -0700585 cma_sysfs_account_success_pages(cma, count);
586 } else {
Minchan Kim0fb39172021-03-04 12:30:30 -0800587 count_vm_event(CMA_ALLOC_FAIL);
Minchan Kima5903592021-03-24 16:10:53 -0700588 if (cma)
589 cma_sysfs_account_fail_pages(cma, count);
590 }
Minchan Kim0fb39172021-03-04 12:30:30 -0800591
Joonsoo Kima2541292014-08-06 16:05:25 -0700592 return page;
593}
Sandeep Patil716306e2019-09-13 14:50:38 -0700594EXPORT_SYMBOL_GPL(cma_alloc);
Joonsoo Kima2541292014-08-06 16:05:25 -0700595
596/**
597 * cma_release() - release allocated pages
598 * @cma: Contiguous memory region for which the allocation is performed.
599 * @pages: Allocated pages.
600 * @count: Number of allocated pages.
601 *
Ryohei Suzuki929f92f2019-07-16 16:26:00 -0700602 * This function releases memory allocated by cma_alloc().
Joonsoo Kima2541292014-08-06 16:05:25 -0700603 * It returns false when provided pages do not belong to contiguous area and
604 * true otherwise.
605 */
Sasha Levinac173822015-04-14 15:47:04 -0700606bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
Joonsoo Kima2541292014-08-06 16:05:25 -0700607{
608 unsigned long pfn;
609
610 if (!cma || !pages)
611 return false;
612
Charan Teja Reddybfb68bd2020-12-14 19:13:26 -0800613 pr_debug("%s(page %p, count %u)\n", __func__, (void *)pages, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700614
615 pfn = page_to_pfn(pages);
616
617 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
618 return false;
619
620 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
Jianqun Xu87e83912022-03-24 17:49:08 +0800621 if (!IS_ENABLED(CONFIG_CMA_INACTIVE))
622 free_contig_range(pfn, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700623 cma_clear_bitmap(cma, pfn, count);
Minchan Kim76782b52021-04-12 00:02:38 -0400624 trace_cma_release(cma->name, pfn, pages, count);
Joonsoo Kima2541292014-08-06 16:05:25 -0700625
626 return true;
627}
Sandeep Patil716306e2019-09-13 14:50:38 -0700628EXPORT_SYMBOL_GPL(cma_release);
Laura Abbotte4231bc2017-04-18 11:27:04 -0700629
630int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
631{
632 int i;
633
634 for (i = 0; i < cma_area_count; i++) {
635 int ret = it(&cma_areas[i], data);
636
637 if (ret)
638 return ret;
639 }
640
641 return 0;
642}
Sandeep Patil716306e2019-09-13 14:50:38 -0700643EXPORT_SYMBOL_GPL(cma_for_each_area);