KVM: x86: Always set kvm_run->if_flag
PATCH v2 1/1 KVM: x86: Always set kvm_run->if_flag
The kvm_run struct's if_flag is a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., with guest_state_protected set).
---
v1 -> v2
- fix typos in commit message
- fix svm_get_if_flag() to work for non-SEV
- remove !! in return for both [svm|vmx]_get_if_flag()
- refactor svm_interrupt_blocked() to use svm_get_if_flag() arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 21 ++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 6 ++++++ arch/x86/kvm/x86.c | 9 +-------- 5 files changed, 21 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7) KVM_X86_OP(cache_reg) KVM_X86_OP(get_rflags) KVM_X86_OP(set_rflags) +KVM_X86_OP(get_if_flag) KVM_X86_OP(tlb_flush_all) KVM_X86_OP(tlb_flush_current) KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {void (*cache_reg)(struct kvm_vcpu *vcpu,enum kvm_reg reg);unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags); +bool (*get_if_flag)(struct kvm_vcpu *vcpu);void (*tlb_flush_all)(struct kvm_vcpu *vcpu);void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +12 -9
@@ -1585,6 +1585,15 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)to_svm (vcpu)->vmcb->save.rflags = rflags; } +static bool svm_get_if_flag (struct kvm_vcpu *vcpu) +{ +struct vmcb *vmcb =to_svm (vcpu)->vmcb; + +return sev_es_guest (vcpu->kvm) + ? vmcb->control.int_state &SVM_GUEST_INTERRUPT_MASK + :kvm_get_rflags (vcpu) &X86_EFLAGS_IF ; +} +static void svm_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) {switch (reg) {
@@ -3568,14 +3577,7 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)if (!gif_set (svm))return true; -if (sev_es_guest (vcpu->kvm)) { -/* -* SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask -* bit to determine the state of the IF flag. -*/ -if (!(vmcb->control.int_state &SVM_GUEST_INTERRUPT_MASK )) -return true; - }else if (is_guest_mode (vcpu)) { +if (is_guest_mode (vcpu)) {/* As long as interrupts are being delivered... */ if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
@@ -3586,7 +3588,7 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)if (nested_exit_on_intr (svm))return false; }else { -if (!(kvm_get_rflags (vcpu) &X86_EFLAGS_IF )) +if (!svm_get_if_flag (vcpu))return true; }
@@ -4621,6 +4623,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .cache_reg = svm_cache_reg, .get_rflags = svm_get_rflags, .set_rflags = svm_set_rflags, + .get_if_flag = svm_get_if_flag, .tlb_flush_all = svm_flush_tlb, .tlb_flush_current = svm_flush_tlb,
arch/x86/kvm/vmx/vmx.c +6 -0
@@ -1363,6 +1363,11 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) vmx->emulation_required =vmx_emulation_required (vcpu); } +static bool vmx_get_if_flag (struct kvm_vcpu *vcpu) +{ +return vmx_get_rflags (vcpu) &X86_EFLAGS_IF ; +} +u32 vmx_get_interrupt_shadow (struct kvm_vcpu *vcpu) {u32 interruptibility =vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO );
@@ -7575,6 +7580,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .cache_reg = vmx_cache_reg, .get_rflags = vmx_get_rflags, .set_rflags = vmx_set_rflags, + .get_if_flag = vmx_get_if_flag, .tlb_flush_all = vmx_flush_tlb_all, .tlb_flush_current = vmx_flush_tlb_current,
arch/x86/kvm/x86.c +1 -8
@@ -8995,14 +8995,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run; -/* -* if_flag is obsolete and useless, so do not bother -* setting it for SEV-ES guests. Userspace can just -* use kvm_run->ready_for_interrupt_injection. -*/ - kvm_run->if_flag = !vcpu->arch.guest_state_protected - && (kvm_get_rflags (vcpu) &X86_EFLAGS_IF ) !=0 ; - + kvm_run->if_flag =static_call (kvm_x86_get_if_flag)(vcpu); kvm_run->cr8 =kvm_get_cr8 (vcpu); kvm_run->apic_base =kvm_get_apic_base (vcpu);
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
Quoted 132 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr <[email protected]>---
v1 -> v2
- fix typos in commit message
- fixsvm_get_if_flag()to work for non-SEV
- remove !! in return for both [svm|vmx]_get_if_flag()
- refactorsvm_interrupt_blocked()to usesvm_get_if_flag()arch/x86/include/asm/kvm-x86-ops.h| 1 +arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 21 ++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 6 ++++++ arch/x86/kvm/x86.c | 9 +-------- 5 files changed, 21 insertions(+), 17 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7) KVM_X86_OP(cache_reg) KVM_X86_OP(get_rflags) KVM_X86_OP(set_rflags) +KVM_X86_OP(get_if_flag) KVM_X86_OP(tlb_flush_all) KVM_X86_OP(tlb_flush_current) KVM_X86_OP_NULL(tlb_remote_flush)arch/x86/include/asm/kvm_host.h +1
-0@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {void (*cache_reg)(struct kvm_vcpu *vcpu,enum kvm_reg reg);unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags); +bool (*get_if_flag)(struct kvm_vcpu *vcpu);void (*tlb_flush_all)(struct kvm_vcpu *vcpu);void (*tlb_flush_current)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm/svm.c +12
-9@@ -1585,6 +1585,15 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)to_svm (vcpu)->vmcb->save.rflags = rflags; } +static bool svm_get_if_flag (struct kvm_vcpu *vcpu) +{ +struct vmcb *vmcb =to_svm (vcpu)->vmcb; + +return sev_es_guest (vcpu->kvm) + ? vmcb->control.int_state &SVM_GUEST_INTERRUPT_MASK + :kvm_get_rflags (vcpu) &X86_EFLAGS_IF ; +} +static void svm_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) {switch (reg) {@@ -3568,14 +3577,7 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)if (!gif_set (svm))return true; -if (sev_es_guest (vcpu->kvm)) { -/* -* SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask -* bit to determine the state of the IF flag. -*/ -if (!(vmcb->control.int_state &SVM_GUEST_INTERRUPT_MASK )) -return true; - }else if (is_guest_mode (vcpu)) { +if (is_guest_mode (vcpu)) {/* As long as interrupts are being delivered... */ if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)@@ -3586,7 +3588,7 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)if (nested_exit_on_intr (svm))return false; }else { -if (!(kvm_get_rflags (vcpu) &X86_EFLAGS_IF )) +if (!svm_get_if_flag (vcpu))return true; }@@ -4621,6 +4623,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .cache_reg = svm_cache_reg, .get_rflags = svm_get_rflags, .set_rflags = svm_set_rflags, + .get_if_flag = svm_get_if_flag, .tlb_flush_all = svm_flush_tlb, .tlb_flush_current = svm_flush_tlb,arch/x86/kvm/vmx/vmx.c +6
-0@@ -1363,6 +1363,11 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) vmx->emulation_required =vmx_emulation_required (vcpu); } +static bool vmx_get_if_flag (struct kvm_vcpu *vcpu) +{ +return vmx_get_rflags (vcpu) &X86_EFLAGS_IF ; +} +u32 vmx_get_interrupt_shadow (struct kvm_vcpu *vcpu) {u32 interruptibility =vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO );@@ -7575,6 +7580,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .cache_reg = vmx_cache_reg, .get_rflags = vmx_get_rflags, .set_rflags = vmx_set_rflags, + .get_if_flag = vmx_get_if_flag, .tlb_flush_all = vmx_flush_tlb_all, .tlb_flush_current = vmx_flush_tlb_current,arch/x86/kvm/x86.c +0
-2@@ -8995,14 +8995,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run; -/* -* if_flag is obsolete and useless, so do not bother
arch/x86/kvm/x86.c static void post_kvm_run_save(struct kvm_vcpu *vcpu) · line 9001
Quoted 10 lines
arch/x86/kvm/x86.c +1
-6-* setting it for SEV-ES guests. Userspace can just -* use kvm_run->ready_for_interrupt_injection. -*/ - kvm_run->if_flag = !vcpu->arch.guest_state_protected - && (kvm_get_rflags (vcpu) &X86_EFLAGS_IF ) !=0 ; - + kvm_run->if_flag =static_call (kvm_x86_get_if_flag)(vcpu); kvm_run->cr8 =kvm_get_cr8 (vcpu); kvm_run->apic_base =kvm_get_apic_base (vcpu);
Best regards,
Maxim Levitsky
Quoted 8 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr<[email protected]>
Applied, though I wonder if it is really needed by those other VMMs (which? gVisor is the only one that comes to mind that is interested in userspace APIC).
It shouldn't be necessary for in-kernel APIC (where userspace can inject interrupts at any time), and ready_for_interrupt_injection is superior for userspace APIC.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).
Quoted 6 lines
Quoted 2 lines
Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr<[email protected]>Applied, though I wonder if it is really needed by those other VMMs (which? gVisor is the only one that comes to mind that is interested in userspace APIC).
Vanadium appears to have one use of it.
Quoted 3 lines
It shouldn't be necessary for in-kernel APIC (where userspace can inject interrupts at any time), and
ready_for_interrupt_injectionis superior for userspace APIC.
LOL. Here's that one use...
if (vcpu_run_state_->request_interrupt_window &&vcpu_run_state_->ready_for_interrupt_injection &&vcpu_run_state_->if_flag) {
...
}
So, maybe this is much ado about nothing?
Sean Christopherson
maintainerQuoted 24 lines · Jim Mattson
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).Quoted 6 lines
Quoted 2 lines
Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr<[email protected]>Applied, though I wonder if it is really needed by those other VMMs (which? gVisor is the only one that comes to mind that is interested in userspace APIC).
Vanadium appears to have one use of it.
Quoted 3 lines
It shouldn't be necessary for in-kernel APIC (where userspace can inject interrupts at any time), and
ready_for_interrupt_injectionis superior for userspace APIC.LOL. Here's that one use...
Quoted 7 lines
if (
vcpu_run_state_->request_interrupt_window&&vcpu_run_state_->ready_for_interrupt_injection&&vcpu_run_state_->if_flag) {
...
}So, maybe this is much ado about nothing?
I assume the issue is that SEV-ES always squishes if_flag, so that above statement can never evaluate true.
Quoted 32 lines · Sean Christopherson
Quoted 30 lines · Jim Mattson
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).Quoted 6 lines
Quoted 2 lines
Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr<[email protected]>Applied, though I wonder if it is really needed by those other VMMs (which? gVisor is the only one that comes to mind that is interested in userspace APIC).
Vanadium appears to have one use of it.
Quoted 3 lines
It shouldn't be necessary for in-kernel APIC (where userspace can inject interrupts at any time), and
ready_for_interrupt_injectionis superior for userspace APIC.LOL. Here's that one use...
if (
vcpu_run_state_->request_interrupt_window&&vcpu_run_state_->ready_for_interrupt_injection&&vcpu_run_state_->if_flag) {
...
}
Quoted 4 lines
Quoted 1 line
So, maybe this is much ado about nothing?
I assume the issue is that SEV-ES always squishes
if_flag, so that above statement can never evaluate true.
If that's the only use, though, it's pretty easy to just remove that conjunct.
Quoted 32 lines · Sean Christopherson
Quoted 30 lines · Jim Mattson
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Marc Orr
The
kvm_runstruct'sif_flagis a part of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted registers (i.e., withguest_state_protectedset).Quoted 6 lines
Quoted 2 lines
Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")Signed-off-by Marc Orr<[email protected]>Applied, though I wonder if it is really needed by those other VMMs (which? gVisor is the only one that comes to mind that is interested in userspace APIC).
Vanadium appears to have one use of it.
Quoted 3 lines
It shouldn't be necessary for in-kernel APIC (where userspace can inject interrupts at any time), and
ready_for_interrupt_injectionis superior for userspace APIC.LOL. Here's that one use...
if (
vcpu_run_state_->request_interrupt_window&&vcpu_run_state_->ready_for_interrupt_injection&&vcpu_run_state_->if_flag) {
...
}
Quoted 4 lines
Quoted 1 line
So, maybe this is much ado about nothing?
I assume the issue is that SEV-ES always squishes
if_flag, so that above statement can never evaluate true.
Correct. And the Vanadium code snippet above is what motivated this patch. Technically Vanadium uses the if_flag in one other place, but I also think that place can be replaced with the ready_for_interrupt_injection flag. In fact, I had an internal patch prepped to do this because my reading of the KVM code was identical to Paolo's that the ready_for_interrupt_injection is superior to the if_flag. But then after some internal discussion, we felt that the userspace/kernel API shouldn't have been changed.
All that being said, after Jim added his Ack to this patch (which I forgot to attach to the v2), we realized that technically the ES patches were within their right to redefine if_flag since it's previous semantics are maintained for non-ES VMs and ES requires userspace changes anyway (PSP commands, guest memory pinning, etc.).
I'm OK either way here. But I assume that if this flag is giving us pains it will give others pains. And this patch seems reasonable to me. So all things being equal, I'd prefer to proceed with it.
Paolo Bonzini
maintainerQuoted 5 lines · Marc Orr
All that being said, after Jim added his Ack to this patch (which I forgot to attach to the v2), we realized that technically the ES patches were within their right to redefine
if_flagsince it's previous semantics are maintained for non-ES VMs and ES requires userspace changes anyway (PSP commands, guest memory pinning, etc.).
Correct, but it's a bit ugly to redefine the semantics and that is why I am going to apply the patch anyway.
Paolo
Quoted 4 lines
I'm OK either way here. But I assume that if this flag is giving us pains it will give others pains. And this patch seems reasonable to me. So all things being equal, I'd prefer to proceed with it.