mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryan Afranji <afranji@google.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org
Cc: sagis@google.com, bp@alien8.de, chao.p.peng@linux.intel.com,
	 dave.hansen@linux.intel.com, dmatlack@google.com,
	erdemaktas@google.com,  isaku.yamahata@intel.com,
	kai.huang@intel.com, mingo@redhat.com,  pbonzini@redhat.com,
	seanjc@google.com, tglx@linutronix.de,  zhi.wang.linux@gmail.com,
	ackerleytng@google.com, andrew.jones@linux.dev,
	 david@redhat.com, hpa@zytor.com,
	kirill.shutemov@linux.intel.com,
	 linux-kselftest@vger.kernel.org, tabba@google.com,
	vannapurve@google.com,  yan.y.zhao@intel.com,
	rick.p.edgecombe@intel.com,  Ryan Afranji <afranji@google.com>
Subject: [RFC PATCH v2 02/10] KVM: x86: Adjust locking order in move_enc_context_from
Date: Wed, 11 Jun 2025 21:16:29 +0000	[thread overview]
Message-ID: <ee22d844512a828dc5285a93676699d1aca0e0ed.1749672978.git.afranji@google.com> (raw)
In-Reply-To: <cover.1749672978.git.afranji@google.com>

Previously, the order for acquiring the locks required for the migration
function move_enc_context_from() was: 1) memslot lock 2) vCPU lock. This
can trigger a deadlock warning because a vCPU IOCTL modifying memslots
will acquire the locks in reverse order: 1) vCPU lock 2) memslot lock.

This patch adjusts move_enc_context_from() to match vCPU IOCTL’s locking
order to prevent deadlock warnings.

Signed-off-by: Ryan Afranji <afranji@google.com>
---
 arch/x86/kvm/svm/sev.c | 13 +------------
 arch/x86/kvm/x86.c     | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 402543994b0b..380d5951f8dd 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -1961,26 +1961,15 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, struct kvm *source_kvm)
 		charged = true;
 	}
 
-	ret = kvm_lock_all_vcpus(kvm);
-	if (ret)
-		goto out_dst_cgroup;
-	ret = kvm_lock_all_vcpus(source_kvm);
-	if (ret)
-		goto out_dst_vcpu;
-
 	ret = sev_check_source_vcpus(kvm, source_kvm);
 	if (ret)
-		goto out_source_vcpu;
+		goto out_dst_cgroup;
 
 	sev_migrate_from(kvm, source_kvm);
 	kvm_vm_dead(source_kvm);
 	cg_cleanup_sev = src_sev;
 	ret = 0;
 
-out_source_vcpu:
-	kvm_unlock_all_vcpus(source_kvm);
-out_dst_vcpu:
-	kvm_unlock_all_vcpus(kvm);
 out_dst_cgroup:
 	/* Operates on the source on success, on the destination on failure.  */
 	if (charged)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b1672379a16b..c28fa28a8e42 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6743,10 +6743,18 @@ static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
 	if (r)
 		goto out_mark_migration_done;
 
-	r = kvm_lock_vm_memslots(kvm, source_kvm);
+	r = kvm_lock_all_vcpus(kvm);
 	if (r)
 		goto out_unlock;
 
+	r = kvm_lock_all_vcpus(source_kvm);
+	if (r)
+		goto out_unlock_vcpus;
+
+	r = kvm_lock_vm_memslots(kvm, source_kvm);
+	if (r)
+		goto out_unlock_source_vcpus;
+
 	r = kvm_move_memory_ctxt_from(kvm, source_kvm);
 	if (r)
 		goto out_unlock_memslots;
@@ -6762,6 +6770,10 @@ static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
 
 out_unlock_memslots:
 	kvm_unlock_vm_memslots(kvm, source_kvm);
+out_unlock_source_vcpus:
+	kvm_unlock_all_vcpus(source_kvm);
+out_unlock_vcpus:
+	kvm_unlock_all_vcpus(kvm);
 out_unlock:
 	kvm_unlock_two_vms(kvm, source_kvm);
 out_mark_migration_done:
-- 
2.50.0.rc1.591.g9c95f17f64-goog


  parent reply	other threads:[~2025-06-11 21:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-11 21:16 [RFC PATCH v2 00/10] Add TDX intra-host migration support Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 01/10] KVM: Split tdp_mmu_pages to mirror and direct counters Ryan Afranji
2025-06-11 21:16 ` Ryan Afranji [this message]
2025-06-11 21:16 ` [RFC PATCH v2 03/10] KVM: TDX: Add base implementation for tdx_vm_move_enc_context_from Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 04/10] KVM: TDX: Implement moving mirror pages between 2 TDs Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 05/10] KVM: TDX: Allow vCPUs to be created for migration Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 06/10] KVM: TDX: Add core logic for TDX intra-host migration Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 07/10] KVM: selftests: Refactor userspace_mem_region creation out of vm_mem_add Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 08/10] KVM: selftests: TDX: Add tests for TDX in-place migration Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 09/10] KVM: selftests: Add TDX support for ucalls Ryan Afranji
2025-06-11 21:16 ` [RFC PATCH v2 10/10] KVM: selftests: Add irqfd/interrupts test for TDX with migration Ryan Afranji
2026-03-09 14:17 ` [RFC PATCH v2 00/10] Add TDX intra-host migration support Sean Christopherson
2026-03-09 15:04   ` Vishal Annapurve

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=ee22d844512a828dc5285a93676699d1aca0e0ed.1749672978.git.afranji@google.com \
    --to=afranji@google.com \
    --cc=ackerleytng@google.com \
    --cc=andrew.jones@linux.dev \
    --cc=bp@alien8.de \
    --cc=chao.p.peng@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=dmatlack@google.com \
    --cc=erdemaktas@google.com \
    --cc=hpa@zytor.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=sagis@google.com \
    --cc=seanjc@google.com \
    --cc=tabba@google.com \
    --cc=tglx@linutronix.de \
    --cc=vannapurve@google.com \
    --cc=x86@kernel.org \
    --cc=yan.y.zhao@intel.com \
    --cc=zhi.wang.linux@gmail.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

all inboxes | Powered by JetHome®