linux cma内存管理

来源:互联网 发布:淘宝店铺折扣怎么设置 编辑:程序博客网 时间:2024/05/16 09:12
1. 什么是cma
cma(contigous memory allocator),是内存管理子系统中的一个模块,负责物理地址连续的内存分配。一般系统会在启动过程中,从整个memory中配置一段连续内存用于cma,其他模块可以通过cma的接口api进行连续内存分配。


2. cma优势
解决内存浪费的问题,其保留的内存可以由buddy系统分配,当真正需要此连续内存时,可以将由buddy系统分配的内存migrate到其他地方,腾出连续的空间


3. cma问题
性能问题,内存迁移会带来性能损耗,在内存紧张时尤为明显,cpu占用率会变高


4. cma类型
cma有两种类型:global cma area和per device cma area


5. cma配置
linux 4.9中已经实现cma的相关机制,可以在make config中进行相关配置打开此功能
Kernel Features  --->
[*] Contiguous Memory Allocator
Device Drivers  --->
Generic Driver Options  --->
[*] DMA Contiguous Memory Allocator

(是否有其他配置选项还有待研究)


6. cma分配
有3种方法进行cma内存分配,通过dts分配(推荐),通过命令行参数,通过make config配置
后两种方法只可以分配global cma area,所以推荐使用dts分配。

dts分配参考示例,其中有“shared-dma-pool”关键字的cma区域作为global cma area。

{        #address-cells = <1>;        #size-cells = <1>;        memory {                reg = <0x40000000 0x40000000>;        };        reserved-memory {                #address-cells = <1>;                #size-cells = <1>;                ranges;                /* global autoconfigured region for contiguous allocations */                linux,cma {                        compatible = "shared-dma-pool";                        reusable;                        size = <0x4000000>;                        alignment = <0x2000>;                        linux,cma-default;                };                display_reserved: framebuffer@78000000 {                        reg = <0x78000000 0x800000>;                };                multimedia_reserved: multimedia@77000000 {                        compatible = "acme,multimedia-memory";                        reg = <0x77000000 0x4000000>;                };        };        /* ... */        fb0: video@12300000 {                memory-region = <&display_reserved>;                /* ... */        };        scaler: scaler@12500000 {                memory-region = <&multimedia_reserved>;                /* ... */        };        codec: codec@12600000 {                memory-region = <&multimedia_reserved>;                /* ... */        };};

7. cma初始化
setup_arch --> arm64_memblock_init --> early_init_fdt_scan_reserved_mem --> __fdt_scan_reserved_mem fdt_init_reserved_mem


8. cma使用
申请和释放cma area时仍然采用dma-mapping.c定义的方法dma_alloc_coherent和dma_free_coherent。

dma_alloc_coherent --> dma_alloc_attrs --> ops->alloc --> __dma_alloc_coherent --> dma_alloc_from_contiguous --> cma_alloc

dma_free_coherent --> dma_free_attrs --> ops->free --> __dma_free_coherent --> dma_release_from_contiguous --> cma_release

以上函数参考linux-4.9 kernel


参考文章

http://happyseeker.github.io/kernel/2016/02/29/about-CMA.html

http://www.wowotech.net/memory_management/cma.html

http://blog.csdn.net/21cnbao/article/details/7309757


crcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcr

Hufikyu的学习空间,欢迎大家提出问题,共同进步。

crcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcrcr

阅读全文
0 0