dma: Attempt gigantic allocations outside of CMA
Subsystems (3)
PATCH 1/1 dma: Attempt gigantic allocations outside of CMA
Do not restrict gigantic DMA allocations to memory from CMA. If CMA memory is exhausted or not present and the requested size has an order greater than MAX_PAGE_ORDER, then attempt to find a suitable range of contiguous pages at call-time and allocate them with alloc_contig_pages().
Previously, if a region with an order exceeding MAX_PAGE_ORDER was requested via dma_direct_alloc() and CMA memory was unavailable, the allocation would fall back to the buddy allocator and fail. This fix makes the allocation fall back to alloc_contig_pages() increasing the chances of a successful allocation.
Signed-off-by Jake Freeland <[email protected]>
--- include/linux/dma-map-ops.h | 8 +++++++- include/linux/gfp.h | 16 ++++++++++++++-- kernel/dma/contiguous.c | 8 ++++++-- kernel/dma/direct.c | 26 +++++++++++++++++--------- 4 files changed, 44 insertions(+), 14 deletions(-)
include/linux/dma-map-ops.h +7 -1
@@ -151,7 +151,13 @@ static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,static inline void dma_free_contiguous (struct device *dev,struct page *page,size_t size) { -__free_pages (page,get_order (size)); +unsigned long nr_pages =PAGE_ALIGN (size) >>PAGE_SHIFT ; +int order =get_order (size); + +if (order >MAX_PAGE_ORDER ) +free_contig_range (page_to_pfn (page), nr_pages); +else +__free_pages (page, order); }#endif /* CONFIG_DMA_CMA*/
include/linux/gfp.h +14 -2
@@ -403,13 +403,25 @@ extern gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma);/* The below functions must be run on a range from a single zone. */ extern int alloc_contig_range_noprof (unsigned long start,unsigned long end,unsigned migratetype,gfp_t gfp_mask); -#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__))extern struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask,int nid,nodemask_t *nodemask); -#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) +#else +static inline int alloc_contig_range_noprof (unsigned long start,unsigned long end, +unsigned migratetype,gfp_t gfp_mask) +{ +return -ENOSYS ; +} +static inline struct page *alloc_contig_pages_noprof (unsigned long nr_pages, +gfp_t gfp_mask,int nid, +nodemask_t *nodemask) +{ +return NULL ; +}#endif +#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) +#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__))void free_contig_range (unsigned long pfn,unsigned long nr_pages);#ifdef CONFIG_CONTIG_ALLOC
kernel/dma/contiguous.c +6 -2
@@ -396,11 +396,12 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)* This function releases memory allocated by dma_alloc_contiguous(). As the * cma_release returns false when provided pages do not belong to contiguous * area and true otherwise, this function then does a fallback __free_pages() -* upon a false-return. +* or free_contig_range() (depending on order) upon a false-return. */ void dma_free_contiguous (struct device *dev,struct page *page,size_t size) {unsigned int count =PAGE_ALIGN (size) >>PAGE_SHIFT ; +int order =get_order (size);/* if dev has its own cma, free page from there */ if (dev->cma_area) {
@@ -423,7 +424,10 @@ void dma_free_contiguous(struct device *dev, struct page *page, size_t size) }/* not in any cma, free from buddy */ -__free_pages (page,get_order (size)); +if (order >MAX_PAGE_ORDER ) +free_contig_range (page_to_pfn (page), count); +else +__free_pages (page, order); }/*
kernel/dma/direct.c +17 -9
@@ -118,7 +118,8 @@ static struct page *dma_direct_alloc_swiotlb(struct device *dev, size_t size)static struct page *__dma_direct_alloc_pages (struct device *dev,size_t size,gfp_t gfp,bool allow_highmem) { -int node =dev_to_node (dev); +unsigned long nr_pages =PAGE_ALIGN (size) >>PAGE_SHIFT ; +int node =dev_to_node (dev), order =get_order (size);struct page *page =NULL ;u64 phys_limit;
@@ -130,17 +131,24 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, gfp |=dma_direct_optimal_gfp_mask (dev, &phys_limit); page =dma_alloc_contiguous (dev, size, gfp);if (page) { -if (!dma_coherent_ok (dev,page_to_phys (page), size) || - (!allow_highmem &&PageHighMem (page))) { -dma_free_contiguous (dev, page, size); - page =NULL ; - } +if (dma_coherent_ok (dev,page_to_phys (page), size) && + (allow_highmem || !PageHighMem (page))) +return page; +dma_free_contiguous (dev, page, size); } + again: -if (!page) - page =alloc_pages_node (node, gfp,get_order (size)); +if (order >MAX_PAGE_ORDER ) + page =alloc_contig_pages (nr_pages, gfp, + node !=NUMA_NO_NODE ? node :numa_mem_id (), + &node_states[N_MEMORY ]); +else + page =alloc_pages_node (node, gfp, order);if (page && !dma_coherent_ok (dev,page_to_phys (page), size)) { -__free_pages (page,get_order (size)); +if (order >MAX_PAGE_ORDER ) +free_contig_range (page_to_pfn (page), nr_pages); +else +__free_pages (page, order); page =NULL ; if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
Signature · 1 line
2.48.1