mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yan Zhao <yan.y.zhao@intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	<linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>,
	"Isaku Yamahata" <isaku.yamahata@intel.com>,
	Tony Lindgren <tony.lindgren@linux.intel.com>,
	Sean Christopherson <sean.j.christopherson@intel.com>,
	Kai Huang <kai.huang@intel.com>
Subject: Re: [PATCH 20/30] KVM: TDX: create/destroy VM structure
Date: Fri, 21 Feb 2025 19:04:08 +0800	[thread overview]
Message-ID: <Z7hdqCqOLJWcR71K@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <Z7fO9gqzgaETeMYB@google.com>

On Thu, Feb 20, 2025 at 04:55:18PM -0800, Sean Christopherson wrote:
> TL;DR: Please don't merge this patch to kvm/next or kvm/queue.
> 
> On Thu, Feb 20, 2025, Paolo Bonzini wrote:
> > @@ -72,8 +94,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
> >  	.has_emulated_msr = vmx_has_emulated_msr,
> >  
> >  	.vm_size = sizeof(struct kvm_vmx),
> > -	.vm_init = vmx_vm_init,
> > -	.vm_destroy = vmx_vm_destroy,
> > +
> > +	.vm_init = vt_vm_init,
> > +	.vm_destroy = vt_vm_destroy,
> > +	.vm_free = vt_vm_free,
> >  
> >  	.vcpu_precreate = vmx_vcpu_precreate,
> >  	.vcpu_create = vmx_vcpu_create,
> 
> ...
> 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 374d89e6663f..e0b9b845df58 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -12884,6 +12884,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
> >  	kvm_page_track_cleanup(kvm);
> >  	kvm_xen_destroy_vm(kvm);
> >  	kvm_hv_destroy_vm(kvm);
> > +	static_call_cond(kvm_x86_vm_free)(kvm);
> >  }
> 
> Sorry to throw a wrench in things, but I have a fix that I want to send for 6.14[1],
> i.e. before this code, and to land that fix I need/want to destroy vCPUs before
> calling kvm_x86_ops.vm_destroy().  *sigh*
> 
> The underlying issue is that both nVMX and nSVM suck and access all manner of VM-wide
> state when destroying a vCPU that is currently in nested guest mode, and I want
> to fix the most pressing issue of destroying vCPUs at a random time once and for
> all.  nVMX and nSVM also need to be cleaned up to not access so much darn state,
> but I'm worried that "fixing" the nested cases will only whack the biggest mole.
> 
> Commit 6fcee03df6a1 ("KVM: x86: avoid loading a vCPU after .vm_destroy was called")
> papered over an AVIC case, but there are issues, e.g. with the MSR filters[2],
> and the NULL pointer deref that's blocking the aforementioned fix is a nVMX access
> to the PIC.
> 
> I haven't fully tested destroying vCPUs before calling vm_destroy(), but I can't
> see anything in vmx_vm_destroy() or svm_vm_destroy() that expects to run while
> vCPUs are still alive.  If anything, it's opposite, e.g. freeing VMX's IPIv PID
> table before vCPUs are destroyed is blatantly unsafe.
Is it possible to simply move the code like freeing PID table from
vmx_vm_destroy() to static_call_cond(kvm_x86_vm_free)(kvm) ?

> 
> The good news is, I think it'll lead to a better approach (and naming).  KVM already
> frees MMU state before vCPU state, because while MMUs are largely VM-scoped, all
> of the common MMU state needs to be freed before any one vCPU is freed.
> 
> And so my plan is to carved out a kvm_destroy_mmus() helper, which can then call
> the TDX hook to release/reclaim the HKID, which I assume needs to be done after
> KVM's general MMU destruction, but before vCPUs are freed.
> 
> I'll make sure to Cc y'all on the series (typing and testing furiously to try and
> get it out asap).  But to try and avoid posting code that's not usable for TDX,
> will this work?
> 
> static void kvm_destroy_mmus(struct kvm *kvm)
> {
> 	struct kvm_vcpu *vcpu;
> 	unsigned long i;
> 
> 	if (current->mm == kvm->mm) {
> 		/*
> 		 * Free memory regions allocated on behalf of userspace,
> 		 * unless the memory map has changed due to process exit
> 		 * or fd copying.
> 		 */
> 		mutex_lock(&kvm->slots_lock);
> 		__x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
> 					0, 0);
> 		__x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
> 					0, 0);
> 		__x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
> 		mutex_unlock(&kvm->slots_lock);
> 	}
> 
> 	kvm_for_each_vcpu(i, vcpu, kvm) {
> 		kvm_clear_async_pf_completion_queue(vcpu);
> 		kvm_unload_vcpu_mmu(vcpu);
> 	}
> 
> 	kvm_x86_call(mmu_destroy)(kvm);
I suppose this will hook tdx_mmu_release_hkid() ?

> }
> 
> void kvm_arch_pre_destroy_vm(struct kvm *kvm)
> {
> 	kvm_mmu_pre_destroy_vm(kvm);
> }
> 
> void kvm_arch_destroy_vm(struct kvm *kvm)
> {
> 	/*
> 	 * WARNING!  MMUs must be destroyed before vCPUs, and vCPUs must be
> 	 * destroyed before any VM state.  Most MMU state is VM-wide, but is
> 	 * tracked per-vCPU, and so must be unloaded/freed in its entirety
> 	 * before any one vCPU is destroyed.  For all other VM state, vCPUs
> 	 * expect to be able to access VM state until the vCPU is freed.
> 	 */
> 	kvm_destroy_mmus(kvm);
> 	kvm_destroy_vcpus(kvm);
> 	kvm_x86_call(vm_destroy)(kvm);
> 
> 	...
> }
> 
> [1] https://lore.kernel.org/all/Z66RC673dzlq2YuA@google.com
> [2] https://lore.kernel.org/all/20240703175618.2304869-2-aaronlewis@google.com

  parent reply	other threads:[~2025-02-21 11:06 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-20 17:05 [PATCH v3 00/30] TDX initialization + vCPU/VM creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 01/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX KeyID management Paolo Bonzini
2025-02-20 17:05 ` [PATCH 02/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX TD creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 03/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX vCPU creation Paolo Bonzini
2025-02-20 17:05 ` [PATCH 04/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX page cache management Paolo Bonzini
2025-02-20 17:05 ` [PATCH 05/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX VM/vCPU field access Paolo Bonzini
2025-02-20 17:05 ` [PATCH 06/30] x86/virt/tdx: Add SEAMCALL wrappers for TDX flush operations Paolo Bonzini
2025-02-20 17:05 ` [PATCH 07/30] x86/virt/tdx: allocate tdx_sys_info in static memory Paolo Bonzini
2025-02-20 21:59   ` Huang, Kai
2025-02-20 23:37   ` Edgecombe, Rick P
2025-02-20 17:05 ` [PATCH 08/30] x86/virt/tdx: Read essential global metadata for KVM Paolo Bonzini
2025-02-20 17:05 ` [PATCH 09/30] x86/virt/tdx: Add tdx_guest_keyid_alloc/free() to alloc and free TDX guest KeyID Paolo Bonzini
2025-02-20 17:05 ` [PATCH 10/30] KVM: Export hardware virtualization enabling/disabling functions Paolo Bonzini
2025-02-20 17:05 ` [PATCH 11/30] KVM: VMX: Refactor VMX module init/exit functions Paolo Bonzini
2025-02-20 21:55   ` Huang, Kai
2025-02-20 17:05 ` [PATCH 12/30] KVM: VMX: Initialize TDX during KVM module load Paolo Bonzini
2025-02-20 23:27   ` Huang, Kai
2025-02-24 18:57     ` Paolo Bonzini
2025-02-24 21:31       ` Huang, Kai
2025-02-20 17:05 ` [PATCH 13/30] KVM: TDX: Get TDX global information Paolo Bonzini
2025-02-21  0:12   ` Huang, Kai
2025-02-20 17:05 ` [PATCH 14/30] KVM: TDX: Add placeholders for TDX VM/vCPU structures Paolo Bonzini
2025-02-20 17:05 ` [PATCH 15/30] KVM: TDX: Define TDX architectural definitions Paolo Bonzini
2025-02-20 17:05 ` [PATCH 16/30] KVM: TDX: Add TDX "architectural" error codes Paolo Bonzini
2025-02-20 17:05 ` [PATCH 17/30] KVM: TDX: Add helper functions to print TDX SEAMCALL error Paolo Bonzini
2025-02-20 17:05 ` [PATCH 18/30] KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl Paolo Bonzini
2025-02-25 10:50   ` Huang, Kai
2025-02-20 17:05 ` [PATCH 19/30] KVM: TDX: Get system-wide info about TDX module on initialization Paolo Bonzini
2025-02-20 17:05 ` [PATCH 20/30] KVM: TDX: create/destroy VM structure Paolo Bonzini
2025-02-21  0:55   ` Sean Christopherson
2025-02-21  1:08     ` Sean Christopherson
2025-02-22  0:30       ` Edgecombe, Rick P
2025-02-22  1:38         ` Sean Christopherson
2025-02-24  8:32           ` Yan Zhao
2025-02-21 11:04     ` Yan Zhao [this message]
2025-02-21 19:43       ` Sean Christopherson
2025-02-21 12:25   ` Yan Zhao
2025-02-25 16:24   ` Xiaoyao Li
2025-02-20 17:05 ` [PATCH 21/30] KVM: TDX: Support per-VM KVM_CAP_MAX_VCPUS extension check Paolo Bonzini
2025-02-20 17:05 ` [PATCH 22/30] KVM: x86: expose cpuid_entry2_find for TDX Paolo Bonzini
2025-02-20 17:05 ` [PATCH 23/30] KVM: TDX: initialize VM with TDX specific parameters Paolo Bonzini
2025-02-21  2:31   ` Xiaoyao Li
2025-02-25 17:28     ` Paolo Bonzini
2025-02-20 17:05 ` [PATCH 24/30] KVM: TDX: Make pmu_intel.c ignore guest TD case Paolo Bonzini
2025-02-20 17:05 ` [PATCH 25/30] KVM: TDX: Don't offline the last cpu of one package when there's TDX guest Paolo Bonzini
2025-02-20 17:06 ` [PATCH 26/30] KVM: TDX: create/free TDX vcpu structure Paolo Bonzini
2025-02-20 17:06 ` [PATCH 27/30] KVM: TDX: Do TDX specific vcpu initialization Paolo Bonzini
2025-02-20 17:06 ` [PATCH 28/30] KVM: x86: Introduce KVM_TDX_GET_CPUID Paolo Bonzini
2025-02-20 17:06 ` [PATCH 29/30] KVM: x86/mmu: Taking guest pa into consideration when calculate tdp level Paolo Bonzini
2025-02-20 17:06 ` [PATCH 30/30] KVM: TDX: Register TDX host key IDs to cgroup misc controller Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z7hdqCqOLJWcR71K@yzhao56-desk.sh.intel.com \
    --to=yan.y.zhao@intel.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=sean.j.christopherson@intel.com \
    --cc=seanjc@google.com \
    --cc=tony.lindgren@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®