mm/cma.c
Source file repositories/reference/linux-study-clean/mm/cma.c
File Facts
- System
- Linux kernel
- Corpus path
mm/cma.c- Extension
.c- Size
- 30137 bytes
- Lines
- 1150
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/memblock.hlinux/err.hlinux/list.hlinux/mm.hlinux/sizes.hlinux/slab.hlinux/string.hlinux/string_choices.hlinux/log2.hlinux/cma.hlinux/highmem.hlinux/io.hlinux/kmemleak.htrace/events/cma.hinternal.hcma.h
Detected Declarations
struct cma_init_memrangefunction cma_get_basefunction cma_get_sizefunction cma_bitmap_aligned_maskfunction cma_bitmap_aligned_offsetfunction cma_bitmap_pages_to_bitsfunction cma_clear_bitmapfunction cma_validate_zonesfunction cma_activate_areafunction cma_init_reserved_areasfunction cma_reserve_pages_on_errorfunction cma_new_areafunction cma_drop_areafunction cma_init_reserved_memfunction revsizecmpfunction basecmpfunction list_insert_sortedfunction list_for_eachfunction cma_fixed_reservefunction cma_alloc_memfunction __cma_declare_contiguous_nidfunction elementsfunction cma_declare_contiguous_nidfunction cma_debug_show_areasfunction for_each_clear_bitrangefunction cma_range_allocfunction cma_allocfunction __cma_release_frozenfunction cma_releasefunction cma_release_frozenfunction cma_for_each_areafunction cma_intersectsfunction callermodule init cma_init_reserved_areasexport cma_get_nameexport cma_allocexport cma_release
Annotated Snippet
core_initcall(cma_init_reserved_areas);
void __init cma_reserve_pages_on_error(struct cma *cma)
{
set_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags);
}
static int __init cma_new_area(const char *name, phys_addr_t size,
unsigned int order_per_bit,
struct cma **res_cma)
{
struct cma *cma;
if (cma_area_count == ARRAY_SIZE(cma_areas)) {
pr_err("Not enough slots for CMA reserved regions!\n");
return -ENOSPC;
}
/*
* Each reserved area must be initialised later, when more kernel
* subsystems (like slab allocator) are available.
*/
cma = &cma_areas[cma_area_count];
cma_area_count++;
if (name)
strscpy(cma->name, name);
else
snprintf(cma->name, CMA_MAX_NAME, "cma%d\n", cma_area_count);
cma->available_count = cma->count = size >> PAGE_SHIFT;
cma->order_per_bit = order_per_bit;
*res_cma = cma;
totalcma_pages += cma->count;
return 0;
}
static void __init cma_drop_area(struct cma *cma)
{
totalcma_pages -= cma->count;
cma_area_count--;
}
/**
* cma_init_reserved_mem() - create custom contiguous area from reserved memory
* @base: Base address of the reserved area
* @size: Size of the reserved area (in bytes),
* @order_per_bit: Order of pages represented by one bit on bitmap.
* @name: The name of the area. If this parameter is NULL, the name of
* the area will be set to "cmaN", where N is a running counter of
* used areas.
* @res_cma: Pointer to store the created cma region.
*
* This function creates custom contiguous area from already reserved memory.
*/
int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
unsigned int order_per_bit,
const char *name,
struct cma **res_cma)
{
struct cma *cma;
int ret;
/* Sanity checks */
if (!size || !memblock_is_region_reserved(base, size))
return -EINVAL;
/*
* CMA uses CMA_MIN_ALIGNMENT_BYTES as alignment requirement which
* needs pageblock_order to be initialized. Let's enforce it.
*/
if (!pageblock_order) {
pr_err("pageblock_order not yet initialized. Called during early boot?\n");
return -EINVAL;
}
/* ensure minimal alignment required by mm core */
if (!IS_ALIGNED(base | size, CMA_MIN_ALIGNMENT_BYTES))
return -EINVAL;
ret = cma_new_area(name, size, order_per_bit, &cma);
if (ret != 0)
return ret;
cma->ranges[0].base_pfn = PFN_DOWN(base);
cma->ranges[0].early_pfn = PFN_DOWN(base);
cma->ranges[0].count = cma->count;
cma->nranges = 1;
cma->nid = NUMA_NO_NODE;
Annotation
- Immediate include surface: `linux/memblock.h`, `linux/err.h`, `linux/list.h`, `linux/mm.h`, `linux/sizes.h`, `linux/slab.h`, `linux/string.h`, `linux/string_choices.h`.
- Detected declarations: `struct cma_init_memrange`, `function cma_get_base`, `function cma_get_size`, `function cma_bitmap_aligned_mask`, `function cma_bitmap_aligned_offset`, `function cma_bitmap_pages_to_bits`, `function cma_clear_bitmap`, `function cma_validate_zones`, `function cma_activate_area`, `function cma_init_reserved_areas`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.