Linux USB
 help / color / mirror / Atom feed
* [PATCH 0/7] usb: xhci: command queueing cleanups
@ 2026-12-01 19:41 Niklas Neronin
  2026-12-01 19:41 ` [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx() Niklas Neronin
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

This series cleans up xHCI command construction and queueing, removes
some command-specific special cases, and simplifies the Configure
Endpoint / Evaluate Context paths.

The final patch removes the command ring reservation for so-called
"must succeed" commands. There does not appear to be a documented
requirement for reserving command ring space for these commands, and a
full command ring already prevents further command progress. Removing
the reservation also eliminates the stream allocation limit that
depended on it.

Niklas Neronin (7):
  usb: xhci: simplify xhci_get_input_control_ctx()
  usb: xhci: add xhci_queue_set_deq() helper
  usb: xhci: simplify prepare_ring()
  usb: xhci: split Configure Endpoint and Evaluate Context handling
  usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails
  usb: xhci: remove pre-allocated free streams command
  usb: xhci: remove command ring TRB reservation

 drivers/usb/host/xhci-mem.c   |  28 +--
 drivers/usb/host/xhci-ring.c  |  98 ++++-----
 drivers/usb/host/xhci-trace.h |  10 +
 drivers/usb/host/xhci.c       | 387 +++++++++++++---------------------
 drivers/usb/host/xhci.h       |  14 +-
 5 files changed, 202 insertions(+), 335 deletions(-)

-- 
2.50.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx()
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
@ 2026-12-01 19:41 ` 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
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

xhci_get_input_control_ctx() returns NULL if the container context is not
an input context, so every caller carries its own error handling for that
case.

That case cannot happen at runtime. A container's type is set once at
allocation by xhci_alloc_container_ctx() and never changes. Every context
passed to this helper is a 'virt_dev' or command 'in_ctx', which is always
allocated as XHCI_CTX_TYPE_INPUT. A wrong type can only come from a driver
bug, not from hardware or user input.

Replace the NULL return with WARN_ON_ONCE() so that misuse produces a
backtrace pointing at the bad caller, and remove the error handling from
all callers.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c  |  7 +---
 drivers/usb/host/xhci-ring.c |  6 +--
 drivers/usb/host/xhci.c      | 79 +-----------------------------------
 3 files changed, 5 insertions(+), 87 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 75577f441cdd..3ccc9f7404b7 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -512,12 +512,9 @@ void xhci_free_port_bw_ctx(struct xhci_hcd *xhci,
 	kfree(ctx);
 }
 
-struct xhci_input_control_ctx *xhci_get_input_control_ctx(
-					      struct xhci_container_ctx *ctx)
+struct xhci_input_control_ctx *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
 {
-	if (ctx->type != XHCI_CTX_TYPE_INPUT)
-		return NULL;
-
+	WARN_ON_ONCE(ctx->type != XHCI_CTX_TYPE_INPUT);
 	return (struct xhci_input_control_ctx *)ctx->bytes;
 }
 
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a1fa188680f5..53e1e9a59f14 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -1700,12 +1700,8 @@ static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id)
 	virt_dev = xhci->devs[slot_id];
 	if (!virt_dev)
 		return;
-	ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "Could not get input context, bad type.\n");
-		return;
-	}
 
+	ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
 	add_flags = le32_to_cpu(ctrl_ctx->add_flags);
 
 	/* Input ctx add_flags are the endpoint index plus one */
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index fc1b0a6b0429..9688c5986a40 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1580,12 +1580,7 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
 
 		command->in_ctx = vdev->in_ctx;
 		ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
-		if (!ctrl_ctx) {
-			xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-					__func__);
-			ret = -ENOMEM;
-			break;
-		}
+
 		/* Set up the modified control endpoint 0 */
 		xhci_endpoint_copy(xhci, vdev->in_ctx, vdev->out_ctx, 0);
 
@@ -1929,12 +1924,6 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
 	in_ctx = xhci->devs[udev->slot_id]->in_ctx;
 	out_ctx = xhci->devs[udev->slot_id]->out_ctx;
 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return 0;
-	}
-
 	ep_index = xhci_get_endpoint_index(&ep->desc);
 	ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
 	/* If the HC already knows the endpoint is disabled,
@@ -2019,11 +2008,6 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
 	virt_dev = xhci->devs[udev->slot_id];
 	in_ctx = virt_dev->in_ctx;
 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return 0;
-	}
 
 	ep_index = xhci_get_endpoint_index(&ep->desc);
 	/* If this endpoint is already in use, and the upper layers are trying
@@ -2091,11 +2075,6 @@ static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *vir
 	int i;
 
 	ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return;
-	}
 
 	/* When a device's add flag and drop flag are zero, any subsequent
 	 * configure endpoint command will leave that endpoint's state
@@ -2809,11 +2788,6 @@ static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
 		old_active_eps = virt_dev->tt_info->active_eps;
 
 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return -ENOMEM;
-	}
 
 	for (i = 0; i < 31; i++) {
 		if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
@@ -2981,12 +2955,6 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 	virt_dev = xhci->devs[udev->slot_id];
 
 	ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
-	if (!ctrl_ctx) {
-		spin_unlock_irqrestore(&xhci->lock, flags);
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return -ENOMEM;
-	}
 
 	if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
 			xhci_reserve_host_resources(xhci, ctrl_ctx)) {
@@ -3106,12 +3074,6 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
 
 	/* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
 	ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		ret = -ENOMEM;
-		goto command_cleanup;
-	}
 	ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
 	ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
 	ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
@@ -3421,13 +3383,6 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,
 
 	/* config ep command clears toggle if add and drop ep flags are set */
 	ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
-	if (!ctrl_ctx) {
-		spin_unlock_irqrestore(&xhci->lock, flags);
-		xhci_free_command(xhci, cfg_cmd);
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		goto cleanup;
-	}
 
 	xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx,
 					   ctrl_ctx, ep_flag, ep_flag);
@@ -3650,12 +3605,6 @@ static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
 		return -ENOMEM;
 
 	ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		xhci_free_command(xhci, config_cmd);
-		return -ENOMEM;
-	}
 
 	/* Check to make sure all endpoints are not already configured for
 	 * streams.  While we're at it, find the maximum number of streams that
@@ -3812,12 +3761,6 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 	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);
-	if (!ctrl_ctx) {
-		spin_unlock_irqrestore(&xhci->lock, flags);
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return -EINVAL;
-	}
 
 	for (i = 0; i < num_eps; i++) {
 		struct xhci_ep_ctx *ep_ctx;
@@ -4384,12 +4327,7 @@ static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
 
 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
 	ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		ret = -EINVAL;
-		goto out;
-	}
+
 	/*
 	 * If this is the first Set Address since device plug-in or
 	 * virt_device realloaction after a resume with an xHCI power loss,
@@ -4570,13 +4508,6 @@ static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
 
 	/* Attempt to issue an Evaluate Context command to change the MEL. */
 	ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
-	if (!ctrl_ctx) {
-		spin_unlock_irqrestore(&xhci->lock, flags);
-		xhci_free_command(xhci, command);
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		return -ENOMEM;
-	}
 
 	xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
 	spin_unlock_irqrestore(&xhci->lock, flags);
@@ -5302,12 +5233,6 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
 		return -ENOMEM;
 
 	ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
-	if (!ctrl_ctx) {
-		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
-				__func__);
-		xhci_free_command(xhci, config_cmd);
-		return -ENOMEM;
-	}
 
 	spin_lock_irqsave(&xhci->lock, flags);
 	if (hdev->speed == USB_SPEED_HIGH &&
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/7] usb: xhci: add xhci_queue_set_deq() helper
  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:41 ` Niklas Neronin
  2026-12-01 19:41 ` [PATCH 3/7] usb: xhci: simplify prepare_ring() Niklas Neronin
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

Most xHCI commands have dedicated xhci_queue_*() helper functions that
encapsulate TRB construction and command queueing.

The Set TR Dequeue Pointer command is currently assembled directly in
xhci_move_deq_to_next_td(), making it the only command that bypasses
this pattern.

Add an xhci_queue_set_deq() helper and convert the existing user to it.
This improves consistency with the other command queue helpers and keeps
Set TR Dequeue Pointer TRB construction in a single location.

No functional change intended.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-ring.c | 21 +++++++++++++--------
 drivers/usb/host/xhci.h      |  3 +++
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 53e1e9a59f14..7f983e8d4697 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -62,9 +62,6 @@
 #include "xhci.h"
 #include "xhci-trace.h"
 
-static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
-			 u32 field1, u32 field2,
-			 u32 field3, u32 field4, bool command_must_succeed);
 static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci, int slot_id, int ep_index);
 
 /*
@@ -755,11 +752,9 @@ static int xhci_move_deq_to_next_td(struct xhci_hcd *xhci,
 
 	if (stream_id)
 		trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
-	ret = queue_command(xhci, cmd,
-		lower_32_bits(addr) | trb_sct | new_cycle,
-		upper_32_bits(addr),
-		STREAM_ID_FOR_TRB(stream_id), SLOT_ID_FOR_TRB(ep->vdev->slot_id) |
-		EP_INDEX_FOR_TRB(ep->ep_index) | TRB_TYPE(TRB_SET_DEQ), false);
+
+	ret = xhci_queue_set_deq(xhci, cmd, addr, ep->vdev->slot_id, ep->ep_index, stream_id,
+				 new_cycle, trb_sct);
 	if (ret < 0)
 		goto err_out;
 
@@ -4541,3 +4536,13 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
 	return queue_command(xhci, cmd, 0, 0, 0,
 			trb_slot_id | trb_ep_index | type, false);
 }
+
+int xhci_queue_set_deq(struct xhci_hcd *xhci, struct xhci_command *cmd, dma_addr_t addr,
+		       u32 slot_id, unsigned int ep_index, unsigned int stream_id, int new_cycle,
+		       u32 trb_sct)
+{
+	return queue_command(xhci, cmd, lower_32_bits(addr) | trb_sct | new_cycle,
+			     upper_32_bits(addr), STREAM_ID_FOR_TRB(stream_id),
+			     SLOT_ID_FOR_TRB(slot_id) | EP_INDEX_FOR_TRB(ep_index) |
+			     TRB_TYPE(TRB_SET_DEQ), false);
+}
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index ec4bfeb4887c..852a6b537a60 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1950,6 +1950,9 @@ int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		int slot_id, unsigned int ep_index,
 		enum xhci_ep_reset_type reset_type);
+int xhci_queue_set_deq(struct xhci_hcd *xhci, struct xhci_command *cmd, dma_addr_t addr,
+		       u32 slot_id, unsigned int ep_index, unsigned int stream_id, int new_cycle,
+		       u32 trb_sct);
 int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		u32 slot_id);
 void xhci_handle_command_timeout(struct work_struct *work);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/7] usb: xhci: simplify prepare_ring()
  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:41 ` [PATCH 2/7] usb: xhci: add xhci_queue_set_deq() helper Niklas Neronin
@ 2026-12-01 19:41 ` Niklas Neronin
  2026-12-01 19:41 ` [PATCH 4/7] usb: xhci: split Configure Endpoint and Evaluate Context handling Niklas Neronin
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

Function prepare_ring() contains special handling for the command ring
even though the only differences are that command rings cannot be expanded
and use a different free space check.

Move the command ring specific logic into queue_command() and simplify
prepare_ring() to handle only endpoint rings. This makes prepare_ring()
align with the existing documentation, comments, and error messages,
which all describe endpoint ring handling.

No functional change intended.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-ring.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 7f983e8d4697..15f7add6efe2 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -3343,18 +3343,12 @@ static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
 		return -EINVAL;
 	}
 
-	if (ep_ring != xhci->cmd_ring) {
-		new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs);
-	} else if (xhci_num_trbs_free(ep_ring) <= num_trbs) {
-		xhci_err(xhci, "Do not support expand command ring\n");
-		return -ENOMEM;
-	}
-
+	new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs);
 	if (new_segs) {
 		xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
 				"ERROR no room on ep ring, try ring expansion");
 		if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) {
-			xhci_err(xhci, "Ring expansion failed\n");
+			xhci_err(xhci, "Endpoint ring expansion failed\n");
 			return -ENOMEM;
 		}
 	}
@@ -4399,7 +4393,6 @@ static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 {
 	int reserved_trbs = xhci->cmd_ring_reserved_trbs;
 	struct usb_hcd *hcd = xhci_to_hcd(xhci);
-	int ret;
 
 	if ((xhci->xhc_state & XHCI_STATE_DYING) ||
 		(xhci->xhc_state & XHCI_STATE_HALTED)) {
@@ -4416,14 +4409,18 @@ static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 	if (!command_must_succeed)
 		reserved_trbs++;
 
-	ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
-			reserved_trbs, GFP_ATOMIC);
-	if (ret < 0) {
-		xhci_err(xhci, "ERR: No room for command on command ring\n");
+	if (xhci_num_trbs_free(xhci->cmd_ring) <= reserved_trbs) {
+		xhci_err(xhci, "No room for command on command ring\n");
 		if (command_must_succeed)
-			xhci_err(xhci, "ERR: Reserved TRB counting for "
-					"unfailable commands failed.\n");
-		return ret;
+			xhci_err(xhci, "Reserved TRB counting for unfailable commands failed.\n");
+		return -ENOMEM;
+	}
+
+	if (trb_is_link(xhci->cmd_ring->enqueue))
+		inc_enq_past_link(xhci, xhci->cmd_ring, 0);
+	if (last_trb_on_seg(xhci->cmd_ring->enq_seg, xhci->cmd_ring->enqueue)) {
+		xhci_warn(xhci, "Missing link TRB at end of ring segment\n");
+		return -EINVAL;
 	}
 
 	cmd->command_trb = xhci->cmd_ring->enqueue;
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/7] usb: xhci: split Configure Endpoint and Evaluate Context handling
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
                   ` (2 preceding siblings ...)
  2026-12-01 19:41 ` [PATCH 3/7] usb: xhci: simplify prepare_ring() Niklas Neronin
@ 2026-12-01 19:41 ` Niklas Neronin
  2026-12-01 19:41 ` [PATCH 5/7] usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails Niklas Neronin
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

xhci_configure_endpoint() handles both Configure Endpoint and Evaluate
Context commands, selected through the 'ctx_change' parameter.

While the two commands share some infrastructure, they have different
requirements, queue different command TRBs, and handle different
completion codes. As a result, xhci_configure_endpoint() contains
command-specific branching throughout its setup and completion paths.
The completion status handling is also split into separate helper
functions.

Separate the Evaluate Context path into a dedicated xhci_evaluate_context()
helper and fold the command completion handling into the respective command
submission functions. This removes the 'ctx_change' parameter, reduces
command specific branching while making the Configure Endpoint and
Evaluate Context flows easier to follow.

Evaluate Context no longer goes through the XHCI_EP_LIMIT_QUIRK.
Evaluate Context only modifies the Slot and EP0 contexts, which
xhci_count_num_new_endpoints() and xhci_count_num_dropped_endpoints()
ignore, so this accounting was always a no-op for it.
Software bandwidth checking was already skipped for Evaluate Context.

Add separate xhci_evaluate_context and xhci_evaluate_context_ctrl_ctx
trace events, so that Evaluate Context commands are no longer traced as
Configure Endpoint.

The resulting code is smaller and more closely reflects the distinct
semantics of the two xHCI commands.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-trace.h |  10 ++
 drivers/usb/host/xhci.c       | 262 ++++++++++++++++------------------
 2 files changed, 132 insertions(+), 140 deletions(-)

diff --git a/drivers/usb/host/xhci-trace.h b/drivers/usb/host/xhci-trace.h
index 724cba2dbb78..9b3f45743aa9 100644
--- a/drivers/usb/host/xhci-trace.h
+++ b/drivers/usb/host/xhci-trace.h
@@ -446,6 +446,11 @@ DEFINE_EVENT(xhci_log_slot_ctx, xhci_configure_endpoint,
 	TP_ARGS(ctx)
 );
 
+DEFINE_EVENT(xhci_log_slot_ctx, xhci_evaluate_context,
+	TP_PROTO(struct xhci_slot_ctx *ctx),
+	TP_ARGS(ctx)
+);
+
 DECLARE_EVENT_CLASS(xhci_log_ctrl_ctx,
 	TP_PROTO(struct xhci_input_control_ctx *ctrl_ctx),
 	TP_ARGS(ctrl_ctx),
@@ -471,6 +476,11 @@ DEFINE_EVENT(xhci_log_ctrl_ctx, xhci_configure_endpoint_ctrl_ctx,
 	TP_ARGS(ctrl_ctx)
 );
 
+DEFINE_EVENT(xhci_log_ctrl_ctx, xhci_evaluate_context_ctrl_ctx,
+	TP_PROTO(struct xhci_input_control_ctx *ctrl_ctx),
+	TP_ARGS(ctrl_ctx)
+);
+
 DECLARE_EVENT_CLASS(xhci_log_ring,
 	TP_PROTO(struct xhci_ring *ring),
 	TP_ARGS(ring),
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 9688c5986a40..d2ca031cff70 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1535,9 +1535,8 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
 	return 1;
 }
 
-static int xhci_configure_endpoint(struct xhci_hcd *xhci,
-		struct usb_device *udev, struct xhci_command *command,
-		bool ctx_change, bool must_succeed);
+static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
+				 struct xhci_command *command, bool must_succeed);
 
 /*
  * Full speed devices may have a max packet size greater than 8 bytes, but the
@@ -1592,8 +1591,7 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
 		ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
 		ctrl_ctx->drop_flags = 0;
 
-		ret = xhci_configure_endpoint(xhci, vdev->udev, command,
-					      true, false);
+		ret = xhci_evaluate_context(xhci, vdev->udev, command, false);
 		/* Clean up the input context for later use by bandwidth functions */
 		ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
 		break;
@@ -2096,106 +2094,6 @@ static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *vir
 	}
 }
 
-static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
-		struct usb_device *udev, u32 *cmd_status)
-{
-	int ret;
-
-	switch (*cmd_status) {
-	case COMP_COMMAND_ABORTED:
-	case COMP_COMMAND_RING_STOPPED:
-		xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
-		ret = -ETIME;
-		break;
-	case COMP_RESOURCE_ERROR:
-		dev_warn(&udev->dev,
-			 "Not enough host controller resources for new device state.\n");
-		ret = -ENOMEM;
-		/* FIXME: can we allocate more resources for the HC? */
-		break;
-	case COMP_BANDWIDTH_ERROR:
-	case COMP_SECONDARY_BANDWIDTH_ERROR:
-		dev_warn(&udev->dev,
-			 "Not enough bandwidth for new device state.\n");
-		ret = -ENOSPC;
-		/* FIXME: can we go back to the old state? */
-		break;
-	case COMP_TRB_ERROR:
-		/* the HCD set up something wrong */
-		dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
-				"add flag = 1, "
-				"and endpoint is not disabled.\n");
-		ret = -EINVAL;
-		break;
-	case COMP_INCOMPATIBLE_DEVICE_ERROR:
-		dev_warn(&udev->dev,
-			 "ERROR: Incompatible device for endpoint configure command.\n");
-		ret = -ENODEV;
-		break;
-	case COMP_SUCCESS:
-		xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
-				"Successful Endpoint Configure command");
-		ret = 0;
-		break;
-	default:
-		xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
-				*cmd_status);
-		ret = -EINVAL;
-		break;
-	}
-	return ret;
-}
-
-static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
-		struct usb_device *udev, u32 *cmd_status)
-{
-	int ret;
-
-	switch (*cmd_status) {
-	case COMP_COMMAND_ABORTED:
-	case COMP_COMMAND_RING_STOPPED:
-		xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
-		ret = -ETIME;
-		break;
-	case COMP_PARAMETER_ERROR:
-		dev_warn(&udev->dev,
-			 "WARN: xHCI driver setup invalid evaluate context command.\n");
-		ret = -EINVAL;
-		break;
-	case COMP_SLOT_NOT_ENABLED_ERROR:
-		dev_warn(&udev->dev,
-			"WARN: slot not enabled for evaluate context command.\n");
-		ret = -EINVAL;
-		break;
-	case COMP_CONTEXT_STATE_ERROR:
-		dev_warn(&udev->dev,
-			"WARN: invalid context state for evaluate context command.\n");
-		ret = -EINVAL;
-		break;
-	case COMP_INCOMPATIBLE_DEVICE_ERROR:
-		dev_warn(&udev->dev,
-			"ERROR: Incompatible device for evaluate context command.\n");
-		ret = -ENODEV;
-		break;
-	case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
-		/* Max Exit Latency too large error */
-		dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
-		ret = -EINVAL;
-		break;
-	case COMP_SUCCESS:
-		xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
-				"Successful evaluate context command");
-		ret = 0;
-		break;
-	default:
-		xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
-			*cmd_status);
-		ret = -EINVAL;
-		break;
-	}
-	return ret;
-}
-
 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
 		struct xhci_input_control_ctx *ctrl_ctx)
 {
@@ -2928,13 +2826,8 @@ int xhci_usb_endpoint_maxp(struct usb_device *udev,
 	return usb_endpoint_maxp(&host_ep->desc);
 }
 
-/* Issue a configure endpoint command or evaluate context command
- * and wait for it to finish.
- */
-static int xhci_configure_endpoint(struct xhci_hcd *xhci,
-		struct usb_device *udev,
-		struct xhci_command *command,
-		bool ctx_change, bool must_succeed)
+static int xhci_configure_endpoint(struct xhci_hcd *xhci, struct usb_device *udev,
+				   struct xhci_command *command, bool must_succeed)
 {
 	int ret;
 	unsigned long flags;
@@ -2964,7 +2857,8 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 				xhci->num_active_eps);
 		return -ENOMEM;
 	}
-	if ((xhci->quirks & XHCI_SW_BW_CHECKING) && !ctx_change &&
+
+	if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
 	    xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
 		if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
 			xhci_free_host_resources(xhci, ctrl_ctx);
@@ -2978,14 +2872,8 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 	trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx);
 	trace_xhci_configure_endpoint(slot_ctx);
 
-	if (!ctx_change)
-		ret = xhci_queue_configure_endpoint(xhci, command,
-				command->in_ctx->dma,
-				udev->slot_id, must_succeed);
-	else
-		ret = xhci_queue_evaluate_context(xhci, command,
-				command->in_ctx->dma,
-				udev->slot_id, must_succeed);
+	ret = xhci_queue_configure_endpoint(xhci, command, command->in_ctx->dma,
+					    udev->slot_id, must_succeed);
 	if (ret < 0) {
 		if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
 			xhci_free_host_resources(xhci, ctrl_ctx);
@@ -2994,18 +2882,47 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 				"FIXME allocate a new ring segment");
 		return -ENOMEM;
 	}
+
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
 	/* Wait for the configure endpoint command to complete */
 	wait_for_completion(command->completion);
 
-	if (!ctx_change)
-		ret = xhci_configure_endpoint_result(xhci, udev,
-						     &command->status);
-	else
-		ret = xhci_evaluate_context_result(xhci, udev,
-						   &command->status);
+	switch (command->status) {
+	case COMP_COMMAND_ABORTED:
+	case COMP_COMMAND_RING_STOPPED:
+		xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
+		ret = -ETIME;
+		break;
+	case COMP_RESOURCE_ERROR:
+		dev_warn(&udev->dev, "Not enough host controller resources for new device state.\n");
+		ret = -ENOMEM;
+		break;
+	case COMP_BANDWIDTH_ERROR:
+	case COMP_SECONDARY_BANDWIDTH_ERROR:
+		dev_warn(&udev->dev, "Not enough bandwidth for new device state.\n");
+		ret = -ENOSPC;
+		/* FIXME: can we go back to the old state? */
+		break;
+	case COMP_TRB_ERROR:
+		dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, and endpoint is not disabled.\n");
+		ret = -EINVAL;
+		break;
+	case COMP_INCOMPATIBLE_DEVICE_ERROR:
+		dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint configure command.\n");
+		ret = -ENODEV;
+		break;
+	case COMP_SUCCESS:
+		xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
+			       "Successful Endpoint Configure command");
+		break;
+	default:
+		xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
+			 command->status);
+		ret = -EINVAL;
+		break;
+	}
 
 	if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
 		spin_lock_irqsave(&xhci->lock, flags);
@@ -3021,6 +2938,78 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 	return ret;
 }
 
+static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
+				 struct xhci_command *command, bool must_succeed)
+{
+	int ret;
+	unsigned long flags;
+
+	if (!command)
+		return -EINVAL;
+
+	spin_lock_irqsave(&xhci->lock, flags);
+
+	if (xhci->xhc_state & XHCI_STATE_DYING) {
+		spin_unlock_irqrestore(&xhci->lock, flags);
+		return -ESHUTDOWN;
+	}
+
+	trace_xhci_evaluate_context_ctrl_ctx(xhci_get_input_control_ctx(command->in_ctx));
+	trace_xhci_evaluate_context(xhci_get_slot_ctx(xhci, command->in_ctx));
+
+	ret = xhci_queue_evaluate_context(xhci, command, command->in_ctx->dma, udev->slot_id,
+					  must_succeed);
+	if (ret < 0) {
+		spin_unlock_irqrestore(&xhci->lock, flags);
+		return -ENOMEM;
+	}
+
+	xhci_ring_cmd_db(xhci);
+	spin_unlock_irqrestore(&xhci->lock, flags);
+
+	wait_for_completion(command->completion);
+
+	switch (command->status) {
+	case COMP_COMMAND_ABORTED:
+	case COMP_COMMAND_RING_STOPPED:
+		xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
+		ret = -ETIME;
+		break;
+	case COMP_PARAMETER_ERROR:
+		dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate context command.\n");
+		ret = -EINVAL;
+		break;
+	case COMP_SLOT_NOT_ENABLED_ERROR:
+		dev_warn(&udev->dev, "WARN: slot not enabled for evaluate context command.\n");
+		ret = -EINVAL;
+		break;
+	case COMP_CONTEXT_STATE_ERROR:
+		dev_warn(&udev->dev, "WARN: invalid context state for evaluate context command.\n");
+		ret = -EINVAL;
+		break;
+	case COMP_INCOMPATIBLE_DEVICE_ERROR:
+		dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate context command.\n");
+		ret = -ENODEV;
+		break;
+	case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
+		/* Max Exit Latency too large error */
+		dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
+		ret = -EINVAL;
+		break;
+	case COMP_SUCCESS:
+		xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
+			       "Successful evaluate context command");
+		break;
+	default:
+		xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
+			 command->status);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
 	struct xhci_virt_device *vdev, int i)
 {
@@ -3097,8 +3086,7 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
 		}
 	}
 
-	ret = xhci_configure_endpoint(xhci, udev, command,
-			false, false);
+	ret = xhci_configure_endpoint(xhci, udev, command, false);
 	if (ret)
 		/* Callee should call reset_bandwidth() */
 		goto command_cleanup;
@@ -3677,8 +3665,7 @@ static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
 			changed_ep_bitmask, changed_ep_bitmask);
 
 	/* Issue and wait for the configure endpoint command */
-	ret = xhci_configure_endpoint(xhci, udev, config_cmd,
-			false, false);
+	ret = xhci_configure_endpoint(xhci, udev, config_cmd, false);
 
 	/* xHC rejected the configure endpoint command for some reason, so we
 	 * leave the old ring intact and free our internal streams data
@@ -3783,8 +3770,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 	/* Issue and wait for the configure endpoint command,
 	 * which must succeed.
 	 */
-	ret = xhci_configure_endpoint(xhci, udev, command,
-			false, true);
+	ret = xhci_configure_endpoint(xhci, udev, command, true);
 
 	/* xHC rejected the configure endpoint command for some reason, so we
 	 * leave the streams rings intact.
@@ -4522,9 +4508,7 @@ static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
 			"Set up evaluate context for LPM MEL change.");
 
 	/* Issue and wait for the evaluate context command. */
-	ret = xhci_configure_endpoint(xhci, udev, command,
-			true, true);
-
+	ret = xhci_evaluate_context(xhci, udev, command, true);
 	if (!ret) {
 		spin_lock_irqsave(&xhci->lock, flags);
 		virt_dev->current_mel = max_exit_latency;
@@ -5291,11 +5275,9 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
 	 * evaluate context command.
 	 */
 	if (xhci->hci_version > 0x95)
-		ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
-				false, false);
+		ret = xhci_configure_endpoint(xhci, hdev, config_cmd, false);
 	else
-		ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
-				true, false);
+		ret = xhci_evaluate_context(xhci, hdev, config_cmd, false);
 
 	xhci_free_command(xhci, config_cmd);
 	return ret;
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 5/7] usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
                   ` (3 preceding siblings ...)
  2026-12-01 19:41 ` [PATCH 4/7] usb: xhci: split Configure Endpoint and Evaluate Context handling Niklas Neronin
@ 2026-12-01 19:41 ` Niklas Neronin
  2026-12-01 19:52   ` sashiko-bot
  2026-12-01 19:41 ` [PATCH 6/7] usb: xhci: remove pre-allocated free streams command Niklas Neronin
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

xhci_free_streams() sets EP_GETTING_NO_STREAMS on each endpoint before
issuing the Configure Endpoint command that removes the streams. The
flag is only cleared on success. If the command fails, either because
it can't be queued or because the xHC rejects it, the function returns
with the flag still set.

xhci_urb_enqueue() rejects URBs for endpoints that have
EP_GETTING_NO_STREAMS set, so after a failed free_streams call the
endpoint can't be used until the device is reconfigured or reset.

Clear EP_GETTING_NO_STREAMS on the error path. The streams rings and
EP_HAS_STREAMS are left untouched, so the endpoint stays in its
previous streams state and the caller can retry.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index d2ca031cff70..841ef59b83f6 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -3754,8 +3754,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 
 		ep_index = xhci_get_endpoint_index(&eps[i]->desc);
 		ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
-		xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
-			EP_GETTING_NO_STREAMS;
+		vdev->eps[ep_index].ep_state |= EP_GETTING_NO_STREAMS;
 
 		xhci_endpoint_copy(xhci, command->in_ctx,
 				vdev->out_ctx, ep_index);
@@ -3771,12 +3770,16 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 	 * which must succeed.
 	 */
 	ret = xhci_configure_endpoint(xhci, udev, command, true);
-
-	/* xHC rejected the configure endpoint command for some reason, so we
-	 * leave the streams rings intact.
-	 */
-	if (ret < 0)
+	if (ret < 0) {
+		/* Configure Endpoint failed for some reason, so leave the streams rings intact. */
+		spin_lock_irqsave(&xhci->lock, flags);
+		for (i = 0; i < num_eps; i++) {
+			ep_index = xhci_get_endpoint_index(&eps[i]->desc);
+			vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
+		}
+		spin_unlock_irqrestore(&xhci->lock, flags);
 		return ret;
+	}
 
 	/*
 	 * dma_free_coherent() called by xhci_free_stream_info() may sleep,
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 6/7] usb: xhci: remove pre-allocated free streams command
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
                   ` (4 preceding siblings ...)
  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:41 ` Niklas Neronin
  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
  7 siblings, 0 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 7/7] usb: xhci: remove command ring TRB reservation
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
                   ` (5 preceding siblings ...)
  2026-12-01 19:41 ` [PATCH 6/7] usb: xhci: remove pre-allocated free streams command Niklas Neronin
@ 2026-12-01 19:41 ` Niklas Neronin
  2026-12-01 19:52 ` [PATCH 0/7] usb: xhci: command queueing cleanups sashiko-bot
  7 siblings, 0 replies; 11+ messages in thread
From: Niklas Neronin @ 2026-12-01 19:41 UTC (permalink / raw)
  To: mathias.nyman; +Cc: linux-usb, Niklas Neronin

The command ring reservation mechanism lets "must succeed" commands use
command ring space that other commands can't. It has two kinds of users:

 * One TRB reserved at init for the Evaluate Context command issued
   when changing Max Exit Latency to disable LPM.
 * One TRB reserved for each allocated stream_info, so that the
   Configure Endpoint command in xhci_free_streams() can always be
   queued.

This dates back to the original command ring implementation. Neither
the original commits nor later changes explain why these commands
specifically need guaranteed ring space.

The command ring has 254 usable TRBs, and commands are normally
completed and removed long before the ring fills up. If the ring does
fill, something else has already gone wrong, such as commands not
completing. In that case, reserving a few TRBs only delays the failure.
Both affected callers already handle a queueing error.

The reservation also costs something: the reserved TRBs are always
excluded from the free space, even when unused. Each stream allocation
takes one more TRB, capped at MAX_RSVD_CMD_TRBS, and further stream
allocations fail once the cap is reached.

Remove the reservation completely. queue_command() now accepts any
command as long as more than one TRB is free. One TRB is always kept
free so that a full ring is not mistaken for an empty one
(enqueue == dequeue). Before, normal commands needed at least
cmd_ring_reserved_trbs + 2 free TRBs, so all commands may now use
slightly more of the ring, and stream allocation no longer fails because
the reservation cap was reached.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c  | 12 ++-------
 drivers/usb/host/xhci-ring.c | 50 ++++++++++++------------------------
 drivers/usb/host/xhci.c      | 42 +++++++++++-------------------
 drivers/usb/host/xhci.h      | 10 +++-----
 4 files changed, 37 insertions(+), 77 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 78eb8358ea29..6300cc0b33c0 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -617,16 +617,11 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
 
 	xhci_dbg(xhci, "Allocating %u streams and %u stream context array entries.\n",
 			num_streams, num_stream_ctxs);
-	if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
-		xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
-		return NULL;
-	}
-	xhci->cmd_ring_reserved_trbs++;
 
 	stream_info = kzalloc_node(sizeof(*stream_info), mem_flags,
 			dev_to_node(dev));
 	if (!stream_info)
-		goto cleanup_trbs;
+		return NULL;
 
 	stream_info->num_streams = num_streams;
 	stream_info->num_stream_ctxs = num_stream_ctxs;
@@ -704,8 +699,6 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
 	kfree(stream_info->stream_rings);
 cleanup_info:
 	kfree(stream_info);
-cleanup_trbs:
-	xhci->cmd_ring_reserved_trbs--;
 	return NULL;
 }
 /*
@@ -766,7 +759,7 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
 			stream_info->stream_rings[cur_stream] = NULL;
 		}
 	}
-	xhci->cmd_ring_reserved_trbs--;
+
 	if (stream_info->stream_ctx_array)
 		xhci_free_stream_ctx(xhci,
 				stream_info->num_stream_ctxs,
@@ -1991,7 +1984,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 	if (xhci->rh_bw)
 		xhci_rh_bw_cleanup(xhci);
 
-	xhci->cmd_ring_reserved_trbs = 0;
 	xhci->usb2_rhub.num_ports = 0;
 	xhci->usb3_rhub.num_ports = 0;
 	xhci->num_active_eps = 0;
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 15f7add6efe2..d9d8f95b068d 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -4379,19 +4379,11 @@ int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
 
 /****		Command Ring Operations		****/
 
-/* Generic function for queueing a command TRB on the command ring.
- * Check to make sure there's room on the command ring for one command TRB.
- * Also check that there's room reserved for commands that must not fail.
- * If this is a command that must not fail, meaning command_must_succeed = TRUE,
- * then only check for the number of reserved spots.
- * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
- * because the command event handler may want to resubmit a failed command.
- */
+/* Generic function for queueing a command TRB on the command ring. */
 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 			 u32 field1, u32 field2,
-			 u32 field3, u32 field4, bool command_must_succeed)
+			 u32 field3, u32 field4)
 {
-	int reserved_trbs = xhci->cmd_ring_reserved_trbs;
 	struct usb_hcd *hcd = xhci_to_hcd(xhci);
 
 	if ((xhci->xhc_state & XHCI_STATE_DYING) ||
@@ -4406,13 +4398,9 @@ static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		return -ESHUTDOWN;
 	}
 
-	if (!command_must_succeed)
-		reserved_trbs++;
-
-	if (xhci_num_trbs_free(xhci->cmd_ring) <= reserved_trbs) {
+	/* Keep 1 TRB free so a full ring is not mistaken for an empty one */
+	if (xhci_num_trbs_free(xhci->cmd_ring) <= 1) {
 		xhci_err(xhci, "No room for command on command ring\n");
-		if (command_must_succeed)
-			xhci_err(xhci, "Reserved TRB counting for unfailable commands failed.\n");
 		return -ENOMEM;
 	}
 
@@ -4443,7 +4431,7 @@ int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		u32 trb_type, u32 slot_id)
 {
 	return queue_command(xhci, cmd, 0, 0, 0,
-			TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
+			TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
 }
 
 /* Queue an address device command TRB */
@@ -4453,13 +4441,13 @@ int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
 	return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
 			upper_32_bits(in_ctx_ptr), 0,
 			TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)
-			| (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false);
+			| (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0));
 }
 
 int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		u32 field1, u32 field2, u32 field3, u32 field4)
 {
-	return queue_command(xhci, cmd, field1, field2, field3, field4, false);
+	return queue_command(xhci, cmd, field1, field2, field3, field4);
 }
 
 /* Queue a reset device command TRB */
@@ -4467,40 +4455,36 @@ int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		u32 slot_id)
 {
 	return queue_command(xhci, cmd, 0, 0, 0,
-			TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
-			false);
+			TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id));
 }
 
 /* Queue a configure endpoint command TRB */
 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
 		struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
-		u32 slot_id, bool command_must_succeed)
+		u32 slot_id)
 {
 	return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
 			upper_32_bits(in_ctx_ptr), 0,
-			TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
-			command_must_succeed);
+			TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
 }
 
 /* Queue a get root hub port bandwidth command TRB */
 int xhci_queue_get_port_bw(struct xhci_hcd *xhci,
 		struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
-		u8 dev_speed, bool command_must_succeed)
+		u8 dev_speed)
 {
 	return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
 		upper_32_bits(in_ctx_ptr), 0,
-		TRB_TYPE(TRB_GET_BW) | DEV_SPEED_FOR_TRB(dev_speed),
-		command_must_succeed);
+		TRB_TYPE(TRB_GET_BW) | DEV_SPEED_FOR_TRB(dev_speed));
 }
 
 /* Queue an evaluate context command TRB */
 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
-		dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed)
+		dma_addr_t in_ctx_ptr, u32 slot_id)
 {
 	return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
 			upper_32_bits(in_ctx_ptr), 0,
-			TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
-			command_must_succeed);
+			TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id));
 }
 
 /*
@@ -4516,7 +4500,7 @@ int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
 	u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
 
 	return queue_command(xhci, cmd, 0, 0, 0,
-			trb_slot_id | trb_ep_index | type | trb_suspend, false);
+			trb_slot_id | trb_ep_index | type | trb_suspend);
 }
 
 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
@@ -4531,7 +4515,7 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		type |= TRB_TSP;
 
 	return queue_command(xhci, cmd, 0, 0, 0,
-			trb_slot_id | trb_ep_index | type, false);
+			trb_slot_id | trb_ep_index | type);
 }
 
 int xhci_queue_set_deq(struct xhci_hcd *xhci, struct xhci_command *cmd, dma_addr_t addr,
@@ -4541,5 +4525,5 @@ int xhci_queue_set_deq(struct xhci_hcd *xhci, struct xhci_command *cmd, dma_addr
 	return queue_command(xhci, cmd, lower_32_bits(addr) | trb_sct | new_cycle,
 			     upper_32_bits(addr), STREAM_ID_FOR_TRB(stream_id),
 			     SLOT_ID_FOR_TRB(slot_id) | EP_INDEX_FOR_TRB(ep_index) |
-			     TRB_TYPE(TRB_SET_DEQ), false);
+			     TRB_TYPE(TRB_SET_DEQ));
 }
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index f419b4246c64..3a4773ca8e97 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -542,12 +542,7 @@ static void xhci_init(struct usb_hcd *hcd)
 
 	/* Initialize the Command ring */
 	xhci_ring_init(xhci, xhci->cmd_ring);
-	/*
-	 * Reserve one command ring TRB for disabling LPM.
-	 * Since the USB core grabs the shared usb_bus bandwidth mutex before
-	 * disabling LPM, we only need to reserve one TRB for all devices.
-	 */
-	xhci->cmd_ring_reserved_trbs = 1;
+
 	/* Set the address in the Command Ring Control register */
 	xhci_set_cmd_ring_deq(xhci);
 
@@ -1188,7 +1183,6 @@ int xhci_resume(struct xhci_hcd *xhci, bool power_lost, bool is_auto_resume)
 		xhci->num_active_eps = 0;
 		xhci_rh_bw_cleanup(xhci);
 
-		xhci->cmd_ring_reserved_trbs = 0;
 		xhci_for_each_ring_seg(xhci->cmd_ring->first_seg, seg)
 			memset(seg->trbs, 0, sizeof(union xhci_trb) * TRBS_PER_SEGMENT);
 
@@ -1536,7 +1530,7 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
 }
 
 static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
-				 struct xhci_command *command, bool must_succeed);
+				 struct xhci_command *command);
 
 /*
  * Full speed devices may have a max packet size greater than 8 bytes, but the
@@ -1591,7 +1585,7 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
 		ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
 		ctrl_ctx->drop_flags = 0;
 
-		ret = xhci_evaluate_context(xhci, vdev->udev, command, false);
+		ret = xhci_evaluate_context(xhci, vdev->udev, command);
 		/* Clean up the input context for later use by bandwidth functions */
 		ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
 		break;
@@ -2827,7 +2821,7 @@ int xhci_usb_endpoint_maxp(struct usb_device *udev,
 }
 
 static int xhci_configure_endpoint(struct xhci_hcd *xhci, struct usb_device *udev,
-				   struct xhci_command *command, bool must_succeed)
+				   struct xhci_command *command)
 {
 	int ret;
 	unsigned long flags;
@@ -2872,8 +2866,7 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci, struct usb_device *ude
 	trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx);
 	trace_xhci_configure_endpoint(slot_ctx);
 
-	ret = xhci_queue_configure_endpoint(xhci, command, command->in_ctx->dma,
-					    udev->slot_id, must_succeed);
+	ret = xhci_queue_configure_endpoint(xhci, command, command->in_ctx->dma, udev->slot_id);
 	if (ret < 0) {
 		if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
 			xhci_free_host_resources(xhci, ctrl_ctx);
@@ -2939,7 +2932,7 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci, struct usb_device *ude
 }
 
 static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
-				 struct xhci_command *command, bool must_succeed)
+				 struct xhci_command *command)
 {
 	int ret;
 	unsigned long flags;
@@ -2957,8 +2950,7 @@ static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
 	trace_xhci_evaluate_context_ctrl_ctx(xhci_get_input_control_ctx(command->in_ctx));
 	trace_xhci_evaluate_context(xhci_get_slot_ctx(xhci, command->in_ctx));
 
-	ret = xhci_queue_evaluate_context(xhci, command, command->in_ctx->dma, udev->slot_id,
-					  must_succeed);
+	ret = xhci_queue_evaluate_context(xhci, command, command->in_ctx->dma, udev->slot_id);
 	if (ret < 0) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		return -ENOMEM;
@@ -3086,7 +3078,7 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
 		}
 	}
 
-	ret = xhci_configure_endpoint(xhci, udev, command, false);
+	ret = xhci_configure_endpoint(xhci, udev, command);
 	if (ret)
 		/* Callee should call reset_bandwidth() */
 		goto command_cleanup;
@@ -3176,7 +3168,7 @@ int xhci_get_port_bandwidth(struct xhci_hcd *xhci, struct xhci_container_ctx *ct
 	/* get xhci port bandwidth, refer to xhci rev1_2 protocol 4.6.15 */
 	spin_lock_irqsave(&xhci->lock, flags);
 
-	ret = xhci_queue_get_port_bw(xhci, cmd, ctx->dma, dev_speed, 0);
+	ret = xhci_queue_get_port_bw(xhci, cmd, ctx->dma, dev_speed);
 	if (ret) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		goto err_out;
@@ -3376,8 +3368,7 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,
 					   ctrl_ctx, ep_flag, ep_flag);
 	xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index);
 
-	err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma,
-				      udev->slot_id, false);
+	err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma, udev->slot_id);
 	if (err < 0) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		xhci_free_command(xhci, cfg_cmd);
@@ -3665,7 +3656,7 @@ static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
 			changed_ep_bitmask, changed_ep_bitmask);
 
 	/* Issue and wait for the configure endpoint command */
-	ret = xhci_configure_endpoint(xhci, udev, config_cmd, false);
+	ret = xhci_configure_endpoint(xhci, udev, config_cmd);
 
 	/* xHC rejected the configure endpoint command for some reason, so we
 	 * leave the old ring intact and free our internal streams data
@@ -3764,10 +3755,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 			changed_ep_bitmask, changed_ep_bitmask);
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
-	/* Issue and wait for the configure endpoint command,
-	 * which must succeed.
-	 */
-	ret = xhci_configure_endpoint(xhci, udev, command, true);
+	ret = xhci_configure_endpoint(xhci, udev, command);
 	xhci_free_command(xhci, command);
 	if (ret < 0) {
 		/* Configure Endpoint failed for some reason, so leave the streams rings intact. */
@@ -4510,7 +4498,7 @@ static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
 			"Set up evaluate context for LPM MEL change.");
 
 	/* Issue and wait for the evaluate context command. */
-	ret = xhci_evaluate_context(xhci, udev, command, true);
+	ret = xhci_evaluate_context(xhci, udev, command);
 	if (!ret) {
 		spin_lock_irqsave(&xhci->lock, flags);
 		virt_dev->current_mel = max_exit_latency;
@@ -5277,9 +5265,9 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
 	 * evaluate context command.
 	 */
 	if (xhci->hci_version > 0x95)
-		ret = xhci_configure_endpoint(xhci, hdev, config_cmd, false);
+		ret = xhci_configure_endpoint(xhci, hdev, config_cmd);
 	else
-		ret = xhci_evaluate_context(xhci, hdev, config_cmd, false);
+		ret = xhci_evaluate_context(xhci, hdev, config_cmd);
 
 	xhci_free_command(xhci, config_cmd);
 	return ret;
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 7c3cb7f3b0aa..d7f62b0b79f6 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1262,8 +1262,6 @@ static inline const char *xhci_trb_type_string(u8 type)
  * It must also be greater than 16.
  */
 #define TRBS_PER_SEGMENT	256
-/* Allow two commands + a link TRB, along with any reserved command TRBs */
-#define MAX_RSVD_CMD_TRBS	(TRBS_PER_SEGMENT - 3)
 #define TRB_SEGMENT_SIZE	(TRBS_PER_SEGMENT*16)
 #define TRB_SEGMENT_SHIFT	(ilog2(TRB_SEGMENT_SIZE))
 /* TRB buffer pointers can't cross 64KB boundaries */
@@ -1546,7 +1544,6 @@ struct xhci_hcd {
 #define CMD_RING_STATE_ABORTED         BIT(1)
 #define CMD_RING_STATE_STOPPED         BIT(2)
 	struct list_head        cmd_list;
-	unsigned int		cmd_ring_reserved_trbs;
 	struct delayed_work	cmd_timer;
 	struct completion	cmd_ring_stop_completion;
 	struct xhci_command	*current_cmd;
@@ -1937,15 +1934,14 @@ int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
 		struct urb *urb, int slot_id, unsigned int ep_index);
 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
-		struct xhci_command *cmd, dma_addr_t in_ctx_ptr, u32 slot_id,
-		bool command_must_succeed);
+		struct xhci_command *cmd, dma_addr_t in_ctx_ptr, u32 slot_id);
 int xhci_queue_get_port_bw(struct xhci_hcd *xhci,
 		struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
-		u8 dev_speed, bool command_must_succeed);
+		u8 dev_speed);
 int xhci_get_port_bandwidth(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx,
 		u8 dev_speed);
 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
-		dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed);
+		dma_addr_t in_ctx_ptr, u32 slot_id);
 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		int slot_id, unsigned int ep_index,
 		enum xhci_ep_reset_type reset_type);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx()
  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
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-01 19:47 UTC (permalink / raw)
  To: Niklas Neronin; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] usb: xhci: race condition in xhci_free_tt_info() [1]
--

commit e4be7ecc7f7cb6e246c200a24855015365ab6a17
Author: Niklas Neronin <niklas.neronin@linux.intel.com>

usb: xhci: simplify xhci_get_input_control_ctx()

This commit replaces the NULL return in xhci_get_input_control_ctx() with a
WARN_ON_ONCE() check and removes the corresponding NULL checks from all
callers.

> diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
> index 7f0d34c465f8d..de04a7b705f84 100644
> --- a/drivers/usb/host/xhci.c
> +++ b/drivers/usb/host/xhci.c

[ ... ]

> @@ -5267,12 +5198,6 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
>  		return -ENOMEM;
>  
>  	ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
> -	if (!ctrl_ctx) {
> -		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
> -				__func__);
> -		xhci_free_command(xhci, config_cmd);
> -		return -ENOMEM;
> -	}
>  
>  	spin_lock_irqsave(&xhci->lock, flags);
>  	if (hdev->speed == USB_SPEED_HIGH &&

[Severity: Critical]
This problem wasn't introduced by this patch, but can xhci_alloc_tt_info()
and xhci_free_tt_info() race and corrupt the root hub port's tts list
[1]?

xhci_update_hub_device() holds xhci->lock while xhci_alloc_tt_info() adds
entries to xhci->rh_bw[...].tts.  If another device on the same root hub
port concurrently fails setup or resets (for example via
xhci_disable_and_free_slot()), xhci_free_virt_device() calls
xhci_free_tt_info() without holding xhci->lock:

CPU0                                CPU1
xhci_update_hub_device()            xhci_disable_and_free_slot()
  spin_lock_irqsave(&xhci->lock)      xhci_free_virt_device()
  xhci_alloc_tt_info()                  xhci_free_tt_info()
    list_add(..., &rh_bw->tts)            list_del(&tt_info->tt_list)
                                            <-- list corruption
  spin_unlock_irqrestore(&xhci->lock)

The concurrent unlocked list_del() can corrupt the tts list pointers,
leading to a crash, infinite loop, or use-after-free during subsequent TT
bandwidth checks.

[1] https://sashiko.dev/#/bug/linux-3e30f7e2-c6ce-4541-8c1c-3eafceec7003

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201194134.265480-1-niklas.neronin@linux.intel.com?part=1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 5/7] usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails
  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
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-01 19:52 UTC (permalink / raw)
  To: Niklas Neronin; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] usb: xhci: missing Fixes trailer for xhci_free_streams() error
  cleanup
--

commit c360eaefc8b0f4f038713f3a4fb3c4d97cf53b19
Author: Niklas Neronin <niklas.neronin@linux.intel.com>

> usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails
>
> xhci_free_streams() sets EP_GETTING_NO_STREAMS on each endpoint before
> issuing the Configure Endpoint command that removes the streams. The
> flag is only cleared on success. If the command fails, either because
> it can't be queued or because the xHC rejects it, the function returns
> with the flag still set.
>
> xhci_urb_enqueue() rejects URBs for endpoints that have
> EP_GETTING_NO_STREAMS set, so after a failed free_streams call the
> endpoint can't be used until the device is reconfigured or reset.
>
> Clear EP_GETTING_NO_STREAMS on the error path. The streams rings and
> EP_HAS_STREAMS are left untouched, so the endpoint stays in its
> previous streams state and the caller can retry.
>
> Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>

[Severity: Low]
This fixes an error-handling omission from the original xhci_free_streams()
implementation. Should this include:

  Fixes: 8df75f42f8e6 ("USB: xhci: Add memory allocation for USB3 bulk streams.")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201194134.265480-1-niklas.neronin@linux.intel.com?part=5

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/7] usb: xhci: command queueing cleanups
  2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
                   ` (6 preceding siblings ...)
  2026-12-01 19:41 ` [PATCH 7/7] usb: xhci: remove command ring TRB reservation Niklas Neronin
@ 2026-12-01 19:52 ` sashiko-bot
  7 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-01 19:52 UTC (permalink / raw)
  To: Niklas Neronin; +Cc: linux-usb

Sashiko has finished reviewing this 7-patch series:

- Patches 2-4, 6, and 7: no issues found.
- Patch 1 ("usb: xhci: simplify xhci_get_input_control_ctx()"): 1
  related pre-existing bug noted
- Patch 5 ("usb: xhci: clear EP_GETTING_NO_STREAMS when freeing..."): 1
  potential issue (1 Low)

Detailed reviews have been sent as separate replies to patches 1 and 5.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201194134.265480-1-niklas.neronin@linux.intel.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-01 19:52 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 6/7] usb: xhci: remove pre-allocated free streams command Niklas Neronin
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox