From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9F884746CC for ; Mon, 31 Aug 2026 18:32:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788201126; cv=none; b=U+RN2/IFitVtgrde5DzWn6CH8/0hbV6ZX1z6SiuU9FeaXyuAWeaCeFlnDrD1yGxs/Z4hmtOxzs944SbgJ+tAYfzSdJjbzxsWL2xeV/VBzxcjqfaDd+d5l3OP/BPowiQu4JefywsOHvTctw5gjsUbdj0rmwuipeOlw9WVhLETtZ8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788201126; c=relaxed/simple; bh=iNXBruw9D1kyVJTag13tx7am3b8B5uSCWVY+ZvmNGAU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O+uqCTSlHW+kUUkl6WHIING/UCzaTPT3AYofDiizLF6vzAVTsdMQRXp9I90FjsdOlXb1Wovgp/GszXjR2WZmheIi7o/yfHaKB35xqm8I4wxIN4TpnFF5YUIJjMgAxLS3TefTEbewcpMZijKIGTV1Q2ne31yFNmOgJ+CC7iWcdSE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hN+NKM/E; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hN+NKM/E" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 458211F00A3F; Mon, 31 Aug 2026 18:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788201124; bh=gQ2sxhSLRDaa92ZRKZBmJkinqHs+nF7rInNzQNqvqh0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hN+NKM/EldlNF2GzfYKTqbnB3aveJuFxPlJ8HJTOMvf/0zrwRKyQz2+P6SoGsIJd9 YxvkROwrCqWoE6dL08qNAKM/DjyyPMRgrLfNMVLAu6T+s6hu6AJDRlCfmO7L4weu0G U1Akp3WvzcYBai/j4ldovGplKVN4AqnVFsCRcthNEM20dOyNwOetSSsdaZyCKzpRqs +oZg+AxlhXuFCsHYk6uZbPuko/PzaUy6WSEbcDsffYnadiPbucY1QhdtmhGxcqSb2x 4HvLOST+pEfVjJo03b5BZ5Q6THFyQ2yEbL6DT1kpOYBfTiGZxlEoSjnftAlT/6V3an CqV0FzfGFyP0A== From: Sasha Levin To: stable@vger.kernel.org Cc: Sean Christopherson , Michael Roth , Sasha Levin Subject: [PATCH 6.12.y 6/6] KVM: SEV: Track the GPA of the guest-controlled VMSA used for SNP guests Date: Mon, 31 Aug 2026 14:31:57 -0400 Message-ID: <20260831183157.1673215-6-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260831183157.1673215-1-sashal@kernel.org> References: <2026083106-utensil-mangy-28f5@gregkh> <20260831183157.1673215-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson [ Upstream commit 42a39ad5d592aec87a70527a4e694f6210694482 ] Track the GPA of the guest-provided VMSA used after AP_CREATION events when running SNP guests, instead of simply tracking whether or not the vCPU is using a guest-provided VMSA. KVM needs to know the GPA of the VMSA that's actively being used so that it can react to MMU invalidation events, i.e. so that KVM can drop the VMSA if its backing guest_memfd page is punched out of existence. Opportunistically rename snp_vmsa_gpa to clarify that it tracks the pending VMSA GPA, whereas snp_guest_vmsa_gpa now tracks the in-use VMSA GPA. Note! Take care to track the GPA, not the GFN, as VALID_PAGE() won't behave correctly if an invalid GFN is converted to a GPA for checking. Note #2! Keep snp_has_guest_vmsa so that switching to a guest-provided VMSA is sticky, even if the guest-provided VMSA becomes invalid. No functional change intended. Cc: stable@vger.kernel.org # 6.12.x Reviewed-by: Michael Roth Link: https://patch.msgid.link/20260709204948.1988414-2-seanjc@google.com Signed-off-by: Sean Christopherson [ dropped the guest_tsc_protected line since Secure TSC support (a311fce2b6945) is not present in this tree ] Signed-off-by: Sasha Levin --- arch/x86/kvm/svm/sev.c | 15 ++++++++++----- arch/x86/kvm/svm/svm.h | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ca07b2c05a4c6..c8723aa2e0f40 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3960,6 +3960,7 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) /* Clear use of the VMSA */ svm->vmcb->control.vmsa_pa = INVALID_PAGE; + svm->sev_es.snp_guest_vmsa_gpa = INVALID_PAGE; /* * When replacing the VMSA during SEV-SNP AP creation, @@ -3967,11 +3968,11 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) */ vmcb_mark_all_dirty(svm->vmcb); - if (!VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) + if (!VALID_PAGE(svm->sev_es.snp_pending_vmsa_gpa)) return; - gfn = gpa_to_gfn(svm->sev_es.snp_vmsa_gpa); - svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + gfn = gpa_to_gfn(svm->sev_es.snp_pending_vmsa_gpa); + svm->sev_es.snp_pending_vmsa_gpa = INVALID_PAGE; slot = gfn_to_memslot(vcpu->kvm, gfn); if (!slot) @@ -3996,6 +3997,7 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) svm->sev_es.snp_has_guest_vmsa = true; /* Use the new VMSA */ + svm->sev_es.snp_guest_vmsa_gpa = gfn_to_gpa(gfn); svm->vmcb->control.vmsa_pa = pfn_to_hpa(pfn); /* Mark the vCPU as runnable */ @@ -4086,10 +4088,10 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) goto out; } - target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + target_svm->sev_es.snp_pending_vmsa_gpa = svm->vmcb->control.exit_info_2; break; case SVM_VMGEXIT_AP_DESTROY: - target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + target_svm->sev_es.snp_pending_vmsa_gpa = INVALID_PAGE; break; default: vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", @@ -4686,6 +4688,9 @@ int sev_vcpu_create(struct kvm_vcpu *vcpu) return -ENOMEM; svm->sev_es.vmsa = page_address(vmsa_page); + svm->sev_es.snp_pending_vmsa_gpa = INVALID_PAGE; + svm->sev_es.snp_guest_vmsa_gpa = INVALID_PAGE; + return 0; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index a595064d6a52f..26ed4ab3b257a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -238,7 +238,8 @@ struct vcpu_sev_es_state { u64 ghcb_registered_gpa; struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ - gpa_t snp_vmsa_gpa; + gpa_t snp_pending_vmsa_gpa; + gpa_t snp_guest_vmsa_gpa; bool snp_ap_waiting_for_reset; bool snp_has_guest_vmsa; }; -- 2.53.0