KVM: x86: limit PIT timer frequency
authorMarcelo Tosatti <mtosatti@redhat.com>
Mon, 6 Jan 2014 14:00:02 +0000 (12:00 -0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 6 Feb 2014 19:08:12 +0000 (11:08 -0800)
commit 9ed96e87c5748de4c2807ef17e81287c7304186c upstream.

Limit PIT timer frequency similarly to the limit applied by
LAPIC timer.

Reviewed-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kvm/i8254.c
arch/x86/kvm/lapic.c
arch/x86/kvm/x86.c
arch/x86/kvm/x86.h

index 412a5aa0ef94858ade3a32de9e81dfdbdf44d925..518d86471b76f0be7460c6e038755a18083b26b4 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "irq.h"
 #include "i8254.h"
+#include "x86.h"
 
 #ifndef CONFIG_X86_64
 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
@@ -349,6 +350,23 @@ static void create_pit_timer(struct kvm *kvm, u32 val, int is_period)
        atomic_set(&ps->pending, 0);
        ps->irq_ack = 1;
 
+       /*
+        * Do not allow the guest to program periodic timers with small
+        * interval, since the hrtimers are not throttled by the host
+        * scheduler.
+        */
+       if (ps->is_periodic) {
+               s64 min_period = min_timer_period_us * 1000LL;
+
+               if (ps->period < min_period) {
+                       pr_info_ratelimited(
+                           "kvm: requested %lld ns "
+                           "i8254 timer period limited to %lld ns\n",
+                           ps->period, min_period);
+                       ps->period = min_period;
+               }
+       }
+
        hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
                      HRTIMER_MODE_ABS);
 }
index 683bf5057e7a19f9ad1f7f958f85a3b5bcf26f83..61d9fed5eb31dfebb58e20ee0a9060a2f4e0ba5e 100644 (file)
@@ -71,9 +71,6 @@
 #define VEC_POS(v) ((v) & (32 - 1))
 #define REG_POS(v) (((v) >> 5) << 4)
 
-static unsigned int min_timer_period_us = 500;
-module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
-
 static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
 {
        *((u32 *) (apic->regs + reg_off)) = val;
index ad6a01605aaa15f5a05f54bd11a88ce140702c2d..1bd6ea07d7d30a882b3d7d8b48c735bb91ef2917 100644 (file)
@@ -94,6 +94,9 @@ EXPORT_SYMBOL_GPL(kvm_x86_ops);
 static bool ignore_msrs = 0;
 module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
 
+unsigned int min_timer_period_us = 500;
+module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
+
 bool kvm_has_tsc_control;
 EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
 u32  kvm_max_guest_tsc_khz;
index e224f7a671b66fb23a291cdf25c5eda7f4bdd157..3186542f2fa3bb5069e0729f49450b1dabd1a1bc 100644 (file)
@@ -124,5 +124,7 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
 
 extern u64 host_xcr0;
 
+extern unsigned int min_timer_period_us;
+
 extern struct static_key kvm_no_apic_vcpu;
 #endif