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


在我们使用ARM等嵌入式Linux系统的时候,一个头疼的问题是GPU,Camera,HDMI等都需要预留大量连续内存,这部分内存平时不用,但是一般的做法又必须先预留着。目前,Marek Szyprowski和Michal Nazarewicz实现了一套全新的Contiguous Memory Allocator。通过这套机制,我们可以做到不预留内存,这些内存平时是可用的,只有当需要的时候才被分配给Camera,HDMI等设备。下面分析它的基本代码流程。

声明连续内存

内核启动过程中arch/arm/mm/init.c中的arm_memblock_init()会调用dma_contiguous_reserve(min(arm_dma_limit, arm_lowmem_limit));

该函数位于:drivers/base/dma-contiguous.c

  1. /** 
  2.  * dma_contiguous_reserve() - reserve area for contiguous memory handling 
  3.  * @limit: End address of the reserved memory (optional, 0 for any). 
  4.  * 
  5.  * This function reserves memory from early allocator. It should be 
  6.  * called by arch specific code once the early allocator (memblock or bootmem) 
  7.  * has been activated and all other subsystems have already allocated/reserved 
  8.  * memory. 
  9.  */  
  10. void __init dma_contiguous_reserve(phys_addr_t limit)  
  11. {  
  12.         unsigned long selected_size = 0;  
  13.   
  14.         pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);  
  15.   
  16.         if (size_cmdline != -1) {  
  17.                 selected_size = size_cmdline;  
  18.         } else {  
  19. #ifdef CONFIG_CMA_SIZE_SEL_MBYTES   
  20.                 selected_size = size_bytes;  
  21. #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)   
  22.                 selected_size = cma_early_percent_memory();  
  23. #elif defined(CONFIG_CMA_SIZE_SEL_MIN)   
  24.                 selected_size = min(size_bytes, cma_early_percent_memory());  
  25. #elif defined(CONFIG_CMA_SIZE_SEL_MAX)   
  26.                 selected_size = max(size_bytes, cma_early_percent_memory());  
  27. #endif   
  28.         }     
  29.   
  30.         if (selected_size) {  
  31.                 pr_debug("%s: reserving %ld MiB for global area\n", __func__,  
  32.                          selected_size / SZ_1M);  
  33.   
  34.                 dma_declare_contiguous(NULL, selected_size, 0, limit);  
  35.         }     
  36. };  

其中的size_bytes定义为:

static const unsigned long size_bytes = CMA_SIZE_MBYTES * SZ_1M; 默认情况下,CMA_SIZE_MBYTES会被定义为16MB,来源于CONFIG_CMA_SIZE_MBYTES=16

->
  1. int __init dma_declare_contiguous(struct device *dev, unsigned long size,  
  2.                                   phys_addr_t base, phys_addr_t limit)  
  3. {  
  4.         ...  
  5.         /* Reserve memory */  
  6.         if (base) {  
  7.                 if (memblock_is_region_reserved(base, size) ||  
  8.                     memblock_reserve(base, size) < 0) {  
  9.                         base = -EBUSY;  
  10.                         goto err;  
  11.                 }  
  12.         } else {  
  13.                 /* 
  14.                  * Use __memblock_alloc_base() since 
  15.                  * memblock_alloc_base() panic()s. 
  16.                  */  
  17.                 phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);  
  18.                 if (!addr) {  
  19.                         base = -ENOMEM;  
  20.                         goto err;  
  21.                 } else if (addr + size > ~(unsigned long)0) {  
  22.                         memblock_free(addr, size);  
  23.                         base = -EINVAL;  
  24.                         base = -EINVAL;  
  25.                         goto err;  
  26.                 } else {  
  27.                         base = addr;  
  28.                 }  
  29.         }  
  30.   
  31.         /* 
  32.          * Each reserved area must be initialised later, when more kernel 
  33.          * subsystems (like slab allocator) are available. 
  34.          */  
  35.         r->start = base;  
  36.         r->size = size;  
  37.         r->dev = dev;  
  38.         cma_reserved_count++;  
  39.         pr_info("CMA: reserved %ld MiB at %08lx\n", size / SZ_1M,  
  40.                 (unsigned long)base);  
  41.   
  42.         /* Architecture specific contiguous memory fixup. */  
  43.         dma_contiguous_early_fixup(base, size);  
  44.         return 0;  
  45. err:  
  46.         pr_err("CMA: failed to reserve %ld MiB\n", size / SZ_1M);  
  47.         return base;  
  48. }   

由此可见,连续内存区域也是在内核启动的早期,通过__memblock_alloc_base()拿到的。

另外:

drivers/base/dma-contiguous.c里面的core_initcall()会导致cma_init_reserved_areas()被调用:

  1. static int __init cma_init_reserved_areas(void)  
  2. {  
  3.         struct cma_reserved *r = cma_reserved;  
  4.         unsigned i = cma_reserved_count;  
  5.   
  6.         pr_debug("%s()\n", __func__);  
  7.   
  8.         for (; i; --i, ++r) {  
  9.                 struct cma *cma;  
  10.                 cma = cma_create_area(PFN_DOWN(r->start),  
  11.                                       r->size >> PAGE_SHIFT);  
  12.                 if (!IS_ERR(cma))  
  13.                         dev_set_cma_area(r->dev, cma);  
  14.         }  
  15.         return 0;  
  16. }  
  17. core_initcall(cma_init_reserved_areas);  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容