ae23c9
From b6a062c64f9639558a88f46edc3dd76b54b26bb5 Mon Sep 17 00:00:00 2001
ae23c9
From: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Date: Thu, 13 Dec 2018 15:51:59 +0000
ae23c9
Subject: [PATCH 1/5] x86: host-phys-bits-limit option
ae23c9
ae23c9
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Message-id: <20181213155200.20300-2-ehabkost@redhat.com>
ae23c9
Patchwork-id: 83479
ae23c9
O-Subject: [RHEL8/rhel qemu-kvm PATCH 1/2] x86: host-phys-bits-limit option
ae23c9
Bugzilla: 1598284
ae23c9
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ae23c9
RH-Acked-by: Pankaj Gupta <pagupta@redhat.com>
ae23c9
RH-Acked-by: Bandan Das <bsd@redhat.com>
ae23c9
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
ae23c9
ae23c9
Some downstream distributions of QEMU set host-phys-bits=on by
ae23c9
default.  This worked very well for most use cases, because
ae23c9
phys-bits really didn't have huge consequences. The only
ae23c9
difference was on the CPUID data seen by guests, and on the
ae23c9
handling of reserved bits.
ae23c9
ae23c9
This changed in KVM commit 855feb673640 ("KVM: MMU: Add 5 level
ae23c9
EPT & Shadow page table support").  Now choosing a large
ae23c9
phys-bits value for a VM has bigger impact: it will make KVM use
ae23c9
5-level EPT even when it's not really necessary.  This means
ae23c9
using the host phys-bits value may not be the best choice.
ae23c9
ae23c9
Management software could address this problem by manually
ae23c9
configuring phys-bits depending on the size of the VM and the
ae23c9
amount of MMIO address space required for hotplug.  But this is
ae23c9
not trivial to implement.
ae23c9
ae23c9
However, there's another workaround that would work for most
ae23c9
cases: keep using the host phys-bits value, but only if it's
ae23c9
smaller than 48.  This patch makes this possible by introducing a
ae23c9
new "-cpu" option: "host-phys-bits-limit".  Management software
ae23c9
or users can make sure they will always use 4-level EPT using:
ae23c9
"host-phys-bits=on,host-phys-bits-limit=48".
ae23c9
ae23c9
This behavior is still not enabled by default because QEMU
ae23c9
doesn't enable host-phys-bits=on by default.  But users,
ae23c9
management software, or downstream distributions may choose to
ae23c9
change their defaults using the new option.
ae23c9
ae23c9
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Message-Id: <20181211192527.13254-1-ehabkost@redhat.com>
ae23c9
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 target/i386/cpu.c | 5 +++++
ae23c9
 target/i386/cpu.h | 3 +++
ae23c9
 2 files changed, 8 insertions(+)
ae23c9
ae23c9
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
ae23c9
index a44912c..c37cd1e 100644
ae23c9
--- a/target/i386/cpu.c
ae23c9
+++ b/target/i386/cpu.c
ae23c9
@@ -4826,6 +4826,10 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
ae23c9
             if (cpu->host_phys_bits) {
ae23c9
                 /* The user asked for us to use the host physical bits */
ae23c9
                 cpu->phys_bits = host_phys_bits;
ae23c9
+                if (cpu->host_phys_bits_limit &&
ae23c9
+                    cpu->phys_bits > cpu->host_phys_bits_limit) {
ae23c9
+                    cpu->phys_bits = cpu->host_phys_bits_limit;
ae23c9
+                }
ae23c9
             }
ae23c9
 
ae23c9
             /* Print a warning if the user set it to a value that's not the
ae23c9
@@ -5377,6 +5381,7 @@ static Property x86_cpu_properties[] = {
ae23c9
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
ae23c9
     DEFINE_PROP_UINT32("phys-bits", X86CPU, phys_bits, 0),
ae23c9
     DEFINE_PROP_BOOL("host-phys-bits", X86CPU, host_phys_bits, false),
ae23c9
+    DEFINE_PROP_UINT8("host-phys-bits-limit", X86CPU, host_phys_bits_limit, 0),
ae23c9
     DEFINE_PROP_BOOL("fill-mtrr-mask", X86CPU, fill_mtrr_mask, true),
ae23c9
     DEFINE_PROP_UINT32("level", X86CPU, env.cpuid_level, UINT32_MAX),
ae23c9
     DEFINE_PROP_UINT32("xlevel", X86CPU, env.cpuid_xlevel, UINT32_MAX),
ae23c9
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
ae23c9
index 4a3ef4b..58d5430 100644
ae23c9
--- a/target/i386/cpu.h
ae23c9
+++ b/target/i386/cpu.h
ae23c9
@@ -1418,6 +1418,9 @@ struct X86CPU {
ae23c9
     /* if true override the phys_bits value with a value read from the host */
ae23c9
     bool host_phys_bits;
ae23c9
 
ae23c9
+    /* if set, limit maximum value for phys_bits when host_phys_bits is true */
ae23c9
+    uint8_t host_phys_bits_limit;
ae23c9
+
ae23c9
     /* Stop SMI delivery for migration compatibility with old machines */
ae23c9
     bool kvm_no_smi_migration;
ae23c9
 
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9