Skip to contentSkip to conversation

LoongArch: KVM: Reload one-shot TVAL on migration destination

PATCH Superseded Tao Cui 2 participants 2 messages Jul 15

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH v2 1/1 LoongArch: KVM: Reload one-shot TVAL on migration destination

From: Tao Cui <[email protected]>

kvm_restore_timer() rebuilds the remaining timer countdown from vcpu->arch.expire, which is host-internal and is not part of the migrated vCPU state. On the migration destination it is still 0, so for a one-shot timer that has not expired yet the computed delta is 0 and write_gcsr_timertick(0) injects the timer interrupt immediately instead of after the remaining time.

The expired one-shot case (TVAL = -1) is already handled earlier. When expire has not been set (i.e. on the migration destination), reload the remaining countdown from the migrated TVAL. The regular preempt/resume path on the source, where expire is valid, is unchanged.

Fixes a5857b9ff6e0 ("LoongArch: KVM: Implement vcpu timer operations")

---

Changes in v2:
- Move the expire check before ktime_before() (suggested by Bibo Mao)
- Remove the old else-if branch that was one-shot only.

---
 arch/loongarch/kvm/timer.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/loongarch/kvm/timer.c +11 -0
index 3829f35a4070..08359a612bde 100644
@@ -119,6 +119,17 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
 	delta = 0;
 	now = ktime_get();
 	expire = vcpu->arch.expire;
+	if (!expire) {
+		/*
+		 * vcpu->arch.expire is host-internal and is not migrated,
+		 * so it is 0 after migration. Reload the remaining countdown
+		 * from the migrated TVAL.
+		 */
+		if (ticks < cfg)
+			delta = tick_to_ns(vcpu, ticks);
+		expire = ktime_add_ns(ktime_get(), delta);
+	}
+
 	if (ktime_before(now, expire))
 		delta = ktime_to_tick(vcpu, ktime_sub(expire, now));
 	else if (cfg & CSR_TCFG_PERIOD) {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] Redundant ktime_get() call introduces a race condition and non-deterministic timer inflation.

Pre-existing issues:
- [High] Unvalidated guest-controlled timer period leads to a division by zero and host kernel panic. --

--- Patch [1]: [PATCH v2] LoongArch: KVM: Reload one-shot TVAL on migration destination --- commit 76bdf1c5f64a97f768f72eb78cf6f70484cc8af9
Author: Tao Cui <[email protected]>

LoongArch: KVM: Reload one-shot TVAL on migration destination

This commit handles restoring one-shot timers on migration destinations where vcpu->arch.expire is uninitialized. It calculates the remaining countdown using the migrated TVAL.

Quoted 4 lines
arch/loongarch/kvm/timer.c +0 -0
index 3829f35a4070b..08359a612bdee 100644

[ ... ]

arch/loongarch/kvm/timer.c void kvm_restore_timer(struct kvm_vcpu *vcpu) · line 130

Quoted 13 lines
arch/loongarch/kvm/timer.c +9 -0
@@ -119,6 +119,17 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
 	delta = 0;
 	now = ktime_get();
 	expire = vcpu->arch.expire;
+	if (!expire) {
+		/*
+		 * vcpu->arch.expire is host-internal and is not migrated,
+		 * so it is 0 after migration. Reload the remaining countdown
+		 * from the migrated TVAL.
+		 */
+		if (ticks < cfg)
+			delta = tick_to_ns(vcpu, ticks);
+		expire = ktime_add_ns(ktime_get(), delta);

[Severity: Medium]
Is it intentional to call ktime_get() again here instead of reusing the 'now' timestamp captured just above?

If the host thread is preempted or delayed between capturing 'now' and evaluating this second ktime_get(), the execution delay is effectively added to the remaining timer duration.

For an already expired one-shot timer, could this cause the code to program a small positive tick value instead of 0, delaying the interrupt injection?

Quoted 8 lines
arch/loongarch/kvm/timer.c +2 -0
+	}
+
 	if (ktime_before(now, expire))
 		delta = ktime_to_tick(vcpu, ktime_sub(expire, now));
 	else if (cfg & CSR_TCFG_PERIOD) {
		period = cfg & CSR_TCFG_VAL;
		delta = ktime_to_tick(vcpu, ktime_sub(now, expire));
		delta = period - (delta % period);

[Severity: High]
This isn't a bug introduced by this patch, but does this code allow a guest to trigger a host kernel panic?

If a malicious guest writes to LOONGARCH_CSR_TCFG, setting the CSR_TCFG_PERIOD bit but leaving the CSR_TCFG_VAL bits as 0, 'period' will evaluate to 0.

When the vCPU is scheduled out and later restored, this branch executes because the timer is past its expiration, resulting in a modulo by zero operation.

Could this division by zero in the host kernel be prevented by validating the guest-controlled timer period?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1