Skip to contentSkip to conversation

KVM: Factor out kvm_vcpu_kick to arch-generic code

PATCH Applied Christoffer Dall 5 participants 9 messages Feb 10, 2012 – Mar 13, 2012

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 KVM: Factor out kvm_vcpu_kick to arch-generic code

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---
 arch/ia64/include/asm/kvm_host.h    |    1 +
 arch/ia64/kvm/kvm-ia64.c            |   20 +++++---------------
 arch/powerpc/include/asm/kvm_host.h |    6 ++++++
 arch/powerpc/kvm/powerpc.c          |   21 ++++++---------------
 arch/s390/kvm/kvm-s390.c            |    8 ++++++++
 arch/x86/kvm/x86.c                  |   16 ++--------------
 include/linux/kvm_host.h            |    9 +++++++++
 virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++
 8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+	u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-	int me;
-	int cpu = vcpu->cpu;
-
-	if (waitqueue_active(&vcpu->wq))
-		wake_up_interruptible(&vcpu->wq);
-
-	me = get_cpu();
-	if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-		if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-			smp_send_reschedule(cpu);
-	put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
 	return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		(kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +6 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR	0x0040
 #define KVM_MMIO_REG_FQPR	0x0060
 
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.wqp;
+}
+
 #endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/kvm/powerpc.c +6 -15
index 0e21d15..b82e206 100644
@@ -43,6 +43,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 	       v->requests;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	return 1;
+}
+
 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
 {
 	int nr = kvmppc_get_gpr(vcpu, 11);
@@ -578,21 +583,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	return r;
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-	int me;
-	int cpu = vcpu->cpu;
-
-	me = get_cpu();
-	if (waitqueue_active(vcpu->arch.wqp)) {
-		wake_up_interruptible(vcpu->arch.wqp);
-		vcpu->stat.halt_wakeup++;
-	} else if (cpu != me && cpu != -1) {
-		smp_send_reschedule(vcpu->cpu);
-	}
-	put_cpu();
-}
-
 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 {
 	if (irq->irq = KVM_INTERRUPT_UNSET) {
@@ -601,6 +591,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 	}
 
 	kvmppc_core_queue_external(vcpu, irq);
+
 	kvm_vcpu_kick(vcpu);
 
 	return 0;
arch/s390/kvm/kvm-s390.c +8 -0
index 0b91679..aa907b7 100644
@@ -422,6 +422,14 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	/* kvm common code refers to this, but never calls it */
+	BUG();
+	return 0;
+}
+
+
 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
 {
 	kvm_s390_vcpu_initial_reset(vcpu);
arch/x86/kvm/x86.c +2 -14
index 2bd77a3..244bc9a 100644
@@ -6207,21 +6207,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		 kvm_cpu_has_interrupt(vcpu));
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
-	int me;
-	int cpu = vcpu->cpu;
-
-	if (waitqueue_active(&vcpu->wq)) {
-		wake_up_interruptible(&vcpu->wq);
-		++vcpu->stat.halt_wakeup;
-	}
-
-	me = get_cpu();
-	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
-		if (kvm_vcpu_exiting_guest_mode(vcpu) = IN_GUEST_MODE)
-			smp_send_reschedule(cpu);
-	put_cpu();
+	return kvm_vcpu_exiting_guest_mode(vcpu) = IN_GUEST_MODE;
 }
 
 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
include/linux/kvm_host.h +9 -0
index 9698080..f72e5f9 100644
@@ -438,6 +438,7 @@ void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
 			     gfn_t gfn);
 
 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
 void kvm_resched(struct kvm_vcpu *vcpu);
 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
@@ -506,6 +507,7 @@ int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 void kvm_free_physmem(struct kvm *kvm);
 
@@ -521,6 +523,13 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
 }
 #endif
 
+#ifndef __KVM_HAVE_ARCH_VCPU_GET_WQ
+static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->wq;
+}
+#endif
+
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
 void kvm_arch_destroy_vm(struct kvm *kvm);
 void kvm_free_all_assigned_devices(struct kvm *kvm);
virt/kvm/kvm_main.c +22 -0
index 470e305..b531928 100644
@@ -1562,6 +1562,28 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
 	finish_wait(&vcpu->wq, &wait);
 }
 
+/*
+ * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
+ */
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+{
+	int me;
+	int cpu = vcpu->cpu;
+	wait_queue_head_t *wqp;
+
+	wqp = kvm_arch_vcpu_wq(vcpu);
+	if (waitqueue_active(wqp)) {
+		wake_up_interruptible(wqp);
+		++vcpu->stat.halt_wakeup;
+	}
+
+	me = get_cpu();
+	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
+		if (kvm_arch_vcpu_should_kick(vcpu))
+			smp_send_reschedule(cpu);
+	put_cpu();
+}
+
 void kvm_resched(struct kvm_vcpu *vcpu)
 {
 	if (!need_resched())
#1 lore raw
Quoted 141 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---

arch/ia64/include/asm/kvm_host.h    |    1 +
arch/ia64/kvm/kvm-ia64.c            |   20 +++++---------------
arch/powerpc/include/asm/kvm_host.h |    6 ++++++
arch/powerpc/kvm/powerpc.c          |   21 ++++++---------------
arch/s390/kvm/kvm-s390.c            |    8 ++++++++
arch/x86/kvm/x86.c                  |   16 ++--------------
include/linux/kvm_host.h            |    9 +++++++++
virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++
8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+	u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-	int me;
-	int cpu = vcpu->cpu;
-
-	if (waitqueue_active(&vcpu->wq))
-		wake_up_interruptible(&vcpu->wq);
-
-	me = get_cpu();
-	if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-		if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-			smp_send_reschedule(cpu);
-	put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
 	return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		(kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +6 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR	0x0040
 #define KVM_MMIO_REG_FQPR	0x0060
 
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.wqp;
+}
+
 #endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/kvm/powerpc.c +5 -15
index 0e21d15..b82e206 100644
@@ -43,6 +43,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 	       v->requests;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	return 1;
+}
+
 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
 {
 	int nr = kvmppc_get_gpr(vcpu, 11);
@@ -578,21 +583,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	return r;
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-	int me;
-	int cpu = vcpu->cpu;
-
-	me = get_cpu();
-	if (waitqueue_active(vcpu->arch.wqp)) {
-		wake_up_interruptible(vcpu->arch.wqp);
-		vcpu->stat.halt_wakeup++;
-	} else if (cpu != me && cpu != -1) {
-		smp_send_reschedule(vcpu->cpu);
-	}
-	put_cpu();
-}
-
 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 {
 	if (irq->irq = KVM_INTERRUPT_UNSET) {

arch/powerpc/kvm/powerpc.c int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) · line 594

Quoted 4 lines
arch/powerpc/kvm/powerpc.c +1 -0
@@ -601,6 +591,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 	}
 
 	kvmppc_core_queue_external(vcpu, irq);
+

Apart from this little glitch the patch looks fine to me. But I'd like to have Scott and Paul ack it too.

Alex

Quoted 122 lines
arch/powerpc/kvm/powerpc.c +0 -0
 	kvm_vcpu_kick(vcpu);
 
 	return 0;
arch/s390/kvm/kvm-s390.c +8 -0
index 0b91679..aa907b7 100644
@@ -422,6 +422,14 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+	/* kvm common code refers to this, but never calls it */
+	BUG();
+	return 0;
+}
+
+
 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
 {
 	kvm_s390_vcpu_initial_reset(vcpu);
arch/x86/kvm/x86.c +2 -14
index 2bd77a3..244bc9a 100644
@@ -6207,21 +6207,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 		 kvm_cpu_has_interrupt(vcpu));
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
-	int me;
-	int cpu = vcpu->cpu;
-
-	if (waitqueue_active(&vcpu->wq)) {
-		wake_up_interruptible(&vcpu->wq);
-		++vcpu->stat.halt_wakeup;
-	}
-
-	me = get_cpu();
-	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
-		if (kvm_vcpu_exiting_guest_mode(vcpu) = IN_GUEST_MODE)
-			smp_send_reschedule(cpu);
-	put_cpu();
+	return kvm_vcpu_exiting_guest_mode(vcpu) = IN_GUEST_MODE;
 }
 
 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
include/linux/kvm_host.h +9 -0
index 9698080..f72e5f9 100644
@@ -438,6 +438,7 @@ void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
 			     gfn_t gfn);
 
 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
 void kvm_resched(struct kvm_vcpu *vcpu);
 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
@@ -506,6 +507,7 @@ int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 void kvm_free_physmem(struct kvm *kvm);
 
@@ -521,6 +523,13 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
 }
 #endif
 
+#ifndef __KVM_HAVE_ARCH_VCPU_GET_WQ
+static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->wq;
+}
+#endif
+
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
 void kvm_arch_destroy_vm(struct kvm *kvm);
 void kvm_free_all_assigned_devices(struct kvm *kvm);
virt/kvm/kvm_main.c +22 -1
index 470e305..b531928 100644
@@ -1562,6 +1562,28 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
 	finish_wait(&vcpu->wq, &wait);
 }
 
+/*
+ * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
+ */
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+{
+	int me;
+	int cpu = vcpu->cpu;
+	wait_queue_head_t *wqp;
+
+	wqp = kvm_arch_vcpu_wq(vcpu);
+	if (waitqueue_active(wqp)) {
+		wake_up_interruptible(wqp);
+		++vcpu->stat.halt_wakeup;
+	}
+
+	me = get_cpu();
+	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
+		if (kvm_arch_vcpu_should_kick(vcpu))
+			smp_send_reschedule(cpu);
+	put_cpu();
+}
+
 void kvm_resched(struct kvm_vcpu *vcpu)
 {
 	if (!need_resched())
 
--

To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 9 lines

arch/powerpc/kvm/powerpc.c int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) · line 594

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -0
@@ -601,6 +591,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
       }
 
       kvmppc_core_queue_external(vcpu, irq);
+

Apart from this little glitch the patch looks fine to me. But I'd like to have Scott and Paul ack it too.

damn, can't believe that slipped its way in. too many revisions.

thanks

#3 lore raw
In reply to Alexander Graf

Any news on the status of this?

Quoted 277 lines · Alexander Graf
Quoted 141 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---
arch/ia64/include/asm/kvm_host.h    |    1 + arch/ia64/kvm/kvm-ia64.c            |   20 +++++--------------- arch/powerpc/include/asm/kvm_host.h |    6 ++++++ arch/powerpc/kvm/powerpc.c          |   21 ++++++--------------- arch/s390/kvm/kvm-s390.c            |    8 ++++++++ arch/x86/kvm/x86.c                  |   16 ++-------------- include/linux/kvm_host.h            |    9 +++++++++ virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++

8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+     u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-     int me;
-     int cpu = vcpu->cpu;
-
-     if (waitqueue_active(&vcpu->wq))
-             wake_up_interruptible(&vcpu->wq);
-
-     me = get_cpu();
-     if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-             if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-                     smp_send_reschedule(cpu);
-     put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
       return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
               (kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
                                   struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +6 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR      0x0040
 #define KVM_MMIO_REG_FQPR     0x0060
 
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+     return vcpu->arch.wqp;
+}
+
 #endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/kvm/powerpc.c +5 -15
index 0e21d15..b82e206 100644
@@ -43,6 +43,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
              v->requests;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     return 1;
+}
+
 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
 {
       int nr = kvmppc_get_gpr(vcpu, 11);
@@ -578,21 +583,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
       return r;
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-     int me;
-     int cpu = vcpu->cpu;
-
-     me = get_cpu();
-     if (waitqueue_active(vcpu->arch.wqp)) {
-             wake_up_interruptible(vcpu->arch.wqp);
-             vcpu->stat.halt_wakeup++;
-     } else if (cpu != me && cpu != -1) {
-             smp_send_reschedule(vcpu->cpu);
-     }
-     put_cpu();
-}
-
 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 {
       if (irq->irq == KVM_INTERRUPT_UNSET) {

arch/powerpc/kvm/powerpc.c int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) · line 594

Quoted 4 lines
arch/powerpc/kvm/powerpc.c +1 -0
@@ -601,6 +591,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
       }
 
       kvmppc_core_queue_external(vcpu, irq);
+

Apart from this little glitch the patch looks fine to me. But I'd like to have Scott and Paul ack it too.

Alex

Quoted 122 lines
arch/powerpc/kvm/powerpc.c +0 -0
       kvm_vcpu_kick(vcpu);
 
       return 0;
arch/s390/kvm/kvm-s390.c +8 -0
index 0b91679..aa907b7 100644
@@ -422,6 +422,14 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
       return 0;
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     /* kvm common code refers to this, but never calls it */
+     BUG();
+     return 0;
+}
+
+
 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
 {
       kvm_s390_vcpu_initial_reset(vcpu);
arch/x86/kvm/x86.c +2 -14
index 2bd77a3..244bc9a 100644
@@ -6207,21 +6207,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
                kvm_cpu_has_interrupt(vcpu));
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
-     int me;
-     int cpu = vcpu->cpu;
-
-     if (waitqueue_active(&vcpu->wq)) {
-             wake_up_interruptible(&vcpu->wq);
-             ++vcpu->stat.halt_wakeup;
-     }
-
-     me = get_cpu();
-     if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
-             if (kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE)
-                     smp_send_reschedule(cpu);
-     put_cpu();
+     return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
include/linux/kvm_host.h +9 -0
index 9698080..f72e5f9 100644
@@ -438,6 +438,7 @@ void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
                            gfn_t gfn);
 
 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
 void kvm_resched(struct kvm_vcpu *vcpu);
 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
@@ -506,6 +507,7 @@ int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 void kvm_free_physmem(struct kvm *kvm);
 
@@ -521,6 +523,13 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
 }
 #endif
 
+#ifndef __KVM_HAVE_ARCH_VCPU_GET_WQ
+static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+     return &vcpu->wq;
+}
+#endif
+
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
 void kvm_arch_destroy_vm(struct kvm *kvm);
 void kvm_free_all_assigned_devices(struct kvm *kvm);
virt/kvm/kvm_main.c +22 -1
index 470e305..b531928 100644
@@ -1562,6 +1562,28 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
       finish_wait(&vcpu->wq, &wait);
 }
 
+/*
+ * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
+ */
+void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
+{
+     int me;
+     int cpu = vcpu->cpu;
+     wait_queue_head_t *wqp;
+
+     wqp = kvm_arch_vcpu_wq(vcpu);
+     if (waitqueue_active(wqp)) {
+             wake_up_interruptible(wqp);
+             ++vcpu->stat.halt_wakeup;
+     }
+
+     me = get_cpu();
+     if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
+             if (kvm_arch_vcpu_should_kick(vcpu))
+                     smp_send_reschedule(cpu);
+     put_cpu();
+}
+
 void kvm_resched(struct kvm_vcpu *vcpu)
 {
       if (!need_resched())
 
--

To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 4 lines · Christoffer Dall

Any news on the status of this?

Quoted 1 line · Alexander Graf

Apart from this little glitch the patch looks fine to me. But I'd like to have Scott and Paul ack it too.

ACK

-Scott

#5 lore raw
In reply to Christoffer Dall
Quoted 32 lines · Christoffer Dall

Any news on the status of this?

Quoted 29 lines · Alexander Graf
Quoted 26 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

Applied, thanks.

#6 lore raw
In reply to Christoffer Dall
Quoted 101 lines · Christoffer Dall

Any news on the status of this?

Quoted 98 lines · Alexander Graf
Quoted 95 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---

arch/ia64/include/asm/kvm_host.h    |    1 +
arch/ia64/kvm/kvm-ia64.c            |   20 +++++---------------
arch/powerpc/include/asm/kvm_host.h |    6 ++++++
arch/powerpc/kvm/powerpc.c          |   21 ++++++---------------
arch/s390/kvm/kvm-s390.c            |    8 ++++++++
arch/x86/kvm/x86.c                  |   16 ++--------------
include/linux/kvm_host.h            |    9 +++++++++
virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++
8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+     u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-     int me;
-     int cpu = vcpu->cpu;
-
-     if (waitqueue_active(&vcpu->wq))
-             wake_up_interruptible(&vcpu->wq);
-
-     me = get_cpu();
-     if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-             if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-                     smp_send_reschedule(cpu);
-     put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
      return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
              (kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
                                  struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +0 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR      0x0040
 #define KVM_MMIO_REG_FQPR     0x0060
 
Quoted 4 lines
Quoted 4 lines

arch/powerpc/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 501

Quoted 4 lines
arch/powerpc/include/asm/kvm_host.h +4 -0
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+     return vcpu->arch.wqp;

Sorry for the late notification - I didn't compile test it.

That code breaks for 2 reasons:

1) It's wait_queue_head_t, not wait_queue_head
2) struct kvm_vcpu isn't defined here yet, only arch is. Maybe we could pass in &vcpu->arch instead of vcpu? Or move the function somewhere else?

Alex

#7 lore raw
Quoted 111 lines · Alexander Graf
Quoted 101 lines · Christoffer Dall

Any news on the status of this?

Quoted 98 lines · Alexander Graf
Quoted 95 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---
arch/ia64/include/asm/kvm_host.h    |    1 + arch/ia64/kvm/kvm-ia64.c            |   20 +++++--------------- arch/powerpc/include/asm/kvm_host.h |    6 ++++++ arch/powerpc/kvm/powerpc.c          |   21 ++++++--------------- arch/s390/kvm/kvm-s390.c            |    8 ++++++++ arch/x86/kvm/x86.c                  |   16 ++-------------- include/linux/kvm_host.h            |    9 +++++++++ virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++

8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+     u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-     int me;
-     int cpu = vcpu->cpu;
-
-     if (waitqueue_active(&vcpu->wq))
-             wake_up_interruptible(&vcpu->wq);
-
-     me = get_cpu();
-     if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-             if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-                     smp_send_reschedule(cpu);
-     put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
       return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
               (kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
                                   struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +0 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR      0x0040
 #define KVM_MMIO_REG_FQPR     0x0060
 
Quoted 4 lines
Quoted 4 lines

arch/powerpc/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 501

Quoted 4 lines
arch/powerpc/include/asm/kvm_host.h +4 -0
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+     return vcpu->arch.wqp;

Sorry for the late notification - I didn't compile test it.

Quoted 6 lines

That code breaks for 2 reasons:

 1) It's wait_queue_head_t, not wait_queue_head
 2) struct kvm_vcpu isn't defined here yet, only arch is. Maybe we could pass in &vcpu->arch instead of vcpu? Or move the function somewhere else?

Alex, would you mind fixing it up? I don't have powerpc cross compilation set up and would like to avoid getting into that just these days?

-Christoffer

#8 lore raw
Quoted 114 lines · Christoffer Dall
Quoted 113 lines · Alexander Graf
Quoted 101 lines · Christoffer Dall

Any news on the status of this?

Quoted 98 lines · Alexander Graf
Quoted 95 lines · Christoffer Dall

The kvm_vcpu_kick function performs roughly the same funcitonality on most all architectures, so we shouldn't have separate copies.

PowerPC keeps a pointer to interchanging waitqueues on the vcpu_arch structure and to accomodate this special need a __KVM_HAVE_ARCH_VCPU_GET_WQ define and accompanying function kvm_arch_vcpu_wq have been defined. For all other architectures this is a generic inline that just returns &vcpu->wq;

This patch applies to d1c28f7568a74faacec896ee4f84afbffd20e5ab on
git://git.kernel.org/pub/scm/virt/kvm/kvm.git.

Changes since v3:
- Doesn't try to generalize vcpu->mode across all architectures and instead calls kvm_arch_vcpu_should_kick, which is properly defined on x86 and ia64, but other architectures simply return 1 as to maintain status quo.

Changes since v2:
- Restore arch-specific vcpu->cpu assignment to arch-specific code

Changes since v1:
- Abstact CPU mode check into arch-specific function
- Remove redundant vcpu->cpu assignment

---

arch/ia64/include/asm/kvm_host.h    |    1 +
arch/ia64/kvm/kvm-ia64.c            |   20 +++++---------------
arch/powerpc/include/asm/kvm_host.h |    6 ++++++
arch/powerpc/kvm/powerpc.c          |   21 ++++++---------------
arch/s390/kvm/kvm-s390.c            |    8 ++++++++
arch/x86/kvm/x86.c                  |   16 ++--------------
include/linux/kvm_host.h            |    9 +++++++++
virt/kvm/kvm_main.c                 |   22 ++++++++++++++++++++++
8 files changed, 59 insertions(+), 44 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index 2689ee5..06a5e91 100644
@@ -365,6 +365,7 @@ struct thash_cb {
 };
 
 struct kvm_vcpu_stat {
+     u32 halt_wakeup;
 };
 
 struct kvm_vcpu_arch {
arch/ia64/kvm/kvm-ia64.c +5 -15
index 8ca7261..96dceaa 100644
@@ -1857,21 +1857,6 @@ void kvm_arch_hardware_unsetup(void)
 {
 }
 
-void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
-{
-     int me;
-     int cpu = vcpu->cpu;
-
-     if (waitqueue_active(&vcpu->wq))
-             wake_up_interruptible(&vcpu->wq);
-
-     me = get_cpu();
-     if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
-             if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
-                     smp_send_reschedule(cpu);
-     put_cpu();
-}
-
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
 {
      return __apic_accept_irq(vcpu, irq->vector);
@@ -1941,6 +1926,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
              (kvm_highest_pending_irq(vcpu) != -1);
 }
 
+int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
+{
+     return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
                                  struct kvm_mp_state *mp_state)
 {
arch/powerpc/include/asm/kvm_host.h +0 -0
index 1843d5d..d089969 100644
@@ -495,4 +495,10 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_QPR      0x0040
 #define KVM_MMIO_REG_FQPR     0x0060
 
Quoted 4 lines
Quoted 4 lines

arch/powerpc/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 501

Quoted 4 lines
arch/powerpc/include/asm/kvm_host.h +4 -0
+#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
+static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
+{
+     return vcpu->arch.wqp;

Sorry for the late notification - I didn't compile test it.

That code breaks for 2 reasons:

Quoted 8 lines
Quoted 4 lines

1) It's wait_queue_head_t, not wait_queue_head
2) struct kvm_vcpu isn't defined here yet, only arch is. Maybe we could pass in &vcpu->arch instead of vcpu? Or move the function somewhere else?

Alex, would you mind fixing it up? I don't have powerpc cross compilation set up and would like to avoid getting into that just these days?

Yeah, trying to get my head around all the different layers of #include's ;). It's not exactly easy to find the right spot to put this in.

Alex