All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexandre Becholey" <alex@becholey.ch>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v2 4/5] ALSA: usx2y: us144mkii: wait for the URB completion handlers on stop and close
Date: Mon, 16 Nov 2026 08:31:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-216538@kernel.org> (raw)
In-Reply-To: <20261116071707.217448-5-alex@becholey.ch>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: usx2y: us144mkii: tascam_pcm_sync_stop() bypassed after
  pause
- [Low] ALSA: usx2y: unlocked rcu_assign_pointer() races with
  stop_pcm_work
--

commit dcdd2bae04ac4bdf13b9db3d7f98d8186df8888e
Author: Alexandre Becholey <alex@becholey.ch>

ALSA: usx2y: us144mkii: wait for the URB completion handlers on stop and close

This commit protects playback_substream and capture_substream with RCU and
adds synchronize_rcu() calls in .close and a new .sync_stop callback so
URB completion handlers and capture work finish using the substream and
its runtime before they are freed.

> diff --git a/sound/usb/usx2y/us144mkii_pcm.c b/sound/usb/usx2y/us144mkii_pcm.c
> index 03dfb1f388012..7be6710e7fe32 100644
> --- a/sound/usb/usx2y/us144mkii_pcm.c
> +++ b/sound/usb/usx2y/us144mkii_pcm.c
> @@ -259,6 +259,18 @@ int tascam_pcm_hw_free(struct snd_pcm_substream *substream)
>  	return 0;
>  }
>  
> +int tascam_pcm_sync_stop(struct snd_pcm_substream *substream)
> +{
> +	/*
> +	 * The URBs of both streams keep running as long as one of them does.
> +	 * The URB completion handlers and the capture work use the substream
> +	 * inside an RCU read-side section; wait for them before the buffer is
> +	 * freed or reallocated.
> +	 */
> +	synchronize_rcu();
> +	return 0;
> +}
> +
>  int tascam_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
>  {
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);

[Severity: High]
Can tascam_pcm_sync_stop() be skipped when a paused stream is stopped or
closed, allowing do_hw_free() to free runtime->dma_area while
playback_urb_complete() is still copying from it?

tascam_pcm_hw advertises SNDRV_PCM_INFO_PAUSE, and tascam_pcm_trigger()
accepts SNDRV_PCM_TRIGGER_PAUSE_PUSH, which transitions the substream to
SNDRV_PCM_STATE_PAUSED without setting runtime->stop_operating.  However,
tascam_pcm_trigger() does not handle SNDRV_PCM_TRIGGER_PAUSE_RELEASE:

sound/usb/usx2y/us144mkii_pcm.c:tascam_pcm_trigger() {
	...
		case SNDRV_PCM_TRIGGER_STOP:
		case SNDRV_PCM_TRIGGER_SUSPEND:
		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
			if (atomic_read(&tascam->playback_active)) {
				atomic_set(&tascam->playback_active, 0);
				atomic_set(&tascam->capture_active, 0);
				do_stop = true;
			}
			break;
		default:
			err = -EINVAL;
			break;
	...
}

When snd_pcm_drop() (including from snd_pcm_release_substream() before
.close) or snd_pcm_prepare() runs on a paused substream, it first calls
snd_pcm_pause(substream, false) to return the stream to
SNDRV_PCM_STATE_RUNNING before calling snd_pcm_stop().  Because
SNDRV_PCM_TRIGGER_PAUSE_RELEASE fails with -EINVAL, runtime->state remains
SNDRV_PCM_STATE_PAUSED, so snd_pcm_do_stop() sees !snd_pcm_running() and
never sets runtime->stop_operating = true.  snd_pcm_sync_stop() then skips
tascam_pcm_sync_stop():

CPU0                                  CPU1 (<softirq>)
                                      playback_urb_complete()
                                        rcu_read_lock()
                                        lock(&tascam->lock)
                                        running = snd_pcm_running(substream)
                                        unlock(&tascam->lock)
snd_pcm_pause(substream, true)
  tascam_pcm_trigger(PAUSE_PUSH)
  state = SNDRV_PCM_STATE_PAUSED
snd_pcm_release()
  snd_pcm_drop()
    snd_pcm_pause(substream, false)
      tascam_pcm_trigger(PAUSE_RELEASE) -> -EINVAL
    snd_pcm_stop()
      /* !snd_pcm_running(), stop_operating not set */
  do_hw_free()
    snd_pcm_sync_stop()               /* skipped */
    snd_pcm_lib_free_pages()
                                        memcpy(dst_buf, runtime->dma_area, ...)
                                          <-- use-after-free / NULL dereference

> diff --git a/sound/usb/usx2y/us144mkii_playback.c b/sound/usb/usx2y/us144mkii_playback.c
> index 7621d752c899e..6fb3be2bacd98 100644
> --- a/sound/usb/usx2y/us144mkii_playback.c
> +++ b/sound/usb/usx2y/us144mkii_playback.c
> @@ -17,7 +17,8 @@ static int tascam_playback_open(struct snd_pcm_substream *substream)
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
>  
>  	substream->runtime->hw = tascam_pcm_hw;
> -	tascam->playback_substream = substream;
> +	/* Pairs with rcu_dereference() in the URB completion handlers. */
> +	rcu_assign_pointer(tascam->playback_substream, substream);
>  	atomic_set(&tascam->playback_active, 0);
>  
>  	return 0;

[ ... ]

> @@ -479,8 +493,10 @@ void tascam_stop_pcm_work_handler(struct work_struct *work)
>  	struct snd_pcm_substream *playback_ss, *capture_ss;
>  
>  	scoped_guard(spinlock_irqsave, &tascam->lock) {
> -		playback_ss = tascam->playback_substream;
> -		capture_ss = tascam->capture_substream;
> +		playback_ss = rcu_dereference_protected(tascam->playback_substream,
> +							lockdep_is_held(&tascam->lock));
> +		capture_ss = rcu_dereference_protected(tascam->capture_substream,
> +						       lockdep_is_held(&tascam->lock));
>  	}
>  
>  	if (playback_ss)

[Severity: Low]
Does rcu_dereference_protected() in tascam_stop_pcm_work_handler() race
with the unlocked rcu_assign_pointer() in tascam_playback_open() and
tascam_capture_open()?

rcu_dereference_protected() omits READ_ONCE() because it assumes the held
lock prevents concurrent updates, but tascam_playback_open() and
tascam_capture_open() publish the substream pointers without holding
tascam->lock.  If one substream is opened while stop_pcm_work runs for the
other stream, the plain load in tascam_stop_pcm_work_handler() causes a
data race and drops the READ_ONCE() needed to pair with
rcu_assign_pointer().  Either the .open callbacks should assign the
pointers under tascam->lock, or tascam_stop_pcm_work_handler() should use
rcu_dereference_check().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116071707.217448-1-alex@becholey.ch?part=4

  reply	other threads:[~2026-11-16  8:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  7:17 [PATCH v2 0/5] ALSA: usx2y: us144mkii: fix PCM stream lifetime bugs Alexandre Becholey
2026-11-16  7:17 ` [PATCH v2 1/5] ALSA: usx2y: us144mkii: fix stop_pcm_work race with PCM close Alexandre Becholey
2026-11-16  7:17 ` [PATCH v2 2/5] ALSA: usx2y: us144mkii: wait for the capture work on capture close Alexandre Becholey
2026-11-16  7:17 ` [PATCH v2 3/5] ALSA: usx2y: us144mkii: only touch the buffer of a running PCM stream Alexandre Becholey
2026-11-16  7:17 ` [PATCH v2 4/5] ALSA: usx2y: us144mkii: wait for the URB completion handlers on stop and close Alexandre Becholey
2026-11-16  8:31   ` sashiko-bot [this message]
2026-11-16  7:17 ` [PATCH v2 5/5] ALSA: usx2y: us144mkii: do not restart the stream while stop_work runs Alexandre Becholey
2026-11-16 12:14 ` [PATCH v2 0/5] ALSA: usx2y: us144mkii: fix PCM stream lifetime bugs Takashi Iwai
2026-11-16 13:04   ` Alexandre Becholey

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-216538@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alex@becholey.ch \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.