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
87 return kvm_clock_read();87 return kvm_clock_read();
88}88}
89 89
90static 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
90static noinstr u64 kvm_sched_clock_read(void)111static noinstr u64 kvm_sched_clock_read(void)
91{112{
92 return pvclock_clocksource_read_nowd(this_cpu_pvti()) - kvm_sched_clock_offset;113 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
156}177}
157 178
158static struct clocksource kvm_clock = {179static struct clocksource kvm_clock = {
159 .name = "kvm-clock",180 .name = "kvm-clock",
160 .read = kvm_clock_get_cycles,181 .read = kvm_clock_get_cycles,
161 .rating = 400,182 .read_snapshot = kvm_clock_get_cycles_snapshot,
162 .mask = CLOCKSOURCE_MASK(64),183 .rating = 400,
163 .flags = CLOCK_SOURCE_IS_CONTINUOUS,184 .mask = CLOCKSOURCE_MASK(64),
164 .id = CSID_X86_KVM_CLK,185 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
165 .enable = kvm_cs_enable,186 .id = CSID_X86_KVM_CLK,
187 .enable = kvm_cs_enable,
166};188};
167 189
168static void kvm_register_clock(char *txt)190static 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