Back to home page

LXR

 
 

    


0001 /*
0002  * Contiguous Memory Allocator
0003  *
0004  * Copyright (c) 2010-2011 by Samsung Electronics.
0005  * Copyright IBM Corporation, 2013
0006  * Copyright LG Electronics Inc., 2014
0007  * Written by:
0008  *  Marek Szyprowski <m.szyprowski@samsung.com>
0009  *  Michal Nazarewicz <mina86@mina86.com>
0010  *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
0011  *  Joonsoo Kim <iamjoonsoo.kim@lge.com>
0012  *
0013  * This program is free software; you can redistribute it and/or
0014  * modify it under the terms of the GNU General Public License as
0015  * published by the Free Software Foundation; either version 2 of the
0016  * License or (at your optional) any later version of the license.
0017  */
0018 
0019 #define pr_fmt(fmt) "cma: " fmt
0020 
0021 #ifdef CONFIG_CMA_DEBUG
0022 #ifndef DEBUG
0023 #  define DEBUG
0024 #endif
0025 #endif
0026 #define CREATE_TRACE_POINTS
0027 
0028 #include <linux/memblock.h>
0029 #include <linux/err.h>
0030 #include <linux/mm.h>
0031 #include <linux/mutex.h>
0032 #include <linux/sizes.h>
0033 #include <linux/slab.h>
0034 #include <linux/log2.h>
0035 #include <linux/cma.h>
0036 #include <linux/highmem.h>
0037 #include <linux/io.h>
0038 #include <trace/events/cma.h>
0039 
0040 #include "cma.h"
0041 
0042 struct cma cma_areas[MAX_CMA_AREAS];
0043 unsigned cma_area_count;
0044 static DEFINE_MUTEX(cma_mutex);
0045 
0046 phys_addr_t cma_get_base(const struct cma *cma)
0047 {
0048     return PFN_PHYS(cma->base_pfn);
0049 }
0050 
0051 unsigned long cma_get_size(const struct cma *cma)
0052 {
0053     return cma->count << PAGE_SHIFT;
0054 }
0055 
0056 static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
0057                          int align_order)
0058 {
0059     if (align_order <= cma->order_per_bit)
0060         return 0;
0061     return (1UL << (align_order - cma->order_per_bit)) - 1;
0062 }
0063 
0064 /*
0065  * Find a PFN aligned to the specified order and return an offset represented in
0066  * order_per_bits.
0067  */
0068 static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
0069                            int align_order)
0070 {
0071     if (align_order <= cma->order_per_bit)
0072         return 0;
0073 
0074     return (ALIGN(cma->base_pfn, (1UL << align_order))
0075         - cma->base_pfn) >> cma->order_per_bit;
0076 }
0077 
0078 static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
0079                           unsigned long pages)
0080 {
0081     return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
0082 }
0083 
0084 static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
0085                  unsigned int count)
0086 {
0087     unsigned long bitmap_no, bitmap_count;
0088 
0089     bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit;
0090     bitmap_count = cma_bitmap_pages_to_bits(cma, count);
0091 
0092     mutex_lock(&cma->lock);
0093     bitmap_clear(cma->bitmap, bitmap_no, bitmap_count);
0094     mutex_unlock(&cma->lock);
0095 }
0096 
0097 static int __init cma_activate_area(struct cma *cma)
0098 {
0099     int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long);
0100     unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
0101     unsigned i = cma->count >> pageblock_order;
0102     struct zone *zone;
0103 
0104     cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
0105 
0106     if (!cma->bitmap)
0107         return -ENOMEM;
0108 
0109     WARN_ON_ONCE(!pfn_valid(pfn));
0110     zone = page_zone(pfn_to_page(pfn));
0111 
0112     do {
0113         unsigned j;
0114 
0115         base_pfn = pfn;
0116         for (j = pageblock_nr_pages; j; --j, pfn++) {
0117             WARN_ON_ONCE(!pfn_valid(pfn));
0118             /*
0119              * alloc_contig_range requires the pfn range
0120              * specified to be in the same zone. Make this
0121              * simple by forcing the entire CMA resv range
0122              * to be in the same zone.
0123              */
0124             if (page_zone(pfn_to_page(pfn)) != zone)
0125                 goto err;
0126         }
0127         init_cma_reserved_pageblock(pfn_to_page(base_pfn));
0128     } while (--i);
0129 
0130     mutex_init(&cma->lock);
0131 
0132 #ifdef CONFIG_CMA_DEBUGFS
0133     INIT_HLIST_HEAD(&cma->mem_head);
0134     spin_lock_init(&cma->mem_head_lock);
0135 #endif
0136 
0137     return 0;
0138 
0139 err:
0140     kfree(cma->bitmap);
0141     cma->count = 0;
0142     return -EINVAL;
0143 }
0144 
0145 static int __init cma_init_reserved_areas(void)
0146 {
0147     int i;
0148 
0149     for (i = 0; i < cma_area_count; i++) {
0150         int ret = cma_activate_area(&cma_areas[i]);
0151 
0152         if (ret)
0153             return ret;
0154     }
0155 
0156     return 0;
0157 }
0158 core_initcall(cma_init_reserved_areas);
0159 
0160 /**
0161  * cma_init_reserved_mem() - create custom contiguous area from reserved memory
0162  * @base: Base address of the reserved area
0163  * @size: Size of the reserved area (in bytes),
0164  * @order_per_bit: Order of pages represented by one bit on bitmap.
0165  * @res_cma: Pointer to store the created cma region.
0166  *
0167  * This function creates custom contiguous area from already reserved memory.
0168  */
0169 int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
0170                  unsigned int order_per_bit,
0171                  struct cma **res_cma)
0172 {
0173     struct cma *cma;
0174     phys_addr_t alignment;
0175 
0176     /* Sanity checks */
0177     if (cma_area_count == ARRAY_SIZE(cma_areas)) {
0178         pr_err("Not enough slots for CMA reserved regions!\n");
0179         return -ENOSPC;
0180     }
0181 
0182     if (!size || !memblock_is_region_reserved(base, size))
0183         return -EINVAL;
0184 
0185     /* ensure minimal alignment required by mm core */
0186     alignment = PAGE_SIZE <<
0187             max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
0188 
0189     /* alignment should be aligned with order_per_bit */
0190     if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
0191         return -EINVAL;
0192 
0193     if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
0194         return -EINVAL;
0195 
0196     /*
0197      * Each reserved area must be initialised later, when more kernel
0198      * subsystems (like slab allocator) are available.
0199      */
0200     cma = &cma_areas[cma_area_count];
0201     cma->base_pfn = PFN_DOWN(base);
0202     cma->count = size >> PAGE_SHIFT;
0203     cma->order_per_bit = order_per_bit;
0204     *res_cma = cma;
0205     cma_area_count++;
0206     totalcma_pages += (size / PAGE_SIZE);
0207 
0208     return 0;
0209 }
0210 
0211 /**
0212  * cma_declare_contiguous() - reserve custom contiguous area
0213  * @base: Base address of the reserved area optional, use 0 for any
0214  * @size: Size of the reserved area (in bytes),
0215  * @limit: End address of the reserved memory (optional, 0 for any).
0216  * @alignment: Alignment for the CMA area, should be power of 2 or zero
0217  * @order_per_bit: Order of pages represented by one bit on bitmap.
0218  * @fixed: hint about where to place the reserved area
0219  * @res_cma: Pointer to store the created cma region.
0220  *
0221  * This function reserves memory from early allocator. It should be
0222  * called by arch specific code once the early allocator (memblock or bootmem)
0223  * has been activated and all other subsystems have already allocated/reserved
0224  * memory. This function allows to create custom reserved areas.
0225  *
0226  * If @fixed is true, reserve contiguous area at exactly @base.  If false,
0227  * reserve in range from @base to @limit.
0228  */
0229 int __init cma_declare_contiguous(phys_addr_t base,
0230             phys_addr_t size, phys_addr_t limit,
0231             phys_addr_t alignment, unsigned int order_per_bit,
0232             bool fixed, struct cma **res_cma)
0233 {
0234     phys_addr_t memblock_end = memblock_end_of_DRAM();
0235     phys_addr_t highmem_start;
0236     int ret = 0;
0237 
0238 #ifdef CONFIG_X86
0239     /*
0240      * high_memory isn't direct mapped memory so retrieving its physical
0241      * address isn't appropriate.  But it would be useful to check the
0242      * physical address of the highmem boundary so it's justifiable to get
0243      * the physical address from it.  On x86 there is a validation check for
0244      * this case, so the following workaround is needed to avoid it.
0245      */
0246     highmem_start = __pa_nodebug(high_memory);
0247 #else
0248     highmem_start = __pa(high_memory);
0249 #endif
0250     pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
0251         __func__, &size, &base, &limit, &alignment);
0252 
0253     if (cma_area_count == ARRAY_SIZE(cma_areas)) {
0254         pr_err("Not enough slots for CMA reserved regions!\n");
0255         return -ENOSPC;
0256     }
0257 
0258     if (!size)
0259         return -EINVAL;
0260 
0261     if (alignment && !is_power_of_2(alignment))
0262         return -EINVAL;
0263 
0264     /*
0265      * Sanitise input arguments.
0266      * Pages both ends in CMA area could be merged into adjacent unmovable
0267      * migratetype page by page allocator's buddy algorithm. In the case,
0268      * you couldn't get a contiguous memory, which is not what we want.
0269      */
0270     alignment = max(alignment,  (phys_addr_t)PAGE_SIZE <<
0271               max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
0272     base = ALIGN(base, alignment);
0273     size = ALIGN(size, alignment);
0274     limit &= ~(alignment - 1);
0275 
0276     if (!base)
0277         fixed = false;
0278 
0279     /* size should be aligned with order_per_bit */
0280     if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
0281         return -EINVAL;
0282 
0283     /*
0284      * If allocating at a fixed base the request region must not cross the
0285      * low/high memory boundary.
0286      */
0287     if (fixed && base < highmem_start && base + size > highmem_start) {
0288         ret = -EINVAL;
0289         pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
0290             &base, &highmem_start);
0291         goto err;
0292     }
0293 
0294     /*
0295      * If the limit is unspecified or above the memblock end, its effective
0296      * value will be the memblock end. Set it explicitly to simplify further
0297      * checks.
0298      */
0299     if (limit == 0 || limit > memblock_end)
0300         limit = memblock_end;
0301 
0302     /* Reserve memory */
0303     if (fixed) {
0304         if (memblock_is_region_reserved(base, size) ||
0305             memblock_reserve(base, size) < 0) {
0306             ret = -EBUSY;
0307             goto err;
0308         }
0309     } else {
0310         phys_addr_t addr = 0;
0311 
0312         /*
0313          * All pages in the reserved area must come from the same zone.
0314          * If the requested region crosses the low/high memory boundary,
0315          * try allocating from high memory first and fall back to low
0316          * memory in case of failure.
0317          */
0318         if (base < highmem_start && limit > highmem_start) {
0319             addr = memblock_alloc_range(size, alignment,
0320                             highmem_start, limit,
0321                             MEMBLOCK_NONE);
0322             limit = highmem_start;
0323         }
0324 
0325         if (!addr) {
0326             addr = memblock_alloc_range(size, alignment, base,
0327                             limit,
0328                             MEMBLOCK_NONE);
0329             if (!addr) {
0330                 ret = -ENOMEM;
0331                 goto err;
0332             }
0333         }
0334 
0335         /*
0336          * kmemleak scans/reads tracked objects for pointers to other
0337          * objects but this address isn't mapped and accessible
0338          */
0339         kmemleak_ignore_phys(addr);
0340         base = addr;
0341     }
0342 
0343     ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
0344     if (ret)
0345         goto err;
0346 
0347     pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
0348         &base);
0349     return 0;
0350 
0351 err:
0352     pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
0353     return ret;
0354 }
0355 
0356 /**
0357  * cma_alloc() - allocate pages from contiguous area
0358  * @cma:   Contiguous memory region for which the allocation is performed.
0359  * @count: Requested number of pages.
0360  * @align: Requested alignment of pages (in PAGE_SIZE order).
0361  *
0362  * This function allocates part of contiguous memory on specific
0363  * contiguous memory area.
0364  */
0365 struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align)
0366 {
0367     unsigned long mask, offset;
0368     unsigned long pfn = -1;
0369     unsigned long start = 0;
0370     unsigned long bitmap_maxno, bitmap_no, bitmap_count;
0371     struct page *page = NULL;
0372     int ret;
0373 
0374     if (!cma || !cma->count)
0375         return NULL;
0376 
0377     pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
0378          count, align);
0379 
0380     if (!count)
0381         return NULL;
0382 
0383     mask = cma_bitmap_aligned_mask(cma, align);
0384     offset = cma_bitmap_aligned_offset(cma, align);
0385     bitmap_maxno = cma_bitmap_maxno(cma);
0386     bitmap_count = cma_bitmap_pages_to_bits(cma, count);
0387 
0388     if (bitmap_count > bitmap_maxno)
0389         return NULL;
0390 
0391     for (;;) {
0392         mutex_lock(&cma->lock);
0393         bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
0394                 bitmap_maxno, start, bitmap_count, mask,
0395                 offset);
0396         if (bitmap_no >= bitmap_maxno) {
0397             mutex_unlock(&cma->lock);
0398             break;
0399         }
0400         bitmap_set(cma->bitmap, bitmap_no, bitmap_count);
0401         /*
0402          * It's safe to drop the lock here. We've marked this region for
0403          * our exclusive use. If the migration fails we will take the
0404          * lock again and unmark it.
0405          */
0406         mutex_unlock(&cma->lock);
0407 
0408         pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
0409         mutex_lock(&cma_mutex);
0410         ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
0411         mutex_unlock(&cma_mutex);
0412         if (ret == 0) {
0413             page = pfn_to_page(pfn);
0414             break;
0415         }
0416 
0417         cma_clear_bitmap(cma, pfn, count);
0418         if (ret != -EBUSY)
0419             break;
0420 
0421         pr_debug("%s(): memory range at %p is busy, retrying\n",
0422              __func__, pfn_to_page(pfn));
0423         /* try again with a bit different memory target */
0424         start = bitmap_no + mask + 1;
0425     }
0426 
0427     trace_cma_alloc(pfn, page, count, align);
0428 
0429     pr_debug("%s(): returned %p\n", __func__, page);
0430     return page;
0431 }
0432 
0433 /**
0434  * cma_release() - release allocated pages
0435  * @cma:   Contiguous memory region for which the allocation is performed.
0436  * @pages: Allocated pages.
0437  * @count: Number of allocated pages.
0438  *
0439  * This function releases memory allocated by alloc_cma().
0440  * It returns false when provided pages do not belong to contiguous area and
0441  * true otherwise.
0442  */
0443 bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
0444 {
0445     unsigned long pfn;
0446 
0447     if (!cma || !pages)
0448         return false;
0449 
0450     pr_debug("%s(page %p)\n", __func__, (void *)pages);
0451 
0452     pfn = page_to_pfn(pages);
0453 
0454     if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
0455         return false;
0456 
0457     VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
0458 
0459     free_contig_range(pfn, count);
0460     cma_clear_bitmap(cma, pfn, count);
0461     trace_cma_release(pfn, pages, count);
0462 
0463     return true;
0464 }