kvm_timer cleanup
cover kvm_timer cleanup
While preparing to fix up KVM_REQ_PENDING_TIMERS, and looking for a bug that turned out not to exist, I cleaned up kvm_timer a little.
Avi Kivity (4):
KVM: Remove internal timer abstraction.
KVM: Simplify kvm_timer
KVM: Simplify kvm_pit_timer
KVM: fold kvm_pit_timer into kvm_kpit_state
arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/i8254.c | 64 +++++++++++++++++++----------------------------- arch/x86/kvm/i8254.h | 6 ++++- arch/x86/kvm/kvm_timer.h | 18 -------------- arch/x86/kvm/lapic.c | 42 ++++++++++++++++++++++--------- arch/x86/kvm/lapic.h | 9 ++++++- arch/x86/kvm/timer.c | 47 ----------------------------------- arch/x86/kvm/x86.c | 2 +- 8 files changed, 71 insertions(+), 119 deletions(-) delete mode 100644 arch/x86/kvm/kvm_timer.h delete mode 100644 arch/x86/kvm/timer.c
Signature · 1 line
1.7.11.3
PATCH 1/4 KVM: Remove internal timer abstraction
kvm_timer_fn(), the sole inhabitant of timer.c, is only used by lapic.c. Move it there to make it easier to hack on it.
struct kvm_timer is a thin wrapper around hrtimer, and only adds obfuscation. Move near its two users (with different names) to prepare for simplification.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/i8254.c | 8 ++++---- arch/x86/kvm/i8254.h | 18 +++++++++++++++++- arch/x86/kvm/kvm_timer.h | 18 ------------------ arch/x86/kvm/lapic.c | 30 +++++++++++++++++++++++++++++- arch/x86/kvm/lapic.h | 17 ++++++++++++++++- arch/x86/kvm/timer.c | 47 ----------------------------------------------- 7 files changed, 67 insertions(+), 73 deletions(-) delete mode 100644 arch/x86/kvm/kvm_timer.h delete mode 100644 arch/x86/kvm/timer.c
arch/x86/kvm/Makefile +1 -1
@@ -12,7 +12,7 @@ kvm-$(CONFIG_IOMMU_API) += $(addprefix ../../../virt/kvm/, iommu.o) kvm-$(CONFIG_KVM_ASYNC_PF) += $(addprefix ../../../virt/kvm/, async_pf.o) kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ - i8254.o timer.o cpuid.o pmu.o + i8254.o cpuid.o pmu.o kvm-intel-y += vmx.o kvm-amd-y += svm.o
arch/x86/kvm/i8254.c +4 -4
@@ -272,14 +272,14 @@ static void destroy_pit_timer(struct kvm_pit *pit)flush_kthread_work (&pit->expired); } -static bool kpit_is_periodic (struct kvm_timer *ktimer) +static bool kpit_is_periodic (struct kvm_pit_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 = { +static struct kvm_pit_timer_ops kpit_ops = { .is_periodic = kpit_is_periodic, };
@@ -322,7 +322,7 @@ static void pit_do_work(struct kthread_work *work)static enum hrtimer_restart pit_timer_fn (struct hrtimer *data) { - struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); + struct kvm_pit_timer *ktimer = container_of(data, struct kvm_pit_timer, timer);struct kvm_pit *pt = ktimer->kvm->arch.vpit;if (ktimer->reinject || !atomic_read (&ktimer->pending)) {
@@ -340,7 +340,7 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)static void create_pit_timer (struct kvm *kvm,u32 val,int is_period) {struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; -struct kvm_timer *pt = &ps->pit_timer; +struct kvm_pit_timer *pt = &ps->pit_timer;s64 interval;if (!irqchip_in_kernel (kvm) || ps->flags &KVM_PIT_FLAGS_HPET_LEGACY )
arch/x86/kvm/i8254.h +17 -1
@@ -21,10 +21,26 @@ struct kvm_kpit_channel_state {ktime_t count_load_time; }; +struct kvm_pit_timer { +struct hrtimer timer; +s64 period;/* unit: ns */ +u32 timer_mode_mask; +u64 tscdeadline; +atomic_t pending;/* accumulated triggered timers */ +bool reinject; +struct kvm_pit_timer_ops *t_ops; +struct kvm *kvm; +struct kvm_vcpu *vcpu; +}; + +struct kvm_pit_timer_ops { +bool (*is_periodic)(struct kvm_pit_timer *); +}; +struct kvm_kpit_state {struct kvm_kpit_channel_state channels[3 ];u32 flags; -struct kvm_timer pit_timer; +struct kvm_pit_timer pit_timer;bool is_periodic;u32 speaker_data_on;struct mutex lock;
arch/x86/kvm/kvm_timer.h deleted +0 -18
@@ -1,18 +0,0 @@ - -struct kvm_timer { -struct hrtimer timer; -s64 period;/* unit: ns */ -u32 timer_mode_mask; -u64 tscdeadline; -atomic_t pending;/* accumulated triggered timers */ -bool reinject; -struct kvm_timer_ops *t_ops; -struct kvm *kvm; -struct kvm_vcpu *vcpu; -}; - -struct kvm_timer_ops { -bool (*is_periodic)(struct kvm_timer *); -}; - -enum hrtimer_restart kvm_timer_fn (struct hrtimer *data);
arch/x86/kvm/lapic.c +29 -1
@@ -1260,6 +1260,34 @@ void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu) .write = apic_mmio_write, }; +static enum hrtimer_restart apic_timer_fn (struct hrtimer *data) +{ + struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); +struct kvm_vcpu *vcpu = ktimer->vcpu; +wait_queue_head_t *q = &vcpu->wq; + +/* +* There is a race window between reading and incrementing, but we do +* not care about potentially losing timer events in the !reinject +* case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked +* in vcpu_enter_guest. +*/ +if (ktimer->reinject || !atomic_read (&ktimer->pending)) { +atomic_inc (&ktimer->pending); +/* FIXME: this code should not know anything about vcpus */ +kvm_make_request (KVM_REQ_PENDING_TIMER , vcpu); + } + +if (waitqueue_active (q)) +wake_up_interruptible (q); + +if (ktimer->t_ops->is_periodic (ktimer)) { +hrtimer_add_expires_ns (&ktimer->timer, ktimer->period); +return HRTIMER_RESTART ; + }else +return HRTIMER_NORESTART ; +} +int kvm_create_lapic (struct kvm_vcpu *vcpu) {struct kvm_lapic *apic;
@@ -1283,7 +1311,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)hrtimer_init (&apic->lapic_timer.timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); - apic->lapic_timer.timer.function = kvm_timer_fn; + apic->lapic_timer.timer.function = apic_timer_fn; apic->lapic_timer.t_ops = &lapic_timer_ops; apic->lapic_timer.kvm = vcpu->kvm; apic->lapic_timer.vcpu = vcpu;
arch/x86/kvm/lapic.h +16 -1
@@ -2,10 +2,25 @@#define __KVM_X86_LAPIC_H#include "iodev.h" -#include "kvm_timer.h" #include <linux/kvm_host.h> +struct kvm_timer { +struct hrtimer timer; +s64 period;/* unit: ns */ +u32 timer_mode_mask; +u64 tscdeadline; +atomic_t pending;/* accumulated triggered timers */ +bool reinject; +struct kvm_timer_ops *t_ops; +struct kvm *kvm; +struct kvm_vcpu *vcpu; +}; + +struct kvm_timer_ops { +bool (*is_periodic)(struct kvm_timer *); +}; +struct kvm_lapic {unsigned long base_address;struct kvm_io_device dev;
arch/x86/kvm/timer.c deleted +0 -47
47 lines not on this page · raw message
Signature · 1 line
1.7.11.3
PATCH 2/4 KVM: Simplify kvm_timer
In reply to Avi Kivity'reinject' is never initialized
't_ops' only serves as indirection to lapic_is_periodic; call that directly
instead
'kvm' is never used
'vcpu' can be derived via container_of
Remove these fields.
--- arch/x86/kvm/lapic.c | 18 +++++------------- arch/x86/kvm/lapic.h | 8 -------- 2 files changed, 5 insertions(+), 21 deletions(-)
arch/x86/kvm/lapic.c +5 -13
@@ -1212,10 +1212,8 @@ int kvm_lapic_enabled(struct kvm_vcpu *vcpu)*---------------------------------------------------------------------- */ -static bool lapic_is_periodic (struct kvm_timer *ktimer) +static bool lapic_is_periodic (struct kvm_lapic *apic) { - struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, - lapic_timer);return apic_lvtt_period (apic); }
@@ -1251,10 +1249,6 @@ void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)kvm_apic_local_deliver (apic,APIC_LVT0 ); } -static struct kvm_timer_ops lapic_timer_ops = { - .is_periodic = lapic_is_periodic, -}; -static const struct kvm_io_device_ops apic_mmio_ops = { .read = apic_mmio_read, .write = apic_mmio_write,
@@ -1263,7 +1257,8 @@ void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)static enum hrtimer_restart apic_timer_fn (struct hrtimer *data) { struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); -struct kvm_vcpu *vcpu = ktimer->vcpu; + struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer); +struct kvm_vcpu *vcpu = apic->vcpu;wait_queue_head_t *q = &vcpu->wq;/*
@@ -1272,7 +1267,7 @@ static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)* case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked * in vcpu_enter_guest. */ -if (ktimer->reinject || !atomic_read (&ktimer->pending)) { +if (!atomic_read (&ktimer->pending)) {atomic_inc (&ktimer->pending);/* FIXME: this code should not know anything about vcpus */ kvm_make_request (KVM_REQ_PENDING_TIMER , vcpu);
@@ -1281,7 +1276,7 @@ static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)if (waitqueue_active (q))wake_up_interruptible (q); -if (ktimer->t_ops->is_periodic (ktimer)) { +if (lapic_is_periodic (apic)) {hrtimer_add_expires_ns (&ktimer->timer, ktimer->period);return HRTIMER_RESTART ; }else
@@ -1312,9 +1307,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)hrtimer_init (&apic->lapic_timer.timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); apic->lapic_timer.timer.function = apic_timer_fn; - apic->lapic_timer.t_ops = &lapic_timer_ops; - apic->lapic_timer.kvm = vcpu->kvm; - apic->lapic_timer.vcpu = vcpu; apic->base_address =APIC_DEFAULT_PHYS_BASE ; vcpu->arch.apic_base =APIC_DEFAULT_PHYS_BASE ;
arch/x86/kvm/lapic.h +0 -8
@@ -11,14 +11,6 @@ struct kvm_timer {u32 timer_mode_mask;u64 tscdeadline;atomic_t pending;/* accumulated triggered timers */ -bool reinject; -struct kvm_timer_ops *t_ops; -struct kvm *kvm; -struct kvm_vcpu *vcpu; -}; - -struct kvm_timer_ops { -bool (*is_periodic)(struct kvm_timer *); };struct kvm_lapic {
Signature · 1 line
1.7.11.3
PATCH 3/4 KVM: Simplify kvm_pit_timer
In reply to Avi Kivity'timer_mode_mask' is unused
'tscdeadline' is unused
't_ops' only adds needless indirection
'vcpu' is unused
Remove.
--- arch/x86/kvm/i8254.c | 14 +------------- arch/x86/kvm/i8254.h | 8 -------- 2 files changed, 1 insertion(+), 21 deletions(-)
arch/x86/kvm/i8254.c +1 -13
@@ -272,17 +272,6 @@ static void destroy_pit_timer(struct kvm_pit *pit)flush_kthread_work (&pit->expired); } -static bool kpit_is_periodic (struct kvm_pit_timer *ktimer) -{ - struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state, - pit_timer); -return ps->is_periodic; -} - -static struct kvm_pit_timer_ops kpit_ops = { - .is_periodic = kpit_is_periodic, -}; -static void pit_do_work (struct kthread_work *work) { struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
@@ -330,7 +319,7 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)queue_kthread_work (&pt->worker, &pt->expired); } -if (ktimer->t_ops->is_periodic (ktimer)) { +if (pt->pit_state.is_periodic) {hrtimer_add_expires_ns (&ktimer->timer, ktimer->period);return HRTIMER_RESTART ; }else
@@ -357,7 +346,6 @@ static void create_pit_timer(struct kvm *kvm, u32 val, int is_period) ps->is_periodic = is_period; pt->timer.function = pit_timer_fn; - pt->t_ops = &kpit_ops; pt->kvm = ps->pit->kvm;atomic_set (&pt->pending,0 );
arch/x86/kvm/i8254.h +0 -8
@@ -24,17 +24,9 @@ struct kvm_kpit_channel_state {struct kvm_pit_timer {struct hrtimer timer;s64 period;/* unit: ns */ -u32 timer_mode_mask; -u64 tscdeadline;atomic_t pending;/* accumulated triggered timers */ bool reinject; -struct kvm_pit_timer_ops *t_ops;struct kvm *kvm; -struct kvm_vcpu *vcpu; -}; - -struct kvm_pit_timer_ops { -bool (*is_periodic)(struct kvm_pit_timer *); };struct kvm_kpit_state {
Signature · 1 line
1.7.11.3
PATCH 4/4 KVM: fold kvm_pit_timer into kvm_kpit_state
In reply to Avi KivityOne structure nests inside the other, providing no value at all.
--- arch/x86/kvm/i8254.c | 52 +++++++++++++++++++++++++--------------------------- arch/x86/kvm/i8254.h | 14 +++++--------- arch/x86/kvm/x86.c | 2 +- 3 files changed, 31 insertions(+), 37 deletions(-)
arch/x86/kvm/i8254.c +25 -27
@@ -108,7 +108,7 @@ static s64 __kpit_elapsed(struct kvm *kvm)ktime_t remaining;struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; -if (!ps->pit_timer.period) +if (!ps->period)return 0 ;/*
@@ -120,9 +120,9 @@ static s64 __kpit_elapsed(struct kvm *kvm)* itself with the initial count and continues counting * from there. */ - remaining =hrtimer_get_remaining (&ps->pit_timer.timer); - elapsed = ps->pit_timer.period -ktime_to_ns (remaining); - elapsed =mod_64 (elapsed, ps->pit_timer.period); + remaining =hrtimer_get_remaining (&ps->timer); + elapsed = ps->period -ktime_to_ns (remaining); + elapsed =mod_64 (elapsed, ps->period);return elapsed; }
@@ -238,12 +238,12 @@ static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)int value;spin_lock (&ps->inject_lock); - value =atomic_dec_return (&ps->pit_timer.pending); + value =atomic_dec_return (&ps->pending);if (value <0 )/* spurious acks can be generated if, for example, the * PIC is being reset. Handle it gracefully here */ -atomic_inc (&ps->pit_timer.pending); +atomic_inc (&ps->pending);else if (value >0 )/* in this case, we had multiple outstanding pit interrupts * that we needed to inject. Reinject
@@ -261,14 +261,14 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)if (!kvm_vcpu_is_bsp (vcpu) || !pit)return ; - timer = &pit->pit_state.pit_timer.timer; + timer = &pit->pit_state.timer;if (hrtimer_cancel (timer))hrtimer_start_expires (timer,HRTIMER_MODE_ABS ); }static void destroy_pit_timer (struct kvm_pit *pit) { -hrtimer_cancel (&pit->pit_state.pit_timer.timer); +hrtimer_cancel (&pit->pit_state.timer);flush_kthread_work (&pit->expired); }
@@ -311,16 +311,16 @@ static void pit_do_work(struct kthread_work *work)static enum hrtimer_restart pit_timer_fn (struct hrtimer *data) { - struct kvm_pit_timer *ktimer = container_of(data, struct kvm_pit_timer, timer); -struct kvm_pit *pt = ktimer->kvm->arch.vpit; + struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer); +struct kvm_pit *pt = ps->kvm->arch.vpit; -if (ktimer->reinject || !atomic_read (&ktimer->pending)) { -atomic_inc (&ktimer->pending); +if (ps->reinject || !atomic_read (&ps->pending)) { +atomic_inc (&ps->pending);queue_kthread_work (&pt->worker, &pt->expired); } -if (pt->pit_state.is_periodic) { -hrtimer_add_expires_ns (&ktimer->timer, ktimer->period); +if (ps->is_periodic) { +hrtimer_add_expires_ns (&ps->timer, ps->period);return HRTIMER_RESTART ; }else return HRTIMER_NORESTART ;
@@ -329,7 +329,6 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)static void create_pit_timer (struct kvm *kvm,u32 val,int is_period) {struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; -struct kvm_pit_timer *pt = &ps->pit_timer;s64 interval;if (!irqchip_in_kernel (kvm) || ps->flags &KVM_PIT_FLAGS_HPET_LEGACY )
@@ -340,18 +339,18 @@ static void create_pit_timer(struct kvm *kvm, u32 val, int is_period)pr_debug ("create pit timer, interval is %llu nsec\n" , interval);/* TODO The new value only affected after the retriggered */ -hrtimer_cancel (&pt->timer); +hrtimer_cancel (&ps->timer);flush_kthread_work (&ps->pit->expired); - pt->period = interval; + ps->period = interval; ps->is_periodic = is_period; - pt->timer.function = pit_timer_fn; - pt->kvm = ps->pit->kvm; + ps->timer.function = pit_timer_fn; + ps->kvm = ps->pit->kvm; -atomic_set (&pt->pending,0 ); +atomic_set (&ps->pending,0 ); ps->irq_ack =1 ; -hrtimer_start (&pt->timer,ktime_add_ns (ktime_get (), interval), +hrtimer_start (&ps->timer,ktime_add_ns (ktime_get (), interval),HRTIMER_MODE_ABS ); }
@@ -627,7 +626,7 @@ void kvm_pit_reset(struct kvm_pit *pit) }mutex_unlock (&pit->pit_state.lock); -atomic_set (&pit->pit_state.pit_timer.pending,0 ); +atomic_set (&pit->pit_state.pending,0 ); pit->pit_state.irq_ack =1 ; }
@@ -636,7 +635,7 @@ 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) { -atomic_set (&pit->pit_state.pit_timer.pending,0 ); +atomic_set (&pit->pit_state.pending,0 ); pit->pit_state.irq_ack =1 ; } }
@@ -694,12 +693,11 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) pit_state = &pit->pit_state; pit_state->pit = pit; -hrtimer_init (&pit_state->pit_timer.timer, -CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); +hrtimer_init (&pit_state->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); 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); - pit_state->pit_timer.reinject = true; + pit_state->reinject = true;mutex_unlock (&pit->pit_state.lock);kvm_pit_reset (pit);
@@ -749,7 +747,7 @@ void kvm_free_pit(struct kvm *kvm)kvm_unregister_irq_ack_notifier (kvm, &kvm->arch.vpit->pit_state.irq_ack_notifier);mutex_lock (&kvm->arch.vpit->pit_state.lock); - timer = &kvm->arch.vpit->pit_state.pit_timer.timer; + timer = &kvm->arch.vpit->pit_state.timer;hrtimer_cancel (timer);flush_kthread_work (&kvm->arch.vpit->expired);kthread_stop (kvm->arch.vpit->worker_task);
arch/x86/kvm/i8254.h +5 -9
@@ -21,19 +21,15 @@ struct kvm_kpit_channel_state {ktime_t count_load_time; }; -struct kvm_pit_timer { -struct hrtimer timer; -s64 period;/* unit: ns */ -atomic_t pending;/* accumulated triggered timers */ -bool reinject; -struct kvm *kvm; -}; -struct kvm_kpit_state {struct kvm_kpit_channel_state channels[3 ];u32 flags; -struct kvm_pit_timer pit_timer;bool is_periodic; +s64 period;/* unit: ns */ +struct hrtimer timer; +atomic_t pending;/* accumulated triggered timers */ +bool reinject; +struct kvm *kvm;u32 speaker_data_on;struct mutex lock;struct kvm_pit *pit;
arch/x86/kvm/x86.c +1 -1
@@ -3082,7 +3082,7 @@ static int kvm_vm_ioctl_reinject(struct kvm *kvm,if (!kvm->arch.vpit)return -ENXIO ;mutex_lock (&kvm->arch.vpit->pit_state.lock); - kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; + kvm->arch.vpit->pit_state.reinject = control->pit_reinject;mutex_unlock (&kvm->arch.vpit->pit_state.lock);return 0 ; }
Signature · 1 line
1.7.11.3
Marcelo Tosatti
AppliedQuoted 8 lines · Avi Kivity
While preparing to fix up
KVM_REQ_PENDING_TIMERS, and looking for a bug that turned out not to exist, I cleaned upkvm_timera little.Avi Kivity (4):
KVM: Remove internal timer abstraction.
KVM: Simplifykvm_timer
KVM: Simplifykvm_pit_timer
KVM: foldkvm_pit_timerintokvm_kpit_state
Applied, thanks.