我们在阅读源码时,函数功能可以分为两类:1. bootmem.c 2. page_alloc.c。

  1. bootmem.c是关于bootmem allocator的,上篇文章已经简述过。

  2. page_alloc.c是关于Memory Management subsystem的。

  关于内存管理子系统的初始化调用了多个函数,我们首先分析在bootmem_init中调用的add_active_range函数。

  start_kernel --> setup_arch ---> arch_mem_init ---> bootmem_init ---> add_active_range.

  通过上述分析,我们此时可以确认内存信息,那么我们确认的内存信息必然要告诉Memory Management subsystem,首先通过add_active_range完成。

  调用两次add_active_range:

    1. add_active_range(0, start, end);     // start = 0, end = 57344

    2. add_active_range(0, start, end);     // start = 196608, end = 262144

/**
* add_active_range - Register a range of PFNs backed by physical memory 【注册物理内存的PFN范围】
* @nid: The node ID the range resides on
* @start_pfn: The start PFN of the available physical memory
* @end_pfn: The end PFN of the available physical memory
* @id :    该物理内存所属的内存结点。由于我们是UMA系统,只用一个内存结点,所以我们在调用时,传入的参数为0.
* @start_pfn: 可用物理内存的开始PFN号
* @end_pfn: 可用屋里内存的结束PFN号
* These ranges are stored in an early_node_map[] and later used by
* free_area_init_nodes() to calculate zone sizes and holes. If the
* range spans a memory hole, it is up to the architecture to ensure
* the memory is not freed by the bootmem allocator. If possible
* the range being registered will be merged with existing ranges.
* 传入的物理内存信息存储到一个数组中:early_node_map[], 稍后会在 free_area_init_nodes() 函数中使用,用来计算 zone sizes 和 holes。
* 如果物理内存的范围包含一个 memory hole,it is up to the architecture to ensure the memory is not freed by the bootmem allocator.
* 新注册的物理内存范围会Merge到已经存在的物理内存范围中去。
*/
void __init add_active_range(unsigned int nid, unsigned long start_pfn,
unsigned long end_pfn)
{
int i; mminit_dprintk(MMINIT_TRACE, "memory_register",
"Entering add_active_range(%d, %#lx, %#lx) "
"%d entries of %d used\n",
nid, start_pfn, end_pfn,
nr_nodemap_entries, MAX_ACTIVE_REGIONS); mminit_validate_memmodel_limits(&start_pfn, &end_pfn); // 对start_pfn 和 end_pfn 做某些验证,我们不Care /* Merge with existing active regions if possible */
for (i = ; i < nr_nodemap_entries; i++) {
if (early_node_map[i].nid != nid)
continue; /* Skip if an existing region covers this new one */
if (start_pfn >= early_node_map[i].start_pfn &&
end_pfn <= early_node_map[i].end_pfn)
return; /* Merge forward if suitable */
if (start_pfn <= early_node_map[i].end_pfn &&
end_pfn > early_node_map[i].end_pfn) {
early_node_map[i].end_pfn = end_pfn;
return;
} /* Merge backward if suitable */
if (start_pfn < early_node_map[i].start_pfn &&
end_pfn >= early_node_map[i].start_pfn) {
early_node_map[i].start_pfn = start_pfn;
return;
}
} /* Check that early_node_map is large enough */ // earlay_node_map数组有上限
if (i >= MAX_ACTIVE_REGIONS) {
printk(KERN_CRIT "More than %d memory regions, truncating\n",
MAX_ACTIVE_REGIONS);
return;
} early_node_map[i].nid = nid;
early_node_map[i].start_pfn = start_pfn;
early_node_map[i].end_pfn = end_pfn;
nr_nodemap_entries = i + ;
  /*
    early_node_map[0].nid = 0;
    early_node_map[0].start_pfn = 0;
    early_node_map[0].end_pfn = 57344;
    early_node_map[1].nid = 0;
    early_node_map[1].start_pfn = 196608;
    early_node_map[1].end_pfn = 262144;     很好理解,不啰嗦了!!!
  */
}

  总结:我们在从command_line中确定了屋里内存信息后,这是第一个把这个好消息告诉了buddy system。所以,一定要记住这个关键的数组——>early_node_map.

