All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
To: Faisal Hassan <faisal.hassan@oss.qualcomm.com>
Cc: Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Felipe Balbi <felipe.balbi@linux.intel.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v3 2/2] usb: dwc3: gadget: linearize SG requests that exceed the TRB cache
Date: Sat, 5 Dec 2026 02:37:20 +0000	[thread overview]
Message-ID: <axNzLg9xzeGbZGjY@vbox> (raw)
In-Reply-To: <20261124111321.2731905-3-faisal.hassan@oss.qualcomm.com>

On Tue, Nov 24, 2026, Faisal Hassan wrote:
> The controller prefetches the TRBs of a transfer into an internal cache
> that holds DWC_USB3_CACHE_TRBS_PER_TRANSFER entries. Databook 4.2.3.3

When referencing databook, please also mention specific IP and version
as the section may vary between versions.

> requires that the number of chained TRBs needed to construct a single
> packet never exceeds (DWC_USB3_CACHE_TRBS_PER_TRANSFER - 1), where the
> -1 accounts for the one Link TRB that may be part of the chain when a
> transfer wraps the TRB ring. The cache never holds more than 15 TRBs
> however the core was configured, so the usable budget is
> min(GHWPARAMS4[5:0] - 1, 15) data TRBs per packet.
> 
> Each scatter-gather entry of a request is queued as one TRB, so a gadget
> function driver that hands down an IN request whose entries are smaller
> than MaxPacketSize can exceed that budget with no way of knowing it. On
> a SuperSpeed bulk IN endpoint, a 1024-byte request scattered across 16
> pages of 64 bytes needs 16 chained TRBs for its single packet, one more
> than the controller can cache: the controller never assembles the
> packet, the endpoint keeps responding NRDY and the request never
> completes. The same request runs fine at High-Speed, where the 512-byte
> MaxPacketSize splits it into two packets of 8 TRBs each.
> 
> Walk the scatter-gather list of IN requests when they are queued,
> counting the TRBs each packet would be built from, and copy the request
> into a single contiguous buffer when a packet would exceed the budget.
> Doing it at queue time lets a failed allocation or a scatter-gather list
> shorter than the request length be returned to the function driver from
> usb_ep_queue(), instead of leaving a request on the pending list that can
> never be started. The original usb_request fields are restored on
> completion, so function drivers stay independent of the controller
> limitation.
> 
> The host side already applies the equivalent workaround unconditionally
> for every dwc3 instance through XHCI_SG_TRB_CACHE_SIZE_QUIRK. A request
> that trips this check cannot be transferred at all, so applying the
> workaround unconditionally can only turn a stalled endpoint into one
> extra copy. That copy is a GFP_ATOMIC allocation of the request length,
> which reaches order-4 for a 64 KiB request, but it is only reached by
> requests that would otherwise never complete.
> 
> Fixes: eeb720fb21d6 ("usb: dwc3: gadget: add support for SG lists")
> Cc: stable@vger.kernel.org
> Signed-off-by: Faisal Hassan <faisal.hassan@oss.qualcomm.com>
> ---


<snip>

>  
>  /**
>   * struct dwc3_ep - device side endpoint representation
> @@ -959,8 +961,15 @@ struct dwc3_hwparams {
>   * @trb: pointer to struct dwc3_trb
>   * @trb_dma: DMA address of @trb
>   * @num_trbs: number of TRBs used by this request
> + * @sg_trb_cache_buf: temporary linear buffer the scatter-gather list is
> + *			copied into when it exceeds the TRB cache budget
> + * @sg_trb_cache_sg: scatter-gather list replaced by @sg_trb_cache_buf
> + * @sg_trb_cache_num_sgs: number of entries in @sg_trb_cache_sg
> + * @sg_trb_cache_orig_buf: request buffer replaced by @sg_trb_cache_buf
>   * @direction: IN or OUT direction flag
>   * @mapped: true when request has been dma-mapped
> + * @sg_trb_cache_bounced: true while this request is queued through
> + *			@sg_trb_cache_buf instead of its scatter-gather list
>   */
>  struct dwc3_request {
>  	struct usb_request	request;
> @@ -986,8 +995,14 @@ struct dwc3_request {
>  
>  	unsigned int		num_trbs;
>  
> +	void			*sg_trb_cache_buf;
> +	struct scatterlist	*sg_trb_cache_sg;
> +	unsigned int		sg_trb_cache_num_sgs;
> +	void			*sg_trb_cache_orig_buf;
> +
>  	unsigned int		direction:1;
>  	unsigned int		mapped:1;
> +	unsigned int		sg_trb_cache_bounced:1;

Can we drop sg_trb_cache_buf and sg_trb_cache_bounced? While bounced,
req->request.buf is the bounce buffer, and sg_trb_cache_sg is only set
for a linearized request. For example, we can do something like this in
dwc3_gadget_restore_sg_trb_cache_buf():

	if (!req->sg_trb_cache_sg)
		return;

	kfree(req->request.buf);
	req->request.buf = req->sg_trb_cache_orig_buf;
	req->request.sg = req->sg_trb_cache_sg;
	req->request.num_sgs = req->sg_trb_cache_num_sgs;
	req->sg_trb_cache_sg = NULL;

>  };
>  
>  /*

<snip>

>  
> +/*
> + * Each scatter-gather entry is queued as one TRB, so an IN request backed by
> + * entries smaller than MaxPacketSize can need more chained TRBs to construct a
> + * single packet than the controller is able to cache. Databook 4.2.3.3 limits

Same here. Please also mention specific IP and version.

> + * the chained TRBs a single packet may be built from to
> + * (DWC_USB3_CACHE_TRBS_PER_TRANSFER - 1), where the -1 accounts for the single
> + * Link TRB the controller walks as part of the chain when a transfer wraps the
> + * TRB ring. The cache itself never holds more than DWC3_TRB_CACHE_MAX TRBs, so
> + * clamp the limit for cores built with a larger parameter.
> + *
> + * Walk the scatter-gather list counting the TRBs each packet would be built
> + * from, and report the requests that exceed that budget.
> + */
> +static bool dwc3_gadget_sg_trb_cache_limit(struct dwc3_ep *dep,
> +		struct dwc3_request *req)

Can we rename this to something like dwc3_gadget_sg_exceeds_trb_cache()
since it returns a bool?

The rest looks fine to me.

Thanks,
Thinh

  reply	other threads:[~2026-12-05  2:37 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-04  3:14 [PATCH] usb: dwc3: gadget: linearize SG requests that exceed the TRB cache Faisal Hassan
2026-10-06 17:22 ` [PATCH v2] " Faisal Hassan
2026-10-06 17:41   ` sashiko-bot
2026-10-28 19:10   ` Faisal Hassan
2026-10-29  6:20     ` Greg Kroah-Hartman
2026-10-29  9:55     ` Greg Kroah-Hartman
2026-10-30 10:30       ` Faisal Hassan
2026-11-24 11:13   ` [PATCH v3 0/2] usb: dwc3: gadget: fix request unmap and " Faisal Hassan
2026-11-24 11:13     ` [PATCH v3 1/2] usb: dwc3: gadget: fix DMA mapping leak of requests without TRBs Faisal Hassan
2026-12-05  2:09       ` Thinh Nguyen
2026-11-24 11:13     ` [PATCH v3 2/2] usb: dwc3: gadget: linearize SG requests that exceed the TRB cache Faisal Hassan
2026-12-05  2:37       ` Thinh Nguyen [this message]
2026-11-24 11:26     ` [PATCH v3 0/2] usb: dwc3: gadget: fix request unmap and " sashiko-bot

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=axNzLg9xzeGbZGjY@vbox \
    --to=thinh.nguyen@synopsys.com \
    --cc=faisal.hassan@oss.qualcomm.com \
    --cc=felipe.balbi@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.