From patchwork Tue May 13 11:38:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wang <00107082@163.com> X-Patchwork-Id: 889847 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 342D928CF7E; Tue, 13 May 2025 11:39:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747136365; cv=none; b=hOyFJAELjcClF/AxwZrOvckn6b9on4psCTg6dB99XES7y1xJkyo/31rbB7mPs9mlawBjDB18gtFSCS9wSy8HDzjYk29F7qGyAlLQN/7IakqaY+lW6MgNq0rreBij2VC788iwCPiaYB6CSw9EDRsVwsJtXNh0JEV1EjKfcBEG9yY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747136365; c=relaxed/simple; bh=sakjGWFrGCsJtJMuNHZvf4IXAuWs0DjW6D7LHP/RS9w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lNgoJz5J9+P+WStUmtyXr3yucwbj6NeCOBCbJGaUioeBp6pfHqG1arjKfAZkoq+EO47yLB3MQ+9NQQ39uY4yptVN04ld6AJVWKIOv8yqNLzWaYNKIdsw8cIj2TphRsb46YrgEqvS7pD1VU3no43zt3Ah9i5NAmhSkgJWynqbbT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=mvFAUHeS; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="mvFAUHeS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=D1 d5Mv3O2B4ADycUafrn1h0QXPGeO+8J5MjFluopYfY=; b=mvFAUHeS0wKcgzl+Nl 27I669SfkEft4YOkU/UNmLgMkZCHW+bdhAcAoncXE23BLkCtEmZ2E82cqfgvbrR1 ZlI/uT76icnLb4PINi7RHsPTk9bfHM85n47UDvjXXrkMiY7AtzRVP7bLB7WYcG+W +az5lsHRHCWlgdMpgMDnAgH90= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wC3kuMqLyNotfVmBA--.12824S4; Tue, 13 May 2025 19:38:33 +0800 (CST) From: David Wang <00107082@163.com> To: mathias.nyman@intel.com, gregkh@linuxfoundation.org, oneukum@suse.com Cc: stern@rowland.harvard.edu, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, David Wang <00107082@163.com> Subject: [PATCH v2 1/2] USB: core: add a memory pool to urb for host-controller private data Date: Tue, 13 May 2025 19:38:17 +0800 Message-Id: <20250513113817.11962-1-00107082@163.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250512150724.4560-1-00107082@163.com> References: <20250512150724.4560-1-00107082@163.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: _____wC3kuMqLyNotfVmBA--.12824S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxXF17CFWxCw1DJF18Cw17ZFb_yoW5tFWUpF 4xA3s3tF1FqrW3XrZ3Gan7uayrJ3Wv9ryjkryfu345Xr17tw18A3Z2yFyrCr9xJr4Syw4Y qFsIgan8Ww1UAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pEF1v7UUUUU= X-CM-SenderInfo: qqqrilqqysqiywtou0bp/xtbB0hxMqmgjJyLldgAAs1 --- Changes: 1. Use caller's mem_flags if a larger memory is needed. Thanks to Oliver Neukum 's review. --- 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 and above. 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 mempool only holds one block of memory and grows when larger size is requested. Signed-off-by: David Wang <00107082@163.com> --- drivers/usb/core/urb.c | 23 +++++++++++++++++++++++ include/linux/usb.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c index 5e52a35486af..51bf25125aeb 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); } @@ -1037,3 +1038,25 @@ 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 + * @mem_flags: the type of memory to allocate + * + * Return: NULL if out of memory, otherwise memory are zeroed + */ +void *urb_hcpriv_mempool_zalloc(struct urb *urb, size_t size, gfp_t mem_flags) +{ + if (urb->hcpriv_mempool_size < size) { + kfree(urb->hcpriv_mempool); + urb->hcpriv_mempool_size = size; + urb->hcpriv_mempool = kmalloc(size, mem_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..4400e41271bc 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -1602,6 +1602,8 @@ 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; /* a single slot of cache for HCD's private data */ + size_t hcpriv_mempool_size; /* current size of the memory pool */ atomic_t use_count; /* concurrent submissions counter */ atomic_t reject; /* submissions will fail */ @@ -1790,6 +1792,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, gfp_t mem_flags); #define usb_unblock_urb usb_unpoison_urb From patchwork Tue May 13 05:55:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wang <00107082@163.com> X-Patchwork-Id: 889849 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F1321DEFE7; Tue, 13 May 2025 05:56:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747115785; cv=none; b=fuZhb5XW/9Odug3CZg5yC5H4HlD0C4jPQYz1/gCHdU8FYfxN0+n4clDC8BSnzzhwwRb3Bw2sTdWco42CYNJcaVNzY52uKhqTb+n8p97tEz3ioNZss/gub0w78TeRdsq8OmjjBdfgxlXPsk5lWnR7okaAmdy2X1Isu+EjDBkxi7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747115785; c=relaxed/simple; bh=p6w275A94nytvklausbAYSIp4RX8gBNakpOf+CKSRjw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FBqBE8Prj1RGyBcpQjX3YXoxP6nblZf92cIESrBk5ZLLjJ+wd/IKWZlTHweEoO6Od8ZyfcjCDzHC//ECoBUWG8iQdyi2vixIb4d5vCdtUonRU/6LaGFIBK+PUu5CPZRe9HV8b4+qtAiTBrsu/WZktGmrk4yLbWy8gwh7TcmoQ9k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=NyhffNw2; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="NyhffNw2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=+k /VGJTQ7VpQV+dYkPSM1DAaHCORay8ZMGNM7qiLKr4=; b=NyhffNw2hT0vC/Y7OC 1M8Tpnlzg6PAJ10KD+S3ymC4yHvG+8MXN3oiKLAhNZu2jsfD2c8PuviVxVL2NZLD /YTaijHubh7jFX5YHSHPXknZg88hpI4LCO+7qpRSiP1VsAeaAx4ID89nBmIdpaM0 rMJvDRow9tzmWhWQQ7b2CSHNg= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-2 (Coremail) with SMTP id _____wBHPnXi3iJoD9BIBA--.18930S4; Tue, 13 May 2025 13:55:54 +0800 (CST) From: David Wang <00107082@163.com> To: mathias.nyman@intel.com, gregkh@linuxfoundation.org Cc: stern@rowland.harvard.edu, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, David Wang <00107082@163.com> Subject: [PATCH 2/2] USB: xhci: use urb hcpriv mempool for private data Date: Tue, 13 May 2025 13:55:45 +0800 Message-Id: <20250513055545.5738-1-00107082@163.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250512150724.4560-1-00107082@163.com> References: <20250512150724.4560-1-00107082@163.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: _____wBHPnXi3iJoD9BIBA--.18930S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7KFWUGF4rtw13uryxCry7Awb_yoW8Zw4UpF WrWa40kr1Fyr42qFW5Jw1DA3WfJw4kWFyDWFWxG3yUur42k3srG34IyFWF9FnaqrykCrsI q3W8X3y8GF1DKFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0p__-BDUUUUU= X-CM-SenderInfo: qqqrilqqysqiywtou0bp/1tbiqBlMqmgi2uV4+gAAsg xhci keeps alloc/free private data for each enqueue/dequeue cycles, when using a USB webcam, the memory allocation frequency could reach about 1k/s. URB objects have longer lifecycle than private data, hand over ownership of private data to urb can save lots of memory allocations over time. Signed-off-by: David Wang <00107082@163.com> --- drivers/usb/host/xhci-ring.c | 1 - drivers/usb/host/xhci.c | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 423bf3649570..b98e5211693d 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -831,7 +831,6 @@ static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, usb_amd_quirk_pll_enable(); } } - 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..94ff5fa9dcf2 100644 --- a/drivers/usb/host/xhci.c +++ b/drivers/usb/host/xhci.c @@ -1552,7 +1552,7 @@ 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); + urb_priv = urb_hcpriv_mempool_zalloc(urb, struct_size(urb_priv, td, num_tds)); if (!urb_priv) return -ENOMEM; @@ -1626,7 +1626,6 @@ 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); urb->hcpriv = NULL; } spin_unlock_irqrestore(&xhci->lock, flags); @@ -1789,8 +1788,6 @@ static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) return ret; err_giveback: - if (urb_priv) - xhci_urb_free_priv(urb_priv); usb_hcd_unlink_urb_from_ep(hcd, urb); spin_unlock_irqrestore(&xhci->lock, flags); usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);