diff --git a/tools/kmod/sof_remove.sh b/tools/kmod/sof_remove.sh index 40d336b4..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,10 +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. 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 +# 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_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 @@ -342,7 +371,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 +384,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 ]