History

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 8577370fb0cb ("KVM: Use simple waitqueue for vcpu->wq") shows that, on avg, latency is reduced by around 15-20% with this change.

Davidlohr Bueso committed 6 years ago

Commit da4ad881 parent 191a43b11 files · +38 −43Browse files

Unified · Split

11 files
  1. arch/mips/kvm/mips.c +2 −4
  2. arch/powerpc/include/asm/kvm_book3s.h +1 −1
  3. arch/powerpc/include/asm/kvm_host.h +1 −1
  4. arch/powerpc/kvm/book3s_hv.c +10 −13
  5. arch/powerpc/kvm/powerpc.c +1 −1
  6. arch/x86/kvm/lapic.c +1 −1
  7. include/linux/kvm_host.h +5 −5
  8. virt/kvm/arm/arch_timer.c +2 −1
  9. virt/kvm/arm/arm.c +5 −4
  10. virt/kvm/async_pf.c +1 −2
  11. virt/kvm/kvm_main.c +9 −10
arch/mips/kvm/mips.c+2−4
@@ -283,8 +283,7 @@ static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer) · 282 unmodified lines
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, · 220 unmodified lines
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+1−1
@@ -78,7 +78,7 @@ struct kvmppc_vcore { · 77 unmodified lines
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+1−1
@@ -751,7 +751,7 @@ struct kvm_vcpu_arch { · 750 unmodified lines
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+10−13
@@ -230,13 +230,11 @@ static bool kvmppc_ipi_thread(int cpu) · 229 unmodified lines
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) · 1882 unmodified lines
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) · 1652 unmodified lines
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) · 21 unmodified lines
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) · 7 unmodified lines
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) · 104 unmodified lines
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) · 332 unmodified lines
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+1−1
@@ -752,7 +752,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · 751 unmodified lines
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+1−1
@@ -1861,7 +1861,7 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu) · 1860 unmodified lines
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+5−5
@@ -23,7 +23,7 @@ · 22 unmodified lines
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 { · 247 unmodified lines
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) · 676 unmodified lines
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+2−1
@@ -571,6 +571,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) · 570 unmodified lines
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) · 16 unmodified lines
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+5−4
@@ -579,16 +579,17 @@ void kvm_arm_resume_guest(struct kvm *kvm) · 578 unmodified lines
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+1−2
@@ -80,8 +80,7 @@ static void async_pf_execute(struct work_struct *work) · 79 unmodified lines
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);
virt/kvm/kvm_main.c+9−10
@@ -349,7 +349,7 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) · 348 unmodified lines
349349 vcpu->kvm = kvm;
350350 vcpu->vcpu_id = id;
351351 vcpu->pid = NULL;
352− init_swait_queue_head(&vcpu->wq);
352+ rcuwait_init(&vcpu->wait);
353353 kvm_async_pf_vcpu_init(vcpu);
354354
355355 vcpu->pre_pcpu = -1;
@@ -2678,7 +2678,6 @@ out: · 2322 unmodified lines
26782678 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
26792679 {
26802680 ktime_t start, cur;
2681− DECLARE_SWAITQUEUE(wait);
26822681 bool waited = false;
26832682 u64 block_ns;
26842683
@@ -2704,8 +2703,9 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 19 unmodified lines
27042703 } while (single_task_running() && ktime_before(cur, stop));
27052704 }
27062705
2706+ prepare_to_rcuwait(&vcpu->wait);
27072707 for (;;) {
2708− prepare_to_swait_exclusive(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
2708+ set_current_state(TASK_INTERRUPTIBLE);
27092709
27102710 if (kvm_vcpu_check_block(vcpu) < 0)
27112711 break;
@@ -2713,8 +2713,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) · 1 unmodified line
27132713 waited = true;
27142714 schedule();
27152715 }
2716−
2717− finish_swait(&vcpu->wq, &wait);
2716+ finish_rcuwait(&vcpu->wait);
27182717 cur = ktime_get();
27192718 out:
27202719 kvm_arch_vcpu_unblocking(vcpu);
@@ -2746,11 +2745,10 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_block); · 25 unmodified lines
27462745
27472746 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
27482747 {
2749− struct swait_queue_head *wqp;
2748+ struct rcuwait *waitp;
27502749
2751− wqp = kvm_arch_vcpu_wq(vcpu);
2752− if (swq_has_sleeper(wqp)) {
2753− swake_up_one(wqp);
2750+ waitp = kvm_arch_vcpu_get_wait(vcpu);
2751+ if (rcuwait_wake_up(waitp)) {
27542752 WRITE_ONCE(vcpu->ready, true);
27552753 ++vcpu->stat.halt_wakeup;
27562754 return true;
@@ -2892,7 +2890,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) · 135 unmodified lines
28922890 continue;
28932891 if (vcpu == me)
28942892 continue;
2895− if (swait_active(&vcpu->wq) && !vcpu_dy_runnable(vcpu))
2893+ if (rcuwait_active(&vcpu->wait) &&
2894+ !vcpu_dy_runnable(vcpu))
28962895 continue;
28972896 if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
28982897 !kvm_arch_vcpu_in_kernel(vcpu))