aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-03-20 13:32:09 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-20 13:32:09 -0400
commit161f7a7161191ab9c2e97f787829ef8dd2b95771 (patch)
tree9776d3f963c7f0d247b7fb324eab4811a1302f67
parent2ba68940c893c8f0bfc8573c041254251bb6aeab (diff)
parenta078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33 (diff)
Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timer changes for v3.4 from Ingo Molnar * 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (32 commits) ntp: Fix integer overflow when setting time math: Introduce div64_long cs5535-clockevt: Allow the MFGPT IRQ to be shared cs5535-clockevt: Don't ignore MFGPT on SMP-capable kernels x86/time: Eliminate unused irq0_irqs counter clocksource: scx200_hrt: Fix the build x86/tsc: Reduce the TSC sync check time for core-siblings timer: Fix bad idle check on irq entry nohz: Remove ts->Einidle checks before restarting the tick nohz: Remove update_ts_time_stat from tick_nohz_start_idle clockevents: Leave the broadcast device in shutdown mode when not needed clocksource: Load the ACPI PM clocksource asynchronously clocksource: scx200_hrt: Convert scx200 to use clocksource_register_hz clocksource: Get rid of clocksource_calc_mult_shift() clocksource: dbx500: convert to clocksource_register_hz() clocksource: scx200_hrt: use pr_<level> instead of printk time: Move common updates to a function time: Reorder so the hot data is together time: Remove most of xtime_lock usage in timekeeping.c ntp: Add ntp_lock to replace xtime_locking ...
-rw-r--r--arch/x86/include/asm/hardirq.h1
-rw-r--r--arch/x86/kernel/time.c3
-rw-r--r--arch/x86/kernel/tsc_sync.c29
-rw-r--r--drivers/clocksource/acpi_pm.c24
-rw-r--r--drivers/clocksource/clksrc-dbx500-prcmu.c5
-rw-r--r--drivers/clocksource/cs5535-clockevt.c3
-rw-r--r--drivers/clocksource/cyclone.c2
-rw-r--r--drivers/clocksource/scx200_hrt.c24
-rw-r--r--drivers/rtc/interface.c30
-rw-r--r--include/linux/clocksource.h7
-rw-r--r--include/linux/math64.h4
-rw-r--r--include/linux/timex.h17
-rw-r--r--kernel/softirq.c2
-rw-r--r--kernel/time/ntp.c85
-rw-r--r--kernel/time/tick-broadcast.c4
-rw-r--r--kernel/time/tick-sched.c17
-rw-r--r--kernel/time/timekeeping.c338
17 files changed, 343 insertions, 252 deletions
diff --git a/arch/x86/include/asm/hardirq.h b/arch/x86/include/asm/hardirq.h
index da0b3ca815b7..382f75d735f3 100644
--- a/arch/x86/include/asm/hardirq.h
+++ b/arch/x86/include/asm/hardirq.h
@@ -7,7 +7,6 @@
7typedef struct { 7typedef struct {
8 unsigned int __softirq_pending; 8 unsigned int __softirq_pending;
9 unsigned int __nmi_count; /* arch dependent */ 9 unsigned int __nmi_count; /* arch dependent */
10 unsigned int irq0_irqs;
11#ifdef CONFIG_X86_LOCAL_APIC 10#ifdef CONFIG_X86_LOCAL_APIC
12 unsigned int apic_timer_irqs; /* arch dependent */ 11 unsigned int apic_timer_irqs; /* arch dependent */
13 unsigned int irq_spurious_count; 12 unsigned int irq_spurious_count;
diff --git a/arch/x86/kernel/time.c b/arch/x86/kernel/time.c
index dd5fbf4101fc..c6eba2b42673 100644
--- a/arch/x86/kernel/time.c
+++ b/arch/x86/kernel/time.c
@@ -57,9 +57,6 @@ EXPORT_SYMBOL(profile_pc);
57 */ 57 */
58static irqreturn_t timer_interrupt(int irq, void *dev_id) 58static irqreturn_t timer_interrupt(int irq, void *dev_id)
59{ 59{
60 /* Keep nmi watchdog up to date */
61 inc_irq_stat(irq0_irqs);
62
63 global_clock_event->event_handler(global_clock_event); 60 global_clock_event->event_handler(global_clock_event);
64 61
65 /* MCA bus quirk: Acknowledge irq0 by setting bit 7 in port 0x61 */ 62 /* MCA bus quirk: Acknowledge irq0 by setting bit 7 in port 0x61 */
diff --git a/arch/x86/kernel/tsc_sync.c b/arch/x86/kernel/tsc_sync.c
index 9eba29b46cb7..fc25e60a5884 100644
--- a/arch/x86/kernel/tsc_sync.c
+++ b/arch/x86/kernel/tsc_sync.c
@@ -42,7 +42,7 @@ static __cpuinitdata int nr_warps;
42/* 42/*
43 * TSC-warp measurement loop running on both CPUs: 43 * TSC-warp measurement loop running on both CPUs:
44 */ 44 */
45static __cpuinit void check_tsc_warp(void) 45static __cpuinit void check_tsc_warp(unsigned int timeout)
46{ 46{
47 cycles_t start, now, prev, end; 47 cycles_t start, now, prev, end;
48 int i; 48 int i;
@@ -51,9 +51,9 @@ static __cpuinit void check_tsc_warp(void)
51 start = get_cycles(); 51 start = get_cycles();
52 rdtsc_barrier(); 52 rdtsc_barrier();
53 /* 53 /*
54 * The measurement runs for 20 msecs: 54 * The measurement runs for 'timeout' msecs:
55 */ 55 */
56 end = start + tsc_khz * 20ULL; 56 end = start + (cycles_t) tsc_khz * timeout;
57 now = start; 57 now = start;
58 58
59 for (i = 0; ; i++) { 59 for (i = 0; ; i++) {
@@ -99,6 +99,25 @@ static __cpuinit void check_tsc_warp(void)
99} 99}
100 100
101/* 101/*
102 * If the target CPU coming online doesn't have any of its core-siblings
103 * online, a timeout of 20msec will be used for the TSC-warp measurement
104 * loop. Otherwise a smaller timeout of 2msec will be used, as we have some
105 * information about this socket already (and this information grows as we
106 * have more and more logical-siblings in that socket).
107 *
108 * Ideally we should be able to skip the TSC sync check on the other
109 * core-siblings, if the first logical CPU in a socket passed the sync test.
110 * But as the TSC is per-logical CPU and can potentially be modified wrongly
111 * by the bios, TSC sync test for smaller duration should be able
112 * to catch such errors. Also this will catch the condition where all the
113 * cores in the socket doesn't get reset at the same time.
114 */
115static inline unsigned int loop_timeout(int cpu)
116{
117 return (cpumask_weight(cpu_core_mask(cpu)) > 1) ? 2 : 20;
118}
119
120/*
102 * Source CPU calls into this - it waits for the freshly booted 121 * Source CPU calls into this - it waits for the freshly booted
103 * target CPU to arrive and then starts the measurement: 122 * target CPU to arrive and then starts the measurement:
104 */ 123 */
@@ -135,7 +154,7 @@ void __cpuinit check_tsc_sync_source(int cpu)
135 */ 154 */
136 atomic_inc(&start_count); 155 atomic_inc(&start_count);
137 156
138 check_tsc_warp(); 157 check_tsc_warp(loop_timeout(cpu));
139 158
140 while (atomic_read(&stop_count) != cpus-1) 159 while (atomic_read(&stop_count) != cpus-1)
141 cpu_relax(); 160 cpu_relax();
@@ -183,7 +202,7 @@ void __cpuinit check_tsc_sync_target(void)
183 while (atomic_read(&start_count) != cpus) 202 while (atomic_read(&start_count) != cpus)
184 cpu_relax(); 203 cpu_relax();
185 204
186 check_tsc_warp(); 205 check_tsc_warp(loop_timeout(smp_processor_id()));
187 206
188 /* 207 /*
189 * Ok, we are done: 208 * Ok, we are done:
diff --git a/drivers/clocksource/acpi_pm.c b/drivers/clocksource/acpi_pm.c
index 6b5cf02c35c8..82e882028fcf 100644
--- a/drivers/clocksource/acpi_pm.c
+++ b/drivers/clocksource/acpi_pm.c
@@ -23,6 +23,7 @@
23#include <linux/init.h> 23#include <linux/init.h>
24#include <linux/pci.h> 24#include <linux/pci.h>
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/async.h>
26#include <asm/io.h> 27#include <asm/io.h>
27 28
28/* 29/*
@@ -179,17 +180,15 @@ static int verify_pmtmr_rate(void)
179/* Number of reads we try to get two different values */ 180/* Number of reads we try to get two different values */
180#define ACPI_PM_READ_CHECKS 10000 181#define ACPI_PM_READ_CHECKS 10000
181 182
182static int __init init_acpi_pm_clocksource(void) 183static void __init acpi_pm_clocksource_async(void *unused, async_cookie_t cookie)
183{ 184{
184 cycle_t value1, value2; 185 cycle_t value1, value2;
185 unsigned int i, j = 0; 186 unsigned int i, j = 0;
186 187
187 if (!pmtmr_ioport)
188 return -ENODEV;
189 188
190 /* "verify" this timing source: */ 189 /* "verify" this timing source: */
191 for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) { 190 for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) {
192 udelay(100 * j); 191 usleep_range(100 * j, 100 * j + 100);
193 value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 192 value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
194 for (i = 0; i < ACPI_PM_READ_CHECKS; i++) { 193 for (i = 0; i < ACPI_PM_READ_CHECKS; i++) {
195 value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 194 value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
@@ -203,25 +202,34 @@ static int __init init_acpi_pm_clocksource(void)
203 " 0x%#llx, 0x%#llx - aborting.\n", 202 " 0x%#llx, 0x%#llx - aborting.\n",
204 value1, value2); 203 value1, value2);
205 pmtmr_ioport = 0; 204 pmtmr_ioport = 0;
206 return -EINVAL; 205 return;
207 } 206 }
208 if (i == ACPI_PM_READ_CHECKS) { 207 if (i == ACPI_PM_READ_CHECKS) {
209 printk(KERN_INFO "PM-Timer failed consistency check " 208 printk(KERN_INFO "PM-Timer failed consistency check "
210 " (0x%#llx) - aborting.\n", value1); 209 " (0x%#llx) - aborting.\n", value1);
211 pmtmr_ioport = 0; 210 pmtmr_ioport = 0;
212 return -ENODEV; 211 return;
213 } 212 }
214 } 213 }
215 214
216 if (verify_pmtmr_rate() != 0){ 215 if (verify_pmtmr_rate() != 0){
217 pmtmr_ioport = 0; 216 pmtmr_ioport = 0;
218 return -ENODEV; 217 return;
219 } 218 }
220 219
221 return clocksource_register_hz(&clocksource_acpi_pm, 220 clocksource_register_hz(&clocksource_acpi_pm,
222 PMTMR_TICKS_PER_SEC); 221 PMTMR_TICKS_PER_SEC);
223} 222}
224 223
224static int __init init_acpi_pm_clocksource(void)
225{
226 if (!pmtmr_ioport)
227 return -ENODEV;
228
229 async_schedule(acpi_pm_clocksource_async, NULL);
230 return 0;
231}
232
225/* We use fs_initcall because we want the PCI fixups to have run 233/* We use fs_initcall because we want the PCI fixups to have run
226 * but we still need to load before device_initcall 234 * but we still need to load before device_initcall
227 */ 235 */
diff --git a/drivers/clocksource/clksrc-dbx500-prcmu.c b/drivers/clocksource/clksrc-dbx500-prcmu.c
index fb6b6d28b60e..c26c369eb9e6 100644
--- a/drivers/clocksource/clksrc-dbx500-prcmu.c
+++ b/drivers/clocksource/clksrc-dbx500-prcmu.c
@@ -52,7 +52,6 @@ static struct clocksource clocksource_dbx500_prcmu = {
52 .name = "dbx500-prcmu-timer", 52 .name = "dbx500-prcmu-timer",
53 .rating = 300, 53 .rating = 300,
54 .read = clksrc_dbx500_prcmu_read, 54 .read = clksrc_dbx500_prcmu_read,
55 .shift = 10,
56 .mask = CLOCKSOURCE_MASK(32), 55 .mask = CLOCKSOURCE_MASK(32),
57 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 56 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
58}; 57};
@@ -90,7 +89,5 @@ void __init clksrc_dbx500_prcmu_init(void __iomem *base)
90 setup_sched_clock(dbx500_prcmu_sched_clock_read, 89 setup_sched_clock(dbx500_prcmu_sched_clock_read,
91 32, RATE_32K); 90 32, RATE_32K);
92#endif 91#endif
93 clocksource_calc_mult_shift(&clocksource_dbx500_prcmu, 92 clocksource_register_hz(&clocksource_dbx500_prcmu, RATE_32K);
94 RATE_32K, SCHED_CLOCK_MIN_WRAP);
95 clocksource_register(&clocksource_dbx500_prcmu);
96} 93}
diff --git a/drivers/clocksource/cs5535-clockevt.c b/drivers/clocksource/cs5535-clockevt.c
index b7dab32ce63c..540795cd0760 100644
--- a/drivers/clocksource/cs5535-clockevt.c
+++ b/drivers/clocksource/cs5535-clockevt.c
@@ -100,7 +100,6 @@ static struct clock_event_device cs5535_clockevent = {
100 .set_mode = mfgpt_set_mode, 100 .set_mode = mfgpt_set_mode,
101 .set_next_event = mfgpt_next_event, 101 .set_next_event = mfgpt_next_event,
102 .rating = 250, 102 .rating = 250,
103 .cpumask = cpu_all_mask,
104 .shift = 32 103 .shift = 32
105}; 104};
106 105
@@ -133,7 +132,7 @@ static irqreturn_t mfgpt_tick(int irq, void *dev_id)
133 132
134static struct irqaction mfgptirq = { 133static struct irqaction mfgptirq = {
135 .handler = mfgpt_tick, 134 .handler = mfgpt_tick,
136 .flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TIMER, 135 .flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TIMER | IRQF_SHARED,
137 .name = DRV_NAME, 136 .name = DRV_NAME,
138}; 137};
139 138
diff --git a/drivers/clocksource/cyclone.c b/drivers/clocksource/cyclone.c
index 72f811f73e9c..9e0998f22885 100644
--- a/drivers/clocksource/cyclone.c
+++ b/drivers/clocksource/cyclone.c
@@ -55,11 +55,11 @@ static int __init init_cyclone_clocksource(void)
55 } 55 }
56 /* even on 64bit systems, this is only 32bits: */ 56 /* even on 64bit systems, this is only 32bits: */
57 base = readl(reg); 57 base = readl(reg);
58 iounmap(reg);
58 if (!base) { 59 if (!base) {
59 printk(KERN_ERR "Summit chipset: Could not find valid CBAR value.\n"); 60 printk(KERN_ERR "Summit chipset: Could not find valid CBAR value.\n");
60 return -ENODEV; 61 return -ENODEV;
61 } 62 }
62 iounmap(reg);
63 63
64 /* setup PMCC: */ 64 /* setup PMCC: */
65 offset = base + CYCLONE_PMCC_OFFSET; 65 offset = base + CYCLONE_PMCC_OFFSET;
diff --git a/drivers/clocksource/scx200_hrt.c b/drivers/clocksource/scx200_hrt.c
index 27f4d9637b62..64f9e8294434 100644
--- a/drivers/clocksource/scx200_hrt.c
+++ b/drivers/clocksource/scx200_hrt.c
@@ -49,9 +49,6 @@ static cycle_t read_hrt(struct clocksource *cs)
49 return (cycle_t) inl(scx200_cb_base + SCx200_TIMER_OFFSET); 49 return (cycle_t) inl(scx200_cb_base + SCx200_TIMER_OFFSET);
50} 50}
51 51
52#define HRT_SHIFT_1 22
53#define HRT_SHIFT_27 26
54
55static struct clocksource cs_hrt = { 52static struct clocksource cs_hrt = {
56 .name = "scx200_hrt", 53 .name = "scx200_hrt",
57 .rating = 250, 54 .rating = 250,
@@ -63,6 +60,7 @@ static struct clocksource cs_hrt = {
63 60
64static int __init init_hrt_clocksource(void) 61static int __init init_hrt_clocksource(void)
65{ 62{
63 u32 freq;
66 /* Make sure scx200 has initialized the configuration block */ 64 /* Make sure scx200 has initialized the configuration block */
67 if (!scx200_cb_present()) 65 if (!scx200_cb_present())
68 return -ENODEV; 66 return -ENODEV;
@@ -71,7 +69,7 @@ static int __init init_hrt_clocksource(void)
71 if (!request_region(scx200_cb_base + SCx200_TIMER_OFFSET, 69 if (!request_region(scx200_cb_base + SCx200_TIMER_OFFSET,
72 SCx200_TIMER_SIZE, 70 SCx200_TIMER_SIZE,
73 "NatSemi SCx200 High-Resolution Timer")) { 71 "NatSemi SCx200 High-Resolution Timer")) {
74 printk(KERN_WARNING NAME ": unable to lock timer region\n"); 72 pr_warn("unable to lock timer region\n");
75 return -ENODEV; 73 return -ENODEV;
76 } 74 }
77 75
@@ -79,19 +77,13 @@ static int __init init_hrt_clocksource(void)
79 outb(HR_TMEN | (mhz27 ? HR_TMCLKSEL : 0), 77 outb(HR_TMEN | (mhz27 ? HR_TMCLKSEL : 0),
80 scx200_cb_base + SCx200_TMCNFG_OFFSET); 78 scx200_cb_base + SCx200_TMCNFG_OFFSET);
81 79
82 if (mhz27) { 80 freq = (HRT_FREQ + ppm);
83 cs_hrt.shift = HRT_SHIFT_27; 81 if (mhz27)
84 cs_hrt.mult = clocksource_hz2mult((HRT_FREQ + ppm) * 27, 82 freq *= 27;
85 cs_hrt.shift); 83
86 } else { 84 pr_info("enabling scx200 high-res timer (%s MHz +%d ppm)\n", mhz27 ? "27":"1", ppm);
87 cs_hrt.shift = HRT_SHIFT_1;
88 cs_hrt.mult = clocksource_hz2mult(HRT_FREQ + ppm,
89 cs_hrt.shift);
90 }
91 printk(KERN_INFO "enabling scx200 high-res timer (%s MHz +%d ppm)\n",
92 mhz27 ? "27":"1", ppm);
93 85
94 return clocksource_register(&cs_hrt); 86 return clocksource_register_hz(&cs_hrt, freq);
95} 87}
96 88
97module_init(init_hrt_clocksource); 89module_init(init_hrt_clocksource);
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 8a1c031391d6..dc87eda65814 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -73,6 +73,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
73 err = -EINVAL; 73 err = -EINVAL;
74 74
75 mutex_unlock(&rtc->ops_lock); 75 mutex_unlock(&rtc->ops_lock);
76 /* A timer might have just expired */
77 schedule_work(&rtc->irqwork);
76 return err; 78 return err;
77} 79}
78EXPORT_SYMBOL_GPL(rtc_set_time); 80EXPORT_SYMBOL_GPL(rtc_set_time);
@@ -112,6 +114,8 @@ int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
112 err = -EINVAL; 114 err = -EINVAL;
113 115
114 mutex_unlock(&rtc->ops_lock); 116 mutex_unlock(&rtc->ops_lock);
117 /* A timer might have just expired */
118 schedule_work(&rtc->irqwork);
115 119
116 return err; 120 return err;
117} 121}
@@ -380,18 +384,27 @@ EXPORT_SYMBOL_GPL(rtc_set_alarm);
380int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 384int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
381{ 385{
382 int err; 386 int err;
387 struct rtc_time now;
383 388
384 err = rtc_valid_tm(&alarm->time); 389 err = rtc_valid_tm(&alarm->time);
385 if (err != 0) 390 if (err != 0)
386 return err; 391 return err;
387 392
393 err = rtc_read_time(rtc, &now);
394 if (err)
395 return err;
396
388 err = mutex_lock_interruptible(&rtc->ops_lock); 397 err = mutex_lock_interruptible(&rtc->ops_lock);
389 if (err) 398 if (err)
390 return err; 399 return err;
391 400
392 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); 401 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
393 rtc->aie_timer.period = ktime_set(0, 0); 402 rtc->aie_timer.period = ktime_set(0, 0);
394 if (alarm->enabled) { 403
404 /* Alarm has to be enabled & in the futrure for us to enqueue it */
405 if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
406 rtc->aie_timer.node.expires.tv64)) {
407
395 rtc->aie_timer.enabled = 1; 408 rtc->aie_timer.enabled = 1;
396 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); 409 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
397 } 410 }
@@ -763,6 +776,14 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
763 return 0; 776 return 0;
764} 777}
765 778
779static void rtc_alarm_disable(struct rtc_device *rtc)
780{
781 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
782 return;
783
784 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
785}
786
766/** 787/**
767 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue 788 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
768 * @rtc rtc device 789 * @rtc rtc device
@@ -784,8 +805,10 @@ static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
784 struct rtc_wkalrm alarm; 805 struct rtc_wkalrm alarm;
785 int err; 806 int err;
786 next = timerqueue_getnext(&rtc->timerqueue); 807 next = timerqueue_getnext(&rtc->timerqueue);
787 if (!next) 808 if (!next) {
809 rtc_alarm_disable(rtc);
788 return; 810 return;
811 }
789 alarm.time = rtc_ktime_to_tm(next->expires); 812 alarm.time = rtc_ktime_to_tm(next->expires);
790 alarm.enabled = 1; 813 alarm.enabled = 1;
791 err = __rtc_set_alarm(rtc, &alarm); 814 err = __rtc_set_alarm(rtc, &alarm);
@@ -847,7 +870,8 @@ again:
847 err = __rtc_set_alarm(rtc, &alarm); 870 err = __rtc_set_alarm(rtc, &alarm);
848 if (err == -ETIME) 871 if (err == -ETIME)
849 goto again; 872 goto again;
850 } 873 } else
874 rtc_alarm_disable(rtc);
851 875
852 mutex_unlock(&rtc->ops_lock); 876 mutex_unlock(&rtc->ops_lock);
853} 877}
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 081147da0564..fbe89e17124e 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -319,13 +319,6 @@ static inline void __clocksource_updatefreq_khz(struct clocksource *cs, u32 khz)
319 __clocksource_updatefreq_scale(cs, 1000, khz); 319 __clocksource_updatefreq_scale(cs, 1000, khz);
320} 320}
321 321
322static inline void
323clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
324{
325 return clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
326 NSEC_PER_SEC, minsec);
327}
328
329#ifdef CONFIG_GENERIC_TIME_VSYSCALL 322#ifdef CONFIG_GENERIC_TIME_VSYSCALL
330extern void 323extern void
331update_vsyscall(struct timespec *ts, struct timespec *wtm, 324update_vsyscall(struct timespec *ts, struct timespec *wtm,
diff --git a/include/linux/math64.h b/include/linux/math64.h
index 23fcdfcba81b..b8ba85544721 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -6,6 +6,8 @@
6 6
7#if BITS_PER_LONG == 64 7#if BITS_PER_LONG == 64
8 8
9#define div64_long(x,y) div64_s64((x),(y))
10
9/** 11/**
10 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder 12 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
11 * 13 *
@@ -45,6 +47,8 @@ static inline s64 div64_s64(s64 dividend, s64 divisor)
45 47
46#elif BITS_PER_LONG == 32 48#elif BITS_PER_LONG == 32
47 49
50#define div64_long(x,y) div_s64((x),(y))
51
48#ifndef div_u64_rem 52#ifndef div_u64_rem
49static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) 53static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
50{ 54{
diff --git a/include/linux/timex.h b/include/linux/timex.h
index aa60fe7b6ed6..b75e1864ed19 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -234,23 +234,9 @@ struct timex {
234extern unsigned long tick_usec; /* USER_HZ period (usec) */ 234extern unsigned long tick_usec; /* USER_HZ period (usec) */
235extern unsigned long tick_nsec; /* ACTHZ period (nsec) */ 235extern unsigned long tick_nsec; /* ACTHZ period (nsec) */
236 236
237/*
238 * phase-lock loop variables
239 */
240extern int time_status; /* clock synchronization status bits */
241
242extern void ntp_init(void); 237extern void ntp_init(void);
243extern void ntp_clear(void); 238extern void ntp_clear(void);
244 239
245/**
246 * ntp_synced - Returns 1 if the NTP status is not UNSYNC
247 *
248 */
249static inline int ntp_synced(void)
250{
251 return !(time_status & STA_UNSYNC);
252}
253
254/* Required to safely shift negative values */ 240/* Required to safely shift negative values */
255#define shift_right(x, s) ({ \ 241#define shift_right(x, s) ({ \
256 __typeof__(x) __x = (x); \ 242 __typeof__(x) __x = (x); \
@@ -264,10 +250,9 @@ static inline int ntp_synced(void)
264#define NTP_INTERVAL_LENGTH (NSEC_PER_SEC/NTP_INTERVAL_FREQ) 250#define NTP_INTERVAL_LENGTH (NSEC_PER_SEC/NTP_INTERVAL_FREQ)
265 251
266/* Returns how long ticks are at present, in ns / 2^NTP_SCALE_SHIFT. */ 252/* Returns how long ticks are at present, in ns / 2^NTP_SCALE_SHIFT. */
267extern u64 tick_length; 253extern u64 ntp_tick_length(void);
268 254
269extern void second_overflow(void); 255extern void second_overflow(void);
270extern void update_ntp_one_tick(void);
271extern int do_adjtimex(struct timex *); 256extern int do_adjtimex(struct timex *);
272extern void hardpps(const struct timespec *, const struct timespec *); 257extern void hardpps(const struct timespec *, const struct timespec *);
273 258
diff --git a/kernel/softirq.c b/kernel/softirq.c
index 15352e0cbd5d..671f9594e368 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -297,7 +297,7 @@ void irq_enter(void)
297 int cpu = smp_processor_id(); 297 int cpu = smp_processor_id();
298 298
299 rcu_irq_enter(); 299 rcu_irq_enter();
300 if (idle_cpu(cpu) && !in_interrupt()) { 300 if (is_idle_task(current) && !in_interrupt()) {
301 /* 301 /*
302 * Prevent raise_softirq from needlessly waking up ksoftirqd 302 * Prevent raise_softirq from needlessly waking up ksoftirqd
303 * here, as softirq will be serviced on return from interrupt. 303 * here, as softirq will be serviced on return from interrupt.
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index f6117a4c7cb8..6e039b144daf 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -22,13 +22,16 @@
22 * NTP timekeeping variables: 22 * NTP timekeeping variables:
23 */ 23 */
24 24
25DEFINE_SPINLOCK(ntp_lock);
26
27
25/* USER_HZ period (usecs): */ 28/* USER_HZ period (usecs): */
26unsigned long tick_usec = TICK_USEC; 29unsigned long tick_usec = TICK_USEC;
27 30
28/* ACTHZ period (nsecs): */ 31/* ACTHZ period (nsecs): */
29unsigned long tick_nsec; 32unsigned long tick_nsec;
30 33
31u64 tick_length; 34static u64 tick_length;
32static u64 tick_length_base; 35static u64 tick_length_base;
33 36
34static struct hrtimer leap_timer; 37static struct hrtimer leap_timer;
@@ -49,7 +52,7 @@ static struct hrtimer leap_timer;
49static int time_state = TIME_OK; 52static int time_state = TIME_OK;
50 53
51/* clock status bits: */ 54/* clock status bits: */
52int time_status = STA_UNSYNC; 55static int time_status = STA_UNSYNC;
53 56
54/* TAI offset (secs): */ 57/* TAI offset (secs): */
55static long time_tai; 58static long time_tai;
@@ -133,7 +136,7 @@ static inline void pps_reset_freq_interval(void)
133/** 136/**
134 * pps_clear - Clears the PPS state variables 137 * pps_clear - Clears the PPS state variables
135 * 138 *
136 * Must be called while holding a write on the xtime_lock 139 * Must be called while holding a write on the ntp_lock
137 */ 140 */
138static inline void pps_clear(void) 141static inline void pps_clear(void)
139{ 142{
@@ -149,7 +152,7 @@ static inline void pps_clear(void)
149 * the last PPS signal. When it reaches 0, indicate that PPS signal is 152 * the last PPS signal. When it reaches 0, indicate that PPS signal is
150 * missing. 153 * missing.
151 * 154 *
152 * Must be called while holding a write on the xtime_lock 155 * Must be called while holding a write on the ntp_lock
153 */ 156 */
154static inline void pps_dec_valid(void) 157static inline void pps_dec_valid(void)
155{ 158{
@@ -233,6 +236,17 @@ static inline void pps_fill_timex(struct timex *txc)
233 236
234#endif /* CONFIG_NTP_PPS */ 237#endif /* CONFIG_NTP_PPS */
235 238
239
240/**
241 * ntp_synced - Returns 1 if the NTP status is not UNSYNC
242 *
243 */
244static inline int ntp_synced(void)
245{
246 return !(time_status & STA_UNSYNC);
247}
248
249
236/* 250/*
237 * NTP methods: 251 * NTP methods:
238 */ 252 */
@@ -275,7 +289,7 @@ static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
275 289
276 time_status |= STA_MODE; 290 time_status |= STA_MODE;
277 291
278 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs); 292 return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
279} 293}
280 294
281static void ntp_update_offset(long offset) 295static void ntp_update_offset(long offset)
@@ -330,11 +344,13 @@ static void ntp_update_offset(long offset)
330 344
331/** 345/**
332 * ntp_clear - Clears the NTP state variables 346 * ntp_clear - Clears the NTP state variables
333 *
334 * Must be called while holding a write on the xtime_lock
335 */ 347 */
336void ntp_clear(void) 348void ntp_clear(void)
337{ 349{
350 unsigned long flags;
351
352 spin_lock_irqsave(&ntp_lock, flags);
353
338 time_adjust = 0; /* stop active adjtime() */ 354 time_adjust = 0; /* stop active adjtime() */
339 time_status |= STA_UNSYNC; 355 time_status |= STA_UNSYNC;
340 time_maxerror = NTP_PHASE_LIMIT; 356 time_maxerror = NTP_PHASE_LIMIT;
@@ -347,8 +363,23 @@ void ntp_clear(void)
347 363
348 /* Clear PPS state variables */ 364 /* Clear PPS state variables */
349 pps_clear(); 365 pps_clear();
366 spin_unlock_irqrestore(&ntp_lock, flags);
367
350} 368}
351 369
370
371u64 ntp_tick_length(void)
372{
373 unsigned long flags;
374 s64 ret;
375
376 spin_lock_irqsave(&ntp_lock, flags);
377 ret = tick_length;
378 spin_unlock_irqrestore(&ntp_lock, flags);
379 return ret;
380}
381
382
352/* 383/*
353 * Leap second processing. If in leap-insert state at the end of the 384 * Leap second processing. If in leap-insert state at the end of the
354 * day, the system clock is set back one second; if in leap-delete 385 * day, the system clock is set back one second; if in leap-delete
@@ -357,14 +388,15 @@ void ntp_clear(void)
357static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer) 388static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
358{ 389{
359 enum hrtimer_restart res = HRTIMER_NORESTART; 390 enum hrtimer_restart res = HRTIMER_NORESTART;
391 unsigned long flags;
392 int leap = 0;
360 393
361 write_seqlock(&xtime_lock); 394 spin_lock_irqsave(&ntp_lock, flags);
362
363 switch (time_state) { 395 switch (time_state) {
364 case TIME_OK: 396 case TIME_OK:
365 break; 397 break;
366 case TIME_INS: 398 case TIME_INS:
367 timekeeping_leap_insert(-1); 399 leap = -1;
368 time_state = TIME_OOP; 400 time_state = TIME_OOP;
369 printk(KERN_NOTICE 401 printk(KERN_NOTICE
370 "Clock: inserting leap second 23:59:60 UTC\n"); 402 "Clock: inserting leap second 23:59:60 UTC\n");
@@ -372,7 +404,7 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
372 res = HRTIMER_RESTART; 404 res = HRTIMER_RESTART;
373 break; 405 break;
374 case TIME_DEL: 406 case TIME_DEL:
375 timekeeping_leap_insert(1); 407 leap = 1;
376 time_tai--; 408 time_tai--;
377 time_state = TIME_WAIT; 409 time_state = TIME_WAIT;
378 printk(KERN_NOTICE 410 printk(KERN_NOTICE
@@ -387,8 +419,14 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
387 time_state = TIME_OK; 419 time_state = TIME_OK;
388 break; 420 break;
389 } 421 }
422 spin_unlock_irqrestore(&ntp_lock, flags);
390 423
391 write_sequnlock(&xtime_lock); 424 /*
425 * We have to call this outside of the ntp_lock to keep
426 * the proper locking hierarchy
427 */
428 if (leap)
429 timekeeping_leap_insert(leap);
392 430
393 return res; 431 return res;
394} 432}
@@ -404,6 +442,9 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
404void second_overflow(void) 442void second_overflow(void)
405{ 443{
406 s64 delta; 444 s64 delta;
445 unsigned long flags;
446
447 spin_lock_irqsave(&ntp_lock, flags);
407 448
408 /* Bump the maxerror field */ 449 /* Bump the maxerror field */
409 time_maxerror += MAXFREQ / NSEC_PER_USEC; 450 time_maxerror += MAXFREQ / NSEC_PER_USEC;
@@ -423,23 +464,25 @@ void second_overflow(void)
423 pps_dec_valid(); 464 pps_dec_valid();
424 465
425 if (!time_adjust) 466 if (!time_adjust)
426 return; 467 goto out;
427 468
428 if (time_adjust > MAX_TICKADJ) { 469 if (time_adjust > MAX_TICKADJ) {
429 time_adjust -= MAX_TICKADJ; 470 time_adjust -= MAX_TICKADJ;
430 tick_length += MAX_TICKADJ_SCALED; 471 tick_length += MAX_TICKADJ_SCALED;
431 return; 472 goto out;
432 } 473 }
433 474
434 if (time_adjust < -MAX_TICKADJ) { 475 if (time_adjust < -MAX_TICKADJ) {
435 time_adjust += MAX_TICKADJ; 476 time_adjust += MAX_TICKADJ;
436 tick_length -= MAX_TICKADJ_SCALED; 477 tick_length -= MAX_TICKADJ_SCALED;
437 return; 478 goto out;
438 } 479 }
439 480
440 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ) 481 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
441 << NTP_SCALE_SHIFT; 482 << NTP_SCALE_SHIFT;
442 time_adjust = 0; 483 time_adjust = 0;
484out:
485 spin_unlock_irqrestore(&ntp_lock, flags);
443} 486}
444 487
445#ifdef CONFIG_GENERIC_CMOS_UPDATE 488#ifdef CONFIG_GENERIC_CMOS_UPDATE
@@ -663,7 +706,7 @@ int do_adjtimex(struct timex *txc)
663 706
664 getnstimeofday(&ts); 707 getnstimeofday(&ts);
665 708
666 write_seqlock_irq(&xtime_lock); 709 spin_lock_irq(&ntp_lock);
667 710
668 if (txc->modes & ADJ_ADJTIME) { 711 if (txc->modes & ADJ_ADJTIME) {
669 long save_adjust = time_adjust; 712 long save_adjust = time_adjust;
@@ -705,7 +748,7 @@ int do_adjtimex(struct timex *txc)
705 /* fill PPS status fields */ 748 /* fill PPS status fields */
706 pps_fill_timex(txc); 749 pps_fill_timex(txc);
707 750
708 write_sequnlock_irq(&xtime_lock); 751 spin_unlock_irq(&ntp_lock);
709 752
710 txc->time.tv_sec = ts.tv_sec; 753 txc->time.tv_sec = ts.tv_sec;
711 txc->time.tv_usec = ts.tv_nsec; 754 txc->time.tv_usec = ts.tv_nsec;
@@ -903,7 +946,7 @@ void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
903 946
904 pts_norm = pps_normalize_ts(*phase_ts); 947 pts_norm = pps_normalize_ts(*phase_ts);
905 948
906 write_seqlock_irqsave(&xtime_lock, flags); 949 spin_lock_irqsave(&ntp_lock, flags);
907 950
908 /* clear the error bits, they will be set again if needed */ 951 /* clear the error bits, they will be set again if needed */
909 time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR); 952 time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
@@ -916,7 +959,7 @@ void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
916 * just start the frequency interval */ 959 * just start the frequency interval */
917 if (unlikely(pps_fbase.tv_sec == 0)) { 960 if (unlikely(pps_fbase.tv_sec == 0)) {
918 pps_fbase = *raw_ts; 961 pps_fbase = *raw_ts;
919 write_sequnlock_irqrestore(&xtime_lock, flags); 962 spin_unlock_irqrestore(&ntp_lock, flags);
920 return; 963 return;
921 } 964 }
922 965
@@ -931,7 +974,7 @@ void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
931 time_status |= STA_PPSJITTER; 974 time_status |= STA_PPSJITTER;
932 /* restart the frequency calibration interval */ 975 /* restart the frequency calibration interval */
933 pps_fbase = *raw_ts; 976 pps_fbase = *raw_ts;
934 write_sequnlock_irqrestore(&xtime_lock, flags); 977 spin_unlock_irqrestore(&ntp_lock, flags);
935 pr_err("hardpps: PPSJITTER: bad pulse\n"); 978 pr_err("hardpps: PPSJITTER: bad pulse\n");
936 return; 979 return;
937 } 980 }
@@ -948,7 +991,7 @@ void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
948 991
949 hardpps_update_phase(pts_norm.nsec); 992 hardpps_update_phase(pts_norm.nsec);
950 993
951 write_sequnlock_irqrestore(&xtime_lock, flags); 994 spin_unlock_irqrestore(&ntp_lock, flags);
952} 995}
953EXPORT_SYMBOL(hardpps); 996EXPORT_SYMBOL(hardpps);
954 997
diff --git a/kernel/time/tick-broadcast.c b/kernel/time/tick-broadcast.c
index fd4a7b1625a2..e883f57a3cd3 100644
--- a/kernel/time/tick-broadcast.c
+++ b/kernel/time/tick-broadcast.c
@@ -575,11 +575,15 @@ void tick_broadcast_switch_to_oneshot(void)
575 unsigned long flags; 575 unsigned long flags;
576 576
577 raw_spin_lock_irqsave(&tick_broadcast_lock, flags); 577 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
578 if (cpumask_empty(tick_get_broadcast_mask()))
579 goto end;
578 580
579 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT; 581 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
580 bc = tick_broadcast_device.evtdev; 582 bc = tick_broadcast_device.evtdev;
581 if (bc) 583 if (bc)
582 tick_broadcast_setup_oneshot(bc); 584 tick_broadcast_setup_oneshot(bc);
585
586end:
583 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); 587 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
584} 588}
585 589
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 7656642e4b8e..3526038f2836 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -182,11 +182,7 @@ static void tick_nohz_stop_idle(int cpu, ktime_t now)
182 182
183static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts) 183static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
184{ 184{
185 ktime_t now; 185 ktime_t now = ktime_get();
186
187 now = ktime_get();
188
189 update_ts_time_stats(cpu, ts, now, NULL);
190 186
191 ts->idle_entrytime = now; 187 ts->idle_entrytime = now;
192 ts->idle_active = 1; 188 ts->idle_active = 1;
@@ -562,20 +558,21 @@ void tick_nohz_idle_exit(void)
562 558
563 local_irq_disable(); 559 local_irq_disable();
564 560
565 if (ts->idle_active || (ts->inidle && ts->tick_stopped)) 561 WARN_ON_ONCE(!ts->inidle);
562
563 ts->inidle = 0;
564
565 if (ts->idle_active || ts->tick_stopped)
566 now = ktime_get(); 566 now = ktime_get();
567 567
568 if (ts->idle_active) 568 if (ts->idle_active)
569 tick_nohz_stop_idle(cpu, now); 569 tick_nohz_stop_idle(cpu, now);
570 570
571 if (!ts->inidle || !ts->tick_stopped) { 571 if (!ts->tick_stopped) {
572 ts->inidle = 0;
573 local_irq_enable(); 572 local_irq_enable();
574 return; 573 return;
575 } 574 }
576 575
577 ts->inidle = 0;
578
579 /* Update jiffies first */ 576 /* Update jiffies first */
580 select_nohz_load_balancer(0); 577 select_nohz_load_balancer(0);
581 tick_do_update_jiffies64(now); 578 tick_do_update_jiffies64(now);
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 0c6358186401..403c2a092830 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -25,6 +25,8 @@
25struct timekeeper { 25struct timekeeper {
26 /* Current clocksource used for timekeeping. */ 26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock; 27 struct clocksource *clock;
28 /* NTP adjusted clock multiplier */
29 u32 mult;
28 /* The shift value of the current clocksource. */ 30 /* The shift value of the current clocksource. */
29 int shift; 31 int shift;
30 32
@@ -45,12 +47,47 @@ struct timekeeper {
45 /* Shift conversion between clock shifted nano seconds and 47 /* Shift conversion between clock shifted nano seconds and
46 * ntp shifted nano seconds. */ 48 * ntp shifted nano seconds. */
47 int ntp_error_shift; 49 int ntp_error_shift;
48 /* NTP adjusted clock multiplier */ 50
49 u32 mult; 51 /* The current time */
52 struct timespec xtime;
53 /*
54 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
55 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
56 * at zero at system boot time, so wall_to_monotonic will be negative,
57 * however, we will ALWAYS keep the tv_nsec part positive so we can use
58 * the usual normalization.
59 *
60 * wall_to_monotonic is moved after resume from suspend for the
61 * monotonic time not to jump. We need to add total_sleep_time to
62 * wall_to_monotonic to get the real boot based time offset.
63 *
64 * - wall_to_monotonic is no longer the boot time, getboottime must be
65 * used instead.
66 */
67 struct timespec wall_to_monotonic;
68 /* time spent in suspend */
69 struct timespec total_sleep_time;
70 /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
71 struct timespec raw_time;
72
73 /* Seqlock for all timekeeper values */
74 seqlock_t lock;
50}; 75};
51 76
52static struct timekeeper timekeeper; 77static struct timekeeper timekeeper;
53 78
79/*
80 * This read-write spinlock protects us from races in SMP while
81 * playing with xtime.
82 */
83__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
84
85
86/* flag for if timekeeping is suspended */
87int __read_mostly timekeeping_suspended;
88
89
90
54/** 91/**
55 * timekeeper_setup_internals - Set up internals to use clocksource clock. 92 * timekeeper_setup_internals - Set up internals to use clocksource clock.
56 * 93 *
@@ -135,47 +172,28 @@ static inline s64 timekeeping_get_ns_raw(void)
135 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); 172 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
136} 173}
137 174
138/* 175/* must hold write on timekeeper.lock */
139 * This read-write spinlock protects us from races in SMP while 176static void timekeeping_update(bool clearntp)
140 * playing with xtime. 177{
141 */ 178 if (clearntp) {
142__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); 179 timekeeper.ntp_error = 0;
143 180 ntp_clear();
144 181 }
145/* 182 update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
146 * The current time 183 timekeeper.clock, timekeeper.mult);
147 * wall_to_monotonic is what we need to add to xtime (or xtime corrected 184}
148 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
149 * at zero at system boot time, so wall_to_monotonic will be negative,
150 * however, we will ALWAYS keep the tv_nsec part positive so we can use
151 * the usual normalization.
152 *
153 * wall_to_monotonic is moved after resume from suspend for the monotonic
154 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
155 * to get the real boot based time offset.
156 *
157 * - wall_to_monotonic is no longer the boot time, getboottime must be
158 * used instead.
159 */
160static struct timespec xtime __attribute__ ((aligned (16)));
161static struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
162static struct timespec total_sleep_time;
163
164/*
165 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
166 */
167static struct timespec raw_time;
168 185
169/* flag for if timekeeping is suspended */
170int __read_mostly timekeeping_suspended;
171 186
172/* must hold xtime_lock */
173void timekeeping_leap_insert(int leapsecond) 187void timekeeping_leap_insert(int leapsecond)
174{ 188{
175 xtime.tv_sec += leapsecond; 189 unsigned long flags;
176 wall_to_monotonic.tv_sec -= leapsecond; 190
177 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock, 191 write_seqlock_irqsave(&timekeeper.lock, flags);
178 timekeeper.mult); 192 timekeeper.xtime.tv_sec += leapsecond;
193 timekeeper.wall_to_monotonic.tv_sec -= leapsecond;
194 timekeeping_update(false);
195 write_sequnlock_irqrestore(&timekeeper.lock, flags);
196
179} 197}
180 198
181/** 199/**
@@ -202,10 +220,10 @@ static void timekeeping_forward_now(void)
202 /* If arch requires, add in gettimeoffset() */ 220 /* If arch requires, add in gettimeoffset() */
203 nsec += arch_gettimeoffset(); 221 nsec += arch_gettimeoffset();
204 222
205 timespec_add_ns(&xtime, nsec); 223 timespec_add_ns(&timekeeper.xtime, nsec);
206 224
207 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); 225 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
208 timespec_add_ns(&raw_time, nsec); 226 timespec_add_ns(&timekeeper.raw_time, nsec);
209} 227}
210 228
211/** 229/**
@@ -222,15 +240,15 @@ void getnstimeofday(struct timespec *ts)
222 WARN_ON(timekeeping_suspended); 240 WARN_ON(timekeeping_suspended);
223 241
224 do { 242 do {
225 seq = read_seqbegin(&xtime_lock); 243 seq = read_seqbegin(&timekeeper.lock);
226 244
227 *ts = xtime; 245 *ts = timekeeper.xtime;
228 nsecs = timekeeping_get_ns(); 246 nsecs = timekeeping_get_ns();
229 247
230 /* If arch requires, add in gettimeoffset() */ 248 /* If arch requires, add in gettimeoffset() */
231 nsecs += arch_gettimeoffset(); 249 nsecs += arch_gettimeoffset();
232 250
233 } while (read_seqretry(&xtime_lock, seq)); 251 } while (read_seqretry(&timekeeper.lock, seq));
234 252
235 timespec_add_ns(ts, nsecs); 253 timespec_add_ns(ts, nsecs);
236} 254}
@@ -245,14 +263,16 @@ ktime_t ktime_get(void)
245 WARN_ON(timekeeping_suspended); 263 WARN_ON(timekeeping_suspended);
246 264
247 do { 265 do {
248 seq = read_seqbegin(&xtime_lock); 266 seq = read_seqbegin(&timekeeper.lock);
249 secs = xtime.tv_sec + wall_to_monotonic.tv_sec; 267 secs = timekeeper.xtime.tv_sec +
250 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec; 268 timekeeper.wall_to_monotonic.tv_sec;
269 nsecs = timekeeper.xtime.tv_nsec +
270 timekeeper.wall_to_monotonic.tv_nsec;
251 nsecs += timekeeping_get_ns(); 271 nsecs += timekeeping_get_ns();
252 /* If arch requires, add in gettimeoffset() */ 272 /* If arch requires, add in gettimeoffset() */
253 nsecs += arch_gettimeoffset(); 273 nsecs += arch_gettimeoffset();
254 274
255 } while (read_seqretry(&xtime_lock, seq)); 275 } while (read_seqretry(&timekeeper.lock, seq));
256 /* 276 /*
257 * Use ktime_set/ktime_add_ns to create a proper ktime on 277 * Use ktime_set/ktime_add_ns to create a proper ktime on
258 * 32-bit architectures without CONFIG_KTIME_SCALAR. 278 * 32-bit architectures without CONFIG_KTIME_SCALAR.
@@ -278,14 +298,14 @@ void ktime_get_ts(struct timespec *ts)
278 WARN_ON(timekeeping_suspended); 298 WARN_ON(timekeeping_suspended);
279 299
280 do { 300 do {
281 seq = read_seqbegin(&xtime_lock); 301 seq = read_seqbegin(&timekeeper.lock);
282 *ts = xtime; 302 *ts = timekeeper.xtime;
283 tomono = wall_to_monotonic; 303 tomono = timekeeper.wall_to_monotonic;
284 nsecs = timekeeping_get_ns(); 304 nsecs = timekeeping_get_ns();
285 /* If arch requires, add in gettimeoffset() */ 305 /* If arch requires, add in gettimeoffset() */
286 nsecs += arch_gettimeoffset(); 306 nsecs += arch_gettimeoffset();
287 307
288 } while (read_seqretry(&xtime_lock, seq)); 308 } while (read_seqretry(&timekeeper.lock, seq));
289 309
290 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, 310 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
291 ts->tv_nsec + tomono.tv_nsec + nsecs); 311 ts->tv_nsec + tomono.tv_nsec + nsecs);
@@ -313,10 +333,10 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
313 do { 333 do {
314 u32 arch_offset; 334 u32 arch_offset;
315 335
316 seq = read_seqbegin(&xtime_lock); 336 seq = read_seqbegin(&timekeeper.lock);
317 337
318 *ts_raw = raw_time; 338 *ts_raw = timekeeper.raw_time;
319 *ts_real = xtime; 339 *ts_real = timekeeper.xtime;
320 340
321 nsecs_raw = timekeeping_get_ns_raw(); 341 nsecs_raw = timekeeping_get_ns_raw();
322 nsecs_real = timekeeping_get_ns(); 342 nsecs_real = timekeeping_get_ns();
@@ -326,7 +346,7 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
326 nsecs_raw += arch_offset; 346 nsecs_raw += arch_offset;
327 nsecs_real += arch_offset; 347 nsecs_real += arch_offset;
328 348
329 } while (read_seqretry(&xtime_lock, seq)); 349 } while (read_seqretry(&timekeeper.lock, seq));
330 350
331 timespec_add_ns(ts_raw, nsecs_raw); 351 timespec_add_ns(ts_raw, nsecs_raw);
332 timespec_add_ns(ts_real, nsecs_real); 352 timespec_add_ns(ts_real, nsecs_real);
@@ -365,23 +385,19 @@ int do_settimeofday(const struct timespec *tv)
365 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) 385 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
366 return -EINVAL; 386 return -EINVAL;
367 387
368 write_seqlock_irqsave(&xtime_lock, flags); 388 write_seqlock_irqsave(&timekeeper.lock, flags);
369 389
370 timekeeping_forward_now(); 390 timekeeping_forward_now();
371 391
372 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; 392 ts_delta.tv_sec = tv->tv_sec - timekeeper.xtime.tv_sec;
373 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; 393 ts_delta.tv_nsec = tv->tv_nsec - timekeeper.xtime.tv_nsec;
374 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta); 394 timekeeper.wall_to_monotonic =
375 395 timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
376 xtime = *tv;
377 396
378 timekeeper.ntp_error = 0; 397 timekeeper.xtime = *tv;
379 ntp_clear(); 398 timekeeping_update(true);
380
381 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
382 timekeeper.mult);
383 399
384 write_sequnlock_irqrestore(&xtime_lock, flags); 400 write_sequnlock_irqrestore(&timekeeper.lock, flags);
385 401
386 /* signal hrtimers about time change */ 402 /* signal hrtimers about time change */
387 clock_was_set(); 403 clock_was_set();
@@ -405,20 +421,17 @@ int timekeeping_inject_offset(struct timespec *ts)
405 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 421 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
406 return -EINVAL; 422 return -EINVAL;
407 423
408 write_seqlock_irqsave(&xtime_lock, flags); 424 write_seqlock_irqsave(&timekeeper.lock, flags);
409 425
410 timekeeping_forward_now(); 426 timekeeping_forward_now();
411 427
412 xtime = timespec_add(xtime, *ts); 428 timekeeper.xtime = timespec_add(timekeeper.xtime, *ts);
413 wall_to_monotonic = timespec_sub(wall_to_monotonic, *ts); 429 timekeeper.wall_to_monotonic =
414 430 timespec_sub(timekeeper.wall_to_monotonic, *ts);
415 timekeeper.ntp_error = 0;
416 ntp_clear();
417 431
418 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock, 432 timekeeping_update(true);
419 timekeeper.mult);
420 433
421 write_sequnlock_irqrestore(&xtime_lock, flags); 434 write_sequnlock_irqrestore(&timekeeper.lock, flags);
422 435
423 /* signal hrtimers about time change */ 436 /* signal hrtimers about time change */
424 clock_was_set(); 437 clock_was_set();
@@ -490,11 +503,11 @@ void getrawmonotonic(struct timespec *ts)
490 s64 nsecs; 503 s64 nsecs;
491 504
492 do { 505 do {
493 seq = read_seqbegin(&xtime_lock); 506 seq = read_seqbegin(&timekeeper.lock);
494 nsecs = timekeeping_get_ns_raw(); 507 nsecs = timekeeping_get_ns_raw();
495 *ts = raw_time; 508 *ts = timekeeper.raw_time;
496 509
497 } while (read_seqretry(&xtime_lock, seq)); 510 } while (read_seqretry(&timekeeper.lock, seq));
498 511
499 timespec_add_ns(ts, nsecs); 512 timespec_add_ns(ts, nsecs);
500} 513}
@@ -510,24 +523,30 @@ int timekeeping_valid_for_hres(void)
510 int ret; 523 int ret;
511 524
512 do { 525 do {
513 seq = read_seqbegin(&xtime_lock); 526 seq = read_seqbegin(&timekeeper.lock);
514 527
515 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 528 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
516 529
517 } while (read_seqretry(&xtime_lock, seq)); 530 } while (read_seqretry(&timekeeper.lock, seq));
518 531
519 return ret; 532 return ret;
520} 533}
521 534
522/** 535/**
523 * timekeeping_max_deferment - Returns max time the clocksource can be deferred 536 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
524 *
525 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
526 * ensure that the clocksource does not change!
527 */ 537 */
528u64 timekeeping_max_deferment(void) 538u64 timekeeping_max_deferment(void)
529{ 539{
530 return timekeeper.clock->max_idle_ns; 540 unsigned long seq;
541 u64 ret;
542 do {
543 seq = read_seqbegin(&timekeeper.lock);
544
545 ret = timekeeper.clock->max_idle_ns;
546
547 } while (read_seqretry(&timekeeper.lock, seq));
548
549 return ret;
531} 550}
532 551
533/** 552/**
@@ -572,28 +591,29 @@ void __init timekeeping_init(void)
572 read_persistent_clock(&now); 591 read_persistent_clock(&now);
573 read_boot_clock(&boot); 592 read_boot_clock(&boot);
574 593
575 write_seqlock_irqsave(&xtime_lock, flags); 594 seqlock_init(&timekeeper.lock);
576 595
577 ntp_init(); 596 ntp_init();
578 597
598 write_seqlock_irqsave(&timekeeper.lock, flags);
579 clock = clocksource_default_clock(); 599 clock = clocksource_default_clock();
580 if (clock->enable) 600 if (clock->enable)
581 clock->enable(clock); 601 clock->enable(clock);
582 timekeeper_setup_internals(clock); 602 timekeeper_setup_internals(clock);
583 603
584 xtime.tv_sec = now.tv_sec; 604 timekeeper.xtime.tv_sec = now.tv_sec;
585 xtime.tv_nsec = now.tv_nsec; 605 timekeeper.xtime.tv_nsec = now.tv_nsec;
586 raw_time.tv_sec = 0; 606 timekeeper.raw_time.tv_sec = 0;
587 raw_time.tv_nsec = 0; 607 timekeeper.raw_time.tv_nsec = 0;
588 if (boot.tv_sec == 0 && boot.tv_nsec == 0) { 608 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
589 boot.tv_sec = xtime.tv_sec; 609 boot.tv_sec = timekeeper.xtime.tv_sec;
590 boot.tv_nsec = xtime.tv_nsec; 610 boot.tv_nsec = timekeeper.xtime.tv_nsec;
591 } 611 }
592 set_normalized_timespec(&wall_to_monotonic, 612 set_normalized_timespec(&timekeeper.wall_to_monotonic,
593 -boot.tv_sec, -boot.tv_nsec); 613 -boot.tv_sec, -boot.tv_nsec);
594 total_sleep_time.tv_sec = 0; 614 timekeeper.total_sleep_time.tv_sec = 0;
595 total_sleep_time.tv_nsec = 0; 615 timekeeper.total_sleep_time.tv_nsec = 0;
596 write_sequnlock_irqrestore(&xtime_lock, flags); 616 write_sequnlock_irqrestore(&timekeeper.lock, flags);
597} 617}
598 618
599/* time in seconds when suspend began */ 619/* time in seconds when suspend began */
@@ -614,9 +634,11 @@ static void __timekeeping_inject_sleeptime(struct timespec *delta)
614 return; 634 return;
615 } 635 }
616 636
617 xtime = timespec_add(xtime, *delta); 637 timekeeper.xtime = timespec_add(timekeeper.xtime, *delta);
618 wall_to_monotonic = timespec_sub(wall_to_monotonic, *delta); 638 timekeeper.wall_to_monotonic =
619 total_sleep_time = timespec_add(total_sleep_time, *delta); 639 timespec_sub(timekeeper.wall_to_monotonic, *delta);
640 timekeeper.total_sleep_time = timespec_add(
641 timekeeper.total_sleep_time, *delta);
620} 642}
621 643
622 644
@@ -640,17 +662,15 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
640 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0)) 662 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
641 return; 663 return;
642 664
643 write_seqlock_irqsave(&xtime_lock, flags); 665 write_seqlock_irqsave(&timekeeper.lock, flags);
666
644 timekeeping_forward_now(); 667 timekeeping_forward_now();
645 668
646 __timekeeping_inject_sleeptime(delta); 669 __timekeeping_inject_sleeptime(delta);
647 670
648 timekeeper.ntp_error = 0; 671 timekeeping_update(true);
649 ntp_clear();
650 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
651 timekeeper.mult);
652 672
653 write_sequnlock_irqrestore(&xtime_lock, flags); 673 write_sequnlock_irqrestore(&timekeeper.lock, flags);
654 674
655 /* signal hrtimers about time change */ 675 /* signal hrtimers about time change */
656 clock_was_set(); 676 clock_was_set();
@@ -673,7 +693,7 @@ static void timekeeping_resume(void)
673 693
674 clocksource_resume(); 694 clocksource_resume();
675 695
676 write_seqlock_irqsave(&xtime_lock, flags); 696 write_seqlock_irqsave(&timekeeper.lock, flags);
677 697
678 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 698 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
679 ts = timespec_sub(ts, timekeeping_suspend_time); 699 ts = timespec_sub(ts, timekeeping_suspend_time);
@@ -683,7 +703,7 @@ static void timekeeping_resume(void)
683 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); 703 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
684 timekeeper.ntp_error = 0; 704 timekeeper.ntp_error = 0;
685 timekeeping_suspended = 0; 705 timekeeping_suspended = 0;
686 write_sequnlock_irqrestore(&xtime_lock, flags); 706 write_sequnlock_irqrestore(&timekeeper.lock, flags);
687 707
688 touch_softlockup_watchdog(); 708 touch_softlockup_watchdog();
689 709
@@ -701,7 +721,7 @@ static int timekeeping_suspend(void)
701 721
702 read_persistent_clock(&timekeeping_suspend_time); 722 read_persistent_clock(&timekeeping_suspend_time);
703 723
704 write_seqlock_irqsave(&xtime_lock, flags); 724 write_seqlock_irqsave(&timekeeper.lock, flags);
705 timekeeping_forward_now(); 725 timekeeping_forward_now();
706 timekeeping_suspended = 1; 726 timekeeping_suspended = 1;
707 727
@@ -711,7 +731,7 @@ static int timekeeping_suspend(void)
711 * try to compensate so the difference in system time 731 * try to compensate so the difference in system time
712 * and persistent_clock time stays close to constant. 732 * and persistent_clock time stays close to constant.
713 */ 733 */
714 delta = timespec_sub(xtime, timekeeping_suspend_time); 734 delta = timespec_sub(timekeeper.xtime, timekeeping_suspend_time);
715 delta_delta = timespec_sub(delta, old_delta); 735 delta_delta = timespec_sub(delta, old_delta);
716 if (abs(delta_delta.tv_sec) >= 2) { 736 if (abs(delta_delta.tv_sec) >= 2) {
717 /* 737 /*
@@ -724,7 +744,7 @@ static int timekeeping_suspend(void)
724 timekeeping_suspend_time = 744 timekeeping_suspend_time =
725 timespec_add(timekeeping_suspend_time, delta_delta); 745 timespec_add(timekeeping_suspend_time, delta_delta);
726 } 746 }
727 write_sequnlock_irqrestore(&xtime_lock, flags); 747 write_sequnlock_irqrestore(&timekeeper.lock, flags);
728 748
729 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); 749 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
730 clocksource_suspend(); 750 clocksource_suspend();
@@ -775,7 +795,7 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
775 * Now calculate the error in (1 << look_ahead) ticks, but first 795 * Now calculate the error in (1 << look_ahead) ticks, but first
776 * remove the single look ahead already included in the error. 796 * remove the single look ahead already included in the error.
777 */ 797 */
778 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1); 798 tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1);
779 tick_error -= timekeeper.xtime_interval >> 1; 799 tick_error -= timekeeper.xtime_interval >> 1;
780 error = ((error - tick_error) >> look_ahead) + tick_error; 800 error = ((error - tick_error) >> look_ahead) + tick_error;
781 801
@@ -943,22 +963,22 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
943 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift; 963 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
944 while (timekeeper.xtime_nsec >= nsecps) { 964 while (timekeeper.xtime_nsec >= nsecps) {
945 timekeeper.xtime_nsec -= nsecps; 965 timekeeper.xtime_nsec -= nsecps;
946 xtime.tv_sec++; 966 timekeeper.xtime.tv_sec++;
947 second_overflow(); 967 second_overflow();
948 } 968 }
949 969
950 /* Accumulate raw time */ 970 /* Accumulate raw time */
951 raw_nsecs = timekeeper.raw_interval << shift; 971 raw_nsecs = timekeeper.raw_interval << shift;
952 raw_nsecs += raw_time.tv_nsec; 972 raw_nsecs += timekeeper.raw_time.tv_nsec;
953 if (raw_nsecs >= NSEC_PER_SEC) { 973 if (raw_nsecs >= NSEC_PER_SEC) {
954 u64 raw_secs = raw_nsecs; 974 u64 raw_secs = raw_nsecs;
955 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); 975 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
956 raw_time.tv_sec += raw_secs; 976 timekeeper.raw_time.tv_sec += raw_secs;
957 } 977 }
958 raw_time.tv_nsec = raw_nsecs; 978 timekeeper.raw_time.tv_nsec = raw_nsecs;
959 979
960 /* Accumulate error between NTP and clock interval */ 980 /* Accumulate error between NTP and clock interval */
961 timekeeper.ntp_error += tick_length << shift; 981 timekeeper.ntp_error += ntp_tick_length() << shift;
962 timekeeper.ntp_error -= 982 timekeeper.ntp_error -=
963 (timekeeper.xtime_interval + timekeeper.xtime_remainder) << 983 (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
964 (timekeeper.ntp_error_shift + shift); 984 (timekeeper.ntp_error_shift + shift);
@@ -970,17 +990,19 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
970/** 990/**
971 * update_wall_time - Uses the current clocksource to increment the wall time 991 * update_wall_time - Uses the current clocksource to increment the wall time
972 * 992 *
973 * Called from the timer interrupt, must hold a write on xtime_lock.
974 */ 993 */
975static void update_wall_time(void) 994static void update_wall_time(void)
976{ 995{
977 struct clocksource *clock; 996 struct clocksource *clock;
978 cycle_t offset; 997 cycle_t offset;
979 int shift = 0, maxshift; 998 int shift = 0, maxshift;
999 unsigned long flags;
1000
1001 write_seqlock_irqsave(&timekeeper.lock, flags);
980 1002
981 /* Make sure we're fully resumed: */ 1003 /* Make sure we're fully resumed: */
982 if (unlikely(timekeeping_suspended)) 1004 if (unlikely(timekeeping_suspended))
983 return; 1005 goto out;
984 1006
985 clock = timekeeper.clock; 1007 clock = timekeeper.clock;
986 1008
@@ -989,7 +1011,8 @@ static void update_wall_time(void)
989#else 1011#else
990 offset = (clock->read(clock) - clock->cycle_last) & clock->mask; 1012 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
991#endif 1013#endif
992 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift; 1014 timekeeper.xtime_nsec = (s64)timekeeper.xtime.tv_nsec <<
1015 timekeeper.shift;
993 1016
994 /* 1017 /*
995 * With NO_HZ we may have to accumulate many cycle_intervals 1018 * With NO_HZ we may have to accumulate many cycle_intervals
@@ -1002,7 +1025,7 @@ static void update_wall_time(void)
1002 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval); 1025 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
1003 shift = max(0, shift); 1026 shift = max(0, shift);
1004 /* Bound shift to one less then what overflows tick_length */ 1027 /* Bound shift to one less then what overflows tick_length */
1005 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1; 1028 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
1006 shift = min(shift, maxshift); 1029 shift = min(shift, maxshift);
1007 while (offset >= timekeeper.cycle_interval) { 1030 while (offset >= timekeeper.cycle_interval) {
1008 offset = logarithmic_accumulation(offset, shift); 1031 offset = logarithmic_accumulation(offset, shift);
@@ -1040,8 +1063,10 @@ static void update_wall_time(void)
1040 * Store full nanoseconds into xtime after rounding it up and 1063 * Store full nanoseconds into xtime after rounding it up and
1041 * add the remainder to the error difference. 1064 * add the remainder to the error difference.
1042 */ 1065 */
1043 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1; 1066 timekeeper.xtime.tv_nsec = ((s64)timekeeper.xtime_nsec >>
1044 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift; 1067 timekeeper.shift) + 1;
1068 timekeeper.xtime_nsec -= (s64)timekeeper.xtime.tv_nsec <<
1069 timekeeper.shift;
1045 timekeeper.ntp_error += timekeeper.xtime_nsec << 1070 timekeeper.ntp_error += timekeeper.xtime_nsec <<
1046 timekeeper.ntp_error_shift; 1071 timekeeper.ntp_error_shift;
1047 1072
@@ -1049,15 +1074,17 @@ static void update_wall_time(void)
1049 * Finally, make sure that after the rounding 1074 * Finally, make sure that after the rounding
1050 * xtime.tv_nsec isn't larger then NSEC_PER_SEC 1075 * xtime.tv_nsec isn't larger then NSEC_PER_SEC
1051 */ 1076 */
1052 if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) { 1077 if (unlikely(timekeeper.xtime.tv_nsec >= NSEC_PER_SEC)) {
1053 xtime.tv_nsec -= NSEC_PER_SEC; 1078 timekeeper.xtime.tv_nsec -= NSEC_PER_SEC;
1054 xtime.tv_sec++; 1079 timekeeper.xtime.tv_sec++;
1055 second_overflow(); 1080 second_overflow();
1056 } 1081 }
1057 1082
1058 /* check to see if there is a new clocksource to use */ 1083 timekeeping_update(false);
1059 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock, 1084
1060 timekeeper.mult); 1085out:
1086 write_sequnlock_irqrestore(&timekeeper.lock, flags);
1087
1061} 1088}
1062 1089
1063/** 1090/**
@@ -1074,8 +1101,10 @@ static void update_wall_time(void)
1074void getboottime(struct timespec *ts) 1101void getboottime(struct timespec *ts)
1075{ 1102{
1076 struct timespec boottime = { 1103 struct timespec boottime = {
1077 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec, 1104 .tv_sec = timekeeper.wall_to_monotonic.tv_sec +
1078 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec 1105 timekeeper.total_sleep_time.tv_sec,
1106 .tv_nsec = timekeeper.wall_to_monotonic.tv_nsec +
1107 timekeeper.total_sleep_time.tv_nsec
1079 }; 1108 };
1080 1109
1081 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); 1110 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
@@ -1101,13 +1130,13 @@ void get_monotonic_boottime(struct timespec *ts)
1101 WARN_ON(timekeeping_suspended); 1130 WARN_ON(timekeeping_suspended);
1102 1131
1103 do { 1132 do {
1104 seq = read_seqbegin(&xtime_lock); 1133 seq = read_seqbegin(&timekeeper.lock);
1105 *ts = xtime; 1134 *ts = timekeeper.xtime;
1106 tomono = wall_to_monotonic; 1135 tomono = timekeeper.wall_to_monotonic;
1107 sleep = total_sleep_time; 1136 sleep = timekeeper.total_sleep_time;
1108 nsecs = timekeeping_get_ns(); 1137 nsecs = timekeeping_get_ns();
1109 1138
1110 } while (read_seqretry(&xtime_lock, seq)); 1139 } while (read_seqretry(&timekeeper.lock, seq));
1111 1140
1112 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec, 1141 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
1113 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs); 1142 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
@@ -1137,19 +1166,19 @@ EXPORT_SYMBOL_GPL(ktime_get_boottime);
1137 */ 1166 */
1138void monotonic_to_bootbased(struct timespec *ts) 1167void monotonic_to_bootbased(struct timespec *ts)
1139{ 1168{
1140 *ts = timespec_add(*ts, total_sleep_time); 1169 *ts = timespec_add(*ts, timekeeper.total_sleep_time);
1141} 1170}
1142EXPORT_SYMBOL_GPL(monotonic_to_bootbased); 1171EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
1143 1172
1144unsigned long get_seconds(void) 1173unsigned long get_seconds(void)
1145{ 1174{
1146 return xtime.tv_sec; 1175 return timekeeper.xtime.tv_sec;
1147} 1176}
1148EXPORT_SYMBOL(get_seconds); 1177EXPORT_SYMBOL(get_seconds);
1149 1178
1150struct timespec __current_kernel_time(void) 1179struct timespec __current_kernel_time(void)
1151{ 1180{
1152 return xtime; 1181 return timekeeper.xtime;
1153} 1182}
1154 1183
1155struct timespec current_kernel_time(void) 1184struct timespec current_kernel_time(void)
@@ -1158,10 +1187,10 @@ struct timespec current_kernel_time(void)
1158 unsigned long seq; 1187 unsigned long seq;
1159 1188
1160 do { 1189 do {
1161 seq = read_seqbegin(&xtime_lock); 1190 seq = read_seqbegin(&timekeeper.lock);
1162 1191
1163 now = xtime; 1192 now = timekeeper.xtime;
1164 } while (read_seqretry(&xtime_lock, seq)); 1193 } while (read_seqretry(&timekeeper.lock, seq));
1165 1194
1166 return now; 1195 return now;
1167} 1196}
@@ -1173,11 +1202,11 @@ struct timespec get_monotonic_coarse(void)
1173 unsigned long seq; 1202 unsigned long seq;
1174 1203
1175 do { 1204 do {
1176 seq = read_seqbegin(&xtime_lock); 1205 seq = read_seqbegin(&timekeeper.lock);
1177 1206
1178 now = xtime; 1207 now = timekeeper.xtime;
1179 mono = wall_to_monotonic; 1208 mono = timekeeper.wall_to_monotonic;
1180 } while (read_seqretry(&xtime_lock, seq)); 1209 } while (read_seqretry(&timekeeper.lock, seq));
1181 1210
1182 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, 1211 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1183 now.tv_nsec + mono.tv_nsec); 1212 now.tv_nsec + mono.tv_nsec);
@@ -1209,11 +1238,11 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1209 unsigned long seq; 1238 unsigned long seq;
1210 1239
1211 do { 1240 do {
1212 seq = read_seqbegin(&xtime_lock); 1241 seq = read_seqbegin(&timekeeper.lock);
1213 *xtim = xtime; 1242 *xtim = timekeeper.xtime;
1214 *wtom = wall_to_monotonic; 1243 *wtom = timekeeper.wall_to_monotonic;
1215 *sleep = total_sleep_time; 1244 *sleep = timekeeper.total_sleep_time;
1216 } while (read_seqretry(&xtime_lock, seq)); 1245 } while (read_seqretry(&timekeeper.lock, seq));
1217} 1246}
1218 1247
1219/** 1248/**
@@ -1225,9 +1254,10 @@ ktime_t ktime_get_monotonic_offset(void)
1225 struct timespec wtom; 1254 struct timespec wtom;
1226 1255
1227 do { 1256 do {
1228 seq = read_seqbegin(&xtime_lock); 1257 seq = read_seqbegin(&timekeeper.lock);
1229 wtom = wall_to_monotonic; 1258 wtom = timekeeper.wall_to_monotonic;
1230 } while (read_seqretry(&xtime_lock, seq)); 1259 } while (read_seqretry(&timekeeper.lock, seq));
1260
1231 return timespec_to_ktime(wtom); 1261 return timespec_to_ktime(wtom);
1232} 1262}
1233 1263