Skip to contentSkip to conversation

usb: xhci: Fix isochronous scheduling regression

PATCH In review Michal Pecio 3 participants 10 messages Aug 21 – Aug 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: xhci: Fix isochronous scheduling regression

An isoc URB without URB_ISO_ASAP should be scheduled immediately after the previous one, unless it's the first submission or prior URBs have completed without resubmitting and the endpoint became idle.

An HCD_BH driver must consider URBs pending completion in the BH queue in addition to its own queue. Regrettably, core doesn't provide much information, we can only know if we are being called by completion now. This issue is as old as HCD_BH, affects ehci-hcd too and has no known reproducible impact, as drivers generally resubmit from completion.

A recent patch tried to address it by looking at xHCI HW state instead. Obviously, HW has no knowledge of the BH giveback queue either, and the whole solution amounts to testing whether prior URBs have been unlinked instead of completing normally - then a new stream is assumed.

This leads to false negatives when a driver simply allows the endpoint to empty out and begins a new stream. New URBs are scheduled into the past and promptly fail with -EXDEV status, causing data loss and worse, because drivers get confused by premature completion, particularly when multiple endpoints are started at once and required to stay in sync.

snd-usb-audio underruns the OUT endpoint when userspace fails to supply playback data in time. If this is detected in duplex mode, IN URBs are unlinked and both streams restarted. OUT underruns again before IN even begins, another recovery is attempted and the cycle repeats.

