๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
18 messages
18 total messages Started by Jakub Kicinski Thu, 08 Oct 2026 03:00
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#443
Author: Jakub Kicinski
Date: Thu, 08 Oct 2026 03:00
30 lines
6725 bytes
On Mon,  5 Oct 2026 00:59:25 +0300 ร–mer Mete Kaya wrote:
> tx_complete() calls this_cpu_ptr() before disabling preemption, which
> triggers a BUG when running with CONFIG_DEBUG_PREEMPT:
> 
>   BUG: using smp_processor_id() in preemptible code in tx_complete
> 
> Fix by using get_cpu_ptr()/put_cpu_ptr() which disable preemption and
> return the per-CPU pointer atomically. The usbnet_skb_return() hunk
> is a hardening change: that path runs in softirq context so no warning
> fires there, but the same fix is applied for consistency.

This looks odd, how did we miss this for 8 years.

Greg is probably busy, but would be good to get a confirmation
from either him or some other USB expert that the callbacks
can indeed be called in process context.

FWIW stack trace from syzbot

 <TASK>
 check_preemption_disabled+0xd8/0xe0 lib/smp_processor_id.c:47
 tx_complete+0x237/0x770 drivers/net/usb/usbnet.c:1301
 __usb_hcd_giveback_urb+0x38d/0x610 drivers/usb/core/hcd.c:1657
 usb_hcd_giveback_urb+0x3ca/0x4a0 drivers/usb/core/hcd.c:1741
 vhci_recv_ret_submit drivers/usb/usbip/vhci_rx.c:107 [inline]
 vhci_rx_pdu drivers/usb/usbip/vhci_rx.c:242 [inline]
 vhci_rx_loop+0x60e/0xa60 drivers/usb/usbip/vhci_rx.c:265
 kthread+0x370/0x450 kernel/kthread.c:436
 ret_from_fork+0x72b/0xd50 arch/x86/kernel/process.c:158
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#697
Author: Alan Stern
Date: Thu, 08 Oct 2026 14:16
23 lines
9390 bytes
On Wed, Oct 07, 2026 at 08:00:49PM -0700, Jakub Kicinski wrote:
> On Mon,  5 Oct 2026 00:59:25 +0300 ร–mer Mete Kaya wrote:
> > tx_complete() calls this_cpu_ptr() before disabling preemption, which
> > triggers a BUG when running with CONFIG_DEBUG_PREEMPT:
> > 
> >   BUG: using smp_processor_id() in preemptible code in tx_complete
> > 
> > Fix by using get_cpu_ptr()/put_cpu_ptr() which disable preemption and
> > return the per-CPU pointer atomically. The usbnet_skb_return() hunk
> > is a hardening change: that path runs in softirq context so no warning
> > fires there, but the same fix is applied for consistency.
> 
> This looks odd, how did we miss this for 8 years.
> 
> Greg is probably busy, but would be good to get a confirmation
> from either him or some other USB expert that the callbacks
> can indeed be called in process context.

They can be called in BH context with interrupts enabled.  Is that close 
enough?

Alan Stern
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#788
Author: Jakub Kicinski
Date: Thu, 08 Oct 2026 16:43
17 lines
6388 bytes
On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
> > This looks odd, how did we miss this for 8 years.
> > 
> > Greg is probably busy, but would be good to get a confirmation
> > from either him or some other USB expert that the callbacks
> > can indeed be called in process context.  
> 
> They can be called in BH context with interrupts enabled.  Is that close 
> enough?

