From cec6909a2887757b668439cd6f9061291c6beace Mon Sep 17 00:00:00 2001 From: harajend Date: Mon, 23 Nov 2026 07:26:55 +0530 Subject: [PATCH 1/2] tools/kmod: tolerate USB-audio-busy for shared ALSA core modules 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. --- tools/kmod/sof_remove.sh | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tools/kmod/sof_remove.sh b/tools/kmod/sof_remove.sh index 40d336b4..70645514 100755 --- a/tools/kmod/sof_remove.sh +++ b/tools/kmod/sof_remove.sh @@ -193,10 +193,16 @@ remove_module snd_soc_sdw_rt700 remove_module snd_soc_sdw_rt711_rt1308_rt715 remove_module snd_usb_offload remove_module soc_usb -# SOF CI has a dependency on usb audio. This has to come after -# snd_usb_offload and soc_usb, which hold references on snd_usb_audio. -remove_module snd_usb_audio -remove_module snd_usbmidi_lib +# SOF CI has a dependency on usb audio, but it's not part of the SOF stack +# under test, so don't abort the whole unload if it's briefly busy. If it +# stays loaded, it also keeps the shared snd_hwdep/snd_rawmidi/snd_timer/snd +# modules below in use; that specific, already-tolerated condition is +# preserved there too, instead of blanket-ignored, so unrelated SOF-stack +# failures still abort. This has to come after snd_usb_offload and soc_usb, +# which hold references on snd_usb_audio. +usb_audio_still_loaded=false +remove_module snd_usb_audio || usb_audio_still_loaded=true +remove_module snd_usbmidi_lib || usb_audio_still_loaded=true remove_module snd_soc_sof_sdw remove_module snd_soc_sdw_utils remove_module snd_soc_sof_es8336 @@ -342,7 +348,7 @@ remove_module snd_soc_sdca remove_module snd_soc_core remove_module snd_hda_codec remove_module snd_hda_core -remove_module snd_hwdep +remove_module snd_hwdep || [ "$usb_audio_still_loaded" = true ] remove_module snd_compress remove_module snd_pcm_dmaengine remove_module snd_pcm @@ -355,11 +361,11 @@ remove_module snd_seq_midi_event # rmmod: ERROR: Module snd_rawmidi is in use by: snd_ump # aborting reload_drivers() and cascading into every driver-reload test case. remove_module snd_ump -remove_module snd_rawmidi +remove_module snd_rawmidi || [ "$usb_audio_still_loaded" = true ] remove_module snd_seq_dummy remove_module snd_seq -remove_module snd_seq_device +remove_module snd_seq_device || [ "$usb_audio_still_loaded" = true ] remove_module snd_hrtimer -remove_module snd_timer -remove_module snd -remove_module soundcore +remove_module snd_timer || [ "$usb_audio_still_loaded" = true ] +remove_module snd || [ "$usb_audio_still_loaded" = true ] +remove_module soundcore || [ "$usb_audio_still_loaded" = true ] From f2a78b87f6a56dffdd3e0e95e76a98e7a74f9980 Mon Sep 17 00:00:00 2001 From: harajend Date: Thu, 3 Dec 2026 17:44:38 +0530 Subject: [PATCH 2/2] tools/kmod: diagnose and retry busy USB audio modules --- tools/kmod/sof_remove.sh | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/tools/kmod/sof_remove.sh b/tools/kmod/sof_remove.sh index 70645514..861648df 100755 --- a/tools/kmod/sof_remove.sh +++ b/tools/kmod/sof_remove.sh @@ -27,20 +27,45 @@ remove_module() { fi } +remove_usb_audio_module() +{ + local module="$1" attempt + + for attempt in 1 2 3; do + if remove_module "$module"; then + return 0 + fi + if [ "$attempt" -lt 3 ]; then + printf 'Retrying removal of %s in 1s (attempt %s/3 failed)\n' \ + "$module" "$attempt" + sleep 1 + fi + done + + return 1 +} + exit_handler() { local exit_status="$1" + local daemon + + # Audio daemons can hold USB audio modules even when no SOF playback is active. + for daemon in pulseaudio pipewire pipewire-pulse wireplumber jackd jackdbus; do + pgrep -a -x "$daemon" || true + done + # Even without any active audio, pulseaudio can use modules # "non-deterministically". So even if we are successful this time, # warn about any running pulseaudio because it could make us fail # the next time. - # TODO: display any pipewire process too. if pgrep -a pulseaudio; then systemctl_show_pulseaudio || true fi if test "$exit_status" -ne 0; then lsmod | grep -e sof -e snd -e sound -e drm + sudo fuser -v /dev/snd/* || true # rmmod can fail silently, for instance when "Used by" is -1 printf "%s FAILED\n" "$0" fi @@ -193,16 +218,14 @@ remove_module snd_soc_sdw_rt700 remove_module snd_soc_sdw_rt711_rt1308_rt715 remove_module snd_usb_offload remove_module soc_usb -# SOF CI has a dependency on usb audio, but it's not part of the SOF stack -# under test, so don't abort the whole unload if it's briefly busy. If it -# stays loaded, it also keeps the shared snd_hwdep/snd_rawmidi/snd_timer/snd -# modules below in use; that specific, already-tolerated condition is -# preserved there too, instead of blanket-ignored, so unrelated SOF-stack -# failures still abort. This has to come after snd_usb_offload and soc_usb, -# which hold references on snd_usb_audio. +# CI may use a USB audio device outside the SOF stack under test. Retry +# removal in case its references are transient; if it remains loaded, +# tolerate failures only for the shared ALSA modules it keeps busy. Other +# unload failures remain fatal. This has to come after snd_usb_offload and +# soc_usb, which hold references on snd_usb_audio. usb_audio_still_loaded=false -remove_module snd_usb_audio || usb_audio_still_loaded=true -remove_module snd_usbmidi_lib || usb_audio_still_loaded=true +remove_usb_audio_module snd_usb_audio || usb_audio_still_loaded=true +remove_usb_audio_module snd_usbmidi_lib || usb_audio_still_loaded=true remove_module snd_soc_sof_sdw remove_module snd_soc_sdw_utils remove_module snd_soc_sof_es8336