All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability
@ 2022-06-15  1:16 Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
                   ` (6 more replies)
  0 siblings, 7 replies; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

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 everything on AMD machines.

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 (4):
  KVM: x86: Move *_in_guest power management flags to vCPU scope
  KVM: x86: add vCPU scoped toggling for disabled exits
  KVM: x86: Add a new guest_debug flag forcing exit to userspace
  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                        |  44 +++++-
 arch/x86/kvm/vmx/vmx.c                        |  54 ++++++-
 arch/x86/kvm/x86.c                            |  76 +++++++--
 arch/x86/kvm/x86.h                            |  16 +-
 include/uapi/linux/kvm.h                      |   5 +-
 tools/include/uapi/linux/kvm.h                |   1 +
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/x86_64/svm_util.h   |   1 +
 .../selftests/kvm/x86_64/disable_exits_test.c | 147 ++++++++++++++++++
 16 files changed, 330 insertions(+), 47 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/disable_exits_test.c

-- 
2.32.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-07-20 17:12   ` Sean Christopherson
  2022-06-15  1:16 ` [RFC PATCH v3 2/7] KVM: x86: Move *_in_guest power management flags to vCPU scope Kechen Lu
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

From: Sean Christopherson <seanjc@google.com>

Since VMX and SVM both would never update the control bits if exits
are disable after vCPUs are created, only allow setting exits
disable flag before vCPU creation.

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

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Kechen Lu <kechenl@nvidia.com>
---
 Documentation/virt/kvm/api.rst | 1 +
 arch/x86/kvm/x86.c             | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 11e00a46c610..d0d8749591a8 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6933,6 +6933,7 @@ branch to guests' 0x200 interrupt vector.
 :Architectures: x86
 :Parameters: args[0] defines which exits are disabled
 :Returns: 0 on success, -EINVAL when args[0] contains invalid exits
+          or if any vCPU has already been created
 
 Valid bits in args[0] are::
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 158b2e135efc..3ac6329e6d43 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6006,6 +6006,10 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (cap->args[0] & ~KVM_X86_DISABLE_VALID_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())
 			kvm->arch.mwait_in_guest = true;
@@ -6016,6 +6020,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
 			kvm->arch.cstate_in_guest = true;
 		r = 0;
+disable_exits_unlock:
+		mutex_unlock(&kvm->lock);
 		break;
 	case KVM_CAP_MSR_PLATFORM_INFO:
 		kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 2/7] KVM: x86: Move *_in_guest power management flags to vCPU scope
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed Kechen Lu
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

Make the runtime disabled mwait/hlt/pause/cstate exits flags vCPU scope
to allow finer-grained, per-vCPU control.  The VM-scoped control is only
allowed before vCPUs are created, thus preserving the existing behavior
is a simple matter of snapshotting the flags at vCPU creation.

Signed-off-by: Kechen Lu <kechenl@nvidia.com>
Suggested-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/kvm_host.h |  5 +++++
 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          | 12 ++++++------
 arch/x86/kvm/vmx/vmx.c          | 16 ++++++++--------
 arch/x86/kvm/x86.c              |  6 +++++-
 arch/x86/kvm/x86.h              | 16 ++++++++--------
 8 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 9217bd6cf0d1..573a39bf7a84 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -924,6 +924,11 @@ struct kvm_vcpu_arch {
 #if IS_ENABLED(CONFIG_HYPERV)
 	hpa_t hv_root_tdp;
 #endif
+
+	bool mwait_in_guest;
+	bool hlt_in_guest;
+	bool pause_in_guest;
+	bool cstate_in_guest;
 };
 
 struct kvm_lpage_info {
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index de6d44e07e34..f013ff4f49c5 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -245,8 +245,8 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e
 		best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
 
 	best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent);
-	if (kvm_hlt_in_guest(vcpu->kvm) && best &&
-		(best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
+	if (kvm_hlt_in_guest(vcpu) &&
+	    best && (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
 		best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
 
 	if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 0e68b4c937fc..9e29d658a8c2 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -147,14 +147,13 @@ static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
 static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
 {
 	return pi_inject_timer && kvm_vcpu_apicv_active(vcpu) &&
-		(kvm_mwait_in_guest(vcpu->kvm) || kvm_hlt_in_guest(vcpu->kvm));
+		(kvm_mwait_in_guest(vcpu) || kvm_hlt_in_guest(vcpu));
 }
 
 bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu)
 {
-	return kvm_x86_ops.set_hv_timer
-	       && !(kvm_mwait_in_guest(vcpu->kvm) ||
-		    kvm_can_post_timer_interrupt(vcpu));
+	return kvm_x86_ops.set_hv_timer &&
+		!(kvm_mwait_in_guest(vcpu) || kvm_can_post_timer_interrupt(vcpu));
 }
 EXPORT_SYMBOL_GPL(kvm_can_use_hv_timer);
 
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index ba7cd26f438f..f143ec757467 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -675,7 +675,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 
 	pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
 	pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
-	if (kvm_pause_in_guest(svm->vcpu.kvm)) {
+	if (kvm_pause_in_guest(&svm->vcpu)) {
 		/* use guest values since host doesn't intercept PAUSE */
 		vmcb02->control.pause_filter_count = pause_count12;
 		vmcb02->control.pause_filter_thresh = pause_thresh12;
@@ -951,7 +951,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 	vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
 	vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
 
-	if (!kvm_pause_in_guest(vcpu->kvm)) {
+	if (!kvm_pause_in_guest(vcpu)) {
 		vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
 		vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
 
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 87da90360bc7..b32987f54ace 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -921,7 +921,7 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	int old = control->pause_filter_count;
 
-	if (kvm_pause_in_guest(vcpu->kvm))
+	if (kvm_pause_in_guest(vcpu))
 		return;
 
 	control->pause_filter_count = __grow_ple_window(old,
@@ -942,7 +942,7 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu)
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	int old = control->pause_filter_count;
 
-	if (kvm_pause_in_guest(vcpu->kvm))
+	if (kvm_pause_in_guest(vcpu))
 		return;
 
 	control->pause_filter_count =
@@ -1136,12 +1136,12 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	svm_set_intercept(svm, INTERCEPT_RDPRU);
 	svm_set_intercept(svm, INTERCEPT_RSM);
 
-	if (!kvm_mwait_in_guest(vcpu->kvm)) {
+	if (!kvm_mwait_in_guest(vcpu)) {
 		svm_set_intercept(svm, INTERCEPT_MONITOR);
 		svm_set_intercept(svm, INTERCEPT_MWAIT);
 	}
 
-	if (!kvm_hlt_in_guest(vcpu->kvm))
+	if (!kvm_hlt_in_guest(vcpu))
 		svm_set_intercept(svm, INTERCEPT_HLT);
 
 	control->iopm_base_pa = __sme_set(iopm_base);
@@ -1185,7 +1185,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	svm->nested.vmcb12_gpa = INVALID_GPA;
 	svm->nested.last_vmcb12_gpa = INVALID_GPA;
 
-	if (!kvm_pause_in_guest(vcpu->kvm)) {
+	if (!kvm_pause_in_guest(vcpu)) {
 		control->pause_filter_count = pause_filter_count;
 		if (pause_filter_thresh)
 			control->pause_filter_thresh = pause_filter_thresh;
@@ -4269,7 +4269,7 @@ static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
 
 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
 {
-	if (!kvm_pause_in_guest(vcpu->kvm))
+	if (!kvm_pause_in_guest(vcpu))
 		shrink_ple_window(vcpu);
 }
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 553dd2317b9c..f24c9a357f70 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1597,7 +1597,7 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
 	 * then the instruction is already executing and RIP has already been
 	 * advanced.
 	 */
-	if (kvm_hlt_in_guest(vcpu->kvm) &&
+	if (kvm_hlt_in_guest(vcpu) &&
 			vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
 		vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
 }
@@ -4212,10 +4212,10 @@ static u32 vmx_exec_control(struct vcpu_vmx *vmx)
 		exec_control |= CPU_BASED_CR3_STORE_EXITING |
 				CPU_BASED_CR3_LOAD_EXITING  |
 				CPU_BASED_INVLPG_EXITING;
-	if (kvm_mwait_in_guest(vmx->vcpu.kvm))
+	if (kvm_mwait_in_guest(&vmx->vcpu))
 		exec_control &= ~(CPU_BASED_MWAIT_EXITING |
 				CPU_BASED_MONITOR_EXITING);
-	if (kvm_hlt_in_guest(vmx->vcpu.kvm))
+	if (kvm_hlt_in_guest(&vmx->vcpu))
 		exec_control &= ~CPU_BASED_HLT_EXITING;
 	return exec_control;
 }
@@ -4294,7 +4294,7 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 	}
 	if (!enable_unrestricted_guest)
 		exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
-	if (kvm_pause_in_guest(vmx->vcpu.kvm))
+	if (kvm_pause_in_guest(&vmx->vcpu))
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!kvm_vcpu_apicv_active(vcpu))
 		exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
@@ -4397,7 +4397,7 @@ 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(&vmx->vcpu)) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmx->ple_window = ple_window;
 		vmx->ple_window_dirty = true;
@@ -5562,7 +5562,7 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu)
  */
 static int handle_pause(struct kvm_vcpu *vcpu)
 {
-	if (!kvm_pause_in_guest(vcpu->kvm))
+	if (!kvm_pause_in_guest(vcpu))
 		grow_ple_window(vcpu);
 
 	/*
@@ -7059,7 +7059,7 @@ static int vmx_vcpu_create(struct kvm_vcpu *vcpu)
 	vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
 	vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
 	vmx_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
-	if (kvm_cstate_in_guest(vcpu->kvm)) {
+	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);
@@ -7597,7 +7597,7 @@ static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
 
 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
 {
-	if (!kvm_pause_in_guest(vcpu->kvm))
+	if (!kvm_pause_in_guest(vcpu))
 		shrink_ple_window(vcpu);
 }
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3ac6329e6d43..b419b258ed90 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11355,6 +11355,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 #if IS_ENABLED(CONFIG_HYPERV)
 	vcpu->arch.hv_root_tdp = INVALID_PAGE;
 #endif
+	vcpu->arch.mwait_in_guest = vcpu->kvm->arch.mwait_in_guest;
+	vcpu->arch.hlt_in_guest = vcpu->kvm->arch.hlt_in_guest;
+	vcpu->arch.pause_in_guest = vcpu->kvm->arch.pause_in_guest;
+	vcpu->arch.cstate_in_guest = vcpu->kvm->arch.cstate_in_guest;
 
 	r = static_call(kvm_x86_vcpu_create)(vcpu);
 	if (r)
@@ -12539,7 +12543,7 @@ bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
 		     vcpu->arch.exception.pending))
 		return false;
 
-	if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
+	if (kvm_hlt_in_guest(vcpu) && !kvm_can_deliver_async_pf(vcpu))
 		return false;
 
 	/*
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 588792f00334..a59b73e11726 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -324,24 +324,24 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
 	    __rem;						\
 	 })
 
-static inline bool kvm_mwait_in_guest(struct kvm *kvm)
+static inline bool kvm_mwait_in_guest(struct kvm_vcpu *vcpu)
 {
-	return kvm->arch.mwait_in_guest;
+	return vcpu->arch.mwait_in_guest;
 }
 
-static inline bool kvm_hlt_in_guest(struct kvm *kvm)
+static inline bool kvm_hlt_in_guest(struct kvm_vcpu *vcpu)
 {
-	return kvm->arch.hlt_in_guest;
+	return vcpu->arch.hlt_in_guest;
 }
 
-static inline bool kvm_pause_in_guest(struct kvm *kvm)
+static inline bool kvm_pause_in_guest(struct kvm_vcpu *vcpu)
 {
-	return kvm->arch.pause_in_guest;
+	return vcpu->arch.pause_in_guest;
 }
 
-static inline bool kvm_cstate_in_guest(struct kvm *kvm)
+static inline bool kvm_cstate_in_guest(struct kvm_vcpu *vcpu)
 {
-	return kvm->arch.cstate_in_guest;
+	return vcpu->arch.cstate_in_guest;
 }
 
 enum kvm_intr_type {
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 2/7] KVM: x86: Move *_in_guest power management flags to vCPU scope Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-07-20 17:13   ` Sean Christopherson
  2022-06-15  1:16 ` [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits Kechen Lu
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

From: Sean Christopherson <seanjc@google.com>

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 <seanjc@google.com>
---
 arch/x86/kvm/x86.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b419b258ed90..f31ebbb1b94f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4199,6 +4199,17 @@ 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_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
+		KVM_X86_DISABLE_EXITS_CSTATE;
+
+	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)
 {
@@ -4318,10 +4329,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:
 		/* SMBASE is usually relocated above 1M on modern chipsets,
@@ -6003,15 +6011,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.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
                   ` (2 preceding siblings ...)
  2022-06-15  1:16 ` [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-06-15  2:51   ` Chao Gao
  2022-06-15  1:16 ` [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for " Kechen Lu
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

From: Sean Christopherson <seanjc@google.com>

Add an OVERRIDE flag to KVM_CAP_X86_DISABLE_EXITS allow userspace to
re-enable exits and/or override previous settings.  There's no real use
case for the the per-VM ioctl, but a future per-vCPU variant wants to let
userspace toggle interception while the vCPU is running; add the OVERRIDE
functionality now to provide consistent between between the per-VM and
per-vCPU variants.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 Documentation/virt/kvm/api.rst |  5 +++++
 arch/x86/kvm/x86.c             | 39 +++++++++++++++++++++++-----------
 include/uapi/linux/kvm.h       |  4 +++-
 3 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index d0d8749591a8..89e13b6783b5 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6941,6 +6941,7 @@ Valid bits in args[0] are::
   #define KVM_X86_DISABLE_EXITS_HLT              (1 << 1)
   #define KVM_X86_DISABLE_EXITS_PAUSE            (1 << 2)
   #define KVM_X86_DISABLE_EXITS_CSTATE           (1 << 3)
+  #define KVM_X86_DISABLE_EXITS_OVERRIDE         (1ull << 63)
 
 Enabling this capability on a VM provides userspace with a way to no
 longer intercept some instructions for improved latency in some
@@ -6949,6 +6950,10 @@ 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.
 
+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,
+in which case KVM will enable/disable according to the mask (a '1' == disable).
+
 Do not enable KVM_FEATURE_PV_UNHALT if you disable HLT exits.
 
 7.14 KVM_CAP_S390_HPAGE_1M
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index f31ebbb1b94f..7cc8ac550bc7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4201,11 +4201,10 @@ static inline bool kvm_can_mwait_in_guest(void)
 
 static u64 kvm_get_allowed_disable_exits(void)
 {
-	u64 r = KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
-		KVM_X86_DISABLE_EXITS_CSTATE;
+	u64 r = KVM_X86_DISABLE_VALID_EXITS;
 
-	if(kvm_can_mwait_in_guest())
-		r |= KVM_X86_DISABLE_EXITS_MWAIT;
+	if (!kvm_can_mwait_in_guest())
+		r &= ~KVM_X86_DISABLE_EXITS_MWAIT;
 
 	return r;
 }
@@ -5264,6 +5263,28 @@ static int kvm_vcpu_ioctl_device_attr(struct kvm_vcpu *vcpu,
 	return r;
 }
 
+
+#define kvm_ioctl_disable_exits(a, mask)				     \
+({									     \
+	if (!kvm_can_mwait_in_guest())                                       \
+		(mask) &= KVM_X86_DISABLE_EXITS_MWAIT;                       \
+	if ((mask) & KVM_X86_DISABLE_EXITS_OVERRIDE) {			     \
+		(a).mwait_in_guest = (mask) & KVM_X86_DISABLE_EXITS_MWAIT;   \
+		(a).hlt_in_guest = (mask) & KVM_X86_DISABLE_EXITS_HLT;	     \
+		(a).pause_in_guest = (mask) & KVM_X86_DISABLE_EXITS_PAUSE;   \
+		(a).cstate_in_guest = (mask) & KVM_X86_DISABLE_EXITS_CSTATE; \
+	} else {							     \
+		if ((mask) & KVM_X86_DISABLE_EXITS_MWAIT)		     \
+			(a).mwait_in_guest = true;			     \
+		if ((mask) & KVM_X86_DISABLE_EXITS_HLT)			     \
+			(a).hlt_in_guest = true;			     \
+		if ((mask) & KVM_X86_DISABLE_EXITS_PAUSE)		     \
+			(a).pause_in_guest = true;			     \
+		if ((mask) & KVM_X86_DISABLE_EXITS_CSTATE)		     \
+			(a).cstate_in_guest = true;			     \
+	}								     \
+})
+
 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
 				     struct kvm_enable_cap *cap)
 {
@@ -6018,14 +6039,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (kvm->created_vcpus)
 			goto disable_exits_unlock;
 
-		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;
-		if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
-			kvm->arch.pause_in_guest = true;
-		if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
-			kvm->arch.cstate_in_guest = true;
+		kvm_ioctl_disable_exits(kvm->arch, cap->args[0]);
+
 		r = 0;
 disable_exits_unlock:
 		mutex_unlock(&kvm->lock);
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 5088bd9f1922..f2e76e436be5 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -814,10 +814,12 @@ struct kvm_ioeventfd {
 #define KVM_X86_DISABLE_EXITS_HLT            (1 << 1)
 #define KVM_X86_DISABLE_EXITS_PAUSE          (1 << 2)
 #define KVM_X86_DISABLE_EXITS_CSTATE         (1 << 3)
+#define KVM_X86_DISABLE_EXITS_OVERRIDE	     (1ull << 63)
 #define KVM_X86_DISABLE_VALID_EXITS          (KVM_X86_DISABLE_EXITS_MWAIT | \
                                               KVM_X86_DISABLE_EXITS_HLT | \
                                               KVM_X86_DISABLE_EXITS_PAUSE | \
-                                              KVM_X86_DISABLE_EXITS_CSTATE)
+                                              KVM_X86_DISABLE_EXITS_CSTATE | \
+					      KVM_X86_DISABLE_EXITS_OVERRIDE)
 
 /* for KVM_ENABLE_CAP */
 struct kvm_enable_cap {
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for disabled exits
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
                   ` (3 preceding siblings ...)
  2022-06-15  1:16 ` [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-06-15  2:43   ` Chao Gao
  2022-06-15  1:16 ` [RFC PATCH v3 6/7] KVM: x86: Add a new guest_debug flag forcing exit to userspace Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS Kechen Lu
  6 siblings, 1 reply; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

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 enabled
the vCPU-scoped exits control toggling, also align the VM-scoped
exits control behaviors.

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>
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                 | 20 ++++++++++++----
 6 files changed, 87 insertions(+), 5 deletions(-)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 89e13b6783b5..7f614b7d5ad8 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6948,7 +6948,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 da47f60a4650..c17d417cb3cf 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -128,6 +128,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 573a39bf7a84..1c9e6067c34f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1584,6 +1584,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 b32987f54ace..7b3d64b3b901 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4589,6 +4589,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);
@@ -4732,7 +4759,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 f24c9a357f70..2d000638cc9b 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7716,6 +7716,41 @@ static bool vmx_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason)
 	return supported & BIT(reason);
 }
 
+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",
 
@@ -7849,6 +7884,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 7cc8ac550bc7..8123309d097f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5331,6 +5331,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;
@@ -5980,6 +5987,8 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {
+	struct kvm_vcpu *vcpu;
+	unsigned long i;
 	int r;
 
 	if (cap->flags)
@@ -6036,14 +6045,17 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			break;
 
 		mutex_lock(&kvm->lock);
-		if (kvm->created_vcpus)
-			goto disable_exits_unlock;
+		if (kvm->created_vcpus) {
+			kvm_for_each_vcpu(i, vcpu, kvm) {
+				kvm_ioctl_disable_exits(vcpu->arch, cap->args[0]);
+				static_call(kvm_x86_update_disabled_exits)(vcpu);
+			}
+		}
+		mutex_unlock(&kvm->lock);
 
 		kvm_ioctl_disable_exits(kvm->arch, cap->args[0]);
 
 		r = 0;
-disable_exits_unlock:
-		mutex_unlock(&kvm->lock);
 		break;
 	case KVM_CAP_MSR_PLATFORM_INFO:
 		kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 6/7] KVM: x86: Add a new guest_debug flag forcing exit to userspace
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
                   ` (4 preceding siblings ...)
  2022-06-15  1:16 ` [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for " Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-06-15  1:16 ` [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS Kechen Lu
  6 siblings, 0 replies; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

For debug and test purposes, there are needs to explicitly make
instruction triggered exits could be trapped to userspace. Simply
add a new flag for guest_debug interface could achieve this.

This patch also fills the userspace accessible field
vcpu->run->hw.hardware_exit_reason for userspace to determine the
original triggered VM-exits.

Signed-off-by: Kechen Lu <kechenl@nvidia.com>
---
 arch/x86/kvm/svm/svm.c         | 2 ++
 arch/x86/kvm/vmx/vmx.c         | 1 +
 arch/x86/kvm/x86.c             | 2 ++
 include/uapi/linux/kvm.h       | 1 +
 tools/include/uapi/linux/kvm.h | 1 +
 5 files changed, 7 insertions(+)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 7b3d64b3b901..e7ced6c3fbea 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3259,6 +3259,8 @@ int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
 	if (!svm_check_exit_valid(exit_code))
 		return svm_handle_invalid_exit(vcpu, exit_code);
 
+	vcpu->run->hw.hardware_exit_reason = exit_code;
+
 #ifdef CONFIG_RETPOLINE
 	if (exit_code == SVM_EXIT_MSR)
 		return msr_interception(vcpu);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 2d000638cc9b..c32c20c4aa4d 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6151,6 +6151,7 @@ static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 
 	if (exit_reason.basic >= kvm_vmx_max_exit_handlers)
 		goto unexpected_vmexit;
+	vcpu->run->hw.hardware_exit_reason = exit_reason.basic;
 #ifdef CONFIG_RETPOLINE
 	if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
 		return kvm_emulate_wrmsr(vcpu);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 8123309d097f..c6124a7e2180 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8349,6 +8349,8 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
 	 */
 	if (unlikely(rflags & X86_EFLAGS_TF))
 		r = kvm_vcpu_do_singlestep(vcpu);
+	r &= !(vcpu->guest_debug & KVM_GUESTDBG_EXIT_USERSPACE);
+
 	return r;
 }
 EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index f2e76e436be5..23c335a6a285 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -777,6 +777,7 @@ struct kvm_s390_irq_state {
 
 #define KVM_GUESTDBG_ENABLE		0x00000001
 #define KVM_GUESTDBG_SINGLESTEP		0x00000002
+#define KVM_GUESTDBG_EXIT_USERSPACE	0x00000004
 
 struct kvm_guest_debug {
 	__u32 control;
diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h
index 6a184d260c7f..373b4a2b7fe9 100644
--- a/tools/include/uapi/linux/kvm.h
+++ b/tools/include/uapi/linux/kvm.h
@@ -773,6 +773,7 @@ struct kvm_s390_irq_state {
 
 #define KVM_GUESTDBG_ENABLE		0x00000001
 #define KVM_GUESTDBG_SINGLESTEP		0x00000002
+#define KVM_GUESTDBG_EXIT_USERSPACE	0x00000004
 
 struct kvm_guest_debug {
 	__u32 control;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS
  2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
                   ` (5 preceding siblings ...)
  2022-06-15  1:16 ` [RFC PATCH v3 6/7] KVM: x86: Add a new guest_debug flag forcing exit to userspace Kechen Lu
@ 2022-06-15  1:16 ` Kechen Lu
  2022-06-15  3:14   ` Chao Gao
  6 siblings, 1 reply; 19+ messages in thread
From: Kechen Lu @ 2022-06-15  1:16 UTC (permalink / raw)
  To: kvm, pbonzini, seanjc; +Cc: vkuznets, somduttar, kechenl, linux-kernel

Add tests for KVM cap KVM_CAP_X86_DISABLE_EXITS overriding flags
in VM and vCPU scope both works as expected.

Signed-off-by: Kechen Lu <kechenl@nvidia.com>
---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/x86_64/svm_util.h   |   1 +
 .../selftests/kvm/x86_64/disable_exits_test.c | 147 ++++++++++++++++++
 4 files changed, 150 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/disable_exits_test.c

diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
index 4509a3a7eeae..2b50170db9b2 100644
--- a/tools/testing/selftests/kvm/.gitignore
+++ b/tools/testing/selftests/kvm/.gitignore
@@ -15,6 +15,7 @@
 /x86_64/cpuid_test
 /x86_64/cr4_cpuid_sync_test
 /x86_64/debug_regs
+/x86_64/disable_exits_test
 /x86_64/evmcs_test
 /x86_64/emulator_error_test
 /x86_64/fix_hypercall_test
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 22423c871ed6..de11d1f95700 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -115,6 +115,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/disable_exits_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
diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
index a25aabd8f5e7..d8cad1cff578 100644
--- a/tools/testing/selftests/kvm/include/x86_64/svm_util.h
+++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
@@ -17,6 +17,7 @@
 #define CPUID_SVM		BIT_ULL(CPUID_SVM_BIT)
 
 #define SVM_EXIT_MSR		0x07c
+#define SVM_EXIT_HLT		0x078
 #define SVM_EXIT_VMMCALL	0x081
 
 struct svm_test_data {
diff --git a/tools/testing/selftests/kvm/x86_64/disable_exits_test.c b/tools/testing/selftests/kvm/x86_64/disable_exits_test.c
new file mode 100644
index 000000000000..8ca427357ed0
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/disable_exits_test.c
@@ -0,0 +1,147 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test per-VM and per-vCPU disable exits cap
+ *
+ */
+
+#define _GNU_SOURCE /* for program_invocation_short_name */
+#include <sys/ioctl.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "svm_util.h"
+#include "vmx.h"
+#include "processor.h"
+
+#define VCPU_ID_1 0
+#define VCPU_ID_2 1
+
+static void guest_code_exits(void) {
+	asm volatile("sti; hlt; cli");
+}
+
+/* Set debug control for trapped instruction exiting to userspace */
+static void vcpu_set_debug_exit_userspace(struct kvm_vm *vm, int vcpuid,
+		struct kvm_guest_debug *debug) {
+	memset(debug, 0, sizeof(*debug));
+	debug->control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_EXIT_USERSPACE;
+	vcpu_set_guest_debug(vm, VCPU_ID_1, debug);
+}
+
+static void test_vm_cap_disable_exits(void) {
+	struct kvm_enable_cap cap = {
+		.cap = KVM_CAP_X86_DISABLE_EXITS,
+		.args[0] = KVM_X86_DISABLE_EXITS_HLT|KVM_X86_DISABLE_EXITS_OVERRIDE,
+	};
+	struct kvm_guest_debug debug;
+	struct kvm_vm *vm;
+	struct kvm_run *run;
+
+	/* Create VM */
+	vm = vm_create_without_vcpus(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES);
+
+	/* Test Case #1
+	 * Default without disabling HLT exits in VM scope
+	 */
+	vm_vcpu_add_default(vm, VCPU_ID_1, (void *)guest_code_exits);
+	vcpu_set_debug_exit_userspace(vm, VCPU_ID_1, &debug);
+	run = vcpu_state(vm, VCPU_ID_1);
+	vcpu_run(vm, VCPU_ID_1);
+	/* Exit reason should be HLT */
+	if (is_amd_cpu())
+		TEST_ASSERT(run->hw.hardware_exit_reason == SVM_EXIT_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+	else
+		TEST_ASSERT(run->hw.hardware_exit_reason == EXIT_REASON_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+
+	/* Test Case #2
+	 * Disabling HLT exits in VM scope
+	 */
+	vm_vcpu_add_default(vm, VCPU_ID_2, (void *)guest_code_exits);
+	vcpu_set_debug_exit_userspace(vm, VCPU_ID_2, &debug);
+	run = vcpu_state(vm, VCPU_ID_2);
+	/* Set VM scoped cap arg
+	 * KVM_X86_DISABLE_EXITS_HLT|KVM_X86_DISABLE_EXITS_OVERRIDE
+	 * after vCPUs creation so requiring override flag
+	 */
+	TEST_ASSERT(!vm_enable_cap(vm, &cap), "Failed to set KVM_CAP_X86_DISABLE_EXITS");
+	vcpu_run(vm, VCPU_ID_2);
+	/* Exit reason should not be HLT, would finish the guest
+	 * running and exit (e.g. SVM_EXIT_SHUTDOWN)
+	 */
+	if (is_amd_cpu())
+		TEST_ASSERT(run->hw.hardware_exit_reason != SVM_EXIT_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+	else
+		TEST_ASSERT(run->hw.hardware_exit_reason != EXIT_REASON_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+
+	kvm_vm_free(vm);
+}
+
+static void test_vcpu_cap_disable_exits(void) {
+	struct kvm_enable_cap cap = {
+		.cap = KVM_CAP_X86_DISABLE_EXITS,
+		.args[0] = KVM_X86_DISABLE_EXITS_HLT|KVM_X86_DISABLE_EXITS_OVERRIDE,
+	};
+	struct kvm_guest_debug debug;
+	struct kvm_vm *vm;
+	struct kvm_run *run;
+
+	/* Create VM */
+	vm = vm_create_without_vcpus(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES);
+	vm_vcpu_add_default(vm, VCPU_ID_1, (void *)guest_code_exits);
+	vcpu_set_debug_exit_userspace(vm, VCPU_ID_1, &debug);
+	vm_vcpu_add_default(vm, VCPU_ID_2, (void *)guest_code_exits);
+	vcpu_set_debug_exit_userspace(vm, VCPU_ID_2, &debug);
+	/* Set vCPU 2 scoped cap arg
+	 * KVM_X86_DISABLE_EXITS_HLT|KVM_X86_DISABLE_EXITS_OVERRIDE
+	 */
+	TEST_ASSERT(!vcpu_enable_cap(vm, VCPU_ID_2, &cap), "Failed to set KVM_CAP_X86_DISABLE_EXITS");
+
+	/* Test Case #3
+	 * Default without disabling HLT exits in this vCPU 1
+	 */
+	run = vcpu_state(vm, VCPU_ID_1);
+	vcpu_run(vm, VCPU_ID_1);
+	/* Exit reason should be HLT */
+	if (is_amd_cpu())
+		TEST_ASSERT(run->hw.hardware_exit_reason == SVM_EXIT_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+	else
+		TEST_ASSERT(run->hw.hardware_exit_reason == EXIT_REASON_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+
+	/* Test Case #4
+	 * Disabling HLT exits in vCPU 2
+	 */
+	run = vcpu_state(vm, VCPU_ID_2);
+	vcpu_run(vm, VCPU_ID_2);
+	/* Exit reason should not be HLT, would finish the guest
+	 * running and exit (e.g. SVM_EXIT_SHUTDOWN)
+	 */
+	if (is_amd_cpu())
+		TEST_ASSERT(run->hw.hardware_exit_reason != SVM_EXIT_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+	else
+		TEST_ASSERT(run->hw.hardware_exit_reason != EXIT_REASON_HLT,
+			"Got exit_reason other than HLT: 0x%llx\n",
+			run->hw.hardware_exit_reason);
+
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+	test_vm_cap_disable_exits();
+	test_vcpu_cap_disable_exits();
+	return 0;
+}
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for disabled exits
  2022-06-15  1:16 ` [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for " Kechen Lu
@ 2022-06-15  2:43   ` Chao Gao
  2022-06-16  3:04     ` Kechen Lu
  2022-07-20 17:38     ` Sean Christopherson
  0 siblings, 2 replies; 19+ messages in thread
From: Chao Gao @ 2022-06-15  2:43 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, seanjc, vkuznets, somduttar, linux-kernel

>@@ -5980,6 +5987,8 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
> int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> 			    struct kvm_enable_cap *cap)
> {
>+	struct kvm_vcpu *vcpu;
>+	unsigned long i;
> 	int r;
> 
> 	if (cap->flags)
>@@ -6036,14 +6045,17 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> 			break;
> 
> 		mutex_lock(&kvm->lock);
>-		if (kvm->created_vcpus)
>-			goto disable_exits_unlock;
>+		if (kvm->created_vcpus) {
>+			kvm_for_each_vcpu(i, vcpu, kvm) {
>+				kvm_ioctl_disable_exits(vcpu->arch, cap->args[0]);
>+				static_call(kvm_x86_update_disabled_exits)(vcpu);

IMO, this won't work on Intel platforms. Because, to manipulate a vCPU's
VMCS, vcpu_load() should be invoked in advance to load the VMCS.
Alternatively, you can add a request KVM_REQ_XXX and defer updating VMCS
to the next vCPU entry.

>+			}
>+		}
>+		mutex_unlock(&kvm->lock);
> 
> 		kvm_ioctl_disable_exits(kvm->arch, cap->args[0]);
> 
> 		r = 0;
>-disable_exits_unlock:
>-		mutex_unlock(&kvm->lock);
> 		break;
> 	case KVM_CAP_MSR_PLATFORM_INFO:
> 		kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
>-- 
>2.32.0
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits
  2022-06-15  1:16 ` [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits Kechen Lu
@ 2022-06-15  2:51   ` Chao Gao
  2022-07-20 17:18     ` Sean Christopherson
  0 siblings, 1 reply; 19+ messages in thread
From: Chao Gao @ 2022-06-15  2:51 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, seanjc, vkuznets, somduttar, linux-kernel

On Tue, Jun 14, 2022 at 06:16:19PM -0700, Kechen Lu wrote:
>From: Sean Christopherson <seanjc@google.com>
>
>Add an OVERRIDE flag to KVM_CAP_X86_DISABLE_EXITS allow userspace to
>re-enable exits and/or override previous settings.  There's no real use
>case for the the per-VM ioctl, but a future per-vCPU variant wants to let
>userspace toggle interception while the vCPU is running; add the OVERRIDE
>functionality now to provide consistent between between the per-VM and
>per-vCPU variants.
>
>Signed-off-by: Sean Christopherson <seanjc@google.com>
>---
> Documentation/virt/kvm/api.rst |  5 +++++
> arch/x86/kvm/x86.c             | 39 +++++++++++++++++++++++-----------
> include/uapi/linux/kvm.h       |  4 +++-
> 3 files changed, 35 insertions(+), 13 deletions(-)
>
>diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
>index d0d8749591a8..89e13b6783b5 100644
>--- a/Documentation/virt/kvm/api.rst
>+++ b/Documentation/virt/kvm/api.rst
>@@ -6941,6 +6941,7 @@ Valid bits in args[0] are::
>   #define KVM_X86_DISABLE_EXITS_HLT              (1 << 1)
>   #define KVM_X86_DISABLE_EXITS_PAUSE            (1 << 2)
>   #define KVM_X86_DISABLE_EXITS_CSTATE           (1 << 3)
>+  #define KVM_X86_DISABLE_EXITS_OVERRIDE         (1ull << 63)
> 
> Enabling this capability on a VM provides userspace with a way to no
> longer intercept some instructions for improved latency in some
>@@ -6949,6 +6950,10 @@ 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.
> 
>+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,
>+in which case KVM will enable/disable according to the mask (a '1' == disable).
>+
> Do not enable KVM_FEATURE_PV_UNHALT if you disable HLT exits.
> 
> 7.14 KVM_CAP_S390_HPAGE_1M
>diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>index f31ebbb1b94f..7cc8ac550bc7 100644
>--- a/arch/x86/kvm/x86.c
>+++ b/arch/x86/kvm/x86.c
>@@ -4201,11 +4201,10 @@ static inline bool kvm_can_mwait_in_guest(void)
> 
> static u64 kvm_get_allowed_disable_exits(void)
> {
>-	u64 r = KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
>-		KVM_X86_DISABLE_EXITS_CSTATE;
>+	u64 r = KVM_X86_DISABLE_VALID_EXITS;
> 
>-	if(kvm_can_mwait_in_guest())
>-		r |= KVM_X86_DISABLE_EXITS_MWAIT;
>+	if (!kvm_can_mwait_in_guest())
>+		r &= ~KVM_X86_DISABLE_EXITS_MWAIT;

This hunk looks like a fix to patch 3; it can be squashed into that patch.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS
  2022-06-15  1:16 ` [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS Kechen Lu
@ 2022-06-15  3:14   ` Chao Gao
  2022-06-15 18:21     ` Kechen Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Chao Gao @ 2022-06-15  3:14 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, seanjc, vkuznets, somduttar, linux-kernel

>+/* Set debug control for trapped instruction exiting to userspace */
>+static void vcpu_set_debug_exit_userspace(struct kvm_vm *vm, int vcpuid,
>+		struct kvm_guest_debug *debug) {

The debug argument looks pointless. Probably you can remove it.

>+	memset(debug, 0, sizeof(*debug));
>+	debug->control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_EXIT_USERSPACE;
>+	vcpu_set_guest_debug(vm, VCPU_ID_1, debug);
>+}

^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS
  2022-06-15  3:14   ` Chao Gao
@ 2022-06-15 18:21     ` Kechen Lu
  0 siblings, 0 replies; 19+ messages in thread
From: Kechen Lu @ 2022-06-15 18:21 UTC (permalink / raw)
  To: Chao Gao; +Cc: kvm, pbonzini, seanjc, vkuznets, Somdutta Roy, linux-kernel

Hi Chao,

> -----Original Message-----
> From: Chao Gao <chao.gao@intel.com>
> Sent: Tuesday, June 14, 2022 8:14 PM
> To: Kechen Lu <kechenl@nvidia.com>
> Cc: kvm@vger.kernel.org; pbonzini@redhat.com; seanjc@google.com;
> vkuznets@redhat.com; Somdutta Roy <somduttar@nvidia.com>; linux-
> kernel@vger.kernel.org
> Subject: Re: [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU
> cap KVM_CAP_X86_DISABLE_EXITS
> 
> External email: Use caution opening links or attachments
> 
> 
> >+/* Set debug control for trapped instruction exiting to userspace */
> >+static void vcpu_set_debug_exit_userspace(struct kvm_vm *vm, int
> vcpuid,
> >+              struct kvm_guest_debug *debug) {
> 
> The debug argument looks pointless. Probably you can remove it.

Makes sense. Thanks for pointing out. Will remove it.

BR,
Kechen

> 
> >+      memset(debug, 0, sizeof(*debug));
> >+      debug->control = KVM_GUESTDBG_ENABLE |
> KVM_GUESTDBG_EXIT_USERSPACE;
> >+      vcpu_set_guest_debug(vm, VCPU_ID_1, debug); }

^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for disabled exits
  2022-06-15  2:43   ` Chao Gao
@ 2022-06-16  3:04     ` Kechen Lu
  2022-07-20 17:38     ` Sean Christopherson
  1 sibling, 0 replies; 19+ messages in thread
From: Kechen Lu @ 2022-06-16  3:04 UTC (permalink / raw)
  To: Chao Gao; +Cc: kvm, pbonzini, seanjc, vkuznets, Somdutta Roy, linux-kernel



> -----Original Message-----
> From: Chao Gao <chao.gao@intel.com>
> Sent: Tuesday, June 14, 2022 7:43 PM
> To: Kechen Lu <kechenl@nvidia.com>
> Cc: kvm@vger.kernel.org; pbonzini@redhat.com; seanjc@google.com;
> vkuznets@redhat.com; Somdutta Roy <somduttar@nvidia.com>; linux-
> kernel@vger.kernel.org
> Subject: Re: [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for
> disabled exits
> 
> External email: Use caution opening links or attachments
> 
> 
> >@@ -5980,6 +5987,8 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct
> >kvm_irq_level *irq_event,  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> >                           struct kvm_enable_cap *cap)  {
> >+      struct kvm_vcpu *vcpu;
> >+      unsigned long i;
> >       int r;
> >
> >       if (cap->flags)
> >@@ -6036,14 +6045,17 @@ int kvm_vm_ioctl_enable_cap(struct kvm
> *kvm,
> >                       break;
> >
> >               mutex_lock(&kvm->lock);
> >-              if (kvm->created_vcpus)
> >-                      goto disable_exits_unlock;
> >+              if (kvm->created_vcpus) {
> >+                      kvm_for_each_vcpu(i, vcpu, kvm) {
> >+                              kvm_ioctl_disable_exits(vcpu->arch, cap->args[0]);
> >+
> >+ static_call(kvm_x86_update_disabled_exits)(vcpu);
> 
> IMO, this won't work on Intel platforms. Because, to manipulate a vCPU's
> VMCS, vcpu_load() should be invoked in advance to load the VMCS.
> Alternatively, you can add a request KVM_REQ_XXX and defer updating
> VMCS to the next vCPU entry.
> 

I see. Then adding a KVM request for VM-scoped exits toggling case on vmcs bits updating makes sense. 
Thanks for the suggestion.

BR,
Kechen
> >+                      }
> >+              }
> >+              mutex_unlock(&kvm->lock);
> >
> >               kvm_ioctl_disable_exits(kvm->arch, cap->args[0]);
> >
> >               r = 0;
> >-disable_exits_unlock:
> >-              mutex_unlock(&kvm->lock);
> >               break;
> >       case KVM_CAP_MSR_PLATFORM_INFO:
> >               kvm->arch.guest_can_read_msr_platform_info =
> >cap->args[0];
> >--
> >2.32.0
> >

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created
  2022-06-15  1:16 ` [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
@ 2022-07-20 17:12   ` Sean Christopherson
  2022-07-20 17:49     ` Sean Christopherson
  0 siblings, 1 reply; 19+ messages in thread
From: Sean Christopherson @ 2022-07-20 17:12 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, vkuznets, somduttar, linux-kernel

On Tue, Jun 14, 2022, Kechen Lu wrote:
> From: Sean Christopherson <seanjc@google.com>
> 
> Since VMX and SVM both would never update the control bits if exits
> are disable after vCPUs are created, only allow setting exits
> disable flag before vCPU creation.
> 
> Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable MWAIT
> intercepts")

Don't wrap the Fixes: line (ignore any complaints from checkpatch).

> Signed-off-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Kechen Lu <kechenl@nvidia.com>
> ---

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed
  2022-06-15  1:16 ` [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed Kechen Lu
@ 2022-07-20 17:13   ` Sean Christopherson
  0 siblings, 0 replies; 19+ messages in thread
From: Sean Christopherson @ 2022-07-20 17:13 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, vkuznets, somduttar, linux-kernel

On Tue, Jun 14, 2022, Kechen Lu wrote:
> From: Sean Christopherson <seanjc@google.com>
> 
> 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 <seanjc@google.com>
> ---
>  arch/x86/kvm/x86.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index b419b258ed90..f31ebbb1b94f 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4199,6 +4199,17 @@ 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_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
> +		KVM_X86_DISABLE_EXITS_CSTATE;
> +
> +	if(kvm_can_mwait_in_guest())

Probably my fault for not fixing this during copy+paste, but add a space after the if, i.e.

	if (kvm_can_mwait_in_guest())

> +		r |= KVM_X86_DISABLE_EXITS_MWAIT;
> +
> +	return r;
> +}

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits
  2022-06-15  2:51   ` Chao Gao
@ 2022-07-20 17:18     ` Sean Christopherson
  0 siblings, 0 replies; 19+ messages in thread
From: Sean Christopherson @ 2022-07-20 17:18 UTC (permalink / raw)
  To: Chao Gao; +Cc: Kechen Lu, kvm, pbonzini, vkuznets, somduttar, linux-kernel

On Wed, Jun 15, 2022, Chao Gao wrote:
> On Tue, Jun 14, 2022 at 06:16:19PM -0700, Kechen Lu wrote:
> > 7.14 KVM_CAP_S390_HPAGE_1M
> >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> >index f31ebbb1b94f..7cc8ac550bc7 100644
> >--- a/arch/x86/kvm/x86.c
> >+++ b/arch/x86/kvm/x86.c
> >@@ -4201,11 +4201,10 @@ static inline bool kvm_can_mwait_in_guest(void)
> > 
> > static u64 kvm_get_allowed_disable_exits(void)
> > {
> >-	u64 r = KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
> >-		KVM_X86_DISABLE_EXITS_CSTATE;
> >+	u64 r = KVM_X86_DISABLE_VALID_EXITS;
> > 
> >-	if(kvm_can_mwait_in_guest())
> >-		r |= KVM_X86_DISABLE_EXITS_MWAIT;
> >+	if (!kvm_can_mwait_in_guest())
> >+		r &= ~KVM_X86_DISABLE_EXITS_MWAIT;
> 
> This hunk looks like a fix to patch 3; it can be squashed into that patch.

It's not a fix, just an inversion of the logic to make it easier to maintain
going forward.  I intentionally made the change in patch 4 so that adding the
kvm_get_allowed_disable_exits() is a more "pure" movement of code from the "check"
path to a common helper.

I agree it's kinda odd, but I still think splitting the changes is desirable.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for disabled exits
  2022-06-15  2:43   ` Chao Gao
  2022-06-16  3:04     ` Kechen Lu
@ 2022-07-20 17:38     ` Sean Christopherson
  1 sibling, 0 replies; 19+ messages in thread
From: Sean Christopherson @ 2022-07-20 17:38 UTC (permalink / raw)
  To: Chao Gao; +Cc: Kechen Lu, kvm, pbonzini, vkuznets, somduttar, linux-kernel

On Wed, Jun 15, 2022, Chao Gao wrote:
> >@@ -5980,6 +5987,8 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
> > int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> > 			    struct kvm_enable_cap *cap)
> > {
> >+	struct kvm_vcpu *vcpu;
> >+	unsigned long i;
> > 	int r;
> > 
> > 	if (cap->flags)
> >@@ -6036,14 +6045,17 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
> > 			break;
> > 
> > 		mutex_lock(&kvm->lock);
> >-		if (kvm->created_vcpus)
> >-			goto disable_exits_unlock;
> >+		if (kvm->created_vcpus) {
> >+			kvm_for_each_vcpu(i, vcpu, kvm) {
> >+				kvm_ioctl_disable_exits(vcpu->arch, cap->args[0]);
> >+				static_call(kvm_x86_update_disabled_exits)(vcpu);
> 
> IMO, this won't work on Intel platforms.

It's not safe on AMD either because at best the behavior is non-deterministic if
the vCPU is already running in the guest, and at worst could cause explosions,
e.g. if hardware doesn't like software modifying in-use VMCB state.

> Because, to manipulate a vCPU's VMCS, vcpu_load() should be invoked in
> advance to load the VMCS.  Alternatively, you can add a request KVM_REQ_XXX
> and defer updating VMCS to the next vCPU entry.

Definitely use a request, doing vcpu_load() from a KVM-scoped ioctl() would be
a mess as KVM would need to acquire the per-vCPU lock for every vCPU.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created
  2022-07-20 17:12   ` Sean Christopherson
@ 2022-07-20 17:49     ` Sean Christopherson
  2022-07-20 18:38       ` Kechen Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Sean Christopherson @ 2022-07-20 17:49 UTC (permalink / raw)
  To: Kechen Lu; +Cc: kvm, pbonzini, vkuznets, somduttar, linux-kernel

On Wed, Jul 20, 2022, Sean Christopherson wrote:
> On Tue, Jun 14, 2022, Kechen Lu wrote:
> > From: Sean Christopherson <seanjc@google.com>
> > 
> > Since VMX and SVM both would never update the control bits if exits
> > are disable after vCPUs are created, only allow setting exits
> > disable flag before vCPU creation.
> > 
> > Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable MWAIT
> > intercepts")
> 
> Don't wrap the Fixes: line (ignore any complaints from checkpatch).

Sorry, I didn't see that you had sent v4 already and replied to some v3 patches.
This one still holds true for v4 (very minor nit though).

^ permalink raw reply	[flat|nested] 19+ messages in thread

* RE: [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created
  2022-07-20 17:49     ` Sean Christopherson
@ 2022-07-20 18:38       ` Kechen Lu
  0 siblings, 0 replies; 19+ messages in thread
From: Kechen Lu @ 2022-07-20 18:38 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: kvm, pbonzini, vkuznets, Somdutta Roy, linux-kernel



> -----Original Message-----
> From: Sean Christopherson <seanjc@google.com>
> Sent: Wednesday, July 20, 2022 10:50 AM
> To: Kechen Lu <kechenl@nvidia.com>
> Cc: kvm@vger.kernel.org; pbonzini@redhat.com; vkuznets@redhat.com;
> Somdutta Roy <somduttar@nvidia.com>; linux-kernel@vger.kernel.org
> Subject: Re: [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before
> vCPUs created
> 
> External email: Use caution opening links or attachments
> 
> 
> On Wed, Jul 20, 2022, Sean Christopherson wrote:
> > On Tue, Jun 14, 2022, Kechen Lu wrote:
> > > From: Sean Christopherson <seanjc@google.com>
> > >
> > > Since VMX and SVM both would never update the control bits if exits
> > > are disable after vCPUs are created, only allow setting exits
> > > disable flag before vCPU creation.
> > >
> > > Fixes: 4d5422cea3b6 ("KVM: X86: Provide a capability to disable
> > > MWAIT
> > > intercepts")
> >
> > Don't wrap the Fixes: line (ignore any complaints from checkpatch).
> 
> Sorry, I didn't see that you had sent v4 already and replied to some v3
> patches.
> This one still holds true for v4 (very minor nit though).

Sure:)

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-07-20 18:49 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15  1:16 [RFC PATCH v3 0/7] KVM: x86: add per-vCPU exits disable capability Kechen Lu
2022-06-15  1:16 ` [RFC PATCH v3 1/7] KVM: x86: only allow exits disable before vCPUs created Kechen Lu
2022-07-20 17:12   ` Sean Christopherson
2022-07-20 17:49     ` Sean Christopherson
2022-07-20 18:38       ` Kechen Lu
2022-06-15  1:16 ` [RFC PATCH v3 2/7] KVM: x86: Move *_in_guest power management flags to vCPU scope Kechen Lu
2022-06-15  1:16 ` [RFC PATCH v3 3/7] KVM: x86: Reject disabling of MWAIT interception when not allowed Kechen Lu
2022-07-20 17:13   ` Sean Christopherson
2022-06-15  1:16 ` [RFC PATCH v3 4/7] KVM: x86: Let userspace re-enable previously disabled exits Kechen Lu
2022-06-15  2:51   ` Chao Gao
2022-07-20 17:18     ` Sean Christopherson
2022-06-15  1:16 ` [RFC PATCH v3 5/7] KVM: x86: add vCPU scoped toggling for " Kechen Lu
2022-06-15  2:43   ` Chao Gao
2022-06-16  3:04     ` Kechen Lu
2022-07-20 17:38     ` Sean Christopherson
2022-06-15  1:16 ` [RFC PATCH v3 6/7] KVM: x86: Add a new guest_debug flag forcing exit to userspace Kechen Lu
2022-06-15  1:16 ` [RFC PATCH v3 7/7] KVM: selftests: Add tests for VM and vCPU cap KVM_CAP_X86_DISABLE_EXITS Kechen Lu
2022-06-15  3:14   ` Chao Gao
2022-06-15 18:21     ` Kechen Lu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.