Fix this by using the best criteria we can muster, taken from ehci-hcd. This brings false negative rate back to zero and false positive rate to less than ever before in xhci-hcd. Traditional logic was equivalent to:

	if (list_empty(&ep_ring->td_list) ||
	    GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
		// consider this URB a new stream

While free of false negatives, it had easily avoidable false positives:
* no check for completion in progress when the list is empty
* the ep_ctx check doesn't make up for it at all, but it adds a race - EP state can remain "stopped" for a while after the first submission

Fixes add8469b3e00 ("xhci: fix frame id calculation and checks for isoc URBs")
---
 drivers/usb/host/xhci-ring.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +4 -3
index f27bc132d0e9..8b0c27d6f12d 100644
@@ -4311,10 +4311,11 @@ int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
 	check_interval(urb, ep_ctx);
 
 	/*
-	 * Check if this starts the isoc data flow. Relies on hw setting ep ctx
-	 * state after doorbell ring. Consider adding list_empty(td_list) check
+	 * Schedule the URB discontiguously if all previous URBs have completed.
+	 * XXX core can't tell if completions are pending but not running yet.
 	 */
-	if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
+	if (list_empty(&ep_ring->td_list) &&
+	    !hcd_periodic_completion_in_progress(xhci_to_hcd(xhci), urb->ep))
 		xep->next_uframe = -1;
 
 	return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
Signature · 1 line
2.48.1
#1 lore raw
Quoted 30 lines · Michal Pecio

An isoc URB without URB_ISO_ASAP should be scheduled immediately after the previous one, unless it's the first submission or prior URBs have completed without resubmitting and the endpoint became idle.

An HCD_BH driver must consider URBs pending completion in the BH queue in addition to its own queue. Regrettably, core doesn't provide much information, we can only know if we are being called by completion now. This issue is as old as HCD_BH, affects ehci-hcd too and has no known reproducible impact, as drivers generally resubmit from completion.

A recent patch tried to address it by looking at xHCI HW state instead. Obviously, HW has no knowledge of the BH giveback queue either, and the whole solution amounts to testing whether prior URBs have been unlinked instead of completing normally - then a new stream is assumed.

This leads to false negatives when a driver simply allows the endpoint to empty out and begins a new stream. New URBs are scheduled into the past and promptly fail with -EXDEV status, causing data loss and worse, because drivers get confused by premature completion, particularly when multiple endpoints are started at once and required to stay in sync.

snd-usb-audio underruns the OUT endpoint when userspace fails to supply playback data in time. If this is detected in duplex mode, IN URBs are unlinked and both streams restarted. OUT underruns again before IN even begins, another recovery is attempted and the cycle repeats.

Fix this by using the best criteria we can muster, taken from ehci-hcd. This brings false negative rate back to zero and false positive rate to less than ever before in xhci-hcd. Traditional logic was equivalent to:

Quoted 8 lines
	if (list_empty(&ep_ring->td_list) ||
	    GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
		// consider this URB a new stream

While free of false negatives, it had easily avoidable false positives:
* no check for completion in progress when the list is empty
* the ep_ctx check doesn't make up for it at all, but it adds a race - EP state can remain "stopped" for a while after the first submission

I would still prefer:
if (list_empty(&ep_ring->td_list) && GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING) to detect the start of a new isoch stream.

It has zero false positives mid stream, and fixes the "stopped" state race case.

I can't figure out when the false negatives it introduces is an issue.

If you can show me a usecase where a driver or specification is designed to let an isoch endpoint intentionally run dry, leaving it in running/idle, and then continue queuing URBs expecting ASAP scheduling, then we can change it.

UAC and UVC go to altsetting 0 and drop the isoch endpoint on stop/pause (says AI)

Quoted 15 lines
Fixes add8469b3e00 ("xhci: fix frame id calculation and checks for isoc URBs")
---
  drivers/usb/host/xhci-ring.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +0 -0
index f27bc132d0e9..8b0c27d6f12d 100644
@@ -4311,10 +4311,11 @@ int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
  	check_interval(urb, ep_ctx);
 

drivers/usb/host/xhci-ring.c int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, · line 4319

Quoted 10 lines
drivers/usb/host/xhci-ring.c +4 -3
  	/*
-	 * Check if this starts the isoc data flow. Relies on hw setting ep ctx
-	 * state after doorbell ring. Consider adding list_empty(td_list) check
+	 * Schedule the URB discontiguously if all previous URBs have completed.
+	 * XXX core can't tell if completions are pending but not running yet.
  	 */
-	if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
+	if (list_empty(&ep_ring->td_list) &&
+	    !hcd_periodic_completion_in_progress(xhci_to_hcd(xhci), urb->ep))
  		xep->next_uframe = -1;

I don't know why we intentionally should introduce the false positive case, hiding a known issue.

At least we should add a debug message so audio/video developers can find the reason for the glitches:

if (list_empty(&ep_ring->td_list) &&

    !hcd_periodic_completion_in_progress(xhci_to_hcd(xhci), urb->ep)) {
	if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING)
		xhci_dbg(xhci, "Starting new isoc stream on running endpoint at uframe %d, killing sync...
	xep->next_uframe = -1;
}

Thanks
Mathias

#2 lore raw
Quoted 21 lines · Mathias Nyman
Quoted 4 lines · Michal Pecio

Fix this by using the best criteria we can muster, taken from ehci-hcd. This brings false negative rate back to zero and false positive rate to less than ever before in xhci-hcd. Traditional logic was equivalent to:

Quoted 8 lines
	if (list_empty(&ep_ring->td_list) ||
	    GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
		// consider this URB a new stream

While free of false negatives, it had easily avoidable false positives:
* no check for completion in progress when the list is empty
* the ep_ctx check doesn't make up for it at all, but it adds a race - EP state can remain "stopped" for a while after the first submission

I would still prefer:
if (list_empty(&ep_ring->td_list) && GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING) to detect the start of a new isoch stream.

It has zero false positives mid stream, and fixes the "stopped" state race case.

Quoted 6 lines

I can't figure out when the false negatives it introduces is an issue.

If you can show me a usecase where a driver or specification is designed to let an isoch endpoint intentionally run dry, leaving it in running/idle, and then continue queuing URBs expecting ASAP scheduling, then we can change it.

Well, snd-usb-audio does. In snd_complete_urb(), in the usb_pipeout(ep->pipe) case and ep->lowlatency_playback, which is the default (controlled by 'lowlatency' module parameter).

It adds the completed URB to a queue of "ready" URBs and then tries to submit as many URBs from that queue as possible, which may be zero if there is no data to send. Then pending URB count is checked for zero, which triggers xrun recovery in ALSA core.

I haven't dug into ALSA, but the observable outcome is:

snd_usb_endpoint_stop() is called on both IN and OUT, which unlinks IN URBs and does nothing to OUT (zero URBs left).

snd_usb_endpoint_start() is called on both endpoints to submit URBs.

See dynamic debug below. Notably, "Setting usb interface" does appear, but only at the end, when I killed jackd. And it seems suboptimal to demand that drivers spend time on control requests in such cases.

[146029.539992] xhci_hcd 0000:06:00.0: Stopped on Transfer TRB for slot 1 ep 2
[146029.540031] xhci_hcd 0000:06:00.0: Underrun event on slot 1 ep 1
[146029.542857] usb 6-2: Starting data EP 0x81 (running 0)
[146029.542949] usb 6-2: 12 URBs submitted for EP 0x81
[146029.542958] usb 6-2: 2:2 Start Capture PCM
[146029.542962] usb 6-2: Starting data EP 0x1 (running 0)
[146029.542991] usb 6-2: 2 URBs submitted for EP 0x1
[146029.542996] usb 6-2: 1:3 Start Playback PCM
[146029.543799] usb 6-2: notify_xrun ep 01 from line 579	<-- my patch
[146029.543816] usb 6-2: Stopping data EP 0x81 (running 1)
[146029.543861] usb 6-2: 2:2 Stop Capture PCM
[146029.543863] usb 6-2: Stopping data EP 0x1 (running 1)
[146029.543865] usb 6-2: 1:3 Stop Playback PCM
[146029.543882] xhci_hcd 0000:06:00.0: Stopped on Transfer TRB for slot 1 ep 2
[146029.543886] xhci_hcd 0000:06:00.0: Underrun event on slot 1 ep 1
[146029.545849] usb 6-2: Starting data EP 0x81 (running 0)
[146029.545962] usb 6-2: 12 URBs submitted for EP 0x81
[146029.545969] usb 6-2: 2:2 Start Capture PCM
[146029.545972] usb 6-2: Starting data EP 0x1 (running 0)
[146029.545998] usb 6-2: 2 URBs submitted for EP 0x1
[146029.546001] usb 6-2: 1:3 Start Playback PCM
[146029.546793] usb 6-2: notify_xrun ep 01 from line 579
[146029.546814] usb 6-2: Stopping data EP 0x81 (running 1)
[146029.546876] usb 6-2: 2:2 Stop Capture PCM
[146029.546881] usb 6-2: Stopping data EP 0x1 (running 1)
[146029.546885] usb 6-2: 1:3 Stop Playback PCM
[146029.546903] xhci_hcd 0000:06:00.0: Underrun event on slot 1 ep 1
[146029.546909] xhci_hcd 0000:06:00.0: Stopped on Transfer TRB for slot 1 ep 2
[146029.557837] usb 6-2: Closing EP 0x81 (count 1)
[146029.557845] usb 6-2: Setting usb interface 2:0 for EP 0x81
[146029.558636] usb 6-2: EP 0x81 closed
[146029.558664] usb 6-2: Closing EP 0x1 (count 1)
[146029.558666] usb 6-2: Setting usb interface 1:0 for EP 0x1
[146029.559505] usb 6-2: EP 0x1 closed

0000:06:00.0 is ASM1042, USB 3.0, no CFC. Hence no Missed Service before the Underrun. OUT URBs seem to complete successfully, but ALSA apparently doesn't feed playback data until the IN endpoint starts moving, which is scheduled to happen later, so OUT underruns again and the whole mess repeats.

On CFC, all OUT URBs complete 1 uframe after submisison with MSE, so it's pretty much guaranteed to happen before any IN is scheduled.

Up to v7.2, such xrun is only a brief audible glitch. On usb-next it loops like that for seconds to minutes before recovering by some lucky miracle (IDK why that happens, but sometimes it panics and begins unlinking OUT URBs before the last one completes).

Hence the idea to not rock the boat too much and stay with zero false negatives and only the unavoidable false postives which have been here in HCD_BH drivers for ages.

The whole thing is madness. It nobody has issues with wasting 4 bytes per endpoint, I think it could be fixed by counting all pending URBs from submit to complete. Changes to ehci/xhci-hcd will be minimal. Or add a flag which overrides this guesswork. But too late for v7.3.

Quoted 9 lines

At least we should add a debug message so audio/video developers can find the reason for the glitches:

if (list_empty(&ep_ring->td_list) &&

     !hcd_periodic_completion_in_progress(xhci_to_hcd(xhci), urb->ep)) {
		if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING)
 			xhci_dbg(xhci, "Starting new isoc stream on running endpoint at uframe %d, killing sync..
		xep->next_uframe = -1;
}

That seems harmless, although the same information can be obtained by checking urb->start_frame after submission.

Fun fact, I found only two in-tree drivers which do anything meaningful with it. Particularly, if snd-usb-audio submits IN and OUT across frame boundary, it can only recover by xruning and trying again, meh.

Regards,
Michal

#3 lore raw
In reply to Michal Pecio
Quoted 1 line · Michal Pecio

Fix this by using the best criteria we can muster, taken from ehci-hcd.

Maybe it's time to correct the hcd_periodic_completion_in_progress() implementation.

For instance, we could add an atomic giveback_count field to the usb_host_endpoint struct. The HCD would increment the field (while still holding its private lock) before doing a giveback, and __usb_hcd_giveback_urb() would decrement the field after calling the completion handler.

What do you think?

Alan Stern

#4 lore raw
Quoted 10 lines · Alan Stern

Maybe it's time to correct the hcd_periodic_completion_in_progress() implementation.

For instance, we could add an atomic giveback_count field to the usb_host_endpoint struct. The HCD would increment the field (while still holding its private lock) before doing a giveback, and __usb_hcd_giveback_urb() would decrement the field after calling the completion handler.

What do you think?

I would go as far as incrementing it on successful usb_submit_urb() and completely doing away with those list_empty(td_list) checks in HCDs.

I wrote an xhci-only (less compilation and module reloading) prototype which relies on hijacking completions of isoc URBs for counting, it worked, results identical as with the standard solution in a few test runs with snd-usb-audio.

Theoretical race condition: it seems we can't prevent new submissions after completion releases its lock and class driver considers the pipe idle, but before the counter is decremented to zero. That would be another case of "scheduling to the past" unexpectedly.

Seems low probability, but this type of bug hasn't existed so far, we generally have the opposite problem.

Or does it exist in non-BH HCDs?

Maybe the documented API guarantee just isn't feasible?

Entirely out of the box alternative: new URB flag. And really, if only xhci-hcd existed, it wouldn't be hard to even implement explicit start_frame requests or hints as MOTU wished for, which would make the whole business of starting synchronized endpoints cleaner.

Regards,
Michal

#5 lore raw
Quoted 25 lines · Michal Pecio
Quoted 10 lines · Alan Stern

Maybe it's time to correct the hcd_periodic_completion_in_progress() implementation.

For instance, we could add an atomic giveback_count field to the usb_host_endpoint struct. The HCD would increment the field (while still holding its private lock) before doing a giveback, and __usb_hcd_giveback_urb() would decrement the field after calling the completion handler.

What do you think?

I would go as far as incrementing it on successful usb_submit_urb() and completely doing away with those list_empty(td_list) checks in HCDs.

I wrote an xhci-only (less compilation and module reloading) prototype which relies on hijacking completions of isoc URBs for counting, it worked, results identical as with the standard solution in a few test runs with snd-usb-audio.

Theoretical race condition: it seems we can't prevent new submissions after completion releases its lock and class driver considers the pipe idle, but before the counter is decremented to zero. That would be another case of "scheduling to the past" unexpectedly.

Quoted 4 lines

Seems low probability, but this type of bug hasn't existed so far, we generally have the opposite problem.

Or does it exist in non-BH HCDs?

It does. It's impossible for a non-BH HCD to reacquire its private lock exactly when the completion handler returns, so somewhere around that time the class driver and the HCD will inevitably have differing opinions about whether the iso queue is empty.

Quoted 1 line

Maybe the documented API guarantee just isn't feasible?

I'm not averse to your proposed solution. As for that race, just update the API documentation to take it into account. Seriously -- it's an unavoidable defect and that's all we can do about it.

Possible alternative: Make the URB_ISO_ASAP flag take precedence over the "queue is non-empty" condition.

Quoted 4 lines

Entirely out of the box alternative: new URB flag. And really, if only xhci-hcd existed, it wouldn't be hard to even implement explicit start_frame requests or hints as MOTU wished for, which would make the whole business of starting synchronized endpoints cleaner.

Yes. Something like an URB_USE_START_FRAME flag. It wouldn't help much for starting a single endpoint, but it would allow drivers to synchronize new iso streams with existing ones more easily. (Although there would be a little uncertainty in cases where the schedules for the two streams don't have the same phase.)

Alan Stern

#6 lore raw
Quoted 11 lines · Alan Stern
Quoted 10 lines · Michal Pecio
Quoted 9 lines · Alan Stern

Maybe it's time to correct the hcd_periodic_completion_in_progress() implementation.

For instance, we could add an atomic giveback_count field to the usb_host_endpoint struct. The HCD would increment the field (while still holding its private lock) before doing a giveback, and __usb_hcd_giveback_urb() would decrement the field after calling the completion handler.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

What do you think?

I would go as far as incrementing it on successful usb_submit_urb() and completely doing away with those list_empty(td_list) checks in HCDs.

This sounds like a good idea.

Regarding theoretical race conditions: If URBs for some odd and unknown reason are submitted by several callers to the same endpoint (no idea why or how this would happen) then, usb_hcd_submit_urb() could theoretically call hcd->driver->urb_enqueue(hcd) for the second URB right when .urb_enqueue(hcd) returned for the first URB, but _before_ increasing the atomic counter.

In this case hcd would incorrectly assume both first and second URB start a new isoc stream. This would be avoided by hcd still checking list_empty(td_list)

Anyway, suggested solution still sounds like an improvement compared to what we currently have.

Quoted 21 lines
Quoted 11 lines

I wrote an xhci-only (less compilation and module reloading) prototype which relies on hijacking completions of isoc URBs for counting, it worked, results identical as with the standard solution in a few test runs with snd-usb-audio.

Theoretical race condition: it seems we can't prevent new submissions after completion releases its lock and class driver considers the pipe idle, but before the counter is decremented to zero. That would be another case of "scheduling to the past" unexpectedly.

Quoted 4 lines

Seems low probability, but this type of bug hasn't existed so far, we generally have the opposite problem.

Or does it exist in non-BH HCDs?

It does. It's impossible for a non-BH HCD to reacquire its private lock exactly when the completion handler returns, so somewhere around that time the class driver and the HCD will inevitably have differing opinions about whether the iso queue is empty.

Quoted 8 lines
Quoted 1 line

Maybe the documented API guarantee just isn't feasible?

I'm not averse to your proposed solution. As for that race, just update the API documentation to take it into account. Seriously -- it's an unavoidable defect and that's all we can do about it.

Possible alternative: Make the URB_ISO_ASAP flag take precedence over the "queue is non-empty" condition.

xhci driver does this. If URB_ISO_ASAP is set then xhci driver always sets the SIA "Start Isoch ASAP" flag for the transfer blocks.

The urb->start_frame value set by xhci driver might not be correct in this case.
Thanks
Mathias

#7 lore raw
Quoted 19 lines · Mathias Nyman
Quoted 11 lines · Alan Stern
Quoted 10 lines · Michal Pecio
Quoted 9 lines · Alan Stern

Maybe it's time to correct the hcd_periodic_completion_in_progress() implementation.

For instance, we could add an atomic giveback_count field to the usb_host_endpoint struct. The HCD would increment the field (while still holding its private lock) before doing a giveback, and __usb_hcd_giveback_urb() would decrement the field after calling the completion handler.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

What do you think?

I would go as far as incrementing it on successful usb_submit_urb() and completely doing away with those list_empty(td_list) checks in HCDs.

This sounds like a good idea.

Quoted 8 lines

Regarding theoretical race conditions: If URBs for some odd and unknown reason are submitted by several callers to the same endpoint (no idea why or how this would happen) then, usb_hcd_submit_urb() could theoretically call hcd->driver->urb_enqueue(hcd) for the second URB right when .urb_enqueue(hcd) returned for the first URB, but _before_ increasing the atomic counter.

In this case hcd would incorrectly assume both first and second URB start a new isoc stream. This would be avoided by hcd still checking list_empty(td_list)

This is why my original suggestion had the HCD incrementing the counter while holding its private lock.

Quoted 8 lines

Anyway, suggested solution still sounds like an improvement compared to what we currently have.

Quoted 2 lines

Possible alternative: Make the URB_ISO_ASAP flag take precedence over the "queue is non-empty" condition.

xhci driver does this. If URB_ISO_ASAP is set then xhci driver always sets the SIA "Start Isoch ASAP" flag for the transfer blocks.

I wasn't very precise before. I meant URB_ISO_ASAP should take precedence when there are no active URBs but there may still be some URBs being given back. In other words, when the list_empty test succeeds. If the list of queued URBs is not empty then new URBs should always be assigned to the next available slot -- unless we decide to support a new URB_USE_FRAME flag and the flag is set.

The point here is that if the class driver wants to change the alignment between URBs and uframes, without worrying about the BH giveback race, all it has to do is set URB_ISO_ASAP. If it doesn't care about the alignment (implying that it also doesn't care if some URBs are assigned to expired slots) then the race doesn't matter.

And of course, if the class driver wants to maintain the alignment then it should resubmit URBs from the completion handler, so that the queue doesn't empty out unless there is an underrun. Then the BH giveback race won't be an issue.

Quoted 2 lines

The urb->start_frame value set by xhci driver might not be correct in this case.

Do you mean it might be wrong because xhci-hcd can't tell what uframe the xHC hardware actually selects?

Alan Stern

#8 lore raw
Quoted 11 lines · Alan Stern
Quoted 9 lines · Mathias Nyman
Quoted 5 lines · Alan Stern
Quoted 1 line · Michal Pecio

On Fri, 21 Aug 2026 10:44:07 -0400, Alan Stern wrote:

Possible alternative: Make the URB_ISO_ASAP flag take precedence over the "queue is non-empty" condition.

xhci driver does this. If URB_ISO_ASAP is set then xhci driver always sets the SIA "Start Isoch ASAP" flag for the transfer blocks.

Quoted 7 lines

I wasn't very precise before. I meant URB_ISO_ASAP should take precedence when there are no active URBs but there may still be some URBs being given back. In other words, when the list_empty test succeeds. If the list of queued URBs is not empty then new URBs should always be assigned to the next available slot -- unless we decide to support a new URB_USE_FRAME flag and the flag is set.

URB_ISO_ASAP always takes precedence in xHC case.

xHC controller has a "SIA" flag that we set for each TD (URB frame) in the URB when USR_ISO_ASAP is set.
If there are no active URBs mid stream, and ring underruns, then xHC will process the next TD with SIA flag at its earliest possible slot.
So URBs with URB_ISO_ASAP flag will be out of sync and laggy, but not lose data in underrun cases.

A frame is only dropped in SIA case if xHC fails for internal reasons to process that transfer in time. This triggers a Missed Service Error, and xHC moves to process the next TD (frame).

Quoted 11 lines

The point here is that if the class driver wants to change the alignment between URBs and uframes, without worrying about the BH giveback race, all it has to do is set URB_ISO_ASAP. If it doesn't care about the alignment (implying that it also doesn't care if some URBs are assigned to expired slots) then the race doesn't matter.

