From nobody Fri Apr 3 02:58:18 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19E29353EE0 for ; Tue, 24 Mar 2026 20:39:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774384744; cv=none; b=KR/TWa2AAxFzmsBZPW2qpB+fmaE5/5uvSoiwcU18EvSNxWeCAgAF6sIJa05x15sX5JTElA2bFjut1B/y6n4Sj7irFFHk8gUfWJbenRuodDhez1diktFxFH2CKWb5+rE6ukePIz610JoPMG2CrrZ4jRxI41g6aNpjjBbgGbANN6A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774384744; c=relaxed/simple; bh=ui8z7EgL6T4YJLaYi5pgyedUFLK7y1/+PZfCWJp3y6k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=M787rm14QbIixNAOtdPDNESDjE4dl16PFq0D2uBr/epNV1rNFOxwqTRSFo2GRBtt390FZ0G5i6QQKb4ofnH0f7jhCx0wju/7fuAxFvC59wHmKgiqLlQ3ARn1UKerUyENWFpJsaa4xB6Ga+3D3CgK6NbzTtEUTAsKNvnXrkcAwIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=d9bi4ePT; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="d9bi4ePT" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2adef9d486bso50857525ad.2 for ; Tue, 24 Mar 2026 13:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774384741; x=1774989541; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qwU4aXxWSI1Yibx5n5U5XqPUOhxeAgF8tQCq/SJZEJ4=; b=d9bi4ePTowDIWhSBavAAhu9P6H30G5qN/Mry3uZW28/3i8saiM4o7bqEjVzvBscvyw DOz6oqOVRwDD5730xPIJSwhtH7hjsxXjxSO3+tzeL6EDdpkdy2eipjRTGDqcRB3d8zWD BeOG5/uWy8eBdvScpkUiHYtajWzdUQpA8TA8zmSStNy/XqmSv0+zO9TaiSDCmc2tb4j3 ynMnxP8EGwaqyQScMkxWypYjZt1GlOh7/yw5ApnERzcYam+tpVIaWwcO43/SYZTIScOB SsdxgYZSiQBncjiK/1aNE3jrHw0g/mdg+NQWfXPqib6q3g3kCI9COCCsEXGgSoqJfFnJ k0Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774384741; x=1774989541; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qwU4aXxWSI1Yibx5n5U5XqPUOhxeAgF8tQCq/SJZEJ4=; b=q8mIh6wBtB5D/NQUfYiyWrnHifNLIVy+Nz5mbAoAO5xSWEpnD1nf0yWQFvFi15Xi5m tiOn+NXj8GIWHm9zZBY35yNUZb+w8AvwKCLTOMxeyrcAkBA9++F1y1RCfOb/BfC4Z6TZ X3wDAirpZeenEElgNGfg0E2O/9DyFryH++LECvpY+FFl+cjjRkuQ+izn3m1wdyTbdbZW w1wqYyhIsNSnT6jjQ1H8wvTqRYTEdetLc1GDmluSNHOxMdnNGpQeposm32Pl/fXEwaCI EZEC7ULNzpobqhiEiO0e/ji3onn+JF888lm7kZyAhizonb88G52s7wpWeP8t3IDOdtin H6Kg== X-Forwarded-Encrypted: i=1; AJvYcCWTpTcEXhYNO7MEZd3JaTS5yNM4IXTXF9mWRbxXwbYz4HGLTCKI49UK8ttYIOedk3eJu2IUE1bh74ILrmk=@vger.kernel.org X-Gm-Message-State: AOJu0Yyfjj84HIhdpv+PCoypn2QEzGINFzusC3O94z+Y11hspIFNdOiF Knr8EfdFqYasLJFd1QbRgcTLyff5Bs58oK9QpW9kR4e1Dr/NNHCaV2Us1nqxKRKLG+KqNEubjdp xKD/pXXADfriv7sgvVw== X-Received: from plq8.prod.google.com ([2002:a17:903:2f88:b0:2b0:6928:c3aa]) (user=guanyulin job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e848:b0:2b0:62dd:3a87 with SMTP id d9443c01a7336-2b0b09a718cmr11214625ad.10.1774384741330; Tue, 24 Mar 2026 13:39:01 -0700 (PDT) Date: Tue, 24 Mar 2026 20:38:09 +0000 In-Reply-To: <20260324203851.4091193-1-guanyulin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260324203851.4091193-1-guanyulin@google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog Message-ID: <20260324203851.4091193-2-guanyulin@google.com> Subject: [PATCH v3 1/2] usb: core: use dedicated spinlock for offload state From: Guan-Yu Lin To: gregkh@linuxfoundation.org, mathias.nyman@intel.com, perex@perex.cz, tiwai@suse.com, quic_wcheng@quicinc.com, broonie@kernel.org, arnd@arndb.de, xiaopei01@kylinos.cn, wesley.cheng@oss.qualcomm.com, hannelotta@gmail.com, sakari.ailus@linux.intel.com, eadavis@qq.com, stern@rowland.harvard.edu, amardeep.rai@intel.com, xu.yang_2@nxp.com, andriy.shevchenko@linux.intel.com, nkapron@google.com Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sound@vger.kernel.org, Guan-Yu Lin , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the coarse USB device lock with a dedicated offload_lock spinlock to reduce contention during offload operations. Use offload_pm_locked to synchronize with PM transitions and replace the legacy offload_at_suspend flag. Optimize usb_offload_get/put by switching from auto-resume/suspend to pm_runtime_get_if_active(). This ensures offload state is only modified when the device is already active, avoiding unnecessary power transitions. Cc: stable@vger.kernel.org Fixes: ef82a4803aab ("xhci: sideband: add api to trace sideband usage") Signed-off-by: Guan-Yu Lin Tested-by: hailong.liu@oppo.com --- drivers/usb/core/driver.c | 23 ++++--- drivers/usb/core/offload.c | 107 ++++++++++++++++++------------- drivers/usb/core/usb.c | 1 + drivers/usb/host/xhci-sideband.c | 4 +- include/linux/usb.h | 10 ++- 5 files changed, 89 insertions(+), 56 deletions(-) diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index d29edc7c616a..74b8bdc27dbf 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c @@ -1415,14 +1415,16 @@ static int usb_suspend_both(struct usb_device *udev= , pm_message_t msg) int status =3D 0; int i =3D 0, n =3D 0; struct usb_interface *intf; + bool offload_active =3D false; =20 if (udev->state =3D=3D USB_STATE_NOTATTACHED || udev->state =3D=3D USB_STATE_SUSPENDED) goto done; =20 + usb_offload_set_pm_locked(udev, true); if (msg.event =3D=3D PM_EVENT_SUSPEND && usb_offload_check(udev)) { dev_dbg(&udev->dev, "device offloaded, skip suspend.\n"); - udev->offload_at_suspend =3D 1; + offload_active =3D true; } =20 /* Suspend all the interfaces and then udev itself */ @@ -1436,8 +1438,7 @@ static int usb_suspend_both(struct usb_device *udev, = pm_message_t msg) * interrupt urbs, allowing interrupt events to be * handled during system suspend. */ - if (udev->offload_at_suspend && - intf->needs_remote_wakeup) { + if (offload_active && intf->needs_remote_wakeup) { dev_dbg(&intf->dev, "device offloaded, skip suspend.\n"); continue; @@ -1452,7 +1453,7 @@ static int usb_suspend_both(struct usb_device *udev, = pm_message_t msg) } } if (status =3D=3D 0) { - if (!udev->offload_at_suspend) + if (!offload_active) status =3D usb_suspend_device(udev, msg); =20 /* @@ -1498,7 +1499,7 @@ static int usb_suspend_both(struct usb_device *udev, = pm_message_t msg) */ } else { udev->can_submit =3D 0; - if (!udev->offload_at_suspend) { + if (!offload_active) { for (i =3D 0; i < 16; ++i) { usb_hcd_flush_endpoint(udev, udev->ep_out[i]); usb_hcd_flush_endpoint(udev, udev->ep_in[i]); @@ -1507,6 +1508,8 @@ static int usb_suspend_both(struct usb_device *udev, = pm_message_t msg) } =20 done: + if (status !=3D 0) + usb_offload_set_pm_locked(udev, false); dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); return status; } @@ -1536,16 +1539,19 @@ static int usb_resume_both(struct usb_device *udev,= pm_message_t msg) int status =3D 0; int i; struct usb_interface *intf; + bool offload_active =3D false; =20 if (udev->state =3D=3D USB_STATE_NOTATTACHED) { status =3D -ENODEV; goto done; } udev->can_submit =3D 1; + if (msg.event =3D=3D PM_EVENT_RESUME) + offload_active =3D usb_offload_check(udev); =20 /* Resume the device */ if (udev->state =3D=3D USB_STATE_SUSPENDED || udev->reset_resume) { - if (!udev->offload_at_suspend) + if (!offload_active) status =3D usb_resume_device(udev, msg); else dev_dbg(&udev->dev, @@ -1562,8 +1568,7 @@ static int usb_resume_both(struct usb_device *udev, p= m_message_t msg) * pending interrupt urbs, allowing interrupt events * to be handled during system suspend. */ - if (udev->offload_at_suspend && - intf->needs_remote_wakeup) { + if (offload_active && intf->needs_remote_wakeup) { dev_dbg(&intf->dev, "device offloaded, skip resume.\n"); continue; @@ -1572,11 +1577,11 @@ static int usb_resume_both(struct usb_device *udev,= pm_message_t msg) udev->reset_resume); } } - udev->offload_at_suspend =3D 0; usb_mark_last_busy(udev); =20 done: dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); + usb_offload_set_pm_locked(udev, false); if (!status) udev->reset_resume =3D 0; return status; diff --git a/drivers/usb/core/offload.c b/drivers/usb/core/offload.c index 7c699f1b8d2b..c24945294d7e 100644 --- a/drivers/usb/core/offload.c +++ b/drivers/usb/core/offload.c @@ -25,33 +25,30 @@ */ int usb_offload_get(struct usb_device *udev) { - int ret; + int ret =3D 0; =20 - usb_lock_device(udev); - if (udev->state =3D=3D USB_STATE_NOTATTACHED) { - usb_unlock_device(udev); + if (!usb_get_dev(udev)) return -ENODEV; - } =20 - if (udev->state =3D=3D USB_STATE_SUSPENDED || - udev->offload_at_suspend) { - usb_unlock_device(udev); - return -EBUSY; + if (pm_runtime_get_if_active(&udev->dev) !=3D 1) { + ret =3D -EBUSY; + goto err_rpm; } =20 - /* - * offload_usage could only be modified when the device is active, since - * it will alter the suspend flow of the device. - */ - ret =3D usb_autoresume_device(udev); - if (ret < 0) { - usb_unlock_device(udev); - return ret; + spin_lock(&udev->offload_lock); + + if (udev->offload_pm_locked) { + ret =3D -EAGAIN; + goto err; } =20 udev->offload_usage++; - usb_autosuspend_device(udev); - usb_unlock_device(udev); + +err: + spin_unlock(&udev->offload_lock); + pm_runtime_put_autosuspend(&udev->dev); +err_rpm: + usb_put_dev(udev); =20 return ret; } @@ -69,35 +66,32 @@ EXPORT_SYMBOL_GPL(usb_offload_get); */ int usb_offload_put(struct usb_device *udev) { - int ret; + int ret =3D 0; =20 - usb_lock_device(udev); - if (udev->state =3D=3D USB_STATE_NOTATTACHED) { - usb_unlock_device(udev); + if (!usb_get_dev(udev)) return -ENODEV; - } =20 - if (udev->state =3D=3D USB_STATE_SUSPENDED || - udev->offload_at_suspend) { - usb_unlock_device(udev); - return -EBUSY; + if (pm_runtime_get_if_active(&udev->dev) !=3D 1) { + ret =3D -EBUSY; + goto err_rpm; } =20 - /* - * offload_usage could only be modified when the device is active, since - * it will alter the suspend flow of the device. - */ - ret =3D usb_autoresume_device(udev); - if (ret < 0) { - usb_unlock_device(udev); - return ret; + spin_lock(&udev->offload_lock); + + if (udev->offload_pm_locked) { + ret =3D -EAGAIN; + goto err; } =20 /* Drop the count when it wasn't 0, ignore the operation otherwise. */ if (udev->offload_usage) udev->offload_usage--; - usb_autosuspend_device(udev); - usb_unlock_device(udev); + +err: + spin_unlock(&udev->offload_lock); + pm_runtime_put_autosuspend(&udev->dev); +err_rpm: + usb_put_dev(udev); =20 return ret; } @@ -112,25 +106,52 @@ EXPORT_SYMBOL_GPL(usb_offload_put); * management. * * The caller must hold @udev's device lock. In addition, the caller should - * ensure downstream usb devices are all either suspended or marked as - * "offload_at_suspend" to ensure the correctness of the return value. + * ensure downstream usb devices are all marked as "offload_pm_locked" to + * ensure the correctness of the return value. * * Returns true on any offload activity, false otherwise. */ bool usb_offload_check(struct usb_device *udev) __must_hold(&udev->dev->mu= tex) { struct usb_device *child; - bool active; + bool active =3D false; int port1; =20 + spin_lock(&udev->offload_lock); + if (udev->offload_usage) + active =3D true; + spin_unlock(&udev->offload_lock); + + if (active) + return true; + usb_hub_for_each_child(udev, port1, child) { usb_lock_device(child); active =3D usb_offload_check(child); usb_unlock_device(child); + if (active) - return true; + break; } =20 - return !!udev->offload_usage; + return active; } EXPORT_SYMBOL_GPL(usb_offload_check); + +/** + * usb_offload_set_pm_locked - set the PM lock state of a USB device + * @udev: the USB device to modify + * @locked: the new lock state + * + * Setting @locked to true prevents offload_usage from being modified. This + * ensures that offload activities cannot be started or stopped during cri= tical + * power management transitions, maintaining a stable state for the durati= on + * of the transition. + */ +void usb_offload_set_pm_locked(struct usb_device *udev, bool locked) +{ + spin_lock(&udev->offload_lock); + udev->offload_pm_locked =3D locked; + spin_unlock(&udev->offload_lock); +} +EXPORT_SYMBOL_GPL(usb_offload_set_pm_locked); diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c index e740f7852bcd..8f7ca084010f 100644 --- a/drivers/usb/core/usb.c +++ b/drivers/usb/core/usb.c @@ -671,6 +671,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *par= ent, set_dev_node(&dev->dev, dev_to_node(bus->sysdev)); dev->state =3D USB_STATE_ATTACHED; dev->lpm_disable_count =3D 1; + spin_lock_init(&dev->offload_lock); dev->offload_usage =3D 0; atomic_set(&dev->urbnum, 0); =20 diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideb= and.c index 2bd77255032b..1ddb64b0a48e 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -291,8 +291,8 @@ EXPORT_SYMBOL_GPL(xhci_sideband_get_event_buffer); * Allow other drivers, such as usb controller driver, to check if there a= re * any sideband activity on the host controller. This information could be= used * for power management or other forms of resource management. The caller = should - * ensure downstream usb devices are all either suspended or marked as - * "offload_at_suspend" to ensure the correctness of the return value. + * ensure downstream usb devices are all marked as "offload_pm_locked" to = ensure + * the correctness of the return value. * * Returns true on any active sideband existence, false otherwise. */ diff --git a/include/linux/usb.h b/include/linux/usb.h index fbfcc70b07fb..a4b031196da3 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -21,6 +21,7 @@ #include /* for struct completion */ #include /* for current && schedule_timeout */ #include /* for struct mutex */ +#include /* for spinlock_t */ #include /* for runtime PM */ =20 struct usb_device; @@ -636,8 +637,9 @@ struct usb3_lpm_parameters { * @do_remote_wakeup: remote wakeup should be enabled * @reset_resume: needs reset instead of resume * @port_is_suspended: the upstream port is suspended (L2 or U3) - * @offload_at_suspend: offload activities during suspend is enabled. + * @offload_pm_locked: prevents offload_usage changes during PM transition= s. * @offload_usage: number of offload activities happening on this usb devi= ce. + * @offload_lock: protects offload_usage and offload_pm_locked * @slot_id: Slot ID assigned by xHCI * @l1_params: best effor service latency for USB2 L1 LPM state, and L1 ti= meout. * @u1_params: exit latencies for USB3 U1 LPM state, and hub-initiated tim= eout. @@ -726,8 +728,9 @@ struct usb_device { unsigned do_remote_wakeup:1; unsigned reset_resume:1; unsigned port_is_suspended:1; - unsigned offload_at_suspend:1; + unsigned offload_pm_locked:1; int offload_usage; + spinlock_t offload_lock; enum usb_link_tunnel_mode tunnel_mode; struct device_link *usb4_link; =20 @@ -849,6 +852,7 @@ static inline void usb_mark_last_busy(struct usb_device= *udev) int usb_offload_get(struct usb_device *udev); int usb_offload_put(struct usb_device *udev); bool usb_offload_check(struct usb_device *udev); +void usb_offload_set_pm_locked(struct usb_device *udev, bool locked); #else =20 static inline int usb_offload_get(struct usb_device *udev) @@ -857,6 +861,8 @@ static inline int usb_offload_put(struct usb_device *ud= ev) { return 0; } static inline bool usb_offload_check(struct usb_device *udev) { return false; } +static inline void usb_offload_set_pm_locked(struct usb_device *udev, bool= locked) +{ } #endif =20 extern int usb_disable_lpm(struct usb_device *udev); --=20 2.53.0.1018.g2bb0e51243-goog From nobody Fri Apr 3 02:58:18 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6978035504D for ; Tue, 24 Mar 2026 20:39:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774384746; cv=none; b=fZHbW3L5x+ao+J9WtopitV+F9YRryaD0RgjsLLB6vtuSkyaQcwIhw3SAo7necAjpyaU73qDMYhdVSC8vYjvL8XTkdLdBDKRejxnRCnasNYNGRy+7QI47banhgHKTrKO2bER2T3mO0bDzpf38ObAwd9tYTKEnO++BvdeE1QRYsmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774384746; c=relaxed/simple; bh=6Ck3L+CE5zCcBRuao0IdjzcnQd7HU4crf4dPRSbvMKw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LwQKMD0St6uCw91VRJtzWc93YqQrGjbjmKHm4IvxrGeXy7A9pFBaPizZ7KjH4lOuMFgOmaZMdWS4ZW4vDjjf5Z42nxL3/xsm6cXe6VE6kML5BC79yShxKf4L1YlSM10wz+svctBt35Hl4IlEN6DhnydfdxNvCer8Vu290eqvwCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Io/UBixT; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--guanyulin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Io/UBixT" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c738b1f630eso23238474a12.2 for ; Tue, 24 Mar 2026 13:39:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774384745; x=1774989545; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OL+7kQbp5Bd7JEAMiGmzSMDg6A6JNXw/2f9uUU+JDmw=; b=Io/UBixTBsWof4aB50JqkjQOoeglK3KlMjhMFQsx5n4pzQC9tklyg91ZVSsKh4nmjp PGufXh42HXIuxFxc6x6C16CYWIQA0RNsr/2LPGr15sHeQv8g0pS6sHDxa0nxjjiW0qjM wBOC58duUHiEajkZ9//M2Aallc0NOPMFjI1Mm0CTy7SkBhWSX7HunPMRKlC2T3soQLYX 7cuepZjDWn+sH9Od/32+BZ5LqGtcT5SRDIAlkqkUvua10KLS+0MW/tnaM2Yq2dtW6Vvd Q5tFkeh90jyiCieBGdmjGSyhXEEjRnSqICAJ7sA0LrBzF/Undrj+DgNFqNhWls2Jkk3/ Dwvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774384745; x=1774989545; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OL+7kQbp5Bd7JEAMiGmzSMDg6A6JNXw/2f9uUU+JDmw=; b=Qa+opqmFqhcYtuWVu9rLhtunuNukFV690A5lzvj3OyjnP18rL/GGKQ1qc6ftIYVA/7 HUoZAlwKOXwH4nY7Bb1nufeaq8ol0J7qdtxRj+LNV4Rgp+wMUsG+6p70jqPzvo61vB84 M1fe+MMirVKXXBBx1uRVir+taZyUS8ZkwsTAfIrKsyFkDayTBYoBNG5MD3g41pj7wY+d 2Id/qxmHORDfoEMOAgBavDZ7BRPP59z0sEdTY0F59jEiyDDObTq9D4Ti+gdxHGPTDAku qidGvtfVrYA4hag3XQhRYuV4R2NOREjv4subcDUtC2ED/ryhfSvc25csbxl2Y7V8J0bS OlmQ== X-Forwarded-Encrypted: i=1; AJvYcCXF66X5QT4fLPNBdJ7Eo4jp1jnUjYbjEx2MyCF9jGvZgZzXnz+0uqEP16hB2YwoRj8/ResLfUBtM/rkuz8=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5k2Yo/ShdX1b4uhPBElX9TMUisTT2/Gn1xIFwhSKRNjqjq370 MXwMmjMwwde0O/lKTDLdzsCY+MJ0Jzgjw1NDBh4hDIQkeSbbGP/mmtvzObWl2JQ19gfde/ylLZK aWW/4dysYdw7HVHS14Q== X-Received: from pgbfq3.prod.google.com ([2002:a05:6a02:2983:b0:c76:5844:e6da]) (user=guanyulin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3388:b0:398:9466:2ee8 with SMTP id adf61e73a8af0-39c4abb6ac3mr1138240637.7.1774384744541; Tue, 24 Mar 2026 13:39:04 -0700 (PDT) Date: Tue, 24 Mar 2026 20:38:10 +0000 In-Reply-To: <20260324203851.4091193-1-guanyulin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260324203851.4091193-1-guanyulin@google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog Message-ID: <20260324203851.4091193-3-guanyulin@google.com> Subject: [PATCH v3 2/2] usb: host: xhci-sideband: delegate offload_usage tracking to class drivers From: Guan-Yu Lin To: gregkh@linuxfoundation.org, mathias.nyman@intel.com, perex@perex.cz, tiwai@suse.com, quic_wcheng@quicinc.com, broonie@kernel.org, arnd@arndb.de, xiaopei01@kylinos.cn, wesley.cheng@oss.qualcomm.com, hannelotta@gmail.com, sakari.ailus@linux.intel.com, eadavis@qq.com, stern@rowland.harvard.edu, amardeep.rai@intel.com, xu.yang_2@nxp.com, andriy.shevchenko@linux.intel.com, nkapron@google.com Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sound@vger.kernel.org, Guan-Yu Lin , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove usb_offload_get() and usb_offload_put() from the xHCI sideband interrupter creation and removal paths. The responsibility of manipulating offload_usage now lies entirely with the USB class drivers. They have the precise context of when an offload data stream actually starts and stops, ensuring a much more accurate representation of offload activity for power management. Cc: stable@vger.kernel.org Fixes: ef82a4803aab ("xhci: sideband: add api to trace sideband usage") Signed-off-by: Guan-Yu Lin Tested-by: hailong.liu@oppo.com --- drivers/usb/host/xhci-sideband.c | 14 +------------- sound/usb/qcom/qc_audio_offload.c | 10 +++++++++- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideb= and.c index 1ddb64b0a48e..651973606137 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -93,8 +93,6 @@ __xhci_sideband_remove_endpoint(struct xhci_sideband *sb,= struct xhci_virt_ep *e static void __xhci_sideband_remove_interrupter(struct xhci_sideband *sb) { - struct usb_device *udev; - lockdep_assert_held(&sb->mutex); =20 if (!sb->ir) @@ -102,10 +100,6 @@ __xhci_sideband_remove_interrupter(struct xhci_sideban= d *sb) =20 xhci_remove_secondary_interrupter(xhci_to_hcd(sb->xhci), sb->ir); sb->ir =3D NULL; - udev =3D sb->vdev->udev; - - if (udev->state !=3D USB_STATE_NOTATTACHED) - usb_offload_put(udev); } =20 /* sideband api functions */ @@ -328,9 +322,6 @@ int xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg, bool ip_autoclear, u32 imod_interval, int intr_num) { - int ret =3D 0; - struct usb_device *udev; - if (!sb || !sb->xhci) return -ENODEV; =20 @@ -348,12 +339,9 @@ xhci_sideband_create_interrupter(struct xhci_sideband = *sb, int num_seg, if (!sb->ir) return -ENOMEM; =20 - udev =3D sb->vdev->udev; - ret =3D usb_offload_get(udev); - sb->ir->ip_autoclear =3D ip_autoclear; =20 - return ret; + return 0; } EXPORT_SYMBOL_GPL(xhci_sideband_create_interrupter); =20 diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_of= fload.c index cfb30a195364..abafbc78dea9 100644 --- a/sound/usb/qcom/qc_audio_offload.c +++ b/sound/usb/qcom/qc_audio_offload.c @@ -699,6 +699,7 @@ static void uaudio_event_ring_cleanup_free(struct uaudi= o_dev *dev) uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE); xhci_sideband_remove_interrupter(uadev[dev->chip->card->number].sb); + usb_offload_put(dev->udev); } } =20 @@ -1182,12 +1183,16 @@ static int uaudio_event_ring_setup(struct snd_usb_s= ubstream *subs, dma_coherent =3D dev_is_dma_coherent(subs->dev->bus->sysdev); er_pa =3D 0; =20 + ret =3D usb_offload_get(subs->dev); + if (ret < 0) + goto exit; + /* event ring */ ret =3D xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false, 0, uaudio_qdev->data->intr_num); if (ret < 0) { dev_err(&subs->dev->dev, "failed to fetch interrupter\n"); - goto exit; + goto put_offload; } =20 sgt =3D xhci_sideband_get_event_buffer(uadev[card_num].sb); @@ -1219,6 +1224,8 @@ static int uaudio_event_ring_setup(struct snd_usb_sub= stream *subs, mem_info->dma =3D 0; remove_interrupter: xhci_sideband_remove_interrupter(uadev[card_num].sb); +put_offload: + usb_offload_put(subs->dev); exit: return ret; } @@ -1483,6 +1490,7 @@ static int prepare_qmi_response(struct snd_usb_substr= eam *subs, uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE); free_sec_ring: xhci_sideband_remove_interrupter(uadev[card_num].sb); + usb_offload_put(subs->dev); drop_sync_ep: if (subs->sync_endpoint) { uaudio_iommu_unmap(MEM_XFER_RING, --=20 2.53.0.1018.g2bb0e51243-goog