All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org
Subject: [PATCH 23/45] KVM: protect assigned dev workqueue, int handler and irq acker
Date: Sun, 24 May 2009 18:50:08 +0300	[thread overview]
Message-ID: <1243180230-2480-24-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1243180230-2480-1-git-send-email-avi@redhat.com>

From: Marcelo Tosatti <mtosatti@redhat.com>

kvm_assigned_dev_ack_irq is vulnerable to a race condition with the
interrupt handler function. It does:

        if (dev->host_irq_disabled) {
                enable_irq(dev->host_irq);
                dev->host_irq_disabled = false;
        }

If an interrupt triggers before the host->dev_irq_disabled assignment,
it will disable the interrupt and set dev->host_irq_disabled to true.

On return to kvm_assigned_dev_ack_irq, dev->host_irq_disabled is set to
false, and the next kvm_assigned_dev_ack_irq call will fail to reenable
it.

Other than that, having the interrupt handler and work handlers run in
parallel sounds like asking for trouble (could not spot any obvious
problem, but better not have to, its fragile).

CC: sheng.yang@intel.com
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
---
 include/linux/kvm_host.h |    1 +
 virt/kvm/kvm_main.c      |   13 ++++++++++++-
 2 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 1618162..aacc544 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -345,6 +345,7 @@ struct kvm_assigned_dev_kernel {
 	int flags;
 	struct pci_dev *dev;
 	struct kvm *kvm;
+	spinlock_t assigned_dev_lock;
 };
 
 struct kvm_irq_mask_notifier {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2b73e19..4d00942 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -42,6 +42,7 @@
 #include <linux/mman.h>
 #include <linux/swap.h>
 #include <linux/bitops.h>
+#include <linux/spinlock.h>
 
 #include <asm/processor.h>
 #include <asm/io.h>
@@ -130,6 +131,7 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
 	 * finer-grained lock, update this
 	 */
 	mutex_lock(&kvm->lock);
+	spin_lock_irq(&assigned_dev->assigned_dev_lock);
 	if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
 		struct kvm_guest_msix_entry *guest_entries =
 			assigned_dev->guest_msix_entries;
@@ -156,18 +158,21 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
 		}
 	}
 
+	spin_unlock_irq(&assigned_dev->assigned_dev_lock);
 	mutex_unlock(&assigned_dev->kvm->lock);
 }
 
 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
 {
+	unsigned long flags;
 	struct kvm_assigned_dev_kernel *assigned_dev =
 		(struct kvm_assigned_dev_kernel *) dev_id;
 
+	spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
 	if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
 		int index = find_index_from_host_irq(assigned_dev, irq);
 		if (index < 0)
-			return IRQ_HANDLED;
+			goto out;
 		assigned_dev->guest_msix_entries[index].flags |=
 			KVM_ASSIGNED_MSIX_PENDING;
 	}
@@ -177,6 +182,8 @@ static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
 	disable_irq_nosync(irq);
 	assigned_dev->host_irq_disabled = true;
 
+out:
+	spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
 	return IRQ_HANDLED;
 }
 
@@ -184,6 +191,7 @@ static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 {
 	struct kvm_assigned_dev_kernel *dev;
+	unsigned long flags;
 
 	if (kian->gsi == -1)
 		return;
@@ -196,10 +204,12 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 	/* The guest irq may be shared so this ack may be
 	 * from another device.
 	 */
+	spin_lock_irqsave(&dev->assigned_dev_lock, flags);
 	if (dev->host_irq_disabled) {
 		enable_irq(dev->host_irq);
 		dev->host_irq_disabled = false;
 	}
+	spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
 }
 
 static void deassign_guest_irq(struct kvm *kvm,
@@ -615,6 +625,7 @@ static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
 	match->host_devfn = assigned_dev->devfn;
 	match->flags = assigned_dev->flags;
 	match->dev = dev;
+	spin_lock_init(&match->assigned_dev_lock);
 	match->irq_source_id = -1;
 	match->kvm = kvm;
 	match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
-- 
1.6.0.6


  parent reply	other threads:[~2009-05-24 15:57 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-24 15:49 [PATCH 00/45] KVM updates for the 2.6.31 merge window (batch 3/3) Avi Kivity
2009-05-24 15:49 ` [PATCH 01/45] KVM: Remove inject_pending_vectors() callback Avi Kivity
2009-05-24 15:49 ` [PATCH 02/45] KVM: Remove kvm_push_irq() Avi Kivity
2009-05-24 15:49 ` [PATCH 03/45] KVM: sync_lapic_to_cr8() should always sync cr8 to V_TPR Avi Kivity
2009-05-24 15:49 ` [PATCH 04/45] KVM: Do not report TPR write to userspace if new value bigger or equal to a previous one Avi Kivity
2009-05-24 15:49 ` [PATCH 05/45] KVM: Get rid of arch.interrupt_window_open & arch.nmi_window_open Avi Kivity
2009-05-24 15:49 ` [PATCH 06/45] KVM: SVM: Add NMI injection support Avi Kivity
2009-05-24 15:49 ` [PATCH 07/45] KVM: Fix userspace IRQ chip migration Avi Kivity
2009-05-24 15:49 ` [PATCH 08/45] KVM: Get rid of get_irq() callback Avi Kivity
2009-05-24 15:49 ` [PATCH 09/45] KVM: Wake up waitqueue before calling get_cpu() Avi Kivity
2009-05-24 15:49 ` [PATCH 10/45] KVM: Replace get_mt_mask_shift with get_mt_mask Avi Kivity
2009-05-24 15:49 ` [PATCH 11/45] KVM: Enable snooping control for supported hardware Avi Kivity
2009-05-24 15:49 ` [PATCH 12/45] KVM: VMX: Disable VMX when system shutdown Avi Kivity
2009-05-24 15:49 ` [PATCH 13/45] KVM: Trivial format fix in setup_routing_entry() Avi Kivity
2009-05-24 15:49 ` [PATCH 14/45] KVM: fix apic_debug instances Avi Kivity
2009-05-24 15:50 ` [PATCH 15/45] KVM: SVM: Fix cross vendor migration issue in segment segment descriptor Avi Kivity
2009-05-24 15:50 ` [PATCH 16/45] KVM: SVM: Don't reinject event that caused a task switch Avi Kivity
2009-05-24 15:50 ` [PATCH 17/45] KVM: Drop request_nmi from stats Avi Kivity
2009-05-24 15:50 ` [PATCH 18/45] KVM: Fix cpuid feature misreporting Avi Kivity
2009-05-24 15:50 ` [PATCH 19/45] KVM: Add AMD cpuid bit: cr8_legacy, abm, misaligned sse, sse4, 3dnow prefetch Avi Kivity
2009-05-24 15:50 ` [PATCH 20/45] x86: Add cpu features MOVBE and POPCNT Avi Kivity
2009-05-24 15:50 ` [PATCH 21/45] KVM: Update cpuid 1.ecx reporting Avi Kivity
2009-05-24 15:50 ` [PATCH 22/45] KVM: use smp_send_reschedule in kvm_vcpu_kick Avi Kivity
2009-05-24 15:50 ` Avi Kivity [this message]
2009-05-24 15:50 ` [PATCH 24/45] KVM: Replace ->drop_interrupt_shadow() by ->set_interrupt_shadow() Avi Kivity
2009-05-24 15:50 ` [PATCH 25/45] KVM: Deal with interrupt shadow state for emulated instructions Avi Kivity
2009-05-24 15:50 ` [PATCH 26/45] KVM: MMU: protect kvm_mmu_change_mmu_pages with mmu_lock Avi Kivity
2009-05-24 15:50 ` [PATCH 27/45] KVM: take mmu_lock when updating a deleted slot Avi Kivity
2009-05-24 15:50 ` [PATCH 28/45] KVM: x86: check for cr3 validity in mmu_alloc_roots Avi Kivity
2009-05-24 15:50 ` [PATCH 29/45] KVM: Expand on "help" info to specify kvm intel and amd module names Avi Kivity
2009-05-24 15:50 ` [PATCH 30/45] KVM: s390: Fix memory slot versus run - v3 Avi Kivity
2009-05-24 15:50 ` [PATCH 31/45] KVM: s390: use hrtimer for clock wakeup from idle - v2 Avi Kivity
2009-05-24 15:50 ` [PATCH 32/45] KVM: s390: optimize float int lock: spin_lock_bh --> spin_lock Avi Kivity
2009-05-24 15:50 ` [PATCH 33/45] KVM: s390: Unlink vcpu on destroy - v2 Avi Kivity
2009-05-24 15:50 ` [PATCH 34/45] KVM: s390: Sanity check on validity intercept Avi Kivity
2009-05-24 15:50 ` [PATCH 35/45] KVM: s390: Verify memory in kvm run Avi Kivity
2009-05-24 15:50 ` [PATCH 36/45] KVM: Unprotect a page if #PF happens during NMI injection Avi Kivity
2009-05-24 15:50 ` [PATCH 37/45] KVM: Do not allow interrupt injection from userspace if there is a pending event Avi Kivity
2009-05-24 15:50 ` [PATCH 38/45] KVM: Remove irq_pending bitmap Avi Kivity
2009-05-24 15:50 ` [PATCH 39/45] KVM: skip_emulated_instruction() decode instruction if size is not known Avi Kivity
2009-05-24 15:50 ` [PATCH 40/45] KVM: Do not re-execute INTn instruction Avi Kivity
2009-05-24 15:50 ` [PATCH 41/45] KVM: Always request IRQ/NMI window if an interrupt is pending Avi Kivity
2009-05-24 15:50 ` [PATCH 42/45] KVM: inject NMI after IRET from a previous NMI, not before Avi Kivity
2009-05-24 15:50 ` [PATCH 43/45] KVM: Do not migrate pending software interrupts Avi Kivity
2009-05-24 15:50 ` [PATCH 44/45] KVM: Disable CR8 intercept if tpr patching is active Avi Kivity
2009-05-24 15:50 ` [PATCH 45/45] KVM: Move "exit due to NMI" handling into vmx_complete_interrupts() 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=1243180230-2480-24-git-send-email-avi@redhat.com \
    --to=avi@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.