• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
13  */
14 
15 #define pr_fmt(fmt) "cma: " fmt
16 
17 #define CREATE_TRACE_POINTS
18 
19 #include <linux/memblock.h>
20 #include <linux/err.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/sizes.h>
24 #include <linux/slab.h>
25 #include <linux/log2.h>
26 #include <linux/cma.h>
27 #include <linux/highmem.h>
28 #include <linux/io.h>
29 #include <linux/kmemleak.h>
30 #include <linux/sched.h>
31 #include <linux/jiffies.h>
32 #include <linux/gcma.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/cma.h>
35 #undef CREATE_TRACE_POINTS
36 #include <trace/hooks/mm.h>
37 
38 #include "internal.h"
39 #include "cma.h"
40 
41 #undef CREATE_TRACE_POINTS
42 #ifndef __GENKSYMS__
43 #include <trace/hooks/mm.h>
44 #endif
45 
46 EXPORT_TRACEPOINT_SYMBOL_GPL(cma_alloc_start);
47 EXPORT_TRACEPOINT_SYMBOL_GPL(cma_alloc_finish);
48 EXPORT_TRACEPOINT_SYMBOL_GPL(cma_alloc_busy_retry);
49 EXPORT_TRACEPOINT_SYMBOL_GPL(cma_release);
50 
51 struct cma cma_areas[MAX_CMA_AREAS];
52 unsigned cma_area_count;
53 static DEFINE_MUTEX(cma_mutex);
54 
cma_get_base(const struct cma * cma)55 phys_addr_t cma_get_base(const struct cma *cma)
56 {
57 	return PFN_PHYS(cma->base_pfn);
58 }
59 
cma_get_size(const struct cma * cma)60 unsigned long cma_get_size(const struct cma *cma)
61 {
62 	return cma->count << PAGE_SHIFT;
63 }
64 
cma_get_name(const struct cma * cma)65 const char *cma_get_name(const struct cma *cma)
66 {
67 	return cma->name;
68 }
69 EXPORT_SYMBOL_GPL(cma_get_name);
70 
cma_bitmap_aligned_mask(const struct cma * cma,unsigned int align_order)71 static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
72 					     unsigned int align_order)
73 {
74 	if (align_order <= cma->order_per_bit)
75 		return 0;
76 	return (1UL << (align_order - cma->order_per_bit)) - 1;
77 }
78 
79 /*
80  * Find the offset of the base PFN from the specified align_order.
81  * The value returned is represented in order_per_bits.
82  */
cma_bitmap_aligned_offset(const struct cma * cma,unsigned int align_order)83 static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
84 					       unsigned int align_order)
85 {
86 	return (cma->base_pfn & ((1UL << align_order) - 1))
87 		>> cma->order_per_bit;
88 }
89 
cma_bitmap_pages_to_bits(const struct cma * cma,unsigned long pages)90 static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
91 					      unsigned long pages)
92 {
93 	return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
94 }
95 
cma_clear_bitmap(struct cma * cma,unsigned long pfn,unsigned long count)96 static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
97 			     unsigned long count)
98 {
99 	unsigned long bitmap_no, bitmap_count;
100 	unsigned long flags;
101 
102 	bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
103 	bitmap_count = cma_bitmap_pages_to_bits(cma, count);
104 
105 	spin_lock_irqsave(&cma->lock, flags);
106 	bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
107 	spin_unlock_irqrestore(&cma->lock, flags);
108 }
109 
cma_activate_area(struct cma * cma)110 static void __init cma_activate_area(struct cma *cma)
111 {
112 	unsigned long base_pfn = cma->base_pfn, pfn;
113 	struct zone *zone;
114 
115 	cma->bitmap = bitmap_zalloc(cma_bitmap_maxno(cma), GFP_KERNEL);
116 	if (!cma->bitmap)
117 		goto out_error;
118 
119 	/*
120 	 * alloc_contig_range() requires the pfn range specified to be in the
121 	 * same zone. Simplify by forcing the entire CMA resv range to be in the
122 	 * same zone.
123 	 */
124 	WARN_ON_ONCE(!pfn_valid(base_pfn));
125 	zone = page_zone(pfn_to_page(base_pfn));
126 	for (pfn = base_pfn + 1; pfn < base_pfn + cma->count; pfn++) {
127 		WARN_ON_ONCE(!pfn_valid(pfn));
128 		if (page_zone(pfn_to_page(pfn)) != zone)
129 			goto not_in_zone;
130 	}
131 
132 	if (cma->gcma) {
133 		register_gcma_area(cma->name, PFN_PHYS(base_pfn),
134 				   cma->count << PAGE_SHIFT);
135 	} else {
136 		for (pfn = base_pfn; pfn < base_pfn + cma->count;
137 		     pfn += pageblock_nr_pages)
138 			init_cma_reserved_pageblock(pfn_to_page(pfn));
139 	}
140 
141 	spin_lock_init(&cma->lock);
142 
143 #ifdef CONFIG_CMA_DEBUGFS
144 	INIT_HLIST_HEAD(&cma->mem_head);
145 	spin_lock_init(&cma->mem_head_lock);
146 #endif
147 
148 	android_init_vendor_data(cma, 1);
149 
150 	return;
151 
152 not_in_zone:
153 	bitmap_free(cma->bitmap);
154 out_error:
155 	/* Expose all pages to the buddy, they are useless for CMA. */
156 	if (!cma->reserve_pages_on_error) {
157 		for (pfn = base_pfn; pfn < base_pfn + cma->count; pfn++)
158 			free_reserved_page(pfn_to_page(pfn));
159 	}
160 	totalcma_pages -= cma->count;
161 	cma->count = 0;
162 	pr_err("CMA area %s could not be activated\n", cma->name);
163 	return;
164 }
165 
cma_init_reserved_areas(void)166 static int __init cma_init_reserved_areas(void)
167 {
168 	int i;
169 
170 	for (i = 0; i < cma_area_count; i++)
171 		cma_activate_area(&cma_areas[i]);
172 
173 	return 0;
174 }
175 core_initcall(cma_init_reserved_areas);
176 
cma_reserve_pages_on_error(struct cma * cma)177 void __init cma_reserve_pages_on_error(struct cma *cma)
178 {
179 	cma->reserve_pages_on_error = true;
180 }
181 
182 /**
183  * cma_init_reserved_mem() - create custom contiguous area from reserved memory
184  * @base: Base address of the reserved area
185  * @size: Size of the reserved area (in bytes),
186  * @order_per_bit: Order of pages represented by one bit on bitmap.
187  * @name: The name of the area. If this parameter is NULL, the name of
188  *        the area will be set to "cmaN", where N is a running counter of
189  *        used areas.
190  * @res_cma: Pointer to store the created cma region.
191  *
192  * This function creates custom contiguous area from already reserved memory.
193  */
cma_init_reserved_mem(phys_addr_t base,phys_addr_t size,unsigned int order_per_bit,const char * name,struct cma ** res_cma,bool gcma)194 int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
195 				 unsigned int order_per_bit,
196 				 const char *name,
197 				 struct cma **res_cma, bool gcma)
198 {
199 	struct cma *cma;
200 
201 	/* Sanity checks */
202 	if (cma_area_count == ARRAY_SIZE(cma_areas)) {
203 		pr_err("Not enough slots for CMA reserved regions!\n");
204 		return -ENOSPC;
205 	}
206 
207 	if (!size || !memblock_is_region_reserved(base, size))
208 		return -EINVAL;
209 
210 	/* ensure minimal alignment required by mm core */
211 	if (!IS_ALIGNED(base | size, CMA_MIN_ALIGNMENT_BYTES))
212 		return -EINVAL;
213 
214 	/*
215 	 * Each reserved area must be initialised later, when more kernel
216 	 * subsystems (like slab allocator) are available.
217 	 */
218 	cma = &cma_areas[cma_area_count];
219 
220 	if (name)
221 		snprintf(cma->name, CMA_MAX_NAME, name);
222 	else
223 		snprintf(cma->name, CMA_MAX_NAME,
224 			 gcma ? "gcma%d\n" : "cma%d\n", cma_area_count);
225 
226 	cma->base_pfn = PFN_DOWN(base);
227 	cma->count = size >> PAGE_SHIFT;
228 	cma->order_per_bit = order_per_bit;
229 	cma->gcma = gcma;
230 	*res_cma = cma;
231 	cma_area_count++;
232 	totalcma_pages += cma->count;
233 
234 	return 0;
235 }
236 
237 /**
238  * cma_declare_contiguous_nid() - reserve custom contiguous area
239  * @base: Base address of the reserved area optional, use 0 for any
240  * @size: Size of the reserved area (in bytes),
241  * @limit: End address of the reserved memory (optional, 0 for any).
242  * @alignment: Alignment for the CMA area, should be power of 2 or zero
243  * @order_per_bit: Order of pages represented by one bit on bitmap.
244  * @fixed: hint about where to place the reserved area
245  * @name: The name of the area. See function cma_init_reserved_mem()
246  * @res_cma: Pointer to store the created cma region.
247  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
248  *
249  * This function reserves memory from early allocator. It should be
250  * called by arch specific code once the early allocator (memblock or bootmem)
251  * has been activated and all other subsystems have already allocated/reserved
252  * memory. This function allows to create custom reserved areas.
253  *
254  * If @fixed is true, reserve contiguous area at exactly @base.  If false,
255  * reserve in range from @base to @limit.
256  */
cma_declare_contiguous_nid(phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit,bool fixed,const char * name,struct cma ** res_cma,int nid)257 int __init cma_declare_contiguous_nid(phys_addr_t base,
258 			phys_addr_t size, phys_addr_t limit,
259 			phys_addr_t alignment, unsigned int order_per_bit,
260 			bool fixed, const char *name, struct cma **res_cma,
261 			int nid)
262 {
263 	phys_addr_t memblock_end = memblock_end_of_DRAM();
264 	phys_addr_t highmem_start;
265 	int ret;
266 
267 	/*
268 	 * We can't use __pa(high_memory) directly, since high_memory
269 	 * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly)
270 	 * complain. Find the boundary by adding one to the last valid
271 	 * address.
272 	 */
273 	highmem_start = __pa(high_memory - 1) + 1;
274 	pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
275 		__func__, &size, &base, &limit, &alignment);
276 
277 	if (cma_area_count == ARRAY_SIZE(cma_areas)) {
278 		pr_err("Not enough slots for CMA reserved regions!\n");
279 		return -ENOSPC;
280 	}
281 
282 	if (!size)
283 		return -EINVAL;
284 
285 	if (alignment && !is_power_of_2(alignment))
286 		return -EINVAL;
287 
288 	if (!IS_ENABLED(CONFIG_NUMA))
289 		nid = NUMA_NO_NODE;
290 
291 	/* Sanitise input arguments. */
292 	alignment = max_t(phys_addr_t, alignment, CMA_MIN_ALIGNMENT_BYTES);
293 	if (fixed && base & (alignment - 1)) {
294 		ret = -EINVAL;
295 		pr_err("Region at %pa must be aligned to %pa bytes\n",
296 			&base, &alignment);
297 		goto err;
298 	}
299 	base = ALIGN(base, alignment);
300 	size = ALIGN(size, alignment);
301 	limit &= ~(alignment - 1);
302 
303 	if (!base)
304 		fixed = false;
305 
306 	/* size should be aligned with order_per_bit */
307 	if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
308 		return -EINVAL;
309 
310 	/*
311 	 * If allocating at a fixed base the request region must not cross the
312 	 * low/high memory boundary.
313 	 */
314 	if (fixed && base < highmem_start && base + size > highmem_start) {
315 		ret = -EINVAL;
316 		pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
317 			&base, &highmem_start);
318 		goto err;
319 	}
320 
321 	/*
322 	 * If the limit is unspecified or above the memblock end, its effective
323 	 * value will be the memblock end. Set it explicitly to simplify further
324 	 * checks.
325 	 */
326 	if (limit == 0 || limit > memblock_end)
327 		limit = memblock_end;
328 
329 	if (base + size > limit) {
330 		ret = -EINVAL;
331 		pr_err("Size (%pa) of region at %pa exceeds limit (%pa)\n",
332 			&size, &base, &limit);
333 		goto err;
334 	}
335 
336 	/* Reserve memory */
337 	if (fixed) {
338 		if (memblock_is_region_reserved(base, size) ||
339 		    memblock_reserve(base, size) < 0) {
340 			ret = -EBUSY;
341 			goto err;
342 		}
343 	} else {
344 		phys_addr_t addr = 0;
345 
346 		/*
347 		 * If there is enough memory, try a bottom-up allocation first.
348 		 * It will place the new cma area close to the start of the node
349 		 * and guarantee that the compaction is moving pages out of the
350 		 * cma area and not into it.
351 		 * Avoid using first 4GB to not interfere with constrained zones
352 		 * like DMA/DMA32.
353 		 */
354 #ifdef CONFIG_PHYS_ADDR_T_64BIT
355 		if (!memblock_bottom_up() && memblock_end >= SZ_4G + size) {
356 			memblock_set_bottom_up(true);
357 			addr = memblock_alloc_range_nid(size, alignment, SZ_4G,
358 							limit, nid, true);
359 			memblock_set_bottom_up(false);
360 		}
361 #endif
362 
363 		/*
364 		 * All pages in the reserved area must come from the same zone.
365 		 * If the requested region crosses the low/high memory boundary,
366 		 * try allocating from high memory first and fall back to low
367 		 * memory in case of failure.
368 		 */
369 		if (!addr && base < highmem_start && limit > highmem_start) {
370 			addr = memblock_alloc_range_nid(size, alignment,
371 					highmem_start, limit, nid, true);
372 			limit = highmem_start;
373 		}
374 
375 		if (!addr) {
376 			addr = memblock_alloc_range_nid(size, alignment, base,
377 					limit, nid, true);
378 			if (!addr) {
379 				ret = -ENOMEM;
380 				goto err;
381 			}
382 		}
383 
384 		/*
385 		 * kmemleak scans/reads tracked objects for pointers to other
386 		 * objects but this address isn't mapped and accessible
387 		 */
388 		kmemleak_ignore_phys(addr);
389 		base = addr;
390 	}
391 
392 	ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma,
393 				    false);
394 	if (ret)
395 		goto free_mem;
396 
397 	pr_info("Reserved %ld MiB at %pa on node %d\n", (unsigned long)size / SZ_1M,
398 		&base, nid);
399 	return 0;
400 
401 free_mem:
402 	memblock_phys_free(base, size);
403 err:
404 	pr_err("Failed to reserve %ld MiB on node %d\n", (unsigned long)size / SZ_1M,
405 	       nid);
406 	return ret;
407 }
408 
cma_debug_show_areas(struct cma * cma)409 static void cma_debug_show_areas(struct cma *cma)
410 {
411 	unsigned long next_zero_bit, next_set_bit, nr_zero;
412 	unsigned long start = 0;
413 	unsigned long nr_part, nr_total = 0;
414 	unsigned long nbits = cma_bitmap_maxno(cma);
415 
416 	spin_lock_irq(&cma->lock);
417 	pr_info("number of available pages: ");
418 	for (;;) {
419 		next_zero_bit = find_next_zero_bit(cma->bitmap, nbits, start);
420 		if (next_zero_bit >= nbits)
421 			break;
422 		next_set_bit = find_next_bit(cma->bitmap, nbits, next_zero_bit);
423 		nr_zero = next_set_bit - next_zero_bit;
424 		nr_part = nr_zero << cma->order_per_bit;
425 		pr_cont("%s%lu@%lu", nr_total ? "+" : "", nr_part,
426 			next_zero_bit);
427 		nr_total += nr_part;
428 		start = next_zero_bit + nr_zero;
429 	}
430 	pr_cont("=> %lu free of %lu total pages\n", nr_total, cma->count);
431 	spin_unlock_irq(&cma->lock);
432 }
433 
__cma_alloc(struct cma * cma,unsigned long count,unsigned int align,gfp_t gfp)434 struct page *__cma_alloc(struct cma *cma, unsigned long count,
435 				unsigned int align, gfp_t gfp)
436 {
437 	unsigned long mask, offset;
438 	unsigned long pfn = -1;
439 	unsigned long start = 0;
440 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
441 	unsigned long i;
442 	struct page *page = NULL;
443 	int ret = -ENOMEM;
444 	const char *name = cma ? cma->name : NULL;
445 	int num_attempts = 0;
446 	int max_retries = 5;
447 	bool bypass = false;
448 
449 	if (WARN_ON_ONCE((gfp & GFP_KERNEL) == 0 ||
450 		(gfp & ~(GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY)) != 0))
451 		return page;
452 
453 	trace_android_vh_cma_alloc_bypass(cma, count, align, gfp,
454 				&page, &bypass);
455 	trace_android_vh_cma_alloc_start(cma);
456 
457 	if (bypass)
458 		return page;
459 
460 	trace_cma_alloc_start(name, count, align);
461 
462 	if (!cma || !cma->count || !cma->bitmap)
463 		return page;
464 
465 	pr_debug("%s(cma %p, name: %s, count %lu, align %d)\n", __func__,
466 		(void *)cma, cma->name, count, align);
467 
468 	if (!count)
469 		return page;
470 
471 	mask = cma_bitmap_aligned_mask(cma, align);
472 	offset = cma_bitmap_aligned_offset(cma, align);
473 	bitmap_maxno = cma_bitmap_maxno(cma);
474 	bitmap_count = cma_bitmap_pages_to_bits(cma, count);
475 
476 	if (bitmap_count > bitmap_maxno)
477 		return page;
478 
479 	trace_android_vh_cma_alloc_retry(cma->name, &max_retries);
480 	for (;;) {
481 		spin_lock_irq(&cma->lock);
482 		bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
483 				bitmap_maxno, start, bitmap_count, mask,
484 				offset);
485 #ifdef CONFIG_ANDROID_VENDOR_OEM_DATA
486 		trace_android_rvh_bitmap_find_best_next_area(cma->bitmap,
487 				bitmap_maxno, start, bitmap_count, mask,
488 				offset, &bitmap_no, cma->android_vendor_data1);
489 #endif
490 		if (bitmap_no >= bitmap_maxno) {
491 			if ((num_attempts < max_retries) && (ret == -EBUSY)) {
492 				spin_unlock_irq(&cma->lock);
493 
494 				if (fatal_signal_pending(current) ||
495 				    (gfp & __GFP_NORETRY))
496 					break;
497 
498 				/*
499 				 * Page may be momentarily pinned by some other
500 				 * process which has been scheduled out, e.g.
501 				 * in exit path, during unmap call, or process
502 				 * fork and so cannot be freed there. Sleep
503 				 * for 100ms and retry the allocation.
504 				 */
505 				start = 0;
506 				ret = -ENOMEM;
507 				schedule_timeout_killable(msecs_to_jiffies(100));
508 				num_attempts++;
509 				continue;
510 			} else {
511 				spin_unlock_irq(&cma->lock);
512 				break;
513 			}
514 		}
515 		bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
516 		/*
517 		 * It's safe to drop the lock here. We've marked this region for
518 		 * our exclusive use. If the migration fails we will take the
519 		 * lock again and unmark it.
520 		 */
521 		spin_unlock_irq(&cma->lock);
522 
523 		pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
524 		mutex_lock(&cma_mutex);
525 		if (cma->gcma) {
526 			gcma_alloc_range(pfn, pfn + count - 1);
527 			ret = 0;
528 		} else {
529 			ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp);
530 		}
531 		mutex_unlock(&cma_mutex);
532 		if (ret == 0) {
533 			page = pfn_to_page(pfn);
534 			break;
535 		}
536 
537 		cma_clear_bitmap(cma, pfn, count);
538 		if (ret != -EBUSY)
539 			break;
540 
541 		pr_debug("%s(): memory range at pfn 0x%lx %p is busy, retrying\n",
542 			 __func__, pfn, pfn_to_page(pfn));
543 		trace_android_vh_cma_alloc_busy_info(&pfn);
544 		trace_cma_alloc_busy_retry(cma->name, pfn, pfn_to_page(pfn),
545 					   count, align);
546 		/* try again with a bit different memory target */
547 		start = bitmap_no + mask + 1;
548 	}
549 
550 	/*
551 	 * CMA can allocate multiple page blocks, which results in different
552 	 * blocks being marked with different tags. Reset the tags to ignore
553 	 * those page blocks.
554 	 */
555 	if (page) {
556 		for (i = 0; i < count; i++)
557 			page_kasan_tag_reset(nth_page(page, i));
558 	}
559 
560 	if (ret && !(gfp & __GFP_NOWARN)) {
561 		trace_android_vh_cma_alloc_fail(cma->name, cma->count, count);
562 		pr_err_ratelimited("%s: %s: alloc failed, req-size: %lu pages, ret: %d\n",
563 				   __func__, cma->name, count, ret);
564 		cma_debug_show_areas(cma);
565 	}
566 
567 	pr_debug("%s(): returned %p\n", __func__, page);
568 	trace_cma_alloc_finish(name, pfn, page, count, align, ret);
569 	trace_android_vh_cma_alloc_finish(cma);
570 
571 	if (page) {
572 		count_vm_event(CMA_ALLOC_SUCCESS);
573 		cma_sysfs_account_success_pages(cma, count);
574 	} else {
575 		count_vm_event(CMA_ALLOC_FAIL);
576 		cma_sysfs_account_fail_pages(cma, count);
577 	}
578 
579 	return page;
580 }
581 EXPORT_SYMBOL_GPL(__cma_alloc);
582 
583 /**
584  * cma_alloc() - allocate pages from contiguous area
585  * @cma:   Contiguous memory region for which the allocation is performed.
586  * @count: Requested number of pages.
587  * @align: Requested alignment of pages (in PAGE_SIZE order).
588  * @no_warn: Avoid printing message about failed allocation
589  *
590  * This function allocates part of contiguous memory on specific
591  * contiguous memory area.
592  */
cma_alloc(struct cma * cma,unsigned long count,unsigned int align,bool no_warn)593 struct page *cma_alloc(struct cma *cma, unsigned long count,
594 		       unsigned int align, bool no_warn)
595 {
596 	return __cma_alloc(cma, count, align, GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
597 }
598 EXPORT_SYMBOL_GPL(cma_alloc);
599 
cma_alloc_folio(struct cma * cma,int order,gfp_t gfp)600 struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp)
601 {
602 	struct page *page;
603 
604 	if (WARN_ON(!order || !(gfp & __GFP_COMP)))
605 		return NULL;
606 
607 	page = __cma_alloc(cma, 1 << order, order, gfp);
608 
609 	return page ? page_folio(page) : NULL;
610 }
611 
cma_pages_valid(struct cma * cma,const struct page * pages,unsigned long count)612 bool cma_pages_valid(struct cma *cma, const struct page *pages,
613 		     unsigned long count)
614 {
615 	unsigned long pfn;
616 
617 	if (!cma || !pages)
618 		return false;
619 
620 	pfn = page_to_pfn(pages);
621 
622 	if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) {
623 		pr_debug("%s(page %p, count %lu)\n", __func__,
624 						(void *)pages, count);
625 		return false;
626 	}
627 
628 	return true;
629 }
630 
631 /**
632  * cma_release() - release allocated pages
633  * @cma:   Contiguous memory region for which the allocation is performed.
634  * @pages: Allocated pages.
635  * @count: Number of allocated pages.
636  *
637  * This function releases memory allocated by cma_alloc().
638  * It returns false when provided pages do not belong to contiguous area and
639  * true otherwise.
640  */
cma_release(struct cma * cma,const struct page * pages,unsigned long count)641 bool cma_release(struct cma *cma, const struct page *pages,
642 		 unsigned long count)
643 {
644 	unsigned long pfn;
645 
646 	if (!cma_pages_valid(cma, pages, count))
647 		return false;
648 
649 	pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count);
650 
651 	pfn = page_to_pfn(pages);
652 
653 	VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
654 
655 	if (cma->gcma)
656 		gcma_free_range(pfn, pfn + count - 1);
657 	else
658 		free_contig_range(pfn, count);
659 	cma_clear_bitmap(cma, pfn, count);
660 	cma_sysfs_account_release_pages(cma, count);
661 	trace_cma_release(cma->name, pfn, pages, count);
662 
663 	return true;
664 }
665 EXPORT_SYMBOL_GPL(cma_release);
666 
cma_free_folio(struct cma * cma,const struct folio * folio)667 bool cma_free_folio(struct cma *cma, const struct folio *folio)
668 {
669 	if (WARN_ON(!folio_test_large(folio)))
670 		return false;
671 
672 	return cma_release(cma, &folio->page, folio_nr_pages(folio));
673 }
674 
cma_for_each_area(int (* it)(struct cma * cma,void * data),void * data)675 int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data)
676 {
677 	int i;
678 
679 	for (i = 0; i < cma_area_count; i++) {
680 		int ret = it(&cma_areas[i], data);
681 
682 		if (ret)
683 			return ret;
684 	}
685 
686 	return 0;
687 }
688 EXPORT_SYMBOL_GPL(cma_for_each_area);
689