And of course, if the class driver wants to maintain the alignment then it should resubmit URBs from the completion handler, so that the queue doesn't empty out unless there is an underrun. Then the BH giveback race won't be an issue.

Quoted 5 lines
Quoted 2 lines

The urb->start_frame value set by xhci driver might not be correct in this case.

Do you mean it might be wrong because xhci-hcd can't tell what uframe the xHC hardware actually selects?

xhci driver currently guesses the urb->start_frame based on current harware frame number + a scheduling threshold. This is small inaccuracy in the start. Not the real issue.

After this the xhci driver start_frame is just a running number during the stream, incremented on every queued frame. urb->start_frame mid stream is based on this.

If URB_ISO_ASAP (SIA in xhci) is set then we know that every URB enqueued after a underrun event will be delayed by at least one frame. In underrun cases where there aren't any URBs queued yet we should set base the next urb->start_frame on hardware frame id + scheduling threshold instead of the running number.

This is on my todo list.

Class driver can then detect lagging out of sync URB_ISO_ASAP transfers based on the unexpected large gaps between urb->start_frame numbers

Thanks
Mathias

#9 lore raw
Quoted 20 lines · Mathias Nyman
Quoted 11 lines · Alan Stern
Quoted 9 lines · Mathias Nyman
Quoted 5 lines · Alan Stern
Quoted 1 line · Michal Pecio

On Fri, 21 Aug 2026 10:44:07 -0400, Alan Stern wrote:

Possible alternative: Make the URB_ISO_ASAP flag take precedence over the "queue is non-empty" condition.

xhci driver does this. If URB_ISO_ASAP is set then xhci driver always sets the SIA "Start Isoch ASAP" flag for the transfer blocks.

Quoted 7 lines

I wasn't very precise before. I meant URB_ISO_ASAP should take precedence when there are no active URBs but there may still be some URBs being given back. In other words, when the list_empty test succeeds. If the list of queued URBs is not empty then new URBs should always be assigned to the next available slot -- unless we decide to support a new URB_USE_FRAME flag and the flag is set.

Quoted 8 lines

URB_ISO_ASAP always takes precedence in xHC case.

xHC controller has a "SIA" flag that we set for each TD (URB frame) in the URB when USR_ISO_ASAP is set.
If there are no active URBs mid stream, and ring underruns, then xHC will process the next TD with SIA flag at its earliest possible slot.
So URBs with URB_ISO_ASAP flag will be out of sync and laggy, but not lose data in underrun cases.

Well, that's kind of the point.

The USB spec explicitly allows -- and expects! -- isochronous transfers to lose data from time to time. That's why these transfers don't use ACK/NAK handshakes and don't do retransmissions.

