VM: SVM: Honor KVM_MAX_VCPUS when AVIC is enabled
Sean Christopherson
maintainercover VM: SVM: Honor KVM_MAX_VCPUS when AVIC is enabled
The only true functional change in this entire mess is to change KVM's handling of KVM_CREATE_VCPU when AVIC is enabled. Currently, KVM rejects vCPU creation if the vcpu_id is unaddressable, i.e. if it's larger than what is suppported by AVIC/x2AVIC hardware. That is a rather blatant violation of both KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID, as KVM will advertise a KVM_CAP_MAX_VCPUS as 1024 and KVM_CAP_MAX_VCPU_ID as 4096, but then reject vcpu_ids as low as 256 (AVIC).
To fix the problem, add yet another AVIC inhibit to disable AVIC if userspace creates unaddressable vCPUs. Alternatively, KVM could report different KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID values when AVIC is enabled, but IMO that path sets KVM up for failure, e.g. it would make it really hard for us to enable AVIC/x2AVIC by default, and we'd have to have to rework KVM selftests, which assume that KVM supports at least 512 vCPUs, e.g. recalc_apic_map_test fails when AVIC is enabled.
The bulk of this series is cleaning up related code, most of which is purely opportunistic, e.g. the many pointless PA masks, but some of which are functionally "necessary", for some definitions of necessary.
Lightly tested, and the IOMMU interaction is basically compile tested only. But this is firmly post-6.6 material, so no rush on anyone testing this (I wouldn't even care all that much if the darn selftests didn't fail).
Sean Christopherson (10):
KVM: SVM: Drop pointless masking of default APIC base when setting
V_APIC_BAR
KVM: SVM: Use AVIC_HPA_MASK when initializing vCPU's Physical ID entry
KVM: SVM: Drop pointless masking of kernel page pa's with "AVIC's" HPA
mask
KVM: SVM: Add helper to deduplicate code for getting AVIC backing page
KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr
KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU
creation
KVM: SVM: WARN if KVM attempts to create AVIC backing page with user
APIC
KVM: SVM: Drop redundant check in AVIC code on ID during vCPU creation
KVM: SVM: Rename "avic_physical_id_cache" to "avic_physical_id_entry"
arch/x86/include/asm/kvm_host.h | 6 +++ arch/x86/include/asm/svm.h | 6 +-- arch/x86/kvm/svm/avic.c | 79 +++++++++++++++------------------ arch/x86/kvm/svm/svm.h | 6 +-- drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 6 files changed, 48 insertions(+), 52 deletions(-)
base-commit: 240f736891887939571854bd6d734b6c9291f22e
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Sean Christopherson
maintainerPATCH 1/10 KVM: SVM: Drop pointless masking of default APIC base when setting V_APIC_BAR
Drop VMCB_AVIC_APIC_BAR_MASK, it's just a regurgitation of the maximum theoretical 4KiB-aligned physical address, i.e. is not novel in any way, and its only usage is to mask the default APIC base, which is 4KiB aligned and (obviously) a legal physical address.
No functional change intended.
--- arch/x86/include/asm/svm.h | 2 -- arch/x86/kvm/svm/avic.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/include/asm/svm.h +0 -2
@@ -257,8 +257,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define AVIC_DOORBELL_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL -#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
arch/x86/kvm/svm/avic.c +1 -1
@@ -251,7 +251,7 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) vmcb->control.avic_backing_page = bpa &AVIC_HPA_MASK ; vmcb->control.avic_logical_id = lpa &AVIC_HPA_MASK ; vmcb->control.avic_physical_id = ppa &AVIC_HPA_MASK ; - vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE &VMCB_AVIC_APIC_BAR_MASK ; + vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;if (kvm_apicv_activated (svm->vcpu.kvm))avic_activate_vmcb (svm);
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 01/10] KVM: SVM: Drop pointless masking of default APIC base when setting V_APIC_BAR
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 18 lines
Drop
VMCB_AVIC_APIC_BAR_MASK, it's just a regurgitation of the maximum theoretical 4KiB-aligned physical address, i.e. is not novel in any way, and its only usage is to mask the default APIC base, which is 4KiB aligned and (obviously) a legal physical address.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/svm.h | 2 -- arch/x86/kvm/svm/avic.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)arch/x86/include/asm/svm.h +0
-0
arch/x86/include/asm/svm.h struct __attribute__ ((__packed__)) vmcb_control_area { · line 260
Quoted 4 lines
arch/x86/include/asm/svm.h +0
-1@@ -257,8 +257,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define AVIC_DOORBELL_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
While this mask is indeed not needed now because AVIC doesn't support non default APIC base, this mask will be needed in my upcoming nested AVIC support, because nested hypervisor can ask for any apic base it wishes for.
Quoted 9 lines
arch/x86/include/asm/svm.h +0
-1-#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFFarch/x86/kvm/svm/avic.c +0
-0
arch/x86/kvm/svm/avic.c void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) · line 254
Quoted 5 lines
arch/x86/kvm/svm/avic.c +1
-1@@ -251,7 +251,7 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) vmcb->control.avic_backing_page = bpa &AVIC_HPA_MASK ; vmcb->control.avic_logical_id = lpa &AVIC_HPA_MASK ; vmcb->control.avic_physical_id = ppa &AVIC_HPA_MASK ; - vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE &VMCB_AVIC_APIC_BAR_MASK ; + vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;
Here I agree that the '&' is functionally pointless, although I am not sure that removing it makes the code more readable.
Best regards,
Maxim Levitsky
Quoted 3 lines
arch/x86/kvm/svm/avic.c +0
-0if (kvm_apicv_activated (svm->vcpu.kvm))avic_activate_vmcb (svm);
Sean Christopherson
maintainerPATCH 2/10 KVM: SVM: Use AVIC_HPA_MASK when initializing vCPU's Physical ID entry
In reply to Sean ChristophersonUse AVIC_HPA_MASK instead of AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK when initializing a vCPU's Physical ID table entry, the two masks are identical. Keep both #defines for now, along with a few new static asserts. A future change will clean up the entire mess (spoiler alert, the masks are pointless).
Opportunisitically move the bitwise-OR of AVIC_PHYSICAL_ID_ENTRY_VALID_MASK outside of the call to __sme_set(), again to pave the way for code deduplication. __sme_set() is purely additive, i.e. ORing in the valid bit before or after the C-bit does not change the end result.
No functional change intended.
--- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/avic.c | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +2 -0
@@ -285,6 +285,8 @@ static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == AVIC_MAX_static_assert ((X2AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==X2AVIC_MAX_PHYSICAL_ID );#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==AVIC_HPA_MASK ); +static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==GENMASK_ULL (51 ,12 ));#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)
arch/x86/kvm/svm/avic.c +2 -3
@@ -308,9 +308,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set ((page_to_phys (svm->avic_backing_page) & -AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ) | -AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ); + new_entry =__sme_set (page_to_phys (svm->avic_backing_page) &AVIC_HPA_MASK ) | +AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry); svm->avic_physical_id_cache = entry;
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 02/10] KVM: SVM: Use AVIC_HPA_MASK when initializing vCPU's Physical ID entry
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 2 lines
Use
AVIC_HPA_MASKinstead ofAVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASKwhen initializing a vCPU's Physical ID table entry, the two masks are identical.
Masks are identical, but they refer to different things.AVIC_HPA_MASK is about avic's fields in vmcb, while AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK is about the portion of the physical id entry which has the address.
They are the same currently but for documentation purposes I don't understand why removal of one of them makes sense.
IMHO, it's best to define them to the same value, e.g:
#define AVIC_HPA_MASK GENMASK_ULL(51, 12)
#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK GENMASK_ULL(51, 12)
Best regards,
Maxim Levitsky
Quoted 46 lines
Keep both #defines for now, along with a few new static asserts. A future change will clean up the entire mess (spoiler alert, the masks are pointless).
Opportunisitically move the bitwise-OR of
AVIC_PHYSICAL_ID_ENTRY_VALID_MASKoutside of the call to__sme_set(), again to pave the way for code deduplication.__sme_set()is purely additive, i.e. ORing in the valid bit before or after the C-bit does not change the end result.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/avic.c | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-)arch/x86/include/asm/svm.h +2
-0@@ -285,6 +285,8 @@ static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == AVIC_MAX_static_assert ((X2AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==X2AVIC_MAX_PHYSICAL_ID );#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==AVIC_HPA_MASK ); +static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==GENMASK_ULL (51 ,12 ));#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)arch/x86/kvm/svm/avic.c +2
-3@@ -308,9 +308,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set ((page_to_phys (svm->avic_backing_page) & -AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ) | -AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ); + new_entry =__sme_set (page_to_phys (svm->avic_backing_page) &AVIC_HPA_MASK ) | +AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry); svm->avic_physical_id_cache = entry;
Sean Christopherson
maintainerPATCH 3/10 KVM: SVM: Drop pointless masking of kernel page pa's with "AVIC's" HPA mask
In reply to Sean ChristophersonDrop AVIC_HPA_MASK and all its users, the mask is just the 4KiB-aligned maximum theoretical physical address for x86-64 CPUs. All usage in KVM masks the result of page_to_phys(), which on x86-64 is guaranteed to be 4KiB aligned and a legal physical address; if either of those requirements doesn't hold true, KVM has far bigger problems.
The unnecessarily masking in avic_init_vmcb() also incorrectly assumes that SME's C-bit resides between bits 51:11; that holds true for current CPUs, but isn't required by AMD's architecture:
In some implementations, the bit used may be a physical address bit
Key word being "may".
Opportunistically use the GENMASK_ULL() version for AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK, which is far more readable than a set of repeating Fs. Keep the macro even though it's unused, and will likely never be used, as it helps visualize the layout of an entry.
--- arch/x86/include/asm/svm.h | 6 +----- arch/x86/kvm/svm/avic.c | 11 +++++------ 2 files changed, 6 insertions(+), 11 deletions(-)
arch/x86/include/asm/svm.h +1 -5
@@ -250,7 +250,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) +#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK GENMASK_ULL(51, 12)#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)#define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK (0xFFULL)
@@ -284,10 +284,6 @@ enum avic_ipi_failure_cause {static_assert ((AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==AVIC_MAX_PHYSICAL_ID );static_assert ((X2AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==X2AVIC_MAX_PHYSICAL_ID ); -#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==AVIC_HPA_MASK ); -static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==GENMASK_ULL (51 ,12 )); -#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)struct vmcb_seg {
arch/x86/kvm/svm/avic.c +5 -6
@@ -248,9 +248,9 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb)phys_addr_t lpa =__sme_set (page_to_phys (kvm_svm->avic_logical_id_table_page));phys_addr_t ppa =__sme_set (page_to_phys (kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa &AVIC_HPA_MASK ; - vmcb->control.avic_logical_id = lpa &AVIC_HPA_MASK ; - vmcb->control.avic_physical_id = ppa &AVIC_HPA_MASK ; + vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_logical_id = lpa; + vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;if (kvm_apicv_activated (svm->vcpu.kvm))
@@ -308,7 +308,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set (page_to_phys (svm->avic_backing_page) &AVIC_HPA_MASK ) | + new_entry =__sme_set (page_to_phys (svm->avic_backing_page)) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry);
@@ -917,8 +917,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =__sme_set (page_to_phys (svm->avic_backing_page) & -AVIC_HPA_MASK ); + pi.base =__sme_set (page_to_phys (svm->avic_backing_page)); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 03/10] KVM: SVM: Drop pointless masking of kernel page pa's with "AVIC's" HPA mask
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 2 lines
Drop
AVIC_HPA_MASKand all its users, the mask is just the 4KiB-aligned maximum theoretical physical address for x86-64 CPUs.
Typo: you mean 'current max theoretical physical address' because it might increase the future again (Intel already did increase it, couple of times).
Quoted 4 lines
All usage in KVM
masks the result ofpage_to_phys(), which on x86-64 is guaranteed to be 4KiB aligned and a legal physical address; if either of those requirements doesn't hold true, KVM has far bigger problems.
I do think that a build time assert that max physical address is as expected by AVIC, is needed.
Consider this: intel/amd releases yet another extension of the max physical address, the kernel gets updated, but for CPUs which lack this extension the max physical address will still be 52 bit and should still be respected.
My CPU for example has 48 bits in max physical address, while the kernel thinks that max valid physical address is 52 bit.
I understand that this is a theoretical problem but at least a build time assert that kernel max physical address matches avic's should be done.
Especially if we consider the fact that this patch also fixes a purely theoretical problem as well.
I do agree that masking output of page_to_phys is pointless and even dangerous, but asserting that we are not trying to use address which is
outside of AVIC's capabilities is a good thing to have, instead of relying blindly on the kernel to do the right thing.
Quoted 84 lines
The unnecessarily masking in
avic_init_vmcb()also incorrectly assumes that SME's C-bit resides between bits 51:11; that holds true for current CPUs, but isn't required by AMD's architecture:In some implementations, the bit used may be a physical address bit
Key word being "may".
Opportunistically use the
GENMASK_ULL()version forAVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK, which is far more readable than a set of repeating Fs. Keep the macro even though it's unused, and will likely never be used, as it helps visualize the layout of an entry.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/svm.h | 6 +----- arch/x86/kvm/svm/avic.c | 11 +++++------ 2 files changed, 6 insertions(+), 11 deletions(-)arch/x86/include/asm/svm.h +1
-5@@ -250,7 +250,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) +#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK GENMASK_ULL(51, 12)#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)#define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK (0xFFULL)@@ -284,10 +284,6 @@ enum avic_ipi_failure_cause {static_assert ((AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==AVIC_MAX_PHYSICAL_ID );static_assert ((X2AVIC_MAX_PHYSICAL_ID &AVIC_PHYSICAL_MAX_INDEX_MASK ) ==X2AVIC_MAX_PHYSICAL_ID ); -#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==AVIC_HPA_MASK ); -static_assert (AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK ==GENMASK_ULL (51 ,12 )); -#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)struct vmcb_seg {arch/x86/kvm/svm/avic.c +5
-6@@ -248,9 +248,9 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb)phys_addr_t lpa =__sme_set (page_to_phys (kvm_svm->avic_logical_id_table_page));phys_addr_t ppa =__sme_set (page_to_phys (kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa &AVIC_HPA_MASK ; - vmcb->control.avic_logical_id = lpa &AVIC_HPA_MASK ; - vmcb->control.avic_physical_id = ppa &AVIC_HPA_MASK ; + vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_logical_id = lpa; + vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;if (kvm_apicv_activated (svm->vcpu.kvm))@@ -308,7 +308,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set (page_to_phys (svm->avic_backing_page) &AVIC_HPA_MASK ) | + new_entry =__sme_set (page_to_phys (svm->avic_backing_page)) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry);@@ -917,8 +917,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =__sme_set (page_to_phys (svm->avic_backing_page) & -AVIC_HPA_MASK ); + pi.base =__sme_set (page_to_phys (svm->avic_backing_page)); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;
Sean Christopherson
maintainerPATCH 4/10 KVM: SVM: Add helper to deduplicate code for getting AVIC backing page
In reply to Sean ChristophersonAdd a helper to get the physical address of the AVIC backing page, both to deduplicate code and to prepare for getting the address directly from apic->regs, at which point it won't be all that obvious that the address in question is what SVM calls the AVIC backing page.
No functional change intended.
--- arch/x86/kvm/svm/avic.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/avic.c +9 -5
@@ -241,14 +241,18 @@ int avic_vm_init(struct kvm *kvm)return err; } +static phys_addr_t avic_get_backing_page_address (struct vcpu_svm *svm) +{ +return __sme_set (page_to_phys (svm->avic_backing_page)); +} +void avic_init_vmcb (struct vcpu_svm *svm,struct vmcb *vmcb) {struct kvm_svm *kvm_svm =to_kvm_svm (svm->vcpu.kvm); -phys_addr_t bpa =__sme_set (page_to_phys (svm->avic_backing_page));phys_addr_t lpa =__sme_set (page_to_phys (kvm_svm->avic_logical_id_table_page));phys_addr_t ppa =__sme_set (page_to_phys (kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_backing_page =avic_get_backing_page_address (svm); vmcb->control.avic_logical_id = lpa; vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;
@@ -308,7 +312,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set (page_to_phys (svm->avic_backing_page)) | + new_entry =avic_get_backing_page_address (svm) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry);
@@ -859,7 +863,7 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,pr_debug ("SVM: %s: use GA mode for irq %u\n" , __func__, irq.vector); *svm =to_svm (vcpu); - vcpu_info->pi_desc_addr =__sme_set (page_to_phys ((*svm)->avic_backing_page)); + vcpu_info->pi_desc_addr =avic_get_backing_page_address (*svm); vcpu_info->vector = irq.vector;return 0 ;
@@ -917,7 +921,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =__sme_set (page_to_phys (svm->avic_backing_page)); + pi.base =avic_get_backing_page_address (svm); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 04/10] KVM: SVM: Add helper to deduplicate code for getting AVIC backing page
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 56 lines
Add a helper to get the physical address of the AVIC backing page, both to deduplicate code and to prepare for getting the address directly from
apic->regs, at which point it won't be all that obvious that the address in question is what SVM calls the AVIC backing page.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)arch/x86/kvm/svm/avic.c +8
-4@@ -241,14 +241,18 @@ int avic_vm_init(struct kvm *kvm)return err; } +static phys_addr_t avic_get_backing_page_address (struct vcpu_svm *svm) +{ +return __sme_set (page_to_phys (svm->avic_backing_page)); +} +void avic_init_vmcb (struct vcpu_svm *svm,struct vmcb *vmcb) {struct kvm_svm *kvm_svm =to_kvm_svm (svm->vcpu.kvm); -phys_addr_t bpa =__sme_set (page_to_phys (svm->avic_backing_page));phys_addr_t lpa =__sme_set (page_to_phys (kvm_svm->avic_logical_id_table_page));phys_addr_t ppa =__sme_set (page_to_phys (kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_backing_page =avic_get_backing_page_address (svm); vmcb->control.avic_logical_id = lpa; vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar =APIC_DEFAULT_PHYS_BASE ;@@ -308,7 +312,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)if (!entry)return -EINVAL ; - new_entry =__sme_set (page_to_phys (svm->avic_backing_page)) | + new_entry =avic_get_backing_page_address (svm) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (*entry, new_entry);@@ -859,7 +863,7 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,pr_debug ("SVM: %s: use GA mode for irq %u\n" , __func__, irq.vector); *svm =to_svm (vcpu); - vcpu_info->pi_desc_addr =__sme_set (page_to_phys ((*svm)->avic_backing_page)); + vcpu_info->pi_desc_addr =avic_get_backing_page_address (*svm); vcpu_info->vector = irq.vector;return 0 ;
arch/x86/kvm/svm/avic.c int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, · line 927
Quoted 8 lines
arch/x86/kvm/svm/avic.c +1
-1@@ -917,7 +921,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =__sme_set (page_to_phys (svm->avic_backing_page)); + pi.base =avic_get_backing_page_address (svm); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 5/10 KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field
In reply to Sean ChristophersonDrop vcpu_svm's avic_backing_page pointer and instead grab the physical address of KVM's vAPIC page directly from the source. Getting a physical address from a kernel virtual address is not an expensive operation, and getting the physical address from a struct page is *more* expensive for CONFIG_SPARSEMEM=y kernels. Regardless, none of the paths that consume the address are hot paths, i.e. shaving cycles is not a priority.
Eliminating the "cache" means KVM doesn't have to worry about the cache being invalid, which will simplify a future fix when dealing with vCPU IDs that are too big.
--- arch/x86/kvm/svm/avic.c | 4 +--- arch/x86/kvm/svm/svm.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-)
arch/x86/kvm/svm/avic.c +1 -3
@@ -243,7 +243,7 @@ int avic_vm_init(struct kvm *kvm)static phys_addr_t avic_get_backing_page_address (struct vcpu_svm *svm) { -return __sme_set (page_to_phys (svm->avic_backing_page)); +return __sme_set (__pa (svm->vcpu.arch.apic->regs)); }void avic_init_vmcb (struct vcpu_svm *svm,struct vmcb *vmcb)
@@ -305,8 +305,6 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return ret; } - svm->avic_backing_page =virt_to_page (vcpu->arch.apic->regs); -/* Setting AVIC backing page address in the phy APIC ID table */ entry =avic_get_physical_id_entry (vcpu, id);if (!entry)
arch/x86/kvm/svm/svm.h +0 -1
@@ -261,7 +261,6 @@ struct vcpu_svm {u32 ldr_reg;u32 dfr_reg; -struct page *avic_backing_page;u64 *avic_physical_id_cache;/*
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 05/10] KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 22 lines
Drop vcpu_svm's
avic_backing_pagepointer and instead grab the physical address of KVM's vAPIC page directly from the source. Getting a physical address from a kernel virtual address is not an expensive operation, and getting the physical address from a struct page is *more* expensive forCONFIG_SPARSEMEM=ykernels. Regardless, none of the paths that consume the address are hot paths, i.e. shaving cycles is not a priority.Eliminating the "cache" means KVM doesn't have to worry about the cache being invalid, which will simplify a future fix when dealing with vCPU IDs that are too big.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 4 +--- arch/x86/kvm/svm/svm.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-)arch/x86/kvm/svm/avic.c +0
-0
arch/x86/kvm/svm/avic.c int avic_vm_init(struct kvm *kvm) · line 246
Quoted 5 lines
arch/x86/kvm/svm/avic.c +1
-1@@ -243,7 +243,7 @@ int avic_vm_init(struct kvm *kvm)static phys_addr_t avic_get_backing_page_address (struct vcpu_svm *svm) { -return __sme_set (page_to_phys (svm->avic_backing_page)); +return __sme_set (__pa (svm->vcpu.arch.apic->regs));
I overall agree with this patch however the old code was safer:
svm->avic_backing_page is set to physical address of the apic registers only in the avic_init_backing_page() and after checking the vcpu->arch.apic->regs != NULL and now in theory NULL vcpu->arch.apic->regs are not checked.
I know that you later add a patch which adds a similar warning, but I prefer that you fold it with this patch.
Quoted 17 lines
arch/x86/kvm/svm/avic.c +0
-2}void avic_init_vmcb (struct vcpu_svm *svm,struct vmcb *vmcb)@@ -305,8 +305,6 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return ret; } - svm->avic_backing_page =virt_to_page (vcpu->arch.apic->regs); -/* Setting AVIC backing page address in the phy APIC ID table */ entry =avic_get_physical_id_entry (vcpu, id);if (!entry)arch/x86/kvm/svm/svm.h +0
-0
arch/x86/kvm/svm/svm.h struct vcpu_svm { · line 266
Quoted 7 lines
arch/x86/kvm/svm/svm.h +0
-1@@ -261,7 +261,6 @@ struct vcpu_svm {u32 ldr_reg;u32 dfr_reg; -struct page *avic_backing_page;u64 *avic_physical_id_cache;/*
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 6/10 iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr
In reply to Sean ChristophersonUse vcpu_data.pi_desc_addr instead of amd_iommu_pi_data.base to get the GA root pointer. KVM is the only source of amd_iommu_pi_data.base, and KVM's one and only path for writing amd_iommu_pi_data.base computes the exact same value for vcpu_data.pi_desc_addr and amd_iommu_pi_data.base, and fills amd_iommu_pi_data.base if and only if vcpu_data.pi_desc_addr is valid, i.e. amd_iommu_pi_data.base is fully redundant.
--- arch/x86/kvm/svm/avic.c | 1 - drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/kvm/svm/avic.c +0 -1
@@ -919,7 +919,6 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =avic_get_backing_page_address (svm); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;
drivers/iommu/amd/iommu.c +1 -1
@@ -3622,7 +3622,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) pi_data->prev_ga_tag = ir_data->cached_ga_tag;if (pi_data->is_guest_mode) { - ir_data->ga_root_ptr = (pi_data->base >>12 ); + ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >>12 ); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; ret =amd_iommu_activate_guest_mode (ir_data);
include/linux/amd-iommu.h +0 -1
@@ -20,7 +20,6 @@ struct amd_iommu;struct amd_iommu_pi_data {u32 ga_tag;u32 prev_ga_tag; -u64 base;bool is_guest_mode;struct vcpu_data *vcpu_data;void *ir_data;
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 06/10] iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr
Quoted 8 lines · Sean Christopherson
Use vcpu_data.pi_desc_addr instead of amd_iommu_pi_data.base to get the GA root pointer. KVM is the only source of amd_iommu_pi_data.base, and KVM's one and only path for writing amd_iommu_pi_data.base computes the exact same value for vcpu_data.pi_desc_addr and amd_iommu_pi_data.base, and fills amd_iommu_pi_data.base if and only if vcpu_data.pi_desc_addr is valid, i.e. amd_iommu_pi_data.base is fully redundant.
Signed-off-by Sean Christopherson <[email protected]>
Nice cleanup,
Quoted 43 lines
--- arch/x86/kvm/svm/avic.c | 1 - drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-)arch/x86/kvm/svm/avic.c +0
-1@@ -919,7 +919,6 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =avic_get_backing_page_address (svm); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;drivers/iommu/amd/iommu.c +1
-1@@ -3622,7 +3622,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) pi_data->prev_ga_tag = ir_data->cached_ga_tag;if (pi_data->is_guest_mode) { - ir_data->ga_root_ptr = (pi_data->base >>12 ); + ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >>12 ); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; ret =amd_iommu_activate_guest_mode (ir_data);include/linux/amd-iommu.h +0
-1@@ -20,7 +20,6 @@ struct amd_iommu;struct amd_iommu_pi_data {u32 ga_tag;u32 prev_ga_tag; -u64 base;bool is_guest_mode;struct vcpu_data *vcpu_data;void *ir_data;
Re: [PATCH 06/10] iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr
In reply to Sean ChristophersonУ вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 44 lines
Use vcpu_data.pi_desc_addr instead of amd_iommu_pi_data.base to get the GA root pointer. KVM is the only source of amd_iommu_pi_data.base, and KVM's one and only path for writing amd_iommu_pi_data.base computes the exact same value for vcpu_data.pi_desc_addr and amd_iommu_pi_data.base, and fills amd_iommu_pi_data.base if and only if vcpu_data.pi_desc_addr is valid, i.e. amd_iommu_pi_data.base is fully redundant.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 1 - drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-)arch/x86/kvm/svm/avic.c +0
-1@@ -919,7 +919,6 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,struct amd_iommu_pi_data pi;/* Try to enable guest_mode in IRTE */ - pi.base =avic_get_backing_page_address (svm); pi.ga_tag =AVIC_GATAG (to_kvm_svm (kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true;drivers/iommu/amd/iommu.c +1
-1@@ -3622,7 +3622,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) pi_data->prev_ga_tag = ir_data->cached_ga_tag;if (pi_data->is_guest_mode) { - ir_data->ga_root_ptr = (pi_data->base >>12 ); + ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >>12 ); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; ret =amd_iommu_activate_guest_mode (ir_data);include/linux/amd-iommu.h +0
-0
include/linux/amd-iommu.h struct amd_iommu; · line 23
Quoted 4 lines
include/linux/amd-iommu.h +0
-1@@ -20,7 +20,6 @@ struct amd_iommu;struct amd_iommu_pi_data {u32 ga_tag;u32 prev_ga_tag; -u64 base;
I think that a comment describing where the base address of the apic register page is taken now is needed, because before your patch the 'amd_iommu_pi_data' was self documenting, since it had all the info about the PI remap entry, but now it doesn't.
include/linux/amd-iommu.h struct amd_iommu; · line 25
Quoted 3 lines
include/linux/amd-iommu.h +0
-0bool is_guest_mode;struct vcpu_data *vcpu_data;void *ir_data;
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 7/10 KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU creation
In reply to Sean ChristophersonInhibit AVIC with a new "ID too big" flag if userspace creates a vCPU with an ID that is too big, but otherwise allow vCPU creation to succeed. Rejecting KVM_CREATE_VCPU with EINVAL violates KVM's ABI as KVM advertises that the max vCPU ID is 4095, but disallows creating vCPUs with IDs bigger than 254 (AVIC) or 511 (x2AVIC).
Alternatively, KVM could advertise an accurate value depending on which AVIC mode is in use, but that wouldn't really solve the underlying problem, e.g. would be a breaking change if KVM were to ever try and enable AVIC or x2AVIC by default.
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm/avic.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -1243,6 +1243,12 @@ enum kvm_apicv_inhibit {* mapping between logical ID and vCPU. */ APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED , + +/* +* AVIC is disabled because the vCPU's APIC ID is beyond the max +* supported by AVIC/x2AVIC, i.e. the vCPU is unaddressable. +*/ + APICV_INHIBIT_REASON_ID_TOO_BIG, }; struct kvm_arch {
arch/x86/kvm/svm/avic.c +14 -2
@@ -284,9 +284,21 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)int id = vcpu->vcpu_id;struct vcpu_svm *svm =to_svm (vcpu); +/* +* Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC +* hardware. Do so immediately, i.e. don't defer the update via a +* request, as avic_vcpu_load() expects to be called if and only if the +* vCPU has fully initialized AVIC. Bypass all of the helpers and just +* clear apicv_active directly, the vCPU isn't reachable and the VMCB +* isn't even initialized at this point, i.e. there is no possibility +* of needing to deal with the n +*/ if ((!x2avic_enabled && id >AVIC_MAX_PHYSICAL_ID ) || - (id >X2AVIC_MAX_PHYSICAL_ID )) -return -EINVAL ; + (id >X2AVIC_MAX_PHYSICAL_ID )) { +kvm_set_apicv_inhibit (vcpu->kvm,APICV_INHIBIT_REASON_ID_TOO_BIG ); + vcpu->arch.apic->apicv_active = false; +return 0 ; + }if (!vcpu->arch.apic->regs)return -EINVAL ;
arch/x86/kvm/svm/svm.h +2 -1
@@ -632,7 +632,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops; BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | \ BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \ BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \ - BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) \ + BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) | \ + BIT(APICV_INHIBIT_REASON_ID_TOO_BIG) \ )bool avic_hardware_setup (void );
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 07/10] KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU creation
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 24 lines
Inhibit AVIC with a new "ID too big" flag if userspace creates a vCPU with an ID that is too big, but otherwise allow vCPU creation to succeed. Rejecting
KVM_CREATE_VCPUwith EINVAL violates KVM's ABI as KVM advertises that the max vCPU ID is 4095, but disallows creating vCPUs with IDs bigger than 254 (AVIC) or 511 (x2AVIC).Alternatively, KVM could advertise an accurate value depending on which AVIC mode is in use, but that wouldn't really solve the underlying problem, e.g. would be a breaking change if KVM were to ever try and enable AVIC or x2AVIC by default.
Cc Maxim Levitsky <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm/avic.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h enum kvm_apicv_inhibit { · line 1251
Quoted 9 lines
arch/x86/include/asm/kvm_host.h +6
-0@@ -1243,6 +1243,12 @@ enum kvm_apicv_inhibit {* mapping between logical ID and vCPU. */ APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED , + +/* +* AVIC is disabled because the vCPU's APIC ID is beyond the max +* supported by AVIC/x2AVIC, i.e. the vCPU is unaddressable. +*/ + APICV_INHIBIT_REASON_ID_TOO_BIG,
I prefer APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG
Quoted 9 lines
arch/x86/include/asm/kvm_host.h +0
-0};struct kvm_arch {arch/x86/kvm/svm/avic.c +0
-0@@ -284,9 +284,21 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)int id = vcpu->vcpu_id;
arch/x86/kvm/svm/avic.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 294
Quoted 10 lines
arch/x86/kvm/svm/avic.c +8
-0struct vcpu_svm *svm =to_svm (vcpu); +/* +* Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC +* hardware. Do so immediately, i.e. don't defer the update via a +* request, as avic_vcpu_load() expects to be called if and only if the +* vCPU has fully initialized AVIC. Bypass all of the helpers and just +* clear apicv_active directly, the vCPU isn't reachable and the VMCB +* isn't even initialized at this point, i.e. there is no possibility +* of needing to deal with the n
Which helpers do you bypass? I see a call to normal kvm_set_apicv_inhibit() as it should be.
Note that userspace can add vCPUs at will so this can happen any time during the guest's lifetime so I don't think that this code can bypass anything.
Quoted 26 lines
arch/x86/kvm/svm/avic.c +6
-2+*/ if ((!x2avic_enabled && id >AVIC_MAX_PHYSICAL_ID ) || - (id >X2AVIC_MAX_PHYSICAL_ID )) -return -EINVAL ; + (id >X2AVIC_MAX_PHYSICAL_ID )) { +kvm_set_apicv_inhibit (vcpu->kvm,APICV_INHIBIT_REASON_ID_TOO_BIG ); + vcpu->arch.apic->apicv_active = false; +return 0 ; + }if (!vcpu->arch.apic->regs)return -EINVAL ;arch/x86/kvm/svm/svm.h +2
-1@@ -632,7 +632,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops;BIT (APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED ) | \BIT (APICV_INHIBIT_REASON_APIC_ID_MODIFIED ) | \BIT (APICV_INHIBIT_REASON_APIC_BASE_MODIFIED ) | \ -BIT (APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED ) \ +BIT (APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED ) | \ +BIT (APICV_INHIBIT_REASON_ID_TOO_BIG ) \ )
Quoted 1 line
bool
avic_hardware_setup(void);
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 8/10 KVM: SVM: WARN if KVM attempts to create AVIC backing page with user APIC
In reply to Sean ChristophersonWARN if KVM attempts to allocate a vCPU's AVIC backing page without an in-kernel local APIC. avic_init_vcpu() bails early if the APIC is not in-kernel, and KVM disallows enabling an in-kernel APIC after vCPUs have been created, i.e. it should be impossible to reach avic_init_backing_page() without the vAPIC being allocated.
--- arch/x86/kvm/svm/avic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/avic.c +1 -1
@@ -300,7 +300,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return 0 ; } -if (!vcpu->arch.apic->regs) +if (WARN_ON_ONCE (!vcpu->arch.apic->regs))return -EINVAL ;if (kvm_apicv_activated (vcpu->kvm)) {
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 08/10] KVM: SVM: WARN if KVM attempts to create AVIC backing page with user APIC
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 16 lines
WARN if KVM attempts to allocate a vCPU's AVIC backing page without an in-kernel local APIC.
avic_init_vcpu()bails early if the APIC is not in-kernel, and KVM disallows enabling an in-kernel APIC after vCPUs have been created, i.e. it should be impossible to reachavic_init_backing_page()without the vAPIC being allocated.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/svm/avic.c +0
-0
arch/x86/kvm/svm/avic.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 306
Quoted 8 lines
arch/x86/kvm/svm/avic.c +1
-1@@ -300,7 +300,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return 0 ; } -if (!vcpu->arch.apic->regs) +if (WARN_ON_ONCE (!vcpu->arch.apic->regs))return -EINVAL ;if (kvm_apicv_activated (vcpu->kvm)) {
As I said I prefer this to be folded with patch that adds the avic_init_backing_page().
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 9/10 KVM: SVM: Drop redundant check in AVIC code on ID during vCPU creation
In reply to Sean ChristophersonDrop avic_get_physical_id_entry()'s compatibility check on the incoming ID, as its sole caller, avic_init_backing_page(), performs the exact same check. Drop avic_get_physical_id_entry() entirely as the only remaining functionality is getting the address of the Physical ID table, and accessing the array without an immediate bounds check is kludgy.
No functional change intended.
--- arch/x86/kvm/svm/avic.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-)
arch/x86/kvm/svm/avic.c +6 -22
@@ -263,26 +263,12 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb)avic_deactivate_vmcb (svm); } -static u64 *avic_get_physical_id_entry (struct kvm_vcpu *vcpu, -unsigned int index) -{ -u64 *avic_physical_id_table; -struct kvm_svm *kvm_svm =to_kvm_svm (vcpu->kvm); - -if ((!x2avic_enabled && index >AVIC_MAX_PHYSICAL_ID ) || - (index >X2AVIC_MAX_PHYSICAL_ID )) -return NULL ; - - avic_physical_id_table =page_address (kvm_svm->avic_physical_id_table_page); - -return &avic_physical_id_table[index]; -} -static int avic_init_backing_page (struct kvm_vcpu *vcpu) { -u64 *entry, new_entry; +struct kvm_svm *kvm_svm =to_kvm_svm (vcpu->kvm); +struct vcpu_svm *svm =to_svm (vcpu); +u64 *table, new_entry;int id = vcpu->vcpu_id; -struct vcpu_svm *svm =to_svm (vcpu);/* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC
@@ -318,15 +304,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) }/* Setting AVIC backing page address in the phy APIC ID table */ - entry =avic_get_physical_id_entry (vcpu, id); -if (!entry) -return -EINVAL ; + table =page_address (kvm_svm->avic_physical_id_table_page); new_entry =avic_get_backing_page_address (svm) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ; -WRITE_ONCE (*entry, new_entry); +WRITE_ONCE (table[id], new_entry); - svm->avic_physical_id_cache = entry; + svm->avic_physical_id_cache = &table[id];return 0 ; }
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 09/10] KVM: SVM: Drop redundant check in AVIC code on ID during vCPU creation
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 20 lines
Drop avic_get_physical_id_entry()'s compatibility check on the incoming ID, as its sole caller,
avic_init_backing_page(), performs the exact same check. Dropavic_get_physical_id_entry()entirely as the only remaining functionality is getting the address of the Physical ID table, and accessing the array without an immediate bounds check is kludgy.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-)arch/x86/kvm/svm/avic.c +0
-0@@ -263,26 +263,12 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb)avic_deactivate_vmcb (svm); }
arch/x86/kvm/svm/avic.c void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) · line 274
Quoted 10 lines
arch/x86/kvm/svm/avic.c +0
-9-static u64 *avic_get_physical_id_entry (struct kvm_vcpu *vcpu, -unsigned int index) -{ -u64 *avic_physical_id_table; -struct kvm_svm *kvm_svm =to_kvm_svm (vcpu->kvm); - -if ((!x2avic_enabled && index >AVIC_MAX_PHYSICAL_ID ) || - (index >X2AVIC_MAX_PHYSICAL_ID )) -return NULL ;
While removing this code doesn't introduce a bug, it does make it less safe, because new code just blindly trusts that vcpu_id will never be out of bounds of the physical id table.
Bugs happen and that can and will someday happen.
Quoted 19 lines
arch/x86/kvm/svm/avic.c +3
-8- - avic_physical_id_table =page_address (kvm_svm->avic_physical_id_table_page); - -return &avic_physical_id_table[index]; -} -static int avic_init_backing_page (struct kvm_vcpu *vcpu) { -u64 *entry, new_entry; +struct kvm_svm *kvm_svm =to_kvm_svm (vcpu->kvm); +struct vcpu_svm *svm =to_svm (vcpu); +u64 *table, new_entry;int id = vcpu->vcpu_id; -struct vcpu_svm *svm =to_svm (vcpu);/* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC @@ -318,15 +304,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) }
arch/x86/kvm/svm/avic.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 327
Quoted 10 lines
arch/x86/kvm/svm/avic.c +1
-4/* Setting AVIC backing page address in the phy APIC ID table */ - entry =avic_get_physical_id_entry (vcpu, id); -if (!entry) -return -EINVAL ; + table =page_address (kvm_svm->avic_physical_id_table_page); new_entry =avic_get_backing_page_address (svm) |AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ; -WRITE_ONCE (*entry, new_entry);
Here I prefer to at least have an assert that id is in bounds of a page (at least less than 512) so that a bug will not turn into a security issue by overflowing the buffer.
arch/x86/kvm/svm/avic.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 316
Quoted 7 lines
arch/x86/kvm/svm/avic.c +2
-1+WRITE_ONCE (table[id], new_entry); - svm->avic_physical_id_cache = entry; + svm->avic_physical_id_cache = &table[id];return 0 ; }
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerPATCH 10/10 KVM: SVM: Rename "avic_physical_id_cache" to "avic_physical_id_entry"
In reply to Sean ChristophersonRename the vCPU's pointer to its AVIC Physical ID entry from "cache" to "entry". While the field technically caches the result of the pointer calculation, it's all too easy to misinterpret the name and think that the field somehow caches the _data_ in the table.
No functional change intended.
--- arch/x86/kvm/svm/avic.c | 10 +++++----- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/avic.c +5 -5
@@ -310,7 +310,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (table[id], new_entry); - svm->avic_physical_id_cache = &table[id]; + svm->avic_physical_id_entry = &table[id];return 0 ; }
@@ -1028,14 +1028,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)if (kvm_vcpu_is_blocking (vcpu))return ; - entry =READ_ONCE (*(svm->avic_physical_id_cache)); + entry =READ_ONCE (*(svm->avic_physical_id_entry));WARN_ON_ONCE (entry &AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ); entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK ; entry |= (h_physical_id &AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK ); entry |=AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ; -WRITE_ONCE (*(svm->avic_physical_id_cache), entry); +WRITE_ONCE (*(svm->avic_physical_id_entry), entry);avic_update_iommu_vcpu_affinity (vcpu, h_physical_id, true); }
@@ -1046,7 +1046,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)lockdep_assert_preemption_disabled (); - entry =READ_ONCE (*(svm->avic_physical_id_cache)); + entry =READ_ONCE (*(svm->avic_physical_id_entry));/* Nothing to do if IsRunning == '0' due to vCPU blocking. */ if (!(entry &AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ))
@@ -1055,7 +1055,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)avic_update_iommu_vcpu_affinity (vcpu,-1 ,0 ); entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ; -WRITE_ONCE (*(svm->avic_physical_id_cache), entry); +WRITE_ONCE (*(svm->avic_physical_id_entry), entry); }void avic_refresh_virtual_apic_mode (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +1 -1
@@ -261,7 +261,7 @@ struct vcpu_svm {u32 ldr_reg;u32 dfr_reg; -u64 *avic_physical_id_cache; +u64 *avic_physical_id_entry;/* * Per-vcpu list of struct amd_svm_iommu_ir:
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Re: [PATCH 10/10] KVM: SVM: Rename "avic_physical_id_cache" to "avic_physical_id_entry"
У вт, 2023-08-15 у 14:35 -0700, Sean Christopherson пише:
Quoted 4 lines
Rename the vCPU's pointer to its AVIC Physical ID entry from "cache" to "entry". While the field technically caches the result of the pointer calculation, it's all too easy to misinterpret the name and think that the field somehow caches the _data_ in the table.
I also strongly dislike the 'avic_physical_id_cache', but if you are refactoring it, IMHO the 'avic_physical_id_entry' is just as confusing since its a pointer to an entry and not the entry itself.
At least a comment to explain where this pointer points, or maybe (not sure) drop the avic_physical_id_cache completely and calculate it every time (I doubt that there is any perf loss due to this)
Best regards,
Maxim Levitsky
Quoted 70 lines
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 10 +++++----- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-)arch/x86/kvm/svm/avic.c +5
-5@@ -310,7 +310,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)AVIC_PHYSICAL_ID_ENTRY_VALID_MASK ;WRITE_ONCE (table[id], new_entry); - svm->avic_physical_id_cache = &table[id]; + svm->avic_physical_id_entry = &table[id];return 0 ; }@@ -1028,14 +1028,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)if (kvm_vcpu_is_blocking (vcpu))return ; - entry =READ_ONCE (*(svm->avic_physical_id_cache)); + entry =READ_ONCE (*(svm->avic_physical_id_entry));WARN_ON_ONCE (entry &AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ); entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK ; entry |= (h_physical_id &AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK ); entry |=AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ; -WRITE_ONCE (*(svm->avic_physical_id_cache), entry); +WRITE_ONCE (*(svm->avic_physical_id_entry), entry);avic_update_iommu_vcpu_affinity (vcpu, h_physical_id, true); }@@ -1046,7 +1046,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)lockdep_assert_preemption_disabled (); - entry =READ_ONCE (*(svm->avic_physical_id_cache)); + entry =READ_ONCE (*(svm->avic_physical_id_entry));/* Nothing to do if IsRunning == '0' due to vCPU blocking. */ if (!(entry &AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ))@@ -1055,7 +1055,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)avic_update_iommu_vcpu_affinity (vcpu,-1 ,0 ); entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ; -WRITE_ONCE (*(svm->avic_physical_id_cache), entry); +WRITE_ONCE (*(svm->avic_physical_id_entry), entry); }void avic_refresh_virtual_apic_mode (struct kvm_vcpu *vcpu)arch/x86/kvm/svm/svm.h +1
-1@@ -261,7 +261,7 @@ struct vcpu_svm {u32 ldr_reg;u32 dfr_reg; -u64 *avic_physical_id_cache; +u64 *avic_physical_id_entry;/* * Per-vcpu list of struct amd_svm_iommu_ir: