public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Zachary Amsden <zamsden@redhat.com>
To: Chris Lalancette <clalance@redhat.com>
Cc: kvm@vger.kernel.org, avi@redhat.com, mtosatti@redhat.com
Subject: Re: [RFC][PATCH 1/3] Introduce a workqueue to deliver PIT timer interrupts.
Date: Tue, 08 Jun 2010 16:36:16 -1000	[thread overview]
Message-ID: <4C0EFE20.1080508@redhat.com> (raw)
In-Reply-To: <1276019703-18136-2-git-send-email-clalance@redhat.com>

On 06/08/2010 07:55 AM, Chris Lalancette wrote:
> We really want to "kvm_set_irq" during the hrtimer callback,
> but that is risky because that is during interrupt context.
> Instead, offload the work to a workqueue, which is a bit safer
> and should provide most of the same functionality.
>
> Signed-off-by: Chris Lalancette<clalance@redhat.com>
> ---
>   arch/x86/kvm/i8254.c |  106 ++++++++++++++++++++++++++++++-------------------
>   arch/x86/kvm/i8254.h |    4 ++
>   arch/x86/kvm/irq.c   |    1 -
>   arch/x86/kvm/x86.c   |    4 ++
>   4 files changed, 73 insertions(+), 42 deletions(-)
>
> diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
> index 188d827..dd655ef 100644
> --- a/arch/x86/kvm/i8254.c
> +++ b/arch/x86/kvm/i8254.c
> @@ -34,6 +34,7 @@
>
>   #include<linux/kvm_host.h>
>   #include<linux/slab.h>
> +#include<linux/workqueue.h>
>
>   #include "irq.h"
>   #include "i8254.h"
> @@ -49,6 +50,8 @@
>   #define RW_STATE_WORD0 3
>   #define RW_STATE_WORD1 4
>
> +static struct workqueue_struct *pit_wq;
> +
>   /* Compute with 96 bit intermediate result: (a*b)/c */
>   static u64 muldiv64(u64 a, u32 b, u32 c)
>   {
> @@ -281,6 +284,58 @@ static struct kvm_timer_ops kpit_ops = {
>   	.is_periodic = kpit_is_periodic,
>   };
>
> +static void pit_do_work(struct work_struct *work)
> +{
> +       struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
> +       struct kvm *kvm = pit->kvm;
> +       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.
> +        */
> +       raw_spin_lock(&ps->inject_lock);
> +       if (ps->irq_ack) {
> +               ps->irq_ack = 0;
> +               inject = 1;
> +       }
> +       raw_spin_unlock(&ps->inject_lock);
> +       if (inject) {
> +               kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
> +               kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
> +
> +               /*
> +                * 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 (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)
> +{
> +       struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
> +       struct kvm_pit *pt = ktimer->kvm->arch.vpit;
> +
> +       queue_work(pit_wq,&pt->expired);
> +
> +       if (ktimer->t_ops->is_periodic(ktimer)) {
> +               hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
> +              return HRTIMER_RESTART;
> +       }
> +       else
> +              return HRTIMER_NORESTART;
> +}
> +
>   static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
>   {
>   	struct kvm_timer *pt =&ps->pit_timer;
> @@ -295,10 +350,9 @@ static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
>   	pt->period = interval;
>   	ps->is_periodic = is_period;
>
> -	pt->timer.function = kvm_timer_fn;
> +	pt->timer.function = pit_timer_fn;
>
>    

I am happy to see this.  I thought kvm_timer_fn was a step backwards; it 
was too general of a function to justify the savings of 20 some odd 
lines of code.

Is there any chance that using a workqueue might help the problem of 
hrtimers firing too quickly?  I wanted to return HR_NORESTART from 
pit_timer_fn always, then restart the hrtimer on delivery, but because 
of unreliable delivery, it wasn't clear how to do that.

Perhaps the workqueue can be used to restart the timer instead, avoiding 
problems of impossibly small timeouts causing hrtimers to run amok.

Zach

  reply	other threads:[~2010-06-09  2:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-08 17:55 [RFC][PATCH 0/3]: Fixes to IRQ routing Chris Lalancette
2010-06-08 17:55 ` [RFC][PATCH 1/3] Introduce a workqueue to deliver PIT timer interrupts Chris Lalancette
2010-06-09  2:36   ` Zachary Amsden [this message]
2010-06-09 13:23     ` Marcelo Tosatti
2010-06-09 14:16       ` Avi Kivity
2010-06-09 14:40         ` Marcelo Tosatti
2010-06-09 21:17       ` Zachary Amsden
2010-06-10  9:24         ` Avi Kivity
2010-06-10 19:17           ` Zachary Amsden
2010-06-10 16:18         ` Marcelo Tosatti
2010-06-09 10:55   ` Avi Kivity
2010-06-09 13:49   ` Marcelo Tosatti
2010-06-09 14:20     ` Avi Kivity
2010-06-08 17:55 ` [RFC][PATCH 2/3] Allow any LAPIC to accept PIC interrupts Chris Lalancette
2010-06-08 17:55 ` [RFC][PATCH 3/3] In DM_LOWEST, only deliver interrupts to vcpus with enabled LAPIC's Chris Lalancette
2010-06-09 11:01   ` Gleb Natapov
2010-06-09 11:08     ` Avi Kivity
2010-06-09 11:20       ` Gleb Natapov
2010-06-09 11:22         ` Avi Kivity
2010-06-09 11:02 ` [RFC][PATCH 0/3]: Fixes to IRQ routing 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=4C0EFE20.1080508@redhat.com \
    --to=zamsden@redhat.com \
    --cc=avi@redhat.com \
    --cc=clalance@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox