Skip to content

RFC In-kernel PIT model

Yang, Sheng kvm 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest2 patches 13 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2KVM: In-kernel PIT modelarch/x86/kvm/Makefile arch/x86/kvm/i8254.c arch/x86/kvm/i8254.h +4 more+750 -111 replies
2/2kvm: libkvm: In-kernel PIT modelkernel/Kbuild libkvm/kvm-common.h libkvm/libkvm.c +3 more+62 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC][PATCH 0/2]In-kernel PIT model

Hi,

The patch have moved PIT from QEMU to kernel, which greatly increase the accuracy of KVM guest timer. The code mostly based on QEMU and Xen's code.

The patch works well on IA32e host(passed 2.6.22, 2.6.20, 2.6.18, 2.6.16 with hpet=disable, 2.6.9 with clock=pit), mostly OK on pae host(passed 2.6.18, 2.6.9 with clock=pit). But Linux 2.6.16 guest's timer on pae host is inaccuracy with the patch, about 1/10 faster. This is a regression, for it's OK with QEMU. The same kernel on IA32e need hpet=disable to keep accuracy, but I can't find a parameter to make it accuracy on pae host with 2.6.16.

The patch didn't contains save/restore part, for the function is broken in KVM now (I may spent some effect on this issue later).

Any comments are welcome!

Thanks
Yang, Sheng

From 56a50952929f9a7e78fc3ec812dd4550c623b956 Mon Sep 17 00:00:00 2001
From: Sheng Yang <[email protected]>
Date: Mon, 21 Jan 2008 16:42:37 +0800
Subject: [PATCH] KVM: In-kernel PIT model

---
 arch/x86/kvm/Makefile      |    3 +-
 arch/x86/kvm/i8254.c       |  589 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/i8254.h       |   60 +++++
 arch/x86/kvm/irq.c         |    3 +
 arch/x86/kvm/x86.c         |    9 +
 include/asm-x86/kvm_host.h |    1 +
 include/linux/kvm.h        |    2 +
 7 files changed, 666 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/i8254.c
 create mode 100644 arch/x86/kvm/i8254.h
arch/x86/kvm/Makefile +2 -1
index ffdd0b3..4d0c22e 100644
@@ -6,7 +6,8 @@ common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o)
 
 EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm
 
-kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o
+kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \
+	i8254.o
 obj-$(CONFIG_KVM) += kvm.o
 kvm-intel-objs = vmx.o
 obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
arch/x86/kvm/i8254.c new file +589 -0

589 lines not on this page · raw message

arch/x86/kvm/i8254.h new file +60 -0

60 lines not on this page · raw message

arch/x86/kvm/irq.c +3 -0
index e571475..dbfe21c 100644
@@ -23,6 +23,7 @@
 #include <linux/kvm_host.h>
 
 #include "irq.h"
+#include "i8254.h"
 
 /*
  * check if there is pending interrupt without
@@ -66,6 +67,7 @@ EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
+	kvm_inject_pit_timer_irqs(vcpu);
 	/* TODO: PIT, RTC etc. */
 }
 EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
@@ -73,6 +75,7 @@ EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
 void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
 {
 	kvm_apic_timer_intr_post(vcpu, vec);
+	kvm_pit_timer_intr_post(vcpu, vec);
 	/* TODO: PIT, RTC etc. */
 }
 EXPORT_SYMBOL_GPL(kvm_timer_intr_post);
arch/x86/kvm/x86.c +9 -0
index 8f94a0b..0993fad 100644
@@ -18,6 +18,7 @@
 #include "segment_descriptor.h"
 #include "irq.h"
 #include "mmu.h"
+#include "i8254.h"
 
 #include <linux/kvm.h>
 #include <linux/fs.h>
@@ -680,6 +681,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_USER_MEMORY:
 	case KVM_CAP_SET_TSS_ADDR:
 	case KVM_CAP_EXT_CPUID:
+	case KVM_CAP_PIT:
 		r = 1;
 		break;
 	case KVM_CAP_VAPIC:
@@ -1432,6 +1434,12 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		} else
 			goto out;
 		break;
+	case KVM_CREATE_PIT:
+		r = -ENOMEM;
+		kvm->arch.vpit = kvm_create_pit(kvm);
+		if (kvm->arch.vpit)
+			r = 0;
+		break;
 	case KVM_IRQ_LINE: {
 		struct kvm_irq_level irq_event;
 
@@ -3207,6 +3215,7 @@ static void kvm_free_vcpus(struct kvm *kvm)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
+	kvm_free_pit(kvm);
 	kfree(kvm->arch.vpic);
 	kfree(kvm->arch.vioapic);
 	kvm_free_vcpus(kvm);
include/asm-x86/kvm_host.h +1 -0
index d6db0de..66dbdd5 100644
@@ -283,6 +283,7 @@ struct kvm_arch{
 	struct list_head active_mmu_pages;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
+	struct kvm_pit *vpit;
 
 	int round_robin_prev_vcpu;
 	unsigned int tss_addr;
include/linux/kvm.h +2 -0
index 4de4fd2..2f18cfe 100644
@@ -232,6 +232,7 @@ struct kvm_vapic_addr {
 #define KVM_CAP_SET_TSS_ADDR 4
 #define KVM_CAP_EXT_CPUID 5
 #define KVM_CAP_VAPIC 6
+#define KVM_CAP_PIT 7
 
 /*
  * ioctls for VM fds
@@ -255,6 +256,7 @@ struct kvm_vapic_addr {
 #define KVM_IRQ_LINE		  _IOW(KVMIO, 0x61, struct kvm_irq_level)
 #define KVM_GET_IRQCHIP		  _IOWR(KVMIO, 0x62, struct kvm_irqchip)
 #define KVM_SET_IRQCHIP		  _IOR(KVMIO,  0x63, struct kvm_irqchip)
+#define KVM_CREATE_PIT		  _IO(KVMIO,  0x64)
 
 /*
  * ioctls for vcpu fds
Signature · 1 line
debian.1.5.3.7.1-dirty

From 5f7e9bf8856602cf8ffcb50ff744ee1d0058a850 Mon Sep 17 00:00:00 2001
From: Sheng Yang <[email protected]>
Date: Mon, 21 Jan 2008 16:41:47 +0800
Subject: [PATCH] kvm: libkvm: In-kernel PIT model

---
 kernel/Kbuild       |    2 +-
 libkvm/kvm-common.h |    2 ++
 libkvm/libkvm.c     |   20 ++++++++++++++++++++
 qemu/qemu-kvm.c     |    4 ++++
 qemu/qemu-kvm.h     |    1 +
 qemu/vl.c           |    6 ++++++
 6 files changed, 34 insertions(+), 1 deletions(-)
kernel/Kbuild +1 -1
index ed02f5a..014cc17 100644
@@ -1,7 +1,7 @@
 EXTRA_CFLAGS := -I$(src)/include -include $(src)/external-module-compat.h
 obj-m := kvm.o kvm-intel.o kvm-amd.o
 kvm-objs := kvm_main.o x86.o mmu.o x86_emulate.o anon_inodes.o irq.o i8259.o \
-	 lapic.o ioapic.o preempt.o
+	 lapic.o ioapic.o preempt.o i8254.o
 kvm-intel-objs := vmx.o vmx-debug.o
 kvm-amd-objs := svm.o
 
libkvm/kvm-common.h +2 -0
index f4040be..bd9f1de 100644
@@ -47,6 +47,8 @@ struct kvm_context {
 	int no_irqchip_creation;
 	/// in-kernel irqchip status
 	int irqchip_in_kernel;
+	/// do not create in-kernel pit if set
+	int no_pit_creation;
 };
 
 void init_slots(void);
libkvm/libkvm.c +20 -0
index 45f58d6..15e7c0d 100644
@@ -271,6 +271,11 @@ void kvm_disable_irqchip_creation(kvm_context_t kvm)
 	kvm->no_irqchip_creation = 1;
 }
 
+void kvm_disable_pit_creation(kvm_context_t kvm)
+{
+	kvm->no_pit_creation = 1;
+}
+
 int kvm_create_vcpu(kvm_context_t kvm, int slot)
 {
 	long mmap_size;
@@ -368,6 +373,20 @@ void kvm_create_irqchip(kvm_context_t kvm)
 #endif
 }
 
+void kvm_create_pit(kvm_context_t kvm)
+{
+	int r;
+
+	if (!kvm->no_pit_creation) {
+		r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_PIT);
+		if (r > 0) {
+			r = ioctl(kvm->vm_fd, KVM_CREATE_PIT);
+			if (r < 0)
+				printf("Create kernel PIC irqchip failed\n");
+		}
+	}
+}
+
 int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem)
 {
 	int r;
@@ -383,6 +402,7 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem)
 	if (r < 0)
 	        return r;
 	kvm_create_irqchip(kvm);
+	kvm_create_pit(kvm);
 	r = kvm_create_vcpu(kvm, 0);
 	if (r < 0)
 		return r;
qemu/qemu-kvm.c +4 -0
index fddbbd6..a4f4761 100644
@@ -10,6 +10,7 @@
 
 int kvm_allowed = KVM_ALLOWED_DEFAULT;
 int kvm_irqchip = 1;
+int kvm_pit = 1;
 
 #ifdef USE_KVM
 
@@ -556,6 +557,9 @@ int kvm_qemu_create_context(void)
     if (!kvm_irqchip) {
         kvm_disable_irqchip_creation(kvm_context);
     }
+    if (!kvm_pit) {
+        kvm_disable_pit_creation(kvm_context);
+    }
     if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
 	kvm_qemu_destroy();
 	return -1;
qemu/qemu-kvm.h +1 -0
index c4514bb..883a4da 100644
@@ -42,6 +42,7 @@ void kvm_arch_update_regs_for_sipi(CPUState *env);
 
 extern int kvm_allowed;
 extern int kvm_irqchip;
+extern int kvm_pit;
 
 void kvm_tpr_opt_setup(CPUState *env);
 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
qemu/vl.c +6 -0
index 756e13d..5b76c8d 100644
@@ -8015,6 +8015,7 @@ static void help(int exitcode)
 #ifdef USE_KVM
 	   "-no-kvm         disable KVM hardware virtualization\n"
 	   "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
+	   "-no-kvm-pit	    disable KVM kernel mode PIT\n"
 #endif
 #ifdef TARGET_I386
            "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
@@ -8131,6 +8132,7 @@ enum {
     QEMU_OPTION_no_acpi,
     QEMU_OPTION_no_kvm,
     QEMU_OPTION_no_kvm_irqchip,
+    QEMU_OPTION_no_kvm_pit,
     QEMU_OPTION_no_reboot,
     QEMU_OPTION_show_cursor,
     QEMU_OPTION_daemonize,
@@ -8213,6 +8215,7 @@ const QEMUOption qemu_options[] = {
 #ifdef USE_KVM
     { "no-kvm", 0, QEMU_OPTION_no_kvm },
     { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip },
+    { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit },
 #endif
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
     { "g", 1, QEMU_OPTION_g },
@@ -9046,6 +9049,9 @@ int main(int argc, char **argv)
 	    case QEMU_OPTION_no_kvm_irqchip:
 		kvm_irqchip = 0;
 		break;
+	    case QEMU_OPTION_no_kvm_pit:
+		kvm_pit = 0;
+		break;
 #endif
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
Signature · 1 line
debian.1.5.3.7.1-dirty

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel