Skip to content

Commit 8bc15d0

Browse files
bibo-maochenhuacai
authored andcommitted
LoongArch: KVM: Start SW timer only when vcpu is blocking
SW timer is enabled when vcpu thread is scheduled out, and it is to wake up vcpu from blocked queue. If vcpu thread is scheduled out but is not blocked, such as it is preempted by other threads, it is not necessary to enable SW timer. Since vcpu thread is still on running queue if it is preempted and SW timer is only to wake up vcpu on blocking queue, so SW timer is not useful in this situation. This patch enables SW timer only when vcpu is scheduled out and is blocking. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent aebd3bd commit 8bc15d0

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

arch/loongarch/kvm/timer.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
9393
/*
9494
* Freeze the soft-timer and sync the guest stable timer with it.
9595
*/
96-
hrtimer_cancel(&vcpu->arch.swtimer);
96+
if (kvm_vcpu_is_blocking(vcpu))
97+
hrtimer_cancel(&vcpu->arch.swtimer);
9798

9899
/*
99100
* From LoongArch Reference Manual Volume 1 Chapter 7.6.2
@@ -168,26 +169,20 @@ static void _kvm_save_timer(struct kvm_vcpu *vcpu)
168169
* Here judge one-shot timer fired by checking whether TVAL is larger
169170
* than TCFG
170171
*/
171-
if (ticks < cfg) {
172+
if (ticks < cfg)
172173
delta = tick_to_ns(vcpu, ticks);
173-
expire = ktime_add_ns(ktime_get(), delta);
174-
vcpu->arch.expire = expire;
174+
else
175+
delta = 0;
176+
177+
expire = ktime_add_ns(ktime_get(), delta);
178+
vcpu->arch.expire = expire;
179+
if (kvm_vcpu_is_blocking(vcpu)) {
175180

176181
/*
177182
* HRTIMER_MODE_PINNED is suggested since vcpu may run in
178183
* the same physical cpu in next time
179184
*/
180185
hrtimer_start(&vcpu->arch.swtimer, expire, HRTIMER_MODE_ABS_PINNED);
181-
} else if (vcpu->stat.generic.blocking) {
182-
/*
183-
* Inject timer interrupt so that halt polling can dectect and exit.
184-
* VCPU is scheduled out already and sleeps in rcuwait queue and
185-
* will not poll pending events again. kvm_queue_irq() is not enough,
186-
* hrtimer swtimer should be used here.
187-
*/
188-
expire = ktime_add_ns(ktime_get(), 10);
189-
vcpu->arch.expire = expire;
190-
hrtimer_start(&vcpu->arch.swtimer, expire, HRTIMER_MODE_ABS_PINNED);
191186
}
192187
}
193188

0 commit comments

Comments
 (0)