* [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks
@ 2026-11-30 16:48 Ryan Brue
2026-11-30 16:55 ` sashiko-bot
2026-11-30 18:18 ` Alan Stern
0 siblings, 2 replies; 4+ messages in thread
From: Ryan Brue @ 2026-11-30 16:48 UTC (permalink / raw)
To: Chunfeng Yun, Greg Kroah-Hartman, Ryan Brue
Cc: linux-usb, linux-arm-kernel, linux-mediatek, linux-kernel, stable
Commit 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks
with interrupts disabled") made the threaded mtu3_irq() take mtu->lock
with spin_lock_bh(). mtu3_req_complete() drops the lock with a plain
spin_unlock() around usb_gadget_giveback_request(), so the gadget
function's completion callback now runs with interrupts enabled.
That breaks the gadget API, struct usb_request's @complete "will always
be called with interrupts disabled". u_serial's console relies on it:
gs_console_complete_out() takes cons->lock with spin_lock(), and
gs_console_write() takes the same lock with spin_lock_irqsave() from
printk, which can then interrupt the callback on the same CPU while it
holds the lock.
Keep spin_lock_irqsave() in mtu3_irq() and disable bottom halves around
it instead, as dwc3 does in commit 84918a89d6ef ("usb: dwc3: gadget: Let
the interrupt handler disable bottom halves."). netif_rx() then finds
softirqs disabled and leaves them alone, the completion callbacks run
with interrupts disabled again, and the softirq netif_rx() raises runs
at local_bh_enable(), once the lock is dropped and interrupts are on.
Tested on an MT8173 tablet with PROVE_LOCKING, copying a 21 MB file over
USB ethernet three times per arm: with spin_lock_bh(), all 8,333 completion
callbacks ran with interrupts enabled; with this change, none of about
16,000 did, with no lockdep report and every copy intact.
Fixes: 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks with interrupts disabled")
Cc: stable@kernel.org
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
Greg, this is a resend of the patch I posted on 2026-11-22 [1], unchanged
apart from the base, which is now v7.4-rc5.
It fixes 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks
with interrupts disabled"), which is in v7.4-rc5 and tagged for stable.
That commit is v1 of my mtu3 callbacks fix, the one I asked you on
2026-11-03 not to apply: with spin_lock_bh() the gadget completion
callbacks run with interrupts enabled, which the gadget API rules out.
v2 [2] got no reply, so this is v2's change as a fix on top of
625dd9076e35; the resulting mtu3_irq() is the same as v2's.
Could it go in for 7.4, so that stable picks up both together?
My bad for the original error in v1.
[1] https://lore.kernel.org/all/20261122-usb-mtu3-callbacks-irqs-off-v1-1-27733bb678c4@gmail.com/
[2] https://lore.kernel.org/all/20261104-usb-mtu3-gadget-completion-callback-fix-v2-1-2769533a39cb@gmail.com/
---
drivers/usb/mtu3/mtu3_core.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/mtu3/mtu3_core.c b/drivers/usb/mtu3/mtu3_core.c
index fc274646fcde..f6677f58dd1f 100644
--- a/drivers/usb/mtu3/mtu3_core.c
+++ b/drivers/usb/mtu3/mtu3_core.c
@@ -790,9 +790,11 @@ static irqreturn_t mtu3_u2_common_isr(struct mtu3 *mtu)
static irqreturn_t mtu3_irq(int irq, void *data)
{
struct mtu3 *mtu = (struct mtu3 *)data;
+ unsigned long flags;
u32 level1;
- spin_lock_bh(&mtu->lock);
+ local_bh_disable();
+ spin_lock_irqsave(&mtu->lock, flags);
/* U3D_LV1ISR is RU */
level1 = mtu3_readl(mtu->mac_base, U3D_LV1ISR);
@@ -813,7 +815,8 @@ static irqreturn_t mtu3_irq(int irq, void *data)
if (level1 & QMU_INTR)
mtu3_qmu_isr(mtu);
- spin_unlock_bh(&mtu->lock);
+ spin_unlock_irqrestore(&mtu->lock, flags);
+ local_bh_enable();
return IRQ_HANDLED;
}
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261122-usb-mtu3-callbacks-irqs-off-71bfbb2ed4fc
Best regards,
--
Ryan Brue <ryanbrue.dev@gmail.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks
2026-11-30 16:48 [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks Ryan Brue
@ 2026-11-30 16:55 ` sashiko-bot
2026-11-30 18:18 ` Alan Stern
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-30 16:55 UTC (permalink / raw)
To: Ryan Brue; +Cc: linux-usb
> Commit 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks
> with interrupts disabled") made the threaded mtu3_irq() take mtu->lock
> with spin_lock_bh(). mtu3_req_complete() drops the lock with a plain
> spin_unlock() around usb_gadget_giveback_request(), so the gadget
> function's completion callback now runs with interrupts enabled.
>
> That breaks the gadget API, struct usb_request's @complete "will always
> be called with interrupts disabled". u_serial's console relies on it:
> gs_console_complete_out() takes cons->lock with spin_lock(), and
> gs_console_write() takes the same lock with spin_lock_irqsave() from
> printk, which can then interrupt the callback on the same CPU while it
> holds the lock.
>
> Keep spin_lock_irqsave() in mtu3_irq() and disable bottom halves around
> it instead, as dwc3 does in commit 84918a89d6ef ("usb: dwc3: gadget: Let
> the interrupt handler disable bottom halves."). netif_rx() then finds
> softirqs disabled and leaves them alone, the completion callbacks run
> with interrupts disabled again, and the softirq netif_rx() raises runs
> at local_bh_enable(), once the lock is dropped and interrupts are on.
>
> Tested on an MT8173 tablet with PROVE_LOCKING, copying a 21 MB file over
> USB ethernet three times per arm: with spin_lock_bh(), all 8,333 completion
> callbacks ran with interrupts enabled; with this change, none of about
> 16,000 did, with no lockdep report and every copy intact.
>
> Fixes: 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks with interrupts disabled")
> Cc: stable@kernel.org
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-usb-mtu3-callbacks-irqs-off-v1-1-b3180defb60c@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks
2026-11-30 16:48 [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks Ryan Brue
2026-11-30 16:55 ` sashiko-bot
@ 2026-11-30 18:18 ` Alan Stern
2026-12-02 4:44 ` Ryan Brue
1 sibling, 1 reply; 4+ messages in thread
From: Alan Stern @ 2026-11-30 18:18 UTC (permalink / raw)
To: Ryan Brue
Cc: Chunfeng Yun, Greg Kroah-Hartman, linux-usb, linux-arm-kernel,
linux-mediatek, linux-kernel, stable
On Mon, Nov 30, 2026 at 10:48:56AM -0600, Ryan Brue wrote:
> Commit 625dd9076e35 ("usb: mtu3: don't run gadget completion callbacks
> with interrupts disabled") made the threaded mtu3_irq() take mtu->lock
> with spin_lock_bh(). mtu3_req_complete() drops the lock with a plain
> spin_unlock() around usb_gadget_giveback_request(), so the gadget
> function's completion callback now runs with interrupts enabled.
>
> That breaks the gadget API, struct usb_request's @complete "will always
> be called with interrupts disabled". u_serial's console relies on it:
> gs_console_complete_out() takes cons->lock with spin_lock(), and
> gs_console_write() takes the same lock with spin_lock_irqsave() from
> printk, which can then interrupt the callback on the same CPU while it
> holds the lock.
>
> Keep spin_lock_irqsave() in mtu3_irq() and disable bottom halves around
> it instead, as dwc3 does in commit 84918a89d6ef ("usb: dwc3: gadget: Let
> the interrupt handler disable bottom halves."). netif_rx() then finds
> softirqs disabled and leaves them alone, the completion callbacks run
> with interrupts disabled again, and the softirq netif_rx() raises runs
> at local_bh_enable(), once the lock is dropped and interrupts are on.
This seems like a strange approach.
Yes, interrupts certainly should be disabled when the request's
completion routine is called. But why explicitly disable bottom halves?
And besides, doesn't spin_lock_irqsave() disable bottom halves already?
Also, why should a networking gadget depend on some particular BH
setting when the completion routine runs? Different UDC drivers will
behave in different ways, and the gadget driver has to work with all of
them. The only real guarantee is the one stated in the API, namely,
that interrupts will be disabled. If the gadget wants more, it has to
take the additional actions itself. The UDC driver isn't responsible.
Alan Stern
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks
2026-11-30 18:18 ` Alan Stern
@ 2026-12-02 4:44 ` Ryan Brue
0 siblings, 0 replies; 4+ messages in thread
From: Ryan Brue @ 2026-12-02 4:44 UTC (permalink / raw)
To: Alan Stern
Cc: Chunfeng Yun, Greg Kroah-Hartman, linux-usb, linux-arm-kernel,
linux-mediatek, linux-kernel, stable
Hi Alan, thanks for the look.
On 11/30/26 12:18 PM, Alan Stern wrote:
> This seems like a strange approach.
>
> Yes, interrupts certainly should be disabled when the request's
> completion routine is called. But why explicitly disable bottom halves?
> And besides, doesn't spin_lock_irqsave() disable bottom halves already?
Not in the sense that matters here. spin_lock_irqsave() turns interrupts
off, but it leaves softirq_count() at zero, and mtu3_irq() is a threaded
handler (no primary handler) since 13118959cb1a. So the completion runs
in task context with interrupts off and neither hardirq_count() nor
softirq_count() set. netif_rx() checks exactly those two counts, and
when both are zero it does its own local_bh_disable()/local_bh_enable().
local_bh_enable() with interrupts disabled is the WARN that 625dd9076e35
intended to fix:
WARNING: kernel/softirq.c:430 at __local_bh_enable_ip+0x17c/0x188
netif_rx
rx_complete [u_ether]
usb_gadget_giveback_request
mtu3_req_complete
...
mtu3_irq
irq_thread_fn
Disabling bottom halves explicitly, outside the irqsave, means
netif_rx() sees softirq_count() set and leaves BH alone. The NET_RX
softirq it raises then runs at our local_bh_enable(), after interrupts
are back on.
> Also, why should a networking gadget depend on some particular BH
> setting when the completion routine runs? Different UDC drivers will
> behave in different ways, and the gadget driver has to work with all of
> them. The only real guarantee is the one stated in the API, namely,
> that interrupts will be disabled. If the gadget wants more, it has to
> take the additional actions itself. The UDC driver isn't responsible.
>
> Alan Stern
I agree that the API only promises interrupts disabled, but I don't
think u_ether can take those actions itself. Called with interrupts off
from task context, it can't call netif_rx(), and it can't wrap the call
in its own local_bh_disable()/local_bh_enable() either, because that
hits the same assertion. It would have to defer the receive path to a
work item or similar.
The callbacks used to run in hardirq context, where all of this is fine.
They stopped doing so when mtu3 moved to a threaded handler. For
force-threaded handlers the IRQ core already brackets the handler with
local_bh_disable()/local_bh_enable() (irq_forced_thread_fn()) for this
reason. mtu3 threads its handler voluntarily, so it doesn't get that.
dwc3 had the same problem and fixed it the same way in the controller,
in 84918a89d6ef ("usb: dwc3: gadget: Let the interrupt handler disable
bottom halves."), which this follows.
Also, the BH part is already in mainline: 625dd9076e35 (in v7.4-rc5)
switched mtu3_irq() to spin_lock_bh(). That made the callbacks run with
interrupts enabled, which breaks the guarantee you quote: u_serial's
console callback takes a lock that printk takes with spin_lock_irqsave()
on the same CPU. This patch only restores interrupts-off and keeps the
BH disabling.
If you'd rather see the gadget side changed instead, I can look at that,
but mtu3 would still need this fix, since 625dd9076e35 breaks the
interrupts-disabled guarantee on its own (my bad :|).
Best regards,
Ryan Brue
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 4:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:48 [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks Ryan Brue
2026-11-30 16:55 ` sashiko-bot
2026-11-30 18:18 ` Alan Stern
2026-12-02 4:44 ` Ryan Brue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox