Skip to contentSkip to conversation

dma-direct: do not allocate a single page from CMA area

PATCH In review Nicolin Chen 2 participants 6 messages Jan 15, 2019 – Feb 7, 2019 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dma-direct: do not allocate a single page from CMA area

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.

---
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(-)
kernel/dma/direct.c +8 -2
index 355d16acee6d..5d57f99b2edf 100644
@@ -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)) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 11 lines · Nicolin Chen

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.

That DMA_ATTR_FORCE_CONTIGUOUS flag does not make sense. A single page allocation is per defintion always contigous.

kernel/dma/direct.c struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, · line 119

Quoted 11 lines

again:

kernel/dma/direct.c +8 -2
-	/* 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)) {

And my other concern is that this skips allocating from the per-device pool, which drivers might rely on. To be honest I'm not sure there is much of a point in the per-device CMA pool vs the traditional per-device coherent pool, but I'd rather change that behavior in a clearly documented commit with intentions rather as a side effect from a random optimization.

#2 lore raw

Hi Christoph

Quoted 16 lines · Christoph Hellwig
Quoted 11 lines · Nicolin Chen

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.

That DMA_ATTR_FORCE_CONTIGUOUS flag does not make sense. A single page allocation is per defintion always contigous.

Quoted 14 lines

kernel/dma/direct.c struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, · line 119

Quoted 11 lines

again:

kernel/dma/direct.c +8 -2
-	/* 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)) {

And my other concern is that this skips allocating from the per-device pool, which drivers might rely on.

Actually Robin had the same concern at v1 and suggested that we could always use DMA_ATTR_FORCE_CONTIGUOUS to enforce into per-device pool.

Quoted 4 lines

To be honest I'm not sure there is
much of a point in the per-device CMA pool vs the traditional per-device coherent pool, but I'd rather change that behavior in a clearly documented commit with intentions rather as a side effect from a random optimization.

Hmm..sorry, I don't really follow this suggestion. Is it possible for you to make it clear that what should I do for the change?

Thanks
Nicolin

#3 lore raw
Quoted 5 lines · Nicolin Chen
Quoted 2 lines

And my other concern is that this skips allocating from the per-device pool, which drivers might rely on.

Actually Robin had the same concern at v1 and suggested that we could always use DMA_ATTR_FORCE_CONTIGUOUS to enforce into per-device pool.

That is both against the documented behavior of DMA_ATTR_FORCE_CONTIGUOUS and doesn't help existing drivers that specify their CMA area in DT.

Quoted 7 lines
Quoted 4 lines

To be honest I'm not sure there is
much of a point in the per-device CMA pool vs the traditional per-device coherent pool, but I'd rather change that behavior in a clearly documented commit with intentions rather as a side effect from a random optimization.

Hmm..sorry, I don't really follow this suggestion. Is it possible for you to make it clear that what should I do for the change?

Something like this (plus proper comments):

kernel/dma/contiguous.c +10 -1
index b2a87905846d..789d734f0f77 100644
@@ -192,10 +192,19 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 				       unsigned int align, bool no_warn)
 {
+	struct cma *cma;
+
 	if (align > CONFIG_CMA_ALIGNMENT)
 		align = CONFIG_CMA_ALIGNMENT;
 
-	return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
+	if (dev && dev->cma_area)
+		cma = dev->cma_area;
+	else if (count > PAGE_SIZE)
+		cma = dma_contiguous_default_area;
+	else
+		return NULL;
+
+	return cma_alloc(cma, count, align, no_warn);
 }
 
 /**
#4 lore raw

Hi Christoph,

Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Nicolin Chen
Quoted 2 lines

And my other concern is that this skips allocating from the per-device pool, which drivers might rely on.

Actually Robin had the same concern at v1 and suggested that we could always use DMA_ATTR_FORCE_CONTIGUOUS to enforce into per-device pool.

That is both against the documented behavior of DMA_ATTR_FORCE_CONTIGUOUS and doesn't help existing drivers that specify their CMA area in DT.

OK. I will drop it.

Quoted 20 lines
Quoted 7 lines
Quoted 4 lines

To be honest I'm not sure there is
much of a point in the per-device CMA pool vs the traditional per-device coherent pool, but I'd rather change that behavior in a clearly documented commit with intentions rather as a side effect from a random optimization.

Hmm..sorry, I don't really follow this suggestion. Is it possible for you to make it clear that what should I do for the change?

Something like this (plus proper comments):

kernel/dma/contiguous.c +2 -0
index b2a87905846d..789d734f0f77 100644
@@ -192,10 +192,19 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 				       unsigned int align, bool no_warn)
 {
+	struct cma *cma;
+

kernel/dma/contiguous.c int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, · line 205

Quoted 10 lines
kernel/dma/contiguous.c +6 -1
 	if (align > CONFIG_CMA_ALIGNMENT)
 		align = CONFIG_CMA_ALIGNMENT;
 
-	return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
+	if (dev && dev->cma_area)
+		cma = dev->cma_area;
+	else if (count > PAGE_SIZE)
+		cma = dma_contiguous_default_area;
+	else
+		return NULL;

So we will keep allocating single pages in dev->cma_area if it's present, in order to address your previous concern?

Thanks
Nicolin

#5 lore raw
Quoted 2 lines · Nicolin Chen

So we will keep allocating single pages in dev->cma_area if it's present, in order to address your previous concern?

Yes.