Skip to contentSkip to conversation

ALSA: memalloc: Fixes for sync check and SG helper

PATCH New Takashi Iwai 1 participant 3 messages Feb 10, 2022 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: memalloc: Fixes for sync check and SG helper

The recent changes and additions in ALSA core memalloc helpers had incorrect usages of dma_need_sync() and the order of SG page invalidation. This patch corrects those.

Takashi

===

Takashi Iwai (2):
ALSA: memalloc: Fix dma_need_sync() checks
ALSA: memalloc: invalidate SG pages before sync

 sound/core/memalloc.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 ALSA: memalloc: Fix dma_need_sync() checks

dma_need_sync() checks each DMA address. Fix the incorrect usages for non-contiguous and non-coherent page allocations. Fortunately, there are no actual call sites that need manual syncs yet.

Fixes a25684a95646 ("ALSA: memalloc: Support for non-contiguous page allocation")
Fixes 73325f60e2ed ("ALSA: memalloc: Support for non-coherent page allocation")
---
 sound/core/memalloc.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
sound/core/memalloc.c +9 -4
index d1fcd1d5adae..f98f694e3ce4 100644
@@ -511,7 +511,8 @@ static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
 				      DEFAULT_GFP, 0);
 	if (!sgt)
 		return NULL;
-	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
+	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
+					    sg_dma_address(sgt->sgl));
 	p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
 	if (p)
 		dmab->private_data = sgt;
@@ -671,9 +672,13 @@ static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
  */
 static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
 {
-	dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->dev.dir);
-	return dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
-				     dmab->dev.dir, DEFAULT_GFP);
+	void *p;
+
+	p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
+				  dmab->dev.dir, DEFAULT_GFP);
+	if (p)
+		dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
+	return p;
 }
 
 static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/2 ALSA: memalloc: invalidate SG pages before sync

In reply to Takashi Iwai

It seems that calling invalidate_kernel_vmap_range() is more correct to be called before dma_sync_*(), judging from the other thread:
https://lore.kernel.org/all/[email protected]/
Although this won't matter much in practice, let's fix the call order for consistency.

Fixes a25684a95646 ("ALSA: memalloc: Support for non-contiguous page allocation")
---
 sound/core/memalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/memalloc.c +1 -1
index f98f694e3ce4..6fd763d4d15b 100644
@@ -541,9 +541,9 @@ static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
 	if (mode == SNDRV_DMA_SYNC_CPU) {
 		if (dmab->dev.dir == DMA_TO_DEVICE)
 			return;
+		invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
 		dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
 					 dmab->dev.dir);
-		invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
 	} else {
 		if (dmab->dev.dir == DMA_FROM_DEVICE)
 			return;
Signature · 1 line
2.31.1