All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] kvm: x86: export vCPU halted state to sysfs
@ 2018-02-01 17:54 Luiz Capitulino
  2018-02-01 20:15 ` Radim Krčmář
  2018-02-02 12:49 ` Daniel P. Berrangé
  0 siblings, 2 replies; 50+ messages in thread
From: Luiz Capitulino @ 2018-02-01 17:54 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini, rkrcmar, berrange, ehabkost


Libvirt needs to know when a vCPU is halted. To get this information,
libvirt has started using the query-cpus command from QEMU. However,
if in kernel irqchip is in use, query-cpus will force all vCPUs
to user-space since they have to issue the KVM_GET_MP_STATE ioctl.
This has catastrophic implications to low-latency workloads like
KVM-RT and zero packet loss with DPDK. To make matters worse, there's
an OpenStack service called ceilometer that causes libvirt to
issue query-cpus every few minutes.

The solution proposed in this patch is to export the vCPU
halted state in the already existing vcpu directory in sysfs.
This way, libvirt can read the vCPU halted state from sysfs and avoid
using the query-cpus command. This solution seems to be sufficient
for libvirt needs, but it has the following cons:

 * vcpu information in sysfs lives in a debug directory, so
   libvirt would be basing its API on debug info

 * Currently, only x86 supports the vcpu dir in sysfs, so
   we'd have to expand this to other archs (should be doable)

If we agree that this solution is feasible, I'll work on extending
the vcpu debug information to other archs for my next posting.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 arch/x86/kvm/debugfs.c   | 15 +++++++++++++++
 arch/x86/kvm/x86.c       |  2 ++
 include/linux/kvm_host.h | 11 +++++++++++
 3 files changed, 28 insertions(+)

diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c
index c19c7ede9bd6..056dd1c787bc 100644
--- a/arch/x86/kvm/debugfs.c
+++ b/arch/x86/kvm/debugfs.c
@@ -15,6 +15,15 @@ bool kvm_arch_has_vcpu_debugfs(void)
 	return true;
 }
 
+static int vcpu_get_halted(void *data, u64 *val)
+{
+	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
+	*val = vcpu->halted;
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vcpu_halted_fops, vcpu_get_halted, NULL, "%lld\n");
+
 static int vcpu_get_tsc_offset(void *data, u64 *val)
 {
 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
@@ -51,6 +60,12 @@ int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
 	if (!ret)
 		return -ENOMEM;
 
+	ret = debugfs_create_file("halted", 0444,
+				    vcpu->debugfs_dentry,
+				    vcpu, &vcpu_halted_fops);
+	if (!ret)
+		return -ENOMEM;
+
 	if (kvm_has_tsc_control) {
 		ret = debugfs_create_file("tsc-scaling-ratio", 0444,
 							vcpu->debugfs_dentry,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c13cd14c4780..9841841d186b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6273,6 +6273,7 @@ void kvm_arch_exit(void)
 
 int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
 {
+	kvm_vcpu_set_halted(vcpu);
 	++vcpu->stat.halt_exits;
 	if (lapic_in_kernel(vcpu)) {
 		vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
@@ -7204,6 +7205,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 
 	for (;;) {
 		if (kvm_vcpu_running(vcpu)) {
+			kvm_vcpu_set_running(vcpu);
 			r = vcpu_enter_guest(vcpu);
 		} else {
 			r = vcpu_block(kvm, vcpu);
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index ac0062b74aed..430a4d06b0fb 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -272,10 +272,21 @@ struct kvm_vcpu {
 	} spin_loop;
 #endif
 	bool preempted;
+	bool halted;
 	struct kvm_vcpu_arch arch;
 	struct dentry *debugfs_dentry;
 };
 
+static inline void kvm_vcpu_set_running(struct kvm_vcpu *vcpu)
+{
+    vcpu->halted = 0;
+}
+
+static inline void kvm_vcpu_set_halted(struct kvm_vcpu *vcpu)
+{
+    vcpu->halted = 1;
+}
+
 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
 {
 	/*
-- 
2.13.6

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

end of thread, other threads:[~2018-02-06 14:05 UTC | newest]

Thread overview: 50+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-01 17:54 [RFC] kvm: x86: export vCPU halted state to sysfs Luiz Capitulino
2018-02-01 20:15 ` Radim Krčmář
2018-02-01 20:26   ` Eduardo Habkost
2018-02-02 13:53     ` Viktor Mihajlovski
2018-02-02 14:14       ` Luiz Capitulino
2018-02-02 14:15       ` Eduardo Habkost
2018-02-02 14:19         ` Daniel P. Berrangé
2018-02-02 14:21           ` Luiz Capitulino
2018-02-02 14:50             ` Eduardo Habkost
2018-02-02 14:50               ` [Qemu-devel] " Eduardo Habkost
2018-02-02 14:55               ` [libvirt] " Luiz Capitulino
2018-02-02 14:55                 ` [Qemu-devel] " Luiz Capitulino
2018-02-02 15:07               ` Daniel P. Berrangé
2018-02-02 15:07                 ` [Qemu-devel] " Daniel P. Berrangé
2018-02-02 15:25                 ` Eduardo Habkost
2018-02-02 15:25                   ` [Qemu-devel] " Eduardo Habkost
2018-02-02 16:23                   ` [libvirt] " Eric Blake
2018-02-02 16:23                     ` [Qemu-devel] " Eric Blake
2018-02-02 15:19               ` [libvirt] " Eric Blake
2018-02-02 15:19                 ` [Qemu-devel] " Eric Blake
2018-02-02 17:23               ` Dr. David Alan Gilbert
2018-02-02 17:38                 ` Eduardo Habkost
2018-02-02 15:08         ` Viktor Mihajlovski
2018-02-02 15:22           ` [libvirt] " Luiz Capitulino
2018-02-02 15:51             ` Viktor Mihajlovski
2018-02-02 15:54               ` Daniel P. Berrangé
2018-02-02 16:01                 ` Luiz Capitulino
2018-02-02 16:07                   ` Luiz Capitulino
2018-02-02 16:19                   ` Viktor Mihajlovski
2018-02-02 17:42                     ` [libvirt] " Eduardo Habkost
2018-02-02 18:50                       ` Luiz Capitulino
2018-02-02 20:09                         ` Eduardo Habkost
2018-02-02 20:19                           ` [libvirt] " Luiz Capitulino
2018-02-02 20:41                             ` Eduardo Habkost
2018-02-02 21:49                               ` Luiz Capitulino
2018-02-02 21:54                                 ` Luiz Capitulino
2018-02-05 13:43                               ` Viktor Mihajlovski
2018-02-05 13:47                                 ` Daniel P. Berrangé
2018-02-05 15:37                                   ` Luiz Capitulino
2018-02-05 16:10                                     ` Viktor Mihajlovski
2018-02-05 16:36                                       ` Luiz Capitulino
2018-02-05 22:50                                     ` Eduardo Habkost
2018-02-06  2:04                                       ` Luiz Capitulino
2018-02-02 15:55               ` [libvirt] " Luiz Capitulino
2018-02-06 10:29     ` Viktor Mihajlovski
2018-02-06 14:05       ` Luiz Capitulino
2018-02-02 12:47   ` Daniel P. Berrangé
2018-02-02 13:46     ` Luiz Capitulino
2018-02-02 12:49 ` Daniel P. Berrangé
2018-02-02 13:49   ` Luiz Capitulino

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.