Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Rob Duncan <rduncan@tesla.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: "alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>
Subject: Re: Thread spinning in kernel snd_pcm_link()/snd_pcm_unlink()
Date: Tue, 2 Oct 2018 09:55:34 -0700	[thread overview]
Message-ID: <dw77emh8i45lvd.fsf@tesla.com> (raw)
In-Reply-To: <s5h5zyks7m9.wl-tiwai@suse.de>

Hi Takashi,

Thanks for taking a look at this.

> I'm not sure whether that's the case.  Do you mean that one thread
> gets stuck at pcm_release_private() which calls snd_pcm_unlink()?
> Or do you really use the PCM linkage?

We're not explicitly using the link/unlink APIs, so I think it must be
pcm_release_private().

I'll try out your suggestion over the next couple of days.  In the
meantime we've avoided the issue by arranging for the realtime threads
to have the same priority (which I think they should have anyway).

Rob.

At 08:14 on Tue, Oct 02 2018, Takashi wrote:
> On Fri, 28 Sep 2018 18:23:24 +0200,
> Rob Duncan wrote:
>>
>> I'm trying to address a bug where we end up with a thread spinning and
>> consuming an entire cpu.  The issue seems to be this code in
>> sound/core/pcm_native.c:
>>
>>     /* Writer in rwsem may block readers even during its waiting in queue,
>>      * and this may lead to a deadlock when the code path takes read sem
>>      * twice (e.g. one in snd_pcm_action_nonatomic() and another in
>>      * snd_pcm_stream_lock()).  As a (suboptimal) workaround, let writer to
>>      * spin until it gets the lock.
>>      */
>>     static inline void down_write_nonblock(struct rw_semaphore *lock)
>>     {
>>             while (!down_write_trylock(lock))
>>                     cond_resched();
>>     }
>>
>> The original commit for this is 67ec1072b053c15564e6090ab30127895dc77a89
>>
>> What we're suspecting is that a normal thread (SCHED_OTHER) has a reader
>> lock and a real-time thread using SCHED_RR or SCHED_FIFO is trying to
>> take the writer lock.  If both threads are pinned to the same CPU for
>> some reason then the reader thread will never get scheduled (because the
>> real-time writer thread is still runnable), and we will never make
>> progress.
>>
>> Does this sound right?  What can we do to fix this?
>
> I'm not sure whether that's the case.  Do you mean that one thread
> gets stuck at pcm_release_private() which calls snd_pcm_unlink()?
> Or do you really use the PCM linkage?
>
> In the former case, we may loosen it by optimizing like the patch
> below (totally untested).  I guess it won't be a problem about racy
> access, but need double-checks afterward.
>
>
> thanks,
>
> Takashi
>
>
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -2369,7 +2369,8 @@ int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
>
>  static void pcm_release_private(struct snd_pcm_substream *substream)
>  {
> -	snd_pcm_unlink(substream);
> +	if (snd_pcm_stream_linked(substream))
> +		snd_pcm_unlink(substream);
>  }
>
>  void snd_pcm_release_substream(struct snd_pcm_substream *substream)

      reply	other threads:[~2018-10-02 16:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-28 16:23 Thread spinning in kernel snd_pcm_link()/snd_pcm_unlink() Rob Duncan
2018-10-02 15:14 ` Takashi Iwai
2018-10-02 16:55   ` Rob Duncan [this message]

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=dw77emh8i45lvd.fsf@tesla.com \
    --to=rduncan@tesla.com \
    --cc=alsa-devel@alsa-project.org \
    --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