From 102a0743326a03cd1a1202ceda21e175b7d3575c Mon Sep 17 00:00:00 2001 From: hc <hc@nodka.com> Date: Tue, 20 Feb 2024 01:20:52 +0000 Subject: [PATCH] add new system file --- kernel/mm/cma.c | 282 ++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 184 insertions(+), 98 deletions(-) diff --git a/kernel/mm/cma.c b/kernel/mm/cma.c index 0fa4d33..bf1cc2e 100644 --- a/kernel/mm/cma.c +++ b/kernel/mm/cma.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Contiguous Memory Allocator * @@ -9,11 +10,6 @@ * Michal Nazarewicz <mina86@mina86.com> * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> * Joonsoo Kim <iamjoonsoo.kim@lge.com> - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License or (at your optional) any later version of the license. */ #define pr_fmt(fmt) "cma: " fmt @@ -28,6 +24,7 @@ #include <linux/memblock.h> #include <linux/err.h> #include <linux/mm.h> +#include <linux/module.h> #include <linux/mutex.h> #include <linux/sizes.h> #include <linux/slab.h> @@ -36,9 +33,17 @@ #include <linux/highmem.h> #include <linux/io.h> #include <linux/kmemleak.h> +#include <linux/sched.h> +#include <linux/jiffies.h> #include <trace/events/cma.h> +#undef CREATE_TRACE_POINTS +#include <trace/hooks/mm.h> + #include "cma.h" + +extern void lru_cache_disable(void); +extern void lru_cache_enable(void); struct cma cma_areas[MAX_CMA_AREAS]; unsigned cma_area_count; @@ -48,17 +53,15 @@ { return PFN_PHYS(cma->base_pfn); } -EXPORT_SYMBOL(cma_get_base); unsigned long cma_get_size(const struct cma *cma) { return cma->count << PAGE_SHIFT; } -EXPORT_SYMBOL(cma_get_size); const char *cma_get_name(const struct cma *cma) { - return cma->name ? cma->name : "(undefined)"; + return cma->name; } EXPORT_SYMBOL_GPL(cma_get_name); @@ -100,45 +103,35 @@ mutex_unlock(&cma->lock); } -static int __init cma_activate_area(struct cma *cma) +static void __init cma_activate_area(struct cma *cma) { - int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long); - unsigned long base_pfn = cma->base_pfn, pfn = base_pfn; - unsigned i = cma->count >> pageblock_order; + unsigned long base_pfn = cma->base_pfn, pfn; struct zone *zone; - cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL); + cma->bitmap = bitmap_zalloc(cma_bitmap_maxno(cma), GFP_KERNEL); + if (!cma->bitmap) + goto out_error; - if (!cma->bitmap) { - cma->count = 0; - return -ENOMEM; + if (IS_ENABLED(CONFIG_CMA_INACTIVE)) + goto out; + /* + * alloc_contig_range() requires the pfn range specified to be in the + * same zone. Simplify by forcing the entire CMA resv range to be in the + * same zone. + */ + WARN_ON_ONCE(!pfn_valid(base_pfn)); + zone = page_zone(pfn_to_page(base_pfn)); + for (pfn = base_pfn + 1; pfn < base_pfn + cma->count; pfn++) { + WARN_ON_ONCE(!pfn_valid(pfn)); + if (page_zone(pfn_to_page(pfn)) != zone) + goto not_in_zone; } - if (cma->inactive) - goto done; + for (pfn = base_pfn; pfn < base_pfn + cma->count; + pfn += pageblock_nr_pages) + init_cma_reserved_pageblock(pfn_to_page(pfn)); - WARN_ON_ONCE(!pfn_valid(pfn)); - zone = page_zone(pfn_to_page(pfn)); - - do { - unsigned j; - - base_pfn = pfn; - for (j = pageblock_nr_pages; j; --j, pfn++) { - WARN_ON_ONCE(!pfn_valid(pfn)); - /* - * alloc_contig_range requires the pfn range - * specified to be in the same zone. Make this - * simple by forcing the entire CMA resv range - * to be in the same zone. - */ - if (page_zone(pfn_to_page(pfn)) != zone) - goto not_in_zone; - } - init_cma_reserved_pageblock(pfn_to_page(base_pfn)); - } while (--i); - -done: +out: mutex_init(&cma->lock); #ifdef CONFIG_CMA_DEBUGFS @@ -146,25 +139,26 @@ spin_lock_init(&cma->mem_head_lock); #endif - return 0; + return; not_in_zone: - pr_err("CMA area %s could not be activated\n", cma->name); - kfree(cma->bitmap); + bitmap_free(cma->bitmap); +out_error: + /* Expose all pages to the buddy, they are useless for CMA. */ + for (pfn = base_pfn; pfn < base_pfn + cma->count; pfn++) + free_reserved_page(pfn_to_page(pfn)); + totalcma_pages -= cma->count; cma->count = 0; - return -EINVAL; + pr_err("CMA area %s could not be activated\n", cma->name); + return; } static int __init cma_init_reserved_areas(void) { int i; - for (i = 0; i < cma_area_count; i++) { - int ret = cma_activate_area(&cma_areas[i]); - - if (ret) - return ret; - } + for (i = 0; i < cma_area_count; i++) + cma_activate_area(&cma_areas[i]); return 0; } @@ -188,7 +182,9 @@ struct cma **res_cma) { struct cma *cma; +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) phys_addr_t alignment; +#endif /* Sanity checks */ if (cma_area_count == ARRAY_SIZE(cma_areas)) { @@ -199,6 +195,7 @@ if (!size || !memblock_is_region_reserved(base, size)) return -EINVAL; +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) /* ensure minimal alignment required by mm core */ alignment = PAGE_SIZE << max_t(unsigned long, MAX_ORDER - 1, pageblock_order); @@ -209,19 +206,19 @@ if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size) return -EINVAL; +#endif /* * Each reserved area must be initialised later, when more kernel * subsystems (like slab allocator) are available. */ cma = &cma_areas[cma_area_count]; - if (name) { - cma->name = name; - } else { - cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count); - if (!cma->name) - return -ENOMEM; - } + + if (name) + snprintf(cma->name, CMA_MAX_NAME, name); + else + snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count); + cma->base_pfn = PFN_DOWN(base); cma->count = size >> PAGE_SHIFT; cma->order_per_bit = order_per_bit; @@ -233,7 +230,7 @@ } /** - * cma_declare_contiguous() - reserve custom contiguous area + * cma_declare_contiguous_nid() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), * @limit: End address of the reserved memory (optional, 0 for any). @@ -242,6 +239,7 @@ * @fixed: hint about where to place the reserved area * @name: The name of the area. See function cma_init_reserved_mem() * @res_cma: Pointer to store the created cma region. + * @nid: nid of the free area to find, %NUMA_NO_NODE for any node * * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem) @@ -251,10 +249,11 @@ * If @fixed is true, reserve contiguous area at exactly @base. If false, * reserve in range from @base to @limit. */ -int __init cma_declare_contiguous(phys_addr_t base, +int __init 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) + bool fixed, const char *name, struct cma **res_cma, + int nid) { phys_addr_t memblock_end = memblock_end_of_DRAM(); phys_addr_t highmem_start; @@ -281,6 +280,7 @@ if (alignment && !is_power_of_2(alignment)) return -EINVAL; +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) /* * Sanitise input arguments. * Pages both ends in CMA area could be merged into adjacent unmovable @@ -295,6 +295,7 @@ &base, &alignment); goto err; } +#endif base = ALIGN(base, alignment); size = ALIGN(size, alignment); limit &= ~(alignment - 1); @@ -349,16 +350,31 @@ * memory in case of failure. */ if (base < highmem_start && limit > highmem_start) { - addr = memblock_alloc_range(size, alignment, - highmem_start, limit, - MEMBLOCK_NONE); + addr = memblock_alloc_range_nid(size, alignment, + highmem_start, limit, nid, true); limit = highmem_start; } + /* + * If there is enough memory, try a bottom-up allocation first. + * It will place the new cma area close to the start of the node + * and guarantee that the compaction is moving pages out of the + * cma area and not into it. + * Avoid using first 4GB to not interfere with constrained zones + * like DMA/DMA32. + */ +#ifdef CONFIG_PHYS_ADDR_T_64BIT + if (!memblock_bottom_up() && memblock_end >= SZ_4G + size) { + memblock_set_bottom_up(true); + addr = memblock_alloc_range_nid(size, alignment, SZ_4G, + limit, nid, true); + memblock_set_bottom_up(false); + } +#endif + if (!addr) { - addr = memblock_alloc_range(size, alignment, base, - limit, - MEMBLOCK_NONE); + addr = memblock_alloc_range_nid(size, alignment, base, + limit, nid, true); if (!addr) { ret = -ENOMEM; goto err; @@ -377,14 +393,23 @@ if (ret) goto free_mem; +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M, &base); +#else + pr_info("Reserved %ld KiB at %pa\n", (unsigned long)size / SZ_1K, + &base); +#endif return 0; free_mem: memblock_free(base, size); err: +#if !IS_ENABLED(CONFIG_CMA_INACTIVE) pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); +#else + pr_err("Failed to reserve %ld KiB\n", (unsigned long)size / SZ_1K); +#endif return ret; } @@ -422,13 +447,13 @@ * @cma: Contiguous memory region for which the allocation is performed. * @count: Requested number of pages. * @align: Requested alignment of pages (in PAGE_SIZE order). - * @no_warn: Avoid printing message about failed allocation + * @gfp_mask: GFP mask to use during the cma allocation. * * This function allocates part of contiguous memory on specific * contiguous memory area. */ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, - bool no_warn) + gfp_t gfp_mask) { unsigned long mask, offset; unsigned long pfn = -1; @@ -437,15 +462,23 @@ size_t i; struct page *page = NULL; int ret = -ENOMEM; + int num_attempts = 0; + int max_retries = 5; + s64 ts; + struct cma_alloc_info cma_info = {0}; - if (!cma || !cma->count) - return NULL; + trace_android_vh_cma_alloc_start(&ts); - pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma, - count, align); + if (!cma || !cma->count || !cma->bitmap) + goto out; + + pr_debug("%s(cma %p, count %zu, align %d gfp_mask 0x%x)\n", __func__, + (void *)cma, count, align, gfp_mask); if (!count) - return NULL; + goto out; + + trace_cma_alloc_start(cma->name, count, align); mask = cma_bitmap_aligned_mask(cma, align); offset = cma_bitmap_aligned_offset(cma, align); @@ -453,16 +486,40 @@ bitmap_count = cma_bitmap_pages_to_bits(cma, count); if (bitmap_count > bitmap_maxno) - return NULL; + goto out; + lru_cache_disable(); for (;;) { + struct acr_info info = {0}; + mutex_lock(&cma->lock); bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap, bitmap_maxno, start, bitmap_count, mask, offset); if (bitmap_no >= bitmap_maxno) { - mutex_unlock(&cma->lock); - break; + if ((num_attempts < max_retries) && (ret == -EBUSY)) { + mutex_unlock(&cma->lock); + + if (fatal_signal_pending(current) || + (gfp_mask & __GFP_NORETRY)) + break; + + /* + * Page may be momentarily pinned by some other + * process which has been scheduled out, e.g. + * in exit path, during unmap call, or process + * fork and so cannot be freed there. Sleep + * for 100ms and retry the allocation. + */ + start = 0; + ret = -ENOMEM; + schedule_timeout_killable(msecs_to_jiffies(100)); + num_attempts++; + continue; + } else { + mutex_unlock(&cma->lock); + break; + } } bitmap_set(cma->bitmap, bitmap_no, bitmap_count); /* @@ -473,15 +530,25 @@ mutex_unlock(&cma->lock); pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); - if (cma->inactive) { - ret = 0; + if (IS_ENABLED(CONFIG_CMA_INACTIVE)) { page = pfn_to_page(pfn); - break; + lru_cache_enable(); + goto out; } mutex_lock(&cma_mutex); - ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, - GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); + ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp_mask, &info); mutex_unlock(&cma_mutex); + cma_info.nr_migrated += info.nr_migrated; + cma_info.nr_reclaimed += info.nr_reclaimed; + cma_info.nr_mapped += info.nr_mapped; + if (info.err) { + if (info.err & ACR_ERR_ISOLATE) + cma_info.nr_isolate_fail++; + if (info.err & ACR_ERR_MIGRATE) + cma_info.nr_migrate_fail++; + if (info.err & ACR_ERR_TEST) + cma_info.nr_test_fail++; + } if (ret == 0) { page = pfn_to_page(pfn); break; @@ -493,11 +560,24 @@ pr_debug("%s(): memory range at %p is busy, retrying\n", __func__, pfn_to_page(pfn)); - /* try again with a bit different memory target */ - start = bitmap_no + mask + 1; + + trace_cma_alloc_busy_retry(cma->name, pfn, pfn_to_page(pfn), + count, align); + + if (info.failed_pfn && gfp_mask & __GFP_NORETRY) { + /* try again from following failed page */ + start = (pfn_max_align_up(info.failed_pfn + 1) - + cma->base_pfn) >> cma->order_per_bit; + + } else { + /* try again with a bit different memory target */ + start = bitmap_no + mask + 1; + } } - trace_cma_alloc(pfn, page, count, align); + lru_cache_enable(); + trace_cma_alloc_finish(cma->name, pfn, page, count, align); + trace_cma_alloc_info(cma->name, page, count, align, &cma_info); /* * CMA can allocate multiple page blocks, which results in different @@ -509,13 +589,24 @@ page_kasan_tag_reset(page + i); } - if (ret && !no_warn) { - pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n", - __func__, count, ret); + if (ret && !(gfp_mask & __GFP_NOWARN)) { + pr_err("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n", + __func__, cma->name, count, ret); cma_debug_show_areas(cma); } pr_debug("%s(): returned %p\n", __func__, page); +out: + trace_android_vh_cma_alloc_finish(cma, page, count, align, gfp_mask, ts); + if (page) { + count_vm_event(CMA_ALLOC_SUCCESS); + cma_sysfs_account_success_pages(cma, count); + } else { + count_vm_event(CMA_ALLOC_FAIL); + if (cma) + cma_sysfs_account_fail_pages(cma, count); + } + return page; } EXPORT_SYMBOL_GPL(cma_alloc); @@ -526,7 +617,7 @@ * @pages: Allocated pages. * @count: Number of allocated pages. * - * This function releases memory allocated by alloc_cma(). + * This function releases memory allocated by cma_alloc(). * It returns false when provided pages do not belong to contiguous area and * true otherwise. */ @@ -537,7 +628,7 @@ if (!cma || !pages) return false; - pr_debug("%s(page %p)\n", __func__, (void *)pages); + pr_debug("%s(page %p, count %u)\n", __func__, (void *)pages, count); pfn = page_to_pfn(pages); @@ -545,16 +636,16 @@ return false; VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); - - if (!cma->inactive) + if (!IS_ENABLED(CONFIG_CMA_INACTIVE)) free_contig_range(pfn, count); cma_clear_bitmap(cma, pfn, count); - trace_cma_release(pfn, pages, count); + trace_cma_release(cma->name, pfn, pages, count); return true; } EXPORT_SYMBOL_GPL(cma_release); +#ifdef CONFIG_NO_GKI unsigned long cma_used_pages(void) { struct cma *cma; @@ -572,6 +663,7 @@ return val; } EXPORT_SYMBOL_GPL(cma_used_pages); +#endif int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) { @@ -587,9 +679,3 @@ return 0; } EXPORT_SYMBOL_GPL(cma_for_each_area); - -void set_cma_area_inactive(struct cma *cma) -{ - cma->inactive = true; -} -EXPORT_SYMBOL_GPL(set_cma_area_inactive); -- Gitblit v1.6.2