Skip to content

Commit 274b3f7

Browse files
author
Christoph Hellwig
committed
dma-contiguous: cleanup dma_alloc_contiguous
Split out a cma_alloc_aligned helper to deal with the "interesting" calling conventions for cma_alloc, which then allows to the main function to be written straight forward. This also takes advantage of the fact that NULL dev arguments have been gone from the DMA API for a while. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Nicolin Chen <[email protected]> Reviewed-by: Barry Song <[email protected]>
1 parent 23efed6 commit 274b3f7

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

kernel/dma/contiguous.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
215215
return cma_release(dev_get_cma_area(dev), pages, count);
216216
}
217217

218+
static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
219+
{
220+
unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);
221+
222+
return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN);
223+
}
224+
218225
/**
219226
* dma_alloc_contiguous() - allocate contiguous pages
220227
* @dev: Pointer to device for which the allocation is performed.
@@ -231,24 +238,14 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
231238
*/
232239
struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
233240
{
234-
size_t count = size >> PAGE_SHIFT;
235-
struct page *page = NULL;
236-
struct cma *cma = NULL;
237-
238-
if (dev && dev->cma_area)
239-
cma = dev->cma_area;
240-
else if (count > 1)
241-
cma = dma_contiguous_default_area;
242-
243241
/* CMA can be used only in the context which permits sleeping */
244-
if (cma && gfpflags_allow_blocking(gfp)) {
245-
size_t align = get_order(size);
246-
size_t cma_align = min_t(size_t, align, CONFIG_CMA_ALIGNMENT);
247-
248-
page = cma_alloc(cma, count, cma_align, gfp & __GFP_NOWARN);
249-
}
250-
251-
return page;
242+
if (!gfpflags_allow_blocking(gfp))
243+
return NULL;
244+
if (dev->cma_area)
245+
return cma_alloc_aligned(dev->cma_area, size, gfp);
246+
if (size <= PAGE_SIZE || !dma_contiguous_default_area)
247+
return NULL;
248+
return cma_alloc_aligned(dma_contiguous_default_area, size, gfp);
252249
}
253250

254251
/**

0 commit comments

Comments
 (0)