* [PATCH 0/2] Fix sum_trb_lengths()
@ 2024-09-10 14:01 Michal Pecio
2024-09-10 14:02 ` [PATCH 1/2] usb: xhci: Remove unused parameters of next_trb() Michal Pecio
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Michal Pecio @ 2024-09-10 14:01 UTC (permalink / raw)
To: Mathias Nyman; +Cc: linux-usb
Hi,
This function sums from ring->dequeue instead of td->first_trb, and
this is even documented. Makes no sense and doesn't work right.
I searched xhci-ring.c and this appears to be the last such abuse of
'dequeue' still surviving to this day.
Regards,
Michal
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] usb: xhci: Remove unused parameters of next_trb()
2024-09-10 14:01 [PATCH 0/2] Fix sum_trb_lengths() Michal Pecio
@ 2024-09-10 14:02 ` Michal Pecio
2024-09-10 14:03 ` [PATCH 2/2] usb: xhci: Fix sum_trb_lengths() Michal Pecio
2024-09-11 15:48 ` [PATCH 0/2] " Mathias Nyman
2 siblings, 0 replies; 4+ messages in thread
From: Michal Pecio @ 2024-09-10 14:02 UTC (permalink / raw)
To: Mathias Nyman; +Cc: linux-usb
The function has two parameters which it doesn't use and hasn't ever
used. One caller even puts NULL there, knowing it will work anyway.
Signed-off-by: Michal Pecio <michal.pecio@gmail.com>
---
drivers/usb/host/xhci-ring.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 4ea2c3e072a9..a3ffadf8e400 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -145,10 +145,8 @@ static void trb_to_noop(union xhci_trb *trb, u32 noop_type)
* TRB is in a new segment. This does not skip over link TRBs, and it does not
* effect the ring dequeue or enqueue pointers.
*/
-static void next_trb(struct xhci_hcd *xhci,
- struct xhci_ring *ring,
- struct xhci_segment **seg,
- union xhci_trb **trb)
+static void next_trb(struct xhci_segment **seg,
+ union xhci_trb **trb)
{
if (trb_is_link(*trb) || last_trb_on_seg(*seg, *trb)) {
*seg = (*seg)->next;
@@ -446,9 +444,9 @@ static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags)
* avoiding corrupting the command ring pointer in case the command ring
* is stopped by the time the upper dword is written.
*/
- next_trb(xhci, NULL, &new_seg, &new_deq);
+ next_trb(&new_seg, &new_deq);
if (trb_is_link(new_deq))
- next_trb(xhci, NULL, &new_seg, &new_deq);
+ next_trb(&new_seg, &new_deq);
crcr = xhci_trb_virt_to_dma(new_seg, new_deq);
xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring);
@@ -678,7 +676,7 @@ static int xhci_move_dequeue_past_td(struct xhci_hcd *xhci,
link_trb_toggles_cycle(new_deq))
new_cycle ^= 0x1;
- next_trb(xhci, ep_ring, &new_seg, &new_deq);
+ next_trb(&new_seg, &new_deq);
/* Search wrapped around, bail out */
if (new_deq == ep->ring->dequeue) {
@@ -756,7 +754,7 @@ static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
if (trb == td->last_trb)
break;
- next_trb(xhci, ep_ring, &seg, &trb);
+ next_trb(&seg, &trb);
}
}
@@ -2259,7 +2257,7 @@ static int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring,
union xhci_trb *trb = ring->dequeue;
struct xhci_segment *seg = ring->deq_seg;
- for (sum = 0; trb != stop_trb; next_trb(xhci, ring, &seg, &trb)) {
+ for (sum = 0; trb != stop_trb; next_trb(&seg, &trb)) {
if (!trb_is_noop(trb) && !trb_is_link(trb))
sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] usb: xhci: Fix sum_trb_lengths()
2024-09-10 14:01 [PATCH 0/2] Fix sum_trb_lengths() Michal Pecio
2024-09-10 14:02 ` [PATCH 1/2] usb: xhci: Remove unused parameters of next_trb() Michal Pecio
@ 2024-09-10 14:03 ` Michal Pecio
2024-09-11 15:48 ` [PATCH 0/2] " Mathias Nyman
2 siblings, 0 replies; 4+ messages in thread
From: Michal Pecio @ 2024-09-10 14:03 UTC (permalink / raw)
To: Mathias Nyman; +Cc: linux-usb
This function is supposed to sum the lengths of all transfer TRBs in
a TD up to a point, but it starts summing at the current dequeue since
it only ever gets called on the first pending TD.
This won't work when there are cancelled TDs at the beginning of the
ring. The function tries to exclude No-Ops from the count, but not all
cancelled TDs are No-Op'ed - not those the HW stopped on.
The absolutely obvious fix is to start counting at the TD's first TRB.
And remove the now-useless 'ring' parameter, and 'xhci' too.
Signed-off-by: Michal Pecio <michal.pecio@gmail.com>
---
drivers/usb/host/xhci-ring.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a3ffadf8e400..e5c5cf4d38c6 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -2249,13 +2249,12 @@ static int finish_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
return xhci_td_cleanup(xhci, td, ep_ring, td->status);
}
-/* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */
-static int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring,
- union xhci_trb *stop_trb)
+/* sum trb lengths from the first trb up to stop_trb, _excluding_ stop_trb */
+static u32 sum_trb_lengths(struct xhci_td *td, union xhci_trb *stop_trb)
{
u32 sum;
- union xhci_trb *trb = ring->dequeue;
- struct xhci_segment *seg = ring->deq_seg;
+ union xhci_trb *trb = td->first_trb;
+ struct xhci_segment *seg = td->start_seg;
for (sum = 0; trb != stop_trb; next_trb(&seg, &trb)) {
if (!trb_is_noop(trb) && !trb_is_link(trb))
@@ -2446,7 +2445,7 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
goto finish_td;
if (sum_trbs_for_length)
- frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) +
+ frame->actual_length = sum_trb_lengths(td, ep_trb) +
ep_trb_len - remaining;
else
frame->actual_length = requested;
@@ -2529,7 +2528,7 @@ static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
goto finish_td;
case COMP_STOPPED_LENGTH_INVALID:
/* stopped on ep trb with invalid length, exclude it */
- td->urb->actual_length = sum_trb_lengths(xhci, ep_ring, ep_trb);
+ td->urb->actual_length = sum_trb_lengths(td, ep_trb);
goto finish_td;
case COMP_USB_TRANSACTION_ERROR:
if (xhci->quirks & XHCI_NO_SOFT_RETRY ||
@@ -2550,7 +2549,7 @@ static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
td->urb->actual_length = requested - remaining;
else
td->urb->actual_length =
- sum_trb_lengths(xhci, ep_ring, ep_trb) +
+ sum_trb_lengths(td, ep_trb) +
ep_trb_len - remaining;
finish_td:
if (remaining > requested) {
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] Fix sum_trb_lengths()
2024-09-10 14:01 [PATCH 0/2] Fix sum_trb_lengths() Michal Pecio
2024-09-10 14:02 ` [PATCH 1/2] usb: xhci: Remove unused parameters of next_trb() Michal Pecio
2024-09-10 14:03 ` [PATCH 2/2] usb: xhci: Fix sum_trb_lengths() Michal Pecio
@ 2024-09-11 15:48 ` Mathias Nyman
2 siblings, 0 replies; 4+ messages in thread
From: Mathias Nyman @ 2024-09-11 15:48 UTC (permalink / raw)
To: Michal Pecio, Mathias Nyman; +Cc: linux-usb
On 10.9.2024 17.01, Michal Pecio wrote:
> Hi,
>
> This function sums from ring->dequeue instead of td->first_trb, and
> this is even documented. Makes no sense and doesn't work right.
>
> I searched xhci-ring.c and this appears to be the last such abuse of
> 'dequeue' still surviving to this day.
>
> Regards,
> Michal
>
Thanks, both patches added to queue
-Mathias
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-09-11 15:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-10 14:01 [PATCH 0/2] Fix sum_trb_lengths() Michal Pecio
2024-09-10 14:02 ` [PATCH 1/2] usb: xhci: Remove unused parameters of next_trb() Michal Pecio
2024-09-10 14:03 ` [PATCH 2/2] usb: xhci: Fix sum_trb_lengths() Michal Pecio
2024-09-11 15:48 ` [PATCH 0/2] " Mathias Nyman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).