KVM TDX basic feature support
Subsystems (8)
Re: [PATCH v19 060/130] KVM: x86/tdp_mmu: Apply mmu notifier callback to only shared GPA
In reply to Binbin WuQuoted 11 lines · Binbin Wu
Quoted 6 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
The private GPAs that typically guest memfd backs aren't subject to MMU notifier because it isn't mapped into virtual address of user process.
kvm_tdp_mmu_handle_gfn()handles the callback of the MMU notifier,clear_flush_young(),clear_young(), test_young()() andchange_pte(). Make^ an extra "()"
Will fix it. Thanks.
Quoted 8 lines
Quoted 4 lines
kvm_tdp_mmu_handle_gfn()aware of private mapping and skip private mapping.Even with
AS_UNMOVABLEset, those mmu notifier are called. For example, ksmd triggerschange_pte().The description about the "
AS_UNMOVABLE", you are refering to shared memory, right?
Then, it seems not related to the change of this patch.
Ok, will remove this sentence.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 61/130 KVM: x86/tdp_mmu: Sprinkle __must_check
In reply to [email protected]From: 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. Sprinkle __must_check to guarantee it.
--
v19:
- Added Reviewed-by: Binbin
--- arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +5 -5
@@ -507,9 +507,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);
@@ -539,8 +539,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.25.1
PATCH v19 62/130 KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Allocate protected page table for private page table, and add hooks to operate on protected page table. This patch adds allocation/free of protected page tables and hooks. When calling hooks to update SPTE entry, freeze the entry, call hooks and unfreeze the entry to allow concurrent updates on page tables. Which is the advantage of TDP MMU. As kvm_gfn_shared_mask() returns false always, those hooks aren't called yet with this patch.
When the faulting GPA is private, the KVM fault is called private. When resolving private KVM fault, allocate protected page table and call hooks to operate on protected page table. On the change of the private PTE entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to protected page table. The following depicts the relationship.
private KVM page fault |
| |
V |
private GPA | CPU protected EPTP
| | |
V | V
private PT root | protected PT root
| | |
V | V
private PT --hook to propagate-->protected PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For protected page table, hooks are called to update protected page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K protected SPTE => error vcpu 1: TDX SEAMCALL to update 2M protected SPTE
To avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update protected SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
---
v19:
- drop CONFIG_KVM_MMU_PRIVATE
v18:
- Rename freezed => frozen
v14 -> v15:
- Refined is_private condition check in kvm_tdp_mmu_map(). Add kvm_gfn_shared_mask() check.
- catch up for struct kvm_range change
--- arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 11 ++ arch/x86/kvm/mmu/mmu.c | 17 +- arch/x86/kvm/mmu/mmu_internal.h | 13 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 308 +++++++++++++++++++++++++---- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- virt/kvm/kvm_main.c | 1 + 8 files changed, 320 insertions(+), 39 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +5 -0
@@ -101,6 +101,11 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(link_private_spt) +KVM_X86_OP_OPTIONAL(free_private_spt) +KVM_X86_OP_OPTIONAL(set_private_spte) +KVM_X86_OP_OPTIONAL(remove_private_spte) +KVM_X86_OP_OPTIONAL(zap_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +11 -0
@@ -468,6 +468,7 @@ struct kvm_mmu {int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;
@@ -1740,6 +1741,16 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*free_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*set_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*remove_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*zap_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +15 -2
@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (tdp_mmu_enabled) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level);
@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (kvm_mmu_honors_guest_mtrrs (vcpu->kvm)) {for ( ; fault->max_level >PG_LEVEL_4K ; --fault->max_level) {int page_num =KVM_PAGES_PER_HPAGE (fault->max_level); -gfn_t base =gfn_round_for_level (fault->gfn, +gfn_t base =gfn_round_for_level (gpa_to_gfn (fault->addr), fault->max_level);if (kvm_mtrr_check_gfn_range_consistency (vcpu, base, page_num))
@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };WARN_ON_ONCE (!vcpu->arch.mmu->root_role.direct); + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); r =mmu_topup_memory_caches (vcpu, false);
@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (tdp_mmu_enabled) { +write_lock (&vcpu->kvm->mmu_lock); +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL ); +write_unlock (&vcpu->kvm->mmu_lock); + }free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);
arch/x86/kvm/mmu/mmu_internal.h +12 -1
@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#ifdef CONFIG_KVM_PROVE_MMU #define KVM_MMU_WARN_ON (x) WARN_ON_ONCE(x)#else
@@ -205,6 +207,15 @@ static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp)free_page ((unsigned long )sp->private_spt); } +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_to_private (kvm, gfn); +else +return kvm_gfn_to_shared (kvm, gfn); +} +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/*
@@ -363,7 +374,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int r;if (vcpu->arch.mmu->root_role.direct) { - fault.gfn = fault.addr >>PAGE_SHIFT ; + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); }
arch/x86/kvm/mmu/tdp_iter.h +1 -1
@@ -91,7 +91,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
arch/x86/kvm/mmu/tdp_mmu.c +274 -34
470 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +1 -1
@@ -10,7 +10,7 @@void kvm_mmu_init_tdp_mmu (struct kvm *kvm);void kvm_mmu_uninit_tdp_mmu (struct kvm *kvm); -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {
virt/kvm/kvm_main.c +1 -0
@@ -201,6 +201,7 @@ struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)return NULL ; } +EXPORT_SYMBOL_GPL (kvm_pfn_to_refcounted_page);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 1 line
2.25.1
Edgecombe, Rick P
maintainerRe: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
arch/x86/include/asm/kvm_host.h struct kvm_mmu { · line 471
Quoted 9 lines · On Mon, 2024-02-26 at 00:26 -0800, [email protected] wrote:
arch/x86/include/asm/kvm_host.h +1
-0@@ -468,6 +468,7 @@ struct kvm_mmu { int (*sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int i); struct kvm_mmu_root_info root; + hpa_t private_root_hpa;
Per the conversation about consistent naming between private, shared and mirror: I wonder if this should be named these with mirror instead of private. Like:
hpa_t mirror_root_hpa;
Since the actual private root is not tracked by KVM.
Quoted 28 lines
arch/x86/include/asm/kvm_host.h +0
-0union kvm_cpu_role cpu_role; union kvm_mmu_page_role root_role;arch/x86/include/asm/kvm_host.h +0
-0@@ -1740,6 +1741,16 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);
+ int (*link_private_spt)(struct kvm *kvm,gfn_tgfn, enumpg_levellevel, + void*private_spt);
+ int (*free_private_spt)(struct kvm *kvm,gfn_tgfn, enumpg_levellevel, + void*private_spt);
+ int (*set_private_spte)(struct kvm *kvm,gfn_tgfn, enumpg_levellevel, +kvm_pfn_tpfn);
+ int (*remove_private_spte)(struct kvm *kvm,gfn_tgfn, enumpg_levellevel, +kvm_pfn_tpfn);
+ int (*zap_private_spte)(struct kvm *kvm,gfn_tgfn, enumpg_levellevel); +
bool (*has_wbinvd_exit)(void);
u64 (*get_l2_tsc_offset)(structkvm_vcpu*vcpu);arch/x86/kvm/mmu/mmu.c +0
-0@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;
if (tdp_mmu_enabled) {
Quoted 8 lines
- root =
kvm_tdp_mmu_get_vcpu_root_hpa(vcpu);
+ if (kvm_gfn_shared_mask(vcpu->kvm)&&
+ !VALID_PAGE(mmu->private_root_hpa)) {
+ root =kvm_tdp_mmu_get_vcpu_root_hpa(vcpu, true); + mmu->private_root_hpa = root;
+ }
+ root =kvm_tdp_mmu_get_vcpu_root_hpa(vcpu, false);mmu->root.hpa= root;
This has changed now, due to rebase on,
https://lore.kernel.org/lkml/[email protected]/
...to this:
- if (tdp_mmu_enabled)
- return kvm_tdp_mmu_alloc_root(vcpu);
+ if (tdp_mmu_enabled) {
+ if (kvm_gfn_shared_mask(vcpu->kvm) &&
+ !VALID_PAGE(mmu->private_root_hpa)) {
+ r = kvm_tdp_mmu_alloc_root(vcpu, true);
+ if (r)
+ return r;
+ }
+ return kvm_tdp_mmu_alloc_root(vcpu, false);
+ }
I don't see why the !VALID_PAGE(mmu->private_root_hpa) check is needed. kvm_tdp_mmu_get_vcpu_root_hpa() already has logic to prevent allocating multiple roots with the same role.
Also, kvm_tdp_mmu_alloc_root() never returns non-zero, even though mmu_alloc_direct_roots() does. Probably today when there is one caller it makes mmu_alloc_direct_roots() cleaner to just have it return the always zero value from kvm_tdp_mmu_alloc_root(). Now that there are two calls, I think we should refactor kvm_tdp_mmu_alloc_root() to return void, and have kvm_tdp_mmu_alloc_root() return 0 manually in this case.
Or maybe instead change it back to returning an hpa_t and then kvm_tdp_mmu_alloc_root() can lose the "if (private)" logic at the end too.
Quoted 28 lines
} else if (
shadow_root_level>=PT64_ROOT_4LEVEL) { root =mmu_alloc_root(vcpu, 0, 0, shadow_root_level);arch/x86/kvm/mmu/mmu.c +1
-1@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) if (kvm_mmu_honors_guest_mtrrs(vcpu->kvm)) { for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) { int page_num = KVM_PAGES_PER_HPAGE(fault->max_level); - gfn_t base = gfn_round_for_level(fault->gfn, + gfn_t base = gfn_round_for_level(gpa_to_gfn(fault->addr), fault->max_level);
if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))arch/x86/kvm/mmu/mmu.c +0
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };
WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct);
+ fault.gfn =gpa_to_gfn(fault.addr)& ~kvm_gfn_shared_mask(vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
r =mmu_topup_memory_caches(vcpu, false);arch/x86/kvm/mmu/mmu.c +0
-0@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
mmu->root.hpa =INVALID_PAGE;
mmu->root.pgd = 0;
+ mmu->private_root_hpa =INVALID_PAGE;
for (i = 0; i <KVM_MMU_NUM_PREV_ROOTS; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID;
arch/x86/kvm/mmu/mmu.c int kvm_mmu_vendor_module_init(void) · line 7225
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +5
-0@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void) void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { kvm_mmu_unload(vcpu); + if (tdp_mmu_enabled) { + write_lock(&vcpu->kvm->mmu_lock); + mmu_free_root_page(vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, + NULL); + write_unlock(&vcpu->kvm->mmu_lock);
What is the reason for the special treatment of private_root_hpa here? The rest of the roots are freed in kvm_mmu_unload(). I think it is because we don't want the mirror to get freed during kvm_mmu_reset_context()?
Oof. For the sake of trying to justify the code, I'm trying to keep track of the pros and cons of treating the mirror/private root like a normal one with just a different role bit.
The whole “list of roots” thing seems to date from the shadow paging, where there is is critical to keep multiple cached shared roots of different CPU modes of the same shadowed page tables. Today with non-nested TDP, AFAICT, the only different root is for SMM. I guess since the machinery for managing multiple roots in a list already exists it makes sense to use it for both.
For TDX there are also only two, but the difference is, things need to be done in special ways for the two roots. You end up with a bunch of loops (for_each_*tdp_mmu_root(), etc) that essentially process a list of two different roots, but with inner logic tortured to work for the peculiarities of both private and shared. An easier to read alternative could be to open code both cases.
I guess the major benefit is to keep one set of logic for shadow paging, normal TDP and TDX, but it makes the logic a bit difficult to follow for TDX compared to looking at it from the normal guest perspective. So I wonder if making special versions of the TDX root traversing operations might make the code a little easier to follow. I’m not advocating for it at this point, just still working on an opinion. Is there any history around this design point?
Quoted 21 lines
arch/x86/kvm/mmu/mmu.c +1
-0+ } free_mmu_pages(&vcpu->arch.root_mmu); free_mmu_pages(&vcpu->arch.guest_mmu); mmu_free_memory_caches(vcpu);arch/x86/kvm/mmu/mmu_internal.h +0
-0@@ -6,6 +6,8 @@ #include <linux/kvm_host.h> #include <asm/kvm_host.h>arch/x86/kvm/mmu/mmu_internal.h +2
-0+#include "mmu.h" +#ifdef
CONFIG_KVM_PROVE_MMU
#defineKVM_MMU_WARN_ON(x)WARN_ON_ONCE(x)
#elsearch/x86/kvm/mmu/mmu_internal.h +0
-0@@ -205,6 +207,15 @@ static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp) free_page((unsigned long)sp->private_spt); }
arch/x86/kvm/mmu/mmu_internal.h static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp) · line 218
Quoted 9 lines
arch/x86/kvm/mmu/mmu_internal.h +9
-0+static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, + gfn_t gfn) +{ + if (is_private_sp(root)) + return kvm_gfn_to_private(kvm, gfn); + else + return kvm_gfn_to_shared(kvm, gfn); +} +
It could be branchless, but might not be worth the readability:
static inline gfn_t kvm_gfn_for_root(struct kvm *kvm, struct kvm_mmu_page *root,
gfn_t gfn)
{
gfn_t gfn_for_root = kvm_gfn_to_private(kvm, gfn);
gfn_for_root |= -(gfn_t)!is_private_sp(root) & kvm_gfn_shared_mask(kvm);
return gfn_for_root;
}Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 15 lines · Rick P
arch/x86/include/asm/kvm_host.h struct kvm_mmu { · line 471
Quoted 9 lines · On Mon, 2024-02-26 at 00:26 -0800, [email protected] wrote:
arch/x86/include/asm/kvm_host.h +1
-0@@ -468,6 +468,7 @@ struct kvm_mmu { int (*sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int i); struct kvm_mmu_root_info root; + hpa_t private_root_hpa;Per the conversation about consistent naming between private, shared and mirror: I wonder if this should be named these with mirror instead of private. Like:
hpa_tmirror_root_hpa;
Quoted 1 line
Since the actual private root is not tracked by KVM.
It's mirrored only without direct associated Secure EPT page. The association is implicit, a part of TDCS pages.
Quoted 33 lines
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +0
-0@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;
if (tdp_mmu_enabled) {Quoted 8 lines
- root =
kvm_tdp_mmu_get_vcpu_root_hpa(vcpu);
+ if (kvm_gfn_shared_mask(vcpu->kvm)&&
+ !VALID_PAGE(mmu->private_root_hpa)) {
+ root =kvm_tdp_mmu_get_vcpu_root_hpa(vcpu, true); + mmu->private_root_hpa = root;
+ }
+ root =kvm_tdp_mmu_get_vcpu_root_hpa(vcpu, false);mmu->root.hpa= root;This has changed now, due to rebase on,
https://lore.kernel.org/lkml/[email protected]/...to this:
(quoted diff) +9
-2- if (tdp_mmu_enabled) - return kvm_tdp_mmu_alloc_root(vcpu); + if (tdp_mmu_enabled) { + if (kvm_gfn_shared_mask(vcpu->kvm) && + !VALID_PAGE(mmu->private_root_hpa)) { + r = kvm_tdp_mmu_alloc_root(vcpu, true); + if (r) + return r; + } + return kvm_tdp_mmu_alloc_root(vcpu, false); + }
Quoted 3 lines
I don't see why the !VALID_PAGE(mmu->private_root_hpa) check is needed.
kvm_tdp_mmu_get_vcpu_root_hpa()already has logic to prevent allocating multiple roots with the same role.
Historically we needed it. We don't need it now. We can drop it.
Quoted 8 lines
Also,
kvm_tdp_mmu_alloc_root()never returns non-zero, even thoughmmu_alloc_direct_roots()does. Probably today when there is one caller it makesmmu_alloc_direct_roots()cleaner to just have it return the always zero value fromkvm_tdp_mmu_alloc_root(). Now that there are two calls, I think we should refactorkvm_tdp_mmu_alloc_root()to return void, and havekvm_tdp_mmu_alloc_root()return 0 manually in this case.Or maybe instead change it back to returning an
hpa_tand thenkvm_tdp_mmu_alloc_root()can lose the "if (private)" logic at the end too.
Probably we can make void kvm_tdp_mmu_alloc_root() instead of returning always zero as clean up.
Quoted 32 lines
Quoted 32 lines
} else if (
shadow_root_level>=PT64_ROOT_4LEVEL) { root =mmu_alloc_root(vcpu, 0, 0, shadow_root_level);arch/x86/kvm/mmu/mmu.c +1
-1@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) if (kvm_mmu_honors_guest_mtrrs(vcpu->kvm)) { for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) { int page_num = KVM_PAGES_PER_HPAGE(fault->max_level); - gfn_t base = gfn_round_for_level(fault->gfn, + gfn_t base = gfn_round_for_level(gpa_to_gfn(fault->addr), fault->max_level);
if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))arch/x86/kvm/mmu/mmu.c +0
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };
WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct);
+ fault.gfn =gpa_to_gfn(fault.addr)& ~kvm_gfn_shared_mask(vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
r =mmu_topup_memory_caches(vcpu, false);arch/x86/kvm/mmu/mmu.c +0
-0@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
mmu->root.hpa =INVALID_PAGE;
mmu->root.pgd = 0;
+ mmu->private_root_hpa =INVALID_PAGE;
for (i = 0; i <KVM_MMU_NUM_PREV_ROOTS; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID;arch/x86/kvm/mmu/mmu.c +1
-0@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void) void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { kvm_mmu_unload(vcpu); + if (tdp_mmu_enabled) {
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c int kvm_mmu_vendor_module_init(void) · line 7225
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +4
-0+ write_lock(&vcpu->kvm->mmu_lock); + mmu_free_root_page(vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, + NULL); + write_unlock(&vcpu->kvm->mmu_lock);What is the reason for the special treatment of
private_root_hpahere? The rest of the roots are freed inkvm_mmu_unload(). I think it is because we don't want the mirror to get freed duringkvm_mmu_reset_context()?
It reflects that we don't free Secure-EPT pages during runtime, and free them when destroying the guest.
Quoted 14 lines
Oof. For the sake of trying to justify the code, I'm trying to keep track of the pros and cons of treating the mirror/private root like a normal one with just a different role bit.
The whole “list of roots” thing seems to date from the shadow paging, where there is is critical to keep multiple cached shared roots of different CPU modes of the same shadowed page tables. Today with non-nested TDP, AFAICT, the only different root is for SMM. I guess since the machinery for managing multiple roots in a list already exists it makes sense to use it for both.
For TDX there are also only two, but the difference is, things need to be done in special ways for the two roots. You end up with a bunch of loops (for_each_*tdp_mmu_root(), etc) that essentially process a list of two different roots, but with inner logic tortured to work for the peculiarities of both private and shared. An easier to read alternative could be to open code both cases.
Quoted 5 lines
I guess the major benefit is to keep one set of logic for shadow paging, normal TDP and TDX, but it makes the logic a bit difficult to follow for TDX compared to looking at it from the normal guest perspective. So I wonder if making special versions of the TDX root traversing operations might make the code a little easier to follow. I’m not advocating for it at this point, just still working on an opinion. Is there any history around this design point?
The original desire to keep the modification contained, and not introduce a function for population and zap. With the open coding, do you want something like the followings? We can try it and compare the outcome.
For zapping
if (private) {
__for_each_tdp_mmu_root_yield_safe_private()
private case
} else {
__for_each_tdp_mmu_root_yield_safe()
shared case}
For fault,kvm_tdp_mmu_map()
if (private) {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)
private case
} else {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)
shared case
}
Signature · 1 line
Isaku Yamahata <[email protected]>
Edgecombe, Rick P
maintainerRe: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 9 lines · Isaku Yamahata
Also,
kvm_tdp_mmu_alloc_root()never returns non-zero, even thoughmmu_alloc_direct_roots()does.Quoted 8 lines
Probably today when there is one caller it makes
mmu_alloc_direct_roots()cleaner to just have it
return the always zero value fromkvm_tdp_mmu_alloc_root(). Now that there are two calls, I think we
should refactorkvm_tdp_mmu_alloc_root()to return void, and havekvm_tdp_mmu_alloc_root()return 0
manually in this case.
Quoted 6 lines
Quoted 3 lines
Or maybe instead change it back to returning an
hpa_tand thenkvm_tdp_mmu_alloc_root()can lose the
"if (private)" logic at the end too.Probably we can make void
kvm_tdp_mmu_alloc_root()instead of returning always zero as clean up.
Why is it better than returning an hpa_t once we are calling it twice for mirror and shared roots.
Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
} else if (
shadow_root_level>=PT64_ROOT_4LEVEL) { root =mmu_alloc_root(vcpu, 0, 0, shadow_root_level);arch/x86/kvm/mmu/mmu.c +1
-1@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) if (kvm_mmu_honors_guest_mtrrs(vcpu->kvm)) { for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) { int page_num = KVM_PAGES_PER_HPAGE(fault->max_level); - gfn_t base = gfn_round_for_level(fault->gfn, + gfn_t base = gfn_round_for_level(gpa_to_gfn(fault->addr), fault->max_level);
if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))(quoted diff) +0
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };
WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct);
+ fault.gfn =gpa_to_gfn(fault.addr)& ~kvm_gfn_shared_mask(vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
r =mmu_topup_memory_caches(vcpu, false);(quoted diff) +0
-0@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
mmu->root.hpa =INVALID_PAGE;
mmu->root.pgd = 0;
+ mmu->private_root_hpa =INVALID_PAGE;
for (i = 0; i <KVM_MMU_NUM_PREV_ROOTS; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID;arch/x86/kvm/mmu/mmu.c +1
-0@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void) void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { kvm_mmu_unload(vcpu); + if (tdp_mmu_enabled) {Quoted 8 lines
arch/x86/kvm/mmu/mmu.c int kvm_mmu_vendor_module_init(void) · line 7225
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +4
-0+ write_lock(&vcpu->kvm->mmu_lock); + mmu_free_root_page(vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, + NULL); + write_unlock(&vcpu->kvm->mmu_lock);What is the reason for the special treatment of
private_root_hpahere? The rest of the roots are freed inkvm_mmu_unload(). I think it is because we don't want the mirror to get freed duringkvm_mmu_reset_context()?
Quoted 2 lines
It reflects that we don't free Secure-EPT pages during runtime, and free them when destroying the guest.
Right. If would be great if we could do something like warn on freeing role.private = 1 sp's during runtime. It could cover several cases that get worried about in other patches.
While looking at how we could do this, I noticed that kvm_arch_vcpu_create() calls kvm_mmu_destroy() in an error path. So this could end up zapping/freeing a private root. It should be bad userspace behavior too I guess. But the number of edge cases makes me think the case of zapping private sp while a guest is running is something that deserves a VM_BUG_ON().
Quoted 43 lines
Quoted 20 lines
Oof. For the sake of trying to justify the code, I'm trying to keep track of the pros and cons of
treating the mirror/private root like a normal one with just a different role bit.The whole “list of roots” thing seems to date from the shadow paging, where there is is critical to
keep multiple cached shared roots of different CPU modes of the same shadowed page tables. Today with non-nested TDP, AFAICT, the only different root is for SMM. I guess since the machinery for managing multiple roots in a list already exists it makes sense to use it for both.For TDX there are also only two, but the difference is, things need to be done in special ways for
the two roots. You end up with a bunch of loops (for_each_*tdp_mmu_root(), etc) that essentially process a list of two different roots, but with inner logic tortured to work for the peculiarities
of both private and shared. An easier to read alternative could be to open code both cases.Quoted 9 lines
I guess the major benefit is to keep one set of logic for shadow paging, normal TDP and TDX, but it
makes the logic a bit difficult to follow for TDX compared to looking at it from the normal guest
perspective. So I wonder if making special versions of the TDX root traversing operations might make
the code a little easier to follow. I’m not advocating for it at this point, just still working on
an opinion. Is there any history around this design point?The original desire to keep the modification contained, and not introduce a function for population and zap. With the open coding, do you want something like the followings? We can try it and compare the outcome.
For zapping
if (private) {
__for_each_tdp_mmu_root_yield_safe_private()
private case
} else {
__for_each_tdp_mmu_root_yield_safe()
shared case
}
Quoted 9 lines
For fault,
kvm_tdp_mmu_map()
if (private) {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)private case
} else {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)shared case
}
I was wondering about something limited to the operations that iterate over the roots. So not keeping private_root_hpa in the list of roots where it has to be carefully protected from getting zapped or get its gfn adjusted, and instead open coding the private case in the higher level zapping operations. For normal VM's the private case would be a NOP.
Since kvm_tdp_mmu_map() already grabs private_root_hpa manually, it wouldn't change in this idea. I don't know how much better it would be though. I think you are right we would have to create them and compare.
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 10 lines · Rick P
Quoted 9 lines · Isaku Yamahata
Also,
kvm_tdp_mmu_alloc_root()never returns non-zero, even thoughmmu_alloc_direct_roots()does.Quoted 8 lines
Probably today when there is one caller it makes
mmu_alloc_direct_roots()cleaner to just have it
return the always zero value fromkvm_tdp_mmu_alloc_root(). Now that there are two calls, I think we
should refactorkvm_tdp_mmu_alloc_root()to return void, and havekvm_tdp_mmu_alloc_root()return 0
manually in this case.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Or maybe instead change it back to returning an
hpa_tand thenkvm_tdp_mmu_alloc_root()can lose the
"if (private)" logic at the end too.Probably we can make void
kvm_tdp_mmu_alloc_root()instead of returning always zero as clean up.Why is it better than returning an
hpa_tonce we are calling it twice for mirror and shared roots.
You mean split out "if (private)" from the core part? Makes sense.
Quoted 43 lines
Quoted 43 lines
Quoted 42 lines
Quoted 30 lines
} else if (
shadow_root_level>=PT64_ROOT_4LEVEL) { root =mmu_alloc_root(vcpu, 0, 0, shadow_root_level);arch/x86/kvm/mmu/mmu.c +1
-1@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) if (kvm_mmu_honors_guest_mtrrs(vcpu->kvm)) { for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) { int page_num = KVM_PAGES_PER_HPAGE(fault->max_level); - gfn_t base = gfn_round_for_level(fault->gfn, + gfn_t base = gfn_round_for_level(gpa_to_gfn(fault->addr), fault->max_level);
if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))(quoted diff) +0
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };
WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct);
+ fault.gfn =gpa_to_gfn(fault.addr)& ~kvm_gfn_shared_mask(vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
r =mmu_topup_memory_caches(vcpu, false);(quoted diff) +0
-0@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
mmu->root.hpa =INVALID_PAGE;
mmu->root.pgd = 0;
+ mmu->private_root_hpa =INVALID_PAGE;
for (i = 0; i <KVM_MMU_NUM_PREV_ROOTS; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID;arch/x86/kvm/mmu/mmu.c int kvm_mmu_vendor_module_init(void) · line 7225
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +5
-0@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void) void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { kvm_mmu_unload(vcpu); + if (tdp_mmu_enabled) { + write_lock(&vcpu->kvm->mmu_lock); + mmu_free_root_page(vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, + NULL); + write_unlock(&vcpu->kvm->mmu_lock);What is the reason for the special treatment of
private_root_hpahere? The rest of the roots are freed inkvm_mmu_unload(). I think it is because we don't want the mirror to get freed duringkvm_mmu_reset_context()?
Quoted 5 lines
Quoted 2 lines
It reflects that we don't free Secure-EPT pages during runtime, and free them when destroying the guest.
Right. If would be great if we could do something like warn on freeing role.private = 1 sp's during runtime. It could cover several cases that get worried about in other patches.
Ok, let me move it to kvm_mmu_unload() and try to sprinkle warn-on.
Quoted 4 lines
While looking at how we could do this, I noticed that
kvm_arch_vcpu_create()callskvm_mmu_destroy()in an error path. So this could end up zapping/freeing a private root. It should be bad userspace behavior too I guess. But the number of edge cases makes me think the case of zapping private sp while a guest is running is something that deserves aVM_BUG_ON().
Let me clean the code. I think we can clean them up.
Quoted 50 lines
Quoted 40 lines
Quoted 17 lines
Oof. For the sake of trying to justify the code, I'm trying to keep track of the pros and cons of
treating the mirror/private root like a normal one with just a different role bit.The whole “list of roots” thing seems to date from the shadow paging, where there is is critical to
keep multiple cached shared roots of different CPU modes of the same shadowed page tables. Today with non-nested TDP, AFAICT, the only different root is for SMM. I guess since the machinery for managing multiple roots in a list already exists it makes sense to use it for both.For TDX there are also only two, but the difference is, things need to be done in special ways for
the two roots. You end up with a bunch of loops (for_each_*tdp_mmu_root(), etc) that essentially process a list of two different roots, but with inner logic tortured to work for the peculiarities
of both private and shared. An easier to read alternative could be to open code both cases.Quoted 9 lines
I guess the major benefit is to keep one set of logic for shadow paging, normal TDP and TDX, but it
makes the logic a bit difficult to follow for TDX compared to looking at it from the normal guest
perspective. So I wonder if making special versions of the TDX root traversing operations might make
the code a little easier to follow. I’m not advocating for it at this point, just still working on
an opinion. Is there any history around this design point?The original desire to keep the modification contained, and not introduce a function for population and zap. With the open coding, do you want something like the followings? We can try it and compare the outcome.
For zapping
if (private) {
__for_each_tdp_mmu_root_yield_safe_private()
private case
} else {
__for_each_tdp_mmu_root_yield_safe()
shared case
}Quoted 9 lines
For fault,
kvm_tdp_mmu_map()
if (private) {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)private case
} else {
tdp_mmu_for_each_pte_private(iter, mmu, raw_gfn, raw_gfn + 1)shared case
}
Quoted 8 lines
I was wondering about something limited to the operations that iterate over the roots. So not keeping
private_root_hpain the list of roots where it has to be carefully protected from getting zapped or get its gfn adjusted, and instead open coding the private case in the higher level zapping operations. For normal VM's the private case would be a NOP.Since
kvm_tdp_mmu_map()already grabsprivate_root_hpamanually, it wouldn't change in this idea. I don't know how much better it would be though. I think you are right we would have to create them and compare.
Given the large page support gets complicated, it would be worthwhile to try, I think.
Signature · 1 line
Isaku Yamahata <[email protected]>
Edgecombe, Rick P
maintainerRe: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 8 lines · Isaku Yamahata
Quoted 8 lines
I was wondering about something limited to the operations that iterate over the roots. So not
keepingprivate_root_hpain the list of roots where it has to be carefully protected from getting
zapped or get its gfn adjusted, and instead open coding the private case in the higher level zapping
operations. For normal VM's the private case would be a NOP.
Quoted 8 lines
Quoted 5 lines
Since
kvm_tdp_mmu_map()already grabsprivate_root_hpamanually, it wouldn't change in this idea. I
don't know how much better it would be though. I think you are right we would have to create them
and compare.Given the large page support gets complicated, it would be worthwhile to try, I think.
Circling back here, let's keep things as is for the MMU breakout series. We didn't get any maintainer comments on the proposed refactor, and we might get some on the smaller MMU breakout series. Then we can just have the smaller less controversial changes already incorporated for the discussion. We can mention the idea in the coverletter.
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]arch/x86/kvm/mmu/mmu.c static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) · line 3723
Quoted 11 lines
arch/x86/kvm/mmu/mmu.c +4
-1@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (tdp_mmu_enabled) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root;
just
mmu->private_root_hpa = kvm_tdp_mmu_get_vcpu_root_hpa(vcpu, true);
arch/x86/kvm/mmu/mmu.c static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) · line 3726
Quoted 3 lines
arch/x86/kvm/mmu/mmu.c +2
-0+ } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root;
ditto
Quoted 9 lines
arch/x86/kvm/mmu/mmu.c +1
-1}else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level);@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (kvm_mmu_honors_guest_mtrrs (vcpu->kvm)) {for ( ; fault->max_level >PG_LEVEL_4K ; --fault->max_level) {int page_num =KVM_PAGES_PER_HPAGE (fault->max_level); -gfn_t base =gfn_round_for_level (fault->gfn, +gfn_t base =gfn_round_for_level (gpa_to_gfn (fault->addr), fault->max_level);
...
arch/x86/kvm/mmu/mmu.c int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, · line 4670
Quoted 7 lines
if (
kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))arch/x86/kvm/mmu/mmu.c +1
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };WARN_ON_ONCE (!vcpu->arch.mmu->root_role.direct); + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm);
Could you clarify when shared bits need to be masked out or kept? shared bits are masked out here but kept in the hunk right above and ..
arch/x86/kvm/mmu/mmu.c struct tdp_iter { · line 95
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +2
-1+++ b/arch/x86/kvm/mmu/tdp_iter.h@@ -91,7 +91,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;
.. in @gfn of tdp_iter.
arch/x86/kvm/mmu/mmu.c struct tdp_iter {
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +1
-1/* The level of the root page given to the iterator */ int root_level; -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root(struct kvm_vcpu *vcpu,
Maybe fold it into its sole caller.
Quoted 35 lines
arch/x86/kvm/mmu/mmu.c +1
-0+ bool private){ union kvm_mmu_page_role role = vcpu->arch.mmu->root_role; struct kvm *kvm = vcpu->kvm;arch/x86/kvm/mmu/mmu.c +2
-0@@ -221,6 +225,8 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)* Check for an existing root before allocating a new one. Note, the * role check prevents consuming an invalid root. */ +if (private) +kvm_mmu_page_role_set_private (&role);for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word &&kvm_tdp_mmu_get_root (root))@@ -244,12 +250,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock);out:
arch/x86/kvm/mmu/mmu.c +6
-1-return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt);}
static void handle_changed_spte(struct kvm *kvm, int
as_id,gfn_tgfn,arch/x86/kvm/mmu/mmu.c +2
-2- u64 old_spte, u64 new_spte, int level, - bool shared); + u64 old_spte, u64 new_spte, + union kvm_mmu_page_role role, bool shared);static void
tdp_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp){
arch/x86/kvm/mmu/mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 395
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +6
-1@@ -376,12 +387,78 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) REMOVED_SPTE, level); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); + old_spte,REMOVED_SPTE , sp->role, + shared); + } + +if (is_private_sp (sp) && + WARN_ON(static_call(kvm_x86_free_private_spt)(kvm, sp->gfn, sp->role.level,
WARN_ON_ONCE()?
Quoted 21 lines
arch/x86/kvm/mmu/mmu.c +7
-0+ kvm_mmu_private_spt(sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_spt (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); }rcu_read_lock ();for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end) {arch/x86/kvm/mmu/mmu.c +0
-1@@ -960,10 +1158,26 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); -else
arch/x86/kvm/mmu/mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1167
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7
-3- wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); +else { +unsigned long pte_access =ACC_ALL ; +gfn_t gfn = iter->gfn; + +if (kvm_gfn_shared_mask (vcpu->kvm)) { +if (fault->is_private) + gfn |=kvm_gfn_shared_mask (vcpu->kvm);
this is an open-coded kvm_gfn_to_shared().
I don't get why a spte is installed for a shared gfn when fault->is_private is true. could you elaborate?
arch/x86/kvm/mmu/mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1172
Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +5
-0+else +/* +* TDX shared GPAs are no executable, enforce +* this for the SDV. +*/
what do you mean by the SDV?
Quoted 22 lines
arch/x86/kvm/mmu/mmu.c +10
-0+ pte_access &= ~ACC_EXEC_MASK ; + } + + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, gfn, + fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte); + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1041,6 +1255,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)struct kvm *kvm = vcpu->kvm;struct tdp_iter iter;struct kvm_mmu_page *sp; +gfn_t raw_gfn; +bool is_private = fault->is_private &&kvm_gfn_shared_mask (kvm);int ret =RET_PF_RETRY ;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1049,7 +1265,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
arch/x86/kvm/mmu/mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1274
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7
-1rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) || + !kvm_pfn_to_refcounted_page (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ;
This needs a comment. why this check is necessary? does this imply some kernel bugs?
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 9 lines · Chao Gao
arch/x86/kvm/mmu/mmu.c int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, · line 4670
Quoted 6 lines
if (
kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))arch/x86/kvm/mmu/mmu.c +1
-0@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };WARN_ON_ONCE (!vcpu->arch.mmu->root_role.direct); + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm);Could you clarify when shared bits need to be masked out or kept? shared bits are masked out here but kept in the hunk right above and ..
Sure, it deserves comment. I'll add a comment.
When we gets pfn, kvm_faultin_pfn() or loop with kvm memslot, drop shared bits because KVM memslot doesn't know about shared bit.
When walks in EPT tables, keep the shared bit because we need to find the EPT entry including shared bit.
Quoted 16 lines
arch/x86/kvm/mmu/tdp_iter.h struct tdp_iter { · line 95
Quoted 8 lines
arch/x86/kvm/mmu/tdp_iter.h +2
-1+++ b/arch/x86/kvm/mmu/tdp_iter.h@@ -91,7 +91,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;.. in @gfn of
tdp_iter.Quoted 5 lines
arch/x86/kvm/mmu/tdp_iter.h +0
-0/* The level of the root page given to the iterator */ int root_level;
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep, · line 215
Quoted 2 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-1-hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root(struct kvm_vcpu *vcpu,Maybe fold it into its sole caller.
Sure.
Quoted 83 lines
Quoted 36 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+ bool private){ union kvm_mmu_page_role role = vcpu->arch.mmu->root_role; struct kvm *kvm = vcpu->kvm;arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -221,6 +225,8 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)* Check for an existing root before allocating a new one. Note, the * role check prevents consuming an invalid root. */ +if (private) +kvm_mmu_page_role_set_private (&role);for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word &&kvm_tdp_mmu_get_root (root))@@ -244,12 +250,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock);out:
arch/x86/kvm/mmu/tdp_mmu.c +6
-1-return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt);}
static void handle_changed_spte(struct kvm *kvm, int
as_id,gfn_tgfn,arch/x86/kvm/mmu/tdp_mmu.c +2
-2- u64 old_spte, u64 new_spte, int level, - bool shared); + u64 old_spte, u64 new_spte, + union kvm_mmu_page_role role, bool shared);static void
tdp_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp){arch/x86/kvm/mmu/tdp_mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 395
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +6
-1@@ -376,12 +387,78 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) REMOVED_SPTE, level); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); + old_spte,REMOVED_SPTE , sp->role, + shared); + } + +if (is_private_sp (sp) && + WARN_ON(static_call(kvm_x86_free_private_spt)(kvm, sp->gfn, sp->role.level,
WARN_ON_ONCE()?Quoted 21 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0+ kvm_mmu_private_spt(sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_spt (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); }rcu_read_lock ();for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end) {arch/x86/kvm/mmu/tdp_mmu.c +0
-1@@ -960,10 +1158,26 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); -else arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1167
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-3- wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); +else { +unsigned long pte_access =ACC_ALL ; +gfn_t gfn = iter->gfn; + +if (kvm_gfn_shared_mask (vcpu->kvm)) { +if (fault->is_private) + gfn |=kvm_gfn_shared_mask (vcpu->kvm);this is an open-coded
kvm_gfn_to_shared().
Quoted 2 lines
I don't get why a spte is installed for a shared gfn when
fault->is_privateis true. could you elaborate?
This is stale code. And you're right. I'll remove this part.
Quoted 7 lines
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1172
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +5
-0+else +/* +* TDX shared GPAs are no executable, enforce +* this for the SDV. +*/ what do you mean by the SDV?
That's development nonsense. I'll remove the second sentence.
Quoted 24 lines
Quoted 24 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+ pte_access &= ~ACC_EXEC_MASK ; + } + + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, gfn, + fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte); + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1041,6 +1255,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)struct kvm *kvm = vcpu->kvm;struct tdp_iter iter;struct kvm_mmu_page *sp; +gfn_t raw_gfn; +bool is_private = fault->is_private &&kvm_gfn_shared_mask (kvm);int ret =RET_PF_RETRY ;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1049,7 +1265,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock ();
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1274
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-1-tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) || + !kvm_pfn_to_refcounted_page (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ;This needs a comment. why this check is necessary? does this imply some kernel bugs?
Will add a comment. It's due to the current TDX KVM implementation that increments the page refcount.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]Quoted 204 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate protected page table for private page table, and add hooks to operate on protected page table. This patch adds allocation/free of protected page tables and hooks. When calling hooks to update SPTE entry, freeze the entry, call hooks and unfreeze the entry to allow concurrent updates on page tables. Which is the advantage of TDP MMU. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet with this patch.When the faulting GPA is private, the KVM fault is called private. When resolving private KVM fault, allocate protected page table and call hooks to operate on protected page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to protected page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | CPU protected EPTP | | | V | V private PT root | protected PT root | | | V | Vprivate PT --hook to propagate-->protected PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|
PT: page tableThe existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For protected page table, hooks are called to update protected page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K protected SPTE => error vcpu 1: TDX SEAMCALL to update 2M protected SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update protected SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- dropCONFIG_KVM_MMU_PRIVATEv18:
- Rename freezed => frozenv14 -> v15:
- Refinedis_privatecondition check inkvm_tdp_mmu_map(). Addkvm_gfn_shared_mask()check.
- catch up for structkvm_rangechangeSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 11 ++ arch/x86/kvm/mmu/mmu.c | 17 +- arch/x86/kvm/mmu/mmu_internal.h | 13 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 308 +++++++++++++++++++++++++---- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- virt/kvm/kvm_main.c | 1 + 8 files changed, 320 insertions(+), 39 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +5
-0@@ -101,6 +101,11 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(link_private_spt) +KVM_X86_OP_OPTIONAL(free_private_spt) +KVM_X86_OP_OPTIONAL(set_private_spte) +KVM_X86_OP_OPTIONAL(remove_private_spte) +KVM_X86_OP_OPTIONAL(zap_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +11
-0@@ -468,6 +468,7 @@ struct kvm_mmu {int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1740,6 +1741,16 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*free_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*set_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*remove_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*zap_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +15
-2@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (tdp_mmu_enabled) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level);@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (kvm_mmu_honors_guest_mtrrs (vcpu->kvm)) {for ( ; fault->max_level >PG_LEVEL_4K ; --fault->max_level) {int page_num =KVM_PAGES_PER_HPAGE (fault->max_level); -gfn_t base =gfn_round_for_level (fault->gfn, +gfn_t base =gfn_round_for_level (gpa_to_gfn (fault->addr), fault->max_level);if (kvm_mtrr_check_gfn_range_consistency (vcpu, base, page_num))@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };WARN_ON_ONCE (!vcpu->arch.mmu->root_role.direct); + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); r =mmu_topup_memory_caches (vcpu, false);@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (tdp_mmu_enabled) { +write_lock (&vcpu->kvm->mmu_lock); +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL ); +write_unlock (&vcpu->kvm->mmu_lock); + }free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#ifdef CONFIG_KVM_PROVE_MMU #define KVM_MMU_WARN_ON (x) WARN_ON_ONCE(x)#else
arch/x86/kvm/mmu/mmu_internal.h static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp) · line 214
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h +5
-0@@ -205,6 +207,15 @@ static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp)free_page ((unsigned long )sp->private_spt); } +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_to_private (kvm, gfn);
IIUC, the purpose of this function is to add back shared bit to gfn for shared memory. For private address, the gfn should not contain shared bit anyway.
It seems weird to clear the shared bit from gfn for private address.
Quoted 131 lines
arch/x86/kvm/mmu/mmu_internal.h +5
-1+else +return kvm_gfn_to_shared (kvm, gfn); +} +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* @@ -363,7 +374,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int r;if (vcpu->arch.mmu->root_role.direct) { - fault.gfn = fault.addr >>PAGE_SHIFT ; + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); }arch/x86/kvm/mmu/tdp_iter.h +1
-1@@ -91,7 +91,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;arch/x86/kvm/mmu/tdp_mmu.c +53
-5@@ -187,6 +187,9 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu, sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache); sp->role = role; +if (kvm_mmu_page_role_is_private (role)) +kvm_mmu_alloc_private_spt (vcpu, sp); +return sp; }@@ -209,7 +212,8 @@ static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep,trace_kvm_mmu_get_page (sp, true); } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role = vcpu->arch.mmu->root_role;struct kvm *kvm = vcpu->kvm;@@ -221,6 +225,8 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)* Check for an existing root before allocating a new one. Note, the * role check prevents consuming an invalid root. */ +if (private) +kvm_mmu_page_role_set_private (&role);for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word &&kvm_tdp_mmu_get_root (root))@@ -244,12 +250,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +u64 old_spte,u64 new_spte, +union kvm_mmu_page_role role,bool shared);static void tdp_account_mmu_page (struct kvm *kvm,struct kvm_mmu_page *sp) {@@ -376,12 +387,78 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) REMOVED_SPTE, level); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); + old_spte,REMOVED_SPTE , sp->role, + shared); + } + +if (is_private_sp (sp) && +WARN_ON (static_call (kvm_x86_free_private_spt)(kvm, sp->gfn, sp->role.level, +kvm_mmu_private_spt (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_spt (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); } +static void *get_private_spt (gfn_t gfn,u64 new_spte,int level) +{ +if (is_shadow_present_pte (new_spte) && !is_last_spte (new_spte, level)) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (spte_to_pfn (new_spte))); +void *private_spt =kvm_mmu_private_spt (sp); + +WARN_ON_ONCE (!private_spt); +WARN_ON_ONCE (sp->role.level +1 != level); +WARN_ON_ONCE (sp->gfn != gfn); +return private_spt; + } + +return NULL ; +} + +static void handle_removed_private_spte (struct kvm *kvm,gfn_t gfn, +u64 old_spte,u64 new_spte, +int level) +{ +bool was_present =is_shadow_present_pte (old_spte); +bool is_present =is_shadow_present_pte (new_spte); +bool was_leaf = was_present &&is_last_spte (old_spte, level); +bool is_leaf = is_present &&is_last_spte (new_spte, level); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte);
arch/x86/kvm/mmu/tdp_mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 442
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+int ret; + +/* Ignore change of software only bits. e.g. host_writable */ +if (was_leaf == is_leaf && was_present == is_present) +return ; + +/* +* Allow only leaf page to be zapped. Reclaim Non-leaf page tables at +* destroying VM. +*/
The comment seems just for !was_leaf,
move the comment just before "if (!was_leaf)" ?
arch/x86/kvm/mmu/tdp_mmu.c static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 443
Quoted 1 line
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+WARN_ON_ONCE (is_present);
Is this warning needed?
It can be captured by the later "KVM_BUG_ON(new_pfn, kvm)"
Quoted 91 lines
arch/x86/kvm/mmu/tdp_mmu.c +45
-6+if (!was_leaf) +return ; + +/* non-present -> non-present doesn't make sense. */ +KVM_BUG_ON (!was_present, kvm); +KVM_BUG_ON (new_pfn, kvm); + +/* Zapping leaf spte is allowed only when write lock is held. */ +lockdep_assert_held_write (&kvm->mmu_lock); + ret =static_call (kvm_x86_zap_private_spte)(kvm, gfn, level); +/* Because write lock is held, operation should success. */ +if (KVM_BUG_ON (ret, kvm)) +return ; + + ret =static_call (kvm_x86_remove_private_spte)(kvm, gfn, level, old_pfn); +KVM_BUG_ON (ret, kvm); +} +/** * handle_changed_spte - handle bookkeeping associated with an SPTE change * @kvm: kvm instance @@ -389,7 +466,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* @gfn: the base GFN that was mapped by the SPTE * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change -* @level: the level of the PT the SPTE is part of in the paging structure +* @role: the role of the PT the SPTE is part of in the paging structure * @shared: This operation may not be running under the exclusive use of * the MMU lock and the operation must synchronize with other * threads that might be modifying SPTEs. @@ -399,14 +476,18 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)* and fast_pf_fix_direct_spte()). */ static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +u64 old_spte,u64 new_spte, +union kvm_mmu_page_role role,bool shared) { +bool is_private =kvm_mmu_page_role_is_private (role); +int level = role.level;bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn;WARN_ON_ONCE (level >PT64_ROOT_MAX_LEVEL );WARN_ON_ONCE (level <PG_LEVEL_4K );@@ -473,7 +554,7 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn);/* * Recursively handle child PTs if the change removed a subtree from @@ -482,14 +563,82 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* pages are kernel allocations and should never be migrated. */ if (was_present && !was_leaf && - (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) + (is_leaf || !is_present ||WARN_ON_ONCE (pfn_changed))) { +KVM_BUG_ON (is_private !=is_private_sptep (spte_to_child_pt (old_spte, level)), + kvm);handle_removed_pt (kvm,spte_to_child_pt (old_spte, level), shared); + } + +/* +* Secure-EPT requires to remove Secure-EPT tables after removing +* children. hooks after handling lower page table by above +* handle_remove_pt(). +*/ +if (is_private && !is_present) +handle_removed_private_spte (kvm, gfn, old_spte, new_spte, role.level);if (was_leaf &&is_accessed_spte (old_spte) && (!is_present || !is_accessed_spte (new_spte) || pfn_changed))kvm_set_pfn_accessed (spte_to_pfn (old_spte)); } +static int __must_check __set_private_spte_present (struct kvm *kvm,tdp_ptep_t sptep, +gfn_t gfn,u64 old_spte, +u64 new_spte,int level) +{ +bool was_present =is_shadow_present_pte (old_spte); +bool is_present =is_shadow_present_pte (new_spte);
arch/x86/kvm/mmu/tdp_mmu.c static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, · line 600
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+bool is_leaf = is_present &&is_last_spte (new_spte, level); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +int ret =0 ; + +lockdep_assert_held (&kvm->mmu_lock); +/* TDP MMU doesn't change present -> present */ +KVM_BUG_ON (was_present, kvm); + +/* +* Use different call to either set up middle level
^
calls
arch/x86/kvm/mmu/tdp_mmu.c static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, · line 604
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +4
-0+* private page table, or leaf. +*/ +if (is_leaf) + ret =static_call (kvm_x86_set_private_spte)(kvm, gfn, level, new_pfn);
Braces are missing.
Quoted 115 lines
arch/x86/kvm/mmu/tdp_mmu.c +77
-11+else { +void *private_spt =get_private_spt (gfn, new_spte, level); + +KVM_BUG_ON (!private_spt, kvm); + ret =static_call (kvm_x86_link_private_spt)(kvm, gfn, level, private_spt); + } + +return ret; +} + +static int __must_check set_private_spte_present (struct kvm *kvm,tdp_ptep_t sptep, +gfn_t gfn,u64 old_spte, +u64 new_spte,int level) +{ +int ret; + +/* +* For private page table, callbacks are needed to propagate SPTE +* change into the protected page table. In order to atomically update +* both the SPTE and the protected page tables with callbacks, utilize +* freezing SPTE. +* - Freeze the SPTE. Set entry to REMOVED_SPTE. +* - Trigger callbacks for protected page tables. +* - Unfreeze the SPTE. Set the entry to new_spte. +*/ +lockdep_assert_held (&kvm->mmu_lock); +if (!try_cmpxchg64 (sptep, &old_spte,REMOVED_SPTE )) +return -EBUSY ; + + ret =__set_private_spte_present (kvm, sptep, gfn, old_spte, new_spte, level); +if (ret) +__kvm_tdp_mmu_write_spte (sptep, old_spte); +else +__kvm_tdp_mmu_write_spte (sptep, new_spte); +return ret; +} +/* * tdp_mmu_set_spte_atomic - Set a TDP MMU SPTE atomically * and handle the associated bookkeeping. Do not mark the page dirty @@ -512,6 +661,7 @@ static inline int __must_check tdp_mmu_set_spte_atomic(struct kvm *kvm, u64 new_spte) {u64 *sptep =rcu_dereference (iter->sptep); +bool frozen = false;/* * The caller is responsible for ensuring the old SPTE is not a REMOVED @@ -523,19 +673,45 @@ static inline int __must_check tdp_mmu_set_spte_atomic(struct kvm *kvm,lockdep_assert_held_read (&kvm->mmu_lock); -/* -* Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs and -* does not hold the mmu_lock. On failure, i.e. if a different logical -* CPU modified the SPTE, try_cmpxchg64() updates iter->old_spte with -* the current value, so the caller operates on fresh data, e.g. if it -* retries tdp_mmu_set_spte_atomic() -*/ -if (!try_cmpxchg64 (sptep, &iter->old_spte, new_spte)) -return -EBUSY ; +if (is_private_sptep (iter->sptep) && !is_removed_spte (new_spte)) { +int ret; -handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +if (is_shadow_present_pte (new_spte)) { +/* +* Populating case. handle_changed_spte() can +* process without freezing because it only updates +* stats. +*/ + ret =set_private_spte_present (kvm, iter->sptep, iter->gfn, + iter->old_spte, new_spte, iter->level); +if (ret) +return ret; + }else { +/* +* Zapping case. handle_changed_spte() calls Secure-EPT +* blocking or removal. Freeze the entry. +*/ +if (!try_cmpxchg64 (sptep, &iter->old_spte,REMOVED_SPTE )) +return -EBUSY ; + frozen = true; + } + }else { +/* +* Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs +* and does not hold the mmu_lock. On failure, i.e. if a +* different logical CPU modified the SPTE, try_cmpxchg64() +* updates iter->old_spte with the current value, so the caller +* operates on fresh data, e.g. if it retries +* tdp_mmu_set_spte_atomic() +*/ +if (!try_cmpxchg64 (sptep, &iter->old_spte, new_spte)) +return -EBUSY ; + } +handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, + new_spte,sptep_to_sp (sptep)->role, true); +if (frozen) +__kvm_tdp_mmu_write_spte (sptep, new_spte);return 0 ; }@@ -585,6 +761,8 @@ static inline int __must_check tdp_mmu_zap_spte_atomic(struct kvm *kvm,static u64 tdp_mmu_set_spte (struct kvm *kvm,int as_id,tdp_ptep_t sptep,u64 old_spte,u64 new_spte,gfn_t gfn,int level) { +union kvm_mmu_page_role role; +lockdep_assert_held_write (&kvm->mmu_lock);/*
arch/x86/kvm/mmu/tdp_mmu.c static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, · line 780
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-0@@ -597,8 +775,17 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep,WARN_ON_ONCE (is_removed_spte (old_spte) ||is_removed_spte (new_spte)); old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte, new_spte, level); +if (is_private_sptep (sptep) && !is_removed_spte (new_spte) && +is_shadow_present_pte (new_spte)) { +lockdep_assert_held_write (&kvm->mmu_lock);
tdp_mmu_set_spte() has already called lockdep_assert_held_write() above.
Quoted 82 lines
arch/x86/kvm/mmu/tdp_mmu.c +42
-7+/* Because write spin lock is held, no race. It should success. */ +KVM_BUG_ON (__set_private_spte_present (kvm, sptep, gfn, old_spte, + new_spte, level), kvm); + } -handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, false); + role =sptep_to_sp (sptep)->role; + role.level = level; +handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, role, false);return old_spte; }@@ -621,8 +808,11 @@ static inline void tdp_mmu_iter_set_spte(struct kvm *kvm, struct tdp_iter *iter,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, root_to_sp(_mmu->root.hpa), _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + root_to_sp((_private) ? _mmu->private_root_hpa : \ + _mmu->root.hpa), \ + _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -784,6 +974,14 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,if (!zap_private &&is_private_sp (root))return false; +/* +* start and end doesn't have GFN shared bit. This function zaps +* a region including alias. Adjust shared bit of [start, end) if the +* root is shared. +*/ + start =kvm_gfn_for_root (kvm, root, start); + end =kvm_gfn_for_root (kvm, root, end); +rcu_read_lock ();for_each_tdp_pte_min_level (iter, root,PG_LEVEL_4K , start, end) {@@ -960,10 +1158,26 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); -else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); +else { +unsigned long pte_access =ACC_ALL ; +gfn_t gfn = iter->gfn; + +if (kvm_gfn_shared_mask (vcpu->kvm)) { +if (fault->is_private) + gfn |=kvm_gfn_shared_mask (vcpu->kvm); +else +/* +* TDX shared GPAs are no executable, enforce +* this for the SDV. +*/ + pte_access &= ~ACC_EXEC_MASK ; + } + + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, gfn, + fault->pfn, iter->old_spte, + fault->prefetch, true, fault->map_writable, + &new_spte); + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1041,6 +1255,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)struct kvm *kvm = vcpu->kvm;struct tdp_iter iter;struct kvm_mmu_page *sp; +gfn_t raw_gfn; +bool is_private = fault->is_private &&kvm_gfn_shared_mask (kvm);int ret =RET_PF_RETRY ;kvm_mmu_hugepage_adjust (vcpu, fault);
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1272
Quoted 9 lines
arch/x86/kvm/mmu/tdp_mmu.c +5
-1@@ -1049,7 +1265,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) || + !kvm_pfn_to_refcounted_page (fault->pfn)) { +if (is_private) {
Why this is only checked for private fault?
Quoted 27 lines
arch/x86/kvm/mmu/tdp_mmu.c +13
-2+rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {int r;if (fault->nx_huge_page_workaround_enabled)@@ -1079,9 +1305,14 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) sp->nx_huge_page_disallowed = fault->huge_page_disallowed; -if (is_shadow_present_pte (iter.old_spte)) +if (is_shadow_present_pte (iter.old_spte)) { +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +KVM_BUG_ON (is_private_sptep (iter.sptep), vcpu->kvm); r =tdp_mmu_split_huge_page (kvm, &iter, sp, true); -else + }else r =tdp_mmu_link_sp (kvm, &iter, sp, true);/*
arch/x86/kvm/mmu/tdp_mmu.c static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp, union kvm_mm · line 1597
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -1362,6 +1593,8 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp, union kvm_mm sp->role = role; sp->spt = (void *)__get_free_page (gfp); +/* TODO: large page support for private GPA. */ +WARN_ON_ONCE (kvm_mmu_page_role_is_private (role));
Seems not needed, since __tdp_mmu_alloc_sp_for_split() is only called in tdp_mmu_alloc_sp_for_split() and it has KVM_BUG_ON() for large page case.
Quoted 60 lines
arch/x86/kvm/mmu/tdp_mmu.c +9
-2if (!sp->spt) {kmem_cache_free (mmu_page_header_cache, sp);return NULL ;@@ -1378,6 +1611,10 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,struct kvm_mmu_page *sp;kvm_lockdep_assert_mmu_lock_held (kvm, shared); +KVM_BUG_ON (kvm_mmu_page_role_is_private (role) != +is_private_sptep (iter->sptep), kvm); +/* TODO: Large page isn't supported for private SPTE yet. */ +KVM_BUG_ON (kvm_mmu_page_role_is_private (role), kvm);/* * Since we are allocating while under the MMU lock we have to be @@ -1802,7 +2039,7 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, *root_level = vcpu->arch.mmu->root_role.level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1829,7 +2066,10 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +/* fast page fault for private GPA isn't supported. */ +WARN_ON_ONCE (kvm_is_private_gpa (vcpu->kvm, addr)); + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; }arch/x86/kvm/mmu/tdp_mmu.h +1
-1@@ -10,7 +10,7 @@void kvm_mmu_init_tdp_mmu (struct kvm *kvm);void kvm_mmu_uninit_tdp_mmu (struct kvm *kvm); -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {virt/kvm/kvm_main.c +1
-0@@ -201,6 +201,7 @@ struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)return NULL ; } +EXPORT_SYMBOL_GPL (kvm_pfn_to_refcounted_page);/* * Switches to specified vcpu, until a matching vcpu_put()
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 201 lines · Binbin Wu
Quoted 198 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Allocate protected page table for private page table, and add hooks to operate on protected page table. This patch adds allocation/free of protected page tables and hooks. When calling hooks to update SPTE entry, freeze the entry, call hooks and unfreeze the entry to allow concurrent updates on page tables. Which is the advantage of TDP MMU. As
kvm_gfn_shared_mask()returns false always, those hooks aren't called yet with this patch.When the faulting GPA is private, the KVM fault is called private. When resolving private KVM fault, allocate protected page table and call hooks to operate on protected page table. On the change of the private PTE entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to protected page table. The following depicts the relationship.private KVM page fault | | | V | private GPA | CPU protected EPTP | | | V | V private PT root | protected PT root | | | V | Vprivate PT --hook to propagate-->protected PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|
PT: page tableThe existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For protected page table, hooks are called to update protected page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K protected SPTE => error vcpu 1: TDX SEAMCALL to update 2M protected SPTETo avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update protected SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- dropCONFIG_KVM_MMU_PRIVATEv18:
- Rename freezed => frozenv14 -> v15:
- Refinedis_privatecondition check inkvm_tdp_mmu_map(). Addkvm_gfn_shared_mask()check.
- catch up for structkvm_rangechangeSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 11 ++ arch/x86/kvm/mmu/mmu.c | 17 +- arch/x86/kvm/mmu/mmu_internal.h | 13 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 308 +++++++++++++++++++++++++---- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- virt/kvm/kvm_main.c | 1 + 8 files changed, 320 insertions(+), 39 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +5
-0@@ -101,6 +101,11 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(link_private_spt) +KVM_X86_OP_OPTIONAL(free_private_spt) +KVM_X86_OP_OPTIONAL(set_private_spte) +KVM_X86_OP_OPTIONAL(remove_private_spte) +KVM_X86_OP_OPTIONAL(zap_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +11
-0@@ -468,6 +468,7 @@ struct kvm_mmu {int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;@@ -1740,6 +1741,16 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*free_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*set_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*remove_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*zap_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +15
-2@@ -3717,7 +3717,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (tdp_mmu_enabled) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level);@@ -4627,7 +4632,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (kvm_mmu_honors_guest_mtrrs (vcpu->kvm)) {for ( ; fault->max_level >PG_LEVEL_4K ; --fault->max_level) {int page_num =KVM_PAGES_PER_HPAGE (fault->max_level); -gfn_t base =gfn_round_for_level (fault->gfn, +gfn_t base =gfn_round_for_level (gpa_to_gfn (fault->addr), fault->max_level);if (kvm_mtrr_check_gfn_range_consistency (vcpu, base, page_num))@@ -4662,6 +4667,7 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, };WARN_ON_ONCE (!vcpu->arch.mmu->root_role.direct); + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); r =mmu_topup_memory_caches (vcpu, false);@@ -6166,6 +6172,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -7211,6 +7218,12 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (tdp_mmu_enabled) { +write_lock (&vcpu->kvm->mmu_lock); +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL ); +write_unlock (&vcpu->kvm->mmu_lock); + }free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/mmu_internal.h +4
-0@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#ifdef CONFIG_KVM_PROVE_MMU #define KVM_MMU_WARN_ON (x) WARN_ON_ONCE(x)#else @@ -205,6 +207,15 @@ static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp)free_page ((unsigned long )sp->private_spt); } +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn)
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp) · line 214
Quoted 3 lines
arch/x86/kvm/mmu/mmu_internal.h +3
-0+{ +if (is_private_sp (root)) +return kvm_gfn_to_private (kvm, gfn);IIUC, the purpose of this function is to add back shared bit to gfn for shared memory.
For private address, the gfn should not contain shared bit anyway. It seems weird to clear the shared bit from gfn for private address.
The current caller happens to do so. With such assumption, we can code it as something like
if (is_private_sp(root)) {
WARN_ON_ONCE(gfn & kvm_gfn_shared_mask(kvm));
return gfn;
}... snip ...
Quoted 51 lines
Quoted 51 lines
arch/x86/kvm/mmu/mmu_internal.h +43
-1@@ -376,12 +387,78 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) REMOVED_SPTE, level); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_spte,REMOVED_SPTE , level, shared); + old_spte,REMOVED_SPTE , sp->role, + shared); + } + +if (is_private_sp (sp) && +WARN_ON (static_call (kvm_x86_free_private_spt)(kvm, sp->gfn, sp->role.level, +kvm_mmu_private_spt (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_spt (sp,NULL ); }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); } +static void *get_private_spt (gfn_t gfn,u64 new_spte,int level) +{ +if (is_shadow_present_pte (new_spte) && !is_last_spte (new_spte, level)) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (spte_to_pfn (new_spte))); +void *private_spt =kvm_mmu_private_spt (sp); + +WARN_ON_ONCE (!private_spt); +WARN_ON_ONCE (sp->role.level +1 != level); +WARN_ON_ONCE (sp->gfn != gfn); +return private_spt; + } + +return NULL ; +} + +static void handle_removed_private_spte (struct kvm *kvm,gfn_t gfn, +u64 old_spte,u64 new_spte, +int level) +{ +bool was_present =is_shadow_present_pte (old_spte); +bool is_present =is_shadow_present_pte (new_spte); +bool was_leaf = was_present &&is_last_spte (old_spte, level); +bool is_leaf = is_present &&is_last_spte (new_spte, level); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +int ret; + +/* Ignore change of software only bits. e.g. host_writable */ +if (was_leaf == is_leaf && was_present == is_present) +return ;
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 440
Quoted 5 lines
arch/x86/kvm/mmu/mmu_internal.h +5
-0+ +/* +* Allow only leaf page to be zapped. Reclaim Non-leaf page tables at +* destroying VM. +*/ The comment seems just for !was_leaf,
move the comment just before "if (!was_leaf)" ?
Makes sense.
Quoted 4 lines
arch/x86/kvm/mmu/mmu_internal.h static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) · line 441
Quoted 1 line
arch/x86/kvm/mmu/mmu_internal.h +1
-0+WARN_ON_ONCE (is_present);Is this warning needed?
It can be captured by the later "KVM_BUG_ON(new_pfn, kvm)"
Yes, let's remove this warn_on.
... snip ...
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, · line 779
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +3
-0@@ -597,8 +775,17 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep,WARN_ON_ONCE (is_removed_spte (old_spte) ||is_removed_spte (new_spte)); old_spte =kvm_tdp_mmu_write_spte (sptep, old_spte, new_spte, level); +if (is_private_sptep (sptep) && !is_removed_spte (new_spte) && +is_shadow_present_pte (new_spte)) { +lockdep_assert_held_write (&kvm->mmu_lock);
tdp_mmu_set_spte()has already calledlockdep_assert_held_write()above.
Ok.
Quoted 10 lines
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0@@ -1041,6 +1255,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)struct kvm *kvm = vcpu->kvm;struct tdp_iter iter;struct kvm_mmu_page *sp; +gfn_t raw_gfn; +bool is_private = fault->is_private &&kvm_gfn_shared_mask (kvm);int ret =RET_PF_RETRY ;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1049,7 +1265,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock ();
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1270
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +5
-1-tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + +if (is_error_noslot_pfn (fault->pfn) || + !kvm_pfn_to_refcounted_page (fault->pfn)) { +if (is_private) {Why this is only checked for private fault?
Because (the current implementation of) the TDX vendor backend gets page reference count. In future, it should be removed with allowing page migration.
Quoted 23 lines
Quoted 23 lines
arch/x86/kvm/mmu/mmu_internal.h +13
-2+rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {int r;if (fault->nx_huge_page_workaround_enabled)@@ -1079,9 +1305,14 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) sp->nx_huge_page_disallowed = fault->huge_page_disallowed; -if (is_shadow_present_pte (iter.old_spte)) +if (is_shadow_present_pte (iter.old_spte)) { +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +KVM_BUG_ON (is_private_sptep (iter.sptep), vcpu->kvm); r =tdp_mmu_split_huge_page (kvm, &iter, sp, true); -else + }else r =tdp_mmu_link_sp (kvm, &iter, sp, true);/* @@ -1362,6 +1593,8 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp, union kvm_mm
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp, union kvm_mm · line 1596
Quoted 4 lines
arch/x86/kvm/mmu/mmu_internal.h +2
-0sp->role = role; sp->spt = (void *)__get_free_page (gfp); +/* TODO: large page support for private GPA. */ +WARN_ON_ONCE (kvm_mmu_page_role_is_private (role));Seems not needed, since
__tdp_mmu_alloc_sp_for_split()is only called intdp_mmu_alloc_sp_for_split()and it hasKVM_BUG_ON()for large page case.
Ah, yes. Will remove one of two.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 062/130] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1259
Quoted 8 lines · On Mon, Feb 26, 2024 at 12:26:04AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -1041,6 +1255,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)struct kvm *kvm = vcpu->kvm;struct tdp_iter iter;struct kvm_mmu_page *sp; +gfn_t raw_gfn; +bool is_private = fault->is_private &&kvm_gfn_shared_mask (kvm);
Why not put the checking of kvm_gfn_shared_mask() earlier before determining fault->is_private?
e.g. in kvm_mmu_page_fault().
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1270
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-1int ret =RET_PF_RETRY ;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1049,7 +1265,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + + if (is_error_noslot_pfn(fault->pfn) ||
The checking for noslot pfn is not required after this cleanup series [1] from Sean, right?
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1271
Quoted 1 line
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+ !kvm_pfn_to_refcounted_page(fault->pfn)) {
What's the purpose of rejecting non-refcounted page (which is useful for trusted IO)?
Besides, looks pages are allocated via kvm_faultin_pfn_private() if fault->is_private, so where is the non-refcounted page from?
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1276
Quoted 5 lines
arch/x86/kvm/mmu/tdp_mmu.c +5
-0+if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + }
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {int r;if (fault->nx_huge_page_workaround_enabled)
...
Quoted 16 lines
virt/kvm/kvm_main.c +1
-0@@ -201,6 +201,7 @@ struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)return NULL ; } +EXPORT_SYMBOL_GPL (kvm_pfn_to_refcounted_page);/* * Switches to specified vcpu, until a matching vcpu_put() Signature · 1 line
2.25.1
PATCH v19 63/130 The start of TDX KVM patch series: TDX EPT violation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX EPT violation.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -20,11 +20,11 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Not yet +* TDX EPT violation: Applying * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied * KVM TDP refactoring for TDX: Applied -* KVM TDP MMU hooks: Applying +* KVM TDP MMU hooks: Applied
Signature · 1 line
2.25.1
PATCH v19 64/130 KVM: x86/mmu: Do not enable page track for TD guest
In reply to [email protected]From: Yan Zhao <[email protected]>
TDX does not support write protection and hence page track. Though !tdp_enabled and kvm_shadow_root_allocated(kvm) are always false for TD guest, should also return false when external write tracking is enabled.
---
v19:
- drop TDX: from the short log
- Added reviewed-by: BinBin
--- arch/x86/kvm/mmu/page_track.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/mmu/page_track.c +3 -0
@@ -22,6 +22,9 @@bool kvm_page_track_write_tracking_enabled (struct kvm *kvm) { +if (kvm->arch.vm_type ==KVM_X86_TDX_VM ) +return false; +return IS_ENABLED (CONFIG_KVM_EXTERNAL_WRITE_TRACKING ) || !tdp_enabled ||kvm_shadow_root_allocated (kvm); }
Signature · 1 line
2.25.1
PATCH v19 65/130 KVM: VMX: Split out guts of EPT violation to common/exposed function
In reply to [email protected]From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
--- arch/x86/kvm/vmx/common.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 25 +++---------------------- 2 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/common.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/vmx.c +3 -22
@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "hyperv.h" #include "kvm_onhyperv.h"
@@ -5779,11 +5780,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu);gpa_t gpa; -u64 error_code; - - exit_qualification =vmx_get_exit_qual (vcpu);/* * EPT violation happened while executing iret from NMI,
@@ -5798,23 +5796,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS );trace_kvm_page_fault (vcpu, gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
@@ -5828,7 +5809,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr && !kvm_vcpu_is_legal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
PATCH v19 66/130 KVM: TDX: Add accessors VMX VMCS helpers
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines SEAMCALL APIs to access TDX control structures corresponding to the VMX VMCS. Introduce helper accessors to hide its SEAMCALL ABI details.
---
v19:
- deleted unnecessary stub functions, tdvps_state_non_arch_check() and tdvps_management_check().
--- arch/x86/kvm/vmx/tdx.h | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)
arch/x86/kvm/vmx/tdx.h +92 -0
@@ -58,6 +58,98 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); } +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) +{ +#define VMCS_ENC_ACCESS_TYPE_MASK 0x1UL +#define VMCS_ENC_ACCESS_TYPE_FULL 0x0UL +#define VMCS_ENC_ACCESS_TYPE_HIGH 0x1UL +#define VMCS_ENC_ACCESS_TYPE (field) ((field) & VMCS_ENC_ACCESS_TYPE_MASK) + +/* TDX is 64bit only. HIGH field isn't supported. */ +BUILD_BUG_ON_MSG (__builtin_constant_p (field) && +VMCS_ENC_ACCESS_TYPE (field) ==VMCS_ENC_ACCESS_TYPE_HIGH , +"Read/Write to TD VMCS *_HIGH fields not supported" ); + +BUILD_BUG_ON (bits !=16 && bits !=32 && bits !=64 ); + +#define VMCS_ENC_WIDTH_MASK GENMASK(14, 13) +#define VMCS_ENC_WIDTH_16BIT (0UL << 13) +#define VMCS_ENC_WIDTH_64BIT (1UL << 13) +#define VMCS_ENC_WIDTH_32BIT (2UL << 13) +#define VMCS_ENC_WIDTH_NATURAL (3UL << 13) +#define VMCS_ENC_WIDTH (field) ((field) & VMCS_ENC_WIDTH_MASK) + +/* TDX is 64bit only. i.e. natural width = 64bit. */ +BUILD_BUG_ON_MSG (bits !=64 &&__builtin_constant_p (field) && + (VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_64BIT || +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_NATURAL ), +"Invalid TD VMCS access for 64-bit field" ); +BUILD_BUG_ON_MSG (bits !=32 &&__builtin_constant_p (field) && +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_32BIT , +"Invalid TD VMCS access for 32-bit field" ); +BUILD_BUG_ON_MSG (bits !=16 &&__builtin_constant_p (field) && +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_16BIT , +"Invalid TD VMCS access for 16-bit field" ); +} + +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ + u32 field) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_rd(tdx->tdvpr_pa, TDVPS_##uclass(field), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) { \ + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ + field, err); \ + return 0; \ + } \ + return (u##bits)out.r8; \ +} \ +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ + u32 field, u##bits val) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), val, \ + GENMASK_ULL(bits - 1, 0), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ + field, (u64)val, err); \ +} \ +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), bit, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} \ +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), 0, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} + +TDX_BUILD_TDVPS_ACCESSORS (16 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out;
Signature · 1 line
2.25.1
PATCH v19 67/130 KVM: TDX: Add load_mmu_pgd method for TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
---
v19:
- Add WARN_ON_ONCE() to tdx_load_mmu_pgd() and drop unconditional mask
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 13 ++++++++++++- arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -236,6 +236,7 @@ enum vmcs_field {TSC_MULTIPLIER_HIGH =0x00002033 ,TERTIARY_VM_EXEC_CONTROL =0x00002034 ,TERTIARY_VM_EXEC_CONTROL_HIGH =0x00002035 , +SHARED_EPT_POINTER =0x0000203C ,PID_POINTER_TABLE =0x00002042 ,PID_POINTER_TABLE_HIGH =0x00002043 , GUEST_PHYSICAL_ADDRESS = 0x00002400,
arch/x86/kvm/vmx/main.c +12 -1
@@ -123,6 +123,17 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) { +tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +return ; + } + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -256,7 +267,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,
arch/x86/kvm/vmx/tdx.c +6 -0
@@ -453,6 +453,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +WARN_ON_ONCE (root_hpa & ~PAGE_MASK ); +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa); +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -152,6 +152,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -173,6 +175,8 @@ static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
Re: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption.
Virtual IO is a use case of shared memory, it's better to use it as a example instead of putting it at the beginning of the sentence.
Quoted 7 lines
Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
May have a mention that the EPTP for priavet GPAs is set by TDX module.
Quoted 96 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>---
v19:
- AddWARN_ON_ONCE()totdx_load_mmu_pgd()and drop unconditional mask--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 13 ++++++++++++- arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-)arch/x86/include/asm/vmx.h +1
-0@@ -236,6 +236,7 @@ enum vmcs_field {TSC_MULTIPLIER_HIGH =0x00002033 ,TERTIARY_VM_EXEC_CONTROL =0x00002034 ,TERTIARY_VM_EXEC_CONTROL_HIGH =0x00002035 , +SHARED_EPT_POINTER =0x0000203C ,PID_POINTER_TABLE =0x00002042 ,PID_POINTER_TABLE_HIGH =0x00002043 , GUEST_PHYSICAL_ADDRESS = 0x00002400,arch/x86/kvm/vmx/main.c +12
-1@@ -123,6 +123,17 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) { +tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +return ; + } + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -256,7 +267,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,arch/x86/kvm/vmx/tdx.c +6
-0@@ -453,6 +453,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +WARN_ON_ONCE (root_hpa & ~PAGE_MASK ); +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa); +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/x86_ops.h +4
-0@@ -152,6 +152,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}@@ -173,6 +175,8 @@ static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Re: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 12 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption.
Virtual IO is a use case of shared memory, it's better to use it as a example instead of putting it at the beginning of the sentence.
Quoted 8 lines
Quoted 7 lines
Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
May have a mention that the EPTP for priavet GPAs is set by TDX module.
Sure, let me update the commit message.
Signature · 1 line
Isaku Yamahata <[email protected]>
Edgecombe, Rick P
maintainerRe: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 17 lines · Isaku Yamahata
Quoted 14 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption.
Virtual IO is a use case of shared memory, it's better to use it as a example instead of putting it at the beginning of the sentence.
Quoted 2 lines
Shared EPT is used to map guest pages in unprotected way.
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
May have a mention that the EPTP for priavet GPAs is set by TDX module.
Sure, let me update the commit message.
How about this?
KVM: TDX: Add load_mmu_pgd method for TDX
TDX has uses two EPT pointers, one for the private half of the GPA space and one for the shared half. The private half used the normal EPT_POINTER vmcs field and is managed in a special way by the TDX module. The shared half uses a new SHARED_EPT_POINTER field and will be managed by the conventional MMU management operations that operate directly on the EPT tables. This means for TDX the .load_mmu_pgd() operation will need to know to use the SHARED_EPT_POINTER field instead of the normal one. Add a new wrapper in x86 ops for load_mmu_pgd() that either directs the write to the existing vmx implementation or a TDX one.
For the TDX operation, EPT will always be used, so it can simpy write to the SHARED_EPT_POINTER field.
Re: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 22 lines · Rick P
Quoted 21 lines · Isaku Yamahata
Quoted 18 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption.
Virtual IO is a use case of shared memory, it's better to use it as a example instead of putting it at the beginning of the sentence.
Quoted 6 lines
Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
May have a mention that the EPTP for priavet GPAs is set by TDX module.
Sure, let me update the commit message.
How about this?
Looks good. Some nits though:
Quoted 4 lines
KVM: TDX: Add
load_mmu_pgdmethod for TDXTDX has uses two EPT pointers, one for the private half of the GPA
"TDX uses"
Quoted 1 line
space and one for the shared half. The private half used the normal
"used" -> "uses"
Quoted 1 line
EPT_POINTERvmcs field and is managed in a special way by the TDX module.
Perhaps add:
KVM is not allowed to operate on the EPT_POINTER directly.
Quoted 4 lines
The shared half uses a new
SHARED_EPT_POINTERfield and will be managed by the conventional MMU management operations that operate directly on the EPT tables.
I would like to explicitly call out KVM can update SHARED_EPT_POINTER directly:
The shared half uses a new SHARED_EPT_POINTER field. KVM is allowed to set it directly by the interface provided by the TDX module, and KVM is expected to manage the shared half just like it manages the existing EPT page table today.
Quoted 7 lines
This means for TDX the .load_mmu_pgd() operation will need to know to use the
SHARED_EPT_POINTERfield instead of the normal one. Add a new wrapper in x86 ops forload_mmu_pgd()that either directs the write to the existing vmx implementation or a TDX one.For the TDX operation, EPT will always be used, so it can simpy write to the
SHARED_EPT_POINTERfield.
Re: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 44 lines · Kai
Quoted 16 lines · Rick P
Quoted 15 lines · Isaku Yamahata
Quoted 12 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption.
Virtual IO is a use case of shared memory, it's better to use it as a example instead of putting it at the beginning of the sentence.
Quoted 2 lines
Shared EPT is used to map guest pages in unprotected way.
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
May have a mention that the EPTP for priavet GPAs is set by TDX module.
Sure, let me update the commit message.
How about this?
Looks good. Some nits though:
Quoted 3 lines
KVM: TDX: Add
load_mmu_pgdmethod for TDXTDX has uses two EPT pointers, one for the private half of the GPA
"TDX uses"
Quoted 1 line
space and one for the shared half. The private half used the normal
"used" -> "uses"
Quoted 1 line
EPT_POINTERvmcs field and is managed in a special way by the TDX module.Perhaps add:
KVM is not allowed to operate on the
EPT_POINTERdirectly.Quoted 4 lines
The shared half uses a new
SHARED_EPT_POINTERfield and will be managed by the conventional MMU management operations that operate directly on the EPT tables.
Quoted 13 lines
I would like to explicitly call out KVM can update
SHARED_EPT_POINTERdirectly:The shared half uses a new
SHARED_EPT_POINTERfield. KVM is allowed to set it directly by the interface provided by the TDX module, and KVM is expected to manage the shared half just like it manages the existing EPT page table today.Quoted 6 lines
This means for TDX the .load_mmu_pgd() operation will need to know to use the
SHARED_EPT_POINTERfield instead of the normal one. Add a new wrapper in x86 ops forload_mmu_pgd()that either directs the write to the existing vmx implementation or a TDX one.For the TDX operation, EPT will always be used, so it can simpy write to
Maybe remove "so"? IMO, there is no causal relationship between the first and second half of the sentence.
Typo, "simpy" -> "simply"
Quoted 2 lines
Quoted 1 line
the
SHARED_EPT_POINTERfield.
Edgecombe, Rick P
maintainerRe: [PATCH v19 067/130] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 31 lines · Binbin Wu
Quoted 31 lines
Looks good. Some nits though:
Quoted 3 lines
KVM: TDX: Add
load_mmu_pgdmethod for TDXTDX has uses two EPT pointers, one for the private half of the GPA
"TDX uses"
Quoted 1 line
space and one for the shared half. The private half used the normal
"used" -> "uses"
Quoted 1 line
EPT_POINTERvmcs field and is managed in a special way by the TDX module.Perhaps add:
KVM is not allowed to operate on the
EPT_POINTERdirectly.Quoted 4 lines
The shared half uses a new
SHARED_EPT_POINTERfield and will be managed by the conventional MMU management operations that operate directly on the EPT tables.I would like to explicitly call out KVM can update
SHARED_EPT_POINTERdirectly:The shared half uses a new
SHARED_EPT_POINTERfield. KVM is allowed to set it directly by the interface provided by the TDX module, and KVM is expected to manage the shared half just like it manages the existing EPT page table today.Quoted 5 lines
This means for TDX the .load_mmu_pgd() operation will need to know to use the
SHARED_EPT_POINTERfield instead of the normal one. Add a new wrapper in x86 ops forload_mmu_pgd()that either directs the write to the existing vmx implementation or a TDX one.
Quoted 6 lines
Quoted 1 line
Quoted 1 line
For the TDX operation, EPT will always be used, so it can simpy write to
Maybe remove "so"? IMO, there is no causal relationship between the first and second half of the sentence.
I was trying to nod at why tdx_load_mmu_pgd() is so much simpler than vmx_load_mmu_pgd(). Here is a new version with all the feedback:
KVM: TDX: Add load_mmu_pgd method for TDX
TDX uses two EPT pointers, one for the private half of the GPA space and one for the shared half. The private half uses the normal EPT_POINTER vmcs field, which is managed in a special way by the TDX module. For TDX, KVM is not allowed to operate on it directly. The shared half uses a new SHARED_EPT_POINTER field and will be managed by the conventional MMU management operations that operate directly on the EPT root. This means for TDX the .load_mmu_pgd() operation will need to know to use the SHARED_EPT_POINTER field instead of the normal one. Add a new wrapper in x86 ops for load_mmu_pgd() that either directs the write to the existing vmx implementation or a TDX one.
For the TDX mode of operation, EPT will always be used and KVM does not need to be involved in virtualization of CR3 behavior. So tdx_load_mmu_pgd() can simply write to SHARED_EPT_POINTER.
PATCH v19 68/130 KVM: TDX: Retry seamcall when TDX_OPERAND_BUSY with operand SEPT
In reply to [email protected]From: Yuan Yao <[email protected]>
TDX module internally uses locks to protect internal resources. It tries to acquire the locks. If it fails to obtain the lock, it returns TDX_OPERAND_BUSY error without spin because its execution time limitation.
TDX SEAMCALL API reference describes what resources are used. It's known which TDX SEAMCALL can cause contention with which resources. VMM can avoid contention inside the TDX module by avoiding contentious TDX SEAMCALL with, for example, spinlock. Because OS knows better its process scheduling and its scalability, a lock at OS/VMM layer would work better than simply retrying TDX SEAMCALLs.
TDH.MEM.* API except for TDH.MEM.TRACK operates on a secure EPT tree and the TDX module internally tries to acquire the lock of the secure EPT tree. They return TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT in case of failure to get the lock. TDX KVM allows sept callbacks to return error so that TDP MMU layer can retry.
Retry TDX TDH.MEM.* API on the error because the error is a rare event caused by zero-step attack mitigation.
---
v19:
- fix typo TDG.VP.ENTER => TDH.VP.ENTER, TDX_OPRRAN_BUSY => TDX_OPERAND_BUSY
- drop the description on TDH.VP.ENTER as this patch doesn't touch TDH.VP.ENTER
--- arch/x86/kvm/vmx/tdx_ops.h | 48 +++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/tdx_ops.h +39 -9
@@ -44,6 +44,36 @@ static inline u64 tdx_seamcall(u64 op, struct tdx_module_args *in,void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out);#endif +/* +* TDX module acquires its internal lock for resources. It doesn't spin to get +* locks because of its restrictions of allowed execution time. Instead, it +* returns TDX_OPERAND_BUSY with an operand id. +* +* Multiple VCPUs can operate on SEPT. Also with zero-step attack mitigation, +* TDH.VP.ENTER may rarely acquire SEPT lock and release it when zero-step +* attack is suspected. It results in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT +* with TDH.MEM.* operation. Note: TDH.MEM.TRACK is an exception. +* +* Because TDP MMU uses read lock for scalability, spin lock around SEAMCALL +* spoils TDP MMU effort. Retry several times with the assumption that SEPT +* lock contention is rare. But don't loop forever to avoid lockup. Let TDP +* MMU retry. +*/ +#define TDX_ERROR_SEPT_BUSY (TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT) + +static inline u64 tdx_seamcall_sept (u64 op,struct tdx_module_args *in, +struct tdx_module_args *out) +{ +#define SEAMCALL_RETRY_MAX 16 +int retry =SEAMCALL_RETRY_MAX ; +u64 ret; + +do { + ret =tdx_seamcall (op, in, out); + }while (ret ==TDX_ERROR_SEPT_BUSY && retry-- >0 ); +return ret; +} +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) {struct tdx_module_args in = {
@@ -66,7 +96,7 @@ static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, hpa_t hpa, hpa_t source };clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_ADD , &in, out); +return tdx_seamcall_sept (TDH_MEM_PAGE_ADD , &in, out); } static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page,
@@ -79,7 +109,7 @@ static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page, };clflush_cache_range (__va (page),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_SEPT_ADD , &in, out); +return tdx_seamcall_sept (TDH_MEM_SEPT_ADD , &in, out); } static inline u64 tdh_mem_sept_rd(hpa_t tdr, gpa_t gpa, int level,
@@ -90,7 +120,7 @@ static inline u64 tdh_mem_sept_rd(hpa_t tdr, gpa_t gpa, int level, .rdx = tdr, }; -return tdx_seamcall (TDH_MEM_SEPT_RD , &in, out); +return tdx_seamcall_sept (TDH_MEM_SEPT_RD , &in, out); } static inline u64 tdh_mem_sept_remove(hpa_t tdr, gpa_t gpa, int level,
@@ -101,7 +131,7 @@ static inline u64 tdh_mem_sept_remove(hpa_t tdr, gpa_t gpa, int level, .rdx = tdr, }; -return tdx_seamcall (TDH_MEM_SEPT_REMOVE , &in, out); +return tdx_seamcall_sept (TDH_MEM_SEPT_REMOVE , &in, out); }static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr)
@@ -125,7 +155,7 @@ static inline u64 tdh_mem_page_relocate(hpa_t tdr, gpa_t gpa, hpa_t hpa, };clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_RELOCATE , &in, out); +return tdx_seamcall_sept (TDH_MEM_PAGE_RELOCATE , &in, out); } static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa,
@@ -138,7 +168,7 @@ static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa, };clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_AUG , &in, out); +return tdx_seamcall_sept (TDH_MEM_PAGE_AUG , &in, out); } static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level,
@@ -149,7 +179,7 @@ static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level, .rdx = tdr, }; -return tdx_seamcall (TDH_MEM_RANGE_BLOCK , &in, out); +return tdx_seamcall_sept (TDH_MEM_RANGE_BLOCK , &in, out); }static inline u64 tdh_mng_key_config (hpa_t tdr)
@@ -299,7 +329,7 @@ static inline u64 tdh_mem_page_remove(hpa_t tdr, gpa_t gpa, int level, .rdx = tdr, }; -return tdx_seamcall (TDH_MEM_PAGE_REMOVE , &in, out); +return tdx_seamcall_sept (TDH_MEM_PAGE_REMOVE , &in, out); }static inline u64 tdh_sys_lp_shutdown (void )
@@ -327,7 +357,7 @@ static inline u64 tdh_mem_range_unblock(hpa_t tdr, gpa_t gpa, int level, .rdx = tdr, }; -return tdx_seamcall (TDH_MEM_RANGE_UNBLOCK , &in, out); +return tdx_seamcall_sept (TDH_MEM_RANGE_UNBLOCK , &in, out); }static inline u64 tdh_phymem_cache_wb (bool resume)
Signature · 1 line
2.25.1
PATCH v19 69/130 KVM: TDX: Require TDP MMU and mmio caching for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching. Disable TDX support when TDP MMU or mmio caching aren't supported.
--- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/main.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -104,6 +104,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);* If the hardware supports that we don't need to do shadow paging. */ bool tdp_enabled = false; +EXPORT_SYMBOL_GPL (tdp_enabled);static bool __ro_after_init tdp_mmu_allowed;
arch/x86/kvm/vmx/main.c +13 -0
@@ -3,6 +3,7 @@#include "x86_ops.h" #include "vmx.h" +#include "mmu.h" #include "nested.h" #include "pmu.h" #include "tdx.h"
@@ -36,6 +37,18 @@ static __init int vt_hardware_setup(void)if (ret)return ret; +/* TDX requires KVM TDP MMU. */ +if (enable_tdx && !tdp_enabled) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires TDP MMU. Please enable TDP MMU for TDX.\n" ); + } + +/* TDX requires MMIO caching. */ +if (enable_tdx && !enable_mmio_caching) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); + } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,
Signature · 1 line
2.25.1
Re: [PATCH v19 069/130] KVM: TDX: Require TDP MMU and mmio caching for TDX
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching.
Can you add some description about why TDX requires mmio caching in the changelog?
Quoted 52 lines
Disable
TDX support when TDP MMU or mmio caching aren't supported.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/main.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)arch/x86/kvm/mmu/mmu.c +1
-0@@ -104,6 +104,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);* If the hardware supports that we don't need to do shadow paging. */ bool tdp_enabled = false; +EXPORT_SYMBOL_GPL (tdp_enabled);static bool __ro_after_init tdp_mmu_allowed;arch/x86/kvm/vmx/main.c +13
-0@@ -3,6 +3,7 @@#include "x86_ops.h" #include "vmx.h" +#include "mmu.h" #include "nested.h" #include "pmu.h" #include "tdx.h" @@ -36,6 +37,18 @@ static __init int vt_hardware_setup(void)if (ret)return ret; +/* TDX requires KVM TDP MMU. */ +if (enable_tdx && !tdp_enabled) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires TDP MMU. Please enable TDP MMU for TDX.\n" ); + } + +/* TDX requires MMIO caching. */ +if (enable_tdx && !enable_mmio_caching) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); + } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,
Re: [PATCH v19 069/130] KVM: TDX: Require TDP MMU and mmio caching for TDX
Quoted 10 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching.
Can you add some description about why TDX requires mmio caching in the changelog?
Sure, will update the commit log.
As the TDX guest is protected, the guest has to issue TDG.VP.VMCALL<MMIO> on VE. The VMM has to setup Shared-EPT entry to inject VE by setting the entry value with VE suppress bit cleared.
KVM mmio caching is a feature to set the EPT entry to special value for MMIO GFN instead of the default value with suppress VE bit set. So TDX KVM wants to utilize it.
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v19 069/130] KVM: TDX: Require TDP MMU and mmio caching for TDX
In reply to [email protected]Quoted 17 lines · On Mon, Feb 26, 2024, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching. Disable TDX support when TDP MMU or mmio caching aren't supported.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/main.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644); · line 107
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +1
-0@@ -104,6 +104,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);* If the hardware supports that we don't need to do shadow paging. */ bool tdp_enabled = false; +EXPORT_SYMBOL_GPL (tdp_enabled);
I haven't looked at the rest of the series, but this should be unnecessary. Just use enable_ept. Ah, the code is wrong.
Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0
-0static bool __ro_after_init tdp_mmu_allowed;arch/x86/kvm/vmx/main.c +1
-0@@ -3,6 +3,7 @@#include "x86_ops.h" #include "vmx.h" +#include "mmu.h" #include "nested.h" #include "pmu.h" #include "tdx.h"
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 40
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-0@@ -36,6 +37,18 @@ static __init int vt_hardware_setup(void)if (ret)return ret; +/* TDX requires KVM TDP MMU. */
This is a useless comment (assuming the code is correctly written), it's quite obvious from:
if (!tdp_mmu_enabled) enable_tdx = false;
that the TDP MMU is required. Explaining *why* could be useful, but I'd probably just omit a comment. In the not too distant future, it will likely be common knowledge that the shadow MMU doesn't support newfangled features, and it _should_ be very easy for someone to get the info from the changelog.
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 41
Quoted 1 line
arch/x86/kvm/vmx/main.c +1
-0+if (enable_tdx && !tdp_enabled) {
tdp_enabled can be true without the TDP MMU, you want tdp_mmu_enabled.
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 43
Quoted 2 lines
arch/x86/kvm/vmx/main.c +2
-0+ enable_tdx = false; +pr_warn_ratelimited ("TDX requires TDP MMU. Please enable TDP MMU for TDX.\n" );
Drop the pr_warn(), TDX will presumably be on by default at some point, I don't want to get spam every time I test with TDP disabled.
Also, ratelimiting this code is pointless (as is _once()), it should only ever be called once per module module, and the limiting/once protections are tied to the module, i.e. effectively get reset when a module is reloaded.
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 49
Quoted 6 lines
arch/x86/kvm/vmx/main.c +6
-0+ } + +/* TDX requires MMIO caching. */ +if (enable_tdx && !enable_mmio_caching) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" );
Same comments here.
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 52
Quoted 3 lines
arch/x86/kvm/vmx/main.c +2
-0+ } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);
All of the above code belongs in tdx_hardware_setup(), especially since you need tdp_mmu_enabled, not enable_ept.
Quoted 5 lines
arch/x86/kvm/vmx/main.c +0
-0if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,Signature · 1 line
2.25.1
Re: [PATCH v19 069/130] KVM: TDX: Require TDP MMU and mmio caching for TDX
Quoted 80 lines · Sean Christopherson
Quoted 17 lines · On Mon, Feb 26, 2024, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching. Disable TDX support when TDP MMU or mmio caching aren't supported.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/main.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)arch/x86/kvm/mmu/mmu.c +0
-0arch/x86/kvm/mmu/mmu.c module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644); · line 107
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +1
-0@@ -104,6 +104,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);* If the hardware supports that we don't need to do shadow paging. */ bool tdp_enabled = false; +EXPORT_SYMBOL_GPL (tdp_enabled);I haven't looked at the rest of the series, but this should be unnecessary. Just use
enable_ept. Ah, the code is wrong.Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0
-0static bool __ro_after_init tdp_mmu_allowed;arch/x86/kvm/vmx/main.c +1
-0@@ -3,6 +3,7 @@#include "x86_ops.h" #include "vmx.h" +#include "mmu.h" #include "nested.h" #include "pmu.h" #include "tdx.h" arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 40
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-0@@ -36,6 +37,18 @@ static __init int vt_hardware_setup(void)if (ret)return ret; +/* TDX requires KVM TDP MMU. */ This is a useless comment (assuming the code is correctly written), it's quite obvious from:
if (!tdp_mmu_enabled) enable_tdx = false;that the TDP MMU is required. Explaining *why* could be useful, but I'd probably just omit a comment. In the not too distant future, it will likely be common knowledge that the shadow MMU doesn't support newfangled features, and it _should_ be very easy for someone to get the info from the changelog.
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 41
Quoted 1 line
arch/x86/kvm/vmx/main.c +1
-0+if (enable_tdx && !tdp_enabled) {
tdp_enabledcan be true without the TDP MMU, you wanttdp_mmu_enabled.arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 43
Quoted 2 lines
arch/x86/kvm/vmx/main.c +2
-0+ enable_tdx = false; +pr_warn_ratelimited ("TDX requires TDP MMU. Please enable TDP MMU for TDX.\n" );Drop the
pr_warn(), TDX will presumably be on by default at some point, I don't want to get spam every time I test with TDP disabled.Also, ratelimiting this code is pointless (as is
_once()), it should only ever be called once per module module, and the limiting/once protections are tied to the module, i.e. effectively get reset when a module is reloaded.arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 49
Quoted 6 lines
arch/x86/kvm/vmx/main.c +6
-0+ } + +/* TDX requires MMIO caching. */ +if (enable_tdx && !enable_mmio_caching) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" );Same comments here.
Quoted 6 lines
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 52
Quoted 3 lines
arch/x86/kvm/vmx/main.c +2
-0+ } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);All of the above code belongs in
tdx_hardware_setup(), especially since you needtdp_mmu_enabled, notenable_ept.
Thanks for review. With tdp_mmu_enabled, removing warning and comments, I come up with the followings.
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -110,6 +110,7 @@ static bool __ro_after_init tdp_mmu_allowed;#ifdef CONFIG_X86_64 bool __read_mostly tdp_mmu_enabled = true;module_param_named (tdp_mmu, tdp_mmu_enabled, bool,0444 ); +EXPORT_SYMBOL_GPL (tdp_mmu_enabled);#endif static int max_huge_page_level__read_mostly ;
arch/x86/kvm/vmx/tdx.c +2 -7
@@ -1253,14 +1253,9 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)int r =0 ;int i; -if (!cpu_feature_enabled (X86_FEATURE_MOVDIR64B )) { -pr_warn ("MOVDIR64B is reqiured for TDX\n" ); +if (!tdp_mmu_enabled || !enable_mmio_caching || + !cpu_feature_enabled (X86_FEATURE_MOVDIR64B ))return -EOPNOTSUPP ; - } -if (!enable_ept) { -pr_warn ("Cannot enable TDX with EPT disabled\n" ); -return -EINVAL ; - } max_pkgs =topology_max_packages (); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock),
Signature · 4 lines
2.43.2 -- Isaku Yamahata <[email protected]>
PATCH v19 70/130 KVM: TDX: TDP MMU TDX support
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instruction
---
v19:
- Compile fix when CONFIG_HYPERV != y.
It's due to the following patch. Catch it up.
https://lore.kernel.org/all/[email protected]/
- Add comments on tlb shootdown to explan the sequence.
- Use gmem_max_level callback, delete tdp_max_page_level.
v18:
- rename tdx_sept_page_aug() -> tdx_mem_page_aug()
- checkpatch: space => tab
v15 -> v16:
- Add the handling of TD_ATTR_SEPT_VE_DISABLE case.
v14 -> v15:
- Implemented tdx_flush_tlb_current()
- Removed unnecessary invept in tdx_flush_tlb(). It was carry over from the very old code base.
--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 91 ++++++++- arch/x86/kvm/vmx/tdx.c | 372 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 6 + arch/x86/kvm/vmx/x86_ops.h | 13 ++ 6 files changed, 481 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/spte.c +2 -1
@@ -74,7 +74,8 @@ 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 (!vcpu->kvm->arch.shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_shared_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; spte |= vcpu->kvm->arch.shadow_mmio_value | access;
arch/x86/kvm/vmx/main.c +87 -4
132 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.c +372 -0
410 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +1 -1
@@ -18,6 +18,7 @@ struct kvm_tdx {int hkid;bool finalized; +atomic_t tdh_mem_track;u64 tsc_offset; };
@@ -162,7 +163,6 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fiel }return out.r8; } -#else struct kvm_tdx { struct kvm kvm;
arch/x86/kvm/vmx/tdx_ops.h +6 -0
@@ -44,6 +44,12 @@ static inline u64 tdx_seamcall(u64 op, struct tdx_module_args *in,void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out);#endif +static inline int pg_level_to_tdx_sept_level (enum pg_level level) +{ +WARN_ON_ONCE (level ==PG_LEVEL_NONE ); +return level -1 ; +} +/* * TDX module acquires its internal lock for resources. It doesn't spin to get * locks because of its restrictions of allowed execution time. Instead, it
arch/x86/kvm/vmx/x86_ops.h +13 -0
@@ -153,7 +153,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu); +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu); +int tdx_sept_flush_remote_tlbs (struct kvm *kvm);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int tdx_gmem_max_level (struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn, +bool is_private,u8 *max_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -176,7 +181,15 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {} +static inline void tdx_flush_tlb_current (struct kvm_vcpu *vcpu) {} +static inline int tdx_sept_flush_remote_tlbs (struct kvm *kvm) {return 0 ; }static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {} +static inline int tdx_gmem_max_level (struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn, +bool is_private,u8 *max_level) +{ +return -EOPNOTSUPP ; +}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 22 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +11
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + }
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 10 lines
arch/x86/kvm/vmx/main.c +5
-0+#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx )); +#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;
Is this hook necessary/beneficial to TDX?
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
struct kvm_x86_ops {
...
#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);
#endif
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 37 lines · Chao Gao
Quoted 22 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +11
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + }arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 10 lines
arch/x86/kvm/vmx/main.c +5
-0+#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx )); +#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;Is this hook necessary/beneficial to TDX?
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
Quoted 7 lines
struct
kvm_x86_ops{
...#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);#endif
Will fix it. I made mistake when I rebased it. Now those hooks are only for CONFIG_HPYERV.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
In reply to Chao GaoQuoted 29 lines · Chao Gao
Quoted 29 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +13
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } +#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx ));
Quoted 6 lines
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;Is this hook necessary/beneficial to TDX?
I think so.
We happended to encounter the following error and breaks the boot up:
"SEAMCALL (0x000000000000000f) failed: 0xc0000b0800000001" 0xc0000b0800000001 indicates the TDX_TLB_TRACKING_NOT_DONE, and it is caused by page demotion but not yet doing a tlb shotdown by tlb track.
It was found on my system the CONFIG_HYPERV is not set, and it makes kvm_arch_flush_remote_tlbs() not invoking tdx_track() before the tdh_mem_page_demote(), which caused the problem.
Quoted 9 lines
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
struct
kvm_x86_ops{
...#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);#endif
If the flush_remote_tlbs implementation are both available in HYPERV and TDX, does it make sense to remove the config checks? I thought when commit 0277022a77a5 was introduced, the only user of flush_remote_tlbs() is hyperv, and now there is TDX.
thanks,
Chenyu
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 59 lines · Chen Yu
Quoted 29 lines · Chao Gao
Quoted 29 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +13
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } +#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx ));Quoted 6 lines
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;Is this hook necessary/beneficial to TDX?
I think so.
We happended to encounter the following error and breaks the boot up:
"SEAMCALL (0x000000000000000f) failed:0xc0000b0800000001"0xc0000b0800000001indicates theTDX_TLB_TRACKING_NOT_DONE, and it is caused by page demotion but not yet doing a tlb shotdown by tlb track.It was found on my system the
CONFIG_HYPERVis not set, and it makeskvm_arch_flush_remote_tlbs()not invokingtdx_track()before thetdh_mem_page_demote(), which caused the problem.Quoted 9 lines
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
struct
kvm_x86_ops{
...#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);#endif
Quoted 4 lines
If the
flush_remote_tlbsimplementation are both available in HYPERV and TDX, does it make sense to remove the config checks? I thought when commit0277022a77a5was introduced, the only user offlush_remote_tlbs()is hyperv, and now there is TDX.
You don't like IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(CONFIG_TDX_HOST) in many places? Then, we can do something like the followings. Although It would be a bit ugly than the commit of 0277022a77a5, it's better to keep the intention of it.
#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(CONFIG_TDX_HOST)
# define KVM_X86_WANT_FLUSH_REMOTE_TLBS #endif
#if IS_DEFINED(KVM_X86_WANT_FLUSH_REMOTE_TLBS)
...
#endif
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 67 lines · Isaku Yamahata
Quoted 59 lines · Chen Yu
Quoted 29 lines · Chao Gao
Quoted 29 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +13
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } +#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx ));Quoted 6 lines
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;Is this hook necessary/beneficial to TDX?
I think so.
We happended to encounter the following error and breaks the boot up:
"SEAMCALL (0x000000000000000f) failed:0xc0000b0800000001"0xc0000b0800000001indicates theTDX_TLB_TRACKING_NOT_DONE, and it is caused by page demotion but not yet doing a tlb shotdown by tlb track.It was found on my system the
CONFIG_HYPERVis not set, and it makeskvm_arch_flush_remote_tlbs()not invokingtdx_track()before thetdh_mem_page_demote(), which caused the problem.Quoted 9 lines
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
struct
kvm_x86_ops{
...#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);#endif
Quoted 4 lines
If the
flush_remote_tlbsimplementation are both available in HYPERV and TDX, does it make sense to remove the config checks? I thought when commit0277022a77a5was introduced, the only user offlush_remote_tlbs()is hyperv, and now there is TDX.
Quoted 5 lines
You don't like
IS_ENABLED(CONFIG_HYPERV)||IS_ENABLED(CONFIG_TDX_HOST)in many places? Then, we can do something like the followings. Although It would be a bit ugly than the commit of0277022a77a5, it's better to keep the intention of it.
Ah, we have already __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE. We can use it.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 70 lines · Isaku Yamahata
Quoted 67 lines · Isaku Yamahata
Quoted 63 lines · Chen Yu
Quoted 29 lines · Chao Gao
Quoted 29 lines
arch/x86/kvm/vmx/main.c +4
-0+#if IS_ENABLED (CONFIG_HYPERV ) +static int vt_flush_remote_tlbs (struct kvm *kvm); +#endif +static
__initintvt_hardware_setup(void){ int ret;arch/x86/kvm/vmx/main.c +13
-0@@ -49,11 +53,29 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +#if IS_ENABLED (CONFIG_HYPERV ) +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } +#endif + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx ));Quoted 6 lines
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 76
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+#if IS_ENABLED (CONFIG_HYPERV ) +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;Is this hook necessary/beneficial to TDX?
I think so.
We happended to encounter the following error and breaks the boot up:
"SEAMCALL (0x000000000000000f) failed:0xc0000b0800000001"0xc0000b0800000001indicates theTDX_TLB_TRACKING_NOT_DONE, and it is caused by page demotion but not yet doing a tlb shotdown by tlb track.It was found on my system the
CONFIG_HYPERVis not set, and it makeskvm_arch_flush_remote_tlbs()not invokingtdx_track()before thetdh_mem_page_demote(), which caused the problem.Quoted 9 lines
if no, we can leave .flush_remote_tlbs as NULL. if yes, we should do:
struct
kvm_x86_ops{
...#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(TDX...) int (*flush_remote_tlbs)(struct kvm *kvm); int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, gfn_t nr_pages);#endif
If the
flush_remote_tlbsimplementation are both available in HYPERV and TDX, does it make sense to remove the config checks? I thought when commit0277022a77a5was introduced, the only user offlush_remote_tlbs()is hyperv, and now there is TDX.
Quoted 7 lines
Quoted 5 lines
You don't like
IS_ENABLED(CONFIG_HYPERV)||IS_ENABLED(CONFIG_TDX_HOST)in many places? Then, we can do something like the followings. Although It would be a bit ugly than the commit of0277022a77a5, it's better to keep the intention of it.Ah, we have already
__KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE. We can use it.
Yes, and there is also __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS so we can use ifdef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE when needed.
thanks,
Chenyu
Quoted 2 lines
Signature · 1 line
Isaku Yamahata <[email protected]>
Edgecombe, Rick P
maintainerRe: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 9 lines · Chen Yu
Quoted 9 lines
Quoted 8 lines
You don't like
IS_ENABLED(CONFIG_HYPERV)||IS_ENABLED(CONFIG_TDX_HOST)in many
places? Then, we can do something like the followings. Although It would be
a bit ugly than the commit of0277022a77a5, it's better to keep the intention
of it.
Quoted 4 lines
Quoted 1 line
Ah, we have already
__KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE. We can use it.Yes, and there is also
__KVM_HAVE_ARCH_FLUSH_REMOTE_TLBSso we can use ifdef__KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGEwhen needed.
In the current dev branch we already have:
#if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(CONFIG_INTEL_TDX_HOST) int (*flush_remote_tlbs)(struct kvm *kvm);
#endif
It was apparently reported by Vijay Dhanraj also.
0277022a77a5 has:
Declare the kvm_x86_ops hooks used to wire up paravirt TLB flushes when running under Hyper-V if and only if CONFIG_HYPERV!=n. Wrapping yet more code with IS_ENABLED(CONFIG_HYPERV) eliminates a handful of conditional branches, and makes it super obvious why the hooks *might* be valid.
So the bug should already be fixed, but if anyone has a strong style opinion please send a fixup. None of the options jump out to me as obviously better.
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
In reply to [email protected]Quoted 9 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Quoted 6 lines
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry.
But for private memory, zapping holds write lock, right?
Quoted 4 lines
It zaps the secure entry, increments TLB counter, sends
IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping.
exclude lock -> exclusive lock
How to understand this sentence?
Since it's holding exclusive lock, how it can be handled as concurrent zapping? Or you want to describe the current implementation prevents concurrent zapping?
Quoted 44 lines
Although it's inefficient,
it can be optimized in the future.For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v19:
- Compile fix whenCONFIG_HYPERV!= y.
It's due to the following patch. Catch it up.
https://lore.kernel.org/all/[email protected]/
- Add comments on tlb shootdown to explan the sequence.
- Usegmem_max_levelcallback, deletetdp_max_page_level.v18:
- renametdx_sept_page_aug()->tdx_mem_page_aug()
- checkpatch: space => tabv15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 91 ++++++++- arch/x86/kvm/vmx/tdx.c | 372 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 6 + arch/x86/kvm/vmx/x86_ops.h | 13 ++ 6 files changed, 481 insertions(+), 6 deletions(-)
[...]
Quoted 17 lines
arch/x86/kvm/vmx/tdx.c +17
-0+ +static int tdx_mem_page_aug (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +union tdx_sept_level_state level_state; +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_args out; +union tdx_sept_entry entry; +u64 err; + + err =tdh_mem_page_aug (kvm_tdx->tdr_pa, gpa, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) { +tdx_unpin (kvm, pfn); +return -EAGAIN ;
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 508
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ } +if (unlikely (err == (TDX_EPT_ENTRY_STATE_INCORRECT |TDX_OPERAND_ID_RCX ))) { + entry.raw = out.rcx; + level_state.raw = out.rdx; +if (level_state.level == tdx_level && + level_state.state ==TDX_SEPT_PENDING && + entry.leaf && entry.pfn == pfn && entry.sve) { +tdx_unpin (kvm, pfn); +WARN_ON_ONCE (!(to_kvm_tdx (kvm)->attributes & +TDX_TD_ATTR_SEPT_VE_DISABLE ));
to_kvm_tdx(kvm) -> kvm_tdx
Since the implementation requires attributes.TDX_TD_ATTR_SEPT_VE_DISABLE is set, should it check the value passed from userspace? And the reason should be described somewhere in changelog or/and comment.
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +13
-0+return -EAGAIN ; + } + } +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_AUG , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + } + +return 0 ; +} + +static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ enum pg_level level, kvm_pfn_t pfn) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* Because restricted mem
The term "restricted mem" is not used anymore, right? Should update the comment.
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 532
Quoted 2 lines
doesn't support page migration with
arch/x86/kvm/vmx/tdx.c +1
-0+* a_ops->migrate_page (yet), no callback isn't triggered for KVM on
no callback isn't -> no callback is
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 533
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+* page migration. Until restricted mem supports page migration,
"restricted mem" -> guest_mem
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 535
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+* prevent page migration. +* TODO: Once restricted mem introduces callback on page migration,
ditto
Quoted 71 lines
arch/x86/kvm/vmx/tdx.c +71
-0+* implement it and remove get_page/put_page(). +*/ +get_page (pfn_to_page (pfn)); + +if (likely (is_td_finalized (kvm_tdx))) +return tdx_mem_page_aug (kvm, gfn, level, pfn); + +/* TODO: tdh_mem_page_add() comes here for the initial memory. */ + +return 0 ; +} + +static int tdx_sept_drop_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_module_args out; +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =pfn_to_hpa (pfn); +hpa_t hpa_with_hkid; +u64 err; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +if (unlikely (!is_hkid_assigned (kvm_tdx))) { +/* +* The HKID assigned to this TD was already freed and cache +* was already flushed. We don't have to flush again. +*/ + err =tdx_reclaim_page (hpa); +if (KVM_BUG_ON (err, kvm)) +return -EIO ; +tdx_unpin (kvm, pfn); +return 0 ; + } + +do { +/* +* When zapping private page, write lock is held. So no race +* condition with other vcpu sept operation. Race only with +* TDH.VP.ENTER. +*/ + err =tdh_mem_page_remove (kvm_tdx->tdr_pa, gpa, tdx_level, &out); + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_REMOVE , err, &out); +return -EIO ; + } + + hpa_with_hkid =set_hkid_to_hpa (hpa, (u16 )kvm_tdx->hkid); +do { +/* +* TDX_OPERAND_BUSY can happen on locking PAMT entry. Because +* this page was removed above, other thread shouldn't be +* repeatedly operating on this page. Just retry loop. +*/ + err =tdh_phymem_page_wbinvd (hpa_with_hkid); + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_PHYMEM_PAGE_WBINVD , err,NULL ); +return -EIO ; + } +tdx_clear_page (hpa); +tdx_unpin (kvm, pfn); +return 0 ; +} + +static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 616
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ enum pg_level level, void *private_spt) +{ + int tdx_level = pg_level_to_tdx_sept_level(level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =__pa (private_spt); +struct tdx_module_args out; +u64 err; + + err =tdh_mem_sept_add (kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out);
kvm_tdx is only used here, can drop the local var.
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +10
-0+if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_SEPT_ADD , err, &out); +return -EIO ; + } + +return 0 ; +} +
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 12 lines · Binbin Wu
Quoted 9 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Quoted 8 lines
Quoted 6 lines
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry.
But for private memory, zapping holds write lock, right?
Right.
Quoted 5 lines
Quoted 4 lines
It zaps the secure entry, increments TLB counter, sends
IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping.
Quoted 7 lines
exclude lock -> exclusive lock
How to understand this sentence?
Since it's holding exclusive lock, how it can be handled as concurrent zapping?
Or you want to describe the current implementation prevents concurrent zapping?
The sentences is mixture of the currenct TDP MMU and how the new enhancement with this patch provides. Because this patch is TDX backend, let me drop the description about the TDP MMU part.
Propagate the entry change to Secure EPT. The possible entry changes are non-present -> present(population) and present -> non-present(zapping). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. On zapping, It blocks the Secure-EPT entry (clear present bit) , increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT.
Quoted 75 lines
Quoted 44 lines
Although it's inefficient,
it can be optimized in the future.For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v19:
- Compile fix whenCONFIG_HYPERV!= y.
It's due to the following patch. Catch it up.
https://lore.kernel.org/all/[email protected]/
- Add comments on tlb shootdown to explan the sequence.
- Usegmem_max_levelcallback, deletetdp_max_page_level.v18:
- renametdx_sept_page_aug()->tdx_mem_page_aug()
- checkpatch: space => tabv15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 91 ++++++++- arch/x86/kvm/vmx/tdx.c | 372 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 6 + arch/x86/kvm/vmx/x86_ops.h | 13 ++ 6 files changed, 481 insertions(+), 6 deletions(-)[...]
Quoted 17 lines
arch/x86/kvm/vmx/tdx.c +17
-0+ +static int tdx_mem_page_aug (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +union tdx_sept_level_state level_state; +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_args out; +union tdx_sept_entry entry; +u64 err; + + err =tdh_mem_page_aug (kvm_tdx->tdr_pa, gpa, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) { +tdx_unpin (kvm, pfn); +return -EAGAIN ;arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 508
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ } +if (unlikely (err == (TDX_EPT_ENTRY_STATE_INCORRECT |TDX_OPERAND_ID_RCX ))) { + entry.raw = out.rcx; + level_state.raw = out.rdx; +if (level_state.level == tdx_level && + level_state.state ==TDX_SEPT_PENDING && + entry.leaf && entry.pfn == pfn && entry.sve) { +tdx_unpin (kvm, pfn); +WARN_ON_ONCE (!(to_kvm_tdx (kvm)->attributes & +TDX_TD_ATTR_SEPT_VE_DISABLE ));
to_kvm_tdx(kvm)->kvm_tdx
Quoted 1 line
Since the implementation requires attributes.TDX_TD_ATTR_SEPT_VE_DISABLE is set,
TDX KVM allows either configuration. set or cleared.
Quoted 1 line
should it check the value passed from userspace?
It's user-space configurable value.
Quoted 1 line
And the reason should be described somewhere in changelog or/and comment.
This WARN_ON_ONCE() is a guard for buggy TDX module. It shouldn't return (TDX_EPT_ENTRY_STATE_INCORRECT | TDX_OPERAND_ID_RCX)) when SEPT_VE_DISABLED cleared. Maybe we should remove this WARN_ON_ONCE() because the TDX module is mature.
Quoted 18 lines
Quoted 18 lines
arch/x86/kvm/vmx/tdx.c +18
-0+return -EAGAIN ; + } + } +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_AUG , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + } + +return 0 ; +} + +static int tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* TODO: handle large pages. */
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* Because restricted mem The term "restricted mem" is not used anymore, right? Should update the comment.
Sure, will update it to guest_memfd.
Quoted 91 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 532
Quoted 3 lines
doesn't support page migration with
arch/x86/kvm/vmx/tdx.c +1
-0+* a_ops->migrate_page (yet), no callback isn't triggered for KVM on no callback isn't -> no callback is
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 533
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+* page migration. Until restricted mem supports page migration, "restricted mem" ->
guest_memarch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 535
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+* prevent page migration. +* TODO: Once restricted mem introduces callback on page migration, ditto
Quoted 75 lines
arch/x86/kvm/vmx/tdx.c +75
-0+* implement it and remove get_page/put_page(). +*/ +get_page (pfn_to_page (pfn)); + +if (likely (is_td_finalized (kvm_tdx))) +return tdx_mem_page_aug (kvm, gfn, level, pfn); + +/* TODO: tdh_mem_page_add() comes here for the initial memory. */ + +return 0 ; +} + +static int tdx_sept_drop_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_module_args out; +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =pfn_to_hpa (pfn); +hpa_t hpa_with_hkid; +u64 err; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +if (unlikely (!is_hkid_assigned (kvm_tdx))) { +/* +* The HKID assigned to this TD was already freed and cache +* was already flushed. We don't have to flush again. +*/ + err =tdx_reclaim_page (hpa); +if (KVM_BUG_ON (err, kvm)) +return -EIO ; +tdx_unpin (kvm, pfn); +return 0 ; + } + +do { +/* +* When zapping private page, write lock is held. So no race +* condition with other vcpu sept operation. Race only with +* TDH.VP.ENTER. +*/ + err =tdh_mem_page_remove (kvm_tdx->tdr_pa, gpa, tdx_level, &out); + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_REMOVE , err, &out); +return -EIO ; + } + + hpa_with_hkid =set_hkid_to_hpa (hpa, (u16 )kvm_tdx->hkid); +do { +/* +* TDX_OPERAND_BUSY can happen on locking PAMT entry. Because +* this page was removed above, other thread shouldn't be +* repeatedly operating on this page. Just retry loop. +*/ + err =tdh_phymem_page_wbinvd (hpa_with_hkid); + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_PHYMEM_PAGE_WBINVD , err,NULL ); +return -EIO ; + } +tdx_clear_page (hpa); +tdx_unpin (kvm, pfn); +return 0 ; +} + +static int tdx_sept_link_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 616
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =__pa (private_spt); +struct tdx_module_args out; +u64 err; + + err =tdh_mem_sept_add (kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out);
kvm_tdxis only used here, can drop the local var.
Will drop it.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
In reply to [email protected]Quoted 61 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v19:
- Compile fix whenCONFIG_HYPERV!= y.
It's due to the following patch. Catch it up.
https://lore.kernel.org/all/[email protected]/
- Add comments on tlb shootdown to explan the sequence.
- Usegmem_max_levelcallback, deletetdp_max_page_level.v18:
- renametdx_sept_page_aug()->tdx_mem_page_aug()
- checkpatch: space => tabv15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 91 ++++++++- arch/x86/kvm/vmx/tdx.c | 372 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 6 + arch/x86/kvm/vmx/x86_ops.h | 13 ++ 6 files changed, 481 insertions(+), 6 deletions(-)
[...]
Quoted 42 lines
arch/x86/kvm/vmx/tdx.c +42
-0+static int tdx_sept_zap_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn) &KVM_HPAGE_MASK (level); +struct tdx_module_args out; +u64 err; + +/* This can be called when destructing guest TD after freeing HKID. */ +if (unlikely (!is_hkid_assigned (kvm_tdx))) +return 0 ; + +/* For now large page isn't supported yet. */ +WARN_ON_ONCE (level !=PG_LEVEL_4K ); + err =tdh_mem_range_block (kvm_tdx->tdr_pa, gpa, tdx_level, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_RANGE_BLOCK , err, &out); +return -EIO ; + } +return 0 ; +} + +/* +* TLB shoot down procedure: +* There is a global epoch counter and each vcpu has local epoch counter. +* - TDH.MEM.RANGE.BLOCK(TDR. level, range) on one vcpu +* This blocks the subsequenct creation of TLB translation on that range. +* This corresponds to clear the present bit(all RXW) in EPT entry +* - TDH.MEM.TRACK(TDR): advances the epoch counter which is global. +* - IPI to remote vcpus +* - TDExit and re-entry with TDH.VP.ENTER on remote vcpus +* - On re-entry, TDX module compares the local epoch counter with the global +* epoch counter. If the local epoch counter is older than the global epoch +* counter, update the local epoch counter and flushes TLB. +*/ +static void tdx_track (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err;
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 678
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ +KVM_BUG_ON (!is_hkid_assigned (kvm_tdx), kvm); +/* If TD isn't finalized, it's before any vcpu running. */ +if (unlikely (!is_td_finalized (kvm_tdx))) +return ; + +/* +* tdx_flush_tlb() waits for this function to issue TDH.MEM.TRACK() by +* the counter. The counter is used instead of bool because multiple +* TDH_MEM_TRACK() can be issued concurrently by multiple vcpus.
Which case will have concurrent issues of TDH_MEM_TRACK() by multiple vcpus? For now, zapping is holding write lock.
Promotion/demotion may have concurrent issues of TDH_MEM_TRACK(), but it's not supported yet.
Quoted 56 lines
arch/x86/kvm/vmx/tdx.c +56
-0+* +* optimization: The TLB shoot down procedure described in The TDX +* specification is, TDH.MEM.TRACK(), send IPI to remote vcpus, confirm +* all remote vcpus exit to VMM, and execute vcpu, both local and +* remote. Twist the sequence to reduce IPI overhead as follows. +* +* local remote +* ----- ------ +* increment tdh_mem_track +* +* request KVM_REQ_TLB_FLUSH +* send IPI +* +* TDEXIT to KVM due to IPI +* +* IPI handler calls tdx_flush_tlb() +* to process KVM_REQ_TLB_FLUSH. +* spin wait for tdh_mem_track == 0 +* +* TDH.MEM.TRACK() +* +* decrement tdh_mem_track +* +* complete KVM_REQ_TLB_FLUSH +* +* TDH.VP.ENTER to flush tlbs TDH.VP.ENTER to flush tlbs +*/ +atomic_inc (&kvm_tdx->tdh_mem_track); +/* +* KVM_REQ_TLB_FLUSH waits for the empty IPI handler, ack_flush(), with +* KVM_REQUEST_WAIT. +*/ +kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH ); + +do { + err =tdh_mem_track (kvm_tdx->tdr_pa); + }while (unlikely ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_BUSY )); + +/* Release remote vcpu waiting for TDH.MEM.TRACK in tdx_flush_tlb(). */ +atomic_dec (&kvm_tdx->tdh_mem_track); + +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_TRACK , err,NULL ); + +} + +static int tdx_sept_free_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* +* The HKID assigned to this TD was already freed and cache was +* already flushed. We don't have to flush again. +*/ +if (!is_hkid_assigned (kvm_tdx))
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 744
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+return tdx_reclaim_page (__pa (private_spt)); + +/* +* free_private_spt() is (obviously) called when a shadow page is being +* zapped. KVM doesn't (yet) zap private SPs while the TD is active. +* Note: This function is for private shadow page. Not for private +* guest page. private guest page can be zapped during TD is active. +* shared <-> private conversion and slot move/deletion. +*/ +KVM_BUG_ON (is_hkid_assigned (kvm_tdx), kvm);
At this point, is_hkid_assigned(kvm_tdx) is always true.
Re: [PATCH v19 070/130] KVM: TDX: TDP MMU TDX support
Quoted 115 lines · Binbin Wu
Quoted 61 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v19:
- Compile fix whenCONFIG_HYPERV!= y.
It's due to the following patch. Catch it up.
https://lore.kernel.org/all/[email protected]/
- Add comments on tlb shootdown to explan the sequence.
- Usegmem_max_levelcallback, deletetdp_max_page_level.v18:
- renametdx_sept_page_aug()->tdx_mem_page_aug()
- checkpatch: space => tabv15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 91 ++++++++- arch/x86/kvm/vmx/tdx.c | 372 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 6 + arch/x86/kvm/vmx/x86_ops.h | 13 ++ 6 files changed, 481 insertions(+), 6 deletions(-)[...]
Quoted 49 lines
arch/x86/kvm/vmx/tdx.c +49
-0+static int tdx_sept_zap_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn) &KVM_HPAGE_MASK (level); +struct tdx_module_args out; +u64 err; + +/* This can be called when destructing guest TD after freeing HKID. */ +if (unlikely (!is_hkid_assigned (kvm_tdx))) +return 0 ; + +/* For now large page isn't supported yet. */ +WARN_ON_ONCE (level !=PG_LEVEL_4K ); + err =tdh_mem_range_block (kvm_tdx->tdr_pa, gpa, tdx_level, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_RANGE_BLOCK , err, &out); +return -EIO ; + } +return 0 ; +} + +/* +* TLB shoot down procedure: +* There is a global epoch counter and each vcpu has local epoch counter. +* - TDH.MEM.RANGE.BLOCK(TDR. level, range) on one vcpu +* This blocks the subsequenct creation of TLB translation on that range. +* This corresponds to clear the present bit(all RXW) in EPT entry +* - TDH.MEM.TRACK(TDR): advances the epoch counter which is global. +* - IPI to remote vcpus +* - TDExit and re-entry with TDH.VP.ENTER on remote vcpus +* - On re-entry, TDX module compares the local epoch counter with the global +* epoch counter. If the local epoch counter is older than the global epoch +* counter, update the local epoch counter and flushes TLB. +*/ +static void tdx_track (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +KVM_BUG_ON (!is_hkid_assigned (kvm_tdx), kvm); +/* If TD isn't finalized, it's before any vcpu running. */ +if (unlikely (!is_td_finalized (kvm_tdx))) +return ; + +/*
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 678
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* tdx_flush_tlb() waits for this function to issue TDH.MEM.TRACK() by +* the counter. The counter is used instead of bool because multiple +* TDH_MEM_TRACK() can be issued concurrently by multiple vcpus. Which case will have concurrent issues of
TDH_MEM_TRACK()by multiple vcpus? For now, zapping is holding write lock.
Promotion/demotion may have concurrent issues ofTDH_MEM_TRACK(), but it's not supported yet.
You're right. Large page support will use it. With the assumption of only single vcpu issuing tlb flush, The alternative is boolean + memory barrier. I prefer to keep atomic_t and drop this comment than boolean + memory barrier because we will eventually switch to atomic_t.
Quoted 60 lines
Quoted 60 lines
arch/x86/kvm/vmx/tdx.c +60
-0+* +* optimization: The TLB shoot down procedure described in The TDX +* specification is, TDH.MEM.TRACK(), send IPI to remote vcpus, confirm +* all remote vcpus exit to VMM, and execute vcpu, both local and +* remote. Twist the sequence to reduce IPI overhead as follows. +* +* local remote +* ----- ------ +* increment tdh_mem_track +* +* request KVM_REQ_TLB_FLUSH +* send IPI +* +* TDEXIT to KVM due to IPI +* +* IPI handler calls tdx_flush_tlb() +* to process KVM_REQ_TLB_FLUSH. +* spin wait for tdh_mem_track == 0 +* +* TDH.MEM.TRACK() +* +* decrement tdh_mem_track +* +* complete KVM_REQ_TLB_FLUSH +* +* TDH.VP.ENTER to flush tlbs TDH.VP.ENTER to flush tlbs +*/ +atomic_inc (&kvm_tdx->tdh_mem_track); +/* +* KVM_REQ_TLB_FLUSH waits for the empty IPI handler, ack_flush(), with +* KVM_REQUEST_WAIT. +*/ +kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH ); + +do { + err =tdh_mem_track (kvm_tdx->tdr_pa); + }while (unlikely ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_BUSY )); + +/* Release remote vcpu waiting for TDH.MEM.TRACK in tdx_flush_tlb(). */ +atomic_dec (&kvm_tdx->tdh_mem_track); + +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_TRACK , err,NULL ); + +} + +static int tdx_sept_free_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* +* The HKID assigned to this TD was already freed and cache was +* already flushed. We don't have to flush again. +*/ +if (!is_hkid_assigned (kvm_tdx)) +return tdx_reclaim_page (__pa (private_spt)); + +/* +* free_private_spt() is (obviously) called when a shadow page is being
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 744
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+* zapped. KVM doesn't (yet) zap private SPs while the TD is active. +* Note: This function is for private shadow page. Not for private +* guest page. private guest page can be zapped during TD is active. +* shared <-> private conversion and slot move/deletion. +*/ +KVM_BUG_ON (is_hkid_assigned (kvm_tdx), kvm);At this point,
is_hkid_assigned(kvm_tdx)is always true.
Yes, will drop this KVM_BUG_ON().
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 71/130 KVM: TDX: MTRR: implement get_mt_mask() for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because TDX virtualize cpuid[0x1].EDX[MTRR: bit 12] to fixed 1, guest TD thinks MTRR is supported. Although TDX supports only WB for private GPA, it's desirable to support MTRR for shared GPA. As guest access to MTRR MSRs causes #VE and KVM/x86 tracks the values of MTRR MSRs, the remaining part is to implement get_mt_mask method for TDX for shared GPA.
---
v19:
- typo in the commit message
- Deleted stale paragraph in the commit message
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 23 +++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -219,6 +219,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static u8 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_mt_mask (vcpu, gfn, is_mmio); + +return vmx_get_mt_mask (vcpu, gfn, is_mmio); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -348,7 +356,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr, - .get_mt_mask = vmx_get_mt_mask, + .get_mt_mask = vt_get_mt_mask, .get_exit_info = vmx_get_exit_info,
arch/x86/kvm/vmx/tdx.c +23 -0
@@ -393,6 +393,29 @@ int tdx_vm_init(struct kvm *kvm)return 0 ; } +u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) +{ +if (is_mmio) +return MTRR_TYPE_UNCACHABLE <<VMX_EPT_MT_EPTE_SHIFT ; + +if (!kvm_arch_has_noncoherent_dma (vcpu->kvm)) +return (MTRR_TYPE_WRBACK <<VMX_EPT_MT_EPTE_SHIFT ) |VMX_EPT_IPAT_BIT ; + +/* +* TDX enforces CR0.CD = 0 and KVM MTRR emulation enforces writeback. +* TODO: implement MTRR MSR emulation so that +* MTRRCap: SMRR=0: SMRR interface unsupported +* WC=0: write combining unsupported +* FIX=0: Fixed range registers unsupported +* VCNT=0: number of variable range regitsers = 0 +* MTRRDefType: E=1, FE=0, type=writeback only. Don't allow other value. +* E=1: enable MTRR +* FE=0: disable fixed range MTRRs +* type: default memory type=writeback +*/ +return MTRR_TYPE_WRBACK <<VMX_EPT_MT_EPTE_SHIFT ; +} +int tdx_vcpu_create (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -178,6 +179,7 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPstatic inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v19 72/130 The start of TDX KVM patch series: TD finalization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD finalization.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -11,6 +11,7 @@ What qemu can do - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. +- Qemu can populate initial guest memory image. Patch Layer status ------------------
@@ -20,8 +21,8 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Applying -* TD finalization: Not yet +* TDX EPT violation: Applied +* TD finalization: Applying * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet
Signature · 1 line
2.25.1
PATCH v19 73/130 KVM: x86: Add hooks in kvm_arch_vcpu_memory_mapping()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
In the case of TDX, the memory contents needs to be provided to be encrypted when populating guest memory before running the guest. Add hooks in kvm_mmu_map_tdp_page() for KVM_MEMORY_MAPPING before/after calling kvm_mmu_tdp_page(). TDX KVM will implement the hooks.
---
v19:
- newly added
--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 5 +++++ arch/x86/kvm/x86.c | 13 ++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -151,6 +151,8 @@ 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_max_level) +KVM_X86_OP_OPTIONAL(pre_memory_mapping); +KVM_X86_OP_OPTIONAL(post_memory_mapping); #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +5 -0
@@ -1839,6 +1839,11 @@ struct kvm_x86_ops {int (*gmem_max_level)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,bool is_private,u8 *max_level); +int (*pre_memory_mapping)(struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping, +u64 *error_code,u8 *max_level); +void (*post_memory_mapping)(struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +12 -1
@@ -5826,10 +5826,21 @@ int kvm_arch_vcpu_memory_mapping(struct kvm_vcpu *vcpu,u8 max_level =KVM_MAX_HUGEPAGE_LEVEL ;u64 error_code =PFERR_WRITE_MASK ;u8 goal_level =PG_LEVEL_4K ; -int r; +int r =0 ; + +if (kvm_x86_ops.pre_memory_mapping) + r =static_call (kvm_x86_pre_memory_mapping)(vcpu, mapping, &error_code, &max_level); +else { +if (mapping->source) + r = -EINVAL ; + } +if (r) +return r; r =kvm_mmu_map_tdp_page (vcpu,gfn_to_gpa (mapping->base_gfn), error_code, max_level, &goal_level); +if (kvm_x86_ops.post_memory_mapping) +static_call (kvm_x86_post_memory_mapping)(vcpu, mapping);if (r)return r;
Signature · 1 line
2.25.1
PATCH v19 74/130 KVM: TDX: Create initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, set_private_spte, handles it.
Implement the hooks for KVM_MEMORY_MAPPING, pre_memory_mapping() and post_memory_mapping() to handle it.
---
v19:
- Switched to use KVM_MEMORY_MAPPING
- Dropped measurement extension
- updated commit message. private_page_add() => set_private_spte()
--- arch/x86/kvm/vmx/main.c | 23 ++++++++++ arch/x86/kvm/vmx/tdx.c | 93 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 4 ++ arch/x86/kvm/vmx/x86_ops.h | 12 +++++ 4 files changed, 129 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/main.c +23 -0
@@ -252,6 +252,27 @@ static int vt_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn,return 0 ; } +static int vt_pre_memory_mapping (struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping, +u64 *error_code,u8 *max_level) +{ +if (is_td_vcpu (vcpu)) +return tdx_pre_memory_mapping (vcpu, mapping, error_code, max_level); + +if (mapping->source) +return -EINVAL ; +return 0 ; +} + +static void vt_post_memory_mapping (struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping) +{ +if (!is_td_vcpu (vcpu)) +return ; + +tdx_post_memory_mapping (vcpu, mapping); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -415,6 +436,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_mem_enc_ioctl = vt_vcpu_mem_enc_ioctl, .gmem_max_level = vt_gmem_max_level, + .pre_memory_mapping = vt_pre_memory_mapping, + .post_memory_mapping = vt_post_memory_mapping, };struct kvm_x86_init_ops vt_init_ops__initdata = {
arch/x86/kvm/vmx/tdx.c +90 -3
117 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -21,6 +21,10 @@ struct kvm_tdx {atomic_t tdh_mem_track;u64 tsc_offset; + +/* For KVM_MEMORY_MAPPING */ +struct mutex source_lock; +struct page *source_page; };struct vcpu_tdx {
arch/x86/kvm/vmx/x86_ops.h +12 -0
@@ -160,6 +160,11 @@ int tdx_sept_flush_remote_tlbs(struct kvm *kvm);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);int tdx_gmem_max_level (struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,bool is_private,u8 *max_level); +int tdx_pre_memory_mapping (struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping, +u64 *error_code,u8 *max_level); +void tdx_post_memory_mapping (struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -192,6 +197,13 @@ static inline int tdx_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, {return -EOPNOTSUPP ; } +int tdx_pre_memory_mapping (struct kvm_vcpu *vcpu, +struct kvm_memory_mapping *mapping, +u64 *error_code,u8 *max_level) +{ +return -EOPNOTSUPP ; +} +void tdx_post_memory_mapping (struct kvm_vcpu *vcpu,struct kvm_memory_mapping *mapping) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
PATCH v19 75/130 KVM: TDX: Extend memory measurement with initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX allows to extned memory measurement with the initial memory. Define new subcommand, KVM_TDX_EXTEND_MEMORY, of VM-scoped KVM_MEMORY_ENCRYPT_OP. it extends memory measurement of the TDX guest. The memory region must be populated with KVM_MEMORY_MAPPING command.
---
v19:
- newly added
- Split KVM_TDX_INIT_MEM_REGION into only extension function
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -572,6 +572,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_EXTEND_MEMORY , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +64 -0
76 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v19 76/130 KVM: TDX: Finalize VM initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run.
---
v18:
- Remove the change of tools/arch/x86/include/uapi/asm/kvm.h.
v14 -> v15:
- removed unconditional tdx_track() by tdx_flush_tlb_current() that does tdx_track().
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_EXTEND_MEMORY , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -1400,6 +1400,24 @@ static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_hkid_assigned (kvm_tdx) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;
@@ -1422,6 +1440,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_EXTEND_MEMORY : r =tdx_extend_memory (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.25.1
Adrian Hunter
reviewerRe: [PATCH v19 076/130] KVM: TDX: Finalize VM initialization
Quoted 10 lines · On 26/02/24 10:26, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand,
KVM_TDX_FINALIZE_VM, for VM-scopedKVM_MEMORY_ENCRYPT_OPto finalize the measurement and mark the TDX VM ready to run.
Perhaps a spruced up commit message would be:
<BEGIN>
Add a new VM-scoped KVM_MEMORY_ENCRYPT_OP IOCTL subcommand, KVM_TDX_FINALIZE_VM, to perform TD Measurement Finalization.
Documentation for the API is added in another patch:
"Documentation/virt/kvm: Document on Trust Domain Extensions(TDX)"
For the purpose of attestation, a measurement must be made of the TDX VM initial state. This is referred to as TD Measurement Finalization, and uses SEAMCALL TDH.MR.FINALIZE, after which:
1. The VMM adding TD private pages with arbitrary content is no longer allowed
2. The TDX VM is runnable
<END>
History:
This code is essentially unchanged from V1, as below. Except for V5, the code has never had any comments. Paolo's comment from then still appears unaddressed.
V19: Unchanged
V18: Undoes change of V17
V17: Also change tools/arch/x86/include/uapi/asm/kvm.h
V16: Unchanged
V15: Undoes change of V10
V11-V14: Unchanged
V10: Adds a hack (related to TDH_MEM_TRACK)
that was later removed in V15
V6-V9: Unchanged
V5 Broke out the code into a separate patch and
received its only comments, which were from Paolo:
"Reviewed-by: Paolo Bonzini <[email protected]> Note however that errors should be passed back in the struct."
This presumably refers to struct kvm_tdx_cmd which has an "error" member, but that is not updated by tdx_td_finalizemr()
V4 was a cut-down series and the code was not present V3 introduced WARN_ON_ONCE for the error condition V2 accommodated renaming the seamcall function and ID
Outstanding:
1. Address Paolo's comment about the error code
2. Is WARN_ON sensible?
Final note:
It might be possible to make TD Measurement Finalization transparent to the user space VMM and forego another API, but it seems doubtful that would really make anything much simpler.
Quoted 37 lines
Signed-off-by Isaku Yamahata <[email protected]>---
v18:
- Remove the change oftools/arch/x86/include/uapi/asm/kvm.h.v14 -> v15:
- removed unconditionaltdx_track()bytdx_flush_tlb_current()that doestdx_track().Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_EXTEND_MEMORY , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };arch/x86/kvm/vmx/tdx.c +0
-0@@ -1400,6 +1400,24 @@ static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; }
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1412
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_hkid_assigned (kvm_tdx) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) {
Is a failed SEAMCALL really something to WARN over?
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1413
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+pr_tdx_error (TDH_MR_FINALIZE , err,NULL );
As per Paolo, error code is not returned in struct kvm_tdx_cmd
Quoted 20 lines
arch/x86/kvm/vmx/tdx.c +10
-0+return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -1422,6 +1440,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_EXTEND_MEMORY : r =tdx_extend_memory (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
Re: [PATCH v19 076/130] KVM: TDX: Finalize VM initialization
Quoted 50 lines · Adrian Hunter
Quoted 10 lines · On 26/02/24 10:26, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand,
KVM_TDX_FINALIZE_VM, for VM-scopedKVM_MEMORY_ENCRYPT_OPto finalize the measurement and mark the TDX VM ready to run.Perhaps a spruced up commit message would be:
<BEGIN>
Add a new VM-scopedKVM_MEMORY_ENCRYPT_OPIOCTL subcommand,KVM_TDX_FINALIZE_VM, to perform TD Measurement Finalization.Documentation for the API is added in another patch:
"Documentation/virt/kvm: Document on Trust Domain Extensions(TDX)"For the purpose of attestation, a measurement must be made of the TDX VM initial state. This is referred to as TD Measurement Finalization, and uses SEAMCALL TDH.MR.FINALIZE, after which:
1. The VMM adding TD private pages with arbitrary content is no longer allowed
2. The TDX VM is runnable
<END>History:
This code is essentially unchanged from V1, as below. Except for V5, the code has never had any comments. Paolo's comment from then still appears unaddressed.
V19: Unchanged
V18: Undoes change of V17
V17: Also changetools/arch/x86/include/uapi/asm/kvm.h
V16: Unchanged
V15: Undoes change of V10
V11-V14: Unchanged
V10: Adds a hack (related toTDH_MEM_TRACK)
that was later removed in V15
V6-V9: Unchanged
V5 Broke out the code into a separate patch and
received its only comments, which were from Paolo:"Reviewed-by: Paolo Bonzini <[email protected]> Note however that errors should be passed back in the struct."
Quoted 6 lines
This presumably refers to struct
kvm_tdx_cmdwhich has an "error" member, but that is not updated bytdx_td_finalizemr()V4 was a cut-down series and the code was not present V3 introduced
WARN_ON_ONCEfor the error condition V2 accommodated renaming the seamcall function and ID
Thank you for creating histories. Let me update the commit message.
Quoted 4 lines
Outstanding:
1. Address Paolo's comment about the error code
2. IsWARN_ONsensible?
See below.
Quoted 43 lines
Final note:
It might be possible to make TD Measurement Finalization transparent to the user space VMM and forego another API, but it seems doubtful that would really make anything much simpler.
Quoted 37 lines
Signed-off-by Isaku Yamahata <[email protected]>---
v18:
- Remove the change oftools/arch/x86/include/uapi/asm/kvm.h.v14 -> v15:
- removed unconditionaltdx_track()bytdx_flush_tlb_current()that doestdx_track().Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_EXTEND_MEMORY , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };arch/x86/kvm/vmx/tdx.c +0
-0@@ -1400,6 +1400,24 @@ static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; }
Quoted 12 lines
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1412
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_hkid_assigned (kvm_tdx) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) {Is a failed SEAMCALL really something to WARN over?
Because user can trigger an error in some cases, we shouldn't WARN in such case. Except those, TDH.MR.FINALIZE() shouldn't return error. If we hit such error, it typically implies serious error so that the recovery is difficult. For example, the TDX module was broken by the host overwriting private pages. That's the reason why we have KVM_BUN_ON. So the error check should be something like
/* We can hit busy error to exclusively access TDR. */
if (err == (TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX))
return -EAGAIN;
/* User can call KVM_TDX_INIT_VM without any vCPUs created. */
if (err == TDX_NO_VCPUS)
return -EIO;
/* Other error shouldn't happen. */
if (KVM_BUG_ON(err, kvm)) {
pr_tdx_error(TDH_MR_FINALIZE, err);
return -EIO;
}Quoted 3 lines
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1413
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+pr_tdx_error (TDH_MR_FINALIZE , err,NULL );As per Paolo, error code is not returned in struct
kvm_tdx_cmd
It will be something like the followings. No compile test yet.
arch/x86/kvm/vmx/tdx.c +9 -0
@@ -2757,6 +2757,12 @@ static int tdx_td_finalizemr(struct kvm *kvm)return -EINVAL ; err =tdh_mr_finalize (kvm_tdx); + kvm_tdx->hw_error = err; + +if (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX )) +return -EAGAIN ; +if (err ==TDX_NO_VCPUS ) +return -EIO ;if (KVM_BUG_ON (err, kvm)) {pr_tdx_error (TDH_MR_FINALIZE , err);return -EIO ;
@@ -2768,6 +2774,7 @@ static int tdx_td_finalizemr(struct kvm *kvm)int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) { +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);struct kvm_tdx_cmd tdx_cmd;int r;
@@ -2777,6 +2784,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)return -EINVAL ;mutex_lock (&kvm->lock); + kvm_tdx->hw_error =0 ;switch (tdx_cmd.id) {case KVM_TDX_CAPABILITIES :
@@ -2793,6 +2801,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) goto out; } + tdx_cmd.error = kvm_tdx->hw_error;if (copy_to_user (argp, &tdx_cmd,sizeof (struct kvm_tdx_cmd ))) r = -EFAULT ;
arch/x86/kvm/vmx/tdx.h +3 -0
@@ -18,6 +18,9 @@ struct kvm_tdx {u64 xfam;int hkid; +/* For KVM_TDX ioctl to return SEAMCALL status code. */ +u64 hw_error; +/* * Used on each TD-exit, see tdx_user_return_update_cache(). * TSX_CTRL value on TD exit
arch/x86/kvm/vmx/tdx_errno.h +1 -0
@@ -41,6 +41,7 @@#define TDX_TD_FATAL 0xC000060400000000ULL#define TDX_TD_NON_DEBUG 0xC000060500000000ULL#define TDX_LIFECYCLE_STATE_INCORRECT 0xC000060700000000ULL +#define TDX_NO_VCPUS 0xC000060900000000ULL#define TDX_TDCX_NUM_INCORRECT 0xC000061000000000ULL#define TDX_VCPU_STATE_INCORRECT 0xC000070000000000ULL#define TDX_VCPU_ASSOCIATED 0x8000070100000000ULL
Signature · 3 lines
2.43.2 -- Isaku Yamahata <[email protected]>
Adrian Hunter
reviewerRe: [PATCH v19 076/130] KVM: TDX: Finalize VM initialization
Quoted 161 lines · Isaku Yamahata
Quoted 50 lines · Adrian Hunter
Quoted 10 lines · On 26/02/24 10:26, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand,
KVM_TDX_FINALIZE_VM, for VM-scopedKVM_MEMORY_ENCRYPT_OPto finalize the measurement and mark the TDX VM ready to run.Perhaps a spruced up commit message would be:
<BEGIN>
Add a new VM-scopedKVM_MEMORY_ENCRYPT_OPIOCTL subcommand,KVM_TDX_FINALIZE_VM, to perform TD Measurement Finalization.Documentation for the API is added in another patch:
"Documentation/virt/kvm: Document on Trust Domain Extensions(TDX)"For the purpose of attestation, a measurement must be made of the TDX VM initial state. This is referred to as TD Measurement Finalization, and uses SEAMCALL TDH.MR.FINALIZE, after which:
1. The VMM adding TD private pages with arbitrary content is no longer allowed
2. The TDX VM is runnable
<END>History:
This code is essentially unchanged from V1, as below. Except for V5, the code has never had any comments. Paolo's comment from then still appears unaddressed.
V19: Unchanged
V18: Undoes change of V17
V17: Also changetools/arch/x86/include/uapi/asm/kvm.h
V16: Unchanged
V15: Undoes change of V10
V11-V14: Unchanged
V10: Adds a hack (related toTDH_MEM_TRACK)
that was later removed in V15
V6-V9: Unchanged
V5 Broke out the code into a separate patch and
received its only comments, which were from Paolo:"Reviewed-by: Paolo Bonzini <[email protected]> Note however that errors should be passed back in the struct."
Quoted 6 lines
This presumably refers to struct
kvm_tdx_cmdwhich has an "error" member, but that is not updated bytdx_td_finalizemr()V4 was a cut-down series and the code was not present V3 introduced
WARN_ON_ONCEfor the error condition V2 accommodated renaming the seamcall function and IDThank you for creating histories. Let me update the commit message.
Quoted 4 lines
Outstanding:
1. Address Paolo's comment about the error code
2. IsWARN_ONsensible?See below.
Quoted 43 lines
Final note:
It might be possible to make TD Measurement Finalization transparent to the user space VMM and forego another API, but it seems doubtful that would really make anything much simpler.
Quoted 37 lines
Signed-off-by Isaku Yamahata <[email protected]>---
v18:
- Remove the change oftools/arch/x86/include/uapi/asm/kvm.h.v14 -> v15:
- removed unconditionaltdx_track()bytdx_flush_tlb_current()that doestdx_track().Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_EXTEND_MEMORY , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };arch/x86/kvm/vmx/tdx.c +0
-0@@ -1400,6 +1400,24 @@ static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; }Quoted 12 lines
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1412
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_hkid_assigned (kvm_tdx) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) {Is a failed SEAMCALL really something to WARN over?
Because user can trigger an error in some cases, we shouldn't WARN in such case. Except those,
TDH.MR.FINALIZE()shouldn't return error. If we hit such error, it typically implies serious error so that the recovery is difficult. For example, the TDX module was broken by the host overwriting private pages. That's the reason why we haveKVM_BUN_ON. So the error check should be something like/* We can hit busy error to exclusively access TDR. */ if (err == (TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX)) return -EAGAIN; /* User can call KVM_TDX_INIT_VM without any vCPUs created. */ if (err == TDX_NO_VCPUS) return -EIO; /* Other error shouldn't happen. */ if (KVM_BUG_ON(err, kvm)) { pr_tdx_error(TDH_MR_FINALIZE, err); return -EIO; }Quoted 3 lines
arch/x86/kvm/vmx/tdx.c static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 1413
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+pr_tdx_error (TDH_MR_FINALIZE , err,NULL );As per Paolo, error code is not returned in struct
kvm_tdx_cmdIt will be something like the followings. No compile test yet.
arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c static int tdx_td_finalizemr(struct kvm *kvm) · line 2762
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -2757,6 +2757,12 @@ static int tdx_td_finalizemr(struct kvm *kvm)return -EINVAL ; err =tdh_mr_finalize (kvm_tdx); + kvm_tdx->hw_error = err; + +if (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))
There seem to be also implicit operand codes. How sure are we that TDX_OPERAND_ID_RCX is the only valid busy operand?
arch/x86/kvm/vmx/tdx.c static int tdx_td_finalizemr(struct kvm *kvm) · line 2764
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+return -EAGAIN ; +if (err ==TDX_NO_VCPUS )
TDX_NO_VCPUS is not one of the completion status codes for TDH.MR.FINALIZE
Quoted 33 lines
arch/x86/kvm/vmx/tdx.c +4
-0+return -EIO ;if (KVM_BUG_ON (err, kvm)) {pr_tdx_error (TDH_MR_FINALIZE , err);return -EIO ;@@ -2768,6 +2774,7 @@ static int tdx_td_finalizemr(struct kvm *kvm)int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) { +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);struct kvm_tdx_cmd tdx_cmd;int r;@@ -2777,6 +2784,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)return -EINVAL ;mutex_lock (&kvm->lock); + kvm_tdx->hw_error =0 ;switch (tdx_cmd.id) {case KVM_TDX_CAPABILITIES :@@ -2793,6 +2801,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) goto out; } + tdx_cmd.error = kvm_tdx->hw_error;if (copy_to_user (argp, &tdx_cmd,sizeof (struct kvm_tdx_cmd ))) r = -EFAULT ;arch/x86/kvm/vmx/tdx.h +0
-0
arch/x86/kvm/vmx/tdx.h struct kvm_tdx { · line 22
Quoted 5 lines
arch/x86/kvm/vmx/tdx.h +2
-0@@ -18,6 +18,9 @@ struct kvm_tdx {u64 xfam;int hkid; +/* For KVM_TDX ioctl to return SEAMCALL status code. */ +u64 hw_error;
For this case, it seems weird to have a struct member to pass back a return status code, why not make it a parameter of tdx_td_finalizemr() or pass &tdx_cmd?
Quoted 16 lines
arch/x86/kvm/vmx/tdx.h +1
-0+/* * Used on each TD-exit, see tdx_user_return_update_cache(). * TSX_CTRL value on TD exit arch/x86/kvm/vmx/tdx_errno.h +1
-0@@ -41,6 +41,7 @@#define TDX_TD_FATAL 0xC000060400000000ULL#define TDX_TD_NON_DEBUG 0xC000060500000000ULL#define TDX_LIFECYCLE_STATE_INCORRECT 0xC000060700000000ULL +#define TDX_NO_VCPUS 0xC000060900000000ULL#define TDX_TDCX_NUM_INCORRECT 0xC000061000000000ULL#define TDX_VCPU_STATE_INCORRECT 0xC000070000000000ULL#define TDX_VCPU_ASSOCIATED 0x8000070100000000ULL
Re: [PATCH v19 076/130] KVM: TDX: Finalize VM initialization
Quoted 7 lines · Adrian Hunter
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +0
-0@@ -2757,6 +2757,12 @@ static int tdx_td_finalizemr(struct kvm *kvm)return -EINVAL ;
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static int tdx_td_finalizemr(struct kvm *kvm) · line 2762
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +3
-0err =tdh_mr_finalize (kvm_tdx); + kvm_tdx->hw_error = err; + +if (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))There seem to be also implicit operand codes. How sure are we that
TDX_OPERAND_ID_RCXis the only valid busy operand?
According to the description of TDH.MR.FINALIZE, it locks exclusively, RCX in TDR, TDCS as implicit, OP_STATE as implicit. And the basic TDX feature to run guest TD, TDX module locks in order of TDR => OP_STATE. We won't see OP_STATE lock failure after gaining TDR lock.
If you worry for future, we can code it as
(err & TDX_SEAMCALL_STATUS_MASK) == TDX_OPERAND_BUSY. We should do it consistently, though.
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c static int tdx_td_finalizemr(struct kvm *kvm) · line 2764
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+return -EAGAIN ; +if (err ==TDX_NO_VCPUS )
TDX_NO_VCPUSis not one of the completion status codes for TDH.MR.FINALIZE
It depends on the document version. Need to check TDX_OP_STATE_INCORRECT to be defensive.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/tdx.h +0
-0@@ -18,6 +18,9 @@ struct kvm_tdx {u64 xfam;int hkid;
Quoted 6 lines
arch/x86/kvm/vmx/tdx.h struct kvm_tdx { · line 22
Quoted 2 lines
arch/x86/kvm/vmx/tdx.h +2
-0+/* For KVM_TDX ioctl to return SEAMCALL status code. */ +u64 hw_error;For this case, it seems weird to have a struct member to pass back a return status code, why not make it a parameter of
tdx_td_finalizemr()or pass&tdx_cmd?
I created the patch too quick. Given KVM_TDX_CAPABILITIES and KVM_TDX_INIT_VM take tdx_cmd already, it's consistent to make tdx_td_finalize() take it.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 77/130 The start of TDX KVM patch series: TD vcpu enter/exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu enter/exit.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -12,6 +12,7 @@ What qemu can do - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. +- Qemu can finalize guest TD. Patch Layer status ------------------
@@ -22,8 +23,8 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Applying -* TD vcpu enter/exit: Not yet +* TD finalization: Applied +* TD vcpu enter/exit: Applying * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v19 78/130 KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on sched_in event as TDX doesn't support pause loop.
TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export perf_restore_debug_store.
---
v19:
- Removed export_symbol_gpl(host_xcr0) to the patch that uses it
Changes v15 -> v16:
- use __seamcall_saved_ret()
- As struct tdx_module_args doesn't match with vcpu.arch.regs, copy regs before/after calling __seamcall_saved_ret().
--- arch/x86/kvm/vmx/main.c | 21 +++++++++- arch/x86/kvm/vmx/tdx.c | 84 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 + 4 files changed, 138 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +19 -2
@@ -158,6 +158,23 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +/* Unconditionally continue to vcpu_run(). */ +return 1 ; + +return vmx_vcpu_pre_run (vcpu); +} + +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -343,8 +360,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .flush_tlb_gva = vt_flush_tlb_gva, .flush_tlb_guest = vt_flush_tlb_guest, - .vcpu_pre_run = vmx_vcpu_pre_run, - .vcpu_run = vmx_vcpu_run, + .vcpu_pre_run = vt_vcpu_pre_run, + .vcpu_run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +84 -0
96 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +33 -0
@@ -27,6 +27,37 @@ struct kvm_tdx {struct page *source_page; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;
@@ -34,6 +65,8 @@ struct vcpu_tdx {unsigned long *tdvpx_pa;bool td_vcpu_created; +union tdx_exit_reason exit_reason; +bool initialized;/*
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -184,6 +185,7 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPstatic inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Sean Christopherson
maintainerRe: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Review
Quoted 7 lines · On Mon, Feb 26, 2024, [email protected] wrote:
(quoted diff) +7
-0+static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) +{ + struct tdx_module_args args; + + /* + * Avoid section mismatch with to_tdx() with KVM_VM_BUG(). The caller + * should call to_tdx().
C'mon. I don't think it's unreasonable to expect that at least one of the many people working on TDX would figure out why to_vmx() is __always_inline.
Quoted 4 lines
(quoted diff) +4
-0+ */ + struct kvm_vcpu *vcpu = &tdx->vcpu; + + guest_state_enter_irqoff();
Review
Quoted 10 lines
(quoted diff) +10
-0+ + /* + * TODO: optimization: + * - Eliminate copy between args and vcpu->arch.regs. + * - copyin/copyout registers only if (tdx->tdvmvall.regs_mask != 0) + * which means TDG.VP.VMCALL. + */ + args = (struct tdx_module_args) { + .rcx = tdx->tdvpr_pa, +#define REG(reg, REG) .reg = vcpu->arch.regs[VCPU_REGS_ ## REG]
Organizing tdx_module_args's registers by volatile vs. non-volatile is asinine. This code should not need to exist.
Quoted 35 lines
(quoted diff) +27
-0+ WARN_ON_ONCE(!kvm_rebooting && + (tdx->exit_reason.full & TDX_SW_ERROR) == TDX_SW_ERROR); + + guest_state_exit_irqoff(); +} + +fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) +{ + struct vcpu_tdx *tdx = to_tdx(vcpu); + + if (unlikely(!tdx->initialized)) + return -EINVAL; + if (unlikely(vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full = TDX_NON_RECOVERABLE_VCPU; + return EXIT_FASTPATH_NONE; + } + + trace_kvm_entry(vcpu); + + tdx_vcpu_enter_exit(tdx); + + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET; + trace_kvm_exit(vcpu, KVM_ISA_VMX); + + return EXIT_FASTPATH_NONE; +} + void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) { WARN_ON_ONCE(root_hpa & ~PAGE_MASK);arch/x86/kvm/vmx/tdx.h +0
-0
arch/x86/kvm/vmx/tdx.h struct kvm_tdx { · line 32
Quoted 6 lines
arch/x86/kvm/vmx/tdx.h +3
-0@@ -27,6 +27,37 @@ struct kvm_tdx {struct page *source_page; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */
Then use "union vmx_exit_reason", having to maintain duplicate copies of the same union is not something I want to do.
I'm honestly not even convinced that "union tdx_exit_reason" needs to exist. I added vmx_exit_reason because we kept having bugs where KVM would fail to strip bits 31:16, and because nested VMX needs to stuff failed_vmentry, but I don't see a similar need for TDX.
I would even go so far as to say the vcpu_tdx field shouldn't be exit_reason, and instead should be "return_code" or something. E.g. if the TDX module refuses to run the vCPU, there's no VM-Enter and thus no VM-Exit (unless you count the SEAMCALL itself, har har). Ditto for #GP or #UD on the SEAMCALL (or any other reason that generates TDX_SW_ERROR).
Ugh, I'm doubling down on that suggesting. This:
WARN_ON_ONCE(!kvm_rebooting &&
(tdx->vp_enter_ret & TDX_SW_ERROR) == TDX_SW_ERROR);
if ((u16)tdx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI &&
is_nmi(tdexit_intr_info(vcpu))) {
kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
vmx_do_nmi_irqoff();
kvm_after_interrupt(vcpu);
}is heinous. If there's an error that leaves bits 15:0 zero, KVM will synthesize a spurious NMI. I don't know whether or not that can happen, but it's not something that should even be possible in KVM, i.e. the exit reason should be processed if and only if KVM *knows* there was a sane VM-Exit from non-root mode.
tdx_vcpu_run() has a similar issue, though it's probably benign. If there's an error in bits 15:0 that happens to collide with EXIT_REASON_TDCALL, weird things will happen.
if (tdx->exit_reason.basic == EXIT_REASON_TDCALL) tdx->tdvmcall.rcx = vcpu->arch.regs[VCPU_REGS_RCX]; else tdx->tdvmcall.rcx = 0;
I vote for something like the below, with much more robust checking of vp_enter_ret before it's converted to a VMX exit reason.
static __always_inline union vmx_exit_reason tdexit_exit_reason(struct kvm_vcpu *vcpu)
{
return (u32)vcpu->vp_enter_ret;
}arch/x86/kvm/vmx/tdx.h +2 -32
@@ -43,37 +43,6 @@ struct kvm_tdx {struct page *source_page; }; -union tdx_exit_reason { -struct { -/* 31:0 mirror the VMX Exit Reason format */ -u64 basic :16 ; -u64 reserved16 :1 ; -u64 reserved17 :1 ; -u64 reserved18 :1 ; -u64 reserved19 :1 ; -u64 reserved20 :1 ; -u64 reserved21 :1 ; -u64 reserved22 :1 ; -u64 reserved23 :1 ; -u64 reserved24 :1 ; -u64 reserved25 :1 ; -u64 bus_lock_detected :1 ; -u64 enclave_mode :1 ; -u64 smi_pending_mtf :1 ; -u64 smi_from_vmx_root :1 ; -u64 reserved30 :1 ; -u64 failed_vmentry :1 ; - -/* 63:32 are TDX specific */ -u64 details_l1 :8 ; -u64 class :8 ; -u64 reserved61_48 :14 ; -u64 non_recoverable :1 ; -u64 error :1 ; - }; -u64 full; -}; -struct vcpu_tdx {struct kvm_vcpu vcpu;
@@ -103,7 +72,8 @@ struct vcpu_tdx { };u64 rcx; } tdvmcall; -union tdx_exit_reason exit_reason; + +u64 vp_enter_ret;bool initialized;
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 52 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.h struct kvm_tdx { · line 32
Quoted 11 lines
arch/x86/kvm/vmx/tdx.h +3
-0@@ -27,6 +27,37 @@ struct kvm_tdx {struct page *source_page; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ Then use "union
vmx_exit_reason", having to maintain duplicate copies of the same union is not something I want to do.I'm honestly not even convinced that "union
tdx_exit_reason" needs to exist. I addedvmx_exit_reasonbecause we kept having bugs where KVM would fail to strip bits 31:16, and because nested VMX needs to stufffailed_vmentry, but I don't see a similar need for TDX.I would even go so far as to say the
vcpu_tdxfield shouldn't beexit_reason, and instead should be "return_code" or something. E.g. if the TDX module refuses to run the vCPU, there's no VM-Enter and thus no VM-Exit (unless you count the SEAMCALL itself, har har). Ditto for #GP or #UD on the SEAMCALL (or any other reason that generatesTDX_SW_ERROR).Ugh, I'm doubling down on that suggesting. This:
WARN_ON_ONCE(!kvm_rebooting && (tdx->vp_enter_ret & TDX_SW_ERROR) == TDX_SW_ERROR); if ((u16)tdx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI && is_nmi(tdexit_intr_info(vcpu))) { kvm_before_interrupt(vcpu, KVM_HANDLING_NMI); vmx_do_nmi_irqoff(); kvm_after_interrupt(vcpu); }is heinous. If there's an error that leaves bits 15:0 zero, KVM will synthesize a spurious NMI. I don't know whether or not that can happen, but it's not something that should even be possible in KVM, i.e. the exit reason should be processed if and only if KVM *knows* there was a sane VM-Exit from non-root mode.
tdx_vcpu_run()has a similar issue, though it's probably benign. If there's an error in bits 15:0 that happens to collide withEXIT_REASON_TDCALL, weird things will happen.if (tdx->exit_reason.basic == EXIT_REASON_TDCALL) tdx->tdvmcall.rcx = vcpu->arch.regs[VCPU_REGS_RCX]; else tdx->tdvmcall.rcx = 0;
Quoted 7 lines
I vote for something like the below, with much more robust checking of
vp_enter_retbefore it's converted to a VMX exit reason.static
__always_inlineunionvmx_exit_reasontdexit_exit_reason(struct kvm_vcpu *vcpu){ return (u32)vcpu->vp_enter_ret; }
Thank you for the concrete suggestion. Let me explore what safe guard check can be done to make exit path robust.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
In reply to Sean ChristophersonQuoted 17 lines · Sean Christopherson
Quoted 4 lines · On Mon, Feb 26, 2024, [email protected] wrote:
(quoted diff) +4
-0+ */ + struct kvm_vcpu *vcpu = &tdx->vcpu; + + guest_state_enter_irqoff();Review
Quoted 10 lines
(quoted diff) +10
-0+ + /* + * TODO: optimization: + * - Eliminate copy between args and vcpu->arch.regs. + * - copyin/copyout registers only if (tdx->tdvmvall.regs_mask != 0) + * which means TDG.VP.VMCALL. + */ + args = (struct tdx_module_args) { + .rcx = tdx->tdvpr_pa, +#define REG(reg, REG) .reg = vcpu->arch.regs[VCPU_REGS_ ## REG]Organizing tdx_module_args's registers by volatile vs. non-volatile is asinine. This code should not need to exist.
Did you suggest to align the tdx_module_args with enum kvm_reg for GP registers, so it can be done by a simple mem copy?
Quoted 8 lines
Quoted 8 lines
(quoted diff) +6
-0+ WARN_ON_ONCE(!kvm_rebooting && + (tdx->exit_reason.full & TDX_SW_ERROR) == TDX_SW_ERROR); + + guest_state_exit_irqoff(); +} +
Edgecombe, Rick P
maintainerRe: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 567
Quoted 11 lines · On Mon, 2024-02-26 at 00:26 -0800, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +11
-0+fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ + struct vcpu_tdx *tdx = to_tdx(vcpu); + + if (unlikely(!tdx->initialized)) + return -EINVAL; + if (unlikely(vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full = TDX_NON_RECOVERABLE_VCPU; + return EXIT_FASTPATH_NONE; + } +
Isaku, can you elaborate on why this needs special handling? There is a check in vcpu_enter_guest() like:
if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) {
r = -EIO;
goto out;
}Instead it returns a SEAM error code for something actuated by KVM. But can it even be reached because of the other check? Not sure if there is a problem, just sticks out to me and wondering whats going on.
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 15 lines · Rick P
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 567
Quoted 11 lines · On Mon, 2024-02-26 at 00:26 -0800, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +11
-0+fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ + struct vcpu_tdx *tdx = to_tdx(vcpu); + + if (unlikely(!tdx->initialized)) + return -EINVAL; + if (unlikely(vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full = TDX_NON_RECOVERABLE_VCPU; + return EXIT_FASTPATH_NONE; + } +Isaku, can you elaborate on why this needs special handling? There is a check in
vcpu_enter_guest()like:
Quoted 8 lines
if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) { r = -EIO; goto out; }Instead it returns a SEAM error code for something actuated by KVM. But can it even be reached because of the other check? Not sure if there is a problem, just sticks out to me and wondering whats going on.
The original intention is to get out the inner loop. As Sean pointed it out, the current code does poor job to check error of __seamcall_saved_ret(TDH_VP_ENTER). So it fails to call KVM_BUG_ON() when it returns unexcepted error.
The right fix is to properly check an error from TDH_VP_ENTER and call KVM_BUG_ON(). Then the check you pointed out should go away.
for // out loop
if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) {
for // inner loop
vcpu_run()
kvm_vcpu_exit_request(vcpu).Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]Quoted 6 lines · On Mon, Feb 26, 2024 at 12:26:20AM -0800, [email protected] wrote:
(quoted diff) +2
-0@@ -491,6 +494,87 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) +{
...
Review
Quoted 1 line
(quoted diff) +1
-0+ tdx->exit_reason.full = __seamcall_saved_ret(TDH_VP_ENTER, &args);
Call to __seamcall_saved_ret() leaves noinstr section.
__seamcall_saved_ret() has to be moved:
arch/x86/virt/vmx/tdx/seamcall.S +2 -0
@@ -44,6 +44,8 @@ SYM_FUNC_START(__seamcall_ret) SYM_FUNC_END(__seamcall_ret) EXPORT_SYMBOL_GPL(__seamcall_ret); +.section .noinstr.text, "ax" + /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software * (the P-SEAMLDR or the TDX module), with saving output registers to the
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 21 lines · Kirill A. Shutemov
Quoted 6 lines · On Mon, Feb 26, 2024 at 12:26:20AM -0800, [email protected] wrote:
(quoted diff) +2
-0@@ -491,6 +494,87 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) +{...
Review
Quoted 1 line
(quoted diff) +1
-0+ tdx->exit_reason.full = __seamcall_saved_ret(TDH_VP_ENTER, &args);Call to
__seamcall_saved_ret()leaves noinstr section.
__seamcall_saved_ret()has to be moved:arch/x86/virt/vmx/tdx/seamcall.S +0
-0
arch/x86/virt/vmx/tdx/seamcall.S SYM_FUNC_START(__seamcall_ret) · line 51
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamcall.S +2
-0@@ -44,6 +44,8 @@ SYM_FUNC_START(__seamcall_ret) SYM_FUNC_END(__seamcall_ret) EXPORT_SYMBOL_GPL(__seamcall_ret); +.section .noinstr.text, "ax" + /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software * (the P-SEAMLDR or the TDX module), with saving output registers to the
Alternatively, I think we can explicitly use instrumentation_begin()/end() around __seamcall_saved_ret() here.
__seamcall_saved_ret() could be used in the future for new SEAMCALLs (e.g., TDH.MEM.IMPORT) for TDX guest live migration. And for that I don't think the caller(s) is/are tagged with noinstr.
Sean Christopherson
maintainerRe: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 25 lines · Kai Huang
Quoted 24 lines · Kirill A. Shutemov
Quoted 6 lines · On Mon, Feb 26, 2024 at 12:26:20AM -0800, [email protected] wrote:
(quoted diff) +2
-0@@ -491,6 +494,87 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) +{...
Review
Quoted 1 line
(quoted diff) +1
-0+ tdx->exit_reason.full = __seamcall_saved_ret(TDH_VP_ENTER, &args);Call to
__seamcall_saved_ret()leaves noinstr section.
__seamcall_saved_ret()has to be moved:arch/x86/virt/vmx/tdx/seamcall.S +0
-0@@ -44,6 +44,8 @@ SYM_FUNC_START(__seamcall_ret) SYM_FUNC_END(__seamcall_ret) EXPORT_SYMBOL_GPL(__seamcall_ret);
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamcall.S SYM_FUNC_START(__seamcall_ret) · line 51
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamcall.S +2
-0+.section .noinstr.text, "ax" + /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software * (the P-SEAMLDR or the TDX module), with saving output registers to theAlternatively, I think we can explicitly use instrumentation_begin()/end() around
__seamcall_saved_ret()here.
No, that will just paper over the complaint. Dang it, I was going to say that I called out earlier that tdx_vcpu_enter_exit() doesn't need to be noinstr, but it looks like my brain and fingers didn't connect.
So I'll say it now :-)
I don't think tdx_vcpu_enter_exit() needs to be noinstr, because the SEAMCALL is functionally a VM-Exit, and so all host state is saved/restored "atomically" across the SEAMCALL (some by hardware, some by software (TDX-module)).
The reason the VM-Enter flows for VMX and SVM need to be noinstr is they do things like load the guest's CR2, and handle NMI VM-Exits with NMIs blocks. None of that applies to TDX. Either that, or there are some massive bugs lurking due to missing code.
Re: [PATCH v19 078/130] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 41 lines · Sean Christopherson
Quoted 25 lines · Kai Huang
Quoted 24 lines · Kirill A. Shutemov
Quoted 6 lines · On Mon, Feb 26, 2024 at 12:26:20AM -0800, [email protected] wrote:
(quoted diff) +2
-0@@ -491,6 +494,87 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) +{...
Review
Quoted 1 line
(quoted diff) +1
-0+ tdx->exit_reason.full = __seamcall_saved_ret(TDH_VP_ENTER, &args);Call to
__seamcall_saved_ret()leaves noinstr section.
__seamcall_saved_ret()has to be moved:arch/x86/virt/vmx/tdx/seamcall.S +0
-0@@ -44,6 +44,8 @@ SYM_FUNC_START(__seamcall_ret) SYM_FUNC_END(__seamcall_ret) EXPORT_SYMBOL_GPL(__seamcall_ret);Quoted 8 lines
arch/x86/virt/vmx/tdx/seamcall.S SYM_FUNC_START(__seamcall_ret) · line 51
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamcall.S +2
-0+.section .noinstr.text, "ax" + /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software * (the P-SEAMLDR or the TDX module), with saving output registers to theAlternatively, I think we can explicitly use instrumentation_begin()/end() around
__seamcall_saved_ret()here.No, that will just paper over the complaint. Dang it, I was going to say that I called out earlier that
tdx_vcpu_enter_exit()doesn't need to be noinstr, but it looks like my brain and fingers didn't connect.So I'll say it now :-)
Quoted 8 lines
I don't think
tdx_vcpu_enter_exit()needs to be noinstr, because the SEAMCALL is functionally a VM-Exit, and so all host state is saved/restored "atomically" across the SEAMCALL (some by hardware, some by software (TDX-module)).The reason the VM-Enter flows for VMX and SVM need to be noinstr is they do things like load the guest's CR2, and handle NMI VM-Exits with NMIs blocks. None of that applies to TDX. Either that, or there are some massive bugs lurking due to missing code.
Ah right. That's even better :-)
Thanks for jumping in and pointing out!
PATCH v19 79/130 KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
--- arch/x86/kvm/vmx/main.c | 30 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 42 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 78 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +28 -2
@@ -158,6 +158,32 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_put (vcpu); +return ; + } + +vmx_vcpu_put (vcpu); +} +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -326,9 +352,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_switch_to_guest = vmx_prepare_switch_to_guest, + .prepare_switch_to_guest = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,
arch/x86/kvm/vmx/tdx.c +42 -0
68 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -69,6 +69,10 @@ struct vcpu_tdx {bool initialized; +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; +/* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work.
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -151,6 +151,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -186,6 +188,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [PATCH v19 079/130] KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case.
Could you add more descriptions about the differences?
Quoted 1 line
Add TDX hooks to save/restore host/guest CPU state.
KVM doesn't save/restore guest CPU state for TDX.
Quoted 16 lines
Save/restore kernel GS base MSR.
Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/main.c | 30 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 42 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 78 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/main.c +0
-0
arch/x86/kvm/vmx/main.c static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 164
Quoted 7 lines
arch/x86/kvm/vmx/main.c +4
-0@@ -158,6 +158,32 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET,
It sounds confusing to me.
If all host states are saved/restored across SEAMCALL/SEAMRET, why this patch saves/restores MSR_KERNEL_GS_BASE for host?
Quoted 58 lines
arch/x86/kvm/vmx/main.c +22
-0and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_put (vcpu); +return ; + } + +vmx_vcpu_put (vcpu); +} +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) {if (
is_td_vcpu(vcpu))arch/x86/kvm/vmx/main.c +2
-2@@ -326,9 +352,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_switch_to_guest = vmx_prepare_switch_to_guest, + .prepare_switch_to_guest = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,arch/x86/kvm/vmx/tdx.c +2
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h> @@ -423,6 +424,7 @@ u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)int tdx_vcpu_create (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); +struct vcpu_tdx *tdx =to_tdx (vcpu);WARN_ON_ONCE (vcpu->arch.cpuid_entries);WARN_ON_ONCE (vcpu->arch.cpuid_nent);
arch/x86/kvm/vmx/tdx.c int tdx_vcpu_create(struct kvm_vcpu *vcpu) · line 457
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-0@@ -446,9 +448,47 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)if ((kvm_tdx->xfam &XFEATURE_MASK_XTILE ) ==XFEATURE_MASK_XTILE ) vcpu->arch.xfd_no_write_intercept = true; + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu)
Just like vmx_prepare_switch_to_host(), the input can be "struct vcpu_tdx *", since vcpu is not used inside the function. And the callsites just use "to_tdx(vcpu)"
arch/x86/kvm/vmx/tdx.c int tdx_vcpu_create(struct kvm_vcpu *vcpu) · line 459
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+{ +struct vcpu_tdx *tdx =to_tdx (vcpu);
Then, this can be dropped.
arch/x86/kvm/vmx/tdx.c int tdx_vcpu_create(struct kvm_vcpu *vcpu) · line 472
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +13
-0+ +if (!tdx->host_state_need_save) +return ; + +if (likely (is_64bit_mm (current->mm))) + tdx->msr_host_kernel_gs_base = current->thread.gsbase; +else + tdx->msr_host_kernel_gs_base =read_msr (MSR_KERNEL_GS_BASE ); + + tdx->host_state_need_save = false; +} + +static void tdx_prepare_switch_to_host (struct kvm_vcpu *vcpu)
ditto
Quoted 68 lines
arch/x86/kvm/vmx/tdx.c +21
-0+{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + tdx->host_state_need_save = true; +if (!tdx->host_state_need_restore) +return ; + + ++vcpu->stat.host_state_reload; + +wrmsrl (MSR_KERNEL_GS_BASE , tdx->msr_host_kernel_gs_base); + tdx->host_state_need_restore = false; +} + +void tdx_vcpu_put (struct kvm_vcpu *vcpu) +{ +vmx_vcpu_pi_put (vcpu); +tdx_prepare_switch_to_host (vcpu); +} +void tdx_vcpu_free (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -569,6 +609,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); + tdx->host_state_need_restore = true; + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX );arch/x86/kvm/vmx/tdx.h +4
-0@@ -69,6 +69,10 @@ struct vcpu_tdx {bool initialized; +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; +/* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work. arch/x86/kvm/vmx/x86_ops.h +4
-0@@ -151,6 +151,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -186,6 +188,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Re: [PATCH v19 079/130] KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
Quoted 7 lines · Binbin Wu
Quoted 7 lines
arch/x86/kvm/vmx/main.c +0
-0@@ -158,6 +158,32 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); }
Quoted 9 lines
arch/x86/kvm/vmx/main.c static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Quoted 4 lines
arch/x86/kvm/vmx/main.c +4
-0+static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, It sounds confusing to me.
If all host states are saved/restored across SEAMCALL/SEAMRET, why this patch saves/restoresMSR_KERNEL_GS_BASEfor host?
No. Probably we should update the comment. Something like
restored => restored or initialized to reset state.
Except conditionally saved/restored MSRs (e.g., perfrmon, debugreg), IA32_START, IA32_LSTART, MSR_SYSCALL_MASK, IA32_TSC_AUX and TA32_KERNEL_GS_BASE are reset to initial state. uret handles the first four. The kernel_gs_base needs to be restored on TDExit.
Quoted 66 lines
Quoted 53 lines
arch/x86/kvm/vmx/main.c +24
-2and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_put (vcpu); +return ; + } + +vmx_vcpu_put (vcpu); +} +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -326,9 +352,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_switch_to_guest = vmx_prepare_switch_to_guest, + .prepare_switch_to_guest = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,arch/x86/kvm/vmx/tdx.c +2
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h> @@ -423,6 +424,7 @@ u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)int tdx_vcpu_create (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); +struct vcpu_tdx *tdx =to_tdx (vcpu);WARN_ON_ONCE (vcpu->arch.cpuid_entries);WARN_ON_ONCE (vcpu->arch.cpuid_nent);arch/x86/kvm/vmx/tdx.c int tdx_vcpu_create(struct kvm_vcpu *vcpu) · line 455
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +4
-0@@ -446,9 +448,47 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)if ((kvm_tdx->xfam &XFEATURE_MASK_XTILE ) ==XFEATURE_MASK_XTILE ) vcpu->arch.xfd_no_write_intercept = true; + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu)Just like
vmx_prepare_switch_to_host(), the input can be "structvcpu_tdx*", since vcpu is not used inside the function.
And the callsites just use "to_tdx(vcpu)"
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c int tdx_vcpu_create(struct kvm_vcpu *vcpu) · line 457
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+{ +struct vcpu_tdx *tdx =to_tdx (vcpu);Then, this can be dropped.
prepare_switch_to_guest() is used for kvm_x86_ops.prepare_switch_to_guest(). kvm_x86_ops consistently takes struct kvm_vcpu.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 079/130] KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
Quoted 10 lines · Isaku Yamahata
Quoted 8 lines
Quoted 1 line
+void
tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)Just like
vmx_prepare_switch_to_host(), the input can be "structvcpu_tdx*", since vcpu is not used inside the function.
And the callsites just use "to_tdx(vcpu)"Review
Quoted 2 lines
(quoted diff) +2
-0+{ + struct vcpu_tdx *tdx = to_tdx(vcpu);Then, this can be dropped.
prepare_switch_to_guest()is used forkvm_x86_ops.prepare_switch_to_guest().kvm_x86_opsconsistently takes structkvm_vcpu.
Oh yes, it's not suitable for tdx_prepare_switch_to_guest(). Still, it can be for tdx_prepare_switch_to_host().
PATCH v19 80/130 KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
---
v19:
- Add EXPORT_SYMBOL_GPL(host_xcr0)
v15 -> v16:
- Added CET flag mask
--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)
arch/x86/kvm/vmx/tdx.c +19 -0
@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h"
@@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} + static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) {struct tdx_module_args args;
@@ -609,6 +627,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;
arch/x86/kvm/x86.c +1 -0
@@ -315,6 +315,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);static struct kmem_cache *x86_emulator_cache;
Signature · 1 line
2.25.1
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
Quoted 41 lines · On 2024-02-26 at 00:26:22 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +7
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0))
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 553
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +}
Maybe one minor question regarding the pkru restore. In the non-TDX version kvm_load_host_xsave_state(), it first tries to read the current setting vcpu->arch.pkru = rdpkru(); if this setting does not equal to host_pkru, it trigger the write_pkru on host. Does it mean we can also leverage that mechanism in TDX to avoid 1 pkru write(I guess pkru write is costly than a read pkru)?
thanks,
Chenyu
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
Quoted 50 lines · Chen Yu
Quoted 49 lines · On 2024-02-26 at 00:26:22 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +15
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU ))
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 553
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+write_pkru (vcpu->arch.host_pkru); +}Maybe one minor question regarding the pkru restore. In the non-TDX version
kvm_load_host_xsave_state(), it first tries to read the current settingvcpu->arch.pkru=rdpkru(); if this setting does not equal tohost_pkru, it trigger thewrite_pkruon host. Does it mean we can also leverage that mechanism in TDX to avoid 1 pkru write(I guess pkru write is costly than a read pkru)?
Yes, that's the intention. When we set the PKRU feature for the guest, TDX module unconditionally initialize pkru. Do you have use case that wrpkru() (without rdpkru()) is better?
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
Quoted 62 lines · Isaku Yamahata
Quoted 50 lines · Chen Yu
Quoted 49 lines · On 2024-02-26 at 00:26:22 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +15
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU ))Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 553
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+write_pkru (vcpu->arch.host_pkru); +}Maybe one minor question regarding the pkru restore. In the non-TDX version
kvm_load_host_xsave_state(), it first tries to read the current settingvcpu->arch.pkru=rdpkru(); if this setting does not equal tohost_pkru, it trigger thewrite_pkruon host. Does it mean we can also leverage that mechanism in TDX to avoid 1 pkru write(I guess pkru write is costly than a read pkru)?
Quoted 2 lines
Yes, that's the intention. When we set the PKRU feature for the guest, TDX module unconditionally initialize pkru.
I see, thanks for the information. Please correct me if I'm wrong, and I'm not sure if wrpkru instruction would trigger the TD exit. The TDX module spec[1] mentioned PKS (protected key for supervisor pages), but does not metion PKU for user pages. PKS is controlled by MSR IA32_PKRS. The TDX module will passthrough the MSR IA32_PKRS write in TD, because TDX module clears the PKS bitmap in VMCS:
https://github.com/intel/tdx-module/blob/tdx_1.5/src/common/helpers/helpers.c#L1723
so neither write to MSR IA32_PKRS nor wrpkru triggers TD exit.
However, after a second thought, I found that after commit 72a6c08c44e4, the current code should not be a problem, because write_pkru() would first read the current pkru settings and decide whether to update to the pkru register.
Quoted 2 lines
Do you have use case that
wrpkru()
(withoutrdpkru()) is better?
I don't have use case yet. But with/without rdpkru() in tdx_restore_host_xsave_state(), there is no much difference because write_pkru() has taken care of it if I understand correctly.
thanks,
Chenyu
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
Quoted 66 lines · Chen Yu
Quoted 62 lines · Isaku Yamahata
Quoted 50 lines · Chen Yu
Quoted 49 lines · On 2024-02-26 at 00:26:22 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +15
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU ))Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 553
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+write_pkru (vcpu->arch.host_pkru); +}Maybe one minor question regarding the pkru restore. In the non-TDX version
kvm_load_host_xsave_state(), it first tries to read the current settingvcpu->arch.pkru=rdpkru(); if this setting does not equal tohost_pkru, it trigger thewrite_pkruon host. Does it mean we can also leverage that mechanism in TDX to avoid 1 pkru write(I guess pkru write is costly than a read pkru)?Quoted 2 lines
Yes, that's the intention. When we set the PKRU feature for the guest, TDX module unconditionally initialize pkru.
Quoted 7 lines
I see, thanks for the information. Please correct me if I'm wrong, and I'm not sure if wrpkru instruction would trigger the TD exit. The TDX module spec[1] mentioned PKS (protected key for supervisor pages), but does not metion PKU for user pages. PKS is controlled by MSR
IA32_PKRS. The TDX module will passthrough the MSRIA32_PKRSwrite in TD, because TDX module clears the PKS bitmap in VMCS:
https://github.com/intel/tdx-module/blob/tdx_1.5/src/common/helpers/helpers.c#L1723
so neither write to MSRIA32_PKRSnor wrpkru triggers TD exit.
wrpkru instruction in TDX guest doesn't cause exit to TDX module. TDX module runs with CR4.PKE=0. The value of pkru doesn't matter to the TDX module. When exiting from TDX module to the host VMM, PKRU is initialized to zero with xrestr. So it doesn't matter.
We need to refer to NP-SEAMLDR for the register value for TDX module on SEAMCALL. It sets up the register values for TDX module on SEAMCALL.
Quoted 10 lines
However, after a second thought, I found that after commit
72a6c08c44e4, the current code should not be a problem, becausewrite_pkru()would first read the current pkru settings and decide whether to update to the pkru register.Quoted 2 lines
Do you have use case that
wrpkru()
(withoutrdpkru()) is better?I don't have use case yet. But with/without
rdpkru()intdx_restore_host_xsave_state(), there is no much difference becausewrite_pkru()has taken care of it if I understand correctly.
The code in this hunk is TDX version of kvm_load_guest_xsave_state(). We case follow the VMX case at the moment.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]Quoted 34 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +1
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ }
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 546
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +9
-0+static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); + if (static_cpu_has(X86_FEATURE_XSAVES) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */
The comment needs to be updated to reflect the case for CET.
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 548
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss | XFEATURE_MASK_PT | TDX_TD_XFAM_CET)))
For TDX_TD_XFAM_CET, maybe no need to make it TDX specific?
BTW, the definitions for XFEATURE_MASK_CET_USER/XFEATURE_MASK_CET_KERNEL have been merged.
https://lore.kernel.org/all/20230613001108.3040476-25-rick.p.edgecombe%40intel.com
You can resolve the TODO in https://lore.kernel.org/kvm/5eca97e6a3978cf4dcf1cff21be6ec8b639a66b9.1708933498.git.isaku.yamahata@intel.com/
Quoted 29 lines
arch/x86/kvm/vmx/tdx.c +7
-0+wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} + static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) {struct tdx_module_args args;@@ -609,6 +627,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;arch/x86/kvm/x86.c +1
-0@@ -315,6 +315,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);static struct kmem_cache *x86_emulator_cache;
Re: [PATCH v19 080/130] KVM: TDX: restore host xsave state when exit from the guest TD
Quoted 51 lines · Binbin Wu
Quoted 31 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- AddEXPORT_SYMBOL_GPL(host_xcr0)v15 -> v16:
- Added CET flag maskSigned-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 2 files changed, 20 insertions(+)arch/x86/kvm/vmx/tdx.c +1
-0@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h" @@ -534,6 +535,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 545
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +9
-0} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); + if (static_cpu_has(X86_FEATURE_XSAVES) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ The comment needs to be updated to reflect the case for CET.
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 548
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss | XFEATURE_MASK_PT | TDX_TD_XFAM_CET)))For
TDX_TD_XFAM_CET, maybe no need to make it TDX specific?
Quoted 4 lines
BTW, the definitions for XFEATURE_MASK_CET_USER/XFEATURE_MASK_CET_KERNEL
have been merged.
https://lore.kernel.org/all/20230613001108.3040476-25-rick.p.edgecombe%40intel.com
You can resolve the TODO in https://lore.kernel.org/kvm/5eca97e6a3978cf4dcf1cff21be6ec8b639a66b9.1708933498.git.isaku.yamahata@intel.com/
Yes, will update those constants to use the one in arch/x86/include/asm/fpu/types.h
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 81/130 KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.
TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -2259,6 +2259,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {
arch/x86/kvm/x86.c +20 -5
@@ -440,6 +440,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();
@@ -454,15 +463,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.25.1
Re: [PATCH v19 081/130] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
Quoted 10 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses
kvm_set_user_return_msr()to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in
kvm_user_return_msrsare
^ extra "are"
Quoted 52 lines
inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead
kvm_on_user_return()to skip restoring some MSRs to the host's values.kvm_set_user_return_msr()can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation ofkvm_set_user_return_msr()to update cached values and skip that wrmsr.Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -2259,6 +2259,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {arch/x86/kvm/x86.c +9
-1@@ -440,6 +440,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();@@ -454,15 +463,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) {
arch/x86/kvm/x86.c int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) · line 471
Quoted 10 lines
arch/x86/kvm/x86.c +2
-4- msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */
Not sure this comment is necessary, since the code is simple.
arch/x86/kvm/x86.c int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) · line 472
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+void kvm_user_return_update_cache (unsigned int slot,u64 value)
As a public API, is it better to use "kvm_user_return_msr_update_cache" instead of "kvm_user_return_update_cache"? Although it makes the API name longer...
Quoted 11 lines
arch/x86/kvm/x86.c +8
-0+{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Re: [PATCH v19 081/130] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
Quoted 25 lines · Binbin Wu
Quoted 25 lines
arch/x86/kvm/x86.c +9
-3@@ -440,6 +440,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();@@ -454,15 +463,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn);
Quoted 8 lines
arch/x86/kvm/x86.c int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) · line 471
Quoted 7 lines
arch/x86/kvm/x86.c +2
-2- msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ Not sure this comment is necessary, since the code is simple.
Ok, let's remove it.
Quoted 5 lines
arch/x86/kvm/x86.c int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) · line 472
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+void kvm_user_return_update_cache (unsigned int slot,u64 value)As a public API, is it better to use "
kvm_user_return_msr_update_cache" instead of "kvm_user_return_update_cache"?
Although it makes the API name longer...
Yes, other functions consistently user user_return_msr. We should do so.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 82/130 KVM: TDX: restore user ret MSRs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3. Because TSX_CTRL requires special treat, this patch doesn't address it.
--- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
arch/x86/kvm/vmx/tdx.c +43 -0
@@ -535,6 +535,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK , .defval =0x20200 }, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
@@ -627,6 +649,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1972,6 +1995,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { +/* +* Here it checks if MSRs (tdx_uret_msrs) can be saved/restored +* before returning to user space. +* +* this_cpu_ptr(user_return_msrs)->registered isn't checked +* because the registration is done at vcpu runtime by +* kvm_set_user_return_msr(). +* Here is setting up cpu feature before running vcpu, +* registered is already false. +*/ + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Signature · 1 line
2.25.1
Re: [PATCH v19 082/130] KVM: TDX: restore user ret MSRs
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit
Here "Restore" is not accurate, since the previous patch just updates the cached value on TD exit.
Quoted 52 lines
and before returning to ring 3. Because
TSX_CTRLrequires special treat, this patch doesn't address it.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)arch/x86/kvm/vmx/tdx.c +23
-0@@ -535,6 +535,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK , .defval =0x20200 }, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);@@ -627,6 +649,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;@@ -1972,6 +1995,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ;
arch/x86/kvm/vmx/tdx.c int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) · line 2005
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0} +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { +/* +* Here it checks if MSRs (tdx_uret_msrs) can be saved/restored +* before returning to user space. +* +* this_cpu_ptr(user_return_msrs)->registered isn't checked +* because the registration is done at vcpu runtime by +* kvm_set_user_return_msr().
Should be tdx_user_return_update_cache(), if it's the final API name.
arch/x86/kvm/vmx/tdx.c int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) · line 2007
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+* Here is setting up cpu feature before running vcpu, +* registered is already false.
^
remove "already"?
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +10
-0+*/ + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Re: [PATCH v19 082/130] KVM: TDX: restore user ret MSRs
Quoted 10 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit
Here "Restore" is not accurate, since the previous patch just updates the cached value on TD exit.
Sure, let me update it.
Quoted 50 lines
Quoted 50 lines
and before returning to ring 3. Because
TSX_CTRLrequires special treat, this patch doesn't address it.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)arch/x86/kvm/vmx/tdx.c +24
-0@@ -535,6 +535,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK , .defval =0x20200 }, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);@@ -627,6 +649,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;@@ -1972,6 +1995,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) {
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) · line 2005
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+/* +* Here it checks if MSRs (tdx_uret_msrs) can be saved/restored +* before returning to user space. +* +* this_cpu_ptr(user_return_msrs)->registered isn't checked +* because the registration is done at vcpu runtime by +* kvm_set_user_return_msr(). Should be
tdx_user_return_update_cache(), if it's the final API name.
Yes, it will be tdx_user_reutrn_msr_update_cache().
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) · line 2007
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+* Here is setting up cpu feature before running vcpu, +* registered is already false. ^
remove "already"?
We can this sentence.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 83/130 KVM: TDX: Add TSX_CTRL msr into uret_msrs list
In reply to [email protected]From: Yang Weijiang <[email protected]>
TDX module resets the TSX_CTRL MSR to 0 at TD exit if TSX is enabled for TD. Or it preserves the TSX_CTRL MSR if TSX is disabled for TD. VMM can rely on uret_msrs mechanism to defer the reload of host value until exiting to user space.
---
v19:
- fix the type of tdx_uret_tsx_ctrl_slot. unguent int => int.
--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +31 -2
@@ -547,14 +547,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static int tdx_uret_tsx_ctrl_slot; -static void tdx_user_return_update_cache (void ) +static void tdx_user_return_update_cache (struct kvm_vcpu *vcpu) {int i;for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++)kvm_user_return_update_cache (tdx_uret_msrs[i].slot, tdx_uret_msrs[i].defval); +/* +* TSX_CTRL is reset to 0 if guest TSX is supported. Otherwise +* preserved. +*/ +if (to_kvm_tdx (vcpu->kvm)->tsx_supported && tdx_uret_tsx_ctrl_slot !=-1 ) +kvm_user_return_update_cache (tdx_uret_tsx_ctrl_slot,0 ); }static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu)
@@ -649,7 +656,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); -tdx_user_return_update_cache (); +tdx_user_return_update_cache (vcpu);tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1167,6 +1174,22 @@ static int setup_tdparams_xfam(struct kvm_cpuid2 *cpuid, struct td_params *td_pareturn 0 ; } +static bool tdparams_tsx_supported (struct kvm_cpuid2 *cpuid) +{ +const struct kvm_cpuid_entry2 *entry; +u64 mask; +u32 ebx; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0x7 ,0 ); +if (entry) + ebx = entry->ebx; +else + ebx =0 ; + + mask =__feature_bit (X86_FEATURE_HLE ) |__feature_bit (X86_FEATURE_RTM ); +return ebx & mask; +} +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params,struct kvm_tdx_init_vm *init_vm) {
@@ -1209,6 +1232,7 @@ static int setup_tdparams(struct kvm *kvm, struct td_params *td_params,MEMCPY_SAME_SIZE (td_params->mrowner, init_vm->mrowner);MEMCPY_SAME_SIZE (td_params->mrownerconfig, init_vm->mrownerconfig); +to_kvm_tdx (kvm)->tsx_supported =tdparams_tsx_supported (cpuid);return 0 ; }
@@ -2014,6 +2038,11 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EIO ; } } + tdx_uret_tsx_ctrl_slot =kvm_find_user_return_msr (MSR_IA32_TSX_CTRL ); +if (tdx_uret_tsx_ctrl_slot ==-1 &&boot_cpu_has (X86_FEATURE_MSR_TSX_CTRL )) { +pr_err ("MSR_IA32_TSX_CTRL isn't included by kvm_find_user_return_msr\n" ); +return -EIO ; + } max_pkgs =topology_max_packages (); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock),
arch/x86/kvm/vmx/tdx.h +8 -0
@@ -17,6 +17,14 @@ struct kvm_tdx {u64 xfam;int hkid; +/* +* Used on each TD-exit, see tdx_user_return_update_cache(). +* TSX_CTRL value on TD exit +* - set 0 if guest TSX enabled +* - preserved if guest TSX disabled +*/ +bool tsx_supported; +bool finalized;atomic_t tdh_mem_track;
Signature · 1 line
2.25.1
Re: [PATCH v19 083/130] KVM: TDX: Add TSX_CTRL msr into uret_msrs list
Quoted 30 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Yang Weijiang <[email protected]>
TDX module resets the
TSX_CTRLMSR to 0 at TD exit if TSX is enabled for TD. Or it preserves theTSX_CTRLMSR if TSX is disabled for TD. VMM can rely onuret_msrsmechanism to defer the reload of host value until exiting to user space.Signed-off-by Yang Weijiang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- fix the type oftdx_uret_tsx_ctrl_slot. unguent int => int.--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-1@@ -547,14 +547,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static int tdx_uret_tsx_ctrl_slot; -static void tdx_user_return_update_cache (void ) +static void tdx_user_return_update_cache (struct kvm_vcpu *vcpu) {
arch/x86/kvm/vmx/tdx.c static struct tdx_uret_msr tdx_uret_msrs[] = { · line 563
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +5
-0int i;for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++)kvm_user_return_update_cache (tdx_uret_msrs[i].slot, tdx_uret_msrs[i].defval); +/* +* TSX_CTRL is reset to 0 if guest TSX is supported. Otherwise +* preserved. +*/ +if (to_kvm_tdx (vcpu->kvm)->tsx_supported && tdx_uret_tsx_ctrl_slot !=-1 )
If to_kvm_tdx(vcpu->kvm)->tsx_supported is true, tdx_uret_tsx_ctrl_slot shouldn't be -1 at this point. Otherwise, it's a KVM bug, right?
Not sure if it needs a warning if tdx_uret_tsx_ctrl_slot is -1, or just remove the check?
Quoted 75 lines
arch/x86/kvm/vmx/tdx.c +24
-1+kvm_user_return_update_cache (tdx_uret_tsx_ctrl_slot,0 ); }static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu)@@ -649,7 +656,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); -tdx_user_return_update_cache (); +tdx_user_return_update_cache (vcpu);tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;@@ -1167,6 +1174,22 @@ static int setup_tdparams_xfam(struct kvm_cpuid2 *cpuid, struct td_params *td_pareturn 0 ; } +static bool tdparams_tsx_supported (struct kvm_cpuid2 *cpuid) +{ +const struct kvm_cpuid_entry2 *entry; +u64 mask; +u32 ebx; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0x7 ,0 ); +if (entry) + ebx = entry->ebx; +else + ebx =0 ; + + mask =__feature_bit (X86_FEATURE_HLE ) |__feature_bit (X86_FEATURE_RTM ); +return ebx & mask; +} +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params,struct kvm_tdx_init_vm *init_vm) {@@ -1209,6 +1232,7 @@ static int setup_tdparams(struct kvm *kvm, struct td_params *td_params,MEMCPY_SAME_SIZE (td_params->mrowner, init_vm->mrowner);MEMCPY_SAME_SIZE (td_params->mrownerconfig, init_vm->mrownerconfig); +to_kvm_tdx (kvm)->tsx_supported =tdparams_tsx_supported (cpuid);return 0 ; }@@ -2014,6 +2038,11 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EIO ; } } + tdx_uret_tsx_ctrl_slot =kvm_find_user_return_msr (MSR_IA32_TSX_CTRL ); +if (tdx_uret_tsx_ctrl_slot ==-1 &&boot_cpu_has (X86_FEATURE_MSR_TSX_CTRL )) { +pr_err ("MSR_IA32_TSX_CTRL isn't included by kvm_find_user_return_msr\n" ); +return -EIO ; + } max_pkgs =topology_max_packages (); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock),arch/x86/kvm/vmx/tdx.h +8
-0@@ -17,6 +17,14 @@ struct kvm_tdx {u64 xfam;int hkid; +/* +* Used on each TD-exit, see tdx_user_return_update_cache(). +* TSX_CTRL value on TD exit +* - set 0 if guest TSX enabled +* - preserved if guest TSX disabled +*/ +bool tsx_supported; +bool finalized;atomic_t tdh_mem_track;
Re: [PATCH v19 083/130] KVM: TDX: Add TSX_CTRL msr into uret_msrs list
Quoted 39 lines · Binbin Wu
Quoted 36 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Yang Weijiang <[email protected]>
TDX module resets the
TSX_CTRLMSR to 0 at TD exit if TSX is enabled for TD. Or it preserves theTSX_CTRLMSR if TSX is disabled for TD. VMM can rely onuret_msrsmechanism to defer the reload of host value until exiting to user space.Signed-off-by Yang Weijiang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v19:
- fix the type oftdx_uret_tsx_ctrl_slot. unguent int => int.--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/tdx.c +5
-1@@ -547,14 +547,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static int tdx_uret_tsx_ctrl_slot; -static void tdx_user_return_update_cache (void ) +static void tdx_user_return_update_cache (struct kvm_vcpu *vcpu) {int i;for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++)kvm_user_return_update_cache (tdx_uret_msrs[i].slot, tdx_uret_msrs[i].defval); +/* +* TSX_CTRL is reset to 0 if guest TSX is supported. Otherwise +* preserved.
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static struct tdx_uret_msr tdx_uret_msrs[] = { · line 563
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+*/ +if (to_kvm_tdx (vcpu->kvm)->tsx_supported && tdx_uret_tsx_ctrl_slot !=-1 )If to_kvm_tdx(vcpu->kvm)->tsx_supported is true,
tdx_uret_tsx_ctrl_slotshouldn't be -1 at this point.
Otherwise, it's a KVM bug, right?
Not sure if it needs a warning iftdx_uret_tsx_ctrl_slotis -1, or just remove the check?
You're right. Let me remove the != -1 check.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 84/130 The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu exits, interrupts, and hypercalls.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -13,6 +13,7 @@ What qemu can do - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. - Qemu can finalize guest TD. +- Qemu can start to run vcpu. But vcpu can not make progress yet. Patch Layer status ------------------
@@ -24,7 +25,7 @@ Patch Layer status * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied * TD finalization: Applied -* TD vcpu enter/exit: Applying +* TD vcpu enter/exit: Applied * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v19 85/130 KVM: TDX: Complete interrupts after tdexit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
---
v19:
- move tdvps_management_check() to this patch
- typo: complete -> Complete in short log
--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); } +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ u32 field) \
@@ -200,6 +202,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out;
Signature · 1 line
2.25.1
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Hi Isaku,
(In shortlog "tdexit" can be "TD exit" to be consistent with documentation.)
Quoted 4 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
This seems to be the first appearance of NMI and the changelog is very brief. How about expending it with:
"This corresponds to VMX __vmx_complete_interrupts(). Because TDX
virtualize vAPIC, KVM only needs to care about NMI injection.
KVM can request TDX to inject an NMI into a guest TD vCPU when the vCPU is not active. TDX will attempt to inject an NMI as soon as possible on TD entry. NMI injection is managed by writing to (to inject NMI) and reading from (to get status of NMI injection) the PEND_NMI field within the TDX vCPU scope metadata (Trust Domain Virtual Processor State (TDVPS)).
Update KVM's NMI status on TD exit by checking whether a requested NMI has been injected into the TD. Reading the metadata via SEAMCALL is expensive so only perform the check if an NMI was injected.
This is the first need to access vCPU scope metadata in the "management" class. Ensure that needed accessor is available.
"
Quoted 18 lines
Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 540
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */
/* Avoid costly SEAMCALL if no NMI was injected. */
Quoted 22 lines
arch/x86/kvm/vmx/tdx.c +7
-0+if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }arch/x86/kvm/vmx/tdx.h +0
-0
arch/x86/kvm/vmx/tdx.h static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) · line 145
Quoted 4 lines
arch/x86/kvm/vmx/tdx.h +1
-0@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); } +static __always_inline void tdvps_management_check(u64 field, u8 bits) {}
Is this intended to be a stub or is it expected to be fleshed out with some checks?
arch/x86/kvm/vmx/tdx.h TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs); · line 209
Quoted 13 lines
arch/x86/kvm/vmx/tdx.h +3
-0+#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ u32 field) \@@ -200,6 +202,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out;
Reinette
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Quoted 84 lines · Reinette Chatre
Hi Isaku,
(In shortlog "tdexit" can be "TD exit" to be consistent with documentation.)
Quoted 4 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.This seems to be the first appearance of NMI and the changelog is very brief. How about expending it with:
"This corresponds to VMX
__vmx_complete_interrupts(). Because TDX
virtualize vAPIC, KVM only needs to care about NMI injection.KVM can request TDX to inject an NMI into a guest TD vCPU when the vCPU is not active. TDX will attempt to inject an NMI as soon as possible on TD entry. NMI injection is managed by writing to (to inject NMI) and reading from (to get status of NMI injection) the
PEND_NMIfield within the TDX vCPU scope metadata (Trust Domain Virtual Processor State (TDVPS)).Update KVM's NMI status on TD exit by checking whether a requested NMI has been injected into the TD. Reading the metadata via SEAMCALL is expensive so only perform the check if an NMI was injected.
This is the first need to access vCPU scope metadata in the "management" class. Ensure that needed accessor is available.
"Quoted 18 lines
Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 540
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ /* Avoid costly SEAMCALL if no NMI was injected. */Quoted 25 lines
arch/x86/kvm/vmx/tdx.c +7
-0+if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }arch/x86/kvm/vmx/tdx.h +0
-0@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); }
Quoted 4 lines
arch/x86/kvm/vmx/tdx.h static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) · line 145
Quoted 1 line
arch/x86/kvm/vmx/tdx.h +1
-0+static __always_inline void tdvps_management_check(u64 field, u8 bits) {}Is this intended to be a stub or is it expected to be fleshed out with some checks?
It was used to check if field id matches bits. We should make tdvps_vmcs_check() common for vmcs, management and state_non_arch.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
In reply to Reinette ChatreQuoted 8 lines · Reinette Chatre
Hi Isaku,
(In shortlog "tdexit" can be "TD exit" to be consistent with documentation.)
Quoted 2 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Quoted 7 lines
Quoted 2 lines
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.This seems to be the first appearance of NMI and the changelog is very brief. How about expending it with:
"This corresponds to VMX
__vmx_complete_interrupts(). Because TDX
virtualize vAPIC, KVM only needs to care about NMI injection.
^
virtualizes
Also, does it need to mention that non-NMI interrupts are handled by posted-interrupt mechanism?
For example:
"This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualizes vAPIC, and non-NMI interrupts are delivered using posted-interrupt mechanism, KVM only needs to care about NMI injection.
...
"
Quoted 16 lines
KVM can request TDX to inject an NMI into a guest TD vCPU when the vCPU is not active. TDX will attempt to inject an NMI as soon as possible on TD entry. NMI injection is managed by writing to (to inject NMI) and reading from (to get status of NMI injection) the
PEND_NMIfield within the TDX vCPU scope metadata (Trust Domain Virtual Processor State (TDVPS)).Update KVM's NMI status on TD exit by checking whether a requested NMI has been injected into the TD. Reading the metadata via SEAMCALL is expensive so only perform the check if an NMI was injected.
This is the first need to access vCPU scope metadata in the "management" class. Ensure that needed accessor is available.
"
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Quoted 23 lines · Binbin Wu
Quoted 8 lines · Reinette Chatre
Hi Isaku,
(In shortlog "tdexit" can be "TD exit" to be consistent with documentation.)
Quoted 2 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Quoted 7 lines
Quoted 2 lines
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
This seems to be the first appearance of NMI and the changelog is very brief. How about expending it with:
"This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care about NMI injection.
^
virtualizesAlso, does it need to mention that non-NMI interrupts are handled by posted-interrupt mechanism?
Quoted 8 lines
For example:
"This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualizes vAPIC, and non-NMI interrupts are delivered using posted-interrupt mechanism, KVM only needs to care about NMI injection.
...
"
Thank you Binbin. Looks good to me.
Reinette
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
In reply to [email protected]Quoted 22 lines · On Mon, Feb 26, 2024 at 12:26:27AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 544
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +7
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +}
Looks this leads to NMI injection delay or even won't be reinjected if KVM_REQ_EVENT is not set on the target cpu when more than 1 NMIs are pending there.
On normal VM, KVM uses NMI window vmexit for injection successful case to rasie the KVM_REQ_EVENT again for remain pending NMIs, see handle_nmi_window(). KVM also checks vectoring info after VMEXIT for case that the NMI is not injected successfully in this vmentry vmexit round, and raise KVM_REQ_EVENT to try again, see __vmx_complete_interrupts().
In TDX, consider there's no way to get vectoring info or handle nmi window vmexit, below checking should cover both scenarios for NMI injection:
arch/x86/kvm/vmx/tdx.c +4 -1
@@ -835,9 +835,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected) +if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +if (vcpu->arch.nmi_injected || vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu); + } }
Quoted 39 lines
arch/x86/kvm/vmx/tdx.c +3
-0+struct tdx_uret_msr {u32 msr;unsigned int slot;@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }arch/x86/kvm/vmx/tdx.h +4
-1@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); } +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ u32 field) \@@ -200,6 +202,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out; --2.25.1
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Quoted 53 lines · Yuan Yao
Quoted 22 lines · On Mon, Feb 26, 2024 at 12:26:27AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 544
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +7
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +}Looks this leads to NMI injection delay or even won't be reinjected if
KVM_REQ_EVENTis not set on the target cpu when more than 1 NMIs are pending there.On normal VM, KVM uses NMI window vmexit for injection successful case to rasie the
KVM_REQ_EVENTagain for remain pending NMIs, seehandle_nmi_window(). KVM also checks vectoring info after VMEXIT for case that the NMI is not injected successfully in this vmentry vmexit round, and raiseKVM_REQ_EVENTto try again, see__vmx_complete_interrupts().In TDX, consider there's no way to get vectoring info or handle nmi window vmexit, below checking should cover both scenarios for NMI injection:
arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 842
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +3
-1@@ -835,9 +835,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected) +if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +if (vcpu->arch.nmi_injected || vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu);
For nmi_injected, it should be OK because TD_VCPU_PEND_NMI is still set. But for nmi_pending, it should be checked and raise event.
I remember there was a discussion in the following link:
https://lore.kernel.org/kvm/[email protected]/
It said tdx_vcpu_run() will ignore force_immediate_exit.
If force_immediate_exit is igored for TDX, then the nmi_pending handling could still be delayed if the previous NMI was injected successfully.
Quoted 42 lines
arch/x86/kvm/vmx/tdx.c +1
-0+ } }Quoted 39 lines
arch/x86/kvm/vmx/tdx.c +3
-0+struct tdx_uret_msr {u32 msr;unsigned int slot;@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }arch/x86/kvm/vmx/tdx.h +4
-1@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); } +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ u32 field) \@@ -200,6 +202,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out; --2.25.1
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Quoted 60 lines · Binbin Wu
Quoted 57 lines · Yuan Yao
Quoted 22 lines · On Mon, Feb 26, 2024 at 12:26:27AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 544
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +7
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +}Looks this leads to NMI injection delay or even won't be reinjected if
KVM_REQ_EVENTis not set on the target cpu when more than 1 NMIs are pending there.On normal VM, KVM uses NMI window vmexit for injection successful case to rasie the
KVM_REQ_EVENTagain for remain pending NMIs, seehandle_nmi_window(). KVM also checks vectoring info after VMEXIT for case that the NMI is not injected successfully in this vmentry vmexit round, and raiseKVM_REQ_EVENTto try again, see__vmx_complete_interrupts().In TDX, consider there's no way to get vectoring info or handle nmi window vmexit, below checking should cover both scenarios for NMI injection:
arch/x86/kvm/vmx/tdx.c +0
-1@@ -835,9 +835,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected)
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 842
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +3
-0+if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +if (vcpu->arch.nmi_injected || vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu);For
nmi_injected, it should be OK becauseTD_VCPU_PEND_NMIis still set. But fornmi_pending, it should be checked and raise event.
Right, I just forgot the tdx module can do more than "hardware":
arch/x86/kvm/vmx/tdx.c +8 -1
@@ -835,9 +835,16 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected) +if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +/* +tdx module will retry injection in case of TD_VCPU_PEND_NMI, +so don't need to set KVM_REQ_EVENT for it again. +*/ +if (!vcpu->arch.nmi_injected && vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu); + } }
Quoted 6 lines
I remember there was a discussion in the following link:
https://lore.kernel.org/kvm/[email protected]/
It saidtdx_vcpu_run()will ignoreforce_immediate_exit.
Ifforce_immediate_exitis igored for TDX, then thenmi_pendinghandling could still be delayed if the previous NMI was injected successfully.
Yes, not sure the possibility of meeting this in real use case, I know it happens in some testing, e.g. the kvm unit test's multiple NMI tesing.
Quoted 45 lines
Quoted 44 lines
arch/x86/kvm/vmx/tdx.c +1
-0+ } }Quoted 39 lines
arch/x86/kvm/vmx/tdx.c +3
-0+struct tdx_uret_msr {u32 msr;unsigned int slot;@@ -663,6 +671,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }arch/x86/kvm/vmx/tdx.h +4
-1@@ -142,6 +142,8 @@ static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) "Invalid TD VMCS access for 16-bit field"); } +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ u32 field) \@@ -200,6 +202,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out; --2.25.1
Re: [PATCH v19 085/130] KVM: TDX: Complete interrupts after tdexit
Quoted 95 lines · Yuan Yao
Quoted 50 lines · Binbin Wu
Quoted 48 lines · Yuan Yao
Quoted 22 lines · On Mon, Feb 26, 2024 at 12:26:27AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Reviewed-by Binbin Wu <[email protected]>---
v19:
- movetdvps_management_check()to this patch
- typo: complete -> Complete in short log--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ 2 files changed, 14 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 544
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +7
-0@@ -535,6 +535,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +}Looks this leads to NMI injection delay or even won't be reinjected if
KVM_REQ_EVENTis not set on the target cpu when more than 1 NMIs are pending there.On normal VM, KVM uses NMI window vmexit for injection successful case to rasie the
KVM_REQ_EVENTagain for remain pending NMIs, seehandle_nmi_window(). KVM also checks vectoring info after VMEXIT for case that the NMI is not injected successfully in this vmentry vmexit round, and raiseKVM_REQ_EVENTto try again, see__vmx_complete_interrupts().In TDX, consider there's no way to get vectoring info or handle nmi window vmexit, below checking should cover both scenarios for NMI injection:
Quoted 16 lines
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +0
-0arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 842
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +3
-1@@ -835,9 +835,12 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected) +if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +if (vcpu->arch.nmi_injected || vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu);For
nmi_injected, it should be OK becauseTD_VCPU_PEND_NMIis still set. But fornmi_pending, it should be checked and raise event.Right, I just forgot the tdx module can do more than "hardware":
arch/x86/kvm/vmx/tdx.c +8
-1@@ -835,9 +835,16 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) {/* Avoid costly SEAMCALL if no nmi was injected */ -if (vcpu->arch.nmi_injected) +if (vcpu->arch.nmi_injected) { vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ); +/* +tdx module will retry injection in case of TD_VCPU_PEND_NMI, +so don't need to set KVM_REQ_EVENT for it again. +*/ +if (!vcpu->arch.nmi_injected && vcpu->arch.nmi_pending) +kvm_make_request (KVM_REQ_EVENT , vcpu); + } }Quoted 5 lines
I remember there was a discussion in the following link:
https://lore.kernel.org/kvm/[email protected]/
It saidtdx_vcpu_run()will ignoreforce_immediate_exit.
Ifforce_immediate_exitis igored for TDX, then thenmi_pendinghandling could still be delayed if the previous NMI was injected successfully.
Quoted 3 lines
Yes, not sure the possibility of meeting this in real use case, I know it happens in some testing, e.g. the kvm unit test's multiple NMI tesing.
Delay the pending NMI to the next VM exit will have problem.
Current Linux kernel code on NMI handling, it will check back-to-back NMI when handling unknown NMI.
Here are the comments in arch/x86/kernel/nmi.c
/*
* Only one NMI can be latched at a time. To handle
* this we may process multiple nmi handlers at once to
* cover the case where an NMI is dropped. The downside
* to this approach is we may process an NMI prematurely,
* while its real NMI is sitting latched. This will cause
* an unknown NMI on the next run of the NMI processing.
*
* We tried to flag that condition above, by setting the
* swallow_nmi flag when we process more than one event.
* This condition is also only present on the second half
* of a back-to-back NMI, so we flag that condition too.
*
* If both are true, we assume we already processed this
* NMI previously and we swallow it. ...
*/
Assume there are two NMIs pending in KVM, i.e. nmi_pending is 2.
KVM injects one NMI by settting TD_VCPU_PEND_NMI field and the nmi_pending is decreased to 1.
The pending NMI will be delayed until the next VM Exit, it will not be detected as the second half of back-to-back NMI in guest. Then it will be considered as a real unknown NMI, and if no one handles it (because it could have been handled in the previous NMI handler). At last, guest kernel will fire error message for the "unhandled" unknown NMI, and even panic if unknown_nmi_panic or panic_on_unrecovered_nmi is set true.
Since KVM doesn't have the capability to get NMI blocking status or request NMI-window exit for TDX, how about limiting the nmi pending to 1 for TDX? I.e. if TD_VCPU_PEND_NMI is not set, limit nmi_pending to 1 in process_nmi();
if TD_VCPU_PEND_NMI is set, limit nmi_pending to 0 in process_nmi().
Had some checks about the history when nmi_pending limit changed to 2.
The discussion in the link https://lore.kernel.org/all/[email protected]/ said:
" ... the NMI handlers are now being reworked to handle
just one NMI source (hopefully the cheapest) in the handler, and if we
detect a back-to-back NMI, handle all possible NMI sources."
IIUC, the change in NMI handlers described above is referring to the patch set "x86, nmi: new NMI handling routines"
https://lore.kernel.org/all/[email protected]/
I noticed that in v6 of the patch series, there was an optimization, but removed in v7.
v6 link: https://lore.kernel.org/all/[email protected]/
v7 link: https://lore.kernel.org/all/[email protected]/
The Optimization code in v6, but removed in v7:
-static int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs) +static int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs, bool b2b) {
struct nmi_desc *desc = nmi_to_desc(type);
struct nmiaction *a;
@@ -89,6 +89,15 @@ static int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs)
handled += a->handler(type, regs);
+ /*
+ * Optimization: only loop once if this is not a + * back-to-back NMI. The idea is nothing is dropped + * on the first NMI, only on the second of a back-to-back + * NMI. No need to waste cycles going through all the + * handlers.
+ */
+ if (!b2b && handled)
+ break;
}
At last, back-to-back NMI optimization is not used in Linux kernel.
So the kernel is able to handle NMI sources if we drop later NMIs when there is already one virtual NMI pending for TDX.
PATCH v19 86/130 KVM: TDX: restore debug store when TD exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/intel/ds.c +1 -0
@@ -2428,3 +2428,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -665,6 +665,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx);tdx_user_return_update_cache (vcpu); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Signature · 1 line
2.25.1
Re: [PATCH v19 086/130] KVM: TDX: restore debug store when TD exit
Hi Isaku,
Quoted 4 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
I am trying to understand why this is needed and finding a few places that seem to indicate that this is not needed.
For example, in the TX base spec, section 15.2 "On-TD Performance Monitoring", subsection "15.2.1 Overview":
* IA32_DS_AREA MSR is context-switched across TD entry and exit transitions.
To confirm I peeked at the TDX module code and found (if I understand this correctly) that IA32_DS_AREA is saved on TD entry (see [1]) and restored on TD exit (see [2]).
Quoted 21 lines
Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)arch/x86/events/intel/ds.c +1
-0@@ -2428,3 +2428,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) · line 671
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +1
-0@@ -665,6 +665,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx);tdx_user_return_update_cache (vcpu); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Reinette
[1] https://github.com/intel/tdx-module/blob/tdx_1.5/src/td_transitions/tdh_vp_enter.c#L719
[2] https://github.com/intel/tdx-module/blob/tdx_1.5/src/td_transitions/td_exit.c#L130
Re: [PATCH v19 086/130] KVM: TDX: restore debug store when TD exit
Quoted 11 lines · Reinette Chatre
Hi Isaku,
Quoted 4 lines · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
I am trying to understand why this is needed and finding a few places that seem to indicate that this is not needed.
Quoted 7 lines
For example, in the TX base spec, section 15.2 "On-TD Performance Monitoring", subsection "15.2.1 Overview":
*IA32_DS_AREAMSR is context-switched across TD entry and exit transitions.To confirm I peeked at the TDX module code and found (if I understand this correctly) that
IA32_DS_AREAis saved on TD entry (see [1]) and restored on TD exit (see [2]).
Hi Reinette,
You are right. I asked TDX module to preserve IA32_DS_AREA across TD transitions and they made this change. So, this patch can be dropped now.
PATCH v19 87/130 KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/main.c +29 -3
@@ -33,6 +33,14 @@ static int vt_max_vcpus(struct kvm *kvm)static int vt_flush_remote_tlbs (struct kvm *kvm);#endif +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +if (enable_tdx) +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;
@@ -201,6 +209,16 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_load (vcpu, cpu); +return ; + } + +vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -262,6 +280,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static u8 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {if (is_td_vcpu (vcpu))
@@ -335,7 +361,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {/* TDX cpu enablement is done by tdx_hardware_setup(). */ .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_disable = vt_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported,
@@ -353,7 +379,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,
@@ -440,7 +466,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
arch/x86/kvm/vmx/tdx.c +186 -4
268 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -73,6 +73,8 @@ struct vcpu_tdx {unsigned long *tdvpx_pa;bool td_vcpu_created; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -137,6 +137,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_disable (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_offline_cpu (void );
@@ -153,6 +154,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -171,6 +173,7 @@ void tdx_post_memory_mapping(struct kvm_vcpu *vcpu,#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_offline_cpu (void ) {return 0 ; }
@@ -190,6 +193,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 8 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine,
"the machine" -> "a VM"
Quoted 1 line
(VMX or TDX) vcpus needs to be shutdown on
^
need
Quoted 93 lines
each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/main.c +29
-3@@ -33,6 +33,14 @@ static int vt_max_vcpus(struct kvm *kvm)static int vt_flush_remote_tlbs (struct kvm *kvm);#endif +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +if (enable_tdx) +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;@@ -201,6 +209,16 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_load (vcpu, cpu); +return ; + } + +vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {@@ -262,6 +280,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static u8 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {if (is_td_vcpu (vcpu))@@ -335,7 +361,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {/* TDX cpu enablement is done by tdx_hardware_setup(). */ .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_disable = vt_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported,@@ -353,7 +379,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,@@ -440,7 +466,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c static DEFINE_MUTEX(tdx_lock); · line 111
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -106,6 +106,14 @@ static DEFINE_MUTEX(tdx_lock);static struct mutex *tdx_mng_key_config_lock;static atomic_t nr_configured_hkid; +/* +* A per-CPU list of TD vCPUs associated with a given CPU. Used when a CPU +* is brought down
Not necessarily to be a CPU brought down.
It will also be triggered by the last VM being destroyed.
But had a seond thought, if it is trigged by the last VM case, the list should be empty already. So I am OK with the descripton.
arch/x86/kvm/vmx/tdx.c static DEFINE_MUTEX(tdx_lock); · line 112
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +0
-0to invoke TDH_VP_FLUSH on the approapriate TD vCPUS.
"approapriate" -> "appropriate"
Quoted 47 lines
arch/x86/kvm/vmx/tdx.c +5
-0+* Protected by interrupt mask. This list is manipulated in process context +* of vcpu and IPI callback. See tdx_flush_vp_on_cpu(). +*/ +static DEFINE_PER_CPU(struct list_head, associated_tdvcpus); + static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid) {return pa | ((hpa_t)hkid << boot_cpu_data.x86_phys_bits);arch/x86/kvm/vmx/tdx.c +31
-0@@ -138,6 +146,37 @@ static inline bool is_td_finalized(struct kvm_tdx *kvm_tdx)return kvm_tdx->finalized; } +static inline void tdx_disassociate_vp (struct kvm_vcpu *vcpu) +{ +lockdep_assert_irqs_disabled (); + +list_del (&to_tdx (vcpu)->cpu_list); + +/* +* Ensure tdx->cpu_list is updated is before setting vcpu->cpu to -1, +* otherwise, a different CPU can see vcpu->cpu = -1 and add the vCPU +* to its list before its deleted from this CPUs list. +*/ +smp_wmb (); + + vcpu->cpu =-1 ; +} + +static void tdx_disassociate_vp_arg (void *vcpu) +{ +tdx_disassociate_vp (vcpu); +} + +static void tdx_disassociate_vp_on_cpu (struct kvm_vcpu *vcpu) +{ +int cpu = vcpu->cpu; + +if (unlikely (cpu ==-1 )) +return ; + +smp_call_function_single (cpu, tdx_disassociate_vp_arg, vcpu,1 ); +} +static void tdx_clear_page (unsigned long page_pa) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));
arch/x86/kvm/vmx/tdx.c static void tdx_reclaim_control_page(unsigned long td_page_pa) · line 265
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +6
-0@@ -218,6 +257,87 @@ static void tdx_reclaim_control_page(unsigned long td_page_pa)free_page ((unsigned long )__va (td_page_pa)); } +struct tdx_flush_vp_arg { +struct kvm_vcpu *vcpu; +u64 err; +}; + +static void tdx_flush_vp (void *arg_)
It is more common to use "_arg" instead of "arg_".
Quoted 263 lines
arch/x86/kvm/vmx/tdx.c +141
-4+{ +struct tdx_flush_vp_arg *arg = arg_; +struct kvm_vcpu *vcpu = arg->vcpu; +u64 err; + + arg->err =0 ; +lockdep_assert_irqs_disabled (); + +/* Task migration can race with CPU offlining. */ +if (unlikely (vcpu->cpu !=raw_smp_processor_id ())) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The +* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized. +*/ +if (is_td_vcpu_created (to_tdx (vcpu))) { +/* +* No need to retry. TDX Resources needed for TDH.VP.FLUSH are, +* TDVPR as exclusive, TDR as shared, and TDCS as shared. This +* vp flush function is called when destructing vcpu/TD or vcpu +* migration. No other thread uses TDVPR in those cases. +*/ + err =tdh_vp_flush (to_tdx (vcpu)->tdvpr_pa); +if (unlikely (err && err !=TDX_VCPU_NOT_ASSOCIATED )) { +/* +* This function is called in IPI context. Do not use +* printk to avoid console semaphore. +* The caller prints out the error message, instead. +*/ +if (err) + arg->err = err; + } + } + +tdx_disassociate_vp (vcpu); +} + +static void tdx_flush_vp_on_cpu (struct kvm_vcpu *vcpu) +{ +struct tdx_flush_vp_arg arg = { + .vcpu = vcpu, + }; +int cpu = vcpu->cpu; + +if (unlikely (cpu ==-1 )) +return ; + +smp_call_function_single (cpu, tdx_flush_vp, &arg,1 ); +if (WARN_ON_ONCE (arg.err)) { +pr_err ("cpu: %d " , cpu); +pr_tdx_error (TDH_VP_FLUSH , arg.err,NULL ); + } +} + +void tdx_hardware_disable (void ) +{ +int cpu =raw_smp_processor_id (); +struct list_head *tdvcpus = &per_cpu (associated_tdvcpus, cpu); +struct tdx_flush_vp_arg arg; +struct vcpu_tdx *tdx, *tmp; +unsigned long flags; + +lockdep_assert_preemption_disabled (); + +local_irq_save (flags); +/* Safe variant needed as tdx_disassociate_vp() deletes the entry. */ +list_for_each_entry_safe (tdx, tmp, tdvcpus, cpu_list) { + arg.vcpu = &tdx->vcpu; +tdx_flush_vp (&arg); + } +local_irq_restore (flags); +} +static void tdx_do_tdh_phymem_cache_wb (void *unused) {u64 err =0 ;@@ -233,26 +353,31 @@ static void tdx_do_tdh_phymem_cache_wb(void *unused)pr_tdx_error (TDH_PHYMEM_CACHE_WB , err,NULL ); } -void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization. @@ -270,6 +395,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) { + ret = -EBUSY ; + goto out; + } +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +pr_err ("tdh_mng_vpflushdone() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + ret = -EIO ; + goto out; + } + for_each_online_cpu(i) { if (packages_allocated && cpumask_test_and_set_cpu(topology_physical_package_id(i),@@ -291,14 +429,24 @@ void tdx_mmu_release_hkid(struct kvm *kvm)pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL );pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , kvm_tdx->hkid); + ret = -EIO ; }else tdx_hkid_free (kvm_tdx); +out:write_unlock (&kvm->mmu_lock);mutex_unlock (&tdx_lock);cpus_read_unlock ();free_cpumask_var (targets);free_cpumask_var (packages); + +return ret; +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }void tdx_vm_free (struct kvm *kvm)@@ -455,6 +603,26 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return 0 ; } +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (vcpu->cpu == cpu) +return ; + +tdx_flush_vp_on_cpu (vcpu); + +local_irq_disable (); +/* +* Pairs with the smp_wmb() in tdx_disassociate_vp() to ensure +* vcpu->cpu is read before tdx->cpu_list. +*/ +smp_rmb (); + +list_add (&tdx->cpu_list, &per_cpu (associated_tdvcpus, cpu)); +local_irq_enable (); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -495,6 +663,16 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)struct vcpu_tdx *tdx =to_tdx (vcpu);int i; +/* +* When destroying VM, kvm_unload_vcpu_mmu() calls vcpu_load() for every +* vcpu after they already disassociated from the per cpu list by +* tdx_mmu_release_hkid(). So we need to disassociate them again, +* otherwise the freed vcpu data will be accessed when do +* list_{del,add}() on associated_tdvcpus list later. +*/ +tdx_disassociate_vp_on_cpu (vcpu); +WARN_ON_ONCE (vcpu->cpu !=-1 ); +/* * This methods can be called when vcpu allocation/initialization * failed. So it's possible that hkid, tdvpx and tdvpr are not assigned @@ -2030,6 +2208,10 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +/* tdx_hardware_disable() uses associated_tdvcpus. */ +for_each_possible_cpu (i) +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus, i)); +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) {/* * Here it checks if MSRs (tdx_uret_msrs) can be saved/restored arch/x86/kvm/vmx/tdx.h +2
-0@@ -73,6 +73,8 @@ struct vcpu_tdx {unsigned long *tdvpx_pa;bool td_vcpu_created; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;arch/x86/kvm/vmx/x86_ops.h +4
-0@@ -137,6 +137,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_disable (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_offline_cpu (void );@@ -153,6 +154,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -171,6 +173,7 @@ void tdx_post_memory_mapping(struct kvm_vcpu *vcpu,#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_offline_cpu (void ) {return 0 ; }@@ -190,6 +193,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]Hi Isaku,
Quoted 1 line · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
...
Quoted 24 lines
arch/x86/kvm/vmx/tdx.c +20
-0@@ -218,6 +257,87 @@ static void tdx_reclaim_control_page(unsigned long td_page_pa)free_page ((unsigned long )__va (td_page_pa)); } +struct tdx_flush_vp_arg { +struct kvm_vcpu *vcpu; +u64 err; +}; + +static void tdx_flush_vp (void *arg_) +{ +struct tdx_flush_vp_arg *arg = arg_; +struct kvm_vcpu *vcpu = arg->vcpu; +u64 err; + + arg->err =0 ; +lockdep_assert_irqs_disabled (); + +/* Task migration can race with CPU offlining. */ +if (unlikely (vcpu->cpu !=raw_smp_processor_id ())) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The
arch/x86/kvm/vmx/tdx.c static void tdx_reclaim_control_page(unsigned long td_page_pa) · line 289
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized. +*/ +if (is_td_vcpu_created (to_tdx (vcpu))) { +/* +* No need to retry. TDX Resources needed for TDH.VP.FLUSH are, +* TDVPR as exclusive, TDR as shared, and TDCS as shared. This +* vp flush function is called when destructing vcpu/TD or vcpu +* migration. No other thread uses TDVPR in those cases. +*/
(I have comment later that refer back to this comment about needing retry.)
...
Quoted 69 lines
arch/x86/kvm/vmx/tdx.c +24
-4@@ -233,26 +353,31 @@ static void tdx_do_tdh_phymem_cache_wb(void *unused)pr_tdx_error (TDH_PHYMEM_CACHE_WB , err,NULL ); } -void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization. @@ -270,6 +395,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) { + ret = -EBUSY ; + goto out; + } +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +pr_err ("tdh_mng_vpflushdone() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + ret = -EIO ; + goto out; + } + for_each_online_cpu(i) { if (packages_allocated && cpumask_test_and_set_cpu(topology_physical_package_id(i),@@ -291,14 +429,24 @@ void tdx_mmu_release_hkid(struct kvm *kvm)pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL );pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , kvm_tdx->hkid); + ret = -EIO ; }else tdx_hkid_free (kvm_tdx); +out:write_unlock (&kvm->mmu_lock);mutex_unlock (&tdx_lock);cpus_read_unlock ();free_cpumask_var (targets);
arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0free_cpumask_var (packages); + +return ret; +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }
As I understand, __tdx_mmu_release_hkid() returns -EBUSY after TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returns TDX_FLUSHVP_NOT_DONE.
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this while() loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within __tdx_mmu_release_hkid() to _just_ resend TDH.MNG.VPFLUSHDONE?
Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
I note that TDX_FLUSHVP_NOT_DONE is distinct from TDX_OPERAND_BUSY that can also be returned from TDH.MNG.VPFLUSHDONE and wonder if a retry may be needed in that case also/instead? It looks like TDH.MNG.VPFLUSHDONE needs exclusive access to all operands and I do not know enough yet if this is the case here.
Reinette
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 130 lines · Reinette Chatre
Hi Isaku,
Quoted 1 line · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
...
Quoted 24 lines
arch/x86/kvm/vmx/tdx.c +20
-0@@ -218,6 +257,87 @@ static void tdx_reclaim_control_page(unsigned long td_page_pa)free_page ((unsigned long )__va (td_page_pa)); } +struct tdx_flush_vp_arg { +struct kvm_vcpu *vcpu; +u64 err; +}; + +static void tdx_flush_vp (void *arg_) +{ +struct tdx_flush_vp_arg *arg = arg_; +struct kvm_vcpu *vcpu = arg->vcpu; +u64 err; + + arg->err =0 ; +lockdep_assert_irqs_disabled (); + +/* Task migration can race with CPU offlining. */ +if (unlikely (vcpu->cpu !=raw_smp_processor_id ())) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The arch/x86/kvm/vmx/tdx.c static void tdx_reclaim_control_page(unsigned long td_page_pa) · line 289
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized. +*/ +if (is_td_vcpu_created (to_tdx (vcpu))) { +/* +* No need to retry. TDX Resources needed for TDH.VP.FLUSH are, +* TDVPR as exclusive, TDR as shared, and TDCS as shared. This +* vp flush function is called when destructing vcpu/TD or vcpu +* migration. No other thread uses TDVPR in those cases. +*/ (I have comment later that refer back to this comment about needing retry.)
...
Quoted 69 lines
arch/x86/kvm/vmx/tdx.c +24
-4@@ -233,26 +353,31 @@ static void tdx_do_tdh_phymem_cache_wb(void *unused)pr_tdx_error (TDH_PHYMEM_CACHE_WB , err,NULL ); } -void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization. @@ -270,6 +395,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) { + ret = -EBUSY ; + goto out; + } +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +pr_err ("tdh_mng_vpflushdone() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + ret = -EIO ; + goto out; + } + for_each_online_cpu(i) { if (packages_allocated && cpumask_test_and_set_cpu(topology_physical_package_id(i),@@ -291,14 +429,24 @@ void tdx_mmu_release_hkid(struct kvm *kvm)pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL );pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , kvm_tdx->hkid); + ret = -EIO ; }else tdx_hkid_free (kvm_tdx); +out:write_unlock (&kvm->mmu_lock);mutex_unlock (&tdx_lock);cpus_read_unlock ();free_cpumask_var (targets);arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0free_cpumask_var (packages); + +return ret; +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.
Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again. The MMU notifier release hook eventually calls tdx_mmu_release_hkid(). Other kernel thread (concretely, vhost krenel thread) can get reference count to mmu and put it by timer, the MMU notifier release hook can be triggered during closing vCPU fd.
The possible alternative is to make the vCPU closing path complicated not to load vCPU context instead f sending IPI on every retry.
Quoted 5 lines
I note that
TDX_FLUSHVP_NOT_DONEis distinct fromTDX_OPERAND_BUSYthat can also be returned from TDH.MNG.VPFLUSHDONE and wonder if a retry may be needed in that case also/instead? It looks like TDH.MNG.VPFLUSHDONE needs exclusive access to all operands and I do not know enough yet if this is the case here.
Because we're destructing the guest and gain mmu_lock, we shouldn't have other thread racing. Probably we can simply retry on TDX_OPERAND_BUSY without worrying race. It would be more robust and easier to understand.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 16 lines · Isaku Yamahata
Quoted 15 lines · Reinette Chatre
arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +4
-0+void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?
Quoted 6 lines
Quoted 3 lines
Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
But why is _loading_ a vCPU context problematic? If I'm reading the TDX module code correctly, TDX_FLUSHVP_NOT_DONE is returned when a vCPU is "associated" with a pCPU, and association only happens during TDH.VP_ENTER, TDH.MNG.RD, and TDH.MNG.WR, none of which I see in tdx_vcpu_load().
Assuming there is something problematic lurking under vcpu_load(), I would love, love, LOVE an excuse to not do vcpu_{load,put}() in kvm_unload_vcpu_mmu(), i.e. get rid of that thing entirely.
I have definitely looked into kvm_unload_vcpu_mmu() on more than one occassion, but I can't remember off the top of my head why I have never yanked out the vcpu_{load,put}(). Maybe I was just scared of breaking something and didn't have a good reason to risk breakage?
Quoted 7 lines
The MMU notifier release hook eventually calls
tdx_mmu_release_hkid(). Other kernel thread (concretely, vhost krenel thread) can get reference count to mmu and put it by timer, the MMU notifier release hook can be triggered during closing vCPU fd.The possible alternative is to make the vCPU closing path complicated not to load vCPU context instead f sending IPI on every retry.
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 17 lines · Sean Christopherson
Quoted 16 lines · Isaku Yamahata
Quoted 15 lines · Reinette Chatre
arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +4
-0+void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why tdx_mmu_release_hkid() issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 27 lines · Isaku Yamahata
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Isaku Yamahata
Quoted 10 lines · Reinette Chatre
arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +4
-0+void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
Quoted 6 lines
Quoted 1 line
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why
tdx_mmu_release_hkid()issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.
By "problematic", I meant, why can that result in a "missed in one TDH.VP.FLUSH cycle"? AFAICT, loading a vCPU shouldn't cause that vCPU to be associated from the TDX module's perspective, and thus shouldn't trigger TDX_FLUSHVP_NOT_DONE.
I.e. looping should be unnecessary, no?
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 35 lines · Sean Christopherson
Quoted 27 lines · Isaku Yamahata
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Isaku Yamahata
Quoted 10 lines · Reinette Chatre
arch/x86/kvm/vmx/tdx.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 450
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +4
-0+void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
Quoted 6 lines
Quoted 1 line
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why
tdx_mmu_release_hkid()issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.
Quoted 5 lines
By "problematic", I meant, why can that result in a "missed in one TDH.VP.FLUSH cycle"? AFAICT, loading a vCPU shouldn't cause that vCPU to be associated from the TDX module's perspective, and thus shouldn't trigger
TDX_FLUSHVP_NOT_DONE.I.e. looping should be unnecessary, no?
The loop is unnecessary with the current code.
The possible future optimization is to reduce destruction time of Secure-EPT somehow. One possible option is to release HKID while vCPUs are still alive and destruct Secure-EPT with multiple vCPU context. Because that's future optimization, we can ignore it at this phase.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 42 lines · Isaku
Quoted 39 lines · Sean Christopherson
Quoted 27 lines · Isaku Yamahata
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Isaku Yamahata
Quoted 10 lines · Reinette Chatre
Review
Quoted 5 lines
(quoted diff) +4
-0+void tdx_mmu_release_hkid(struct kvm *kvm) +{ + while (__tdx_mmu_release_hkid(kvm) == -EBUSY) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
Quoted 6 lines
Quoted 1 line
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why
tdx_mmu_release_hkid()issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.By "problematic", I meant, why can that result in a "missed in one TDH.VP.FLUSH cycle"? AFAICT, loading a vCPU shouldn't cause that vCPU to be associated from the TDX module's perspective, and thus shouldn't trigger
TDX_FLUSHVP_NOT_DONE.
Quoted 8 lines
Quoted 1 line
I.e. looping should be unnecessary, no?
The loop is unnecessary with the current code.
The possible future optimization is to reduce destruction time of Secure-EPT somehow. One possible option is to release HKID while vCPUs are still alive and destruct Secure-EPT with multiple vCPU context. Because that's future optimization, we can ignore it at this phase.
I kinda lost here.
I thought in the current v19 code, you have already implemented this optimization?
Or is this optimization totally different from what we discussed in an earlier patch?
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 56 lines · Kai
Quoted 42 lines · Isaku
Quoted 39 lines · Sean Christopherson
Quoted 27 lines · Isaku Yamahata
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Isaku Yamahata
Quoted 10 lines · Reinette Chatre
Review
Quoted 5 lines
(quoted diff) +4
-0+void tdx_mmu_release_hkid(struct kvm *kvm) +{ + while (__tdx_mmu_release_hkid(kvm) == -EBUSY) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
Quoted 6 lines
Quoted 1 line
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why
tdx_mmu_release_hkid()issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.By "problematic", I meant, why can that result in a "missed in one TDH.VP.FLUSH cycle"? AFAICT, loading a vCPU shouldn't cause that vCPU to be associated from the TDX module's perspective, and thus shouldn't trigger
TDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Quoted 1 line
I.e. looping should be unnecessary, no?
The loop is unnecessary with the current code.
The possible future optimization is to reduce destruction time of Secure-EPT somehow. One possible option is to release HKID while vCPUs are still alive and destruct Secure-EPT with multiple vCPU context. Because that's future optimization, we can ignore it at this phase.
I kinda lost here.
Quoted 7 lines
I thought in the current v19 code, you have already implemented this optimization?
Or is this optimization totally different from what we discussed in an earlier patch?
That's only the first step. We can optimize it further with multiple vCPUs context.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 62 lines · Isaku Yamahata
Quoted 59 lines · Kai
Quoted 42 lines · Isaku
Quoted 39 lines · Sean Christopherson
Quoted 27 lines · Isaku Yamahata
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Isaku Yamahata
Quoted 10 lines · Reinette Chatre
Review
Quoted 5 lines
(quoted diff) +4
-0+void tdx_mmu_release_hkid(struct kvm *kvm) +{ + while (__tdx_mmu_release_hkid(kvm) == -EBUSY) + ; }As I understand,
__tdx_mmu_release_hkid()returns-EBUSYafter TDH.VP.FLUSH has been sent for every vCPU followed by TDH.MNG.VPFLUSHDONE, which returnsTDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Considering earlier comment that a retry of TDH.VP.FLUSH is not needed, why is this
while()loop here that sends the TDH.VP.FLUSH again to all vCPUs instead of just a loop within__tdx_mmu_release_hkid()to _just_ resend TDH.MNG.VPFLUSHDONE?Could it be possible for a vCPU to appear during this time, thus be missed in one TDH.VP.FLUSH cycle, to require a new cycle of TDH.VP.FLUSH?
Yes. There is a race between closing KVM vCPU fd and MMU notifier release hook. When KVM vCPU fd is closed, vCPU context can be loaded again.
Quoted 6 lines
Quoted 1 line
But why is _loading_ a vCPU context problematic?
It's nothing problematic. It becomes a bit harder to understand why
tdx_mmu_release_hkid()issues IPI on each loop. I think it's reasonable to make the normal path easy and to complicate/penalize the destruction path. Probably I should've added comment on the function.By "problematic", I meant, why can that result in a "missed in one TDH.VP.FLUSH cycle"? AFAICT, loading a vCPU shouldn't cause that vCPU to be associated from the TDX module's perspective, and thus shouldn't trigger
TDX_FLUSHVP_NOT_DONE.Quoted 8 lines
Quoted 1 line
I.e. looping should be unnecessary, no?
The loop is unnecessary with the current code.
The possible future optimization is to reduce destruction time of Secure-EPT somehow. One possible option is to release HKID while vCPUs are still alive and destruct Secure-EPT with multiple vCPU context. Because that's future optimization, we can ignore it at this phase.
I kinda lost here.
I thought in the current v19 code, you have already implemented this optimization?
Quoted 7 lines
Quoted 4 lines
Or is this optimization totally different from what we discussed in an earlier patch?
That's only the first step. We can optimize it further with multiple vCPUs context.
OK. Let's put aside how important these optimizations are and whether they should be done in the initial TDX support, I think the right way to organize the patches is to bring functionality first and then put performance optimization later.
That can make both writing code and code review easier.
And more importantly, the "performance optimization" can be discussed _separately_.
For example, as mentioned in the link above, I think the optimization of "releasing HKID in the MMU notifier release to improve TD teardown latency" complicates things a lot, e.g., not only to the TD creation/teardown sequence, but also here -- w/o it we don't even need to consider the race between vCPU load and MMU notifier release:
https://lore.kernel.org/kvm/[email protected]/
So I think we should start with implementing these sequences in "normal way" first, and then do the optimization(s) later.
And to me the "normal way" for TD creation/destruction we can just do:
1) Use normal SEPT sequence to teardown private EPT page table;
2) Do VP.FLUSH when vCPU is destroyed
3) Do VPFLUSHDONE after all vCPUs are destroyed
4) release HKID at last stage of destroying VM.
For vCPU migration, you do VP.FLUSH on the old pCPU before you load the vCPU to the new pCPU, as shown in this patch.
Then you don't need to cover the silly code change around tdx_mmu_release_hkid() in this patch.
Am I missing anything?
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
In reply to Reinette ChatreQuoted 32 lines · Reinette Chatre
Hi Isaku,
Quoted 1 line · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
...
Quoted 26 lines
arch/x86/kvm/vmx/tdx.c +22
-0@@ -218,6 +257,87 @@ static void tdx_reclaim_control_page(unsigned long td_page_pa)free_page ((unsigned long )__va (td_page_pa)); } +struct tdx_flush_vp_arg { +struct kvm_vcpu *vcpu; +u64 err; +}; + +static void tdx_flush_vp (void *arg_) +{ +struct tdx_flush_vp_arg *arg = arg_; +struct kvm_vcpu *vcpu = arg->vcpu; +u64 err; + + arg->err =0 ; +lockdep_assert_irqs_disabled (); + +/* Task migration can race with CPU offlining. */ +if (unlikely (vcpu->cpu !=raw_smp_processor_id ())) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The +* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized.
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static void tdx_reclaim_control_page(unsigned long td_page_pa) · line 289
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +8
-0+*/ +if (is_td_vcpu_created (to_tdx (vcpu))) { +/* +* No need to retry. TDX Resources needed for TDH.VP.FLUSH are, +* TDVPR as exclusive, TDR as shared, and TDCS as shared. This +* vp flush function is called when destructing vcpu/TD or vcpu +* migration. No other thread uses TDVPR in those cases. +*/
Is it possible that other thread uses TDR or TDCS as exclusive?
Quoted 1 line
Quoted 1 line
Re: [PATCH v19 087/130] KVM: TDX: handle vcpu migration over logical processor
Quoted 33 lines · Binbin Wu
Quoted 32 lines · Reinette Chatre
Hi Isaku,
Quoted 1 line · On 2/26/2024 12:26 AM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
...
Quoted 26 lines
arch/x86/kvm/vmx/tdx.c +23
-0@@ -218,6 +257,87 @@ static void tdx_reclaim_control_page(unsigned long td_page_pa)free_page ((unsigned long )__va (td_page_pa)); } +struct tdx_flush_vp_arg { +struct kvm_vcpu *vcpu; +u64 err; +}; + +static void tdx_flush_vp (void *arg_) +{ +struct tdx_flush_vp_arg *arg = arg_; +struct kvm_vcpu *vcpu = arg->vcpu; +u64 err; + + arg->err =0 ; +lockdep_assert_irqs_disabled (); + +/* Task migration can race with CPU offlining. */ +if (unlikely (vcpu->cpu !=raw_smp_processor_id ())) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The +* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized. +*/
Quoted 8 lines
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static void tdx_reclaim_control_page(unsigned long td_page_pa) · line 289
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +7
-0+if (is_td_vcpu_created (to_tdx (vcpu))) { +/* +* No need to retry. TDX Resources needed for TDH.VP.FLUSH are, +* TDVPR as exclusive, TDR as shared, and TDCS as shared. This +* vp flush function is called when destructing vcpu/TD or vcpu +* migration. No other thread uses TDVPR in those cases. +*/ Is it possible that other thread uses TDR or TDCS as exclusive?
Exclusive lock is taken only when the guest creation or destruction. TDH.MNG.{ADDCX, CREATE, INIT, KEY.CONFIG, KEY.FREEID, FLUSHDONE}() TDH.MR.{EXTEND, FINALIZE}()TDH.MEM.PAGE.ADD()
TDH.VP.{CREATE, INIT, ADDCX, FLUSH}()
During run time (while vcpu can run), they are locked as shared.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 88/130 KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a flag, KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.
Opportunistically convert the KVM_DEBUGREG_* definitions to use BIT().
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -2
@@ -610,8 +610,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers (DR0-3 and DR6) are saved/restored by hardware +* on exit from or enter to guest. KVM needn't switch them. Because DR7 +* is cleared on exit from guest, DR7 need to be saved/restored. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -586,6 +586,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;
arch/x86/kvm/x86.c +8 -3
@@ -11013,7 +11013,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
@@ -11059,6 +11059,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
@@ -11071,8 +11072,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) { +if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 ); + } vcpu->arch.last_vmentry_cpu = vcpu->cpu; vcpu->arch.last_guest_tsc =kvm_read_l1_tsc (vcpu,rdtsc ());
Signature · 1 line
2.25.1
Re: [PATCH v19 088/130] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a flag,
KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags.
Here "irrespective of any other flags" sounds like other flags will be ignored if KVM_DEBUGREG_AUTO_SWITCHED_GUEST is set. But the code below doesn't align with it.
Quoted 73 lines
TDX-SEAM unconditionally saves and
restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.Opportunistically convert the KVM_DEBUGREG_* definitions to use
BIT().Reported-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Chao Gao <[email protected]>Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm_host.h +8
-2@@ -610,8 +610,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers (DR0-3 and DR6) are saved/restored by hardware +* on exit from or enter to guest. KVM needn't switch them. Because DR7 +* is cleared on exit from guest, DR7 need to be saved/restored. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {arch/x86/kvm/vmx/tdx.c +1
-0@@ -586,6 +586,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;arch/x86/kvm/x86.c +2
-1@@ -11013,7 +11013,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );@@ -11059,6 +11059,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11079
Quoted 10 lines
arch/x86/kvm/x86.c +5
-2@@ -11071,8 +11072,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) { +if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 );
According to TDX module 1.5 ABI spec:
DR0-3, DR6 and DR7 are set to their architectural INIT value, why is only DR7 restored?
I found a discussion about it in
https://lore.kernel.org/kvm/[email protected]/
Sean mentioned:
"
The TDX module context switches the guest _and_ host debug registers. It restores the host DRs because it needs to write _something_ to hide guest state, so it might as well restore the host values. The above was an optmization to avoid rewriting all debug registers.
"
I have question on "so it might as well restore the host values". Was it a guess/assumption or it was the fact?
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0+ } vcpu->arch.last_vmentry_cpu = vcpu->cpu; vcpu->arch.last_guest_tsc =kvm_read_l1_tsc (vcpu,rdtsc ());
Re: [PATCH v19 088/130] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
Quoted 11 lines · Binbin Wu
Quoted 4 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a flag,
KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags.Here "irrespective of any other flags" sounds like other flags will be ignored if
KVM_DEBUGREG_AUTO_SWITCHED_GUESTis set.
But the code below doesn't align with it.
Sure, let's update the commit message.
Quoted 73 lines
Quoted 73 lines
TDX-SEAM unconditionally saves and
restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.Opportunistically convert the KVM_DEBUGREG_* definitions to use
BIT().Reported-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Chao Gao <[email protected]>Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm_host.h +8
-2@@ -610,8 +610,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers (DR0-3 and DR6) are saved/restored by hardware +* on exit from or enter to guest. KVM needn't switch them. Because DR7 +* is cleared on exit from guest, DR7 need to be saved/restored. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {arch/x86/kvm/vmx/tdx.c +1
-0@@ -586,6 +586,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;arch/x86/kvm/x86.c +3
-3@@ -11013,7 +11013,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );@@ -11059,6 +11059,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);@@ -11071,8 +11072,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) {
Quoted 8 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11079
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 );According to TDX module 1.5 ABI spec:
DR0-3, DR6 and DR7 are set to their architectural INIT value, why is only DR7 restored?
This hunk should be dropped. Thank you for finding this.
I checked the base SPEC, the ABI spec, and the TDX module code. It seems the documentation bug of the TDX module 1.5 base architecture specification.
The TDX module code:
- restores guest DR<N> on TD Entry to guest.
- saves guest DR<N> on TD Exit from guest TD
- initializes DR<N> on TD Exit to host VMM
TDX module 1.5 base architecture specification:
15.1.2.1 Context Switch
By design, the Intel TDX module context-switches all debug/tracing state that the guest TD is allowed to use.
DR0-3, DR6 and IA32_DS_AREA MSR are context-switched in TDH.VP.ENTER and TD exit flows
RFLAGS, IA32_DEBUGCTL MSR and DR7 are saved and cleared on VM exits from the guest TD and restored on VM entry to the guest TD.
TDX module 1.5 ABI specification:
5.3.65. TDH.VP.ENTER Leaf
CPU State Preservation Following a Successful TD Entry and a TD Exit Following a successful TD entry and a TD exit, some CPU state is modified:
Registers DR0, DR1, DR2, DR3, DR6 and DR7 are set to their architectural INIT value.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v19 088/130] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]Quoted 59 lines · On 2/26/2024 4:26 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a flag,
KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.Opportunistically convert the KVM_DEBUGREG_* definitions to use
BIT().Reported-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Chao Gao <[email protected]>Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm_host.h +8
-2@@ -610,8 +610,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers (DR0-3 and DR6) are saved/restored by hardware +* on exit from or enter to guest. KVM needn't switch them. Because DR7 +* is cleared on exit from guest, DR7 need to be saved/restored. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {arch/x86/kvm/vmx/tdx.c +1
-0@@ -586,6 +586,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11016
Quoted 5 lines
arch/x86/kvm/x86.c +1
-1@@ -11013,7 +11013,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {
As pointed by Paolo in https://lore.kernel.org/lkml/[email protected]/KVM_DEBUGREG_BP_ENABLED could be set in vcpu->arch.switch_db_regs, by userspace kvm_vcpu_ioctl_x86_set_debugregs() --> kvm_update_dr7()
So it should be fixed as:
- if (unlikely(vcpu->arch.switch_db_regs)) {
+ if (unlikely(vcpu->arch.switch_db_regs &&
+ !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
Quoted 26 lines
arch/x86/kvm/x86.c +7
-2set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );@@ -11059,6 +11059,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);@@ -11071,8 +11072,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) { +if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 ); + } vcpu->arch.last_vmentry_cpu = vcpu->cpu; vcpu->arch.last_guest_tsc =kvm_read_l1_tsc (vcpu,rdtsc ());
Re: [PATCH v19 088/130] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
Quoted 16 lines · Binbin Wu
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11015
Quoted 9 lines
arch/x86/kvm/x86.c +1
-1@@ -11013,7 +11013,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {As pointed by Paolo in
https://lore.kernel.org/lkml/[email protected]/KVM_DEBUGREG_BP_ENABLEDcould be set in vcpu->arch.switch_db_regs, by userspace
kvm_vcpu_ioctl_x86_set_debugregs() -->kvm_update_dr7()
Quoted 6 lines
So it should be fixed as:
- if (
unlikely(vcpu->arch.switch_db_regs)) { + if (unlikely(vcpu->arch.switch_db_regs && + !(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH))) {
Yes, that's true. Thanks for catching this.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v19 89/130 KVM: TDX: Add support for find pending IRQ in a protected local APIC
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add flag and hook to KVM's local APIC management to support determining whether or not a TDX guest as a pending IRQ. For TDX vCPUs, the virtual APIC page is owned by the TDX module and cannot be accessed by KVM. As a result, registers that are virtualized by the CPU, e.g. PPR, cannot be read or written by KVM. To deliver interrupts for TDX guests, KVM must send an IRQ to the CPU on the posted interrupt notification vector. And to determine if TDX vCPU has a pending interrupt, KVM must check if there is an outstanding notification.
Return "no interrupt" in kvm_apic_has_interrupt() if the guest APIC is protected to short-circuit the various other flows that try to pull an IRQ out of the vAPIC, the only valid operation is querying _if_ an IRQ is pending, KVM can't do anything based on _which_ IRQ is pending.
Intentionally omit sanity checks from other flows, e.g. PPR update, so as not to degrade non-TDX guests with unnecessary checks. A well-behaved KVM and userspace will never reach those flows for TDX guests, but reaching them is not fatal if something does go awry.
Note, this doesn't handle interrupts that have been delivered to the vCPU but not yet recognized by the core, i.e. interrupts that are sitting in vmcs.GUEST_INTR_STATUS. Querying that state requires a SEAMCALL and will be supported in a future patch.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq.c | 3 +++ arch/x86/kvm/lapic.c | 3 +++ arch/x86/kvm/lapic.h | 2 ++ arch/x86/kvm/vmx/main.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 8 files changed, 28 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -124,6 +124,7 @@ KVM_X86_OP_OPTIONAL(pi_start_assignment) KVM_X86_OP_OPTIONAL(apicv_pre_state_restore) KVM_X86_OP_OPTIONAL(apicv_post_state_restore) KVM_X86_OP_OPTIONAL_RET0(dy_apicv_has_pending_interrupt) +KVM_X86_OP_OPTIONAL(protected_apic_has_interrupt) KVM_X86_OP_OPTIONAL(set_hv_timer) KVM_X86_OP_OPTIONAL(cancel_hv_timer) KVM_X86_OP(setup_mce)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1800,6 +1800,7 @@ struct kvm_x86_ops {void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*protected_apic_has_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/irq.c +3 -0
@@ -100,6 +100,9 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)if (kvm_cpu_has_extint (v))return 1 ; +if (lapic_in_kernel (v) && v->arch.apic->guest_apic_protected) +return static_call (kvm_x86_protected_apic_has_interrupt)(v); +return kvm_apic_has_interrupt (v) !=-1 ;/* LAPIC */ }EXPORT_SYMBOL_GPL (kvm_cpu_has_interrupt);
arch/x86/kvm/lapic.c +3 -0
@@ -2860,6 +2860,9 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)if (!kvm_apic_present (vcpu))return -1 ; +if (apic->guest_apic_protected) +return -1 ; +__apic_update_ppr (apic, &ppr);return apic_has_interrupt_for_ppr (apic, ppr); }
arch/x86/kvm/lapic.h +2 -0
@@ -66,6 +66,8 @@ struct kvm_lapic {bool sw_enabled;bool irr_pending;bool lvt0_in_nmi_mode; +/* Select registers in the vAPIC cannot be read/written. */ +bool guest_apic_protected;/* Number of bits set in ISR. */ s16 isr_count;/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
arch/x86/kvm/vmx/main.c +10 -0
@@ -78,6 +78,8 @@ static __init int vt_hardware_setup(void)if (enable_tdx) vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,sizeof (struct kvm_tdx )); +else + vt_x86_ops.protected_apic_has_interrupt =NULL ;#if IS_ENABLED (CONFIG_HYPERV )if (enable_tdx)
@@ -219,6 +221,13 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)vmx_vcpu_load (vcpu, cpu); } +static bool vt_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ +KVM_BUG_ON (!is_td_vcpu (vcpu), vcpu->kvm); + +return tdx_protected_apic_has_interrupt (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -443,6 +452,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, + .protected_apic_has_interrupt = vt_protected_apic_has_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr,
arch/x86/kvm/vmx/tdx.c +6 -0
@@ -583,6 +583,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return -EINVAL ;fpstate_set_confidential (&vcpu->arch.guest_fpu); + vcpu->arch.apic->guest_apic_protected = true; vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ;
@@ -624,6 +625,11 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)local_irq_enable (); } +bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ +return pi_has_pending_interrupt (vcpu); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -155,6 +155,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -194,6 +195,7 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) { return EXIT_FASTPstatic inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {} +static inline bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) {return false; }static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Edgecombe, Rick P
maintainerRe: [PATCH v19 089/130] KVM: TDX: Add support for find pending IRQ in a protected local APIC
arch/x86/kvm/vmx/main.c static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 230
Quoted 8 lines · On Mon, 2024-02-26 at 00:26 -0800, [email protected] wrote:
arch/x86/kvm/vmx/main.c +7
-0+static bool vt_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ + KVM_BUG_ON(!is_td_vcpu(vcpu), vcpu->kvm); + + return tdx_protected_apic_has_interrupt(vcpu); +} +
There was some internal discussion on whether to drop the KVM_BUG_ON() here, and then since vt_protected_apic_has_interrupt() would be just a simple call to tdx_protected_apic_has_interrupt(), just wire in tdx_protected_apic_has_interrupt() directly.
The reasoning for dropping the KVM_BUG_ON() is that the function has "protected" in its name so is unlikely to be called in another context. It was apparently added when the TDX series was still new. But with the more mature series it doesn't seem likely to find any bugs. The caller checks vcpu->arch.apic->guest_apic_protected.