Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Gyeongtaek Lee" <gt82.lee@samsung.com>
To: "'Kuninori Morimoto'" <kuninori.morimoto.gx@renesas.com>,
	<broonie@kernel.org>, <cpgs@samsung.com>
Cc: alsa-devel@alsa-project.org, khw0178.kim@samsung.com,
	'Takashi Iwai' <tiwai@suse.de>,
	'Pierre-Louis Bossart' <pierre-louis.bossart@linux.intel.com>,
	lgirdwood@gmail.com, kimty@samsung.com, donggyun.ko@samsung.com,
	hmseo@samsung.com, cpgs@samsung.com, s47.kang@samsung.com,
	pilsun.jang@samsung.com, tkjung@samsung.com
Subject: [PATCH v5 1/1] ASoC: dpcm: acquire dpcm_lock in dpcm_do_trigger()
Date: Wed, 3 Mar 2021 15:01:34 +0900	[thread overview]
Message-ID: <1935972447.41614751502383.JavaMail.epsvc@epcpadp3> (raw)
In-Reply-To: CGME20210303060135epcas2p3677a88a1ae15b93fea8f0e2821558f6e@epcas2p3.samsung.com

If stop by underrun and DPCM BE disconnection is run simultaneously,
data abort can be occurred by the sequence below.

CPU0					CPU1
dpcm_be_dai_trigger():			dpcm_be_disconnect():

for_each_dpcm_be(fe, stream, dpcm) {

spin_lock_irqsave(&fe->card->dpcm_lock, flags);
					list_del(&dpcm->list_be);
					list_del(&dpcm->list_fe);

spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
					kfree(dpcm);

struct snd_soc_pcm_runtime *be = dpcm->be; <-- Accessing freed memory

To prevent this situation, dpcm_lock should be acquired during
iteration of dpcm list in dpcm_be_dai_trigger().

Signed-off-by: Gyeongtaek Lee <gt82.lee@samsung.com>
Cc: stable@vger.kernel.org
---
 include/sound/soc-dpcm.h |  5 ++++
 sound/soc/soc-pcm.c      | 59 +++++++++++++++++++++++++++++++++-------
 2 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/include/sound/soc-dpcm.h b/include/sound/soc-dpcm.h
index 0f6c50b17bba..599cd6054bc3 100644
--- a/include/sound/soc-dpcm.h
+++ b/include/sound/soc-dpcm.h
@@ -103,6 +103,11 @@ struct snd_soc_dpcm_runtime {
 	int trigger_pending; /* trigger cmd + 1 if pending, 0 if not */
 };
 
+struct snd_soc_dpcm_rtd_list {
+	int num_rtds;
+	struct snd_soc_pcm_runtime *rtds[0];
+};
+
 #define for_each_dpcm_fe(be, stream, _dpcm)				\
 	list_for_each_entry(_dpcm, &(be)->dpcm[stream].fe_clients, list_fe)
 
diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
index 14d85ca1e435..ccda0f77e827 100644
--- a/sound/soc/soc-pcm.c
+++ b/sound/soc/soc-pcm.c
@@ -2028,15 +2028,51 @@ static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
 	return ret;
 }
 
+static int dpcm_be_list_create(struct snd_soc_pcm_runtime *fe, int stream,
+		struct snd_soc_dpcm_rtd_list **be_list)
+{
+	struct snd_soc_dpcm *dpcm;
+	unsigned long flags;
+	int size, i, ret = 0;
+
+	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
+
+	size = 0;
+	for_each_dpcm_be(fe, stream, dpcm)
+		size++;
+
+	*be_list = kzalloc(struct_size(*be_list, rtds, size), GFP_ATOMIC);
+	if (*be_list) {
+		i = 0;
+		for_each_dpcm_be(fe, stream, dpcm)
+			(*be_list)->rtds[i++] = dpcm->be;
+
+		if (i != size)
+			dev_err(fe->dev, "ASoC: abnormal change in be clients: %d -> %d\n",
+					size, i);
+
+		(*be_list)->num_rtds = i;
+	} else {
+		ret = -ENOMEM;
+	}
+
+	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
+
+	return ret;
+}
+
 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 			       int cmd)
 {
-	struct snd_soc_dpcm *dpcm;
-	int ret = 0;
+	struct snd_soc_dpcm_rtd_list *be_list;
+	int i, ret = 0;
 
-	for_each_dpcm_be(fe, stream, dpcm) {
+	ret = dpcm_be_list_create(fe, stream, &be_list);
+	if (ret < 0)
+		return ret;
 
-		struct snd_soc_pcm_runtime *be = dpcm->be;
+	for (i = 0; i < be_list->num_rtds; i++) {
+		struct snd_soc_pcm_runtime *be = be_list->rtds[i];
 		struct snd_pcm_substream *be_substream =
 			snd_soc_dpcm_get_substream(be, stream);
 
@@ -2056,7 +2092,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
 			break;
@@ -2066,7 +2102,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
 			break;
@@ -2076,7 +2112,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
 			break;
@@ -2090,7 +2126,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
 			break;
@@ -2103,7 +2139,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
 			break;
@@ -2116,13 +2152,16 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
 
 			ret = soc_pcm_trigger(be_substream, cmd);
 			if (ret)
-				return ret;
+				break;
 
 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
 			break;
 		}
+		if (ret < 0)
+			break;
 	}
 
+	kfree(be_list);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
-- 
2.21.0




       reply	other threads:[~2021-03-03  6:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20210303060135epcas2p3677a88a1ae15b93fea8f0e2821558f6e@epcas2p3.samsung.com>
2021-03-03  6:01 ` Gyeongtaek Lee [this message]
2021-03-03  9:46   ` [PATCH v5 1/1] ASoC: dpcm: acquire dpcm_lock in dpcm_do_trigger() Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1935972447.41614751502383.JavaMail.epsvc@epcpadp3 \
    --to=gt82.lee@samsung.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=cpgs@samsung.com \
    --cc=donggyun.ko@samsung.com \
    --cc=hmseo@samsung.com \
    --cc=khw0178.kim@samsung.com \
    --cc=kimty@samsung.com \
    --cc=kuninori.morimoto.gx@renesas.com \
    --cc=lgirdwood@gmail.com \
    --cc=pierre-louis.bossart@linux.intel.com \
    --cc=pilsun.jang@samsung.com \
    --cc=s47.kang@samsung.com \
    --cc=tiwai@suse.de \
    --cc=tkjung@samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox