Linux USB
 help / color / mirror / Atom feed
From: Guoming Chen <chenguoming88s@gmail.com>
To: Peter Chen <peter.chen@kernel.org>,
	Pawel Laszczak <pawell@cadence.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Roger Quadros" <rogerq@kernel.org>,
	"Théo Lebrun" <theo.lebrun@bootlin.com>,
	linux-usb@vger.kernel.org
Subject: [PATCH v5] usb: cdns3: fix NULL dereference in cdns_host_resume()
Date: Wed, 25 Nov 2026 17:01:15 +0800	[thread overview]
Message-ID: <20261125090115.2993-1-chenguoming88s@gmail.com> (raw)
In-Reply-To: <20261123075116.3868-1-chenguoming88s@gmail.com>

cdns_host_resume() dereferences the xhci-hcd child's drvdata without
checking it, and both the child and its drvdata can be NULL:

- cdns->host_dev: cdns_role_start() sets cdns->role before calling the
  role's start callback and keeps it on failure, so the next resume
  still calls cdns_host_resume() after a failed host start.

- the child's drvdata: __cdns_host_init() creates the child, but
  xhci-plat sets the drvdata only in its probe.  The parent is resumed
  before that, by __driver_probe_device() when the child is first
  probed and by a system resume in between.

Commit 87e4b043b98a ("usb: cdns3: fix role switching during resume")
only covers a role change during resume.

Check both and return early; the child's probe initializes the
controller from scratch, so power_lost has nothing to act on yet.
Publish cdns->host_dev only after platform_device_add() succeeds, so
the error paths leave it NULL instead of pointing at a freed device.

The resume-before-probe case oopses as:

  pc : usb_hcd_is_primary_hcd+0x0/0x1c
  lr : cdns_host_resume+0x24/0x44
  Call trace:
   usb_hcd_is_primary_hcd+0x0/0x1c
   cdns_resume+0x50/0xdc
   cdnsp_controller_resume.isra.0+0x14c/0x198
   cdnsp_plat_runtime_resume+0x20/0x10c
   ...
   __pm_runtime_resume+0x4c/0x90
   __driver_probe_device+0x64/0x1a0
   xhci_plat_init+0x44/0x50

Fixes: 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")
Cc: stable@vger.kernel.org
Signed-off-by: Guoming Chen <chenguoming88s@gmail.com>
---
Changes in v5:
 - Rewrite the commit message around the two NULL checks and their
   exact causes; add the oops call trace.  No functional changes.
Changes in v4:
 - Assign cdns->host_dev only after platform_device_add() succeeds, as
   reported by Sashiko; otherwise the error paths leave a dangling
   pointer.
 - Cc stable@vger.kernel.org, mention the system-wide resume path, and
   shorten the commit message.
Changes in v3:
 - Add the changelog below the tear line.
 - Clarify that 87e4b043b98a covers only the role-switch variant, and
   correct the description of when cdns->host_dev can be NULL.  No
   functional changes.
Changes in v2:
 - Resend with the correct sender address; the patch is unchanged from
   v1.
---
 drivers/usb/cdns3/host.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/cdns3/host.c b/drivers/usb/cdns3/host.c
index e01264dc625d..b0269c21e339 100644
--- a/drivers/usb/cdns3/host.c
+++ b/drivers/usb/cdns3/host.c
@@ -81,7 +81,6 @@ static int __cdns_host_init(struct cdns *cdns)
 	}
 
 	xhci->dev.parent = cdns->dev;
-	cdns->host_dev = xhci;
 
 	ret = platform_device_add_resources(xhci, cdns->xhci_res,
 					    CDNS_XHCI_RESOURCES_NUM);
@@ -116,6 +115,13 @@ static int __cdns_host_init(struct cdns *cdns)
 		goto free_memory;
 	}
 
+	/*
+	 * Publish the child only once it has been added: the error paths
+	 * above free it, and a stale cdns->host_dev would be dereferenced
+	 * by the next resume.
+	 */
+	cdns->host_dev = xhci;
+
 	/* Glue needs to access xHCI region register for Power management */
 	hcd = platform_get_drvdata(xhci);
 	if (hcd)
@@ -140,9 +146,23 @@ static void cdns_host_exit(struct cdns *cdns)
 
 static int cdns_host_resume(struct cdns *cdns, bool power_lost)
 {
-	struct usb_hcd *hcd = platform_get_drvdata(cdns->host_dev);
-	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
-
+	struct usb_hcd *hcd;
+	struct xhci_plat_priv *priv;
+
+	/*
+	 * The xhci-hcd child may not be bound yet: cdns3 creates it, but
+	 * xhci-plat probes it only later, and the drvdata is set in that
+	 * probe.  cdns->host_dev itself can be NULL, e.g. when the host
+	 * role failed to start.
+	 */
+	if (!cdns->host_dev)
+		return 0;
+
+	hcd = platform_get_drvdata(cdns->host_dev);
+	if (!hcd)
+		return 0;
+
+	priv = hcd_to_xhci_priv(hcd);
 	priv->power_lost = power_lost;
 
 	return 0;
-- 
2.34.1


  parent reply	other threads:[~2026-11-25  9:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:43 [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume() Guomin chen
2026-11-23  4:56 ` sashiko-bot
2026-11-23  7:51 ` [PATCH v4] " Guomin chen
2026-11-23  8:05   ` sashiko-bot
2026-11-23 11:07   ` Théo Lebrun
2026-11-24 14:20     ` guoming chen
2026-11-25  9:01   ` Guoming Chen [this message]
2026-11-25  9:11     ` [PATCH v5] " 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=20261125090115.2993-1-chenguoming88s@gmail.com \
    --to=chenguoming88s@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pawell@cadence.com \
    --cc=peter.chen@kernel.org \
    --cc=rogerq@kernel.org \
    --cc=theo.lebrun@bootlin.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