All of lore.kernel.org
 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 1/7] usb: xhci: simplify xhci_get_input_control_ctx()
Date: Tue,  1 Dec 2026 20:41:28 +0100	[thread overview]
Message-ID: <20261201194134.265480-2-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20261201194134.265480-1-niklas.neronin@linux.intel.com>

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


  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 ` Niklas Neronin [this message]
2026-12-01 19:47   ` [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx() 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

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-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.