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 v4] ALSA: line6: Stop URBs synchronously via sync_stop callback
Date: Sun, 22 Nov 2026 09:20:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-227847@kernel.org> (raw)
In-Reply-To: <20261122090454.6217-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] ALSA: line6: snd_line6_sync_stop() kills URBs while shared
  stream is running
- [Medium] ALSA: line6: snd_line6_sync_stop() misses helper playback
  stream on capture stop
- [Medium] ALSA: line6: unsynchronized URB unlink when pstr->opened
  remains non-zero
- [Medium] ALSA: line6: NULL pointer dereference in
  line6_kill_audio_urbs() on probe error
--

commit 03ea0b5c4363156759700a216da2dd67752857be
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

ALSA: line6: Stop URBs synchronously via sync_stop callback

This commit replaces line6_wait_clear_audio_urbs() with a synchronous
line6_kill_audio_urbs() helper and wires it into the PCM sync_stop
callback while removing the URB wait calls from snd_line6_prepare() and
line6_pcm_acquire().

> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..dc850aa2ebd09 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -109,29 +109,33 @@ 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++) {
> +	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]);

[Severity: Medium]
Can pcms->urbs[i] dereference a NULL pcms->urbs pointer when
line6_init_pcm() fails during probe?

line6_init_pcm() publishes line6->line6pcm before checking
max_packet_size_in/out and before allocating out.urbs and in.urbs.  If one
of those steps fails, line6_probe() unwinds via line6_disconnect() ->
line6_pcm_disconnect() -> line6_kill_audio_urbs() while pcms->urbs is still
NULL.  Checking if (!pcms->urbs) before the loop (as cleanup_urbs() does)
would avoid this.

[ ... ]

> @@ -337,8 +341,6 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
>  		ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
>  		if (ret < 0)
>  			goto error;
> -		if (!pstr->running)
> -			line6_wait_clear_audio_urbs(line6pcm, pstr);
>  	}

[Severity: Medium]
Does dropping the !pstr->running synchronization from line6_pcm_acquire()
and snd_line6_prepare() leave URBs only asynchronously unlinked when
pstr->opened remains non-zero?

When a monitor or impulse stream is stopped via line6_pcm_release(),
__line6_pcm_release() calls line6_stream_stop() (which only runs the
unlocked, asynchronous line6_unlink_audio_urbs()) and then
line6_buffer_release(), which only calls line6_kill_audio_urbs() when
pstr->opened reaches 0.  If an idle PCM substream already holds a bit in
pstr->opened (for example, snd_line6_capture_open() sets
LINE6_STREAM_CAPTURE_HELPER in both out.opened and in.opened),
line6_kill_audio_urbs() is skipped and ALSA core will not invoke
snd_line6_sync_stop() because the PCM substream itself was not running.

If the stream is then started again via line6_pcm_acquire() or
snd_line6_trigger() while active_urbs or unlink_urbs is still non-zero
from the asynchronous unlink, line6_stream_start() sees
(pstr->active_urbs || pstr->unlink_urbs) non-zero, sets the bit in
pstr->running without submitting URBs, and leaves the stream stalled once
the unlinking URBs complete:

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

[ ... ]

> @@ -592,15 +592,23 @@ int line6_init_pcm(struct usb_line6 *line6,
>  }
>  EXPORT_SYMBOL_GPL(line6_init_pcm);
>  
> +/* 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);
> +	line6_kill_audio_urbs(line6pcm, pstr);
> +	return 0;
> +}

[Severity: High]
Does snd_line6_sync_stop() kill in-flight URBs on pstr even when another
stream type in pstr->running is still using the stream?

pstr->running is a bitmask shared across LINE6_STREAM_PCM,
LINE6_STREAM_MONITOR, LINE6_STREAM_IMPULSE, and
LINE6_STREAM_CAPTURE_HELPER.  When a PCM substream stops,
line6_stream_stop() clears LINE6_STREAM_PCM and returns early if another
bit in pstr->running is still set so the URBs keep running:

sound/usb/line6/pcm.c:line6_stream_stop() {
	scoped_guard(spinlock_irqsave, &pstr->lock) {
		clear_bit(type, &pstr->running);
		if (pstr->running)
			return;
	}

	line6_unlink_audio_urbs(line6pcm, pstr);
}

Because snd_line6_sync_stop() does not check if (!pstr->running) before
calling line6_kill_audio_urbs(), stopping a PCM playback or capture stream
(on drop, prepare, hw_free, or close) kills the URBs while software
monitoring (enabled at startup on TonePort devices) or capture on
LINE6_CAP_IN_NEEDS_OUT devices is still active.  With the other stream's
bit still set in pstr->running, later line6_stream_start() calls for that
type see test_and_set_bit(type, &pstr->running) already set and cannot
restart the URBs.

[Severity: Medium]
On devices with LINE6_CAP_IN_NEEDS_OUT, does snd_line6_sync_stop() also
need to synchronize &line6pcm->out when stopping a capture substream?

In snd_line6_trigger(), stopping SNDRV_PCM_STREAM_CAPTURE on a
LINE6_CAP_IN_NEEDS_OUT device also stops LINE6_STREAM_CAPTURE_HELPER on
SNDRV_PCM_STREAM_PLAYBACK via line6_stream_stop(), which only performs an
asynchronous line6_unlink_audio_urbs() on &line6pcm->out.  Because
snd_line6_sync_stop() only kills URBs on get_stream(line6pcm,
substream->stream) (&line6pcm->in) and the wait in snd_line6_prepare() is
removed, re-preparing and restarting capture while &line6pcm->out URBs are
still unlinking causes line6_stream_start() for
LINE6_STREAM_CAPTURE_HELPER to see active_urbs or unlink_urbs non-zero and
skip submitting playback URBs, stalling capture.

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

  reply	other threads:[~2026-11-22  9:20 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 [this message]
2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
2026-11-22  9:51     ` sashiko-bot

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-227847@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