TDX/SNP part 1 of n, for 6.9
Subsystems (6)
Paolo Bonzini
maintainercover TDX/SNP part 1 of n, for 6.9
This is a first set of, hopefully non-controversial patches from the SNP and TDX series. They cover mostly changes to generic code and new gmem APIs, and in general have already been reviewed when posted by Isaku and Michael.
One important change is that the gmem hook for initializing memory is designed to return -EEXIST if the page already exists in the guestmemfd filemap. The idea is that the special case of KVM_SEV_SNP_LAUNCH_UPDATE, where __kvm_gmem_get_pfn() is used to return an uninitialized page and make it guest-owned, can be be done at most once per page unless the ioctl fails.
Of course these patches add a bunch of dead code. This is intentional because it's the only way to trim the large TDX (and to some extent SNP) series to the point that it's possible to discuss them. The next step is probably going to be the private<->shared page logic from the TDX series.
Paolo
Isaku Yamahata (5):
KVM: x86/mmu: Add Suppress VE bit to EPT
shadow_mmio_mask/shadow_present_mask
KVM: VMX: Introduce test mode related to EPT violation VE
KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at
allocation
KVM: x86/tdp_mmu: Sprinkle __must_check
KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
Michael Roth (2):
KVM: x86: Add gmem hook for invalidating memory
KVM: x86: Add gmem hook for determining max NPT mapping level
Paolo Bonzini (6):
KVM: x86/mmu: pass error code back to MMU when async pf is ready
KVM: x86/mmu: Use PFERR_GUEST_ENC_MASK to indicate fault is private
KVM: guest_memfd: pass error up from filemap_grab_folio
filemap: add FGP_CREAT_ONLY
KVM: x86: Add gmem hook for initializing memory
KVM: guest_memfd: add API to undo kvm_gmem_get_uninit_pfn
Sean Christopherson (7):
KVM: x86: Split core of hypercall emulation to helper function
KVM: Allow page-sized MMU caches to be initialized with custom 64-bit
values
KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed
SPTE
KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
KVM: VMX: Modify NMI and INTR handlers to take intr_info as function
argument
Tom Lendacky (1):
KVM: SEV: Use a VMSA physical address variable for populating VMCB
arch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 12 + arch/x86/include/asm/vmx.h | 13 + arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 55 ++-- arch/x86/kvm/mmu/mmu_internal.h | 6 +- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 4 +- arch/x86/kvm/mmu/spte.c | 16 +- arch/x86/kvm/mmu/spte.h | 21 +- arch/x86/kvm/mmu/tdp_iter.h | 12 + arch/x86/kvm/mmu/tdp_mmu.c | 74 +++-- arch/x86/kvm/svm/sev.c | 3 +- arch/x86/kvm/svm/svm.c | 9 +- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 168 +++++++++++ arch/x86/kvm/vmx/vmcs.h | 5 + arch/x86/kvm/vmx/vmx.c | 460 +++++++++++------------------ arch/x86/kvm/vmx/vmx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 124 ++++++++ arch/x86/kvm/x86.c | 69 +++-- include/linux/kvm_host.h | 25 ++ include/linux/kvm_types.h | 1 + include/linux/pagemap.h | 2 + mm/filemap.c | 4 + virt/kvm/Kconfig | 8 + virt/kvm/guest_memfd.c | 120 +++++++- virt/kvm/kvm_main.c | 16 +- 29 files changed, 855 insertions(+), 387 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.h
Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 1/21 KVM: x86: Split core of hypercall emulation to helper function
From: Sean Christopherson <[email protected]>
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX.
Message-Id: <5134caa55ac3dec33fb2addb5545b52b3b52db02.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 56 ++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 18 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -2133,6 +2133,10 @@ static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,kvm_set_or_clear_apicv_inhibit (kvm, reason, false); } +unsigned long __kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3, +int op_64_bit,int cpl);int kvm_emulate_hypercall (struct kvm_vcpu *vcpu); int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
arch/x86/kvm/x86.c +38 -18
89 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH 01/21] KVM: x86: Split core of hypercall emulation to helper function
Quoted 9 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5134caa55ac3dec33fb2addb5545b52b3b52db02.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Re: [PATCH 01/21] KVM: x86: Split core of hypercall emulation to helper function
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5134caa55ac3dec33fb2addb5545b52b3b52db02.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 116 lines
--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 56 ++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 18 deletions(-)arch/x86/include/asm/kvm_host.h +4
-0@@ -2133,6 +2133,10 @@ static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,kvm_set_or_clear_apicv_inhibit (kvm, reason, false); } +unsigned long __kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3, +int op_64_bit,int cpl);int kvm_emulate_hypercall (struct kvm_vcpu *vcpu); int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,arch/x86/kvm/x86.c +38
-18@@ -10046,26 +10046,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); } -int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +unsigned long __kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3, +int op_64_bit,int cpl) { -unsigned long nr, a0, a1, a2, a3, ret; -int op_64_bit; - -if (kvm_xen_hypercall_enabled (vcpu->kvm)) -return kvm_xen_hypercall (vcpu); - -if (kvm_hv_hypercall_enabled (vcpu)) -return kvm_hv_hypercall (vcpu); - - nr =kvm_rax_read (vcpu); - a0 =kvm_rbx_read (vcpu); - a1 =kvm_rcx_read (vcpu); - a2 =kvm_rdx_read (vcpu); - a3 =kvm_rsi_read (vcpu); +unsigned long ret;trace_kvm_hypercall (nr, a0, a1, a2, a3); - op_64_bit =is_64_bit_hypercall (vcpu);if (!op_64_bit) { nr &=0xFFFFFFFF ; a0 &=0xFFFFFFFF ;@@ -10074,7 +10063,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &=0xFFFFFFFF ; } -if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { +if (cpl) { ret = -KVM_EPERM ; goto out; }@@ -10135,18 +10124,49 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)WARN_ON_ONCE (vcpu->run->hypercall.flags &KVM_EXIT_HYPERCALL_MBZ ); vcpu->arch.complete_userspace_io = complete_hypercall_exit; +/* stat is incremented on completion. */ return 0 ; }default : ret = -KVM_ENOSYS ;break ; } + out: + ++vcpu->stat.hypercalls; +return ret; +} +EXPORT_SYMBOL_GPL (__kvm_emulate_hypercall); + +int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +{ +unsigned long nr, a0, a1, a2, a3, ret; +int op_64_bit; +int cpl; + +if (kvm_xen_hypercall_enabled (vcpu->kvm)) +return kvm_xen_hypercall (vcpu); + +if (kvm_hv_hypercall_enabled (vcpu)) +return kvm_hv_hypercall (vcpu); + + nr =kvm_rax_read (vcpu); + a0 =kvm_rbx_read (vcpu); + a1 =kvm_rcx_read (vcpu); + a2 =kvm_rdx_read (vcpu); + a3 =kvm_rsi_read (vcpu); + op_64_bit =is_64_bit_hypercall (vcpu); + cpl =static_call (kvm_x86_get_cpl)(vcpu); + + ret =__kvm_emulate_hypercall (vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl); +if (nr ==KVM_HC_MAP_GPA_RANGE && !ret) +/* MAP_GPA tosses the request to the user space. */ +return 0 ; +if (!op_64_bit) ret = (u32 )ret;kvm_rax_write (vcpu, ret); - ++vcpu->stat.hypercalls;return kvm_skip_emulated_instruction (vcpu); }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall);
Paolo Bonzini
maintainerPATCH 2/21 KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).
Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com>
--- include/linux/kvm_types.h | 1 + virt/kvm/kvm_main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/kvm_types.h +1 -0
@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache {struct kvm_mmu_memory_cache {gfp_t gfp_zero;gfp_t gfp_custom; +u64 init_value;struct kmem_cache *kmem_cache;int capacity;int nobjs;
virt/kvm/kvm_main.c +14 -2
@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj (struct kvm_mmu_memory_cache *mc,gfp_t gfp_flags) { +void *page; + gfp_flags |= mc->gfp_zero;if (mc->kmem_cache)return kmem_cache_alloc (mc->kmem_cache, gfp_flags); -else -return (void *)__get_free_page (gfp_flags); + + page = (void *)__get_free_page (gfp_flags); +if (page && mc->init_value) +memset64 (page, mc->init_value,PAGE_SIZE /sizeof (mc->init_value)); +return page; }int __kvm_mmu_topup_memory_cache (struct kvm_mmu_memory_cache *mc,int capacity,int min)
@@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity,if (WARN_ON_ONCE (!capacity))return -EIO ; +/* +* Custom init values can be used only for page allocations, +* and obviously conflict with __GFP_ZERO. +*/ +if (WARN_ON_ONCE (mc->init_value && (mc->kmem_cache || mc->gfp_zero))) +return -EIO ; + mc->objects =kvmalloc_array (capacity,sizeof (void *), gfp);if (!mc->objects)return -ENOMEM ;
Signature · 1 line
2.39.0
Re: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Quoted 10 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).
Quoted 4 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 55 lines
--- include/linux/kvm_types.h | 1 + virt/kvm/kvm_main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)include/linux/kvm_types.h +1
-0@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache {struct kvm_mmu_memory_cache {gfp_t gfp_zero;gfp_t gfp_custom; +u64 init_value;struct kmem_cache *kmem_cache;int capacity;int nobjs;virt/kvm/kvm_main.c +14
-2@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj (struct kvm_mmu_memory_cache *mc,gfp_t gfp_flags) { +void *page; + gfp_flags |= mc->gfp_zero;if (mc->kmem_cache)return kmem_cache_alloc (mc->kmem_cache, gfp_flags); -else -return (void *)__get_free_page (gfp_flags); + + page = (void *)__get_free_page (gfp_flags); +if (page && mc->init_value) +memset64 (page, mc->init_value,PAGE_SIZE /sizeof (mc->init_value)); +return page; }int __kvm_mmu_topup_memory_cache (struct kvm_mmu_memory_cache *mc,int capacity,int min)@@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity,if (WARN_ON_ONCE (!capacity))return -EIO ; +/* +* Custom init values can be used only for page allocations, +* and obviously conflict with __GFP_ZERO. +*/ +if (WARN_ON_ONCE (mc->init_value && (mc->kmem_cache || mc->gfp_zero))) +return -EIO ; + mc->objects =kvmalloc_array (capacity,sizeof (void *), gfp);if (!mc->objects)return -ENOMEM ;
Re: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
In reply to Paolo BonziniQuoted 10 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).
Quoted 8 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/kvm_types.h | 1 + virt/kvm/kvm_main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)
Quoted 51 lines
include/linux/kvm_types.h +1
-0@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache {struct kvm_mmu_memory_cache {gfp_t gfp_zero;gfp_t gfp_custom; +u64 init_value;struct kmem_cache *kmem_cache;int capacity;int nobjs;virt/kvm/kvm_main.c +14
-2@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj (struct kvm_mmu_memory_cache *mc,gfp_t gfp_flags) { +void *page; + gfp_flags |= mc->gfp_zero;if (mc->kmem_cache)return kmem_cache_alloc (mc->kmem_cache, gfp_flags); -else -return (void *)__get_free_page (gfp_flags); + + page = (void *)__get_free_page (gfp_flags); +if (page && mc->init_value) +memset64 (page, mc->init_value,PAGE_SIZE /sizeof (mc->init_value)); +return page; }int __kvm_mmu_topup_memory_cache (struct kvm_mmu_memory_cache *mc,int capacity,int min)@@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity,if (WARN_ON_ONCE (!capacity))return -EIO ; +/* +* Custom init values can be used only for page allocations, +* and obviously conflict with __GFP_ZERO. +*/ +if (WARN_ON_ONCE (mc->init_value && (mc->kmem_cache || mc->gfp_zero))) +return -EIO ; + mc->objects =kvmalloc_array (capacity,sizeof (void *), gfp);if (!mc->objects)return -ENOMEM ;
Re: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Quoted 49 lines · Binbin Wu
Quoted 10 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).
Quoted 8 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>---
include/linux/kvm_types.h| 1 +
virt/kvm/kvm_main.c | 16 ++++++++++++++--2 files changed, 15 insertions(+), 2 deletions(-)Reviewed-by Binbin Wu <[email protected]>Quoted 25 lines
include/linux/kvm_types.h +1
-0@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache { struct kvm_mmu_memory_cache { gfp_t gfp_zero; gfp_t gfp_custom; + u64 init_value; struct kmem_cache *kmem_cache; int capacity; int nobjs;virt/kvm/kvm_main.c +2
-0@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm) static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc, gfp_t gfp_flags) { + void *page; + gfp_flags |= mc->gfp_zero; if (mc->kmem_cache)
Quoted 8 lines
virt/kvm/kvm_main.c static void kvm_flush_shadow_all(struct kvm *kvm)
Quoted 8 lines
virt/kvm/kvm_main.c +4
-2return kmem_cache_alloc(mc->kmem_cache, gfp_flags); - else - return (void *)__get_free_page(gfp_flags); + + page = (void *)__get_free_page(gfp_flags); + if (page && mc->init_value) + memset64(page, mc->init_value, PAGE_SIZE /sizeof (mc->init_value));
Do we need a static_assert() to make sure mc->init_value is 64bit?
Quoted 20 lines
Quoted 18 lines
virt/kvm/kvm_main.c +1
-0+ return page; } int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min)@@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, if (WARN_ON_ONCE(!capacity)) return -EIO;+ /*
+ * Custom init values can be used only for page allocations, + * and obviously conflict with__GFP_ZERO.
+ */
+ if (WARN_ON_ONCE(mc->init_value && (mc->kmem_cache || mc->gfp_zero)))
+ return-EIO;
+
mc->objects=kvmalloc_array(capacity, sizeof(void *), gfp); if (!mc->objects)
return-ENOMEM;
Re: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Quoted 54 lines · Binbin Wu
Quoted 53 lines · Binbin Wu
Quoted 11 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs
to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).Quoted 8 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>---
include/linux/kvm_types.h| 1 +
virt/kvm/kvm_main.c | 16 ++++++++++++++--2 files changed, 15 insertions(+), 2 deletions(-)Reviewed-by Binbin Wu <[email protected]>Quoted 28 lines
include/linux/kvm_types.h +1
-0@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache { struct kvm_mmu_memory_cache { gfp_t gfp_zero; gfp_t gfp_custom; + u64 init_value; struct kmem_cache *kmem_cache; int capacity; int nobjs;virt/kvm/kvm_main.c +0
-0@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj(struct
kvm_mmu_memory_cache*mc,
gfp_tgfp_flags)
{
+ void *page;
+
gfp_flags|=mc->gfp_zero;
if (mc->kmem_cache)
returnkmem_cache_alloc(mc->kmem_cache, gfp_flags); - else
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
- return (void *)__get_free_page(gfp_flags); +
+ page = (void *)__get_free_page(gfp_flags); + if (page &&mc->init_value)
+memset64(page, mc->init_value, PAGE_SIZE / sizeof(mc->init_value));Do we need a
static_assert()to make suremc->init_valueis 64bit?
That's overkill because EPT entry is defined as 64bit and KVM uses u64 for it uniformly.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Quoted 23 lines · Isaku Yamahata
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
index
9c99c9373a3e..c9828feb7a1c100644virt/kvm/kvm_main.c +0
-0@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj(struct
kvm_mmu_memory_cache*mc,
gfp_tgfp_flags)
{
+ void *page;
+
gfp_flags|=mc->gfp_zero;
if (mc->kmem_cache)
returnkmem_cache_alloc(mc->kmem_cache, gfp_flags); - elseQuoted 8 lines
Quoted 6 lines
Quoted 6 lines
- return (void *)__get_free_page(gfp_flags); +
+ page = (void *)__get_free_page(gfp_flags); + if (page &&mc->init_value)
+memset64(page, mc->init_value, PAGE_SIZE / sizeof(mc->init_value));Do we need a
static_assert()to make suremc->init_valueis 64bit?
Quoted 2 lines
That's overkill because EPT entry is defined as 64bit and KVM uses u64 for it uniformly.
Oops, I picked the wrong mail to reply. Sorry for noise.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
In reply to Isaku YamahataQuoted 37 lines · Isaku Yamahata
Quoted 27 lines · Binbin Wu
Quoted 27 lines
Quoted 27 lines
include/linux/kvm_types.h +1
-0@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache { struct kvm_mmu_memory_cache { gfp_t gfp_zero; gfp_t gfp_custom; + u64 init_value; struct kmem_cache *kmem_cache; int capacity; int nobjs;virt/kvm/kvm_main.c +0
-0@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj(struct
kvm_mmu_memory_cache*mc,
gfp_tgfp_flags)
{
+ void *page;
+
gfp_flags|=mc->gfp_zero;
if (mc->kmem_cache)
returnkmem_cache_alloc(mc->kmem_cache, gfp_flags); - elseQuoted 8 lines
Quoted 6 lines
Quoted 6 lines
- return (void *)__get_free_page(gfp_flags); +
+ page = (void *)__get_free_page(gfp_flags); + if (page &&mc->init_value)
+memset64(page, mc->init_value, PAGE_SIZE / sizeof(mc->init_value));Do we need a
static_assert()to make suremc->init_valueis 64bit?
Quoted 2 lines
That's overkill because EPT entry is defined as 64bit and KVM uses u64 for it uniformly.
I'm pretty sure Binbin is talking about passing init_value to memset64(), not about whether or not that suffices for EPT. So I wouldn't say it's overkill.
However, I don't think a static assert is warranted. Functionally, tracking init_value as a u32 or even a u8 would be a-ok as it's a copy-by-value parameter that won't be sign-extended or truncated. I.e. the real reqiurement comes from TDX wanting to set a 64-bit value.
And trying to set bit 63 in a 32-bit field _will_ make the compiler unhappy:
arch/x86/kvm/mmu/mmu.c: In function ‘kvm_mmu_create’:include/vdso/bits.h:8:33: error: conversion from ‘long long unsigned int’ to ‘u32’ {aka ‘unsigned int’} changes value from ‘9223372036854775808’ to ‘0’ [-Werror=overflow]
8 | #define BIT_ULL(nr) (ULL(1) << (nr))
| ^arch/x86/kvm/mmu/spte.h:162:33: note: in expansion of macro ‘BIT_ULL’
162 | #define SHADOW_NONPRESENT_VALUE BIT_ULL(63)
| ^~~~~~~arch/x86/kvm/mmu/mmu.c:6225:17: note: in expansion of macro ‘SHADOW_NONPRESENT_VALUE’
6225 | SHADOW_NONPRESENT_VALUE;
| ^~~~~~~~~~~~~~~~~~~~~~~I suppose one could argue that changing init_value to a u128 could result in undetected truncation, but IMO that firmly crosses into ridiculous territory.
Paolo Bonzini
maintainerPATCH 3/21 KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
The TDX support will need the "suppress #VE" bit (bit 63) set as the initial value for SPTE. To reduce code change size, introduce a new macro SHADOW_NONPRESENT_VALUE for the initial value for the shadow page table entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page tables with their value.
The plan is to unconditionally set the "suppress #VE" bit for both AMD and Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and ignored for non-present SPTE; 2) for conventional VMX guests, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
Message-Id: <acdf09bf60cad12c495005bf3495c54f6b3069c9.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/mmu/mmu.c | 14 +++++++++----- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.h | 2 ++ arch/x86/kvm/mmu/tdp_mmu.c | 14 +++++++------- 4 files changed, 19 insertions(+), 13 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -5
@@ -567,9 +567,9 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)if (!is_shadow_present_pte (old_spte) || !spte_has_volatile_bits (old_spte)) -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE );else - old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte =__update_clear_spte_slow (sptep,SHADOW_NONPRESENT_VALUE );if (!is_shadow_present_pte (old_spte))return old_spte;
@@ -603,7 +603,7 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)*/ static void mmu_spte_clear_no_track (u64 *sptep) { -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE ); }static u64 mmu_spte_get_lockless (u64 *sptep)
@@ -1950,7 +1950,8 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)static int kvm_sync_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i) { -if (!sp->spt[i]) +/* sp->spt[i] has initial value of shadow page table allocation */ +if (sp->spt[i] ==SHADOW_NONPRESENT_VALUE )return 0 ;return vcpu->arch.mmu->sync_spte (vcpu, sp, i);
@@ -6173,7 +6174,10 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; - vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; + vcpu->arch.mmu_shadow_page_cache.init_value = +SHADOW_NONPRESENT_VALUE ; +if (!vcpu->arch.mmu_shadow_page_cache.init_value) + vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -911,7 +911,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, intgpa_t pte_gpa;gfn_t gfn; -if (WARN_ON_ONCE (!sp->spt[i])) +if (WARN_ON_ONCE (sp->spt[i] ==SHADOW_NONPRESENT_VALUE ))return 0 ; first_pte_gpa =FNAME (get_level1_sp_gpa)(sp);
arch/x86/kvm/mmu/spte.h +2 -0
@@ -149,6 +149,8 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +#define SHADOW_NONPRESENT_VALUE 0ULL +extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;extern u64 __read_mostly shadow_nx_mask;
arch/x86/kvm/mmu/tdp_mmu.c +7 -7
@@ -603,7 +603,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* here since the SPTE is going from non-present to non-present. Use * the raw write helper to avoid an unnecessary check on volatile bits. */ -__kvm_tdp_mmu_write_spte (iter->sptep,0 ); +__kvm_tdp_mmu_write_spte (iter->sptep,SHADOW_NONPRESENT_VALUE );return 0 ; }
@@ -740,8 +740,8 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) -tdp_mmu_iter_set_spte (kvm, &iter,0 ); -else if (tdp_mmu_set_spte_atomic (kvm, &iter,0 )) +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); +else if (tdp_mmu_set_spte_atomic (kvm, &iter,SHADOW_NONPRESENT_VALUE )) goto retry; } }
@@ -808,8 +808,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)if (WARN_ON_ONCE (!is_shadow_present_pte (old_spte)))return false; -tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,0 , - sp->gfn, sp->role.level +1 ); +tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte, +SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 );return true; }
@@ -843,7 +843,7 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, !is_last_spte(iter.old_spte, iter.level))continue ; -tdp_mmu_iter_set_spte (kvm, &iter,0 ); +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE );/* * Zappings SPTEs in invalid roots doesn't require a TLB flush,
@@ -1276,7 +1276,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_iter_set_spte (kvm, iter,0 ); +tdp_mmu_iter_set_spte (kvm, iter,SHADOW_NONPRESENT_VALUE );if (!pte_write (range->arg.pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.39.0
Re: [PATCH 03/21] KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
Quoted 14 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
The TDX support will need the "suppress #VE" bit (bit 63) set as the initial value for SPTE. To reduce code change size, introduce a new macro
SHADOW_NONPRESENT_VALUEfor the initial value for the shadow page table entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page tables with their value.The plan is to unconditionally set the "suppress #VE" bit for both AMD and Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and ignored for non-present SPTE; 2) for conventional VMX guests, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
Quoted 5 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <acdf09bf60cad12c495005bf3495c54f6b3069c9.1705965635.git.isaku.yamahata@intel.com>
[Remove unnecessary CONFIG_X86_64 check. - Paolo]Signed-off-by Paolo Bonzini <[email protected]>
Re: [PATCH 03/21] KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
In reply to Paolo BonziniQuoted 17 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
The TDX support will need the "suppress #VE" bit (bit 63) set as the initial value for SPTE. To reduce code change size, introduce a new macro
SHADOW_NONPRESENT_VALUEfor the initial value for the shadow page table entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page tables with their value.The plan is to unconditionally set the "suppress #VE" bit for both AMD and Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and ignored for non-present SPTE; 2) for conventional VMX guests, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <acdf09bf60cad12c495005bf3495c54f6b3069c9.1705965635.git.isaku.yamahata@intel.com>
Quoted 8 lines
[Remove unnecessary CONFIG_X86_64 check. - Paolo]Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 14 +++++++++----- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.h | 2 ++ arch/x86/kvm/mmu/tdp_mmu.c | 14 +++++++------- 4 files changed, 19 insertions(+), 13 deletions(-)
BTW, does it prefer to add "No functional change intended." in changelog for a patch like this?
Quoted 127 lines
arch/x86/kvm/mmu/mmu.c +9
-5@@ -567,9 +567,9 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)if (!is_shadow_present_pte (old_spte) || !spte_has_volatile_bits (old_spte)) -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE );else - old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte =__update_clear_spte_slow (sptep,SHADOW_NONPRESENT_VALUE );if (!is_shadow_present_pte (old_spte))return old_spte;@@ -603,7 +603,7 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)*/ static void mmu_spte_clear_no_track (u64 *sptep) { -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE ); }static u64 mmu_spte_get_lockless (u64 *sptep)@@ -1950,7 +1950,8 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)static int kvm_sync_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i) { -if (!sp->spt[i]) +/* sp->spt[i] has initial value of shadow page table allocation */ +if (sp->spt[i] ==SHADOW_NONPRESENT_VALUE )return 0 ;return vcpu->arch.mmu->sync_spte (vcpu, sp, i);@@ -6173,7 +6174,10 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; - vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; + vcpu->arch.mmu_shadow_page_cache.init_value = +SHADOW_NONPRESENT_VALUE ; +if (!vcpu->arch.mmu_shadow_page_cache.init_value) + vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -911,7 +911,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, intgpa_t pte_gpa;gfn_t gfn; -if (WARN_ON_ONCE (!sp->spt[i])) +if (WARN_ON_ONCE (sp->spt[i] ==SHADOW_NONPRESENT_VALUE ))return 0 ; first_pte_gpa =FNAME (get_level1_sp_gpa)(sp);arch/x86/kvm/mmu/spte.h +2
-0@@ -149,6 +149,8 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +#define SHADOW_NONPRESENT_VALUE 0ULL +extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;extern u64 __read_mostly shadow_nx_mask;arch/x86/kvm/mmu/tdp_mmu.c +7
-7@@ -603,7 +603,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* here since the SPTE is going from non-present to non-present. Use * the raw write helper to avoid an unnecessary check on volatile bits. */ -__kvm_tdp_mmu_write_spte (iter->sptep,0 ); +__kvm_tdp_mmu_write_spte (iter->sptep,SHADOW_NONPRESENT_VALUE );return 0 ; }@@ -740,8 +740,8 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) -tdp_mmu_iter_set_spte (kvm, &iter,0 ); -else if (tdp_mmu_set_spte_atomic (kvm, &iter,0 )) +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); +else if (tdp_mmu_set_spte_atomic (kvm, &iter,SHADOW_NONPRESENT_VALUE )) goto retry; } }@@ -808,8 +808,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)if (WARN_ON_ONCE (!is_shadow_present_pte (old_spte)))return false; -tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,0 , - sp->gfn, sp->role.level +1 ); +tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte, +SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 );return true; }@@ -843,7 +843,7 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, !is_last_spte(iter.old_spte, iter.level))continue ; -tdp_mmu_iter_set_spte (kvm, &iter,0 ); +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE );/* * Zappings SPTEs in invalid roots doesn't require a TLB flush, @@ -1276,7 +1276,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_iter_set_spte (kvm, iter,0 ); +tdp_mmu_iter_set_spte (kvm, iter,SHADOW_NONPRESENT_VALUE );if (!pte_write (range->arg.pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Paolo Bonzini
maintainerPATCH 4/21 KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
For TD guest, the current way to emulate MMIO doesn't work any more, as KVM is not able to access the private memory of TD guest and do the emulation. Instead, TD guest expects to receive #VE when it accesses the MMIO and then it can explicitly make hypercall to KVM to get the expected information.
To achieve this, the TDX module always enables "EPT-violation #VE" in the VMCS control. And accordingly, for the MMIO spte for the shared GPA,
1. KVM needs to set "suppress #VE" bit for the non-present SPTE so that EPT violation happens on TD accessing MMIO range. 2. On EPT violation, KVM sets the MMIO spte to clear "suppress #VE" bit so the TD guest can receive the #VE instead of EPT misconfiguration unlike VMX case. For the shared GPA that is not populated yet, EPT violation need to be triggered when TD guest accesses such shared GPA. The non-present SPTE value for shared GPA should set "suppress #VE" bit.
Add "suppress #VE" bit (bit 63) to SHADOW_NONPRESENT_VALUE and REMOVED_SPTE. Unconditionally set the "suppress #VE" bit (which is bit 63) for both AMD and Intel as: 1) AMD hardware doesn't use this bit when present bit is off; 2) for normal VMX guest, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
Message-Id: <a99cb866897c7083430dce7f24c63b17d7121134.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/mmu/spte.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/spte.h +14 -1
@@ -149,7 +149,20 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +/* +* Non-present SPTE value for both VMX and SVM for TDP MMU. +* For SVM NPT, for non-present spte (bit 0 = 0), other bits are ignored. +* For VMX EPT, bit 63 is ignored if #VE is disabled. (EPT_VIOLATION_VE=0) +* bit 63 is #VE suppress if #VE is enabled. (EPT_VIOLATION_VE=1) +* For TDX: +* TDX module sets EPT_VIOLATION_VE for Secure-EPT and conventional EPT +*/ +#ifdef CONFIG_X86_64 +#define SHADOW_NONPRESENT_VALUE BIT_ULL(63) +static_assert (!(SHADOW_NONPRESENT_VALUE &SPTE_MMU_PRESENT_MASK )); +#else #define SHADOW_NONPRESENT_VALUE 0ULL +#endif extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;
@@ -196,7 +209,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* * Only used by the TDP MMU. */ -#define REMOVED_SPTE 0x5a0ULL +#define REMOVED_SPTE (SHADOW_NONPRESENT_VALUE | 0x5a0ULL)/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK ));
Signature · 1 line
2.39.0
Re: [PATCH 04/21] KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
Quoted 59 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
For TD guest, the current way to emulate MMIO doesn't work any more, as KVM is not able to access the private memory of TD guest and do the emulation. Instead, TD guest expects to receive #VE when it accesses the MMIO and then it can explicitly make hypercall to KVM to get the expected information.
To achieve this, the TDX module always enables "EPT-violation #VE" in the VMCS control. And accordingly, for the MMIO spte for the shared GPA,
1. KVM needs to set "suppress #VE" bit for the non-present SPTE so that EPT violation happens on TD accessing MMIO range. 2. On EPT violation, KVM sets the MMIO spte to clear "suppress #VE" bit so the TD guest can receive the #VE instead of EPT misconfiguration unlike VMX case. For the shared GPA that is not populated yet, EPT violation need to be triggered when TD guest accesses such shared GPA. The non-present SPTE value for shared GPA should set "suppress #VE" bit.Add "suppress #VE" bit (bit 63) to
SHADOW_NONPRESENT_VALUEandREMOVED_SPTE. Unconditionally set the "suppress #VE" bit (which is bit 63) for both AMD and Intel as: 1) AMD hardware doesn't use this bit when present bit is off; 2) for normal VMX guest, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Binbin Wu <[email protected]>Message-Id: <a99cb866897c7083430dce7f24c63b17d7121134.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu/spte.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)arch/x86/kvm/mmu/spte.h +13
-0@@ -149,7 +149,20 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +/* +* Non-present SPTE value for both VMX and SVM for TDP MMU. +* For SVM NPT, for non-present spte (bit 0 = 0), other bits are ignored. +* For VMX EPT, bit 63 is ignored if #VE is disabled. (EPT_VIOLATION_VE=0) +* bit 63 is #VE suppress if #VE is enabled. (EPT_VIOLATION_VE=1) +* For TDX: +* TDX module sets EPT_VIOLATION_VE for Secure-EPT and conventional EPT +*/ +#ifdef CONFIG_X86_64 +#define SHADOW_NONPRESENT_VALUE BIT_ULL(63) +static_assert (!(SHADOW_NONPRESENT_VALUE &SPTE_MMU_PRESENT_MASK )); +#else #define SHADOW_NONPRESENT_VALUE 0ULL +#endif extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;@@ -196,7 +209,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
* vulnerability. Use only low bits to avoid 64-bit immediates. ^
We may remove this comment. Others are fine.
Quoted 13 lines
arch/x86/kvm/mmu/spte.h +1
-1* * Only used by the TDP MMU. */ -#define REMOVED_SPTE 0x5a0ULL +#define REMOVED_SPTE (SHADOW_NONPRESENT_VALUE | 0x5a0ULL)/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK ));Signature · 1 line
2.39.0
Re: [PATCH 04/21] KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE
In reply to Paolo BonziniQuoted 24 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
For TD guest, the current way to emulate MMIO doesn't work any more, as KVM is not able to access the private memory of TD guest and do the emulation. Instead, TD guest expects to receive #VE when it accesses the MMIO and then it can explicitly make hypercall to KVM to get the expected information.
To achieve this, the TDX module always enables "EPT-violation #VE" in the VMCS control. And accordingly, for the MMIO spte for the shared GPA,
1. KVM needs to set "suppress #VE" bit for the non-present SPTE so that EPT violation happens on TD accessing MMIO range. 2. On EPT violation, KVM sets the MMIO spte to clear "suppress #VE" bit so the TD guest can receive the #VE instead of EPT misconfiguration unlike VMX case. For the shared GPA that is not populated yet, EPT violation need to be triggered when TD guest accesses such shared GPA. The non-present SPTE value for shared GPA should set "suppress #VE" bit.Add "suppress #VE" bit (bit 63) to
SHADOW_NONPRESENT_VALUEandREMOVED_SPTE. Unconditionally set the "suppress #VE" bit (which is bit 63) for both AMD and Intel as: 1) AMD hardware doesn't use this bit when present bit is off; 2) for normal VMX guest, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
Quoted 5 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Binbin Wu <[email protected]>Message-Id: <a99cb866897c7083430dce7f24c63b17d7121134.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
+ 1 to the nit pointed by Yilun,
after that,
Re: [PATCH 04/21] KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE
In reply to Paolo BonziniQuoted 4 lines
+/*
+ * Non-present SPTE value for both VMX and SVM for TDP MMU.
In the previous patch, SHADOW_NONPRESENT_VALUE is also used in the shadow MMU code. So here when you change SHADOW_NONPRESENT_VALUE to a non-zero value, the "for TDP MMU" part doesn't stand.
I am wondering whether we can just avoid using SHADOW_NONPRESENT_VALUE in shadow MMU code in the previous patch, and state explicitly that we are only going to support TDP MMU for non-zero value for non-present SPTE?
Quoted 16 lines
+ * For SVM NPT, for non-present spte (bit 0 = 0), other bits are ignored.
+ * For VMX EPT, bit 63 is ignored if #VE is disabled. (EPT_VIOLATION_VE=0)
+ * bit 63 is #VE suppress if #VE is enabled. (EPT_VIOLATION_VE=1)
+ * For TDX:
+ * TDX module setsEPT_VIOLATION_VEfor Secure-EPT and conventional EPT
+ */arch/x86/kvm/mmu/spte.h +5
-0+#ifdef CONFIG_X86_64 +#define SHADOW_NONPRESENT_VALUE BIT_ULL(63) +static_assert (!(SHADOW_NONPRESENT_VALUE &SPTE_MMU_PRESENT_MASK )); +#else #define SHADOW_NONPRESENT_VALUE 0ULL +#endif extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask; · line 212
Quoted 5 lines
arch/x86/kvm/mmu/spte.h +1
-1@@ -196,7 +209,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* * Only used by the TDP MMU. */ -#define REMOVED_SPTE 0x5a0ULL +#define REMOVED_SPTE (SHADOW_NONPRESENT_VALUE | 0x5a0ULL)
I kinda prefer moving this chunk to the previous patch, because the reason to have SHADOW_NONPRESENT_VALUE is to have a non-zero value for non-present SPTEs, which include the REMOVED_SPTE.
But just my 2cents.
Paolo Bonzini
maintainerPATCH 5/21 KVM: x86/mmu: Add Suppress VE bit to EPT shadow_mmio_mask/shadow_present_mask
In reply to Paolo BonziniFrom: Isaku Yamahata <[email protected]>
To make use of the same value of shadow_mmio_mask and shadow_present_mask for TDX and VMX, add Suppress-VE bit to shadow_mmio_mask and shadow_present_mask so that they can be common for both VMX and TDX.
TDX will require shadow_mmio_mask and shadow_present_mask to include VMX_SUPPRESS_VE for shared GPA so that EPT violation is triggered for shared GPA. For VMX, VMX_SUPPRESS_VE doesn't matter for MMIO because the spte value is defined so as to cause EPT misconfig.
Message-Id: <97cc616b3563cd8277be91aaeb3e14bce23c3649.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu/spte.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -513,6 +513,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/spte.c +4 -2
@@ -429,7 +429,9 @@ 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 ;/* * EPT overrides the host MTRRs, and so KVM must program the desired * memtype directly into the SPTEs. Note, this mask is just the mask
@@ -446,7 +448,7 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only)* 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 ); +VMX_EPT_RWX_MASK |VMX_EPT_SUPPRESS_VE_BIT ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);
Signature · 1 line
2.39.0
Re: [PATCH 05/21] KVM: x86/mmu: Add Suppress VE bit to EPT shadow_mmio_mask/shadow_present_mask
Quoted 6 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To make use of the same value of
shadow_mmio_maskandshadow_present_maskfor TDX and VMX, add Suppress-VE bit toshadow_mmio_maskandshadow_present_maskso that they can be common for both VMX and TDX.
Quoted 8 lines
TDX will require
shadow_mmio_maskandshadow_present_maskto includeVMX_SUPPRESS_VEfor shared GPA so that EPT violation is triggered for shared GPA. For VMX,VMX_SUPPRESS_VEdoesn't matter for MMIO because the spte value is defined so as to cause EPT misconfig.Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <97cc616b3563cd8277be91aaeb3e14bce23c3649.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 41 lines
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu/spte.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)arch/x86/include/asm/vmx.h +1
-0@@ -513,6 +513,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/spte.c +4
-2@@ -429,7 +429,9 @@ 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 ;/* * EPT overrides the host MTRRs, and so KVM must program the desired * memtype directly into the SPTEs. Note, this mask is just the mask @@ -446,7 +448,7 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only)* 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 ); +VMX_EPT_RWX_MASK |VMX_EPT_SUPPRESS_VE_BIT ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);
Re: [PATCH 05/21] KVM: x86/mmu: Add Suppress VE bit to EPT shadow_mmio_mask/shadow_present_mask
In reply to Paolo BonziniQuoted 6 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To make use of the same value of
shadow_mmio_maskandshadow_present_maskfor TDX and VMX, add Suppress-VE bit toshadow_mmio_maskandshadow_present_maskso that they can be common for both VMX and TDX.
Quoted 8 lines
TDX will require
shadow_mmio_maskandshadow_present_maskto includeVMX_SUPPRESS_VEfor shared GPA so that EPT violation is triggered for shared GPA. For VMX,VMX_SUPPRESS_VEdoesn't matter for MMIO because the spte value is defined so as to cause EPT misconfig.Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <97cc616b3563cd8277be91aaeb3e14bce23c3649.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 41 lines
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu/spte.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)arch/x86/include/asm/vmx.h +1
-0@@ -513,6 +513,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/spte.c +4
-2@@ -429,7 +429,9 @@ 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 ;/* * EPT overrides the host MTRRs, and so KVM must program the desired * memtype directly into the SPTEs. Note, this mask is just the mask @@ -446,7 +448,7 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only)* 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 ); +VMX_EPT_RWX_MASK |VMX_EPT_SUPPRESS_VE_BIT ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);
Paolo Bonzini
maintainerPATCH 6/21 KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
In reply to Paolo BonziniFrom: 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. Introduce a separate setter function so that guest TD can override later.
Message-Id: <229a18434e5d83f45b1fcd7bf1544d79db1becb6.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++--- arch/x86/kvm/mmu/spte.c | 10 ++++++++-- arch/x86/kvm/mmu/spte.h | 4 ++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 6 files changed, 21 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1313,6 +1313,8 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +struct iommu_domain *iommu_domain;bool iommu_noncoherent;#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
arch/x86/kvm/mmu.h +1 -0
@@ -101,6 +101,7 @@ static inline u8 kvm_get_shadow_phys_bits(void) }void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);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 +5 -3
@@ -2515,7 +2515,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 ;
@@ -4197,7 +4197,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON_ONCE (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);
@@ -4813,7 +4813,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);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;
@@ -6320,6 +6320,8 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)void kvm_mmu_init_vm (struct kvm *kvm) { + + kvm->arch.shadow_mmio_value = shadow_mmio_value;INIT_LIST_HEAD (&kvm->arch.active_mmu_pages);INIT_LIST_HEAD (&kvm->arch.zapped_obsolete_pages);INIT_LIST_HEAD (&kvm->arch.possible_nx_huge_pages);
arch/x86/kvm/mmu/spte.c +8 -2
@@ -74,10 +74,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 ;
@@ -411,6 +411,12 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value) +{ + kvm->arch.shadow_mmio_value = mmio_value; +} +EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_value); +void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask) {/* shadow_me_value must be a subset of shadow_me_mask */
arch/x86/kvm/mmu/spte.h +2 -2
@@ -264,9 +264,9 @@ static inline struct kvm_mmu_page *root_to_sp(hpa_t root)return spte_to_child_sp (root); } -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 && +return (spte & shadow_mmio_mask) == kvm->arch.shadow_mmio_value &&likely (enable_mmio_caching); }
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -495,8 +495,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_ONCE (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON_ONCE (!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"
@@ -1028,7 +1028,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);
Signature · 1 line
2.39.0
Re: [PATCH 06/21] KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
Quoted 39 lines · Paolo Bonzini
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. Introduce a separate setter function so that guest TD can override later.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <229a18434e5d83f45b1fcd7bf1544d79db1becb6.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++--- arch/x86/kvm/mmu/spte.c | 10 ++++++++-- arch/x86/kvm/mmu/spte.h | 4 ++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 6 files changed, 21 insertions(+), 10 deletions(-)arch/x86/include/asm/kvm_host.h +2
-0@@ -1313,6 +1313,8 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +struct iommu_domain *iommu_domain;bool iommu_noncoherent;#define __KVM_HAVE_ARCH_NONCOHERENT_DMAarch/x86/kvm/mmu.h +0
-0
arch/x86/kvm/mmu.h static inline u8 kvm_get_shadow_phys_bits(void) · line 104
Quoted 4 lines
arch/x86/kvm/mmu.h +1
-0@@ -101,6 +101,7 @@ static inline u8 kvm_get_shadow_phys_bits(void) }void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);
Now, the new added function along with above function mislead people on their names. It's easily to think
- kvm_mmu_set_mmio_spte_mask() is to set shadow_mmio_mask, while
- kvm_mmu_set_mmio_spte_value() is to set shadow_mmio_value
we'd better come up with a better name.
Other than it, the patch looks good to me.
Re: [PATCH 06/21] KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
In reply to Paolo BonziniQuoted 4 lines · Paolo Bonzini
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
Nit: members -> a member, since only 'shadow_mmio_value' is added.
Quoted 8 lines
variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working. Introduce a separate setter function so that guest TD can override later.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <229a18434e5d83f45b1fcd7bf1544d79db1becb6.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 144 lines
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++--- arch/x86/kvm/mmu/spte.c | 10 ++++++++-- arch/x86/kvm/mmu/spte.h | 4 ++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 6 files changed, 21 insertions(+), 10 deletions(-)arch/x86/include/asm/kvm_host.h +2
-0@@ -1313,6 +1313,8 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +struct iommu_domain *iommu_domain;bool iommu_noncoherent;#define __KVM_HAVE_ARCH_NONCOHERENT_DMAarch/x86/kvm/mmu.h +1
-0@@ -101,6 +101,7 @@ static inline u8 kvm_get_shadow_phys_bits(void) }void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);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 +5
-3@@ -2515,7 +2515,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 ;@@ -4197,7 +4197,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON_ONCE (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);@@ -4813,7 +4813,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);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;@@ -6320,6 +6320,8 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)void kvm_mmu_init_vm (struct kvm *kvm) { + + kvm->arch.shadow_mmio_value = shadow_mmio_value;INIT_LIST_HEAD (&kvm->arch.active_mmu_pages);INIT_LIST_HEAD (&kvm->arch.zapped_obsolete_pages);INIT_LIST_HEAD (&kvm->arch.possible_nx_huge_pages);arch/x86/kvm/mmu/spte.c +8
-2@@ -74,10 +74,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 ;@@ -411,6 +411,12 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value) +{ + kvm->arch.shadow_mmio_value = mmio_value; +} +EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_value); +void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask) {/* shadow_me_value must be a subset of shadow_me_mask */ arch/x86/kvm/mmu/spte.h +2
-2@@ -264,9 +264,9 @@ static inline struct kvm_mmu_page *root_to_sp(hpa_t root)return spte_to_child_sp (root); } -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 && +return (spte & shadow_mmio_mask) == kvm->arch.shadow_mmio_value &&likely (enable_mmio_caching); }arch/x86/kvm/mmu/tdp_mmu.c +3
-3@@ -495,8 +495,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_ONCE (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON_ONCE (!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"@@ -1028,7 +1028,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);
Re: [PATCH 06/21] KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
In reply to Paolo BonziniQuoted 3 lines · Paolo Bonzini
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
"members" -> "member".
Quoted 1 line
variables.
"variables" -> "variable".
By using the per-VM EPT entry value for MMIO, the existing VMX
Quoted 2 lines
logic is kept working. Introduce a separate setter function so that guest TD can override later.
"guest TD can override " -> "KVM can override for TDX guest".
Quoted 71 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <229a18434e5d83f45b1fcd7bf1544d79db1becb6.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++--- arch/x86/kvm/mmu/spte.c | 10 ++++++++-- arch/x86/kvm/mmu/spte.h | 4 ++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 6 files changed, 21 insertions(+), 10 deletions(-)arch/x86/include/asm/kvm_host.h +2
-0@@ -1313,6 +1313,8 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +struct iommu_domain *iommu_domain;bool iommu_noncoherent;#define __KVM_HAVE_ARCH_NONCOHERENT_DMAarch/x86/kvm/mmu.h +1
-0@@ -101,6 +101,7 @@ static inline u8 kvm_get_shadow_phys_bits(void) }void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);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 +3
-3@@ -2515,7 +2515,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 ;@@ -4197,7 +4197,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON_ONCE (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);@@ -4813,7 +4813,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);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 static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) · line 6324
Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +2
-0@@ -6320,6 +6320,8 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)void kvm_mmu_init_vm (struct kvm *kvm) { + + kvm->arch.shadow_mmio_value = shadow_mmio_value;
Unintended blank line.
[...]
Paolo Bonzini
maintainerPATCH 7/21 KVM: VMX: Introduce test mode related to EPT violation VE
In reply to Paolo BonziniFrom: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM uses the suppress #VE bit in EPT entries selectively, in order to be able to trap non-present conditions. However, #VE isn't used for VMX and it's a bug if it happens. To be defensive and test that VMX case isn't broken introduce an option ept_violation_ve_test and when it's set, BUG the vm.
Message-Id: <d6db6ba836605c0412e166359ba5c46a63c22f86.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/include/asm/vmx.h | 12 +++++++ arch/x86/kvm/vmx/vmcs.h | 5 +++ arch/x86/kvm/vmx/vmx.c | 69 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 6 +++- 4 files changed, 90 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +12 -0
@@ -70,6 +70,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_ENABLE_XSAVES VMCS_CONTROL_BIT(XSAVES)#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC VMCS_CONTROL_BIT(MODE_BASED_EPT_EXEC)
@@ -225,6 +226,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,
@@ -630,4 +633,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/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 +67 -1
123 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +5 -1
@@ -364,6 +364,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 {
@@ -576,7 +579,8 @@ static inline u8 vmx_get_rvi(void) SECONDARY_EXEC_ENABLE_VMFUNC | \ SECONDARY_EXEC_BUS_LOCK_DETECTION | \ SECONDARY_EXEC_NOTIFY_VM_EXITING | \ - SECONDARY_EXEC_ENCLS_EXITING) + SECONDARY_EXEC_ENCLS_EXITING | \ + SECONDARY_EXEC_EPT_VIOLATION_VE)#define KVM_REQUIRED_VMX_TERTIARY_VM_EXEC_CONTROL 0#define KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL \
Signature · 1 line
2.39.0
Sean Christopherson
maintainerRe: [PATCH 07/21] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 7 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM uses the suppress #VE bit in EPT entries selectively, in order to be able to trap non-present conditions. However, #VE isn't used for VMX and it's a bug if it happens. To be defensive and test that VMX case isn't broken introduce an option
ept_violation_ve_testand when it's set, BUG the vm.
This needs to be two patches:
1. Add the architecture #defines, enums, structures, and is_ve_fault().
2. Add the forced #VE enabling test code
Quoted 34 lines
Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <d6db6ba836605c0412e166359ba5c46a63c22f86.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/vmx.h | 12 +++++++ arch/x86/kvm/vmx/vmcs.h | 5 +++ arch/x86/kvm/vmx/vmx.c | 69 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 6 +++- 4 files changed, 90 insertions(+), 2 deletions(-)arch/x86/include/asm/vmx.h +3
-0@@ -70,6 +70,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_ENABLE_XSAVES VMCS_CONTROL_BIT(XSAVES)#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC VMCS_CONTROL_BIT(MODE_BASED_EPT_EXEC)@@ -225,6 +226,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,@@ -630,4 +633,13 @@ enum vmx_l1d_flush_state {
arch/x86/include/asm/vmx.h enum vmx_l1d_flush_state { · line 643
Quoted 10 lines
arch/x86/include/asm/vmx.h +8
-0extern 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; +};
Should this be __packed since it's hardware-defined, or are we ok relying on the compiler to not be stupid?
Quoted 22 lines
arch/x86/include/asm/vmx.h +0
-0#endif 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 +0
-0
arch/x86/kvm/vmx/vmx.c module_param(error_on_inconsistent_vmcs_config, bool, 0444); · line 130
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -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; +module_param (ept_violation_ve_test, bool,0444 );
I would much prefer to enable #VE if CONFIG_KVM_PROVE_MMU=y. We already have too many module params to deal with for testing, and practically speaking the only people who will ever turn this on are the same people that run with CONFIG_KVM_PROVE_MMU=y.
Quoted 17 lines
arch/x86/kvm/vmx/vmx.c +6
-0#define MSR_BITMAP_MODE_X2APIC 1#define MSR_BITMAP_MODE_X2APIC_APICV 2@@ -868,6 +871,12 @@ 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. To test against unexpected changes +* 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.
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2617
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -2603,6 +2613,9 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, &_cpu_based_2nd_exec_control))return -EIO ; } +if (!ept_violation_ve_test) + _cpu_based_2nd_exec_control &= ~SECONDARY_EXEC_EPT_VIOLATION_VE ;
_If_ we add a module param, the param needs to be disabled if #VE isn't supported.
Quoted 34 lines
arch/x86/kvm/vmx/vmx.c +11
-1#ifndef CONFIG_X86_64 if (!(_cpu_based_2nd_exec_control &SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ))@@ -2627,6 +2640,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) {@@ -4592,6 +4606,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)@@ -4715,8 +4730,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);
arch/x86/kvm/vmx/vmx.c static void init_vmcs(struct vcpu_vmx *vmx) · line 4752
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+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 ;
This is completely unnecessary, the entire page is zero-allocated.
arch/x86/kvm/vmx/vmx.c static void init_vmcs(struct vcpu_vmx *vmx) · line 4759
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +7
-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.
No, it's not. This is silly on multiple fronts. (a) KVM knows if it's going to enable #VE when the vCPU is first created, the allocation can and should be done at that time along with all the other allocations needed for the VM. (b) Except for error injection from syzkaller and friends, there is basically zero chance the VM will live on if one 4KiB allocation fails. (c) This will never be enabled in production; it's totally fine if the vCPU creation fails during testing, because as a above, that will practically never happen outside of deliberate error injection.
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +7
-0+*/ +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));
arch/x86/kvm/vmx/vmx.c static int handle_exception_nmi(struct kvm_vcpu *vcpu) · line 5255
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -5204,6 +5251,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. Block the VM if it does.
This is not a useful comment. Obviously #VE isn't supposed to happen, otherwise KVM wouldn't be bugging the VM.
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +4
-0+*/ +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 );
arch/x86/kvm/vmx/vmx.c void dump_vmcs(struct kvm_vcpu *vcpu) · line 6454
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +6
-0@@ -6393,6 +6446,18 @@ 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 ));
Why!?!? You have the address in vcpu_vmx, just use that. If KVM is dumping the VMCS, then something has gone wrong, possible in hardware or ucode. Derefencing an address from the VMCS, which could very well be corrupted, is a terrible idea. This could easily escalate from a dead VM into a dead host.
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-0+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); + } }/*
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_free(struct kvm_vcpu *vcpu) · line 7501
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -7433,6 +7498,8 @@ static 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)
Unnecessary, free_page() does this for you.
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +1
-0+free_page ((unsigned long )vmx->ve_info); }static int vmx_vcpu_create (struct kvm_vcpu *vcpu)arch/x86/kvm/vmx/vmx.h +0
-0
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 368
Quoted 5 lines
arch/x86/kvm/vmx/vmx.h +2
-0@@ -364,6 +364,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. */
This is also not a useful comment. Even the one at the allocation site is of dubious value.
Quoted 18 lines
arch/x86/kvm/vmx/vmx.h +3
-1+struct vmx_ve_information *ve_info; };struct kvm_vmx {@@ -576,7 +579,8 @@ static inline u8 vmx_get_rvi(void) SECONDARY_EXEC_ENABLE_VMFUNC | \ SECONDARY_EXEC_BUS_LOCK_DETECTION | \ SECONDARY_EXEC_NOTIFY_VM_EXITING | \ - SECONDARY_EXEC_ENCLS_EXITING) + SECONDARY_EXEC_ENCLS_EXITING | \ + SECONDARY_EXEC_EPT_VIOLATION_VE)#define KVM_REQUIRED_VMX_TERTIARY_VM_EXEC_CONTROL 0#define KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL \Signature · 1 line
2.39.0
Re: [PATCH 07/21] KVM: VMX: Introduce test mode related to EPT violation VE
In reply to Paolo BonziniQuoted 7 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM uses the suppress #VE bit in EPT entries selectively, in order to be able to trap non-present conditions. However, #VE isn't used for VMX and it's a bug if it happens. To be defensive and test that VMX case isn't broken introduce an option
ept_violation_ve_testand when it's set, BUG the vm.
I am wondering from HW's point of view, is it OK for the kernel to explicitly send #VE IPI, in which case, IIUC, the guest can legally get the #VE w/o being a TDX guest?
Sean Christopherson
maintainerRe: [PATCH 07/21] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 9 lines · Kai Huang
Quoted 7 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM uses the suppress #VE bit in EPT entries selectively, in order to be able to trap non-present conditions. However, #VE isn't used for VMX and it's a bug if it happens. To be defensive and test that VMX case isn't broken introduce an option
ept_violation_ve_testand when it's set, BUG the vm.
Quoted 3 lines
I am wondering from HW's point of view, is it OK for the kernel to explicitly send #VE IPI, in which case, IIUC, the guest can legally get the #VE w/o being a TDX guest?
Ooh, fun. Short answer: there's nothing to worry about here.
Legally, no. Vectors 0-31 are reserved. However, I do _think_ the guest could technically send IPIs on vectors 16-31, as the local APIC doesn't outright reject such vectors. But such software would be in clear violation of the SDM.
11.5.2 Valid Interrupt Vectors
The Intel 64 and IA-32 architectures define 256 vector numbers, ranging from 0 through 255 (see Section 6.2, “Exception and Interrupt Vectors”). Local and I/O APICs support 240 of these vectors (in the range of 16 to 255) as valid interrupts.
When an interrupt vector in the range of 0 to 15 is sent or received through the local APIC, the APIC indicates an illegal vector in its Error Status Register (see Section 11.5.3, “Error Handling”). The Intel 64 and IA-32 architectures reserve vectors 16 through 31 for predefined interrupts, exceptions, and Intel-reserved encodings (see Table 6-1). However, the local APIC does not treat vectors in this range as illegal.
When an illegal vector value (0 to 15) is written to an LVT entry and the delivery mode is Fixed (bits 8-11 equal 0), the APIC may signal an illegal vector error, without regard to whether the mask bit is set or whether an interrupt is actually seen on the input.
where Table 6-1 defines the various exceptions, including #VE, and for vectors 22-31 says "Intel reserved. Do not use." Vectors 32-255 are explicitly described as "User Defined (Non-reserved) Interrupts" that can be generated via "External interrupt or INT n instruction."
However, INTn is far more interesting than IPIs, as INTn can definitely generate interrupts for vectors 0-31, and the legality of software generating such interrupts is questionable. E.g. KVM used to "forward" NMI VM-Exits to the kernel by doing INTn with vector 2.
Key word "interrupts"! IPIs are hardware interrupts, and INTn generates software interrupts, neither of which are subject to exception bitmap interception:
Exceptions (faults, traps, and aborts) cause VM exits based on the exception bitmap (see Section 25.6.3). If an exception occurs, its vector (in the range 0–31) is used to select a bit in the exception bitmap. If the bit is 1, a VM exit occurs; if the bit is 0, the exception is delivered normally through the guest IDT. This use of the exception bitmap applies also to exceptions generated by the instructions INT1, INT3, INTO, BOUND, UD0, UD1, and UD2.
with a footnote that further says:
INT1 and INT3 refer to the instructions with opcodes F1 and CC, respectively, and not to INT n with value 1 or 3 for n.
So while a misbehaving guest could generate a software interrupt on vector 20, it would not be a true #VE, i.e. not an exception, and thus would not generate an EXCEPTION_NMI VM-Exit. I.e. the KVM_BUG_ON() can't be triggered by the guest (assuming hardware isn't broken).
Re: [PATCH 07/21] KVM: VMX: Introduce test mode related to EPT violation VE
Quoted 34 lines · Sean Christopherson
Quoted 9 lines · Kai Huang
Quoted 7 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM uses the suppress #VE bit in EPT entries selectively, in order to be able to trap non-present conditions. However, #VE isn't used for VMX and it's a bug if it happens. To be defensive and test that VMX case isn't broken introduce an option
ept_violation_ve_testand when it's set, BUG the vm.Quoted 3 lines
I am wondering from HW's point of view, is it OK for the kernel to explicitly send #VE IPI, in which case, IIUC, the guest can legally get the #VE w/o being a TDX guest?
Ooh, fun. Short answer: there's nothing to worry about here.
Legally, no. Vectors 0-31 are reserved. However, I do _think_ the guest could technically send IPIs on vectors 16-31, as the local APIC doesn't outright reject such vectors. But such software would be in clear violation of the SDM.
11.5.2 Valid Interrupt Vectors
The Intel 64 and IA-32 architectures define 256 vector numbers, ranging from 0 through 255 (see Section 6.2, “Exception and Interrupt Vectors”). Local and I/O APICs support 240 of these vectors (in the range of 16 to 255) as valid interrupts.
When an interrupt vector in the range of 0 to 15 is sent or received through the local APIC, the APIC indicates an illegal vector in its Error Status Register (see Section 11.5.3, “Error Handling”). The Intel 64 and IA-32 architectures reserve vectors 16 through 31 for predefined interrupts, exceptions, and Intel-reserved encodings (see Table 6-1). However, the local APIC does not treat vectors in this range as illegal.
Quoted 4 lines
When an illegal vector value (0 to 15) is written to an LVT entry and the delivery mode is Fixed (bits 8-11 equal 0), the APIC may signal an illegal vector error, without regard to whether the mask bit is set or whether an interrupt is actually seen on the input.
I hate the "may" here :-)
Quoted 23 lines
where Table 6-1 defines the various exceptions, including #VE, and for vectors 22-31 says "Intel reserved. Do not use." Vectors 32-255 are explicitly described as "User Defined (Non-reserved) Interrupts" that can be generated via "External interrupt or INT n instruction."
However, INTn is far more interesting than IPIs, as INTn can definitely generate interrupts for vectors 0-31, and the legality of software generating such interrupts is questionable. E.g. KVM used to "forward" NMI VM-Exits to the kernel by doing INTn with vector 2.
Key word "interrupts"! IPIs are hardware interrupts, and INTn generates software interrupts, neither of which are subject to exception bitmap interception:
Exceptions (faults, traps, and aborts) cause VM exits based on the exception bitmap (see Section 25.6.3). If an exception occurs, its vector (in the range 0–31) is used to select a bit in the exception bitmap. If the bit is 1, a VM exit occurs; if the bit is 0, the exception is delivered normally through the guest IDT. This use of the exception bitmap applies also to exceptions generated by the instructions INT1, INT3, INTO, BOUND, UD0, UD1, and UD2.
with a footnote that further says:
Quoted 8 lines
INT1 and INT3 refer to the instructions with opcodes F1 and CC, respectively, and not to INT n with value 1 or 3 for n.
So while a misbehaving guest could generate a software interrupt on vector 20, it would not be a true #VE, i.e. not an exception, and thus would not generate an
EXCEPTION_NMIVM-Exit. I.e. theKVM_BUG_ON()can't be triggered by the guest (assuming hardware isn't broken).
Ah, right, software-interrupts but not exceptions.
Thanks for the full explanation!
Paolo Bonzini
maintainerPATCH 8/21 KVM: VMX: Move out vmx_x86_ops to 'main.c' to dispatch VMX and TDX
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions to operate on VM. TDX doesn't allow VMM to operate VMCS directly. Instead, TDX has its own data structures, and TDX SEAMCALL APIs for VMM to indirectly operate those data structures. This means we must have a TDX version of kvm_x86_ops.
The existing global struct kvm_x86_ops already defines an interface which can be adapted to TDX, but kvm_x86_ops is a system-wide, not per-VM structure. To allow VMX to coexist with TDs, the kvm_x86_ops callbacks will have wrappers "if (tdx) tdx_op() else vmx_op()" to pick VMX or TDX at run time.
To split the runtime switch, the VMX implementation, and the TDX implementation, add main.c, and move out the vmx_x86_ops hooks in preparation for adding TDX. Use 'vt' for the naming scheme as a nod to VT-x and as a concatenation of VmxTdx.
The eventually converted code will look like this:
vmx.c:
vmx_op() { ... }
VMX initializationtdx.c:
tdx_op() { ... }
TDX initializationx86_ops.h:
vmx_op();
tdx_op();main.c:
static vt_op() { if (tdx) tdx_op() else vmx_op() }
static struct kvm_x86_ops vt_x86_ops = {
.op = vt_op,initialization functions call both VMX and TDX initialization
Opportunistically, fix the name inconsistency from vmx_create_vcpu() and vmx_free_vcpu() to vmx_vcpu_create() and vmx_vcpu_free().
Message-Id: <e603c317587f933a9d1bee8728c84e4935849c16.1705965634.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/main.c | 168 +++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 375 ++++++++++--------------------------- arch/x86/kvm/vmx/x86_ops.h | 124 ++++++++++++ 4 files changed, 395 insertions(+), 274 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.h
arch/x86/kvm/Makefile +1 -1
@@ -20,7 +20,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ - vmx/nested.o vmx/posted_intr.o + vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o kvm-intel-$(CONFIG_KVM_HYPERV) += vmx/hyperv.o vmx/hyperv_evmcs.o
arch/x86/kvm/vmx/main.c new file +168 -0
168 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +102 -273
924 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 9/21 KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass intr_info to the NMI and INTR handlers instead of pulling it from vcpu_vmx in preparation for sharing the bulk of the handlers with TDX.
When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason, intr_info, exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)
TDX:
- get exit reason, intr_info, exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
Message-Id: <0396a9ae70d293c9d0b060349dae385a8a4fbcec.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/vmx/vmx.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -9
@@ -7000,24 +7000,22 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) { -u32 intr_info =vmx_get_intr_info (&vmx->vcpu); -/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check (); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;
@@ -7040,9 +7038,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_irqoff (vmx); +handle_exception_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Signature · 1 line
2.39.0
Re: [PATCH 09/21] KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
Quoted 19 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass
intr_infoto the NMI and INTR handlers instead of pulling it fromvcpu_vmxin preparation for sharing the bulk of the handlers with TDX.When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason,intr_info,exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)TDX:
- get exit reason,intr_info,exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
Quoted 5 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Message-Id: <0396a9ae70d293c9d0b060349dae385a8a4fbcec.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 50 lines
--- arch/x86/kvm/vmx/vmx.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/vmx.c +7
-9@@ -7000,24 +7000,22 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) { -u32 intr_info =vmx_get_intr_info (&vmx->vcpu); -/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check (); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;@@ -7040,9 +7038,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_irqoff (vmx); +handle_exception_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Re: [PATCH 09/21] KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
In reply to Paolo BonziniQuoted 19 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass
intr_infoto the NMI and INTR handlers instead of pulling it fromvcpu_vmxin preparation for sharing the bulk of the handlers with TDX.When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason,intr_info,exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)TDX:
- get exit reason,intr_info,exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
Quoted 5 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Message-Id: <0396a9ae70d293c9d0b060349dae385a8a4fbcec.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 50 lines
--- arch/x86/kvm/vmx/vmx.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/vmx.c +7
-9@@ -7000,24 +7000,22 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) { -u32 intr_info =vmx_get_intr_info (&vmx->vcpu); -/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check (); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;@@ -7040,9 +7038,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_irqoff (vmx); +handle_exception_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Re: [PATCH 09/21] KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass
intr_infoto the NMI and INTR handlers instead of pulling it fromvcpu_vmxin preparation for sharing the bulk of the handlers with TDX.When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS.
IMHO this can be simpler:
TDX conveys VM exit information via GPRs while normal VMX does via VMCS fields.
The eventual code will be
Quoted 9 lines
VMX:
- get exit reason,intr_info,exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)TDX:
- get exit reason,intr_info,exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
It's kinda nicer to mention why to change handle_exception_irqoff()'s first argument from @vmx to @vcpu.
Anyway, doesn't matter ...
Quoted 6 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Message-Id: <0396a9ae70d293c9d0b060349dae385a8a4fbcec.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
... Acked-by: Kai Huang <[email protected]>
Quoted 50 lines
--- arch/x86/kvm/vmx/vmx.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/vmx.c +7
-9@@ -7000,24 +7000,22 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) { -u32 intr_info =vmx_get_intr_info (&vmx->vcpu); -/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check (); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;@@ -7040,9 +7038,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_irqoff (vmx); +handle_exception_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Paolo Bonzini
maintainerPATCH 10/21 KVM: SEV: Use a VMSA physical address variable for populating VMCB
In reply to Paolo BonziniFrom: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
Message-Id: <[email protected]>
--- arch/x86/kvm/svm/sev.c | 3 +-- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +1 -2
@@ -3094,8 +3094,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)* the VMSA will be NULL if this vCPU is the destination for intrahost * migration, and will be copied later. */ -if (svm->sev_es.vmsa) - svm->vmcb->control.vmsa_pa =__pa (svm->sev_es.vmsa); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa;/* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept (svm,INTERCEPT_CR0_READ );
arch/x86/kvm/svm/svm.c +8 -1
@@ -1459,9 +1459,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm->vmcb01.pa =__sme_set (page_to_pfn (vmcb01_page) <<PAGE_SHIFT );svm_switch_vmcb (svm, &svm->vmcb01); -if (vmsa_page) +if (vmsa_page) { svm->sev_es.vmsa =page_address (vmsa_page); +/* +* Do not include the encryption mask on the VMSA physical +* address since hardware will access it using the guest key. +*/ + svm->sev_es.vmsa_pa =__pa (svm->sev_es.vmsa); + } + svm->guest_state_loaded = false;return 0 ;
arch/x86/kvm/svm/svm.h +1 -0
@@ -198,6 +198,7 @@ struct vcpu_sev_es_state {struct ghcb *ghcb;u8 valid_bitmap[16 ];struct kvm_host_map ghcb_map; +hpa_t vmsa_pa;bool received_first_sipi;/* SEV-ES scratch area support */
Signature · 1 line
2.39.0
Sean Christopherson
maintainerRe: [PATCH 10/21] KVM: SEV: Use a VMSA physical address variable for populating VMCB
Quoted 6 lines · Paolo Bonzini
From: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
No, this patch belongs in the SNP series. The hanlding of vmsa_pa is broken (KVM leaks the page set by the guest; I need to follow-up in the SNP series). On top of that, I detest duplicat variables, and I don't like that KVM keeps its original VMSA (kernel allocation) after the guest creates its own.
I can't possibly imagine why this needs to be pulled in early. There's no way TDX needs this, and while this patch is _small_, the functional change it leads to is not.
Paolo Bonzini
maintainerRe: [PATCH 10/21] KVM: SEV: Use a VMSA physical address variable for populating VMCB
Quoted 9 lines · Sean Christopherson
Quoted 6 lines · Paolo Bonzini
From: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
Quoted 8 lines
No, this patch belongs in the SNP series. The hanlding of
vmsa_pais broken (KVM leaks the page set by the guest; I need to follow-up in the SNP series). On top of that, I detest duplicat variables, and I don't like that KVM keeps its original VMSA (kernel allocation) after the guest creates its own.I can't possibly imagine why this needs to be pulled in early. There's no way TDX needs this, and while this patch is _small_, the functional change it leads to is not.
Well, the point of this series (and there will be more if you agree) is exactly to ask "why not" in a way that is more manageable than through the huge TDX and SNP series. My reading of the above is that you believe this is small enough that it can even be merged with "KVM:
SEV: Support SEV-SNP AP Creation NAE event" (with fixes), which I don't disagree with.
Otherwise, if the approach was good there's no reason _not_ to get it in early. It's just a refactoring.
Talking in general: I think I agree about keeping the gmem parts in a kvm-coco-queue branch (and in the meanwhile involving the mm people if mm/filemap.c changes are needed). #VE too, probably, but what I _really_ want to avoid is that these series (the plural is not a typo) become a new bottleneck for everybody. Basically these are meant to be a "these seem good to go to me, please confirm or deny" between comaintainers more than a real patch posting; having an extra branch is extra protection against screwups but we should be mindful that force pushes are painful for everyone.
If you think I'm misguided, please do speak out or feel free to ask me to talk on voice.
Paolo
Sean Christopherson
maintainerRe: [PATCH 10/21] KVM: SEV: Use a VMSA physical address variable for populating VMCB
Quoted 19 lines · Paolo Bonzini
Quoted 9 lines · Sean Christopherson
Quoted 6 lines · Paolo Bonzini
From: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
Quoted 8 lines
No, this patch belongs in the SNP series. The hanlding of
vmsa_pais broken (KVM leaks the page set by the guest; I need to follow-up in the SNP series). On top of that, I detest duplicat variables, and I don't like that KVM keeps its original VMSA (kernel allocation) after the guest creates its own.I can't possibly imagine why this needs to be pulled in early. There's no way TDX needs this, and while this patch is _small_, the functional change it leads to is not.
Quoted 6 lines
Well, the point of this series (and there will be more if you agree) is exactly to ask "why not" in a way that is more manageable than through the huge TDX and SNP series. My reading of the above is that you believe this is small enough that it can even be merged with "KVM:
SEV: Support SEV-SNP AP Creation NAE event" (with fixes), which I don't disagree with.
Maybe? That wasn't my point.
Quoted 2 lines
Otherwise, if the approach was good there's no reason _not_ to get it in early. It's just a refactoring.
It's not really a refactoring though, that's why I'm objecting. If this patch stored _just_ the physical adddress of the VMSA, then I would consider it a refactoring and would have no problem applying it earlier.
But this patch adds a second, 100% duplicate field (as of now), and the reason it does so is to allow "svm->sev_es.vmsa" to become disconnected from the "real" VMSA that is used by hardware, which is all kinds of messed up. That's what I meant by "the functional change it leads to is not (small)".
Quoted 9 lines
Talking in general: I think I agree about keeping the gmem parts in a kvm-coco-queue branch (and in the meanwhile involving the mm people if
mm/filemap.cchanges are needed). #VE too, probably, but what I _really_ want to avoid is that these series (the plural is not a typo) become a new bottleneck for everybody. Basically these are meant to be a "these seem good to go to me, please confirm or deny" between comaintainers more than a real patch posting; having an extra branch is extra protection against screwups but we should be mindful that force pushes are painful for everyone.
Yes, which is largely why I suggested we separate the gmem. I suspect we'll need to force push to fixup gmem things, whereas I'm confident the other prep work won't need to be tweaked once it's fully reviewed.
For the other stuff, specifically to avoid creating another bottleneck, my preference is to follow the "normal" rules for posting patches, with slightly relaxed bundling rules. I.e. post multiple, independent series so that they can be reviewed, iterated upon, and applied like any other series.
E.g. my objection to this VMSA tracking patch shouldn't get in the way of the MMU changes, the #VE patch shoudln't interfere with the vmx/main.c patch, etc. In other words, throwing everything into a kitchen sink "TDX/SNP prep work" series just creates another (smaller) bottleneck.
I am 100% in favor of applying prep patches in advance of the larger SNP and TDX series. That's actually partly why I ended up posting my series that includes the PFERR_PRIVATE_ACCESS patch; I was trying to pull in using PFERR_GUEST_ENC_MASK and some of the other "simple" patches, and the darn thing failed on me.
Paolo Bonzini
maintainerPATCH 11/21 KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at allocation
In reply to Paolo BonziniFrom: Isaku Yamahata <[email protected]>
Refactor tdp_mmu_alloc_sp() and tdp_mmu_init_sp and eliminate tdp_mmu_init_child_sp(). Currently tdp_mmu_init_sp() (or tdp_mmu_init_child_sp()) sets kvm_mmu_page.role after tdp_mmu_alloc_sp() allocating struct kvm_mmu_page and its page table page. This patch makes tdp_mmu_alloc_sp() initialize kvm_mmu_page.role instead of tdp_mmu_init_sp().
To handle private page tables, argument of is_private needs to be passed down. Given that already page level is passed down, it would be cumbersome to add one more parameter about sp. Instead replace the level argument with union kvm_mmu_page_role. Thus the number of argument won't be increased and more info about sp can be passed down.
For private sp, secure page table will be also allocated in addition to struct kvm_mmu_page and page table (spt member). The allocation functions (tdp_mmu_alloc_sp() and __tdp_mmu_alloc_sp_for_split()) need to know if the allocation is for the conventional page table or private page table. Pass union kvm_mmu_role to those functions and initialize role member of struct kvm_mmu_page.
Message-Id: <d69acdd7f0b0b104f330a6d42ac28f9a9b1b5850.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/mmu/tdp_iter.h | 12 ++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 44 ++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 25 deletions(-)
arch/x86/kvm/mmu/tdp_iter.h +12 -0
@@ -135,4 +135,16 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,void tdp_iter_next (struct tdp_iter *iter);void tdp_iter_restart (struct tdp_iter *iter); +static inline union kvm_mmu_page_role tdp_iter_child_role (struct tdp_iter *iter) +{ +union kvm_mmu_page_role child_role; +struct kvm_mmu_page *parent_sp; + + parent_sp =sptep_to_sp (rcu_dereference (iter->sptep)); + + child_role = parent_sp->role; + child_role.level--; +return child_role; +} +#endif /* __KVM_X86_MMU_TDP_ITER_H */
arch/x86/kvm/mmu/tdp_mmu.c +19 -25
119 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH 11/21] KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at allocation
Quoted 3 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
Refactor
tdp_mmu_alloc_sp()andtdp_mmu_init_spand eliminate
^
tdp_mmu_init_sp()
Quoted 10 lines
tdp_mmu_init_child_sp(). Currentlytdp_mmu_init_sp()(ortdp_mmu_init_child_sp()) sets kvm_mmu_page.role aftertdp_mmu_alloc_sp()allocating structkvm_mmu_pageand its page table page. This patch makestdp_mmu_alloc_sp()initialize kvm_mmu_page.role instead oftdp_mmu_init_sp().To handle private page tables, argument of
is_privateneeds to be passed down. Given that already page level is passed down, it would be cumbersome to add one more parameter about sp. Instead replace the level argument with unionkvm_mmu_page_role. Thus the number of argument won't be increased
This section is hard to understand. I'm lost at which functions are mentioned here that took the level argument and should be replaced by role.
Quoted 1 line
and more info about sp can be passed down.
My understanding of the change is:
Extra handling is need for Allocation of private page tables, so earlier caculate the kvm_mmu_page_role for the sp and pass it to tdp_mmu_alloc_sp(). Since the sp.role could be decided on sp allocation, in turn remove the role argument for tdp_mmu_init_sp(), also eliminate the helper tdp_mmu_init_child_sp().
Quoted 6 lines
For private sp, secure page table will be also allocated in addition to struct
kvm_mmu_pageand page table (spt member). The allocation functions (tdp_mmu_alloc_sp()and__tdp_mmu_alloc_sp_for_split()) need to know if the allocation is for the conventional page table or private page table. Pass unionkvm_mmu_roleto those functions and initialize role member of struct
^
Should be kvm_mmu_page_role
Thanks,
Yilun
Re: [PATCH 11/21] KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at allocation
In reply to Paolo BonziniQuoted 22 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
Refactor
tdp_mmu_alloc_sp()andtdp_mmu_init_spand eliminate tdp_mmu_init_child_sp(). Currentlytdp_mmu_init_sp()(ortdp_mmu_init_child_sp()) sets kvm_mmu_page.role aftertdp_mmu_alloc_sp()allocating structkvm_mmu_pageand its page table page. This patch makestdp_mmu_alloc_sp()initialize kvm_mmu_page.role instead oftdp_mmu_init_sp().To handle private page tables, argument of
is_privateneeds to be passed down. Given that already page level is passed down, it would be cumbersome to add one more parameter about sp. Instead replace the level argument with union kvm_mmu_page_role. Thus the number of argument won't be increased and more info about sp can be passed down.For private sp, secure page table will be also allocated in addition to struct
kvm_mmu_pageand page table (spt member). The allocation functions (tdp_mmu_alloc_sp()and__tdp_mmu_alloc_sp_for_split()) need to know if the allocation is for the conventional page table or private page table. Pass unionkvm_mmu_roleto those functions and initialize role member of structkvm_mmu_page.
Quoted 3 lines
Signed-off-by Isaku Yamahata <[email protected]>Message-Id: <d69acdd7f0b0b104f330a6d42ac28f9a9b1b5850.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
We were discussing on v19 of the TDX series whether we could drop this patch and end up with simpler code in later patches:
https://lore.kernel.org/lkml/[email protected]/
TDX can manage in either case, but it might not be needed for TDX. Does it have any benefit for SNP?
Paolo Bonzini
maintainerPATCH 12/21 KVM: x86/tdp_mmu: Sprinkle __must_check
In reply to Paolo BonziniFrom: Isaku Yamahata <[email protected]>
TDP MMU allows tdp_mmu_set_spte_atomic() and tdp_mmu_zap_spte_atomic() to return -EBUSY or -EAGAIN error. The caller must check the return value and retry. Add __must_check to ensure that it does so.
Message-Id: <8f7d5a1b241bf5351eaab828d1a1efe5c17699ca.1705965635.git.isaku.yamahata@intel.com>
--- arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +5 -5
@@ -539,9 +539,9 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* no side-effects other than setting iter->old_spte to the last * known value of the spte. */ -static inline int tdp_mmu_set_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter, -u64 new_spte) +static inline int __must_check tdp_mmu_set_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter, +u64 new_spte) {u64 *sptep =rcu_dereference (iter->sptep);
@@ -571,8 +571,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return 0 ; } -static inline int tdp_mmu_zap_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter) +static inline int __must_check tdp_mmu_zap_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter) {int ret;
Signature · 1 line
2.39.0
Re: [PATCH 12/21] KVM: x86/tdp_mmu: Sprinkle __must_check
Quoted 10 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
TDP MMU allows
tdp_mmu_set_spte_atomic()andtdp_mmu_zap_spte_atomic()to return-EBUSYor-EAGAINerror. The caller must check the return value and retry. Add__must_checkto ensure that it does so.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Binbin Wu <[email protected]>Message-Id: <8f7d5a1b241bf5351eaab828d1a1efe5c17699ca.1705965635.git.isaku.yamahata@intel.com>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 32 lines
--- arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +5
-5@@ -539,9 +539,9 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* no side-effects other than setting iter->old_spte to the last * known value of the spte. */ -static inline int tdp_mmu_set_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter, -u64 new_spte) +static inline int __must_check tdp_mmu_set_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter, +u64 new_spte) {u64 *sptep =rcu_dereference (iter->sptep);@@ -571,8 +571,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return 0 ; } -static inline int tdp_mmu_zap_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter) +static inline int __must_check tdp_mmu_zap_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter) {int ret;
Paolo Bonzini
maintainerPATCH 13/21 KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
In reply to Paolo BonziniFrom: Isaku Yamahata <[email protected]>
In some cases the full 64-bit error code for the KVM page fault will be needed to determine things like whether or not a fault was for a private or shared guest page, so update related code to accept the full 64-bit value so it can be plumbed all the way through to where it is needed.
The use of lower_32_bits() moves from kvm_mmu_page_fault() to FNAME(page_fault), since walking is independent of the data in the upper bits of the error code.
[mdr: drop references/changes to code not in current gmem tree, update
commit message]
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -2
@@ -5869,8 +5869,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false, + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false, &emulation_type);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ;
arch/x86/kvm/mmu/mmu_internal.h +2 -2
@@ -190,7 +190,7 @@ static inline bool is_nx_huge_page_enabled(struct kvm *kvm)struct kvm_page_fault {/* arguments to kvm_mmu_do_page_fault. */ const gpa_t addr; -const u32 error_code; +const u64 error_code;const bool prefetch;/* Derived from error_code. */
@@ -280,7 +280,7 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch,int *emulation_type) +u64 err,bool prefetch,int *emulation_type) {struct kvm_page_fault fault = { .addr = cr2_or_gpa,
arch/x86/kvm/mmu/mmutrace.h +1 -1
@@ -260,7 +260,7 @@ TRACE_EVENT(TP_STRUCT__entry (__field (int ,vcpu_id )__field (gpa_t, cr2_or_gpa) -__field (u32, error_code) +__field (u64, error_code) __field(u64 *, sptep)__field (u64, old_spte) __field(u64, new_spte)
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -787,7 +787,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* The bit needs to be cleared before walking guest page tables. */ r =FNAME (walk_addr)(&walker, vcpu, fault->addr, - fault->error_code & ~PFERR_RSVD_MASK ); +lower_32_bits (fault->error_code) & ~PFERR_RSVD_MASK );/* * The page is not mapped by the guest. Let the guest handle it.
Signature · 1 line
2.39.0
Re: [PATCH 13/21] KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
Quoted 2 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
...
Quoted 3 lines
The use of
lower_32_bits()moves fromkvm_mmu_page_fault()toFNAME(page_fault), since walking is independent of the data in the upper bits of the error code.
Is it a must? I don't see any issue if full u64 error_code is passed to FNAME(page_fault) as well.
Sean Christopherson
maintainerRe: [PATCH 13/21] KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
Quoted 10 lines · Xiaoyao Li
Quoted 2 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
...
Quoted 3 lines
The use of
lower_32_bits()moves fromkvm_mmu_page_fault()toFNAME(page_fault), since walking is independent of the data in the upper bits of the error code.Is it a must? I don't see any issue if full u64
error_codeis passed toFNAME(page_fault)as well.
Heh, my thought as well.
Paolo Bonzini
maintainerRe: [PATCH 13/21] KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
In reply to Xiaoyao LiQuoted 11 lines · Xiaoyao Li
Quoted 2 lines · Paolo Bonzini
From: Isaku Yamahata <[email protected]>
...
Quoted 3 lines
The use of
lower_32_bits()moves fromkvm_mmu_page_fault()toFNAME(page_fault), since walking is independent of the data in the upper bits of the error code.Is it a must? I don't see any issue if full u64
error_codeis passed toFNAME(page_fault)as well.
The full error code *is* passed to kvm_mmu_do_page_fault() and FNAME(page_fault), it's only dropped when passed to FNAME(walk_addr).
Paolo
Paolo Bonzini
maintainerPATCH 14/21 KVM: x86/mmu: pass error code back to MMU when async pf is ready
In reply to Paolo BonziniRight now the error code is not used when an async page fault is completed. This is not a problem in the current code, but it is untidy. For protected VMs we need to check that the page attributes match the current state of the page. Async page faults can only occur on shared pages (because private pages go through kvm_faultin_pfn_private() instead of __gfn_to_pfn_memslot()), but it is risky to rely on the polarity of PFERR_GUEST_ENC_MASK and the high 32 bits of the error code being zero. So, for clarity and future-proofing of the code, pipe the error code from kvm_arch_setup_async_pf() to kvm_arch_async_page_ready() via the architecture-specific async page fault data.
Extracted from a patch by Isaku Yamahata.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1839,6 +1839,7 @@ struct kvm_arch_async_pf {gfn_t gfn;unsigned long cr3;bool direct_map; +u64 error_code; };extern u32 __read_mostly kvm_nr_uret_msrs;
arch/x86/kvm/mmu/mmu.c +7 -7
@@ -4260,18 +4260,18 @@ static u32 alloc_apf_token(struct kvm_vcpu *vcpu)return (vcpu->arch.apf.id++ <<12 ) | vcpu->vcpu_id; } -static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -gfn_t gfn) +static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) {struct kvm_arch_async_pf arch; arch.token =alloc_apf_token (vcpu); - arch.gfn = gfn; + arch.gfn = fault->gfn; arch.direct_map = vcpu->arch.mmu->root_role.direct; arch.cr3 =kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu); -return kvm_setup_async_pf (vcpu, cr2_or_gpa, -kvm_vcpu_gfn_to_hva (vcpu, gfn), &arch); +return kvm_setup_async_pf (vcpu, fault->addr, +kvm_vcpu_gfn_to_hva (vcpu, fault->gfn), &arch); }void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu,struct kvm_async_pf *work)
@@ -4290,7 +4290,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu)) return; -kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true,NULL ); +kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa, work->arch.error_code, true,NULL ); }static inline u8 kvm_max_level_for_order (int order)
@@ -4395,7 +4395,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faulttrace_kvm_async_pf_repeated_fault (fault->addr, fault->gfn);kvm_make_request (KVM_REQ_APF_HALT , vcpu);return RET_PF_RETRY ; - }else if (kvm_arch_setup_async_pf (vcpu, fault->addr, fault->gfn)) { + }else if (kvm_arch_setup_async_pf (vcpu, fault)) {return RET_PF_RETRY ; } }
Signature · 1 line
2.39.0
Sean Christopherson
maintainerRe: [PATCH 14/21] KVM: x86/mmu: pass error code back to MMU when async pf is ready
Quoted 60 lines · Paolo Bonzini
Right now the error code is not used when an async page fault is completed. This is not a problem in the current code, but it is untidy. For protected VMs we need to check that the page attributes match the current state of the page. Async page faults can only occur on shared pages (because private pages go through
kvm_faultin_pfn_private()instead of__gfn_to_pfn_memslot()), but it is risky to rely on the polarity ofPFERR_GUEST_ENC_MASKand the high 32 bits of the error code being zero. So, for clarity and future-proofing of the code, pipe the error code fromkvm_arch_setup_async_pf()tokvm_arch_async_page_ready()via the architecture-specific async page fault data.Extracted from a patch by Isaku Yamahata.
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1839,6 +1839,7 @@ struct kvm_arch_async_pf {gfn_t gfn;unsigned long cr3;bool direct_map; +u64 error_code; };extern u32 __read_mostly kvm_nr_uret_msrs;arch/x86/kvm/mmu/mmu.c +5
-5@@ -4260,18 +4260,18 @@ static u32 alloc_apf_token(struct kvm_vcpu *vcpu)return (vcpu->arch.apf.id++ <<12 ) | vcpu->vcpu_id; } -static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -gfn_t gfn) +static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) {struct kvm_arch_async_pf arch; arch.token =alloc_apf_token (vcpu); - arch.gfn = gfn; + arch.gfn = fault->gfn; arch.direct_map = vcpu->arch.mmu->root_role.direct; arch.cr3 =kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu); -return kvm_setup_async_pf (vcpu, cr2_or_gpa, -kvm_vcpu_gfn_to_hva (vcpu, gfn), &arch); +return kvm_setup_async_pf (vcpu, fault->addr, +kvm_vcpu_gfn_to_hva (vcpu, fault->gfn), &arch); }void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu,struct kvm_async_pf *work)
arch/x86/kvm/mmu/mmu.c void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) · line 4293
Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +1
-1@@ -4290,7 +4290,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu))return ; -kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true,NULL ); +kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa, work->arch.error_code, true,NULL );
This is silly. If we're going to bother plumbing in the error code, then we should use it to do sanity checks. Things have gone off the rails if end up with an async #PF on private memory.
Quoted 16 lines
arch/x86/kvm/mmu/mmu.c +1
-1}static inline u8 kvm_max_level_for_order (int order)@@ -4395,7 +4395,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faulttrace_kvm_async_pf_repeated_fault (fault->addr, fault->gfn);kvm_make_request (KVM_REQ_APF_HALT , vcpu);return RET_PF_RETRY ; - }else if (kvm_arch_setup_async_pf (vcpu, fault->addr, fault->gfn)) { + }else if (kvm_arch_setup_async_pf (vcpu, fault)) {return RET_PF_RETRY ; } }Signature · 1 line
2.39.0
Paolo Bonzini
maintainerRe: [PATCH 14/21] KVM: x86/mmu: pass error code back to MMU when async pf is ready
Quoted 65 lines · Sean Christopherson
Quoted 63 lines · Paolo Bonzini
Right now the error code is not used when an async page fault is completed. This is not a problem in the current code, but it is untidy. For protected VMs we need to check that the page attributes match the current state of the page. Async page faults can only occur on shared pages (because private pages go through
kvm_faultin_pfn_private()instead of__gfn_to_pfn_memslot()), but it is risky to rely on the polarity ofPFERR_GUEST_ENC_MASKand the high 32 bits of the error code being zero. So, for clarity and future-proofing of the code, pipe the error code fromkvm_arch_setup_async_pf()tokvm_arch_async_page_ready()via the architecture-specific async page fault data.Extracted from a patch by Isaku Yamahata.
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1839,6 +1839,7 @@ struct kvm_arch_async_pf {gfn_t gfn;unsigned long cr3;bool direct_map; +u64 error_code; };extern u32 __read_mostly kvm_nr_uret_msrs;arch/x86/kvm/mmu/mmu.c +5
-5@@ -4260,18 +4260,18 @@ static u32 alloc_apf_token(struct kvm_vcpu *vcpu)return (vcpu->arch.apf.id++ <<12 ) | vcpu->vcpu_id; } -static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -gfn_t gfn) +static bool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) {struct kvm_arch_async_pf arch; arch.token =alloc_apf_token (vcpu); - arch.gfn = gfn; + arch.gfn = fault->gfn; arch.direct_map = vcpu->arch.mmu->root_role.direct; arch.cr3 =kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu); -return kvm_setup_async_pf (vcpu, cr2_or_gpa, -kvm_vcpu_gfn_to_hva (vcpu, gfn), &arch); +return kvm_setup_async_pf (vcpu, fault->addr, +kvm_vcpu_gfn_to_hva (vcpu, fault->gfn), &arch); }void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu,struct kvm_async_pf *work)@@ -4290,7 +4290,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu))return ;
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) · line 4293
Quoted 2 lines
arch/x86/kvm/mmu/mmu.c +1
-1-kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true,NULL ); +kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa, work->arch.error_code, true,NULL );This is silly. If we're going to bother plumbing in the error code, then we should use it to do sanity checks. Things have gone off the rails if end up with an async #PF on private memory.
Sure, I split this part out not just because it makes sense to do so, but also because it's not strictly necessary. I'll add the check and tweak the changelog.
Paolo
Quoted 18 lines
Quoted 17 lines
arch/x86/kvm/mmu/mmu.c +1
-2}static inline u8 kvm_max_level_for_order (int order)@@ -4395,7 +4395,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faulttrace_kvm_async_pf_repeated_fault (fault->addr, fault->gfn);kvm_make_request (KVM_REQ_APF_HALT , vcpu);return RET_PF_RETRY ; - }else if (kvm_arch_setup_async_pf (vcpu, fault->addr, fault->gfn)) { + }else if (kvm_arch_setup_async_pf (vcpu, fault)) {return RET_PF_RETRY ; } } --2.39.0
Paolo Bonzini
maintainerPATCH 15/21 KVM: x86/mmu: Use PFERR_GUEST_ENC_MASK to indicate fault is private
In reply to Paolo BonziniSEV-SNP defines PFERR_GUEST_ENC_MASK (bit 34) in page-fault error bits to represent the guest page is encrypted. Use the bit to designate that the page fault is private and that it requires looking up memory attributes.
The vendor kvm page fault handler should set PFERR_GUEST_ENC_MASK bit based on their fault information. It may or may not use the hardware value directly or parse the hardware value to set the bit.
Based on a patch by Isaku Yamahata.
---
Compared to what is in the Intel TDX tree, I am dropping the
if (vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM)
return RET_PF_RETRY;
change in __kvm_faultin_pfn(). It is not well documented why it is needed and selftests seem to pass.
Also, checking has_private_mem is needed so as not to break SEV-ES.
arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 9 +++++++++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -264,6 +264,7 @@ enum x86_intercept_stage;#define PFERR_SGX_BIT 15#define PFERR_GUEST_FINAL_BIT 32#define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34#define PFERR_IMPLICIT_ACCESS_BIT 48#define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT)
@@ -275,6 +276,7 @@ enum x86_intercept_stage;#define PFERR_SGX_MASK BIT(PFERR_SGX_BIT)#define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_ENC_MASK BIT_ULL(PFERR_GUEST_ENC_BIT)#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
arch/x86/kvm/mmu/mmu.c +9 -0
@@ -5846,6 +5846,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err {int r, emulation_type =EMULTYPE_PF ;bool direct = vcpu->arch.mmu->root_role.direct; +struct kvm *kvm = vcpu->kvm;/* * IMPLICIT_ACCESS is a KVM-defined flag used to correctly perform SMAP
@@ -5861,6 +5862,14 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 errif (WARN_ON_ONCE (!VALID_PAGE (vcpu->arch.mmu->root.hpa)))return RET_PF_RETRY ; +/* +* There is no vendor code that can set PFERR_GUEST_ENC_MASK for +* software-protected VMs. Compute it here. +*/ +if (kvm->arch.vm_type ==KVM_X86_SW_PROTECTED_VM && +kvm_mem_is_private (kvm, cr2_or_gpa >>PAGE_SHIFT )) + error_code |=PFERR_GUEST_ENC_MASK ; + r =RET_PF_INVALID ;if (unlikely (error_code &PFERR_RSVD_MASK )) { r =handle_mmio_page_fault (vcpu, cr2_or_gpa, direct);
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -290,6 +290,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .present = err & PFERR_PRESENT_MASK, .rsvd = err & PFERR_RSVD_MASK, .user = err & PFERR_USER_MASK, + .is_private = vcpu->kvm->arch.has_private_mem && (err & PFERR_GUEST_ENC_MASK), .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled =
@@ -298,7 +299,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), };int r;
Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 16/21 KVM: guest_memfd: pass error up from filemap_grab_folio
In reply to Paolo BonziniSome SNP ioctls will require the page not to be in the pagecache, and as such they will want to return EEXIST to userspace. Start by passing the error up from filemap_grab_folio.
--- virt/kvm/guest_memfd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +3 -3
@@ -20,7 +20,7 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index)/* TODO: Support huge pages. */ folio =filemap_grab_folio (inode->i_mapping, index);if (IS_ERR_OR_NULL (folio)) -return NULL ; +return folio;/* * Use the up-to-date flag to track whether or not the memory has been
@@ -146,8 +146,8 @@ static long kvm_gmem_allocate(struct inode *inode, loff_t offset, loff_t len) } folio =kvm_gmem_get_folio (inode, index); -if (!folio) { - r = -ENOMEM ; +if (IS_ERR_OR_NULL (folio)) { + r = folio ?PTR_ERR (folio) : -ENOMEM ;break ; }
Signature · 1 line
2.39.0
Re: [PATCH 16/21] KVM: guest_memfd: pass error up from filemap_grab_folio
Quoted 14 lines · Paolo Bonzini
Some SNP ioctls will require the page not to be in the pagecache, and as such they will want to return EEXIST to userspace. Start by passing the error up from
filemap_grab_folio.Signed-off-by Paolo Bonzini <[email protected]>--- virt/kvm/guest_memfd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)virt/kvm/guest_memfd.c +0
-0
virt/kvm/guest_memfd.c static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) · line 23
Quoted 5 lines
virt/kvm/guest_memfd.c +1
-1@@ -20,7 +20,7 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index)/* TODO: Support huge pages. */ folio =filemap_grab_folio (inode->i_mapping, index);if (IS_ERR_OR_NULL (folio)) -return NULL ; +return folio;
I think it impacts kvm_gmem_get_pfn()
{
...
folio = kvm_gmem_get_folio(file_inode(file), index);
if (!folio) {
r = -ENOMEM;
goto out_fput;
}
...
}Thanks,
Yilun
Quoted 19 lines
virt/kvm/guest_memfd.c +2
-2/* * Use the up-to-date flag to track whether or not the memory has been @@ -146,8 +146,8 @@ static long kvm_gmem_allocate(struct inode *inode, loff_t offset, loff_t len) } folio =kvm_gmem_get_folio (inode, index); -if (!folio) { - r = -ENOMEM ; +if (IS_ERR_OR_NULL (folio)) { + r = folio ?PTR_ERR (folio) : -ENOMEM ;break ; }Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 17/21 filemap: add FGP_CREAT_ONLY
In reply to Paolo Bonzini--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)
include/linux/pagemap.h +2 -0
@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance.
@@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)
mm/filemap.c +4 -0
@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) {
Signature · 1 line
2.39.0
Sean Christopherson
maintainerRe: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
On Tue, Feb 27, 2024, Paolo Bonzini wrote:
This needs a changelog, and also needs to be Cc'd to someone(s) that can give it a thumbs up.
Quoted 45 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)include/linux/pagemap.h +2
-0@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. @@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +4
-0@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) {Signature · 1 line
2.39.0
Re: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
Quoted 5 lines · Sean Christopherson
On Tue, Feb 27, 2024, Paolo Bonzini wrote:
This needs a changelog, and also needs to be Cc'd to someone(s) that can give it a thumbs up.
+Matthew Wilcox
Quoted 47 lines
Quoted 46 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)include/linux/pagemap.h +2
-0@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. @@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +4
-1@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) { --2.39.0
Matthew Wilcox
maintainerRe: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
Quoted 8 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson
On Tue, Feb 27, 2024, Paolo Bonzini wrote:
This needs a changelog, and also needs to be Cc'd to someone(s) that can give it a thumbs up.
+Matthew Wilcox
If only there were an entry in MAINTAINERS for filemap.c ...
This looks bogus to me, and if it's not bogus, it's incomplete. But it's hard to judge without a commit message that describes what it's supposed to mean.
Quoted 47 lines
Quoted 47 lines
Quoted 46 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)include/linux/pagemap.h +2
-0@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. @@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +4
-1@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) { --2.39.0
Paolo Bonzini
maintainerRe: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
Quoted 4 lines · Matthew Wilcox
Quoted 2 lines · Yosry Ahmed
Quoted 1 line · Sean Christopherson
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines · Paolo Bonzini
This needs a changelog, and also needs to be Cc'd to someone(s) that can give it a thumbs up.
+Matthew Wilcox
If only there were an entry in
MAINTAINERSforfilemap.c...
Not CCing you (or mm in general) was intentional because I first wanted a review of the KVM APIs; of course I wouldn't have committed it without an Acked-by. But yeah, not writing the changelog yet was pure laziness.
Since you're here: KVM would like to add a ioctl to encrypt and install a page into guest_memfd, in preparation for launching an encrypted guest. For this API we want to rule out the possibility of overwriting a page that is already in the guest_memfd's filemap, therefore this API would pass FGP_CREAT_ONLY|FGP_CREAT into__filemap_get_folio. Do you think this is bogus...
Quoted 1 line
This looks bogus to me, and if it's not bogus, it's incomplete.
... or if not, what incompleteness can you spot?
Thanks,
Paolo
Quoted 51 lines
But it's hard to judge without a commit message that describes what it's supposed to mean.
Quoted 47 lines
Quoted 47 lines
Quoted 46 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)include/linux/pagemap.h +2
-0@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. @@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +4
-1@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) { --2.39.0
Matthew Wilcox
maintainerRe: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
Quoted 6 lines · Paolo Bonzini
Since you're here: KVM would like to add a ioctl to encrypt and install a page into
guest_memfd, in preparation for launching an encrypted guest. For this API we want to rule out the possibility of overwriting a page that is already in the guest_memfd's filemap, therefore this API would pass FGP_CREAT_ONLY|FGP_CREATinto__filemap_get_folio. Do you think this is bogus...
Would it work to start out by either asserting the memfd is empty of pages, or by evicting any existing pages? Both those seem nicer than starting, realising you've got some unencrypted memory and aborting.
Quoted 3 lines
Quoted 1 line
This looks bogus to me, and if it's not bogus, it's incomplete.
... or if not, what incompleteness can you spot?
The part where we race another caller passing FGP_CREAT_ONLY and one gets an EEXIST back from filemap_add_folio(). Maybe that's not something that can happen in your use case, but it's at least semantics that need documenting.
Paolo Bonzini
maintainerRe: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Paolo Bonzini
Since you're here: KVM would like to add a ioctl to encrypt and install a page into
guest_memfd, in preparation for launching an encrypted guest. For this API we want to rule out the possibility of overwriting a page that is already in the guest_memfd's filemap, therefore this API would pass FGP_CREAT_ONLY|FGP_CREATinto__filemap_get_folio. Do you think this is bogus...
Quoted 3 lines
Would it work to start out by either asserting the memfd is empty of pages, or by evicting any existing pages? Both those seem nicer than starting, realising you've got some unencrypted memory and aborting.
Unfortunately it would be quite ugly to force userspace to do all the initialization in one go. For example, there are different kinds of pages that probably would be initialized at different points (e.g. before vs. after vCPUs are created, because the initial vCPU state is also encrypted).
The thing that I want to protect against is userspace trying to initialize the same encrypted page twice.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
This looks bogus to me, and if it's not bogus, it's incomplete.
... or if not, what incompleteness can you spot?
The part where we race another caller passing
FGP_CREAT_ONLYand one gets an EEXIST back fromfilemap_add_folio(). Maybe that's not something that can happen in your use case, but it's at least semantics that need documenting.
From the point of view of filemap_add_folio(), one of the racers wins and one fails. It doesn't matter to filemap.c if the missing synchronization is in the kernel or in userspace. In the case of KVM, the ioctl will return the number of pages before it found an existing page, or -EEXIST if that number is zero (similar to what nonblocking read does with EAGAIN).
I'll improve the documentation and changelog and make sure to Cc you on the next version.
Thanks again!
Paolo
Re: [PATCH 17/21] filemap: add FGP_CREAT_ONLY
In reply to Paolo BonziniQuoted 44 lines · Paolo Bonzini
Quoted 4 lines · Matthew Wilcox
Quoted 2 lines · Yosry Ahmed
Quoted 1 line · Sean Christopherson
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines · Paolo Bonzini
This needs a changelog, and also needs to be Cc'd to someone(s) that can give it a thumbs up.
+Matthew Wilcox
If only there were an entry in
MAINTAINERSforfilemap.c...Not CCing you (or mm in general) was intentional because I first wanted a review of the KVM APIs; of course I wouldn't have committed it without an Acked-by. But yeah, not writing the changelog yet was pure laziness.
Since you're here: KVM would like to add a ioctl to encrypt and install a page into
guest_memfd, in preparation for launching an encrypted guest. For this API we want to rule out the possibility of overwriting a page that is already in the guest_memfd's filemap, therefore this API would pass FGP_CREAT_ONLY|FGP_CREATinto__filemap_get_folio. Do you think this is bogus...Quoted 1 line
This looks bogus to me, and if it's not bogus, it's incomplete.
... or if not, what incompleteness can you spot?
Thanks,
Paolo
Quoted 10 lines
But it's hard to judge without a commit message that describes what it's supposed to mean.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 4 ++++ 2 files changed, 6 insertions(+)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
include/linux/pagemap.h pgoff_t page_cache_prev_miss(struct address_space *mapping, · line 589
Quoted 9 lines
include/linux/pagemap.h +1
-0@@ -586,6 +586,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* * %FGP_CREAT - If no folio is present then a new folio is allocated, * added to the page cache and the VM's LRU list. The folio is * returned locked. +* * %FGP_CREAT_ONLY - Fail if a folio is not present
^
So should be: Fail if a folio is present.
Thanks,
Yilun
Quoted 34 lines
Quoted 32 lines
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
include/linux/pagemap.h +1
-0* * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the * folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. @@ -606,6 +607,7 @@ typedef unsigned int __bitwise fgf_t;#define FGP_NOWAIT ((__force fgf_t)0x00000020)#define FGP_FOR_MMAP ((__force fgf_t)0x00000040)#define FGP_STABLE ((__force fgf_t)0x00000080) +#define FGP_CREAT_ONLY ((__force fgf_t)0x00000100)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +4
-1@@ -1854,6 +1854,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, folio =NULL ;if (!folio) goto no_page; +if (fgp_flags &FGP_CREAT_ONLY ) { +folio_put (folio); +return ERR_PTR (-EEXIST ); + }if (fgp_flags &FGP_LOCK ) {if (fgp_flags &FGP_NOWAIT ) { --2.39.0
Paolo Bonzini
maintainerPATCH 18/21 KVM: x86: Add gmem hook for initializing memory
In reply to Paolo Bonziniguest_memfd pages are generally expected to be in some arch-defined initial state prior to using them for guest memory. For SEV-SNP this initial state is 'private', or 'guest-owned', and requires additional operations to move these pages into a 'private' state by updating the corresponding entries the RMP table.
Allow for an arch-defined hook to handle updates of this sort, and go ahead and implement one for x86 so KVM implementations like AMD SVM can register a kvm_x86_ops callback to handle these updates for SEV-SNP guests.
The preparation callback is always called when allocating/grabbing folios via gmem, and it is up to the architecture to keep track of whether or not the pages are already in the expected state (e.g. the RMP table in the case of SEV-SNP).
In some cases, it is necessary to defer the preparation of the pages to handle things like in-place encryption of initial guest memory payloads before marking these pages as 'private'/'guest-owned', so also add a helper that performs the same function as kvm_gmem_get_pfn(), but allows for the preparation callback to be bypassed to allow for pages to be accessed beforehand.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 +++ include/linux/kvm_host.h | 14 ++++++ virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 72 +++++++++++++++++++++++++++--- 6 files changed, 92 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -139,6 +139,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1804,6 +1804,7 @@ struct kvm_x86_ops {unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags); +int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +6 -0
@@ -13598,6 +13598,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_arch_no_poll); +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order) +{ +return static_call (kvm_x86_gmem_prepare)(kvm, pfn, gfn, max_order); +} +#endif int kvm_spec_ctrl_test_value (u64 value) {
include/linux/kvm_host.h +14 -0
@@ -2434,6 +2434,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)#ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +int kvm_gmem_get_uninit_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2442,6 +2444,18 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm);return -EIO ; } + +static inline int kvm_gmem_get_uninit_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +kvm_pfn_t *pfn,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +}#endif /* CONFIG_KVM_PRIVATE_MEM */ +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order); +#endif +#endif
virt/kvm/Kconfig +4 -0
@@ -111,3 +111,7 @@ config KVM_GENERIC_PRIVATE_MEM select KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_PRIVATE_MEM bool + +config HAVE_KVM_GMEM_PREPARE + bool + depends on KVM_PRIVATE_MEM
virt/kvm/guest_memfd.c +66 -6
110 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH 18/21] KVM: x86: Add gmem hook for initializing memory
Quoted 153 lines · Paolo Bonzini
guest_memfdpages are generally expected to be in some arch-defined initial state prior to using them for guest memory. For SEV-SNP this initial state is 'private', or 'guest-owned', and requires additional operations to move these pages into a 'private' state by updating the corresponding entries the RMP table.Allow for an arch-defined hook to handle updates of this sort, and go ahead and implement one for x86 so KVM implementations like AMD SVM can register a
kvm_x86_opscallback to handle these updates for SEV-SNP guests.The preparation callback is always called when allocating/grabbing folios via gmem, and it is up to the architecture to keep track of whether or not the pages are already in the expected state (e.g. the RMP table in the case of SEV-SNP).
In some cases, it is necessary to defer the preparation of the pages to handle things like in-place encryption of initial guest memory payloads before marking these pages as 'private'/'guest-owned', so also add a helper that performs the same function as
kvm_gmem_get_pfn(), but allows for the preparation callback to be bypassed to allow for pages to be accessed beforehand.Co-developed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>Message-Id: <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 +++ include/linux/kvm_host.h | 14 ++++++ virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 72 +++++++++++++++++++++++++++--- 6 files changed, 92 insertions(+), 6 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -139,6 +139,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +1
-0@@ -1804,6 +1804,7 @@ struct kvm_x86_ops {unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags); +int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order); };struct kvm_x86_nested_ops {arch/x86/kvm/x86.c +6
-0@@ -13598,6 +13598,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_arch_no_poll); +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order) +{ +return static_call (kvm_x86_gmem_prepare)(kvm, pfn, gfn, max_order); +} +#endif int kvm_spec_ctrl_test_value (u64 value) {include/linux/kvm_host.h +14
-0@@ -2434,6 +2434,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)#ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +int kvm_gmem_get_uninit_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,@@ -2442,6 +2444,18 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm);return -EIO ; } + +static inline int kvm_gmem_get_uninit_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +kvm_pfn_t *pfn,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +}#endif /* CONFIG_KVM_PRIVATE_MEM */ +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order); +#endif +#endif virt/kvm/Kconfig +4
-0@@ -111,3 +111,7 @@ config KVM_GENERIC_PRIVATE_MEM select KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_PRIVATE_MEM bool + +config HAVE_KVM_GMEM_PREPARE + bool + depends on KVM_PRIVATE_MEMvirt/kvm/guest_memfd.c +22
-1@@ -13,12 +13,50 @@ struct kvm_gmem {struct list_head entry; }; -static struct folio *kvm_gmem_get_folio (struct inode *inode,pgoff_t index) +static int kvm_gmem_prepare_folio (struct inode *inode,pgoff_t index,struct folio *folio) +{ +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +struct list_head *gmem_list = &inode->i_mapping->i_private_list; +struct kvm_gmem *gmem; + +list_for_each_entry (gmem, gmem_list, entry) { +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +struct page *page; +kvm_pfn_t pfn; +gfn_t gfn; +int rc; + + slot =xa_load (&gmem->bindings, index); +if (!slot) +continue ; + + page =folio_file_page (folio, index); + pfn =page_to_pfn (page); + gfn = slot->base_gfn + index - slot->gmem.pgoff; + rc =kvm_arch_gmem_prepare (kvm, gfn, pfn,compound_order (compound_head (page)));
virt/kvm/guest_memfd.c struct kvm_gmem { · line 47
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+if (rc) { +pr_warn_ratelimited ("gmem: Failed to prepare folio for index %lx, error %d.\n" , + index, rc); +return rc; + } + } + +#endif +return 0 ; +}
Can we make it conditional?
TDX doesn't need prepare hook to set gmem_parepare = NULL. With large memory guest(several hundreds Gbyte) to lookup page cache, this loop slows down guest startup. I think it would also applies to SW_PROTECTED_VM (and pKVM in future).
include/linux/kvm_host.h +3 -0
@@ -842,6 +842,9 @@ struct kvm {#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES /* Protected by slots_locks (for writes) and RCU (for reads) */ struct xarray mem_attr_array; +#endif +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +bool gmem_need_prepare;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };
virt/kvm/guest_memfd.c +4 -0
@@ -16,6 +16,7 @@ struct kvm_gmem {static int kvm_gmem_prepare_folio (struct inode *inode,pgoff_t index,struct folio *folio) {#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE + rc =kvm_arch_gmem_prepare (inode, index, folio);struct list_head *gmem_list = &inode->i_mapping->i_private_list;struct kvm_gmem *gmem;
@@ -27,6 +28,9 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct folgfn_t gfn;int rc; +if (!kvm->gmem_need_prepare) +continue ; + slot =xa_load (&gmem->bindings, index);if (!slot)continue ;
Signature · 1 line
Isaku Yamahata <[email protected]>
Paolo Bonzini
maintainerPATCH 19/21 KVM: guest_memfd: add API to undo kvm_gmem_get_uninit_pfn
In reply to Paolo BonziniIn order to be able to redo kvm_gmem_get_uninit_pfn, a hole must be punched into the filemap, thus allowing FGP_CREAT_ONLY to succeed again. This will be used whenever an operation that follows kvm_gmem_get_uninit_pfn fails.
--- include/linux/kvm_host.h | 7 +++++++ virt/kvm/guest_memfd.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/linux/kvm_host.h +7 -0
@@ -2436,6 +2436,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order);int kvm_gmem_get_uninit_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +int kvm_gmem_undo_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,int order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2452,6 +2454,11 @@ static inline int kvm_gmem_get_uninit_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm); return -EIO; } + +static inline int kvm_gmem_undo_get_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +int order) +{}#endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE
virt/kvm/guest_memfd.c +28 -0
@@ -590,3 +590,31 @@ int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,return __kvm_gmem_get_pfn (kvm, slot, gfn, pfn, max_order, false); }EXPORT_SYMBOL_GPL (kvm_gmem_get_uninit_pfn); + +int kvm_gmem_undo_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,int order) +{ +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct file *file; +int r; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { + r = -EIO ; + goto out_fput; + } + + r =kvm_gmem_punch_hole (file_inode (file), index <<PAGE_SHIFT ,PAGE_SHIFT << order); + +out_fput: +fput (file); + +return r; +} +EXPORT_SYMBOL_GPL (kvm_gmem_undo_get_pfn);
Signature · 1 line
2.39.0
Re: [PATCH 19/21] KVM: guest_memfd: add API to undo kvm_gmem_get_uninit_pfn
Quoted 24 lines · Paolo Bonzini
In order to be able to redo
kvm_gmem_get_uninit_pfn, a hole must be punched into the filemap, thus allowingFGP_CREAT_ONLYto succeed again. This will be used whenever an operation that followskvm_gmem_get_uninit_pfnfails.Signed-off-by Paolo Bonzini <[email protected]>--- include/linux/kvm_host.h | 7 +++++++ virt/kvm/guest_memfd.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)include/linux/kvm_host.h +2
-0@@ -2436,6 +2436,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order);int kvm_gmem_get_uninit_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +int kvm_gmem_undo_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,int order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
include/linux/kvm_host.h static inline int kvm_gmem_get_uninit_pfn(struct kvm *kvm, · line 2461
Quoted 8 lines
include/linux/kvm_host.h +5
-0@@ -2452,6 +2454,11 @@ static inline int kvm_gmem_get_uninit_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm);return -EIO ; } + +static inline int kvm_gmem_undo_get_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +int order) +{}
return -EIO;
or compiler would complain that no return value.
Quoted 8 lines
include/linux/kvm_host.h +0
-0#endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE virt/kvm/guest_memfd.c +0
-0
virt/kvm/guest_memfd.c int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, · line 595
Quoted 6 lines
virt/kvm/guest_memfd.c +3
-0@@ -590,3 +590,31 @@ int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,return __kvm_gmem_get_pfn (kvm, slot, gfn, pfn, max_order, false); }EXPORT_SYMBOL_GPL (kvm_gmem_get_uninit_pfn); + +int kvm_gmem_undo_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,int order)
Didn't see the caller yet, but do we need to ensure the gfn is aligned with page order? e.g.
WARN_ON(gfn & ((1UL << order) - 1));
Quoted 8 lines
virt/kvm/guest_memfd.c +8
-0+{ +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct file *file; +int r; + + file =kvm_gmem_get_file (slot); +if (!file)
virt/kvm/guest_memfd.c int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, · line 613
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { + r = -EIO ; + goto out_fput; + } + + r =kvm_gmem_punch_hole (file_inode (file), index <<PAGE_SHIFT ,PAGE_SHIFT << order);
^
PAGE_SIZE << order
Thanks,
Yilun
Quoted 12 lines
virt/kvm/guest_memfd.c +7
-0+ +out_fput: +fput (file); + +return r; +} +EXPORT_SYMBOL_GPL (kvm_gmem_undo_get_pfn);Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 20/21 KVM: x86: Add gmem hook for invalidating memory
In reply to Paolo BonziniFrom: Michael Roth <[email protected]>
In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Wire up arch-defined hooks to the .free_folio kvm_gmem_aops callback to allow for special handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations and when releasing the inode, and go ahead and define an arch-specific hook for x86 since it will be needed for handling memory used for SEV-SNP guests.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 7 +++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/Kconfig | 4 ++++ virt/kvm/guest_memfd.c | 14 ++++++++++++++ 6 files changed, 31 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1805,6 +1805,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order); +void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +7 -0
@@ -13605,6 +13605,13 @@ int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_ord }#endif +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end) +{ +static_call_cond (kvm_x86_gmem_invalidate)(start, end); +} +#endif +int kvm_spec_ctrl_test_value (u64 value) {/*
include/linux/kvm_host.h +4 -0
@@ -2465,4 +2465,8 @@ static inline int kvm_gmem_undo_get_pfn(struct kvm *kvm,int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order);#endif +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end); +#endif +#endif
virt/kvm/Kconfig +4 -0
@@ -115,3 +115,7 @@ config KVM_GENERIC_PRIVATE_MEM config HAVE_KVM_GMEM_PREPARE bool depends on KVM_PRIVATE_MEM + +config HAVE_KVM_GMEM_INVALIDATE + bool + depends on KVM_PRIVATE_MEM
virt/kvm/guest_memfd.c +14 -0
@@ -345,10 +345,24 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folreturn MF_DELAYED ; } +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +static void kvm_gmem_free_folio (struct folio *folio) +{ +struct page *page =folio_page (folio,0 ); +kvm_pfn_t pfn =page_to_pfn (page); +int order =folio_order (folio); + +kvm_arch_gmem_invalidate (pfn, pfn + (1ul << order)); +} +#endif +static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE + .free_folio = kvm_gmem_free_folio, +#endif }; static int kvm_gmem_getattr(struct mnt_idmap *idmap, const struct path *path,
Signature · 1 line
2.39.0
Paolo Bonzini
maintainerPATCH 21/21 KVM: x86: Add gmem hook for determining max NPT mapping level
In reply to Paolo BonziniFrom: Michael Roth <[email protected]>
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance:
- gmem allocates 2MB page
- guest issues PVALIDATE on 2MB page
- guest later converts a subpage to shared
- SNP host code issues PSMASH to split 2MB RMP mapping to 4K
- KVM MMU splits NPT mapping to 4K
At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this.
Add a hook to determine the max NPT mapping size in situations like this.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 7 +++++++ 3 files changed, 9 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(gmem_validate_fault) KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OP
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1806,6 +1806,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order);void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); +int (*gmem_validate_fault)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -4339,6 +4339,13 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, fault->max_level); fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); + r =static_call (kvm_x86_gmem_validate_fault)(vcpu->kvm, fault->pfn, + fault->gfn, &fault->max_level); +if (r) { +kvm_release_pfn_clean (fault->pfn); +return r; + } +return RET_PF_CONTINUE ; }
Signature · 1 line
2.39.0
Re: [PATCH 21/21] KVM: x86: Add gmem hook for determining max NPT mapping level
Quoted 8 lines · Paolo Bonzini
From: Michael Roth<[email protected]>
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance:
Quoted 5 lines
- gmem allocates 2MB page
- guest issues PVALIDATE on 2MB page
- guest later converts a subpage to shared
- SNP host code issues PSMASH to split 2MB RMP mapping to 4K
- KVM MMU splits NPT mapping to 4K
Is here a sentence missing that "guest converts the shared subpage back to private"?
Otherwise, it conflicts with the following statement "there are no mixed attributes".
Quoted 60 lines
At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this.
Add a hook to determine the max NPT mapping size in situations like this.
Signed-off-by Michael Roth<[email protected]>Message-Id:<[email protected]>
Signed-off-by Paolo Bonzini<[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 7 +++++++ 3 files changed, 9 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(gmem_validate_fault) KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OParch/x86/include/asm/kvm_host.h +1
-0@@ -1806,6 +1806,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order);void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); +int (*gmem_validate_fault)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level); };struct kvm_x86_nested_ops {arch/x86/kvm/mmu/mmu.c +7
-0@@ -4339,6 +4339,13 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, fault->max_level); fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); + r =static_call (kvm_x86_gmem_validate_fault)(vcpu->kvm, fault->pfn, + fault->gfn, &fault->max_level); +if (r) { +kvm_release_pfn_clean (fault->pfn); +return r; + } +return RET_PF_CONTINUE ; }
Re: [PATCH 21/21] KVM: x86: Add gmem hook for determining max NPT mapping level
In reply to Paolo BonziniQuoted 49 lines · Paolo Bonzini
From: Michael Roth <[email protected]>
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance:
- gmem allocates 2MB page
- guest issues PVALIDATE on 2MB page
- guest later converts a subpage to shared
- SNP host code issues PSMASH to split 2MB RMP mapping to 4K
- KVM MMU splits NPT mapping to 4KAt this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this.
Add a hook to determine the max NPT mapping size in situations like this.
Signed-off-by Michael Roth <[email protected]>Message-Id: <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 7 +++++++ 3 files changed, 9 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(gmem_validate_fault) KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OParch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 1809
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -1806,6 +1806,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order);void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); +int (*gmem_validate_fault)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level);
Since it's named "gmem_validate_fault", can we just passed in the "fault" as an argument to avoid passing in pfn, gfn, max_level individually? I noticed in Isaku's TDX patch set, fault->private would also need to be passed in.
Quoted 21 lines
arch/x86/include/asm/kvm_host.h +0
-0};struct kvm_x86_nested_ops {arch/x86/kvm/mmu/mmu.c +7
-0@@ -4339,6 +4339,13 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, fault->max_level); fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); + r =static_call (kvm_x86_gmem_validate_fault)(vcpu->kvm, fault->pfn, + fault->gfn, &fault->max_level); +if (r) { +kvm_release_pfn_clean (fault->pfn); +return r; + } +return RET_PF_CONTINUE ; }
Sean Christopherson
maintainerQuoted 1 line · Paolo Bonzini
This is a first set of, hopefully non-controversial patches from the
Heh, you jinxed yourself. :-)
Quoted 18 lines
SNP and TDX series. They cover mostly changes to generic code and new gmem APIs, and in general have already been reviewed when posted by Isaku and Michael.
One important change is that the gmem hook for initializing memory is designed to return
-EEXISTif the page already exists in the guestmemfd filemap. The idea is that the special case ofKVM_SEV_SNP_LAUNCH_UPDATE, where__kvm_gmem_get_pfn()is used to return an uninitialized page and make it guest-owned, can be be done at most once per page unless the ioctl fails.Of course these patches add a bunch of dead code. This is intentional because it's the only way to trim the large TDX (and to some extent SNP) series to the point that it's possible to discuss them. The next step is probably going to be the private<->shared page logic from the TDX series.
Paolo
Quoted 8 lines
Isaku Yamahata (5):
KVM: x86/mmu: Add Suppress VE bit to EPT
shadow_mmio_mask/shadow_present_mask
KVM: VMX: Introduce test mode related to EPT violation VE
KVM: x86/tdp_mmu: Init role member of structkvm_mmu_pageat
allocation
KVM: x86/tdp_mmu: Sprinkle__must_check
KVM: x86/mmu: Pass around full 64-bit error code for KVM page faults
I have a slight tweak to this patch (drop truncation), and a rewritten changelog.
Quoted 7 lines
Michael Roth (2):
KVM: x86: Add gmem hook for invalidating memory
KVM: x86: Add gmem hook for determining max NPT mapping levelPaolo Bonzini (6):
KVM: x86/mmu: pass error code back to MMU when async pf is ready
KVM: x86/mmu: UsePFERR_GUEST_ENC_MASKto indicate fault is private
This doesn't work. The ENC flag gets set on any SNP *capable* CPU, which results in false positives for SEV and SEV-ES guests[*].
I have a medium-sized series to add a KVM-defined synthetic flag, and clean up the related code (it also has my slight variation on the 64-bit error code patch).
I'll post my series exactly as I have it, mostly so that I don't need to redo testing, but also because it's pretty much a drop-in replacement. This series applies cleanly on top, except for the two obvious conflicts.
[*] https://lore.kernel.org/all/[email protected]
Quoted 56 lines
KVM:
guest_memfd: pass error up fromfilemap_grab_folio
filemap: addFGP_CREAT_ONLY
KVM: x86: Add gmem hook for initializing memory
KVM:guest_memfd: add API to undokvm_gmem_get_uninit_pfnSean Christopherson (7):
KVM: x86: Split core of hypercall emulation to helper function
KVM: Allow page-sized MMU caches to be initialized with custom 64-bit
values
KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed
SPTE
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas function
argumentTom Lendacky (1):
KVM: SEV: Use a VMSA physical address variable for populating VMCBarch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 12 + arch/x86/include/asm/vmx.h | 13 + arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 55 ++-- arch/x86/kvm/mmu/mmu_internal.h | 6 +- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 4 +- arch/x86/kvm/mmu/spte.c | 16 +- arch/x86/kvm/mmu/spte.h | 21 +- arch/x86/kvm/mmu/tdp_iter.h | 12 + arch/x86/kvm/mmu/tdp_mmu.c | 74 +++-- arch/x86/kvm/svm/sev.c | 3 +- arch/x86/kvm/svm/svm.c | 9 +- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 168 +++++++++++ arch/x86/kvm/vmx/vmcs.h | 5 + arch/x86/kvm/vmx/vmx.c | 460 +++++++++++------------------ arch/x86/kvm/vmx/vmx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 124 ++++++++ arch/x86/kvm/x86.c | 69 +++-- include/linux/kvm_host.h | 25 ++ include/linux/kvm_types.h | 1 + include/linux/pagemap.h | 2 + mm/filemap.c | 4 + virt/kvm/Kconfig | 8 + virt/kvm/guest_memfd.c | 120 +++++++- virt/kvm/kvm_main.c | 16 +- 29 files changed, 855 insertions(+), 387 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.hSignature · 1 line
2.39.0
Paolo Bonzini
maintainerQuoted 5 lines · Sean Christopherson
Quoted 1 line · Paolo Bonzini
This is a first set of, hopefully non-controversial patches from the
Heh, you jinxed yourself. :-)
Well I
Quoted 33 lines
Quoted 18 lines
SNP and TDX series. They cover mostly changes to generic code and new gmem APIs, and in general have already been reviewed when posted by Isaku and Michael.
One important change is that the gmem hook for initializing memory is designed to return
-EEXISTif the page already exists in the guestmemfd filemap. The idea is that the special case ofKVM_SEV_SNP_LAUNCH_UPDATE, where__kvm_gmem_get_pfn()is used to return an uninitialized page and make it guest-owned, can be be done at most once per page unless the ioctl fails.Of course these patches add a bunch of dead code. This is intentional because it's the only way to trim the large TDX (and to some extent SNP) series to the point that it's possible to discuss them. The next step is probably going to be the private<->shared page logic from the TDX series.
Paolo
Quoted 8 lines
Isaku Yamahata (5):
KVM: x86/mmu: Add Suppress VE bit to EPT
shadow_mmio_mask/shadow_present_mask
KVM: VMX: Introduce test mode related to EPT violation VE
KVM: x86/tdp_mmu: Init role member of structkvm_mmu_pageat
allocation
KVM: x86/tdp_mmu: Sprinkle__must_check
KVM: x86/mmu: Pass around full 64-bit error code for KVM page faultsI have a slight tweak to this patch (drop truncation), and a rewritten changelog.
Quoted 4 lines
Michael Roth (2):
KVM: x86: Add gmem hook for invalidating memory
KVM: x86: Add gmem hook for determining max NPT mapping level
Quoted 6 lines
Quoted 3 lines
Paolo Bonzini (6):
KVM: x86/mmu: pass error code back to MMU when async pf is ready
KVM: x86/mmu: UsePFERR_GUEST_ENC_MASKto indicate fault is privateThis doesn't work. The ENC flag gets set on any SNP *capable* CPU, which results in false positives for SEV and SEV-ES guests[*].
You didn't look at the patch did you? :) It does check for has_private_mem (alternatively I could have dropped the bit in SVM code for SEV and SEV-ES guests).
Quoted 6 lines
I have a medium-sized series to add a KVM-defined synthetic flag, and clean up the related code (it also has my slight variation on the 64-bit error code patch).
I'll post my series exactly as I have it, mostly so that I don't need to redo testing, but also because it's pretty much a drop-in replacement. This series applies cleanly on top, except for the two obvious conflicts.
Ok, I will check it out. This is exactly why I posted these.
Paolo
Quoted 59 lines
[*] https://lore.kernel.org/all/[email protected]
Quoted 56 lines
KVM:
guest_memfd: pass error up fromfilemap_grab_folio
filemap: addFGP_CREAT_ONLY
KVM: x86: Add gmem hook for initializing memory
KVM:guest_memfd: add API to undokvm_gmem_get_uninit_pfnSean Christopherson (7):
KVM: x86: Split core of hypercall emulation to helper function
KVM: Allow page-sized MMU caches to be initialized with custom 64-bit
values
KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed
SPTE
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas function
argumentTom Lendacky (1):
KVM: SEV: Use a VMSA physical address variable for populating VMCBarch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 12 + arch/x86/include/asm/vmx.h | 13 + arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 55 ++-- arch/x86/kvm/mmu/mmu_internal.h | 6 +- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 4 +- arch/x86/kvm/mmu/spte.c | 16 +- arch/x86/kvm/mmu/spte.h | 21 +- arch/x86/kvm/mmu/tdp_iter.h | 12 + arch/x86/kvm/mmu/tdp_mmu.c | 74 +++-- arch/x86/kvm/svm/sev.c | 3 +- arch/x86/kvm/svm/svm.c | 9 +- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 168 +++++++++++ arch/x86/kvm/vmx/vmcs.h | 5 + arch/x86/kvm/vmx/vmx.c | 460 +++++++++++------------------ arch/x86/kvm/vmx/vmx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 124 ++++++++ arch/x86/kvm/x86.c | 69 +++-- include/linux/kvm_host.h | 25 ++ include/linux/kvm_types.h | 1 + include/linux/pagemap.h | 2 + mm/filemap.c | 4 + virt/kvm/Kconfig | 8 + virt/kvm/guest_memfd.c | 120 +++++++- virt/kvm/kvm_main.c | 16 +- 29 files changed, 855 insertions(+), 387 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.h--
2.39.0
Sean Christopherson
maintainerQuoted 5 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Quoted 4 lines
Michael Roth (2):
KVM: x86: Add gmem hook for invalidating memory
KVM: x86: Add gmem hook for determining max NPT mapping level
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Paolo Bonzini (6):
KVM: x86/mmu: pass error code back to MMU when async pf is ready
KVM: x86/mmu: UsePFERR_GUEST_ENC_MASKto indicate fault is privateThis doesn't work. The ENC flag gets set on any SNP *capable* CPU, which results in false positives for SEV and SEV-ES guests[*].
You didn't look at the patch did you? :)
Guilty, sort of. I looked (and tested) the patch from the TDX series, but I didn't look at what you postd. But it's a moot point, because now I did look at what you posted, and it's still broken :-)
Quoted 2 lines
It does check for
has_private_mem(alternatively I could have dropped the bit in SVM code for SEV and SEV-ES guests).
The problem isn't with *KVM* setting the bit, it's with *hardware* setting the bit for SEV and SEV-ES guests. That results in this:
.is_private = vcpu->kvm->arch.has_private_mem && (err & PFERR_GUEST_ENC_MASK),
marking the fault as private. Which, in a vacuum, isn't technically wrong, since from hardware's perspective the vCPU access was "private". But from KVM's perspective, SEV and SEV-ES guests don't have private memory, they have memory that can be *encrypted*, and marking the access as "private" results in violations of KVM's rules for private memory. Specifically, it results in KVM triggering emulated MMIO for faults that are marked private, which we want to disallow for SNP and TDX.
And because the flag only gets set on SNP capable hardware (in my limited testing of a whole two systems), running the same VM on different hardware would result in faults being marked private on one system, but not the other. Which means that KVM can't rely on the flag being set for SEV or SEV-ES guests, i.e. we can't retroactively enforce anything (not to mention that that might break existing VMs).
Paolo Bonzini
maintainerQuoted 12 lines · Sean Christopherson
Quoted 4 lines
Quoted 2 lines
This doesn't work. The ENC flag gets set on any SNP *capable* CPU, which results in false positives for SEV and SEV-ES guests[*].
You didn't look at the patch did you? :)
Guilty, sort of. I looked (and tested) the patch from the TDX series, but I didn't look at what you postd. But it's a moot point, because now I did look at what you posted, and it's still broken :-)
Quoted 2 lines
It does check for
has_private_mem(alternatively I could have dropped the bit in SVM code for SEV and SEV-ES guests).
Quoted 8 lines
The problem isn't with *KVM* setting the bit, it's with *hardware* setting the bit for SEV and SEV-ES guests. That results in this:
.is_private =
vcpu->kvm->arch.has_private_mem&& (err &PFERR_GUEST_ENC_MASK),marking the fault as private. Which, in a vacuum, isn't technically wrong, since from hardware's perspective the vCPU access was "private". But from KVM's perspective, SEV and SEV-ES guests don't have private memory
vcpu->kvm->arch.has_private_mem is the flag from the SEV VM types series. It's false on SEV and SEV-ES VMs, therefore fault->is_private is going to be false as well. Is it ENOCOFFEE for you or ENODINNER for me? :)
Paolo
Quoted 6 lines
And because the flag only gets set on SNP capable hardware (in my limited testing of a whole two systems), running the same VM on different hardware would result in faults being marked private on one system, but not the other. Which means that KVM can't rely on the flag being set for SEV or SEV-ES guests, i.e. we can't retroactively enforce anything (not to mention that that might break existing VMs).
Sean Christopherson
maintainerQuoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Quoted 4 lines
Quoted 2 lines
This doesn't work. The ENC flag gets set on any SNP *capable* CPU, which results in false positives for SEV and SEV-ES guests[*].
You didn't look at the patch did you? :)
Guilty, sort of. I looked (and tested) the patch from the TDX series, but I didn't look at what you postd. But it's a moot point, because now I did look at what you posted, and it's still broken :-)
Quoted 2 lines
It does check for
has_private_mem(alternatively I could have dropped the bit in SVM code for SEV and SEV-ES guests).The problem isn't with *KVM* setting the bit, it's with *hardware* setting the bit for SEV and SEV-ES guests. That results in this:
.is_private =
vcpu->kvm->arch.has_private_mem&& (err &PFERR_GUEST_ENC_MASK),
Quoted 8 lines
Quoted 3 lines
marking the fault as private. Which, in a vacuum, isn't technically wrong, since from hardware's perspective the vCPU access was "private". But from KVM's perspective, SEV and SEV-ES guests don't have private memory
vcpu->kvm->arch.has_private_memis the flag from the SEV VM types series. It's false on SEV and SEV-ES VMs, thereforefault->is_privateis going to be false as well. Is it ENOCOFFEE for you or ENODINNER for me? :)
*sigh*, ENOCOFFEE.
Sean Christopherson
maintainerI would strongly prefer we taret 6.10, not 6.9. The TDX and SNP folks don't need any of this code to be in Linus' tree, they just need it in _a_ KVM tree so that they can develop on top.
And I will have limited availability the rest of this week (potentially very limited), and I obviously have strong opinions about some of this code. But even if I had cycles to review this properly, I just don't see a reason to rush it in.
For the guest_memfd changes in particular, they're impossible to review in this series. Rather than prematurely shove them into mainline, we should create a volatile topic branch and use that to enable TDX/SNP development. That way we can fixup patches if things need to change.
Quoted 16 lines · Paolo Bonzini
This is a first set of, hopefully non-controversial patches from the SNP and TDX series. They cover mostly changes to generic code and new gmem APIs, and in general have already been reviewed when posted by Isaku and Michael.
One important change is that the gmem hook for initializing memory is designed to return
-EEXISTif the page already exists in the guestmemfd filemap. The idea is that the special case ofKVM_SEV_SNP_LAUNCH_UPDATE, where__kvm_gmem_get_pfn()is used to return an uninitialized page and make it guest-owned, can be be done at most once per page unless the ioctl fails.Of course these patches add a bunch of dead code. This is intentional because it's the only way to trim the large TDX (and to some extent SNP) series to the point that it's possible to discuss them. The next step is probably going to be the private<->shared page logic from the TDX series.