Skip to content

Commit b6d3383

Browse files
Christoffer Dallavikivity
authored andcommitted
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; Acked-by: Scott Wood <scottwood@freescale.com> Signed-off-by: Christoffer Dall <c.dall@virtualopensystems.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 66ef893 commit b6d3383

8 files changed

Lines changed: 59 additions & 44 deletions

File tree

‎arch/ia64/include/asm/kvm_host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ struct thash_cb {
365365
};
366366

367367
struct kvm_vcpu_stat {
368+
u32 halt_wakeup;
368369
};
369370

370371
struct kvm_vcpu_arch {

‎arch/ia64/kvm/kvm-ia64.c‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,21 +1872,6 @@ void kvm_arch_hardware_unsetup(void)
18721872
{
18731873
}
18741874

1875-
void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1876-
{
1877-
int me;
1878-
int cpu = vcpu->cpu;
1879-
1880-
if (waitqueue_active(&vcpu->wq))
1881-
wake_up_interruptible(&vcpu->wq);
1882-
1883-
me = get_cpu();
1884-
if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
1885-
if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
1886-
smp_send_reschedule(cpu);
1887-
put_cpu();
1888-
}
1889-
18901875
int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
18911876
{
18921877
return __apic_accept_irq(vcpu, irq->vector);
@@ -1956,6 +1941,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
19561941
(kvm_highest_pending_irq(vcpu) != -1);
19571942
}
19581943

1944+
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1945+
{
1946+
return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
1947+
}
1948+
19591949
int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
19601950
struct kvm_mp_state *mp_state)
19611951
{

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,10 @@ struct kvm_vcpu_arch {
498498
#define KVM_MMIO_REG_QPR 0x0040
499499
#define KVM_MMIO_REG_FQPR 0x0060
500500

501+
#define __KVM_HAVE_ARCH_VCPU_GET_WQ 1
502+
static inline wait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
503+
{
504+
return vcpu->arch.wqp;
505+
}
506+
501507
#endif /* __POWERPC_KVM_HOST_H__ */

‎arch/powerpc/kvm/powerpc.c‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
4343
v->requests;
4444
}
4545

46+
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
47+
{
48+
return 1;
49+
}
50+
4651
int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
4752
{
4853
int nr = kvmppc_get_gpr(vcpu, 11);
@@ -588,21 +593,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
588593
return r;
589594
}
590595

591-
void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
592-
{
593-
int me;
594-
int cpu = vcpu->cpu;
595-
596-
me = get_cpu();
597-
if (waitqueue_active(vcpu->arch.wqp)) {
598-
wake_up_interruptible(vcpu->arch.wqp);
599-
vcpu->stat.halt_wakeup++;
600-
} else if (cpu != me && cpu != -1) {
601-
smp_send_reschedule(vcpu->cpu);
602-
}
603-
put_cpu();
604-
}
605-
606596
int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
607597
{
608598
if (irq->irq == KVM_INTERRUPT_UNSET) {
@@ -611,6 +601,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
611601
}
612602

613603
kvmppc_core_queue_external(vcpu, irq);
604+
614605
kvm_vcpu_kick(vcpu);
615606

616607
return 0;

‎arch/s390/kvm/kvm-s390.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
423423
return 0;
424424
}
425425

426+
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
427+
{
428+
/* kvm common code refers to this, but never calls it */
429+
BUG();
430+
return 0;
431+
}
432+
433+
426434
static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
427435
{
428436
kvm_s390_vcpu_initial_reset(vcpu);

‎arch/x86/kvm/x86.c‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6403,21 +6403,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
64036403
kvm_cpu_has_interrupt(vcpu));
64046404
}
64056405

6406-
void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
6406+
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
64076407
{
6408-
int me;
6409-
int cpu = vcpu->cpu;
6410-
6411-
if (waitqueue_active(&vcpu->wq)) {
6412-
wake_up_interruptible(&vcpu->wq);
6413-
++vcpu->stat.halt_wakeup;
6414-
}
6415-
6416-
me = get_cpu();
6417-
if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
6418-
if (kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE)
6419-
smp_send_reschedule(cpu);
6420-
put_cpu();
6408+
return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
64216409
}
64226410

64236411
int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)

‎include/linux/kvm_host.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
439439
gfn_t gfn);
440440

441441
void kvm_vcpu_block(struct kvm_vcpu *vcpu);
442+
void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
442443
void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
443444
void kvm_resched(struct kvm_vcpu *vcpu);
444445
void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
@@ -507,6 +508,7 @@ int kvm_arch_hardware_setup(void);
507508
void kvm_arch_hardware_unsetup(void);
508509
void kvm_arch_check_processor_compat(void *rtn);
509510
int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
511+
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
510512

511513
void kvm_free_physmem(struct kvm *kvm);
512514

@@ -522,6 +524,13 @@ static inline void kvm_arch_free_vm(struct kvm *kvm)
522524
}
523525
#endif
524526

527+
#ifndef __KVM_HAVE_ARCH_VCPU_GET_WQ
528+
static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
529+
{
530+
return &vcpu->wq;
531+
}
532+
#endif
533+
525534
int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
526535
void kvm_arch_destroy_vm(struct kvm *kvm);
527536
void kvm_free_all_assigned_devices(struct kvm *kvm);

‎virt/kvm/kvm_main.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,28 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
15141514
finish_wait(&vcpu->wq, &wait);
15151515
}
15161516

1517+
/*
1518+
* Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
1519+
*/
1520+
void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1521+
{
1522+
int me;
1523+
int cpu = vcpu->cpu;
1524+
wait_queue_head_t *wqp;
1525+
1526+
wqp = kvm_arch_vcpu_wq(vcpu);
1527+
if (waitqueue_active(wqp)) {
1528+
wake_up_interruptible(wqp);
1529+
++vcpu->stat.halt_wakeup;
1530+
}
1531+
1532+
me = get_cpu();
1533+
if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
1534+
if (kvm_arch_vcpu_should_kick(vcpu))
1535+
smp_send_reschedule(cpu);
1536+
put_cpu();
1537+
}
1538+
15171539
void kvm_resched(struct kvm_vcpu *vcpu)
15181540
{
15191541
if (!need_resched())

0 commit comments

Comments
 (0)