Linux-3.14.12内存管理笔记【构建内存管理框架(3)】

此处接前文,分析free_area_init_nodes()函数最后部分,分析其末尾的循环:

for_each_online_node(nid) {

    pg_data_t *pgdat = NODE_DATA(nid);

    free_area_init_node(nid, NULL,

            find_min_pfn_for_node(nid), NULL);

    /* Any memory on that node */

    if (pgdat->node_present_pages)

        node_set_state(nid, N_MEMORY);

    check_for_memory(pgdat, nid);

}

这里面的关键函数是free_area_init_node(),其入参find_min_pfn_for_node()用于获取node节点中最低的内存页框号。

而free_area_init_node()其实现:

【file:/mm/page_alloc.c】
void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
        unsigned long node_start_pfn, unsigned long *zholes_size)
{
    pg_data_t *pgdat = NODE_DATA(nid);
    unsigned long start_pfn = 0;
    unsigned long end_pfn = 0;
 
    /* pg_data_t should be reset to zero when it's allocated */
    WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
 
    pgdat->node_id = nid;
    pgdat->node_start_pfn = node_start_pfn;
    init_zone_allows_reclaim(nid);
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
    get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
#endif
    calculate_node_totalpages(pgdat, start_pfn, end_pfn,
                  zones_size, zholes_size);
 
    alloc_node_mem_map(pgdat);
#ifdef CONFIG_FLAT_NODE_MEM_MAP
    printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
        nid, (unsigned long)pgdat,
        (unsigned long)pgdat->node_mem_map);
#endif
 
    free_area_init_core(pgdat, start_pfn, end_pfn,
                zones_size, zholes_size);
}

该函数中,其中init_zone_allows_reclaim()用于计算评估内存管理区是否可回收以及合适的node节点数,如果非NUMA环境,则该函数为空。而基于CONFIG_HAVE_MEMBLOCK_NODE_MAP的配置下,接下来将是get_pfn_range_for_nid():

【file:/mm/page_alloc.c】
/**
 * get_pfn_range_for_nid - Return the start and end page frames for a node
 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
 *
 * It returns the start and end page frame of a node based on information
 * provided by an arch calling add_active_range(). If called for a node
 * with no available memory, a warning is printed and the start and end
 * PFNs will be 0.
 */
void __meminit get_pfn_range_for_nid(unsigned int nid,
            unsigned long *start_pfn, unsigned long *end_pfn)
{
    unsigned long this_start_pfn, this_end_pfn;
    int i;
 
    *start_pfn = -1UL;
    *end_pfn = 0;
 
    for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
        *start_pfn = min(*start_pfn, this_start_pfn);
        *end_pfn = max(*end_pfn, this_end_pfn);
    }
 
    if (*start_pfn == -1UL)
        *start_pfn = 0;
}

此函数主要是将内存node节点的起始和末尾页框号返回给接下来的calculate_node_totalpages()来使用。

calculate_node_totalpages()实现:

【file:/mm/page_alloc.c】
static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
                        unsigned long node_start_pfn,
                        unsigned long node_end_pfn,
                        unsigned long *zones_size,
                        unsigned long *zholes_size)
{
    unsigned long realtotalpages, totalpages = 0;
    enum zone_type i;
 
    for (i = 0; i < MAX_NR_ZONES; i++)
        totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
                             node_start_pfn,
                             node_end_pfn,
                             zones_size);
    pgdat->node_spanned_pages = totalpages;
 
    realtotalpages = totalpages;
    for (i = 0; i < MAX_NR_ZONES; i++)
        realtotalpages -=
            zone_absent_pages_in_node(pgdat->node_id, i,
                          node_start_pfn, node_end_pfn,
                          zholes_size);
    pgdat->node_present_pages = realtotalpages;
    printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
                            realtotalpages);
}

其中zone_spanned_pages_in_node():

【file:/mm/page_alloc.c】
/*
 * Return the number of pages a zone spans in a node, including holes
 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
 */
static unsigned long __meminit zone_spanned_pages_in_node(int nid,
                    unsigned long zone_type,
                    unsigned long node_start_pfn,
                    unsigned long node_end_pfn,
                    unsigned long *ignored)
{
    unsigned long zone_start_pfn, zone_end_pfn;
 
    /* Get the start and end of the zone */
    zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
    zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
    adjust_zone_range_for_zone_movable(nid, zone_type,
                node_start_pfn, node_end_pfn,
                &zone_start_pfn, &zone_end_pfn);
 
    /* Check that this node has pages within the zone's required range */
    if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
        return 0;
 
    /* Move the zone boundaries inside the node if necessary */
    zone_end_pfn = min(zone_end_pfn, node_end_pfn);
    zone_start_pfn = max(zone_start_pfn, node_start_pfn);
 
    /* Return the spanned pages */
    return zone_end_pfn - zone_start_pfn;
}

其主要是统计node管理节点的内存跨度,该跨度不包括movable管理区的,里面调用的adjust_zone_range_for_zone_movable()则是用于剔除movable管理区的部分。

另外的zone_absent_pages_in_node():

【file:/mm/page_alloc.c】
/* Return the number of page frames in holes in a zone on a node */
static unsigned long __meminit zone_absent_pages_in_node(int nid,
                    unsigned long zone_type,
                    unsigned long node_start_pfn,
                    unsigned long node_end_pfn,
                    unsigned long *ignored)
{
    unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
    unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
    unsigned long zone_start_pfn, zone_end_pfn;
 
    zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
    zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
 
    adjust_zone_range_for_zone_movable(nid, zone_type,
            node_start_pfn, node_end_pfn,
            &zone_start_pfn, &zone_end_pfn);
    return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
}

该函数主要用于计算内存空洞页面数的。完了将会得到物理页面总数并在calculate_node_totalpages()中将页面总数打印出来:

image

再往下的mminit_verify_pageflags_layout()函数主要用于内存初始化调测使用的,由于未开启CONFIG_DEBUG_MEMORY_INIT配置项,此函数为空。而setup_nr_node_ids()是用于设置内存节点总数的,此处如果最大节点数MAX_NUMNODES不超过1,则是空函数。

free_area_init_nodes()函数末了还有一个遍历各个节点做初始化的操作,暂且留待后面再分析。

你可能感兴趣的:(Linux-3.14.12内存管理笔记【构建内存管理框架(3)】)