]> Git Repo - linux.git/blame - mm/cma.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
[linux.git] / mm / cma.c
CommitLineData
a254129e
JK
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 <[email protected]>
9 * Michal Nazarewicz <[email protected]>
10 * Aneesh Kumar K.V <[email protected]>
11 * Joonsoo Kim <[email protected]>
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
99e8ea6c 26#define CREATE_TRACE_POINTS
a254129e
JK
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>
f7426b98 36#include <linux/highmem.h>
620951e2 37#include <linux/io.h>
514c6032 38#include <linux/kmemleak.h>
99e8ea6c 39#include <trace/events/cma.h>
a254129e 40
28b24c1f 41#include "cma.h"
bad8c6c0 42#include "internal.h"
28b24c1f
SL
43
44struct cma cma_areas[MAX_CMA_AREAS];
45unsigned cma_area_count;
a254129e
JK
46static DEFINE_MUTEX(cma_mutex);
47
ac173824 48phys_addr_t cma_get_base(const struct cma *cma)
a254129e
JK
49{
50 return PFN_PHYS(cma->base_pfn);
51}
52
ac173824 53unsigned long cma_get_size(const struct cma *cma)
a254129e
JK
54{
55 return cma->count << PAGE_SHIFT;
56}
57
f318dd08
LA
58const char *cma_get_name(const struct cma *cma)
59{
60 return cma->name ? cma->name : "(undefined)";
61}
62
ac173824 63static unsigned long cma_bitmap_aligned_mask(const struct cma *cma,
e048cb32 64 unsigned int align_order)
a254129e 65{
68faed63
WY
66 if (align_order <= cma->order_per_bit)
67 return 0;
68 return (1UL << (align_order - cma->order_per_bit)) - 1;
a254129e
JK
69}
70
850fc430 71/*
e048cb32
DB
72 * Find the offset of the base PFN from the specified align_order.
73 * The value returned is represented in order_per_bits.
850fc430 74 */
ac173824 75static unsigned long cma_bitmap_aligned_offset(const struct cma *cma,
e048cb32 76 unsigned int align_order)
b5be83e3 77{
e048cb32
DB
78 return (cma->base_pfn & ((1UL << align_order) - 1))
79 >> cma->order_per_bit;
b5be83e3
GF
80}
81
ac173824
SL
82static unsigned long cma_bitmap_pages_to_bits(const struct cma *cma,
83 unsigned long pages)
a254129e
JK
84{
85 return ALIGN(pages, 1UL << cma->order_per_bit) >> cma->order_per_bit;
86}
87
ac173824
SL
88static void cma_clear_bitmap(struct cma *cma, unsigned long pfn,
89 unsigned int count)
a254129e
JK
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
101static 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
a254129e
JK
113 do {
114 unsigned j;
115
116 base_pfn = pfn;
bad8c6c0
JK
117 if (!pfn_valid(base_pfn))
118 goto err;
119
120 zone = page_zone(pfn_to_page(base_pfn));
a254129e 121 for (j = pageblock_nr_pages; j; --j, pfn++) {
bad8c6c0
JK
122 if (!pfn_valid(pfn))
123 goto err;
124
a254129e 125 /*
bad8c6c0
JK
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.
a254129e
JK
129 */
130 if (page_zone(pfn_to_page(pfn)) != zone)
bad8c6c0 131 goto err;
a254129e
JK
132 }
133 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
134 } while (--i);
135
136 mutex_init(&cma->lock);
26b02a1f
SL
137
138#ifdef CONFIG_CMA_DEBUGFS
139 INIT_HLIST_HEAD(&cma->mem_head);
140 spin_lock_init(&cma->mem_head_lock);
141#endif
142
a254129e
JK
143 return 0;
144
bad8c6c0 145err:
e35ef639 146 pr_err("CMA area %s could not be activated\n", cma->name);
a254129e 147 kfree(cma->bitmap);
f022d8cb 148 cma->count = 0;
a254129e
JK
149 return -EINVAL;
150}
151
152static int __init cma_init_reserved_areas(void)
153{
154 int i;
bad8c6c0
JK
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 }
a254129e
JK
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
bad8c6c0
JK
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
a254129e
JK
221 return 0;
222}
bad8c6c0 223pure_initcall(cma_init_reserved_areas);
a254129e 224
de9e14ee
MS
225/**
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.
e8b098fc
MR
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.
de9e14ee
MS
233 * @res_cma: Pointer to store the created cma region.
234 *
235 * This function creates custom contiguous area from already reserved memory.
236 */
237int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
ac173824 238 unsigned int order_per_bit,
f318dd08 239 const char *name,
ac173824 240 struct cma **res_cma)
de9e14ee
MS
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
0f96ae29 254 /* ensure minimal alignment required by mm core */
badbda53
SR
255 alignment = PAGE_SIZE <<
256 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
de9e14ee
MS
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];
f318dd08
LA
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 }
de9e14ee
MS
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++;
94737a85 282 totalcma_pages += (size / PAGE_SIZE);
de9e14ee
MS
283
284 return 0;
285}
286
a254129e
JK
287/**
288 * cma_declare_contiguous() - reserve custom contiguous area
a254129e 289 * @base: Base address of the reserved area optional, use 0 for any
c1f733aa 290 * @size: Size of the reserved area (in bytes),
a254129e
JK
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.
a254129e 294 * @fixed: hint about where to place the reserved area
e8b098fc 295 * @name: The name of the area. See function cma_init_reserved_mem()
c1f733aa 296 * @res_cma: Pointer to store the created cma region.
a254129e
JK
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 */
c1f733aa
JK
306int __init cma_declare_contiguous(phys_addr_t base,
307 phys_addr_t size, phys_addr_t limit,
a254129e 308 phys_addr_t alignment, unsigned int order_per_bit,
f318dd08 309 bool fixed, const char *name, struct cma **res_cma)
a254129e 310{
f7426b98 311 phys_addr_t memblock_end = memblock_end_of_DRAM();
6b101e2a 312 phys_addr_t highmem_start;
a254129e
JK
313 int ret = 0;
314
6b101e2a 315 /*
2dece445
LA
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.
6b101e2a 320 */
2dece445 321 highmem_start = __pa(high_memory - 1) + 1;
56fa4f60
LP
322 pr_debug("%s(size %pa, base %pa, limit %pa alignment %pa)\n",
323 __func__, &size, &base, &limit, &alignment);
a254129e
JK
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 */
badbda53
SR
342 alignment = max(alignment, (phys_addr_t)PAGE_SIZE <<
343 max_t(unsigned long, MAX_ORDER - 1, pageblock_order));
a254129e
JK
344 base = ALIGN(base, alignment);
345 size = ALIGN(size, alignment);
346 limit &= ~(alignment - 1);
347
800a85d3
LP
348 if (!base)
349 fixed = false;
350
a254129e
JK
351 /* size should be aligned with order_per_bit */
352 if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
353 return -EINVAL;
354
f7426b98 355 /*
16195ddd
LP
356 * If allocating at a fixed base the request region must not cross the
357 * low/high memory boundary.
f7426b98 358 */
16195ddd 359 if (fixed && base < highmem_start && base + size > highmem_start) {
f7426b98 360 ret = -EINVAL;
56fa4f60
LP
361 pr_err("Region at %pa defined on low/high memory boundary (%pa)\n",
362 &base, &highmem_start);
f7426b98
MS
363 goto err;
364 }
365
16195ddd
LP
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
a254129e 374 /* Reserve memory */
800a85d3 375 if (fixed) {
a254129e
JK
376 if (memblock_is_region_reserved(base, size) ||
377 memblock_reserve(base, size) < 0) {
378 ret = -EBUSY;
379 goto err;
380 }
381 } else {
16195ddd
LP
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,
fc6daaf9
TL
392 highmem_start, limit,
393 MEMBLOCK_NONE);
16195ddd
LP
394 limit = highmem_start;
395 }
396
a254129e 397 if (!addr) {
16195ddd 398 addr = memblock_alloc_range(size, alignment, base,
fc6daaf9
TL
399 limit,
400 MEMBLOCK_NONE);
16195ddd
LP
401 if (!addr) {
402 ret = -ENOMEM;
403 goto err;
404 }
a254129e 405 }
16195ddd 406
620951e2
TR
407 /*
408 * kmemleak scans/reads tracked objects for pointers to other
409 * objects but this address isn't mapped and accessible
410 */
9099daed 411 kmemleak_ignore_phys(addr);
16195ddd 412 base = addr;
a254129e
JK
413 }
414
f318dd08 415 ret = cma_init_reserved_mem(base, size, order_per_bit, name, res_cma);
de9e14ee
MS
416 if (ret)
417 goto err;
a254129e 418
56fa4f60
LP
419 pr_info("Reserved %ld MiB at %pa\n", (unsigned long)size / SZ_1M,
420 &base);
a254129e
JK
421 return 0;
422
423err:
0de9d2eb 424 pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
a254129e
JK
425 return ret;
426}
427
dbe43d4d
JK
428#ifdef CONFIG_CMA_DEBUG
429static 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
451static inline void cma_debug_show_areas(struct cma *cma) { }
452#endif
453
a254129e
JK
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).
e8b098fc 459 * @gfp_mask: GFP mask to use during compaction
a254129e
JK
460 *
461 * This function allocates part of contiguous memory on specific
462 * contiguous memory area.
463 */
e2f466e3
LS
464struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
465 gfp_t gfp_mask)
a254129e 466{
3acaea68
AM
467 unsigned long mask, offset;
468 unsigned long pfn = -1;
469 unsigned long start = 0;
a254129e
JK
470 unsigned long bitmap_maxno, bitmap_no, bitmap_count;
471 struct page *page = NULL;
dbe43d4d 472 int ret = -ENOMEM;
a254129e
JK
473
474 if (!cma || !cma->count)
475 return NULL;
476
67a2e213 477 pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
a254129e
JK
478 count, align);
479
480 if (!count)
481 return NULL;
482
483 mask = cma_bitmap_aligned_mask(cma, align);
b5be83e3 484 offset = cma_bitmap_aligned_offset(cma, align);
a254129e
JK
485 bitmap_maxno = cma_bitmap_maxno(cma);
486 bitmap_count = cma_bitmap_pages_to_bits(cma, count);
487
6b36ba59
SH
488 if (bitmap_count > bitmap_maxno)
489 return NULL;
490
a254129e
JK
491 for (;;) {
492 mutex_lock(&cma->lock);
b5be83e3
GF
493 bitmap_no = bitmap_find_next_zero_area_off(cma->bitmap,
494 bitmap_maxno, start, bitmap_count, mask,
495 offset);
a254129e
JK
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);
ca96b625 510 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
e2f466e3 511 gfp_mask);
a254129e
JK
512 mutex_unlock(&cma_mutex);
513 if (ret == 0) {
514 page = pfn_to_page(pfn);
515 break;
a254129e 516 }
b7155e76 517
a254129e 518 cma_clear_bitmap(cma, pfn, count);
b7155e76
JK
519 if (ret != -EBUSY)
520 break;
521
a254129e
JK
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
3acaea68 528 trace_cma_alloc(pfn, page, count, align);
99e8ea6c 529
ef465014 530 if (ret && !(gfp_mask & __GFP_NOWARN)) {
5984af10 531 pr_err("%s: alloc failed, req-size: %zu pages, ret: %d\n",
dbe43d4d
JK
532 __func__, count, ret);
533 cma_debug_show_areas(cma);
534 }
535
a254129e
JK
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 */
ac173824 550bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)
a254129e
JK
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);
99e8ea6c 568 trace_cma_release(pfn, pages, count);
a254129e
JK
569
570 return true;
571}
e4231bcd
LA
572
573int 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}
This page took 0.328238 seconds and 4 git commands to generate.