2023-01-21 02:49:16

by Kechen Lu

[permalink] [raw]
Subject: [RFC PATCH v6 0/6] KVM: x86: add per-vCPU exits disable capability

Summary
===========
Introduce support of vCPU-scoped ioctl with KVM_CAP_X86_DISABLE_EXITS
cap for disabling exits to enable finer-grained VM exits disabling
on per vCPU scales instead of whole guest. This patch series enabled
the vCPU-scoped exits control and toggling.

Motivation
============
In use cases like Windows guest running heavy CPU-bound
workloads, disabling HLT VM-exits could mitigate host sched ctx switch
overhead. Simply HLT disabling on all vCPUs could bring
performance benefits, but if no pCPUs reserved for host threads, could
happened to the forced preemption as host does not know the time to do
the schedule for other host threads want to run. With this patch, we
could only disable part of vCPUs HLT exits for one guest, this still
keeps performance benefits, and also shows resiliency to host stressing
workload running at the same time.

Performance and Testing
=========================
In the host stressing workload experiment with Windows guest heavy
CPU-bound workloads, it shows good resiliency and having the ~3%
performance improvement. E.g. Passmark running in a Windows guest
with this patch disabling HLT exits on only half of vCPUs still
showing 2.4% higher main score v/s baseline.

Tested on AMD machines.

v5->v6 :
- Correct the selftest tests design for HLT executes in guest cases,
wakeup is required for all these cases (Zhi Wang)
- Refactor the test helper functions encapsulation

v4->v5 :
- Drop the usage of KVM request, keep the VM-scoped exits disable
as the existing design, and only allow per-vCPU settings to
override the per-VM settings (Sean Christopherson)
- Refactor the disable exits selftest without introducing any
new prerequisite patch, tests per-vCPU exits disable and overrides,
and per-VM exits disable

v3->v4 (Chao Gao) :
- Use kvm vCPU request KVM_REQ_DISABLE_EXIT to perform the arch
VMCS updating (patch 5)
- Fix selftests redundant arguments (patch 7)
- Merge overlapped fix bits from patch 4 to patch 3

v2->v3 (Sean Christopherson) :
- Reject KVM_CAP_X86_DISABLE_EXITS if userspace disable MWAIT exits
when MWAIT is not allowed in guest (patch 3)
- Make userspace able to re-enable previously disabled exits (patch 4)
- Add mwait/pause/cstate exits flag toggling instead of only hlt
exits (patch 5)
- Add selftests for KVM_CAP_X86_DISABLE_EXITS (patch 7)

v1->v2 (Sean Christopherson) :
- Add explicit restriction for VM-scoped exits disabling to be called
before vCPUs creation (patch 1)
- Use vCPU ioctl instead of 64bit vCPU bitmask (patch 5), and make exits
disable flags check purely for vCPU instead of VM (patch 2)

Best Regards,
Kechen

Kechen Lu (3):
KVM: x86: Move *_in_guest power management flags to vCPU scope
KVM: x86: add vCPU scoped toggling for disabled exits
KVM: selftests: Add tests for VM and vCPU cap
KVM_CAP_X86_DISABLE_EXITS

Sean Christopherson (3):
KVM: x86: only allow exits disable before vCPUs created
KVM: x86: Reject disabling of MWAIT interception when not allowed
KVM: x86: Let userspace re-enable previously disabled exits

Documentation/virt/kvm/api.rst | 8 +-
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 7 +
arch/x86/kvm/cpuid.c | 4 +-
arch/x86/kvm/lapic.c | 7 +-
arch/x86/kvm/svm/nested.c | 4 +-
arch/x86/kvm/svm/svm.c | 42 +-
arch/x86/kvm/vmx/vmx.c | 53 ++-
arch/x86/kvm/x86.c | 69 +++-
arch/x86/kvm/x86.h | 16 +-
include/uapi/linux/kvm.h | 4 +-
tools/testing/selftests/kvm/Makefile | 1 +
.../selftests/kvm/x86_64/disable_exits_test.c | 363 ++++++++++++++++++
13 files changed, 532 insertions(+), 47 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86_64/disable_exits_test.c

--
2.34.1


2023-01-21 03:20:05

by Kechen Lu

[permalink] [raw]
Subject: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT interception when not allowed

From: Sean Christopherson <[email protected]>

Reject KVM_CAP_X86_DISABLE_EXITS if userspace attempts to disable MWAIT
exits and KVM previously reported (via KVM_CHECK_EXTENSION) that MWAIT is
not allowed in guest, e.g. because it's not supported or the CPU doesn't
have an aways-running APIC timer.

Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable MWAIT intercepts")

Signed-off-by: Sean Christopherson <[email protected]>
Signed-off-by: Kechen Lu <[email protected]>
---
arch/x86/kvm/x86.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9a77b55142c6..60caa3fd40e5 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4326,6 +4326,16 @@ static inline bool kvm_can_mwait_in_guest(void)
boot_cpu_has(X86_FEATURE_ARAT);
}

+static u64 kvm_get_allowed_disable_exits(void)
+{
+ u64 r = KVM_X86_DISABLE_VALID_EXITS;
+
+ if (!kvm_can_mwait_in_guest())
+ r &= ~KVM_X86_DISABLE_EXITS_MWAIT;
+
+ return r;
+}
+
static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
struct kvm_cpuid2 __user *cpuid_arg)
{
@@ -4448,10 +4458,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
r = KVM_CLOCK_VALID_FLAGS;
break;
case KVM_CAP_X86_DISABLE_EXITS:
- r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
- KVM_X86_DISABLE_EXITS_CSTATE;
- if(kvm_can_mwait_in_guest())
- r |= KVM_X86_DISABLE_EXITS_MWAIT;
+ r |= kvm_get_allowed_disable_exits();
break;
case KVM_CAP_X86_SMM:
if (!IS_ENABLED(CONFIG_KVM_SMM))
@@ -6224,15 +6231,14 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
break;
case KVM_CAP_X86_DISABLE_EXITS:
r = -EINVAL;
- if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
+ if (cap->args[0] & ~kvm_get_allowed_disable_exits())
break;

mutex_lock(&kvm->lock);
if (kvm->created_vcpus)
goto disable_exits_unlock;

- if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
- kvm_can_mwait_in_guest())
+ if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT)
kvm->arch.mwait_in_guest = true;
if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
kvm->arch.hlt_in_guest = true;
--
2.34.1

2023-01-31 12:04:35

by Zhao Liu

[permalink] [raw]
Subject: Re: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT interception when not allowed

On Sat, Jan 21, 2023 at 02:07:35AM +0000, Kechen Lu wrote:
> Date: Sat, 21 Jan 2023 02:07:35 +0000
> From: Kechen Lu <[email protected]>
> Subject: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT
> interception when not allowed
> X-Mailer: git-send-email 2.34.1
>
> From: Sean Christopherson <[email protected]>
>
> Reject KVM_CAP_X86_DISABLE_EXITS if userspace attempts to disable MWAIT
> exits and KVM previously reported (via KVM_CHECK_EXTENSION) that MWAIT is
> not allowed in guest, e.g. because it's not supported or the CPU doesn't
> have an aways-running

nit: always-running?

> APIC timer.
>
> Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable MWAIT intercepts")
>
> Signed-off-by: Sean Christopherson <[email protected]>
> Signed-off-by: Kechen Lu <[email protected]>
> ---
> arch/x86/kvm/x86.c | 20 +++++++++++++-------
> 1 file changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 9a77b55142c6..60caa3fd40e5 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4326,6 +4326,16 @@ static inline bool kvm_can_mwait_in_guest(void)
> boot_cpu_has(X86_FEATURE_ARAT);
> }
>
> +static u64 kvm_get_allowed_disable_exits(void)
> +{
> + u64 r = KVM_X86_DISABLE_VALID_EXITS;
> +
> + if (!kvm_can_mwait_in_guest())
> + r &= ~KVM_X86_DISABLE_EXITS_MWAIT;
> +
> + return r;
> +}
> +
> static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
> struct kvm_cpuid2 __user *cpuid_arg)
> {
> @@ -4448,10 +4458,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
> r = KVM_CLOCK_VALID_FLAGS;
> break;
> case KVM_CAP_X86_DISABLE_EXITS:
> - r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
> - KVM_X86_DISABLE_EXITS_CSTATE;
> - if(kvm_can_mwait_in_guest())
> - r |= KVM_X86_DISABLE_EXITS_MWAIT;
> + r |= kvm_get_allowed_disable_exits();
> break;
> case KVM_CAP_X86_SMM:
> if (!IS_ENABLED(CONFIG_KVM_SMM))
> @@ -6224,15 +6231,14 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> break;
> case KVM_CAP_X86_DISABLE_EXITS:
> r = -EINVAL;
> - if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
> + if (cap->args[0] & ~kvm_get_allowed_disable_exits())
> break;
>
> mutex_lock(&kvm->lock);
> if (kvm->created_vcpus)
> goto disable_exits_unlock;
>
> - if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
> - kvm_can_mwait_in_guest())
> + if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT)
> kvm->arch.mwait_in_guest = true;
> if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
> kvm->arch.hlt_in_guest = true;
> --
> 2.34.1
>

2023-02-01 00:43:40

by Kechen Lu

[permalink] [raw]
Subject: RE: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT interception when not allowed

Hi Zhao,

> -----Original Message-----
> From: Zhao Liu <[email protected]>
> Sent: Tuesday, January 31, 2023 4:12 AM
> To: Kechen Lu <[email protected]>
> Cc: [email protected]; [email protected]; [email protected];
> [email protected]; [email protected]; [email protected];
> [email protected]; [email protected]
> Subject: Re: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT
> interception when not allowed
>
> External email: Use caution opening links or attachments
>
>
> On Sat, Jan 21, 2023 at 02:07:35AM +0000, Kechen Lu wrote:
> > Date: Sat, 21 Jan 2023 02:07:35 +0000
> > From: Kechen Lu <[email protected]>
> > Subject: [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT
> > interception when not allowed
> > X-Mailer: git-send-email 2.34.1
> >
> > From: Sean Christopherson <[email protected]>
> >
> > Reject KVM_CAP_X86_DISABLE_EXITS if userspace attempts to disable
> > MWAIT exits and KVM previously reported (via KVM_CHECK_EXTENSION)
> that
> > MWAIT is not allowed in guest, e.g. because it's not supported or the
> > CPU doesn't have an aways-running
>
> nit: always-running?

Ack. Thanks.

BR,
Kechen

>
> > APIC timer.
> >
> > Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable MWAIT
> > intercepts")
> >
> > Signed-off-by: Sean Christopherson <[email protected]>
> > Signed-off-by: Kechen Lu <[email protected]>
> > ---
> > arch/x86/kvm/x86.c | 20 +++++++++++++-------
> > 1 file changed, 13 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index
> > 9a77b55142c6..60caa3fd40e5 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -4326,6 +4326,16 @@ static inline bool
> kvm_can_mwait_in_guest(void)
> > boot_cpu_has(X86_FEATURE_ARAT); }
> >
> > +static u64 kvm_get_allowed_disable_exits(void)
> > +{
> > + u64 r = KVM_X86_DISABLE_VALID_EXITS;
> > +
> > + if (!kvm_can_mwait_in_guest())
> > + r &= ~KVM_X86_DISABLE_EXITS_MWAIT;
> > +
> > + return r;
> > +}
> > +
> > static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
> > struct kvm_cpuid2 __user
> > *cpuid_arg) { @@ -4448,10 +4458,7 @@ int
> > kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
> > r = KVM_CLOCK_VALID_FLAGS;
> > break;
> > case KVM_CAP_X86_DISABLE_EXITS:
> > - r |= KVM_X86_DISABLE_EXITS_HLT |
> KVM_X86_DISABLE_EXITS_PAUSE |
> > - KVM_X86_DISABLE_EXITS_CSTATE;
> > - if(kvm_can_mwait_in_guest())
> > - r |= KVM_X86_DISABLE_EXITS_MWAIT;
> > + r |= kvm_get_allowed_disable_exits();
> > break;
> > case KVM_CAP_X86_SMM:
> > if (!IS_ENABLED(CONFIG_KVM_SMM)) @@ -6224,15 +6231,14 @@
> > int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> > break;
> > case KVM_CAP_X86_DISABLE_EXITS:
> > r = -EINVAL;
> > - if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
> > + if (cap->args[0] & ~kvm_get_allowed_disable_exits())
> > break;
> >
> > mutex_lock(&kvm->lock);
> > if (kvm->created_vcpus)
> > goto disable_exits_unlock;
> >
> > - if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
> > - kvm_can_mwait_in_guest())
> > + if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT)
> > kvm->arch.mwait_in_guest = true;
> > if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
> > kvm->arch.hlt_in_guest = true;
> > --
> > 2.34.1
> >