Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294140
  • 博文数量: 44
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1354
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 15:38
个人简介

人生像是在跑马拉松,能够完赛的都是不断地坚持向前迈进;人生就是像在跑马拉松,不断调整步伐,把握好分分秒秒;人生还是像在跑马拉松,能力决定了能跑短程、半程还是全程。人生其实就是一场马拉松,坚持不懈,珍惜时间。

文章分类

分类: LINUX

2017-01-09 13:25:34

【注:往事不可追,也不知道是自己的编辑问题还是其他原因,整理博文的时候,发现有篇博文居然漏了,只好在此补上,该文延续《系统启动阶段的memblock算法(1)》的分析补充。难怪总觉得怪怪的,可愁死我这强迫症患者了,没法往回插入,只好作罢】
     结束完memblock算法初始化前的准备工作,回到memblock算法初始化及其算法实现上面。memblock是一个很简单的算法。

memblock算法的实现是,它将所有状态都保存在一个全局变量__initdata_memblock中,算法的初始化以及内存的申请释放都是在将内存块的状态做变更。那么从数据结构入手,__initdata_memblock是一个memblock结构体。其结构体定义:

  1. 【file:/include/linux/memblock.h】
  2. struct memblock {
  3.     bool bottom_up; /* is bottom up direction? */
  4.     phys_addr_t current_limit;
  5.     struct memblock_type memory;
  6.     struct memblock_type reserved;
  7. };

结构体内各成员的意思:

bottom_up:用来表示分配器分配内存是自低地址(低地址指的是内核映像尾部,下同)向高地址还是自高地址向低地址来分配的;

current_limit:用来表示用来限制memblock_alloc()memblock_alloc_base(..., MEMBLOCK_ALLOC_ACCESSIBLE)的内存申请;

memory:表示可用可分配的内存;

reserved:表示已经分配出去了的内存;                              

memory和reserved是很关键的一个数据结构,memblock算法的内存初始化和申请释放都是围绕着它们转。

往下看看memoryreserved的结构体memblock_type定义:

  1. 【file:/include/linux/memblock.h】
  2. struct memblock_type {
  3.     unsigned long cnt; /* number of regions */
  4.     unsigned long max; /* size of the allocated array */
  5.     phys_addr_t total_size; /* size of all regions */
  6.     struct memblock_region *regions;
  7. };

cnt和max分别表示当前状态(memory/reserved)的内存块可用数和可支持的最大数,total_size则表示当前状态(memory/reserved)的空间大小(也就是可用的内存块信息大小总和),而regions则是用于保存内存块信息的结构(包括基址、大小和标记等):

  1. 【file:/include/linux/memblock.h】
  2. struct memblock_region {
  3.     phys_addr_t base;
  4.     phys_addr_t size;
  5.     unsigned long flags;
  6. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  7.     int nid;
  8. #endif
  9. };

memblock算法的主要结构体也就这么多了,总的关系如图:

回去看看__initdata_memblock的定义:

  1. 【file:/mm/memblock.c】
  2. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  3. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  4.  
  5. struct memblock memblock __initdata_memblock = {
  6.     .memory.regions = memblock_memory_init_regions,
  7.     .memory.cnt = 1, /* empty dummy entry */
  8.     .memory.max = INIT_MEMBLOCK_REGIONS,
  9.  
  10.     .reserved.regions = memblock_reserved_init_regions,
  11.     .reserved.cnt = 1, /* empty dummy entry */
  12.     .reserved.max = INIT_MEMBLOCK_REGIONS,
  13.  
  14.     .bottom_up = false,
  15.     .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  16. };

它初始化了部分成员,表示内存申请自高地址向低地址,且current_limit设为~0,即0xFFFFFFFF,同时通过全局变量定义为memblock的算法管理中的memoryreserved准备了内存空间。

接下来分析一下memblock算法初始化,其初始化函数为memblock_x86_fill(),初始化调用栈位置:

start_kernel()                          #/init/main.c

└->setup_arch()                        #/arch/x86/kernel/setup.c

└->memblock_x86_fill()                #/arch/x86/kernel/e820.c

函数实现:

  1. 【file:/arch/x86/kernel/e820.c】
  2. void __init memblock_x86_fill(void)
  3. {
  4.     int i;
  5.     u64 end;
  6.  
  7.     /*
  8.      * EFI may have more than 128 entries
  9.      * We are safe to enable resizing, beause memblock_x86_fill()
  10.      * is rather later for x86
  11.      */
  12.     memblock_allow_resize();
  13.  
  14.     for (i = 0; i < e820.nr_map; i++) {
  15.         struct e820entry *ei = &e820.map[i];
  16.  
  17.         end = ei->addr + ei->size;
  18.         if (end != (resource_size_t)end)
  19.             continue;
  20.  
  21.         if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  22.             continue;
  23.  
  24.         memblock_add(ei->addr, ei->size);
  25.     }
  26.  
  27.     /* throw away partial pages */
  28.     memblock_trim_memory(PAGE_SIZE);
  29.  
  30.     memblock_dump_all();
  31. }

该函数的实现中,调用了memblock_allow_resize() 仅是用于置memblock_can_resize的值;里面的for则是用于循环遍历e820的内存布局信息,将信息做memblock_add的操作;最后循环退出后,将调用memblock_trim_memory()memblock_dump_all()做后处理。这里首先看一下memblock_add()的函数实现:

  1. 【file:/mm/memblock.c】
  2. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  3. {
  4.     return memblock_add_region(&memblock.memory, base, size,
  5.                    MAX_NUMNODES, 0);
  6. }

memblock_add()主要封装了memblock_add_region(),特别需要留意它操作对象是memblock.memory(可用可分配的内存),可以推测其意图是将e820的内存信息往这里添加,接着往下看memblock_add_region()的实现:

  1. 【file:/mm/memblock.c】
  2. /**
  3.  * memblock_add_region - add new memblock region
  4.  * @type: memblock type to add new region into
  5.  * @base: base address of the new region
  6.  * @size: size of the new region
  7.  * @nid: nid of the new region
  8.  * @flags: flags of the new region
  9.  *
  10.  * Add new memblock region [@base,@base+@size) into @type. The new region
  11.  * is allowed to overlap with existing ones - overlaps don't affect already
  12.  * existing regions. @type is guaranteed to be minimal (all neighbouring
  13.  * compatible regions are merged) after the addition.
  14.  *
  15.  * RETURNS:
  16.  * 0 on success, -errno on failure.
  17.  */
  18. static int __init_memblock memblock_add_region(struct memblock_type *type,
  19.                 phys_addr_t base, phys_addr_t size,
  20.                 int nid, unsigned long flags)
  21. {
  22.     bool insert = false;
  23.     phys_addr_t obase = base;
  24.     phys_addr_t end = base + memblock_cap_size(base, &size);
  25.     int i, nr_new;
  26.  
  27.     if (!size)
  28.         return 0;
  29.  
  30.     /* special case for empty array */
  31.     if (type->regions[0].size == 0) {
  32.         WARN_ON(type->cnt != 1 || type->total_size);
  33.         type->regions[0].base = base;
  34.         type->regions[0].size = size;
  35.         type->regions[0].flags = flags;
  36.         memblock_set_region_node(&type->regions[0], nid);
  37.         type->total_size = size;
  38.         return 0;
  39.     }
  40. repeat:
  41.     /*
  42.      * The following is executed twice. Once with %false @insert and
  43.      * then with %true. The first counts the number of regions needed
  44.      * to accomodate the new area. The second actually inserts them.
  45.      */
  46.     base = obase;
  47.     nr_new = 0;
  48.  
  49.     for (i = 0; i < type->cnt; i++) {
  50.         struct memblock_region *rgn = &type->regions[i];
  51.         phys_addr_t rbase = rgn->base;
  52.         phys_addr_t rend = rbase + rgn->size;
  53.  
  54.         if (rbase >= end)
  55.             break;
  56.         if (rend <= base)
  57.             continue;
  58.         /*
  59.          * @rgn overlaps. If it separates the lower part of new
  60.          * area, insert that portion.
  61.          */
  62.         if (rbase > base) {
  63.             nr_new++;
  64.             if (insert)
  65.                 memblock_insert_region(type, i++, base,
  66.                                rbase - base, nid,
  67.                                flags);
  68.         }
  69.         /* area below @rend is dealt with, forget about it */
  70.         base = min(rend, end);
  71.     }
  72.  
  73.     /* insert the remaining portion */
  74.     if (base < end) {
  75.         nr_new++;
  76.         if (insert)
  77.             memblock_insert_region(type, i, base, end - base,
  78.                            nid, flags);
  79.     }
  80.  
  81.     /*
  82.      * If this was the first round, resize array and repeat for actual
  83.      * insertions; otherwise, merge and return.
  84.      */
  85.     if (!insert) {
  86.         while (type->cnt + nr_new > type->max)
  87.             if (memblock_double_array(type, obase, size) < 0)
  88.                 return -ENOMEM;
  89.         insert = true;
  90.         goto repeat;
  91.     } else {
  92.         memblock_merge_regions(type);
  93.         return 0;
  94.     }
  95. }

分析一下memblock_add_region()函数的行为流程:

1、 如果memblock算法管理内存为空的时候,则将当前空间添加进去;

2、 不为空的情况下,则先检查是否存在内存重叠的情况,如果有的话,则剔除重叠部分,然后将其余非重叠的部分添加进去;

3、 如果出现region[]数组空间不够的情况,则通过memblock_double_array()添加新的region[]空间;

4、 最后通过memblock_merge_regions()把紧挨着的内存合并了。

现在很明了,可以看到其功能作用是把e820图里面的内存布局转换到memblock管理算法当中的memblock.memory进行管理,表示该内存可用。

接着回到memblock_x86_fill()退出for循环的两个后处理函数memblock_trim_memory()memblock_dump_all(),其中memblock_trim_memory()的实现:

  1. 【file:/mm/memblock.c】
  2. void __init_memblock memblock_trim_memory(phys_addr_t align)
  3. {
  4.     int i;
  5.     phys_addr_t start, end, orig_start, orig_end;
  6.     struct memblock_type *mem = &memblock.memory;
  7.  
  8.     for (i = 0; i < mem->cnt; i++) {
  9.         orig_start = mem->regions[i].base;
  10.         orig_end = mem->regions[i].base + mem->regions[i].size;
  11.         start = round_up(orig_start, align);
  12.         end = round_down(orig_end, align);
  13.  
  14.         if (start == orig_start && end == orig_end)
  15.             continue;
  16.  
  17.         if (start < end) {
  18.             mem->regions[i].base = start;
  19.             mem->regions[i].size = end - start;
  20.         } else {
  21.             memblock_remove_region(mem, i);
  22.             i--;
  23.         }
  24.     }
  25. }

该函数主要用于对memblock.memory做修整,剔除不对齐的部分。而最后memblock_dump_all则是将整理的信息做dump输出,这里就不分析了。

至此memblock内存管理算是初始化完毕了。接下来看一下算法的内存申请和释放的,memblock算法下的内存申请和释放的接口分别为:

memblock_alloc()和memblock_free()

memblock_alloc()的函数实现(入参为size大小和align用于字节对齐):

  1. 【file:/mm/memblock.c】
  2. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  3. {
  4.     return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  5. }

加上标示MEMBLOCK_ALLOC_ACCESSIBLE表示申请内存可访问,封装调用memblock_alloc_base()

  1. 【file:/mm/memblock.c】
  2. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  3. {
  4.     phys_addr_t alloc;
  5.  
  6.     alloc = __memblock_alloc_base(size, align, max_addr);
  7.  
  8.     if (alloc == 0)
  9.         panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  10.               (unsigned long long) size, (unsigned long long) max_addr);
  11.  
  12.     return alloc;
  13. }

继续__memblock_alloc_base()(封装了memblock_alloc_base_nid(),新增NUMA_NO_NODE入参表示无NUMA的节点,毕竟当前还没初始化到那一步):

  1. 【file:/mm/memblock.c】
  2. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  3. {
  4.     return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE);
  5. }

继续memblock_alloc_base_nid()

  1. 【file:/mm/memblock.c】
  2. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  3.                     phys_addr_t align, phys_addr_t max_addr,
  4.                     int nid)
  5. {
  6.     phys_addr_t found;
  7.  
  8.     if (!align)
  9.         align = SMP_CACHE_BYTES;
  10.  
  11.     found = memblock_find_in_range_node(size, align, 0, max_addr, nid);
  12.     if (found && !memblock_reserve(found, size))
  13.         return found;
  14.  
  15.     return 0;
  16. }

这里主要留意两个关键函数memblock_find_in_range_node()memblock_reserve()

先看一下memblock_find_in_range_node()的实现:

  1. 【file:/mm/memblock.c】
  2. /**
  3.  * memblock_find_in_range_node - find free area in given range and node
  4.  * @size: size of free area to find
  5.  * @align: alignment of free area to find
  6.  * @start: start of candidate range
  7.  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  8.  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  9.  *
  10.  * Find @size free area aligned to @align in the specified range and node.
  11.  *
  12.  * When allocation direction is bottom-up, the @start should be greater
  13.  * than the end of the kernel image. Otherwise, it will be trimmed. The
  14.  * reason is that we want the bottom-up allocation just near the kernel
  15.  * image so it is highly likely that the allocated memory and the kernel
  16.  * will reside in the same node.
  17.  *
  18.  * If bottom-up allocation failed, will try to allocate memory top-down.
  19.  *
  20.  * RETURNS:
  21.  * Found address on success, 0 on failure.
  22.  */
  23. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
  24.                     phys_addr_t align, phys_addr_t start,
  25.                     phys_addr_t end, int nid)
  26. {
  27.     int ret;
  28.     phys_addr_t kernel_end;
  29.  
  30.     /* pump up @end */
  31.     if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  32.         end = memblock.current_limit;
  33.  
  34.     /* avoid allocating the first page */
  35.     start = max_t(phys_addr_t, start, PAGE_SIZE);
  36.     end = max(start, end);
  37.     kernel_end = __pa_symbol(_end);
  38.  
  39.     /*
  40.      * try bottom-up allocation only when bottom-up mode
  41.      * is set and @end is above the kernel image.
  42.      */
  43.     if (memblock_bottom_up() && end > kernel_end) {
  44.         phys_addr_t bottom_up_start;
  45.  
  46.         /* make sure we will allocate above the kernel */
  47.         bottom_up_start = max(start, kernel_end);
  48.  
  49.         /* ok, try bottom-up allocation first */
  50.         ret = __memblock_find_range_bottom_up(bottom_up_start, end,
  51.                               size, align, nid);
  52.         if (ret)
  53.             return ret;
  54.  
  55.         /*
  56.          * we always limit bottom-up allocation above the kernel,
  57.          * but top-down allocation doesn't have the limit, so
  58.          * retrying top-down allocation may succeed when bottom-up
  59.          * allocation failed.
  60.          *
  61.          * bottom-up allocation is expected to be fail very rarely,
  62.          * so we use WARN_ONCE() here to see the stack trace if
  63.          * fail happens.
  64.          */
  65.         WARN_ONCE(1, "memblock: bottom-up allocation failed, "
  66.                  "memory hotunplug may be affected\n");
  67.     }
  68.  
  69.     return __memblock_find_range_top_down(start, end, size, align, nid);
  70. }

粗略讲解一下,判断end的范围,从前面调用关系跟下来,end其实就是MEMBLOCK_ALLOC_ACCESSIBLE,由此一来,将会设置为memblock.current_limit。紧接着对start做调整,为的是避免申请到第一个页面。memblock_bottom_up()返回的是memblock.bottom_up,前面初始化的时候也知道这个值是false(这不是一定的,在numa初始化时会设置为true),所以最后应该调用的是__memblock_find_range_top_down()去查找内存。看一下__memblock_find_range_top_down()的实现:

  1. 【file:/mm/memblock.c】
  2. /**
  3.  * __memblock_find_range_top_down - find free area utility, in top-down
  4.  * @start: start of candidate range
  5.  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  6.  * @size: size of free area to find
  7.  * @align: alignment of free area to find
  8.  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  9.  *
  10.  * Utility called from memblock_find_in_range_node(), find free area top-down.
  11.  *
  12.  * RETURNS:
  13.  * Found address on success, 0 on failure.
  14.  */
  15. static phys_addr_t __init_memblock
  16. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  17.                    phys_addr_t size, phys_addr_t align, int nid)
  18. {
  19.     phys_addr_t this_start, this_end, cand;
  20.     u64 i;
  21.  
  22.     for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  23.         this_start = clamp(this_start, start, end);
  24.         this_end = clamp(this_end, start, end);
  25.  
  26.         if (this_end < size)
  27.             continue;
  28.  
  29.         cand = round_down(this_end - size, align);
  30.         if (cand >= this_start)
  31.             return cand;
  32.     }
  33.  
  34.     return 0;
  35. }

