mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, Amit Shah <amit.shah@redhat.com>
Subject: [PATCH 21/25] KVM: Device Assignment: Free device structures if IRQ allocation fails
Date: Sun, 28 Sep 2008 18:44:57 +0300	[thread overview]
Message-ID: <1222616701-1206-22-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1222616701-1206-1-git-send-email-avi@redhat.com>

From: Amit Shah <amit.shah@redhat.com>

When an IRQ allocation fails, we free up the device structures and
disable the device so that we can unregister the device in the
userspace and not expose it to the guest at all.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/x86.c |   86 +++++++++++++++++++++++++++-------------------------
 1 files changed, 45 insertions(+), 41 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c8a2793..61eddbe 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -166,6 +166,43 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 	enable_irq(dev->host_irq);
 }
 
+static void kvm_free_assigned_device(struct kvm *kvm,
+				     struct kvm_assigned_dev_kernel
+				     *assigned_dev)
+{
+	if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
+		free_irq(assigned_dev->host_irq, (void *)assigned_dev);
+
+	kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
+
+	if (cancel_work_sync(&assigned_dev->interrupt_work))
+		/* We had pending work. That means we will have to take
+		 * care of kvm_put_kvm.
+		 */
+		kvm_put_kvm(kvm);
+
+	pci_release_regions(assigned_dev->dev);
+	pci_disable_device(assigned_dev->dev);
+	pci_dev_put(assigned_dev->dev);
+
+	list_del(&assigned_dev->list);
+	kfree(assigned_dev);
+}
+
+static void kvm_free_all_assigned_devices(struct kvm *kvm)
+{
+	struct list_head *ptr, *ptr2;
+	struct kvm_assigned_dev_kernel *assigned_dev;
+
+	list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
+		assigned_dev = list_entry(ptr,
+					  struct kvm_assigned_dev_kernel,
+					  list);
+
+		kvm_free_assigned_device(kvm, assigned_dev);
+	}
+}
+
 static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
 				   struct kvm_assigned_irq
 				   *assigned_irq)
@@ -194,8 +231,8 @@ static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
 
 	if (irqchip_in_kernel(kvm)) {
 		if (!capable(CAP_SYS_RAWIO)) {
-			return -EPERM;
-			goto out;
+			r = -EPERM;
+			goto out_release;
 		}
 
 		if (assigned_irq->host_irq)
@@ -214,17 +251,18 @@ static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
 		 */
 		if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
 				"kvm_assigned_device", (void *)match)) {
-			printk(KERN_INFO "%s: couldn't allocate irq for pv "
-			       "device\n", __func__);
 			r = -EIO;
-			goto out;
+			goto out_release;
 		}
 	}
 
 	match->irq_requested = true;
-out:
 	mutex_unlock(&kvm->lock);
 	return r;
+out_release:
+	mutex_unlock(&kvm->lock);
+	kvm_free_assigned_device(kvm, match);
+	return r;
 }
 
 static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
@@ -300,40 +338,6 @@ out_free:
 	return r;
 }
 
-static void kvm_free_assigned_devices(struct kvm *kvm)
-{
-	struct list_head *ptr, *ptr2;
-	struct kvm_assigned_dev_kernel *assigned_dev;
-
-	list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
-		assigned_dev = list_entry(ptr,
-					  struct kvm_assigned_dev_kernel,
-					  list);
-
-		if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested) {
-			free_irq(assigned_dev->host_irq,
-				 (void *)assigned_dev);
-
-			kvm_unregister_irq_ack_notifier(kvm,
-							&assigned_dev->
-							ack_notifier);
-		}
-
-		if (cancel_work_sync(&assigned_dev->interrupt_work))
-			/* We had pending work. That means we will have to take
-			 * care of kvm_put_kvm.
-			 */
-			kvm_put_kvm(kvm);
-
-		pci_release_regions(assigned_dev->dev);
-		pci_disable_device(assigned_dev->dev);
-		pci_dev_put(assigned_dev->dev);
-
-		list_del(&assigned_dev->list);
-		kfree(assigned_dev);
-	}
-}
-
 unsigned long segment_base(u16 selector)
 {
 	struct descriptor_table gdt;
@@ -4296,7 +4300,7 @@ static void kvm_free_vcpus(struct kvm *kvm)
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
 	kvm_iommu_unmap_guest(kvm);
-	kvm_free_assigned_devices(kvm);
+	kvm_free_all_assigned_devices(kvm);
 	kvm_free_pit(kvm);
 	kfree(kvm->arch.vpic);
 	kfree(kvm->arch.vioapic);
-- 
1.6.0.1


  parent reply	other threads:[~2008-09-28 15:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-28 15:44 [PATCH 00/25] KVM Updates for 2.6.28 merge window (part 3 of 3) Avi Kivity
2008-09-28 15:44 ` [PATCH 01/25] KVM: x86 emulator: Add std and cld instructions (opcodes 0xfc-0xfd) Avi Kivity
2008-09-28 15:44 ` [PATCH 02/25] KVM: ia64: Implement kvm_arch_vcpu_ioctl_{set,get}_mpstate Avi Kivity
2008-09-28 15:44 ` [PATCH 03/25] KVM: MMU: Modify kvm_shadow_walk.entry to accept u64 addr Avi Kivity
2008-09-28 15:44 ` [PATCH 04/25] KVM: Add statistics for guest irq injections Avi Kivity
2008-09-28 15:44 ` [PATCH 05/25] KVM: x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef) Avi Kivity
2008-09-28 15:44 ` [PATCH 06/25] KVM: x86: do not execute halted vcpus Avi Kivity
2008-09-28 15:44 ` [PATCH 07/25] KVM: x86 emulator: Add call near absolute instruction (opcode 0xff/2) Avi Kivity
2008-09-28 15:44 ` [PATCH 08/25] KVM: x86: unhalt vcpu0 on reset Avi Kivity
2008-09-28 15:44 ` [PATCH 09/25] KVM: ppc: kvmppc_44x_shadow_release() does not require mmap_sem to be locked Avi Kivity
2008-09-28 15:44 ` [PATCH 10/25] KVM: VMX: Cleanup stalled INTR_INFO read Avi Kivity
2008-09-28 15:44 ` [PATCH 11/25] KVM: x86 emulator: fix jmp r/m64 instruction Avi Kivity
2008-09-28 15:44 ` [PATCH 12/25] add MAINTAINERS entry for the KVM AMD module Avi Kivity
2008-09-28 15:44 ` [PATCH 13/25] x86: Move VMX MSRs to msr-index.h Avi Kivity
2008-09-28 15:44 ` [PATCH 14/25] KVM: VMX: Rename IA32_FEATURE_CONTROL bits Avi Kivity
2008-09-28 15:44 ` [PATCH 15/25] x86: Move FEATURE_CONTROL bits to msr-index.h Avi Kivity
2008-09-28 15:44 ` [PATCH 16/25] KVM: x86 emulator: Add DstAcc operand type Avi Kivity
2008-09-28 15:44 ` [PATCH 17/25] KVM: x86 emulator: Add cmp al, imm and cmp ax, imm instructions (ocodes 3c, 3d) Avi Kivity
2008-09-28 15:44 ` [PATCH 18/25] KVM: x86 emulator: Use DstAcc for 'and' Avi Kivity
2008-09-28 15:44 ` [PATCH 19/25] VT-d: Changes to support KVM Avi Kivity
2008-09-28 15:44 ` [PATCH 20/25] KVM: Device Assignment with VT-d Avi Kivity
2008-09-28 15:44 ` Avi Kivity [this message]
2008-09-28 15:44 ` [PATCH 22/25] KVM: opencode gfn_to_page in kvm_vm_fault Avi Kivity
2008-09-28 15:44 ` [PATCH 23/25] KVM: switch to get_user_pages_fast Avi Kivity
2008-09-28 15:45 ` [PATCH 24/25] KVM: x86.c make kvm_load_realmode_segment static Avi Kivity
2008-09-28 15:45 ` [PATCH 25/25] MAINTAINERS: Update Avi Kivity's email address Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1222616701-1206-22-git-send-email-avi@redhat.com \
    --to=avi@redhat.com \
    --cc=amit.shah@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome