public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Mathias Nyman <mathias.nyman@linux.intel.com>
To: "Michał Pecio" <michal.pecio@gmail.com>, niklas.neronin@linux.intel.com
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 3/4] usb: xhci: rework and simplify trb_in_td()
Date: Wed, 19 Feb 2025 16:25:31 +0200	[thread overview]
Message-ID: <fc3be972-6e28-4942-b37d-4f2e39bb866b@linux.intel.com> (raw)
In-Reply-To: <20250219095637.5bd6e9e4@foxbook>

On 19.2.2025 10.56, Michał Pecio wrote:
> Hi,
> 
>> +		/* Edge case, the TD wrapped around to the start segment. */
>> +		if (xhci_trb_virt_to_dma(td->end_seg, td->end_trb) < dma &&
>> +		    dma < xhci_trb_virt_to_dma(td->start_seg, td->start_trb))
>> +			return NULL;
>> +		if (seg->dma <= dma && dma <= (seg->dma + TRB_SEGMENT_SIZE))
> 
> It should be strict inequality for the upper bound here.
> 
> Note that this wraparound case souldn't be happening (the driver avoids
> moving enqueue into deq_seg to simplify ring expansion) so no amount of
> testing will catch problems here, until maybe something changes one day.
> 
>> +			return seg;
>> +		seg = seg->next;
>> +	}
> 
> The situation is tricky now, because we are either in start_seg and
> end_seg is elsewhere or in start_seg->next and wraparound. But it looks
> like the loop below will work OK for either case.
> 
>> +	/* Loop through segment which don't contain the DMA address. */
>> +	while (dma < seg->dma || (seg->dma + TRB_SEGMENT_SIZE) <= dma) {
> 
> This condition looks like it could use the in_range() macro.
> 
>> +		if (seg == td->end_seg)
>> +			return NULL;
>> +
>> +		seg = seg->next;
>> +		if (seg == td->start_seg)
>> +			return NULL;
> 
> I suppose this only happens if end_seg is not on the ring, fair enough.
> 
>> +	}
> 
> Maybe a comment here? Something like:

> 
> * At this point seg contains the dma and either:
> * a. start_seg != end_seg and seg can be anywhere
> * b. start_seg == end_seg in wraparound case and seg != start_seg

Agreed, a comment here would help.

> 
>> +	if (seg == td->start_seg) {
>> +		if (dma < xhci_trb_virt_to_dma(td->start_seg, td->start_trb))
>> +			return NULL;
>> +	} else if (seg == td->end_seg) {
>> +		if (xhci_trb_virt_to_dma(td->end_seg, td->end_trb) < dma)
>> +			return NULL;
>> +	}
>> +	return seg;
> 
> This should be corrent, but it's not something immediately obvious.
> 
> Not sure if this new implementation is really simpler than the old one.
> I wonder if it wouldn't make sense to reorder this after the API change
> (patch 4/4) to allow emergency revert if something unexpected shows up.

Had to draw several cases on paper to go through this new version.
But I might just be used to the old one

> 
> As for efficiency, those virt_to_dma translations aren't exactly free
> and there are two. Maybe it could be faster to translate dma to virt
> once and then compare. Sometimes also sizeof(*) < sizeof(dma_addr_t).

Agreed

dma_addr_t start_dma = xhci_trb_virt_to_dma(td->start_seg, td->start_trb);
dma_addr_t end_dma = xhci_trb_virt_to_dma(td->end_seg, td->end_trb);

comparisons will then be a lot easier to read with start_dma and end_dma

-Mathias


  reply	other threads:[~2025-02-19 14:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-06 10:34 [PATCH 0/4] usb: xhci: improve trb_in_td() Niklas Neronin
2025-02-06 10:34 ` [PATCH 1/4] usb: xhci: refactor trb_in_td() to be static Niklas Neronin
2025-02-06 10:34 ` [PATCH 2/4] usb: xhci: move debug capabilities from trb_in_td() to handle_tx_event() Niklas Neronin
2025-03-05  8:46   ` Michał Pecio
2025-03-05  9:17     ` Neronin, Niklas
2025-02-06 10:34 ` [PATCH 3/4] usb: xhci: rework and simplify trb_in_td() Niklas Neronin
2025-02-19  8:56   ` Michał Pecio
2025-02-19 14:25     ` Mathias Nyman [this message]
2025-02-20 12:14     ` [PATCH 5/4 RFC] An alternative dma_in_range() implementation Michał Pecio
2025-02-20 13:18       ` Neronin, Niklas
2025-02-20 12:25     ` [PATCH 3/4] usb: xhci: rework and simplify trb_in_td() Neronin, Niklas
2025-02-06 10:34 ` [PATCH 4/4] usb: xhci: modify trb_in_td() to be more modular Niklas Neronin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fc3be972-6e28-4942-b37d-4f2e39bb866b@linux.intel.com \
    --to=mathias.nyman@linux.intel.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=michal.pecio@gmail.com \
    --cc=niklas.neronin@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox