Skip to contentSkip to conversation

ALSA: SOC: DMA: increment buffer pointer atomically

PATCH Superseded Jiada Wang 2 participants 4 messages Nov 30, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiada Wang

author
#0 lore raw

PATCH 1/1 ALSA: SOC: DMA: increment buffer pointer atomically

From: Andreas Pape <[email protected]>

Setting pointer and afterwards check for wrap around leads to the possibility of returning the inconsistent pointer position. This patch increments buffer pointer atomically to avoid this issue.

---
 sound/core/pcm_dmaengine.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/pcm_dmaengine.c +5 -3
index 8eb58c7..6f6da11 100644
@@ -139,12 +139,14 @@ EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
 
 static void dmaengine_pcm_dma_complete(void *arg)
 {
+	unsigned int new_pos;
 	struct snd_pcm_substream *substream = arg;
 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 
-	prtd->pos += snd_pcm_lib_period_bytes(substream);
-	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
-		prtd->pos = 0;
+	new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
+	if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
+		new_pos = 0;
+	prtd->pos = new_pos;
 
 	snd_pcm_period_elapsed(substream);
 }
Signature · 1 line
2.9.3
#1 lore raw
Quoted 5 lines · Jiada Wang

From: Andreas Pape <[email protected]>

Setting pointer and afterwards check for wrap around leads to the possibility of returning the inconsistent pointer position. This patch increments buffer pointer atomically to avoid this issue.

Makes sense.

Quoted 16 lines
---
 sound/core/pcm_dmaengine.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/pcm_dmaengine.c +1 -0
index 8eb58c7..6f6da11 100644
@@ -139,12 +139,14 @@ EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
 
 static void dmaengine_pcm_dma_complete(void *arg)
 {
+	unsigned int new_pos;

sound/core/pcm_dmaengine.c EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data); · line 149

Quoted 10 lines
sound/core/pcm_dmaengine.c +4 -3
 	struct snd_pcm_substream *substream = arg;
 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 
-	prtd->pos += snd_pcm_lib_period_bytes(substream);
-	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
-		prtd->pos = 0;
+	new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
+	if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
+		new_pos = 0;
+	prtd->pos = new_pos;

But to really make it atomic I think this needs READ_ONCE/WRITE_ONCE.

#2 lore raw
Quoted 28 lines · Lars-Peter Clausen
Quoted 5 lines · Jiada Wang

From: Andreas Pape <[email protected]>

Setting pointer and afterwards check for wrap around leads to the possibility of returning the inconsistent pointer position. This patch increments buffer pointer atomically to avoid this issue.

Makes sense.

Quoted 19 lines
---
 sound/core/pcm_dmaengine.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/pcm_dmaengine.c +1 -0
index 8eb58c7..6f6da11 100644
@@ -139,12 +139,14 @@ EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
 
 static void dmaengine_pcm_dma_complete(void *arg)
 {
+	unsigned int new_pos;
 	struct snd_pcm_substream *substream = arg;
 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 
Quoted 9 lines

sound/core/pcm_dmaengine.c EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data); · line 149

Quoted 7 lines
sound/core/pcm_dmaengine.c +4 -3
-	prtd->pos += snd_pcm_lib_period_bytes(substream);
-	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
-		prtd->pos = 0;
+	new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
+	if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
+		new_pos = 0;
+	prtd->pos = new_pos;

But to really make it atomic I think this needs READ_ONCE/WRITE_ONCE.

And the access to prtd->pos in snd_dmaengine_pcm_pointer_no_residue() should also use READ_ONCE(). It is very unlikely that the code gets mis-compiled to generate more than one access, but having READ_ONCE() acts as a annotation that makes it explicit that this is data that can be updated concurrently without further synchronization.

#3 lore raw
Quoted 39 lines · Lars-Peter Clausen
Quoted 28 lines · Lars-Peter Clausen
Quoted 5 lines · Jiada Wang

From: Andreas Pape <[email protected]>

Setting pointer and afterwards check for wrap around leads to the possibility of returning the inconsistent pointer position. This patch increments buffer pointer atomically to avoid this issue.

Makes sense.

Quoted 19 lines
---
 sound/core/pcm_dmaengine.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/pcm_dmaengine.c +1 -0
index 8eb58c7..6f6da11 100644
@@ -139,12 +139,14 @@ EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
 
 static void dmaengine_pcm_dma_complete(void *arg)
 {
+	unsigned int new_pos;
 	struct snd_pcm_substream *substream = arg;
 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 
Quoted 9 lines

sound/core/pcm_dmaengine.c EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data); · line 149

Quoted 7 lines
sound/core/pcm_dmaengine.c +4 -3
-	prtd->pos += snd_pcm_lib_period_bytes(substream);
-	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
-		prtd->pos = 0;
+	new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
+	if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
+		new_pos = 0;
+	prtd->pos = new_pos;

But to really make it atomic I think this needs READ_ONCE/WRITE_ONCE.

Quoted 5 lines

And the access to prtd->pos in snd_dmaengine_pcm_pointer_no_residue() should also use READ_ONCE(). It is very unlikely that the code gets mis-compiled to generate more than one access, but having READ_ONCE() acts as a annotation that makes it explicit that this is data that can be updated concurrently without further synchronization.

Having given this some additional thoughts, I think a READ_ONCE() in dmaengine_pcm_dma_complete() is not necessary. dmaengine_pcm_dma_complete() is the only writer of prtd->pos and it is not running concurrently to itself. So we'll always observe consistent state, even if the compiler decides to issue multiple reads. The WRITE_ONCE() is required to make sure that the prtd->pos state stays consistent to concurrent readers. And the READ_ONCE() in snd_dmaengine_pcm_pointer_no_residue() is required to make sure that consistent state is observed from concurrent writers.