RFC: in-kernel timer emulation changes
cover RFC: in-kernel timer emulation changes
This still needs to survive kvm-autotest, posting early for comments.
It replaces hrtimer based emulation with ktime_t comparisons on guest entry (avoids host load when guests are scheduled out, removes a spinlock acquision on entry (i8254.c's inject_lock), and makes future improvements easier).
PATCH 1/8 KVM: timer interface unification
Hide details of timer emulation behind an interface, and unify the hrtimer based implementation.
arch/x86/kvm/i8254.c +13 -51
@@ -113,7 +113,7 @@ static s64 __kpit_elapsed(struct kvm *kv* itself with the initial count and continues counting * from there. */ - remaining =hrtimer_expires_remaining (&ps->pit_timer.timer); + remaining =kvm_timer_remaining (&ps->pit_timer); elapsed = ps->pit_timer.period -ktime_to_ns (remaining); elapsed =mod_64 (elapsed, ps->pit_timer.period);
@@ -229,7 +229,7 @@ int pit_has_pending_timer(struct kvm_vcpstruct kvm_pit *pit = vcpu->kvm->arch.vpit;if (pit &&kvm_vcpu_is_bsp (vcpu) && pit->pit_state.irq_ack) -return atomic_read (&pit->pit_state.pit_timer.pending); +return kvm_timer_has_pending (&pit->pit_state.pit_timer);return 0 ; }
@@ -238,42 +238,17 @@ static void kvm_pit_ack_irq(struct kvm_i struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, irq_ack_notifier);spin_lock (&ps->inject_lock); -if (atomic_dec_return (&ps->pit_timer.pending) <0 ) -atomic_inc (&ps->pit_timer.pending); +kvm_timer_ack (&ps->pit_timer); ps->irq_ack =1 ;spin_unlock (&ps->inject_lock); } -void __kvm_migrate_pit_timer (struct kvm_vcpu *vcpu) -{ -struct kvm_pit *pit = vcpu->kvm->arch.vpit; -struct hrtimer *timer; - -if (!kvm_vcpu_is_bsp (vcpu) || !pit) -return ; - - timer = &pit->pit_state.pit_timer.timer; -if (hrtimer_cancel (timer)) -hrtimer_start_expires (timer,HRTIMER_MODE_ABS ); -} -static void destroy_pit_timer (struct kvm_timer *pt) {pr_debug ("pit: execute del timer!\n" ); -hrtimer_cancel (&pt->timer); +kvm_timer_cancel (pt); } -static bool kpit_is_periodic (struct kvm_timer *ktimer) -{ - struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state, - pit_timer); -return ps->is_periodic; -} - -static struct kvm_timer_ops kpit_ops = { - .is_periodic = kpit_is_periodic, -}; -static void create_pit_timer (struct kvm_kpit_state *ps,u32 val,int is_period) {struct kvm_timer *pt = &ps->pit_timer;
@@ -284,20 +259,10 @@ static void create_pit_timer(struct kvm_pr_debug ("pit: create pit timer, interval is %llu nsec\n" , interval);/* TODO The new value only affected after the retriggered */ -hrtimer_cancel (&pt->timer); - pt->period = interval; - ps->is_periodic = is_period; - - pt->timer.function = kvm_timer_fn; - pt->t_ops = &kpit_ops; - pt->kvm = ps->pit->kvm; - pt->vcpu = pt->kvm->bsp_vcpu; +kvm_timer_cancel (pt); -atomic_set (&pt->pending,0 ); ps->irq_ack =1 ; - -hrtimer_start (&pt->timer,ktime_add_ns (ktime_get (), interval), -HRTIMER_MODE_ABS ); +kvm_timer_start (pt, interval, is_period); }static void pit_load_count (struct kvm *kvm,int channel,u32 val)
@@ -545,7 +510,7 @@ static int speaker_ioport_read(struct kvreturn 0 ; } -void kvm_pit_reset (struct kvm_pit *pit) +static void kvm_pit_reset (struct kvm_pit *pit) {int i;struct kvm_kpit_channel_state *c;
@@ -559,7 +524,7 @@ void kvm_pit_reset(struct kvm_pit *pit) }mutex_unlock (&pit->pit_state.lock); -atomic_set (&pit->pit_state.pit_timer.pending,0 ); +kvm_timer_reset (&pit->pit_state.pit_timer); pit->pit_state.irq_ack =1 ; }
@@ -568,7 +533,7 @@ static void pit_mask_notifer(struct kvm_ struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);if (!mask) { -atomic_set (&pit->pit_state.pit_timer.pending,0 ); +kvm_timer_reset (&pit->pit_state.pit_timer); pit->pit_state.irq_ack =1 ; } }
@@ -608,8 +573,8 @@ struct kvm_pit *kvm_create_pit(struct kv pit_state = &pit->pit_state; pit_state->pit = pit; -hrtimer_init (&pit_state->pit_timer.timer, -CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); +kvm_timer_init (kvm, &pit_state->pit_timer); + pit_state->irq_ack_notifier.gsi =0 ; pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;kvm_register_irq_ack_notifier (kvm, &pit_state->irq_ack_notifier);
@@ -634,14 +599,11 @@ struct kvm_pit *kvm_create_pit(struct kvvoid kvm_free_pit (struct kvm *kvm) { -struct hrtimer *timer; -if (kvm->arch.vpit) { +kvm_timer_cancel (&kvm->arch.vpit->pit_state.pit_timer);kvm_unregister_irq_mask_notifier (kvm,0 , &kvm->arch.vpit->mask_notifier);mutex_lock (&kvm->arch.vpit->pit_state.lock); - timer = &kvm->arch.vpit->pit_state.pit_timer.timer; -hrtimer_cancel (timer);kvm_free_irq_source_id (kvm, kvm->arch.vpit->irq_source_id);mutex_unlock (&kvm->arch.vpit->pit_state.lock);kfree (kvm->arch.vpit);
@@ -686,7 +648,7 @@ void kvm_inject_pit_timer_irqs(struct kv* last one has been acked. */ spin_lock (&ps->inject_lock); -if (atomic_read (&ps->pit_timer.pending) && ps->irq_ack) { +if (kvm_timer_has_pending (&ps->pit_timer) && ps->irq_ack) { ps->irq_ack =0 ; inject =1 ; }
arch/x86/kvm/kvm_timer.h +14 -7
@@ -1,18 +1,25 @@struct kvm_timer {struct hrtimer timer; -s64 period;/* unit: ns */ -atomic_t pending;/* accumulated triggered timers */ +s64 period;/* unit: ns */ +atomic_t pending;/* accumulated triggered timers */ bool reinject; -struct kvm_timer_ops *t_ops; +bool periodic;struct kvm *kvm;struct kvm_vcpu *vcpu; }; -struct kvm_timer_ops { -bool (*is_periodic)(struct kvm_timer *); -}; +void kvm_timer_init (struct kvm *kvm,struct kvm_timer *ktimer); +void kvm_timer_start (struct kvm_timer *ktimer,u64 interval,bool periodic); +void kvm_timer_cancel (struct kvm_timer *ktimer); +void kvm_timer_vcpu_bind (struct kvm_timer *ktimer,struct kvm_vcpu *vcpu); + +int kvm_timer_has_pending (struct kvm_timer *ktimer); +void kvm_timer_ack (struct kvm_timer *ktimer); +void kvm_timer_reset (struct kvm_timer *ktimer); + +void kvm_migrate_timer (struct kvm_timer *ktimer); -enum hrtimer_restart kvm_timer_fn (struct hrtimer *data); +ktime_t kvm_timer_remaining (struct kvm_timer *ktimer);
arch/x86/kvm/lapic.c +13 -59
158 lines not on this page · raw message
arch/x86/kvm/timer.c +57 -2
74 lines not on this page · raw message
arch/x86/kvm/i8254.h +0 -2
@@ -22,7 +22,6 @@ struct kvm_kpit_channel_state {struct kvm_kpit_state {struct kvm_kpit_channel_state channels[3 ];struct kvm_timer pit_timer; -bool is_periodic;u32 speaker_data_on;struct mutex lock;struct kvm_pit *pit;
@@ -52,6 +51,5 @@ void kvm_inject_pit_timer_irqs(struct kvvoid kvm_pit_load_count (struct kvm *kvm,int channel,u32 val);struct kvm_pit *kvm_create_pit (struct kvm *kvm,u32 flags);void kvm_free_pit (struct kvm *kvm); -void kvm_pit_reset (struct kvm_pit *pit);#endif
arch/x86/kvm/irq.c +20 -0
@@ -94,6 +94,26 @@ void kvm_inject_pending_timer_irqs(struc }EXPORT_SYMBOL_GPL (kvm_inject_pending_timer_irqs); +static void __kvm_migrate_apic_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_lapic *apic = vcpu->arch.apic; + +if (!apic) +return ; + +kvm_migrate_timer (&apic->lapic_timer); +} + +static void __kvm_migrate_pit_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_pit *pit = vcpu->kvm->arch.vpit; + +if (!kvm_vcpu_is_bsp (vcpu) || !pit) +return ; + +kvm_migrate_timer (&pit->pit_state.pit_timer); +} +void __kvm_migrate_timers (struct kvm_vcpu *vcpu) {__kvm_migrate_apic_timer (vcpu);
arch/x86/kvm/irq.h +0 -2
@@ -94,8 +94,6 @@ void kvm_pic_reset(struct kvm_kpic_statevoid kvm_inject_pending_timer_irqs (struct kvm_vcpu *vcpu);void kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu);void kvm_apic_nmi_wd_deliver (struct kvm_vcpu *vcpu); -void __kvm_migrate_apic_timer (struct kvm_vcpu *vcpu); -void __kvm_migrate_pit_timer (struct kvm_vcpu *vcpu);void __kvm_migrate_timers (struct kvm_vcpu *vcpu);int pit_has_pending_timer (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +4 -0
@@ -4565,6 +4565,7 @@ struct kvm_vcpu *kvm_arch_vcpu_create(stint kvm_arch_vcpu_setup (struct kvm_vcpu *vcpu) {int r; +struct kvm *kvm = vcpu->kvm;/* We do fxsave: this must be aligned. */ BUG_ON ((unsigned long )&vcpu->arch.host_fx_image &0xF );
@@ -4578,6 +4579,9 @@ int kvm_arch_vcpu_setup(struct kvm_vcpuif (r <0 ) goto free_vcpu; +if (kvm->arch.vpit &&kvm_vcpu_is_bsp (vcpu)) +kvm_timer_vcpu_bind (&kvm->arch.vpit->pit_state.pit_timer, vcpu); +return 0 ; free_vcpu: kvm_x86_ops->vcpu_free (vcpu);
Re: [patch 1/8] KVM: timer interface unification
Quoted 3 lines · Marcelo Tosatti
Hide details of timer emulation behind an interface, and unify the hrtimer based implementation.
For bisectability, might want to split into three: introduce generic timer, migrate lapic timer, migrate pit timer.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 2/8 KVM: move lapic timer ack to EOI handler
In reply to Marcelo Tosattilapic timer is acked on interrupt injection, move it to EOI handler.
arch/x86/kvm/lapic.c +6 -4
@@ -427,6 +427,7 @@ int kvm_apic_compare_prio(struct kvm_vcpstatic void apic_set_eoi (struct kvm_lapic *apic) {int vector =apic_find_highest_isr (apic); +int lvt_vector =apic_get_reg (apic,APIC_LVTT ) &APIC_VECTOR_MASK ;int trigger_mode;/* * Not every write EOI will has corresponding ISR,
@@ -435,6 +436,9 @@ static void apic_set_eoi(struct kvm_lapiif (vector ==-1 )return ; +if (vector == lvt_vector) +kvm_timer_ack (&apic->lapic_timer); +apic_clear_vector (vector, apic->regs +APIC_ISR );apic_update_ppr (apic);
@@ -982,10 +986,8 @@ void kvm_inject_apic_timer_irqs(struct k {struct kvm_lapic *apic = vcpu->arch.apic; -if (apic &&kvm_timer_has_pending (&apic->lapic_timer)) { -if (kvm_apic_local_deliver (apic,APIC_LVTT )) -kvm_timer_ack (&apic->lapic_timer); - } +if (apic &&kvm_timer_has_pending (&apic->lapic_timer)) +kvm_apic_local_deliver (apic,APIC_LVTT ); }int kvm_get_apic_interrupt (struct kvm_vcpu *vcpu)
PATCH 3/8 KVM: x86: per-vcpu timer list
In reply to Marcelo TosattiAdd a per-vcpu list of (emulated) timers.
arch/x86/include/asm/kvm_host.h +2 -0
@@ -375,6 +375,8 @@ struct kvm_vcpu_arch {u64 mcg_status;u64 mcg_ctl;u64 *mce_banks; + +struct list_head timers; };struct kvm_mem_alias {
arch/x86/kvm/kvm_timer.h +1 -0
@@ -7,6 +7,7 @@ struct kvm_timer {bool periodic;struct kvm *kvm;struct kvm_vcpu *vcpu; +struct list_head vcpu_timer; };void kvm_timer_init (struct kvm *kvm,struct kvm_timer *ktimer);
arch/x86/kvm/x86.c +2 -0
@@ -4644,6 +4644,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *BUG_ON (vcpu->kvm ==NULL ); kvm = vcpu->kvm; +INIT_LIST_HEAD (&vcpu->arch.timers); + vcpu->arch.mmu.root_hpa =INVALID_PAGE ;if (!irqchip_in_kernel (kvm) ||kvm_vcpu_is_bsp (vcpu)) vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;
arch/x86/kvm/timer.c +2 -0
@@ -53,11 +53,13 @@ void kvm_timer_init(struct kvm *kvm, str ktimer->kvm = kvm;hrtimer_init (&ktimer->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); ktimer->timer.function = kvm_timer_fn; +INIT_LIST_HEAD (&ktimer->vcpu_timer); }void kvm_timer_vcpu_bind (struct kvm_timer *ktimer,struct kvm_vcpu *vcpu) { ktimer->vcpu = vcpu; +list_add (&ktimer->vcpu_timer, &vcpu->arch.timers); }void kvm_timer_start (struct kvm_timer *ktimer,u64 interval,bool periodic)
PATCH 4/8 KVM: x86: replace hrtimer based timer emulation
In reply to Marcelo TosattiReplace hrtimer based timer emulation with host timebase (ktime_t) comparisons on guest entry.
This avoids host load when guests are scheduled out, removes a spinlock acquision on entry (i8254.c's inject_lock), and makes future improvements easier.
arch/x86/kvm/x86.c +9 -6
@@ -3461,8 +3461,6 @@ static int vcpu_enter_guest(struct kvm_v goto out;if (vcpu->requests) { -if (test_and_clear_bit (KVM_REQ_MIGRATE_TIMER , &vcpu->requests)) -__kvm_migrate_timers (vcpu);if (test_and_clear_bit (KVM_REQ_KVMCLOCK_UPDATE , &vcpu->requests))kvm_write_guest_time (vcpu);if (test_and_clear_bit (KVM_REQ_MMU_SYNC , &vcpu->requests))
@@ -3482,6 +3480,9 @@ static int vcpu_enter_guest(struct kvm_v } } + +kvm_inject_pending_timer_irqs (vcpu); +preempt_disable (); kvm_x86_ops->prepare_guest_switch (vcpu);
@@ -3499,6 +3500,8 @@ static int vcpu_enter_guest(struct kvm_v goto out; } +kvm_vcpu_arm_exit (vcpu); +if (vcpu->arch.exception.pending)__queue_exception (vcpu);else
@@ -3564,6 +3567,8 @@ static int vcpu_enter_guest(struct kvm_vpreempt_enable (); +kvm_vcpu_cleanup_timer (vcpu); +down_read (&vcpu->kvm->slots_lock);/*
@@ -3627,10 +3632,6 @@ static int __vcpu_run(struct kvm_vcpu *vif (r <=0 )break ; -clear_bit (KVM_REQ_PENDING_TIMER , &vcpu->requests); -if (kvm_cpu_has_pending_timer (vcpu)) -kvm_inject_pending_timer_irqs (vcpu); -if (dm_request_for_irq_injection (vcpu, kvm_run)) { r = -EINTR ; kvm_run->exit_reason =KVM_EXIT_INTR ;
@@ -4579,6 +4580,8 @@ int kvm_arch_vcpu_setup(struct kvm_vcpuif (r <0 ) goto free_vcpu; +kvm_vcpu_init_armed_exit (vcpu); +if (kvm->arch.vpit &&kvm_vcpu_is_bsp (vcpu))kvm_timer_vcpu_bind (&kvm->arch.vpit->pit_state.pit_timer, vcpu);
virt/kvm/kvm_main.c +10 -1
@@ -1656,11 +1656,19 @@ void mark_page_dirty(struct kvm *kvm, gf } } +#ifndef KVM_ARCH_HAVE_TIMER_EVENT +ktime_t kvm_vcpu_next_timer_event (struct kvm_vcpu *vcpu) +{ +return (ktime_t ) { .tv64 =KTIME_MAX }; +} +#endif +/* * The vCPU has executed a HLT instruction with in-kernel mode enabled. */ void kvm_vcpu_block (struct kvm_vcpu *vcpu) { +ktime_t expires;DEFINE_WAIT (wait);for (;;) {
@@ -1677,8 +1685,9 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpif (signal_pending (current))break ; + expires =kvm_vcpu_next_timer_event (vcpu);vcpu_put (vcpu); -schedule (); +schedule_hrtimeout (&expires,HRTIMER_MODE_ABS );vcpu_load (vcpu); }
arch/x86/kvm/i8254.c +31 -57
109 lines not on this page · raw message
arch/x86/kvm/lapic.c +15 -19
@@ -875,16 +875,6 @@ int kvm_lapic_enabled(struct kvm_vcpu *v*---------------------------------------------------------------------- */ -int apic_has_pending_timer (struct kvm_vcpu *vcpu) -{ -struct kvm_lapic *lapic = vcpu->arch.apic; - -if (lapic &&apic_enabled (lapic) &&apic_lvt_enabled (lapic,APIC_LVTT )) -return kvm_timer_has_pending (&lapic->lapic_timer); - -return 0 ; -} -static int kvm_apic_local_deliver (struct kvm_lapic *apic,int lvt_type) {u32 reg =apic_get_reg (apic, lvt_type);
@@ -912,6 +902,20 @@ static const struct kvm_io_device_ops ap .write = apic_mmio_write, }; +void inject_lapic_timer (struct kvm_timer *ktimer) +{ +struct kvm_vcpu *vcpu = ktimer->vcpu; +struct kvm_lapic *apic = vcpu->arch.apic; + +if (apic) +kvm_apic_local_deliver (apic,APIC_LVTT ); +} + +struct kvm_timer_ops lapic_timer_ops = { + .inject = inject_lapic_timer, + .name ="lapic" , +}; +int kvm_create_lapic (struct kvm_vcpu *vcpu) {struct kvm_lapic *apic;
@@ -935,7 +939,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcmemset (apic->regs,0 ,PAGE_SIZE ); apic->vcpu = vcpu; -kvm_timer_init (vcpu->kvm, &apic->lapic_timer); +kvm_timer_init (vcpu->kvm, &apic->lapic_timer, &lapic_timer_ops);kvm_timer_vcpu_bind (&apic->lapic_timer, vcpu); apic->base_address =APIC_DEFAULT_PHYS_BASE ;
@@ -982,14 +986,6 @@ int kvm_apic_accept_pic_intr(struct kvm_return r; } -void kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu) -{ -struct kvm_lapic *apic = vcpu->arch.apic; - -if (apic &&kvm_timer_has_pending (&apic->lapic_timer)) -kvm_apic_local_deliver (apic,APIC_LVTT ); -} -int kvm_get_apic_interrupt (struct kvm_vcpu *vcpu) {int vector =kvm_apic_has_interrupt (vcpu);
arch/x86/include/asm/kvm_host.h +4 -0
@@ -377,6 +377,7 @@ struct kvm_vcpu_arch {u64 *mce_banks;struct list_head timers; +struct hrtimer exit_timer; };struct kvm_mem_alias {
@@ -800,4 +801,7 @@ int kvm_unmap_hva(struct kvm *kvm, unsigint kvm_age_hva (struct kvm *kvm,unsigned long hva);int cpuid_maxphyaddr (struct kvm_vcpu *vcpu); +#define KVM_ARCH_HAVE_TIMER_EVENT +ktime_t kvm_vcpu_next_timer_event (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/irq.c +9 -41
63 lines not on this page · raw message
arch/x86/kvm/svm.c +0 -1
@@ -738,7 +738,6 @@ static void svm_vcpu_load(struct kvm_vcp delta = vcpu->arch.host_tsc - tsc_this; svm->vmcb->control.tsc_offset += delta; vcpu->cpu = cpu; -kvm_migrate_timers (vcpu); }for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++)
arch/x86/kvm/vmx.c +0 -1
@@ -703,7 +703,6 @@ static void vmx_vcpu_load(struct kvm_vcpif (vcpu->cpu != cpu) {vcpu_clear (vmx); -kvm_migrate_timers (vcpu);vpid_sync_vcpu_all (vmx);local_irq_disable (); list_add(&vmx->local_vcpus_link,
arch/x86/kvm/kvm_timer.h +22 -7
@@ -1,26 +1,41 @@ +struct kvm_timer_ops ;struct kvm_timer { -struct hrtimer timer; -s64 period;/* unit: ns */ -atomic_t pending;/* accumulated triggered timers */ +ktime_t count_load_time; +ktime_t inject_time; +u64 period;/* unit: ns */ +u64 acked_events; + +bool can_inject;bool reinject;bool periodic; +struct kvm *kvm;struct kvm_vcpu *vcpu;struct list_head vcpu_timer; +struct kvm_timer_ops *ops; }; -void kvm_timer_init (struct kvm *kvm,struct kvm_timer *ktimer); +struct kvm_timer_ops { +void (*inject)(struct kvm_timer *); +char *name; +}; + +void kvm_timer_init (struct kvm *kvm,struct kvm_timer *ktimer, +struct kvm_timer_ops *ops); +void kvm_timer_vcpu_bind (struct kvm_timer *ktimer,struct kvm_vcpu *vcpu);void kvm_timer_start (struct kvm_timer *ktimer,u64 interval,bool periodic);void kvm_timer_cancel (struct kvm_timer *ktimer); -void kvm_timer_vcpu_bind (struct kvm_timer *ktimer,struct kvm_vcpu *vcpu); -int kvm_timer_has_pending (struct kvm_timer *ktimer);void kvm_timer_ack (struct kvm_timer *ktimer);void kvm_timer_reset (struct kvm_timer *ktimer);void kvm_migrate_timer (struct kvm_timer *ktimer); +void kvm_vcpu_init_armed_exit (struct kvm_vcpu *vcpu); -ktime_t kvm_timer_remaining (struct kvm_timer *ktimer); +void kvm_vcpu_arm_exit (struct kvm_vcpu *vcpu); +void kvm_vcpu_cleanup_timer (struct kvm_vcpu *vcpu); +ktime_t kvm_timer_next_event (struct kvm_timer *ktimer); +ktime_t kvm_timer_remaining (struct kvm_timer *ktimer);
arch/x86/kvm/timer.c +132 -63
239 lines not on this page · raw message
arch/x86/kvm/irq.h +0 -4
@@ -94,9 +94,5 @@ void kvm_pic_reset(struct kvm_kpic_statevoid kvm_inject_pending_timer_irqs (struct kvm_vcpu *vcpu);void kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu);void kvm_apic_nmi_wd_deliver (struct kvm_vcpu *vcpu); -void __kvm_migrate_timers (struct kvm_vcpu *vcpu); - -int pit_has_pending_timer (struct kvm_vcpu *vcpu); -int apic_has_pending_timer (struct kvm_vcpu *vcpu);#endif
include/linux/kvm_host.h +8 -14
@@ -30,15 +30,14 @@* vcpu->requests bit members */ #define KVM_REQ_TLB_FLUSH 0 -#define KVM_REQ_MIGRATE_TIMER 1 -#define KVM_REQ_REPORT_TPR_ACCESS 2 -#define KVM_REQ_MMU_RELOAD 3 -#define KVM_REQ_TRIPLE_FAULT 4 -#define KVM_REQ_PENDING_TIMER 5 -#define KVM_REQ_UNHALT 6 -#define KVM_REQ_MMU_SYNC 7 -#define KVM_REQ_KVMCLOCK_UPDATE 8 -#define KVM_REQ_KICK 9 +#define KVM_REQ_REPORT_TPR_ACCESS 1 +#define KVM_REQ_MMU_RELOAD 2 +#define KVM_REQ_TRIPLE_FAULT 3 +#define KVM_REQ_PENDING_TIMER 4 +#define KVM_REQ_UNHALT 5 +#define KVM_REQ_MMU_SYNC 6 +#define KVM_REQ_KVMCLOCK_UPDATE 7 +#define KVM_REQ_KICK 8#define KVM_USERSPACE_IRQ_SOURCE_ID 0
@@ -469,11 +468,6 @@ static inline hpa_t pfn_to_hpa(pfn_t pfnreturn (hpa_t )pfn <<PAGE_SHIFT ; } -static inline void kvm_migrate_timers (struct kvm_vcpu *vcpu) -{ -set_bit (KVM_REQ_MIGRATE_TIMER , &vcpu->requests); -} -enum kvm_stat_kind {KVM_STAT_VM ,KVM_STAT_VCPU ,
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Excellent patch series.
arch/x86/kvm/irq.c line 37
Quoted 12 lines · Marcelo Tosatti
int
kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu){arch/x86/kvm/irq.c +6
-3-int ret; +ktime_t now, expires; - ret =pit_has_pending_timer (vcpu); - ret |=apic_has_pending_timer (vcpu); + expires =kvm_vcpu_next_timer_event (vcpu); + now =ktime_get (); +if (expires.tv64 <= now.tv64) { +if (kvm_arch_interrupt_allowed (vcpu)) +set_bit (KVM_REQ_UNHALT , &vcpu->requests);
You shouldn't unhalt vcpu here. Not every timer event will generate interrupt (vector can be masked in pic/ioapic) or timer event can generate NMI instead of interrupt. Leaving this code out probably means that you can't remove kvm_inject_pending_timer_irqs() call from __vcpu_run().
arch/x86/kvm/irq.c line 43
Quoted 8 lines
arch/x86/kvm/irq.c +3
-1+return 1 ; + } -return ret; +return 0 ; }EXPORT_SYMBOL (kvm_cpu_has_pending_timer);
--
Gleb.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 8 lines · Gleb Natapov
Excellent patch series.
Quoted 5 lines · Marcelo Tosatti
int
kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu){(quoted diff) +1
-1- int ret; + ktime_t now, expires;
Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +5
-2- ret = pit_has_pending_timer(vcpu); - ret |= apic_has_pending_timer(vcpu); + expires = kvm_vcpu_next_timer_event(vcpu); + now = ktime_get(); + if (expires.tv64 <= now.tv64) { + if (kvm_arch_interrupt_allowed(vcpu)) + set_bit(KVM_REQ_UNHALT, &vcpu->requests);You shouldn't unhalt vcpu here. Not every timer event will generate interrupt (vector can be masked in pic/ioapic)
Yeah. Note however that kvm_vcpu_next_timer_event only returns the expiration time for events that have been acked (for timers that have had events injected, but not acked it returns KTIME_MAX).
So, the code above will set one spurious unhalt if:
- inject timer irq
- guest acks irq
- guest mask irq
- unhalt (once)
I had a "kvm_timer_mask" callback before (along with the attached patch), but decided to keep it simpler using the ack trick above.
I suppose one spurious unhalt is harmless, or is it a correctness issue?
Quoted 1 line
or timer event can generate NMI instead of interrupt.
In the NMI case it should not unhalt the processor?
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg i8254.c NMI injection via all cpus LINT0).
Quoted 7 lines
Leaving this code out probably means
that you can't removekvm_inject_pending_timer_irqs()call from__vcpu_run().Quoted 3 lines
(quoted diff) +2
-0+ return 1; + }
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +1
-1- return ret; + return 0; } EXPORT_SYMBOL(kvm_cpu_has_pending_timer);--
Gleb.
arch/x86/kvm/i8259.c +12 -0
@@ -122,6 +122,18 @@ static inline int get_priority(struct kvreturn priority; } +int pic_int_is_masked (struct kvm *kvm,int irq) +{ +u8 imr; +struct kvm_pic *s =pic_irqchip (kvm); + +pic_lock (s); + imr = s->pics[SELECT_PIC (irq)].imr; +pic_unlock (s); + +return (imr & (1 << irq)); +} +/* * return the pic wanted interrupt. return -1 if none */
arch/x86/kvm/irq.c +23 -0
@@ -42,6 +42,29 @@ int kvm_cpu_has_pending_timer(struct kvmEXPORT_SYMBOL (kvm_cpu_has_pending_timer);/* +* The PIT interrupt can be masked on different levels: PIC/IOAPIC/LAPIC. +* Hide the maze from the PIT mask notifier, all it cares about is whether +* the interrupt can reach the processor. +*/ +void kvm_pit_mask_notifier (struct kvm_irq_mask_notifier *kimn,bool mask) +{ +bool masked =0 ; +struct kvm *kvm; + struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); + + kvm = pit->kvm; + +/* LAPIC hardware disabled or INTIN0 programmed as ExtINT */ +if (kvm_apic_accept_pic_intr (kvm->vcpus[0 ])) { + masked =pic_int_is_masked (kvm,0 ); +/* IOAPIC -> LAPIC */ + }else + masked =ioapic_int_is_masked (kvm,0 ); + +kvm_pit_internal_mask_notifier (kvm, mask); +} + +/* * check if there is pending interrupt without * intack. */
virt/kvm/ioapic.c +10 -0
@@ -101,6 +101,16 @@ static int ioapic_service(struct kvm_ioareturn injected; } +int ioapic_int_is_masked (struct kvm *kvm,int pin) +{ +struct kvm_ioapic *ioapic = kvm->arch.vioapic; + +if (!ioapic) +return 1 ; + +return ioapic->redirtbl[pin].fields.mask; +} +static void ioapic_write_indirect (struct kvm_ioapic *ioapic,u32 val) {unsigned index;
arch/x86/kvm/i8254.c +4 -4
@@ -558,9 +558,9 @@ void kvm_pit_reset(struct kvm_pit *pit) pit->pit_state.irq_ack =1 ; } -static void pit_mask_notifer (struct kvm_irq_mask_notifier *kimn,bool mask) +void kvm_pit_internal_mask_notifier (struct kvm *kvm,bool mask) { - struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); +struct kvm_pit *pit = kvm->arch.vpit;if (!mask) {atomic_set (&pit->pit_state.pit_timer.pending,0 );
@@ -614,8 +614,8 @@ struct kvm_pit *kvm_create_pit(struct kvkvm_pit_reset (pit); - pit->mask_notifier.func = pit_mask_notifer; -kvm_register_irq_mask_notifier (kvm,0 , &pit->mask_notifier); + pit->mask_notifier.func = kvm_pit_mask_notifier; +kvm_register_irq_mask_notifier (kvm,0 , &kvm->arch.vpit->mask_notifier);kvm_iodevice_init (&pit->dev, &pit_dev_ops);kvm_io_bus_register_dev (&kvm->pio_bus, &pit->dev);
arch/x86/kvm/irq.h +6 -0
@@ -98,7 +98,13 @@ void __kvm_migrate_apic_timer(struct kvmvoid __kvm_migrate_pit_timer (struct kvm_vcpu *vcpu);void __kvm_migrate_timers (struct kvm_vcpu *vcpu); +void kvm_pit_internal_mask_notifier (struct kvm *kvm,bool mask); +void kvm_pit_mask_notifier (struct kvm_irq_mask_notifier *kimn,bool mask); +int pit_has_pending_timer (struct kvm_vcpu *vcpu);int apic_has_pending_timer (struct kvm_vcpu *vcpu); +int pic_int_is_masked (struct kvm *kvm,int irq); +int ioapic_int_is_masked (struct kvm *kvm,int irq); +#endif
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 30 lines · Marcelo Tosatti
Quoted 8 lines · Gleb Natapov
Excellent patch series.
Quoted 5 lines · Marcelo Tosatti
int
kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu){(quoted diff) +1
-1- int ret; + ktime_t now, expires;Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +5
-2- ret = pit_has_pending_timer(vcpu); - ret |= apic_has_pending_timer(vcpu); + expires = kvm_vcpu_next_timer_event(vcpu); + now = ktime_get(); + if (expires.tv64 <= now.tv64) { + if (kvm_arch_interrupt_allowed(vcpu)) + set_bit(KVM_REQ_UNHALT, &vcpu->requests);You shouldn't unhalt vcpu here. Not every timer event will generate interrupt (vector can be masked in pic/ioapic)
Yeah. Note however that
kvm_vcpu_next_timer_eventonly returns the expiration time for events that have been acked (for timers that have had events injected, but not acked it returnsKTIME_MAX).So, the code above will set one spurious unhalt if:
- inject timer irq
- guest acks irq
- guest mask irq
- unhalt (once)
Quoted 5 lines
I had a "
kvm_timer_mask" callback before (along with the attached patch), but decided to keep it simpler using the ack trick above.I suppose one spurious unhalt is harmless, or is it a correctness issue?
This is correctness issue. We should be as close to real CPU as possible. This will save us may hours of debugging later :)
Quoted 3 lines
Quoted 1 line
or timer event can generate NMI instead of interrupt.
In the NMI case it should not unhalt the processor?
Why? It should. It should jump to NMI handler.
Quoted 134 lines
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg
i8254.cNMI injection via all cpus LINT0).Quoted 7 lines
Leaving this code out probably means
that you can't removekvm_inject_pending_timer_irqs()call from__vcpu_run().Quoted 3 lines
virt/kvm/ioapic.c +2
-0+return 1 ; + }Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +1
-1- return ret; + return 0; } EXPORT_SYMBOL(kvm_cpu_has_pending_timer);--
Gleb.Index:
kvm/arch/x86/kvm/i8259.c
===================================================================arch/x86/kvm/i8259.c +12
-0@@ -122,6 +122,18 @@ static inline int get_priority(struct kvreturn priority; } +int pic_int_is_masked (struct kvm *kvm,int irq) +{ +u8 imr; +struct kvm_pic *s =pic_irqchip (kvm); + +pic_lock (s); + imr = s->pics[SELECT_PIC (irq)].imr; +pic_unlock (s); + +return (imr & (1 << irq)); +} +/* * return the pic wanted interrupt. return -1 if none */ Index:
kvm/arch/x86/kvm/irq.c
===================================================================arch/x86/kvm/irq.c +23
-0@@ -42,6 +42,29 @@ int kvm_cpu_has_pending_timer(struct kvmEXPORT_SYMBOL (kvm_cpu_has_pending_timer);/* +* The PIT interrupt can be masked on different levels: PIC/IOAPIC/LAPIC. +* Hide the maze from the PIT mask notifier, all it cares about is whether +* the interrupt can reach the processor. +*/ +void kvm_pit_mask_notifier (struct kvm_irq_mask_notifier *kimn,bool mask) +{ +bool masked =0 ; +struct kvm *kvm; + struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); + + kvm = pit->kvm; + +/* LAPIC hardware disabled or INTIN0 programmed as ExtINT */ +if (kvm_apic_accept_pic_intr (kvm->vcpus[0 ])) { + masked =pic_int_is_masked (kvm,0 ); +/* IOAPIC -> LAPIC */ + }else + masked =ioapic_int_is_masked (kvm,0 ); + +kvm_pit_internal_mask_notifier (kvm, mask); +} + +/* * check if there is pending interrupt without * intack. */ Index:
kvm/virt/kvm/ioapic.c
===================================================================virt/kvm/ioapic.c +10
-0@@ -101,6 +101,16 @@ static int ioapic_service(struct kvm_ioareturn injected; } +int ioapic_int_is_masked (struct kvm *kvm,int pin) +{ +struct kvm_ioapic *ioapic = kvm->arch.vioapic; + +if (!ioapic) +return 1 ; + +return ioapic->redirtbl[pin].fields.mask; +} +static void ioapic_write_indirect (struct kvm_ioapic *ioapic,u32 val) {unsigned index;Index:
kvm/arch/x86/kvm/i8254.c
===================================================================arch/x86/kvm/i8254.c +4
-4@@ -558,9 +558,9 @@ void kvm_pit_reset(struct kvm_pit *pit) pit->pit_state.irq_ack =1 ; } -static void pit_mask_notifer (struct kvm_irq_mask_notifier *kimn,bool mask) +void kvm_pit_internal_mask_notifier (struct kvm *kvm,bool mask) { - struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); +struct kvm_pit *pit = kvm->arch.vpit;if (!mask) {atomic_set (&pit->pit_state.pit_timer.pending,0 );@@ -614,8 +614,8 @@ struct kvm_pit *kvm_create_pit(struct kvkvm_pit_reset (pit); - pit->mask_notifier.func = pit_mask_notifer; -kvm_register_irq_mask_notifier (kvm,0 , &pit->mask_notifier); + pit->mask_notifier.func = kvm_pit_mask_notifier; +kvm_register_irq_mask_notifier (kvm,0 , &kvm->arch.vpit->mask_notifier);kvm_iodevice_init (&pit->dev, &pit_dev_ops);kvm_io_bus_register_dev (&kvm->pio_bus, &pit->dev);Index:
kvm/arch/x86/kvm/irq.h
===================================================================arch/x86/kvm/irq.h +0
-0@@ -98,7 +98,13 @@ void __kvm_migrate_apic_timer(struct kvmvoid __kvm_migrate_pit_timer (struct kvm_vcpu *vcpu);void __kvm_migrate_timers (struct kvm_vcpu *vcpu);
arch/x86/kvm/irq.h void __kvm_migrate_apic_timer(struct kvm · line 110
Quoted 10 lines
arch/x86/kvm/irq.h +6
-0+void kvm_pit_internal_mask_notifier (struct kvm *kvm,bool mask); +void kvm_pit_mask_notifier (struct kvm_irq_mask_notifier *kimn,bool mask); +int pit_has_pending_timer (struct kvm_vcpu *vcpu);int apic_has_pending_timer (struct kvm_vcpu *vcpu); +int pic_int_is_masked (struct kvm *kvm,int irq); +int ioapic_int_is_masked (struct kvm *kvm,int irq); +#endif
--
Gleb.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Marcelo Tosatti
Quoted 8 lines · Gleb Natapov
Excellent patch series.
Quoted 5 lines · Marcelo Tosatti
int
kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu){(quoted diff) +1
-1- int ret; + ktime_t now, expires;Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +5
-2- ret = pit_has_pending_timer(vcpu); - ret |= apic_has_pending_timer(vcpu); + expires = kvm_vcpu_next_timer_event(vcpu); + now = ktime_get(); + if (expires.tv64 <= now.tv64) { + if (kvm_arch_interrupt_allowed(vcpu)) + set_bit(KVM_REQ_UNHALT, &vcpu->requests);You shouldn't unhalt vcpu here. Not every timer event will generate interrupt (vector can be masked in pic/ioapic)
Yeah. Note however that
kvm_vcpu_next_timer_eventonly returns the expiration time for events that have been acked (for timers that have had events injected, but not acked it returnsKTIME_MAX).So, the code above will set one spurious unhalt if:
- inject timer irq
- guest acks irq
- guest mask irq
- unhalt (once)
Quoted 7 lines
Quoted 5 lines
I had a "
kvm_timer_mask" callback before (along with the attached patch), but decided to keep it simpler using the ack trick above.I suppose one spurious unhalt is harmless, or is it a correctness issue?
This is correctness issue. We should be as close to real CPU as possible. This will save us may hours of debugging later :)
Hum, fine. Will update the kvm_timer_mask patch below and let you know.
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
or timer event can generate NMI instead of interrupt.
In the NMI case it should not unhalt the processor?
Why? It should. It should jump to NMI handler.
I meant unhalt as in KVM_REQ_UNHALT so vcpu_enter_guest runs.
What did you mention about ISR/IRR again?
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 40 lines · Marcelo Tosatti
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Marcelo Tosatti
Quoted 8 lines · Gleb Natapov
Excellent patch series.
Quoted 5 lines · Marcelo Tosatti
int
kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu){(quoted diff) +1
-1- int ret; + ktime_t now, expires;Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +5
-2- ret = pit_has_pending_timer(vcpu); - ret |= apic_has_pending_timer(vcpu); + expires = kvm_vcpu_next_timer_event(vcpu); + now = ktime_get(); + if (expires.tv64 <= now.tv64) { + if (kvm_arch_interrupt_allowed(vcpu)) + set_bit(KVM_REQ_UNHALT, &vcpu->requests);You shouldn't unhalt vcpu here. Not every timer event will generate interrupt (vector can be masked in pic/ioapic)
Yeah. Note however that
kvm_vcpu_next_timer_eventonly returns the expiration time for events that have been acked (for timers that have had events injected, but not acked it returnsKTIME_MAX).So, the code above will set one spurious unhalt if:
- inject timer irq
- guest acks irq
- guest mask irq
- unhalt (once)Quoted 7 lines
Quoted 5 lines
I had a "
kvm_timer_mask" callback before (along with the attached patch), but decided to keep it simpler using the ack trick above.I suppose one spurious unhalt is harmless, or is it a correctness issue?
This is correctness issue. We should be as close to real CPU as possible. This will save us may hours of debugging later :)
Quoted 9 lines
Hum, fine. Will update the
kvm_timer_maskpatch below and let you know.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
or timer event can generate NMI instead of interrupt.
In the NMI case it should not unhalt the processor?
Why? It should. It should jump to NMI handler.
I meant unhalt as in
KVM_REQ_UNHALTsovcpu_enter_guestruns.
Yes. It should. Inside vcpu_enter_guest() NMI will be injected and nmi handler will be executed.
Quoted 1 line
What did you mention about ISR/IRR again?
On real HW the following may happen:
Timer interrupt delivered to apic and placed into IRR Timer interrupt delivered to cpu and moved from IRR to ISR New timer interrupt delivered to apic and placed into IRR before
previous one is acked.
In your patch ktimer->can_inject is set to false when timer is injected and next interrupt is injected only after OS acks previous timer. So the above situation cannot happen. I don't know if this important or not. It is possible to write code that will work only with former behaviour, but I don't see why somebody will want to do that. We can emulate former behaviour though. If we will no rely on acks to count delivered event but make ->inject callback return status that will indicate if interrupt was delivered to apic or not.
--
Gleb.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
In reply to Marcelo TosattiQuoted 4 lines · Marcelo Tosatti
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg
i8254.cNMI injection via all cpus LINT0).
I've looked at this. Why do you say i8254.c NMI injection bypass the irq injection system? It look to me like it uses usual way to send interrupt to all cpus.
--
Gleb.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 8 lines · Gleb Natapov
Quoted 4 lines · Marcelo Tosatti
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg
i8254.cNMI injection via all cpus LINT0).I've looked at this. Why do you say
i8254.cNMI injection bypass the irq injection system? It look to me like it uses usual way to send interrupt to all cpus.
It goes through apic_accept_irq so its somewhat fine. But it accesses the lapics of other vcpus locklessly (which does not happen via kvm_set_irq path due to irq_lock protection).
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 6 lines · Marcelo Tosatti
Quoted 5 lines · Gleb Natapov
Quoted 4 lines · Marcelo Tosatti
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg
i8254.cNMI injection via all cpus LINT0).
Quoted 7 lines
Quoted 3 lines
I've looked at this. Why do you say
i8254.cNMI injection bypass the irq injection system? It look to me like it uses usual way to send interrupt to all cpus.It goes through
apic_accept_irqso its somewhat fine. But it accesses the lapics of other vcpus locklessly (which does not happen viakvm_set_irqpath due toirq_lockprotection).
Ah, that the bug then. But otherwise it goes through usual interrupt logic. Do you know why the lock is missing? Due to potential deadlock or we just forget to lock?
--
Gleb.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Gleb Natapov
Quoted 4 lines · Marcelo Tosatti
(but yes, bypassing the irq injection system its not a very beatiful shortcut, but its done in other places too eg
i8254.cNMI injection via all cpus LINT0).I've looked at this. Why do you say
i8254.cNMI injection bypass the irq injection system? It look to me like it uses usual way to send interrupt to all cpus.
Quoted 6 lines
Quoted 3 lines
It goes through
apic_accept_irqso its somewhat fine. But it accesses the lapics of other vcpus locklessly (which does not happen viakvm_set_irqpath due toirq_lockprotection).Ah, that the bug then. But otherwise it goes through usual interrupt logic. Do you know why the lock is missing? Due to potential deadlock or we just forget to lock?
There is no lock to protect the lapics right, normally isr setting is lockless which is fine, but kvm_apic_local_deliver also reads a register. Hum, should be fine though.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
In reply to Marcelo TosattiQuoted 7 lines · Marcelo Tosatti
Replace hrtimer based timer emulation with host timebase (
ktime_t) comparisons on guest entry.This avoids host load when guests are scheduled out, removes a spinlock acquision on entry (i8254.c's
inject_lock), and makes future improvements easier.
I wonder if we're really winning with this. Guests should be scheduled-out-but-not-halted rarely, and in all other cases we need to keep the timer. A timer comparison on each guest entry might be expensive (maybe not so much with tsc based timers).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Marcelo Tosatti
Replace hrtimer based timer emulation with host timebase (
ktime_t) comparisons on guest entry.This avoids host load when guests are scheduled out, removes a spinlock acquision on entry (i8254.c's
inject_lock), and makes future improvements easier.
Quoted 4 lines
I wonder if we're really winning with this. Guests should be scheduled-out-but-not-halted rarely, and in all other cases we need to keep the timer. A timer comparison on each guest entry might be expensive (maybe not so much with tsc based timers).
Any activity outside of guest mode that takes more than the period of the timer (think 1000Hz) causes unnecessary host load.
Booting a RHEL5 UP without VNC or serial output on an idle host:
timer_int_normal=95416 timer_interrupt_accumulated=873
(and it continues to increase in that rate, roughly 1%).
Now factor in multiple guests, loaded host, and you'll probably see more than that.
Re: [patch 4/8] KVM: x86: replace hrtimer based timer emulation
Quoted 9 lines · Marcelo Tosatti
Quoted 5 lines
I wonder if we're really winning with this. Guests should be scheduled-out-but-not-halted rarely, and in all other cases we need to keep the timer. A timer comparison on each guest entry might be expensive (maybe not so much with tsc based timers).
Any activity outside of guest mode that takes more than the period of the timer (think 1000Hz) causes unnecessary host load.
Quoted 9 lines
Booting a RHEL5 UP without VNC or serial output on an idle host:
timer_int_normal=95416 timer_interrupt_accumulated=873
(and it continues to increase in that rate, roughly 1%).
Now factor in multiple guests, loaded host, and you'll probably see more than that.
If you're using qcow, you may be seeing the non-aio accesses. Otherwise, I can't think what can cause 1ms latency. Random host process? long mmu resync?
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 5/8 KVM: timer: honor noreinject tunable
In reply to Marcelo TosattiSkip events which have been missed by the guest.
arch/x86/kvm/timer.c +17 -0
@@ -58,6 +58,21 @@ void kvm_timer_ack(struct kvm_timer *kti ktimer->can_inject = true; } +static void no_reinject_adjust (struct kvm_timer *ktimer,ktime_t expire) +{ +u64 unacked_events, delta; +ktime_t now; + + now =ktime_get (); + delta =ktime_to_ns (ktime_sub (now, expire)); + unacked_events =0 ; + +if (now.tv64 > expire.tv64) + unacked_events =div_u64 (delta, ktimer->period); +if (unacked_events >1 ) + ktimer->acked_events += unacked_events-1 ; +} +static ktime_t periodic_timer_next_event (struct kvm_timer *ktimer) {ktime_t last_acked_event;
@@ -136,6 +151,8 @@ void kvm_inject_pending_timer_irqs(struc ktimer->ops->inject (ktimer);if (!ktimer->periodic)list_del_init (&ktimer->vcpu_timer); +else if (unlikely (!ktimer->reinject)) +no_reinject_adjust (ktimer, expire); } }
PATCH 6/8 KVM: timer: optimize next_timer_event and vcpu_arm_exit
In reply to Marcelo TosattiWhich reduces the added entry/exit overhead down to ~= 30 cycles.
arch/x86/kvm/timer.c +8 -3
@@ -135,14 +135,15 @@ ktime_t kvm_vcpu_next_timer_event(structvoid kvm_inject_pending_timer_irqs (struct kvm_vcpu *vcpu) {struct kvm_timer *ktimer, *n; -ktime_t now =ktime_get ();list_for_each_entry_safe (ktimer, n, &vcpu->arch.timers, vcpu_timer) { -ktime_t expire; +ktime_t expire, now;if (!ktimer->can_inject)continue ; + now =ktime_get (); + expire =kvm_timer_next_event (ktimer);if (ktime_to_ns (now) <ktime_to_ns (expire))continue ;
@@ -173,8 +174,12 @@ void kvm_vcpu_arm_exit(struct kvm_vcpu * {ktime_t expire;ktime_t now; -struct kvm_timer *ktimer =kvm_vcpu_injectable_timer_event (vcpu); +struct kvm_timer *ktimer; + +if (hrtimer_active (&vcpu->arch.exit_timer)) +return ; + ktimer =kvm_vcpu_injectable_timer_event (vcpu);if (!ktimer)return ;
arch/x86/kvm/x86.c +0 -2
@@ -3567,8 +3567,6 @@ static int vcpu_enter_guest(struct kvm_vpreempt_enable (); -kvm_vcpu_cleanup_timer (vcpu); -down_read (&vcpu->kvm->slots_lock);/*
PATCH 7/8 KVM: PIT: removed unused code
In reply to Marcelo Tosattiarch/x86/kvm/i8254.c +1 -9
@@ -228,10 +228,7 @@ static void kvm_pit_ack_irq(struct kvm_i { struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, irq_ack_notifier); -spin_lock (&ps->inject_lock);kvm_timer_ack (&ps->pit_timer); - ps->irq_ack =1 ; -spin_unlock (&ps->inject_lock); }static void destroy_pit_timer (struct kvm_timer *pt)
@@ -252,7 +249,6 @@ static void create_pit_timer(struct kvm_/* TODO The new value only affected after the retriggered */ kvm_timer_cancel (pt); - ps->irq_ack =1 ;kvm_timer_start (pt, interval, is_period); }
@@ -516,17 +512,14 @@ static void kvm_pit_reset(struct kvm_pitmutex_unlock (&pit->pit_state.lock);kvm_timer_reset (&pit->pit_state.pit_timer); - pit->pit_state.irq_ack =1 ; }static void pit_mask_notifer (struct kvm_irq_mask_notifier *kimn,bool mask) { struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); -if (!mask) { +if (!mask)kvm_timer_reset (&pit->pit_state.pit_timer); - pit->pit_state.irq_ack =1 ; - } }static const struct kvm_io_device_ops pit_dev_ops = {
@@ -587,7 +580,6 @@ struct kvm_pit *kvm_create_pit(struct kvmutex_init (&pit->pit_state.lock);mutex_lock (&pit->pit_state.lock); -spin_lock_init (&pit->pit_state.inject_lock); kvm->arch.vpit = pit; pit->kvm = kvm;
arch/x86/kvm/i8254.h +0 -2
@@ -25,8 +25,6 @@ struct kvm_kpit_state {u32 speaker_data_on;struct mutex lock;struct kvm_pit *pit; -spinlock_t inject_lock; -unsigned long irq_ack;struct kvm_irq_ack_notifier irq_ack_notifier; };
PATCH 8/8 kvmctl: time testcase
In reply to Marcelo Tosattikvm/user/config-x86-common.mak +3 -0
@@ -56,6 +56,9 @@ $(TEST_DIR)/tsc.flat: $(cstart.o) $(TEST $(TEST_DIR)/apic.flat: $(cstart.o) $(TEST_DIR)/apic.o $(TEST_DIR)/vm.o \ $(TEST_DIR)/print.o +$(TEST_DIR)/time.flat: $(cstart.o) $(TEST_DIR)/time.o $(TEST_DIR)/vm.o \ + $(TEST_DIR)/print.o + $(TEST_DIR)/realmode.flat: $(TEST_DIR)/realmode.o $(CC) -m32 -nostdlib -o $@ -Wl,-T,$(TEST_DIR)/realmode.lds $^
kvm/user/config-x86_64.mak +2 -1
@@ -7,6 +7,7 @@ CFLAGS += -D__x86_64__ tests = $(TEST_DIR)/access.flat $(TEST_DIR)/irq.flat $(TEST_DIR)/sieve.flat \ $(TEST_DIR)/simple.flat $(TEST_DIR)/stringio.flat \ $(TEST_DIR)/memtest1.flat $(TEST_DIR)/emulator.flat \ - $(TEST_DIR)/hypercall.flat $(TEST_DIR)/apic.flat + $(TEST_DIR)/hypercall.flat $(TEST_DIR)/apic.flat \ + $(TEST_DIR)/time.flat include config-x86-common.mak
kvm/user/test/x86/io.h new file +35 -0
@@ -0,0 +1,35 @@ +static inline void outb (unsigned char val,unsigned short port) +{ + asmvolatile ("outb %0, %w1" : :"a" (val),"Nd" (port)); +} + +static inline void outw (unsigned short val,unsigned short port) +{ + asmvolatile ("outw %0, %w1" : :"a" (val),"Nd" (port)); +} + +static inline void outl (unsigned long val,unsigned short port) +{ + asmvolatile ("outl %0, %w1" : :"a" (val),"Nd" (port)); +} + +static inline unsigned char inb (unsigned short port) +{ +unsigned char val; + asmvolatile ("inb %w1, %0" :"=a" (val) :"Nd" (port)); +return val; +} + +static inline short inw (unsigned short port) +{ +short val; + asmvolatile ("inw %w1, %0" :"=a" (val) :"Nd" (port)); +return val; +} + +static inline unsigned int inl (unsigned short port) +{ +unsigned int val; + asmvolatile ("inl %w1, %0" :"=a" (val) :"Nd" (port)); +return val; +}
kvm/user/test/x86/time.c new file +766 -0
766 lines not on this page · raw message
Quoted 7 lines · Marcelo Tosatti
This still needs to survive kvm-autotest, posting early for comments.
It replaces hrtimer based emulation with
ktime_tcomparisons on guest entry (avoids host load when guests are scheduled out, removes a spinlock acquision on entry (i8254.c'sinject_lock), and makes future improvements easier).
Overall, very nice. I await the autotest results with interest :)
Signature · 1 line
error compiling committee.c: too many arguments to function