From: David Wang <00107082@163.com>
To: mathias.nyman@intel.com, gregkh@linuxfoundation.org
Cc: stern@rowland.harvard.edu, surenb@google.com,
kent.overstreet@linux.dev, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org, David Wang <00107082@163.com>
Subject: [PATCH 1/2] USB: core: add a memory pool to urb for host-controller private data
Date: Tue, 13 May 2025 13:54:47 +0800 [thread overview]
Message-ID: <20250513055447.5696-1-00107082@163.com> (raw)
In-Reply-To: <20250512150724.4560-1-00107082@163.com>
URB objects have long lifecycle, an urb can be reused between
enqueue-dequeue loops; The private data needed by some host controller
has very short lifecycle, the memory is alloced when enqueue, and
released when dequeue. For example, on a system with xhci, several
minutes of usage of webcam/keyboard/mouse have memory alloc counts:
drivers/usb/core/urb.c:75 [usbcore] func:usb_alloc_urb 661
drivers/usb/host/xhci.c:1555 [xhci_hcd] func:xhci_urb_enqueue 424863
Memory allocation frequency for host-controller private data can reach
~1k/s.
High frequent allocations for host-controller private data can be
avoided if urb take over the ownership of memory, the memory then shares
the longer lifecycle with urb objects.
Add a mempool to urb for hcpriv usage, the memory inherits the memory
flags from urb object itself, and will grow if larger size is requested.
Signed-off-by: David Wang <00107082@163.com>
---
drivers/usb/core/urb.c | 23 +++++++++++++++++++++++
include/linux/usb.h | 4 ++++
2 files changed, 27 insertions(+)
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 5e52a35486af..7c13b971b435 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -23,6 +23,7 @@ static void urb_destroy(struct kref *kref)
if (urb->transfer_flags & URB_FREE_BUFFER)
kfree(urb->transfer_buffer);
+ kfree(urb->hcpriv_mempool);
kfree(urb);
}
@@ -77,6 +78,7 @@ struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags)
if (!urb)
return NULL;
usb_init_urb(urb);
+ urb->hcpriv_mempool_flags = mem_flags;
return urb;
}
EXPORT_SYMBOL_GPL(usb_alloc_urb);
@@ -1037,3 +1039,24 @@ int usb_anchor_empty(struct usb_anchor *anchor)
EXPORT_SYMBOL_GPL(usb_anchor_empty);
+/**
+ * urb_hcpriv_mempool_zalloc - alloc memory from mempool for hcpriv
+ * @urb: pointer to URB being used
+ * @size: memory size requested by current host controller
+ *
+ * Return: NULL if out of memory, otherwise memory are zeroed
+ */
+void *urb_hcpriv_mempool_zalloc(struct urb *urb, size_t size)
+{
+ if (urb->hcpriv_mempool_size < size) {
+ kfree(urb->hcpriv_mempool);
+ urb->hcpriv_mempool_size = size;
+ urb->hcpriv_mempool = kmalloc(size, urb->hcpriv_mempool_flags);
+ }
+ if (urb->hcpriv_mempool)
+ memset(urb->hcpriv_mempool, 0, size);
+ else
+ urb->hcpriv_mempool_size = 0;
+ return urb->hcpriv_mempool;
+}
+EXPORT_SYMBOL_GPL(urb_hcpriv_mempool_zalloc);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index b46738701f8d..a31535f3b2bc 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1602,6 +1602,9 @@ struct urb {
struct kref kref; /* reference count of the URB */
int unlinked; /* unlink error code */
void *hcpriv; /* private data for host controller */
+ void *hcpriv_mempool; /* cache for host controller's private data */
+ size_t hcpriv_mempool_size; /* current size of the memory pool */
+ gfp_t hcpriv_mempool_flags; /* mem flags for memory pool */
atomic_t use_count; /* concurrent submissions counter */
atomic_t reject; /* submissions will fail */
@@ -1790,6 +1793,7 @@ extern int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor,
extern struct urb *usb_get_from_anchor(struct usb_anchor *anchor);
extern void usb_scuttle_anchored_urbs(struct usb_anchor *anchor);
extern int usb_anchor_empty(struct usb_anchor *anchor);
+extern void *urb_hcpriv_mempool_zalloc(struct urb *urb, size_t size);
#define usb_unblock_urb usb_unpoison_urb
--
2.39.2
next prev parent reply other threads:[~2025-05-13 5:56 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-12 15:07 [RFC] USB: core/xhci: add a buffer in urb for host controller " David Wang
2025-05-12 15:34 ` Alan Stern
2025-05-12 16:19 ` David Wang
2025-05-13 5:54 ` David Wang [this message]
2025-05-13 8:11 ` [PATCH 1/2] USB: core: add a memory pool to urb for host-controller " 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=20250513055447.5696-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=stern@rowland.harvard.edu \
--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
all inboxes | Powered by JetHome®