Linux USB
 help / color / mirror / Atom feed
From: Niklas Neronin <niklas.neronin@linux.intel.com>
To: mathias.nyman@linux.intel.com
Cc: linux-usb@vger.kernel.org,
	Niklas Neronin <niklas.neronin@linux.intel.com>
Subject: [PATCH 6/7] usb: xhci: remove pre-allocated free streams command
Date: Tue,  1 Dec 2026 20:41:33 +0100	[thread overview]
Message-ID: <20261201194134.265480-7-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20261201194134.265480-1-niklas.neronin@linux.intel.com>

Most xHCI commands are allocated immediately before being queued and freed
after completion. The free streams path is an exception, keeping a command
structure pre-allocated in struct 'xhci_stream_info' solely for later use
by xhci_free_streams().

This is a leftover from older command handling. Similar pre-allocated
commands have already been removed elsewhere, leaving the free streams
command as the only remaining user of this pattern.

Allocate the command when xhci_free_streams() is invoked and free it
after use. This removes a special case from stream management and makes
command lifetime handling consistent with the rest of the driver.

As a result, xhci_free_streams() can now fail with -ENOMEM if the command
allocation fails. This is the same failure mode as other paths that
issue commands, and the function could already fail for other reasons
(e.g. command queueing or completion errors), so callers already handle
error return. In that case the endpoints are left untouched and keep
their streams.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c |  9 ---------
 drivers/usb/host/xhci.c     | 15 +++++++--------
 drivers/usb/host/xhci.h     |  1 -
 3 files changed, 7 insertions(+), 18 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 3ccc9f7404b7..78eb8358ea29 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -645,12 +645,6 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
 	if (!stream_info->stream_ctx_array)
 		goto cleanup_ring_array;
 
-	/* Allocate everything needed to free the stream rings later */
-	stream_info->free_streams_command =
-		xhci_alloc_command_with_ctx(xhci, true, mem_flags);
-	if (!stream_info->free_streams_command)
-		goto cleanup_ctx;
-
 	INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
 
 	/* Allocate rings for all the streams that the driver will use,
@@ -702,8 +696,6 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
 			stream_info->stream_rings[cur_stream] = NULL;
 		}
 	}
-	xhci_free_command(xhci, stream_info->free_streams_command);
-cleanup_ctx:
 	xhci_free_stream_ctx(xhci,
 		stream_info->num_stream_ctxs,
 		stream_info->stream_ctx_array,
@@ -774,7 +766,6 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
 			stream_info->stream_rings[cur_stream] = NULL;
 		}
 	}
-	xhci_free_command(xhci, stream_info->free_streams_command);
 	xhci->cmd_ring_reserved_trbs--;
 	if (stream_info->stream_ctx_array)
 		xhci_free_stream_ctx(xhci,
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 841ef59b83f6..f419b4246c64 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -3732,23 +3732,20 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 	xhci = hcd_to_xhci(hcd);
 	vdev = xhci->devs[udev->slot_id];
 
+	command = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
+	if (!command)
+		return -ENOMEM;
+
 	/* Set up a configure endpoint command to remove the streams rings */
 	spin_lock_irqsave(&xhci->lock, flags);
 	changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
 			udev, eps, num_eps);
 	if (changed_ep_bitmask == 0) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
+		xhci_free_command(xhci, command);
 		return -EINVAL;
 	}
 
-	/* Use the xhci_command structure from the first endpoint.  We may have
-	 * allocated too many, but the driver may call xhci_free_streams() for
-	 * each endpoint it grouped into one call to xhci_alloc_streams().
-	 */
-	ep_index = xhci_get_endpoint_index(&eps[0]->desc);
-	command = vdev->eps[ep_index].stream_info->free_streams_command;
-	ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
-
 	for (i = 0; i < num_eps; i++) {
 		struct xhci_ep_ctx *ep_ctx;
 
@@ -3761,6 +3758,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 		xhci_setup_no_streams_ep_input_ctx(ep_ctx,
 				&vdev->eps[ep_index]);
 	}
+	ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
 	xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
 			vdev->out_ctx, ctrl_ctx,
 			changed_ep_bitmask, changed_ep_bitmask);
@@ -3770,6 +3768,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 	 * which must succeed.
 	 */
 	ret = xhci_configure_endpoint(xhci, udev, command, true);
+	xhci_free_command(xhci, command);
 	if (ret < 0) {
 		/* Configure Endpoint failed for some reason, so leave the streams rings intact. */
 		spin_lock_irqsave(&xhci->lock, flags);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 852a6b537a60..7c3cb7f3b0aa 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -592,7 +592,6 @@ struct xhci_stream_info {
 	dma_addr_t			ctx_array_dma;
 	/* For mapping physical TRB addresses to segments in stream rings */
 	struct radix_tree_root		trb_address_map;
-	struct xhci_command		*free_streams_command;
 };
 
 #define	SMALL_STREAM_ARRAY_SIZE		256
-- 
2.50.1


  parent reply	other threads:[~2026-12-01 19:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
2026-12-01 19:41 ` [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx() Niklas Neronin
2026-12-01 19:47   ` sashiko-bot
2026-12-01 19:41 ` [PATCH 2/7] usb: xhci: add xhci_queue_set_deq() helper Niklas Neronin
2026-12-01 19:41 ` [PATCH 3/7] usb: xhci: simplify prepare_ring() Niklas Neronin
2026-12-01 19:41 ` [PATCH 4/7] usb: xhci: split Configure Endpoint and Evaluate Context handling Niklas Neronin
2026-12-01 19:41 ` [PATCH 5/7] usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails Niklas Neronin
2026-12-01 19:52   ` sashiko-bot
2026-12-01 19:41 ` Niklas Neronin [this message]
2026-12-01 19:41 ` [PATCH 7/7] usb: xhci: remove command ring TRB reservation Niklas Neronin
2026-12-01 19:52 ` [PATCH 0/7] usb: xhci: command queueing cleanups sashiko-bot

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=20261201194134.265480-7-niklas.neronin@linux.intel.com \
    --to=niklas.neronin@linux.intel.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.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