linux cma内存,【原创】(十六)Linux内存管理之CMA,
【原创】(十六)Linux内存管理之CMA,
背景
Read the fucking source code! --By 鲁迅
A picture is worth a thousand words. --By 高尔基
说明:
1. 概述
Contiguous Memory Allocator, CMA,连续内存分配器,用于分配连续的大块内存。
CMA分配器,会Reserve一片物理内存区域:
此外,CMA分配器还可以与DMA子系统集成在一起,使用DMA的设备驱动程序无需使用单独的CMA API。
2. 数据结构
内核定义了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值;
来一张图就会清晰明了:
3. 流程分析
3.1 CMA区域创建
3.1.1 方式一 根据dts来配置
之前的文章也都分析过,物理内存的描述放置在dts中,最终会在系统启动过程中,对dtb文件进行解析,从而完成内存信息注册。
CMA的内存在dts中的描述示例如下图:
在dtb解析过程中,会调用到rmem_cma_setup函数:
RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
3.1.2 方式二 根据参数或宏配置
可以通过内核参数或配置宏,来进行CMA区域的创建,最终会调用到cma_declare_contiguous函数,如下图:
3.2 CMA添加到Buddy System
在创建完CMA区域后,该内存区域成了保留区域,如果单纯给驱动使用,显然会造成内存的浪费,因此内存管理模块会将CMA区域添加到Buddy System中,用于可移动页面的分配和管理。CMA区域是通过cma_init_reserved_areas接口来添加到Buddy System中的。
core_initcall(cma_init_reserved_areas);
core_initcall宏将cma_init_reserved_areas函数放置到特定的段中,在系统启动的时候会调用到该函数。
3.3 CMA分配/释放
CMA分配,入口函数为cma_alloc:
CMA释放,入口函数为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;
}
3.4 DMA使用
代码参考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分配器还是比较简单易懂,也不再深入分析。
4.后记
内存管理的分析先告一段落,后续可能还会针对某些模块进一步的研究与完善。
内存管理子系统,极其复杂,盘根错节,很容易就懵圈了,尽管费了不少心力,也只能说略知皮毛。
学习就像是爬山,面对一座高山,可能会有心理障碍,但是当你跨越之后,再看到同样高的山,心理上你将不再畏惧。
接下来将研究进程管理子系统,将任督二脉打通。
未来会持续分析内核中的各类框架,并发机制等,敬请关注,一起探讨。
http://www.dengb.com/Linuxjc/1389768.htmlwww.dengb.comtruehttp://www.dengb.com/Linuxjc/1389768.htmlTechArticle【原创】(十六)Linux内存管理之CMA, 背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: 1. 概述 Contiguo...
linux cma内存,【原创】(十六)Linux内存管理之CMA,相关推荐
- 【Linux命令】《鸟哥Linux基础》第十六章 进程管理与SELinux初探
第十六章 进程管理与SELinux初探 16.1 什么是进程(process) Linux下的所有命令与你能够执行的操作 ===>都与权限有关 如何判断权限? 账号管理中的UID.GID:文件属 ...
- linux基础-第十六单元 yum管理RPM包
第十六单元 yum管理RPM包 yum的功能 本地yum配置 光盘挂载和镜像挂载 本地yum配置 网络yum配置 网络yum配置 Yum命令的使用 使用yum安装软件 使用yum删除软件 安装组件 删 ...
- linux查看hex编码,小弟我使用过的Linux命令之hexdump - ”十六“进制查看器
我使用过的Linux命令之hexdump - "十六"进制查看器 我使用过的Linux命令之hexdump - "十六"进制查看器 本文链接:http://co ...
- 十六.linux开发之Kernel移植——内核的配置和编译原理
有道云笔记地址: 详情看这里链接,记录太多,就不一一排版了. http://note.youdao.com/noteshare?id=d25dbce79566963e3699574a74048154& ...
- Linux命令之hexdump - ”十六“进制查看器
转载链接:http://codingstandards.iteye.com/blog/805778 用途说明 hexdump命令一般用来查看"二进制"文件的十六进制编码,但实际上它 ...
- Linux 驱动开发 四十六:Linux MISC驱动实验
misc 的意思是混合.杂项的,因此MISC 驱动也叫做杂项驱动,也就是当我们板子上的某些外设无法进行分类的时候就可以使用 MISC 驱动. MISC 驱动其实就是最简单的字符设备驱动,通常嵌套在 p ...
- Linux 驱动开发 五十六:《ioctl-number.txt》翻译
文档目录:linux-imx-4.1.15\Documentation\ioctl\ioctl-number.txt. 如果要向内核添加新的 ioctl,则应使用 <linux/ioctl.h& ...
- linux编译内核实验,实验六 Linux内核编译实验.doc
实验六 Linux内核编译 讲师:杨行 [实验目的] 1.掌握Linux内核编译 2.了解Linux内核Makefile 3.了解Linux内核Kbuild系统 [实验原理] 网站可以下载标准内核文件 ...
- linux 命令详解 十六
十七. xargs命令: 该命令的主要功能是从输入中构建和执行shell命令. 在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给e ...
- Linux内核分析 - 网络[十六]:TCP三次握手
内核:2.6.34 TCP是应用最广泛的传输层协议,其提供了面向连接的.可靠的字节流服务,但也正是因为这些特性,使得TCP较之UDP异常复杂,还是分两部分[创建与使用]来进行分析.这篇主要 ...
最新文章
- CALayer(二)
- python新闻推荐系统_python实现推荐系统(一)
- OpenCV周期性除噪滤波器
- 花里胡哨?一起来看看 PyCharm 2019.3 增加了哪些新功能吧
- MySQL下perror工具查看System Error Code信息
- Numpy数组的保存与读取方法
- memcpy和memmove的区别以及内存重叠问题
- 微信重大更新,mac版可刷朋友圈!可以看,可以评论! 支持M1
- Flask学习-Flask app接受第一个HTTP请求
- mpvue使用vant Weapp运行npm run build命令打包后失效
- 《小米网抢购系统开发实践》读后感
- C# 3.0新语言特性和改进
- flink写入 mysql_基于 Binlog + Flink 实现多表数据同构/异构方案
- VMWare共享文件夹的使用
- 定了!2021考研时间
- Apifiny任命FBI前高管Timothy Murphy为董事会成员,帮助公司完成上市计划
- 人民的名义1-55集全 已看完(观后感)
- 编程哲学-13条左右人生的金科玉律
- 第三十三篇,网络编程TCP协议通讯过程实现和函数接口
- matlab中用高斯-赛德尔(Gauss-Seidel)迭代法解线性方程组
热门文章
- 【理解springboot自动装配原理】
- 强烈推荐 10 款 Mac 软件!
- 消防cad图例_给排水、消防CAD图例符号大全与画法请君收藏!
- Troubleshooting Office Web Apps with SharePoint 2013
- js:Vue.js自定义指令实现scroll下滑滚动翻页
- Django教程:第一个Django应用程序(1部分)
- Java实验06-GUI文件加解密软件,ScrollPanel嵌套Boxlayout,DES分组加密算法,进度条控制
- [Unity框架] SimpleMiniForUnity 用于Unity单机游戏的迷你框架
- 杭电计算机研究生复试,杭电计算机考研经验
- 编程团体赛 python