Skip to contentSkip to conversation

kvm: x86/cpu: Support guest MAXPHYADDR < host MAXPHYADDR

PATCH In review Mohammed Gamal 6 participants 15 messages Jun 19, 2020 – Jul 10, 2020 KVM

lore
Share & tools
lore
#0 lore raw

cover kvm: x86/cpu: Support guest MAXPHYADDR < host MAXPHYADDR

This series adds support for KVM_CAP_HAS_SMALLER_MAXPHYADDR to QEMU. Some processors might not handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR in the expected manner. Hence, we added KVM_CAP_HAS_SMALLER_MAXPHYADDR to KVM.
In this implementation KVM is queried for KVM_CAP_HAS_SMALLER_MAXPHYADDR when setting vCPU physical bits, and if the CPU doesn't support KVM_CAP_HAS_SMALLER_MAXPHYADDR the ,phys-bits is ignore and host phyiscal bits are used. A warning message is printed to the user.

Mohammed Gamal (2):
kvm: Add support for KVM_CAP_HAS_SMALLER_MAXPHYADDR x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that
don't support it

 linux-headers/linux/kvm.h |  1 +
 target/i386/cpu.c         | 11 +++++++++++
 target/i386/kvm.c         |  5 +++++
 target/i386/kvm_i386.h    |  1 +
 4 files changed, 18 insertions(+)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 kvm: Add support for KVM_CAP_HAS_SMALLER_MAXPHYADDR

This adds the KVM_CAP_HAS_SMALLER_MAXPHYADDR capability and a helper function to check for this capability. This will allow QEMU to decide to what to do if the host CPU can't handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR properly.

---
 linux-headers/linux/kvm.h | 1 +
 target/i386/kvm.c         | 5 +++++
 target/i386/kvm_i386.h    | 1 +
 3 files changed, 7 insertions(+)
linux-headers/linux/kvm.h +1 -0
index 9804495a46..9eb61a303f 100644
@@ -1017,6 +1017,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_S390_VCPU_RESETS 179
 #define KVM_CAP_S390_PROTECTED 180
 #define KVM_CAP_PPC_SECURE_GUEST 181
+#define KVM_CAP_SMALLER_MAXPHYADDR 184
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
target/i386/kvm.c +5 -0
index b3c13cb898..01100dbf20 100644
@@ -136,6 +136,11 @@ bool kvm_has_smm(void)
     return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
 }
 
+bool kvm_has_smaller_maxphyaddr(void)
+{
+    return kvm_check_extension(kvm_state, KVM_CAP_SMALLER_MAXPHYADDR);
+}
+
 bool kvm_has_adjust_clock_stable(void)
 {
     int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
target/i386/kvm_i386.h +1 -0
index 00bde7acaf..513f8eebbb 100644
@@ -34,6 +34,7 @@
 
 bool kvm_allows_irq0_override(void);
 bool kvm_has_smm(void);
+bool kvm_has_smaller_maxphyaddr(void);
 bool kvm_has_adjust_clock_stable(void);
 bool kvm_has_exception_payload(void);
 void kvm_synchronize_all_tsc(void);
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/2 x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

In reply to Mohammed Gamal

If the CPU doesn't support GUEST_MAXPHYADDR < HOST_MAXPHYADDR we let QEMU choose to use the host MAXPHYADDR and print a warning to the user.

---
 target/i386/cpu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/cpu.c +11 -0
index b1b311baa2..91c57117ce 100644
@@ -6589,6 +6589,17 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
             uint32_t host_phys_bits = x86_host_phys_bits();
             static bool warned;
 
+	    /*
+	     * If host doesn't support setting physical bits on the guest,
+	     * report it and return
+	     */
+	    if (cpu->phys_bits < host_phys_bits &&
+		!kvm_has_smaller_maxphyaddr()) {
+		warn_report("Host doesn't support setting smaller phys-bits."
+			    " Using host phys-bits\n");
+		cpu->phys_bits = host_phys_bits;
+	    }
+
             /* Print a warning if the user set it to a value that's not the
              * host value.
              */
Signature · 1 line
2.26.2
maintainer
#3 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 22 lines · Mohammed Gamal

If the CPU doesn't support GUEST_MAXPHYADDR < HOST_MAXPHYADDR we let QEMU choose to use the host MAXPHYADDR and print a warning to the user.

---
 target/i386/cpu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/cpu.c +4 -0
index b1b311baa2..91c57117ce 100644
@@ -6589,6 +6589,17 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
             uint32_t host_phys_bits = x86_host_phys_bits();
             static bool warned;
 
+	    /*
+	     * If host doesn't support setting physical bits on the guest,
+	     * report it and return
+	     */

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 6605

Quoted 10 lines
target/i386/cpu.c +7 -0
+	    if (cpu->phys_bits < host_phys_bits &&
+		!kvm_has_smaller_maxphyaddr()) {
+		warn_report("Host doesn't support setting smaller phys-bits."
+			    " Using host phys-bits\n");
+		cpu->phys_bits = host_phys_bits;
+	    }
+
             /* Print a warning if the user set it to a value that's not the
              * host value.
              */

You should remove the existing warning too.

Paolo

#4 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

In reply to Mohammed Gamal

(CCing libvir-list, and people who were included in the OVMF thread[1])

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 18 lines · Mohammed Gamal

If the CPU doesn't support GUEST_MAXPHYADDR < HOST_MAXPHYADDR we let QEMU choose to use the host MAXPHYADDR and print a warning to the user.

---
 target/i386/cpu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/cpu.c +1 -0
index b1b311baa2..91c57117ce 100644
@@ -6589,6 +6589,17 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
             uint32_t host_phys_bits = x86_host_phys_bits();
             static bool warned;
 
+	    /*

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 6602

Quoted 10 lines
target/i386/cpu.c +10 -0
+	     * If host doesn't support setting physical bits on the guest,
+	     * report it and return
+	     */
+	    if (cpu->phys_bits < host_phys_bits &&
+		!kvm_has_smaller_maxphyaddr()) {
+		warn_report("Host doesn't support setting smaller phys-bits."
+			    " Using host phys-bits\n");
+		cpu->phys_bits = host_phys_bits;
+	    }
+

This looks like a regression from existing behavior. Today, using smaller phys-bits doesn't crash most guests, and still allows live migration to smaller hosts. I agree using the host phys-bits is probably a better default, but we shouldn't override options set explicitly in the command line.

Also, it's important that we work with libvirt and management software to ensure they have appropriate APIs to choose what to do when a cluster has hosts with different MAXPHYADDR.

Quoted 6 lines
target/i386/cpu.c +0 -0
             /* Print a warning if the user set it to a value that's not the
              * host value.
              */
Signature · 1 line
2.26.2
Signature · 1 line
Eduardo
#5 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 32 lines · Eduardo Habkost

(CCing libvir-list, and people who were included in the OVMF thread[1])

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 26 lines · Mohammed Gamal

If the CPU doesn't support GUEST_MAXPHYADDR < HOST_MAXPHYADDR we let QEMU choose to use the host MAXPHYADDR and print a warning to the user.

---
 target/i386/cpu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/i386/cpu.c +9 -0
index b1b311baa2..91c57117ce 100644
@@ -6589,6 +6589,17 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
             uint32_t host_phys_bits = x86_host_phys_bits();
             static bool warned;
 
+	    /*
+	     * If host doesn't support setting physical bits on the guest,
+	     * report it and return
+	     */
+	    if (cpu->phys_bits < host_phys_bits &&
+		!kvm_has_smaller_maxphyaddr()) {
+		warn_report("Host doesn't support setting smaller phys-bits."
+			    " Using host phys-bits\n");
+		cpu->phys_bits = host_phys_bits;
Quoted 8 lines

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 6602

Quoted 2 lines
target/i386/cpu.c +2 -0
+	    }
+

This looks like a regression from existing behavior. Today, using smaller phys-bits doesn't crash most guests, and still allows live migration to smaller hosts. I agree using the host phys-bits is probably a better default, but we shouldn't override options set explicitly in the command line.

Yeah, this looks like it would cause a behaviour change / regression so looks questionable.

Quoted 3 lines

Also, it's important that we work with libvirt and management software to ensure they have appropriate APIs to choose what to do when a cluster has hosts with different MAXPHYADDR.

There's been so many complex discussions that it is hard to have any understanding of what we should be doing going forward. There's enough problems wrt phys bits, that I think we would benefit from a doc that outlines the big picture expectation for how to handle this in the virt stack.

As mentioned in the thread quoted above, using host_phys_bits is a obvious thing to do when the user requested "-cpu host".

The harder issue is how to handle other CPU models. I had suggested we should try associating a phys bits value with them, which would probably involve creating Client/Server variants for all our CPU models which don't currently have it. I still think that's worth exploring as a strategy and with versioned CPU models we should be ok wrt back compatibility with that approach.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Hi,

Quoted 9 lines
Quoted 8 lines

(CCing libvir-list, and people who were included in the OVMF thread[1])

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Also, it's important that we work with libvirt and management software to ensure they have appropriate APIs to choose what to do when a cluster has hosts with different MAXPHYADDR.

Quoted 5 lines

There's been so many complex discussions that it is hard to have any understanding of what we should be doing going forward. There's enough problems wrt phys bits, that I think we would benefit from a doc that outlines the big picture expectation for how to handle this in the virt stack.

Well, the fundamental issue is not that hard actually. We have three cases:

(1) GUEST_MAXPHYADDR == HOST_MAXPHYADDR

Everything is fine ;)

(2) GUEST_MAXPHYADDR < HOST_MAXPHYADDR

Mostly fine. Some edge cases, like different page fault errors for addresses above GUEST_MAXPHYADDR and below HOST_MAXPHYADDR. Which I think Mohammed fixed in the kernel recently.

(3) GUEST_MAXPHYADDR > HOST_MAXPHYADDR

Broken. If the guest uses addresses above HOST_MAXPHYADDR everything goes south.

The (2) case isn't much of a problem. We only need to figure whenever we want qemu allow this unconditionally (current state) or only in case the kernel fixes are present (state with this patch applied if I read it correctly).

The (3) case is the reason why guest firmware never ever uses GUEST_MAXPHYADDR and goes with very conservative heuristics instead, which in turn leads to the consequences discussed at length in the OVMF thread linked above.

Ideally we would simply outlaw (3), but it's hard for backward compatibility reasons. Second best solution is a flag somewhere (msr, cpuid, ...) telling the guest firmware "you can use GUEST_MAXPHYADDR, we guarantee it is <= HOST_MAXPHYADDR".

Quoted 9 lines

As mentioned in the thread quoted above, using host_phys_bits is a obvious thing to do when the user requested "-cpu host".

The harder issue is how to handle other CPU models. I had suggested we should try associating a phys bits value with them, which would probably involve creating Client/Server variants for all our CPU models which don't currently have it. I still think that's worth exploring as a strategy and with versioned CPU models we should be ok wrt back compatibility with that approach.

Yep, better defaults for GUEST_MAXPHYADDR would be good too, but that is a separate (although related) discussion.

take care,
Gerd

#7 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 54 lines · Gerd Hoffmann

Hi,

Quoted 9 lines
Quoted 8 lines

(CCing libvir-list, and people who were included in the OVMF thread[1])

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
Also, it's important that we work with libvirt and management software to ensure they have appropriate APIs to choose what to do when a cluster has hosts with different MAXPHYADDR.

Quoted 8 lines

There's been so many complex discussions that it is hard to have any
understanding of what we should be doing going forward. There's enough
problems wrt phys bits, that I think we would benefit from a doc that
outlines the big picture expectation for how to handle this in the virt stack.

Well, the fundamental issue is not that hard actually. We have three cases:

(1) GUEST_MAXPHYADDR == HOST_MAXPHYADDR

Everything is fine ;)

(2) GUEST_MAXPHYADDR < HOST_MAXPHYADDR

Mostly fine. Some edge cases, like different page fault errors
for
addresses above GUEST_MAXPHYADDR and below
HOST_MAXPHYADDR. Which I
think Mohammed fixed in the kernel recently.

(3) GUEST_MAXPHYADDR > HOST_MAXPHYADDR

Broken. If the guest uses addresses above HOST_MAXPHYADDR
everything
goes south.

The (2) case isn't much of a problem. We only need to figure whenever
we want qemu allow this unconditionally (current state) or only in case
the kernel fixes are present (state with this patch applied if I read it
correctly).

The (3) case is the reason why guest firmware never ever uses GUEST_MAXPHYADDR and goes with very conservative heuristics instead, which in turn leads to the consequences discussed at length in the OVMF thread linked above.

Quoted 4 lines

Ideally we would simply outlaw (3), but it's hard for backward compatibility reasons. Second best solution is a flag somewhere (msr, cpuid, ...) telling the guest firmware "you can use GUEST_MAXPHYADDR, we guarantee it is <= HOST_MAXPHYADDR".

Problem is GUEST_MAXPHYADDR > HOST_MAXPHYADDR is actually a supported configuration on some setups. Namely when memory encryption is enabled on AMD CPUs[1].

Quoted 11 lines
Quoted 10 lines

As mentioned in the thread quoted above, using host_phys_bits is a obvious thing to do when the user requested "-cpu host".

The harder issue is how to handle other CPU models. I had suggested we should try associating a phys bits value with them, which would probably involve creating Client/Server variants for all our CPU models which don't currently have it. I still think that's worth exploring as a strategy and with versioned CPU models we should be ok wrt back compatibility with that approach.

Quoted 6 lines

Yep, better defaults for GUEST_MAXPHYADDR would be good too, but that is a separate (although related) discussion.

take care,
Gerd

[1] - https://lkml.org/lkml/2020/6/19/2371

maintainer
#8 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 8 lines · Mohammed Gamal
Quoted 4 lines

Ideally we would simply outlaw (3), but it's hard for backward compatibility reasons. Second best solution is a flag somewhere (msr, cpuid, ...) telling the guest firmware "you can use GUEST_MAXPHYADDR, we guarantee it is <= HOST_MAXPHYADDR".

Problem is GUEST_MAXPHYADDR > HOST_MAXPHYADDR is actually a supported configuration on some setups. Namely when memory encryption is enabled on AMD CPUs[1].

It's not that bad since there's two MAXPHYADDRs, the one in CPUID and the one computed internally by the kernel. GUEST_MAXPHYADDR greater than the host CPUID maxphyaddr is never supported.

Paolo

#9 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

In reply to Gerd Hoffmann
Quoted 5 lines · Gerd Hoffmann

(2) GUEST_MAXPHYADDR < HOST_MAXPHYADDR

Mostly fine. Some edge cases, like different page fault errors for addresses above GUEST_MAXPHYADDR and below HOST_MAXPHYADDR. Which I think Mohammed fixed in the kernel recently.

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

#10 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 10 lines · Jim Mattson
Quoted 5 lines · Gerd Hoffmann

(2) GUEST_MAXPHYADDR < HOST_MAXPHYADDR

Mostly fine. Some edge cases, like different page fault errors for addresses above GUEST_MAXPHYADDR and below HOST_MAXPHYADDR. Which I think Mohammed fixed in the kernel recently.

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

If the fix has additional overhead, is the additional overhead bad enough to warrant making it optional? Most existing GUEST_MAXPHYADDR < HOST_MAXPHYADDR guests already work today without the fix.

Signature · 1 line
Eduardo
maintainer
#11 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 6 lines · Eduardo Habkost
Quoted 2 lines

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

If the fix has additional overhead, is the additional overhead bad enough to warrant making it optional? Most existing GUEST_MAXPHYADDR < HOST_MAXPHYADDR guests already work today without the fix.

The problematic case is when host maxphyaddr is 52. That case wouldn't work at all without the fix.

Paolo

#12 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Eduardo Habkost
Quoted 2 lines

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

If the fix has additional overhead, is the additional overhead bad enough to warrant making it optional? Most existing GUEST_MAXPHYADDR < HOST_MAXPHYADDR guests already work today without the fix.

The problematic case is when host maxphyaddr is 52. That case wouldn't work at all without the fix.

What can QEMU do to do differentiate "can't work at all without the fix" from "not the best idea, but will probably work"?

Signature · 1 line
Eduardo
maintainer
#13 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

Quoted 9 lines · Eduardo Habkost
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Eduardo Habkost
Quoted 2 lines

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

If the fix has additional overhead, is the additional overhead bad enough to warrant making it optional? Most existing GUEST_MAXPHYADDR < HOST_MAXPHYADDR guests already work today without the fix.

Quoted 5 lines
Quoted 2 lines

The problematic case is when host maxphyaddr is 52. That case wouldn't work at all without the fix.

What can QEMU do to do differentiate "can't work at all without the fix" from "not the best idea, but will probably work"?

Blocking guest_maxphyaddr < host_maxphyaddr if maxphyaddr==52 would be a good start. However it would block the default configuration on IceLake processors (which is why Mohammed looked at this thing in the first place).

Paolo

maintainer
#14 lore raw

Re: [PATCH 2/2] x86/cpu: Handle GUEST_MAXPHYADDR < HOST_MAXPHYADDR for hosts that don't support it

In reply to Jim Mattson
Quoted 6 lines · Jim Mattson
Quoted 4 lines

Mostly fine. Some edge cases, like different page fault errors for addresses above GUEST_MAXPHYADDR and below HOST_MAXPHYADDR. Which I think Mohammed fixed in the kernel recently.

Doesn't this require intercepting MOV-to-CR3 when the guest is in PAE mode, so that the hypervisor can validate the high bits in the PDPTEs?

In theory yes, but in practice it just means we'd use the AMD behavior of loading guest PDPT entries on demand during address translation (because the PDPT would point to nonexistent memory and cause an EPT violation on the PDE).

Paolo