Re: [RFC PATCH v3 10/10] kernel/time: Add /dev/vmclock_host miscdev
From: Wen Gu
Date: Thu May 21 2026 - 02:30:16 EST
On 2026/5/20 21:33, David Woodhouse wrote:
+/*
+ * Called from pvclock_gtod_notify on every timekeeping update.
+ * Only does real work when ntp_tick or skew_delta changes.
+ */
+static int vmclock_host_notify(struct notifier_block *nb,
+ unsigned long was_set, void *data)
+{
<...>
+
+ /* Compute time tuple: C = A + ntp_error + time_offset */
+ ns = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
+ sec = tk->xtime_sec + tk->tai_offset;
+ ns += tk->ntp_error >> (tk->tkr_mono.shift + tk->ntp_error_shift);
+ ns += ntp_get_time_offset_ns(tk->id);
+
+ while (ns < 0) {
+ ns += NSEC_PER_SEC;
+ sec--;
+ }
+ while (ns >= NSEC_PER_SEC) {
+ ns -= NSEC_PER_SEC;
+ sec++;
+ }
+
+ counter_value = tk->tkr_mono.cycle_last;
+ hi = div64_u64_rem((u64)ns << 32, 1000000000ULL, &rem);
+ time_frac = (hi << 32) | div64_u64(rem << 32, 1000000000ULL);
+
+ clock_status = !(ntp_get_status() & STA_UNSYNC) ?
+ VMCLOCK_STATUS_SYNCHRONIZED : VMCLOCK_STATUS_FREERUNNING;
+
+ /* Prepare le values */
+ le_counter_value = cpu_to_le64(counter_value);
+ le_time_sec = cpu_to_le64(sec);
+ le_time_frac = cpu_to_le64(time_frac);
+ le_period_frac = cpu_to_le64(cached_period_frac);
+ period_shift = cached_period_shift;
+
+ /* Update page under seqcount */
+ WRITE_ONCE(clk->seq_count, cpu_to_le32(
+ le32_to_cpu(READ_ONCE(clk->seq_count)) + 1));
+ smp_wmb();
+
+ clk->counter_id = counter_id;
+ clk->counter_value = le_counter_value;
+ clk->time_sec = le_time_sec;
+ clk->time_frac_sec = le_time_frac;
+ if (period_changed) {
+ clk->counter_period_frac_sec = le_period_frac;
+ clk->counter_period_shift = period_shift;
+ }
+ clk->clock_status = clock_status;
+
+ /* Set leap second indicator from NTP time_state */
+ switch (ntp_get_time_state()) {
+ case TIME_INS:
+ clk->leap_indicator = VMCLOCK_LEAP_PRE_POS;
+ break;
+ case TIME_DEL:
+ clk->leap_indicator = VMCLOCK_LEAP_PRE_NEG;
+ break;
+ case TIME_OOP:
+ clk->leap_indicator = VMCLOCK_LEAP_POS;
+ break;
+ case TIME_WAIT:
+ clk->leap_indicator = (ntp_get_status() & STA_DEL) ?
+ VMCLOCK_LEAP_POST_NEG : VMCLOCK_LEAP_POST_POS;
+ break;
+ default:
+ clk->leap_indicator = VMCLOCK_LEAP_NONE;
+ break;
+ }
+
+ /* Export as TAI if tai_offset is known, otherwise UTC */
+ if (tk->tai_offset) {
+ clk->time_type = VMCLOCK_TIME_TAI;
+ clk->tai_offset_sec = cpu_to_le16((s16)tk->tai_offset);
I think this should be cpu_to_le16(-(s16)tk->tai_offset)?
vmclock_set_tk_reference() in Patch 9 treats clk->tai_offset_sec as
a (UTC - TAI) offset, so the sign here probably needs to be inverted.
+ clk->flags = cpu_to_le64(VMCLOCK_FLAG_TAI_OFFSET_VALID |
+ VMCLOCK_FLAG_TIME_MONOTONIC |
+ VMCLOCK_FLAG_NOTIFICATION_PRESENT);
+ } else {
+ clk->time_type = VMCLOCK_TIME_UTC;
+ clk->tai_offset_sec = 0;
+ clk->flags = cpu_to_le64(VMCLOCK_FLAG_TIME_MONOTONIC |
+ VMCLOCK_FLAG_NOTIFICATION_PRESENT);
+ }
+
+ smp_wmb();
+ WRITE_ONCE(clk->seq_count, cpu_to_le32(
+ le32_to_cpu(READ_ONCE(clk->seq_count)) + 1));
+
+ wake_up_interruptible(&vmclock_wait);
+ return NOTIFY_DONE;
+}