[RFC PATCH] xhci: Use Cached ring during endpoint ring allocation

From: Anurag Kumar Vulisha
Date: Wed Feb 22 2017 - 10:06:14 EST


Currently during endpoint initialization, a new endpoint ring is alloacte
using xhci_ring_alloc(), if this function fails to allocate ring a cached
ring(if available) is assigned to endpoint ring.
This patch modifies the code that during endpoint initialization, if cached
ring is available it is assigned to the endpoint ring. If cached rings are
not available then xhci_ring_alloc() is called to allocate a new ring.
Doing so will avoid unncessary memory allocations if cached ring is already
available for use. This also fixes endpoint "Ring expansion failed" error
which occurs due to insufficient memory during ring expansion.

Signed-off-by: Anurag Kumar Vulisha <anuragku@xxxxxxxxxx>
---
drivers/usb/host/xhci-mem.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 8414ed2..587b6c6 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -1514,19 +1514,22 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
mult = 0;

/* Set up the endpoint ring */
- virt_dev->eps[ep_index].new_ring =
- xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags);
- if (!virt_dev->eps[ep_index].new_ring) {
+ if (virt_dev->num_rings_cached > 0) {
/* Attempt to use the ring cache */
- if (virt_dev->num_rings_cached == 0)
- return -ENOMEM;
virt_dev->num_rings_cached--;
virt_dev->eps[ep_index].new_ring =
virt_dev->ring_cache[virt_dev->num_rings_cached];
virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring,
1, ring_type);
+ } else {
+ virt_dev->eps[ep_index].new_ring =
+ xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet,
+ mem_flags);
+ if (!virt_dev->eps[ep_index].new_ring)
+ return -ENOMEM;
}
+
virt_dev->eps[ep_index].skip = false;
ep_ring = virt_dev->eps[ep_index].new_ring;

--
2.1.1