Blame SOURCES/kvm-i386-kvm-Disable-arch_capabilities-if-MSR-can-t-be-s.patch

Pablo Greco e6a3ae
From c48019a17552791a63cc143321944ed916de0672 Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Date: Thu, 9 May 2019 22:43:18 +0100
Pablo Greco e6a3ae
Subject: [PATCH 2/2] i386: kvm: Disable arch_capabilities if MSR can't be set
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190509224318.23376-3-ehabkost@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 87252
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 2/2] i386: kvm: Disable arch_capabilities if MSR can't be set
Pablo Greco e6a3ae
Bugzilla: 1707706
Pablo Greco e6a3ae
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Bandan Das <bsd@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
KVM has two bugs in the handling of MSR_IA32_ARCH_CAPABILITIES:
Pablo Greco e6a3ae
Pablo Greco e6a3ae
1) Linux commit commit 1eaafe91a0df ("kvm: x86: IA32_ARCH_CAPABILITIES
Pablo Greco e6a3ae
   is always supported") makes GET_SUPPORTED_CPUID return
Pablo Greco e6a3ae
   arch_capabilities even if running on SVM.  This makes "-cpu
Pablo Greco e6a3ae
   host,migratable=off" incorrectly expose arch_capabilities on CPUID on
Pablo Greco e6a3ae
   AMD hosts (where the MSR is not emulated by KVM).
Pablo Greco e6a3ae
Pablo Greco e6a3ae
2) KVM_GET_MSR_INDEX_LIST does not return MSR_IA32_ARCH_CAPABILITIES if
Pablo Greco e6a3ae
   the MSR is not supported by the host CPU.  This makes QEMU not
Pablo Greco e6a3ae
   initialize the MSR properly at kvm_put_msrs() on those hosts.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Work around both bugs on the QEMU side, by checking if the MSR
Pablo Greco e6a3ae
was returned by KVM_GET_MSR_INDEX_LIST before returning the
Pablo Greco e6a3ae
feature flag on kvm_arch_get_supported_cpuid().
Pablo Greco e6a3ae
Pablo Greco e6a3ae
This has the unfortunate side effect of making arch_capabilities
Pablo Greco e6a3ae
unavailable on hosts without hardware support for the MSR until bug #2
Pablo Greco e6a3ae
is fixed on KVM, but I can't see another way to work around bug #1
Pablo Greco e6a3ae
without that side effect.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Message-Id: <20190125220606.4864-2-ehabkost@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 485b1d256bcb0874bcde0223727c159b6837e6f8)
Pablo Greco e6a3ae
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 target/i386/kvm.c | 9 +++++++++
Pablo Greco e6a3ae
 1 file changed, 9 insertions(+)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
Pablo Greco e6a3ae
index e3e8c78..8a4d31d 100644
Pablo Greco e6a3ae
--- a/target/i386/kvm.c
Pablo Greco e6a3ae
+++ b/target/i386/kvm.c
Pablo Greco e6a3ae
@@ -374,6 +374,15 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
Pablo Greco e6a3ae
         if (host_tsx_blacklisted()) {
Pablo Greco e6a3ae
             ret &= ~(CPUID_7_0_EBX_RTM | CPUID_7_0_EBX_HLE);
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
+    } else if (function == 7 && index == 0 && reg == R_EDX) {
Pablo Greco e6a3ae
+        /*
Pablo Greco e6a3ae
+         * Linux v4.17-v4.20 incorrectly return ARCH_CAPABILITIES on SVM hosts.
Pablo Greco e6a3ae
+         * We can detect the bug by checking if MSR_IA32_ARCH_CAPABILITIES is
Pablo Greco e6a3ae
+         * returned by KVM_GET_MSR_INDEX_LIST.
Pablo Greco e6a3ae
+         */
Pablo Greco e6a3ae
+        if (!has_msr_arch_capabs) {
Pablo Greco e6a3ae
+            ret &= ~CPUID_7_0_EDX_ARCH_CAPABILITIES;
Pablo Greco e6a3ae
+        }
Pablo Greco e6a3ae
     } else if (function == 0x80000001 && reg == R_ECX) {
Pablo Greco e6a3ae
         /*
Pablo Greco e6a3ae
          * It's safe to enable TOPOEXT even if it's not returned by
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae