Skip to content

Commit d921665

Browse files
Maxim Levitskysean-jc
authored andcommitted
KVM: SVM: Add enable_ipiv param, never set IsRunning if disabled
Let userspace "disable" IPI virtualization for AVIC via the enable_ipiv module param, by never setting IsRunning. SVM doesn't provide a way to disable IPI virtualization in hardware, but by ensuring CPUs never see IsRunning=1, every IPI in the guest (except for self-IPIs) will generate a VM-Exit. To avoid setting the real IsRunning bit, while still allowing KVM to use each vCPU's entry to update GA log entries, simply maintain a shadow of the entry, without propagating IsRunning updates to the real table when IPI virtualization is disabled. Providing a way to effectively disable IPI virtualization will allow KVM to safely enable AVIC on hardware that is susceptible to erratum #1235, which causes hardware to sometimes fail to detect that the IsRunning bit has been cleared by software. Note, the table _must_ be fully populated, as broadcast IPIs skip invalid entries, i.e. won't generate VM-Exit if every entry is invalid, and so simply pointing the VMCB at a common dummy table won't work. Alternatively, KVM could allocate a shadow of the entire table, but that'd be a waste of 4KiB since the per-vCPU entry doesn't actually consume an additional 8 bytes of memory (vCPU structures are large enough that they are backed by order-N pages). Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> [sean: keep "entry" variables, reuse enable_ipiv, split from erratum] Link: https://lore.kernel.org/r/20250611224604.313496-19-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent bafddc7 commit d921665

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
293293
/* Setting AVIC backing page address in the phy APIC ID table */
294294
new_entry = avic_get_backing_page_address(svm) |
295295
AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
296+
svm->avic_physical_id_entry = new_entry;
297+
298+
/*
299+
* Initialize the real table, as vCPUs must have a valid entry in order
300+
* for broadcast IPIs to function correctly (broadcast IPIs ignore
301+
* invalid entries, i.e. aren't guaranteed to generate a VM-Exit).
302+
*/
296303
WRITE_ONCE(kvm_svm->avic_physical_id_table[id], new_entry);
297304

298305
return 0;
@@ -770,8 +777,6 @@ static int svm_ir_list_add(struct vcpu_svm *svm,
770777
struct amd_iommu_pi_data *pi)
771778
{
772779
struct kvm_vcpu *vcpu = &svm->vcpu;
773-
struct kvm *kvm = vcpu->kvm;
774-
struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
775780
unsigned long flags;
776781
u64 entry;
777782

@@ -789,7 +794,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm,
789794
* will update the pCPU info when the vCPU awkened and/or scheduled in.
790795
* See also avic_vcpu_load().
791796
*/
792-
entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]);
797+
entry = svm->avic_physical_id_entry;
793798
if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
794799
amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK,
795800
true, pi->ir_data);
@@ -999,14 +1004,26 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
9991004
*/
10001005
spin_lock_irqsave(&svm->ir_list_lock, flags);
10011006

1002-
entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]);
1007+
entry = svm->avic_physical_id_entry;
10031008
WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
10041009

10051010
entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
10061011
entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
10071012
entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
10081013

1014+
svm->avic_physical_id_entry = entry;
1015+
1016+
/*
1017+
* If IPI virtualization is disabled, clear IsRunning when updating the
1018+
* actual Physical ID table, so that the CPU never sees IsRunning=1.
1019+
* Keep the APIC ID up-to-date in the entry to minimize the chances of
1020+
* things going sideways if hardware peeks at the ID.
1021+
*/
1022+
if (!enable_ipiv)
1023+
entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1024+
10091025
WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
1026+
10101027
avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true);
10111028

10121029
spin_unlock_irqrestore(&svm->ir_list_lock, flags);
@@ -1031,7 +1048,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
10311048
* can't be scheduled out and thus avic_vcpu_{put,load}() can't run
10321049
* recursively.
10331050
*/
1034-
entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]);
1051+
entry = svm->avic_physical_id_entry;
10351052

10361053
/* Nothing to do if IsRunning == '0' due to vCPU blocking. */
10371054
if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK))
@@ -1050,7 +1067,10 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
10501067
avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
10511068

10521069
entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1053-
WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
1070+
svm->avic_physical_id_entry = entry;
1071+
1072+
if (enable_ipiv)
1073+
WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
10541074

10551075
spin_unlock_irqrestore(&svm->ir_list_lock, flags);
10561076

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ module_param(tsc_scaling, int, 0444);
232232
*/
233233
static bool avic;
234234
module_param(avic, bool, 0444);
235+
module_param(enable_ipiv, bool, 0444);
235236

236237
module_param(enable_device_posted_irqs, bool, 0444);
237238

@@ -5581,6 +5582,7 @@ static __init int svm_hardware_setup(void)
55815582
enable_apicv = avic = avic && avic_hardware_setup();
55825583

55835584
if (!enable_apicv) {
5585+
enable_ipiv = false;
55845586
svm_x86_ops.vcpu_blocking = NULL;
55855587
svm_x86_ops.vcpu_unblocking = NULL;
55865588
svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;

arch/x86/kvm/svm/svm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,14 @@ struct vcpu_svm {
307307
u32 ldr_reg;
308308
u32 dfr_reg;
309309

310+
/* This is essentially a shadow of the vCPU's actual entry in the
311+
* Physical ID table that is programmed into the VMCB, i.e. that is
312+
* seen by the CPU. If IPI virtualization is disabled, IsRunning is
313+
* only ever set in the shadow, i.e. is never propagated to the "real"
314+
* table, so that hardware never sees IsRunning=1.
315+
*/
316+
u64 avic_physical_id_entry;
317+
310318
/*
311319
* Per-vCPU list of irqfds that are eligible to post IRQs directly to
312320
* the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list

0 commit comments

Comments
 (0)