usbnet: fix smp_processor_id() use in preemptible context
Subsystems (5)
PATCH v6 1/1 usbnet: fix smp_processor_id() use in preemptible context
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.
---
v6: Fix commit message and Fixes tag as pointed out by Sashiko:
- BUG is in tx_complete(), not usbnet_skb_return()
- usbnet_skb_return() hunk is hardening, not a fix
- CONFIG_DEBUG_PREEMPT, not PREEMPT_FULL
- Fixes tag updated to ed194d136769
--- drivers/net/usb/usbnet.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/usb/usbnet.c +6 -2
@@ -325,7 +325,7 @@ static void __usbnet_status_stop_force(struct usbnet *dev)*/ void usbnet_skb_return (struct usbnet *dev,struct sk_buff *skb) { -struct pcpu_sw_netstats *stats64 =this_cpu_ptr (dev->net->tstats); +struct pcpu_sw_netstats *stats64;unsigned long flags;int status;
@@ -338,10 +338,12 @@ void usbnet_skb_return(struct usbnet *dev, struct sk_buff *skb)if (skb->protocol ==0 ) skb->protocol =eth_type_trans (skb, dev->net); + stats64 =get_cpu_ptr (dev->net->tstats); flags =u64_stats_update_begin_irqsave (&stats64->syncp);u64_stats_inc (&stats64->rx_packets);u64_stats_add (&stats64->rx_bytes, skb->len);u64_stats_update_end_irqrestore (&stats64->syncp, flags); +put_cpu_ptr (dev->net->tstats);netif_dbg (dev, rx_status, dev->net,"< rx, len %zu, type 0x%x\n" , skb->len +sizeof (struct ethhdr ), skb->protocol);
@@ -1298,13 +1300,15 @@ static void tx_complete(struct urb *urb)struct usbnet *dev = entry->dev;if (urb->status ==0 ) { -struct pcpu_sw_netstats *stats64 =this_cpu_ptr (dev->net->tstats); +struct pcpu_sw_netstats *stats64;unsigned long flags; + stats64 =get_cpu_ptr (dev->net->tstats); flags =u64_stats_update_begin_irqsave (&stats64->syncp);u64_stats_add (&stats64->tx_packets, entry->packets);u64_stats_add (&stats64->tx_bytes, entry->length);u64_stats_update_end_irqrestore (&stats64->syncp, flags); +put_cpu_ptr (dev->net->tstats); }else { dev->net->stats.tx_errors++;
--
2.55.0
Jakub Kicinski
maintainerQuoted 9 lines · On Mon, 5 Oct 2026 00:59:25 +0300 Ömer Mete Kaya wrote:
tx_complete()callsthis_cpu_ptr()before disabling preemption, which triggers a BUG when running withCONFIG_DEBUG_PREEMPT:BUG: using smp_processor_id() in preemptible code in tx_completeFix 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
Quoted 11 lines · Jakub Kicinski
Quoted 9 lines · On Mon, 5 Oct 2026 00:59:25 +0300 Ömer Mete Kaya wrote:
tx_complete()callsthis_cpu_ptr()before disabling preemption, which triggers a BUG when running withCONFIG_DEBUG_PREEMPT:BUG: using smp_processor_id() in preemptible code in tx_completeFix 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.
Quoted 5 lines
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
Jakub Kicinski
maintainerQuoted 8 lines · On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
Quoted 5 lines
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
Quoted 10 lines · Jakub Kicinski
Quoted 8 lines · On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
Quoted 5 lines
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?
Quoted 6 lines
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
Greg Kroah-Hartman
maintainerQuoted 18 lines · Alan Stern
Quoted 10 lines · Jakub Kicinski
Quoted 8 lines · On Thu, 8 Oct 2026 10:16:02 -0400 Alan Stern wrote:
Quoted 5 lines
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?
Quoted 6 lines
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
Quoted 4 lines
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.hsays "The completion callback is madein_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?
Quoted 2 lines
Drivers shouldn't rely on any particular context guarantees. Not even whether local irqs are enabled/disabled.
Agreed.
thanks,
greg k-h
Quoted 10 lines · Greg Kroah-Hartman
Quoted 4 lines
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.hsays "The completion callback is madein_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.
Quoted 4 lines
Quoted 2 lines
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.
Quoted 3 lines
thanks,
greg k-h
Sebastian
Quoted 21 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Greg Kroah-Hartman
Quoted 4 lines
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.hsays "The completion callback is madein_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 evendummy_hcdinvokes them from the timer if my memory is right.Quoted 3 lines
Quoted 2 lines
Drivers shouldn't rely on any particular context guarantees. Not even whether local irqs are enabled/disabled.
Quoted 7 lines
Quoted 1 line
Agreed.
I would suggest to make VHCI invoke the completion callback with BH disabled. usbnet's
tx_complete()does alsodefer_bh()which schedules a BH worker viaqueue_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
Quoted 24 lines · Alan Stern
Quoted 23 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Greg Kroah-Hartman
Quoted 4 lines
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.hsays "The completion callback is madein_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 evendummy_hcdinvokes them from the timer if my memory is right.Quoted 4 lines
Quoted 2 lines
Drivers shouldn't rely on any particular context guarantees. Not even whether local irqs are enabled/disabled.
Agreed.
Quoted 7 lines
Quoted 5 lines
I would suggest to make VHCI invoke the completion callback with BH disabled. usbnet's
tx_complete()does alsodefer_bh()which schedules a BH worker viaqueue_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.
Quoted 2 lines
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?
Quoted 1 line
Alan Stern
Sebastian
Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 30 lines · Alan Stern
Quoted 28 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Greg Kroah-Hartman
Quoted 4 lines
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.hsays "The completion callback is madein_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 evendummy_hcdinvokes them from the timer if my memory is right.Quoted 4 lines
Quoted 2 lines
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 alsodefer_bh()which schedules a BH worker viaqueue_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.
Quoted 3 lines
Quoted 1 line
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.
Quoted 13 lines
The other driver is
drivers/net/mctp/mctp-usb.c.mctp_usb_out_complete()-> … ->mctp_usblib_tx_stats_update()doesget_cpu_ptr()for stats. Butmctp_usb_in_complete()hasmctp_usblib_rx_complete()->mctp_usblib_rx_stats_single_drop()andmctp_usblib_rx()which do justthis_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.
Quoted 2 lines
Should we document any requirements that all HCDs will have to follow and that all completion handlers can rely on?
Quoted 8 lines
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
Paolo Abeni
maintainerQuoted 75 lines · Alan Stern
Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 30 lines · Alan Stern
Quoted 28 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Greg Kroah-Hartman
Quoted 4 lines
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.hsays "The completion callback is madein_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 evendummy_hcdinvokes them from the timer if my memory is right.Quoted 4 lines
Quoted 2 lines
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 alsodefer_bh()which schedules a BH worker viaqueue_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.Quoted 3 lines
Quoted 1 line
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.
Quoted 13 lines
The other driver is
drivers/net/mctp/mctp-usb.c.mctp_usb_out_complete()-> … ->mctp_usblib_tx_stats_update()doesget_cpu_ptr()for stats. Butmctp_usb_in_complete()hasmctp_usblib_rx_complete()->mctp_usblib_rx_stats_single_drop()andmctp_usblib_rx()which do justthis_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.
Quoted 2 lines
Should we document any requirements that all HCDs will have to follow and that all completion handlers can rely on?
Quoted 8 lines
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.
Quoted 6 lines
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.
Please help me wrapping this up. I read the above as a more generic patch is under discussion, am I correct?
Thanks,
Paolo
Quoted 3 lines · Paolo Abeni
Quoted 1 line
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.
Quoted 3 lines
Thanks,
Paolo
Sebastian
Quoted 7 lines · Alan Stern
Quoted 3 lines
Quoted 1 line
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.
Quoted 7 lines
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.
Quoted 6 lines
Quoted 1 line
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.
Quoted 6 lines
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.
Quoted 1 line
Alan Stern
Sebastian
Quoted 24 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Alan Stern
Quoted 3 lines
Quoted 1 line
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.
Quoted 7 lines
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.
Quoted 2 lines
Quoted 1 line
Do you want to document it yourself and have me do the VHCI bits?
Quoted 8 lines
Quoted 4 lines
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 toHCD_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.
Quoted 10 lines
Quoted 6 lines
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
Quoted 6 lines · Alan Stern
Quoted 3 lines
"local interrupts disabled" as is
local_irq_disable()? If so, that is not the case. If it completes in BH context (due toHCD_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.
Quoted 1 line
Otherwise I would say that we don't guarantee anything.
Okay.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
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.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
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.
Quoted 5 lines
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 ofspin_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.
Quoted 2 lines
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 ;)
Quoted 1 line
Alan Stern
Sebastian
PATCH 1/1 USB: Documentation: Update description of completion callback context
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.
---
drivers/usb/core/urb.c | 8 ++++++++ include/linux/usb.h | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/usb/core/urb.c +8 -0
@@ -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
include/linux/usb.h +6 -1
@@ -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
[email protected]
AI reviewRe: [PATCH] USB: Documentation: Update description of completion callback context
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
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 <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH] USB: Documentation: Update description of completion callback context
In reply to Alan SternQuoted 9 lines · Alan Stern
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 <[email protected]>
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
Quoted 13 lines · Sebastian Andrzej Siewior
Quoted 9 lines · Alan Stern
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 <[email protected]>Reviewed-by Sebastian Andrzej Siewior <[email protected]>
Quoted 8 lines
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
Quoted 21 lines · Alan Stern
Quoted 20 lines · Sebastian Andrzej Siewior
Quoted 9 lines · Alan Stern
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 <[email protected]>Reviewed-by Sebastian Andrzej Siewior <[email protected]>
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.
Quoted 5 lines
Quoted 1 line
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.
Quoted 1 line
Alan Stern
Sebastian
Oliver Neukum
maintainerQuoted 4 lines · Alan Stern
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.
Regards
Oliver
Quoted 9 lines · Oliver Neukum
Quoted 4 lines · Alan Stern
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?
Quoted 2 lines
Regards
Oliver
Sebastian
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Mon, 5 Oct 2026 00:59:25 +0300 you wrote:
tx_complete()callsthis_cpu_ptr()before disabling preemption, which triggers a BUG when running withCONFIG_DEBUG_PREEMPT:BUG: using smp_processor_id() in preemptible code in tx_completeFix 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.[...]
Here is the summary with links:
- [net,v6] usbnet: fix smp_processor_id() use in preemptible context
https://git.kernel.org/netdev/net-next/c/95a5c641fc7b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html