diff mbox series

[v4,10/46] xen/sched: add scheduler helpers hiding vcpu

Message ID 20190927070050.12405-11-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series xen: add core scheduling support | expand

Commit Message

Jürgen Groß Sept. 27, 2019, 7 a.m. UTC
Add the following helpers using a sched_unit as input instead of a
vcpu:

- is_idle_unit() similar to is_idle_vcpu()
- is_unit_online() similar to is_vcpu_online() (returns true when any
  of its vcpus is online)
- unit_runnable() like vcpu_runnable() (returns true if any of its
  vcpus is runnable)
- sched_set_res() to set the current scheduling resource of a unit
- sched_unit_cpu() to get the current processor of a unit (returns
  the master_cpu of the scheduling resource of a unit)
- sched_{set|clear}_pause_flags[_atomic]() to modify pause_flags of the
  associated vcpu(s) (modifies the pause_flags of all vcpus of the
  unit)
- sched_idle_unit() to get the sched_unit pointer of the idle vcpu of a
  specific physical cpu

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V3:
- make unit parameter of sched_unit_cpu() const
V4:
- loop over vcpus in helpers where appropriate (Jan Beulich)
- add some comments regarding semantics (Jan Beulich)
- rename sched_unit_cpu() to sched_unit_master() (Jan Beulich)
---
 xen/common/sched_credit.c  |  3 +-
 xen/common/schedule.c      | 21 +++++------
 xen/include/xen/sched-if.h | 92 ++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 99 insertions(+), 17 deletions(-)

Comments

Dario Faggioli Sept. 27, 2019, 8:52 a.m. UTC | #1
On Fri, 2019-09-27 at 09:00 +0200, Juergen Gross wrote:
> Add the following helpers using a sched_unit as input instead of a
> vcpu:
> 
> - is_idle_unit() similar to is_idle_vcpu()
> - is_unit_online() similar to is_vcpu_online() (returns true when any
>   of its vcpus is online)
> - unit_runnable() like vcpu_runnable() (returns true if any of its
>   vcpus is runnable)
> - sched_set_res() to set the current scheduling resource of a unit
> - sched_unit_cpu() to get the current processor of a unit (returns
>
    ^sched_unit_master() ?

>   the master_cpu of the scheduling resource of a unit)
> - sched_{set|clear}_pause_flags[_atomic]() to modify pause_flags of
> the
>   associated vcpu(s) (modifies the pause_flags of all vcpus of the
>   unit)
> - sched_idle_unit() to get the sched_unit pointer of the idle vcpu of
> a
>   specific physical cpu
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
>
With that fixed:

Reviewed-by: Dario Faggioli <dfaggioli@suse.com>

Regards
Jürgen Groß Sept. 27, 2019, 9:01 a.m. UTC | #2
On 27.09.19 10:52, Dario Faggioli wrote:
> On Fri, 2019-09-27 at 09:00 +0200, Juergen Gross wrote:
>> Add the following helpers using a sched_unit as input instead of a
>> vcpu:
>>
>> - is_idle_unit() similar to is_idle_vcpu()
>> - is_unit_online() similar to is_vcpu_online() (returns true when any
>>    of its vcpus is online)
>> - unit_runnable() like vcpu_runnable() (returns true if any of its
>>    vcpus is runnable)
>> - sched_set_res() to set the current scheduling resource of a unit
>> - sched_unit_cpu() to get the current processor of a unit (returns
>>
>      ^sched_unit_master() ?

Yes, had seen that already.

> 
>>    the master_cpu of the scheduling resource of a unit)
>> - sched_{set|clear}_pause_flags[_atomic]() to modify pause_flags of
>> the
>>    associated vcpu(s) (modifies the pause_flags of all vcpus of the
>>    unit)
>> - sched_idle_unit() to get the sched_unit pointer of the idle vcpu of
>> a
>>    specific physical cpu
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>
> With that fixed:
> 
> Reviewed-by: Dario Faggioli <dfaggioli@suse.com>

Thanks,


Juergen
diff mbox series

Patch

diff --git a/xen/common/sched_credit.c b/xen/common/sched_credit.c
index cb7b9c5d91..56e47d5e54 100644
--- a/xen/common/sched_credit.c
+++ b/xen/common/sched_credit.c
@@ -1660,8 +1660,7 @@  csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
             SCHED_STAT_CRANK(migrate_queued);
             WARN_ON(vc->is_urgent);
             runq_remove(speer);
-            vc->processor = cpu;
-            vc->sched_unit->res = get_sched_res(cpu);
+            sched_set_res(vc->sched_unit, get_sched_res(cpu));
             /*
              * speer will start executing directly on cpu, without having to
              * go through runq_insert(). So we must update the runnable count
diff --git a/xen/common/schedule.c b/xen/common/schedule.c
index 747dfc0eab..6d27c2f4a5 100644
--- a/xen/common/schedule.c
+++ b/xen/common/schedule.c
@@ -84,7 +84,7 @@  static spinlock_t *
 sched_idle_switch_sched(struct scheduler *new_ops, unsigned int cpu,
                         void *pdata, void *vdata)
 {
-    idle_vcpu[cpu]->sched_unit->priv = NULL;
+    sched_idle_unit(cpu)->priv = NULL;
 
     return &sched_free_cpu_lock;
 }
@@ -377,12 +377,11 @@  int sched_init_vcpu(struct vcpu *v, unsigned int processor)
     struct domain *d = v->domain;
     struct sched_unit *unit;
 
-    v->processor = processor;
-
     if ( (unit = sched_alloc_unit(v)) == NULL )
         return 1;
 
-    unit->res = get_sched_res(processor);
+    sched_set_res(unit, get_sched_res(processor));
+
     /* Initialise the per-vcpu timers. */
     spin_lock_init(&v->periodic_timer_lock);
     init_timer(&v->periodic_timer, vcpu_periodic_timer_fn,
@@ -497,8 +496,7 @@  int sched_move_domain(struct domain *d, struct cpupool *c)
 
         sched_set_affinity(v, &cpumask_all, &cpumask_all);
 
-        v->processor = new_p;
-        v->sched_unit->res = get_sched_res(new_p);
+        sched_set_res(v->sched_unit, get_sched_res(new_p));
         /*
          * With v->processor modified we must not
          * - make any further changes assuming we hold the scheduler lock,
@@ -818,8 +816,9 @@  void restore_vcpu_affinity(struct domain *d)
         spinlock_t *lock;
         unsigned int old_cpu = v->processor;
         struct sched_unit *unit = v->sched_unit;
+        struct sched_resource *res;
 
-        ASSERT(!vcpu_runnable(v));
+        ASSERT(!unit_runnable(unit));
 
         /*
          * Re-assign the initial processor as after resume we have no
@@ -852,15 +851,15 @@  void restore_vcpu_affinity(struct domain *d)
             }
         }
 
-        v->processor = cpumask_any(cpumask_scratch_cpu(cpu));
-        unit->res = get_sched_res(v->processor);
+        res = get_sched_res(cpumask_any(cpumask_scratch_cpu(cpu)));
+        sched_set_res(unit, res);
 
         spin_unlock_irq(lock);
 
         /* v->processor might have changed, so reacquire the lock. */
         lock = unit_schedule_lock_irq(unit);
-        unit->res = sched_pick_resource(vcpu_scheduler(v), unit);
-        v->processor = unit->res->master_cpu;
+        res = sched_pick_resource(vcpu_scheduler(v), unit);
+        sched_set_res(unit, res);
         spin_unlock_irq(lock);
 
         if ( old_cpu != v->processor )
diff --git a/xen/include/xen/sched-if.h b/xen/include/xen/sched-if.h
index 6e5d813d6c..0587a8d3d2 100644
--- a/xen/include/xen/sched-if.h
+++ b/xen/include/xen/sched-if.h
@@ -63,6 +63,93 @@  static inline struct sched_unit *curr_on_cpu(unsigned int cpu)
     return get_sched_res(cpu)->curr;
 }
 
+static inline bool is_idle_unit(const struct sched_unit *unit)
+{
+    return is_idle_vcpu(unit->vcpu_list);
+}
+
+/* Returns true if at least one vcpu of the unit is online. */
+static inline bool is_unit_online(const struct sched_unit *unit)
+{
+    const struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        if ( is_vcpu_online(v) )
+            return true;
+
+    return false;
+}
+
+/* Returns true if at least one vcpu of the unit is runnable. */
+static inline bool unit_runnable(const struct sched_unit *unit)
+{
+    const struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        if ( vcpu_runnable(v) )
+            return true;
+
+    return false;
+}
+
+static inline void sched_set_res(struct sched_unit *unit,
+                                 struct sched_resource *res)
+{
+    unit->vcpu_list->processor = res->master_cpu;
+    unit->res = res;
+}
+
+/* Return master cpu of the scheduling resource the unit is assigned to. */
+static inline unsigned int sched_unit_master(const struct sched_unit *unit)
+{
+    return unit->res->master_cpu;
+}
+
+/* Set a bit in pause_flags of all vcpus of a unit. */
+static inline void sched_set_pause_flags(struct sched_unit *unit,
+                                         unsigned int bit)
+{
+    struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        __set_bit(bit, &v->pause_flags);
+}
+
+/* Clear a bit in pause_flags of all vcpus of a unit. */
+static inline void sched_clear_pause_flags(struct sched_unit *unit,
+                                           unsigned int bit)
+{
+    struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        __clear_bit(bit, &v->pause_flags);
+}
+
+/* Set a bit in pause_flags of all vcpus of a unit via atomic updates. */
+static inline void sched_set_pause_flags_atomic(struct sched_unit *unit,
+                                                unsigned int bit)
+{
+    struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        set_bit(bit, &v->pause_flags);
+}
+
+/* Clear a bit in pause_flags of all vcpus of a unit via atomic updates. */
+static inline void sched_clear_pause_flags_atomic(struct sched_unit *unit,
+                                                  unsigned int bit)
+{
+    struct vcpu *v;
+
+    for_each_sched_unit_vcpu ( unit, v )
+        clear_bit(bit, &v->pause_flags);
+}
+
+static inline struct sched_unit *sched_idle_unit(unsigned int cpu)
+{
+    return idle_vcpu[cpu]->sched_unit;
+}
+
 /*
  * Scratch space, for avoiding having too many cpumask_t on the stack.
  * Within each scheduler, when using the scratch mask of one pCPU:
@@ -349,10 +436,7 @@  static inline void sched_migrate(const struct scheduler *s,
     if ( s->migrate )
         s->migrate(s, unit, cpu);
     else
-    {
-        unit->vcpu_list->processor = cpu;
-        unit->res = get_sched_res(cpu);
-    }
+        sched_set_res(unit, get_sched_res(cpu));
 }
 
 static inline struct sched_resource *sched_pick_resource(