kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support
@ 2019-06-11  7:34 Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state Wanpeng Li
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

After exposing some host CPU power management capabilities to dedicated 
instances, there is a requirement to consult current idle power-state 
residency statistics by turbostat.

This patchset adds virtual C-states residency msrs emulation. Allowing 
guest reads CORE cstate when exposing host CPU power management capabilities 
to the guest. PKG cstate is restricted currently to avoid a guest to get 
the whole package information in multi-tenant scenario.

v1 -> v2:
 * add residency msrs emulation (base on Paolo's design)

Wanpeng Li (5):
  KVM: X86: Dynamic allocate core residency msr state
  KVM: X86: Introduce residency msrs read/write operations
  KVM: X86: setup residency msrs during vCPU creation
  KVM: VMX: Add get/set residency msrs logic
  KVM: X86: Save/restore residency values when sched_out/sched_in

 arch/arm/include/asm/kvm_host.h     |   1 +
 arch/arm64/include/asm/kvm_host.h   |   1 +
 arch/mips/include/asm/kvm_host.h    |   1 +
 arch/powerpc/include/asm/kvm_host.h |   1 +
 arch/s390/include/asm/kvm_host.h    |   1 +
 arch/x86/include/asm/kvm_host.h     |  11 ++++
 arch/x86/kvm/vmx/vmx.c              |  15 ++++++
 arch/x86/kvm/x86.c                  | 104 ++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.h                  |   5 ++
 include/linux/kvm_host.h            |   1 +
 virt/kvm/kvm_main.c                 |   1 +
 11 files changed, 142 insertions(+)

-- 
2.7.4


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

* [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state
  2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
@ 2019-06-11  7:34 ` Wanpeng Li
  2019-06-12 16:01   ` Sean Christopherson
  2019-06-11  7:34 ` [PATCH v2 2/5] KVM: X86: Introduce residency msrs read/write operations Wanpeng Li
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

Dynamic allocate core residency msr state. MSR_CORE_C1_RES is unreadable 
except for ATOM platform, so it is ignore here.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/include/asm/kvm_host.h | 11 +++++++++++
 arch/x86/kvm/vmx/vmx.c          |  5 +++++
 2 files changed, 16 insertions(+)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 15e973d..bd615ee 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -538,6 +538,15 @@ struct kvm_vcpu_hv {
 	cpumask_t tlb_flush;
 };
 
+#define NR_CORE_RESIDENCY_MSRS 3
+
+struct kvm_residency_msr {
+	s64 value;
+	u32 index;
+	bool delta_from_host;
+	bool count_with_host;
+};
+
 struct kvm_vcpu_arch {
 	/*
 	 * rip and regs accesses must go through
@@ -785,6 +794,8 @@ struct kvm_vcpu_arch {
 
 	/* AMD MSRC001_0015 Hardware Configuration */
 	u64 msr_hwcr;
+
+	struct kvm_residency_msr *core_cstate_msrs;
 };
 
 struct kvm_lpage_info {
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 0b241f4..4dc2459 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6658,6 +6658,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 			goto free_vmcs;
 	}
 
+	vmx->vcpu.arch.core_cstate_msrs = kzalloc(sizeof(struct kvm_residency_msr) *
+		NR_CORE_RESIDENCY_MSRS, GFP_KERNEL_ACCOUNT);
+	if (!vmx->vcpu.arch.core_cstate_msrs)
+		goto free_vmcs;
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
 					   vmx_capability.ept,
-- 
2.7.4


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

* [PATCH v2 2/5] KVM: X86: Introduce residency msrs read/write operations
  2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state Wanpeng Li
@ 2019-06-11  7:34 ` Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 3/5] KVM: X86: setup residency msrs during vCPU creation Wanpeng Li
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

Both live migration and vCPU migrates between pCPUs possibly to set the MSRs 
in the host to change the delta between the host and guest values. This patch 
introduces msrs read/write operations in the host.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/x86.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index cc97aae..841a794 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1331,6 +1331,51 @@ void kvm_enable_efer_bits(u64 mask)
 }
 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
 
+u64 kvm_residency_read_host(struct kvm_vcpu *vcpu,
+		struct kvm_residency_msr *msr)
+{
+	u64 host_value;
+	rdmsrl_safe(msr->index, &host_value);
+	return kvm_scale_tsc(vcpu, host_value);
+}
+
+struct kvm_residency_msr *find_residency_msr_index(struct kvm_vcpu *vcpu,
+		u32 msr_index)
+{
+	int i;
+	struct kvm_residency_msr *msr;
+
+	for (i = 0; i < NR_CORE_RESIDENCY_MSRS; i++)
+		if (vcpu->arch.core_cstate_msrs[i].index == msr_index) {
+			msr = &vcpu->arch.core_cstate_msrs[i];
+			return msr;
+		}
+
+	return NULL;
+}
+
+u64 kvm_residency_read(struct kvm_vcpu *vcpu, u32 msr_index)
+{
+	struct kvm_residency_msr *msr = find_residency_msr_index(vcpu, msr_index);
+
+	if (msr)
+		return msr->value +
+			(msr->delta_from_host ? kvm_residency_read_host(vcpu, msr) : 0);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_residency_read);
+
+void kvm_residency_write(struct kvm_vcpu *vcpu,
+		u32 msr_index, u64 value)
+{
+	struct kvm_residency_msr *msr = find_residency_msr_index(vcpu, msr_index);
+
+	if (msr)
+		msr->value = value -
+			(msr->delta_from_host ? kvm_residency_read_host(vcpu, msr) : 0);
+}
+EXPORT_SYMBOL_GPL(kvm_residency_write);
+
 /*
  * Writes msr value into into the appropriate "register".
  * Returns 0 on success, non-0 otherwise.
-- 
2.7.4


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

* [PATCH v2 3/5] KVM: X86: setup residency msrs during vCPU creation
  2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 2/5] KVM: X86: Introduce residency msrs read/write operations Wanpeng Li
@ 2019-06-11  7:34 ` Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 4/5] KVM: VMX: Add get/set residency msrs logic Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 5/5] KVM: X86: Save/restore residency values when vCPU migrations Wanpeng Li
  4 siblings, 0 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

To setup core residency msrs during vCPU creation. Allowing guest reads 
CORE cstate when exposing host CPU power management capabilities to the 
guest. PKG cstate is restricted currently to avoid a guest to get the 
whole package information in multi-tenant scenario.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/vmx/vmx.c |  2 ++
 arch/x86/kvm/x86.c     | 21 +++++++++++++++++++++
 arch/x86/kvm/x86.h     |  5 +++++
 3 files changed, 28 insertions(+)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 4dc2459..2ebaa90 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6663,6 +6663,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (!vmx->vcpu.arch.core_cstate_msrs)
 		goto free_vmcs;
 
+	kvm_core_residency_setup(&vmx->vcpu);
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
 					   vmx_capability.ept,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 841a794..36905cd 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1376,6 +1376,16 @@ void kvm_residency_write(struct kvm_vcpu *vcpu,
 }
 EXPORT_SYMBOL_GPL(kvm_residency_write);
 
+static void kvm_residency_setup(struct kvm_vcpu *vcpu, struct kvm_residency_msr *msr,
+		u16 index, bool count_with_host)
+{
+	/* Preserve value on calls after the first */
+	u64 value = msr->index ? kvm_residency_read(vcpu, msr->index) : 0;
+	msr->delta_from_host = msr->count_with_host = count_with_host;
+	msr->index = index;
+	kvm_residency_write(vcpu, msr->index, value);
+}
+
 /*
  * Writes msr value into into the appropriate "register".
  * Returns 0 on success, non-0 otherwise.
@@ -3311,6 +3321,17 @@ static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
 	return kvm_arch_has_noncoherent_dma(vcpu->kvm);
 }
 
+void kvm_core_residency_setup(struct kvm_vcpu *vcpu)
+{
+	kvm_residency_setup(vcpu, &vcpu->arch.core_cstate_msrs[0],
+		MSR_CORE_C3_RESIDENCY, kvm_mwait_in_guest(vcpu->kvm));
+	kvm_residency_setup(vcpu, &vcpu->arch.core_cstate_msrs[1],
+		MSR_CORE_C6_RESIDENCY, kvm_mwait_in_guest(vcpu->kvm));
+	kvm_residency_setup(vcpu, &vcpu->arch.core_cstate_msrs[2],
+		MSR_CORE_C7_RESIDENCY, kvm_mwait_in_guest(vcpu->kvm));
+}
+EXPORT_SYMBOL_GPL(kvm_core_residency_setup);
+
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	/* Address WBINVD may be executed by guest */
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index dc61dbd..123fc8d 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -284,6 +284,11 @@ bool kvm_vector_hashing_enabled(void);
 int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
 			    int emulation_type, void *insn, int insn_len);
 
+u64 kvm_residency_read(struct kvm_vcpu *vcpu, u32 msr_index);
+void kvm_residency_write(struct kvm_vcpu *vcpu,
+				u32 msr_index, u64 value);
+void kvm_core_residency_setup(struct kvm_vcpu *vcpu);
+
 #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
 				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
 				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
-- 
2.7.4


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

* [PATCH v2 4/5] KVM: VMX: Add get/set residency msrs logic
  2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
                   ` (2 preceding siblings ...)
  2019-06-11  7:34 ` [PATCH v2 3/5] KVM: X86: setup residency msrs during vCPU creation Wanpeng Li
@ 2019-06-11  7:34 ` Wanpeng Li
  2019-06-11  7:34 ` [PATCH v2 5/5] KVM: X86: Save/restore residency values when vCPU migrations Wanpeng Li
  4 siblings, 0 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

Add logic to get/set the residency msrs. Then current idle
power-state residency statistics can be consult by guest, and 
be save/restore during live migration.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/vmx/vmx.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 2ebaa90..852f51e 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1715,6 +1715,9 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_IA32_FEATURE_CONTROL:
 		msr_info->data = vmx->msr_ia32_feature_control;
 		break;
+	case MSR_CORE_C3_RESIDENCY ... MSR_CORE_C7_RESIDENCY:
+		msr_info->data = kvm_residency_read(vcpu, msr_info->index);
+		break;
 	case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
 		if (!nested_vmx_allowed(vcpu))
 			return 1;
@@ -1928,6 +1931,11 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (msr_info->host_initiated && data == 0)
 			vmx_leave_nested(vcpu);
 		break;
+	case MSR_CORE_C3_RESIDENCY ... MSR_CORE_C7_RESIDENCY:
+		if (!msr_info->host_initiated)
+			return 1;
+		kvm_residency_write(vcpu, msr_info->index, data);
+		break;
 	case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
 		if (!msr_info->host_initiated)
 			return 1; /* they are read-only */
-- 
2.7.4


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

* [PATCH v2 5/5] KVM: X86: Save/restore residency values when vCPU migrations
  2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
                   ` (3 preceding siblings ...)
  2019-06-11  7:34 ` [PATCH v2 4/5] KVM: VMX: Add get/set residency msrs logic Wanpeng Li
@ 2019-06-11  7:34 ` Wanpeng Li
  4 siblings, 0 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-11  7:34 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

To save/restore residency values when vCPU migrates between mulitple 
pCPUs.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/arm/include/asm/kvm_host.h     |  1 +
 arch/arm64/include/asm/kvm_host.h   |  1 +
 arch/mips/include/asm/kvm_host.h    |  1 +
 arch/powerpc/include/asm/kvm_host.h |  1 +
 arch/s390/include/asm/kvm_host.h    |  1 +
 arch/x86/kvm/x86.c                  | 38 +++++++++++++++++++++++++++++++++++++
 include/linux/kvm_host.h            |  1 +
 virt/kvm/kvm_main.c                 |  1 +
 8 files changed, 45 insertions(+)

diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h
index 075e192..5e6a487 100644
--- a/arch/arm/include/asm/kvm_host.h
+++ b/arch/arm/include/asm/kvm_host.h
@@ -346,6 +346,7 @@ static inline void kvm_arch_hardware_unsetup(void) {}
 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_sched_out(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
 
 static inline void kvm_arm_init_debug(void) {}
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 4bcd9c1..12fec7d 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -557,6 +557,7 @@ void kvm_arm_vcpu_ptrauth_trap(struct kvm_vcpu *vcpu);
 static inline void kvm_arch_hardware_unsetup(void) {}
 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_sched_out(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
 
 void kvm_arm_init_debug(void);
diff --git a/arch/mips/include/asm/kvm_host.h b/arch/mips/include/asm/kvm_host.h
index 41204a4..217bbfd 100644
--- a/arch/mips/include/asm/kvm_host.h
+++ b/arch/mips/include/asm/kvm_host.h
@@ -1136,6 +1136,7 @@ static inline void kvm_arch_free_memslot(struct kvm *kvm,
 		struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {}
 static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_sched_out(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
index d10df67..4f5306d 100644
--- a/arch/powerpc/include/asm/kvm_host.h
+++ b/arch/powerpc/include/asm/kvm_host.h
@@ -854,6 +854,7 @@ static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
 static inline void kvm_arch_flush_shadow_all(struct kvm *kvm) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_sched_out(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_exit(void) {}
 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h
index da5825a..8710298 100644
--- a/arch/s390/include/asm/kvm_host.h
+++ b/arch/s390/include/asm/kvm_host.h
@@ -908,6 +908,7 @@ static inline void kvm_arch_hardware_disable(void) {}
 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_sched_out(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_free_memslot(struct kvm *kvm,
 		struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {}
 static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 36905cd..de91cc5 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3332,6 +3332,36 @@ void kvm_core_residency_setup(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_core_residency_setup);
 
+static void kvm_residency_sched_out(struct kvm_vcpu *vcpu)
+{
+	int i;
+	struct kvm_residency_msr *msr;
+
+	for (i = 0; i < NR_CORE_RESIDENCY_MSRS; i++) {
+		msr = &vcpu->arch.core_cstate_msrs[i];
+		if (msr->count_with_host) {
+			WARN_ON(!msr->delta_from_host);
+			msr->value += kvm_residency_read_host(vcpu, msr);
+			msr->delta_from_host = false;
+		}
+	}
+}
+
+static void kvm_residency_sched_in(struct kvm_vcpu *vcpu)
+{
+	int i;
+	struct kvm_residency_msr *msr;
+
+	for (i = 0; i < NR_CORE_RESIDENCY_MSRS; i++) {
+		msr = &vcpu->arch.core_cstate_msrs[i];
+		if (msr->count_with_host) {
+			WARN_ON(msr->delta_from_host);
+			msr->value -= kvm_residency_read_host(vcpu, msr);
+			msr->delta_from_host = true;
+		}
+	}
+}
+
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	/* Address WBINVD may be executed by guest */
@@ -9276,6 +9306,14 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
 {
 	vcpu->arch.l1tf_flush_l1d = true;
 	kvm_x86_ops->sched_in(vcpu, cpu);
+	if (kvm_mwait_in_guest(vcpu->kvm))
+		kvm_residency_sched_in(vcpu);
+}
+
+void kvm_arch_sched_out(struct kvm_vcpu *vcpu)
+{
+	if (kvm_mwait_in_guest(vcpu->kvm))
+		kvm_residency_sched_out(vcpu);
 }
 
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index abafddb..288bd34 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -854,6 +854,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
 
 void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu);
+void kvm_arch_sched_out(struct kvm_vcpu *vcpu);
 
 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9613987..7d504c0 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4221,6 +4221,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
 
 	if (current->state == TASK_RUNNING)
 		vcpu->preempted = true;
+	kvm_arch_sched_out(vcpu);
 	kvm_arch_vcpu_put(vcpu);
 }
 
-- 
2.7.4


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

* Re: [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state
  2019-06-11  7:34 ` [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state Wanpeng Li
@ 2019-06-12 16:01   ` Sean Christopherson
  2019-06-13  0:43     ` Wanpeng Li
  0 siblings, 1 reply; 8+ messages in thread
From: Sean Christopherson @ 2019-06-12 16:01 UTC (permalink / raw)
  To: Wanpeng Li; +Cc: linux-kernel, kvm, Paolo Bonzini, Radim Krčmář

On Tue, Jun 11, 2019 at 03:34:07PM +0800, Wanpeng Li wrote:
> From: Wanpeng Li <wanpengli@tencent.com>
> 
> Dynamic allocate core residency msr state. MSR_CORE_C1_RES is unreadable 
> except for ATOM platform, so it is ignore here.
> 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> ---
>  arch/x86/include/asm/kvm_host.h | 11 +++++++++++
>  arch/x86/kvm/vmx/vmx.c          |  5 +++++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 15e973d..bd615ee 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -538,6 +538,15 @@ struct kvm_vcpu_hv {
>  	cpumask_t tlb_flush;
>  };
>  
> +#define NR_CORE_RESIDENCY_MSRS 3
> +
> +struct kvm_residency_msr {
> +	s64 value;
> +	u32 index;
> +	bool delta_from_host;
> +	bool count_with_host;
> +};
> +
>  struct kvm_vcpu_arch {
>  	/*
>  	 * rip and regs accesses must go through
> @@ -785,6 +794,8 @@ struct kvm_vcpu_arch {
>  
>  	/* AMD MSRC001_0015 Hardware Configuration */
>  	u64 msr_hwcr;
> +
> +	struct kvm_residency_msr *core_cstate_msrs;

Why are these in kvm_vcpu_arch?  AFAICT they're only wired up for VMX.

>  };
>  
>  struct kvm_lpage_info {
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 0b241f4..4dc2459 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -6658,6 +6658,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
>  			goto free_vmcs;
>  	}
>  
> +	vmx->vcpu.arch.core_cstate_msrs = kzalloc(sizeof(struct kvm_residency_msr) *
> +		NR_CORE_RESIDENCY_MSRS, GFP_KERNEL_ACCOUNT);
> +	if (!vmx->vcpu.arch.core_cstate_msrs)
> +		goto free_vmcs;
> +
>  	if (nested)
>  		nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
>  					   vmx_capability.ept,
> -- 
> 2.7.4
> 

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

* Re: [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state
  2019-06-12 16:01   ` Sean Christopherson
@ 2019-06-13  0:43     ` Wanpeng Li
  0 siblings, 0 replies; 8+ messages in thread
From: Wanpeng Li @ 2019-06-13  0:43 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: LKML, kvm, Paolo Bonzini, Radim Krčmář

On Thu, 13 Jun 2019 at 00:01, Sean Christopherson
<sean.j.christopherson@intel.com> wrote:
>
> On Tue, Jun 11, 2019 at 03:34:07PM +0800, Wanpeng Li wrote:
> > From: Wanpeng Li <wanpengli@tencent.com>
> >
> > Dynamic allocate core residency msr state. MSR_CORE_C1_RES is unreadable
> > except for ATOM platform, so it is ignore here.
> >
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Radim Krčmář <rkrcmar@redhat.com>
> > Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> > ---
> >  arch/x86/include/asm/kvm_host.h | 11 +++++++++++
> >  arch/x86/kvm/vmx/vmx.c          |  5 +++++
> >  2 files changed, 16 insertions(+)
> >
> > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> > index 15e973d..bd615ee 100644
> > --- a/arch/x86/include/asm/kvm_host.h
> > +++ b/arch/x86/include/asm/kvm_host.h
> > @@ -538,6 +538,15 @@ struct kvm_vcpu_hv {
> >       cpumask_t tlb_flush;
> >  };
> >
> > +#define NR_CORE_RESIDENCY_MSRS 3
> > +
> > +struct kvm_residency_msr {
> > +     s64 value;
> > +     u32 index;
> > +     bool delta_from_host;
> > +     bool count_with_host;
> > +};
> > +
> >  struct kvm_vcpu_arch {
> >       /*
> >        * rip and regs accesses must go through
> > @@ -785,6 +794,8 @@ struct kvm_vcpu_arch {
> >
> >       /* AMD MSRC001_0015 Hardware Configuration */
> >       u64 msr_hwcr;
> > +
> > +     struct kvm_residency_msr *core_cstate_msrs;
>
> Why are these in kvm_vcpu_arch?  AFAICT they're only wired up for VMX.

They can be used by SVM later though I'm too busy to do that.

Regards,
Wanpeng Li

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

end of thread, other threads:[~2019-06-13 16:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-11  7:34 [PATCH v2 0/5] KVM: X86: Add virtual C-states residency msrs support Wanpeng Li
2019-06-11  7:34 ` [PATCH v2 1/5] KVM: X86: Dynamic allocate core residency msr state Wanpeng Li
2019-06-12 16:01   ` Sean Christopherson
2019-06-13  0:43     ` Wanpeng Li
2019-06-11  7:34 ` [PATCH v2 2/5] KVM: X86: Introduce residency msrs read/write operations Wanpeng Li
2019-06-11  7:34 ` [PATCH v2 3/5] KVM: X86: setup residency msrs during vCPU creation Wanpeng Li
2019-06-11  7:34 ` [PATCH v2 4/5] KVM: VMX: Add get/set residency msrs logic Wanpeng Li
2019-06-11  7:34 ` [PATCH v2 5/5] KVM: X86: Save/restore residency values when vCPU migrations Wanpeng Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).