Skip to content

Commit 5f4b37d

Browse files
committed
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timer fixes from Thomas Gleixner: "A few fixes for timekeeping and timers: - Plug a subtle race due to a missing READ_ONCE() in the timekeeping code where reloading of a pointer results in an inconsistent callback argument being supplied to the clocksource->read function. - Correct the CLOCK_MONOTONIC_RAW sub-nanosecond accounting in the time keeping core code, to prevent a possible discontuity. - Apply a similar fix to the arm64 vdso clock_gettime() implementation - Add missing includes to clocksource drivers, which relied on indirect includes which fails in certain configs. - Use the proper iomem pointer for read/iounmap in a probe function" * 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: arm64/vdso: Fix nsec handling for CLOCK_MONOTONIC_RAW time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting time: Fix clock->read(clock) race around clocksource changes clocksource: Explicitly include linux/clocksource.h when needed clocksource/drivers/arm_arch_timer: Fix read and iounmap of incorrect variable
2 parents 35d8d5d + 8e6cec1 commit 5f4b37d

File tree

7 files changed

+55
-33
lines changed

7 files changed

+55
-33
lines changed

arch/arm64/kernel/vdso.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ void update_vsyscall(struct timekeeper *tk)
221221
/* tkr_mono.cycle_last == tkr_raw.cycle_last */
222222
vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
223223
vdso_data->raw_time_sec = tk->raw_time.tv_sec;
224-
vdso_data->raw_time_nsec = tk->raw_time.tv_nsec;
224+
vdso_data->raw_time_nsec = (tk->raw_time.tv_nsec <<
225+
tk->tkr_raw.shift) +
226+
tk->tkr_raw.xtime_nsec;
225227
vdso_data->xtime_clock_sec = tk->xtime_sec;
226228
vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
227-
/* tkr_raw.xtime_nsec == 0 */
228229
vdso_data->cs_mono_mult = tk->tkr_mono.mult;
229230
vdso_data->cs_raw_mult = tk->tkr_raw.mult;
230231
/* tkr_mono.shift == tkr_raw.shift */

arch/arm64/kernel/vdso/gettimeofday.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ monotonic_raw:
256256
seqcnt_check fail=monotonic_raw
257257

258258
/* All computations are done with left-shifted nsecs. */
259-
lsl x14, x14, x12
260259
get_nsec_per_sec res=x9
261260
lsl x9, x9, x12
262261

drivers/clocksource/arm_arch_timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,9 @@ arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
12091209
return 0;
12101210
}
12111211

1212-
rate = readl_relaxed(frame + CNTFRQ);
1212+
rate = readl_relaxed(base + CNTFRQ);
12131213

1214-
iounmap(frame);
1214+
iounmap(base);
12151215

12161216
return rate;
12171217
}

drivers/clocksource/cadence_ttc_timer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/clk.h>
1919
#include <linux/interrupt.h>
2020
#include <linux/clockchips.h>
21+
#include <linux/clocksource.h>
2122
#include <linux/of_address.h>
2223
#include <linux/of_irq.h>
2324
#include <linux/slab.h>

drivers/clocksource/timer-sun5i.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/clk.h>
1414
#include <linux/clockchips.h>
15+
#include <linux/clocksource.h>
1516
#include <linux/delay.h>
1617
#include <linux/interrupt.h>
1718
#include <linux/irq.h>

include/linux/timekeeper_internal.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
struct tk_read_base {
3131
struct clocksource *clock;
32-
u64 (*read)(struct clocksource *cs);
3332
u64 mask;
3433
u64 cycle_last;
3534
u32 mult;
@@ -58,7 +57,7 @@ struct tk_read_base {
5857
* interval.
5958
* @xtime_remainder: Shifted nano seconds left over when rounding
6059
* @cycle_interval
61-
* @raw_interval: Raw nano seconds accumulated per NTP interval.
60+
* @raw_interval: Shifted raw nano seconds accumulated per NTP interval.
6261
* @ntp_error: Difference between accumulated time and NTP time in ntp
6362
* shifted nano seconds.
6463
* @ntp_error_shift: Shift conversion between clock shifted nano seconds and
@@ -100,7 +99,7 @@ struct timekeeper {
10099
u64 cycle_interval;
101100
u64 xtime_interval;
102101
s64 xtime_remainder;
103-
u32 raw_interval;
102+
u64 raw_interval;
104103
/* The ntp_tick_length() value currently being used.
105104
* This cached copy ensures we consistently apply the tick
106105
* length for an entire tick, as ntp_tick_length may change

kernel/time/timekeeping.c

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
118118
tk->offs_boot = ktime_add(tk->offs_boot, delta);
119119
}
120120

121+
/*
122+
* tk_clock_read - atomic clocksource read() helper
123+
*
124+
* This helper is necessary to use in the read paths because, while the
125+
* seqlock ensures we don't return a bad value while structures are updated,
126+
* it doesn't protect from potential crashes. There is the possibility that
127+
* the tkr's clocksource may change between the read reference, and the
128+
* clock reference passed to the read function. This can cause crashes if
129+
* the wrong clocksource is passed to the wrong read function.
130+
* This isn't necessary to use when holding the timekeeper_lock or doing
131+
* a read of the fast-timekeeper tkrs (which is protected by its own locking
132+
* and update logic).
133+
*/
134+
static inline u64 tk_clock_read(struct tk_read_base *tkr)
135+
{
136+
struct clocksource *clock = READ_ONCE(tkr->clock);
137+
138+
return clock->read(clock);
139+
}
140+
121141
#ifdef CONFIG_DEBUG_TIMEKEEPING
122142
#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
123143

@@ -175,7 +195,7 @@ static inline u64 timekeeping_get_delta(struct tk_read_base *tkr)
175195
*/
176196
do {
177197
seq = read_seqcount_begin(&tk_core.seq);
178-
now = tkr->read(tkr->clock);
198+
now = tk_clock_read(tkr);
179199
last = tkr->cycle_last;
180200
mask = tkr->mask;
181201
max = tkr->clock->max_cycles;
@@ -209,7 +229,7 @@ static inline u64 timekeeping_get_delta(struct tk_read_base *tkr)
209229
u64 cycle_now, delta;
210230

211231
/* read clocksource */
212-
cycle_now = tkr->read(tkr->clock);
232+
cycle_now = tk_clock_read(tkr);
213233

214234
/* calculate the delta since the last update_wall_time */
215235
delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
@@ -238,12 +258,10 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
238258
++tk->cs_was_changed_seq;
239259
old_clock = tk->tkr_mono.clock;
240260
tk->tkr_mono.clock = clock;
241-
tk->tkr_mono.read = clock->read;
242261
tk->tkr_mono.mask = clock->mask;
243-
tk->tkr_mono.cycle_last = tk->tkr_mono.read(clock);
262+
tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
244263

245264
tk->tkr_raw.clock = clock;
246-
tk->tkr_raw.read = clock->read;
247265
tk->tkr_raw.mask = clock->mask;
248266
tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
249267

@@ -262,7 +280,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
262280
/* Go back from cycles -> shifted ns */
263281
tk->xtime_interval = interval * clock->mult;
264282
tk->xtime_remainder = ntpinterval - tk->xtime_interval;
265-
tk->raw_interval = (interval * clock->mult) >> clock->shift;
283+
tk->raw_interval = interval * clock->mult;
266284

267285
/* if changing clocks, convert xtime_nsec shift units */
268286
if (old_clock) {
@@ -404,7 +422,7 @@ static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
404422

405423
now += timekeeping_delta_to_ns(tkr,
406424
clocksource_delta(
407-
tkr->read(tkr->clock),
425+
tk_clock_read(tkr),
408426
tkr->cycle_last,
409427
tkr->mask));
410428
} while (read_seqcount_retry(&tkf->seq, seq));
@@ -461,6 +479,10 @@ static u64 dummy_clock_read(struct clocksource *cs)
461479
return cycles_at_suspend;
462480
}
463481

482+
static struct clocksource dummy_clock = {
483+
.read = dummy_clock_read,
484+
};
485+
464486
/**
465487
* halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
466488
* @tk: Timekeeper to snapshot.
@@ -477,13 +499,13 @@ static void halt_fast_timekeeper(struct timekeeper *tk)
477499
struct tk_read_base *tkr = &tk->tkr_mono;
478500

479501
memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
480-
cycles_at_suspend = tkr->read(tkr->clock);
481-
tkr_dummy.read = dummy_clock_read;
502+
cycles_at_suspend = tk_clock_read(tkr);
503+
tkr_dummy.clock = &dummy_clock;
482504
update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
483505

484506
tkr = &tk->tkr_raw;
485507
memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
486-
tkr_dummy.read = dummy_clock_read;
508+
tkr_dummy.clock = &dummy_clock;
487509
update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
488510
}
489511

@@ -649,11 +671,10 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action)
649671
*/
650672
static void timekeeping_forward_now(struct timekeeper *tk)
651673
{
652-
struct clocksource *clock = tk->tkr_mono.clock;
653674
u64 cycle_now, delta;
654675
u64 nsec;
655676

656-
cycle_now = tk->tkr_mono.read(clock);
677+
cycle_now = tk_clock_read(&tk->tkr_mono);
657678
delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
658679
tk->tkr_mono.cycle_last = cycle_now;
659680
tk->tkr_raw.cycle_last = cycle_now;
@@ -929,8 +950,7 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
929950

930951
do {
931952
seq = read_seqcount_begin(&tk_core.seq);
932-
933-
now = tk->tkr_mono.read(tk->tkr_mono.clock);
953+
now = tk_clock_read(&tk->tkr_mono);
934954
systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
935955
systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
936956
base_real = ktime_add(tk->tkr_mono.base,
@@ -1108,7 +1128,7 @@ int get_device_system_crosststamp(int (*get_time_fn)
11081128
* Check whether the system counter value provided by the
11091129
* device driver is on the current timekeeping interval.
11101130
*/
1111-
now = tk->tkr_mono.read(tk->tkr_mono.clock);
1131+
now = tk_clock_read(&tk->tkr_mono);
11121132
interval_start = tk->tkr_mono.cycle_last;
11131133
if (!cycle_between(interval_start, cycles, now)) {
11141134
clock_was_set_seq = tk->clock_was_set_seq;
@@ -1629,7 +1649,7 @@ void timekeeping_resume(void)
16291649
* The less preferred source will only be tried if there is no better
16301650
* usable source. The rtc part is handled separately in rtc core code.
16311651
*/
1632-
cycle_now = tk->tkr_mono.read(clock);
1652+
cycle_now = tk_clock_read(&tk->tkr_mono);
16331653
if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
16341654
cycle_now > tk->tkr_mono.cycle_last) {
16351655
u64 nsec, cyc_delta;
@@ -1976,7 +1996,7 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
19761996
u32 shift, unsigned int *clock_set)
19771997
{
19781998
u64 interval = tk->cycle_interval << shift;
1979-
u64 raw_nsecs;
1999+
u64 snsec_per_sec;
19802000

19812001
/* If the offset is smaller than a shifted interval, do nothing */
19822002
if (offset < interval)
@@ -1991,14 +2011,15 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
19912011
*clock_set |= accumulate_nsecs_to_secs(tk);
19922012

19932013
/* Accumulate raw time */
1994-
raw_nsecs = (u64)tk->raw_interval << shift;
1995-
raw_nsecs += tk->raw_time.tv_nsec;
1996-
if (raw_nsecs >= NSEC_PER_SEC) {
1997-
u64 raw_secs = raw_nsecs;
1998-
raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
1999-
tk->raw_time.tv_sec += raw_secs;
2014+
tk->tkr_raw.xtime_nsec += (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
2015+
tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2016+
snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2017+
while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2018+
tk->tkr_raw.xtime_nsec -= snsec_per_sec;
2019+
tk->raw_time.tv_sec++;
20002020
}
2001-
tk->raw_time.tv_nsec = raw_nsecs;
2021+
tk->raw_time.tv_nsec = tk->tkr_raw.xtime_nsec >> tk->tkr_raw.shift;
2022+
tk->tkr_raw.xtime_nsec -= (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
20022023

20032024
/* Accumulate error between NTP and clock interval */
20042025
tk->ntp_error += tk->ntp_tick << shift;
@@ -2030,7 +2051,7 @@ void update_wall_time(void)
20302051
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
20312052
offset = real_tk->cycle_interval;
20322053
#else
2033-
offset = clocksource_delta(tk->tkr_mono.read(tk->tkr_mono.clock),
2054+
offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
20342055
tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
20352056
#endif
20362057

0 commit comments

Comments
 (0)