[alsa-devel] [PATCH v2 0/2] fix race condition in rsnd_ssi_pointer_update
From: Jiada Wang jiada_wang@mentor.com
This patch set aims to fix the race condition in rsnd_ssi_pointer_update, between set of byte_pos and wrap it around when new buffer starts.
Jiada Wang (2): ASoC: rsnd: ssi: fix race condition in rsnd_ssi_pointer_update ASoC: rsnd: ssi: remove unnesessary period_pos
Changes for v2: * Updated commit message
sound/soc/sh/rcar/ssi.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
From: Jiada Wang jiada_wang@mentor.com
Currently there is race condition between set of byte_pos and wrap it around when new buffer starts. If .pointer is called in-between it will result in inconsistent pointer position be returned from .pointer callback.
This patch increments buffer pointer atomically to avoid this issue.
Signed-off-by: Jiada Wang jiada_wang@mentor.com Reviewed-by: Takashi Sakamoto takashi.sakamoto@miraclelinux.com --- sound/soc/sh/rcar/ssi.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/sound/soc/sh/rcar/ssi.c b/sound/soc/sh/rcar/ssi.c index fece1e5f..cbf3bf3 100644 --- a/sound/soc/sh/rcar/ssi.c +++ b/sound/soc/sh/rcar/ssi.c @@ -446,25 +446,29 @@ static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod, int byte) { struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); + bool ret = false; + int byte_pos;
- ssi->byte_pos += byte; + byte_pos = ssi->byte_pos + byte;
- if (ssi->byte_pos >= ssi->next_period_byte) { + if (byte_pos >= ssi->next_period_byte) { struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ssi->period_pos++; ssi->next_period_byte += ssi->byte_per_period;
if (ssi->period_pos >= runtime->periods) { - ssi->byte_pos = 0; + byte_pos = 0; ssi->period_pos = 0; ssi->next_period_byte = ssi->byte_per_period; }
- return true; + ret = true; }
- return false; + WRITE_ONCE(ssi->byte_pos, byte_pos); + + return ret; }
/* @@ -838,7 +842,7 @@ static int rsnd_ssi_pointer(struct rsnd_mod *mod, struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
- *pointer = bytes_to_frames(runtime, ssi->byte_pos); + *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
return 0; }
From: Jiada Wang jiada_wang@mentor.com
period_pos can always be calculated by byte_pos and byte_per_period, there is no reason to maintain this variable in rsnd_dai_stream.
This patch removes period_pos from rsnd_ssi and calculates next_period_byte with consideration of actual byte_pos value.
Signed-off-by: Jiada Wang jiada_wang@mentor.com --- sound/soc/sh/rcar/ssi.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/sound/soc/sh/rcar/ssi.c b/sound/soc/sh/rcar/ssi.c index cbf3bf3..f212024 100644 --- a/sound/soc/sh/rcar/ssi.c +++ b/sound/soc/sh/rcar/ssi.c @@ -80,7 +80,6 @@ struct rsnd_ssi { unsigned int usrcnt;
int byte_pos; - int period_pos; int byte_per_period; int next_period_byte; }; @@ -421,7 +420,6 @@ static void rsnd_ssi_pointer_init(struct rsnd_mod *mod, struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ssi->byte_pos = 0; - ssi->period_pos = 0; ssi->byte_per_period = runtime->period_size * runtime->channels * samples_to_bytes(runtime, 1); @@ -453,13 +451,12 @@ static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
if (byte_pos >= ssi->next_period_byte) { struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); + int period_pos = byte_pos / ssi->byte_per_period;
- ssi->period_pos++; - ssi->next_period_byte += ssi->byte_per_period; + ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
- if (ssi->period_pos >= runtime->periods) { + if (period_pos >= runtime->periods) { byte_pos = 0; - ssi->period_pos = 0; ssi->next_period_byte = ssi->byte_per_period; }
Hi
This patch set aims to fix the race condition in rsnd_ssi_pointer_update, between set of byte_pos and wrap it around when new buffer starts.
Jiada Wang (2): ASoC: rsnd: ssi: fix race condition in rsnd_ssi_pointer_update ASoC: rsnd: ssi: remove unnesessary period_pos
Changes for v2:
- Updated commit message
sound/soc/sh/rcar/ssi.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
for all patches
Acked-by: Kuninori Morimoto kuninori.morimoto.gx@renesas.com
participants (2)
-
jiada_wang@mentor.com
-
Kuninori Morimoto