欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

【原创】(九)Linux内存管理 - zoned page frame allocator - 4

程序员文章站 2022-05-09 14:14:32
背景 By 鲁迅 By 高尔基 说明: 1. Kernel版本:4.14 2. ARM64处理器,Contex A53,双核 3. 使用工具:Source Insight 3.5, Visio 1. 概述 本文将描述 ,内存碎片整理技术。 内存碎片分为内碎片和外碎片: 内碎片:内存页里边的碎片; 外 ......

背景

  • read the fucking source code! --by 鲁迅
  • a picture is worth a thousand words. --by 高尔基

说明:

  1. kernel版本:4.14
  2. arm64处理器,contex-a53,双核
  3. 使用工具:source insight 3.5, visio

1. 概述

本文将描述memory compaction,内存碎片整理技术。
内存碎片分为内碎片和外碎片:

  • 内碎片:内存页里边的碎片;
  • 外碎片:内存页之间的碎片,可能会造成连续物理页面分配失败。

memory compaction就是通过将正在使用的可移动页面迁移到另一个地方以获得连续的空闲页面的方法。针对内存碎片,内核中定义了migrate_type用于描述迁移类型:

  • migrate_unmovable:不可移动,对应于内核分配的页面;
  • migrate_movable:可移动,对应于从用户空间分配的内存或文件;
  • migrate_reclaimable:不可移动,可以进行回收处理;

先来一张memory compaction的概况图:
【原创】(九)Linux内存管理 - zoned page frame allocator - 4

上图对应的是struct page的操作,而针对物理内存的操作如下图所示:
【原创】(九)Linux内存管理 - zoned page frame allocator - 4

在之前的文章中提到过pageblock,我们看到图中zone区域是以pageblock为单位上下扫描的,pageblock的大小定义如下(未使用huge table情况下),与buddy system管理中的最大块大小一致:

/* if huge pages are not used, group by max_order_nr_pages */
#define pageblock_order     (max_order-1)

#define pageblock_nr_pages  (1ul << pageblock_order)

好了,已经有一个初步印象了,那就进一步的分析吧。

1. 数据结构

1.1 compact_priority

/*
 * determines how hard direct compaction should try to succeed.
 * lower value means higher priority, analogically to reclaim priority.
 */
enum compact_priority {
    compact_prio_sync_full,
    min_compact_priority = compact_prio_sync_full,
    compact_prio_sync_light,
    min_compact_costly_priority = compact_prio_sync_light,
    def_compact_priority = compact_prio_sync_light,
    compact_prio_async,
    init_compact_priority = compact_prio_async
};

本结构用于描述memory compact的几种不同方式:

  • compact_prio_sync_full/min_compact_priority:最高优先级,压缩和迁移以同步的方式完成;
  • compact_prio_sync_light/min_compact_costly_priority/def_compact_priority:中优先级,压缩以同步方式处理,迁移以异步方式处理;
  • compact_prio_async/init_compact_priority:最低优先级,压缩和迁移以异步方式处理。

1.2 compact_result

本结构用于描述压缩处理函数的返回值:

/* return values for compact_zone() and try_to_compact_pages() */
/* when adding new states, please adjust include/trace/events/compaction.h */
enum compact_result {
    /* for more detailed tracepoint output - internal to compaction */
    compact_not_suitable_zone,
    /*
     * compaction didn't start as it was not possible or direct reclaim
     * was more suitable
     */
    compact_skipped,
    /* compaction didn't start as it was deferred due to past failures */
    compact_deferred,

    /* compaction not active last round */
    compact_inactive = compact_deferred,

    /* for more detailed tracepoint output - internal to compaction */
    compact_no_suitable_page,
    /* compaction should continue to another pageblock */
    compact_continue,

    /*
     * the full zone was compacted scanned but wasn't successfull to compact
     * suitable pages.
     */
    compact_complete,
    /*
     * direct compaction has scanned part of the zone but wasn't successfull
     * to compact suitable pages.
     */
    compact_partial_skipped,

    /* compaction terminated prematurely due to lock contentions */
    compact_contended,

    /*
     * direct compaction terminated after concluding that the allocation
     * should now succeed
     */
    compact_success,
};

1.3 migrate_mode

本结构用于描述migrate过程中的不同模式,主要针对同步和异步的处理。

/*
 * migrate_async means never block
 * migrate_sync_light in the current implementation means to allow blocking
 *  on most operations but not ->writepage as the potential stall time
 *  is too significant
 * migrate_sync will block when migrating pages
 * migrate_sync_no_copy will block when migrating pages but will not copy pages
 *  with the cpu. instead, page copy happens outside the migratepage()
 *  callback and is likely using a dma engine. see migrate_vma() and hmm
 *  (mm/hmm.c) for users of this mode.
 */
enum migrate_mode {
    migrate_async,
    migrate_sync_light,
    migrate_sync,
    migrate_sync_no_copy,
};

1.4 compact_control

compact_control结构体用于在执行compact的时候,维护两个扫描器,对应freepagesmigratepages,最终将migratepages中的页拷贝到freepages中去。具体的字段注释足够详尽,不细说了。

