Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v5] ALSA: line6: Stop URBs synchronously via sync_stop callback
Date: Sun, 22 Nov 2026 09:51:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-227863@kernel.org> (raw)
In-Reply-To: <20261122093454.14422-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: line6: missing URB sync on shared stream stop before
  snd_line6_prepare()
- [Medium] ALSA: line6: race between line6_kill_audio_urbs() and
  snd_line6_trigger()
--

commit 61dfc4a9ef88afb11aedb1393a3ece64bb26365b
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

ALSA: line6: Stop URBs synchronously via sync_stop callback

This commit replaces the polling in line6_wait_clear_audio_urbs() with
synchronous URB termination via usb_kill_urb() in line6_kill_audio_urbs(),
wires it up to the PCM sync_stop callback, and drops the wait from
snd_line6_prepare().

> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..16e6f9900514b 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -109,29 +109,37 @@ static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
>  }
>  
>  /*
> -	Wait until unlinking of all currently active URBs has been finished.
> -*/
> -static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
> -					struct line6_pcm_stream *pcms)
> + * Mark all currently active URBs for unlinking and kill them.
> + *
> + * The unlink bit is set under the stream lock, so the completion
> + * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
> + * waits for the giveback of every URB, hence the stream buffer is no
> + * longer referenced by the host controller.  The unlink marks are
> + * cleared afterwards, so that a later stream start can submit the URBs
> + * again.  This function must be called from a sleepable context.
> + */
> +static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
> +				  struct line6_pcm_stream *pcms)
>  {
> -	int timeout = HZ;
>  	int i;
> -	int alive;
>  
> -	do {
> -		alive = 0;
> -		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
> +	/* May be called when the URB creation failed during the probe */
> +	if (!pcms->urbs)
> +		return;
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
>  			if (test_bit(i, &pcms->active_urbs))
> -				alive++;
> -		}
> -		if (!alive)
> -			break;
> -		set_current_state(TASK_UNINTERRUPTIBLE);
> -		schedule_timeout(1);
> -	} while (--timeout > 0);
> -	if (alive)
> -		dev_err(line6pcm->line6->ifcdev,
> -			"timeout: still %d active urbs..\n", alive);
> +				set_bit(i, &pcms->unlink_urbs);
> +	}
> +
> +	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +		if (pcms->urbs[i])
> +			usb_kill_urb(pcms->urbs[i]);
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		pcms->unlink_urbs = 0;
> +	}
>  }

[Severity: Medium]
Can line6_kill_audio_urbs() race with a concurrent snd_line6_trigger() on
a shared line6_pcm_stream?

line6_pcm_acquire() (called by snd_line6_capture_open() for both out and
in, and by mixer controls for monitor/impulse) and capture's
snd_line6_sync_stop() (which also kills URBs on line6pcm->out for
LINE6_CAP_IN_NEEDS_OUT devices) hold only line6pcm->state_mutex, check
!pstr->running outside pstr->lock, and call line6_kill_audio_urbs().
Meanwhile, an already-prepared PCM substream on that direction can enter
snd_line6_trigger(SNDRV_PCM_TRIGGER_START) -> line6_stream_start(), which
only takes pstr->lock:

CPU0                                  CPU1
snd_line6_capture_open()
  line6_pcm_acquire()
    lock(&line6pcm->state_mutex)
    if (!out.running)
      line6_kill_audio_urbs(..., &out)
        lock(&out.lock)
        /* active_urbs == 0 */
        unlock(&out.lock)
                                      snd_line6_trigger(PLAYBACK, START)
                                        line6_stream_start(..., &out)
                                          lock(&out.lock)
                                          set_bit(..., &out.running)
                                          line6_submit_audio_out_all_urbs()
                                          unlock(&out.lock)
        for (i = 0; i < iso_buffers; i++)
          usb_kill_urb(out.urbs[i])  <-- kills newly submitted playback URBs

Because line6_kill_audio_urbs() does not recheck pcms->running under
pcms->lock and drops pcms->lock across the unconditional usb_kill_urb()
loop, a concurrent playback start either has its newly submitted URBs
killed (leaving playback running with zero active URBs) or sees
active_urbs || unlink_urbs non-zero in line6_stream_start() and skips URB
submission.  In addition, if a stream starts and stops before the final
pcms->unlink_urbs = 0 assignment, its newly set unlink bits are cleared
and completion callbacks can resubmit URBs after stop.

