linux内核工程师 3.03节 Linux伙伴系统(三)--分配页

前面已经介绍了伙伴系统的原理和Linux伙伴系统的数据结构,现在来看伙伴系统是如何来分配页面的。实际上,伙伴系统分配页面的算法并不复杂,但是由于考虑到分配内存时要尽量减少碎片的产生(涉及迁移机制)以及当内存不足时需要采取各种更为积极的手段,使得内核分配页面的相关函数完整地分析起来比较复杂庞大。在这里,我们只关注分配时最一般的情况,而其他情况的处理在以后单独拿出来讨论。

       我们从__alloc_pages_nodemask()这个函数开始分析,所有的分配页面的函数最终都会落到这个函数上面,它是伙伴系统的入口。

 

[cpp]  view plain  copy
  1. <span style="font-size:12px;">struct page *  
  2. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,  
  3.             struct zonelist *zonelist, nodemask_t *nodemask)  
  4. {  
  5.     /*根据gfp_mask确定分配页所处的管理区*/  
  6.     enum zone_type high_zoneidx = gfp_zone(gfp_mask);  
  7.     struct zone *preferred_zone;  
  8.     struct page *page;  
  9.     /*根据gfp_mask得到迁移类分配页的型*/  
  10.     int migratetype = allocflags_to_migratetype(gfp_mask);  
  11.   
  12.     gfp_mask &= gfp_allowed_mask;  
  13.   
  14.     lockdep_trace_alloc(gfp_mask);  
  15.   
  16.     might_sleep_if(gfp_mask & __GFP_WAIT);  
  17.   
  18.     if (should_fail_alloc_page(gfp_mask, order))  
  19.         return NULL;  
  20.   
  21.     /* 
  22.      * Check the zones suitable for the gfp_mask contain at least one 
  23.      * valid zone. It's possible to have an empty zonelist as a result 
  24.      * of GFP_THISNODE and a memoryless node 
  25.      */  
  26.     if (unlikely(!zonelist->_zonerefs->zone))  
  27.         return NULL;  
  28.   
  29.     /* The preferred zone is used for statistics later */  
  30.     /*从zonelist中找到zone_idx与high_zoneidx相同的管理区,也就是之前认定的管理区*/  
  31.     first_zones_zonelist(zonelist, high_zoneidx, nodemask, &preferred_zone);  
  32.     if (!preferred_zone)  
  33.         return NULL;  
  34.   
  35.     /* First allocation attempt */  
  36.     page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,  
  37.             zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,  
  38.             preferred_zone, migratetype);  
  39.     if (unlikely(!page))  
  40.         /*第一次分配失败的话则会用通过一条低速路径来进行第二次分配,包括唤醒页换出守护进程等等*/  
  41.         page = __alloc_pages_slowpath(gfp_mask, order,  
  42.                 zonelist, high_zoneidx, nodemask,  
  43.                 preferred_zone, migratetype);  
  44.   
  45.     trace_mm_page_alloc(page, order, gfp_mask, migratetype);  
  46.     return page;  
  47. }</span>  
  • 首先要做的就是找到指定的分配管理区,管理区的编号保存在high_zoneidx中
  • 然后就是尝试第一次分配,流程是从指定的管理区开始扫描管理区-->找到充足的管理区-->从指定的迁移类型链表中分配内存-->如果在指定迁移类型中找不到则到其他的迁移类型中去寻找
  • 如果第二步在各个区域都找不到可以满足分配的内存了,那么说明管理区的内存已经确实不够了,于是开始启用一条慢速的途径来分配,包括尝试去换出一些不经常使用的页等等,内核会在这次分配中表现得更为积极,其中的细节涉及到了其他一些复杂的东西,以后再做分析
[cpp]  view plain  copy
  1. static struct page *  
  2. get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,  
  3.         struct zonelist *zonelist, int high_zoneidx, int alloc_flags,  
  4.         struct zone *preferred_zone, int migratetype)  
  5. {  
  6.     struct zoneref *z;  
  7.     struct page *page = NULL;  
  8.     int classzone_idx;  
  9.     struct zone *zone;  
  10.     nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */  
  11.     int zlc_active = 0;     /* set if using zonelist_cache */  
  12.     int did_zlc_setup = 0;      /* just call zlc_setup() one time */  
  13.   
  14.     /*获取管理区的编号*/  
  15.     classzone_idx = zone_idx(preferred_zone);  
  16. zonelist_scan:  
  17.     /* 
  18.      * Scan zonelist, looking for a zone with enough free. 
  19.      * See also cpuset_zone_allowed() comment in kernel/cpuset.c. 
  20.      */  
  21.     /*从认定的管理区开始遍历,直到找到一个拥有足够空间的管理区, 
  22.       例如,如果high_zoneidx对应的ZONE_HIGHMEM,则遍历顺序为HIGHMEM-->NORMAL-->DMA, 
  23.       如果high_zoneidx对应ZONE_NORMAL,则遍历顺序为NORMAL-->DMA*/  
  24.     for_each_zone_zonelist_nodemask(zone, z, zonelist,  
  25.                         high_zoneidx, nodemask) {  
  26.         if (NUMA_BUILD && zlc_active &&  
  27.             !zlc_zone_worth_trying(zonelist, z, allowednodes))  
  28.                 continue;  
  29.   
  30.         /*检查给定的内存域是否属于该进程允许运行的CPU*/  
  31.         if ((alloc_flags & ALLOC_CPUSET) &&  
  32.             !cpuset_zone_allowed_softwall(zone, gfp_mask))  
  33.                 goto try_next_zone;  
  34.   
  35.         BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);  
  36.         if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {  
  37.             unsigned long mark;  
  38.             int ret;  
  39.               
  40.             /*通过alloc_flags来确定是使用何种水印,pages_min?pages_low?pages_high? 
  41.               选择了一种水印,就要求分配后的空闲不低于该水印才能进行分配*/  
  42.             mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];  
  43.   
  44.             /*如果管理区的水位线处于正常水平,则在该管理区进行分配*/  
  45.             if (zone_watermark_ok(zone, order, mark,  
  46.                     classzone_idx, alloc_flags))  
  47.                 goto try_this_zone;  
  48.   
  49.             if (zone_reclaim_mode == 0)  
  50.                 goto this_zone_full;  
  51.   
  52.             /*下面这部分都是针对NUMA架构的申请页面回收*/  
  53.             ret = zone_reclaim(zone, gfp_mask, order);  
  54.             switch (ret) {  
  55.             case ZONE_RECLAIM_NOSCAN:/*没有进行回收*/  
  56.                 /* did not scan */  
  57.                 goto try_next_zone;  
  58.             case ZONE_RECLAIM_FULL:  /*没有找到可回收的页面*/  
  59.                 /* scanned but unreclaimable */  
  60.                 goto this_zone_full;  
  61.             default:  
  62.                 /* did we reclaim enough */  
  63.                 if (!zone_watermark_ok(zone, order, mark,  
  64.                         classzone_idx, alloc_flags))  
  65.                     goto this_zone_full;  
  66.             }  
  67.         }  
  68.   
  69. try_this_zone:/*分配2^order个页*/  
  70.         page = buffered_rmqueue(preferred_zone, zone, order,  
  71.                         gfp_mask, migratetype);  
  72.         if (page)  
  73.             break;  
  74. this_zone_full:  
  75.         if (NUMA_BUILD)  
  76.             zlc_mark_zone_full(zonelist, z);  
  77. try_next_zone:  
  78.         if (NUMA_BUILD && !did_zlc_setup && nr_online_nodes > 1) {  
  79.             /* 
  80.              * we do zlc_setup after the first zone is tried but only 
  81.              * if there are multiple nodes make it worthwhile 
  82.              */  
  83.             allowednodes = zlc_setup(zonelist, alloc_flags);  
  84.             zlc_active = 1;  
  85.             did_zlc_setup = 1;  
  86.         }  
  87.     }  
  88.   
  89.     if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {  
  90.         /* Disable zlc cache for second zonelist scan */  
  91.         zlc_active = 0;  
  92.         goto zonelist_scan;  
  93.     }  
  94.     return page;  
  95. }  



  • 从指定的管理区开始按照zonelist中定义的顺序来遍历管理区
  • 如果该管理区的水位线正常,则调用buffered_rmqueue()在该管理区中分配
  • 如果管理区的水位线过低,则在NUMA架构下会申请页面回收
[cpp]  view plain  copy
  1. <span style="font-size:12px;">static inline  
  2. struct page *buffered_rmqueue(struct zone *preferred_zone,  
  3.             struct zone *zone, int order, gfp_t gfp_flags,  
  4.             int migratetype)  
  5. {  
  6.     unsigned long flags;  
  7.     struct page *page;  
  8.     int cold = !!(gfp_flags & __GFP_COLD);  
  9.     int cpu;  
  10.   
  11. again:  
  12.     cpu  = get_cpu();  
  13.     if (likely(order == 0)) {/*order为0,即要求分配一个页*/  
  14.         struct per_cpu_pages *pcp;  
  15.         struct list_head *list;  
  16.   
  17.         pcp = &zone_pcp(zone, cpu)->pcp;/*获取本地CPU对应的pcp*/  
  18.         list = &pcp->lists[migratetype];/*获取和迁移类型对应的链表*/  
  19.         local_irq_save(flags);  
  20.   
  21.         /*如果链表为空,则表示没有可分配的页,需要从伙伴系统中分配2^batch个页给list*/  
  22.         if (list_empty(list)) {  
  23.             pcp->count += rmqueue_bulk(zone, 0,  
  24.                     pcp->batch, list,  
  25.                     migratetype, cold);  
  26.             if (unlikely(list_empty(list)))  
  27.                 goto failed;  
  28.         }  
  29.   
  30.         if (cold)/*如果是需要冷页,则从链表的尾部获取*/  
  31.             page = list_entry(list->prev, struct page, lru);  
  32.         else     /*如果是需要热页,则从链表的头部获取*/  
  33.             page = list_entry(list->next, struct page, lru);  
  34.           
  35.         list_del(&page->lru);  
  36.         pcp->count--;  
  37.     } else {  
  38.         if (unlikely(gfp_flags & __GFP_NOFAIL)) {  
  39.             /* 
  40.              * __GFP_NOFAIL is not to be used in new code. 
  41.              * 
  42.              * All __GFP_NOFAIL callers should be fixed so that they 
  43.              * properly detect and handle allocation failures. 
  44.              * 
  45.              * We most definitely don't want callers attempting to 
  46.              * allocate greater than order-1 page units with 
  47.              * __GFP_NOFAIL. 
  48.              */  
  49.             WARN_ON_ONCE(order > 1);  
  50.         }  
  51.         spin_lock_irqsave(&zone->lock, flags);  
  52.         /*从管理区的伙伴系统中选择合适的内存块进行分配*/  
  53.         page = __rmqueue(zone, order, migratetype);  
  54.         spin_unlock(&zone->lock);  
  55.         if (!page)  
  56.             goto failed;  
  57.         __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << order));  
  58.     }  
  59.   
  60.     __count_zone_vm_events(PGALLOC, zone, 1 << order);  
  61.     zone_statistics(preferred_zone, zone);  
  62.     local_irq_restore(flags);  
  63.     put_cpu();  
  64.   
  65.     VM_BUG_ON(bad_range(zone, page));  
  66.     if (prep_new_page(page, order, gfp_flags))  
  67.         goto again;  
  68.     return page;  
  69.   
  70. failed:  
  71.     local_irq_restore(flags);  
  72.     put_cpu();  
  73.     return NULL;  
  74. }  
  75. </span>  

 

  • 该函数分两种情况进行处理,一种是只要求分配单个页框,另一种是要求分配多个连续页框
  • 对于单个页面,内核选择从每CPU页框高速缓存中分配,它的核心描述结构也是MIGRATE_TYPES个链表,只不过链表中的元素都是单个页。这些页分为热页和冷页,所谓热页就是还处在CPU高速缓存中的页,相反,冷页就是不存在于高速缓存中的页。对于单个页框的申请,分配热页可以提高效率。需要注意的是,越靠近链表头的页越热,越靠近链表尾的页越冷,因为每次释放单个页框的时候,页框是插入到链表的头部的,也就是说靠近头部的页框是最近才释放的,因此最有可能存在于高速缓存当中
  • 对于连续的页框分配,通过调用__rmqueue()来完成分配
[cpp]  view plain  copy
  1. <span style="font-size:12px;">static struct page *__rmqueue(struct zone *zone, unsigned int order,  
  2.                         int migratetype)  
  3. {  
  4.     struct page *page;  
  5.   
  6. retry_reserve:  
  7.       
  8.     page = __rmqueue_smallest(zone, order, migratetype);  
  9.   
  10.     /*如果分配失败并且迁移类型不是MIGRATE_RESERVE(如果是MIGRATE_RESERVE, 
  11.       则表明已经没有其他的迁移类型可供选择了)*/  
  12.     if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {  
  13.         page = __rmqueue_fallback(zone, order, migratetype);  
  14.   
  15.         /* 
  16.          * Use MIGRATE_RESERVE rather than fail an allocation. goto 
  17.          * is used because __rmqueue_smallest is an inline function 
  18.          * and we want just one call site 
  19.          */  
  20.         if (!page) {  
  21.             migratetype = MIGRATE_RESERVE;  
  22.             goto retry_reserve;  
  23.         }  
  24.     }  
  25.   
  26.     trace_mm_page_alloc_zone_locked(page, order, migratetype);  
  27.     return page;  
  28. }  
  29. </span>  

 

  • 首先按照指定的迁移类型,调用__rmqueue_smallest()来分配对应的内存块,该函数是伙伴系统的算法体现
  • 如果分配失败,则说明指定的迁移类型中没有充足的内存来满足分配,这时就要按fallbacks中定义的顺序从其他的迁移链表中寻找了,__rmqueue_fallback()函数较为复杂,体现了利用迁移类型来避免碎片的思想,后面单独拿出来分析
[cpp]  view plain  copy
  1. static inline  
  2. struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,  
  3.                         int migratetype)  
  4. {  
  5.     unsigned int current_order;  
  6.     struct free_area * area;  
  7.     struct page *page;  
  8.   
  9.     /* Find a page of the appropriate size in the preferred list */  
  10.     for (current_order = order; current_order < MAX_ORDER; ++current_order) {  
  11.   
  12.         /*获取和现在的阶数对应的free_area*/  
  13.         area = &(zone->free_area[current_order]);  
  14.   
  15.         /*和迁移类型对应的free_list为空则不执行下面的内容*/  
  16.         if (list_empty(&area->free_list[migratetype]))  
  17.             continue;  
  18.           
  19.         /*得到满足要求的页块中的第一个页描述符*/  
  20.         page = list_entry(area->free_list[migratetype].next,  
  21.                             struct page, lru);  
  22.         list_del(&page->lru);  
  23.         rmv_page_order(page);/*将page的private域设为0*/  
  24.         area->nr_free--;         /*内存块数减1*/  
  25.           
  26.         /*进行拆分(在current_order>order的情况下)*/  
  27.         expand(zone, page, order, current_order, area, migratetype);  
  28.         return page;  
  29.     }  
  30.   
  31.     return NULL;  
  32. }  
[cpp]  view plain  copy
  1.    
  2.   
  3. static inline void expand(struct zone *zone, struct page *page,  
  4.     int low, int high, struct free_area *area,  
  5.     int migratetype)  
  6. {  
  7.     unsigned long size = 1 << high;/*order为high的页块对应的页框数*/  
  8.   
  9.     /*申请的order为low,实际分配的块对应的order为high 
  10.       如果high大于low则要将大块进行拆分,并且将拆分后的伙伴块添加到下一级order的块链表中去*/  
  11.     while (high > low) {  
  12.         area--;/*area减1得到下一级order对应的area*/  
  13.         high--;/*high减1表明进行了一次拆分*/  
  14.         size >>= 1;/*拆分一次size就要除以2*/  
  15.         VM_BUG_ON(bad_range(zone, &page[size]));  
  16.   
  17.         /*通过size来定位拆分后的伙伴块的起始页框描述符, 
  18.         并将其作为第一个块添加到下一级order的块链表中*/  
  19.         list_add(&page[size].lru, &area->free_list[migratetype]);  
  20.         area->nr_free++;/*该order区域的块数加1*/  
  21.         set_page_order(&page[size], high);/*设定private域为high*/  
  22.     }  
  23. }  
  24.   
  25.    
只需要注意一点,一个块的定位可以由块首的起始页对应的描述符和order(size)来定位,因此只需要将一个块的第一个页描述符链入相应的链表就可以了。

猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80682545