Sophie

Sophie

distrib > Scientific%20Linux > 5x > x86_64 > by-pkgid > 340e01248478ba8b78a6d4d1809b1eff > files > 437

kvm-83-270.el5_11.src.rpm

From 5ce625429daf15ceccbf98ed505f5d9f9a710c13 Mon Sep 17 00:00:00 2001
Message-Id: <5ce625429daf15ceccbf98ed505f5d9f9a710c13.1334057689.git.minovotn@redhat.com>
From: Marcelo Tosatti <mtosatti@redhat.com>
Date: Wed, 28 Mar 2012 23:38:47 +0200
Subject: [PATCH] KVM: Ensure all vcpus are consistent with in-kernel irqchip
 settings

RH-Author: Marcelo Tosatti <mtosatti@redhat.com>
Message-id: <20120328233846.GA28225@amt.cnet>
Patchwork-id: 39022
O-Subject: [RHEL 5.9 5.8.z KVM PATCH] KVM: Ensure all vcpus are consistent with in-kernel irqchip settings
Bugzilla: 807854
RH-Acked-by: Gleb Natapov <gleb@redhat.com>
RH-Acked-by: Avi Kivity <avi@redhat.com>
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>

(upstream kvm.git commit 5b40572ed5f0344b9dbee486a17c589ce1abe1a3)

If some vcpus are created before KVM_CREATE_IRQCHIP, then
irqchip_in_kernel() and vcpu->arch.apic will be inconsistent, leading
to potential NULL pointer dereferences.

Fix by:
- ensuring that no vcpus are installed when KVM_CREATE_IRQCHIP is called
- ensuring that a vcpu has an apic if it is installed after KVM_CREATE_IRQCHIP

This is somewhat long winded because vcpu->arch.apic is created without
kvm->lock held.

Based on earlier patch by Michael Ellerman.

Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
Signed-off-by: Avi Kivity <avi@redhat.com>

BZ: 807854
Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 arch/x86/kvm/x86.c       |   22 +++++++++++++++++++++-
 include/linux/kvm_host.h |    4 ++++
 virt/kvm/kvm_main.c      |    4 ++++
 3 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7522330..fe0b2dc 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2007,7 +2007,21 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		if (r)
 			goto out;
 		break;
-	case KVM_CREATE_IRQCHIP:
+	case KVM_CREATE_IRQCHIP: {
+		int i;
+		struct kvm_vcpu *vcpu;
+		r = -EINVAL;
+
+		mutex_lock(&kvm->lock);
+		for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+			vcpu = kvm->vcpus[i];
+			if (vcpu) {
+				mutex_unlock(&kvm->lock);
+				goto out;
+			}
+		}
+		mutex_unlock(&kvm->lock);
+		
 		r = -ENOMEM;
 		kvm->arch.vpic = kvm_create_pic(kvm);
 		if (kvm->arch.vpic) {
@@ -2026,6 +2040,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			goto out;
 		}
 		break;
+	}
 	case KVM_CREATE_PIT:
 		mutex_lock(&kvm->lock);
 		r = -EEXIST;
@@ -4505,6 +4520,11 @@ void kvm_arch_check_processor_compat(void *rtn)
 	kvm_x86_ops->check_processor_compatibility(rtn);
 }
 
+bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
+}
+
 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 {
 	struct page *page;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 2d3afd2..419345d 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -521,8 +521,12 @@ int kvm_set_irq_routing(struct kvm *kvm,
 			unsigned flags);
 void kvm_free_irq_routing(struct kvm *kvm);
 
+bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
+
 #else
 
+static bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
+
 static inline void kvm_free_irq_routing(struct kvm *kvm) {}
 
 #endif
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 3131aac..5ac50a3 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1814,6 +1814,10 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
 		return r;
 
 	mutex_lock(&kvm->lock);
+	if (!kvm_vcpu_compatible(vcpu)) {
+		r = -EINVAL;
+		goto vcpu_destroy;
+	}
 	if (kvm->vcpus[n]) {
 		r = -EEXIST;
 		goto vcpu_destroy;
-- 
1.7.7.6