iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] dma-pool: Fix atomic pool selection
@ 2020-07-14 12:39 Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 12:39 UTC (permalink / raw)
  To: hch, linux-kernel
  Cc: jeremy.linton, iommu, linux-rpi-kernel, David Rientjes, Robin Murphy

This is my attempt at fixing one of the regressions we've seen[1] after
the introduction of per-zone atomic pools.

This combined with "dma-pool: Do not allocate pool memory from CMA"[2]
should fix the boot issues on Jeremy's RPi4 setup.

[1] https://lkml.org/lkml/2020/7/2/974
[2] https://lkml.org/lkml/2020/7/8/1108

---

Changes since v1:
 - Rebase into linus' master

Nicolas Saenz Julienne (4):
  dma-direct: Provide function to check physical memory area validity
  dma-pool: Get rid of dma_in_atomic_pool()
  dma-pool: Introduce dma_guess_pool()
  dma-pool: Make sure atomic pool suits device

 include/linux/dma-direct.h |  1 +
 kernel/dma/direct.c        |  2 +-
 kernel/dma/pool.c          | 80 +++++++++++++++++++++++++-------------
 3 files changed, 56 insertions(+), 27 deletions(-)

-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* [PATCH v2 1/4] dma-direct: Provide function to check physical memory area validity
  2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
@ 2020-07-14 12:39 ` Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 12:39 UTC (permalink / raw)
  To: hch, linux-kernel, Marek Szyprowski, Robin Murphy
  Cc: iommu, linux-rpi-kernel, jeremy.linton

dma_coherent_ok() checks if a physical memory area fits a device's DMA
constraints.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---
 include/linux/dma-direct.h | 1 +
 kernel/dma/direct.c        | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h
index 5184735a0fe8..ab2e20cba951 100644
--- a/include/linux/dma-direct.h
+++ b/include/linux/dma-direct.h
@@ -69,6 +69,7 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
 u64 dma_direct_get_required_mask(struct device *dev);
 gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
 				  u64 *phys_mask);
+bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size);
 void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 		gfp_t gfp, unsigned long attrs);
 void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 95866b647581..67f060b86a73 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -70,7 +70,7 @@ gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
 	return 0;
 }
 
-static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
+bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 {
 	return phys_to_dma_direct(dev, phys) + size - 1 <=
 			min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit);
-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* [PATCH v2 2/4] dma-pool: Get rid of dma_in_atomic_pool()
  2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
@ 2020-07-14 12:39 ` Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 12:39 UTC (permalink / raw)
  To: hch, linux-kernel, Marek Szyprowski, Robin Murphy
  Cc: iommu, linux-rpi-kernel, jeremy.linton

The function is only used once and can be simplified to a one-liner.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---
 kernel/dma/pool.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 39ca26fa41b5..318035e093fb 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -217,15 +217,6 @@ static inline struct gen_pool *dev_to_pool(struct device *dev)
 	return atomic_pool_kernel;
 }
 
-static bool dma_in_atomic_pool(struct device *dev, void *start, size_t size)
-{
-	struct gen_pool *pool = dev_to_pool(dev);
-
-	if (unlikely(!pool))
-		return false;
-	return gen_pool_has_addr(pool, (unsigned long)start, size);
-}
-
 void *dma_alloc_from_pool(struct device *dev, size_t size,
 			  struct page **ret_page, gfp_t flags)
 {
@@ -260,7 +251,7 @@ bool dma_free_from_pool(struct device *dev, void *start, size_t size)
 {
 	struct gen_pool *pool = dev_to_pool(dev);
 
-	if (!dma_in_atomic_pool(dev, start, size))
+	if (!pool || !gen_pool_has_addr(pool, (unsigned long)start, size))
 		return false;
 	gen_pool_free(pool, (unsigned long)start, size);
 	return true;
-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* [PATCH v2 3/4] dma-pool: Introduce dma_guess_pool()
  2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
@ 2020-07-14 12:39 ` Nicolas Saenz Julienne
  2020-07-14 12:39 ` [PATCH v2 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
  2020-07-15  7:12 ` [PATCH v2 0/4] dma-pool: Fix atomic pool selection Christoph Hellwig
  4 siblings, 0 replies; 6+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 12:39 UTC (permalink / raw)
  To: hch, linux-kernel, Marek Szyprowski, Robin Murphy
  Cc: iommu, linux-rpi-kernel, jeremy.linton

dma-pool's dev_to_pool() creates the false impression that there is a
way to grantee a mapping between a device's DMA constraints and an
atomic pool. It tuns out it's just a guess, and the device might need to
use an atomic pool containing memory from a 'safer' (or lower) memory
zone.

To help mitigate this, introduce dma_guess_pool() which can be fed a
device's DMA constraints and atomic pools already known to be faulty, in
order for it to provide an better guess on which pool to use.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---
 kernel/dma/pool.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 318035e093fb..5b9eaa2b498d 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -203,7 +203,7 @@ static int __init dma_atomic_pool_init(void)
 }
 postcore_initcall(dma_atomic_pool_init);
 
-static inline struct gen_pool *dev_to_pool(struct device *dev)
+static inline struct gen_pool *dma_guess_pool_from_device(struct device *dev)
 {
 	u64 phys_mask;
 	gfp_t gfp;
@@ -217,10 +217,30 @@ static inline struct gen_pool *dev_to_pool(struct device *dev)
 	return atomic_pool_kernel;
 }
 
+static inline struct gen_pool *dma_get_safer_pool(struct gen_pool *bad_pool)
+{
+	if (bad_pool == atomic_pool_kernel)
+		return atomic_pool_dma32 ? : atomic_pool_dma;
+
+	if (bad_pool == atomic_pool_dma32)
+		return atomic_pool_dma;
+
+	return NULL;
+}
+
+static inline struct gen_pool *dma_guess_pool(struct device *dev,
+					      struct gen_pool *bad_pool)
+{
+	if (bad_pool)
+		return dma_get_safer_pool(bad_pool);
+
+	return dma_guess_pool_from_device(dev);
+}
+
 void *dma_alloc_from_pool(struct device *dev, size_t size,
 			  struct page **ret_page, gfp_t flags)
 {
-	struct gen_pool *pool = dev_to_pool(dev);
+	struct gen_pool *pool = dma_guess_pool(dev, NULL);
 	unsigned long val;
 	void *ptr = NULL;
 
@@ -249,7 +269,7 @@ void *dma_alloc_from_pool(struct device *dev, size_t size,
 
 bool dma_free_from_pool(struct device *dev, void *start, size_t size)
 {
-	struct gen_pool *pool = dev_to_pool(dev);
+	struct gen_pool *pool = dma_guess_pool(dev, NULL);
 
 	if (!pool || !gen_pool_has_addr(pool, (unsigned long)start, size))
 		return false;
-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* [PATCH v2 4/4] dma-pool: Make sure atomic pool suits device
  2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
                   ` (2 preceding siblings ...)
  2020-07-14 12:39 ` [PATCH v2 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
@ 2020-07-14 12:39 ` Nicolas Saenz Julienne
  2020-07-15  7:12 ` [PATCH v2 0/4] dma-pool: Fix atomic pool selection Christoph Hellwig
  4 siblings, 0 replies; 6+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 12:39 UTC (permalink / raw)
  To: hch, linux-kernel, Marek Szyprowski, Robin Murphy, David Rientjes
  Cc: iommu, linux-rpi-kernel, jeremy.linton

When allocating DMA memory from a pool, the core can only guess which
atomic pool will fit a device's constraints. If it doesn't, get a safer
atomic pool and try again.

Fixes: c84dc6e68a1d ("dma-pool: add additional coherent pools to map to gfp mask")
Reported-by: Jeremy Linton <jeremy.linton@arm.com>
Suggested-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---

Changes since v1:
 - Rebase to linus' master

 kernel/dma/pool.c | 57 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 20 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 5b9eaa2b498d..d48d9acb585f 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -240,39 +240,56 @@ static inline struct gen_pool *dma_guess_pool(struct device *dev,
 void *dma_alloc_from_pool(struct device *dev, size_t size,
 			  struct page **ret_page, gfp_t flags)
 {
-	struct gen_pool *pool = dma_guess_pool(dev, NULL);
-	unsigned long val;
+	struct gen_pool *pool = NULL;
+	unsigned long val = 0;
 	void *ptr = NULL;
-
-	if (!pool) {
-		WARN(1, "%pGg atomic pool not initialised!\n", &flags);
-		return NULL;
+	phys_addr_t phys;
+
+	while (1) {
+		pool = dma_guess_pool(dev, pool);
+		if (!pool) {
+			WARN(1, "Failed to get suitable pool for %s\n",
+			     dev_name(dev));
+			break;
+		}
+
+		val = gen_pool_alloc(pool, size);
+		if (!val)
+			continue;
+
+		phys = gen_pool_virt_to_phys(pool, val);
+		if (dma_coherent_ok(dev, phys, size))
+			break;
+
+		gen_pool_free(pool, val, size);
+		val = 0;
 	}
 
-	val = gen_pool_alloc(pool, size);
-	if (likely(val)) {
-		phys_addr_t phys = gen_pool_virt_to_phys(pool, val);
 
+	if (val) {
 		*ret_page = pfn_to_page(__phys_to_pfn(phys));
 		ptr = (void *)val;
 		memset(ptr, 0, size);
-	} else {
-		WARN_ONCE(1, "DMA coherent pool depleted, increase size "
-			     "(recommended min coherent_pool=%zuK)\n",
-			  gen_pool_size(pool) >> 9);
+
+		if (gen_pool_avail(pool) < atomic_pool_size)
+			schedule_work(&atomic_pool_work);
 	}
-	if (gen_pool_avail(pool) < atomic_pool_size)
-		schedule_work(&atomic_pool_work);
 
 	return ptr;
 }
 
 bool dma_free_from_pool(struct device *dev, void *start, size_t size)
 {
-	struct gen_pool *pool = dma_guess_pool(dev, NULL);
+	struct gen_pool *pool = NULL;
 
-	if (!pool || !gen_pool_has_addr(pool, (unsigned long)start, size))
-		return false;
-	gen_pool_free(pool, (unsigned long)start, size);
-	return true;
+	while (1) {
+		pool = dma_guess_pool(dev, pool);
+		if (!pool)
+			return false;
+
+		if (gen_pool_has_addr(pool, (unsigned long)start, size)) {
+			gen_pool_free(pool, (unsigned long)start, size);
+			return true;
+		}
+	}
 }
-- 
2.27.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v2 0/4] dma-pool: Fix atomic pool selection
  2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
                   ` (3 preceding siblings ...)
  2020-07-14 12:39 ` [PATCH v2 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
@ 2020-07-15  7:12 ` Christoph Hellwig
  4 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2020-07-15  7:12 UTC (permalink / raw)
  To: Nicolas Saenz Julienne
  Cc: linux-kernel, jeremy.linton, iommu, linux-rpi-kernel,
	David Rientjes, Robin Murphy, hch

On Tue, Jul 14, 2020 at 02:39:24PM +0200, Nicolas Saenz Julienne wrote:
> This is my attempt at fixing one of the regressions we've seen[1] after
> the introduction of per-zone atomic pools.
> 
> This combined with "dma-pool: Do not allocate pool memory from CMA"[2]
> should fix the boot issues on Jeremy's RPi4 setup.

I've applied the series as well as that patch to the dma-mapping tree,
thanks.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

end of thread, other threads:[~2020-07-15  7:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-14 12:39 [PATCH v2 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
2020-07-14 12:39 ` [PATCH v2 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
2020-07-14 12:39 ` [PATCH v2 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
2020-07-14 12:39 ` [PATCH v2 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
2020-07-14 12:39 ` [PATCH v2 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
2020-07-15  7:12 ` [PATCH v2 0/4] dma-pool: Fix atomic pool selection 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).