Linux核心最新的連續記憶體 Clerk(CMA):避免預留大塊記憶體

來源:互聯網
上載者:User

在我們使用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

/** * dma_contiguous_reserve() - reserve area for contiguous memory handling * @limit: End address of the reserved memory (optional, 0 for any). * * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem) * has been activated and all other subsystems have already allocated/reserved * memory. */void __init dma_contiguous_reserve(phys_addr_t limit){        unsigned long selected_size = 0;         pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);         if (size_cmdline != -1) {                selected_size = size_cmdline;        } else {#ifdef CONFIG_CMA_SIZE_SEL_MBYTES                selected_size = size_bytes;#elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)                selected_size = cma_early_percent_memory();#elif defined(CONFIG_CMA_SIZE_SEL_MIN)                selected_size = min(size_bytes, cma_early_percent_memory());#elif defined(CONFIG_CMA_SIZE_SEL_MAX)                selected_size = max(size_bytes, cma_early_percent_memory());#endif        }            if (selected_size) {                pr_debug("%s: reserving %ld MiB for global area\n", __func__,                         selected_size / SZ_1M);                 dma_declare_contiguous(NULL, selected_size, 0, limit);        }   };

其中的size_bytes定義為:

static const unsigned long size_bytes = CMA_SIZE_MBYTES * SZ_1M; 預設情況下,CMA_SIZE_MBYTES會被定義為16MB,來源於CONFIG_CMA_SIZE_MBYTES=16

->

int __init dma_declare_contiguous(struct device *dev, unsigned long size,                                  phys_addr_t base, phys_addr_t limit){        ...        /* Reserve memory */        if (base) {                if (memblock_is_region_reserved(base, size) ||                    memblock_reserve(base, size) < 0) {                        base = -EBUSY;                        goto err;                }        } else {                /*                 * Use __memblock_alloc_base() since                 * memblock_alloc_base() panic()s.                 */                phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);                if (!addr) {                        base = -ENOMEM;                        goto err;                } else if (addr + size > ~(unsigned long)0) {                        memblock_free(addr, size);                        base = -EINVAL;                        base = -EINVAL;                        goto err;                } else {                        base = addr;                }        }         /*         * Each reserved area must be initialised later, when more kernel         * subsystems (like slab allocator) are available.         */        r->start = base;        r->size = size;        r->dev = dev;        cma_reserved_count++;        pr_info("CMA: reserved %ld MiB at %08lx\n", size / SZ_1M,                (unsigned long)base);         /* Architecture specific contiguous memory fixup. */        dma_contiguous_early_fixup(base, size);        return 0;err:        pr_err("CMA: failed to reserve %ld MiB\n", size / SZ_1M);        return base;} 

由此可見,連續記憶體地區也是在核心啟動的早期,通過__memblock_alloc_base()拿到的。

另外:

drivers/base/dma-contiguous.c裡面的core_initcall()會導致cma_init_reserved_areas()被調用:

static int __init cma_init_reserved_areas(void){        struct cma_reserved *r = cma_reserved;        unsigned i = cma_reserved_count;         pr_debug("%s()\n", __func__);         for (; i; --i, ++r) {                struct cma *cma;                cma = cma_create_area(PFN_DOWN(r->start),                                      r->size >> PAGE_SHIFT);                if (!IS_ERR(cma))                        dev_set_cma_area(r->dev, cma);        }        return 0;}core_initcall(cma_init_reserved_areas);

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類型的:

#ifdef CONFIG_CMA/* Free whole pageblock and set it's migration type to MIGRATE_CMA. */void __init init_cma_reserved_pageblock(struct page *page){                                            unsigned i = pageblock_nr_pages;        struct page *p = page;                do {                __ClearPageReserved(p);                set_page_count(p, 0);        } while (++p, --i);                set_page_refcounted(page);        set_pageblock_migratetype(page, MIGRATE_CMA);        __free_pages(page, pageblock_order);        totalram_pages += pageblock_nr_pages;}       #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]);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.