Skip to contentSkip to conversation

kvm: Improving directed yield in PLE handler

RFC PATCH Superseded Raghavendra K T 3 participants 15 messages Jul 16, 2012 – Jul 18, 2012

Subsystems (5)
x86 KVM s390 KVM x86 +1 more subsystems

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover kvm: Improving directed yield in PLE handler

Currently Pause Loop Exit (PLE) handler is doing directed yield to a random vcpu on pl-exit. We already have filtering while choosing the candidate to yield_to. This change adds more checks while choosing a candidate to yield_to.

On a large vcpu guests, there is a high probability of yielding to the same vcpu who had recently done a pause-loop exit. Such a yield can lead to the vcpu spinning again.

The patchset keeps track of the pause loop exit and gives chance to a vcpu which has:

(a) Not done pause loop exit at all (probably he is preempted lock-holder)

(b) vcpu skipped in last iteration because it did pause loop exit, and probably has become eligible now (next eligible lock holder)

This concept also helps in cpu relax interception cases which use same handler.

Changes since V3:
- arch specific fix/changes (Christian)

Changes since v2:
- Move ple structure to common code (Avi)
- rename pause_loop_exited to cpu_relax_intercepted (Avi)
- add config HAVE_KVM_CPU_RELAX_INTERCEPT (Avi)
- Drop superfluous curly braces (Ingo)

Changes since v1:
- Add more documentation for structure and algorithm and Rename plo ==> ple (Rik).
- change dy_eligible initial value to false. (otherwise very first directed yield will not be skipped. (Nikunj)
- fixup signoff/from issue

Future enhancements:
(1) Currently we have a boolean to decide on eligibility of vcpu. It would be nice if I get feedback on guest (>32 vcpu) whether we can improve better with integer counter. (with counter = say f(log n )).

(2) We have not considered system load during iteration of vcpu. With that information we can limit the scan and also decide whether schedule() is better. [ I am able to use #kicked vcpus to decide on this But may be there are better ideas like information from global loadavg.]

(3) We can exploit this further with PV patches since it also knows about next eligible lock-holder.

Summary: There is a very good improvement for moderate / no over-commit scenario
for kvm based guest on PLE machine.

Results: kernbench improves by around 30%, 6% for 1x,2x respectively
ebizzy improves by around 87%, 23% for 1x,2x respectively

Note: The patches are tested on x86.

Links
V1: https://lkml.org/lkml/2012/7/9/32
V2: https://lkml.org/lkml/2012/7/10/392
V3: https://lkml.org/lkml/2012/7/12/437

Raghavendra K T (3):
config: Add config to support ple or cpu relax optimzation kvm : Note down when cpu relax intercepted or pause loop exited kvm : Choose a better candidate for directed yield

---
 arch/s390/kvm/Kconfig    |    1 +
 arch/x86/kvm/Kconfig     |    1 +
 include/linux/kvm_host.h |   42 ++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/Kconfig         |    3 +++
 virt/kvm/kvm_main.c      |   40 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 87 insertions(+), 0 deletions(-)
#1 lore raw

RFC PATCH v4 1/3 kvm/config: Add config to support ple or cpu relax optimzation

From: Raghavendra K T <[email protected]>

---
 arch/s390/kvm/Kconfig |    1 +
 arch/x86/kvm/Kconfig  |    1 +
 virt/kvm/Kconfig      |    3 +++
 3 files changed, 5 insertions(+), 0 deletions(-)
arch/s390/kvm/Kconfig +1 -0
index 78eb984..a6e2677 100644
@@ -21,6 +21,7 @@ config KVM
 	depends on HAVE_KVM && EXPERIMENTAL
 	select PREEMPT_NOTIFIERS
 	select ANON_INODES
+	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	---help---
 	  Support hosting paravirtualized guest machines using the SIE
 	  virtualization capability on the mainframe. This should work
arch/x86/kvm/Kconfig +1 -0
index a28f338..45c044f 100644
@@ -37,6 +37,7 @@ config KVM
 	select TASK_DELAY_ACCT
 	select PERF_EVENTS
 	select HAVE_KVM_MSI
+	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	---help---
 	  Support hosting fully virtualized guest machines using hardware
 	  virtualization extensions.  You will need a fairly recent
virt/kvm/Kconfig +3 -0
index 28694f4..d01b24b 100644
@@ -21,3 +21,6 @@ config KVM_ASYNC_PF
 
 config HAVE_KVM_MSI
        bool
+
+config HAVE_KVM_CPU_RELAX_INTERCEPT
+       bool
#2 lore raw

RFC PATCH v4 2/3 kvm: Note down when cpu relax intercepted or pause loop exited

In reply to Raghavendra K T

From: Raghavendra K T <[email protected]>

Noting pause loop exited vcpu or cpu relax intercepted helps in filtering right candidate to yield. Wrong selection of vcpu; i.e., a vcpu that just did a pl-exit or cpu relax intercepted may contribute to performance degradation.

---
V2 was:

 include/linux/kvm_host.h |   37 +++++++++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c      |    5 +++++
 2 files changed, 42 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +37 -0
index c446435..fa353bd 100644
@@ -183,6 +183,18 @@ struct kvm_vcpu {
 	} async_pf;
 #endif
 
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
+	/*
+	 * Cpu relax intercept or pause loop exit optimization
+	 * cpu_relax_intercepted: set when a vcpu does a pause loop exit
+	 *  or cpu relax intercepted.
+	 * dy_eligible: indicates whether vcpu is eligible for directed yield.
+	 */
+	struct {
+		bool cpu_relax_intercepted;
+		bool dy_eligible;
+	} ple;
+#endif
 	struct kvm_vcpu_arch arch;
 };
 
@@ -890,5 +902,30 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
 	}
 }
 
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
+
+static inline void kvm_vcpu_set_cpu_relax_intercepted(struct kvm_vcpu *vcpu,
+							bool val)
+{
+	vcpu->ple.cpu_relax_intercepted = val;
+}
+static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
+{
+	vcpu->ple.dy_eligible = val;
+}
+
+#else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
+
+static inline void kvm_vcpu_set_cpu_relax_intercepted(struct kvm_vcpu *vcpu,
+							bool val)
+{
+}
+
+static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
+{
+}
+
+#endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
+
 #endif
 
virt/kvm/kvm_main.c +5 -0
index 7e14068..c50213f 100644
@@ -236,6 +236,9 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	}
 	vcpu->run = page_address(page);
 
+	kvm_vcpu_set_cpu_relax_intercepted(vcpu, false);
+	kvm_vcpu_set_dy_eligible(vcpu, false);
+
 	r = kvm_arch_vcpu_init(vcpu);
 	if (r < 0)
 		goto fail_free_run;
@@ -1577,6 +1580,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 	int pass;
 	int i;
 
+	kvm_vcpu_set_cpu_relax_intercepted(me, true);
 	/*
 	 * We boost the priority of a VCPU that is runnable but not
 	 * currently running, because it got preempted by something
@@ -1602,6 +1606,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 			}
 		}
 	}
+	kvm_vcpu_set_cpu_relax_intercepted(me, false);
 }
 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
 
#7 lore raw

RFC PATCH v4 3/3 kvm: Choose better candidate for directed yield

In reply to Raghavendra K T

From: Raghavendra K T <[email protected]>

Currently, on a large vcpu guests, there is a high probability of yielding to the same vcpu who had recently done a pause-loop exit or cpu relax intercepted. Such a yield can lead to the vcpu spinning again and hence degrade the performance.

The patchset keeps track of the pause loop exit/cpu relax interception and gives chance to a vcpu which:
(a) Has not done pause loop exit or cpu relax intercepted at all (probably he is preempted lock-holder)
(b) Was skipped in last iteration because it did pause loop exit or cpu relax intercepted, and probably has become eligible now (next eligible lock holder)

---
V2 was:

 include/linux/kvm_host.h |    5 +++++
 virt/kvm/kvm_main.c      |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +5 -0
index fa353bd..cfb38a2 100644
@@ -925,6 +925,11 @@ static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
 {
 }
 
+static inline bool kvm_vcpu_check_and_update_eligible(struct kvm_vcpu *vcpu)
+{
+	return true;
+}
+
 #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
 
 #endif
virt/kvm/kvm_main.c +35 -0
index c50213f..6a82580 100644
@@ -1571,6 +1571,39 @@ bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
 }
 EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
 
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
+/*
+ * Helper that checks whether a VCPU is eligible for directed yield.
+ * Most eligible candidate to yield is decided by following heuristics:
+ *
+ *  (a) VCPU which has not done pl-exit or cpu relax intercepted recently
+ *  (preempted lock holder), indicated by @cpu_relax_intercepted.
+ *  Set at the beiginning and cleared at the end of interception/PLE handler.
+ *
+ *  (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
+ *  chance last time (mostly it has become eligible now since we have probably
+ *  yielded to lockholder in last iteration. This is done by toggling
+ *  @dy_eligible each time a VCPU checked for eligibility.)
+ *
+ *  Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
+ *  to preempted lock-holder could result in wrong VCPU selection and CPU
+ *  burning. Giving priority for a potential lock-holder increases lock
+ *  progress.
+ */
+bool kvm_vcpu_check_and_update_eligible(struct kvm_vcpu *vcpu)
+{
+	bool eligible;
+
+	eligible = !vcpu->ple.cpu_relax_intercepted ||
+			(vcpu->ple.cpu_relax_intercepted &&
+			 vcpu->ple.dy_eligible);
+
+	if (vcpu->ple.cpu_relax_intercepted)
+		vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;
+
+	return eligible;
+}
+#endif
 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 {
 	struct kvm *kvm = me->kvm;
@@ -1599,6 +1632,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 				continue;
 			if (waitqueue_active(&vcpu->wq))
 				continue;
+			if (!kvm_vcpu_check_and_update_eligible(vcpu))
+				continue;
 			if (kvm_vcpu_yield_to(vcpu)) {
 				kvm->last_boosted_vcpu = i;
 				yielded = 1;
#3 lore raw

Re: [PATCH RFC V4 2/3] kvm: Note down when cpu relax intercepted or pause loop exited

In reply to Raghavendra K T
Quoted 25 lines · Raghavendra K T

From: Raghavendra K T <[email protected]>

Noting pause loop exited vcpu or cpu relax intercepted helps in filtering right candidate to yield. Wrong selection of vcpu; i.e., a vcpu that just did a pl-exit or cpu relax intercepted may contribute to performance degradation.

---
V2 was:

 include/linux/kvm_host.h |   37 +++++++++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c      |    5 +++++
 2 files changed, 42 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +1 -0
index c446435..fa353bd 100644
@@ -183,6 +183,18 @@ struct kvm_vcpu {
 	} async_pf;
 #endif
 
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT

include/linux/kvm_host.h struct kvm_vcpu { · line 196

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+	/*
+	 * Cpu relax intercept or pause loop exit optimization
+	 * cpu_relax_intercepted: set when a vcpu does a pause loop exit
+	 *  or cpu relax intercepted.
+	 * dy_eligible: indicates whether vcpu is eligible for directed yield.
+	 */
+	struct {
+		bool cpu_relax_intercepted;
+		bool dy_eligible;
+	} ple;

Naming:

- ple is the x86 name, but this is common code. I'd call it spin_loop.
- cpu_relax_intercepted: can call it in_spin_loop, the original name can me "are we intercepting cpu_relax or not" which may be useful at one point (when we know we aren't overcommitted)
- dy_eligible: I'd like to understand the algorithm more and would want the name to reflect it, will come to this later if I have a better idea.

include/linux/kvm_host.h struct kvm_vcpu { · line 197

Quoted 1 line
include/linux/kvm_host.h +1 -0
+#endif

blank line.

Quoted 3 lines
include/linux/kvm_host.h +0 -0
 	struct kvm_vcpu_arch arch;
 };
 
Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

In reply to Raghavendra K T
Quoted 27 lines · Raghavendra K T

From: Raghavendra K T <[email protected]>

Currently, on a large vcpu guests, there is a high probability of yielding to the same vcpu who had recently done a pause-loop exit or cpu relax intercepted. Such a yield can lead to the vcpu spinning again and hence degrade the performance.

The patchset keeps track of the pause loop exit/cpu relax interception and gives chance to a vcpu which:
(a) Has not done pause loop exit or cpu relax intercepted at all (probably he is preempted lock-holder)
(b) Was skipped in last iteration because it did pause loop exit or cpu relax intercepted, and probably has become eligible now (next eligible lock holder)

virt/kvm/kvm_main.c +10 -0
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
+/*
+ * Helper that checks whether a VCPU is eligible for directed yield.
+ * Most eligible candidate to yield is decided by following heuristics:
+ *
+ *  (a) VCPU which has not done pl-exit or cpu relax intercepted recently
+ *  (preempted lock holder), indicated by @cpu_relax_intercepted.
+ *  Set at the beiginning and cleared at the end of interception/PLE handler.
+ *
+ *  (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1593

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+ *  chance last time (mostly it has become eligible now since we have probably
+ *  yielded to lockholder in last iteration. This is done by toggling
+ *  @dy_eligible each time a VCPU checked for eligibility.)
+ *
+ *  Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
+ *  to preempted lock-holder could result in wrong VCPU selection and CPU
+ *  burning. Giving priority for a potential lock-holder increases lock
+ *  progress.
+ */
+bool kvm_vcpu_check_and_update_eligible(struct kvm_vcpu *vcpu)

Predicates' names should give a hint as to what true and false returns mean. For example vcpu_eligible_for_directed_yield().

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1602

Quoted 9 lines
virt/kvm/kvm_main.c +9 -0
+{
+	bool eligible;
+
+	eligible = !vcpu->ple.cpu_relax_intercepted ||
+			(vcpu->ple.cpu_relax_intercepted &&
+			 vcpu->ple.dy_eligible);
+
+	if (vcpu->ple.cpu_relax_intercepted)
+		vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;

Probably should assign 'true', since the previous value is essentially random.

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1605

Quoted 3 lines
virt/kvm/kvm_main.c +3 -0
+
+	return eligible;
+}

You're accessing another vcpu's data structures without any locking. This is probably okay since we're not basing any life or death decisions on this, but a comment would be good to explain to readers that this has been considered and is okay (and why).

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

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

Quoted 4 lines · Avi Kivity
Quoted 4 lines
virt/kvm/kvm_main.c +4 -0
+{
+	bool eligible;
+
+	eligible = !vcpu->ple.cpu_relax_intercepted ||
Quoted 8 lines

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1602

Quoted 5 lines
virt/kvm/kvm_main.c +5 -0
+			(vcpu->ple.cpu_relax_intercepted&&
+			 vcpu->ple.dy_eligible);
+
+	if (vcpu->ple.cpu_relax_intercepted)
+		vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;

Probably should assign 'true', since the previous value is essentially random.

I suspect the intended purpose of this conditional is to flip the eligibility of a vcpu for being selected as a direct yield target.

In other words, that bit of the code is correct.

Signature · 1 line
All rights reversed
#10 lore raw

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

Quoted 15 lines · Rik van Riel
Quoted 4 lines · Avi Kivity
Quoted 4 lines
virt/kvm/kvm_main.c +4 -0
+{
+ bool eligible;
+
+ eligible = !vcpu->ple.cpu_relax_intercepted ||
Quoted 8 lines
Quoted 5 lines

+ (vcpu->ple.cpu_relax_intercepted&&
+ vcpu->ple.dy_eligible);
+
+ if (vcpu->ple.cpu_relax_intercepted)
+ vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;

Probably should assign 'true', since the previous value is essentially random.

Quoted 5 lines

I suspect the intended purpose of this conditional is to flip the eligibility of a vcpu for being selected as a direct yield target.

In other words, that bit of the code is correct.

Yes, That is the intention.

The first intention was to make sure, not to select previously selected pause loop exited guy.
But second intention was not to make a vcpu permanently eligible after skipping once.

The problem is when several PL exits happen simultaneously, (most possible in large vcpu guest), it is very much probable that same vcpu is tried as target of directed yield slowing down yielding to a eligible guy.

But I 'll test one more time to make sure that.

(if we can make dy_eligible true as suggested, that means probably we can live with only one bool variable.)

#4 lore raw

Re: [PATCH RFC V4 2/3] kvm: Note down when cpu relax intercepted or pause loop exited

In reply to Avi Kivity
Quoted 39 lines · Avi Kivity
Quoted 25 lines · Raghavendra K T

From: Raghavendra K T<[email protected]>

Noting pause loop exited vcpu or cpu relax intercepted helps in filtering right candidate to yield. Wrong selection of vcpu; i.e., a vcpu that just did a pl-exit or cpu relax intercepted may contribute to performance degradation.

---
V2 was:

  include/linux/kvm_host.h |   37 +++++++++++++++++++++++++++++++++++++
  virt/kvm/kvm_main.c      |    5 +++++
  2 files changed, 42 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +1 -0
index c446435..fa353bd 100644
@@ -183,6 +183,18 @@ struct kvm_vcpu {
  	} async_pf;
  #endif
 
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT

include/linux/kvm_host.h struct kvm_vcpu { · line 196

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+	/*
+	 * Cpu relax intercept or pause loop exit optimization
+	 * cpu_relax_intercepted: set when a vcpu does a pause loop exit
+	 *  or cpu relax intercepted.
+	 * dy_eligible: indicates whether vcpu is eligible for directed yield.
+	 */
+	struct {
+		bool cpu_relax_intercepted;
+		bool dy_eligible;
+	} ple;

Naming:

Quoted 1 line

- ple is the x86 name, but this is common code. I'd call it spin_loop.

agree.

Quoted 1 line

- cpu_relax_intercepted: can call it in_spin_loop,

Yes

the original name can

Quoted 2 lines

me "are we intercepting cpu_relax or not" which may be useful at one point (when we know we aren't overcommitted)

Okay.
So are you saying allow vcpu to spin in non over-commit scenarios? So that we avoid all yield_to etc...

( Or even in some other place where it is useful).

Quoted 2 lines

- dy_eligible: I'd like to understand the algorithm more and would want the name to reflect it, will come to this later if I have a better idea.

Yes, welcome any good name.

I had thought eligible initially, but felt it is too generic.

The intention of variable is to decide whether the vcpu is eligible for directed yield, so made it short, and added eligible.

Quoted 3 lines

include/linux/kvm_host.h struct kvm_vcpu { · line 197

Quoted 1 line
include/linux/kvm_host.h +1 -0
+#endif

blank line.

oops! 'll take care of this.

Quoted 6 lines
Quoted 4 lines
include/linux/kvm_host.h +0 -0
  	struct kvm_vcpu_arch arch;
  };
 
#14 lore raw

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

In reply to Avi Kivity
Quoted 33 lines · Avi Kivity
Quoted 32 lines · Raghavendra K T

From: Raghavendra K T<[email protected]>

Currently, on a large vcpu guests, there is a high probability of yielding to the same vcpu who had recently done a pause-loop exit or cpu relax intercepted. Such a yield can lead to the vcpu spinning again and hence degrade the performance.

The patchset keeps track of the pause loop exit/cpu relax interception and gives chance to a vcpu which:
(a) Has not done pause loop exit or cpu relax intercepted at all (probably he is preempted lock-holder)
(b) Was skipped in last iteration because it did pause loop exit or cpu relax intercepted, and probably has become eligible now (next eligible lock holder)

virt/kvm/kvm_main.c +15 -0
+#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
+/*
+ * Helper that checks whether a VCPU is eligible for directed yield.
+ * Most eligible candidate to yield is decided by following heuristics:
+ *
+ *  (a) VCPU which has not done pl-exit or cpu relax intercepted recently
+ *  (preempted lock holder), indicated by @cpu_relax_intercepted.
+ *  Set at the beiginning and cleared at the end of interception/PLE handler.
+ *
+ *  (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
+ *  chance last time (mostly it has become eligible now since we have probably
+ *  yielded to lockholder in last iteration. This is done by toggling
+ *  @dy_eligible each time a VCPU checked for eligibility.)
+ *
+ *  Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
Quoted 9 lines
Quoted 5 lines
+ *  to preempted lock-holder could result in wrong VCPU selection and CPU
+ *  burning. Giving priority for a potential lock-holder increases lock
+ *  progress.

+ */
+bool kvm_vcpu_check_and_update_eligible(struct kvm_vcpu *vcpu)

Predicates' names should give a hint as to what true and false returns mean. For example vcpu_eligible_for_directed_yield().

I agree regarding the Predicate name. My confusion was it was doing more than that (flipping eligible flag).
So, I ll go with kvm_vcpu_eligible_for_directed_yield()

Quoted 1 line
Quoted 1 line

+{

[...]

Quoted 9 lines

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1605

Quoted 2 lines
virt/kvm/kvm_main.c +2 -0
+	return eligible;
+}

You're accessing another vcpu's data structures without any locking. This is probably okay since we're not basing any life or death decisions on this, but a comment would be good to explain to readers that this has been considered and is okay (and why).

True and agree. What we doing here is not worth of locking overhead. will try to explain more on that.

#5 lore raw

Re: [PATCH RFC V4 2/3] kvm: Note down when cpu relax intercepted or pause loop exited

In reply to Raghavendra K T
Quoted 4 lines · Raghavendra K T

So are you saying allow vcpu to spin in non over-commit scenarios? So that we avoid all yield_to etc...

( Or even in some other place where it is useful).

When is yielding useful, if you're not overcommitted?

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

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

In reply to Rik van Riel
Quoted 15 lines · Rik van Riel
Quoted 4 lines · Avi Kivity
Quoted 4 lines
virt/kvm/kvm_main.c +4 -0
+{
+    bool eligible;
+
+    eligible = !vcpu->ple.cpu_relax_intercepted ||
Quoted 8 lines

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1602

Quoted 5 lines
virt/kvm/kvm_main.c +5 -0
+            (vcpu->ple.cpu_relax_intercepted&&
+             vcpu->ple.dy_eligible);
+
+    if (vcpu->ple.cpu_relax_intercepted)
+        vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;

Probably should assign 'true', since the previous value is essentially random.

Quoted 5 lines

I suspect the intended purpose of this conditional is to flip the eligibility of a vcpu for being selected as a direct yield target.

In other words, that bit of the code is correct.

If vcpu A is in a long spin loop and is preempted away, and vcpu B dips several times in kvm_vcpu_on_spin(), then it will act as intended. But if vcpu A is spinning for x% of its time and processing on the other, then vcpu B will flip its dy_eligible for those x%, and not flip it when it's processing. I don't understand how this is useful.

I guess this is an attempt to impose fairness on yielding, and it makes sense to do this, but I don't know if this is the best way to achieve it.

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

Re: [PATCH RFC V4 2/3] kvm: Note down when cpu relax intercepted or pause loop exited

In reply to Avi Kivity
Quoted 8 lines · Avi Kivity
Quoted 4 lines · Raghavendra K T

So are you saying allow vcpu to spin in non over-commit scenarios? So that we avoid all yield_to etc...

( Or even in some other place where it is useful).

When is yielding useful, if you're not overcommitted?

Right. There is no need to do yield_to when run queue has only one task.

#12 lore raw

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

In reply to Avi Kivity
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Rik van Riel
Quoted 12 lines · Avi Kivity

virt/kvm/kvm_main.c bool kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1602

Quoted 9 lines
virt/kvm/kvm_main.c +9 -0
+{
+    bool eligible;
+
+    eligible = !vcpu->ple.cpu_relax_intercepted ||
+            (vcpu->ple.cpu_relax_intercepted&&
+             vcpu->ple.dy_eligible);
+
+    if (vcpu->ple.cpu_relax_intercepted)
+        vcpu->ple.dy_eligible = !vcpu->ple.dy_eligible;

Probably should assign 'true', since the previous value is essentially random.

Quoted 8 lines
Quoted 5 lines

I suspect the intended purpose of this conditional is to flip the eligibility of a vcpu for being selected as a direct yield target.

In other words, that bit of the code is correct.

If vcpu A is in a long spin loop and is preempted away, and vcpu B dips several times in kvm_vcpu_on_spin(), then it will act as intended.

Yes, true.

But

Quoted 3 lines

if vcpu A is spinning for x% of its time and processing on the other, then vcpu B will flip its dy_eligible for those x%, and not flip it when it's processing. I don't understand how this is useful.

Suppose A is doing really good job and and has not done pause loop exit, we will not touch it's dy_eligible flag. Also dy_eligible flag will not prevent B doing yield_to to A.

Suppose A has started spinning in the beginning itself, it will do pause loop exit if it crosses threshold, and we will now start toggling dy_eligible.

Was that you were referring?

And it seems we may still have to set dy_eligible flag to false at the beginning of vcpu_on_spin along with cpu_relax_intercepted = true, like below, so that we do not have spill-over status from previous PL exits.

vcpu_on_spin()
{
cpu_relax_intercepted = true;
dy_eligible = false;
.
.
.

cpu_relax_intercepted = false;
}

Let me know if that addresses your concern.

Quoted 4 lines

I guess this is an attempt to impose fairness on yielding, and it makes sense to do this, but I don't know if this is the best way to achieve it.

#13 lore raw

Re: [PATCH RFC V4 3/3] kvm: Choose better candidate for directed yield

On 07/17/2012 02:39 PM, Raghavendra K T wrote:
[...]

Quoted 28 lines

But

Quoted 3 lines

if vcpu A is spinning for x% of its time and processing on the other, then vcpu B will flip its dy_eligible for those x%, and not flip it when it's processing. I don't understand how this is useful.

Suppose A is doing really good job and and has not done pause loop exit, we will not touch it's dy_eligible flag. Also dy_eligible flag will not prevent B doing yield_to to A.

Suppose A has started spinning in the beginning itself, it will do pause loop exit if it crosses threshold, and we will now start toggling dy_eligible.

Was that you were referring?

And it seems we may still have to set dy_eligible flag to false at the beginning of vcpu_on_spin along with cpu_relax_intercepted = true, like below, so that we do not have spill-over status from previous PL exits.

vcpu_on_spin()
{
cpu_relax_intercepted = true;
dy_eligible = false;
.
.
.

Quoted 5 lines

cpu_relax_intercepted = false;
}

Let me know if that addresses your concern.

Thought you brought in is miraculous. taking care of not having spill-over dy_eligible status is needed for making algorithm technically more correct. will spin V5 with all these changes.

Quoted 5 lines
Quoted 4 lines

I guess this is an attempt to impose fairness on yielding, and it makes sense to do this, but I don't know if this is the best way to achieve it.