[PATCH v2 3/5] um: time: use the host TSC as clocksource for the vDSO

Haikal Firmansyah haikalfirmansyh.kernel at gmail.com
Wed Nov 25 01:37:57 PST 2026


Calibrate the host TSC at boot (median of 5 rounds over 100 ms
windows against os_nsecs()) and switch the timekeeper clocksource
from os_nsecs() to the raw TSC when calibration succeeds, setting
vdso_clock_mode to VDSO_CLOCKMODE_TSC so the generic vDSO can
interpolate time in userspace.  Under time-travel the guest clock
is independent of the host clock, so calibration is skipped and
the old os_nsecs() clocksource is kept; on calibration failure
the same fallback applies, and the vDSO falls back to syscalls.
Measured: clock_gettime() median 70 ns in the guest (80 ns in
the boot where the syscall baseline was measured) vs 36,535 ns
via the raw syscall: about 457x faster same-boot, about 520x at
the typical median.

Signed-off-by: Haikal Firmansyah <haikalfirmansyh.kernel at gmail.com>
---
 arch/um/kernel/time.c | 99 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)

diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
index d620b6d63..0f32a02a1 100644
--- a/arch/um/kernel/time.c
+++ b/arch/um/kernel/time.c
@@ -881,6 +881,80 @@ static irqreturn_t um_timer(int irq, void *dev)
 	return IRQ_HANDLED;
 }

+/*
+ * TSC-based clocksource.
+ *
+ * The guest userspace can read the host TSC with RDTSC without a trap
+ * (verified by measurement in the guest: no fault, 0 monotonic
+ * decreases, skew p50 ~100 ns), so the timekeeper clocksource is
+ * switched from os_nsecs()/256 to the raw TSC. update_vsyscall()
+ * then publishes cycle_last/mult/shift for this clocksource into the
+ * vvar, and the generic vDSO interpolates the time from the TSC in
+ * userspace.
+ *
+ * The TSC frequency is calibrated at boot against os_nsecs() (host
+ * CLOCK_MONOTONIC). If calibration fails or time-travel is active,
+ * um_tsc_ok stays false and the old os_nsecs() clocksource is kept,
+ * with vdso_clock_mode NONE so the vDSO falls back to syscalls.
+ */
+static __always_inline u64 um_rdtsc(void)
+{
+	u32 lo, hi;
+
+	asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
+	return ((u64)hi << 32) | lo;
+}
+
+static bool um_tsc_ok;
+static u64 um_tsc_hz;
+
+#define UM_TSC_CAL_ROUNDS	5
+#define UM_TSC_CAL_NS		100000000ULL /* 100 ms per round */
+
+static u64 __init um_calibrate_tsc(void)
+{
+	u64 freq[UM_TSC_CAL_ROUNDS];
+	u64 tmp;
+	int i, j;
+
+	if (time_travel_mode != TT_MODE_OFF)
+		return 0;
+
+	for (i = 0; i < UM_TSC_CAL_ROUNDS; i++) {
+		long long t0, t1;
+		u64 c0, c1;
+
+		t0 = os_nsecs();
+		c0 = um_rdtsc();
+		do {
+			t1 = os_nsecs();
+		} while (t1 - t0 < (long long)UM_TSC_CAL_NS);
+		c1 = um_rdtsc();
+		/* t1 is read after c1; the endpoint skew (~one os_nsecs()
+		 * call, < 1 us) is negligible over a 100 ms window.
+		 */
+		if (t1 <= t0 || c1 <= c0)
+			return 0;
+		freq[i] = (c1 - c0) * NSEC_PER_SEC / (u64)(t1 - t0);
+	}
+
+	/* Median of the rounds (robust against one jittered round). */
+	for (i = 0; i < UM_TSC_CAL_ROUNDS; i++)
+		for (j = i + 1; j < UM_TSC_CAL_ROUNDS; j++)
+			if (freq[j] < freq[i]) {
+				tmp = freq[i];
+				freq[i] = freq[j];
+				freq[j] = tmp;
+			}
+
+	/* Sanity: a plausible TSC rate (100 MHz .. 4 GHz, fits u32 Hz). */
+	if (freq[UM_TSC_CAL_ROUNDS / 2] < 100000000ULL ||
+	    freq[UM_TSC_CAL_ROUNDS / 2] > 4000000000ULL)
+		return 0;
+
+	return freq[UM_TSC_CAL_ROUNDS / 2];
+}
+
 static u64 timer_read(struct clocksource *cs)
 {
 	if (time_travel_mode != TT_MODE_OFF) {
@@ -902,6 +976,9 @@ static u64 timer_read(struct clocksource *cs)
 		return time_travel_time / TIMER_MULTIPLIER;
 	}

+	if (um_tsc_ok)
+		return um_rdtsc();
+
 	return os_nsecs() / TIMER_MULTIPLIER;
 }

@@ -945,11 +1022,33 @@ static void __init um_timer_init(void)
 		return;
 	}

+	/*
+	 * Calibrate the TSC before registering the clocksource, so the
+	 * timekeeper (and the vDSO) use the TSC rate from the first tick.
+	 * Under time-travel the guest clock is independent of the host
+	 * clock, so the host TSC cannot be used and calibration is
+	 * skipped. On failure keep the old os_nsecs() clocksource:
+	 * timer_read() falls back to it and vdso_clock_mode stays NONE.
+	 */
+	if (time_travel_mode == TT_MODE_OFF)
+		um_tsc_hz = um_calibrate_tsc();
+	if (um_tsc_hz) {
+		um_tsc_ok = true;
+		timer_clocksource.name = "tsc";
+		timer_clocksource.vdso_clock_mode = VDSO_CLOCKMODE_TSC;
+		pr_info("UML: TSC clocksource, frequency %llu Hz\n", um_tsc_hz);
+		err = clocksource_register_hz(&timer_clocksource,
+					      (u32)um_tsc_hz);
+		goto registered;
+	}
+	pr_info("UML: TSC unavailable, keeping os_nsecs() (no vDSO)\n");
 	err = clocksource_register_hz(&timer_clocksource,
NSEC_PER_SEC/TIMER_MULTIPLIER);
+registered:
 	if (err) {
 		printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
 		return;
 	}
+
 }

 void read_persistent_clock64(struct timespec64 *ts)
-- 
2.43.0



More information about the linux-um mailing list