public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: "Radim Krčmář" <rkrcmar@redhat.com>, linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, Yuki Shibuya <shibuya.yk@ncos.nec.co.jp>
Subject: Re: [PATCH 2/4] KVM: x86: refactor PIT state inject_lock
Date: Wed, 3 Feb 2016 17:45:09 +0100	[thread overview]
Message-ID: <56B22E95.30504@redhat.com> (raw)
In-Reply-To: <1454516585-28491-3-git-send-email-rkrcmar@redhat.com>



On 03/02/2016 17:23, Radim Krčmář wrote:
> Following patches would be even uglier if inject_lock didn't go away.
> 
> Patch changes the virtual wire comment to better describe our situation.
> 
> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
> ---
>  arch/x86/kvm/i8254.c | 67 ++++++++++++++++++++++------------------------------
>  arch/x86/kvm/i8254.h |  3 +--
>  2 files changed, 29 insertions(+), 41 deletions(-)
> 
> diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
> index de5f5018026f..a137eb381012 100644
> --- a/arch/x86/kvm/i8254.c
> +++ b/arch/x86/kvm/i8254.c
> @@ -236,16 +236,13 @@ static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
>  {
>  	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
>  						 irq_ack_notifier);
> -	int value;
>  
> -	spin_lock(&ps->inject_lock);
> +	atomic_set(&ps->irq_ack, 1);

smp_mb__before_atomic();

>  	if (atomic_add_unless(&ps->pending, -1, 0))
>  		/* in this case, we had multiple outstanding pit interrupts
>  		 * that we needed to inject.  Reinject
>  		 */
>  		queue_kthread_work(&ps->pit->worker, &ps->pit->expired);
> -	ps->irq_ack = 1;
> -	spin_unlock(&ps->inject_lock);
>  }
>  
>  void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
> @@ -276,34 +273,25 @@ static void pit_do_work(struct kthread_work *work)
>  	struct kvm_vcpu *vcpu;
>  	int i;
>  	struct kvm_kpit_state *ps = &pit->pit_state;
> -	int inject = 0;
>  
> -	/* Try to inject pending interrupts when
> -	 * last one has been acked.
> +	if (!atomic_xchg(&ps->irq_ack, 0))
> +		return;
> +
> +	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false);
> +	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0, false);
> +
> +	/*
> +	 * Provides NMI watchdog support via Virtual Wire mode.
> +	 * The route is: PIT -> LVT0 in NMI mode.
> +	 *
> +	 * Note: Our Virtual Wire implementation does not follow
> +	 * the MP specification.  We propagate a PIT interrupt to all
> +	 * VCPUs and only when LVT0 is in NMI mode.  The interrupt can
> +	 * also be simultaneously delivered through PIC and IOAPIC.
>  	 */
> -	spin_lock(&ps->inject_lock);
> -	if (ps->irq_ack) {
> -		ps->irq_ack = 0;
> -		inject = 1;
> -	}
> -	spin_unlock(&ps->inject_lock);
> -	if (inject) {
> -		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false);
> -		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0, false);
> -
> -		/*
> -		 * Provides NMI watchdog support via Virtual Wire mode.
> -		 * The route is: PIT -> PIC -> LVT0 in NMI mode.
> -		 *
> -		 * Note: Our Virtual Wire implementation is simplified, only
> -		 * propagating PIT interrupts to all VCPUs when they have set
> -		 * LVT0 to NMI delivery. Other PIC interrupts are just sent to
> -		 * VCPU0, and only if its LVT0 is in EXTINT mode.
> -		 */
> -		if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
> -			kvm_for_each_vcpu(i, vcpu, kvm)
> -				kvm_apic_nmi_wd_deliver(vcpu);
> -	}
> +	if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
> +		kvm_for_each_vcpu(i, vcpu, kvm)
> +			kvm_apic_nmi_wd_deliver(vcpu);
>  }
>  
>  static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
> @@ -323,6 +311,12 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
>  		return HRTIMER_NORESTART;
>  }
>  
> +static void kvm_pit_reset_reinject(struct kvm_pit *pit)
> +{
> +	atomic_set(&pit->pit_state.pending, 0);

smp_wmb()?

Looks safe otherwise.  (Please also add a comment before the memory
barriers to show the pairing).

Paolo

  reply	other threads:[~2016-02-03 16:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-03 16:23 [PATCH 0/4] KVM: x86: change and fix PIT discard tick policy Radim Krčmář
2016-02-03 16:23 ` [PATCH 1/4] KVM: x86: fix interrupt dropping race in PIT Radim Krčmář
2016-02-04 13:35   ` Radim Krčmář
2016-02-03 16:23 ` [PATCH 2/4] KVM: x86: refactor PIT state inject_lock Radim Krčmář
2016-02-03 16:45   ` Paolo Bonzini [this message]
2016-02-04 13:13     ` Radim Krčmář
2016-02-03 16:23 ` [PATCH 3/4] KVM: x86: change PIT discard tick policy Radim Krčmář
2016-02-03 16:48   ` Paolo Bonzini
2016-02-03 16:23 ` [PATCH 4/4] KVM: x86: remove notifiers from PIT discard policy Radim Krčmář
2016-02-03 16:51   ` Paolo Bonzini
2016-02-04 13:15     ` Radim Krčmář

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=56B22E95.30504@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rkrcmar@redhat.com \
    --cc=shibuya.yk@ncos.nec.co.jp \
    /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