All of lore.kernel.org
 help / color / mirror / Atom feed
From: Feng Wu <feng.wu@intel.com>
To: pbonzini@redhat.com, alex.williamson@redhat.com, joro@8bytes.org,
	mtosatti@redhat.com
Cc: eric.auger@linaro.org, kvm@vger.kernel.org,
	iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org,
	Feng Wu <feng.wu@intel.com>
Subject: [PATCH v8 10/13] KVM: Update Posted-Interrupts Descriptor when vCPU is preempted
Date: Wed, 16 Sep 2015 16:50:06 +0800	[thread overview]
Message-ID: <1442393409-2623-11-git-send-email-feng.wu@intel.com> (raw)
In-Reply-To: <1442393409-2623-1-git-send-email-feng.wu@intel.com>

This patch updates the Posted-Interrupts Descriptor when vCPU
is preempted.

sched out:
- Set 'SN' to suppress furture non-urgent interrupts posted for
the vCPU.

sched in:
- Clear 'SN'
- Change NDST if vCPU is scheduled to a different CPU
- Set 'NV' to POSTED_INTR_VECTOR

Signed-off-by: Feng Wu <feng.wu@intel.com>
---
v8:
- Add two wrapper fucntion vmx_vcpu_pi_load() and vmx_vcpu_pi_put().
- Only handle VT-d PI related logic when the VM has assigned devices.

 arch/x86/kvm/vmx.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 5a25651..5ceb280 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -1943,6 +1943,52 @@ static void vmx_load_host_state(struct vcpu_vmx *vmx)
 	preempt_enable();
 }
 
+static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+	struct pi_desc old, new;
+	unsigned int dest;
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	do {
+		old.control = new.control = pi_desc->control;
+
+		/*
+		 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
+		 * are two possible cases:
+		 * 1. After running 'pi_pre_block', context switch
+		 *    happened. For this case, 'sn' was set in
+		 *    vmx_vcpu_put(), so we need to clear it here.
+		 * 2. After running 'pi_pre_block', we were blocked,
+		 *    and woken up by some other guy. For this case,
+		 *    we don't need to do anything, 'pi_post_block'
+		 *    will do everything for us. However, we cannot
+		 *    check whether it is case #1 or case #2 here
+		 *    (maybe, not needed), so we also clear sn here,
+		 *    I think it is not a big deal.
+		 */
+		if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
+			if (vcpu->cpu != cpu) {
+				dest = cpu_physical_id(cpu);
+
+				if (x2apic_enabled())
+					new.ndst = dest;
+				else
+					new.ndst = (dest << 8) & 0xFF00;
+			}
+
+			/* set 'NV' to 'notification vector' */
+			new.nv = POSTED_INTR_VECTOR;
+		}
+
+		/* Allow posting non-urgent interrupts */
+		new.sn = 0;
+	} while (cmpxchg(&pi_desc->control, old.control,
+			new.control) != old.control);
+}
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  * vcpu mutex is already taken.
@@ -1993,10 +2039,27 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
 		vmx->loaded_vmcs->cpu = cpu;
 	}
+
+	vmx_vcpu_pi_load(vcpu, cpu);
+}
+
+static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	/* Set SN when the vCPU is preempted */
+	if (vcpu->preempted)
+		pi_set_sn(pi_desc);
 }
 
 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
 {
+	vmx_vcpu_pi_put(vcpu);
+
 	__vmx_load_host_state(to_vmx(vcpu));
 	if (!vmm_exclusive) {
 		__loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
-- 
2.1.0


WARNING: multiple messages have this Message-ID (diff)
From: Feng Wu <feng.wu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
To: pbonzini-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	alex.williamson-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org,
	mtosatti-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Cc: iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	kvm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	eric.auger-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org
Subject: [PATCH v8 10/13] KVM: Update Posted-Interrupts Descriptor when vCPU is preempted
Date: Wed, 16 Sep 2015 16:50:06 +0800	[thread overview]
Message-ID: <1442393409-2623-11-git-send-email-feng.wu@intel.com> (raw)
In-Reply-To: <1442393409-2623-1-git-send-email-feng.wu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>

This patch updates the Posted-Interrupts Descriptor when vCPU
is preempted.

sched out:
- Set 'SN' to suppress furture non-urgent interrupts posted for
the vCPU.

sched in:
- Clear 'SN'
- Change NDST if vCPU is scheduled to a different CPU
- Set 'NV' to POSTED_INTR_VECTOR

Signed-off-by: Feng Wu <feng.wu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
---
v8:
- Add two wrapper fucntion vmx_vcpu_pi_load() and vmx_vcpu_pi_put().
- Only handle VT-d PI related logic when the VM has assigned devices.

 arch/x86/kvm/vmx.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 5a25651..5ceb280 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -1943,6 +1943,52 @@ static void vmx_load_host_state(struct vcpu_vmx *vmx)
 	preempt_enable();
 }
 
+static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+	struct pi_desc old, new;
+	unsigned int dest;
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	do {
+		old.control = new.control = pi_desc->control;
+
+		/*
+		 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
+		 * are two possible cases:
+		 * 1. After running 'pi_pre_block', context switch
+		 *    happened. For this case, 'sn' was set in
+		 *    vmx_vcpu_put(), so we need to clear it here.
+		 * 2. After running 'pi_pre_block', we were blocked,
+		 *    and woken up by some other guy. For this case,
+		 *    we don't need to do anything, 'pi_post_block'
+		 *    will do everything for us. However, we cannot
+		 *    check whether it is case #1 or case #2 here
+		 *    (maybe, not needed), so we also clear sn here,
+		 *    I think it is not a big deal.
+		 */
+		if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
+			if (vcpu->cpu != cpu) {
+				dest = cpu_physical_id(cpu);
+
+				if (x2apic_enabled())
+					new.ndst = dest;
+				else
+					new.ndst = (dest << 8) & 0xFF00;
+			}
+
+			/* set 'NV' to 'notification vector' */
+			new.nv = POSTED_INTR_VECTOR;
+		}
+
+		/* Allow posting non-urgent interrupts */
+		new.sn = 0;
+	} while (cmpxchg(&pi_desc->control, old.control,
+			new.control) != old.control);
+}
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  * vcpu mutex is already taken.
@@ -1993,10 +2039,27 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
 		vmx->loaded_vmcs->cpu = cpu;
 	}
+
+	vmx_vcpu_pi_load(vcpu, cpu);
+}
+
+static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	/* Set SN when the vCPU is preempted */
+	if (vcpu->preempted)
+		pi_set_sn(pi_desc);
 }
 
 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
 {
+	vmx_vcpu_pi_put(vcpu);
+
 	__vmx_load_host_state(to_vmx(vcpu));
 	if (!vmm_exclusive) {
 		__loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
-- 
2.1.0

  parent reply	other threads:[~2015-09-16  9:04 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-16  8:49 [PATCH v8 00/13] Add VT-d Posted-Interrupts support Feng Wu
2015-09-16  8:49 ` Feng Wu
2015-09-16  8:49 ` [PATCH v8 01/13] KVM: Extend struct pi_desc for VT-d Posted-Interrupts Feng Wu
2015-09-16  8:49   ` Feng Wu
2015-09-16  8:49 ` [PATCH v8 02/13] KVM: Add some helper functions for Posted-Interrupts Feng Wu
2015-09-16  8:49   ` Feng Wu
2015-09-16  8:49 ` [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu() Feng Wu
2015-09-16  8:49   ` Feng Wu
2015-09-16  9:23   ` Paolo Bonzini
2015-09-16  9:23     ` Paolo Bonzini
2015-09-17  3:17     ` Wu, Feng
2015-09-17  3:17       ` Wu, Feng
2015-09-17  9:42       ` Paolo Bonzini
2015-09-17 13:36         ` Wu, Feng
2015-09-17 13:36           ` Wu, Feng
2015-09-17 14:24           ` Paolo Bonzini
2015-09-17 14:24             ` Paolo Bonzini
2015-09-17 15:58             ` Radim Krčmář
2015-09-17 16:00               ` Paolo Bonzini
2015-09-17 16:00                 ` Paolo Bonzini
2015-09-17 23:18                 ` Wu, Feng
2015-09-17 23:18                   ` Wu, Feng
2015-09-18 16:16                   ` Radim Krčmář
2015-09-18 16:16                     ` Radim Krčmář
2015-09-18 16:17                     ` Paolo Bonzini
2015-09-18 16:17                       ` Paolo Bonzini
2015-09-17 23:15             ` Wu, Feng
2015-09-17 23:15               ` Wu, Feng
2015-09-16  8:50 ` [PATCH v8 04/13] KVM: Make struct kvm_irq_routing_table accessible Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  8:50 ` [PATCH v8 05/13] KVM: make kvm_set_msi_irq() public Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  8:50 ` [PATCH v8 06/13] vfio: Register/unregister irq_bypass_producer Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  8:50 ` [PATCH v8 07/13] KVM: x86: Update IRTE for posted-interrupts Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  8:50 ` [PATCH v8 08/13] KVM: Implement IRQ bypass consumer callbacks for x86 Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  8:50 ` [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd' Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  9:27   ` Paolo Bonzini
2015-09-16  9:27     ` Paolo Bonzini
2015-09-17  1:51     ` Wu, Feng
2015-09-17  1:51       ` Wu, Feng
2015-09-17  9:38       ` Paolo Bonzini
2015-09-17  9:38         ` Paolo Bonzini
2015-09-16  8:50 ` Feng Wu [this message]
2015-09-16  8:50   ` [PATCH v8 10/13] KVM: Update Posted-Interrupts Descriptor when vCPU is preempted Feng Wu
2015-09-16  9:29   ` Paolo Bonzini
2015-09-16  9:29     ` Paolo Bonzini
2015-09-16  8:50 ` [PATCH v8 11/13] KVM: Update Posted-Interrupts Descriptor when vCPU is blocked Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  9:32   ` Paolo Bonzini
2015-09-16  9:32     ` Paolo Bonzini
2015-09-16  8:50 ` [PATCH v8 12/13] KVM: Warn if 'SN' is set during posting interrupts by software Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  9:32   ` Paolo Bonzini
2015-09-16  9:32     ` Paolo Bonzini
2015-09-16  8:50 ` [PATCH v8 13/13] iommu/vt-d: Add a command line parameter for VT-d posted-interrupts Feng Wu
2015-09-16  8:50   ` Feng Wu
2015-09-16  9:34 ` [PATCH v8 00/13] Add VT-d Posted-Interrupts support Paolo Bonzini
2015-09-16  9:34   ` Paolo Bonzini

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=1442393409-2623-11-git-send-email-feng.wu@intel.com \
    --to=feng.wu@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=eric.auger@linaro.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    /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.