[Xen-devel] [PATCH v4] xen: add steal_clock support on x86
The pv_time_ops structure contains a function pointer for the "steal_clock" functionality used only by KVM and Xen on ARM. Xen on x86 uses its own mechanism to account for the "stolen" time a thread wasn't able to run due to hypervisor scheduling. Add support in Xen arch independent time handling for this feature by moving it out of the arm arch into drivers/xen and remove the x86 Xen hack. Signed-off-by: Juergen Gross Reviewed-by: Boris Ostrovsky --- V4: minor adjustments as requested by Stefano Stabellini (remove no longer needed #include, remove __init from header) V3: add #include to avoid build error on arm V2: remove the x86 do_stolen_accounting() hack --- arch/arm/xen/enlighten.c| 18 ++ arch/x86/xen/time.c | 44 ++-- drivers/xen/time.c | 20 include/linux/kernel_stat.h | 1 - include/xen/xen-ops.h | 1 + kernel/sched/cputime.c | 10 -- 6 files changed, 25 insertions(+), 69 deletions(-) diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c index 75cd734..71db30c 100644 --- a/arch/arm/xen/enlighten.c +++ b/arch/arm/xen/enlighten.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -84,19 +83,6 @@ int xen_unmap_domain_gfn_range(struct vm_area_struct *vma, } EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range); -static unsigned long long xen_stolen_accounting(int cpu) -{ - struct vcpu_runstate_info state; - - BUG_ON(cpu != smp_processor_id()); - - xen_get_runstate_snapshot(&state); - - WARN_ON(state.state != RUNSTATE_running); - - return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline]; -} - static void xen_read_wallclock(struct timespec64 *ts) { u32 version; @@ -355,8 +341,8 @@ static int __init xen_guest_init(void) register_cpu_notifier(&xen_cpu_notifier); - pv_time_ops.steal_clock = xen_stolen_accounting; - static_key_slow_inc(¶virt_steal_enabled); + xen_time_setup_guest(); + if (xen_initial_domain()) pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c index a0a4e55..6be31df 100644 --- a/arch/x86/xen/time.c +++ b/arch/x86/xen/time.c @@ -11,8 +11,6 @@ #include #include #include -#include -#include #include #include #include @@ -31,44 +29,6 @@ /* Xen may fire a timer up to this many ns early */ #define TIMER_SLOP 10 -#define NS_PER_TICK(10LL / HZ) - -/* snapshots of runstate info */ -static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate_snapshot); - -/* unused ns of stolen time */ -static DEFINE_PER_CPU(u64, xen_residual_stolen); - -static void do_stolen_accounting(void) -{ - struct vcpu_runstate_info state; - struct vcpu_runstate_info *snap; - s64 runnable, offline, stolen; - cputime_t ticks; - - xen_get_runstate_snapshot(&state); - - WARN_ON(state.state != RUNSTATE_running); - - snap = this_cpu_ptr(&xen_runstate_snapshot); - - /* work out how much time the VCPU has not been runn*ing* */ - runnable = state.time[RUNSTATE_runnable] - snap->time[RUNSTATE_runnable]; - offline = state.time[RUNSTATE_offline] - snap->time[RUNSTATE_offline]; - - *snap = state; - - /* Add the appropriate number of ticks of stolen time, - including any left-overs from last time. */ - stolen = runnable + offline + __this_cpu_read(xen_residual_stolen); - - if (stolen < 0) - stolen = 0; - - ticks = iter_div_u64_rem(stolen, NS_PER_TICK, &stolen); - __this_cpu_write(xen_residual_stolen, stolen); - account_steal_ticks(ticks); -} /* Get the TSC speed from Xen */ static unsigned long xen_tsc_khz(void) @@ -335,8 +295,6 @@ static irqreturn_t xen_timer_interrupt(int irq, void *dev_id) ret = IRQ_HANDLED; } - do_stolen_accounting(); - return ret; } @@ -431,6 +389,8 @@ static void __init xen_time_init(void) xen_setup_timer(cpu); xen_setup_cpu_clockevents(); + xen_time_setup_guest(); + if (xen_initial_domain()) pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); } diff --git a/drivers/xen/time.c b/drivers/xen/time.c index 7107842..2257b66 100644 --- a/drivers/xen/time.c +++ b/drivers/xen/time.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -75,6 +76,15 @@ bool xen_vcpu_stolen(int vcpu) return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable; } +static u64 xen_steal_clock(int cpu) +{ + struct vcpu_runstate_info state; + + BUG_ON(cpu != smp_processor_id()); + xen_get_runstate_snapshot(&state); + return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline]; +} + void xen_setup_runstate_info(int cpu) { struct vcpu_register_runstate_mem
Re: [Xen-devel] [PATCH v4] xen: add steal_clock support on x86
On Fri, 20 May 2016, Juergen Gross wrote: > The pv_time_ops structure contains a function pointer for the > "steal_clock" functionality used only by KVM and Xen on ARM. Xen on x86 > uses its own mechanism to account for the "stolen" time a thread wasn't > able to run due to hypervisor scheduling. > > Add support in Xen arch independent time handling for this feature by > moving it out of the arm arch into drivers/xen and remove the x86 Xen > hack. > > Signed-off-by: Juergen Gross > Reviewed-by: Boris Ostrovsky Reviewed-by: Stefano Stabellini > V4: minor adjustments as requested by Stefano Stabellini (remove > no longer needed #include, remove __init from header) > V3: add #include to avoid build error on arm > V2: remove the x86 do_stolen_accounting() hack > --- > arch/arm/xen/enlighten.c| 18 ++ > arch/x86/xen/time.c | 44 ++-- > drivers/xen/time.c | 20 > include/linux/kernel_stat.h | 1 - > include/xen/xen-ops.h | 1 + > kernel/sched/cputime.c | 10 -- > 6 files changed, 25 insertions(+), 69 deletions(-) > > diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c > index 75cd734..71db30c 100644 > --- a/arch/arm/xen/enlighten.c > +++ b/arch/arm/xen/enlighten.c > @@ -12,7 +12,6 @@ > #include > #include > #include > -#include > #include > #include > #include > @@ -84,19 +83,6 @@ int xen_unmap_domain_gfn_range(struct vm_area_struct *vma, > } > EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range); > > -static unsigned long long xen_stolen_accounting(int cpu) > -{ > - struct vcpu_runstate_info state; > - > - BUG_ON(cpu != smp_processor_id()); > - > - xen_get_runstate_snapshot(&state); > - > - WARN_ON(state.state != RUNSTATE_running); > - > - return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline]; > -} > - > static void xen_read_wallclock(struct timespec64 *ts) > { > u32 version; > @@ -355,8 +341,8 @@ static int __init xen_guest_init(void) > > register_cpu_notifier(&xen_cpu_notifier); > > - pv_time_ops.steal_clock = xen_stolen_accounting; > - static_key_slow_inc(¶virt_steal_enabled); > + xen_time_setup_guest(); > + > if (xen_initial_domain()) > pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); > > diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c > index a0a4e55..6be31df 100644 > --- a/arch/x86/xen/time.c > +++ b/arch/x86/xen/time.c > @@ -11,8 +11,6 @@ > #include > #include > #include > -#include > -#include > #include > #include > #include > @@ -31,44 +29,6 @@ > > /* Xen may fire a timer up to this many ns early */ > #define TIMER_SLOP 10 > -#define NS_PER_TICK (10LL / HZ) > - > -/* snapshots of runstate info */ > -static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate_snapshot); > - > -/* unused ns of stolen time */ > -static DEFINE_PER_CPU(u64, xen_residual_stolen); > - > -static void do_stolen_accounting(void) > -{ > - struct vcpu_runstate_info state; > - struct vcpu_runstate_info *snap; > - s64 runnable, offline, stolen; > - cputime_t ticks; > - > - xen_get_runstate_snapshot(&state); > - > - WARN_ON(state.state != RUNSTATE_running); > - > - snap = this_cpu_ptr(&xen_runstate_snapshot); > - > - /* work out how much time the VCPU has not been runn*ing* */ > - runnable = state.time[RUNSTATE_runnable] - > snap->time[RUNSTATE_runnable]; > - offline = state.time[RUNSTATE_offline] - snap->time[RUNSTATE_offline]; > - > - *snap = state; > - > - /* Add the appropriate number of ticks of stolen time, > -including any left-overs from last time. */ > - stolen = runnable + offline + __this_cpu_read(xen_residual_stolen); > - > - if (stolen < 0) > - stolen = 0; > - > - ticks = iter_div_u64_rem(stolen, NS_PER_TICK, &stolen); > - __this_cpu_write(xen_residual_stolen, stolen); > - account_steal_ticks(ticks); > -} > > /* Get the TSC speed from Xen */ > static unsigned long xen_tsc_khz(void) > @@ -335,8 +295,6 @@ static irqreturn_t xen_timer_interrupt(int irq, void > *dev_id) > ret = IRQ_HANDLED; > } > > - do_stolen_accounting(); > - > return ret; > } > > @@ -431,6 +389,8 @@ static void __init xen_time_init(void) > xen_setup_timer(cpu); > xen_setup_cpu_clockevents(); > > + xen_time_setup_guest(); > + > if (xen_initial_domain()) > pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); > } > diff --git a/drivers/xen/time.c b/drivers/xen/time.c > index 7107842..2257b66 100644 > --- a/drivers/xen/time.c > +++ b/drivers/xen/time.c > @@ -6,6 +6,7 @@ > #include > #include > > +#include > #include > #include > > @@ -75,6 +76,15 @@ bool xen_vcpu_stolen(int vcpu) > return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable; > } > > +static u64 xen_steal_
Re: [Xen-devel] [PATCH v4] xen: add steal_clock support on x86
On 20/05/16 08:26, Juergen Gross wrote: > The pv_time_ops structure contains a function pointer for the > "steal_clock" functionality used only by KVM and Xen on ARM. Xen on x86 > uses its own mechanism to account for the "stolen" time a thread wasn't > able to run due to hypervisor scheduling. > > Add support in Xen arch independent time handling for this feature by > moving it out of the arm arch into drivers/xen and remove the x86 Xen > hack. Applied for-linus-4.8, thanks. David ___ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel