Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull timer core updates from Thomas Gleixner:
"The timer changes contain:

- posix timer code consolidation and fixes for odd corner cases

- sched_clock implementation moved from ARM to core code to avoid
duplication by other architectures

- alarm timer updates

- clocksource and clockevents unregistration facilities

- clocksource/events support for new hardware

- precise nanoseconds RTC readout (Xen feature)

- generic support for Xen suspend/resume oddities

- the usual lot of fixes and cleanups all over the place

The parts which touch other areas (ARM/XEN) have been coordinated with
the relevant maintainers. Though this results in an handful of
trivial to solve merge conflicts, which we preferred over nasty cross
tree merge dependencies.

The patches which have been committed in the last few days are bug
fixes plus the posix timer lot. The latter was in akpms queue and
next for quite some time; they just got forgotten and Frederic
collected them last minute."

* 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (59 commits)
hrtimer: Remove unused variable
hrtimers: Move SMP function call to thread context
clocksource: Reselect clocksource when watchdog validated high-res capability
posix-cpu-timers: don't account cpu timer after stopped thread runtime accounting
posix_timers: fix racy timer delta caching on task exit
posix-timers: correctly get dying task time sample in posix_cpu_timer_schedule()
selftests: add basic posix timers selftests
posix_cpu_timers: consolidate expired timers check
posix_cpu_timers: consolidate timer list cleanups
posix_cpu_timer: consolidate expiry time type
tick: Sanitize broadcast control logic
tick: Prevent uncontrolled switch to oneshot mode
tick: Make oneshot broadcast robust vs. CPU offlining
x86: xen: Sync the CMOS RTC as well as the Xen wallclock
x86: xen: Sync the wallclock when the system time is set
timekeeping: Indicate that clock was set in the pvclock gtod notifier
timekeeping: Pass flags instead of multiple bools to timekeeping_update()
xen: Remove clock_was_set() call in the resume path
hrtimers: Support resuming with two or more CPUs online (but stopped)
timer: Fix jiffies wrap behavior of round_jiffies_common()
...

+2053 -706
+33
Documentation/devicetree/bindings/timer/lsi,zevio-timer.txt
··· 1 + TI-NSPIRE timer 2 + 3 + Required properties: 4 + 5 + - compatible : should be "lsi,zevio-timer". 6 + - reg : The physical base address and size of the timer (always first). 7 + - clocks: phandle to the source clock. 8 + 9 + Optional properties: 10 + 11 + - interrupts : The interrupt number of the first timer. 12 + - reg : The interrupt acknowledgement registers 13 + (always after timer base address) 14 + 15 + If any of the optional properties are not given, the timer is added as a 16 + clock-source only. 17 + 18 + Example: 19 + 20 + timer { 21 + compatible = "lsi,zevio-timer"; 22 + reg = <0x900D0000 0x1000>, <0x900A0020 0x8>; 23 + interrupts = <19>; 24 + clocks = <&timer_clk>; 25 + }; 26 + 27 + Example (no clock-events): 28 + 29 + timer { 30 + compatible = "lsi,zevio-timer"; 31 + reg = <0x900D0000 0x1000>; 32 + clocks = <&timer_clk>; 33 + };
+1
arch/arm/Kconfig
··· 14 14 select GENERIC_IRQ_PROBE 15 15 select GENERIC_IRQ_SHOW 16 16 select GENERIC_PCI_IOMAP 17 + select GENERIC_SCHED_CLOCK 17 18 select GENERIC_SMP_IDLE_THREAD 18 19 select GENERIC_IDLE_POLL_SETUP 19 20 select GENERIC_STRNCPY_FROM_USER
+1 -1
arch/arm/common/timer-sp.c
··· 28 28 #include <linux/of.h> 29 29 #include <linux/of_address.h> 30 30 #include <linux/of_irq.h> 31 + #include <linux/sched_clock.h> 31 32 32 - #include <asm/sched_clock.h> 33 33 #include <asm/hardware/arm_timer.h> 34 34 #include <asm/hardware/timer-sp.h> 35 35
+3 -15
arch/arm/include/asm/sched_clock.h
··· 1 - /* 2 - * sched_clock.h: support for extending counters to full 64-bit ns counter 3 - * 4 - * This program is free software; you can redistribute it and/or modify 5 - * it under the terms of the GNU General Public License version 2 as 6 - * published by the Free Software Foundation. 1 + /* You shouldn't include this file. Use linux/sched_clock.h instead. 2 + * Temporary file until all asm/sched_clock.h users are gone 7 3 */ 8 - #ifndef ASM_SCHED_CLOCK 9 - #define ASM_SCHED_CLOCK 10 - 11 - extern void sched_clock_postinit(void); 12 - extern void setup_sched_clock(u32 (*read)(void), int bits, unsigned long rate); 13 - 14 - extern unsigned long long (*sched_clock_func)(void); 15 - 16 - #endif 4 + #include <linux/sched_clock.h>
+1 -1
arch/arm/kernel/Makefile
··· 16 16 # Object file lists. 17 17 18 18 obj-y := elf.o entry-common.o irq.o opcodes.o \ 19 - process.o ptrace.o return_address.o sched_clock.o \ 19 + process.o ptrace.o return_address.o \ 20 20 setup.o signal.o stacktrace.o sys_arm.o time.o traps.o 21 21 22 22 obj-$(CONFIG_ATAGS) += atags_parse.o
+1 -1
arch/arm/kernel/arch_timer.c
··· 11 11 #include <linux/init.h> 12 12 #include <linux/types.h> 13 13 #include <linux/errno.h> 14 + #include <linux/sched_clock.h> 14 15 15 16 #include <asm/delay.h> 16 - #include <asm/sched_clock.h> 17 17 18 18 #include <clocksource/arm_arch_timer.h> 19 19
+6 -11
arch/arm/kernel/sched_clock.c kernel/time/sched_clock.c
··· 13 13 #include <linux/sched.h> 14 14 #include <linux/syscore_ops.h> 15 15 #include <linux/timer.h> 16 - 17 - #include <asm/sched_clock.h> 16 + #include <linux/sched_clock.h> 18 17 19 18 struct clock_data { 20 19 u64 epoch_ns; ··· 23 24 u32 mult; 24 25 u32 shift; 25 26 bool suspended; 26 - bool needs_suspend; 27 27 }; 28 28 29 29 static void sched_clock_poll(unsigned long wrap_ticks); ··· 49 51 return (cyc * mult) >> shift; 50 52 } 51 53 52 - static unsigned long long notrace cyc_to_sched_clock(u32 cyc, u32 mask) 54 + static unsigned long long notrace sched_clock_32(void) 53 55 { 54 56 u64 epoch_ns; 55 57 u32 epoch_cyc; 58 + u32 cyc; 56 59 57 60 if (cd.suspended) 58 61 return cd.epoch_ns; ··· 72 73 smp_rmb(); 73 74 } while (epoch_cyc != cd.epoch_cyc_copy); 74 75 75 - return epoch_ns + cyc_to_ns((cyc - epoch_cyc) & mask, cd.mult, cd.shift); 76 + cyc = read_sched_clock(); 77 + cyc = (cyc - epoch_cyc) & sched_clock_mask; 78 + return epoch_ns + cyc_to_ns(cyc, cd.mult, cd.shift); 76 79 } 77 80 78 81 /* ··· 164 163 enable_sched_clock_irqtime(); 165 164 166 165 pr_debug("Registered %pF as sched_clock source\n", read); 167 - } 168 - 169 - static unsigned long long notrace sched_clock_32(void) 170 - { 171 - u32 cyc = read_sched_clock(); 172 - return cyc_to_sched_clock(cyc, sched_clock_mask); 173 166 } 174 167 175 168 unsigned long long __read_mostly (*sched_clock_func)(void) = sched_clock_32;
+1 -3
arch/arm/kernel/time.c
··· 24 24 #include <linux/timer.h> 25 25 #include <linux/clocksource.h> 26 26 #include <linux/irq.h> 27 + #include <linux/sched_clock.h> 27 28 28 29 #include <asm/thread_info.h> 29 - #include <asm/sched_clock.h> 30 30 #include <asm/stacktrace.h> 31 31 #include <asm/mach/arch.h> 32 32 #include <asm/mach/time.h> ··· 120 120 machine_desc->init_time(); 121 121 else 122 122 clocksource_of_init(); 123 - 124 - sched_clock_postinit(); 125 123 }
+1 -1
arch/arm/mach-davinci/time.c
··· 18 18 #include <linux/clk.h> 19 19 #include <linux/err.h> 20 20 #include <linux/platform_device.h> 21 + #include <linux/sched_clock.h> 21 22 22 - #include <asm/sched_clock.h> 23 23 #include <asm/mach/irq.h> 24 24 #include <asm/mach/time.h> 25 25
+1 -1
arch/arm/mach-imx/time.c
··· 26 26 #include <linux/clockchips.h> 27 27 #include <linux/clk.h> 28 28 #include <linux/err.h> 29 + #include <linux/sched_clock.h> 29 30 30 - #include <asm/sched_clock.h> 31 31 #include <asm/mach/time.h> 32 32 33 33 #include "common.h"
+1 -1
arch/arm/mach-integrator/integrator_ap.c
··· 41 41 #include <linux/stat.h> 42 42 #include <linux/sys_soc.h> 43 43 #include <linux/termios.h> 44 + #include <linux/sched_clock.h> 44 45 45 46 #include <mach/hardware.h> 46 47 #include <mach/platform.h> ··· 49 48 #include <asm/setup.h> 50 49 #include <asm/param.h> /* HZ */ 51 50 #include <asm/mach-types.h> 52 - #include <asm/sched_clock.h> 53 51 54 52 #include <mach/lm.h> 55 53 #include <mach/irqs.h>
+1 -1
arch/arm/mach-ixp4xx/common.c
··· 30 30 #include <linux/export.h> 31 31 #include <linux/gpio.h> 32 32 #include <linux/cpu.h> 33 + #include <linux/sched_clock.h> 33 34 34 35 #include <mach/udc.h> 35 36 #include <mach/hardware.h> ··· 39 38 #include <asm/pgtable.h> 40 39 #include <asm/page.h> 41 40 #include <asm/irq.h> 42 - #include <asm/sched_clock.h> 43 41 #include <asm/system_misc.h> 44 42 45 43 #include <asm/mach/map.h>
+1 -1
arch/arm/mach-mmp/time.c
··· 28 28 #include <linux/of.h> 29 29 #include <linux/of_address.h> 30 30 #include <linux/of_irq.h> 31 + #include <linux/sched_clock.h> 31 32 32 - #include <asm/sched_clock.h> 33 33 #include <mach/addr-map.h> 34 34 #include <mach/regs-timers.h> 35 35 #include <mach/regs-apbc.h>
+1 -1
arch/arm/mach-msm/timer.c
··· 23 23 #include <linux/of.h> 24 24 #include <linux/of_address.h> 25 25 #include <linux/of_irq.h> 26 + #include <linux/sched_clock.h> 26 27 27 28 #include <asm/mach/time.h> 28 29 #include <asm/localtimer.h> 29 - #include <asm/sched_clock.h> 30 30 31 31 #include "common.h" 32 32
+1 -1
arch/arm/mach-omap1/time.c
··· 43 43 #include <linux/clocksource.h> 44 44 #include <linux/clockchips.h> 45 45 #include <linux/io.h> 46 + #include <linux/sched_clock.h> 46 47 47 48 #include <asm/irq.h> 48 - #include <asm/sched_clock.h> 49 49 50 50 #include <mach/hardware.h> 51 51 #include <asm/mach/irq.h>
+1 -1
arch/arm/mach-omap2/timer.c
··· 41 41 #include <linux/of_irq.h> 42 42 #include <linux/platform_device.h> 43 43 #include <linux/platform_data/dmtimer-omap.h> 44 + #include <linux/sched_clock.h> 44 45 45 46 #include <asm/mach/time.h> 46 47 #include <asm/smp_twd.h> 47 - #include <asm/sched_clock.h> 48 48 49 49 #include "omap_hwmod.h" 50 50 #include "omap_device.h"
+1 -1
arch/arm/mach-pxa/time.c
··· 16 16 #include <linux/init.h> 17 17 #include <linux/interrupt.h> 18 18 #include <linux/clockchips.h> 19 + #include <linux/sched_clock.h> 19 20 20 21 #include <asm/div64.h> 21 22 #include <asm/mach/irq.h> 22 23 #include <asm/mach/time.h> 23 - #include <asm/sched_clock.h> 24 24 #include <mach/regs-ost.h> 25 25 #include <mach/irqs.h> 26 26
+1 -1
arch/arm/mach-sa1100/time.c
··· 14 14 #include <linux/irq.h> 15 15 #include <linux/timex.h> 16 16 #include <linux/clockchips.h> 17 + #include <linux/sched_clock.h> 17 18 18 19 #include <asm/mach/time.h> 19 - #include <asm/sched_clock.h> 20 20 #include <mach/hardware.h> 21 21 #include <mach/irqs.h> 22 22
+1 -1
arch/arm/mach-u300/timer.c
··· 21 21 #include <linux/delay.h> 22 22 #include <linux/of_address.h> 23 23 #include <linux/of_irq.h> 24 + #include <linux/sched_clock.h> 24 25 25 26 /* Generic stuff */ 26 - #include <asm/sched_clock.h> 27 27 #include <asm/mach/map.h> 28 28 #include <asm/mach/time.h> 29 29
+1 -1
arch/arm/plat-iop/time.c
··· 22 22 #include <linux/clocksource.h> 23 23 #include <linux/clockchips.h> 24 24 #include <linux/export.h> 25 + #include <linux/sched_clock.h> 25 26 #include <mach/hardware.h> 26 27 #include <asm/irq.h> 27 - #include <asm/sched_clock.h> 28 28 #include <asm/uaccess.h> 29 29 #include <asm/mach/irq.h> 30 30 #include <asm/mach/time.h>
+1 -1
arch/arm/plat-omap/counter_32k.c
··· 18 18 #include <linux/err.h> 19 19 #include <linux/io.h> 20 20 #include <linux/clocksource.h> 21 + #include <linux/sched_clock.h> 21 22 22 23 #include <asm/mach/time.h> 23 - #include <asm/sched_clock.h> 24 24 25 25 #include <plat/counter-32k.h> 26 26
+1 -1
arch/arm/plat-orion/time.c
··· 16 16 #include <linux/clockchips.h> 17 17 #include <linux/interrupt.h> 18 18 #include <linux/irq.h> 19 - #include <asm/sched_clock.h> 19 + #include <linux/sched_clock.h> 20 20 21 21 /* 22 22 * MBus bridge block registers.
+1 -1
arch/arm/plat-samsung/samsung-time.c
··· 15 15 #include <linux/clk.h> 16 16 #include <linux/clockchips.h> 17 17 #include <linux/platform_device.h> 18 + #include <linux/sched_clock.h> 18 19 19 20 #include <asm/smp_twd.h> 20 21 #include <asm/mach/time.h> 21 22 #include <asm/mach/arch.h> 22 23 #include <asm/mach/map.h> 23 - #include <asm/sched_clock.h> 24 24 25 25 #include <mach/map.h> 26 26 #include <plat/devs.h>
+1 -1
arch/arm/plat-versatile/sched-clock.c
··· 20 20 */ 21 21 #include <linux/kernel.h> 22 22 #include <linux/io.h> 23 + #include <linux/sched_clock.h> 23 24 24 - #include <asm/sched_clock.h> 25 25 #include <plat/sched_clock.h> 26 26 27 27 static void __iomem *ctr;
+2 -2
arch/x86/include/asm/mc146818rtc.h
··· 95 95 unsigned char rtc_cmos_read(unsigned char addr); 96 96 void rtc_cmos_write(unsigned char val, unsigned char addr); 97 97 98 - extern int mach_set_rtc_mmss(unsigned long nowtime); 99 - extern unsigned long mach_get_cmos_time(void); 98 + extern int mach_set_rtc_mmss(const struct timespec *now); 99 + extern void mach_get_cmos_time(struct timespec *now); 100 100 101 101 #define RTC_IRQ 8 102 102
+2 -2
arch/x86/include/asm/mrst-vrtc.h
··· 3 3 4 4 extern unsigned char vrtc_cmos_read(unsigned char reg); 5 5 extern void vrtc_cmos_write(unsigned char val, unsigned char reg); 6 - extern unsigned long vrtc_get_time(void); 7 - extern int vrtc_set_mmss(unsigned long nowtime); 6 + extern void vrtc_get_time(struct timespec *now); 7 + extern int vrtc_set_mmss(const struct timespec *now); 8 8 9 9 #endif
+4 -2
arch/x86/include/asm/x86_init.h
··· 142 142 void (*fixup_cpu_id)(struct cpuinfo_x86 *c, int node); 143 143 }; 144 144 145 + struct timespec; 146 + 145 147 /** 146 148 * struct x86_platform_ops - platform specific runtime functions 147 149 * @calibrate_tsc: calibrate TSC ··· 158 156 */ 159 157 struct x86_platform_ops { 160 158 unsigned long (*calibrate_tsc)(void); 161 - unsigned long (*get_wallclock)(void); 162 - int (*set_wallclock)(unsigned long nowtime); 159 + void (*get_wallclock)(struct timespec *ts); 160 + int (*set_wallclock)(const struct timespec *ts); 163 161 void (*iommu_shutdown)(void); 164 162 bool (*is_untracked_pat_range)(u64 start, u64 end); 165 163 void (*nmi_init)(void);
+3 -6
arch/x86/kernel/kvmclock.c
··· 48 48 * have elapsed since the hypervisor wrote the data. So we try to account for 49 49 * that with system time 50 50 */ 51 - static unsigned long kvm_get_wallclock(void) 51 + static void kvm_get_wallclock(struct timespec *now) 52 52 { 53 53 struct pvclock_vcpu_time_info *vcpu_time; 54 - struct timespec ts; 55 54 int low, high; 56 55 int cpu; 57 56 ··· 63 64 cpu = smp_processor_id(); 64 65 65 66 vcpu_time = &hv_clock[cpu].pvti; 66 - pvclock_read_wallclock(&wall_clock, vcpu_time, &ts); 67 + pvclock_read_wallclock(&wall_clock, vcpu_time, now); 67 68 68 69 preempt_enable(); 69 - 70 - return ts.tv_sec; 71 70 } 72 71 73 - static int kvm_set_wallclock(unsigned long now) 72 + static int kvm_set_wallclock(const struct timespec *now) 74 73 { 75 74 return -1; 76 75 }
+7 -10
arch/x86/kernel/rtc.c
··· 38 38 * jump to the next second precisely 500 ms later. Check the Motorola 39 39 * MC146818A or Dallas DS12887 data sheet for details. 40 40 */ 41 - int mach_set_rtc_mmss(unsigned long nowtime) 41 + int mach_set_rtc_mmss(const struct timespec *now) 42 42 { 43 + unsigned long nowtime = now->tv_sec; 43 44 struct rtc_time tm; 44 45 int retval = 0; 45 46 ··· 59 58 return retval; 60 59 } 61 60 62 - unsigned long mach_get_cmos_time(void) 61 + void mach_get_cmos_time(struct timespec *now) 63 62 { 64 63 unsigned int status, year, mon, day, hour, min, sec, century = 0; 65 64 unsigned long flags; ··· 108 107 } else 109 108 year += CMOS_YEARS_OFFS; 110 109 111 - return mktime(year, mon, day, hour, min, sec); 110 + now->tv_sec = mktime(year, mon, day, hour, min, sec); 111 + now->tv_nsec = 0; 112 112 } 113 113 114 114 /* Routines for accessing the CMOS RAM/RTC. */ ··· 137 135 138 136 int update_persistent_clock(struct timespec now) 139 137 { 140 - return x86_platform.set_wallclock(now.tv_sec); 138 + return x86_platform.set_wallclock(&now); 141 139 } 142 140 143 141 /* not static: needed by APM */ 144 142 void read_persistent_clock(struct timespec *ts) 145 143 { 146 - unsigned long retval; 147 - 148 - retval = x86_platform.get_wallclock(); 149 - 150 - ts->tv_sec = retval; 151 - ts->tv_nsec = 0; 144 + x86_platform.get_wallclock(ts); 152 145 } 153 146 154 147
+2 -2
arch/x86/lguest/boot.c
··· 882 882 * It would be far better for everyone if the Guest had its own clock, but 883 883 * until then the Host gives us the time on every interrupt. 884 884 */ 885 - static unsigned long lguest_get_wallclock(void) 885 + static void lguest_get_wallclock(struct timespec *now) 886 886 { 887 - return lguest_data.time.tv_sec; 887 + *now = lguest_data.time; 888 888 } 889 889 890 890 /*
+6 -4
arch/x86/platform/efi/efi.c
··· 274 274 return status; 275 275 } 276 276 277 - int efi_set_rtc_mmss(unsigned long nowtime) 277 + int efi_set_rtc_mmss(const struct timespec *now) 278 278 { 279 + unsigned long nowtime = now->tv_sec; 279 280 efi_status_t status; 280 281 efi_time_t eft; 281 282 efi_time_cap_t cap; ··· 311 310 return 0; 312 311 } 313 312 314 - unsigned long efi_get_time(void) 313 + void efi_get_time(struct timespec *now) 315 314 { 316 315 efi_status_t status; 317 316 efi_time_t eft; ··· 321 320 if (status != EFI_SUCCESS) 322 321 pr_err("Oops: efitime: can't read time!\n"); 323 322 324 - return mktime(eft.year, eft.month, eft.day, eft.hour, 325 - eft.minute, eft.second); 323 + now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour, 324 + eft.minute, eft.second); 325 + now->tv_nsec = 0; 326 326 } 327 327 328 328 /*
+6 -5
arch/x86/platform/mrst/vrtc.c
··· 56 56 } 57 57 EXPORT_SYMBOL_GPL(vrtc_cmos_write); 58 58 59 - unsigned long vrtc_get_time(void) 59 + void vrtc_get_time(struct timespec *now) 60 60 { 61 61 u8 sec, min, hour, mday, mon; 62 62 unsigned long flags; ··· 82 82 printk(KERN_INFO "vRTC: sec: %d min: %d hour: %d day: %d " 83 83 "mon: %d year: %d\n", sec, min, hour, mday, mon, year); 84 84 85 - return mktime(year, mon, mday, hour, min, sec); 85 + now->tv_sec = mktime(year, mon, mday, hour, min, sec); 86 + now->tv_nsec = 0; 86 87 } 87 88 88 - int vrtc_set_mmss(unsigned long nowtime) 89 + int vrtc_set_mmss(const struct timespec *now) 89 90 { 90 91 unsigned long flags; 91 92 struct rtc_time tm; 92 93 int year; 93 94 int retval = 0; 94 95 95 - rtc_time_to_tm(nowtime, &tm); 96 + rtc_time_to_tm(now->tv_sec, &tm); 96 97 if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) { 97 98 /* 98 99 * tm.year is the number of years since 1900, and the ··· 111 110 } else { 112 111 printk(KERN_ERR 113 112 "%s: Invalid vRTC value: write of %lx to vRTC failed\n", 114 - __FUNCTION__, nowtime); 113 + __FUNCTION__, now->tv_sec); 115 114 retval = -EINVAL; 116 115 } 117 116 return retval;
+45 -17
arch/x86/xen/time.c
··· 15 15 #include <linux/math64.h> 16 16 #include <linux/gfp.h> 17 17 #include <linux/slab.h> 18 + #include <linux/pvclock_gtod.h> 18 19 19 20 #include <asm/pvclock.h> 20 21 #include <asm/xen/hypervisor.h> ··· 180 179 put_cpu_var(xen_vcpu); 181 180 } 182 181 183 - static unsigned long xen_get_wallclock(void) 182 + static void xen_get_wallclock(struct timespec *now) 184 183 { 185 - struct timespec ts; 186 - 187 - xen_read_wallclock(&ts); 188 - return ts.tv_sec; 184 + xen_read_wallclock(now); 189 185 } 190 186 191 - static int xen_set_wallclock(unsigned long now) 187 + static int xen_set_wallclock(const struct timespec *now) 192 188 { 193 - struct xen_platform_op op; 194 - int rc; 189 + return -1; 190 + } 195 191 196 - /* do nothing for domU */ 197 - if (!xen_initial_domain()) 198 - return -1; 192 + static int xen_pvclock_gtod_notify(struct notifier_block *nb, 193 + unsigned long was_set, void *priv) 194 + { 195 + /* Protected by the calling core code serialization */ 196 + static struct timespec next_sync; 197 + 198 + struct xen_platform_op op; 199 + struct timespec now; 200 + 201 + now = __current_kernel_time(); 202 + 203 + /* 204 + * We only take the expensive HV call when the clock was set 205 + * or when the 11 minutes RTC synchronization time elapsed. 206 + */ 207 + if (!was_set && timespec_compare(&now, &next_sync) < 0) 208 + return NOTIFY_OK; 199 209 200 210 op.cmd = XENPF_settime; 201 - op.u.settime.secs = now; 202 - op.u.settime.nsecs = 0; 211 + op.u.settime.secs = now.tv_sec; 212 + op.u.settime.nsecs = now.tv_nsec; 203 213 op.u.settime.system_time = xen_clocksource_read(); 204 214 205 - rc = HYPERVISOR_dom0_op(&op); 206 - WARN(rc != 0, "XENPF_settime failed: now=%ld\n", now); 215 + (void)HYPERVISOR_dom0_op(&op); 207 216 208 - return rc; 217 + /* 218 + * Move the next drift compensation time 11 minutes 219 + * ahead. That's emulating the sync_cmos_clock() update for 220 + * the hardware RTC. 221 + */ 222 + next_sync = now; 223 + next_sync.tv_sec += 11 * 60; 224 + 225 + return NOTIFY_OK; 209 226 } 227 + 228 + static struct notifier_block xen_pvclock_gtod_notifier = { 229 + .notifier_call = xen_pvclock_gtod_notify, 230 + }; 210 231 211 232 static struct clocksource xen_clocksource __read_mostly = { 212 233 .name = "xen", ··· 505 482 xen_setup_runstate_info(cpu); 506 483 xen_setup_timer(cpu); 507 484 xen_setup_cpu_clockevents(); 485 + 486 + if (xen_initial_domain()) 487 + pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); 508 488 } 509 489 510 490 void __init xen_init_time_ops(void) ··· 520 494 521 495 x86_platform.calibrate_tsc = xen_tsc_khz; 522 496 x86_platform.get_wallclock = xen_get_wallclock; 523 - x86_platform.set_wallclock = xen_set_wallclock; 497 + /* Dom0 uses the native method to set the hardware RTC. */ 498 + if (!xen_initial_domain()) 499 + x86_platform.set_wallclock = xen_set_wallclock; 524 500 } 525 501 526 502 #ifdef CONFIG_XEN_PVHVM
+5
drivers/clocksource/Kconfig
··· 87 87 Samsung S3C, S5P and Exynos SoCs, replacing an earlier driver 88 88 for all devicetree enabled platforms. This driver will be 89 89 needed only on systems that do not have the Exynos MCT available. 90 + 91 + config VF_PIT_TIMER 92 + bool 93 + help 94 + Support for Period Interrupt Timer on Freescale Vybrid Family SoCs.
+3
drivers/clocksource/Makefile
··· 22 22 obj-$(CONFIG_SUN4I_TIMER) += sun4i_timer.o 23 23 obj-$(CONFIG_ARCH_TEGRA) += tegra20_timer.o 24 24 obj-$(CONFIG_VT8500_TIMER) += vt8500_timer.o 25 + obj-$(CONFIG_ARCH_NSPIRE) += zevio-timer.o 25 26 obj-$(CONFIG_ARCH_BCM) += bcm_kona_timer.o 26 27 obj-$(CONFIG_CADENCE_TTC_TIMER) += cadence_ttc_timer.o 27 28 obj-$(CONFIG_CLKSRC_EXYNOS_MCT) += exynos_mct.o 28 29 obj-$(CONFIG_CLKSRC_SAMSUNG_PWM) += samsung_pwm_timer.o 30 + obj-$(CONFIG_VF_PIT_TIMER) += vf_pit_timer.o 29 31 30 32 obj-$(CONFIG_ARM_ARCH_TIMER) += arm_arch_timer.o 31 33 obj-$(CONFIG_CLKSRC_METAG_GENERIC) += metag_generic.o 34 + obj-$(CONFIG_ARCH_HAS_TICK_BROADCAST) += dummy_timer.o
+1 -1
drivers/clocksource/bcm2835_timer.c
··· 28 28 #include <linux/of_platform.h> 29 29 #include <linux/slab.h> 30 30 #include <linux/string.h> 31 + #include <linux/sched_clock.h> 31 32 32 - #include <asm/sched_clock.h> 33 33 #include <asm/irq.h> 34 34 35 35 #define REG_CONTROL 0x00
+1 -2
drivers/clocksource/clksrc-dbx500-prcmu.c
··· 14 14 */ 15 15 #include <linux/clockchips.h> 16 16 #include <linux/clksrc-dbx500-prcmu.h> 17 - 18 - #include <asm/sched_clock.h> 17 + #include <linux/sched_clock.h> 19 18 20 19 #define RATE_32K 32768 21 20
+69
drivers/clocksource/dummy_timer.c
··· 1 + /* 2 + * linux/drivers/clocksource/dummy_timer.c 3 + * 4 + * Copyright (C) 2013 ARM Ltd. 5 + * All Rights Reserved 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License version 2 as 9 + * published by the Free Software Foundation. 10 + */ 11 + #include <linux/clockchips.h> 12 + #include <linux/cpu.h> 13 + #include <linux/init.h> 14 + #include <linux/percpu.h> 15 + #include <linux/cpumask.h> 16 + 17 + static DEFINE_PER_CPU(struct clock_event_device, dummy_timer_evt); 18 + 19 + static void dummy_timer_set_mode(enum clock_event_mode mode, 20 + struct clock_event_device *evt) 21 + { 22 + /* 23 + * Core clockevents code will call this when exchanging timer devices. 24 + * We don't need to do anything here. 25 + */ 26 + } 27 + 28 + static void __cpuinit dummy_timer_setup(void) 29 + { 30 + int cpu = smp_processor_id(); 31 + struct clock_event_device *evt = __this_cpu_ptr(&dummy_timer_evt); 32 + 33 + evt->name = "dummy_timer"; 34 + evt->features = CLOCK_EVT_FEAT_PERIODIC | 35 + CLOCK_EVT_FEAT_ONESHOT | 36 + CLOCK_EVT_FEAT_DUMMY; 37 + evt->rating = 100; 38 + evt->set_mode = dummy_timer_set_mode; 39 + evt->cpumask = cpumask_of(cpu); 40 + 41 + clockevents_register_device(evt); 42 + } 43 + 44 + static int __cpuinit dummy_timer_cpu_notify(struct notifier_block *self, 45 + unsigned long action, void *hcpu) 46 + { 47 + if ((action & ~CPU_TASKS_FROZEN) == CPU_STARTING) 48 + dummy_timer_setup(); 49 + 50 + return NOTIFY_OK; 51 + } 52 + 53 + static struct notifier_block dummy_timer_cpu_nb __cpuinitdata = { 54 + .notifier_call = dummy_timer_cpu_notify, 55 + }; 56 + 57 + static int __init dummy_timer_register(void) 58 + { 59 + int err = register_cpu_notifier(&dummy_timer_cpu_nb); 60 + if (err) 61 + return err; 62 + 63 + /* We won't get a call on the boot CPU, so register immediately */ 64 + if (num_possible_cpus() > 1) 65 + dummy_timer_setup(); 66 + 67 + return 0; 68 + } 69 + early_initcall(dummy_timer_register);
-12
drivers/clocksource/dw_apb_timer.c
··· 387 387 { 388 388 return (cycle_t)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE); 389 389 } 390 - 391 - /** 392 - * dw_apb_clocksource_unregister() - unregister and free a clocksource. 393 - * 394 - * @dw_cs: The clocksource to unregister/free. 395 - */ 396 - void dw_apb_clocksource_unregister(struct dw_apb_clocksource *dw_cs) 397 - { 398 - clocksource_unregister(&dw_cs->cs); 399 - 400 - kfree(dw_cs); 401 - }
+2 -4
drivers/clocksource/dw_apb_timer_of.c
··· 21 21 #include <linux/of_address.h> 22 22 #include <linux/of_irq.h> 23 23 #include <linux/clk.h> 24 - 25 - #include <asm/mach/time.h> 26 - #include <asm/sched_clock.h> 24 + #include <linux/sched_clock.h> 27 25 28 26 static void timer_get_base_and_rate(struct device_node *np, 29 27 void __iomem **base, u32 *rate) ··· 66 68 u32 irq, rate; 67 69 68 70 irq = irq_of_parse_and_map(event_timer, 0); 69 - if (irq == NO_IRQ) 71 + if (irq == 0) 70 72 panic("No IRQ for clock event timer"); 71 73 72 74 timer_get_base_and_rate(event_timer, &iobase, &rate);
+1 -1
drivers/clocksource/mxs_timer.c
··· 29 29 #include <linux/of_address.h> 30 30 #include <linux/of_irq.h> 31 31 #include <linux/stmp_device.h> 32 + #include <linux/sched_clock.h> 32 33 33 34 #include <asm/mach/time.h> 34 - #include <asm/sched_clock.h> 35 35 36 36 /* 37 37 * There are 2 versions of the timrot on Freescale MXS-based SoCs.
+1 -1
drivers/clocksource/nomadik-mtu.c
··· 21 21 #include <linux/delay.h> 22 22 #include <linux/err.h> 23 23 #include <linux/platform_data/clocksource-nomadik-mtu.h> 24 + #include <linux/sched_clock.h> 24 25 #include <asm/mach/time.h> 25 - #include <asm/sched_clock.h> 26 26 27 27 /* 28 28 * The MTU device hosts four different counters, with 4 set of
+1 -1
drivers/clocksource/samsung_pwm_timer.c
··· 21 21 #include <linux/of_irq.h> 22 22 #include <linux/platform_device.h> 23 23 #include <linux/slab.h> 24 + #include <linux/sched_clock.h> 24 25 25 26 #include <clocksource/samsung_pwm.h> 26 27 27 - #include <asm/sched_clock.h> 28 28 29 29 /* 30 30 * Clocksource driver
+1 -1
drivers/clocksource/tegra20_timer.c
··· 26 26 #include <linux/io.h> 27 27 #include <linux/of_address.h> 28 28 #include <linux/of_irq.h> 29 + #include <linux/sched_clock.h> 29 30 30 31 #include <asm/mach/time.h> 31 32 #include <asm/smp_twd.h> 32 - #include <asm/sched_clock.h> 33 33 34 34 #define RTC_SECONDS 0x08 35 35 #define RTC_SHADOW_SECONDS 0x0c
+1 -1
drivers/clocksource/time-armada-370-xp.c
··· 27 27 #include <linux/of_address.h> 28 28 #include <linux/irq.h> 29 29 #include <linux/module.h> 30 + #include <linux/sched_clock.h> 30 31 31 - #include <asm/sched_clock.h> 32 32 #include <asm/localtimer.h> 33 33 #include <linux/percpu.h> 34 34 /*
+1 -1
drivers/clocksource/timer-marco.c
··· 17 17 #include <linux/of.h> 18 18 #include <linux/of_irq.h> 19 19 #include <linux/of_address.h> 20 - #include <asm/sched_clock.h> 20 + #include <linux/sched_clock.h> 21 21 #include <asm/localtimer.h> 22 22 #include <asm/mach/time.h> 23 23
+1 -1
drivers/clocksource/timer-prima2.c
··· 18 18 #include <linux/of.h> 19 19 #include <linux/of_irq.h> 20 20 #include <linux/of_address.h> 21 - #include <asm/sched_clock.h> 21 + #include <linux/sched_clock.h> 22 22 #include <asm/mach/time.h> 23 23 24 24 #define SIRFSOC_TIMER_COUNTER_LO 0x0000
+194
drivers/clocksource/vf_pit_timer.c
··· 1 + /* 2 + * Copyright 2012-2013 Freescale Semiconductor, Inc. 3 + * 4 + * This program is free software; you can redistribute it and/or 5 + * modify it under the terms of the GNU General Public License 6 + * as published by the Free Software Foundation; either version 2 7 + * of the License, or (at your option) any later version. 8 + */ 9 + 10 + #include <linux/interrupt.h> 11 + #include <linux/clockchips.h> 12 + #include <linux/clk.h> 13 + #include <linux/of_address.h> 14 + #include <linux/of_irq.h> 15 + #include <linux/sched_clock.h> 16 + 17 + /* 18 + * Each pit takes 0x10 Bytes register space 19 + */ 20 + #define PITMCR 0x00 21 + #define PIT0_OFFSET 0x100 22 + #define PITn_OFFSET(n) (PIT0_OFFSET + 0x10 * (n)) 23 + #define PITLDVAL 0x00 24 + #define PITCVAL 0x04 25 + #define PITTCTRL 0x08 26 + #define PITTFLG 0x0c 27 + 28 + #define PITMCR_MDIS (0x1 << 1) 29 + 30 + #define PITTCTRL_TEN (0x1 << 0) 31 + #define PITTCTRL_TIE (0x1 << 1) 32 + #define PITCTRL_CHN (0x1 << 2) 33 + 34 + #define PITTFLG_TIF 0x1 35 + 36 + static void __iomem *clksrc_base; 37 + static void __iomem *clkevt_base; 38 + static unsigned long cycle_per_jiffy; 39 + 40 + static inline void pit_timer_enable(void) 41 + { 42 + __raw_writel(PITTCTRL_TEN | PITTCTRL_TIE, clkevt_base + PITTCTRL); 43 + } 44 + 45 + static inline void pit_timer_disable(void) 46 + { 47 + __raw_writel(0, clkevt_base + PITTCTRL); 48 + } 49 + 50 + static inline void pit_irq_acknowledge(void) 51 + { 52 + __raw_writel(PITTFLG_TIF, clkevt_base + PITTFLG); 53 + } 54 + 55 + static unsigned int pit_read_sched_clock(void) 56 + { 57 + return __raw_readl(clksrc_base + PITCVAL); 58 + } 59 + 60 + static int __init pit_clocksource_init(unsigned long rate) 61 + { 62 + /* set the max load value and start the clock source counter */ 63 + __raw_writel(0, clksrc_base + PITTCTRL); 64 + __raw_writel(~0UL, clksrc_base + PITLDVAL); 65 + __raw_writel(PITTCTRL_TEN, clksrc_base + PITTCTRL); 66 + 67 + setup_sched_clock(pit_read_sched_clock, 32, rate); 68 + return clocksource_mmio_init(clksrc_base + PITCVAL, "vf-pit", rate, 69 + 300, 32, clocksource_mmio_readl_down); 70 + } 71 + 72 + static int pit_set_next_event(unsigned long delta, 73 + struct clock_event_device *unused) 74 + { 75 + /* 76 + * set a new value to PITLDVAL register will not restart the timer, 77 + * to abort the current cycle and start a timer period with the new 78 + * value, the timer must be disabled and enabled again. 79 + * and the PITLAVAL should be set to delta minus one according to pit 80 + * hardware requirement. 81 + */ 82 + pit_timer_disable(); 83 + __raw_writel(delta - 1, clkevt_base + PITLDVAL); 84 + pit_timer_enable(); 85 + 86 + return 0; 87 + } 88 + 89 + static void pit_set_mode(enum clock_event_mode mode, 90 + struct clock_event_device *evt) 91 + { 92 + switch (mode) { 93 + case CLOCK_EVT_MODE_PERIODIC: 94 + pit_set_next_event(cycle_per_jiffy, evt); 95 + break; 96 + default: 97 + break; 98 + } 99 + } 100 + 101 + static irqreturn_t pit_timer_interrupt(int irq, void *dev_id) 102 + { 103 + struct clock_event_device *evt = dev_id; 104 + 105 + pit_irq_acknowledge(); 106 + 107 + /* 108 + * pit hardware doesn't support oneshot, it will generate an interrupt 109 + * and reload the counter value from PITLDVAL when PITCVAL reach zero, 110 + * and start the counter again. So software need to disable the timer 111 + * to stop the counter loop in ONESHOT mode. 112 + */ 113 + if (likely(evt->mode == CLOCK_EVT_MODE_ONESHOT)) 114 + pit_timer_disable(); 115 + 116 + evt->event_handler(evt); 117 + 118 + return IRQ_HANDLED; 119 + } 120 + 121 + static struct clock_event_device clockevent_pit = { 122 + .name = "VF pit timer", 123 + .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, 124 + .set_mode = pit_set_mode, 125 + .set_next_event = pit_set_next_event, 126 + .rating = 300, 127 + }; 128 + 129 + static struct irqaction pit_timer_irq = { 130 + .name = "VF pit timer", 131 + .flags = IRQF_TIMER | IRQF_IRQPOLL, 132 + .handler = pit_timer_interrupt, 133 + .dev_id = &clockevent_pit, 134 + }; 135 + 136 + static int __init pit_clockevent_init(unsigned long rate, int irq) 137 + { 138 + __raw_writel(0, clkevt_base + PITTCTRL); 139 + __raw_writel(PITTFLG_TIF, clkevt_base + PITTFLG); 140 + 141 + BUG_ON(setup_irq(irq, &pit_timer_irq)); 142 + 143 + clockevent_pit.cpumask = cpumask_of(0); 144 + clockevent_pit.irq = irq; 145 + /* 146 + * The value for the LDVAL register trigger is calculated as: 147 + * LDVAL trigger = (period / clock period) - 1 148 + * The pit is a 32-bit down count timer, when the conter value 149 + * reaches 0, it will generate an interrupt, thus the minimal 150 + * LDVAL trigger value is 1. And then the min_delta is 151 + * minimal LDVAL trigger value + 1, and the max_delta is full 32-bit. 152 + */ 153 + clockevents_config_and_register(&clockevent_pit, rate, 2, 0xffffffff); 154 + 155 + return 0; 156 + } 157 + 158 + static void __init pit_timer_init(struct device_node *np) 159 + { 160 + struct clk *pit_clk; 161 + void __iomem *timer_base; 162 + unsigned long clk_rate; 163 + int irq; 164 + 165 + timer_base = of_iomap(np, 0); 166 + BUG_ON(!timer_base); 167 + 168 + /* 169 + * PIT0 and PIT1 can be chained to build a 64-bit timer, 170 + * so choose PIT2 as clocksource, PIT3 as clockevent device, 171 + * and leave PIT0 and PIT1 unused for anyone else who needs them. 172 + */ 173 + clksrc_base = timer_base + PITn_OFFSET(2); 174 + clkevt_base = timer_base + PITn_OFFSET(3); 175 + 176 + irq = irq_of_parse_and_map(np, 0); 177 + BUG_ON(irq <= 0); 178 + 179 + pit_clk = of_clk_get(np, 0); 180 + BUG_ON(IS_ERR(pit_clk)); 181 + 182 + BUG_ON(clk_prepare_enable(pit_clk)); 183 + 184 + clk_rate = clk_get_rate(pit_clk); 185 + cycle_per_jiffy = clk_rate / (HZ); 186 + 187 + /* enable the pit module */ 188 + __raw_writel(~PITMCR_MDIS, timer_base + PITMCR); 189 + 190 + BUG_ON(pit_clocksource_init(clk_rate)); 191 + 192 + pit_clockevent_init(clk_rate, irq); 193 + } 194 + CLOCKSOURCE_OF_DECLARE(vf610, "fsl,vf610-pit", pit_timer_init);
+215
drivers/clocksource/zevio-timer.c
··· 1 + /* 2 + * linux/drivers/clocksource/zevio-timer.c 3 + * 4 + * Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au> 5 + * 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License version 2, as 8 + * published by the Free Software Foundation. 9 + * 10 + */ 11 + 12 + #include <linux/io.h> 13 + #include <linux/irq.h> 14 + #include <linux/of.h> 15 + #include <linux/of_address.h> 16 + #include <linux/of_irq.h> 17 + #include <linux/clk.h> 18 + #include <linux/clockchips.h> 19 + #include <linux/cpumask.h> 20 + #include <linux/interrupt.h> 21 + #include <linux/slab.h> 22 + 23 + #define IO_CURRENT_VAL 0x00 24 + #define IO_DIVIDER 0x04 25 + #define IO_CONTROL 0x08 26 + 27 + #define IO_TIMER1 0x00 28 + #define IO_TIMER2 0x0C 29 + 30 + #define IO_MATCH_BEGIN 0x18 31 + #define IO_MATCH(x) (IO_MATCH_BEGIN + ((x) << 2)) 32 + 33 + #define IO_INTR_STS 0x00 34 + #define IO_INTR_ACK 0x00 35 + #define IO_INTR_MSK 0x04 36 + 37 + #define CNTL_STOP_TIMER (1 << 4) 38 + #define CNTL_RUN_TIMER (0 << 4) 39 + 40 + #define CNTL_INC (1 << 3) 41 + #define CNTL_DEC (0 << 3) 42 + 43 + #define CNTL_TOZERO 0 44 + #define CNTL_MATCH(x) ((x) + 1) 45 + #define CNTL_FOREVER 7 46 + 47 + /* There are 6 match registers but we only use one. */ 48 + #define TIMER_MATCH 0 49 + 50 + #define TIMER_INTR_MSK (1 << (TIMER_MATCH)) 51 + #define TIMER_INTR_ALL 0x3F 52 + 53 + struct zevio_timer { 54 + void __iomem *base; 55 + void __iomem *timer1, *timer2; 56 + void __iomem *interrupt_regs; 57 + 58 + struct clk *clk; 59 + struct clock_event_device clkevt; 60 + struct irqaction clkevt_irq; 61 + 62 + char clocksource_name[64]; 63 + char clockevent_name[64]; 64 + }; 65 + 66 + static int zevio_timer_set_event(unsigned long delta, 67 + struct clock_event_device *dev) 68 + { 69 + struct zevio_timer *timer = container_of(dev, struct zevio_timer, 70 + clkevt); 71 + 72 + writel(delta, timer->timer1 + IO_CURRENT_VAL); 73 + writel(CNTL_RUN_TIMER | CNTL_DEC | CNTL_MATCH(TIMER_MATCH), 74 + timer->timer1 + IO_CONTROL); 75 + 76 + return 0; 77 + } 78 + 79 + static void zevio_timer_set_mode(enum clock_event_mode mode, 80 + struct clock_event_device *dev) 81 + { 82 + struct zevio_timer *timer = container_of(dev, struct zevio_timer, 83 + clkevt); 84 + 85 + switch (mode) { 86 + case CLOCK_EVT_MODE_RESUME: 87 + case CLOCK_EVT_MODE_ONESHOT: 88 + /* Enable timer interrupts */ 89 + writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_MSK); 90 + writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK); 91 + break; 92 + case CLOCK_EVT_MODE_SHUTDOWN: 93 + case CLOCK_EVT_MODE_UNUSED: 94 + /* Disable timer interrupts */ 95 + writel(0, timer->interrupt_regs + IO_INTR_MSK); 96 + writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK); 97 + /* Stop timer */ 98 + writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL); 99 + break; 100 + case CLOCK_EVT_MODE_PERIODIC: 101 + default: 102 + /* Unsupported */ 103 + break; 104 + } 105 + } 106 + 107 + static irqreturn_t zevio_timer_interrupt(int irq, void *dev_id) 108 + { 109 + struct zevio_timer *timer = dev_id; 110 + u32 intr; 111 + 112 + intr = readl(timer->interrupt_regs + IO_INTR_ACK); 113 + if (!(intr & TIMER_INTR_MSK)) 114 + return IRQ_NONE; 115 + 116 + writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_ACK); 117 + writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL); 118 + 119 + if (timer->clkevt.event_handler) 120 + timer->clkevt.event_handler(&timer->clkevt); 121 + 122 + return IRQ_HANDLED; 123 + } 124 + 125 + static int __init zevio_timer_add(struct device_node *node) 126 + { 127 + struct zevio_timer *timer; 128 + struct resource res; 129 + int irqnr, ret; 130 + 131 + timer = kzalloc(sizeof(*timer), GFP_KERNEL); 132 + if (!timer) 133 + return -ENOMEM; 134 + 135 + timer->base = of_iomap(node, 0); 136 + if (!timer->base) { 137 + ret = -EINVAL; 138 + goto error_free; 139 + } 140 + timer->timer1 = timer->base + IO_TIMER1; 141 + timer->timer2 = timer->base + IO_TIMER2; 142 + 143 + timer->clk = of_clk_get(node, 0); 144 + if (IS_ERR(timer->clk)) { 145 + ret = PTR_ERR(timer->clk); 146 + pr_err("Timer clock not found! (error %d)\n", ret); 147 + goto error_unmap; 148 + } 149 + 150 + timer->interrupt_regs = of_iomap(node, 1); 151 + irqnr = irq_of_parse_and_map(node, 0); 152 + 153 + of_address_to_resource(node, 0, &res); 154 + scnprintf(timer->clocksource_name, sizeof(timer->clocksource_name), 155 + "%llx.%s_clocksource", 156 + (unsigned long long)res.start, node->name); 157 + 158 + scnprintf(timer->clockevent_name, sizeof(timer->clockevent_name), 159 + "%llx.%s_clockevent", 160 + (unsigned long long)res.start, node->name); 161 + 162 + if (timer->interrupt_regs && irqnr) { 163 + timer->clkevt.name = timer->clockevent_name; 164 + timer->clkevt.set_next_event = zevio_timer_set_event; 165 + timer->clkevt.set_mode = zevio_timer_set_mode; 166 + timer->clkevt.rating = 200; 167 + timer->clkevt.cpumask = cpu_all_mask; 168 + timer->clkevt.features = CLOCK_EVT_FEAT_ONESHOT; 169 + timer->clkevt.irq = irqnr; 170 + 171 + writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL); 172 + writel(0, timer->timer1 + IO_DIVIDER); 173 + 174 + /* Start with timer interrupts disabled */ 175 + writel(0, timer->interrupt_regs + IO_INTR_MSK); 176 + writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK); 177 + 178 + /* Interrupt to occur when timer value matches 0 */ 179 + writel(0, timer->base + IO_MATCH(TIMER_MATCH)); 180 + 181 + timer->clkevt_irq.name = timer->clockevent_name; 182 + timer->clkevt_irq.handler = zevio_timer_interrupt; 183 + timer->clkevt_irq.dev_id = timer; 184 + timer->clkevt_irq.flags = IRQF_TIMER | IRQF_IRQPOLL; 185 + 186 + setup_irq(irqnr, &timer->clkevt_irq); 187 + 188 + clockevents_config_and_register(&timer->clkevt, 189 + clk_get_rate(timer->clk), 0x0001, 0xffff); 190 + pr_info("Added %s as clockevent\n", timer->clockevent_name); 191 + } 192 + 193 + writel(CNTL_STOP_TIMER, timer->timer2 + IO_CONTROL); 194 + writel(0, timer->timer2 + IO_CURRENT_VAL); 195 + writel(0, timer->timer2 + IO_DIVIDER); 196 + writel(CNTL_RUN_TIMER | CNTL_FOREVER | CNTL_INC, 197 + timer->timer2 + IO_CONTROL); 198 + 199 + clocksource_mmio_init(timer->timer2 + IO_CURRENT_VAL, 200 + timer->clocksource_name, 201 + clk_get_rate(timer->clk), 202 + 200, 16, 203 + clocksource_mmio_readw_up); 204 + 205 + pr_info("Added %s as clocksource\n", timer->clocksource_name); 206 + 207 + return 0; 208 + error_unmap: 209 + iounmap(timer->base); 210 + error_free: 211 + kfree(timer); 212 + return ret; 213 + } 214 + 215 + CLOCKSOURCE_OF_DECLARE(zevio_timer, "lsi,zevio-timer", zevio_timer_add);
-3
drivers/xen/manage.c
··· 168 168 169 169 dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE); 170 170 171 - /* Make sure timer events get retriggered on all CPUs */ 172 - clock_was_set(); 173 - 174 171 out_thaw: 175 172 #ifdef CONFIG_PREEMPT 176 173 thaw_processes();
+108 -23
fs/timerfd.c
··· 8 8 * 9 9 */ 10 10 11 + #include <linux/alarmtimer.h> 11 12 #include <linux/file.h> 12 13 #include <linux/poll.h> 13 14 #include <linux/init.h> ··· 27 26 #include <linux/rcupdate.h> 28 27 29 28 struct timerfd_ctx { 30 - struct hrtimer tmr; 29 + union { 30 + struct hrtimer tmr; 31 + struct alarm alarm; 32 + } t; 31 33 ktime_t tintv; 32 34 ktime_t moffs; 33 35 wait_queue_head_t wqh; ··· 45 41 static LIST_HEAD(cancel_list); 46 42 static DEFINE_SPINLOCK(cancel_lock); 47 43 44 + static inline bool isalarm(struct timerfd_ctx *ctx) 45 + { 46 + return ctx->clockid == CLOCK_REALTIME_ALARM || 47 + ctx->clockid == CLOCK_BOOTTIME_ALARM; 48 + } 49 + 48 50 /* 49 51 * This gets called when the timer event triggers. We set the "expired" 50 52 * flag, but we do not re-arm the timer (in case it's necessary, 51 53 * tintv.tv64 != 0) until the timer is accessed. 52 54 */ 53 - static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) 55 + static void timerfd_triggered(struct timerfd_ctx *ctx) 54 56 { 55 - struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr); 56 57 unsigned long flags; 57 58 58 59 spin_lock_irqsave(&ctx->wqh.lock, flags); ··· 65 56 ctx->ticks++; 66 57 wake_up_locked(&ctx->wqh); 67 58 spin_unlock_irqrestore(&ctx->wqh.lock, flags); 59 + } 68 60 61 + static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) 62 + { 63 + struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, 64 + t.tmr); 65 + timerfd_triggered(ctx); 69 66 return HRTIMER_NORESTART; 67 + } 68 + 69 + static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm, 70 + ktime_t now) 71 + { 72 + struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx, 73 + t.alarm); 74 + timerfd_triggered(ctx); 75 + return ALARMTIMER_NORESTART; 70 76 } 71 77 72 78 /* ··· 131 107 132 108 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) 133 109 { 134 - if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) && 135 - (flags & TFD_TIMER_CANCEL_ON_SET)) { 110 + if ((ctx->clockid == CLOCK_REALTIME || 111 + ctx->clockid == CLOCK_REALTIME_ALARM) && 112 + (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { 136 113 if (!ctx->might_cancel) { 137 114 ctx->might_cancel = true; 138 115 spin_lock(&cancel_lock); ··· 149 124 { 150 125 ktime_t remaining; 151 126 152 - remaining = hrtimer_expires_remaining(&ctx->tmr); 127 + if (isalarm(ctx)) 128 + remaining = alarm_expires_remaining(&ctx->t.alarm); 129 + else 130 + remaining = hrtimer_expires_remaining(&ctx->t.tmr); 131 + 153 132 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; 154 133 } 155 134 ··· 171 142 ctx->expired = 0; 172 143 ctx->ticks = 0; 173 144 ctx->tintv = timespec_to_ktime(ktmr->it_interval); 174 - hrtimer_init(&ctx->tmr, clockid, htmode); 175 - hrtimer_set_expires(&ctx->tmr, texp); 176 - ctx->tmr.function = timerfd_tmrproc; 145 + 146 + if (isalarm(ctx)) { 147 + alarm_init(&ctx->t.alarm, 148 + ctx->clockid == CLOCK_REALTIME_ALARM ? 149 + ALARM_REALTIME : ALARM_BOOTTIME, 150 + timerfd_alarmproc); 151 + } else { 152 + hrtimer_init(&ctx->t.tmr, clockid, htmode); 153 + hrtimer_set_expires(&ctx->t.tmr, texp); 154 + ctx->t.tmr.function = timerfd_tmrproc; 155 + } 156 + 177 157 if (texp.tv64 != 0) { 178 - hrtimer_start(&ctx->tmr, texp, htmode); 158 + if (isalarm(ctx)) { 159 + if (flags & TFD_TIMER_ABSTIME) 160 + alarm_start(&ctx->t.alarm, texp); 161 + else 162 + alarm_start_relative(&ctx->t.alarm, texp); 163 + } else { 164 + hrtimer_start(&ctx->t.tmr, texp, htmode); 165 + } 166 + 179 167 if (timerfd_canceled(ctx)) 180 168 return -ECANCELED; 181 169 } ··· 204 158 struct timerfd_ctx *ctx = file->private_data; 205 159 206 160 timerfd_remove_cancel(ctx); 207 - hrtimer_cancel(&ctx->tmr); 161 + 162 + if (isalarm(ctx)) 163 + alarm_cancel(&ctx->t.alarm); 164 + else 165 + hrtimer_cancel(&ctx->t.tmr); 208 166 kfree_rcu(ctx, rcu); 209 167 return 0; 210 168 } ··· 265 215 * callback to avoid DoS attacks specifying a very 266 216 * short timer period. 267 217 */ 268 - ticks += hrtimer_forward_now(&ctx->tmr, 269 - ctx->tintv) - 1; 270 - hrtimer_restart(&ctx->tmr); 218 + if (isalarm(ctx)) { 219 + ticks += alarm_forward_now( 220 + &ctx->t.alarm, ctx->tintv) - 1; 221 + alarm_restart(&ctx->t.alarm); 222 + } else { 223 + ticks += hrtimer_forward_now(&ctx->t.tmr, 224 + ctx->tintv) - 1; 225 + hrtimer_restart(&ctx->t.tmr); 226 + } 271 227 } 272 228 ctx->expired = 0; 273 229 ctx->ticks = 0; ··· 315 259 316 260 if ((flags & ~TFD_CREATE_FLAGS) || 317 261 (clockid != CLOCK_MONOTONIC && 318 - clockid != CLOCK_REALTIME)) 262 + clockid != CLOCK_REALTIME && 263 + clockid != CLOCK_REALTIME_ALARM && 264 + clockid != CLOCK_BOOTTIME_ALARM)) 319 265 return -EINVAL; 320 266 321 267 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); ··· 326 268 327 269 init_waitqueue_head(&ctx->wqh); 328 270 ctx->clockid = clockid; 329 - hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS); 271 + 272 + if (isalarm(ctx)) 273 + alarm_init(&ctx->t.alarm, 274 + ctx->clockid == CLOCK_REALTIME_ALARM ? 275 + ALARM_REALTIME : ALARM_BOOTTIME, 276 + timerfd_alarmproc); 277 + else 278 + hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); 279 + 330 280 ctx->moffs = ktime_get_monotonic_offset(); 331 281 332 282 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, ··· 371 305 */ 372 306 for (;;) { 373 307 spin_lock_irq(&ctx->wqh.lock); 374 - if (hrtimer_try_to_cancel(&ctx->tmr) >= 0) 375 - break; 308 + 309 + if (isalarm(ctx)) { 310 + if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) 311 + break; 312 + } else { 313 + if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) 314 + break; 315 + } 376 316 spin_unlock_irq(&ctx->wqh.lock); 377 317 cpu_relax(); 378 318 } ··· 389 317 * We do not update "ticks" and "expired" since the timer will be 390 318 * re-programmed again in the following timerfd_setup() call. 391 319 */ 392 - if (ctx->expired && ctx->tintv.tv64) 393 - hrtimer_forward_now(&ctx->tmr, ctx->tintv); 320 + if (ctx->expired && ctx->tintv.tv64) { 321 + if (isalarm(ctx)) 322 + alarm_forward_now(&ctx->t.alarm, ctx->tintv); 323 + else 324 + hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); 325 + } 394 326 395 327 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); 396 328 old->it_interval = ktime_to_timespec(ctx->tintv); ··· 421 345 spin_lock_irq(&ctx->wqh.lock); 422 346 if (ctx->expired && ctx->tintv.tv64) { 423 347 ctx->expired = 0; 424 - ctx->ticks += 425 - hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1; 426 - hrtimer_restart(&ctx->tmr); 348 + 349 + if (isalarm(ctx)) { 350 + ctx->ticks += 351 + alarm_forward_now( 352 + &ctx->t.alarm, ctx->tintv) - 1; 353 + alarm_restart(&ctx->t.alarm); 354 + } else { 355 + ctx->ticks += 356 + hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) 357 + - 1; 358 + hrtimer_restart(&ctx->t.tmr); 359 + } 427 360 } 428 361 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); 429 362 t->it_interval = ktime_to_timespec(ctx->tintv);
+4
include/linux/alarmtimer.h
··· 44 44 void alarm_init(struct alarm *alarm, enum alarmtimer_type type, 45 45 enum alarmtimer_restart (*function)(struct alarm *, ktime_t)); 46 46 int alarm_start(struct alarm *alarm, ktime_t start); 47 + int alarm_start_relative(struct alarm *alarm, ktime_t start); 48 + void alarm_restart(struct alarm *alarm); 47 49 int alarm_try_to_cancel(struct alarm *alarm); 48 50 int alarm_cancel(struct alarm *alarm); 49 51 50 52 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval); 53 + u64 alarm_forward_now(struct alarm *alarm, ktime_t interval); 54 + ktime_t alarm_expires_remaining(const struct alarm *alarm); 51 55 52 56 /* Provide way to access the rtc device being used by alarmtimers */ 53 57 struct rtc_device *alarmtimer_get_rtcdev(void);
+4 -1
include/linux/clockchips.h
··· 30 30 #include <linux/notifier.h> 31 31 32 32 struct clock_event_device; 33 + struct module; 33 34 34 35 /* Clock event mode commands */ 35 36 enum clock_event_mode { ··· 84 83 * @irq: IRQ number (only for non CPU local devices) 85 84 * @cpumask: cpumask to indicate for which CPUs this device works 86 85 * @list: list head for the management code 86 + * @owner: module reference 87 87 */ 88 88 struct clock_event_device { 89 89 void (*event_handler)(struct clock_event_device *); ··· 114 112 int irq; 115 113 const struct cpumask *cpumask; 116 114 struct list_head list; 115 + struct module *owner; 117 116 } ____cacheline_aligned; 118 117 119 118 /* ··· 141 138 extern u64 clockevent_delta2ns(unsigned long latch, 142 139 struct clock_event_device *evt); 143 140 extern void clockevents_register_device(struct clock_event_device *dev); 141 + extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu); 144 142 145 143 extern void clockevents_config(struct clock_event_device *dev, u32 freq); 146 144 extern void clockevents_config_and_register(struct clock_event_device *dev, ··· 154 150 struct clock_event_device *new); 155 151 extern void clockevents_set_mode(struct clock_event_device *dev, 156 152 enum clock_event_mode mode); 157 - extern int clockevents_register_notifier(struct notifier_block *nb); 158 153 extern int clockevents_program_event(struct clock_event_device *dev, 159 154 ktime_t expires, bool force); 160 155
+6 -2
include/linux/clocksource.h
··· 21 21 /* clocksource cycle base type */ 22 22 typedef u64 cycle_t; 23 23 struct clocksource; 24 + struct module; 24 25 25 26 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA 26 27 #include <asm/clocksource.h> ··· 163 162 * @suspend: suspend function for the clocksource, if necessary 164 163 * @resume: resume function for the clocksource, if necessary 165 164 * @cycle_last: most recent cycle counter value seen by ::read() 165 + * @owner: module reference, must be set by clocksource in modules 166 166 */ 167 167 struct clocksource { 168 168 /* ··· 197 195 cycle_t cs_last; 198 196 cycle_t wd_last; 199 197 #endif 198 + struct module *owner; 200 199 } ____cacheline_aligned; 201 200 202 201 /* ··· 210 207 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20 211 208 #define CLOCK_SOURCE_UNSTABLE 0x40 212 209 #define CLOCK_SOURCE_SUSPEND_NONSTOP 0x80 210 + #define CLOCK_SOURCE_RESELECT 0x100 213 211 214 212 /* simplify initialization of mask field */ 215 213 #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1) ··· 283 279 284 280 285 281 extern int clocksource_register(struct clocksource*); 286 - extern void clocksource_unregister(struct clocksource*); 282 + extern int clocksource_unregister(struct clocksource*); 287 283 extern void clocksource_touch_watchdog(void); 288 284 extern struct clocksource* clocksource_get_next(void); 289 285 extern void clocksource_change_rating(struct clocksource *cs, int rating); ··· 325 321 } 326 322 327 323 328 - extern void timekeeping_notify(struct clocksource *clock); 324 + extern int timekeeping_notify(struct clocksource *clock); 329 325 330 326 extern cycle_t clocksource_mmio_readl_up(struct clocksource *); 331 327 extern cycle_t clocksource_mmio_readl_down(struct clocksource *);
-1
include/linux/dw_apb_timer.h
··· 51 51 void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs); 52 52 void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs); 53 53 cycle_t dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs); 54 - void dw_apb_clocksource_unregister(struct dw_apb_clocksource *dw_cs); 55 54 56 55 #endif /* __DW_APB_TIMER_H__ */
+2 -2
include/linux/efi.h
··· 594 594 extern int __init efi_uart_console_only (void); 595 595 extern void efi_initialize_iomem_resources(struct resource *code_resource, 596 596 struct resource *data_resource, struct resource *bss_resource); 597 - extern unsigned long efi_get_time(void); 598 - extern int efi_set_rtc_mmss(unsigned long nowtime); 597 + extern void efi_get_time(struct timespec *now); 598 + extern int efi_set_rtc_mmss(const struct timespec *now); 599 599 extern void efi_reserve_boot_services(void); 600 600 extern struct efi_memory_map memmap; 601 601
+6 -4
include/linux/ktime.h
··· 229 229 static inline ktime_t timeval_to_ktime(const struct timeval tv) 230 230 { 231 231 return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec, 232 - .nsec = (s32)tv.tv_usec * 1000 } }; 232 + .nsec = (s32)(tv.tv_usec * 233 + NSEC_PER_USEC) } }; 233 234 } 234 235 235 236 /** ··· 321 320 322 321 static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec) 323 322 { 324 - return ktime_add_ns(kt, usec * 1000); 323 + return ktime_add_ns(kt, usec * NSEC_PER_USEC); 325 324 } 326 325 327 326 static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec) 328 327 { 329 - return ktime_sub_ns(kt, usec * 1000); 328 + return ktime_sub_ns(kt, usec * NSEC_PER_USEC); 330 329 } 331 330 332 331 extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs); ··· 339 338 * 340 339 * Returns true if there was a successful conversion, false if kt was 0. 341 340 */ 342 - static inline bool ktime_to_timespec_cond(const ktime_t kt, struct timespec *ts) 341 + static inline __must_check bool ktime_to_timespec_cond(const ktime_t kt, 342 + struct timespec *ts) 343 343 { 344 344 if (kt.tv64) { 345 345 *ts = ktime_to_timespec(kt);
+11 -5
include/linux/posix-timers.h
··· 7 7 #include <linux/timex.h> 8 8 #include <linux/alarmtimer.h> 9 9 10 - union cpu_time_count { 11 - cputime_t cpu; 12 - unsigned long long sched; 13 - }; 10 + 11 + static inline unsigned long long cputime_to_expires(cputime_t expires) 12 + { 13 + return (__force unsigned long long)expires; 14 + } 15 + 16 + static inline cputime_t expires_to_cputime(unsigned long long expires) 17 + { 18 + return (__force cputime_t)expires; 19 + } 14 20 15 21 struct cpu_timer_list { 16 22 struct list_head entry; 17 - union cpu_time_count expires, incr; 23 + unsigned long long expires, incr; 18 24 struct task_struct *task; 19 25 int firing; 20 26 };
+7
include/linux/pvclock_gtod.h
··· 3 3 4 4 #include <linux/notifier.h> 5 5 6 + /* 7 + * The pvclock gtod notifier is called when the system time is updated 8 + * and is used to keep guest time synchronized with host time. 9 + * 10 + * The 'action' parameter in the notifier function is false (0), or 11 + * true (non-zero) if system time was stepped. 12 + */ 6 13 extern int pvclock_gtod_register_notifier(struct notifier_block *nb); 7 14 extern int pvclock_gtod_unregister_notifier(struct notifier_block *nb); 8 15
+21
include/linux/sched_clock.h
··· 1 + /* 2 + * sched_clock.h: support for extending counters to full 64-bit ns counter 3 + * 4 + * This program is free software; you can redistribute it and/or modify 5 + * it under the terms of the GNU General Public License version 2 as 6 + * published by the Free Software Foundation. 7 + */ 8 + #ifndef LINUX_SCHED_CLOCK 9 + #define LINUX_SCHED_CLOCK 10 + 11 + #ifdef CONFIG_GENERIC_SCHED_CLOCK 12 + extern void sched_clock_postinit(void); 13 + #else 14 + static inline void sched_clock_postinit(void) { } 15 + #endif 16 + 17 + extern void setup_sched_clock(u32 (*read)(void), int bits, unsigned long rate); 18 + 19 + extern unsigned long long (*sched_clock_func)(void); 20 + 21 + #endif
+3
init/Kconfig
··· 780 780 config HAVE_UNSTABLE_SCHED_CLOCK 781 781 bool 782 782 783 + config GENERIC_SCHED_CLOCK 784 + bool 785 + 783 786 # 784 787 # For architectures that want to enable the support for NUMA-affine scheduler 785 788 # balancing logic:
+2
init/main.c
··· 74 74 #include <linux/ptrace.h> 75 75 #include <linux/blkdev.h> 76 76 #include <linux/elevator.h> 77 + #include <linux/sched_clock.h> 77 78 78 79 #include <asm/io.h> 79 80 #include <asm/bugs.h> ··· 555 554 softirq_init(); 556 555 timekeeping_init(); 557 556 time_init(); 557 + sched_clock_postinit(); 558 558 perf_event_init(); 559 559 profile_init(); 560 560 call_function_init();
+16 -16
kernel/hrtimer.c
··· 722 722 return 1; 723 723 } 724 724 725 + static void clock_was_set_work(struct work_struct *work) 726 + { 727 + clock_was_set(); 728 + } 729 + 730 + static DECLARE_WORK(hrtimer_work, clock_was_set_work); 731 + 725 732 /* 726 - * Called from timekeeping code to reprogramm the hrtimer interrupt 727 - * device. If called from the timer interrupt context we defer it to 728 - * softirq context. 733 + * Called from timekeeping and resume code to reprogramm the hrtimer 734 + * interrupt device on all cpus. 729 735 */ 730 736 void clock_was_set_delayed(void) 731 737 { 732 - struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 733 - 734 - cpu_base->clock_was_set = 1; 735 - __raise_softirq_irqoff(HRTIMER_SOFTIRQ); 738 + schedule_work(&hrtimer_work); 736 739 } 737 740 738 741 #else ··· 777 774 778 775 /* 779 776 * During resume we might have to reprogram the high resolution timer 780 - * interrupt (on the local CPU): 777 + * interrupt on all online CPUs. However, all other CPUs will be 778 + * stopped with IRQs interrupts disabled so the clock_was_set() call 779 + * must be deferred. 781 780 */ 782 781 void hrtimers_resume(void) 783 782 { 784 783 WARN_ONCE(!irqs_disabled(), 785 784 KERN_INFO "hrtimers_resume() called with IRQs enabled!"); 786 785 786 + /* Retrigger on the local CPU */ 787 787 retrigger_next_event(NULL); 788 - timerfd_clock_was_set(); 788 + /* And schedule a retrigger for all others */ 789 + clock_was_set_delayed(); 789 790 } 790 791 791 792 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer) ··· 1440 1433 1441 1434 static void run_hrtimer_softirq(struct softirq_action *h) 1442 1435 { 1443 - struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1444 - 1445 - if (cpu_base->clock_was_set) { 1446 - cpu_base->clock_was_set = 0; 1447 - clock_was_set(); 1448 - } 1449 - 1450 1436 hrtimer_peek_ahead_timers(); 1451 1437 } 1452 1438
+140 -255
kernel/posix-cpu-timers.c
··· 51 51 return error; 52 52 } 53 53 54 - static inline union cpu_time_count 54 + static inline unsigned long long 55 55 timespec_to_sample(const clockid_t which_clock, const struct timespec *tp) 56 56 { 57 - union cpu_time_count ret; 58 - ret.sched = 0; /* high half always zero when .cpu used */ 57 + unsigned long long ret; 58 + 59 + ret = 0; /* high half always zero when .cpu used */ 59 60 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { 60 - ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec; 61 + ret = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec; 61 62 } else { 62 - ret.cpu = timespec_to_cputime(tp); 63 + ret = cputime_to_expires(timespec_to_cputime(tp)); 63 64 } 64 65 return ret; 65 66 } 66 67 67 68 static void sample_to_timespec(const clockid_t which_clock, 68 - union cpu_time_count cpu, 69 + unsigned long long expires, 69 70 struct timespec *tp) 70 71 { 71 72 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) 72 - *tp = ns_to_timespec(cpu.sched); 73 + *tp = ns_to_timespec(expires); 73 74 else 74 - cputime_to_timespec(cpu.cpu, tp); 75 - } 76 - 77 - static inline int cpu_time_before(const clockid_t which_clock, 78 - union cpu_time_count now, 79 - union cpu_time_count then) 80 - { 81 - if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { 82 - return now.sched < then.sched; 83 - } else { 84 - return now.cpu < then.cpu; 85 - } 86 - } 87 - static inline void cpu_time_add(const clockid_t which_clock, 88 - union cpu_time_count *acc, 89 - union cpu_time_count val) 90 - { 91 - if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { 92 - acc->sched += val.sched; 93 - } else { 94 - acc->cpu += val.cpu; 95 - } 96 - } 97 - static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock, 98 - union cpu_time_count a, 99 - union cpu_time_count b) 100 - { 101 - if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { 102 - a.sched -= b.sched; 103 - } else { 104 - a.cpu -= b.cpu; 105 - } 106 - return a; 75 + cputime_to_timespec((__force cputime_t)expires, tp); 107 76 } 108 77 109 78 /* ··· 80 111 * given the current clock sample. 81 112 */ 82 113 static void bump_cpu_timer(struct k_itimer *timer, 83 - union cpu_time_count now) 114 + unsigned long long now) 84 115 { 85 116 int i; 117 + unsigned long long delta, incr; 86 118 87 - if (timer->it.cpu.incr.sched == 0) 119 + if (timer->it.cpu.incr == 0) 88 120 return; 89 121 90 - if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) { 91 - unsigned long long delta, incr; 122 + if (now < timer->it.cpu.expires) 123 + return; 92 124 93 - if (now.sched < timer->it.cpu.expires.sched) 94 - return; 95 - incr = timer->it.cpu.incr.sched; 96 - delta = now.sched + incr - timer->it.cpu.expires.sched; 97 - /* Don't use (incr*2 < delta), incr*2 might overflow. */ 98 - for (i = 0; incr < delta - incr; i++) 99 - incr = incr << 1; 100 - for (; i >= 0; incr >>= 1, i--) { 101 - if (delta < incr) 102 - continue; 103 - timer->it.cpu.expires.sched += incr; 104 - timer->it_overrun += 1 << i; 105 - delta -= incr; 106 - } 107 - } else { 108 - cputime_t delta, incr; 125 + incr = timer->it.cpu.incr; 126 + delta = now + incr - timer->it.cpu.expires; 109 127 110 - if (now.cpu < timer->it.cpu.expires.cpu) 111 - return; 112 - incr = timer->it.cpu.incr.cpu; 113 - delta = now.cpu + incr - timer->it.cpu.expires.cpu; 114 - /* Don't use (incr*2 < delta), incr*2 might overflow. */ 115 - for (i = 0; incr < delta - incr; i++) 116 - incr += incr; 117 - for (; i >= 0; incr = incr >> 1, i--) { 118 - if (delta < incr) 119 - continue; 120 - timer->it.cpu.expires.cpu += incr; 121 - timer->it_overrun += 1 << i; 122 - delta -= incr; 123 - } 128 + /* Don't use (incr*2 < delta), incr*2 might overflow. */ 129 + for (i = 0; incr < delta - incr; i++) 130 + incr = incr << 1; 131 + 132 + for (; i >= 0; incr >>= 1, i--) { 133 + if (delta < incr) 134 + continue; 135 + 136 + timer->it.cpu.expires += incr; 137 + timer->it_overrun += 1 << i; 138 + delta -= incr; 124 139 } 125 140 } 126 141 ··· 123 170 return 0; 124 171 } 125 172 126 - static inline cputime_t prof_ticks(struct task_struct *p) 173 + static inline unsigned long long prof_ticks(struct task_struct *p) 127 174 { 128 175 cputime_t utime, stime; 129 176 130 177 task_cputime(p, &utime, &stime); 131 178 132 - return utime + stime; 179 + return cputime_to_expires(utime + stime); 133 180 } 134 - static inline cputime_t virt_ticks(struct task_struct *p) 181 + static inline unsigned long long virt_ticks(struct task_struct *p) 135 182 { 136 183 cputime_t utime; 137 184 138 185 task_cputime(p, &utime, NULL); 139 186 140 - return utime; 187 + return cputime_to_expires(utime); 141 188 } 142 189 143 190 static int ··· 178 225 * Sample a per-thread clock for the given task. 179 226 */ 180 227 static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p, 181 - union cpu_time_count *cpu) 228 + unsigned long long *sample) 182 229 { 183 230 switch (CPUCLOCK_WHICH(which_clock)) { 184 231 default: 185 232 return -EINVAL; 186 233 case CPUCLOCK_PROF: 187 - cpu->cpu = prof_ticks(p); 234 + *sample = prof_ticks(p); 188 235 break; 189 236 case CPUCLOCK_VIRT: 190 - cpu->cpu = virt_ticks(p); 237 + *sample = virt_ticks(p); 191 238 break; 192 239 case CPUCLOCK_SCHED: 193 - cpu->sched = task_sched_runtime(p); 240 + *sample = task_sched_runtime(p); 194 241 break; 195 242 } 196 243 return 0; ··· 237 284 */ 238 285 static int cpu_clock_sample_group(const clockid_t which_clock, 239 286 struct task_struct *p, 240 - union cpu_time_count *cpu) 287 + unsigned long long *sample) 241 288 { 242 289 struct task_cputime cputime; 243 290 ··· 246 293 return -EINVAL; 247 294 case CPUCLOCK_PROF: 248 295 thread_group_cputime(p, &cputime); 249 - cpu->cpu = cputime.utime + cputime.stime; 296 + *sample = cputime_to_expires(cputime.utime + cputime.stime); 250 297 break; 251 298 case CPUCLOCK_VIRT: 252 299 thread_group_cputime(p, &cputime); 253 - cpu->cpu = cputime.utime; 300 + *sample = cputime_to_expires(cputime.utime); 254 301 break; 255 302 case CPUCLOCK_SCHED: 256 303 thread_group_cputime(p, &cputime); 257 - cpu->sched = cputime.sum_exec_runtime; 304 + *sample = cputime.sum_exec_runtime; 258 305 break; 259 306 } 260 307 return 0; ··· 265 312 { 266 313 const pid_t pid = CPUCLOCK_PID(which_clock); 267 314 int error = -EINVAL; 268 - union cpu_time_count rtn; 315 + unsigned long long rtn; 269 316 270 317 if (pid == 0) { 271 318 /* ··· 399 446 return ret; 400 447 } 401 448 449 + static void cleanup_timers_list(struct list_head *head, 450 + unsigned long long curr) 451 + { 452 + struct cpu_timer_list *timer, *next; 453 + 454 + list_for_each_entry_safe(timer, next, head, entry) 455 + list_del_init(&timer->entry); 456 + } 457 + 402 458 /* 403 459 * Clean out CPU timers still ticking when a thread exited. The task 404 460 * pointer is cleared, and the expiry time is replaced with the residual ··· 418 456 cputime_t utime, cputime_t stime, 419 457 unsigned long long sum_exec_runtime) 420 458 { 421 - struct cpu_timer_list *timer, *next; 459 + 422 460 cputime_t ptime = utime + stime; 423 461 424 - list_for_each_entry_safe(timer, next, head, entry) { 425 - list_del_init(&timer->entry); 426 - if (timer->expires.cpu < ptime) { 427 - timer->expires.cpu = 0; 428 - } else { 429 - timer->expires.cpu -= ptime; 430 - } 431 - } 432 - 433 - ++head; 434 - list_for_each_entry_safe(timer, next, head, entry) { 435 - list_del_init(&timer->entry); 436 - if (timer->expires.cpu < utime) { 437 - timer->expires.cpu = 0; 438 - } else { 439 - timer->expires.cpu -= utime; 440 - } 441 - } 442 - 443 - ++head; 444 - list_for_each_entry_safe(timer, next, head, entry) { 445 - list_del_init(&timer->entry); 446 - if (timer->expires.sched < sum_exec_runtime) { 447 - timer->expires.sched = 0; 448 - } else { 449 - timer->expires.sched -= sum_exec_runtime; 450 - } 451 - } 462 + cleanup_timers_list(head, cputime_to_expires(ptime)); 463 + cleanup_timers_list(++head, cputime_to_expires(utime)); 464 + cleanup_timers_list(++head, sum_exec_runtime); 452 465 } 453 466 454 467 /* ··· 453 516 tsk->se.sum_exec_runtime + sig->sum_sched_runtime); 454 517 } 455 518 456 - static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now) 519 + static void clear_dead_task(struct k_itimer *itimer, unsigned long long now) 457 520 { 521 + struct cpu_timer_list *timer = &itimer->it.cpu; 522 + 458 523 /* 459 524 * That's all for this thread or process. 460 525 * We leave our residual in expires to be reported. 461 526 */ 462 - put_task_struct(timer->it.cpu.task); 463 - timer->it.cpu.task = NULL; 464 - timer->it.cpu.expires = cpu_time_sub(timer->it_clock, 465 - timer->it.cpu.expires, 466 - now); 527 + put_task_struct(timer->task); 528 + timer->task = NULL; 529 + if (timer->expires < now) { 530 + timer->expires = 0; 531 + } else { 532 + timer->expires -= now; 533 + } 467 534 } 468 535 469 536 static inline int expires_gt(cputime_t expires, cputime_t new_exp) ··· 499 558 500 559 listpos = head; 501 560 list_for_each_entry(next, head, entry) { 502 - if (cpu_time_before(timer->it_clock, nt->expires, next->expires)) 561 + if (nt->expires < next->expires) 503 562 break; 504 563 listpos = &next->entry; 505 564 } 506 565 list_add(&nt->entry, listpos); 507 566 508 567 if (listpos == head) { 509 - union cpu_time_count *exp = &nt->expires; 568 + unsigned long long exp = nt->expires; 510 569 511 570 /* 512 571 * We are the new earliest-expiring POSIX 1.b timer, hence ··· 517 576 518 577 switch (CPUCLOCK_WHICH(timer->it_clock)) { 519 578 case CPUCLOCK_PROF: 520 - if (expires_gt(cputime_expires->prof_exp, exp->cpu)) 521 - cputime_expires->prof_exp = exp->cpu; 579 + if (expires_gt(cputime_expires->prof_exp, expires_to_cputime(exp))) 580 + cputime_expires->prof_exp = expires_to_cputime(exp); 522 581 break; 523 582 case CPUCLOCK_VIRT: 524 - if (expires_gt(cputime_expires->virt_exp, exp->cpu)) 525 - cputime_expires->virt_exp = exp->cpu; 583 + if (expires_gt(cputime_expires->virt_exp, expires_to_cputime(exp))) 584 + cputime_expires->virt_exp = expires_to_cputime(exp); 526 585 break; 527 586 case CPUCLOCK_SCHED: 528 587 if (cputime_expires->sched_exp == 0 || 529 - cputime_expires->sched_exp > exp->sched) 530 - cputime_expires->sched_exp = exp->sched; 588 + cputime_expires->sched_exp > exp) 589 + cputime_expires->sched_exp = exp; 531 590 break; 532 591 } 533 592 } ··· 542 601 /* 543 602 * User don't want any signal. 544 603 */ 545 - timer->it.cpu.expires.sched = 0; 604 + timer->it.cpu.expires = 0; 546 605 } else if (unlikely(timer->sigq == NULL)) { 547 606 /* 548 607 * This a special case for clock_nanosleep, 549 608 * not a normal timer from sys_timer_create. 550 609 */ 551 610 wake_up_process(timer->it_process); 552 - timer->it.cpu.expires.sched = 0; 553 - } else if (timer->it.cpu.incr.sched == 0) { 611 + timer->it.cpu.expires = 0; 612 + } else if (timer->it.cpu.incr == 0) { 554 613 /* 555 614 * One-shot timer. Clear it as soon as it's fired. 556 615 */ 557 616 posix_timer_event(timer, 0); 558 - timer->it.cpu.expires.sched = 0; 617 + timer->it.cpu.expires = 0; 559 618 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { 560 619 /* 561 620 * The signal did not get queued because the signal ··· 573 632 */ 574 633 static int cpu_timer_sample_group(const clockid_t which_clock, 575 634 struct task_struct *p, 576 - union cpu_time_count *cpu) 635 + unsigned long long *sample) 577 636 { 578 637 struct task_cputime cputime; 579 638 ··· 582 641 default: 583 642 return -EINVAL; 584 643 case CPUCLOCK_PROF: 585 - cpu->cpu = cputime.utime + cputime.stime; 644 + *sample = cputime_to_expires(cputime.utime + cputime.stime); 586 645 break; 587 646 case CPUCLOCK_VIRT: 588 - cpu->cpu = cputime.utime; 647 + *sample = cputime_to_expires(cputime.utime); 589 648 break; 590 649 case CPUCLOCK_SCHED: 591 - cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p); 650 + *sample = cputime.sum_exec_runtime + task_delta_exec(p); 592 651 break; 593 652 } 594 653 return 0; ··· 635 694 struct itimerspec *new, struct itimerspec *old) 636 695 { 637 696 struct task_struct *p = timer->it.cpu.task; 638 - union cpu_time_count old_expires, new_expires, old_incr, val; 697 + unsigned long long old_expires, new_expires, old_incr, val; 639 698 int ret; 640 699 641 700 if (unlikely(p == NULL)) { ··· 690 749 } 691 750 692 751 if (old) { 693 - if (old_expires.sched == 0) { 752 + if (old_expires == 0) { 694 753 old->it_value.tv_sec = 0; 695 754 old->it_value.tv_nsec = 0; 696 755 } else { ··· 705 764 * new setting. 706 765 */ 707 766 bump_cpu_timer(timer, val); 708 - if (cpu_time_before(timer->it_clock, val, 709 - timer->it.cpu.expires)) { 710 - old_expires = cpu_time_sub( 711 - timer->it_clock, 712 - timer->it.cpu.expires, val); 767 + if (val < timer->it.cpu.expires) { 768 + old_expires = timer->it.cpu.expires - val; 713 769 sample_to_timespec(timer->it_clock, 714 770 old_expires, 715 771 &old->it_value); ··· 729 791 goto out; 730 792 } 731 793 732 - if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) { 733 - cpu_time_add(timer->it_clock, &new_expires, val); 794 + if (new_expires != 0 && !(flags & TIMER_ABSTIME)) { 795 + new_expires += val; 734 796 } 735 797 736 798 /* ··· 739 801 * arm the timer (we'll just fake it for timer_gettime). 740 802 */ 741 803 timer->it.cpu.expires = new_expires; 742 - if (new_expires.sched != 0 && 743 - cpu_time_before(timer->it_clock, val, new_expires)) { 804 + if (new_expires != 0 && val < new_expires) { 744 805 arm_timer(timer); 745 806 } 746 807 ··· 763 826 timer->it_overrun_last = 0; 764 827 timer->it_overrun = -1; 765 828 766 - if (new_expires.sched != 0 && 767 - !cpu_time_before(timer->it_clock, val, new_expires)) { 829 + if (new_expires != 0 && !(val < new_expires)) { 768 830 /* 769 831 * The designated time already passed, so we notify 770 832 * immediately, even if the thread never runs to ··· 785 849 786 850 static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp) 787 851 { 788 - union cpu_time_count now; 852 + unsigned long long now; 789 853 struct task_struct *p = timer->it.cpu.task; 790 854 int clear_dead; 791 855 ··· 795 859 sample_to_timespec(timer->it_clock, 796 860 timer->it.cpu.incr, &itp->it_interval); 797 861 798 - if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */ 862 + if (timer->it.cpu.expires == 0) { /* Timer not armed at all. */ 799 863 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0; 800 864 return; 801 865 } ··· 827 891 */ 828 892 put_task_struct(p); 829 893 timer->it.cpu.task = NULL; 830 - timer->it.cpu.expires.sched = 0; 894 + timer->it.cpu.expires = 0; 831 895 read_unlock(&tasklist_lock); 832 896 goto dead; 833 897 } else { ··· 848 912 goto dead; 849 913 } 850 914 851 - if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) { 915 + if (now < timer->it.cpu.expires) { 852 916 sample_to_timespec(timer->it_clock, 853 - cpu_time_sub(timer->it_clock, 854 - timer->it.cpu.expires, now), 917 + timer->it.cpu.expires - now, 855 918 &itp->it_value); 856 919 } else { 857 920 /* ··· 862 927 } 863 928 } 864 929 930 + static unsigned long long 931 + check_timers_list(struct list_head *timers, 932 + struct list_head *firing, 933 + unsigned long long curr) 934 + { 935 + int maxfire = 20; 936 + 937 + while (!list_empty(timers)) { 938 + struct cpu_timer_list *t; 939 + 940 + t = list_first_entry(timers, struct cpu_timer_list, entry); 941 + 942 + if (!--maxfire || curr < t->expires) 943 + return t->expires; 944 + 945 + t->firing = 1; 946 + list_move_tail(&t->entry, firing); 947 + } 948 + 949 + return 0; 950 + } 951 + 865 952 /* 866 953 * Check for any per-thread CPU timers that have fired and move them off 867 954 * the tsk->cpu_timers[N] list onto the firing list. Here we update the ··· 892 935 static void check_thread_timers(struct task_struct *tsk, 893 936 struct list_head *firing) 894 937 { 895 - int maxfire; 896 938 struct list_head *timers = tsk->cpu_timers; 897 939 struct signal_struct *const sig = tsk->signal; 940 + struct task_cputime *tsk_expires = &tsk->cputime_expires; 941 + unsigned long long expires; 898 942 unsigned long soft; 899 943 900 - maxfire = 20; 901 - tsk->cputime_expires.prof_exp = 0; 902 - while (!list_empty(timers)) { 903 - struct cpu_timer_list *t = list_first_entry(timers, 904 - struct cpu_timer_list, 905 - entry); 906 - if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) { 907 - tsk->cputime_expires.prof_exp = t->expires.cpu; 908 - break; 909 - } 910 - t->firing = 1; 911 - list_move_tail(&t->entry, firing); 912 - } 944 + expires = check_timers_list(timers, firing, prof_ticks(tsk)); 945 + tsk_expires->prof_exp = expires_to_cputime(expires); 913 946 914 - ++timers; 915 - maxfire = 20; 916 - tsk->cputime_expires.virt_exp = 0; 917 - while (!list_empty(timers)) { 918 - struct cpu_timer_list *t = list_first_entry(timers, 919 - struct cpu_timer_list, 920 - entry); 921 - if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) { 922 - tsk->cputime_expires.virt_exp = t->expires.cpu; 923 - break; 924 - } 925 - t->firing = 1; 926 - list_move_tail(&t->entry, firing); 927 - } 947 + expires = check_timers_list(++timers, firing, virt_ticks(tsk)); 948 + tsk_expires->virt_exp = expires_to_cputime(expires); 928 949 929 - ++timers; 930 - maxfire = 20; 931 - tsk->cputime_expires.sched_exp = 0; 932 - while (!list_empty(timers)) { 933 - struct cpu_timer_list *t = list_first_entry(timers, 934 - struct cpu_timer_list, 935 - entry); 936 - if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) { 937 - tsk->cputime_expires.sched_exp = t->expires.sched; 938 - break; 939 - } 940 - t->firing = 1; 941 - list_move_tail(&t->entry, firing); 942 - } 950 + tsk_expires->sched_exp = check_timers_list(++timers, firing, 951 + tsk->se.sum_exec_runtime); 943 952 944 953 /* 945 954 * Check for the special case thread timers. ··· 953 1030 static u32 onecputick; 954 1031 955 1032 static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, 956 - cputime_t *expires, cputime_t cur_time, int signo) 1033 + unsigned long long *expires, 1034 + unsigned long long cur_time, int signo) 957 1035 { 958 1036 if (!it->expires) 959 1037 return; ··· 990 1066 static void check_process_timers(struct task_struct *tsk, 991 1067 struct list_head *firing) 992 1068 { 993 - int maxfire; 994 1069 struct signal_struct *const sig = tsk->signal; 995 - cputime_t utime, ptime, virt_expires, prof_expires; 1070 + unsigned long long utime, ptime, virt_expires, prof_expires; 996 1071 unsigned long long sum_sched_runtime, sched_expires; 997 1072 struct list_head *timers = sig->cpu_timers; 998 1073 struct task_cputime cputime; ··· 1001 1078 * Collect the current process totals. 1002 1079 */ 1003 1080 thread_group_cputimer(tsk, &cputime); 1004 - utime = cputime.utime; 1005 - ptime = utime + cputime.stime; 1081 + utime = cputime_to_expires(cputime.utime); 1082 + ptime = utime + cputime_to_expires(cputime.stime); 1006 1083 sum_sched_runtime = cputime.sum_exec_runtime; 1007 - maxfire = 20; 1008 - prof_expires = 0; 1009 - while (!list_empty(timers)) { 1010 - struct cpu_timer_list *tl = list_first_entry(timers, 1011 - struct cpu_timer_list, 1012 - entry); 1013 - if (!--maxfire || ptime < tl->expires.cpu) { 1014 - prof_expires = tl->expires.cpu; 1015 - break; 1016 - } 1017 - tl->firing = 1; 1018 - list_move_tail(&tl->entry, firing); 1019 - } 1020 1084 1021 - ++timers; 1022 - maxfire = 20; 1023 - virt_expires = 0; 1024 - while (!list_empty(timers)) { 1025 - struct cpu_timer_list *tl = list_first_entry(timers, 1026 - struct cpu_timer_list, 1027 - entry); 1028 - if (!--maxfire || utime < tl->expires.cpu) { 1029 - virt_expires = tl->expires.cpu; 1030 - break; 1031 - } 1032 - tl->firing = 1; 1033 - list_move_tail(&tl->entry, firing); 1034 - } 1035 - 1036 - ++timers; 1037 - maxfire = 20; 1038 - sched_expires = 0; 1039 - while (!list_empty(timers)) { 1040 - struct cpu_timer_list *tl = list_first_entry(timers, 1041 - struct cpu_timer_list, 1042 - entry); 1043 - if (!--maxfire || sum_sched_runtime < tl->expires.sched) { 1044 - sched_expires = tl->expires.sched; 1045 - break; 1046 - } 1047 - tl->firing = 1; 1048 - list_move_tail(&tl->entry, firing); 1049 - } 1085 + prof_expires = check_timers_list(timers, firing, ptime); 1086 + virt_expires = check_timers_list(++timers, firing, utime); 1087 + sched_expires = check_timers_list(++timers, firing, sum_sched_runtime); 1050 1088 1051 1089 /* 1052 1090 * Check for the special case process timers. ··· 1046 1162 } 1047 1163 } 1048 1164 1049 - sig->cputime_expires.prof_exp = prof_expires; 1050 - sig->cputime_expires.virt_exp = virt_expires; 1165 + sig->cputime_expires.prof_exp = expires_to_cputime(prof_expires); 1166 + sig->cputime_expires.virt_exp = expires_to_cputime(virt_expires); 1051 1167 sig->cputime_expires.sched_exp = sched_expires; 1052 1168 if (task_cputime_zero(&sig->cputime_expires)) 1053 1169 stop_process_timers(sig); ··· 1060 1176 void posix_cpu_timer_schedule(struct k_itimer *timer) 1061 1177 { 1062 1178 struct task_struct *p = timer->it.cpu.task; 1063 - union cpu_time_count now; 1179 + unsigned long long now; 1064 1180 1065 1181 if (unlikely(p == NULL)) 1066 1182 /* ··· 1089 1205 */ 1090 1206 put_task_struct(p); 1091 1207 timer->it.cpu.task = p = NULL; 1092 - timer->it.cpu.expires.sched = 0; 1208 + timer->it.cpu.expires = 0; 1093 1209 goto out_unlock; 1094 1210 } else if (unlikely(p->exit_state) && thread_group_empty(p)) { 1095 1211 /* ··· 1097 1213 * not yet reaped. Take this opportunity to 1098 1214 * drop our task ref. 1099 1215 */ 1216 + cpu_timer_sample_group(timer->it_clock, p, &now); 1100 1217 clear_dead_task(timer, now); 1101 1218 goto out_unlock; 1102 1219 } ··· 1272 1387 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, 1273 1388 cputime_t *newval, cputime_t *oldval) 1274 1389 { 1275 - union cpu_time_count now; 1390 + unsigned long long now; 1276 1391 1277 1392 BUG_ON(clock_idx == CPUCLOCK_SCHED); 1278 1393 cpu_timer_sample_group(clock_idx, tsk, &now); ··· 1284 1399 * it to be absolute. 1285 1400 */ 1286 1401 if (*oldval) { 1287 - if (*oldval <= now.cpu) { 1402 + if (*oldval <= now) { 1288 1403 /* Just about to fire. */ 1289 1404 *oldval = cputime_one_jiffy; 1290 1405 } else { 1291 - *oldval -= now.cpu; 1406 + *oldval -= now; 1292 1407 } 1293 1408 } 1294 1409 1295 1410 if (!*newval) 1296 1411 goto out; 1297 - *newval += now.cpu; 1412 + *newval += now; 1298 1413 } 1299 1414 1300 1415 /* ··· 1344 1459 } 1345 1460 1346 1461 while (!signal_pending(current)) { 1347 - if (timer.it.cpu.expires.sched == 0) { 1462 + if (timer.it.cpu.expires == 0) { 1348 1463 /* 1349 1464 * Our timer fired and was reset, below 1350 1465 * deletion can not fail.
+36 -3
kernel/sched/stats.h
··· 162 162 */ 163 163 164 164 /** 165 + * cputimer_running - return true if cputimer is running 166 + * 167 + * @tsk: Pointer to target task. 168 + */ 169 + static inline bool cputimer_running(struct task_struct *tsk) 170 + 171 + { 172 + struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 173 + 174 + if (!cputimer->running) 175 + return false; 176 + 177 + /* 178 + * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime 179 + * in __exit_signal(), we won't account to the signal struct further 180 + * cputime consumed by that task, even though the task can still be 181 + * ticking after __exit_signal(). 182 + * 183 + * In order to keep a consistent behaviour between thread group cputime 184 + * and thread group cputimer accounting, lets also ignore the cputime 185 + * elapsing after __exit_signal() in any thread group timer running. 186 + * 187 + * This makes sure that POSIX CPU clocks and timers are synchronized, so 188 + * that a POSIX CPU timer won't expire while the corresponding POSIX CPU 189 + * clock delta is behind the expiring timer value. 190 + */ 191 + if (unlikely(!tsk->sighand)) 192 + return false; 193 + 194 + return true; 195 + } 196 + 197 + /** 165 198 * account_group_user_time - Maintain utime for a thread group. 166 199 * 167 200 * @tsk: Pointer to task structure. ··· 209 176 { 210 177 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 211 178 212 - if (!cputimer->running) 179 + if (!cputimer_running(tsk)) 213 180 return; 214 181 215 182 raw_spin_lock(&cputimer->lock); ··· 232 199 { 233 200 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 234 201 235 - if (!cputimer->running) 202 + if (!cputimer_running(tsk)) 236 203 return; 237 204 238 205 raw_spin_lock(&cputimer->lock); ··· 255 222 { 256 223 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 257 224 258 - if (!cputimer->running) 225 + if (!cputimer_running(tsk)) 259 226 return; 260 227 261 228 raw_spin_lock(&cputimer->lock);
+2
kernel/time/Makefile
··· 4 4 obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o 5 5 obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o 6 6 obj-$(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) += tick-broadcast.o 7 + obj-$(CONFIG_GENERIC_SCHED_CLOCK) += sched_clock.o 7 8 obj-$(CONFIG_TICK_ONESHOT) += tick-oneshot.o 8 9 obj-$(CONFIG_TICK_ONESHOT) += tick-sched.o 9 10 obj-$(CONFIG_TIMER_STATS) += timer_stats.o 11 + obj-$(CONFIG_DEBUG_FS) += timekeeping_debug.o
+46 -1
kernel/time/alarmtimer.c
··· 199 199 200 200 } 201 201 202 + ktime_t alarm_expires_remaining(const struct alarm *alarm) 203 + { 204 + struct alarm_base *base = &alarm_bases[alarm->type]; 205 + return ktime_sub(alarm->node.expires, base->gettime()); 206 + } 207 + EXPORT_SYMBOL_GPL(alarm_expires_remaining); 208 + 202 209 #ifdef CONFIG_RTC_CLASS 203 210 /** 204 211 * alarmtimer_suspend - Suspend time callback ··· 310 303 alarm->type = type; 311 304 alarm->state = ALARMTIMER_STATE_INACTIVE; 312 305 } 306 + EXPORT_SYMBOL_GPL(alarm_init); 313 307 314 308 /** 315 - * alarm_start - Sets an alarm to fire 309 + * alarm_start - Sets an absolute alarm to fire 316 310 * @alarm: ptr to alarm to set 317 311 * @start: time to run the alarm 318 312 */ ··· 331 323 spin_unlock_irqrestore(&base->lock, flags); 332 324 return ret; 333 325 } 326 + EXPORT_SYMBOL_GPL(alarm_start); 327 + 328 + /** 329 + * alarm_start_relative - Sets a relative alarm to fire 330 + * @alarm: ptr to alarm to set 331 + * @start: time relative to now to run the alarm 332 + */ 333 + int alarm_start_relative(struct alarm *alarm, ktime_t start) 334 + { 335 + struct alarm_base *base = &alarm_bases[alarm->type]; 336 + 337 + start = ktime_add(start, base->gettime()); 338 + return alarm_start(alarm, start); 339 + } 340 + EXPORT_SYMBOL_GPL(alarm_start_relative); 341 + 342 + void alarm_restart(struct alarm *alarm) 343 + { 344 + struct alarm_base *base = &alarm_bases[alarm->type]; 345 + unsigned long flags; 346 + 347 + spin_lock_irqsave(&base->lock, flags); 348 + hrtimer_set_expires(&alarm->timer, alarm->node.expires); 349 + hrtimer_restart(&alarm->timer); 350 + alarmtimer_enqueue(base, alarm); 351 + spin_unlock_irqrestore(&base->lock, flags); 352 + } 353 + EXPORT_SYMBOL_GPL(alarm_restart); 334 354 335 355 /** 336 356 * alarm_try_to_cancel - Tries to cancel an alarm timer ··· 380 344 spin_unlock_irqrestore(&base->lock, flags); 381 345 return ret; 382 346 } 347 + EXPORT_SYMBOL_GPL(alarm_try_to_cancel); 383 348 384 349 385 350 /** ··· 398 361 cpu_relax(); 399 362 } 400 363 } 364 + EXPORT_SYMBOL_GPL(alarm_cancel); 401 365 402 366 403 367 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval) ··· 431 393 alarm->node.expires = ktime_add(alarm->node.expires, interval); 432 394 return overrun; 433 395 } 396 + EXPORT_SYMBOL_GPL(alarm_forward); 434 397 398 + u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) 399 + { 400 + struct alarm_base *base = &alarm_bases[alarm->type]; 435 401 402 + return alarm_forward(alarm, base->gettime(), interval); 403 + } 404 + EXPORT_SYMBOL_GPL(alarm_forward_now); 436 405 437 406 438 407 /**
+241 -32
kernel/time/clockevents.c
··· 15 15 #include <linux/hrtimer.h> 16 16 #include <linux/init.h> 17 17 #include <linux/module.h> 18 - #include <linux/notifier.h> 19 18 #include <linux/smp.h> 19 + #include <linux/device.h> 20 20 21 21 #include "tick-internal.h" 22 22 23 23 /* The registered clock event devices */ 24 24 static LIST_HEAD(clockevent_devices); 25 25 static LIST_HEAD(clockevents_released); 26 - 27 - /* Notification for clock events */ 28 - static RAW_NOTIFIER_HEAD(clockevents_chain); 29 - 30 26 /* Protection for the above */ 31 27 static DEFINE_RAW_SPINLOCK(clockevents_lock); 28 + /* Protection for unbind operations */ 29 + static DEFINE_MUTEX(clockevents_mutex); 30 + 31 + struct ce_unbind { 32 + struct clock_event_device *ce; 33 + int res; 34 + }; 32 35 33 36 /** 34 37 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds ··· 235 232 return (rc && force) ? clockevents_program_min_delta(dev) : rc; 236 233 } 237 234 238 - /** 239 - * clockevents_register_notifier - register a clock events change listener 240 - */ 241 - int clockevents_register_notifier(struct notifier_block *nb) 242 - { 243 - unsigned long flags; 244 - int ret; 245 - 246 - raw_spin_lock_irqsave(&clockevents_lock, flags); 247 - ret = raw_notifier_chain_register(&clockevents_chain, nb); 248 - raw_spin_unlock_irqrestore(&clockevents_lock, flags); 249 - 250 - return ret; 251 - } 252 - 253 - /* 254 - * Notify about a clock event change. Called with clockevents_lock 255 - * held. 256 - */ 257 - static void clockevents_do_notify(unsigned long reason, void *dev) 258 - { 259 - raw_notifier_call_chain(&clockevents_chain, reason, dev); 260 - } 261 - 262 235 /* 263 236 * Called after a notify add to make devices available which were 264 237 * released from the notifier call. ··· 248 269 struct clock_event_device, list); 249 270 list_del(&dev->list); 250 271 list_add(&dev->list, &clockevent_devices); 251 - clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); 272 + tick_check_new_device(dev); 252 273 } 253 274 } 275 + 276 + /* 277 + * Try to install a replacement clock event device 278 + */ 279 + static int clockevents_replace(struct clock_event_device *ced) 280 + { 281 + struct clock_event_device *dev, *newdev = NULL; 282 + 283 + list_for_each_entry(dev, &clockevent_devices, list) { 284 + if (dev == ced || dev->mode != CLOCK_EVT_MODE_UNUSED) 285 + continue; 286 + 287 + if (!tick_check_replacement(newdev, dev)) 288 + continue; 289 + 290 + if (!try_module_get(dev->owner)) 291 + continue; 292 + 293 + if (newdev) 294 + module_put(newdev->owner); 295 + newdev = dev; 296 + } 297 + if (newdev) { 298 + tick_install_replacement(newdev); 299 + list_del_init(&ced->list); 300 + } 301 + return newdev ? 0 : -EBUSY; 302 + } 303 + 304 + /* 305 + * Called with clockevents_mutex and clockevents_lock held 306 + */ 307 + static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu) 308 + { 309 + /* Fast track. Device is unused */ 310 + if (ced->mode == CLOCK_EVT_MODE_UNUSED) { 311 + list_del_init(&ced->list); 312 + return 0; 313 + } 314 + 315 + return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY; 316 + } 317 + 318 + /* 319 + * SMP function call to unbind a device 320 + */ 321 + static void __clockevents_unbind(void *arg) 322 + { 323 + struct ce_unbind *cu = arg; 324 + int res; 325 + 326 + raw_spin_lock(&clockevents_lock); 327 + res = __clockevents_try_unbind(cu->ce, smp_processor_id()); 328 + if (res == -EAGAIN) 329 + res = clockevents_replace(cu->ce); 330 + cu->res = res; 331 + raw_spin_unlock(&clockevents_lock); 332 + } 333 + 334 + /* 335 + * Issues smp function call to unbind a per cpu device. Called with 336 + * clockevents_mutex held. 337 + */ 338 + static int clockevents_unbind(struct clock_event_device *ced, int cpu) 339 + { 340 + struct ce_unbind cu = { .ce = ced, .res = -ENODEV }; 341 + 342 + smp_call_function_single(cpu, __clockevents_unbind, &cu, 1); 343 + return cu.res; 344 + } 345 + 346 + /* 347 + * Unbind a clockevents device. 348 + */ 349 + int clockevents_unbind_device(struct clock_event_device *ced, int cpu) 350 + { 351 + int ret; 352 + 353 + mutex_lock(&clockevents_mutex); 354 + ret = clockevents_unbind(ced, cpu); 355 + mutex_unlock(&clockevents_mutex); 356 + return ret; 357 + } 358 + EXPORT_SYMBOL_GPL(clockevents_unbind); 254 359 255 360 /** 256 361 * clockevents_register_device - register a clock event device ··· 353 290 raw_spin_lock_irqsave(&clockevents_lock, flags); 354 291 355 292 list_add(&dev->list, &clockevent_devices); 356 - clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); 293 + tick_check_new_device(dev); 357 294 clockevents_notify_released(); 358 295 359 296 raw_spin_unlock_irqrestore(&clockevents_lock, flags); ··· 449 386 * released list and do a notify add later. 450 387 */ 451 388 if (old) { 389 + module_put(old->owner); 452 390 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); 453 391 list_del(&old->list); 454 392 list_add(&old->list, &clockevents_released); ··· 497 433 int cpu; 498 434 499 435 raw_spin_lock_irqsave(&clockevents_lock, flags); 500 - clockevents_do_notify(reason, arg); 501 436 502 437 switch (reason) { 438 + case CLOCK_EVT_NOTIFY_BROADCAST_ON: 439 + case CLOCK_EVT_NOTIFY_BROADCAST_OFF: 440 + case CLOCK_EVT_NOTIFY_BROADCAST_FORCE: 441 + tick_broadcast_on_off(reason, arg); 442 + break; 443 + 444 + case CLOCK_EVT_NOTIFY_BROADCAST_ENTER: 445 + case CLOCK_EVT_NOTIFY_BROADCAST_EXIT: 446 + tick_broadcast_oneshot_control(reason); 447 + break; 448 + 449 + case CLOCK_EVT_NOTIFY_CPU_DYING: 450 + tick_handover_do_timer(arg); 451 + break; 452 + 453 + case CLOCK_EVT_NOTIFY_SUSPEND: 454 + tick_suspend(); 455 + tick_suspend_broadcast(); 456 + break; 457 + 458 + case CLOCK_EVT_NOTIFY_RESUME: 459 + tick_resume(); 460 + break; 461 + 503 462 case CLOCK_EVT_NOTIFY_CPU_DEAD: 463 + tick_shutdown_broadcast_oneshot(arg); 464 + tick_shutdown_broadcast(arg); 465 + tick_shutdown(arg); 504 466 /* 505 467 * Unregister the clock event devices which were 506 468 * released from the users in the notify chain. ··· 552 462 raw_spin_unlock_irqrestore(&clockevents_lock, flags); 553 463 } 554 464 EXPORT_SYMBOL_GPL(clockevents_notify); 465 + 466 + #ifdef CONFIG_SYSFS 467 + struct bus_type clockevents_subsys = { 468 + .name = "clockevents", 469 + .dev_name = "clockevent", 470 + }; 471 + 472 + static DEFINE_PER_CPU(struct device, tick_percpu_dev); 473 + static struct tick_device *tick_get_tick_dev(struct device *dev); 474 + 475 + static ssize_t sysfs_show_current_tick_dev(struct device *dev, 476 + struct device_attribute *attr, 477 + char *buf) 478 + { 479 + struct tick_device *td; 480 + ssize_t count = 0; 481 + 482 + raw_spin_lock_irq(&clockevents_lock); 483 + td = tick_get_tick_dev(dev); 484 + if (td && td->evtdev) 485 + count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name); 486 + raw_spin_unlock_irq(&clockevents_lock); 487 + return count; 488 + } 489 + static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL); 490 + 491 + /* We don't support the abomination of removable broadcast devices */ 492 + static ssize_t sysfs_unbind_tick_dev(struct device *dev, 493 + struct device_attribute *attr, 494 + const char *buf, size_t count) 495 + { 496 + char name[CS_NAME_LEN]; 497 + size_t ret = sysfs_get_uname(buf, name, count); 498 + struct clock_event_device *ce; 499 + 500 + if (ret < 0) 501 + return ret; 502 + 503 + ret = -ENODEV; 504 + mutex_lock(&clockevents_mutex); 505 + raw_spin_lock_irq(&clockevents_lock); 506 + list_for_each_entry(ce, &clockevent_devices, list) { 507 + if (!strcmp(ce->name, name)) { 508 + ret = __clockevents_try_unbind(ce, dev->id); 509 + break; 510 + } 511 + } 512 + raw_spin_unlock_irq(&clockevents_lock); 513 + /* 514 + * We hold clockevents_mutex, so ce can't go away 515 + */ 516 + if (ret == -EAGAIN) 517 + ret = clockevents_unbind(ce, dev->id); 518 + mutex_unlock(&clockevents_mutex); 519 + return ret ? ret : count; 520 + } 521 + static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev); 522 + 523 + #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 524 + static struct device tick_bc_dev = { 525 + .init_name = "broadcast", 526 + .id = 0, 527 + .bus = &clockevents_subsys, 528 + }; 529 + 530 + static struct tick_device *tick_get_tick_dev(struct device *dev) 531 + { 532 + return dev == &tick_bc_dev ? tick_get_broadcast_device() : 533 + &per_cpu(tick_cpu_device, dev->id); 534 + } 535 + 536 + static __init int tick_broadcast_init_sysfs(void) 537 + { 538 + int err = device_register(&tick_bc_dev); 539 + 540 + if (!err) 541 + err = device_create_file(&tick_bc_dev, &dev_attr_current_device); 542 + return err; 543 + } 544 + #else 545 + static struct tick_device *tick_get_tick_dev(struct device *dev) 546 + { 547 + return &per_cpu(tick_cpu_device, dev->id); 548 + } 549 + static inline int tick_broadcast_init_sysfs(void) { return 0; } 555 550 #endif 551 + 552 + static int __init tick_init_sysfs(void) 553 + { 554 + int cpu; 555 + 556 + for_each_possible_cpu(cpu) { 557 + struct device *dev = &per_cpu(tick_percpu_dev, cpu); 558 + int err; 559 + 560 + dev->id = cpu; 561 + dev->bus = &clockevents_subsys; 562 + err = device_register(dev); 563 + if (!err) 564 + err = device_create_file(dev, &dev_attr_current_device); 565 + if (!err) 566 + err = device_create_file(dev, &dev_attr_unbind_device); 567 + if (err) 568 + return err; 569 + } 570 + return tick_broadcast_init_sysfs(); 571 + } 572 + 573 + static int __init clockevents_init_sysfs(void) 574 + { 575 + int err = subsys_system_register(&clockevents_subsys, NULL); 576 + 577 + if (!err) 578 + err = tick_init_sysfs(); 579 + return err; 580 + } 581 + device_initcall(clockevents_init_sysfs); 582 + #endif /* SYSFS */ 583 + 584 + #endif /* GENERIC_CLOCK_EVENTS */
+199 -67
kernel/time/clocksource.c
··· 31 31 #include <linux/tick.h> 32 32 #include <linux/kthread.h> 33 33 34 + #include "tick-internal.h" 35 + 34 36 void timecounter_init(struct timecounter *tc, 35 37 const struct cyclecounter *cc, 36 38 u64 start_tstamp) ··· 176 174 static struct clocksource *curr_clocksource; 177 175 static LIST_HEAD(clocksource_list); 178 176 static DEFINE_MUTEX(clocksource_mutex); 179 - static char override_name[32]; 177 + static char override_name[CS_NAME_LEN]; 180 178 static int finished_booting; 181 179 182 180 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG 183 181 static void clocksource_watchdog_work(struct work_struct *work); 182 + static void clocksource_select(void); 184 183 185 184 static LIST_HEAD(watchdog_list); 186 185 static struct clocksource *watchdog; ··· 302 299 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && 303 300 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && 304 301 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { 302 + /* Mark it valid for high-res. */ 305 303 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 304 + 306 305 /* 307 - * We just marked the clocksource as highres-capable, 308 - * notify the rest of the system as well so that we 309 - * transition into high-res mode: 306 + * clocksource_done_booting() will sort it if 307 + * finished_booting is not set yet. 310 308 */ 311 - tick_clock_notify(); 309 + if (!finished_booting) 310 + continue; 311 + 312 + /* 313 + * If this is not the current clocksource let 314 + * the watchdog thread reselect it. Due to the 315 + * change to high res this clocksource might 316 + * be preferred now. If it is the current 317 + * clocksource let the tick code know about 318 + * that change. 319 + */ 320 + if (cs != curr_clocksource) { 321 + cs->flags |= CLOCK_SOURCE_RESELECT; 322 + schedule_work(&watchdog_work); 323 + } else { 324 + tick_clock_notify(); 325 + } 312 326 } 313 327 } 314 328 ··· 408 388 409 389 static void clocksource_dequeue_watchdog(struct clocksource *cs) 410 390 { 411 - struct clocksource *tmp; 412 391 unsigned long flags; 413 392 414 393 spin_lock_irqsave(&watchdog_lock, flags); 415 - if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 416 - /* cs is a watched clocksource. */ 417 - list_del_init(&cs->wd_list); 418 - } else if (cs == watchdog) { 419 - /* Reset watchdog cycles */ 420 - clocksource_reset_watchdog(); 421 - /* Current watchdog is removed. Find an alternative. */ 422 - watchdog = NULL; 423 - list_for_each_entry(tmp, &clocksource_list, list) { 424 - if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY) 425 - continue; 426 - if (!watchdog || tmp->rating > watchdog->rating) 427 - watchdog = tmp; 394 + if (cs != watchdog) { 395 + if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 396 + /* cs is a watched clocksource. */ 397 + list_del_init(&cs->wd_list); 398 + /* Check if the watchdog timer needs to be stopped. */ 399 + clocksource_stop_watchdog(); 428 400 } 429 401 } 430 - cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 431 - /* Check if the watchdog timer needs to be stopped. */ 432 - clocksource_stop_watchdog(); 433 402 spin_unlock_irqrestore(&watchdog_lock, flags); 434 403 } 435 404 436 - static int clocksource_watchdog_kthread(void *data) 405 + static int __clocksource_watchdog_kthread(void) 437 406 { 438 407 struct clocksource *cs, *tmp; 439 408 unsigned long flags; 440 409 LIST_HEAD(unstable); 410 + int select = 0; 441 411 442 - mutex_lock(&clocksource_mutex); 443 412 spin_lock_irqsave(&watchdog_lock, flags); 444 - list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) 413 + list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { 445 414 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 446 415 list_del_init(&cs->wd_list); 447 416 list_add(&cs->wd_list, &unstable); 417 + select = 1; 448 418 } 419 + if (cs->flags & CLOCK_SOURCE_RESELECT) { 420 + cs->flags &= ~CLOCK_SOURCE_RESELECT; 421 + select = 1; 422 + } 423 + } 449 424 /* Check if the watchdog timer needs to be stopped. */ 450 425 clocksource_stop_watchdog(); 451 426 spin_unlock_irqrestore(&watchdog_lock, flags); ··· 450 435 list_del_init(&cs->wd_list); 451 436 __clocksource_change_rating(cs, 0); 452 437 } 438 + return select; 439 + } 440 + 441 + static int clocksource_watchdog_kthread(void *data) 442 + { 443 + mutex_lock(&clocksource_mutex); 444 + if (__clocksource_watchdog_kthread()) 445 + clocksource_select(); 453 446 mutex_unlock(&clocksource_mutex); 454 447 return 0; 448 + } 449 + 450 + static bool clocksource_is_watchdog(struct clocksource *cs) 451 + { 452 + return cs == watchdog; 455 453 } 456 454 457 455 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ ··· 477 449 478 450 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } 479 451 static inline void clocksource_resume_watchdog(void) { } 480 - static inline int clocksource_watchdog_kthread(void *data) { return 0; } 452 + static inline int __clocksource_watchdog_kthread(void) { return 0; } 453 + static bool clocksource_is_watchdog(struct clocksource *cs) { return false; } 481 454 482 455 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ 483 456 ··· 582 553 583 554 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET 584 555 585 - /** 586 - * clocksource_select - Select the best clocksource available 587 - * 588 - * Private function. Must hold clocksource_mutex when called. 589 - * 590 - * Select the clocksource with the best rating, or the clocksource, 591 - * which is selected by userspace override. 592 - */ 593 - static void clocksource_select(void) 556 + static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) 594 557 { 595 - struct clocksource *best, *cs; 558 + struct clocksource *cs; 596 559 597 560 if (!finished_booting || list_empty(&clocksource_list)) 561 + return NULL; 562 + 563 + /* 564 + * We pick the clocksource with the highest rating. If oneshot 565 + * mode is active, we pick the highres valid clocksource with 566 + * the best rating. 567 + */ 568 + list_for_each_entry(cs, &clocksource_list, list) { 569 + if (skipcur && cs == curr_clocksource) 570 + continue; 571 + if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 572 + continue; 573 + return cs; 574 + } 575 + return NULL; 576 + } 577 + 578 + static void __clocksource_select(bool skipcur) 579 + { 580 + bool oneshot = tick_oneshot_mode_active(); 581 + struct clocksource *best, *cs; 582 + 583 + /* Find the best suitable clocksource */ 584 + best = clocksource_find_best(oneshot, skipcur); 585 + if (!best) 598 586 return; 599 - /* First clocksource on the list has the best rating. */ 600 - best = list_first_entry(&clocksource_list, struct clocksource, list); 587 + 601 588 /* Check for the override clocksource. */ 602 589 list_for_each_entry(cs, &clocksource_list, list) { 590 + if (skipcur && cs == curr_clocksource) 591 + continue; 603 592 if (strcmp(cs->name, override_name) != 0) 604 593 continue; 605 594 /* ··· 625 578 * capable clocksource if the tick code is in oneshot 626 579 * mode (highres or nohz) 627 580 */ 628 - if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && 629 - tick_oneshot_mode_active()) { 581 + if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { 630 582 /* Override clocksource cannot be used. */ 631 583 printk(KERN_WARNING "Override clocksource %s is not " 632 584 "HRT compatible. Cannot switch while in " ··· 636 590 best = cs; 637 591 break; 638 592 } 639 - if (curr_clocksource != best) { 640 - printk(KERN_INFO "Switching to clocksource %s\n", best->name); 593 + 594 + if (curr_clocksource != best && !timekeeping_notify(best)) { 595 + pr_info("Switched to clocksource %s\n", best->name); 641 596 curr_clocksource = best; 642 - timekeeping_notify(curr_clocksource); 643 597 } 598 + } 599 + 600 + /** 601 + * clocksource_select - Select the best clocksource available 602 + * 603 + * Private function. Must hold clocksource_mutex when called. 604 + * 605 + * Select the clocksource with the best rating, or the clocksource, 606 + * which is selected by userspace override. 607 + */ 608 + static void clocksource_select(void) 609 + { 610 + return __clocksource_select(false); 611 + } 612 + 613 + static void clocksource_select_fallback(void) 614 + { 615 + return __clocksource_select(true); 644 616 } 645 617 646 618 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */ 647 619 648 620 static inline void clocksource_select(void) { } 621 + static inline void clocksource_select_fallback(void) { } 649 622 650 623 #endif 651 624 ··· 679 614 { 680 615 mutex_lock(&clocksource_mutex); 681 616 curr_clocksource = clocksource_default_clock(); 682 - mutex_unlock(&clocksource_mutex); 683 - 684 617 finished_booting = 1; 685 - 686 618 /* 687 619 * Run the watchdog first to eliminate unstable clock sources 688 620 */ 689 - clocksource_watchdog_kthread(NULL); 690 - 691 - mutex_lock(&clocksource_mutex); 621 + __clocksource_watchdog_kthread(); 692 622 clocksource_select(); 693 623 mutex_unlock(&clocksource_mutex); 694 624 return 0; ··· 816 756 list_del(&cs->list); 817 757 cs->rating = rating; 818 758 clocksource_enqueue(cs); 819 - clocksource_select(); 820 759 } 821 760 822 761 /** ··· 827 768 { 828 769 mutex_lock(&clocksource_mutex); 829 770 __clocksource_change_rating(cs, rating); 771 + clocksource_select(); 830 772 mutex_unlock(&clocksource_mutex); 831 773 } 832 774 EXPORT_SYMBOL(clocksource_change_rating); 775 + 776 + /* 777 + * Unbind clocksource @cs. Called with clocksource_mutex held 778 + */ 779 + static int clocksource_unbind(struct clocksource *cs) 780 + { 781 + /* 782 + * I really can't convince myself to support this on hardware 783 + * designed by lobotomized monkeys. 784 + */ 785 + if (clocksource_is_watchdog(cs)) 786 + return -EBUSY; 787 + 788 + if (cs == curr_clocksource) { 789 + /* Select and try to install a replacement clock source */ 790 + clocksource_select_fallback(); 791 + if (curr_clocksource == cs) 792 + return -EBUSY; 793 + } 794 + clocksource_dequeue_watchdog(cs); 795 + list_del_init(&cs->list); 796 + return 0; 797 + } 833 798 834 799 /** 835 800 * clocksource_unregister - remove a registered clocksource 836 801 * @cs: clocksource to be unregistered 837 802 */ 838 - void clocksource_unregister(struct clocksource *cs) 803 + int clocksource_unregister(struct clocksource *cs) 839 804 { 805 + int ret = 0; 806 + 840 807 mutex_lock(&clocksource_mutex); 841 - clocksource_dequeue_watchdog(cs); 842 - list_del(&cs->list); 843 - clocksource_select(); 808 + if (!list_empty(&cs->list)) 809 + ret = clocksource_unbind(cs); 844 810 mutex_unlock(&clocksource_mutex); 811 + return ret; 845 812 } 846 813 EXPORT_SYMBOL(clocksource_unregister); 847 814 ··· 893 808 return count; 894 809 } 895 810 811 + size_t sysfs_get_uname(const char *buf, char *dst, size_t cnt) 812 + { 813 + size_t ret = cnt; 814 + 815 + /* strings from sysfs write are not 0 terminated! */ 816 + if (!cnt || cnt >= CS_NAME_LEN) 817 + return -EINVAL; 818 + 819 + /* strip of \n: */ 820 + if (buf[cnt-1] == '\n') 821 + cnt--; 822 + if (cnt > 0) 823 + memcpy(dst, buf, cnt); 824 + dst[cnt] = 0; 825 + return ret; 826 + } 827 + 896 828 /** 897 829 * sysfs_override_clocksource - interface for manually overriding clocksource 898 830 * @dev: unused ··· 924 822 struct device_attribute *attr, 925 823 const char *buf, size_t count) 926 824 { 927 - size_t ret = count; 928 - 929 - /* strings from sysfs write are not 0 terminated! */ 930 - if (count >= sizeof(override_name)) 931 - return -EINVAL; 932 - 933 - /* strip of \n: */ 934 - if (buf[count-1] == '\n') 935 - count--; 825 + size_t ret; 936 826 937 827 mutex_lock(&clocksource_mutex); 938 828 939 - if (count > 0) 940 - memcpy(override_name, buf, count); 941 - override_name[count] = 0; 942 - clocksource_select(); 829 + ret = sysfs_get_uname(buf, override_name, count); 830 + if (ret >= 0) 831 + clocksource_select(); 943 832 944 833 mutex_unlock(&clocksource_mutex); 945 834 946 835 return ret; 836 + } 837 + 838 + /** 839 + * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource 840 + * @dev: unused 841 + * @attr: unused 842 + * @buf: unused 843 + * @count: length of buffer 844 + * 845 + * Takes input from sysfs interface for manually unbinding a clocksource. 846 + */ 847 + static ssize_t sysfs_unbind_clocksource(struct device *dev, 848 + struct device_attribute *attr, 849 + const char *buf, size_t count) 850 + { 851 + struct clocksource *cs; 852 + char name[CS_NAME_LEN]; 853 + size_t ret; 854 + 855 + ret = sysfs_get_uname(buf, name, count); 856 + if (ret < 0) 857 + return ret; 858 + 859 + ret = -ENODEV; 860 + mutex_lock(&clocksource_mutex); 861 + list_for_each_entry(cs, &clocksource_list, list) { 862 + if (strcmp(cs->name, name)) 863 + continue; 864 + ret = clocksource_unbind(cs); 865 + break; 866 + } 867 + mutex_unlock(&clocksource_mutex); 868 + 869 + return ret ? ret : count; 947 870 } 948 871 949 872 /** ··· 1013 886 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, 1014 887 sysfs_override_clocksource); 1015 888 889 + static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource); 890 + 1016 891 static DEVICE_ATTR(available_clocksource, 0444, 1017 892 sysfs_show_available_clocksources, NULL); 1018 893 ··· 1038 909 error = device_create_file( 1039 910 &device_clocksource, 1040 911 &dev_attr_current_clocksource); 912 + if (!error) 913 + error = device_create_file(&device_clocksource, 914 + &dev_attr_unbind_clocksource); 1041 915 if (!error) 1042 916 error = device_create_file( 1043 917 &device_clocksource,
+104 -22
kernel/time/tick-broadcast.c
··· 19 19 #include <linux/profile.h> 20 20 #include <linux/sched.h> 21 21 #include <linux/smp.h> 22 + #include <linux/module.h> 22 23 23 24 #include "tick-internal.h" 24 25 ··· 30 29 31 30 static struct tick_device tick_broadcast_device; 32 31 static cpumask_var_t tick_broadcast_mask; 32 + static cpumask_var_t tick_broadcast_on; 33 33 static cpumask_var_t tmpmask; 34 34 static DEFINE_RAW_SPINLOCK(tick_broadcast_lock); 35 35 static int tick_broadcast_force; ··· 66 64 /* 67 65 * Check, if the device can be utilized as broadcast device: 68 66 */ 69 - int tick_check_broadcast_device(struct clock_event_device *dev) 67 + static bool tick_check_broadcast_device(struct clock_event_device *curdev, 68 + struct clock_event_device *newdev) 69 + { 70 + if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) || 71 + (newdev->features & CLOCK_EVT_FEAT_C3STOP)) 72 + return false; 73 + 74 + if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT && 75 + !(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) 76 + return false; 77 + 78 + return !curdev || newdev->rating > curdev->rating; 79 + } 80 + 81 + /* 82 + * Conditionally install/replace broadcast device 83 + */ 84 + void tick_install_broadcast_device(struct clock_event_device *dev) 70 85 { 71 86 struct clock_event_device *cur = tick_broadcast_device.evtdev; 72 87 73 - if ((dev->features & CLOCK_EVT_FEAT_DUMMY) || 74 - (tick_broadcast_device.evtdev && 75 - tick_broadcast_device.evtdev->rating >= dev->rating) || 76 - (dev->features & CLOCK_EVT_FEAT_C3STOP)) 77 - return 0; 88 + if (!tick_check_broadcast_device(cur, dev)) 89 + return; 78 90 79 - clockevents_exchange_device(tick_broadcast_device.evtdev, dev); 91 + if (!try_module_get(dev->owner)) 92 + return; 93 + 94 + clockevents_exchange_device(cur, dev); 80 95 if (cur) 81 96 cur->event_handler = clockevents_handle_noop; 82 97 tick_broadcast_device.evtdev = dev; ··· 109 90 */ 110 91 if (dev->features & CLOCK_EVT_FEAT_ONESHOT) 111 92 tick_clock_notify(); 112 - return 1; 113 93 } 114 94 115 95 /* ··· 141 123 */ 142 124 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) 143 125 { 126 + struct clock_event_device *bc = tick_broadcast_device.evtdev; 144 127 unsigned long flags; 145 - int ret = 0; 128 + int ret; 146 129 147 130 raw_spin_lock_irqsave(&tick_broadcast_lock, flags); 148 131 ··· 157 138 dev->event_handler = tick_handle_periodic; 158 139 tick_device_setup_broadcast_func(dev); 159 140 cpumask_set_cpu(cpu, tick_broadcast_mask); 160 - tick_broadcast_start_periodic(tick_broadcast_device.evtdev); 141 + tick_broadcast_start_periodic(bc); 161 142 ret = 1; 162 143 } else { 163 144 /* 164 - * When the new device is not affected by the stop 165 - * feature and the cpu is marked in the broadcast mask 166 - * then clear the broadcast bit. 145 + * Clear the broadcast bit for this cpu if the 146 + * device is not power state affected. 167 147 */ 168 - if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) { 169 - int cpu = smp_processor_id(); 148 + if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) 170 149 cpumask_clear_cpu(cpu, tick_broadcast_mask); 171 - tick_broadcast_clear_oneshot(cpu); 172 - } else { 150 + else 173 151 tick_device_setup_broadcast_func(dev); 152 + 153 + /* 154 + * Clear the broadcast bit if the CPU is not in 155 + * periodic broadcast on state. 156 + */ 157 + if (!cpumask_test_cpu(cpu, tick_broadcast_on)) 158 + cpumask_clear_cpu(cpu, tick_broadcast_mask); 159 + 160 + switch (tick_broadcast_device.mode) { 161 + case TICKDEV_MODE_ONESHOT: 162 + /* 163 + * If the system is in oneshot mode we can 164 + * unconditionally clear the oneshot mask bit, 165 + * because the CPU is running and therefore 166 + * not in an idle state which causes the power 167 + * state affected device to stop. Let the 168 + * caller initialize the device. 169 + */ 170 + tick_broadcast_clear_oneshot(cpu); 171 + ret = 0; 172 + break; 173 + 174 + case TICKDEV_MODE_PERIODIC: 175 + /* 176 + * If the system is in periodic mode, check 177 + * whether the broadcast device can be 178 + * switched off now. 179 + */ 180 + if (cpumask_empty(tick_broadcast_mask) && bc) 181 + clockevents_shutdown(bc); 182 + /* 183 + * If we kept the cpu in the broadcast mask, 184 + * tell the caller to leave the per cpu device 185 + * in shutdown state. The periodic interrupt 186 + * is delivered by the broadcast device. 187 + */ 188 + ret = cpumask_test_cpu(cpu, tick_broadcast_mask); 189 + break; 190 + default: 191 + /* Nothing to do */ 192 + ret = 0; 193 + break; 174 194 } 175 195 } 176 196 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); ··· 339 281 switch (*reason) { 340 282 case CLOCK_EVT_NOTIFY_BROADCAST_ON: 341 283 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE: 284 + cpumask_set_cpu(cpu, tick_broadcast_on); 342 285 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) { 343 286 if (tick_broadcast_device.mode == 344 287 TICKDEV_MODE_PERIODIC) ··· 349 290 tick_broadcast_force = 1; 350 291 break; 351 292 case CLOCK_EVT_NOTIFY_BROADCAST_OFF: 352 - if (!tick_broadcast_force && 353 - cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) { 293 + if (tick_broadcast_force) 294 + break; 295 + cpumask_clear_cpu(cpu, tick_broadcast_on); 296 + if (!tick_device_is_functional(dev)) 297 + break; 298 + if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) { 354 299 if (tick_broadcast_device.mode == 355 300 TICKDEV_MODE_PERIODIC) 356 301 tick_setup_periodic(dev, 0); ··· 412 349 413 350 bc = tick_broadcast_device.evtdev; 414 351 cpumask_clear_cpu(cpu, tick_broadcast_mask); 352 + cpumask_clear_cpu(cpu, tick_broadcast_on); 415 353 416 354 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) { 417 355 if (bc && cpumask_empty(tick_broadcast_mask)) ··· 539 475 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) { 540 476 struct tick_device *td = &per_cpu(tick_cpu_device, cpu); 541 477 542 - clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT); 478 + /* 479 + * We might be in the middle of switching over from 480 + * periodic to oneshot. If the CPU has not yet 481 + * switched over, leave the device alone. 482 + */ 483 + if (td->mode == TICKDEV_MODE_ONESHOT) { 484 + clockevents_set_mode(td->evtdev, 485 + CLOCK_EVT_MODE_ONESHOT); 486 + } 543 487 } 544 488 } 545 489 ··· 592 520 /* Take care of enforced broadcast requests */ 593 521 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask); 594 522 cpumask_clear(tick_broadcast_force_mask); 523 + 524 + /* 525 + * Sanity check. Catch the case where we try to broadcast to 526 + * offline cpus. 527 + */ 528 + if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask))) 529 + cpumask_and(tmpmask, tmpmask, cpu_online_mask); 595 530 596 531 /* 597 532 * Wakeup the cpus which have an expired event. ··· 840 761 raw_spin_lock_irqsave(&tick_broadcast_lock, flags); 841 762 842 763 /* 843 - * Clear the broadcast mask flag for the dead cpu, but do not 844 - * stop the broadcast device! 764 + * Clear the broadcast masks for the dead cpu, but do not stop 765 + * the broadcast device! 845 766 */ 846 767 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask); 768 + cpumask_clear_cpu(cpu, tick_broadcast_pending_mask); 769 + cpumask_clear_cpu(cpu, tick_broadcast_force_mask); 847 770 848 771 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); 849 772 } ··· 873 792 void __init tick_broadcast_init(void) 874 793 { 875 794 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT); 795 + zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT); 876 796 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT); 877 797 #ifdef CONFIG_TICK_ONESHOT 878 798 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
+80 -117
kernel/time/tick-common.c
··· 18 18 #include <linux/percpu.h> 19 19 #include <linux/profile.h> 20 20 #include <linux/sched.h> 21 + #include <linux/module.h> 21 22 22 23 #include <asm/irq_regs.h> 23 24 ··· 34 33 ktime_t tick_next_period; 35 34 ktime_t tick_period; 36 35 int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT; 37 - static DEFINE_RAW_SPINLOCK(tick_device_lock); 38 36 39 37 /* 40 38 * Debugging: see timer_list.c ··· 194 194 * When global broadcasting is active, check if the current 195 195 * device is registered as a placeholder for broadcast mode. 196 196 * This allows us to handle this x86 misfeature in a generic 197 - * way. 197 + * way. This function also returns !=0 when we keep the 198 + * current active broadcast state for this CPU. 198 199 */ 199 200 if (tick_device_uses_broadcast(newdev, cpu)) 200 201 return; ··· 206 205 tick_setup_oneshot(newdev, handler, next_event); 207 206 } 208 207 208 + void tick_install_replacement(struct clock_event_device *newdev) 209 + { 210 + struct tick_device *td = &__get_cpu_var(tick_cpu_device); 211 + int cpu = smp_processor_id(); 212 + 213 + clockevents_exchange_device(td->evtdev, newdev); 214 + tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 215 + if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 216 + tick_oneshot_notify(); 217 + } 218 + 219 + static bool tick_check_percpu(struct clock_event_device *curdev, 220 + struct clock_event_device *newdev, int cpu) 221 + { 222 + if (!cpumask_test_cpu(cpu, newdev->cpumask)) 223 + return false; 224 + if (cpumask_equal(newdev->cpumask, cpumask_of(cpu))) 225 + return true; 226 + /* Check if irq affinity can be set */ 227 + if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq)) 228 + return false; 229 + /* Prefer an existing cpu local device */ 230 + if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 231 + return false; 232 + return true; 233 + } 234 + 235 + static bool tick_check_preferred(struct clock_event_device *curdev, 236 + struct clock_event_device *newdev) 237 + { 238 + /* Prefer oneshot capable device */ 239 + if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) { 240 + if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT)) 241 + return false; 242 + if (tick_oneshot_mode_active()) 243 + return false; 244 + } 245 + 246 + /* 247 + * Use the higher rated one, but prefer a CPU local device with a lower 248 + * rating than a non-CPU local device 249 + */ 250 + return !curdev || 251 + newdev->rating > curdev->rating || 252 + !cpumask_equal(curdev->cpumask, newdev->cpumask); 253 + } 254 + 209 255 /* 210 - * Check, if the new registered device should be used. 256 + * Check whether the new device is a better fit than curdev. curdev 257 + * can be NULL ! 211 258 */ 212 - static int tick_check_new_device(struct clock_event_device *newdev) 259 + bool tick_check_replacement(struct clock_event_device *curdev, 260 + struct clock_event_device *newdev) 261 + { 262 + if (tick_check_percpu(curdev, newdev, smp_processor_id())) 263 + return false; 264 + 265 + return tick_check_preferred(curdev, newdev); 266 + } 267 + 268 + /* 269 + * Check, if the new registered device should be used. Called with 270 + * clockevents_lock held and interrupts disabled. 271 + */ 272 + void tick_check_new_device(struct clock_event_device *newdev) 213 273 { 214 274 struct clock_event_device *curdev; 215 275 struct tick_device *td; 216 - int cpu, ret = NOTIFY_OK; 217 - unsigned long flags; 218 - 219 - raw_spin_lock_irqsave(&tick_device_lock, flags); 276 + int cpu; 220 277 221 278 cpu = smp_processor_id(); 222 279 if (!cpumask_test_cpu(cpu, newdev->cpumask)) ··· 284 225 curdev = td->evtdev; 285 226 286 227 /* cpu local device ? */ 287 - if (!cpumask_equal(newdev->cpumask, cpumask_of(cpu))) { 228 + if (!tick_check_percpu(curdev, newdev, cpu)) 229 + goto out_bc; 288 230 289 - /* 290 - * If the cpu affinity of the device interrupt can not 291 - * be set, ignore it. 292 - */ 293 - if (!irq_can_set_affinity(newdev->irq)) 294 - goto out_bc; 231 + /* Preference decision */ 232 + if (!tick_check_preferred(curdev, newdev)) 233 + goto out_bc; 295 234 296 - /* 297 - * If we have a cpu local device already, do not replace it 298 - * by a non cpu local device 299 - */ 300 - if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 301 - goto out_bc; 302 - } 303 - 304 - /* 305 - * If we have an active device, then check the rating and the oneshot 306 - * feature. 307 - */ 308 - if (curdev) { 309 - /* 310 - * Prefer one shot capable devices ! 311 - */ 312 - if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) && 313 - !(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) 314 - goto out_bc; 315 - /* 316 - * Check the rating 317 - */ 318 - if (curdev->rating >= newdev->rating) 319 - goto out_bc; 320 - } 235 + if (!try_module_get(newdev->owner)) 236 + return; 321 237 322 238 /* 323 239 * Replace the eventually existing device by the new ··· 307 273 tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 308 274 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 309 275 tick_oneshot_notify(); 310 - 311 - raw_spin_unlock_irqrestore(&tick_device_lock, flags); 312 - return NOTIFY_STOP; 276 + return; 313 277 314 278 out_bc: 315 279 /* 316 280 * Can the new device be used as a broadcast device ? 317 281 */ 318 - if (tick_check_broadcast_device(newdev)) 319 - ret = NOTIFY_STOP; 320 - 321 - raw_spin_unlock_irqrestore(&tick_device_lock, flags); 322 - 323 - return ret; 282 + tick_install_broadcast_device(newdev); 324 283 } 325 284 326 285 /* ··· 321 294 * 322 295 * Called with interrupts disabled. 323 296 */ 324 - static void tick_handover_do_timer(int *cpup) 297 + void tick_handover_do_timer(int *cpup) 325 298 { 326 299 if (*cpup == tick_do_timer_cpu) { 327 300 int cpu = cpumask_first(cpu_online_mask); ··· 338 311 * access the hardware device itself. 339 312 * We just set the mode and remove it from the lists. 340 313 */ 341 - static void tick_shutdown(unsigned int *cpup) 314 + void tick_shutdown(unsigned int *cpup) 342 315 { 343 316 struct tick_device *td = &per_cpu(tick_cpu_device, *cpup); 344 317 struct clock_event_device *dev = td->evtdev; 345 - unsigned long flags; 346 318 347 - raw_spin_lock_irqsave(&tick_device_lock, flags); 348 319 td->mode = TICKDEV_MODE_PERIODIC; 349 320 if (dev) { 350 321 /* ··· 354 329 dev->event_handler = clockevents_handle_noop; 355 330 td->evtdev = NULL; 356 331 } 357 - raw_spin_unlock_irqrestore(&tick_device_lock, flags); 358 332 } 359 333 360 - static void tick_suspend(void) 334 + void tick_suspend(void) 361 335 { 362 336 struct tick_device *td = &__get_cpu_var(tick_cpu_device); 363 - unsigned long flags; 364 337 365 - raw_spin_lock_irqsave(&tick_device_lock, flags); 366 338 clockevents_shutdown(td->evtdev); 367 - raw_spin_unlock_irqrestore(&tick_device_lock, flags); 368 339 } 369 340 370 - static void tick_resume(void) 341 + void tick_resume(void) 371 342 { 372 343 struct tick_device *td = &__get_cpu_var(tick_cpu_device); 373 - unsigned long flags; 374 344 int broadcast = tick_resume_broadcast(); 375 345 376 - raw_spin_lock_irqsave(&tick_device_lock, flags); 377 346 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_RESUME); 378 347 379 348 if (!broadcast) { ··· 376 357 else 377 358 tick_resume_oneshot(); 378 359 } 379 - raw_spin_unlock_irqrestore(&tick_device_lock, flags); 380 360 } 381 - 382 - /* 383 - * Notification about clock event devices 384 - */ 385 - static int tick_notify(struct notifier_block *nb, unsigned long reason, 386 - void *dev) 387 - { 388 - switch (reason) { 389 - 390 - case CLOCK_EVT_NOTIFY_ADD: 391 - return tick_check_new_device(dev); 392 - 393 - case CLOCK_EVT_NOTIFY_BROADCAST_ON: 394 - case CLOCK_EVT_NOTIFY_BROADCAST_OFF: 395 - case CLOCK_EVT_NOTIFY_BROADCAST_FORCE: 396 - tick_broadcast_on_off(reason, dev); 397 - break; 398 - 399 - case CLOCK_EVT_NOTIFY_BROADCAST_ENTER: 400 - case CLOCK_EVT_NOTIFY_BROADCAST_EXIT: 401 - tick_broadcast_oneshot_control(reason); 402 - break; 403 - 404 - case CLOCK_EVT_NOTIFY_CPU_DYING: 405 - tick_handover_do_timer(dev); 406 - break; 407 - 408 - case CLOCK_EVT_NOTIFY_CPU_DEAD: 409 - tick_shutdown_broadcast_oneshot(dev); 410 - tick_shutdown_broadcast(dev); 411 - tick_shutdown(dev); 412 - break; 413 - 414 - case CLOCK_EVT_NOTIFY_SUSPEND: 415 - tick_suspend(); 416 - tick_suspend_broadcast(); 417 - break; 418 - 419 - case CLOCK_EVT_NOTIFY_RESUME: 420 - tick_resume(); 421 - break; 422 - 423 - default: 424 - break; 425 - } 426 - 427 - return NOTIFY_OK; 428 - } 429 - 430 - static struct notifier_block tick_notifier = { 431 - .notifier_call = tick_notify, 432 - }; 433 361 434 362 /** 435 363 * tick_init - initialize the tick control 436 - * 437 - * Register the notifier with the clockevents framework 438 364 */ 439 365 void __init tick_init(void) 440 366 { 441 - clockevents_register_notifier(&tick_notifier); 442 367 tick_broadcast_init(); 443 368 }
+14 -3
kernel/time/tick-internal.h
··· 6 6 7 7 extern seqlock_t jiffies_lock; 8 8 9 + #define CS_NAME_LEN 32 10 + 9 11 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD 10 12 11 13 #define TICK_DO_TIMER_NONE -1 ··· 20 18 21 19 extern void tick_setup_periodic(struct clock_event_device *dev, int broadcast); 22 20 extern void tick_handle_periodic(struct clock_event_device *dev); 21 + extern void tick_check_new_device(struct clock_event_device *dev); 22 + extern void tick_handover_do_timer(int *cpup); 23 + extern void tick_shutdown(unsigned int *cpup); 24 + extern void tick_suspend(void); 25 + extern void tick_resume(void); 26 + extern bool tick_check_replacement(struct clock_event_device *curdev, 27 + struct clock_event_device *newdev); 28 + extern void tick_install_replacement(struct clock_event_device *dev); 23 29 24 30 extern void clockevents_shutdown(struct clock_event_device *dev); 31 + 32 + extern size_t sysfs_get_uname(const char *buf, char *dst, size_t cnt); 25 33 26 34 /* 27 35 * NO_HZ / high resolution timer shared code ··· 102 90 */ 103 91 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 104 92 extern int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu); 105 - extern int tick_check_broadcast_device(struct clock_event_device *dev); 93 + extern void tick_install_broadcast_device(struct clock_event_device *dev); 106 94 extern int tick_is_broadcast_device(struct clock_event_device *dev); 107 95 extern void tick_broadcast_on_off(unsigned long reason, int *oncpu); 108 96 extern void tick_shutdown_broadcast(unsigned int *cpup); ··· 114 102 115 103 #else /* !BROADCAST */ 116 104 117 - static inline int tick_check_broadcast_device(struct clock_event_device *dev) 105 + static inline void tick_install_broadcast_device(struct clock_event_device *dev) 118 106 { 119 - return 0; 120 107 } 121 108 122 109 static inline int tick_is_broadcast_device(struct clock_event_device *dev)
+43 -22
kernel/time/timekeeping.c
··· 25 25 26 26 #include "tick-internal.h" 27 27 #include "ntp_internal.h" 28 + #include "timekeeping_internal.h" 29 + 30 + #define TK_CLEAR_NTP (1 << 0) 31 + #define TK_MIRROR (1 << 1) 32 + #define TK_CLOCK_WAS_SET (1 << 2) 28 33 29 34 static struct timekeeper timekeeper; 30 35 static DEFINE_RAW_SPINLOCK(timekeeper_lock); ··· 205 200 206 201 static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); 207 202 208 - static void update_pvclock_gtod(struct timekeeper *tk) 203 + static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) 209 204 { 210 - raw_notifier_call_chain(&pvclock_gtod_chain, 0, tk); 205 + raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); 211 206 } 212 207 213 208 /** ··· 221 216 222 217 raw_spin_lock_irqsave(&timekeeper_lock, flags); 223 218 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); 224 - update_pvclock_gtod(tk); 219 + update_pvclock_gtod(tk, true); 225 220 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 226 221 227 222 return ret; ··· 246 241 EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); 247 242 248 243 /* must hold timekeeper_lock */ 249 - static void timekeeping_update(struct timekeeper *tk, bool clearntp, bool mirror) 244 + static void timekeeping_update(struct timekeeper *tk, unsigned int action) 250 245 { 251 - if (clearntp) { 246 + if (action & TK_CLEAR_NTP) { 252 247 tk->ntp_error = 0; 253 248 ntp_clear(); 254 249 } 255 250 update_vsyscall(tk); 256 - update_pvclock_gtod(tk); 251 + update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); 257 252 258 - if (mirror) 253 + if (action & TK_MIRROR) 259 254 memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper)); 260 255 } 261 256 ··· 513 508 514 509 tk_set_xtime(tk, tv); 515 510 516 - timekeeping_update(tk, true, true); 511 + timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 517 512 518 513 write_seqcount_end(&timekeeper_seq); 519 514 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); ··· 557 552 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts)); 558 553 559 554 error: /* even if we error out, we forwarded the time, so call update */ 560 - timekeeping_update(tk, true, true); 555 + timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 561 556 562 557 write_seqcount_end(&timekeeper_seq); 563 558 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); ··· 632 627 write_seqcount_begin(&timekeeper_seq); 633 628 634 629 timekeeping_forward_now(tk); 635 - if (!new->enable || new->enable(new) == 0) { 636 - old = tk->clock; 637 - tk_setup_internals(tk, new); 638 - if (old->disable) 639 - old->disable(old); 630 + /* 631 + * If the cs is in module, get a module reference. Succeeds 632 + * for built-in code (owner == NULL) as well. 633 + */ 634 + if (try_module_get(new->owner)) { 635 + if (!new->enable || new->enable(new) == 0) { 636 + old = tk->clock; 637 + tk_setup_internals(tk, new); 638 + if (old->disable) 639 + old->disable(old); 640 + module_put(old->owner); 641 + } else { 642 + module_put(new->owner); 643 + } 640 644 } 641 - timekeeping_update(tk, true, true); 645 + timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 642 646 643 647 write_seqcount_end(&timekeeper_seq); 644 648 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); ··· 662 648 * This function is called from clocksource.c after a new, better clock 663 649 * source has been registered. The caller holds the clocksource_mutex. 664 650 */ 665 - void timekeeping_notify(struct clocksource *clock) 651 + int timekeeping_notify(struct clocksource *clock) 666 652 { 667 653 struct timekeeper *tk = &timekeeper; 668 654 669 655 if (tk->clock == clock) 670 - return; 656 + return 0; 671 657 stop_machine(change_clocksource, clock, NULL); 672 658 tick_clock_notify(); 659 + return tk->clock == clock ? 0 : -1; 673 660 } 674 661 675 662 /** ··· 856 841 tk_xtime_add(tk, delta); 857 842 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta)); 858 843 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta)); 844 + tk_debug_account_sleep_time(delta); 859 845 } 860 846 861 847 /** ··· 888 872 889 873 __timekeeping_inject_sleeptime(tk, delta); 890 874 891 - timekeeping_update(tk, true, true); 875 + timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 892 876 893 877 write_seqcount_end(&timekeeper_seq); 894 878 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); ··· 970 954 tk->cycle_last = clock->cycle_last = cycle_now; 971 955 tk->ntp_error = 0; 972 956 timekeeping_suspended = 0; 973 - timekeeping_update(tk, false, true); 957 + timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); 974 958 write_seqcount_end(&timekeeper_seq); 975 959 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 976 960 ··· 1252 1236 * It also calls into the NTP code to handle leapsecond processing. 1253 1237 * 1254 1238 */ 1255 - static inline void accumulate_nsecs_to_secs(struct timekeeper *tk) 1239 + static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) 1256 1240 { 1257 1241 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift; 1242 + unsigned int action = 0; 1258 1243 1259 1244 while (tk->xtime_nsec >= nsecps) { 1260 1245 int leap; ··· 1278 1261 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap); 1279 1262 1280 1263 clock_was_set_delayed(); 1264 + action = TK_CLOCK_WAS_SET; 1281 1265 } 1282 1266 } 1267 + return action; 1283 1268 } 1284 1269 1285 1270 /** ··· 1366 1347 struct timekeeper *tk = &shadow_timekeeper; 1367 1348 cycle_t offset; 1368 1349 int shift = 0, maxshift; 1350 + unsigned int action; 1369 1351 unsigned long flags; 1370 1352 1371 1353 raw_spin_lock_irqsave(&timekeeper_lock, flags); ··· 1419 1399 * Finally, make sure that after the rounding 1420 1400 * xtime_nsec isn't larger than NSEC_PER_SEC 1421 1401 */ 1422 - accumulate_nsecs_to_secs(tk); 1402 + action = accumulate_nsecs_to_secs(tk); 1423 1403 1424 1404 write_seqcount_begin(&timekeeper_seq); 1425 1405 /* Update clock->cycle_last with the new value */ ··· 1435 1415 * updating. 1436 1416 */ 1437 1417 memcpy(real_tk, tk, sizeof(*tk)); 1438 - timekeeping_update(real_tk, false, false); 1418 + timekeeping_update(real_tk, action); 1439 1419 write_seqcount_end(&timekeeper_seq); 1440 1420 out: 1441 1421 raw_spin_unlock_irqrestore(&timekeeper_lock, flags); ··· 1697 1677 1698 1678 if (tai != orig_tai) { 1699 1679 __timekeeping_set_tai_offset(tk, tai); 1680 + update_pvclock_gtod(tk, true); 1700 1681 clock_was_set_delayed(); 1701 1682 } 1702 1683 write_seqcount_end(&timekeeper_seq);
+72
kernel/time/timekeeping_debug.c
··· 1 + /* 2 + * debugfs file to track time spent in suspend 3 + * 4 + * Copyright (c) 2011, Google, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License as published by 8 + * the Free Software Foundation; either version 2 of the License, or 9 + * (at your option) any later version. 10 + * 11 + * This program is distributed in the hope that it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + */ 16 + 17 + #include <linux/debugfs.h> 18 + #include <linux/err.h> 19 + #include <linux/init.h> 20 + #include <linux/kernel.h> 21 + #include <linux/seq_file.h> 22 + #include <linux/time.h> 23 + 24 + static unsigned int sleep_time_bin[32] = {0}; 25 + 26 + static int tk_debug_show_sleep_time(struct seq_file *s, void *data) 27 + { 28 + unsigned int bin; 29 + seq_puts(s, " time (secs) count\n"); 30 + seq_puts(s, "------------------------------\n"); 31 + for (bin = 0; bin < 32; bin++) { 32 + if (sleep_time_bin[bin] == 0) 33 + continue; 34 + seq_printf(s, "%10u - %-10u %4u\n", 35 + bin ? 1 << (bin - 1) : 0, 1 << bin, 36 + sleep_time_bin[bin]); 37 + } 38 + return 0; 39 + } 40 + 41 + static int tk_debug_sleep_time_open(struct inode *inode, struct file *file) 42 + { 43 + return single_open(file, tk_debug_show_sleep_time, NULL); 44 + } 45 + 46 + static const struct file_operations tk_debug_sleep_time_fops = { 47 + .open = tk_debug_sleep_time_open, 48 + .read = seq_read, 49 + .llseek = seq_lseek, 50 + .release = single_release, 51 + }; 52 + 53 + static int __init tk_debug_sleep_time_init(void) 54 + { 55 + struct dentry *d; 56 + 57 + d = debugfs_create_file("sleep_time", 0444, NULL, NULL, 58 + &tk_debug_sleep_time_fops); 59 + if (!d) { 60 + pr_err("Failed to create sleep_time debug file\n"); 61 + return -ENOMEM; 62 + } 63 + 64 + return 0; 65 + } 66 + late_initcall(tk_debug_sleep_time_init); 67 + 68 + void tk_debug_account_sleep_time(struct timespec *t) 69 + { 70 + sleep_time_bin[fls(t->tv_sec)]++; 71 + } 72 +
+14
kernel/time/timekeeping_internal.h
··· 1 + #ifndef _TIMEKEEPING_INTERNAL_H 2 + #define _TIMEKEEPING_INTERNAL_H 3 + /* 4 + * timekeeping debug functions 5 + */ 6 + #include <linux/time.h> 7 + 8 + #ifdef CONFIG_DEBUG_FS 9 + extern void tk_debug_account_sleep_time(struct timespec *t); 10 + #else 11 + #define tk_debug_account_sleep_time(x) 12 + #endif 13 + 14 + #endif /* _TIMEKEEPING_INTERNAL_H */
+5 -3
kernel/timer.c
··· 149 149 /* now that we have rounded, subtract the extra skew again */ 150 150 j -= cpu * 3; 151 151 152 - if (j <= jiffies) /* rounding ate our timeout entirely; */ 153 - return original; 154 - return j; 152 + /* 153 + * Make sure j is still in the future. Otherwise return the 154 + * unmodified value. 155 + */ 156 + return time_is_after_jiffies(j) ? j : original; 155 157 } 156 158 157 159 /**
+1
tools/testing/selftests/Makefile
··· 6 6 TARGETS += mqueue 7 7 TARGETS += net 8 8 TARGETS += ptrace 9 + TARGETS += timers 9 10 TARGETS += vm 10 11 11 12 all:
+8
tools/testing/selftests/timers/Makefile
··· 1 + all: 2 + gcc posix_timers.c -o posix_timers -lrt 3 + 4 + run_tests: all 5 + ./posix_timers 6 + 7 + clean: 8 + rm -f ./posix_timers
+221
tools/testing/selftests/timers/posix_timers.c
··· 1 + /* 2 + * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com> 3 + * 4 + * Licensed under the terms of the GNU GPL License version 2 5 + * 6 + * Selftests for a few posix timers interface. 7 + * 8 + * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com> 9 + */ 10 + 11 + #include <sys/time.h> 12 + #include <stdio.h> 13 + #include <signal.h> 14 + #include <unistd.h> 15 + #include <time.h> 16 + #include <pthread.h> 17 + 18 + #define DELAY 2 19 + #define USECS_PER_SEC 1000000 20 + 21 + static volatile int done; 22 + 23 + /* Busy loop in userspace to elapse ITIMER_VIRTUAL */ 24 + static void user_loop(void) 25 + { 26 + while (!done); 27 + } 28 + 29 + /* 30 + * Try to spend as much time as possible in kernelspace 31 + * to elapse ITIMER_PROF. 32 + */ 33 + static void kernel_loop(void) 34 + { 35 + void *addr = sbrk(0); 36 + 37 + while (!done) { 38 + brk(addr + 4096); 39 + brk(addr); 40 + } 41 + } 42 + 43 + /* 44 + * Sleep until ITIMER_REAL expiration. 45 + */ 46 + static void idle_loop(void) 47 + { 48 + pause(); 49 + } 50 + 51 + static void sig_handler(int nr) 52 + { 53 + done = 1; 54 + } 55 + 56 + /* 57 + * Check the expected timer expiration matches the GTOD elapsed delta since 58 + * we armed the timer. Keep a 0.5 sec error margin due to various jitter. 59 + */ 60 + static int check_diff(struct timeval start, struct timeval end) 61 + { 62 + long long diff; 63 + 64 + diff = end.tv_usec - start.tv_usec; 65 + diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC; 66 + 67 + if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) { 68 + printf("Diff too high: %lld..", diff); 69 + return -1; 70 + } 71 + 72 + return 0; 73 + } 74 + 75 + static int check_itimer(int which) 76 + { 77 + int err; 78 + struct timeval start, end; 79 + struct itimerval val = { 80 + .it_value.tv_sec = DELAY, 81 + }; 82 + 83 + printf("Check itimer "); 84 + 85 + if (which == ITIMER_VIRTUAL) 86 + printf("virtual... "); 87 + else if (which == ITIMER_PROF) 88 + printf("prof... "); 89 + else if (which == ITIMER_REAL) 90 + printf("real... "); 91 + 92 + fflush(stdout); 93 + 94 + done = 0; 95 + 96 + if (which == ITIMER_VIRTUAL) 97 + signal(SIGVTALRM, sig_handler); 98 + else if (which == ITIMER_PROF) 99 + signal(SIGPROF, sig_handler); 100 + else if (which == ITIMER_REAL) 101 + signal(SIGALRM, sig_handler); 102 + 103 + err = gettimeofday(&start, NULL); 104 + if (err < 0) { 105 + perror("Can't call gettimeofday()\n"); 106 + return -1; 107 + } 108 + 109 + err = setitimer(which, &val, NULL); 110 + if (err < 0) { 111 + perror("Can't set timer\n"); 112 + return -1; 113 + } 114 + 115 + if (which == ITIMER_VIRTUAL) 116 + user_loop(); 117 + else if (which == ITIMER_PROF) 118 + kernel_loop(); 119 + else if (which == ITIMER_REAL) 120 + idle_loop(); 121 + 122 + gettimeofday(&end, NULL); 123 + if (err < 0) { 124 + perror("Can't call gettimeofday()\n"); 125 + return -1; 126 + } 127 + 128 + if (!check_diff(start, end)) 129 + printf("[OK]\n"); 130 + else 131 + printf("[FAIL]\n"); 132 + 133 + return 0; 134 + } 135 + 136 + static int check_timer_create(int which) 137 + { 138 + int err; 139 + timer_t id; 140 + struct timeval start, end; 141 + struct itimerspec val = { 142 + .it_value.tv_sec = DELAY, 143 + }; 144 + 145 + printf("Check timer_create() "); 146 + if (which == CLOCK_THREAD_CPUTIME_ID) { 147 + printf("per thread... "); 148 + } else if (which == CLOCK_PROCESS_CPUTIME_ID) { 149 + printf("per process... "); 150 + } 151 + fflush(stdout); 152 + 153 + done = 0; 154 + timer_create(which, NULL, &id); 155 + if (err < 0) { 156 + perror("Can't create timer\n"); 157 + return -1; 158 + } 159 + signal(SIGALRM, sig_handler); 160 + 161 + err = gettimeofday(&start, NULL); 162 + if (err < 0) { 163 + perror("Can't call gettimeofday()\n"); 164 + return -1; 165 + } 166 + 167 + err = timer_settime(id, 0, &val, NULL); 168 + if (err < 0) { 169 + perror("Can't set timer\n"); 170 + return -1; 171 + } 172 + 173 + user_loop(); 174 + 175 + gettimeofday(&end, NULL); 176 + if (err < 0) { 177 + perror("Can't call gettimeofday()\n"); 178 + return -1; 179 + } 180 + 181 + if (!check_diff(start, end)) 182 + printf("[OK]\n"); 183 + else 184 + printf("[FAIL]\n"); 185 + 186 + return 0; 187 + } 188 + 189 + int main(int argc, char **argv) 190 + { 191 + int err; 192 + 193 + printf("Testing posix timers. False negative may happen on CPU execution \n"); 194 + printf("based timers if other threads run on the CPU...\n"); 195 + 196 + if (check_itimer(ITIMER_VIRTUAL) < 0) 197 + return -1; 198 + 199 + if (check_itimer(ITIMER_PROF) < 0) 200 + return -1; 201 + 202 + if (check_itimer(ITIMER_REAL) < 0) 203 + return -1; 204 + 205 + if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0) 206 + return -1; 207 + 208 + /* 209 + * It's unfortunately hard to reliably test a timer expiration 210 + * on parallel multithread cputime. We could arm it to expire 211 + * on DELAY * nr_threads, with nr_threads busy looping, then wait 212 + * the normal DELAY since the time is elapsing nr_threads faster. 213 + * But for that we need to ensure we have real physical free CPUs 214 + * to ensure true parallelism. So test only one thread until we 215 + * find a better solution. 216 + */ 217 + if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0) 218 + return -1; 219 + 220 + return 0; 221 + }