archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <>
To: Peter Gonda <>
Cc:, Lars Bull <>,
	Brijesh Singh <>,
	Marc Orr <>,
	Paolo Bonzini <>,
	David Rientjes <>,
	"Dr . David Alan Gilbert" <>,
	Vitaly Kuznetsov <>,
	Wanpeng Li <>,
	Jim Mattson <>, Joerg Roedel <>,
	Thomas Gleixner <>,
	Ingo Molnar <>, Borislav Petkov <>,
	"H. Peter Anvin" <>,
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/> (raw)
In-Reply-To: <>

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.


> +-------------------------------------
> +
> +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.
	if (sev->migration_in_progress)
		ret = -EINVAL;
		sev->migration_in_progress = true;

	if (!ret)

	return ret;

static void svm_unlock_after_migration(struct kvm *kvm)
	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>

	if (source_kvm_file)
	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:

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

  git send-email \
    --in-reply-to=YQMpChJVo13/ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
    --subject='Re: [PATCH 2/3 V3] KVM, SEV: Add support for SEV intra host migration' \

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