Skip to contentSkip to conversation

xen/sched: refactor urgent count update

PATCH In review Ruslan Ruslichenko 2 participants 3 messages Sep 25 – Sep 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/sched: refactor urgent count update

From: Ruslan Ruslichenko <[email protected]>

With current implementation whenever vCPU blocks on waiting for event channel via SCHEDOP_poll hypercall, scheduler sets 'is_urgent' flag. The flag is cleared on vCPU runstate change, if it is no longer in 'VPF_blocked' state or its vcpu_id is not in poll_mask.

However, the only way for vCPU runstate to change in polled state is when it's going to wakeup.

Thus when 'is_urgent' is set, inner condition will always be:

if ( unlikely(v->is_urgent) )

{
    if ( !(v->pause_flags & VPF_blocked) ||            // Always True
         !test_bit(v->vcpu_id, v->domain->poll_mask) ) // Never evaluated

This makes the second condition dead code.

Thus simplify overall code structure and remove dead code.

The patch has no functional change.

---
 xen/common/sched/core.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
xen/common/sched/core.c +11 -15
index 77cbfd5226..22b3cd1007 100644
@@ -241,26 +241,22 @@ static inline void trace_continue_running(const struct vcpu *v)
 
 static inline void vcpu_urgent_count_update(struct vcpu *v)
 {
+    bool cur_urgent;
+
     if ( is_idle_vcpu(v) )
         return;
 
-    if ( unlikely(v->is_urgent) )
-    {
-        if ( !(v->pause_flags & VPF_blocked) ||
-             !test_bit(v->vcpu_id, v->domain->poll_mask) )
-        {
-            v->is_urgent = 0;
-            atomic_dec(&per_cpu(sched_urgent_count, v->processor));
-        }
-    }
-    else
+    cur_urgent = (v->pause_flags & VPF_blocked) &&
+                 test_bit(v->vcpu_id, v->domain->poll_mask);
+
+    if ( unlikely(v->is_urgent != cur_urgent) )
     {
-        if ( unlikely(v->pause_flags & VPF_blocked) &&
-             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
-        {
-            v->is_urgent = 1;
+        v->is_urgent = cur_urgent;
+
+        if ( cur_urgent )
             atomic_inc(&per_cpu(sched_urgent_count, v->processor));
-        }
+        else
+            atomic_dec(&per_cpu(sched_urgent_count, v->processor));
     }
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Ruslan Ruslichenko

From: Ruslan Ruslichenko <[email protected]>

With current implementation whenever vCPU blocks on waiting for event channel via SCHEDOP_poll hypercall, scheduler sets 'is_urgent' flag. The flag is cleared on vCPU runstate change, if it is no longer in 'VPF_blocked' state or its vcpu_id is not in poll_mask.

However, the only way for vCPU runstate to change in polled state is when it's going to wakeup.

Quoted 6 lines

Thus when 'is_urgent' is set, inner condition will always be:

if ( unlikely(v->is_urgent) )

{
    if ( !(v->pause_flags & VPF_blocked) ||            // Always True
         !test_bit(v->vcpu_id, v->domain->poll_mask) ) // Never evaluated

Maybe this invariant indeed applies, but then there must be more to it. Simply from taking the "else" branch here

        if ( unlikely(v->pause_flags & VPF_blocked) &&
             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
        {
            v->is_urgent = 1;
            atomic_inc(&per_cpu(sched_urgent_count, v->processor));
        }

clearly immediately afterwards the invariant you name does not hold.

Quoted 27 lines
xen/common/sched/core.c +6 -11
@@ -241,26 +241,22 @@ static inline void trace_continue_running(const struct vcpu *v)
 
 static inline void vcpu_urgent_count_update(struct vcpu *v)
 {
+    bool cur_urgent;
+
     if ( is_idle_vcpu(v) )
         return;
 
-    if ( unlikely(v->is_urgent) )
-    {
-        if ( !(v->pause_flags & VPF_blocked) ||
-             !test_bit(v->vcpu_id, v->domain->poll_mask) )
-        {
-            v->is_urgent = 0;
-            atomic_dec(&per_cpu(sched_urgent_count, v->processor));
-        }
-    }
-    else
+    cur_urgent = (v->pause_flags & VPF_blocked) &&
+                 test_bit(v->vcpu_id, v->domain->poll_mask);
+
+    if ( unlikely(v->is_urgent != cur_urgent) )
     {
-        if ( unlikely(v->pause_flags & VPF_blocked) &&

xen/common/sched/core.c static inline void trace_continue_running(const struct vcpu *v) · line 259

Quoted 10 lines
xen/common/sched/core.c +5 -4
-             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
-        {
-            v->is_urgent = 1;
+        v->is_urgent = cur_urgent;
+
+        if ( cur_urgent )
             atomic_inc(&per_cpu(sched_urgent_count, v->processor));
-        }
+        else
+            atomic_dec(&per_cpu(sched_urgent_count, v->processor));

Assuming the transformation is valid to make (scheduler maintainers will need to judge), may I suggest to fold these two into a single atomic_add(), passing in either +1 or -1?

Jan

author
#2 lore raw

Resending since Google web client mangled the formatting of previous mail.

Quoted 22 lines · Jan Beulich
Quoted 11 lines · Ruslan Ruslichenko

From: Ruslan Ruslichenko <[email protected]>

With current implementation whenever vCPU blocks on waiting for event channel via SCHEDOP_poll hypercall, scheduler sets 'is_urgent' flag. The flag is cleared on vCPU runstate change, if it is no longer in 'VPF_blocked' state or its vcpu_id is not in poll_mask.

However, the only way for vCPU runstate to change in polled state is when it's going to wakeup.

Quoted 6 lines

Thus when 'is_urgent' is set, inner condition will always be:

if ( unlikely(v->is_urgent) )

{
    if ( !(v->pause_flags & VPF_blocked) ||            // Always True
         !test_bit(v->vcpu_id, v->domain->poll_mask) ) // Never evaluated

Maybe this invariant indeed applies, but then there must be more to it. Simply from taking the "else" branch here

Quoted 9 lines
        if ( unlikely(v->pause_flags & VPF_blocked) &&
             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
        {
            v->is_urgent = 1;
            atomic_inc(&per_cpu(sched_urgent_count, v->processor));
        }

clearly immediately afterwards the invariant you name does not hold.

I should have described the preconditions for this. vcpu_urgent_count_update() only called by vcpu_runstate_change(), which returns early if 'v->runstate.state == new_state'.
v->is_urgent set means vCPU is in RUNSTATE_blocked, so the only change from RUNSTATE_blocked
is to RUNSTATE_runnable or RUNSTATE_offline, and both require _VPF_blocked clear.
Also vcpu_unblock() clears _VPF_blocked first and it is used by wakeup
sources: event_channel, irqs and poll_timer itself.

I will refine this in the commit message.

Quoted 33 lines
Quoted 33 lines
xen/common/sched/core.c +9 -14
@@ -241,26 +241,22 @@ static inline void trace_continue_running(const struct vcpu *v)
 
 static inline void vcpu_urgent_count_update(struct vcpu *v)
 {
+    bool cur_urgent;
+
     if ( is_idle_vcpu(v) )
         return;
 
-    if ( unlikely(v->is_urgent) )
-    {
-        if ( !(v->pause_flags & VPF_blocked) ||
-             !test_bit(v->vcpu_id, v->domain->poll_mask) )
-        {
-            v->is_urgent = 0;
-            atomic_dec(&per_cpu(sched_urgent_count, v->processor));
-        }
-    }
-    else
+    cur_urgent = (v->pause_flags & VPF_blocked) &&
+                 test_bit(v->vcpu_id, v->domain->poll_mask);
+
+    if ( unlikely(v->is_urgent != cur_urgent) )
     {
-        if ( unlikely(v->pause_flags & VPF_blocked) &&
-             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
-        {
-            v->is_urgent = 1;
+        v->is_urgent = cur_urgent;
+
+        if ( cur_urgent )
Quoted 9 lines

xen/common/sched/core.c static inline void trace_continue_running(const struct vcpu *v) · line 259

Quoted 4 lines
xen/common/sched/core.c +2 -1
             atomic_inc(&per_cpu(sched_urgent_count, v->processor));
-        }
+        else
+            atomic_dec(&per_cpu(sched_urgent_count, v->processor));

Assuming the transformation is valid to make (scheduler maintainers will need to judge), may I suggest to fold these two into a single atomic_add(), passing in either +1 or -1?

This looks like a good improvement, thanks. I will update for v2.

--
Best Regards,
Ruslan

Quoted 1 line

Jan