Commit 92a586bd authored by Takashi Iwai's avatar Takashi Iwai

ALSA: usb-audio: Fix races at disconnection and PCM closing

When a USB-audio device is disconnected while PCM is still running, we
still see some race: the disconnect callback calls
snd_usb_endpoint_free() that calls release_urbs() and then kfree()
while a PCM stream would be closed at the same time and calls
stop_endpoints() that leads to wait_clear_urbs().  That is, the EP
object might be deallocated while a PCM stream is syncing with
wait_clear_urbs() with the same EP.

Basically calling multiple wait_clear_urbs() would work fine, also
calling wait_clear_urbs() and release_urbs() would work, too, as
wait_clear_urbs() just reads some fields in ep.  The problem is the
succeeding kfree() in snd_pcm_endpoint_free().

This patch moves out the EP deallocation into the later point, the
destructor callback.  At this stage, all PCMs must have been already
closed, so it's safe to free the objects.
Reported-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 8b3dfdaf
...@@ -307,6 +307,11 @@ static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) ...@@ -307,6 +307,11 @@ static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
static int snd_usb_audio_free(struct snd_usb_audio *chip) static int snd_usb_audio_free(struct snd_usb_audio *chip)
{ {
struct list_head *p, *n;
list_for_each_safe(p, n, &chip->ep_list)
snd_usb_endpoint_free(p);
mutex_destroy(&chip->mutex); mutex_destroy(&chip->mutex);
kfree(chip); kfree(chip);
return 0; return 0;
...@@ -585,7 +590,7 @@ static void snd_usb_audio_disconnect(struct usb_device *dev, ...@@ -585,7 +590,7 @@ static void snd_usb_audio_disconnect(struct usb_device *dev,
struct snd_usb_audio *chip) struct snd_usb_audio *chip)
{ {
struct snd_card *card; struct snd_card *card;
struct list_head *p, *n; struct list_head *p;
if (chip == (void *)-1L) if (chip == (void *)-1L)
return; return;
...@@ -598,14 +603,16 @@ static void snd_usb_audio_disconnect(struct usb_device *dev, ...@@ -598,14 +603,16 @@ static void snd_usb_audio_disconnect(struct usb_device *dev,
mutex_lock(&register_mutex); mutex_lock(&register_mutex);
chip->num_interfaces--; chip->num_interfaces--;
if (chip->num_interfaces <= 0) { if (chip->num_interfaces <= 0) {
struct snd_usb_endpoint *ep;
snd_card_disconnect(card); snd_card_disconnect(card);
/* release the pcm resources */ /* release the pcm resources */
list_for_each(p, &chip->pcm_list) { list_for_each(p, &chip->pcm_list) {
snd_usb_stream_disconnect(p); snd_usb_stream_disconnect(p);
} }
/* release the endpoint resources */ /* release the endpoint resources */
list_for_each_safe(p, n, &chip->ep_list) { list_for_each_entry(ep, &chip->ep_list, list) {
snd_usb_endpoint_free(p); snd_usb_endpoint_release(ep);
} }
/* release the midi resources */ /* release the midi resources */
list_for_each(p, &chip->midi_list) { list_for_each(p, &chip->midi_list) {
......
...@@ -986,20 +986,31 @@ void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) ...@@ -986,20 +986,31 @@ void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
wait_clear_urbs(ep); wait_clear_urbs(ep);
} }
/**
* snd_usb_endpoint_release: Tear down an snd_usb_endpoint
*
* @ep: the endpoint to release
*
* This function does not care for the endpoint's use count but will tear
* down all the streaming URBs immediately.
*/
void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
{
release_urbs(ep, 1);
}
/** /**
* snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
* *
* @ep: the list header of the endpoint to free * @ep: the list header of the endpoint to free
* *
* This function does not care for the endpoint's use count but will tear * This free all resources of the given ep.
* down all the streaming URBs immediately and free all resources.
*/ */
void snd_usb_endpoint_free(struct list_head *head) void snd_usb_endpoint_free(struct list_head *head)
{ {
struct snd_usb_endpoint *ep; struct snd_usb_endpoint *ep;
ep = list_entry(head, struct snd_usb_endpoint, list); ep = list_entry(head, struct snd_usb_endpoint, list);
release_urbs(ep, 1);
kfree(ep); kfree(ep);
} }
......
...@@ -23,6 +23,7 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep); ...@@ -23,6 +23,7 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep);
void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep); void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep);
int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep); int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep);
void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep); void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep);
void snd_usb_endpoint_release(struct snd_usb_endpoint *ep);
void snd_usb_endpoint_free(struct list_head *head); void snd_usb_endpoint_free(struct list_head *head);
int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep); int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment