[PATCH] usb: xhci: Use common error handling code in two functions

Markus Elfring <[email protected]> Wed, 10 Jun 2026 14:20:11 +0200
Newsgroups org.kernel.vger.kernel-janitors,org.kernel.vger.linux-kernel,org.kernel.vger.linux-usb
Message-ID <[email protected]>
From: Markus Elfring <[email protected]>
Date: Wed, 10 Jun 2026 14:10:24 +0200

Use additional labels so that a bit of exception handling can be better
reused at the end of two function implementations.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
 drivers/usb/host/xhci-mem.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 997fe90f54e5..0e851d0c5bea 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -40,18 +40,15 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
 		return NULL;
 
 	seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma);
-	if (!seg->trbs) {
-		kfree(seg);
-		return NULL;
-	}
+	if (!seg->trbs)
+		goto free_seg;
 
 	if (max_packet) {
 		seg->bounce_buf = kzalloc_node(max_packet, flags,
 					dev_to_node(dev));
 		if (!seg->bounce_buf) {
 			dma_pool_free(xhci->segment_pool, seg->trbs, dma);
-			kfree(seg);
-			return NULL;
+			goto free_seg;
 		}
 	}
 	seg->num = num;
@@ -59,6 +56,10 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
 	seg->next = NULL;
 
 	return seg;
+
+free_seg:
+	kfree(seg);
+	return NULL;
 }
 
 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
@@ -2309,19 +2310,21 @@ xhci_alloc_interrupter(struct xhci_hcd *xhci, unsigned int segs, gfp_t flags)
 	ir->event_ring = xhci_ring_alloc(xhci, segs, TYPE_EVENT, 0, flags);
 	if (!ir->event_ring) {
 		xhci_warn(xhci, "Failed to allocate interrupter event ring\n");
-		kfree(ir);
-		return NULL;
+		goto free_ir;
 	}
 
 	ret = xhci_alloc_erst(xhci, ir->event_ring, &ir->erst, flags);
 	if (ret) {
 		xhci_warn(xhci, "Failed to allocate interrupter erst\n");
 		xhci_ring_free(xhci, ir->event_ring);
-		kfree(ir);
-		return NULL;
+		goto free_ir;
 	}
 
 	return ir;
+
+free_ir:
+	kfree(ir);
+	return NULL;
 }
 
 void xhci_add_interrupter(struct xhci_hcd *xhci, unsigned int intr_num)
-- 
2.54.0