diff mbox

ASoC: rsnd: stop all working stream when .remove

Message ID s5h8thtnvyq.wl-tiwai@suse.de (mailing list archive)
State New, archived
Headers show

Commit Message

Takashi Iwai Sept. 5, 2017, 12:58 p.m. UTC
On Tue, 05 Sep 2017 13:35:58 +0200,
Takashi Iwai wrote:
> 
> On Tue, 05 Sep 2017 11:33:42 +0200,
> Takashi Iwai wrote:
> > 
> > On Tue, 05 Sep 2017 10:58:37 +0200,
> > Kuninori Morimoto wrote:
> > > 
> > > Hi Takashi-san
> > > 
> > > Thank you for your feedback
> > > 
> > > > Right, you can't cancel or return an error at that point.
> > > > That is, you'd need to sync (wait) until the all top-level operations are canceled at remove callback.
> > > >
> > > > For example, snd_card_free() processes the disconnection procedure at first,
> > > > then waits for the completion.  That's how the hot-unplug works safely.
> > > > It's implemented, at least, in the top-level driver removal.
> > > >
> > > > Now for the lower level driver, you'd need a similar strategy; notify to the toplevel for
> > > > hot-unplug (disconnect in ALSA), and sync with the stop operation, then continue the rest of its own remove procedure.
> > > 
> > > OK, it needs ALSA SoC framework side new feature.
> > 
> > Not only ASoC but also in all ALSA component generally.
> > The component-level hot unplug isn't implemented yet properly.
> > 
> > > But can I confirm current situation ?
> > > 
> > > In ALSA SoC, it has Card/CPU/Codec/Platform drivers, and we can unbind these randomly.
> > > Now, if I unbind CPU first, it checks connected Card, and will disconnect it if needed (Then, other drivers are as-is).
> > > Because of this, Card will be disconnected automatically, and we can't use it again if user didn't remove all other
> > > remaining drivers and re-bind all drivers again. This is current ALSA SoC I think.
> > > 
> > > If my understanding was correct, your idea is that we want to call remove function for all connected drivers somehow.
> > > And then, Card want to wait all drivers are removed. Correct ?
> > 
> > Right.  Unless we really want to support the hog-plug/unplug of each
> > component, it'd be more straightforward to do the full hot-unplug upon
> > every component unbind action.
> > 
> > > I'm happy to work for it.
> > > But adding new unplug feature is for sync with all "drivers", and this patch is sync for "clk" for my CPU driver.
> > > Can we separate these ?
> > 
> > It belongs with the same thing.  Basically you're tweaking clk per PCM
> > stream status.  By handling the full hot-plug properly, the PCM stream
> > is assured to be stopped, thus you don't have to fiddle with clk in
> > the remove callback at all.
> 
> So my idea is something like below (totally untested): call
> snd_card_disconnect_sync() at the remove call of each component at the
> beginning.
> That assures stopping all pending operations and syncs with the file
> releases.  For ASoC, we may want to wrap it with ASoC structs, but you
> can have an idea by this patch.

An obvious spinlock was forgotten in one place, the revised patch
below.


Takashi

---

Comments

Kuninori Morimoto Sept. 26, 2017, 8:21 a.m. UTC | #1
Hi Takashi

This is reply for old email.
This thread talked about hot-unplug during playback.

	aplay xxx &
	echo xxx > /sys/bus/platform/drivers/xxx/unbind

We need to stop all components on framework level.
And you kindly created its prototype solution.

> ---
> diff --git a/include/sound/core.h b/include/sound/core.h
> index 4104a9d1001f..5f181b875c2f 100644
> --- a/include/sound/core.h
> +++ b/include/sound/core.h
> @@ -133,6 +133,7 @@ struct snd_card {
>  	struct device card_dev;		/* cardX object for sysfs */
>  	const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
>  	bool registered;		/* card_dev is registered? */
> +	wait_queue_head_t remove_sleep;
>  
>  #ifdef CONFIG_PM
>  	unsigned int power_state;	/* power state */
> @@ -240,6 +241,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
>  		 struct snd_card **card_ret);
>  
>  int snd_card_disconnect(struct snd_card *card);
> +void snd_card_disconnect_sync(struct snd_card *card);
>  int snd_card_free(struct snd_card *card);
>  int snd_card_free_when_closed(struct snd_card *card);
>  void snd_card_set_id(struct snd_card *card, const char *id);
> diff --git a/sound/core/init.c b/sound/core/init.c
> index 32ebe2f6bc59..5cde6cc0d867 100644
> --- a/sound/core/init.c
> +++ b/sound/core/init.c
> @@ -255,6 +255,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
>  #ifdef CONFIG_PM
>  	init_waitqueue_head(&card->power_sleep);
>  #endif
> +	init_waitqueue_head(&card->remove_sleep);
>  
>  	device_initialize(&card->card_dev);
>  	card->card_dev.parent = parent;
> @@ -452,6 +453,17 @@ int snd_card_disconnect(struct snd_card *card)
>  }
>  EXPORT_SYMBOL(snd_card_disconnect);
>  
> +void snd_card_disconnect_sync(struct snd_card *card)
> +{
> +	snd_card_disconnect(card);
> +	spin_lock_irq(&card->files_lock);
> +	wait_event_lock_irq(card->remove_sleep,
> +			    list_empty(&card->files_list),
> +			    card->files_lock);
> +	spin_unlock_irq(&card->files_lock);
> +}
> +EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
> +
>  static int snd_card_do_free(struct snd_card *card)
>  {
>  #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
> @@ -957,6 +969,8 @@ int snd_card_file_remove(struct snd_card *card, struct file *file)
>  			break;
>  		}
>  	}
> +	if (list_empty(&card->files_list))
> +		wake_up_all(&card->remove_sleep);
>  	spin_unlock(&card->files_lock);
>  	if (!found) {
>  		dev_err(card->dev, "card file remove problem (%p)\n", file);


I tried this patch, and noticed 2 issues (at this point).
I think below thing happen on this process.

 1) snd_pcm_dev_disconnect() is called
  - status will be SNDRV_PCM_STATE_DISCONNECTED;

 2) snd_pcm_release() is called
  - snd_pcm_release_substream() is called
   - snd_pcm_drop() is called

snd_pcm_drop() will call snd_pcm_stop(), and
it stops each driver. This is needed.
The issue will be happen if 1) happen before 2) (I think)

1st issue is

snd_pcm_drop() has this check

	if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
	    runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
		return -EBADFD;

if 1) happen before 2), snd_pcm_drop() returns immediately,
and doesn't call snd_pcm_stop().

I modified this as quick-hack locally.
Then, 2nd issue happen on snd_pcm_stop(). it will call snd_pcm_do_stop(),
and it has

	if (substream->runtime->trigger_master == substream &&
	    snd_pcm_running(substream))
		substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);

Here, trigger isn't called, because snd_pcm_running() is 0.
This is because status is SNDRV_PCM_STATE_DISCONNECTED if 1) happen before 2).

I don't know detail of these status magic,
but do you have some idea/solution ?

Best regards
---
Kuninori Morimoto
diff mbox

Patch

diff --git a/include/sound/core.h b/include/sound/core.h
index 4104a9d1001f..5f181b875c2f 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -133,6 +133,7 @@  struct snd_card {
 	struct device card_dev;		/* cardX object for sysfs */
 	const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
 	bool registered;		/* card_dev is registered? */
+	wait_queue_head_t remove_sleep;
 
 #ifdef CONFIG_PM
 	unsigned int power_state;	/* power state */
@@ -240,6 +241,7 @@  int snd_card_new(struct device *parent, int idx, const char *xid,
 		 struct snd_card **card_ret);
 
 int snd_card_disconnect(struct snd_card *card);
+void snd_card_disconnect_sync(struct snd_card *card);
 int snd_card_free(struct snd_card *card);
 int snd_card_free_when_closed(struct snd_card *card);
 void snd_card_set_id(struct snd_card *card, const char *id);
diff --git a/sound/core/init.c b/sound/core/init.c
index 32ebe2f6bc59..5cde6cc0d867 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -255,6 +255,7 @@  int snd_card_new(struct device *parent, int idx, const char *xid,
 #ifdef CONFIG_PM
 	init_waitqueue_head(&card->power_sleep);
 #endif
+	init_waitqueue_head(&card->remove_sleep);
 
 	device_initialize(&card->card_dev);
 	card->card_dev.parent = parent;
@@ -452,6 +453,17 @@  int snd_card_disconnect(struct snd_card *card)
 }
 EXPORT_SYMBOL(snd_card_disconnect);
 
+void snd_card_disconnect_sync(struct snd_card *card)
+{
+	snd_card_disconnect(card);
+	spin_lock_irq(&card->files_lock);
+	wait_event_lock_irq(card->remove_sleep,
+			    list_empty(&card->files_list),
+			    card->files_lock);
+	spin_unlock_irq(&card->files_lock);
+}
+EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
+
 static int snd_card_do_free(struct snd_card *card)
 {
 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
@@ -957,6 +969,8 @@  int snd_card_file_remove(struct snd_card *card, struct file *file)
 			break;
 		}
 	}
+	if (list_empty(&card->files_list))
+		wake_up_all(&card->remove_sleep);
 	spin_unlock(&card->files_lock);
 	if (!found) {
 		dev_err(card->dev, "card file remove problem (%p)\n", file);