Skip to contentSkip to conversation

Steal time for KVM

PATCH In review Glauber Costa 4 participants 27 messages Jun 16, 2011 – Jun 28, 2011

Subsystems (6)
Documentation x86 KVM KVM x86 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover Steal time for KVM

Here's the same series as before, just incorporating the resolutions we've agreed upon over the list. If you guys are in agreement with me with what stayed, this series is ready for getting merged, I believe. Feel free to keep the discussion going otherwise.

Glauber Costa (7):
KVM-HDR Add constant to represent KVM MSRs enabled bit
KVM-HDR: KVM Steal time implementation
KVM-HV: KVM Steal time implementation
KVM-GST: Add a pv_ops stub for steal time
KVM-GST: KVM Steal time accounting
KVM-GST: adjust scheduler cpu power
KVM-GST: KVM Steal time registration

 Documentation/kernel-parameters.txt   |    4 ++
 Documentation/virtual/kvm/msr.txt     |   33 ++++++++++++
 arch/x86/Kconfig                      |   12 +++++
 arch/x86/include/asm/kvm_host.h       |    8 +++
 arch/x86/include/asm/kvm_para.h       |   15 ++++++
 arch/x86/include/asm/paravirt.h       |    9 +++
 arch/x86/include/asm/paravirt_types.h |    1 +
 arch/x86/kernel/kvm.c                 |   73 +++++++++++++++++++++++++++
 arch/x86/kernel/kvmclock.c            |    2 +
 arch/x86/kernel/paravirt.c            |    9 +++
 arch/x86/kvm/x86.c                    |   60 +++++++++++++++++++++-
 kernel/sched.c                        |   89 +++++++++++++++++++++++++++++---
 kernel/sched_features.h               |    4 +-
 13 files changed, 305 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH v2 1/7 KVM-HDR Add constant to represent KVM MSRs enabled bit

This patch is simple, put in a different commit so it can be more easily shared between guest and hypervisor. It just defines a named constant to indicate the enable bit for KVM-specific MSRs.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 arch/x86/include/asm/kvm_para.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +1 -0
index a427bf7..d6cd79b 100644
@@ -30,6 +30,7 @@
 #define MSR_KVM_WALL_CLOCK  0x11
 #define MSR_KVM_SYSTEM_TIME 0x12
 
+#define KVM_MSR_ENABLED 1
 /* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */
 #define MSR_KVM_WALL_CLOCK_NEW  0x4b564d00
 #define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
Signature · 1 line
1.7.3.4
Tested-by on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] KVM-HDR Add constant to represent KVM MSRs enabled bit

Quoted 11 lines · Glauber Costa

This patch is simple, put in a different commit so it can be more easily shared between guest and hypervisor. It just defines a named constant to indicate the enable bit for KVM-specific MSRs.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#3 lore raw

PATCH v2 2/7 KVM-HDR: KVM Steal time implementation

In reply to Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the headers for it. I am keeping it separate to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 Documentation/virtual/kvm/msr.txt |   33 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/kvm_para.h   |    9 +++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
Documentation/virtual/kvm/msr.txt +33 -0
index d079aed..79c12a7 100644
@@ -185,3 +185,36 @@ MSR_KVM_ASYNC_PF_EN: 0x4b564d02
 
 	Currently type 2 APF will be always delivered on the same vcpu as
 	type 1 was, but guest should not rely on that.
+
+MSR_KVM_STEAL_TIME: 0x4b564d03
+
+	data: 64-byte alignment physical address of a memory area which must be
+	in guest RAM, plus an enable bit in bit 0. This memory is expected to
+	hold a copy of the following structure:
+
+	struct kvm_steal_time {
+	  	__u64 steal;
+ 		__u32 version;
+ 		__u32 flags;
+	 	__u32 pad[6];
+	}
+
+	whose data will be filled in by the hypervisor periodically. Only one
+	write, or registration, is needed for each VCPU. The interval between
+	updates of this structure is arbitrary and implementation-dependent.
+	The hypervisor may update this structure at any time it sees fit until
+	anything with bit0 == 0 is written to it. Guest is required to make sure
+	this structure is initialized to zero.
+
+	Fields have the following meanings:
+
+		version: guest has to check version before and after grabbing
+		time information and check that they are both equal and even.
+		An odd version indicates an in-progress update.
+
+		flags: At this point, always zero. May be used to indicate
+		changes in this structure in the future.
+
+		steal: the amount of time in which this vCPU did not run, in
+		nanoseconds.
+
arch/x86/include/asm/kvm_para.h +9 -0
index d6cd79b..ac306c4 100644
@@ -21,6 +21,7 @@
  */
 #define KVM_FEATURE_CLOCKSOURCE2        3
 #define KVM_FEATURE_ASYNC_PF		4
+#define KVM_FEATURE_STEAL_TIME		5
 
 /* The last 8 bits are used to indicate how to interpret the flags field
  * in pvclock structure. If no bits are set, all flags are ignored.
@@ -35,6 +36,14 @@
 #define MSR_KVM_WALL_CLOCK_NEW  0x4b564d00
 #define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
 #define MSR_KVM_ASYNC_PF_EN 0x4b564d02
+#define MSR_KVM_STEAL_TIME  0x4b564d03
+
+struct kvm_steal_time {
+	__u64 steal;
+	__u32 version;
+	__u32 flags;
+	__u32 pad[6];
+};
 
 #define KVM_MAX_MMU_OP_BATCH           32
 
Signature · 1 line
1.7.3.4
Tested-by on v2 2/7
#4 lore raw

Re: [PATCH v2 2/7] KVM-HDR: KVM Steal time implementation

Quoted 12 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the headers for it. I am keeping it separate to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Quoted 7 lines
CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#5 lore raw

Re: [PATCH v2 2/7] KVM-HDR: KVM Steal time implementation

In reply to Glauber Costa
Quoted 22 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the headers for it. I am keeping it separate to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

index d079aed..79c12a7 100644

Documentation/virtual/kvm/msr.txt +2 -0
@@ -185,3 +185,36 @@ MSR_KVM_ASYNC_PF_EN: 0x4b564d02
 
  	Currently type 2 APF will be always delivered on the same vcpu as
  	type 1 was, but guest should not rely on that.
+
+MSR_KVM_STEAL_TIME: 0x4b564d03

Documentation/virtual/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02 · line 199

Quoted 10 lines
Documentation/virtual/kvm/msr.txt +10 -0
+
+	data: 64-byte alignment physical address of a memory area which must be
+	in guest RAM, plus an enable bit in bit 0. This memory is expected to
+	hold a copy of the following structure:
+
+	struct kvm_steal_time {
+	  	__u64 steal;
+ 		__u32 version;
+ 		__u32 flags;
+	 	__u32 pad[6];

Should be 12 to be a 64-byte structure, no?

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Re: [PATCH v2 2/7] KVM-HDR: KVM Steal time implementation

Quoted 21 lines · Avi Kivity
Quoted 20 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information
about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the headers for it. I am keeping it separate to facilitate
backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

index d079aed..79c12a7 100644

Documentation/virtual/kvm/msr.txt +0 -0
@@ -185,3 +185,36 @@ MSR_KVM_ASYNC_PF_EN: 0x4b564d02
 
Quoted 16 lines
Quoted 4 lines

Currently type 2 APF will be always delivered on the same vcpu as type 1 was, but guest should not rely on that.

Documentation/virtual/kvm/msr.txt +2 -0
+
+MSR_KVM_STEAL_TIME: 0x4b564d03

Documentation/virtual/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02 · line 199

Quoted 10 lines
Documentation/virtual/kvm/msr.txt +10 -0
+
+ data: 64-byte alignment physical address of a memory area which must be
+ in guest RAM, plus an enable bit in bit 0. This memory is expected to
+ hold a copy of the following structure:
+
+ struct kvm_steal_time {
+ __u64 steal;
+ __u32 version;
+ __u32 flags;
+ __u32 pad[6];

Should be 12 to be a 64-byte structure, no?

Avi, you should understand this is a discussion between 3 people: One of them can do math, the other can't.

#7 lore raw

PATCH v2 3/7 KVM-HV: KVM Steal time implementation

In reply to Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 arch/x86/include/asm/kvm_host.h |    8 +++++
 arch/x86/include/asm/kvm_para.h |    4 ++
 arch/x86/kvm/x86.c              |   60 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 69 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -0
index fc38eca..5dce014 100644
@@ -388,6 +388,14 @@ struct kvm_vcpu_arch {
 	unsigned int hw_tsc_khz;
 	unsigned int time_offset;
 	struct page *time_page;
+
+	struct {
+		u64 msr_val;
+		gpa_t stime;
+		struct kvm_steal_time steal;
+		u64 this_time_out;
+	} st;
+
 	u64 last_guest_tsc;
 	u64 last_kernel_ns;
 	u64 last_tsc_nsec;
arch/x86/include/asm/kvm_para.h +4 -0
index ac306c4..0341e61 100644
@@ -45,6 +45,10 @@ struct kvm_steal_time {
 	__u32 pad[6];
 };
 
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL << (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1 << KVM_STEAL_ALIGNMENT_BITS) - 1 ) << 1)
+
 #define KVM_MAX_MMU_OP_BATCH           32
 
 #define KVM_ASYNC_PF_ENABLED			(1 << 0)
arch/x86/kvm/x86.c +57 -3

112 lines not on this page · raw message

Signature · 1 line
1.7.3.4
Tested-by on v2 3/7
#8 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

Quoted 12 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Quoted 7 lines
CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#9 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

In reply to Glauber Costa
Quoted 14 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL<<  (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1<<  KVM_STEAL_ALIGNMENT_BITS) - 1 )<<  1)

Clumsy, but okay.

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+	u64 delta;
+
+	if (vcpu->arch.st.stime&&  vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

Quoted 25 lines
arch/x86/kvm/x86.c +23 -0
+
+		if (unlikely(kvm_read_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+
+		delta = (get_kernel_ns() - vcpu->arch.st.this_time_out);
+
+		vcpu->arch.st.steal.steal += delta;
+		vcpu->arch.st.steal.version += 2;
+
+		if (unlikely(kvm_write_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+	}
+
+}
+

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2211

Quoted 6 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  			kvm_migrate_timers(vcpu);
  		vcpu->cpu = cpu;
  	}
+
+	record_steal_time(vcpu);
  }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

Quoted 25 lines · Avi Kivity
Quoted 20 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information
about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from
the headers to facilitate backports to people who wants to backport the kernel
part but not the hypervisor, or the other way around.

(quoted diff) +3 -0
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL<< (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1<< KVM_STEAL_ALIGNMENT_BITS) - 1
Quoted 1 line

)<< 1)

Clumsy, but okay.

Quoted 7 lines

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+ u64 delta;
+
+ if (vcpu->arch.st.stime&& vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

how exactly? stime is a guest physical address...

Quoted 24 lines
Quoted 24 lines
arch/x86/kvm/x86.c +23 -0
+
+ if (unlikely(kvm_read_guest(vcpu->kvm, vcpu->arch.st.stime,
+ &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+ vcpu->arch.st.stime = 0;
+ return;
+ }
+
+ delta = (get_kernel_ns() - vcpu->arch.st.this_time_out);
+
+ vcpu->arch.st.steal.steal += delta;
+ vcpu->arch.st.steal.version += 2;
+
+ if (unlikely(kvm_write_guest(vcpu->kvm, vcpu->arch.st.stime,
+ &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+ vcpu->arch.st.stime = 0;
+ return;
+ }
+ }
+
+}
+
Quoted 11 lines

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, · line 2212

Quoted 8 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu,
 int cpu)
 kvm_migrate_timers(vcpu);
 vcpu->cpu = cpu;
 }
+
+ record_steal_time(vcpu);
 }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

There are arguments either way.

Right now, the way it is, it does account our iothread as steal time, which is not 100 % accurate if we think steal time as "whatever takes time away from our VM". I tend to think it as "whatever takes time away from this CPU", which includes other cpus in the same VM. So thinking this way, in a 1-1 phys-to-virt cpu mapping, if the iothread is taking 80 % cpu for whatever reason, we have 80 % steal time the cpu that is sharing the physical cpu with the iothread.

Maybe we could account that as iotime ?
Questions like that are one of the reasons behind me leaving extra fields in the steal time structure. We could do a more fine grained accounting and differentiate between the multiple entities that can do work (of various kinds) in our behalf.

#11 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

Quoted 11 lines · Glauber Costa
Quoted 9 lines

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+ u64 delta;
+
+ if (vcpu->arch.st.stime&& vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

Quoted 1 line

how exactly? stime is a guest physical address...

0 is a valid physical address.

Quoted 13 lines
Quoted 12 lines

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, · line 2212

Quoted 9 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu,
 int cpu)
 kvm_migrate_timers(vcpu);
 vcpu->cpu = cpu;
 }
+
+ record_steal_time(vcpu);
 }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

Quoted 3 lines

There are arguments either way.

Right now, the way it is, it does account our iothread as steal time, which is not 100 % accurate if we think steal time as "whatever takes time away from our VM". I tend to think it as "whatever takes time away from this CPU", which includes other cpus in the same VM. So thinking this way, in a 1-1 phys-to-virt cpu mapping, if the iothread is taking 80 % cpu for whatever reason, we have 80 % steal time the cpu that is sharing the physical cpu with the iothread.

I'm not talking about the iothread, rather the vcpu thread while running in userspace.

Quoted 5 lines

Maybe we could account that as iotime ?
Questions like that are one of the reasons behind me leaving extra fields in the steal time structure. We could do a more fine grained accounting and differentiate between the multiple entities that can do work (of various kinds) in our behalf.

What do other architectures do (xen, s390)?

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#12 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

In reply to Avi Kivity
Quoted 53 lines · Avi Kivity
Quoted 14 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL<<  (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1<<  KVM_STEAL_ALIGNMENT_BITS) - 1 )<<  1)

Clumsy, but okay.

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+	u64 delta;
+
+	if (vcpu->arch.st.stime&&  vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

Quoted 24 lines
arch/x86/kvm/x86.c +23 -0
+
+		if (unlikely(kvm_read_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+
+		delta = (get_kernel_ns() - vcpu->arch.st.this_time_out);
+
+		vcpu->arch.st.steal.steal += delta;
+		vcpu->arch.st.steal.version += 2;
+
+		if (unlikely(kvm_write_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+	}
+
+}
+
Quoted 10 lines

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2211

Quoted 7 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 			kvm_migrate_timers(vcpu);
 		vcpu->cpu = cpu;
 	}
+
+	record_steal_time(vcpu);
 }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

It also accounts halt time (kvm_vcpu_block) as steal time. Glauber, you could instead use the "runnable-state-but-waiting-in-runqueue" field of SCHEDSTATS, i forgot the exact name.

#13 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

Quoted 65 lines · Marcelo Tosatti
Quoted 53 lines · Avi Kivity
Quoted 14 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL<<   (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1<<   KVM_STEAL_ALIGNMENT_BITS) - 1 )<<   1)

Clumsy, but okay.

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+	u64 delta;
+
+	if (vcpu->arch.st.stime&&   vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

Quoted 24 lines
arch/x86/kvm/x86.c +23 -0
+
+		if (unlikely(kvm_read_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+
+		delta = (get_kernel_ns() - vcpu->arch.st.this_time_out);
+
+		vcpu->arch.st.steal.steal += delta;
+		vcpu->arch.st.steal.version += 2;
+
+		if (unlikely(kvm_write_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+	}
+
+}
+
Quoted 10 lines

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2211

Quoted 7 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  			kvm_migrate_timers(vcpu);
  		vcpu->cpu = cpu;
  	}
+
+	record_steal_time(vcpu);
  }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

Quoted 4 lines

It also accounts halt time (kvm_vcpu_block) as steal time. Glauber, you could instead use the "runnable-state-but-waiting-in-runqueue" field of SCHEDSTATS, i forgot the exact name.

I thought about it in the past. I let the idea aside because I didn't want to introduce a dependency on SCHEDSTATS. But thinking about it again now (and after some days of experimentations with it), I think we could have both.

use run_delay (the field you were thinking of) when schedstats are available, and fallback to an estimate method like the one we're doing when it is not.

Objections ?

#14 lore raw

Re: [PATCH v2 3/7] KVM-HV: KVM Steal time implementation

Quoted 75 lines · Glauber Costa
Quoted 65 lines · Marcelo Tosatti
Quoted 53 lines · Avi Kivity
Quoted 14 lines · Glauber Costa

To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.

In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time

This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define KVM_STEAL_ALIGNMENT_BITS 5
+#define KVM_STEAL_VALID_BITS ((-1ULL<<   (KVM_STEAL_ALIGNMENT_BITS + 1)))
+#define KVM_STEAL_RESERVED_MASK (((1<<   KVM_STEAL_ALIGNMENT_BITS) - 1 )<<   1)

Clumsy, but okay.

arch/x86/kvm/x86.c static void kvmclock_reset(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+static void record_steal_time(struct kvm_vcpu *vcpu)
+{
+	u64 delta;
+
+	if (vcpu->arch.st.stime&&   vcpu->arch.st.this_time_out) {

0 is a valid value for stime.

Quoted 24 lines
arch/x86/kvm/x86.c +23 -0
+
+		if (unlikely(kvm_read_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+
+		delta = (get_kernel_ns() - vcpu->arch.st.this_time_out);
+
+		vcpu->arch.st.steal.steal += delta;
+		vcpu->arch.st.steal.version += 2;
+
+		if (unlikely(kvm_write_guest(vcpu->kvm, vcpu->arch.st.stime,
+			&vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) {
+
+			vcpu->arch.st.stime = 0;
+			return;
+		}
+	}
+
+}
+
Quoted 10 lines

arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2211

Quoted 7 lines
arch/x86/kvm/x86.c +2 -0
@@ -2158,6 +2206,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 			kvm_migrate_timers(vcpu);
 		vcpu->cpu = cpu;
 	}
+
+	record_steal_time(vcpu);
 }

This records time spent in userspace in the vcpu thread as steal time. Is this what we want? Or just time preempted away?

Quoted 4 lines

It also accounts halt time (kvm_vcpu_block) as steal time. Glauber, you could instead use the "runnable-state-but-waiting-in-runqueue" field of SCHEDSTATS, i forgot the exact name.

I thought about it in the past. I let the idea aside because I didn't want to introduce a dependency on SCHEDSTATS. But thinking about it again now (and after some days of experimentations with it), I think we could have both.

Quoted 5 lines

use run_delay (the field you were thinking of) when schedstats are available, and fallback to an estimate method like the one we're doing when it is not.

Objections ?

I'm okay with that.

#15 lore raw

PATCH v2 4/7 KVM-GST: Add a pv_ops stub for steal time

In reply to Glauber Costa

This patch adds a function pointer in one of the many paravirt_ops structs, to allow guests to register a steal time function.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 arch/x86/include/asm/paravirt.h       |    9 +++++++++
 arch/x86/include/asm/paravirt_types.h |    1 +
 arch/x86/kernel/paravirt.c            |    9 +++++++++
 3 files changed, 19 insertions(+), 0 deletions(-)
arch/x86/include/asm/paravirt.h +9 -0
index ebbc4d8..a7d2db9 100644
@@ -230,6 +230,15 @@ static inline unsigned long long paravirt_sched_clock(void)
 	return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
 }
 
+struct jump_label_key;
+extern struct jump_label_key paravirt_steal_enabled;
+extern struct jump_label_key paravirt_steal_rq_enabled;
+
+static inline u64 paravirt_steal_clock(int cpu)
+{
+	return PVOP_CALL1(u64, pv_time_ops.steal_clock, cpu);
+}
+
 static inline unsigned long long paravirt_read_pmc(int counter)
 {
 	return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
arch/x86/include/asm/paravirt_types.h +1 -0
index 8288509..2c76521 100644
@@ -89,6 +89,7 @@ struct pv_lazy_ops {
 
 struct pv_time_ops {
 	unsigned long long (*sched_clock)(void);
+	unsigned long long (*steal_clock)(int cpu);
 	unsigned long (*get_tsc_khz)(void);
 };
 
arch/x86/kernel/paravirt.c +9 -0
index 869e1ae..613a793 100644
@@ -202,6 +202,14 @@ static void native_flush_tlb_single(unsigned long addr)
 	__native_flush_tlb_single(addr);
 }
 
+struct jump_label_key paravirt_steal_enabled;
+struct jump_label_key paravirt_steal_rq_enabled;
+
+static u64 native_steal_clock(int cpu)
+{
+	return 0;
+}
+
 /* These are in entry.S */
 extern void native_iret(void);
 extern void native_irq_enable_sysexit(void);
@@ -307,6 +315,7 @@ struct pv_init_ops pv_init_ops = {
 
 struct pv_time_ops pv_time_ops = {
 	.sched_clock = native_sched_clock,
+	.steal_clock = native_steal_clock,
 };
 
 struct pv_irq_ops pv_irq_ops = {
Signature · 1 line
1.7.3.4
Tested-by on v2 4/7
#16 lore raw

Re: [PATCH v2 4/7] KVM-GST: Add a pv_ops stub for steal time

Quoted 10 lines · Glauber Costa

This patch adds a function pointer in one of the many paravirt_ops structs, to allow guests to register a steal time function.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#17 lore raw

PATCH v2 5/7 KVM-GST: KVM Steal time accounting

In reply to Glauber Costa

This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.

Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 kernel/sched.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
kernel/sched.c +53 -0
index 3f2e502..fa983c6 100644
@@ -75,6 +75,7 @@
 #include <asm/tlb.h>
 #include <asm/irq_regs.h>
 #include <asm/mutex.h>
+#include <asm/paravirt.h>
 
 #include "sched_cpupri.h"
 #include "workqueue_sched.h"
@@ -528,6 +529,7 @@ struct rq {
 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
 	u64 prev_irq_time;
 #endif
+	u64 prev_steal_time;
 
 	/* calc_load related fields */
 	unsigned long calc_load_update;
@@ -3705,6 +3707,49 @@ unsigned long long thread_group_sched_runtime(struct task_struct *p)
 }
 
 /*
+ * We have to at flush steal time information every time something else
+ * is accounted. Since the accounting functions are all visible to the rest
+ * of the kernel, it gets tricky to do them in one place. This helper function
+ * helps us.
+ *
+ * When the system is idle, the concept of steal time does not apply. We just
+ * tell the underlying hypervisor that we grabbed the data, but skip steal time
+ * accounting
+ */
+static inline bool touch_steal_time(int is_idle)
+{
+	u64 steal, st = 0;
+
+	if (static_branch(&paravirt_steal_enabled)) {
+
+		steal = paravirt_steal_clock(smp_processor_id());
+
+		steal -= this_rq()->prev_steal_time;
+		if (is_idle) {
+			this_rq()->prev_steal_time += steal;
+			return false;
+		}
+
+		while (steal >= TICK_NSEC) {
+			/*
+			 * Inline assembly required to prevent the compiler
+			 * optimising this loop into a divmod call.
+			 * See __iter_div_u64_rem() for another example of this.
+			 */
+			asm("" : "+rm" (steal));
+
+			steal -= TICK_NSEC;
+			this_rq()->prev_steal_time += TICK_NSEC;
+			st++;
+		}
+
+		account_steal_time(st);
+		return !!st;
+	}
+	return false;
+}
+
+/*
  * Account user cpu time to a process.
  * @p: the process that the cpu time gets accounted to
  * @cputime: the cpu time spent in user space since the last update
@@ -3716,6 +3761,9 @@ void account_user_time(struct task_struct *p, cputime_t cputime,
 	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
 	cputime64_t tmp;
 
+	if (touch_steal_time(0))
+		return;
+
 	/* Add user time to process. */
 	p->utime = cputime_add(p->utime, cputime);
 	p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
@@ -3802,6 +3850,9 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
 	cputime64_t *target_cputime64;
 
+	if (touch_steal_time(0))
+		return;
+
 	if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
 		account_guest_time(p, cputime, cputime_scaled);
 		return;
@@ -3839,6 +3890,8 @@ void account_idle_time(cputime_t cputime)
 	cputime64_t cputime64 = cputime_to_cputime64(cputime);
 	struct rq *rq = this_rq();
 
+	touch_steal_time(1);
+
 	if (atomic_read(&rq->nr_iowait) > 0)
 		cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
 	else
Signature · 1 line
1.7.3.4
Tested-by on v2 5/7
#18 lore raw

Re: [PATCH v2 5/7] KVM-GST: KVM Steal time accounting

Quoted 10 lines · Glauber Costa

This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.

Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.

Quoted 7 lines
CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#19 lore raw

Re: [PATCH v2 5/7] KVM-GST: KVM Steal time accounting

In reply to Glauber Costa
Quoted 30 lines · Glauber Costa

This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.

Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.

/*
+ * We have to at flush steal time information every time something else
+ * is accounted. Since the accounting functions are all visible to the rest
+ * of the kernel, it gets tricky to do them in one place. This helper function
+ * helps us.
+ *
+ * When the system is idle, the concept of steal time does not apply. We just
+ * tell the underlying hypervisor that we grabbed the data, but skip steal time
+ * accounting
+ */

kernel/sched.c +10 -0
+static inline bool touch_steal_time(int is_idle)
+{
+	u64 steal, st = 0;
+
+	if (static_branch(&paravirt_steal_enabled)) {
+
+		steal = paravirt_steal_clock(smp_processor_id());
+
+		steal -= this_rq()->prev_steal_time;
+		if (is_idle) {

kernel/sched.c unsigned long long thread_group_sched_runtime(struct task_struct *p) · line 3738

Quoted 10 lines
kernel/sched.c +10 -0
+			this_rq()->prev_steal_time += steal;
+			return false;
+		}
+
+		while (steal>= TICK_NSEC) {
+			/*
+			 * Inline assembly required to prevent the compiler
+			 * optimising this loop into a divmod call.
+			 * See __iter_div_u64_rem() for another example of this.
+			 */

Why not use said function?

kernel/sched.c unsigned long long thread_group_sched_runtime(struct task_struct *p) · line 3743

Quoted 5 lines
kernel/sched.c +5 -0
+			asm("" : "+rm" (steal));
+
+			steal -= TICK_NSEC;
+			this_rq()->prev_steal_time += TICK_NSEC;
+			st++;

Suppose a live migration or SIGSTOP causes lots of steal time. How long will we spend here?

kernel/sched.c unsigned long long thread_group_sched_runtime(struct task_struct *p) · line 3747

Quoted 4 lines
kernel/sched.c +4 -0
+		}
+
+		account_steal_time(st);
+		return !!st;

!! !needed, you're returning a bool.

kernel/sched.c unsigned long long thread_group_sched_runtime(struct task_struct *p) · line 3751

Quoted 4 lines
kernel/sched.c +4 -0
+	}
+	return false;
+}
+

I'll need Peter's (or another sched maintainer's) review to apply this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#20 lore raw

Re: [PATCH v2 5/7] KVM-GST: KVM Steal time accounting

Quoted 39 lines · Avi Kivity
Quoted 38 lines · Glauber Costa

This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.

Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.

/*
+ * We have to at flush steal time information every time something else
+ * is accounted. Since the accounting functions are all visible to the rest
+ * of the kernel, it gets tricky to do them in one place. This helper function
+ * helps us.
+ *
+ * When the system is idle, the concept of steal time does not apply. We just
+ * tell the underlying hypervisor that we grabbed the data, but skip steal time
+ * accounting
+ */

kernel/sched.c +14 -0
+static inline bool touch_steal_time(int is_idle)
+{
+ u64 steal, st = 0;
+
+ if (static_branch(&paravirt_steal_enabled)) {
+
+ steal = paravirt_steal_clock(smp_processor_id());
+
+ steal -= this_rq()->prev_steal_time;
+ if (is_idle) {
+ this_rq()->prev_steal_time += steal;
+ return false;
+ }
+
Quoted 8 lines
Quoted 6 lines

+ while (steal>= TICK_NSEC) {
+ /*
+ * Inline assembly required to prevent the compiler
+ * optimising this loop into a divmod call.
+ * See __iter_div_u64_rem() for another example of this.
+ */

Why not use said function?

because here we want to do work during each loop. The said function
would have to be adapted for that, possibly using a macro, to run arbitrary code during each loop iteration, in a way that I don't think it is worthy given the current number of callers (2 counting this new one)

Quoted 8 lines
Quoted 5 lines

+ asm("" : "+rm" (steal));
+
+ steal -= TICK_NSEC;
+ this_rq()->prev_steal_time += TICK_NSEC;
+ st++;

Suppose a live migration or SIGSTOP causes lots of steal time. How long will we spend here?

Silly me. I actually used this same argument with Peter to cap it with "delta" in the next patch in this series. So I think you are 100 % right. Here, however, we do want to account all that time, I believe.

How about we do a slow division if we're > 10 sec (unlikely), and account everything as steal time in this scenario ?

Quoted 6 lines
Quoted 4 lines

+ }
+
+ account_steal_time(st);
+ return !!st;

!! !needed, you're returning a bool.

ah, sure thing.

Quoted 7 lines

Review

Quoted 4 lines

+ }
+ return false;

(quoted diff) +2 -0
+}
+

I'll need Peter's (or another sched maintainer's) review to apply this.

#21 lore raw

Re: [PATCH v2 5/7] KVM-GST: KVM Steal time accounting

Quoted 50 lines · Glauber Costa
Quoted 40 lines · Avi Kivity
Quoted 39 lines · Glauber Costa

This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.

Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.

/*
+ * We have to at flush steal time information every time something else
+ * is accounted. Since the accounting functions are all visible to the rest
+ * of the kernel, it gets tricky to do them in one place. This helper function
+ * helps us.
+ *
+ * When the system is idle, the concept of steal time does not apply. We just
+ * tell the underlying hypervisor that we grabbed the data, but skip steal time
+ * accounting
+ */

kernel/sched.c +14 -0
+static inline bool touch_steal_time(int is_idle)
+{
+ u64 steal, st = 0;
+
+ if (static_branch(&paravirt_steal_enabled)) {
+
+ steal = paravirt_steal_clock(smp_processor_id());
+
+ steal -= this_rq()->prev_steal_time;
+ if (is_idle) {
+ this_rq()->prev_steal_time += steal;
+ return false;
+ }
+
Quoted 8 lines
Quoted 6 lines

+ while (steal>= TICK_NSEC) {
+ /*
+ * Inline assembly required to prevent the compiler
+ * optimising this loop into a divmod call.
+ * See __iter_div_u64_rem() for another example of this.
+ */

Why not use said function?

Quoted 3 lines

because here we want to do work during each loop. The said function
would have to be adapted for that, possibly using a macro, to run arbitrary code during each loop iteration, in a way that I don't think it is worthy given the current number of callers (2 counting this new one)

You mean adding to prev_steal_time? That can be done outside the loop.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

+ asm("" : "+rm" (steal));
+
+ steal -= TICK_NSEC;
+ this_rq()->prev_steal_time += TICK_NSEC;
+ st++;

Quoted 5 lines
Quoted 2 lines

Suppose a live migration or SIGSTOP causes lots of steal time. How long will we spend here?

Silly me. I actually used this same argument with Peter to cap it with "delta" in the next patch in this series. So I think you are 100 % right. Here, however, we do want to account all that time, I believe.

How about we do a slow division if we're > 10 sec (unlikely), and account everything as steal time in this scenario ?

Okay. Division would be faster for a lot less than 10s though.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#22 lore raw

PATCH v2 6/7 KVM-GST: adjust scheduler cpu power

In reply to Glauber Costa

This is a first proposal for using steal time information to influence the scheduler. There are a lot of optimizations and fine grained adjustments to be done, but it is working reasonably so far for me (mostly)

With this patch (and some host pinnings to demonstrate the situation), two vcpus with very different steal time (Say 80 % vs 1 %) will not get an even distribution of processes. This is a situation that can naturally arise, specially in overcommited scenarios. Previosly, the guest scheduler would wrongly think that all cpus have the same ability to run processes, lowering the overall throughput.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 arch/x86/Kconfig        |   12 ++++++++++++
 kernel/sched.c          |   36 +++++++++++++++++++++++++++---------
 kernel/sched_features.h |    4 ++--
 3 files changed, 41 insertions(+), 11 deletions(-)
arch/x86/Kconfig +12 -0
index da34972..b26f312 100644
@@ -512,6 +512,18 @@ menuconfig PARAVIRT_GUEST
 
 if PARAVIRT_GUEST
 
+config PARAVIRT_TIME_ACCOUNTING
+	bool "Paravirtual steal time accounting"
+	select PARAVIRT
+	default n
+	---help---
+	  Select this option to enable fine granularity task steal time 
+	  accounting. Time spent executing other tasks in parallel with
+	  the current vCPU is discounted from the vCPU power. To account for
+	  that, there can be a small performance impact.
+
+	  If in doubt, say N here.
+
 source "arch/x86/xen/Kconfig"
 
 config KVM_CLOCK
kernel/sched.c +27 -9
index fa983c6..8513898 100644
@@ -530,6 +530,9 @@ struct rq {
 	u64 prev_irq_time;
 #endif
 	u64 prev_steal_time;
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+	u64 prev_steal_time_acc;
+#endif
 
 	/* calc_load related fields */
 	unsigned long calc_load_update;
@@ -1955,10 +1958,13 @@ void account_system_vtime(struct task_struct *curr)
 }
 EXPORT_SYMBOL_GPL(account_system_vtime);
 
+#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
+
 static void update_rq_clock_task(struct rq *rq, s64 delta)
 {
-	s64 irq_delta;
+	s64 irq_delta = 0, steal = 0;
 
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
 	irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
 
 	/*
@@ -1981,12 +1987,29 @@ static void update_rq_clock_task(struct rq *rq, s64 delta)
 
 	rq->prev_irq_time += irq_delta;
 	delta -= irq_delta;
+#endif
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+	if (static_branch((&paravirt_steal_rq_enabled))) {
+
+		steal = paravirt_steal_clock(cpu_of(rq));
+		steal -= rq->prev_steal_time_acc;
+
+		rq->prev_steal_time_acc += steal;
+
+		if (steal > delta)
+			steal = delta;
+
+		delta -= steal;
+	}
+#endif
+
 	rq->clock_task += delta;
 
-	if (irq_delta && sched_feat(NONIRQ_POWER))
-		sched_rt_avg_update(rq, irq_delta);
+	if ((irq_delta + steal) && sched_feat(NONTASK_POWER))
+		sched_rt_avg_update(rq, irq_delta + steal);
 }
 
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
 static int irqtime_account_hi_update(void)
 {
 	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
@@ -2021,12 +2044,7 @@ static int irqtime_account_si_update(void)
 
 #define sched_clock_irqtime	(0)
 
-static void update_rq_clock_task(struct rq *rq, s64 delta)
-{
-	rq->clock_task += delta;
-}
-
-#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
+#endif
 
 #include "sched_idletask.c"
 #include "sched_fair.c"
kernel/sched_features.h +2 -2
index be40f73..ca3b025 100644
@@ -61,9 +61,9 @@ SCHED_FEAT(LB_BIAS, 1)
 SCHED_FEAT(OWNER_SPIN, 1)
 
 /*
- * Decrement CPU power based on irq activity
+ * Decrement CPU power based on time not spent running tasks
  */
-SCHED_FEAT(NONIRQ_POWER, 1)
+SCHED_FEAT(NONTASK_POWER, 1)
 
 /*
  * Queue remote wakeups on the target CPU and process them
Signature · 1 line
1.7.3.4
Tested-by on v2 6/7
#23 lore raw

Re: [PATCH v2 6/7] KVM-GST: adjust scheduler cpu power

Quoted 12 lines · Glauber Costa

This is a first proposal for using steal time information to influence the scheduler. There are a lot of optimizations and fine grained adjustments to be done, but it is working reasonably so far for me (mostly)

With this patch (and some host pinnings to demonstrate the situation), two vcpus with very different steal time (Say 80 % vs 1 %) will not get an even distribution of processes. This is a situation that can naturally arise, specially in overcommited scenarios. Previosly, the guest scheduler would wrongly think that all cpus have the same ability to run processes, lowering the overall throughput.

Quoted 7 lines
CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
#24 lore raw

Re: [PATCH v2 6/7] KVM-GST: adjust scheduler cpu power

In reply to Glauber Costa
Quoted 5 lines · Glauber Costa

This is a first proposal for using steal time information to influence the scheduler. There are a lot of optimizations and fine grained adjustments to be done, but it is working reasonably so far for me (mostly)

Quoted 7 lines

With this patch (and some host pinnings to demonstrate the situation), two vcpus with very different steal time (Say 80 % vs 1 %) will not get an even distribution of processes. This is a situation that can naturally arise, specially in overcommited scenarios. Previosly, the guest scheduler would wrongly think that all cpus have the same ability to run processes, lowering the overall throughput.

Looks fine, but sched maintainer review needed.

Signature · 1 line
error compiling committee.c: too many arguments to function
#25 lore raw

PATCH v2 7/7 KVM-GST: KVM Steal time registration

In reply to Glauber Costa

Register steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>
---
 Documentation/kernel-parameters.txt |    4 ++
 arch/x86/include/asm/kvm_para.h     |    1 +
 arch/x86/kernel/kvm.c               |   73 +++++++++++++++++++++++++++++++++++
 arch/x86/kernel/kvmclock.c          |    2 +
 4 files changed, 80 insertions(+), 0 deletions(-)
Documentation/kernel-parameters.txt +4 -0
index fd248a31..a722574 100644
@@ -1737,6 +1737,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 	no-kvmapf	[X86,KVM] Disable paravirtualized asynchronous page
 			fault handling.
 
+	no-steal-acc    [X86,KVM] Disable paravirtualized steal time accounting.
+			steal time is computed, but won't influence scheduler
+			behaviour
+
 	nolapic		[X86-32,APIC] Do not enable or use the local APIC.
 
 	nolapic_timer	[X86-32,APIC] Do not use the local APIC timer.
arch/x86/include/asm/kvm_para.h +1 -0
index 0341e61..2a8f2a5 100644
@@ -94,6 +94,7 @@ struct kvm_vcpu_pv_apf_data {
 
 extern void kvmclock_init(void);
 extern int kvm_register_clock(char *txt);
+extern void kvm_disable_steal_time(void);
 
 
 /* This instruction is vmcall.  On non-VT architectures, it will generate a
arch/x86/kernel/kvm.c +73 -0

118 lines not on this page · raw message

arch/x86/kernel/kvmclock.c +2 -0
index 6389a6b..c1a0188 100644
@@ -160,6 +160,7 @@ static void __cpuinit kvm_setup_secondary_clock(void)
 static void kvm_crash_shutdown(struct pt_regs *regs)
 {
 	native_write_msr(msr_kvm_system_time, 0, 0);
+	kvm_disable_steal_time();
 	native_machine_crash_shutdown(regs);
 }
 #endif
@@ -167,6 +168,7 @@ static void kvm_crash_shutdown(struct pt_regs *regs)
 static void kvm_shutdown(void)
 {
 	native_write_msr(msr_kvm_system_time, 0, 0);
+	kvm_disable_steal_time();
 	native_machine_shutdown();
 }
 
Signature · 1 line
1.7.3.4
Tested-by on v2 7/7
#26 lore raw

Re: [PATCH v2 7/7] KVM-GST: KVM Steal time registration

Quoted 11 lines · Glauber Costa

Register steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.

CC Rik van Riel <[email protected]>
CC Jeremy Fitzhardinge <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Avi Kivity <[email protected]>
CC Anthony Liguori <[email protected]>
CC Eric B Munson <[email protected]>