From patchwork Thu Dec 3 08:59:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyeongtaek Lee X-Patchwork-Id: 336978 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.7 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, URIBL_BLOCKED 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 7F17DC64E8A for ; Thu, 3 Dec 2020 09:17:09 +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 1D06F20758 for ; Thu, 3 Dec 2020 09:17:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1D06F20758 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=samsung.com 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 D96C11819; Thu, 3 Dec 2020 10:16:12 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz D96C11819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1606987023; bh=PwRLMvJHAUwCjlDQgTiSqtXT16/6gII85ctLNdFXxFI=; h=From:To:Subject:Date:References:Cc:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From; b=h6U+MUNY3FUH2Df3VHJeacwdpIXJrVw/bIHNtVtv97mTYcjSHjB8SEXnQGRRewNSM 07ttZkdb1GEeLnvKAEIpx4YzReGyVXD/oa5Y+PrUq7uI7SvHiw1JV5pmM+qqJ80Y+u xttLcCDgtH1NVI6jOEB1sp9dhpZuUAud0vzM36KI= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 2247DF8026B; Thu, 3 Dec 2020 10:15:22 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 62901F8016D; Thu, 3 Dec 2020 10:15:19 +0100 (CET) Received: from mailout1.samsung.com (mailout1.samsung.com [203.254.224.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id CADF6F80158 for ; Thu, 3 Dec 2020 10:15:06 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz CADF6F80158 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b="YziIk1PV" Received: from epcas3p3.samsung.com (unknown [182.195.41.21]) by mailout1.samsung.com (KnoxPortal) with ESMTP id 20201203091502epoutp0158a4a0db121d7b0946625f02b46d48aa~NKhLSSaVL0500205002epoutp01B for ; Thu, 3 Dec 2020 09:15:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.samsung.com 20201203091502epoutp0158a4a0db121d7b0946625f02b46d48aa~NKhLSSaVL0500205002epoutp01B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1606986902; bh=6mjmvTKYi5n8uE/b9GzEfeO2rilVfByLM24p6DFxT5w=; h=From:To:Cc:Subject:Date:References:From; b=YziIk1PVPPsydFjTeL7TLKCWxE0ED9YIv0LeMpdBZXNoTgHnoa87HMQRTkL918mDQ S0WKqiZ6ykwpl3G6C9imh9SolIWgOMGbC9gBzL6C0caHp884liBLKBoNjSLW5frR33 39GYCvwrk550fRRWbYkLqqLFMiA7pJdkm2HWxuwg= Received: from epsnrtp2.localdomain (unknown [182.195.42.163]) by epcas3p2.samsung.com (KnoxPortal) with ESMTP id 20201203091501epcas3p2decf290381b3af5c23b895ae5259c18d~NKhK6KVTY2350523505epcas3p2G; Thu, 3 Dec 2020 09:15:01 +0000 (GMT) Received: from epcpadp3 (unknown [182.195.40.17]) by epsnrtp2.localdomain (Postfix) with ESMTP id 4CmqtF6bxMzMqYkV; Thu, 3 Dec 2020 09:15:01 +0000 (GMT) Received: from epsmtrp1.samsung.com (unknown [182.195.40.13]) by epcas2p3.samsung.com (KnoxPortal) with ESMTPA id 20201203085905epcas2p34cc261df88bb8e99f02c8f8b0a81fdc8~NKTP2bOvn1054810548epcas2p3n; Thu, 3 Dec 2020 08:59:05 +0000 (GMT) Received: from epsmgms1p1new.samsung.com (unknown [182.195.42.41]) by epsmtrp1.samsung.com (KnoxPortal) with ESMTP id 20201203085905epsmtrp14cad4e01a251fa93245d6a48d2fab545~NKTP1asnr1098510985epsmtrp1X; Thu, 3 Dec 2020 08:59:05 +0000 (GMT) X-AuditID: b6c32a29-f55ff7000000349e-8a-5fc8a8d88330 Received: from epsmtip2.samsung.com ( [182.195.34.31]) by epsmgms1p1new.samsung.com (Symantec Messaging Gateway) with SMTP id 3F.A2.13470.8D8A8CF5; Thu, 3 Dec 2020 17:59:05 +0900 (KST) Received: from KORDO025540 (unknown [12.36.182.130]) by epsmtip2.samsung.com (KnoxPortal) with ESMTPA id 20201203085904epsmtip2b6f4cb59183b1e693cef89be7642aae4~NKTPl_0kf0224702247epsmtip2W; Thu, 3 Dec 2020 08:59:04 +0000 (GMT) From: "Gyeongtaek Lee" To: "'Kuninori Morimoto'" , Subject: [PATCH v3 1/1] ASoC: dpcm: acquire dpcm_lock in dpcm_do_trigger() Date: Thu, 3 Dec 2020 17:59:04 +0900 Message-ID: <1007309018.21606986901925.JavaMail.epsvc@epcpadp3> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdbJUefzWBsBwqKdQFqEbWIOCt4/TA== Content-Language: ko X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpnkeLIzCtJLcpLzFFi42LZdlhJXvfmihPxBlM6mC2uXDzEZPHykKbF 3R/mFjO2dbNYNN6bwGax+uoWJotXh3cxWny70sFk8ev/MyaLoxcXM1k03G1mt3i5+Q2TxZHG KUwOvB4bPjexeeycdZfdY97JQI9vZyayePRtWcXosfl0dQBbFJdNSmpOZllqkb5dAlfGqU9X 2QoeGFUcunCftYHxv0YXIweHhICJxJcr6V2MXBxCArsZJVZ2fmXsYuQEiktIfJh/hh3CFpa4 33KEFaLoOaPEn2VfmEESbAK6El/u3QGzRQSCJBYs/QtWxCzwjkli4pqPLCAJYQEviYeP/7CB 2CwCKhKL96xmAdnMK2ApsemJBEiYV0BQ4uTMJ2BhZgE9ibaNYDcwC8hLbH87hxniBgWJn0+X sYKUiACV3FzDDVEiIjG7s415AqPgLCSDZiEMmoVk0CwkHQsYWVYxSqYWFOem5xYbFhjmpZbr FSfmFpfmpesl5+duYgTHlZbmDsbtqz7oHWJk4mA8xCjBwawkwnt76Yl4Id6UxMqq1KL8+KLS nNTiQ4zSHCxK4rw3ChfGCQmkJ5akZqemFqQWwWSZODilGpj6VL4anri84vjRVRW3JjydeGTS 1BYzFeZzWWe5bziEdAvFLbh+z+h6xFGL11zcS5v+HF+m+kA+rS0qW7cvs49J/bTH5uW5ugqX xLyYWs/uM1j7WT7+T4p+S6mkU5Jf2ppZr47sY2vpepYe8KKGXdMs7c/Mcrk3twuZVoT65RSp mn/lDHvMcsv+zobz3nPNtA6J/ZI3n1d5x8RnMvfOzBYGB8mcjoIXIWd4g+suX2uQKI89v7/P //uC81e/7uC8ITHjCOe19Zul93P49y5nmOfIY7ucfWLvdJOl+5f6M2686rdfrEmp6oHspRxh lgfzMidWGG88sDrkld66q67Tm5yns9maelw0PxrQ9Njw9wElluKMREMt5qLiRACV2kk7GgMA AA== X-CMS-MailID: 20201203085905epcas2p34cc261df88bb8e99f02c8f8b0a81fdc8 X-Msg-Generator: CA X-Sendblock-Type: AUTO_CONFIDENTIAL X-CPGSPASS: Y X-Hop-Count: 3 X-CMS-RootMailID: 20201203085905epcas2p34cc261df88bb8e99f02c8f8b0a81fdc8 References: Cc: alsa-devel@alsa-project.org, khw0178.kim@samsung.com, 'Takashi Iwai' , lgirdwood@gmail.com, 'Pierre-Louis Bossart' , kimty@samsung.com, donggyun.ko@samsung.com, hmseo@samsung.com, cpgs@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" 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 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 dcab9527ba3d..7c5d950a8628 100644 --- a/sound/soc/soc-pcm.c +++ b/sound/soc/soc-pcm.c @@ -2073,6 +2073,9 @@ 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); + static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, struct snd_pcm_substream *substream, int cmd) { @@ -2092,8 +2095,10 @@ 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; @@ -2113,7 +2118,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, ret = dpcm_do_trigger(dpcm, be_substream, cmd); if (ret) - return ret; + break; be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; break; @@ -2123,7 +2128,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, ret = dpcm_do_trigger(dpcm, be_substream, cmd); if (ret) - return ret; + break; be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; break; @@ -2133,7 +2138,7 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, ret = dpcm_do_trigger(dpcm, be_substream, cmd); if (ret) - return ret; + break; be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; break; @@ -2142,12 +2147,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 = dpcm_do_trigger(dpcm, be_substream, cmd); if (ret) - return ret; + break; be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; break; @@ -2155,12 +2160,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 = dpcm_do_trigger(dpcm, be_substream, cmd); if (ret) - return ret; + break; be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; break; @@ -2168,17 +2173,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 = dpcm_do_trigger(dpcm, 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; } @@ -2916,10 +2924,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) @@ -2933,17 +2940,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[] = { @@ -2954,6 +2956,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); /* @@ -2963,6 +2982,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, @@ -2970,6 +2992,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);