手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux内核最新的连续内存分配器(CMA)——避免预留大块内存

[日期:2012-03-03] 来源:Linux社区  作者:宋宝华 [字体:大 中 小]
cma_create_area()会调用cma_activate_area(),cma_activate_area()函数则会针对每个page调用:

init_cma_reserved_pageblock(pfn_to_page(base_pfn));

这个函数则会通过set_pageblock_migratetype(page, MIGRATE_CMA)将页设置为MIGRATE_CMA类型的:

  1. #ifdef CONFIG_CMA   
  2. /* Free whole pageblock and set it's migration type to MIGRATE_CMA. */  
  3. void __init init_cma_reserved_pageblock(struct page *page)  
  4. {                                      
  5.         unsigned i = pageblock_nr_pages;  
  6.         struct page *p = page;  
  7.           
  8.         do {  
  9.                 __ClearPageReserved(p);  
  10.                 set_page_count(p, 0);  
  11.         } while (++p, --i);  
  12.           
  13.         set_page_refcounted(page);  
  14.         set_pageblock_migratetype(page, MIGRATE_CMA);  
  15.         __free_pages(page, pageblock_order);  
  16.         totalram_pages += pageblock_nr_pages;  
  17. }         
  18. #endif  
同时其中调用的__free_pages(page, pageblock_order);最终会调用到__free_one_page(page, zone, order, migratetype);
相关的page会被加到MIGRATE_CMA的free_list上面去:

list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);

申请连续内存

申请连续内存仍然使用标准的arch/arm/mm/dma-mapping.c中定义的dma_alloc_coherent()和dma_alloc_writecombine(),这二者会间接调用drivers/base/dma-contiguous.c中的

  1. struct page *dma_alloc_from_contiguous(struct device *dev, int count,  
  2.                                        unsigned int align)  

->

  1.  struct page *dma_alloc_from_contiguous(struct device *dev, int count,  
  2.                                       unsigned int align)  
  3. {  
  4.        ...  
  5.   
  6.        for (;;) {  
  7.                 pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,  
  8.                                                     start, count, mask);  
  9.                 if (pageno >= cma->count) {  
  10.                         ret = -ENOMEM;  
  11.                         goto error;  
  12.                 }  
  13.   
  14.                 pfn = cma->base_pfn + pageno;  
  15.                 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);  
  16.                 if (ret == 0) {  
  17.                         bitmap_set(cma->bitmap, pageno, count);  
  18.                         break;  
  19.                 } else if (ret != -EBUSY) {  
  20.                         goto error;  
  21.                 }  
  22.                 pr_debug("%s(): memory range at %p is busy, retrying\n",  
  23.                          __func__, pfn_to_page(pfn));  
  24.                 /* try again with a bit different memory target */  
  25.                 start = pageno + mask + 1;  
  26.         }  
  27.        ...  
  28.   
  29. }  
->

int alloc_contig_range(unsigned long start, unsigned long end,

                       unsigned migratetype)

需要隔离page,隔离page的作用通过代码的注释可以体现:

  1. /* 
  2.  * What we do here is we mark all pageblocks in range as 
  3.  * MIGRATE_ISOLATE.  Because of the way page allocator work, we 
  4.  * align the range to MAX_ORDER pages so that page allocator 
  5.  * won't try to merge buddies from different pageblocks and 
  6.  * change MIGRATE_ISOLATE to some other migration type. 
  7.  * 
  8.  * Once the pageblocks are marked as MIGRATE_ISOLATE, we 
  9.  * migrate the pages from an unaligned range (ie. pages that 
  10.  * we are interested in).  This will put all the pages in 
  11.  * range back to page allocator as MIGRATE_ISOLATE. 
  12.  * 
  13.  * When this is done, we take the pages in range from page 
  14.  * allocator removing them from the buddy system.  This way 
  15.  * page allocator will never consider using them. 
  16.  * 
  17.  * This lets us mark the pageblocks back as 
  18.  * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the 
  19.  * MAX_ORDER aligned range but not in the unaligned, original 
  20.  * range are put back to page allocator so that buddy can use 
  21.  * them.  
  22.  */    
  23.           
  24. ret = start_isolate_page_range(pfn_align_to_maxpage_down(start),  
  25.                                pfn_align_to_maxpage_up(end),  
  26.                                migratetype);  
简单地说,就是把相关的page标记为MIGRATE_ISOLATE,这样buddy系统就不会再使用他们。
  1. /*       
  2.  * start_isolate_page_range() -- make page-allocation-type of range of pages 
  3.  * to be MIGRATE_ISOLATE. 
  4.  * @start_pfn: The lower PFN of the range to be isolated. 
  5.  * @end_pfn: The upper PFN of the range to be isolated. 
  6.  * @migratetype: migrate type to set in error recovery. 
  7.  * 
  8.  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in 
  9.  * the range will never be allocated. Any free pages and pages freed in the 
  10.  * future will not be allocated again. 
  11.  * 
  12.  * start_pfn/end_pfn must be aligned to pageblock_order. 
  13.  * Returns 0 on success and -EBUSY if any part of range cannot be isolated. 
  14.  */  
  15. int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,  
  16.                              unsigned migratetype)  
  17. {  
  18.         unsigned long pfn;  
  19.         unsigned long undo_pfn;  
  20.         struct page *page;  
  21.   
  22.         BUG_ON((start_pfn) & (pageblock_nr_pages - 1));  
  23.         BUG_ON((end_pfn) & (pageblock_nr_pages - 1));  
  24.   
  25.         for (pfn = start_pfn;  
  26.              pfn < end_pfn;  
  27.              pfn += pageblock_nr_pages) {  
  28.                 page = __first_valid_page(pfn, pageblock_nr_pages);  
  29.                 if (page && set_migratetype_isolate(page)) {  
  30.                         undo_pfn = pfn;  
  31.                         goto undo;  
  32.                 }  
  33.         }  
  34.         return 0;  
  35. undo:  
  36.         for (pfn = start_pfn;  
  37.              pfn < undo_pfn;  
  38.              pfn += pageblock_nr_pages)  
  39.                 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);  
  40.   
  41.         return -EBUSY;  
  42. }  

接下来调用__alloc_contig_migrate_range()进行页面隔离和迁移:

  1. static int __alloc_contig_migrate_range(unsigned long start, unsigned long end)   
  2. {  
  3.         /* This function is based on compact_zone() from compaction.c. */  
  4.   
  5.         unsigned long pfn = start;  
  6.         unsigned int tries = 0;   
  7.         int ret = 0;   
  8.   
  9.         struct compact_control cc = {  
  10.                 .nr_migratepages = 0,   
  11.                 .order = -1,  
  12.                 .zone = page_zone(pfn_to_page(start)),  
  13.                 .sync = true,  
  14.         };     
  15.         INIT_LIST_HEAD(&cc.migratepages);  
  16.   
  17.         migrate_prep_local();  
  18.   
  19.         while (pfn < end || !list_empty(&cc.migratepages)) {  
  20.                 if (fatal_signal_pending(current)) {  
  21.                         ret = -EINTR;  
  22.                         break;  
  23.                 }      
  24.   
  25.                 if (list_empty(&cc.migratepages)) {  
  26.                         cc.nr_migratepages = 0;   
  27.                         pfn = isolate_migratepages_range(cc.zone, &cc,   
  28.                                                          pfn, end);  
  29.                         if (!pfn) {  
  30.                                 ret = -EINTR;  
  31.                                 break;  
  32.                         }      
  33.                         tries = 0;   
  34.                 } else if (++tries == 5) {   
  35.                         ret = ret < 0 ? ret : -EBUSY;  
  36.                         break;  
  37.                 }      
  38.   
  39.                 ret = migrate_pages(&cc.migratepages,  
  40.                                     __alloc_contig_migrate_alloc,  
  41.                                     0, false, true);  
  42.         }      
  43.   
  44.         putback_lru_pages(&cc.migratepages);  
  45.         return ret > 0 ? 0 : ret;   
  46. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款