From: David Woodhouse <[email protected]>

sev_snp_max_planes() reports the VMPL count straight from CPUID
0x8000001f EBX[15:12], a 4-bit field the host provides. A value above
four lets userspace create SNP planes that no VMPL switch can ever
reach, because the switch already refuses a VMPL at or above
SVM_SEV_VMPL_MAX.

Clamp the count to between one and SVM_SEV_VMPL_MAX, so that a
VM's plane count never exceeds what the architecture defines.

Fixes the plane count in "kvm: svm: Implement max_planes x86
operation".

Fixes: https://lore.kernel.org/all/[email protected]/
Signed-off-by: David Woodhouse <[email protected]>
---
 arch/x86/kvm/svm/sev.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index c8a9f808dcc4..eefa8641100b 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -6010,8 +6010,10 @@ bool sev_snp_blocked(enum inject_type type, struct 
kvm_vcpu *vcpu)
 int sev_snp_max_planes(struct kvm *kvm)
 {
        /*
-        * CPUID 0x8000001f EBX[15:12] is host-provided data; a zero there
-        * must not report "no planes at all" (plane 0 always exists).
+        * CPUID 0x8000001f EBX[15:12] is host-provided data.  A zero there
+        * must not report "no planes at all" (plane 0 always exists), and
+        * the architecture defines no more than SVM_SEV_VMPL_MAX VMPLs, the
+        * bound the VMPL switch already enforces.
         */
-       return max(vmpl_levels, 1U);
+       return clamp(vmpl_levels, 1U, SVM_SEV_VMPL_MAX);
 }
-- 
2.50.1


Reply via email to