Skip to content

tools/kmod: tolerate USB-audio-busy for shared ALSA core modules - #1367

Open
harajend wants to merge 2 commits into
mainfrom
usb-audio-busy-tolerance-shared-alsa-core
Open

harajend wants to merge 2 commits into
mainfrom
usb-audio-busy-tolerance-shared-alsa-core

Conversation

@harajend

Copy link
Copy Markdown
Contributor

Problem

sof_remove.sh runs under 'bash -e'. If a USB audio device (e.g. a loopback/offload device used by CI) is briefly busy when snd_usb_audio/snd_usbmidi_lib are removed, the script aborts right there.

Root cause

Because the script aborts mid-way under set -e, snd_sof can already be removed by the time the abort happens, but reload_drivers() never gets to reload it. That breaks the DSP for every subsequent test case in the run, not just the current one.

Fix

Track whether USB audio is still loaded after the removal attempt (usb_audio_still_loaded) and tolerate the same failure on the shared ALSA-core modules it can keep busy: snd_hwdep, snd_rawmidi, snd_seq_device, snd_timer, snd, soundcore. This mirrors the existing tolerance already in place for snd_sof_intel_hda_common's SDW-BRA busy case a few lines above. Unrelated failures on these modules still abort normally, since the tolerance is conditional on USB audio itself having failed to unload.

Verification

Internally we carried an equivalent fix on our downstream fork and confirmed it resolves a cascade of ~38 failed test cases caused by a USB audio loopback device (WCL_HDA_AIOC) keeping shared modules busy during driver reload on an RVP DUT. This PR ports the same fix onto the current upstream module ordering (which already differs from our fork, e.g. the snd_usb_offload/soc_usb reordering and the snd_ump handling).

sof_remove.sh runs under 'set -e'. If a USB audio device (e.g. a
loopback/offload device under test) is briefly busy when
snd_usb_audio/snd_usbmidi_lib are removed, the script aborts right
there. That leaves snd_sof removed but never reloaded, breaking the
DSP for the rest of the test run, since reload_drivers() never
completes.

Track whether USB audio is still loaded after the removal attempt and
tolerate the same failure on the shared ALSA-core modules it can keep
busy (snd_hwdep, snd_rawmidi, snd_seq_device, snd_timer, snd,
soundcore), the same way snd_sof_intel_hda_common's SDW-BRA busy case
is already tolerated above. Unrelated failures on these modules still
abort, since the tolerance only applies when USB audio itself failed
to unload.
@harajend
harajend requested review from a team, lgirdwood, marc-hb and plbossart as code owners November 23, 2026 01:57
@harajend harajend self-assigned this Nov 23, 2026
@marc-hb

marc-hb commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

If a USB audio device (e.g. a loopback/offload device used by CI) is briefly busy

Why is it "briefly busy"? Some audio daemon interfering maybe? Which could cause many other, unrelated failures? As has already happened so many other times in the past?

See https://github.com/thesofproject/sof-test/pulls?q=pulseaudio , #289, + many other Intel-private issues.

It seems wiser to find and deal with the root cause and make the test configuration more deterministic rather than trying to paper over it.

reload_drivers() never gets to reload it. That breaks the DSP for every subsequent test case in the run, not just the current one.

In an ideal world, rebooting would be really fast and performed after each failure. Not rebooting after a failure is always a risky optimization because there is a million of other, unexpected ways a failure can cascade and lose the entire run - not just this USB one. Trying to manage them all has always been a game of whack-a-mole. This has already been discussed a lot in the past.

This being said, an interesting question is: why is reload_drivers() not capable of recovering from this failure and unexpected state? Maybe cleanup and setup functions should try harder to produce a deterministic test setup (with or without rebooting)

Track whether USB audio is still loaded after the removal attempt (usb_audio_still_loaded) and tolerate the same failure on the shared ALSA-core modules it can keep busy:

This tries to deal with a non-deterministic setup by making it even less deterministic. Good luck with that...

@marc-hb

marc-hb commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Why is it "briefly busy"? Some audio daemon interfering maybe? Which could cause many other, unrelated failures? As has already happened so many other times in the past?

BTW pgrep -a pipewire and all other audio daemons are definitely missing from the exit_handler(). That could answer this question.

sof_remove.sh runs under 'bash -e'. If a USB audio device (e.g. a loopback/offload device used by CI) is briefly busy when snd_usb_audio/snd_usbmidi_lib are removed, the script aborts right there.

If you want to keep ignoring why the test setup is not deterministic, another option could be something like this:

main
{
  for i in $(seq 1 $max_tries); do
   if remove_all_modules; then
       break;
   fi
   if [ $i -lt $max_tries]; then
      WARN sof_remove.sh failed...
   else
      die ...
   fi
done
}
{
  main "$@"; exit $?
}

@harajend harajend left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for calling out that “briefly busy” was an assumption and that the original failure path lacked useful audio-process diagnostics. I removed that wording. The reproduced CI scenario is a USB loopback/offload device outside the SOF stack under test; the current evidence does not establish whether an audio daemon is the reference owner.

I updated the PR to retry removal of snd_usb_audio and snd_usbmidi_lib up to three times before applying the existing narrow tolerance. On exit, sof_remove.sh now lists PulseAudio, PipeWire, WirePlumber, and JACK processes; on failure it also reports users of /dev/snd/*. The shared ALSA modules are still tolerated only when USB-audio removal failed, and unrelated unload failures remain fatal.

I chose scoped retries over rerunning the whole unload sequence: the failure is localized to the USB-audio modules, while the rest of the sequence already skips modules that are no longer loaded. The added diagnostics should help establish whether a daemon or open audio device owns the reference on the next occurrence.

@lgirdwood

Copy link
Copy Markdown
Member

@harajend can you check the CI results, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants