From: Kechen Lu <kechenl@nvidia.com>
To: <kvm@vger.kernel.org>, <seanjc@google.com>, <pbonzini@redhat.com>
Cc: <zhi.wang.linux@gmail.com>, <chao.gao@intel.com>,
<shaoqin.huang@intel.com>, <vkuznets@redhat.com>,
<kechenl@nvidia.com>, <linux-kernel@vger.kernel.org>
Subject: [RFC PATCH v6 5/6] KVM: x86: add vCPU scoped toggling for disabled exits
Date: Sat, 21 Jan 2023 02:07:37 +0000 [thread overview]
Message-ID: <20230121020738.2973-6-kechenl@nvidia.com> (raw)
In-Reply-To: <20230121020738.2973-1-kechenl@nvidia.com>
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 enables
the vCPU-scoped exits control toggling, but keeps the VM-scoped
exits control behaviors restriction as before.
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.
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.
Suggested-by: Sean Christopherson <seanjc@google.com>
Suggested-by: Chao Gao <chao.gao@intel.com>
Signed-off-by: Kechen Lu <kechenl@nvidia.com>
---
Documentation/virt/kvm/api.rst | 2 +-
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 2 ++
arch/x86/kvm/svm/svm.c | 30 ++++++++++++++++++++++++
arch/x86/kvm/vmx/vmx.c | 37 ++++++++++++++++++++++++++++++
arch/x86/kvm/x86.c | 7 ++++++
6 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 3850202942d0..698f476d36dd 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -7102,7 +7102,7 @@ longer intercept some instructions for improved latency in some
workloads, and is suggested when vCPUs are associated to dedicated
physical CPUs. More bits can be added in the future; userspace can
just pass the KVM_CHECK_EXTENSION result to KVM_ENABLE_CAP to disable
-all such vmexits.
+all such vmexits. VM scoped and vCPU scoped capability are both supported.
By default, this capability only disables exits. To re-enable an exit, or to
override previous settings, userspace can set KVM_X86_DISABLE_EXITS_OVERRIDE,
diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index abccd51dcfca..534322c21168 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -131,6 +131,7 @@ KVM_X86_OP(msr_filter_changed)
KVM_X86_OP(complete_emulated_msr)
KVM_X86_OP(vcpu_deliver_sipi_vector)
KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP(update_disabled_exits)
#undef KVM_X86_OP
#undef KVM_X86_OP_OPTIONAL
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 41b998234a04..e21e5d452b5d 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1711,6 +1711,8 @@ struct kvm_x86_ops {
* Returns vCPU specific APICv inhibit reasons
*/
unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+ void (*update_disabled_exits)(struct kvm_vcpu *vcpu);
};
struct kvm_x86_nested_ops {
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index dc7176605e01..81c387dfa46c 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4680,6 +4680,33 @@ static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
sev_vcpu_deliver_sipi_vector(vcpu, vector);
}
+static void svm_update_disabled_exits(struct kvm_vcpu *vcpu)
+{
+ struct vcpu_svm *svm = to_svm(vcpu);
+ struct vmcb_control_area *control = &svm->vmcb->control;
+
+ if (kvm_hlt_in_guest(vcpu))
+ svm_clr_intercept(svm, INTERCEPT_HLT);
+ else
+ svm_set_intercept(svm, INTERCEPT_HLT);
+
+ if (kvm_mwait_in_guest(vcpu)) {
+ svm_clr_intercept(svm, INTERCEPT_MONITOR);
+ svm_clr_intercept(svm, INTERCEPT_MWAIT);
+ } else {
+ svm_set_intercept(svm, INTERCEPT_MONITOR);
+ svm_set_intercept(svm, INTERCEPT_MWAIT);
+ }
+
+ if (kvm_pause_in_guest(vcpu)) {
+ svm_clr_intercept(svm, INTERCEPT_PAUSE);
+ } else {
+ control->pause_filter_count = pause_filter_count;
+ if (pause_filter_thresh)
+ control->pause_filter_thresh = pause_filter_thresh;
+ }
+}
+
static void svm_vm_destroy(struct kvm *kvm)
{
avic_vm_destroy(kvm);
@@ -4825,7 +4852,10 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
.complete_emulated_msr = svm_complete_emulated_msr,
.vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
+
.vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons,
+
+ .update_disabled_exits = svm_update_disabled_exits,
};
/*
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 019a20029878..f5137afdd424 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -8070,6 +8070,41 @@ static void vmx_vm_destroy(struct kvm *kvm)
free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
}
+static void vmx_update_disabled_exits(struct kvm_vcpu *vcpu)
+{
+ struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+ if (kvm_hlt_in_guest(vcpu))
+ exec_controls_clearbit(vmx, CPU_BASED_HLT_EXITING);
+ else
+ exec_controls_setbit(vmx, CPU_BASED_HLT_EXITING);
+
+ if (kvm_mwait_in_guest(vcpu))
+ exec_controls_clearbit(vmx, CPU_BASED_MWAIT_EXITING |
+ CPU_BASED_MONITOR_EXITING);
+ else
+ exec_controls_setbit(vmx, CPU_BASED_MWAIT_EXITING |
+ CPU_BASED_MONITOR_EXITING);
+
+ if (!kvm_pause_in_guest(vcpu)) {
+ vmcs_write32(PLE_GAP, ple_gap);
+ vmx->ple_window = ple_window;
+ vmx->ple_window_dirty = true;
+ }
+
+ if (kvm_cstate_in_guest(vcpu)) {
+ vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
+ vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
+ vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
+ vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
+ } else {
+ vmx_enable_intercept_for_msr(vcpu, MSR_CORE_C1_RES, MSR_TYPE_R);
+ vmx_enable_intercept_for_msr(vcpu, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
+ vmx_enable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
+ vmx_enable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
+ }
+}
+
static struct kvm_x86_ops vmx_x86_ops __initdata = {
.name = "kvm_intel",
@@ -8207,6 +8242,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,
+
+ .update_disabled_exits = vmx_update_disabled_exits,
};
static unsigned int vmx_handle_intel_pt_intr(void)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3ea5f12536a0..8c15292c6886 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5552,6 +5552,13 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
if (vcpu->arch.pv_cpuid.enforce)
kvm_update_pv_runtime(vcpu);
+ return 0;
+ case KVM_CAP_X86_DISABLE_EXITS:
+ if (cap->args[0] & ~kvm_get_allowed_disable_exits())
+ return -EINVAL;
+
+ kvm_ioctl_disable_exits(vcpu->arch, cap->args[0]);
+ static_call(kvm_x86_update_disabled_exits)(vcpu);
return 0;
default:
return -EINVAL;
--
2.34.1
next prev parent reply other threads:[~2023-01-21 2:08 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-21 2:07 [RFC PATCH v6 0/6] KVM: x86: add per-vCPU exits disable capability Kechen Lu
2023-01-21 2:07 ` [RFC PATCH v6 1/6] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
2023-01-21 7:28 ` Greg KH
2023-01-22 1:48 ` Kechen Lu
2023-01-21 2:07 ` [RFC PATCH v6 2/6] KVM: x86: Move *_in_guest power management flags to vCPU scope Kechen Lu
2023-02-02 14:56 ` Zhi Wang
2023-02-02 19:42 ` Kechen Lu
2023-01-21 2:07 ` [RFC PATCH v6 3/6] KVM: x86: Reject disabling of MWAIT interception when not allowed Kechen Lu
2023-01-31 12:11 ` Zhao Liu
2023-02-01 0:43 ` Kechen Lu
2023-01-21 2:07 ` [RFC PATCH v6 4/6] KVM: x86: Let userspace re-enable previously disabled exits Kechen Lu
2023-01-30 6:19 ` Chao Gao
2023-01-30 20:25 ` Kechen Lu
2023-01-21 2:07 ` Kechen Lu [this message]
2023-01-30 6:42 ` [RFC PATCH v6 5/6] KVM: x86: add vCPU scoped toggling for " Chao Gao
2023-01-30 20:57 ` Kechen Lu
2023-01-31 2:23 ` Chao Gao
2023-01-21 2:07 ` [RFC PATCH v6 6/6] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS Kechen Lu
2023-02-02 15:08 ` Zhi Wang
2023-02-02 20:17 ` Kechen Lu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230121020738.2973-6-kechenl@nvidia.com \
--to=kechenl@nvidia.com \
--cc=chao.gao@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shaoqin.huang@intel.com \
--cc=vkuznets@redhat.com \
--cc=zhi.wang.linux@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox