linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Timo Wischer <twischer@de.adit-jv.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: <broonie@kernel.org>, <perex@perex.cz>,
	<alsa-devel@alsa-project.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 04/10] ALSA: aloop: Use always spin_lock_irqsave() for cable->lock
Date: Tue, 26 Mar 2019 09:12:35 +0100	[thread overview]
Message-ID: <3423ff53-7ee3-01e7-c4bf-a4f730fdfe94@de.adit-jv.com> (raw)
In-Reply-To: <s5hlg127uww.wl-tiwai@suse.de>

On 3/25/19 17:58, Takashi Iwai wrote:
> On Mon, 25 Mar 2019 17:40:23 +0100,
> Timo Wischer wrote:
>> Best regards
>> Timo Wischer
>> Engineering Software Base (ADITG/ESB)
>>
>> Tel. +49 5121 49 6938
>> On 3/25/19 17:07, Takashi Iwai wrote:
>>
>>      On Mon, 25 Mar 2019 17:00:38 +0100,
>>      <twischer@de.adit-jv.com> wrote:
>>      
>>          From: Timo Wischer <twischer@de.adit-jv.com>
>>          
>>          to allow the usage of timer callbacks from interrupt context.
>>          For example the sound timer.
>>          
>>      The trigger callback is already irq-disabled.  And, open/close must
>>      not be irq-disabled OTOH.  So these changes must be superfluous.
>>      
>> Hello Takashi,
>>
>> could you explain why open/close must not be irq-disabled?
> The open/close callbacks get called always in sleepable context where
> you can use mutex & co gracefully.  The only non-sleepable (atomic)
> context is pointer and trigger callbacks.  So it's safe to use
> spin_lock() without irq in these callbacks.
> OTOH, the rest (open, close,hw_params, hw_free, prepare) callbacks are
> always sleepable, hence it's safe to use spin_lock_irq() (not
> save/restore) variant.
>
> When a driver declares as non-atomic via PCM flag, all callbacks
> become sleepable, but this isn't the case for aloop driver.
>
>> I see a potential deadlock in case of free_cable() uses only spin_lock()
>> instead of spin_lock_irqsave().
>> For example the following will be executed:
>>
>> loopback_close()
>> free_cable()
>> spin_lock(&dpcm->cable->lock)
> Which spin_lock() call?  The free_cable() calls spin_lock_irq().
>
>>>> Interrupted by jiffies timer IRQ before calling spin_unlock()
>> loopback_jiffies_timer_function()
>> spin_lock_irqsave(&dpcm->cable->lock)
>>>> DEADLOCK due to dpcm->cable->lock is already locked
>> Do you also see this deadlock or do you see any reason why this could not
>> happen?
> If spin_lock_irq() is used in free_cable(), it must be OK.
>
>
> Takashi
Thanks for the detailed explanation.

@all:
Please simple ignore this patch.

Best regards

Timo

