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