Linux userland API discussions
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Joonho Lee <mynameisjune111@gmail.com>
Cc: Takashi Iwai <tiwai@suse.de>, Takashi Iwai <tiwai@suse.com>,
	Jaroslav Kysela <perex@perex.cz>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-api@vger.kernel.org
Subject: Re: [RFC PATCH 2/2] ALSA: pcm: refresh capture pointers before drain
Date: Wed, 14 Oct 2026 17:33:11 +0200	[thread overview]
Message-ID: <87jynkcn60.wl-tiwai@suse.de> (raw)
In-Reply-To: <core-drain-linked-lock-reply.cf2e307fcac34b6a@gmail.com>

On Wed, 14 Oct 2026 17:20:55 +0200,
Joonho Lee wrote:
> 
> Hi Takashi,
> 
> Thanks for the suggestion and thoughts. I tried the smaller change and hit a
> recursive locking issue with linked capture streams.
> 
> I used a test driver with two linked capture substreams and made the
> later member's pointer callback return SNDRV_PCM_POS_XRUN during
> drain. This led to the function calls as follows! :
> 
>   snd_pcm_do_drain_init()
>     snd_pcm_update_hw_ptr()
>       snd_pcm_update_hw_ptr0()
>         __snd_pcm_xrun()
>           snd_pcm_stop()
>             snd_pcm_action()
>               snd_pcm_stream_group_ref()
> 
> The outer drain action already holds the group locks here. Lockdep
> reported the recursive acquisition of group->lock for atomic streams and
> group->mutex for nonatomic streams. Both tests remained stuck until
> the 45-second timeout. Removing the link let both tests complete.

OK, so my previous assumption was wrong, obviously...

> I went back to the notify=false approach from your first sketch, which is keeping the pointer update inside snd_pcm_do_drain_init(). I also
> added the guard to the early SNDRV_PCM_POS_XRUN branch, since that
> path still called __snd_pcm_xrun(). Drain init then handles the stop,
> while ordinary pointer updates keep their existing behavior.
> 
> With this change, all 24 linked test cases passed across both locking
> modes, including pointer errors, tail reads and reopen/recovery.
> KASAN and lockdep stayed quiet. This was tested with a synthetic
> driver under ARM64 QEMU; physical-device testing is still pending.
> 
> One thing I wanted to check before sending a revision is, how should
> drain report a pointer error?
> 
> The current change puts the failing member in SETUP and allows a
> healthy linked member to keep its tail in DRAINING. A blocking
> capture-only drain returns 0 if the rest of the action succeeds,
> and reading the failed member afterwards returns -EBADFD.
> 
> Would you prefer that behavior, or should we remember the error,
> finish stopping all members, and return -EPIPE from drain? My v2 proposal
> returned -EPIPE and moved the linked group to XRUN, so this changes
> what userspace sees.

Honestly speaking, I'm no longer sure whether my suggestion is better
for now.  It's certainly clearer if we can call the update_hwptr stuff
inside drain_init call, but this looks like a hard way, and the
conditional call at each PCM operation sounds fragile, too.

Maybe we can go back to your way as v2, but slightly differently.
Namely, factor out the snd_pcm_link_rwsem and update hwptr loop into a
helper function, and just call it in snd_pcm_drain().  The already
existing calls of snd_pcm_pause() and snd_pcm_action() with
snd_pcm_action_drain_init can be kept as-is; they need no protection
of snd_pcm_link_rwsem.

Also, I think it's fine to call snd_pcm_update_hw_ptr() no matter
whether it's playback or capture stream.


thanks,

Takashi

  parent reply	other threads:[~2026-10-14 15:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  3:30 [RFC PATCH 0/2] ALSA: pcm: preserve capture tail during drain Joonho Lee
2026-10-14  3:30 ` [RFC PATCH 1/2] ALSA: pcm: allow capture reads while draining Joonho Lee
2026-10-14  3:30 ` [RFC PATCH 2/2] ALSA: pcm: refresh capture pointers before drain Joonho Lee
2026-10-14  8:15   ` Takashi Iwai
2026-10-14  8:35     ` Takashi Iwai
2026-10-14 15:20       ` Joonho Lee
2026-10-14 15:24         ` [RFC PATCH v3 0/2] ALSA: pcm: preserve capture tail during drain Joonho Lee
2026-10-14 15:24           ` [RFC PATCH v3 1/2] ALSA: pcm: allow capture reads while draining Joonho Lee
2026-10-14 15:24           ` [RFC PATCH v3 2/2] ALSA: pcm: refresh capture pointers in the drain action Joonho Lee
2026-10-14 15:33         ` Takashi Iwai [this message]
2026-10-14  8:15 ` [RFC PATCH v2 0/2] ALSA: pcm: preserve capture tail during drain Joonho Lee
2026-10-14  8:15   ` [RFC PATCH v2 1/2] ALSA: pcm: allow capture reads while draining Joonho Lee
2026-10-14  8:15   ` [RFC PATCH v2 2/2] ALSA: pcm: refresh capture pointers before drain Joonho Lee

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=87jynkcn60.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=mynameisjune111@gmail.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /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