diff mbox series

[v3,6/8] ASoC: soc-pcm: call snd_soc_component_open/close() once

Message ID 874kvzcey1.wl-kuninori.morimoto.gx@renesas.com (mailing list archive)
State Accepted
Commit dd03907bf129b42e9e3203fdf405ea9873b28dd3
Headers show
Series ASoC: soc-pcm cleanup step2 | expand

Commit Message

Kuninori Morimoto Feb. 10, 2020, 3:14 a.m. UTC
From: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>

Current soc_pcm_open() calls snd_soc_component_open() under loop.
Thus, it needs to care about opened/not-yet-opened Component.

But, if soc-component.c is handling it, soc-pcm.c don't need to care
about it.
This patch adds opened flag to soc-component.h, and simplify soc-pcm.c.
This is one of prepare for cleanup soc-pcm-open()

Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
---
v2 -> v3

	- use flag
	- Opened Component only can call Close,
	  and it can't open again without close.

 include/sound/soc-component.h |  7 +++++--
 sound/soc/soc-component.c     | 35 ++++++++++++++++++++++++++++-------
 sound/soc/soc-pcm.c           | 19 ++++++-------------
 3 files changed, 39 insertions(+), 22 deletions(-)

Comments

Pierre-Louis Bossart Feb. 10, 2020, 2:41 p.m. UTC | #1
Hi Morimoto-san,

>   #define for_each_component_dais(component, dai)\
> diff --git a/sound/soc/soc-component.c b/sound/soc/soc-component.c
> index 14e175c..ee00c09 100644
> --- a/sound/soc/soc-component.c
> +++ b/sound/soc/soc-component.c
> @@ -297,34 +297,55 @@ EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
>   int snd_soc_component_module_get(struct snd_soc_component *component,
>   				 int upon_open)
>   {
> +	if (component->module)
> +		return 0;
> +
>   	if (component->driver->module_get_upon_open == !!upon_open &&
>   	    !try_module_get(component->dev->driver->owner))
>   		return -ENODEV;
>   
> +	component->module = 1;
> +

Maybe a red-herring but is there a potential for race conditions here if 
that function is called twice from different places? Don't we need some 
sort of lock for all the new flags introduced here?

Thanks
-Pierre
Mark Brown Feb. 11, 2020, 12:16 p.m. UTC | #2
On Mon, Feb 10, 2020 at 08:41:07AM -0600, Pierre-Louis Bossart wrote:

> Maybe a red-herring but is there a potential for race conditions here if
> that function is called twice from different places? Don't we need some sort
> of lock for all the new flags introduced here?

The probe stuff is all going to get pretty upset if it's called from
multiple paths already.
Kuninori Morimoto Feb. 12, 2020, 12:32 a.m. UTC | #3
Hi Pierre-Louis, Mark

> > Maybe a red-herring but is there a potential for race conditions here if
> > that function is called twice from different places? Don't we need some sort
> > of lock for all the new flags introduced here?
> 
> The probe stuff is all going to get pretty upset if it's called from
> multiple paths already.

Hmm.. indeed we need to consider about it.
But I think it can/should/will be incremental patch.
Thank you for pointing it.

Thank you for your help !!
Best regards
---
Kuninori Morimoto
Kuninori Morimoto Feb. 12, 2020, 1:16 a.m. UTC | #4
Hi Pierre-Louis, Mark

> > > Maybe a red-herring but is there a potential for race conditions here if
> > > that function is called twice from different places? Don't we need some sort
> > > of lock for all the new flags introduced here?
> > 
> > The probe stuff is all going to get pretty upset if it's called from
> > multiple paths already.
> 
> Hmm.. indeed we need to consider about it.
> But I think it can/should/will be incremental patch.
> Thank you for pointing it.

Is it OK for you ?
Or, do we need v4 patch ?

Thank you for your help !!
Best regards
---
Kuninori Morimoto
Pierre-Louis Bossart Feb. 12, 2020, 3:26 p.m. UTC | #5
On 2/11/20 7:16 PM, Kuninori Morimoto wrote:
> 
> Hi Pierre-Louis, Mark
> 
>>>> Maybe a red-herring but is there a potential for race conditions here if
>>>> that function is called twice from different places? Don't we need some sort
>>>> of lock for all the new flags introduced here?
>>>
>>> The probe stuff is all going to get pretty upset if it's called from
>>> multiple paths already.
>>
>> Hmm.. indeed we need to consider about it.
>> But I think it can/should/will be incremental patch.
>> Thank you for pointing it.
> 
> Is it OK for you ?
> Or, do we need v4 patch ?

I can't prove that the code is broken, this was a question. If there is 
a follow-up that looks into potential conflicts that's fine, I can see 
the benefits of the series as is so

Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>

Thank you Morimoto-san for all this work, much appreciated.
diff mbox series

Patch

diff --git a/include/sound/soc-component.h b/include/sound/soc-component.h
index 154d02f..1866ecc 100644
--- a/include/sound/soc-component.h
+++ b/include/sound/soc-component.h
@@ -147,8 +147,6 @@  struct snd_soc_component {
 
 	unsigned int active;
 
-	unsigned int suspended:1; /* is in suspend PM state */
-
 	struct list_head list;
 	struct list_head card_aux_list; /* for auxiliary bound components */
 	struct list_head card_list;
@@ -182,6 +180,11 @@  struct snd_soc_component {
 	struct dentry *debugfs_root;
 	const char *debugfs_prefix;
 #endif
+
+	/* bit field */
+	unsigned int suspended:1; /* is in suspend PM state */
+	unsigned int opened:1;
+	unsigned int module:1;
 };
 
 #define for_each_component_dais(component, dai)\
diff --git a/sound/soc/soc-component.c b/sound/soc/soc-component.c
index 14e175c..ee00c09 100644
--- a/sound/soc/soc-component.c
+++ b/sound/soc/soc-component.c
@@ -297,34 +297,55 @@  EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
 int snd_soc_component_module_get(struct snd_soc_component *component,
 				 int upon_open)
 {
+	if (component->module)
+		return 0;
+
 	if (component->driver->module_get_upon_open == !!upon_open &&
 	    !try_module_get(component->dev->driver->owner))
 		return -ENODEV;
 
+	component->module = 1;
+
 	return 0;
 }
 
 void snd_soc_component_module_put(struct snd_soc_component *component,
 				  int upon_open)
 {
-	if (component->driver->module_get_upon_open == !!upon_open)
+	if (component->module &&
+	    component->driver->module_get_upon_open == !!upon_open)
 		module_put(component->dev->driver->owner);
+
+	component->module = 0;
 }
 
 int snd_soc_component_open(struct snd_soc_component *component,
 			   struct snd_pcm_substream *substream)
 {
-	if (component->driver->open)
-		return component->driver->open(component, substream);
-	return 0;
+	int ret = 0;
+
+	if (!component->opened &&
+	    component->driver->open)
+		ret = component->driver->open(component, substream);
+
+	if (ret == 0)
+		component->opened = 1;
+
+	return ret;
 }
 
 int snd_soc_component_close(struct snd_soc_component *component,
 			    struct snd_pcm_substream *substream)
 {
-	if (component->driver->close)
-		return component->driver->close(component, substream);
-	return 0;
+	int ret = 0;
+
+	if (component->opened &&
+	    component->driver->close)
+		ret = component->driver->close(component, substream);
+
+	component->opened = 0;
+
+	return ret;
 }
 
 int snd_soc_component_prepare(struct snd_soc_component *component,
diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
index d53afb9..ae94d8a 100644
--- a/sound/soc/soc-pcm.c
+++ b/sound/soc/soc-pcm.c
@@ -463,16 +463,13 @@  static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
 }
 
-static int soc_pcm_components_open(struct snd_pcm_substream *substream,
-				   struct snd_soc_component **last)
+static int soc_pcm_components_open(struct snd_pcm_substream *substream)
 {
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	struct snd_soc_component *component;
 	int i, ret = 0;
 
 	for_each_rtd_components(rtd, i, component) {
-		*last = component;
-
 		ret = snd_soc_component_module_get_when_open(component);
 		if (ret < 0) {
 			dev_err(component->dev,
@@ -489,21 +486,17 @@  static int soc_pcm_components_open(struct snd_pcm_substream *substream,
 			return ret;
 		}
 	}
-	*last = NULL;
+
 	return 0;
 }
 
-static int soc_pcm_components_close(struct snd_pcm_substream *substream,
-				    struct snd_soc_component *last)
+static int soc_pcm_components_close(struct snd_pcm_substream *substream)
 {
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	struct snd_soc_component *component;
 	int i, r, ret = 0;
 
 	for_each_rtd_components(rtd, i, component) {
-		if (component == last)
-			break;
-
 		r = snd_soc_component_close(component, substream);
 		if (r < 0)
 			ret = r; /* use last ret */
@@ -545,7 +538,7 @@  static int soc_pcm_open(struct snd_pcm_substream *substream)
 		goto out;
 	}
 
-	ret = soc_pcm_components_open(substream, &component);
+	ret = soc_pcm_components_open(substream);
 	if (ret < 0)
 		goto component_err;
 
@@ -642,7 +635,7 @@  static int soc_pcm_open(struct snd_pcm_substream *substream)
 		snd_soc_dai_shutdown(codec_dai, substream);
 
 component_err:
-	soc_pcm_components_close(substream, component);
+	soc_pcm_components_close(substream);
 
 	snd_soc_dai_shutdown(cpu_dai, substream);
 out:
@@ -696,7 +689,7 @@  static int soc_pcm_close(struct snd_pcm_substream *substream)
 
 	soc_rtd_shutdown(rtd, substream);
 
-	soc_pcm_components_close(substream, NULL);
+	soc_pcm_components_close(substream);
 
 	snd_soc_dapm_stream_stop(rtd, substream->stream);