File 61122ac6-credit2-avoid-spuriously-picking-idle.patch of Package xen.32200

References: bsc#1179246

# Commit 0f742839ae57e10687e7a573070c37430f31068c
# Date 2021-08-10 09:29:10 +0200
# Author Dario Faggioli <dfaggioli@suse.com>
# Committer Jan Beulich <jbeulich@suse.com>
credit2: avoid picking a spurious idle unit when caps are used

Commit 07b0eb5d0ef0 ("credit2: make sure we pick a runnable unit from the
runq if there is one") did not fix completely the problem of potentially
selecting a scheduling unit that will then not be able to run.

In fact, in case caps are used and the unit we are currently looking
at, during the runqueue scan, does not have enough budget for being run,
we should continue looking instead than giving up and picking the idle
unit.

Suggested-by: George Dunlap <george.dunlap@citrix.com>
Signed-off-by: Dario Faggioli <dfaggioli@suse.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/common/sched_credit2.c
+++ b/xen/common/sched_credit2.c
@@ -3361,6 +3361,15 @@ runq_candidate(struct csched2_runqueue_d
                         (unsigned char *)&d);
         }
 
+        /*
+         * If the vcpu in the runqueue has more credits than current (or than
+         * idle, if current is not runnable) or if current is yielding, we may
+         * want to pick it up. Otherwise, there's no need to keep scanning the
+         * runqueue any further.
+         */
+        if ( !yield && svc->credit <= snext->credit )
+            break;
+
         /* Skip non runnable vcpus that we (temporarily) have in the runq */
         if ( unlikely(!vcpu_runnable(svc->vcpu)) )
             continue;
@@ -3392,16 +3401,25 @@ runq_candidate(struct csched2_runqueue_d
         }
 
         /*
-         * If the one in the runqueue has more credit than current (or idle,
-         * if current is not runnable), or if current is yielding, and also
-         * if the one in runqueue either is not capped, or is capped but has
-         * some budget, then choose it.
+         * If we are here, we are almost sure we want to pick the vcpu in
+         * the runqueue. Last thing we need to check is that it either is
+         * not capped or, if it is, it has some budget.
+         *
+         * Note that budget availability must be the very last check that we
+         * do, in this loop, due to the side effects that vcpu_grab_budget().
+         * causes.
+         *
+         * In fact, if there is budget available in the vcpu's domain's
+         * budget pool, the function will pick some for running this vcpu.
+         * And we clearly want to do that only if we're otherwise sure that
+         * the vcpu will actually run, consume it, and return the leftover
+         * (if any) in the usual way.
          */
-        if ( (yield || svc->credit > snext->credit) &&
-             (!has_cap(svc) || vcpu_grab_budget(svc)) )
-            snext = svc;
+        if ( has_cap(svc) && !vcpu_grab_budget(svc) )
+            continue;
 
         /* In any case, if we got this far, break. */
+        snext = svc;
         break;
     }
 
openSUSE Build Service is sponsored by