【Linux 内核 内存管理】memblock 分配器编程接口 ② ( memblock_add_range 函数分析 | memblock_insert_region 函数分析 )
文章目录
一、memblock_add_range 函数原型分析
在 memblock_add
函数 中 , 调用 memblock_add_range
函数 插入了一块内存 ;
memblock_add_range
函数 原型如下 :
int __init_memblock memblock_add_range(struct memblock_type *type,
phys_addr_t base, phys_addr_t size,
int nid, unsigned long flags)
源码路径 : linux-4.12\mm\memblock.c#511
struct memblock_type *type
参数 表示 指向要添加到新区域的 内存块 类型 ;
phys_addr_t base
参数 表示 新加入的 内存块 的 起始地址 ;
phys_addr_t size
参数 表示 新加入 内存块 的 大小 ;
int nid
参数 表示 新区域的 nid , 指向 NUMA 结构 ;
unsigned long flags
参数 表示 新加入 内存块 的 标志位 ;
二、memblock_add_range 函数源码分析
内存块对比 : 在该函数中 , 调用了如下代码 , 其作用是 遍历 所有 " 内存块 " , 每次遍历 , 都将 " 新的内存区块 " 与 " 该内存块 " 进行对比 ;
for_each_memblock_type(type, rgn) {
phys_addr_t rbase = rgn->base;
phys_addr_t rend = rbase + rgn->size;
if (rbase >= end)
break;
if (rend <= base)
continue;
/*
* @rgn overlaps. If it separates the lower part of new
* area, insert that portion.
*/
if (rbase > base) {
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
WARN_ON(nid != memblock_get_region_node(rgn));
#endif
WARN_ON(flags != rgn->flags);
nr_new++;
if (insert)
memblock_insert_region(type, idx++, base,
rbase - base, nid,
flags);
}
/* area below @rend is dealt with, forget about it */
base = min(rend, end);
}
源码路径 : linux-4.12\mm\memblock.c#543
插入内存 : 假如 起始地址 , 小于 end 地址 , 则需要插入内存 , 调用 memblock_insert_region
函数 , 完成内存插入操作 ;
/* insert the remaining portion */
if (base < end) {
nr_new++;
if (insert)
memblock_insert_region(type, idx, base, end - base,
nid, flags);
}
源码路径 : linux-4.12\mm\memblock.c#571/font>
内存区块链表标志 : nr_new
参数表示 是否有新的 " 内存块 " 要加入到 " 内存区块链表 " 中 ;
if (!nr_new)
return 0;
三、memblock_insert_region 函数原型分析
memblock_insert_region
函数原型如下 :
struct memblock_type *type
参数 指针指向内存区
int idx
参数 表示 内存区链表索引
phys_addr_t base
参数表示 内存区 基地址
phys_addr_t size
参数 表示 内存区大小
int nid
参数 表示 内存节点 id
unsigned long flags
参数 表示 内存块标志位
/**
* memblock_insert_region - insert new memblock region
* @type: memblock type to insert into
* @idx: index for the insertion point
* @base: base address of the new region
* @size: size of the new region
* @nid: node id of the new region
* @flags: flags of the new region
*
* Insert new memblock region [@base,@base+@size) into @type at @idx.
* @type must already have extra room to accommodate the new region.
*/
static void __init_memblock memblock_insert_region(struct memblock_type *type,
int idx, phys_addr_t base,
phys_addr_t size,
int nid, unsigned long flags)
源码路径 : linux-4.12\mm\memblock.c#478
四、memblock_add_range 函数源码
memblock_add_range
函数 定义在 Linux 内核源码的 linux-4.12\mm\memblock.c#511 位置 ;
/**
* memblock_add_range - add new memblock region
* @type: memblock type to add new region into
* @base: base address of the new region
* @size: size of the new region
* @nid: nid of the new region
* @flags: flags of the new region
*
* Add new memblock region [@base,@base+@size) into @type. The new region
* is allowed to overlap with existing ones - overlaps don't affect already
* existing regions. @type is guaranteed to be minimal (all neighbouring
* compatible regions are merged) after the addition.
*
* RETURNS:
* 0 on success, -errno on failure.
*/
int __init_memblock memblock_add_range(struct memblock_type *type,
phys_addr_t base, phys_addr_t size,
int nid, unsigned long flags)
{
bool insert = false;
phys_addr_t obase = base;
phys_addr_t end = base + memblock_cap_size(base, &size);
int idx, nr_new;
struct memblock_region *rgn;
if (!size)
return 0;
/* special case for empty array */
if (type->regions[0].size == 0) {
WARN_ON(type->cnt != 1 || type->total_size);
type->regions[0].base = base;
type->regions[0].size = size;
type->regions[0].flags = flags;
memblock_set_region_node(&type->regions[0], nid);
type->total_size = size;
return 0;
}
repeat:
/*
* The following is executed twice. Once with %false @insert and
* then with %true. The first counts the number of regions needed
* to accommodate the new area. The second actually inserts them.
*/
base = obase;
nr_new = 0;
for_each_memblock_type(type, rgn) {
phys_addr_t rbase = rgn->base;
phys_addr_t rend = rbase + rgn->size;
if (rbase >= end)
break;
if (rend <= base)
continue;
/*
* @rgn overlaps. If it separates the lower part of new
* area, insert that portion.
*/
if (rbase > base) {
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
WARN_ON(nid != memblock_get_region_node(rgn));
#endif
WARN_ON(flags != rgn->flags);
nr_new++;
if (insert)
memblock_insert_region(type, idx++, base,
rbase - base, nid,
flags);
}
/* area below @rend is dealt with, forget about it */
base = min(rend, end);
}
/* insert the remaining portion */
if (base < end) {
nr_new++;
if (insert)
memblock_insert_region(type, idx, base, end - base,
nid, flags);
}
if (!nr_new)
return 0;
/*
* If this was the first round, resize array and repeat for actual
* insertions; otherwise, merge and return.
*/
if (!insert) {
while (type->cnt + nr_new > type->max)
if (memblock_double_array(type, obase, size) < 0)
return -ENOMEM;
insert = true;
goto repeat;
} else {
memblock_merge_regions(type);
return 0;
}
}
源码路径 : linux-4.12\mm\memblock.c#511
相关文章
- Linux系统禁用广告保护隐私(linux屏蔽广告)
- 探索Linux服务器版的下载之旅(linux服务器版下载)
- Linux脚本的运算符概览(linux脚本运算符)
- 驱动系统掌握:Linux内核I2C驱动开发技能(linux内核i2c)
- Linux内核的延时:分析与对策(linux内核延时)
- Linux服务器架构优化之分区实践(linux服务器分区)
- 优化Linux机器内存优化:从零到千(linux机器内存)
- Linux环境下快速安装SVN服务(linux下安装svn)
- 精妙绝伦:Linux内核设计之艺术(linux内核设计艺术)
- 系统基于Linux的视频点播系统实现(linux视频点播)
- Linux 登入记录:保护系统安全(linux登入日志)
- 禁用Linux防火墙:一步深入操作(关闭linux的防火墙)
- Linux浏览器:解锁高效的内核体验(linux浏览器内核)
- Linux下使用串口模式的快速指南(linux串口模式)
- Linux内核:从零开始剖析(linux内核完全剖析)
- Linux网络编程:从基础到实践(linux网络编程书籍)
- 深入探索:如何关闭Linux防火墙(关闭linux的防火墙)
- 一款强大的公共操作系统——Linux(25字)(公共的linux)
- Linux内核:从位置谈起(linux内核位置)
- 深入Linux:如何轻松切换内核(linux切换内核)
- Linux下启用无线网卡实现无线上网(linux启用无线网卡)
- 轻松了解Linux:快速查看磁盘空间使用情况(linux查看空间占用)
- Linux下快速安装ADB的指南(linux安装adb)
- Linux系统下进程流量监控的实现(linux 进程流量监控)
- Linux内存状态:洞悉你的系统情况(linux 内存状态)
- Linux 内核视频教程:掌握系统核心技术(linux 内核视频教程)
- Linux内核学习的最佳实践:探索之路(linux内核学习方法)
- Linux内存计算:提升效能的关键(linux内存计算)