Skip to contentSkip to conversation

xen/sched: add fall back to idle vcpu when scheduling unit

PATCH Jan Beulich 3 participants 4 messages Jul 2, 2019 – Jul 3, 2019

lore
Share & tools
lore
#0 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.05.19 at 12:32, <[email protected]> wrote:

When running an idle vcpu in a non-idle scheduling unit use a specific guest idle loop not performing any tasklets, memory scrubbing and livepatching in order to avoid populating the cpu caches with memory used by other domains (as far as possible). Softirqs are considered to be save (timers might want to be excluded, but this can be fine-tuned later).

How could timers be legitimately excluded? And how are softirqs (which similarly can't be excluded here) any less risky than e.g. tasklets?

As to scrubbing - what gets brought into cache is, except for a very brief moment, the value the scrubbing routine actually stores. There's no knowledge to be gained from that by a guest.

Quoted 12 lines
xen/arch/x86/domain.c +6 -0
@@ -159,6 +159,23 @@ static void idle_loop(void)
     }
 }
 
+/*
+ * Idle loop for siblings of active schedule units.
+ * We don't do any standard idle work like tasklets, page scrubbing or
+ * livepatching.
+ */
+static void guest_idle_loop(void)

xen/arch/x86/domain.c static void idle_loop(void) · line 177

Quoted 10 lines
xen/arch/x86/domain.c +10 -0
+{
+    unsigned int cpu = smp_processor_id();
+
+    for ( ; ; )
+    {
+        if ( !softirq_pending(cpu) )
+            sched_guest_idle(pm_idle, cpu);
+        do_softirq();
+    }
+}

In the comment I think you mean "siblings of <whatever> in active schedule units"?

Having had quite some fun with soft-offlining of CPUs recently, may I ask that you ASSERT(!cpu_is_offline(cpu)) in the loop body, such that the absence of a call to play_dead() is also covered?

xen/arch/x86/domain.c void startup_cpu_idle_loop(void) · line 197

Quoted 10 lines
xen/arch/x86/domain.c +4 -0
@@ -172,6 +189,10 @@ void startup_cpu_idle_loop(void)
 
 static void noreturn continue_idle_domain(struct vcpu *v)
 {
+    /* Idle vcpus might be attached to non-idle units! */
+    if ( !is_idle_domain(v->sched_unit->domain) )
+        reset_stack_and_jump(guest_idle_loop);
+
     reset_stack_and_jump(idle_loop);
 }

You're aware that there's a call to check_for_livepatch_work() hidden in reset_stack_and_jump(), which you say you don't want to allow in this context?

Quoted 6 lines
xen/common/schedule.c +0 -0
@@ -82,7 +82,18 @@ static struct scheduler __read_mostly ops;
 static inline struct vcpu *sched_unit2vcpu_cpu(struct sched_unit *unit,
                                                unsigned int cpu)
 {

xen/common/schedule.c static struct scheduler __read_mostly ops; · line 93

Quoted 10 lines
xen/common/schedule.c +9 -1
-    return unit->domain->vcpu[unit->unit_id + per_cpu(sched_res_idx, cpu)];
+    unsigned int idx = unit->unit_id + per_cpu(sched_res_idx, cpu);
+    const struct domain *d = unit->domain;
+    struct vcpu *v;
+
+    if ( idx < d->max_vcpus && d->vcpu[idx] )
+    {
+        v = d->vcpu[idx];
+        if ( v->new_state == RUNSTATE_running )
+            return v;

Isn't this enough of the cache fill half of a gadget to warrant use of array_index_nospec() or alike?

xen/common/schedule.c static inline void vcpu_runstate_change( · line 226

Quoted 7 lines
xen/common/schedule.c +1 -2
@@ -209,19 +223,11 @@ static inline void vcpu_runstate_change(
     v->runstate.state = new_state;
 }
 
-static inline void sched_unit_runstate_change(struct sched_unit *unit,
-    bool running, s_time_t new_entry_time)
+void sched_guest_idle(void (*idle) (void), unsigned int cpu)

Stray blank between closing and opening parenthesis.

Quoted 5 lines
xen/common/schedule.c +0 -4
 {
-    struct vcpu *v;
-
-    for_each_sched_unit_vcpu ( unit, v )
-        if ( running )

xen/common/schedule.c static inline void vcpu_runstate_change( · line 231

Quoted 10 lines
xen/common/schedule.c +3 -6
-            vcpu_runstate_change(v, v->new_state, new_entry_time);
-        else
-            vcpu_runstate_change(v,
-                ((v->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
-                 (vcpu_runnable(v) ? RUNSTATE_runnable : RUNSTATE_offline)),
-                new_entry_time);
+    atomic_inc(&get_sched_res(cpu)->urgent_count);
+    idle();
+    atomic_dec(&get_sched_res(cpu)->urgent_count);
 }

What is "urgent" about an idle vCPU filling an empty sched unit slot? That is, why do you need to prevent the thread from sleeping as power efficiently as possible (potentially allowing the sibling thread to even use more resources)?

xen/common/schedule.c static void sched_switch_units(struct sched_resource *sd, · line 1647

Quoted 12 lines
xen/common/schedule.c +1 -7
@@ -1637,33 +1644,67 @@ static void sched_switch_units(struct sched_resource *sd,
                                struct sched_unit *next, struct sched_unit *prev,
                                s_time_t now)
 {
-    sd->curr = next;
-
-    TRACE_3D(TRC_SCHED_SWITCH_INFPREV, prev->domain->domain_id, prev->unit_id,
-             now - prev->state_entry_time);
-    TRACE_4D(TRC_SCHED_SWITCH_INFNEXT, next->domain->domain_id, next->unit_id,
-             (next->vcpu->runstate.state == RUNSTATE_runnable) ?
-             (now - next->state_entry_time) : 0, prev->next_time);
+    int cpu;

unsigned int

Quoted 15 lines
xen/common/schedule.c +2 -4
@@ -1719,25 +1760,25 @@ static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now,
     if ( prev->next_time >= 0 ) /* -ve means no limit */
         set_timer(&sd->s_timer, now + prev->next_time);
 
-    if ( likely(prev != next) )
-        sched_switch_units(sd, next, prev, now);
+    sched_switch_units(sd, next, prev, now);
 
     return next;
 }
 
-static void context_saved(struct vcpu *prev)
+static void context_saved(struct sched_unit *unit)
 {
-    struct sched_unit *unit = prev->sched_unit;

xen/common/schedule.c static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now, · line 1777

Quoted 10 lines
xen/common/schedule.c +2 -2
-
     unit->is_running = 0;
     unit->state_entry_time = NOW();
+    get_sched_res(smp_processor_id())->prev = NULL;
 
     /* Check for migration request /after/ clearing running flag. */
     smp_mb();
 
-    sched_context_saved(vcpu_scheduler(prev), unit);
+    sched_context_saved(vcpu_scheduler(unit->vcpu), unit);

An example of it unlikely being just one of the vCPU-s in a unit that you actually want to deal with.

Quoted 11 lines
xen/common/schedule.c +2 -1
@@ -1870,7 +1921,8 @@ static void sched_slave(void)
 
     pcpu_schedule_unlock_irq(lock, cpu);
 
-    sched_context_switch(vprev, sched_unit2vcpu_cpu(next, cpu), now);
+    sched_context_switch(vprev, sched_unit2vcpu_cpu(next, cpu),
+                         is_idle_unit(next) && !is_idle_unit(prev), now);
 }
 
 /*

xen/common/schedule.c static void schedule(void) · line 1987

Quoted 7 lines
xen/common/schedule.c +2 -1
@@ -1930,7 +1982,8 @@ static void schedule(void)
     pcpu_schedule_unlock_irq(lock, cpu);
 
     vnext = sched_unit2vcpu_cpu(next, cpu);
-    sched_context_switch(vprev, vnext, now);
+    sched_context_switch(vprev, vnext,
+                         !is_idle_unit(prev) && is_idle_unit(next), now);
 }

Seeing these two calls I'm not only slightly puzzled by the expressions having operands in opposite order wrt one another, but also why the callee can't work out the condition without the new parameter/argument.

xen/include/asm-arm/current.h DECLARE_PER_CPU(struct vcpu *, curr_vcpu); · line 21

Quoted 7 lines
xen/include/asm-arm/current.h +1 -0
@@ -18,6 +18,7 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
 
 #define current            (this_cpu(curr_vcpu))
 #define set_current(vcpu)  do { current = (vcpu); } while (0)
+#define get_cpu_current(cpu)  (per_cpu(curr_vcpu, cpu))

Pointless outer pair of parentheses.

Quoted 15 lines
xen/include/asm-x86/current.h +5 -0
@@ -77,6 +77,11 @@ struct cpu_info {
     /* get_stack_bottom() must be 16-byte aligned */
 };
 
+static inline struct cpu_info *get_cpu_info_from_stack(unsigned long sp)
+{
+    return (struct cpu_info *)((sp | (STACK_SIZE - 1)) + 1) - 1;
+}
+
 static inline struct cpu_info *get_cpu_info(void)
 {
 #ifdef __clang__

xen/include/asm-x86/current.h static inline struct cpu_info *get_cpu_info(void) · line 96

Quoted 6 lines
xen/include/asm-x86/current.h +1 -1
@@ -87,7 +92,7 @@ static inline struct cpu_info *get_cpu_info(void)
     register unsigned long sp asm("rsp");
 #endif
 
-    return (struct cpu_info *)((sp | (STACK_SIZE - 1)) + 1) - 1;
+    return get_cpu_info_from_stack(sp);
 }

With these, why does ...

xen/include/asm-x86/smp.h void set_nr_sockets(void); · line 80

Quoted 8 lines
xen/include/asm-x86/smp.h +2 -0
@@ -76,6 +76,9 @@ void set_nr_sockets(void);
 /* Representing HT and core siblings in each socket. */
 extern cpumask_t **socket_cpumask;
 
+#define get_cpu_current(cpu) \
+    (get_cpu_info_from_stack((unsigned long)stack_base[cpu])->current_vcpu)

... this end up in a different header? (The outermost pair of parentheses isn't strictly needed here.)

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

#1 lore raw
Quoted 3 lines · Jan Beulich

As to scrubbing - what gets brought into cache is, except for a very brief moment, the value the scrubbing routine actually stores. There's no knowledge to be gained from that by a guest.

Unless we scrub with instructions which have Direct Write semantics (we don't), the cacheline gets pulled into L1.

That means its leakable via L1TF for the entire duration between the first speculative touch of the page (especially as prefetching is liable to bring the content in in short order) to the retirement of the instruction.

That said, there is zero difference (from an attackers point of view) between scrubbing a single page as part of alloc_{dom,xen}heap_page(), and scrubbing in the idle loop.

The former is strictly required to stay, therefore, don't see restricting the idle scrubbing as having any impact on security (although it might very well have an impact on performance).

~Andrew

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

#2 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.05.19 at 12:32, <[email protected]> wrote:

When running an idle vcpu in a non-idle scheduling unit use a specific guest idle loop not performing any tasklets, memory scrubbing and livepatching in order to avoid populating the cpu caches with memory used by other domains (as far as possible). Softirqs are considered to be save (timers might want to be excluded, but this can be fine-tuned later).

How could timers be legitimately excluded? And how are softirqs (which similarly can't be excluded here) any less risky than e.g. tasklets?

At least some timers are for other guests. I can drop mentioning timers.

Tasklets are sometimes used for deferred processing of guest specific actions, like continue_hypercall_on_cpu(). This is something we really don't want here.

Quoted 3 lines

As to scrubbing - what gets brought into cache is, except for a very brief moment, the value the scrubbing routine actually stores. There's no knowledge to be gained from that by a guest.

With Andrew's answer going in a similar direction I can add scrubbing again.

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/domain.c +11 -0
@@ -159,6 +159,23 @@ static void idle_loop(void)
      }
  }
 
+/*
+ * Idle loop for siblings of active schedule units.
+ * We don't do any standard idle work like tasklets, page scrubbing or
+ * livepatching.
+ */
+static void guest_idle_loop(void)
+{
+    unsigned int cpu = smp_processor_id();
+
+    for ( ; ; )
+    {
Quoted 8 lines

xen/arch/x86/domain.c static void idle_loop(void) · line 177

Quoted 5 lines
xen/arch/x86/domain.c +5 -0
+        if ( !softirq_pending(cpu) )
+            sched_guest_idle(pm_idle, cpu);
+        do_softirq();
+    }
+}

In the comment I think you mean "siblings of <whatever> in active schedule units"?

Is "siblings of cpus in guest mode" fine?

Quoted 5 lines

Having had quite some fun with soft-offlining of CPUs recently, may I ask that you ASSERT(!cpu_is_offline(cpu)) in the loop body, such that the absence of a call to play_dead() is also covered?

Sure.

Quoted 15 lines

xen/arch/x86/domain.c void startup_cpu_idle_loop(void) · line 197

Quoted 11 lines
xen/arch/x86/domain.c +4 -0
@@ -172,6 +189,10 @@ void startup_cpu_idle_loop(void)
 
  static void noreturn continue_idle_domain(struct vcpu *v)
  {
+    /* Idle vcpus might be attached to non-idle units! */
+    if ( !is_idle_domain(v->sched_unit->domain) )
+        reset_stack_and_jump(guest_idle_loop);
+
      reset_stack_and_jump(idle_loop);
  }

You're aware that there's a call to check_for_livepatch_work() hidden in reset_stack_and_jump(), which you say you don't want to allow in this context?

Good point.

IMO it would be best to have a "no-livepatch" variant of reset_stack_and_jump().

Quoted 12 lines
Quoted 12 lines
xen/common/schedule.c +4 -1
@@ -82,7 +82,18 @@ static struct scheduler __read_mostly ops;
  static inline struct vcpu *sched_unit2vcpu_cpu(struct sched_unit *unit,
                                                 unsigned int cpu)
  {
-    return unit->domain->vcpu[unit->unit_id + per_cpu(sched_res_idx, cpu)];
+    unsigned int idx = unit->unit_id + per_cpu(sched_res_idx, cpu);
+    const struct domain *d = unit->domain;
+    struct vcpu *v;
+
Quoted 8 lines

xen/common/schedule.c static struct scheduler __read_mostly ops; · line 93

Quoted 5 lines
xen/common/schedule.c +5 -0
+    if ( idx < d->max_vcpus && d->vcpu[idx] )
+    {
+        v = d->vcpu[idx];
+        if ( v->new_state == RUNSTATE_running )
+            return v;

Isn't this enough of the cache fill half of a gadget to warrant use of array_index_nospec() or alike?

The input data is in no way user controlled. Do we really want to add barriers before each array access?

Quoted 10 lines

xen/common/schedule.c static inline void vcpu_runstate_change( · line 226

Quoted 8 lines
xen/common/schedule.c +1 -2
@@ -209,19 +223,11 @@ static inline void vcpu_runstate_change(
      v->runstate.state = new_state;
  }
 
-static inline void sched_unit_runstate_change(struct sched_unit *unit,
-    bool running, s_time_t new_entry_time)
+void sched_guest_idle(void (*idle) (void), unsigned int cpu)

Stray blank between closing and opening parenthesis.

Oh, indeed.

Quoted 17 lines
Quoted 6 lines
xen/common/schedule.c +0 -4
  {
-    struct vcpu *v;
-
-    for_each_sched_unit_vcpu ( unit, v )
-        if ( running )

xen/common/schedule.c static inline void vcpu_runstate_change( · line 231

Quoted 10 lines
xen/common/schedule.c +3 -6
-            vcpu_runstate_change(v, v->new_state, new_entry_time);
-        else
-            vcpu_runstate_change(v,
-                ((v->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
-                 (vcpu_runnable(v) ? RUNSTATE_runnable : RUNSTATE_offline)),
-                new_entry_time);
+    atomic_inc(&get_sched_res(cpu)->urgent_count);
+    idle();
+    atomic_dec(&get_sched_res(cpu)->urgent_count);
  }
Quoted 4 lines

What is "urgent" about an idle vCPU filling an empty sched unit slot? That is, why do you need to prevent the thread from sleeping as power efficiently as possible (potentially allowing the sibling thread to even use more resources)?

The deeper the thread is sleeping the longer it will take to wake it up for synchronized context switching. I'd like to avoid additional latencies.

Quoted 15 lines

xen/common/schedule.c static void sched_switch_units(struct sched_resource *sd, · line 1647

Quoted 13 lines
xen/common/schedule.c +1 -7
@@ -1637,33 +1644,67 @@ static void sched_switch_units(struct sched_resource *sd,
                                 struct sched_unit *next, struct sched_unit *prev,
                                 s_time_t now)
  {
-    sd->curr = next;
-
-    TRACE_3D(TRC_SCHED_SWITCH_INFPREV, prev->domain->domain_id, prev->unit_id,
-             now - prev->state_entry_time);
-    TRACE_4D(TRC_SCHED_SWITCH_INFNEXT, next->domain->domain_id, next->unit_id,
-             (next->vcpu->runstate.state == RUNSTATE_runnable) ?
-             (now - next->state_entry_time) : 0, prev->next_time);
+    int cpu;

unsigned int

Okay.

Quoted 21 lines
Quoted 21 lines
xen/common/schedule.c +3 -5
@@ -1719,25 +1760,25 @@ static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now,
      if ( prev->next_time >= 0 ) /* -ve means no limit */
          set_timer(&sd->s_timer, now + prev->next_time);
 
-    if ( likely(prev != next) )
-        sched_switch_units(sd, next, prev, now);
+    sched_switch_units(sd, next, prev, now);
 
      return next;
  }
 
-static void context_saved(struct vcpu *prev)
+static void context_saved(struct sched_unit *unit)
  {
-    struct sched_unit *unit = prev->sched_unit;
-
      unit->is_running = 0;
      unit->state_entry_time = NOW();
+    get_sched_res(smp_processor_id())->prev = NULL;
 
Quoted 8 lines

xen/common/schedule.c static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now, · line 1777

Quoted 5 lines
xen/common/schedule.c +1 -1
      /* Check for migration request /after/ clearing running flag. */
      smp_mb();
 
-    sched_context_saved(vcpu_scheduler(prev), unit);
+    sched_context_saved(vcpu_scheduler(unit->vcpu), unit);

An example of it unlikely being just one of the vCPU-s in a unit that you actually want to deal with.

No. All vcpus of a unit share the same scheduler.

OTOH I think there is some room for tuning here: vcpu_scheduler is doing quite some work to find the correct struct scheduler. Replacing it by unit_scheduler() might be a good idea.

Quoted 14 lines
Quoted 14 lines
xen/common/schedule.c +2 -1
@@ -1870,7 +1921,8 @@ static void sched_slave(void)
 
      pcpu_schedule_unlock_irq(lock, cpu);
 
-    sched_context_switch(vprev, sched_unit2vcpu_cpu(next, cpu), now);
+    sched_context_switch(vprev, sched_unit2vcpu_cpu(next, cpu),
+                         is_idle_unit(next) && !is_idle_unit(prev), now);
  }
 
  /*
@@ -1930,7 +1982,8 @@ static void schedule(void)
      pcpu_schedule_unlock_irq(lock, cpu);
 
Quoted 8 lines

xen/common/schedule.c static void schedule(void) · line 1987

Quoted 5 lines
xen/common/schedule.c +2 -1
      vnext = sched_unit2vcpu_cpu(next, cpu);
-    sched_context_switch(vprev, vnext, now);
+    sched_context_switch(vprev, vnext,
+                         !is_idle_unit(prev) && is_idle_unit(next), now);
  }

Seeing these two calls I'm not only slightly puzzled by the expressions having operands in opposite order wrt one another, but also why the

Ah, yes, will change one of the calls.

Quoted 1 line

callee can't work out the condition without the new parameter/argument.

The next patch adds other users of sched_context_switch().

Quoted 10 lines

xen/include/asm-arm/current.h DECLARE_PER_CPU(struct vcpu *, curr_vcpu); · line 21

Quoted 8 lines
xen/include/asm-arm/current.h +1 -0
@@ -18,6 +18,7 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
 
  #define current            (this_cpu(curr_vcpu))
  #define set_current(vcpu)  do { current = (vcpu); } while (0)
+#define get_cpu_current(cpu)  (per_cpu(curr_vcpu, cpu))

Pointless outer pair of parentheses.

Okay.

Quoted 31 lines
Quoted 16 lines
xen/include/asm-x86/current.h +5 -0
@@ -77,6 +77,11 @@ struct cpu_info {
      /* get_stack_bottom() must be 16-byte aligned */
  };
 
+static inline struct cpu_info *get_cpu_info_from_stack(unsigned long sp)
+{
+    return (struct cpu_info *)((sp | (STACK_SIZE - 1)) + 1) - 1;
+}
+
  static inline struct cpu_info *get_cpu_info(void)
  {
  #ifdef __clang__

xen/include/asm-x86/current.h static inline struct cpu_info *get_cpu_info(void) · line 96

Quoted 6 lines
xen/include/asm-x86/current.h +1 -1
@@ -87,7 +92,7 @@ static inline struct cpu_info *get_cpu_info(void)
      register unsigned long sp asm("rsp");
  #endif
 
-    return (struct cpu_info *)((sp | (STACK_SIZE - 1)) + 1) - 1;
+    return get_cpu_info_from_stack(sp);
  }

With these, why does ...

Quoted 6 lines
xen/include/asm-x86/smp.h +0 -0
@@ -76,6 +76,9 @@ void set_nr_sockets(void);
  /* Representing HT and core siblings in each socket. */
  extern cpumask_t **socket_cpumask;
 
Quoted 5 lines

xen/include/asm-x86/smp.h void set_nr_sockets(void); · line 80

Quoted 2 lines
xen/include/asm-x86/smp.h +2 -0
+#define get_cpu_current(cpu) \
+    (get_cpu_info_from_stack((unsigned long)stack_base[cpu])->current_vcpu)

... this end up in a different header? (The outermost pair of parentheses isn't strictly needed here.)

Hmm, must be a leftover from a previous version. Will move it.

Juergen

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

#3 lore raw
Quoted 13 lines · Juergen Gross
Quoted 11 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.05.19 at 12:32, <[email protected]> wrote:

When running an idle vcpu in a non-idle scheduling unit use a specific guest idle loop not performing any tasklets, memory scrubbing and livepatching in order to avoid populating the cpu caches with memory used by other domains (as far as possible). Softirqs are considered to be save (timers might want to be excluded, but this can be fine-tuned later).

How could timers be legitimately excluded? And how are softirqs (which similarly can't be excluded here) any less risky than e.g. tasklets?

Quoted 6 lines

At least some timers are for other guests. I can drop mentioning timers.

Tasklets are sometimes used for deferred processing of guest specific actions, like continue_hypercall_on_cpu(). This is something we really don't want here.

Yet what softirqs act on you won't know either.

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/x86/domain.c +11 -0
@@ -159,6 +159,23 @@ static void idle_loop(void)
       }
   }
+/*
+ * Idle loop for siblings of active schedule units.
+ * We don't do any standard idle work like tasklets, page scrubbing or
+ * livepatching.
+ */
+static void guest_idle_loop(void)
+{
+    unsigned int cpu = smp_processor_id();
+
+    for ( ; ; )
+    {
Quoted 8 lines

xen/arch/x86/domain.c static void idle_loop(void) · line 176

Quoted 5 lines
xen/arch/x86/domain.c +5 -0
+        if ( !softirq_pending(cpu) )
+            sched_guest_idle(pm_idle, cpu);
+        do_softirq();
+    }
+}

In the comment I think you mean "siblings of <whatever> in active schedule units"?

Quoted 1 line

Is "siblings of cpus in guest mode" fine?

Sure. As would perhaps be "siblings in active schedule units".

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/domain.c +2 -0
@@ -172,6 +189,10 @@ void startup_cpu_idle_loop(void)
   static void noreturn continue_idle_domain(struct vcpu *v)
   {
+    /* Idle vcpus might be attached to non-idle units! */
+    if ( !is_idle_domain(v->sched_unit->domain) )
Quoted 8 lines

xen/arch/x86/domain.c void startup_cpu_idle_loop(void) · line 196

Quoted 4 lines
xen/arch/x86/domain.c +2 -0
+        reset_stack_and_jump(guest_idle_loop);
+
       reset_stack_and_jump(idle_loop);
   }

You're aware that there's a call to check_for_livepatch_work() hidden in reset_stack_and_jump(), which you say you don't want to allow in this context?

Quoted 4 lines

Good point.

IMO it would be best to have a "no-livepatch" variant of reset_stack_and_jump().

That's what I too was thinking.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
xen/common/schedule.c +4 -1
@@ -82,7 +82,18 @@ static struct scheduler __read_mostly ops;
   static inline struct vcpu *sched_unit2vcpu_cpu(struct sched_unit *unit,
                                                  unsigned int cpu)
   {
-    return unit->domain->vcpu[unit->unit_id + per_cpu(sched_res_idx, cpu)];
+    unsigned int idx = unit->unit_id + per_cpu(sched_res_idx, cpu);
+    const struct domain *d = unit->domain;
+    struct vcpu *v;
+
Quoted 8 lines

xen/common/schedule.c static struct scheduler __read_mostly ops; · line 93

Quoted 5 lines
xen/common/schedule.c +5 -0
+    if ( idx < d->max_vcpus && d->vcpu[idx] )
+    {
+        v = d->vcpu[idx];
+        if ( v->new_state == RUNSTATE_running )
+            return v;

Isn't this enough of the cache fill half of a gadget to warrant use of array_index_nospec() or alike?

Quoted 2 lines

The input data is in no way user controlled. Do we really want to add barriers before each array access?

array_index_nospec() does not involve any barriers. Indeed I'd have been more hesitant to suggest a change here if that would involve adding a barrier.

As to "in no way guest controlled" - did you perhaps see the discussion regarding speculative NULL derefs that Norbert and I had on his grant table changes? If NULL can plausibly be found in d->vcpu[idx] (after all you check for it), then a PV guest could control what a speculative deref would produce as data, and hence what might be used for further indirection.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
xen/common/schedule.c +2 -12
@@ -209,19 +223,11 @@ static inline void vcpu_runstate_change(
       v->runstate.state = new_state;
   }
-static inline void sched_unit_runstate_change(struct sched_unit *unit,
-    bool running, s_time_t new_entry_time)
+void sched_guest_idle(void (*idle) (void), unsigned int cpu)
   {
-    struct vcpu *v;
-
-    for_each_sched_unit_vcpu ( unit, v )
-        if ( running )
-            vcpu_runstate_change(v, v->new_state, new_entry_time);
-        else
-            vcpu_runstate_change(v,
-                ((v->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
-                 (vcpu_runnable(v) ? RUNSTATE_runnable : RUNSTATE_offline)),
-                new_entry_time);
+    atomic_inc(&get_sched_res(cpu)->urgent_count);
Quoted 8 lines

xen/common/schedule.c static inline void vcpu_runstate_change( · line 230

Quoted 3 lines
xen/common/schedule.c +2 -0
+    idle();
+    atomic_dec(&get_sched_res(cpu)->urgent_count);
   }

What is "urgent" about an idle vCPU filling an empty sched unit slot? That is, why do you need to prevent the thread from sleeping as power efficiently as possible (potentially allowing the sibling thread to even use more resources)?

Quoted 3 lines

The deeper the thread is sleeping the longer it will take to wake it up for synchronized context switching. I'd like to avoid additional latencies.

Whether to trade latencies for power savings imo is a decision to be made by an admin, not us. Use of C-states incurs higher latencies elsewhere as well, so if they're deemed a problem, use of deeper C-states could be disallowed via already available command line options.

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/common/schedule.c +0 -0
@@ -1719,25 +1760,25 @@ static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now,

      if ( prev->next_time >= 0 ) /* -ve means no limit */ set_timer(&sd->s_timer, now + prev->next_time); -    if ( likely(prev != next) )
-        sched_switch_units(sd, next, prev, now); +    sched_switch_units(sd, next, prev, now); return next;
  }

xen/common/schedule.c +1 -1
-static void context_saved(struct vcpu *prev)
+static void context_saved(struct sched_unit *unit)

  {
-    struct sched_unit *unit = prev->sched_unit;
-
      unit->is_running = 0;
      unit->state_entry_time = NOW();

Quoted 8 lines
Quoted 5 lines

+    get_sched_res(smp_processor_id())->prev = NULL;
      /* Check for migration request /after/ clearing running flag. */ smp_mb();
-    sched_context_saved(vcpu_scheduler(prev), unit);
+    sched_context_saved(vcpu_scheduler(unit->vcpu), unit);

An example of it unlikely being just one of the vCPU-s in a unit that you actually want to deal with.

Quoted 1 line

No. All vcpus of a unit share the same scheduler.

Oh, right.

Quoted 3 lines

OTOH I think there is some room for tuning here: vcpu_scheduler is doing quite some work to find the correct struct scheduler. Replacing it by unit_scheduler() might be a good idea.

Yes please, if such is available; otherwise I'd have suggested to have it available. If all vCPU-s of a unit share the same scheduler (which is to be expected, i.e. I should have paid more attention to the further context above), why would vcpu_scheduler() be any more expensive than unit_scheduler() anyway? It ought to simply be unit_scheduler(v->unit).

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