diff mbox

[1/2] ASoC: dpcm: fix BE dai not hw_free and shutdown

Message ID 1526980408-1935-2-git-send-email-kaichieh.chuang@mediatek.com (mailing list archive)
State New, archived
Headers show

Commit Message

KaiChieh Chuang May 22, 2018, 9:13 a.m. UTC
In case, one BE is used by two FE1/FE2
FE1--->BE-->
FE2--->

When FE1/FE2 call dpcm_be_dai_hw_free() together
the BE users will be 2 (> 1), hence cannot be hw_free.
The be state will leave at, ex. SND_SOC_DPCM_STATE_STOP

Later FE1/FE2 call dpcm_be_dai_shutdown(),
will be skip due to wrong state.
Leaving the BE not being hw_free and shutdown.

This patch add a flag in snd_soc_dpcm to denote
the hw_free cannot be excute for this fe->be dpcm.
The BE dai will be hw_free later when calling
dpcm_be_dai_shutdown() if still in invalid state.

Signed-off-by: KaiChieh Chuang <kaichieh.chuang@mediatek.com>
---
 include/sound/soc-dpcm.h |  2 ++
 sound/soc/soc-pcm.c      | 17 ++++++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

Comments

Mark Brown May 25, 2018, 11:31 a.m. UTC | #1
On Tue, May 22, 2018 at 05:13:27PM +0800, KaiChieh Chuang wrote:
> In case, one BE is used by two FE1/FE2
> FE1--->BE-->
> FE2--->
> 
> When FE1/FE2 call dpcm_be_dai_hw_free() together
> the BE users will be 2 (> 1), hence cannot be hw_free.
> The be state will leave at, ex. SND_SOC_DPCM_STATE_STOP
> 
> Later FE1/FE2 call dpcm_be_dai_shutdown(),
> will be skip due to wrong state.
> Leaving the BE not being hw_free and shutdown.
> 
> This patch add a flag in snd_soc_dpcm to denote
> the hw_free cannot be excute for this fe->be dpcm.
> The BE dai will be hw_free later when calling
> dpcm_be_dai_shutdown() if still in invalid state.

This works but feels messy and fragile - the problem here is that we use
the users count to decide if we can do a hw_free() but we don't
decrement that users count until shutdown which leaves the race
condition you're fixing here.  We probably need to add a second refcount
here for hw_free() which also feels a bit messy but is probably robust.

Another option is to just unconditionally do the hw_free() and clean up
if we're in the wrong state rather than checking the flag (so basically
your patch but ignoring the flag), that is simpler and should be robust
- I can't think of any reason that'd be a problem?
diff mbox

Patch

diff --git a/include/sound/soc-dpcm.h b/include/sound/soc-dpcm.h
index 8060590..52d9b5d 100644
--- a/include/sound/soc-dpcm.h
+++ b/include/sound/soc-dpcm.h
@@ -86,6 +86,8 @@  struct snd_soc_dpcm {
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *debugfs_state;
 #endif
+
+	bool be_hw_free_deferred;
 };
 
 /*
diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
index 87c9af2..481604a 100644
--- a/sound/soc/soc-pcm.c
+++ b/sound/soc/soc-pcm.c
@@ -1866,8 +1866,16 @@  int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
 			continue;
 
 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
-		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
-			continue;
+		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
+			if (dpcm->be_hw_free_deferred) {
+				soc_pcm_hw_free(be_substream);
+				be->dpcm[stream].state =
+					SND_SOC_DPCM_STATE_HW_FREE;
+				dpcm->be_hw_free_deferred = false;
+			} else {
+				continue;
+			}
+		}
 
 		dev_dbg(be->dev, "ASoC: close BE %s\n",
 			be->dai_link->name);
@@ -1924,8 +1932,10 @@  int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
 				continue;
 
 		/* do not free hw if this BE is used by other FE */
-		if (be->dpcm[stream].users > 1)
+		if (be->dpcm[stream].users > 1) {
+			dpcm->be_hw_free_deferred = true;
 			continue;
+		}
 
 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
@@ -1941,6 +1951,7 @@  int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
 		soc_pcm_hw_free(be_substream);
 
 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
+		dpcm->be_hw_free_deferred = false;
 	}
 
 	return 0;