Skip to content

Commit da4ad88

Browse files
Davidlohr Buesobonzini
authored andcommitted
kvm: Replace vcpu->swait with rcuwait
The use of any sort of waitqueue (simple or regular) for wait/waking vcpus has always been an overkill and semantically wrong. Because this is per-vcpu (which is blocked) there is only ever a single waiting vcpu, thus no need for any sort of queue. As such, make use of the rcuwait primitive, with the following considerations: - rcuwait already provides the proper barriers that serialize concurrent waiter and waker. - Task wakeup is done in rcu read critical region, with a stable task pointer. - Because there is no concurrency among waiters, we need not worry about rcuwait_wait_event() calls corrupting the wait->task. As a consequence, this saves the locking done in swait when modifying the queue. This also applies to per-vcore wait for powerpc kvm-hv. The x86 tscdeadline_latency test mentioned in 8577370 ("KVM: Use simple waitqueue for vcpu->wq") shows that, on avg, latency is reduced by around 15-20% with this change. Cc: Paul Mackerras <paulus@ozlabs.org> Cc: kvmarm@lists.cs.columbia.edu Cc: linux-mips@vger.kernel.org Reviewed-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Message-Id: <20200424054837.5138-6-dave@stgolabs.net> [Avoid extra logic changes. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 191a43b commit da4ad88

11 files changed

Lines changed: 38 additions & 43 deletions

File tree

‎arch/mips/kvm/mips.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
283283
kvm_mips_callbacks->queue_timer_int(vcpu);
284284

285285
vcpu->arch.wait = 0;
286-
if (swq_has_sleeper(&vcpu->wq))
287-
swake_up_one(&vcpu->wq);
286+
rcuwait_wake_up(&vcpu->wait);
288287

289288
return kvm_mips_count_timeout(vcpu);
290289
}
@@ -511,8 +510,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
511510

512511
dvcpu->arch.wait = 0;
513512

514-
if (swq_has_sleeper(&dvcpu->wq))
515-
swake_up_one(&dvcpu->wq);
513+
rcuwait_wake_up(&dvcpu->wait);
516514

517515
return 0;
518516
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct kvmppc_vcore {
7878
struct kvm_vcpu *runnable_threads[MAX_SMT_THREADS];
7979
struct list_head preempt_list;
8080
spinlock_t lock;
81-
struct swait_queue_head wq;
81+
struct rcuwait wait;
8282
spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
8383
u64 stolen_tb;
8484
u64 preempt_tb;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ struct kvm_vcpu_arch {
751751
u8 irq_pending; /* Used by XIVE to signal pending guest irqs */
752752
u32 last_inst;
753753

754-
struct swait_queue_head *wqp;
754+
struct rcuwait *waitp;
755755
struct kvmppc_vcore *vcore;
756756
int ret;
757757
int trap;

‎arch/powerpc/kvm/book3s_hv.c‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,11 @@ static bool kvmppc_ipi_thread(int cpu)
230230
static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
231231
{
232232
int cpu;
233-
struct swait_queue_head *wqp;
233+
struct rcuwait *waitp;
234234

235-
wqp = kvm_arch_vcpu_wq(vcpu);
236-
if (swq_has_sleeper(wqp)) {
237-
swake_up_one(wqp);
235+
waitp = kvm_arch_vcpu_get_wait(vcpu);
236+
if (rcuwait_wake_up(waitp))
238237
++vcpu->stat.halt_wakeup;
239-
}
240238

241239
cpu = READ_ONCE(vcpu->arch.thread_cpu);
242240
if (cpu >= 0 && kvmppc_ipi_thread(cpu))
@@ -2125,7 +2123,7 @@ static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
21252123

21262124
spin_lock_init(&vcore->lock);
21272125
spin_lock_init(&vcore->stoltb_lock);
2128-
init_swait_queue_head(&vcore->wq);
2126+
rcuwait_init(&vcore->wait);
21292127
vcore->preempt_tb = TB_NIL;
21302128
vcore->lpcr = kvm->arch.lpcr;
21312129
vcore->first_vcpuid = id;
@@ -3784,7 +3782,6 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
37843782
ktime_t cur, start_poll, start_wait;
37853783
int do_sleep = 1;
37863784
u64 block_ns;
3787-
DECLARE_SWAITQUEUE(wait);
37883785

37893786
/* Poll for pending exceptions and ceded state */
37903787
cur = start_poll = ktime_get();
@@ -3812,10 +3809,10 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
38123809
}
38133810
}
38143811

3815-
prepare_to_swait_exclusive(&vc->wq, &wait, TASK_INTERRUPTIBLE);
3816-
3812+
prepare_to_rcuwait(&vc->wait);
3813+
set_current_state(TASK_INTERRUPTIBLE);
38173814
if (kvmppc_vcore_check_block(vc)) {
3818-
finish_swait(&vc->wq, &wait);
3815+
finish_rcuwait(&vc->wait);
38193816
do_sleep = 0;
38203817
/* If we polled, count this as a successful poll */
38213818
if (vc->halt_poll_ns)
@@ -3829,7 +3826,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
38293826
trace_kvmppc_vcore_blocked(vc, 0);
38303827
spin_unlock(&vc->lock);
38313828
schedule();
3832-
finish_swait(&vc->wq, &wait);
3829+
finish_rcuwait(&vc->wait);
38333830
spin_lock(&vc->lock);
38343831
vc->vcore_state = VCORE_INACTIVE;
38353832
trace_kvmppc_vcore_blocked(vc, 1);
@@ -3940,7 +3937,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
39403937
kvmppc_start_thread(vcpu, vc);
39413938
trace_kvm_guest_enter(vcpu);
39423939
} else if (vc->vcore_state == VCORE_SLEEPING) {
3943-
swake_up_one(&vc->wq);
3940+
rcuwait_wake_up(&vc->wait);
39443941
}
39453942

39463943
}
@@ -4279,7 +4276,7 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
42794276
}
42804277
user_vrsave = mfspr(SPRN_VRSAVE);
42814278

4282-
vcpu->arch.wqp = &vcpu->arch.vcore->wq;
4279+
vcpu->arch.waitp = &vcpu->arch.vcore->wait;
42834280
vcpu->arch.pgdir = kvm->mm->pgd;
42844281
vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
42854282

‎arch/powerpc/kvm/powerpc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
752752
if (err)
753753
goto out_vcpu_uninit;
754754

755-
vcpu->arch.wqp = &vcpu->wq;
755+
vcpu->arch.waitp = &vcpu->wait;
756756
kvmppc_create_vcpu_debugfs(vcpu, vcpu->vcpu_id);
757757
return 0;
758758

‎arch/x86/kvm/lapic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
18611861
/* If the preempt notifier has already run, it also called apic_timer_expired */
18621862
if (!apic->lapic_timer.hv_timer_in_use)
18631863
goto out;
1864-
WARN_ON(swait_active(&vcpu->wq));
1864+
WARN_ON(rcuwait_active(&vcpu->wait));
18651865
cancel_hv_timer(apic);
18661866
apic_timer_expired(apic);
18671867

‎include/linux/kvm_host.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <linux/irqflags.h>
2424
#include <linux/context_tracking.h>
2525
#include <linux/irqbypass.h>
26-
#include <linux/swait.h>
26+
#include <linux/rcuwait.h>
2727
#include <linux/refcount.h>
2828
#include <linux/nospec.h>
2929
#include <asm/signal.h>
@@ -277,7 +277,7 @@ struct kvm_vcpu {
277277
struct mutex mutex;
278278
struct kvm_run *run;
279279

280-
struct swait_queue_head wq;
280+
struct rcuwait wait;
281281
struct pid __rcu *pid;
282282
int sigset_active;
283283
sigset_t sigset;
@@ -960,12 +960,12 @@ static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
960960
}
961961
#endif
962962

963-
static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
963+
static inline struct rcuwait *kvm_arch_vcpu_get_wait(struct kvm_vcpu *vcpu)
964964
{
965965
#ifdef __KVM_HAVE_ARCH_WQP
966-
return vcpu->arch.wqp;
966+
return vcpu->arch.waitp;
967967
#else
968-
return &vcpu->wq;
968+
return &vcpu->wait;
969969
#endif
970970
}
971971

‎virt/kvm/arm/arch_timer.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
571571
{
572572
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
573573
struct timer_map map;
574+
struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
574575

575576
if (unlikely(!timer->enabled))
576577
return;
@@ -593,7 +594,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
593594
if (map.emul_ptimer)
594595
soft_timer_cancel(&map.emul_ptimer->hrtimer);
595596

596-
if (swait_active(kvm_arch_vcpu_wq(vcpu)))
597+
if (rcuwait_active(wait))
597598
kvm_timer_blocking(vcpu);
598599

599600
/*

‎virt/kvm/arm/arm.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,17 @@ void kvm_arm_resume_guest(struct kvm *kvm)
579579

580580
kvm_for_each_vcpu(i, vcpu, kvm) {
581581
vcpu->arch.pause = false;
582-
swake_up_one(kvm_arch_vcpu_wq(vcpu));
582+
rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu));
583583
}
584584
}
585585

586586
static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
587587
{
588-
struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
588+
struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
589589

590-
swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
591-
(!vcpu->arch.pause)));
590+
rcuwait_wait_event(wait,
591+
(!vcpu->arch.power_off) &&(!vcpu->arch.pause),
592+
TASK_INTERRUPTIBLE);
592593

593594
if (vcpu->arch.power_off || vcpu->arch.pause) {
594595
/* Awaken to handle a signal, request we sleep again later. */

‎virt/kvm/async_pf.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ static void async_pf_execute(struct work_struct *work)
8080

8181
trace_kvm_async_pf_completed(addr, cr2_or_gpa);
8282

83-
if (swq_has_sleeper(&vcpu->wq))
84-
swake_up_one(&vcpu->wq);
83+
rcuwait_wake_up(&vcpu->wait);
8584

8685
mmput(mm);
8786
kvm_put_kvm(vcpu->kvm);

0 commit comments

Comments
 (0)