diff options
| author | Liran Alon <liran.alon@oracle.com> | 2019-04-16 13:36:34 -0400 |
|---|---|---|
| committer | Paolo Bonzini <pbonzini@redhat.com> | 2019-04-18 12:53:17 -0400 |
| commit | c09d65d9eab69985c75f98ed64541229f6fa9aa6 (patch) | |
| tree | c741b2c49a84b863e5c8c27731c9f370a02c4509 | |
| parent | 78671ab4c9f42e2348bff60e2f4a0b3bb62353af (diff) | |
KVM: x86: Consider LAPIC TSC-Deadline timer expired if deadline too short
If guest sets MSR_IA32_TSCDEADLINE to value such that in host
time-domain it's shorter than lapic_timer_advance_ns, we can
reach a case that we call hrtimer_start() with expiration time set at
the past.
Because lapic_timer.timer is init with HRTIMER_MODE_ABS_PINNED, it
is not allowed to run in softirq and therefore will never expire.
To avoid such a scenario, verify that deadline expiration time is set on
host time-domain further than (now + lapic_timer_advance_ns).
A future patch can also consider adding a min_timer_deadline_ns module parameter,
similar to min_timer_period_us to avoid races that amount of ns it takes
to run logic could still call hrtimer_start() with expiration timer set
at the past.
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Signed-off-by: Liran Alon <liran.alon@oracle.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
| -rw-r--r-- | arch/x86/kvm/lapic.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 9bf70cf84564..6cb990dbc15a 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c | |||
| @@ -1542,9 +1542,12 @@ static void start_sw_tscdeadline(struct kvm_lapic *apic) | |||
| 1542 | 1542 | ||
| 1543 | now = ktime_get(); | 1543 | now = ktime_get(); |
| 1544 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); | 1544 | guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 1545 | if (likely(tscdeadline > guest_tsc)) { | 1545 | |
| 1546 | ns = (tscdeadline - guest_tsc) * 1000000ULL; | 1546 | ns = (tscdeadline - guest_tsc) * 1000000ULL; |
| 1547 | do_div(ns, this_tsc_khz); | 1547 | do_div(ns, this_tsc_khz); |
| 1548 | |||
| 1549 | if (likely(tscdeadline > guest_tsc) && | ||
| 1550 | likely(ns > lapic_timer_advance_ns)) { | ||
| 1548 | expire = ktime_add_ns(now, ns); | 1551 | expire = ktime_add_ns(now, ns); |
| 1549 | expire = ktime_sub_ns(expire, lapic_timer_advance_ns); | 1552 | expire = ktime_sub_ns(expire, lapic_timer_advance_ns); |
| 1550 | hrtimer_start(&apic->lapic_timer.timer, | 1553 | hrtimer_start(&apic->lapic_timer.timer, |