BH should be fine on !RT. The patch, AFAIU, is because vhci calls
the completion callbacks in pure, unadulterated process context.
I'm trying to figure out how urgent the fix is, basically.
If it's a vhci bug then the usbnet fix is at most an RT problem.
Also if vhci is doing something wrong we don't want a truckload
of slop patches sent our way to fix 300 drivers :S
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#835
Author: Alan Stern
Date: Thu, 08 Oct 2026 18:03
28 lines
9771 bytes
On Thu, Oct 08, 2026 at 09:43:38AM -0700, Jakub Kicinski wrote:
> On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
> > > This looks odd, how did we miss this for 8 years.
> > > 
> > > Greg is probably busy, but would be good to get a confirmation
> > > from either him or some other USB expert that the callbacks
> > > can indeed be called in process context.  
> > 
> > They can be called in BH context with interrupts enabled.  Is that close 
> > enough?
> 
> BH should be fine on !RT. The patch, AFAIU, is because vhci calls
> the completion callbacks in pure, unadulterated process context.
> I'm trying to figure out how urgent the fix is, basically.
> If it's a vhci bug then the usbnet fix is at most an RT problem.
> Also if vhci is doing something wrong we don't want a truckload
> of slop patches sent our way to fix 300 drivers :S

As far as I am aware, there aren't really any guarantees on the 
context of a USB URB-completion callback.  The kerneldoc for struct urb 
in include/linux/usb.h says "The completion callback is made 
in_interrupt()", but that is most definitely out of date.

Drivers shouldn't rely on any particular context guarantees.  Not even 
whether local irqs are enabled/disabled.

Alan Stern
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#1192
Author: Greg Kroah-Hartman
Date: Fri, 09 Oct 2026 11:31
39 lines
7306 bytes
On Thu, Oct 08, 2026 at 02:03:34PM -0400, Alan Stern wrote:
> On Thu, Oct 08, 2026 at 09:43:38AM -0700, Jakub Kicinski wrote:
> > On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
> > > > This looks odd, how did we miss this for 8 years.
> > > > 
> > > > Greg is probably busy, but would be good to get a confirmation
> > > > from either him or some other USB expert that the callbacks
> > > > can indeed be called in process context.  
> > > 
> > > They can be called in BH context with interrupts enabled.  Is that close 
> > > enough?
> > 
> > BH should be fine on !RT. The patch, AFAIU, is because vhci calls
> > the completion callbacks in pure, unadulterated process context.
> > I'm trying to figure out how urgent the fix is, basically.
> > If it's a vhci bug then the usbnet fix is at most an RT problem.
> > Also if vhci is doing something wrong we don't want a truckload
> > of slop patches sent our way to fix 300 drivers :S
> 
> As far as I am aware, there aren't really any guarantees on the 
> context of a USB URB-completion callback.  The kerneldoc for struct urb 
> in include/linux/usb.h says "The completion callback is made 
> in_interrupt()", but that is most definitely out of date.

I don't think so, I think some platforms still have those callbacks in
irq context, unless we changed to threaded irq handlers everywhere?  I
could have missed that, but we should still write the callbacks to
assume that and then we should be fine even if we aren't in irq context,
right?

> Drivers shouldn't rely on any particular context guarantees.  Not even 
> whether local irqs are enabled/disabled.

Agreed.

thanks,

greg k-h
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2506
Author: Sebastian Andrzej Siewior
Date: Mon, 12 Oct 2026 19:49
35 lines
8270 bytes
On 2026-10-09 13:31:17 [+0200], Greg Kroah-Hartman wrote:
> > As far as I am aware, there aren't really any guarantees on the 
> > context of a USB URB-completion callback.  The kerneldoc for struct urb 
> > in include/linux/usb.h says "The completion callback is made 
> > in_interrupt()", but that is most definitely out of date.
> 
> I don't think so, I think some platforms still have those callbacks in
> irq context, unless we changed to threaded irq handlers everywhere?  I
> could have missed that, but we should still write the callbacks to
> assume that and then we should be fine even if we aren't in irq context,
> right?

The HCD invokes the completion callback usually in IRQ context. I think
the VHCI is an exception here.
For the forced-threaded mode we disable BH before the IRQ handler is
invoked. I think even dummy_hcd invokes them from the timer if my memory
is right.

> > Drivers shouldn't rely on any particular context guarantees.  Not even 
> > whether local irqs are enabled/disabled.
> 
> Agreed.

I would suggest to make VHCI invoke the completion callback with BH
disabled. usbnet's tx_complete() does also defer_bh() which schedules a
BH worker via queue_work(). While this works without disabling BH it
leads to waking ksoftirqd and an additional context switch which we
could avoid by dealing with it directly.

> thanks,
> 
> greg k-h

Sebastian
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2508
Author: Alan Stern
Date: Mon, 12 Oct 2026 20:26
37 lines
10331 bytes
On Mon, Oct 12, 2026 at 09:49:02PM +0200, Sebastian Andrzej Siewior wrote:
> On 2026-10-09 13:31:17 [+0200], Greg Kroah-Hartman wrote:
> > > As far as I am aware, there aren't really any guarantees on the 
> > > context of a USB URB-completion callback.  The kerneldoc for struct urb 
> > > in include/linux/usb.h says "The completion callback is made 
> > > in_interrupt()", but that is most definitely out of date.
> > 
> > I don't think so, I think some platforms still have those callbacks in
> > irq context, unless we changed to threaded irq handlers everywhere?  I
> > could have missed that, but we should still write the callbacks to
> > assume that and then we should be fine even if we aren't in irq context,
> > right?
> 
> The HCD invokes the completion callback usually in IRQ context. I think
> the VHCI is an exception here.
> For the forced-threaded mode we disable BH before the IRQ handler is
> invoked. I think even dummy_hcd invokes them from the timer if my memory
> is right.
> 
> > > Drivers shouldn't rely on any particular context guarantees.  Not even 
> > > whether local irqs are enabled/disabled.
> > 
> > Agreed.
> 
> I would suggest to make VHCI invoke the completion callback with BH
> disabled. usbnet's tx_complete() does also defer_bh() which schedules a
> BH worker via queue_work(). While this works without disabling BH it
> leads to waking ksoftirqd and an additional context switch which we
> could avoid by dealing with it directly.

Today you fix VHCI, tomorrow some other virtual HCD will cause problems.

Should we document any requirements that all HCDs will have to follow 
and that all completion handlers can rely on?

Alan Stern
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2521
Author: Sebastian Andrzej Siewior
Date: Mon, 12 Oct 2026 21:25
63 lines
9983 bytes
On 2026-10-12 16:26:28 [-0400], Alan Stern wrote:
> On Mon, Oct 12, 2026 at 09:49:02PM +0200, Sebastian Andrzej Siewior wrote:
> > On 2026-10-09 13:31:17 [+0200], Greg Kroah-Hartman wrote:
> > > > As far as I am aware, there aren't really any guarantees on the 
> > > > context of a USB URB-completion callback.  The kerneldoc for struct urb 
> > > > in include/linux/usb.h says "The completion callback is made 
> > > > in_interrupt()", but that is most definitely out of date.
> > > 
> > > I don't think so, I think some platforms still have those callbacks in
> > > irq context, unless we changed to threaded irq handlers everywhere?  I
> > > could have missed that, but we should still write the callbacks to
> > > assume that and then we should be fine even if we aren't in irq context,
> > > right?
> > 
> > The HCD invokes the completion callback usually in IRQ context. I think
> > the VHCI is an exception here.
> > For the forced-threaded mode we disable BH before the IRQ handler is
> > invoked. I think even dummy_hcd invokes them from the timer if my memory
> > is right.
> > 
> > > > Drivers shouldn't rely on any particular context guarantees.  Not even 
> > > > whether local irqs are enabled/disabled.
> > > 
> > > Agreed.
> > 
> > I would suggest to make VHCI invoke the completion callback with BH
> > disabled. usbnet's tx_complete() does also defer_bh() which schedules a
> > BH worker via queue_work(). While this works without disabling BH it
> > leads to waking ksoftirqd and an additional context switch which we
> > could avoid by dealing with it directly.
> 
> Today you fix VHCI, tomorrow some other virtual HCD will cause problems.

We don't have other virtual HCDs, do we?
The other driver is drivers/net/mctp/mctp-usb.c.
mctp_usb_out_complete() -> โ€ฆ -> mctp_usblib_tx_stats_update() does
get_cpu_ptr() for stats. But mctp_usb_in_complete() has 
mctp_usblib_rx_complete() -> mctp_usblib_rx_stats_single_drop() and
mctp_usblib_rx() which do just this_cpu_ptr(). Plus the queue wakes &
netif_rx() which wake ksoftirqd.

That makes two drivers from a quick grep. But I guess it is just
networking for the per-CPU stats.

> Should we document any requirements that all HCDs will have to follow 
> and that all completion handlers can rely on?

It used to complete in IRQ context. Then you added the possibility to
complete BH for those HCDs marked as such (except for the RH). I think
it was always reasonable to assume that it is either IRQ-context
(force-threaded or not for !HCD_BH) or BH (for HCD_BH). It makes hardly
a difference, BH is always off and networking uses to rely on it.
So yes, documenting would make sense.

Do you want to document it yourself and have me do the VHCI bits?

> Alan Stern

Sebastian
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2582
Author: Alan Stern
Date: Tue, 13 Oct 2026 02:09
84 lines
12978 bytes
On Mon, Oct 12, 2026 at 11:25:27PM +0200, Sebastian Andrzej Siewior wrote:
> On 2026-10-12 16:26:28 [-0400], Alan Stern wrote:
> > On Mon, Oct 12, 2026 at 09:49:02PM +0200, Sebastian Andrzej Siewior wrote:
> > > On 2026-10-09 13:31:17 [+0200], Greg Kroah-Hartman wrote:
> > > > > As far as I am aware, there aren't really any guarantees on the 
> > > > > context of a USB URB-completion callback.  The kerneldoc for struct urb 
> > > > > in include/linux/usb.h says "The completion callback is made 
> > > > > in_interrupt()", but that is most definitely out of date.
> > > > 
> > > > I don't think so, I think some platforms still have those callbacks in
> > > > irq context, unless we changed to threaded irq handlers everywhere?  I
> > > > could have missed that, but we should still write the callbacks to
> > > > assume that and then we should be fine even if we aren't in irq context,
> > > > right?
> > > 
> > > The HCD invokes the completion callback usually in IRQ context. I think
> > > the VHCI is an exception here.
> > > For the forced-threaded mode we disable BH before the IRQ handler is
> > > invoked. I think even dummy_hcd invokes them from the timer if my memory
> > > is right.
> > > 
> > > > > Drivers shouldn't rely on any particular context guarantees.  Not even 
> > > > > whether local irqs are enabled/disabled.
> > > > 
> > > > Agreed.
> > > 
> > > I would suggest to make VHCI invoke the completion callback with BH
> > > disabled. usbnet's tx_complete() does also defer_bh() which schedules a
> > > BH worker via queue_work(). While this works without disabling BH it
> > > leads to waking ksoftirqd and an additional context switch which we
> > > could avoid by dealing with it directly.
> > 
> > Today you fix VHCI, tomorrow some other virtual HCD will cause problems.
> 
> We don't have other virtual HCDs, do we?

Well, we've got dummy-hcd.  And just recently there was an RFC post with 
a virtio USB HCD implementation.  It wouldn't be surprising to encounter 
other ones.

Also, don't forget, URBs can complete in several different ways.  
There's normal completion, explicit unlinking, unlinking because the 
device was unplugged, unlinking because the device or HC is going into 
suspend, and so on.  And there's a difference between URBs sent to 
root hubs as opposed to devices actually on the bus.  Not to mention 
that in some cases completions may occur synchronously with submission 
or unlinking, even though they usually are asynchronous.

> The other driver is drivers/net/mctp/mctp-usb.c.
> mctp_usb_out_complete() -> โ€ฆ -> mctp_usblib_tx_stats_update() does
> get_cpu_ptr() for stats. But mctp_usb_in_complete() has 
> mctp_usblib_rx_complete() -> mctp_usblib_rx_stats_single_drop() and
> mctp_usblib_rx() which do just this_cpu_ptr(). Plus the queue wakes &
> netif_rx() which wake ksoftirqd.
> 
> That makes two drivers from a quick grep. But I guess it is just
> networking for the per-CPU stats.
> 
> > Should we document any requirements that all HCDs will have to follow 
> > and that all completion handlers can rely on?
> 
> It used to complete in IRQ context. Then you added the possibility to
> complete BH for those HCDs marked as such (except for the RH). I think
> it was always reasonable to assume that it is either IRQ-context
> (force-threaded or not for !HCD_BH) or BH (for HCD_BH). It makes hardly
> a difference, BH is always off and networking uses to rely on it.
> So yes, documenting would make sense.
> 
> Do you want to document it yourself and have me do the VHCI bits?

At the moment, the only thing I think we can actually guarantee is that 
completion handlers will be called with local interrupts disabled.  No 
commitments about process context vs. interrupt context vs. BH context 
or anything else.

Of course, in special cases you might know more about how a particular 
HCD works.  But except for platform-specific settings, it's hard to be 
certain that your driver will always be used with that one particular 
HCD.

If this is okay with you, I can write a documentation patch.

Alan Stern
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2709
Author: Sebastian Andrzej Siewior
Date: Tue, 13 Oct 2026 09:20
23 lines
7923 bytes
On 2026-10-13 10:54:09 [+0200], Oliver Neukum wrote:
> On 12.10.26 22:26, Alan Stern wrote:
> > Today you fix VHCI, tomorrow some other virtual HCD will cause problems.
> > 
> > Should we document any requirements that all HCDs will have to follow
> > and that all completion handlers can rely on?
> 
> Numerous corner cases. For example I strongly suspect that unloading
> the driver for the HC will always complete URBs synchronously.
> Please do not do this.

Sorry, I have hard time to parse this. The expectation of the USB driver
(was) that the completion handler is invoked in interrupt context.
Therefore BH is disabled one way or another.
Are you saying we should not have this behaviour documented/ expected
and assume preemptible context because "rmmod hcd" will complete all
enqueued URBs in the rmmod context?

> 	Regards
> 		Oliver

Sebastian
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#2701
Author: Sebastian Andrzej Siewior
Date: Tue, 13 Oct 2026 09:22
14 lines
7491 bytes
On 2026-10-13 11:11:04 [+0200], Paolo Abeni wrote:
> > If this is okay with you, I can write a documentation patch.
> Please help me wrapping this up. I read the above as a more generic
> patch is under discussion, am I correct?

We are discussing if we fix the identified driver(s) or fix the
offending HCD and document the expected behaviour.

> Thanks,
> 
> Paolo

Sebastian
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#3351
Author: Sebastian Andrzej Siewior
Date: Wed, 14 Oct 2026 19:11
50 lines
9112 bytes
On 2026-10-12 22:09:25 [-0400], Alan Stern wrote:
> > > Today you fix VHCI, tomorrow some other virtual HCD will cause problems.
> > 
> > We don't have other virtual HCDs, do we?
> 
> Well, we've got dummy-hcd.  And just recently there was an RFC post with 
> a virtio USB HCD implementation.  It wouldn't be surprising to encounter 
> other ones.

dummy-hcd is timer driven (so BH). Without looking at virtio I would
expect it to be interrupt driven.

> Also, don't forget, URBs can complete in several different ways.  
> There's normal completion, explicit unlinking, unlinking because the 
> device was unplugged, unlinking because the device or HC is going into 
> suspend, and so on.  And there's a difference between URBs sent to 
> root hubs as opposed to devices actually on the bus.  Not to mention 
> that in some cases completions may occur synchronously with submission 
> or unlinking, even though they usually are asynchronous.

True.

> > Do you want to document it yourself and have me do the VHCI bits?
> 
> At the moment, the only thing I think we can actually guarantee is that 
> completion handlers will be called with local interrupts disabled.  No 
> commitments about process context vs. interrupt context vs. BH context 
> or anything else.

"local interrupts disabled" as is local_irq_disable()? If so, that is
not the case. If it completes in BH context (due to HCD_BH) then
interrupts remain enabled.

> Of course, in special cases you might know more about how a particular 
> HCD works.  But except for platform-specific settings, it's hard to be 
> certain that your driver will always be used with that one particular 
> HCD.
> 
> If this is okay with you, I can write a documentation patch.

Sure. The kernel doc for usb_hcd_giveback_urb() says BH or caller's
context. I think you need to clarify that part and once we have tha, we
can proceed with the drivers.

Thank you.

> Alan Stern

Sebastian
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#3352
Author: Alan Stern
Date: Wed, 14 Oct 2026 19:22
64 lines
11723 bytes
On Wed, Oct 14, 2026 at 09:11:41PM +0200, Sebastian Andrzej Siewior wrote:
> On 2026-10-12 22:09:25 [-0400], Alan Stern wrote:
> > > > Today you fix VHCI, tomorrow some other virtual HCD will cause problems.
> > > 
> > > We don't have other virtual HCDs, do we?
> > 
> > Well, we've got dummy-hcd.  And just recently there was an RFC post with 
> > a virtio USB HCD implementation.  It wouldn't be surprising to encounter 
> > other ones.
> 
> dummy-hcd is timer driven (so BH). Without looking at virtio I would
> expect it to be interrupt driven.
> 
> > Also, don't forget, URBs can complete in several different ways.  
> > There's normal completion, explicit unlinking, unlinking because the 
> > device was unplugged, unlinking because the device or HC is going into 
> > suspend, and so on.  And there's a difference between URBs sent to 
> > root hubs as opposed to devices actually on the bus.  Not to mention 
> > that in some cases completions may occur synchronously with submission 
> > or unlinking, even though they usually are asynchronous.
> 
> True.
> 
> > > Do you want to document it yourself and have me do the VHCI bits?
> > 
> > At the moment, the only thing I think we can actually guarantee is that 
> > completion handlers will be called with local interrupts disabled.  No 
> > commitments about process context vs. interrupt context vs. BH context 
> > or anything else.
> 
> "local interrupts disabled" as is local_irq_disable()? If so, that is
> not the case. If it completes in BH context (due to HCD_BH) then
> interrupts remain enabled.

Yes, but we can call local_irq_disable() before invoking the completion 
handler -- if we decide that would be a good thing to do.

Otherwise I would say that we don't guarantee anything.

> > Of course, in special cases you might know more about how a particular 
> > HCD works.  But except for platform-specific settings, it's hard to be 
> > certain that your driver will always be used with that one particular 
> > HCD.
> > 
> > If this is okay with you, I can write a documentation patch.
> 
> Sure. The kernel doc for usb_hcd_giveback_urb() says BH or caller's
> context. I think you need to clarify that part and once we have tha, we
> can proceed with the drivers.

What do you think?  Should local interrupts always be disabled, or 
should we offer no guarantee?

Completion handler routines will have to be conservative, so if we don't 
guarantee anything then they will need to disable interrupts themselves 
whenever they require it (for example, by using spin_lock_irqsave() 
instead of spin_lock()).  The core could save them this extra effort, at 
the cost of unnecessarily disabling interrupts sometimes.

USB isn't a tremendously fast bus, so the overhead probably doesn't 
matter much.  Which tradeoff do you prefer?

Alan Stern
Re: [PATCH net v6] usbnet: fix smp_processor_id() use in preemptible context
#3378
Author: Sebastian Andrzej Siewior
Date: Wed, 14 Oct 2026 22:04
60 lines
9582 bytes
On 2026-10-14 15:22:51 [-0400], Alan Stern wrote:
> > "local interrupts disabled" as is local_irq_disable()? If so, that is
> > not the case. If it completes in BH context (due to HCD_BH) then
> > interrupts remain enabled.
> 
> Yes, but we can call local_irq_disable() before invoking the completion 
> handler -- if we decide that would be a good thing to do.

Oh please don't. This will break PREEMPT_RT badly.

> Otherwise I would say that we don't guarantee anything.

Okay.

> > > Of course, in special cases you might know more about how a particular 
> > > HCD works.  But except for platform-specific settings, it's hard to be 
> > > certain that your driver will always be used with that one particular 
> > > HCD.
> > > 
> > > If this is okay with you, I can write a documentation patch.
> > 
> > Sure. The kernel doc for usb_hcd_giveback_urb() says BH or caller's
> > context. I think you need to clarify that part and once we have tha, we
> > can proceed with the drivers.
> 
> What do you think?  Should local interrupts always be disabled, or 
> should we offer no guarantee?

No guarantees in this regard, please. Disabling interrupts will break
PREEMPT_RT here.

> Completion handler routines will have to be conservative, so if we don't 
> guarantee anything then they will need to disable interrupts themselves 
> whenever they require it (for example, by using spin_lock_irqsave() 
> instead of spin_lock()).  The core could save them this extra effort, at 
> the cost of unnecessarily disabling interrupts sometimes.

The problem is spin_lock_irqsave() will not disable interrupts on
PREEMPT_RT while local_irq_save() will. For this reason I would prefer
to skip it.

> USB isn't a tremendously fast bus, so the overhead probably doesn't 
> matter much.  Which tradeoff do you prefer?

So if we don't guarantee that BH is disabled then I would say nothing is
guaranteed, anything is possible.

A little note: cdns and dwc3 do disable BH in their voluntary threaded
interrupt handler. The reason is that the networking driver expect this.
I know this is the gadget and not HCD side but sort of the same issue.
While BH is disabled for interrupt handler (and forced threaded handler)
it is not if requested explicitly.

This would mean that the completion handler of the networking driver
would have to do local_bh_disable() themself. I could live with this ;)

> Alan Stern

Sebastian
[PATCH] USB: Documentation: Update description of completion callback context
#3641
Author: Alan Stern
Date: Thu, 15 Oct 2026 16:00
54 lines
11329 bytes
The existing kerneldoc documentation for URB completion callbacks is
incorrect about the context in which the callbacks occur; it is not
always in_interrupt.  State explicitly that a callback may occur in
any context with no guarantees, and likewise for the status of
CPU-local interrupts.

Documentation update only; no code changes.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>

---

 drivers/usb/core/urb.c |    8 ++++++++
 include/linux/usb.h    |    7 ++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

Index: usb-devel/drivers/usb/core/urb.c
===================================================================
--- usb-devel.orig/drivers/usb/core/urb.c
+++ usb-devel/drivers/usb/core/urb.c
@@ -254,6 +254,14 @@ EXPORT_SYMBOL_GPL(usb_urb_ep_type_check)
  * request.  The completion handler may then immediately free or reuse that
  * URB.
  *
+ * The USB core makes no guarantees at all about the context in which
+ * the complete() callback is invoked.  It may be in process context,
+ * interrupt context, BH context, or anything else.  Also, local interrupts
+ * may be enabled or disabled.  To deal with this, the callback routine
+ * must always use the _irqsave and _irqrestore variants of things like
+ * spin_lock(), and it must always use GFP_ATOMIC for its memory
+ * allocation flags.
+ *
  * With few exceptions, USB device drivers should never access URB fields
  * provided by usbcore or the HCD until its complete() is called.
  * The exceptions relate to periodic transfer scheduling.  For both
Index: usb-devel/include/linux/usb.h
===================================================================
--- usb-devel.orig/include/linux/usb.h
+++ usb-devel/include/linux/usb.h
@@ -1605,7 +1605,12 @@ typedef void (*usb_complete_t)(struct ur
  *
  * Completion Callbacks:
  *
- * The completion callback is made in_interrupt(), and one of the first
+ * The completion callback can occur in any context: process, interrupt,
+ * BH, or anything else.  Also, interrupts may or may not be disabled on
+ * the local CPU when the callback runs.  A completion handler routine
+ * must not depend on any particular properties of its environment.
+ *
+ * One of the first
  * things that a completion handler should do is check the status field.
  * The status field is provided for all URBs.  It is used to report
  * unlinked URBs, and status for all non-ISO transfers.  It should not
Re: [PATCH] USB: Documentation: Update description of completion callback context
#3668
Author: Sebastian Andrzej Siewior
Date: Thu, 15 Oct 2026 18:26
24 lines
7996 bytes
On 2026-10-15 12:00:46 [-0400], Alan Stern wrote:
> The existing kerneldoc documentation for URB completion callbacks is
> incorrect about the context in which the callbacks occur; it is not
> always in_interrupt.  State explicitly that a callback may occur in
> any context with no guarantees, and likewise for the status of
> CPU-local interrupts.
> 
> Documentation update only; no code changes.
> 
> Signed-off-by: Alan Stern <stern@rowland.harvard.edu>

Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

usb_hcd_giveback_urb() kernel doc says: 

|  * Context: atomic. The completion callback is invoked either in a work queue
|  * (BH) context or in the caller's context, depending on whether the HCD_BH
|  * flag is set in the @hcd structure, except that URBs submitted to the
|  * root hub always complete in BH context.

Do you think this deserves an update?

Sebastian
Re: [PATCH] USB: Documentation: Update description of completion callback context
#3670
Author: Alan Stern
Date: Thu, 15 Oct 2026 18:35
29 lines
10089 bytes
On Thu, Oct 15, 2026 at 08:26:43PM +0200, Sebastian Andrzej Siewior wrote:
> On 2026-10-15 12:00:46 [-0400], Alan Stern wrote:
> > The existing kerneldoc documentation for URB completion callbacks is
> > incorrect about the context in which the callbacks occur; it is not
> > always in_interrupt.  State explicitly that a callback may occur in
> > any context with no guarantees, and likewise for the status of
> > CPU-local interrupts.
> > 
> > Documentation update only; no code changes.
> > 
> > Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> 
> Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> 
> usb_hcd_giveback_urb() kernel doc says: 
> 
> |  * Context: atomic. The completion callback is invoked either in a work queue
> |  * (BH) context or in the caller's context, depending on whether the HCD_BH
> |  * flag is set in the @hcd structure, except that URBs submitted to the
> |  * root hub always complete in BH context.
> 
> Do you think this deserves an update?

I think that's okay as is.  It describes the current implementation 
pretty well, even though it doesn't mention the lack of guarantees.  If 
you feel it should be changed, let me know.

Alan Stern
Re: [PATCH] USB: Documentation: Update description of completion callback context
#4116
Author: Sebastian Andrzej Siewior
Date: Fri, 16 Oct 2026 18:56
35 lines
8628 bytes
On 2026-10-15 14:35:56 [-0400], Alan Stern wrote:
> On Thu, Oct 15, 2026 at 08:26:43PM +0200, Sebastian Andrzej Siewior wrote:
> > On 2026-10-15 12:00:46 [-0400], Alan Stern wrote:
> > > The existing kerneldoc documentation for URB completion callbacks is
> > > incorrect about the context in which the callbacks occur; it is not
> > > always in_interrupt.  State explicitly that a callback may occur in
> > > any context with no guarantees, and likewise for the status of
> > > CPU-local interrupts.
> > > 
> > > Documentation update only; no code changes.
> > > 
> > > Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> > 
> > Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> > 
> > usb_hcd_giveback_urb() kernel doc says: 
> > 
> > |  * Context: atomic. The completion callback is invoked either in a work queue
> > |  * (BH) context or in the caller's context, depending on whether the HCD_BH
> > |  * flag is set in the @hcd structure, except that URBs submitted to the
> > |  * root hub always complete in BH context.
> > 
> > Do you think this deserves an update?
> 
> I think that's okay as is.  It describes the current implementation 
> pretty well, even though it doesn't mention the lack of guarantees.  If 
> you feel it should be changed, let me know.

It reads as it is always atomic. Maybe removing parts of the description
and adding a pointer to your initial documentation would be better.

> Alan Stern

Sebastian
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads