From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 6F2EB423141 for ; Fri, 29 May 2026 18:36:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079801; cv=none; b=rcUfiRLlTgz9VODzvMO4G0DqqDVpZcdp2OBPM9NG0h9IH/Rr72ZJusjtkMHgBorqS4mLA0gMIcFT34MFKTVAryFwT+3KUBx/F9XrxC7WKJlA28JeWcyi8H2yZa9qGPJPbSYWYkC1eKEwbhx9i8wgZKUCl1UHQN5gugAkbp1ml+w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079801; c=relaxed/simple; bh=HFan/JApmEWnmTSuXMk9cfG+lv/KHWfT+K+r0sq3rN0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YMViu1q0bg2TOZcKBUEOgH61QHKe12RnXqBxjH4d+puZRv9Iz+6bFcbqPXxgkqggROmGGv/3gKho83rjgTHh2Ftc69Wd64EFoDluuClkJj06O1HYrhRRW3+35HgXK6CMMeXMQYqGiBtfHl7f8WxbAV265HJ5ZsDs0zv+fJP4Vtk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WXGGkklJ; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=U/qGFlu/; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WXGGkklJ"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="U/qGFlu/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780079796; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JgVglsrid7Bemu25RQaHLDDenSgALocYZBi9heN+Y3M=; b=WXGGkklJacmAJy7kaqkoU72tx8MiHf57WEkuCiLyvDBupys6JyS9Rna2A+ewyttHN8ZGZK tHXlDM1tdFoNiS7tcHCk9EGgmTIQsbKvZXEilKUGqlqt5k29MbTnZPr43Em1i4s2+FylPf MwYOXRmwCjZZ/jgzzU35hTEcFOCu1+4= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-511-1fHe-dl0P8G0tNiQsEYDww-1; Fri, 29 May 2026 14:36:35 -0400 X-MC-Unique: 1fHe-dl0P8G0tNiQsEYDww-1 X-Mimecast-MFC-AGG-ID: 1fHe-dl0P8G0tNiQsEYDww_1780079794 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-45ef6ad76bfso164582f8f.0 for ; Fri, 29 May 2026 11:36:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780079794; x=1780684594; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JgVglsrid7Bemu25RQaHLDDenSgALocYZBi9heN+Y3M=; b=U/qGFlu/GNXkfTSuhY7P6KYkCDcvglHTPmhF5Z3ANAfSCTfVL+gP4Lh4Kyx7bAReM9 ElLpHWc+4f7Ghsda6UFXFa3IV2gnotG7EnDNXtTD0BGc7umwjZbGEBOdMKGrx84UvMv3 tb3QrOSb8VndAGitzUcwnYbW5xWXHt54uwRcWh+WFdPeFxJs2+AwEOTPLABZipSOgOc+ AjT+tBk2SJf7/7GsHSu+RZHcsHuNIhSFxeDTnfIpRdRGiRF6RjcaTwYdcXRsUzdC4TbI kKPAjabXrQg7IEcC9dVjCkPuEehysplSQrmOy33KJRr+H1yPQEiO5UND/TPl8aovp0FM 9wIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780079794; x=1780684594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JgVglsrid7Bemu25RQaHLDDenSgALocYZBi9heN+Y3M=; b=NOUshkr9lj5PgKdcfBj98+tZKtWcY2JkdaeJZ6v/uKVs799tGx6JLCa6OWThnZfwbu RPjbCfgeD3uG71agCLu+f/q2vriovAqJdVHwwGzyzzi211Gvm9OrBz6Rx2f6qvEui7U0 i8MyrBprAf7g1URjbdfTqmqSs4RtMmPPooRGwQ4Kbhi0GxPESEbQMt4jxES8iPCqIDnc NvpQzryQtN9bskS3i0G1OtKkCfUlUsu7h2rhMpRK2gwuST0kk78v2F88+cgYXvkXlhin gyragTcJLIVUbKQ6fegw855wxhVAzoaT4dkWfG66hHWYMe0yVRyhwDf0Rou7T7jrBICb wXlA== X-Gm-Message-State: AOJu0Yzvadz/UtfrUGc2DjjqimjAL7MBrfG0ppmWWaFitJk19akf+Trk LyCRHAq31dk1eB7G4GE+JxE9y5fUWsSSUrRmLG3xQPpTsmGKXy6t25dNBRGxmILQp6s+hK1f4Nv MR9ObGGbKYXdZdvKkMHfiaxtZlkoxRz40kpEYnXjd/aTxae3OLS48y89mRzxisipDN0ZvR7hLmH PFRowPLzdUvxcw/I6ehZ92z2G2PRI2Gx36o07OpwkMWfyfeAa9sA== X-Gm-Gg: Acq92OGB03gaNHuPLFRDkFwcFn5IqSDuOGVA90HUv4f66sgGgLU+PyMMYXVLsmgmfM4 b0LZ9TKG843jmVthyACQyN10gOsAPqRlQ06hElJisVjZ934sdYsFZpYgcF5z9uvbk48EI+jj9LD YA1Q4u2+VfN9YRKTYmxpmx1T3HDgoqFCMGZ3AYdVnYM4PjJ2cKF0zsGfN1UPcX+bYUImWySbWB4 ht/NiJMQrAxLv6SzyPLiE5r8n9ntpdY4x6w7LQmqu4Y4SHXPEiL3/2d1MyL8QC07L5NjvJ32L3n vu8KVSuyfHhQJdfC4xDhAyDwGQhK3a7S8nl1u4jVWZsbJtDvj0Iq7DJTjgwLYdrN7oPBfhMY0UJ kdKG01IjMx7F1oXOzv910mulaDItS/iDfI4J2ixMn8P+5kBB9PXCg3Hg8o14GHgKmarc9e3fzL6 fbq1FlDE1YGkx2znqFdICgdY8vC25FDSWukIORjw== X-Received: by 2002:a05:600c:a111:b0:490:469c:556b with SMTP id 5b1f17b1804b1-490a2933355mr11002595e9.12.1780079793732; Fri, 29 May 2026 11:36:33 -0700 (PDT) X-Received: by 2002:a05:600c:a111:b0:490:469c:556b with SMTP id 5b1f17b1804b1-490a2933355mr11002175e9.12.1780079793305; Fri, 29 May 2026 11:36:33 -0700 (PDT) Received: from [192.168.10.48] ([151.49.251.208]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4909c116e2bsm20236265e9.28.2026.05.29.11.36.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 11:36:32 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky , Michael Roth , stable@vger.kernel.org Subject: [PATCH 16/24] KVM: SEV: Decouple the need to sync the GHCB SA from the need to free the SA Date: Fri, 29 May 2026 20:35:41 +0200 Message-ID: <20260529183549.1104619-17-pbonzini@redhat.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260529183549.1104619-1-pbonzini@redhat.com> References: <20260529183549.1104619-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson Decouple synchronizing the GHCB SA from freeing/unpinning the SA, so that the free/unpin path can be reused when freeing a vCPU. Opportunistically add a WARN to harden KVM against stomping over (and thus leaking) an already-allocated scratch area. Cc: stable@vger.kernel.org Reviewed-by: Tom Lendacky Reviewed-by: Michael Roth Signed-off-by: Sean Christopherson Message-ID: <20260501202250.2115252-17-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 437282f0ea94..11d46600cbdc 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3560,20 +3560,17 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) if (!svm->sev_es.ghcb) return; - if (svm->sev_es.ghcb_sa_free) { - /* - * The scratch area lives outside the GHCB, so there is a - * buffer that, depending on the operation performed, may - * need to be synced, then freed. - */ - if (svm->sev_es.ghcb_sa_sync) { - kvm_write_guest(svm->vcpu.kvm, - svm->sev_es.sw_scratch, - svm->sev_es.ghcb_sa, - svm->sev_es.ghcb_sa_len); - svm->sev_es.ghcb_sa_sync = false; - } + /* + * If the scratch area lives outside the GHCB, there's a buffer that, + * depending on the operation performed, may need to be synced. + */ + if (svm->sev_es.ghcb_sa_sync) { + kvm_write_guest(svm->vcpu.kvm, svm->sev_es.sw_scratch, + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); + svm->sev_es.ghcb_sa_sync = false; + } + if (svm->sev_es.ghcb_sa_free) { kvfree(svm->sev_es.ghcb_sa); svm->sev_es.ghcb_sa = NULL; svm->sev_es.ghcb_sa_free = false; @@ -3685,6 +3682,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 min_len) goto e_scratch; } + WARN_ON_ONCE(svm->sev_es.ghcb_sa_sync || svm->sev_es.ghcb_sa_free); + if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) { /* Scratch area begins within GHCB */ ghcb_scratch_beg = control->ghcb_gpa + @@ -3706,6 +3705,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 min_len) scratch_va = (void *)svm->sev_es.ghcb; scratch_va += (scratch_gpa_beg - control->ghcb_gpa); + svm->sev_es.ghcb_sa_sync = false; + svm->sev_es.ghcb_sa_free = false; svm->sev_es.ghcb_sa_len = ghcb_scratch_end - scratch_gpa_beg; } else { /* GHCB v2 requires the scratch area to be within the GHCB. */ -- 2.54.0