Skip to contentSkip to conversation

KVM: x86: get rid of pvclock_gtod_copy

PATCH In review Denis Plotnikov 3 participants 17 messages Aug 30, 2017 – Sep 27, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v5 cover KVM: x86: get rid of pvclock_gtod_copy

V5:
It was decided to split the series
"make L2's kvm-clock stable, get rid of pvclock_gtod_copy in KVM" into two parts to make the changes finer grained:
1. get rid of pvclock_gtod_copy in KVM
2. make L2's kvm-clock stable
This V5 series implements the 1st part. This part gets rid of pvclock_gtod_copy by switching to use the timekeeper functionality being extended. This new scheme doesn't add any new notifiers and keeps using the existing one (see patch 6 description) which had been used by the pvclock_gtod_copy to keep track timekeeper data changes.

The 2nd part is being discussed and is going to be sent later on.

V4:
* removed "is stable" function with vague definition of stability there is the only function which does time with cycle stamp getting
* some variables renamed
* some patches split into smaller once
* atomic64_t usage is replaced with atomic_t

V3:
Changing the timekeeper interface for clocksource reading looks like an overkill to achive the goal of getting cycles stamp for KVM. Instead extend the timekeeping interface and add functions which provide necessary data: read clocksource with cycles stamp, check whether the clock source is stable.

Use those functions and improve existing timekeeper functionality to replace pvclock_gtod_copy scheme in masterclock data calculation.

V2:
The main goal is to make L2 kvm-clock be stable when it's running over L1 with stable kvm-clock.

The patch series is for x86 architecture only. If the series is approved I'll do changes for other architectures but I don't have an ability to compile and check for every single on (help needed)

The patch series do the following:

* change timekeeper interface to get cycles stamp value from the timekeeper
* get rid of pvclock copy in KVM by using the changed timekeeper
interface: get time and cycles right from the timekeeper
* make KVM recognize a stable kvm-clock as stable clocksource and use the KVM masterclock in this case, which means making L2 stable when running over stable L1 kvm-clock

Denis Plotnikov (6):
timekeeper: introduce extended clocksource reading callback
timekeeper: introduce boot field in system_time_snapshot
timekeeper: use the extended reading function on snapshot acquiring
tsc: implement the extended tsc reading function
KVM: x86: switch to masterclock update using timekeeper functionality
KVM: x86: remove not used pvclock_gtod_copy

 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kernel/tsc.c           |  10 ++
 arch/x86/kvm/trace.h            |  31 ++---
 arch/x86/kvm/x86.c              | 245 +++++++++-------------------------------
 include/linux/clocksource.h     |  11 +-
 include/linux/timekeeping.h     |   5 +
 kernel/time/timekeeping.c       |  17 ++-
 7 files changed, 104 insertions(+), 217 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v5 1/6 timekeeper: introduce extended clocksource reading callback

The callback provides extended information about just read clocksource value.

It's going to be used in cases when detailed system information needed for further time related values calculation, e.g in KVM masterclock settings calculation.

---
 include/linux/clocksource.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
include/linux/clocksource.h +10 -1
index a78cb18..786a522 100644
@@ -48,7 +48,14 @@ struct module;
  *			400-499: Perfect
  *				The ideal clocksource. A must-use where
  *				available.
- * @read:		returns a cycle value, passes clocksource as argument
+ * @read:		returns a cycle value (might be not quite cycles:
+ *			see pvclock) passes clocksource as argument
+ * @read_with_stamp:	saves cycles value (got from timekeeper) and cycles
+ *			stamp (got from hardware counter value and used by
+ *			timekeeper to calculate the cycles value) to
+ *			corresponding input pointers return true if cycles
+ *			stamp holds real cycles and false if it holds some
+ *			time derivative value
  * @enable:		optional function to enable the clocksource
  * @disable:		optional function to disable the clocksource
  * @mask:		bitmask for two's complement
@@ -78,6 +85,8 @@ struct module;
  */
 struct clocksource {
 	u64 (*read)(struct clocksource *cs);
+	bool (*read_with_stamp)(struct clocksource *cs,
+				u64 *cycles, u64 *cycles_stamp);
 	u64 mask;
 	u32 mult;
 	u32 shift;
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 19 lines · Denis Plotnikov

The callback provides extended information about just read clocksource value.

It's going to be used in cases when detailed system information needed for further time related values calculation, e.g in KVM masterclock settings calculation.

---
 include/linux/clocksource.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
include/linux/clocksource.h +0 -0
index a78cb18..786a522 100644
@@ -48,7 +48,14 @@ struct module;
  *			400-499: Perfect
  *				The ideal clocksource. A must-use where

include/linux/clocksource.h struct module; · line 58

Quoted 10 lines
include/linux/clocksource.h +8 -1
  *				available.
- * @read:		returns a cycle value, passes clocksource as argument
+ * @read:		returns a cycle value (might be not quite cycles:
+ *			see pvclock) passes clocksource as argument
+ * @read_with_stamp:	saves cycles value (got from timekeeper) and cycles
+ *			stamp (got from hardware counter value and used by
+ *			timekeeper to calculate the cycles value) to
+ *			corresponding input pointers return true if cycles
+ *			stamp holds real cycles and false if it holds some
+ *			time derivative value

Neither the changelog nor this comment make any sense. Not to talk about the actual TSC side implementation which does the same as tsc_read() - it actually uses tsc_read() - but stores the same value twice and unconditionally returns true.

I have no idea why you need this extra voodoo function if you can achieve the same thing with a simple property bit in clocksource->flags.

Neither do I understand the rest of the magic you introduce in the snapshot code:

include/linux/clocksource.h struct module;

Quoted 9 lines
include/linux/clocksource.h +9 -0
+               if (clock->read_with_stamp)
+                       systime_snapshot->cycles_valid =
+                               clock->read_with_stamp(
+                                       clock, &now, &systime_snapshot->cycles);
+               else {
+                       systime_snapshot->cycles_valid = false;
+                       now = clock->read(clock);
+                       systime_snapshot->cycles = now;
+               }

The only difference between the two code pathes is the effect on systime_snapshot->cycles_valid. The explanation of that bit is not making much sense either.

+ * @cycles_valid: The flag is true when @cycles contain actual
+ * number of cycles instead some cycle derivative

Why the heck would cycles be something different than what clock->read() returns?

What you really want to convey is the information whether

     now = tk_clock_read(&tk->tkr_mono);

is a value which you can use for your pvclock correlation or not.

Unless I'm missing something important all of this can be achieved with a minimal and actually understandable patch. See below.

Thanks,

tglx

8<------------------

arch/x86/kernel/tsc.c +2 -1
@@ -1045,7 +1045,8 @@ static struct clocksource clocksource_ts
 	.read                   = read_tsc,
 	.mask                   = CLOCKSOURCE_MASK(64),
 	.flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
-				  CLOCK_SOURCE_MUST_VERIFY,
+				  CLOCK_SOURCE_MUST_VERIFY |
+				  CLOCK_SOURCE_VALID_FOR_PVCLOCK_UPDATE,
 	.archdata               = { .vclock_mode = VCLOCK_TSC },
 	.resume			= tsc_resume,
 	.mark_unstable		= tsc_cs_mark_unstable,
include/linux/clocksource.h +3 -1
@@ -118,7 +118,9 @@ struct clocksource {
 #define CLOCK_SOURCE_VALID_FOR_HRES		0x20
 #define CLOCK_SOURCE_UNSTABLE			0x40
 #define CLOCK_SOURCE_SUSPEND_NONSTOP		0x80
-#define CLOCK_SOURCE_RESELECT			0x100
+#define CLOCK_SOURCE_VALID_FOR_PVCLOCK_UPDATE	0x100
+
+#define CLOCK_SOURCE_RESELECT			0x200
 
 /* simplify initialization of mask field */
 #define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
include/linux/timekeeping.h +3 -0
@@ -285,6 +285,8 @@ extern void ktime_get_raw_and_real_ts64(
  * @raw:	Monotonic raw system time
  * @clock_was_set_seq:	The sequence number of clock was set events
  * @cs_was_changed_seq:	The sequence number of clocksource change events
+ * @valid_for_pvclock_update: @cycles is from a clocksource which
+ *			       can be used for pvclock updates
  */
 struct system_time_snapshot {
 	u64		cycles;
@@ -292,6 +294,7 @@ struct system_time_snapshot {
 	ktime_t		raw;
 	unsigned int	clock_was_set_seq;
 	u8		cs_was_changed_seq;
+	bool		valid_for_pvclock_update;
 };
 
 /*
kernel/time/timekeeping.c +4 -1
@@ -948,7 +948,7 @@ time64_t __ktime_get_real_seconds(void)
 void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
-	unsigned long seq;
+	unsigned long seq, clk_flags;
 	ktime_t base_raw;
 	ktime_t base_real;
 	u64 nsec_raw;
@@ -960,6 +960,7 @@ void ktime_get_snapshot(struct system_ti
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
 		now = tk_clock_read(&tk->tkr_mono);
+		clk_flags = tk->tkr_mono.clock->flags;
 		systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
 		systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
 		base_real = ktime_add(tk->tkr_mono.base,
@@ -970,6 +971,8 @@ void ktime_get_snapshot(struct system_ti
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
 	systime_snapshot->cycles = now;
+	systime_snapshot->valid_for_pvclock_update =
+		clk_flags & CLOCK_SOURCE_VALID_FOR_PVCLOCK_UPDATE,
 	systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
 	systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
 }
maintainer
#3 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 7 lines · Thomas Gleixner
Quoted 6 lines

+ * @read_with_stamp: saves cycles value (got from timekeeper) and cycles
+ * stamp (got from hardware counter value and used by
+ * timekeeper to calculate the cycles value) to
+ * corresponding input pointers return true if cycles
+ * stamp holds real cycles and false if it holds some
+ * time derivative value

Quoted 7 lines

Neither the changelog nor this comment make any sense. Not to talk about the actual TSC side implementation which does the same as tsc_read() - it actually uses tsc_read() - but stores the same value twice and unconditionally returns true.

Unless I'm missing something important all of this can be achieved with a minimal and actually understandable patch. See below.

If that is acceptable, that's certainly okay for us too as a start, in order to clean up the KVM code.

*However*, I must once more ask for help understanding ktime_get_snapshot, otherwise there is no way that I can start using it in kvmclock.

In particular I'd like to understand the contract of ktime_get_snapshot, namely the meaning of the "cycles" field in struct system_time_snapshot.

Does it have to be system clock cycles, like TSC, or can it be just the value of the clock source? And if the latter, are the users broken, because they can receive any random clocksource output in ->cycles? It doesn't help that, for all of the users of ->cycles (which are all reached from get_device_system_crosststamp), the code that actually uses the member is never called in the current git master.

I asked these questions to John in the previous review, but got no answer. My understanding is that get_device_system_crosststamp is broken for !tsc clocksource. Because struct system_counterval_t must have a TSC value, history_begin needs to contain a cross-timestamp of the TSC (in ->cycles) and the clock (in ->real and ->raw). And if this cross-timestamp is not available, get_device_system_crosststamp needs to know, so that it doesn't use history_begin at all.

Now, such cross-timestamp functionality is exactly what "read_with_stamp" provides. Patch 3 also introduces "->cycles_valid" in struct system_time_snapshot, to report whether the cross-timestamp is available. In the case of the TSC clocksource, of course, the two values of course are the same, and always available (so the function always returns true).

However, if get_device_system_crosststamp ran with kvmclock or Hyper-V clocksource, the two values stored by read_with_stamp would be different, basically a (TSC, nanoseconds) pair. ktime_get_snapshot would then return the TSC in ->cycles, and use the nanoseconds value to compute ->real and ->raw. Because the cross timestamp is available, ->cycles_valid would be true.

So, if history_begin _is_ broken, after fixing it (with "read_with_stamp" or otherwise) ktime_get_snapshot would provide KVM with everything it needs. If not, I'd be completely off base, and I'd have to apologize to Denis for screwing up.

Paolo

#4 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 26 lines · Paolo Bonzini
Quoted 7 lines · Thomas Gleixner
Quoted 6 lines

+ * @read_with_stamp: saves cycles value (got from timekeeper) and cycles
+ * stamp (got from hardware counter value and used by
+ * timekeeper to calculate the cycles value) to
+ * corresponding input pointers return true if cycles
+ * stamp holds real cycles and false if it holds some
+ * time derivative value

Quoted 7 lines

Neither the changelog nor this comment make any sense. Not to talk about the actual TSC side implementation which does the same as tsc_read() - it actually uses tsc_read() - but stores the same value twice and unconditionally returns true.

Unless I'm missing something important all of this can be achieved with a minimal and actually understandable patch. See below.

If that is acceptable, that's certainly okay for us too as a start, in order to clean up the KVM code.

*However*, I must once more ask for help understanding ktime_get_snapshot, otherwise there is no way that I can start using it in kvmclock.

In particular I'd like to understand the contract of ktime_get_snapshot, namely the meaning of the "cycles" field in struct system_time_snapshot.

Quoted 6 lines

Does it have to be system clock cycles, like TSC, or can it be just the value of the clock source? And if the latter, are the users broken, because they can receive any random clocksource output in ->cycles? It doesn't help that, for all of the users of ->cycles (which are all reached from get_device_system_crosststamp), the code that actually uses the member is never called in the current git master.

stamp->cycles is always the raw counter value retrieved via clock->read(). clock is the current clock source.

Quoted 9 lines

I asked these questions to John in the previous review, but got no answer. My understanding is that get_device_system_crosststamp is broken for !tsc clocksource.

Because struct system_counterval_t must have a TSC value, history_begin needs to contain a cross-timestamp of the TSC (in ->cycles) and the clock (in ->real and ->raw). And if this cross-timestamp is not available, get_device_system_crosststamp needs to know, so that it doesn't use history_begin at all.

Forget about TSC. TSC is one particular use case.

What's required here are two clocks which are strictly coupled, i.e. have a constant frequency ratio and a constant offset. And that is enforced:

                /*
                 * Verify that the clocksource associated with the captured
                 * system counter value is the same as the currently installed
                 * timekeeper clocksource
                 */
                if (tk->tkr_mono.clock != system_counterval.cs)
                        return -ENODEV;

And that's not broken, that merily makes sure that the function CAN provide valid data and is not trying to correlate stuff which is not coupled.

Quoted 2 lines

Now, such cross-timestamp functionality is exactly what "read_with_stamp" provides.

No, it's a hack.

Quoted 4 lines

So, if history_begin _is_ broken, after fixing it (with "read_with_stamp" or otherwise) ktime_get_snapshot would provide KVM with everything it needs. If not, I'd be completely off base, and I'd have to apologize to Denis for screwing up.

I told you folks before that you are trying to abuse infrastructure which was designed for a different purpose. Now you're claiming it's broken. It's not broken, it works perfectly fine for the intended use cases.

It correlates hardware captured timestamps with system time. One particular use case is high precision PTP.

The network card can capture both PTP time and ART time atomically. ART and TSC have a constant ratio and constant offset. The cross time stamp machinery relates that values, which might be outside of the current timekeeping window, back to CLOCK_MONOTONIC and CLOCK_REALTIME.

But there is no requirement that current clocksource is TSC. The requirement is:

The hardware reference clock, the one which can be captured atomically with device clock (PTP, audio whatever), is the coupled clock of the current timekeeping clocksource. Both have a fixed ratio and offset.

That's completely independend of TSC. TSC/ART are a particular hardware implementation which can use that infrastructure because they fulfil the requirement.

So please stop these uninformed claims about brokeness and TSC requirements. Instead please sit down and figure out whether your particular use case of kvmclock/hyperv clock actually fit into that functionality, i.e. whether you have

1) 'device time'
2) 'system reference time'
3) 'system time'

where

#1 and #2 can be captured atomically

#2 and #3 are coupled clocks with a fixed ratio and offset

If those requirements are fulfilled then you can simply use it as is and it will give you CLOCK_MONOTONIC and CLOCK_REALTIME for the captured device time.

If your use case is different and does not fit into that picture then please write it up clearly what you are trying to achieve and we can discuss how to make it work w/o adding duct tape hackery.

Thanks,

tglx

maintainer
#5 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 10 lines · Thomas Gleixner

But there is no requirement that current clocksource is TSC. The requirement is:

The hardware reference clock, the one which can be captured atomically with device clock (PTP, audio whatever), is the coupled clock of the current timekeeping clocksource. Both have a fixed ratio and offset.

That's completely independend of TSC. TSC/ART are a particular hardware implementation which can use that infrastructure because they fulfil the requirement.

Ok, this helps.

Quoted 2 lines

So please stop these uninformed claims about brokeness and TSC requirements.

It was a question, not an uninformed claim. You answered the question now.

Quoted 14 lines

Instead please sit down and figure out whether your particular use case of kvmclock/hyperv clock actually fit into that functionality, i.e. whether you have

1) 'device time'
2) 'system reference time'
3) 'system time'

where

#1 and #2 can be captured atomically

#2 and #3 are coupled clocks with a fixed ratio and offset

Quoted 7 lines

If those requirements are fulfilled then you can simply use it as is and it will give you CLOCK_MONOTONIC and CLOCK_REALTIME for the captured device time.

If your use case is different and does not fit into that picture then please write it up clearly what you are trying to achieve and we can discuss how to make it work w/o adding duct tape hackery.

Yes, I understand better now why you consider read_with_stamp a hack. And it is---but I was confused and couldn't think of anything better.

The definitions do fit KVM, and indeed there is ptp-kvm that does something very similar to what you describe in the other mail. We have:

#1 is host time
#2 is host TSC
#3 is guest TSC

We know that #2 and #3 have a fixed ratio and offset. The point is whether #1 and #2 can be captured atomically.

For PTP-KVM, the host tells the guest; if capturing the two is impossible, it fails the hypercall and ioctl(PTP_SYS_OFFSET_PRECISE) fails too.

Right now, the hypercall fails if the host clocksource is not the TSC clocksource, which is safe.

These patches are about ascertaining whether #1 and #2 can be captured atomically in a more generic way. In the read_with_stamp case:

- if it returns true, it gives an atomic reading of #1 and #2

- if it returns false, it gives a reading of #1 only.

I think the hook should be specific to x86. For example it could be an array of function pointers, indexed by vclock_mode, with the same semantics as read_with_stamp.

Paolo

#6 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 20 lines · Paolo Bonzini

The definitions do fit KVM, and indeed there is ptp-kvm that does something very similar to what you describe in the other mail. We have:

#1 is host time
#2 is host TSC
#3 is guest TSC

We know that #2 and #3 have a fixed ratio and offset. The point is whether #1 and #2 can be captured atomically.

For PTP-KVM, the host tells the guest; if capturing the two is impossible, it fails the hypercall and ioctl(PTP_SYS_OFFSET_PRECISE) fails too.

Right now, the hypercall fails if the host clocksource is not the TSC clocksource, which is safe.

These patches are about ascertaining whether #1 and #2 can be captured atomically in a more generic way. In the read_with_stamp case:

Quoted 8 lines

- if it returns true, it gives an atomic reading of #1 and #2

- if it returns false, it gives a reading of #1 only.

I think the hook should be specific to x86. For example it could be an array of function pointers, indexed by vclock_mode, with the same semantics as read_with_stamp.

I don't think you need that.

The get_time_fn() which is handed in to get_device_system_crossstamp() can convey that information:

                /*
                 * Try to synchronously capture device time and a system
                 * counter value calling back into the device driver
                 */
                ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
                if (ret)
                        return ret;

So in your case get_time_fn() would be kvmclock or hyperv clock specific and the actual hypercall implementation can return a failure code if the requirements are not met:

1) host clock source is TSC
2) capturing of host time and TSC is atomic

The host side of the hypercall can use ktime_get_snapshot() with the boot time extension and the extra bits I posted yesterday and fail if the snapshot is not valid for that purpose, i.e. host clocksource is not valid for guest correlation.

The hypercall side on the guest can then do:

	if (hypercall(hypercalldata))
		return -ENODEV;
	system_counterval->cs = tsc_clocksource;
       	system_counterval->data = hypercalldata;
	return 0;

Then the following check in get_device_system_crossstamp() will do the last sanity check:

		/*
		 * Verify that the clocksource associated with the captured
		 * system counter value is the same as the currently installed
		 * timekeeper clocksource
		 */
		if (tk->tkr_mono.clock != system_counterval.cs)
			return -ENODEV;

which catches the case where the guest side clock source is !TSC.

Thanks,

tglx

maintainer
#7 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 16 lines · Thomas Gleixner
Quoted 3 lines

I think the hook should be specific to x86. For example it could be an array of function pointers, indexed by vclock_mode, with the same semantics as read_with_stamp.

I don't think you need that.

The get_time_fn() which is handed in to get_device_system_crossstamp() can convey that information:

                /*
                 * Try to synchronously capture device time and a system
                 * counter value calling back into the device driver
                 */
                ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
                if (ret)
                        return ret;
Quoted 6 lines

So in your case get_time_fn() would be kvmclock or hyperv clock specific and the actual hypercall implementation can return a failure code if the requirements are not met:

1) host clock source is TSC
2) capturing of host time and TSC is atomic

So you are suggesting reusing the cross-timestamp hypercall to implement nested pvclock. There are advantages and disadvantages to that.

With read_with_stamp-like callbacks:

+ running on old KVM or on Hyper-V is supported
- pvclock_gtod_copy does not go away

With hypercall-based callbacks on the contrary:

+ KVM can use ktime_get_snapshot for the bare metal case
- only very new KVM is supported

Paolo

#8 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

Quoted 29 lines · Paolo Bonzini
Quoted 16 lines · Thomas Gleixner
Quoted 3 lines

I think the hook should be specific to x86. For example it could be an array of function pointers, indexed by vclock_mode, with the same semantics as read_with_stamp.

I don't think you need that.

The get_time_fn() which is handed in to get_device_system_crossstamp() can convey that information:

                /*
                 * Try to synchronously capture device time and a system
                 * counter value calling back into the device driver
                 */
                ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
                if (ret)
                        return ret;
Quoted 6 lines

So in your case get_time_fn() would be kvmclock or hyperv clock specific and the actual hypercall implementation can return a failure code if the requirements are not met:

1) host clock source is TSC
2) capturing of host time and TSC is atomic

So you are suggesting reusing the cross-timestamp hypercall to implement nested pvclock. There are advantages and disadvantages to that.

With read_with_stamp-like callbacks:

Quoted 7 lines

+ running on old KVM or on Hyper-V is supported
- pvclock_gtod_copy does not go away

With hypercall-based callbacks on the contrary:

+ KVM can use ktime_get_snapshot for the bare metal case
- only very new KVM is supported

I don't think that it's an either or decision.

get_device_system_crossstamp(get_time_fn, ......)

So you can have specific get_time_fn() implementations for your situation:

old_kvm_fn()
retrieve data from pvclock_gtod copy

new_kvm_fn()
use hypercall

hyperv_fn()
do what must be done

All implementations need a way to tell you:

1) Host time
2) Host TSC timestamp which corresponds to #1
3) Validity

For old_kvm_fn() pvclock_gtod_data.clock.vclock_mode == VCLOCK_TSC For new_kvm_fn() hypercall result
For hyperv_fn() whatever it takes

Thanks,

tglx

#9 lore raw

Re: [PATCH v5 1/6] timekeeper: introduce extended clocksource reading callback

In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

However, if get_device_system_crosststamp ran with kvmclock or Hyper-V clocksource, the two values stored by read_with_stamp would be different, basically a (TSC, nanoseconds) pair.

And that's exactly the problem. The cross time stamp infrastructure keeps the system clocksource (e.g. TSC) and the reference clock (e.g. ART) separate. There is enforcement that the two are coupled which I pointed out in the other mail, but conceptually they are different things and we are not going to change that and add voodoo callbacks which return different timestamps from different sources into the clocksource.

So if you want to map kvmclock to the existing infrastructure then:

device clock: maps to the hypervisor clock
system reference clock: maps to REF_TSC
system timekeeper clock: maps to TSC

Even if REF_TSC and TSC are the same physically conceptually they are different.

To make use of the existing infrastructure you have to provide means to capture HV clock and reference clock atomically. Thats probably something he hypervisor provides as a value pair HVCLOCK_NSEC, REF_TSC_VAL.

And then you can correlate system time to HVCLOCK_NSEC via REF_TSC_VAL because that has a fixed ratio / offset to the system TSC.

Thanks,

tglx

#10 lore raw

PATCH v5 2/6 timekeeper: introduce boot field in system_time_snapshot

In reply to Denis Plotnikov

The field keeps monotonic time since boot.

The value of boot will be used in KVM for masterclock.

This is a part of the work aiming to move to a more simple scheme of masterclock related values calculation in KVM

---
 include/linux/timekeeping.h | 2 ++
 kernel/time/timekeeping.c   | 4 ++++
 2 files changed, 6 insertions(+)
include/linux/timekeeping.h +2 -0
index ddc229f..5008e3e 100644
@@ -283,6 +283,7 @@ extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
  * @cycles:	Clocksource counter value to produce the system times
  * @real:	Realtime system time
  * @raw:	Monotonic raw system time
+ * @boot:	Monotonic time since boot
  * @clock_was_set_seq:	The sequence number of clock was set events
  * @cs_was_changed_seq:	The sequence number of clocksource change events
  */
@@ -290,6 +291,7 @@ struct system_time_snapshot {
 	u64		cycles;
 	ktime_t		real;
 	ktime_t		raw;
+	ktime_t		boot;
 	unsigned int	clock_was_set_seq;
 	u8		cs_was_changed_seq;
 };
kernel/time/timekeeping.c +4 -0
index cedafa0..66f7701 100644
@@ -953,6 +953,7 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 	unsigned long seq;
 	ktime_t base_raw;
 	ktime_t base_real;
+	ktime_t base_boot;
 	u64 nsec_raw;
 	u64 nsec_real;
 	u64 now;
@@ -967,6 +968,8 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 		base_real = ktime_add(tk->tkr_mono.base,
 				      tk_core.timekeeper.offs_real);
 		base_raw = tk->tkr_raw.base;
+		base_boot = ktime_add(tk->tkr_mono.base,
+					tk_core.timekeeper.offs_boot);
 		nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
 		nsec_raw  = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
 	} while (read_seqcount_retry(&tk_core.seq, seq));
@@ -974,6 +977,7 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 	systime_snapshot->cycles = now;
 	systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
 	systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
+	systime_snapshot->boot = ktime_add_ns(base_boot, nsec_real);
 }
 EXPORT_SYMBOL_GPL(ktime_get_snapshot);
 
Signature · 1 line
2.7.4
#11 lore raw

PATCH v5 3/6 timekeeper: use the extended reading function on snapshot acquiring

In reply to Denis Plotnikov

Make use of the extended reading function on time snapshot getting:
get raw cycles value if extended function is defined. The raw cycles value then is used for KVM masterclock.

This is a part of the work aiming to move to a more simple scheme of masterclock related values calculation in KVM

---
 include/linux/timekeeping.h |  3 +++
 kernel/time/timekeeping.c   | 13 +++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/timekeeping.h +3 -0
index 5008e3e..528d088 100644
@@ -286,6 +286,8 @@ extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
  * @boot:	Monotonic time since boot
  * @clock_was_set_seq:	The sequence number of clock was set events
  * @cs_was_changed_seq:	The sequence number of clocksource change events
