linux-kernel.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, Lars Bull <larsbull@google.com>,
	Brijesh Singh <brijesh.singh@amd.com>,
	Marc Orr <marcorr@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	David Rientjes <rientjes@google.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.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 2/3 V3] KVM, SEV: Add support for SEV intra host migration
Date: Thu, 29 Jul 2021 22:17:46 +0000	[thread overview]
Message-ID: <YQMpChJVo13/Njnc@google.com> (raw)
In-Reply-To: <20210726195015.2106033-3-pgonda@google.com>

On Mon, Jul 26, 2021, Peter Gonda wrote:
> To avoid exposing this internal state to userspace and prevent other
> processes from importing state they shouldn't have access to, the send
> returns a token to userspace that is handed off to the target VM. The
> target passes in this token to receive the sent state. The token is only
> valid for one-time use. Functionality on the source becomes limited
> after send has been performed. If the source is destroyed before the
> target has received, the token becomes invalid.

...

> +11. KVM_SEV_INTRA_HOST_RECEIVE
> +-------------------------------------
> +
> +The KVM_SEV_INTRA_HOST_RECEIVE command is used to transfer staged SEV
> +info to a target VM from some source VM. SEV on the target VM should be active
> +when receive is performed, but not yet launched and without any pinned memory.
> +The launch commands should be skipped after receive because they should have
> +already been performed on the source.
> +
> +Parameters (in/out): struct kvm_sev_intra_host_receive
> +
> +Returns: 0 on success, -negative on error
> +
> +::
> +
> +    struct kvm_sev_intra_host_receive {
> +        __u64 info_token;    /* token referencing the staged info */

Sorry to belatedly throw a wrench in things, but why use a token approach?  This
is only intended for migrating between two userspace VMMs using the same KVM 
module, which can access both the source and target KVM instances (VMs/guests).
Rather than indirectly communicate through a token, why not communidate directly?
Same idea as svm_vm_copy_asid_from().

The locking needs special consideration, e.g. attempting to take kvm->lock on
both the source and dest could deadlock if userspace is malicious and
double-migrates, but I think a flag and global spinlock to state that migration
is in-progress would suffice.                                                                                 

Locking aside, this would reduce the ABI to a single ioctl(), should avoid most 
if not all temporary memory allocations, and would obviate the need for patch 1 
since there's no limbo state, i.e. the encrypted regions are either owned by the
source or the dest.

I think the following would work?  Another thought would be to make the helpers
and "lock for multi-lock" flag arch-agnostic, e.g. the logic below works iff
this is the only path that takes two kvm->locks simultaneous.

static int svm_sev_lock_for_migration(struct kvm *kvm)
{
	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
	int ret = 0;

	/*
	 * Bail if this VM is already involved in a migration to avoid deadlock
	 * between two VMs trying to migrate to/from each other.
	 */
	spin_lock(&sev_migration_lock);
	if (sev->migration_in_progress)
		ret = -EINVAL;
	else
		sev->migration_in_progress = true;
	spin_unlock(&sev_migration_lock);

	if (!ret)
		mutex_lock(&kvm->lock);

	return ret;
}

static void svm_unlock_after_migration(struct kvm *kvm)
{
	mutex_unlock(&kvm->lock);
	WRITE_ONCE(sev->migration_in_progress, false);
}

int svm_sev_migrate_from(struct kvm *kvm, unsigned int source_fd)
{
	struct file *source_kvm_file;
	struct kvm *source_kvm;
	int ret = -EINVAL;

	ret = svm_sev_lock_for_migration(kvm);
	if (ret)
		return ret;

	if (!sev_guest(kvm))
		goto out_unlock;

	source_kvm_file = fget(source_fd);
	if (!file_is_kvm(source_kvm_file)) {
		ret = -EBADF;
		goto out_fput;
	}

	source_kvm = source_kvm_file->private_data;
	ret = svm_sev_lock_for_migration(source_kvm);
	if (ret)
		goto out_fput;

	if (!sev_guest(source_kvm)) {
		ret = -EINVAL;
		goto out_source;
	}

	<migration magic>

out_source:
	svm_unlock_after_migration(&source_kvm->lock);
out_fpu:
	if (source_kvm_file)
		fput(source_kvm_file);
out_unlock:
	svm_unlock_after_migration(kvm);
	return ret;
}

  reply	other threads:[~2021-07-29 22:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-26 19:50 [PATCH 0/3 V3] Add AMD SEV and SEV-ES intra host migration support Peter Gonda
2021-07-26 19:50 ` [PATCH 1/3 V3] KVM, SEV: Refactor out function for unregistering encrypted regions Peter Gonda
2021-07-29 21:17   ` Sean Christopherson
2021-07-26 19:50 ` [PATCH 2/3 V3] KVM, SEV: Add support for SEV intra host migration Peter Gonda
2021-07-29 22:17   ` Sean Christopherson [this message]
2021-07-26 19:50 ` [PATCH 3/3 V3] KVM, SEV: Add support for SEV-ES " 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=YQMpChJVo13/Njnc@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=larsbull@google.com \
    --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=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    --subject='Re: [PATCH 2/3 V3] KVM, SEV: Add support for SEV intra host migration' \
    /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

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).