Skip to content

Commit c45c528

Browse files
ktraghavendraGleb Natapov
authored andcommitted
kvm: Handle yield_to failure return code for potential undercommit case
yield_to returns -ESRCH, When source and target of yield_to run queue length is one. When we see three successive failures of yield_to we assume we are in potential undercommit case and abort from PLE handler. The assumption is backed by low probability of wrong decision for even worst case scenarios such as average runqueue length between 1 and 2. More detail on rationale behind using three tries: if p is the probability of finding rq length one on a particular cpu, and if we do n tries, then probability of exiting ple handler is: p^(n+1) [ because we would have come across one source with rq length 1 and n target cpu rqs with length 1 ] so num tries: probability of aborting ple handler (1.5x overcommit) 1 1/4 2 1/8 3 1/16 We can increase this probability with more tries, but the problem is the overhead. Also, If we have tried three times that means we would have iterated over 3 good eligible vcpus along with many non-eligible candidates. In worst case if we iterate all the vcpus, we reduce 1x performance and overcommit performance get hit. note that we do not update last boosted vcpu in failure cases. Thank Avi for raising question on aborting after first fail from yield_to. Reviewed-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Signed-off-by: Raghavendra K T <raghavendra.kt@linux.vnet.ibm.com> Tested-by: Chegu Vinod <chegu_vinod@hp.com> Signed-off-by: Gleb Natapov <gleb@redhat.com>
1 parent 7b270f6 commit c45c528

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎virt/kvm/kvm_main.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,24 +1694,23 @@ bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
16941694
{
16951695
struct pid *pid;
16961696
struct task_struct *task = NULL;
1697+
bool ret = false;
16971698

16981699
rcu_read_lock();
16991700
pid = rcu_dereference(target->pid);
17001701
if (pid)
17011702
task = get_pid_task(target->pid, PIDTYPE_PID);
17021703
rcu_read_unlock();
17031704
if (!task)
1704-
return false;
1705+
return ret;
17051706
if (task->flags & PF_VCPU) {
17061707
put_task_struct(task);
1707-
return false;
1708-
}
1709-
if (yield_to(task, 1)) {
1710-
put_task_struct(task);
1711-
return true;
1708+
return ret;
17121709
}
1710+
ret = yield_to(task, 1);
17131711
put_task_struct(task);
1714-
return false;
1712+
1713+
return ret;
17151714
}
17161715
EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
17171716

@@ -1752,12 +1751,14 @@ bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
17521751
return eligible;
17531752
}
17541753
#endif
1754+
17551755
void kvm_vcpu_on_spin(struct kvm_vcpu *me)
17561756
{
17571757
struct kvm *kvm = me->kvm;
17581758
struct kvm_vcpu *vcpu;
17591759
int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
17601760
int yielded = 0;
1761+
int try = 3;
17611762
int pass;
17621763
int i;
17631764

@@ -1769,7 +1770,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
17691770
* VCPU is holding the lock that we need and will release it.
17701771
* We approximate round-robin by starting at the last boosted VCPU.
17711772
*/
1772-
for (pass = 0; pass < 2 && !yielded; pass++) {
1773+
for (pass = 0; pass < 2 && !yielded && try; pass++) {
17731774
kvm_for_each_vcpu(i, vcpu, kvm) {
17741775
if (!pass && i <= last_boosted_vcpu) {
17751776
i = last_boosted_vcpu;
@@ -1782,10 +1783,15 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
17821783
continue;
17831784
if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
17841785
continue;
1785-
if (kvm_vcpu_yield_to(vcpu)) {
1786+
1787+
yielded = kvm_vcpu_yield_to(vcpu);
1788+
if (yielded > 0) {
17861789
kvm->last_boosted_vcpu = i;
1787-
yielded = 1;
17881790
break;
1791+
} else if (yielded < 0) {
1792+
try--;
1793+
if (!try)
1794+
break;
17891795
}
17901796
}
17911797
}

0 commit comments

Comments
 (0)