Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp465490rwb; Thu, 12 Jan 2023 08:16:06 -0800 (PST) X-Google-Smtp-Source: AMrXdXvaQNWhREmuO+dZntkUG4f9hZHgIAv9J7b6gePmP3YEdAh+0P/zWZ5O2P+ICnheQLAd/E6/ X-Received: by 2002:a05:6402:2a08:b0:470:4d71:6d43 with SMTP id ey8-20020a0564022a0800b004704d716d43mr70019060edb.25.1673540166512; Thu, 12 Jan 2023 08:16:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673540166; cv=none; d=google.com; s=arc-20160816; b=FdYQC4KUL2hqKVAa3m4LiT1pAODVgo5WKdxsFWslJveizaWmUwOh+BCaMKr3dpfOJN Pe9cZdWo4Vmh1yI+yTuD+cFA3EzNLeCzRO5/r8K27+uLXkhOoSOrKKu9ndfTw8r38EKb A01ac8uFhcREt0xxh7CaOUlH1XJo89BaBJ2ieK718MBnb0omR3aZvYpBMULOhD/lzuEb W/bRN/je3f8yavkbj0lbwQ6BrB7qAy5x6Bi93nCKeTZQHN3J48yTpOQl5vIlY7JH4mlH 9ZmwaZLHWgDG7Aqjox2iWiqcOdl4njJszhlDX/5R8/nS6btObles9gXPsrgUpXPutd7S FkAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:dkim-signature; bh=Rkaqg8bS493+6Z0jo3SY3Z4IH2gxZmp+Rqm1DyjsVnM=; b=KEUoKe+FxtcrQhuwLzjLnz/7BZ0/TrJiXTnmrfqL799hwD+9UWmCgbMvdAL2DP51iI Y4748ZXSnM9lWfhDcYeqLCF3IZejKc5lQcIfmwrf0K6kBNsjPqjE+lodXWo4ZPST2d8l mmv/YaGrIXHOD4R6l+r19LB904p9r7y0jrdtvemGOI5P2fx0T7SI+ZfspbplCI8t8pWO dTofLrQuj2Aicm8tgby+waVKbKbSgOW0DBDywUskV8N5wzLbqQEhnF3+3Jz2QVYdBlYE eR/sE9YD9pDURTMhnT9FKdZQMGo88ZpPyuvGu+OpqgZmHjitOJG2IOHgl8nLhHWpTxkb vW3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a6Pkf11c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y2-20020a50e602000000b0048bd3a68aa1si17115077edm.171.2023.01.12.08.15.53; Thu, 12 Jan 2023 08:16:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a6Pkf11c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240372AbjALPbo (ORCPT + 50 others); Thu, 12 Jan 2023 10:31:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbjALP3r (ORCPT ); Thu, 12 Jan 2023 10:29:47 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81295A1A9 for ; Thu, 12 Jan 2023 07:23:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673536990; x=1705072990; h=message-id:date:mime-version:subject:to:references:from: in-reply-to:content-transfer-encoding; bh=D0hNIdfv9EWyP1RVxQgqv7k1sO7+UaCb9F79/VZ74AM=; b=a6Pkf11cwTb0P/s5Bjlhh3x10EMKqRkaMdMf22nj0UbGU+1bGbjYfa2r s0QDAp87mMHC0/J1WIJeJTXl75gD+ht4tikr5qvMtRwfYcGVY/pgzGDgx wDWMRyehX2SsJTqC7uq2L1J1XXQD5bl85tBO0PRVPGn+SRNwg0Q+tfKwl hiXlCYYV8R0j+j6CtGn8FcqapyJ73LeFnsI3+I4A5EjIdSmmOODDYpr8e 8Rpz2NIuwwNGgU1Zh4WKtvTacV8Ko+J0bElYLLOF0MlCfUxHSWnVryy1t ClXRpMF5dy8QcHyBozgZYZF14D8vRu9MqZXLK3056IIpJtFlGFAYiT7+X A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="388212558" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="388212558" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 06:57:10 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="607816924" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="607816924" Received: from jbetan3x-mobl1.amr.corp.intel.com (HELO [10.209.143.163]) ([10.209.143.163]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 06:57:10 -0800 Message-ID: Date: Thu, 12 Jan 2023 08:19:40 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.4.2 Subject: Re: [PATCH] ASoC: soc-pcm.c: Introduce a count to record the times of setting DAIs parameters To: Chancel Liu , lgirdwood@gmail.com, broonie@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org References: <20230112065834.580192-1-chancel.liu@nxp.com> Content-Language: en-US From: Pierre-Louis Bossart In-Reply-To: <20230112065834.580192-1-chancel.liu@nxp.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/12/23 00:58, Chancel Liu wrote: > The commit 1da681e52853 ("ASoC: soc-pcm.c: Clear DAIs parameters after > stream_active is updated") tries to make sure DAIs parameters can be > cleared properly through moving the cleanup to the place where > stream_active is updated. However, it will cause the cleanup only > happening in soc_pcm_close(). > > Suppose a case: aplay -Dhw:0 44100.wav 48000.wav. The case calls > soc_pcm_open()->soc_pcm_hw_params()->soc_pcm_hw_free()-> > soc_pcm_hw_params()->soc_pcm_hw_free()->soc_pcm_close() in order. The > parameters would be remained in the system even if the playback of > 44100.wav is finished. > > The case requires us clearing parameters in phase of soc_pcm_hw_free(). > We shouldn't use stream_active to decide if we must do the cleanup > since it is finally updated in soc_pcm_close(). > > This patch introduces a usage count called hw_params_count in > snd_soc_dai structure. It records the times of setting parameters to > this DAI then decreases each time soc_pcm_hw_free() is called. If the > count decreases to 0, it means this DAI is not used now and we should > clear the parameters. what makes you think that the use of hw_params and hw_free is symmetrical? A couple of years ago we found a case where the FE hw_params failed, and in that case the BE hw_free was called without the BE hw_params ever being invoked first. This is due to the DPCM error handling, and as a result all our hw_free implementations test if the resources are actually allocated/valid and never assume hw_params was used. IIRC it's also valid to call hw_params multiple times without calling hw_free every time. > Fixes: 1da681e52853 ("ASoC: soc-pcm.c: Clear DAIs parameters after stream_active is updated") > > Signed-off-by: Chancel Liu > --- > include/sound/soc-dai.h | 3 +++ > sound/soc/soc-pcm.c | 16 +++++++++++----- > 2 files changed, 14 insertions(+), 5 deletions(-) > > diff --git a/include/sound/soc-dai.h b/include/sound/soc-dai.h > index ea7509672086..a7e589a0fe72 100644 > --- a/include/sound/soc-dai.h > +++ b/include/sound/soc-dai.h > @@ -451,6 +451,9 @@ struct snd_soc_dai { > unsigned int channels; > unsigned int sample_bits; > > + /* Count of setting DAI parameters */ > + unsigned int hw_params_count; > + > /* parent platform/codec */ > struct snd_soc_component *component; > > diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c > index 579a44d81d9a..2c2a5dcf9e06 100644 > --- a/sound/soc/soc-pcm.c > +++ b/sound/soc/soc-pcm.c > @@ -711,14 +711,10 @@ static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd, > > if (!rollback) { > snd_soc_runtime_deactivate(rtd, substream->stream); > - /* clear the corresponding DAIs parameters when going to be inactive */ > - for_each_rtd_dais(rtd, i, dai) { > - if (snd_soc_dai_active(dai) == 0) > - soc_pcm_set_dai_params(dai, NULL); > > + for_each_rtd_dais(rtd, i, dai) > if (snd_soc_dai_stream_active(dai, substream->stream) == 0) > snd_soc_dai_digital_mute(dai, 1, substream->stream); > - } > } > > for_each_rtd_dais(rtd, i, dai) > @@ -949,6 +945,14 @@ static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd, > > snd_soc_dpcm_mutex_assert_held(rtd); > > + /* clear the corresponding DAIs parameters when hw_params_count decreases to 0 */ > + for_each_rtd_dais(rtd, i, dai) > + if (snd_soc_dai_stream_valid(dai, substream->stream)) { > + dai->hw_params_count--; > + if (dai->hw_params_count == 0) > + soc_pcm_set_dai_params(dai, NULL); > + } > + > /* run the stream event */ > snd_soc_dapm_stream_stop(rtd, substream->stream); > > @@ -1051,6 +1055,7 @@ static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd, > > soc_pcm_set_dai_params(codec_dai, &codec_params); > snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); > + codec_dai->hw_params_count++; > } > > for_each_rtd_cpu_dais(rtd, i, cpu_dai) { > @@ -1068,6 +1073,7 @@ static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd, > /* store the parameters for each DAI */ > soc_pcm_set_dai_params(cpu_dai, params); > snd_soc_dapm_update_dai(substream, params, cpu_dai); > + cpu_dai->hw_params_count++; > } > > ret = snd_soc_pcm_component_hw_params(substream, params);