From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796439162; b=Iqlmz//F1hDpCiIxSKootHYCXSilbFz6pB7a/IVGVG79AU97PvxQlpd9SJ7ejB05p5Uf rjxDRRJGWzmDhRBgDzYPTlGo3V2HfEU2/uOd0KhnQANc7wozASbIEfmfNYBlpmuLF36t5 LThzbP/yjXOIQTaEiMq+bW1eDH6LKSo0Keiw885fZiPc41t14V6DwjYgkEVqRFxrJ7dST VfpP5uq3CzuX3iAw5nHm00OcVujlk+KnVADGKBHe8fTh3ahjXs68yJk0tVu5m3c1N5QCF XTyjl72kVjJbgZpesrHA/CzS/pae7Mqv0Q7gc7LL8OLwUjzn9hJATu6nRimwjFJHMyQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796439162; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=PlLYGexT3nJlD4a0dFWUf1HsyU3yCM0Q82nfZqhq0mI=; b=Fqg7Hf4eTNbZDg9kixId6JNSi/BNO20emqEijHczjSdcH+FAwY2tw3TT1/SEIr0jypiE 6A/5AHl26wD01xBevt6tFyNuWRN7UcXE7yl38se6WvDYDPHexvMhv82XZ2dNnH8UW8drk BW2QtlwjVwPelBygmsnta6T1DU5Fxl6h9BxS8UtRbQVc/NBDgeK5Nls6ARJCCbZnPIKFr daQFZ6JGeur8uU6A8B+WyHKXLtJ9OFHnTuNEpDpelITLhc4UTI8fmfQctvHQrgttERYEj JwVUw8siOruYK0h3l14CQJfZEScs5qTQvo8OnsCK83ylOWFZHEOX0CgFaeeKs932lOg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=R8gCVwLG; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF91D606A322; Sat, 5 Dec 2026 02:52:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=R8gCVwLG Received: by smtp.kernel.org (Postfix) with ESMTPSA id A44281F0092B; Sat, 5 Dec 2026 02:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796439161; bh=PlLYGexT3nJlD4a0dFWUf1HsyU3yCM0Q82nfZqhq0mI=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=R8gCVwLGnEWg7beVI15MdfQe/0Ks6zz+ILZ3Q5bC1ZnU3xnOTNwjQcAVbZHbB298U cyxYB2BJaPKrkUHcPH7loAM3cpvW7mjVg3ZIAsUYUw8nBnkMQp2ZX7QmLJlbNNvxxE r5csnSWyHyC4lCTarj3qO9fxISnJiCzRzV/uKhZaueB/sb9pVJ8rAMNaDDxMNbrJLq 7A9UYabz9gp5Bxf0UMLT8V68SCXHyzSPrHcSF0oSIribYAB0+cBUDAiQbd/zZbkLMV e4b0BS1aTaXBW6Btj/GC0jAA1GyEnZjnsh7JRUx0P/fHoES7mXGxDDr//+af4jidkX Kl+H0fpajWl+w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4] usb: gadget: midi2: serialize the IN endpoint transmit path Reply-To: sashiko-reviews@lists.linux.dev To: "Jaegyun Lee" Cc: linux-usb@vger.kernel.org, linux-sound@vger.kernel.org In-Reply-To: <179638876078.1420263.2904571847750437390@gmail.com> References: <871p7yuv9b.wl-tiwai@suse.de> <179638876078.1420263.2904571847750437390@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 02:52:41 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: gadget: midi2: unbounded IRQ-off tx_lock hold and barrier overhead - [Medium] usb: gadget: midi2: f_midi2_start_eps() drops tx_pending after unlocking -- commit 8783d30e4bce8c594e55868e72c705df12dae801 Author: Jaegyun Lee usb: gadget: midi2: serialize the IN endpoint transmit path This commit adds a per-endpoint spinlock and pending flag to serialize the MIDI 1.0 and UMP IN transmit paths against concurrent completions and endpoint request allocation and teardown. > diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/f= unction/f_midi2.c > index 75036d4d60b35..0e24fb834f221 100644 > --- a/drivers/usb/gadget/function/f_midi2.c > +++ b/drivers/usb/gadget/function/f_midi2.c [ ... ] > @@ -1052,6 +1056,48 @@ static void process_midi1_transmit(struct f_midi2 = *midi2) > } > } > =20 > +static void f_midi2_tx_do_once(struct f_midi2_usb_ep *usb_ep) > +{ > + clear_bit(0, &usb_ep->tx_pending); > + /* order the clear before the request scan below */ > + smp_mb__after_atomic(); > + if (usb_ep->ep) > + process_ump_transmit(usb_ep->ep); > + else > + process_midi1_transmit(usb_ep->card); > +} > + > +static void f_midi2_tx_run_blocking(struct f_midi2_usb_ep *usb_ep) > +{ > + if (!usb_ep->usb_ep) > + return; > + > + do { > + scoped_guard(spinlock_irqsave, &usb_ep->tx_lock) > + f_midi2_tx_do_once(usb_ep); > + /* pairs with smp_mb__after_atomic() in f_midi2_tx_request() */ > + smp_mb(); > + } while (test_bit(0, &usb_ep->tx_pending)); > +} > + > +/* > + * The UDC may complete an IN request inside usb_ep_queue(), i.e. while > + * tx_lock is held by the transmitter. Such a completion only sets > + * tx_pending, and the lock holder transmits again after unlocking. > + */ > +static void f_midi2_tx_request(struct f_midi2_usb_ep *usb_ep) > +{ > + set_bit(0, &usb_ep->tx_pending); > + /* pairs with smp_mb() in f_midi2_tx_run_blocking() */ > + smp_mb__after_atomic(); > + > + scoped_cond_guard(spinlock_irqsave_try, return, &usb_ep->tx_lock) > + f_midi2_tx_do_once(usb_ep); > + smp_mb(); > + if (test_bit(0, &usb_ep->tx_pending)) > + f_midi2_tx_run_blocking(usb_ep); > +} > + > /* complete handler for MIDI1 EP-in requests */ > static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep, > struct usb_request *req) [Severity: High] Can holding tx_lock with IRQs disabled across f_midi2_tx_do_once() spin indefinitely with local interrupts off? In altset 0, process_midi1_transmit() loops indefinitely calling snd_ump_transmit() four bytes at a time. If userspace continuously writes non-convertible UMP packets (such as Utility or Stream messages) or packets for an unmapped group, the loop skips them without queueing a USB request. A concurrent writer in snd_rawmidi_kernel_write1() can refill the ring buff= er faster than this 4-byte loop drains it, keeping tx_lock held with IRQs off indefinitely and triggering RCU stalls, hard lockups, or spins in f_midi2_set_alt() -> f_midi2_stop_eps(). Even during normal streaming, f_midi2_ep_in_complete() and f_midi2_midi1_ep_in_complete() call put_empty_request() without taking tx_lock, so cross-CPU completions can continuously replenish free_reqs and keep both transmit loops running with IRQs disabled. Separately, every uncontended IN completion in f_midi2_tx_request() now pays two atomic bitops, three full memory barriers, and an IRQ-off spinlock cycle per packet. In addition, f_midi2_stop_eps() holds ep_in->tx_lock with IRQs disabled acr= oss the unrelated f_midi2_drop_reqs(ep_out) calls, and f_midi2_start_eps() holds it across up to 32 alloc_ep_req() allocations. [ ... ] > @@ -1284,7 +1332,8 @@ static int f_midi2_start_eps(struct f_midi2_usb_ep = *ep_in, > if (err) > return err; > =20 > - err =3D f_midi2_alloc_ep_reqs(ep_in); > + scoped_guard(spinlock_irqsave, &ep_in->tx_lock) > + err =3D f_midi2_alloc_ep_reqs(ep_in); > if (err) > return err; > err =3D f_midi2_alloc_ep_reqs(ep_out); [Severity: Medium] Can f_midi2_start_eps() lose a pending transmit wakeup after releasing ep_in->tx_lock? f_midi2_tx_request() returns immediately when trylock fails, relying on the current tx_lock holder to observe and drain tx_pending after unlocking. He= re f_midi2_set_alt() sets operation_mode and f_midi2_start_eps() enables ep_in before taking tx_lock, and does not check tx_pending after releasing it: CPU0 CPU1 f_midi2_set_alt() f_midi2_start_eps() f_midi2_start_ep(ep_in, fn) lock(&ep_in->tx_lock) f_midi2_alloc_ep_reqs(ep_in) f_midi2_ep_in_complete() f_midi2_tx_request(ep_in) set_bit(0, &ep_in->tx_pending) trylock(&ep_in->tx_lock) -> fails return unlock(&ep_in->tx_lock) <-- tx_pending never drained If an IN completion runs on another CPU during f_midi2_alloc_ep_reqs(ep_in), remaining buffered ALSA MIDI output stalls until the next userspace write triggers f_midi2_ump_trigger(). Checking tx_pending after unlocking (or calling f_midi2_tx_run_blocking() when set) would close this window. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/179638876078.142026= 3.2904571847750437390@gmail.com?part=3D1