KVM TDX basic feature support
Subsystems (17)
PATCH v7 37/102 KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to kvm_arch and track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working.
In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM searches backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. Once MMIO is triggered on the EPT entry, the EPT entry is updated to trigger EPT misconfiguration for the future MMIO on the same GPA. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to understand the value and access width for MMIO. Instead, VMM sets up (Shared) EPT to trigger #VE by clearing the VE-suppress bit. When the guest TD issues MMIO, #VE is injected. Guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instructions.
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu.h | 4 +++- arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++---- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 41 +++++++++++++++------------------ arch/x86/kvm/mmu/spte.h | 11 ++++----- arch/x86/kvm/mmu/tdp_mmu.c | 6 ++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 8 +++++++ 10 files changed, 59 insertions(+), 40 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1161,6 +1161,10 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +bool enable_mmio_caching; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;
arch/x86/include/asm/vmx.h +1 -0
@@ -511,6 +511,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)
arch/x86/kvm/mmu.h +3 -1
@@ -108,7 +108,9 @@ static inline u8 kvm_get_shadow_phys_bits(void)return boot_cpu_data.x86_phys_bits; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only);
arch/x86/kvm/mmu/mmu.c +15 -5
@@ -2287,7 +2287,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
@@ -3067,8 +3067,13 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* by L0 userspace (you can observe gfn > L1.MAXPHYADDR if * and only if L1's MAXPHYADDR is inaccurate with respect to * the hardware's). +* +* Excludes the INTEL TD guest. Because TD memory is +* protected, the instruction can't be emulated. Instead, use +* SPTE value without #VE suppress bit cleared +* (kvm->arch.shadow_mmio_value = 0). */ -if (unlikely (!enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }
@@ -3200,7 +3205,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)else sptep =fast_pf_get_last_sptep (vcpu, fault->addr, &spte); -if (!is_shadow_present_pte (spte) ||is_mmio_spte (spte)) +if (!is_shadow_present_pte (spte) || +is_mmio_spte (vcpu->kvm, spte))break ; sp =sptep_to_sp (sptep);
@@ -3907,7 +3913,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);
@@ -4350,7 +4356,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
@@ -5864,6 +5870,10 @@ int kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); +kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, + shadow_default_mmio_mask, +ACC_WRITE_MASK |ACC_USER_MASK ); +return 0 ; }
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -1032,7 +1032,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)gfn_t gfn;if (!is_shadow_present_pte (sp->spt[i]) && - !is_mmio_spte (sp->spt[i])) + !is_mmio_spte (vcpu->kvm, sp->spt[i]))continue ; pte_gpa = first_pte_gpa + i *sizeof (pt_element_t);
arch/x86/kvm/mmu/spte.c +19 -22
88 lines not on this page · raw message
arch/x86/kvm/mmu/spte.h +4 -7
@@ -5,8 +5,6 @@#include "mmu_internal.h" -extern bool __read_mostly enable_mmio_caching; -/* * A MMU present SPTE is backed by actual memory and may or may not be present * in hardware. E.g. MMIO SPTEs are not considered present. Use bit 11, as it
@@ -160,8 +158,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_value;
@@ -233,10 +230,10 @@ static inline bool is_removed_spte(u64 spte)*/ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (enable_mmio_caching); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.enable_mmio_caching ||kvm_gfn_shared_mask (kvm)); }static inline bool is_shadow_present_pte (u64 pte)
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -569,8 +569,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"
@@ -1108,7 +1108,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) { vcpu->stat.pf_mmio_spte_created++;trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte);
arch/x86/kvm/svm/svm.c +1 -1
@@ -4870,7 +4870,7 @@ static __init void svm_adjust_mmio_mask(void)*/ mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); +kvm_mmu_set_default_mmio_spte_mask (mask); }static __init void svm_set_cpu_caps (void )
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -7289,6 +7289,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_RWX_MASK ,0 ); +if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
Signature · 1 line
2.25.1
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 17 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to
kvm_archand track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working.In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM searches backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. Once MMIO is triggered on the EPT entry, the EPT entry is updated to trigger EPT misconfiguration for the future MMIO on the same GPA. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
Quoted 8 lines
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to understand the value and access width for MMIO. Instead, VMM sets up (Shared) EPT to trigger #VE by clearing the VE-suppress bit. When the guest TD issues MMIO, #VE is injected. Guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instructions.
This is an infrastructural patch which enables per-VM MMIO caching. Why not putting this patch first so you don't need to do below changes (which are introduced by your previous patches)?
[...]
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +2
-1-if (!is_shadow_present_pte (spte) ||is_mmio_spte (spte)) +if (!is_shadow_present_pte (spte) || + is_mmio_spte(vcpu->kvm, spte))break;
[...]
Quoted 7 lines
arch/x86/kvm/mmu/paging_tmpl.h +0
-0@@ -1032,7 +1032,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) gfn_t gfn;
if (!is_shadow_present_pte(sp->spt[i]) &&arch/x86/kvm/mmu/paging_tmpl.h +1
-1- !is_mmio_spte(sp->spt[i])) + !is_mmio_spte(vcpu->kvm, sp->spt[i]))continue;
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]Quoted 17 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to
kvm_archand track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working.In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM searches backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. Once MMIO is triggered on the EPT entry, the EPT entry is updated to trigger EPT misconfiguration for the future MMIO on the same GPA. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
Quoted 8 lines
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to understand the value and access width for MMIO. Instead, VMM sets up (Shared) EPT to trigger #VE by clearing the VE-suppress bit. When the guest TD issues MMIO, #VE is injected. Guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instructions.
To me only first paragraph is needed. It already describes _why_ we need this patch and _how_ you are going to implement.
The last two paragraphs only elaborate the _why_ in the first paragraph, but they does not say this patch will do more. And they have been explained in previous patches so looks they are not mandatory here.
Quoted 36 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu.h | 4 +++- arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++---- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 41 +++++++++++++++------------------ arch/x86/kvm/mmu/spte.h | 11 ++++----- arch/x86/kvm/mmu/tdp_mmu.c | 6 ++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 8 +++++++ 10 files changed, 59 insertions(+), 40 deletions(-)arch/x86/include/asm/kvm_host.h +4
-0@@ -1161,6 +1161,10 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +bool enable_mmio_caching; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;arch/x86/include/asm/vmx.h +0
-0
arch/x86/include/asm/vmx.h enum vmcs_field { · line 514
Quoted 4 lines
arch/x86/include/asm/vmx.h +1
-0@@ -511,6 +511,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)
Both the patch title and the changelog say this patch only does per-VM MMIO value/mask tracking. Why do we need this bit here?
Quoted 32 lines
arch/x86/include/asm/vmx.h +0
-0#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)arch/x86/kvm/mmu.h +3
-1@@ -108,7 +108,9 @@ static inline u8 kvm_get_shadow_phys_bits(void)return boot_cpu_data.x86_phys_bits; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only);arch/x86/kvm/mmu/mmu.c +1
-1@@ -2287,7 +2287,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
arch/x86/kvm/mmu/mmu.c static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau · line 3075
Quoted 9 lines
arch/x86/kvm/mmu/mmu.c +5
-0@@ -3067,8 +3067,13 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* by L0 userspace (you can observe gfn > L1.MAXPHYADDR if * and only if L1's MAXPHYADDR is inaccurate with respect to * the hardware's). +* +* Excludes the INTEL TD guest. Because TD memory is +* protected, the instruction can't be emulated. Instead, use +* SPTE value without #VE suppress bit cleared +* (kvm->arch.shadow_mmio_value = 0). */
Again, I don't think this chunk should be in this patch. It's out-of-scope of what the patch claims to do.
I see you will make below code change in later patch (couple of patches later):
- if (unlikely(!vcpu->kvm->arch.enable_mmio_caching) ||
+ if (unlikely(!vcpu->kvm->arch.enable_mmio_caching &&
+ !kvm_gfn_shared_mask(vcpu->kvm)) ||
unlikely(fault->gfn > kvm_mmu_max_gfn()))
return RET_PF_EMULATE;
So why not putting the comment and the code change together?
Quoted 34 lines
arch/x86/kvm/mmu/mmu.c +5
-4-if (unlikely (!enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }@@ -3200,7 +3205,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)else sptep =fast_pf_get_last_sptep (vcpu, fault->addr, &spte); -if (!is_shadow_present_pte (spte) ||is_mmio_spte (spte)) +if (!is_shadow_present_pte (spte) || +is_mmio_spte (vcpu->kvm, spte))break ; sp =sptep_to_sp (sptep);@@ -3907,7 +3913,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);@@ -4350,7 +4356,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
arch/x86/kvm/mmu/mmu.c int kvm_mmu_init_vm(struct kvm *kvm) · line 5876
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +4
-0@@ -5864,6 +5870,10 @@ int kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); +kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, + shadow_default_mmio_mask, +ACC_WRITE_MASK |ACC_USER_MASK ); +
This (along with shadow_default_mmio_mask) looks a little bit weird. Please also see comments below.
Quoted 21 lines
arch/x86/kvm/mmu/mmu.c +0
-0return 0 ; }arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -1032,7 +1032,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)gfn_t gfn;if (!is_shadow_present_pte (sp->spt[i]) && - !is_mmio_spte (sp->spt[i])) + !is_mmio_spte (vcpu->kvm, sp->spt[i]))continue ; pte_gpa = first_pte_gpa + i *sizeof (pt_element_t);arch/x86/kvm/mmu/spte.c +0
-0
arch/x86/kvm/mmu/spte.c u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ · line 32
Quoted 6 lines
arch/x86/kvm/mmu/spte.c +1
-2@@ -29,8 +29,7 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask; +u64 __read_mostly shadow_default_mmio_mask;
This shadow_default_mmio_mask looks a little bit weird. Please also see below.
arch/x86/kvm/mmu/spte.c u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access) · line 65
Quoted 10 lines
arch/x86/kvm/mmu/spte.c +2
-1u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_value;@@ -62,10 +61,11 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_shared_mask (vcpu->kvm));
Chunk shouldn't belong to this patch.
Quoted 33 lines
arch/x86/kvm/mmu/spte.c +6
-7access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;@@ -337,7 +337,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask) {BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);@@ -366,11 +367,9 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (__REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; -if (!mmio_value) - enable_mmio_caching = false; - - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; + kvm->arch.enable_mmio_caching = !!mmio_value; + kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; shadow_mmio_access_mask = access_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask);
arch/x86/kvm/mmu/spte.c void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only) · line 397
Quoted 7 lines
arch/x86/kvm/mmu/spte.c +3
-1@@ -393,24 +392,18 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only) shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; +/* VMX_EPT_SUPPRESS_VE_BIT is needed for W or X violation. */ + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) |VMX_EPT_SUPPRESS_VE_BIT ;
Again, this chunk shouldn't be in this patch.
Quoted 21 lines
arch/x86/kvm/mmu/spte.c +0
-8shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();
arch/x86/kvm/mmu/spte.c void kvm_mmu_reset_all_pte_masks(void) · line 458
Quoted 9 lines
arch/x86/kvm/mmu/spte.c +3
-2@@ -459,9 +452,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;else - mask =0 ; + shadow_default_mmio_mask =0 ; +}
Shadow_default_mmio_mask alone looks a little bit weird with per-VM MMIO tracking. I think it can be removed by moving this code to vmx_vm_init(), and call it as VM's MMIO mask/value for non-EPT case. If EPT is enabled, it can override using new mask/value.
Quoted 32 lines
arch/x86/kvm/mmu/spte.c +4
-1-kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask) +{ + shadow_default_mmio_mask = mask; } +EXPORT_SYMBOL_GPL (kvm_mmu_set_default_mmio_spte_mask);arch/x86/kvm/mmu/spte.h +1
-4@@ -5,8 +5,6 @@#include "mmu_internal.h" -extern bool __read_mostly enable_mmio_caching; -/* * A MMU present SPTE is backed by actual memory and may or may not be present * in hardware. E.g. MMIO SPTEs are not considered present. Use bit 11, as it @@ -160,8 +158,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_value;@@ -233,10 +230,10 @@ static inline bool is_removed_spte(u64 spte)*/
arch/x86/kvm/mmu/spte.h static inline bool is_removed_spte(u64 spte) · line 237
Quoted 10 lines
arch/x86/kvm/mmu/spte.h +3
-3extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (enable_mmio_caching); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.enable_mmio_caching ||kvm_gfn_shared_mask (kvm)); }
This chunk (checking kvm_gfn_shared_mask(kvm)) should not be in this patch.
Quoted 31 lines
arch/x86/kvm/mmu/spte.h +0
-0static inline bool is_shadow_present_pte (u64 pte)arch/x86/kvm/mmu/tdp_mmu.c +3
-3@@ -569,8 +569,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"@@ -1108,7 +1108,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) { vcpu->stat.pf_mmio_spte_created++;trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte);arch/x86/kvm/svm/svm.c +0
-0
arch/x86/kvm/svm/svm.c static __init void svm_adjust_mmio_mask(void) · line 4873
Quoted 5 lines
arch/x86/kvm/svm/svm.c +1
-1@@ -4870,7 +4870,7 @@ static __init void svm_adjust_mmio_mask(void)*/ mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); +kvm_mmu_set_default_mmio_spte_mask (mask);
SVM doesn't need shadow_default_mmio_mask. Instead, it can define a local variable in svm.c, and call kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK) in svm_vm_init().
Quoted 9 lines
arch/x86/kvm/svm/svm.c +0
-0}static __init void svm_set_cpu_caps (void )arch/x86/kvm/vmx/vmx.c +0
-0@@ -7289,6 +7289,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap)
arch/x86/kvm/vmx/vmx.c int vmx_vm_init(struct kvm *kvm) · line 7299
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +8
-0kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_RWX_MASK ,0 ); +
As commented above, I think we can remove shadow_default_mmio_mask by moving the logic in kvm_mmu_reset_all_pte_mask() here.
Or use SVM similar way, use a local variable 'mask' in vmx.c, calculate the 'mask' during hardware_setup(), and use it here for non-EPT case.
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +0
-0if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]Here is the updated one. The changes are
- removed hunks that should be a part of other patches.
- removed shadow_default_mmio_mask
- trimed down commit messages.
From ed6b4a076e515550878b069596cf156a1bc33514 Mon Sep 17 00:00:00 2001
Message-Id: <ed6b4a076e515550878b069596cf156a1bc33514.1658220363.git.isaku.yamahata@intel.com>
In-Reply-To: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658220363.git.isaku.yamahata@intel.com>
References: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658220363.git.isaku.yamahata@intel.com>
From: Sean Christopherson <[email protected]>
Date: Wed, 10 Jun 2020 15:46:38 -0700
Subject: [PATCH 036/306] KVM: x86/mmu: Track shadow MMIO value/mask on a
per-VM basis
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to kvm_arch and track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working. To untangle the logic to initialize shadow_mmio_access_mask, introduce a setter function.
--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/mmu/mmu.c | 8 +++--- arch/x86/kvm/mmu/spte.c | 45 +++++++++------------------------ arch/x86/kvm/mmu/spte.h | 10 +++----- arch/x86/kvm/mmu/tdp_mmu.c | 6 ++--- arch/x86/kvm/svm/svm.c | 11 +++++--- arch/x86/kvm/vmx/tdx.c | 4 +++ arch/x86/kvm/vmx/vmx.c | 26 +++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 1 + 10 files changed, 66 insertions(+), 52 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1161,6 +1161,10 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +bool enable_mmio_caching; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;
arch/x86/kvm/mmu.h +2 -1
@@ -108,7 +108,8 @@ static inline u8 kvm_get_shadow_phys_bits(void)return boot_cpu_data.x86_phys_bits; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask); +void kvm_mmu_set_mmio_access_mask (u64 mmio_access_mask);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only);
arch/x86/kvm/mmu/mmu.c +4 -4
@@ -2298,7 +2298,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
@@ -3079,7 +3079,7 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* and only if L1's MAXPHYADDR is inaccurate with respect to * the hardware's). */ -if (unlikely (!enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }
@@ -3918,7 +3918,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);
@@ -4361,7 +4361,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
arch/x86/kvm/mmu/spte.c +12 -33
89 lines not on this page · raw message
arch/x86/kvm/mmu/spte.h +3 -7
@@ -5,8 +5,6 @@#include "mmu_internal.h" -extern bool __read_mostly enable_mmio_caching; -/* * A MMU present SPTE is backed by actual memory and may or may not be present * in hardware. E.g. MMIO SPTEs are not considered present. Use bit 11, as it
@@ -160,8 +158,6 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_value;
@@ -228,10 +224,10 @@ static inline bool is_removed_spte(u64 spte)*/ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (enable_mmio_caching); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.enable_mmio_caching); }static inline bool is_shadow_present_pte (u64 pte)
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -569,8 +569,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"
@@ -1108,7 +1108,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) { vcpu->stat.pf_mmio_spte_created++;trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte);
arch/x86/kvm/svm/svm.c +7 -4
@@ -198,6 +198,7 @@ module_param(dump_invalid_vmcb, bool, 0644);bool intercept_smi = true;module_param (intercept_smi, bool,0444 ); +static u64 __read_mostly svm_shadow_mmio_mask;static bool svm_gp_erratum_intercept = true;
@@ -4685,6 +4686,9 @@ static bool svm_is_vm_type_supported(unsigned long type)static int svm_vm_init (struct kvm *kvm) { +kvm_mmu_set_mmio_spte_mask (kvm, svm_shadow_mmio_mask, + svm_shadow_mmio_mask); +if (!pause_filter_count || !pause_filter_thresh) kvm->arch.pause_in_guest = true;
@@ -4834,7 +4838,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {static __init void svm_adjust_mmio_mask (void ) {unsigned int enc_bit, mask_bit; -u64 msr, mask; +u64 msr;/* If there is no memory encryption support, use existing mask */ if (cpuid_eax (0x80000000 ) <0x8000001f )
@@ -4861,9 +4865,8 @@ static __init void svm_adjust_mmio_mask(void)* * If the mask bit location is 52 (or above), then clear the mask. */ - mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; - -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); + svm_shadow_mmio_mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; +kvm_mmu_set_mmio_access_mask (PT_WRITABLE_MASK |PT_USER_MASK ); }static __init void svm_set_cpu_caps (void )
arch/x86/kvm/vmx/tdx.c +4 -0
@@ -7,6 +7,7 @@#include "x86_ops.h" #include "tdx.h" #include "x86.h" +#include "mmu.h" #undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
@@ -276,6 +277,9 @@ int tdx_vm_init(struct kvm *kvm)int ret, i;u64 err; +kvm_mmu_set_mmio_spte_mask (kvm, vmx_shadow_mmio_mask, + vmx_shadow_mmio_mask); +/* vCPUs can't be created until after KVM_TDX_INIT_VM. */ kvm->max_vcpus =0 ;
arch/x86/kvm/vmx/vmx.c +26 -0
@@ -141,6 +141,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);extern bool __read_mostly allow_smaller_maxphyaddr;module_param (allow_smaller_maxphyaddr, bool,S_IRUGO ); +u64 __ro_after_init vmx_shadow_mmio_mask; +#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE#define KVM_VM_CR0_ALWAYS_ON \
@@ -7359,6 +7361,17 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_RWX_MASK ); +else +kvm_mmu_set_mmio_spte_mask (kvm, vmx_shadow_mmio_mask, + vmx_shadow_mmio_mask); +if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
@@ -8358,6 +8371,19 @@ int __init vmx_init(void)if (!enable_ept) allow_smaller_maxphyaddr = true; +/* +* Set a reserved PA bit in MMIO SPTEs to generate page faults with +* PFEC.RSVD=1 on MMIO accesses. 64-bit PTEs (PAE, x86-64, and EPT +* paging) support a maximum of 52 bits of PA, i.e. if the CPU supports +* 52-bit physical addresses then there are no reserved PA bits in the +* PTEs and so the reserved PA approach must be disabled. +*/ +if (kvm_get_shadow_phys_bits () <52 ) + vmx_shadow_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; +else + vmx_shadow_mmio_mask =0 ; +kvm_mmu_set_mmio_access_mask (0 ); +return 0 ; }
arch/x86/kvm/vmx/x86_ops.h +1 -0
@@ -13,6 +13,7 @@ void hv_vp_assist_page_exit(void);void __init vmx_init_early (void );int __init vmx_init (void );void vmx_exit (void ); +extern u64 __ro_after_init vmx_shadow_mmio_mask;__init int vmx_cpu_has_kvm_support (void );__init int vmx_disabled_by_bios (void );
Signature · 5 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 14 lines · Isaku Yamahata
Here is the updated one. The changes are
- removed hunks that should be a part of other patches.
- removedshadow_default_mmio_mask
- trimed down commit messages.From
ed6b4a076e515550878b069596cf156a1bc33514Mon Sep 17 00:00:00 2001
Message-Id: <ed6b4a076e515550878b069596cf156a1bc33514.1658220363.git.isaku.yamahata@intel.com>
In-Reply-To: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658220363.git.isaku.yamahata@intel.com>
References: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658220363.git.isaku.yamahata@intel.com>
From: Sean Christopherson <[email protected]>
Date: Wed, 10 Jun 2020 15:46:38 -0700
Subject: [PATCH 036/306] KVM: x86/mmu: Track shadow MMIO value/mask on a
per-VM basis
Quoted 5 lines
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to
kvm_archand track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working. To untangle the logic to initializeshadow_mmio_access_mask, introduce a setter function.
introduce a separate setter function for it.
Quoted 123 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/mmu/mmu.c | 8 +++--- arch/x86/kvm/mmu/spte.c | 45 +++++++++------------------------ arch/x86/kvm/mmu/spte.h | 10 +++----- arch/x86/kvm/mmu/tdp_mmu.c | 6 ++--- arch/x86/kvm/svm/svm.c | 11 +++++--- arch/x86/kvm/vmx/tdx.c | 4 +++ arch/x86/kvm/vmx/vmx.c | 26 +++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 1 + 10 files changed, 66 insertions(+), 52 deletions(-)arch/x86/include/asm/kvm_host.h +4
-0@@ -1161,6 +1161,10 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +bool enable_mmio_caching; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;arch/x86/kvm/mmu.h +2
-1@@ -108,7 +108,8 @@ static inline u8 kvm_get_shadow_phys_bits(void)return boot_cpu_data.x86_phys_bits; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask); +void kvm_mmu_set_mmio_access_mask (u64 mmio_access_mask);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only);arch/x86/kvm/mmu/mmu.c +4
-4@@ -2298,7 +2298,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;@@ -3079,7 +3079,7 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* and only if L1's MAXPHYADDR is inaccurate with respect to * the hardware's). */ -if (unlikely (!enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }@@ -3918,7 +3918,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);@@ -4361,7 +4361,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;arch/x86/kvm/mmu/spte.c +3
-6@@ -29,8 +29,6 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_value;@@ -62,10 +60,10 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;@@ -337,9 +335,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask) { -BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);if (!enable_mmio_caching)
arch/x86/kvm/mmu/spte.c void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) · line 366
Quoted 10 lines
arch/x86/kvm/mmu/spte.c +1
-6@@ -366,12 +363,9 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (__REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; -if (!mmio_value) - enable_mmio_caching = false; - - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; - shadow_mmio_access_mask = access_mask; + kvm->arch.enable_mmio_caching = !!mmio_value;
KVM has a global enable_mmio_caching boolean, and I think we should honor it here (in this patch) by doing below first:
if (enabling_mmio_caching) mmio_value = 0;
For TD guest, the logic around enable_mmio_caching doesn't make sense anymore, so perhaps we can later tweak it by doing something like:
/*
* Treat mmio_caching is false for TD guest, or true for it, depending
* on how you define it.
*/
if (kvm_gfn_shared_mask(kvm))
kvm->arch.enable_mmio_caching = false; /* or true? */
else {
if (!enable_mmio_caching)
mmio_value = 0;
kvm->arch.enable_mmio_caching = !!mmio_value;
}Quoted 56 lines
arch/x86/kvm/mmu/spte.c +8
-21+ kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask);@@ -399,20 +393,12 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only) shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();@@ -452,18 +438,11 @@ void kvm_mmu_reset_all_pte_masks(void) shadow_host_writable_mask =DEFAULT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =DEFAULT_SPTE_MMU_WRITABLE ; +} -/* -* Set a reserved PA bit in MMIO SPTEs to generate page faults with -* PFEC.RSVD=1 on MMIO accesses. 64-bit PTEs (PAE, x86-64, and EPT -* paging) support a maximum of 52 bits of PA, i.e. if the CPU supports -* 52-bit physical addresses then there are no reserved PA bits in the -* PTEs and so the reserved PA approach must be disabled. -*/ -if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; -else - mask =0 ; - -kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_mmio_access_mask (u64 mmio_access_mask) +{ +BUG_ON ((u64 )(unsigned )mmio_access_mask != mmio_access_mask); + shadow_mmio_access_mask = mmio_access_mask; } +EXPORT_SYMBOL (kvm_mmu_set_mmio_access_mask);arch/x86/kvm/mmu/spte.h +0
-0
arch/x86/kvm/mmu/spte.h line 9
Quoted 5 lines
arch/x86/kvm/mmu/spte.h +0
-2@@ -5,8 +5,6 @@#include "mmu_internal.h" -extern bool __read_mostly enable_mmio_caching; -
Here you removed the ability to control enable_mmio_caching globally. It's not something you stated to do in the changelog. Perhaps we should still keep it, and enforce it in kvm_mmu_set_mmio_spte_mask() as commented above.
And in upstream KVM, it is a module parameter. What happens to it?
Quoted 106 lines
arch/x86/kvm/mmu/spte.h +3
-5/* * A MMU present SPTE is backed by actual memory and may or may not be present * in hardware. E.g. MMIO SPTEs are not considered present. Use bit 11, as it @@ -160,8 +158,6 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_value;@@ -228,10 +224,10 @@ static inline bool is_removed_spte(u64 spte)*/ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (enable_mmio_caching); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.enable_mmio_caching); }static inline bool is_shadow_present_pte (u64 pte)arch/x86/kvm/mmu/tdp_mmu.c +3
-3@@ -569,8 +569,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"@@ -1108,7 +1108,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) { vcpu->stat.pf_mmio_spte_created++;trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte);arch/x86/kvm/svm/svm.c +7
-4@@ -198,6 +198,7 @@ module_param(dump_invalid_vmcb, bool, 0644);bool intercept_smi = true;module_param (intercept_smi, bool,0444 ); +static u64 __read_mostly svm_shadow_mmio_mask;static bool svm_gp_erratum_intercept = true;@@ -4685,6 +4686,9 @@ static bool svm_is_vm_type_supported(unsigned long type)static int svm_vm_init (struct kvm *kvm) { +kvm_mmu_set_mmio_spte_mask (kvm, svm_shadow_mmio_mask, + svm_shadow_mmio_mask); +if (!pause_filter_count || !pause_filter_thresh) kvm->arch.pause_in_guest = true;@@ -4834,7 +4838,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {static __init void svm_adjust_mmio_mask (void ) {unsigned int enc_bit, mask_bit; -u64 msr, mask; +u64 msr;/* If there is no memory encryption support, use existing mask */ if (cpuid_eax (0x80000000 ) <0x8000001f )@@ -4861,9 +4865,8 @@ static __init void svm_adjust_mmio_mask(void)* * If the mask bit location is 52 (or above), then clear the mask. */ - mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; - -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); + svm_shadow_mmio_mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; +kvm_mmu_set_mmio_access_mask (PT_WRITABLE_MASK |PT_USER_MASK ); }static __init void svm_set_cpu_caps (void )arch/x86/kvm/vmx/tdx.c +1
-0@@ -7,6 +7,7 @@#include "x86_ops.h" #include "tdx.h" #include "x86.h" +#include "mmu.h" #undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
arch/x86/kvm/vmx/tdx.c int tdx_vm_init(struct kvm *kvm) · line 282
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -276,6 +277,9 @@ int tdx_vm_init(struct kvm *kvm)int ret, i;u64 err; +kvm_mmu_set_mmio_spte_mask (kvm, vmx_shadow_mmio_mask, + vmx_shadow_mmio_mask); +
I prefer to split this chunk out to another patch so this patch can be purely
infrastructural. In this way you can even move this patch around easily in
this series.
Quoted 70 lines
arch/x86/kvm/vmx/tdx.c +0
-0/* vCPUs can't be created until after KVM_TDX_INIT_VM. */ kvm->max_vcpus =0 ;arch/x86/kvm/vmx/vmx.c +26
-0@@ -141,6 +141,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);extern bool __read_mostly allow_smaller_maxphyaddr;module_param (allow_smaller_maxphyaddr, bool,S_IRUGO ); +u64 __ro_after_init vmx_shadow_mmio_mask; +#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE#define KVM_VM_CR0_ALWAYS_ON \@@ -7359,6 +7361,17 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_RWX_MASK ); +else +kvm_mmu_set_mmio_spte_mask (kvm, vmx_shadow_mmio_mask, + vmx_shadow_mmio_mask); +if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :@@ -8358,6 +8371,19 @@ int __init vmx_init(void)if (!enable_ept) allow_smaller_maxphyaddr = true; +/* +* Set a reserved PA bit in MMIO SPTEs to generate page faults with +* PFEC.RSVD=1 on MMIO accesses. 64-bit PTEs (PAE, x86-64, and EPT +* paging) support a maximum of 52 bits of PA, i.e. if the CPU supports +* 52-bit physical addresses then there are no reserved PA bits in the +* PTEs and so the reserved PA approach must be disabled. +*/ +if (kvm_get_shadow_phys_bits () <52 ) + vmx_shadow_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; +else + vmx_shadow_mmio_mask =0 ; +kvm_mmu_set_mmio_access_mask (0 ); +return 0 ; }arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -13,6 +13,7 @@ void hv_vp_assist_page_exit(void);void __init vmx_init_early (void );int __init vmx_init (void );void vmx_exit (void ); +extern u64 __ro_after_init vmx_shadow_mmio_mask;__init int vmx_cpu_has_kvm_support (void );__init int vmx_disabled_by_bios (void );Signature · 1 line
2.25.1
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 26 lines · Kai Huang
Quoted 12 lines
arch/x86/kvm/mmu/spte.c +1
-2@@ -337,9 +335,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask) { -BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);if (!enable_mmio_caching)arch/x86/kvm/mmu/spte.c void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) · line 366
Quoted 10 lines
arch/x86/kvm/mmu/spte.c +1
-6@@ -366,12 +363,9 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (__REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; -if (!mmio_value) - enable_mmio_caching = false; - - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; - shadow_mmio_access_mask = access_mask; + kvm->arch.enable_mmio_caching = !!mmio_value;KVM has a global
enable_mmio_cachingboolean, and I think we should honor it here (in this patch) by doing below first:
Quoted 2 lines
if (enabling_mmio_caching) mmio_value = 0;
This function already includes "if (!enable_mmio_caching) mmio_value = 0;" in the beginning. (But not in this hunk, though). So this patch honors the kernel module parameter.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/mmu/spte.h +0
-0@@ -5,8 +5,6 @@#include "mmu_internal.h"
Quoted 8 lines
arch/x86/kvm/mmu/spte.h line 9
Quoted 2 lines
arch/x86/kvm/mmu/spte.h +0
-2-extern bool __read_mostly enable_mmio_caching; -Here you removed the ability to control
enable_mmio_cachingglobally. It's not something you stated to do in the changelog. Perhaps we should still keep it, and enforce it inkvm_mmu_set_mmio_spte_mask()as commented above.And in upstream KVM, it is a module parameter. What happens to it?
Ditto. the upstredam kvm_mmu_set_mmio_spte_mask() has "if (!enable_mmio_caching) mmio_value = 0;" and this patch keeps it.
Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/vmx/tdx.c +1
-0@@ -7,6 +7,7 @@#include "x86_ops.h" #include "tdx.h" #include "x86.h" +#include "mmu.h" #undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt@@ -276,6 +277,9 @@ int tdx_vm_init(struct kvm *kvm)int ret, i;u64 err;
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c int tdx_vm_init(struct kvm *kvm) · line 282
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+kvm_mmu_set_mmio_spte_mask (kvm, vmx_shadow_mmio_mask, + vmx_shadow_mmio_mask); +I prefer to split this chunk out to another patch so this patch can be purely
infrastructural. In this way you can even move this patch around easily in
this series.
Ok. I'll move it to a patch that touches TDX.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 037/102] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 10 lines · Isaku Yamahata
Quoted 5 lines
KVM has a global
enable_mmio_cachingboolean, and I think we should honor it here (in this patch) by doing below first:if (
enabling_mmio_caching)
mmio_value= 0;This function already includes "if (!enable_mmio_caching)
mmio_value= 0;" in the beginning. (But not in this hunk, though). So this patch honors the kernel
module parameter.
Yeah I missed that. Thanks.
Signature · 2 lines
Thanks, -Kai
PATCH v7 38/102 KVM: x86/mmu: Disallow fast page fault on private GPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires TDX SEAMCALL to operate Secure EPT instead of direct memory access and TDX SEAMCALL is heavy operation. Fast page fault on private GPA doesn't make sense. Disallow fast page fault on private GPA.
--- arch/x86/kvm/mmu/mmu.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +10 -2
@@ -3081,8 +3081,16 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faureturn RET_PF_CONTINUE ; } -static bool page_fault_can_be_fast (struct kvm_page_fault *fault) +static bool page_fault_can_be_fast (struct kvm *kvm,struct kvm_page_fault *fault) { +/* +* TDX private mapping doesn't support fast page fault because the EPT +* entry is read/written with TDX SEAMCALLs instead of direct memory +* access. +*/ +if (kvm_is_private_gpa (kvm, fault->addr)) +return false; +/* * Page faults with reserved bits set, i.e. faults on MMIO SPTEs, only * reach the common page fault handler if the SPTE has an invalid MMIO
@@ -3192,7 +3200,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)u64 *sptep =NULL ;uint retry_count =0 ; -if (!page_fault_can_be_fast (fault)) +if (!page_fault_can_be_fast (vcpu->kvm, fault))return ret;walk_shadow_page_lockless_begin (vcpu);
Signature · 1 line
2.25.1
PATCH v7 39/102 KVM: x86/mmu: Allow per-VM override of the TDP max page level
In reply to [email protected]From: Sean Christopherson <[email protected]>
TODO: This is a transient workaround patch until the large page support for TDX is implemented. Support large page for TDX and remove this patch.
At this point, large page for TDX isn't supported, and need to allow guest TD to work only with 4K pages. On the other hand, conventional VMX VMs should continue to work with large page. Allow per-VM override of the TDP max page level.
In the existing x86 KVM MMU code, there is already max_level member in struct kvm_page_fault with KVM_MAX_HUGEPAGE_LEVEL initial value. The KVM page fault handler denies page size larger than max_level.
Add per-VM member to indicate the allowed maximum page size with KVM_MAX_HUGEPAGE_LEVEL as default value and initialize max_level in struct kvm_page_fault with it. For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/mmu/mmu_internal.h | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1146,6 +1146,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -5878,6 +5878,7 @@ int kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ;kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, shadow_default_mmio_mask,ACC_WRITE_MASK |ACC_USER_MASK );
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -244,7 +244,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };
Signature · 1 line
2.25.1
Re: [PATCH v7 039/102] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 4 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TODO: This is a transient workaround patch until the large page support for TDX is implemented. Support large page for TDX and remove this patch.
I don't understand. How does this patch have anything to do with what you are talking about here?
If you want to remove this patch later, then why not just explain the reason to remove when you actually have that patch?
Quoted 5 lines
At this point, large page for TDX isn't supported, and need to allow guest TD to work only with 4K pages. On the other hand, conventional VMX VMs should continue to work with large page. Allow per-VM override of the TDP max page level.
At which point/previous patch have you made/declared "large page for TDX isn't supported"?
If you want to declare you don't want to support large page for TDX, IMHO just declare it here, for instance:
"For simplicity, only support 4K page for TD guest."
Quoted 10 lines
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it. For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
To me it's overcomplicated. You just need simple sentences for such simple infrastructural patch. For instance:
"TDX requires special handling to support large private page. For simplicity, only support 4K page for TD guest for now. Add per-VM maximum page level support to support different maximum page sizes for TD guest and conventional VMX guest."
Just for your reference.
Quoted 46 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/mmu/mmu_internal.h | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1146,6 +1146,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;arch/x86/kvm/mmu/mmu.c +1
-0@@ -5878,6 +5878,7 @@ int kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ;kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, shadow_default_mmio_mask,ACC_WRITE_MASK |ACC_USER_MASK );arch/x86/kvm/mmu/mmu_internal.h +1
-1@@ -244,7 +244,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };
Re: [PATCH v7 039/102] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 40 lines · Kai Huang
Quoted 4 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TODO: This is a transient workaround patch until the large page support for TDX is implemented. Support large page for TDX and remove this patch.
I don't understand. How does this patch have anything to do with what you are talking about here?
If you want to remove this patch later, then why not just explain the reason to remove when you actually have that patch?
Quoted 5 lines
At this point, large page for TDX isn't supported, and need to allow guest TD to work only with 4K pages. On the other hand, conventional VMX VMs should continue to work with large page. Allow per-VM override of the TDP max page level.
At which point/previous patch have you made/declared "large page for TDX isn't supported"?
If you want to declare you don't want to support large page for TDX, IMHO just declare it here, for instance:
"For simplicity, only support 4K page for TD guest."
Quoted 10 lines
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it. For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.To me it's overcomplicated. You just need simple sentences for such simple infrastructural patch. For instance:
Quoted 6 lines
"TDX requires special handling to support large private page. For simplicity, only support 4K page for TD guest for now. Add per-VM maximum page level support to support different maximum page sizes for TD guest and conventional VMX guest."
Just for your reference.
Thanks for the sentences. I'll replace the commit message with yours.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 40/102 KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu
In reply to [email protected]From: Sean Christopherson <[email protected]>
For kvm mmu that has shared bit mask, zap only leaf SPTEs when deleting/moving a memslot. The existing kvm_mmu_zap_memslot() depends on role.invalid with read lock of mmu_lock so that other vcpu can operate on kvm mmu concurrently. Mark the root page table invalid, unlink it from page table pointer of CPU, process the page table. It doesn't work for private page table to unlink the root page table because it requires all SPTE entry to be non-present. Instead, with write-lock of mmu_lock and zap only leaf SPTEs for kvm mmu with shared bit mask.
--- arch/x86/kvm/mmu/mmu.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +34 -1
@@ -5854,11 +5854,44 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)return unlikely (!list_empty_careful (&kvm->arch.zapped_obsolete_pages)); } +static void kvm_mmu_zap_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) +{ +bool flush = false; + +write_lock (&kvm->mmu_lock); + +/* +* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst +* case scenario we'll have unused shadow pages lying around until they +* are recycled due to age or when the VM is destroyed. +*/ +if (is_tdp_mmu_enabled (kvm)) { +struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = false, + }; + + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush); + }else { + flush =slot_handle_level (kvm, slot, kvm_zap_rmapp,PG_LEVEL_4K , +KVM_MAX_HUGEPAGE_LEVEL , true); + } +if (flush) +kvm_flush_remote_tlbs (kvm); + +write_unlock (&kvm->mmu_lock); +} +static void kvm_mmu_invalidate_zap_pages_in_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvm_page_track_notifier_node *node) { -kvm_mmu_zap_all_fast (kvm); +if (kvm_gfn_shared_mask (kvm)) +kvm_mmu_zap_memslot (kvm, slot); +else +kvm_mmu_zap_all_fast (kvm); }int kvm_mmu_init_vm (struct kvm *kvm)
Signature · 1 line
2.25.1
Re: [PATCH v7 040/102] KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu
Quoted 4 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For kvm mmu that has shared bit mask, zap only leaf SPTEs when deleting/moving a memslot. The existing
kvm_mmu_zap_memslot()depends on
Unless I am mistaken, I don't see there's an 'existing' kvm_mmu_zap_memslot().
Quoted 7 lines
role.invalid with read lock of
mmu_lockso that other vcpu can operate on kvm mmu concurrently.Mark the root page table invalid, unlink it from page table pointer of CPU, process the page table.
Are you talking about the behaviour of existing code, or the change you are going to make? Looks like you mean the latter but I believe it's the former.
Quoted 4 lines
It doesn't work for private
page table to unlink the root page table because it requires all SPTE entry to be non-present.
I don't think we can truly *unlink* the private root page table from secure EPTP, right? The EPTP (root table) is fixed (and hidden) during TD's runtime.
I guess you are trying to say: removing/unlinking one secure-EPT page requires removing/unlinking all its children first?
So the reason to only zap leaf is we cannot truly unlink the private root page table, correct? Sorry your changelog is not obvious to me.
Quoted 27 lines
Instead, with write-lock of
mmu_lockand zap only leaf SPTEs for kvm mmu with shared bit mask.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)arch/x86/kvm/mmu/mmu.c +10
-0@@ -5854,11 +5854,44 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)return unlikely (!list_empty_careful (&kvm->arch.zapped_obsolete_pages)); } +static void kvm_mmu_zap_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) +{ +bool flush = false; + +write_lock (&kvm->mmu_lock); + +/* +* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst +* case scenario we'll have unused shadow pages lying around until they +* are recycled due to age or when the VM is destroyed.
arch/x86/kvm/mmu/mmu.c static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) · line 5876
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +10
-0+*/ +if (is_tdp_mmu_enabled (kvm)) { +struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = false, + }; + + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush);
It appears you only unmap private GFNs (because the base_gfn doesn't have shared bit)? I think shared mapping in this slot must be zapped too?
How is this done? Or the kvm_tdp_mmu_unmap_gfn_range() also zaps shared mappings?
It's hard to review if one patch's behaviour/logic depends on further patches.
Quoted 22 lines
arch/x86/kvm/mmu/mmu.c +14
-1+ }else { + flush =slot_handle_level (kvm, slot, kvm_zap_rmapp,PG_LEVEL_4K , +KVM_MAX_HUGEPAGE_LEVEL , true); + } +if (flush) +kvm_flush_remote_tlbs (kvm); + +write_unlock (&kvm->mmu_lock); +} +static void kvm_mmu_invalidate_zap_pages_in_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvm_page_track_notifier_node *node) { -kvm_mmu_zap_all_fast (kvm); +if (kvm_gfn_shared_mask (kvm)) +kvm_mmu_zap_memslot (kvm, slot); +else +kvm_mmu_zap_all_fast (kvm); }int kvm_mmu_init_vm (struct kvm *kvm)
Re: [PATCH v7 040/102] KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu
Quoted 7 lines · Kai Huang
Quoted 4 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For kvm mmu that has shared bit mask, zap only leaf SPTEs when deleting/moving a memslot. The existing
kvm_mmu_zap_memslot()depends onUnless I am mistaken, I don't see there's an 'existing'
kvm_mmu_zap_memslot().
Oops. it should be kvm_tdp_mmu_invalidate_all_roots().
Quoted 10 lines
Quoted 7 lines
role.invalid with read lock of
mmu_lockso that other vcpu can operate on kvm mmu concurrently.Mark the root page table invalid, unlink it from page table pointer of CPU, process the page table.
Are you talking about the behaviour of existing code, or the change you are going to make? Looks like you mean the latter but I believe it's the former.
The existing code. The should "It marks ...".
Quoted 10 lines
Quoted 4 lines
It doesn't work for private
page table to unlink the root page table because it requires all SPTE entry to be non-present.I don't think we can truly *unlink* the private root page table from secure EPTP, right? The EPTP (root table) is fixed (and hidden) during TD's runtime.
I guess you are trying to say: removing/unlinking one secure-EPT page requires removing/unlinking all its children first?
That's right. I'll update it as follows.
Quoted 2 lines
So the reason to only zap leaf is we cannot truly unlink the private root page table, correct? Sorry your changelog is not obvious to me.
The reason is, to unlink a page table from the parent's SPTE, all SPTEs of the page table need to be non-present.
Here is the updated commit message.
KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu|
For kvm mmu that has shared bit mask, zap only leaf SPTEs when |
deleting/moving a memslot. The existing kvm_tdp_mmu_invalidate_all_roots()|
depends on role.invalid with read lock of mmu_lock so that other vcpu can |
operate on kvm mmu concurrently. It marks the root page table invalid, |
zaps SPTEs of the root page tables. |
|
It doesn't work to unlink a private page table from the parent's SPTE entry|
because it requires all SPTE entry of the page table to be non-present. |
Instead, with write-lock of mmu_lock and zap only leaf SPTEs for kvm mmu |with shared bit mask.
Quoted 42 lines
Quoted 27 lines
Instead, with write-lock of
mmu_lockand zap only leaf SPTEs for kvm mmu with shared bit mask.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)arch/x86/kvm/mmu/mmu.c +10
-0@@ -5854,11 +5854,44 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)return unlikely (!list_empty_careful (&kvm->arch.zapped_obsolete_pages)); } +static void kvm_mmu_zap_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) +{ +bool flush = false; + +write_lock (&kvm->mmu_lock); + +/* +* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst +* case scenario we'll have unused shadow pages lying around until they +* are recycled due to age or when the VM is destroyed. arch/x86/kvm/mmu/mmu.c static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) · line 5876
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +10
-0+*/ +if (is_tdp_mmu_enabled (kvm)) { +struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = false, + }; + + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush);It appears you only unmap private GFNs (because the
base_gfndoesn't have shared bit)? I think shared mapping in this slot must be zapped too?
Quoted 2 lines
How is this done? Or the
kvm_tdp_mmu_unmap_gfn_range()also zaps shared mappings?
kvm_tdp_mmu_unmap_gfn_range() handles both private gfn and shared gfn as they are alias.
Quoted 1 line
It's hard to review if one patch's behaviour/logic depends on further patches.
I'll add a comment on the call.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 040/102] KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu
Quoted 51 lines · Isaku Yamahata
Quoted 7 lines · Kai Huang
Quoted 4 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For kvm mmu that has shared bit mask, zap only leaf SPTEs when deleting/moving a memslot. The existing
kvm_mmu_zap_memslot()depends onUnless I am mistaken, I don't see there's an 'existing'
kvm_mmu_zap_memslot().Oops. it should be
kvm_tdp_mmu_invalidate_all_roots().Quoted 10 lines
Quoted 7 lines
role.invalid with read lock of
mmu_lockso that other vcpu can operate on kvm mmu concurrently.Mark the root page table invalid, unlink it from page table pointer of CPU, process the page table.
Are you talking about the behaviour of existing code, or the change you are going to make? Looks like you mean the latter but I believe it's the former.
The existing code. The should "It marks ...".
Quoted 10 lines
Quoted 4 lines
It doesn't work for private
page table to unlink the root page table because it requires all SPTE entry to be non-present.I don't think we can truly *unlink* the private root page table from secure EPTP, right? The EPTP (root table) is fixed (and hidden) during TD's runtime.
I guess you are trying to say: removing/unlinking one secure-EPT page requires removing/unlinking all its children first?
That's right. I'll update it as follows.
Quoted 2 lines
So the reason to only zap leaf is we cannot truly unlink the private root page table, correct? Sorry your changelog is not obvious to me.
The reason is, to unlink a page table from the parent's SPTE, all SPTEs of the page table need to be non-present.
Here is the updated commit message.
Quoted 9 lines
KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu|
For kvm mmu that has shared bit mask, zap only leaf SPTEs when |
deleting/moving a memslot. The existing kvm_tdp_mmu_invalidate_all_roots()|
depends on role.invalid with read lock of mmu_lock so that other vcpu can |
operate on kvm mmu concurrently. It marks the root page table invalid, |
zaps SPTEs of the root page tables. |
|
It doesn't work to unlink a private page table from the parent's SPTE entry|
because it requires all SPTE entry of the page table to be non-present. |AFAICT this isn't the real reason that you cannot mark private root table as invalid, and do the same zapping as you mentioned above. There might be some change to support "zapping all children before zapping the parent for private table" (currently the actual page table is freed after RCU grace period, but not at unlink time), but I don't see how this cannot be supported, or at least the changelog doesn't explain why it cannot be supported.
The true reason is, if I understand correctly, you cannot truly unlink the private root page table from the hardware and then, i.e. allocate a new one for it. So just zap the leafs.
Quoted 48 lines
Instead, with write-lock of
mmu_lockand zap only leaf SPTEs for kvm mmu |
with shared bit mask.Quoted 42 lines
Quoted 27 lines
Instead, with write-lock of
mmu_lockand zap only leaf SPTEs for kvm mmu with shared bit mask.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)arch/x86/kvm/mmu/mmu.c +10
-0@@ -5854,11 +5854,44 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)return unlikely (!list_empty_careful (&kvm->arch.zapped_obsolete_pages)); } +static void kvm_mmu_zap_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) +{ +bool flush = false; + +write_lock (&kvm->mmu_lock); + +/* +* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst +* case scenario we'll have unused shadow pages lying around until they +* are recycled due to age or when the VM is destroyed. arch/x86/kvm/mmu/mmu.c static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) · line 5876
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +10
-0+*/ +if (is_tdp_mmu_enabled (kvm)) { +struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = false, + }; + + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush);It appears you only unmap private GFNs (because the
base_gfndoesn't have shared bit)? I think shared mapping in this slot must be zapped too?Quoted 2 lines
How is this done? Or the
kvm_tdp_mmu_unmap_gfn_range()also zaps shared mappings?
Quoted 8 lines
kvm_tdp_mmu_unmap_gfn_range()handles both private gfn and shared gfn as they are alias.Quoted 1 line
It's hard to review if one patch's behaviour/logic depends on further patches.
I'll add a comment on the call.
I don't think adding a comment is enough. The correctness of one patch needs to depend on future patch doesn't seem right. Please also consider patch reorganize/reorder.
PATCH v7 41/102 KVM: VMX: Introduce test mode related to EPT violation VE
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option ept_violation_ve_test and when it's set, set error.
--- arch/x86/include/asm/vmx.h | 12 +++++++ arch/x86/kvm/vmx/vmx.c | 68 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 3 ++ 3 files changed, 82 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +12 -0
@@ -68,6 +68,7 @@#define SECONDARY_EXEC_ENCLS_EXITING VMCS_CONTROL_BIT(ENCLS_EXITING)#define SECONDARY_EXEC_RDSEED_EXITING VMCS_CONTROL_BIT(RDSEED_EXITING)#define SECONDARY_EXEC_ENABLE_PML VMCS_CONTROL_BIT(PAGE_MOD_LOGGING) +#define SECONDARY_EXEC_EPT_VIOLATION_VE VMCS_CONTROL_BIT(EPT_VIOLATION_VE)#define SECONDARY_EXEC_PT_CONCEAL_VMX VMCS_CONTROL_BIT(PT_CONCEAL_VMX)#define SECONDARY_EXEC_XSAVES VMCS_CONTROL_BIT(XSAVES)#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC VMCS_CONTROL_BIT(MODE_BASED_EPT_EXEC)
@@ -223,6 +224,8 @@ enum vmcs_field {VMREAD_BITMAP_HIGH =0x00002027 ,VMWRITE_BITMAP =0x00002028 ,VMWRITE_BITMAP_HIGH =0x00002029 , +VE_INFORMATION_ADDRESS =0x0000202A , +VE_INFORMATION_ADDRESS_HIGH =0x0000202B ,XSS_EXIT_BITMAP =0x0000202C ,XSS_EXIT_BITMAP_HIGH =0x0000202D , ENCLS_EXITING_BITMAP = 0x0000202E,
@@ -628,4 +631,13 @@ enum vmx_l1d_flush_state {extern enum vmx_l1d_flush_state l1tf_vmx_mitigation; +struct vmx_ve_information { +u32 exit_reason; +u32 delivery; +u64 exit_qualification; +u64 guest_linear_address; +u64 guest_physical_address; +u16 eptp_index; +}; +#endif
arch/x86/kvm/vmx/vmx.c +67 -1
124 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +3 -0
@@ -338,6 +338,9 @@ struct vcpu_vmx {DECLARE_BITMAP (read,MAX_POSSIBLE_PASSTHROUGH_MSRS );DECLARE_BITMAP (write,MAX_POSSIBLE_PASSTHROUGH_MSRS ); } shadow_msr_intercept; + +/* ve_info must be page aligned. */ +struct vmx_ve_information *ve_info; };struct kvm_vmx {
Signature · 1 line
2.25.1
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.
I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Quoted 194 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/vmx.h | 12 +++++++ arch/x86/kvm/vmx/vmx.c | 68 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 3 ++ 3 files changed, 82 insertions(+), 1 deletion(-)arch/x86/include/asm/vmx.h +12
-0@@ -68,6 +68,7 @@#define SECONDARY_EXEC_ENCLS_EXITING VMCS_CONTROL_BIT(ENCLS_EXITING)#define SECONDARY_EXEC_RDSEED_EXITING VMCS_CONTROL_BIT(RDSEED_EXITING)#define SECONDARY_EXEC_ENABLE_PML VMCS_CONTROL_BIT(PAGE_MOD_LOGGING) +#define SECONDARY_EXEC_EPT_VIOLATION_VE VMCS_CONTROL_BIT(EPT_VIOLATION_VE)#define SECONDARY_EXEC_PT_CONCEAL_VMX VMCS_CONTROL_BIT(PT_CONCEAL_VMX)#define SECONDARY_EXEC_XSAVES VMCS_CONTROL_BIT(XSAVES)#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC VMCS_CONTROL_BIT(MODE_BASED_EPT_EXEC)@@ -223,6 +224,8 @@ enum vmcs_field {VMREAD_BITMAP_HIGH =0x00002027 ,VMWRITE_BITMAP =0x00002028 ,VMWRITE_BITMAP_HIGH =0x00002029 , +VE_INFORMATION_ADDRESS =0x0000202A , +VE_INFORMATION_ADDRESS_HIGH =0x0000202B ,XSS_EXIT_BITMAP =0x0000202C ,XSS_EXIT_BITMAP_HIGH =0x0000202D , ENCLS_EXITING_BITMAP = 0x0000202E,@@ -628,4 +631,13 @@ enum vmx_l1d_flush_state {extern enum vmx_l1d_flush_state l1tf_vmx_mitigation; +struct vmx_ve_information { +u32 exit_reason; +u32 delivery; +u64 exit_qualification; +u64 guest_linear_address; +u64 guest_physical_address; +u16 eptp_index; +}; +#endif arch/x86/kvm/vmx/vmx.c +67
-1@@ -126,6 +126,9 @@ module_param(error_on_inconsistent_vmcs_config, bool, 0444);static bool __read_mostly dump_invalid_vmcs =0 ;module_param (dump_invalid_vmcs, bool,0644 ); +static bool __read_mostly ept_violation_ve_test =0 ; +module_param (ept_violation_ve_test, bool,0444 ); +#define MSR_BITMAP_MODE_X2APIC 1#define MSR_BITMAP_MODE_X2APIC_APICV 2@@ -726,6 +729,13 @@ void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu) eb = (1u <<PF_VECTOR ) | (1u <<UD_VECTOR ) | (1u <<MC_VECTOR ) | (1u <<DB_VECTOR ) | (1u <<AC_VECTOR ); +/* +* #VE isn't used for VMX, but for TDX. To test against unexpected +* change related to #VE for VMX, intercept unexpected #VE and warn on +* it. +*/ +if (ept_violation_ve_test) + eb |=1u <<VE_VECTOR ;/* * Guest access to VMware backdoor ports could legitimately * trigger #GP because of TSS I/O permission bitmap. @@ -2524,6 +2534,8 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,SECONDARY_EXEC_NOTIFY_VM_EXITING ;if (cpu_has_sgx ()) opt2 |=SECONDARY_EXEC_ENCLS_EXITING ; +if (ept_violation_ve_test) + opt2 |=SECONDARY_EXEC_EPT_VIOLATION_VE ;if (adjust_vmx_controls (min2, opt2,MSR_IA32_VMX_PROCBASED_CTLS2 , &_cpu_based_2nd_exec_control) <0 )@@ -2558,6 +2570,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,return -EIO ; vmx_cap->ept =0 ; + _cpu_based_2nd_exec_control &= ~SECONDARY_EXEC_EPT_VIOLATION_VE ; }if (!(_cpu_based_2nd_exec_control &SECONDARY_EXEC_ENABLE_VPID ) && vmx_cap->vpid) {@@ -4390,6 +4403,7 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) exec_control &= ~SECONDARY_EXEC_ENABLE_VPID ;if (!enable_ept) { exec_control &= ~SECONDARY_EXEC_ENABLE_EPT ; + exec_control &= ~SECONDARY_EXEC_EPT_VIOLATION_VE ; enable_unrestricted_guest =0 ; }if (!enable_unrestricted_guest)@@ -4517,8 +4531,40 @@ static void init_vmcs(struct vcpu_vmx *vmx)exec_controls_set (vmx,vmx_exec_control (vmx)); -if (cpu_has_secondary_exec_ctrls ()) +if (cpu_has_secondary_exec_ctrls ()) {secondary_exec_controls_set (vmx,vmx_secondary_exec_control (vmx)); +if (secondary_exec_controls_get (vmx) & +SECONDARY_EXEC_EPT_VIOLATION_VE ) { +if (!vmx->ve_info) { +/* ve_info must be page aligned. */ +struct page *page; + +BUILD_BUG_ON (sizeof (*vmx->ve_info) >PAGE_SIZE ); + page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (page) + vmx->ve_info =page_to_virt (page); + } +if (vmx->ve_info) { +/* +* Allow #VE delivery. CPU sets this field to +* 0xFFFFFFFF on #VE delivery. Another #VE can +* occur only if software clears the field. +*/ + vmx->ve_info->delivery =0 ; +vmcs_write64 (VE_INFORMATION_ADDRESS , +__pa (vmx->ve_info)); + }else { +/* +* Because SECONDARY_EXEC_EPT_VIOLATION_VE is +* used only when ept_violation_ve_test is true, +* it's okay to go with the bit disabled. +*/ +pr_err ("Failed to allocate ve_info. disabling EPT_VIOLATION_VE.\n" ); +secondary_exec_controls_clearbit ( + vmx,SECONDARY_EXEC_EPT_VIOLATION_VE ); + } + } + }if (cpu_has_tertiary_exec_ctrls ())tertiary_exec_controls_set (vmx,vmx_tertiary_exec_control (vmx));@@ -5116,7 +5162,14 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)if (handle_guest_split_lock (kvm_rip_read (vcpu)))return 1 ; fallthrough; +case VE_VECTOR :default : +if (ept_violation_ve_test && ex_no ==VE_VECTOR ) { +pr_err ("VMEXIT due to unexpected #VE.\n" ); +secondary_exec_controls_clearbit ( + vmx,SECONDARY_EXEC_EPT_VIOLATION_VE ); +return 1 ; + } kvm_run->exit_reason =KVM_EXIT_EXCEPTION ; kvm_run->ex.exception = ex_no; kvm_run->ex.error_code = error_code;@@ -6182,6 +6235,17 @@ void dump_vmcs(struct kvm_vcpu *vcpu)if (secondary_exec_control &SECONDARY_EXEC_ENABLE_VPID )pr_err ("Virtual processor ID = 0x%04x\n" ,vmcs_read16 (VIRTUAL_PROCESSOR_ID )); +if (secondary_exec_control &SECONDARY_EXEC_EPT_VIOLATION_VE ) { +struct vmx_ve_information *ve_info; +pr_err ("VE info address = 0x%016llx\n" , +vmcs_read64 (VE_INFORMATION_ADDRESS )); + ve_info =__va (vmcs_read64 (VE_INFORMATION_ADDRESS )); +pr_err ("ve_info: 0x%08x 0x%08x 0x%016llx 0x%016llx 0x%016llx 0x%04x\n" , + ve_info->exit_reason, ve_info->delivery, + ve_info->exit_qualification, + ve_info->guest_linear_address, + ve_info->guest_physical_address, ve_info->eptp_index); + } }/* @@ -7173,6 +7237,8 @@ void vmx_vcpu_free(struct kvm_vcpu *vcpu)free_vpid (vmx->vpid);nested_vmx_free_vcpu (vcpu);free_loaded_vmcs (vmx->loaded_vmcs); +if (vmx->ve_info) +free_page ((unsigned long )vmx->ve_info); }int vmx_vcpu_create (struct kvm_vcpu *vcpu)arch/x86/kvm/vmx/vmx.h +3
-0@@ -338,6 +338,9 @@ struct vcpu_vmx {DECLARE_BITMAP (read,MAX_POSSIBLE_PASSTHROUGH_MSRS );DECLARE_BITMAP (write,MAX_POSSIBLE_PASSTHROUGH_MSRS ); } shadow_msr_intercept; + +/* ve_info must be page aligned. */ +struct vmx_ve_information *ve_info; };struct kvm_vmx {
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 9 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.
Quoted 5 lines
I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
https://lore.kernel.org/lkml/[email protected]/
Quoted 27 lines · On 3/4/22 20:48, [email protected] wrote:
Review
Quoted 7 lines
+ if (
enable_ept) {(quoted diff) +4
-1+ const u64 init_value = enable_tdx ? VMX_EPT_SUPPRESS_VE_BIT : 0ull; kvm_mmu_set_ept_masks(enable_ept_ad_bits, - cpu_has_vmx_ept_execute_only()); + cpu_has_vmx_ept_execute_only(), init_value); + kvm_mmu_set_spte_init_value(init_value); + }I think kvm-intel.ko should use
VMX_EPT_SUPPRESS_VE_BITunconditionally as the init value. The bit is ignored anyway if the "EPT-violation #VE" execution control is 0. Otherwise looks good, but I have a couple more crazy ideas:1) there could even be a test mode where KVM enables the execution control, traps #VE in the exception bitmap, and shouts loudly if it gets a #VE. That might avoid hard-to-find bugs due to forgetting about
VMX_EPT_SUPPRESS_VE_BIT.2) or even, perhaps the
init_valuefor the TDP MMU could set bit 63 _unconditionally_, because KVM always sets the NX bit on AMD hardware. That would remove the whole infrastructure to keepshadow_init_value, because it would be constant 0 inmmu.cand constant BIT(63) intdp_mmu.c.Sean, what do you think?
Paolo
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 15 lines · Isaku Yamahata
Quoted 12 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
Quoted 9 lines
Quoted 2 lines
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
https://lore.kernel.org/lkml/[email protected]/
Quoted 1 line
OK. But can we assume a normal guest won't sending #VE IPI?
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 19 lines · Kai Huang
Quoted 18 lines · Isaku Yamahata
Quoted 14 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Quoted 8 lines
Quoted 6 lines
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
https://lore.kernel.org/lkml/[email protected]/
Quoted 1 line
OK. But can we assume a normal guest won't sending #VE IPI?
Theoretically nothing prevents that. I wouldn't way "normal". Anyway this is off by default.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 25 lines · Isaku Yamahata
Quoted 22 lines · Kai Huang
Quoted 21 lines · Isaku Yamahata
Quoted 9 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.Quoted 5 lines
I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
https://lore.kernel.org/lkml/[email protected]/
Quoted 1 line
OK. But can we assume a normal guest won't sending #VE IPI?
Theoretically nothing prevents that. I wouldn't way "normal". Anyway this is off by default.
I don't think whether it is on or off by default matters. If it can happen legitimately in the guest, it doesn't look right to print out something like below:
pr_err("VMEXIT due to unexpected #VE.\n");Anyway, will let maintainers to decide.
Signature · 2 lines
Thanks, -Kai
Sean Christopherson
maintainerRe: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 30 lines · Kai Huang
Quoted 29 lines · Isaku Yamahata
Quoted 26 lines · Kai Huang
Quoted 24 lines · Isaku Yamahata
Quoted 9 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.Quoted 5 lines
I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
https://lore.kernel.org/lkml/[email protected]/
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
OK. But can we assume a normal guest won't sending #VE IPI?
Theoretically nothing prevents that. I wouldn't way "normal". Anyway this is off by default.
I don't think whether it is on or off by default matters.
It matters in the sense that the module param is intended purely for testing, i.e. there's zero reason to ever enable it in production. That changes what is and wasn't isn't a reasonable response to an unexpected #VE.
Quoted 4 lines
If it can happen legitimately in the guest, it doesn't look right to print out something like below:
pr_err("VMEXIT due to unexpected #VE.\n");
Agreed. In this particular case I think the right approach is to treat an unexpected #VE as a fatal KVM bug. Yes, disabling EPT violation #VEs would likely allow the guest to live, but as above the module param should never be enabled in production. And if we get a #VE with the module param disabled, then KVM is truly in the weeds and killing the VM is the safe option.
E.g. something like
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -5010,6 +5010,9 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)if (is_invalid_opcode (intr_info))return handle_ud (vcpu); +if (KVM_BUG_ON (is_ve_fault (intr_info), vcpu->kvm)) +return -EIO ; + error_code =0 ;if (intr_info &INTR_INFO_DELIVER_CODE_MASK ) error_code =vmcs_read32 (VM_EXIT_INTR_ERROR_CODE );
Re: [PATCH v7 041/102] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 47 lines · Sean Christopherson
Quoted 30 lines · Kai Huang
Quoted 29 lines · Isaku Yamahata
Quoted 26 lines · Kai Huang
Quoted 24 lines · Isaku Yamahata
Quoted 9 lines · Kai Huang
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option
ept_violation_ve_testand when it's set, set error.Quoted 5 lines
I don't see why we need this patch. It may be helpful during your test, but why do we need this patch for formal submission?
And for a normal guest, what prevents one vcpu from sending #VE IPI to another vcpu?
Paolo suggested it as follows. Maybe it should be kernel config. (I forgot to add suggested-by. I'll add it)
https://lore.kernel.org/lkml/[email protected]/
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
OK. But can we assume a normal guest won't sending #VE IPI?
Theoretically nothing prevents that. I wouldn't way "normal". Anyway this is off by default.
I don't think whether it is on or off by default matters.
It matters in the sense that the module param is intended purely for testing, i.e. there's zero reason to ever enable it in production. That changes what is and wasn't isn't a reasonable response to an unexpected #VE.
Quoted 4 lines
If it can happen legitimately in the guest, it doesn't look right to print out something like below:
pr_err("VMEXIT due to unexpected #VE.\n");
Quoted 7 lines
Agreed. In this particular case I think the right approach is to treat an unexpected #VE as a fatal KVM bug. Yes, disabling EPT violation #VEs would likely allow the guest to live, but as above the module param should never be enabled in production. And if we get a #VE with the module param disabled, then KVM is truly in the weeds and killing the VM is the safe option.
E.g. something like
Thanks, I finally ended up with the following.
arch/x86/kvm/vmx/vmcs.h +5 -0
@@ -140,6 +140,11 @@ static inline bool is_nm_fault(u32 intr_info)return is_exception_n (intr_info,NM_VECTOR ); } +static inline bool is_ve_fault (u32 intr_info) +{ +return is_exception_n (intr_info,VE_VECTOR ); +} +/* Undocumented: icebp/int1 */ static inline bool is_icebp (u32 intr_info) {
arch/x86/kvm/vmx/vmx.c +6 -7
@@ -5047,6 +5047,12 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)if (is_invalid_opcode (intr_info))return handle_ud (vcpu); +/* +* #VE isn't supposed to happen. Although vcpu can send +*/ +if (KVM_BUG_ON (is_ve_fault (intr_info), vcpu->kvm)) +return -EIO ; + error_code =0 ;if (intr_info &INTR_INFO_DELIVER_CODE_MASK ) error_code =vmcs_read32 (VM_EXIT_INTR_ERROR_CODE );
@@ -5167,14 +5173,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)if (handle_guest_split_lock (kvm_rip_read (vcpu)))return 1 ; fallthrough; -case VE_VECTOR :default : -if (ept_violation_ve_test && ex_no ==VE_VECTOR ) { -pr_err ("VMEXIT due to unexpected #VE.\n" ); -secondary_exec_controls_clearbit ( - vmx,SECONDARY_EXEC_EPT_VIOLATION_VE ); -return 1 ; - } kvm_run->exit_reason =KVM_EXIT_EXCEPTION ; kvm_run->ex.exception = ex_no; kvm_run->ex.error_code = error_code;
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 42/102 The start of TDX KVM patch series: KVM TDP MMU hooks
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU hooks.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -25,6 +25,6 @@ Patch Layer status * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied -* KVM TDP refactoring for TDX: Applying -* KVM TDP MMU hooks: Not yet +* KVM TDP refactoring for TDX: Applied +* KVM TDP MMU hooks: Applying * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
PATCH v7 43/102 KVM: x86/mmu: Focibly use TDP MMU for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
In this patch series, TDX supports only TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
--- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +7 -2
@@ -18,8 +18,13 @@ int kvm_mmu_init_tdp_mmu(struct kvm *kvm) {struct workqueue_struct *wq; -if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) -return 0 ; +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled))) +return false; wq =alloc_workqueue ("kvm" ,WQ_UNBOUND |WQ_MEM_RECLAIM |WQ_CPU_INTENSIVE ,0 );if (!wq)
Signature · 1 line
2.25.1
Re: [PATCH v7 043/102] KVM: x86/mmu: Focibly use TDP MMU for TDX
Quoted 18 lines · On Mon, Jun 27, 2022 at 02:53:35PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
In this patch series, TDX supports only TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0@@ -18,8 +18,13 @@ int kvm_mmu_init_tdp_mmu(struct kvm *kvm) {struct workqueue_struct *wq;
arch/x86/kvm/mmu/tdp_mmu.c int kvm_mmu_init_tdp_mmu(struct kvm *kvm) · line 27
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-2-if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) -return 0 ; +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled))) +return false;
Please return 0 here for int return value type.
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-1wq =alloc_workqueue ("kvm" ,WQ_UNBOUND |WQ_MEM_RECLAIM |WQ_CPU_INTENSIVE ,0 );if (!wq) --2.25.1
Sean Christopherson
maintainerRe: [PATCH v7 043/102] KVM: x86/mmu: Focibly use TDP MMU for TDX
In reply to [email protected]s/Focibly/Forcibly, but that's a moot point because KVM shouldn't override the the module param. KVM should instead _require_ the TDP MMU to be enabled. E.g. if userspace disables the TDP MMU to workaround a fatal bug, then forcing the TDP MMU may silently expose KVM to said bug.
And overriding tdp_enabled is just mind-boggling broken, all of the SPTE masks will be wrong.
Quoted 5 lines · On Mon, Jun 27, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
In this patch series, TDX supports only TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
Do not refer to the "patch series", instead phrase the statement with respect to what KVM support.
Require the TDP MMU for TDX guests, the so called "shadow" MMU does not support mapping guest private memory, i.e. does not support Secure-EPT.
Quoted 12 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0@@ -18,8 +18,13 @@ int kvm_mmu_init_tdp_mmu(struct kvm *kvm) {struct workqueue_struct *wq;
arch/x86/kvm/mmu/tdp_mmu.c int kvm_mmu_init_tdp_mmu(struct kvm *kvm) · line 27
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-2-if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) -return 0 ; +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled))) +return false;
Yeah, no.
if (!tdp_enabled || !READ_ONCE(tdp_mmu_enabled)) return kvm->arch.vm_type == KVM_X86_TDX_VM ? -EINVAL : 0;
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0wq =alloc_workqueue ("kvm" ,WQ_UNBOUND |WQ_MEM_RECLAIM |WQ_CPU_INTENSIVE ,0 );if (!wq)Signature · 1 line
2.25.1
Re: [PATCH v7 043/102] KVM: x86/mmu: Focibly use TDP MMU for TDX
Quoted 15 lines · Sean Christopherson
s/Focibly/Forcibly, but that's a moot point because KVM shouldn't override the the module param. KVM should instead _require_ the TDP MMU to be enabled. E.g. if userspace disables the TDP MMU to workaround a fatal bug, then forcing the TDP MMU may silently expose KVM to said bug.
And overriding
tdp_enabledis just mind-boggling broken, all of the SPTE masks will be wrong.Quoted 5 lines · On Mon, Jun 27, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
In this patch series, TDX supports only TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
Quoted 5 lines
Do not refer to the "patch series", instead phrase the statement with respect to what KVM support.
Require the TDP MMU for TDX guests, the so called "shadow" MMU does not support mapping guest private memory, i.e. does not support Secure-EPT.
Thanks for rewrite of the commit message. Now the TDP MMU is default, I'll change
Quoted 25 lines
Quoted 12 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0@@ -18,8 +18,13 @@ int kvm_mmu_init_tdp_mmu(struct kvm *kvm) {struct workqueue_struct *wq;arch/x86/kvm/mmu/tdp_mmu.c int kvm_mmu_init_tdp_mmu(struct kvm *kvm) · line 27
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-2-if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) -return 0 ; +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled))) +return false;Yeah, no.
Quoted 2 lines
if (!tdp_enabled || !READ_ONCE(tdp_mmu_enabled)) return kvm->arch.vm_type == KVM_X86_TDX_VM ? -EINVAL : 0;
I'll use -EOPNOTSUPP instead of -EINVAL.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 44/102 KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct kvm_mmu_page for mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a given kvm_mmu_page is private. The later patch introduces hooks to operate on the mirrored private page table.
KVM page fault |
| |
V |
-------------+---------- |
| | |
V V |
shared GPA private GPA |
| | |
V V |
CPU/KVM shared PT root KVM private PT root | CPU private PT root
| | | |
V V | V
shared PT private PT <----mirror----> mirrored private PT
| | | |
| \-----------------+------\ |
| | | |
V | V V
shared guest page | private guest page
|
non-encrypted memory | encrypted memory
|PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state.
arch/x86/kvm/mmu/mmu.c +9 -0
@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);
@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }
@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
arch/x86/kvm/mmu/mmu_internal.h +84 -0
96 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +3 -0
@@ -72,6 +72,8 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }
@@ -295,6 +297,7 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }
Signature · 1 line
2.25.1
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
Quoted 43 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |
Quoted 4 lines
PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Shouldn't the private page table maintained by KVM be "mirrored private PT"?
To me "mirrored" normally implies it is fake, or backup which isn't actually used. But here "mirrored private PT" is actually used by hardware.
And to me, "CPU and KVM" above are confusing. For instance, "Both CPU and KVM refer to CPU/KVM shared page table" took me at least one minute to understand, with the help from the diagram -- otherwise I won't be able to understand.
I guess you can just say somewhere:
1) Shared PT is visible to KVM and it is used by CPU;
1) Private PT is used by CPU but it is invisible to KVM;
2) Mirrored private PT is visible to KVM but not used by CPU. It is used to mirror the actual private PT which is used by CPU.
[...]
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +5
-0+ +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ + sp->private_sp = private_sp; +}
[...]
arch/x86/kvm/mmu/tdp_mmu.c static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, · line 300
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0@@ -295,6 +297,7 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);
Can this even compile? Unless I am seeing mistakenly, kvm_mmu_init_private_sp() (see above) has two arguments..
Please make sure each patch can at least compile and doesn't cause warning...
Signature · 2 lines
Thanks, -Kai
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
Quoted 58 lines · Kai Huang
Quoted 43 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |Quoted 4 lines
PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Shouldn't the private page table maintained by KVM be "mirrored private PT"?
To me "mirrored" normally implies it is fake, or backup which isn't actually used. But here "mirrored private PT" is actually used by hardware.
And to me, "CPU and KVM" above are confusing. For instance, "Both CPU and KVM refer to CPU/KVM shared page table" took me at least one minute to understand, with the help from the diagram -- otherwise I won't be able to understand.
Quoted 6 lines
I guess you can just say somewhere:
1) Shared PT is visible to KVM and it is used by CPU;
1) Private PT is used by CPU but it is invisible to KVM;
2) Mirrored private PT is visible to KVM but not used by CPU. It is used to mirror the actual private PT which is used by CPU.
I removed "mirror" word and use protected for encrypted page table.
KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk protected (encrypted) page table, allocate one more page to copy the protected page table for KVM MMU code to directly walk. Resolve KVM page fault with the existing code, and do additional operations necessary for the protected page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. not associated with protected page table), and the page table with protected page table is called a private page table. The relationship is depicted below.
Add a private pointer to struct kvm_mmu_page for protected page table and add helper functions to allocate/initialize/free a protected page table page. Also, add helper functions to check if a given kvm_mmu_page is private. The later patch introduces hooks to operate on the protected page table.
KVM page fault |
| |
V |
-------------+---------- |
| | |
V V |
shared GPA private GPA |
| | |
V V |
shared PT root private PT root | protected PT root
| | | |
V V | V
shared PT private PT ----propagate----> protected PT
| | | |
| \-----------------+------\ |
| | | |
V | V V
shared guest page | private guest page
|
non-encrypted memory | encrypted memory
|
PT: page table
- Shared PT is visible to KVM and it is used by CPU.
- Protected PT is used by CPU but it is invisible to KVM.
- Private PT is visible to KVM but not used by CPU. It is used to propagate PT change to the actual protected PT which is used by CPU.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
In reply to [email protected]Quoted 118 lines · On Mon, Jun 27, 2022 at 02:53:36PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +1
-0@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +9
-0@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +4
-0@@ -55,6 +55,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE +/* associated private shadow page, e.g. SEPT page. */ +void *private_sp; +#endif /* Currently serving as active root */ union {int root_count;
arch/x86/kvm/mmu/mmu_internal.h static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp) · line 123
Quoted 5 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -115,6 +119,86 @@ static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)return kvm_mmu_role_as_id (sp->role); } +/* +* TDX vcpu allocates page for root Secure EPT page and assigns to CPU secure
"TDX vcpu" is a little confused, how about "TDX moudule allocates(or manages) page for ..." ?
Quoted 18 lines
arch/x86/kvm/mmu/mmu_internal.h +18
-0+* EPT pointer. KVM doesn't need to allocate and link to the secure EPT. +* Dummy value to make is_pivate_sp() return true. +*/ +#define KVM_MMU_PRIVATE_SP_ROOT ((void *)1) + +#ifdef CONFIG_KVM_MMU_PRIVATE +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return !!sp->private_sp; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +WARN_ON (!sptep); +return is_private_sp (sptep_to_sp (sptep)); +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp)
arch/x86/kvm/mmu/mmu_internal.h static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp) · line 151
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+{ +return sp->private_sp; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ + sp->private_sp = private_sp; +} + +/* Valid sp->role.level is required. */
I didn't see such requirement in kvm_mmu_alloc_private_sp(), please consider to move the comment with the code that introduces such requirement together.
Quoted 77 lines
arch/x86/kvm/mmu/mmu_internal.h +50
-0+static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +if (is_root) + sp->private_sp =KVM_MMU_PRIVATE_SP_ROOT ; +else + sp->private_sp =kvm_mmu_memory_cache_alloc ( + &vcpu->arch.mmu_private_sp_cache); +/* +* Because mmu_private_sp_cache is topped up before staring kvm page +* fault resolving, the allocation above shouldn't fail. +*/ +WARN_ON_ONCE (!sp->private_sp); +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT ) +free_page ((unsigned long )sp->private_sp); +} +#else +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return false; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +return false; +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return NULL ; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ +} + +static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +} +#endif +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* arch/x86/kvm/mmu/tdp_mmu.c +3
-1@@ -72,6 +72,8 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }@@ -295,6 +297,7 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); } --2.25.1
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
In reply to [email protected]Quoted 61 lines · On Mon, Jun 27, 2022 at 02:53:36PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 719
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;
I notice that mmu_private_sp_cache.gfp_zero is left unset so these pages may contain garbage. Is this by design because the TDX module can't rely on the contents being zero and has to take care of initializing the page itself? i.e. GFP_ZERO would be a waste of cycles?
If I'm correct please include a comment here in the next revision to explain why GFP_ZERO is not necessary.
Quoted 30 lines
arch/x86/include/asm/kvm_host.h +0
-0/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +8
-0@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }
arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct · line 1747
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +1
-0@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );
This is unnecessary. kvm_mmu_page structs are zero-initialized so private_sp will already be NULL.
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 59
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +0
-0/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -55,6 +55,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE +/* associated private shadow page, e.g. SEPT page. */
Can we use "Secure EPT" instead of SEPT in KVM code and comments? (i.e. also including variable names like sept_page -> secure_ept_page)
"SEPT" looks like a mispelling of SPTE, which is used all over KVM. It will be difficult to read code that contains both acronyms.
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 60
Quoted 1 line
arch/x86/kvm/mmu/mmu_internal.h +1
-0+void *private_sp;
Please name this "private_spt" and move it up next to "spt".
sp" or "shadow page" is used to refer to kvm_mmu_page structs. For example, look at all the code in KVM that uses struct kvm_mmu_page *sp.
"spt" is "shadow page table", i.e. the actual page table memory. See kvm_mmu_page.spt. Calling this field "private_spt" makes it obvious that this pointer is pointing to a page table.
Also please update the language in the comment accordingly to "private shadow page table".
Quoted 115 lines
arch/x86/kvm/mmu/mmu_internal.h +81
-0+#endif /* Currently serving as active root */ union {int root_count;@@ -115,6 +119,86 @@ static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)return kvm_mmu_role_as_id (sp->role); } +/* +* TDX vcpu allocates page for root Secure EPT page and assigns to CPU secure +* EPT pointer. KVM doesn't need to allocate and link to the secure EPT. +* Dummy value to make is_pivate_sp() return true. +*/ +#define KVM_MMU_PRIVATE_SP_ROOT ((void *)1) + +#ifdef CONFIG_KVM_MMU_PRIVATE +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return !!sp->private_sp; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +WARN_ON (!sptep); +return is_private_sp (sptep_to_sp (sptep)); +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return sp->private_sp; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ + sp->private_sp = private_sp; +} + +/* Valid sp->role.level is required. */ +static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +if (is_root) + sp->private_sp =KVM_MMU_PRIVATE_SP_ROOT ; +else + sp->private_sp =kvm_mmu_memory_cache_alloc ( + &vcpu->arch.mmu_private_sp_cache); +/* +* Because mmu_private_sp_cache is topped up before staring kvm page +* fault resolving, the allocation above shouldn't fail. +*/ +WARN_ON_ONCE (!sp->private_sp); +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT ) +free_page ((unsigned long )sp->private_sp); +} +#else +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return false; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +return false; +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return NULL ; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ +} + +static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +} +#endif +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* arch/x86/kvm/mmu/tdp_mmu.c +3
-0@@ -72,6 +72,8 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }@@ -295,6 +297,7 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }Signature · 1 line
2.25.1
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
Quoted 15 lines · David Matlack
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 719
Quoted 9 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;I notice that mmu_private_sp_cache.gfp_zero is left unset so these pages may contain garbage. Is this by design because the TDX module can't rely on the contents being zero and has to take care of initializing the page itself? i.e.
GFP_ZEROwould be a waste of cycles?
Quoted 2 lines
If I'm correct please include a comment here in the next revision to explain why
GFP_ZEROis not necessary.
Yes, exactly. Here is the added comments.
/*
* This cache is to allocate pages used for Secure-EPT used by the TDX
* module. Because the TDX module doesn't trust VMM and initializes the
* pages itself, KVM doesn't initialize them. Allocate pages with
* garbage and give them to the TDX module.
*/
Quoted 20 lines
Quoted 20 lines
arch/x86/include/asm/kvm_host.h +0
-0/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +7
-0@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);
Quoted 16 lines
arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct · line 1747
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +2
-0@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );This is unnecessary.
kvm_mmu_pagestructs are zero-initialized soprivate_spwill already be NULL.
Ok.
Quoted 17 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 59
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +0
-0/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -55,6 +55,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE +/* associated private shadow page, e.g. SEPT page. */ Can we use "Secure EPT" instead of SEPT in KVM code and comments? (i.e. also including variable names like
sept_page->secure_ept_page)
Quoted 2 lines
"SEPT" looks like a mispelling of SPTE, which is used all over KVM. It will be difficult to read code that contains both acronyms.
Makes sense. Will update it.
Quoted 7 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 60
Quoted 1 line
arch/x86/kvm/mmu/mmu_internal.h +1
-0+void *private_sp;Please name this "
private_spt" and move it up next to "spt".sp" or "shadow page" is used to refer to
kvm_mmu_pagestructs. For example, look at all the code in KVM that usesstruct kvm_mmu_page *sp.
Quoted 6 lines
"spt" is "shadow page table", i.e. the actual page table memory. See kvm_mmu_page.spt. Calling this field "
private_spt" makes it obvious that this pointer is pointing to a page table.Also please update the language in the comment accordingly to "private shadow page table".
I'll rename as follows
private_sp => private_sptspet_page => private_sptmmu_private_sp_cache => mmu_private_spt_cache kvm_mmu_init_private_sp => kvm_mmu_inite_private_spt kvm_mmu_alloc_private_sp => kvm_mmu_alloc_private_spt kvm_mmu_free_private_sp => kvm_mmu_free_private_spt kvm_alloc_private_sp_for_split => kvm_alloc_private_spt_for_split
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
In reply to [email protected]Quoted 107 lines · On Mon, Jun 27, 2022 at 02:53:36PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +1
-0@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +9
-0@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +0
-0
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 61
Quoted 7 lines
arch/x86/kvm/mmu/mmu_internal.h +4
-0@@ -55,6 +55,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE +/* associated private shadow page, e.g. SEPT page. */ +void *private_sp; +#endif
write_flooding_count and unsync_children are only used in shadow MMU SPs and private_sp is only used in TDP MMU SPs. So it seems like we could put these together in a union and drop CONFIG_KVM_MMU_PRIVATE without increasing the size of kvm_mmu_page. i.e.
union {
struct {
unsigned int unsync_children;
/* Number of writes since the last time traversal visited this page. */
atomic_t write_flooding_count;
};
/*
* The associated private shadow page table, e.g. for Secure EPT.
* Only valid if tdp_mmu_page is true.
*/
void *private_spt;
};Then change is_private_sp() to:
static inline bool is_private_sp(struct kvm_mmu_page *sp)
{
return sp->tdp_mmu_page && sp->private_sp;
}This will allow us to drop CONFIG_KVM_MMU_PRIVATE, the only benefit of which I see is to avoid increasing the size of kvm_mmu_page. However to actually realize that benefit Cloud vendors (for example) would have to create separate kernel builds for TDX and non-TDX hosts, which seems like a huge hassel.
Quoted 114 lines
arch/x86/kvm/mmu/mmu_internal.h +80
-0/* Currently serving as active root */ union {int root_count;@@ -115,6 +119,86 @@ static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)return kvm_mmu_role_as_id (sp->role); } +/* +* TDX vcpu allocates page for root Secure EPT page and assigns to CPU secure +* EPT pointer. KVM doesn't need to allocate and link to the secure EPT. +* Dummy value to make is_pivate_sp() return true. +*/ +#define KVM_MMU_PRIVATE_SP_ROOT ((void *)1) + +#ifdef CONFIG_KVM_MMU_PRIVATE +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return !!sp->private_sp; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +WARN_ON (!sptep); +return is_private_sp (sptep_to_sp (sptep)); +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return sp->private_sp; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ + sp->private_sp = private_sp; +} + +/* Valid sp->role.level is required. */ +static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +if (is_root) + sp->private_sp =KVM_MMU_PRIVATE_SP_ROOT ; +else + sp->private_sp =kvm_mmu_memory_cache_alloc ( + &vcpu->arch.mmu_private_sp_cache); +/* +* Because mmu_private_sp_cache is topped up before staring kvm page +* fault resolving, the allocation above shouldn't fail. +*/ +WARN_ON_ONCE (!sp->private_sp); +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT ) +free_page ((unsigned long )sp->private_sp); +} +#else +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return false; +} + +static inline bool is_private_sptep (u64 *sptep) +{ +return false; +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return NULL ; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp,void *private_sp) +{ +} + +static inline void kvm_mmu_alloc_private_sp ( +struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,bool is_root) +{ +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +} +#endif +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* arch/x86/kvm/mmu/tdp_mmu.c +3
-0@@ -72,6 +72,8 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }@@ -295,6 +297,7 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }Signature · 1 line
2.25.1
Re: [PATCH v7 044/102] KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
Quoted 112 lines · David Matlack
Quoted 111 lines · On Mon, Jun 27, 2022 at 02:53:36PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk encrypted private page table, allocate a more page to mirror the existing KVM page table. Resolve KVM page fault with the existing code, and do additional operations necessary for the mirrored private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. no mirrored private page table), and the KVM page table with mirrored private page table is called a private page table. The relationship is depicted below.
Add private pointer to struct
kvm_mmu_pagefor mirrored private page table and add helper functions to allocate/initialize/free a mirrored private page table page. Also, add helper functions to check if a givenkvm_mmu_pageis private. The later patch introduces hooks to operate on the mirrored private page table.KVM page fault | | | V | -------------+---------- | | | | V V | shared GPA private GPA | | | | V V | CPU/KVM shared PT root KVM private PT root | CPU private PT root | | | | V V | V shared PT private PT <----mirror----> mirrored private PT | | | | | \-----------------+------\ | | | | | V | V V shared guest page | private guest page | non-encrypted memory | encrypted memory |PT: page table
Both CPU and KVM refer to CPU/KVM shared page table. Private page table is used only by KVM. CPU refers to mirrored private page table.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +1
-0@@ -716,6 +716,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +9
-0@@ -691,6 +691,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)int start, end, i, r;bool is_tdp_mmu =is_tdp_mmu_enabled (vcpu->kvm); +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (is_tdp_mmu && shadow_nonpresent_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);@@ -732,6 +739,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }@@ -1736,6 +1744,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp,NULL );/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +1
-0@@ -55,6 +55,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_mmu_page { · line 61
Quoted 3 lines
arch/x86/kvm/mmu/mmu_internal.h +3
-0+/* associated private shadow page, e.g. SEPT page. */ +void *private_sp; +#endif
write_flooding_countandunsync_childrenare only used in shadow MMU SPs andprivate_spis only used in TDP MMU SPs. So it seems like we could put these together in a union and dropCONFIG_KVM_MMU_PRIVATEwithout increasing the size ofkvm_mmu_page. i.e.
I introduced KVM_MMU_PRIVATE as a alias to INTEL_TDX_HOST because I don't want to use it in kvm/mmu and I'd like KVM_MMU_PRIVATE (a sort of) independent from INTEL_TDX_HOST. Anyway once the patch series is merged, we can drop KVM_MMU_PRIVATE.
Quoted 20 lines
union { struct { unsigned int unsync_children; /* Number of writes since the last time traversal visited this page. */ atomic_t write_flooding_count; }; /* * The associated private shadow page table, e.g. for Secure EPT. * Only valid if tdp_mmu_page is true. */ void *private_spt; };Then change
is_private_sp()to:static inline bool
is_private_sp(struct kvm_mmu_page *sp){ return sp->tdp_mmu_page && sp->private_sp; }
Quoted 5 lines
This will allow us to drop
CONFIG_KVM_MMU_PRIVATE, the only benefit of which I see is to avoid increasing the size ofkvm_mmu_page. However to actually realize that benefit Cloud vendors (for example) would have to create separate kernel builds for TDX and non-TDX hosts, which seems like a huge hassel.
Good idea. I'll use union.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 45/102 KVM: x86/tdp_mmu: refactor kvm_tdp_mmu_map()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out non-leaf SPTE population logic from kvm_tdp_mmu_map(). MapGPA hypercall needs to populate non-leaf SPTE to record which GPA, private or shared, is allowed in the leaf EPT entry.
--- arch/x86/kvm/mmu/tdp_mmu.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +19 -7
@@ -1167,6 +1167,24 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,return 0 ; } +static int tdp_mmu_populate_nonleaf ( +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +{ +struct kvm_mmu_page *sp; +int ret; + +WARN_ON (is_shadow_present_pte (iter->old_spte)); +WARN_ON (is_removed_spte (iter->old_spte)); + + sp =tdp_mmu_alloc_sp (vcpu); +tdp_mmu_init_child_sp (sp, iter); + + ret =tdp_mmu_link_sp (vcpu->kvm, iter, sp, account_nx, true); +if (ret) +tdp_mmu_free_sp (sp); +return ret; +} +/* * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing * page tables and SPTEs to translate the faulting guest physical address.
@@ -1175,7 +1193,6 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; -struct kvm_mmu_page *sp;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);
@@ -1221,13 +1238,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (is_removed_spte (iter.old_spte))break ; - sp =tdp_mmu_alloc_sp (vcpu); -tdp_mmu_init_child_sp (sp, &iter); - -if (tdp_mmu_link_sp (vcpu->kvm, &iter, sp, account_nx, true)) { -tdp_mmu_free_sp (sp); +if (tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx))break ; - } } }
Signature · 1 line
2.25.1
PATCH v7 46/102 KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Allocate mirrored private page table for private page table, and add hooks to operate on mirrored private page table. This patch adds only hooks. As kvm_gfn_shared_mask() returns false always, those hooks aren't called yet.
Because private guest page is protected, page copy with mmu_notifier to migrate page doesn't work. Callback from backing store is needed.
When the faulting GPA is private, the KVM fault is also called private. When resolving private KVM, allocate mirrored private page table and call hooks to operate on mirrored private page table. On the change of the private PTE entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to mirrored private page table. The following depicts the relationship.
private KVM page fault |
| |
V |
private GPA |
| |
V |
KVM private PT root | CPU private PT root
| | |
V | V
private PT ---hook to mirror--->mirrored private PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For mirrored private page table, hooks are called to update mirrored private page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K mirrored private SPTE => error vcpu 1: TDX SEAMCALL to update 2M mirrored private SPTE
To avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update mirrored private SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add is_private member to kvm_page_fault to indicate the fault is private. Also is_private member to struct tdp_inter to propagate it.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 86 +++++++++- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_iter.c | 1 + arch/x86/kvm/mmu/tdp_iter.h | 5 +- arch/x86/kvm/mmu/tdp_mmu.c | 247 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 7 +- virt/kvm/kvm_main.c | 1 + 10 files changed, 346 insertions(+), 62 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +20 -0
@@ -440,6 +440,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;
@@ -1435,6 +1436,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;
@@ -1547,6 +1562,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +79 -7
182 lines not on this page · raw message
arch/x86/kvm/mmu/mmu_internal.h +37 -0
@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG
@@ -164,11 +166,30 @@ static inline void kvm_mmu_alloc_private_sp(WARN_ON_ONCE (!sp->private_sp); } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT )free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline bool is_private_sp (struct kvm_mmu_page *sp) {
@@ -194,11 +215,25 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r); +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/*
@@ -246,6 +281,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;/* * Whether a >4KB mapping can be created or is forbidden due to NX
@@ -327,6 +363,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K,
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -877,7 +877,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);return r; }
arch/x86/kvm/mmu/tdp_iter.c +1 -0
@@ -53,6 +53,7 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root, iter->min_level = min_level; iter->pt_path[iter->root_level -1 ] = (tdp_ptep_t )root->spt; iter->as_id =kvm_mmu_page_as_id (root); + iter->is_private =is_private_sp (root);tdp_iter_restart (iter); }
arch/x86/kvm/mmu/tdp_iter.h +4 -1
@@ -71,7 +71,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
@@ -94,6 +94,9 @@ struct tdp_iter {* level instead of advancing to the next entry. */ bool yielded; + +/* True if this iter is handling private KVM page fault. */ +bool is_private; };/*
arch/x86/kvm/mmu/tdp_mmu.c +197 -50
521 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +4 -3
@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {
@@ -16,7 +16,8 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool drop_private);bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp);void kvm_tdp_mmu_zap_all (struct kvm *kvm);void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm);
@@ -25,7 +26,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush); +bool flush,bool drop_private);bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +1 -0
@@ -196,6 +196,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 1 line
2.25.1
Re: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 143 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate mirrored private page table for private page table, and add hooks to operate on mirrored private page table. This patch adds only hooks. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet.Because private guest page is protected, page copy with
mmu_notifierto migrate page doesn't work. Callback from backing store is needed.When the faulting GPA is private, the KVM fault is also called private. When resolving private KVM, allocate mirrored private page table and call hooks to operate on mirrored private page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to mirrored private page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | | | V | KVM private PT root | CPU private PT root | | | V | V private PT ---hook to mirror--->mirrored private PT | | | \--------------------+------\ | | | | | V V | private guest page | | non-encrypted memory | encrypted memory |PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For mirrored private page table, hooks are called to update mirrored private page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K mirrored private SPTE => error vcpu 1: TDX SEAMCALL to update 2M mirrored private SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update mirrored private SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add
is_privatemember tokvm_page_faultto indicate the fault is private. Alsois_privatemember to structtdp_interto propagate it.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 86 +++++++++- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_iter.c | 1 + arch/x86/kvm/mmu/tdp_iter.h | 5 +- arch/x86/kvm/mmu/tdp_mmu.c | 247 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 7 +- virt/kvm/kvm_main.c | 1 + 10 files changed, 346 insertions(+), 62 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +20
-0@@ -440,6 +440,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1435,6 +1436,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;@@ -1547,6 +1562,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1583
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +4
-1@@ -1577,7 +1577,11 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) flush =kvm_handle_gfn_range (kvm, range, kvm_unmap_rmapp);if (is_tdp_mmu_enabled (kvm)) - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush); +/* +* private page needs to be kept and handle page migration +* on next EPT violation. +*/
I don't think this series supports page migration? How can page migration be handled in next EPT violation?
arch/x86/kvm/mmu/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1584
Quoted 1 line
arch/x86/kvm/mmu/mmu.c +1
-0+ flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush, false);
The meaning of the additional 'false' isn't clear at all. I need to go through entire patch to figure out what does it mean.
How about splitting 'adding additional false argument' part as a separate patch (no functional change), give a short changelog to explain, and put it before this patch? In this way we can clearly understand what it does here.
arch/x86/kvm/mmu/mmu.c static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau · line 3090
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +2
-1return flush; }@@ -3082,7 +3086,8 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* SPTE value without #VE suppress bit cleared * (kvm->arch.shadow_mmio_value = 0). */ - if (unlikely(!vcpu->kvm->arch.enable_mmio_caching) || + if (unlikely(!vcpu->kvm->arch.enable_mmio_caching && + !kvm_gfn_shared_mask(vcpu->kvm)) ||
This chunk belongs to MMIO fault handling patch.
Quoted 18 lines
arch/x86/kvm/mmu/mmu.c +6
-1unlikely(fault->gfn > kvm_mmu_max_gfn()))return RET_PF_EMULATE ; }@@ -3454,7 +3459,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);
arch/x86/kvm/mmu/mmu.c void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) · line 4041
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c +3
-0@@ -4026,6 +4036,32 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true); } +/* +* Private page can't be release on mmu_notifier without losing page contents. +* The help, callback, from backing store is needed to allow page migration.
I hardly can understand what does ', callback, ' part mean. I guess it is used to explain exactly what is the 'help'. I am not native speaker, but the grammar doesn't look right to me.
This series is fully of this patten. It hurts readability a lot. Would you improve it?
arch/x86/kvm/mmu/mmu.c void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) · line 4043
Quoted 2 lines
arch/x86/kvm/mmu/mmu.c +2
-0+* For now, pin the page. +*/
Back to the technical point. IMHO you need to explain how page migration is supposed to work to justify why page migration needs help from backing store first. Perhaps you can briefly explain in the changelog so people can understand what part is done by backing store and which part is done by KVM.
For instance, for anonymous page, page migration is done by core-kernel. So why backing store cannot handle page migration for TDX? Is it technically unable to by design, or is it because it just hasn't implemented it yet?
If the latter, why backing store doesn't pin the page directly but requires KVM to do?
Quoted 38 lines
arch/x86/kvm/mmu/mmu.c +24
-0+static int kvm_faultin_pfn_private_mapped (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +hva_t hva =gfn_to_hva_memslot (fault->slot, fault->gfn); +struct page *page[1 ]; + + fault->map_writable = false; + fault->pfn =KVM_PFN_ERR_FAULT ; +if (hva ==KVM_HVA_ERR_RO_BAD || hva ==KVM_HVA_ERR_BAD ) +return RET_PF_CONTINUE ; + +/* TDX allows only RWX. Read-only isn't supported. */ +WARN_ON_ONCE (!fault->write); +if (pin_user_pages_fast (hva,1 ,FOLL_WRITE , page) !=1 ) +return RET_PF_INVALID ; + + fault->map_writable = true; + fault->pfn =page_to_pfn (page[0 ]); +return RET_PF_CONTINUE ; +} +static int kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -4058,6 +4094,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return RET_PF_EMULATE ; } +if (fault->is_private) +return kvm_faultin_pfn_private_mapped (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, &async, fault->write, &fault->map_writable,@@ -4110,6 +4149,17 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva); }
arch/x86/kvm/mmu/mmu.c static bool is_page_fault_stale(struct kvm_vcpu *vcpu, · line 4161
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +10
-0+void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r) +{ +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) +return ; + +if (fault->is_private) +put_page (pfn_to_page (fault->pfn)); +else +kvm_release_pfn_clean (fault->pfn); +}
What's the purpose of 'int r'? Is it even used?
arch/x86/kvm/mmu/mmu.c static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault · line 4171
Quoted 11 lines
arch/x86/kvm/mmu/mmu.c +2
-1+static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);@@ -4117,7 +4167,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultunsigned long mmu_seq;int r; - fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->gfn =gpa_to_gfn (fault->addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn);
Where is fault->is_private set? Shouldn't it be set here?
Quoted 20 lines
arch/x86/kvm/mmu/mmu.c +2
-1if (page_fault_handle_page_track (vcpu, fault))@@ -4166,7 +4216,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultread_unlock (&vcpu->kvm->mmu_lock); else write_unlock(&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);return r; }@@ -5665,6 +5715,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
arch/x86/kvm/mmu/mmu.c static void kvm_mmu_zap_all_fast(struct kvm *kvm) · line 5912
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +4
-0@@ -5855,6 +5906,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)* lead to use-after-free. */ if (is_tdp_mmu_enabled (kvm)) +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/
Please put the comment to the code which actually does the job.
Quoted 14 lines
arch/x86/kvm/mmu/mmu.c +2
-1kvm_tdp_mmu_zap_invalidated_roots (kvm); }@@ -5882,7 +5937,8 @@ static void kvm_mmu_zap_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) .may_block = false, }; - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush); +/* All private page should be zapped on memslot deletion. */ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush, true); }else { flush =slot_handle_level (kvm, slot, kvm_zap_rmapp,PG_LEVEL_4K ,KVM_MAX_HUGEPAGE_LEVEL , true);
arch/x86/kvm/mmu/mmu.c void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) · line 6049
Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +1
-1@@ -5990,7 +6046,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_leafs (kvm, i, gfn_start, - gfn_end, true, flush); + gfn_end, true, flush, false);
Add a comment to why kvm_zap_gfn_range() only zap shared?
arch/x86/kvm/mmu/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 6086
Quoted 12 lines
arch/x86/kvm/mmu/mmu.c +5
-0}if (flush)@@ -6023,6 +6079,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/
Unless I am mistaken, 'kvm_tdp_mmu_wrprot_slot() will give a WARN() if it hits for TD" is done in your later patch "KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs". Why putting comment here?
Please move this comment to that patch, and I think you can put that patch before this patch.
And this problem happens repeatedly in this series. Could you check the entire series?
arch/x86/kvm/mmu/mmu.c static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, · line 6177
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +3
-0if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -6111,6 +6172,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_sptep (sptep), kvm); +
Looks like this chunk should belong to patch "KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs".
Or you can just merge the two patches together if that make things clearer.
arch/x86/kvm/mmu/mmu.c void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm, · line 6222
Quoted 12 lines
arch/x86/kvm/mmu/mmu.c +5
-0/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -6151,6 +6215,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/
The same to above..
arch/x86/kvm/mmu/mmu.c int kvm_mmu_vendor_module_init(void) · line 6511
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +3
-0if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6437,6 +6506,9 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );
Cannot judge correctness now, but at least a comment would help here.
Quoted 19 lines
arch/x86/kvm/mmu/mmu.c +0
-0free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG @@ -164,11 +166,30 @@ static inline void kvm_mmu_alloc_private_sp(WARN_ON_ONCE (!sp->private_sp); }
arch/x86/kvm/mmu/mmu_internal.h static inline void kvm_mmu_alloc_private_sp( · line 177
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +9
-0+static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +}
What does "for_split" mean? Why do we need it?
Quoted 53 lines
arch/x86/kvm/mmu/mmu_internal.h +25
-0+static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT )free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline bool is_private_sp (struct kvm_mmu_page *sp) {@@ -194,11 +215,25 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r); +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* @@ -246,6 +281,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;/* * Whether a >4KB mapping can be created or is forbidden due to NX
arch/x86/kvm/mmu/mmu_internal.h static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 366
Quoted 4 lines
arch/x86/kvm/mmu/mmu_internal.h +1
-0@@ -327,6 +363,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa),
I guess putting this chunk and setting up fault->gfn together would be clearer?
arch/x86/kvm/mmu/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault · line 880
Quoted 13 lines
arch/x86/kvm/mmu/mmu_internal.h +0
-0.max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K,arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -877,7 +877,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);
Too painful to review. If 'r' is ever needed, please at least consider a more meaningful name.
Quoted 89 lines
arch/x86/kvm/mmu/paging_tmpl.h +0
-0return r; }arch/x86/kvm/mmu/tdp_iter.c +1
-0@@ -53,6 +53,7 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root, iter->min_level = min_level; iter->pt_path[iter->root_level -1 ] = (tdp_ptep_t )root->spt; iter->as_id =kvm_mmu_page_as_id (root); + iter->is_private =is_private_sp (root);tdp_iter_restart (iter); }arch/x86/kvm/mmu/tdp_iter.h +4
-1@@ -71,7 +71,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;@@ -94,6 +94,9 @@ struct tdp_iter {* level instead of advancing to the next entry. */ bool yielded; + +/* True if this iter is handling private KVM page fault. */ +bool is_private; };/* arch/x86/kvm/mmu/tdp_mmu.c +11
-5@@ -278,18 +278,24 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, kvm_mmu_page_as_id(_root) != _as_id) { \ } else -static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *tdp_mmu_alloc_sp( +struct kvm_vcpu *vcpu,bool private,bool is_root) {struct kvm_mmu_page *sp; sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache); +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp, is_root); +else +kvm_mmu_init_private_sp (sp,NULL ); +return sp; } -static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep, -gfn_t gfn,union kvm_mmu_page_role role) +static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep,gfn_t gfn, +union kvm_mmu_page_role role) {set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -297,7 +303,6 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }@@ -316,7 +321,8 @@ static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp,tdp_mmu_init_sp (child_sp, iter->sptep, iter->gfn, role); } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role = vcpu->arch.mmu->root_role;struct kvm *kvm = vcpu->kvm;
arch/x86/kvm/mmu/tdp_mmu.c hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) · line 340
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0@@ -330,11 +336,12 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)*/ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (root))
Is it better to have a role.private, so you don't need this change?
arch/x86/kvm/mmu/tdp_mmu.c hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) · line 344
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1goto out; } - root =tdp_mmu_alloc_sp (vcpu); + root =tdp_mmu_alloc_sp (vcpu, private, true);
With role.private, I think you can avoid 'private' argument here?
And can you check sp->role.level to determine whether it is root?
Quoted 33 lines
arch/x86/kvm/mmu/tdp_mmu.c +9
-3tdp_mmu_init_sp (root,NULL ,0 , role);refcount_set (&root->tdp_mmu_root_count,1 );@@ -344,12 +351,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -410,6 +422,7 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* * @kvm: kvm instance * @pt: the page removed from the paging structure +* @is_private: pt is private or not. * @shared: This operation may not be running under the exclusive use * of the MMU lock and the operation must synchronize with other * threads that might be modifying SPTEs.
arch/x86/kvm/mmu/tdp_mmu.c static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp, · line 439
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-1@@ -422,7 +435,8 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* this thread will be responsible for ensuring the page is freed. Hence the * early rcu_dereferences in the function. */ -static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool shared) +static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool is_private, +bool shared)
I think you can get whether the page table is private or not by ...
arch/x86/kvm/mmu/tdp_mmu.c static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp, · line 441
Quoted 2 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0{struct kvm_mmu_page *sp =sptep_to_sp (rcu_dereference (pt));
... checking is_private_sp(sp), right?
Why do you need 'is_private' argumenet?
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c +4
-2int level = sp->role.level;@@ -498,8 +512,20 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte,REMOVED_SPTE , level); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, is_private, + old_spte,REMOVED_SPTE , level, + shared); + }
arch/x86/kvm/mmu/tdp_mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 528
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+ +if (is_private &&WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp,NULL );
At least explicitly give a error message, or even a WARN().
Quoted 12 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0}call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback);@@ -510,6 +536,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure
arch/x86/kvm/mmu/tdp_mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 552
Quoted 7 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-2@@ -521,14 +548,30 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared)
I am thinking if you can just pass parent 'sp', or sptep, then you can get all roles internally, including whether it is private. I guess it is more flexible.
Quoted 90 lines
arch/x86/kvm/mmu/tdp_mmu.c +55
-8{bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn; +struct kvm_spte_change change = { + .gfn = gfn, + .level = level, + .old = { + .pfn = old_pfn, + .is_present = was_present, + .is_leaf = was_leaf, + }, + .new = { + .pfn = new_pfn, + .is_present = is_present, + .is_leaf = is_leaf, + }, + };WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -595,7 +638,7 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn);/* * Recursively handle child PTs if the change removed a subtree from @@ -604,16 +647,47 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* pages are kernel allocations and should never be migrated. */ if (was_present && !was_leaf && - (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) -handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), shared); + (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) { +WARN_ON (private_spte != +is_private_sptep (spte_to_child_pt (old_spte, level))); +handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), + private_spte, shared); + } + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +* +* This is after handling lower page table by above +* handle_remove_tdp_mmu_page(). S-EPT requires to remove S-EPT tables +* after removing childrens. +*/ +if (private_spte && +/* Ignore change of software only bits. e.g. host_writable */ + (was_leaf != is_leaf || was_present != is_present || pfn_changed)) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + change.sept_page = sept_page; + +static_call (kvm_x86_handle_changed_private_spte)(kvm, &change); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);
arch/x86/kvm/mmu/tdp_mmu.c static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm, · line 718
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -640,6 +714,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = iter->is_private && !is_removed_spte(new_spte); +u64 tmp_spte = freeze_spte ?REMOVED_SPTE : new_spte;
Perhaps I am missing something. Could you add comments to explain the logic?
Quoted 100 lines
arch/x86/kvm/mmu/tdp_mmu.c +23
-11u64 *sptep =rcu_dereference (iter->sptep);u64 old_spte;@@ -657,7 +733,7 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,* Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs and * does not hold the mmu_lock. */ - old_spte =cmpxchg64 (sptep, iter->old_spte, new_spte); + old_spte =cmpxchg64 (sptep, iter->old_spte, tmp_spte);if (old_spte != iter->old_spte) {/* * The page table entry was modified by a different logical @@ -669,10 +745,14 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return -EBUSY ; } -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte ( + kvm, iter->as_id, iter->gfn, iter->is_private, + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +__kvm_tdp_mmu_write_spte (sptep, new_spte); +return 0 ; }@@ -734,13 +814,15 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* unless performing certain dirty logging operations. * Leaving record_dirty_log unset in that case prevents page * writes from being double counted. +* @is_private: The fault is private. * * Returns the old SPTE value, which _may_ be different than @old_spte if the * SPTE had voldatile bits. */ static u64 __tdp_mmu_set_spte (struct kvm *kvm,int as_id,tdp_ptep_t sptep, -u64 old_spte,u64 new_spte,gfn_t gfn,int level, -bool record_acc_track,bool record_dirty_log) +u64 old_spte,u64 new_spte,gfn_t gfn,int level, +bool record_acc_track,bool record_dirty_log, +bool is_private) {lockdep_assert_held_write (&kvm->mmu_lock);@@ -755,7 +837,8 @@ static u64 __tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte, new_spte, level); -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, false); +__handle_changed_spte (kvm, as_id, gfn, is_private, + old_spte, new_spte, level, false);if (record_acc_track)handle_changed_spte_acc_track (old_spte, new_spte, level);@@ -774,7 +857,8 @@ static inline void _tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, iter->old_spte =__tdp_mmu_set_spte (kvm, iter->as_id, iter->sptep, iter->old_spte, new_spte, iter->gfn, iter->level, - record_acc_track, record_dirty_log); + record_acc_track, record_dirty_log, + iter->is_private); } static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,@@ -807,8 +891,11 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, to_shadow_page(_mmu->root.hpa), _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + to_shadow_page((_private) ? _mmu->private_root_hpa : \ + _mmu->root.hpa), \ + _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -945,7 +1032,7 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)__tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 , - true, true); + true, true,is_private_sp (sp));return true; }@@ -961,13 +1048,21 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)* operation can cause a soft lockup. */ static bool tdp_mmu_zap_leafs (struct kvm *kvm,struct kvm_mmu_page *root, -gfn_t start,gfn_t end,bool can_yield,bool flush) +gfn_t start,gfn_t end,bool can_yield,bool flush, +bool drop_private) {struct tdp_iter iter;
arch/x86/kvm/mmu/tdp_mmu.c bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) · line 1065
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0end =min (end,tdp_mmu_max_gfn_exclusive ());lockdep_assert_held_write (&kvm->mmu_lock); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +WARN_ON_ONCE (!is_private_sp (root) && drop_private); + start =kvm_gfn_for_root (kvm, root, start); + end =kvm_gfn_for_root (kvm, root, end);
So for the GFN given to the iterator, it always doesn't have shared bit, right?
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0rcu_read_lock ();@@ -1002,12 +1097,13 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */
arch/x86/kvm/mmu/tdp_mmu.c static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, · line 1106
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-2bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start,gfn_t end, -bool can_yield,bool flush) +bool can_yield,bool flush,bool drop_private) {struct kvm_mmu_page *root;for_each_tdp_mmu_root_yield_safe (kvm, root, as_id) - flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush); + flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush, + drop_private &&is_private_sp (root));
if (is_private_sp(root) && drop_private) continue; flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, flush);
In this case, I guess you can remove 'is_private' in tdp_mmu_zap_leafs()?
Quoted 17 lines
arch/x86/kvm/mmu/tdp_mmu.c +6
-0return flush; }@@ -1067,6 +1163,12 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)lockdep_assert_held_write (&kvm->mmu_lock);list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (!root->role.invalid && !WARN_ON_ONCE (!kvm_tdp_mmu_get_root (root))) { root->role.invalid = true;
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1193
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -1087,14 +1189,22 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ; +gfn_t gfn_unalias = iter->gfn & ~kvm_gfn_shared_mask (vcpu->kvm);
Here looks the iter->gfn still contains the shared bits. It is not consistent with above.
Can you put some words into the changelog explaining exactly what GFN will you put to iterator?
Or can you even split out this part as a separate patch?
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1202
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +6
-1WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (kvm_gfn_shared_mask (vcpu->kvm) && !fault->is_private) + pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, gfn_unalias, pte_access);
This part belong to MMIO fault handing patch.
Quoted 156 lines
arch/x86/kvm/mmu/tdp_mmu.c +48
-13else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, + gfn_unalias, fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1167,8 +1277,7 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,return 0 ; } -static int tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +static int tdp_mmu_populate_nonleaf (struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) {struct kvm_mmu_page *sp;int ret;@@ -1176,7 +1285,7 @@ static int tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =tdp_mmu_alloc_sp (vcpu); + sp =tdp_mmu_alloc_sp (vcpu, iter->is_private, false);tdp_mmu_init_child_sp (sp, iter); ret =tdp_mmu_link_sp (vcpu->kvm, iter, sp, account_nx, true);@@ -1193,6 +1302,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private = fault->is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1201,7 +1312,16 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);@@ -1217,6 +1337,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_sptep (iter.sptep)); +/* * The iter must explicitly re-read the spte here @@ -1258,11 +1384,13 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return ret; } +/* Used by mmu notifier via kvm_unmap_gfn_range() */ bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush) +bool flush,bool drop_private) {return kvm_tdp_mmu_zap_leafs (kvm, range->slot->as_id, range->start, - range->end, range->may_block, flush); + range->end, range->may_block, flush, + drop_private); } typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,@@ -1445,7 +1573,8 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (gfp_t gfp) +static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split ( +gfp_t gfp,bool is_private) {struct kvm_mmu_page *sp;@@ -1456,6 +1585,12 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)return NULL ; sp->spt = (void *)__get_free_page (gfp); +if (is_private) { +if (kvm_alloc_private_sp_for_split (sp, gfp)) { +free_page ((unsigned long )sp->spt); + sp->spt =NULL ; + } + }if (!sp->spt) {kmem_cache_free (mmu_page_header_cache, sp);return NULL ;@@ -1469,6 +1604,11 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, bool shared) {struct kvm_mmu_page *sp; +bool is_private = iter->is_private; + +/* TODO: For now large page isn't supported for private SPTE. */ +WARN_ON (is_private); +WARN_ON (iter->is_private !=is_private_sptep (iter->sptep));/* * Since we are allocating while under the MMU lock we have to be @@ -1479,7 +1619,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,* If this allocation fails we drop the lock and retry with reclaim * allowed. */ - sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT); + sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT, is_private);if (sp)return sp;@@ -1491,7 +1631,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,write_unlock (&kvm->mmu_lock); iter->yielded = true; - sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT ); + sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT , is_private);if (shared)read_lock (&kvm->mmu_lock);@@ -1907,10 +2047,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gpa (vcpu->kvm, addr); *root_level = vcpu->arch.mmu->root_role.level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }
arch/x86/kvm/mmu/tdp_mmu.c u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr, · line 2085
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-1@@ -1937,7 +2081,10 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +/* fast page fault for private GPA isn't supported. */ +WARN_ON_ONCE (kvm_is_private_gpa (vcpu->kvm, addr));
Shouldn't this chunk belong to patch:
[PATCH v7 038/102] KVM: x86/mmu: Disallow fast page fault on private GPA
?
Quoted 49 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0+ +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; }arch/x86/kvm/mmu/tdp_mmu.h +4
-3@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {@@ -16,7 +16,8 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool drop_private);bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp);void kvm_tdp_mmu_zap_all (struct kvm *kvm);void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm);@@ -25,7 +26,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush); +bool flush,bool drop_private);bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range);virt/kvm/kvm_main.c +1
-0@@ -196,6 +196,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Isaku Yamahata
Acked-byRe: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 38 lines · Kai Huang
Quoted 38 lines
(quoted diff) +24
-0+static int kvm_faultin_pfn_private_mapped(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + hva_t hva = gfn_to_hva_memslot(fault->slot, fault->gfn); + struct page *page[1]; + + fault->map_writable = false; + fault->pfn = KVM_PFN_ERR_FAULT; + if (hva == KVM_HVA_ERR_RO_BAD || hva == KVM_HVA_ERR_BAD) + return RET_PF_CONTINUE; + + /* TDX allows only RWX. Read-only isn't supported. */ + WARN_ON_ONCE(!fault->write); + if (pin_user_pages_fast(hva, 1, FOLL_WRITE, page) != 1) + return RET_PF_INVALID; + + fault->map_writable = true; + fault->pfn = page_to_pfn(page[0]); + return RET_PF_CONTINUE; +} + static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { struct kvm_memory_slot *slot = fault->slot;@@ -4058,6 +4094,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) return RET_PF_EMULATE; } + if (fault->is_private) + return kvm_faultin_pfn_private_mapped(vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, &async, fault->write, &fault->map_writable,@@ -4110,6 +4149,17 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu, mmu_notifier_retry_hva(vcpu->kvm, mmu_seq, fault->hva); }
Quoted 12 lines
Review static bool is_page_fault_stale(struct kvm_vcpu *vcpu, · line 4161
Quoted 10 lines
(quoted diff) +10
-0+void kvm_mmu_release_fault(struct kvm *kvm, struct kvm_page_fault *fault, int r) +{ + if (is_error_noslot_pfn(fault->pfn) || kvm_is_reserved_pfn(fault->pfn)) + return; + + if (fault->is_private) + put_page(pfn_to_page(fault->pfn)); + else + kvm_release_pfn_clean(fault->pfn); +}What's the purpose of 'int r'? Is it even used?
removed r because r is unused.
Quoted 7 lines
Quoted 7 lines
(quoted diff) +0
-0static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { bool is_tdp_mmu_fault = is_tdp_mmu(vcpu->arch.mmu);@@ -4117,7 +4167,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault unsigned long mmu_seq; int r;
Quoted 5 lines
Review static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault · line 4171
Quoted 3 lines
(quoted diff) +1
-1- fault->gfn = fault->addr >> PAGE_SHIFT; + fault->gfn = gpa_to_gfn(fault->addr) & ~kvm_gfn_shared_mask(vcpu->kvm); fault->slot = kvm_vcpu_gfn_to_memslot(vcpu, fault->gfn);Where is
fault->is_privateset? Shouldn't it be set here?
kvm_mmu_do_page_fault() does it and no because is_private is constant. is_private is input. On the other hand gfn and slot is working variables.
Quoted 17 lines
Review void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 6086
Quoted 12 lines
(quoted diff) +0
-0}(quoted diff) +5
-0if (flush)@@ -6023,6 +6079,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, write_unlock(&kvm->mmu_lock); } + /* + * For now this can only happen for non-TD VM, because TD private + * mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() + * will give a WARN() if it hits for TD. + */Unless I am mistaken, '
kvm_tdp_mmu_wrprot_slot()will give aWARN()if it hits for TD" is done in your later patch "KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs". Why putting comment here?
Quoted 5 lines
Please move this comment to that patch, and I think you can put that patch before this patch.
And this problem happens repeatedly in this series. Could you check the entire series?
Split out those stuff into a patch.
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG @@ -164,11 +166,30 @@ static inline void kvm_mmu_alloc_private_sp(WARN_ON_ONCE (!sp->private_sp); }
Quoted 11 lines
arch/x86/kvm/mmu/mmu_internal.h static inline void kvm_mmu_alloc_private_sp( · line 177
Quoted 9 lines
arch/x86/kvm/mmu/mmu_internal.h +9
-0+static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +}What does "
for_split" mean? Why do we need it?
Split large page into smaller sized one. Followed tdp_mmu_alloc_sp_for_split(). We can defer to introduce this function until large page support.
Quoted 50 lines
Quoted 50 lines
arch/x86/kvm/mmu/mmu_internal.h +25
-0+static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT )free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline bool is_private_sp (struct kvm_mmu_page *sp) {@@ -194,11 +215,25 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r); +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* @@ -246,6 +281,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;
Quoted 9 lines
arch/x86/kvm/mmu/mmu_internal.h static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 366
Quoted 7 lines
arch/x86/kvm/mmu/mmu_internal.h +1
-0/* * Whether a >4KB mapping can be created or is forbidden due to NX @@ -327,6 +363,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa),I guess putting this chunk and setting up
fault->gfntogether would be clearer?
is_private is input for kvm page fault. gfn is working variable to resolve kvm page fault.
Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-2static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared){
arch/x86/kvm/mmu/mmu_internal.h +2
-2-__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm, · line 718
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -640,6 +714,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = iter->is_private && !is_removed_spte(new_spte); +u64 tmp_spte = freeze_spte ?REMOVED_SPTE : new_spte;Perhaps I am missing something. Could you add comments to explain the logic?
Add a comment.
+ /*
+ * For conventional page table, the update flow is + * - update STPE with atomic operation + * - hanlde changed SPTE. __handle_changed_spte() + * NOTE: __handle_changed_spte() (and functions) must be safe against + * concurrent update. It is an exception to zap SPTE. See + * tdp_mmu_zap_spte_atomic(). + * + * For private page table, callbacks are needed to propagate SPTE + * change into the protected page table. In order to atomically update + * both the SPTE and the protected page tables with callbacks, utilize + * freezing SPTE. + * - Freeze the SPTE. Set entry to REMOVED_SPTE. + * - Trigger callbacks for protected page tables. __handle_changed_spte() + * - Unfreeze the SPTE. Set the entry to new_spte. + */
Quoted 23 lines
Quoted 14 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0@@ -1067,6 +1163,12 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)lockdep_assert_held_write (&kvm->mmu_lock);list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (!root->role.invalid && !WARN_ON_ONCE (!kvm_tdp_mmu_get_root (root))) { root->role.invalid = true;arch/x86/kvm/mmu/mmu_internal.h static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1193
Quoted 5 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -1087,14 +1189,22 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ; +gfn_t gfn_unalias = iter->gfn & ~kvm_gfn_shared_mask (vcpu->kvm);Here looks the
iter->gfnstill contains the shared bits. It is not consistent with above.
Quoted 4 lines
Can you put some words into the changelog explaining exactly what GFN will you put to iterator?
Or can you even split out this part as a separate patch?
I think you meant the above is zap_leafs function. It zaps GPA range module alias (module shared bit).
This function is to resolve kvm page fault. It means gpa includes shared bit.
here is the updated patch.
From ae3cee62e53a877bef04813e6ae8d710b4a9128a Mon Sep 17 00:00:00 2001
Message-Id: <ae3cee62e53a877bef04813e6ae8d710b4a9128a.1658878587.git.isaku.yamahata@intel.com>
In-Reply-To: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658878587.git.isaku.yamahata@intel.com>
References: <3941849bf08a55cfbbe69b222f0fd0dac7c5ee53.1658878587.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Thu, 14 Jul 2022 15:11:24 -0700
Subject: [PATCH 048/292] KVM: x86/tdp_mmu: Support TDX private mapping for TDP
MMU
Allocate protected page table for private page table, and add hooks to operate on protected page table. This patch adds allocation/free of protected page tables and hooks. When calling hooks to update SPTE entry, freeze the entry, call hooks and unfree the entry to allow concurrent updates on page tables. Which is the advantage of TDP MMU. As kvm_gfn_shared_mask() returns false always, those hooks aren't called yet with this patch.
When the faulting GPA is private, the KVM fault is called private. When resolving private KVM, allocate protected page table and call hooks to operate on protected page table. On the change of the private PTE entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to protected page table. The following depicts the relationship.
private KVM page fault |
| |
V |
private GPA | CPU protected EPTP
| | |
V | V
private PT root | protected PT root
| | |
V | V
private PT --hook to propagate-->protected PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For protected page table, hooks are called to update protected page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K protected SPTE => error vcpu 1: TDX SEAMCALL to update 2M protected SPTE
To avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update protected SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add is_private member to kvm_page_fault to indicate the fault is private. Also is_private member to struct tdp_inter to propagate it.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 15 +- arch/x86/kvm/mmu/mmu_internal.h | 35 +++++ arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 215 ++++++++++++++++++++++++----- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- virt/kvm/kvm_main.c | 1 + 8 files changed, 254 insertions(+), 38 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +20 -0
@@ -467,6 +467,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;
@@ -1462,6 +1463,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;
@@ -1574,6 +1589,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +11 -2
@@ -3465,7 +3465,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);
@@ -4128,7 +4133,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultunsigned long mmu_seq;int r; - fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->gfn =gpa_to_gfn (fault->addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))
@@ -5669,6 +5674,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -6467,6 +6473,9 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);
arch/x86/kvm/mmu/mmu_internal.h +35 -0
@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG
@@ -163,11 +165,30 @@ static inline void kvm_mmu_alloc_private_sp( } } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp)free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) {
@@ -183,9 +204,21 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp)
@@ -235,6 +268,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;/* * Whether a >4KB mapping can be created or is forbidden due to NX
@@ -316,6 +350,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K,
arch/x86/kvm/mmu/tdp_iter.h +1 -1
@@ -71,7 +71,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
arch/x86/kvm/mmu/tdp_mmu.c +182 -33
386 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +1 -1
@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {
virt/kvm/kvm_main.c +1 -0
@@ -196,6 +196,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 7 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]Quoted 223 lines · On Mon, Jun 27, 2022 at 02:53:38PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate mirrored private page table for private page table, and add hooks to operate on mirrored private page table. This patch adds only hooks. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet.Because private guest page is protected, page copy with
mmu_notifierto migrate page doesn't work. Callback from backing store is needed.When the faulting GPA is private, the KVM fault is also called private. When resolving private KVM, allocate mirrored private page table and call hooks to operate on mirrored private page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to mirrored private page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | | | V | KVM private PT root | CPU private PT root | | | V | V private PT ---hook to mirror--->mirrored private PT | | | \--------------------+------\ | | | | | V V | private guest page | | non-encrypted memory | encrypted memory |PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For mirrored private page table, hooks are called to update mirrored private page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K mirrored private SPTE => error vcpu 1: TDX SEAMCALL to update 2M mirrored private SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update mirrored private SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add
is_privatemember tokvm_page_faultto indicate the fault is private. Alsois_privatemember to structtdp_interto propagate it.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 86 +++++++++- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_iter.c | 1 + arch/x86/kvm/mmu/tdp_iter.h | 5 +- arch/x86/kvm/mmu/tdp_mmu.c | 247 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 7 +- virt/kvm/kvm_main.c | 1 + 10 files changed, 346 insertions(+), 62 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +20
-0@@ -440,6 +440,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1435,6 +1436,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;@@ -1547,6 +1562,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +42
-3@@ -1577,7 +1577,11 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) flush =kvm_handle_gfn_range (kvm, range, kvm_unmap_rmapp);if (is_tdp_mmu_enabled (kvm)) - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush); +/* +* private page needs to be kept and handle page migration +* on next EPT violation. +*/ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush, false);return flush; }@@ -3082,7 +3086,8 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* SPTE value without #VE suppress bit cleared * (kvm->arch.shadow_mmio_value = 0). */ -if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching && + !kvm_gfn_shared_mask (vcpu->kvm)) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }@@ -3454,7 +3459,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -4026,6 +4036,32 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true); } +/* +* Private page can't be release on mmu_notifier without losing page contents. +* The help, callback, from backing store is needed to allow page migration. +* For now, pin the page. +*/ +static int kvm_faultin_pfn_private_mapped (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +hva_t hva =gfn_to_hva_memslot (fault->slot, fault->gfn); +struct page *page[1 ]; + + fault->map_writable = false; + fault->pfn =KVM_PFN_ERR_FAULT ; +if (hva ==KVM_HVA_ERR_RO_BAD || hva ==KVM_HVA_ERR_BAD ) +return RET_PF_CONTINUE ; + +/* TDX allows only RWX. Read-only isn't supported. */ +WARN_ON_ONCE (!fault->write); +if (pin_user_pages_fast (hva,1 ,FOLL_WRITE , page) !=1 ) +return RET_PF_INVALID ; + + fault->map_writable = true; + fault->pfn =page_to_pfn (page[0 ]); +return RET_PF_CONTINUE ; +} +static int kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -4058,6 +4094,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return RET_PF_EMULATE ; } +if (fault->is_private) +return kvm_faultin_pfn_private_mapped (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, &async, fault->write, &fault->map_writable,
arch/x86/kvm/mmu/mmu.c static bool is_page_fault_stale(struct kvm_vcpu *vcpu, · line 4158
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7
-0@@ -4110,6 +4149,17 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva); } +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r) +{ +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) +return ; + +if (fault->is_private) +put_page (pfn_to_page (fault->pfn));
The pin_user_pages_fast() is used above which has FOLL_PIN set internal, so should we use unpin_user_page() here ? The FOLL_PIN means the unpin should be done by unpin_user_page() but not put_page, please see /Documentation/core-api/pin_user_pages.rst and comments on FOLL_PIN;
Quoted 850 lines
arch/x86/kvm/mmu/mmu.c +30
-4+else +kvm_release_pfn_clean (fault->pfn); +} +static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);@@ -4117,7 +4167,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultunsigned long mmu_seq;int r; - fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->gfn =gpa_to_gfn (fault->addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))@@ -4166,7 +4216,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultread_unlock (&vcpu->kvm->mmu_lock); else write_unlock(&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);return r; }@@ -5665,6 +5715,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -5855,6 +5906,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)* lead to use-after-free. */ if (is_tdp_mmu_enabled (kvm)) +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ kvm_tdp_mmu_zap_invalidated_roots (kvm); }@@ -5882,7 +5937,8 @@ static void kvm_mmu_zap_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) .may_block = false, }; - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush); +/* All private page should be zapped on memslot deletion. */ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush, true); }else { flush =slot_handle_level (kvm, slot, kvm_zap_rmapp,PG_LEVEL_4K ,KVM_MAX_HUGEPAGE_LEVEL , true);@@ -5990,7 +6046,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_leafs (kvm, i, gfn_start, - gfn_end, true, flush); + gfn_end, true, flush, false); }if (flush)@@ -6023,6 +6079,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -6111,6 +6172,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_sptep (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -6151,6 +6215,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6437,6 +6506,9 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/mmu_internal.h +37
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG @@ -164,11 +166,30 @@ static inline void kvm_mmu_alloc_private_sp(WARN_ON_ONCE (!sp->private_sp); } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT )free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline bool is_private_sp (struct kvm_mmu_page *sp) {@@ -194,11 +215,25 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r); +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* @@ -246,6 +281,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;/* * Whether a >4KB mapping can be created or is forbidden due to NX @@ -327,6 +363,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K,arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -877,7 +877,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);return r; }arch/x86/kvm/mmu/tdp_iter.c +1
-0@@ -53,6 +53,7 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root, iter->min_level = min_level; iter->pt_path[iter->root_level -1 ] = (tdp_ptep_t )root->spt; iter->as_id =kvm_mmu_page_as_id (root); + iter->is_private =is_private_sp (root);tdp_iter_restart (iter); }arch/x86/kvm/mmu/tdp_iter.h +4
-1@@ -71,7 +71,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;@@ -94,6 +94,9 @@ struct tdp_iter {* level instead of advancing to the next entry. */ bool yielded; + +/* True if this iter is handling private KVM page fault. */ +bool is_private; };/* arch/x86/kvm/mmu/tdp_mmu.c +197
-50@@ -278,18 +278,24 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, kvm_mmu_page_as_id(_root) != _as_id) { \ } else -static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *tdp_mmu_alloc_sp( +struct kvm_vcpu *vcpu,bool private,bool is_root) {struct kvm_mmu_page *sp; sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache); +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp, is_root); +else +kvm_mmu_init_private_sp (sp,NULL ); +return sp; } -static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep, -gfn_t gfn,union kvm_mmu_page_role role) +static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep,gfn_t gfn, +union kvm_mmu_page_role role) {set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -297,7 +303,6 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }@@ -316,7 +321,8 @@ static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp,tdp_mmu_init_sp (child_sp, iter->sptep, iter->gfn, role); } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role = vcpu->arch.mmu->root_role;struct kvm *kvm = vcpu->kvm;@@ -330,11 +336,12 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)*/ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (root)) goto out; } - root =tdp_mmu_alloc_sp (vcpu); + root =tdp_mmu_alloc_sp (vcpu, private, true);tdp_mmu_init_sp (root,NULL ,0 , role);refcount_set (&root->tdp_mmu_root_count,1 );@@ -344,12 +351,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -410,6 +422,7 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* * @kvm: kvm instance * @pt: the page removed from the paging structure +* @is_private: pt is private or not. * @shared: This operation may not be running under the exclusive use * of the MMU lock and the operation must synchronize with other * threads that might be modifying SPTEs. @@ -422,7 +435,8 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* this thread will be responsible for ensuring the page is freed. Hence the * early rcu_dereferences in the function. */ -static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool shared) +static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool is_private, +bool shared) {struct kvm_mmu_page *sp =sptep_to_sp (rcu_dereference (pt));int level = sp->role.level;@@ -498,8 +512,20 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte,REMOVED_SPTE , level); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, is_private, + old_spte,REMOVED_SPTE , level, + shared); + } + +if (is_private &&WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback);@@ -510,6 +536,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure @@ -521,14 +548,30 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared) {bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn; +struct kvm_spte_change change = { + .gfn = gfn, + .level = level, + .old = { + .pfn = old_pfn, + .is_present = was_present, + .is_leaf = was_leaf, + }, + .new = { + .pfn = new_pfn, + .is_present = is_present, + .is_leaf = is_leaf, + }, + };WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -595,7 +638,7 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn);/* * Recursively handle child PTs if the change removed a subtree from @@ -604,16 +647,47 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* pages are kernel allocations and should never be migrated. */ if (was_present && !was_leaf && - (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) -handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), shared); + (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) { +WARN_ON (private_spte != +is_private_sptep (spte_to_child_pt (old_spte, level))); +handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), + private_spte, shared); + } + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +* +* This is after handling lower page table by above +* handle_remove_tdp_mmu_page(). S-EPT requires to remove S-EPT tables +* after removing childrens. +*/ +if (private_spte && +/* Ignore change of software only bits. e.g. host_writable */ + (was_leaf != is_leaf || was_present != is_present || pfn_changed)) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + change.sept_page = sept_page; + +static_call (kvm_x86_handle_changed_private_spte)(kvm, &change); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);@@ -640,6 +714,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = iter->is_private && !is_removed_spte(new_spte); +u64 tmp_spte = freeze_spte ?REMOVED_SPTE : new_spte;u64 *sptep =rcu_dereference (iter->sptep);u64 old_spte;@@ -657,7 +733,7 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,* Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs and * does not hold the mmu_lock. */ - old_spte =cmpxchg64 (sptep, iter->old_spte, new_spte); + old_spte =cmpxchg64 (sptep, iter->old_spte, tmp_spte);if (old_spte != iter->old_spte) {/* * The page table entry was modified by a different logical @@ -669,10 +745,14 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return -EBUSY ; } -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte ( + kvm, iter->as_id, iter->gfn, iter->is_private, + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +__kvm_tdp_mmu_write_spte (sptep, new_spte); +return 0 ; }@@ -734,13 +814,15 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* unless performing certain dirty logging operations. * Leaving record_dirty_log unset in that case prevents page * writes from being double counted. +* @is_private: The fault is private. * * Returns the old SPTE value, which _may_ be different than @old_spte if the * SPTE had voldatile bits. */ static u64 __tdp_mmu_set_spte (struct kvm *kvm,int as_id,tdp_ptep_t sptep, -u64 old_spte,u64 new_spte,gfn_t gfn,int level, -bool record_acc_track,bool record_dirty_log) +u64 old_spte,u64 new_spte,gfn_t gfn,int level, +bool record_acc_track,bool record_dirty_log, +bool is_private) {lockdep_assert_held_write (&kvm->mmu_lock);@@ -755,7 +837,8 @@ static u64 __tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte, new_spte, level); -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, false); +__handle_changed_spte (kvm, as_id, gfn, is_private, + old_spte, new_spte, level, false);if (record_acc_track)handle_changed_spte_acc_track (old_spte, new_spte, level);@@ -774,7 +857,8 @@ static inline void _tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, iter->old_spte =__tdp_mmu_set_spte (kvm, iter->as_id, iter->sptep, iter->old_spte, new_spte, iter->gfn, iter->level, - record_acc_track, record_dirty_log); + record_acc_track, record_dirty_log, + iter->is_private); } static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,@@ -807,8 +891,11 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, to_shadow_page(_mmu->root.hpa), _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + to_shadow_page((_private) ? _mmu->private_root_hpa : \ + _mmu->root.hpa), \ + _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -945,7 +1032,7 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)__tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 , - true, true); + true, true,is_private_sp (sp));return true; }@@ -961,13 +1048,21 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)* operation can cause a soft lockup. */ static bool tdp_mmu_zap_leafs (struct kvm *kvm,struct kvm_mmu_page *root, -gfn_t start,gfn_t end,bool can_yield,bool flush) +gfn_t start,gfn_t end,bool can_yield,bool flush, +bool drop_private) {struct tdp_iter iter; end =min (end,tdp_mmu_max_gfn_exclusive ());lockdep_assert_held_write (&kvm->mmu_lock); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +WARN_ON_ONCE (!is_private_sp (root) && drop_private); + start =kvm_gfn_for_root (kvm, root, start); + end =kvm_gfn_for_root (kvm, root, end);rcu_read_lock ();@@ -1002,12 +1097,13 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */ bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start,gfn_t end, -bool can_yield,bool flush) +bool can_yield,bool flush,bool drop_private) {struct kvm_mmu_page *root;for_each_tdp_mmu_root_yield_safe (kvm, root, as_id) - flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush); + flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush, + drop_private &&is_private_sp (root));return flush; }@@ -1067,6 +1163,12 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)lockdep_assert_held_write (&kvm->mmu_lock);list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (!root->role.invalid && !WARN_ON_ONCE (!kvm_tdp_mmu_get_root (root))) { root->role.invalid = true;@@ -1087,14 +1189,22 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ; +gfn_t gfn_unalias = iter->gfn & ~kvm_gfn_shared_mask (vcpu->kvm);WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (kvm_gfn_shared_mask (vcpu->kvm) && !fault->is_private) + pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, gfn_unalias, pte_access);else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, + gfn_unalias, fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1167,8 +1277,7 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,return 0 ; } -static int tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +static int tdp_mmu_populate_nonleaf (struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) {struct kvm_mmu_page *sp;int ret;@@ -1176,7 +1285,7 @@ static int tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =tdp_mmu_alloc_sp (vcpu); + sp =tdp_mmu_alloc_sp (vcpu, iter->is_private, false);tdp_mmu_init_child_sp (sp, iter); ret =tdp_mmu_link_sp (vcpu->kvm, iter, sp, account_nx, true);@@ -1193,6 +1302,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private = fault->is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1201,7 +1312,16 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);@@ -1217,6 +1337,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_sptep (iter.sptep)); +/* * The iter must explicitly re-read the spte here @@ -1258,11 +1384,13 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return ret; } +/* Used by mmu notifier via kvm_unmap_gfn_range() */ bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush) +bool flush,bool drop_private) {return kvm_tdp_mmu_zap_leafs (kvm, range->slot->as_id, range->start, - range->end, range->may_block, flush); + range->end, range->may_block, flush, + drop_private); } typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,@@ -1445,7 +1573,8 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (gfp_t gfp) +static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split ( +gfp_t gfp,bool is_private) {struct kvm_mmu_page *sp;@@ -1456,6 +1585,12 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)return NULL ; sp->spt = (void *)__get_free_page (gfp); +if (is_private) { +if (kvm_alloc_private_sp_for_split (sp, gfp)) { +free_page ((unsigned long )sp->spt); + sp->spt =NULL ; + } + }if (!sp->spt) {kmem_cache_free (mmu_page_header_cache, sp);return NULL ;@@ -1469,6 +1604,11 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, bool shared) {struct kvm_mmu_page *sp; +bool is_private = iter->is_private; + +/* TODO: For now large page isn't supported for private SPTE. */ +WARN_ON (is_private); +WARN_ON (iter->is_private !=is_private_sptep (iter->sptep));/* * Since we are allocating while under the MMU lock we have to be @@ -1479,7 +1619,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,* If this allocation fails we drop the lock and retry with reclaim * allowed. */ - sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT); + sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT, is_private);if (sp)return sp;@@ -1491,7 +1631,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,write_unlock (&kvm->mmu_lock); iter->yielded = true; - sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT ); + sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT , is_private);if (shared)read_lock (&kvm->mmu_lock);@@ -1907,10 +2047,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gpa (vcpu->kvm, addr); *root_level = vcpu->arch.mmu->root_role.level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1937,7 +2081,10 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +/* fast page fault for private GPA isn't supported. */ +WARN_ON_ONCE (kvm_is_private_gpa (vcpu->kvm, addr)); + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; }arch/x86/kvm/mmu/tdp_mmu.h +4
-3@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {@@ -16,7 +16,8 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool drop_private);bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp);void kvm_tdp_mmu_zap_all (struct kvm *kvm);void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm);@@ -25,7 +26,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush); +bool flush,bool drop_private);bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range);virt/kvm/kvm_main.c +1
-1@@ -196,6 +196,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put() -- 2.25.1
Re: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 227 lines · Yuan Yao
Quoted 226 lines · On Mon, Jun 27, 2022 at 02:53:38PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate mirrored private page table for private page table, and add hooks to operate on mirrored private page table. This patch adds only hooks. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet.Because private guest page is protected, page copy with
mmu_notifierto migrate page doesn't work. Callback from backing store is needed.When the faulting GPA is private, the KVM fault is also called private. When resolving private KVM, allocate mirrored private page table and call hooks to operate on mirrored private page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to mirrored private page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | | | V | KVM private PT root | CPU private PT root | | | V | V private PT ---hook to mirror--->mirrored private PT | | | \--------------------+------\ | | | | | V V | private guest page | | non-encrypted memory | encrypted memory |PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For mirrored private page table, hooks are called to update mirrored private page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K mirrored private SPTE => error vcpu 1: TDX SEAMCALL to update 2M mirrored private SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update mirrored private SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add
is_privatemember tokvm_page_faultto indicate the fault is private. Alsois_privatemember to structtdp_interto propagate it.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 86 +++++++++- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_iter.c | 1 + arch/x86/kvm/mmu/tdp_iter.h | 5 +- arch/x86/kvm/mmu/tdp_mmu.c | 247 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 7 +- virt/kvm/kvm_main.c | 1 + 10 files changed, 346 insertions(+), 62 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +20
-0@@ -440,6 +440,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1435,6 +1436,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;@@ -1547,6 +1562,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +42
-3@@ -1577,7 +1577,11 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) flush =kvm_handle_gfn_range (kvm, range, kvm_unmap_rmapp);if (is_tdp_mmu_enabled (kvm)) - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush); +/* +* private page needs to be kept and handle page migration +* on next EPT violation. +*/ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush, false);return flush; }@@ -3082,7 +3086,8 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* SPTE value without #VE suppress bit cleared * (kvm->arch.shadow_mmio_value = 0). */ -if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching && + !kvm_gfn_shared_mask (vcpu->kvm)) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }@@ -3454,7 +3459,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -4026,6 +4036,32 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true); } +/* +* Private page can't be release on mmu_notifier without losing page contents. +* The help, callback, from backing store is needed to allow page migration. +* For now, pin the page. +*/ +static int kvm_faultin_pfn_private_mapped (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +hva_t hva =gfn_to_hva_memslot (fault->slot, fault->gfn); +struct page *page[1 ]; + + fault->map_writable = false; + fault->pfn =KVM_PFN_ERR_FAULT ; +if (hva ==KVM_HVA_ERR_RO_BAD || hva ==KVM_HVA_ERR_BAD ) +return RET_PF_CONTINUE ; + +/* TDX allows only RWX. Read-only isn't supported. */ +WARN_ON_ONCE (!fault->write); +if (pin_user_pages_fast (hva,1 ,FOLL_WRITE , page) !=1 ) +return RET_PF_INVALID ; + + fault->map_writable = true; + fault->pfn =page_to_pfn (page[0 ]); +return RET_PF_CONTINUE ; +} +static int kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -4058,6 +4094,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return RET_PF_EMULATE ; } +if (fault->is_private) +return kvm_faultin_pfn_private_mapped (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, &async, fault->write, &fault->map_writable,@@ -4110,6 +4149,17 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva); }
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c static bool is_page_fault_stale(struct kvm_vcpu *vcpu, · line 4158
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +7
-0+void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r) +{ +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) +return ; + +if (fault->is_private) +put_page (pfn_to_page (fault->pfn));The
pin_user_pages_fast()is used above which hasFOLL_PINset internal, so should we useunpin_user_page()here ? TheFOLL_PINmeans the unpin should be done byunpin_user_page()but notput_page, please see /Documentation/core-api/pin_user_pages.rst and comments onFOLL_PIN;
To align with large page support, I'll make it to use get_user_pages_fast() and put_page().
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]Quoted 431 lines · On Mon, Jun 27, 2022 at 02:53:38PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate mirrored private page table for private page table, and add hooks to operate on mirrored private page table. This patch adds only hooks. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet.Because private guest page is protected, page copy with
mmu_notifierto migrate page doesn't work. Callback from backing store is needed.When the faulting GPA is private, the KVM fault is also called private. When resolving private KVM, allocate mirrored private page table and call hooks to operate on mirrored private page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to mirrored private page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | | | V | KVM private PT root | CPU private PT root | | | V | V private PT ---hook to mirror--->mirrored private PT | | | \--------------------+------\ | | | | | V V | private guest page | | non-encrypted memory | encrypted memory |PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For mirrored private page table, hooks are called to update mirrored private page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K mirrored private SPTE => error vcpu 1: TDX SEAMCALL to update 2M mirrored private SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update mirrored private SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Add
is_privatemember tokvm_page_faultto indicate the fault is private. Alsois_privatemember to structtdp_interto propagate it.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 20 +++ arch/x86/kvm/mmu/mmu.c | 86 +++++++++- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_iter.c | 1 + arch/x86/kvm/mmu/tdp_iter.h | 5 +- arch/x86/kvm/mmu/tdp_mmu.c | 247 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 7 +- virt/kvm/kvm_main.c | 1 + 10 files changed, 346 insertions(+), 62 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -93,6 +93,8 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(free_private_sp) +KVM_X86_OP_OPTIONAL(handle_changed_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +20
-0@@ -440,6 +440,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1435,6 +1436,20 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; } +struct kvm_spte { +kvm_pfn_t pfn; +bool is_present; +bool is_leaf; +}; + +struct kvm_spte_change { +gfn_t gfn; +enum pg_level level; +struct kvm_spte old; +struct kvm_spte new; +void *sept_page; +}; +struct kvm_x86_ops {const char *name;@@ -1547,6 +1562,11 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,const struct kvm_spte_change *change); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +79
-7@@ -1577,7 +1577,11 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) flush =kvm_handle_gfn_range (kvm, range, kvm_unmap_rmapp);if (is_tdp_mmu_enabled (kvm)) - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush); +/* +* private page needs to be kept and handle page migration +* on next EPT violation. +*/ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, range, flush, false);return flush; }@@ -3082,7 +3086,8 @@ static int handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fau* SPTE value without #VE suppress bit cleared * (kvm->arch.shadow_mmio_value = 0). */ -if (unlikely (!vcpu->kvm->arch.enable_mmio_caching) || +if (unlikely (!vcpu->kvm->arch.enable_mmio_caching && + !kvm_gfn_shared_mask (vcpu->kvm)) ||unlikely (fault->gfn >kvm_mmu_max_gfn ()))return RET_PF_EMULATE ; }@@ -3454,7 +3459,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -4026,6 +4036,32 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true); } +/* +* Private page can't be release on mmu_notifier without losing page contents. +* The help, callback, from backing store is needed to allow page migration. +* For now, pin the page. +*/ +static int kvm_faultin_pfn_private_mapped (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +hva_t hva =gfn_to_hva_memslot (fault->slot, fault->gfn); +struct page *page[1 ]; + + fault->map_writable = false; + fault->pfn =KVM_PFN_ERR_FAULT ; +if (hva ==KVM_HVA_ERR_RO_BAD || hva ==KVM_HVA_ERR_BAD ) +return RET_PF_CONTINUE ; + +/* TDX allows only RWX. Read-only isn't supported. */ +WARN_ON_ONCE (!fault->write); +if (pin_user_pages_fast (hva,1 ,FOLL_WRITE , page) !=1 ) +return RET_PF_INVALID ; + + fault->map_writable = true; + fault->pfn =page_to_pfn (page[0 ]); +return RET_PF_CONTINUE ; +} +static int kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -4058,6 +4094,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return RET_PF_EMULATE ; } +if (fault->is_private) +return kvm_faultin_pfn_private_mapped (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, &async, fault->write, &fault->map_writable,@@ -4110,6 +4149,17 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva); } +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r) +{ +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) +return ; + +if (fault->is_private) +put_page (pfn_to_page (fault->pfn)); +else +kvm_release_pfn_clean (fault->pfn); +} +static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);@@ -4117,7 +4167,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultunsigned long mmu_seq;int r; - fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->gfn =gpa_to_gfn (fault->addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))@@ -4166,7 +4216,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultread_unlock (&vcpu->kvm->mmu_lock); else write_unlock(&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);return r; }@@ -5665,6 +5715,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -5855,6 +5906,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)* lead to use-after-free. */ if (is_tdp_mmu_enabled (kvm)) +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ kvm_tdp_mmu_zap_invalidated_roots (kvm); }@@ -5882,7 +5937,8 @@ static void kvm_mmu_zap_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) .may_block = false, }; - flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush); +/* All private page should be zapped on memslot deletion. */ + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush, true); }else { flush =slot_handle_level (kvm, slot, kvm_zap_rmapp,PG_LEVEL_4K ,KVM_MAX_HUGEPAGE_LEVEL , true);@@ -5990,7 +6046,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_leafs (kvm, i, gfn_start, - gfn_end, true, flush); + gfn_end, true, flush, false); }if (flush)@@ -6023,6 +6079,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -6111,6 +6172,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_sptep (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -6151,6 +6215,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6437,6 +6506,9 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/mmu_internal.h +37
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#undef MMU_DEBUG#ifdef MMU_DEBUG @@ -164,11 +166,30 @@ static inline void kvm_mmu_alloc_private_sp(WARN_ON_ONCE (!sp->private_sp); } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ + gfp &= ~__GFP_ZERO; + sp->private_sp = (void *)__get_free_page (gfp); +if (!sp->private_sp) +return -ENOMEM ; +return 0 ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) {if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT )free_page ((unsigned long )sp->private_sp); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_private (kvm, gfn); +else +return kvm_gfn_shared (kvm, gfn); +}#else static inline bool is_private_sp (struct kvm_mmu_page *sp) {@@ -194,11 +215,25 @@ static inline void kvm_mmu_alloc_private_sp( { } +static inline int kvm_alloc_private_sp_for_split ( +struct kvm_mmu_page *sp,gfp_t gfp) +{ +return -ENOMEM ; +} +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif +void kvm_mmu_release_fault (struct kvm *kvm,struct kvm_page_fault *fault,int r); +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* @@ -246,6 +281,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp;const bool nx_huge_page_workaround_enabled; +const bool is_private;/* * Whether a >4KB mapping can be created or is forbidden due to NX @@ -327,6 +363,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), + .is_private = kvm_is_private_gpa(vcpu->kvm, cr2_or_gpa), .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K,arch/x86/kvm/mmu/paging_tmpl.h +0
-0
arch/x86/kvm/mmu/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault · line 880
Quoted 5 lines
arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -877,7 +877,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);
Do we really need this? Shadow page table is not supported for TD guest.
Quoted 647 lines
arch/x86/kvm/mmu/paging_tmpl.h +0
-0return r; }arch/x86/kvm/mmu/tdp_iter.c +1
-0@@ -53,6 +53,7 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root, iter->min_level = min_level; iter->pt_path[iter->root_level -1 ] = (tdp_ptep_t )root->spt; iter->as_id =kvm_mmu_page_as_id (root); + iter->is_private =is_private_sp (root);tdp_iter_restart (iter); }arch/x86/kvm/mmu/tdp_iter.h +4
-1@@ -71,7 +71,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;@@ -94,6 +94,9 @@ struct tdp_iter {* level instead of advancing to the next entry. */ bool yielded; + +/* True if this iter is handling private KVM page fault. */ +bool is_private; };/* arch/x86/kvm/mmu/tdp_mmu.c +197
-50@@ -278,18 +278,24 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, kvm_mmu_page_as_id(_root) != _as_id) { \ } else -static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *tdp_mmu_alloc_sp( +struct kvm_vcpu *vcpu,bool private,bool is_root) {struct kvm_mmu_page *sp; sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache); +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp, is_root); +else +kvm_mmu_init_private_sp (sp,NULL ); +return sp; } -static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep, -gfn_t gfn,union kvm_mmu_page_role role) +static void tdp_mmu_init_sp (struct kvm_mmu_page *sp,tdp_ptep_t sptep,gfn_t gfn, +union kvm_mmu_page_role role) {set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -297,7 +303,6 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, sp->gfn = gfn; sp->ptep = sptep; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true); }@@ -316,7 +321,8 @@ static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp,tdp_mmu_init_sp (child_sp, iter->sptep, iter->gfn, role); } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role = vcpu->arch.mmu->root_role;struct kvm *kvm = vcpu->kvm;@@ -330,11 +336,12 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)*/ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (root)) goto out; } - root =tdp_mmu_alloc_sp (vcpu); + root =tdp_mmu_alloc_sp (vcpu, private, true);tdp_mmu_init_sp (root,NULL ,0 , role);refcount_set (&root->tdp_mmu_root_count,1 );@@ -344,12 +351,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -410,6 +422,7 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* * @kvm: kvm instance * @pt: the page removed from the paging structure +* @is_private: pt is private or not. * @shared: This operation may not be running under the exclusive use * of the MMU lock and the operation must synchronize with other * threads that might be modifying SPTEs. @@ -422,7 +435,8 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,* this thread will be responsible for ensuring the page is freed. Hence the * early rcu_dereferences in the function. */ -static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool shared) +static void handle_removed_pt (struct kvm *kvm,tdp_ptep_t pt,bool is_private, +bool shared) {struct kvm_mmu_page *sp =sptep_to_sp (rcu_dereference (pt));int level = sp->role.level;@@ -498,8 +512,20 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte,REMOVED_SPTE , level); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, is_private, + old_spte,REMOVED_SPTE , level, + shared); + } + +if (is_private &&WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback);@@ -510,6 +536,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure @@ -521,14 +548,30 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared) {bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn; +struct kvm_spte_change change = { + .gfn = gfn, + .level = level, + .old = { + .pfn = old_pfn, + .is_present = was_present, + .is_leaf = was_leaf, + }, + .new = { + .pfn = new_pfn, + .is_present = is_present, + .is_leaf = is_leaf, + }, + };WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -595,7 +638,7 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn);/* * Recursively handle child PTs if the change removed a subtree from @@ -604,16 +647,47 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* pages are kernel allocations and should never be migrated. */ if (was_present && !was_leaf && - (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) -handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), shared); + (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) { +WARN_ON (private_spte != +is_private_sptep (spte_to_child_pt (old_spte, level))); +handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), + private_spte, shared); + } + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +* +* This is after handling lower page table by above +* handle_remove_tdp_mmu_page(). S-EPT requires to remove S-EPT tables +* after removing childrens. +*/ +if (private_spte && +/* Ignore change of software only bits. e.g. host_writable */ + (was_leaf != is_leaf || was_present != is_present || pfn_changed)) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + change.sept_page = sept_page; + +static_call (kvm_x86_handle_changed_private_spte)(kvm, &change); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);@@ -640,6 +714,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = iter->is_private && !is_removed_spte(new_spte); +u64 tmp_spte = freeze_spte ?REMOVED_SPTE : new_spte;u64 *sptep =rcu_dereference (iter->sptep);u64 old_spte;@@ -657,7 +733,7 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,* Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs and * does not hold the mmu_lock. */ - old_spte =cmpxchg64 (sptep, iter->old_spte, new_spte); + old_spte =cmpxchg64 (sptep, iter->old_spte, tmp_spte);if (old_spte != iter->old_spte) {/* * The page table entry was modified by a different logical @@ -669,10 +745,14 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return -EBUSY ; } -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte ( + kvm, iter->as_id, iter->gfn, iter->is_private, + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +__kvm_tdp_mmu_write_spte (sptep, new_spte); +return 0 ; }@@ -734,13 +814,15 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* unless performing certain dirty logging operations. * Leaving record_dirty_log unset in that case prevents page * writes from being double counted. +* @is_private: The fault is private. * * Returns the old SPTE value, which _may_ be different than @old_spte if the * SPTE had voldatile bits. */ static u64 __tdp_mmu_set_spte (struct kvm *kvm,int as_id,tdp_ptep_t sptep, -u64 old_spte,u64 new_spte,gfn_t gfn,int level, -bool record_acc_track,bool record_dirty_log) +u64 old_spte,u64 new_spte,gfn_t gfn,int level, +bool record_acc_track,bool record_dirty_log, +bool is_private) {lockdep_assert_held_write (&kvm->mmu_lock);@@ -755,7 +837,8 @@ static u64 __tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte, new_spte, level); -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, false); +__handle_changed_spte (kvm, as_id, gfn, is_private, + old_spte, new_spte, level, false);if (record_acc_track)handle_changed_spte_acc_track (old_spte, new_spte, level);@@ -774,7 +857,8 @@ static inline void _tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, iter->old_spte =__tdp_mmu_set_spte (kvm, iter->as_id, iter->sptep, iter->old_spte, new_spte, iter->gfn, iter->level, - record_acc_track, record_dirty_log); + record_acc_track, record_dirty_log, + iter->is_private); } static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,@@ -807,8 +891,11 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, to_shadow_page(_mmu->root.hpa), _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + to_shadow_page((_private) ? _mmu->private_root_hpa : \ + _mmu->root.hpa), \ + _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -945,7 +1032,7 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)__tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 , - true, true); + true, true,is_private_sp (sp));return true; }@@ -961,13 +1048,21 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)* operation can cause a soft lockup. */ static bool tdp_mmu_zap_leafs (struct kvm *kvm,struct kvm_mmu_page *root, -gfn_t start,gfn_t end,bool can_yield,bool flush) +gfn_t start,gfn_t end,bool can_yield,bool flush, +bool drop_private) {struct tdp_iter iter; end =min (end,tdp_mmu_max_gfn_exclusive ());lockdep_assert_held_write (&kvm->mmu_lock); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +WARN_ON_ONCE (!is_private_sp (root) && drop_private); + start =kvm_gfn_for_root (kvm, root, start); + end =kvm_gfn_for_root (kvm, root, end);rcu_read_lock ();@@ -1002,12 +1097,13 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */ bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start,gfn_t end, -bool can_yield,bool flush) +bool can_yield,bool flush,bool drop_private) {struct kvm_mmu_page *root;for_each_tdp_mmu_root_yield_safe (kvm, root, as_id) - flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush); + flush =tdp_mmu_zap_leafs (kvm, root, start, end, can_yield, flush, + drop_private &&is_private_sp (root));return flush; }@@ -1067,6 +1163,12 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)lockdep_assert_held_write (&kvm->mmu_lock);list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (!root->role.invalid && !WARN_ON_ONCE (!kvm_tdp_mmu_get_root (root))) { root->role.invalid = true;@@ -1087,14 +1189,22 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ; +gfn_t gfn_unalias = iter->gfn & ~kvm_gfn_shared_mask (vcpu->kvm);WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (kvm_gfn_shared_mask (vcpu->kvm) && !fault->is_private) + pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, gfn_unalias, pte_access);else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, + gfn_unalias, fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1167,8 +1277,7 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,return 0 ; } -static int tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +static int tdp_mmu_populate_nonleaf (struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) {struct kvm_mmu_page *sp;int ret;@@ -1176,7 +1285,7 @@ static int tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =tdp_mmu_alloc_sp (vcpu); + sp =tdp_mmu_alloc_sp (vcpu, iter->is_private, false);tdp_mmu_init_child_sp (sp, iter); ret =tdp_mmu_link_sp (vcpu->kvm, iter, sp, account_nx, true);@@ -1193,6 +1302,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private = fault->is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1201,7 +1312,16 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);@@ -1217,6 +1337,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_sptep (iter.sptep)); +/* * The iter must explicitly re-read the spte here @@ -1258,11 +1384,13 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return ret; } +/* Used by mmu notifier via kvm_unmap_gfn_range() */ bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush) +bool flush,bool drop_private) {return kvm_tdp_mmu_zap_leafs (kvm, range->slot->as_id, range->start, - range->end, range->may_block, flush); + range->end, range->may_block, flush, + drop_private); } typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,@@ -1445,7 +1573,8 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (gfp_t gfp) +static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split ( +gfp_t gfp,bool is_private) {struct kvm_mmu_page *sp;@@ -1456,6 +1585,12 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)return NULL ; sp->spt = (void *)__get_free_page (gfp); +if (is_private) { +if (kvm_alloc_private_sp_for_split (sp, gfp)) { +free_page ((unsigned long )sp->spt); + sp->spt =NULL ; + } + }if (!sp->spt) {kmem_cache_free (mmu_page_header_cache, sp);return NULL ;@@ -1469,6 +1604,11 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, bool shared) {struct kvm_mmu_page *sp; +bool is_private = iter->is_private; + +/* TODO: For now large page isn't supported for private SPTE. */ +WARN_ON (is_private); +WARN_ON (iter->is_private !=is_private_sptep (iter->sptep));/* * Since we are allocating while under the MMU lock we have to be @@ -1479,7 +1619,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,* If this allocation fails we drop the lock and retry with reclaim * allowed. */ - sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT); + sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT, is_private);if (sp)return sp;@@ -1491,7 +1631,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,write_unlock (&kvm->mmu_lock); iter->yielded = true; - sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT ); + sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT , is_private);if (shared)read_lock (&kvm->mmu_lock);@@ -1907,10 +2047,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gpa (vcpu->kvm, addr); *root_level = vcpu->arch.mmu->root_role.level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1937,7 +2081,10 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +/* fast page fault for private GPA isn't supported. */ +WARN_ON_ONCE (kvm_is_private_gpa (vcpu->kvm, addr)); + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; }arch/x86/kvm/mmu/tdp_mmu.h +4
-3@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {@@ -16,7 +16,8 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool drop_private);bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp);void kvm_tdp_mmu_zap_all (struct kvm *kvm);void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm);@@ -25,7 +26,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range, -bool flush); +bool flush,bool drop_private);bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_tdp_mmu_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range);virt/kvm/kvm_main.c +1
-1@@ -196,6 +196,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put() -- 2.25.1
Re: [PATCH v7 046/102] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 6 lines · Yuan Yao
Quoted 6 lines
arch/x86/kvm/mmu/paging_tmpl.h +0
-0@@ -877,7 +877,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
Quoted 6 lines
arch/x86/kvm/mmu/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault · line 880
Quoted 4 lines
arch/x86/kvm/mmu/paging_tmpl.h +1
-1out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (fault->pfn); +kvm_mmu_release_fault (vcpu->kvm, fault, r);Do we really need this? Shadow page table is not supported for TD guest.
For consistency. pair of kvm_faultin_pfn() and kvm_mmu_release_fault().
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 47/102 The start of TDX KVM patch series: TDX EPT violation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX EPT violation.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -19,12 +19,12 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Not yet +* TDX EPT violation: Applying * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied * KVM TDP refactoring for TDX: Applied -* KVM TDP MMU hooks: Applying +* KVM TDP MMU hooks: Applied * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
PATCH v7 48/102 KVM: x86/mmu: Disallow dirty logging for x86 TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX doesn't support dirty logging. Report dirty logging isn't supported so that device model, for example qemu, can properly handle it.
--- arch/x86/kvm/x86.c | 5 +++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +5 -0
@@ -13164,6 +13164,11 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io); +bool kvm_arch_dirty_log_supported (struct kvm *kvm) +{ +return kvm->arch.vm_type !=KVM_X86_TDX_VM ; +} +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);
include/linux/kvm_host.h +1 -0
@@ -1452,6 +1452,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm); +bool kvm_arch_dirty_log_supported (struct kvm *kvm);#ifndef __KVM_HAVE_ARCH_VM_ALLOC/*
virt/kvm/kvm_main.c +12 -3
@@ -1467,9 +1467,18 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region *mem) +bool __weak kvm_arch_dirty_log_supported (struct kvm *kvm) { -u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ; +return true; +} + +static int check_memory_region_flags (struct kvm *kvm, +const struct kvm_userspace_memory_region *mem) +{ +u32 valid_flags =0 ; + +if (kvm_arch_dirty_log_supported (kvm)) + valid_flags |=KVM_MEM_LOG_DIRTY_PAGES ;#ifdef __KVM_HAVE_READONLY_MEM valid_flags |=KVM_MEM_READONLY ;
@@ -1871,7 +1880,7 @@ int __kvm_set_memory_region(struct kvm *kvm,int as_id, id;int r; - r =check_memory_region_flags (mem); + r =check_memory_region_flags (kvm, mem);if (r)return r;
Signature · 1 line
2.25.1
Re: [PATCH v7 048/102] KVM: x86/mmu: Disallow dirty logging for x86 TDX
Quoted 7 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX doesn't support dirty logging. Report dirty logging isn't supported so that device model, for example qemu, can properly handle it.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>
Xiaoyao's SoB looks weird.
Quoted 70 lines
Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 5 +++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-)arch/x86/kvm/x86.c +5
-0@@ -13164,6 +13164,11 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io); +bool kvm_arch_dirty_log_supported (struct kvm *kvm) +{ +return kvm->arch.vm_type !=KVM_X86_TDX_VM ; +} +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);include/linux/kvm_host.h +1
-0@@ -1452,6 +1452,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm); +bool kvm_arch_dirty_log_supported (struct kvm *kvm);#ifndef __KVM_HAVE_ARCH_VM_ALLOC/* virt/kvm/kvm_main.c +12
-3@@ -1467,9 +1467,18 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region *mem) +bool __weak kvm_arch_dirty_log_supported (struct kvm *kvm) { -u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ; +return true; +} + +static int check_memory_region_flags (struct kvm *kvm, +const struct kvm_userspace_memory_region *mem) +{ +u32 valid_flags =0 ; + +if (kvm_arch_dirty_log_supported (kvm)) + valid_flags |=KVM_MEM_LOG_DIRTY_PAGES ;#ifdef __KVM_HAVE_READONLY_MEM valid_flags |=KVM_MEM_READONLY ;@@ -1871,7 +1880,7 @@ int __kvm_set_memory_region(struct kvm *kvm,int as_id, id;int r; - r =check_memory_region_flags (mem); + r =check_memory_region_flags (kvm, mem);if (r)return r;
PATCH v7 49/102 KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Some KVM MMU operations (dirty page logging, page migration, aging page) aren't supported for private GFNs (yet) with the first generation of TDX. Silently return on unsupported TDX KVM MMU operations.
--- arch/x86/kvm/mmu/tdp_mmu.c | 74 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 3 ++ 2 files changed, 72 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +69 -5
@@ -387,6 +387,8 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,if ((!is_writable_pte (old_spte) || pfn_changed) &&is_writable_pte (new_spte)) { +/* For memory slot operations, use GFN without aliasing */ + gfn = gfn & ~kvm_gfn_shared_mask (kvm); slot =__gfn_to_memslot (__kvm_memslots (kvm, as_id), gfn);mark_page_dirty_in_slot (kvm, slot, gfn); }
@@ -1398,7 +1400,8 @@ typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter, static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,struct kvm_gfn_range *range, -tdp_handler_t handler) +tdp_handler_t handler, +bool only_shared) {struct kvm_mmu_page *root;struct tdp_iter iter;
@@ -1409,9 +1412,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { +gfn_t start; +gfn_t end; + +if (only_shared &&is_private_sp (root)) +continue ; +rcu_read_lock (); -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/ + start =kvm_gfn_for_root (kvm, root, range->start); + end =kvm_gfn_for_root (kvm, root, range->end); + +tdp_root_for_each_leaf_pte (iter, root, start, end) ret |=handler (kvm, &iter, range);rcu_read_unlock ();
@@ -1455,7 +1472,12 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range); +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest. +*/ +return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range, true); } static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,
@@ -1466,7 +1488,7 @@ static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn); +return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn, false); } static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,
@@ -1511,8 +1533,11 @@ bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)* No need to handle the remote TLB flush under RCU protection, the * target SPTE _must_ be a leaf SPTE, i.e. cannot result in freeing a * shadow page. See the WARN on pfn_changed in __handle_changed_spte(). +* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. */ -return kvm_tdp_mmu_handle_gfn (kvm, range, set_spte_gfn); +return kvm_tdp_mmu_handle_gfn (kvm, range, set_spte_gfn, true); }/*
@@ -1566,6 +1591,14 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* Because first TDX generation doesn't support write protecting private +* mappings and kvm_arch_dirty_log_supported(kvm) = false, it's a bug +* to reach here for guest TD. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return false; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true) spte_set |=wrprot_gfn_range (kvm, root, slot->base_gfn, slot->base_gfn + slot->npages, min_level);
@@ -1830,6 +1863,14 @@ bool kvm_tdp_mmu_clear_dirty_slot(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return false; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true) spte_set |=clear_dirty_gfn_range (kvm, root, slot->base_gfn, slot->base_gfn + slot->npages);
@@ -1896,6 +1937,13 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,struct kvm_mmu_page *root;lockdep_assert_held_write (&kvm->mmu_lock); +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. For now silently +* ignore KVM_CLEAR_DIRTY_LOG. +*/ +if (!kvm_arch_dirty_log_supported (kvm)) +return ;for_each_tdp_mmu_root (kvm, root, slot->as_id)clear_dirty_pt_masked (kvm, root, gfn, mask, wrprot); }
@@ -1975,6 +2023,13 @@ void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* This should only be reachable when diryt-log is supported. It's a +* bug to reach here. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return ; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true)zap_collapsible_spte_range (kvm, root, slot); }
@@ -2028,6 +2083,15 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,bool spte_set = false;lockdep_assert_held_write (&kvm->mmu_lock); + +/* +* First TDX generation doesn't support write protecting private +* mappings, silently ignore the request. KVM_GET_DIRTY_LOG etc +* can reach here, no warning. +*/ +if (!kvm_arch_dirty_log_supported (kvm)) +return false; +for_each_tdp_mmu_root (kvm, root, slot->as_id) spte_set |=write_protect_gfn (kvm, root, gfn, min_level);
arch/x86/kvm/x86.c +3 -0
@@ -12243,6 +12243,9 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm,u32 new_flags = new ? new->flags :0 ;bool log_dirty_pages = new_flags &KVM_MEM_LOG_DIRTY_PAGES ; +if (!kvm_arch_dirty_log_supported (kvm) && log_dirty_pages) +return ; +/* * Update CPU dirty logging if dirty logging is being toggled. This * applies to all operations.
Signature · 1 line
2.25.1
Re: [PATCH v7 049/102] KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
Quoted 17 lines · On Mon, Jun 27, 2022 at 02:53:41PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some KVM MMU operations (dirty page logging, page migration, aging page) aren't supported for private GFNs (yet) with the first generation of TDX. Silently return on unsupported TDX KVM MMU operations.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 74 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 3 ++ 2 files changed, 72 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0
arch/x86/kvm/mmu/tdp_mmu.c static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn, · line 391
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -387,6 +387,8 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,if ((!is_writable_pte (old_spte) || pfn_changed) &&is_writable_pte (new_spte)) { +/* For memory slot operations, use GFN without aliasing */ + gfn = gfn & ~kvm_gfn_shared_mask (kvm);
This should be part of enabling, please consider to squash it into patch 46.
Quoted 53 lines
arch/x86/kvm/mmu/tdp_mmu.c +23
-3slot =__gfn_to_memslot (__kvm_memslots (kvm, as_id), gfn);mark_page_dirty_in_slot (kvm, slot, gfn); }@@ -1398,7 +1400,8 @@ typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter, static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,struct kvm_gfn_range *range, -tdp_handler_t handler) +tdp_handler_t handler, +bool only_shared) {struct kvm_mmu_page *root;struct tdp_iter iter;@@ -1409,9 +1412,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { +gfn_t start; +gfn_t end; + +if (only_shared &&is_private_sp (root)) +continue ; +rcu_read_lock (); -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/ + start =kvm_gfn_for_root (kvm, root, range->start); + end =kvm_gfn_for_root (kvm, root, range->end); + +tdp_root_for_each_leaf_pte (iter, root, start, end) ret |=handler (kvm, &iter, range);rcu_read_unlock ();@@ -1455,7 +1472,12 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range); +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest. +*/ +return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range, true); } static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,
arch/x86/kvm/mmu/tdp_mmu.c static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter, · line 1491
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1@@ -1466,7 +1488,7 @@ static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn); +return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn, false);
The "false" here means we will do young testing for even private pages, but we don't have actual A bit state in iter->old_spte for them, so may here should be "true" ?
arch/x86/kvm/mmu/tdp_mmu.c bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) · line 1539
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-0} static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,@@ -1511,8 +1533,11 @@ bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)* No need to handle the remote TLB flush under RCU protection, the * target SPTE _must_ be a leaf SPTE, i.e. cannot result in freeing a * shadow page. See the WARN on pfn_changed in __handle_changed_spte(). +* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. */
Worth to catch this by WARN_ON() ? Depends on you.
Quoted 96 lines
arch/x86/kvm/mmu/tdp_mmu.c +40
-1-return kvm_tdp_mmu_handle_gfn (kvm, range, set_spte_gfn); +return kvm_tdp_mmu_handle_gfn (kvm, range, set_spte_gfn, true); }/* @@ -1566,6 +1591,14 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* Because first TDX generation doesn't support write protecting private +* mappings and kvm_arch_dirty_log_supported(kvm) = false, it's a bug +* to reach here for guest TD. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return false; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true) spte_set |=wrprot_gfn_range (kvm, root, slot->base_gfn, slot->base_gfn + slot->npages, min_level);@@ -1830,6 +1863,14 @@ bool kvm_tdp_mmu_clear_dirty_slot(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return false; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true) spte_set |=clear_dirty_gfn_range (kvm, root, slot->base_gfn, slot->base_gfn + slot->npages);@@ -1896,6 +1937,13 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,struct kvm_mmu_page *root;lockdep_assert_held_write (&kvm->mmu_lock); +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. For now silently +* ignore KVM_CLEAR_DIRTY_LOG. +*/ +if (!kvm_arch_dirty_log_supported (kvm)) +return ;for_each_tdp_mmu_root (kvm, root, slot->as_id)clear_dirty_pt_masked (kvm, root, gfn, mask, wrprot); }@@ -1975,6 +2023,13 @@ void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); +/* +* This should only be reachable when diryt-log is supported. It's a +* bug to reach here. +*/ +if (WARN_ON (!kvm_arch_dirty_log_supported (kvm))) +return ; +for_each_valid_tdp_mmu_root_yield_safe (kvm, root, slot->as_id, true)zap_collapsible_spte_range (kvm, root, slot); }@@ -2028,6 +2083,15 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,bool spte_set = false;lockdep_assert_held_write (&kvm->mmu_lock); + +/* +* First TDX generation doesn't support write protecting private +* mappings, silently ignore the request. KVM_GET_DIRTY_LOG etc +* can reach here, no warning. +*/ +if (!kvm_arch_dirty_log_supported (kvm)) +return false; +for_each_tdp_mmu_root (kvm, root, slot->as_id) spte_set |=write_protect_gfn (kvm, root, gfn, min_level);arch/x86/kvm/x86.c +3
-1@@ -12243,6 +12243,9 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm,u32 new_flags = new ? new->flags :0 ;bool log_dirty_pages = new_flags &KVM_MEM_LOG_DIRTY_PAGES ; +if (!kvm_arch_dirty_log_supported (kvm) && log_dirty_pages) +return ; +/* * Update CPU dirty logging if dirty logging is being toggled. This * applies to all operations. -- 2.25.1
Re: [PATCH v7 049/102] KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
Quoted 19 lines · Yuan Yao
Quoted 18 lines · On Mon, Jun 27, 2022 at 02:53:41PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some KVM MMU operations (dirty page logging, page migration, aging page) aren't supported for private GFNs (yet) with the first generation of TDX. Silently return on unsupported TDX KVM MMU operations.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 74 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 3 ++ 2 files changed, 72 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0@@ -387,6 +387,8 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn, · line 391
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0if ((!is_writable_pte (old_spte) || pfn_changed) &&is_writable_pte (new_spte)) { +/* For memory slot operations, use GFN without aliasing */ + gfn = gfn & ~kvm_gfn_shared_mask (kvm);This should be part of enabling, please consider to squash it into patch 46.
Yes, merged into it.
Quoted 54 lines
Quoted 54 lines
arch/x86/kvm/mmu/tdp_mmu.c +23
-3slot =__gfn_to_memslot (__kvm_memslots (kvm, as_id), gfn);mark_page_dirty_in_slot (kvm, slot, gfn); }@@ -1398,7 +1400,8 @@ typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter, static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,struct kvm_gfn_range *range, -tdp_handler_t handler) +tdp_handler_t handler, +bool only_shared) {struct kvm_mmu_page *root;struct tdp_iter iter;@@ -1409,9 +1412,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { +gfn_t start; +gfn_t end; + +if (only_shared &&is_private_sp (root)) +continue ; +rcu_read_lock (); -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/ + start =kvm_gfn_for_root (kvm, root, range->start); + end =kvm_gfn_for_root (kvm, root, range->end); + +tdp_root_for_each_leaf_pte (iter, root, start, end) ret |=handler (kvm, &iter, range);rcu_read_unlock ();@@ -1455,7 +1472,12 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,bool kvm_tdp_mmu_age_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range); +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest. +*/ +return kvm_tdp_mmu_handle_gfn (kvm, range, age_gfn_range, true); } static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,@@ -1466,7 +1488,7 @@ static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter, · line 1491
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1bool kvm_tdp_mmu_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn); +return kvm_tdp_mmu_handle_gfn (kvm, range, test_age_gfn, false);The "false" here means we will do young testing for even private pages, but we don't have actual A bit state in
iter->old_sptefor them, so may here should be "true" ?
Yes, nice catch.
Quoted 13 lines
arch/x86/kvm/mmu/tdp_mmu.c bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) · line 1539
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-0} static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,@@ -1511,8 +1533,11 @@ bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)* No need to handle the remote TLB flush under RCU protection, the * target SPTE _must_ be a leaf SPTE, i.e. cannot result in freeing a * shadow page. See the WARN on pfn_changed in __handle_changed_spte(). +* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. */ Worth to catch this by
WARN_ON()? Depends on you.
It call back can be called for shared pages. Here there is no easy way which GPA (private or shared) caused it. i.e. no easy condition for WARN_ON().
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 50/102 KVM: VMX: Split out guts of EPT violation to common/exposed function
In reply to [email protected]From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
--- arch/x86/kvm/vmx/common.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 32 ++++++-------------------------- 2 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/common.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/vmx.c +6 -26
@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h"
@@ -5578,11 +5579,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; -gpa_t gpa; -u64 error_code; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu); +gpa_t gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); - exit_qualification =vmx_get_exit_qual (vcpu); +trace_kvm_page_fault (gpa, exit_qualification);/* * EPT violation happened while executing iret from NMI,
@@ -5591,29 +5591,9 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)* AAK134, BY25. */ if (!(to_vmx (vcpu)->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - enable_vnmi && - (exit_qualification &INTR_INFO_UNBLOCK_NMI )) + enable_vnmi && (exit_qualification &INTR_INFO_UNBLOCK_NMI ))vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); - gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); -trace_kvm_page_fault (gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
@@ -5627,7 +5607,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
Re: [PATCH v7 050/102] KVM: VMX: Split out guts of EPT violation to common/exposed function
Quoted 83 lines · On Mon, 2022-06-27 at 14:53 -0700, [email protected] wrote:
From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/common.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 32 ++++++-------------------------- 2 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.harch/x86/kvm/vmx/common.h new file +33
-0@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */ arch/x86/kvm/vmx/vmx.c +4
-4@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h" @@ -5578,11 +5579,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; -gpa_t gpa; -u64 error_code; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu); +gpa_t gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); - exit_qualification =vmx_get_exit_qual (vcpu); +trace_kvm_page_fault (gpa, exit_qualification);/* * EPT violation happened while executing iret from NMI,
arch/x86/kvm/vmx/vmx.c static int handle_ept_violation(struct kvm_vcpu *vcpu) · line 5594
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1
-2@@ -5591,29 +5591,9 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)* AAK134, BY25. */ if (!(to_vmx (vcpu)->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - enable_vnmi && - (exit_qualification &INTR_INFO_UNBLOCK_NMI )) + enable_vnmi && (exit_qualification &INTR_INFO_UNBLOCK_NMI ))
Why this code change?
With this removed:
Quoted 33 lines
arch/x86/kvm/vmx/vmx.c +1
-20vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); - gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); -trace_kvm_page_fault (gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/* @@ -5627,7 +5607,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
PATCH v7 51/102 KVM: VMX: Move setting of EPT MMU masks to common VT-x code
In reply to [email protected]From: Sean Christopherson <[email protected]>
EPT MMU masks are used commonly for VMX and TDX. The value needs to be initialized in common code before both VMX/TDX-specific initialization code.
--- arch/x86/kvm/vmx/main.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +5 -0
@@ -4,6 +4,7 @@#include "x86_ops.h" #include "vmx.h" #include "nested.h" +#include "mmu.h" #include "pmu.h" #include "tdx.h"
@@ -26,6 +27,10 @@ static __init int vt_hardware_setup(void) enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); +if (enable_ept) +kvm_mmu_set_ept_masks (enable_ept_ad_bits, +cpu_has_vmx_ept_execute_only ()); +return 0 ; }
arch/x86/kvm/vmx/vmx.c +0 -4
@@ -8139,10 +8139,6 @@ __init int vmx_hardware_setup(void)set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ -if (enable_ept) -kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); -/* * Setup shadow_me_value/shadow_me_mask to include MKTME KeyID * bits to shadow_zero_check.
Signature · 1 line
2.25.1
PATCH v7 52/102 KVM: TDX: Add load_mmu_pgd method for TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 11 ++++++++++- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -234,6 +234,7 @@ enum vmcs_field {TSC_MULTIPLIER_HIGH =0x00002033 ,TERTIARY_VM_EXEC_CONTROL =0x00002034 ,TERTIARY_VM_EXEC_CONTROL_HIGH =0x00002035 , +SHARED_EPT_POINTER =0x0000203C ,PID_POINTER_TABLE =0x00002042 ,PID_POINTER_TABLE_HIGH =0x00002043 , GUEST_PHYSICAL_ADDRESS = 0x00002400,
arch/x86/kvm/vmx/main.c +10 -1
@@ -110,6 +110,15 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) +return tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -228,7 +237,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -532,6 +532,11 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); +} +int tdx_dev_ioctl (void __user *argp) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -144,6 +144,8 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return 0 ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }
@@ -161,6 +163,8 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
PATCH v7 53/102 KVM: TDX: don't request KVM_REQ_APIC_PAGE_RELOAD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX doesn't need APIC page depending on vapic and its callback is WARN_ON_ONCE(is_tdx). To avoid unnecessary overhead and WARN_ON_ONCE(), skip requesting KVM_REQ_APIC_PAGE_RELOAD when TD.
------------[ cut here ]------------ WARNING: CPU: 134 PID: 42205 at arch/x86/kvm/vmx/main.c:696 vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] Modules linked in: squashfs nls_iso8859_1 nls_cp437 vhost_vsock vhost vhost_iotlb tdx_debug kvm_intel kvm irqbypass crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel crypto_simd cryptd i2c_i801 i2c_smbus i2c_ismt CPU: 134 PID: 42205 Comm: tdx_vm_tests Tainted: G W 5.17.0-rc8 #165 4baba67c36c7c1001d782c47f2964b779a5659c7 Hardware name: Intel Corporation EAGLESTREAM/EAGLESTREAM, BIOS EGSDCRB1.SYS.0066.D24.2110072326 10/07/2021 RIP: 0010:vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] Code: e7 d5 49 8b 1c 24 48 8d bb 78 15 00 00 e8 4c 78 e7 d5 48 83 bb 78 15 00 00 01 74 0d 4c 89 e7 e8 7a 9b fd ff 5b 41 5c 5d c3 90 <0f 0b 90 5b 41 5c 5d c3 66 66 2e 0f 1f 84 00 00 00 00 00 90 0f 1f RSP: 0018:ffa0000027477b68 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffa00000572d9000 RCX: ffffffffde6864d4 RDX: dffffc0000000000 RSI: 0000000000000008 RDI: ffa00000572da578 RBP: ffa0000027477b78 R08: 0000000000000001 R09: ffe21c006df80008 R10: ff1100036fc0003f R11: ffe21c006df80007 R12: ff1100036fc00000 R13: ff1100036fc000d8 R14: ff1100036fc00038 R15: ff1100036fc00000 FS: 00007fdf1ad32740(0000) GS:ff11000e1ed00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fdf15f1b000 CR3: 000000011e462005 CR4: 0000000000773ee0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400 PKRU: 55555554 Call Trace: <TASK> vcpu_enter_guest+0x145d/0x24d0 [kvm] ? inject_pending_event+0x750/0x750 [kvm] ? xsaves+0x31/0x40 ? rcu_read_lock_held_common+0x1e/0x60 ? rcu_read_lock_sched_held+0x60/0xe0 ? rcu_read_lock_bh_held+0xc0/0xc0 kvm_arch_vcpu_ioctl_run+0x25d/0xcc0 [kvm] kvm_vcpu_ioctl+0x414/0xa30 [kvm]] ? kvm_clear_dirty_log_protect+0x4d0/0x4d0 [kvm] ? userfaultfd_unmap_prep+0x240/0x240 ? __up_read+0x17f/0x530 ? rwsem_wake+0x110/0x110 ? __do_munmap+0x437/0x7c0 ? rcu_read_lock_held_common+0x1e/0x60 ? rcu_read_lock_sched_held+0x60/0xe0 ? rcu_read_lock_sched_held+0x60/0xe0 ? __kasan_check_read+0x11/0x20 ? __fget_light+0xa9/0x100 __x64_sys_ioctl+0xc0/0x100 do_syscall_64+0x39/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7fdf1ae493db Code: 0f 1e fa 48 8b 05 b5 7a 0d 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48 3d 01 f0 ff ff 73 01 c3 48 8b 0d 85 7a 0d 00 f7 d8 64 89 01 48 RSP: 002b:00007ffcf8bdfb38 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00000000006f26d0 RCX: 00007fdf1ae493db RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 0000000000000007 RBP: 0000000000000000 R08: 0000000000411d36 R09: 0000000000000000 R10: fffffffffffffb69 R11: 0000000000000246 R12: 0000000000402410 R13: 00000000006f02b0 R14: 0000000000000000 R15: 0000000000000000 </TASK> irq event stamp: 0 hardirqs last enabled at (0): [<0000000000000000>] 0x0 hardirqs last disabled at (0): [<ffffffffb40c809a>] copy_process+0xaca/0x3270 softirqs last enabled at (0): [<ffffffffb40c809a>] copy_process+0xaca/0x3270 softirqs last disabled at (0): [<0000000000000000>] 0x0 ---[ end trace 0000000000000000 ]---
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -10042,7 +10042,8 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,* Update it when it becomes invalid. */ apic_address =gfn_to_hva (kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); -if (start <= apic_address && apic_address < end) +if (start <= apic_address && apic_address < end && + !kvm_gfn_shared_mask (kvm))kvm_make_all_cpus_request (kvm,KVM_REQ_APIC_PAGE_RELOAD ); }
Signature · 1 line
2.25.1
Re: [PATCH v7 053/102] KVM: TDX: don't request KVM_REQ_APIC_PAGE_RELOAD
Quoted 55 lines · On Mon, Jun 27, 2022 at 02:53:45PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX doesn't need APIC page depending on vapic and its callback is
WARN_ON_ONCE(is_tdx). To avoid unnecessary overhead andWARN_ON_ONCE(), skip requestingKVM_REQ_APIC_PAGE_RELOADwhen TD.------------[ cut here ]------------ WARNING: CPU: 134 PID: 42205 at arch/x86/kvm/vmx/main.c:696 vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] Modules linked in: squashfs nls_iso8859_1 nls_cp437 vhost_vsock vhost vhost_iotlb tdx_debug kvm_intel kvm irqbypass crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel crypto_simd cryptd i2c_i801 i2c_smbus i2c_ismt CPU: 134 PID: 42205 Comm: tdx_vm_tests Tainted: G W 5.17.0-rc8 #165 4baba67c36c7c1001d782c47f2964b779a5659c7 Hardware name: Intel Corporation EAGLESTREAM/EAGLESTREAM, BIOS EGSDCRB1.SYS.0066.D24.2110072326 10/07/2021 RIP: 0010:vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] Code: e7 d5 49 8b 1c 24 48 8d bb 78 15 00 00 e8 4c 78 e7 d5 48 83 bb 78 15 00 00 01 74 0d 4c 89 e7 e8 7a 9b fd ff 5b 41 5c 5d c3 90 <0f 0b 90 5b 41 5c 5d c3 66 66 2e 0f 1f 84 00 00 00 00 00 90 0f 1f RSP: 0018:ffa0000027477b68 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffa00000572d9000 RCX: ffffffffde6864d4 RDX: dffffc0000000000 RSI: 0000000000000008 RDI: ffa00000572da578 RBP: ffa0000027477b78 R08: 0000000000000001 R09: ffe21c006df80008 R10: ff1100036fc0003f R11: ffe21c006df80007 R12: ff1100036fc00000 R13: ff1100036fc000d8 R14: ff1100036fc00038 R15: ff1100036fc00000 FS: 00007fdf1ad32740(0000) GS:ff11000e1ed00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fdf15f1b000 CR3: 000000011e462005 CR4: 0000000000773ee0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400 PKRU: 55555554 Call Trace: <TASK> vcpu_enter_guest+0x145d/0x24d0 [kvm] ? inject_pending_event+0x750/0x750 [kvm] ? xsaves+0x31/0x40 ? rcu_read_lock_held_common+0x1e/0x60 ? rcu_read_lock_sched_held+0x60/0xe0 ? rcu_read_lock_bh_held+0xc0/0xc0 kvm_arch_vcpu_ioctl_run+0x25d/0xcc0 [kvm] kvm_vcpu_ioctl+0x414/0xa30 [kvm]] ? kvm_clear_dirty_log_protect+0x4d0/0x4d0 [kvm] ? userfaultfd_unmap_prep+0x240/0x240 ? __up_read+0x17f/0x530 ? rwsem_wake+0x110/0x110 ? __do_munmap+0x437/0x7c0 ? rcu_read_lock_held_common+0x1e/0x60 ? rcu_read_lock_sched_held+0x60/0xe0 ? rcu_read_lock_sched_held+0x60/0xe0 ? __kasan_check_read+0x11/0x20 ? __fget_light+0xa9/0x100 __x64_sys_ioctl+0xc0/0x100 do_syscall_64+0x39/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7fdf1ae493db Code: 0f 1e fa 48 8b 05 b5 7a 0d 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48 3d 01 f0 ff ff 73 01 c3 48 8b 0d 85 7a 0d 00 f7 d8 64 89 01 48 RSP: 002b:00007ffcf8bdfb38 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00000000006f26d0 RCX: 00007fdf1ae493db RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 0000000000000007 RBP: 0000000000000000 R08: 0000000000411d36 R09: 0000000000000000 R10: fffffffffffffb69 R11: 0000000000000246 R12: 0000000000402410
Quoted 8 lines
R13: 00000000006f02b0 R14: 0000000000000000 R15: 0000000000000000 </TASK> irq event stamp: 0 hardirqs last enabled at (0): [<0000000000000000>] 0x0 hardirqs last disabled at (0): [<ffffffffb40c809a>] copy_process+0xaca/0x3270 softirqs last enabled at (0): [<ffffffffb40c809a>] copy_process+0xaca/0x3270 softirqs last disabled at (0): [<0000000000000000>] 0x0 ---[ end trace 0000000000000000 ]---
The trace can be simplified to :
WARNING: arch/x86/kvm/vmx/main.c:696 vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] RIP: 0010:vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel] Call Trace: vcpu_enter_guest+0x145d/0x24d0 [kvm] kvm_arch_vcpu_ioctl_run+0x25d/0xcc0 [kvm] kvm_vcpu_ioctl+0x414/0xa30 [kvm]] __x64_sys_ioctl+0xc0/0x100 do_syscall_64+0x39/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae
Becasue here you just want to record the trace path of WARN_ON_ONCE(), but not request some help for debugging it.
Quoted 11 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 10046
Quoted 6 lines
arch/x86/kvm/x86.c +2
-1@@ -10042,7 +10042,8 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,* Update it when it becomes invalid. */ apic_address =gfn_to_hva (kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); -if (start <= apic_address && apic_address < end) +if (start <= apic_address && apic_address < end && + !kvm_gfn_shared_mask (kvm))
Minor: please condier to check kvm_gfn_shared_mask(kvm) before range, means firstly check is or not, then suitable or not.
Quoted 6 lines
arch/x86/kvm/x86.c +0
-1kvm_make_all_cpus_request (kvm,KVM_REQ_APIC_PAGE_RELOAD ); } --2.25.1
Re: [PATCH v7 053/102] KVM: TDX: don't request KVM_REQ_APIC_PAGE_RELOAD
Quoted 6 lines · Yuan Yao
Quoted 5 lines · On Mon, Jun 27, 2022 at 02:53:45PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX doesn't need APIC page depending on vapic and its callback is
WARN_ON_ONCE(is_tdx). To avoid unnecessary overhead andWARN_ON_ONCE(), skip requestingKVM_REQ_APIC_PAGE_RELOADwhen TD.
!kvm_gfn_shared_mask() doesn't ensure the VM is a TD. Right?
Quoted 28 lines
Quoted 12 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, · line 10046
Quoted 6 lines
arch/x86/kvm/x86.c +2
-1@@ -10042,7 +10042,8 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,* Update it when it becomes invalid. */ apic_address =gfn_to_hva (kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); -if (start <= apic_address && apic_address < end) +if (start <= apic_address && apic_address < end && + !kvm_gfn_shared_mask (kvm))Minor: please condier to check
kvm_gfn_shared_mask(kvm)before range, means firstly check is or not, then suitable or not.Quoted 6 lines
arch/x86/kvm/x86.c +0
-1kvm_make_all_cpus_request (kvm,KVM_REQ_APIC_PAGE_RELOAD ); } --2.25.1
Re: [PATCH v7 053/102] KVM: TDX: don't request KVM_REQ_APIC_PAGE_RELOAD
Quoted 9 lines · Chao Gao
Quoted 6 lines · Yuan Yao
Quoted 5 lines · On Mon, Jun 27, 2022 at 02:53:45PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX doesn't need APIC page depending on vapic and its callback is
WARN_ON_ONCE(is_tdx). To avoid unnecessary overhead andWARN_ON_ONCE(), skip requestingKVM_REQ_APIC_PAGE_RELOADwhen TD.!kvm_gfn_shared_mask() doesn't ensure the VM is a TD. Right?
That's right. I changed the check as follows.
commit 6753fc53f3b3fcbbd07ac688578ff5fb7f7f7d96 (HEAD)
Author: Isaku Yamahata <[email protected]> Date: Wed Mar 30 22:32:03 2022 -0700
KVM: TDX: don't request KVM_REQ_APIC_PAGE_RELOAD
TDX doesn't need APIC page depending on vapic and its callback is
WARN_ON_ONCE(is_tdx). To avoid unnecessary overhead and WARN_ON_ONCE(),
skip requesting KVM_REQ_APIC_PAGE_RELOAD when TD.
WARNING: arch/x86/kvm/vmx/main.c:696 vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel]
RIP: 0010:vt_set_apic_access_page_addr+0x3c/0x50 [kvm_intel]
Call Trace:
vcpu_enter_guest+0x145d/0x24d0 [kvm]
kvm_arch_vcpu_ioctl_run+0x25d/0xcc0 [kvm]
kvm_vcpu_ioctl+0x414/0xa30 [kvm]
__x64_sys_ioctl+0xc0/0x100
do_syscall_64+0x39/0xc0
entry_SYSCALL_64_after_hwframe+0x44/0xae
Signed-off-by: Isaku Yamahata <[email protected]>arch/x86/kvm/x86.c +3 -1
@@ -10045,7 +10045,9 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,* Update it when it becomes invalid. */ apic_address =gfn_to_hva (kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); -if (start <= apic_address && apic_address < end) +/* TDX doesn't need APIC page. */ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + start <= apic_address && apic_address < end)kvm_make_all_cpus_request (kvm,KVM_REQ_APIC_PAGE_RELOAD ); }
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v7 54/102 KVM: TDX: TDP MMU TDX support
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page.
TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown.
For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagating the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL.
Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shootdown with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT.
For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
--- arch/x86/kvm/vmx/main.c | 40 ++++- arch/x86/kvm/vmx/tdx.c | 318 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 21 +++ arch/x86/kvm/vmx/x86_ops.h | 2 + 4 files changed, 377 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +36 -4
@@ -110,6 +110,38 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_current (vcpu); +} + +static void vt_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) +{ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_flush_tlb_gva (vcpu, addr); +} + +static void vt_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_flush_tlb_guest (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {
@@ -185,10 +217,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .flush_tlb_all = vmx_flush_tlb_all, - .flush_tlb_current = vmx_flush_tlb_current, - .flush_tlb_gva = vmx_flush_tlb_gva, - .flush_tlb_guest = vmx_flush_tlb_guest, + .flush_tlb_all = vt_flush_tlb_all, + .flush_tlb_current = vt_flush_tlb_current, + .flush_tlb_gva = vt_flush_tlb_gva, + .flush_tlb_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .vcpu_run = vmx_vcpu_run,
arch/x86/kvm/vmx/tdx.c +318 -0
355 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +21 -0
@@ -26,9 +26,24 @@ struct kvm_tdx {int hkid;bool finalized; +atomic_t tdh_mem_track;u64 tsc_offset;unsigned long tsc_khz; + +/* +* Some SEAMCALLs try to lock TD resources (e.g. Secure-EPT) they use or +* update. If TDX module fails to obtain the lock, it returns +* TDX_OPERAND_BUSY error without spinning. It's VMM/OS responsibility +* to retry or guarantee no contention because TDX module has the +* restriction on cpu cycles it can spend and VMM/OS knows better +* vcpu scheduling. +* +* TDP MMU uses read lock of kvm.arch.mmu_lock so TDP MMU code can be +* run concurrently with multiple vCPUs. Lock to prevent seamcalls from +* running concurrently when TDP MMU is enabled. +*/ +spinlock_t seamcall_lock; };struct vcpu_tdx {
@@ -169,6 +184,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else static inline int tdx_module_setup (void ) {return -ENODEV ; };
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -145,6 +145,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return 0 ; }
@@ -164,6 +165,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {}static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif
Signature · 1 line
2.25.1
PATCH v7 55/102 The start of TDX KVM patch series: KVM TDP MMU MapGPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU MapGPA.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -11,6 +11,7 @@ What qemu can do - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. +- Qemu can populate initial guest memory image. Patch Layer status ------------------
@@ -19,7 +20,7 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Applying +* TDX EPT violation: Applied * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet
Signature · 1 line
2.25.1
PATCH v7 56/102 KVM: x86/mmu: steal software usable git to record if GFN is for shared or not
In reply to [email protected]From: Isaku Yamahata <[email protected]>
With TDX, all GFNs are private at guest boot time. At run time guest TD can explicitly change it to shared from private or vice-versa by MapGPA hypercall. If it's specified, the given GFN can't be used as otherwise. That's is, if a guest tells KVM that the GFN is shared, it can't be used as private. or vice-versa.
Steal software usable bit, SPTE_SHARED_MASK, for it from MMIO counter to record it. Use the bit SPTE_SHARED_MASK in shared or private EPT to determine which mapping, shared or private, is allowed. If requested mapping isn't allowed, return RET_PF_RETRY to wait for other vcpu to change it. The bit is recorded in both shared and private shadow page to avoid traverse one more shadow page when resolving KVM page fault.
The bit needs to be kept over zapping the EPT entry. Currently the EPT entry is initialized SHADOW_NONPRESENT_VALUE unconditionally to clear SPTE_SHARED_MASK bit. To carry SPTE_SHARED_MASK bit, introduce a helper function to get initial value for zapped entry with SPTE_SHARED_MASK bit. Replace SHADOW_NONPRESENT_VALUE with it.
--- arch/x86/kvm/mmu/spte.h | 17 +++++++--- arch/x86/kvm/mmu/tdp_mmu.c | 65 ++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 14 deletions(-)
arch/x86/kvm/mmu/spte.h +13 -4
@@ -14,6 +14,9 @@*/ #define SPTE_MMU_PRESENT_MASK BIT_ULL(11) +/* Masks that used to track for shared GPA **/ +#define SPTE_SHARED_MASK BIT_ULL(62) +/* * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
@@ -104,7 +107,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));* the memslots generation and is derived as follows: * * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 -* Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 +* Bits 8-18 of the MMIO generation are propagated to spte bits 52-61 * * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in * the MMIO generation number, as doing so would require stealing a bit from
@@ -118,7 +121,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));#define MMIO_SPTE_GEN_LOW_END 10#define MMIO_SPTE_GEN_HIGH_START 52 -#define MMIO_SPTE_GEN_HIGH_END 62 +#define MMIO_SPTE_GEN_HIGH_END 61#define MMIO_SPTE_GEN_LOW_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, \ MMIO_SPTE_GEN_LOW_START)
@@ -131,7 +134,7 @@ static_assert(!(SPTE_MMU_PRESENT_MASK &#define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)/* remember to adjust the comment above as well if you change these */ -static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==11 ); +static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==10 );#define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0)#define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)
@@ -208,6 +211,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(__REMOVED_SPTE &SPTE_MMU_PRESENT_MASK ));static_assert (!(__REMOVED_SPTE &SHADOW_NONPRESENT_VALUE )); +static_assert (!(__REMOVED_SPTE &SPTE_SHARED_MASK ));/* * See above comment around __REMOVED_SPTE. REMOVED_SPTE is the actual
@@ -217,7 +221,12 @@ static_assert(!(__REMOVED_SPTE & SHADOW_NONPRESENT_VALUE));static inline bool is_removed_spte (u64 spte) { -return spte ==REMOVED_SPTE ; +return (spte & ~SPTE_SHARED_MASK ) ==REMOVED_SPTE ; +} + +static inline u64 spte_shared_mask (u64 spte) +{ +return spte &SPTE_SHARED_MASK ; }/*
arch/x86/kvm/mmu/tdp_mmu.c +55 -10
119 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v7 056/102] KVM: x86/mmu: steal software usable git to record if GFN is for shared or not
Quoted 1 line · On Mon, Jun 27, 2022 at 02:53:48PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Subject: s/git/bit
Quoted 41 lines
With TDX, all GFNs are private at guest boot time. At run time guest TD can explicitly change it to shared from private or vice-versa by MapGPA hypercall. If it's specified, the given GFN can't be used as otherwise. That's is, if a guest tells KVM that the GFN is shared, it can't be used as private. or vice-versa.
Steal software usable bit,
SPTE_SHARED_MASK, for it from MMIO counter to record it. Use the bitSPTE_SHARED_MASKin shared or private EPT to determine which mapping, shared or private, is allowed. If requested mapping isn't allowed, returnRET_PF_RETRYto wait for other vcpu to change it. The bit is recorded in both shared and private shadow page to avoid traverse one more shadow page when resolving KVM page fault.The bit needs to be kept over zapping the EPT entry. Currently the EPT entry is initialized
SHADOW_NONPRESENT_VALUEunconditionally to clearSPTE_SHARED_MASKbit. To carrySPTE_SHARED_MASKbit, introduce a helper function to get initial value for zapped entry withSPTE_SHARED_MASKbit. ReplaceSHADOW_NONPRESENT_VALUEwith it.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.h | 17 +++++++--- arch/x86/kvm/mmu/tdp_mmu.c | 65 ++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 14 deletions(-)arch/x86/kvm/mmu/spte.h +3
-0@@ -14,6 +14,9 @@*/ #define SPTE_MMU_PRESENT_MASK BIT_ULL(11) +/* Masks that used to track for shared GPA **/ +#define SPTE_SHARED_MASK BIT_ULL(62) +/* * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
arch/x86/kvm/mmu/spte.h static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK)); · line 110
Quoted 5 lines
arch/x86/kvm/mmu/spte.h +1
-1@@ -104,7 +107,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));* the memslots generation and is derived as follows: * * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 -* Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 +* Bits 8-18 of the MMIO generation are propagated to spte bits 52-61
Should be 8-17.
Quoted 128 lines
arch/x86/kvm/mmu/spte.h +9
-3* * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in * the MMIO generation number, as doing so would require stealing a bit from @@ -118,7 +121,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));#define MMIO_SPTE_GEN_LOW_END 10#define MMIO_SPTE_GEN_HIGH_START 52 -#define MMIO_SPTE_GEN_HIGH_END 62 +#define MMIO_SPTE_GEN_HIGH_END 61#define MMIO_SPTE_GEN_LOW_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, \ MMIO_SPTE_GEN_LOW_START)@@ -131,7 +134,7 @@ static_assert(!(SPTE_MMU_PRESENT_MASK &#define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)/* remember to adjust the comment above as well if you change these */ -static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==11 ); +static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==10 );#define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0)#define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)@@ -208,6 +211,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(__REMOVED_SPTE &SPTE_MMU_PRESENT_MASK ));static_assert (!(__REMOVED_SPTE &SHADOW_NONPRESENT_VALUE )); +static_assert (!(__REMOVED_SPTE &SPTE_SHARED_MASK ));/* * See above comment around __REMOVED_SPTE. REMOVED_SPTE is the actual @@ -217,7 +221,12 @@ static_assert(!(__REMOVED_SPTE & SHADOW_NONPRESENT_VALUE));static inline bool is_removed_spte (u64 spte) { -return spte ==REMOVED_SPTE ; +return (spte & ~SPTE_SHARED_MASK ) ==REMOVED_SPTE ; +} + +static inline u64 spte_shared_mask (u64 spte) +{ +return spte &SPTE_SHARED_MASK ; }/* arch/x86/kvm/mmu/tdp_mmu.c +29
-8@@ -758,6 +758,11 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return 0 ; } +static u64 shadow_nonpresent_spte (u64 old_spte) +{ +return SHADOW_NONPRESENT_VALUE |spte_shared_mask (old_spte); +} +static inline int tdp_mmu_zap_spte_atomic (struct kvm *kvm,struct tdp_iter *iter) {@@ -791,7 +796,8 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* SHADOW_NONPRESENT_VALUE (which sets "suppress #VE" bit) so it * can be set when EPT table entries are zapped. */ -__kvm_tdp_mmu_write_spte (iter->sptep,SHADOW_NONPRESENT_VALUE ); +__kvm_tdp_mmu_write_spte (iter->sptep, +shadow_nonpresent_spte (iter->old_spte));return 0 ; }@@ -975,8 +981,11 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) -tdp_mmu_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); -else if (tdp_mmu_set_spte_atomic (kvm, &iter,SHADOW_NONPRESENT_VALUE )) +tdp_mmu_set_spte (kvm, &iter, +shadow_nonpresent_spte (iter.old_spte)); +else if (tdp_mmu_set_spte_atomic ( + kvm, &iter, +shadow_nonpresent_spte (iter.old_spte))) goto retry; } }@@ -1033,7 +1042,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)return false;__tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte, -SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 , +shadow_nonpresent_spte (old_spte), + sp->gfn, sp->role.level +1 , true, true,is_private_sp (sp));return true;@@ -1075,11 +1085,20 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,continue ; } +/* +* SPTE_SHARED_MASK is stored as 4K granularity. The +* information is lost if we delete upper level SPTE page. +* TODO: support large page. +*/ +if (kvm_gfn_shared_mask (kvm) && iter.level >PG_LEVEL_4K ) +continue ; +if (!is_shadow_present_pte (iter.old_spte) || !is_last_spte (iter.old_spte, iter.level))continue ; -tdp_mmu_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); +tdp_mmu_set_spte (kvm, &iter, +shadow_nonpresent_spte (iter.old_spte)); flush = true; }@@ -1195,18 +1214,44 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,gfn_t gfn_unalias = iter->gfn & ~kvm_gfn_shared_mask (vcpu->kvm);WARN_ON (sp->role.level != fault->goal_level); +WARN_ON (is_private_sptep (iter->sptep) != fault->is_private); -/* TDX shared GPAs are no executable, enforce this for the SDV. */ -if (kvm_gfn_shared_mask (vcpu->kvm) && !fault->is_private) - pte_access &= ~ACC_EXEC_MASK ; +if (kvm_gfn_shared_mask (vcpu->kvm)) { +if (fault->is_private) { +/* +* SPTE allows only RWX mapping. PFN can't be mapped it
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1232
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+* as READONLY in GPA. +*/ +if (fault->slot && !fault->map_writable) +return RET_PF_RETRY ; +/* +* This GPA is not allowed to map as private. Let +* vcpu loop in page fault until other vcpu change it +* by MapGPA hypercall. +*/ + if (fault->slot &&
Please consider to merge this if into above "if (fault->slot) {}"
Quoted 13 lines
arch/x86/kvm/mmu/tdp_mmu.c +11
-0+ spte_shared_mask(iter->old_spte)) +return RET_PF_RETRY ; + }else { +/* This GPA is not allowed to map as shared. */ +if (fault->slot && + !spte_shared_mask (iter->old_spte)) +return RET_PF_RETRY ; +/* TDX shared GPAs are no executable, enforce this. */ + pte_access &= ~ACC_EXEC_MASK ; + } + }if (unlikely (!fault->slot))
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1254
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +4
-1new_spte =make_mmio_spte (vcpu, gfn_unalias, pte_access); - else + else { wrprot =make_spte (vcpu, sp, fault->slot, pte_access, gfn_unalias, fault->pfn, iter->old_spte, fault->prefetch, true, fault->map_writable, &new_spte); +if (spte_shared_mask (iter->old_spte)) + new_spte |=SPTE_SHARED_MASK ; + }
The if can be eliminated:new_spte |= spte_shared_mask(iter->old_spte);
Quoted 15 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-2if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1509,7 +1554,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter,SHADOW_NONPRESENT_VALUE ); +tdp_mmu_set_spte (kvm, iter,shadow_nonpresent_spte (iter->old_spte));if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte, --2.25.1
PATCH v7 57/102 KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX Guest-Hypervisor communication interface(GHCI) specification defines MapGPA hypercall for guest TD to request the host VMM to map given GPA range as private or shared.
It means the guest TD uses the GPA as shared (or private). The GPA won't be used as private (or shared). VMM should enforce GPA usage. VMM doesn't have to map the GPA on the hypercall request.
- Allocate 4k PTE to record SPTE_SHARED_MASK bit.
- Zap the aliased region.
If shared (or private) GPA is requested, zap private (or shared) GPA (modulo shared bit).
- Record the request GPA is shared (or private) by SPTE_SHARED_MASK in SPTE in both shared and private EPT tables.
- With SPTE_SHARED_MASK set, a shared GPA is allowed.
- With SPTE_SHARED_MASK cleared, a private GPA is allowed.
The reason to record SPTE_SHARED_MASK in both shared and private EPT is to optimize EPT violation path for normal guest TD execution path and penalize map_gpa hypercall.
If the guest TD faults on not-allowed GPA (modulo shared bit), the KVM doesn't resolve EPT violation and let vcpu retry. vcpu will keep faulting until other vcpu maps the region with MapGPA hypercall. With the nonpresent value of spte(shadow_nonpresent_value), SPTE_SHARED_MASK is cleared. So the default behavior doesn't change.
- don't map GPA.
The GPA is mapped on the next EPT violation.
--- arch/x86/kvm/mmu.h | 3 + arch/x86/kvm/mmu/mmu.c | 106 +++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 271 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/mmu/tdp_mmu.h | 5 + 4 files changed, 382 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu.h +3 -0
@@ -225,6 +225,9 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);int kvm_arch_write_log_dirty (struct kvm_vcpu *vcpu); +int kvm_mmu_map_gpa (struct kvm_vcpu *vcpu,gfn_t *startp,gfn_t end, +bool allow_private); +int kvm_mmu_post_init_vm (struct kvm *kvm);void kvm_mmu_pre_destroy_vm (struct kvm *kvm);
arch/x86/kvm/mmu/mmu.c +106 -0
112 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +268 -3
301 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +5 -0
@@ -51,6 +51,11 @@ void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm, gfn_t start, gfn_t end, int target_level, bool shared); +int kvm_tdp_mmu_populate_nonleaf (struct kvm_vcpu *vcpu,gfn_t start,gfn_t end, +bool is_private,bool shared); +int kvm_tdp_mmu_map_gpa (struct kvm_vcpu *vcpu, +gfn_t *startp,gfn_t end,bool allow_private); +static inline void kvm_tdp_mmu_walk_lockless_begin (void ) {rcu_read_lock ();
Signature · 1 line
2.25.1
PATCH v7 58/102 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -163,6 +163,9 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) vcpu->arch.mmu->root_role.level); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level); +/* * Check if a given access (described through the I/D, W/R and U/S bits of a * page fault error code pfec) causes a permission fault with the given PTE
arch/x86/kvm/mmu/mmu.c +39 -0
@@ -4278,6 +4278,45 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return direct_page_fault (vcpu, fault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level) +{ +int r; +struct kvm_page_fault fault = (struct kvm_page_fault ) { + .addr = gpa, + .error_code = error_code, + .exec = error_code &PFERR_FETCH_MASK , + .write = error_code &PFERR_WRITE_MASK , + .present = error_code &PFERR_PRESENT_MASK , + .rsvd = error_code &PFERR_RSVD_MASK , + .user = error_code &PFERR_USER_MASK , + .prefetch = false, + .is_tdp = true, + .nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (), + .is_private =kvm_is_private_gpa (vcpu->kvm, gpa), + }; + +if (mmu_topup_memory_caches (vcpu, false)) +return KVM_PFN_ERR_FAULT ; + +/* +* Loop on the page fault path to handle the case where an mmu_notifier +* invalidation triggers RET_PF_RETRY. In the normal page fault path, +* KVM needs to resume the guest in case the invalidation changed any +* of the page fault properties, i.e. the gpa or error code. For this +* path, the gpa and error code are fixed by the caller, and the caller +* expects failure if and only if the page fault can't be fixed. +*/ +do { + fault.max_level = max_level; + fault.req_level =PG_LEVEL_4K ; + fault.goal_level =PG_LEVEL_4K ; + r =direct_page_fault (vcpu, &fault); + }while (r ==RET_PF_RETRY && !is_error_noslot_pfn (fault.pfn)); +return fault.pfn; +} +EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.25.1
PATCH v7 59/102 The start of TDX KVM patch series: TD finalization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD finalization.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -21,11 +21,11 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Not yet +* TD finalization: Applying * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied * KVM TDP refactoring for TDX: Applied * KVM TDP MMU hooks: Applied -* KVM TDP MMU MapGPA: Not yet +* KVM TDP MMU MapGPA: Applied
Signature · 1 line
2.25.1
PATCH v7 60/102 KVM: TDX: Create initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, private_page_add, handles it.
Define new subcommand, KVM_TDX_INIT_MEM_REGION, of VM-scoped KVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.
--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 135 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 155 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -539,6 +539,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -616,4 +617,12 @@ struct kvm_tdx_init_vm { }; }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -5285,6 +5285,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) out:return r; } +EXPORT_SYMBOL (kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) {
arch/x86/kvm/vmx/tdx.c +134 -1
172 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -25,6 +25,8 @@ struct kvm_tdx {u64 xfam;int hkid; +hpa_t source_pa; +bool finalized;atomic_t tdh_mem_track;
tools/arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -533,6 +533,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -610,4 +611,12 @@ struct kvm_tdx_init_vm { }; }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
PATCH v7 61/102 KVM: TDX: Finalize VM initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -540,6 +540,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -1230,6 +1230,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_td_initialized (kvm) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr.pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;
@@ -1249,6 +1267,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_INIT_MEM_REGION : r =tdx_init_mem_region (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
Signature · 1 line
2.25.1
PATCH v7 62/102 The start of TDX KVM patch series: TD vcpu enter/exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu enter/exit.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -12,6 +12,7 @@ What qemu can do - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. +- Qemu can finalize guest TD. Patch Layer status ------------------
@@ -21,8 +22,8 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Applying -* TD vcpu enter/exit: Not yet +* TD finalization: Applied +* TD vcpu enter/exit: Applying * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v7 63/102 KVM: TDX: Add helper assembly function to TDX vcpu
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines an API to run TDX vcpu with its own ABI. Define an assembly helper function to run TDX vcpu to hide the special ABI so that C code can call it with function call ABI.
--- arch/x86/kvm/vmx/vmenter.S | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+)
arch/x86/kvm/vmx/vmenter.S +146 -0
@@ -2,6 +2,7 @@ #include <linux/linkage.h> #include <asm/asm.h> #include <asm/bitsperlong.h> +#include <asm/errno.h> #include <asm/kvm_vcpu_regs.h> #include <asm/nospec-branch.h> #include <asm/segment.h>
@@ -28,6 +29,13 @@ #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE #endif +#ifdef CONFIG_INTEL_TDX_HOST +#define TDENTER 0 +#define EXIT_REASON_TDCALL 77 +#define TDENTER_ERROR_BIT 63 +#define seamcall .byte 0x66,0x0f,0x01,0xcf +#endif + .section .noinstr.text, "ax" /**
@@ -328,3 +336,141 @@ SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff) pop %_ASM_BP RET SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff) + +#ifdef CONFIG_INTEL_TDX_HOST + +.pushsection .noinstr.text, "ax" + +/** + * __tdx_vcpu_run - Call SEAMCALL(TDENTER) to run a TD vcpu + * @tdvpr: physical address of TDVPR + * @regs: void * (to registers of TDVCPU) + * @gpr_mask: non-zero if guest registers need to be loaded prior to TDENTER + * + * Returns: + * TD-Exit Reason + * + * Note: KVM doesn't support using XMM in its hypercalls, it's the HyperV + * code's responsibility to save/restore XMM registers on TDVMCALL. + */ +SYM_FUNC_START(__tdx_vcpu_run) + push %rbp + mov %rsp, %rbp + + push %r15 + push %r14 + push %r13 + push %r12 + push %rbx + + /* Save @regs, which is needed after TDENTER to capture output. */ + push %rsi + + /* Load @tdvpr to RCX */ + mov %rdi, %rcx + + /* No need to load guest GPRs if the last exit wasn't a TDVMCALL. */ + test %dx, %dx + je 1f + + /* Load @regs to RAX, which will be clobbered with $TDENTER anyways. */ + mov %rsi, %rax + + mov VCPU_RBX(%rax), %rbx + mov VCPU_RDX(%rax), %rdx + mov VCPU_RBP(%rax), %rbp + mov VCPU_RSI(%rax), %rsi + mov VCPU_RDI(%rax), %rdi + + mov VCPU_R8 (%rax), %r8 + mov VCPU_R9 (%rax), %r9 + mov VCPU_R10(%rax), %r10 + mov VCPU_R11(%rax), %r11 + mov VCPU_R12(%rax), %r12 + mov VCPU_R13(%rax), %r13 + mov VCPU_R14(%rax), %r14 + mov VCPU_R15(%rax), %r15 + + /* Load TDENTER to RAX. This kills the @regs pointer! */ +1: mov $TDENTER, %rax + +2: seamcall + + /* Skip to the exit path if TDENTER failed. */ + bt $TDENTER_ERROR_BIT, %rax + jc 4f + + /* Temporarily save the TD-Exit reason. */ + push %rax + + /* check if TD-exit due to TDVMCALL */ + cmp $EXIT_REASON_TDCALL, %ax + + /* Reload @regs to RAX. */ + mov 8(%rsp), %rax + + /* Jump on non-TDVMCALL */ + jne 3f + + /* Save all output from SEAMCALL(TDENTER) */ + mov %rbx, VCPU_RBX(%rax) + mov %rbp, VCPU_RBP(%rax) + mov %rsi, VCPU_RSI(%rax) + mov %rdi, VCPU_RDI(%rax) + mov %r10, VCPU_R10(%rax) + mov %r11, VCPU_R11(%rax) + mov %r12, VCPU_R12(%rax) + mov %r13, VCPU_R13(%rax) + mov %r14, VCPU_R14(%rax) + mov %r15, VCPU_R15(%rax) + +3: mov %rcx, VCPU_RCX(%rax) + mov %rdx, VCPU_RDX(%rax) + mov %r8, VCPU_R8 (%rax) + mov %r9, VCPU_R9 (%rax) + + /* + * Clear all general purpose registers except RSP and RAX to prevent + * speculative use of the guest's values. + */ + xor %rbx, %rbx + xor %rcx, %rcx + xor %rdx, %rdx + xor %rsi, %rsi + xor %rdi, %rdi + xor %rbp, %rbp + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + + /* Restore the TD-Exit reason to RAX for return. */ + pop %rax + + /* "POP" @regs. */ +4: add $8, %rsp + pop %rbx + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + pop %rbp + RET + +5: cmpb $0, kvm_rebooting + je 6f + mov $-EFAULT, %rax + jmp 4b +6: ud2 + _ASM_EXTABLE(2b, 5b) + +SYM_FUNC_END(__tdx_vcpu_run) + +.popsection + +#endif
Signature · 1 line
2.25.1
PATCH v7 64/102 KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on sched_in event as TDX doesn't support pause loop.
TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export perf_restore_debug_store.
--- arch/x86/kvm/vmx/main.c | 21 +++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 32 ++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 1 + 5 files changed, 87 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +19 -2
@@ -110,6 +110,23 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +/* Unconditionally continue to vcpu_run(). */ +return 1 ; + +return vmx_vcpu_pre_run (vcpu); +} + +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -222,8 +239,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .flush_tlb_gva = vt_flush_tlb_gva, .flush_tlb_guest = vt_flush_tlb_guest, - .vcpu_pre_run = vmx_vcpu_pre_run, - .vcpu_run = vmx_vcpu_run, + .vcpu_pre_run = vt_vcpu_pre_run, + .vcpu_run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +32 -0
@@ -10,6 +10,9 @@#include "vmx.h" #include "x86.h" +#include <trace/events/kvm.h> +#include "trace.h" +#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
@@ -552,6 +555,35 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); + + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +return EXIT_FASTPATH_NONE ; +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
arch/x86/kvm/vmx/tdx.h +33 -0
@@ -48,12 +48,45 @@ struct kvm_tdx {spinlock_t seamcall_lock; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +union tdx_exit_reason exit_reason; +bool initialized;/*
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -141,6 +141,7 @@ void tdx_vm_free(struct kvm *kvm);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -161,6 +162,7 @@ static inline void tdx_vm_free(struct kvm *kvm) {}static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
arch/x86/kvm/x86.c +1 -0
@@ -300,6 +300,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);static struct kmem_cache *x86_emulator_cache;
Signature · 1 line
2.25.1
PATCH v7 65/102 KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
--- arch/x86/kvm/vmx/main.c | 28 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 73 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +26 -2
@@ -110,6 +110,30 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_put (vcpu); + +return vmx_vcpu_put (vcpu); +} +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -206,9 +230,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_switch_to_guest = vmx_prepare_switch_to_guest, + .prepare_switch_to_guest = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,
arch/x86/kvm/vmx/tdx.c +39 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h>
@@ -463,6 +464,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; free_tdvpx:
@@ -476,6 +480,39 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return ret; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (!tdx->host_state_need_save) +return ; + +if (likely (is_64bit_mm (current->mm))) + tdx->msr_host_kernel_gs_base = current->thread.gsbase; +else + tdx->msr_host_kernel_gs_base =read_msr (MSR_KERNEL_GS_BASE ); + + tdx->host_state_need_save = false; +} + +static void tdx_prepare_switch_to_host (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + tdx->host_state_need_save = true; +if (!tdx->host_state_need_restore) +return ; + +wrmsrl (MSR_KERNEL_GS_BASE , tdx->msr_host_kernel_gs_base); + tdx->host_state_need_restore = false; +} + +void tdx_vcpu_put (struct kvm_vcpu *vcpu) +{ +vmx_vcpu_pi_put (vcpu); +tdx_prepare_switch_to_host (vcpu); +} +void tdx_vcpu_free (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -578,6 +615,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); + tdx->host_state_need_restore = true; + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX );
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -89,6 +89,10 @@ struct vcpu_tdx {bool initialized; +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; +/* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work.
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -142,6 +142,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -163,6 +165,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v7 66/102 KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
--- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kvm/vmx/tdx.c +18 -0
@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h"
@@ -592,6 +593,22 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & (kvm_caps.supported_xss |XFEATURE_MASK_PT ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
@@ -615,6 +632,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;
Signature · 1 line
2.25.1
PATCH v7 67/102 KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.
TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -2025,6 +2025,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {
arch/x86/kvm/x86.c +20 -5
@@ -425,6 +425,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();
@@ -439,15 +448,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.25.1
PATCH v7 68/102 KVM: TDX: restore user ret MSRs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3.
--- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
arch/x86/kvm/vmx/tdx.c +43 -0
@@ -593,6 +593,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK ,}, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
@@ -632,6 +654,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1470,6 +1493,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (WARN_ON_ONCE (x86_ops->tlb_remote_flush))return -EIO ; +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { +/* +* Here it checks if MSRs (tdx_uret_msrs) can be saved/restored +* before returning to user space. +* +* this_cpu_ptr(user_return_msrs)->registerd isn't checked +* because the registration is done at vcpu runtime by +* kvm_set_user_return_msr(). +* Here is setting up cpu feature before running vcpu, +* registered is alreays false. +*/ + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Signature · 1 line
2.25.1
PATCH v7 69/102 The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu exits, interrupts, and hypercalls.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -13,6 +13,7 @@ What qemu can do - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. - Qemu can finalize guest TD. +- Qemu can start to run vcpu. But vcpu can not make progress yet. Patch Layer status ------------------
@@ -23,7 +24,7 @@ Patch Layer status * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied * TD finalization: Applied -* TD vcpu enter/exit: Applying +* TD vcpu enter/exit: Applied * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v7 70/102 KVM: TDX: complete interrupts after tdexit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -593,6 +593,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
@@ -661,6 +669,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }
Signature · 1 line
2.25.1
PATCH v7 71/102 KVM: TDX: restore debug store when TD exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/intel/ds.c +1 -0
@@ -2256,3 +2256,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -663,6 +663,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache (); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Signature · 1 line
2.25.1
PATCH v7 72/102 KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VCMS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
--- arch/x86/kvm/vmx/main.c | 43 +++++++++++-- arch/x86/kvm/vmx/tdx.c | 121 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 6 ++ 4 files changed, 168 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +39 -4
@@ -17,6 +17,25 @@ static bool vt_is_vm_type_supported(unsigned long type) (enable_tdx &&tdx_is_vm_type_supported (type)); } +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret) +return ret; + +tdx_hardware_enable (); +return 0 ; +} + +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;
@@ -151,6 +170,14 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_load (vcpu, cpu); + +return vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -192,6 +219,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -214,8 +249,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vt_hardware_unsetup, .check_processor_compatibility = vmx_check_processor_compatibility, - .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_enable = vt_hardware_enable, + .hardware_disable = vt_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported,
@@ -231,7 +266,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,
@@ -317,7 +352,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
arch/x86/kvm/vmx/tdx.c +121 -0
166 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -85,6 +85,8 @@ struct vcpu_tdx {struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/x86_ops.h +6 -0
@@ -132,6 +132,8 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);bool tdx_is_vm_type_supported (unsigned long type);void tdx_hardware_unsetup (void ); +void tdx_hardware_enable (void ); +void tdx_hardware_disable (void );int tdx_dev_ioctl (void __user *argp);int tdx_vm_init (struct kvm *kvm);
@@ -144,6 +146,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -154,6 +157,8 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return 0 ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_enable (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline int tdx_dev_ioctl (void __user *argp) {return -EOPNOTSUPP ; };static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }
@@ -167,6 +172,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v7 73/102 KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a flag, KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.
Opportunistically convert the KVM_DEBUGREG_* definitions to use BIT().
--- arch/x86/include/asm/kvm_host.h | 9 +++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -2
@@ -556,8 +556,13 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers are saved/restored by hardware on exit from +* or enter guest. KVM needn't switch them. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -545,6 +545,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;
arch/x86/kvm/x86.c +8 -3
@@ -10326,7 +10326,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
@@ -10368,6 +10368,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
@@ -10380,8 +10381,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) { +if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 ); + } vcpu->arch.last_vmentry_cpu = vcpu->cpu; vcpu->arch.last_guest_tsc =kvm_read_l1_tsc (vcpu,rdtsc ());
Signature · 1 line
2.25.1
PATCH v7 74/102 KVM: TDX: Add support for find pending IRQ in a protected local APIC
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add flag and hook to KVM's local APIC management to support determining whether or not a TDX guest as a pending IRQ. For TDX vCPUs, the virtual APIC page is owned by the TDX module and cannot be accessed by KVM. As a result, registers that are virtualized by the CPU, e.g. PPR, cannot be read or written by KVM. To deliver interrupts for TDX guests, KVM must send an IRQ to the CPU on the posted interrupt notification vector. And to determine if TDX vCPU has a pending interrupt, KVM must check if there is an outstanding notification.
Return "no interrupt" in kvm_apic_has_interrupt() if the guest APIC is protected to short-circuit the various other flows that try to pull an IRQ out of the vAPIC, the only valid operation is querying _if_ an IRQ is pending, KVM can't do anything based on _which_ IRQ is pending.
Intentionally omit sanity checks from other flows, e.g. PPR update, so as not to degrade non-TDX guests with unecessary checks. A well-behaved KVM and userspace will never reach those flows for TDX guests, but reaching them is not fatal if something does go awry.
Note, this doesn't handle interrupts that have been delivered to the vCPU but not yet recognized by the core, i.e. interrupts that are sitting in vmcs.GUEST_INTR_STATUS. Querying that state requires a SEAMCALL and will be supported in a future patch.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq.c | 3 +++ arch/x86/kvm/lapic.c | 3 +++ arch/x86/kvm/lapic.h | 2 ++ arch/x86/kvm/vmx/main.c | 11 +++++++++++ arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 8 files changed, 29 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -112,6 +112,7 @@ KVM_X86_OP_OPTIONAL(pi_update_irte) KVM_X86_OP_OPTIONAL(pi_start_assignment) KVM_X86_OP_OPTIONAL(apicv_post_state_restore) KVM_X86_OP_OPTIONAL_RET0(dy_apicv_has_pending_interrupt) +KVM_X86_OP_OPTIONAL(protected_apic_has_interrupt) KVM_X86_OP_OPTIONAL(set_hv_timer) KVM_X86_OP_OPTIONAL(cancel_hv_timer) KVM_X86_OP(setup_mce)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1614,6 +1614,7 @@ struct kvm_x86_ops {void (*pi_start_assignment)(struct kvm *kvm);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*protected_apic_has_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/irq.c +3 -0
@@ -100,6 +100,9 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)if (kvm_cpu_has_extint (v))return 1 ; +if (lapic_in_kernel (v) && v->arch.apic->guest_apic_protected) +return static_call (kvm_x86_protected_apic_has_interrupt)(v); +return kvm_apic_has_interrupt (v) !=-1 ;/* LAPIC */ }EXPORT_SYMBOL_GPL (kvm_cpu_has_interrupt);
arch/x86/kvm/lapic.c +3 -0
@@ -2585,6 +2585,9 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)if (!kvm_apic_present (vcpu))return -1 ; +if (apic->guest_apic_protected) +return -1 ; +__apic_update_ppr (apic, &ppr);return apic_has_interrupt_for_ppr (apic, ppr); }
arch/x86/kvm/lapic.h +2 -0
@@ -51,6 +51,8 @@ struct kvm_lapic {bool sw_enabled;bool irr_pending;bool lvt0_in_nmi_mode; +/* Select registers in the vAPIC cannot be read/written. */ +bool guest_apic_protected;/* Number of bits set in ISR. */ s16 isr_count;/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
arch/x86/kvm/vmx/main.c +11 -0
@@ -46,6 +46,9 @@ static __init int vt_hardware_setup(void) enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); +if (!enable_tdx) + vt_x86_ops.protected_apic_has_interrupt =NULL ; +if (enable_ept)kvm_mmu_set_ept_masks (enable_ept_ad_bits,cpu_has_vmx_ept_execute_only ());
@@ -178,6 +181,13 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static bool vt_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ +KVM_BUG_ON (!is_td_vcpu (vcpu), vcpu->kvm); + +return tdx_protected_apic_has_interrupt (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -329,6 +339,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, + .protected_apic_has_interrupt = vt_protected_apic_has_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr,
arch/x86/kvm/vmx/tdx.c +6 -0
@@ -526,6 +526,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return -EINVAL ;fpstate_set_confidential (&vcpu->arch.guest_fpu); + vcpu->arch.apic->guest_apic_protected = true; ret =tdx_alloc_td_page (&tdx->tdvpr);if (ret)
@@ -590,6 +591,11 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)local_irq_enable (); } +bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ +return pi_has_pending_interrupt (vcpu); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -147,6 +147,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -173,6 +174,7 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) { return EXIT_FASTPstatic inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {} +static inline bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) {return false; }static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v7 75/102 KVM: x86: Assume timer IRQ was injected if APIC state is proteced
In reply to [email protected]From: Sean Christopherson <[email protected]>
If APIC state is protected, i.e. the vCPU is a TDX guest, assume a timer IRQ was injected when deciding whether or not to busy wait in the "timer advanced" path. The "real" vIRR is not readable/writable, so trying to query for a pending timer IRQ will return garbage.
Note, TDX can scour the PIR if it wants to be more precise and skip the "wait" call entirely.
--- arch/x86/kvm/lapic.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +10 -1
@@ -1578,8 +1578,17 @@ static void apic_update_lvtt(struct kvm_lapic *apic)static bool lapic_timer_int_injected (struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic; -u32 reg =kvm_lapic_get_reg (apic,APIC_LVTT ); +u32 reg; +/* +* Assume a timer IRQ was "injected" if the APIC is protected. KVM's +* copy of the vIRR is bogus, it's the responsibility of the caller to +* precisely check whether or not a timer IRQ is pending. +*/ +if (apic->guest_apic_protected) +return true; + + reg =kvm_lapic_get_reg (apic,APIC_LVTT );if (kvm_apic_hw_enabled (apic)) {int vec = reg &APIC_VECTOR_MASK ;void *bitmap = apic->regs +APIC_ISR ;
Signature · 1 line
2.25.1
PATCH v7 76/102 KVM: TDX: remove use of struct vcpu_vmx from posted_interrupt.c
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As TDX will use posted_interrupt.c, the use of struct vcpu_vmx is a blocker. Because the members of struct pi_desc pi_desc and struct list_head pi_wakeup_list are only used in posted_interrupt.c, introduce common structure, struct vcpu_pi, make vcpu_vmx and vcpu_tdx has same layout in the top of structure.
To minimize the diff size, avoid code conversion like, vmx->pi_desc => vmx->common->pi_desc. Instead add compile time check if the layout is expected.
--- arch/x86/kvm/vmx/posted_intr.c | 41 ++++++++++++++++++++++++++-------- arch/x86/kvm/vmx/posted_intr.h | 11 +++++++++ arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/vmx/tdx.h | 8 +++++++ arch/x86/kvm/vmx/vmx.h | 14 +++++++----- 5 files changed, 60 insertions(+), 15 deletions(-)
arch/x86/kvm/vmx/posted_intr.c +32 -9
@@ -9,6 +9,7 @@#include "posted_intr.h" #include "trace.h" #include "vmx.h" +#include "tdx.h" /* * Maintain a per-CPU list of vCPUs that need to be awakened by wakeup_handler()
@@ -29,9 +30,29 @@ static DEFINE_PER_CPU(struct list_head, wakeup_vcpus_on_cpu);*/ static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock); +/* +* The layout of the head of struct vcpu_vmx and struct vcpu_tdx must match with +* struct vcpu_pi. +*/ +static_assert (offsetof(struct vcpu_pi , pi_desc) == + offsetof(struct vcpu_vmx , pi_desc)); +static_assert (offsetof(struct vcpu_pi , pi_wakeup_list) == + offsetof(struct vcpu_vmx , pi_wakeup_list)); +#ifdef CONFIG_INTEL_TDX_HOST +static_assert (offsetof(struct vcpu_pi , pi_desc) == + offsetof(struct vcpu_tdx , pi_desc)); +static_assert (offsetof(struct vcpu_pi , pi_wakeup_list) == + offsetof(struct vcpu_tdx , pi_wakeup_list)); +#endif + +static inline struct vcpu_pi *vcpu_to_pi (struct kvm_vcpu *vcpu) +{ +return (struct vcpu_pi *)vcpu; +} +static inline struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) { -return &(to_vmx (vcpu)->pi_desc); +return &vcpu_to_pi (vcpu)->pi_desc; }static int pi_try_set_control (struct pi_desc *pi_desc,u64 old,u64 new)
@@ -50,8 +71,8 @@ static int pi_try_set_control(struct pi_desc *pi_desc, u64 old, u64 new)void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu,int cpu) { -struct pi_desc *pi_desc =vcpu_to_pi_desc (vcpu); -struct vcpu_vmx *vmx =to_vmx (vcpu); +struct vcpu_pi *vcpu_pi =vcpu_to_pi (vcpu); +struct pi_desc *pi_desc = &vcpu_pi->pi_desc;struct pi_desc old, new;unsigned long flags;unsigned int dest;
@@ -88,7 +109,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)*/ if (pi_desc->nv ==POSTED_INTR_WAKEUP_VECTOR ) {raw_spin_lock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); -list_del (&vmx->pi_wakeup_list); +list_del (&vcpu_pi->pi_wakeup_list);raw_spin_unlock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); }
@@ -142,15 +163,15 @@ static bool vmx_can_use_vtd_pi(struct kvm *kvm)*/ static void pi_enable_wakeup_handler (struct kvm_vcpu *vcpu) { -struct pi_desc *pi_desc =vcpu_to_pi_desc (vcpu); -struct vcpu_vmx *vmx =to_vmx (vcpu); +struct vcpu_pi *vcpu_pi =vcpu_to_pi (vcpu); +struct pi_desc *pi_desc = &vcpu_pi->pi_desc;struct pi_desc old, new;unsigned long flags;local_irq_save (flags);raw_spin_lock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); -list_add_tail (&vmx->pi_wakeup_list, +list_add_tail (&vcpu_pi->pi_wakeup_list, &per_cpu (wakeup_vcpus_on_cpu, vcpu->cpu));raw_spin_unlock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu));
@@ -187,7 +208,8 @@ static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)* notification vector is switched to the one that calls * back to the pi_wakeup_handler() function. */ -return vmx_can_use_ipiv (vcpu) ||vmx_can_use_vtd_pi (vcpu->kvm); +return (vmx_can_use_ipiv (vcpu) && !is_td_vcpu (vcpu)) || +vmx_can_use_vtd_pi (vcpu->kvm); }void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu)
@@ -197,7 +219,8 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)if (!vmx_needs_pi_wakeup (vcpu))return ; -if (kvm_vcpu_is_blocking (vcpu) && !vmx_interrupt_blocked (vcpu)) +if (kvm_vcpu_is_blocking (vcpu) && + (is_td_vcpu (vcpu) || !vmx_interrupt_blocked (vcpu)))pi_enable_wakeup_handler (vcpu);/*
arch/x86/kvm/vmx/posted_intr.h +11 -0
@@ -94,6 +94,17 @@ static inline bool pi_test_sn(struct pi_desc *pi_desc) (unsigned long *)&pi_desc->control); } +struct vcpu_pi { +struct kvm_vcpu vcpu; + +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here common layout betwwn vcpu_vmx and vcpu_tdx. */ +}; +void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu,int cpu);void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu);void pi_wakeup_handler (void );
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -527,6 +527,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)fpstate_set_confidential (&vcpu->arch.guest_fpu); vcpu->arch.apic->guest_apic_protected = true; +INIT_LIST_HEAD (&tdx->pi_wakeup_list); ret =tdx_alloc_td_page (&tdx->tdvpr);if (ret)
arch/x86/kvm/vmx/tdx.h +8 -0
@@ -4,6 +4,7 @@#ifdef CONFIG_INTEL_TDX_HOST +#include "posted_intr.h" #include "pmu_intel.h" #include "tdx_ops.h"
@@ -82,6 +83,13 @@ union tdx_exit_reason {struct vcpu_tdx {struct kvm_vcpu vcpu; +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here same layout to struct vcpu_pi. */ +struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx;
arch/x86/kvm/vmx/vmx.h +8 -6
@@ -226,6 +226,14 @@ struct nested_vmx {struct vcpu_vmx {struct kvm_vcpu vcpu; + +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here same layout to struct vcpu_pi. */ +u8 fail;u8 x2apic_msr_bitmap_mode;
@@ -295,12 +303,6 @@ struct vcpu_vmx {union vmx_exit_reason exit_reason; -/* Posted interrupt descriptor */ -struct pi_desc pi_desc; - -/* Used if this vCPU is waiting for PI notification wakeup. */ -struct list_head pi_wakeup_list; -/* Support for a guest hypervisor (nested VMX) */ struct nested_vmx nested;
Signature · 1 line
2.25.1
PATCH v7 77/102 KVM: TDX: Implement interrupt injection
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX supports interrupt inject into vcpu with posted interrupt. Wire up the corresponding kvm x86 operations to posted interrupt. Move kvm_vcpu_trigger_posted_interrupt() from vmx.c to common.h to share the code.
VMX can inject interrupt by setting interrupt information field, VM_ENTRY_INTR_INFO_FIELD, of VMCS. TDX supports interrupt injection only by posted interrupt. Ignore the execution path to access VM_ENTRY_INTR_INFO_FIELD.
As cpu state is protected and apicv is enabled for the TDX guest, VMM can inject interrupt by updating posted interrupt descriptor. Treat interrupt can be injected always.
--- arch/x86/kvm/vmx/common.h | 71 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/main.c | 92 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/posted_intr.c | 2 +- arch/x86/kvm/vmx/posted_intr.h | 2 + arch/x86/kvm/vmx/tdx.c | 25 +++++++++ arch/x86/kvm/vmx/vmx.c | 67 +------------------------ arch/x86/kvm/vmx/x86_ops.h | 7 ++- 7 files changed, 189 insertions(+), 77 deletions(-)
arch/x86/kvm/vmx/common.h +71 -0
@@ -4,6 +4,7 @@#include <linux/kvm_host.h> +#include "posted_intr.h" #include "mmu.h" static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,
@@ -30,4 +31,74 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } +static inline void kvm_vcpu_trigger_posted_interrupt (struct kvm_vcpu *vcpu, +int pi_vec) +{ +#ifdef CONFIG_SMP +if (vcpu->mode ==IN_GUEST_MODE ) { +/* +* The vector of the virtual has already been set in the PIR. +* Send a notification event to deliver the virtual interrupt +* unless the vCPU is the currently running vCPU, i.e. the +* event is being sent from a fastpath VM-Exit handler, in +* which case the PIR will be synced to the vIRR before +* re-entering the guest. +* +* When the target is not the running vCPU, the following +* possibilities emerge: +* +* Case 1: vCPU stays in non-root mode. Sending a notification +* event posts the interrupt to the vCPU. +* +* Case 2: vCPU exits to root mode and is still runnable. The +* PIR will be synced to the vIRR before re-entering the guest. +* Sending a notification event is ok as the host IRQ handler +* will ignore the spurious event. +* +* Case 3: vCPU exits to root mode and is blocked. vcpu_block() +* has already synced PIR to vIRR and never blocks the vCPU if +* the vIRR is not empty. Therefore, a blocked vCPU here does +* not wait for any requested interrupts in PIR, and sending a +* notification event also results in a benign, spurious event. +*/ + +if (vcpu !=kvm_get_running_vcpu ()) + apic->send_IPI_mask (get_cpu_mask (vcpu->cpu), pi_vec); +return ; + } +#endif +/* +* The vCPU isn't in the guest; wake the vCPU in case it is blocking, +* otherwise do nothing as KVM will grab the highest priority pending +* IRQ via ->sync_pir_to_irr() in vcpu_enter_guest(). +*/ +kvm_vcpu_wake_up (vcpu); +} + +/* +* Send interrupt to vcpu via posted interrupt way. +* 1. If target vcpu is running(non-root mode), send posted interrupt +* notification to vcpu and hardware will sync PIR to vIRR atomically. +* 2. If target vcpu isn't running(root mode), kick it to pick up the +* interrupt from PIR in next vmentry. +*/ +static inline void __vmx_deliver_posted_interrupt ( +struct kvm_vcpu *vcpu,struct pi_desc *pi_desc,int vector) +{ +if (pi_test_and_set_pir (vector, pi_desc)) +return ; + +/* If a previous notification has sent the IPI, nothing to do. */ +if (pi_test_and_set_on (pi_desc)) +return ; + +/* +* The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() +* after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is +* guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a +* posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. +*/ +kvm_vcpu_trigger_posted_interrupt (vcpu,POSTED_INTR_VECTOR ); +} +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/main.c +83 -9
126 lines not on this page · raw message
arch/x86/kvm/vmx/posted_intr.c +1 -1
@@ -50,7 +50,7 @@ static inline struct vcpu_pi *vcpu_to_pi(struct kvm_vcpu *vcpu)return (struct vcpu_pi *)vcpu; } -static inline struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) +struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) {return &vcpu_to_pi (vcpu)->pi_desc; }
arch/x86/kvm/vmx/posted_intr.h +2 -0
@@ -105,6 +105,8 @@ struct vcpu_pi {/* Until here common layout betwwn vcpu_vmx and vcpu_tdx. */ }; +struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu); +void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu,int cpu);void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu);void pi_wakeup_handler (void );
arch/x86/kvm/vmx/tdx.c +25 -0
@@ -7,6 +7,7 @@#include "capabilities.h" #include "x86_ops.h" +#include "common.h" #include "mmu.h" #include "tdx.h" #include "vmx.h"
@@ -556,6 +557,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->pi_desc.nv =POSTED_INTR_VECTOR ; + tdx->pi_desc.sn =1 ; + tdx->host_state_need_save = true; tdx->host_state_need_restore = false;
@@ -576,6 +580,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {struct vcpu_tdx *tdx =to_tdx (vcpu); +vmx_vcpu_pi_load (vcpu, cpu);if (vcpu->cpu == cpu)return ;
@@ -789,6 +794,12 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)trace_kvm_entry (vcpu); +if (pi_test_on (&tdx->pi_desc)) { + apic->send_IPI_self (POSTED_INTR_VECTOR ); + +kvm_wait_lapic_expire (vcpu); + } +tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache ();
@@ -1126,6 +1137,16 @@ static void tdx_handle_changed_private_spte( } } +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector) +{ +struct kvm_vcpu *vcpu = apic->vcpu; +struct vcpu_tdx *tdx =to_tdx (vcpu); + +/* TDX supports only posted interrupt. No lapic emulation. */ +__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); +} +int tdx_dev_ioctl (void __user *argp) {struct kvm_tdx_capabilities __user *user_caps;
@@ -1562,6 +1583,10 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)return -EIO ; } +td_vmcs_write16 (tdx,POSTED_INTR_NV ,POSTED_INTR_VECTOR ); +td_vmcs_write64 (tdx,POSTED_INTR_DESC_ADDR ,__pa (&tdx->pi_desc)); +td_vmcs_setbit32 (tdx,PIN_BASED_VM_EXEC_CONTROL ,PIN_BASED_POSTED_INTR ); + tdx->initialized = true;return 0 ; }
arch/x86/kvm/vmx/vmx.c +1 -66
85 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +6 -1
@@ -53,7 +53,6 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu,bool vmx_apic_init_signal_blocked (struct kvm_vcpu *vcpu);void vmx_migrate_timers (struct kvm_vcpu *vcpu);void vmx_set_virtual_apic_mode (struct kvm_vcpu *vcpu); -void vmx_apicv_post_state_restore (struct kvm_vcpu *vcpu);bool vmx_check_apicv_inhibit_reasons (enum kvm_apicv_inhibit reason);void vmx_hwapic_irr_update (struct kvm_vcpu *vcpu,int max_irr);void vmx_hwapic_isr_update (struct kvm_vcpu *vcpu,int max_isr);
@@ -149,6 +148,9 @@ void tdx_vcpu_put(struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu); +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -176,6 +178,9 @@ static inline void tdx_vcpu_put(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) {return false; } +static inline void tdx_deliver_interrupt ( +struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v7 78/102 KVM: TDX: Implements vcpu request_immediate_exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now we are able to inject interrupts into TDX vcpu, it's ready to block TDX vcpu. Wire up kvm x86 methods for blocking/unblocking vcpu for TDX. To unblock on pending events, request immediate exit methods is also needed.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -311,6 +311,14 @@ static void vt_enable_irq_window(struct kvm_vcpu *vcpu)vmx_enable_irq_window (vcpu); } +static void vt_request_immediate_exit (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return __kvm_request_immediate_exit (vcpu); + +vmx_request_immediate_exit (vcpu); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -435,7 +443,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff, - .request_immediate_exit = vmx_request_immediate_exit, + .request_immediate_exit = vt_request_immediate_exit, .sched_in = vt_sched_in,
Signature · 1 line
2.25.1