Linux内存管理之伙伴系统

在内核初始化完成之后, 内存管理的责任就由伙伴系统来承担. 伙伴系统基于一种相对简单然而令人吃惊的强大算法.
linux内核使用二进制伙伴算法来管理和分配物理内存页面, 该算法由knowlton设计, 后来knuth又进行了更深刻的描述.
伙伴系统是一个结合了2的方幂个分配器和空闲缓冲区合并计技术的内存分配方案, 其基本思想很简单. 内存被分成含有很多页面的大块, 每一块都是2个页面大小的方幂. 如果找不到想要的块, 一个大块会被分成两部分, 这两部分彼此就成为伙伴. 其中一半被用来分配, 而另一半则空闲. 这些块在以后分配的过程中会继续被二分直至产生一个所需大小的块. 当一个块被最终释放时, 其伙伴将被检测出来, 如果伙伴也空闲则合并两者.
内核如何记住哪些内存块是空闲的
分配空闲页面的方法
影响分配器行为的众多标识位
内存碎片的问题和分配器如何处理碎片
伙伴系统的结构
伙伴系统数据结构
系统内存中的每个物理内存页(页帧),都对应于一个struct page实例, 每个内存域都关联了一个struct zone的实例,其中保存了用于管理伙伴数据的主要数数组
// http://lxr.free-electrons.com/source/include/linux/mmzone.h?v=4.7#l324struct zone{ /* free areas of different sizes */ struct free_area free_area[max_order];};struct free_area是一个伙伴系统的辅助数据结构.struct free_area { struct list_head free_list[migrate_types]; unsigned long nr_free;}; ​
伙伴系统的分配器维护空闲页面所组成的块, 这里每一块都是2的方幂个页面, 方幂的指数称为阶.
阶是伙伴系统中一个非常重要的术语. 它描述了内存分配的数量单位. 内存块的长度是2^0,order , 其中order的范围从0到max_order
zone->free_area[max_order]数组中阶作为各个元素的索引, 用于指定对应链表中的连续内存区包含多少个页帧.
数组中第0个元素的阶为0, 它的free_list链表域指向具有包含区为单页(2^0 = 1)的内存页面链表
数组中第1个元素的free_list域管理的内存区为两页(2^1 = 2)
第3个管理的内存区为4页, 依次类推.
直到 2^maxorder-1个页面大小的块

最大阶max_order与force_max_zoneorder配置选项
一般来说max_order默认定义为11, 这意味着一次分配可以请求的页数最大是2^11=2048.
/* free memory management - zoned buddy allocator. */#ifndef config_force_max_zoneorder#define max_order 11#else#define max_order config_force_max_zoneorder#endif#define max_order_nr_pages (1 << (max_order - 1)) 但如果特定于体系结构的代码设置了force_max_zoneorder配置选项, 该值也可以手工改变
例如,ia-64系统上巨大的地址空间可以处理max_order = 18的情形,而arm或v850系统则使用更小的值(如8或9). 但这不一定是由计算机支持的内存数量比较小引起的,也可能是内存对齐方式的要求所导致
可以参考一些架构的kconfig文件如下

比如arm64体系结构的kconfig配置文件的描述
config force_max_zoneorderintdefault 14 if (arm64_64k_pages && transparent_hugepage)default 12 if (arm64_16k_pages && transparent_hugepage)default 11 内存区是如何连接的
内存区中第1页内的链表元素, 可用于将内存区维持在链表中。因此,也不必引入新的数据结构来管理物理上连续的页,否则这些页不可能在同一内存区中. 如下图所示

伙伴不必是彼此连接的. 如果一个内存区在分配其间分解为两半, 内核会自动将未用的一半加入到对应的链表中.
如果在未来的某个时刻, 由于内存释放的缘故, 两个内存区都处于空闲状态, 可通过其地址判断其是否为伙伴. 管理工作较少, 是伙伴系统的一个主要优点.
基于伙伴系统的内存管理专注于某个结点的某个内存域, 例如, dma或高端内存域. 但所有内存域和结点的伙伴系统都通过备用分配列表连接起来.
下图说明了这种关系.

最后要注意, 有关伙伴系统和当前状态的信息可以在/proc/buddyinfo中获取

内核中很多时候要求分配连续页. 为快速检测内存中的连续区域, 内核采用了一种古老而历经检验的技术: 伙伴系统
系统中的空闲内存块总是两两分组, 每组中的两个内存块称作伙伴. 伙伴的分配可以是彼此独立的. 但如果两个伙伴都是空闲的, 内核会将其合并为一个更大的内存块, 作为下一层次上某个内存块的伙伴.
下图示范了该系统, 图中给出了一对伙伴, 初始大小均为8页. 即系统中所有的页面都是8页的.
内核对所有大小相同的伙伴(1、2、4、8、16或其他数目的页),都放置到同一个列表中管理. 各有8页的一对伙伴也在相应的列表中.
如果系统现在需要8个页帧, 则将16个页帧组成的块拆分为两个伙伴. 其中一块用于满足应用程序的请求, 而剩余的8个页帧则放置到对应8页大小内存块的列表中.
如果下一个请求只需要2个连续页帧, 则由8页组成的块会分裂成2个伙伴, 每个包含4个页帧. 其中一块放置回伙伴列表中,而另一个再次分裂成2个伙伴, 每个包含2页。其中一个回到伙伴系统,另一个则传递给应用程序.
在应用程序释放内存时, 内核可以直接检查地址, 来判断是否能够创建一组伙伴, 并合并为一个更大的内存块放回到伙伴列表中, 这刚好是内存块分裂的逆过程。这提高了较大内存块可用的可能性.
在系统长期运行时,服务器运行几个星期乃至几个月是很正常的,许多桌面系统也趋向于长期开机运行,那么会发生称为碎片的内存管理问题。频繁的分配和释放页帧可能导致一种情况:系统中有若干页帧是空闲的,但却散布在物理地址空间的各处。换句话说,系统中缺乏连续页帧组成的较大的内存块,而从性能上考虑,却又很需要使用较大的连续内存块。通过伙伴系统可以在某种程度上减少这种效应,但无法完全消除。如果在大块的连续内存中间刚好有一个页帧分配出去,很显然这两块空闲的内存是无法合并的.
在内核版本2.6.24之后, 增加了一些有效措施来防止内存碎片.
避免碎片
在第1章给出的简化说明中, 一个双链表即可满足伙伴系统的所有需求. 在内核版本2.6.23之前, 的确是这样. 但在内核2.6.24开发期间, 内核开发者对伙伴系统的争论持续了相当长时间. 这是因为伙伴系统是内核最值得尊敬的一部分,对它的改动不会被大家轻易接受
内存碎片
伙伴系统的基本原理已经在第1章中讨论过,其方案在最近几年间确实工作得非常好。但在linux内存管理方面,有一个长期存在的问题:在系统启动并长期运行后,物理内存会产生很多碎片。该情形如下图所示
假定内存由60页组成,这显然不是超级计算机,但用于示例却足够了。左侧的地址空间中散布着空闲页。尽管大约25%的物理内存仍然未分配,但最大的连续空闲区只有一页. 这对用户空间应用程序没有问题:其内存是通过页表映射的,无论空闲页在物理内存中的分布如何,应用程序看到的内存 似乎总是连续的。右图给出的情形中,空闲页和使用页的数目与左图相同,但所有空闲页都位于一个连续区中。
但对内核来说,碎片是一个问题. 由于(大多数)物理内存一致映射到地址空间的内核部分, 那么在左图的场景中, 无法映射比一页更大的内存区. 尽管许多时候内核都分配的是比较小的内存, 但也有时候需要分配多于一页的内存. 显而易见, 在分配较大内存的情况下, 右图中所有已分配页和空闲页都处于连续内存区的情形,是更为可取的.
很有趣的一点是, 在大部分内存仍然未分配时, 就也可能发生碎片问题. 考虑图3-25的情形.
只分配了4页,但可分配的最大连续区只有8页,因为伙伴系统所能工作的分配范围只能是2的幂次.
我提到内存碎片只涉及内核,这只是部分正确的。大多数现代cpu都提供了使用巨型页的可能性,比普通页大得多。这对内存使用密集的应用程序有好处。在使用更大的页时,地址转换后备缓冲器只需处理较少的项,降低了tlb缓存失效的可能性。但分配巨型页需要连续的空闲物理内存!
很长时间以来,物理内存的碎片确实是linux的弱点之一。尽管已经提出了许多方法,但没有哪个方法能够既满足linux需要处理的各种类型工作负荷提出的苛刻需求,同时又对其他事务影响不大。
依据可移动性组织页
在内核2.6.24开发期间,防止碎片的方法最终加入内核。在我讨论具体策略之前,有一点需要澄清。
文件系统也有碎片,该领域的碎片问题主要通过碎片合并工具解决。它们分析文件系统,重新排序已分配存储块,从而建立较大的连续存储区. 理论上,该方法对物理内存也是可能的,但由于许多物理内存页不能移动到任意位置,阻碍了该方法的实施。因此,内核的方法是反碎片(anti-fragmentation), 即试图从最初开始尽可能防止碎片.
反碎片的工作原理如何?
为理解该方法,我们必须知道内核将已分配页划分为下面3种不同类型。
页面类型 描述 举例
不可移动页 在内存中有固定位置, 不能移动到其他地方. 核心内核分配的大多数内存属于该类别
可移动页 可以随意地移动. 属于用户空间应用程序的页属于该类别. 它们是通过页表映射的
如果它们复制到新位置,页表项可以相应地更新,应用程序不会注意到任何事
可回收页 不能直接移动, 但可以删除, 其内容可以从某些源重新生成. 例如,映射自文件的数据属于该类别
kswapd守护进程会根据可回收页访问的频繁程度,周期性释放此类内存. , 页面回收本身就是一个复杂的过程. 内核会在可回收页占据了太多内存时进行回收, 在内存短缺(即分配失败)时也可以发起页面回收.
页的可移动性,依赖该页属于3种类别的哪一种. 内核使用的反碎片技术, 即基于将具有相同可移动性的页分组的思想.
为什么这种方法有助于减少碎片?
由于页无法移动, 导致在原本几乎全空的内存区中无法进行连续分配. 根据页的可移动性, 将其分配到不同的列表中, 即可防止这种情形. 例如, 不可移动的页不能位于可移动内存区的中间, 否则就无法从该内存区分配较大的连续内存块.
想一下, 上图中大多数空闲页都属于可回收的类别, 而分配的页则是不可移动的. 如果这些页聚集到两个不同的列表中, 如下图所示. 在不可移动页中仍然难以找到较大的连续空闲空间, 但对可回收的页, 就容易多了.

但要注意, 从最初开始, 内存并未划分为可移动性不同的区. 这些是在运行时形成的. 内核的另一种方法确实将内存分区, 分别用于可移动页和不可移动页的分配, 我会下文讨论其工作原理. 但这种划分对这里描述的方法是不必要的
避免碎片数据结构
enum { migrate_unmovable, migrate_movable, migrate_reclaimable, migrate_pcptypes, /* the number of types on the pcp lists */ migrate_highatomic = migrate_pcptypes,#ifdef config_cma /* * migrate_cma migration type is designed to mimic the way * zone_movable works. only movable pages can be allocated * from migrate_cma pageblocks and page allocator never * implicitly change migration type of migrate_cma pageblock. * * the way to use it is to change migratetype of a range of * pageblocks to migrate_cma which can be done by * __free_pageblock_cma() function. what is important though * is that a range of pageblocks must be aligned to * max_order_nr_pages should biggest page be bigger then * a single pageblock. */ migrate_cma,#endif#ifdef config_memory_isolation migrate_isolate, /* can't allocate from here */#endif migrate_types}; 宏 类型
migrate_unmovable 不可移动页
migrate_movable 可移动页
migrate_reclaimable 可回收页
migrate_pcptypes 是per_cpu_pageset, 即用来表示每cpu页框高速缓存的数据结构中的链表的迁移类型数目
migrate_highatomic 在罕见的情况下,内核需要分配一个高阶的页面块而不能休眠.如果向具有特定可移动性的列表请求分配内存失败,这种紧急情况下可从migrate_highatomic中分配内存
migrate_cma linux内核最新的连续内存分配器(cma), 用于避免预留大块内存
migrate_isolate 是一个特殊的虚拟区域, 用于跨越numa结点移动物理内存页. 在大型系统上, 它有益于将物理内存页移动到接近于使用该页最频繁的cpu.
migrate_types
对于migrate_cma类型, 其中在我们使用arm等嵌入式linux系统的时候, 一个头疼的问题是gpu, camera, hdmi等都需要预留大量连续内存,这部分内存平时不用,但是一般的做法又必须先预留着. 目前, marek szyprowski和michal nazarewicz实现了一套全新的contiguous memory allocator. 通过这套机制, 我们可以做到不预留内存,这些内存平时是可用的,只有当需要的时候才被分配给camera,hdmi等设备. 内核为此提供了函数is_migrate_cma来检测当前类型是否为migrate_cma, 该函数定义在include/linux/mmzone.h?v=4.7, line 69
/* in mm/page_alloc.c; keep in sync also with show_migration_types() there */extern char * const migratetype_names[migrate_types];#ifdef config_cma# define is_migrate_cma(migratetype) unlikely((migratetype) == migrate_cma)#else# define is_migrate_cma(migratetype) false#endif 对伙伴系统数据结构的主要调整, 是将空闲列表分解为migrate_type个列表, 可以参见free_area的定义include/linux/mmzone.h?v=4.7, line 88
struct free_area{ struct list_head free_list[migrate_types]; unsigned long nr_free;};nr_free统计了所有列表上空闲页的数目,而每种迁移类型都对应于一个空闲列表宏for_each_migratetype_order(order, type)可用于迭代指定迁移类型的所有分配阶#define for_each_migratetype_order(order, type) \ for (order = 0; order < max_order; order++) \ for (type = 0; type < migrate_types; type++) 迁移备用列表fallbacks
如果内核无法满足针对某一给定迁移类型的分配请求, 会怎么样?
此前已经出现过一个类似的问题, 即特定的numa内存域无法满足分配请求时. 我们需要从其他内存域中选择一个代价最低的内存域完成内存的分配, 因此内核在内存的结点pg_data_t中提供了一个备用内存域列表zonelists.
内核在内存迁移的过程中处理这种情况下的做法是类似的. 提供了一个备用列表fallbacks, 规定了在指定列表中无法满足分配请求时. 接下来应使用哪一种迁移类型, 定义在mm/page_alloc.c?v=4.7, line 1799
/* * this array describes the order lists are fallen back to when * the free lists for the desirable migrate type are depleted * 该数组描述了指定迁移类型的空闲列表耗尽时 * 其他空闲列表在备用列表中的次序 */static int fallbacks[migrate_types][4] = { // 分配不可移动页失败的备用列表 [migrate_unmovable] = { migrate_reclaimable, migrate_movable, migrate_types }, // 分配可回收页失败时的备用列表 [migrate_reclaimable] = { migrate_unmovable, migrate_movable, migrate_types }, // 分配可移动页失败时的备用列表 [migrate_movable] = { migrate_reclaimable, migrate_unmovable, migrate_types },#ifdef config_cma [migrate_cma] = { migrate_types }, /* never used */#endif#ifdef config_memory_isolation [migrate_isolate] = { migrate_types }, /* never used */#endif}; 该数据结构大体上是自明的 : 每一行对应一个类型的备用搜索域的顺序, 在内核想要分配不可移动页migrate_unmovable时, 如果对应链表为空, 则遍历fallbacks[migrate_unmovable], 首先后退到可回收页链表migrate_reclaimable, 接下来到可移动页链表migrate_movable, 最后到紧急分配链表migrate_types.
pageblock_order变量
全局变量和辅助函数尽管页可移动性分组特性总是编译到内核中,但只有在系统中有足够内存可以分配到多个迁移类型对应的链表时,才是有意义的。由于每个迁移链表都应该有适当数量的内存,内核需要定义”适当”的概念. 这是通过两个全局变量pageblock_order和pageblock_nr_pages提供的. 第一个表示内核认为是”大”的一个分配阶, pageblock_nr_pages则表示该分配阶对应的页数。如果体系结构提供了巨型页机制, 则pageblock_order通常定义为巨型页对应的分配阶. 定义在include/linux/pageblock-flags.h?v=4.7, line 44
#ifdef config_hugetlb_page #ifdef config_hugetlb_page_size_variable /* huge page sizes are variable */ extern unsigned int pageblock_order; #else /* config_hugetlb_page_size_variable */ /* huge pages are a constant size */ #define pageblock_order hugetlb_page_order #endif /* config_hugetlb_page_size_variable */#else /* config_hugetlb_page */ /* if huge pages are not used, group by max_order_nr_pages */ #define pageblock_order (max_order-1)#endif /* config_hugetlb_page */#define pageblock_nr_pages (1ul << pageblock_order) 在ia-32体系结构上, 巨型页长度是4mb, 因此每个巨型页由1024个普通页组成, 而hugetlb_page_order则定义为10. 相比之下, ia-64体系结构允许设置可变的普通和巨型页长度, 因此hugetlb_page_order的值取决于内核配置.
如果体系结构不支持巨型页, 则将其定义为第二高的分配阶, 即max_order - 1
/* if huge pages are not used, group by max_order_nr_pages */#define pageblock_order (max_order-1) 如果各迁移类型的链表中没有一块较大的连续内存, 那么页面迁移不会提供任何好处, 因此在可用内存太少时内核会关闭该特性. 这是在build_all_zonelists函数中检查的, 该函数用于初始化内存域列表. 如果没有足够的内存可用, 则全局变量page_group_by_mobility_disabled设置为0, 否则设置为1.
内核如何知道给定的分配内存属于何种迁移类型?
我们将在以后讲解, 有关各个内存分配的细节都通过分配掩码指定.
内核提供了两个标志,分别用于表示分配的内存是可移动的(__gfp_movable)或可回收的(__gfp_reclaimable).
gfpflags_to_migratetype函数
/* convert gfp flags to their corresponding migrate type */static inline int allocflags_to_migratetype(gfp_t gfp_flags){ warn_on((gfp_flags & gfp_movable_mask) == gfp_movable_mask); if (unlikely(page_group_by_mobility_disabled)) return migrate_unmovable; /* group based on mobility */ return (((gfp_flags & __gfp_movable) != 0) struct zone{#ifndef config_sparsemem /* * flags for a pageblock_nr_pages block. see pageblock-flags.h. * in sparsemem, this map is stored in struct mem_section */ unsigned long *pageblock_flags;#endif /* config_sparsemem */}; 在初始化期间, 内核自动确保对内存域中的每个不同的迁移类型分组, 在pageblock_flags中都分配了足够存储nr_pageblock_bits个比特位的空间。当前,表示一个连续内存区的迁移类型需要3个比特位, 参见include/linux/pageblock-flags.h?v=4.7, line 28
/* bit indices that affect a whole block of pages */enum pageblock_bits { pb_migrate, pb_migrate_end = pb_migrate + 3 - 1, /* 3 bits required for migrate types */ pb_migrate_skip,/* if set the block is skipped by compaction */ /* * assume the bits will always align on a word. if this assumption * changes then get/set pageblock needs updating. */ nr_pageblock_bits}; 内核提供set_pageblock_migratetype负责设置以page为首的一个内存区的迁移类型, 该函数定义在mm/page_alloc.c?v=4.7, line 458, 如下所示
void set_pageblock_migratetype(struct page *page, int migratetype){ if (unlikely(page_group_by_mobility_disabled && migratetype < migrate_pcptypes)) migratetype = migrate_unmovable; set_pageblock_flags_group(page, (unsigned long)migratetype, pb_migrate, pb_migrate_end);} migratetype参数可以通过上文介绍的gfpflags_to_migratetype辅助函数构建. 请注意很重要的一点, 页的迁移类型是预先分配好的, 对应的比特位总是可用, 与页是否由伙伴系统管理无关. 在释放内存时,页必须返回到正确的迁移链表。这之所以可行,是因为能够从get_pageblock_migratetype获得所需的信息. 参见include/linux/mmzone.h?v=4.7, line 84
#define get_pageblock_migratetype(page) \ get_pfnblock_flags_mask(page, page_to_pfn(page), \ pb_migrate_end, migratetype_mask) 最后请注意, 在各个迁移链表之间, 当前的页面分配状态可以从/proc/pagetypeinfo获得.
初始化基于可移动性的分组
在内存子系统初始化期间, memmap_init_zone负责处理内存域的page实例. 该函数完成了一些不怎么有趣的标准初始化工作,但其中有一件是实质性的,即所有的页最初都标记为可移动的. 参见mm/page_alloc.c?v=4.7, line 5224
/* * initially all pages are reserved - free ones are freed * up by free_all_bootmem() once the early boot process is * done. non-atomic initialization, single-pass. */void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, unsigned long start_pfn, enum memmap_context context){ /* ...... */ for (pfn = start_pfn; pfn < end_pfn; pfn++) { /* ...... */not_early: if (!(pfn & (pageblock_nr_pages - 1))) { struct page *page = pfn_to_page(pfn); __init_single_page(page, pfn, zone, nid); set_pageblock_migratetype(page, migrate_movable); } else { __init_single_pfn(pfn, zone, nid); } }} 在分配内存时, 如果必须”盗取”不同于预定迁移类型的内存区, 内核在策略上倾向于”盗取”更大的内存区. 由于所有页最初都是可移动的, 那么在内核分配不可移动的内存区时, 则必须”盗取”.
实际上, 在启动期间分配可移动内存区的情况较少, 那么分配器有很高的几率分配长度最大的内存区, 并将其从可移动列表转换到不可移动列表. 由于分配的内存区长度是最大的, 因此不会向可移动内存中引入碎片.
总而言之, 这种做法避免了启动期间内核分配的内存(经常在系统的整个运行时间都不释放)散布到物理内存各处, 从而使其他类型的内存分配免受碎片的干扰,这也是页可移动性分组框架的最重要的目标之一.
分配器api
分配内存的接口
就伙伴系统的接口而言, numa或uma体系结构是没有差别的, 二者的调用语法都是相同的.
所有函数的一个共同点是 : 只能分配2的整数幂个页.
因此,接口中不像c标准库的malloc函数或bootmem和memblock分配器那样指定了所需内存大小作为参数. 相反, 必须指定的是分配阶, 伙伴系统将在内存中分配2^0 rder页. 内核中细粒度的分配只能借助于slab分配器(或者slub、slob分配器), 后者基于伙伴系统

在空闲内存无法满足请求以至于分配失败的情况下,所有上述函数都返回空指针(比如alloc_pages和alloc_page)或者0(比如get_zeroed_page、__get_free_pages和__get_free_page).
因此内核在各次分配之后都必须检查返回的结果. 这种惯例与设计得很好的用户层应用程序没什么不同, 但在内核中忽略检查会导致严重得多的故障
内核除了伙伴系统函数之外, 还提供了其他内存管理函数. 它们以伙伴系统为基础, 但并不属于伙伴分配器自身. 这些函数包括vmalloc和vmalloc_32, 使用页表将不连续的内存映射到内核地址空间中, 使之看上去是连续的.
还有一组kmalloc类型的函数, 用于分配小于一整页的内存区. 其实现.
释放函数
有4个函数用于释放不再使用的页,与所述函数稍有不同

分配掩码(gfp_mask标志)
分配掩码
前述所有函数中强制使用的mask参数,到底是什么语义?
我们知道linux将内存划分为内存域. 内核提供了所谓的内存域修饰符(zone modifier)(在掩码的最低4个比特位定义), 来指定从哪个内存域分配所需的页.
内核使用宏的方式定义了这些掩码, 一个掩码的定义被划分为3个部分进行定义, 我们会逐步展开来讲解, 参见include/linux/gfp.h?v=4.7, line 12~374, 共计26个掩码信息, 因此后面__gfp_bits_shift = 26.
掩码分类
linux中这些掩码标志gfp_mask分为3种类型 :
内核中掩码的定义
内核中的定义方式
// http://lxr.free-electrons.com/source/include/linux/gfp.h?v=4.7/* line 12 ~ line 44 第一部分 * 定义可掩码所在位的信息, 每个掩码对应一位为1 * 定义形式为 #define ___gfp_xxx 0x01u *//* plain integer gfp bitmasks. do not use this directly. */#define ___gfp_dma 0x01u#define ___gfp_highmem 0x02u#define ___gfp_dma32 0x04u#define ___gfp_movable 0x08u/* ...... *//* line 46 ~ line 192 第二部分 * 定义掩码和mask信息, 第二部分的某些宏可能是第一部分一个或者几个的组合 * 定义形式为 #define __gfp_xxx ((__force gfp_t)___gfp_xxx) */#define __gfp_dma ((__force gfp_t)___gfp_dma)#define __gfp_highmem ((__force gfp_t)___gfp_highmem)#define __gfp_dma32 ((__force gfp_t)___gfp_dma32)#define __gfp_movable ((__force gfp_t)___gfp_movable) /* zone_movable allowed */#define gfp_zonemask (__gfp_dma|__gfp_highmem|__gfp_dma32|__gfp_movable)/* line 194 ~ line 260 第三部分 * 定义掩码 * 定义形式为 #define gfp_xxx __gfp_xxx */#define gfp_dma __gfp_dma#define gfp_dma32 __gfp_dma32 其中gfp缩写的意思为获取空闲页(get free page), __gfp_movable不表示物理内存域, 但通知内核应在特殊的虚拟内存域zone_movable进行相应的分配.
定义掩码位
我们首先来看第一部分, 内核源代码中定义在include/linux/gfp.h?v=4.7, line 18 ~ line 44, 共计26个掩码信息.
/* plain integer gfp bitmasks. do not use this directly. */// 区域修饰符#define ___gfp_dma 0x01u#define ___gfp_highmem 0x02u#define ___gfp_dma32 0x04u// 行为修饰符#define ___gfp_movable 0x08u /* 页是可移动的 */#define ___gfp_reclaimable 0x10u /* 页是可回收的 */#define ___gfp_high 0x20u /* 应该访问紧急分配池? */#define ___gfp_io 0x40u /* 可以启动物理io? */#define ___gfp_fs 0x80u /* 可以调用底层文件系统? */#define ___gfp_cold 0x100u /* 需要非缓存的冷页 */#define ___gfp_nowarn 0x200u /* 禁止分配失败警告 */#define ___gfp_repeat 0x400u /* 重试分配,可能失败 */#define ___gfp_nofail 0x800u /* 一直重试,不会失败 */#define ___gfp_noretry 0x1000u /* 不重试,可能失败 */#define ___gfp_memalloc 0x2000u /* 使用紧急分配链表 */#define ___gfp_comp 0x4000u /* 增加复合页元数据 */#define ___gfp_zero 0x8000u /* 成功则返回填充字节0的页 */// 类型修饰符#define ___gfp_nomemalloc 0x10000u /* 不使用紧急分配链表 */#define ___gfp_hardwall 0x20000u /* 只允许在进程允许运行的cpu所关联的结点分配内存 */#define ___gfp_thisnode 0x40000u /* 没有备用结点,没有策略 */#define ___gfp_atomic 0x80000u /* 用于原子分配,在任何情况下都不能中断 */#define ___gfp_account 0x100000u#define ___gfp_notrack 0x200000u#define ___gfp_direct_reclaim 0x400000u#define ___gfp_other_node 0x800000u#define ___gfp_write 0x1000000u#define ___gfp_kswapd_reclaim 0x2000000u 定义掩码
然后第二部分, 相对而言每一个宏又被重新定义如下, 参见include/linux/gfp.h?v=4.7, line 46 ~ line 192
/** physical address zone modifiers (see linux/mmzone.h - low four bits)** do not put any conditional on these. if necessary modify the definitions* without the underscores and use them consistently. the definitions here may* be used in bit comparisons.* 定义区描述符*/#define __gfp_dma ((__force gfp_t)___gfp_dma)#define __gfp_highmem ((__force gfp_t)___gfp_highmem)#define __gfp_dma32 ((__force gfp_t)___gfp_dma32)#define __gfp_movable ((__force gfp_t)___gfp_movable) /* zone_movable allowed */#define gfp_zonemask (__gfp_dma|__gfp_highmem|__gfp_dma32|__gfp_movable)/** page mobility and placement hints** these flags provide hints about how mobile the page is. pages with similar* mobility are placed within the same pageblocks to minimise problems due* to external fragmentation.** __gfp_movable (also a zone modifier) indicates that the page can be* moved by page migration during memory compaction or can be reclaimed.** __gfp_reclaimable is used for slab allocations that specify* slab_reclaim_account and whose pages can be freed via shrinkers.** __gfp_write indicates the caller intends to dirty the page. where possible,* these pages will be spread between local zones to avoid all the dirty* pages being in one zone (fair zone allocation policy).** __gfp_hardwall enforces the cpuset memory allocation policy.** __gfp_thisnode forces the allocation to be satisified from the requested* node with no fallbacks or placement policy enforcements.** __gfp_account causes the allocation to be accounted to kmemcg (only relevant* to kmem allocations).*/#define __gfp_reclaimable ((__force gfp_t)___gfp_reclaimable)#define __gfp_write ((__force gfp_t)___gfp_write)#define __gfp_hardwall ((__force gfp_t)___gfp_hardwall)#define __gfp_thisnode ((__force gfp_t)___gfp_thisnode)#define __gfp_account ((__force gfp_t)___gfp_account)/** watermark modifiers -- controls access to emergency reserves** __gfp_high indicates that the caller is high-priority and that granting* the request is necessary before the system can make forward progress.* for example, creating an io context to clean pages.** __gfp_atomic indicates that the caller cannot reclaim or sleep and is* high priority. users are typically interrupt handlers. this may be* used in conjunction with __gfp_high * * __gfp_memalloc allows access to all memory. this should only be used when * the caller guarantees the allocation will allow more memory to be freed * very shortly e.g. process exiting or swapping. users either should * be the mm or co-ordinating closely with the vm (e.g. swap over nfs). * * __gfp_nomemalloc is used to explicitly forbid access to emergency reserves. * this takes precedence over the __gfp_memalloc flag if both are set. */#define __gfp_atomic ((__force gfp_t)___gfp_atomic)#define __gfp_high ((__force gfp_t)___gfp_high)#define __gfp_memalloc ((__force gfp_t)___gfp_memalloc)#define __gfp_nomemalloc ((__force gfp_t)___gfp_nomemalloc)/* * reclaim modifiers * * __gfp_io can start physical io. * * __gfp_fs can call down to the low-level fs. clearing the flag avoids the * allocator recursing into the filesystem which might already be holding * locks. * * __gfp_direct_reclaim indicates that the caller may enter direct reclaim. * this flag can be cleared to avoid unnecessary delays when a fallback * option is available. * * __gfp_kswapd_reclaim indicates that the caller wants to wake kswapd when * the low watermark is reached and have it reclaim pages until the high * watermark is reached. a caller may wish to clear this flag when fallback * options are available and the reclaim is likely to disrupt the system. the * canonical example is thp allocation where a fallback is cheap but * reclaim/compaction may cause indirect stalls. * * __gfp_reclaim is shorthand to allow/forbid both direct and kswapd reclaim. * * __gfp_repeat: try hard to allocate the memory, but the allocation attempt * _might_ fail. this depends upon the particular vm implementation. * * __gfp_nofail: the vm implementation _must_ retry infinitely: the caller * cannot handle allocation failures. new users should be evaluated carefully * (and the flag should be used only when there is no reasonable failure * policy) but it is definitely preferable to use the flag rather than * opencode endless loop around allocator. * * __gfp_noretry: the vm implementation must not retry indefinitely and will * return null when direct reclaim and memory compaction have failed to allow * the allocation to succeed. the oom killer is not called with the current * implementation. */#define __gfp_io ((__force gfp_t)___gfp_io)#define __gfp_fs ((__force gfp_t)___gfp_fs)#define __gfp_direct_reclaim ((__force gfp_t)___gfp_direct_reclaim) /* caller can reclaim */#define __gfp_kswapd_reclaim ((__force gfp_t)___gfp_kswapd_reclaim) /* kswapd can wake */#define __gfp_reclaim ((__force gfp_t)(___gfp_direct_reclaim|___gfp_kswapd_reclaim))#define __gfp_repeat ((__force gfp_t)___gfp_repeat)#define __gfp_nofail ((__force gfp_t)___gfp_nofail)#define __gfp_noretry ((__force gfp_t)___gfp_noretry)/* * action modifiers * * __gfp_cold indicates that the caller does not expect to be used in the near * future. where possible, a cache-cold page will be returned. * * __gfp_nowarn suppresses allocation failure reports. * * __gfp_comp address compound page metadata. * * __gfp_zero returns a zeroed page on success. * * __gfp_notrack avoids tracking with kmemcheck. * * __gfp_notrack_false_positive is an alias of __gfp_notrack. it's a means of * distinguishing in the source between false positives and allocations that * cannot be supported (e.g. page tables). * * __gfp_other_node is for allocations that are on a remote node but that * should not be accounted for as a remote allocation in vmstat. a * typical user would be khugepaged collapsing a huge page on a remote * node. */#define __gfp_cold ((__force gfp_t)___gfp_cold)#define __gfp_nowarn ((__force gfp_t)___gfp_nowarn)#define __gfp_comp ((__force gfp_t)___gfp_comp)#define __gfp_zero ((__force gfp_t)___gfp_zero)#define __gfp_notrack ((__force gfp_t)___gfp_notrack)#define __gfp_notrack_false_positive (__gfp_notrack)#define __gfp_other_node ((__force gfp_t)___gfp_other_node)/* room for n __gfp_foo bits */#define __gfp_bits_shift 26#define __gfp_bits_mask ((__force gfp_t)((1 << __gfp_bits_shift) - 1)) 给出的常数,其中一些很少使用,因此我不会讨论。其中最重要的一些常数语义如下所示
其中在开始的位置定义了对应的区修饰符,

其次还定义了我们程序和函数中所需要的掩码mask的信息, 由于其中__gfp_dma, __gfp_dma32, __gfp_highmem, __gfp_movable是在内存中分别有对应的内存域信息, 因此我们定义了内存域的掩码gfp_zonemask, 参见include/linux/gfp.h?v=4.7, line 57
#define gfp_zonemask (__gfp_dma|__gfp_highmem|__gfp_dma32|__gfp_movable) 接着内核定义了行为修饰符
/* __gfp_wait表示分配内存的请求可以中断。也就是说,调度器在该请求期间可随意选择另一个过程执行,或者该请求可以被另一个更重要的事件中断. 分配器还可以在返回内存之前, 在队列上等待一个事件(相关进程会进入睡眠状态).
虽然名字相似,但__gfp_high与__gfp_highmem毫无关系,请不要弄混这两者\
行为修饰符 描述
__gfp_reclaimable
__gfp_movable 是页迁移机制所需的标志. 顾名思义,它们分别将分配的内存标记为可回收的或可移动的。这影响从空闲列表的哪个子表获取内存
__gfp_write  
__gfp_hardwall 只在numa系统上有意义. 它限制只在分配到当前进程的各个cpu所关联的结点分配内存。如果进程允许在所有cpu上运行(默认情况),该标志是无意义的。只有进程可以运行的cpu受限时,该标志才有效果
__gfp_thisnode 也只在numa系统上有意义。如果设置该比特位,则内存分配失败的情况下不允许使用其他结点作为备用,需要保证在当前结点或者明确指定的结点上成功分配内存
__gfp_account  
__gfp_atomic  
__gfp_high 如果请求非常重要, 则设置__gfp_high,即内核急切地需要内存时。在分配内存失败可能给内核带来严重后果时(比如威胁到系统稳定性或系统崩溃), 总是会使用该标志
__gfp_memalloc  
__gfp_nomemalloc  
__gfp_io 说明在查找空闲内存期间内核可以进行i/o操作. 实际上, 这意味着如果内核在内存分配期间换出页, 那么仅当设置该标志时, 才能将选择的页写入硬盘
__gfp_fs 允许内核执行vfs操作. 在与vfs层有联系的内核子系统中必须禁用, 因为这可能引起循环递归调用.
__gfp_direct_reclaim  
__gfp_kswapd_reclaim  
__gfp_reclaim  
__gfp_repeat 在分配失败后自动重试,但在尝试若干次之后会停止
__gfp_nofail 在分配失败后一直重试,直至成功
__gfp_noretry 在分配失败后不重试,因此可能分配失败
__gfp_cold 如果需要分配不在cpu高速缓存中的“冷”页时,则设置__gfp_cold
__gfp_nowarn 在分配失败时禁止内核故障警告。在极少数场合该标志有用
__gfp_comp 添加混合页元素, 在hugetlb的代码内部使用
__gfp_zero 在分配成功时,将返回填充字节0的页
__gfp_notrack  
__gfp_notrack_false_positive
__gfp_notrack  
__gfp_other_node
那自然还有__gfp_bits_shift来表示我们所有的掩码位, 由于我们共计26个掩码位
/* room for n __gfp_foo bits */#define __gfp_bits_shift 26#define __gfp_bits_mask ((__force gfp_t)((1 < normal0x1 => dma or normal0x2 => highmem or normal0x3 => bad (dma+highmem)0x4 => dma32 or dma or normal0x5 => bad (dma+dma32)0x6 => bad (highmem+dma32)0x7 => bad (highmem+dma32+dma)0x8 => normal (movable+0)0x9 => dma or normal (movable+dma)0xa => movable (movable is valid only if highmem is set too)0xb => bad (movable+highmem+dma)0xc => dma32 (movable+dma32)0xd => bad (movable+dma32+dma)0xe => bad (movable+dma32+highmem)0xf => bad (movable+dma32+highmem+dma)gfp_zones_shift must be _zonerefs->zone)) return null; if (is_enabled(config_cma) && ac.migratetype == migrate_movable) alloc_flags |= alloc_cma;retry_cpuset: cpuset_mems_cookie = read_mems_allowed_begin(); /* dirty zone balancing only done in the fast path */ ac.spread_dirty_pages = (gfp_mask & __gfp_write); /* * the preferred zone is used for statistics but crucially it is * also used as the starting point for the zonelist iterator. it * may get reset for allocations that ignore memory policies. */ ac.preferred_zoneref = first_zones_zonelist(ac.zonelist, ac.high_zoneidx, ac.nodemask); if (!ac.preferred_zoneref) { page = null; goto no_zone; } /* first allocation attempt */ page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac); if (likely(page)) goto out; /* * runtime pm, block io and its error handling path can deadlock * because i/o on the device might not complete. */ alloc_mask = memalloc_noio_flags(gfp_mask); ac.spread_dirty_pages = false; /* * restore the original nodemask if it was potentially replaced with * &cpuset_current_mems_allowed to optimize the fast-path attempt. */ if (cpusets_enabled()) ac.nodemask = nodemask; page = __alloc_pages_slowpath(alloc_mask, order, &ac);no_zone: /* * when updating a task's mems_allowed, it is possible to race with * parallel threads in such a way that an allocation can fail while * the mask is being updated. if a page allocation is about to fail, * check if the cpuset changed during allocation and if so, retry. */ if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie))) { alloc_mask = gfp_mask; goto retry_cpuset; }out: if (kmemcheck_enabled && page) kmemcheck_pagealloc_alloc(page, order, gfp_mask); trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype); return page;}export_symbol(__alloc_pages_nodemask); __free_pages
类似地,内存释放函数也可以归约到一个主要的函数(__free_pages), 只是用不同的参数调用而已
前面我们讲过内核释放的两个主要函数有__free_page和free_page,
void free_pages(unsigned long addr, unsigned int order){ if (addr != 0) { vm_bug_on(!virt_addr_valid((void *)addr)); __free_pages(virt_to_page((void *)addr), order); }} free_pages和__free_pages之间的关系通过函数而不是宏建立, 因为首先必须将虚拟地址转换为指向struct page的指针
virt_to_page将虚拟内存地址转换为指向page实例的指针. 基本上, 这是讲解内存分配函数时介绍的page_address辅助函数的逆过程.
下图以图形化方式综述了各个内存释放函数之间的关系



灵犀微光将在今年完成规模产线的建设为AR设备提供核心光波导模组
电脑出现间歇性黑屏现象的原因及解决方法
挂脖风扇、小风扇取电方案
Intel加速存储芯片业务确保半导体市场地位
可充电的WiFi再曝光 这回是真的吗?
Linux内存管理之伙伴系统
PLC的调试步骤具体是怎样的
华为折叠屏手机Mate X2具体的细节 外翻和Galaxy Z差不多
高压电缆温度在线监测系统方案
智能AR泳镜Smart Swim AR开售 通过AR技术来颠覆水下体验
AT89C52单片机实现短距离无线传输的设计
直播预告 | @12/8 车用功能一把罩-NXP S32E/Z与您携手定义未来
数显糖度计的应用领域以及使用效果的详解
WT2605C-32N音频录音蓝牙芯片的详细介绍
DFRobot工程笔记本-黑色介绍
数字接口系列文章之SPI总线
云传物联室内硬池养殖水质检测控制器好用吗
什么是DDR SDRAM内存
PGA204/205开关选择可编程增益
如何制作实时USB