usb: xhci: fix DMA address corruption in abort_td
Subsystems (2)
PATCH 1/1 usb: xhci: fix DMA address corruption in abort_td
When aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).
Inside xhci_trb_virt_to_dma(), the offset calculation:
segment_offset = trb - seg->trbs;
operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a SET_DEQ_PTR command was issued with a DMA address of 0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).
Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
--- drivers/usb/host/xhci-ring.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +5 -3
@@ -582,9 +582,11 @@ static void abort_td(struct usb_device *udev, int ep_index)TRB_TO_SLOT_ID (le32_to_cpu (event->event_cmd.flags)) != udev->slot_id || (comp != COMP_SUCCESS && comp != COMP_CTX_STATE));xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue); +if (ring->cycle_state) + addr |=0x1ULL ; +else + addr &= ~0x1ULL ;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
Signature · 1 line
2.25.1
Quoted 3 lines · ANANDHAKRISHNAN S
When aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
reset_ep() seems to have the same problem ?
Quoted 30 lines
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to
xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).Inside
xhci_trb_virt_to_dma(), the offset calculation:
segment_offset= trb -seg->trbs;operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a
SET_DEQ_PTRcommand was issued with a DMA address of0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Signed-off-by ANANDHAKRISHNAN S <[email protected]>--- drivers/usb/host/xhci-ring.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/usb/host/xhci-ring.c +0
-0
drivers/usb/host/xhci-ring.c static void abort_td(struct usb_device *udev, int ep_index) · line 587
Quoted 6 lines
drivers/usb/host/xhci-ring.c +0
-3@@ -582,9 +582,11 @@ static void abort_td(struct usb_device *udev, int ep_index)TRB_TO_SLOT_ID (le32_to_cpu (event->event_cmd.flags)) != udev->slot_id || (comp != COMP_SUCCESS && comp != COMP_CTX_STATE));xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state));
- (void *)((uintptr_t)ring->enqueue | ring->cycle_state));
+ (void *)(uintptr_t)ring->enqueue) | ring->cycle_state;
cycle_state is already either 0 or 1, isn't it ?
Quoted 8 lines
drivers/usb/host/xhci-ring.c +5
-0+ addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue); +if (ring->cycle_state) + addr |=0x1ULL ; +else + addr &= ~0x1ULL ;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
Quoted 1 line · Marek Vasut
reset_ep()seems to have the same problem ?
Yes,it follows the same pattern of OR-ing the cycle state into the virtual enqueue pointer before translation. I’ll audit that path and include a corresponding fix so the cycle bit is applied only after converting to a DMA address, consistent with the change here.
Quoted 1 line
cycle_stateis already either 0 or 1, isn't it ?
Yes, ring->cycle_state is indeed either 0 or 1. The intent of the explicit masking was to make it clear that the cycle bit is applied only to the *physical* address returned by xhci_trb_virt_to_dma(), and not to the virtual TRB pointer itself, which was the root cause of the corruption. I agree this can be simplified stylistically, and I can fold it into a more compact expression in a v2.
I’m happy to change this to:
addr = (addr & ~0x1ULL) | ring->cycle_state;
I’ll send a v2 addressing both points.
Thanks,
Anandhakrishnan S
Quoted 18 lines · ANANDHAKRISHNAN S
Quoted 1 line · Marek Vasut
reset_ep()seems to have the same problem ?Yes,it follows the same pattern of OR-ing the cycle state into the virtual enqueue pointer before translation. I’ll audit that path and include a corresponding fix so the cycle bit is applied only after converting to a DMA address, consistent with the change here.
Quoted 1 line
cycle_stateis already either 0 or 1, isn't it ?Yes,
ring->cycle_stateis indeed either 0 or 1. The intent of the explicit masking was to make it clear that the cycle bit is applied only to the *physical* address returned byxhci_trb_virt_to_dma(), and not to the virtual TRB pointer itself, which was the root cause of the corruption. I agree this can be simplified stylistically, and I can fold it into a more compact expression in a v2.
Quoted 5 lines
I’m happy to change this to:
addr = (addr & ~0x1ULL) | ring->cycle_state;I’ll send a v2 addressing both points.
Please do a V2, thanks !
Also, please CC Mattijs for V2.
PATCH v2 1/1 usb: xhci: fix DMA address corruption in abort_td
In reply to ANANDHAKRISHNAN SWhen aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).
Inside xhci_trb_virt_to_dma(), the offset calculation:
segment_offset = trb - seg->trbs;
operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a SET_DEQ_PTR command was issued with a DMA address of 0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).
Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Changes in v2:
- Apply the same fix to reset_ep()
- Simplify cycle bit handling to a single mask operation
--- drivers/usb/host/xhci-ring.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/usb/host/xhci-ring.c +4 -6
@@ -523,9 +523,8 @@ static void reset_ep(struct usb_device *udev, int ep_index) field =le32_to_cpu (event->trans_event.flags);BUG_ON (TRB_TO_SLOT_ID (field) != udev->slot_id);xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue); + addr = (addr & ~0x1ULL ) | ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
@@ -582,9 +581,8 @@ static void abort_td(struct usb_device *udev, int ep_index)TRB_TO_SLOT_ID (le32_to_cpu (event->event_cmd.flags)) != udev->slot_id || (comp != COMP_SUCCESS && comp != COMP_CTX_STATE));xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue); + addr = (addr & ~0x1ULL ) | ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
Signature · 1 line
2.25.1
Quoted 38 lines · ANANDHAKRISHNAN S
When aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to
xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).Inside
xhci_trb_virt_to_dma(), the offset calculation:
segment_offset= trb -seg->trbs;operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a
SET_DEQ_PTRcommand was issued with a DMA address of0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Changes in v2:
- Apply the same fix toreset_ep()
- Simplify cycle bit handling to a single mask operationSigned-off-by ANANDHAKRISHNAN S <[email protected]>--- drivers/usb/host/xhci-ring.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)drivers/usb/host/xhci-ring.c +0
-0
drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 527
Quoted 8 lines
drivers/usb/host/xhci-ring.c +2
-3@@ -523,9 +523,8 @@ static void reset_ep(struct usb_device *udev, int ep_index) field =le32_to_cpu (event->trans_event.flags);BUG_ON (TRB_TO_SLOT_ID (field) != udev->slot_id);xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue); + addr = (addr & ~0x1ULL ) | ring->cycle_state;
Can addr actually ever have bit(0) set ? If not, addr & ~0x1ULL is not needed and you can literally addr = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue) | ring->cycle_state;
Quoted 3 lines
Can addr actually ever have bit(0) set ? If not, addr & ~0x1ULL is not needed and you can literally addr =
xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue)|ring->cycle_state;
You're right. The address is guaranteed to be aligned and bit(0) will always be 0. I'll remove the redundant masking and send a v3 with the simplified logic.
Thanks,
Anandhakrishnan S
PATCH v3 1/1 usb: xhci: fix DMA address corruption in abort_td
In reply to ANANDHAKRISHNAN SWhen aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).
Inside xhci_trb_virt_to_dma(), the offset calculation:
segment_offset = trb - seg->trbs;
operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a SET_DEQ_PTR command was issued with a DMA address of 0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).
Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Changes in v3:
- Simplified the DMA address calculation by removing the redundant bit(0) mask (& ~0x1ULL), as XHCI guarantees the address is already aligned.
Changes in v2:
- Apply the same fix to reset_ep()
- Simplify cycle bit handling to a single mask operation
--- drivers/usb/host/xhci-ring.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/usb/host/xhci-ring.c +4 -6
@@ -523,9 +523,8 @@ static void reset_ep(struct usb_device *udev, int ep_index) field =le32_to_cpu (event->trans_event.flags);BUG_ON (TRB_TO_SLOT_ID (field) != udev->slot_id);xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue) | + ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
@@ -582,9 +581,8 @@ static void abort_td(struct usb_device *udev, int ep_index)TRB_TO_SLOT_ID (le32_to_cpu (event->event_cmd.flags)) != udev->slot_id || (comp != COMP_SUCCESS && comp != COMP_CTX_STATE));xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue) | + ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
Signature · 1 line
2.25.1
Quoted 20 lines · ANANDHAKRISHNAN S
When aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to
xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).Inside
xhci_trb_virt_to_dma(), the offset calculation:
segment_offset= trb -seg->trbs;operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a
SET_DEQ_PTRcommand was issued with a DMA address of0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).
Quoted 5 lines
Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Changes in v3:
The changelog should be below --- , else it gets included in the commit when it is applied.
Quoted 10 lines
- Simplified the DMA address calculation by removing the redundant bit(0) mask (& ~0x1ULL), as XHCI guarantees the address is already aligned.
Changes in v2:
- Apply the same fix toreset_ep()
- Simplify cycle bit handling to a single mask operationSigned-off-by ANANDHAKRISHNAN S <[email protected]>---
i.e. patch changelog goes HERE
Quoted 2 lines
drivers/usb/host/xhci-ring.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
PATCH v4 1/1 usb: xhci: fix DMA address corruption in abort_td
In reply to ANANDHAKRISHNAN SWhen aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).
Inside xhci_trb_virt_to_dma(), the offset calculation:
segment_offset = trb - seg->trbs;
operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a SET_DEQ_PTR command was issued with a DMA address of 0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).
Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
---
Changes in v4:
- Moved the changelog below the '---' marker to prevent it from being included in the permanent git history.
Changes in v3:
- Simplified the DMA address calculation by removing the redundant bit(0) mask (& ~0x1ULL), as XHCI guarantees the address is already aligned.
Changes in v2:
- Apply the same fix to reset_ep()
- Simplify cycle bit handling to a single mask operation
drivers/usb/host/xhci-ring.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/usb/host/xhci-ring.c +4 -6
@@ -523,9 +523,8 @@ static void reset_ep(struct usb_device *udev, int ep_index) field =le32_to_cpu (event->trans_event.flags);BUG_ON (TRB_TO_SLOT_ID (field) != udev->slot_id);xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue) | + ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
@@ -582,9 +581,8 @@ static void abort_td(struct usb_device *udev, int ep_index)TRB_TO_SLOT_ID (le32_to_cpu (event->event_cmd.flags)) != udev->slot_id || (comp != COMP_SUCCESS && comp != COMP_CTX_STATE));xhci_acknowledge_event (ctrl); - - addr =xhci_trb_virt_to_dma (ring->enq_seg, - (void *)((uintptr_t )ring->enqueue | ring->cycle_state)); + addr =xhci_trb_virt_to_dma (ring->enq_seg, ring->enqueue) | + ring->cycle_state;xhci_queue_command (ctrl, addr, udev->slot_id, ep_index,TRB_SET_DEQ ); event =xhci_wait_for_event (ctrl,TRB_COMPLETION );if (!event)
Signature · 1 line
2.25.1
Quoted 25 lines · ANANDHAKRISHNAN S
When aborting a Transfer Descriptor (TD), the xHCI driver updates the device dequeue pointer by converting the virtual enqueue TRB pointer into a DMA address.
Previously, the code OR-ed the ring's Dequeue Cycle State (DCS) bit into the virtual TRB pointer before passing it to
xhci_trb_virt_to_dma(). This produced an unaligned virtual address (e.g. ending in 0x...1).Inside
xhci_trb_virt_to_dma(), the offset calculation:
segment_offset= trb -seg->trbs;operated on this unaligned pointer, resulting in an incorrect TRB index. In wraparound cases, this caused the bounds check to fail and the function to return 0.
As a result, a
SET_DEQ_PTRcommand was issued with a DMA address of0x0, leading to controller hangs and transfer timeouts, most commonly when aborting TDs near the end of a ring segment (e.g. index 63).Fix this by translating the aligned virtual TRB pointer to a DMA address first, and only then applying the DCS bit to the resulting physical address.
Quoted 5 lines
Signed-off-by ANANDHAKRISHNAN S <[email protected]>---
Changes in v4:
- Moved the changelog below the '---' marker to prevent it from being included in the permanent git history.
Please keep the RB tag next time.
Applied, thanks.