Re: [PATCH RESEND] usb: mtu3: keep interrupts disabled in gadget completion callbacks
From: Alan Stern
Date: Wed Dec 02 2026 - 13:49:36 EST
On Tue, Dec 01, 2026 at 10:44:31PM -0600, Ryan Brue wrote:
> 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
I'm confused. At the start of __local_bh_enable_ip() in 7.4-rc3 I see:
WARN_ON_ONCE(in_hardirq());
and preempt.h defines in_hardirq() as:
#define in_hardirq() (hardirq_count())
So the warning shouldn't fire, since hardirq_count() is zero. Am I
looking at the wrong warning?
(Logically the behavior you describe is inconsistent. There's nothing
wrong with disabling interrupts while in process context, and there's
nothing wrong with disabling BHs while in process context with
interrupts disabled, but then enabling BHs again provokes a warning?
Even though it's merely restoring the state we entered earlier with no
indication of a problem?)
Anyway, I see in the kerneldoc for netif_rx() that callers in process
context must not disable interrupts. Since u_ether's rx_complete() will
run with interrupts disabled, we need to leave process context somewhere
before netif_rx() is called.
> 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.
This seems like a huge oversight. Or is it perhaps an unstated
assumption in the kernel: Disabling interrupts implies that BHs are also
disabled? If that's so then mtu3_irq() shouldn't have to do anything
special -- when it disables interrupts, BHs should be disabled right
along with them. Another symptom of an inconsistency?
Would holding an otherwise unused spinlock around the netif_rx() call in
rx_complete() avoid the __local_bh_enable_ip() warning?
> 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 :|).
Yes, I agree some fix is needed. It seems very odd that _all_ gadget
completion handlers should be forced to run with BHs disabled merely
because one particular gadget has this requirement. There ought to be a
way to do what we want without affecting other gadgets. (The same goes
for dwc3.)
Alan Stern