Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933378AbcCIQ4O (ORCPT ); Wed, 9 Mar 2016 11:56:14 -0500 Received: from mail-wm0-f48.google.com ([74.125.82.48]:36081 "EHLO mail-wm0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933258AbcCIQ4G (ORCPT ); Wed, 9 Mar 2016 11:56:06 -0500 Subject: Re: [PATCH] usb: gadget: f_midi: added spinlock on transmit function To: linux-usb@vger.kernel.org References: <1457540261-32203-1-git-send-email-eu@felipetonello.com> Cc: linux-kernel@vger.kernel.org, Felipe Balbi , "# v4 . 5+" From: Felipe Ferreri Tonello Message-ID: <56E0561C.109@felipetonello.com> Date: Wed, 9 Mar 2016 16:58:04 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <1457540261-32203-1-git-send-email-eu@felipetonello.com> Content-Type: multipart/mixed; boundary="------------010206060004000606020605" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 11261 Lines: 236 This is a multi-part message in MIME format. --------------010206060004000606020605 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hi, On 09/03/16 16:17, Felipe F. Tonello wrote: > Since f_midi_transmit is called by both ALSA and USB sub-systems, it can > potentially cause a race condition between both calls because f_midi_transmit > is not reentrant nor thread-safe. This is due to an implementation detail that > the transmit function looks for the next available usb request from the fifo > and only enqueues it if there is data to send, otherwise just re-uses it. So, > if both ALSA and USB frameworks calls this function at the same time, > kfifo_seek() will return the same usb_request, which will cause a race > condition. > > To solve this problem a syncronization mechanism is necessary. In this case it > is used a spinlock since f_midi_transmit is also called by usb_request->complete > callback in interrupt context. > > Cc: # v4.5+ > Fixes: e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests") > Signed-off-by: Felipe F. Tonello I'm sorry but I forgot to add v2 to the subject prefix. Anyway, the changes from the previous patch is that this patch is on top of Linus' v4.5-rc7 tag. > --- > drivers/usb/gadget/function/f_midi.c | 14 +++++++++++++- > 1 file changed, 13 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c > index fb1fe96d3215..ecb46d6abf0e 100644 > --- a/drivers/usb/gadget/function/f_midi.c > +++ b/drivers/usb/gadget/function/f_midi.c > @@ -24,6 +24,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -92,6 +93,7 @@ struct f_midi { > /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */ > DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *); > unsigned int in_last_port; > + spinlock_t transmit_lock; > }; > > static inline struct f_midi *func_to_midi(struct usb_function *f) > @@ -535,12 +537,15 @@ static void f_midi_drop_out_substreams(struct f_midi *midi) > static void f_midi_transmit(struct f_midi *midi) > { > struct usb_ep *ep = midi->in_ep; > + unsigned long flags; > bool active; > > /* We only care about USB requests if IN endpoint is enabled */ > if (!ep || !ep->enabled) > goto drop_out; > > + spin_lock_irqsave(&midi->transmit_lock, flags); > + > do { > struct usb_request *req = NULL; > unsigned int len, i; > @@ -552,14 +557,17 @@ static void f_midi_transmit(struct f_midi *midi) > len = kfifo_peek(&midi->in_req_fifo, &req); > if (len != 1) { > ERROR(midi, "%s: Couldn't get usb request\n", __func__); > + spin_unlock_irqrestore(&midi->transmit_lock, flags); > goto drop_out; > } > > /* If buffer overrun, then we ignore this transmission. > * IMPORTANT: This will cause the user-space rawmidi device to block until a) usb > * requests have been completed or b) snd_rawmidi_write() times out. */ > - if (req->length > 0) > + if (req->length > 0) { > + spin_unlock_irqrestore(&midi->transmit_lock, flags); > return; > + } > > for (i = midi->in_last_port; i < MAX_PORTS; i++) { > struct gmidi_in_port *port = midi->in_port[i]; > @@ -611,6 +619,8 @@ static void f_midi_transmit(struct f_midi *midi) > } > } while (active); > > + spin_unlock_irqrestore(&midi->transmit_lock, flags); > + > return; > > drop_out: > @@ -1216,6 +1226,8 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi) > if (status) > goto setup_fail; > > + spin_lock_init(&midi->transmit_lock); > + > ++opts->refcnt; > mutex_unlock(&opts->lock); > > -- Felipe --------------010206060004000606020605 Content-Type: application/pgp-keys; name="0x92698E6A.asc" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0x92698E6A.asc" -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v2 mQINBFYedIcBEACVKGKoEjb3zlvAz5SUvBej7Sx13BPd8hVulQD+mqjfuRFPmZA5 LBXPX1zTRWwGEbbZegP3tLfKP+XekzO6BQhDihMmKuRusdgDsdMtldwhjHuUwKn7 kxB2k79jSG802lAjIv2l5hijOfKIGTATKwiMijuXho54DGltIgNyN/Onwk9HnM6d jsV5uubaI468JRH6j8HXXievo24BDvsimIE75ImiM53ruiwPwEry1hi1CnE5OpqG oe/lt27+nLXijfNZOpBZ3Q+RPVBdqPTkMlBJAa4sg+qwZoSMvQJFAGROiJ7+ICCW O4GPMrAn8CRcCI9ENKBj2dQ4bBEP1a+f16GNMUUU37wocqtyNHo0Pa/DnFh91kcu /2dvUX4XPeEimEoSKroRLOXC9RGSFYB/r9UXqFgbmyQ4TZLx0mAWIjoUQtbIJNRz Pt46UeznCVLJTg7CzIvtv8vwmMFvaepr8ONoZn+tpX8VW4dgzsMZDrVspE0Vg3oo K9JRi1nN3GcJJK4zG2ShvEkPffRHuBuyX5wR8MPRYTShKnJR5qd1cCSK73fCv4DP bjywmGjucqcLiyYbByjmHaqzRaKJclmT/jhs6qZHs+pVLkmHkHdf/WLP6Xgcvmo8 c+SATrJwRsyW9riyMB7uNg3T84umbQrl00nAhcq73rem/602H+Qrh8rEfQARAQAB tC1GZWxpcGUgRmVycmVyaSBUb25lbGxvIDxldUBmZWxpcGV0b25lbGxvLmNvbT6J AjwEEwEIACYCGyMHCwkIBwMCAQYVCAIJCgsEFgIDAQIeAQ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kG/MHBBbNxvSGP1h2yUM5C7g4nc6B+nz 5bIULhw0ojZx+U1i63gJkMsVMjKDAa+mGsWu0vAI3v0AEQEAAYkCHwQYAQgACQIb DAUCVlXsegAKCRDMRLTQkmmOao4vD/9GYLw5VmydK5BJ0sX12QmR7oMK4skMkcg/ gofkJ6njI4ETDG/pQfgBSQ/P0gUMXmhHgmpaRgs0tkS2NwsvURianXiQNpJwUA2g qKngqWlEvGOVkgOw7JKpdLWBvEW4vfP0Z0Q9brdeQG//9T9xyoyTC/jPM9A33NtI KlexBfBT7lXBy/y4tqf96KTOXk3STmI6nmSwmpPPtjxXXGQiMExGxAru+VX0HFxi JBS2fw/ucluVk2kffAO0y7DTL0UH3IWq4wEtHJQlEcujmUkP8PJSVIkhJpNlppR2 97DVxLcMAbnyxCW+ms1lCuZ5KB98KtowipqY9jhAPeP06W2WikENdmpLfNHhyIaM NKF7J+4zqPda7jZN1aKqs5OguJ4V82xXRJ0O6qSAd3Kvwi3MaKtNAv09JFG7UIcK ALJ+OQBzc00z54ii19ZPHjER6cYmcVutBUGOssqtyCOZymVbc7VYecdjxVrLwdDp LHHpcIjFn5ADjIAwGlt5ttkPvFTmqLeSWG9+vtoBShzQdeKJ4AN4r6/for/XsmF4 Jf7qGK8RamVX8lpWaNjUGtVSUOMTaSR1pmQJERDO2/WEZCm5hUFkYDbu+h+9m1W+ KL2ZkyGRC8FoZ46ly1J8koEeEoa57Au/uyK3KyvzlXuCI5zgGE+TohIjOr6d3TN4 SkAyCfQaSw== =JwBB -----END PGP PUBLIC KEY BLOCK----- --------------010206060004000606020605--