From: sashiko-bot@kernel.org
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v6] ALSA: line6: Stop URBs synchronously via sync_stop callback
Date: Sun, 22 Nov 2026 10:56:47 +0000 [thread overview]
Message-ID: <sashiko-outbox-227902@kernel.org> (raw)
In-Reply-To: <20261122104039.7160-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 in snd_line6_prepare() and
__line6_pcm_release()
- [Medium] ALSA: line6: race between line6_kill_audio_urbs() and
line6_stream_start()
--
commit 20c8f40e0e74158190f3553ea4d2067649a84199
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
ALSA: line6: Stop URBs synchronously via sync_stop callback
This commit replaces the polling loop in line6_wait_clear_audio_urbs()
with line6_kill_audio_urbs(), which marks active URBs under the stream
lock and waits for them with usb_kill_urb(), and wires it up to the PCM
sync_stop callback.
> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..34d68a9ce3285 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -109,29 +109,51 @@ 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. This function must be
> + * called from a sleepable context.
> + *
> + * When @idle_only is set, nothing is done if some stream is still running
> + * on @pcms, and only the URBs that are active at that moment are killed,
> + * so that a concurrently started stream is never stopped.
> + */
> +static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
> + struct line6_pcm_stream *pcms, bool idle_only)
> {
> - int timeout = HZ;
> + unsigned long active;
> int i;
> - int alive;
>
> - do {
> - alive = 0;
> - 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);
> + /* May be called when the URB creation failed during the probe */
> + if (!pcms->urbs)
> + return;
> +
> + scoped_guard(spinlock_irqsave, &pcms->lock) {
> + if (idle_only && pcms->running)
> + return;
> + active = pcms->active_urbs;
> + for_each_set_bit(i, &active, line6pcm->line6->iso_buffers)
> + set_bit(i, &pcms->unlink_urbs);
> + }
> +
> + for_each_set_bit(i, &active, line6pcm->line6->iso_buffers)
> + if (pcms->urbs[i])
> + usb_kill_urb(pcms->urbs[i]);
> +
> + /*
> + * Drop the unlink marks left on URBs that were completed and not
> + * resubmitted, so that a later stream start can submit the URBs
> + * again. An URB that is still active keeps its mark.
> + */
> + scoped_guard(spinlock_irqsave, &pcms->lock) {
> + for_each_set_bit(i, &pcms->unlink_urbs,
> + line6pcm->line6->iso_buffers)
> + if (!test_bit(i, &pcms->active_urbs))
> + clear_bit(i, &pcms->unlink_urbs);
> + }
> }
[Severity: Medium]
Can a stream started concurrently while line6_kill_audio_urbs() is inside
the usb_kill_urb() loop still end up with no active URBs?
snd_line6_trigger(SNDRV_PCM_TRIGGER_START) calls line6_stream_start()
under pcms->lock without holding state_mutex:
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;
/* Submit all currently available URBs */
if (direction == SNDRV_PCM_STREAM_PLAYBACK)
ret = line6_submit_audio_out_all_urbs(line6pcm);
else
ret = line6_submit_audio_in_all_urbs(line6pcm);
}
}
Because line6_kill_audio_urbs() drops pcms->lock before calling
usb_kill_urb(), a concurrent trigger on an already-prepared PCM substream
can interleave while another caller (such as line6_pcm_acquire() or
capture's snd_line6_sync_stop() on a LINE6_CAP_IN_NEEDS_OUT device) is
killing leftover URBs on the same stream:
CPU0 CPU1
line6_kill_audio_urbs(..., true)
lock(&pcms->lock)
active = pcms->active_urbs
set_bit(i, &pcms->unlink_urbs)
unlock(&pcms->lock)
snd_line6_trigger(START)
line6_stream_start()
lock(&pcms->lock)
test_and_set_bit(..., &pcms->running)
/* active_urbs || unlink_urbs != 0 */
/* skips URB submission */
unlock(&pcms->lock)
usb_kill_urb(pcms->urbs[i])
/* callback sees unlink_urbs */
/* does not resubmit */
If instead the unlinking URBs finish their callbacks before the
usb_kill_urb() loop reaches index i, line6_stream_start() sees
active_urbs and unlink_urbs both zero and submits the shared
pcms->urbs[] pool for the new stream, after which the remaining
usb_kill_urb(pcms->urbs[i]) calls kill the newly submitted URBs. In both
cases pcms->running has the new stream's bit set, but all URBs are dead.
[ ... ]
> @@ -592,6 +611,27 @@ 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, true);
> + /*
> + * 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.
> + */
> + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
> + (line6pcm->line6->properties->capabilities &
> + LINE6_CAP_IN_NEEDS_OUT))
> + line6_kill_audio_urbs(line6pcm, &line6pcm->out, true);
> + return 0;
> +}
> +
> /* prepare pcm callback */
> int snd_line6_prepare(struct snd_pcm_substream *substream)
> {
> @@ -599,8 +639,12 @@ int snd_line6_prepare(struct snd_pcm_substream *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);
> + /*
> + * sync_stop() is called only after this substream itself was
> + * stopped; other stream types sharing the stream (monitor, impulse
> + * or the capture helper) may have left URBs unlinking.
> + */
> + line6_kill_audio_urbs(line6pcm, pstr, true);
[Severity: Medium]
Should snd_line6_prepare() also kill leftover URBs on &line6pcm->out when
preparing a capture stream on a LINE6_CAP_IN_NEEDS_OUT device, and should
__line6_pcm_release() do the same when pstr->opened remains non-zero?
As the comment notes, snd_line6_sync_stop() is only called when the
capture substream itself was stopped. If another stream sharing
line6pcm->out (such as monitor, impulse, or playback) just stopped and
left URBs unlinking on line6pcm->out, preparing capture only kills URBs
on pstr (&line6pcm->in). The subsequent capture start trigger then calls
line6_stream_start() for LINE6_STREAM_CAPTURE_HELPER on line6pcm->out
while out.active_urbs or out.unlink_urbs is still non-zero, skipping
playback URB submission.
Similarly, __line6_pcm_release() stops the stream with
line6_stream_stop() (which only runs the asynchronous
line6_unlink_audio_urbs()) and then calls line6_buffer_release(), which
only calls line6_kill_audio_urbs() when pstr->opened becomes 0. If a PCM
substream is already open and in SNDRV_PCM_STATE_PREPARED (holding
BIT(LINE6_STREAM_PCM) or BIT(LINE6_STREAM_CAPTURE_HELPER) in
pstr->opened) when monitor or impulse is released via
line6_pcm_release(), the unlinking URBs are not synchronously killed
there, and starting the already-prepared PCM substream goes straight to
snd_line6_trigger(SNDRV_PCM_TRIGGER_START) without calling
snd_line6_prepare() or snd_line6_sync_stop().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122104039.7160-1-25031212351@stu.xidian.edu.cn?part=1
prev parent reply other threads:[~2026-11-22 10:56 UTC|newest]
Thread overview: 8+ 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
2026-11-22 10:40 ` [PATCH v6] " YuanZhe Liu
2026-11-22 10:56 ` 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-227902@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