Re: [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP
From: Magnus Lindholm
Date: Sat Dec 05 2026 - 19:30:41 EST
Hi Matt,
On Sat, Dec 5, 2026 at 9:38 PM Matt Turner <mattst88@xxxxxxxxx> wrote:
>
> The rpcc clocksource is registered only when the HWRPB lists a single
> processor, because the CPUs' cycle counters are not synchronized. SMP
> machines therefore fall back to the jiffies clocksource, with a
> resolution of one tick.
>
> The counters cannot be synchronized directly. Firmware zeroes each CPU's
> counter separately, in PALcode reset initialization when that CPU starts,
> and only PALcode can write CC_CTL. The Marvel firmware's Tru64 PALcode
> never writes it and offers no CSERVE function that does; Tru64 itself
> never corrects the counters, because it does not use them to keep time.
> On an ES47 with two 1.3 GHz EV7s the counters differ by about half a
> second.
>
> Instead, keep a per-CPU offset that the clocksource read adds to the
> counter, the way the parisc cr16 patch under review does, and measure
> each CPU's offset against a reference CPU before the clocksource is
> registered. The measurement is a ping-pong over a shared cache line with
> interrupts disabled on both CPUs: the target CPU reads its counter, asks
> the reference CPU for a timestamp, and reads its counter again when the
> reply arrives. The reply is assumed to have been taken halfway through
> the round trip, and the round with the shortest round trip out of 64
> wins. The counters are 32 bits wide, so all of this is modulo 2^32.
>
> Measure once, at boot. On the ES47, measurements repeated every minute
> for 20 minutes differed by at most 275 cycles with no trend, so the
> counters do not drift apart. Alpha has no CPU hotplug, so no CPU needs
> measuring later.
>
> An offset cannot correct counters that run at different rates. From
> revision 13 of the HWRPB, each CPU's slot can give its own cycle counter
> frequency (Tru64 reads it there); keep the jiffies clocksource if any two
> differ.
>
> Signed-off-by: Matt Turner <mattst88@xxxxxxxxx>
> ---
> arch/alpha/include/asm/hwrpb.h | 1 +
> arch/alpha/kernel/time.c | 203 +++++++++++++++++++++++++++++++++++++++--
> 2 files changed, 194 insertions(+), 10 deletions(-)
>
> diff --git a/arch/alpha/include/asm/hwrpb.h b/arch/alpha/include/asm/hwrpb.h
> index db831cf8de10..5469c97b9733 100644
> --- a/arch/alpha/include/asm/hwrpb.h
> +++ b/arch/alpha/include/asm/hwrpb.h
> @@ -114,6 +114,7 @@ struct percpu_struct {
> unsigned long console_data_log_pa;
> unsigned long console_data_log_length;
> unsigned long bcache_info;
> + unsigned long cycle_freq; /* HWRPB revision > 12 */
> };
>
> struct procdesc_struct {
> diff --git a/arch/alpha/kernel/time.c b/arch/alpha/kernel/time.c
> index 7a5aa0ce5ac5..fd797e42d243 100644
> --- a/arch/alpha/kernel/time.c
> +++ b/arch/alpha/kernel/time.c
> @@ -44,6 +44,8 @@
> #include <linux/timex.h>
> #include <linux/clocksource.h>
> #include <linux/clockchips.h>
> +#include <linux/percpu.h>
> +#include <linux/smp.h>
>
> #include "proto.h"
> #include "irq_impl.h"
> @@ -249,20 +251,200 @@ common_init_rtc(void)
> /*
> * The RPCC as a clocksource primitive.
> *
> - * While we have free-running timecounters running on all CPUs, and we make
> - * a half-hearted attempt in init_rtc_rpcc_info to sync the timecounter
> - * with the wall clock, that initialization isn't kept up-to-date across
> - * different time counters in SMP mode. Therefore we can only use this
> - * method when there's only one CPU enabled.
> + * Firmware zeroes each CPU's cycle counter separately, when the CPU starts,
> + * and only PALcode can write it, so on SMP the counters of different CPUs
> + * disagree. Keep a per-CPU offset that aligns each CPU's counter with a
> + * reference CPU's, measured once before the clocksource is registered.
> + * Only the clocksource applies it.
> *
> * When using the WTINT PALcall, the RPCC may shift to a lower frequency,
> * or stop altogether, while waiting for the interrupt. Therefore we cannot
> * use this method when the PALcode implements WTINT.
> */
>
> +#ifdef CONFIG_SMP
> +static DEFINE_PER_CPU(u32, rpcc_offset);
> +
> +static inline u32 rpcc_this_cpu_offset(void)
> +{
> + return __this_cpu_read(rpcc_offset);
> +}
> +
> +#define RPCC_SYNC_ROUNDS 64
> +
> +/*
> + * The target CPU asks for a timestamp by setting @go, and the reference CPU
> + * replies with its adjusted counter in @ref_time (never 0). The target takes
> + * the reply as having been read halfway through its round trip and keeps the
> + * round with the shortest round trip.
> + *
> + * Each flag is set with a release store and polled with an acquire load, so
> + * that a CPU clearing @go or @ref_time for the next round, or writing its
> + * new offset before @done, is seen to do so before the flag it then sets.
> + *
> + * The counters are 32 bits wide; all arithmetic on them is modulo 2^32.
> + */
> +static struct {
> + unsigned long go;
> + unsigned long ref_time;
> + unsigned long done;
> + unsigned long rtt;
> +} rpcc_sync ____cacheline_aligned_in_smp;
> +
> +static void __init rpcc_sync_target(void *unused)
> +{
> + u32 t0, t1, tm, rtt;
> + u32 best_rtt = U32_MAX, best_mid = 0, best_tm = 0;
> + int i;
> +
> + for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
> + t0 = rpcc();
> + /* Orders the clearing of @ref_time before the request. */
> + smp_store_release(&rpcc_sync.go, 1);
> + /* Pairs with the release of @ref_time in rpcc_sync_cpu(). */
> + while (!(tm = smp_load_acquire(&rpcc_sync.ref_time)))
> + cpu_relax();
> + /* Do not let the read of the counter pass the load above. */
> + mb();
> + t1 = rpcc();
> + WRITE_ONCE(rpcc_sync.ref_time, 0);
> +
> + rtt = t1 - t0;
> + if (rtt < best_rtt) {
> + best_rtt = rtt;
> + best_mid = t0 + rtt / 2;
> + best_tm = tm;
> + }
> + }
> +
> + rpcc_sync.rtt = best_rtt;
> + __this_cpu_write(rpcc_offset, best_tm - best_mid);
> +
> + /* Publishes the results above; pairs with rpcc_sync_cpu(). */
> + smp_store_release(&rpcc_sync.done, 1);
> +}
> +
> +static void __init rpcc_sync_wait(unsigned long *flag)
> +{
> + /* Pairs with the release of @flag in rpcc_sync_target(). */
> + while (!smp_load_acquire(flag))
> + cpu_relax();
> +}
> +
> +/* Align @cpu's counter with this CPU's. Called with migration disabled. */
> +static void __init rpcc_sync_cpu(unsigned int cpu)
> +{
> + unsigned long flags;
> + u32 tm;
> + int i;
> +
> + rpcc_sync.go = 0;
> + rpcc_sync.ref_time = 0;
> + rpcc_sync.done = 0;
> +
> + preempt_disable();
> + if (smp_call_function_single(cpu, rpcc_sync_target, NULL, 0)) {
> + preempt_enable();
> + return;
> + }
> +
> + /*
> + * Wait for the first request with interrupts enabled: the target CPU
> + * may be waiting for this one to answer an IPI before it can run
> + * rpcc_sync_target(). That only lengthens the first round trip.
> + */
> + rpcc_sync_wait(&rpcc_sync.go);
> + local_irq_save(flags);
> + for (i = 0; i < RPCC_SYNC_ROUNDS; i++) {
> + if (i)
> + rpcc_sync_wait(&rpcc_sync.go);
> + WRITE_ONCE(rpcc_sync.go, 0);
> + /* Do not let the read of the counter pass the load above. */
> + mb();
> + tm = rpcc() + rpcc_this_cpu_offset();
> + /* Orders the clearing of @go before the reply. */
> + smp_store_release(&rpcc_sync.ref_time, tm ?: 1);
> + }
> + local_irq_restore(flags);
> +
> + rpcc_sync_wait(&rpcc_sync.done);
> + preempt_enable();
> +
> + pr_info("rpcc: CPU%u offset %d cycles (reference CPU%u, round trip %lu cycles)\n",
> + cpu, (s32)per_cpu(rpcc_offset, cpu), smp_processor_id(),
> + rpcc_sync.rtt);
> +}
> +
> +/*
> + * An offset corrects only counters that run at the same rate. From HWRPB
> + * revision 13 on, each CPU's slot may give its own cycle counter frequency.
> + */
> +static bool __init rpcc_same_freq(void)
> +{
> + unsigned long freq = 0;
> + unsigned int cpu;
> +
> + if (hwrpb->revision <= 12)
> + return true;
> +
> + for_each_online_cpu(cpu) {
> + struct percpu_struct *slot = (struct percpu_struct *)
> + ((char *)hwrpb + hwrpb->processor_offset
> + + cpu * hwrpb->processor_size);
> +
> + if (!slot->cycle_freq)
> + continue;
Table 26-4 of the Alpha Architecture Reference Manual says a zero
per-CPU frequency means to use HWRPB+112. If one CPU inherits that
frequency and another reports a different one explicitly, skipping
the zero entry makes rpcc_same_freq() accept the pair.
Could we compare slot->cycle_freq ?: hwrpb->cycle_freq for each
online CPU instead? A fixed offset can't correct that rate difference.
Thanks,
Magnus
> + if (freq && slot->cycle_freq != freq) {
> + pr_info("rpcc: CPU%u counts at %lu Hz, not %lu Hz\n",
> + cpu, slot->cycle_freq, freq);
> + return false;
> + }
> + freq = slot->cycle_freq;
> + }
> +
> + return true;
> +}
> +
> +static bool __init rpcc_sync_init(void)
> +{
> + unsigned int cpu;
> +
> + if (num_online_cpus() == 1)
> + return true;
> +
> + if (!rpcc_same_freq())
> + return false;
> +
> + migrate_disable();
> + for_each_online_cpu(cpu) {
> + if (cpu != smp_processor_id())
> + rpcc_sync_cpu(cpu);
> + }
> + migrate_enable();
> +
> + return true;
> +}
> +#else
> +static inline u32 rpcc_this_cpu_offset(void)
> +{
> + return 0;
> +}
> +
> +static inline bool rpcc_sync_init(void)
> +{
> + return true;
> +}
> +#endif /* CONFIG_SMP */
> +
> static u64 read_rpcc(struct clocksource *cs)
> {
> - return rpcc();
> + u32 now;
> +
> + preempt_disable_notrace();
> + now = rpcc() + rpcc_this_cpu_offset();
> + preempt_enable_notrace();
> +
> + return now;
> }
>
> static struct clocksource clocksource_rpcc = {
> @@ -444,7 +626,8 @@ time_init(void)
>
> /*
> * Registered from an initcall rather than time_init(), which runs with
> - * interrupts disabled, too early to probe for WTINT.
> + * interrupts disabled, too early to probe for WTINT, and before the
> + * secondary CPUs are up to have their counters measured.
> */
> static int __init init_rpcc_clocksource(void)
> {
> @@ -453,9 +636,6 @@ static int __init init_rpcc_clocksource(void)
> return 0;
>
> /* See above for restrictions on using clocksource_rpcc. */
> - if (hwrpb->nr_processors != 1)
> - return 0;
> -
> if (IS_ENABLED(CONFIG_ALPHA_WTINT)) {
> /* Returns at the next interrupt, or traps if unimplemented. */
> wtint(0);
> @@ -463,6 +643,9 @@ static int __init init_rpcc_clocksource(void)
> return 0;
> }
>
> + if (!rpcc_sync_init())
> + return 0;
> +
> clocksource_register_hz(&clocksource_rpcc, rpcc_freq);
>
> return 0;
>
> --
> 2.56.1
>