.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Contiguous Memory Allocator |
---|
3 | 4 | * |
---|
.. | .. |
---|
9 | 10 | * Michal Nazarewicz <mina86@mina86.com> |
---|
10 | 11 | * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
---|
11 | 12 | * 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 | 13 | */ |
---|
18 | 14 | |
---|
19 | 15 | #define pr_fmt(fmt) "cma: " fmt |
---|
.. | .. |
---|
28 | 24 | #include <linux/memblock.h> |
---|
29 | 25 | #include <linux/err.h> |
---|
30 | 26 | #include <linux/mm.h> |
---|
| 27 | +#include <linux/module.h> |
---|
31 | 28 | #include <linux/mutex.h> |
---|
32 | 29 | #include <linux/sizes.h> |
---|
33 | 30 | #include <linux/slab.h> |
---|
.. | .. |
---|
36 | 33 | #include <linux/highmem.h> |
---|
37 | 34 | #include <linux/io.h> |
---|
38 | 35 | #include <linux/kmemleak.h> |
---|
| 36 | +#include <linux/sched.h> |
---|
| 37 | +#include <linux/jiffies.h> |
---|
39 | 38 | #include <trace/events/cma.h> |
---|
40 | 39 | |
---|
| 40 | +#undef CREATE_TRACE_POINTS |
---|
| 41 | +#include <trace/hooks/mm.h> |
---|
| 42 | + |
---|
41 | 43 | #include "cma.h" |
---|
| 44 | + |
---|
| 45 | +extern void lru_cache_disable(void); |
---|
| 46 | +extern void lru_cache_enable(void); |
---|
42 | 47 | |
---|
43 | 48 | struct cma cma_areas[MAX_CMA_AREAS]; |
---|
44 | 49 | unsigned cma_area_count; |
---|
.. | .. |
---|
48 | 53 | { |
---|
49 | 54 | return PFN_PHYS(cma->base_pfn); |
---|
50 | 55 | } |
---|
51 | | -EXPORT_SYMBOL(cma_get_base); |
---|
52 | 56 | |
---|
53 | 57 | unsigned long cma_get_size(const struct cma *cma) |
---|
54 | 58 | { |
---|
55 | 59 | return cma->count << PAGE_SHIFT; |
---|
56 | 60 | } |
---|
57 | | -EXPORT_SYMBOL(cma_get_size); |
---|
58 | 61 | |
---|
59 | 62 | const char *cma_get_name(const struct cma *cma) |
---|
60 | 63 | { |
---|
61 | | - return cma->name ? cma->name : "(undefined)"; |
---|
| 64 | + return cma->name; |
---|
62 | 65 | } |
---|
63 | 66 | EXPORT_SYMBOL_GPL(cma_get_name); |
---|
64 | 67 | |
---|
.. | .. |
---|
100 | 103 | mutex_unlock(&cma->lock); |
---|
101 | 104 | } |
---|
102 | 105 | |
---|
103 | | -static int __init cma_activate_area(struct cma *cma) |
---|
| 106 | +static void __init cma_activate_area(struct cma *cma) |
---|
104 | 107 | { |
---|
105 | | - int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long); |
---|
106 | | - unsigned long base_pfn = cma->base_pfn, pfn = base_pfn; |
---|
107 | | - unsigned i = cma->count >> pageblock_order; |
---|
| 108 | + unsigned long base_pfn = cma->base_pfn, pfn; |
---|
108 | 109 | struct zone *zone; |
---|
109 | 110 | |
---|
110 | | - cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
---|
| 111 | + cma->bitmap = bitmap_zalloc(cma_bitmap_maxno(cma), GFP_KERNEL); |
---|
| 112 | + if (!cma->bitmap) |
---|
| 113 | + goto out_error; |
---|
111 | 114 | |
---|
112 | | - if (!cma->bitmap) { |
---|
113 | | - cma->count = 0; |
---|
114 | | - return -ENOMEM; |
---|
| 115 | + if (IS_ENABLED(CONFIG_CMA_INACTIVE)) |
---|
| 116 | + goto out; |
---|
| 117 | + /* |
---|
| 118 | + * alloc_contig_range() requires the pfn range specified to be in the |
---|
| 119 | + * same zone. Simplify by forcing the entire CMA resv range to be in the |
---|
| 120 | + * same zone. |
---|
| 121 | + */ |
---|
| 122 | + WARN_ON_ONCE(!pfn_valid(base_pfn)); |
---|
| 123 | + zone = page_zone(pfn_to_page(base_pfn)); |
---|
| 124 | + for (pfn = base_pfn + 1; pfn < base_pfn + cma->count; pfn++) { |
---|
| 125 | + WARN_ON_ONCE(!pfn_valid(pfn)); |
---|
| 126 | + if (page_zone(pfn_to_page(pfn)) != zone) |
---|
| 127 | + goto not_in_zone; |
---|
115 | 128 | } |
---|
116 | 129 | |
---|
117 | | - if (cma->inactive) |
---|
118 | | - goto done; |
---|
| 130 | + for (pfn = base_pfn; pfn < base_pfn + cma->count; |
---|
| 131 | + pfn += pageblock_nr_pages) |
---|
| 132 | + init_cma_reserved_pageblock(pfn_to_page(pfn)); |
---|
119 | 133 | |
---|
120 | | - WARN_ON_ONCE(!pfn_valid(pfn)); |
---|
121 | | - zone = page_zone(pfn_to_page(pfn)); |
---|
122 | | - |
---|
123 | | - do { |
---|
124 | | - unsigned j; |
---|
125 | | - |
---|
126 | | - base_pfn = pfn; |
---|
127 | | - for (j = pageblock_nr_pages; j; --j, pfn++) { |
---|
128 | | - WARN_ON_ONCE(!pfn_valid(pfn)); |
---|
129 | | - /* |
---|
130 | | - * alloc_contig_range requires the pfn range |
---|
131 | | - * specified to be in the same zone. Make this |
---|
132 | | - * simple by forcing the entire CMA resv range |
---|
133 | | - * to be in the same zone. |
---|
134 | | - */ |
---|
135 | | - if (page_zone(pfn_to_page(pfn)) != zone) |
---|
136 | | - goto not_in_zone; |
---|
137 | | - } |
---|
138 | | - init_cma_reserved_pageblock(pfn_to_page(base_pfn)); |
---|
139 | | - } while (--i); |
---|
140 | | - |
---|
141 | | -done: |
---|
| 134 | +out: |
---|
142 | 135 | mutex_init(&cma->lock); |
---|
143 | 136 | |
---|
144 | 137 | #ifdef CONFIG_CMA_DEBUGFS |
---|
.. | .. |
---|
146 | 139 | spin_lock_init(&cma->mem_head_lock); |
---|
147 | 140 | #endif |
---|
148 | 141 | |
---|
149 | | - return 0; |
---|
| 142 | + return; |
---|
150 | 143 | |
---|
151 | 144 | not_in_zone: |
---|
152 | | - pr_err("CMA area %s could not be activated\n", cma->name); |
---|
153 | | - kfree(cma->bitmap); |
---|
| 145 | + bitmap_free(cma->bitmap); |
---|
| 146 | +out_error: |
---|
| 147 | + /* Expose all pages to the buddy, they are useless for CMA. */ |
---|
| 148 | + for (pfn = base_pfn; pfn < base_pfn + cma->count; pfn++) |
---|
| 149 | + free_reserved_page(pfn_to_page(pfn)); |
---|
| 150 | + totalcma_pages -= cma->count; |
---|
154 | 151 | cma->count = 0; |
---|
155 | | - return -EINVAL; |
---|
| 152 | + pr_err("CMA area %s could not be activated\n", cma->name); |
---|
| 153 | + return; |
---|
156 | 154 | } |
---|
157 | 155 | |
---|
158 | 156 | static int __init cma_init_reserved_areas(void) |
---|
159 | 157 | { |
---|
160 | 158 | int i; |
---|
161 | 159 | |
---|
162 | | - for (i = 0; i < cma_area_count; i++) { |
---|
163 | | - int ret = cma_activate_area(&cma_areas[i]); |
---|
164 | | - |
---|
165 | | - if (ret) |
---|
166 | | - return ret; |
---|
167 | | - } |
---|
| 160 | + for (i = 0; i < cma_area_count; i++) |
---|
| 161 | + cma_activate_area(&cma_areas[i]); |
---|
168 | 162 | |
---|
169 | 163 | return 0; |
---|
170 | 164 | } |
---|
.. | .. |
---|
188 | 182 | struct cma **res_cma) |
---|
189 | 183 | { |
---|
190 | 184 | struct cma *cma; |
---|
| 185 | +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) |
---|
191 | 186 | phys_addr_t alignment; |
---|
| 187 | +#endif |
---|
192 | 188 | |
---|
193 | 189 | /* Sanity checks */ |
---|
194 | 190 | if (cma_area_count == ARRAY_SIZE(cma_areas)) { |
---|
.. | .. |
---|
199 | 195 | if (!size || !memblock_is_region_reserved(base, size)) |
---|
200 | 196 | return -EINVAL; |
---|
201 | 197 | |
---|
| 198 | +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) |
---|
202 | 199 | /* ensure minimal alignment required by mm core */ |
---|
203 | 200 | alignment = PAGE_SIZE << |
---|
204 | 201 | max_t(unsigned long, MAX_ORDER - 1, pageblock_order); |
---|
.. | .. |
---|
209 | 206 | |
---|
210 | 207 | if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size) |
---|
211 | 208 | return -EINVAL; |
---|
| 209 | +#endif |
---|
212 | 210 | |
---|
213 | 211 | /* |
---|
214 | 212 | * Each reserved area must be initialised later, when more kernel |
---|
215 | 213 | * subsystems (like slab allocator) are available. |
---|
216 | 214 | */ |
---|
217 | 215 | cma = &cma_areas[cma_area_count]; |
---|
218 | | - if (name) { |
---|
219 | | - cma->name = name; |
---|
220 | | - } else { |
---|
221 | | - cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count); |
---|
222 | | - if (!cma->name) |
---|
223 | | - return -ENOMEM; |
---|
224 | | - } |
---|
| 216 | + |
---|
| 217 | + if (name) |
---|
| 218 | + snprintf(cma->name, CMA_MAX_NAME, name); |
---|
| 219 | + else |
---|
| 220 | + snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count); |
---|
| 221 | + |
---|
225 | 222 | cma->base_pfn = PFN_DOWN(base); |
---|
226 | 223 | cma->count = size >> PAGE_SHIFT; |
---|
227 | 224 | cma->order_per_bit = order_per_bit; |
---|
.. | .. |
---|
233 | 230 | } |
---|
234 | 231 | |
---|
235 | 232 | /** |
---|
236 | | - * cma_declare_contiguous() - reserve custom contiguous area |
---|
| 233 | + * cma_declare_contiguous_nid() - reserve custom contiguous area |
---|
237 | 234 | * @base: Base address of the reserved area optional, use 0 for any |
---|
238 | 235 | * @size: Size of the reserved area (in bytes), |
---|
239 | 236 | * @limit: End address of the reserved memory (optional, 0 for any). |
---|
.. | .. |
---|
242 | 239 | * @fixed: hint about where to place the reserved area |
---|
243 | 240 | * @name: The name of the area. See function cma_init_reserved_mem() |
---|
244 | 241 | * @res_cma: Pointer to store the created cma region. |
---|
| 242 | + * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
245 | 243 | * |
---|
246 | 244 | * This function reserves memory from early allocator. It should be |
---|
247 | 245 | * called by arch specific code once the early allocator (memblock or bootmem) |
---|
.. | .. |
---|
251 | 249 | * If @fixed is true, reserve contiguous area at exactly @base. If false, |
---|
252 | 250 | * reserve in range from @base to @limit. |
---|
253 | 251 | */ |
---|
254 | | -int __init cma_declare_contiguous(phys_addr_t base, |
---|
| 252 | +int __init cma_declare_contiguous_nid(phys_addr_t base, |
---|
255 | 253 | phys_addr_t size, phys_addr_t limit, |
---|
256 | 254 | phys_addr_t alignment, unsigned int order_per_bit, |
---|
257 | | - bool fixed, const char *name, struct cma **res_cma) |
---|
| 255 | + bool fixed, const char *name, struct cma **res_cma, |
---|
| 256 | + int nid) |
---|
258 | 257 | { |
---|
259 | 258 | phys_addr_t memblock_end = memblock_end_of_DRAM(); |
---|
260 | 259 | phys_addr_t highmem_start; |
---|
.. | .. |
---|
281 | 280 | if (alignment && !is_power_of_2(alignment)) |
---|
282 | 281 | return -EINVAL; |
---|
283 | 282 | |
---|
| 283 | +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) |
---|
284 | 284 | /* |
---|
285 | 285 | * Sanitise input arguments. |
---|
286 | 286 | * Pages both ends in CMA area could be merged into adjacent unmovable |
---|
.. | .. |
---|
295 | 295 | &base, &alignment); |
---|
296 | 296 | goto err; |
---|
297 | 297 | } |
---|
| 298 | +#endif |
---|
298 | 299 | base = ALIGN(base, alignment); |
---|
299 | 300 | size = ALIGN(size, alignment); |
---|
300 | 301 | limit &= ~(alignment - 1); |
---|
.. | .. |
---|
349 | 350 | * memory in case of failure. |
---|
350 | 351 | */ |
---|
351 | 352 | if (base < highmem_start && limit > highmem_start) { |
---|
352 | | - addr = memblock_alloc_range(size, alignment, |
---|
353 | | - highmem_start, limit, |
---|
354 | | - MEMBLOCK_NONE); |
---|
| 353 | + addr = memblock_alloc_range_nid(size, alignment, |
---|
| 354 | + highmem_start, limit, nid, true); |
---|
355 | 355 | limit = highmem_start; |
---|
356 | 356 | } |
---|
357 | 357 | |
---|
| 358 | + /* |
---|
| 359 | + * If there is enough memory, try a bottom-up allocation first. |
---|
| 360 | + * It will place the new cma area close to the start of the node |
---|
| 361 | + * and guarantee that the compaction is moving pages out of the |
---|
| 362 | + * cma area and not into it. |
---|
| 363 | + * Avoid using first 4GB to not interfere with constrained zones |
---|
| 364 | + * like DMA/DMA32. |
---|
| 365 | + */ |
---|
| 366 | +#ifdef CONFIG_PHYS_ADDR_T_64BIT |
---|
| 367 | + if (!memblock_bottom_up() && memblock_end >= SZ_4G + size) { |
---|
| 368 | + memblock_set_bottom_up(true); |
---|
| 369 | + addr = memblock_alloc_range_nid(size, alignment, SZ_4G, |
---|
| 370 | + limit, nid, true); |
---|
| 371 | + memblock_set_bottom_up(false); |
---|
| 372 | + } |
---|
| 373 | +#endif |
---|
| 374 | + |
---|
358 | 375 | if (!addr) { |
---|
359 | | - addr = memblock_alloc_range(size, alignment, base, |
---|
360 | | - limit, |
---|
361 | | - MEMBLOCK_NONE); |
---|
| 376 | + addr = memblock_alloc_range_nid(size, alignment, base, |
---|
| 377 | + limit, nid, true); |
---|
362 | 378 | if (!addr) { |
---|
363 | 379 | ret = -ENOMEM; |
---|
364 | 380 | goto err; |
---|
.. | .. |
---|
377 | 393 | if (ret) |
---|
378 | 394 | goto free_mem; |
---|
379 | 395 | |
---|
| 396 | +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) |
---|
380 | 397 | pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M, |
---|
381 | 398 | &base); |
---|
| 399 | +#else |
---|
| 400 | + pr_info("Reserved %ld KiB at %pa\n", (unsigned long)size / SZ_1K, |
---|
| 401 | + &base); |
---|
| 402 | +#endif |
---|
382 | 403 | return 0; |
---|
383 | 404 | |
---|
384 | 405 | free_mem: |
---|
385 | 406 | memblock_free(base, size); |
---|
386 | 407 | err: |
---|
| 408 | +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) |
---|
387 | 409 | pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); |
---|
| 410 | +#else |
---|
| 411 | + pr_err("Failed to reserve %ld KiB\n", (unsigned long)size / SZ_1K); |
---|
| 412 | +#endif |
---|
388 | 413 | return ret; |
---|
389 | 414 | } |
---|
390 | 415 | |
---|
.. | .. |
---|
422 | 447 | * @cma: Contiguous memory region for which the allocation is performed. |
---|
423 | 448 | * @count: Requested number of pages. |
---|
424 | 449 | * @align: Requested alignment of pages (in PAGE_SIZE order). |
---|
425 | | - * @no_warn: Avoid printing message about failed allocation |
---|
| 450 | + * @gfp_mask: GFP mask to use during the cma allocation. |
---|
426 | 451 | * |
---|
427 | 452 | * This function allocates part of contiguous memory on specific |
---|
428 | 453 | * contiguous memory area. |
---|
429 | 454 | */ |
---|
430 | 455 | struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, |
---|
431 | | - bool no_warn) |
---|
| 456 | + gfp_t gfp_mask) |
---|
432 | 457 | { |
---|
433 | 458 | unsigned long mask, offset; |
---|
434 | 459 | unsigned long pfn = -1; |
---|
.. | .. |
---|
437 | 462 | size_t i; |
---|
438 | 463 | struct page *page = NULL; |
---|
439 | 464 | int ret = -ENOMEM; |
---|
| 465 | + int num_attempts = 0; |
---|
| 466 | + int max_retries = 5; |
---|
| 467 | + s64 ts; |
---|
| 468 | + struct cma_alloc_info cma_info = {0}; |
---|
440 | 469 | |
---|
441 | | - if (!cma || !cma->count) |
---|
442 | | - return NULL; |
---|
| 470 | + trace_android_vh_cma_alloc_start(&ts); |
---|
443 | 471 | |
---|
444 | | - pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma, |
---|
445 | | - count, align); |
---|
| 472 | + if (!cma || !cma->count || !cma->bitmap) |
---|
| 473 | + goto out; |
---|
| 474 | + |
---|
| 475 | + pr_debug("%s(cma %p, count %zu, align %d gfp_mask 0x%x)\n", __func__, |
---|
| 476 | + (void *)cma, count, align, gfp_mask); |
---|
446 | 477 | |
---|
447 | 478 | if (!count) |
---|
448 | | - return NULL; |
---|
| 479 | + goto out; |
---|
| 480 | + |
---|
| 481 | + trace_cma_alloc_start(cma->name, count, align); |
---|
449 | 482 | |
---|
450 | 483 | mask = cma_bitmap_aligned_mask(cma, align); |
---|
451 | 484 | offset = cma_bitmap_aligned_offset(cma, align); |
---|
.. | .. |
---|
453 | 486 | bitmap_count = cma_bitmap_pages_to_bits(cma, count); |
---|
454 | 487 | |
---|
455 | 488 | if (bitmap_count > bitmap_maxno) |
---|
456 | | - return NULL; |
---|
| 489 | + goto out; |
---|
457 | 490 | |
---|
| 491 | + lru_cache_disable(); |
---|
458 | 492 | for (;;) { |
---|
| 493 | + struct acr_info info = {0}; |
---|
| 494 | + |
---|
459 | 495 | mutex_lock(&cma->lock); |
---|
460 | 496 | bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap, |
---|
461 | 497 | bitmap_maxno, start, bitmap_count, mask, |
---|
462 | 498 | offset); |
---|
463 | 499 | if (bitmap_no >= bitmap_maxno) { |
---|
464 | | - mutex_unlock(&cma->lock); |
---|
465 | | - break; |
---|
| 500 | + if ((num_attempts < max_retries) && (ret == -EBUSY)) { |
---|
| 501 | + mutex_unlock(&cma->lock); |
---|
| 502 | + |
---|
| 503 | + if (fatal_signal_pending(current) || |
---|
| 504 | + (gfp_mask & __GFP_NORETRY)) |
---|
| 505 | + break; |
---|
| 506 | + |
---|
| 507 | + /* |
---|
| 508 | + * Page may be momentarily pinned by some other |
---|
| 509 | + * process which has been scheduled out, e.g. |
---|
| 510 | + * in exit path, during unmap call, or process |
---|
| 511 | + * fork and so cannot be freed there. Sleep |
---|
| 512 | + * for 100ms and retry the allocation. |
---|
| 513 | + */ |
---|
| 514 | + start = 0; |
---|
| 515 | + ret = -ENOMEM; |
---|
| 516 | + schedule_timeout_killable(msecs_to_jiffies(100)); |
---|
| 517 | + num_attempts++; |
---|
| 518 | + continue; |
---|
| 519 | + } else { |
---|
| 520 | + mutex_unlock(&cma->lock); |
---|
| 521 | + break; |
---|
| 522 | + } |
---|
466 | 523 | } |
---|
467 | 524 | bitmap_set(cma->bitmap, bitmap_no, bitmap_count); |
---|
468 | 525 | /* |
---|
.. | .. |
---|
473 | 530 | mutex_unlock(&cma->lock); |
---|
474 | 531 | |
---|
475 | 532 | pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); |
---|
476 | | - if (cma->inactive) { |
---|
477 | | - ret = 0; |
---|
| 533 | + if (IS_ENABLED(CONFIG_CMA_INACTIVE)) { |
---|
478 | 534 | page = pfn_to_page(pfn); |
---|
479 | | - break; |
---|
| 535 | + lru_cache_enable(); |
---|
| 536 | + goto out; |
---|
480 | 537 | } |
---|
481 | 538 | mutex_lock(&cma_mutex); |
---|
482 | | - ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, |
---|
483 | | - GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); |
---|
| 539 | + ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp_mask, &info); |
---|
484 | 540 | mutex_unlock(&cma_mutex); |
---|
| 541 | + cma_info.nr_migrated += info.nr_migrated; |
---|
| 542 | + cma_info.nr_reclaimed += info.nr_reclaimed; |
---|
| 543 | + cma_info.nr_mapped += info.nr_mapped; |
---|
| 544 | + if (info.err) { |
---|
| 545 | + if (info.err & ACR_ERR_ISOLATE) |
---|
| 546 | + cma_info.nr_isolate_fail++; |
---|
| 547 | + if (info.err & ACR_ERR_MIGRATE) |
---|
| 548 | + cma_info.nr_migrate_fail++; |
---|
| 549 | + if (info.err & ACR_ERR_TEST) |
---|
| 550 | + cma_info.nr_test_fail++; |
---|
| 551 | + } |
---|
485 | 552 | if (ret == 0) { |
---|
486 | 553 | page = pfn_to_page(pfn); |
---|
487 | 554 | break; |
---|
.. | .. |
---|
493 | 560 | |
---|
494 | 561 | pr_debug("%s(): memory range at %p is busy, retrying\n", |
---|
495 | 562 | __func__, pfn_to_page(pfn)); |
---|
496 | | - /* try again with a bit different memory target */ |
---|
497 | | - start = bitmap_no + mask + 1; |
---|
| 563 | + |
---|
| 564 | + trace_cma_alloc_busy_retry(cma->name, pfn, pfn_to_page(pfn), |
---|
| 565 | + count, align); |
---|
| 566 | + |
---|
| 567 | + if (info.failed_pfn && gfp_mask & __GFP_NORETRY) { |
---|
| 568 | + /* try again from following failed page */ |
---|
| 569 | + start = (pfn_max_align_up(info.failed_pfn + 1) - |
---|
| 570 | + cma->base_pfn) >> cma->order_per_bit; |
---|
| 571 | + |
---|
| 572 | + } else { |
---|
| 573 | + /* try again with a bit different memory target */ |
---|
| 574 | + start = bitmap_no + mask + 1; |
---|
| 575 | + } |
---|
498 | 576 | } |
---|
499 | 577 | |
---|
500 | | - trace_cma_alloc(pfn, page, count, align); |
---|
| 578 | + lru_cache_enable(); |
---|
| 579 | + trace_cma_alloc_finish(cma->name, pfn, page, count, align); |
---|
| 580 | + trace_cma_alloc_info(cma->name, page, count, align, &cma_info); |
---|
501 | 581 | |
---|
502 | 582 | /* |
---|
503 | 583 | * CMA can allocate multiple page blocks, which results in different |
---|
.. | .. |
---|
509 | 589 | page_kasan_tag_reset(page + i); |
---|
510 | 590 | } |
---|
511 | 591 | |
---|
512 | | - if (ret && !no_warn) { |
---|
513 | | - pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n", |
---|
514 | | - __func__, count, ret); |
---|
| 592 | + if (ret && !(gfp_mask & __GFP_NOWARN)) { |
---|
| 593 | + pr_err("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n", |
---|
| 594 | + __func__, cma->name, count, ret); |
---|
515 | 595 | cma_debug_show_areas(cma); |
---|
516 | 596 | } |
---|
517 | 597 | |
---|
518 | 598 | pr_debug("%s(): returned %p\n", __func__, page); |
---|
| 599 | +out: |
---|
| 600 | + trace_android_vh_cma_alloc_finish(cma, page, count, align, gfp_mask, ts); |
---|
| 601 | + if (page) { |
---|
| 602 | + count_vm_event(CMA_ALLOC_SUCCESS); |
---|
| 603 | + cma_sysfs_account_success_pages(cma, count); |
---|
| 604 | + } else { |
---|
| 605 | + count_vm_event(CMA_ALLOC_FAIL); |
---|
| 606 | + if (cma) |
---|
| 607 | + cma_sysfs_account_fail_pages(cma, count); |
---|
| 608 | + } |
---|
| 609 | + |
---|
519 | 610 | return page; |
---|
520 | 611 | } |
---|
521 | 612 | EXPORT_SYMBOL_GPL(cma_alloc); |
---|
.. | .. |
---|
526 | 617 | * @pages: Allocated pages. |
---|
527 | 618 | * @count: Number of allocated pages. |
---|
528 | 619 | * |
---|
529 | | - * This function releases memory allocated by alloc_cma(). |
---|
| 620 | + * This function releases memory allocated by cma_alloc(). |
---|
530 | 621 | * It returns false when provided pages do not belong to contiguous area and |
---|
531 | 622 | * true otherwise. |
---|
532 | 623 | */ |
---|
.. | .. |
---|
537 | 628 | if (!cma || !pages) |
---|
538 | 629 | return false; |
---|
539 | 630 | |
---|
540 | | - pr_debug("%s(page %p)\n", __func__, (void *)pages); |
---|
| 631 | + pr_debug("%s(page %p, count %u)\n", __func__, (void *)pages, count); |
---|
541 | 632 | |
---|
542 | 633 | pfn = page_to_pfn(pages); |
---|
543 | 634 | |
---|
.. | .. |
---|
545 | 636 | return false; |
---|
546 | 637 | |
---|
547 | 638 | VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); |
---|
548 | | - |
---|
549 | | - if (!cma->inactive) |
---|
| 639 | + if (!IS_ENABLED(CONFIG_CMA_INACTIVE)) |
---|
550 | 640 | free_contig_range(pfn, count); |
---|
551 | 641 | cma_clear_bitmap(cma, pfn, count); |
---|
552 | | - trace_cma_release(pfn, pages, count); |
---|
| 642 | + trace_cma_release(cma->name, pfn, pages, count); |
---|
553 | 643 | |
---|
554 | 644 | return true; |
---|
555 | 645 | } |
---|
556 | 646 | EXPORT_SYMBOL_GPL(cma_release); |
---|
557 | 647 | |
---|
| 648 | +#ifdef CONFIG_NO_GKI |
---|
558 | 649 | unsigned long cma_used_pages(void) |
---|
559 | 650 | { |
---|
560 | 651 | struct cma *cma; |
---|
.. | .. |
---|
572 | 663 | return val; |
---|
573 | 664 | } |
---|
574 | 665 | EXPORT_SYMBOL_GPL(cma_used_pages); |
---|
| 666 | +#endif |
---|
575 | 667 | |
---|
576 | 668 | int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) |
---|
577 | 669 | { |
---|
.. | .. |
---|
587 | 679 | return 0; |
---|
588 | 680 | } |
---|
589 | 681 | EXPORT_SYMBOL_GPL(cma_for_each_area); |
---|
590 | | - |
---|
591 | | -void set_cma_area_inactive(struct cma *cma) |
---|
592 | | -{ |
---|
593 | | - cma->inactive = true; |
---|
594 | | -} |
---|
595 | | -EXPORT_SYMBOL_GPL(set_cma_area_inactive); |
---|