From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-usb@vger.kernel.org
Cc: Alan Stern <stern@rowland.harvard.edu>,
linux-kernel@vger.kernel.org,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Josef Bacik <josef@toxicpanda.com>, stable <stable@kernel.org>,
Peter Chen <peter.chen@kernel.org>,
Pawel Laszczak <pawell@cadence.com>,
Roger Quadros <rogerq@kernel.org>
Subject: [PATCH 05/19] usb: cdns3: switch the role unlocked on resume
Date: Tue, 27 Oct 2026 15:57:45 +0100 [thread overview]
Message-ID: <20261027-llm-usb-v1-5-b9751ddb9c7c@linuxfoundation.org> (raw)
In-Reply-To: <20261027-llm-usb-v1-0-b9751ddb9c7c@linuxfoundation.org>
From: Josef Bacik <josef@toxicpanda.com>
cdns3_controller_resume() calls cdns_resume() with cdns->lock held and
interrupts off. If the controller has lost its power during suspend and
the role which the ID and VBUS inputs ask for is not the current one any
more, cdns_resume() calls cdns_hw_role_switch(). That stops one role
and starts another, which takes a mutex, allocates memory with
GFP_KERNEL, requests interrupts and registers or unregisters the xHCI
platform device or the UDC:
BUG: sleeping function called from invalid context
To get there, change the cable of a dual role port without a role switch
device while the system is asleep, on a SoC which powers the controller
down.
Move the switch to a function of its own, cdns_resume_role(), which is
called before the lock is taken. What remains of cdns_resume() is still
called with the lock held, and skipped if the role has been switched or
the switch has failed, as before.
cdns_hw_role_switch() also calls pm_runtime_get_sync() for the
controller. That cannot work from its own runtime resume callback, where
it would wait for itself, so the role is only switched on system resume
now. After a runtime resume the old role is resumed, like after a
suspend without loss of power.
Fixes: 2cf2581cd229 ("usb: cdns3: add power lost support for system resume")
Cc: stable <stable@kernel.org>
Cc: Peter Chen <peter.chen@kernel.org>
Cc: Pawel Laszczak <pawell@cadence.com>
Cc: Roger Quadros <rogerq@kernel.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/usb/cdns3/cdns3-plat.c | 8 ++++--
drivers/usb/cdns3/core.c | 55 ++++++++++++++++++++++++++----------------
drivers/usb/cdns3/core.h | 3 +++
3 files changed, 43 insertions(+), 23 deletions(-)
diff --git a/drivers/usb/cdns3/cdns3-plat.c b/drivers/usb/cdns3/cdns3-plat.c
index e3f32c3e9535..674625cdd4d4 100644
--- a/drivers/usb/cdns3/cdns3-plat.c
+++ b/drivers/usb/cdns3/cdns3-plat.c
@@ -268,7 +268,7 @@ static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
{
struct cdns *cdns = dev_get_drvdata(dev);
- int ret;
+ int ret, role_changed;
unsigned long flags;
if (!cdns->in_lpm)
@@ -292,8 +292,12 @@ static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
cdns3_set_platform_suspend(cdns->dev, false, false);
+ /* It resumes the device, which cannot be done from runtime resume */
+ role_changed = PMSG_IS_AUTO(msg) ? 0 : cdns_resume_role(cdns);
+
spin_lock_irqsave(&cdns->lock, flags);
- cdns_resume(cdns);
+ if (!role_changed)
+ cdns_resume(cdns);
cdns->in_lpm = false;
spin_unlock_irqrestore(&cdns->lock, flags);
cdns_set_active(cdns, !PMSG_IS_AUTO(msg));
diff --git a/drivers/usb/cdns3/core.c b/drivers/usb/cdns3/core.c
index 87122fb5bdb8..3b2b8f231a92 100644
--- a/drivers/usb/cdns3/core.c
+++ b/drivers/usb/cdns3/core.c
@@ -504,36 +504,49 @@ int cdns_suspend(struct cdns *cdns)
}
EXPORT_SYMBOL_GPL(cdns_suspend);
+/**
+ * cdns_resume_role - switch the role if it has changed while the power was off
+ * @cdns: controller
+ *
+ * Stopping and starting a role sleeps, so this has to be called before
+ * cdns->lock is taken for cdns_resume().
+ *
+ * Returns 1 if the role has been switched: it has been started from scratch
+ * and must not be resumed. 0 if there was nothing to do, negative errno if the
+ * switch has failed.
+ */
+int cdns_resume_role(struct cdns *cdns)
+{
+ int ret;
+
+ if (cdns->role_sw || !cdns_power_is_lost(cdns))
+ return 0;
+
+ if (cdns_hw_role_state_machine(cdns) == cdns->role)
+ return 0;
+
+ ret = cdns_hw_role_switch(cdns);
+
+ return ret ? ret : 1;
+}
+EXPORT_SYMBOL_GPL(cdns_resume_role);
+
int cdns_resume(struct cdns *cdns)
{
bool power_lost = cdns_power_is_lost(cdns);
- enum usb_role real_role;
- bool role_changed = false;
int ret = 0;
if (power_lost) {
- if (!cdns->role_sw) {
- real_role = cdns_hw_role_state_machine(cdns);
- if (real_role != cdns->role) {
- ret = cdns_hw_role_switch(cdns);
- if (ret)
- return ret;
- role_changed = true;
- }
- }
+ if (cdns->role == USB_ROLE_HOST)
+ ret = cdns_drd_host_on(cdns);
+ else if (cdns->role == USB_ROLE_DEVICE)
+ ret = cdns_drd_gadget_on(cdns);
- if (!role_changed) {
- if (cdns->role == USB_ROLE_HOST)
- ret = cdns_drd_host_on(cdns);
- else if (cdns->role == USB_ROLE_DEVICE)
- ret = cdns_drd_gadget_on(cdns);
-
- if (ret)
- return ret;
- }
+ if (ret)
+ return ret;
}
- if (!role_changed && cdns->roles[cdns->role]->resume)
+ if (cdns->roles[cdns->role]->resume)
cdns->roles[cdns->role]->resume(cdns, power_lost);
return 0;
diff --git a/drivers/usb/cdns3/core.h b/drivers/usb/cdns3/core.h
index 8c492fda924c..cfc3be9ddf9e 100644
--- a/drivers/usb/cdns3/core.h
+++ b/drivers/usb/cdns3/core.h
@@ -136,10 +136,13 @@ int cdns_remove(struct cdns *cdns);
int cdns_core_init_role(struct cdns *cdns);
#ifdef CONFIG_PM_SLEEP
+int cdns_resume_role(struct cdns *cdns);
int cdns_resume(struct cdns *cdns);
int cdns_suspend(struct cdns *cdns);
void cdns_set_active(struct cdns *cdns, u8 set_active);
#else /* CONFIG_PM_SLEEP */
+static inline int cdns_resume_role(struct cdns *cdns)
+{ return 0; }
static inline int cdns_resume(struct cdns *cdns)
{ return 0; }
static inline void cdns_set_active(struct cdns *cdns, u8 set_active) { }
--
2.56.0
next prev parent reply other threads:[~2026-10-27 14:58 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 14:57 [PATCH 00/19] USB: a bunch of llm-found fixes Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 01/19] usb: core: sysfs: take the port lock to read the BOS Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 02/19] USB: core: signal the end of an sg request last Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 03/19] usbip: vudc: free the unlink item if the send fails Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 04/19] usb: host: oxu210hp-hcd: fix oxu_qh_alloc() deadlock Greg Kroah-Hartman
2026-10-27 14:57 ` Greg Kroah-Hartman [this message]
2026-10-27 14:57 ` [PATCH 06/19] usb: gadget: snps_udc_plat: lock out the interrupt Greg Kroah-Hartman
2026-10-27 18:17 ` Herve Codina
2026-10-27 14:57 ` [PATCH 07/19] usb: gadget: bdc: the resume callback is optional Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 08/19] usb: gadget: uvc: fix oops on a premature STREAMON Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 09/19] usb: gadget: f_fs: let the request keep its fence Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 10/19] usb: host: ehci-orion: fix the probe without DT Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 11/19] usb: typec: tcpm: don't wait for the VDM discovery work Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 12/19] usb: typec: tcpci: don't act on unread alerts Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 13/19] usb: typec: anx7411: zero on-stack alt mode descriptors Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 14/19] usb: typec: mux: it5205: fix oops on accessory modes Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 15/19] usb: typec: mux: tusb1046: " Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 16/19] usb: renesas_usbhs: link URBs with interrupts disabled Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 17/19] usb: renesas_usbhs: unlink the URB if it is not queued Greg Kroah-Hartman
2026-10-27 14:57 ` [PATCH 18/19] usb: isp1760: wait for the TT clear in endpoint_disable Greg Kroah-Hartman
2026-10-27 15:21 ` Rui Miguel Silva
2026-10-27 14:57 ` [PATCH 19/19] usb: dwc2: " Greg Kroah-Hartman
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=20261027-llm-usb-v1-5-b9751ddb9c7c@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=josef@toxicpanda.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=pawell@cadence.com \
--cc=peter.chen@kernel.org \
--cc=rogerq@kernel.org \
--cc=stable@kernel.org \
--cc=stern@rowland.harvard.edu \
/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
all inboxes | Powered by JetHome®