[ ... ]

> @@ -592,15 +598,36 @@ int line6_init_pcm(struct usb_line6 *line6,
>  }
>  EXPORT_SYMBOL_GPL(line6_init_pcm);
>  
> -/* prepare pcm callback */
> -int snd_line6_prepare(struct snd_pcm_substream *substream)
> +/* common PCM sync_stop callback */
> +int snd_line6_sync_stop(struct snd_pcm_substream *substream)
>  {
>  	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
>  	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
>  
>  	guard(mutex)(&line6pcm->state_mutex);
>  	if (!pstr->running)
> -		line6_wait_clear_audio_urbs(line6pcm, pstr);
> +		line6_kill_audio_urbs(line6pcm, pstr);
> +	/*
> +	 * A capture stream may use the playback stream as a helper, and
> +	 * snd_line6_trigger() stops it together with the capture stream.
> +	 * Stop the leftover playback URBs here as well, so that the helper
> +	 * stream can be restarted at the next trigger; skip it while
> +	 * another playback stream is still running.
> +	 */
> +	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
> +	    (line6pcm->line6->properties->capabilities &
> +	     LINE6_CAP_IN_NEEDS_OUT) &&
> +	    !line6pcm->out.running)
> +		line6_kill_audio_urbs(line6pcm, &line6pcm->out);
> +	return 0;
> +}
> +
> +/* prepare pcm callback */
> +int snd_line6_prepare(struct snd_pcm_substream *substream)
> +{
> +	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
> +
> +	guard(mutex)(&line6pcm->state_mutex);

[Severity: Medium]
Does dropping the !pstr->running URB synchronization from
snd_line6_prepare() leave unlinking URBs unsynchronized when another
stream sharing pstr stops after this substream?

snd_pcm_sync_stop() only invokes snd_line6_sync_stop() when
substream->runtime->stop_operating is set, and clears stop_operating
immediately.  Two cases then reach snd_line6_prepare() and
line6_stream_start() with pstr->running == 0 without ever calling
line6_kill_audio_urbs():

1. A PCM substream stops first while another stream type (such as
   LINE6_STREAM_MONITOR, LINE6_STREAM_IMPULSE, or a companion PCM/capture
   helper stream) is still running on pstr.  Its snd_line6_sync_stop()
   consumes stop_operating but skips line6_kill_audio_urbs() because
   pstr->running is still non-zero.  When the other stream later stops
   (for example via XRUN in trigger(STOP) -> line6_stream_stop(), or via
   __line6_pcm_release()), pstr->running drops to 0 with only an
   asynchronous line6_unlink_audio_urbs() call.
2. LINE6_STREAM_MONITOR or LINE6_STREAM_IMPULSE is stopped via
   __line6_pcm_release() while an open PCM substream holds a bit in
   pstr->opened (such as BIT(LINE6_STREAM_PCM) after hw_params or
   BIT(LINE6_STREAM_CAPTURE_HELPER) after capture open).
   line6_buffer_release() skips line6_kill_audio_urbs() because
   pstr->opened is non-zero, and stopping monitor/impulse does not set
   stop_operating on the PCM substream.

When the PCM substream is then prepared and started, line6_stream_start()
can see pstr->active_urbs || pstr->unlink_urbs still non-zero:

sound/usb/line6/pcm.c:line6_stream_start() {
	guard(spinlock_irqsave)(&pstr->lock);
	if (!test_and_set_bit(type, &pstr->running) &&
	    !(pstr->active_urbs || pstr->unlink_urbs)) {
		pstr->count = 0;
		...
}

line6_stream_start() sets BIT(type) in pstr->running and skips URB
submission, leaving the stream stalled with no active URBs once the
unlinked URBs complete (or allowing an URB that raced with the unlocked
line6_unlink_audio_urbs() scan to keep resubmitting while stopped).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122093454.14422-1-25031212351@stu.xidian.edu.cn?part=1

      reply	other threads:[~2026-11-22  9:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 10:40 [PATCH v3] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
2026-11-22  8:16 ` Takashi Iwai
2026-11-22  9:04   ` [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback YuanZhe Liu
2026-11-22  9:20     ` sashiko-bot
2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
2026-11-22  9:51     ` sashiko-bot [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=sashiko-outbox-227863@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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