All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gleb Natapov <gleb@redhat.com>
To: Yang Zhang <yang.z.zhang@intel.com>
Cc: kvm@vger.kernel.org, mtosatti@redhat.com,
	xiantao.zhang@intel.com, jun.nakajima@intel.com
Subject: Re: [PATCH v7 6/7] KVM: VMX: Add the algorithm of deliver posted interrupt
Date: Sun, 7 Apr 2013 17:55:56 +0300	[thread overview]
Message-ID: <20130407145556.GA17919@redhat.com> (raw)
In-Reply-To: <1364787155-3208-7-git-send-email-yang.z.zhang@intel.com>

On Mon, Apr 01, 2013 at 11:32:34AM +0800, Yang Zhang wrote:
> From: Yang Zhang <yang.z.zhang@Intel.com>
> 
> Only deliver the posted interrupt when target vcpu is running
> and there is no previous interrupt pending in pir.
> 
> Signed-off-by: Yang Zhang <yang.z.zhang@Intel.com>
> ---
>  arch/x86/include/asm/kvm_host.h |    2 +
>  arch/x86/kvm/lapic.c            |   13 ++++++++
>  arch/x86/kvm/lapic.h            |    1 +
>  arch/x86/kvm/svm.c              |    6 ++++
>  arch/x86/kvm/vmx.c              |   60 ++++++++++++++++++++++++++++++++++++++-
>  virt/kvm/kvm_main.c             |    1 +
>  6 files changed, 82 insertions(+), 1 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 8e95512..842ea5a 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -704,6 +704,8 @@ struct kvm_x86_ops {
>  	void (*hwapic_isr_update)(struct kvm *kvm, int isr);
>  	void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
>  	void (*set_virtual_x2apic_mode)(struct kvm_vcpu *vcpu, bool set);
> +	void (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu, int vector);
> +	void (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);
>  	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
>  	int (*get_tdp_level)(void);
>  	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 686afee..95e8f4a 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -310,6 +310,19 @@ static u8 count_vectors(void *bitmap)
>  	return count;
>  }
>  
> +void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
> +{
> +	u32 i, pir_val;
> +	struct kvm_lapic *apic = vcpu->arch.apic;
> +
> +	for (i = 0; i <= 7; i++) {
> +		pir_val = xchg(&pir[i], 0);
> +		if (pir_val)
> +			*((u32 *)(apic->regs + APIC_IRR + i * 0x10)) |= pir_val;
> +	}
> +}
> +EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
> +
>  static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
>  {
>  	apic->irr_pending = true;
> diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
> index 599076e..16c3949 100644
> --- a/arch/x86/kvm/lapic.h
> +++ b/arch/x86/kvm/lapic.h
> @@ -54,6 +54,7 @@ u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
>  void kvm_apic_set_version(struct kvm_vcpu *vcpu);
>  
>  void kvm_apic_update_tmr(struct kvm_vcpu *vcpu, u32 *tmr);
> +void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir);
>  int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
>  int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
>  int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> index 2f8fe3f..d6713e1 100644
> --- a/arch/x86/kvm/svm.c
> +++ b/arch/x86/kvm/svm.c
> @@ -3577,6 +3577,11 @@ static void svm_hwapic_isr_update(struct kvm *kvm, int isr)
>  	return;
>  }
>  
> +static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
> +{
> +	return;
> +}
> +
>  static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
>  {
>  	struct vcpu_svm *svm = to_svm(vcpu);
> @@ -4305,6 +4310,7 @@ static struct kvm_x86_ops svm_x86_ops = {
>  	.vm_has_apicv = svm_vm_has_apicv,
>  	.load_eoi_exitmap = svm_load_eoi_exitmap,
>  	.hwapic_isr_update = svm_hwapic_isr_update,
> +	.sync_pir_to_irr = svm_sync_pir_to_irr,
>  
>  	.set_tss_addr = svm_set_tss_addr,
>  	.get_tdp_level = get_npt_level,
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index edfc87a..690734c 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -380,6 +380,23 @@ struct pi_desc {
>  	} u;
>  } __aligned(64);
>  
> +static bool pi_test_and_set_on(struct pi_desc *pi_desc)
> +{
> +	return test_and_set_bit(POSTED_INTR_ON,
> +			(unsigned long *)&pi_desc->u.control);
> +}
> +
> +static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
> +{
> +	return test_and_clear_bit(POSTED_INTR_ON,
> +			(unsigned long *)&pi_desc->u.control);
> +}
> +
> +static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
> +{
> +	return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
> +}
> +
>  struct vcpu_vmx {
>  	struct kvm_vcpu       vcpu;
>  	unsigned long         host_rsp;
> @@ -2851,8 +2868,10 @@ static __init int hardware_setup(void)
>  
>  	if (enable_apicv)
>  		kvm_x86_ops->update_cr8_intercept = NULL;
> -	else
> +	else {
>  		kvm_x86_ops->hwapic_irr_update = NULL;
> +		kvm_x86_ops->deliver_posted_interrupt = NULL;
> +	}
>  
>  	if (nested)
>  		nested_vmx_setup_ctls_msrs();
> @@ -3914,6 +3933,43 @@ static int vmx_vm_has_apicv(struct kvm *kvm)
>  }
>  
>  /*
> + * Send interrupt to vcpu via posted interrupt way.
> + * 1. If target vcpu is running(non-root mode), send posted interrupt
> + * notification to vcpu and hardware will sync PIR to vIRR atomically.
> + * 2. If target vcpu isn't running(root mode), kick it to pick up the
> + * interrupt from PIR in next vmentry.
> + */
> +static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
> +{
> +	struct vcpu_vmx *vmx = to_vmx(vcpu);
> +	int r;
> +
> +	if (pi_test_and_set_pir(vector, &vmx->pi_desc))
> +		return;
> +
> +	r = pi_test_and_set_on(&vmx->pi_desc);
> +	kvm_make_request(KVM_REQ_EVENT, vcpu);
> +	if (!r && (vcpu->mode == IN_GUEST_MODE)) {
> +		apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
> +				POSTED_INTR_VECTOR);
> +	} else
> +		kvm_vcpu_kick(vcpu);
> +
> +	return;
> +}
> +
> +static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
> +{
> +	struct vcpu_vmx *vmx = to_vmx(vcpu);
> +
> +	if (!vmx_vm_has_apicv(vcpu->kvm) ||
If we set kvm_ops->sync_pir_to_irr() to a function that does nothing if
apicv is disabled we can drop this fast path check here.

> +			!pi_test_and_clear_on(&vmx->pi_desc))
> +		return;
> +
> +	kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
> +}
> +
> +/*
>   * Set up the vmcs's constant host-state fields, i.e., host-state fields that
>   * will not change in the lifetime of the guest.
>   * Note that host-state that does change is set elsewhere. E.g., host-state
> @@ -7756,6 +7812,8 @@ static struct kvm_x86_ops vmx_x86_ops = {
>  	.load_eoi_exitmap = vmx_load_eoi_exitmap,
>  	.hwapic_irr_update = vmx_hwapic_irr_update,
>  	.hwapic_isr_update = vmx_hwapic_isr_update,
> +	.sync_pir_to_irr = vmx_sync_pir_to_irr,
> +	.deliver_posted_interrupt = vmx_deliver_posted_interrupt,
>  
>  	.set_tss_addr = vmx_set_tss_addr,
>  	.get_tdp_level = get_ept_level,
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 2d44013..8ad1799 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1671,6 +1671,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
>  			smp_send_reschedule(cpu);
>  	put_cpu();
>  }
> +EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
>  #endif /* !CONFIG_S390 */
>  
>  void kvm_resched(struct kvm_vcpu *vcpu)
> -- 
> 1.7.1

--
			Gleb.

  reply	other threads:[~2013-04-07 14:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-01  3:32 [PATCH v7 0/7] KVM: VMX: Add Posted Interrupt supporting Yang Zhang
2013-04-01  3:32 ` [PATCH v7 1/7] KVM: VMX: Enable acknowledge interupt on vmexit Yang Zhang
2013-04-01  3:32 ` [PATCH v7 2/7] KVM: VMX: Register a new IPI for posted interrupt Yang Zhang
2013-04-01  3:32 ` [PATCH v7 3/7] KVM: VMX: Check the posted interrupt capability Yang Zhang
2013-04-07 14:14   ` Gleb Natapov
2013-04-08  2:57     ` Zhang, Yang Z
2013-04-01  3:32 ` [PATCH v7 4/7] KVM: Call common update function when ioapic entry changed Yang Zhang
2013-04-07 13:33   ` Gleb Natapov
2013-04-07 14:00     ` Zhang, Yang Z
2013-04-07 14:05       ` Gleb Natapov
2013-04-08  4:10         ` Zhang, Yang Z
2013-04-01  3:32 ` [PATCH v7 5/7] KVM: Set TMR when programming ioapic entry Yang Zhang
2013-04-07 14:10   ` Gleb Natapov
2013-04-01  3:32 ` [PATCH v7 6/7] KVM: VMX: Add the algorithm of deliver posted interrupt Yang Zhang
2013-04-07 14:55   ` Gleb Natapov [this message]
2013-04-08  5:38     ` Zhang, Yang Z
2013-04-01  3:32 ` [PATCH v7 7/7] KVM: VMX: Use posted interrupt to deliver virtual interrupt Yang Zhang

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=20130407145556.GA17919@redhat.com \
    --to=gleb@redhat.com \
    --cc=jun.nakajima@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=xiantao.zhang@intel.com \
    --cc=yang.z.zhang@intel.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.