Given this attitude, it seems very likely that the USBIF would recommend losing data rather than losing synchronization. I don't know what the attitude is of the various drivers in the kernel, but it wouldn't be surprising if they also take this point of view. (Consider trying to keep an audio stream in sync with a video stream, for example.)

Quoted 3 lines

A frame is only dropped in SIA case if xHC fails for internal reasons to process that transfer in time. This triggers a Missed Service Error, and xHC moves to process the next TD (frame).

But as you said, the hardware will try to transfer the packet even if it chooses to use a time slot later than the one the class driver wanted. This is the wrong approach; the packet should be dropped, and the later time slot should contain the packet that the class driver intended.

Perhaps you will say this merely shows that these class drivers shouldn't set URB_ISO_ASAP. Or to put it another way, URB_ISO_ASAP indicates that the driver prefers losing synchronization over losing data.

Quoted 2 lines

Class driver can then detect lagging out of sync URB_ISO_ASAP transfers based on the unexpected large gaps between urb->start_frame numbers

The class driver should realize that URB_ISO_ASAP generally _will_ incur lag and loss of synchronization occasionally. It should set that flag only when it doesn't mind these things. In which case it probably doesn't need to check the gaps between start_frame numbers.

The real problem we are trying to solve here is what should happen when a class driver starts falling behind, submitting URBs too late. If it gets _very_ far behind, there's really no choice but to restart the stream, losing synchronization. But if the driver is only a little behind (and has a chance of catching up again), there is a choice between losing synchronization and losing data. URB_ISO_ASAP allows the driver to tell us how it wants that choice to be made.

Furthermore, as long as the class driver is doing the best it can -- by resubmitting from within its completion handler -- we should try to support it by putting in the "only a little behind" category.

Alan Stern