@@ -713,6 +713,12 @@ downgrade_to_shared_kvm_lock() {
713713 echo " Downgraded to shared KVM lock"
714714}
715715
716+ # Get the reference count of a kernel module from /proc/modules.
717+ # Returns 0 if the module is not loaded.
718+ get_kvm_refcount () {
719+ awk -v mod=" $1 " ' $1 == mod {print $3; found=1} END {if (!found) print 0}' /proc/modules
720+ }
721+
716722# Reload KVM modules with the given vendor module and kvm params.
717723# Always enables avic=1 on AMD. Unloads first if already loaded.
718724# Usage: reload_kvm_modules <vendor_mod> [kvm_param...]
@@ -814,16 +820,17 @@ setup_kvm() {
814820 fi
815821
816822 if [[ $need_kvm_reload -eq 1 ]]; then
817- echo " Reloading KVM modules"
818- upgrade_to_exclusive_kvm_lock
819- if ! reload_kvm_modules " $vendor_mod " " ${kvm_extra_params[@]} " ; then
820- if [[ -c /dev/kvm ]]; then
821- say_warn " Could not reload KVM modules (in use), continuing with current parameters"
822- else
823- die " Could not reload KVM modules and /dev/kvm not found"
824- fi
823+ local refcount
824+ refcount=$( get_kvm_refcount " $vendor_mod " )
825+ if [[ " $refcount " -gt 0 ]]; then
826+ say_warn " KVM module $vendor_mod is in use (refcount=$refcount ), skipping reload"
827+ else
828+ echo " Reloading KVM modules"
829+ upgrade_to_exclusive_kvm_lock
830+ reload_kvm_modules " $vendor_mod " " ${kvm_extra_params[@]} "
831+ ok_or_die " Could not reload kvm modules"
832+ downgrade_to_shared_kvm_lock
825833 fi
826- downgrade_to_shared_kvm_lock
827834 fi
828835
829836 tail -v $itlb_multihit $nx_huge_pages
0 commit comments