__memblock_find_range_top_down()通过使用for_each_free_mem_range_reverse宏封装调用__next_free_mem_range_rev()函数,此函数逐一将memblock.memory里面的内存块信息提取出来与memblock.reserved的各项信息进行检验,确保返回的this_startthis_end不会与reserved的内存存在交叉重叠的情况。然后通过clamp取中间值,判断大小是否满足,满足的情况下,将自末端向前(因为这是top-down申请方式)的size大小的空间的起始地址(前提该地址不会超出this_start)返回回去。至此满足要求的内存块算是找到了。

多说一些,其实__memblock_find_range_bottom_up()__memblock_find_range_top_down()的查找内存实现是完全类似的,仅在down-toptop-down上面存在差异罢了。

既然满足条件的内存块找到了,那么回到memblock_alloc_base_nid()调用的另一个关键函数memblock_reserve()

  1. 【file:/mm/memblock.c】
  2. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  3. {
  4.     return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
  5. }

接着看一下memblock_reserve_region()

  1. 【file:/mm/memblock.c】
  2. static int __init_memblock memblock_reserve_region(phys_addr_t base,
  3.                            phys_addr_t size,
  4.                            int nid,
  5.                            unsigned long flags)
  6. {
  7.     struct memblock_type *_rgn = &memblock.reserved;
  8.  
  9.     memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
  10.              (unsigned long long)base,
  11.              (unsigned long long)base + size - 1,
  12.              flags, (void *)_RET_IP_);
  13.  
  14.     return memblock_add_region(_rgn, base, size, nid, flags);
  15. }

可以看到memblock_reserve_region()是通过memblock_add_region()函数往memblock.reserved里面添加内存块信息。

最后看看memblock算法的memblock_free()实现:

  1. 【file:/mm/memblock.c】
  2. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  3. {
  4.     memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  5.              (unsigned long long)base,
  6.              (unsigned long long)base + size - 1,
  7.              (void *)_RET_IP_);
  8.  
  9.     return __memblock_remove(&memblock.reserved, base, size);
  10. }

该函数主要封装了__memblock_remove()用于对memblock.reserved的操作。

接着看__memblock_remove()

  1. 【file:/mm/memblock.c】
  2. static int __init_memblock __memblock_remove(struct memblock_type *type,
  3.                          phys_addr_t base, phys_addr_t size)
  4. {
  5.     int start_rgn, end_rgn;
  6.     int i, ret;
  7.  
  8.     ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  9.     if (ret)
  10.         return ret;
  11.  
  12.     for (i = end_rgn - 1; i >= start_rgn; i--)
  13.         memblock_remove_region(type, i);
  14.     return 0;
  15. }

该函数主要调用两个关键函数memblock_isolate_range()memblock_remove_region()。先看一下memblock_isolate_range()

  1. 【file:/mm/memblock.c】
  2. /**
  3.  * memblock_isolate_range - isolate given range into disjoint memblocks
  4.  * @type: memblock type to isolate range for
  5.  * @base: base of range to isolate
  6.  * @size: size of range to isolate
  7.  * @start_rgn: out parameter for the start of isolated region
  8.  * @end_rgn: out parameter for the end of isolated region
  9.  *
  10.  * Walk @type and ensure that regions don't cross the boundaries defined by
  11.  * [@base,@base+@size). Crossing regions are split at the boundaries,
  12.  * which may create at most two more regions. The index of the first
  13.  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  14.  *
  15.  * RETURNS:
  16.  * 0 on success, -errno on failure.
  17.  */
  18. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  19.                     phys_addr_t base, phys_addr_t size,
  20.                     int *start_rgn, int *end_rgn)
  21. {
  22.     phys_addr_t end = base + memblock_cap_size(base, &size);
  23.     int i;
  24.  
  25.     *start_rgn = *end_rgn = 0;
  26.  
  27.     if (!size)
  28.         return 0;
  29.  
  30.     /* we'll create at most two more regions */
  31.     while (type->cnt + 2 > type->max)
  32.         if (memblock_double_array(type, base, size) < 0)
  33.             return -ENOMEM;
  34.  
  35.     for (i = 0; i < type->cnt; i++) {
  36.         struct memblock_region *rgn = &type->regions[i];
  37.         phys_addr_t rbase = rgn->base;
  38.         phys_addr_t rend = rbase + rgn->size;
  39.  
  40.         if (rbase >= end)
  41.             break;
  42.         if (rend <= base)
  43.             continue;
  44.  
  45.         if (rbase < base) {
  46.             /*
  47.              * @rgn intersects from below. Split and continue
  48.              * to process the next region - the new top half.
  49.              */
  50.             rgn->base = base;
  51.             rgn->size -= base - rbase;
  52.             type->total_size -= base - rbase;
  53.             memblock_insert_region(type, i, rbase, base - rbase,
  54.                            memblock_get_region_node(rgn),
  55.                            rgn->flags);
  56.         } else if (rend > end) {
  57.             /*
  58.              * @rgn intersects from above. Split and redo the
  59.              * current region - the new bottom half.
  60.              */
  61.             rgn->base = end;
  62.             rgn->size -= end - rbase;
  63.             type->total_size -= end - rbase;
  64.             memblock_insert_region(type, i--, rbase, end - rbase,
  65.                            memblock_get_region_node(rgn),
  66.                            rgn->flags);
  67.         } else {
  68.             /* @rgn is fully contained, record it */
  69.             if (!*end_rgn)
  70.                 *start_rgn = i;
  71.             *end_rgn = i + 1;
  72.         }
  73.     }
  74.  
  75.     return 0;
  76. }

可以看到memblock_isolate_range()主要是找到覆盖了指定的内存块的内存项的下标索引给找到并以出参返回回去。接着看memblock_remove_region的实现:

  1. 【file:/mm/memblock.c】
  2. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  3. {
  4.     type->total_size -= type->regions[r].size;
  5.     memmove(&type->regions[r], &type->regions[r + 1],
  6.         (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  7.     type->cnt--;
  8.  
  9.     /* Special case for empty arrays */
  10.     if (type->cnt == 0) {
  11.         WARN_ON(type->total_size != 0);
  12.         type->cnt = 1;
  13.         type->regions[0].base = 0;
  14.         type->regions[0].size = 0;
  15.         type->regions[0].flags = 0;
  16.         memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  17.     }
  18. }

其主要功能是将指定下标索引的内存项从memblock.reserved管理结构中移除。

两者结合起来,更容易理解。在__memblock_remove()里面,memblock_isolate_range()主要作用是基于被释放的内存信息将memblock.reserved划分为两段,将memblock.reserved覆盖了被释放的内存项自开始项到结束项的下标索引以start_rgnend_rgn返回回去。memblock_isolate_range()返回后,接着memblock_remove_region()则借助于start_rgnend_rgn把这几项从memblock.reserved管理结构中移除。至此内存释放完毕。

简单点做个小结:memblock管理算法将可用可分配的内存在memblock.memory进行管理起来,已分配的内存在memblock.reserved进行管理,只要内存块加入到memblock.reserved里面就表示该内存已经被申请占用了。所以有个关键点需要注意,内存申请的时候,仅是把被申请到的内存加入到memblock.reserved中,并不会在memblock.memory里面有相关的删除或改动的操作,这也就是为什么申请和释放的操作都集中在memblock.reserved的原因了。这个算法效率并不高,但是这是合理的,毕竟在初始化阶段没有那么多复杂的内存操作场景,甚至很多地方都是申请了内存做永久使用的。

阅读(4660) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~