[PATCH v4 0/2] usb: offload: Decouple interrupter lifecycle and refactor usage tracking

Guan-Yu Lin posted 2 patches 6 hours ago
drivers/usb/core/driver.c         |  23 ++++---
drivers/usb/core/offload.c        | 102 +++++++++++++++++-------------
drivers/usb/core/usb.c            |   1 +
drivers/usb/host/xhci-sideband.c  |  18 +-----
include/linux/usb.h               |  10 ++-
sound/usb/qcom/qc_audio_offload.c |  10 ++-
6 files changed, 94 insertions(+), 70 deletions(-)
[PATCH v4 0/2] usb: offload: Decouple interrupter lifecycle and refactor usage tracking
Posted by Guan-Yu Lin 6 hours ago
The current USB offload implementation couples the allocation of xHCI
sideband interrupters with the device's offload usage counter. This
coupling is conceptually incorrect, as hardware resource availability
and power management state serve distinct purposes.

Furthermore, the reliance on the coarse USB device lock for offload
state updates has led to potential recursive locking issues,
especially during device disconnect when the lock is already held
by the USB core.

This series refactors the offload synchronization by introducing a
dedicated spinlock for offload state, allowing for more granular
concurrency control and avoiding deadlocks. It also optimizes power
management by ensuring that offload state is only modified when the
device is already active, avoiding unnecessary auto-resumes.

Patch 1 introduces the `offload_lock` spinlock and `offload_pm_locked`
synchronization, replacing the coarse `udev->lock` and the legacy
`offload_at_suspend` flag. It also updates `usb_offload_get/put` to use
`pm_runtime_get_if_active()`.

Patch 2 removes the implicit usage tracking from the xHCI sideband layer
and delegates the responsibility to class drivers, who have the
correct context for managing offload data stream activity.

---
Changes in v4:
- remove spinlock in `usb_offload_check`.
- Collect the <Tested-by> tag from the OPPO team.
- Link to v3: https://lore.kernel.org/all/20260324203851.4091193-1-guanyulin@google.com/

Changes in v3:
- Replace the coarse USB device lock with a dedicated `offload_lock`
  spinlock to reduce contention and prevent recursive locking.
- Introduce `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()`, avoiding unnecessary power transitions.
- Explicitly delegate `offload_usage` tracking to USB class drivers
  (e.g., the Qualcomm USB audio offload driver).
- Link to v2: https://lore.kernel.org/all/20260309022205.28136-1-guanyulin@google.com/

Changes in v2:
- Collect the <Tested-by> tag from the OPPO team.
- Link to v1: https://lore.kernel.org/all/20260225064601.270301-1-guanyulin@google.com/

Changes in v1:
- Fix build error when building sound/usb/qcom/qc_audio_offload.o.
- Link to RFC v2: https://lore.kernel.org/all/20260213100736.2914690-1-guanyulin@google.com/

Changes in RFC v2:
- Move device locking to callers.
- Decouple sideband from offload counting.
- Link to RFC v1: https://lore.kernel.org/all/20260130074746.287750-1-guanyulin@google.com/
---
Guan-Yu Lin (2):
  usb: core: use dedicated spinlock for offload state
  usb: host: xhci-sideband: delegate offload_usage tracking to class
    drivers

 drivers/usb/core/driver.c         |  23 ++++---
 drivers/usb/core/offload.c        | 102 +++++++++++++++++-------------
 drivers/usb/core/usb.c            |   1 +
 drivers/usb/host/xhci-sideband.c  |  18 +-----
 include/linux/usb.h               |  10 ++-
 sound/usb/qcom/qc_audio_offload.c |  10 ++-
 6 files changed, 94 insertions(+), 70 deletions(-)

-- 
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v4 0/2] usb: offload: Decouple interrupter lifecycle and refactor usage tracking
Posted by Greg KH 5 hours ago
On Wed, Apr 01, 2026 at 12:32:16PM +0000, Guan-Yu Lin wrote:
> The current USB offload implementation couples the allocation of xHCI
> sideband interrupters with the device's offload usage counter. This
> coupling is conceptually incorrect, as hardware resource availability
> and power management state serve distinct purposes.
> 
> Furthermore, the reliance on the coarse USB device lock for offload
> state updates has led to potential recursive locking issues,
> especially during device disconnect when the lock is already held
> by the USB core.
> 
> This series refactors the offload synchronization by introducing a
> dedicated spinlock for offload state, allowing for more granular
> concurrency control and avoiding deadlocks. It also optimizes power
> management by ensuring that offload state is only modified when the
> device is already active, avoiding unnecessary auto-resumes.
> 
> Patch 1 introduces the `offload_lock` spinlock and `offload_pm_locked`
> synchronization, replacing the coarse `udev->lock` and the legacy
> `offload_at_suspend` flag. It also updates `usb_offload_get/put` to use
> `pm_runtime_get_if_active()`.
> 
> Patch 2 removes the implicit usage tracking from the xHCI sideband layer
> and delegates the responsibility to class drivers, who have the
> correct context for managing offload data stream activity.

Ok, this looks much better, thanks for the revisions.

If the xhci maintainer acks these, I'll be glad to apply them to my
tree.

thanks,

greg k-h
Re: [PATCH v4 0/2] usb: offload: Decouple interrupter lifecycle and refactor usage tracking
Posted by Mathias Nyman 4 hours ago
On 4/1/26 16:38, Greg KH wrote:
> On Wed, Apr 01, 2026 at 12:32:16PM +0000, Guan-Yu Lin wrote:
>> The current USB offload implementation couples the allocation of xHCI
>> sideband interrupters with the device's offload usage counter. This
>> coupling is conceptually incorrect, as hardware resource availability
>> and power management state serve distinct purposes.
>>
>> Furthermore, the reliance on the coarse USB device lock for offload
>> state updates has led to potential recursive locking issues,
>> especially during device disconnect when the lock is already held
>> by the USB core.
>>
>> This series refactors the offload synchronization by introducing a
>> dedicated spinlock for offload state, allowing for more granular
>> concurrency control and avoiding deadlocks. It also optimizes power
>> management by ensuring that offload state is only modified when the
>> device is already active, avoiding unnecessary auto-resumes.
>>
>> Patch 1 introduces the `offload_lock` spinlock and `offload_pm_locked`
>> synchronization, replacing the coarse `udev->lock` and the legacy
>> `offload_at_suspend` flag. It also updates `usb_offload_get/put` to use
>> `pm_runtime_get_if_active()`.
>>
>> Patch 2 removes the implicit usage tracking from the xHCI sideband layer
>> and delegates the responsibility to class drivers, who have the
>> correct context for managing offload data stream activity.
> 
> Ok, this looks much better, thanks for the revisions.
> 
> If the xhci maintainer acks these, I'll be glad to apply them to my
> tree.

For both patches:

Acked-by: Mathias Nyman <mathias.nyman@linux.intel.com>