>
>
>> Best regards
>>
>> Timo
>>
>>      thanks,
>>      
>>      Takashi
>>
>>          Signed-off-by: Timo Wischer <twischer@de.adit-jv.com>
>>          ---
>>           sound/drivers/aloop.c | 33 +++++++++++++++++++--------------
>>           1 file changed, 19 insertions(+), 14 deletions(-)
>>          
>>          diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c
>>          index 11e8ed6..c6217c4 100644
>>          --- a/sound/drivers/aloop.c
>>          +++ b/sound/drivers/aloop.c
>>          @@ -272,6 +272,7 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
>>                struct loopback_pcm *dpcm = runtime->private_data;
>>                struct loopback_cable *cable = dpcm->cable;
>>                int err = 0, stream = 1 << substream->stream;
>>          +     unsigned long flags;
>>           
>>                switch (cmd) {
>>                case SNDRV_PCM_TRIGGER_START:
>>          @@ -281,39 +282,39 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
>>                                      dpcm->last_jiffies = jiffies;
>>                                      dpcm->pcm_rate_shift = 0;
>>                                      dpcm->last_drift = 0;
>>          -                           spin_lock(&cable->lock);
>>          +                           spin_lock_irqsave(&cable->lock, flags);
>>                                      cable->running |= stream;
>>                                      cable->pause &= ~stream;
>>                                      err = loopback_timer_start(dpcm);
>>          -                           spin_unlock(&cable->lock);
>>          +                           spin_unlock_irqrestore(&cable->lock, flags);
>>                                      if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>>                                                            loopback_active_notify(dpcm);
>>                                      break;
>>                case SNDRV_PCM_TRIGGER_STOP:
>>          -                           spin_lock(&cable->lock);
>>          +                           spin_lock_irqsave(&cable->lock, flags);
>>                                      cable->running &= ~stream;
>>                                      cable->pause &= ~stream;
>>                                      err = loopback_timer_stop(dpcm);
>>          -                           spin_unlock(&cable->lock);
>>          +                           spin_unlock_irqrestore(&cable->lock, flags);
>>                                      if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>>                                                            loopback_active_notify(dpcm);
>>                                      break;
>>                case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
>>                case SNDRV_PCM_TRIGGER_SUSPEND:
>>          -                           spin_lock(&cable->lock);
>>          +                           spin_lock_irqsave(&cable->lock, flags);
>>                                      cable->pause |= stream;
>>                                      err = loopback_timer_stop(dpcm);
>>          -                           spin_unlock(&cable->lock);
>>          +                           spin_unlock_irqrestore(&cable->lock, flags);
>>                                      if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>>                                                            loopback_active_notify(dpcm);
>>                                      break;
>>                case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>>                case SNDRV_PCM_TRIGGER_RESUME:
>>          -                           spin_lock(&cable->lock);
>>          +                           spin_lock_irqsave(&cable->lock, flags);
>>                                      dpcm->last_jiffies = jiffies;
>>                                      cable->pause &= ~stream;
>>                                      err = loopback_timer_start(dpcm);
>>          -                           spin_unlock(&cable->lock);
>>          +                           spin_unlock_irqrestore(&cable->lock, flags);
>>                                      if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
>>                                                            loopback_active_notify(dpcm);
>>                                      break;
>>          @@ -557,12 +558,13 @@ static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
>>           {
>>                struct snd_pcm_runtime *runtime = substream->runtime;
>>                struct loopback_pcm *dpcm = runtime->private_data;
>>          +     unsigned long flags;
>>                snd_pcm_uframes_t pos;
>>           
>>          -     spin_lock(&dpcm->cable->lock);
>>          +     spin_lock_irqsave(&dpcm->cable->lock, flags);
>>                loopback_pos_update(dpcm->cable);
>>                pos = dpcm->buf_pos;
>>          -     spin_unlock(&dpcm->cable->lock);
>>          +     spin_unlock_irqrestore(&dpcm->cable->lock, flags);
>>                return bytes_to_frames(runtime, pos);
>>           }
>>           
>>          @@ -679,10 +681,12 @@ static void free_cable(struct snd_pcm_substream *substream)
>>                if (!cable)
>>                                      return;
>>                if (cable->streams[!substream->stream]) {
>>          +                           unsigned long flags;
>>          +
>>                                      /* other stream is still alive */
>>          -                           spin_lock_irq(&cable->lock);
>>          +                           spin_lock_irqsave(&cable->lock, flags);
>>                                      cable->streams[substream->stream] = NULL;
>>          -                           spin_unlock_irq(&cable->lock);
>>          +                           spin_unlock_irqrestore(&cable->lock, flags);
>>                } else {
>>                                      /* free the cable */
>>                                      loopback->cables[substream->number][dev] = NULL;
>>          @@ -698,6 +702,7 @@ static int loopback_open(struct snd_pcm_substream *substream)
>>                struct loopback_cable *cable = NULL;
>>                int err = 0;
>>                int dev = get_cable_index(substream);
>>          +     unsigned long flags;
>>           
>>                mutex_lock(&loopback->cable_lock);
>>                dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
>>          @@ -753,9 +758,9 @@ static int loopback_open(struct snd_pcm_substream *substream)
>>                else
>>                                      runtime->hw = cable->hw;
>>           
>>          -     spin_lock_irq(&cable->lock);
>>          +     spin_lock_irqsave(&cable->lock, flags);
>>                cable->streams[substream->stream] = dpcm;
>>          -     spin_unlock_irq(&cable->lock);
>>          +     spin_unlock_irqrestore(&cable->lock, flags);
>>           
>>            unlock:
>>                if (err < 0) {
>>          --
>>          2.7.4
>>
>>

  reply	other threads:[~2019-03-26  8:12 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-25 16:00 [PATCH 00/10] ALSA: aloop: Support selection of snd_timer twischer
2019-03-25 16:00 ` [PATCH 01/10] ALSA: aloop: Describe units of variables twischer
2019-03-25 16:00 ` [PATCH 02/10] ALSA: aloop: loopback_timer_start: Support return of error code twischer
2019-03-25 16:00 ` [PATCH 03/10] ALSA: aloop: loopback_timer_stop: " twischer
2019-03-25 16:00 ` [PATCH 04/10] ALSA: aloop: Use always spin_lock_irqsave() for cable->lock twischer
2019-03-25 16:07   ` Takashi Iwai
     [not found]     ` <237e2d2d-9a8f-a273-4c40-db2c670c6858@de.adit-jv.com>
2019-03-25 16:58       ` Takashi Iwai
2019-03-26  8:12         ` Timo Wischer [this message]
2019-03-26  7:49 ` [PATCH 05/10] ALSA: aloop: Use callback functions for timer specific implementations twischer
2019-03-26  7:49   ` [PATCH 06/10] ALSA: aloop: Rename all jiffies timer specific functions twischer
2019-03-26  7:49   ` [PATCH 07/10] ALSA: aloop: Move CABLE_VALID_BOTH to the top of file twischer
2019-03-26  7:49   ` [PATCH 08/10] ALSA: aloop: Support selection of snd_timer instead of jiffies twischer
2019-03-26  7:49   ` [PATCH 09/10] ALSA: pcm: Add snd_pcm_ops for snd_pcm_link() twischer
2019-03-26  8:35     ` Takashi Iwai
     [not found]       ` <3ad5c313-ba2c-b541-8930-3f2515dfca72@de.adit-jv.com>
2019-03-26 14:23         ` Takashi Iwai
2019-03-26 15:16           ` Timo Wischer
2019-03-26 16:00             ` Takashi Iwai
2019-03-27  8:34               ` Timo Wischer
2019-03-27  9:11                 ` Takashi Iwai
2019-03-27  9:26                   ` Timo Wischer
2019-03-27  9:38                     ` Takashi Iwai
2019-04-04 10:18                       ` Timo Wischer
2019-04-05 15:21                         ` Takashi Iwai
2019-03-26  7:52 ` [PATCH 10/10] ALSA: aloop: Use timer of linked card if chosen twischer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3423ff53-7ee3-01e7-c4bf-a4f730fdfe94@de.adit-jv.com \
    --to=twischer@de.adit-jv.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).