OSDN Git Service

dma-contiguous: add dma_{alloc,free}_contiguous() helpers
authorNicolin Chen <nicoleotsuka@gmail.com>
Fri, 24 May 2019 04:06:32 +0000 (21:06 -0700)
committerChristoph Hellwig <hch@lst.de>
Mon, 3 Jun 2019 14:00:07 +0000 (16:00 +0200)
commitb1d2dc009dece4cd7e629419b52266ba51960a6b
treea31a473e64a10c54420e767337fd288753b7d6e3
parent1b961423158caaae49d3900b7c9c37477bbfa9b3
dma-contiguous: add dma_{alloc,free}_contiguous() helpers

Both dma_alloc_from_contiguous() and dma_release_from_contiguous() are
very simply implemented, but requiring callers to pass certain
parameters like count and align, and taking a boolean parameter to check
__GFP_NOWARN in the allocation flags. So every function call duplicates
similar work:

unsigned long order = get_order(size);
size_t count = size >> PAGE_SHIFT;

page = dma_alloc_from_contiguous(dev, count, order,
gfp & __GFP_NOWARN);

[...]

dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);

Additionally, as CMA can be used only in the context which permits
sleeping, most of callers do a gfpflags_allow_blocking() check and a
corresponding fallback allocation of normal pages upon any false result:

if (gfpflags_allow_blocking(flag))
page = dma_alloc_from_contiguous();
if (!page)
page = alloc_pages();

[...]

if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));

So this patch simplifies those function calls by abstracting these
operations into the two new functions: dma_{alloc,free}_contiguous.

As some callers of dma_{alloc,release}_from_contiguous() might be
complicated, this patch just implements these two new functions to
kernel/dma/direct.c only as an initial step.

Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Tested-by: dann frazier <dann.frazier@canonical.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
include/linux/dma-contiguous.h
kernel/dma/contiguous.c
kernel/dma/direct.c