[PATCH v1 6/8] x86/tsc: Use seqcount_latch_t

Ahmed S. Darwish
2020-08-27T15:12:20Z

Latch sequence counters have unique read and write APIs, and thus
seqcount_latch_t was recently introduced at seqlock.h.

Use that new data type instead of plain seqcount_t. This adds the
necessary type-safety and ensures that only latching-safe seqcount APIs
are to be used.

Signed-off-by: Ahmed S. Darwish <a.darwish@linutronix.de>
---
arch/x86/kernel/tsc.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 49d925043171..cbf17e9f1d03 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -54,7 +54,7 @@ struct clocksource *art_related_clocksource;

struct cyc2ns {
struct cyc2ns_data data[2]; /* 0 + 2*16 = 32 */
- seqcount_t seq; /* 32 + 4 = 36 */
+ seqcount_latch_t seq; /* 32 + 4 = 36 */

}; /* fits one cacheline */

@@ -68,19 +68,21 @@ early_param("tsc_early_khz", tsc_early_khz_setup);

__always_inline void cyc2ns_read_begin(struct cyc2ns_data *data)
{
+ seqcount_latch_t *seqcount;
int seq, idx;

preempt_disable_notrace();

+ seqcount = &this_cpu_ptr(&cyc2ns)->seq;
do {
- seq = this_cpu_read(cyc2ns.seq.sequence);
+ seq = raw_read_seqcount_latch(seqcount);
idx = seq & 1;

data->cyc2ns_offset = this_cpu_read(cyc2ns.data[idx].cyc2ns_offset);
data->cyc2ns_mul = this_cpu_read(cyc2ns.data[idx].cyc2ns_mul);
data->cyc2ns_shift = this_cpu_read(cyc2ns.data[idx].cyc2ns_shift);

- } while (unlikely(seq != this_cpu_read(cyc2ns.seq.sequence)));
+ } while (read_seqcount_latch_retry(seqcount, seq));
}

__always_inline void cyc2ns_read_end(void)
@@ -186,7 +188,7 @@ static void __init cyc2ns_init_boot_cpu(void)
{
struct cyc2ns *c2n = this_cpu_ptr(&cyc2ns);

- seqcount_init(&c2n->seq);
+ seqcount_latch_init(&c2n->seq);
__set_cyc2ns_scale(tsc_khz, smp_processor_id(), rdtsc());
}

@@ -203,7 +205,7 @@ static void __init cyc2ns_init_secondary_cpus(void)

for_each_possible_cpu(cpu) {
if (cpu != this_cpu) {
- seqcount_init(&c2n->seq);
+ seqcount_latch_init(&c2n->seq);
c2n = per_cpu_ptr(&cyc2ns, cpu);
c2n->data[0] = data[0];
c2n->data[1] = data[1];
--
2.28.0