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 B298B451047 for ; Fri, 29 May 2026 18:36:48 +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=1780079813; cv=none; b=lr1tia1nLVO14+1apOrNCMvj+MFm95FNOMxeUENVMZjEs5nFRMEa9rRuaVgI1C8cb+KXcneKUa6gp5AjwJk4OraUYB60/f+5C0T6FNwOKGXG1dVc8/3EUI1KbrmRtPAJdk5G5zIQvwDeGzkWl6gxhdfSNmj/8Kc1C8yE1e0/ONQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079813; c=relaxed/simple; bh=GPVgJwMV79jQCtFTmJHYFUk8lo/Ze/eyh7yu+TUXh1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BA5XuFmKzj2V9FNI31w72UarSId5X5qQ1Cymjak2txBQ8Ng8hCL+EMRC7tuSCp1f6xj7U1wfYvkg5Xh+m185tv8CiRSB3U4uXa4uTY9Jju3Y3z5HDtUcscedHDVQq/ycOeSy7WPIDUMEcAEuurTJO9mrdVGe+5zLaRxNx7671PU= 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=Qx/E7gKJ; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=XmrJ1qRt; 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="Qx/E7gKJ"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="XmrJ1qRt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780079807; 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=f+Twr8CVFouDlcQRM41p6WqaCoEvHfdFFepauY7rVzw=; b=Qx/E7gKJoJbPC49ive18gqRQ+4h+4l6bhtZQeeyqeQvqRUH4S71N8Ofkb3K6huOSbCu9G6 SiTnaHd3Zmt4pfo+4DSR0iNvqUoDSaQcupypG4QR//HqM8zhveRG8s34k1B2TgMfdnRUkM +iBAyYXJWyavbkSXDYKd1rH6fP7obgw= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-651-01v6rmU4PreunltBIahfHg-1; Fri, 29 May 2026 14:36:46 -0400 X-MC-Unique: 01v6rmU4PreunltBIahfHg-1 X-Mimecast-MFC-AGG-ID: 01v6rmU4PreunltBIahfHg_1780079805 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4909c0f0ba2so7789945e9.3 for ; Fri, 29 May 2026 11:36:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780079805; x=1780684605; 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=f+Twr8CVFouDlcQRM41p6WqaCoEvHfdFFepauY7rVzw=; b=XmrJ1qRtclkBVtUdd0hjMUclYTFZQaohG39KCMCjZcdialsGQJaCtFt+SkKLCPhu95 vovHwXH+Oj1opglbZ7TS2lL1prDoKrtbPzfUQGiNIYPrNbzW4ouxS4KS9ZNtE4BEjq4/ 1I+DE+ZI4wy56kq7wEoxzFns8cdMM3xcScOuX1qvGvgrHUBxcqJ4om3TZNvf++HrbYzy cBiCIbOIhTHbAdE0/laFzgrUh9wExDe62nOCLaOXWS8asJ070CyX45/JzD3QU1C0bnpb wOVDbA4MU1ZsB4bsSuFzqX3S6zdm6A0MtWzM5VixYUvk25f0B/+1a+ZJe68jVil19gBN rnrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780079805; x=1780684605; 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=f+Twr8CVFouDlcQRM41p6WqaCoEvHfdFFepauY7rVzw=; b=EQFF6DDBjtTVsOx2W8VNss8bNZIMGq4cotw/TU3jncB3ducZJ15SG+9ZesvbVlrK4D AuYrNUHjBs2jCCqYeQP1UzzOJv4KHgknK1IBPjILlWZbqdELauD3D+xc8NFKDk/3klqy IvZgwcA3DzgWM1L2h/NftTe7BCq1i73Ry5Fv8d04pUwDNs5uHRsD9MOzpcGi0xGuv6rK b1cF+wcnGMemFmQDIp4OAgmbofXqFJZ3f/xl6K2brH9axey+ZJGXCFWzjtH49xAQ/TTb AV1MtnJ2KDspnRjFwuilun4JZDBjvmOXgRLbXSo8UcUGlcVynk4jllTJl6tNusfbyN+a EwGw== X-Gm-Message-State: AOJu0YyGPOx9ZlEISfuU+ju0T1f+xEEkqYUpHLq5bJOkoycVWX5fSlsS hLDptBn8hwnEazlmgU65t7FWLDwszp+dauv3s8BOIICgOi9N2uuDA0a7cHnDHfVPfjA5AoXujAG TNattpLqabjBsL4axO2gdkL/5X6YR/bQ2EU5E92Z28UFm1O5v3qlsMKPDakzdgRs9sm+BDUdOSB aO5fyRW4WzXUdyu3FZTZCKHiF4e/yJZcRqDKIm2xq7wp6XUyU1Ww== X-Gm-Gg: Acq92OGH3gPqk+dUT7FWVNPyxwXxYY+9J7f4qpSFTlFRschudHzNTuZ9IrTwtKe66O3 h63TIvYfl/RRVRJuETTP3TViLXztJFagHCFdC6/gtulOnl4EB1NQlECcOpmI6PuUjDHCUoBC1m+ YCBr1QT7LRb53JjC9J/rL12pWErz9GMYbW54DVnvPlgpnLX1hXZ6xEJnkZRa1IoiW6ohH8IoKwQ C1MJdlGpTH5D3Lc1mYkdTnlUlTpNChNHUX/z2l99NS7m3mdXy/zVcMfnL7SPOIa4s/sG8DMOhlU fw/QkktlWP2jIpKmc6bC6JMr1t92BBQshZCYmO9B9lqgWadvUCU1rE4ae3hhVYqh1s9JNrrVVO1 d7Hng/zDZfeoIpjOJwjTxXjWL4e5Xjt+B/YD1uJnqGfqvLcJEOpDEEMWo/IoAQISWULA8MpUUUb 2Gg9ZxRv8EdbMR6n4T3METYXNNhAX7ktMO41/AFg== X-Received: by 2002:a05:600c:400f:b0:48a:5821:5ff2 with SMTP id 5b1f17b1804b1-490a2919a2emr9350415e9.8.1780079805262; Fri, 29 May 2026 11:36:45 -0700 (PDT) X-Received: by 2002:a05:600c:400f:b0:48a:5821:5ff2 with SMTP id 5b1f17b1804b1-490a2919a2emr9349965e9.8.1780079804757; Fri, 29 May 2026 11:36:44 -0700 (PDT) Received: from [192.168.10.48] ([151.49.251.208]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4909c121d63sm19041295e9.29.2026.05.29.11.36.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 11:36:40 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky , Michael Roth Subject: [PATCH 19/24] KVM: SEV: Move GHCB "usage" check out of sev_es_validate_vmgexit() Date: Fri, 29 May 2026 20:35:44 +0200 Message-ID: <20260529183549.1104619-20-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 Move the check to verify the guest's requested GHCB out of sev_es_validate_vmgexit() as the first step towards making said helper a predicate whose sole purpose is to verify the guest has marked required GHCB fields as valid. Using a single "validate" helper sounds good on paper, but in practice it's difficult to verify that KVM is performing the necessary sanity checks (the usage of state is far removed from the relevant checks), makes it difficult to understand that "legacy" exits are simply routed to KVM's existing exit handlers, and most importantly, has directly contributed to a number of bugs as adding case-statements to the validation subtly removes them from the default path that rejects unknown exit codes with INVALID_EVENT. Deliberately extract the usage code check first so as to preserve the order of KVM's checks, even though future code extraction will technically fix bugs. No functional change intended. Reviewed-by: Tom Lendacky Reviewed-by: Michael Roth Signed-off-by: Sean Christopherson Message-ID: <20260501202250.2115252-20-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7c2ebc81306f..880a2acd77bf 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3416,12 +3416,6 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) struct kvm_vcpu *vcpu = &svm->vcpu; u64 reason; - /* Only GHCB Usage code 0 is supported */ - if (svm->sev_es.ghcb->ghcb_usage) { - reason = GHCB_ERR_INVALID_USAGE; - goto vmgexit_err; - } - reason = GHCB_ERR_MISSING_INPUT; if (!kvm_ghcb_sw_exit_code_is_valid(svm) || @@ -3534,10 +3528,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) * Print the exit code even though it may not be marked valid as it * could help with debugging. */ - if (reason == GHCB_ERR_INVALID_USAGE) { - vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n", - svm->sev_es.ghcb->ghcb_usage); - } else if (reason == GHCB_ERR_INVALID_EVENT) { + if (reason == GHCB_ERR_INVALID_EVENT) { vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n", control->exit_code); } else { @@ -4528,6 +4519,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return 1; } + /* Only GHCB Usage code 0 is supported */ + if (svm->sev_es.ghcb->ghcb_usage) { + vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n", + svm->sev_es.ghcb->ghcb_usage); + svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_USAGE); + return 1; + } + ret = sev_es_validate_vmgexit(svm); if (ret) return ret; -- 2.54.0