timekeeping: Use sequence counter with associated raw spinlock

A sequence counter write side critical section must be protected by some
form of locking to serialize writers. A plain seqcount_t does not
contain the information of which lock must be held when entering a write
side critical section.

Use the new seqcount_raw_spinlock_t data type, which allows to associate
a raw spinlock with the sequence counter. This enables lockdep to verify
that the raw spinlock used for writer serialization is held when the
write side critical section is entered.

If lockdep is disabled this lock association is compiled out and has
neither storage size nor runtime overhead.

Signed-off-by: Ahmed S. Darwish <a.darwish@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20200720155530.1173732-18-a.darwish@linutronix.de
This commit is contained in:
Ahmed S. Darwish 2020-07-20 17:55:23 +02:00 committed by Peter Zijlstra
parent 77cc278f7b
commit 025e82bcbc
1 changed files with 11 additions and 8 deletions

View File

@ -39,18 +39,19 @@ enum timekeeping_adv_mode {
TK_ADV_FREQ TK_ADV_FREQ
}; };
static DEFINE_RAW_SPINLOCK(timekeeper_lock);
/* /*
* The most important data for readout fits into a single 64 byte * The most important data for readout fits into a single 64 byte
* cache line. * cache line.
*/ */
static struct { static struct {
seqcount_t seq; seqcount_raw_spinlock_t seq;
struct timekeeper timekeeper; struct timekeeper timekeeper;
} tk_core ____cacheline_aligned = { } tk_core ____cacheline_aligned = {
.seq = SEQCNT_ZERO(tk_core.seq), .seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_core.seq, &timekeeper_lock),
}; };
static DEFINE_RAW_SPINLOCK(timekeeper_lock);
static struct timekeeper shadow_timekeeper; static struct timekeeper shadow_timekeeper;
/** /**
@ -63,7 +64,7 @@ static struct timekeeper shadow_timekeeper;
* See @update_fast_timekeeper() below. * See @update_fast_timekeeper() below.
*/ */
struct tk_fast { struct tk_fast {
seqcount_t seq; seqcount_raw_spinlock_t seq;
struct tk_read_base base[2]; struct tk_read_base base[2];
}; };
@ -80,11 +81,13 @@ static struct clocksource dummy_clock = {
}; };
static struct tk_fast tk_fast_mono ____cacheline_aligned = { static struct tk_fast tk_fast_mono ____cacheline_aligned = {
.seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_fast_mono.seq, &timekeeper_lock),
.base[0] = { .clock = &dummy_clock, }, .base[0] = { .clock = &dummy_clock, },
.base[1] = { .clock = &dummy_clock, }, .base[1] = { .clock = &dummy_clock, },
}; };
static struct tk_fast tk_fast_raw ____cacheline_aligned = { static struct tk_fast tk_fast_raw ____cacheline_aligned = {
.seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_fast_raw.seq, &timekeeper_lock),
.base[0] = { .clock = &dummy_clock, }, .base[0] = { .clock = &dummy_clock, },
.base[1] = { .clock = &dummy_clock, }, .base[1] = { .clock = &dummy_clock, },
}; };
@ -157,7 +160,7 @@ static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
* tk_clock_read - atomic clocksource read() helper * tk_clock_read - atomic clocksource read() helper
* *
* This helper is necessary to use in the read paths because, while the * This helper is necessary to use in the read paths because, while the
* seqlock ensures we don't return a bad value while structures are updated, * seqcount ensures we don't return a bad value while structures are updated,
* it doesn't protect from potential crashes. There is the possibility that * it doesn't protect from potential crashes. There is the possibility that
* the tkr's clocksource may change between the read reference, and the * the tkr's clocksource may change between the read reference, and the
* clock reference passed to the read function. This can cause crashes if * clock reference passed to the read function. This can cause crashes if
@ -222,10 +225,10 @@ static inline u64 timekeeping_get_delta(const struct tk_read_base *tkr)
unsigned int seq; unsigned int seq;
/* /*
* Since we're called holding a seqlock, the data may shift * Since we're called holding a seqcount, the data may shift
* under us while we're doing the calculation. This can cause * under us while we're doing the calculation. This can cause
* false positives, since we'd note a problem but throw the * false positives, since we'd note a problem but throw the
* results away. So nest another seqlock here to atomically * results away. So nest another seqcount here to atomically
* grab the points we are checking with. * grab the points we are checking with.
*/ */
do { do {
@ -486,7 +489,7 @@ EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
* *
* To keep it NMI safe since we're accessing from tracing, we're not using a * To keep it NMI safe since we're accessing from tracing, we're not using a
* separate timekeeper with updates to monotonic clock and boot offset * separate timekeeper with updates to monotonic clock and boot offset
* protected with seqlocks. This has the following minor side effects: * protected with seqcounts. This has the following minor side effects:
* *
* (1) Its possible that a timestamp be taken after the boot offset is updated * (1) Its possible that a timestamp be taken after the boot offset is updated
* but before the timekeeper is updated. If this happens, the new boot offset * but before the timekeeper is updated. If this happens, the new boot offset