All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: alsa-devel@alsa-project.org, Takashi Iwai <tiwai@suse.de>,
	marcan@marcan.st, Liam Girdwood <lgirdwood@gmail.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Mark Brown <broonie@kernel.org>
Subject: Re: [GIT PULL] sound updates for 5.14-rc1
Date: Sat, 3 Jul 2021 15:38:48 +0900	[thread overview]
Message-ID: <YOAF+EnvdBvSeZnR@workstation> (raw)
In-Reply-To: <CAHk-=wisOVeVpH42f6i5qW1gxtYxbRJQXvpt=mdVx+8p=w-yMg@mail.gmail.com>

Hi,

On Fri, Jul 02, 2021 at 10:19:46PM -0700, Linus Torvalds wrote:
> On Fri, Jul 2, 2021 at 9:37 PM Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > But I thought I'd report this as a likely candidate.
> 
> Confirmed. The watchdog hang bisects right down to commit 9ce650a75a3b
> ("ALSA: usb-audio: Reduce latency at playback start").
> 
> And reverting it on top of my tree also fixes the hang, so it's not
> some bisection fluke.
> 
> I have no idea what is actually wrong with that commit, but it most
> definitely is the problem, and I have reverted it in my tree so that I
> can continue merging stuff tomorrow.

The cause seems to be the attempt to lock PCM substream recursively
introduced by the issued commit.

