From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796503136; b=uB9KikQ4zqide2AtNRQwvNUYsrXPZFbuw8TZZ7WXWausdeEODPMoHSGYMvdNo7psW8z4 a1akNixD7sMLJEm0Yj7YFIWG9n9S7KWNZ0gT5edFnBOrpNzVxtBGVROfvuS8n6HEE1fvE vEHjJyO8j3bfpzcUeOCoU4rpBmLTcOV/M/Vi71egxqpYhPzHSCuurDYwxvt+XPVfOxhvB GTC/0O9KC5nEd7jiUMd9JnWQ2uIZLsIo7R0kaPDRuLu0p2G7WoCCr2ve2BgCMmPsI0n7D QzAfQ8zfA/nBDF57oAjrMLVTfQuJ6OY3cCznhbu/auh/jtEdFkNd+HuRfx2aLJlelpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796503136; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Type:X-Mailer:X-Developer-Signature: X-Developer-Key:Content-Transfer-Encoding; bh=IszBxnOrJt+3HmlvwonT8oQnEfGq7/Vlt74+J6ct8IM=; b=iX6cOcTCnIyPNiVPtb6kl4LH5drJIXsSD6ACtf/h5NMxWDHn0on/88RkAnRPN6lalt41 dkdVXqhlbOLXp1gTjNWtBfGezqhqRJ83rxkc11tkjRlKI7guqpWqrb70VXOqHokMcZ5BY XmUl8LBTR49hYDc5lzz3PcJ1UyjpWVhaHbsbLFugI/EtPTs+sq49p/Dij41UjJ5ZNnyJw t1DNUYY1i9bOrFgN18lnpkFCkQ3FkbKHyUbHEC5s/xXurHLAnNTTzMePlQJKB+vCJTwxU S5PC1yc0ATnNDheySK4MBIMhCB4exd0yK7fcZ1jb3Gj1wywp5F9z1BK/uK/HTCBz40g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZjQzlNO6; arc=none smtp.remote-ip=74.125.224.51 Received: from mail-yx1-f51.google.com (mail-yx1-f51.google.com [74.125.224.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4CDD606A33A for ; Sat, 5 Dec 2026 20:38:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D4CDD606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZjQzlNO6 Received: by mail-yx1-f51.google.com with SMTP id 956f58d0204a3-67da69e7c31so2210451d50.3 for ; Sat, 05 Dec 2026 12:38:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796503135; x=1797107935; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=IszBxnOrJt+3HmlvwonT8oQnEfGq7/Vlt74+J6ct8IM=; b=ZjQzlNO6IkPOLpdlidU5TnsBOf7hVsqnrRPvbuJyEN8Vb+TczO6xTug2OFAj6Q0WSQ mk6p8VBGbRPiOLcPY/iUqaRdY67mjhaA6TDckA3WW5zQWWhRcvwkLjfqAa1BiB4f5oMo t1vuuakt3wVria1nILqZc5rgFC673X2mcTA5NBqIMcA1DGl3JDcxlzHuNcPjj+aBdVdd UaXRDvV4JvKCnURgCuxO9lEeDHLzTD+nBe/gpqCceHlviQ0u8qP2bSRR9Zn+ZhKYmV8X OqnvFHcY40kquyes+n021AZYM3yvyD4vVTRNj9RYzmj6f1lo6ZLX8TyeNkOptfmY6OgA gHpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796503135; x=1797107935; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=IszBxnOrJt+3HmlvwonT8oQnEfGq7/Vlt74+J6ct8IM=; b=0LKPtioykgaus1nzne1iRmFR27sh+9kO8d1X19oqum1O2oKdly9OOPo00gO9aY3ush ss97nogc7Vad1GMMi3zjoOWf+tpGW7ePNdkWQbBZ6aciE4kLMJItHlbBBLS3iipp784a VXPJlCuP6n2fULd71Gua+wr+mdnDLyO0vTF8542npXaEI5SaRLPbYLmLcUUYxH3OyIvD SNeX6D1ExDFduKLVT6tB3HX1vHOZ2pfwK7S8J+2qUwuYAT87pw0Y+yYf2M1WVB8wuXkN aDH2T83y45EzHfmTYdiFQBfNsBCb02SlmYGDMr9rI1uuU7UJPAZTpCegty/lTGdUR9QN oJNA== X-Forwarded-Encrypted: i=1; AL+hErH94WdEyVl1+Lg14H50AV0farZdimCIlL+buavEA1aSxpc7Vpuc5JWIe7YeyPETf182aBYyqMEYh+eKh90=@vger.kernel.org X-Gm-Message-State: AB4q+uHUYII+Sqf9nqw/T4IzwUs/yZtRJ28upWMJwgR82xI1OjuQHsRv x85LtKbj+Kibt2NT0B33PXF29iYEINoy7txmZ5AH6AL1/z1iShPbl/Hz X-Gm-Gg: AZFdTZfc9hsxaCNwIMWoPusbsZpXWEZEHwf02Nw2hvWccldvh8wFDfhuGzO2+LcjCOl 7U77kEBiGOkllnF2oYYFPvWJF/bLfw9ubWsbpL3YjshbOBVxl4bCv/g+n4HocMPsQq35DZuSJXW 03VwfahTs3lIrK/KmBKv/I+KEJbWt3pLlmmbkYdMbl5P4Q7VM4/w0OoHj0wHG0t6orXHS0AQidk KiLyuV49NJem33oQMh5SHqsqM6TKLvXs4XUjdVVH3spJZIPZYlzEWt7b+2Vrxa514XJ53yXGlyo 1G5XUFscPxoxOmLFh2qovnVT+7lIiEi70eQAxId3FLwxVNEmVlloqQn0StSSzoEgUE9l9I0mGme 4EtFm9jJiAGx1vlNI1rK/q7/XyGhxAU8c/iaKNfBkxF1yC/HUr/XL5APlyPChWN4xhhGYoy/Aa/ LCyfkUw/3V4GO+YvpOti+t3Qpfj+LQtpajVmeYGMi1i/p0+p2WwfJrn0F/s5s5NDUmAWDNyUuQE iAs5M6h4Jl8JUk/GM8dFHM1GETlfd5zTIQSEFS2gtZHH73k5UT37xz8jh88XZoIt7MGgp+4RLE9 fPJp+2OlmEalzc9RfdHqLKN1z25UmcBBbq6wIZIKtFLuekKNyuY7XVQ0DnwyfrGk/w2sTYmMR4W v86le5kS6YKSazlpVIW6W06F4Xp/wCwtYBWY3NBnbg5Mld2XuASPwHAzZCz3isfAuyU1fbR0d+l IJzI9WJ7cIio79SyJXlg== X-Received: by 2002:a05:690e:440e:b0:67b:f621:8d4c with SMTP id 956f58d0204a3-67f7242d122mr2100542d50.25.1796503134680; Sat, 05 Dec 2026 12:38:54 -0800 (PST) Received: from localhost (107-220-129-194.lightspeed.chrlnc.sbcglobal.net. [107.220.129.194]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0c08479asm2713606d50.8.2026.12.05.12.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 12:38:52 -0800 (PST) From: Matt Turner To: Richard Henderson , Magnus Lindholm Cc: linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] alpha: Synchronize rpcc across CPUs on SMP Date: Sat, 5 Dec 2026 15:38:49 -0500 Message-ID: <20261205-alpha-rpcc-smp-v1-2-aae5b79e7bac@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261205-alpha-rpcc-smp-v1-0-084c39baee43@gmail.com> References: <20261205-alpha-rpcc-smp-v1-0-084c39baee43@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9637; i=mattst88@gmail.com; h=from:subject:message-id; bh=lRshFhMvgmMWxD/S3EsaZb1h/BpRelhLpMcTSVhWpGo=; b=owGbwMvMwCW25rVmCc8sv+mMp9WSGLJFyhwOZqtteHj2ZNadaXfEpps+aGONKuq3mit0qDrow lOff955HR9ZGMS4GGaKKbLErVdkmdW2Y6nPaelfMHNYmUCGSIs0MAABCwNfbmJeqZGOkZ6ptqGe IZChYxQPkdNj0MgsLi5NLdJNKyhyyMsvSSzJzM8r1ssvSM0rSC/QS8tMK8nIyC8qTgUaoZeXWmL q6uhmZGhgYuloYeZk4Whq4uzsZOjk5ujo7OpkZGluYuBs6WjiamnOwMUpAHONjSkjwyGeqB9r7j 7tjpqie0Ij9ONJ3Vmix1+qam0T2sB5KXHeFhOG/3FPMmtnfGHbyqcuq1FVdzvp78+0sCer3xZLn jg0uaZDhgsA X-Developer-Key: i=mattst88@gmail.com; a=openpgp; fpr=3BB639E56F861FA2E86505690FDD682D974CA72A Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include +#include #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; + 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