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 v2] ALSA: line6: Kill URBs before releasing the PCM buffer
Date: Sat, 21 Nov 2026 10:21:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-226937@kernel.org> (raw)
In-Reply-To: <20261121095643.8196-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: line6: race and stale unlink_urbs in
  line6_kill_audio_urbs()
--

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

ALSA: line6: Kill URBs before releasing the PCM buffer

This commit introduces line6_kill_audio_urbs() and calls it from
line6_buffer_release() instead of line6_wait_clear_audio_urbs() to stop and
wait for active URBs before freeing the PCM stream buffer.

> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..6a8983507a390 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -134,6 +134,31 @@ static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
>  			"timeout: still %d active urbs..\n", alive);
>  }
>  
> +/*
> + * 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 until each of them is given back and hence the stream buffer is
> + * no longer referenced by the host controller.  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 i;
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +			if (test_bit(i, &pcms->active_urbs))
> +				set_bit(i, &pcms->unlink_urbs);
> +	}
> +
> +	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +		if (test_bit(i, &pcms->active_urbs))
> +			usb_kill_urb(pcms->urbs[i]);
> +}

[Severity: High]
Can line6_kill_audio_urbs() skip usb_kill_urb() while an URB completion
callback is still running, and leave stale bits in pcms->unlink_urbs?

In audio_out_callback() and handle_audio_in(), clear_bit() clears the bit
in active_urbs inside the completion handler before unlink_urbs is tested
and before __usb_hcd_giveback_urb() finishes:

CPU0                              CPU1
line6_buffer_release()
  line6_kill_audio_urbs()
    lock(&pcms->lock)
    set_bit(i, &pcms->unlink_urbs)
    unlock(&pcms->lock)
                                  __usb_hcd_giveback_urb()
                                    audio_out_callback()
                                      lock(&pcms->lock)
                                      clear_bit(i, &pcms->active_urbs)
    if (test_bit(i, &pcms->active_urbs))
      /* false: skips usb_kill_urb() */
  kfree(pstr->buffer)
                                      ...
                                    atomic_dec(&urb->use_count)

Because the second loop checks active_urbs outside pcms->lock, CPU0 sees
active_urbs cleared and returns from line6_kill_audio_urbs() while the
callback and URB giveback are still executing on CPU1.

In addition, line6_stream_stop() runs line6_unlink_audio_urbs() without
holding pcms->lock before line6_buffer_release() is called.  If URB k
completes right after line6_unlink_audio_urbs() tests active_urbs and its
callback resubmits using a lower free index (say URB 0),
line6_unlink_audio_urbs() sets bit k in unlink_urbs on the now-idle URB k.
line6_kill_audio_urbs() only kills URBs that are currently in active_urbs
and never clears pcms->unlink_urbs, so bit k remains set and subsequent
calls to line6_stream_start() see pstr->unlink_urbs != 0 and never submit
URBs again.

Calling usb_kill_urb(pcms->urbs[i]) unconditionally for all iso_buffers and
clearing pcms->unlink_urbs afterward would avoid both issues.

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

      reply	other threads:[~2026-11-21 10:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  9:56 [PATCH v2] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
2026-11-21 10:21 ` 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-226937@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