Skip to contentSkip to conversation

Optimize dma_*_from_contiguous calls

PATCH Superseded Nicolin Chen 4 participants 22 messages May 6, 2019 – Aug 29, 2019

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Optimize dma_*_from_contiguous calls

[ Per discussion at v1, we decide to add two new functions and start
replacing callers one by one. For this series, it only touches the dma-direct part. And instead of merging two PATCHes, I still keep them separate so that we may easily revert PATCH-2 if anything bad happens as last time -- PATCH-1 is supposed to be a safe cleanup. ]

This series of patches try to optimize dma_*_from_contiguous calls:
PATCH-1 abstracts two new functions and applies to dma-direct.c file. PATCH-2 saves single pages and reduce fragmentations from CMA area.

Please check their commit messages for detail changelog.

Nicolin Chen (2):
dma-contiguous: Abstract dma_{alloc,free}_contiguous()
dma-contiguous: Use fallback alloc_pages for single pages

 include/linux/dma-contiguous.h | 10 ++++++
 kernel/dma/contiguous.c        | 57 ++++++++++++++++++++++++++++++++++
 kernel/dma/direct.c            | 24 +++-----------
 3 files changed, 71 insertions(+), 20 deletions(-)
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw

PATCH v2 1/2 dma-contiguous: Abstract dma_{alloc,free}_contiguous()

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:
/* A piece of example */
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:
/* A piece of example */
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.

---
Changelog
v1->v2:
* Added new functions beside the old ones so we can replace callers one by one later.
* Applied new functions to dma/direct.c only, because it's the best example caller to apply and should be safe with the new functions.

 include/linux/dma-contiguous.h | 10 +++++++
 kernel/dma/contiguous.c        | 48 ++++++++++++++++++++++++++++++++++
 kernel/dma/direct.c            | 24 +++--------------
 3 files changed, 62 insertions(+), 20 deletions(-)
include/linux/dma-contiguous.h +10 -0
index f247e8aa5e3d..dacbdcb91a89 100644
@@ -115,6 +115,8 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 				       unsigned int order, bool no_warn);
 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
 				 int count);
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
 
 #else
 
@@ -157,6 +159,14 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
 	return false;
 }
 
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+{
+	return NULL;
+}
+
+static inline
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size) { }
+
 #endif
 
 #endif
kernel/dma/contiguous.c +48 -0

54 lines not on this page · raw message

kernel/dma/direct.c +4 -20
index 2c2772e9702a..0816c1e8b05a 100644
@@ -96,8 +96,6 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
-	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-	int page_order = get_order(size);
 	struct page *page = NULL;
 	u64 phys_mask;
 
@@ -109,20 +107,9 @@ 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)) {
-		page = dma_alloc_from_contiguous(dev, count, page_order,
-						 gfp & __GFP_NOWARN);
-		if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-			dma_release_from_contiguous(dev, page, count);
-			page = NULL;
-		}
-	}
-	if (!page)
-		page = alloc_pages_node(dev_to_node(dev), gfp, page_order);
-
+	page = dma_alloc_contiguous(dev, size, gfp);
 	if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-		__free_pages(page, page_order);
+		dma_free_contiguous(dev, page, size);
 		page = NULL;
 
 		if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
@@ -154,7 +141,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
 	if (PageHighMem(page)) {
 		/*
 		 * Depending on the cma= arguments and per-arch setup
-		 * dma_alloc_from_contiguous could return highmem pages.
+		 * dma_alloc_contiguous could return highmem pages.
 		 * Without remapping there is no way to return them here,
 		 * so log an error and fail.
 		 */
@@ -176,10 +163,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
 
 void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page)
 {
-	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-
-	if (!dma_release_from_contiguous(dev, page, count))
-		__free_pages(page, get_order(size));
+	dma_free_contiguous(dev, page, size);
 }
 
 void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v2 1/2] dma-contiguous: Abstract dma_{alloc, free}_contiguous()

Quoted 81 lines · Nicolin Chen

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:
/* A piece of example */
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:
/* A piece of example */
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.

---
Changelog
v1->v2:
* Added new functions beside the old ones so we can replace callers one by one later.
* Applied new functions to dma/direct.c only, because it's the best example caller to apply and should be safe with the new functions.

 include/linux/dma-contiguous.h | 10 +++++++
 kernel/dma/contiguous.c        | 48 ++++++++++++++++++++++++++++++++++
 kernel/dma/direct.c            | 24 +++--------------
 3 files changed, 62 insertions(+), 20 deletions(-)
include/linux/dma-contiguous.h +10 -0
index f247e8aa5e3d..dacbdcb91a89 100644
@@ -115,6 +115,8 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
                                       unsigned int order, bool no_warn);
 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
                                 int count);
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
 
 #else
 
@@ -157,6 +159,14 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
        return false;
 }
 
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+{
+       return NULL;
+}
+
+static inline
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size) { }
+
 #endif
 
 #endif
kernel/dma/contiguous.c +0 -0
index b2a87905846d..21f39a6cb04f 100644

kernel/dma/contiguous.c bool dma_release_from_contiguous(struct device *dev, struct page *pages, · line 215

Quoted 2 lines
kernel/dma/contiguous.c +0 -0
@@ -214,6 +214,54 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
        return cma_release(dev_get_cma_area(dev), pages, count);
 }

This breaks the build for me if CONFIG_DMA_CMA=n:

LD [M] fs/9p/9p.o
ld: fs/9p/vfs_inode.o: in function `dma_alloc_contiguous':
vfs_inode.c:(.text+0xa60): multiple definition of `dma_alloc_contiguous'; fs/9p/vfs_super.o:vfs_super.c:(.text+0x500):
first defined here

Do the following insertions need to be under an #ifdef CONFIG_DMA_CMA ?

-dann

Quoted 112 lines
kernel/dma/contiguous.c +48 -0
+/**
+ * dma_alloc_contiguous() - allocate contiguous pages
+ * @dev:   Pointer to device for which the allocation is performed.
+ * @size:  Requested allocation size.
+ * @gfp:   Allocation flags.
+ *
+ * This function allocates contiguous memory buffer for specified device. It
+ * first tries to use device specific contiguous memory area if available or
+ * the default global one, then tries a fallback allocation of normal pages.
+ */
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+{
+       int node = dev ? dev_to_node(dev) : NUMA_NO_NODE;
+       size_t count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+       size_t align = get_order(PAGE_ALIGN(size));
+       struct cma *cma = dev_get_cma_area(dev);
+       struct page *page = NULL;
+
+       /* CMA can be used only in the context which permits sleeping */
+       if (cma && gfpflags_allow_blocking(gfp)) {
+               align = min_t(size_t, align, CONFIG_CMA_ALIGNMENT);
+               page = cma_alloc(cma, count, align, gfp & __GFP_NOWARN);
+       }
+
+       /* Fallback allocation of normal pages */
+       if (!page)
+               page = alloc_pages_node(node, gfp, align);
+
+       return page;
+}
+
+/**
+ * dma_free_contiguous() - release allocated pages
+ * @dev:   Pointer to device for which the pages were allocated.
+ * @page:  Pointer to the allocated pages.
+ * @size:  Size of allocated pages.
+ *
+ * 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.
+ */
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size)
+{
+       if (!cma_release(dev_get_cma_area(dev), page, size >> PAGE_SHIFT))
+               __free_pages(page, get_order(size));
+}
+
 /*
  * Support for reserved memory regions defined in device tree
  */
kernel/dma/direct.c +4 -21
index 2c2772e9702a..0816c1e8b05a 100644
@@ -96,8 +96,6 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
                dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
-       unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-       int page_order = get_order(size);
        struct page *page = NULL;
        u64 phys_mask;
 
@@ -109,20 +107,9 @@ 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)) {
-               page = dma_alloc_from_contiguous(dev, count, page_order,
-                                                gfp & __GFP_NOWARN);
-               if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-                       dma_release_from_contiguous(dev, page, count);
-                       page = NULL;
-               }
-       }
-       if (!page)
-               page = alloc_pages_node(dev_to_node(dev), gfp, page_order);
-
+       page = dma_alloc_contiguous(dev, size, gfp);
        if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-               __free_pages(page, page_order);
+               dma_free_contiguous(dev, page, size);
                page = NULL;
 
                if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
@@ -154,7 +141,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
        if (PageHighMem(page)) {
                /*
                 * Depending on the cma= arguments and per-arch setup
-                * dma_alloc_from_contiguous could return highmem pages.
+                * dma_alloc_contiguous could return highmem pages.
                 * Without remapping there is no way to return them here,
                 * so log an error and fail.
                 */
@@ -176,10 +163,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
 
 void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page)
 {
-       unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-
-       if (!dma_release_from_contiguous(dev, page, count))
-               __free_pages(page, get_order(size));
+       dma_free_contiguous(dev, page, size);
 }
 
 void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
--

2.17.1

Quoted 4 lines

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw

Re: [PATCH v2 1/2] dma-contiguous: Abstract dma_{alloc, free}_contiguous()

Quoted 88 lines · dann frazier
Quoted 81 lines · Nicolin Chen

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:
/* A piece of example */
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:
/* A piece of example */
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.

---
Changelog
v1->v2:
* Added new functions beside the old ones so we can replace callers one by one later.
* Applied new functions to dma/direct.c only, because it's the best example caller to apply and should be safe with the new functions.

 include/linux/dma-contiguous.h | 10 +++++++
 kernel/dma/contiguous.c        | 48 ++++++++++++++++++++++++++++++++++
 kernel/dma/direct.c            | 24 +++--------------
 3 files changed, 62 insertions(+), 20 deletions(-)
include/linux/dma-contiguous.h +10 -0
index f247e8aa5e3d..dacbdcb91a89 100644
@@ -115,6 +115,8 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
                                       unsigned int order, bool no_warn);
 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
                                 int count);
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
 
 #else
 
@@ -157,6 +159,14 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
        return false;
 }
 
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+{
+       return NULL;
+}
+
+static inline
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size) { }
+
 #endif
 
 #endif
kernel/dma/contiguous.c +0 -0
index b2a87905846d..21f39a6cb04f 100644

kernel/dma/contiguous.c bool dma_release_from_contiguous(struct device *dev, struct page *pages, · line 215

Quoted 2 lines
kernel/dma/contiguous.c +0 -0
@@ -214,6 +214,54 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
        return cma_release(dev_get_cma_area(dev), pages, count);
 }

This breaks the build for me if CONFIG_DMA_CMA=n:

Quoted 7 lines

LD [M] fs/9p/9p.o
ld: fs/9p/vfs_inode.o: in function `dma_alloc_contiguous':
vfs_inode.c:(.text+0xa60): multiple definition of `dma_alloc_contiguous'; fs/9p/vfs_super.o:vfs_super.c:(.text+0x500):
first defined here

Do the following insertions need to be under an #ifdef CONFIG_DMA_CMA ?

Ah, no - the problem is actually a missing "static inline" in the !CONFIG_DMA_CMA version of dma_alloc_contiguous().

-dann

Quoted 116 lines
Quoted 116 lines
kernel/dma/contiguous.c +48 -0
+/**
+ * dma_alloc_contiguous() - allocate contiguous pages
+ * @dev:   Pointer to device for which the allocation is performed.
+ * @size:  Requested allocation size.
+ * @gfp:   Allocation flags.
+ *
+ * This function allocates contiguous memory buffer for specified device. It
+ * first tries to use device specific contiguous memory area if available or
+ * the default global one, then tries a fallback allocation of normal pages.
+ */
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+{
+       int node = dev ? dev_to_node(dev) : NUMA_NO_NODE;
+       size_t count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+       size_t align = get_order(PAGE_ALIGN(size));
+       struct cma *cma = dev_get_cma_area(dev);
+       struct page *page = NULL;
+
+       /* CMA can be used only in the context which permits sleeping */
+       if (cma && gfpflags_allow_blocking(gfp)) {
+               align = min_t(size_t, align, CONFIG_CMA_ALIGNMENT);
+               page = cma_alloc(cma, count, align, gfp & __GFP_NOWARN);
+       }
+
+       /* Fallback allocation of normal pages */
+       if (!page)
+               page = alloc_pages_node(node, gfp, align);
+
+       return page;
+}
+
+/**
+ * dma_free_contiguous() - release allocated pages
+ * @dev:   Pointer to device for which the pages were allocated.
+ * @page:  Pointer to the allocated pages.
+ * @size:  Size of allocated pages.
+ *
+ * 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.
+ */
+void dma_free_contiguous(struct device *dev, struct page *page, size_t size)
+{
+       if (!cma_release(dev_get_cma_area(dev), page, size >> PAGE_SHIFT))
+               __free_pages(page, get_order(size));
+}
+
 /*
  * Support for reserved memory regions defined in device tree
  */
kernel/dma/direct.c +4 -21
index 2c2772e9702a..0816c1e8b05a 100644
@@ -96,8 +96,6 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
                dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
-       unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-       int page_order = get_order(size);
        struct page *page = NULL;
        u64 phys_mask;
 
@@ -109,20 +107,9 @@ 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)) {
-               page = dma_alloc_from_contiguous(dev, count, page_order,
-                                                gfp & __GFP_NOWARN);
-               if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-                       dma_release_from_contiguous(dev, page, count);
-                       page = NULL;
-               }
-       }
-       if (!page)
-               page = alloc_pages_node(dev_to_node(dev), gfp, page_order);
-
+       page = dma_alloc_contiguous(dev, size, gfp);
        if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
-               __free_pages(page, page_order);
+               dma_free_contiguous(dev, page, size);
                page = NULL;
 
                if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
@@ -154,7 +141,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
        if (PageHighMem(page)) {
                /*
                 * Depending on the cma= arguments and per-arch setup
-                * dma_alloc_from_contiguous could return highmem pages.
+                * dma_alloc_contiguous could return highmem pages.
                 * Without remapping there is no way to return them here,
                 * so log an error and fail.
                 */
@@ -176,10 +163,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
 
 void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page)
 {
-       unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-
-       if (!dma_release_from_contiguous(dev, page, count))
-               __free_pages(page, get_order(size));
+       dma_free_contiguous(dev, page, size);
 }
 
 void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
--

2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#4 lore raw

Re: [PATCH v2 1/2] dma-contiguous: Abstract dma_{alloc, free}_contiguous()

Quoted 16 lines · dann frazier
Quoted 16 lines

kernel/dma/contiguous.c bool dma_release_from_contiguous(struct device *dev, struct page *pages, · line 215

Quoted 7 lines
kernel/dma/contiguous.c +0 -0
index b2a87905846d..21f39a6cb04f 100644
@@ -214,6 +214,54 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
        return cma_release(dev_get_cma_area(dev), pages, count);
 }

This breaks the build for me if CONFIG_DMA_CMA=n:

LD [M] fs/9p/9p.o
ld: fs/9p/vfs_inode.o: in function `dma_alloc_contiguous':
vfs_inode.c:(.text+0xa60): multiple definition of `dma_alloc_contiguous'; fs/9p/vfs_super.o:vfs_super.c:(.text+0x500):
first defined here

Quoted 4 lines
Quoted 1 line

Do the following insertions need to be under an #ifdef CONFIG_DMA_CMA ?

Ah, no - the problem is actually a missing "static inline" in the !CONFIG_DMA_CMA version of dma_alloc_contiguous().

Yea, I saw it. Thanks for the testing and pointing it out.

Sending v3.....
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#5 lore raw

PATCH v2 2/2 dma-contiguous: Use fallback alloc_pages for single pages

In reply to Nicolin Chen

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

However, there is also a concern that a device might care where a page comes from -- it might expect the page from CMA area and act differently if the page doesn't.

This patch tries to use the fallback alloc_pages path, instead of one-page size allocations from the global CMA area in case that a device does not have its own CMA area. This'd save resources from the CMA global area for more CMA allocations, and also reduce CMA fragmentations resulted from trivial allocations.

---
 kernel/dma/contiguous.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/dma/contiguous.c +10 -1
index 21f39a6cb04f..6914b92d5c88 100644
@@ -223,14 +223,23 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  * This function allocates contiguous memory buffer for specified device. It
  * first tries to use device specific contiguous memory area if available or
  * the default global one, then tries a fallback allocation of normal pages.
+ *
+ * Note that it byapss one-page size of allocations from the global area as
+ * the addresses within one page are always contiguous, so there is no need
+ * to waste CMA pages for that kind; it also helps reduce fragmentations.
  */
 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
 {
 	int node = dev ? dev_to_node(dev) : NUMA_NO_NODE;
 	size_t count = PAGE_ALIGN(size) >> PAGE_SHIFT;
 	size_t align = get_order(PAGE_ALIGN(size));
-	struct cma *cma = dev_get_cma_area(dev);
 	struct page *page = NULL;
+	struct cma *cma = NULL;
+
+	if (dev && dev->cma_area)
+		cma = dev->cma_area;
+	else if (count > 1)
+		cma = dma_contiguous_default_area;
 
 	/* CMA can be used only in the context which permits sleeping */
 	if (cma && gfpflags_allow_blocking(gfp)) {
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#6 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 11 lines · Nicolin Chen

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

However, there is also a concern that a device might care where a page comes from -- it might expect the page from CMA area and act differently if the page doesn't.

Quoted 7 lines

This patch tries to use the fallback alloc_pages path, instead of one-page size allocations from the global CMA area in case that a device does not have its own CMA area. This'd save resources from the CMA global area for more CMA allocations, and also reduce CMA fragmentations resulted from trivial allocations.

This commit (bd2e75633c8012fc8a7431c82fda66237133bf7e) broke the DMA for my MMC driver in the following way:

[    1.876755] mmc0: ADMA error
[    1.883385] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    1.889834] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    1.896284] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    1.902733] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    1.909182] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    1.915631] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    1.922081] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000fa07
[    1.928530] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    1.934981] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    1.941429] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    1.947880] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    1.954329] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    1.960778] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    1.967229] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    1.973678] mmc0: sdhci: Host ctl2: 0x00000000
[    1.978125] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    1.985271] mmc0: sdhci: ============================================
[    1.991758] mmc0: error -5 whilst initialising MMC card
[    1.991913] 43fb0000.uart: ttyS1 at MMIO 0x43fb0000 (irq = 0,
base_baud = 768000) is a 16550A
[    2.011011] hctosys: unable to open rtc device (rtc0)
[    2.017694] Freeing unused kernel memory: 2368K
[    2.027131] Run /init as init process

Starting syslogd: OK
Starting klogd: OK
Initializing random number generator... [ 2.074399] random: dd:
uninitialized urandom read (512 bytes read)
done.
Starting network: OK

[    2.109593] mmc0: ADMA error
[    2.112488] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.118941] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.125389] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.131840] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.138289] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.144738] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.151188] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x00004e47
[    2.157637] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.164087] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.170536] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.176987] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.183435] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.189886] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.196335] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.202784] mmc0: sdhci: Host ctl2: 0x00000000
[    2.207232] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.214379] mmc0: sdhci: ============================================
[    2.220881] mmc0: error -5 whilst initialising MMC card
Welcome to Buildroot
buildroot login: [    2.332786] mmc0: ADMA error
[    2.335668] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.342119] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.348568] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.355018] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.361468] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.367917] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.374367] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000f447
[    2.380816] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.387267] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.393716] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.400166] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.406615] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.413065] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.419515] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.425963] mmc0: sdhci: Host ctl2: 0x00000000
[    2.430412] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.437557] mmc0: sdhci: ============================================
[    2.444031] mmc0: error -5 whilst initialising MMC card
[    2.572203] mmc0: ADMA error
[    2.575089] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.581540] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.587989] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.594439] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.600889] mmc0: sdhci: Present:   0x01ef02f6 | Host ctl: 0x00000019
[    2.607339] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.613788] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000e8c7
[    2.620237] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.626686] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.633137] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.639586] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.646036] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.652485] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.658936] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.665384] mmc0: sdhci: Host ctl2: 0x00000000
[    2.669832] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.676979] mmc0: sdhci: ============================================
[    2.683450] mmc0: error -5 whilst initialising MMC card

CTRL-A Z for help | 115200 8N1 | NOR | Minicom 2.7.1 | VT102 | Offline | ttyUSB0

Reverting this commit fixed the problem.

Signature · 2 lines
Best Regards
Masahiro Yamada
#7 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

+ linux-mmc, Ulf Hansson, Adrian Hunter,

ADMA of SDHCI is not working
since bd2e75633c8012fc8a7431c82fda66237133bf7e

Did anybody see the same problem?

Masahiro

Quoted 128 lines · Masahiro Yamada
Quoted 11 lines · Nicolin Chen

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

However, there is also a concern that a device might care where a page comes from -- it might expect the page from CMA area and act differently if the page doesn't.

Quoted 7 lines

This patch tries to use the fallback alloc_pages path, instead of one-page size allocations from the global CMA area in case that a device does not have its own CMA area. This'd save resources from the CMA global area for more CMA allocations, and also reduce CMA fragmentations resulted from trivial allocations.

This commit (bd2e75633c8012fc8a7431c82fda66237133bf7e) broke the DMA for my MMC driver in the following way:

[    1.876755] mmc0: ADMA error
[    1.883385] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    1.889834] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    1.896284] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    1.902733] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    1.909182] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    1.915631] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    1.922081] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000fa07
[    1.928530] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    1.934981] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    1.941429] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    1.947880] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    1.954329] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    1.960778] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    1.967229] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    1.973678] mmc0: sdhci: Host ctl2: 0x00000000
[    1.978125] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    1.985271] mmc0: sdhci: ============================================
[    1.991758] mmc0: error -5 whilst initialising MMC card
[    1.991913] 43fb0000.uart: ttyS1 at MMIO 0x43fb0000 (irq = 0,
base_baud = 768000) is a 16550A
[    2.011011] hctosys: unable to open rtc device (rtc0)
[    2.017694] Freeing unused kernel memory: 2368K
[    2.027131] Run /init as init process

Starting syslogd: OK
Starting klogd: OK
Initializing random number generator... [ 2.074399] random: dd:
uninitialized urandom read (512 bytes read)
done.
Starting network: OK

[    2.109593] mmc0: ADMA error
[    2.112488] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.118941] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.125389] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.131840] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.138289] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.144738] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.151188] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x00004e47
[    2.157637] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.164087] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.170536] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.176987] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.183435] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.189886] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.196335] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.202784] mmc0: sdhci: Host ctl2: 0x00000000
[    2.207232] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.214379] mmc0: sdhci: ============================================
[    2.220881] mmc0: error -5 whilst initialising MMC card
Welcome to Buildroot
buildroot login: [    2.332786] mmc0: ADMA error
[    2.335668] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.342119] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.348568] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.355018] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.361468] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.367917] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.374367] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000f447
[    2.380816] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.387267] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.393716] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.400166] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.406615] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.413065] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.419515] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.425963] mmc0: sdhci: Host ctl2: 0x00000000
[    2.430412] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.437557] mmc0: sdhci: ============================================
[    2.444031] mmc0: error -5 whilst initialising MMC card
[    2.572203] mmc0: ADMA error
[    2.575089] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.581540] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.587989] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.594439] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.600889] mmc0: sdhci: Present:   0x01ef02f6 | Host ctl: 0x00000019
[    2.607339] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.613788] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000e8c7
[    2.620237] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.626686] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.633137] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.639586] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.646036] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.652485] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.658936] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.665384] mmc0: sdhci: Host ctl2: 0x00000000
[    2.669832] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.676979] mmc0: sdhci: ============================================
[    2.683450] mmc0: error -5 whilst initialising MMC card

CTRL-A Z for help | 115200 8N1 | NOR | Minicom 2.7.1 | VT102 | Offline | ttyUSB0

Reverting this commit fixed the problem.

--
Best Regards
Masahiro Yamada

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Christoph,

Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Masahiro Yamada

+ linux-mmc, Ulf Hansson, Adrian Hunter,

ADMA of SDHCI is not working
since bd2e75633c8012fc8a7431c82fda66237133bf7e

Does it work for you with this commit:

http://git.infradead.org/users/hch/dma-mapping.git/commitdiff/90ae409f9eb3bcaf38688f9ec22375816053a08e

This is included in v5.3-rc6
so I tested it.

No, it did not fix the problem.

--
Best Regards
Masahiro Yamada

#10 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 2 lines · Masahiro Yamada

This is included in v5.3-rc6
so I tested it.

So there is no allocation failure, but you get I/O errors later?

Does the device use a device-private CMA area? Does it work with Linux 5.2 if CONFIG_DMA_CMA is disabled?

#11 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Masahiro Yamada

This is included in v5.3-rc6
so I tested it.

So there is no allocation failure, but you get I/O errors later?

Right.

Quoted 2 lines

Does the device use a device-private CMA area?

Not sure.
My driver is drivers/mmc/host/sdhci-cadence.c
It reuses routines in drivers/mmc/host/sdhci.c

Quoted 2 lines

Does it work with Linux
5.2 if CONFIG_DMA_CMA is disabled?

No.
5.2 + disable CONFIG_DMA_CMA
failed in the same way.

Signature · 2 lines
Best Regards
Masahiro Yamada
#12 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 19 lines · Masahiro Yamada
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Masahiro Yamada

This is included in v5.3-rc6
so I tested it.

So there is no allocation failure, but you get I/O errors later?

Right.

Quoted 2 lines

Does the device use a device-private CMA area?

Not sure.
My driver is drivers/mmc/host/sdhci-cadence.c
It reuses routines in drivers/mmc/host/sdhci.c

Quoted 6 lines
Quoted 2 lines

Does it work with Linux
5.2 if CONFIG_DMA_CMA is disabled?

No.
5.2 + disable CONFIG_DMA_CMA
failed in the same way.

So it seems like the device wants CMA memory. I guess the patch
below will fix it, but that isn't the solution. Can you try it to confirm? In the end it probably assumes a dma mask it doesn't set that the CMA memory satisfies or something similar.

kernel/dma/contiguous.c +1 -1
index 69cfb4345388..bd2f24aa7f19 100644
@@ -236,7 +236,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
 
 	if (dev && dev->cma_area)
 		cma = dev->cma_area;
-	else if (count > 1)
+	else
 		cma = dma_contiguous_default_area;
 
 	/* CMA can be used only in the context which permits sleeping */
#13 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 24 lines · Christoph Hellwig
Quoted 22 lines · Masahiro Yamada
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Masahiro Yamada

This is included in v5.3-rc6
so I tested it.

So there is no allocation failure, but you get I/O errors later?

Right.

Quoted 2 lines

Does the device use a device-private CMA area?

Not sure.
My driver is drivers/mmc/host/sdhci-cadence.c
It reuses routines in drivers/mmc/host/sdhci.c

Quoted 2 lines

Does it work with Linux
5.2 if CONFIG_DMA_CMA is disabled?

Quoted 8 lines
Quoted 3 lines

No.
5.2 + disable CONFIG_DMA_CMA
failed in the same way.

So it seems like the device wants CMA memory. I guess the patch
below will fix it, but that isn't the solution. Can you try it to confirm? In the end it probably assumes a dma mask it doesn't set that the CMA memory satisfies or something similar.

Thanks for the pointer.

kernel/dma/contiguous.c struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) · line 242

Quoted 13 lines
kernel/dma/contiguous.c +1 -1
index 69cfb4345388..bd2f24aa7f19 100644
@@ -236,7 +236,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
 
        if (dev && dev->cma_area)
                cma = dev->cma_area;
-       else if (count > 1)
+       else
                cma = dma_contiguous_default_area;
 
        /* CMA can be used only in the context which permits sleeping */

Yes, this makes my driver working again
when CONFIG_DMA_CMA=y.

If I apply the following, my driver gets back working irrespective of CONFIG_DMA_CMA.

drivers/mmc/host/sdhci-cadence.c +1 -0
index 163d1cf4367e..504459395c39 100644
@@ -237,6 +237,7 @@ static const struct sdhci_ops sdhci_cdns_ops = {
 
 static const struct sdhci_pltfm_data sdhci_cdns_pltfm_data = {
        .ops = &sdhci_cdns_ops,
+       .quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
 };
 
 static int sdhci_cdns_set_tune_val(struct sdhci_host *host, unsigned int val)
Signature · 2 lines
Best Regards
Masahiro Yamada
#14 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 6 lines · Masahiro Yamada

Yes, this makes my driver working again
when CONFIG_DMA_CMA=y.

If I apply the following, my driver gets back working irrespective of CONFIG_DMA_CMA.

That sounds a lot like the device simply isn't 64-bit DMA capable, and previously always got CMA allocations under the limit it actually supported. I suggest that you submit this quirk to the mmc maintainers.

#15 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Hi Christoph,

Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Masahiro Yamada

Yes, this makes my driver working again
when CONFIG_DMA_CMA=y.

Quoted 6 lines
Quoted 2 lines

If I apply the following, my driver gets back working irrespective of CONFIG_DMA_CMA.

That sounds a lot like the device simply isn't 64-bit DMA capable, and previously always got CMA allocations under the limit it actually supported. I suggest that you submit this quirk to the mmc maintainers.

I tested v5.2 and my MMC host controller works with dma_address that exceeds 32-bit physical address.

So, I believe my MMC device is 64-bit DMA capable.

I am still looking into the code
to find out what was changed.

--
Best Regards
Masahiro Yamada

#16 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 18 lines · Masahiro Yamada

Hi Christoph,

Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Masahiro Yamada

Yes, this makes my driver working again
when CONFIG_DMA_CMA=y.

Quoted 6 lines
Quoted 2 lines

If I apply the following, my driver gets back working irrespective of CONFIG_DMA_CMA.

That sounds a lot like the device simply isn't 64-bit DMA capable, and previously always got CMA allocations under the limit it actually supported. I suggest that you submit this quirk to the mmc maintainers.

Quoted 7 lines

I tested v5.2 and my MMC host controller works with dma_address that exceeds 32-bit physical address.

So, I believe my MMC device is 64-bit DMA capable.

I am still looking into the code
to find out what was changed.

I retract this comment.

Prior to bd2e75633c8012fc8a7431c82fda66237133bf7e, the descriptor table for ADMA is placed within the 32-bit phys address range, not exceeds the 32-bit limit.

Probably, my device is not 64-bit capable.

I will talk to the hardware engineer,
and check the hardware spec just in case.

Thanks.

Signature · 2 lines
Best Regards
Masahiro Yamada
#17 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Quoted 32 lines · Masahiro Yamada
Quoted 18 lines · Masahiro Yamada

Hi Christoph,

Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Masahiro Yamada

Yes, this makes my driver working again
when CONFIG_DMA_CMA=y.

Quoted 6 lines
Quoted 2 lines

If I apply the following, my driver gets back working irrespective of CONFIG_DMA_CMA.

That sounds a lot like the device simply isn't 64-bit DMA capable, and previously always got CMA allocations under the limit it actually supported. I suggest that you submit this quirk to the mmc maintainers.

Quoted 7 lines

I tested v5.2 and my MMC host controller works with dma_address that exceeds 32-bit physical address.

So, I believe my MMC device is 64-bit DMA capable.

I am still looking into the code
to find out what was changed.

I retract this comment.

Quoted 9 lines

Prior to bd2e75633c8012fc8a7431c82fda66237133bf7e, the descriptor table for ADMA is placed within the 32-bit phys address range, not exceeds the 32-bit limit.

Probably, my device is not 64-bit capable.

I will talk to the hardware engineer,
and check the hardware spec just in case.

After looking more into my hardware,
I found out how to fix my driver:
https://lore.kernel.org/patchwork/patch/1121600/

Signature · 2 lines
Best Regards
Masahiro Yamada
#18 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

In reply to Masahiro Yamada
Quoted 117 lines · Masahiro Yamada
Quoted 11 lines · Nicolin Chen

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

However, there is also a concern that a device might care where a page comes from -- it might expect the page from CMA area and act differently if the page doesn't.

Quoted 7 lines

This patch tries to use the fallback alloc_pages path, instead of one-page size allocations from the global CMA area in case that a device does not have its own CMA area. This'd save resources from the CMA global area for more CMA allocations, and also reduce CMA fragmentations resulted from trivial allocations.

This commit (bd2e75633c8012fc8a7431c82fda66237133bf7e) broke the DMA for my MMC driver in the following way:

[    1.876755] mmc0: ADMA error
[    1.883385] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    1.889834] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    1.896284] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    1.902733] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    1.909182] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    1.915631] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    1.922081] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000fa07
[    1.928530] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    1.934981] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    1.941429] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    1.947880] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    1.954329] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    1.960778] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    1.967229] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    1.973678] mmc0: sdhci: Host ctl2: 0x00000000
[    1.978125] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    1.985271] mmc0: sdhci: ============================================
[    1.991758] mmc0: error -5 whilst initialising MMC card
[    1.991913] 43fb0000.uart: ttyS1 at MMIO 0x43fb0000 (irq = 0,
base_baud = 768000) is a 16550A
[    2.011011] hctosys: unable to open rtc device (rtc0)
[    2.017694] Freeing unused kernel memory: 2368K
[    2.027131] Run /init as init process

Starting syslogd: OK
Starting klogd: OK
Initializing random number generator... [ 2.074399] random: dd:
uninitialized urandom read (512 bytes read)
done.
Starting network: OK

[    2.109593] mmc0: ADMA error
[    2.112488] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.118941] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.125389] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.131840] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.138289] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.144738] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.151188] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x00004e47
[    2.157637] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.164087] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.170536] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.176987] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.183435] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.189886] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.196335] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.202784] mmc0: sdhci: Host ctl2: 0x00000000
[    2.207232] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.214379] mmc0: sdhci: ============================================
[    2.220881] mmc0: error -5 whilst initialising MMC card
Welcome to Buildroot
buildroot login: [    2.332786] mmc0: ADMA error
[    2.335668] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.342119] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.348568] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.355018] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.361468] mmc0: sdhci: Present:   0x01ff02f6 | Host ctl: 0x00000019
[    2.367917] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.374367] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000f447
[    2.380816] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.387267] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.393716] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.400166] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.406615] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.413065] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.419515] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.425963] mmc0: sdhci: Host ctl2: 0x00000000
[    2.430412] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.437557] mmc0: sdhci: ============================================
[    2.444031] mmc0: error -5 whilst initialising MMC card
[    2.572203] mmc0: ADMA error
[    2.575089] mmc0: sdhci: ============ SDHCI REGISTER DUMP ===========
[    2.581540] mmc0: sdhci: Sys addr:  0x00000000 | Version:  0x00000002
[    2.587989] mmc0: sdhci: Blk size:  0x00007200 | Blk cnt:  0x00000001
[    2.594439] mmc0: sdhci: Argument:  0x00000000 | Trn mode: 0x00000013
[    2.600889] mmc0: sdhci: Present:   0x01ef02f6 | Host ctl: 0x00000019
[    2.607339] mmc0: sdhci: Power:     0x0000000b | Blk gap:  0x00000000
[    2.613788] mmc0: sdhci: Wake-up:   0x00000000 | Clock:    0x0000e8c7
[    2.620237] mmc0: sdhci: Timeout:   0x0000000b | Int stat: 0x00000001
[    2.626686] mmc0: sdhci: Int enab:  0x03ff008b | Sig enab: 0x03ff008b
[    2.633137] mmc0: sdhci: ACmd stat: 0x00000000 | Slot int: 0x00000001
[    2.639586] mmc0: sdhci: Caps:      0x546ec800 | Caps_1:   0x00000000
[    2.646036] mmc0: sdhci: Cmd:       0x0000083a | Max curr: 0x00000000
[    2.652485] mmc0: sdhci: Resp[0]:   0x00000900 | Resp[1]:  0xffffffff
[    2.658936] mmc0: sdhci: Resp[2]:   0x320f5903 | Resp[3]:  0x3fd05e00
[    2.665384] mmc0: sdhci: Host ctl2: 0x00000000
[    2.669832] mmc0: sdhci: ADMA Err:  0x00000001 | ADMA Ptr: 0x000000013965b200
[    2.676979] mmc0: sdhci: ============================================
[    2.683450] mmc0: error -5 whilst initialising MMC card
Quoted 4 lines

CTRL-A Z for help | 115200 8N1 | NOR | Minicom 2.7.1 | VT102 | Offline | ttyUSB0

Reverting this commit fixed the problem.

We are having another problem with the new API and Christoph submitted a patch at: https://lkml.org/lkml/2019/8/20/86

Would it be possible for you to test to see if it can fix?

We can revert my fallback change after all, if Christoph's patch doesn't work for you either.

Thanks
Nicolin

#19 lore raw

Re: [PATCH v2 2/2] dma-contiguous: Use fallback alloc_pages for single pages

Hi Nicolin,

Quoted 10 lines · Nicolin Chen
Quoted 2 lines · Masahiro Yamada

Reverting this commit fixed the problem.

We are having another problem with the new API and Christoph submitted a patch at: https://lkml.org/lkml/2019/8/20/86

Would it be possible for you to test to see if it can fix?

It is included in 5.3-rc6

I tested 5.3-rc6 in on my board,
but I still see the same DMA fauilure.

Masahiro

Quoted 5 lines

We can revert my fallback change after all, if Christoph's patch doesn't work for you either.

Thanks
Nicolin

Signature · 2 lines
Best Regards
Masahiro Yamada
#20 lore raw
In reply to Nicolin Chen

Hi Nicolin,

modulo a trivial comment typo I found this looks fine to me. I plan to apply it with that fixed up around -rc2 time when I open the dma mapping tree opens for the the 5.3 merge window, unless someone finds an issue until then.

#21 lore raw
Quoted 4 lines · Christoph Hellwig

modulo a trivial comment typo I found this looks fine to me. I plan to apply it with that fixed up around -rc2 time when I open the dma mapping tree opens for the the 5.3 merge window, unless someone finds an issue until then.

Thanks for the help all the way.