内存管理初始化源码4:add_active_range的更多相关文章

  1. 内存管理初始化源码1:setup_arch

    源码声明:基于Linux kernel 3.08 1. 在kernel/arch/mips/kernel/head.S中会做一些特定硬件相关的初始化,然后会调用内核启动函数:start_kernel: ...

  2. 内存管理初始化源码2:setup_arch

    PFN相关宏说明: /* kernel/include/linux/pfn.h */ PFN : Page Frame Number(物理页帧) /* * PFN_ALIGN:返回地址x所在那一页帧的 ...

  3. 内存管理初始化源码3:bootmem

    start_kernel ——> setup_arch ——> arch_mem_init ——> bootmem_init ——> init_bootmem_node: 此时 ...

  4. 内存管理初始化源码5:free_area_init_nodes

    start_kernel ——> setup_arch ——> arch_mem_init ——> |——> bootmem_init  |——> device_tree ...

  5. C++动态内存管理与源码剖析

    引言 在本篇文章中,我们主要剖析c++中的动态内存管理,包括malloc.new expression.operator new.array new和allocator内存分配方法以及对应的内存释放方 ...

  6. Jedis cluster集群初始化源码剖析

    Jedis cluster集群初始化源码剖析 环境 jar版本: spring-data-redis-1.8.4-RELEASE.jar.jedis-2.9.0.jar 测试环境: Redis 3.2 ...

  7. C#共享内存实例 附源码

    原文 C#共享内存实例 附源码 网上有C#共享内存类,不过功能太简单了,并且写内存每次都从开头写.故对此进行了改进,并做了个小例子,供需要的人参考. 主要改进点: 通过利用共享内存的一部分空间(以下称 ...

  8. Firefly卡牌手游《暗黑世界V1.5》服务器端源码+GM管理后台源码

    http://www.9miao.com/content-6-304.html Firefly卡牌手游<暗黑世界V1.5>服务器端源码+GM管理后台源码 关于<暗黑世界V1.5> ...

  9. 内存管理 初始化(八) 至kswapd_init

    至此,内存初始化部分已看完,遗留问题: 1.对于unicore或者mips的页表建立都很清楚,但是对于ARM我不清楚: 初始化部分涉及的页表映射建立,我都以unicore架构为准,ARM的页表映射从原 ...

随机推荐

  1. Mybatis中List传值

    Mybatis遇到List传值时常用方法. 1. List<E>,E为基本数据类型和String Mapper类文件: List<User> selectByIds(List& ...

  2. eclipse及idea使用问题记录

    使用eclipse或idea的时候会遇到各式各样的小问题,解决方案其实网上也大都搜得到,但是下次遇到的时候总是想不起来如何解决,还要花费时间再次查资料.所以以后把遇到的问题都记录一下. Eclipse ...

  3. Jmeter 常用函数(18)- 详解 __isDefined

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.htm 作用 判断 Jmeter 变量是否存在,1 就 ...

  4. 超详细的阿里字节Spring面试技术点总结(建议收藏)

    前言 Spring作为现在最流行Java开发技术,其内部源码设计非常优秀. Spring这个词对于Java开发者想必不会陌生,可能你每天都在使用Spring,享受着Spring生态提供的服务.现在很多 ...

  5. centos iso镜像自动挂载

    vim /etc/fstab 添加如下行:/usr/ison/centos.iso /media/cdrom iso9660 defaults,loop 0 0

  6. cenos 7 mysql

    CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载 # wget http://dev.mysql.com/get/mysql-community- ...

  7. Netty之旅二:口口相传的高性能Netty到底是什么?

    高清思维导图原件(xmind/pdf/jpg)可以关注公众号:一枝花算不算浪漫 回复netty01即可. 前言 上一篇文章讲了NIO相关的知识点,相比于传统IO,NIO已经做得很优雅了,为什么我们还要 ...

  8. promise和async await的区别

    在项目中第一次遇到async await的这种异步写法,来搞懂它 项目场景 :点击登录按钮后执行的事件,先进行表单校验 this.$refs.loginFormRef.validate(element ...

  9. 【译】Introducing “Web Live Preview”

    如果你开发的应用有 UI,你可能经历开发->编译->测试->修改->编译->测试的循环.根据所使用的框架或技术,有些可以改善这一流程,比如 edit-and-contin ...

  10. centos6.8上安装部署 jhipster-registry

    必备环境:jdk8,git,maven 1.安装nodejs #由于采用编译的方式很容易出现一些意外的惊喜,所以我们这儿直接用yum命令安装 #1.查看nodejs版本(命令中不要加 -y 如果版本不 ...