/*
 * compact_control is used to track pages being migrated and the free pages
 * they are being migrated to during memory compaction. the free_pfn starts
 * at the end of a zone and migrate_pfn begins at the start. movable pages
 * are moved to the end of a zone during a compaction run and the run
 * completes when free_pfn <= migrate_pfn
 */
struct compact_control {
    struct list_head freepages; /* list of free pages to migrate to */
    struct list_head migratepages;  /* list of pages being migrated */
    struct zone *zone;
    unsigned long nr_freepages; /* number of isolated free pages */
    unsigned long nr_migratepages;  /* number of pages to migrate */
    unsigned long total_migrate_scanned;
    unsigned long total_free_scanned;
    unsigned long free_pfn;     /* isolate_freepages search base */
    unsigned long migrate_pfn;  /* isolate_migratepages search base */
    unsigned long last_migrated_pfn;/* not yet flushed page being freed */
    const gfp_t gfp_mask;       /* gfp mask of a direct compactor */
    int order;          /* order a direct compactor needs */
    int migratetype;        /* migratetype of direct compactor */
    const unsigned int alloc_flags; /* alloc flags of a direct compactor */
    const int classzone_idx;    /* zone index of a direct compactor */
    enum migrate_mode mode;     /* async or sync migration mode */
    bool ignore_skip_hint;      /* scan blocks even if marked skip */
    bool ignore_block_suitable; /* scan blocks considered unsuitable */
    bool direct_compaction;     /* false from kcompactd or /proc/... */
    bool whole_zone;        /* whole zone should/has been scanned */
    bool contended;         /* signal lock or sched contention */
    bool finishing_block;       /* finishing current pageblock */
};

2. 调用流程

光看上文的数据结构,会比较零散,看看整体的流程吧。
在内核中,有三种方式来操作memory compact

  1. 在内存分配过程中,由于分配请求不能满足,直接触发内存compact处理;
  2. 在没有足够内存的情况下,kcompactd守护线程在后台唤醒,执行compact处理;
  3. 手动触发,通过echo 1 > /proc/sys/vm/compact_memory来触发;

图来了:
【原创】(九)Linux内存管理 - zoned page frame allocator - 4

实际操作一把:
cat /proc/pagetypeinfo如下图:
【原创】(九)Linux内存管理 - zoned page frame allocator - 4

3. compact处理

这个处理的过程还是很复杂的,下图显示了大概的过程:
【原创】(九)Linux内存管理 - zoned page frame allocator - 4

下边将针对各个子模块更深入点分析。

  • compaction_suitable
    【原创】(九)Linux内存管理 - zoned page frame allocator - 4

判断是否执行内存的碎片整理,需要满足以下三个条件:

  1. 除去申请的页面,空闲页面数将低于水印值,或者虽然大于等于水印值,但是没有一个足够大的空闲页块;
  2. 空闲页面减去两倍的申请页面(两倍表明有足够多的的空闲页面作为迁移目标),高于水印值;
  3. 申请的order大于page_alloc_costly_order时,计算碎片指数fragindex,根据值来判断;
  • isolate_migratepages
    isolate_migratepages函数中,迁移扫描器以pageblock为单位,扫描可移动页,最终把可移动的页添加到struct compact_control结构中的migratepages链表中。如下图所示:
    【原创】(九)Linux内存管理 - zoned page frame allocator - 4

isolate_freepages的逻辑与isolate_migratepages类似,也是对页进行隔离处理,最终添加cc->freepages链表中。

当空闲扫描器和迁移扫描器完成扫描之后,那就是时候将两个链表中的页做一下migrate操作了。

  • migrate_pages
  1. 调用compact_alloc函数,从cc->freepages链表中取出一个空闲页;
  2. 调用__unmap_and_move来把可移动页移动到空闲页处;
    _unmap_and_move函数涉及到反向映射,以及页缓存等,留在以后再深入看。这个函数两个关键作用:1)调用try_to_unmap删除进程页表中旧的映射关系,在需要访问的时候再重新映射到新的物理地址上;2)调用move_to_new_page函数将旧页移动到新的物理页上,其中在汇编文件arch/arm64/lib/copy_page.scopy_page函数完成拷贝。
  • compact_finished
    compact_finished函数主要用于检查compact是否完成。
    【原创】(九)Linux内存管理 - zoned page frame allocator - 4

  • compaction_deferred/compaction_defer_reset/defer_compaction
    上述这三个函数与内存碎片推迟compact有关,这三个函数是在try_to_compact_pages中调用。当free pages除去申请页面数高于水位值,且申请或备用的迁移类型至少有一个足够大的空闲页面时,可以认为compact成功。在没有成功时,可能需要推迟几次来处理。
    struct zone结构中与之有关的字段如下:
struct zone {
...
    /*
     * on compaction failure, 1<<compact_defer_shift compactions
     * are skipped before trying again. the number attempted since
     * last failure is tracked with compact_considered.
     */
    unsigned int        compact_considered; //记录推迟次数
    unsigned int        compact_defer_shift; //(1 << compact_defer_shift)=推迟次数,最大为6
    int                    compact_order_failed; //记录碎片整理失败时的申请order值
...
};

【原创】(九)Linux内存管理 - zoned page frame allocator - 4

【原创】(九)Linux内存管理 - zoned page frame allocator - 4