This is a note to let you know that I've just added the patch titled ALSA: usb-audio: Handle invalid running state at releasing EP to the 5.11-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: alsa-usb-audio-handle-invalid-running-state-at-releasing-ep.patch and it can be found in the queue-5.11 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From d6cda4655e2a7612a1e48c49795a5330abc01c5a Mon Sep 17 00:00:00 2001 From: Takashi Iwai <tiwai@xxxxxxx> Date: Sat, 6 Feb 2021 21:30:50 +0100 Subject: ALSA: usb-audio: Handle invalid running state at releasing EP From: Takashi Iwai <tiwai@xxxxxxx> commit d6cda4655e2a7612a1e48c49795a5330abc01c5a upstream. When we stop an endpoint in release_urbs(), it ignores the inconsistent endpoint state and tries to release the resources. This shouldn't happen in theory, but it's still safer to abort the release and let the caller proper error handling. Also, stop_and_unlink_urbs() called from release_urbs() does two step works, and it's more straightforward to split this to two functions again, so that the call from the PCM trigger won't take the path with sleeping. This patch modifies the EP management code to adapt two points above. Fixes: d0f09d1e4a88 ("ALSA: usb-audio: Refactoring endpoint URB deactivation") Cc: <stable@xxxxxxxxxxxxxxx> Link: https://lore.kernel.org/r/20210206203052.15606-2-tiwai@xxxxxxx Signed-off-by: Takashi Iwai <tiwai@xxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- sound/usb/endpoint.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c @@ -868,24 +868,22 @@ void snd_usb_endpoint_sync_pending_stop( } /* - * Stop and unlink active urbs. + * Stop active urbs * - * This function checks and clears EP_FLAG_RUNNING state. - * When @wait_sync is set, it waits until all pending URBs are killed. + * This function moves the EP to STOPPING state if it's being RUNNING. */ -static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force, - bool wait_sync) +static int stop_urbs(struct snd_usb_endpoint *ep, bool force) { unsigned int i; if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */ return -EBADFD; - if (atomic_read(&ep->running)) + if (!force && atomic_read(&ep->running)) return -EBUSY; if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags)) - goto out; + return 0; set_bit(EP_FLAG_STOPPING, &ep->flags); INIT_LIST_HEAD(&ep->ready_playback_urbs); @@ -901,24 +899,25 @@ static int stop_and_unlink_urbs(struct s } } - out: - if (wait_sync) - return wait_clear_urbs(ep); return 0; } /* * release an endpoint's urbs */ -static void release_urbs(struct snd_usb_endpoint *ep, int force) +static int release_urbs(struct snd_usb_endpoint *ep, bool force) { - int i; + int i, err; /* route incoming urbs to nirvana */ snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); - /* stop urbs */ - stop_and_unlink_urbs(ep, force, true); + /* stop and unlink urbs */ + err = stop_urbs(ep, force); + if (err) + return err; + + wait_clear_urbs(ep); for (i = 0; i < ep->nurbs; i++) release_urb_ctx(&ep->urb[i]); @@ -928,6 +927,7 @@ static void release_urbs(struct snd_usb_ ep->syncbuf = NULL; ep->nurbs = 0; + return 0; } /* @@ -1118,7 +1118,7 @@ static int data_ep_set_params(struct snd return 0; out_of_memory: - release_urbs(ep, 0); + release_urbs(ep, false); return -ENOMEM; } @@ -1162,7 +1162,7 @@ static int sync_ep_set_params(struct snd return 0; out_of_memory: - release_urbs(ep, 0); + release_urbs(ep, false); return -ENOMEM; } @@ -1180,7 +1180,9 @@ static int snd_usb_endpoint_set_params(s int err; /* release old buffers, if any */ - release_urbs(ep, 0); + err = release_urbs(ep, false); + if (err < 0) + return err; ep->datainterval = fmt->datainterval; ep->maxpacksize = fmt->maxpacksize; @@ -1433,7 +1435,7 @@ void snd_usb_endpoint_stop(struct snd_us WRITE_ONCE(ep->sync_source->sync_sink, NULL); if (!atomic_dec_return(&ep->running)) - stop_and_unlink_urbs(ep, false, false); + stop_urbs(ep, false); } /** @@ -1446,7 +1448,7 @@ void snd_usb_endpoint_stop(struct snd_us */ void snd_usb_endpoint_release(struct snd_usb_endpoint *ep) { - release_urbs(ep, 1); + release_urbs(ep, true); } /** Patches currently in stable-queue which might be from tiwai@xxxxxxx are queue-5.11/alsa-usb-audio-correct-document-for-snd_usb_endpoint_free_all.patch queue-5.11/alsa-pcm-don-t-call-sync_stop-if-it-hasn-t-been-stopped.patch queue-5.11/alsa-usb-audio-fix-pcm-buffer-allocation-in-non-vmalloc-mode.patch queue-5.11/alsa-usb-audio-handle-invalid-running-state-at-releasing-ep.patch queue-5.11/alsa-usb-audio-more-strict-state-change-in-ep.patch queue-5.11/alsa-hda-add-another-cometlake-h-pci-id.patch queue-5.11/alsa-hda-realtek-quirk-for-hp-spectre-x360-14-amp-setup.patch queue-5.11/alsa-pcm-assure-sync-with-the-pending-stop-operation-at-suspend.patch queue-5.11/alsa-fireface-fix-to-parse-sync-status-register-of-latter-protocol.patch queue-5.11/alsa-hda-realtek-modify-eapd-in-the-alc886.patch queue-5.11/alsa-pcm-call-sync_stop-at-disconnection.patch queue-5.11/alsa-hda-hdmi-drop-bogus-check-at-closing-a-stream.patch queue-5.11/asoc-siu-fix-build-error-by-a-wrong-const-prefix.patch queue-5.11/alsa-usb-audio-don-t-avoid-stopping-the-stream-at-disconnection.patch queue-5.11/alsa-usb-audio-add-implicit-fb-quirk-for-boss-gp-10.patch