kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Peter Gonda <pgonda@google.com>
Cc: kvm@vger.kernel.org, Marc Orr <marcorr@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	David Rientjes <rientjes@google.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	Brijesh Singh <brijesh.singh@amd.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH V11 3/5] KVM: SEV: Add support for SEV-ES intra host migration
Date: Thu, 4 Nov 2021 23:33:03 +0000	[thread overview]
Message-ID: <YYRtr4xINL4MkwGx@google.com> (raw)
In-Reply-To: <20211021174303.385706-4-pgonda@google.com>

On Thu, Oct 21, 2021, Peter Gonda wrote:
> ---
>  arch/x86/kvm/svm/sev.c | 50 +++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 49 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index 2c2f724c9096..d8ce93fd1129 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -1605,6 +1605,48 @@ static void sev_migrate_from(struct kvm_sev_info *dst,
>  	list_replace_init(&src->regions_list, &dst->regions_list);
>  }
>  
> +static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
> +{
> +	int i;
> +	struct kvm_vcpu *dst_vcpu, *src_vcpu;
> +	struct vcpu_svm *dst_svm, *src_svm;

What do you think about following the style of svm_vm_migrate_from(), where the
"dst" is simply "kvm"?  I like that because (a) it shortens all of these lines,
and (b) conveys the idea that the functions are running in the context of "this"
kvm, as opposed to being a third party that operates on an unrelated source and
destination.

> +
> +	if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
> +		return -EINVAL;
> +
> +	kvm_for_each_vcpu(i, src_vcpu, src) {
> +		if (!src_vcpu->arch.guest_state_protected)
> +			return -EINVAL;
> +	}
> +
> +	kvm_for_each_vcpu(i, src_vcpu, src) {
> +		src_svm = to_svm(src_vcpu);
> +		dst_vcpu = kvm_get_vcpu(dst, i);
> +		dst_svm = to_svm(dst_vcpu);
> +
> +		/*
> +		 * Transfer VMSA and GHCB state to the destination.  Nullify and
> +		 * clear source fields as appropriate, the state now belongs to
> +		 * the destination.
> +		 */
> +		dst_vcpu->vcpu_id = src_vcpu->vcpu_id;

vcpu_id is an odd thing to copy over.  That's fully controlled by userspace, and
is effectively immutable in KVM.  I don't think SEV-ES should be touching anything
besides SEV-ES state.

> +		dst_svm->sev_es = src_svm->sev_es;

Uber nit, maybe use memcpy() to "pair" with the memset() below?

> +		dst_svm->vmcb->control.ghcb_gpa =
> +			src_svm->vmcb->control.ghcb_gpa;
> +		dst_svm->vmcb->control.vmsa_pa = __pa(dst_svm->sev_es.vmsa);

Oof!  This _looks_ wrong, but it's not because dst_svm->sev_es.vmsa is copied
from the source in that subtle not-memcpy()-memcpy above.  The result of __pa()
absolutely will not change, so I would say just do the obvious

		dst_svm->vmcb->control.vmsa_pa = src_svm->vmcb->control.vmsa_pa;

and not force readers to think too hard.  That also avoids breakage if the order
is changed.

> +		dst_vcpu->arch.guest_state_protected = true;
> +
> +		memset(&src_svm->sev_es, 0, sizeof(src_svm->sev_es));
> +		src_svm->vmcb->control.ghcb_gpa = 0;
> +		src_svm->vmcb->control.vmsa_pa = 0;

'0' is not an invalid (G)PA.  INVALID_PAGE would be the most appropriate.

> +		src_vcpu->arch.guest_state_protected = false;
> +	}
> +	to_kvm_svm(src)->sev_info.es_active = false;
> +	to_kvm_svm(dst)->sev_info.es_active = true;
> +
> +	return 0;
> +}
> +
>  int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
>  {
>  	struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;

And if we do the above s/dst_//, do it here as well.

> @@ -1633,7 +1675,7 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
>  	if (ret)
>  		goto out_fput;
>  
> -	if (!sev_guest(source_kvm) || sev_es_guest(source_kvm)) {
> +	if (!sev_guest(source_kvm)) {
>  		ret = -EINVAL;
>  		goto out_source;
>  	}
> @@ -1644,6 +1686,12 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
>  	if (ret)
>  		goto out_source_vcpu;
>  
> +	if (sev_es_guest(source_kvm)) {
> +		ret = sev_es_migrate_from(kvm, source_kvm);
> +		if (ret)
> +			goto out_source_vcpu;
> +	}
> +
>  	sev_migrate_from(dst_sev, &to_kvm_svm(source_kvm)->sev_info);
>  	kvm_for_each_vcpu (i, vcpu, source_kvm) {
>  		kvm_vcpu_reset(vcpu, /* init_event= */ false);
> -- 
> 2.33.0.1079.g6e70778dc9-goog
> 

  reply	other threads:[~2021-11-04 23:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-21 17:42 [PATCH 0/5 V11] Add AMD SEV and SEV-ES intra host migration support Peter Gonda
2021-10-21 17:42 ` [PATCH 1/5 V11] KVM: SEV: Refactor out sev_es_state struct Peter Gonda
2021-11-04 20:06   ` Sean Christopherson
2021-10-21 17:43 ` [PATCH V11 2/5] KVM: SEV: Add support for SEV intra host migration Peter Gonda
2021-11-04 22:07   ` Sean Christopherson
2021-11-04 23:04     ` Sean Christopherson
2021-11-09 15:19     ` Peter Gonda
2021-11-11 15:17     ` Paolo Bonzini
2021-11-11 15:18     ` Paolo Bonzini
2021-10-21 17:43 ` [PATCH V11 3/5] KVM: SEV: Add support for SEV-ES " Peter Gonda
2021-11-04 23:33   ` Sean Christopherson [this message]
2021-10-21 17:43 ` [PATCH V11 4/5] selftest: KVM: Add open sev dev helper Peter Gonda
2021-11-05 22:47   ` Sean Christopherson
2021-10-21 17:43 ` [PATCH V11 5/5] selftest: KVM: Add intra host migration tests Peter Gonda

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=YYRtr4xINL4MkwGx@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=brijesh.singh@amd.com \
    --cc=dgilbert@redhat.com \
    --cc=hpa@zytor.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcorr@google.com \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=pgonda@google.com \
    --cc=rientjes@google.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.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;
as well as URLs for NNTP newsgroup(s).