linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] dma-direct: do not allocate a single page from CMA area
@ 2019-01-15 21:51 Nicolin Chen
  2019-02-04  8:23 ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolin Chen @ 2019-01-15 21:51 UTC (permalink / raw)
  To: hch, m.szyprowski, robin.murphy; +Cc: vdumpa, iommu, linux-kernel

The addresses within a single page are always contiguous, so it's
not so necessary to allocate one single page from CMA area. Since
the CMA area has a limited predefined size of space, it might run
out of space in some heavy use case, where there might be quite a
lot CMA pages being allocated for single pages.

This patch tries to skip CMA allocations of single pages and lets
them go through normal page allocations unless the allocation has
a DMA_ATTR_FORCE_CONTIGUOUS attribute. This'd save some resources
in the CMA area for further more CMA allocations, and it can also
reduce CMA fragmentations resulted from trivial allocations.

Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
---
Robin/Christoph,

I have some personal priority to submit this patch. I understand
you might have other plan to clean up the code first. Just would
it be possible for you to review and apply this one if it doesn't
conflict too much? Thanks!

Changelog
v1->v2:
 * Added DMA_ATTR_FORCE_CONTIGUOUS flag check so as to enforce
   CMA allocations if callers specified.
 * Added to the commit message the reduction of fragmentations
   suggested by Robin.

 kernel/dma/direct.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 355d16acee6d..5d57f99b2edf 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -109,8 +109,14 @@ struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
 	gfp |= __dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
 			&phys_mask);
 again:
-	/* CMA can be used only in the context which permits sleeping */
-	if (gfpflags_allow_blocking(gfp)) {
+	/*
+	 * CMA can be used only in the context which permits sleeping.
+	 * Since addresses within one PAGE are always contiguous, skip
+	 * CMA allocation for a single page to save CMA reserved space
+	 * unless DMA_ATTR_FORCE_CONTIGUOUS is flagged.
+	 */
+	if (gfpflags_allow_blocking(gfp) &&
+	    (count > 1 || attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
 		page = dma_alloc_from_contiguous(dev, count, page_order,
 						 gfp & __GFP_NOWARN);
 		if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-02-07  5:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-15 21:51 [PATCH v2] dma-direct: do not allocate a single page from CMA area Nicolin Chen
2019-02-04  8:23 ` Christoph Hellwig
2019-02-05 23:05   ` Nicolin Chen
2019-02-06  7:07     ` Christoph Hellwig
2019-02-07  2:28       ` Nicolin Chen
2019-02-07  5:37         ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).