Would I ask you to test with below patch? I apologize that the patch is
still untested in my side since at present I have no preparation to debug
USB stuffs instantly (I'm just a maintainer for ALSA firewire stack...),
so I'm glad if getting your cooperation for the issue.

======== 8< --------

From f7ab449f10152635ad7083aa73d80e3fb1adabb4 Mon Sep 17 00:00:00 2001
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Date: Sat, 3 Jul 2021 15:23:25 +0900
Subject: [PATCH] ALSA: usb-audio: fix recursive lock of PCM substream when
 starting playback PCM substream

A commit 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
unfortunately introduced the call of snd_pcm_period_elapsed() under acquired
lock of PCM substream. This causes recursive lock and results in dead-lock.

->ioctl(2)
  (sound/core/pcm_native.c)
  ->snd_pcm_stream_lock_irqsave() <-
    ...
    ->struct snd_pcm_ops.trigger()
    (sound/usb/pcm.c)
    = snd_usb_substream_playback_trigger()
      ->start_endpoints()
        (sound/usb/endpoint.c)
        ->snd_usb_endpoint_start()
          ->prepare_outbound_urb()
            ->struct snd_usb_endpoint.prepare_data_urb()
            (sound/usb/pcm.c)
            = prepare_playback_urb()
              (sound/core/pcm_lib.c)
              ->snd_pcm_period_elapsed()
                (sound/core/pcm_native.c)
                ->snd_pcm_stream_lock_irqsave() <-

This commit fixes the issue to use newly added function;
snd_pcm_period_elapsed_under_stream_lock() with condition to check running
context.

Reported-by: Hector Martin <marcan@marcan.st>
Fixes: 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 sound/usb/pcm.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index c66831ee15f9..235070f0236a 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -1395,8 +1395,16 @@ static void prepare_playback_urb(struct snd_usb_substream *subs,
 
 	spin_unlock_irqrestore(&subs->lock, flags);
 	urb->transfer_buffer_length = bytes;
-	if (period_elapsed)
-		snd_pcm_period_elapsed(subs->pcm_substream);
+	if (period_elapsed) {
+		// The callback of struct snd_pcm_ops.trigger with SNDRV_PCM_TRIGGER_START command
+		// can reach here, under acquired lock of PCM substream. To avoid dead-lock, check
+		// current context and call corresponding function.
+		if (in_softirq()) {
+			snd_pcm_period_elapsed(subs->pcm_substream);
+		} else {
+			snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
+		}
+	}
 }
 
 /*
-- 
2.30.2

======== 8< --------

Thanks

Takashi (not subsystem maintainer) Sakamoto

WARNING: multiple messages have this Message-ID (diff)
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Takashi Iwai <tiwai@suse.de>,
	alsa-devel@alsa-project.org, Mark Brown <broonie@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	marcan@marcan.st
Subject: Re: [GIT PULL] sound updates for 5.14-rc1
Date: Sat, 3 Jul 2021 15:38:48 +0900	[thread overview]
Message-ID: <YOAF+EnvdBvSeZnR@workstation> (raw)
In-Reply-To: <CAHk-=wisOVeVpH42f6i5qW1gxtYxbRJQXvpt=mdVx+8p=w-yMg@mail.gmail.com>

Hi,

On Fri, Jul 02, 2021 at 10:19:46PM -0700, Linus Torvalds wrote:
> On Fri, Jul 2, 2021 at 9:37 PM Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > But I thought I'd report this as a likely candidate.
> 
> Confirmed. The watchdog hang bisects right down to commit 9ce650a75a3b
> ("ALSA: usb-audio: Reduce latency at playback start").
> 
> And reverting it on top of my tree also fixes the hang, so it's not
> some bisection fluke.
> 
> I have no idea what is actually wrong with that commit, but it most
> definitely is the problem, and I have reverted it in my tree so that I
> can continue merging stuff tomorrow.

The cause seems to be the attempt to lock PCM substream recursively
introduced by the issued commit.

Would I ask you to test with below patch? I apologize that the patch is
still untested in my side since at present I have no preparation to debug
USB stuffs instantly (I'm just a maintainer for ALSA firewire stack...),
so I'm glad if getting your cooperation for the issue.

======== 8< --------

From f7ab449f10152635ad7083aa73d80e3fb1adabb4 Mon Sep 17 00:00:00 2001
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Date: Sat, 3 Jul 2021 15:23:25 +0900
Subject: [PATCH] ALSA: usb-audio: fix recursive lock of PCM substream when
 starting playback PCM substream

A commit 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
unfortunately introduced the call of snd_pcm_period_elapsed() under acquired
lock of PCM substream. This causes recursive lock and results in dead-lock.

->ioctl(2)
  (sound/core/pcm_native.c)
  ->snd_pcm_stream_lock_irqsave() <-
    ...
    ->struct snd_pcm_ops.trigger()
    (sound/usb/pcm.c)
    = snd_usb_substream_playback_trigger()
      ->start_endpoints()
        (sound/usb/endpoint.c)
        ->snd_usb_endpoint_start()
          ->prepare_outbound_urb()
            ->struct snd_usb_endpoint.prepare_data_urb()
            (sound/usb/pcm.c)
            = prepare_playback_urb()
              (sound/core/pcm_lib.c)
              ->snd_pcm_period_elapsed()
                (sound/core/pcm_native.c)
                ->snd_pcm_stream_lock_irqsave() <-

This commit fixes the issue to use newly added function;
snd_pcm_period_elapsed_under_stream_lock() with condition to check running
context.

Reported-by: Hector Martin <marcan@marcan.st>
Fixes: 9ce650a75a3b ("ALSA: usb-audio: Reduce latency at playback start")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 sound/usb/pcm.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index c66831ee15f9..235070f0236a 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -1395,8 +1395,16 @@ static void prepare_playback_urb(struct snd_usb_substream *subs,
 
 	spin_unlock_irqrestore(&subs->lock, flags);
 	urb->transfer_buffer_length = bytes;
-	if (period_elapsed)
-		snd_pcm_period_elapsed(subs->pcm_substream);
+	if (period_elapsed) {
+		// The callback of struct snd_pcm_ops.trigger with SNDRV_PCM_TRIGGER_START command
+		// can reach here, under acquired lock of PCM substream. To avoid dead-lock, check
+		// current context and call corresponding function.
+		if (in_softirq()) {
+			snd_pcm_period_elapsed(subs->pcm_substream);
+		} else {
+			snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
+		}
+	}
 }
 
 /*
-- 
2.30.2

======== 8< --------

Thanks

Takashi (not subsystem maintainer) Sakamoto

  reply	other threads:[~2021-07-03  6:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-02  8:29 [GIT PULL] sound updates for 5.14-rc1 Takashi Iwai
2021-07-02 22:37 ` pr-tracker-bot
2021-07-03  1:36 ` Linus Torvalds
2021-07-03  4:37   ` Linus Torvalds
2021-07-03  5:19     ` Linus Torvalds
2021-07-03  5:19       ` Linus Torvalds
2021-07-03  6:38       ` Takashi Sakamoto [this message]
2021-07-03  6:38         ` Takashi Sakamoto
2021-07-03  7:56         ` Takashi Iwai
2021-07-03  7:56           ` Takashi Iwai
2021-07-03 12:06           ` Hector Martin
2021-07-03 12:06             ` Hector Martin
2021-07-03 18:34             ` Takashi Iwai
2021-07-03 18:34               ` Takashi Iwai
2021-07-03 18:47               ` Takashi Iwai
2021-07-03 18:47                 ` Takashi Iwai

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=YOAF+EnvdBvSeZnR@workstation \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcan@marcan.st \
    --cc=tiwai@suse.de \
    --cc=torvalds@linux-foundation.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.