Commit 2d19cdc1 authored by Jerry Zhang's avatar Jerry Zhang Committed by Felipe Balbi

usb: gadget: f_midi: Use snd_card_free_when_closed with refcount

Currenly, f_midi_free uses snd_card_free, which will wait
until the user has released the sound card before
returning. However, if the user doesn't release the sound
card, then f_midi_free can block for an arbitrary amount
of time, which also blocks any gadget operations on that
thread.

Instead, we can use snd_card_free_when_closed which returns
before all handles are released. Since f_midi can be
accessed through rmidi if usb_put_function is called before
release_card_device, add refcounting to f_midi_free and
have rawmidi's private free call it. The f_midi memory
is only kfreed when usb_put_function and release_card_device
have both been called.
Signed-off-by: default avatarJerry Zhang <zhangjerry@google.com>
Signed-off-by: default avatarFelipe Balbi <felipe.balbi@linux.intel.com>
parent 24cf3459
...@@ -98,6 +98,7 @@ struct f_midi { ...@@ -98,6 +98,7 @@ struct f_midi {
DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *); DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *);
spinlock_t transmit_lock; spinlock_t transmit_lock;
unsigned int in_last_port; unsigned int in_last_port;
unsigned char free_ref;
struct gmidi_in_port in_ports_array[/* in_ports */]; struct gmidi_in_port in_ports_array[/* in_ports */];
}; };
...@@ -108,6 +109,7 @@ static inline struct f_midi *func_to_midi(struct usb_function *f) ...@@ -108,6 +109,7 @@ static inline struct f_midi *func_to_midi(struct usb_function *f)
} }
static void f_midi_transmit(struct f_midi *midi); static void f_midi_transmit(struct f_midi *midi);
static void f_midi_rmidi_free(struct snd_rawmidi *rmidi);
DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1); DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
...@@ -832,6 +834,8 @@ static int f_midi_register_card(struct f_midi *midi) ...@@ -832,6 +834,8 @@ static int f_midi_register_card(struct f_midi *midi)
SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_INPUT |
SNDRV_RAWMIDI_INFO_DUPLEX; SNDRV_RAWMIDI_INFO_DUPLEX;
rmidi->private_data = midi; rmidi->private_data = midi;
rmidi->private_free = f_midi_rmidi_free;
midi->free_ref++;
/* /*
* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
...@@ -1233,14 +1237,21 @@ static void f_midi_free(struct usb_function *f) ...@@ -1233,14 +1237,21 @@ static void f_midi_free(struct usb_function *f)
midi = func_to_midi(f); midi = func_to_midi(f);
opts = container_of(f->fi, struct f_midi_opts, func_inst); opts = container_of(f->fi, struct f_midi_opts, func_inst);
kfree(midi->id);
mutex_lock(&opts->lock); mutex_lock(&opts->lock);
if (!--midi->free_ref) {
kfree(midi->id);
kfifo_free(&midi->in_req_fifo); kfifo_free(&midi->in_req_fifo);
kfree(midi); kfree(midi);
--opts->refcnt; --opts->refcnt;
}
mutex_unlock(&opts->lock); mutex_unlock(&opts->lock);
} }
static void f_midi_rmidi_free(struct snd_rawmidi *rmidi)
{
f_midi_free(rmidi->private_data);
}
static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f) static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
{ {
struct usb_composite_dev *cdev = f->config->cdev; struct usb_composite_dev *cdev = f->config->cdev;
...@@ -1255,7 +1266,7 @@ static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f) ...@@ -1255,7 +1266,7 @@ static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
card = midi->card; card = midi->card;
midi->card = NULL; midi->card = NULL;
if (card) if (card)
snd_card_free(card); snd_card_free_when_closed(card);
usb_free_all_descriptors(f); usb_free_all_descriptors(f);
} }
...@@ -1299,6 +1310,7 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi) ...@@ -1299,6 +1310,7 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
midi->buflen = opts->buflen; midi->buflen = opts->buflen;
midi->qlen = opts->qlen; midi->qlen = opts->qlen;
midi->in_last_port = 0; midi->in_last_port = 0;
midi->free_ref = 1;
status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL); status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
if (status) if (status)
......
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