Read the fucking source code!
--By 鲁迅A picture is worth a thousand words.
--By 高尔基说明:数组
Contiguous Memory Allocator, CMA
,连续内存分配器,用于分配连续的大块内存。
CMA分配器
,会Reserve一片物理内存区域:数据结构
此外,CMA分配器
还能够与DMA子系统
集成在一块儿,使用DMA的设备驱动程序无需使用单独的CMA API
。并发
内核定义了struct cma
结构,用于管理一个CMA区域
,此外还定义了全局的cma数组
,以下:框架
struct cma { unsigned long base_pfn; unsigned long count; unsigned long *bitmap; unsigned int order_per_bit; /* Order of pages represented by one bit */ struct mutex lock; #ifdef CONFIG_CMA_DEBUGFS struct hlist_head mem_head; spinlock_t mem_head_lock; #endif const char *name; }; extern struct cma cma_areas[MAX_CMA_AREAS]; extern unsigned cma_area_count;
base_pfn
:CMA区域物理地址的起始页帧号;count
:CMA区域整体的页数;*bitmap
:位图,用于描述页的分配状况;order_per_bit
:位图中每一个bit
描述的物理页面的order
值,其中页面数为2^order
值;来一张图就会清晰明了:ide
以前的文章也都分析过,物理内存的描述放置在dts
中,最终会在系统启动过程当中,对dtb
文件进行解析,从而完成内存信息注册。函数
CMA
的内存在dts
中的描述示例以下图:工具
在dtb
解析过程当中,会调用到rmem_cma_setup
函数:学习
RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
能够经过内核参数或配置宏,来进行CMA区域的建立,最终会调用到cma_declare_contiguous
函数,以下图:ui
在建立完CMA区域
后,该内存区域成了保留区域,若是单纯给驱动使用,显然会形成内存的浪费,所以内存管理模块会将CMA区域
添加到Buddy System
中,用于可移动页面的分配和管理。CMA区域
是经过cma_init_reserved_areas
接口来添加到Buddy System
中的。this
core_initcall(cma_init_reserved_areas);
core_initcall
宏将cma_init_reserved_areas
函数放置到特定的段中,在系统启动的时候会调用到该函数。
cma_alloc
:cma_release
:/** * cma_release() - release allocated pages * @cma: Contiguous memory region for which the allocation is performed. * @pages: Allocated pages. * @count: Number of allocated pages. * * This function releases memory allocated by alloc_cma(). * It returns false when provided pages do not belong to contiguous area and * true otherwise. */ bool cma_release(struct cma *cma, const struct page *pages, unsigned int count) { unsigned long pfn; if (!cma || !pages) return false; pr_debug("%s(page %p)\n", __func__, (void *)pages); pfn = page_to_pfn(pages); if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) return false; VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); free_contig_range(pfn, count); cma_clear_bitmap(cma, pfn, count); trace_cma_release(pfn, pages, count); return true; }
代码参考driver/base/dma-contiguous.c
,主要包括的接口有:
/** * dma_alloc_from_contiguous() - allocate pages from contiguous area * @dev: Pointer to device for which the allocation is performed. * @count: Requested number of pages. * @align: Requested alignment of pages (in PAGE_SIZE order). * @gfp_mask: GFP flags to use for this allocation. * * This function allocates memory buffer for specified device. It uses * device specific contiguous memory area if available or the default * global one. Requires architecture specific dev_get_cma_area() helper * function. */ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, unsigned int align, gfp_t gfp_mask); /** * dma_release_from_contiguous() - release allocated pages * @dev: Pointer to device for which the pages were allocated. * @pages: Allocated pages. * @count: Number of allocated pages. * * This function releases memory allocated by dma_alloc_from_contiguous(). * It returns false when provided pages do not belong to contiguous area and * true otherwise. */ bool dma_release_from_contiguous(struct device *dev, struct page *pages, int count);
在上述的接口中,实际调用的就是cma_alloc/cma_release
接口来实现的。
总体来看,CMA分配器仍是比较简单易懂,也再也不深刻分析。
内存管理的分析先告一段落,后续可能还会针对某些模块进一步的研究与完善。
内存管理子系统,极其复杂,盘根错节,很容易就懵圈了,尽管费了很多心力,也只能说略知皮毛。
学习就像是登山,面对一座高山,可能会有心理障碍,可是当你跨越以后,再看到一样高的山,心理上你将再也不畏惧。
接下来将研究进程管理子系统
,将任督二脉打通。
将来会持续分析内核中的各种框架,并发机制等,敬请关注,一块儿探讨。