Skip to contentSkip to conversation

KVM: SVM: Unregister GALog notifier on module exit

PATCH Applied Sean Christopherson 1 participant 5 messages Oct 16, 2025 – Nov 4, 2025

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: SVM: Unregister GALog notifier on module exit

Unregister KVM's GALog notifier when kvm-amd.ko is being unloaded so that a spurious GALog event, e.g. due to some other bug, doesn't escalate to a use-after-free.

I deliberately didn't tag this for stable@, as shuffling the setup code around could easily introduce more problems than it solves, e.g. the patch might apply cleanly to an older kernel, but blow up at runtime due to the ordering being wrong.

My thought/plan is to get the fix into 6.18, where avic is first enabled by default, but not bother getting it into older LTS kernels.

Sean Christopherson (3):
KVM: SVM: Initialize per-CPU svm_data at the end of hardware setup
KVM: SVM: Unregister KVM's GALog notifier on kvm-amd.ko exit
KVM: SVM: Make avic_ga_log_notifier() local to avic.c

 arch/x86/kvm/svm/avic.c |  8 +++++++-
 arch/x86/kvm/svm/svm.c  | 15 +++++++++------
 arch/x86/kvm/svm/svm.h  |  2 +-
 3 files changed, 17 insertions(+), 8 deletions(-)

base-commit: 6b36119b94d0b2bb8cea9d512017efafd461d6ac

Signature · 1 line
2.51.0.858.gf9c4a03a3a-goog
author maintainer
#1 lore raw

PATCH 1/3 KVM: SVM: Initialize per-CPU svm_data at the end of hardware setup

Setup the per-CPU SVM data structures at the very end of hardware setup so that svm_hardware_unsetup() can be used in svm_hardware_setup() to unwind AVIC setup (for the GALog notifier). Alternatively, the error path could do an explicit, manual unwind, e.g. by adding a helper to free the per-CPU structures. But the per-CPU allocations have no interactions or dependencies, i.e. can comfortably live at the end, and so converting to a manual unwind would introduce churn and code without providing any immediate advantage.

---
 arch/x86/kvm/svm/svm.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +7 -6
index 153c12dbf3eb..efc3a7adebef 100644
@@ -5386,12 +5386,6 @@ static __init int svm_hardware_setup(void)
 
 	svm_hv_hardware_setup();
 
-	for_each_possible_cpu(cpu) {
-		r = svm_cpu_init(cpu);
-		if (r)
-			goto err;
-	}
-
 	enable_apicv = avic_hardware_setup();
 	if (!enable_apicv) {
 		enable_ipiv = false;
@@ -5435,6 +5429,13 @@ static __init int svm_hardware_setup(void)
 	svm_set_cpu_caps();
 
 	kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED;
+
+	for_each_possible_cpu(cpu) {
+		r = svm_cpu_init(cpu);
+		if (r)
+			goto err;
+	}
+
 	return 0;
 
 err:
Signature · 1 line
2.51.0.858.gf9c4a03a3a-goog
author maintainer
#2 lore raw

PATCH 2/3 KVM: SVM: Unregister KVM's GALog notifier on kvm-amd.ko exit

In reply to Sean Christopherson

Unregister the GALog notifier (used to get notified of wake events for blocking vCPUs) on kvm-amd.ko exit so that a KVM or IOMMU driver bug that results in a spurious GALog event "only" results in a spurious IRQ, and doesn't trigger a use-after-free due to executing unloaded module code.

Fixes 5881f73757cc ("svm: Introduce AMD IOMMU avic_ga_log_notifier")
---
 arch/x86/kvm/svm/avic.c | 6 ++++++
 arch/x86/kvm/svm/svm.c  | 2 ++
 arch/x86/kvm/svm/svm.h  | 1 +
 3 files changed, 9 insertions(+)
arch/x86/kvm/svm/avic.c +6 -0
index f286b5706d7c..3ab74f2bd584 100644
@@ -1243,3 +1243,9 @@ bool __init avic_hardware_setup(void)
 
 	return true;
 }
+
+void avic_hardware_unsetup(void)
+{
+	if (avic)
+		amd_iommu_register_ga_log_notifier(NULL);
+}
arch/x86/kvm/svm/svm.c +2 -0
index efc3a7adebef..76055c0ba177 100644
@@ -921,6 +921,8 @@ static void svm_hardware_unsetup(void)
 {
 	int cpu;
 
+	avic_hardware_unsetup();
+
 	sev_hardware_unsetup();
 
 	for_each_possible_cpu(cpu)
arch/x86/kvm/svm/svm.h +1 -0
index e4b04f435b3d..b0fe40c21728 100644
@@ -805,6 +805,7 @@ extern struct kvm_x86_nested_ops svm_nested_ops;
 )
 
 bool __init avic_hardware_setup(void);
+void avic_hardware_unsetup(void);
 int avic_ga_log_notifier(u32 ga_tag);
 void avic_vm_destroy(struct kvm *kvm);
 int avic_vm_init(struct kvm *kvm);
Signature · 1 line
2.51.0.858.gf9c4a03a3a-goog
author maintainer
#3 lore raw

PATCH 3/3 KVM: SVM: Make avic_ga_log_notifier() local to avic.c

In reply to Sean Christopherson

Make amd_iommu_register_ga_log_notifier() a local symbol now that it's defined and used purely within avic.c.

No functional change intended.

Fixes 4bdec12aa8d6 ("KVM: SVM: Detect X2APIC virtualization (x2AVIC) support")
---
 arch/x86/kvm/svm/avic.c | 2 +-
 arch/x86/kvm/svm/svm.h  | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/svm/avic.c +1 -1
index 3ab74f2bd584..89864fee6e83 100644
@@ -216,7 +216,7 @@ static void avic_deactivate_vmcb(struct vcpu_svm *svm)
  * This function is called from IOMMU driver to notify
  * SVM to schedule in a particular vCPU of a particular VM.
  */
-int avic_ga_log_notifier(u32 ga_tag)
+static int avic_ga_log_notifier(u32 ga_tag)
 {
 	unsigned long flags;
 	struct kvm_svm *kvm_svm;
arch/x86/kvm/svm/svm.h +0 -1
index b0fe40c21728..8c36ee0d67ef 100644
@@ -806,7 +806,6 @@ extern struct kvm_x86_nested_ops svm_nested_ops;
 
 bool __init avic_hardware_setup(void);
 void avic_hardware_unsetup(void);
-int avic_ga_log_notifier(u32 ga_tag);
 void avic_vm_destroy(struct kvm *kvm);
 int avic_vm_init(struct kvm *kvm);
 void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb);
Signature · 1 line
2.51.0.858.gf9c4a03a3a-goog
author maintainer Applied
#4 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Unregister KVM's GALog notifier when kvm-amd.ko is being unloaded so that a spurious GALog event, e.g. due to some other bug, doesn't escalate to a use-after-free.

I deliberately didn't tag this for stable@, as shuffling the setup code around could easily introduce more problems than it solves, e.g. the patch might apply cleanly to an older kernel, but blow up at runtime due to the ordering being wrong.

[...]

Applied to kvm-x86 fixes, thanks!

[1/3] KVM: SVM: Initialize per-CPU svm_data at the end of hardware setup

      https://github.com/kvm-x86/linux/commit/59a217ced3e7
[2/3] KVM: SVM: Unregister KVM's GALog notifier on kvm-amd.ko exit
      https://github.com/kvm-x86/linux/commit/adc6ae972971
[3/3] KVM: SVM: Make avic_ga_log_notifier() local to avic.c
      https://github.com/kvm-x86/linux/commit/aaac099459f9

--
https://github.com/kvm-x86/linux/tree/next