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>,
Brijesh Singh <brijesh.singh@amd.com>,
stable@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: SEV: Acquire vcpu mutex when updating VMSA
Date: Tue, 14 Sep 2021 21:33:52 +0000 [thread overview]
Message-ID: <YUEVQDEvLbdJF+sj@google.com> (raw)
In-Reply-To: <20210914200639.3305617-1-pgonda@google.com>
On Tue, Sep 14, 2021, Peter Gonda wrote:
> Adds mutex guard to the VMSA updating code. Also adds a check to skip a
> vCPU if it has already been LAUNCH_UPDATE_VMSA'd which should allow
> userspace to retry this ioctl until all the vCPUs can be successfully
> LAUNCH_UPDATE_VMSA'd. Because this operation cannot be undone we cannot
> unwind if one vCPU fails.
>
> Fixes: ad73109ae7ec ("KVM: SVM: Provide support to launch and run an SEV-ES guest")
>
> Signed-off-by: Peter Gonda <pgonda@google.com>
> Cc: Marc Orr <marcorr@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Brijesh Singh <brijesh.singh@amd.com>
> Cc: kvm@vger.kernel.org
> Cc: stable@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
> arch/x86/kvm/svm/sev.c | 24 +++++++++++++++++++-----
> 1 file changed, 19 insertions(+), 5 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index 75e0b21ad07c..9a2ebd0328ca 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -598,22 +598,29 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)
> static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
> {
> struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
> - struct sev_data_launch_update_vmsa vmsa;
> + struct sev_data_launch_update_vmsa vmsa = {0};
> struct kvm_vcpu *vcpu;
> int i, ret;
>
> if (!sev_es_guest(kvm))
> return -ENOTTY;
>
> - vmsa.reserved = 0;
> -
Zeroing all of 'vmsa' is an unrelated chagne and belongs in a separate patch. I
would even go so far as to say it's unnecessary, even field of the struct is
explicitly written before it's consumed.
> kvm_for_each_vcpu(i, vcpu, kvm) {
> struct vcpu_svm *svm = to_svm(vcpu);
>
> + ret = mutex_lock_killable(&vcpu->mutex);
> + if (ret)
> + goto out_unlock;
Rather than multiple unlock labels, move the guts of the loop to a wrapper.
As discussed off list, this really should be a vCPU-scoped ioctl, but that ship
has sadly sailed :-( We can at least imitate that by making the VM-scoped ioctl
nothing but a wrapper.
> +
> + /* Skip to the next vCPU if this one has already be updated. */
s/be/been
Uber nit, there may not be a next vCPU. It'd be more slightly more accurate to
say something like "Do nothing if this vCPU has already been updated".
> + ret = sev_es_sync_vmsa(svm);
> + if (svm->vcpu.arch.guest_state_protected)
> + goto unlock;
This belongs in a separate patch, too. It also introduces a bug (arguably two)
in that it adds a duplicate call to sev_es_sync_vmsa(). The second bug is that
if sev_es_sync_vmsa() fails _and_ the vCPU is already protected, this will cause
that failure to be squashed.
In the end, I think the least gross implementation will look something like this,
implemented over two patches (one for the lock, one for the protected check).
static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
int *error)
{
struct sev_data_launch_update_vmsa vmsa;
struct vcpu_svm *svm = to_svm(vcpu);
int ret;
/*
* Do nothing if this vCPU has already been updated. This is allowed
* to let userspace retry LAUNCH_UPDATE_VMSA if the command fails on a
* later vCPU.
*/
if (svm->vcpu.arch.guest_state_protected)
return 0;
/* Perform some pre-encryption checks against the VMSA */
ret = sev_es_sync_vmsa(svm);
if (ret)
return ret;
/*
* The LAUNCH_UPDATE_VMSA command will perform in-place
* encryption of the VMSA memory content (i.e it will write
* the same memory region with the guest's key), so invalidate
* it first.
*/
clflush_cache_range(svm->vmsa, PAGE_SIZE);
vmsa.reserved = 0;
vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
vmsa.address = __sme_pa(svm->vmsa);
vmsa.len = PAGE_SIZE;
return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
}
static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
{
struct kvm_vcpu *vcpu;
int i, ret;
if (!sev_es_guest(kvm))
return -ENOTTY;
kvm_for_each_vcpu(i, vcpu, kvm) {
ret = mutex_lock_killable(&vcpu->mutex);
if (ret)
return ret;
ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
mutex_unlock(&vcpu->mutex);
if (ret)
return ret;
}
return 0;
}
next prev parent reply other threads:[~2021-09-14 21:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-14 20:06 [PATCH] KVM: SEV: Acquire vcpu mutex when updating VMSA Peter Gonda
2021-09-14 21:33 ` Sean Christopherson [this message]
2021-09-14 22:39 ` Peter Gonda
2021-09-14 23:43 ` Sean Christopherson
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=YUEVQDEvLbdJF+sj@google.com \
--to=seanjc@google.com \
--cc=brijesh.singh@amd.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcorr@google.com \
--cc=pbonzini@redhat.com \
--cc=pgonda@google.com \
--cc=stable@vger.kernel.org \
/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).