Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CED7C433DB for ; Wed, 17 Feb 2021 07:30:46 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5DA6164E04 for ; Wed, 17 Feb 2021 07:30:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5DA6164E04 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id B96171616; Wed, 17 Feb 2021 08:29:51 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz B96171616 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1613547041; bh=NDfHFHQZLCVzCeHpLnTYwID4lnrYNWOISImyGG8P3zg=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=rNMfJb/5LWPdAIuVNr1Ig5YXy2zTXyd2cSOxwH9lU1XdFS+hSF/rcj/xtYLpI4mmN CfQrLbB5HcVTjvFqizMUWxDLzUD2L0snR+NseKOY8bgzJquTTDpakzPBRsTCmiCDns NoZT83tSkwqxpzXTKObh0M+LmBuv8m9FhUGhxzU0= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id F02BEF8020B; Wed, 17 Feb 2021 08:29:50 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 67CB4F80258; Wed, 17 Feb 2021 08:29:23 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 3A62DF800E9 for ; Wed, 17 Feb 2021 08:29:08 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 3A62DF800E9 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 6EDB0AF42; Wed, 17 Feb 2021 07:29:03 +0000 (UTC) Date: Wed, 17 Feb 2021 08:29:02 +0100 Message-ID: From: Takashi Iwai To: "Gyeongtaek Lee" Subject: Re: [PATCH v4 1/1] ASoC: dpcm: acquire dpcm_lock in dpcm_do_trigger() In-Reply-To: <1773638882.61613536382796.JavaMail.epsvc@epcpadp4> References: <1773638882.61613536382796.JavaMail.epsvc@epcpadp4> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Cc: alsa-devel@alsa-project.org, khw0178.kim@samsung.com, 'Kuninori Morimoto' , kimty@samsung.com, lgirdwood@gmail.com, 'Pierre-Louis Bossart' , broonie@kernel.org, hmseo@samsung.com, cpgs@samsung.com, donggyun.ko@samsung.com, s47.kang@samsung.com, pilsun.jang@samsung.com, tkjung@samsung.com X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" On Wed, 17 Feb 2021 05:31:49 +0100, Gyeongtaek Lee wrote: > > 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(). I don't think we can apply spin lock there blindly. There is non-atomic PCM that must not take a spin lock there, too. thanks, Takashi > > Signed-off-by: Gyeongtaek Lee > Cc: stable@vger.kernel.org > --- > sound/soc/soc-pcm.c | 62 ++++++++++++++++++++++++++++++++------------- > 1 file changed, 44 insertions(+), 18 deletions(-) > > diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c > index ee51dc7fd893..718f6b3a309a 100644 > --- a/sound/soc/soc-pcm.c > +++ b/sound/soc/soc-pcm.c > @@ -2074,12 +2074,17 @@ static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, > return ret; > } > > +static int dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, > + struct snd_soc_pcm_runtime *be, int stream); > + > int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, > int cmd) > { > struct snd_soc_dpcm *dpcm; > + unsigned long flags; > int ret = 0; > > + spin_lock_irqsave(&fe->card->dpcm_lock, flags); > for_each_dpcm_be(fe, stream, dpcm) { > > struct snd_soc_pcm_runtime *be = dpcm->be; > @@ -2102,7 +2107,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; > @@ -2112,7 +2117,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; > @@ -2122,7 +2127,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; > @@ -2131,12 +2136,12 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, > (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) > continue; > > - if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) > + if (!dpcm_can_be_free_stop(fe, be, stream)) > continue; > > ret = soc_pcm_trigger(be_substream, cmd); > if (ret) > - return ret; > + break; > > be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; > break; > @@ -2144,12 +2149,12 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, > if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) > continue; > > - if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) > + if (!dpcm_can_be_free_stop(fe, be, stream)) > continue; > > ret = soc_pcm_trigger(be_substream, cmd); > if (ret) > - return ret; > + break; > > be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; > break; > @@ -2157,17 +2162,20 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, > if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) > continue; > > - if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) > + if (!dpcm_can_be_free_stop(fe, be, stream)) > continue; > > ret = soc_pcm_trigger(be_substream, cmd); > if (ret) > - return ret; > + break; > > be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; > break; > } > + if (ret) > + break; > } > + spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); > > return ret; > } > @@ -2905,10 +2913,9 @@ static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, > struct snd_soc_dpcm *dpcm; > int state; > int ret = 1; > - unsigned long flags; > int i; > > - spin_lock_irqsave(&fe->card->dpcm_lock, flags); > + lockdep_assert_held(&fe->card->dpcm_lock); > for_each_dpcm_fe(be, stream, dpcm) { > > if (dpcm->fe == fe) > @@ -2922,17 +2929,12 @@ static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, > } > } > } > - spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); > > /* it's safe to do this BE DAI */ > return ret; > } > > -/* > - * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE > - * are not running, paused or suspended for the specified stream direction. > - */ > -int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, > +static int dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, > struct snd_soc_pcm_runtime *be, int stream) > { > const enum snd_soc_dpcm_state state[] = { > @@ -2943,6 +2945,23 @@ int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, > > return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); > } > + > +/* > + * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE > + * are not running, paused or suspended for the specified stream direction. > + */ > +int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, > + struct snd_soc_pcm_runtime *be, int stream) > +{ > + unsigned long flags; > + int ret; > + > + spin_lock_irqsave(&fe->card->dpcm_lock, flags); > + ret = dpcm_can_be_free_stop(fe, be, stream); > + spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); > + > + return ret; > +} > EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); > > /* > @@ -2952,6 +2971,9 @@ EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); > int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, > struct snd_soc_pcm_runtime *be, int stream) > { > + unsigned long flags; > + int ret; > + > const enum snd_soc_dpcm_state state[] = { > SND_SOC_DPCM_STATE_START, > SND_SOC_DPCM_STATE_PAUSED, > @@ -2959,6 +2981,10 @@ int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, > SND_SOC_DPCM_STATE_PREPARE, > }; > > - return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); > + spin_lock_irqsave(&fe->card->dpcm_lock, flags); > + ret = snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); > + spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); > + > + return ret; > } > EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); > -- > 2.21.0 > > >