Skip to contentSkip to conversation

usb: xhci: fix DMA address corruption in abort_td

PATCH Superseded ANANDHAKRISHNAN S 2 participants 11 messages Jan 20 – Jan 22

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

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
index 34eb4536f0e..9e0ff71710a 100644
@@ -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
#1 lore raw
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_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 +0 -0
index 34eb4536f0e..9e0ff71710a 100644

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)
#2 lore raw
In reply to 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_state is 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

#3 lore raw
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_state is 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.

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.

#4 lore raw

PATCH v2 1/1 usb: xhci: fix DMA address corruption in abort_td

In reply to 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_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
index 34eb4536f0e..229882a9dbd 100644
@@ -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
#5 lore raw
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_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 +0 -0
index 34eb4536f0e..229882a9dbd 100644

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;

#6 lore raw
In reply to ANANDHAKRISHNAN S
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

#7 lore raw

PATCH v3 1/1 usb: xhci: fix DMA address corruption in abort_td

In reply to 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_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
index 34eb4536f0e..6f5c4ec97b6 100644
@@ -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
Reviewed-by on v3 1/1
#8 lore raw
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_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).

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 to reset_ep()
- Simplify cycle bit handling to a single mask operation

---

i.e. patch changelog goes HERE

Quoted 2 lines
  drivers/usb/host/xhci-ring.c | 10 ++++------
  1 file changed, 4 insertions(+), 6 deletions(-)
#9 lore raw

PATCH v4 1/1 usb: xhci: fix DMA address corruption in abort_td

In reply to 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_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
index 34eb4536f0e..6f5c4ec97b6 100644
@@ -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
Reviewed-by on v4 1/1 Applied
#10 lore raw
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_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.

Quoted 5 lines

---
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.