History

x86/kvmclock: Implement read_snapshot() for kvmclock clocksource

Implement the read_snapshot() callback for the kvmclock clocksource. This returns the kvmclock nanosecond value (for timekeeping) while also providing the raw TSC value that was used to compute it. The TSC is read inside the pvclock seqlock-protected region, ensuring the raw TSC and derived kvmclock value are atomically paired. This enables ktime_get_snapshot_id() to provide the raw TSC to consumers like the vmclock PTP driver, which currently has to do a separate call to get_cycles() to obtain a value at *approximately* the same time, to feed through the vmclock calculation.

David Woodhouse committed 5 months ago

Commit 19fa3e51 parent c51100f1 file · +29 −7Browse files

Unified · Split

arch/x86/kernel/kvmclock.c+29−7
@@ -87,6 +87,27 @@ static u64 kvm_clock_get_cycles(struct clocksource *cs) · 86 unmodified lines
8787 return kvm_clock_read();
8888 }
8989
90+static u64 kvm_clock_get_cycles_snapshot(struct clocksource *cs,
91+ struct clocksource_hw_snapshot *chs)
92+{
93+ struct pvclock_vcpu_time_info *src;
94+ unsigned version;
95+ u64 ret, tsc;
96+
97+ preempt_disable_notrace();
98+ src = this_cpu_pvti();
99+ do {
100+ version = pvclock_read_begin(src);
101+ tsc = rdtsc_ordered();
102+ ret = __pvclock_read_cycles(src, tsc);
103+ } while (pvclock_read_retry(src, version));
104+ preempt_enable_notrace();
105+
106+ chs->hw_cycles = tsc;
107+ chs->hw_csid = CSID_X86_TSC;
108+ return ret;
109+}
110+
90111 static noinstr u64 kvm_sched_clock_read(void)
91112 {
92113 return pvclock_clocksource_read_nowd(this_cpu_pvti()) - kvm_sched_clock_offset;
@@ -156,13 +177,14 @@ static int kvm_cs_enable(struct clocksource *cs) · 63 unmodified lines
156177 }
157178
158179 static struct clocksource kvm_clock = {
159− .name = "kvm-clock",
160− .read = kvm_clock_get_cycles,
161− .rating = 400,
162− .mask = CLOCKSOURCE_MASK(64),
163− .flags = CLOCK_SOURCE_IS_CONTINUOUS,
164− .id = CSID_X86_KVM_CLK,
165− .enable = kvm_cs_enable,
180+ .name = "kvm-clock",
181+ .read = kvm_clock_get_cycles,
182+ .read_snapshot = kvm_clock_get_cycles_snapshot,
183+ .rating = 400,
184+ .mask = CLOCKSOURCE_MASK(64),
185+ .flags = CLOCK_SOURCE_IS_CONTINUOUS,
186+ .id = CSID_X86_KVM_CLK,
187+ .enable = kvm_cs_enable,
166188 };
167189
168190 static void kvm_register_clock(char *txt)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t