+ * @cycles_valid:	The flag is true when @cycles contain actual
+ *			number of cycles instead some cycle derivative
  */
 struct system_time_snapshot {
 	u64		cycles;
@@ -294,6 +296,7 @@ struct system_time_snapshot {
 	ktime_t		boot;
 	unsigned int	clock_was_set_seq;
 	u8		cs_was_changed_seq;
+	bool		cycles_valid;
 };
 
 /*
kernel/time/timekeeping.c +11 -2
index 66f7701..d1aa575 100644
@@ -957,12 +957,22 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 	u64 nsec_raw;
 	u64 nsec_real;
 	u64 now;
+	struct clocksource *clock;
 
 	WARN_ON_ONCE(timekeeping_suspended);
 
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
-		now = tk_clock_read(&tk->tkr_mono);
+		clock = READ_ONCE(tk->tkr_mono.clock);
+		if (clock->read_with_stamp)
+			systime_snapshot->cycles_valid =
+				clock->read_with_stamp(
+					clock, &now, &systime_snapshot->cycles);
+		else {
+			systime_snapshot->cycles_valid = false;
+			now = clock->read(clock);
+			systime_snapshot->cycles = now;
+		}
 		systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
 		systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
 		base_real = ktime_add(tk->tkr_mono.base,
@@ -974,7 +984,6 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 		nsec_raw  = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
-	systime_snapshot->cycles = now;
 	systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
 	systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
 	systime_snapshot->boot = ktime_add_ns(base_boot, nsec_real);
Signature · 1 line
2.7.4
#12 lore raw

PATCH v5 4/6 tsc: implement the extended tsc reading function

In reply to Denis Plotnikov

By doing that, add tsc clocksource to a list of KVM clocksources providing valid cycle values, meaning that KVM can use its masterclock.

This is a part of the work aiming to move to a more simple scheme of masterclock related values calculation in KVM.

---
 arch/x86/kernel/tsc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/tsc.c +10 -0
index 796d96b..8786454 100644
@@ -1036,6 +1036,15 @@ static void tsc_cs_tick_stable(struct clocksource *cs)
 		sched_clock_tick_stable();
 }
 
+static bool tsc_read_with_stamp(struct clocksource *cs,
+				u64 *cycles, u64 *cycles_stamp)
+{
+	u64 tsc = read_tsc(cs);
+	*cycles = tsc;
+	*cycles_stamp = tsc;
+	return true;
+}
+
 /*
  * .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc()
  */
@@ -1043,6 +1052,7 @@ static struct clocksource clocksource_tsc = {
 	.name                   = "tsc",
 	.rating                 = 300,
 	.read                   = read_tsc,
+	.read_with_stamp	= tsc_read_with_stamp,
 	.mask                   = CLOCKSOURCE_MASK(64),
 	.flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
 				  CLOCK_SOURCE_MUST_VERIFY,
Signature · 1 line
2.7.4
#13 lore raw

PATCH v5 5/6 KVM: x86: switch to masterclock update using timekeeper functionality

In reply to Denis Plotnikov

It is reasonable to switch KVM to using a more simple, effective and conceptually correct scheme of dealing with the data needed for kvm masterclock values calculation.

With the current scheme the kvm needs to have an up-to-date copy of some timekeeper data to provide a guest using kvmclock with necessary information.

This is not:
- simple
KVM has to have a lot of code to do that, instead KVM could use a timekeeper function to get all the data it needs
- effective
the copy of the data used for time data calculation is updated every time it changed although this is not necessary since the updated piece of time data is needed in certain moments only (e.g masterclock updating), instead KVM can request this data directly form the timekeeper at the moments when it's really needed
- conceptually correct
to do the work (calculate the time data) which the other part of the system (timekeeper) has been designed and is able to do is not the proper way, instead deligate the work to the proper part

This patch switches KVM to using the improved timekeeper function for the kvm masterclock time data.

Removing the leftovers of the old scheme is the matter of the next patches.

---
 arch/x86/kvm/x86.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +18 -8
index 05a5e57..0e86729 100644
@@ -1643,22 +1643,32 @@ static int do_realtime(struct timespec *ts, u64 *cycle_now)
 /* returns true if host is using tsc clocksource */
 static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *cycle_now)
 {
-	/* checked again under seqlock below */
-	if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
-		return false;
+	struct system_time_snapshot systime_snapshot;
+
+	ktime_get_snapshot(&systime_snapshot);
+
+	if (systime_snapshot.cycles_valid) {
+		*kernel_ns = ktime_to_ns(systime_snapshot.boot);
+		*cycle_now = systime_snapshot.cycles;
+	}
 
-	return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
+	return systime_snapshot.cycles_valid;
 }
 
 /* returns true if host is using tsc clocksource */
 static bool kvm_get_walltime_and_clockread(struct timespec *ts,
 					   u64 *cycle_now)
 {
-	/* checked again under seqlock below */
-	if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
-		return false;
+	struct system_time_snapshot systime_snapshot;
+
+	ktime_get_snapshot(&systime_snapshot);
+
+	if (systime_snapshot.cycles_valid) {
+		*ts = ktime_to_timespec(systime_snapshot.real);
+		*cycle_now = systime_snapshot.cycles;
+	}
 
-	return do_realtime(ts, cycle_now) == VCLOCK_TSC;
+	return systime_snapshot.cycles_valid;
 }
 #endif
 
Signature · 1 line
2.7.4
#14 lore raw

PATCH v5 6/6 KVM: x86: remove not used pvclock_gtod_copy

In reply to Denis Plotnikov

Since, KVM has been switched to getting masterclock related data right from the timekeeper by the previous patches, now we are able to remove all the parts related to the old scheme of getting masterclock data.

This patch removes those parts.

Using of pvclock_gtod_copy notifier might be redundant because the current scheme doesn't need to update any shadow timekeeper data structure (pvclock_gtod_copy) every time the timekeeper does changes. The notifier has been left unremoved intentionally and its replacing with something else is the matter of future work.

---
 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/trace.h            |  31 ++----
 arch/x86/kvm/x86.c              | 219 +++++++---------------------------------
 3 files changed, 46 insertions(+), 206 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index f4d120a..a1dc06c 100644
@@ -792,7 +792,7 @@ struct kvm_arch {
 	u64 cur_tsc_generation;
 	int nr_vcpus_matched_tsc;
 
-	spinlock_t pvclock_gtod_sync_lock;
+	spinlock_t masterclock_lock;
 	bool use_master_clock;
 	u64 master_kernel_ns;
 	u64 master_cycle_now;
arch/x86/kvm/trace.h +11 -20
index 0a6cc67..923ab31 100644
@@ -807,45 +807,39 @@ TRACE_EVENT(kvm_write_tsc_offset,
 
 #ifdef CONFIG_X86_64
 
-#define host_clocks					\
-	{VCLOCK_NONE, "none"},				\
-	{VCLOCK_TSC,  "tsc"}				\
-
 TRACE_EVENT(kvm_update_master_clock,
-	TP_PROTO(bool use_master_clock, unsigned int host_clock, bool offset_matched),
-	TP_ARGS(use_master_clock, host_clock, offset_matched),
+	TP_PROTO(bool use_master_clock, bool host_clock_stable,
+		bool offset_matched),
+	TP_ARGS(use_master_clock, host_clock_stable, offset_matched),
 
 	TP_STRUCT__entry(
 		__field(		bool,	use_master_clock	)
-		__field(	unsigned int,	host_clock		)
+		__field(		bool,	host_clock_stable	)
 		__field(		bool,	offset_matched		)
 	),
 
 	TP_fast_assign(
 		__entry->use_master_clock	= use_master_clock;
-		__entry->host_clock		= host_clock;
+		__entry->host_clock_stable	= host_clock_stable;
 		__entry->offset_matched		= offset_matched;
 	),
 
-	TP_printk("masterclock %d hostclock %s offsetmatched %u",
+	TP_printk("masterclock %d hostclock stable %u offsetmatched %u",
 		  __entry->use_master_clock,
-		  __print_symbolic(__entry->host_clock, host_clocks),
+		  __entry->host_clock_stable,
 		  __entry->offset_matched)
 );
 
 TRACE_EVENT(kvm_track_tsc,
 	TP_PROTO(unsigned int vcpu_id, unsigned int nr_matched,
-		 unsigned int online_vcpus, bool use_master_clock,
-		 unsigned int host_clock),
-	TP_ARGS(vcpu_id, nr_matched, online_vcpus, use_master_clock,
-		host_clock),
+		 unsigned int online_vcpus, bool use_master_clock),
+	TP_ARGS(vcpu_id, nr_matched, online_vcpus, use_master_clock),
 
 	TP_STRUCT__entry(
 		__field(	unsigned int,	vcpu_id			)
 		__field(	unsigned int,	nr_vcpus_matched_tsc	)
 		__field(	unsigned int,	online_vcpus		)
 		__field(	bool,		use_master_clock	)
-		__field(	unsigned int,	host_clock		)
 	),
 
 	TP_fast_assign(
@@ -853,14 +847,11 @@ TRACE_EVENT(kvm_track_tsc,
 		__entry->nr_vcpus_matched_tsc	= nr_matched;
 		__entry->online_vcpus		= online_vcpus;
 		__entry->use_master_clock	= use_master_clock;
-		__entry->host_clock		= host_clock;
 	),
 
-	TP_printk("vcpu_id %u masterclock %u offsetmatched %u nr_online %u"
-		  " hostclock %s",
+	TP_printk("vcpu_id %u masterclock %u offsetmatched %u nr_online %u",
 		  __entry->vcpu_id, __entry->use_master_clock,
-		  __entry->nr_vcpus_matched_tsc, __entry->online_vcpus,
-		  __print_symbolic(__entry->host_clock, host_clocks))
+		  __entry->nr_vcpus_matched_tsc, __entry->online_vcpus)
 );
 
 #endif /* CONFIG_X86_64 */
arch/x86/kvm/x86.c +34 -185

360 lines not on this page · raw message

Signature · 1 line
2.7.4
#15 lore raw
In reply to Denis Plotnikov

ping!

Quoted 66 lines · Denis Plotnikov

V5:
It was decided to split the series
"make L2's kvm-clock stable, get rid of pvclock_gtod_copy in KVM" into two parts to make the changes finer grained:
1. get rid of pvclock_gtod_copy in KVM
2. make L2's kvm-clock stable
This V5 series implements the 1st part. This part gets rid of pvclock_gtod_copy by switching to use the timekeeper functionality being extended. This new scheme doesn't add any new notifiers and keeps using the existing one (see patch 6 description) which had been used by the pvclock_gtod_copy to keep track timekeeper data changes.

The 2nd part is being discussed and is going to be sent later on.

V4:
* removed "is stable" function with vague definition of stability there is the only function which does time with cycle stamp getting
* some variables renamed
* some patches split into smaller once
* atomic64_t usage is replaced with atomic_t

V3:
Changing the timekeeper interface for clocksource reading looks like an overkill to achive the goal of getting cycles stamp for KVM. Instead extend the timekeeping interface and add functions which provide necessary data: read clocksource with cycles stamp, check whether the clock source is stable.

Use those functions and improve existing timekeeper functionality to replace pvclock_gtod_copy scheme in masterclock data calculation.

V2:
The main goal is to make L2 kvm-clock be stable when it's running over L1 with stable kvm-clock.

The patch series is for x86 architecture only. If the series is approved I'll do changes for other architectures but I don't have an ability to compile and check for every single on (help needed)

The patch series do the following:

* change timekeeper interface to get cycles stamp value from the timekeeper
* get rid of pvclock copy in KVM by using the changed timekeeper
interface: get time and cycles right from the timekeeper
* make KVM recognize a stable kvm-clock as stable clocksource and use the KVM masterclock in this case, which means making L2 stable when running over stable L1 kvm-clock

Denis Plotnikov (6):
timekeeper: introduce extended clocksource reading callback
timekeeper: introduce boot field in system_time_snapshot
timekeeper: use the extended reading function on snapshot acquiring
tsc: implement the extended tsc reading function
KVM: x86: switch to masterclock update using timekeeper functionality
KVM: x86: remove not used pvclock_gtod_copy

  arch/x86/include/asm/kvm_host.h |   2 +-
  arch/x86/kernel/tsc.c           |  10 ++
  arch/x86/kvm/trace.h            |  31 ++---
  arch/x86/kvm/x86.c              | 245 +++++++++-------------------------------
  include/linux/clocksource.h     |  11 +-
  include/linux/timekeeping.h     |   5 +
  kernel/time/timekeeping.c       |  17 ++-
  7 files changed, 104 insertions(+), 217 deletions(-)
Signature · 2 lines
Best,
Denis
#16 lore raw
Quoted 1 line · Denis Plotnikov

ping!

I'll have a look next week as I'm not much around this week.

Thanks,

tglx