Re: [PATCH 1/2] KVM: SVM: Serialize the owner's mirror list in sev_migrate_from()
Paolo Bonzini <[email protected]>
| Newsgroups | org.kernel.vger.kvm |
|---|---|
| Message-ID | <[email protected]> |
On 8/4/26 16:54, Shen Yongchao wrote:
> In sev_migrate_from(), when the destination KVM is a mirror, the mirror
> entry is moved from the source's list to the owner's mirror_vms list
> without holding the owner's lock. All other writers of the owner's
> mirror list (KVM_SEV_MIRROR_CREATE, sev_vm_destroy()) serialize on the
> owner's lock, so a concurrent COPY or destroy can race with
> KVM_SEV_MIGRATE_VM and corrupt the list, e.g. leaving a dangling
> mirror_entry that is later dereferenced.
>
> All kernels with SEV mirroring and intra-host migration support
> (KVM_SEV_MIRROR_CREATE / KVM_SEV_MIGRATE_VM) are affected.
More precisely, KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM and
KVM_CAP_VM_COPY_ENC_CONTEXT_FROM.
> The mirror
> list mechanism was introduced by commit b2125513dfc0 ("KVM: SEV: Allow
> SEV intra-host migration of VM with mirrors", v5.18, 2022-02-18);
> upstream mainline is unfixed as of 2026-08-04. Triggering requires an
> AMD SEV-capable host with CONFIG_KVM_AMD_SEV and a local user able to
> create SEV VMs and mirrors, racing KVM_SEV_MIGRATE_VM against
> KVM_SEV_MIRROR_CREATE or mirror destroy (instruction-level window).
>
> Take the owner's lock around the list operations. The lock is taken
> after sev_lock_two_vms() and the migration_in_progress flags prevent
> two migrations from interleaving. The owner is a third VM whose lock
> is not held: the destination cannot already own an encryption context
> (sev_vm_move_enc_context_from() rejects SEV guests as the destination,
> gating on ->active), and a VM cannot be its own mirror owner. Hence
> SINGLE_DEPTH_NESTING is sufficient and cannot deadlock.
You need to use "2", because SINGLE_DEPTH_NESTING is already taken by
sev_lock_two_vms(). I adjusted a bit the comment to include this
information:
* Writes to the list of mirrors take the owner's lock. It is
* not possible to have a deadlock, because the owner's lock is always
* taken (e.g. by KVM_CAP_VM_COPY_ENC_CONTEXT_FROM) inside the
* mirror's.
and applied the patch, thanks.
Paolo