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

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

---

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          | 76 +++++++++++++++++++++++++++-----------
 3 files changed, 56 insertions(+), 23 deletions(-)

-- 
2.27.0


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

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

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 cdfa400f89b3..cb23a8305132 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 93f578a8e613..4de864cacd22 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


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

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

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 8cfa01243ed2..7363640fc91c 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)
 {
@@ -256,7 +247,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


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

* [PATCH 3/4] dma-pool: Introduce dma_guess_pool()
  2020-07-09 16:19 [PATCH 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
  2020-07-09 16:19 ` [PATCH 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
  2020-07-09 16:19 ` [PATCH 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
@ 2020-07-09 16:19 ` Nicolas Saenz Julienne
  2020-07-14 11:22   ` Christoph Hellwig
  2020-07-09 16:19 ` [PATCH 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
  2020-07-10 18:52 ` [PATCH 0/4] dma-pool: Fix atomic pool selection Jeremy Linton
  4 siblings, 1 reply; 10+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-09 16:19 UTC (permalink / raw)
  To: hch, Robin Murphy, David Rientjes, iommu, Marek Szyprowski
  Cc: linux-rpi-kernel, jeremy.linton, Nicolas Saenz Julienne, linux-kernel

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 or an atomic pool already known to be faulty in
order for it to provide a better guess at 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 7363640fc91c..3d518de07617 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;
 
@@ -245,7 +265,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


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

* [PATCH 4/4] dma-pool: Make sure atomic pool suits device
  2020-07-09 16:19 [PATCH 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
                   ` (2 preceding siblings ...)
  2020-07-09 16:19 ` [PATCH 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
@ 2020-07-09 16:19 ` Nicolas Saenz Julienne
  2020-07-10 18:52 ` [PATCH 0/4] dma-pool: Fix atomic pool selection Jeremy Linton
  4 siblings, 0 replies; 10+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-09 16:19 UTC (permalink / raw)
  To: hch, Robin Murphy, David Rientjes, iommu, Marek Szyprowski
  Cc: linux-rpi-kernel, jeremy.linton, Nicolas Saenz Julienne, linux-kernel

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>
---
 kernel/dma/pool.c | 53 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 37 insertions(+), 16 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 3d518de07617..d48d9acb585f 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -240,35 +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 (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);
+
+		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;
+
+	while (1) {
+		pool = dma_guess_pool(dev, pool);
+		if (!pool)
+			return false;
 
-	if (!pool || !gen_pool_has_addr(pool, (unsigned long)start, size))
-		return false;
-	gen_pool_free(pool, (unsigned long)start, size);
-	return true;
+		if (gen_pool_has_addr(pool, (unsigned long)start, size)) {
+			gen_pool_free(pool, (unsigned long)start, size);
+			return true;
+		}
+	}
 }
-- 
2.27.0


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

* Re: [PATCH 2/4] dma-pool: Get rid of dma_in_atomic_pool()
  2020-07-09 16:19 ` [PATCH 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
@ 2020-07-09 21:51   ` David Rientjes
  2020-07-10  6:50     ` Christoph Hellwig
  0 siblings, 1 reply; 10+ messages in thread
From: David Rientjes @ 2020-07-09 21:51 UTC (permalink / raw)
  To: Nicolas Saenz Julienne
  Cc: hch, Robin Murphy, iommu, Marek Szyprowski, linux-rpi-kernel,
	jeremy.linton, linux-kernel

On Thu, 9 Jul 2020, Nicolas Saenz Julienne wrote:

> The function is only used once and can be simplified to a one-liner.
> 
> Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>

I'll leave this one to Christoph to decide on.  One thing I really liked 
about hacking around in kernel/dma is the coding style, it really follows 
"one function does one thing and does it well" even if there is only one 
caller.  dma_in_atomic_pool() was an attempt to follow in those footsteps.

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

* Re: [PATCH 2/4] dma-pool: Get rid of dma_in_atomic_pool()
  2020-07-09 21:51   ` David Rientjes
@ 2020-07-10  6:50     ` Christoph Hellwig
  0 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2020-07-10  6:50 UTC (permalink / raw)
  To: David Rientjes
  Cc: Nicolas Saenz Julienne, hch, Robin Murphy, iommu,
	Marek Szyprowski, linux-rpi-kernel, jeremy.linton, linux-kernel

On Thu, Jul 09, 2020 at 02:51:13PM -0700, David Rientjes wrote:
> On Thu, 9 Jul 2020, Nicolas Saenz Julienne wrote:
> 
> > The function is only used once and can be simplified to a one-liner.
> > 
> > Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
> 
> I'll leave this one to Christoph to decide on.  One thing I really liked 
> about hacking around in kernel/dma is the coding style, it really follows 
> "one function does one thing and does it well" even if there is only one 
> caller.  dma_in_atomic_pool() was an attempt to follow in those footsteps.

While I like the helper aswell, I don't see how it could work nicely
with the changes in patch 4.

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

* Re: [PATCH 0/4] dma-pool: Fix atomic pool selection
  2020-07-09 16:19 [PATCH 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
                   ` (3 preceding siblings ...)
  2020-07-09 16:19 ` [PATCH 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
@ 2020-07-10 18:52 ` Jeremy Linton
  4 siblings, 0 replies; 10+ messages in thread
From: Jeremy Linton @ 2020-07-10 18:52 UTC (permalink / raw)
  To: Nicolas Saenz Julienne, hch, Robin Murphy, David Rientjes, iommu
  Cc: linux-rpi-kernel, linux-kernel

Hi,


I have merged this to a 5.8 tree along with "dma-pool: Only allocate 
from CMA when in the same memory zone" and tested it in various ACPI/DT 
combinations, particularly on the RPI4. It seems to be working fine.

So thanks for your time and effort clearing this up!

Tested-by: Jeremy Linton <jeremy.linton@arm.com>


On 7/9/20 11:19 AM, 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.
> 
> [1] https://lkml.org/lkml/2020/7/2/974
> [2] https://lkml.org/lkml/2020/7/8/1108
> 
> ---
> 
> 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          | 76 +++++++++++++++++++++++++++-----------
>   3 files changed, 56 insertions(+), 23 deletions(-)
> 


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

* Re: [PATCH 3/4] dma-pool: Introduce dma_guess_pool()
  2020-07-09 16:19 ` [PATCH 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
@ 2020-07-14 11:22   ` Christoph Hellwig
  2020-07-14 11:40     ` Nicolas Saenz Julienne
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2020-07-14 11:22 UTC (permalink / raw)
  To: Nicolas Saenz Julienne
  Cc: hch, Robin Murphy, David Rientjes, iommu, Marek Szyprowski,
	linux-rpi-kernel, jeremy.linton, linux-kernel

This one doesn't appear to actually apply on top of Linus' tree plus the
two previous patches.

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

* Re: [PATCH 3/4] dma-pool: Introduce dma_guess_pool()
  2020-07-14 11:22   ` Christoph Hellwig
@ 2020-07-14 11:40     ` Nicolas Saenz Julienne
  0 siblings, 0 replies; 10+ messages in thread
From: Nicolas Saenz Julienne @ 2020-07-14 11:40 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Robin Murphy, David Rientjes, iommu, Marek Szyprowski,
	linux-rpi-kernel, jeremy.linton, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 189 bytes --]

On Tue, 2020-07-14 at 13:22 +0200, Christoph Hellwig wrote:
> This one doesn't appear to actually apply on top of Linus' tree plus the
> two previous patches.

I'll take care of it.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2020-07-14 11:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-09 16:19 [PATCH 0/4] dma-pool: Fix atomic pool selection Nicolas Saenz Julienne
2020-07-09 16:19 ` [PATCH 1/4] dma-direct: Provide function to check physical memory area validity Nicolas Saenz Julienne
2020-07-09 16:19 ` [PATCH 2/4] dma-pool: Get rid of dma_in_atomic_pool() Nicolas Saenz Julienne
2020-07-09 21:51   ` David Rientjes
2020-07-10  6:50     ` Christoph Hellwig
2020-07-09 16:19 ` [PATCH 3/4] dma-pool: Introduce dma_guess_pool() Nicolas Saenz Julienne
2020-07-14 11:22   ` Christoph Hellwig
2020-07-14 11:40     ` Nicolas Saenz Julienne
2020-07-09 16:19 ` [PATCH 4/4] dma-pool: Make sure atomic pool suits device Nicolas Saenz Julienne
2020-07-10 18:52 ` [PATCH 0/4] dma-pool: Fix atomic pool selection Jeremy Linton

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).