BACKPORT: ASoC: soc-pcm: test refcount before triggering

[ Upstream commit 848aedfdc6ba25ad5652797db9266007773e44dd ]

On start/pause_release/resume, when more than one FE is connected to
the same BE, it's possible that the trigger is sent more than
once. This is not desirable, we only want to trigger a BE once, which
is straightforward to implement with a refcount.

For stop/pause/suspend, the problem is more complicated: the check
implemented in snd_soc_dpcm_can_be_free_stop() may fail due to a
conceptual deadlock when we trigger the BE before the FE. In this
case, the FE states have not yet changed, so there are corner cases
where the TRIGGER_STOP is never sent - the dual case of start where
multiple triggers might be sent.

This patch suggests an unconditional trigger in all cases, without
checking the FE states, using a refcount protected by the BE PCM
stream lock.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Link: https://lore.kernel.org/r/20211207173745.15850-6-pierre-louis.bossart@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
Fixes: aa9ff6a4955f ("ASoC: soc-compress: Reposition and add pcm_mutex")
Signed-off-by: Yixuan Jiang <yixuanjiang@google.com>
Cc: stable@vger.kernel.org # 5.15+

Bug: 287928097
Bug: 277241308
Link: https://lore.kernel.org/all/20230511120841.2096524-6-yixuanjiang@google.com
Change-Id: I294e1f1b55cb1819e0510bff67db41b01c809210
Signed-off-by: Yixuan Jiang <yixuanjiang@google.com>
[preserve the ABI by moving be_start to snd_soc_pcm_runtime]
Signed-off-by: Will McVicker <willmcvicker@google.com>
This commit is contained in:
Pierre-Louis Bossart
2021-12-07 11:37:44 -06:00
committed by Treehugger Robot
parent cdaafbf2de
commit fc213aa3a6
2 changed files with 56 additions and 15 deletions

View File

@@ -1098,7 +1098,17 @@ struct snd_soc_pcm_runtime {
int num_components;
ANDROID_KABI_RESERVE(1);
/* Android KABI preservation.
*
* dpcm_be_start[2] is the backport version of be_start from
* 848aedfdc6ba ("ASoC: soc-pcm: test refcount before triggering")
* which is originally in struct snd_soc_dpcm_runtime. Since we don't
* have ABI reserve fields there, we are adding this refcount variable
* here as an array for each BE stream.
*
* refcount protected by BE stream pcm lock
*/
ANDROID_KABI_USE(1, u32 dpcm_be_start[2]);
struct snd_soc_component *components[]; /* CPU/Codec/Platform */
};

View File

@@ -1630,7 +1630,7 @@ int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
goto unwind;
}
be->dpcm_be_start[stream] = 0;
be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
count++;
}
@@ -2116,35 +2116,54 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
if (!be->dpcm_be_start[stream] &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
be->dpcm_be_start[stream]++;
if (be->dpcm_be_start[stream] != 1)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret) {
be->dpcm_be_start[stream]--;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
case SNDRV_PCM_TRIGGER_RESUME:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
be->dpcm_be_start[stream]++;
if (be->dpcm_be_start[stream]!= 1)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret) {
be->dpcm_be_start[stream]--;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
be->dpcm_be_start[stream]++;
if (be->dpcm_be_start[stream] != 1)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret) {
be->dpcm_be_start[stream]--;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
break;
case SNDRV_PCM_TRIGGER_STOP:
@@ -2152,12 +2171,18 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
(be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
goto next;
if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
be->dpcm_be_start[stream]--;
if (be->dpcm_be_start[stream] != 0)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
be->dpcm_be_start[stream]++;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
break;
@@ -2165,12 +2190,15 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
goto next;
if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
be->dpcm_be_start[stream]--;
if (be->dpcm_be_start[stream] != 0)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm_be_start[stream]++;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
break;
@@ -2178,12 +2206,15 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
goto next;
if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
be->dpcm_be_start[stream]--;
if (be->dpcm_be_start[stream] != 0)
goto next;
ret = soc_pcm_trigger(be_substream, cmd);
if (ret)
if (ret) {
be->dpcm_be_start[stream]++;
goto next;
}
be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
break;