Skip to contentSkip to conversation

xen/sched: avoid cpumasks on stack in sched/core.c

PATCH In review Juergen Gross 2 participants 3 messages Jan 23, 2020 – Jan 24, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/sched: avoid cpumasks on stack in sched/core.c

There are still several instances of cpumask_t on the stack in scheduling code. Avoid them as far as possible.

---
 xen/common/sched/core.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
xen/common/sched/core.c +5 -9
index 944164d78a..73799c2508 100644
@@ -1178,7 +1178,6 @@ int cpu_disable_scheduler(unsigned int cpu)
 {
     struct domain *d;
     const struct cpupool *c;
-    cpumask_t online_affinity;
     int ret = 0;
 
     rcu_read_lock(&sched_res_rculock);
@@ -1196,8 +1195,7 @@ int cpu_disable_scheduler(unsigned int cpu)
             unsigned long flags;
             spinlock_t *lock = unit_schedule_lock_irqsave(unit, &flags);
 
-            cpumask_and(&online_affinity, unit->cpu_hard_affinity, c->cpu_valid);
-            if ( cpumask_empty(&online_affinity) &&
+            if ( !cpumask_intersects(unit->cpu_hard_affinity, c->cpu_valid) &&
                  cpumask_test_cpu(cpu, unit->cpu_hard_affinity) )
             {
                 if ( sched_check_affinity_broken(unit) )
@@ -1336,12 +1334,10 @@ static int vcpu_set_affinity(
 
 int vcpu_set_hard_affinity(struct vcpu *v, const cpumask_t *affinity)
 {
-    cpumask_t online_affinity;
     cpumask_t *online;
 
     online = VCPU2ONLINE(v);
-    cpumask_and(&online_affinity, affinity, online);
-    if ( cpumask_empty(&online_affinity) )
+    if ( !cpumask_intersects(online, affinity) )
         return -EINVAL;
 
     return vcpu_set_affinity(v, affinity, v->sched_unit->cpu_hard_affinity);
@@ -2586,11 +2582,11 @@ static void schedule(void)
 
     if ( gran > 1 )
     {
-        cpumask_t mask;
+        cpumask_t *mask = cpumask_scratch_cpu(cpu);
 
         prev->rendezvous_in_cnt = gran;
-        cpumask_andnot(&mask, sr->cpus, cpumask_of(cpu));
-        cpumask_raise_softirq(&mask, SCHED_SLAVE_SOFTIRQ);
+        cpumask_andnot(mask, sr->cpus, cpumask_of(cpu));
+        cpumask_raise_softirq(mask, SCHED_SLAVE_SOFTIRQ);
         next = sched_wait_rendezvous_in(prev, &lock, cpu, now);
         if ( !next )
             return;
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Juergen Gross

There are still several instances of cpumask_t on the stack in scheduling code. Avoid them as far as possible.

Just curious...

Quoted 5 lines
xen/common/sched/core.c +0 -0
@@ -2586,11 +2582,11 @@ static void schedule(void)
 
     if ( gran > 1 )

xen/common/sched/core.c static void schedule(void) · line 2590

Quoted 10 lines
xen/common/sched/core.c +3 -3
     {
-        cpumask_t mask;
+        cpumask_t *mask = cpumask_scratch_cpu(cpu);
 
         prev->rendezvous_in_cnt = gran;
-        cpumask_andnot(&mask, sr->cpus, cpumask_of(cpu));
-        cpumask_raise_softirq(&mask, SCHED_SLAVE_SOFTIRQ);
+        cpumask_andnot(mask, sr->cpus, cpumask_of(cpu));
+        cpumask_raise_softirq(mask, SCHED_SLAVE_SOFTIRQ);
 

... why are we keeping the temporary variable (mask) ?

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 19 lines · Dario Faggioli
Quoted 5 lines · Juergen Gross

There are still several instances of cpumask_t on the stack in scheduling code. Avoid them as far as possible.

Just curious...

Quoted 9 lines
xen/common/sched/core.c +1 -1
@@ -2586,11 +2582,11 @@ static void schedule(void)
 
      if ( gran > 1 )
      {
-        cpumask_t mask;
+        cpumask_t *mask = cpumask_scratch_cpu(cpu);
 
Quoted 7 lines

xen/common/sched/core.c static void schedule(void)

Quoted 6 lines
xen/common/sched/core.c +2 -2
          prev->rendezvous_in_cnt = gran;
-        cpumask_andnot(&mask, sr->cpus, cpumask_of(cpu));
-        cpumask_raise_softirq(&mask, SCHED_SLAVE_SOFTIRQ);
+        cpumask_andnot(mask, sr->cpus, cpumask_of(cpu));
+        cpumask_raise_softirq(mask, SCHED_SLAVE_SOFTIRQ);
 

... why are we keeping the temporary variable (mask) ?

per_cpu accesses are more expensive than those to local variables. mask is used twice.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel