Introduce Notify VM exit
Subsystems (5)
v6 cover Introduce Notify VM exit
Virtual machines can exploit Intel ISA characterstics to cause functional denial of service to the VMM. This series introduces a new feature named Notify VM exit, which can help mitigate such kind of attacks.
Patch 1: An extension of KVM_SET_VCPU_EVENTS ioctl to inject a synthesized shutdown event from user space. This is also a fix for other synthesized triple fault, e.g. the RSM patch or nested_vmx_abort(), which could get lost when exit to userspace to do migrate.
Patch 2: A selftest about get/set triple fault event.
Patch 3: The main patch to enable Notify VM exit.
---
Change logs:
v5 -> v6
- Do some changes in document.
- Add a selftest about get/set triple fault event. (Sean)
- extend the argument to include both the notify window and some flags when enabling KVM_CAP_X86_BUS_LOCK_EXIT CAP. (Sean)
- Change to use KVM_VCPUEVENT_VALID_TRIPE_FAULT in flags field and add pending_triple_fault field in struct kvm_vcpu_events, which allows userspace to make/clear triple fault request. (Sean)
- Add a flag in kvm_x86_ops to avoid the kvm_has_notify_vmexit global varialbe and its export.(Sean)
- v5: https://lore.kernel.org/lkml/[email protected]/
v4 -> v5
- rename KVM_VCPUEVENTS_SHUTDOWN to KVM_VCPUEVENTS_TRIPLE_FAULT. Make it bidirection and add it to get_vcpu_events. (Sean)
- v4: https://lore.kernel.org/all/[email protected]/
v3 -> v4
- Change this feature to per-VM scope. (Jim)
- Once VM_CONTEXT_INVALID set in exit_qualification, exit to user space notify this fatal case, especially the notify VM exit happens in L2. (Jim)
- extend KVM_SET_VCPU_EVENTS to allow user space to inject a shutdown event. (Jim)
- A minor code changes.
- Add document for the new KVM capability.
- v3: https://lore.kernel.org/lkml/[email protected]/
v2 -> v3
- add a vcpu state notify_window_exits to record the number of occurence as well as a pr_warn output. (Sean)
- Add the handling in nested VM to prevent L1 bypassing the restriction through launching a L2. (Sean)
- Only kill L2 when L2 VM is context invalid, synthesize a EXIT_REASON_TRIPLE_FAULT to L1 (Sean)
- To ease the current implementation, make module parameter notify_window read-only. (Sean)
- Disable notify window exit by default.
- v2: https://lore.kernel.org/lkml/[email protected]/
v1 -> v2
- Default set notify window to 0, less than 0 to disable.
- Add more description in commit message.
---
Chenyi Qiang (2):
KVM: X86: Save&restore the triple fault request
KVM: selftests: Add a test to get/set triple fault event
Tao Xu (1):
KVM: VMX: Enable Notify VM exit
Documentation/virt/kvm/api.rst | 55 +++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/include/asm/vmx.h | 7 ++ arch/x86/include/asm/vmxfeatures.h | 1 + arch/x86/include/uapi/asm/kvm.h | 4 +- arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx/capabilities.h | 6 ++ arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/vmx.c | 48 +++++++++- arch/x86/kvm/x86.c | 33 ++++++- arch/x86/kvm/x86.h | 5 + include/uapi/linux/kvm.h | 10 ++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/triple_fault_event_test.c | 96 +++++++++++++++++++ 15 files changed, 280 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c
Signature · 1 line
2.17.1
PATCH v6 1/3 KVM: X86: Save&restore the triple fault request
For the triple fault sythesized by KVM, e.g. the RSM path or nested_vmx_abort(), if KVM exits to userspace before the request is serviced, userspace could migrate the VM and lose the triple fault.
Add the support to save and restore the triple fault event from userspace. Introduce a new event KVM_VCPUEVENT_VALID_TRIPLE_FAULT in get/set_vcpu_events to track the triple fault request.
Note that in the set_vcpu_events path, userspace is able to set/clear the triple fault request through triple_fault_pending field.
--- Documentation/virt/kvm/api.rst | 7 +++++++ arch/x86/include/uapi/asm/kvm.h | 4 +++- arch/x86/kvm/x86.c | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +7 -0
@@ -1150,6 +1150,9 @@ The following bits are defined in the flags field: fields contain a valid state. This bit will be set whenever KVM_CAP_EXCEPTION_PAYLOAD is enabled. +- KVM_VCPUEVENT_VALID_TRIPLE_FAULT may be set to signal that the + triple_fault_pending field contains a valid state. + ARM64: ^^^^^^
@@ -1245,6 +1248,10 @@ can be set in the flags field to signal that the exception_has_payload, exception_payload, and exception.pending fields contain a valid state and shall be written into the VCPU. +KVM_VCPUEVENT_VALID_TRIPLE_FAULT can be set in flags field to signal that +the triple_fault_pending field contains a valid state and shall be written +into the VCPU. + ARM64: ^^^^^^
arch/x86/include/uapi/asm/kvm.h +3 -1
@@ -325,6 +325,7 @@ struct kvm_reinject_control {#define KVM_VCPUEVENT_VALID_SHADOW 0x00000004#define KVM_VCPUEVENT_VALID_SMM 0x00000008#define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010 +#define KVM_VCPUEVENT_VALID_TRIPLE_FAULT 0x00000020/* Interrupt shadow states */ #define KVM_X86_SHADOW_INT_MOV_SS 0x01
@@ -359,7 +360,8 @@ struct kvm_vcpu_events {__u8 smm_inside_nmi;__u8 latched_init; } smi; -__u8 reserved[27 ]; +__u8 triple_fault_pending; +__u8 reserved[26 ];__u8 exception_has_payload;__u64 exception_payload; };
arch/x86/kvm/x86.c +13 -2
@@ -4911,9 +4911,12 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, !!(vcpu->arch.hflags &HF_SMM_INSIDE_NMI_MASK ); events->smi.latched_init =kvm_lapic_latched_init (vcpu); + events->triple_fault_pending =kvm_test_request (KVM_REQ_TRIPLE_FAULT , vcpu); + events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |KVM_VCPUEVENT_VALID_SHADOW - |KVM_VCPUEVENT_VALID_SMM ); + |KVM_VCPUEVENT_VALID_SMM + |KVM_VCPUEVENT_VALID_TRIPLE_FAULT );if (vcpu->kvm->arch.exception_payload_enabled) events->flags |=KVM_VCPUEVENT_VALID_PAYLOAD ;
@@ -4929,7 +4932,8 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, | KVM_VCPUEVENT_VALID_SIPI_VECTOR | KVM_VCPUEVENT_VALID_SHADOW | KVM_VCPUEVENT_VALID_SMM - | KVM_VCPUEVENT_VALID_PAYLOAD)) + | KVM_VCPUEVENT_VALID_PAYLOAD + | KVM_VCPUEVENT_VALID_TRIPLE_FAULT))return -EINVAL ;if (events->flags &KVM_VCPUEVENT_VALID_PAYLOAD ) {
@@ -5002,6 +5006,13 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, } } +if (events->flags &KVM_VCPUEVENT_VALID_TRIPLE_FAULT ) { +if (events->triple_fault_pending) +kvm_make_request (KVM_REQ_TRIPLE_FAULT , vcpu); +else +kvm_clear_request (KVM_REQ_TRIPLE_FAULT , vcpu); + } +kvm_make_request (KVM_REQ_EVENT , vcpu);return 0 ;
Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v6 1/3] KVM: X86: Save&restore the triple fault request
Nits on the shortlog...
Please don't capitalize x86, spell out "and" instead of using an ampersand (though I think it can be omitted entirely), and since there are plenty of chars left, call out that this is adding/extending KVM's ABI, e.g. it's not obvious from the shortlog where/when the save+restore happens.
KVM: x86: Extend KVM_{G,S}ET_VCPU_EVENTS to support pending triple fault
Quoted 56 lines · Chenyi Qiang
For the triple fault sythesized by KVM, e.g. the RSM path or
nested_vmx_abort(), if KVM exits to userspace before the request is serviced, userspace could migrate the VM and lose the triple fault.Add the support to save and restore the triple fault event from userspace. Introduce a new event
KVM_VCPUEVENT_VALID_TRIPLE_FAULTin get/set_vcpu_events to track the triple fault request.Note that in the
set_vcpu_eventspath, userspace is able to set/clear the triple fault request throughtriple_fault_pendingfield.Signed-off-by Chenyi Qiang <[email protected]>--- Documentation/virt/kvm/api.rst | 7 +++++++ arch/x86/include/uapi/asm/kvm.h | 4 +++- arch/x86/kvm/x86.c | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-)Documentation/virt/kvm/api.rst +7
-0@@ -1150,6 +1150,9 @@ The following bits are defined in the flags field: fields contain a valid state. This bit will be set whenever KVM_CAP_EXCEPTION_PAYLOAD is enabled. +- KVM_VCPUEVENT_VALID_TRIPLE_FAULT may be set to signal that the + triple_fault_pending field contains a valid state. + ARM64: ^^^^^^@@ -1245,6 +1248,10 @@ can be set in the flags field to signal that the exception_has_payload, exception_payload, and exception.pending fields contain a valid state and shall be written into the VCPU. +KVM_VCPUEVENT_VALID_TRIPLE_FAULT can be set in flags field to signal that +the triple_fault_pending field contains a valid state and shall be written +into the VCPU. + ARM64: ^^^^^^arch/x86/include/uapi/asm/kvm.h +1
-0@@ -325,6 +325,7 @@ struct kvm_reinject_control {#define KVM_VCPUEVENT_VALID_SHADOW 0x00000004#define KVM_VCPUEVENT_VALID_SMM 0x00000008#define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010 +#define KVM_VCPUEVENT_VALID_TRIPLE_FAULT 0x00000020/* Interrupt shadow states */ #define KVM_X86_SHADOW_INT_MOV_SS 0x01
arch/x86/include/uapi/asm/kvm.h struct kvm_vcpu_events { · line 363
Quoted 5 lines
arch/x86/include/uapi/asm/kvm.h +1
-1@@ -359,7 +360,8 @@ struct kvm_vcpu_events {__u8 smm_inside_nmi;__u8 latched_init; } smi; -__u8 reserved[27 ]; +__u8 triple_fault_pending;
What about writing this as
struct {
__u8 pending;
} triple_fault;to match the other events? It's kinda silly, but I find it easier to visually identify the various events that are handled by kvm_vcpu_events.
Quoted 9 lines
arch/x86/include/uapi/asm/kvm.h +1
-0+__u8 reserved[26 ];__u8 exception_has_payload;__u64 exception_payload; };arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, · line 4919
Quoted 10 lines
arch/x86/kvm/x86.c +4
-1@@ -4911,9 +4911,12 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, !!(vcpu->arch.hflags &HF_SMM_INSIDE_NMI_MASK ); events->smi.latched_init =kvm_lapic_latched_init (vcpu); + events->triple_fault_pending =kvm_test_request (KVM_REQ_TRIPLE_FAULT , vcpu); + events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |KVM_VCPUEVENT_VALID_SHADOW - |KVM_VCPUEVENT_VALID_SMM ); + |KVM_VCPUEVENT_VALID_SMM + |KVM_VCPUEVENT_VALID_TRIPLE_FAULT );
Does setting KVM_VCPUEVENT_VALID_TRIPLE_FAULT need to be guarded with a capability, a la KVM_CAP_EXCEPTION_PAYLOAD, so that migrating from a new KVM to an old KVM doesn't fail? Seems rather pointless since the VM is likely hosed either way...
Quoted 2 lines
arch/x86/kvm/x86.c +0
-0if (vcpu->kvm->arch.exception_payload_enabled) events->flags |=KVM_VCPUEVENT_VALID_PAYLOAD ;
Re: [PATCH v6 1/3] KVM: X86: Save&restore the triple fault request
Thanks Sean for your review!
Quoted 9 lines · Sean Christopherson
Nits on the shortlog...
Please don't capitalize x86, spell out "and" instead of using an ampersand (though I think it can be omitted entirely), and since there are plenty of chars left, call out that this is adding/extending KVM's ABI, e.g. it's not obvious from the shortlog where/when the save+restore happens.
KVM: x86: Extend KVM_{G,S}ET_VCPU_EVENTS to support pending triple fault
Will fix it.
Quoted 65 lines · Chenyi Qiang
Quoted 56 lines
For the triple fault sythesized by KVM, e.g. the RSM path or
nested_vmx_abort(), if KVM exits to userspace before the request is serviced, userspace could migrate the VM and lose the triple fault.Add the support to save and restore the triple fault event from userspace. Introduce a new event
KVM_VCPUEVENT_VALID_TRIPLE_FAULTin get/set_vcpu_events to track the triple fault request.Note that in the
set_vcpu_eventspath, userspace is able to set/clear the triple fault request throughtriple_fault_pendingfield.Signed-off-by Chenyi Qiang <[email protected]>--- Documentation/virt/kvm/api.rst | 7 +++++++ arch/x86/include/uapi/asm/kvm.h | 4 +++- arch/x86/kvm/x86.c | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-)Documentation/virt/kvm/api.rst +7
-0@@ -1150,6 +1150,9 @@ The following bits are defined in the flags field: fields contain a valid state. This bit will be set whenever KVM_CAP_EXCEPTION_PAYLOAD is enabled. +- KVM_VCPUEVENT_VALID_TRIPLE_FAULT may be set to signal that the + triple_fault_pending field contains a valid state. + ARM64: ^^^^^^@@ -1245,6 +1248,10 @@ can be set in the flags field to signal that the exception_has_payload, exception_payload, and exception.pending fields contain a valid state and shall be written into the VCPU. +KVM_VCPUEVENT_VALID_TRIPLE_FAULT can be set in flags field to signal that +the triple_fault_pending field contains a valid state and shall be written +into the VCPU. + ARM64: ^^^^^^arch/x86/include/uapi/asm/kvm.h +1
-0@@ -325,6 +325,7 @@ struct kvm_reinject_control {#define KVM_VCPUEVENT_VALID_SHADOW 0x00000004#define KVM_VCPUEVENT_VALID_SMM 0x00000008#define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010 +#define KVM_VCPUEVENT_VALID_TRIPLE_FAULT 0x00000020/* Interrupt shadow states */ #define KVM_X86_SHADOW_INT_MOV_SS 0x01arch/x86/include/uapi/asm/kvm.h struct kvm_vcpu_events { · line 363
Quoted 5 lines
arch/x86/include/uapi/asm/kvm.h +1
-1@@ -359,7 +360,8 @@ struct kvm_vcpu_events {__u8 smm_inside_nmi;__u8 latched_init; } smi; -__u8 reserved[27 ]; +__u8 triple_fault_pending;What about writing this as
Quoted 7 lines
struct { __u8 pending; } triple_fault;to match the other events? It's kinda silly, but I find it easier to visually identify the various events that are handled by
kvm_vcpu_events.
Sure, will change in this format.
Quoted 12 lines
Quoted 12 lines
arch/x86/include/uapi/asm/kvm.h +1
-0+__u8 reserved[26 ];__u8 exception_has_payload;__u64 exception_payload; };arch/x86/kvm/x86.c +0
-0@@ -4911,9 +4911,12 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, !!(vcpu->arch.hflags &HF_SMM_INSIDE_NMI_MASK ); events->smi.latched_init =kvm_lapic_latched_init (vcpu);
Quoted 12 lines
arch/x86/kvm/x86.c static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, · line 4919
Quoted 7 lines
arch/x86/kvm/x86.c +4
-1+ events->triple_fault_pending =kvm_test_request (KVM_REQ_TRIPLE_FAULT , vcpu); + events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |KVM_VCPUEVENT_VALID_SHADOW - |KVM_VCPUEVENT_VALID_SMM ); + |KVM_VCPUEVENT_VALID_SMM + |KVM_VCPUEVENT_VALID_TRIPLE_FAULT );Does setting
KVM_VCPUEVENT_VALID_TRIPLE_FAULTneed to be guarded with a capability, a laKVM_CAP_EXCEPTION_PAYLOAD, so that migrating from a new KVM to an old KVM doesn't fail? Seems rather pointless since the VM is likely hosed either way...
Indeed, at least adding a capability makes it more compatible. Will add it in next version.
Quoted 2 lines
Quoted 2 lines
arch/x86/kvm/x86.c +0
-0if (vcpu->kvm->arch.exception_payload_enabled) events->flags |=KVM_VCPUEVENT_VALID_PAYLOAD ;
PATCH v6 2/3 KVM: selftests: Add a test to get/set triple fault event
In reply to Chenyi QiangAdd a selftest to get/set triple fault event by:
- exiting to userspace via I/O from L2;
- using KVM_GET_VCPU_EVENTS to verify the TRIPEL_FAULT field is valid in flags.
- using KVM_SET_VCPU_EVENTS to inject a triple fault event so that L1 can see the appropriate exit.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/triple_fault_event_test.c | 96 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -55,6 +55,7 @@ /x86_64/xen_vmcall_test /x86_64/xss_msr_test /x86_64/vmx_pmu_msrs_test +/x86_64/triple_fault_event_test /access_tracking_perf_test /demand_paging_test /dirty_log_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -88,6 +88,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/amx_test +TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH v6 2/3] KVM: selftests: Add a test to get/set triple fault event
Review
Quoted 2 lines · Chenyi Qiang
(quoted diff) +2
-0+#ifndef __x86_64__ +# error This test is 64-bit only
No need, all of KVM selftests are 64-bit only.
Quoted 4 lines
(quoted diff) +4
-0+#endif + +#define VCPU_ID 0 +#define ARBITRARY_IO_PORT 0x2000
Review
Quoted 10 lines
(quoted diff) +10
-0+ +/* The virtual machine object. */ +static struct kvm_vm *vm; + +static void l2_guest_code(void) +{ + /* + * Generate an exit to L0 userspace, i.e. main(), via I/O to an + * arbitrary port. + */
I think we can test a "real" triple fault without too much effort by abusing vcpu->run->request_interrupt_window. E.g. map the run struct into L2, clear PIN_BASED_EXT_INTR_MASK in vmcs12, and then in l2_guest_code() do:
asm volatile("cli");
run->request_interrupt_window = true;
asm volatile("sti; ud2");
asm volatile("inb %%dx, %%al"
: : [port] "d" (ARBITRARY_IO_PORT) : "rax"); The STI shadow will block the IRQ-window VM-Exit until after the ud2, i.e. until after the triple fault is pending.
And then main() can
1. verify it got KVM_EXIT_IRQ_WINDOW_OPEN with a pending triple fault
2. clear the triple fault and re-enter L1+l2
3. continue with the existing code, e.g. verify it got KVM_EXIT_IO, stuff a pending triple fault, etc...
That said, typing that out makes me think we should technically handle/prevent this in KVM since triple fault / shutdown is kinda sorta just a special exception.
Heh, and a potentially bad/crazy idea would be to use a reserved/magic vector in kvm_vcpu_events.exception to save/restore triple fault, e.g. we could steal NMI_VECTOR.
arch/x86/kvm/x86.c +2 -1
@@ -4743,7 +4743,8 @@ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)return (kvm_arch_interrupt_allowed (vcpu) &&kvm_cpu_accept_dm_intr (vcpu) && !kvm_event_needs_reinjection (vcpu) && - !vcpu->arch.exception.pending); + !vcpu->arch.exception.pending && + !kvm_test_request (KVM_REQ_TRIPLE_FAULT , vcpu)); } static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
Re: [PATCH v6 2/3] KVM: selftests: Add a test to get/set triple fault event
Quoted 25 lines · Sean Christopherson
Review
Quoted 2 lines · Chenyi Qiang
(quoted diff) +2
-0+#ifndef __x86_64__ +# error This test is 64-bit onlyNo need, all of KVM selftests are 64-bit only.
Quoted 4 lines
(quoted diff) +4
-0+#endif + +#define VCPU_ID 0 +#define ARBITRARY_IO_PORT 0x2000Review
Quoted 10 lines
(quoted diff) +10
-0+ +/* The virtual machine object. */ +static struct kvm_vm *vm; + +static void l2_guest_code(void) +{ + /* + * Generate an exit to L0 userspace, i.e. main(), via I/O to an + * arbitrary port. + */I think we can test a "real" triple fault without too much effort by abusing
vcpu->run->request_interrupt_window. E.g. map the run struct into L2, clearPIN_BASED_EXT_INTR_MASKin vmcs12, and then inl2_guest_code()do:
Quoted 4 lines
asm volatile("cli");
run->request_interrupt_window = true;Maybe, A GUEST_SYNC to main() to change the request_interrupt_window also works.
Quoted 2 lines
asm volatile("sti; ud2");How does the "real" triple fault occur here? Through UD2?
Quoted 37 lines
asm volatile("inb %%dx, %%al" : : [port] "d" (ARBITRARY_IO_PORT) : "rax");The STI shadow will block the IRQ-window VM-Exit until after the ud2, i.e. until after the triple fault is pending.
And then
main()can1. verify it got
KVM_EXIT_IRQ_WINDOW_OPENwith a pending triple fault
2. clear the triple fault and re-enter L1+l2
3. continue with the existing code, e.g. verify it gotKVM_EXIT_IO, stuff a pending triple fault, etc...That said, typing that out makes me think we should technically handle/prevent this in KVM since triple fault / shutdown is kinda sorta just a special exception.
Heh, and a potentially bad/crazy idea would be to use a reserved/magic vector in kvm_vcpu_events.exception to save/restore triple fault, e.g. we could steal
NMI_VECTOR.arch/x86/kvm/x86.c +2
-1@@ -4743,7 +4743,8 @@ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)return (kvm_arch_interrupt_allowed (vcpu) &&kvm_cpu_accept_dm_intr (vcpu) && !kvm_event_needs_reinjection (vcpu) && - !vcpu->arch.exception.pending); + !vcpu->arch.exception.pending && + !kvm_test_request (KVM_REQ_TRIPLE_FAULT , vcpu)); } static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
Sean Christopherson
maintainerRe: [PATCH v6 2/3] KVM: selftests: Add a test to get/set triple fault event
Quoted 28 lines · Chenyi Qiang
Quoted 25 lines · Sean Christopherson
Review
Quoted 2 lines · Chenyi Qiang
(quoted diff) +2
-0+#ifndef __x86_64__ +# error This test is 64-bit onlyNo need, all of KVM selftests are 64-bit only.
Quoted 4 lines
(quoted diff) +4
-0+#endif + +#define VCPU_ID 0 +#define ARBITRARY_IO_PORT 0x2000Review
Quoted 10 lines
(quoted diff) +10
-0+ +/* The virtual machine object. */ +static struct kvm_vm *vm; + +static void l2_guest_code(void) +{ + /* + * Generate an exit to L0 userspace, i.e. main(), via I/O to an + * arbitrary port. + */I think we can test a "real" triple fault without too much effort by abusing
vcpu->run->request_interrupt_window. E.g. map the run struct into L2, clearPIN_BASED_EXT_INTR_MASKin vmcs12, and then inl2_guest_code()do:
Quoted 7 lines
Quoted 4 lines
asm volatile("cli"); run->request_interrupt_window = true;Maybe, A
GUEST_SYNCtomain()to change therequest_interrupt_windowalso works.
Hmm, yes, that should work too. Feel free to punt on writing this sub-test. As mentioned below, KVM should treat a pending triple fault as a pending "exception", i.e. userspace shouldn't see KVM_EXIT_IRQ_WINDOW_OPEN with a pending triple fault, and so the test should actually assert that the triple fault occurs in L2. But I can roll that test into the KVM fix if you'd like.
Quoted 4 lines
Quoted 2 lines
asm volatile("sti; ud2");How does the "real" triple fault occur here? Through UD2?
Yeah. IIRC, by default L2 doesn't have a valid IDT, so any fault will escalate to a triple fault.
Quoted 15 lines
Quoted 15 lines
asm volatile("inb %%dx, %%al" : : [port] "d" (ARBITRARY_IO_PORT) : "rax");The STI shadow will block the IRQ-window VM-Exit until after the ud2, i.e. until after the triple fault is pending.
And then
main()can1. verify it got
KVM_EXIT_IRQ_WINDOW_OPENwith a pending triple fault
2. clear the triple fault and re-enter L1+l2
3. continue with the existing code, e.g. verify it gotKVM_EXIT_IO, stuff a pending triple fault, etc...That said, typing that out makes me think we should technically handle/prevent this in KVM since triple fault / shutdown is kinda sorta just a special exception.
Re: [PATCH v6 2/3] KVM: selftests: Add a test to get/set triple fault event
Quoted 34 lines · Sean Christopherson
Quoted 33 lines · Chenyi Qiang
Quoted 29 lines · Sean Christopherson
Review
Quoted 2 lines · Chenyi Qiang
(quoted diff) +2
-0+#ifndef __x86_64__ +# error This test is 64-bit onlyNo need, all of KVM selftests are 64-bit only.
Quoted 4 lines
(quoted diff) +4
-0+#endif + +#define VCPU_ID 0 +#define ARBITRARY_IO_PORT 0x2000Review
Quoted 10 lines
(quoted diff) +10
-0+ +/* The virtual machine object. */ +static struct kvm_vm *vm; + +static void l2_guest_code(void) +{ + /* + * Generate an exit to L0 userspace, i.e. main(), via I/O to an + * arbitrary port. + */I think we can test a "real" triple fault without too much effort by abusing
vcpu->run->request_interrupt_window. E.g. map the run struct into L2, clearPIN_BASED_EXT_INTR_MASKin vmcs12, and then inl2_guest_code()do:asm volatile("cli"); run->request_interrupt_window = true;
Quoted 8 lines
Quoted 2 lines
Maybe, A
GUEST_SYNCtomain()to change therequest_interrupt_windowalso works.Hmm, yes, that should work too. Feel free to punt on writing this sub-test. As mentioned below, KVM should treat a pending triple fault as a pending "exception", i.e. userspace shouldn't see
KVM_EXIT_IRQ_WINDOW_OPENwith a pending triple fault, and so the test should actually assert that the triple fault occurs in L2. But I can roll that test into the KVM fix if you'd like.
If the KVM_EXIT_IRQ_WINDOW_OPEN can't see the pending triple fault, only asserting the triple fault may not have meaning in this selftest. Anyway, feel free to add the pending triple fault test in your KVM fix patches.
Quoted 24 lines
Quoted 5 lines
Quoted 3 lines
asm volatile("sti; ud2");How does the "real" triple fault occur here? Through UD2?
Yeah. IIRC, by default L2 doesn't have a valid IDT, so any fault will escalate to a triple fault.
Quoted 15 lines
Quoted 15 lines
asm volatile("inb %%dx, %%al" : : [port] "d" (ARBITRARY_IO_PORT) : "rax");The STI shadow will block the IRQ-window VM-Exit until after the ud2, i.e. until after the triple fault is pending.
And then
main()can1. verify it got
KVM_EXIT_IRQ_WINDOW_OPENwith a pending triple fault
2. clear the triple fault and re-enter L1+l2
3. continue with the existing code, e.g. verify it gotKVM_EXIT_IO, stuff a pending triple fault, etc...That said, typing that out makes me think we should technically handle/prevent this in KVM since triple fault / shutdown is kinda sorta just a special exception.
PATCH v6 3/3 KVM: VMX: Enable Notify VM exit
In reply to Chenyi QiangFrom: Tao Xu <[email protected]>
There are cases that malicious virtual machines can cause CPU stuck (due to event windows don't open up), e.g., infinite loop in microcode when nested #AC (CVE-2015-5307). No event window means no event (NMI, SMI and IRQ) can be delivered. It leads the CPU to be unavailable to host or other VMs.
VMM can enable notify VM exit that a VM exit generated if no event window occurs in VM non-root mode for a specified amount of time (notify window).
Feature enabling:
- The new vmcs field SECONDARY_EXEC_NOTIFY_VM_EXITING is introduced to enable this feature. VMM can set NOTIFY_WINDOW vmcs field to adjust the expected notify window.
- Add a new KVM capability KVM_CAP_X86_NOTIFY_VMEXIT so that user space can query and enable this feature in per-VM scope. The argument is a 64bit value: bits 63:32 are used for notify window, and bits 31:0 are for flags. Current supported flags:
- KVM_X86_NOTIFY_VMEXIT_ENABLED: enable the feature with the notify window provided.
- KVM_X86_NOTIFY_VMEXIT_USER: exit to userspace once the exits happen.
- It's safe to even set notify window to zero since an internal hardware threshold is added to vmcs.notify_window.
VM exit handling:
- Introduce a vcpu state notify_window_exits to records the count of notify VM exits and expose it through the debugfs.
- Notify VM exit can happen incident to delivery of a vector event. Allow it in KVM.
- Exit to userspace unconditionally for handling when VM_CONTEXT_INVALID bit is set.
Nested handling
- Nested notify VM exits are not supported yet. Keep the same notify window control in vmcs02 as vmcs01, so that L1 can't escape the restriction of notify VM exits through launching L2 VM.
- When L2 VM is context invalid and user space should synthesize a shutdown event to a vcpu. KVM makes KVM_REQ_TRIPLE_FAULT request accordingly and it would synthesize a nested triple fault exit to L1 hypervisor to kill L2.
Notify VM exit is defined in latest Intel Architecture Instruction Set Extensions Programming Reference, chapter 9.2.
--- Documentation/virt/kvm/api.rst | 48 ++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++++++ arch/x86/include/asm/vmx.h | 7 +++++ arch/x86/include/asm/vmxfeatures.h | 1 + arch/x86/include/uapi/asm/vmx.h | 4 ++- arch/x86/kvm/vmx/capabilities.h | 6 ++++ arch/x86/kvm/vmx/nested.c | 8 +++++ arch/x86/kvm/vmx/vmx.c | 48 ++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 18 ++++++++++- arch/x86/kvm/x86.h | 5 ++++ include/uapi/linux/kvm.h | 10 +++++++ 11 files changed, 159 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +48 -0
@@ -6321,6 +6321,26 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_NOTIFY */ + struct { + #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) + __u32 flags; + } notify; + +Used on x86 systems. When the VM capability KVM_CAP_X86_NOTIFY_VMEXIT is +enabled, a VM exit generated if no event window occurs in VM non-root mode +for a specified amount of time. Once KVM_X86_NOTIFY_VMEXIT_USER is set when +enabling the cap, it would exit to userspace with the exit reason +KVM_EXIT_NOTIFY for further handling. The "flags" field contains more +detailed info. + +The valid value for 'data' is: + + - KVM_NOTIFY_CONTEXT_INVALID -- the VM context is corrupted and not valid + in VMCS. It would run into unknown result if resume the target VM. + :: /* Fix the size of the union. */
@@ -7266,6 +7286,34 @@ The valid bits in cap.args[0] are: generate a #UD within the guest. =================================== ============================================ +7.31 KVM_CAP_X86_NOTIFY_VMEXIT +------------------------------ + +:Architectures: x86 +:Target: VM +:Parameters: args[0] is the value of notify window as well as some flags +:Returns: 0 on success, -EINVAL if hardware doesn't support notify VM exit. + +Bits 63:32 of args[0] are used for notify window. +Bits 31:0 of args[0] are for some flags. Valid bits are:: + + #define KVM_X86_NOTIFY_VMEXIT_ENABLED (1 << 0) + #define KVM_X86_NOTIFY_VMEXIT_USER (1 << 1) + +This capability allows userspace to configure the notify VM exit on/off +in per-VM scope during VM creation. Notify VM exit is disabled by default. +When userspace sets KVM_X86_NOTIFY_VMEXIT_ENABLED bit in args[0], VMM would +enable this feature with the notify window provided, which will generate +a VM exit if no event window occurs in VM non-root mode for a specified of +time (notify window). + +If KVM_X86_NOTIFY_VMEXIT_USER is set in args[0], upon notify VM exits happen, +KVM would exit to userspace for handling. + +This capability is aimed to mitigate the threat that malicious VMs can +cause CPU stuck (due to event windows don't open up) and make the CPU +unavailable to host or other VMs. + 8. Other capabilities. ======================
arch/x86/include/asm/kvm_host.h +9 -0
@@ -65,6 +65,9 @@#define KVM_BUS_LOCK_DETECTION_VALID_MODE (KVM_BUS_LOCK_DETECTION_OFF | \ KVM_BUS_LOCK_DETECTION_EXIT) +#define KVM_X86_NOTIFY_VMEXIT_VALID_BITS (KVM_X86_NOTIFY_VMEXIT_ENABLED | \ + KVM_X86_NOTIFY_VMEXIT_USER) +/* x86-specific vcpu->requests bit members */ #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)#define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
@@ -1157,6 +1160,9 @@ struct kvm_arch {bool bus_lock_detection_enabled;bool enable_pmu; + +u32 notify_window; +u32 notify_vmexit_flags;/* * If exit_on_emulation_error is set, and the in-kernel instruction * emulator fails to emulate an instruction, allow userspace
@@ -1293,6 +1299,7 @@ struct kvm_vcpu_stat {u64 directed_yield_attempted;u64 directed_yield_successful;u64 guest_mode; +u64 notify_window_exits; };struct x86_instruction_info ;
@@ -1504,6 +1511,8 @@ struct kvm_x86_ops {* Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + +bool has_notify_vmexit; };struct kvm_x86_nested_ops {
arch/x86/include/asm/vmx.h +7 -0
@@ -74,6 +74,7 @@#define SECONDARY_EXEC_TSC_SCALING VMCS_CONTROL_BIT(TSC_SCALING)#define SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE VMCS_CONTROL_BIT(USR_WAIT_PAUSE)#define SECONDARY_EXEC_BUS_LOCK_DETECTION VMCS_CONTROL_BIT(BUS_LOCK_DETECTION) +#define SECONDARY_EXEC_NOTIFY_VM_EXITING VMCS_CONTROL_BIT(NOTIFY_VM_EXITING)#define PIN_BASED_EXT_INTR_MASK VMCS_CONTROL_BIT(INTR_EXITING)#define PIN_BASED_NMI_EXITING VMCS_CONTROL_BIT(NMI_EXITING)
@@ -269,6 +270,7 @@ enum vmcs_field {SECONDARY_VM_EXEC_CONTROL =0x0000401e ,PLE_GAP =0x00004020 ,PLE_WINDOW =0x00004022 , +NOTIFY_WINDOW =0x00004024 ,VM_INSTRUCTION_ERROR =0x00004400 ,VM_EXIT_REASON =0x00004402 , VM_EXIT_INTR_INFO = 0x00004404,
@@ -553,6 +555,11 @@ enum vm_entry_failure_code {#define EPT_VIOLATION_GVA_IS_VALID (1 << EPT_VIOLATION_GVA_IS_VALID_BIT)#define EPT_VIOLATION_GVA_TRANSLATED (1 << EPT_VIOLATION_GVA_TRANSLATED_BIT) +/* +* Exit Qualifications for NOTIFY VM EXIT +*/ +#define NOTIFY_VM_CONTEXT_INVALID BIT(0) +/* * VM-instruction error numbers */
arch/x86/include/asm/vmxfeatures.h +1 -0
@@ -84,5 +84,6 @@#define VMX_FEATURE_USR_WAIT_PAUSE ( 2*32+ 26)/* Enable TPAUSE, UMONITOR, UMWAIT in guest */ #define VMX_FEATURE_ENCLV_EXITING ( 2*32+ 28)/* "" VM-Exit on ENCLV (leaf dependent) */ #define VMX_FEATURE_BUS_LOCK_DETECTION ( 2*32+ 30)/* "" VM-Exit when bus lock caused */ +#define VMX_FEATURE_NOTIFY_VM_EXITING ( 2*32+ 31)/* VM-Exit when no event windows after notify window */ #endif /* _ASM_X86_VMXFEATURES_H */
arch/x86/include/uapi/asm/vmx.h +3 -1
@@ -91,6 +91,7 @@#define EXIT_REASON_UMWAIT 67#define EXIT_REASON_TPAUSE 68#define EXIT_REASON_BUS_LOCK 74 +#define EXIT_REASON_NOTIFY 75#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
@@ -153,7 +154,8 @@ { EXIT_REASON_XRSTORS, "XRSTORS" }, \ { EXIT_REASON_UMWAIT, "UMWAIT" }, \ { EXIT_REASON_TPAUSE, "TPAUSE" }, \ - { EXIT_REASON_BUS_LOCK, "BUS_LOCK" } + { EXIT_REASON_BUS_LOCK, "BUS_LOCK" }, \ + { EXIT_REASON_NOTIFY, "NOTIFY" } #define VMX_EXIT_REASON_FLAGS \ { VMX_EXIT_REASONS_FAILED_VMENTRY, "FAILED_VMENTRY" }
arch/x86/kvm/vmx/capabilities.h +6 -0
@@ -417,4 +417,10 @@ static inline u64 vmx_supported_debugctl(void) return debugctl; } +static inline bool cpu_has_notify_vmexit (void ) +{ +return vmcs_config.cpu_based_2nd_exec_ctrl & +SECONDARY_EXEC_NOTIFY_VM_EXITING ; +} +#endif /* __KVM_X86_VMX_CAPS_H */
arch/x86/kvm/vmx/nested.c +8 -0
@@ -2133,6 +2133,8 @@ static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)static void prepare_vmcs02_constant_state (struct vcpu_vmx *vmx) { +struct kvm *kvm = vmx->vcpu.kvm; +/* * If vmcs02 hasn't been initialized, set the constant vmcs02 state * according to L0's settings (vmcs12 is irrelevant here). Host
@@ -2175,6 +2177,9 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)if (cpu_has_vmx_encls_vmexit ())vmcs_write64 (ENCLS_EXITING_BITMAP ,INVALID_GPA ); +if (kvm_notify_vmexit_enabled (kvm)) +vmcs_write32 (NOTIFY_WINDOW , kvm->arch.notify_window); +/* * Set the MSR load/store lists to match L0's settings. Only the * addresses are constant (for vmcs02), the counts can change based
@@ -6107,6 +6112,9 @@ static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);case EXIT_REASON_ENCLS :return nested_vmx_exit_handled_encls (vcpu, vmcs12); +case EXIT_REASON_NOTIFY : +/* Notify VM exit is not exposed to L1 */ +return false;default :return true; }
arch/x86/kvm/vmx/vmx.c +45 -3
107 lines not on this page · raw message
arch/x86/kvm/x86.c +17 -1
@@ -291,7 +291,8 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {STATS_DESC_COUNTER (VCPU , nested_run),STATS_DESC_COUNTER (VCPU , directed_yield_attempted),STATS_DESC_COUNTER (VCPU , directed_yield_successful), -STATS_DESC_ICOUNTER (VCPU , guest_mode) +STATS_DESC_ICOUNTER (VCPU , guest_mode), + STATS_DESC_COUNTER(VCPU, notify_window_exits), }; const struct kvm_stats_header kvm_vcpu_stats_header = {
@@ -4389,6 +4390,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_DISABLE_QUIRKS2 : r =KVM_X86_VALID_QUIRKS ;break ; +case KVM_CAP_X86_NOTIFY_VMEXIT : + r = kvm_x86_ops.has_notify_vmexit; +break ;default :break ; }
@@ -6090,6 +6094,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_X86_NOTIFY_VMEXIT : + r = -EINVAL ; +if ((u32 )cap->args[0 ] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS ) +break ; +if (!kvm_x86_ops.has_notify_vmexit) +break ; +if (!(u32 )cap->args[0 ] &KVM_X86_NOTIFY_VMEXIT_ENABLED ) +break ; + kvm->arch.notify_window = cap->args[0 ] >>32 ; + kvm->arch.notify_vmexit_flags = (u32 )cap->args[0 ]; + r =0 ; +break ;default : r = -EINVAL ;break ;
arch/x86/kvm/x86.h +5 -0
@@ -344,6 +344,11 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } +static inline bool kvm_notify_vmexit_enabled (struct kvm *kvm) +{ +return kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_ENABLED ; +} +enum kvm_intr_type {/* Values are arbitrary, but must be non-zero. */ KVM_HANDLING_IRQ =1 ,
include/uapi/linux/kvm.h +10 -0
@@ -270,6 +270,7 @@ struct kvm_xen_exit {#define KVM_EXIT_X86_BUS_LOCK 33#define KVM_EXIT_XEN 34#define KVM_EXIT_RISCV_SBI 35 +#define KVM_EXIT_NOTIFY 36/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -490,6 +491,11 @@ struct kvm_run {unsigned long args[6 ];unsigned long ret[2 ]; } riscv_sbi; +/* KVM_EXIT_NOTIFY */ +struct { +#define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) +__u32 flags; + } notify;/* Fix the size of the union. */ char padding[256 ]; };
@@ -1148,6 +1154,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_PMU_CAPABILITY 212#define KVM_CAP_DISABLE_QUIRKS2 213#define KVM_CAP_VM_TSC_CONTROL 214 +#define KVM_CAP_X86_NOTIFY_VMEXIT 215#ifdef KVM_CAP_IRQ_ROUTING
@@ -2109,4 +2116,7 @@ struct kvm_stats_desc {/* Available with KVM_CAP_XSAVE2 */ #define KVM_GET_XSAVE2 _IOR(KVMIO, 0xcf, struct kvm_xsave) +#define KVM_X86_NOTIFY_VMEXIT_ENABLED (1ULL << 0) +#define KVM_X86_NOTIFY_VMEXIT_USER (1ULL << 1) +#endif /* __LINUX_KVM_H */
Signature · 1 line
2.17.1
kernel test robot
build botRe: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
Thank you for the patch! Perhaps something to improve:
Hi Chenyi,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on next-20220420]
[cannot apply to kvm/master v5.18-rc3 v5.18-rc2 v5.18-rc1 v5.18-rc3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Chenyi-Qiang/Introduce-Notify-VM-exit/20220421-152750 base: f1244c81da13009dbf61cb807f45881501c44789 config: i386-randconfig-a011 (https://download.01.org/0day-ci/archive/20220421/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project bac6cd5bf85669e3376610cfc4c4f9ca015e7b9b)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/d03a8a0979790428b75ba9438199a7ea2c22e0a6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Chenyi-Qiang/Introduce-Notify-VM-exit/20220421-152750
git checkout d03a8a0979790428b75ba9438199a7ea2c22e0a6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kvm/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:6084:7: warning: logical not is only applied to the left hand side of this bitwise operator [-Wlogical-not-parentheses]
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^ ~
include/linux/compiler.h:56:47: note: expanded from macro 'if'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^~~~
include/linux/compiler.h:58:52: note: expanded from macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~
arch/x86/kvm/x86.c:6084:7: note: add parentheses after the '!' to evaluate the bitwise operator first
arch/x86/kvm/x86.c:6084:7: note: add parentheses around left hand side expression to silence this warning
>> arch/x86/kvm/x86.c:6084:7: warning: logical not is only applied to the left hand side of this bitwise operator [-Wlogical-not-parentheses]
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^ ~
include/linux/compiler.h:56:47: note: expanded from macro 'if'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^~~~
include/linux/compiler.h:58:61: note: expanded from macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~
arch/x86/kvm/x86.c:6084:7: note: add parentheses after the '!' to evaluate the bitwise operator first
arch/x86/kvm/x86.c:6084:7: note: add parentheses around left hand side expression to silence this warning
>> arch/x86/kvm/x86.c:6084:7: warning: logical not is only applied to the left hand side of this bitwise operator [-Wlogical-not-parentheses]
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^ ~
include/linux/compiler.h:56:47: note: expanded from macro 'if'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^~~~
include/linux/compiler.h:58:86: note: expanded from macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~
include/linux/compiler.h:69:3: note: expanded from macro '__trace_if_value'
(cond) ? \
^~~~ arch/x86/kvm/x86.c:6084:7: note: add parentheses after the '!' to evaluate the bitwise operator first arch/x86/kvm/x86.c:6084:7: note: add parentheses around left hand side expression to silence this warning 3 warnings generated.
vim +6084 arch/x86/kvm/x86.c
5924
5925 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5926 struct kvm_enable_cap *cap)
5927 {
5928 int r;
5929
5930 if (cap->flags)
5931 return -EINVAL;
5932
5933 switch (cap->cap) {
5934 case KVM_CAP_DISABLE_QUIRKS2:
5935 r = -EINVAL;
5936 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS)
5937 break;
5938 fallthrough;
5939 case KVM_CAP_DISABLE_QUIRKS:
5940 kvm->arch.disabled_quirks = cap->args[0];
5941 r = 0;
5942 break;
5943 case KVM_CAP_SPLIT_IRQCHIP: {
5944 mutex_lock(&kvm->lock);
5945 r = -EINVAL;
5946 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5947 goto split_irqchip_unlock;
5948 r = -EEXIST;
5949 if (irqchip_in_kernel(kvm))
5950 goto split_irqchip_unlock;
5951 if (kvm->created_vcpus)
5952 goto split_irqchip_unlock;
5953 r = kvm_setup_empty_irq_routing(kvm);
5954 if (r)
5955 goto split_irqchip_unlock;
5956 /* Pairs with irqchip_in_kernel. */
5957 smp_wmb();
5958 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
5959 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
5960 kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT);
5961 r = 0;
5962 split_irqchip_unlock:
5963 mutex_unlock(&kvm->lock);
5964 break;
5965 }
5966 case KVM_CAP_X2APIC_API:
5967 r = -EINVAL;
5968 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5969 break;
5970
5971 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5972 kvm->arch.x2apic_format = true;
5973 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5974 kvm->arch.x2apic_broadcast_quirk_disabled = true;
5975
5976 r = 0;
5977 break;
5978 case KVM_CAP_X86_DISABLE_EXITS:
5979 r = -EINVAL;
5980 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5981 break;
5982
5983 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5984 kvm_can_mwait_in_guest())
5985 kvm->arch.mwait_in_guest = true;
5986 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
5987 kvm->arch.hlt_in_guest = true;
5988 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5989 kvm->arch.pause_in_guest = true;
5990 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5991 kvm->arch.cstate_in_guest = true;
5992 r = 0;
5993 break;
5994 case KVM_CAP_MSR_PLATFORM_INFO:
5995 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5996 r = 0;
5997 break;
5998 case KVM_CAP_EXCEPTION_PAYLOAD:
5999 kvm->arch.exception_payload_enabled = cap->args[0];
6000 r = 0;
6001 break;
6002 case KVM_CAP_X86_USER_SPACE_MSR:
6003 kvm->arch.user_space_msr_mask = cap->args[0];
6004 r = 0;
6005 break;
6006 case KVM_CAP_X86_BUS_LOCK_EXIT:
6007 r = -EINVAL;
6008 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
6009 break;
6010
6011 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) &&
6012 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT))
6013 break;
6014
6015 if (kvm_has_bus_lock_exit &&
6016 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
6017 kvm->arch.bus_lock_detection_enabled = true;
6018 r = 0;
6019 break;
6020 #ifdef CONFIG_X86_SGX_KVM
6021 case KVM_CAP_SGX_ATTRIBUTE: {
6022 unsigned long allowed_attributes = 0;
6023
6024 r = sgx_set_attribute(&allowed_attributes, cap->args[0]);
6025 if (r)
6026 break;
6027
6028 /* KVM only supports the PROVISIONKEY privileged attribute. */
6029 if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) &&
6030 !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY))
6031 kvm->arch.sgx_provisioning_allowed = true;
6032 else
6033 r = -EINVAL;
6034 break;
6035 }
6036 #endif
6037 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
6038 r = -EINVAL;
6039 if (!kvm_x86_ops.vm_copy_enc_context_from)
6040 break;
6041
6042 r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]);
6043 break;
6044 case KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM:
6045 r = -EINVAL;
6046 if (!kvm_x86_ops.vm_move_enc_context_from)
6047 break;
6048
6049 r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]);
6050 break;
6051 case KVM_CAP_EXIT_HYPERCALL:
6052 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) {
6053 r = -EINVAL;
6054 break;
6055 }
6056 kvm->arch.hypercall_exit_enabled = cap->args[0];
6057 r = 0;
6058 break;
6059 case KVM_CAP_EXIT_ON_EMULATION_FAILURE:
6060 r = -EINVAL;
6061 if (cap->args[0] & ~1)
6062 break;
6063 kvm->arch.exit_on_emulation_error = cap->args[0];
6064 r = 0;
6065 break;
6066 case KVM_CAP_PMU_CAPABILITY:
6067 r = -EINVAL;
6068 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
6069 break;
6070
6071 mutex_lock(&kvm->lock);
6072 if (!kvm->created_vcpus) {
6073 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE);
6074 r = 0;
6075 }
6076 mutex_unlock(&kvm->lock);
6077 break;
6078 case KVM_CAP_X86_NOTIFY_VMEXIT:
6079 r = -EINVAL;
6080 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS)
6081 break;
6082 if (!kvm_x86_ops.has_notify_vmexit)
6083 break;
> 6084 if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
6085 break;
6086 kvm->arch.notify_window = cap->args[0] >> 32;
6087 kvm->arch.notify_vmexit_flags = (u32)cap->args[0];
6088 r = 0;
6089 break;
6090 default:
6091 r = -EINVAL;
6092 break;
6093 }
6094 return r;
6095 }
6096
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
kernel test robot
build botRe: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
In reply to Chenyi QiangThank you for the patch! Yet something to improve:
Hi Chenyi,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on next-20220420]
[cannot apply to kvm/master v5.18-rc3 v5.18-rc2 v5.18-rc1 v5.18-rc3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Chenyi-Qiang/Introduce-Notify-VM-exit/20220421-152750 base: f1244c81da13009dbf61cb807f45881501c44789 config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20220422/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project bac6cd5bf85669e3376610cfc4c4f9ca015e7b9b)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/d03a8a0979790428b75ba9438199a7ea2c22e0a6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Chenyi-Qiang/Introduce-Notify-VM-exit/20220421-152750 git checkout d03a8a0979790428b75ba9438199a7ea2c22e0a6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:6084:7: error: logical not is only applied to the left hand side of this bitwise operator [-Werror,-Wlogical-not-parentheses]
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^ ~
arch/x86/kvm/x86.c:6084:7: note: add parentheses after the '!' to evaluate the bitwise operator first
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^
( )
arch/x86/kvm/x86.c:6084:7: note: add parentheses around left hand side expression to silence this warning
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
^
( )1 error generated.
vim +6084 arch/x86/kvm/x86.c
5924
5925 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
5926 struct kvm_enable_cap *cap)
5927 {
5928 int r;
5929
5930 if (cap->flags)
5931 return -EINVAL;
5932
5933 switch (cap->cap) {
5934 case KVM_CAP_DISABLE_QUIRKS2:
5935 r = -EINVAL;
5936 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS)
5937 break;
5938 fallthrough;
5939 case KVM_CAP_DISABLE_QUIRKS:
5940 kvm->arch.disabled_quirks = cap->args[0];
5941 r = 0;
5942 break;
5943 case KVM_CAP_SPLIT_IRQCHIP: {
5944 mutex_lock(&kvm->lock);
5945 r = -EINVAL;
5946 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
5947 goto split_irqchip_unlock;
5948 r = -EEXIST;
5949 if (irqchip_in_kernel(kvm))
5950 goto split_irqchip_unlock;
5951 if (kvm->created_vcpus)
5952 goto split_irqchip_unlock;
5953 r = kvm_setup_empty_irq_routing(kvm);
5954 if (r)
5955 goto split_irqchip_unlock;
5956 /* Pairs with irqchip_in_kernel. */
5957 smp_wmb();
5958 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
5959 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
5960 kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT);
5961 r = 0;
5962 split_irqchip_unlock:
5963 mutex_unlock(&kvm->lock);
5964 break;
5965 }
5966 case KVM_CAP_X2APIC_API:
5967 r = -EINVAL;
5968 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
5969 break;
5970
5971 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
5972 kvm->arch.x2apic_format = true;
5973 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
5974 kvm->arch.x2apic_broadcast_quirk_disabled = true;
5975
5976 r = 0;
5977 break;
5978 case KVM_CAP_X86_DISABLE_EXITS:
5979 r = -EINVAL;
5980 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
5981 break;
5982
5983 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
5984 kvm_can_mwait_in_guest())
5985 kvm->arch.mwait_in_guest = true;
5986 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
5987 kvm->arch.hlt_in_guest = true;
5988 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
5989 kvm->arch.pause_in_guest = true;
5990 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
5991 kvm->arch.cstate_in_guest = true;
5992 r = 0;
5993 break;
5994 case KVM_CAP_MSR_PLATFORM_INFO:
5995 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
5996 r = 0;
5997 break;
5998 case KVM_CAP_EXCEPTION_PAYLOAD:
5999 kvm->arch.exception_payload_enabled = cap->args[0];
6000 r = 0;
6001 break;
6002 case KVM_CAP_X86_USER_SPACE_MSR:
6003 kvm->arch.user_space_msr_mask = cap->args[0];
6004 r = 0;
6005 break;
6006 case KVM_CAP_X86_BUS_LOCK_EXIT:
6007 r = -EINVAL;
6008 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
6009 break;
6010
6011 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) &&
6012 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT))
6013 break;
6014
6015 if (kvm_has_bus_lock_exit &&
6016 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
6017 kvm->arch.bus_lock_detection_enabled = true;
6018 r = 0;
6019 break;
6020 #ifdef CONFIG_X86_SGX_KVM
6021 case KVM_CAP_SGX_ATTRIBUTE: {
6022 unsigned long allowed_attributes = 0;
6023
6024 r = sgx_set_attribute(&allowed_attributes, cap->args[0]);
6025 if (r)
6026 break;
6027
6028 /* KVM only supports the PROVISIONKEY privileged attribute. */
6029 if ((allowed_attributes & SGX_ATTR_PROVISIONKEY) &&
6030 !(allowed_attributes & ~SGX_ATTR_PROVISIONKEY))
6031 kvm->arch.sgx_provisioning_allowed = true;
6032 else
6033 r = -EINVAL;
6034 break;
6035 }
6036 #endif
6037 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
6038 r = -EINVAL;
6039 if (!kvm_x86_ops.vm_copy_enc_context_from)
6040 break;
6041
6042 r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]);
6043 break;
6044 case KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM:
6045 r = -EINVAL;
6046 if (!kvm_x86_ops.vm_move_enc_context_from)
6047 break;
6048
6049 r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]);
6050 break;
6051 case KVM_CAP_EXIT_HYPERCALL:
6052 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) {
6053 r = -EINVAL;
6054 break;
6055 }
6056 kvm->arch.hypercall_exit_enabled = cap->args[0];
6057 r = 0;
6058 break;
6059 case KVM_CAP_EXIT_ON_EMULATION_FAILURE:
6060 r = -EINVAL;
6061 if (cap->args[0] & ~1)
6062 break;
6063 kvm->arch.exit_on_emulation_error = cap->args[0];
6064 r = 0;
6065 break;
6066 case KVM_CAP_PMU_CAPABILITY:
6067 r = -EINVAL;
6068 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK))
6069 break;
6070
6071 mutex_lock(&kvm->lock);
6072 if (!kvm->created_vcpus) {
6073 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE);
6074 r = 0;
6075 }
6076 mutex_unlock(&kvm->lock);
6077 break;
6078 case KVM_CAP_X86_NOTIFY_VMEXIT:
6079 r = -EINVAL;
6080 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS)
6081 break;
6082 if (!kvm_x86_ops.has_notify_vmexit)
6083 break;
> 6084 if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
6085 break;
6086 kvm->arch.notify_window = cap->args[0] >> 32;
6087 kvm->arch.notify_vmexit_flags = (u32)cap->args[0];
6088 r = 0;
6089 break;
6090 default:
6091 r = -EINVAL;
6092 break;
6093 }
6094 return r;
6095 }
6096
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Re: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
In reply to Chenyi QiangQuoted 430 lines · Chenyi Qiang
From: Tao Xu <[email protected]>
There are cases that malicious virtual machines can cause CPU stuck (due to event windows don't open up), e.g., infinite loop in microcode when nested #AC (CVE-2015-5307). No event window means no event (NMI, SMI and IRQ) can be delivered. It leads the CPU to be unavailable to host or other VMs.
VMM can enable notify VM exit that a VM exit generated if no event window occurs in VM non-root mode for a specified amount of time (notify window).
Feature enabling:
- The new vmcs fieldSECONDARY_EXEC_NOTIFY_VM_EXITINGis introduced to enable this feature. VMM can setNOTIFY_WINDOWvmcs field to adjust the expected notify window.
- Add a new KVM capabilityKVM_CAP_X86_NOTIFY_VMEXITso that user space can query and enable this feature in per-VM scope. The argument is a 64bit value: bits 63:32 are used for notify window, and bits 31:0 are for flags. Current supported flags:
-KVM_X86_NOTIFY_VMEXIT_ENABLED: enable the feature with the notify window provided.
-KVM_X86_NOTIFY_VMEXIT_USER: exit to userspace once the exits happen.
- It's safe to even set notify window to zero since an internal hardware threshold is added to vmcs.notify_window.VM exit handling:
- Introduce a vcpu statenotify_window_exitsto records the count of notify VM exits and expose it through the debugfs.
- Notify VM exit can happen incident to delivery of a vector event. Allow it in KVM.
- Exit to userspace unconditionally for handling whenVM_CONTEXT_INVALIDbit is set.Nested handling
- Nested notify VM exits are not supported yet. Keep the same notify window control in vmcs02 as vmcs01, so that L1 can't escape the restriction of notify VM exits through launching L2 VM.
- When L2 VM is context invalid and user space should synthesize a shutdown event to a vcpu. KVM makesKVM_REQ_TRIPLE_FAULTrequest accordingly and it would synthesize a nested triple fault exit to L1 hypervisor to kill L2.Notify VM exit is defined in latest Intel Architecture Instruction Set Extensions Programming Reference, chapter 9.2.
Co-developed-by Xiaoyao Li <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Tao Xu <[email protected]>Co-developed-by Chenyi Qiang <[email protected]>Signed-off-by Chenyi Qiang <[email protected]>--- Documentation/virt/kvm/api.rst | 48 ++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++++++ arch/x86/include/asm/vmx.h | 7 +++++ arch/x86/include/asm/vmxfeatures.h | 1 + arch/x86/include/uapi/asm/vmx.h | 4 ++- arch/x86/kvm/vmx/capabilities.h | 6 ++++ arch/x86/kvm/vmx/nested.c | 8 +++++ arch/x86/kvm/vmx/vmx.c | 48 ++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 18 ++++++++++- arch/x86/kvm/x86.h | 5 ++++ include/uapi/linux/kvm.h | 10 +++++++ 11 files changed, 159 insertions(+), 5 deletions(-)Documentation/virt/kvm/api.rst +48
-0@@ -6321,6 +6321,26 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_NOTIFY */ + struct { + #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) + __u32 flags; + } notify; + +Used on x86 systems. When the VM capability KVM_CAP_X86_NOTIFY_VMEXIT is +enabled, a VM exit generated if no event window occurs in VM non-root mode +for a specified amount of time. Once KVM_X86_NOTIFY_VMEXIT_USER is set when +enabling the cap, it would exit to userspace with the exit reason +KVM_EXIT_NOTIFY for further handling. The "flags" field contains more +detailed info. + +The valid value for 'data' is: + + - KVM_NOTIFY_CONTEXT_INVALID -- the VM context is corrupted and not valid + in VMCS. It would run into unknown result if resume the target VM. + :: /* Fix the size of the union. */@@ -7266,6 +7286,34 @@ The valid bits in cap.args[0] are: generate a #UD within the guest. =================================== ============================================ +7.31 KVM_CAP_X86_NOTIFY_VMEXIT +------------------------------ + +:Architectures: x86 +:Target: VM +:Parameters: args[0] is the value of notify window as well as some flags +:Returns: 0 on success, -EINVAL if hardware doesn't support notify VM exit. + +Bits 63:32 of args[0] are used for notify window. +Bits 31:0 of args[0] are for some flags. Valid bits are:: + + #define KVM_X86_NOTIFY_VMEXIT_ENABLED (1 << 0) + #define KVM_X86_NOTIFY_VMEXIT_USER (1 << 1) + +This capability allows userspace to configure the notify VM exit on/off +in per-VM scope during VM creation. Notify VM exit is disabled by default. +When userspace sets KVM_X86_NOTIFY_VMEXIT_ENABLED bit in args[0], VMM would +enable this feature with the notify window provided, which will generate +a VM exit if no event window occurs in VM non-root mode for a specified of +time (notify window). + +If KVM_X86_NOTIFY_VMEXIT_USER is set in args[0], upon notify VM exits happen, +KVM would exit to userspace for handling. + +This capability is aimed to mitigate the threat that malicious VMs can +cause CPU stuck (due to event windows don't open up) and make the CPU +unavailable to host or other VMs. + 8. Other capabilities. ======================arch/x86/include/asm/kvm_host.h +9
-0@@ -65,6 +65,9 @@#define KVM_BUS_LOCK_DETECTION_VALID_MODE (KVM_BUS_LOCK_DETECTION_OFF | \ KVM_BUS_LOCK_DETECTION_EXIT) +#define KVM_X86_NOTIFY_VMEXIT_VALID_BITS (KVM_X86_NOTIFY_VMEXIT_ENABLED | \ + KVM_X86_NOTIFY_VMEXIT_USER) +/* x86-specific vcpu->requests bit members */ #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)#define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)@@ -1157,6 +1160,9 @@ struct kvm_arch {bool bus_lock_detection_enabled;bool enable_pmu; + +u32 notify_window; +u32 notify_vmexit_flags;/* * If exit_on_emulation_error is set, and the in-kernel instruction * emulator fails to emulate an instruction, allow userspace @@ -1293,6 +1299,7 @@ struct kvm_vcpu_stat {u64 directed_yield_attempted;u64 directed_yield_successful;u64 guest_mode; +u64 notify_window_exits; };struct x86_instruction_info ;@@ -1504,6 +1511,8 @@ struct kvm_x86_ops {* Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + +bool has_notify_vmexit; };struct kvm_x86_nested_ops {arch/x86/include/asm/vmx.h +7
-0@@ -74,6 +74,7 @@#define SECONDARY_EXEC_TSC_SCALING VMCS_CONTROL_BIT(TSC_SCALING)#define SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE VMCS_CONTROL_BIT(USR_WAIT_PAUSE)#define SECONDARY_EXEC_BUS_LOCK_DETECTION VMCS_CONTROL_BIT(BUS_LOCK_DETECTION) +#define SECONDARY_EXEC_NOTIFY_VM_EXITING VMCS_CONTROL_BIT(NOTIFY_VM_EXITING)#define PIN_BASED_EXT_INTR_MASK VMCS_CONTROL_BIT(INTR_EXITING)#define PIN_BASED_NMI_EXITING VMCS_CONTROL_BIT(NMI_EXITING)@@ -269,6 +270,7 @@ enum vmcs_field {SECONDARY_VM_EXEC_CONTROL =0x0000401e ,PLE_GAP =0x00004020 ,PLE_WINDOW =0x00004022 , +NOTIFY_WINDOW =0x00004024 ,VM_INSTRUCTION_ERROR =0x00004400 ,VM_EXIT_REASON =0x00004402 , VM_EXIT_INTR_INFO = 0x00004404,@@ -553,6 +555,11 @@ enum vm_entry_failure_code {#define EPT_VIOLATION_GVA_IS_VALID (1 << EPT_VIOLATION_GVA_IS_VALID_BIT)#define EPT_VIOLATION_GVA_TRANSLATED (1 << EPT_VIOLATION_GVA_TRANSLATED_BIT) +/* +* Exit Qualifications for NOTIFY VM EXIT +*/ +#define NOTIFY_VM_CONTEXT_INVALID BIT(0) +/* * VM-instruction error numbers */ arch/x86/include/asm/vmxfeatures.h +1
-0@@ -84,5 +84,6 @@#define VMX_FEATURE_USR_WAIT_PAUSE ( 2*32+ 26)/* Enable TPAUSE, UMONITOR, UMWAIT in guest */ #define VMX_FEATURE_ENCLV_EXITING ( 2*32+ 28)/* "" VM-Exit on ENCLV (leaf dependent) */ #define VMX_FEATURE_BUS_LOCK_DETECTION ( 2*32+ 30)/* "" VM-Exit when bus lock caused */ +#define VMX_FEATURE_NOTIFY_VM_EXITING ( 2*32+ 31)/* VM-Exit when no event windows after notify window */ #endif /* _ASM_X86_VMXFEATURES_H */ arch/x86/include/uapi/asm/vmx.h +3
-1@@ -91,6 +91,7 @@#define EXIT_REASON_UMWAIT 67#define EXIT_REASON_TPAUSE 68#define EXIT_REASON_BUS_LOCK 74 +#define EXIT_REASON_NOTIFY 75#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \@@ -153,7 +154,8 @@ { EXIT_REASON_XRSTORS, "XRSTORS" }, \ { EXIT_REASON_UMWAIT, "UMWAIT" }, \ { EXIT_REASON_TPAUSE, "TPAUSE" }, \ - { EXIT_REASON_BUS_LOCK, "BUS_LOCK" } + { EXIT_REASON_BUS_LOCK, "BUS_LOCK" }, \ + { EXIT_REASON_NOTIFY, "NOTIFY" } #define VMX_EXIT_REASON_FLAGS \ { VMX_EXIT_REASONS_FAILED_VMENTRY, "FAILED_VMENTRY" }arch/x86/kvm/vmx/capabilities.h +6
-0@@ -417,4 +417,10 @@ static inline u64 vmx_supported_debugctl(void) return debugctl; } +static inline bool cpu_has_notify_vmexit (void ) +{ +return vmcs_config.cpu_based_2nd_exec_ctrl & +SECONDARY_EXEC_NOTIFY_VM_EXITING ; +} +#endif /* __KVM_X86_VMX_CAPS_H */ arch/x86/kvm/vmx/nested.c +8
-0@@ -2133,6 +2133,8 @@ static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)static void prepare_vmcs02_constant_state (struct vcpu_vmx *vmx) { +struct kvm *kvm = vmx->vcpu.kvm; +/* * If vmcs02 hasn't been initialized, set the constant vmcs02 state * according to L0's settings (vmcs12 is irrelevant here). Host @@ -2175,6 +2177,9 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)if (cpu_has_vmx_encls_vmexit ())vmcs_write64 (ENCLS_EXITING_BITMAP ,INVALID_GPA ); +if (kvm_notify_vmexit_enabled (kvm)) +vmcs_write32 (NOTIFY_WINDOW , kvm->arch.notify_window); +/* * Set the MSR load/store lists to match L0's settings. Only the * addresses are constant (for vmcs02), the counts can change based @@ -6107,6 +6112,9 @@ static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);case EXIT_REASON_ENCLS :return nested_vmx_exit_handled_encls (vcpu, vmcs12); +case EXIT_REASON_NOTIFY : +/* Notify VM exit is not exposed to L1 */ +return false;default :return true; }arch/x86/kvm/vmx/vmx.c +45
-3@@ -2472,7 +2472,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,SECONDARY_EXEC_PT_USE_GPA |SECONDARY_EXEC_PT_CONCEAL_VMX |SECONDARY_EXEC_ENABLE_VMFUNC | -SECONDARY_EXEC_BUS_LOCK_DETECTION ; +SECONDARY_EXEC_BUS_LOCK_DETECTION | +SECONDARY_EXEC_NOTIFY_VM_EXITING ;if (cpu_has_sgx ()) opt2 |=SECONDARY_EXEC_ENCLS_EXITING ; if (adjust_vmx_controls(min2, opt2,@@ -4357,6 +4358,9 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)if (!vcpu->kvm->arch.bus_lock_detection_enabled) exec_control &= ~SECONDARY_EXEC_BUS_LOCK_DETECTION ; +if (!kvm_notify_vmexit_enabled (vcpu->kvm)) + exec_control &= ~SECONDARY_EXEC_NOTIFY_VM_EXITING ; +return exec_control; }@@ -4364,6 +4368,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)static void init_vmcs (struct vcpu_vmx *vmx) { +struct kvm *kvm = vmx->vcpu.kvm; +if (nested)nested_vmx_set_vmcs_shadowing_bitmap ();@@ -4392,12 +4398,15 @@ static void init_vmcs(struct vcpu_vmx *vmx)vmcs_write64 (POSTED_INTR_DESC_ADDR ,__pa ((&vmx->pi_desc))); } -if (!kvm_pause_in_guest (vmx->vcpu.kvm)) { +if (!kvm_pause_in_guest (kvm)) {vmcs_write32 (PLE_GAP , ple_gap); vmx->ple_window = ple_window; vmx->ple_window_dirty = true; } +if (kvm_notify_vmexit_enabled (kvm)) +vmcs_write32 (NOTIFY_WINDOW , kvm->arch.notify_window); +vmcs_write32 (PAGE_FAULT_ERROR_CODE_MASK ,0 );vmcs_write32 (PAGE_FAULT_ERROR_CODE_MATCH ,0 );vmcs_write32 (CR3_TARGET_COUNT ,0 );/* 22.2.1 */ @@ -5684,6 +5693,32 @@ static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)return 1 ; } +static int handle_notify (struct kvm_vcpu *vcpu) +{ +unsigned long exit_qual =vmx_get_exit_qual (vcpu); +bool context_invalid = exit_qual &NOTIFY_VM_CONTEXT_INVALID ; + + ++vcpu->stat.notify_window_exits; + +/* +* Notify VM exit happened while executing iret from NMI, +* "blocked by NMI" bit has to be set before next VM entry. +*/ +if (enable_vnmi && (exit_qual &INTR_INFO_UNBLOCK_NMI )) +vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , +GUEST_INTR_STATE_NMI ); + +if (vcpu->kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_USER || + context_invalid) { + vcpu->run->exit_reason =KVM_EXIT_NOTIFY ; + vcpu->run->notify.flags = context_invalid ? +KVM_NOTIFY_CONTEXT_INVALID :0 ; +return 0 ; + } + +return 1 ; +} +/* * The exit handlers return 1 if the exit was handled fully and guest execution * may resume. Otherwise they set the kvm_run parameter to indicate what needs @@ -5741,6 +5776,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, [EXIT_REASON_ENCLS] = handle_encls, [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit, + [EXIT_REASON_NOTIFY] = handle_notify, }; static const int kvm_vmx_max_exit_handlers =@@ -6105,7 +6141,8 @@ static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) exit_reason.basic != EXIT_REASON_EPT_VIOLATION && exit_reason.basic != EXIT_REASON_PML_FULL && exit_reason.basic != EXIT_REASON_APIC_ACCESS && - exit_reason.basic != EXIT_REASON_TASK_SWITCH)) { + exit_reason.basic != EXIT_REASON_TASK_SWITCH && + exit_reason.basic != EXIT_REASON_NOTIFY)) {int ndata =3 ; vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ;@@ -7841,6 +7878,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .complete_emulated_msr = kvm_complete_insn_gp, .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + + .has_notify_vmexit = false, };static unsigned int vmx_handle_intel_pt_intr (void )@@ -7979,6 +8018,9 @@ static __init int hardware_setup(void) kvm_tsc_scaling_ratio_frac_bits =48 ; kvm_has_bus_lock_exit =cpu_has_vmx_bus_lock_detection (); +if (cpu_has_notify_vmexit ()) + vmx_x86_ops.has_notify_vmexit = true; +set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ if (enable_ept)arch/x86/kvm/x86.c +5
-1@@ -291,7 +291,8 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {STATS_DESC_COUNTER (VCPU , nested_run),STATS_DESC_COUNTER (VCPU , directed_yield_attempted),STATS_DESC_COUNTER (VCPU , directed_yield_successful), -STATS_DESC_ICOUNTER (VCPU , guest_mode) +STATS_DESC_ICOUNTER (VCPU , guest_mode), + STATS_DESC_COUNTER(VCPU, notify_window_exits), }; const struct kvm_stats_header kvm_vcpu_stats_header = {@@ -4389,6 +4390,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_DISABLE_QUIRKS2 : r =KVM_X86_VALID_QUIRKS ;break ; +case KVM_CAP_X86_NOTIFY_VMEXIT : + r = kvm_x86_ops.has_notify_vmexit; +break ;default :break ; }
arch/x86/kvm/x86.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6103
Quoted 10 lines
arch/x86/kvm/x86.c +7
-0@@ -6090,6 +6094,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_X86_NOTIFY_VMEXIT : + r = -EINVAL ; +if ((u32 )cap->args[0 ] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS ) +break ; +if (!kvm_x86_ops.has_notify_vmexit) +break ; +if (!(u32 )cap->args[0 ] &KVM_X86_NOTIFY_VMEXIT_ENABLED )
Miss the parentheses here, should be !((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
Quoted 64 lines
arch/x86/kvm/x86.c +5
-0+break ; + kvm->arch.notify_window = cap->args[0 ] >>32 ; + kvm->arch.notify_vmexit_flags = (u32 )cap->args[0 ]; + r =0 ; +break ;default : r = -EINVAL ;break ;arch/x86/kvm/x86.h +5
-0@@ -344,6 +344,11 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } +static inline bool kvm_notify_vmexit_enabled (struct kvm *kvm) +{ +return kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_ENABLED ; +} +enum kvm_intr_type {/* Values are arbitrary, but must be non-zero. */ KVM_HANDLING_IRQ =1 ,include/uapi/linux/kvm.h +10
-0@@ -270,6 +270,7 @@ struct kvm_xen_exit {#define KVM_EXIT_X86_BUS_LOCK 33#define KVM_EXIT_XEN 34#define KVM_EXIT_RISCV_SBI 35 +#define KVM_EXIT_NOTIFY 36/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -490,6 +491,11 @@ struct kvm_run {unsigned long args[6 ];unsigned long ret[2 ]; } riscv_sbi; +/* KVM_EXIT_NOTIFY */ +struct { +#define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) +__u32 flags; + } notify;/* Fix the size of the union. */ char padding[256 ]; };@@ -1148,6 +1154,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_PMU_CAPABILITY 212#define KVM_CAP_DISABLE_QUIRKS2 213#define KVM_CAP_VM_TSC_CONTROL 214 +#define KVM_CAP_X86_NOTIFY_VMEXIT 215#ifdef KVM_CAP_IRQ_ROUTING @@ -2109,4 +2116,7 @@ struct kvm_stats_desc {/* Available with KVM_CAP_XSAVE2 */ #define KVM_GET_XSAVE2 _IOR(KVMIO, 0xcf, struct kvm_xsave) +#define KVM_X86_NOTIFY_VMEXIT_ENABLED (1ULL << 0) +#define KVM_X86_NOTIFY_VMEXIT_USER (1ULL << 1) +#endif /* __LINUX_KVM_H */
Sean Christopherson
maintainerRe: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
In reply to Chenyi QiangReview struct kvm_x86_ops { · line 1515
Quoted 6 lines · Chenyi Qiang
(quoted diff) +2
-0@@ -1504,6 +1511,8 @@ struct kvm_x86_ops { * Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + + bool has_notify_vmexit;
I'm pretty sure I suggested this, but seeing it in code, it kinda sorta makes things worst if we don't first consolidate the existing flags. kvm_x86_ops works, but we'd definitely be taking liberties with the "ops" part.
What about adding struct kvm_caps to collect these flags/settings that don't fit into kvm_cpu_caps because they're not a CPUID feature flag? kvm_x86_ops has the advantage of kinda being read-only after init since VMX modifies vmx_x86_ops, but IMO that's not enough reason to shove this into kvm_x86_ops. And long term, we might be able find a way to mark kvm_caps as full __ro_after_init.
If no one objects, the attached patch can slide in before this patch, then has_notifiy_vmexit can land in kvm_caps.
struct kvm_caps {
/* control of guest tsc rate supported? */
bool has_tsc_control;
/* maximum supported tsc_khz for guests */
u32 max_guest_tsc_khz;
/* number of bits of the fractional part of the TSC scaling ratio */
u8 tsc_scaling_ratio_frac_bits;
/* maximum allowed value of TSC scaling ratio */
u64 max_tsc_scaling_ratio;
/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
u64 default_tsc_scaling_ratio;
/* bus lock detection supported? */
bool has_bus_lock_exit;
u64 supported_mce_cap;
u64 supported_xcr0;
u64 supported_xss;
};Review int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6105
Quoted 13 lines
(quoted diff) +9
-0@@ -6090,6 +6094,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, } mutex_unlock(&kvm->lock); break; + case KVM_CAP_X86_NOTIFY_VMEXIT: + r = -EINVAL; + if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) + break; + if (!kvm_x86_ops.has_notify_vmexit) + break; + if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED) + break; + kvm->arch.notify_window = cap->args[0] >> 32;
Setting notify_vmexit and notify_vmexit_flags needs to be done under kvm->lock, and changing notify_window if kvm->created_vcpus > 0 needs to disallowed, otherwise init_vmcs() will use the wrong value.
notify_vmexit_flags could be changed on the fly, but I doubt that's worth supporting as even the smallest amount of complexity will go unused.
So I think this?
case KVM_CAP_X86_NOTIFY_VMEXIT:
r = -EINVAL;
if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS)
break;
if (!kvm_x86_ops.has_notify_vmexit)
break;
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
break;
mutex_lock(&kvm->lock);
if (!kvm->created_vcpus) {
kvm->arch.notify_window = cap->args[0] >> 32;
kvm->arch.notify_vmexit_flags = (u32)cap->args[0];
r = 0;
}
mutex_unlock(&kvm->lock);
break;From efa75f6f68eb62813f9fc78d5fd410426d079edb Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Wed, 18 May 2022 13:52:51 -0700
Subject: [PATCH] KVM: x86: Introduce "struct kvm_caps" to track misc
caps/settings
Add kvm_caps to hold a variety of capabilites and defaults that aren't handled by kvm_cpu_caps because they aren't CPUID bits in order to reduce the amount of boilerplate code required to add a new feature. The vast majority (all?) of the caps interact with vendor code and are written only during initialization, i.e. should be tagged __read_mostly, declared extern in x86.h, and exported.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 15 ----- arch/x86/kvm/cpuid.c | 8 +-- arch/x86/kvm/debugfs.c | 4 +- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/svm/nested.c | 4 +- arch/x86/kvm/svm/svm.c | 13 +++-- arch/x86/kvm/vmx/nested.c | 4 +- arch/x86/kvm/vmx/vmx.c | 22 +++---- arch/x86/kvm/x86.c | 100 ++++++++++++++------------------ arch/x86/kvm/x86.h | 26 ++++++++- 10 files changed, 96 insertions(+), 102 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -15
@@ -1661,21 +1661,6 @@ extern bool tdp_enabled;u64 vcpu_tsc_khz (struct kvm_vcpu *vcpu); -/* control of guest tsc rate supported? */ -extern bool kvm_has_tsc_control; -/* maximum supported tsc_khz for guests */ -extern u32 kvm_max_guest_tsc_khz; -/* number of bits of the fractional part of the TSC scaling ratio */ -extern u8 kvm_tsc_scaling_ratio_frac_bits; -/* maximum allowed value of TSC scaling ratio */ -extern u64 kvm_max_tsc_scaling_ratio; -/* 1ull << kvm_tsc_scaling_ratio_frac_bits */ -extern u64 kvm_default_tsc_scaling_ratio; -/* bus lock detection supported? */ -extern bool kvm_has_bus_lock_exit; - -extern u64 kvm_mce_cap_supported; -/* * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing * userspace I/O) to indicate that the emulation context
arch/x86/kvm/cpuid.c +4 -4
@@ -199,7 +199,7 @@ void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)/* * Calculate guest's supported XCR0 taking into account guest CPUID data and -* supported_xcr0 (comprised of host configuration and KVM_SUPPORTED_XCR0). +* KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). */ static u64 cpuid_get_supported_xcr0 (struct kvm_cpuid_entry2 *entries,int nent) {
@@ -209,7 +209,7 @@ static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent)if (!best)return 0 ; -return (best->eax | ((u64 )best->edx <<32 )) & supported_xcr0; +return (best->eax | ((u64 )best->edx <<32 )) & kvm_caps.supported_xcr0; } static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
@@ -927,8 +927,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) }break ;case 0xd : { -u64 permitted_xcr0 = supported_xcr0 &xstate_get_guest_group_perm (); -u64 permitted_xss = supported_xss; +u64 permitted_xcr0 = kvm_caps.supported_xcr0 &xstate_get_guest_group_perm (); +u64 permitted_xss = kvm_caps.supported_xss; entry->eax &= permitted_xcr0; entry->ebx =xstate_required_size (permitted_xcr0, false);
arch/x86/kvm/debugfs.c +2 -2
@@ -48,7 +48,7 @@ DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops, vcpu_get_tsc_scaling_ratio, NULL,static int vcpu_get_tsc_scaling_frac_bits (void *data,u64 *val) { - *val = kvm_tsc_scaling_ratio_frac_bits; + *val = kvm_caps.tsc_scaling_ratio_frac_bits;return 0 ; }
@@ -66,7 +66,7 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_ debugfs_dentry, vcpu, &vcpu_timer_advance_ns_fops); -if (kvm_has_tsc_control) { +if (kvm_caps.has_tsc_control) {debugfs_create_file ("tsc-scaling-ratio" ,0444 , debugfs_dentry, vcpu, &vcpu_tsc_scaling_fops);
arch/x86/kvm/lapic.c +1 -1
@@ -1602,7 +1602,7 @@ static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)* that __delay() uses delay_tsc whenever the hardware has TSC, thus * always for VMX enabled hardware. */ -if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) { +if (vcpu->arch.tsc_scaling_ratio == kvm_caps.default_tsc_scaling_ratio) {__delay (min (guest_cycles,nsec_to_cycles (vcpu, timer_advance_ns))); }else {
arch/x86/kvm/svm/nested.c +2 -2
@@ -648,7 +648,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) vmcb02->control.tsc_offset = vcpu->arch.tsc_offset; -if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) { +if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {WARN_ON (!svm->tsc_scaling_enabled);nested_svm_update_tsc_ratio_msr (vcpu); }
@@ -979,7 +979,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm)vmcb_mark_dirty (vmcb01,VMCB_INTERCEPTS ); } -if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) { +if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {WARN_ON (!svm->tsc_scaling_enabled); vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;svm_write_tsc_multiplier (vcpu, vcpu->arch.tsc_scaling_ratio);
arch/x86/kvm/svm/svm.c +7 -6
@@ -1225,7 +1225,7 @@ static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)svm_init_osvw (vcpu); vcpu->arch.microcode_version =0x01000065 ; - svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio; + svm->tsc_ratio_msr = kvm_caps.default_tsc_scaling_ratio;if (sev_es_guest (vcpu->kvm))sev_es_vcpu_reset (svm);
@@ -4769,7 +4769,7 @@ static __init void svm_set_cpu_caps(void) {kvm_set_cpu_caps (); - supported_xss =0 ; + kvm_caps.supported_xss =0 ;/* CPUID 0x80000001 and 0x8000000A (SVM features) */ if (nested) {
@@ -4845,7 +4845,8 @@ static __init int svm_hardware_setup(void)init_msrpm_offsets (); - supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); + kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | +XFEATURE_MASK_BNDCSR );if (boot_cpu_has (X86_FEATURE_FXSR_OPT ))kvm_enable_efer_bits (EFER_FFXSR );
@@ -4855,11 +4856,11 @@ static __init int svm_hardware_setup(void) tsc_scaling = false; }else {pr_info ("TSC scaling supported\n" ); - kvm_has_tsc_control = true; + kvm_caps.has_tsc_control = true; } } - kvm_max_tsc_scaling_ratio =SVM_TSC_RATIO_MAX ; - kvm_tsc_scaling_ratio_frac_bits =32 ; + kvm_caps.max_tsc_scaling_ratio =SVM_TSC_RATIO_MAX ; + kvm_caps.tsc_scaling_ratio_frac_bits =32 ; tsc_aux_uret_slot =kvm_add_user_return_msr (MSR_TSC_AUX );
arch/x86/kvm/vmx/nested.c +2 -2
@@ -2548,7 +2548,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmx_get_l2_tsc_multiplier(vcpu));vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset); -if (kvm_has_tsc_control) +if (kvm_caps.has_tsc_control)vmcs_write64 (TSC_MULTIPLIER , vcpu->arch.tsc_scaling_ratio);nested_vmx_transition_tlb_flush (vcpu, vmcs12, true);
@@ -4610,7 +4610,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset); -if (kvm_has_tsc_control) +if (kvm_caps.has_tsc_control)vmcs_write64 (TSC_MULTIPLIER , vcpu->arch.tsc_scaling_ratio);if (vmx->nested.l1_tpr_threshold !=-1 )
arch/x86/kvm/vmx/vmx.c +11 -11
@@ -1715,7 +1715,7 @@ u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu) nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))return vmcs12->tsc_multiplier; -return kvm_default_tsc_scaling_ratio; +return kvm_caps.default_tsc_scaling_ratio; }static void vmx_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset)
@@ -7537,7 +7537,7 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - supported_xss =0 ; + kvm_caps.supported_xss =0 ;if (!cpu_has_vmx_xsaves ())kvm_cpu_cap_clear (X86_FEATURE_XSAVES );
@@ -7678,9 +7678,9 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, delta_tsc =0 ;/* Convert to host delta tsc if tsc scaling is enabled */ -if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio && delta_tsc &&u64_shl_div_u64 (delta_tsc, - kvm_tsc_scaling_ratio_frac_bits, + kvm_caps.tsc_scaling_ratio_frac_bits, vcpu->arch.l1_tsc_scaling_ratio, &delta_tsc))return -ERANGE ;
@@ -8057,8 +8057,8 @@ static __init int hardware_setup(void) }if (!cpu_has_vmx_mpx ()) - supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | -XFEATURE_MASK_BNDCSR ); + kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | +XFEATURE_MASK_BNDCSR );if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ()))
@@ -8125,11 +8125,11 @@ static __init int hardware_setup(void) enable_ipiv = false;if (cpu_has_vmx_tsc_scaling ()) - kvm_has_tsc_control = true; + kvm_caps.has_tsc_control = true; - kvm_max_tsc_scaling_ratio =KVM_VMX_TSC_MULTIPLIER_MAX ; - kvm_tsc_scaling_ratio_frac_bits =48 ; - kvm_has_bus_lock_exit =cpu_has_vmx_bus_lock_detection (); + kvm_caps.max_tsc_scaling_ratio =KVM_VMX_TSC_MULTIPLIER_MAX ; + kvm_caps.tsc_scaling_ratio_frac_bits =48 ; + kvm_caps.has_bus_lock_exit =cpu_has_vmx_bus_lock_detection ();set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */
@@ -8186,7 +8186,7 @@ static __init int hardware_setup(void) vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit; } - kvm_mce_cap_supported |=MCG_LMCE_P ; + kvm_caps.supported_mce_cap |=MCG_LMCE_P ;if (pt_mode !=PT_MODE_SYSTEM && pt_mode !=PT_MODE_HOST_GUEST )return -EINVAL ;
arch/x86/kvm/x86.c +44 -56
299 lines not on this page · raw message
arch/x86/kvm/x86.h +23 -3
@@ -8,6 +8,25 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" +struct kvm_caps { +/* control of guest tsc rate supported? */ +bool has_tsc_control; +/* maximum supported tsc_khz for guests */ +u32 max_guest_tsc_khz; +/* number of bits of the fractional part of the TSC scaling ratio */ +u8 tsc_scaling_ratio_frac_bits; +/* maximum allowed value of TSC scaling ratio */ +u64 max_tsc_scaling_ratio; +/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */ +u64 default_tsc_scaling_ratio; +/* bus lock detection supported? */ +bool has_bus_lock_exit; + +u64 supported_mce_cap; +u64 supported_xcr0; +u64 supported_xss; +}; +void kvm_spurious_fault (void );#define KVM_NESTED_VMENTER_CONSISTENCY_CHECK (consistency_check) \
@@ -283,14 +302,15 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,fastpath_t handle_fastpath_set_msr_irqoff (struct kvm_vcpu *vcpu);extern u64 host_xcr0; -extern u64 supported_xcr0;extern u64 host_xss; -extern u64 supported_xss; + +extern struct kvm_caps kvm_caps; +extern bool enable_pmu;static inline bool kvm_mpx_supported (void ) { -return (supported_xcr0 & (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR )) +return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR )) == (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); }
base-commit: a3808d88461270c71d3fece5e51cc486ecdac7d0
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Re: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
Quoted 31 lines · Sean Christopherson
Review struct kvm_x86_ops { · line 1515
Quoted 6 lines · Chenyi Qiang
(quoted diff) +2
-0@@ -1504,6 +1511,8 @@ struct kvm_x86_ops { * Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + + bool has_notify_vmexit;I'm pretty sure I suggested this, but seeing it in code, it kinda sorta makes things worst if we don't first consolidate the existing flags.
kvm_x86_opsworks, but we'd definitely be taking liberties with the "ops" part.What about adding struct
kvm_capsto collect these flags/settings that don't fit intokvm_cpu_capsbecause they're not a CPUID feature flag?kvm_x86_opshas the advantage of kinda being read-only after init since VMX modifiesvmx_x86_ops, but IMO that's not enough reason to shove this intokvm_x86_ops. And long term, we might be able find a way to markkvm_capsas full__ro_after_init.If no one objects, the attached patch can slide in before this patch, then
has_notifiy_vmexitcan land inkvm_caps.struct kvm_caps { /* control of guest tsc rate supported? */ bool has_tsc_control; /* maximum supported tsc_khz for guests */ u32 max_guest_tsc_khz; /* number of bits of the fractional part of the TSC scaling ratio */ u8 tsc_scaling_ratio_frac_bits; /* maximum allowed value of TSC scaling ratio */ u64 max_tsc_scaling_ratio; /* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
Quoted 9 lines
u64 default_tsc_scaling_ratio; /* bus lock detection supported? */ bool has_bus_lock_exit; u64 supported_mce_cap; u64 supported_xcr0; u64 supported_xss; };
Thanks Sean for your patch. I think an unintentional change is mixed in it:
@@ -4739,7 +4725,8 @@ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
return (kvm_arch_interrupt_allowed(vcpu) &&
kvm_cpu_accept_dm_intr(vcpu) && !kvm_event_needs_reinjection(vcpu) &&
- !vcpu->arch.exception.pending);
+ !vcpu->arch.exception.pending &&
+ !kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu));
}
Maybe this should belong to the patch 1?
Quoted 18 lines
Review int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6105
Quoted 13 lines
(quoted diff) +9
-0@@ -6090,6 +6094,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, } mutex_unlock(&kvm->lock); break; + case KVM_CAP_X86_NOTIFY_VMEXIT: + r = -EINVAL; + if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) + break; + if (!kvm_x86_ops.has_notify_vmexit) + break; + if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED) + break; + kvm->arch.notify_window = cap->args[0] >> 32;Setting
notify_vmexitandnotify_vmexit_flagsneeds to be done underkvm->lock, and changingnotify_windowifkvm->created_vcpus> 0 needs to disallowed, otherwiseinit_vmcs()will use the wrong value.
Quoted 5 lines
notify_vmexit_flagscould be changed on the fly, but I doubt that's worth supporting as even the smallest amount of complexity will go unused.So I think this?
Make sense.
Quoted 16 lines
case KVM_CAP_X86_NOTIFY_VMEXIT:
r = -EINVAL;
if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS)
break;
if (!kvm_x86_ops.has_notify_vmexit)
break;
if (!(u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)
break;
mutex_lock(&kvm->lock);
if (!kvm->created_vcpus) {
kvm->arch.notify_window = cap->args[0] >> 32;
kvm->arch.notify_vmexit_flags = (u32)cap->args[0];
r = 0;
}
mutex_unlock(&kvm->lock);
break;Sean Christopherson
maintainerRe: [PATCH v6 3/3] KVM: VMX: Enable Notify VM exit
Quoted 15 lines · Chenyi Qiang
On 5/19/2022 6:30 AM, Sean Christopherson wrote:
Thanks Sean for your patch. I think an unintentional change is mixed in it:(quoted diff) +2
-1@@ -4739,7 +4725,8 @@ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) return (kvm_arch_interrupt_allowed(vcpu) && kvm_cpu_accept_dm_intr(vcpu) && !kvm_event_needs_reinjection(vcpu) && - !vcpu->arch.exception.pending); + !vcpu->arch.exception.pending && + !kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu)); }
Quoted 1 line
Maybe this should belong to the patch 1?
Good eyes! Let's keep it out for now. I do think it's the right behavior, but it is woefully incomplete; pretty much every patch that checks vcpu->arch.exception.pending also needs to check for pending triple fault.
I'll add a patch to my exception/event cleanup series[*], which introduces kvm_is_exception_pending(), i.e. provides exactly the wrapper needed to handle a pending triple fault with minimal effort.
[*] https://lore.kernel.org/all/[email protected]
Here's a new version without the spurious change...
--
From: Sean Christopherson <[email protected]>
Date: Wed, 18 May 2022 13:52:51 -0700
Subject: [PATCH] KVM: x86: Introduce "struct kvm_caps" to track misc
caps/settings
Add kvm_caps to hold a variety of capabilites and defaults that aren't handled by kvm_cpu_caps because they aren't CPUID bits in order to reduce the amount of boilerplate code required to add a new feature. The vast majority (all?) of the caps interact with vendor code and are written only during initialization, i.e. should be tagged __read_mostly, declared extern in x86.h, and exported.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 15 ----- arch/x86/kvm/cpuid.c | 8 +-- arch/x86/kvm/debugfs.c | 4 +- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/svm/nested.c | 4 +- arch/x86/kvm/svm/svm.c | 13 +++-- arch/x86/kvm/vmx/nested.c | 4 +- arch/x86/kvm/vmx/vmx.c | 22 ++++---- arch/x86/kvm/x86.c | 97 ++++++++++++++------------------- arch/x86/kvm/x86.h | 26 ++++++++- 10 files changed, 94 insertions(+), 101 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -15
@@ -1661,21 +1661,6 @@ extern bool tdp_enabled;u64 vcpu_tsc_khz (struct kvm_vcpu *vcpu); -/* control of guest tsc rate supported? */ -extern bool kvm_has_tsc_control; -/* maximum supported tsc_khz for guests */ -extern u32 kvm_max_guest_tsc_khz; -/* number of bits of the fractional part of the TSC scaling ratio */ -extern u8 kvm_tsc_scaling_ratio_frac_bits; -/* maximum allowed value of TSC scaling ratio */ -extern u64 kvm_max_tsc_scaling_ratio; -/* 1ull << kvm_tsc_scaling_ratio_frac_bits */ -extern u64 kvm_default_tsc_scaling_ratio; -/* bus lock detection supported? */ -extern bool kvm_has_bus_lock_exit; - -extern u64 kvm_mce_cap_supported; -/* * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing * userspace I/O) to indicate that the emulation context
arch/x86/kvm/cpuid.c +4 -4
@@ -199,7 +199,7 @@ void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)/* * Calculate guest's supported XCR0 taking into account guest CPUID data and -* supported_xcr0 (comprised of host configuration and KVM_SUPPORTED_XCR0). +* KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). */ static u64 cpuid_get_supported_xcr0 (struct kvm_cpuid_entry2 *entries,int nent) {
@@ -209,7 +209,7 @@ static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent)if (!best)return 0 ; -return (best->eax | ((u64 )best->edx <<32 )) & supported_xcr0; +return (best->eax | ((u64 )best->edx <<32 )) & kvm_caps.supported_xcr0; } static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
@@ -927,8 +927,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) }break ;case 0xd : { -u64 permitted_xcr0 = supported_xcr0 &xstate_get_guest_group_perm (); -u64 permitted_xss = supported_xss; +u64 permitted_xcr0 = kvm_caps.supported_xcr0 &xstate_get_guest_group_perm (); +u64 permitted_xss = kvm_caps.supported_xss; entry->eax &= permitted_xcr0; entry->ebx =xstate_required_size (permitted_xcr0, false);
arch/x86/kvm/debugfs.c +2 -2
@@ -48,7 +48,7 @@ DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops, vcpu_get_tsc_scaling_ratio, NULL,static int vcpu_get_tsc_scaling_frac_bits (void *data,u64 *val) { - *val = kvm_tsc_scaling_ratio_frac_bits; + *val = kvm_caps.tsc_scaling_ratio_frac_bits;return 0 ; }
@@ -66,7 +66,7 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_ debugfs_dentry, vcpu, &vcpu_timer_advance_ns_fops); -if (kvm_has_tsc_control) { +if (kvm_caps.has_tsc_control) {debugfs_create_file ("tsc-scaling-ratio" ,0444 , debugfs_dentry, vcpu, &vcpu_tsc_scaling_fops);
arch/x86/kvm/lapic.c +1 -1
@@ -1602,7 +1602,7 @@ static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)* that __delay() uses delay_tsc whenever the hardware has TSC, thus * always for VMX enabled hardware. */ -if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) { +if (vcpu->arch.tsc_scaling_ratio == kvm_caps.default_tsc_scaling_ratio) {__delay (min (guest_cycles,nsec_to_cycles (vcpu, timer_advance_ns))); }else {
arch/x86/kvm/svm/nested.c +2 -2
@@ -648,7 +648,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) vmcb02->control.tsc_offset = vcpu->arch.tsc_offset; -if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) { +if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {WARN_ON (!svm->tsc_scaling_enabled);nested_svm_update_tsc_ratio_msr (vcpu); }
@@ -979,7 +979,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm)vmcb_mark_dirty (vmcb01,VMCB_INTERCEPTS ); } -if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) { +if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {WARN_ON (!svm->tsc_scaling_enabled); vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;svm_write_tsc_multiplier (vcpu, vcpu->arch.tsc_scaling_ratio);
arch/x86/kvm/svm/svm.c +7 -6
@@ -1225,7 +1225,7 @@ static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)svm_init_osvw (vcpu); vcpu->arch.microcode_version =0x01000065 ; - svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio; + svm->tsc_ratio_msr = kvm_caps.default_tsc_scaling_ratio;if (sev_es_guest (vcpu->kvm))sev_es_vcpu_reset (svm);
@@ -4769,7 +4769,7 @@ static __init void svm_set_cpu_caps(void) {kvm_set_cpu_caps (); - supported_xss =0 ; + kvm_caps.supported_xss =0 ;/* CPUID 0x80000001 and 0x8000000A (SVM features) */ if (nested) {
@@ -4845,7 +4845,8 @@ static __init int svm_hardware_setup(void)init_msrpm_offsets (); - supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); + kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | +XFEATURE_MASK_BNDCSR );if (boot_cpu_has (X86_FEATURE_FXSR_OPT ))kvm_enable_efer_bits (EFER_FFXSR );
@@ -4855,11 +4856,11 @@ static __init int svm_hardware_setup(void) tsc_scaling = false; }else {pr_info ("TSC scaling supported\n" ); - kvm_has_tsc_control = true; + kvm_caps.has_tsc_control = true; } } - kvm_max_tsc_scaling_ratio =SVM_TSC_RATIO_MAX ; - kvm_tsc_scaling_ratio_frac_bits =32 ; + kvm_caps.max_tsc_scaling_ratio =SVM_TSC_RATIO_MAX ; + kvm_caps.tsc_scaling_ratio_frac_bits =32 ; tsc_aux_uret_slot =kvm_add_user_return_msr (MSR_TSC_AUX );
arch/x86/kvm/vmx/nested.c +2 -2
@@ -2548,7 +2548,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmx_get_l2_tsc_multiplier(vcpu));vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset); -if (kvm_has_tsc_control) +if (kvm_caps.has_tsc_control)vmcs_write64 (TSC_MULTIPLIER , vcpu->arch.tsc_scaling_ratio);nested_vmx_transition_tlb_flush (vcpu, vmcs12, true);
@@ -4610,7 +4610,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT , vmx->msr_autoload.host.nr);vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT , vmx->msr_autoload.guest.nr);vmcs_write64 (TSC_OFFSET , vcpu->arch.tsc_offset); -if (kvm_has_tsc_control) +if (kvm_caps.has_tsc_control)vmcs_write64 (TSC_MULTIPLIER , vcpu->arch.tsc_scaling_ratio);if (vmx->nested.l1_tpr_threshold !=-1 )
arch/x86/kvm/vmx/vmx.c +11 -11
@@ -1715,7 +1715,7 @@ u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu) nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))return vmcs12->tsc_multiplier; -return kvm_default_tsc_scaling_ratio; +return kvm_caps.default_tsc_scaling_ratio; }static void vmx_write_tsc_offset (struct kvm_vcpu *vcpu,u64 offset)
@@ -7537,7 +7537,7 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - supported_xss =0 ; + kvm_caps.supported_xss =0 ;if (!cpu_has_vmx_xsaves ())kvm_cpu_cap_clear (X86_FEATURE_XSAVES );
@@ -7678,9 +7678,9 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, delta_tsc =0 ;/* Convert to host delta tsc if tsc scaling is enabled */ -if (vcpu->arch.l1_tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio && delta_tsc &&u64_shl_div_u64 (delta_tsc, - kvm_tsc_scaling_ratio_frac_bits, + kvm_caps.tsc_scaling_ratio_frac_bits, vcpu->arch.l1_tsc_scaling_ratio, &delta_tsc))return -ERANGE ;
@@ -8057,8 +8057,8 @@ static __init int hardware_setup(void) }if (!cpu_has_vmx_mpx ()) - supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | -XFEATURE_MASK_BNDCSR ); + kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | +XFEATURE_MASK_BNDCSR );if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ()))
@@ -8125,11 +8125,11 @@ static __init int hardware_setup(void) enable_ipiv = false;if (cpu_has_vmx_tsc_scaling ()) - kvm_has_tsc_control = true; + kvm_caps.has_tsc_control = true; - kvm_max_tsc_scaling_ratio =KVM_VMX_TSC_MULTIPLIER_MAX ; - kvm_tsc_scaling_ratio_frac_bits =48 ; - kvm_has_bus_lock_exit =cpu_has_vmx_bus_lock_detection (); + kvm_caps.max_tsc_scaling_ratio =KVM_VMX_TSC_MULTIPLIER_MAX ; + kvm_caps.tsc_scaling_ratio_frac_bits =48 ; + kvm_caps.has_bus_lock_exit =cpu_has_vmx_bus_lock_detection ();set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */
@@ -8186,7 +8186,7 @@ static __init int hardware_setup(void) vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit; } - kvm_mce_cap_supported |=MCG_LMCE_P ; + kvm_caps.supported_mce_cap |=MCG_LMCE_P ;if (pt_mode !=PT_MODE_SYSTEM && pt_mode !=PT_MODE_HOST_GUEST )return -EINVAL ;
arch/x86/kvm/x86.c +42 -55
290 lines not on this page · raw message
arch/x86/kvm/x86.h +23 -3
@@ -8,6 +8,25 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" +struct kvm_caps { +/* control of guest tsc rate supported? */ +bool has_tsc_control; +/* maximum supported tsc_khz for guests */ +u32 max_guest_tsc_khz; +/* number of bits of the fractional part of the TSC scaling ratio */ +u8 tsc_scaling_ratio_frac_bits; +/* maximum allowed value of TSC scaling ratio */ +u64 max_tsc_scaling_ratio; +/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */ +u64 default_tsc_scaling_ratio; +/* bus lock detection supported? */ +bool has_bus_lock_exit; + +u64 supported_mce_cap; +u64 supported_xcr0; +u64 supported_xss; +}; +void kvm_spurious_fault (void );#define KVM_NESTED_VMENTER_CONSISTENCY_CHECK (consistency_check) \
@@ -283,14 +302,15 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,fastpath_t handle_fastpath_set_msr_irqoff (struct kvm_vcpu *vcpu);extern u64 host_xcr0; -extern u64 supported_xcr0;extern u64 host_xss; -extern u64 supported_xss; + +extern struct kvm_caps kvm_caps; +extern bool enable_pmu;static inline bool kvm_mpx_supported (void ) { -return (supported_xcr0 & (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR )) +return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR )) == (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); }
base-commit: a3808d88461270c71d3fece5e51cc486ecdac7d0 --
2.36.1.124.g0e6072fb45-goog
Kindly ping for the comments.
Quoted 86 lines · Chenyi Qiang
Virtual machines can exploit Intel ISA characterstics to cause functional denial of service to the VMM. This series introduces a new feature named Notify VM exit, which can help mitigate such kind of attacks.
Patch 1: An extension of
KVM_SET_VCPU_EVENTSioctl to inject a synthesized shutdown event from user space. This is also a fix for other synthesized triple fault, e.g. the RSM patch ornested_vmx_abort(), which could get lost when exit to userspace to do migrate.Patch 2: A selftest about get/set triple fault event.
Patch 3: The main patch to enable Notify VM exit.
---
Change logs:
v5 -> v6
- Do some changes in document.
- Add a selftest about get/set triple fault event. (Sean)
- extend the argument to include both the notify window and some flags when enablingKVM_CAP_X86_BUS_LOCK_EXITCAP. (Sean)
- Change to useKVM_VCPUEVENT_VALID_TRIPE_FAULTin flags field and addpending_triple_faultfield in structkvm_vcpu_events, which allows userspace to make/clear triple fault request. (Sean)
- Add a flag inkvm_x86_opsto avoid thekvm_has_notify_vmexitglobal varialbe and its export.(Sean)
- v5: https://lore.kernel.org/lkml/[email protected]/v4 -> v5
- renameKVM_VCPUEVENTS_SHUTDOWNtoKVM_VCPUEVENTS_TRIPLE_FAULT. Make it bidirection and add it toget_vcpu_events. (Sean)
- v4: https://lore.kernel.org/all/[email protected]/v3 -> v4
- Change this feature to per-VM scope. (Jim)
- OnceVM_CONTEXT_INVALIDset inexit_qualification, exit to user space notify this fatal case, especially the notify VM exit happens in L2. (Jim)
- extendKVM_SET_VCPU_EVENTSto allow user space to inject a shutdown event. (Jim)
- A minor code changes.
- Add document for the new KVM capability.
- v3: https://lore.kernel.org/lkml/[email protected]/v2 -> v3
- add a vcpu statenotify_window_exitsto record the number of occurence as well as apr_warnoutput. (Sean)
- Add the handling in nested VM to prevent L1 bypassing the restriction through launching a L2. (Sean)
- Only kill L2 when L2 VM is context invalid, synthesize aEXIT_REASON_TRIPLE_FAULTto L1 (Sean)
- To ease the current implementation, make module parameternotify_windowread-only. (Sean)
- Disable notify window exit by default.
- v2: https://lore.kernel.org/lkml/[email protected]/v1 -> v2
- Default set notify window to 0, less than 0 to disable.
- Add more description in commit message.
---Chenyi Qiang (2):
KVM: X86: Save&restore the triple fault request
KVM: selftests: Add a test to get/set triple fault eventTao Xu (1):
KVM: VMX: Enable Notify VM exitDocumentation/virt/kvm/api.rst | 55 +++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/include/asm/vmx.h | 7 ++ arch/x86/include/asm/vmxfeatures.h | 1 + arch/x86/include/uapi/asm/kvm.h | 4 +- arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx/capabilities.h | 6 ++ arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/vmx.c | 48 +++++++++- arch/x86/kvm/x86.c | 33 ++++++- arch/x86/kvm/x86.h | 5 + include/uapi/linux/kvm.h | 10 ++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/triple_fault_event_test.c | 96 +++++++++++++++++++ 15 files changed, 280 insertions(+), 8 deletions(-)create mode 100644
tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c
Paolo Bonzini
maintainerQuoted 5 lines · Chenyi Qiang
Virtual machines can exploit Intel ISA characterstics to cause functional denial of service to the VMM. This series introduces a new feature named Notify VM exit, which can help mitigate such kind of attacks.
Quoted 8 lines
Patch 1: An extension of
KVM_SET_VCPU_EVENTSioctl to inject a synthesized shutdown event from user space. This is also a fix for other synthesized triple fault, e.g. the RSM patch ornested_vmx_abort(), which could get lost when exit to userspace to do migrate.Patch 2: A selftest about get/set triple fault event.
Patch 3: The main patch to enable Notify VM exit.
Chenyi, can you send v7 for inclusion?
Paolo
Quoted 72 lines
---
Change logs:
v5 -> v6
- Do some changes in document.
- Add a selftest about get/set triple fault event. (Sean)
- extend the argument to include both the notify window and some flags when enablingKVM_CAP_X86_BUS_LOCK_EXITCAP. (Sean)
- Change to useKVM_VCPUEVENT_VALID_TRIPE_FAULTin flags field and addpending_triple_faultfield in structkvm_vcpu_events, which allows userspace to make/clear triple fault request. (Sean)
- Add a flag inkvm_x86_opsto avoid thekvm_has_notify_vmexitglobal varialbe and its export.(Sean)
- v5: https://lore.kernel.org/lkml/[email protected]/v4 -> v5
- renameKVM_VCPUEVENTS_SHUTDOWNtoKVM_VCPUEVENTS_TRIPLE_FAULT. Make it bidirection and add it toget_vcpu_events. (Sean)
- v4: https://lore.kernel.org/all/[email protected]/v3 -> v4
- Change this feature to per-VM scope. (Jim)
- OnceVM_CONTEXT_INVALIDset inexit_qualification, exit to user space notify this fatal case, especially the notify VM exit happens in L2. (Jim)
- extendKVM_SET_VCPU_EVENTSto allow user space to inject a shutdown event. (Jim)
- A minor code changes.
- Add document for the new KVM capability.
- v3: https://lore.kernel.org/lkml/[email protected]/v2 -> v3
- add a vcpu statenotify_window_exitsto record the number of occurence as well as apr_warnoutput. (Sean)
- Add the handling in nested VM to prevent L1 bypassing the restriction through launching a L2. (Sean)
- Only kill L2 when L2 VM is context invalid, synthesize aEXIT_REASON_TRIPLE_FAULTto L1 (Sean)
- To ease the current implementation, make module parameternotify_windowread-only. (Sean)
- Disable notify window exit by default.
- v2: https://lore.kernel.org/lkml/[email protected]/v1 -> v2
- Default set notify window to 0, less than 0 to disable.
- Add more description in commit message.
---Chenyi Qiang (2):
KVM: X86: Save&restore the triple fault request
KVM: selftests: Add a test to get/set triple fault eventTao Xu (1):
KVM: VMX: Enable Notify VM exitDocumentation/virt/kvm/api.rst | 55 +++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/include/asm/vmx.h | 7 ++ arch/x86/include/asm/vmxfeatures.h | 1 + arch/x86/include/uapi/asm/kvm.h | 4 +- arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx/capabilities.h | 6 ++ arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/vmx.c | 48 +++++++++- arch/x86/kvm/x86.c | 33 ++++++- arch/x86/kvm/x86.h | 5 + include/uapi/linux/kvm.h | 10 ++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/triple_fault_event_test.c | 96 +++++++++++++++++++ 15 files changed, 280 insertions(+), 8 deletions(-)create mode 100644
tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Chenyi Qiang
Virtual machines can exploit Intel ISA characterstics to cause functional denial of service to the VMM. This series introduces a new feature named Notify VM exit, which can help mitigate such kind of attacks.
Patch 1: An extension of
KVM_SET_VCPU_EVENTSioctl to inject a synthesized shutdown event from user space. This is also a fix for other synthesized triple fault, e.g. the RSM patch ornested_vmx_abort(), which could get lost when exit to userspace to do migrate.
Quoted 8 lines
Quoted 3 lines
Patch 2: A selftest about get/set triple fault event.
Patch 3: The main patch to enable Notify VM exit.
Chenyi, can you send v7 for inclusion?
Paolo
Hi Paolo
v7 is sent out at
https://lore.kernel.org/lkml/[email protected]/
Quoted 80 lines
Quoted 79 lines
---
Change logs:
v5 -> v6
- Do some changes in document.
- Add a selftest about get/set triple fault event. (Sean)
- extend the argument to include both the notify window and some flags when enablingKVM_CAP_X86_BUS_LOCK_EXITCAP. (Sean)
- Change to useKVM_VCPUEVENT_VALID_TRIPE_FAULTin flags field and addpending_triple_faultfield in structkvm_vcpu_events, which allows userspace to make/clear triple fault request. (Sean)
- Add a flag inkvm_x86_opsto avoid thekvm_has_notify_vmexitglobal varialbe and its export.(Sean)
- v5:
https://lore.kernel.org/lkml/[email protected]/v4 -> v5
- renameKVM_VCPUEVENTS_SHUTDOWNtoKVM_VCPUEVENTS_TRIPLE_FAULT. Make it bidirection and add it toget_vcpu_events. (Sean)
- v4:
https://lore.kernel.org/all/[email protected]/v3 -> v4
- Change this feature to per-VM scope. (Jim)
- OnceVM_CONTEXT_INVALIDset inexit_qualification, exit to user space notify this fatal case, especially the notify VM exit happens in L2. (Jim)
- extendKVM_SET_VCPU_EVENTSto allow user space to inject a shutdown event. (Jim)
- A minor code changes.
- Add document for the new KVM capability.
- v3:
https://lore.kernel.org/lkml/[email protected]/v2 -> v3
- add a vcpu statenotify_window_exitsto record the number of occurence as well as apr_warnoutput. (Sean)
- Add the handling in nested VM to prevent L1 bypassing the restriction through launching a L2. (Sean)
- Only kill L2 when L2 VM is context invalid, synthesize aEXIT_REASON_TRIPLE_FAULTto L1 (Sean)
- To ease the current implementation, make module parameternotify_windowread-only. (Sean)
- Disable notify window exit by default.
- v2:
https://lore.kernel.org/lkml/[email protected]/v1 -> v2
- Default set notify window to 0, less than 0 to disable.
- Add more description in commit message.
---Chenyi Qiang (2):
KVM: X86: Save&restore the triple fault request
KVM: selftests: Add a test to get/set triple fault eventTao Xu (1):
KVM: VMX: Enable Notify VM exitDocumentation/virt/kvm/api.rst | 55 +++++++++++ arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/include/asm/vmx.h | 7 ++ arch/x86/include/asm/vmxfeatures.h | 1 +
arch/x86/include/uapi/asm/kvm.h | 4 +- arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx/capabilities.h | 6 ++arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/vmx.c | 48 +++++++++- arch/x86/kvm/x86.c | 33 ++++++- arch/x86/kvm/x86.h | 5 + include/uapi/linux/kvm.h | 10 ++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/triple_fault_event_test.c | 96 +++++++++++++++++++
15 files changed, 280 insertions(+), 8 deletions(-)create mode 100644
tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c