From: David Wang <00107082@163.com>
To: mathias.nyman@intel.com, gregkh@linuxfoundation.org
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
surenb@google.com, kent.overstreet@linux.dev,
David Wang <00107082@163.com>
Subject: [RFC] USB: core/xhci: add a buffer in urb for host controller private data
Date: Mon, 12 May 2025 23:07:24 +0800 [thread overview]
Message-ID: <20250512150724.4560-1-00107082@163.com> (raw)
---
I was checking memory allocation behaviors (via memory profiling[1]),
when I notice a high frequent memory allocation in xhci_urb_enqueue, about
250/s when using a USB webcam. If those alloced buffer could be kept and
reused, lots of memory allocations could be avoid over time.
This patch is just a POC, about 0/s memory allocation in xhci with this
patch, when I use my USB devices, webcam/keyboard/mouse.
A dynamic cached memory would be better: URB keep host controller's
private data, if larger size buffer needed for private data, old buffer
released and a larger buffer alloced.
I did not observe any nagative impact with xhci's 250/s allocations
when using my system, hence no measurement of how useful this changes
can make to user. Just want to collect feedbacks before putting more
effort.
[1] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/
---
xhci keeps allocing new memory when enque a urb for private data,
and enque frequency could be high, about 250/s when using a usb
webcam, about 30/s for high pace USB keyboard/mouse usage.
Using a cache/buffer for those private data could avoid
lots memory allocations.
Signed-off-by: David Wang <00107082@163.com>
---
drivers/usb/host/xhci-ring.c | 3 ++-
drivers/usb/host/xhci.c | 14 +++++++++++---
include/linux/usb.h | 1 +
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 423bf3649570..bc350b307758 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -831,7 +831,8 @@ static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
usb_amd_quirk_pll_enable();
}
}
- xhci_urb_free_priv(urb_priv);
+ if (urb_priv != (void *)urb->hcpriv_buffer)
+ xhci_urb_free_priv(urb_priv);
usb_hcd_unlink_urb_from_ep(hcd, urb);
trace_xhci_urb_giveback(urb);
usb_hcd_giveback_urb(hcd, urb, status);
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 90eb491267b5..85aa5fe526c8 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1539,6 +1539,7 @@ static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
unsigned int *ep_state;
struct urb_priv *urb_priv;
int num_tds;
+ size_t private_size;
ep_index = xhci_get_endpoint_index(&urb->ep->desc);
@@ -1552,7 +1553,13 @@ static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
else
num_tds = 1;
- urb_priv = kzalloc(struct_size(urb_priv, td, num_tds), mem_flags);
+ private_size = struct_size(urb_priv, td, num_tds);
+ if (private_size <= sizeof(urb->hcpriv_buffer)) {
+ memset(urb->hcpriv_buffer, 0, sizeof(urb->hcpriv_buffer));
+ urb_priv = (struct urb_priv *)urb->hcpriv_buffer;
+ } else {
+ urb_priv = kzalloc(private_size, mem_flags);
+ }
if (!urb_priv)
return -ENOMEM;
@@ -1626,7 +1633,8 @@ static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
if (ret) {
free_priv:
- xhci_urb_free_priv(urb_priv);
+ if (urb_priv != (void *)urb->hcpriv_buffer)
+ xhci_urb_free_priv(urb_priv);
urb->hcpriv = NULL;
}
spin_unlock_irqrestore(&xhci->lock, flags);
@@ -1789,7 +1797,7 @@ static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
return ret;
err_giveback:
- if (urb_priv)
+ if (urb_priv && urb_priv != (void *)urb->hcpriv_buffer)
xhci_urb_free_priv(urb_priv);
usb_hcd_unlink_urb_from_ep(hcd, urb);
spin_unlock_irqrestore(&xhci->lock, flags);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index b46738701f8d..4f82bb69081c 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1602,6 +1602,7 @@ struct urb {
struct kref kref; /* reference count of the URB */
int unlinked; /* unlink error code */
void *hcpriv; /* private data for host controller */
+ u8 hcpriv_buffer[4096]; /* small buffer if private data can fit */
atomic_t use_count; /* concurrent submissions counter */
atomic_t reject; /* submissions will fail */
--
2.39.2
next reply other threads:[~2025-05-12 15:08 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-12 15:07 David Wang [this message]
2025-05-12 15:34 ` [RFC] USB: core/xhci: add a buffer in urb for host controller private data Alan Stern
2025-05-12 16:19 ` David Wang
2025-05-13 5:54 ` [PATCH 1/2] USB: core: add a memory pool to urb for host-controller " David Wang
2025-05-13 8:11 ` Oliver Neukum
2025-05-13 8:23 ` David Wang
2025-05-13 8:46 ` Oliver Neukum
2025-05-13 8:53 ` David Wang
2025-05-13 9:49 ` David Wang
2025-05-13 11:02 ` Oliver Neukum
2025-05-13 11:12 ` David Wang
2025-05-13 5:55 ` [PATCH 2/2] USB: xhci: use urb hcpriv mempool for " David Wang
2025-05-13 8:21 ` Oliver Neukum
2025-05-13 8:31 ` David Wang
2025-05-13 9:00 ` Oliver Neukum
2025-05-13 9:27 ` [RFC] USB: core/xhci: add a buffer in urb for host controller " Mathias Nyman
2025-05-13 9:41 ` David Wang
2025-05-13 11:38 ` [PATCH v2 1/2] USB: core: add a memory pool to urb for host-controller " David Wang
2025-05-13 14:25 ` Alan Stern
2025-05-13 14:41 ` David Wang
2025-05-13 15:37 ` Alan Stern
2025-05-13 16:35 ` David Wang
2025-05-13 18:21 ` Alan Stern
2025-05-13 18:48 ` David Wang
2025-05-13 19:46 ` Alan Stern
2025-05-14 11:27 ` Oliver Neukum
2025-05-14 6:44 ` David Wang
2025-05-14 7:29 ` Greg KH
2025-05-14 8:50 ` David Wang
2025-05-14 9:34 ` Oliver Neukum
2025-05-17 9:09 ` David Wang
2025-05-14 11:23 ` Oliver Neukum
2025-05-14 11:51 ` David Wang
2025-05-14 12:03 ` Oliver Neukum
2025-05-14 12:14 ` David Wang
2025-05-16 17:13 ` David Wang
2025-05-13 11:38 ` [PATCH v2 2/2] USB: xhci: use urb hcpriv mempool for " David Wang
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=20250512150724.4560-1-00107082@163.com \
--to=00107082@163.com \
--cc=gregkh@linuxfoundation.org \
--cc=kent.overstreet@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@intel.com \
--cc=surenb@google.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