linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] dma-mapping: Improve atomic pool behaviour
@ 2026-01-12 15:46 Robin Murphy
  2026-01-12 15:46 ` [PATCH 1/3] dma/pool: Improve pool lookup Robin Murphy
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robin Murphy @ 2026-01-12 15:46 UTC (permalink / raw)
  To: m.szyprowski, akpm, vbabka, david
  Cc: bhe, iommu, linux-mm, vladimir.kondratiev, s-adivi, linux-kernel,
	lorenzo.stoakes, Liam.Howlett, rppt, surenb, mhocko, jackmanb,
	hannes, ziy

Hi all,

These days we have somewhat of a matrix of mm zones that may or may not
exist and may or may not be empty, which the per-zone atomic pool logic
doesn't actually handle all that well. It's not all that difficult to
improve robustness and reduce redundancy, so that's what this series
aims for.

I initially wrote this just because I happened to be looking through the
boot log of one of my boards and had one of those out-of-the-blue
realisations of "hang on, why *is* it allocating pools for zones it
doesn't even have memory in?", but coincidentally, it seems that others
happened to notice related aspects around the same time[1][2]. This is
my attempt to sort it all out properly.

I guess this could go via either the dma-mapping or mm tree, whichever
maintainers prefer.

Thanks,
Robin.


[1] https://lore.kernel.org/linux-iommu/20260112065857.3137331-1-vladimir.kondratiev@mobileye.com/
[2] https://lore.kernel.org/linux-iommu/20260112104749.4132641-1-s-adivi@ti.com/

Robin Murphy (3):
  dma/pool: Improve pool lookup
  mm_zone: Generalise has_managed_dma()
  dma/pool: Avoid allocating redundant pools

 include/linux/mmzone.h |  9 +++++----
 kernel/dma/pool.c      | 27 ++++++++++++++++++---------
 mm/page_alloc.c        |  8 ++------
 3 files changed, 25 insertions(+), 19 deletions(-)

-- 
2.34.1



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

* [PATCH 1/3] dma/pool: Improve pool lookup
  2026-01-12 15:46 [PATCH 0/3] dma-mapping: Improve atomic pool behaviour Robin Murphy
@ 2026-01-12 15:46 ` Robin Murphy
  2026-01-12 15:46 ` [PATCH 2/3] mm_zone: Generalise has_managed_dma() Robin Murphy
  2026-01-12 15:46 ` [PATCH 3/3] dma/pool: Avoid allocating redundant pools Robin Murphy
  2 siblings, 0 replies; 5+ messages in thread
From: Robin Murphy @ 2026-01-12 15:46 UTC (permalink / raw)
  To: m.szyprowski, akpm, vbabka, david
  Cc: bhe, iommu, linux-mm, vladimir.kondratiev, s-adivi, linux-kernel,
	lorenzo.stoakes, Liam.Howlett, rppt, surenb, mhocko, jackmanb,
	hannes, ziy

If CONFIG_ZONE_DMA32 is enabled, but we have not allocated the
corresponding atomic_pool_dma32, dma_guess_pool() may return the NULL
value of that and fail a GFP_DMA32 allocation without trying to fall
back to other pools which may exist. Furthermore, if no GFP_DMA pool
exists, it is preferable to try GFP_DMA32 rather than immediately fall
back to GFP_KERNEL with even less chance of success. Improve matters
by encoding an explicit order of pool preference for each flag.

Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 kernel/dma/pool.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 26392badc36b..2645cfb5718b 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -224,10 +224,10 @@ postcore_initcall(dma_atomic_pool_init);
 static inline struct gen_pool *dma_guess_pool(struct gen_pool *prev, gfp_t gfp)
 {
 	if (prev == NULL) {
-		if (IS_ENABLED(CONFIG_ZONE_DMA32) && (gfp & GFP_DMA32))
-			return atomic_pool_dma32;
-		if (atomic_pool_dma && (gfp & GFP_DMA))
-			return atomic_pool_dma;
+		if (gfp & GFP_DMA)
+			return atomic_pool_dma ?: atomic_pool_dma32 ?: atomic_pool_kernel;
+		if (gfp & GFP_DMA32)
+			return atomic_pool_dma32 ?: atomic_pool_dma ?: atomic_pool_kernel;
 		return atomic_pool_kernel;
 	}
 	if (prev == atomic_pool_kernel)
-- 
2.34.1



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

* [PATCH 2/3] mm_zone: Generalise has_managed_dma()
  2026-01-12 15:46 [PATCH 0/3] dma-mapping: Improve atomic pool behaviour Robin Murphy
  2026-01-12 15:46 ` [PATCH 1/3] dma/pool: Improve pool lookup Robin Murphy
@ 2026-01-12 15:46 ` Robin Murphy
  2026-01-12 19:32   ` David Hildenbrand (Red Hat)
  2026-01-12 15:46 ` [PATCH 3/3] dma/pool: Avoid allocating redundant pools Robin Murphy
  2 siblings, 1 reply; 5+ messages in thread
From: Robin Murphy @ 2026-01-12 15:46 UTC (permalink / raw)
  To: m.szyprowski, akpm, vbabka, david
  Cc: bhe, iommu, linux-mm, vladimir.kondratiev, s-adivi, linux-kernel,
	lorenzo.stoakes, Liam.Howlett, rppt, surenb, mhocko, jackmanb,
	hannes, ziy

It would be useful to be able to check for potential DMA pages beyond
just ZONE_DMA - generalise the existing has_managed_dma() function to
allow checking other zones too.

Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 include/linux/mmzone.h | 9 +++++----
 mm/page_alloc.c        | 8 ++------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 75ef7c9f9307..fc5d6c88d2f0 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -1648,14 +1648,15 @@ static inline int is_highmem(const struct zone *zone)
 	return is_highmem_idx(zone_idx(zone));
 }
 
-#ifdef CONFIG_ZONE_DMA
-bool has_managed_dma(void);
-#else
+bool has_managed_zone(enum zone_type zone);
 static inline bool has_managed_dma(void)
 {
+#ifdef CONFIG_ZONE_DMA
+	return has_managed_zone(ZONE_DMA);
+#else
 	return false;
-}
 #endif
+}
 
 
 #ifndef CONFIG_NUMA
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 822e05f1a964..36ccc85c5073 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -7418,20 +7418,16 @@ bool put_page_back_buddy(struct page *page)
 }
 #endif
 
-#ifdef CONFIG_ZONE_DMA
-bool has_managed_dma(void)
+bool has_managed_zone(enum zone_type zone)
 {
 	struct pglist_data *pgdat;
 
 	for_each_online_pgdat(pgdat) {
-		struct zone *zone = &pgdat->node_zones[ZONE_DMA];
-
-		if (managed_zone(zone))
+		if (managed_zone(&pgdat->node_zones[zone]))
 			return true;
 	}
 	return false;
 }
-#endif /* CONFIG_ZONE_DMA */
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
 
-- 
2.34.1



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

* [PATCH 3/3] dma/pool: Avoid allocating redundant pools
  2026-01-12 15:46 [PATCH 0/3] dma-mapping: Improve atomic pool behaviour Robin Murphy
  2026-01-12 15:46 ` [PATCH 1/3] dma/pool: Improve pool lookup Robin Murphy
  2026-01-12 15:46 ` [PATCH 2/3] mm_zone: Generalise has_managed_dma() Robin Murphy
@ 2026-01-12 15:46 ` Robin Murphy
  2 siblings, 0 replies; 5+ messages in thread
From: Robin Murphy @ 2026-01-12 15:46 UTC (permalink / raw)
  To: m.szyprowski, akpm, vbabka, david
  Cc: bhe, iommu, linux-mm, vladimir.kondratiev, s-adivi, linux-kernel,
	lorenzo.stoakes, Liam.Howlett, rppt, surenb, mhocko, jackmanb,
	hannes, ziy

On smaller systems, e.g. embedded arm64, it is common for all memory
to end up in ZONE_DMA32 or even ZONE_DMA. In such cases it is redundant
to allocate a nominal pool for an empty higher zone that just ends up
coming from a lower zone that should already have its own pool anyway.
We already have logic to skip allocating a ZONE_DMA pool when that is
empty, so generalise that to save memory in the case of other zones too.

Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 kernel/dma/pool.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 2645cfb5718b..c5da29ad010c 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -184,6 +184,12 @@ static __init struct gen_pool *__dma_atomic_pool_init(size_t pool_size,
 	return pool;
 }
 
+#ifdef CONFIG_ZONE_DMA32
+#define has_managed_dma32 has_managed_zone(ZONE_DMA32)
+#else
+#define has_managed_dma32 false
+#endif
+
 static int __init dma_atomic_pool_init(void)
 {
 	int ret = 0;
@@ -199,17 +205,20 @@ static int __init dma_atomic_pool_init(void)
 	}
 	INIT_WORK(&atomic_pool_work, atomic_pool_work_fn);
 
-	atomic_pool_kernel = __dma_atomic_pool_init(atomic_pool_size,
+	/* All memory might be in the DMA zone(s) to begin with */
+	if (has_managed_zone(ZONE_NORMAL)) {
+		atomic_pool_kernel = __dma_atomic_pool_init(atomic_pool_size,
 						    GFP_KERNEL);
-	if (!atomic_pool_kernel)
-		ret = -ENOMEM;
+		if (!atomic_pool_kernel)
+			ret = -ENOMEM;
+	}
 	if (has_managed_dma()) {
 		atomic_pool_dma = __dma_atomic_pool_init(atomic_pool_size,
 						GFP_KERNEL | GFP_DMA);
 		if (!atomic_pool_dma)
 			ret = -ENOMEM;
 	}
-	if (IS_ENABLED(CONFIG_ZONE_DMA32)) {
+	if (has_managed_dma32) {
 		atomic_pool_dma32 = __dma_atomic_pool_init(atomic_pool_size,
 						GFP_KERNEL | GFP_DMA32);
 		if (!atomic_pool_dma32)
@@ -228,7 +237,7 @@ static inline struct gen_pool *dma_guess_pool(struct gen_pool *prev, gfp_t gfp)
 			return atomic_pool_dma ?: atomic_pool_dma32 ?: atomic_pool_kernel;
 		if (gfp & GFP_DMA32)
 			return atomic_pool_dma32 ?: atomic_pool_dma ?: atomic_pool_kernel;
-		return atomic_pool_kernel;
+		return atomic_pool_kernel ?: atomic_pool_dma32 ?: atomic_pool_dma;
 	}
 	if (prev == atomic_pool_kernel)
 		return atomic_pool_dma32 ? atomic_pool_dma32 : atomic_pool_dma;
-- 
2.34.1



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

* Re: [PATCH 2/3] mm_zone: Generalise has_managed_dma()
  2026-01-12 15:46 ` [PATCH 2/3] mm_zone: Generalise has_managed_dma() Robin Murphy
@ 2026-01-12 19:32   ` David Hildenbrand (Red Hat)
  0 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Red Hat) @ 2026-01-12 19:32 UTC (permalink / raw)
  To: Robin Murphy, m.szyprowski, akpm, vbabka
  Cc: bhe, iommu, linux-mm, vladimir.kondratiev, s-adivi, linux-kernel,
	lorenzo.stoakes, Liam.Howlett, rppt, surenb, mhocko, jackmanb,
	hannes, ziy

On 1/12/26 16:46, Robin Murphy wrote:
> It would be useful to be able to check for potential DMA pages beyond
> just ZONE_DMA - generalise the existing has_managed_dma() function to
> allow checking other zones too.
> 
> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> ---

Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>

-- 
Cheers

David


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

end of thread, other threads:[~2026-01-12 19:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-12 15:46 [PATCH 0/3] dma-mapping: Improve atomic pool behaviour Robin Murphy
2026-01-12 15:46 ` [PATCH 1/3] dma/pool: Improve pool lookup Robin Murphy
2026-01-12 15:46 ` [PATCH 2/3] mm_zone: Generalise has_managed_dma() Robin Murphy
2026-01-12 19:32   ` David Hildenbrand (Red Hat)
2026-01-12 15:46 ` [PATCH 3/3] dma/pool: Avoid allocating redundant pools Robin Murphy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox