[PATCH v2] ALSA: memalloc: Fix DMA allocation retry cleanup

From: Sreeraj S Kurup

Date: Thu Nov 19 2026 - 05:49:42 EST


do_alloc_pages() retries allocation with lower DMA zones when the
allocated address exceeds the device's coherent DMA mask.

However, the previous allocation is not freed before retrying,
leaking the allocated pages.

Also, if no suitable DMA zone is available, the function can return
an allocation outside the device's DMA mask.

Avoid retrying GFP_DMA32 after GFP_DMA has been selected, and skip
the DMA mask check when the coherent DMA mask is unset.

Free the allocated pages before retrying and return NULL when no
suitable allocation can be obtained.

Fixes: dd164fbfdc20 ("ALSA: memalloc: Drop special handling of GFP for CONTINUOUS allocation")
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@xxxxxxxxx>
---
v2:
- Avoid retrying with GFP_DMA32 after GFP_DMA has been selected.
- Skip DMA mask validation when coherent_dma_mask is unset.
- Add Fixes trailer.

sound/core/memalloc.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c
index 5986e9b84ac0..8fec4a8fd219 100644
--- a/sound/core/memalloc.c
+++ b/sound/core/memalloc.c
@@ -294,17 +294,21 @@ static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
if (!p)
return NULL;
*addr = page_to_phys(virt_to_page(p));
- if (!dev)
+ if (!dev || !dev->coherent_dma_mask)
return p;
if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
- if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
+ if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & (GFP_DMA32 | GFP_DMA))) {
+ free_pages_exact(p, size);
gfp |= GFP_DMA32;
goto again;
}
if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
+ free_pages_exact(p, size);
gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
goto again;
}
+ free_pages_exact(p, size);
+ return NULL;
}
#ifdef CONFIG_X86
if (wc)
--
2.55.0