* [PATCH] ALSA: memalloc: Fix DMA allocation retry cleanup
@ 2026-11-19 10:09 Sreeraj S Kurup
2026-11-19 10:25 ` sashiko-bot
2026-11-19 10:48 ` [PATCH v2] " Sreeraj S Kurup
0 siblings, 2 replies; 3+ messages in thread
From: Sreeraj S Kurup @ 2026-11-19 10:09 UTC (permalink / raw)
To: perex, tiwai; +Cc: linux-kernel, linux-sound, Sreeraj S Kurup
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.
Free the allocated pages before retrying and return NULL when no
suitable allocation can be obtained.
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
sound/core/memalloc.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c
index 5986e9b84ac0..9458f9cbee53 100644
--- a/sound/core/memalloc.c
+++ b/sound/core/memalloc.c
@@ -298,13 +298,17 @@ static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
return p;
if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
+ 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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] ALSA: memalloc: Fix DMA allocation retry cleanup
2026-11-19 10:09 [PATCH] ALSA: memalloc: Fix DMA allocation retry cleanup Sreeraj S Kurup
@ 2026-11-19 10:25 ` sashiko-bot
2026-11-19 10:48 ` [PATCH v2] " Sreeraj S Kurup
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-19 10:25 UTC (permalink / raw)
To: Sreeraj S Kurup; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: memalloc: GFP_DMA32 re-entry in do_alloc_pages() triggers
VM_BUG_ON
- [High] ALSA: memalloc: do_alloc_pages() fails when
dev->coherent_dma_mask is 0
- [Low] ALSA: memalloc: missing Fixes trailer in commit message
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] ALSA: memalloc: page leak and bad GFP retry in do_alloc_pages()
[1]
--
commit 8facdcf2ba9ef9e2a164a42b37b12089edb8eb1a
Author: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
> ALSA: memalloc: Fix DMA allocation retry cleanup
>
> 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.
>
> Free the allocated pages before retrying and return NULL when no
> suitable allocation can be obtained.
>
> Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
[Severity: Low]
Should this commit include a Fixes: tag for the commit that added the DMA
zone retry logic in do_alloc_pages()?
Fixes: dd164fbfdc20 ("ALSA: memalloc: Drop special handling of GFP for CONTINUOUS allocation")
> diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c
> index 5986e9b84ac0a..9458f9cbee537 100644
> --- a/sound/core/memalloc.c
> +++ b/sound/core/memalloc.c
> @@ -298,13 +298,17 @@ static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
> return p;
> if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
> if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
> + 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;
> }
[Severity: High]
This problem wasn't introduced by this patch, but when the GFP_DMA retry in
do_alloc_pages() still exceeds dev->coherent_dma_mask [1], doesn't the
first branch match !(gfp & GFP_DMA32) again because the GFP_DMA branch
cleared GFP_DMA32?
Instead of reaching the new return NULL fallback below, execution re-enters
the first branch, sets GFP_DMA | GFP_DMA32, and triggers VM_BUG_ON() on
GFP_ZONE_BAD in gfp_zone() on the next alloc_pages_exact() call. Checking
!(gfp & (GFP_DMA32 | GFP_DMA)) in the first branch avoids re-entering it
after GFP_DMA has been tried.
> + free_pages_exact(p, size);
> + return NULL;
> }
[Severity: High]
Does returning NULL here cause do_alloc_pages() to unconditionally fail
when a caller passes a non-NULL struct device whose coherent_dma_mask is 0?
When dev->coherent_dma_mask is 0, ~dev->coherent_dma_mask is all ones, so
every retry fails the mask check and now hits this return NULL. For
example, tascam_init_pcm() passes tascam->dev->dev.parent (a USB hub device
with coherent_dma_mask == 0) with SNDRV_DMA_TYPE_CONTINUOUS:
sound/usb/usx2y/us144mkii_pcm.c:tascam_init_pcm() {
...
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
tascam->dev->dev.parent, 64 * 1024,
tascam_pcm_hw.buffer_bytes_max);
...
}
With this regression, PCM buffer allocation for the TASCAM US-144MKII fails
with -ENOMEM. Should do_alloc_pages() check if (!dev ||
!dev->coherent_dma_mask) before enforcing coherent_dma_mask?
[1] https://sashiko.dev/#/bug/linux-189ef083-a418-4b5d-b005-2d24356f0b93
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100928.2740-1-sreekuttan2156239@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2] ALSA: memalloc: Fix DMA allocation retry cleanup
2026-11-19 10:09 [PATCH] ALSA: memalloc: Fix DMA allocation retry cleanup Sreeraj S Kurup
2026-11-19 10:25 ` sashiko-bot
@ 2026-11-19 10:48 ` Sreeraj S Kurup
1 sibling, 0 replies; 3+ messages in thread
From: Sreeraj S Kurup @ 2026-11-19 10:48 UTC (permalink / raw)
To: sreekuttan2156239, perex, tiwai; +Cc: linux-kernel, linux-sound
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@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 10:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 10:09 [PATCH] ALSA: memalloc: Fix DMA allocation retry cleanup Sreeraj S Kurup
2026-11-19 10:25 ` sashiko-bot
2026-11-19 10:48 ` [PATCH v2] " Sreeraj S Kurup
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox