Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Contiguous Memory Allocator |
| 3 | * |
| 4 | * Copyright (c) 2010-2011 by Samsung Electronics. |
| 5 | * Copyright IBM Corporation, 2013 |
| 6 | * Copyright LG Electronics Inc., 2014 |
| 7 | * Written by: |
| 8 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 9 | * Michal Nazarewicz <mina86@mina86.com> |
| 10 | * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
| 11 | * 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 | */ |
| 18 | |
| 19 | #define pr_fmt(fmt) "cma: " fmt |
| 20 | |
| 21 | #ifdef CONFIG_CMA_DEBUG |
| 22 | #ifndef DEBUG |
| 23 | # define DEBUG |
| 24 | #endif |
| 25 | #endif |
Stefan Strogin | 99e8ea6 | 2015-04-15 16:14:50 -0700 | [diff] [blame] | 26 | #define CREATE_TRACE_POINTS |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 27 | |
| 28 | #include <linux/memblock.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/sizes.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/log2.h> |
| 35 | #include <linux/cma.h> |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 36 | #include <linux/highmem.h> |
Thierry Reding | 620951e | 2014-12-12 16:58:31 -0800 | [diff] [blame] | 37 | #include <linux/io.h> |
Randy Dunlap | 514c603 | 2018-04-05 16:25:34 -0700 | [diff] [blame] | 38 | #include <linux/kmemleak.h> |
Stefan Strogin | 99e8ea6 | 2015-04-15 16:14:50 -0700 | [diff] [blame] | 39 | #include <trace/events/cma.h> |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 40 | |
Sasha Levin | 28b24c1 | 2015-04-14 15:44:57 -0700 | [diff] [blame] | 41 | #include "cma.h" |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 42 | #include "internal.h" |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 43 | |
Sasha Levin | 28b24c1 | 2015-04-14 15:44:57 -0700 | [diff] [blame] | 44 | struct cma cma_areas[MAX_CMA_AREAS]; |
| 45 | unsigned cma_area_count; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 46 | static DEFINE_MUTEX(cma_mutex); |
| 47 | |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 48 | phys_addr_t cma_get_base(const struct cma *cma) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 49 | { |
| 50 | return PFN_PHYS(cma->base_pfn); |
| 51 | } |
| 52 | |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 53 | unsigned long cma_get_size(const struct cma *cma) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 54 | { |
| 55 | return cma->count << PAGE_SHIFT; |
| 56 | } |
| 57 | |
Laura Abbott | f318dd0 | 2017-04-18 11:27:03 -0700 | [diff] [blame] | 58 | const char *cma_get_name(const struct cma *cma) |
| 59 | { |
| 60 | return cma->name ? cma->name : "(undefined)"; |
| 61 | } |
| 62 | |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 63 | static unsigned long cma_bitmap_aligned_mask(const struct cma *cma, |
Doug Berger | e048cb3 | 2017-07-10 15:49:44 -0700 | [diff] [blame] | 64 | unsigned int align_order) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 65 | { |
Weijie Yang | 68faed6 | 2014-10-13 15:51:03 -0700 | [diff] [blame] | 66 | if (align_order <= cma->order_per_bit) |
| 67 | return 0; |
| 68 | return (1UL << (align_order - cma->order_per_bit)) - 1; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Danesh Petigara | 850fc43 | 2015-03-12 16:25:57 -0700 | [diff] [blame] | 71 | /* |
Doug Berger | e048cb3 | 2017-07-10 15:49:44 -0700 | [diff] [blame] | 72 | * Find the offset of the base PFN from the specified align_order. |
| 73 | * The value returned is represented in order_per_bits. |
Danesh Petigara | 850fc43 | 2015-03-12 16:25:57 -0700 | [diff] [blame] | 74 | */ |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 75 | static unsigned long cma_bitmap_aligned_offset(const struct cma *cma, |
Doug Berger | e048cb3 | 2017-07-10 15:49:44 -0700 | [diff] [blame] | 76 | unsigned int align_order) |
Gregory Fong | b5be83e | 2014-12-12 16:54:48 -0800 | [diff] [blame] | 77 | { |
Doug Berger | e048cb3 | 2017-07-10 15:49:44 -0700 | [diff] [blame] | 78 | return (cma->base_pfn & ((1UL << align_order) - 1)) |
| 79 | >> cma->order_per_bit; |
Gregory Fong | b5be83e | 2014-12-12 16:54:48 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 82 | static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma, |
| 83 | unsigned long pages) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 84 | { |
| 85 | return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit; |
| 86 | } |
| 87 | |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 88 | static void cma_clear_bitmap(struct cma *cma, unsigned long pfn, |
| 89 | unsigned int count) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 90 | { |
| 91 | unsigned long bitmap_no, bitmap_count; |
| 92 | |
| 93 | bitmap_no = (pfn - cma->base_pfn) >> cma->order_per_bit; |
| 94 | bitmap_count = cma_bitmap_pages_to_bits(cma, count); |
| 95 | |
| 96 | mutex_lock(&cma->lock); |
| 97 | bitmap_clear(cma->bitmap, bitmap_no, bitmap_count); |
| 98 | mutex_unlock(&cma->lock); |
| 99 | } |
| 100 | |
| 101 | static int __init cma_activate_area(struct cma *cma) |
| 102 | { |
| 103 | int bitmap_size = BITS_TO_LONGS(cma_bitmap_maxno(cma)) * sizeof(long); |
| 104 | unsigned long base_pfn = cma->base_pfn, pfn = base_pfn; |
| 105 | unsigned i = cma->count >> pageblock_order; |
| 106 | struct zone *zone; |
| 107 | |
| 108 | cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 109 | |
| 110 | if (!cma->bitmap) |
| 111 | return -ENOMEM; |
| 112 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 113 | do { |
| 114 | unsigned j; |
| 115 | |
| 116 | base_pfn = pfn; |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 117 | if (!pfn_valid(base_pfn)) |
| 118 | goto err; |
| 119 | |
| 120 | zone = page_zone(pfn_to_page(base_pfn)); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 121 | for (j = pageblock_nr_pages; j; --j, pfn++) { |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 122 | if (!pfn_valid(pfn)) |
| 123 | goto err; |
| 124 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 125 | /* |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 126 | * In init_cma_reserved_pageblock(), present_pages |
| 127 | * is adjusted with assumption that all pages in |
| 128 | * the pageblock come from a single zone. |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 129 | */ |
| 130 | if (page_zone(pfn_to_page(pfn)) != zone) |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 131 | goto err; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 132 | } |
| 133 | init_cma_reserved_pageblock(pfn_to_page(base_pfn)); |
| 134 | } while (--i); |
| 135 | |
| 136 | mutex_init(&cma->lock); |
Sasha Levin | 26b02a1 | 2015-04-14 15:44:59 -0700 | [diff] [blame] | 137 | |
| 138 | #ifdef CONFIG_CMA_DEBUGFS |
| 139 | INIT_HLIST_HEAD(&cma->mem_head); |
| 140 | spin_lock_init(&cma->mem_head_lock); |
| 141 | #endif |
| 142 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 145 | err: |
Anshuman Khandual | e35ef63 | 2017-07-10 15:48:12 -0700 | [diff] [blame] | 146 | pr_err("CMA area %s could not be activated\n", cma->name); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 147 | kfree(cma->bitmap); |
Laurent Pinchart | f022d8c | 2014-10-24 13:18:39 +0300 | [diff] [blame] | 148 | cma->count = 0; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | static int __init cma_init_reserved_areas(void) |
| 153 | { |
| 154 | int i; |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 155 | struct zone *zone; |
| 156 | pg_data_t *pgdat; |
| 157 | |
| 158 | if (!cma_area_count) |
| 159 | return 0; |
| 160 | |
| 161 | for_each_online_pgdat(pgdat) { |
| 162 | unsigned long start_pfn = UINT_MAX, end_pfn = 0; |
| 163 | |
| 164 | zone = &pgdat->node_zones[ZONE_MOVABLE]; |
| 165 | |
| 166 | /* |
| 167 | * In this case, we cannot adjust the zone range |
| 168 | * since it is now maximum node span and we don't |
| 169 | * know original zone range. |
| 170 | */ |
| 171 | if (populated_zone(zone)) |
| 172 | continue; |
| 173 | |
| 174 | for (i = 0; i < cma_area_count; i++) { |
| 175 | if (pfn_to_nid(cma_areas[i].base_pfn) != |
| 176 | pgdat->node_id) |
| 177 | continue; |
| 178 | |
| 179 | start_pfn = min(start_pfn, cma_areas[i].base_pfn); |
| 180 | end_pfn = max(end_pfn, cma_areas[i].base_pfn + |
| 181 | cma_areas[i].count); |
| 182 | } |
| 183 | |
| 184 | if (!end_pfn) |
| 185 | continue; |
| 186 | |
| 187 | zone->zone_start_pfn = start_pfn; |
| 188 | zone->spanned_pages = end_pfn - start_pfn; |
| 189 | } |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 190 | |
| 191 | for (i = 0; i < cma_area_count; i++) { |
| 192 | int ret = cma_activate_area(&cma_areas[i]); |
| 193 | |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | } |
| 197 | |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 198 | /* |
| 199 | * Reserved pages for ZONE_MOVABLE are now activated and |
| 200 | * this would change ZONE_MOVABLE's managed page counter and |
| 201 | * the other zones' present counter. We need to re-calculate |
| 202 | * various zone information that depends on this initialization. |
| 203 | */ |
| 204 | build_all_zonelists(NULL); |
| 205 | for_each_populated_zone(zone) { |
| 206 | if (zone_idx(zone) == ZONE_MOVABLE) { |
| 207 | zone_pcp_reset(zone); |
| 208 | setup_zone_pageset(zone); |
| 209 | } else |
| 210 | zone_pcp_update(zone); |
| 211 | |
| 212 | set_zone_contiguous(zone); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * We need to re-init per zone wmark by calling |
| 217 | * init_per_zone_wmark_min() but doesn't call here because it is |
| 218 | * registered on core_initcall and it will be called later than us. |
| 219 | */ |
| 220 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
Joonsoo Kim | bad8c6c | 2018-04-10 16:30:15 -0700 | [diff] [blame] | 223 | pure_initcall(cma_init_reserved_areas); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 226 | * cma_init_reserved_mem() - create custom contiguous area from reserved memory |
| 227 | * @base: Base address of the reserved area |
| 228 | * @size: Size of the reserved area (in bytes), |
| 229 | * @order_per_bit: Order of pages represented by one bit on bitmap. |
Mike Rapoport | e8b098f | 2018-04-05 16:24:57 -0700 | [diff] [blame] | 230 | * @name: The name of the area. If this parameter is NULL, the name of |
| 231 | * the area will be set to "cmaN", where N is a running counter of |
| 232 | * used areas. |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 233 | * @res_cma: Pointer to store the created cma region. |
| 234 | * |
| 235 | * This function creates custom contiguous area from already reserved memory. |
| 236 | */ |
| 237 | int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 238 | unsigned int order_per_bit, |
Laura Abbott | f318dd0 | 2017-04-18 11:27:03 -0700 | [diff] [blame] | 239 | const char *name, |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 240 | struct cma **res_cma) |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 241 | { |
| 242 | struct cma *cma; |
| 243 | phys_addr_t alignment; |
| 244 | |
| 245 | /* Sanity checks */ |
| 246 | if (cma_area_count == ARRAY_SIZE(cma_areas)) { |
| 247 | pr_err("Not enough slots for CMA reserved regions!\n"); |
| 248 | return -ENOSPC; |
| 249 | } |
| 250 | |
| 251 | if (!size || !memblock_is_region_reserved(base, size)) |
| 252 | return -EINVAL; |
| 253 | |
Shailendra Verma | 0f96ae2 | 2015-06-24 16:58:03 -0700 | [diff] [blame] | 254 | /* ensure minimal alignment required by mm core */ |
Stephen Rothwell | badbda5 | 2016-05-27 14:27:41 -0700 | [diff] [blame] | 255 | alignment = PAGE_SIZE << |
| 256 | max_t(unsigned long, MAX_ORDER - 1, pageblock_order); |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 257 | |
| 258 | /* alignment should be aligned with order_per_bit */ |
| 259 | if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit)) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size) |
| 263 | return -EINVAL; |
| 264 | |
| 265 | /* |
| 266 | * Each reserved area must be initialised later, when more kernel |
| 267 | * subsystems (like slab allocator) are available. |
| 268 | */ |
| 269 | cma = &cma_areas[cma_area_count]; |
Laura Abbott | f318dd0 | 2017-04-18 11:27:03 -0700 | [diff] [blame] | 270 | if (name) { |
| 271 | cma->name = name; |
| 272 | } else { |
| 273 | cma->name = kasprintf(GFP_KERNEL, "cma%d\n", cma_area_count); |
| 274 | if (!cma->name) |
| 275 | return -ENOMEM; |
| 276 | } |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 277 | cma->base_pfn = PFN_DOWN(base); |
| 278 | cma->count = size >> PAGE_SHIFT; |
| 279 | cma->order_per_bit = order_per_bit; |
| 280 | *res_cma = cma; |
| 281 | cma_area_count++; |
George G. Davis | 94737a8 | 2015-02-11 15:26:27 -0800 | [diff] [blame] | 282 | totalcma_pages += (size / PAGE_SIZE); |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /** |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 288 | * cma_declare_contiguous() - reserve custom contiguous area |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 289 | * @base: Base address of the reserved area optional, use 0 for any |
Joonsoo Kim | c1f733aa | 2014-08-06 16:05:32 -0700 | [diff] [blame] | 290 | * @size: Size of the reserved area (in bytes), |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 291 | * @limit: End address of the reserved memory (optional, 0 for any). |
| 292 | * @alignment: Alignment for the CMA area, should be power of 2 or zero |
| 293 | * @order_per_bit: Order of pages represented by one bit on bitmap. |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 294 | * @fixed: hint about where to place the reserved area |
Mike Rapoport | e8b098f | 2018-04-05 16:24:57 -0700 | [diff] [blame] | 295 | * @name: The name of the area. See function cma_init_reserved_mem() |
Joonsoo Kim | c1f733aa | 2014-08-06 16:05:32 -0700 | [diff] [blame] | 296 | * @res_cma: Pointer to store the created cma region. |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 297 | * |
| 298 | * This function reserves memory from early allocator. It should be |
| 299 | * called by arch specific code once the early allocator (memblock or bootmem) |
| 300 | * has been activated and all other subsystems have already allocated/reserved |
| 301 | * memory. This function allows to create custom reserved areas. |
| 302 | * |
| 303 | * If @fixed is true, reserve contiguous area at exactly @base. If false, |
| 304 | * reserve in range from @base to @limit. |
| 305 | */ |
Joonsoo Kim | c1f733aa | 2014-08-06 16:05:32 -0700 | [diff] [blame] | 306 | int __init cma_declare_contiguous(phys_addr_t base, |
| 307 | phys_addr_t size, phys_addr_t limit, |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 308 | phys_addr_t alignment, unsigned int order_per_bit, |
Laura Abbott | f318dd0 | 2017-04-18 11:27:03 -0700 | [diff] [blame] | 309 | bool fixed, const char *name, struct cma **res_cma) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 310 | { |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 311 | phys_addr_t memblock_end = memblock_end_of_DRAM(); |
Joonsoo Kim | 6b101e2 | 2014-12-10 15:41:12 -0800 | [diff] [blame] | 312 | phys_addr_t highmem_start; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 313 | int ret = 0; |
| 314 | |
Joonsoo Kim | 6b101e2 | 2014-12-10 15:41:12 -0800 | [diff] [blame] | 315 | /* |
Laura Abbott | 2dece44 | 2017-01-10 13:35:41 -0800 | [diff] [blame] | 316 | * We can't use __pa(high_memory) directly, since high_memory |
| 317 | * isn't a valid direct map VA, and DEBUG_VIRTUAL will (validly) |
| 318 | * complain. Find the boundary by adding one to the last valid |
| 319 | * address. |
Joonsoo Kim | 6b101e2 | 2014-12-10 15:41:12 -0800 | [diff] [blame] | 320 | */ |
Laura Abbott | 2dece44 | 2017-01-10 13:35:41 -0800 | [diff] [blame] | 321 | highmem_start = __pa(high_memory - 1) + 1; |
Laurent Pinchart | 56fa4f6 | 2014-10-24 13:18:42 +0300 | [diff] [blame] | 322 | pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n", |
| 323 | __func__, &size, &base, &limit, &alignment); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 324 | |
| 325 | if (cma_area_count == ARRAY_SIZE(cma_areas)) { |
| 326 | pr_err("Not enough slots for CMA reserved regions!\n"); |
| 327 | return -ENOSPC; |
| 328 | } |
| 329 | |
| 330 | if (!size) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | if (alignment && !is_power_of_2(alignment)) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | /* |
| 337 | * Sanitise input arguments. |
| 338 | * Pages both ends in CMA area could be merged into adjacent unmovable |
| 339 | * migratetype page by page allocator's buddy algorithm. In the case, |
| 340 | * you couldn't get a contiguous memory, which is not what we want. |
| 341 | */ |
Stephen Rothwell | badbda5 | 2016-05-27 14:27:41 -0700 | [diff] [blame] | 342 | alignment = max(alignment, (phys_addr_t)PAGE_SIZE << |
| 343 | max_t(unsigned long, MAX_ORDER - 1, pageblock_order)); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 344 | base = ALIGN(base, alignment); |
| 345 | size = ALIGN(size, alignment); |
| 346 | limit &= ~(alignment - 1); |
| 347 | |
Laurent Pinchart | 800a85d | 2014-10-24 13:18:40 +0300 | [diff] [blame] | 348 | if (!base) |
| 349 | fixed = false; |
| 350 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 351 | /* size should be aligned with order_per_bit */ |
| 352 | if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit)) |
| 353 | return -EINVAL; |
| 354 | |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 355 | /* |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 356 | * If allocating at a fixed base the request region must not cross the |
| 357 | * low/high memory boundary. |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 358 | */ |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 359 | if (fixed && base < highmem_start && base + size > highmem_start) { |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 360 | ret = -EINVAL; |
Laurent Pinchart | 56fa4f6 | 2014-10-24 13:18:42 +0300 | [diff] [blame] | 361 | pr_err("Region at %pa defined on low/high memory boundary (%pa)\n", |
| 362 | &base, &highmem_start); |
Marek Szyprowski | f7426b9 | 2014-10-09 15:26:47 -0700 | [diff] [blame] | 363 | goto err; |
| 364 | } |
| 365 | |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 366 | /* |
| 367 | * If the limit is unspecified or above the memblock end, its effective |
| 368 | * value will be the memblock end. Set it explicitly to simplify further |
| 369 | * checks. |
| 370 | */ |
| 371 | if (limit == 0 || limit > memblock_end) |
| 372 | limit = memblock_end; |
| 373 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 374 | /* Reserve memory */ |
Laurent Pinchart | 800a85d | 2014-10-24 13:18:40 +0300 | [diff] [blame] | 375 | if (fixed) { |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 376 | if (memblock_is_region_reserved(base, size) || |
| 377 | memblock_reserve(base, size) < 0) { |
| 378 | ret = -EBUSY; |
| 379 | goto err; |
| 380 | } |
| 381 | } else { |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 382 | phys_addr_t addr = 0; |
| 383 | |
| 384 | /* |
| 385 | * All pages in the reserved area must come from the same zone. |
| 386 | * If the requested region crosses the low/high memory boundary, |
| 387 | * try allocating from high memory first and fall back to low |
| 388 | * memory in case of failure. |
| 389 | */ |
| 390 | if (base < highmem_start && limit > highmem_start) { |
| 391 | addr = memblock_alloc_range(size, alignment, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 392 | highmem_start, limit, |
| 393 | MEMBLOCK_NONE); |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 394 | limit = highmem_start; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 395 | } |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 396 | |
| 397 | if (!addr) { |
| 398 | addr = memblock_alloc_range(size, alignment, base, |
Tony Luck | fc6daaf | 2015-06-24 16:58:09 -0700 | [diff] [blame] | 399 | limit, |
| 400 | MEMBLOCK_NONE); |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 401 | if (!addr) { |
| 402 | ret = -ENOMEM; |
| 403 | goto err; |
| 404 | } |
| 405 | } |
| 406 | |
Thierry Reding | 620951e | 2014-12-12 16:58:31 -0800 | [diff] [blame] | 407 | /* |
| 408 | * kmemleak scans/reads tracked objects for pointers to other |
| 409 | * objects but this address isn't mapped and accessible |
| 410 | */ |
Catalin Marinas | 9099dae | 2016-10-11 13:55:11 -0700 | [diff] [blame] | 411 | kmemleak_ignore_phys(addr); |
Laurent Pinchart | 16195dd | 2014-10-24 13:18:41 +0300 | [diff] [blame] | 412 | base = addr; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Laura Abbott | f318dd0 | 2017-04-18 11:27:03 -0700 | [diff] [blame] | 415 | ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma); |
Marek Szyprowski | de9e14e | 2014-10-13 15:51:09 -0700 | [diff] [blame] | 416 | if (ret) |
| 417 | goto err; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 418 | |
Laurent Pinchart | 56fa4f6 | 2014-10-24 13:18:42 +0300 | [diff] [blame] | 419 | pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M, |
| 420 | &base); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 421 | return 0; |
| 422 | |
| 423 | err: |
Joonsoo Kim | 0de9d2e | 2014-08-06 16:05:34 -0700 | [diff] [blame] | 424 | pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 425 | return ret; |
| 426 | } |
| 427 | |
Jaewon Kim | dbe43d4 | 2017-02-24 14:58:50 -0800 | [diff] [blame] | 428 | #ifdef CONFIG_CMA_DEBUG |
| 429 | static void cma_debug_show_areas(struct cma *cma) |
| 430 | { |
| 431 | unsigned long next_zero_bit, next_set_bit; |
| 432 | unsigned long start = 0; |
| 433 | unsigned int nr_zero, nr_total = 0; |
| 434 | |
| 435 | mutex_lock(&cma->lock); |
| 436 | pr_info("number of available pages: "); |
| 437 | for (;;) { |
| 438 | next_zero_bit = find_next_zero_bit(cma->bitmap, cma->count, start); |
| 439 | if (next_zero_bit >= cma->count) |
| 440 | break; |
| 441 | next_set_bit = find_next_bit(cma->bitmap, cma->count, next_zero_bit); |
| 442 | nr_zero = next_set_bit - next_zero_bit; |
| 443 | pr_cont("%s%u@%lu", nr_total ? "+" : "", nr_zero, next_zero_bit); |
| 444 | nr_total += nr_zero; |
| 445 | start = next_zero_bit + nr_zero; |
| 446 | } |
| 447 | pr_cont("=> %u free of %lu total pages\n", nr_total, cma->count); |
| 448 | mutex_unlock(&cma->lock); |
| 449 | } |
| 450 | #else |
| 451 | static inline void cma_debug_show_areas(struct cma *cma) { } |
| 452 | #endif |
| 453 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 454 | /** |
| 455 | * cma_alloc() - allocate pages from contiguous area |
| 456 | * @cma: Contiguous memory region for which the allocation is performed. |
| 457 | * @count: Requested number of pages. |
| 458 | * @align: Requested alignment of pages (in PAGE_SIZE order). |
Mike Rapoport | e8b098f | 2018-04-05 16:24:57 -0700 | [diff] [blame] | 459 | * @gfp_mask: GFP mask to use during compaction |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 460 | * |
| 461 | * This function allocates part of contiguous memory on specific |
| 462 | * contiguous memory area. |
| 463 | */ |
Lucas Stach | e2f466e | 2017-02-24 14:58:41 -0800 | [diff] [blame] | 464 | struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, |
| 465 | gfp_t gfp_mask) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 466 | { |
Andrew Morton | 3acaea6 | 2015-11-05 18:50:08 -0800 | [diff] [blame] | 467 | unsigned long mask, offset; |
| 468 | unsigned long pfn = -1; |
| 469 | unsigned long start = 0; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 470 | unsigned long bitmap_maxno, bitmap_no, bitmap_count; |
| 471 | struct page *page = NULL; |
Jaewon Kim | dbe43d4 | 2017-02-24 14:58:50 -0800 | [diff] [blame] | 472 | int ret = -ENOMEM; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 473 | |
| 474 | if (!cma || !cma->count) |
| 475 | return NULL; |
| 476 | |
Rohit Vaswani | 67a2e213 | 2015-10-22 13:32:11 -0700 | [diff] [blame] | 477 | pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma, |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 478 | count, align); |
| 479 | |
| 480 | if (!count) |
| 481 | return NULL; |
| 482 | |
| 483 | mask = cma_bitmap_aligned_mask(cma, align); |
Gregory Fong | b5be83e | 2014-12-12 16:54:48 -0800 | [diff] [blame] | 484 | offset = cma_bitmap_aligned_offset(cma, align); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 485 | bitmap_maxno = cma_bitmap_maxno(cma); |
| 486 | bitmap_count = cma_bitmap_pages_to_bits(cma, count); |
| 487 | |
Shiraz Hashim | 6b36ba5 | 2016-11-10 10:46:16 -0800 | [diff] [blame] | 488 | if (bitmap_count > bitmap_maxno) |
| 489 | return NULL; |
| 490 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 491 | for (;;) { |
| 492 | mutex_lock(&cma->lock); |
Gregory Fong | b5be83e | 2014-12-12 16:54:48 -0800 | [diff] [blame] | 493 | bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap, |
| 494 | bitmap_maxno, start, bitmap_count, mask, |
| 495 | offset); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 496 | if (bitmap_no >= bitmap_maxno) { |
| 497 | mutex_unlock(&cma->lock); |
| 498 | break; |
| 499 | } |
| 500 | bitmap_set(cma->bitmap, bitmap_no, bitmap_count); |
| 501 | /* |
| 502 | * It's safe to drop the lock here. We've marked this region for |
| 503 | * our exclusive use. If the migration fails we will take the |
| 504 | * lock again and unmark it. |
| 505 | */ |
| 506 | mutex_unlock(&cma->lock); |
| 507 | |
| 508 | pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); |
| 509 | mutex_lock(&cma_mutex); |
Lucas Stach | ca96b62 | 2017-02-24 14:58:37 -0800 | [diff] [blame] | 510 | ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, |
Lucas Stach | e2f466e | 2017-02-24 14:58:41 -0800 | [diff] [blame] | 511 | gfp_mask); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 512 | mutex_unlock(&cma_mutex); |
| 513 | if (ret == 0) { |
| 514 | page = pfn_to_page(pfn); |
| 515 | break; |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 516 | } |
Joonsoo Kim | b7155e7 | 2014-08-06 16:05:30 -0700 | [diff] [blame] | 517 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 518 | cma_clear_bitmap(cma, pfn, count); |
Joonsoo Kim | b7155e7 | 2014-08-06 16:05:30 -0700 | [diff] [blame] | 519 | if (ret != -EBUSY) |
| 520 | break; |
| 521 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 522 | pr_debug("%s(): memory range at %p is busy, retrying\n", |
| 523 | __func__, pfn_to_page(pfn)); |
| 524 | /* try again with a bit different memory target */ |
| 525 | start = bitmap_no + mask + 1; |
| 526 | } |
| 527 | |
Andrew Morton | 3acaea6 | 2015-11-05 18:50:08 -0800 | [diff] [blame] | 528 | trace_cma_alloc(pfn, page, count, align); |
Stefan Strogin | 99e8ea6 | 2015-04-15 16:14:50 -0700 | [diff] [blame] | 529 | |
Boris Brezillon | ef46501 | 2017-10-13 15:58:01 -0700 | [diff] [blame] | 530 | if (ret && !(gfp_mask & __GFP_NOWARN)) { |
Pintu Agarwal | 5984af108 | 2017-11-15 17:34:26 -0800 | [diff] [blame] | 531 | pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n", |
Jaewon Kim | dbe43d4 | 2017-02-24 14:58:50 -0800 | [diff] [blame] | 532 | __func__, count, ret); |
| 533 | cma_debug_show_areas(cma); |
| 534 | } |
| 535 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 536 | pr_debug("%s(): returned %p\n", __func__, page); |
| 537 | return page; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * cma_release() - release allocated pages |
| 542 | * @cma: Contiguous memory region for which the allocation is performed. |
| 543 | * @pages: Allocated pages. |
| 544 | * @count: Number of allocated pages. |
| 545 | * |
| 546 | * This function releases memory allocated by alloc_cma(). |
| 547 | * It returns false when provided pages do not belong to contiguous area and |
| 548 | * true otherwise. |
| 549 | */ |
Sasha Levin | ac17382 | 2015-04-14 15:47:04 -0700 | [diff] [blame] | 550 | bool cma_release(struct cma *cma, const struct page *pages, unsigned int count) |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 551 | { |
| 552 | unsigned long pfn; |
| 553 | |
| 554 | if (!cma || !pages) |
| 555 | return false; |
| 556 | |
| 557 | pr_debug("%s(page %p)\n", __func__, (void *)pages); |
| 558 | |
| 559 | pfn = page_to_pfn(pages); |
| 560 | |
| 561 | if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) |
| 562 | return false; |
| 563 | |
| 564 | VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); |
| 565 | |
| 566 | free_contig_range(pfn, count); |
| 567 | cma_clear_bitmap(cma, pfn, count); |
Stefan Strogin | 99e8ea6 | 2015-04-15 16:14:50 -0700 | [diff] [blame] | 568 | trace_cma_release(pfn, pages, count); |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 569 | |
| 570 | return true; |
| 571 | } |
Laura Abbott | e4231bc | 2017-04-18 11:27:04 -0700 | [diff] [blame] | 572 | |
| 573 | int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) |
| 574 | { |
| 575 | int i; |
| 576 | |
| 577 | for (i = 0; i < cma_area_count; i++) { |
| 578 | int ret = it(&cma_areas[i], data); |
| 579 | |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |